Bug 4105: Add EntityOwnershipShard
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / Shard.java
index 5a10c3e961bc8e038a0dd3969134b903aace2a56..7b34f5df6097ce4ea00c49233f3f4a4a8e5c7728 100644 (file)
@@ -162,14 +162,8 @@ public class Shard extends RaftActor {
                 datastoreContext.getShardTransactionCommitTimeoutInSeconds(), TimeUnit.SECONDS) / 2;
     }
 
-    public static Props props(final ShardIdentifier name,
-        final Map<String, String> peerAddresses,
-        final DatastoreContext datastoreContext, final SchemaContext schemaContext) {
-        Preconditions.checkNotNull(name, "name should not be null");
-        Preconditions.checkNotNull(peerAddresses, "peerAddresses should not be null");
-        Preconditions.checkNotNull(datastoreContext, "dataStoreContext should not be null");
-        Preconditions.checkNotNull(schemaContext, "schemaContext should not be null");
-
+    public static Props props(final ShardIdentifier name, final Map<String, String> peerAddresses,
+            final DatastoreContext datastoreContext, final SchemaContext schemaContext) {
         return Props.create(new ShardCreator(name, peerAddresses, datastoreContext, schemaContext));
     }
 
@@ -221,7 +215,7 @@ public class Shard extends RaftActor {
 
         if(context.error().isPresent()){
             LOG.trace("{} : AppendEntriesReply failed to arrive at the expected interval {}", persistenceId(),
-                    context.error());
+                context.error());
         }
 
         try {
@@ -243,9 +237,9 @@ public class Shard extends RaftActor {
             } else if (CloseTransactionChain.SERIALIZABLE_CLASS.isInstance(message)) {
                 closeTransactionChain(CloseTransactionChain.fromSerializable(message));
             } else if (message instanceof RegisterChangeListener) {
-                changeSupport.onMessage((RegisterChangeListener) message, isLeader());
+                changeSupport.onMessage((RegisterChangeListener) message, isLeader(), hasLeader());
             } else if (message instanceof RegisterDataTreeChangeListener) {
-                treeChangeSupport.onMessage((RegisterDataTreeChangeListener) message, isLeader());
+                treeChangeSupport.onMessage((RegisterDataTreeChangeListener) message, isLeader(), hasLeader());
             } else if (message instanceof UpdateSchemaContext) {
                 updateSchemaContext((UpdateSchemaContext) message);
             } else if (message instanceof PeerAddressResolved) {
@@ -271,6 +265,10 @@ public class Shard extends RaftActor {
         }
     }
 
+    private boolean hasLeader() {
+        return getLeaderId() != null;
+    }
+
     public int getPendingTxCommitQueueSize() {
         return commitCoordinator.getQueueSize();
     }
@@ -287,7 +285,7 @@ public class Shard extends RaftActor {
                 leaderPayloadVersion);
     }
 
-    private void onDatastoreContext(DatastoreContext context) {
+    protected void onDatastoreContext(DatastoreContext context) {
         datastoreContext = context;
 
         commitCoordinator.setQueueCapacity(datastoreContext.getShardTransactionCommitQueueCapacity());
@@ -655,8 +653,9 @@ public class Shard extends RaftActor {
     @Override
     protected void onStateChanged() {
         boolean isLeader = isLeader();
-        changeSupport.onLeadershipChange(isLeader);
-        treeChangeSupport.onLeadershipChange(isLeader);
+        boolean hasLeader = hasLeader();
+        changeSupport.onLeadershipChange(isLeader, hasLeader);
+        treeChangeSupport.onLeadershipChange(isLeader, hasLeader);
 
         // If this actor is no longer the leader close all the transaction chains
         if (!isLeader) {
@@ -685,22 +684,29 @@ public class Shard extends RaftActor {
         return commitCoordinator;
     }
 
+    protected abstract static class AbstractShardCreator implements Creator<Shard> {
+        private static final long serialVersionUID = 1L;
+
+        protected final ShardIdentifier name;
+        protected final Map<String, String> peerAddresses;
+        protected final DatastoreContext datastoreContext;
+        protected final SchemaContext schemaContext;
 
-    private static class ShardCreator implements Creator<Shard> {
+        protected AbstractShardCreator(final ShardIdentifier name, final Map<String, String> peerAddresses,
+                final DatastoreContext datastoreContext, final SchemaContext schemaContext) {
+            this.name = Preconditions.checkNotNull(name, "name should not be null");
+            this.peerAddresses = Preconditions.checkNotNull(peerAddresses, "peerAddresses should not be null");
+            this.datastoreContext = Preconditions.checkNotNull(datastoreContext, "dataStoreContext should not be null");
+            this.schemaContext = Preconditions.checkNotNull(schemaContext, "schemaContext should not be null");
+        }
+    }
 
+    private static class ShardCreator extends AbstractShardCreator {
         private static final long serialVersionUID = 1L;
 
-        final ShardIdentifier name;
-        final Map<String, String> peerAddresses;
-        final DatastoreContext datastoreContext;
-        final SchemaContext schemaContext;
-
         ShardCreator(final ShardIdentifier name, final Map<String, String> peerAddresses,
                 final DatastoreContext datastoreContext, final SchemaContext schemaContext) {
-            this.name = name;
-            this.peerAddresses = peerAddresses;
-            this.datastoreContext = datastoreContext;
-            this.schemaContext = schemaContext;
+            super(name, peerAddresses, datastoreContext, schemaContext);
         }
 
         @Override