BUG 8447: Add shard getRole rpcs
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / shardmanager / ShardInformation.java
index 262eb6d246493d697870e1e61676e714d5b1cc6b..0c892295a77888d5c635c380183db1b2fa67d10b 100644 (file)
@@ -72,8 +72,8 @@ final class ShardInformation {
 
     Props newProps(SchemaContext schemaContext) {
         Preconditions.checkNotNull(builder);
-        Props props = builder.id(shardId).peerAddresses(initialPeerAddresses).datastoreContext(datastoreContext).
-                schemaContext(schemaContext).props();
+        Props props = builder.id(shardId).peerAddresses(initialPeerAddresses).datastoreContext(datastoreContext)
+                .schemaContext(schemaContext).props();
         builder = null;
         return props;
     }
@@ -83,7 +83,7 @@ final class ShardInformation {
     }
 
     @Nullable
-    ActorRef getActor(){
+    ActorRef getActor() {
         return actor;
     }
 
@@ -115,14 +115,12 @@ final class ShardInformation {
         }
     }
 
-    void updatePeerAddress(String peerId, String peerAddress, ActorRef sender){
+    void updatePeerAddress(String peerId, String peerAddress, ActorRef sender) {
         LOG.info("updatePeerAddress for peer {} with address {}", peerId, peerAddress);
 
-        if(actor != null) {
-            if(LOG.isDebugEnabled()) {
-                LOG.debug("Sending PeerAddressResolved for peer {} with address {} to {}",
-                        peerId, peerAddress, actor.path());
-            }
+        if (actor != null) {
+            LOG.debug("Sending PeerAddressResolved for peer {} with address {} to {}", peerId,
+                    peerAddress, actor.path());
 
             actor.tell(new PeerAddressResolved(peerId, peerAddress), sender);
         }
@@ -131,13 +129,13 @@ final class ShardInformation {
     }
 
     void peerDown(MemberName memberName, String peerId, ActorRef sender) {
-        if(actor != null) {
+        if (actor != null) {
             actor.tell(new PeerDown(memberName, peerId), sender);
         }
     }
 
     void peerUp(MemberName memberName, String peerId, ActorRef sender) {
-        if(actor != null) {
+        if (actor != null) {
             actor.tell(new PeerUp(memberName, peerId), sender);
         }
     }
@@ -147,8 +145,9 @@ final class ShardInformation {
     }
 
     boolean isShardReadyWithLeaderId() {
-        return leaderAvailable && isShardReady() && !RaftState.IsolatedLeader.name().equals(role) &&
-                (isLeader() || addressResolver.resolve(leaderId) != null);
+        return leaderAvailable && isShardReady() && !RaftState.IsolatedLeader.name().equals(role)
+                && !RaftState.PreLeader.name().equals(role)
+                && (isLeader() || addressResolver.resolve(leaderId) != null);
     }
 
     boolean isShardInitialized() {
@@ -160,7 +159,7 @@ final class ShardInformation {
     }
 
     String getSerializedLeaderActor() {
-        if(isLeader()) {
+        if (isLeader()) {
             return Serialization.serializedActorPath(getActor());
         } else {
             return addressResolver.resolve(leaderId);
@@ -176,7 +175,7 @@ final class ShardInformation {
     }
 
     private void notifyOnShardInitializedCallbacks() {
-        if(onShardInitializedSet.isEmpty()) {
+        if (onShardInitializedSet.isEmpty()) {
             return;
         }
 
@@ -186,7 +185,7 @@ final class ShardInformation {
             ready ? "ready" : "initialized", onShardInitializedSet.size());
 
         Iterator<OnShardInitialized> iter = onShardInitializedSet.iterator();
-        while(iter.hasNext()) {
+        while (iter.hasNext()) {
             OnShardInitialized onShardInitialized = iter.next();
             if (!(onShardInitialized instanceof OnShardReady) || ready) {
                 iter.remove();
@@ -210,14 +209,18 @@ final class ShardInformation {
         notifyOnShardInitializedCallbacks();
     }
 
-    void setFollowerSyncStatus(boolean syncStatus){
+    String getRole() {
+        return role;
+    }
+
+    void setFollowerSyncStatus(boolean syncStatus) {
         this.followerSyncStatus = syncStatus;
     }
 
-    boolean isInSync(){
-        if(RaftState.Follower.name().equals(this.role)){
+    boolean isInSync() {
+        if (RaftState.Follower.name().equals(this.role)) {
             return followerSyncStatus;
-        } else if(RaftState.Leader.name().equals(this.role)){
+        } else if (RaftState.Leader.name().equals(this.role)) {
             return true;
         }
 
@@ -225,9 +228,9 @@ final class ShardInformation {
     }
 
     boolean setLeaderId(String leaderId) {
-        boolean changed = !Objects.equals(this.leaderId, leaderId);
+        final boolean changed = !Objects.equals(this.leaderId, leaderId);
         this.leaderId = leaderId;
-        if(leaderId != null) {
+        if (leaderId != null) {
             this.leaderAvailable = true;
         }
         notifyOnShardInitializedCallbacks();
@@ -242,7 +245,7 @@ final class ShardInformation {
     void setLeaderAvailable(boolean leaderAvailable) {
         this.leaderAvailable = leaderAvailable;
 
-        if(leaderAvailable) {
+        if (leaderAvailable) {
             notifyOnShardInitializedCallbacks();
         }
     }
@@ -262,4 +265,4 @@ final class ShardInformation {
     void setActiveMember(boolean isActiveMember) {
         this.isActiveMember = isActiveMember;
     }
-}
\ No newline at end of file
+}