|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.kafka.clients.admin.internals; |
| 18 | + |
| 19 | +import org.apache.kafka.clients.admin.KafkaAdminClient; |
| 20 | +import org.apache.kafka.clients.admin.ListShareGroupOffsetsOptions; |
| 21 | +import org.apache.kafka.clients.admin.ListShareGroupOffsetsSpec; |
| 22 | +import org.apache.kafka.common.Node; |
| 23 | +import org.apache.kafka.common.TopicPartition; |
| 24 | +import org.apache.kafka.common.message.DescribeShareGroupOffsetsRequestData; |
| 25 | +import org.apache.kafka.common.protocol.Errors; |
| 26 | +import org.apache.kafka.common.requests.AbstractResponse; |
| 27 | +import org.apache.kafka.common.requests.DescribeShareGroupOffsetsRequest; |
| 28 | +import org.apache.kafka.common.requests.DescribeShareGroupOffsetsResponse; |
| 29 | +import org.apache.kafka.common.requests.FindCoordinatorRequest; |
| 30 | +import org.apache.kafka.common.requests.FindCoordinatorRequest.CoordinatorType; |
| 31 | +import org.apache.kafka.common.utils.LogContext; |
| 32 | + |
| 33 | +import org.slf4j.Logger; |
| 34 | + |
| 35 | +import java.util.Collection; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.HashMap; |
| 38 | +import java.util.List; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.Set; |
| 41 | +import java.util.stream.Collectors; |
| 42 | + |
| 43 | +/** |
| 44 | + * This class is the handler for {@link KafkaAdminClient#listShareGroupOffsets(Map, ListShareGroupOffsetsOptions)} call |
| 45 | + */ |
| 46 | +public class ListShareGroupOffsetsHandler extends AdminApiHandler.Batched<CoordinatorKey, Map<TopicPartition, Long>> { |
| 47 | + |
| 48 | + private final Map<String, ListShareGroupOffsetsSpec> groupSpecs; |
| 49 | + private final Logger log; |
| 50 | + private final AdminApiLookupStrategy<CoordinatorKey> lookupStrategy; |
| 51 | + |
| 52 | + public ListShareGroupOffsetsHandler( |
| 53 | + Map<String, ListShareGroupOffsetsSpec> groupSpecs, |
| 54 | + LogContext logContext) { |
| 55 | + this.groupSpecs = groupSpecs; |
| 56 | + this.log = logContext.logger(ListShareGroupOffsetsHandler.class); |
| 57 | + this.lookupStrategy = new CoordinatorStrategy(CoordinatorType.GROUP, logContext); |
| 58 | + } |
| 59 | + |
| 60 | + public static AdminApiFuture.SimpleAdminApiFuture<CoordinatorKey, Map<TopicPartition, Long>> newFuture(Collection<String> groupIds) { |
| 61 | + return AdminApiFuture.forKeys(coordinatorKeys(groupIds)); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public String apiName() { |
| 66 | + return "describeShareGroupOffsets"; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public AdminApiLookupStrategy<CoordinatorKey> lookupStrategy() { |
| 71 | + return lookupStrategy; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public DescribeShareGroupOffsetsRequest.Builder buildBatchedRequest(int coordinatorId, Set<CoordinatorKey> keys) { |
| 76 | + List<String> groupIds = keys.stream().map(key -> { |
| 77 | + if (key.type != FindCoordinatorRequest.CoordinatorType.GROUP) { |
| 78 | + throw new IllegalArgumentException("Invalid group coordinator key " + key + |
| 79 | + " when building `DescribeShareGroupOffsets` request"); |
| 80 | + } |
| 81 | + return key.idValue; |
| 82 | + }).collect(Collectors.toList()); |
| 83 | + // The DescribeShareGroupOffsetsRequest only includes a single group ID at this point, which is likely a mistake to be fixing a follow-on PR. |
| 84 | + String groupId = groupIds.isEmpty() ? null : groupIds.get(0); |
| 85 | + if (groupId == null) { |
| 86 | + throw new IllegalArgumentException("Missing group id in request"); |
| 87 | + } |
| 88 | + ListShareGroupOffsetsSpec spec = groupSpecs.get(groupId); |
| 89 | + List<DescribeShareGroupOffsetsRequestData.DescribeShareGroupOffsetsRequestTopic> topics = |
| 90 | + spec.topicPartitions().stream().map( |
| 91 | + topicPartition -> new DescribeShareGroupOffsetsRequestData.DescribeShareGroupOffsetsRequestTopic() |
| 92 | + .setTopicName(topicPartition.topic()) |
| 93 | + .setPartitions(List.of(topicPartition.partition())) |
| 94 | + ).collect(Collectors.toList()); |
| 95 | + DescribeShareGroupOffsetsRequestData data = new DescribeShareGroupOffsetsRequestData() |
| 96 | + .setGroupId(groupId) |
| 97 | + .setTopics(topics); |
| 98 | + return new DescribeShareGroupOffsetsRequest.Builder(data, true); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public ApiResult<CoordinatorKey, Map<TopicPartition, Long>> handleResponse(Node coordinator, |
| 103 | + Set<CoordinatorKey> groupIds, |
| 104 | + AbstractResponse abstractResponse) { |
| 105 | + final DescribeShareGroupOffsetsResponse response = (DescribeShareGroupOffsetsResponse) abstractResponse; |
| 106 | + final Map<CoordinatorKey, Map<TopicPartition, Long>> completed = new HashMap<>(); |
| 107 | + final Map<CoordinatorKey, Throwable> failed = new HashMap<>(); |
| 108 | + |
| 109 | + for (CoordinatorKey groupId : groupIds) { |
| 110 | + Map<TopicPartition, Long> data = new HashMap<>(); |
| 111 | + response.data().responses().stream().map( |
| 112 | + describedTopic -> |
| 113 | + describedTopic.partitions().stream().map( |
| 114 | + partition -> { |
| 115 | + if (partition.errorCode() == Errors.NONE.code()) |
| 116 | + data.put(new TopicPartition(describedTopic.topicName(), partition.partitionIndex()), partition.startOffset()); |
| 117 | + else |
| 118 | + log.error("Skipping return offset for topic {} partition {} due to error {}.", describedTopic.topicName(), partition.partitionIndex(), Errors.forCode(partition.errorCode())); |
| 119 | + return data; |
| 120 | + } |
| 121 | + ).collect(Collectors.toList()) |
| 122 | + ).collect(Collectors.toList()); |
| 123 | + completed.put(groupId, data); |
| 124 | + } |
| 125 | + return new ApiResult<>(completed, failed, Collections.emptyList()); |
| 126 | + } |
| 127 | + |
| 128 | + private static Set<CoordinatorKey> coordinatorKeys(Collection<String> groupIds) { |
| 129 | + return groupIds.stream() |
| 130 | + .map(CoordinatorKey::byGroupId) |
| 131 | + .collect(Collectors.toSet()); |
| 132 | + } |
| 133 | +} |
0 commit comments