private void onSaveSnapshotSuccess (SaveSnapshotSuccess successMessage) { LOG.debug ("{} saved ShardManager snapshot successfully. Deleting the prev snapshot if available", persistenceId()); deleteSnapshots(new SnapshotSelectionCriteria(scala.Long.MaxValue(), successMessage.metadata().timestamp() - 1, 0, 0)); }
SnapshotSelectionCriteria.create(snapshotRevision, Long.MAX_VALUE), snapshotRevision);
@Test public void testDoLoadAsyncWithAkkaSerializedSnapshot() throws IOException { SnapshotSerializer snapshotSerializer = new SnapshotSerializer((ExtendedActorSystem) system); String name = toSnapshotName(PERSISTENCE_ID, 1, 1000); try (FileOutputStream fos = new FileOutputStream(new File(SNAPSHOT_DIR, name))) { fos.write(snapshotSerializer.toBinary(new Snapshot("one"))); } SnapshotMetadata metadata = new SnapshotMetadata(PERSISTENCE_ID, 1, 1000); TestKit probe = new TestKit(system); snapshotStore.tell(new LoadSnapshot(PERSISTENCE_ID, SnapshotSelectionCriteria.latest(), Long.MAX_VALUE), probe.getRef()); LoadSnapshotResult result = probe.expectMsgClass(LoadSnapshotResult.class); Option<SelectedSnapshot> possibleSnapshot = result.snapshot(); assertEquals("SelectedSnapshot present", TRUE, possibleSnapshot.nonEmpty()); assertEquals("SelectedSnapshot metadata", metadata, possibleSnapshot.get().metadata()); assertEquals("SelectedSnapshot snapshot", "one", possibleSnapshot.get().snapshot()); }
@SuppressWarnings("checkstyle:illegalThrows") @Test(expected = IOException.class) public void testDoLoadAsyncWithFailure() throws Throwable { createSnapshotFile(PERSISTENCE_ID, null, 1, 2000); TestKit probe = new TestKit(system); snapshotStore.tell(new SnapshotProtocol.LoadSnapshot(PERSISTENCE_ID, SnapshotSelectionCriteria.latest(), Long.MAX_VALUE), probe.getRef()); LoadSnapshotFailed failed = probe.expectMsgClass(LoadSnapshotFailed.class); throw failed.cause(); }
private void handleSnapshotMessage(final Object message) { if (message instanceof SaveSnapshotFailure) { LOG.error("{}: failed to persist state", persistenceId(), ((SaveSnapshotFailure) message).cause()); persisting = false; self().tell(PoisonPill.getInstance(), ActorRef.noSender()); } else if (message instanceof SaveSnapshotSuccess) { LOG.debug("{}: got command: {}", persistenceId(), message); SaveSnapshotSuccess saved = (SaveSnapshotSuccess)message; deleteSnapshots(new SnapshotSelectionCriteria(saved.metadata().sequenceNr(), saved.metadata().timestamp() - 1, 0L, 0L)); persisting = false; unstash(); } else { LOG.debug("{}: stashing command {}", persistenceId(), message); stash(); } }
@Test public void testDoLoadAsyncWithNoSnapshots() { TestKit probe = new TestKit(system); snapshotStore.tell(new LoadSnapshot(PERSISTENCE_ID, SnapshotSelectionCriteria.latest(), Long.MAX_VALUE), probe.getRef()); LoadSnapshotResult result = probe.expectMsgClass(LoadSnapshotResult.class); Option<SelectedSnapshot> possibleSnapshot = result.snapshot(); assertEquals("SelectedSnapshot present", FALSE, possibleSnapshot.nonEmpty()); }
SnapshotSelectionCriteria.create(snapshotRevision, Long.MAX_VALUE), snapshotRevision);
@Override AbstractClientActorBehavior<?> onReceiveCommand(final Object command) { if (command instanceof SaveSnapshotFailure) { LOG.error("{}: failed to persist state", persistenceId(), ((SaveSnapshotFailure) command).cause()); return null; } else if (command instanceof SaveSnapshotSuccess) { LOG.debug("{}: got command: {}", persistenceId(), command); SaveSnapshotSuccess saved = (SaveSnapshotSuccess)command; context().deleteSnapshots(new SnapshotSelectionCriteria(saved.metadata().sequenceNr(), saved.metadata().timestamp() - 1, 0L, 0L)); return this; } else if (command instanceof DeleteSnapshotsSuccess) { LOG.debug("{}: got command: {}", persistenceId(), command); } else if (command instanceof DeleteSnapshotsFailure) { // Not treating this as a fatal error. LOG.warn("{}: failed to delete prior snapshots", persistenceId(), ((DeleteSnapshotsFailure) command).cause()); } else { LOG.debug("{}: stashing command {}", persistenceId(), command); context().stash(); return this; } context().unstash(); return context().createBehavior(myId); } }
@Test public void testDoLoadAsyncWithRetry() throws IOException { createSnapshotFile(PERSISTENCE_ID, "one", 0, 1000); createSnapshotFile(PERSISTENCE_ID, null, 1, 2000); SnapshotMetadata metadata = new SnapshotMetadata(PERSISTENCE_ID, 0, 1000); TestKit probe = new TestKit(system); snapshotStore.tell(new LoadSnapshot(PERSISTENCE_ID, SnapshotSelectionCriteria.latest(), Long.MAX_VALUE), probe.getRef()); LoadSnapshotResult result = probe.expectMsgClass(LoadSnapshotResult.class); Option<SelectedSnapshot> possibleSnapshot = result.snapshot(); assertEquals("SelectedSnapshot present", TRUE, possibleSnapshot.nonEmpty()); assertEquals("SelectedSnapshot metadata", metadata, possibleSnapshot.get().metadata()); assertEquals("SelectedSnapshot snapshot", "one", possibleSnapshot.get().snapshot()); }
@Override AbstractClientActorBehavior<?> onReceiveCommand(final Object command) { if (command instanceof SaveSnapshotFailure) { LOG.error("{}: failed to persist state", persistenceId(), ((SaveSnapshotFailure) command).cause()); return null; } else if (command instanceof SaveSnapshotSuccess) { LOG.debug("{}: got command: {}", persistenceId(), command); SaveSnapshotSuccess saved = (SaveSnapshotSuccess)command; context().deleteSnapshots(new SnapshotSelectionCriteria(saved.metadata().sequenceNr(), saved.metadata().timestamp() - 1, 0L, 0L)); return this; } else if (command instanceof DeleteSnapshotsSuccess) { LOG.debug("{}: got command: {}", persistenceId(), command); } else if (command instanceof DeleteSnapshotsFailure) { // Not treating this as a fatal error. LOG.warn("{}: failed to delete prior snapshots", persistenceId(), ((DeleteSnapshotsFailure) command).cause()); } else { LOG.debug("{}: stashing command {}", persistenceId(), command); context().stash(); return this; } context().unstash(); return context().createBehavior(myId); } }
@Test public void testDoLoadAsync() throws IOException { createSnapshotFile(PERSISTENCE_ID, "one", 0, 1000); createSnapshotFile(PERSISTENCE_ID, "two", 1, 2000); createSnapshotFile(PERSISTENCE_ID, "three", 1, 3000); createSnapshotFile(PREFIX_BASED_SHARD_PERSISTENCE_ID, "foo", 0, 1000); createSnapshotFile(PREFIX_BASED_SHARD_PERSISTENCE_ID, "bar", 1, 2000); createSnapshotFile(PREFIX_BASED_SHARD_PERSISTENCE_ID, "foobar", 1, 3000); createSnapshotFile("member-1-shard-default-oper", "foo", 0, 1000); createSnapshotFile("member-1-shard-toaster-oper", "foo", 0, 1000); new File(SNAPSHOT_DIR, "other").createNewFile(); new File(SNAPSHOT_DIR, "other-1485349217290").createNewFile(); SnapshotMetadata metadata3 = new SnapshotMetadata(PERSISTENCE_ID, 1, 3000); TestKit probe = new TestKit(system); snapshotStore.tell(new LoadSnapshot(PERSISTENCE_ID, SnapshotSelectionCriteria.latest(), Long.MAX_VALUE), probe.getRef()); LoadSnapshotResult result = probe.expectMsgClass(LoadSnapshotResult.class); Option<SelectedSnapshot> possibleSnapshot = result.snapshot(); assertEquals("SelectedSnapshot present", TRUE, possibleSnapshot.nonEmpty()); assertEquals("SelectedSnapshot metadata", metadata3, possibleSnapshot.get().metadata()); assertEquals("SelectedSnapshot snapshot", "three", possibleSnapshot.get().snapshot()); snapshotStore.tell(new LoadSnapshot(PREFIX_BASED_SHARD_PERSISTENCE_ID, SnapshotSelectionCriteria.latest(), Long.MAX_VALUE), probe.getRef()); result = probe.expectMsgClass(LoadSnapshotResult.class); possibleSnapshot = result.snapshot(); SnapshotMetadata prefixBasedShardMetada3 = new SnapshotMetadata(PREFIX_BASED_SHARD_PERSISTENCE_ID, 1, 3000); assertEquals("SelectedSnapshot present", TRUE, possibleSnapshot.nonEmpty()); assertEquals("SelectedSnapshot metadata", prefixBasedShardMetada3, possibleSnapshot.get().metadata()); assertEquals("SelectedSnapshot snapshot", "foobar", possibleSnapshot.get().snapshot()); }
@Override public void commit(final long sequenceNumber, long timeStamp) { LOG.debug("{}: Snapshot success - sequence number: {}", persistenceId(), sequenceNumber); if(applySnapshot != null) { try { Snapshot snapshot = applySnapshot.getSnapshot(); //clears the followers log, sets the snapshot index to ensure adjusted-index works context.setReplicatedLog(ReplicatedLogImpl.newInstance(snapshot, context)); context.setLastApplied(snapshot.getLastAppliedIndex()); context.setCommitIndex(snapshot.getLastAppliedIndex()); context.getTermInformation().update(snapshot.getElectionTerm(), snapshot.getElectionVotedFor()); if(snapshot.getServerConfiguration() != null) { context.updatePeerIds(snapshot.getServerConfiguration()); } if(snapshot.getState().length > 0 ) { applySnapshotProcedure.accept(snapshot.getState()); } applySnapshot.getCallback().onSuccess(); } catch (Exception e) { LOG.error("{}: Error applying snapshot", context.getId(), e); } } else { context.getReplicatedLog().snapshotCommit(); } context.getPersistenceProvider().deleteSnapshots(new SnapshotSelectionCriteria(sequenceNumber, timeStamp - 1, 0L, 0L)); context.getPersistenceProvider().deleteMessages(lastSequenceNumber); snapshotComplete(); }