Repository navigation
Add flush page-data accounting and value buffer capacity #1189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,6 +62,8 @@ public class ChunkWriterImpl implements IChunkWriter { | |
|
|
||
| private int numOfPages; | ||
|
|
||
| private long pageDataSize; | ||
|
|
||
| /** write data into current page */ | ||
| private PageWriter pageWriter; | ||
|
|
||
|
|
@@ -346,6 +348,7 @@ private void writePageToPageBuffer() { | |
| } else { | ||
| pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages); | ||
| } | ||
| pageDataSize += pageWriter.getLastPageDataSize(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| // update statistics of this chunk | ||
| numOfPages++; | ||
|
|
@@ -372,6 +375,7 @@ public void writeToFileWriter( | |
|
|
||
| // reinit this chunk writer | ||
| pageBuffer.reset(); | ||
| pageDataSize = 0; | ||
| numOfPages = 0; | ||
| sizeWithoutStatistic = 0; | ||
| firstPageStatistics = null; | ||
|
|
@@ -493,6 +497,7 @@ public void writePageHeaderAndDataIntoBuff(ByteBuffer data, PageHeader header) | |
| } | ||
| numOfPages++; | ||
| // write page content to temp PBAOS | ||
| pageDataSize += data.remaining(); | ||
| try (WritableByteChannel channel = Channels.newChannel(pageBuffer)) { | ||
| channel.write(data); | ||
| } catch (IOException e) { | ||
|
|
@@ -535,7 +540,7 @@ private void writeAllPagesOfChunkToTsFile( | |
| long dataOffset = writer.getPos(); | ||
|
|
||
| // write all pages of this column | ||
| writer.writeBytesToStream(pageBuffer); | ||
| writer.writeBytesToStream(pageBuffer, pageDataSize); | ||
|
|
||
| int dataSize = (int) (writer.getPos() - dataOffset); | ||
| if (dataSize != pageBuffer.size()) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,8 @@ public class TimeChunkWriter { | |
|
|
||
| private int numOfPages; | ||
|
|
||
| private long pageDataSize; | ||
|
|
||
| /** write data into current page */ | ||
| private TimePageWriter pageWriter; | ||
|
|
||
|
|
@@ -208,6 +210,7 @@ public void writePageToPageBuffer() { | |
| } else { | ||
| pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages); | ||
| } | ||
| pageDataSize += pageWriter.getLastPageDataSize(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| // update statistics of this chunk | ||
| numOfPages++; | ||
|
|
@@ -261,6 +264,7 @@ public void writePageHeaderAndDataIntoBuff(ByteBuffer data, PageHeader header) | |
| } | ||
| numOfPages++; | ||
| // write page content to temp PBAOS | ||
| pageDataSize += data.remaining(); | ||
| try (WritableByteChannel channel = Channels.newChannel(pageBuffer)) { | ||
| channel.write(data); | ||
| } catch (IOException e) { | ||
|
|
@@ -274,6 +278,7 @@ public void writeToFileWriter(TsFileIOWriter tsfileWriter) throws IOException { | |
|
|
||
| // reinit this chunk writer | ||
| pageBuffer.reset(); | ||
| pageDataSize = 0; | ||
| numOfPages = 0; | ||
| sizeWithoutStatistic = 0; | ||
| firstPageStatistics = null; | ||
|
|
@@ -352,7 +357,7 @@ public void writeAllPagesOfChunkToTsFile(TsFileIOWriter writer) throws IOExcepti | |
| long dataOffset = writer.getPos(); | ||
|
|
||
| // write all pages of this column | ||
| writer.writeBytesToStream(pageBuffer); | ||
| writer.writeBytesToStream(pageBuffer, pageDataSize); | ||
|
|
||
| int dataSize = (int) (writer.getPos() - dataOffset); | ||
| if (dataSize != pageBuffer.size()) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,8 @@ public class ValueChunkWriter { | |
|
|
||
| private int numOfPages; | ||
|
|
||
| private long pageDataSize; | ||
|
|
||
| /** write data into current page */ | ||
| private ValuePageWriter pageWriter; | ||
|
|
||
|
|
@@ -96,25 +98,14 @@ public ValueChunkWriter( | |
| TSDataType dataType, | ||
| TSEncoding encodingType, | ||
| Encoder valueEncoder) { | ||
| this.measurementId = measurementId; | ||
| this.encodingType = encodingType; | ||
| this.dataType = dataType; | ||
| this.compressionType = compressionType; | ||
| this.encryptParam = EncryptUtils.getEncryptParameter(); | ||
| this.chunkOrdinal = nextChunkOrdinal(); | ||
| this.pageBuffer = new PublicBAOS(); | ||
| this.pageSizeThreshold = TSFileDescriptor.getInstance().getConfig().getPageSizeInByte(); | ||
| this.maxNumberOfPointsInPage = | ||
| TSFileDescriptor.getInstance().getConfig().getMaxNumberOfPointsInPage(); | ||
| this.valueCountInOnePageForNextCheck = MINIMUM_RECORD_COUNT_FOR_CHECK; | ||
|
|
||
| // init statistics for this chunk and page | ||
| this.statistics = Statistics.getStatsByType(dataType); | ||
|
|
||
| this.pageWriter = | ||
| new ValuePageWriter( | ||
| valueEncoder, ICompressor.getCompressor(compressionType), dataType, this.encryptParam); | ||
| this.pageWriter.setChunkOrdinal(chunkOrdinal); | ||
| this( | ||
| measurementId, | ||
| compressionType, | ||
| dataType, | ||
| encodingType, | ||
| valueEncoder, | ||
| EncryptUtils.getEncryptParameter(), | ||
| 0); | ||
| } | ||
|
|
||
| public ValueChunkWriter( | ||
|
|
@@ -124,6 +115,17 @@ public ValueChunkWriter( | |
| TSEncoding encodingType, | ||
| Encoder valueEncoder, | ||
| EncryptParameter encryptParam) { | ||
| this(measurementId, compressionType, dataType, encodingType, valueEncoder, encryptParam, 0); | ||
| } | ||
|
|
||
| public ValueChunkWriter( | ||
| String measurementId, | ||
| CompressionType compressionType, | ||
| TSDataType dataType, | ||
| TSEncoding encodingType, | ||
| Encoder valueEncoder, | ||
| EncryptParameter encryptParam, | ||
| int valueOutInitialCapacity) { | ||
| this.measurementId = measurementId; | ||
| this.encodingType = encodingType; | ||
| this.dataType = dataType; | ||
|
|
@@ -140,8 +142,18 @@ public ValueChunkWriter( | |
| this.statistics = Statistics.getStatsByType(dataType); | ||
|
|
||
| this.pageWriter = | ||
| new ValuePageWriter( | ||
| valueEncoder, ICompressor.getCompressor(compressionType), dataType, this.encryptParam); | ||
| valueOutInitialCapacity > 0 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| ? new ValuePageWriter( | ||
| valueEncoder, | ||
| ICompressor.getCompressor(compressionType), | ||
| dataType, | ||
| this.encryptParam, | ||
| valueOutInitialCapacity) | ||
| : new ValuePageWriter( | ||
| valueEncoder, | ||
| ICompressor.getCompressor(compressionType), | ||
| dataType, | ||
| this.encryptParam); | ||
| this.pageWriter.setChunkOrdinal(chunkOrdinal); | ||
| } | ||
|
|
||
|
|
@@ -240,6 +252,7 @@ public void writePageToPageBuffer() { | |
| } else { | ||
| pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages); | ||
| } | ||
| pageDataSize += pageWriter.getLastPageDataSize(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| // update statistics of this chunk | ||
| numOfPages++; | ||
|
|
@@ -307,6 +320,7 @@ public void writePageHeaderAndDataIntoBuff(ByteBuffer data, PageHeader header) | |
| } | ||
| numOfPages++; | ||
| // write page content to temp PBAOS | ||
| pageDataSize += data.remaining(); | ||
| try (WritableByteChannel channel = Channels.newChannel(pageBuffer)) { | ||
| channel.write(data); | ||
| } catch (IOException e) { | ||
|
|
@@ -326,6 +340,7 @@ public void writeToFileWriter( | |
|
|
||
| // reinit this chunk writer | ||
| pageBuffer.reset(); | ||
| pageDataSize = 0; | ||
| numOfPages = 0; | ||
| sizeWithoutStatistic = 0; | ||
| firstPageStatistics = null; | ||
|
|
@@ -472,7 +487,7 @@ public void writeAllPagesOfChunkToTsFile( | |
| long dataOffset = writer.getPos(); | ||
|
|
||
| // write all pages of this column | ||
| writer.writeBytesToStream(pageBuffer); | ||
| writer.writeBytesToStream(pageBuffer, pageDataSize); | ||
|
|
||
| int dataSize = (int) (writer.getPos() - dataOffset); | ||
| if (dataSize != pageBuffer.size()) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,6 +62,8 @@ public class PageWriter { | |
| */ | ||
| private Statistics<? extends Serializable> statistics; | ||
|
|
||
| private int lastPageDataSize; | ||
|
|
||
| public PageWriter() { | ||
| this((Encoder) null, null); | ||
| } | ||
|
|
@@ -233,6 +235,7 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first) | |
| /** write the page header and data into the PageWriter's output stream. */ | ||
| public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, int pageIndex) | ||
| throws IOException { | ||
| lastPageDataSize = 0; | ||
| if (statistics.getCount() == 0) { | ||
| return 0; | ||
| } | ||
|
|
@@ -243,6 +246,7 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, | |
| PageBodyEncoder.encode( | ||
| pageData, uncompressedSize, compressor, encryptParam, pageIndex, chunkOrdinal); | ||
| int pageBodySize = pageBody.remaining(); | ||
| lastPageDataSize = pageBodySize; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| // write the page header to IOWriter | ||
| int sizeWithoutStatistic = 0; | ||
|
|
@@ -264,6 +268,10 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, | |
| return sizeWithoutStatistic; | ||
| } | ||
|
|
||
| public int getLastPageDataSize() { | ||
| return lastPageDataSize; | ||
| } | ||
|
|
||
| /** | ||
| * calculate max possible memory size it occupies, including time outputStream and value | ||
| * outputStream, because size outputStream is never used until flushing. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,8 @@ public class TimePageWriter { | |
| */ | ||
| private TimeStatistics statistics; | ||
|
|
||
| private int lastPageDataSize; | ||
|
|
||
| public TimePageWriter(Encoder timeEncoder, ICompressor compressor) { | ||
| this.timeOut = new PublicBAOS(); | ||
| this.timeEncoder = timeEncoder; | ||
|
|
@@ -116,6 +118,7 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first) | |
| /** write the page header and data into the PageWriter's output stream. */ | ||
| public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, int pageIndex) | ||
| throws IOException { | ||
| lastPageDataSize = 0; | ||
| if (statistics.getCount() == 0) { | ||
| return 0; | ||
| } | ||
|
|
@@ -126,6 +129,7 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, | |
| PageBodyEncoder.encode( | ||
| pageData, uncompressedSize, compressor, encryptParam, pageIndex, chunkOrdinal); | ||
| int pageBodySize = pageBody.remaining(); | ||
| lastPageDataSize = pageBodySize; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| // write the page header to IOWriter | ||
| int sizeWithoutStatistic = 0; | ||
|
|
@@ -149,6 +153,10 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, | |
| return sizeWithoutStatistic; | ||
| } | ||
|
|
||
| public int getLastPageDataSize() { | ||
| return lastPageDataSize; | ||
| } | ||
|
|
||
| /** | ||
| * calculate max possible memory size it occupies, including time outputStream and value | ||
| * outputStream, because size outputStream is never used until flushing. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,29 +62,57 @@ public class ValuePageWriter { | |
|
|
||
| private int size; | ||
|
|
||
| private int lastPageDataSize; | ||
|
|
||
| private final PublicBAOS bitmapOut; | ||
|
|
||
| private static final int MASK = 1 << 7; | ||
|
|
||
| private static final byte[] NULL_BITMAP_BATCH = new byte[1024]; | ||
|
|
||
| public ValuePageWriter(Encoder valueEncoder, ICompressor compressor, TSDataType dataType) { | ||
| this.valueOut = new PublicBAOS(); | ||
| this.bitmap = 0; | ||
| this.size = 0; | ||
| this.bitmapOut = new PublicBAOS(); | ||
| this.valueEncoder = valueEncoder; | ||
| this.statistics = Statistics.getStatsByType(dataType); | ||
| this.compressor = compressor; | ||
| this.encryptParam = EncryptUtils.getEncryptParameter(); | ||
| this(valueEncoder, compressor, dataType, EncryptUtils.getEncryptParameter()); | ||
| } | ||
|
|
||
| public ValuePageWriter( | ||
| Encoder valueEncoder, | ||
| ICompressor compressor, | ||
| TSDataType dataType, | ||
| EncryptParameter encryptParam) { | ||
| this.valueOut = new PublicBAOS(); | ||
| this(valueEncoder, compressor, dataType, encryptParam, new PublicBAOS()); | ||
| } | ||
|
|
||
| /** Creates a page writer with an initial capacity in bytes for the encoded values buffer. */ | ||
| public ValuePageWriter( | ||
| Encoder valueEncoder, | ||
| ICompressor compressor, | ||
| TSDataType dataType, | ||
| int valueOutInitialCapacity) { | ||
| this( | ||
| valueEncoder, | ||
| compressor, | ||
| dataType, | ||
| EncryptUtils.getEncryptParameter(), | ||
| valueOutInitialCapacity); | ||
| } | ||
|
|
||
| /** Creates a page writer with explicit encryption and an initial encoded values capacity. */ | ||
| public ValuePageWriter( | ||
| Encoder valueEncoder, | ||
| ICompressor compressor, | ||
| TSDataType dataType, | ||
| EncryptParameter encryptParam, | ||
| int valueOutInitialCapacity) { | ||
| this(valueEncoder, compressor, dataType, encryptParam, new PublicBAOS(valueOutInitialCapacity)); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| } | ||
|
|
||
| private ValuePageWriter( | ||
| Encoder valueEncoder, | ||
| ICompressor compressor, | ||
| TSDataType dataType, | ||
| EncryptParameter encryptParam, | ||
| PublicBAOS valueOut) { | ||
| this.valueOut = valueOut; | ||
| this.bitmap = 0; | ||
| this.size = 0; | ||
| this.bitmapOut = new PublicBAOS(); | ||
|
|
@@ -407,6 +435,7 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first) | |
| /** write the page header and data into the PageWriter's output stream. */ | ||
| public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, int pageIndex) | ||
| throws IOException { | ||
| lastPageDataSize = 0; | ||
| if (size == 0) { | ||
| return 0; | ||
| } else if (statistics.getCount() == 0) { | ||
|
|
@@ -420,6 +449,7 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, | |
| PageBodyEncoder.encode( | ||
| pageData, uncompressedSize, compressor, encryptParam, pageIndex, chunkOrdinal); | ||
| int pageBodySize = pageBody.remaining(); | ||
| lastPageDataSize = pageBodySize; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| // write the page header to IOWriter | ||
| int sizeWithoutStatistic = 0; | ||
|
|
@@ -440,6 +470,10 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, | |
| return sizeWithoutStatistic; | ||
| } | ||
|
|
||
| public int getLastPageDataSize() { | ||
| return lastPageDataSize; | ||
| } | ||
|
|
||
| /** | ||
| * calculate max possible memory size it occupies, including time outputStream and value | ||
| * outputStream, because size outputStream is never used until flushing. | ||
|
|
||
There was a problem hiding this comment.
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.