Perfloop Agent created KAFKA-21680:
--------------------------------------

             Summary: Preserve transaction stability when truncating above the 
high watermark
                 Key: KAFKA-21680
                 URL: https://issues.apache.org/jira/browse/KAFKA-21680
             Project: Kafka
          Issue Type: Bug
          Components: log
            Reporter: Perfloop Agent


When a follower truncates diverging uncommitted records above its high 
watermark (`AbstractFetcherThread.truncateOnFetchResponse` -> 
`ReplicaFetcherThread.truncate` -> `Partition.truncateTo` -> 
`UnifiedLog.truncateTo(targetOffset)` with `targetOffset > highWatermark`), 
`UnifiedLog` and `ProducerStateManager` lose or fail to refresh transactional 
stability bounds.

Three failure modes occur on current `trunk`:

1. Premature LSO advance / `READ_COMMITTED` isolation violation:
   Write transactional records at offset 0 (< HW = 1) and a COMMIT or ABORT 
marker at offset 1 (>= HW = 1, unreplicated), roll a segment/producer snapshot 
at offset 2, append uncommitted tail records at 2..3, and truncate to offset 3. 
`ProducerStateManager.truncateAndReload` unconditionally calls 
`unreplicatedTxns.clear()` and reloads snapshot 2, which does not store 
`unreplicatedTxns`. Once the next append refreshes 
`firstUnstableOffsetMetadata`, `lastStableOffset` advances from 0 to 1 (= HW) 
even though the control marker at offset 1 is still unreplicated.

2. LSO regression onto an already-replicated transaction (without a segment 
roll):
   With completed transaction (0, 1) already replicated (`HW = 2`, `LSO = 2`) 
and tail records at 2..3, calling `truncateTo(3)` without a snapshot at offset 
2 replays `[0, 3)` and re-adds (0, 1) to `unreplicatedTxns`. Because 
`UnifiedLog.truncateTo` skips 
`producerStateManager.onHighWatermarkUpdated(highWatermark())` when 
`highWatermark < logEndOffset`, the next append regresses `lastStableOffset` 
from 2 back to 0.

3. Stale `firstUnstableOffset` past `logEndOffset`:
   Truncating an ongoing transaction above the high watermark leaves the cached 
`firstUnstableOffsetMetadata` pointing at the truncated offset because 
`UnifiedLog.truncateTo` does not call `maybeIncrementFirstUnstableOffset()` on 
the `highWatermark < logEndOffset` branch.

Affected reads and metrics:
- `FetchIsolation.TXN_COMMITTED` reads (`UnifiedLog.read`, including KIP-392 
follower fetches and leader reads if promoted before the high watermark passes 
the control marker) can return undecided/unreplicated transactional batches or 
stall behind a regressed LSO.
- `ListOffsets` requests with `IsolationLevel.READ_COMMITTED` 
(`Partition.fetchOffsetForTimestamp` / LSO lookup).
- `LastStableOffsetLag` metrics.

Relation to existing issues:
- KAFKA-14497 (unreplicated transactions lost on log reopen) shares the 
snapshot-format limitation, because producer snapshots do not persist 
`unreplicatedTxns`. Fixing live truncation in memory does not fix log reopen (a 
fresh `ProducerStateManager` has no in-memory entries to retain), so 
KAFKA-14497 requires a separate change.
- KAFKA-21551 (`rebuildProducerState` skipping log replay when producer state 
was empty before truncation) is a separate path and remains out of scope.



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

Reply via email to