You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by "CalvinKirs (via GitHub)" <gi...@apache.org> on 2023/03/03 05:24:00 UTC

[GitHub] [incubator-seatunnel] CalvinKirs commented on a diff in pull request #4235: [Improve][Connector-V2][Doris]Refactor some Doris Sink code as well as support 2pc and cdc

CalvinKirs commented on code in PR #4235:
URL: https://github.com/apache/incubator-seatunnel/pull/4235#discussion_r1124037491


##########
seatunnel-connectors-v2/connector-doris/src/main/java/org/apache/seatunnel/connectors/doris/sink/committer/DorisCommitter.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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.doris.sink.committer;
+
+import org.apache.seatunnel.shade.com.typesafe.config.Config;
+
+import org.apache.seatunnel.api.sink.SinkCommitter;
+import org.apache.seatunnel.connectors.doris.config.DorisConfig;
+import org.apache.seatunnel.connectors.doris.exception.DorisConnectorErrorCode;
+import org.apache.seatunnel.connectors.doris.exception.DorisConnectorException;
+import org.apache.seatunnel.connectors.doris.rest.RestService;
+import org.apache.seatunnel.connectors.doris.sink.HttpPutBuilder;
+import org.apache.seatunnel.connectors.doris.sink.LoadStatus;
+import org.apache.seatunnel.connectors.doris.util.HttpUtil;
+import org.apache.seatunnel.connectors.doris.util.ResponseUtil;
+
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.util.EntityUtils;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/** The committer to commit transaction. */
+@Slf4j
+public class DorisCommitter implements SinkCommitter<DorisCommitInfo> {
+    private static final String COMMIT_PATTERN = "http://%s/api/%s/_stream_load_2pc";
+    private static final int HTTP_TEMPORARY_REDIRECT = 200;
+    private final CloseableHttpClient httpClient;
+    private final DorisConfig dorisConfig;
+    int maxRetry;
+
+    public DorisCommitter(Config pluginConfig) {
+        this(
+                DorisConfig.loadConfig(pluginConfig),
+                DorisConfig.loadConfig(pluginConfig).getMaxRetries(),
+                new HttpUtil().getHttpClient());
+    }
+
+    public DorisCommitter(DorisConfig dorisConfig, int maxRetry, CloseableHttpClient client) {
+        this.dorisConfig = dorisConfig;
+        this.maxRetry = maxRetry;
+        this.httpClient = client;
+    }
+
+    @Override
+    public List<DorisCommitInfo> commit(List<DorisCommitInfo> commitInfos) throws IOException {
+        for (DorisCommitInfo commitInfo : commitInfos) {
+            commitTransaction(commitInfo);
+        }
+        return Collections.emptyList();
+    }
+
+    @Override
+    public void abort(List<DorisCommitInfo> commitInfos) throws IOException {}
+
+    private void commitTransaction(DorisCommitInfo committable)
+            throws IOException, DorisConnectorException {
+        int statusCode = -1;
+        String reasonPhrase = null;
+        int retry = 0;
+        String hostPort = committable.getHostPort();
+        CloseableHttpResponse response = null;
+        while (retry++ <= maxRetry) {
+            HttpPutBuilder putBuilder = new HttpPutBuilder();
+            putBuilder
+                    .setUrl(String.format(COMMIT_PATTERN, hostPort, committable.getDb()))
+                    .baseAuth(dorisConfig.getUsername(), dorisConfig.getPassword())
+                    .addCommonHeader()
+                    .addTxnId(committable.getTxbID())
+                    .setEmptyEntity()
+                    .commit();
+            try {
+                response = httpClient.execute(putBuilder.build());
+            } catch (IOException e) {
+                log.error("commit transaction failed: ", e);
+                hostPort = RestService.getBackend(dorisConfig, log);
+                continue;
+            }
+            statusCode = response.getStatusLine().getStatusCode();
+            reasonPhrase = response.getStatusLine().getReasonPhrase();
+            if (statusCode != HTTP_TEMPORARY_REDIRECT) {
+                log.warn("commit failed with {}, reason {}", hostPort, reasonPhrase);
+                hostPort = RestService.getBackend(dorisConfig, log);
+            } else {
+                break;
+            }
+        }
+
+        if (statusCode != HTTP_TEMPORARY_REDIRECT) {
+            throw new DorisConnectorException(
+                    DorisConnectorErrorCode.STREAM_LOAD_FAILED, reasonPhrase);
+        }
+
+        ObjectMapper mapper = new ObjectMapper();
+        if (response.getEntity() != null) {

Review Comment:
   Beware of NPE



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