jtao15 commented on code in PR #10679: URL: https://github.com/apache/pinot/pull/10679#discussion_r1180654128
########## pinot-controller/src/main/java/org/apache/pinot/controller/api/lineage/DefaultLineageManager.java: ########## @@ -0,0 +1,125 @@ +/** + * 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.controller.api.lineage; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; +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.controller.ControllerConf; +import org.apache.pinot.spi.config.table.TableConfig; +import org.apache.pinot.spi.utils.IngestionConfigUtils; + + +public class DefaultLineageManager implements LineageManager { + private static final long REPLACED_SEGMENTS_RETENTION_IN_MILLIS = TimeUnit.DAYS.toMillis(1L); // 1 day + private static final long LINEAGE_ENTRY_CLEANUP_RETENTION_IN_MILLIS = TimeUnit.DAYS.toMillis(1L); // 1 day + + protected ControllerConf _controllerConf; + + public DefaultLineageManager(ControllerConf controllerConf) { + _controllerConf = controllerConf; + } + + @Override + public void updateLineageForStartReplaceSegments(TableConfig tableConfig, String lineageEntryId, + Map<String, String> customMap, SegmentLineage lineage) { + } + + @Override + public void updateLineageForEndReplaceSegments(TableConfig tableConfig, String lineageEntryId, + Map<String, String> customMap, SegmentLineage lineage) { + } + + @Override + public void updateLineageForRevertReplaceSegments(TableConfig tableConfig, String lineageEntryId, + Map<String, String> customMap, SegmentLineage lineage) { + } + + @Override + public void updateLineageForRetention(TableConfig tableConfig, SegmentLineage lineage, List<String> allSegments, + List<String> segmentsToDelete, Set<String> consumingSegments) { + // 1. The original segments can be deleted once the merged segments are successfully uploaded + // 2. The zombie lineage entry & merged segments should be deleted if the segment replacement failed in + // the middle + Set<String> segmentsForTable = new HashSet<>(allSegments); + Iterator<LineageEntry> lineageEntryIterator = lineage.getLineageEntries().values().iterator(); + while (lineageEntryIterator.hasNext()) { + LineageEntry lineageEntry = lineageEntryIterator.next(); + if (lineageEntry.getState() == LineageEntryState.COMPLETED) { + Set<String> sourceSegments = new HashSet<>(lineageEntry.getSegmentsFrom()); + sourceSegments.retainAll(segmentsForTable); + if (sourceSegments.isEmpty()) { + // If the lineage state is 'COMPLETED' and segmentFrom are removed, it is safe clean up the lineage entry + lineageEntryIterator.remove(); + } else { + // If the lineage state is 'COMPLETED' and we already preserved the original segments for the required + // retention, it is safe to delete all segments from 'segmentsFrom' + if (shouldDeleteReplacedSegments(tableConfig, lineageEntry)) { + segmentsToDelete.addAll(sourceSegments); + } + } + } else if (lineageEntry.getState() == LineageEntryState.REVERTED || ( + lineageEntry.getState() == LineageEntryState.IN_PROGRESS && lineageEntry.getTimestamp() + < System.currentTimeMillis() - LINEAGE_ENTRY_CLEANUP_RETENTION_IN_MILLIS)) { + // If the lineage state is 'IN_PROGRESS' or 'REVERTED', we need to clean up the zombie lineage + // entry and its segments + Set<String> destinationSegments = new HashSet<>(lineageEntry.getSegmentsTo()); + destinationSegments.retainAll(segmentsForTable); + if (destinationSegments.isEmpty()) { + // If the lineage state is 'IN_PROGRESS or REVERTED' and source segments are already removed, it is safe + // to clean up the lineage entry. Deleting lineage will allow the task scheduler to re-schedule the source Review Comment: This is destination segments, for REVERTED/IN_PROGRESS (more than 24hrs) states, we make sure all segmentsTo are deleted before cleaning up the lineage entries, else both segmentsFrom and segmentsTo are used to answer queries. -- 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