Search in sources :

Example 6 with ClientResponse

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

the class Fetcher method getTopicMetadata.

/**
     * Get metadata for all topics present in Kafka cluster
     *
     * @param request The MetadataRequest to send
     * @param timeout time for which getting topic metadata is attempted
     * @return The map of topics with their partition information
     */
public Map<String, List<PartitionInfo>> getTopicMetadata(MetadataRequest.Builder request, long timeout) {
    // Save the round trip if no topics are requested.
    if (!request.isAllTopics() && request.topics().isEmpty())
        return Collections.emptyMap();
    long start = time.milliseconds();
    long remaining = timeout;
    do {
        RequestFuture<ClientResponse> future = sendMetadataRequest(request);
        client.poll(future, remaining);
        if (future.failed() && !future.isRetriable())
            throw future.exception();
        if (future.succeeded()) {
            MetadataResponse response = (MetadataResponse) future.value().responseBody();
            Cluster cluster = response.cluster();
            Set<String> unauthorizedTopics = cluster.unauthorizedTopics();
            if (!unauthorizedTopics.isEmpty())
                throw new TopicAuthorizationException(unauthorizedTopics);
            boolean shouldRetry = false;
            Map<String, Errors> errors = response.errors();
            if (!errors.isEmpty()) {
                // if there were errors, we need to check whether they were fatal or whether
                // we should just retry
                log.debug("Topic metadata fetch included errors: {}", errors);
                for (Map.Entry<String, Errors> errorEntry : errors.entrySet()) {
                    String topic = errorEntry.getKey();
                    Errors error = errorEntry.getValue();
                    if (error == Errors.INVALID_TOPIC_EXCEPTION)
                        throw new InvalidTopicException("Topic '" + topic + "' is invalid");
                    else if (error == Errors.UNKNOWN_TOPIC_OR_PARTITION)
                        // in the returned map
                        continue;
                    else if (error.exception() instanceof RetriableException)
                        shouldRetry = true;
                    else
                        throw new KafkaException("Unexpected error fetching metadata for topic " + topic, error.exception());
                }
            }
            if (!shouldRetry) {
                HashMap<String, List<PartitionInfo>> topicsPartitionInfos = new HashMap<>();
                for (String topic : cluster.topics()) topicsPartitionInfos.put(topic, cluster.availablePartitionsForTopic(topic));
                return topicsPartitionInfos;
            }
        }
        long elapsed = time.milliseconds() - start;
        remaining = timeout - elapsed;
        if (remaining > 0) {
            long backoff = Math.min(remaining, retryBackoffMs);
            time.sleep(backoff);
            remaining -= backoff;
        }
    } while (remaining > 0);
    throw new TimeoutException("Timeout expired while fetching topic metadata");
}
Also used : ClientResponse(org.apache.kafka.clients.ClientResponse) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Cluster(org.apache.kafka.common.Cluster) Errors(org.apache.kafka.common.protocol.Errors) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) KafkaException(org.apache.kafka.common.KafkaException) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) RetriableException(org.apache.kafka.common.errors.RetriableException) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 7 with ClientResponse

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

the class Sender method sendProduceRequest.

/**
     * Create a produce request from the given record batches
     */
private void sendProduceRequest(long now, int destination, short acks, int timeout, List<ProducerBatch> batches) {
    Map<TopicPartition, MemoryRecords> produceRecordsByPartition = new HashMap<>(batches.size());
    final Map<TopicPartition, ProducerBatch> recordsByPartition = new HashMap<>(batches.size());
    for (ProducerBatch batch : batches) {
        TopicPartition tp = batch.topicPartition;
        produceRecordsByPartition.put(tp, batch.records());
        recordsByPartition.put(tp, batch);
    }
    ProduceRequest.Builder requestBuilder = new ProduceRequest.Builder(acks, timeout, produceRecordsByPartition);
    RequestCompletionHandler callback = new RequestCompletionHandler() {

        public void onComplete(ClientResponse response) {
            handleProduceResponse(response, recordsByPartition, time.milliseconds());
        }
    };
    String nodeId = Integer.toString(destination);
    ClientRequest clientRequest = client.newClientRequest(nodeId, requestBuilder, now, acks != 0, callback);
    client.send(clientRequest, now);
    log.trace("Sent produce request to {}: {}", nodeId, requestBuilder);
}
Also used : ClientResponse(org.apache.kafka.clients.ClientResponse) HashMap(java.util.HashMap) ProduceRequest(org.apache.kafka.common.requests.ProduceRequest) RequestCompletionHandler(org.apache.kafka.clients.RequestCompletionHandler) TopicPartition(org.apache.kafka.common.TopicPartition) ClientRequest(org.apache.kafka.clients.ClientRequest) MemoryRecords(org.apache.kafka.common.record.MemoryRecords)

Example 8 with ClientResponse

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

the class StreamsKafkaClient method createTopics.

/**
     * Create a set of new topics using batch request.
     */
public void createTopics(final Map<InternalTopicConfig, Integer> topicsMap, final int replicationFactor, final long windowChangeLogAdditionalRetention, final MetadataResponse metadata) {
    final Map<String, CreateTopicsRequest.TopicDetails> topicRequestDetails = new HashMap<>();
    for (Map.Entry<InternalTopicConfig, Integer> entry : topicsMap.entrySet()) {
        InternalTopicConfig internalTopicConfig = entry.getKey();
        Integer partitions = entry.getValue();
        final Properties topicProperties = internalTopicConfig.toProperties(windowChangeLogAdditionalRetention);
        final Map<String, String> topicConfig = new HashMap<>();
        for (String key : topicProperties.stringPropertyNames()) {
            topicConfig.put(key, topicProperties.getProperty(key));
        }
        final CreateTopicsRequest.TopicDetails topicDetails = new CreateTopicsRequest.TopicDetails(partitions, (short) replicationFactor, topicConfig);
        topicRequestDetails.put(internalTopicConfig.name(), topicDetails);
    }
    final ClientRequest clientRequest = kafkaClient.newClientRequest(getControllerReadyBrokerId(metadata), new CreateTopicsRequest.Builder(topicRequestDetails, streamsConfig.getInt(StreamsConfig.REQUEST_TIMEOUT_MS_CONFIG)), Time.SYSTEM.milliseconds(), true);
    final ClientResponse clientResponse = sendRequest(clientRequest);
    if (!clientResponse.hasResponse()) {
        throw new StreamsException("Empty response for client request.");
    }
    if (!(clientResponse.responseBody() instanceof CreateTopicsResponse)) {
        throw new StreamsException("Inconsistent response type for internal topic creation request. " + "Expected CreateTopicsResponse but received " + clientResponse.responseBody().getClass().getName());
    }
    final CreateTopicsResponse createTopicsResponse = (CreateTopicsResponse) clientResponse.responseBody();
    for (InternalTopicConfig internalTopicConfig : topicsMap.keySet()) {
        CreateTopicsResponse.Error error = createTopicsResponse.errors().get(internalTopicConfig.name());
        if (!error.is(Errors.NONE) && !error.is(Errors.TOPIC_ALREADY_EXISTS)) {
            throw new StreamsException("Could not create topic: " + internalTopicConfig.name() + " due to " + error.messageWithFallback());
        }
    }
}
Also used : ClientResponse(org.apache.kafka.clients.ClientResponse) CreateTopicsResponse(org.apache.kafka.common.requests.CreateTopicsResponse) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) StreamsException(org.apache.kafka.streams.errors.StreamsException) Properties(java.util.Properties) CreateTopicsRequest(org.apache.kafka.common.requests.CreateTopicsRequest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ClientRequest(org.apache.kafka.clients.ClientRequest)

Example 9 with ClientResponse

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

the class StreamsKafkaClient method sendRequest.

private ClientResponse sendRequest(final ClientRequest clientRequest) {
    kafkaClient.send(clientRequest, Time.SYSTEM.milliseconds());
    final long responseTimeout = Time.SYSTEM.milliseconds() + streamsConfig.getInt(StreamsConfig.REQUEST_TIMEOUT_MS_CONFIG);
    // Poll for the response.
    while (Time.SYSTEM.milliseconds() < responseTimeout) {
        List<ClientResponse> responseList = kafkaClient.poll(streamsConfig.getLong(StreamsConfig.POLL_MS_CONFIG), Time.SYSTEM.milliseconds());
        if (!responseList.isEmpty()) {
            if (responseList.size() > 1) {
                throw new StreamsException("Sent one request but received multiple or no responses.");
            }
            ClientResponse response = responseList.get(0);
            if (response.requestHeader().correlationId() == clientRequest.correlationId()) {
                return response;
            } else {
                throw new StreamsException("Inconsistent response received from the broker " + clientRequest.destination() + ", expected correlation id " + clientRequest.correlationId() + ", but received " + response.requestHeader().correlationId());
            }
        }
    }
    throw new StreamsException("Failed to get response from broker within timeout");
}
Also used : ClientResponse(org.apache.kafka.clients.ClientResponse) StreamsException(org.apache.kafka.streams.errors.StreamsException)

Example 10 with ClientResponse

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

the class StreamsKafkaClient method fetchMetadata.

/**
     * Fetch the metadata for all topics
     */
public MetadataResponse fetchMetadata() {
    final ClientRequest clientRequest = kafkaClient.newClientRequest(getAnyReadyBrokerId(), new MetadataRequest.Builder(null), Time.SYSTEM.milliseconds(), true);
    final ClientResponse clientResponse = sendRequest(clientRequest);
    if (!clientResponse.hasResponse()) {
        throw new StreamsException("Empty response for client request.");
    }
    if (!(clientResponse.responseBody() instanceof MetadataResponse)) {
        throw new StreamsException("Inconsistent response type for internal topic metadata request. " + "Expected MetadataResponse but received " + clientResponse.responseBody().getClass().getName());
    }
    final MetadataResponse metadataResponse = (MetadataResponse) clientResponse.responseBody();
    return metadataResponse;
}
Also used : ClientResponse(org.apache.kafka.clients.ClientResponse) MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) StreamsException(org.apache.kafka.streams.errors.StreamsException) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) ClientRequest(org.apache.kafka.clients.ClientRequest)

Aggregations

ClientResponse (org.apache.kafka.clients.ClientResponse)10 ClientRequest (org.apache.kafka.clients.ClientRequest)5 HashMap (java.util.HashMap)4 StreamsException (org.apache.kafka.streams.errors.StreamsException)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 Node (org.apache.kafka.common.Node)3 TopicPartition (org.apache.kafka.common.TopicPartition)2 HeartbeatResponse (org.apache.kafka.common.requests.HeartbeatResponse)2 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Properties (java.util.Properties)1 Set (java.util.Set)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 MockClient (org.apache.kafka.clients.MockClient)1 RequestCompletionHandler (org.apache.kafka.clients.RequestCompletionHandler)1 Cluster (org.apache.kafka.common.Cluster)1