You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "dajac (via GitHub)" <gi...@apache.org> on 2023/06/05 14:27:39 UTC

[GitHub] [kafka] dajac opened a new pull request, #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

dajac opened a new pull request, #13812:
URL: https://github.com/apache/kafka/pull/13812

   This patch includes https://github.com/apache/kafka/pull/13795 for now.
   
   This patch introduces the GroupCoordinatorService. This is the new implementation of the group coordinator based on the coordinator runtime introduced in https://github.com/apache/kafka/pull/13795.
   
   ### Committer Checklist (excluded from commit message)
   - [ ] Verify design and implementation 
   - [ ] Verify test coverage and CI build status
   - [ ] Verify documentation (including upgrade notes)
   


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1235639793


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;
+
+    /**
+     *
+     * @param logContext
+     * @param config
+     * @param runtime
+     */
+    GroupCoordinatorService(
+        LogContext logContext,
+        GroupCoordinatorConfig config,
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime
+    ) {
+        this.log = logContext.logger(CoordinatorLoader.class);
+        this.config = config;
+        this.runtime = runtime;
+    }
+
+    /**
+     * Throws CoordinatorNotAvailableException if the not active.
+     */
+    private void throwIfNotActive() {
+        if (!isActive.get()) {
+            throw Errors.COORDINATOR_NOT_AVAILABLE.exception();
+        }
+    }
+
+    /**
+     * @return TODO
+     */
+    private TopicPartition topicPartitionFor(
+        String groupId
+    ) {
+        return new TopicPartition(Topic.GROUP_METADATA_TOPIC_NAME, partitionFor(groupId));
+    }
+
+    /**
+     * See {@link GroupCoordinator#partitionFor(String)}
+     */
+    @Override
+    public int partitionFor(
+        String groupId
+    ) {
+        throwIfNotActive();
+        return Utils.abs(groupId.hashCode()) % numPartitions;
+    }
+
+    /**
+     * See {@link GroupCoordinator#consumerGroupHeartbeat(RequestContext, ConsumerGroupHeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ConsumerGroupHeartbeatResponseData> consumerGroupHeartbeat(
+        RequestContext context,
+        ConsumerGroupHeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return runtime.scheduleWriteOperation(
+            "consumer-group-heartbeat",
+            topicPartitionFor(request.groupId()),
+            coordinator -> coordinator.consumerGroupHeartbeat(context, request)
+        ).exceptionally(exception -> {
+            if (exception instanceof UnknownTopicOrPartitionException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.COORDINATOR_NOT_AVAILABLE.code());
+            }
+
+            if (exception instanceof NotLeaderOrFollowerException ||
+                exception instanceof NotEnoughReplicasException ||
+                exception instanceof KafkaStorageException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.NOT_COORDINATOR.code());
+            }
+
+            if (exception instanceof RecordTooLargeException ||
+                exception instanceof RecordBatchTooLargeException ||
+                exception instanceof InvalidFetchSizeException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.UNKNOWN_SERVER_ERROR.code());
+            }
+
+            return new ConsumerGroupHeartbeatResponseData()
+                .setErrorCode(Errors.forException(exception).code())
+                .setErrorMessage(exception.getMessage());
+        });
+    }
+
+    /**
+     * See {@link GroupCoordinator#joinGroup(RequestContext, JoinGroupRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<JoinGroupResponseData> joinGroup(
+        RequestContext context,
+        JoinGroupRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#syncGroup(RequestContext, SyncGroupRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<SyncGroupResponseData> syncGroup(
+        RequestContext context,
+        SyncGroupRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#heartbeat(RequestContext, HeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<HeartbeatResponseData> heartbeat(
+        RequestContext context,
+        HeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#leaveGroup(RequestContext, LeaveGroupRequestData)}.
+     */
+    @Override
+    public CompletableFuture<LeaveGroupResponseData> leaveGroup(
+        RequestContext context,
+        LeaveGroupRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#listGroups(RequestContext, ListGroupsRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ListGroupsResponseData> listGroups(
+        RequestContext context,
+        ListGroupsRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#describeGroups(RequestContext, List)}.
+     */
+    @Override
+    public CompletableFuture<List<DescribeGroupsResponseData.DescribedGroup>> describeGroups(
+        RequestContext context,
+        List<String> groupIds
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#deleteGroups(RequestContext, List, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<DeleteGroupsResponseData.DeletableGroupResultCollection> deleteGroups(
+        RequestContext context,
+        List<String> groupIds,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#fetchOffsets(RequestContext, String, List, boolean)}.
+     */
+    @Override
+    public CompletableFuture<List<OffsetFetchResponseData.OffsetFetchResponseTopics>> fetchOffsets(
+        RequestContext context,
+        String groupId,
+        List<OffsetFetchRequestData.OffsetFetchRequestTopics> topics,
+        boolean requireStable
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#fetchAllOffsets(RequestContext, String, boolean)}.
+     */
+    @Override
+    public CompletableFuture<List<OffsetFetchResponseData.OffsetFetchResponseTopics>> fetchAllOffsets(
+        RequestContext context,
+        String groupId,
+        boolean requireStable
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#commitOffsets(RequestContext, OffsetCommitRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<OffsetCommitResponseData> commitOffsets(
+        RequestContext context,
+        OffsetCommitRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#commitTransactionalOffsets(RequestContext, TxnOffsetCommitRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<TxnOffsetCommitResponseData> commitTransactionalOffsets(
+        RequestContext context,
+        TxnOffsetCommitRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#deleteOffsets(RequestContext, OffsetDeleteRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<OffsetDeleteResponseData> deleteOffsets(
+        RequestContext context,
+        OffsetDeleteRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#onTransactionCompleted(long, Iterable, TransactionResult)}.
+     */
+    @Override
+    public void onTransactionCompleted(
+        long producerId,
+        Iterable<TopicPartition> partitions,
+        TransactionResult transactionResult
+    ) {
+        throwIfNotActive();

Review Comment:
   More to the methods



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac merged pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac merged PR #13812:
URL: https://github.com/apache/kafka/pull/13812


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1229221951


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,575 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The name of the consumer metadata topic.
+     */
+    private static final String CONSUMER_METADATA_TOPIC_NAME = "__consumer_offsets";

Review Comment:
   Good point but I think that it should be the other way around. `Topic.GROUP_METADATA_TOPIC_NAME` should be the source of truth.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1235773175


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;

Review Comment:
   Sorry, I mean the topic should only be manually created via tests. Usually it would be done automatically when we use consumers.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1229600983


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorConfig.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.coordinator.group.assignor.PartitionAssignor;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * The group coordinator configurations.
+ */
+public class GroupCoordinatorConfig {
+    public static class Builder {
+        private int numThreads = 1;

Review Comment:
   I have played a bit around with this idea. It create a pretty big diff so I have decided to tackle this separately from this one. I filed https://issues.apache.org/jira/browse/KAFKA-15089 for this purpose.
   
   For this patch, I have reduced `GroupCoordinatorConfig` to a simple POJO for now.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1235696555


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;
+
+    /**
+     *
+     * @param logContext
+     * @param config
+     * @param runtime
+     */
+    GroupCoordinatorService(
+        LogContext logContext,
+        GroupCoordinatorConfig config,
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime
+    ) {
+        this.log = logContext.logger(CoordinatorLoader.class);
+        this.config = config;
+        this.runtime = runtime;
+    }
+
+    /**
+     * Throws CoordinatorNotAvailableException if the not active.
+     */
+    private void throwIfNotActive() {
+        if (!isActive.get()) {
+            throw Errors.COORDINATOR_NOT_AVAILABLE.exception();
+        }
+    }
+
+    /**
+     * @return TODO
+     */
+    private TopicPartition topicPartitionFor(
+        String groupId
+    ) {
+        return new TopicPartition(Topic.GROUP_METADATA_TOPIC_NAME, partitionFor(groupId));
+    }
+
+    /**
+     * See {@link GroupCoordinator#partitionFor(String)}
+     */
+    @Override
+    public int partitionFor(
+        String groupId
+    ) {
+        throwIfNotActive();
+        return Utils.abs(groupId.hashCode()) % numPartitions;
+    }
+
+    /**
+     * See {@link GroupCoordinator#consumerGroupHeartbeat(RequestContext, ConsumerGroupHeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ConsumerGroupHeartbeatResponseData> consumerGroupHeartbeat(
+        RequestContext context,
+        ConsumerGroupHeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return runtime.scheduleWriteOperation(
+            "consumer-group-heartbeat",
+            topicPartitionFor(request.groupId()),
+            coordinator -> coordinator.consumerGroupHeartbeat(context, request)
+        ).exceptionally(exception -> {
+            if (exception instanceof UnknownTopicOrPartitionException) {

Review Comment:
   That seems to be a mistake. I will check this tomorrow.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1230942477


##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/ReplicatedGroupCoordinatorTest.java:
##########
@@ -0,0 +1,308 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupCurrentMemberAssignmentKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupCurrentMemberAssignmentValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMemberMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMemberMetadataValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMetadataValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupPartitionMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupPartitionMetadataValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMemberKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMemberValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMetadataValue;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorResult;
+import org.apache.kafka.server.common.ApiMessageAndVersion;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.apache.kafka.coordinator.group.TestUtil.requestContext;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class ReplicatedGroupCoordinatorTest {
+
+    @Test
+    public void testConsumerGroupHeartbeat() {
+        GroupMetadataManager groupMetadataManager = mock(GroupMetadataManager.class);
+        ReplicatedGroupCoordinator coordinator = new ReplicatedGroupCoordinator(
+            groupMetadataManager
+        );
+
+        RequestContext context = requestContext(ApiKeys.CONSUMER_GROUP_HEARTBEAT);
+        ConsumerGroupHeartbeatRequestData request = new ConsumerGroupHeartbeatRequestData();
+        CoordinatorResult<ConsumerGroupHeartbeatResponseData, Record> result = new CoordinatorResult<>(
+            Collections.emptyList(),
+            new ConsumerGroupHeartbeatResponseData()
+        );
+
+        when(coordinator.consumerGroupHeartbeat(
+            context,
+            request
+        )).thenReturn(result);
+
+        assertEquals(result, coordinator.consumerGroupHeartbeat(context, request));

Review Comment:
   Yeah, it just validates that a successful response is just returned to the caller.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1229222782


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/ReplicatedGroupCoordinator.java:
##########
@@ -0,0 +1,202 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.protocol.ApiMessage;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupCurrentMemberAssignmentKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupCurrentMemberAssignmentValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMemberMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMemberMetadataValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMetadataValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupPartitionMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupPartitionMetadataValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMemberKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMemberValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMetadataValue;
+import org.apache.kafka.coordinator.group.runtime.Coordinator;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilder;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorResult;
+import org.apache.kafka.server.common.ApiMessageAndVersion;
+import org.apache.kafka.timeline.SnapshotRegistry;
+
+/**
+ * The group coordinator replicated state machine that manages the metadata of all generic and
+ * consumer groups. It holds the hard and the soft state of the groups. This class has two kinds
+ * of methods:
+ * 1) The request handlers which handle the requests and generate a response and records to
+ *    mutate the hard state. Those records will be written by the runtime and applied to the
+ *    hard state via the replay methods.
+ * 2) The replay methods which apply records to the hard state. Those are used in the request
+ *    handling as well as during the initial loading of the records from the partitions.
+ */
+public class ReplicatedGroupCoordinator implements Coordinator<Record> {

Review Comment:
   Right. This is where the state is stored. I don't really like the name but I could not come up with a better one. I am opened to suggestions here.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1230193218


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/ReplicatedGroupCoordinator.java:
##########
@@ -0,0 +1,202 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.protocol.ApiMessage;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupCurrentMemberAssignmentKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupCurrentMemberAssignmentValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMemberMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMemberMetadataValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMetadataValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupPartitionMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupPartitionMetadataValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMemberKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMemberValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMetadataValue;
+import org.apache.kafka.coordinator.group.runtime.Coordinator;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilder;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorResult;
+import org.apache.kafka.server.common.ApiMessageAndVersion;
+import org.apache.kafka.timeline.SnapshotRegistry;
+
+/**
+ * The group coordinator replicated state machine that manages the metadata of all generic and
+ * consumer groups. It holds the hard and the soft state of the groups. This class has two kinds
+ * of methods:
+ * 1) The request handlers which handle the requests and generate a response and records to
+ *    mutate the hard state. Those records will be written by the runtime and applied to the
+ *    hard state via the replay methods.
+ * 2) The replay methods which apply records to the hard state. Those are used in the request
+ *    handling as well as during the initial loading of the records from the partitions.
+ */
+public class ReplicatedGroupCoordinator implements Coordinator<Record> {

Review Comment:
   I guess it depends on what the other implementations of coordinator will be 😅 



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac commented on pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on PR #13812:
URL: https://github.com/apache/kafka/pull/13812#issuecomment-1578025976

   @jolshan The new files are:
   - GroupCoordinatorService.java + tests
   - GroupCoordinatorConfig.java + tests
   - ReplicatedGroupCoordinator.java + tests


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on PR #13812:
URL: https://github.com/apache/kafka/pull/13812#issuecomment-1577522950

   @dajac Can you share the names of the new files while this contains all the other changes? Is it just GroupCoordinatorService.java (+ tests)


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] divijvaidya commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "divijvaidya (via GitHub)" <gi...@apache.org>.
divijvaidya commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1223233148


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,575 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The name of the consumer metadata topic.
+     */
+    private static final String CONSUMER_METADATA_TOPIC_NAME = "__consumer_offsets";

Review Comment:
   nit
   
   I assume that this is going to be the source of truth for the topic name? In which case, we should probably use this constant to update `Topic.GROUP_METADATA_TOPIC_NAME`.



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorConfig.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.coordinator.group.assignor.PartitionAssignor;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * The group coordinator configurations.
+ */
+public class GroupCoordinatorConfig {
+    public static class Builder {
+        private int numThreads = 1;

Review Comment:
   Can we please move these to DEFAULT constants? and use `ConfigDef`? 
   
   Please see RemoteLogManagerConfig for inspiration. 



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,575 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The name of the consumer metadata topic.
+     */
+    private static final String CONSUMER_METADATA_TOPIC_NAME = "__consumer_offsets";
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;
+
+    /**
+     *
+     * @param logContext
+     * @param config
+     * @param runtime
+     */
+    GroupCoordinatorService(
+        LogContext logContext,
+        GroupCoordinatorConfig config,
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime
+    ) {
+        this.log = logContext.logger(CoordinatorLoader.class);
+        this.config = config;
+        this.runtime = runtime;
+    }
+
+    /**
+     * Throws CoordinatorNotAvailableException if the not active.
+     */
+    private void throwIfNotActive() {
+        if (!isActive.get()) {
+            throw Errors.COORDINATOR_NOT_AVAILABLE.exception();
+        }
+    }
+
+    /**
+     * @return TODO
+     */
+    private TopicPartition topicPartitionFor(
+        String groupId
+    ) {
+        return new TopicPartition(CONSUMER_METADATA_TOPIC_NAME, partitionFor(groupId));
+    }
+
+    /**
+     * See {@link GroupCoordinator#partitionFor(String)}
+     */
+    @Override
+    public int partitionFor(
+        String groupId
+    ) {
+        throwIfNotActive();
+        return Utils.abs(groupId.hashCode()) % numPartitions;
+    }
+
+    /**
+     * See {@link GroupCoordinator#consumerGroupHeartbeat(RequestContext, ConsumerGroupHeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ConsumerGroupHeartbeatResponseData> consumerGroupHeartbeat(
+        RequestContext context,
+        ConsumerGroupHeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return runtime.scheduleWriteOperation(
+            "consumer-group-heartbeat",
+            topicPartitionFor(request.groupId()),
+            coordinator -> coordinator.consumerGroupHeartbeat(context, request)
+        ).exceptionally(exception -> {
+            if (exception instanceof UnknownTopicOrPartitionException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.COORDINATOR_NOT_AVAILABLE.code());
+            }
+
+            if (exception instanceof NotLeaderOrFollowerException ||
+                exception instanceof NotEnoughReplicasException ||
+                exception instanceof KafkaStorageException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.NOT_COORDINATOR.code());
+            }
+
+            if (exception instanceof RecordTooLargeException ||
+                exception instanceof RecordBatchTooLargeException ||
+                exception instanceof InvalidFetchSizeException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.UNKNOWN_SERVER_ERROR.code());
+            }
+
+            return new ConsumerGroupHeartbeatResponseData()
+                .setErrorCode(Errors.forException(exception).code())
+                .setErrorMessage(exception.getMessage());
+        });
+    }
+
+    /**
+     * See {@link GroupCoordinator#joinGroup(RequestContext, JoinGroupRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<JoinGroupResponseData> joinGroup(
+        RequestContext context,
+        JoinGroupRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#syncGroup(RequestContext, SyncGroupRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<SyncGroupResponseData> syncGroup(
+        RequestContext context,
+        SyncGroupRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#heartbeat(RequestContext, HeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<HeartbeatResponseData> heartbeat(
+        RequestContext context,
+        HeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#leaveGroup(RequestContext, LeaveGroupRequestData)}.
+     */
+    @Override
+    public CompletableFuture<LeaveGroupResponseData> leaveGroup(
+        RequestContext context,
+        LeaveGroupRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#listGroups(RequestContext, ListGroupsRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ListGroupsResponseData> listGroups(
+        RequestContext context,
+        ListGroupsRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#describeGroups(RequestContext, List)}.
+     */
+    @Override
+    public CompletableFuture<List<DescribeGroupsResponseData.DescribedGroup>> describeGroups(
+        RequestContext context,
+        List<String> groupIds
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#deleteGroups(RequestContext, List, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<DeleteGroupsResponseData.DeletableGroupResultCollection> deleteGroups(
+        RequestContext context,
+        List<String> groupIds,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#fetchOffsets(RequestContext, String, List, boolean)}.
+     */
+    @Override
+    public CompletableFuture<List<OffsetFetchResponseData.OffsetFetchResponseTopics>> fetchOffsets(
+        RequestContext context,
+        String groupId,
+        List<OffsetFetchRequestData.OffsetFetchRequestTopics> topics,
+        boolean requireStable
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#fetchAllOffsets(RequestContext, String, boolean)}.
+     */
+    @Override
+    public CompletableFuture<List<OffsetFetchResponseData.OffsetFetchResponseTopics>> fetchAllOffsets(
+        RequestContext context,
+        String groupId,
+        boolean requireStable
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#commitOffsets(RequestContext, OffsetCommitRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<OffsetCommitResponseData> commitOffsets(
+        RequestContext context,
+        OffsetCommitRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#commitTransactionalOffsets(RequestContext, TxnOffsetCommitRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<TxnOffsetCommitResponseData> commitTransactionalOffsets(
+        RequestContext context,
+        TxnOffsetCommitRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#deleteOffsets(RequestContext, OffsetDeleteRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<OffsetDeleteResponseData> deleteOffsets(
+        RequestContext context,
+        OffsetDeleteRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#onTransactionCompleted(long, Iterable, TransactionResult)}.
+     */
+    @Override
+    public void onTransactionCompleted(
+        long producerId,
+        Iterable<TopicPartition> partitions,
+        TransactionResult transactionResult
+    ) {
+        throwIfNotActive();
+    }
+
+    /**
+     * See {@link GroupCoordinator#onPartitionsDeleted(List, BufferSupplier)}.
+     */
+    @Override
+    public void onPartitionsDeleted(
+        List<TopicPartition> topicPartitions,
+        BufferSupplier bufferSupplier
+    ) {
+        throwIfNotActive();
+    }
+
+    /**
+     * See {@link GroupCoordinator#onElection(int, int)}.
+     */
+    @Override
+    public void onElection(
+        int groupMetadataPartitionIndex,
+        int groupMetadataPartitionLeaderEpoch
+    ) {
+        throwIfNotActive();
+        runtime.scheduleLoadOperation(
+            new TopicPartition(CONSUMER_METADATA_TOPIC_NAME, groupMetadataPartitionIndex),
+            groupMetadataPartitionLeaderEpoch
+        );
+    }
+
+    /**
+     * See {@link GroupCoordinator#onResignation(int, OptionalInt)}.
+     */
+    @Override
+    public void onResignation(
+        int groupMetadataPartitionIndex,
+        OptionalInt groupMetadataPartitionLeaderEpoch
+    ) {
+        throwIfNotActive();
+        if (!groupMetadataPartitionLeaderEpoch.isPresent()) {
+            throw new IllegalArgumentException("The leader epoch should always be provided in KRaft.");
+        }
+        runtime.scheduleUnloadOperation(
+            new TopicPartition(CONSUMER_METADATA_TOPIC_NAME, groupMetadataPartitionIndex),
+            groupMetadataPartitionLeaderEpoch.getAsInt()
+        );
+    }
+
+    /**
+     * See {@link GroupCoordinator#onNewMetadataImage(MetadataImage, MetadataDelta)}.
+     */
+    @Override
+    public void onNewMetadataImage(
+        MetadataImage newImage,
+        MetadataDelta delta
+    ) {
+        throwIfNotActive();
+    }
+
+    /**
+     * See {@link GroupCoordinator#groupMetadataTopicConfigs()}.
+     */
+    @Override
+    public Properties groupMetadataTopicConfigs() {
+        Properties properties = new Properties();
+        properties.put(TopicConfig.CLEANUP_POLICY_CONFIG, TopicConfig.CLEANUP_POLICY_COMPACT);
+        properties.put(TopicConfig.COMPRESSION_TYPE_CONFIG, BrokerCompressionType.PRODUCER.name);
+        properties.put(TopicConfig.SEGMENT_BYTES_CONFIG, String.valueOf(config.offsetsTopicSegmentBytes));
+        return properties;
+    }
+
+    /**
+     * See {@link GroupCoordinator#startup(IntSupplier)}.
+     */
+    @Override
+    public void startup(
+        IntSupplier groupMetadataTopicPartitionCount
+    ) {
+        log.info("Starting up.");
+        numPartitions = groupMetadataTopicPartitionCount.getAsInt();
+        isActive.set(true);
+        log.info("Startup complete.");
+    }
+
+    /**
+     * See {@link GroupCoordinator#shutdown()}.
+     */
+    @Override
+    public void shutdown() {

Review Comment:
   please make this idempotent. If notActive, then no op.



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,575 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The name of the consumer metadata topic.
+     */
+    private static final String CONSUMER_METADATA_TOPIC_NAME = "__consumer_offsets";
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;
+
+    /**
+     *
+     * @param logContext
+     * @param config
+     * @param runtime
+     */
+    GroupCoordinatorService(
+        LogContext logContext,
+        GroupCoordinatorConfig config,
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime
+    ) {
+        this.log = logContext.logger(CoordinatorLoader.class);
+        this.config = config;
+        this.runtime = runtime;
+    }
+
+    /**
+     * Throws CoordinatorNotAvailableException if the not active.
+     */
+    private void throwIfNotActive() {
+        if (!isActive.get()) {
+            throw Errors.COORDINATOR_NOT_AVAILABLE.exception();
+        }
+    }
+
+    /**
+     * @return TODO
+     */
+    private TopicPartition topicPartitionFor(
+        String groupId
+    ) {
+        return new TopicPartition(CONSUMER_METADATA_TOPIC_NAME, partitionFor(groupId));
+    }
+
+    /**
+     * See {@link GroupCoordinator#partitionFor(String)}
+     */
+    @Override
+    public int partitionFor(
+        String groupId
+    ) {
+        throwIfNotActive();
+        return Utils.abs(groupId.hashCode()) % numPartitions;
+    }
+
+    /**
+     * See {@link GroupCoordinator#consumerGroupHeartbeat(RequestContext, ConsumerGroupHeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ConsumerGroupHeartbeatResponseData> consumerGroupHeartbeat(
+        RequestContext context,
+        ConsumerGroupHeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return runtime.scheduleWriteOperation(
+            "consumer-group-heartbeat",
+            topicPartitionFor(request.groupId()),
+            coordinator -> coordinator.consumerGroupHeartbeat(context, request)
+        ).exceptionally(exception -> {
+            if (exception instanceof UnknownTopicOrPartitionException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.COORDINATOR_NOT_AVAILABLE.code());
+            }
+
+            if (exception instanceof NotLeaderOrFollowerException ||
+                exception instanceof NotEnoughReplicasException ||
+                exception instanceof KafkaStorageException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.NOT_COORDINATOR.code());
+            }
+
+            if (exception instanceof RecordTooLargeException ||
+                exception instanceof RecordBatchTooLargeException ||
+                exception instanceof InvalidFetchSizeException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.UNKNOWN_SERVER_ERROR.code());
+            }
+
+            return new ConsumerGroupHeartbeatResponseData()
+                .setErrorCode(Errors.forException(exception).code())
+                .setErrorMessage(exception.getMessage());
+        });
+    }
+
+    /**
+     * See {@link GroupCoordinator#joinGroup(RequestContext, JoinGroupRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<JoinGroupResponseData> joinGroup(
+        RequestContext context,
+        JoinGroupRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#syncGroup(RequestContext, SyncGroupRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<SyncGroupResponseData> syncGroup(
+        RequestContext context,
+        SyncGroupRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#heartbeat(RequestContext, HeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<HeartbeatResponseData> heartbeat(
+        RequestContext context,
+        HeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#leaveGroup(RequestContext, LeaveGroupRequestData)}.
+     */
+    @Override
+    public CompletableFuture<LeaveGroupResponseData> leaveGroup(
+        RequestContext context,
+        LeaveGroupRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#listGroups(RequestContext, ListGroupsRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ListGroupsResponseData> listGroups(
+        RequestContext context,
+        ListGroupsRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#describeGroups(RequestContext, List)}.
+     */
+    @Override
+    public CompletableFuture<List<DescribeGroupsResponseData.DescribedGroup>> describeGroups(
+        RequestContext context,
+        List<String> groupIds
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#deleteGroups(RequestContext, List, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<DeleteGroupsResponseData.DeletableGroupResultCollection> deleteGroups(
+        RequestContext context,
+        List<String> groupIds,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#fetchOffsets(RequestContext, String, List, boolean)}.
+     */
+    @Override
+    public CompletableFuture<List<OffsetFetchResponseData.OffsetFetchResponseTopics>> fetchOffsets(
+        RequestContext context,
+        String groupId,
+        List<OffsetFetchRequestData.OffsetFetchRequestTopics> topics,
+        boolean requireStable
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#fetchAllOffsets(RequestContext, String, boolean)}.
+     */
+    @Override
+    public CompletableFuture<List<OffsetFetchResponseData.OffsetFetchResponseTopics>> fetchAllOffsets(
+        RequestContext context,
+        String groupId,
+        boolean requireStable
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#commitOffsets(RequestContext, OffsetCommitRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<OffsetCommitResponseData> commitOffsets(
+        RequestContext context,
+        OffsetCommitRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#commitTransactionalOffsets(RequestContext, TxnOffsetCommitRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<TxnOffsetCommitResponseData> commitTransactionalOffsets(
+        RequestContext context,
+        TxnOffsetCommitRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#deleteOffsets(RequestContext, OffsetDeleteRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<OffsetDeleteResponseData> deleteOffsets(
+        RequestContext context,
+        OffsetDeleteRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#onTransactionCompleted(long, Iterable, TransactionResult)}.
+     */
+    @Override
+    public void onTransactionCompleted(
+        long producerId,
+        Iterable<TopicPartition> partitions,
+        TransactionResult transactionResult
+    ) {
+        throwIfNotActive();
+    }
+
+    /**
+     * See {@link GroupCoordinator#onPartitionsDeleted(List, BufferSupplier)}.
+     */
+    @Override
+    public void onPartitionsDeleted(
+        List<TopicPartition> topicPartitions,
+        BufferSupplier bufferSupplier
+    ) {
+        throwIfNotActive();
+    }
+
+    /**
+     * See {@link GroupCoordinator#onElection(int, int)}.
+     */
+    @Override
+    public void onElection(
+        int groupMetadataPartitionIndex,
+        int groupMetadataPartitionLeaderEpoch
+    ) {
+        throwIfNotActive();
+        runtime.scheduleLoadOperation(
+            new TopicPartition(CONSUMER_METADATA_TOPIC_NAME, groupMetadataPartitionIndex),
+            groupMetadataPartitionLeaderEpoch
+        );
+    }
+
+    /**
+     * See {@link GroupCoordinator#onResignation(int, OptionalInt)}.
+     */
+    @Override
+    public void onResignation(
+        int groupMetadataPartitionIndex,
+        OptionalInt groupMetadataPartitionLeaderEpoch
+    ) {
+        throwIfNotActive();
+        if (!groupMetadataPartitionLeaderEpoch.isPresent()) {
+            throw new IllegalArgumentException("The leader epoch should always be provided in KRaft.");
+        }
+        runtime.scheduleUnloadOperation(
+            new TopicPartition(CONSUMER_METADATA_TOPIC_NAME, groupMetadataPartitionIndex),
+            groupMetadataPartitionLeaderEpoch.getAsInt()
+        );
+    }
+
+    /**
+     * See {@link GroupCoordinator#onNewMetadataImage(MetadataImage, MetadataDelta)}.
+     */
+    @Override
+    public void onNewMetadataImage(
+        MetadataImage newImage,
+        MetadataDelta delta
+    ) {
+        throwIfNotActive();
+    }
+
+    /**
+     * See {@link GroupCoordinator#groupMetadataTopicConfigs()}.
+     */
+    @Override
+    public Properties groupMetadataTopicConfigs() {
+        Properties properties = new Properties();
+        properties.put(TopicConfig.CLEANUP_POLICY_CONFIG, TopicConfig.CLEANUP_POLICY_COMPACT);
+        properties.put(TopicConfig.COMPRESSION_TYPE_CONFIG, BrokerCompressionType.PRODUCER.name);
+        properties.put(TopicConfig.SEGMENT_BYTES_CONFIG, String.valueOf(config.offsetsTopicSegmentBytes));
+        return properties;
+    }
+
+    /**
+     * See {@link GroupCoordinator#startup(IntSupplier)}.
+     */
+    @Override
+    public void startup(

Review Comment:
   please make this idempotent. If already active them no op.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1230295895


##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/GroupCoordinatorServiceTest.java:
##########
@@ -0,0 +1,269 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.CoordinatorNotAvailableException;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.InvalidRequestException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.coordinator.group.assignor.RangeAssignor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.ArgumentMatchers;
+
+import java.util.Collections;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.stream.Stream;
+
+import static org.apache.kafka.coordinator.group.TestUtil.requestContext;
+import static org.apache.kafka.test.TestUtils.assertFutureThrows;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class GroupCoordinatorServiceTest {
+
+    @SuppressWarnings("unchecked")
+    private CoordinatorRuntime<ReplicatedGroupCoordinator, Record> mockRuntime() {
+        return (CoordinatorRuntime<ReplicatedGroupCoordinator, Record>) mock(CoordinatorRuntime.class);
+    }
+
+    private GroupCoordinatorConfig createConfig() {
+        return new GroupCoordinatorConfig(
+            1,
+            45,
+            5,
+            Integer.MAX_VALUE,
+            Collections.singletonList(new RangeAssignor()),
+            1000
+        );
+    }
+
+    @Test
+    public void testStartupShutdown() throws Exception {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        service.startup(() -> 1);
+        service.shutdown();
+
+        verify(runtime, times(1)).close();
+    }
+
+    @Test
+    public void testConsumerGroupHeartbeatWhenNotStarted() {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        ConsumerGroupHeartbeatRequestData request = new ConsumerGroupHeartbeatRequestData()
+            .setGroupId("foo");
+
+        assertFutureThrows(
+            service.consumerGroupHeartbeat(
+                requestContext(ApiKeys.CONSUMER_GROUP_HEARTBEAT),
+                request
+            ),
+            CoordinatorNotAvailableException.class
+        );
+    }
+
+    @Test
+    public void testConsumerGroupHeartbeat() throws ExecutionException, InterruptedException, TimeoutException {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        ConsumerGroupHeartbeatRequestData request = new ConsumerGroupHeartbeatRequestData()
+            .setGroupId("foo");
+
+        service.startup(() -> 1);
+
+        when(runtime.scheduleWriteOperation(
+            ArgumentMatchers.eq("consumer-group-heartbeat"),
+            ArgumentMatchers.eq(new TopicPartition("__consumer_offsets", 0)),
+            ArgumentMatchers.any()
+        )).thenReturn(CompletableFuture.completedFuture(
+            new ConsumerGroupHeartbeatResponseData()
+        ));
+
+        CompletableFuture<ConsumerGroupHeartbeatResponseData> future = service.consumerGroupHeartbeat(
+            requestContext(ApiKeys.CONSUMER_GROUP_HEARTBEAT),
+            request
+        );
+
+        assertEquals(new ConsumerGroupHeartbeatResponseData(), future.get(5, TimeUnit.SECONDS));
+    }
+
+    private static Stream<Arguments> testConsumerGroupHeartbeatWithExceptionSource() {
+        return Stream.of(
+            Arguments.arguments(new UnknownTopicOrPartitionException(), Errors.COORDINATOR_NOT_AVAILABLE.code(), null),
+            Arguments.arguments(new NotLeaderOrFollowerException(), Errors.NOT_COORDINATOR.code(), null),
+            Arguments.arguments(new NotEnoughReplicasException(), Errors.NOT_COORDINATOR.code(), null),
+            Arguments.arguments(new KafkaStorageException(), Errors.NOT_COORDINATOR.code(), null),
+            Arguments.arguments(new RecordTooLargeException(), Errors.UNKNOWN_SERVER_ERROR.code(), null),
+            Arguments.arguments(new RecordBatchTooLargeException(), Errors.UNKNOWN_SERVER_ERROR.code(), null),
+            Arguments.arguments(new InvalidFetchSizeException(""), Errors.UNKNOWN_SERVER_ERROR.code(), null),
+            Arguments.arguments(new InvalidRequestException("Invalid"), Errors.INVALID_REQUEST.code(), "Invalid")
+        );
+    }
+
+    @ParameterizedTest
+    @MethodSource("testConsumerGroupHeartbeatWithExceptionSource")
+    public void testConsumerGroupHeartbeatWithException(
+        Throwable exception,
+        short expectedErrorCode,
+        String expectedErrorMessage
+    ) throws ExecutionException, InterruptedException, TimeoutException {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        ConsumerGroupHeartbeatRequestData request = new ConsumerGroupHeartbeatRequestData()
+            .setGroupId("foo");
+
+        service.startup(() -> 1);
+
+        when(runtime.scheduleWriteOperation(
+            ArgumentMatchers.eq("consumer-group-heartbeat"),
+            ArgumentMatchers.eq(new TopicPartition("__consumer_offsets", 0)),
+            ArgumentMatchers.any()
+        )).thenReturn(FutureUtils.failedFuture(exception));
+
+        CompletableFuture<ConsumerGroupHeartbeatResponseData> future = service.consumerGroupHeartbeat(
+            requestContext(ApiKeys.CONSUMER_GROUP_HEARTBEAT),
+            request
+        );
+
+        assertEquals(
+            new ConsumerGroupHeartbeatResponseData()
+                .setErrorCode(expectedErrorCode)
+                .setErrorMessage(expectedErrorMessage),
+            future.get(5, TimeUnit.SECONDS)
+        );
+    }
+
+    @Test
+    public void testPartitionFor() {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        assertThrows(CoordinatorNotAvailableException.class,
+            () -> service.partitionFor("foo"));
+
+        service.startup(() -> 10);
+
+        assertTrue(service.partitionFor("foo") >= 0);
+        assertTrue(service.partitionFor("foo") < 10);
+    }
+
+    @Test
+    public void testGroupMetadataTopicConfigs() {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        Properties expectedProperties = new Properties();
+        expectedProperties.put(TopicConfig.CLEANUP_POLICY_CONFIG, TopicConfig.CLEANUP_POLICY_COMPACT);
+        expectedProperties.put(TopicConfig.COMPRESSION_TYPE_CONFIG, BrokerCompressionType.PRODUCER.name);
+        expectedProperties.put(TopicConfig.SEGMENT_BYTES_CONFIG, "1000");
+
+        assertEquals(expectedProperties, service.groupMetadataTopicConfigs());
+    }
+
+    @Test
+    public void testOnElection() {

Review Comment:
   did we want to include a test that confirms we throw errors when not active?



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1230243216


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;
+
+    /**
+     *
+     * @param logContext
+     * @param config
+     * @param runtime
+     */
+    GroupCoordinatorService(
+        LogContext logContext,
+        GroupCoordinatorConfig config,
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime
+    ) {
+        this.log = logContext.logger(CoordinatorLoader.class);
+        this.config = config;
+        this.runtime = runtime;
+    }
+
+    /**
+     * Throws CoordinatorNotAvailableException if the not active.
+     */
+    private void throwIfNotActive() {
+        if (!isActive.get()) {
+            throw Errors.COORDINATOR_NOT_AVAILABLE.exception();
+        }
+    }
+
+    /**
+     * @return TODO
+     */
+    private TopicPartition topicPartitionFor(
+        String groupId
+    ) {
+        return new TopicPartition(Topic.GROUP_METADATA_TOPIC_NAME, partitionFor(groupId));
+    }
+
+    /**
+     * See {@link GroupCoordinator#partitionFor(String)}
+     */
+    @Override
+    public int partitionFor(
+        String groupId
+    ) {
+        throwIfNotActive();
+        return Utils.abs(groupId.hashCode()) % numPartitions;
+    }
+
+    /**
+     * See {@link GroupCoordinator#consumerGroupHeartbeat(RequestContext, ConsumerGroupHeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ConsumerGroupHeartbeatResponseData> consumerGroupHeartbeat(
+        RequestContext context,
+        ConsumerGroupHeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return runtime.scheduleWriteOperation(
+            "consumer-group-heartbeat",
+            topicPartitionFor(request.groupId()),
+            coordinator -> coordinator.consumerGroupHeartbeat(context, request)
+        ).exceptionally(exception -> {
+            if (exception instanceof UnknownTopicOrPartitionException) {

Review Comment:
   have we always converted these errors as such? I see in GroupMetadataManager things are slightly different. 



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1230294539


##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/ReplicatedGroupCoordinatorTest.java:
##########
@@ -0,0 +1,308 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupCurrentMemberAssignmentKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupCurrentMemberAssignmentValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMemberMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMemberMetadataValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMetadataValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupPartitionMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupPartitionMetadataValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMemberKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMemberValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMetadataValue;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorResult;
+import org.apache.kafka.server.common.ApiMessageAndVersion;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.apache.kafka.coordinator.group.TestUtil.requestContext;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class ReplicatedGroupCoordinatorTest {
+
+    @Test
+    public void testConsumerGroupHeartbeat() {
+        GroupMetadataManager groupMetadataManager = mock(GroupMetadataManager.class);
+        ReplicatedGroupCoordinator coordinator = new ReplicatedGroupCoordinator(
+            groupMetadataManager
+        );
+
+        RequestContext context = requestContext(ApiKeys.CONSUMER_GROUP_HEARTBEAT);
+        ConsumerGroupHeartbeatRequestData request = new ConsumerGroupHeartbeatRequestData();
+        CoordinatorResult<ConsumerGroupHeartbeatResponseData, Record> result = new CoordinatorResult<>(
+            Collections.emptyList(),
+            new ConsumerGroupHeartbeatResponseData()
+        );
+
+        when(coordinator.consumerGroupHeartbeat(
+            context,
+            request
+        )).thenReturn(result);
+
+        assertEquals(result, coordinator.consumerGroupHeartbeat(context, request));

Review Comment:
   Is this just a test that we don't throw errors?



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1230940881


##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/GroupCoordinatorServiceTest.java:
##########
@@ -0,0 +1,269 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.CoordinatorNotAvailableException;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.InvalidRequestException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.coordinator.group.assignor.RangeAssignor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.ArgumentMatchers;
+
+import java.util.Collections;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.stream.Stream;
+
+import static org.apache.kafka.coordinator.group.TestUtil.requestContext;
+import static org.apache.kafka.test.TestUtils.assertFutureThrows;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class GroupCoordinatorServiceTest {
+
+    @SuppressWarnings("unchecked")
+    private CoordinatorRuntime<ReplicatedGroupCoordinator, Record> mockRuntime() {
+        return (CoordinatorRuntime<ReplicatedGroupCoordinator, Record>) mock(CoordinatorRuntime.class);
+    }
+
+    private GroupCoordinatorConfig createConfig() {
+        return new GroupCoordinatorConfig(
+            1,
+            45,
+            5,
+            Integer.MAX_VALUE,
+            Collections.singletonList(new RangeAssignor()),
+            1000
+        );
+    }
+
+    @Test
+    public void testStartupShutdown() throws Exception {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        service.startup(() -> 1);
+        service.shutdown();
+
+        verify(runtime, times(1)).close();
+    }
+
+    @Test
+    public void testConsumerGroupHeartbeatWhenNotStarted() {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        ConsumerGroupHeartbeatRequestData request = new ConsumerGroupHeartbeatRequestData()
+            .setGroupId("foo");
+
+        assertFutureThrows(
+            service.consumerGroupHeartbeat(
+                requestContext(ApiKeys.CONSUMER_GROUP_HEARTBEAT),
+                request
+            ),
+            CoordinatorNotAvailableException.class
+        );
+    }
+
+    @Test
+    public void testConsumerGroupHeartbeat() throws ExecutionException, InterruptedException, TimeoutException {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        ConsumerGroupHeartbeatRequestData request = new ConsumerGroupHeartbeatRequestData()
+            .setGroupId("foo");
+
+        service.startup(() -> 1);
+
+        when(runtime.scheduleWriteOperation(
+            ArgumentMatchers.eq("consumer-group-heartbeat"),
+            ArgumentMatchers.eq(new TopicPartition("__consumer_offsets", 0)),
+            ArgumentMatchers.any()
+        )).thenReturn(CompletableFuture.completedFuture(
+            new ConsumerGroupHeartbeatResponseData()
+        ));
+
+        CompletableFuture<ConsumerGroupHeartbeatResponseData> future = service.consumerGroupHeartbeat(
+            requestContext(ApiKeys.CONSUMER_GROUP_HEARTBEAT),
+            request
+        );
+
+        assertEquals(new ConsumerGroupHeartbeatResponseData(), future.get(5, TimeUnit.SECONDS));
+    }
+
+    private static Stream<Arguments> testConsumerGroupHeartbeatWithExceptionSource() {
+        return Stream.of(
+            Arguments.arguments(new UnknownTopicOrPartitionException(), Errors.COORDINATOR_NOT_AVAILABLE.code(), null),
+            Arguments.arguments(new NotLeaderOrFollowerException(), Errors.NOT_COORDINATOR.code(), null),
+            Arguments.arguments(new NotEnoughReplicasException(), Errors.NOT_COORDINATOR.code(), null),
+            Arguments.arguments(new KafkaStorageException(), Errors.NOT_COORDINATOR.code(), null),
+            Arguments.arguments(new RecordTooLargeException(), Errors.UNKNOWN_SERVER_ERROR.code(), null),
+            Arguments.arguments(new RecordBatchTooLargeException(), Errors.UNKNOWN_SERVER_ERROR.code(), null),
+            Arguments.arguments(new InvalidFetchSizeException(""), Errors.UNKNOWN_SERVER_ERROR.code(), null),
+            Arguments.arguments(new InvalidRequestException("Invalid"), Errors.INVALID_REQUEST.code(), "Invalid")
+        );
+    }
+
+    @ParameterizedTest
+    @MethodSource("testConsumerGroupHeartbeatWithExceptionSource")
+    public void testConsumerGroupHeartbeatWithException(
+        Throwable exception,
+        short expectedErrorCode,
+        String expectedErrorMessage
+    ) throws ExecutionException, InterruptedException, TimeoutException {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        ConsumerGroupHeartbeatRequestData request = new ConsumerGroupHeartbeatRequestData()
+            .setGroupId("foo");
+
+        service.startup(() -> 1);
+
+        when(runtime.scheduleWriteOperation(
+            ArgumentMatchers.eq("consumer-group-heartbeat"),
+            ArgumentMatchers.eq(new TopicPartition("__consumer_offsets", 0)),
+            ArgumentMatchers.any()
+        )).thenReturn(FutureUtils.failedFuture(exception));
+
+        CompletableFuture<ConsumerGroupHeartbeatResponseData> future = service.consumerGroupHeartbeat(
+            requestContext(ApiKeys.CONSUMER_GROUP_HEARTBEAT),
+            request
+        );
+
+        assertEquals(
+            new ConsumerGroupHeartbeatResponseData()
+                .setErrorCode(expectedErrorCode)
+                .setErrorMessage(expectedErrorMessage),
+            future.get(5, TimeUnit.SECONDS)
+        );
+    }
+
+    @Test
+    public void testPartitionFor() {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        assertThrows(CoordinatorNotAvailableException.class,
+            () -> service.partitionFor("foo"));
+
+        service.startup(() -> 10);
+
+        assertTrue(service.partitionFor("foo") >= 0);

Review Comment:
   That makes sense. Let me change this.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1235772593


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;
+
+    /**
+     *
+     * @param logContext
+     * @param config
+     * @param runtime
+     */
+    GroupCoordinatorService(
+        LogContext logContext,
+        GroupCoordinatorConfig config,
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime
+    ) {
+        this.log = logContext.logger(CoordinatorLoader.class);
+        this.config = config;
+        this.runtime = runtime;
+    }
+
+    /**
+     * Throws CoordinatorNotAvailableException if the not active.
+     */
+    private void throwIfNotActive() {
+        if (!isActive.get()) {
+            throw Errors.COORDINATOR_NOT_AVAILABLE.exception();
+        }
+    }
+
+    /**
+     * @return TODO
+     */
+    private TopicPartition topicPartitionFor(
+        String groupId
+    ) {
+        return new TopicPartition(Topic.GROUP_METADATA_TOPIC_NAME, partitionFor(groupId));
+    }
+
+    /**
+     * See {@link GroupCoordinator#partitionFor(String)}
+     */
+    @Override
+    public int partitionFor(
+        String groupId
+    ) {
+        throwIfNotActive();
+        return Utils.abs(groupId.hashCode()) % numPartitions;
+    }
+
+    /**
+     * See {@link GroupCoordinator#consumerGroupHeartbeat(RequestContext, ConsumerGroupHeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ConsumerGroupHeartbeatResponseData> consumerGroupHeartbeat(
+        RequestContext context,
+        ConsumerGroupHeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return runtime.scheduleWriteOperation(
+            "consumer-group-heartbeat",
+            topicPartitionFor(request.groupId()),
+            coordinator -> coordinator.consumerGroupHeartbeat(context, request)
+        ).exceptionally(exception -> {
+            if (exception instanceof UnknownTopicOrPartitionException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.COORDINATOR_NOT_AVAILABLE.code());
+            }
+
+            if (exception instanceof NotLeaderOrFollowerException ||
+                exception instanceof NotEnoughReplicasException ||
+                exception instanceof KafkaStorageException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.NOT_COORDINATOR.code());
+            }
+
+            if (exception instanceof RecordTooLargeException ||
+                exception instanceof RecordBatchTooLargeException ||
+                exception instanceof InvalidFetchSizeException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.UNKNOWN_SERVER_ERROR.code());
+            }
+
+            return new ConsumerGroupHeartbeatResponseData()
+                .setErrorCode(Errors.forException(exception).code())
+                .setErrorMessage(exception.getMessage());
+        });
+    }
+
+    /**
+     * See {@link GroupCoordinator#joinGroup(RequestContext, JoinGroupRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<JoinGroupResponseData> joinGroup(
+        RequestContext context,
+        JoinGroupRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#syncGroup(RequestContext, SyncGroupRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<SyncGroupResponseData> syncGroup(
+        RequestContext context,
+        SyncGroupRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#heartbeat(RequestContext, HeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<HeartbeatResponseData> heartbeat(
+        RequestContext context,
+        HeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#leaveGroup(RequestContext, LeaveGroupRequestData)}.
+     */
+    @Override
+    public CompletableFuture<LeaveGroupResponseData> leaveGroup(
+        RequestContext context,
+        LeaveGroupRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#listGroups(RequestContext, ListGroupsRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ListGroupsResponseData> listGroups(
+        RequestContext context,
+        ListGroupsRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#describeGroups(RequestContext, List)}.
+     */
+    @Override
+    public CompletableFuture<List<DescribeGroupsResponseData.DescribedGroup>> describeGroups(
+        RequestContext context,
+        List<String> groupIds
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#deleteGroups(RequestContext, List, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<DeleteGroupsResponseData.DeletableGroupResultCollection> deleteGroups(
+        RequestContext context,
+        List<String> groupIds,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#fetchOffsets(RequestContext, String, List, boolean)}.
+     */
+    @Override
+    public CompletableFuture<List<OffsetFetchResponseData.OffsetFetchResponseTopics>> fetchOffsets(
+        RequestContext context,
+        String groupId,
+        List<OffsetFetchRequestData.OffsetFetchRequestTopics> topics,
+        boolean requireStable
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#fetchAllOffsets(RequestContext, String, boolean)}.
+     */
+    @Override
+    public CompletableFuture<List<OffsetFetchResponseData.OffsetFetchResponseTopics>> fetchAllOffsets(
+        RequestContext context,
+        String groupId,
+        boolean requireStable
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#commitOffsets(RequestContext, OffsetCommitRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<OffsetCommitResponseData> commitOffsets(
+        RequestContext context,
+        OffsetCommitRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#commitTransactionalOffsets(RequestContext, TxnOffsetCommitRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<TxnOffsetCommitResponseData> commitTransactionalOffsets(
+        RequestContext context,
+        TxnOffsetCommitRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#deleteOffsets(RequestContext, OffsetDeleteRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<OffsetDeleteResponseData> deleteOffsets(
+        RequestContext context,
+        OffsetDeleteRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#onTransactionCompleted(long, Iterable, TransactionResult)}.
+     */
+    @Override
+    public void onTransactionCompleted(
+        long producerId,
+        Iterable<TopicPartition> partitions,
+        TransactionResult transactionResult
+    ) {
+        throwIfNotActive();

Review Comment:
   got it. thanks for clarifying. maybe we don't need a todo or anything if we at least have the jira.
   Some of the other methods had "not yet implemented" in the body so I wasn't sure about the ones that didn't.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1237133352


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;

Review Comment:
   Right.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1235650169


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;
+
+    /**
+     *
+     * @param logContext
+     * @param config
+     * @param runtime
+     */
+    GroupCoordinatorService(
+        LogContext logContext,
+        GroupCoordinatorConfig config,
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime
+    ) {
+        this.log = logContext.logger(CoordinatorLoader.class);
+        this.config = config;
+        this.runtime = runtime;
+    }
+
+    /**
+     * Throws CoordinatorNotAvailableException if the not active.
+     */
+    private void throwIfNotActive() {
+        if (!isActive.get()) {
+            throw Errors.COORDINATOR_NOT_AVAILABLE.exception();
+        }
+    }
+
+    /**
+     * @return TODO
+     */
+    private TopicPartition topicPartitionFor(
+        String groupId
+    ) {
+        return new TopicPartition(Topic.GROUP_METADATA_TOPIC_NAME, partitionFor(groupId));
+    }
+
+    /**
+     * See {@link GroupCoordinator#partitionFor(String)}
+     */
+    @Override
+    public int partitionFor(
+        String groupId
+    ) {
+        throwIfNotActive();
+        return Utils.abs(groupId.hashCode()) % numPartitions;
+    }
+
+    /**
+     * See {@link GroupCoordinator#consumerGroupHeartbeat(RequestContext, ConsumerGroupHeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ConsumerGroupHeartbeatResponseData> consumerGroupHeartbeat(
+        RequestContext context,
+        ConsumerGroupHeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return runtime.scheduleWriteOperation(
+            "consumer-group-heartbeat",
+            topicPartitionFor(request.groupId()),
+            coordinator -> coordinator.consumerGroupHeartbeat(context, request)
+        ).exceptionally(exception -> {
+            if (exception instanceof UnknownTopicOrPartitionException) {

Review Comment:
   I saw this code, but it seems like we handle it different right?
   
   notenoughreplicas moved to not coordinator when it used to be coordinator not available for example.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1235693242


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;
+
+    /**
+     *
+     * @param logContext
+     * @param config
+     * @param runtime
+     */
+    GroupCoordinatorService(
+        LogContext logContext,
+        GroupCoordinatorConfig config,
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime
+    ) {
+        this.log = logContext.logger(CoordinatorLoader.class);
+        this.config = config;
+        this.runtime = runtime;
+    }
+
+    /**
+     * Throws CoordinatorNotAvailableException if the not active.
+     */
+    private void throwIfNotActive() {
+        if (!isActive.get()) {
+            throw Errors.COORDINATOR_NOT_AVAILABLE.exception();
+        }
+    }
+
+    /**
+     * @return TODO
+     */
+    private TopicPartition topicPartitionFor(
+        String groupId
+    ) {
+        return new TopicPartition(Topic.GROUP_METADATA_TOPIC_NAME, partitionFor(groupId));
+    }
+
+    /**
+     * See {@link GroupCoordinator#partitionFor(String)}
+     */
+    @Override
+    public int partitionFor(
+        String groupId
+    ) {
+        throwIfNotActive();
+        return Utils.abs(groupId.hashCode()) % numPartitions;
+    }
+
+    /**
+     * See {@link GroupCoordinator#consumerGroupHeartbeat(RequestContext, ConsumerGroupHeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ConsumerGroupHeartbeatResponseData> consumerGroupHeartbeat(
+        RequestContext context,
+        ConsumerGroupHeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return runtime.scheduleWriteOperation(
+            "consumer-group-heartbeat",
+            topicPartitionFor(request.groupId()),
+            coordinator -> coordinator.consumerGroupHeartbeat(context, request)
+        ).exceptionally(exception -> {
+            if (exception instanceof UnknownTopicOrPartitionException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.COORDINATOR_NOT_AVAILABLE.code());
+            }
+
+            if (exception instanceof NotLeaderOrFollowerException ||
+                exception instanceof NotEnoughReplicasException ||
+                exception instanceof KafkaStorageException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.NOT_COORDINATOR.code());
+            }
+
+            if (exception instanceof RecordTooLargeException ||
+                exception instanceof RecordBatchTooLargeException ||
+                exception instanceof InvalidFetchSizeException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.UNKNOWN_SERVER_ERROR.code());
+            }
+
+            return new ConsumerGroupHeartbeatResponseData()
+                .setErrorCode(Errors.forException(exception).code())
+                .setErrorMessage(exception.getMessage());
+        });
+    }
+
+    /**
+     * See {@link GroupCoordinator#joinGroup(RequestContext, JoinGroupRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<JoinGroupResponseData> joinGroup(
+        RequestContext context,
+        JoinGroupRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#syncGroup(RequestContext, SyncGroupRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<SyncGroupResponseData> syncGroup(
+        RequestContext context,
+        SyncGroupRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#heartbeat(RequestContext, HeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<HeartbeatResponseData> heartbeat(
+        RequestContext context,
+        HeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#leaveGroup(RequestContext, LeaveGroupRequestData)}.
+     */
+    @Override
+    public CompletableFuture<LeaveGroupResponseData> leaveGroup(
+        RequestContext context,
+        LeaveGroupRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#listGroups(RequestContext, ListGroupsRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ListGroupsResponseData> listGroups(
+        RequestContext context,
+        ListGroupsRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#describeGroups(RequestContext, List)}.
+     */
+    @Override
+    public CompletableFuture<List<DescribeGroupsResponseData.DescribedGroup>> describeGroups(
+        RequestContext context,
+        List<String> groupIds
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#deleteGroups(RequestContext, List, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<DeleteGroupsResponseData.DeletableGroupResultCollection> deleteGroups(
+        RequestContext context,
+        List<String> groupIds,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#fetchOffsets(RequestContext, String, List, boolean)}.
+     */
+    @Override
+    public CompletableFuture<List<OffsetFetchResponseData.OffsetFetchResponseTopics>> fetchOffsets(
+        RequestContext context,
+        String groupId,
+        List<OffsetFetchRequestData.OffsetFetchRequestTopics> topics,
+        boolean requireStable
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#fetchAllOffsets(RequestContext, String, boolean)}.
+     */
+    @Override
+    public CompletableFuture<List<OffsetFetchResponseData.OffsetFetchResponseTopics>> fetchAllOffsets(
+        RequestContext context,
+        String groupId,
+        boolean requireStable
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#commitOffsets(RequestContext, OffsetCommitRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<OffsetCommitResponseData> commitOffsets(
+        RequestContext context,
+        OffsetCommitRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#commitTransactionalOffsets(RequestContext, TxnOffsetCommitRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<TxnOffsetCommitResponseData> commitTransactionalOffsets(
+        RequestContext context,
+        TxnOffsetCommitRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#deleteOffsets(RequestContext, OffsetDeleteRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<OffsetDeleteResponseData> deleteOffsets(
+        RequestContext context,
+        OffsetDeleteRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#onTransactionCompleted(long, Iterable, TransactionResult)}.
+     */
+    @Override
+    public void onTransactionCompleted(
+        long producerId,
+        Iterable<TopicPartition> partitions,
+        TransactionResult transactionResult
+    ) {
+        throwIfNotActive();

Review Comment:
   Yeah, we have to implement of these methods. We have JIRAs for all of them...



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1230229603


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;
+
+    /**
+     *
+     * @param logContext
+     * @param config
+     * @param runtime
+     */
+    GroupCoordinatorService(
+        LogContext logContext,
+        GroupCoordinatorConfig config,
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime
+    ) {
+        this.log = logContext.logger(CoordinatorLoader.class);
+        this.config = config;
+        this.runtime = runtime;
+    }
+
+    /**
+     * Throws CoordinatorNotAvailableException if the not active.
+     */
+    private void throwIfNotActive() {
+        if (!isActive.get()) {
+            throw Errors.COORDINATOR_NOT_AVAILABLE.exception();
+        }
+    }
+
+    /**
+     * @return TODO

Review Comment:
   nit: todo here



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1230246630


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;
+
+    /**
+     *
+     * @param logContext
+     * @param config
+     * @param runtime
+     */
+    GroupCoordinatorService(
+        LogContext logContext,
+        GroupCoordinatorConfig config,
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime
+    ) {
+        this.log = logContext.logger(CoordinatorLoader.class);
+        this.config = config;
+        this.runtime = runtime;
+    }
+
+    /**
+     * Throws CoordinatorNotAvailableException if the not active.
+     */
+    private void throwIfNotActive() {
+        if (!isActive.get()) {
+            throw Errors.COORDINATOR_NOT_AVAILABLE.exception();
+        }
+    }
+
+    /**
+     * @return TODO
+     */
+    private TopicPartition topicPartitionFor(
+        String groupId
+    ) {
+        return new TopicPartition(Topic.GROUP_METADATA_TOPIC_NAME, partitionFor(groupId));
+    }
+
+    /**
+     * See {@link GroupCoordinator#partitionFor(String)}
+     */
+    @Override
+    public int partitionFor(
+        String groupId
+    ) {
+        throwIfNotActive();
+        return Utils.abs(groupId.hashCode()) % numPartitions;
+    }
+
+    /**
+     * See {@link GroupCoordinator#consumerGroupHeartbeat(RequestContext, ConsumerGroupHeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ConsumerGroupHeartbeatResponseData> consumerGroupHeartbeat(
+        RequestContext context,
+        ConsumerGroupHeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return runtime.scheduleWriteOperation(
+            "consumer-group-heartbeat",
+            topicPartitionFor(request.groupId()),
+            coordinator -> coordinator.consumerGroupHeartbeat(context, request)
+        ).exceptionally(exception -> {
+            if (exception instanceof UnknownTopicOrPartitionException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.COORDINATOR_NOT_AVAILABLE.code());
+            }
+
+            if (exception instanceof NotLeaderOrFollowerException ||
+                exception instanceof NotEnoughReplicasException ||
+                exception instanceof KafkaStorageException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.NOT_COORDINATOR.code());
+            }
+
+            if (exception instanceof RecordTooLargeException ||
+                exception instanceof RecordBatchTooLargeException ||
+                exception instanceof InvalidFetchSizeException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.UNKNOWN_SERVER_ERROR.code());
+            }
+
+            return new ConsumerGroupHeartbeatResponseData()
+                .setErrorCode(Errors.forException(exception).code())
+                .setErrorMessage(exception.getMessage());
+        });
+    }
+
+    /**
+     * See {@link GroupCoordinator#joinGroup(RequestContext, JoinGroupRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<JoinGroupResponseData> joinGroup(
+        RequestContext context,
+        JoinGroupRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#syncGroup(RequestContext, SyncGroupRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<SyncGroupResponseData> syncGroup(
+        RequestContext context,
+        SyncGroupRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#heartbeat(RequestContext, HeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<HeartbeatResponseData> heartbeat(
+        RequestContext context,
+        HeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#leaveGroup(RequestContext, LeaveGroupRequestData)}.
+     */
+    @Override
+    public CompletableFuture<LeaveGroupResponseData> leaveGroup(
+        RequestContext context,
+        LeaveGroupRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#listGroups(RequestContext, ListGroupsRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ListGroupsResponseData> listGroups(
+        RequestContext context,
+        ListGroupsRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#describeGroups(RequestContext, List)}.
+     */
+    @Override
+    public CompletableFuture<List<DescribeGroupsResponseData.DescribedGroup>> describeGroups(
+        RequestContext context,
+        List<String> groupIds
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#deleteGroups(RequestContext, List, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<DeleteGroupsResponseData.DeletableGroupResultCollection> deleteGroups(
+        RequestContext context,
+        List<String> groupIds,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#fetchOffsets(RequestContext, String, List, boolean)}.
+     */
+    @Override
+    public CompletableFuture<List<OffsetFetchResponseData.OffsetFetchResponseTopics>> fetchOffsets(
+        RequestContext context,
+        String groupId,
+        List<OffsetFetchRequestData.OffsetFetchRequestTopics> topics,
+        boolean requireStable
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#fetchAllOffsets(RequestContext, String, boolean)}.
+     */
+    @Override
+    public CompletableFuture<List<OffsetFetchResponseData.OffsetFetchResponseTopics>> fetchAllOffsets(
+        RequestContext context,
+        String groupId,
+        boolean requireStable
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#commitOffsets(RequestContext, OffsetCommitRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<OffsetCommitResponseData> commitOffsets(
+        RequestContext context,
+        OffsetCommitRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#commitTransactionalOffsets(RequestContext, TxnOffsetCommitRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<TxnOffsetCommitResponseData> commitTransactionalOffsets(
+        RequestContext context,
+        TxnOffsetCommitRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#deleteOffsets(RequestContext, OffsetDeleteRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<OffsetDeleteResponseData> deleteOffsets(
+        RequestContext context,
+        OffsetDeleteRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#onTransactionCompleted(long, Iterable, TransactionResult)}.
+     */
+    @Override
+    public void onTransactionCompleted(
+        long producerId,
+        Iterable<TopicPartition> partitions,
+        TransactionResult transactionResult
+    ) {
+        throwIfNotActive();

Review Comment:
   do we plan to add more to these? 



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac commented on pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on PR #13812:
URL: https://github.com/apache/kafka/pull/13812#issuecomment-1602115656

   Failed tests are not related:
   ```
   Build / JDK 17 and Scala 2.13 / testOffsetTranslationBehindReplicationFlow() – org.apache.kafka.connect.mirror.integration.IdentityReplicationIntegrationTest
   1m 50s
   Build / JDK 8 and Scala 2.12 / testBumpTransactionalEpoch(String).quorum=kraft – kafka.api.TransactionsTest
   1m 14s
   Build / JDK 11 and Scala 2.13 / testBalancePartitionLeaders() – org.apache.kafka.controller.QuorumControllerTest
   ```


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1222312423


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/ReplicatedGroupCoordinator.java:
##########
@@ -0,0 +1,202 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.protocol.ApiMessage;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupCurrentMemberAssignmentKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupCurrentMemberAssignmentValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMemberMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMemberMetadataValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupMetadataValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupPartitionMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupPartitionMetadataValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMemberKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMemberValue;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMetadataKey;
+import org.apache.kafka.coordinator.group.generated.ConsumerGroupTargetAssignmentMetadataValue;
+import org.apache.kafka.coordinator.group.runtime.Coordinator;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilder;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorResult;
+import org.apache.kafka.server.common.ApiMessageAndVersion;
+import org.apache.kafka.timeline.SnapshotRegistry;
+
+/**
+ * The group coordinator replicated state machine that manages the metadata of all generic and
+ * consumer groups. It holds the hard and the soft state of the groups. This class has two kinds
+ * of methods:
+ * 1) The request handlers which handle the requests and generate a response and records to
+ *    mutate the hard state. Those records will be written by the runtime and applied to the
+ *    hard state via the replay methods.
+ * 2) The replay methods which apply records to the hard state. Those are used in the request
+ *    handling as well as during the initial loading of the records from the partitions.
+ */
+public class ReplicatedGroupCoordinator implements Coordinator<Record> {

Review Comment:
   Is this called replicated because we replicate the state? (In other words, this is the implementation to get the hard state we already have for the current group coordinator)



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1230937577


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;
+
+    /**
+     *
+     * @param logContext
+     * @param config
+     * @param runtime
+     */
+    GroupCoordinatorService(
+        LogContext logContext,
+        GroupCoordinatorConfig config,
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime
+    ) {
+        this.log = logContext.logger(CoordinatorLoader.class);
+        this.config = config;
+        this.runtime = runtime;
+    }
+
+    /**
+     * Throws CoordinatorNotAvailableException if the not active.
+     */
+    private void throwIfNotActive() {
+        if (!isActive.get()) {
+            throw Errors.COORDINATOR_NOT_AVAILABLE.exception();
+        }
+    }
+
+    /**
+     * @return TODO
+     */
+    private TopicPartition topicPartitionFor(
+        String groupId
+    ) {
+        return new TopicPartition(Topic.GROUP_METADATA_TOPIC_NAME, partitionFor(groupId));
+    }
+
+    /**
+     * See {@link GroupCoordinator#partitionFor(String)}
+     */
+    @Override
+    public int partitionFor(
+        String groupId
+    ) {
+        throwIfNotActive();
+        return Utils.abs(groupId.hashCode()) % numPartitions;
+    }
+
+    /**
+     * See {@link GroupCoordinator#consumerGroupHeartbeat(RequestContext, ConsumerGroupHeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ConsumerGroupHeartbeatResponseData> consumerGroupHeartbeat(
+        RequestContext context,
+        ConsumerGroupHeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return runtime.scheduleWriteOperation(
+            "consumer-group-heartbeat",
+            topicPartitionFor(request.groupId()),
+            coordinator -> coordinator.consumerGroupHeartbeat(context, request)
+        ).exceptionally(exception -> {
+            if (exception instanceof UnknownTopicOrPartitionException) {

Review Comment:
   Yes. We have similar logic in the scala code [here](https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/coordinator/group/GroupMetadataManager.scala#L288). The main difference is that we have to do it at a different place now.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1230195090


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorConfig.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.coordinator.group.assignor.PartitionAssignor;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * The group coordinator configurations.
+ */
+public class GroupCoordinatorConfig {
+    public static class Builder {
+        private int numThreads = 1;

Review Comment:
   I was also wondering about how config defs worked with documentation. Thanks for filling this JIRA.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1235651106


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;

Review Comment:
   It should only be done manually via the tests right?



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1229221067


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorConfig.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.coordinator.group.assignor.PartitionAssignor;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * The group coordinator configurations.
+ */
+public class GroupCoordinatorConfig {
+    public static class Builder {
+        private int numThreads = 1;

Review Comment:
   Interesting pattern used in RemoteLogManagerConfig. As we define the properties in the `ConfigDef` in that class and not in the `ConfigDef` used in `KafkaConfig`, all the remote storage properties are not documented in the documentation. The documentation is generated based on the `ConfigDef` in `KafkaConfig`. I suppose that we missed this... We use a similar pattern for `RaftConfig` but in this case we defined the properties in two places. I am not a fan of this because it is error prone. It would be better if we could somehow add a `ConfigDef` to another `ConfigDef` to make this automatic and transparent. I will play a bit with this...
   
   In this case, all the properties of the group coordinator and their default values are already defined in KafkaConfig and I just wanted to have a container to move them around in the java module.
   
   However, I agree with the constants part of your comment.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1230293541


##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/GroupCoordinatorServiceTest.java:
##########
@@ -0,0 +1,269 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.CoordinatorNotAvailableException;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.InvalidRequestException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.coordinator.group.assignor.RangeAssignor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.ArgumentMatchers;
+
+import java.util.Collections;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.stream.Stream;
+
+import static org.apache.kafka.coordinator.group.TestUtil.requestContext;
+import static org.apache.kafka.test.TestUtils.assertFutureThrows;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class GroupCoordinatorServiceTest {
+
+    @SuppressWarnings("unchecked")
+    private CoordinatorRuntime<ReplicatedGroupCoordinator, Record> mockRuntime() {
+        return (CoordinatorRuntime<ReplicatedGroupCoordinator, Record>) mock(CoordinatorRuntime.class);
+    }
+
+    private GroupCoordinatorConfig createConfig() {
+        return new GroupCoordinatorConfig(
+            1,
+            45,
+            5,
+            Integer.MAX_VALUE,
+            Collections.singletonList(new RangeAssignor()),
+            1000
+        );
+    }
+
+    @Test
+    public void testStartupShutdown() throws Exception {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        service.startup(() -> 1);
+        service.shutdown();
+
+        verify(runtime, times(1)).close();
+    }
+
+    @Test
+    public void testConsumerGroupHeartbeatWhenNotStarted() {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        ConsumerGroupHeartbeatRequestData request = new ConsumerGroupHeartbeatRequestData()
+            .setGroupId("foo");
+
+        assertFutureThrows(
+            service.consumerGroupHeartbeat(
+                requestContext(ApiKeys.CONSUMER_GROUP_HEARTBEAT),
+                request
+            ),
+            CoordinatorNotAvailableException.class
+        );
+    }
+
+    @Test
+    public void testConsumerGroupHeartbeat() throws ExecutionException, InterruptedException, TimeoutException {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        ConsumerGroupHeartbeatRequestData request = new ConsumerGroupHeartbeatRequestData()
+            .setGroupId("foo");
+
+        service.startup(() -> 1);
+
+        when(runtime.scheduleWriteOperation(
+            ArgumentMatchers.eq("consumer-group-heartbeat"),
+            ArgumentMatchers.eq(new TopicPartition("__consumer_offsets", 0)),
+            ArgumentMatchers.any()
+        )).thenReturn(CompletableFuture.completedFuture(
+            new ConsumerGroupHeartbeatResponseData()
+        ));
+
+        CompletableFuture<ConsumerGroupHeartbeatResponseData> future = service.consumerGroupHeartbeat(
+            requestContext(ApiKeys.CONSUMER_GROUP_HEARTBEAT),
+            request
+        );
+
+        assertEquals(new ConsumerGroupHeartbeatResponseData(), future.get(5, TimeUnit.SECONDS));
+    }
+
+    private static Stream<Arguments> testConsumerGroupHeartbeatWithExceptionSource() {
+        return Stream.of(
+            Arguments.arguments(new UnknownTopicOrPartitionException(), Errors.COORDINATOR_NOT_AVAILABLE.code(), null),
+            Arguments.arguments(new NotLeaderOrFollowerException(), Errors.NOT_COORDINATOR.code(), null),
+            Arguments.arguments(new NotEnoughReplicasException(), Errors.NOT_COORDINATOR.code(), null),
+            Arguments.arguments(new KafkaStorageException(), Errors.NOT_COORDINATOR.code(), null),
+            Arguments.arguments(new RecordTooLargeException(), Errors.UNKNOWN_SERVER_ERROR.code(), null),
+            Arguments.arguments(new RecordBatchTooLargeException(), Errors.UNKNOWN_SERVER_ERROR.code(), null),
+            Arguments.arguments(new InvalidFetchSizeException(""), Errors.UNKNOWN_SERVER_ERROR.code(), null),
+            Arguments.arguments(new InvalidRequestException("Invalid"), Errors.INVALID_REQUEST.code(), "Invalid")
+        );
+    }
+
+    @ParameterizedTest
+    @MethodSource("testConsumerGroupHeartbeatWithExceptionSource")
+    public void testConsumerGroupHeartbeatWithException(
+        Throwable exception,
+        short expectedErrorCode,
+        String expectedErrorMessage
+    ) throws ExecutionException, InterruptedException, TimeoutException {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        ConsumerGroupHeartbeatRequestData request = new ConsumerGroupHeartbeatRequestData()
+            .setGroupId("foo");
+
+        service.startup(() -> 1);
+
+        when(runtime.scheduleWriteOperation(
+            ArgumentMatchers.eq("consumer-group-heartbeat"),
+            ArgumentMatchers.eq(new TopicPartition("__consumer_offsets", 0)),
+            ArgumentMatchers.any()
+        )).thenReturn(FutureUtils.failedFuture(exception));
+
+        CompletableFuture<ConsumerGroupHeartbeatResponseData> future = service.consumerGroupHeartbeat(
+            requestContext(ApiKeys.CONSUMER_GROUP_HEARTBEAT),
+            request
+        );
+
+        assertEquals(
+            new ConsumerGroupHeartbeatResponseData()
+                .setErrorCode(expectedErrorCode)
+                .setErrorMessage(expectedErrorMessage),
+            future.get(5, TimeUnit.SECONDS)
+        );
+    }
+
+    @Test
+    public void testPartitionFor() {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        assertThrows(CoordinatorNotAvailableException.class,
+            () -> service.partitionFor("foo"));
+
+        service.startup(() -> 10);
+
+        assertTrue(service.partitionFor("foo") >= 0);

Review Comment:
   why don't we just use the hashing algorithm to get the actual partition?



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1230937946


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;
+
+    /**
+     *
+     * @param logContext
+     * @param config
+     * @param runtime
+     */
+    GroupCoordinatorService(
+        LogContext logContext,
+        GroupCoordinatorConfig config,
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime
+    ) {
+        this.log = logContext.logger(CoordinatorLoader.class);
+        this.config = config;
+        this.runtime = runtime;
+    }
+
+    /**
+     * Throws CoordinatorNotAvailableException if the not active.
+     */
+    private void throwIfNotActive() {
+        if (!isActive.get()) {
+            throw Errors.COORDINATOR_NOT_AVAILABLE.exception();
+        }
+    }
+
+    /**
+     * @return TODO
+     */
+    private TopicPartition topicPartitionFor(
+        String groupId
+    ) {
+        return new TopicPartition(Topic.GROUP_METADATA_TOPIC_NAME, partitionFor(groupId));
+    }
+
+    /**
+     * See {@link GroupCoordinator#partitionFor(String)}
+     */
+    @Override
+    public int partitionFor(
+        String groupId
+    ) {
+        throwIfNotActive();
+        return Utils.abs(groupId.hashCode()) % numPartitions;
+    }
+
+    /**
+     * See {@link GroupCoordinator#consumerGroupHeartbeat(RequestContext, ConsumerGroupHeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ConsumerGroupHeartbeatResponseData> consumerGroupHeartbeat(
+        RequestContext context,
+        ConsumerGroupHeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return runtime.scheduleWriteOperation(
+            "consumer-group-heartbeat",
+            topicPartitionFor(request.groupId()),
+            coordinator -> coordinator.consumerGroupHeartbeat(context, request)
+        ).exceptionally(exception -> {
+            if (exception instanceof UnknownTopicOrPartitionException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.COORDINATOR_NOT_AVAILABLE.code());
+            }
+
+            if (exception instanceof NotLeaderOrFollowerException ||
+                exception instanceof NotEnoughReplicasException ||
+                exception instanceof KafkaStorageException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.NOT_COORDINATOR.code());
+            }
+
+            if (exception instanceof RecordTooLargeException ||
+                exception instanceof RecordBatchTooLargeException ||
+                exception instanceof InvalidFetchSizeException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.UNKNOWN_SERVER_ERROR.code());
+            }
+
+            return new ConsumerGroupHeartbeatResponseData()
+                .setErrorCode(Errors.forException(exception).code())
+                .setErrorMessage(exception.getMessage());
+        });
+    }
+
+    /**
+     * See {@link GroupCoordinator#joinGroup(RequestContext, JoinGroupRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<JoinGroupResponseData> joinGroup(
+        RequestContext context,
+        JoinGroupRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#syncGroup(RequestContext, SyncGroupRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<SyncGroupResponseData> syncGroup(
+        RequestContext context,
+        SyncGroupRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#heartbeat(RequestContext, HeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<HeartbeatResponseData> heartbeat(
+        RequestContext context,
+        HeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#leaveGroup(RequestContext, LeaveGroupRequestData)}.
+     */
+    @Override
+    public CompletableFuture<LeaveGroupResponseData> leaveGroup(
+        RequestContext context,
+        LeaveGroupRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#listGroups(RequestContext, ListGroupsRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ListGroupsResponseData> listGroups(
+        RequestContext context,
+        ListGroupsRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#describeGroups(RequestContext, List)}.
+     */
+    @Override
+    public CompletableFuture<List<DescribeGroupsResponseData.DescribedGroup>> describeGroups(
+        RequestContext context,
+        List<String> groupIds
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#deleteGroups(RequestContext, List, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<DeleteGroupsResponseData.DeletableGroupResultCollection> deleteGroups(
+        RequestContext context,
+        List<String> groupIds,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#fetchOffsets(RequestContext, String, List, boolean)}.
+     */
+    @Override
+    public CompletableFuture<List<OffsetFetchResponseData.OffsetFetchResponseTopics>> fetchOffsets(
+        RequestContext context,
+        String groupId,
+        List<OffsetFetchRequestData.OffsetFetchRequestTopics> topics,
+        boolean requireStable
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#fetchAllOffsets(RequestContext, String, boolean)}.
+     */
+    @Override
+    public CompletableFuture<List<OffsetFetchResponseData.OffsetFetchResponseTopics>> fetchAllOffsets(
+        RequestContext context,
+        String groupId,
+        boolean requireStable
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#commitOffsets(RequestContext, OffsetCommitRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<OffsetCommitResponseData> commitOffsets(
+        RequestContext context,
+        OffsetCommitRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#commitTransactionalOffsets(RequestContext, TxnOffsetCommitRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<TxnOffsetCommitResponseData> commitTransactionalOffsets(
+        RequestContext context,
+        TxnOffsetCommitRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#deleteOffsets(RequestContext, OffsetDeleteRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<OffsetDeleteResponseData> deleteOffsets(
+        RequestContext context,
+        OffsetDeleteRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#onTransactionCompleted(long, Iterable, TransactionResult)}.
+     */
+    @Override
+    public void onTransactionCompleted(
+        long producerId,
+        Iterable<TopicPartition> partitions,
+        TransactionResult transactionResult
+    ) {
+        throwIfNotActive();

Review Comment:
   What do you mean? Different ones?



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1235640808


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;
+
+    /**
+     *
+     * @param logContext
+     * @param config
+     * @param runtime
+     */
+    GroupCoordinatorService(
+        LogContext logContext,
+        GroupCoordinatorConfig config,
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime
+    ) {
+        this.log = logContext.logger(CoordinatorLoader.class);
+        this.config = config;
+        this.runtime = runtime;
+    }
+
+    /**
+     * Throws CoordinatorNotAvailableException if the not active.
+     */
+    private void throwIfNotActive() {
+        if (!isActive.get()) {
+            throw Errors.COORDINATOR_NOT_AVAILABLE.exception();
+        }
+    }
+
+    /**
+     * @return TODO
+     */
+    private TopicPartition topicPartitionFor(
+        String groupId
+    ) {
+        return new TopicPartition(Topic.GROUP_METADATA_TOPIC_NAME, partitionFor(groupId));
+    }
+
+    /**
+     * See {@link GroupCoordinator#partitionFor(String)}
+     */
+    @Override
+    public int partitionFor(
+        String groupId
+    ) {
+        throwIfNotActive();
+        return Utils.abs(groupId.hashCode()) % numPartitions;
+    }
+
+    /**
+     * See {@link GroupCoordinator#consumerGroupHeartbeat(RequestContext, ConsumerGroupHeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ConsumerGroupHeartbeatResponseData> consumerGroupHeartbeat(
+        RequestContext context,
+        ConsumerGroupHeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return runtime.scheduleWriteOperation(
+            "consumer-group-heartbeat",
+            topicPartitionFor(request.groupId()),
+            coordinator -> coordinator.consumerGroupHeartbeat(context, request)
+        ).exceptionally(exception -> {
+            if (exception instanceof UnknownTopicOrPartitionException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.COORDINATOR_NOT_AVAILABLE.code());
+            }
+
+            if (exception instanceof NotLeaderOrFollowerException ||
+                exception instanceof NotEnoughReplicasException ||
+                exception instanceof KafkaStorageException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.NOT_COORDINATOR.code());
+            }
+
+            if (exception instanceof RecordTooLargeException ||
+                exception instanceof RecordBatchTooLargeException ||
+                exception instanceof InvalidFetchSizeException) {
+                return new ConsumerGroupHeartbeatResponseData()
+                    .setErrorCode(Errors.UNKNOWN_SERVER_ERROR.code());
+            }
+
+            return new ConsumerGroupHeartbeatResponseData()
+                .setErrorCode(Errors.forException(exception).code())
+                .setErrorMessage(exception.getMessage());
+        });
+    }
+
+    /**
+     * See {@link GroupCoordinator#joinGroup(RequestContext, JoinGroupRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<JoinGroupResponseData> joinGroup(
+        RequestContext context,
+        JoinGroupRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#syncGroup(RequestContext, SyncGroupRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<SyncGroupResponseData> syncGroup(
+        RequestContext context,
+        SyncGroupRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#heartbeat(RequestContext, HeartbeatRequestData)}.
+     */
+    @Override
+    public CompletableFuture<HeartbeatResponseData> heartbeat(
+        RequestContext context,
+        HeartbeatRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#leaveGroup(RequestContext, LeaveGroupRequestData)}.
+     */
+    @Override
+    public CompletableFuture<LeaveGroupResponseData> leaveGroup(
+        RequestContext context,
+        LeaveGroupRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#listGroups(RequestContext, ListGroupsRequestData)}.
+     */
+    @Override
+    public CompletableFuture<ListGroupsResponseData> listGroups(
+        RequestContext context,
+        ListGroupsRequestData request
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#describeGroups(RequestContext, List)}.
+     */
+    @Override
+    public CompletableFuture<List<DescribeGroupsResponseData.DescribedGroup>> describeGroups(
+        RequestContext context,
+        List<String> groupIds
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#deleteGroups(RequestContext, List, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<DeleteGroupsResponseData.DeletableGroupResultCollection> deleteGroups(
+        RequestContext context,
+        List<String> groupIds,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#fetchOffsets(RequestContext, String, List, boolean)}.
+     */
+    @Override
+    public CompletableFuture<List<OffsetFetchResponseData.OffsetFetchResponseTopics>> fetchOffsets(
+        RequestContext context,
+        String groupId,
+        List<OffsetFetchRequestData.OffsetFetchRequestTopics> topics,
+        boolean requireStable
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#fetchAllOffsets(RequestContext, String, boolean)}.
+     */
+    @Override
+    public CompletableFuture<List<OffsetFetchResponseData.OffsetFetchResponseTopics>> fetchAllOffsets(
+        RequestContext context,
+        String groupId,
+        boolean requireStable
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#commitOffsets(RequestContext, OffsetCommitRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<OffsetCommitResponseData> commitOffsets(
+        RequestContext context,
+        OffsetCommitRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#commitTransactionalOffsets(RequestContext, TxnOffsetCommitRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<TxnOffsetCommitResponseData> commitTransactionalOffsets(
+        RequestContext context,
+        TxnOffsetCommitRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#deleteOffsets(RequestContext, OffsetDeleteRequestData, BufferSupplier)}.
+     */
+    @Override
+    public CompletableFuture<OffsetDeleteResponseData> deleteOffsets(
+        RequestContext context,
+        OffsetDeleteRequestData request,
+        BufferSupplier bufferSupplier
+    ) {
+        if (!isActive.get()) {
+            return FutureUtils.failedFuture(Errors.COORDINATOR_NOT_AVAILABLE.exception());
+        }
+
+        return FutureUtils.failedFuture(Errors.UNSUPPORTED_VERSION.exception(
+            "This API is not implemented yet."
+        ));
+    }
+
+    /**
+     * See {@link GroupCoordinator#onTransactionCompleted(long, Iterable, TransactionResult)}.
+     */
+    @Override
+    public void onTransactionCompleted(
+        long producerId,
+        Iterable<TopicPartition> partitions,
+        TransactionResult transactionResult
+    ) {
+        throwIfNotActive();

Review Comment:
   Right now we just throw if not active. Just curious if this is all we plan to do here.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1235694270


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;

Review Comment:
   `numPartitions` is set in `startup` all the time.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1230281526


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;

Review Comment:
   just to confirm this is usually just OffsetsTopicPartitionsProp, but we set to 1 in tests etc?



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1230946396


##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/GroupCoordinatorServiceTest.java:
##########
@@ -0,0 +1,269 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.CoordinatorNotAvailableException;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.InvalidRequestException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.coordinator.group.assignor.RangeAssignor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.ArgumentMatchers;
+
+import java.util.Collections;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.stream.Stream;
+
+import static org.apache.kafka.coordinator.group.TestUtil.requestContext;
+import static org.apache.kafka.test.TestUtils.assertFutureThrows;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class GroupCoordinatorServiceTest {
+
+    @SuppressWarnings("unchecked")
+    private CoordinatorRuntime<ReplicatedGroupCoordinator, Record> mockRuntime() {
+        return (CoordinatorRuntime<ReplicatedGroupCoordinator, Record>) mock(CoordinatorRuntime.class);
+    }
+
+    private GroupCoordinatorConfig createConfig() {
+        return new GroupCoordinatorConfig(
+            1,
+            45,
+            5,
+            Integer.MAX_VALUE,
+            Collections.singletonList(new RangeAssignor()),
+            1000
+        );
+    }
+
+    @Test
+    public void testStartupShutdown() throws Exception {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        service.startup(() -> 1);
+        service.shutdown();
+
+        verify(runtime, times(1)).close();
+    }
+
+    @Test
+    public void testConsumerGroupHeartbeatWhenNotStarted() {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        ConsumerGroupHeartbeatRequestData request = new ConsumerGroupHeartbeatRequestData()
+            .setGroupId("foo");
+
+        assertFutureThrows(
+            service.consumerGroupHeartbeat(
+                requestContext(ApiKeys.CONSUMER_GROUP_HEARTBEAT),
+                request
+            ),
+            CoordinatorNotAvailableException.class
+        );
+    }
+
+    @Test
+    public void testConsumerGroupHeartbeat() throws ExecutionException, InterruptedException, TimeoutException {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        ConsumerGroupHeartbeatRequestData request = new ConsumerGroupHeartbeatRequestData()
+            .setGroupId("foo");
+
+        service.startup(() -> 1);
+
+        when(runtime.scheduleWriteOperation(
+            ArgumentMatchers.eq("consumer-group-heartbeat"),
+            ArgumentMatchers.eq(new TopicPartition("__consumer_offsets", 0)),
+            ArgumentMatchers.any()
+        )).thenReturn(CompletableFuture.completedFuture(
+            new ConsumerGroupHeartbeatResponseData()
+        ));
+
+        CompletableFuture<ConsumerGroupHeartbeatResponseData> future = service.consumerGroupHeartbeat(
+            requestContext(ApiKeys.CONSUMER_GROUP_HEARTBEAT),
+            request
+        );
+
+        assertEquals(new ConsumerGroupHeartbeatResponseData(), future.get(5, TimeUnit.SECONDS));
+    }
+
+    private static Stream<Arguments> testConsumerGroupHeartbeatWithExceptionSource() {
+        return Stream.of(
+            Arguments.arguments(new UnknownTopicOrPartitionException(), Errors.COORDINATOR_NOT_AVAILABLE.code(), null),
+            Arguments.arguments(new NotLeaderOrFollowerException(), Errors.NOT_COORDINATOR.code(), null),
+            Arguments.arguments(new NotEnoughReplicasException(), Errors.NOT_COORDINATOR.code(), null),
+            Arguments.arguments(new KafkaStorageException(), Errors.NOT_COORDINATOR.code(), null),
+            Arguments.arguments(new RecordTooLargeException(), Errors.UNKNOWN_SERVER_ERROR.code(), null),
+            Arguments.arguments(new RecordBatchTooLargeException(), Errors.UNKNOWN_SERVER_ERROR.code(), null),
+            Arguments.arguments(new InvalidFetchSizeException(""), Errors.UNKNOWN_SERVER_ERROR.code(), null),
+            Arguments.arguments(new InvalidRequestException("Invalid"), Errors.INVALID_REQUEST.code(), "Invalid")
+        );
+    }
+
+    @ParameterizedTest
+    @MethodSource("testConsumerGroupHeartbeatWithExceptionSource")
+    public void testConsumerGroupHeartbeatWithException(
+        Throwable exception,
+        short expectedErrorCode,
+        String expectedErrorMessage
+    ) throws ExecutionException, InterruptedException, TimeoutException {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        ConsumerGroupHeartbeatRequestData request = new ConsumerGroupHeartbeatRequestData()
+            .setGroupId("foo");
+
+        service.startup(() -> 1);
+
+        when(runtime.scheduleWriteOperation(
+            ArgumentMatchers.eq("consumer-group-heartbeat"),
+            ArgumentMatchers.eq(new TopicPartition("__consumer_offsets", 0)),
+            ArgumentMatchers.any()
+        )).thenReturn(FutureUtils.failedFuture(exception));
+
+        CompletableFuture<ConsumerGroupHeartbeatResponseData> future = service.consumerGroupHeartbeat(
+            requestContext(ApiKeys.CONSUMER_GROUP_HEARTBEAT),
+            request
+        );
+
+        assertEquals(
+            new ConsumerGroupHeartbeatResponseData()
+                .setErrorCode(expectedErrorCode)
+                .setErrorMessage(expectedErrorMessage),
+            future.get(5, TimeUnit.SECONDS)
+        );
+    }
+
+    @Test
+    public void testPartitionFor() {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        assertThrows(CoordinatorNotAvailableException.class,
+            () -> service.partitionFor("foo"));
+
+        service.startup(() -> 10);
+
+        assertTrue(service.partitionFor("foo") >= 0);
+        assertTrue(service.partitionFor("foo") < 10);
+    }
+
+    @Test
+    public void testGroupMetadataTopicConfigs() {
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime = mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+
+        Properties expectedProperties = new Properties();
+        expectedProperties.put(TopicConfig.CLEANUP_POLICY_CONFIG, TopicConfig.CLEANUP_POLICY_COMPACT);
+        expectedProperties.put(TopicConfig.COMPRESSION_TYPE_CONFIG, BrokerCompressionType.PRODUCER.name);
+        expectedProperties.put(TopicConfig.SEGMENT_BYTES_CONFIG, "1000");
+
+        assertEquals(expectedProperties, service.groupMetadataTopicConfigs());
+    }
+
+    @Test
+    public void testOnElection() {

Review Comment:
   I added an assertion for this in this test. I did the same for testOnResignation.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1230936129


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;
+
+    /**
+     *
+     * @param logContext
+     * @param config
+     * @param runtime
+     */
+    GroupCoordinatorService(
+        LogContext logContext,
+        GroupCoordinatorConfig config,
+        CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime
+    ) {
+        this.log = logContext.logger(CoordinatorLoader.class);
+        this.config = config;
+        this.runtime = runtime;
+    }
+
+    /**
+     * Throws CoordinatorNotAvailableException if the not active.
+     */
+    private void throwIfNotActive() {
+        if (!isActive.get()) {
+            throw Errors.COORDINATOR_NOT_AVAILABLE.exception();
+        }
+    }
+
+    /**
+     * @return TODO

Review Comment:
   Oops. Fixed.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] dajac commented on a diff in pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #13812:
URL: https://github.com/apache/kafka/pull/13812#discussion_r1230939154


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupCoordinatorService.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.coordinator.group;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.errors.InvalidFetchSizeException;
+import org.apache.kafka.common.errors.KafkaStorageException;
+import org.apache.kafka.common.errors.NotEnoughReplicasException;
+import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
+import org.apache.kafka.common.errors.RecordBatchTooLargeException;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.message.DeleteGroupsResponseData;
+import org.apache.kafka.common.message.DescribeGroupsResponseData;
+import org.apache.kafka.common.message.HeartbeatRequestData;
+import org.apache.kafka.common.message.HeartbeatResponseData;
+import org.apache.kafka.common.message.JoinGroupRequestData;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.LeaveGroupRequestData;
+import org.apache.kafka.common.message.LeaveGroupResponseData;
+import org.apache.kafka.common.message.ListGroupsRequestData;
+import org.apache.kafka.common.message.ListGroupsResponseData;
+import org.apache.kafka.common.message.OffsetCommitRequestData;
+import org.apache.kafka.common.message.OffsetCommitResponseData;
+import org.apache.kafka.common.message.OffsetDeleteRequestData;
+import org.apache.kafka.common.message.OffsetDeleteResponseData;
+import org.apache.kafka.common.message.OffsetFetchRequestData;
+import org.apache.kafka.common.message.OffsetFetchResponseData;
+import org.apache.kafka.common.message.SyncGroupRequestData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.message.TxnOffsetCommitRequestData;
+import org.apache.kafka.common.message.TxnOffsetCommitResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.TransactionResult;
+import org.apache.kafka.common.utils.BufferSupplier;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorBuilderSupplier;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorLoader;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorRuntime;
+import org.apache.kafka.coordinator.group.runtime.MultiThreadedEventProcessor;
+import org.apache.kafka.coordinator.group.runtime.PartitionWriter;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.server.record.BrokerCompressionType;
+import org.apache.kafka.server.util.FutureUtils;
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.IntSupplier;
+
+/**
+ * The group coordinator service.
+ */
+public class GroupCoordinatorService implements GroupCoordinator {
+
+    public static class Builder {
+        private final int nodeId;
+        private final GroupCoordinatorConfig config;
+        private PartitionWriter<Record> writer;
+        private CoordinatorLoader<Record> loader;
+
+        public Builder(
+            int nodeId,
+            GroupCoordinatorConfig config
+        ) {
+            this.nodeId = nodeId;
+            this.config = config;
+        }
+
+        public Builder withWriter(PartitionWriter<Record> writer) {
+            this.writer = writer;
+            return this;
+        }
+
+        public Builder withLoader(CoordinatorLoader<Record> loader) {
+            this.loader = loader;
+            return this;
+        }
+
+        public GroupCoordinatorService build() {
+            if (config == null)
+                throw new IllegalArgumentException("Config must be set.");
+            if (writer == null)
+                throw new IllegalArgumentException("Writer must be set.");
+            if (loader == null)
+                throw new IllegalArgumentException("Loader must be set.");
+
+            String logPrefix = String.format("GroupCoordinator id=%d ", nodeId);
+            LogContext logContext = new LogContext(String.format("[%s ]", logPrefix));
+
+            CoordinatorBuilderSupplier<ReplicatedGroupCoordinator, Record> supplier = () ->
+                new ReplicatedGroupCoordinator.Builder(config);
+
+            CoordinatorEventProcessor processor = new MultiThreadedEventProcessor(
+                logContext,
+                "group-coordinator-event-processor-",
+                config.numThreads
+            );
+
+            CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime =
+                new CoordinatorRuntime.Builder<ReplicatedGroupCoordinator, Record>()
+                    .withLogPrefix(logPrefix)
+                    .withLogContext(logContext)
+                    .withEventProcessor(processor)
+                    .withPartitionWriter(writer)
+                    .withLoader(loader)
+                    .withCoordinatorBuilderSupplier(supplier)
+                    .build();
+
+            return new GroupCoordinatorService(
+                logContext,
+                config,
+                runtime
+            );
+        }
+    }
+
+    /**
+     * The logger.
+     */
+    private final Logger log;
+
+    /**
+     * The group coordinator configurations.
+     */
+    private final GroupCoordinatorConfig config;
+
+    /**
+     * The coordinator runtime.
+     */
+    private final CoordinatorRuntime<ReplicatedGroupCoordinator, Record> runtime;
+
+    /**
+     * Boolean indicating whether the coordinator is active or not.
+     */
+    private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+    /**
+     * The number of partitions of the __consumer_offsets topics. This is provided
+     * when the component is started.
+     */
+    private volatile int numPartitions = -1;

Review Comment:
   The __consumer_offsets is created based on `OffsetsTopicPartitionsProp`. However, `numPartitions` is gotten from the metadata cache when the coordinator is started. If the topic was manually created, it could have a different number of partitions.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] jolshan commented on pull request #13812: KAFKA-14462; [18/N] Add GroupCoordinatorService

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on PR #13812:
URL: https://github.com/apache/kafka/pull/13812#issuecomment-1601217809

   Lots of connect failures when trying to shut down the brokers, let's see if this new run is cleaner.


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org