Search in sources :

Example 6 with RaftActorBehavior

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());
}
Also used : HashMap(java.util.HashMap) RaftActorBehavior(org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior) AbstractRaftActorBehavior(org.opendaylight.controller.cluster.raft.behaviors.AbstractRaftActorBehavior) GetOnDemandRaftState(org.opendaylight.controller.cluster.raft.client.messages.GetOnDemandRaftState) OnDemandRaftState(org.opendaylight.controller.cluster.raft.client.messages.OnDemandRaftState) FollowerInfo(org.opendaylight.controller.cluster.raft.client.messages.FollowerInfo) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) AbstractLeader(org.opendaylight.controller.cluster.raft.behaviors.AbstractLeader)

Example 7 with RaftActorBehavior

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;
}
Also used : RaftActorBehavior(org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior) AbstractRaftActorBehavior(org.opendaylight.controller.cluster.raft.behaviors.AbstractRaftActorBehavior)

Example 8 with RaftActorBehavior

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);
}
Also used : RaftActorBehavior(org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior) AbstractRaftActorBehavior(org.opendaylight.controller.cluster.raft.behaviors.AbstractRaftActorBehavior) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 9 with RaftActorBehavior

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);
    }
}
Also used : Leader(org.opendaylight.controller.cluster.raft.behaviors.Leader) RaftActorBehavior(org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 10 with RaftActorBehavior

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();
        }
    });
}
Also used : ActorSelection(akka.actor.ActorSelection) ActorRef(akka.actor.ActorRef) RaftActorBehavior(org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior) LeaderTransitioning(org.opendaylight.controller.cluster.raft.base.messages.LeaderTransitioning)

Aggregations

RaftActorBehavior (org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior)10 AbstractRaftActorBehavior (org.opendaylight.controller.cluster.raft.behaviors.AbstractRaftActorBehavior)6 ActorRef (akka.actor.ActorRef)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Test (org.junit.Test)2 AbstractLeader (org.opendaylight.controller.cluster.raft.behaviors.AbstractLeader)2 Leader (org.opendaylight.controller.cluster.raft.behaviors.Leader)2 ActorSelection (akka.actor.ActorSelection)1 TestActorRef (akka.testkit.TestActorRef)1 ByteString (com.google.protobuf.ByteString)1 HashMap (java.util.HashMap)1 DataPersistenceProvider (org.opendaylight.controller.cluster.DataPersistenceProvider)1 RoleChanged (org.opendaylight.controller.cluster.notifications.RoleChanged)1 ApplyState (org.opendaylight.controller.cluster.raft.base.messages.ApplyState)1 LeaderTransitioning (org.opendaylight.controller.cluster.raft.base.messages.LeaderTransitioning)1 Follower (org.opendaylight.controller.cluster.raft.behaviors.Follower)1 FollowerInfo (org.opendaylight.controller.cluster.raft.client.messages.FollowerInfo)1 GetOnDemandRaftState (org.opendaylight.controller.cluster.raft.client.messages.GetOnDemandRaftState)1 OnDemandRaftState (org.opendaylight.controller.cluster.raft.client.messages.OnDemandRaftState)1 RemoveServer (org.opendaylight.controller.cluster.raft.messages.RemoveServer)1