chia7712 commented on a change in pull request #10275: URL: https://github.com/apache/kafka/pull/10275#discussion_r591036839
########## File path: clients/src/main/java/org/apache/kafka/clients/admin/internals/DescribeProducersHandler.java ########## @@ -0,0 +1,213 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.clients.admin.internals; + +import org.apache.kafka.clients.admin.DescribeProducersOptions; +import org.apache.kafka.clients.admin.DescribeProducersResult.PartitionProducerState; +import org.apache.kafka.clients.admin.ProducerState; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.errors.InvalidTopicException; +import org.apache.kafka.common.errors.TopicAuthorizationException; +import org.apache.kafka.common.message.DescribeProducersRequestData; +import org.apache.kafka.common.message.DescribeProducersResponseData; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.ApiError; +import org.apache.kafka.common.requests.DescribeProducersRequest; +import org.apache.kafka.common.requests.DescribeProducersResponse; +import org.apache.kafka.common.utils.CollectionUtils; +import org.apache.kafka.common.utils.LogContext; +import org.apache.kafka.common.utils.Utils; +import org.slf4j.Logger; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.OptionalInt; +import java.util.OptionalLong; +import java.util.Set; +import java.util.stream.Collectors; + +public class DescribeProducersHandler implements AdminApiHandler<TopicPartition, PartitionProducerState> { + private final LogContext logContext; + private final Logger log; + private final DescribeProducersOptions options; + private final Set<TopicPartition> topicPartitions; + + public DescribeProducersHandler( + Set<TopicPartition> topicPartitions, + DescribeProducersOptions options, + LogContext logContext + ) { + this.topicPartitions = Collections.unmodifiableSet(topicPartitions); + this.options = options; + this.log = logContext.logger(DescribeProducersHandler.class); + this.logContext = logContext; + } + + @Override + public String apiName() { + return "describeProducers"; + } + + @Override + public KeyMappings<TopicPartition> initializeKeys() { + if (options.brokerId().isPresent()) { + // If the options indicate a specific broker, then we can skip the lookup step + int destinationBrokerId = options.brokerId().getAsInt(); + Map<TopicPartition, Integer> staticMappedPartitions = + Utils.initializeMap(topicPartitions, () -> destinationBrokerId); + return new KeyMappings<>( + Optional.of(new StaticKeyMapping<>(staticMappedPartitions)), + Optional.empty() + ); + } else { + PartitionLeaderStrategy lookupStrategy = + new PartitionLeaderStrategy(topicPartitions, logContext); + return new KeyMappings<>( + Optional.empty(), + Optional.of(new DynamicKeyMapping<>(topicPartitions, lookupStrategy)) + ); + } + } + + @Override + public DescribeProducersRequest.Builder buildRequest( + Integer brokerId, + Set<TopicPartition> topicPartitions + ) { + DescribeProducersRequestData request = new DescribeProducersRequestData(); + DescribeProducersRequest.Builder builder = new DescribeProducersRequest.Builder(request); + + CollectionUtils.groupPartitionsByTopic( + topicPartitions, + builder::addTopic, + (topicRequest, partitionId) -> topicRequest.partitionIndexes().add(partitionId) + ); + + return builder; + } + + private void handlePartitionError( + TopicPartition topicPartition, + ApiError apiError, + Map<TopicPartition, Throwable> failed, + List<TopicPartition> unmapped + ) { + switch (apiError.error()) { + case NOT_LEADER_OR_FOLLOWER: + if (options.brokerId().isPresent()) { + // Typically these errors are retriable, but if the user specified the brokerId + // explicitly, then they are fatal. + int brokerId = options.brokerId().getAsInt(); + log.error("Not leader error in `DescribeProducers` response for partition {} " + + "for brokerId {} set in options", topicPartition, brokerId, apiError.exception()); Review comment: Good to know that. thanks! ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org