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/03/24 23:26:51 UTC

[GitHub] [incubator-pinot] Jackie-Jiang commented on a change in pull request #6710: Add a trailer section to data table and measure data table serialization cost on server

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



##########
File path: pinot-core/src/main/java/org/apache/pinot/core/common/datatable/DataTableImplV3.java
##########
@@ -0,0 +1,702 @@
+/**
+ * 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.core.common.datatable;
+
+import com.google.common.primitives.Ints;
+import com.google.common.primitives.Longs;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.TreeMap;
+import org.apache.pinot.common.response.ProcessingException;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.common.utils.DataTable;
+import org.apache.pinot.common.utils.StringUtil;
+import org.apache.pinot.core.common.ObjectSerDeUtils;
+import org.apache.pinot.core.query.request.context.ThreadTimer;
+import org.apache.pinot.spi.utils.ByteArray;
+import org.apache.pinot.spi.utils.BytesUtils;
+
+import static org.apache.pinot.core.common.datatable.DataTableUtils.*;
+
+
+public class DataTableImplV3 implements DataTable {
+  private static final int VERSION = 3;
+
+  // VERSION
+  // NUM_ROWS
+  // NUM_COLUMNS
+  // EXCEPTIONS (START|SIZE)
+  // DICTIONARY_MAP (START|SIZE)
+  // DATA_SCHEMA (START|SIZE)
+  // FIXED_SIZE_DATA (START|SIZE)
+  // VARIABLE_SIZE_DATA (START|SIZE)
+  // TRAILER (START|SIZE)
+  private static final int HEADER_SIZE = Integer.BYTES * 15;
+
+  private final int _numRows;
+  private final int _numColumns;
+  private final DataSchema _dataSchema;
+  private final int[] _columnOffsets;
+  private final int _rowSizeInBytes;
+  private final Map<String, Map<Integer, String>> _dictionaryMap;
+  private final byte[] _fixedSizeDataBytes;
+  private final ByteBuffer _fixedSizeData;
+  private final byte[] _variableSizeDataBytes;
+  private final ByteBuffer _variableSizeData;
+  // _exceptions stores exceptions as a map of errorCode->errorMessage
+  private final Map<Integer, String> _exceptions;
+  /**
+   * _metadata stores KV pairs for metadata. Metadata is actually a part of _trailer in V3 when serialize DataTable
+   * into bytes. When deserialize, we extract metadata from _trailer into this _metadata map to provide the same
+   * interface with V2. There are many code use
+   * datatable.getMetadata().get("key")/datatable.getMetadata().put("key", "value") to get/set metadata.
+   * TODO(@mqliang): revise this if we decide to get/set metadata by
+   *  datable.getTailerData(key)/datable.setTailer(key, value).
+   */
+  private final Map<String, String> _metadata;
+  private Map<TrailerKeys, String> _trailer;
+
+  private long _responseSerializationCpuTimeNs;
+  private int _responseSerializationCpuTimeNsValueOffset;
+
+  /**
+   * Construct data table with results. (Server side)
+   */
+  public DataTableImplV3(int numRows, DataSchema dataSchema, Map<String, Map<Integer, String>> dictionaryMap,
+      byte[] fixedSizeDataBytes, byte[] variableSizeDataBytes) {
+    _numRows = numRows;
+    _numColumns = dataSchema.size();
+    _dataSchema = dataSchema;
+    _columnOffsets = new int[_numColumns];
+    _rowSizeInBytes = DataTableUtils.computeColumnOffsets(dataSchema, _columnOffsets);
+    _dictionaryMap = dictionaryMap;
+    _fixedSizeDataBytes = fixedSizeDataBytes;
+    _fixedSizeData = ByteBuffer.wrap(fixedSizeDataBytes);
+    _variableSizeDataBytes = variableSizeDataBytes;
+    _variableSizeData = ByteBuffer.wrap(variableSizeDataBytes);
+    _exceptions = new HashMap<>();
+    _metadata = new HashMap<>();
+    _trailer = new TreeMap<>();
+  }
+
+  /**
+   * Construct empty data table. (Server side)
+   */
+  public DataTableImplV3() {
+    _numRows = 0;
+    _numColumns = 0;
+    _dataSchema = null;
+    _columnOffsets = null;
+    _rowSizeInBytes = 0;
+    _dictionaryMap = null;
+    _fixedSizeDataBytes = null;
+    _fixedSizeData = null;
+    _variableSizeDataBytes = null;
+    _variableSizeData = null;
+    _exceptions = new HashMap<>();
+    _metadata = new HashMap<>();
+    _trailer = new TreeMap<>();
+  }
+
+  @Override
+  public void addException(ProcessingException processingException) {
+    _exceptions.put(processingException.getErrorCode(), processingException.getMessage());
+  }
+
+  @Override
+  public Map<Integer, String> getExceptions() {
+    return _exceptions;
+  }
+
+  @Override
+  public byte[] toBytes()
+      throws IOException {
+    _trailer.put(TrailerKeys.RESPONSE_SERIALIZATION_CPU_TIME_NS_METADATA_KEY, String.valueOf(-1));
+    ThreadTimer threadTimer = new ThreadTimer();
+    threadTimer.start();
+    byte[] bytes = toBytesInternal();
+    _responseSerializationCpuTimeNs = threadTimer.stopAndGetThreadTimeNs();
+    // Replace the value of "responseSerializationCpuTimeNs" as actual value
+    System.arraycopy(Longs.toByteArray(_responseSerializationCpuTimeNs), 0, bytes,
+        _responseSerializationCpuTimeNsValueOffset, Long.BYTES);
+    return bytes;
+  }
+
+  private byte[] toBytesInternal()
+      throws IOException {
+    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+    DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
+    dataOutputStream.writeInt(VERSION);
+    dataOutputStream.writeInt(_numRows);
+    dataOutputStream.writeInt(_numColumns);
+    int dataOffset = HEADER_SIZE;
+
+    // Write exceptions (START|SIZE).
+    dataOutputStream.writeInt(dataOffset);
+    byte[] exceptionsBytes;
+    exceptionsBytes = serializeExceptions();
+    dataOutputStream.writeInt(exceptionsBytes.length);
+    dataOffset += exceptionsBytes.length;
+
+    // Write dictionary (START|SIZE).
+    dataOutputStream.writeInt(dataOffset);
+    byte[] dictionaryMapBytes = null;
+    if (_dictionaryMap != null) {
+      dictionaryMapBytes = serializeDictionaryMap(_dictionaryMap);
+      dataOutputStream.writeInt(dictionaryMapBytes.length);
+      dataOffset += dictionaryMapBytes.length;
+    } else {
+      dataOutputStream.writeInt(0);
+    }
+
+    // Write data schema (START|SIZE).
+    dataOutputStream.writeInt(dataOffset);
+    byte[] dataSchemaBytes = null;
+    if (_dataSchema != null) {
+      dataSchemaBytes = _dataSchema.toBytes();
+      dataOutputStream.writeInt(dataSchemaBytes.length);
+      dataOffset += dataSchemaBytes.length;
+    } else {
+      dataOutputStream.writeInt(0);
+    }
+
+    // Write fixed size data (START|SIZE).
+    dataOutputStream.writeInt(dataOffset);
+    if (_fixedSizeDataBytes != null) {
+      dataOutputStream.writeInt(_fixedSizeDataBytes.length);
+      dataOffset += _fixedSizeDataBytes.length;
+    } else {
+      dataOutputStream.writeInt(0);
+    }
+
+    // Write variable size data (START|SIZE).
+    dataOutputStream.writeInt(dataOffset);
+    if (_variableSizeDataBytes != null) {
+      dataOutputStream.writeInt(_variableSizeDataBytes.length);
+      dataOffset += _variableSizeDataBytes.length;
+    } else {
+      dataOutputStream.writeInt(0);
+    }
+
+    // Write trailer data (START|SIZE).
+    dataOutputStream.writeInt(dataOffset);
+    // Put all meta data into trailer.
+    _trailer = putAllMetaDataIntoTrailer();
+    _responseSerializationCpuTimeNsValueOffset = dataOffset;
+    byte[] trailerBytes = serializeTrailer();
+    dataOutputStream.writeInt(trailerBytes.length);
+
+    // Write actual data.
+    dataOutputStream.write(exceptionsBytes);
+    if (dictionaryMapBytes != null) {
+      dataOutputStream.write(dictionaryMapBytes);
+    }
+    if (dataSchemaBytes != null) {
+      dataOutputStream.write(dataSchemaBytes);
+    }
+    if (_fixedSizeDataBytes != null) {
+      dataOutputStream.write(_fixedSizeDataBytes);
+    }
+    if (_variableSizeDataBytes != null) {
+      dataOutputStream.write(_variableSizeDataBytes);
+    }
+    dataOutputStream.write(trailerBytes);
+
+    return byteArrayOutputStream.toByteArray();
+  }
+
+  /**
+   * Construct data table from byte array. (broker side)
+   */
+  public DataTableImplV3(ByteBuffer byteBuffer)
+      throws IOException {
+    // Read header.
+    _numRows = byteBuffer.getInt();
+    _numColumns = byteBuffer.getInt();
+    int exceptionsStart = byteBuffer.getInt();
+    int exceptionsLength = byteBuffer.getInt();
+    int dictionaryMapStart = byteBuffer.getInt();
+    int dictionaryMapLength = byteBuffer.getInt();
+    int dataSchemaStart = byteBuffer.getInt();
+    int dataSchemaLength = byteBuffer.getInt();
+    int fixedSizeDataStart = byteBuffer.getInt();
+    int fixedSizeDataLength = byteBuffer.getInt();
+    int variableSizeDataStart = byteBuffer.getInt();
+    int variableSizeDataLength = byteBuffer.getInt();
+    int trailerStart = byteBuffer.getInt();
+    int trailerLength = byteBuffer.getInt();
+
+    // Read exceptions.
+    if (exceptionsLength != 0) {
+      byte[] exceptionsBytes = new byte[exceptionsLength];
+      byteBuffer.position(exceptionsStart);
+      byteBuffer.get(exceptionsBytes);
+      _exceptions = deserializeExceptions(exceptionsBytes);
+    } else {
+      _exceptions = new HashMap<>();
+    }
+
+    // Read dictionary.
+    if (dictionaryMapLength != 0) {
+      byte[] dictionaryMapBytes = new byte[dictionaryMapLength];
+      byteBuffer.position(dictionaryMapStart);
+      byteBuffer.get(dictionaryMapBytes);
+      _dictionaryMap = deserializeDictionaryMap(dictionaryMapBytes);
+    } else {
+      _dictionaryMap = null;
+    }
+
+    // Read data schema.
+    if (dataSchemaLength != 0) {
+      byte[] schemaBytes = new byte[dataSchemaLength];
+      byteBuffer.position(dataSchemaStart);
+      byteBuffer.get(schemaBytes);
+      _dataSchema = DataSchema.fromBytes(schemaBytes);
+      _columnOffsets = new int[_dataSchema.size()];
+      _rowSizeInBytes = DataTableUtils.computeColumnOffsets(_dataSchema, _columnOffsets);
+    } else {
+      _dataSchema = null;
+      _columnOffsets = null;
+      _rowSizeInBytes = 0;
+    }
+
+    // Read fixed size data.
+    if (fixedSizeDataLength != 0) {
+      _fixedSizeDataBytes = new byte[fixedSizeDataLength];
+      byteBuffer.position(fixedSizeDataStart);
+      byteBuffer.get(_fixedSizeDataBytes);
+      _fixedSizeData = ByteBuffer.wrap(_fixedSizeDataBytes);
+    } else {
+      _fixedSizeDataBytes = null;
+      _fixedSizeData = null;
+    }
+
+    // Read variable size data.
+    if (variableSizeDataLength != 0) {
+      _variableSizeDataBytes = new byte[variableSizeDataLength];
+      byteBuffer.position(variableSizeDataStart);
+      byteBuffer.get(_variableSizeDataBytes);
+      _variableSizeData = ByteBuffer.wrap(_variableSizeDataBytes);
+    } else {
+      _variableSizeDataBytes = null;
+      _variableSizeData = null;
+    }
+
+    // Read trailer.
+    byte[] trailerBytes = new byte[trailerLength];
+    byteBuffer.position(trailerStart);
+    byteBuffer.get(trailerBytes);
+    _trailer = deserializeTrailer(trailerBytes);
+
+    /**
+     * Extract metadata from trailer.
+     * Metadata is actually a part of _trailer in V3 when serialize DataTable into bytes. When deserialize,
+     * we extract metadata from _trailer into this _metadata map to provide the same interface with V2.
+     * */
+    _metadata = extractMetadataFormTrailer();
+  }
+
+  /**
+   * Construct data table from V2 byte array. (broker side)
+   */
+  public DataTableImplV3(ByteBuffer byteBuffer, boolean isV2)

Review comment:
       It might be cleaner if you add a data table v2 to v3 converter instead of constructing v3 directly from v2 buffer




-- 
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.

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