syhily commented on a change in pull request #17452: URL: https://github.com/apache/flink/pull/17452#discussion_r794650147
########## 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) { Review comment: All the committers and writers are handled in `org.apache.flink.streaming.runtime.operators.sink.SinkOperator` and share the same thread. Throwing the exception here would crash the pipeline and restart the application. `TransactionNotFoundException` should be treated as an internal failure cause we can't find the transaction which was created in the writer. This often occurs when the timeout for the transaction is smaller than the checkpoint interval. `CoordinatorNotFoundException` only happens when the client doesn't enable the transaction or the broker doesn't enable the transaction. So we just throw these two transactions which are non-retryable. -- 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]
