saurabhd336 commented on code in PR #8708: URL: https://github.com/apache/pinot/pull/8708#discussion_r887554522
########## 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: Could you help me understand the thread safety concerns with this? I don't any, single threaded or multi threaded. Infact, moving this if check inside `waitTillAllSegmentsLoaded()` would lead to unnecessary serialization even when all segments have already been loaded. Even in single threaded env, that's a heavy lock acquisition cost, when `_allSegmentsLoaded` is already true. To the point where, I think we should reduce the critical section here https://github.com/apache/pinot/blob/master/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/upsert/PartialUpsertHandler.java#L74, once `_allSegmentsLoaded` has been set to true, no need to enter a syncronized block. Do let me know your thoughts -- 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-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org