diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DictionaryEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DictionaryEncoder.java index b1fc56ffd..d1c69c270 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DictionaryEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DictionaryEncoder.java @@ -62,14 +62,16 @@ public DictionaryEncoder() { @Override public void encode(Binary value, ByteArrayOutputStream out) { - int i = - entryIndex.computeIfAbsent( - value, - v -> { - indexEntry.add(v); - mapSize += v.getLength(); - return entryIndex.size(); - }); + // Avoid computeIfAbsent: its capturing lambda is allocated on every call, which shows up as + // one of the largest allocation sites when encoding a TEXT column. + Integer i = entryIndex.get(value); + if (i == null) { + indexEntry.add(value); + mapSize += value.getLength(); + // The index must be the size before inserting, matching the original 0-based indexes. + i = entryIndex.size(); + entryIndex.put(value, i); + } valuesEncoder.encode(i, out); } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DoublePrecisionChimpEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DoublePrecisionChimpEncoder.java index 843d17a39..7c46829c3 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DoublePrecisionChimpEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DoublePrecisionChimpEncoder.java @@ -46,8 +46,7 @@ public void flush(ByteArrayOutputStream out) { // flip the byte no matter it is empty or not // the empty ending byte is necessary when decoding - bitsLeft = 0; - flipByte(out); + flushRemainingBits(out); // the encoder may be reused, so let us reset it reset(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DoublePrecisionEncoderV2.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DoublePrecisionEncoderV2.java index 9b487be87..2bda42d23 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DoublePrecisionEncoderV2.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DoublePrecisionEncoderV2.java @@ -46,8 +46,7 @@ public void flush(ByteArrayOutputStream out) { // flip the byte no matter it is empty or not // the empty ending byte is necessary when decoding - bitsLeft = 0; - flipByte(out); + flushRemainingBits(out); // the encoder may be reused, so let us reset it reset(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/Encoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/Encoder.java index c42c22f0f..412f402cf 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/Encoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/Encoder.java @@ -104,6 +104,14 @@ public void encode(BigDecimal value, ByteArrayOutputStream out) { * * @return the maximal possible size of one data item encoded by this encoder */ + /** + * Returns the number of whole bytes that are buffered inside the encoder and not written to the + * output stream yet. Page sizing uses it so buffering bits does not change where pages are split. + */ + public int getPendingByteCount() { + return 0; + } + public int getOneItemMaxSize() { throw new UnsupportedOperationException(); } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/GorillaEncoderV2.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/GorillaEncoderV2.java index 0088a831c..413ea690d 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/GorillaEncoderV2.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/GorillaEncoderV2.java @@ -38,8 +38,11 @@ public abstract class GorillaEncoderV2 extends Encoder { protected int storedLeadingZeros = Integer.MAX_VALUE; protected int storedTrailingZeros = 0; - private byte buffer = 0; - protected int bitsLeft = Byte.SIZE; + private long bitBuffer; + private int bitsUsed; + + /** Reusable 8-byte buffer so a full 64-bit group is written with one call. */ + private final byte[] writeBuffer = new byte[Long.BYTES]; protected GorillaEncoderV2() { super(TSEncoding.GORILLA); @@ -50,56 +53,103 @@ public final long getMaxByteSize() { return 0; } + @Override + public int getPendingByteCount() { + return bitsUsed >>> 3; + } + protected void reset() { firstValueWasWritten = false; storedLeadingZeros = Integer.MAX_VALUE; storedTrailingZeros = 0; - buffer = 0; - bitsLeft = Byte.SIZE; + bitBuffer = 0; + bitsUsed = 0; } /** Stores a 0 and increases the count of bits by 1. */ protected void skipBit(ByteArrayOutputStream out) { - bitsLeft--; - flipByte(out); + appendBit(0, out); } /** Stores a 1 and increases the count of bits by 1. */ protected void writeBit(ByteArrayOutputStream out) { - buffer |= (1 << (bitsLeft - 1)); - bitsLeft--; - flipByte(out); + appendBit(1, out); + } + + private void appendBit(int bit, ByteArrayOutputStream out) { + bitBuffer = (bitBuffer << 1) | bit; + if (++bitsUsed == Long.SIZE) { + writeFullLong(out); + } } /** * Writes the given long value using the defined amount of least significant bits. * - * @param value The long value to be written + * @param value The value to write * @param bits How many bits are stored to the stream */ protected void writeBits(long value, int bits, ByteArrayOutputStream out) { while (bits > 0) { - int shift = bits - bitsLeft; - if (shift >= 0) { - buffer |= (byte) ((value >> shift) & ((1 << bitsLeft) - 1)); - bits -= bitsLeft; - bitsLeft = 0; + int take = Math.min(bits, Long.SIZE - bitsUsed); + if (take == Long.SIZE) { + // bitsUsed is 0 here; taking 64 bits must not use a shift (Java shifts by 64 as 0), + // otherwise a stale bitBuffer would leak into the stream. + bitBuffer = value; } else { - shift = bitsLeft - bits; - buffer |= (byte) (value << shift); - bitsLeft -= bits; - bits = 0; + bitBuffer = (bitBuffer << take) | ((value >>> (bits - take)) & ((1L << take) - 1L)); + } + bitsUsed += take; + bits -= take; + if (bitsUsed == Long.SIZE) { + writeFullLong(out); } - flipByte(out); } } - protected void flipByte(ByteArrayOutputStream out) { - if (bitsLeft == 0) { - out.write(buffer); - buffer = 0; - bitsLeft = Byte.SIZE; + private void writeFullLong(ByteArrayOutputStream out) { + byte[] buffer = writeBuffer; + buffer[0] = (byte) (bitBuffer >>> 56); + buffer[1] = (byte) (bitBuffer >>> 48); + buffer[2] = (byte) (bitBuffer >>> 40); + buffer[3] = (byte) (bitBuffer >>> 32); + buffer[4] = (byte) (bitBuffer >>> 24); + buffer[5] = (byte) (bitBuffer >>> 16); + buffer[6] = (byte) (bitBuffer >>> 8); + buffer[7] = (byte) bitBuffer; + out.write(buffer, 0, Long.BYTES); + bitBuffer = 0; + bitsUsed = 0; + } + + /** + * Flushes the bits accumulated so far, padding the last byte with zeros. This reproduces the + * previous byte-at-a-time output exactly, including the extra zero byte written when the stream + * ends on a byte boundary (the decoder expects it). + */ + protected void flushRemainingBits(ByteArrayOutputStream out) { + if (bitsUsed == 0) { + out.write(0); + bitBuffer = 0; + return; + } + int fullBytes = bitsUsed >>> 3; + int remainder = bitsUsed & 7; + long aligned = bitBuffer << (Long.SIZE - bitsUsed); + byte[] buffer = writeBuffer; + for (int i = 0; i < fullBytes; i++) { + buffer[i] = (byte) (aligned >>> (56 - 8 * i)); + } + if (remainder == 0) { + out.write(buffer, 0, fullBytes); + // The empty ending byte is necessary when decoding. + out.write(0); + } else { + buffer[fullBytes] = (byte) (aligned >>> (56 - 8 * fullBytes)); + out.write(buffer, 0, fullBytes + 1); } + bitBuffer = 0; + bitsUsed = 0; } } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntChimpEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntChimpEncoder.java index 2d342a593..68be79284 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntChimpEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntChimpEncoder.java @@ -95,8 +95,7 @@ public void flush(ByteArrayOutputStream out) { // flip the byte no matter it is empty or not // the empty ending byte is necessary when decoding - bitsLeft = 0; - flipByte(out); + flushRemainingBits(out); // the encoder may be reused, so let us reset it reset(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntGorillaEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntGorillaEncoder.java index dd8a7da4e..9bed56992 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntGorillaEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntGorillaEncoder.java @@ -69,8 +69,7 @@ public void flush(ByteArrayOutputStream out) { // flip the byte no matter it is empty or not // the empty ending byte is necessary when decoding - bitsLeft = 0; - flipByte(out); + flushRemainingBits(out); // the encoder may be reused, so let us reset it reset(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntRleEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntRleEncoder.java index a9fd2e8fc..3cc131881 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntRleEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntRleEncoder.java @@ -21,8 +21,12 @@ import org.apache.tsfile.common.conf.TSFileConfig; import org.apache.tsfile.encoding.bitpacking.IntPacker; +import org.apache.tsfile.i18n.Messages; import org.apache.tsfile.utils.ReadWriteForEncodingUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; @@ -30,19 +34,43 @@ /** Encoder for int value using rle or bit-packing. */ public class IntRleEncoder extends RleEncoder { + private static final Logger logger = LoggerFactory.getLogger(IntRleEncoder.class); + /** Packer for packing int values. */ private IntPacker packer; + /** Scratch array for packing one group, reused so each group does not allocate it. */ + private final int[] packBuffer = new int[TSFileConfig.RLE_MIN_REPEATED_NUM]; + + /** + * Values buffered until {@link #flush(ByteArrayOutputStream)}. A primitive array avoids boxing + * every dictionary index (the dictionary encoder feeds one index per value). + */ + private int[] intValues = new int[TSFileConfig.RLE_MIN_REPEATED_NUM * 4]; + + private int intValueCount; + + /** Primitive mirror of {@code bufferedValues} so encoding an int never boxes it. */ + private final int[] intBufferedValues = new int[TSFileConfig.RLE_MIN_REPEATED_NUM]; + + private int intPreValue; + public IntRleEncoder() { super(); bufferedValues = new Integer[TSFileConfig.RLE_MIN_REPEATED_NUM]; preValue = 0; + // Kept non-null for RleEncoder.reset(); the values themselves are buffered in intValues. values = new ArrayList<>(); } @Override public void encode(int value, ByteArrayOutputStream out) { - values.add(value); + if (intValueCount == intValues.length) { + int[] grown = new int[intValues.length << 1]; + System.arraycopy(intValues, 0, grown, 0, intValueCount); + intValues = grown; + } + intValues[intValueCount++] = value; } @Override @@ -63,18 +91,68 @@ public void encode(boolean value, ByteArrayOutputStream out) { @Override public void flush(ByteArrayOutputStream out) throws IOException { // we get bit width after receiving all data - this.bitWidth = ReadWriteForEncodingUtils.getIntMaxBitWidth(values); + this.bitWidth = getIntMaxBitWidth(intValues, intValueCount); packer = new IntPacker(bitWidth); - for (Integer value : values) { - encodeValue(value); + for (int i = 0; i < intValueCount; i++) { + encodeIntValue(intValues[i]); } super.flush(out); } + /** Primitive version of {@link RleEncoder#encodeValue(Comparable)} that avoids boxing. */ + @SuppressWarnings("squid:S3776") + private void encodeIntValue(int value) { + if (!isBitWidthSaved) { + byteCache.write(bitWidth); + isBitWidthSaved = true; + } + if (value == intPreValue) { + repeatCount++; + if (repeatCount >= TSFileConfig.RLE_MIN_REPEATED_NUM + && repeatCount <= TSFileConfig.RLE_MAX_REPEATED_NUM) { + return; + } else if (repeatCount == TSFileConfig.RLE_MAX_REPEATED_NUM + 1) { + repeatCount = TSFileConfig.RLE_MAX_REPEATED_NUM; + try { + writeRleRun(); + } catch (IOException e) { + logger.error(Messages.get("log.encoding.rle_full_rle_run_error"), repeatCount, e); + } + repeatCount = 1; + intPreValue = value; + } + } else { + if (repeatCount >= TSFileConfig.RLE_MIN_REPEATED_NUM) { + try { + writeRleRun(); + } catch (IOException e) { + logger.error(Messages.get("log.encoding.rle_write_num_error"), repeatCount, e); + } + } + repeatCount = 1; + intPreValue = value; + } + intBufferedValues[numBufferedValues] = value; + numBufferedValues++; + if (numBufferedValues == TSFileConfig.RLE_MIN_REPEATED_NUM) { + writeOrAppendBitPackedRun(); + } + } + + private static int getIntMaxBitWidth(int[] values, int count) { + int max = 1; + for (int i = 0; i < count; i++) { + max = Math.max(max, Integer.SIZE - Integer.numberOfLeadingZeros(values[i])); + } + return max; + } + @Override protected void reset() { super.reset(); preValue = 0; + intPreValue = 0; + intValueCount = 0; } /** write bytes to an outputStream using rle format: [header][value]. */ @@ -82,31 +160,28 @@ protected void reset() { protected void writeRleRun() throws IOException { endPreviousBitPackedRun(TSFileConfig.RLE_MIN_REPEATED_NUM); ReadWriteForEncodingUtils.writeUnsignedVarInt(repeatCount << 1, byteCache); - ReadWriteForEncodingUtils.writeIntLittleEndianPaddedOnBitWidth(preValue, byteCache, bitWidth); + ReadWriteForEncodingUtils.writeIntLittleEndianPaddedOnBitWidth( + intPreValue, byteCache, bitWidth); repeatCount = 0; numBufferedValues = 0; } @Override protected void clearBuffer() { - for (int i = numBufferedValues; i < TSFileConfig.RLE_MIN_REPEATED_NUM; i++) { - bufferedValues[i] = 0; + intBufferedValues[i] = 0; } } @Override protected void convertBuffer() { - byte[] bytes = new byte[bitWidth]; - - int[] tmpBuffer = new int[TSFileConfig.RLE_MIN_REPEATED_NUM]; + byte[] bytes = packScratch(bitWidth); for (int i = 0; i < TSFileConfig.RLE_MIN_REPEATED_NUM; i++) { - tmpBuffer[i] = bufferedValues[i]; + packBuffer[i] = intBufferedValues[i]; } - packer.pack8Values(tmpBuffer, 0, bytes); - // we'll not write bit-packing group to OutputStream immediately - // we buffer them in list - bytesBuffer.add(bytes); + packer.pack8Values(packBuffer, 0, bytes); + // the packed group is appended to the reusable bit-packing buffer + appendBitPackedGroup(bytes, bitWidth); } @Override @@ -119,11 +194,8 @@ public int getOneItemMaxSize() { @Override public long getMaxByteSize() { - if (values == null) { - return 0; - } // try to calculate max value - int groupNum = (values.size() / 8 + 1) / 63 + 1; - return (long) 8 + groupNum * 5 + values.size() * 4; + int groupNum = (intValueCount / 8 + 1) / 63 + 1; + return (long) 8 + groupNum * 5 + (long) intValueCount * 4; } } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongChimpEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongChimpEncoder.java index d3076a572..0759f8b65 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongChimpEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongChimpEncoder.java @@ -106,8 +106,7 @@ public void flush(ByteArrayOutputStream out) { // flip the byte no matter it is empty or not // the empty ending byte is necessary when decoding - bitsLeft = 0; - flipByte(out); + flushRemainingBits(out); // the encoder may be reused, so let us reset it reset(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongGorillaEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongGorillaEncoder.java index 1a13b7d80..c8a7fff5c 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongGorillaEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongGorillaEncoder.java @@ -69,8 +69,7 @@ public void flush(ByteArrayOutputStream out) { // flip the byte no matter it is empty or not // the empty ending byte is necessary when decoding - bitsLeft = 0; - flipByte(out); + flushRemainingBits(out); // the encoder may be reused, so let us reset it reset(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongRleEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongRleEncoder.java index f9e9c5570..8372f80ce 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongRleEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongRleEncoder.java @@ -16,7 +16,6 @@ * specific language governing permissions and limitations * under the License. */ - package org.apache.tsfile.encoding.encoder; import org.apache.tsfile.common.conf.TSFileConfig; @@ -38,7 +37,7 @@ public LongRleEncoder() { super(); bufferedValues = new Long[TSFileConfig.RLE_MIN_REPEATED_NUM]; preValue = (long) 0; - values = new ArrayList(); + values = new ArrayList<>(); } @Override @@ -70,7 +69,7 @@ protected void reset() { } /** - * write bytes to OutputStream using rle rle format: [header][value]. + * write bytes to OutputStream using rle format: [header][value]. * * @throws IOException cannot write rle run */ @@ -92,15 +91,13 @@ protected void clearBuffer() { @Override protected void convertBuffer() { - byte[] bytes = new byte[bitWidth]; + byte[] bytes = packScratch(bitWidth); long[] tmpBuffer = new long[TSFileConfig.RLE_MIN_REPEATED_NUM]; for (int i = 0; i < TSFileConfig.RLE_MIN_REPEATED_NUM; i++) { tmpBuffer[i] = bufferedValues[i]; } packer.pack8Values(tmpBuffer, 0, bytes); - // we'll not write bit-packing group to OutputStream immediately - // we buffer them in list - bytesBuffer.add(bytes); + appendBitPackedGroup(bytes, bitWidth); } @Override diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/RleEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/RleEncoder.java index f3a8be7cd..4872361f1 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/RleEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/RleEncoder.java @@ -25,6 +25,7 @@ import org.apache.tsfile.file.metadata.enums.TSEncoding; import org.apache.tsfile.i18n.Messages; import org.apache.tsfile.utils.Binary; +import org.apache.tsfile.utils.PublicBAOS; import org.apache.tsfile.utils.ReadWriteForEncodingUtils; import org.slf4j.Logger; @@ -33,7 +34,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.math.BigDecimal; -import java.util.ArrayList; import java.util.List; /** @@ -78,10 +78,13 @@ public abstract class RleEncoder> extends Encoder { protected int numBufferedValues; /** - * we will write all bytes using bit-packing to OutputStream once. Before that, all bytes are - * saved in list. + * Buffers the packed bytes of the current bit-packing run. It is reused across pages so that + * packing a group does not allocate a new byte array per group. */ - protected List bytesBuffer; + protected PublicBAOS bitPackedBuffer; + + /** Scratch array for packing one group of {@code RLE_MIN_REPEATED_NUM} values. */ + private byte[] packScratch = new byte[32]; /** flag which indicate encoding mode false -- rle true -- bit-packing. */ protected boolean isBitPackRun; @@ -102,7 +105,7 @@ public abstract class RleEncoder> extends Encoder { /** constructor. */ protected RleEncoder() { super(TSEncoding.RLE); - bytesBuffer = new ArrayList<>(); + bitPackedBuffer = new PublicBAOS(); isBitPackRun = false; isBitWidthSaved = false; byteCache = new ByteArrayOutputStream(); @@ -112,7 +115,7 @@ protected void reset() { numBufferedValues = 0; repeatCount = 0; bitPackedGroupCount = 0; - bytesBuffer.clear(); + bitPackedBuffer.reset(); isBitPackRun = false; isBitWidthSaved = false; byteCache.reset(); @@ -183,8 +186,8 @@ public void writeOrAppendBitPackedRun() { /** * End a bit-packing run write all bit-packing group to OutputStream bit-packing format: {@code - * [header][lastBitPackedNum][bit-packing group]+ [bit-packing group]+ are saved in List - * bytesBuffer }. + * [header][lastBitPackedNum][bit-packing group]+ [bit-packing group]+ are appended to the reusable + * bit-packing buffer. * * @param lastBitPackedNum - in last bit-packing group, it may have useful values less than 8. * This param indicates how many values are useful @@ -196,14 +199,28 @@ protected void endPreviousBitPackedRun(int lastBitPackedNum) { byte bitPackHeader = (byte) ((bitPackedGroupCount << 1) | 1); byteCache.write(bitPackHeader); byteCache.write(lastBitPackedNum); - for (byte[] bytes : bytesBuffer) { - byteCache.write(bytes, 0, bytes.length); - } - bytesBuffer.clear(); + byteCache.write(bitPackedBuffer.getBuf(), 0, bitPackedBuffer.size()); + bitPackedBuffer.reset(); isBitPackRun = false; bitPackedGroupCount = 0; } + /** + * Returns a scratch array with at least {@code length} bytes for packing one group, then appends + * the packed bytes to the current bit-packing run. + */ + protected byte[] packScratch(int length) { + if (packScratch.length < length) { + packScratch = new byte[length]; + } + return packScratch; + } + + /** Appends one packed group to the current bit-packing run. */ + protected void appendBitPackedGroup(byte[] bytes, int length) { + bitPackedBuffer.write(bytes, 0, length); + } + /** * Encode T value using rle or bit-packing. It may not write to OutputStream immediately * diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/SinglePrecisionChimpEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/SinglePrecisionChimpEncoder.java index e31bc592f..75dc4c540 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/SinglePrecisionChimpEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/SinglePrecisionChimpEncoder.java @@ -46,8 +46,7 @@ public void flush(ByteArrayOutputStream out) { // flip the byte no matter it is empty or not // the empty ending byte is necessary when decoding - bitsLeft = 0; - flipByte(out); + flushRemainingBits(out); // the encoder may be reused, so let us reset it reset(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/SinglePrecisionEncoderV2.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/SinglePrecisionEncoderV2.java index 813a1e3fa..2103fb9c0 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/SinglePrecisionEncoderV2.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/SinglePrecisionEncoderV2.java @@ -46,8 +46,7 @@ public void flush(ByteArrayOutputStream out) { // flip the byte no matter it is empty or not // the empty ending byte is necessary when decoding - bitsLeft = 0; - flipByte(out); + flushRemainingBits(out); // the encoder may be reused, so let us reset it reset(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/AlignedChunkGroupWriterImpl.java b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/AlignedChunkGroupWriterImpl.java index 24eae13ee..0a02f9f0f 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/AlignedChunkGroupWriterImpl.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/AlignedChunkGroupWriterImpl.java @@ -267,9 +267,9 @@ public long flushToFileWriter(TsFileIOWriter tsfileWriter) throws IOException { @Override public long updateMaxGroupMemSize() { - long bufferSize = timeChunkWriter.estimateMaxSeriesMemSize(); + long bufferSize = timeChunkWriter.getRetainedMemSize(); for (ValueChunkWriter valueChunkWriter : valueChunkWriterMap.values()) { - bufferSize += valueChunkWriter.estimateMaxSeriesMemSize(); + bufferSize += valueChunkWriter.getRetainedMemSize(); } return bufferSize; } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/AlignedChunkWriterImpl.java b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/AlignedChunkWriterImpl.java index ecdf14151..71143dede 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/AlignedChunkWriterImpl.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/AlignedChunkWriterImpl.java @@ -481,6 +481,14 @@ public void sealCurrentValuePage(int valueIndex) { valueChunkWriterList.get(valueIndex).sealCurrentPage(); } + @Override + public void setExpectedPageCount(int expectedPageCount) { + timeChunkWriter.setExpectedPageCount(expectedPageCount); + for (ValueChunkWriter valueChunkWriter : valueChunkWriterList) { + valueChunkWriter.setExpectedPageCount(expectedPageCount); + } + } + @Override public void clearPageWriter() { timeChunkWriter.clearPageWriter(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ChunkPageBuffer.java b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ChunkPageBuffer.java new file mode 100644 index 000000000..6404c0386 --- /dev/null +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ChunkPageBuffer.java @@ -0,0 +1,305 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tsfile.write.chunk; + +import org.apache.tsfile.utils.PublicBAOS; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.List; + +/** + * Chunk page storage backed by a slab stream. + * + *

The first page is kept in a scratch buffer so its statistics can still be inserted later. + * Subsequent page bytes are appended into slabs; slabs are filled before a new one is taken and are + * recycled through size-class LIFO pools. This keeps the retained memory close to the page bytes + * and avoids both chunk-level doubling and per-page block slack. + */ +final class ChunkPageBuffer extends PublicBAOS { + + private static final int MIN_SLAB = 4 * 1024; + private static final int MAX_POOLED_SLAB = 64 * 1024; + private static final int[] SLAB_SIZES = { + MIN_SLAB, 8 * 1024, 16 * 1024, 32 * 1024, MAX_POOLED_SLAB + }; + + private final List slabs = new ArrayList<>(); + private final ArrayDeque[] freeSlabs; + + private byte[] currentSlab; + private int slabPosition; + private long totalBytes; + private long activeCapacity; + + private PublicBAOS firstPage = new PublicBAOS(); + private boolean firstPageFlushed; + private int firstPageLength; + + private int slabSize = MIN_SLAB; + private long poolBytes; + private long poolLimitBytes = MAX_POOLED_SLAB; + + private int pageIndex = -1; + + @SuppressWarnings("unchecked") + ChunkPageBuffer() { + freeSlabs = new ArrayDeque[SLAB_SIZES.length]; + for (int i = 0; i < freeSlabs.length; i++) { + freeSlabs[i] = new ArrayDeque<>(); + } + } + + void beginPage() { + pageIndex++; + if (pageIndex == 0) { + firstPage.reset(); + firstPageFlushed = false; + firstPageLength = 0; + } + } + + void endPage() { + // Bytes are appended to the slab stream as they arrive; no per-page finalization is needed. + } + + @Override + public void write(int b) { + if (pageIndex <= 0) { + firstPage.write(b); + return; + } + flushFirstPageIfNeeded(); + ensureSlab(1); + currentSlab[slabPosition++] = (byte) b; + totalBytes++; + } + + @Override + public void write(byte[] b, int off, int len) { + if (pageIndex <= 0) { + firstPage.write(b, off, len); + } else { + flushFirstPageIfNeeded(); + appendToSlabs(b, off, len); + } + } + + private void flushFirstPageIfNeeded() { + if (firstPageFlushed) { + return; + } + firstPageLength = firstPage.size(); + slabSize = clampSlabSize(nextPowerOfTwo(firstPageLength)); + appendToSlabs(firstPage.getBuf(), 0, firstPageLength); + firstPage.reset(); + firstPageFlushed = true; + } + + private void appendToSlabs(byte[] b, int off, int len) { + int remaining = len; + int position = off; + while (remaining > 0) { + ensureSlab(remaining); + int copyLength = Math.min(remaining, currentSlab.length - slabPosition); + System.arraycopy(b, position, currentSlab, slabPosition, copyLength); + slabPosition += copyLength; + position += copyLength; + remaining -= copyLength; + } + totalBytes += len; + } + + private void ensureSlab(int required) { + if (currentSlab != null && currentSlab.length - slabPosition > 0) { + return; + } + int size = Math.max(slabSize, required); + if (size > MAX_POOLED_SLAB) { + currentSlab = new byte[size]; + } else { + int sizeClass = sizeClass(size); + currentSlab = freeSlabs[sizeClass].pollFirst(); + if (currentSlab == null) { + currentSlab = new byte[SLAB_SIZES[sizeClass]]; + } else { + poolBytes -= currentSlab.length; + } + } + slabs.add(currentSlab); + activeCapacity += currentSlab.length; + slabPosition = 0; + } + + private void returnSlab(byte[] slab) { + if (slab.length > MAX_POOLED_SLAB || poolBytes + slab.length > poolLimitBytes) { + return; + } + freeSlabs[sizeClass(slab.length)].addFirst(slab); + poolBytes += slab.length; + } + + private static int nextPowerOfTwo(int value) { + if (value <= MIN_SLAB) { + return MIN_SLAB; + } + int highest = Integer.highestOneBit(value - 1); + return highest >= MAX_POOLED_SLAB ? MAX_POOLED_SLAB : highest << 1; + } + + private static int clampSlabSize(int size) { + return Math.max(MIN_SLAB, Math.min(MAX_POOLED_SLAB, size)); + } + + private static int sizeClass(int size) { + for (int i = 0; i < SLAB_SIZES.length; i++) { + if (size <= SLAB_SIZES[i]) { + return i; + } + } + return SLAB_SIZES.length - 1; + } + + @Override + public int size() { + return (int) Math.min(Integer.MAX_VALUE, firstPageFlushed ? totalBytes : firstPage.size()); + } + + int getPageCount() { + return Math.max(0, pageIndex + 1); + } + + byte[] getFirstPage() { + if (!firstPageFlushed) { + return firstPage.toByteArray(); + } + byte[] first = new byte[firstPageLength]; + int position = 0; + for (byte[] slab : slabs) { + int copyLength = Math.min(slab.length, firstPageLength - position); + if (copyLength <= 0) { + break; + } + System.arraycopy(slab, 0, first, position, copyLength); + position += copyLength; + if (position == firstPageLength) { + break; + } + } + return first; + } + + void replaceFirstPage(byte[] page) { + if (firstPageFlushed) { + throw new IllegalStateException("first page has already been flushed"); + } + firstPage.reset(); + firstPage.write(page, 0, page.length); + } + + void truncateFirstPage(int newSize) { + if (firstPageFlushed) { + throw new IllegalStateException("first page has already been flushed"); + } + byte[] page = firstPage.toByteArray(); + if (newSize < 0 || newSize > page.length) { + throw new IllegalArgumentException("newSize=" + newSize + ", pageSize=" + page.length); + } + firstPage.reset(); + firstPage.write(page, 0, newSize); + } + + @Override + public byte[] toByteArray() { + if (!firstPageFlushed) { + return firstPage.toByteArray(); + } + byte[] result = new byte[size()]; + int position = 0; + for (byte[] slab : slabs) { + int copyLength = Math.min(slab.length, result.length - position); + if (copyLength <= 0) { + break; + } + System.arraycopy(slab, 0, result, position, copyLength); + position += copyLength; + if (position == result.length) { + break; + } + } + return result; + } + + @Override + public void writeTo(OutputStream out) throws IOException { + if (!firstPageFlushed) { + out.write(firstPage.getBuf(), 0, firstPage.size()); + return; + } + int remaining = size(); + for (byte[] slab : slabs) { + int writeLength = Math.min(slab.length, remaining); + if (writeLength <= 0) { + break; + } + out.write(slab, 0, writeLength); + remaining -= writeLength; + if (remaining == 0) { + break; + } + } + } + + @Override + public void reset() { + poolLimitBytes = Math.max(MIN_SLAB, totalBytes); + for (byte[] slab : slabs) { + returnSlab(slab); + } + clearState(); + } + + long retainedBytes() { + return activeCapacity + poolBytes + firstPage.getBuf().length; + } + + void release() { + for (ArrayDeque free : freeSlabs) { + free.clear(); + } + poolBytes = 0; + firstPage = new PublicBAOS(); + clearState(); + } + + private void clearState() { + slabs.clear(); + currentSlab = null; + slabPosition = 0; + totalBytes = 0; + activeCapacity = 0; + firstPage.reset(); + firstPageFlushed = false; + firstPageLength = 0; + slabSize = MIN_SLAB; + pageIndex = -1; + } +} diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ChunkWriterImpl.java b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ChunkWriterImpl.java index b4683382b..6843098b3 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ChunkWriterImpl.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ChunkWriterImpl.java @@ -58,7 +58,17 @@ public class ChunkWriterImpl implements IChunkWriter { private long chunkOrdinal = -1; /** all pages of this chunk. */ - private final PublicBAOS pageBuffer; + private ChunkPageBuffer pageBuffer; + + /** Expected number of pages of the next chunk, used to size the chunk buffer once. */ + private int expectedPageCount; + + private static final int MAX_PAGE_HINT = 16; + + /** True when the first page statistics were written up front (multi-page chunk). */ + private boolean firstPageStatisticsWritten; + + private int firstPageStatisticsSize; private int numOfPages; @@ -108,7 +118,7 @@ public ChunkWriterImpl(IMeasurementSchema schema) { this.compressor = ICompressor.getCompressor(schema.getCompressor()); this.encryptParam = EncryptUtils.getEncryptParameter(); this.chunkOrdinal = nextChunkOrdinal(); - this.pageBuffer = new PublicBAOS(); + this.pageBuffer = new ChunkPageBuffer(); this.pageSizeThreshold = TSFileDescriptor.getInstance().getConfig().getPageSizeInByte(); this.maxNumberOfPointsInPage = @@ -134,7 +144,7 @@ public ChunkWriterImpl(IMeasurementSchema schema, EncryptParameter encryptParam) this.compressor = ICompressor.getCompressor(schema.getCompressor()); this.encryptParam = encryptParam; this.chunkOrdinal = nextChunkOrdinal(); - this.pageBuffer = new PublicBAOS(); + this.pageBuffer = new ChunkPageBuffer(); this.pageSizeThreshold = TSFileDescriptor.getInstance().getConfig().getPageSizeInByte(); this.maxNumberOfPointsInPage = @@ -331,22 +341,30 @@ private void checkPageSizeAndMayOpenANewPage() { private void writePageToPageBuffer() { try { + pageBuffer.beginPage(); if (numOfPages == 0) { // record the firstPageStatistics + boolean includeFirstPageStatistics = expectedPageCount > 1; this.firstPageStatistics = pageWriter.getStatistics(); + this.firstPageStatisticsSize = + includeFirstPageStatistics ? firstPageStatistics.getSerializedSize() : 0; this.sizeWithoutStatistic = - pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, true, numOfPages); + pageWriter.writePageHeaderAndDataIntoBuff( + pageBuffer, true, numOfPages, includeFirstPageStatistics); + this.firstPageStatisticsWritten = includeFirstPageStatistics && firstPageStatisticsSize > 0; + if (firstPageStatisticsWritten) { + this.firstPageStatistics = null; + } } else if (numOfPages == 1) { // put the firstPageStatistics into pageBuffer - byte[] b = pageBuffer.toByteArray(); - pageBuffer.reset(); - pageBuffer.write(b, 0, this.sizeWithoutStatistic); - firstPageStatistics.serialize(pageBuffer); - pageBuffer.write(b, this.sizeWithoutStatistic, b.length - this.sizeWithoutStatistic); + if (!firstPageStatisticsWritten) { + insertFirstPageStatistics(); + } pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages); firstPageStatistics = null; } else { pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages); } + pageBuffer.endPage(); // update statistics of this chunk numOfPages++; this.statistics.mergeStatistics(pageWriter.getStatistics()); @@ -368,10 +386,17 @@ public void writeToFileWriter( TsFileIOWriter tsfileWriter, Function measurementNameRemapper) throws IOException { sealCurrentPage(); + compactPreWrittenFirstPageStatistics(); writeAllPagesOfChunkToTsFile(tsfileWriter, statistics, measurementNameRemapper); - // reinit this chunk writer + // reinit this chunk writer so that a recycled writer behaves like a fresh one: reset the page + // size check heuristic (and SDT last point), otherwise the first page of the next chunk could + // be sized differently. pageBuffer.reset(); + firstPageStatisticsWritten = false; + firstPageStatisticsSize = 0; + this.valueCountInOnePageForNextCheck = MINIMUM_RECORD_COUNT_FOR_CHECK; + this.isLastPoint = false; numOfPages = 0; sizeWithoutStatistic = 0; firstPageStatistics = null; @@ -415,6 +440,51 @@ public void clearPageWriter() { pageWriter = null; } + @Override + public void setExpectedPageCount(int expectedPageCount) { + this.expectedPageCount = Math.max(0, Math.min(expectedPageCount, MAX_PAGE_HINT)); + } + + /** + * Removes the first page statistics that were written up front when the chunk ends up with a + * single page. Shifts the page data in place, so no new array is allocated. + */ + private void compactPreWrittenFirstPageStatistics() { + if (!firstPageStatisticsWritten || numOfPages != 1) { + return; + } + byte[] firstPage = pageBuffer.getFirstPage(); + int dataStart = sizeWithoutStatistic + firstPageStatisticsSize; + int tail = firstPage.length - dataStart; + byte[] compacted = new byte[sizeWithoutStatistic + tail]; + System.arraycopy(firstPage, 0, compacted, 0, sizeWithoutStatistic); + System.arraycopy(firstPage, dataStart, compacted, sizeWithoutStatistic, tail); + pageBuffer.replaceFirstPage(compacted); + firstPageStatisticsWritten = false; + } + + /** Inserts the first page statistics between the first page header and its data. */ + private void insertFirstPageStatistics() throws IOException { + byte[] firstPage = pageBuffer.getFirstPage(); + PublicBAOS statisticsBuffer = new PublicBAOS(); + firstPageStatistics.serialize(statisticsBuffer); + byte[] statisticsBytes = statisticsBuffer.toByteArray(); + byte[] newPage = new byte[firstPage.length + statisticsBytes.length]; + System.arraycopy(firstPage, 0, newPage, 0, sizeWithoutStatistic); + System.arraycopy(statisticsBytes, 0, newPage, sizeWithoutStatistic, statisticsBytes.length); + System.arraycopy( + firstPage, + sizeWithoutStatistic, + newPage, + sizeWithoutStatistic + statisticsBytes.length, + firstPage.length - sizeWithoutStatistic); + pageBuffer.replaceFirstPage(newPage); + } + + public long getRetainedMemSize() { + return pageBuffer.retainedBytes() + pageWriter.retainedBufferBytes(); + } + @Override public boolean checkIsUnsealedPageOverThreshold( long size, long pointNum, boolean returnTrueIfPageEmpty) { @@ -455,6 +525,7 @@ public void writePageHeaderAndDataIntoBuff(ByteBuffer data, PageHeader header) throw new org.apache.tsfile.exception.encrypt.EncryptException( Messages.get("error.write.aead_raw_page_copy_unsupported")); } + pageBuffer.beginPage(); // write the page header to pageBuffer try { logger.debug(Messages.get("log.write.page_header_flush_start"), pageBuffer.size()); @@ -498,6 +569,7 @@ public void writePageHeaderAndDataIntoBuff(ByteBuffer data, PageHeader header) } catch (IOException e) { throw new PageException(e); } + pageBuffer.endPage(); } /** diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/IChunkWriter.java b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/IChunkWriter.java index 4a198841c..03db1605f 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/IChunkWriter.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/IChunkWriter.java @@ -54,6 +54,22 @@ void writeToFileWriter( /** set the current pageWriter to null, friendly for gc */ void clearPageWriter(); + /** + * Layout hint: whether the next chunk is expected to have more than one page. The value is + * clamped by the implementation and is only used to decide whether the first-page statistics can + * be written up front. It does not size any buffer. + */ + default void setExpectedPageCount(int expectedPageCount) {} + + /** + * Retained heap bytes of this writer's buffers, used by the global flush memory threshold. It is + * deliberately separate from {@link #estimateMaxSeriesMemSize()}, which keeps the logical + * serialized size used for chunk/page decisions. + */ + default long getRetainedMemSize() { + return estimateMaxSeriesMemSize(); + } + /** * used for compaction to check whether the chunk is over threshold or not. Return true if there * is no unsealed chunk. diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/NonAlignedChunkGroupWriterImpl.java b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/NonAlignedChunkGroupWriterImpl.java index ae1e6aeb0..82d10252b 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/NonAlignedChunkGroupWriterImpl.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/NonAlignedChunkGroupWriterImpl.java @@ -170,7 +170,7 @@ public long flushToFileWriter(TsFileIOWriter fileWriter) throws IOException { public long updateMaxGroupMemSize() { long bufferSize = 0; for (IChunkWriter seriesWriter : chunkWriters.values()) { - bufferSize += seriesWriter.estimateMaxSeriesMemSize(); + bufferSize += seriesWriter.getRetainedMemSize(); } return bufferSize; } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/TimeChunkWriter.java b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/TimeChunkWriter.java index 8f4a01d00..ed58e8178 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/TimeChunkWriter.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/TimeChunkWriter.java @@ -60,7 +60,17 @@ public class TimeChunkWriter { private long chunkOrdinal = -1; /** all pages of this chunk. */ - private PublicBAOS pageBuffer; + private ChunkPageBuffer pageBuffer; + + /** Expected number of pages of the next chunk, used to size the chunk buffer once. */ + private int expectedPageCount; + + private static final int MAX_PAGE_HINT = 16; + + /** True when the first page statistics were written up front (multi-page chunk). */ + private boolean firstPageStatisticsWritten; + + private int firstPageStatisticsSize; private int numOfPages; @@ -98,7 +108,7 @@ public TimeChunkWriter( this.compressionType = compressionType; this.encryptParam = EncryptUtils.getEncryptParameter(); this.chunkOrdinal = nextChunkOrdinal(); - this.pageBuffer = new PublicBAOS(); + this.pageBuffer = new ChunkPageBuffer(); this.pageSizeThreshold = TSFileDescriptor.getInstance().getConfig().getPageSizeInByte(); this.maxNumberOfPointsInPage = @@ -126,7 +136,7 @@ public TimeChunkWriter( this.compressionType = compressionType; this.encryptParam = encryptParam; this.chunkOrdinal = nextChunkOrdinal(); - this.pageBuffer = new PublicBAOS(); + this.pageBuffer = new ChunkPageBuffer(); this.pageSizeThreshold = TSFileDescriptor.getInstance().getConfig().getPageSizeInByte(); this.maxNumberOfPointsInPage = @@ -193,22 +203,30 @@ public long getRemainingPointNumberForCurrentPage() { public void writePageToPageBuffer() { try { + pageBuffer.beginPage(); if (numOfPages == 0) { // record the firstPageStatistics + boolean includeFirstPageStatistics = expectedPageCount > 1; this.firstPageStatistics = pageWriter.getStatistics(); + this.firstPageStatisticsSize = + includeFirstPageStatistics ? firstPageStatistics.getSerializedSize() : 0; this.sizeWithoutStatistic = - pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, true, numOfPages); + pageWriter.writePageHeaderAndDataIntoBuff( + pageBuffer, true, numOfPages, includeFirstPageStatistics); + this.firstPageStatisticsWritten = includeFirstPageStatistics && firstPageStatisticsSize > 0; + if (firstPageStatisticsWritten) { + this.firstPageStatistics = null; + } } else if (numOfPages == 1) { // put the firstPageStatistics into pageBuffer - byte[] b = pageBuffer.toByteArray(); - pageBuffer.reset(); - pageBuffer.write(b, 0, this.sizeWithoutStatistic); - firstPageStatistics.serialize(pageBuffer); - pageBuffer.write(b, this.sizeWithoutStatistic, b.length - this.sizeWithoutStatistic); + if (!firstPageStatisticsWritten) { + insertFirstPageStatistics(); + } pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages); firstPageStatistics = null; } else { pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages); } + pageBuffer.endPage(); // update statistics of this chunk numOfPages++; this.statistics.mergeStatistics(pageWriter.getStatistics()); @@ -226,6 +244,7 @@ public void writePageHeaderAndDataIntoBuff(ByteBuffer data, PageHeader header) throw new org.apache.tsfile.exception.encrypt.EncryptException( Messages.get("error.write.aead_raw_page_copy_unsupported")); } + pageBuffer.beginPage(); // write the page header to pageBuffer try { logger.debug(Messages.get("log.write.page_header_flush_start"), pageBuffer.size()); @@ -266,14 +285,21 @@ public void writePageHeaderAndDataIntoBuff(ByteBuffer data, PageHeader header) } catch (IOException e) { throw new PageException(e); } + pageBuffer.endPage(); } public void writeToFileWriter(TsFileIOWriter tsfileWriter) throws IOException { sealCurrentPage(); + compactPreWrittenFirstPageStatistics(); writeAllPagesOfChunkToTsFile(tsfileWriter); - // reinit this chunk writer + // reinit this chunk writer so that a recycled writer behaves like a fresh one: reset the page + // size check heuristic (and SDT last point), otherwise the first page of the next chunk could + // be sized differently. pageBuffer.reset(); + firstPageStatisticsWritten = false; + firstPageStatisticsSize = 0; + this.valueCountInOnePageForNextCheck = MINIMUM_RECORD_COUNT_FOR_CHECK; numOfPages = 0; sizeWithoutStatistic = 0; firstPageStatistics = null; @@ -313,6 +339,50 @@ public void clearPageWriter() { pageWriter = null; } + public void setExpectedPageCount(int expectedPageCount) { + this.expectedPageCount = Math.max(0, Math.min(expectedPageCount, MAX_PAGE_HINT)); + } + + /** + * Removes the first page statistics that were written up front when the chunk ends up with a + * single page. Shifts the page data in place, so no new array is allocated. + */ + private void compactPreWrittenFirstPageStatistics() { + if (!firstPageStatisticsWritten || numOfPages != 1) { + return; + } + byte[] firstPage = pageBuffer.getFirstPage(); + int dataStart = sizeWithoutStatistic + firstPageStatisticsSize; + int tail = firstPage.length - dataStart; + byte[] compacted = new byte[sizeWithoutStatistic + tail]; + System.arraycopy(firstPage, 0, compacted, 0, sizeWithoutStatistic); + System.arraycopy(firstPage, dataStart, compacted, sizeWithoutStatistic, tail); + pageBuffer.replaceFirstPage(compacted); + firstPageStatisticsWritten = false; + } + + /** Inserts the first page statistics between the first page header and its data. */ + private void insertFirstPageStatistics() throws IOException { + byte[] firstPage = pageBuffer.getFirstPage(); + PublicBAOS statisticsBuffer = new PublicBAOS(); + firstPageStatistics.serialize(statisticsBuffer); + byte[] statisticsBytes = statisticsBuffer.toByteArray(); + byte[] newPage = new byte[firstPage.length + statisticsBytes.length]; + System.arraycopy(firstPage, 0, newPage, 0, sizeWithoutStatistic); + System.arraycopy(statisticsBytes, 0, newPage, sizeWithoutStatistic, statisticsBytes.length); + System.arraycopy( + firstPage, + sizeWithoutStatistic, + newPage, + sizeWithoutStatistic + statisticsBytes.length, + firstPage.length - sizeWithoutStatistic); + pageBuffer.replaceFirstPage(newPage); + } + + public long getRetainedMemSize() { + return pageBuffer.retainedBytes() + pageWriter.retainedBufferBytes(); + } + public int getNumOfPages() { return numOfPages; } @@ -363,9 +433,16 @@ public void writeAllPagesOfChunkToTsFile(TsFileIOWriter writer) throws IOExcepti writer.endCurrentChunk(); } - /** only used for test */ + /** + * @deprecated test-only snapshot of the page bytes; the returned buffer is not live. Use {@link + * #getByteBuffer()} when possible. + */ + @Deprecated public PublicBAOS getPageBuffer() { - return pageBuffer; + PublicBAOS copy = new PublicBAOS(); + byte[] bytes = pageBuffer.toByteArray(); + copy.write(bytes, 0, bytes.length); + return copy; } public TimePageWriter getPageWriter() { diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ValueChunkWriter.java b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ValueChunkWriter.java index 12eebc59f..6252d95a5 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ValueChunkWriter.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ValueChunkWriter.java @@ -64,7 +64,17 @@ public class ValueChunkWriter { private long chunkOrdinal = -1; /** all pages of this chunk. */ - private final PublicBAOS pageBuffer; + private ChunkPageBuffer pageBuffer; + + /** Expected number of pages of the next chunk, used to size the chunk buffer once. */ + private int expectedPageCount; + + private static final int MAX_PAGE_HINT = 16; + + /** True when the first page statistics were written up front (multi-page chunk). */ + private boolean firstPageStatisticsWritten; + + private int firstPageStatisticsSize; private int numOfPages; @@ -102,7 +112,7 @@ public ValueChunkWriter( this.compressionType = compressionType; this.encryptParam = EncryptUtils.getEncryptParameter(); this.chunkOrdinal = nextChunkOrdinal(); - this.pageBuffer = new PublicBAOS(); + this.pageBuffer = new ChunkPageBuffer(); this.pageSizeThreshold = TSFileDescriptor.getInstance().getConfig().getPageSizeInByte(); this.maxNumberOfPointsInPage = TSFileDescriptor.getInstance().getConfig().getMaxNumberOfPointsInPage(); @@ -130,7 +140,7 @@ public ValueChunkWriter( this.compressionType = compressionType; this.encryptParam = encryptParam; this.chunkOrdinal = nextChunkOrdinal(); - this.pageBuffer = new PublicBAOS(); + this.pageBuffer = new ChunkPageBuffer(); this.pageSizeThreshold = TSFileDescriptor.getInstance().getConfig().getPageSizeInByte(); this.maxNumberOfPointsInPage = TSFileDescriptor.getInstance().getConfig().getMaxNumberOfPointsInPage(); @@ -205,35 +215,57 @@ public void write(long[] timestamps, Binary[] values, boolean[] isNull, int batc } public void writeEmptyPageToPageBuffer() throws IOException { - if (numOfPages == 1 && firstPageStatistics != null) { + pageBuffer.beginPage(); + if (!firstPageStatisticsWritten && numOfPages == 1 && firstPageStatistics != null) { // if the first page is not an empty page - byte[] b = pageBuffer.toByteArray(); - pageBuffer.reset(); - pageBuffer.write(b, 0, this.sizeWithoutStatistic); - firstPageStatistics.serialize(pageBuffer); - pageBuffer.write(b, this.sizeWithoutStatistic, b.length - this.sizeWithoutStatistic); + insertFirstPageStatistics(); firstPageStatistics = null; } pageWriter.writeEmptyPageIntoBuff(pageBuffer); + pageBuffer.endPage(); numOfPages++; } + /** Inserts the first page statistics between the first page header and its data. */ + private void insertFirstPageStatistics() throws IOException { + byte[] firstPage = pageBuffer.getFirstPage(); + PublicBAOS statisticsBuffer = new PublicBAOS(); + firstPageStatistics.serialize(statisticsBuffer); + byte[] statisticsBytes = statisticsBuffer.toByteArray(); + byte[] newPage = new byte[firstPage.length + statisticsBytes.length]; + System.arraycopy(firstPage, 0, newPage, 0, sizeWithoutStatistic); + System.arraycopy(statisticsBytes, 0, newPage, sizeWithoutStatistic, statisticsBytes.length); + System.arraycopy( + firstPage, + sizeWithoutStatistic, + newPage, + sizeWithoutStatistic + statisticsBytes.length, + firstPage.length - sizeWithoutStatistic); + pageBuffer.replaceFirstPage(newPage); + } + public void writePageToPageBuffer() { try { + pageBuffer.beginPage(); if (numOfPages == 0) { + boolean includeFirstPageStatistics = expectedPageCount > 1; if (pageWriter.getStatistics().getCount() != 0) { // record the firstPageStatistics if it is not empty page this.firstPageStatistics = pageWriter.getStatistics(); + this.firstPageStatisticsSize = + includeFirstPageStatistics ? firstPageStatistics.getSerializedSize() : 0; } this.sizeWithoutStatistic = - pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, true, numOfPages); + pageWriter.writePageHeaderAndDataIntoBuff( + pageBuffer, true, numOfPages, includeFirstPageStatistics); + this.firstPageStatisticsWritten = includeFirstPageStatistics && firstPageStatisticsSize > 0; + if (firstPageStatisticsWritten) { + this.firstPageStatistics = null; + } } else if (numOfPages == 1) { // put the firstPageStatistics into pageBuffer - if (firstPageStatistics != null) { // Consider previous page is an empty page - byte[] b = pageBuffer.toByteArray(); - pageBuffer.reset(); - pageBuffer.write(b, 0, this.sizeWithoutStatistic); - firstPageStatistics.serialize(pageBuffer); - pageBuffer.write(b, this.sizeWithoutStatistic, b.length - this.sizeWithoutStatistic); + if (!firstPageStatisticsWritten && firstPageStatistics != null) { + // Fallback when the chunk turned out to have a second page but no page count was known. + insertFirstPageStatistics(); } pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages); firstPageStatistics = null; @@ -241,6 +273,7 @@ public void writePageToPageBuffer() { pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages); } + pageBuffer.endPage(); // update statistics of this chunk numOfPages++; this.statistics.mergeStatistics(pageWriter.getStatistics()); @@ -258,6 +291,7 @@ public void writePageHeaderAndDataIntoBuff(ByteBuffer data, PageHeader header) throw new org.apache.tsfile.exception.encrypt.EncryptException( Messages.get("error.write.aead_raw_page_copy_unsupported")); } + pageBuffer.beginPage(); // write the page header to pageBuffer try { logger.debug(Messages.get("log.write.page_header_flush_start"), pageBuffer.size()); @@ -276,11 +310,7 @@ public void writePageHeaderAndDataIntoBuff(ByteBuffer data, PageHeader header) } } else if (numOfPages == 1) { // put the firstPageStatistics into pageBuffer if (firstPageStatistics != null) { - byte[] b = pageBuffer.toByteArray(); - pageBuffer.reset(); - pageBuffer.write(b, 0, this.sizeWithoutStatistic); - firstPageStatistics.serialize(pageBuffer); - pageBuffer.write(b, this.sizeWithoutStatistic, b.length - this.sizeWithoutStatistic); + insertFirstPageStatistics(); } ReadWriteForEncodingUtils.writeUnsignedVarInt(header.getUncompressedSize(), pageBuffer); if (header.getUncompressedSize() != 0) { @@ -312,6 +342,7 @@ public void writePageHeaderAndDataIntoBuff(ByteBuffer data, PageHeader header) } catch (IOException e) { throw new PageException(e); } + pageBuffer.endPage(); } public void writeToFileWriter(TsFileIOWriter tsfileWriter) throws IOException { @@ -322,10 +353,16 @@ public void writeToFileWriter( TsFileIOWriter tsfileWriter, Function measurementNameRemapper) throws IOException { sealCurrentPage(); + compactPreWrittenFirstPageStatistics(); writeAllPagesOfChunkToTsFile(tsfileWriter, measurementNameRemapper); - // reinit this chunk writer + // reinit this chunk writer so that a recycled writer behaves like a fresh one: reset the page + // size check heuristic (and SDT last point), otherwise the first page of the next chunk could + // be sized differently. pageBuffer.reset(); + firstPageStatisticsWritten = false; + firstPageStatisticsSize = 0; + this.valueCountInOnePageForNextCheck = MINIMUM_RECORD_COUNT_FOR_CHECK; numOfPages = 0; sizeWithoutStatistic = 0; firstPageStatistics = null; @@ -405,6 +442,32 @@ public void clearPageWriter() { pageWriter = null; } + public void setExpectedPageCount(int expectedPageCount) { + this.expectedPageCount = Math.max(0, Math.min(expectedPageCount, MAX_PAGE_HINT)); + } + + /** + * Removes the first page statistics that were written up front when the chunk ends up with a + * single page. Shifts the page data in place, so no new array is allocated. + */ + private void compactPreWrittenFirstPageStatistics() { + if (!firstPageStatisticsWritten || numOfPages != 1) { + return; + } + byte[] firstPage = pageBuffer.getFirstPage(); + int dataStart = sizeWithoutStatistic + firstPageStatisticsSize; + int tail = firstPage.length - dataStart; + byte[] compacted = new byte[sizeWithoutStatistic + tail]; + System.arraycopy(firstPage, 0, compacted, 0, sizeWithoutStatistic); + System.arraycopy(firstPage, dataStart, compacted, sizeWithoutStatistic, tail); + pageBuffer.replaceFirstPage(compacted); + firstPageStatisticsWritten = false; + } + + public long getRetainedMemSize() { + return pageBuffer.retainedBytes() + pageWriter.retainedBufferBytes(); + } + public int getNumOfPages() { return numOfPages; } @@ -495,9 +558,16 @@ public CompressionType getCompressionType() { return compressionType; } - /** only used for test */ + /** + * @deprecated test-only snapshot of the page bytes; the returned buffer is not live. Use {@link + * #getByteBuffer()} when possible. + */ + @Deprecated public PublicBAOS getPageBuffer() { - return pageBuffer; + PublicBAOS copy = new PublicBAOS(); + byte[] bytes = pageBuffer.toByteArray(); + copy.write(bytes, 0, bytes.length); + return copy; } public boolean checkIsUnsealedPageOverThreshold(long size) { diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageBodyBuffer.java b/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageBodyBuffer.java new file mode 100644 index 000000000..083a92f46 --- /dev/null +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageBodyBuffer.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tsfile.write.page; + +import java.nio.ByteBuffer; + +/** + * Reusable scratch buffers for building one page body. They are owned by a page writer, are valid + * only until the page data has been copied, and must not be retained by callers. + */ +final class PageBodyBuffer { + + private ByteBuffer uncompressed; + private byte[] compressed; + + /** Returns a cleared heap buffer with at least {@code capacity} bytes. */ + ByteBuffer uncompressed(int capacity) { + if (uncompressed == null || uncompressed.capacity() < capacity) { + uncompressed = ByteBuffer.allocate(capacity); + } else { + uncompressed.clear(); + } + return uncompressed; + } + + /** Retained heap bytes of both scratch buffers. */ + long retainedBytes() { + return (uncompressed == null ? 0 : uncompressed.capacity()) + + (compressed == null ? 0 : compressed.length); + } + + /** Returns an output array with at least {@code capacity} bytes for the compressor. */ + byte[] compressed(int capacity) { + if (compressed == null || compressed.length < capacity) { + compressed = new byte[capacity]; + } + return compressed; + } +} diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageBodyEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageBodyEncoder.java index e86946b5e..31552bd57 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageBodyEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageBodyEncoder.java @@ -43,6 +43,24 @@ static ByteBuffer encode( int pageIndex, long chunkOrdinal) throws IOException { + return encode( + pageData, uncompressedSize, compressor, encryptParameter, pageIndex, chunkOrdinal, null); + } + + /** + * Encodes the page body, reusing the caller's compressor output buffer when it is large enough. + * The returned buffer (and the reusable arrays it may reference) is only valid until the caller + * has copied the page body; it must not be retained. + */ + static ByteBuffer encode( + ByteBuffer pageData, + int uncompressedSize, + ICompressor compressor, + EncryptParameter encryptParameter, + int pageIndex, + long chunkOrdinal, + PageBodyBuffer buffer) + throws IOException { byte[] plaintext; int plaintextOffset = 0; int compressedSize; @@ -56,7 +74,11 @@ static ByteBuffer encode( plaintext = compressor.compress(pageData.array(), inputOffset, uncompressedSize); compressedSize = plaintext.length; } else { - plaintext = new byte[compressor.getMaxBytesForCompression(uncompressedSize)]; + int maxBytesForCompression = compressor.getMaxBytesForCompression(uncompressedSize); + plaintext = + buffer == null + ? new byte[maxBytesForCompression] + : buffer.compressed(maxBytesForCompression); compressedSize = compressor.compress(pageData.array(), inputOffset, uncompressedSize, plaintext); } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageWriter.java b/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageWriter.java index ce840dfff..669ce5a35 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageWriter.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageWriter.java @@ -56,6 +56,10 @@ public class PageWriter { private Encoder valueEncoder; private PublicBAOS valueOut; + // Reused across the pages of a chunk so that encoding and compression do not allocate the page + // body buffers for every page. + private final PageBodyBuffer pageBodyBuffer = new PageBodyBuffer(); + /** * statistic of current page. It will be reset after calling {@code * writePageHeaderAndDataIntoBuff()} @@ -224,6 +228,26 @@ public ByteBuffer getUncompressedBytes() throws IOException { return buffer; } + /** Retained heap bytes of this page writer's buffers, for the global flush threshold. */ + public long retainedBufferBytes() { + return (long) timeOut.getBuf().length + + valueOut.getBuf().length + + pageBodyBuffer.retainedBytes(); + } + + /** Same as {@link #getUncompressedBytes()} but reuses the writer's scratch buffer. */ + private ByteBuffer getUncompressedBytesForWriting() throws IOException { + prepareEndWriteOnePage(); + int timeSize = timeOut.size(); + int valueSize = valueOut.size(); + ByteBuffer buffer = pageBodyBuffer.uncompressed(timeSize + valueSize + 4); + ReadWriteForEncodingUtils.writeUnsignedVarInt(timeSize, buffer); + buffer.put(timeOut.getBuf(), 0, timeSize); + buffer.put(valueOut.getBuf(), 0, valueSize); + buffer.flip(); + return buffer; + } + /** write the page header and data into the PageWriter's output stream. */ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first) throws IOException { @@ -233,15 +257,32 @@ 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 { + return writePageHeaderAndDataIntoBuff(pageBuffer, first, pageIndex, false); + } + + /** + * @param includeFirstPageStatistics when {@code first} is true, serialize the first page + * statistics after the page header. Multi-page chunks need them; writing them up front avoids + * copying the whole chunk buffer when the second page arrives. + */ + public int writePageHeaderAndDataIntoBuff( + PublicBAOS pageBuffer, boolean first, int pageIndex, boolean includeFirstPageStatistics) + throws IOException { if (statistics.getCount() == 0) { return 0; } - ByteBuffer pageData = getUncompressedBytes(); + ByteBuffer pageData = getUncompressedBytesForWriting(); int uncompressedSize = pageData.remaining(); ByteBuffer pageBody = PageBodyEncoder.encode( - pageData, uncompressedSize, compressor, encryptParam, pageIndex, chunkOrdinal); + pageData, + uncompressedSize, + compressor, + encryptParam, + pageIndex, + chunkOrdinal, + pageBodyBuffer); int pageBodySize = pageBody.remaining(); // write the page header to IOWriter @@ -251,6 +292,9 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, ReadWriteForEncodingUtils.writeUnsignedVarInt(uncompressedSize, pageBuffer); sizeWithoutStatistic += ReadWriteForEncodingUtils.writeUnsignedVarInt(pageBodySize, pageBuffer); + if (includeFirstPageStatistics) { + statistics.serialize(pageBuffer); + } } else { ReadWriteForEncodingUtils.writeUnsignedVarInt(uncompressedSize, pageBuffer); ReadWriteForEncodingUtils.writeUnsignedVarInt(pageBodySize, pageBuffer); @@ -273,6 +317,8 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, public long estimateMaxMemSize() { return timeOut.size() + valueOut.size() + + timeEncoder.getPendingByteCount() + + valueEncoder.getPendingByteCount() + timeEncoder.getMaxByteSize() + valueEncoder.getMaxByteSize(); } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/page/TimePageWriter.java b/java/tsfile/src/main/java/org/apache/tsfile/write/page/TimePageWriter.java index 6463ef51b..052557afa 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/page/TimePageWriter.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/page/TimePageWriter.java @@ -49,6 +49,10 @@ public class TimePageWriter { private Encoder timeEncoder; private final PublicBAOS timeOut; + // Reused across the pages of a chunk so that encoding and compression do not allocate the page + // body buffers for every page. + private final PageBodyBuffer pageBodyBuffer = new PageBodyBuffer(); + /** * statistic of current page. It will be reset after calling {@code * writePageHeaderAndDataIntoBuff()} @@ -107,6 +111,21 @@ public ByteBuffer getUncompressedBytes() throws IOException { return buffer; } + /** Retained heap bytes of this page writer's buffers, for the global flush threshold. */ + public long retainedBufferBytes() { + return (long) timeOut.getBuf().length + pageBodyBuffer.retainedBytes(); + } + + /** Same as {@link #getUncompressedBytes()} but reuses the writer's scratch buffer. */ + private ByteBuffer getUncompressedBytesForWriting() throws IOException { + prepareEndWriteOnePage(); + int timeSize = timeOut.size(); + ByteBuffer buffer = pageBodyBuffer.uncompressed(timeSize); + buffer.put(timeOut.getBuf(), 0, timeSize); + buffer.flip(); + return buffer; + } + /** write the page header and data into the PageWriter's output stream. */ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first) throws IOException { @@ -116,15 +135,27 @@ 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 { + return writePageHeaderAndDataIntoBuff(pageBuffer, first, pageIndex, false); + } + + public int writePageHeaderAndDataIntoBuff( + PublicBAOS pageBuffer, boolean first, int pageIndex, boolean includeFirstPageStatistics) + throws IOException { if (statistics.getCount() == 0) { return 0; } - ByteBuffer pageData = getUncompressedBytes(); + ByteBuffer pageData = getUncompressedBytesForWriting(); int uncompressedSize = pageData.remaining(); ByteBuffer pageBody = PageBodyEncoder.encode( - pageData, uncompressedSize, compressor, encryptParam, pageIndex, chunkOrdinal); + pageData, + uncompressedSize, + compressor, + encryptParam, + pageIndex, + chunkOrdinal, + pageBodyBuffer); int pageBodySize = pageBody.remaining(); // write the page header to IOWriter @@ -134,6 +165,9 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, ReadWriteForEncodingUtils.writeUnsignedVarInt(uncompressedSize, pageBuffer); sizeWithoutStatistic += ReadWriteForEncodingUtils.writeUnsignedVarInt(pageBodySize, pageBuffer); + if (includeFirstPageStatistics) { + statistics.serialize(pageBuffer); + } } else { ReadWriteForEncodingUtils.writeUnsignedVarInt(uncompressedSize, pageBuffer); ReadWriteForEncodingUtils.writeUnsignedVarInt(pageBodySize, pageBuffer); @@ -156,7 +190,7 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, * @return allocated size in time, value and outputStream */ public long estimateMaxMemSize() { - return timeOut.size() + timeEncoder.getMaxByteSize(); + return timeOut.size() + timeEncoder.getPendingByteCount() + timeEncoder.getMaxByteSize(); } /** reset this page */ diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/page/ValuePageWriter.java b/java/tsfile/src/main/java/org/apache/tsfile/write/page/ValuePageWriter.java index 16c101ee6..6dac0cd5b 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/page/ValuePageWriter.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/page/ValuePageWriter.java @@ -64,6 +64,10 @@ public class ValuePageWriter { private final PublicBAOS bitmapOut; + // Reused across the pages of a chunk so that encoding and compression do not allocate the page + // body buffers for every page. + private final PageBodyBuffer pageBodyBuffer = new PageBodyBuffer(); + private static final int MASK = 1 << 7; private static final byte[] NULL_BITMAP_BATCH = new byte[1024]; @@ -394,6 +398,26 @@ public ByteBuffer getUncompressedBytes() throws IOException { return buffer; } + /** Retained heap bytes of this page writer's buffers, for the global flush threshold. */ + public long retainedBufferBytes() { + return (long) bitmapOut.getBuf().length + + valueOut.getBuf().length + + pageBodyBuffer.retainedBytes(); + } + + /** Same as {@link #getUncompressedBytes()} but reuses the writer's scratch buffer. */ + private ByteBuffer getUncompressedBytesForWriting() throws IOException { + prepareEndWriteOnePage(); + int bitmapSize = bitmapOut.size(); + int valueSize = valueOut.size(); + ByteBuffer buffer = pageBodyBuffer.uncompressed(Integer.BYTES + bitmapSize + valueSize); + buffer.putInt(size); + buffer.put(bitmapOut.getBuf(), 0, bitmapSize); + buffer.put(valueOut.getBuf(), 0, valueSize); + buffer.flip(); + return buffer; + } + public int writeEmptyPageIntoBuff(PublicBAOS pageBuffer) { return ReadWriteForEncodingUtils.writeUnsignedVarInt(0, pageBuffer); } @@ -407,6 +431,12 @@ 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 { + return writePageHeaderAndDataIntoBuff(pageBuffer, first, pageIndex, false); + } + + public int writePageHeaderAndDataIntoBuff( + PublicBAOS pageBuffer, boolean first, int pageIndex, boolean includeFirstPageStatistics) + throws IOException { if (size == 0) { return 0; } else if (statistics.getCount() == 0) { @@ -414,11 +444,17 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, return writeEmptyPageIntoBuff(pageBuffer); } - ByteBuffer pageData = getUncompressedBytes(); + ByteBuffer pageData = getUncompressedBytesForWriting(); int uncompressedSize = pageData.remaining(); ByteBuffer pageBody = PageBodyEncoder.encode( - pageData, uncompressedSize, compressor, encryptParam, pageIndex, chunkOrdinal); + pageData, + uncompressedSize, + compressor, + encryptParam, + pageIndex, + chunkOrdinal, + pageBodyBuffer); int pageBodySize = pageBody.remaining(); // write the page header to IOWriter @@ -428,6 +464,9 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, ReadWriteForEncodingUtils.writeUnsignedVarInt(uncompressedSize, pageBuffer); sizeWithoutStatistic += ReadWriteForEncodingUtils.writeUnsignedVarInt(pageBodySize, pageBuffer); + if (includeFirstPageStatistics) { + statistics.serialize(pageBuffer); + } } else { ReadWriteForEncodingUtils.writeUnsignedVarInt(uncompressedSize, pageBuffer); ReadWriteForEncodingUtils.writeUnsignedVarInt(pageBodySize, pageBuffer); @@ -447,7 +486,12 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, * @return allocated size in time, value and outputStream */ public long estimateMaxMemSize() { - return Integer.BYTES + bitmapOut.size() + 1 + valueOut.size() + valueEncoder.getMaxByteSize(); + return Integer.BYTES + + bitmapOut.size() + + 1 + + valueOut.size() + + valueEncoder.getPendingByteCount() + + valueEncoder.getMaxByteSize(); } /** reset this page */ diff --git a/java/tsfile/src/test/java/org/apache/tsfile/write/chunk/ChunkPageBufferTest.java b/java/tsfile/src/test/java/org/apache/tsfile/write/chunk/ChunkPageBufferTest.java new file mode 100644 index 000000000..ca79d0575 --- /dev/null +++ b/java/tsfile/src/test/java/org/apache/tsfile/write/chunk/ChunkPageBufferTest.java @@ -0,0 +1,140 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tsfile.write.chunk; + +import org.junit.Assert; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.util.Arrays; + +public class ChunkPageBufferTest { + + @Test + public void testPagesAreStoredExactlyAndWrittenInOrder() throws Exception { + ChunkPageBuffer buffer = new ChunkPageBuffer(); + byte[] page1 = "abc".getBytes("UTF-8"); + byte[] page2 = new byte[1000]; + for (int i = 0; i < page2.length; i++) { + page2[i] = (byte) i; + } + + buffer.beginPage(); + buffer.write(page1, 0, page1.length); + buffer.endPage(); + + buffer.beginPage(); + buffer.write(page2, 0, page2.length); + buffer.endPage(); + + Assert.assertEquals(page1.length + page2.length, buffer.size()); + Assert.assertEquals(2, buffer.getPageCount()); + Assert.assertArrayEquals(page1, buffer.getFirstPage()); + + byte[] expected = new byte[page1.length + page2.length]; + System.arraycopy(page1, 0, expected, 0, page1.length); + System.arraycopy(page2, 0, expected, page1.length, page2.length); + Assert.assertArrayEquals(expected, buffer.toByteArray()); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + buffer.writeTo(out); + Assert.assertArrayEquals(expected, out.toByteArray()); + } + + @Test + public void testReplaceAndTruncateFirstPage() { + ChunkPageBuffer buffer = new ChunkPageBuffer(); + buffer.beginPage(); + buffer.write(new byte[] {1, 2, 3, 4, 5}, 0, 5); + buffer.endPage(); + + buffer.replaceFirstPage(new byte[] {1, 2, 3}); + Assert.assertEquals(3, buffer.size()); + Assert.assertArrayEquals(new byte[] {1, 2, 3}, buffer.getFirstPage()); + + buffer.truncateFirstPage(2); + Assert.assertEquals(2, buffer.size()); + Assert.assertArrayEquals(new byte[] {1, 2}, buffer.toByteArray()); + } + + @Test + public void testResetKeepsScratchButReleaseDropsIt() { + ChunkPageBuffer buffer = new ChunkPageBuffer(); + buffer.beginPage(); + buffer.write(new byte[4096], 0, 4096); + buffer.endPage(); + long retainedAfterPage = buffer.retainedBytes(); + + buffer.reset(); + Assert.assertEquals(0, buffer.size()); + Assert.assertEquals(0, buffer.getPageCount()); + // reset keeps the page scratch for reuse + Assert.assertTrue(buffer.retainedBytes() <= retainedAfterPage); + + buffer.release(); + Assert.assertEquals(0, buffer.size()); + Assert.assertEquals(0, buffer.getPageCount()); + Assert.assertTrue(buffer.retainedBytes() < 4096); + } + + @Test + public void testResetPoolsSlabsForReuse() { + ChunkPageBuffer buffer = new ChunkPageBuffer(); + writeTwoPages(buffer, 10_000); + buffer.reset(); + long retainedAfterFirstChunk = buffer.retainedBytes(); + + writeTwoPages(buffer, 10_000); + buffer.reset(); + long retainedAfterSecondChunk = buffer.retainedBytes(); + + Assert.assertEquals(0, buffer.size()); + Assert.assertTrue(retainedAfterFirstChunk > 0); + Assert.assertTrue(retainedAfterSecondChunk <= retainedAfterFirstChunk + 4096); + } + + private static void writeTwoPages(ChunkPageBuffer buffer, int pageSize) { + byte[] page = new byte[pageSize]; + for (int i = 0; i < page.length; i++) { + page[i] = (byte) i; + } + buffer.beginPage(); + buffer.write(page, 0, page.length); + buffer.endPage(); + buffer.beginPage(); + buffer.write(page, 0, page.length); + buffer.endPage(); + } + + @Test + public void testRetainedBytesEqualsPagesPlusCurrentScratch() { + ChunkPageBuffer buffer = new ChunkPageBuffer(); + buffer.beginPage(); + buffer.write(new byte[300], 0, 300); + buffer.endPage(); + buffer.beginPage(); + buffer.write(new byte[100], 0, 100); + + // 300 stored page bytes + the first page scratch; blocks have at most one small block of slack + Assert.assertTrue(buffer.retainedBytes() >= 400); + Assert.assertTrue(buffer.retainedBytes() < 16 * 1024); + Assert.assertEquals(400, buffer.size()); + Assert.assertTrue(Arrays.equals(buffer.toByteArray(), buffer.toByteArray())); + } +}