jeffkbkim commented on code in PR #13870:
URL: https://github.com/apache/kafka/pull/13870#discussion_r1235822238


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupMetadataManager.java:
##########
@@ -874,4 +1045,1265 @@ public void replay(
             consumerGroup.updateMember(newMember);
         }
     }
+
+    // Below stores all methods to handle generic group APIs.
+
+    /**
+     * Handle a JoinGroupRequest.
+     *
+     * @param context The request context.
+     * @param request The actual JoinGroup request.
+     *
+     * @return The result that contains records to append if the join group 
phase completes.
+     */
+    public CoordinatorResult<CompletableFuture<Errors>, Record> 
genericGroupJoin(
+        RequestContext context,
+        JoinGroupRequestData request,
+        CompletableFuture<JoinGroupResponseData> responseFuture
+    ) {
+        CoordinatorResult<CompletableFuture<Errors>, Record> result = 
EMPTY_RESULT;
+        String groupId = request.groupId();
+        String memberId = request.memberId();
+        int sessionTimeoutMs = request.sessionTimeoutMs();
+
+        if (sessionTimeoutMs < groupMinSessionTimeoutMs ||
+            sessionTimeoutMs > groupMaxSessionTimeoutMs
+        ) {
+            responseFuture.complete(new JoinGroupResponseData()
+                .setMemberId(memberId)
+                .setErrorCode(Errors.INVALID_SESSION_TIMEOUT.code())
+            );
+        } else {
+            boolean isUnknownMember = memberId.equals(UNKNOWN_MEMBER_ID);
+            // Group is created if it does not exist and the member id is 
UNKNOWN. if member
+            // is specified but group does not exist, request is rejected with 
GROUP_ID_NOT_FOUND
+            GenericGroup group;
+            try {
+                group = (GenericGroup) getOrMaybeCreateGroup(groupId, GENERIC, 
isUnknownMember);
+            } catch (Throwable t) {
+                responseFuture.complete(new JoinGroupResponseData()
+                    .setMemberId(memberId)
+                    .setErrorCode(Errors.forException(t).code())
+                );
+                return EMPTY_RESULT;
+            }
+
+            String joinReason = request.reason();
+            if (joinReason == null || joinReason.isEmpty()) {
+                joinReason = "not provided";
+            }
+
+            if (!acceptJoiningMember(group, memberId)) {
+                group.remove(memberId);
+                responseFuture.complete(new JoinGroupResponseData()
+                    .setMemberId(UNKNOWN_MEMBER_ID)
+                    .setErrorCode(Errors.GROUP_MAX_SIZE_REACHED.code())
+                );
+
+            } else if (isUnknownMember) {
+                result = genericGroupJoinNewMember(
+                    context,
+                    request,
+                    group,
+                    joinReason,
+                    responseFuture
+                );
+            } else {
+                result = genericGroupJoinExistingMember(
+                    context,
+                    request,
+                    group,
+                    joinReason,
+                    responseFuture
+                );
+            }
+
+            // Attempt to complete join group phase. We do not complete
+            // the join group phase if this is the initial rebalance.
+            if (group.isInState(PREPARING_REBALANCE) &&
+                group.hasAllMembersJoined() &&
+                group.generationId() != 0
+            ) {
+                completeGenericGroupJoin(group);
+            }
+        }
+
+        return result;
+    }
+
+    /**
+     * Handle a new member generic group join.
+     *
+     * @param context         The request context.
+     * @param request         The join group request.
+     * @param group           The group to add the member.
+     * @param joinReason      The client reason for the join request.
+     * @param responseFuture  The response future to complete.
+     *
+     * @return The coordinator result that will be appended to the log.
+     */
+    private CoordinatorResult<CompletableFuture<Errors>, Record> 
genericGroupJoinNewMember(
+        RequestContext context,
+        JoinGroupRequestData request,
+        GenericGroup group,
+        String joinReason,
+        CompletableFuture<JoinGroupResponseData> responseFuture
+    ) {
+        List<Protocol> protocols = new ArrayList<>();
+        request.protocols().forEach(protocol -> protocols.add(new 
Protocol(protocol.name(), protocol.metadata())));
+        if (group.isInState(DEAD)) {
+            // if the group is marked as dead, it means some other thread has 
just removed the group
+            // from the coordinator metadata; it is likely that the group has 
migrated to some other
+            // coordinator OR the group is in a transient unstable phase. Let 
the member retry
+            // finding the correct coordinator and rejoin.
+            responseFuture.complete(new JoinGroupResponseData()
+                .setMemberId(UNKNOWN_MEMBER_ID)
+                .setErrorCode(Errors.COORDINATOR_NOT_AVAILABLE.code())
+            );
+        } else if (!group.supportsProtocols(request.protocolType(), 
GenericGroupMember.plainProtocolSet(protocols))) {
+            responseFuture.complete(new JoinGroupResponseData()
+                .setMemberId(UNKNOWN_MEMBER_ID)
+                .setErrorCode(Errors.INCONSISTENT_GROUP_PROTOCOL.code())
+            );
+        } else {
+            Optional<String> groupInstanceId = 
Optional.ofNullable(request.groupInstanceId());
+            String newMemberId = group.generateMemberId(context.clientId(), 
groupInstanceId);
+
+            if (groupInstanceId.isPresent()) {

Review Comment:
   the existing protocol allows empty group instance ids for static member



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

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

Reply via email to