mxm commented on code in PR #15996:
URL: https://github.com/apache/iceberg/pull/15996#discussion_r3124469626


##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/maintenance/operator/EqualityConvertDVMerger.java:
##########
@@ -0,0 +1,176 @@
+/*
+ * 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.operator;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.List;
+import java.util.Map;
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.streaming.api.operators.AbstractStreamOperator;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperator;
+import org.apache.flink.streaming.api.watermark.Watermark;
+import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
+import org.apache.iceberg.DeleteFile;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.deletes.BaseDVFileWriter;
+import org.apache.iceberg.deletes.PositionDeleteIndex;
+import org.apache.iceberg.flink.TableLoader;
+import org.apache.iceberg.io.DeleteWriteResult;
+import org.apache.iceberg.io.IOUtil;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFileFactory;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Parallel DV writer that receives {@link DVMergeCommand}s from the {@link
+ * EqualityConvertDVResolver} and writes merged deletion vector files. Each 
instance processes a
+ * subset of data files (distributed via rebalance) and writes its own Puffin 
file.
+ */
+@Internal
+public class EqualityConvertDVMerger extends 
AbstractStreamOperator<DVMergeResult>
+    implements OneInputStreamOperator<DVMergeCommand, DVMergeResult> {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(EqualityConvertDVMerger.class);
+
+  private final String tableName;
+  private final String taskName;
+  private final int taskIndex;
+  private final TableLoader tableLoader;
+
+  private transient Table table;
+  private transient OutputFileFactory fileFactory;
+  private transient List<DVMergeCommand> bufferedCommands;
+
+  public EqualityConvertDVMerger(
+      String tableName, String taskName, int taskIndex, TableLoader 
tableLoader) {
+    this.tableName = tableName;
+    this.taskName = taskName;
+    this.taskIndex = taskIndex;
+    this.tableLoader = tableLoader;
+  }
+
+  @Override
+  public void open() throws Exception {
+    super.open();
+    if (!tableLoader.isOpen()) {
+      tableLoader.open();
+    }
+
+    table = tableLoader.loadTable();
+    fileFactory =
+        OutputFileFactory.builderFor(table, taskIndex, 
0L).format(FileFormat.PUFFIN).build();
+    bufferedCommands = Lists.newArrayList();
+  }
+
+  @Override
+  public void processElement(StreamRecord<DVMergeCommand> record) {
+    bufferedCommands.add(record.getValue());
+  }
+
+  @Override
+  public void processWatermark(Watermark mark) throws Exception {
+    if (!bufferedCommands.isEmpty()) {
+      try {
+        Map<String, DeleteFile> existingDVs = Maps.newHashMap();
+        for (DVMergeCommand cmd : bufferedCommands) {
+          if (cmd.existingDV() != null) {
+            existingDVs.put(cmd.dataFilePath(), cmd.existingDV());
+          }
+        }
+
+        DeleteWriteResult result;
+        try (BaseDVFileWriter dvWriter =
+            new BaseDVFileWriter(fileFactory, path -> loadPreviousDV(path, 
existingDVs))) {
+
+          for (DVMergeCommand cmd : bufferedCommands) {
+            for (long pos : cmd.positions()) {
+              dvWriter.delete(
+                  cmd.dataFilePath(), pos, table.specs().get(cmd.specId()), 
cmd.partition());
+            }
+          }
+
+          dvWriter.close();
+          result = dvWriter.result();
+        }
+
+        LOG.info(
+            "Wrote {} DV files (rewriting {}) for table {} task {}[{}].",
+            result.deleteFiles().size(),
+            result.rewrittenDeleteFiles().size(),
+            tableName,
+            taskName,
+            taskIndex);
+
+        output.collect(
+            new StreamRecord<>(
+                new DVMergeResult(
+                    Lists.newArrayList(result.deleteFiles()),
+                    Lists.newArrayList(result.rewrittenDeleteFiles()))));
+      } catch (Exception e) {
+        LOG.error(
+            "Failed to write DV files for table {} task {}[{}]", tableName, 
taskName, taskIndex, e);
+        output.collect(TaskResultAggregator.ERROR_STREAM, new 
StreamRecord<>(e));
+        // Signal the committer to abort this cycle rather than committing 
data files without DVs.
+        output.collect(new StreamRecord<>(DVMergeResult.abort()));
+      }
+
+      bufferedCommands.clear();
+    }
+
+    super.processWatermark(mark);
+  }
+
+  @Override
+  public void close() throws Exception {
+    super.close();
+    tableLoader.close();
+  }
+
+  private PositionDeleteIndex loadPreviousDV(
+      String dataFilePath, Map<String, DeleteFile> existingDVs) {
+    DeleteFile existingDV = existingDVs.get(dataFilePath);
+    if (existingDV == null) {
+      return null;
+    }

Review Comment:
   I'm using BaseDeleteLoader now instead of the custom method.



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

Reply via email to