+ private void onShutDown() {
+ LOG.debug("{}: onShutDown", persistenceId());
+
+ if(shuttingDown) {
+ return;
+ }
+
+ shuttingDown = true;
+
+ final RaftActorBehavior currentBehavior = context.getCurrentBehavior();
+ if (currentBehavior.state() != RaftState.Leader) {
+ // For non-leaders shutdown is a no-op
+ self().tell(PoisonPill.getInstance(), self());
+ return;
+ }
+
+ if (context.hasFollowers()) {
+ initiateLeadershipTransfer(new RaftActorLeadershipTransferCohort.OnComplete() {
+ @Override
+ public void onSuccess(ActorRef raftActorRef) {
+ LOG.debug("{}: leader transfer succeeded - sending PoisonPill", persistenceId());
+ raftActorRef.tell(PoisonPill.getInstance(), raftActorRef);
+ }
+
+ @Override
+ public void onFailure(ActorRef raftActorRef) {
+ LOG.debug("{}: leader transfer failed - sending PoisonPill", persistenceId());
+ raftActorRef.tell(PoisonPill.getInstance(), raftActorRef);
+ }
+ });
+ } else {
+ pauseLeader(new TimedRunnable(context.getConfigParams().getElectionTimeOutInterval(), this) {
+ @Override
+ protected void doRun() {
+ self().tell(PoisonPill.getInstance(), self());
+ }
+
+ @Override
+ protected void doCancel() {
+ self().tell(PoisonPill.getInstance(), self());
+ }
+ });
+ }
+ }
+
+ private void onLeaderTransitioning() {
+ LOG.debug("{}: onLeaderTransitioning", persistenceId());
+ Optional<ActorRef> roleChangeNotifier = getRoleChangeNotifier();
+ if(getRaftState() == RaftState.Follower && roleChangeNotifier.isPresent()) {
+ roleChangeNotifier.get().tell(newLeaderStateChanged(getId(), null,
+ getCurrentBehavior().getLeaderPayloadVersion()), getSelf());
+ }
+ }
+
+ private void switchBehavior(SwitchBehavior message) {
+ if(!getRaftActorContext().getRaftPolicy().automaticElectionsEnabled()) {
+ RaftState newState = message.getNewState();
+ if( newState == RaftState.Leader || newState == RaftState.Follower) {
+ switchBehavior(reusableSwitchBehaviorSupplier.handleMessage(getSender(), message));
+ getRaftActorContext().getTermInformation().updateAndPersist(message.getNewTerm(), "");
+ } else {
+ LOG.warn("Switching to behavior : {} - not supported", newState);
+ }
+ }
+ }
+
+ private void switchBehavior(Supplier<RaftActorBehavior> supplier){
+ reusableBehaviorStateHolder.init(getCurrentBehavior());
+
+ setCurrentBehavior(supplier.get());
+
+ handleBehaviorChange(reusableBehaviorStateHolder, getCurrentBehavior());
+ }
+
+ @VisibleForTesting
+ RaftActorSnapshotMessageSupport newRaftActorSnapshotMessageSupport() {
+ return new RaftActorSnapshotMessageSupport(context, getRaftActorSnapshotCohort());