Search in sources :

Example 1 with TxnID

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

the class PulsarCommitter method commit.

@Override
public void commit(Collection<CommitRequest<PulsarCommittable>> requests) throws IOException, InterruptedException {
    TransactionCoordinatorClient client = transactionCoordinatorClient();
    for (CommitRequest<PulsarCommittable> request : requests) {
        PulsarCommittable committable = request.getCommittable();
        TxnID txnID = committable.getTxnID();
        String topic = committable.getTopic();
        LOG.debug("Start committing the Pulsar transaction {} for topic {}", txnID, topic);
        try {
            client.commit(txnID);
        } catch (TransactionCoordinatorClientException e) {
            // This is a known bug for Pulsar Transaction.
            // We have to use instanceof instead of catching them.
            TransactionCoordinatorClientException ex = PulsarTransactionUtils.unwrap(e);
            if (ex instanceof CoordinatorNotFoundException) {
                LOG.error("We couldn't find the Transaction Coordinator from Pulsar broker {}. " + "Check your broker configuration.", committable, ex);
                request.signalFailedWithKnownReason(ex);
            } else if (ex instanceof InvalidTxnStatusException) {
                LOG.error("Unable to commit transaction ({}) because it's in an invalid state. " + "Most likely the transaction has been aborted for some reason. " + "Please check the Pulsar broker logs for more details.", committable, ex);
                request.signalAlreadyCommitted();
            } else if (ex instanceof TransactionNotFoundException) {
                if (request.getNumberOfRetries() == 0) {
                    LOG.error("Unable to commit transaction ({}) because it's not found on Pulsar broker. " + "Most likely the checkpoint interval exceed the transaction timeout.", committable, ex);
                    request.signalFailedWithKnownReason(ex);
                } else {
                    LOG.warn("We can't find the transaction {} after {} retry committing. " + "This may mean that the transaction have been committed in previous but failed with timeout. " + "So we just mark it as committed.", txnID, request.getNumberOfRetries());
                    request.signalAlreadyCommitted();
                }
            } else if (ex instanceof MetaStoreHandlerNotExistsException) {
                LOG.error("We can't find the meta store handler by the mostSigBits from TxnID {}. " + "Did you change the metadata for topic {}?", committable, TRANSACTION_COORDINATOR_ASSIGN, ex);
                request.signalFailedWithKnownReason(ex);
            } else {
                LOG.error("Encountered retriable exception while committing transaction {} for topic {}.", committable, topic, ex);
                int maxRecommitTimes = sinkConfiguration.getMaxRecommitTimes();
                if (request.getNumberOfRetries() < maxRecommitTimes) {
                    request.retryLater();
                } else {
                    String message = String.format("Failed to commit transaction %s after retrying %d times", txnID, maxRecommitTimes);
                    request.signalFailedWithKnownReason(new FlinkRuntimeException(message, ex));
                }
            }
        } catch (Exception e) {
            LOG.error("Transaction ({}) encountered unknown error and data could be potentially lost.", committable, e);
            request.signalFailedWithUnknownReason(e);
        }
    }
}
Also used : InvalidTxnStatusException(org.apache.pulsar.client.api.transaction.TransactionCoordinatorClientException.InvalidTxnStatusException) TransactionNotFoundException(org.apache.pulsar.client.api.transaction.TransactionCoordinatorClientException.TransactionNotFoundException) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) MetaStoreHandlerNotExistsException(org.apache.pulsar.client.api.transaction.TransactionCoordinatorClientException.MetaStoreHandlerNotExistsException) TransactionCoordinatorClientException(org.apache.pulsar.client.api.transaction.TransactionCoordinatorClientException) IOException(java.io.IOException) InvalidTxnStatusException(org.apache.pulsar.client.api.transaction.TransactionCoordinatorClientException.InvalidTxnStatusException) CoordinatorNotFoundException(org.apache.pulsar.client.api.transaction.TransactionCoordinatorClientException.CoordinatorNotFoundException) TransactionNotFoundException(org.apache.pulsar.client.api.transaction.TransactionCoordinatorClientException.TransactionNotFoundException) TxnID(org.apache.pulsar.client.api.transaction.TxnID) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) MetaStoreHandlerNotExistsException(org.apache.pulsar.client.api.transaction.TransactionCoordinatorClientException.MetaStoreHandlerNotExistsException) TransactionCoordinatorClientException(org.apache.pulsar.client.api.transaction.TransactionCoordinatorClientException) TransactionCoordinatorClient(org.apache.pulsar.client.api.transaction.TransactionCoordinatorClient) CoordinatorNotFoundException(org.apache.pulsar.client.api.transaction.TransactionCoordinatorClientException.CoordinatorNotFoundException)

Example 2 with TxnID

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

the class TopicProducerRegister method prepareCommit.

/**
 * Convert the transactions into a committable list for Pulsar Committer. The transactions would
 * be removed until Flink triggered a checkpoint.
 */
public List<PulsarCommittable> prepareCommit() {
    List<PulsarCommittable> committables = new ArrayList<>(transactionRegister.size());
    transactionRegister.forEach((topic, transaction) -> {
        TxnID txnID = transaction.getTxnID();
        PulsarCommittable committable = new PulsarCommittable(txnID, topic);
        committables.add(committable);
    });
    clearTransactions();
    return committables;
}
Also used : PulsarCommittable(org.apache.flink.connector.pulsar.sink.committer.PulsarCommittable) TxnID(org.apache.pulsar.client.api.transaction.TxnID) ArrayList(java.util.ArrayList)

Example 3 with TxnID

use of org.apache.pulsar.client.api.transaction.TxnID 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 4 with TxnID

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

the class PulsarCommittableSerializer method deserialize.

@Override
public PulsarCommittable deserialize(int version, byte[] serialized) throws IOException {
    try (final ByteArrayInputStream bais = new ByteArrayInputStream(serialized);
        final DataInputStream in = new DataInputStream(bais)) {
        long mostSigBits = in.readLong();
        long leastSigBits = in.readLong();
        TxnID txnID = new TxnID(mostSigBits, leastSigBits);
        String topic = in.readUTF();
        return new PulsarCommittable(txnID, topic);
    }
}
Also used : TxnID(org.apache.pulsar.client.api.transaction.TxnID) ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream)

Example 5 with TxnID

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

the class PulsarUnorderedSourceReader method snapshotState.

@Override
public List<PulsarPartitionSplit> snapshotState(long checkpointId) {
    LOG.debug("Trigger the new transaction for downstream readers.");
    List<PulsarPartitionSplit> splits = ((PulsarUnorderedFetcherManager<OUT>) splitFetcherManager).snapshotState(checkpointId);
    if (coordinatorClient != null) {
        // Snapshot the transaction status and commit it after checkpoint finished.
        List<TxnID> txnIDs = transactionsToCommit.computeIfAbsent(checkpointId, id -> new ArrayList<>());
        for (PulsarPartitionSplit split : splits) {
            TxnID uncommittedTransactionId = split.getUncommittedTransactionId();
            if (uncommittedTransactionId != null) {
                txnIDs.add(uncommittedTransactionId);
            }
        }
    }
    return splits;
}
Also used : TxnID(org.apache.pulsar.client.api.transaction.TxnID) PulsarUnorderedFetcherManager(org.apache.flink.connector.pulsar.source.reader.fetcher.PulsarUnorderedFetcherManager) PulsarPartitionSplit(org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit)

Aggregations

TxnID (org.apache.pulsar.client.api.transaction.TxnID)13 TransactionCoordinatorClient (org.apache.pulsar.client.api.transaction.TransactionCoordinatorClient)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 SortedMap (java.util.SortedMap)2 TreeMap (java.util.TreeMap)2 PulsarCommittable (org.apache.flink.connector.pulsar.sink.committer.PulsarCommittable)2 PulsarPartitionSplitState (org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplitState)2 FlinkRuntimeException (org.apache.flink.util.FlinkRuntimeException)2 MessageId (org.apache.pulsar.client.api.MessageId)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 List (java.util.List)1 PulsarTransactionUtils.createTransaction (org.apache.flink.connector.pulsar.common.utils.PulsarTransactionUtils.createTransaction)1 SinkConfiguration (org.apache.flink.connector.pulsar.sink.config.SinkConfiguration)1 StopCursor (org.apache.flink.connector.pulsar.source.enumerator.cursor.StopCursor)1 TopicPartition (org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition)1