Skip to content

Commit

Permalink
Delete snapshot chunks while truncating log entries
Browse files Browse the repository at this point in the history
  • Loading branch information
metanet committed Mar 24, 2024
1 parent 04b620f commit 25fc07b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ public void truncateLogEntriesFrom(@Nonnegative long logIndexInclusive) {
@Override
public void truncateLogEntriesUntil(@Nonnegative long logIndexInclusive) {
dsl.deleteFrom(LOG_ENTRIES).where(INDEX.lessOrEqual(logIndexInclusive)).execute();
// we can remove all snapshot chunks belonging to the previous log indices
dsl.deleteFrom(SNAPSHOT_CHUNKS).where(INDEX.lessThan(logIndexInclusive)).execute();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,25 @@ public void testLogEntryFlushing() {
public void testSnapshots() throws IOException {
withRaftStore(RaftSqliteStoreTest::persistInitialState);
withRaftStore(store -> {
store.persistLogEntries(List.of(logEntry(1, 1), logEntry(2, 1), logEntry(3, 1)));
store.persistLogEntries(List.of(logEntry(1, 1), logEntry(2, 1), logEntry(3, 1), logEntry(4, 1)));
store.flush();
store.persistSnapshotChunk(snapshotChunk(2, 0, 0, 1));
store.persistSnapshotChunk(snapshotChunk(2, 1, 0, 1));
store.flush();
// once a snapshot chunk has been flushed, irrelevant log entries can be deleted
store.truncateLogEntriesUntil(2);
store.flush();
store.persistSnapshotChunk(snapshotChunk(3, 1, 0, 1));
store.flush();
// once a snapshot chunk has been flushed, irrelevant log entries can be deleted
store.truncateLogEntriesUntil(3);
store.flush();
assertThat(store.getAllSnapshotChunks()).usingRecursiveFieldByFieldElementComparator()
.containsExactly(snapshotChunk(3, 1, 0, 1));
RestoredRaftState restoredRaftState = store.getRestoredRaftState(false).get();
assertThat(restoredRaftState.getLogEntries()).usingRecursiveFieldByFieldElementComparator()
.containsExactly(logEntry(3, 1));
.containsExactly(logEntry(4, 1));
assertThat(restoredRaftState.getSnapshotEntry().getOperation()).usingRecursiveComparison()
.isEqualTo(List.of(snapshotChunk(2, 0, 0, 1)));
.isEqualTo(List.of(snapshotChunk(3, 1, 0, 1)));
});
sqlite = new File(tempDir.newFolder(), "sqlite.db");
withRaftStore(RaftSqliteStoreTest::persistInitialState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public boolean isKnownMember(RaftEndpoint endpoint) {
}

/**
* Returns true if the given endpoint is a voting member of the Raft group, false
* otherwise.
* Returns true if the given endpoint is a voting member of the Raft group,
* false otherwise.
*/
public boolean isVotingMember(RaftEndpoint endpoint) {
return votingMembers.contains(endpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,8 @@ public boolean isKnownMember(RaftEndpoint endpoint) {
}

/**
* Returns true if the given endpoint is a voting member in the effective group members, false
* otherwise.
* Returns true if the given endpoint is a voting member in the effective group
* members, false otherwise.
*/
public boolean isVotingMember(RaftEndpoint endpoint) {
return effectiveGroupMembers.isVotingMember(endpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,15 @@ void persistAndFlushLocalEndpoint(@Nonnull RaftEndpointPersistentState localEndp

/**
* MicroRaft calls this method after it successfully persists and flushes a
* snapshot. This method is used for deleting log entries that are before the
* snapshot index and are not needed to be restored.
* snapshot. This method is used for deleting log entries and snapshot chunks
* that are before the snapshot index and are not needed to be restored.
*
* This is merely an optimization method and its side-effects can be sync'ed to
* the storage when {@link #flush()} is called,
*
* @param logIndexInclusive
* the log index value until which the log entries must be truncated
* the log index value until which the log entries and snapshot
* chunks must be truncated
*
* @throws IOException
* if any failure occurs during truncating the log entries
Expand Down

0 comments on commit 25fc07b

Please sign in to comment.