use of org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry in project controller by opendaylight.
the class LeaderTest method testReplicationWithPayloadSizeThatExceedsThreshold.
@Test
public void testReplicationWithPayloadSizeThatExceedsThreshold() {
logStart("testReplicationWithPayloadSizeThatExceedsThreshold");
final int serializedSize = SerializationUtils.serialize(new AppendEntries(1, LEADER_ID, -1, -1, Arrays.asList(new SimpleReplicatedLogEntry(0, 1, new MockRaftActorContext.MockPayload("large"))), 0, -1, (short) 0)).length;
final MockRaftActorContext.MockPayload largePayload = new MockRaftActorContext.MockPayload("large", serializedSize);
MockRaftActorContext leaderActorContext = createActorContextWithFollower();
((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setHeartBeatInterval(new FiniteDuration(300, TimeUnit.MILLISECONDS));
((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setSnapshotChunkSize(serializedSize - 50);
leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().build());
leaderActorContext.setCommitIndex(-1);
leaderActorContext.setLastApplied(-1);
leader = new Leader(leaderActorContext);
leaderActorContext.setCurrentBehavior(leader);
// Send initial heartbeat reply so follower is marked active
MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
leader.handleMessage(followerActor, new AppendEntriesReply(FOLLOWER_ID, -1, true, -1, -1, (short) 0));
MessageCollectorActor.clearMessages(followerActor);
// Send normal payload first to prime commit index.
final long term = leaderActorContext.getTermInformation().getCurrentTerm();
sendReplicate(leaderActorContext, term, 0);
AppendEntries appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
assertEquals("Entries size", 1, appendEntries.getEntries().size());
assertEquals("Entry getIndex", 0, appendEntries.getEntries().get(0).getIndex());
leader.handleMessage(followerActor, new AppendEntriesReply(FOLLOWER_ID, term, true, 0, term, (short) 0));
assertEquals("getCommitIndex", 0, leaderActorContext.getCommitIndex());
MessageCollectorActor.clearMessages(followerActor);
// Now send a large payload that exceeds the maximum size for a single AppendEntries - it should be sliced.
sendReplicate(leaderActorContext, term, 1, largePayload);
MessageSlice messageSlice = MessageCollectorActor.expectFirstMatching(followerActor, MessageSlice.class);
assertEquals("getSliceIndex", 1, messageSlice.getSliceIndex());
assertEquals("getTotalSlices", 2, messageSlice.getTotalSlices());
final Identifier slicingId = messageSlice.getIdentifier();
appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
assertEquals("getPrevLogIndex", 0, appendEntries.getPrevLogIndex());
assertEquals("getPrevLogTerm", term, appendEntries.getPrevLogTerm());
assertEquals("getLeaderCommit", -1, appendEntries.getLeaderCommit());
assertEquals("Entries size", 0, appendEntries.getEntries().size());
MessageCollectorActor.clearMessages(followerActor);
// Initiate a heartbeat - it should send an empty AppendEntries since slicing is in progress.
// Sleep for the heartbeat interval so AppendEntries is sent.
Uninterruptibles.sleepUninterruptibly(leaderActorContext.getConfigParams().getHeartBeatInterval().toMillis(), TimeUnit.MILLISECONDS);
leader.handleMessage(leaderActor, SendHeartBeat.INSTANCE);
appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
assertEquals("getLeaderCommit", -1, appendEntries.getLeaderCommit());
assertEquals("Entries size", 0, appendEntries.getEntries().size());
MessageCollectorActor.clearMessages(followerActor);
// Simulate the MessageSliceReply's and AppendEntriesReply from the follower.
leader.handleMessage(followerActor, MessageSliceReply.success(slicingId, 1, followerActor));
messageSlice = MessageCollectorActor.expectFirstMatching(followerActor, MessageSlice.class);
assertEquals("getSliceIndex", 2, messageSlice.getSliceIndex());
leader.handleMessage(followerActor, MessageSliceReply.success(slicingId, 2, followerActor));
leader.handleMessage(followerActor, new AppendEntriesReply(FOLLOWER_ID, term, true, 1, term, (short) 0));
MessageCollectorActor.clearMessages(followerActor);
// Send another normal payload.
sendReplicate(leaderActorContext, term, 2);
appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
assertEquals("Entries size", 1, appendEntries.getEntries().size());
assertEquals("Entry getIndex", 2, appendEntries.getEntries().get(0).getIndex());
assertEquals("getLeaderCommit", 1, appendEntries.getLeaderCommit());
}
use of org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry in project controller by opendaylight.
the class PartitionedCandidateOnStartupElectionScenarioTest method setupInitialMember1AndMember2Behaviors.
private void setupInitialMember1AndMember2Behaviors() {
testLog.info("setupInitialMember1AndMember2Behaviors starting");
// Initialize the ReplicatedLog and election term info for member 1 and 2. The current term
// will be 3 and the last term will be 2.
SimpleReplicatedLog replicatedLog = new SimpleReplicatedLog();
replicatedLog.append(new SimpleReplicatedLogEntry(0, 2, new MockPayload("")));
replicatedLog.append(new SimpleReplicatedLogEntry(1, 3, new MockPayload("")));
// Create member 2's behavior as Follower.
member2Context = newRaftActorContext("member2", member2ActorRef, ImmutableMap.<String, String>builder().put("member1", member1ActorRef.path().toString()).put("member3", member3ActorRef.path().toString()).build());
DefaultConfigParamsImpl member2ConfigParams = newConfigParams();
member2Context.setConfigParams(member2ConfigParams);
member2Context.setReplicatedLog(replicatedLog);
member2Context.setCommitIndex(replicatedLog.lastIndex());
member2Context.setLastApplied(replicatedLog.lastIndex());
member2Context.getTermInformation().update(3, "member1");
member2Actor.self().tell(new SetBehavior(new Follower(member2Context), member2Context), ActorRef.noSender());
// Create member 1's behavior as Leader.
member1Context = newRaftActorContext("member1", member1ActorRef, ImmutableMap.<String, String>builder().put("member2", member2ActorRef.path().toString()).put("member3", member3ActorRef.path().toString()).build());
DefaultConfigParamsImpl member1ConfigParams = newConfigParams();
member1Context.setConfigParams(member1ConfigParams);
member1Context.setReplicatedLog(replicatedLog);
member1Context.setCommitIndex(replicatedLog.lastIndex());
member1Context.setLastApplied(replicatedLog.lastIndex());
member1Context.getTermInformation().update(3, "member1");
initializeLeaderBehavior(member1Actor, member1Context, 1);
member2Actor.clear();
member3Actor.clear();
testLog.info("setupInitialMember1AndMember2Behaviors ending");
}
use of org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry in project controller by opendaylight.
the class RaftActorTest method testFakeSnapshotsForFollowerWithInRealSnapshots.
@Test
public void testFakeSnapshotsForFollowerWithInRealSnapshots() throws Exception {
final String persistenceId = factory.generateActorId("follower-");
final String leaderId = factory.generateActorId("leader-");
ActorRef leaderActor1 = factory.createActor(MessageCollectorActor.props());
DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
config.setIsolatedLeaderCheckInterval(new FiniteDuration(1, TimeUnit.DAYS));
DataPersistenceProvider dataPersistenceProvider = mock(DataPersistenceProvider.class);
Map<String, String> peerAddresses = new HashMap<>();
peerAddresses.put(leaderId, leaderActor1.path().toString());
TestActorRef<MockRaftActor> mockActorRef = factory.createTestActor(MockRaftActor.props(persistenceId, peerAddresses, config, dataPersistenceProvider), persistenceId);
MockRaftActor followerActor = mockActorRef.underlyingActor();
followerActor.getRaftActorContext().setCommitIndex(4);
followerActor.getRaftActorContext().setLastApplied(4);
followerActor.getRaftActorContext().getTermInformation().update(1, persistenceId);
followerActor.waitForInitializeBehaviorComplete();
Follower follower = new Follower(followerActor.getRaftActorContext());
followerActor.setCurrentBehavior(follower);
assertEquals(RaftState.Follower, followerActor.getCurrentBehavior().state());
// create 6 entries in the log - 0 to 4 are applied and will get picked up as part of the capture snapshot
MockRaftActorContext.MockReplicatedLogBuilder logBuilder = new MockRaftActorContext.MockReplicatedLogBuilder();
followerActor.getRaftActorContext().setReplicatedLog(logBuilder.createEntries(0, 6, 1).build());
// log has indices 0-5
assertEquals(6, followerActor.getReplicatedLog().size());
// snapshot on 4
followerActor.getRaftActorContext().getSnapshotManager().capture(new SimpleReplicatedLogEntry(5, 1, new MockRaftActorContext.MockPayload("D")), 4);
verify(followerActor.snapshotCohortDelegate).createSnapshot(anyObject(), anyObject());
assertEquals(6, followerActor.getReplicatedLog().size());
// fake snapshot on index 6
List<ReplicatedLogEntry> entries = Arrays.asList((ReplicatedLogEntry) new SimpleReplicatedLogEntry(6, 1, new MockRaftActorContext.MockPayload("foo-6")));
followerActor.onReceiveCommand(new AppendEntries(1, leaderId, 5, 1, entries, 5, 5, (short) 0));
assertEquals(7, followerActor.getReplicatedLog().size());
// fake snapshot on index 7
assertEquals(RaftState.Follower, followerActor.getCurrentBehavior().state());
entries = Arrays.asList((ReplicatedLogEntry) new SimpleReplicatedLogEntry(7, 1, new MockRaftActorContext.MockPayload("foo-7")));
followerActor.onReceiveCommand(new AppendEntries(1, leaderId, 6, 1, entries, 6, 6, (short) 0));
assertEquals(8, followerActor.getReplicatedLog().size());
assertEquals(RaftState.Follower, followerActor.getCurrentBehavior().state());
ByteString snapshotBytes = fromObject(Arrays.asList(new MockRaftActorContext.MockPayload("foo-0"), new MockRaftActorContext.MockPayload("foo-1"), new MockRaftActorContext.MockPayload("foo-2"), new MockRaftActorContext.MockPayload("foo-3"), new MockRaftActorContext.MockPayload("foo-4")));
followerActor.onReceiveCommand(new CaptureSnapshotReply(ByteState.of(snapshotBytes.toByteArray()), java.util.Optional.empty()));
assertTrue(followerActor.getRaftActorContext().getSnapshotManager().isCapturing());
// The commit is needed to complete the snapshot creation process
followerActor.getRaftActorContext().getSnapshotManager().commit(-1, -1);
// capture snapshot reply should remove the snapshotted entries only till replicatedToAllIndex
// indexes 5,6,7 left in the log
assertEquals(3, followerActor.getReplicatedLog().size());
assertEquals(7, followerActor.getReplicatedLog().lastIndex());
entries = Arrays.asList((ReplicatedLogEntry) new SimpleReplicatedLogEntry(8, 1, new MockRaftActorContext.MockPayload("foo-7")));
// send an additional entry 8 with leaderCommit = 7
followerActor.onReceiveCommand(new AppendEntries(1, leaderId, 7, 1, entries, 7, 7, (short) 0));
// 7 and 8, as lastapplied is 7
assertEquals(2, followerActor.getReplicatedLog().size());
}
use of org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry in project controller by opendaylight.
the class RaftActorTest method testRaftActorRecoveryWithPersistenceEnabled.
@Test
public void testRaftActorRecoveryWithPersistenceEnabled() throws Exception {
TEST_LOG.info("testRaftActorRecoveryWithPersistenceEnabled starting");
TestKit kit = new TestKit(getSystem());
String persistenceId = factory.generateActorId("follower-");
DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
// Set the heartbeat interval high to essentially disable election otherwise the test
// may fail if the actor is switched to Leader and the commitIndex is set to the last
// log entry.
config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
ImmutableMap<String, String> peerAddresses = ImmutableMap.<String, String>builder().put("member1", "address").build();
ActorRef followerActor = factory.createActor(MockRaftActor.props(persistenceId, peerAddresses, config), persistenceId);
kit.watch(followerActor);
List<ReplicatedLogEntry> snapshotUnappliedEntries = new ArrayList<>();
ReplicatedLogEntry entry1 = new SimpleReplicatedLogEntry(4, 1, new MockRaftActorContext.MockPayload("E"));
snapshotUnappliedEntries.add(entry1);
int lastAppliedDuringSnapshotCapture = 3;
int lastIndexDuringSnapshotCapture = 4;
// 4 messages as part of snapshot, which are applied to state
MockSnapshotState snapshotState = new MockSnapshotState(Arrays.asList(new MockRaftActorContext.MockPayload("A"), new MockRaftActorContext.MockPayload("B"), new MockRaftActorContext.MockPayload("C"), new MockRaftActorContext.MockPayload("D")));
Snapshot snapshot = Snapshot.create(snapshotState, snapshotUnappliedEntries, lastIndexDuringSnapshotCapture, 1, lastAppliedDuringSnapshotCapture, 1, -1, null, null);
InMemorySnapshotStore.addSnapshot(persistenceId, snapshot);
// add more entries after snapshot is taken
List<ReplicatedLogEntry> entries = new ArrayList<>();
ReplicatedLogEntry entry2 = new SimpleReplicatedLogEntry(5, 1, new MockRaftActorContext.MockPayload("F", 2));
ReplicatedLogEntry entry3 = new SimpleReplicatedLogEntry(6, 1, new MockRaftActorContext.MockPayload("G", 3));
ReplicatedLogEntry entry4 = new SimpleReplicatedLogEntry(7, 1, new MockRaftActorContext.MockPayload("H", 4));
entries.add(entry2);
entries.add(entry3);
entries.add(entry4);
final int lastAppliedToState = 5;
final int lastIndex = 7;
InMemoryJournal.addEntry(persistenceId, 5, entry2);
// 2 entries are applied to state besides the 4 entries in snapshot
InMemoryJournal.addEntry(persistenceId, 6, new ApplyJournalEntries(lastAppliedToState));
InMemoryJournal.addEntry(persistenceId, 7, entry3);
InMemoryJournal.addEntry(persistenceId, 8, entry4);
// kill the actor
followerActor.tell(PoisonPill.getInstance(), null);
kit.expectMsgClass(kit.duration("5 seconds"), Terminated.class);
kit.unwatch(followerActor);
// reinstate the actor
TestActorRef<MockRaftActor> ref = factory.createTestActor(MockRaftActor.props(persistenceId, peerAddresses, config));
MockRaftActor mockRaftActor = ref.underlyingActor();
mockRaftActor.waitForRecoveryComplete();
RaftActorContext context = mockRaftActor.getRaftActorContext();
assertEquals("Journal log size", snapshotUnappliedEntries.size() + entries.size(), context.getReplicatedLog().size());
assertEquals("Journal data size", 10, context.getReplicatedLog().dataSize());
assertEquals("Last index", lastIndex, context.getReplicatedLog().lastIndex());
assertEquals("Last applied", lastAppliedToState, context.getLastApplied());
assertEquals("Commit index", lastAppliedToState, context.getCommitIndex());
assertEquals("Recovered state size", 6, mockRaftActor.getState().size());
mockRaftActor.waitForInitializeBehaviorComplete();
assertEquals("getRaftState", RaftState.Follower, mockRaftActor.getRaftState());
TEST_LOG.info("testRaftActorRecoveryWithPersistenceEnabled ending");
}
use of org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry in project controller by opendaylight.
the class RaftActorTest method testRestoreFromSnapshotWithRecoveredData.
@Test
public void testRestoreFromSnapshotWithRecoveredData() throws Exception {
TEST_LOG.info("testRestoreFromSnapshotWithRecoveredData starting");
String persistenceId = factory.generateActorId("test-actor-");
DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
List<MockPayload> state = Arrays.asList(new MockRaftActorContext.MockPayload("A"));
Snapshot snapshot = Snapshot.create(ByteState.of(fromObject(state).toByteArray()), Arrays.<ReplicatedLogEntry>asList(), 5, 2, 5, 2, 2, "member-1", null);
InMemoryJournal.addEntry(persistenceId, 1, new SimpleReplicatedLogEntry(0, 1, new MockRaftActorContext.MockPayload("B")));
TestActorRef<MockRaftActor> raftActorRef = factory.createTestActor(MockRaftActor.builder().id(persistenceId).config(config).restoreFromSnapshot(snapshot).props().withDispatcher(Dispatchers.DefaultDispatcherId()), persistenceId);
MockRaftActor mockRaftActor = raftActorRef.underlyingActor();
mockRaftActor.waitForRecoveryComplete();
Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
verify(mockRaftActor.snapshotCohortDelegate, never()).applySnapshot(any(Snapshot.State.class));
RaftActorContext context = mockRaftActor.getRaftActorContext();
assertEquals("Journal log size", 1, context.getReplicatedLog().size());
assertEquals("Last index", 0, context.getReplicatedLog().lastIndex());
assertEquals("Last applied", -1, context.getLastApplied());
assertEquals("Commit index", -1, context.getCommitIndex());
assertEquals("Current term", 0, context.getTermInformation().getCurrentTerm());
assertEquals("Voted for", null, context.getTermInformation().getVotedFor());
TEST_LOG.info("testRestoreFromSnapshotWithRecoveredData ending");
}
Aggregations