Reset snapshot progress on IOExceptions
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / config / PrefixShardConfiguration.java
index 6b79d548f91dfbc0a848fe9c63607c719bb17bcd..c387fe4ba247ae22ec9bb4350ebba945c7169c5d 100644 (file)
@@ -8,19 +8,68 @@
 
 package org.opendaylight.controller.cluster.datastore.config;
 
-import akka.cluster.ddata.ReplicatedData;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableSet;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
 import java.io.Serializable;
+import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashSet;
 import org.opendaylight.controller.cluster.access.concepts.MemberName;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
 
 /**
  * Configuration for prefix based shards.
  */
-public class PrefixShardConfiguration implements ReplicatedData, Serializable {
+public class PrefixShardConfiguration implements Serializable {
+    private static final class Proxy implements Externalizable {
+        private static final long serialVersionUID = 1L;
+
+        private PrefixShardConfiguration prefixShardConfiguration;
+
+        // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
+        // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
+        @SuppressWarnings("checkstyle:RedundantModifier")
+        public Proxy() {
+        }
+
+        Proxy(final PrefixShardConfiguration prefixShardConfiguration) {
+            this.prefixShardConfiguration = prefixShardConfiguration;
+        }
+
+        @Override
+        public void writeExternal(final ObjectOutput objectOutput) throws IOException {
+            objectOutput.writeObject(prefixShardConfiguration.getPrefix());
+            objectOutput.writeObject(prefixShardConfiguration.getShardStrategyName());
+
+            objectOutput.writeInt(prefixShardConfiguration.getShardMemberNames().size());
+            for (MemberName name : prefixShardConfiguration.getShardMemberNames()) {
+                name.writeTo(objectOutput);
+            }
+        }
+
+        @Override
+        public void readExternal(final ObjectInput objectInput) throws IOException, ClassNotFoundException {
+            final DOMDataTreeIdentifier localPrefix = (DOMDataTreeIdentifier) objectInput.readObject();
+            final String localStrategyName = (String) objectInput.readObject();
+
+            final int size = objectInput.readInt();
+            final Collection<MemberName> localShardMemberNames = new ArrayList<>(size);
+            for (int i = 0; i < size; i++) {
+                localShardMemberNames.add(MemberName.readFrom(objectInput));
+            }
+
+            prefixShardConfiguration = new PrefixShardConfiguration(localPrefix, localStrategyName,
+                    localShardMemberNames);
+        }
+
+        private Object readResolve() {
+            return prefixShardConfiguration;
+        }
+    }
+
     private static final long serialVersionUID = 1L;
 
     private final DOMDataTreeIdentifier prefix;
@@ -57,23 +106,7 @@ public class PrefixShardConfiguration implements ReplicatedData, Serializable {
                 + '}';
     }
 
-    public String toDataMapKey() {
-        return "prefix=" + prefix;
-    }
-
-    @Override
-    public ReplicatedData merge(final ReplicatedData replicatedData) {
-        if (!(replicatedData instanceof PrefixShardConfiguration)) {
-            throw new IllegalStateException("replicatedData expected to be instance of PrefixShardConfiguration");
-        }
-        final PrefixShardConfiguration entry = (PrefixShardConfiguration) replicatedData;
-        if (!entry.getPrefix().equals(prefix)) {
-            // this should never happen since the key is the prefix
-            // if it does just return current?
-            return this;
-        }
-        final HashSet<MemberName> members = new HashSet<>(shardMemberNames);
-        members.addAll(entry.getShardMemberNames());
-        return new PrefixShardConfiguration(prefix, shardStrategyName, members);
+    private Object writeReplace() {
+        return new Proxy(this);
     }
 }