JimmyWang6 commented on code in PR #18819:
URL: https://github.com/apache/kafka/pull/18819#discussion_r1954288480


##########
clients/src/main/java/org/apache/kafka/clients/admin/internals/AlterShareGroupOffsetsHandler.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.AlterShareGroupOffsetsOptions;
+import org.apache.kafka.clients.admin.KafkaAdminClient;
+import org.apache.kafka.common.Node;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.message.AlterShareGroupOffsetsRequestData;
+import org.apache.kafka.common.message.AlterShareGroupOffsetsResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.AbstractResponse;
+import org.apache.kafka.common.requests.AlterShareGroupOffsetsRequest;
+import org.apache.kafka.common.requests.AlterShareGroupOffsetsResponse;
+import org.apache.kafka.common.requests.FindCoordinatorRequest;
+import org.apache.kafka.common.utils.LogContext;
+
+import org.slf4j.Logger;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * This class is the handler for {@link 
KafkaAdminClient#alterShareGroupOffsets(String, Map, 
AlterShareGroupOffsetsOptions)} call
+ */
+public class AlterShareGroupOffsetsHandler extends 
AdminApiHandler.Batched<CoordinatorKey, Map<TopicPartition, Errors>> {
+
+    private final CoordinatorKey groupId;
+
+    private final Logger log;
+
+    private final Map<TopicPartition, Long> offsets;
+
+    private final AdminApiLookupStrategy<CoordinatorKey> lookupStrategy;
+
+
+    public AlterShareGroupOffsetsHandler(String groupId, Map<TopicPartition, 
Long> offsets, LogContext logContext) {
+        this.groupId = CoordinatorKey.byGroupId(groupId);
+        this.offsets = offsets;
+        this.log = logContext.logger(AlterShareGroupOffsetsHandler.class);
+        this.lookupStrategy = new 
CoordinatorStrategy(FindCoordinatorRequest.CoordinatorType.GROUP, logContext);
+    }
+
+    @Override
+    AlterShareGroupOffsetsRequest.Builder buildBatchedRequest(int brokerId, 
Set<CoordinatorKey> groupIds) {
+        Map<String, Map<TopicPartition, Long>> offsetsByTopic = new 
HashMap<>();
+        offsets.forEach((topicPartition, offset) -> {
+            Map<TopicPartition, Long> offsetsByPartition = 
offsetsByTopic.computeIfAbsent(topicPartition.topic(), v -> new HashMap<>());
+            offsetsByPartition.put(topicPartition, offset);
+        });
+
+        
AlterShareGroupOffsetsRequestData.AlterShareGroupOffsetsRequestTopicCollection 
requestTopics = new 
AlterShareGroupOffsetsRequestData.AlterShareGroupOffsetsRequestTopicCollection();
+        for (Map.Entry<String, Map<TopicPartition, Long>> topicEntry : 
offsetsByTopic.entrySet()) {
+            String topic = topicEntry.getKey();
+            Map<TopicPartition, Long> offsetsByPartition = 
topicEntry.getValue();
+
+            
List<AlterShareGroupOffsetsRequestData.AlterShareGroupOffsetsRequestPartition> 
partitionList = new ArrayList<>();
+            for (Map.Entry<TopicPartition, Long> partitionEntry : 
offsetsByPartition.entrySet()) {
+                TopicPartition tp = partitionEntry.getKey();
+                Long startOffset = partitionEntry.getValue();
+
+                partitionList.add(new 
AlterShareGroupOffsetsRequestData.AlterShareGroupOffsetsRequestPartition()
+                        .setPartitionIndex(tp.partition())
+                        .setStartOffset(startOffset)
+                );
+            }
+            requestTopics.add(new 
AlterShareGroupOffsetsRequestData.AlterShareGroupOffsetsRequestTopic()
+                    .setTopicName(topic)
+                    .setPartitions(partitionList));
+        }
+
+        AlterShareGroupOffsetsRequestData data = new 
AlterShareGroupOffsetsRequestData()
+                .setGroupId(groupId.idValue)
+                .setTopics(requestTopics);
+
+        return new AlterShareGroupOffsetsRequest.Builder(data);
+    }
+
+    public static AdminApiFuture.SimpleAdminApiFuture<CoordinatorKey, 
Map<TopicPartition, Errors>> newFuture(String groupId) {

Review Comment:
   Done. 



-- 
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]

Reply via email to