Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ private void removeStateMachineDataIfNeeded(long index) {
.getFollowerNextIndices()).min().getAsLong();
LOG.debug("Removing data corresponding to log index {} min index {} "
+ "from cache", index, minIndex);
stateMachineDataCache.removeIf(k -> k >= (Math.min(minIndex, index)));
removeCacheDataUpTo(Math.min(minIndex, index));
}
} catch (Exception e) {
throw new RuntimeException(e);
Expand All @@ -873,9 +873,7 @@ public CompletableFuture<Message> applyTransaction(TransactionContext trx) {
removeStateMachineDataIfNeeded(index);
// if waitOnBothFollower is false, remove the entry from the cache
// as soon as its applied and such entry exists in the cache.
if (!waitOnBothFollowers) {
stateMachineDataCache.removeIf(k -> k >= index);
}
removeStateMachineDataIfMajorityFollowSync(index);
DispatcherContext.Builder builder =
new DispatcherContext.Builder().setTerm(trx.getLogEntry().getTerm())
.setLogIndex(index);
Expand Down Expand Up @@ -982,6 +980,18 @@ public CompletableFuture<Message> applyTransaction(TransactionContext trx) {
}
}

private void removeStateMachineDataIfMajorityFollowSync(long index) {
if (!waitOnBothFollowers) {
// if majority follow in sync, remove all cache previous to current index
// including current index
removeCacheDataUpTo(index);
}
}

private void removeCacheDataUpTo(long index) {
stateMachineDataCache.removeIf(k -> k <= index);
}

private static <T> CompletableFuture<T> completeExceptionally(Exception e) {
final CompletableFuture<T> future = new CompletableFuture<>();
future.completeExceptionally(e);
Expand All @@ -996,7 +1006,7 @@ public void notifyNotLeader(Collection<TransactionContext> pendingEntries) {

@Override
public CompletableFuture<Void> truncate(long index) {
stateMachineDataCache.removeIf(k -> k >= index);
stateMachineDataCache.removeIf(k -> k > index);
return CompletableFuture.completedFuture(null);
}

Expand Down