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


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AssignorSelection.java:
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.ConsumerGroupHeartbeatRequestData;
+
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Selection of a type of assignor used by a member to get partitions assigned 
as part of a
+ * consumer group. Selection could be one of:
+ * <li>CLIENT assignors</li>
+ * <li>SERVER assignors</li>
+ * <p/>
+ * Client assignors include of a list of
+ * {@link 
org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData.Assignor}
+ * Server assignors include a name of the server assignor selected, ex. 
uniform, range.
+ */
+public class AssignorSelection {
+    public enum Type { CLIENT, SERVER }

Review Comment:
   I think the type declaration is redundant because we already know which 
assignor is used by checking their existence.
   
   Also if we use polymorphic constructor, we don't need to pass null's around.
   
   ```
      private Optional<String> serverAssignor = Optional.empty();
       private Optional<List<ConsumerGroupHeartbeatRequestData.Assignor>> 
clientAssignors = Optional.empty();
   
       private 
AssignorSelection(List<ConsumerGroupHeartbeatRequestData.Assignor> assignors) {
           this.clientAssignors = Optional.of(assignors);
       }
   
       private AssignorSelection(String serverAssignor) {
           this.serverAssignor = Optional.of(serverAssignor);
       }
   ```
   



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MemberState.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public enum MemberState {
+
+    /**
+     * Member has not joined a consumer group yet
+     */
+    UNJOINED,
+
+    /**
+     * Member has received a new assignment (partitions assigned or revoked), 
and it is applying
+     * it. While in this state, the member will invoke the user callbacks for
+     * onPartitionsAssigned or onPartitionsRevoked, and then make the new 
assignment effective.
+     */
+    // TODO: determine if separate state will be needed for assign/revoke (not 
for now)
+    PROCESSING_ASSIGNMENT,

Review Comment:
   I guess - this is where we send the first heartbeat? 



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MembershipManagerImpl.java:
##########
@@ -0,0 +1,168 @@
+/*
+ * 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 String groupInstanceId() {
+        // TODO: review empty vs null instance id
+        return groupInstanceId.orElse(null);

Review Comment:
   maybe return an optional is safer.



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MembershipManagerImpl.java:
##########
@@ -0,0 +1,168 @@
+/*
+ * 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) {

Review Comment:
   we can add the null check at the constructor level as we always need an 
assignor.



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MembershipManagerImpl.java:
##########
@@ -0,0 +1,168 @@
+/*
+ * 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 String groupInstanceId() {
+        // TODO: review empty vs null instance id
+        return groupInstanceId.orElse(null);
+    }
+
+    @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);
+            }
+        }
+    }
+
+    public void 
onAssignmentProcessSuccess(ConsumerGroupHeartbeatResponseData.Assignment 
assignment) {
+        currentAssignment = assignment;
+        targetAssignments.remove(assignment);
+        transitionTo(MemberState.STABLE);
+    }
+
+    public void onAssignmentProcessFailure(Throwable error) {
+        // TODO: handle failure scenario when the member was not able to 
process the assignment
+    }
+
+    private void resetMemberIdAndEpoch() {
+        this.memberId = "";

Review Comment:
   do we ever need to reset memberId?



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