Alejandro Benito-Marcos created KAFKA-21753:
-----------------------------------------------

             Summary: KRaftControlRecordStateMachine does not rewind nextOffset 
on truncateNewEntries, so control records appended in the truncated range are 
never read
                 Key: KAFKA-21753
                 URL: https://issues.apache.org/jira/browse/KAFKA-21753
             Project: Kafka
          Issue Type: Bug
          Components: kraft
    Affects Versions: 4.4.0
            Reporter: Alejandro Benito-Marcos


KRaftControlRecordStateMachine.truncateNewEntries(endOffset) truncates 
voterSetHistory and kraftVersionHistory, but it does not update nextOffset, the 
offset from which maybeLoadLog() reads the log.

When a replica handles a diverging epoch in a FETCH response, KafkaRaftClient 
calls log.truncateToEndOffset(...) and then 
partitionState.truncateNewEntries(truncationOffset). If the internal listener 
had already read past the truncation offset (it reads uncommitted records), 
nextOffset stays beyond the new log end offset. The new leader's records are 
then appended starting at the truncation offset, and maybeLoadLog() continues 
reading from the stale nextOffset, so any VotersRecord or KRaftVersionRecord 
between the truncation offset and the old nextOffset is skipped.

Consequence:
The replica keeps a voter set (lastVoterSet() and voterSetAtOffset()) that does 
not match its own log, until it restarts and rebuilds the state from the 
snapshot and log. Voter set membership drives elections, majority counting and 
high-watermark computation, so the replica may act on the wrong quorum after a 
reconfiguration that raced with a leader change.

Reproduction (unit test in KRaftControlRecordStateMachineTest, reproduced on 
trunk 24b0a8355b):
1. Epoch 1: append voter set [4, 5, 6], then an uncommitted voter set [4, 5, 6, 
7] at divergenceOffset, and call updateState().
2. Truncate the log and the state machine to divergenceOffset, as 
KafkaRaftClient does on a diverging epoch.
3. Epoch 2: append voter set [4, 5, 6, 8] at divergenceOffset plus one more 
control record, and call updateState().

Expected: lastVoterSet() is [4, 5, 6, 8].
Actual: lastVoterSet() is still [4, 5, 6]. The record at divergenceOffset is in 
the log but is never read.

{code:java}
log.truncateTo(divergenceOffset);
partitionState.truncateNewEntries(divergenceOffset);

log.appendAsLeader(MemoryRecords.withVotersRecord(divergenceOffset, 0, newEpoch,
    bufferSupplier.get(300), newVoterSet.toVotersRecord((short) 0)), newEpoch);
// one more KRaftVersionRecord
partitionState.updateState();

assertEquals(newVoterSet, partitionState.lastVoterSet()); // fails: 
firstVoterSet
{code}

Proposed fix:
Rewind the read cursor in truncateNewEntries:

{code:java}
nextOffset = Math.min(nextOffset, endOffset);
{code}

truncateNewEntries and maybeLoadLog both run on the raft thread, so no extra 
synchronization is needed. With this change the test above passes, and the 
existing KRaftControlRecordStateMachineTest tests and checkstyle still pass.

I'd like to work on this and will open a PR with the fix and the regression 
test.



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

Reply via email to