dajac commented on code in PR #14323: URL: https://github.com/apache/kafka/pull/14323#discussion_r1319705247
########## 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 { Review Comment: In the first version, we won't support client side assignors. Should we just not care about it for now? ########## 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: How about Reconciling? ########## 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: In general, we want to keep the member id but we have to clear it when receiving UNKNOWN_MEMBER_ID because as you said it is invalid. ########## 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, + + /** + * Member is active in a group (heartbeating) and has processed all assignments received. + */ + STABLE, + + /** + * The member failed with an unrecoverable error + */ + FAILED; Review Comment: I think that we should also have a Fenced state. Fenced is recoverable whereas Failed is not. ########## clients/src/main/java/org/apache/kafka/clients/consumer/internals/MembershipManager.java: ########## @@ -0,0 +1,73 @@ +/* + * 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 java.util.Optional; + +/** + * Manages group membership for a single member. + * Responsible for: + * <li>Keeping member state</li> + * <li>Keeping assignment for the member</li> + * <li>Computing assignment for the group if the member is required to do so<li/> + */ +public interface MembershipManager { + + String groupId(); + + Optional<String> groupInstanceId(); + + String memberId(); + + int memberEpoch(); + + MemberState state(); + + /** + * Update the current state of the member based on a heartbeat response + */ + void updateState(ConsumerGroupHeartbeatResponseData response); + + /** + * Returns the {@link AssignorSelection} for the member + */ + AssignorSelection assignorSelection(); + + /** + * Returns the current assignment for the member + */ + ConsumerGroupHeartbeatResponseData.Assignment assignment(); + + /** + * Sets the current assignment for the member + */ + void updateAssignment(ConsumerGroupHeartbeatResponseData.Assignment assignment); + + + /** + * Compute assignment for the group members using the provided group state and client side + * assignors defined. + * + * @param groupState Group state to be used to compute the assignment + * @return Group assignment computed using the selected client side assignor + */ + //TODO: fix parameters and return types to represent the group state object as defined in the + // ConsumerGroupPrepareAssignmentResponse + Object computeAssignment(Object groupState); Review Comment: Are we going to fix this in this PR? ########## 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; Review Comment: Why do we need a list here? The last one is always the authoritative one. All the others are obsolete. I suppose that the issue is that once a reconciliation between A and B has started, it is not possible to stop it if C comes. Therefore, we have to wait until it completes before we can start the next reconciliation cycle. Do I understand it right? ########## 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: Do we plan to handle all the other errors somewhere else? ########## 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); + } + } + } + + 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 + } Review Comment: This is interesting. Could you elaborate a bit more on it? Is about failures related to the callbacks? ########## 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; Review Comment: nit: This could be final as well, no? ########## 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); + } + } + } + + public void onAssignmentProcessSuccess(ConsumerGroupHeartbeatResponseData.Assignment assignment) { + currentAssignment = assignment; + targetAssignments.remove(assignment); + transitionTo(MemberState.STABLE); Review Comment: With the current logic, I think that we would transition to Stable only if there are no pending assignment to be processed. Or do we retransmission to PROCESSING_ASSIGNMENT somewhere else? -- 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