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 2022/06/02 00:09:37 UTC

[GitHub] [pinot] Jackie-Jiang commented on a diff in pull request #8708: Dedup config

Jackie-Jiang commented on code in PR #8708:
URL: https://github.com/apache/pinot/pull/8708#discussion_r887385795


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/dedup/PartitionDedupMetadataManager.java:
##########
@@ -0,0 +1,154 @@
+/**
+ * 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.dedup;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.helix.HelixManager;
+import org.apache.pinot.common.metrics.ServerGauge;
+import org.apache.pinot.common.metrics.ServerMetrics;
+import org.apache.pinot.segment.local.segment.readers.PinotSegmentColumnReader;
+import org.apache.pinot.segment.local.utils.HashUtils;
+import org.apache.pinot.segment.local.utils.RecordInfo;
+import org.apache.pinot.segment.local.utils.tablestate.TableState;
+import org.apache.pinot.segment.spi.IndexSegment;
+import org.apache.pinot.spi.config.table.HashFunction;
+import org.apache.pinot.spi.data.readers.PrimaryKey;
+import org.apache.pinot.spi.utils.ByteArray;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class PartitionDedupMetadataManager {
+  private static final Logger LOGGER = LoggerFactory.getLogger(PartitionDedupMetadataManager.class);
+
+  private final HelixManager _helixManager;
+  private final String _tableNameWithType;
+  private final List<String> _primaryKeyColumns;
+  private final int _partitionId;
+  private final ServerMetrics _serverMetrics;
+  private final HashFunction _hashFunction;
+  private boolean _allSegmentsLoaded;
+
+  // TODO(saurabh) : We can replace this with a ocncurrent Set
+  @VisibleForTesting
+  final ConcurrentHashMap<Object, IndexSegment> _primaryKeySet = new ConcurrentHashMap<>();

Review Comment:
   ```suggestion
     final ConcurrentHashMap<Object, IndexSegment> _primaryKeyToSegmentMap = new ConcurrentHashMap<>();
   ```



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/dedup/PartitionDedupMetadataManager.java:
##########
@@ -0,0 +1,154 @@
+/**
+ * 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.dedup;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.helix.HelixManager;
+import org.apache.pinot.common.metrics.ServerGauge;
+import org.apache.pinot.common.metrics.ServerMetrics;
+import org.apache.pinot.segment.local.segment.readers.PinotSegmentColumnReader;
+import org.apache.pinot.segment.local.utils.HashUtils;
+import org.apache.pinot.segment.local.utils.RecordInfo;
+import org.apache.pinot.segment.local.utils.tablestate.TableState;
+import org.apache.pinot.segment.spi.IndexSegment;
+import org.apache.pinot.spi.config.table.HashFunction;
+import org.apache.pinot.spi.data.readers.PrimaryKey;
+import org.apache.pinot.spi.utils.ByteArray;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class PartitionDedupMetadataManager {
+  private static final Logger LOGGER = LoggerFactory.getLogger(PartitionDedupMetadataManager.class);
+
+  private final HelixManager _helixManager;
+  private final String _tableNameWithType;
+  private final List<String> _primaryKeyColumns;
+  private final int _partitionId;
+  private final ServerMetrics _serverMetrics;
+  private final HashFunction _hashFunction;
+  private boolean _allSegmentsLoaded;
+
+  // TODO(saurabh) : We can replace this with a ocncurrent Set

Review Comment:
   (minor) Remove this TODO



##########
pinot-segment-local/src/test/resources/data/test_dedup_data.json:
##########
@@ -0,0 +1,22 @@
+[
+  {
+    "event_id": "aa",
+    "description" : "first",
+    "secondsSinceEpoch": 1567205394
+  },
+  {
+    "event_id": "bb",
+    "description" : "first",
+    "secondsSinceEpoch": 1567205396
+  },
+  {
+    "event_id": "aa",
+    "description" : "second",
+    "secondsSinceEpoch": 1567205397
+  },
+  {
+    "event_id": "bb",
+    "description" : "second",
+    "secondsSinceEpoch": 1567205392
+  }
+]

Review Comment:
   (nit) empty line



##########
pinot-core/src/main/java/org/apache/pinot/core/data/manager/realtime/LLRealtimeSegmentDataManager.java:
##########
@@ -538,6 +539,7 @@ private void processStreamEvents(MessageBatch messagesAndOffsets, long idlePipeS
         }
         for (GenericRow transformedRow : reusedResult.getTransformedRows()) {
           try {
+            // TODO(saurabh): we may have dropped the record due to dedup. Should we increment indexedMessageCount?

Review Comment:
   (minor) Since we already track the dropped records, we can remove this TODO and consider changing it to a comment



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/indexsegment/mutable/MutableSegmentImpl.java:
##########
@@ -477,8 +482,22 @@ public boolean index(GenericRow row, @Nullable RowMetadata rowMetadata)
       throws IOException {
     boolean canTakeMore;
     int numDocsIndexed = _numDocsIndexed;
+
+    RecordInfo recordInfo = null;
+
+    if (isDedupEnabled() || isUpsertEnabled()) {
+      recordInfo = getRecordInfo(row, numDocsIndexed);
+    }
+
+    if (isDedupEnabled() && _partitionDedupMetadataManager.checkRecordPresentOrUpdate(recordInfo, this)) {
+      _logger.info("Dropped row {} since its primary key already exists", row);
+      if (_serverMetrics != null) {
+        _serverMetrics.addMeteredTableValue(_realtimeTableName, ServerMeter.REALTIME_DEDUP_DROPPED, 1);
+      }
+      return numDocsIndexed < _capacity;

Review Comment:
   (minor)
   ```suggestion
         return true;
   ```



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/dedup/PartitionDedupMetadataManager.java:
##########
@@ -0,0 +1,154 @@
+/**
+ * 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.dedup;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.helix.HelixManager;
+import org.apache.pinot.common.metrics.ServerGauge;
+import org.apache.pinot.common.metrics.ServerMetrics;
+import org.apache.pinot.segment.local.segment.readers.PinotSegmentColumnReader;
+import org.apache.pinot.segment.local.utils.HashUtils;
+import org.apache.pinot.segment.local.utils.RecordInfo;
+import org.apache.pinot.segment.local.utils.tablestate.TableState;
+import org.apache.pinot.segment.spi.IndexSegment;
+import org.apache.pinot.spi.config.table.HashFunction;
+import org.apache.pinot.spi.data.readers.PrimaryKey;
+import org.apache.pinot.spi.utils.ByteArray;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class PartitionDedupMetadataManager {
+  private static final Logger LOGGER = LoggerFactory.getLogger(PartitionDedupMetadataManager.class);
+
+  private final HelixManager _helixManager;
+  private final String _tableNameWithType;
+  private final List<String> _primaryKeyColumns;
+  private final int _partitionId;
+  private final ServerMetrics _serverMetrics;
+  private final HashFunction _hashFunction;
+  private boolean _allSegmentsLoaded;
+
+  // TODO(saurabh) : We can replace this with a ocncurrent Set
+  @VisibleForTesting
+  final ConcurrentHashMap<Object, IndexSegment> _primaryKeySet = new ConcurrentHashMap<>();
+
+  public PartitionDedupMetadataManager(HelixManager helixManager, String tableNameWithType,
+      List<String> primaryKeyColumns, int partitionId, ServerMetrics serverMetrics, HashFunction hashFunction) {
+    _helixManager = helixManager;
+    _tableNameWithType = tableNameWithType;
+    _primaryKeyColumns = primaryKeyColumns;
+    _partitionId = partitionId;
+    _serverMetrics = serverMetrics;
+    _hashFunction = hashFunction;
+  }
+
+  public void addSegment(IndexSegment segment) {
+    // Add all PKs to _primaryKeySet
+    Iterator<RecordInfo> recordInfoIterator = getRecordInfoIterator(segment, _primaryKeyColumns);
+    while (recordInfoIterator.hasNext()) {
+      RecordInfo recordInfo = recordInfoIterator.next();
+      _primaryKeySet.put(HashUtils.hashPrimaryKey(recordInfo.getPrimaryKey(), _hashFunction), segment);
+    }
+    _serverMetrics.setValueOfPartitionGauge(_tableNameWithType, _partitionId, ServerGauge.DEDUP_PRIMARY_KEYS_COUNT,
+        _primaryKeySet.size());
+  }
+
+  public void removeSegment(IndexSegment segment) {
+    // TODO(saurabh): Explain reload scenario here
+    Iterator<RecordInfo> recordInfoIterator = getRecordInfoIterator(segment, _primaryKeyColumns);
+    while (recordInfoIterator.hasNext()) {
+      RecordInfo recordInfo = recordInfoIterator.next();
+      _primaryKeySet.compute(HashUtils.hashPrimaryKey(recordInfo.getPrimaryKey(), _hashFunction),
+          (primaryKey, currentSegment) -> {
+            if (currentSegment == segment) {
+              return null;
+            } else {
+              return currentSegment;
+            }
+          });
+    }
+    _serverMetrics.setValueOfPartitionGauge(_tableNameWithType, _partitionId, ServerGauge.DEDUP_PRIMARY_KEYS_COUNT,
+        _primaryKeySet.size());
+  }
+
+  @VisibleForTesting
+  public static Iterator<RecordInfo> getRecordInfoIterator(IndexSegment segment, List<String> primaryKeyColumns) {

Review Comment:
   Suggest returning an iterator of `PrimaryKey`. For dedup, we don't need the docId and comparisonValue information from the `RecordInfo`. Similar for the `checkRecordPresentOrUpdate()` which can just take the `PrimaryKey` object. This is not a blocker, so maybe put a TODO and address it later



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/indexsegment/mutable/MutableSegmentImpl.java:
##########
@@ -477,8 +482,22 @@ public boolean index(GenericRow row, @Nullable RowMetadata rowMetadata)
       throws IOException {
     boolean canTakeMore;
     int numDocsIndexed = _numDocsIndexed;
+
+    RecordInfo recordInfo = null;
+
+    if (isDedupEnabled() || isUpsertEnabled()) {
+      recordInfo = getRecordInfo(row, numDocsIndexed);
+    }
+
+    if (isDedupEnabled() && _partitionDedupMetadataManager.checkRecordPresentOrUpdate(recordInfo, this)) {
+      _logger.info("Dropped row {} since its primary key already exists", row);

Review Comment:
   Don't log anything here, it can flood the log



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/utils/tablestate/TableState.java:
##########
@@ -0,0 +1,99 @@
+/**
+ * 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.utils.tablestate;
+
+import java.util.Map;
+import org.apache.helix.HelixDataAccessor;
+import org.apache.helix.HelixManager;
+import org.apache.helix.PropertyKey;
+import org.apache.helix.model.CurrentState;
+import org.apache.helix.model.IdealState;
+import org.apache.helix.model.LiveInstance;
+import org.apache.pinot.spi.utils.CommonConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TableState {

Review Comment:
   (minor) Let's rename it to `TableStateUtils`



##########
pinot-segment-local/src/test/resources/data/test_dedup_schema.json:
##########
@@ -0,0 +1,21 @@
+{
+  "schemaName": "test_pinot_table",
+  "dimensionFieldSpecs": [
+    {
+      "name": "event_id",
+      "dataType": "STRING"
+    },
+    {
+      "name": "description",
+      "dataType": "STRING"
+    }
+  ],
+  "timeFieldSpec": {
+    "incomingGranularitySpec": {
+      "name": "secondsSinceEpoch",
+      "dataType": "LONG",
+      "timeType": "SECONDS"
+    }
+  },
+  "primaryKeyColumns": ["event_id"]
+}

Review Comment:
   (nit) empty line



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/dedup/PartitionDedupMetadataManager.java:
##########
@@ -0,0 +1,154 @@
+/**
+ * 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.dedup;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.helix.HelixManager;
+import org.apache.pinot.common.metrics.ServerGauge;
+import org.apache.pinot.common.metrics.ServerMetrics;
+import org.apache.pinot.segment.local.segment.readers.PinotSegmentColumnReader;
+import org.apache.pinot.segment.local.utils.HashUtils;
+import org.apache.pinot.segment.local.utils.RecordInfo;
+import org.apache.pinot.segment.local.utils.tablestate.TableState;
+import org.apache.pinot.segment.spi.IndexSegment;
+import org.apache.pinot.spi.config.table.HashFunction;
+import org.apache.pinot.spi.data.readers.PrimaryKey;
+import org.apache.pinot.spi.utils.ByteArray;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class PartitionDedupMetadataManager {
+  private static final Logger LOGGER = LoggerFactory.getLogger(PartitionDedupMetadataManager.class);
+
+  private final HelixManager _helixManager;
+  private final String _tableNameWithType;
+  private final List<String> _primaryKeyColumns;
+  private final int _partitionId;
+  private final ServerMetrics _serverMetrics;
+  private final HashFunction _hashFunction;
+  private boolean _allSegmentsLoaded;
+
+  // TODO(saurabh) : We can replace this with a ocncurrent Set
+  @VisibleForTesting
+  final ConcurrentHashMap<Object, IndexSegment> _primaryKeySet = new ConcurrentHashMap<>();
+
+  public PartitionDedupMetadataManager(HelixManager helixManager, String tableNameWithType,
+      List<String> primaryKeyColumns, int partitionId, ServerMetrics serverMetrics, HashFunction hashFunction) {
+    _helixManager = helixManager;
+    _tableNameWithType = tableNameWithType;
+    _primaryKeyColumns = primaryKeyColumns;
+    _partitionId = partitionId;
+    _serverMetrics = serverMetrics;
+    _hashFunction = hashFunction;
+  }
+
+  public void addSegment(IndexSegment segment) {
+    // Add all PKs to _primaryKeySet
+    Iterator<RecordInfo> recordInfoIterator = getRecordInfoIterator(segment, _primaryKeyColumns);
+    while (recordInfoIterator.hasNext()) {
+      RecordInfo recordInfo = recordInfoIterator.next();
+      _primaryKeySet.put(HashUtils.hashPrimaryKey(recordInfo.getPrimaryKey(), _hashFunction), segment);
+    }
+    _serverMetrics.setValueOfPartitionGauge(_tableNameWithType, _partitionId, ServerGauge.DEDUP_PRIMARY_KEYS_COUNT,
+        _primaryKeySet.size());
+  }
+
+  public void removeSegment(IndexSegment segment) {
+    // TODO(saurabh): Explain reload scenario here
+    Iterator<RecordInfo> recordInfoIterator = getRecordInfoIterator(segment, _primaryKeyColumns);
+    while (recordInfoIterator.hasNext()) {
+      RecordInfo recordInfo = recordInfoIterator.next();
+      _primaryKeySet.compute(HashUtils.hashPrimaryKey(recordInfo.getPrimaryKey(), _hashFunction),
+          (primaryKey, currentSegment) -> {
+            if (currentSegment == segment) {
+              return null;
+            } else {
+              return currentSegment;
+            }
+          });
+    }
+    _serverMetrics.setValueOfPartitionGauge(_tableNameWithType, _partitionId, ServerGauge.DEDUP_PRIMARY_KEYS_COUNT,
+        _primaryKeySet.size());
+  }
+
+  @VisibleForTesting
+  public static Iterator<RecordInfo> getRecordInfoIterator(IndexSegment segment, List<String> primaryKeyColumns) {
+    Map<String, PinotSegmentColumnReader> columnToReaderMap = new HashMap<>();
+    for (String primaryKeyColumn : primaryKeyColumns) {
+      columnToReaderMap.put(primaryKeyColumn, new PinotSegmentColumnReader(segment, primaryKeyColumn));
+    }
+    int numTotalDocs = segment.getSegmentMetadata().getTotalDocs();
+    int numPrimaryKeyColumns = primaryKeyColumns.size();
+    return new Iterator<RecordInfo>() {
+      private int _docId = 0;
+
+      @Override
+      public boolean hasNext() {
+        return _docId < numTotalDocs;
+      }
+
+      @Override
+      public RecordInfo next() {
+        Object[] values = new Object[numPrimaryKeyColumns];
+        for (int i = 0; i < numPrimaryKeyColumns; i++) {
+          Object value = columnToReaderMap.get(primaryKeyColumns.get(i)).getValue(_docId);
+          if (value instanceof byte[]) {
+            value = new ByteArray((byte[]) value);
+          }
+          values[i] = value;
+        }
+        PrimaryKey primaryKey = new PrimaryKey(values);
+        return new RecordInfo(primaryKey, _docId++, null);
+      }
+    };
+  }
+
+  private synchronized void waitTillAllSegmentsLoaded() {
+    while (!TableState.isAllSegmentsLoaded(_helixManager, _tableNameWithType)) {
+      LOGGER.info("Sleeping 1 second waiting for all segments loaded for partial-upsert table: {}", _tableNameWithType);
+      try {
+        //noinspection BusyWait
+        Thread.sleep(1000L);
+      } catch (InterruptedException e) {
+        throw new RuntimeException(e);
+      }
+    }
+    _allSegmentsLoaded = true;
+  }
+
+  public boolean checkRecordPresentOrUpdate(RecordInfo recordInfo, IndexSegment indexSegment) {
+    if (!_allSegmentsLoaded) {

Review Comment:
   Let's move the if check into the `waitTillAllSegmentsLoaded()` for thread safety. It is single threaded now, but in case that changes



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