Skip to content

fix(cpp): read chunk headers with long column names - #1163

Closed
ColinLeeo wants to merge 3 commits into
apache:developfrom
ColinLeeo:fix/tsfile-213
Closed

ColinLeeo wants to merge 3 commits into
apache:developfrom
ColinLeeo:fix/tsfile-213

Conversation

@ColinLeeo

@ColinLeeo ColinLeeo commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A table-model column name of 1018 or more characters made the write succeed but the read return E_PARTIAL_READ, i.e. a file that is silently unreadable.

Root cause

The chunk header embeds the measurement name. The reader parsed chunk headers through a fixed 1024-byte window. For a header of chunk_type(1) + varint len(2) + name + data_size varint(1) + 3 bytes, a name of 1017 bytes fits in 1024, while 1018 bytes needs 1025 — read_var_str then returned E_PARTIAL_READ because the chunk-header path had no grow-and-retry window (chunk/page header reads did).

Changes

  • Add a bounded (1 MiB cap) doubling read-window helper for chunk headers and wire it into ChunkReader, the aligned single-value/multi-value chunk readers, and the restorable recovery scan; keeps E_PARTIAL_READ semantics for truncated data and the same cursor/offset behavior.
  • Recovery: keep the recovered device id on OOM and fail recovery instead of treating E_OOM as a corrupt tail and truncating a valid file.
  • Regression tests: long FIELD/TAG names at 1017/1018/1024/4096, corrupted length varints, EOF/partial paths, OOM injection, plus value-level assertions for the multi-column table path.

Verification

  • Debug full C++ gtest: 1917 passed / 0 failed (2 compat cases need the repository cwd).
  • Pre-fix differential: 1018/1024/4096 reproduce the failure, 1017 passes — the threshold matches the analysis.
  • clang-format 17.0.6 clean; independent LSan rerun reports 0 leaks.

Review

Self-reviewed, then an independent read-only review; the delta review concluded "mergeable". The branch contains the reader fix, a review-hardening follow-up (bounded window, OOM recovery) and an OOM cleanup follow-up.

A serialized chunk header embeds the measurement/column name verbatim, so a
column/measurement name of about 1018 characters or more makes the header
longer than the fixed 1024-byte window used by ChunkReader::load_by_meta(),
AlignedChunkReader::load_by_aligned_meta() and
AlignedChunkReader::load_by_aligned_meta_multi(). Reading the header then hit
read_var_str()'s "length beyond the stream" check, which surfaces
E_PARTIAL_READ: the write succeeded but the first read of that column failed.

Add reader/read_chunk_header.h: it reads and deserializes a chunk header at an
offset, doubling the read window while deserialization reports that more bytes
are needed, capped at the end of the file. Use it in both chunk readers and in
the recovery self-check (parse_chunk_header_and_skip()) as well, which
previously mistook such a chunk for a corrupt tail and truncated the recovered
file at it. Callers wrap the returned buffer and advance their stream past
header.serialized_size_ to the first page header.

No on-disk format or writer change: names that could already be written are now
readable again, matching Java, which has no name length limit.

Tests: new LongMeasurementNameTest writes and reads back 1017/1018/1024/4096
character names for table FIELD columns, table TAG columns (schema lookup and
tag filter) and tree measurements; a new restorable-writer case recovers and
appends after a 4096-character name. The FIELD, tree and recovery cases fail
below 1018/1024/4096 characters without this fix.

CLI: no change applicable. The CLI reads through this SDK, and its
sketch-layout chunk-header probe already grows its window (up to 1 MiB), so its
behavior follows the SDK fix without CLI-side changes.
…TsFile-213)

Follow-up to 84cddbc addressing third-party review findings.

The chunk-header read window added in 84cddbc grew up to the remaining file
size, so a corrupt varint name length could drive a multi-MiB read and
allocation. Cap the window at kMaxChunkHeaderWindow (1 MiB, matching the CLI
sketch-layout probe limit); a header that does not fit is reported as
truncated instead of being read into an unbounded buffer. New
ReadChunkHeaderTest cases cover a valid header that needs growth, a corrupt
2 MiB length failing at exactly the cap, and the offset-at-EOF contract.

The recovery self-check now fails on E_OOM instead of treating it like a
corrupt tail: previously an allocation failure during the scan could truncate
a valid file at the last good chunk. The new
RestorableTsFileIOWriterTest.OomDuringSelfCheckDoesNotTruncate injects the
first realloc failure on the scan path and checks that open() returns E_OOM and
that the file size is unchanged.

Test corrections: LongMeasurementNameTest now also asserts a second, short
FIELD column next to the long-named one. Its TAG-column case is documented as
coverage only: in the C++ table model tag values come from the device id
rather than a value chunk, so that case passes without the reader fix and is
not evidence of the chunk-header bug.

Not addressed here: the recovery self-check reads chunk group headers (device
id / tag values) with a fixed 4096-byte window. That is a pre-existing
limitation of the same family for very long device ids and is left for a
separate change.

Tests: full C++ gtest suite 1917 passed (2 EncodingCompressionCompatibility
cases need cwd at the repository root for compat/compatibility-matrix.csv and
pass there); clang-format 17.0.6 clean.
…sFile-213)

Follow-up to 7e29b7c addressing the review finding on the OOM path.

self_check() now returns on E_OOM before flush_chunk_group(), so the
in-progress ChunkGroupMeta stays in the arena. Arena destruction does not run
destructors, and close() only clears the device ids of fully built groups in
self_check_recovered_cgm_, so the std::shared_ptr<IDeviceID> held by
cur_cgm->device_id_ leaked (LeakSanitizer reported 136 bytes for
OomDuringSelfCheckDoesNotTruncate before this change). Reset that device id
before returning; the file is still left untouched.

Known limitation recorded for follow-up: the 1 MiB chunk-header window cap
means a header (and therefore a measurement name) larger than 1 MiB can still
be written but not read or recovered; enforcing the limit on the write side or
documenting it in the skills reference is left for a separate change.

Tests: targeted suites 26 passed; AddressSanitizer + LeakSanitizer build
(RelWithDebInfo, address,undefined) runs OomDuringSelfCheckDoesNotTruncate and
RecoverChunkWithLongMeasurementNameAndAppend without leaks; clang-format 17.0.6
clean.
@ColinLeeo ColinLeeo changed the title fix(cpp): read chunk headers with long column names (TsFile-213) fix(cpp): read chunk headers with long column names Dec 1, 2026
@ColinLeeo ColinLeeo closed this Dec 1, 2026
@ColinLeeo
ColinLeeo deleted the fix/tsfile-213 branch December 1, 2026 02:50
@ColinLeeo

Copy link
Copy Markdown
Contributor Author

Superseded by #1167 after the branch was renamed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant