You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by GitBox <gi...@apache.org> on 2022/08/30 08:50:55 UTC

[GitHub] [incubator-seatunnel] hailin0 commented on a diff in pull request #2555: [Improve][Connector-V2] Refactor the structure of file sink to reduce redundant codes

hailin0 commented on code in PR #2555:
URL: https://github.com/apache/incubator-seatunnel/pull/2555#discussion_r958173553


##########
seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/sink/commit/FileSinkAggregatedCommitter2.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.seatunnel.connectors.seatunnel.file.sink.commit;
+
+import org.apache.seatunnel.api.sink.SinkAggregatedCommitter;
+import org.apache.seatunnel.connectors.seatunnel.file.sink.util.FileSystemUtils;
+
+import org.apache.commons.collections4.CollectionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class FileSinkAggregatedCommitter2 implements SinkAggregatedCommitter<FileCommitInfo2, FileAggregatedCommitInfo2> {
+    private static final Logger LOGGER = LoggerFactory.getLogger(FileSinkAggregatedCommitter2.class);
+
+    @Override
+    public List<FileAggregatedCommitInfo2> commit(List<FileAggregatedCommitInfo2> aggregatedCommitInfo) throws IOException {
+        List<FileAggregatedCommitInfo2> errorAggregatedCommitInfoList = new ArrayList<>();
+        aggregatedCommitInfo.forEach(aggregateCommitInfo -> {

Review Comment:
   rename variable?
   
   aggregatedCommitInfo -> aggregatedCommitInfoList or aggregatedCommitInfos
   aggregateCommitInfo -> aggregatedCommitInfo



##########
seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/sink/BaseFileSinkWriter.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.seatunnel.connectors.seatunnel.file.sink;
+
+import org.apache.seatunnel.api.sink.SinkWriter;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.connectors.seatunnel.file.config.HadoopConf;
+import org.apache.seatunnel.connectors.seatunnel.file.sink.commit.FileCommitInfo2;
+import org.apache.seatunnel.connectors.seatunnel.file.sink.state.FileSinkState2;
+import org.apache.seatunnel.connectors.seatunnel.file.sink.writer.WriteStrategy;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Optional;
+
+public class BaseFileSinkWriter implements SinkWriter<SeaTunnelRow, FileCommitInfo2, FileSinkState2> {
+    private final WriteStrategy writeStrategy;
+    private final HadoopConf hadoopConf;
+    private final SinkWriter.Context context;
+    private final int subTaskIndex;
+    private final String jobId;
+
+    public BaseFileSinkWriter(WriteStrategy writeStrategy, HadoopConf hadoopConf, SinkWriter.Context context, String jobId) {
+        this.writeStrategy = writeStrategy;
+        this.context = context;
+        this.hadoopConf = hadoopConf;
+        this.jobId = jobId;
+        this.subTaskIndex = context.getIndexOfSubtask();
+        writeStrategy.init(hadoopConf, jobId, subTaskIndex);

Review Comment:
   Use `this(writeStrategy, hadoopConf, context, jobId, Collections.EMPTY_LIST)` ?



##########
seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/sink/BaseFileSinkWriter.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.seatunnel.connectors.seatunnel.file.sink;
+
+import org.apache.seatunnel.api.sink.SinkWriter;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.connectors.seatunnel.file.config.HadoopConf;
+import org.apache.seatunnel.connectors.seatunnel.file.sink.commit.FileCommitInfo2;
+import org.apache.seatunnel.connectors.seatunnel.file.sink.state.FileSinkState2;
+import org.apache.seatunnel.connectors.seatunnel.file.sink.writer.WriteStrategy;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Optional;
+
+public class BaseFileSinkWriter implements SinkWriter<SeaTunnelRow, FileCommitInfo2, FileSinkState2> {
+    private final WriteStrategy writeStrategy;
+    private final HadoopConf hadoopConf;
+    private final SinkWriter.Context context;
+    private final int subTaskIndex;
+    private final String jobId;
+
+    public BaseFileSinkWriter(WriteStrategy writeStrategy, HadoopConf hadoopConf, SinkWriter.Context context, String jobId) {
+        this.writeStrategy = writeStrategy;
+        this.context = context;
+        this.hadoopConf = hadoopConf;
+        this.jobId = jobId;
+        this.subTaskIndex = context.getIndexOfSubtask();
+        writeStrategy.init(hadoopConf, jobId, subTaskIndex);
+    }
+
+    public BaseFileSinkWriter(WriteStrategy writeStrategy, HadoopConf hadoopConf, SinkWriter.Context context, String jobId, List<FileSinkState2> fileSinkStates) {
+        this.writeStrategy = writeStrategy;
+        this.context = context;
+        this.hadoopConf = hadoopConf;
+        this.jobId = jobId;
+        this.subTaskIndex = context.getIndexOfSubtask();
+        writeStrategy.init(hadoopConf, jobId, subTaskIndex);
+        if (!fileSinkStates.isEmpty()) {
+            List<String> transactionIds = writeStrategy.getTransactionIdFromStates(fileSinkStates);
+            transactionIds.forEach(writeStrategy::abortPrepare);
+            writeStrategy.beginTransaction(fileSinkStates.get(0).getCheckpointId());
+        }
+    }
+
+    @Override
+    public void write(SeaTunnelRow element) throws IOException {
+        try {
+            writeStrategy.write(element);
+        } catch (Exception e) {
+            throw new RuntimeException("Write data error, please check");

Review Comment:
   Use `throw new RuntimeException(..., e)`?



##########
seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/sink/commit/FileSinkAggregatedCommitter2.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.seatunnel.connectors.seatunnel.file.sink.commit;
+
+import org.apache.seatunnel.api.sink.SinkAggregatedCommitter;
+import org.apache.seatunnel.connectors.seatunnel.file.sink.util.FileSystemUtils;
+
+import org.apache.commons.collections4.CollectionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class FileSinkAggregatedCommitter2 implements SinkAggregatedCommitter<FileCommitInfo2, FileAggregatedCommitInfo2> {
+    private static final Logger LOGGER = LoggerFactory.getLogger(FileSinkAggregatedCommitter2.class);
+
+    @Override
+    public List<FileAggregatedCommitInfo2> commit(List<FileAggregatedCommitInfo2> aggregatedCommitInfo) throws IOException {
+        List<FileAggregatedCommitInfo2> errorAggregatedCommitInfoList = new ArrayList<>();
+        aggregatedCommitInfo.forEach(aggregateCommitInfo -> {
+            try {
+                for (Map.Entry<String, Map<String, String>> entry : aggregateCommitInfo.getTransactionMap().entrySet()) {
+                    for (Map.Entry<String, String> mvFileEntry : entry.getValue().entrySet()) {
+                        // first rename temp file
+                        FileSystemUtils.renameFile(mvFileEntry.getKey(), mvFileEntry.getValue(), true);
+                    }
+                    // second delete transaction directory
+                    FileSystemUtils.deleteFile(entry.getKey());
+                }
+            } catch (Exception e) {
+                LOGGER.error("commit aggregateCommitInfo error ", e);
+                errorAggregatedCommitInfoList.add(aggregateCommitInfo);
+            }
+        });
+        return errorAggregatedCommitInfoList;
+    }
+
+    /**
+     * The logic about how to combine commit message.
+     *
+     * @param commitInfos The list of commit message.
+     * @return The commit message after combine.
+     */
+    @Override
+    public FileAggregatedCommitInfo2 combine(List<FileCommitInfo2> commitInfos) {
+        if (commitInfos == null || commitInfos.size() == 0) {
+            return null;
+        }
+        Map<String, Map<String, String>> aggregateCommitInfo = new HashMap<>();
+        Map<String, List<String>> partitionDirAndValuesMap = new HashMap<>();
+        commitInfos.forEach(commitInfo -> {
+            Map<String, String> needMoveFileMap = aggregateCommitInfo.computeIfAbsent(commitInfo.getTransactionDir(), k -> new HashMap<>());
+            needMoveFileMap.putAll(commitInfo.getNeedMoveFiles());
+            Set<Map.Entry<String, List<String>>> entries = commitInfo.getPartitionDirAndValuesMap().entrySet();
+            if (!CollectionUtils.isEmpty(entries)) {

Review Comment:
   Want to check both null and empty? 
   this call `getPartitionDirAndValuesMap().entrySet()` maybe throw an NPE
   
   Or, use `commitInfo.getPartitionDirAndValuesMap() != null && !commitInfo.getPartitionDirAndValuesMap().isEmpty()`



-- 
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-unsubscribe@seatunnel.apache.org

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