use of org.opendaylight.controller.cluster.raft.RaftState in project controller by opendaylight.
the class AbstractLeaderElectionScenarioTest method verifyBehaviorState.
@SuppressWarnings("checkstyle:IllegalCatch")
void verifyBehaviorState(final String name, final MemberActor actor, final RaftState expState) {
RaftState actualState;
try {
actualState = (RaftState) Await.result(Patterns.ask(actor.self(), GetBehaviorState.INSTANCE, Timeout.apply(5, TimeUnit.SECONDS)), Duration.apply(5, TimeUnit.SECONDS));
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
assertEquals(name + " behavior state", expState, actualState);
}
use of org.opendaylight.controller.cluster.raft.RaftState in project controller by opendaylight.
the class AbstractRaftActorBehaviorTest method testHandleAppendEntriesAddSameEntryToLog.
@Test
public void testHandleAppendEntriesAddSameEntryToLog() {
MockRaftActorContext context = createActorContext();
context.getTermInformation().update(2, "test");
// Prepare the receivers log
MockRaftActorContext.MockPayload payload = new MockRaftActorContext.MockPayload("zero");
setLastLogEntry(context, 2, 0, payload);
List<ReplicatedLogEntry> entries = new ArrayList<>();
entries.add(new SimpleReplicatedLogEntry(0, 2, payload));
final AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 2, -1, (short) 0);
behavior = createBehavior(context);
assertFalse("This test should be overridden when testing Candidate", behavior instanceof Candidate);
RaftState expected = behavior.state();
// Check that the behavior does not handle unknwon message
assertNull(behavior.handleMessage(behaviorActor, "unknown"));
RaftActorBehavior raftBehavior = behavior.handleMessage(behaviorActor, appendEntries);
assertEquals("Raft state", expected, raftBehavior.state());
assertEquals("ReplicatedLog size", 1, context.getReplicatedLog().size());
handleAppendEntriesAddSameEntryToLogReply(behaviorActor);
}
use of org.opendaylight.controller.cluster.raft.RaftState in project controller by opendaylight.
the class AbstractRaftActorBehaviorTest method testHandleAppendEntriesSenderTermLessThanReceiverTerm.
/**
* This test verifies that when an AppendEntries is received with a term that
* is less that the currentTerm of the RaftActor then the RaftActor does not
* change it's state and it responds back with a failure.
*/
@Test
public void testHandleAppendEntriesSenderTermLessThanReceiverTerm() {
MockRaftActorContext context = createActorContext();
short payloadVersion = 5;
context.setPayloadVersion(payloadVersion);
// First set the receivers term to a high number (1000)
context.getTermInformation().update(1000, "test");
AppendEntries appendEntries = new AppendEntries(100, "leader-1", 0, 0, Collections.emptyList(), 101, -1, (short) 4);
behavior = createBehavior(context);
RaftState expected = behavior.state();
RaftActorBehavior raftBehavior = behavior.handleMessage(behaviorActor, appendEntries);
assertEquals("Raft state", expected, raftBehavior.state());
// Also expect an AppendEntriesReply to be sent where success is false
AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(behaviorActor, AppendEntriesReply.class);
assertEquals("isSuccess", false, reply.isSuccess());
assertEquals("getPayloadVersion", payloadVersion, reply.getPayloadVersion());
}
use of org.opendaylight.controller.cluster.raft.RaftState in project controller by opendaylight.
the class ClusterAdminRpcServiceTest method testAddShardReplica.
@Test
public void testAddShardReplica() throws Exception {
String name = "testAddShardReplica";
String moduleShardsConfig = "module-shards-cars-member-1.conf";
MemberNode leaderNode1 = MemberNode.builder(memberNodes).akkaConfig("Member1").testName(name).moduleShardsConfig(moduleShardsConfig).waitForShardLeader("cars").build();
MemberNode newReplicaNode2 = MemberNode.builder(memberNodes).akkaConfig("Member2").testName(name).moduleShardsConfig(moduleShardsConfig).build();
leaderNode1.waitForMembersUp("member-2");
doAddShardReplica(newReplicaNode2, "cars", "member-1");
MemberNode newReplicaNode3 = MemberNode.builder(memberNodes).akkaConfig("Member3").testName(name).moduleShardsConfig(moduleShardsConfig).build();
leaderNode1.waitForMembersUp("member-3");
newReplicaNode2.waitForMembersUp("member-3");
doAddShardReplica(newReplicaNode3, "cars", "member-1", "member-2");
verifyRaftPeersPresent(newReplicaNode2.configDataStore(), "cars", "member-1", "member-3");
verifyRaftPeersPresent(newReplicaNode2.operDataStore(), "cars", "member-1", "member-3");
// Write data to member-2's config datastore and read/verify via member-3
final NormalizedNode<?, ?> configCarsNode = writeCarsNodeAndVerify(newReplicaNode2.configDataStore(), newReplicaNode3.configDataStore());
// Write data to member-3's oper datastore and read/verify via member-2
writeCarsNodeAndVerify(newReplicaNode3.operDataStore(), newReplicaNode2.operDataStore());
// Verify all data has been replicated. We expect 4 log entries and thus last applied index of 3 -
// 2 ServerConfigurationPayload entries, the transaction payload entry plus a purge payload.
RaftStateVerifier verifier = raftState -> {
assertEquals("Commit index", 3, raftState.getCommitIndex());
assertEquals("Last applied index", 3, raftState.getLastApplied());
};
verifyRaftState(leaderNode1.configDataStore(), "cars", verifier);
verifyRaftState(leaderNode1.operDataStore(), "cars", verifier);
verifyRaftState(newReplicaNode2.configDataStore(), "cars", verifier);
verifyRaftState(newReplicaNode2.operDataStore(), "cars", verifier);
verifyRaftState(newReplicaNode3.configDataStore(), "cars", verifier);
verifyRaftState(newReplicaNode3.operDataStore(), "cars", verifier);
// Restart member-3 and verify the cars config shard is re-instated.
Cluster.get(leaderNode1.kit().getSystem()).down(Cluster.get(newReplicaNode3.kit().getSystem()).selfAddress());
newReplicaNode3.cleanup();
newReplicaNode3 = MemberNode.builder(memberNodes).akkaConfig("Member3").testName(name).moduleShardsConfig(moduleShardsConfig).createOperDatastore(false).build();
verifyRaftState(newReplicaNode3.configDataStore(), "cars", verifier);
readCarsNodeAndVerify(newReplicaNode3.configDataStore(), configCarsNode);
}
Aggregations