GH-4043: fix(dboe): don't poison shared buffer on block slice#4051
Open
m3l0n-sq wants to merge 1 commit into
Open
GH-4043: fix(dboe): don't poison shared buffer on block slice#4051m3l0n-sq wants to merge 1 commit into
m3l0n-sq wants to merge 1 commit into
Conversation
BlockAccessMapped.getByteBuffer sliced a block by shrinking the shared per-segment MappedByteBuffer's limit and restoring it afterwards, but the restore was not exception-safe. A throwable escaping between the shrink and the restore (e.g. OOME at slice allocation) left the segment buffer with a shrunken limit, so every later access to a higher block in that segment failed with "newPosition > limit" until JVM restart. Slice from a cleared duplicate instead, so block access neither mutates nor depends on the shared buffer's position/limit. The same fix is applied to the duplicated TDB1 BlockAccessMapped. Widen the diagnostic catch to RuntimeException since the absolute slice reports range violations as IndexOutOfBoundsException. Add TestBlockAccessMappedSegmentState, which injects the poisoned state and verifies higher blocks still read back correctly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GitHub issue resolved #4043
Pull request Description:
Problem
BlockAccessMapped.getByteBufferslices a block out of the shared per-segmentMappedByteBufferby mutating that buffer's state:The restore is not exception-safe. If any throwable escapes between the limit-shrink and the restore — realistically an
OutOfMemoryErrorraised at theslice()allocation under heap pressure — the segment buffer is left with a shrunken limit. From then on, every access to a higher block in that segment fails:The failure is self-sustaining: the poisoned
position(segOff)call throws before reaching the line that would restore the limit. (An access to a block below the stuck limit happens to heal the buffer, but a hot B+Tree node above it keeps failing indefinitely.)Observed in production as a TDB2 dataset returning HTTP 500 for every query touching one B+Tree index, persisting until JVM restart. The reported limit was not segment-aligned (
2588672 = 315 * 8192 + 8192) — exactly a leftover slice limit, not a file-length artifact. This failure presents exactly like on-disk index corruption, but nothing on disk is wrong — which matters, because the standard advice for this signature (dump/reload the database) is unnecessary here; a restart clears it.The existing in-code comment anticipated the hazard:
// Shouldn't (ha!) happen because the second "limit" resetsFix
Slice through a cleared duplicate, using the absolute
slice(index, length):This neither mutates nor depends on the shared buffer's position/limit, so the poisoned state can no longer be created — and reads keep working even if the buffer state were corrupted by other means.
Why not plain
slice(index, length)on the shared buffer? Becauseslice(index, length)validates against the buffer's current limit, not its capacity — it would prevent creating the poison but still fail on an already-poisoned buffer. The cleared duplicate makes the access path fully state-independent.Semantics of the returned slice are unchanged: position 0, limit and capacity =
blockSize, big-endian byte order, view of the same backing region. Cost is one extra view object per block access (the previous code already allocated one view per access).The
catch (IllegalArgumentException ...)diagnostic block is widened toRuntimeException, since the absolute slice reports range violations asIndexOutOfBoundsException.The identical code exists in TDB1 (
org.apache.jena.tdb1.base.file.BlockAccessMapped) and is fixed the same way.Files changed
jena-db/jena-dboe-base/src/main/java/org/apache/jena/dboe/base/file/BlockAccessMapped.javajena-tdb1/src/main/java/org/apache/jena/tdb1/base/file/BlockAccessMapped.javajena-db/jena-dboe-base/src/test/java/org/apache/jena/dboe/base/file/TestBlockAccessMappedSegmentState.java(new)jena-db/jena-dboe-base/src/test/java/org/apache/jena/dboe/base/file/TS_File.java(register new test class)Testing
The trigger (an asynchronous throwable inside a 3-line window) cannot be provoked deterministically in a test, so
TestBlockAccessMappedSegmentStateinjects the state it leaves behind and verifies behavior at that boundary:poisonedSegmentLimit_readsStillWork— reflectively shrinks the shared segment buffer's limit (the state an escaped throwable left behind), then reads all higher blocks. Against the previous implementation this reproduces the production exception verbatim:IllegalArgumentException: newPosition > limit: (448 > 64). With the fix, all blocks read back correctly.sharedSegmentBufferStateUntouchedByAccess— locks in the invariant that block access never mutates the shared buffer's position/limit. Fails against the previous implementation (which leftpositionat the last-accessed segment offset).returnedSlicesAreIndependent— returned slices do not interfere with each other or with the shared buffer.blockRoundTripAfterMixedAccess— content round-trip through the new slice path.Red/green verified: with the fix reverted, the new tests fail with the exact production signature; with the fix applied, they pass.
Full test suites pass with the change:
Compatibility notes
ByteBuffer.slice(int, int)requires JDK 13+; fine for the current Java baseline (21). A backport to older branches would need((ByteBuffer)segBuffer.duplicate().clear()).position(segOff).limit(segOff+blockSize).slice()on a duplicate instead.getByteBufferis private and the returned slice is indistinguishable from before.By submitting this pull request, I acknowledge that I am making a contribution to the Apache Software Foundation under the terms and conditions of the Contributor's Agreement.
See the Apache Jena "Contributing" guide.