use of org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply in project controller by opendaylight.
the class ShardSnapshotActorTest method testSerializeSnapshot.
private static void testSerializeSnapshot(final String testName, final ShardDataTreeSnapshot snapshot, final boolean withInstallSnapshot) throws Exception {
new TestKit(getSystem()) {
{
final ActorRef snapshotActor = getSystem().actorOf(ShardSnapshotActor.props(), testName);
watch(snapshotActor);
final NormalizedNode<?, ?> expectedRoot = snapshot.getRootNode().get();
ByteArrayOutputStream installSnapshotStream = withInstallSnapshot ? new ByteArrayOutputStream() : null;
ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot, Optional.ofNullable(installSnapshotStream), getRef());
final CaptureSnapshotReply reply = expectMsgClass(duration("3 seconds"), CaptureSnapshotReply.class);
assertNotNull("getSnapshotState is null", reply.getSnapshotState());
assertEquals("SnapshotState type", ShardSnapshotState.class, reply.getSnapshotState().getClass());
assertEquals("Snapshot", snapshot, ((ShardSnapshotState) reply.getSnapshotState()).getSnapshot());
if (installSnapshotStream != null) {
final ShardDataTreeSnapshot deserialized;
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(installSnapshotStream.toByteArray()))) {
deserialized = ShardDataTreeSnapshot.deserialize(in);
}
assertEquals("Deserialized snapshot type", snapshot.getClass(), deserialized.getClass());
final Optional<NormalizedNode<?, ?>> maybeNode = deserialized.getRootNode();
assertEquals("isPresent", true, maybeNode.isPresent());
assertEquals("Root node", expectedRoot, maybeNode.get());
}
}
};
}
use of org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply in project controller by opendaylight.
the class RaftActorTest method testRaftActorForwardsToRaftActorSnapshotMessageSupport.
@Test
public void testRaftActorForwardsToRaftActorSnapshotMessageSupport() {
String persistenceId = factory.generateActorId("leader-");
DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
RaftActorSnapshotMessageSupport mockSupport = mock(RaftActorSnapshotMessageSupport.class);
TestActorRef<MockRaftActor> mockActorRef = factory.createTestActor(MockRaftActor.builder().id(persistenceId).config(config).snapshotMessageSupport(mockSupport).props());
MockRaftActor mockRaftActor = mockActorRef.underlyingActor();
// Wait for akka's recovery to complete so it doesn't interfere.
mockRaftActor.waitForRecoveryComplete();
ApplySnapshot applySnapshot = new ApplySnapshot(mock(Snapshot.class));
doReturn(true).when(mockSupport).handleSnapshotMessage(same(applySnapshot), any(ActorRef.class));
mockRaftActor.handleCommand(applySnapshot);
CaptureSnapshotReply captureSnapshotReply = new CaptureSnapshotReply(ByteState.empty(), java.util.Optional.empty());
doReturn(true).when(mockSupport).handleSnapshotMessage(same(captureSnapshotReply), any(ActorRef.class));
mockRaftActor.handleCommand(captureSnapshotReply);
SaveSnapshotSuccess saveSnapshotSuccess = new SaveSnapshotSuccess(new SnapshotMetadata("", 0L, 0L));
doReturn(true).when(mockSupport).handleSnapshotMessage(same(saveSnapshotSuccess), any(ActorRef.class));
mockRaftActor.handleCommand(saveSnapshotSuccess);
SaveSnapshotFailure saveSnapshotFailure = new SaveSnapshotFailure(new SnapshotMetadata("", 0L, 0L), new Throwable());
doReturn(true).when(mockSupport).handleSnapshotMessage(same(saveSnapshotFailure), any(ActorRef.class));
mockRaftActor.handleCommand(saveSnapshotFailure);
doReturn(true).when(mockSupport).handleSnapshotMessage(same(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT), any(ActorRef.class));
mockRaftActor.handleCommand(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT);
doReturn(true).when(mockSupport).handleSnapshotMessage(same(GetSnapshot.INSTANCE), any(ActorRef.class));
mockRaftActor.handleCommand(GetSnapshot.INSTANCE);
verify(mockSupport).handleSnapshotMessage(same(applySnapshot), any(ActorRef.class));
verify(mockSupport).handleSnapshotMessage(same(captureSnapshotReply), any(ActorRef.class));
verify(mockSupport).handleSnapshotMessage(same(saveSnapshotSuccess), any(ActorRef.class));
verify(mockSupport).handleSnapshotMessage(same(saveSnapshotFailure), any(ActorRef.class));
verify(mockSupport).handleSnapshotMessage(same(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT), any(ActorRef.class));
verify(mockSupport).handleSnapshotMessage(same(GetSnapshot.INSTANCE), any(ActorRef.class));
}
use of org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply in project controller by opendaylight.
the class RaftActorTest method testRealSnapshotWhenReplicatedToAllIndexMinusOne.
@Test
public void testRealSnapshotWhenReplicatedToAllIndexMinusOne() throws Exception {
String persistenceId = factory.generateActorId("leader-");
DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
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().put("member1", "address").build();
TestActorRef<MockRaftActor> mockActorRef = factory.createTestActor(MockRaftActor.props(persistenceId, peerAddresses, config, dataPersistenceProvider), persistenceId);
MockRaftActor leaderActor = mockActorRef.underlyingActor();
leaderActor.getRaftActorContext().setCommitIndex(3);
leaderActor.getRaftActorContext().setLastApplied(3);
leaderActor.getRaftActorContext().getTermInformation().update(1, persistenceId);
leaderActor.waitForInitializeBehaviorComplete();
for (int i = 0; i < 4; i++) {
leaderActor.getReplicatedLog().append(new SimpleReplicatedLogEntry(i, 1, new MockRaftActorContext.MockPayload("A")));
}
Leader leader = new Leader(leaderActor.getRaftActorContext());
leaderActor.setCurrentBehavior(leader);
assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
// Simulate an install snaphost to a follower.
leaderActor.getRaftActorContext().getSnapshotManager().captureToInstall(leaderActor.getReplicatedLog().last(), -1, "member1");
// Now send a CaptureSnapshotReply
mockActorRef.tell(new CaptureSnapshotReply(ByteState.of(fromObject("foo").toByteArray()), java.util.Optional.empty()), mockActorRef);
// Trimming log in this scenario is a no-op
assertEquals(-1, leaderActor.getReplicatedLog().getSnapshotIndex());
assertTrue(leaderActor.getRaftActorContext().getSnapshotManager().isCapturing());
assertEquals(-1, leader.getReplicatedToAllIndex());
}
use of org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply in project controller by opendaylight.
the class RaftActorTest method testGetSnapshot.
@Test
public void testGetSnapshot() throws Exception {
TEST_LOG.info("testGetSnapshot starting");
final TestKit kit = new TestKit(getSystem());
String persistenceId = factory.generateActorId("test-actor-");
DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
long term = 3;
long seqN = 1;
InMemoryJournal.addEntry(persistenceId, seqN++, new UpdateElectionTerm(term, "member-1"));
InMemoryJournal.addEntry(persistenceId, seqN++, new SimpleReplicatedLogEntry(0, term, new MockRaftActorContext.MockPayload("A")));
InMemoryJournal.addEntry(persistenceId, seqN++, new SimpleReplicatedLogEntry(1, term, new MockRaftActorContext.MockPayload("B")));
InMemoryJournal.addEntry(persistenceId, seqN++, new ApplyJournalEntries(1));
InMemoryJournal.addEntry(persistenceId, seqN++, new SimpleReplicatedLogEntry(2, term, new MockRaftActorContext.MockPayload("C")));
TestActorRef<MockRaftActor> raftActorRef = factory.createTestActor(MockRaftActor.props(persistenceId, ImmutableMap.<String, String>builder().put("member1", "address").build(), config).withDispatcher(Dispatchers.DefaultDispatcherId()), persistenceId);
MockRaftActor mockRaftActor = raftActorRef.underlyingActor();
mockRaftActor.waitForRecoveryComplete();
mockRaftActor.snapshotCohortDelegate = mock(RaftActorSnapshotCohort.class);
raftActorRef.tell(GetSnapshot.INSTANCE, kit.getRef());
ArgumentCaptor<ActorRef> replyActor = ArgumentCaptor.forClass(ActorRef.class);
verify(mockRaftActor.snapshotCohortDelegate, timeout(5000)).createSnapshot(replyActor.capture(), eq(java.util.Optional.empty()));
byte[] stateSnapshot = new byte[] { 1, 2, 3 };
replyActor.getValue().tell(new CaptureSnapshotReply(ByteState.of(stateSnapshot), java.util.Optional.empty()), ActorRef.noSender());
GetSnapshotReply reply = kit.expectMsgClass(GetSnapshotReply.class);
assertEquals("getId", persistenceId, reply.getId());
Snapshot replySnapshot = reply.getSnapshot();
assertEquals("getElectionTerm", term, replySnapshot.getElectionTerm());
assertEquals("getElectionVotedFor", "member-1", replySnapshot.getElectionVotedFor());
assertEquals("getLastAppliedIndex", 1L, replySnapshot.getLastAppliedIndex());
assertEquals("getLastAppliedTerm", term, replySnapshot.getLastAppliedTerm());
assertEquals("getLastIndex", 2L, replySnapshot.getLastIndex());
assertEquals("getLastTerm", term, replySnapshot.getLastTerm());
assertEquals("getState", ByteState.of(stateSnapshot), replySnapshot.getState());
assertEquals("getUnAppliedEntries size", 1, replySnapshot.getUnAppliedEntries().size());
assertEquals("UnApplied entry index ", 2L, replySnapshot.getUnAppliedEntries().get(0).getIndex());
// Test with timeout
mockRaftActor.getSnapshotMessageSupport().setSnapshotReplyActorTimeout(Duration.create(200, TimeUnit.MILLISECONDS));
reset(mockRaftActor.snapshotCohortDelegate);
raftActorRef.tell(GetSnapshot.INSTANCE, kit.getRef());
Failure failure = kit.expectMsgClass(akka.actor.Status.Failure.class);
assertEquals("Failure cause type", TimeoutException.class, failure.cause().getClass());
mockRaftActor.getSnapshotMessageSupport().setSnapshotReplyActorTimeout(Duration.create(30, TimeUnit.SECONDS));
// Test with persistence disabled.
mockRaftActor.setPersistence(false);
reset(mockRaftActor.snapshotCohortDelegate);
raftActorRef.tell(GetSnapshot.INSTANCE, kit.getRef());
reply = kit.expectMsgClass(GetSnapshotReply.class);
verify(mockRaftActor.snapshotCohortDelegate, never()).createSnapshot(anyObject(), anyObject());
assertEquals("getId", persistenceId, reply.getId());
replySnapshot = reply.getSnapshot();
assertEquals("getElectionTerm", term, replySnapshot.getElectionTerm());
assertEquals("getElectionVotedFor", "member-1", replySnapshot.getElectionVotedFor());
assertEquals("getLastAppliedIndex", -1L, replySnapshot.getLastAppliedIndex());
assertEquals("getLastAppliedTerm", -1L, replySnapshot.getLastAppliedTerm());
assertEquals("getLastIndex", -1L, replySnapshot.getLastIndex());
assertEquals("getLastTerm", -1L, replySnapshot.getLastTerm());
assertEquals("getState type", EmptyState.INSTANCE, replySnapshot.getState());
assertEquals("getUnAppliedEntries size", 0, replySnapshot.getUnAppliedEntries().size());
TEST_LOG.info("testGetSnapshot ending");
}
use of org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply in project controller by opendaylight.
the class RecoveryIntegrationTest method testStatePersistedBetweenSnapshotCaptureAndPersist.
@Test
public void testStatePersistedBetweenSnapshotCaptureAndPersist() {
send2InitialPayloads();
// Block these messages initially so we can control the sequence.
leaderActor.underlyingActor().startDropMessages(CaptureSnapshotReply.class);
follower1Actor.underlyingActor().startDropMessages(AppendEntries.class);
final MockPayload payload2 = sendPayloadData(leaderActor, "two");
// This should trigger a snapshot.
final MockPayload payload3 = sendPayloadData(leaderActor, "three");
MessageCollectorActor.expectMatching(follower1CollectorActor, AppendEntries.class, 3);
// Send another payload.
final MockPayload payload4 = sendPayloadData(leaderActor, "four");
// Now deliver the AppendEntries to the follower
follower1Actor.underlyingActor().stopDropMessages(AppendEntries.class);
MessageCollectorActor.expectMatching(leaderCollectorActor, ApplyJournalEntries.class, 1);
// Now deliver the CaptureSnapshotReply to the leader.
CaptureSnapshotReply captureSnapshotReply = MessageCollectorActor.expectFirstMatching(leaderCollectorActor, CaptureSnapshotReply.class);
leaderActor.underlyingActor().stopDropMessages(CaptureSnapshotReply.class);
leaderActor.tell(captureSnapshotReply, leaderActor);
// Wait for snapshot complete.
MessageCollectorActor.expectFirstMatching(leaderCollectorActor, SaveSnapshotSuccess.class);
reinstateLeaderActor();
assertEquals("Leader snapshot term", currentTerm, leaderContext.getReplicatedLog().getSnapshotTerm());
assertEquals("Leader snapshot index", 1, leaderContext.getReplicatedLog().getSnapshotIndex());
assertEquals("Leader journal log size", 3, leaderContext.getReplicatedLog().size());
assertEquals("Leader journal last index", 4, leaderContext.getReplicatedLog().lastIndex());
assertEquals("Leader commit index", 4, leaderContext.getCommitIndex());
assertEquals("Leader last applied", 4, leaderContext.getLastApplied());
assertEquals("Leader state", Arrays.asList(payload0, payload1, payload2, payload3, payload4), leaderActor.underlyingActor().getState());
}
Aggregations