Skip to content

RATIS-2834. Clear the pending leadership transfer on immediate failure - #1710

Merged
szetszwo merged 1 commit into
apache:masterfrom
d-wang-commit:RATIS-2834
Nov 28, 2026
Merged

szetszwo merged 1 commit into
apache:masterfrom
d-wang-commit:RATIS-2834

Conversation

@d-wang-commit

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

When a follower's log is up to date but its state machine is too far behind, TransferLeadership.start immediately rejects the transfer with FAILED_APPLIED_INDEX_GAP. It completes the request's future but leaves the request in pending. The current leader then continues to report that it is stepping down and can reject writes and configuration changes.

Clear pending before completing the failed request. The compare-and-set clears it only if it still refers to that request.

Generated-by: OpenAI Codex (GPT-6)

What is the link to the Apache JIRA

RATIS-2834

How was this patch tested?

Extended testRiskyLeadershipTransfer to wait for log replication while the follower's state machine is paused. After the transfer is rejected, the test verifies that the original leader accepts a write in the same term.

Before the fix, the write failed with LeaderSteppingDownException on babaf87d5. With the fix, all 29 tests run against master passed: the 27 tests in TestLeaderElectionWithGrpc and the regression test with both Netty and simulated RPC.

@amaliujia amaliujia left a comment

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.

Thanks for catching the issue!

pendingRequest.complete(result);
if (pending.compareAndSet(pendingRequest, null)) {
pendingRequest.complete(result);
}

@amaliujia amaliujia Nov 26, 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.

Why don't we always complete the result if the result is already in the bad cases. If pending does not refer to the pendingRequest, who will be in charge of complete pendingRequest?

pending.compareAndSet(pendingRequest, null);
pendingRequest.complete(result);

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.

If the CAS fails, the callback that took this request out of pending is responsible for completing its future. For example, the follower append-reply path calls TransferLeadership.complete(result), which does pending.getAndSet(null) and then r.complete(result) on the request it just removed. start() only installs a new request when the slot is empty.

There is a small window between those two operations: the callback has taken the request, but its future may still be incomplete. Calling pendingRequest.complete(result) unconditionally in that window could publish the immediate failure before the callback publishes its own result. Keeping the call inside the if lets the path that removed the request decide the reply. Completion path

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.

I see. The implementation contract is then who set the reference as null (thus take the reference) will own the lifecycle.

@amaliujia amaliujia left a comment

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.

LGTM

Triggering the CI jobs.

@d-wang-commit

Copy link
Copy Markdown
Contributor Author

The test failure appears unrelated to this PR. I’ve rebased the branch onto the latest master.

@szetszwo szetszwo left a comment

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.

+1 the change looks good.

@szetszwo
szetszwo merged commit 2a763e0 into apache:master Nov 28, 2026
17 checks passed
@szetszwo

Copy link
Copy Markdown
Contributor

@d-wang-commit , thanks for working on this!

@amaliujia , thanks for reviewing this!

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.

3 participants