Search in sources :

Example 1 with TransactionAbortedException

use of org.apache.kafka.common.errors.TransactionAbortedException in project kafka by apache.

the class Sender method maybeSendAndPollTransactionalRequest.

/**
 * Returns true if a transactional request is sent or polled, or if a FindCoordinator request is enqueued
 */
private boolean maybeSendAndPollTransactionalRequest() {
    if (transactionManager.hasInFlightRequest()) {
        // as long as there are outstanding transactional requests, we simply wait for them to return
        client.poll(retryBackoffMs, time.milliseconds());
        return true;
    }
    if (transactionManager.hasAbortableError() || transactionManager.isAborting()) {
        if (accumulator.hasIncomplete()) {
            // Attempt to get the last error that caused this abort.
            RuntimeException exception = transactionManager.lastError();
            // then this is most likely a case where there was no fatal error.
            if (exception == null) {
                exception = new TransactionAbortedException();
            }
            accumulator.abortUndrainedBatches(exception);
        }
    }
    TransactionManager.TxnRequestHandler nextRequestHandler = transactionManager.nextRequest(accumulator.hasIncomplete());
    if (nextRequestHandler == null)
        return false;
    AbstractRequest.Builder<?> requestBuilder = nextRequestHandler.requestBuilder();
    Node targetNode = null;
    try {
        FindCoordinatorRequest.CoordinatorType coordinatorType = nextRequestHandler.coordinatorType();
        targetNode = coordinatorType != null ? transactionManager.coordinator(coordinatorType) : client.leastLoadedNode(time.milliseconds());
        if (targetNode != null) {
            if (!awaitNodeReady(targetNode, coordinatorType)) {
                log.trace("Target node {} not ready within request timeout, will retry when node is ready.", targetNode);
                maybeFindCoordinatorAndRetry(nextRequestHandler);
                return true;
            }
        } else if (coordinatorType != null) {
            log.trace("Coordinator not known for {}, will retry {} after finding coordinator.", coordinatorType, requestBuilder.apiKey());
            maybeFindCoordinatorAndRetry(nextRequestHandler);
            return true;
        } else {
            log.trace("No nodes available to send requests, will poll and retry when until a node is ready.");
            transactionManager.retry(nextRequestHandler);
            client.poll(retryBackoffMs, time.milliseconds());
            return true;
        }
        if (nextRequestHandler.isRetry())
            time.sleep(nextRequestHandler.retryBackoffMs());
        long currentTimeMs = time.milliseconds();
        ClientRequest clientRequest = client.newClientRequest(targetNode.idString(), requestBuilder, currentTimeMs, true, requestTimeoutMs, nextRequestHandler);
        log.debug("Sending transactional request {} to node {} with correlation ID {}", requestBuilder, targetNode, clientRequest.correlationId());
        client.send(clientRequest, currentTimeMs);
        transactionManager.setInFlightCorrelationId(clientRequest.correlationId());
        client.poll(retryBackoffMs, time.milliseconds());
        return true;
    } catch (IOException e) {
        log.debug("Disconnect from {} while trying to send request {}. Going " + "to back off and retry.", targetNode, requestBuilder, e);
        // We break here so that we pick up the FindCoordinator request immediately.
        maybeFindCoordinatorAndRetry(nextRequestHandler);
        return true;
    }
}
Also used : FindCoordinatorRequest(org.apache.kafka.common.requests.FindCoordinatorRequest) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) Node(org.apache.kafka.common.Node) IOException(java.io.IOException) TransactionAbortedException(org.apache.kafka.common.errors.TransactionAbortedException) ClientRequest(org.apache.kafka.clients.ClientRequest)

Aggregations

IOException (java.io.IOException)1 ClientRequest (org.apache.kafka.clients.ClientRequest)1 Node (org.apache.kafka.common.Node)1 TransactionAbortedException (org.apache.kafka.common.errors.TransactionAbortedException)1 AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)1 FindCoordinatorRequest (org.apache.kafka.common.requests.FindCoordinatorRequest)1