You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2021/10/30 01:50:33 UTC

[GitHub] [pinot] Jackie-Jiang commented on a change in pull request #7661: implement size balanced V4 raw chunk format

Jackie-Jiang commented on a change in pull request #7661:
URL: https://github.com/apache/pinot/pull/7661#discussion_r739593162



##########
File path: pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/writer/impl/VarByteChunkSVForwardIndexWriterV4.java
##########
@@ -0,0 +1,222 @@
+/**
+ * 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.pinot.segment.local.io.writer.impl;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.channels.FileChannel;
+import java.nio.charset.StandardCharsets;
+import javax.annotation.concurrent.NotThreadSafe;
+import org.apache.commons.io.FileUtils;
+import org.apache.pinot.segment.local.io.compression.ChunkCompressorFactory;
+import org.apache.pinot.segment.spi.compression.ChunkCompressionType;
+import org.apache.pinot.segment.spi.compression.ChunkCompressor;
+import org.apache.pinot.segment.spi.memory.CleanerUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Class to write out variable length bytes into a single column.
+ *
+ *
+ * Only sequential writes are supported.
+ */
+@NotThreadSafe
+public class VarByteChunkSVForwardIndexWriterV4 implements VarByteChunkWriter {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(VarByteChunkSVForwardIndexWriterV4.class);
+
+  public static final int VERSION = 4;
+
+  private static final String DATA_BUFFER_SUFFIX = ".buf";
+
+  private final File _dataBuffer;
+  private final RandomAccessFile _output;
+  private final FileChannel _dataChannel;
+  private final ByteBuffer _chunkBuffer;
+  private final ChunkCompressor _chunkCompressor;
+
+  private int _docIdOffset = 0;
+  private int _nextDocId = 0;
+  private int _metadataSize = 0;
+  private long _chunkOffset = 0;
+
+  public VarByteChunkSVForwardIndexWriterV4(File file, ChunkCompressionType compressionType, int chunkSize)
+      throws IOException {
+    _dataBuffer = new File(file.getName() + DATA_BUFFER_SUFFIX);
+    _output = new RandomAccessFile(file, "rw");
+    _dataChannel = new RandomAccessFile(_dataBuffer, "rw").getChannel();
+    _chunkCompressor = ChunkCompressorFactory.getCompressor(compressionType, true);
+    _chunkBuffer = ByteBuffer.allocateDirect(chunkSize).order(ByteOrder.LITTLE_ENDIAN);
+    // reserve space for numDocs
+    _chunkBuffer.position(Integer.BYTES);
+    writeHeader(_chunkCompressor.compressionType(), chunkSize);
+  }
+
+  private void writeHeader(ChunkCompressionType compressionType, int targetDecompressedChunkSize)
+      throws IOException {
+    // keep metadata BE for backwards compatibility
+    // (e.g. the version needs to be read by a factory which assumes BE)
+    _output.writeInt(VERSION);
+    _output.writeInt(targetDecompressedChunkSize);
+    _output.writeInt(compressionType.getValue());
+    // reserve a slot to write the data offset into
+    _output.writeInt(0);
+    _metadataSize += 4 * Integer.BYTES;
+  }
+
+  @Override
+  public void putString(String string) {
+    putBytes(string.getBytes(StandardCharsets.UTF_8));
+  }
+
+  @Override
+  public void putBytes(byte[] bytes) {
+    int sizeRequired = Integer.BYTES + bytes.length;
+    if (_chunkBuffer.position() >= _chunkBuffer.capacity() - sizeRequired) {
+      writeChunk();
+      if (sizeRequired >= _chunkBuffer.capacity() - Integer.BYTES) {
+        writeHugeChunk(bytes);
+        return;
+      }
+    }
+    _chunkBuffer.putInt(bytes.length);
+    _chunkBuffer.put(bytes);
+    _nextDocId++;
+  }
+
+  private void writeHugeChunk(byte[] bytes) {
+    // huge values where the bytes and their length prefix don't fit in to the remainder of the buffer after the prefix
+    // for the number of documents in a regular chunk are written as a single value without metadata, and these chunks
+    // are detected by marking the MSB in the doc id offset
+    final ByteBuffer buffer;
+    if (_chunkCompressor.compressionType() == ChunkCompressionType.SNAPPY
+        || _chunkCompressor.compressionType() == ChunkCompressionType.ZSTANDARD) {
+      buffer = ByteBuffer.allocateDirect(bytes.length);
+      buffer.put(bytes);
+      buffer.flip();
+    } else {
+      // cast for JDK8 javac compatibility
+      buffer = (ByteBuffer) ByteBuffer.wrap(bytes);
+    }
+    try {
+      _nextDocId++;
+      write(buffer, true);
+    } finally {
+      CleanerUtil.cleanQuietly(buffer);
+    }
+  }
+
+  private void writeChunk() {
+    if (_nextDocId > _docIdOffset) {
+      int numDocs = _nextDocId - _docIdOffset;
+      // collect lengths
+      int[] valueLengths = new int[numDocs];
+      int[] offsets = new int[numDocs];
+      int offset = Integer.BYTES;
+      for (int i = 0; i < numDocs; i++) {
+        offsets[i] = offset;
+        int size = _chunkBuffer.getInt(offset);
+        valueLengths[i] = size;
+        offset += size + Integer.BYTES;
+      }
+      _chunkBuffer.putInt(0, numDocs);
+      // now iterate backwards shifting variable length content backwards to make space for prefixes at the start
+      // this pays for itself by allowing random access to readers
+      int limit = _chunkBuffer.position();
+      int accumulatedOffset = Integer.BYTES;
+      for (int i = numDocs - 2; i >= 0; i--) {
+        ByteBuffer source = _chunkBuffer.duplicate();
+        int copyFrom = offsets[i] + Integer.BYTES;
+        source.position(offsets[i]).limit(copyFrom + valueLengths[i]);
+        _chunkBuffer.position(offsets[i] + accumulatedOffset);
+        _chunkBuffer.put(source.slice());
+        accumulatedOffset += Integer.BYTES;
+      }
+      // compute byte offsets of each string from lengths
+      int metadataOffset = Integer.BYTES * (numDocs + 1);
+      offsets[0] = metadataOffset;
+      int cumulativeLength = valueLengths[0];
+      for (int i = 1; i < offsets.length; i++) {
+        offsets[i] = metadataOffset + cumulativeLength;
+        cumulativeLength += valueLengths[i];
+      }
+      // write the lengths into the space created at the front
+      for (int i = 0; i < offsets.length; i++) {
+        _chunkBuffer.putInt(Integer.BYTES * (i + 1), offsets[i]);
+      }
+      _chunkBuffer.position(0);
+      _chunkBuffer.limit(limit);
+      write(_chunkBuffer, false);
+      clearChunkBuffer();
+    }
+  }
+
+  private void write(ByteBuffer buffer, boolean huge) {
+    int maxCompressedSize = _chunkCompressor.maxCompressedSize(buffer.limit());
+    ByteBuffer target = null;
+    try {
+      target = _dataChannel.map(FileChannel.MapMode.READ_WRITE, _chunkOffset, maxCompressedSize)
+          .order(ByteOrder.LITTLE_ENDIAN);
+      int compressedSize = _chunkCompressor.compress(buffer, target);
+      // reverse bytes here because the file writes BE and we want to read the metadata LE
+      _output.writeInt(Integer.reverseBytes(_docIdOffset | (huge ? 0x80000000 : 0)));
+      _output.writeInt(Integer.reverseBytes((int) (_chunkOffset & 0xFFFFFFFFL)));
+      _metadataSize += 8;
+      _chunkOffset += compressedSize;

Review comment:
       Here we should check if we already put larger than 4G data and throw exception




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org