Ivan Andika created RATIS-2847:
----------------------------------

             Summary: Delayed lower-term AppendEntries can overwrite entries 
committed by a newer leader
                 Key: RATIS-2847
                 URL: https://issues.apache.org/jira/browse/RATIS-2847
             Project: Ratis
          Issue Type: Bug
            Reporter: Ivan Andika
            Assignee: Ivan Andika


An AppendEntries request can pass its initial term/prefix checks, wait for 
metadata persistence or an earlier append, and resume after a newer leader has 
replaced and committed the conflicting suffix. The old request can then mutate 
the log using its earlier validation result.

The eventual reply can correctly be `NOT_LEADER` while the committed 
higher-term entry has already been overwritten. Checking the term only before 
replying does not prevent this mutation.

This is a follow-up to the stale-reply protection in RATIS-2644: the defect 
here is invalid log mutation before the reply, rather than only returning a 
stale `SUCCESS`.
h2. Deterministic reproduction

1. Initialize a follower with a term-1 entry at index 0.
2. Admit a term-1 append for index 1 with an additional uncommitted suffix at 
index 2.
3. Hold the old request at its metadata-persistence boundary after admission 
and before log mutation.
4. Deliver a term-2 append that installs a different entry at index 1 and 
commits it.
5. Wait until the follower applies the term-2 entry.
6. Release the delayed term-1 request.
7. Inspect both the reply and the persisted/log-visible entries.

Before the fix, the old request can reply `NOT_LEADER` but replace index 1 with 
its term-1 entry and resurrect its old suffix. The overwrite reproduction 
covers composed appends enabled and disabled.
h2. Expected behavior

The committed term-2 entry must remain intact, the obsolete suffix must not 
reappear, and the old RPC must be rejected using the follower's current term.
h2. Root cause

Admission checks were separated from the eventual mutation by asynchronous 
waits. The request's term, lifecycle, committed/snapshot boundaries and actual 
log prefix could all change during that interval. A final response-time check 
happened after the destructive work.

Composed requests add another requirement: sharing an earlier append's future 
must not produce `SUCCESS` for a different term/index that was never appended.
h2. Proposed fix

Immediately before submitting the mutation, revalidate the request under the 
server monitor. Update configurations only after validation. Keep persistence 
and disk-flush waits outside that monitor, and serialize final 
acknowledgement/commit advancement with term changes.

Before acknowledging a composed request, verify that its requested final 
term/index actually exists. Skip obsolete work without poisoning the shared 
append chain, and reject temporarily paused RPCs separately so replication can 
resume.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to