lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1320145450


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MembershipManagerImpl.java:
##########
@@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.clients.consumer.internals;
+
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData;
+import org.apache.kafka.common.protocol.Errors;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Membership manager that maintains group membership for a single member 
following the new
+ * consumer group protocol.
+ * <p/>
+ * This keeps membership state and assignment updated in-memory, based on the 
heartbeat responses
+ * the member receives. It is also responsible for computing assignment for 
the group based on
+ * the metadata, if the member has been selected by the broker to do so.
+ */
+public class MembershipManagerImpl implements MembershipManager {
+
+    private final String groupId;
+    private Optional<String> groupInstanceId;
+    private String memberId;
+    private int memberEpoch;
+    private MemberState state;
+    private AssignorSelection assignorSelection;
+
+    /**
+     * Assignment that the member received from the server and successfully 
processed
+     */
+    private ConsumerGroupHeartbeatResponseData.Assignment currentAssignment;
+
+    /**
+     * List of assignments that the member received from the server but hasn't 
processed yet
+     */
+    private final List<ConsumerGroupHeartbeatResponseData.Assignment> 
targetAssignments;
+
+    public MembershipManagerImpl(String groupId) {
+        this.groupId = groupId;
+        this.state = MemberState.UNJOINED;
+        this.assignorSelection = AssignorSelection.defaultAssignor();
+        this.targetAssignments = new ArrayList<>();
+    }
+
+    public MembershipManagerImpl(String groupId, String groupInstanceId, 
AssignorSelection assignorSelection) {
+        this(groupId);
+        this.groupInstanceId = Optional.ofNullable(groupInstanceId);
+        setAssignorSelection(assignorSelection);
+    }
+
+    /**
+     * Update assignor selection for the member.
+     *
+     * @param assignorSelection New assignor selection
+     * @throws IllegalArgumentException If the provided assignor selection is 
null
+     */
+    public void setAssignorSelection(AssignorSelection assignorSelection) {
+        if (assignorSelection == null) {
+            throw new IllegalArgumentException("Assignor selection cannot be 
null");
+        }
+        this.assignorSelection = assignorSelection;
+    }
+
+    private void transitionTo(MemberState nextState) {
+        if (!nextState.getPreviousValidStates().contains(state)) {
+            // TODO: handle invalid state transition
+            throw new RuntimeException(String.format("Invalid state transition 
from %s to %s",
+                    state, nextState));
+        }
+        this.state = nextState;
+    }
+
+    @Override
+    public String groupId() {
+        return groupId;
+    }
+
+    @Override
+    public Optional<String> groupInstanceId() {
+        return groupInstanceId;
+    }
+
+    @Override
+    public String memberId() {
+        return memberId;
+    }
+
+    @Override
+    public int memberEpoch() {
+        return memberEpoch;
+    }
+
+    @Override
+    public void updateState(ConsumerGroupHeartbeatResponseData response) {
+        if (response.errorCode() == Errors.NONE.code()) {
+            this.memberId = response.memberId();
+            this.memberEpoch = response.memberEpoch();
+            targetAssignments.add(response.assignment());
+            transitionTo(MemberState.PROCESSING_ASSIGNMENT);
+        } else {
+            if (response.errorCode() == Errors.FENCED_MEMBER_EPOCH.code() || 
response.errorCode() == Errors.UNKNOWN_MEMBER_ID.code()) {
+                resetMemberIdAndEpoch();
+                transitionTo(MemberState.UNJOINED);
+            } else if (response.errorCode() == 
Errors.UNRELEASED_INSTANCE_ID.code()) {
+                transitionTo(MemberState.FAILED);
+            }

Review Comment:
   They will all be handled here, to make sure we update the state and other 
info accordingly. 
   
   I had thought of a follow-up PR for that, mainly to make sure I can go over 
the errors and understand the ones that are fatal (state should transition to 
FAILED), and the ones that are somehow recoverable (like the fencing scenario). 
Adding a TODO here for now, will follow up shortly.



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