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 2020/09/01 17:24:19 UTC

[GitHub] [incubator-pinot] mcvsubbu commented on a change in pull request #5934: Segment processing framework

mcvsubbu commented on a change in pull request #5934:
URL: https://github.com/apache/incubator-pinot/pull/5934#discussion_r481309965



##########
File path: pinot-core/src/main/java/org/apache/pinot/core/segment/processing/utils/SegmentProcessorUtils.java
##########
@@ -0,0 +1,113 @@
+/**
+ * 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.utils;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import org.apache.avro.Schema;
+import org.apache.avro.SchemaBuilder;
+import org.apache.avro.generic.GenericData;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.readers.GenericRow;
+
+
+/**
+ * Helper util methods for SegmentProcessorFramework
+ */
+public final class SegmentProcessorUtils {
+
+  private SegmentProcessorUtils() {
+  }
+
+  /**
+   * Convert a GenericRow to an avro GenericRecord
+   */
+  public static GenericData.Record convertGenericRowToAvroRecord(GenericRow genericRow,

Review comment:
       These should go into avro plugins? Why introduce dependency on avro here?

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/segment/processing/collector/RollupCollector.java
##########
@@ -0,0 +1,138 @@
+/**
+ * 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.collector;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.MetricFieldSpec;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+
+
+/**
+ * A Collector that rolls up the incoming records on unique dimensions + time columns, based on provided aggregation types for metrics.
+ * By default will use the SUM aggregation on metrics.
+ */
+public class RollupCollector implements Collector {
+
+  private final Map<Record, GenericRow> _collection = new HashMap<>();
+
+  private final int _keySize;
+  private final int _valueSize;
+  private final String[] _keyColumns;
+  private final String[] _valueColumns;
+  private final ValueAggregator[] _valueAggregators;
+  private final MetricFieldSpec[] _metricFieldSpecs;
+
+  public RollupCollector(CollectorConfig collectorConfig, Schema schema) {
+    _keySize = schema.getColumnNames().size() - schema.getMetricNames().size();
+    _valueSize = schema.getMetricNames().size();
+    _keyColumns = new String[_keySize];
+    _valueColumns = new String[_valueSize];
+    _valueAggregators = new ValueAggregator[_valueSize];
+    _metricFieldSpecs = new MetricFieldSpec[_valueSize];
+
+    Map<String, ValueAggregatorFactory.ValueAggregatorType> aggregatorTypeMap = collectorConfig.getAggregatorTypeMap();
+    if (aggregatorTypeMap == null) {
+      aggregatorTypeMap = Collections.emptyMap();
+    }
+    int valIdx = 0;
+    int keyIdx = 0;
+    for (FieldSpec fieldSpec : schema.getAllFieldSpecs()) {
+      if (!fieldSpec.isVirtualColumn()) {
+        String name = fieldSpec.getName();
+        if (fieldSpec.getFieldType().equals(FieldSpec.FieldType.METRIC)) {
+          _metricFieldSpecs[valIdx] = (MetricFieldSpec) fieldSpec;
+          _valueColumns[valIdx] = name;
+          _valueAggregators[valIdx] = ValueAggregatorFactory.getValueAggregator(
+              aggregatorTypeMap.getOrDefault(name, ValueAggregatorFactory.ValueAggregatorType.SUM).toString());
+          valIdx++;
+        } else {
+          _keyColumns[keyIdx++] = name;
+        }
+      }
+    }
+  }
+
+  /**
+   * If a row already exists in the collection (based on dimension + time columns), rollup the metric values, else add the row
+   */
+  @Override
+  public void collect(GenericRow genericRow) {
+    Object[] key = new Object[_keySize];
+    for (int i = 0; i < _keySize; i++) {
+      key[i] = genericRow.getValue(_keyColumns[i]);
+    }
+    Record keyRecord = new Record(key);
+    GenericRow prev = _collection.get(keyRecord);
+    if (prev == null) {
+      _collection.put(keyRecord, genericRow);
+    } else {
+      for (int i = 0; i < _valueSize; i++) {
+        String valueColumn = _valueColumns[i];
+        Object aggregate = _valueAggregators[i]
+            .aggregate(prev.getValue(valueColumn), genericRow.getValue(valueColumn), _metricFieldSpecs[i]);
+        prev.putValue(valueColumn, aggregate);
+      }
+    }
+  }
+
+  @Override
+  public Iterator<GenericRow> iterator() {
+    return _collection.values().iterator();
+  }
+
+  @Override
+  public int size() {
+    return _collection.size();
+  }
+
+  @Override
+  public void reset() {
+    _collection.clear();
+  }
+
+  /**
+   * A record representation for the keys of the record
+   * Note that the dimensions can have multi-value columns, and hence the equals and hashCode need deep array operations
+   */
+  private static class Record {
+    private final Object[] _values;

Review comment:
       Are these the key parts? If so, can we name the member as `_keyParts`? `_values` gets confusing.

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/segment/processing/collector/RollupCollector.java
##########
@@ -0,0 +1,138 @@
+/**
+ * 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.collector;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.MetricFieldSpec;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+
+
+/**
+ * A Collector that rolls up the incoming records on unique dimensions + time columns, based on provided aggregation types for metrics.
+ * By default will use the SUM aggregation on metrics.
+ */
+public class RollupCollector implements Collector {
+
+  private final Map<Record, GenericRow> _collection = new HashMap<>();
+
+  private final int _keySize;
+  private final int _valueSize;
+  private final String[] _keyColumns;
+  private final String[] _valueColumns;
+  private final ValueAggregator[] _valueAggregators;
+  private final MetricFieldSpec[] _metricFieldSpecs;
+
+  public RollupCollector(CollectorConfig collectorConfig, Schema schema) {
+    _keySize = schema.getColumnNames().size() - schema.getMetricNames().size();

Review comment:
       So, we include virtual columns here?

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/segment/processing/collector/CollectorFactory.java
##########
@@ -0,0 +1,54 @@
+/**
+ * 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.collector;
+
+import org.apache.pinot.spi.data.Schema;
+
+
+/**
+ * Factory for constructing a Collector from CollectorConfig
+ */
+public final class CollectorFactory {
+
+  private CollectorFactory() {
+
+  }
+
+  public enum CollectorType {
+    ROLLUP, CONCAT

Review comment:
       It is useful to add some comments on each of these, explaining what the collector does (or, what it does differently than others)

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/segment/processing/partitioner/PartitionerFactory.java
##########
@@ -0,0 +1,82 @@
+/**
+ * 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.partitioner;
+
+import com.google.common.base.Preconditions;
+
+
+/**
+ * Factory for Partitioner and PartitionFilter
+ */
+public final class PartitionerFactory {
+
+  private PartitionerFactory() {
+
+  }
+
+  public enum PartitionerType {
+    NO_OP, ROW_HASH, COLUMN_VALUE, TRANSFORM_FUNCTION, TABLE_PARTITION_CONFIG
+  }
+
+  /**
+   * Construct a Partitioner using the PartitioningConfig
+   */
+  public static Partitioner getPartitioner(PartitioningConfig config) {
+
+    Partitioner partitioner = null;
+    switch (config.getPartitionerType()) {
+      case NO_OP:
+        partitioner = new NoOpPartitioner();
+        break;
+      case ROW_HASH:
+        Preconditions
+            .checkState(config.getNumPartitions() > 0, "Must provide numPartitions > 0 for ROW_HASH partitioner");
+        partitioner = new RowHashPartitioner(config.getNumPartitions());
+        break;
+      case COLUMN_VALUE:
+        Preconditions.checkState(config.getColumnName() != null, "Must provide columnName for COLUMN_VALUE partitioner");
+        partitioner = new ColumnValuePartitioner(config.getColumnName());
+        break;
+      case TRANSFORM_FUNCTION:
+        Preconditions.checkState(config.getTransformFunction() != null,
+            "Must provide transformFunction for TRANSFORM_FUNCTION partitioner");
+        partitioner = new TransformFunctionPartitioner(config.getTransformFunction());
+        break;
+      case TABLE_PARTITION_CONFIG:

Review comment:
       Since these are all derived segments (merging m segments into n) should we not be using the table's partitioner all the time?

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/segment/processing/collector/Collector.java
##########
@@ -0,0 +1,50 @@
+/**
+ * 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.collector;
+
+import java.util.Iterator;
+import org.apache.pinot.spi.data.readers.GenericRow;
+
+
+/**
+ * Collects and stores GenericRows
+ */
+public interface Collector {
+
+  /**
+   * Collects the given GenericRow and stores it
+   * @param genericRow the generic row to add to the collection
+   */
+  void collect(GenericRow genericRow);
+
+  /**
+   * Provides an iterator for the GenericRows in the collection
+   */
+  Iterator<GenericRow> iterator();
+
+  /**
+   * The size of the collection

Review comment:
       What does size mean? Number of rows? Memory size?

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/segment/processing/framework/SegmentReducer.java
##########
@@ -0,0 +1,151 @@
+/**
+ * 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.framework;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Iterator;
+import org.apache.avro.file.DataFileWriter;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.pinot.core.segment.processing.collector.Collector;
+import org.apache.pinot.core.segment.processing.collector.CollectorFactory;
+import org.apache.pinot.core.segment.processing.utils.SegmentProcessorUtils;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+import org.apache.pinot.spi.data.readers.RecordReader;
+import org.apache.pinot.spi.data.readers.RecordReaderFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Reducer phase of the SegmentProcessorFramework
+ * Reads the avro files in the input directory and creates output avro files in the reducer output directory.
+ * The avro files in the input directory are expected to contain data for only 1 partition
+ * Performs operations on that partition data as follows:
+ * - concatenation/rollup of records
+ * - split
+ * - TODO: dedup
+ */
+public class SegmentReducer {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(SegmentReducer.class);
+  private static final int MAX_RECORDS_TO_COLLECT = 5_000_000;
+
+  private final File _reducerInputDir;
+  private final File _reducerOutputDir;
+
+  private final String _reducerId;
+  private final Schema _pinotSchema;
+  private final org.apache.avro.Schema _avroSchema;
+  private final Collector _collector;
+  private final int _numRecordsPerPart;
+
+  public SegmentReducer(String reducerId, File reducerInputDir, SegmentReducerConfig reducerConfig,
+      File reducerOutputDir) {
+    _reducerInputDir = reducerInputDir;
+    _reducerOutputDir = reducerOutputDir;
+
+    _reducerId = reducerId;
+    _pinotSchema = reducerConfig.getPinotSchema();
+    _avroSchema = SegmentProcessorUtils.convertPinotSchemaToAvroSchema(_pinotSchema);
+    _collector = CollectorFactory.getCollector(reducerConfig.getCollectorConfig(), _pinotSchema);
+    _numRecordsPerPart = reducerConfig.getNumRecordsPerPart();
+    LOGGER.info("Initialized reducer with id: {}, input dir: {}, output dir: {}, collector: {}, numRecordsPerPart: {}",
+        _reducerId, _reducerInputDir, _reducerOutputDir, _collector.getClass(), _numRecordsPerPart);
+  }
+
+  /**
+   * Reads the avro files in the input directory.
+   * Performs configured operations and outputs to other avro file(s) in the reducer output directory.
+   */
+  public void reduce()
+      throws Exception {
+
+    int part = 0;
+    for (File inputFile : _reducerInputDir.listFiles()) {
+
+      RecordReader avroRecordReader = RecordReaderFactory
+          .getRecordReaderByClass("org.apache.pinot.plugin.inputformat.avro.AvroRecordReader", inputFile,
+              _pinotSchema.getColumnNames(), null);
+
+      while (avroRecordReader.hasNext()) {
+        GenericRow next = avroRecordReader.next();
+
+        // Aggregations
+        _collector.collect(next);
+
+        // Exceeded max records allowed to collect. Flush
+        if (_collector.size() == MAX_RECORDS_TO_COLLECT) {

Review comment:
       Instead of checking the number of records, can we have a method in the collector that says it is full? The criteria could then be something else depending on the collector.




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