Skip to content

RATIS-2399. Introduce separate locks for commitIndex and snapshotIndex in RaftLogBase - #1612

Open
Elia-Renzoni wants to merge 4 commits into
apache:masterfrom
Elia-Renzoni:refactor/avoid-single-lock
Open

Elia-Renzoni wants to merge 4 commits into
apache:masterfrom
Elia-Renzoni:refactor/avoid-single-lock

Conversation

@Elia-Renzoni

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Replace the single lock for both snapshotIndex and commitIndex in RaftLogBase with two separate locks.

What is the link to the Apache JIRA

Jira Issue link:
https://issues.apache.org/jira/browse/RATIS-2399?page=com.atlassian.jira.plugin.system.issuetabpanels%3Aall-tabpanel

How was this patch tested?

This patch was tested via unit test

Signed-off-by: Elia Renzoni <elia.renzoni03@gmail.com>
Signed-off-by: Elia Renzoni <elia.renzoni03@gmail.com>
Comment on lines +156 to +169
try(AutoCloseableLock writeLock = writeLock()) {
try(AutoCloseableLock commitLock = writeLock(LockType.COMMIT_INDEX);
AutoCloseableLock snapshotLock = writeLock(LockType.SNAPSHOT_INDEX)) {

@szetszwo szetszwo Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Elia-Renzoni , SNAPSHOT_INDEX is only acquired here together with COMMIT_INDEX. All the other cases acquire COMMIT_INDEX. Since it already has COMMIT_INDEX, acquiring SNAPSHOT_INDEX has no effect. No?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. I've pushed commit fac0204 with the fix. However, I've changed the original scope of the PR quite a bit I had previously added two separate locks, but since snapshotIndexLock was effectively unused, I've gone back to a single lock as before and used updateToMax to update the index atomically. What do you think of the latest changes?

Elia-Renzoni and others added 2 commits October 6, 2026 14:18
Revert the separate COMMIT_INDEX/SNAPSHOT_INDEX locks: SNAPSHOT_INDEX was
only ever acquired together with COMMIT_INDEX, so it had no effect.

Instead, update snapshotIndex and commitIndex in updateSnapshotIndex with
RaftLogIndex.updateToMax, so it no longer needs the log lock. To keep
commitIndex monotonic against this lock-free writer, updateCommitIndex
also uses updateToMax; it still holds the log lock to keep its checks on
the flush index and entry term consistent with the log.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Elia Renzoni <elia.renzoni03@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Elia Renzoni <elia.renzoni03@gmail.com>
@Elia-Renzoni
Elia-Renzoni requested a review from szetszwo October 6, 2026 12:35

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants