dajac commented on code in PR #12902: URL: https://github.com/apache/kafka/pull/12902#discussion_r1067069590
########## core/src/main/scala/kafka/coordinator/group/GroupCoordinatorAdapter.scala: ########## @@ -0,0 +1,131 @@ +/** + * 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 kafka.coordinator.group + +import kafka.server.RequestLocal +import kafka.utils.Implicits.MapExtensionMethods +import org.apache.kafka.common.TopicPartition +import org.apache.kafka.common.message.{JoinGroupRequestData, JoinGroupResponseData, OffsetDeleteRequestData, OffsetDeleteResponseData} +import org.apache.kafka.common.protocol.Errors +import org.apache.kafka.common.requests.RequestContext +import org.apache.kafka.common.utils.BufferSupplier + +import java.util.concurrent.CompletableFuture +import scala.collection.mutable +import scala.jdk.CollectionConverters._ + +/** + * GroupCoordinatorAdapter is a thin wrapper around kafka.coordinator.group.GroupCoordinator + * that exposes the new org.apache.kafka.coordinator.group.GroupCoordinator interface. + */ +class GroupCoordinatorAdapter( + val coordinator: GroupCoordinator +) extends org.apache.kafka.coordinator.group.GroupCoordinator { + + override def joinGroup( + context: RequestContext, + request: JoinGroupRequestData, + bufferSupplier: BufferSupplier + ): CompletableFuture[JoinGroupResponseData] = { + val future = new CompletableFuture[JoinGroupResponseData]() + + def callback(joinResult: JoinGroupResult): Unit = { + future.complete(new JoinGroupResponseData() + .setErrorCode(joinResult.error.code) + .setGenerationId(joinResult.generationId) + .setProtocolType(joinResult.protocolType.orNull) + .setProtocolName(joinResult.protocolName.orNull) + .setLeader(joinResult.leaderId) + .setSkipAssignment(joinResult.skipAssignment) + .setMemberId(joinResult.memberId) + .setMembers(joinResult.members.asJava) + ) + } + + val groupInstanceId = Option(request.groupInstanceId) + + // Only return MEMBER_ID_REQUIRED error if joinGroupRequest version is >= 4 + // and groupInstanceId is configured to unknown. + val requireKnownMemberId = context.apiVersion >= 4 && groupInstanceId.isEmpty + + val protocols = request.protocols.valuesList.asScala.map { protocol => + (protocol.name, protocol.metadata) + }.toList + + val supportSkippingAssignment = context.apiVersion >= 9 + + coordinator.handleJoinGroup( + request.groupId, + request.memberId, + groupInstanceId, + requireKnownMemberId, + supportSkippingAssignment, + context.clientId, + context.clientAddress.toString, + request.rebalanceTimeoutMs, + request.sessionTimeoutMs, + request.protocolType, + protocols, + callback, + Option(request.reason), + RequestLocal(bufferSupplier) + ) + + future + } + + override def deleteOffsets( + context: RequestContext, + request: OffsetDeleteRequestData, + bufferSupplier: BufferSupplier + ): CompletableFuture[OffsetDeleteResponseData] = { + val future = new CompletableFuture[OffsetDeleteResponseData]() + + val partitions = mutable.ArrayBuffer[TopicPartition]() + request.topics.forEach { topic => + topic.partitions.forEach { partition => + partitions += new TopicPartition(topic.name, partition.partitionIndex) + } + } + + val (groupError, topicPartitionResults) = coordinator.handleDeleteOffsets( + request.groupId, + partitions, + RequestLocal(bufferSupplier) + ) + + if (groupError != Errors.NONE) { + future.completeExceptionally(groupError.exception) + } else { + val response = new OffsetDeleteResponseData() + topicPartitionResults.forKeyValue { (topicPartition, error) => + var topic = response.topics.find(topicPartition.topic) + if (topic == null) { + topic = new OffsetDeleteResponseData.OffsetDeleteResponseTopic().setName(topicPartition.topic) + response.topics.add(topic) + } + topic.partitions.add(new OffsetDeleteResponseData.OffsetDeleteResponsePartition() + .setPartitionIndex(topicPartition.partition) + .setErrorCode(error.code)) + } + + future.complete(response) + } + + future Review Comment: The concept is not new in Kafka. This is how we implemented the quorum controller. I follow the same patterns that we have introduced there. I agree that future are not strictly necessary. We could keep using callbacks as today. I find Future nicer to work with, personally. I don't have a detailed design doc for this part as we usually treat this as implementation details. However, I will have a first implementation of the core building block of the new group coordinator soon. This will help to understand the new architecture. Does it work for you? -- 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: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org