stevenzwu commented on code in PR #4904:
URL: https://github.com/apache/iceberg/pull/4904#discussion_r951004617


##########
flink/v1.15/flink/src/main/java/org/apache/iceberg/flink/sink/writer/StreamWriter.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.writer;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.List;
+import org.apache.flink.api.connector.sink2.SinkWriter;
+import org.apache.flink.api.connector.sink2.StatefulSink.StatefulSinkWriter;
+import org.apache.flink.api.connector.sink2.TwoPhaseCommittingSink;
+import org.apache.flink.table.data.RowData;
+import org.apache.iceberg.flink.sink.TaskWriterFactory;
+import org.apache.iceberg.flink.sink.committer.FilesCommittable;
+import org.apache.iceberg.io.TaskWriter;
+import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+
+public class StreamWriter
+    implements StatefulSinkWriter<RowData, StreamWriterState>,
+        SinkWriter<RowData>,
+        TwoPhaseCommittingSink.PrecommittingSinkWriter<RowData, 
FilesCommittable> {
+  private static final long serialVersionUID = 1L;
+
+  private final String fullTableName;
+  private final TaskWriterFactory<RowData> taskWriterFactory;
+  private transient TaskWriter<RowData> writer;
+  private transient int subTaskId;
+  private final List<FilesCommittable> writeResultsState = 
Lists.newArrayList();
+
+  public StreamWriter(
+      String fullTableName,
+      TaskWriterFactory<RowData> taskWriterFactory,
+      int subTaskId,
+      int numberOfParallelSubtasks) {
+    this.fullTableName = fullTableName;
+    this.subTaskId = subTaskId;
+
+    this.taskWriterFactory = taskWriterFactory;
+    // Initialize the task writer factory.
+    taskWriterFactory.initialize(numberOfParallelSubtasks, subTaskId);
+    // Initialize the task writer.
+    this.writer = taskWriterFactory.create();
+  }
+
+  @Override
+  public void write(RowData element, Context context) throws IOException, 
InterruptedException {
+    writer.write(element);
+  }
+
+  @Override
+  public void flush(boolean endOfInput) throws IOException {}
+
+  @Override
+  public void close() throws Exception {
+    if (writer != null) {
+      writer.close();
+      writer = null;
+    }
+  }
+
+  @Override
+  public String toString() {
+    return MoreObjects.toStringHelper(this)
+        .add("table_name", fullTableName)
+        .add("subtask_id", subTaskId)
+        .toString();
+  }
+
+  @Override
+  public Collection<FilesCommittable> prepareCommit() throws IOException {
+    writeResultsState.add(new FilesCommittable(writer.complete()));
+    this.writer = taskWriterFactory.create();
+    return writeResultsState;
+  }
+
+  @Override
+  public List<StreamWriterState> snapshotState(long checkpointId) {
+    List<StreamWriterState> state = Lists.newArrayList();
+    state.add(new StreamWriterState(Lists.newArrayList(writeResultsState)));

Review Comment:
   Here is the how that Iceberg sink works today and how I hope that it should 
continue to work with v2 sink interface. Writers flushed the data files and 
send `WriteResult` to the global committer. The global committer checkpoints 
the `Manifest` file for fault tolerance/exactly-once.
   
   I understand that `WriteResult` only contains the metadata. It can still be 
substantial in certain use cases. E.g.,
   - writer parallelism is 1,000
   - Each writer writes to 100 files (e.g. bucketing or hourly partition by 
event time)
   - table has many columns. with column-level stats (min, max, count, null 
etc.). it would be significant metadata bytes per data file. say there are 200 
columns, and each columns stats take up 100 bytes. that is 20 KB per file.
   - each checkpoint would have size of 20 KB x 100 x 1,000. that is 2 GB.
   - if checkpoint interval is 5 minutes and commit can't succeed for 10 hours, 
we are talking about 120 checkpoints. Then Iceberg sink would accumulate 240 GB 
of state.
   
   I know this is the worst-case scenario, but it is the motivation behind the 
current IcebergSink design where the global committer only checkpoint one 
`ManifestFile` per checkpoint cycle.



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