Search in sources :

Example 11 with ClientRequest

use of org.apache.kafka.clients.ClientRequest in project apache-kafka-on-k8s by banzaicloud.

the class Sender method maybeSendTransactionalRequest.

private boolean maybeSendTransactionalRequest(long now) {
    if (transactionManager.isCompleting() && accumulator.hasIncomplete()) {
        if (transactionManager.isAborting())
            accumulator.abortUndrainedBatches(new KafkaException("Failing batch since transaction was aborted"));
        // be correct which would lead to an OutOfSequenceException.
        if (!accumulator.flushInProgress())
            accumulator.beginFlush();
    }
    TransactionManager.TxnRequestHandler nextRequestHandler = transactionManager.nextRequestHandler(accumulator.hasIncomplete());
    if (nextRequestHandler == null)
        return false;
    AbstractRequest.Builder<?> requestBuilder = nextRequestHandler.requestBuilder();
    while (running) {
        Node targetNode = null;
        try {
            if (nextRequestHandler.needsCoordinator()) {
                targetNode = transactionManager.coordinator(nextRequestHandler.coordinatorType());
                if (targetNode == null) {
                    transactionManager.lookupCoordinator(nextRequestHandler);
                    break;
                }
                if (!NetworkClientUtils.awaitReady(client, targetNode, time, requestTimeout)) {
                    transactionManager.lookupCoordinator(nextRequestHandler);
                    break;
                }
            } else {
                targetNode = awaitLeastLoadedNodeReady(requestTimeout);
            }
            if (targetNode != null) {
                if (nextRequestHandler.isRetry())
                    time.sleep(nextRequestHandler.retryBackoffMs());
                ClientRequest clientRequest = client.newClientRequest(targetNode.idString(), requestBuilder, now, true, nextRequestHandler);
                transactionManager.setInFlightTransactionalRequestCorrelationId(clientRequest.correlationId());
                log.debug("Sending transactional request {} to node {}", requestBuilder, targetNode);
                client.send(clientRequest, now);
                return true;
            }
        } catch (IOException e) {
            log.debug("Disconnect from {} while trying to send request {}. Going " + "to back off and retry", targetNode, requestBuilder);
            if (nextRequestHandler.needsCoordinator()) {
                // We break here so that we pick up the FindCoordinator request immediately.
                transactionManager.lookupCoordinator(nextRequestHandler);
                break;
            }
        }
        time.sleep(retryBackoffMs);
        metadata.requestUpdate();
    }
    transactionManager.retry(nextRequestHandler);
    return true;
}
Also used : AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) Node(org.apache.kafka.common.Node) KafkaException(org.apache.kafka.common.KafkaException) IOException(java.io.IOException) ClientRequest(org.apache.kafka.clients.ClientRequest)

Example 12 with ClientRequest

use of org.apache.kafka.clients.ClientRequest in project apache-kafka-on-k8s by banzaicloud.

the class Sender method sendAndAwaitInitProducerIdRequest.

private ClientResponse sendAndAwaitInitProducerIdRequest(Node node) throws IOException {
    String nodeId = node.idString();
    InitProducerIdRequest.Builder builder = new InitProducerIdRequest.Builder(null);
    ClientRequest request = client.newClientRequest(nodeId, builder, time.milliseconds(), true, null);
    return NetworkClientUtils.sendAndReceive(client, request, time);
}
Also used : InitProducerIdRequest(org.apache.kafka.common.requests.InitProducerIdRequest) ClientRequest(org.apache.kafka.clients.ClientRequest)

Example 13 with ClientRequest

use of org.apache.kafka.clients.ClientRequest in project kafka by apache.

the class ConsumerNetworkClient method failExpiredRequests.

private void failExpiredRequests(long now) {
    // clear all expired unsent requests and fail their corresponding futures
    Collection<ClientRequest> expiredRequests = unsent.removeExpiredRequests(now);
    for (ClientRequest request : expiredRequests) {
        RequestFutureCompletionHandler handler = (RequestFutureCompletionHandler) request.callback();
        handler.onFailure(new TimeoutException("Failed to send request after " + request.requestTimeoutMs() + " ms."));
    }
}
Also used : ClientRequest(org.apache.kafka.clients.ClientRequest) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 14 with ClientRequest

use of org.apache.kafka.clients.ClientRequest in project kafka by apache.

the class ConsumerNetworkClient method send.

/**
 * Send a new request. Note that the request is not actually transmitted on the
 * network until one of the {@link #poll(Timer)} variants is invoked. At this
 * point the request will either be transmitted successfully or will fail.
 * Use the returned future to obtain the result of the send. Note that there is no
 * need to check for disconnects explicitly on the {@link ClientResponse} object;
 * instead, the future will be failed with a {@link DisconnectException}.
 *
 * @param node The destination of the request
 * @param requestBuilder A builder for the request payload
 * @param requestTimeoutMs Maximum time in milliseconds to await a response before disconnecting the socket and
 *                         cancelling the request. The request may be cancelled sooner if the socket disconnects
 *                         for any reason.
 * @return A future which indicates the result of the send.
 */
public RequestFuture<ClientResponse> send(Node node, AbstractRequest.Builder<?> requestBuilder, int requestTimeoutMs) {
    long now = time.milliseconds();
    RequestFutureCompletionHandler completionHandler = new RequestFutureCompletionHandler();
    ClientRequest clientRequest = client.newClientRequest(node.idString(), requestBuilder, now, true, requestTimeoutMs, completionHandler);
    unsent.put(node, clientRequest);
    // wakeup the client in case it is blocking in poll so that we can send the queued request
    client.wakeup();
    return completionHandler.future;
}
Also used : ClientRequest(org.apache.kafka.clients.ClientRequest)

Example 15 with ClientRequest

use of org.apache.kafka.clients.ClientRequest in project kafka by apache.

the class ConsumerNetworkClient method trySend.

// Visible for testing
long trySend(long now) {
    long pollDelayMs = maxPollTimeoutMs;
    // send any requests that can be sent now
    for (Node node : unsent.nodes()) {
        Iterator<ClientRequest> iterator = unsent.requestIterator(node);
        if (iterator.hasNext())
            pollDelayMs = Math.min(pollDelayMs, client.pollDelayMs(node, now));
        while (iterator.hasNext()) {
            ClientRequest request = iterator.next();
            if (client.ready(node, now)) {
                client.send(request, now);
                iterator.remove();
            } else {
                // try next node when current node is not ready
                break;
            }
        }
    }
    return pollDelayMs;
}
Also used : Node(org.apache.kafka.common.Node) ClientRequest(org.apache.kafka.clients.ClientRequest)

Aggregations

ClientRequest (org.apache.kafka.clients.ClientRequest)38 Node (org.apache.kafka.common.Node)25 Test (org.junit.jupiter.api.Test)11 HashMap (java.util.HashMap)8 MockClient (org.apache.kafka.clients.MockClient)8 Cluster (org.apache.kafka.common.Cluster)8 LinkedHashMap (java.util.LinkedHashMap)7 ClientResponse (org.apache.kafka.clients.ClientResponse)7 Test (org.junit.Test)7 ApiVersions (org.apache.kafka.clients.ApiVersions)6 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)6 MetricName (org.apache.kafka.common.MetricName)6 Sensor (org.apache.kafka.common.metrics.Sensor)6 ByteBuffer (java.nio.ByteBuffer)5 NetworkClient (org.apache.kafka.clients.NetworkClient)5 NodeApiVersions (org.apache.kafka.clients.NodeApiVersions)5 TopicPartition (org.apache.kafka.common.TopicPartition)5 KafkaMetric (org.apache.kafka.common.metrics.KafkaMetric)5 NetworkReceive (org.apache.kafka.common.network.NetworkReceive)5 AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)5