use of org.opendaylight.controller.cluster.raft.base.messages.SwitchBehavior in project controller by opendaylight.
the class RaftActorTest method testSwitchBehavior.
@Test
public void testSwitchBehavior() {
String persistenceId = factory.generateActorId("leader-");
DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
config.setIsolatedLeaderCheckInterval(new FiniteDuration(1, TimeUnit.DAYS));
config.setSnapshotBatchCount(5);
DataPersistenceProvider dataPersistenceProvider = createProvider();
Map<String, String> peerAddresses = ImmutableMap.<String, String>builder().build();
TestActorRef<MockRaftActor> mockActorRef = factory.createTestActor(MockRaftActor.props(persistenceId, peerAddresses, config, dataPersistenceProvider), persistenceId);
MockRaftActor leaderActor = mockActorRef.underlyingActor();
leaderActor.waitForRecoveryComplete();
leaderActor.handleCommand(new SwitchBehavior(RaftState.Follower, 100));
assertEquals(100, leaderActor.getRaftActorContext().getTermInformation().getCurrentTerm());
assertEquals(RaftState.Follower, leaderActor.getCurrentBehavior().state());
leaderActor.handleCommand(new SwitchBehavior(RaftState.Leader, 110));
assertEquals(110, leaderActor.getRaftActorContext().getTermInformation().getCurrentTerm());
assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
leaderActor.handleCommand(new SwitchBehavior(RaftState.Candidate, 125));
assertEquals(110, leaderActor.getRaftActorContext().getTermInformation().getCurrentTerm());
assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
leaderActor.handleCommand(new SwitchBehavior(RaftState.IsolatedLeader, 125));
assertEquals(110, leaderActor.getRaftActorContext().getTermInformation().getCurrentTerm());
assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
}
use of org.opendaylight.controller.cluster.raft.base.messages.SwitchBehavior in project controller by opendaylight.
the class ShardManagerTest method testOnReceiveSwitchShardBehavior.
@Test
public void testOnReceiveSwitchShardBehavior() throws Exception {
new TestKit(getSystem()) {
{
final ActorRef shardManager = actorFactory.createActor(newPropsShardMgrWithMockShardActor());
shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
shardManager.tell(new ActorInitialized(), mockShardActor);
shardManager.tell(new SwitchShardBehavior(mockShardName, RaftState.Leader, 1000), getRef());
SwitchBehavior switchBehavior = MessageCollectorActor.expectFirstMatching(mockShardActor, SwitchBehavior.class);
assertEquals(RaftState.Leader, switchBehavior.getNewState());
assertEquals(1000, switchBehavior.getNewTerm());
}
};
}
use of org.opendaylight.controller.cluster.raft.base.messages.SwitchBehavior in project controller by opendaylight.
the class ShardManager method onSwitchShardBehavior.
private void onSwitchShardBehavior(final SwitchShardBehavior message) {
final ShardIdentifier identifier = message.getShardId();
if (identifier != null) {
final ShardInformation info = localShards.get(identifier.getShardName());
if (info == null) {
getSender().tell(new Status.Failure(new IllegalArgumentException("Shard " + identifier + " is not local")), getSelf());
return;
}
switchShardBehavior(info, new SwitchBehavior(message.getNewState(), message.getTerm()));
} else {
for (ShardInformation info : localShards.values()) {
switchShardBehavior(info, new SwitchBehavior(message.getNewState(), message.getTerm()));
}
}
getSender().tell(new Status.Success(null), getSelf());
}
use of org.opendaylight.controller.cluster.raft.base.messages.SwitchBehavior in project controller by opendaylight.
the class RaftActor method handleCommand.
/**
* Handles a message.
*
* @deprecated This method is not final for testing purposes. DO NOT OVERRIDE IT, override
* {@link #handleNonRaftCommand(Object)} instead.
*/
@Deprecated
@Override
protected // FIXME: make this method final once our unit tests do not need to override it
void handleCommand(final Object message) {
if (serverConfigurationSupport.handleMessage(message, getSender())) {
return;
}
if (snapshotSupport.handleSnapshotMessage(message, getSender())) {
return;
}
if (message instanceof ApplyState) {
ApplyState applyState = (ApplyState) message;
if (!hasFollowers()) {
// for single node, the capture should happen after the apply state
// as we delete messages from the persistent journal which have made it to the snapshot
// capturing the snapshot before applying makes the persistent journal and snapshot out of sync
// and recovery shows data missing
context.getReplicatedLog().captureSnapshotIfReady(applyState.getReplicatedLogEntry());
context.getSnapshotManager().trimLog(context.getLastApplied());
}
possiblyHandleBehaviorMessage(message);
} else if (message instanceof ApplyJournalEntries) {
ApplyJournalEntries applyEntries = (ApplyJournalEntries) message;
LOG.debug("{}: Persisting ApplyJournalEntries with index={}", persistenceId(), applyEntries.getToIndex());
persistence().persistAsync(applyEntries, NoopProcedure.instance());
} else if (message instanceof FindLeader) {
getSender().tell(new FindLeaderReply(getLeaderAddress()), getSelf());
} else if (message instanceof GetOnDemandRaftState) {
onGetOnDemandRaftStats();
} else if (message instanceof InitiateCaptureSnapshot) {
captureSnapshot();
} else if (message instanceof SwitchBehavior) {
switchBehavior((SwitchBehavior) message);
} else if (message instanceof LeaderTransitioning) {
onLeaderTransitioning((LeaderTransitioning) message);
} else if (message instanceof Shutdown) {
onShutDown();
} else if (message instanceof Runnable) {
((Runnable) message).run();
} else if (message instanceof NoopPayload) {
persistData(null, null, (NoopPayload) message, false);
} else if (message instanceof RequestLeadership) {
onRequestLeadership((RequestLeadership) message);
} else if (!possiblyHandleBehaviorMessage(message)) {
handleNonRaftCommand(message);
}
}
Aggregations