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/19 04:33:32 UTC

[GitHub] [incubator-pinot] jackjlli commented on a change in pull request #6479: Support data ingestion for generating offline segment in one pass

jackjlli commented on a change in pull request #6479:
URL: https://github.com/apache/incubator-pinot/pull/6479#discussion_r597400849



##########
File path: pinot-core/src/main/java/org/apache/pinot/core/indexsegment/mutable/IntermediateSegment.java
##########
@@ -0,0 +1,375 @@
+/**
+ * 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.indexsegment.mutable;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import javax.annotation.Nullable;
+import org.apache.pinot.core.common.DataSource;
+import org.apache.pinot.core.data.partition.PartitionFunction;
+import org.apache.pinot.core.data.partition.PartitionFunctionFactory;
+import org.apache.pinot.core.indexsegment.generator.SegmentGeneratorConfig;
+import org.apache.pinot.core.io.readerwriter.PinotDataBufferMemoryManager;
+import org.apache.pinot.core.io.writer.impl.DirectMemoryManager;
+import org.apache.pinot.core.io.writer.impl.MmapMemoryManager;
+import org.apache.pinot.core.realtime.impl.dictionary.MutableDictionaryFactory;
+import org.apache.pinot.core.realtime.impl.forward.FixedByteMVMutableForwardIndex;
+import org.apache.pinot.core.realtime.impl.forward.FixedByteSVMutableForwardIndex;
+import org.apache.pinot.core.segment.creator.impl.V1Constants;
+import org.apache.pinot.core.segment.index.column.IntermediateIndexContainer;
+import org.apache.pinot.core.segment.index.column.NumValuesInfo;
+import org.apache.pinot.core.segment.index.loader.IndexLoadingConfig;
+import org.apache.pinot.core.segment.index.metadata.SegmentMetadata;
+import org.apache.pinot.core.segment.index.readers.MutableDictionary;
+import org.apache.pinot.core.segment.index.readers.MutableForwardIndex;
+import org.apache.pinot.core.segment.index.readers.ValidDocIndexReader;
+import org.apache.pinot.core.startree.v2.StarTreeV2;
+import org.apache.pinot.spi.config.table.ColumnPartitionConfig;
+import org.apache.pinot.spi.config.table.SegmentPartitionConfig;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+import org.apache.pinot.spi.stream.RowMetadata;
+import org.apache.pinot.spi.utils.ByteArray;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Intermediate segment format to store the collected data so far. This segment format will be used to generate the final
+ * offline segment in SegmentIndexCreationDriver.
+ */
+public class IntermediateSegment implements MutableSegment {
+  private static final Logger LOGGER = LoggerFactory.getLogger(IntermediateSegment.class);
+
+  private static final int MAX_MULTI_VALUES_PER_ROW = 1000;
+  private static final int DEFAULT_CAPACITY = 100_000;
+  private static final int DEFAULT_EST_AVG_COL_SIZE = 32;
+  private static final int DEFAULT_EST_CARDINALITY = 5000;
+
+  private final SegmentGeneratorConfig _segmentGeneratorConfig;
+  private final Schema _schema;
+  private final TableConfig _tableConfig;
+  private final String _segmentName;
+  private final PartitionFunction _partitionFunction;
+  private final String _partitionColumn;
+  private final Map<String, IntermediateIndexContainer> _indexContainerMap = new HashMap<>();
+  private final PinotDataBufferMemoryManager _memoryManager;
+
+  private final int _capacity = DEFAULT_CAPACITY;
+  private volatile int _numDocsIndexed = 0;
+
+  public IntermediateSegment(SegmentGeneratorConfig segmentGeneratorConfig) {
+    _segmentGeneratorConfig = segmentGeneratorConfig;
+    _schema = segmentGeneratorConfig.getSchema();
+    _tableConfig = segmentGeneratorConfig.getTableConfig();
+    _segmentName = _segmentGeneratorConfig.getSegmentName();
+
+    Collection<FieldSpec> allFieldSpecs = _schema.getAllFieldSpecs();
+    List<FieldSpec> physicalFieldSpecs = new ArrayList<>(allFieldSpecs.size());
+    physicalFieldSpecs.addAll(allFieldSpecs);
+    Collection<FieldSpec> physicalFieldSpecs1 = Collections.unmodifiableCollection(physicalFieldSpecs);
+
+    SegmentPartitionConfig segmentPartitionConfig = segmentGeneratorConfig.getSegmentPartitionConfig();
+    if (segmentPartitionConfig != null) {
+      Map<String, ColumnPartitionConfig> segmentPartitionConfigColumnPartitionMap =
+          segmentPartitionConfig.getColumnPartitionMap();
+      _partitionColumn = segmentPartitionConfigColumnPartitionMap.keySet().iterator().next();
+      _partitionFunction = PartitionFunctionFactory
+          .getPartitionFunction(segmentPartitionConfig.getFunctionName(_partitionColumn),
+              segmentPartitionConfig.getNumPartitions(_partitionColumn));
+    } else {
+      _partitionColumn = null;
+      _partitionFunction = null;
+    }
+
+    IndexLoadingConfig indexLoadingConfig = new IndexLoadingConfig(null, _tableConfig);
+    boolean offHeap = indexLoadingConfig.isRealtimeOffHeapAllocation();

Review comment:
       Updated to straightly use offheap

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/indexsegment/mutable/IntermediateSegment.java
##########
@@ -0,0 +1,375 @@
+/**
+ * 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.indexsegment.mutable;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import javax.annotation.Nullable;
+import org.apache.pinot.core.common.DataSource;
+import org.apache.pinot.core.data.partition.PartitionFunction;
+import org.apache.pinot.core.data.partition.PartitionFunctionFactory;
+import org.apache.pinot.core.indexsegment.generator.SegmentGeneratorConfig;
+import org.apache.pinot.core.io.readerwriter.PinotDataBufferMemoryManager;
+import org.apache.pinot.core.io.writer.impl.DirectMemoryManager;
+import org.apache.pinot.core.io.writer.impl.MmapMemoryManager;
+import org.apache.pinot.core.realtime.impl.dictionary.MutableDictionaryFactory;
+import org.apache.pinot.core.realtime.impl.forward.FixedByteMVMutableForwardIndex;
+import org.apache.pinot.core.realtime.impl.forward.FixedByteSVMutableForwardIndex;
+import org.apache.pinot.core.segment.creator.impl.V1Constants;
+import org.apache.pinot.core.segment.index.column.IntermediateIndexContainer;
+import org.apache.pinot.core.segment.index.column.NumValuesInfo;
+import org.apache.pinot.core.segment.index.loader.IndexLoadingConfig;
+import org.apache.pinot.core.segment.index.metadata.SegmentMetadata;
+import org.apache.pinot.core.segment.index.readers.MutableDictionary;
+import org.apache.pinot.core.segment.index.readers.MutableForwardIndex;
+import org.apache.pinot.core.segment.index.readers.ValidDocIndexReader;
+import org.apache.pinot.core.startree.v2.StarTreeV2;
+import org.apache.pinot.spi.config.table.ColumnPartitionConfig;
+import org.apache.pinot.spi.config.table.SegmentPartitionConfig;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+import org.apache.pinot.spi.stream.RowMetadata;
+import org.apache.pinot.spi.utils.ByteArray;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Intermediate segment format to store the collected data so far. This segment format will be used to generate the final
+ * offline segment in SegmentIndexCreationDriver.
+ */
+public class IntermediateSegment implements MutableSegment {
+  private static final Logger LOGGER = LoggerFactory.getLogger(IntermediateSegment.class);
+
+  private static final int MAX_MULTI_VALUES_PER_ROW = 1000;
+  private static final int DEFAULT_CAPACITY = 100_000;
+  private static final int DEFAULT_EST_AVG_COL_SIZE = 32;
+  private static final int DEFAULT_EST_CARDINALITY = 5000;
+
+  private final SegmentGeneratorConfig _segmentGeneratorConfig;
+  private final Schema _schema;
+  private final TableConfig _tableConfig;
+  private final String _segmentName;
+  private final PartitionFunction _partitionFunction;
+  private final String _partitionColumn;
+  private final Map<String, IntermediateIndexContainer> _indexContainerMap = new HashMap<>();
+  private final PinotDataBufferMemoryManager _memoryManager;
+
+  private final int _capacity = DEFAULT_CAPACITY;
+  private volatile int _numDocsIndexed = 0;
+
+  public IntermediateSegment(SegmentGeneratorConfig segmentGeneratorConfig) {
+    _segmentGeneratorConfig = segmentGeneratorConfig;
+    _schema = segmentGeneratorConfig.getSchema();
+    _tableConfig = segmentGeneratorConfig.getTableConfig();
+    _segmentName = _segmentGeneratorConfig.getSegmentName();
+
+    Collection<FieldSpec> allFieldSpecs = _schema.getAllFieldSpecs();
+    List<FieldSpec> physicalFieldSpecs = new ArrayList<>(allFieldSpecs.size());
+    physicalFieldSpecs.addAll(allFieldSpecs);
+    Collection<FieldSpec> physicalFieldSpecs1 = Collections.unmodifiableCollection(physicalFieldSpecs);
+
+    SegmentPartitionConfig segmentPartitionConfig = segmentGeneratorConfig.getSegmentPartitionConfig();
+    if (segmentPartitionConfig != null) {
+      Map<String, ColumnPartitionConfig> segmentPartitionConfigColumnPartitionMap =
+          segmentPartitionConfig.getColumnPartitionMap();
+      _partitionColumn = segmentPartitionConfigColumnPartitionMap.keySet().iterator().next();
+      _partitionFunction = PartitionFunctionFactory
+          .getPartitionFunction(segmentPartitionConfig.getFunctionName(_partitionColumn),
+              segmentPartitionConfig.getNumPartitions(_partitionColumn));
+    } else {
+      _partitionColumn = null;
+      _partitionFunction = null;
+    }
+
+    IndexLoadingConfig indexLoadingConfig = new IndexLoadingConfig(null, _tableConfig);
+    boolean offHeap = indexLoadingConfig.isRealtimeOffHeapAllocation();
+    boolean directOffHeap = indexLoadingConfig.isDirectRealtimeOffHeapAllocation();
+    if (offHeap && !directOffHeap) {
+      _memoryManager = new MmapMemoryManager(null, _segmentName, null);
+    } else {
+      _memoryManager = new DirectMemoryManager(_segmentName, null);
+    }
+
+    // Initialize for each column
+    for (FieldSpec fieldSpec : physicalFieldSpecs1) {
+      String column = fieldSpec.getName();
+
+      // Partition info
+      PartitionFunction partitionFunction = null;
+      Set<Integer> partitions = null;
+      if (column.equals(_partitionColumn)) {
+        partitionFunction = _partitionFunction;
+        partitions = new HashSet<>();
+        partitions.add(segmentGeneratorConfig.getSequenceId());
+      }
+
+      FieldSpec.DataType dataType = fieldSpec.getDataType();
+      boolean isFixedWidthColumn = dataType.isFixedWidth();
+      MutableForwardIndex forwardIndex;
+      MutableDictionary dictionary;
+
+      int dictionaryColumnSize;
+      if (isFixedWidthColumn) {
+        dictionaryColumnSize = dataType.size();
+      } else {
+        dictionaryColumnSize = DEFAULT_EST_AVG_COL_SIZE;
+      }
+      // NOTE: preserve 10% buffer for cardinality to reduce the chance of re-sizing the dictionary
+      int estimatedCardinality = (int) (DEFAULT_EST_CARDINALITY * 1.1);
+      String dictionaryAllocationContext =
+          buildAllocationContext(_segmentName, column, V1Constants.Dict.FILE_EXTENSION);
+      dictionary = MutableDictionaryFactory
+          .getMutableDictionary(dataType, offHeap, _memoryManager, dictionaryColumnSize,
+              Math.min(estimatedCardinality, _capacity), dictionaryAllocationContext);
+
+      if (fieldSpec.isSingleValueField()) {
+        // Single-value dictionary-encoded forward index
+        String allocationContext =
+            buildAllocationContext(_segmentName, column, V1Constants.Indexes.UNSORTED_SV_FORWARD_INDEX_FILE_EXTENSION);
+        forwardIndex = new FixedByteSVMutableForwardIndex(true, FieldSpec.DataType.INT, _capacity, _memoryManager,
+            allocationContext);
+      } else {
+        // Multi-value dictionary-encoded forward index
+        String allocationContext =
+            buildAllocationContext(_segmentName, column, V1Constants.Indexes.UNSORTED_MV_FORWARD_INDEX_FILE_EXTENSION);
+        // TODO: Start with a smaller capacity on FixedByteMVForwardIndexReaderWriter and let it expand
+        forwardIndex = new FixedByteMVMutableForwardIndex(MAX_MULTI_VALUES_PER_ROW,
+            indexLoadingConfig.getRealtimeAvgMultiValueCount(), _capacity, Integer.BYTES, _memoryManager,

Review comment:
       Removed.




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