use of org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior in project controller by opendaylight.
the class RaftActor method onGetOnDemandRaftStats.
private void onGetOnDemandRaftStats() {
// Debugging message to retrieve raft stats.
Map<String, String> peerAddresses = new HashMap<>();
Map<String, Boolean> peerVotingStates = new HashMap<>();
for (PeerInfo info : context.getPeers()) {
peerVotingStates.put(info.getId(), info.isVoting());
peerAddresses.put(info.getId(), info.getAddress() != null ? info.getAddress() : "");
}
final RaftActorBehavior currentBehavior = context.getCurrentBehavior();
OnDemandRaftState.AbstractBuilder<?, ?> builder = newOnDemandRaftStateBuilder().commitIndex(context.getCommitIndex()).currentTerm(context.getTermInformation().getCurrentTerm()).inMemoryJournalDataSize(replicatedLog().dataSize()).inMemoryJournalLogSize(replicatedLog().size()).isSnapshotCaptureInitiated(context.getSnapshotManager().isCapturing()).lastApplied(context.getLastApplied()).lastIndex(replicatedLog().lastIndex()).lastTerm(replicatedLog().lastTerm()).leader(getLeaderId()).raftState(currentBehavior.state().toString()).replicatedToAllIndex(currentBehavior.getReplicatedToAllIndex()).snapshotIndex(replicatedLog().getSnapshotIndex()).snapshotTerm(replicatedLog().getSnapshotTerm()).votedFor(context.getTermInformation().getVotedFor()).isVoting(context.isVotingMember()).peerAddresses(peerAddresses).peerVotingStates(peerVotingStates).customRaftPolicyClassName(context.getConfigParams().getCustomRaftPolicyImplementationClass());
ReplicatedLogEntry lastLogEntry = replicatedLog().last();
if (lastLogEntry != null) {
builder.lastLogIndex(lastLogEntry.getIndex());
builder.lastLogTerm(lastLogEntry.getTerm());
}
if (getCurrentBehavior() instanceof AbstractLeader) {
AbstractLeader leader = (AbstractLeader) getCurrentBehavior();
Collection<String> followerIds = leader.getFollowerIds();
List<FollowerInfo> followerInfoList = Lists.newArrayListWithCapacity(followerIds.size());
for (String id : followerIds) {
final FollowerLogInformation info = leader.getFollower(id);
followerInfoList.add(new FollowerInfo(id, info.getNextIndex(), info.getMatchIndex(), info.isFollowerActive(), DurationFormatUtils.formatDurationHMS(TimeUnit.NANOSECONDS.toMillis(info.nanosSinceLastActivity())), context.getPeerInfo(info.getId()).isVoting()));
}
builder.followerInfoList(followerInfoList);
}
sender().tell(builder.build(), self());
}
use of org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior in project controller by opendaylight.
the class RaftActor method possiblyHandleBehaviorMessage.
private boolean possiblyHandleBehaviorMessage(final Object message) {
final RaftActorBehavior currentBehavior = getCurrentBehavior();
final BehaviorState state = behaviorStateTracker.capture(currentBehavior);
// A behavior indicates that it processed the change by returning a reference to the next behavior
// to be used. A null return indicates it has not processed the message and we should be passing it to
// the subclass for handling.
final RaftActorBehavior nextBehavior = currentBehavior.handleMessage(getSender(), message);
if (nextBehavior != null) {
switchBehavior(state, nextBehavior);
return true;
}
return false;
}
use of org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior in project controller by opendaylight.
the class RaftActor method changeCurrentBehavior.
@VisibleForTesting
@SuppressWarnings("checkstyle:IllegalCatch")
protected void changeCurrentBehavior(final RaftActorBehavior newBehavior) {
final RaftActorBehavior currentBehavior = getCurrentBehavior();
if (currentBehavior != null) {
try {
currentBehavior.close();
} catch (Exception e) {
LOG.warn("{}: Error closing behavior {}", persistence(), currentBehavior, e);
}
}
final BehaviorState state = behaviorStateTracker.capture(currentBehavior);
setCurrentBehavior(newBehavior);
handleBehaviorChange(state, newBehavior);
}
use of org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior in project controller by opendaylight.
the class RaftActorLeadershipTransferCohort method doTransfer.
/**
* This method is invoked to perform the leadership transfer.
*/
@VisibleForTesting
void doTransfer() {
RaftActorBehavior behavior = raftActor.getCurrentBehavior();
// Sanity check...
if (behavior instanceof Leader) {
isTransferring = true;
((Leader) behavior).transferLeadership(this);
} else {
LOG.debug("{}: No longer the leader - skipping transfer", raftActor.persistenceId());
finish(true);
}
}
use of org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior in project controller by opendaylight.
the class RaftActorLeadershipTransferCohort method init.
void init() {
RaftActorContext context = raftActor.getRaftActorContext();
RaftActorBehavior currentBehavior = raftActor.getCurrentBehavior();
transferTimer.start();
Optional<ActorRef> roleChangeNotifier = raftActor.getRoleChangeNotifier();
if (roleChangeNotifier.isPresent()) {
roleChangeNotifier.get().tell(raftActor.newLeaderStateChanged(context.getId(), null, currentBehavior.getLeaderPayloadVersion()), raftActor.self());
}
for (String peerId : context.getPeerIds()) {
ActorSelection followerActor = context.getPeerActorSelection(peerId);
if (followerActor != null) {
followerActor.tell(new LeaderTransitioning(context.getId()), context.getActor());
}
}
raftActor.pauseLeader(new TimedRunnable(context.getConfigParams().getElectionTimeOutInterval(), raftActor) {
@Override
protected void doRun() {
LOG.debug("{}: pauseLeader successfully completed - doing transfer", raftActor.persistenceId());
doTransfer();
}
@Override
protected void doCancel() {
LOG.debug("{}: pauseLeader timed out - continuing with transfer", raftActor.persistenceId());
doTransfer();
}
});
}
Aggregations