use of org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry in project controller by opendaylight.
the class RaftActorRecoverySupportTest method testOnApplyJournalEntries.
@Test
public void testOnApplyJournalEntries() {
configParams.setJournalRecoveryLogBatchSize(5);
ReplicatedLog replicatedLog = context.getReplicatedLog();
replicatedLog.append(new SimpleReplicatedLogEntry(0, 1, new MockRaftActorContext.MockPayload("0")));
replicatedLog.append(new SimpleReplicatedLogEntry(1, 1, new MockRaftActorContext.MockPayload("1")));
replicatedLog.append(new SimpleReplicatedLogEntry(2, 1, new MockRaftActorContext.MockPayload("2")));
replicatedLog.append(new SimpleReplicatedLogEntry(3, 1, new MockRaftActorContext.MockPayload("3")));
replicatedLog.append(new SimpleReplicatedLogEntry(4, 1, new MockRaftActorContext.MockPayload("4")));
replicatedLog.append(new SimpleReplicatedLogEntry(5, 1, new MockRaftActorContext.MockPayload("5")));
sendMessageToSupport(new ApplyJournalEntries(2));
assertEquals("Last applied", 2, context.getLastApplied());
assertEquals("Commit index", 2, context.getCommitIndex());
sendMessageToSupport(new ApplyJournalEntries(4));
assertEquals("Last applied", 4, context.getLastApplied());
assertEquals("Last applied", 4, context.getLastApplied());
sendMessageToSupport(new ApplyJournalEntries(5));
assertEquals("Last index", 5, context.getReplicatedLog().lastIndex());
assertEquals("Last applied", 5, context.getLastApplied());
assertEquals("Commit index", 5, context.getCommitIndex());
assertEquals("Snapshot term", -1, context.getReplicatedLog().getSnapshotTerm());
assertEquals("Snapshot index", -1, context.getReplicatedLog().getSnapshotIndex());
InOrder inOrder = Mockito.inOrder(mockCohort);
inOrder.verify(mockCohort).startLogRecoveryBatch(5);
for (int i = 0; i < replicatedLog.size() - 1; i++) {
inOrder.verify(mockCohort).appendRecoveredLogEntry(replicatedLog.get(i).getData());
}
inOrder.verify(mockCohort).applyCurrentLogRecoveryBatch();
inOrder.verify(mockCohort).startLogRecoveryBatch(5);
inOrder.verify(mockCohort).appendRecoveredLogEntry(replicatedLog.get(replicatedLog.size() - 1).getData());
inOrder.verifyNoMoreInteractions();
}
use of org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry in project controller by opendaylight.
the class RaftActorRecoverySupportTest method testOnReplicatedLogEntry.
@Test
public void testOnReplicatedLogEntry() {
ReplicatedLogEntry logEntry = new SimpleReplicatedLogEntry(1, 1, new MockRaftActorContext.MockPayload("1", 5));
sendMessageToSupport(logEntry);
assertEquals("Journal log size", 1, context.getReplicatedLog().size());
assertEquals("Journal data size", 5, context.getReplicatedLog().dataSize());
assertEquals("Last index", 1, context.getReplicatedLog().lastIndex());
assertEquals("Last applied", -1, context.getLastApplied());
assertEquals("Commit index", -1, context.getCommitIndex());
assertEquals("Snapshot term", -1, context.getReplicatedLog().getSnapshotTerm());
assertEquals("Snapshot index", -1, context.getReplicatedLog().getSnapshotIndex());
}
use of org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry in project controller by opendaylight.
the class RaftActorServerConfigurationSupportTest method testChangeToVotingWithNoLeaderAndOtherLeaderElected.
@Test
public void testChangeToVotingWithNoLeaderAndOtherLeaderElected() {
LOG.info("testChangeToVotingWithNoLeaderAndOtherLeaderElected starting");
DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
configParams.setHeartBeatInterval(new FiniteDuration(100, TimeUnit.MILLISECONDS));
configParams.setElectionTimeoutFactor(100000);
final String node1ID = "node1";
final String node2ID = "node2";
configParams.setPeerAddressResolver(peerId -> peerId.equals(node1ID) ? actorFactory.createTestActorPath(node1ID) : peerId.equals(node2ID) ? actorFactory.createTestActorPath(node2ID) : null);
ServerConfigurationPayload persistedServerConfig = new ServerConfigurationPayload(Arrays.asList(new ServerInfo(node1ID, false), new ServerInfo(node2ID, true)));
SimpleReplicatedLogEntry persistedServerConfigEntry = new SimpleReplicatedLogEntry(0, 1, persistedServerConfig);
InMemoryJournal.addEntry(node1ID, 1, new UpdateElectionTerm(1, "node1"));
InMemoryJournal.addEntry(node1ID, 2, persistedServerConfigEntry);
InMemoryJournal.addEntry(node2ID, 1, new UpdateElectionTerm(1, "node1"));
InMemoryJournal.addEntry(node2ID, 2, persistedServerConfigEntry);
ActorRef node1Collector = actorFactory.createActor(MessageCollectorActor.props(), actorFactory.generateActorId("collector"));
TestActorRef<CollectingMockRaftActor> node1RaftActorRef = actorFactory.createTestActor(CollectingMockRaftActor.props(node1ID, ImmutableMap.<String, String>of(), configParams, PERSISTENT, node1Collector).withDispatcher(Dispatchers.DefaultDispatcherId()), node1ID);
final CollectingMockRaftActor node1RaftActor = node1RaftActorRef.underlyingActor();
ActorRef node2Collector = actorFactory.createActor(MessageCollectorActor.props(), actorFactory.generateActorId("collector"));
TestActorRef<CollectingMockRaftActor> node2RaftActorRef = actorFactory.createTestActor(CollectingMockRaftActor.props(node2ID, ImmutableMap.<String, String>of(), configParams, PERSISTENT, node2Collector).withDispatcher(Dispatchers.DefaultDispatcherId()), node2ID);
CollectingMockRaftActor node2RaftActor = node2RaftActorRef.underlyingActor();
// Send a ChangeServersVotingStatus message to node1 to change node1 to voting. This should cause
// node1 to try to elect itself as leader in order to apply the new server config. But we'll drop
// RequestVote messages in node2 and make it the leader so node1 should forward the server change
// request to node2 when node2 is elected.
node2RaftActor.setDropMessageOfType(RequestVote.class);
ChangeServersVotingStatus changeServers = new ChangeServersVotingStatus(ImmutableMap.of(node1ID, true, node2ID, true));
node1RaftActorRef.tell(changeServers, testKit.getRef());
MessageCollectorActor.expectFirstMatching(node2Collector, RequestVote.class);
node2RaftActorRef.tell(TimeoutNow.INSTANCE, ActorRef.noSender());
ServerChangeReply reply = testKit.expectMsgClass(testKit.duration("5 seconds"), ServerChangeReply.class);
assertEquals("getStatus", ServerChangeStatus.OK, reply.getStatus());
MessageCollectorActor.expectFirstMatching(node1Collector, ApplyJournalEntries.class);
verifyServerConfigurationPayloadEntry(node1RaftActor.getRaftActorContext().getReplicatedLog(), votingServer(node1ID), votingServer(node2ID));
assertEquals("isVotingMember", true, node1RaftActor.getRaftActorContext().isVotingMember());
assertEquals("getRaftState", RaftState.Follower, node1RaftActor.getRaftState());
MessageCollectorActor.expectFirstMatching(node2Collector, ApplyJournalEntries.class);
verifyServerConfigurationPayloadEntry(node2RaftActor.getRaftActorContext().getReplicatedLog(), votingServer(node1ID), votingServer(node2ID));
assertEquals("getRaftState", RaftState.Leader, node2RaftActor.getRaftState());
LOG.info("testChangeToVotingWithNoLeaderAndOtherLeaderElected ending");
}
use of org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry in project controller by opendaylight.
the class RaftActorServerConfigurationSupportTest method testChangeToVotingWithNoLeaderAndElectionTimeout.
@Test
public void testChangeToVotingWithNoLeaderAndElectionTimeout() {
LOG.info("testChangeToVotingWithNoLeaderAndElectionTimeout starting");
final String node1ID = "node1";
final String node2ID = "node2";
final PeerAddressResolver peerAddressResolver = peerId -> peerId.equals(node1ID) ? actorFactory.createTestActorPath(node1ID) : peerId.equals(node2ID) ? actorFactory.createTestActorPath(node2ID) : null;
ServerConfigurationPayload persistedServerConfig = new ServerConfigurationPayload(Arrays.asList(new ServerInfo(node1ID, false), new ServerInfo(node2ID, true)));
SimpleReplicatedLogEntry persistedServerConfigEntry = new SimpleReplicatedLogEntry(0, 1, persistedServerConfig);
InMemoryJournal.addEntry(node1ID, 1, new UpdateElectionTerm(1, "node1"));
InMemoryJournal.addEntry(node1ID, 2, persistedServerConfigEntry);
InMemoryJournal.addEntry(node2ID, 1, new UpdateElectionTerm(1, "node1"));
InMemoryJournal.addEntry(node2ID, 2, persistedServerConfigEntry);
DefaultConfigParamsImpl configParams1 = new DefaultConfigParamsImpl();
configParams1.setHeartBeatInterval(new FiniteDuration(100, TimeUnit.MILLISECONDS));
configParams1.setElectionTimeoutFactor(1);
configParams1.setPeerAddressResolver(peerAddressResolver);
ActorRef node1Collector = actorFactory.createActor(MessageCollectorActor.props(), actorFactory.generateActorId("collector"));
TestActorRef<CollectingMockRaftActor> node1RaftActorRef = actorFactory.createTestActor(CollectingMockRaftActor.props(node1ID, ImmutableMap.<String, String>of(), configParams1, PERSISTENT, node1Collector).withDispatcher(Dispatchers.DefaultDispatcherId()), node1ID);
final CollectingMockRaftActor node1RaftActor = node1RaftActorRef.underlyingActor();
DefaultConfigParamsImpl configParams2 = new DefaultConfigParamsImpl();
configParams2.setElectionTimeoutFactor(1000000);
configParams2.setPeerAddressResolver(peerAddressResolver);
ActorRef node2Collector = actorFactory.createActor(MessageCollectorActor.props(), actorFactory.generateActorId("collector"));
TestActorRef<CollectingMockRaftActor> node2RaftActorRef = actorFactory.createTestActor(CollectingMockRaftActor.props(node2ID, ImmutableMap.<String, String>of(), configParams2, PERSISTENT, node2Collector).withDispatcher(Dispatchers.DefaultDispatcherId()), node2ID);
CollectingMockRaftActor node2RaftActor = node2RaftActorRef.underlyingActor();
// Send a ChangeServersVotingStatus message to node1 to change mode1 to voting. This should cause
// node1 to try to elect itself as leader in order to apply the new server config. But we'll drop
// RequestVote messages in node2 which should cause node1 to time out and revert back to the previous
// server config and fail with NO_LEADER. Note that node1 shouldn't forward the request to node2 b/c
// node2 was previously voting.
node2RaftActor.setDropMessageOfType(RequestVote.class);
ChangeServersVotingStatus changeServers = new ChangeServersVotingStatus(ImmutableMap.of(node1ID, true));
node1RaftActorRef.tell(changeServers, testKit.getRef());
ServerChangeReply reply = testKit.expectMsgClass(testKit.duration("5 seconds"), ServerChangeReply.class);
assertEquals("getStatus", ServerChangeStatus.NO_LEADER, reply.getStatus());
assertEquals("Server config", Sets.newHashSet(nonVotingServer(node1ID), votingServer(node2ID)), Sets.newHashSet(node1RaftActor.getRaftActorContext().getPeerServerInfo(true).getServerConfig()));
assertEquals("getRaftState", RaftState.Follower, node1RaftActor.getRaftState());
LOG.info("testChangeToVotingWithNoLeaderAndElectionTimeout ending");
}
use of org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry in project controller by opendaylight.
the class AbstractReplicatedLogImplTest method testRemoveFrom.
@Test
public void testRemoveFrom() {
replicatedLogImpl.append(new SimpleReplicatedLogEntry(4, 2, new MockPayload("E", 2)));
replicatedLogImpl.append(new SimpleReplicatedLogEntry(5, 2, new MockPayload("F", 3)));
assertEquals("dataSize", 9, replicatedLogImpl.dataSize());
long adjusted = replicatedLogImpl.removeFrom(4);
assertEquals("removeFrom - adjusted", 4, adjusted);
assertEquals("size", 4, replicatedLogImpl.size());
assertEquals("dataSize", 4, replicatedLogImpl.dataSize());
takeSnapshot(1);
adjusted = replicatedLogImpl.removeFrom(2);
assertEquals("removeFrom - adjusted", 1, adjusted);
assertEquals("size", 1, replicatedLogImpl.size());
assertEquals("dataSize", 1, replicatedLogImpl.dataSize());
assertEquals("removeFrom - adjusted", -1, replicatedLogImpl.removeFrom(0));
assertEquals("removeFrom - adjusted", -1, replicatedLogImpl.removeFrom(100));
}
Aggregations