+ boolean isLeader = isLeader();
+ LOG.debug("{}: onStateChanged: isLeader: {}, hasLeader: {}", persistenceId(), isLeader, hasLeader());
+
+ // Examine current RAFT state to see if we are in jeopardy, potentially notifying all listeners
+ final boolean inJeopardy = inJeopardy(getRaftState());
+ final boolean wasInJeopardy = listenerSupport.setInJeopardy(inJeopardy);
+ if (inJeopardy != wasInJeopardy) {
+ LOG.debug("{}: {} jeopardy state, notifying all listeners", persistenceId(),
+ inJeopardy ? "entered" : "left");
+ notifyAllListeners();
+ }
+
+ commitCoordinator.onStateChanged(this, isLeader);
+ }
+
+ @Override
+ protected void onLeaderChanged(String oldLeader, String newLeader) {
+ super.onLeaderChanged(oldLeader, newLeader);
+
+ boolean isLeader = isLeader();
+ LOG.debug("{}: onLeaderChanged: oldLeader: {}, newLeader: {}, isLeader: {}", persistenceId(), oldLeader,
+ newLeader, isLeader);
+
+ if(isLeader) {
+
+ // Clear all existing strategies so that they get re-created when we call createStrategy again
+ // This allows the strategies to be re-initialized with existing statistics maintained by
+ // EntityOwnershipStatistics
+ strategyConfig.clearStrategies();
+
+ // Remove the candidates for all members that are known to be down. In a cluster which has greater than
+ // 3 nodes it is possible for a some node beside the leader being down when the leadership transitions
+ // it makes sense to use this event to remove all the candidates for those downed nodes
+ for(String downPeerName : downPeerMemberNames){
+ removeCandidateFromEntities(downPeerName);
+ }
+ } else {
+ // The leader changed - notify the coordinator to check if pending modifications need to be sent.
+ // While onStateChanged also does this, this method handles the case where the shard hears from a
+ // leader and stays in the follower state. In that case no behavior state change occurs.
+ commitCoordinator.onStateChanged(this, isLeader);
+ }