snleee commented on a change in pull request #5728: URL: https://github.com/apache/incubator-pinot/pull/5728#discussion_r463328679
########## File path: pinot-broker/src/main/java/org/apache/pinot/broker/routing/segmentselector/SegmentLineageBasedSegmentPreSelector.java ########## @@ -0,0 +1,65 @@ +/** + * 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.broker.routing.segmentselector; + +import java.util.HashSet; +import java.util.Set; +import org.apache.helix.ZNRecord; +import org.apache.helix.store.zk.ZkHelixPropertyStore; +import org.apache.pinot.common.lineage.LineageEntry; +import org.apache.pinot.common.lineage.LineageEntryState; +import org.apache.pinot.common.lineage.SegmentLineage; +import org.apache.pinot.common.lineage.SegmentLineageAccessHelper; + + +/** + * Segment lineage based segment pre-selector + * + * This pre-selector reads the segment lineage metadata and filters out either merged segments or original segments + * to make sure that the final segments contain no duplicate data. + */ +public class SegmentLineageBasedSegmentPreSelector implements SegmentPreSelector { + private final String _tableNameWithType; + private final ZkHelixPropertyStore<ZNRecord> _propertyStore; + + public SegmentLineageBasedSegmentPreSelector(String tableNameWithType, ZkHelixPropertyStore<ZNRecord> propertyStore) { + _tableNameWithType = tableNameWithType; + _propertyStore = propertyStore; + } + + @Override + public Set<String> preSelect(Set<String> onlineSegments) { + Set<String> segmentsToProcess = new HashSet<>(onlineSegments); + SegmentLineage segmentLineage = SegmentLineageAccessHelper.getSegmentLineage(_propertyStore, _tableNameWithType); + Set<String> segmentsToRemove = new HashSet<>(); + if (segmentLineage != null) { + for (String lineageEntryId : segmentLineage.getLineageEntryIds()) { + LineageEntry lineageEntry = segmentLineage.getLineageEntry(lineageEntryId); + if (lineageEntry.getState() == LineageEntryState.COMPLETED && onlineSegments + .containsAll(lineageEntry.getSegmentsTo())) { Review comment: As for the edge case that you mentioned, the edge case is correctly covered if we check external view when updating the lineage and segment pre-selector only checks `COMPLETED`. This means that once the validation passes and the lineage entry gets updated to `COMPLETED`, we will never fall back to the original segments (which makes sense because original segments needs to be deleted by the retention manager soon). ########## File path: pinot-broker/src/main/java/org/apache/pinot/broker/routing/segmentselector/SegmentLineageBasedSegmentPreSelector.java ########## @@ -0,0 +1,65 @@ +/** + * 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.broker.routing.segmentselector; + +import java.util.HashSet; +import java.util.Set; +import org.apache.helix.ZNRecord; +import org.apache.helix.store.zk.ZkHelixPropertyStore; +import org.apache.pinot.common.lineage.LineageEntry; +import org.apache.pinot.common.lineage.LineageEntryState; +import org.apache.pinot.common.lineage.SegmentLineage; +import org.apache.pinot.common.lineage.SegmentLineageAccessHelper; + + +/** + * Segment lineage based segment pre-selector + * + * This pre-selector reads the segment lineage metadata and filters out either merged segments or original segments + * to make sure that the final segments contain no duplicate data. + */ +public class SegmentLineageBasedSegmentPreSelector implements SegmentPreSelector { + private final String _tableNameWithType; + private final ZkHelixPropertyStore<ZNRecord> _propertyStore; + + public SegmentLineageBasedSegmentPreSelector(String tableNameWithType, ZkHelixPropertyStore<ZNRecord> propertyStore) { + _tableNameWithType = tableNameWithType; + _propertyStore = propertyStore; + } + + @Override + public Set<String> preSelect(Set<String> onlineSegments) { + Set<String> segmentsToProcess = new HashSet<>(onlineSegments); + SegmentLineage segmentLineage = SegmentLineageAccessHelper.getSegmentLineage(_propertyStore, _tableNameWithType); + Set<String> segmentsToRemove = new HashSet<>(); + if (segmentLineage != null) { + for (String lineageEntryId : segmentLineage.getLineageEntryIds()) { + LineageEntry lineageEntry = segmentLineage.getLineageEntry(lineageEntryId); + if (lineageEntry.getState() == LineageEntryState.COMPLETED && onlineSegments + .containsAll(lineageEntry.getSegmentsTo())) { Review comment: As for the edge case that you mentioned, the edge case is correctly covered if we check external view when updating the lineage and segment pre-selector only checks `COMPLETED`. This means that once the validation passes and the lineage entry gets updated to `COMPLETED`, we will never fall back to the original segments (which makes sense because original segments need to be deleted by the retention manager soon). ---------------------------------------------------------------- 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: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org