X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2FRaftActorLeadershipTransferCohort.java;h=32790d0f47251af840193ebfe77fa51c4702ffd1;hb=d796a8de8b208ca24bb57aebfc689f8be8bc2c7b;hp=623fa4902ca6fc45ce5d86630ef620afde3b9c10;hpb=4e000b89c3b5ac555cb1e2c39e999a8633b48a96;p=controller.git
diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorLeadershipTransferCohort.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorLeadershipTransferCohort.java
index 623fa4902c..32790d0f47 100644
--- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorLeadershipTransferCohort.java
+++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorLeadershipTransferCohort.java
@@ -26,6 +26,7 @@ import scala.concurrent.duration.FiniteDuration;
/**
* A raft actor support class that participates in leadership transfer. An instance is created upon
* initialization of leadership transfer.
+ *
*
* The transfer process is as follows:
*
@@ -35,32 +36,32 @@ import scala.concurrent.duration.FiniteDuration;
* their local RoleChangeNotifiers.
* - Call {@link RaftActor#pauseLeader} passing this RaftActorLeadershipTransferCohort
* instance. This allows derived classes to perform work prior to transferring leadership.
- * - When the pause is complete, the {@link #run} method is called which in turn calls
- * {@link Leader#transferLeadership}.
+ * - When the pause is complete, the run method is called which in turn calls
+ * {@link Leader#transferLeadership(RaftActorLeadershipTransferCohort)}.
* - The Leader calls {@link #transferComplete} on successful completion.
* - Wait a short period of time for the new leader to be elected to give the derived class a chance to
* possibly complete work that was suspended while we were transferring.
* - On notification of the new leader from the RaftActor or on time out, notify {@link OnComplete} callbacks.
*
+ *
*
* NOTE: All methods on this class must be called on the actor's thread dispatcher as they may access/modify
* internal state.
*
* @author Thomas Pantelis
*/
-public class RaftActorLeadershipTransferCohort implements Runnable {
+public class RaftActorLeadershipTransferCohort {
private static final Logger LOG = LoggerFactory.getLogger(RaftActorLeadershipTransferCohort.class);
private final RaftActor raftActor;
- private final ActorRef replyTo;
private Cancellable newLeaderTimer;
private final List onCompleteCallbacks = new ArrayList<>();
private long newLeaderTimeoutInMillis = 2000;
private final Stopwatch transferTimer = Stopwatch.createUnstarted();
+ private boolean isTransferring;
- RaftActorLeadershipTransferCohort(RaftActor raftActor, ActorRef replyTo) {
+ RaftActorLeadershipTransferCohort(RaftActor raftActor) {
this.raftActor = raftActor;
- this.replyTo = replyTo;
}
void init() {
@@ -70,30 +71,41 @@ public class RaftActorLeadershipTransferCohort implements Runnable {
transferTimer.start();
Optional roleChangeNotifier = raftActor.getRoleChangeNotifier();
- if(roleChangeNotifier.isPresent()) {
+ if (roleChangeNotifier.isPresent()) {
roleChangeNotifier.get().tell(raftActor.newLeaderStateChanged(context.getId(), null,
currentBehavior.getLeaderPayloadVersion()), raftActor.self());
}
- LeaderTransitioning leaderTransitioning = new LeaderTransitioning();
- for(String peerId: context.getPeerIds()) {
+ for (String peerId: context.getPeerIds()) {
ActorSelection followerActor = context.getPeerActorSelection(peerId);
- if(followerActor != null) {
- followerActor.tell(leaderTransitioning, context.getActor());
+ if (followerActor != null) {
+ followerActor.tell(new LeaderTransitioning(context.getId()), context.getActor());
}
}
- raftActor.pauseLeader(this);
+ raftActor.pauseLeader(new TimedRunnable(context.getConfigParams().getElectionTimeOutInterval(), raftActor) {
+ @Override
+ protected void doRun() {
+ doTransfer();
+ }
+
+ @Override
+ protected void doCancel() {
+ LOG.debug("{}: pauseLeader timed out - aborting transfer", raftActor.persistenceId());
+ abortTransfer();
+ }
+ });
}
/**
- * This method is invoked to run the leadership transfer.
+ * This method is invoked to perform the leadership transfer.
*/
- @Override
- public void run() {
+ @VisibleForTesting
+ void doTransfer() {
RaftActorBehavior behavior = raftActor.getCurrentBehavior();
// Sanity check...
- if(behavior instanceof Leader) {
+ if (behavior instanceof Leader) {
+ isTransferring = true;
((Leader)behavior).transferLeadership(this);
} else {
LOG.debug("{}: No longer the leader - skipping transfer", raftActor.persistenceId());
@@ -123,20 +135,17 @@ public class RaftActorLeadershipTransferCohort implements Runnable {
// Add a timer in case we don't get a leader change - 2 sec should be plenty of time if a new
// leader is elected. Note: the Runnable is sent as a message to the raftActor which executes it
- // safely run on actor's thread dispatcher.
+ // safely run on the actor's thread dispatcher.
FiniteDuration timeout = FiniteDuration.create(newLeaderTimeoutInMillis, TimeUnit.MILLISECONDS);
newLeaderTimer = raftActor.getContext().system().scheduler().scheduleOnce(timeout, raftActor.self(),
- new Runnable() {
- @Override
- public void run() {
- LOG.debug("{}: leader not elected in time", raftActor.persistenceId());
- finish(true);
- }
- }, raftActor.getContext().system().dispatcher(), raftActor.self());
+ (Runnable) () -> {
+ LOG.debug("{}: leader not elected in time", raftActor.persistenceId());
+ finish(true);
+ }, raftActor.getContext().system().dispatcher(), raftActor.self());
}
void onNewLeader(String newLeader) {
- if(newLeader != null && newLeaderTimer != null) {
+ if (newLeader != null && newLeaderTimer != null) {
LOG.debug("{}: leader changed to {}", raftActor.persistenceId(), newLeader);
newLeaderTimer.cancel();
finish(true);
@@ -144,22 +153,22 @@ public class RaftActorLeadershipTransferCohort implements Runnable {
}
private void finish(boolean success) {
- if(transferTimer.isRunning()) {
+ isTransferring = false;
+ if (transferTimer.isRunning()) {
transferTimer.stop();
- if(success) {
+ if (success) {
LOG.info("{}: Successfully transferred leadership to {} in {}", raftActor.persistenceId(),
- raftActor.getLeaderId(), transferTimer.toString());
+ raftActor.getLeaderId(), transferTimer);
} else {
- LOG.info("{}: Failed to transfer leadership in {}", raftActor.persistenceId(),
- transferTimer.toString());
+ LOG.warn("{}: Failed to transfer leadership in {}", raftActor.persistenceId(), transferTimer);
}
}
- for(OnComplete onComplete: onCompleteCallbacks) {
- if(success) {
- onComplete.onSuccess(raftActor.self(), replyTo);
+ for (OnComplete onComplete: onCompleteCallbacks) {
+ if (success) {
+ onComplete.onSuccess(raftActor.self());
} else {
- onComplete.onFailure(raftActor.self(), replyTo);
+ onComplete.onFailure(raftActor.self());
}
}
}
@@ -168,13 +177,18 @@ public class RaftActorLeadershipTransferCohort implements Runnable {
onCompleteCallbacks.add(onComplete);
}
+ boolean isTransferring() {
+ return isTransferring;
+ }
+
@VisibleForTesting
void setNewLeaderTimeoutInMillis(long newLeaderTimeoutInMillis) {
this.newLeaderTimeoutInMillis = newLeaderTimeoutInMillis;
}
interface OnComplete {
- void onSuccess(ActorRef raftActorRef, ActorRef replyTo);
- void onFailure(ActorRef raftActorRef, ActorRef replyTo);
+ void onSuccess(ActorRef raftActorRef);
+
+ void onFailure(ActorRef raftActorRef);
}
}