syhily commented on a change in pull request #17452:
URL: https://github.com/apache/flink/pull/17452#discussion_r794637804



##########
File path: 
flink-connectors/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/sink/committer/PulsarCommitter.java
##########
@@ -0,0 +1,112 @@
+/*
+ * 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.flink.connector.pulsar.sink.committer;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.connector.sink.Committer;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.pulsar.sink.PulsarSink;
+import org.apache.flink.connector.pulsar.sink.config.SinkConfiguration;
+
+import org.apache.pulsar.client.api.PulsarClient;
+import org.apache.pulsar.client.api.transaction.TransactionCoordinatorClient;
+import 
org.apache.pulsar.client.api.transaction.TransactionCoordinatorClientException;
+import 
org.apache.pulsar.client.api.transaction.TransactionCoordinatorClientException.CoordinatorNotFoundException;
+import 
org.apache.pulsar.client.api.transaction.TransactionCoordinatorClientException.TransactionNotFoundException;
+import org.apache.pulsar.client.api.transaction.TxnID;
+import org.apache.pulsar.client.impl.PulsarClientImpl;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import static java.util.Collections.emptyList;
+import static 
org.apache.flink.connector.pulsar.common.config.PulsarConfigUtils.createClient;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * Committer implementation for {@link PulsarSink}.
+ *
+ * <p>The committer is responsible to finalize the Pulsar transactions by 
committing them.
+ */
+@Internal
+public class PulsarCommitter implements Committer<PulsarCommittable>, 
Closeable {
+    private static final Logger LOG = 
LoggerFactory.getLogger(PulsarCommitter.class);
+
+    private final SinkConfiguration sinkConfiguration;
+
+    private PulsarClient pulsarClient;
+    private TransactionCoordinatorClient coordinatorClient;
+
+    public PulsarCommitter(SinkConfiguration sinkConfiguration) {
+        this.sinkConfiguration = sinkConfiguration;
+    }
+
+    @Override
+    public List<PulsarCommittable> commit(List<PulsarCommittable> committables)
+            throws IOException, InterruptedException {
+        if (committables == null || committables.isEmpty()) {
+            return emptyList();
+        }
+
+        List<PulsarCommittable> retryableCommittables = new ArrayList<>();
+        for (PulsarCommittable committable : committables) {
+            TxnID txnID = committable.getTxnID();
+            String topic = committable.getTopic();
+
+            LOG.debug("Start committing the transaction {} for topic {}", 
txnID, topic);
+            try {
+                coordinatorClient.commit(txnID);
+            } catch (TransactionNotFoundException | 
CoordinatorNotFoundException e) {
+                throw e;
+            } catch (TransactionCoordinatorClientException e) {
+                LOG.error("Unable to commit transaction {} for topic {}", 
txnID, topic);
+                retryableCommittables.add(committable);
+            }
+        }
+        return retryableCommittables;
+    }
+
+    /**
+     * Lazy initialize this backend Pulsar client. This committer may not be 
used in {@link
+     * DeliveryGuarantee#NONE} and {@link DeliveryGuarantee#AT_LEAST_ONCE}. So 
we couldn't create
+     * the Pulsar client immediately.
+     */
+    private TransactionCoordinatorClient transactionCoordinatorClient() {

Review comment:
       This is a mistake from my side. This method should be used in the 
`commit` 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]


Reply via email to