apoorvmittal10 commented on code in PR #17775: URL: https://github.com/apache/kafka/pull/17775#discussion_r1904176664
########## clients/src/main/java/org/apache/kafka/clients/admin/ListShareGroupOffsetsSpec.java: ########## @@ -0,0 +1,80 @@ +/* + * 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; + +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.annotation.InterfaceStability; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Map; +import java.util.Objects; + +/** + * Specification of share group offsets to list using {@link Admin#listShareGroupOffsets(Map, ListShareGroupOffsetsOptions)}. + * <p> + * The API of this class is evolving, see {@link Admin} for details. + */ [email protected] +public class ListShareGroupOffsetsSpec { + + private Collection<TopicPartition> topicPartitions; + + public ListShareGroupOffsetsSpec() { + topicPartitions = new ArrayList<>(); + } + + /** + * Set the topic partitions whose offsets are to be listed for a share group. + */ + ListShareGroupOffsetsSpec topicPartitions(Collection<TopicPartition> topicPartitions) { + this.topicPartitions = topicPartitions; + return this; + } + + /** + * Returns the topic partitions whose offsets are to be listed for a share group. + */ + public Collection<TopicPartition> topicPartitions() { + return topicPartitions; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof ListShareGroupOffsetsSpec)) { + return false; + } + ListShareGroupOffsetsSpec that = (ListShareGroupOffsetsSpec) o; + return Objects.equals(topicPartitions, that.topicPartitions); + } + + @Override + public int hashCode() { + return Objects.hash(topicPartitions); + } + + @Override + public String toString() { + return "ListShareGroupOffsetsSpec(" + + "topicPartitions=" + topicPartitions + Review Comment: Will a null `topicPartitions` generate an error? I can see an override where `topicPartitions` can be set to null overriding the constructor initialization. ``` topicPartitions(Collection<TopicPartition> topicPartitions) { ########## clients/src/main/java/org/apache/kafka/clients/admin/KafkaAdminClient.java: ########## @@ -3796,6 +3797,17 @@ public DescribeShareGroupsResult describeShareGroups(final Collection<String> gr .collect(Collectors.toMap(entry -> entry.getKey().idValue, Map.Entry::getValue))); } + @Override + public ListShareGroupOffsetsResult listShareGroupOffsets(Map<String, ListShareGroupOffsetsSpec> groupSpecs, + ListShareGroupOffsetsOptions options) { + SimpleAdminApiFuture<CoordinatorKey, Map<TopicPartition, Long>> future = + ListShareGroupOffsetsHandler.newFuture(groupSpecs.keySet()); + ListShareGroupOffsetsHandler handler = + new ListShareGroupOffsetsHandler(groupSpecs, logContext); Review Comment: nit: merge lines and maybe the above ones as well. ########## clients/src/main/java/org/apache/kafka/clients/admin/internals/ListShareGroupOffsetsHandler.java: ########## @@ -0,0 +1,187 @@ +/* + * 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.ListShareGroupOffsetsSpec; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.FindCoordinatorRequest; +import org.apache.kafka.common.requests.OffsetFetchRequest; +import org.apache.kafka.common.requests.OffsetFetchResponse; +import org.apache.kafka.common.utils.LogContext; + +import org.slf4j.Logger; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +public class ListShareGroupOffsetsHandler implements AdminApiHandler<CoordinatorKey, Map<TopicPartition, Long>> { + + private final Map<String, ListShareGroupOffsetsSpec> groupSpecs; + private final Logger log; + private final CoordinatorStrategy lookupStrategy; + + public ListShareGroupOffsetsHandler( + Map<String, ListShareGroupOffsetsSpec> groupSpecs, + LogContext logContext + ) { + this.log = logContext.logger(ListShareGroupOffsetsHandler.class); + this.lookupStrategy = new CoordinatorStrategy(FindCoordinatorRequest.CoordinatorType.GROUP, logContext); + this.groupSpecs = groupSpecs; + } + + public static AdminApiFuture.SimpleAdminApiFuture<CoordinatorKey, Map<TopicPartition, Long>> newFuture(Collection<String> groupIds) { + return AdminApiFuture.forKeys(coordinatorKeys(groupIds)); + } + + @Override + public String apiName() { + return "offsetFetch"; Review Comment: Isn't `listShareGroupOffsets` be a better name? ########## clients/src/main/java/org/apache/kafka/clients/admin/internals/ListShareGroupOffsetsHandler.java: ########## @@ -0,0 +1,187 @@ +/* + * 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.ListShareGroupOffsetsSpec; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.FindCoordinatorRequest; +import org.apache.kafka.common.requests.OffsetFetchRequest; +import org.apache.kafka.common.requests.OffsetFetchResponse; +import org.apache.kafka.common.utils.LogContext; + +import org.slf4j.Logger; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +public class ListShareGroupOffsetsHandler implements AdminApiHandler<CoordinatorKey, Map<TopicPartition, Long>> { Review Comment: For my undersatnding: ListOffsetsHandled extends Batched which implements AdminApiHandler, hence is ListShareGroupOffsetsHandler differ in functionality with list offsets? ########## clients/src/main/java/org/apache/kafka/clients/admin/internals/ListShareGroupOffsetsHandler.java: ########## @@ -0,0 +1,187 @@ +/* + * 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.ListShareGroupOffsetsSpec; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.FindCoordinatorRequest; +import org.apache.kafka.common.requests.OffsetFetchRequest; +import org.apache.kafka.common.requests.OffsetFetchResponse; +import org.apache.kafka.common.utils.LogContext; + +import org.slf4j.Logger; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +public class ListShareGroupOffsetsHandler implements AdminApiHandler<CoordinatorKey, Map<TopicPartition, Long>> { + + private final Map<String, ListShareGroupOffsetsSpec> groupSpecs; + private final Logger log; + private final CoordinatorStrategy lookupStrategy; + + public ListShareGroupOffsetsHandler( + Map<String, ListShareGroupOffsetsSpec> groupSpecs, + LogContext logContext + ) { + this.log = logContext.logger(ListShareGroupOffsetsHandler.class); + this.lookupStrategy = new CoordinatorStrategy(FindCoordinatorRequest.CoordinatorType.GROUP, logContext); + this.groupSpecs = groupSpecs; + } + + public static AdminApiFuture.SimpleAdminApiFuture<CoordinatorKey, Map<TopicPartition, Long>> newFuture(Collection<String> groupIds) { + return AdminApiFuture.forKeys(coordinatorKeys(groupIds)); + } + + @Override + public String apiName() { + return "offsetFetch"; + } + + @Override + public AdminApiLookupStrategy<CoordinatorKey> lookupStrategy() { + return lookupStrategy; + } + + private void validateKeys(Set<CoordinatorKey> groupIds) { + Set<CoordinatorKey> keys = coordinatorKeys(groupSpecs.keySet()); + if (!keys.containsAll(groupIds)) { + throw new IllegalArgumentException("Received unexpected group ids " + groupIds + + " (expected one of " + keys + ")"); + } + } + + private static Set<CoordinatorKey> coordinatorKeys(Collection<String> groupIds) { + return groupIds.stream() + .map(CoordinatorKey::byGroupId) + .collect(Collectors.toSet()); + } + + public OffsetFetchRequest.Builder buildBatchedRequest(Set<CoordinatorKey> groupIds) { Review Comment: nit: Can Public methods be prior to protected and private? i.e. write public > protected > private, it's just easier that way to look at code files. ########## clients/src/main/java/org/apache/kafka/clients/admin/internals/ListShareGroupOffsetsHandler.java: ########## @@ -0,0 +1,187 @@ +/* + * 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.ListShareGroupOffsetsSpec; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.FindCoordinatorRequest; +import org.apache.kafka.common.requests.OffsetFetchRequest; +import org.apache.kafka.common.requests.OffsetFetchResponse; +import org.apache.kafka.common.utils.LogContext; + +import org.slf4j.Logger; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +public class ListShareGroupOffsetsHandler implements AdminApiHandler<CoordinatorKey, Map<TopicPartition, Long>> { + + private final Map<String, ListShareGroupOffsetsSpec> groupSpecs; + private final Logger log; + private final CoordinatorStrategy lookupStrategy; + + public ListShareGroupOffsetsHandler( + Map<String, ListShareGroupOffsetsSpec> groupSpecs, + LogContext logContext + ) { + this.log = logContext.logger(ListShareGroupOffsetsHandler.class); + this.lookupStrategy = new CoordinatorStrategy(FindCoordinatorRequest.CoordinatorType.GROUP, logContext); + this.groupSpecs = groupSpecs; + } + + public static AdminApiFuture.SimpleAdminApiFuture<CoordinatorKey, Map<TopicPartition, Long>> newFuture(Collection<String> groupIds) { + return AdminApiFuture.forKeys(coordinatorKeys(groupIds)); + } + + @Override + public String apiName() { + return "offsetFetch"; + } + + @Override + public AdminApiLookupStrategy<CoordinatorKey> lookupStrategy() { + return lookupStrategy; + } + + private void validateKeys(Set<CoordinatorKey> groupIds) { + Set<CoordinatorKey> keys = coordinatorKeys(groupSpecs.keySet()); + if (!keys.containsAll(groupIds)) { + throw new IllegalArgumentException("Received unexpected group ids " + groupIds + + " (expected one of " + keys + ")"); Review Comment: `(expected one of ` => `(expected among` as it's a Set not just one? ########## clients/src/main/java/org/apache/kafka/clients/admin/internals/ListShareGroupOffsetsHandler.java: ########## @@ -0,0 +1,187 @@ +/* + * 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.ListShareGroupOffsetsSpec; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.FindCoordinatorRequest; +import org.apache.kafka.common.requests.OffsetFetchRequest; +import org.apache.kafka.common.requests.OffsetFetchResponse; +import org.apache.kafka.common.utils.LogContext; + +import org.slf4j.Logger; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +public class ListShareGroupOffsetsHandler implements AdminApiHandler<CoordinatorKey, Map<TopicPartition, Long>> { + + private final Map<String, ListShareGroupOffsetsSpec> groupSpecs; + private final Logger log; + private final CoordinatorStrategy lookupStrategy; + + public ListShareGroupOffsetsHandler( + Map<String, ListShareGroupOffsetsSpec> groupSpecs, + LogContext logContext + ) { + this.log = logContext.logger(ListShareGroupOffsetsHandler.class); + this.lookupStrategy = new CoordinatorStrategy(FindCoordinatorRequest.CoordinatorType.GROUP, logContext); + this.groupSpecs = groupSpecs; + } + + public static AdminApiFuture.SimpleAdminApiFuture<CoordinatorKey, Map<TopicPartition, Long>> newFuture(Collection<String> groupIds) { + return AdminApiFuture.forKeys(coordinatorKeys(groupIds)); + } + + @Override + public String apiName() { + return "offsetFetch"; + } + + @Override + public AdminApiLookupStrategy<CoordinatorKey> lookupStrategy() { + return lookupStrategy; + } + + private void validateKeys(Set<CoordinatorKey> groupIds) { + Set<CoordinatorKey> keys = coordinatorKeys(groupSpecs.keySet()); + if (!keys.containsAll(groupIds)) { + throw new IllegalArgumentException("Received unexpected group ids " + groupIds + + " (expected one of " + keys + ")"); + } + } + + private static Set<CoordinatorKey> coordinatorKeys(Collection<String> groupIds) { + return groupIds.stream() + .map(CoordinatorKey::byGroupId) + .collect(Collectors.toSet()); + } + + public OffsetFetchRequest.Builder buildBatchedRequest(Set<CoordinatorKey> groupIds) { + // Create a map that only contains the consumer groups owned by the coordinator. + Map<String, List<TopicPartition>> coordinatorGroupIdToTopicPartitions = new HashMap<>(groupIds.size()); + groupIds.forEach(g -> { + ListShareGroupOffsetsSpec spec = groupSpecs.get(g.idValue); + List<TopicPartition> partitions = spec.topicPartitions() != null ? new ArrayList<>(spec.topicPartitions()) : null; + coordinatorGroupIdToTopicPartitions.put(g.idValue, partitions); + }); + + return new OffsetFetchRequest.Builder(coordinatorGroupIdToTopicPartitions, false, false); + } + + @Override + public Collection<RequestAndKeys<CoordinatorKey>> buildRequest(int brokerId, Set<CoordinatorKey> groupIds) { Review Comment: Set<CoordinatorKey> groupIds => Set<CoordinatorKey> coordinatorKeys? Elsewhere as well, it's just confusing to see groupIds. ########## clients/src/main/java/org/apache/kafka/clients/admin/internals/ListShareGroupOffsetsHandler.java: ########## @@ -0,0 +1,187 @@ +/* + * 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.ListShareGroupOffsetsSpec; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.FindCoordinatorRequest; +import org.apache.kafka.common.requests.OffsetFetchRequest; +import org.apache.kafka.common.requests.OffsetFetchResponse; +import org.apache.kafka.common.utils.LogContext; + +import org.slf4j.Logger; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +public class ListShareGroupOffsetsHandler implements AdminApiHandler<CoordinatorKey, Map<TopicPartition, Long>> { + + private final Map<String, ListShareGroupOffsetsSpec> groupSpecs; + private final Logger log; + private final CoordinatorStrategy lookupStrategy; + + public ListShareGroupOffsetsHandler( + Map<String, ListShareGroupOffsetsSpec> groupSpecs, + LogContext logContext + ) { + this.log = logContext.logger(ListShareGroupOffsetsHandler.class); + this.lookupStrategy = new CoordinatorStrategy(FindCoordinatorRequest.CoordinatorType.GROUP, logContext); + this.groupSpecs = groupSpecs; + } + + public static AdminApiFuture.SimpleAdminApiFuture<CoordinatorKey, Map<TopicPartition, Long>> newFuture(Collection<String> groupIds) { + return AdminApiFuture.forKeys(coordinatorKeys(groupIds)); + } + + @Override + public String apiName() { + return "offsetFetch"; + } + + @Override + public AdminApiLookupStrategy<CoordinatorKey> lookupStrategy() { + return lookupStrategy; + } + + private void validateKeys(Set<CoordinatorKey> groupIds) { + Set<CoordinatorKey> keys = coordinatorKeys(groupSpecs.keySet()); + if (!keys.containsAll(groupIds)) { + throw new IllegalArgumentException("Received unexpected group ids " + groupIds + + " (expected one of " + keys + ")"); + } + } + + private static Set<CoordinatorKey> coordinatorKeys(Collection<String> groupIds) { + return groupIds.stream() + .map(CoordinatorKey::byGroupId) + .collect(Collectors.toSet()); + } + + public OffsetFetchRequest.Builder buildBatchedRequest(Set<CoordinatorKey> groupIds) { Review Comment: `Set<CoordinatorKey> groupIds` => `Set<CoordinatorKey> coordinatorKeys`? ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java: ########## @@ -175,6 +189,8 @@ public GroupCoordinatorService build() { throw new IllegalArgumentException("GroupCoordinatorMetrics must be set."); if (groupConfigManager == null) throw new IllegalArgumentException("GroupConfigManager must be set."); + if (persister == null) + throw new IllegalArgumentException("Persister must be set."); Review Comment: Does that mean a Persister is always needed regardless share group is enabled on the broker? ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinator.java: ########## @@ -210,6 +212,18 @@ CompletableFuture<List<ShareGroupDescribeResponseData.DescribedGroup>> shareGrou List<String> groupIds ); + /** + * List share group Offsets. + * + * @param context The coordinator request context. + * @param request The ReadShareGroupStateSummaryRequest data. + * + * @return A future yielding the results or an exception. + */ + CompletableFuture<List<ReadShareGroupStateSummaryResponseData.ReadStateSummaryResult>> listShareGroupOffsets( Review Comment: Shouldn't the method align more with `ReadShareGroupStateSummaryRequest` operation name rather with list group offsets i.e. `readShareGroupStateSummary`? ########## clients/src/main/java/org/apache/kafka/clients/admin/internals/ListShareGroupOffsetsHandler.java: ########## @@ -0,0 +1,187 @@ +/* + * 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.ListShareGroupOffsetsSpec; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.FindCoordinatorRequest; +import org.apache.kafka.common.requests.OffsetFetchRequest; +import org.apache.kafka.common.requests.OffsetFetchResponse; +import org.apache.kafka.common.utils.LogContext; + +import org.slf4j.Logger; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +public class ListShareGroupOffsetsHandler implements AdminApiHandler<CoordinatorKey, Map<TopicPartition, Long>> { + + private final Map<String, ListShareGroupOffsetsSpec> groupSpecs; + private final Logger log; + private final CoordinatorStrategy lookupStrategy; + + public ListShareGroupOffsetsHandler( + Map<String, ListShareGroupOffsetsSpec> groupSpecs, + LogContext logContext + ) { + this.log = logContext.logger(ListShareGroupOffsetsHandler.class); + this.lookupStrategy = new CoordinatorStrategy(FindCoordinatorRequest.CoordinatorType.GROUP, logContext); + this.groupSpecs = groupSpecs; + } + + public static AdminApiFuture.SimpleAdminApiFuture<CoordinatorKey, Map<TopicPartition, Long>> newFuture(Collection<String> groupIds) { + return AdminApiFuture.forKeys(coordinatorKeys(groupIds)); + } + + @Override + public String apiName() { + return "offsetFetch"; + } + + @Override + public AdminApiLookupStrategy<CoordinatorKey> lookupStrategy() { + return lookupStrategy; + } + + private void validateKeys(Set<CoordinatorKey> groupIds) { + Set<CoordinatorKey> keys = coordinatorKeys(groupSpecs.keySet()); + if (!keys.containsAll(groupIds)) { + throw new IllegalArgumentException("Received unexpected group ids " + groupIds + + " (expected one of " + keys + ")"); + } + } + + private static Set<CoordinatorKey> coordinatorKeys(Collection<String> groupIds) { + return groupIds.stream() + .map(CoordinatorKey::byGroupId) + .collect(Collectors.toSet()); + } + + public OffsetFetchRequest.Builder buildBatchedRequest(Set<CoordinatorKey> groupIds) { + // Create a map that only contains the consumer groups owned by the coordinator. + Map<String, List<TopicPartition>> coordinatorGroupIdToTopicPartitions = new HashMap<>(groupIds.size()); + groupIds.forEach(g -> { + ListShareGroupOffsetsSpec spec = groupSpecs.get(g.idValue); + List<TopicPartition> partitions = spec.topicPartitions() != null ? new ArrayList<>(spec.topicPartitions()) : null; Review Comment: Should the list be `mutable`? If not then shall we use `List.copyOf(spec.topicPartitions())`? -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
