Search in sources :

Example 1 with Transaction

use of org.apache.pulsar.client.api.transaction.Transaction in project flink by apache.

the class TopicProducerRegister method abortTransactions.

/**
 * Abort the existed transactions. This method would be used when closing PulsarWriter.
 */
private void abortTransactions() {
    if (transactionRegister.isEmpty()) {
        return;
    }
    TransactionCoordinatorClient coordinatorClient = ((PulsarClientImpl) pulsarClient).getTcClient();
    // This null check is used for making sure transaction is enabled in client.
    checkNotNull(coordinatorClient);
    try (Closer closer = Closer.create()) {
        for (Transaction transaction : transactionRegister.values()) {
            TxnID txnID = transaction.getTxnID();
            closer.register(() -> coordinatorClient.abort(txnID));
        }
        clearTransactions();
    } catch (IOException e) {
        throw new FlinkRuntimeException(e);
    }
}
Also used : Closer(org.apache.flink.shaded.guava30.com.google.common.io.Closer) TxnID(org.apache.pulsar.client.api.transaction.TxnID) PulsarTransactionUtils.createTransaction(org.apache.flink.connector.pulsar.common.utils.PulsarTransactionUtils.createTransaction) Transaction(org.apache.pulsar.client.api.transaction.Transaction) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) IOException(java.io.IOException) TransactionCoordinatorClient(org.apache.pulsar.client.api.transaction.TransactionCoordinatorClient)

Example 2 with Transaction

use of org.apache.pulsar.client.api.transaction.Transaction in project flink by apache.

the class TopicProducerRegister method createMessageBuilder.

/**
 * Create a TypedMessageBuilder which could be sent to Pulsar directly. First, we would create a
 * topic-related producer or use a cached instead. Then we would try to find a topic-related
 * transaction. We would generate a transaction instance if there is no transaction. Finally, we
 * create the message builder and put the element into it.
 */
public <T> TypedMessageBuilder<T> createMessageBuilder(String topic, Schema<T> schema) {
    Producer<T> producer = getOrCreateProducer(topic, schema);
    DeliveryGuarantee deliveryGuarantee = sinkConfiguration.getDeliveryGuarantee();
    if (deliveryGuarantee == DeliveryGuarantee.EXACTLY_ONCE) {
        Transaction transaction = getOrCreateTransaction(topic);
        return producer.newMessage(transaction);
    } else {
        return producer.newMessage();
    }
}
Also used : PulsarTransactionUtils.createTransaction(org.apache.flink.connector.pulsar.common.utils.PulsarTransactionUtils.createTransaction) Transaction(org.apache.pulsar.client.api.transaction.Transaction) DeliveryGuarantee(org.apache.flink.connector.base.DeliveryGuarantee)

Aggregations

PulsarTransactionUtils.createTransaction (org.apache.flink.connector.pulsar.common.utils.PulsarTransactionUtils.createTransaction)2 Transaction (org.apache.pulsar.client.api.transaction.Transaction)2 IOException (java.io.IOException)1 DeliveryGuarantee (org.apache.flink.connector.base.DeliveryGuarantee)1 Closer (org.apache.flink.shaded.guava30.com.google.common.io.Closer)1 FlinkRuntimeException (org.apache.flink.util.FlinkRuntimeException)1 TransactionCoordinatorClient (org.apache.pulsar.client.api.transaction.TransactionCoordinatorClient)1 TxnID (org.apache.pulsar.client.api.transaction.TxnID)1 PulsarClientImpl (org.apache.pulsar.client.impl.PulsarClientImpl)1