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/06/29 18:47:31 UTC

[GitHub] [incubator-pinot] npawar commented on a change in pull request #7092: SegmentProcessorFramework Enhancement

npawar commented on a change in pull request #7092:
URL: https://github.com/apache/incubator-pinot/pull/7092#discussion_r660878218



##########
File path: pinot-core/src/main/java/org/apache/pinot/core/segment/processing/reducer/RollupReducer.java
##########
@@ -0,0 +1,172 @@
+/**
+ * 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.segment.processing.reducer;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.commons.io.FileUtils;
+import org.apache.pinot.core.segment.processing.aggregator.ValueAggregator;
+import org.apache.pinot.core.segment.processing.aggregator.ValueAggregatorFactory;
+import org.apache.pinot.core.segment.processing.genericrow.GenericRowFileManager;
+import org.apache.pinot.core.segment.processing.genericrow.GenericRowFileReader;
+import org.apache.pinot.core.segment.processing.genericrow.GenericRowFileRecordReader;
+import org.apache.pinot.core.segment.processing.genericrow.GenericRowFileWriter;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.FieldSpec.FieldType;
+import org.apache.pinot.spi.data.readers.GenericRow;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * RollupReducer aggregates the metric values for GenericRows with the same dimension + time values.
+ */
+public class RollupReducer implements Reducer {
+  private static final Logger LOGGER = LoggerFactory.getLogger(RollupReducer.class);
+  private static final AggregationFunctionType DEFAULT_AGGREGATOR_TYPE = AggregationFunctionType.SUM;
+
+  private final String _partitionId;
+  private final GenericRowFileManager _fileManager;
+  private final Map<String, AggregationFunctionType> _aggregationTypes;
+  private final File _reducerOutputDir;
+
+  public RollupReducer(String partitionId, GenericRowFileManager fileManager,
+      Map<String, AggregationFunctionType> aggregationTypes, File reducerOutputDir) {
+    _partitionId = partitionId;
+    _fileManager = fileManager;
+    _aggregationTypes = aggregationTypes;
+    _reducerOutputDir = reducerOutputDir;
+  }
+
+  @Override
+  public GenericRowFileManager reduce()
+      throws Exception {
+    LOGGER.info("Start reducing on partition: {}", _partitionId);
+    long reduceStartTimeMs = System.currentTimeMillis();
+
+    GenericRowFileReader fileReader = _fileManager.getFileReader();
+    int numRows = fileReader.getNumRows();
+    int numSortFields = fileReader.getNumSortFields();
+    LOGGER.info("Start sorting on numRows: {}, numSortFields: {}", numRows, numSortFields);
+    long sortStartTimeMs = System.currentTimeMillis();
+    GenericRowFileRecordReader recordReader = fileReader.getRecordReader();
+    LOGGER.info("Finish sorting in {}ms", System.currentTimeMillis() - sortStartTimeMs);
+
+    List<FieldSpec> fieldSpecs = _fileManager.getFieldSpecs();
+    boolean includeNullFields = _fileManager.isIncludeNullFields();
+    List<AggregatorContext> aggregatorContextList = new ArrayList<>();
+    for (FieldSpec fieldSpec : fieldSpecs) {
+      if (fieldSpec.getFieldType() == FieldType.METRIC) {
+        aggregatorContextList.add(new AggregatorContext(fieldSpec,
+            _aggregationTypes.getOrDefault(fieldSpec.getName(), DEFAULT_AGGREGATOR_TYPE)));
+      }
+    }
+
+    File partitionOutputDir = new File(_reducerOutputDir, _partitionId);
+    FileUtils.forceMkdir(partitionOutputDir);
+    LOGGER.info("Start creating rollup file under dir: {}", partitionOutputDir);
+    long rollupFileCreationStartTimeMs = System.currentTimeMillis();
+    GenericRowFileManager rollupFileManager =
+        new GenericRowFileManager(partitionOutputDir, fieldSpecs, includeNullFields, 0);
+    GenericRowFileWriter rollupFileWriter = rollupFileManager.getFileWriter();
+    GenericRow previousRow = new GenericRow();
+    recordReader.read(0, previousRow);
+    int previousRowId = 0;
+    GenericRow buffer = new GenericRow();
+    if (includeNullFields) {
+      for (int i = 1; i < numRows; i++) {
+        buffer.clear();
+        recordReader.read(i, buffer);
+        if (recordReader.compare(previousRowId, i) == 0) {
+          aggregateWithNullFields(previousRow, buffer, aggregatorContextList);
+        } else {
+          rollupFileWriter.write(previousRow);
+          previousRowId = i;
+          GenericRow temp = previousRow;
+          previousRow = buffer;
+          buffer = temp;
+        }
+      }
+    } else {
+      for (int i = 1; i < numRows; i++) {
+        buffer.clear();
+        recordReader.read(i, buffer);
+        if (recordReader.compare(previousRowId, i) == 0) {
+          aggregateWithoutNullFields(previousRow, buffer, aggregatorContextList);
+        } else {
+          rollupFileWriter.write(previousRow);
+          previousRowId = i;
+          GenericRow temp = previousRow;
+          previousRow = buffer;
+          buffer = temp;
+        }
+      }
+    }
+    rollupFileWriter.write(previousRow);
+    rollupFileManager.closeFileWriter();
+    LOGGER.info("Finish creating rollup file in {}ms", System.currentTimeMillis() - rollupFileCreationStartTimeMs);
+
+    _fileManager.cleanUp();

Review comment:
       how are we controlling the number of records per segment now?




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