Preserve valid log records when recovery I/O fails

perfloop-oss/kafka · Ignored Failure

https://perfloop.ai/t/oss/case_67xk79x7tf

Verdict

VERIFIED · settled 2026-11-23 · pull request opened as apache/kafka#24336

What happened: The check fails before this change and passes with it.

Hypothesis

Unclean-shutdown recovery can erase valid records when writing a producer-state snapshot fails. A deterministic native test loses its one valid record and recovery returns normally. The incidence of such failures and the consequences across replicas remain unmeasured.

In `LogLoader.recoverLog`, the catch at lines 473–476 treats any `IOException` from `recoverSegment` like an invalid offset and truncates the segment to its base offset. `recoverSegment` includes both rebuilding producer state and `takeSnapshot()` after successful segment recovery. If the fallback truncates a nonempty segment, lines 478–484 also remove later unflushed segments. This claim concerns those unclean-recovery branches, not clean-shutdown startup or every exception in `load()`. Propagating storage errors has an existing owner: `LogManager.loadLogs` records an escaping `IOException` and reports the log directory offline.

The new JUnit test `storage/src/test/java/org/apache/kafka/storage/internals/log/LogLoaderRecoveryFailureTest.java`, method `snapshotWriteFailureMustNotTruncateValidRecord`, uses one valid v2 record at offset zero, recovery point zero, and one injected `IOException` at the existing public `ProducerStateManager.writeSnapshot` seam for snapshot offset one. It permits the error to escape or be handled, but requires valid record bytes to survive. Command: `./gradlew :storage:test --tests org.apache.kafka.storage.internals.log.LogLoaderRecoveryFailureTest --no-daemon --build-cache --max-workers=2 -PmaxParallelForks=1 -PtestLoggingEvents=passed,failed --console=plain`. The completed run reported `2 tests completed, 1 failed`; the reduced witness failed with `array lengths differ, expected: <69> but was: <0>`. Its diagnostic was `injected=1 returned=RecoveryOffsets[newRecoveryPoint=0, nextOffset=0] propagated=null beforeBytes=69 afterBytes=0 segments=1 nextOffset=0`. The companion test completed all 60 combinations of one to three segments, one to two records per segment, clean/unclean shutdown, and recovery checkpoints from zero through one past the end. It found no record or checkpoint violation in that no-fault domain; this is not an unbounded guarantee.

The proof target is preservation of valid log data when recovery storage work fails, with native regression coverage and unchanged handling of genuinely corrupt tails and recovery checkpoints. Extending the mock-based witness to a fault matrix and replay through `LogManager` is a provisional proof method, not a required test layout.

Inspected upstream implementations are distinct: https://github.com/apache/kafka/pull/23975 serializes checkpoint sampling and writing, while https://github.com/apache/kafka/pull/24239 changes record iteration; neither changes this catch or snapshot-failure response. The bounded dev-list search https://www.mail-archive.com/search?l=dev%40kafka.apache.org&q=%22snapshot%22+%22recovery%22+%22IOException%22 found older snapshot-cleanup and startup-sanity discussions, not this repair. The inspected 200-commit history window contains no implementation of this change. Older open-PR pages are still being paginated, so upstream coverage is not yet complete.

Change to test: Distinguish invalid record or offset data from storage I/O failures during recovery. Route I/O failures to startup's directory-failure handling, or handle a rebuildable snapshot failure without destroying log data, while retaining intentional corrupt-tail truncation.

Where it lives

perfloop-oss/kafka · storage/src/main/java/org/apache/kafka/storage/internals/log/LogManager.java

Evidence

The check fails before this change and passes with it: `During unclean log loading at recovery point zero, a producer snapshot write failure after recovering the first of two valid one-record segments preserves both log files and their exact record bytes, and propagates an IOException.`

Checks: 1 of 1 passed. Verification: no defect found.

Timeline