Search in sources :

Example 1 with NonPersistentDataProvider

use of org.opendaylight.controller.cluster.NonPersistentDataProvider in project controller by opendaylight.

the class CandidateTest method testBecomeLeaderOnReceivingMajorityVotesWithNonVotingPeers.

@Test
public void testBecomeLeaderOnReceivingMajorityVotesWithNonVotingPeers() {
    ElectionTerm mockElectionTerm = Mockito.mock(ElectionTerm.class);
    Mockito.doReturn(1L).when(mockElectionTerm).getCurrentTerm();
    RaftActorContext raftActorContext = new RaftActorContextImpl(candidateActor, candidateActor.actorContext(), "candidate", mockElectionTerm, -1, -1, setupPeers(4), new DefaultConfigParamsImpl(), new NonPersistentDataProvider(Runnable::run), applyState -> {
    }, LOG);
    raftActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().build());
    raftActorContext.getPeerInfo("peer1").setVotingState(VotingState.NON_VOTING);
    raftActorContext.getPeerInfo("peer4").setVotingState(VotingState.NON_VOTING);
    candidate = new Candidate(raftActorContext);
    MessageCollectorActor.expectFirstMatching(peerActors[1], RequestVote.class);
    MessageCollectorActor.expectFirstMatching(peerActors[2], RequestVote.class);
    MessageCollectorActor.assertNoneMatching(peerActors[0], RequestVote.class, 300);
    MessageCollectorActor.assertNoneMatching(peerActors[3], RequestVote.class, 100);
    candidate = candidate.handleMessage(peerActors[1], new RequestVoteReply(1, false));
    assertEquals("Behavior", RaftState.Candidate, candidate.state());
    candidate = candidate.handleMessage(peerActors[2], new RequestVoteReply(1, true));
    assertEquals("Behavior", RaftState.Leader, candidate.state());
}
Also used : ElectionTerm(org.opendaylight.controller.cluster.raft.ElectionTerm) RaftActorContextImpl(org.opendaylight.controller.cluster.raft.RaftActorContextImpl) NonPersistentDataProvider(org.opendaylight.controller.cluster.NonPersistentDataProvider) RaftActorContext(org.opendaylight.controller.cluster.raft.RaftActorContext) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) DefaultConfigParamsImpl(org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl) RequestVoteReply(org.opendaylight.controller.cluster.raft.messages.RequestVoteReply) Test(org.junit.Test)

Example 2 with NonPersistentDataProvider

use of org.opendaylight.controller.cluster.NonPersistentDataProvider in project controller by opendaylight.

the class RaftActorServerConfigurationSupportTest method newFollowerContext.

private static RaftActorContextImpl newFollowerContext(String id, TestActorRef<? extends UntypedActor> actor) {
    DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
    configParams.setHeartBeatInterval(new FiniteDuration(100, TimeUnit.MILLISECONDS));
    configParams.setElectionTimeoutFactor(100000);
    NonPersistentDataProvider noPersistence = new NonPersistentDataProvider(Runnable::run);
    ElectionTermImpl termInfo = new ElectionTermImpl(noPersistence, id, LOG);
    termInfo.update(1, LEADER_ID);
    return new RaftActorContextImpl(actor, actor.underlyingActor().getContext(), id, termInfo, -1, -1, ImmutableMap.of(LEADER_ID, ""), configParams, noPersistence, applyState -> actor.tell(applyState, actor), LOG);
}
Also used : NonPersistentDataProvider(org.opendaylight.controller.cluster.NonPersistentDataProvider) FiniteDuration(scala.concurrent.duration.FiniteDuration)

Example 3 with NonPersistentDataProvider

use of org.opendaylight.controller.cluster.NonPersistentDataProvider in project controller by opendaylight.

the class RaftActor method setPersistence.

protected void setPersistence(final boolean persistent) {
    DataPersistenceProvider currentPersistence = persistence();
    if (persistent && (currentPersistence == null || !currentPersistence.isRecoveryApplicable())) {
        setPersistence(new PersistentDataProvider(this));
        if (getCurrentBehavior() != null) {
            LOG.info("{}: Persistence has been enabled - capturing snapshot", persistenceId());
            captureSnapshot();
        }
    } else if (!persistent && (currentPersistence == null || currentPersistence.isRecoveryApplicable())) {
        setPersistence(new NonPersistentDataProvider(this) {

            /*
                 * The way snapshotting works is,
                 * <ol>
                 * <li> RaftActor calls createSnapshot on the Shard
                 * <li> Shard sends a CaptureSnapshotReply and RaftActor then calls saveSnapshot
                 * <li> When saveSnapshot is invoked on the akka-persistence API it uses the SnapshotStore to save
                 * the snapshot. The SnapshotStore sends SaveSnapshotSuccess or SaveSnapshotFailure. When the
                 * RaftActor gets SaveSnapshot success it commits the snapshot to the in-memory journal. This
                 * commitSnapshot is mimicking what is done in SaveSnapshotSuccess.
                 * </ol>
                 */
            @Override
            public void saveSnapshot(final Object object) {
                // Make saving Snapshot successful
                // Committing the snapshot here would end up calling commit in the creating state which would
                // be a state violation. That's why now we send a message to commit the snapshot.
                self().tell(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT, self());
            }
        });
    }
}
Also used : PersistentDataProvider(org.opendaylight.controller.cluster.PersistentDataProvider) DelegatingPersistentDataProvider(org.opendaylight.controller.cluster.DelegatingPersistentDataProvider) NonPersistentDataProvider(org.opendaylight.controller.cluster.NonPersistentDataProvider) DataPersistenceProvider(org.opendaylight.controller.cluster.DataPersistenceProvider) NonPersistentDataProvider(org.opendaylight.controller.cluster.NonPersistentDataProvider)

Aggregations

NonPersistentDataProvider (org.opendaylight.controller.cluster.NonPersistentDataProvider)3 Test (org.junit.Test)1 DataPersistenceProvider (org.opendaylight.controller.cluster.DataPersistenceProvider)1 DelegatingPersistentDataProvider (org.opendaylight.controller.cluster.DelegatingPersistentDataProvider)1 PersistentDataProvider (org.opendaylight.controller.cluster.PersistentDataProvider)1 DefaultConfigParamsImpl (org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)1 ElectionTerm (org.opendaylight.controller.cluster.raft.ElectionTerm)1 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)1 RaftActorContext (org.opendaylight.controller.cluster.raft.RaftActorContext)1 RaftActorContextImpl (org.opendaylight.controller.cluster.raft.RaftActorContextImpl)1 RequestVoteReply (org.opendaylight.controller.cluster.raft.messages.RequestVoteReply)1 FiniteDuration (scala.concurrent.duration.FiniteDuration)1