Guosmilesmile commented on code in PR #12979:
URL: https://github.com/apache/iceberg/pull/12979#discussion_r2115921971


##########
flink/v1.20/flink/src/test/java/org/apache/iceberg/flink/sink/TestCommittableToTableChangeConverter.java:
##########
@@ -0,0 +1,338 @@
+/*
+ * 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.sink;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.UUID;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.core.io.SimpleVersionedSerialization;
+import org.apache.flink.streaming.api.connector.sink2.CommittableMessage;
+import org.apache.flink.streaming.api.connector.sink2.CommittableWithLineage;
+import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
+import org.apache.flink.streaming.util.OneInputStreamOperatorTestHarness;
+import org.apache.flink.streaming.util.ProcessFunctionTestHarnesses;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.DataFiles;
+import org.apache.iceberg.DeleteFile;
+import org.apache.iceberg.FileMetadata;
+import org.apache.iceberg.ManifestFile;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.flink.SimpleDataUtil;
+import org.apache.iceberg.flink.TableLoader;
+import org.apache.iceberg.flink.maintenance.operator.TableChange;
+import org.apache.iceberg.io.WriteResult;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+class TestCommittableToTableChangeConverter {

Review Comment:
   Right ,I will add it 



##########
flink/v1.20/flink/src/test/java/org/apache/iceberg/flink/sink/TestCommittableToTableChangeConverter.java:
##########
@@ -0,0 +1,338 @@
+/*
+ * 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.sink;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.UUID;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.core.io.SimpleVersionedSerialization;
+import org.apache.flink.streaming.api.connector.sink2.CommittableMessage;
+import org.apache.flink.streaming.api.connector.sink2.CommittableWithLineage;
+import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
+import org.apache.flink.streaming.util.OneInputStreamOperatorTestHarness;
+import org.apache.flink.streaming.util.ProcessFunctionTestHarnesses;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.DataFiles;
+import org.apache.iceberg.DeleteFile;
+import org.apache.iceberg.FileMetadata;
+import org.apache.iceberg.ManifestFile;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.flink.SimpleDataUtil;
+import org.apache.iceberg.flink.TableLoader;
+import org.apache.iceberg.flink.maintenance.operator.TableChange;
+import org.apache.iceberg.io.WriteResult;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+class TestCommittableToTableChangeConverter {
+  @TempDir private File tempDir;
+  Table table;
+  TableLoader tableLoader;
+  DataFile dataFile;
+  DataFile dataFile1;
+  DataFile dataFile2;
+  DeleteFile posDeleteFile;
+  DeleteFile eqDeleteFile;
+
+  @BeforeEach
+  public void before() throws Exception {
+    String warehouse = tempDir.getAbsolutePath();
+
+    String tablePath = warehouse.concat("/test");
+    assertThat(new File(tablePath).mkdir()).as("Should create the table path 
correctly.").isTrue();
+    table = SimpleDataUtil.createTable(tablePath, Maps.newHashMap(), false);
+    tableLoader = TableLoader.fromHadoopTable(tablePath);
+    dataFile =
+        DataFiles.builder(table.spec())
+            .withPath("/path/to/data.parquet")
+            .withFileSizeInBytes(100)
+            .withRecordCount(10)
+            .build();
+    dataFile1 =
+        DataFiles.builder(table.spec())
+            .withPath("/path/to/data1.parquet")
+            .withFileSizeInBytes(100)
+            .withRecordCount(10)
+            .build();
+    dataFile2 =
+        DataFiles.builder(table.spec())
+            .withPath("/path/to/data2.parquet")
+            .withFileSizeInBytes(100)
+            .withRecordCount(10)
+            .build();
+    posDeleteFile =
+        FileMetadata.deleteFileBuilder(table.spec())
+            .ofPositionDeletes()
+            .withPath("/path/to/pos-deletes.parquet")
+            .withFileSizeInBytes(50)
+            .withRecordCount(5)
+            .build();
+    eqDeleteFile =
+        FileMetadata.deleteFileBuilder(table.spec())
+            .ofEqualityDeletes(1)
+            .withPath("/path/to/eq-deletes.parquet")
+            .withFileSizeInBytes(30)
+            .withRecordCount(3)
+            .build();
+  }
+
+  @Test
+  public void testConvertWriteResultToTableChange() throws Exception {
+    String flinkJobId = newFlinkJobId();
+    String operatorId = newOperatorUniqueId();
+    ManifestOutputFileFactory factory =
+        FlinkManifestUtil.createOutputFileFactory(
+            () -> table, table.properties(), flinkJobId, operatorId, 1, 1);
+
+    try 
(OneInputStreamOperatorTestHarness<CommittableMessage<IcebergCommittable>, 
TableChange>
+        harness =
+            ProcessFunctionTestHarnesses.forProcessFunction(
+                new CommittableToTableChangeConverter(tableLoader))) {
+      harness.open();
+      WriteResult writeResult =
+          WriteResult.builder()
+              .addDataFiles(dataFile)
+              .addDeleteFiles(posDeleteFile, eqDeleteFile)
+              .build();
+      DeltaManifests deltaManifests =
+          FlinkManifestUtil.writeCompletedFiles(writeResult, () -> 
factory.create(1), table.spec());
+      IcebergCommittable committable =
+          new IcebergCommittable(
+              SimpleVersionedSerialization.writeVersionAndSerialize(
+                  DeltaManifestsSerializer.INSTANCE, deltaManifests),
+              flinkJobId,
+              operatorId,
+              1L);
+      CommittableWithLineage<IcebergCommittable> message =
+          new CommittableWithLineage<>(committable, 1L, 0);
+      harness.processElement(new StreamRecord<>(message));
+      TableChange tableChange = harness.extractOutputValues().get(0);
+      TableChange expectedTableChange =
+          TableChange.builder()
+              .dataFileCount(1)
+              .dataFileSizeInBytes(100)
+              .posDeleteFileCount(1)
+              .posDeleteRecordCount(5)
+              .eqDeleteFileCount(1)
+              .eqDeleteRecordCount(3)
+              .commitCount(1)
+              .build();
+
+      assertThat(tableChange).isEqualTo(expectedTableChange);
+    }
+  }
+
+  @Test
+  public void testConvertReplays() throws Exception {
+    String flinkJobId = newFlinkJobId();
+    String operatorId = newOperatorUniqueId();
+    ManifestOutputFileFactory factory =
+        FlinkManifestUtil.createOutputFileFactory(
+            () -> table, table.properties(), flinkJobId, operatorId, 1, 1);
+
+    try 
(OneInputStreamOperatorTestHarness<CommittableMessage<IcebergCommittable>, 
TableChange>
+        harness =
+            ProcessFunctionTestHarnesses.forProcessFunction(
+                new CommittableToTableChangeConverter(tableLoader))) {
+      harness.open();
+      WriteResult writeResult =
+          WriteResult.builder()
+              .addDataFiles(dataFile)
+              .addDeleteFiles(posDeleteFile, eqDeleteFile)
+              .build();
+      DeltaManifests deltaManifests =
+          FlinkManifestUtil.writeCompletedFiles(writeResult, () -> 
factory.create(1), table.spec());
+      IcebergCommittable committable =
+          new IcebergCommittable(
+              SimpleVersionedSerialization.writeVersionAndSerialize(
+                  DeltaManifestsSerializer.INSTANCE, deltaManifests),
+              flinkJobId,
+              operatorId,
+              1L);
+      CommittableWithLineage<IcebergCommittable> message =
+          new CommittableWithLineage<>(committable, 1L, 0);
+      harness.processElement(new StreamRecord<>(message));
+      harness.processElement(new StreamRecord<>(message));
+      List<TableChange> tableChanges = harness.extractOutputValues();
+      assertThat(tableChanges).hasSize(1);
+      TableChange tableChange = tableChanges.get(0);
+      TableChange expectedTableChange =
+          TableChange.builder()
+              .dataFileCount(1)
+              .dataFileSizeInBytes(100)
+              .posDeleteFileCount(1)
+              .posDeleteRecordCount(5)
+              .eqDeleteFileCount(1)
+              .eqDeleteRecordCount(3)
+              .commitCount(1)
+              .build();
+
+      assertThat(tableChange).isEqualTo(expectedTableChange);
+    }
+  }
+
+  @Test
+  public void testEmptyCommit() throws Exception {
+    try 
(OneInputStreamOperatorTestHarness<CommittableMessage<IcebergCommittable>, 
TableChange>
+        harness =
+            ProcessFunctionTestHarnesses.forProcessFunction(
+                new CommittableToTableChangeConverter(tableLoader))) {
+
+      harness.open();
+      IcebergCommittable emptyCommittable =
+          new IcebergCommittable(new byte[0], "jobId", "operatorId", 1L);
+      CommittableWithLineage<IcebergCommittable> message =
+          new CommittableWithLineage<>(emptyCommittable, 1L, 0);
+      harness.processElement(new StreamRecord<>(message));
+      assertThat(harness.extractOutputValues()).hasSize(0);
+    }
+  }
+
+  @Test
+  public void testManifestDeletion() throws Exception {
+    String flinkJobId = newFlinkJobId();
+    String operatorId = newOperatorUniqueId();
+    ManifestOutputFileFactory factory =
+        FlinkManifestUtil.createOutputFileFactory(
+            () -> table, table.properties(), flinkJobId, operatorId, 1, 1);
+
+    try 
(OneInputStreamOperatorTestHarness<CommittableMessage<IcebergCommittable>, 
TableChange>
+        harness =
+            ProcessFunctionTestHarnesses.forProcessFunction(
+                new CommittableToTableChangeConverter(tableLoader, 0))) {
+
+      harness.open();
+
+      Tuple2<CommittableWithLineage<IcebergCommittable>, DeltaManifests> 
icebergCommittable =
+          createIcebergCommittable(
+              dataFile, posDeleteFile, eqDeleteFile, factory, table, 
flinkJobId, operatorId, 1L);
+
+      harness.processElement(new StreamRecord<>(icebergCommittable.f0));
+
+      // mock checkpoint
+      harness.snapshot(1L, 1000L);
+
+      // check Manifest files are deleted
+      for (ManifestFile manifest : icebergCommittable.f1.manifests()) {
+        assertThat(new File(manifest.path())).doesNotExist();
+      }
+    }
+  }
+
+  @Test
+  public void testManifestOldestDeletion() throws Exception {
+    String flinkJobId = newFlinkJobId();
+    String operatorId = newOperatorUniqueId();
+    ManifestOutputFileFactory factory =
+        FlinkManifestUtil.createOutputFileFactory(
+            () -> table, table.properties(), flinkJobId, operatorId, 1, 1);
+
+    try 
(OneInputStreamOperatorTestHarness<CommittableMessage<IcebergCommittable>, 
TableChange>
+        harness =
+            ProcessFunctionTestHarnesses.forProcessFunction(
+                new CommittableToTableChangeConverter(tableLoader, 2))) {
+      harness.open();
+
+      Tuple2<CommittableWithLineage<IcebergCommittable>, DeltaManifests> 
icebergCommittable =
+          createIcebergCommittable(
+              dataFile, posDeleteFile, eqDeleteFile, factory, table, 
flinkJobId, operatorId, 1L);
+      Tuple2<CommittableWithLineage<IcebergCommittable>, DeltaManifests> 
icebergCommittable1 =
+          createIcebergCommittable(
+              dataFile1, posDeleteFile, eqDeleteFile, factory, table, 
flinkJobId, operatorId, 2L);
+      Tuple2<CommittableWithLineage<IcebergCommittable>, DeltaManifests> 
icebergCommittable2 =
+          createIcebergCommittable(
+              dataFile2, posDeleteFile, eqDeleteFile, factory, table, 
flinkJobId, operatorId, 3L);
+
+      harness.processElement(new StreamRecord<>(icebergCommittable.f0));
+      harness.processElement(new StreamRecord<>(icebergCommittable1.f0));
+      harness.processElement(new StreamRecord<>(icebergCommittable2.f0));
+
+      List<TableChange> tableChanges = harness.extractOutputValues();
+      assertThat(tableChanges).hasSize(3);
+      TableChange tableChange = tableChanges.get(0);
+      TableChange expectedTableChange =
+          TableChange.builder()
+              .dataFileCount(1)
+              .dataFileSizeInBytes(100)
+              .posDeleteFileCount(1)
+              .posDeleteRecordCount(5)
+              .eqDeleteFileCount(1)
+              .eqDeleteRecordCount(3)
+              .commitCount(1)
+              .build();
+
+      assertThat(tableChange).isEqualTo(expectedTableChange);
+
+      // check Manifest files are deleted
+      for (ManifestFile manifest : icebergCommittable.f1.manifests()) {
+        assertThat(new File(manifest.path())).doesNotExist();
+      }

Review Comment:
   Fix 



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to