Guosmilesmile commented on code in PR #15996: URL: https://github.com/apache/iceberg/pull/15996#discussion_r3098293391
########## flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/maintenance/api/ConvertEqualityDeletes.java: ########## @@ -0,0 +1,251 @@ +/* + * 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.iceberg.flink.maintenance.api; + +import java.io.IOException; +import java.io.UncheckedIOException; +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.streaming.api.datastream.DataStream; +import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator; +import org.apache.iceberg.HasTableOperations; +import org.apache.iceberg.SnapshotRef; +import org.apache.iceberg.Table; +import org.apache.iceberg.flink.TableLoader; +import org.apache.iceberg.flink.maintenance.operator.DVMergeCommand; +import org.apache.iceberg.flink.maintenance.operator.DVMergeResult; +import org.apache.iceberg.flink.maintenance.operator.DVPosition; +import org.apache.iceberg.flink.maintenance.operator.EqualityConvertCommitter; +import org.apache.iceberg.flink.maintenance.operator.EqualityConvertDVMerger; +import org.apache.iceberg.flink.maintenance.operator.EqualityConvertDVResolver; +import org.apache.iceberg.flink.maintenance.operator.EqualityConvertPlanResult; +import org.apache.iceberg.flink.maintenance.operator.EqualityConvertPlanner; +import org.apache.iceberg.flink.maintenance.operator.EqualityConvertReader; +import org.apache.iceberg.flink.maintenance.operator.EqualityConvertWorker; +import org.apache.iceberg.flink.maintenance.operator.IndexCommand; +import org.apache.iceberg.flink.maintenance.operator.ReadCommand; +import org.apache.iceberg.flink.maintenance.operator.SerializedEqualityValues; +import org.apache.iceberg.flink.maintenance.operator.TaskResultAggregator; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; + +/** + * Creates the equality delete to DV conversion data stream. Runs a single iteration of the + * conversion for every {@link Trigger} event. + * + * <p>The pipeline reads equality delete files from a staging branch, converts them to deletion + * vectors (DVs) using a primary key index stored in Flink state, and commits the data files and DVs + * to the main branch. + * + * <p>The conversion is split into parallel stages: + * + * <ol> + * <li>Planner (p=1): scans staging branch, emits file-level ReadCommands with phase timestamps + * <li>Reader (p=N): reads files, emits row-level IndexCommands + * <li>Worker (p=N): maintains PK index shards, resolves equality deletes to DV positions + * <li>DVResolver (p=1): groups positions by data file, resolves partition info and existing DVs + * <li>DVMerger (p=N): writes merged deletion vector files + * <li>Committer (p=1): commits data files and DVs to main branch + * </ol> + * + * <p>Mutual exclusion with concurrent maintenance tasks (e.g. compaction) is enforced by the Flink + * maintenance framework lock. The lock is acquired before the maintenance task runs and released + * after each cycle completes, ensuring no conflicting commits occur on the target branch. + */ +public class ConvertEqualityDeletes { + static final String PLANNER_TASK_NAME = "EqConvert Planner"; + static final String READER_TASK_NAME = "EqConvert Reader"; + static final String WORKER_TASK_NAME = "EqConvert Worker"; + static final String DV_RESOLVER_TASK_NAME = "EqConvert DVResolver"; + static final String DV_MERGER_TASK_NAME = "EqConvert DVMerger"; + static final String COMMIT_TASK_NAME = "EqConvert Commit"; + static final String AGGREGATOR_TASK_NAME = "EqConvert Aggregator"; + + private ConvertEqualityDeletes() {} + + public static Builder builder() { + return new Builder(); + } + + public static class Builder extends MaintenanceTaskBuilder<Builder> { + private String stagingBranch; + private String targetBranch = SnapshotRef.MAIN_BRANCH; + private int maxSnapshotsPerTrigger = Integer.MAX_VALUE; + + @Override + String maintenanceTaskName() { + return "ConvertEqualityDeletes"; + } + + /** Sets the staging branch name that holds the equality delete files and data files. */ + public Builder stagingBranch(String newStagingBranch) { + this.stagingBranch = newStagingBranch; + return this; + } + + /** + * Sets the target branch where converted data files and DVs are committed. Defaults to the main + * branch. + */ + public Builder targetBranch(String newTargetBranch) { + this.targetBranch = newTargetBranch; + return this; + } + + /** + * Sets the maximum number of staging snapshots to process per trigger. Defaults to {@link + * Integer#MAX_VALUE} (process all pending snapshots). + */ + public Builder maxSnapshotsPerTrigger(int newMaxSnapshotsPerTrigger) { + this.maxSnapshotsPerTrigger = newMaxSnapshotsPerTrigger; + return this; + } + + @Override + DataStream<TaskResult> append(DataStream<Trigger> trigger) { + Preconditions.checkNotNull(stagingBranch, "stagingBranch must be set"); + Preconditions.checkArgument( + !stagingBranch.equals(targetBranch), + "stagingBranch and targetBranch must be different, but both are '%s'", + stagingBranch); + validateFormatVersion(); + + // Planner (p=1): emits ReadCommands with phase timestamps and watermarks + SingleOutputStreamOperator<ReadCommand> planned = + trigger + .transform( + operatorName(PLANNER_TASK_NAME), + TypeInformation.of(ReadCommand.class), + new EqualityConvertPlanner( + tableName(), + taskName(), + index(), + tableLoader(), + stagingBranch, + targetBranch, + maxSnapshotsPerTrigger)) + .uid(PLANNER_TASK_NAME + uidSuffix()) + .slotSharingGroup(slotSharingGroup()) + .forceNonParallel(); + + // Reader (p=N): reads files, emits IndexCommands + SingleOutputStreamOperator<IndexCommand> indexed = + planned + .rebalance() + .process(new EqualityConvertReader(tableLoader())) + .name(operatorName(READER_TASK_NAME)) + .uid(READER_TASK_NAME + uidSuffix()) + .slotSharingGroup(slotSharingGroup()) + .setParallelism(parallelism()); + + // Worker (p=N): keyed by full PK, phase-aware buffering + SingleOutputStreamOperator<DVPosition> dvPositions = + indexed + .keyBy(IndexCommand::key, TypeInformation.of(SerializedEqualityValues.class)) + .process(new EqualityConvertWorker()) + .name(operatorName(WORKER_TASK_NAME)) + .uid(WORKER_TASK_NAME + uidSuffix()) + .slotSharingGroup(slotSharingGroup()) + .setParallelism(parallelism()); + + // Metadata side output from planner + DataStream<EqualityConvertPlanResult> metadata = + planned.getSideOutput(EqualityConvertPlanner.METADATA_STREAM); + + // DVResolver (p=1): groups positions, collects partition/DV info + SingleOutputStreamOperator<DVMergeCommand> resolved = + dvPositions + .connect(metadata) + .transform( + operatorName(DV_RESOLVER_TASK_NAME), + TypeInformation.of(DVMergeCommand.class), + new EqualityConvertDVResolver( + tableName(), taskName(), index(), tableLoader(), targetBranch)) + .uid(DV_RESOLVER_TASK_NAME + uidSuffix()) + .slotSharingGroup(slotSharingGroup()) + .forceNonParallel(); + + // DVMerger (p=N): writes merged DV files + SingleOutputStreamOperator<DVMergeResult> merged = + resolved + .rebalance() + .transform( + operatorName(DV_MERGER_TASK_NAME), + TypeInformation.of(DVMergeResult.class), + new EqualityConvertDVMerger(tableName(), taskName(), index(), tableLoader())) + .uid(DV_MERGER_TASK_NAME + uidSuffix()) + .slotSharingGroup(slotSharingGroup()) + .setParallelism(parallelism()); + + // Committer (p=1): commits data files + DVs to main. + // Receives EqualityConvertPlanResult directly from the planner (not via DVResolver) so the + // plan result arrives before pipeline watermarks and can absorb them until the commit. + SingleOutputStreamOperator<Trigger> committed = + merged + .connect(metadata) + .transform( + operatorName(COMMIT_TASK_NAME), + TypeInformation.of(Trigger.class), + new EqualityConvertCommitter( + tableName(), taskName(), index(), tableLoader(), targetBranch)) + .uid(COMMIT_TASK_NAME + uidSuffix()) + .slotSharingGroup(slotSharingGroup()) + .forceNonParallel(); + + // Aggregator (p=1): collects errors and emits TaskResult. + // Uses only the committed stream (not trigger.union(committed)) because the + // Committer emits Trigger records. This prevents the trigger's watermark from + // advancing the Aggregator before the pipeline completes a cycle. + return committed + .connect( + planned + .getSideOutput(TaskResultAggregator.ERROR_STREAM) + .union(resolved.getSideOutput(TaskResultAggregator.ERROR_STREAM)) + .union(merged.getSideOutput(TaskResultAggregator.ERROR_STREAM)) + .union(committed.getSideOutput(TaskResultAggregator.ERROR_STREAM))) Review Comment: Should we also union dvPositions? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
