SnapshotManager implements SnapshotState { @Override public long trimLog(final long desiredTrimIndex) { return currentState.trimLog(desiredTrimIndex); } SnapshotManager(final RaftActorContext context, final Logger logger); boolean isApplying(); @Override boolean isCapturing(); @Override boolean captureToInstall(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex,
            final String targetFollower); @Override boolean capture(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex); @Override boolean captureWithForcedTrim(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex); @Override void apply(final ApplySnapshot snapshot); @Override void persist(final Snapshot.State state, final Optional<OutputStream> installSnapshotStream,
            final long totalMemory); @Override void commit(final long sequenceNumber, final long timeStamp); @Override void rollback(); @Override long trimLog(final long desiredTrimIndex); Snapshot.@NonNull State convertSnapshot(final ByteSource snapshotBytes); long getLastSequenceNumber(); @VisibleForTesting CaptureSnapshot getCaptureSnapshot(); CaptureSnapshot newCaptureSnapshot(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex,
                                              final boolean mandatoryTrim);  }
@Test public void testTrimLogWhenTrimIndexLessThanLastApplied() { doReturn(20L).when(mockRaftActorContext).getLastApplied(); ReplicatedLogEntry replicatedLogEntry = mock(ReplicatedLogEntry.class); doReturn(true).when(mockReplicatedLog).isPresent(10); doReturn(replicatedLogEntry).when(mockReplicatedLog).get(10); doReturn(5L).when(replicatedLogEntry).getTerm(); long retIndex = snapshotManager.trimLog(10); assertEquals("return index", 10L, retIndex); verify(mockReplicatedLog).snapshotPreCommit(10, 5); verify(mockReplicatedLog).snapshotCommit(); verify(mockRaftActorBehavior, never()).setReplicatedToAllIndex(anyLong()); }
@Test public void testTrimLogWhenLastAppliedNotSet() { doReturn(-1L).when(mockRaftActorContext).getLastApplied(); ReplicatedLogEntry replicatedLogEntry = mock(ReplicatedLogEntry.class); doReturn(true).when(mockReplicatedLog).isPresent(10); doReturn(replicatedLogEntry).when(mockReplicatedLog).get(10); doReturn(5L).when(replicatedLogEntry).getTerm(); long retIndex = snapshotManager.trimLog(10); assertEquals("return index", -1L, retIndex); verify(mockReplicatedLog, never()).snapshotPreCommit(anyLong(), anyLong()); verify(mockReplicatedLog, never()).snapshotCommit(); verify(mockRaftActorBehavior, never()).setReplicatedToAllIndex(anyLong()); }
@Test public void testTrimLogWhenLastAppliedZero() { doReturn(0L).when(mockRaftActorContext).getLastApplied(); ReplicatedLogEntry replicatedLogEntry = mock(ReplicatedLogEntry.class); doReturn(true).when(mockReplicatedLog).isPresent(10); doReturn(replicatedLogEntry).when(mockReplicatedLog).get(10); doReturn(5L).when(replicatedLogEntry).getTerm(); long retIndex = snapshotManager.trimLog(10); assertEquals("return index", -1L, retIndex); verify(mockReplicatedLog, never()).snapshotPreCommit(anyLong(), anyLong()); verify(mockReplicatedLog, never()).snapshotCommit(); verify(mockRaftActorBehavior, never()).setReplicatedToAllIndex(anyLong()); }
@Test public void testTrimLogWhenTrimIndexNotPresent() { doReturn(20L).when(mockRaftActorContext).getLastApplied(); doReturn(false).when(mockReplicatedLog).isPresent(10); long retIndex = snapshotManager.trimLog(10); assertEquals("return index", -1L, retIndex); verify(mockReplicatedLog, never()).snapshotPreCommit(anyLong(), anyLong()); verify(mockReplicatedLog, never()).snapshotCommit(); verify(mockRaftActorBehavior).setReplicatedToAllIndex(10L); }