Fix issues with LeastLoadedCandidateSelectionStrategy
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardRecoveryCoordinator.java
index 5c9f0d11c6584378e4c3a9ff4d46fabe597a80ef..634121f5e5e669b5038246876ea0228b454edfe5 100644 (file)
@@ -11,7 +11,6 @@ import com.google.common.base.Preconditions;
 import java.io.IOException;
 import java.net.URI;
 import java.util.Set;
-import org.opendaylight.controller.cluster.datastore.modification.ModificationPayload;
 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
 import org.opendaylight.controller.cluster.datastore.node.utils.transformer.NormalizedNodePruner;
 import org.opendaylight.controller.cluster.datastore.utils.PruningDataTreeModification;
@@ -22,9 +21,7 @@ import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Compos
 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.slf4j.Logger;
@@ -40,15 +37,18 @@ import org.slf4j.Logger;
  */
 class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
     private static final YangInstanceIdentifier ROOT = YangInstanceIdentifier.builder().build();
-    private final DataTree store;
+    private final ShardDataTree store;
     private final String shardName;
     private final Logger log;
     private final Set<URI> validNamespaces;
     private PruningDataTreeModification transaction;
     private int size;
+    private final byte[] restoreFromSnapshot;
 
-    ShardRecoveryCoordinator(ShardDataTree store, SchemaContext schemaContext, String shardName, Logger log) {
-        this.store = store.getDataTree();
+    ShardRecoveryCoordinator(ShardDataTree store, SchemaContext schemaContext, byte[] restoreFromSnapshot,
+            String shardName, Logger log) {
+        this.store = Preconditions.checkNotNull(store);
+        this.restoreFromSnapshot = restoreFromSnapshot;
         this.shardName = shardName;
         this.log = log;
         this.validNamespaces = NormalizedNodePruner.namespaces(schemaContext);
@@ -57,7 +57,7 @@ class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
     @Override
     public void startLogRecoveryBatch(int maxBatchSize) {
         log.debug("{}: starting log recovery batch with max size {}", shardName, maxBatchSize);
-        transaction = new PruningDataTreeModification(store.takeSnapshot().newModification(), validNamespaces);
+        transaction = new PruningDataTreeModification(store.newModification(), validNamespaces);
         size = 0;
     }
 
@@ -69,10 +69,6 @@ class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
             if (payload instanceof DataTreeCandidatePayload) {
                 DataTreeCandidates.applyToModification(transaction, ((DataTreeCandidatePayload)payload).getCandidate());
                 size++;
-            } else if (payload instanceof ModificationPayload) {
-                MutableCompositeModification.fromSerializable(
-                    ((ModificationPayload) payload).getModification()).apply(transaction);
-                size++;
             } else if (payload instanceof CompositeModificationPayload) {
                 MutableCompositeModification.fromSerializable(
                     ((CompositeModificationPayload) payload).getModification()).apply(transaction);
@@ -84,16 +80,13 @@ class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
             } else {
                 log.error("{}: Unknown payload {} received during recovery", shardName, payload);
             }
-        } catch (IOException | ClassNotFoundException e) {
+        } catch (IOException e) {
             log.error("{}: Error extracting ModificationPayload", shardName, e);
         }
     }
 
     private void commitTransaction(PruningDataTreeModification tx) throws DataValidationFailedException {
-        DataTreeModification delegate = tx.getDelegate();
-        delegate.ready();
-        store.validate(delegate);
-        store.commit(store.prepare(delegate));
+        store.commit(tx.getDelegate());
     }
 
     /**
@@ -122,7 +115,7 @@ class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
         log.debug("{}: Applying recovered snapshot", shardName);
 
         final NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
-        final PruningDataTreeModification tx = new PruningDataTreeModification(store.takeSnapshot().newModification(), validNamespaces);
+        final PruningDataTreeModification tx = new PruningDataTreeModification(store.newModification(), validNamespaces);
         tx.write(ROOT, node);
         try {
             commitTransaction(tx);
@@ -130,4 +123,9 @@ class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
             log.error("{}: Failed to apply recovery snapshot", shardName, e);
         }
     }
+
+    @Override
+    public byte[] getRestoreFromSnapshot() {
+        return restoreFromSnapshot;
+    }
 }