Skip to content

Add flush page-data accounting and value buffer capacity - #1189

Open
jt2594838 wants to merge 1 commit into
developfrom
codex/data-only-compression-ratio
Open

jt2594838 wants to merge 1 commit into
developfrom
codex/data-only-compression-ratio

Conversation

@jt2594838

Copy link
Copy Markdown
Contributor

Summary

  • Add an optional initial value-buffer capacity to ValuePageWriter and pass it through the value and aligned chunk writers.
  • Track compressed page-body bytes produced by chunk writers during flush, excluding page headers and page statistics.
  • Restore the byte counter when TsFileIOWriter.reset() truncates bytes after a failed write.

Accounting scope

The counter is intended for flush-encoded pages. Chunks copied through writeChunk are not included. The one-argument writeBytesToStream(PublicBAOS) overload counts the entire packed page buffer, including headers and statistics, so its contribution may overstate page-body bytes. Flush chunk writers use the overload that receives the explicit page-body byte count.

For example, if a packed page buffer contains 3,000 compressed page-body bytes and 20 bytes of page headers and statistics, passing 3,000 records only the page body. The one-argument overload records all 3,020 bytes. A ValuePageWriter constructed with an initial capacity of 4,096 starts its encoded-value buffer at that size.

Validation

  • mvn verify -P with-java
  • Focused page-writer tests: 19 passed

TSDataType dataType,
EncryptParameter encryptParam,
int valueOutInitialCapacity) {
this(valueEncoder, compressor, dataType, encryptParam, new PublicBAOS(valueOutInitialCapacity));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Creating PublicBAOS with this capacity sizes the encoded-value buffer before its first encoder write, allowing flush estimates to avoid repeated growth in this stream. The encryption-aware overload reaches the same allocation point while using its supplied EncryptParameter.

schemaList.get(i).getValueEncoder(),
this.encryptParam));
this.encryptParam,
valueOutInitialCapacity));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Each aligned measurement has its own ValueChunkWriter and encoded-value buffer. Forwarding the capacity here applies the estimate to every aligned value column, not only standalone value-page construction.

PageBodyEncoder.encode(
pageData, uncompressedSize, compressor, encryptParam, pageIndex, chunkOrdinal);
int pageBodySize = pageBody.remaining();
lastPageDataSize = pageBodySize;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

PageBodyEncoder has already produced the bytes appended after the page header. Recording this length excludes page-header varints and statistics from non-aligned page-data accounting.

PageBodyEncoder.encode(
pageData, uncompressedSize, compressor, encryptParam, pageIndex, chunkOrdinal);
int pageBodySize = pageBody.remaining();
lastPageDataSize = pageBodySize;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Aligned time pages also contribute compressed page-body bytes to the flush ratio. Capturing the encoded body length here keeps their page headers out of the count.

TSDataType dataType,
EncryptParameter encryptParam,
int valueOutInitialCapacity) {
this(valueEncoder, compressor, dataType, encryptParam, new PublicBAOS(valueOutInitialCapacity));

@jt2594838 jt2594838 Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Routing constructor variants through the shared initializer keeps statistics, compressor, and chunk-ordinal setup consistent. The capacity-aware overload supplies a pre-sized PublicBAOS through that same initialization path.

PageBodyEncoder.encode(
pageData, uncompressedSize, compressor, encryptParam, pageIndex, chunkOrdinal);
int pageBodySize = pageBody.remaining();
lastPageDataSize = pageBodySize;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This records the compressed or encrypted page body after PageBodyEncoder. The page header and statistics are serialized separately, so this length is the data-only portion used by flush accounting.

} else {
pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages);
}
pageDataSize += pageWriter.getLastPageDataSize();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

A chunk buffer can contain multiple pages. Accumulating each encoded body here gives the explicit writer overload the exact body-byte total when the packed chunk is flushed.

} else {
pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages);
}
pageDataSize += pageWriter.getLastPageDataSize();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This parallel accumulator covers the time-column pages of aligned flushes. It keeps time body bytes in the ratio while their page headers remain excluded.

} else {
pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages);
}
pageDataSize += pageWriter.getLastPageDataSize();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This accumulator covers value-column pages and is reset with the chunk buffer after emission. Together with the time-column counter it represents aligned flush page bodies.

* and page statistics
* @throws IOException if an I/O error occurs.
*/
public void writeBytesToStream(PublicBAOS bytes, long pageDataBytes) throws IOException {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Flush chunk writers already know the page-body total while encoding, so supplying it here avoids reparsing the packed buffer on the write path and keeps page framing out of the exact count.

* writeBytesToStream} overload counts the complete packed buffer, including page headers and page
* statistics, so its contribution is approximate.
*/
public long getWrittenPageDataSize() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This counter is scoped to pages produced by flush chunk writers. Raw chunks copied with writeChunk have no paired MemTable-size contribution, and the legacy one-argument overload can include page framing, so callers can interpret the ratio correctly.


public void reset() throws IOException {
out.truncate(markedPosition);
writtenPageDataSize = markedPageDataSize;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

A failed flush retry truncates the output back to the saved position. Restoring the matching counter checkpoint prevents discarded page bodies from being counted again after the retry.

// A configured value-buffer capacity must reach the aligned column encoder before its first
// write.
@Test
public void testValuePageBufferInitialCapacity() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This test inspects the value encoder buffer before its first write, verifying that AlignedChunkWriterImpl forwards the configured capacity through its per-measurement writer construction.


// The requested initial value-buffer capacity is used while encoding and the page stays readable.
@Test
public void testValueOutInitialCapacity() throws IOException {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The encoder assertion checks the requested allocation, while decoding the resulting page verifies that preallocation preserves the existing serialized value layout.


// An explicit page-data byte count excludes page framing and chunk-group headers.
@Test
public void testWriterCountsSerializedPageDataBytes() throws IOException {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These assertions validate the explicit flush count and confirm that the first page-header fields are excluded from the normal page-body total.


// A chunk copied through writeChunk is excluded from the flush page-data counter.
@Test
public void testWriterDoesNotCountCopiedChunks() throws IOException {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copied chunks are outside the flush ratio because this path has no corresponding MemTable byte count. This test makes that accounting boundary explicit.


// Resetting after a failed write truncates its bytes and removes them from the page-data count.
@Test
public void testWriterResetRestoresPageDataCount() throws IOException {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

A retry truncates bytes after mark. This test verifies the page-data counter is rolled back with the file position, avoiding double counting.

this.pageWriter =
new ValuePageWriter(
valueEncoder, ICompressor.getCompressor(compressionType), dataType, this.encryptParam);
valueOutInitialCapacity > 0

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

A positive flush estimate is supplied to the capacity-aware ValuePageWriter constructor. Zero selects the default allocation path when no historical estimate is available.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.53%. Comparing base (42168ea) to head (d0e29ac).

Files with missing lines Patch % Lines
...org/apache/tsfile/write/writer/TsFileIOWriter.java 66.66% 2 Missing ⚠️
...org/apache/tsfile/write/chunk/TimeChunkWriter.java 75.00% 1 Missing ⚠️
...rg/apache/tsfile/write/chunk/ValueChunkWriter.java 92.85% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1189      +/-   ##
===========================================
+ Coverage    69.48%   69.53%   +0.04%     
===========================================
  Files          798      798              
  Lines        59138    59155      +17     
  Branches      9642     9646       +4     
===========================================
+ Hits         41092    41131      +39     
+ Misses       16256    16235      -21     
+ Partials      1790     1789       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

3 participants