Skip to content

[VL] Fix false-EOF probe in ColumnarCachedBatchSerializer.read trailing markers#12147

Merged
yaooqinn merged 1 commit into
apache:mainfrom
yaooqinn:users/kentyao/gluten-ccbs-available-probe-bug
May 28, 2026
Merged

[VL] Fix false-EOF probe in ColumnarCachedBatchSerializer.read trailing markers#12147
yaooqinn merged 1 commit into
apache:mainfrom
yaooqinn:users/kentyao/gluten-ccbs-available-probe-bug

Conversation

@yaooqinn
Copy link
Copy Markdown
Member

@yaooqinn yaooqinn commented May 26, 2026

What changes were proposed in this pull request?

ColumnarCachedBatchSerializer.read used input.available() > 0 as
an EOF probe before reading the trailing hasStats / hasSchema
boolean markers. This PR replaces the probe with a direct readBoolean()
inside a narrow try/catch on KryoException whose message starts
with "Buffer underflow", which is the actual signal Kryo emits at
true end-of-input.

The V1 wire back-compat behavior (absent trailing booleans read as
null, not throw) is preserved — and is locked by the existing test
ColumnarCachedBatchKryoSuite#"V1 wire (no trailing hasStats/hasSchema booleans) reads as stats=null/schema=null".

Why are the changes needed?

Kryo Input.available() returns
(limit - position) + underlyingStream.available(). The JDK
InputStream.available() contract permits any implementation to
return 0 even when more data follows — BufferedInputStream over
shuffle-spill / network chunk boundaries routinely does so. When the
Kryo buffer is drained AND the underlying stream reports 0 at the
trailing-boolean byte position, the probe falsely concludes EOF,
skips hasStats, and the next readClassAndObject interprets the
stats payload (which contains the schema JSON) as a class name.

Production stack we observed (truncated):

ClassNotFoundException: {"type":"struct","fields":[...
  at DefaultClassResolver.readName(DefaultClassResolver.java:154)
  at DefaultClassResolver.readClass(DefaultClassResolver.java:133)
  at Kryo.readClass(Kryo.java:693)
  at Kryo.readClassAndObject(Kryo.java:804)
  at ColumnarCachedBatchSerializer$$anon$2.hasNext(...)

The length-bound require(... <= maxLen ...) guard from
491070bf34 is preserved — it remains useful against
NegativeArraySizeException / oversized allocation and is
orthogonal to the EOF-probe fix.

Why catch KryoException by message prefix?

Kryo 4.x (the version on Gluten's classpath via kryo-shaded) has
no dedicated KryoBufferUnderflowException subclass — that was added
in Kryo 5.x. The message-prefix check is the narrowest filter we can
apply on 4.x without swallowing real corruption (e.g. a
ClassNotFoundException wrapped during readClassAndObject, which
does not start with "Buffer underflow").

Does this PR introduce any user-facing change?

No (bug fix in a build that has not shipped to any tagged release).

How was this patch tested?

New ColumnarCachedBatchKryoBoundaryProbeBugSuite:

  • "multi-batch deserialize survives boundary-aligned trailing-boolean probe"
    — deterministic repro of the production stack using a
    1-byte-per-read InputStream that returns available() == 0 at
    the trailing-boolean position. Fails on the pre-patch code, passes
    after the fix.

Existing ColumnarCachedBatchKryoSuite#"V1 wire ..." continues to
pass, locking the V1 silent-null back-compat contract.

Verified locally on -Pspark-4.1 -Pscala-2.13; affected suites
(Framed, BuildFilter, StatsBlob, ShipBlockerMarshal,
IntFamilyMarshal) all green (23/23). CI 60/60 green on
da460551e7.


Was this patch authored or co-authored using generative AI tooling?
No

@github-actions github-actions Bot added the VELOX label May 26, 2026
…ColumnarBatch

ColumnarCachedBatchSerializer.read guarded the trailing hasStats /
hasSchema booleans with `input.available() > 0` to tolerate the V1
wire format that predates those markers. The intent was correct --
the existing ColumnarCachedBatchKryoSuite#"V1 wire ..." test locks
absent-trailing as silent null, and that contract must be preserved
-- but `Input.available()` is the wrong probe.

`Kryo Input.available()` returns
`(limit - position) + underlyingStream.available()`, and the JDK
`InputStream.available()` contract permits any implementation to
return 0 even when more data follows -- BufferedInputStream over
shuffle-spill / network chunk boundaries routinely does so. When the
Kryo buffer is drained AND the underlying stream reports 0 at the
trailing-boolean byte position, the probe falsely concludes EOF,
skips hasStats, and the next `readClassAndObject` interprets the
stats payload (which contains the schema JSON) as a class name --
surfacing as `ClassNotFoundException: {"type":"struct",...}` with
the stack topped by `DefaultClassResolver.readName`.

Replace the probe with a try/readBoolean/catch on the narrow Kryo
"Buffer underflow" surface. This catches the real EOF when the V1
wire has no trailing booleans (preserves the silent-null contract)
without ever consulting `available()`, so a V2 wire under
chunked-fill always reads the trailing markers correctly.

The catch is intentionally narrow (message-prefix match on
"Buffer underflow") so that genuine corruption -- including
ClassNotFoundException wrapped during readClassAndObject -- is
never swallowed.

The length-bound `require(... <= maxLen ...)` guard from commit
491070b (defending against NegativeArraySizeException /
oversized allocation) is preserved -- that part is orthogonal to
the V1 probe and remains useful.

A new test ColumnarCachedBatchKryoBoundaryProbeBugSuite locks the
chunked-fill probe contract: a 1-byte-per-read InputStream that
returns `available() == 0` must still round-trip multi-batch V2
wire correctly. The existing V1-wire silent-null test in
ColumnarCachedBatchKryoSuite continues to pass unchanged.
@yaooqinn yaooqinn force-pushed the users/kentyao/gluten-ccbs-available-probe-bug branch from 8c36260 to da46055 Compare May 27, 2026 03:59
liuneng1994

This comment was marked as duplicate.

Copy link
Copy Markdown
Contributor

@liuneng1994 liuneng1994 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description contradicts the code

Cross-referencing the existing locked test in ColumnarCachedBatchKryoSuite.scala:

test("V1 wire (no trailing hasStats/hasSchema booleans) reads as stats=null/schema=null") {
  ...
  assert(read.stats === null, "absent trailing hasStats must read as null, not throw")
  assert(read.schema === null, "absent trailing hasSchema must read as null, not throw")
}

This contract requires V1 compat to be preserved — and the new try/catch (KryoException if isBufferUnderflow) => false is exactly what satisfies it. But the PR description says the opposite:

description claims code / tests actually
"V1 wire era … never released … guard protects nothing" try/catch preserves V1 compat; locked by existing "V1 wire …" test
"A truncated stream now surfaces KryoException rather than silently returning null" truncated stream is caught and returns null stats — same silent behavior
Cites BoundaryProbeBugSuite#"truncated wire … fails fast, not silently" new file only contains 1 test ("multi-batch deserialize survives …") — the cited test doesn't exist

Suggested fix: keep the code as-is (preserving V1 compat is correct), and trim the description down to honestly what the patch does — "fix the false-EOF probe in Input.available() so the trailing booleans are read across chunk boundaries; V1 compat is now achieved via try/catch on KryoException instead". Drop the "Spark cached blocks live and die within a single application instance" paragraph and the cite to a non-existent test.

@yaooqinn yaooqinn changed the title [VL] Remove phantom V1 wire compat guard in ColumnarCachedBatchSerializer [VL] Fix false-EOF probe in ColumnarCachedBatchSerializer.read trailing markers May 28, 2026
@yaooqinn yaooqinn merged commit 7bfd45c into apache:main May 28, 2026
109 of 110 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants