}
@Override
- public void snapshotCommit() {
+ public void snapshotCommit(final boolean updateDataSize) {
snapshottedJournal = null;
previousSnapshotIndex = -1;
previousSnapshotTerm = -1;
- // need to recalc the datasize based on the entries left after precommit.
- int newDataSize = 0;
- for (ReplicatedLogEntry logEntry : journal) {
- newDataSize += logEntry.size();
+ if (updateDataSize) {
+ // need to recalc the datasize based on the entries left after precommit.
+ int newDataSize = 0;
+ for (ReplicatedLogEntry logEntry : journal) {
+ newDataSize += logEntry.size();
+ }
+ LOG.trace("{}: Updated dataSize from {} to {}", logContext, dataSize, newDataSize);
+ dataSize = newDataSize;
}
- LOG.trace("{}: Updated dataSize from {} to {}", logContext, dataSize, newDataSize);
- dataSize = newDataSize;
}
@Override
void snapshotPreCommit(long snapshotCapturedIndex, long snapshotCapturedTerm);
/**
- * Sets the Replicated log to state after snapshot success.
+ * Sets the Replicated log to state after snapshot success. This method is equivalent to
+ * {@code snapshotCommit(true)}.
*/
- void snapshotCommit();
+ default void snapshotCommit() {
+ snapshotCommit(true);
+ }
+
+ /**
+ * Sets the Replicated log to state after snapshot success. Most users will want to use {@link #snapshotCommit()}
+ * instead.
+ *
+ * @param updateDataSize true if {@link #dataSize()} should also be updated
+ */
+ void snapshotCommit(boolean updateDataSize);
/**
* Restores the replicated log to a state in the event of a save snapshot failure.
//use the term of the temp-min, since we check for isPresent, entry will not be null
ReplicatedLogEntry entry = context.getReplicatedLog().get(tempMin);
context.getReplicatedLog().snapshotPreCommit(tempMin, entry.getTerm());
- context.getReplicatedLog().snapshotCommit();
+ context.getReplicatedLog().snapshotCommit(false);
return tempMin;
}
MockitoAnnotations.initMocks(this);
context = new RaftActorContextImpl(null, null, "test",
- new ElectionTermImpl(mockPersistence, "test", LOG), -1, -1, Collections.<String,String>emptyMap(),
+ new ElectionTermImpl(mockPersistence, "test", LOG), -1, -1, Collections.emptyMap(),
configParams, mockPersistence, applyState -> { }, LOG, MoreExecutors.directExecutor());
}
- private void verifyPersist(Object message) throws Exception {
+ private void verifyPersist(final Object message) throws Exception {
verifyPersist(message, new Same(message), true);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
- private void verifyPersist(Object message, ArgumentMatcher<?> matcher, boolean async) throws Exception {
+ private void verifyPersist(final Object message, final ArgumentMatcher<?> matcher, final boolean async)
+ throws Exception {
ArgumentCaptor<Procedure> procedure = ArgumentCaptor.forClass(Procedure.class);
if (async) {
verify(mockPersistence).persistAsync(argThat(matcher), procedure.capture());
verifyNoMoreInteractions(mockPersistence);
}
- public ArgumentMatcher<DeleteEntries> match(final DeleteEntries actual) {
+ @Test
+ public void testCommitFakeSnapshot() {
+ ReplicatedLog log = ReplicatedLogImpl.newInstance(context);
+
+ log.append(new SimpleReplicatedLogEntry(0, 1, new MockPayload("0")));
+ final int dataSizeAfterFirstPayload = log.dataSize();
+
+ log.snapshotPreCommit(0,1);
+ log.snapshotCommit(false);
+
+ assertEquals(0, log.size());
+ assertEquals(dataSizeAfterFirstPayload, log.dataSize());
+ }
+
+ private static ArgumentMatcher<DeleteEntries> match(final DeleteEntries actual) {
return other -> actual.getFromIndex() == other.getFromIndex();
}
}
assertEquals("return index", 10L, retIndex);
verify(mockReplicatedLog).snapshotPreCommit(10, 5);
- verify(mockReplicatedLog).snapshotCommit();
+ verify(mockReplicatedLog).snapshotCommit(false);
verify(mockRaftActorBehavior, never()).setReplicatedToAllIndex(anyLong());
}
assertEquals("return index", -1L, retIndex);
verify(mockReplicatedLog, never()).snapshotPreCommit(anyLong(), anyLong());
- verify(mockReplicatedLog, never()).snapshotCommit();
+ verify(mockReplicatedLog, never()).snapshotCommit(false);
verify(mockRaftActorBehavior, never()).setReplicatedToAllIndex(anyLong());
}
assertEquals("return index", -1L, retIndex);
verify(mockReplicatedLog, never()).snapshotPreCommit(anyLong(), anyLong());
- verify(mockReplicatedLog, never()).snapshotCommit();
+ verify(mockReplicatedLog, never()).snapshotCommit(false);
verify(mockRaftActorBehavior, never()).setReplicatedToAllIndex(anyLong());
}
assertEquals("return index", -1L, retIndex);
verify(mockReplicatedLog, never()).snapshotPreCommit(anyLong(), anyLong());
- verify(mockReplicatedLog, never()).snapshotCommit();
+ verify(mockReplicatedLog, never()).snapshotCommit(false);
// Trim index is greater than replicatedToAllIndex so should update it.
verify(mockRaftActorBehavior).setReplicatedToAllIndex(10L);
snapshotManager.trimLog(10);
verify(mockReplicatedLog, never()).snapshotPreCommit(anyLong(), anyLong());
- verify(mockReplicatedLog, never()).snapshotCommit();
-
+ verify(mockReplicatedLog, never()).snapshotCommit(false);
}
@Test
verify(mockReplicatedLog, never()).snapshotPreCommit(10, 5);
verify(mockReplicatedLog, never()).snapshotCommit();
-
}
@Test