Search in sources :

Example 1 with DescribeProducersResponse

use of org.apache.kafka.common.requests.DescribeProducersResponse in project kafka by apache.

the class DescribeProducersHandler method handleResponse.

@Override
public ApiResult<TopicPartition, PartitionProducerState> handleResponse(Node broker, Set<TopicPartition> keys, AbstractResponse abstractResponse) {
    DescribeProducersResponse response = (DescribeProducersResponse) abstractResponse;
    Map<TopicPartition, PartitionProducerState> completed = new HashMap<>();
    Map<TopicPartition, Throwable> failed = new HashMap<>();
    List<TopicPartition> unmapped = new ArrayList<>();
    for (DescribeProducersResponseData.TopicResponse topicResponse : response.data().topics()) {
        for (DescribeProducersResponseData.PartitionResponse partitionResponse : topicResponse.partitions()) {
            TopicPartition topicPartition = new TopicPartition(topicResponse.name(), partitionResponse.partitionIndex());
            Errors error = Errors.forCode(partitionResponse.errorCode());
            if (error != Errors.NONE) {
                ApiError apiError = new ApiError(error, partitionResponse.errorMessage());
                handlePartitionError(topicPartition, apiError, failed, unmapped);
                continue;
            }
            List<ProducerState> activeProducers = partitionResponse.activeProducers().stream().map(activeProducer -> {
                OptionalLong currentTransactionFirstOffset = activeProducer.currentTxnStartOffset() < 0 ? OptionalLong.empty() : OptionalLong.of(activeProducer.currentTxnStartOffset());
                OptionalInt coordinatorEpoch = activeProducer.coordinatorEpoch() < 0 ? OptionalInt.empty() : OptionalInt.of(activeProducer.coordinatorEpoch());
                return new ProducerState(activeProducer.producerId(), activeProducer.producerEpoch(), activeProducer.lastSequence(), activeProducer.lastTimestamp(), coordinatorEpoch, currentTransactionFirstOffset);
            }).collect(Collectors.toList());
            completed.put(topicPartition, new PartitionProducerState(activeProducers));
        }
    }
    return new ApiResult<>(completed, failed, unmapped);
}
Also used : DescribeProducersOptions(org.apache.kafka.clients.admin.DescribeProducersOptions) ProducerState(org.apache.kafka.clients.admin.ProducerState) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) HashMap(java.util.HashMap) DescribeProducersRequest(org.apache.kafka.common.requests.DescribeProducersRequest) OptionalInt(java.util.OptionalInt) ApiError(org.apache.kafka.common.requests.ApiError) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) OptionalLong(java.util.OptionalLong) DescribeProducersResponse(org.apache.kafka.common.requests.DescribeProducersResponse) LogContext(org.apache.kafka.common.utils.LogContext) Map(java.util.Map) TopicPartition(org.apache.kafka.common.TopicPartition) PartitionProducerState(org.apache.kafka.clients.admin.DescribeProducersResult.PartitionProducerState) Logger(org.slf4j.Logger) DescribeProducersRequestData(org.apache.kafka.common.message.DescribeProducersRequestData) Collection(java.util.Collection) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) Set(java.util.Set) Collectors(java.util.stream.Collectors) CollectionUtils(org.apache.kafka.common.utils.CollectionUtils) List(java.util.List) DescribeProducersResponseData(org.apache.kafka.common.message.DescribeProducersResponseData) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) Errors(org.apache.kafka.common.protocol.Errors) Node(org.apache.kafka.common.Node) Collections(java.util.Collections) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PartitionProducerState(org.apache.kafka.clients.admin.DescribeProducersResult.PartitionProducerState) OptionalInt(java.util.OptionalInt) DescribeProducersResponse(org.apache.kafka.common.requests.DescribeProducersResponse) DescribeProducersResponseData(org.apache.kafka.common.message.DescribeProducersResponseData) Errors(org.apache.kafka.common.protocol.Errors) TopicPartition(org.apache.kafka.common.TopicPartition) ProducerState(org.apache.kafka.clients.admin.ProducerState) PartitionProducerState(org.apache.kafka.clients.admin.DescribeProducersResult.PartitionProducerState) OptionalLong(java.util.OptionalLong) ApiError(org.apache.kafka.common.requests.ApiError)

Example 2 with DescribeProducersResponse

use of org.apache.kafka.common.requests.DescribeProducersResponse in project kafka by apache.

the class KafkaAdminClientTest method buildDescribeProducersResponse.

private DescribeProducersResponse buildDescribeProducersResponse(TopicPartition topicPartition, List<ProducerState> producerStates) {
    DescribeProducersResponseData response = new DescribeProducersResponseData();
    DescribeProducersResponseData.TopicResponse topicResponse = new DescribeProducersResponseData.TopicResponse().setName(topicPartition.topic());
    response.topics().add(topicResponse);
    DescribeProducersResponseData.PartitionResponse partitionResponse = new DescribeProducersResponseData.PartitionResponse().setPartitionIndex(topicPartition.partition()).setErrorCode(Errors.NONE.code());
    topicResponse.partitions().add(partitionResponse);
    partitionResponse.setActiveProducers(producerStates.stream().map(producerState -> new DescribeProducersResponseData.ProducerState().setProducerId(producerState.producerId()).setProducerEpoch(producerState.producerEpoch()).setCoordinatorEpoch(producerState.coordinatorEpoch().orElse(-1)).setLastSequence(producerState.lastSequence()).setLastTimestamp(producerState.lastTimestamp()).setCurrentTxnStartOffset(producerState.currentTransactionStartOffset().orElse(-1L))).collect(Collectors.toList()));
    return new DescribeProducersResponse(response);
}
Also used : ReassignablePartitionResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignablePartitionResponse) DescribeProducersResponseData(org.apache.kafka.common.message.DescribeProducersResponseData) DescribeProducersResponse(org.apache.kafka.common.requests.DescribeProducersResponse)

Example 3 with DescribeProducersResponse

use of org.apache.kafka.common.requests.DescribeProducersResponse in project kafka by apache.

the class DescribeProducersHandlerTest method testCompletedResult.

@Test
public void testCompletedResult() {
    TopicPartition topicPartition = new TopicPartition("foo", 5);
    DescribeProducersOptions options = new DescribeProducersOptions().brokerId(1);
    DescribeProducersHandler handler = newHandler(options);
    PartitionResponse partitionResponse = sampleProducerState(topicPartition);
    DescribeProducersResponse response = describeProducersResponse(singletonMap(topicPartition, partitionResponse));
    Node node = new Node(3, "host", 1);
    ApiResult<TopicPartition, PartitionProducerState> result = handler.handleResponse(node, mkSet(topicPartition), response);
    assertEquals(mkSet(topicPartition), result.completedKeys.keySet());
    assertEquals(emptyMap(), result.failedKeys);
    assertEquals(emptyList(), result.unmappedKeys);
    PartitionProducerState producerState = result.completedKeys.get(topicPartition);
    assertMatchingProducers(partitionResponse, producerState);
}
Also used : PartitionResponse(org.apache.kafka.common.message.DescribeProducersResponseData.PartitionResponse) TopicPartition(org.apache.kafka.common.TopicPartition) Node(org.apache.kafka.common.Node) PartitionProducerState(org.apache.kafka.clients.admin.DescribeProducersResult.PartitionProducerState) DescribeProducersOptions(org.apache.kafka.clients.admin.DescribeProducersOptions) DescribeProducersResponse(org.apache.kafka.common.requests.DescribeProducersResponse) Test(org.junit.jupiter.api.Test)

Example 4 with DescribeProducersResponse

use of org.apache.kafka.common.requests.DescribeProducersResponse in project kafka by apache.

the class DescribeProducersHandlerTest method describeProducersResponse.

private DescribeProducersResponse describeProducersResponse(Map<TopicPartition, PartitionResponse> partitionResponses) {
    DescribeProducersResponseData response = new DescribeProducersResponseData();
    Map<String, Map<Integer, PartitionResponse>> partitionResponsesByTopic = CollectionUtils.groupPartitionDataByTopic(partitionResponses);
    for (Map.Entry<String, Map<Integer, PartitionResponse>> topicEntry : partitionResponsesByTopic.entrySet()) {
        String topic = topicEntry.getKey();
        Map<Integer, PartitionResponse> topicPartitionResponses = topicEntry.getValue();
        TopicResponse topicResponse = new TopicResponse().setName(topic);
        response.topics().add(topicResponse);
        for (Map.Entry<Integer, PartitionResponse> partitionEntry : topicPartitionResponses.entrySet()) {
            Integer partitionId = partitionEntry.getKey();
            PartitionResponse partitionResponse = partitionEntry.getValue();
            topicResponse.partitions().add(partitionResponse.setPartitionIndex(partitionId));
        }
    }
    return new DescribeProducersResponse(response);
}
Also used : PartitionResponse(org.apache.kafka.common.message.DescribeProducersResponseData.PartitionResponse) TopicResponse(org.apache.kafka.common.message.DescribeProducersResponseData.TopicResponse) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) DescribeProducersResponseData(org.apache.kafka.common.message.DescribeProducersResponseData) DescribeProducersResponse(org.apache.kafka.common.requests.DescribeProducersResponse)

Example 5 with DescribeProducersResponse

use of org.apache.kafka.common.requests.DescribeProducersResponse in project kafka by apache.

the class DescribeProducersHandlerTest method handleResponseWithError.

private ApiResult<TopicPartition, PartitionProducerState> handleResponseWithError(DescribeProducersOptions options, TopicPartition topicPartition, Errors error) {
    DescribeProducersHandler handler = newHandler(options);
    DescribeProducersResponse response = buildResponseWithError(topicPartition, error);
    Node node = new Node(options.brokerId().orElse(3), "host", 1);
    return handler.handleResponse(node, mkSet(topicPartition), response);
}
Also used : Node(org.apache.kafka.common.Node) DescribeProducersResponse(org.apache.kafka.common.requests.DescribeProducersResponse)

Aggregations

DescribeProducersResponse (org.apache.kafka.common.requests.DescribeProducersResponse)7 Node (org.apache.kafka.common.Node)5 TopicPartition (org.apache.kafka.common.TopicPartition)4 DescribeProducersResponseData (org.apache.kafka.common.message.DescribeProducersResponseData)3 DescribeProducersRequest (org.apache.kafka.common.requests.DescribeProducersRequest)3 Test (org.junit.jupiter.api.Test)3 Map (java.util.Map)2 DescribeProducersOptions (org.apache.kafka.clients.admin.DescribeProducersOptions)2 PartitionProducerState (org.apache.kafka.clients.admin.DescribeProducersResult.PartitionProducerState)2 PartitionResponse (org.apache.kafka.common.message.DescribeProducersResponseData.PartitionResponse)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 OptionalInt (java.util.OptionalInt)1