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());
}
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);
}
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());
}
});
}
}
Aggregations