You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "lianetm (via GitHub)" <gi...@apache.org> on 2023/09/01 20:51:36 UTC
[GitHub] [kafka] lianetm opened a new pull request, #14323: [WIP] - KAFKA-15275 - Client state machine basic components, states and initial transitions
lianetm opened a new pull request, #14323:
URL: https://github.com/apache/kafka/pull/14323
Core components for maintaining group membership based on heartbeats, following the new consumer group protocol.
These includes the initial components, states and transitions, with a shape suitable for extending it as we implement the protocol on the client covering all possible member states and transitions.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1322000996
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MembershipManagerImpl.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * 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 final 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, null, null);
+ }
+
+ public MembershipManagerImpl(String groupId, String groupInstanceId, AssignorSelection assignorSelection) {
+ this.groupId = groupId;
+ this.state = MemberState.UNJOINED;
+ if (assignorSelection == null) {
+ setAssignorSelection(AssignorSelection.defaultAssignor());
+ } else {
+ setAssignorSelection(assignorSelection);
+ }
+ this.targetAssignments = new ArrayList<>();
+ this.groupInstanceId = Optional.ofNullable(groupInstanceId);
+ }
+
+ /**
+ * 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 (!this.state.equals(nextState) && !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) {
Review Comment:
Uhm actually seems that the heartbeat code you have will end up being simpler if we keep this like we have it now, with a single call to a self contained `updateState` that knows how to update the state based on a HB response (successful or failed). It would only require a single call to `memberState.updateState(response);` and that if/else above would be simpler. WDYT?
Totally ok with renaming to include onHeartbeatResponse as you suggested.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1320013571
##########
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:
Good catch, we do need to consider if there is already another target assignment to reconcile, and only transition to STABLE if there is not.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321577970
##########
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:
In the current consumer, exceptions in the execution of the user-provided callbacks are thrown back to the user from the poll loop as `KafkaException`, breaking the poll loop as they are considered unrecoverable.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1319972998
##########
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:
Exactly, this will be for updating the state when exceptions occur in the user provided callbacks. I expect that this will be called from the assign/revoke partitions logic, and that it will transition the state to an unrecoverable FAILED.
Added the transition to FAILED, as I think we know already that's needed, to ensure same behaviour as the legacy consumer, that fails the poll loop if callbacks fail [here](https://github.com/apache/kafka/blob/84c49c6a098986f7da9f08dc53e6a31c13484c5b/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ConsumerCoordinator.java#L484)). I left the TODO with detaila to make sure we properly update the rest of the membership info in this scenario.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321737185
##########
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:
the server side change you have in mind seems the easiest to reason about of course, but agree the it's a bit less incremental
--
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] lianetm commented on pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on PR #14323:
URL: https://github.com/apache/kafka/pull/14323#issuecomment-1717575694
Thanks @dajac, I will open a follow-up PR addressing the latest suggestions.
--
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] lianetm commented on pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on PR #14323:
URL: https://github.com/apache/kafka/pull/14323#issuecomment-1710622597
Build completed with 6 unrelated test failures:
Build / JDK 20 and Scala 2.13 / testBumpTransactionalEpoch(String).quorum=kraft – kafka.api.TransactionsTest
Build / JDK 8 and Scala 2.12 / testNonDefaultConnectionCountLimitAndRateLimit() – kafka.network.ConnectionQuotasTest
Build / JDK 11 and Scala 2.13 / testDelegationTokenRequests(String).quorum=kraft – kafka.server.DelegationTokenRequestsTest
Build / JDK 11 and Scala 2.13 / executeTieredStorageTest(String).quorum=kraft – org.apache.kafka.tiered.storage.integration.ReassignReplicaExpandTest
Build / JDK 17 and Scala 2.13 / testRackAwareRangeAssignor() – integration.kafka.server.FetchFromFollowerIntegrationTest
Build / JDK 17 and Scala 2.13 / testBumpTransactionalEpoch(String).quorum=kraft – kafka.api.TransactionsTest
--
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 #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321293721
##########
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:
I would prefer having three attributes personally because it is clearer.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321621168
##########
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:
Very interesting, we've had some conversations about this (without clear ideas yet) so I need to think about this more. In principle I don't see a reason why we couldn't process the 2 revocations above in parallel, given they relate to different partitions. It feels like the right thing to do, to take advantage of the incremental assignment when possible. That being said, it would definitely be a change in how we execute the callbacks. User's callbacks could be executed concurrently for distinct sets of partitions, which is something that does not happen today (they are invoked sequentially in the poll loop)
I will update this initial code to have the clearer var definitions for now: `currentAssignment`, `targetAssignment` and `nextTargetAssignment`. I will continue to think about how to handle overlapping assignments, which seems to be where we could consider the trade-offs of starting processing a `nextTargetAssignment` even if there is a `targetAssignment` being processed.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321783061
##########
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:
Exactly. The user would get the `KafkaExpcetion` (hopefully do something about it), and call poll again
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1322000996
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MembershipManagerImpl.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * 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 final 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, null, null);
+ }
+
+ public MembershipManagerImpl(String groupId, String groupInstanceId, AssignorSelection assignorSelection) {
+ this.groupId = groupId;
+ this.state = MemberState.UNJOINED;
+ if (assignorSelection == null) {
+ setAssignorSelection(AssignorSelection.defaultAssignor());
+ } else {
+ setAssignorSelection(assignorSelection);
+ }
+ this.targetAssignments = new ArrayList<>();
+ this.groupInstanceId = Optional.ofNullable(groupInstanceId);
+ }
+
+ /**
+ * 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 (!this.state.equals(nextState) && !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) {
Review Comment:
Uhm actually seems that the heartbeat code you have will end up being simpler if we keep it like we have it now, with a single call to a self contained `updateState` that knows how to update the state based on a HB response. It would only require a single call to `memberState.updateState(response);` and that if/else above would be simpler. WDYT?
Totally ok with renaming to include onHeartbeatResponse as you suggested.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1318655049
##########
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 do have the validation at the constructor level (reusing this), so we make sure there's always an assignor.
For the record, I added this function to update the assignor, with the null check, to enable the case where a member migrates from one type of assignor to another.
--
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 #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321292303
##########
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:
Yeah, I am +1 for not caring about client side assignor 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] pranavrth commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "pranavrth (via GitHub)" <gi...@apache.org>.
pranavrth commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321724746
##########
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:
There is another case - while A and B revocation is in progress, these partitions are reassigned to the same consumer. In this case `MemberEpoch` will be increased on the coordinator side and OffsetCommit will fail for the ongoing revocations. There maybe alot of commits in progress and client will have ordering issues with retrying those commits.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321986491
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MemberState.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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 {
Review Comment:
This is a enum so we'll be getting the value already with the default toString implementation. Do you still prefer to add a custom one including something other than the name?
--
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] philipnee commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "philipnee (via GitHub)" <gi...@apache.org>.
philipnee commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1320021633
##########
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:
would be good to clarify that on the KIP! @dajac
--
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 #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321714750
##########
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:
> User's callbacks could be executed concurrently for distinct sets of partitions, which is something that does not happen today (they are invoked sequentially in the poll loop)
This does not seem doable to me because it would break the contract of the `poll` method, isn't it?
The alternative that I am considering would be to not let the server compute a new assignment until the previous one is ack'ed. I need to think this though a little more. The advantage is that it simplifies the client side (we don't need nextTargetAssignment). The disadvantage is that it is a bit less incremental.
--
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 #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac merged PR #14323:
URL: https://github.com/apache/kafka/pull/14323
--
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] lianetm commented on pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on PR #14323:
URL: https://github.com/apache/kafka/pull/14323#issuecomment-1714511273
Thanks for the comments @dajac, all addressed.
There is still a lot to think about regarding how the client will handle the assignments it receives. In order to unblock the integrations that require this state components, I'm leaving here the initial approach for handling assignments, to continue working on it in a follow-up PR if that's OK. So for now this follows the initial idea that the member may continue to receive assignments while still processing one (so uses current, target and nextTarget with the latest). I updated the transitions and added tests for it, but I expect it will change as we continue to work on it (If we ever move towards the idea of the server not sending new assignments until acked this would be simplified, it not it would probably stay similar to how it is now, requiring more thought on the client side to properly handle all the tricky scenarios we've discussed).
--
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] philipnee commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "philipnee (via GitHub)" <gi...@apache.org>.
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1318635008
##########
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:
Regarding the type, I added it explicitly thinking of cases where we might be interested in checking the type without actually retrieving the assignors (ex. validations). For better readability, seems better to be able to do `if (selection.type() == AssignorSelection.Type.CLIENT)`
Regarding the constructors, having static factory methods here instead of constructors allows to have meaningful names, so clients using the AssignorSelection can explicitly indicate what they want to create (`newClientAssignors` or `newServerAssignor`). We only pass null internally to the private constructor so seemed like a fair price to pay internally in the class, in order to provide a very explicit way to create new instances.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1319927882
##########
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:
Sure, I just intentionally left it like this to double check here: I guess we want to have the `ConsumerGroupPrepareAssignment` API related classes in the same org.apache.kafka.common.requests package where the heartbeat ones are, correct? (not seeing them created already made me doubt). If that's the right place I'll add them and use them 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] philipnee commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "philipnee (via GitHub)" <gi...@apache.org>.
philipnee commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1318822595
##########
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:
why couldn't we do `hasClientAssignor()` ?
Since we use Optional quite liberally - can we make the assignor Optional as well?
--
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 #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "dajac (via GitHub)" <gi...@apache.org>.
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1319944582
##########
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:
Yes, your understanding is right. We need to keep a single target assignment, but since we'll continue to heartbeat while we reconcile, we also need to keep newer assignments that we might receive while reconciling the 1st one (need to keep only the last one though, as you pointed out).
Maybe it'll make more clear if I keep something like a single `targetAssignment` (potentially in the process of being reconciled), and a single optional `nextTargetAssignment`. In the end, I don't expect that this list will ever have more than 2 items.
--
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 #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321316433
##########
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:
I think that we need to think a little more about how we will handle this. My concern is that the server side may start timers based on the `nextTargetAssignment` but the client won't look into it before it finishes the reconciliation to the `targetAssignment`.
For instance, imagine the following scenario.
* Current assignment is A and B;
* Server asks to revoke A and starts the rebalance timer (5 minutes by default);
* Client starts the revocation of A. This can take a while;
* Server asks to revoke B and restarts the rebalance timer;
* Client still waits on the revocation of A to complete. When completed, it starts the revocation of B.
In this scenario, if the revocation of A and B takes more than 5 minutes altogether, the member will be kicked out from the consumer group. The server does not know that the consumer is blocked on the revocation of A.
Do we really need to wait until A is fully revoked before we can start the process to revoke B?
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321733445
##########
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:
Agree that the actual process on the client should be sync to maintain the current contract. It would be only when receiving a new assignment and having already a nextTarget, that we could get smarted on the client side (Ex. nextTargetAssignment being a revocation of partition A, and receiving a revocation of partition B, could we maybe bundle them together on the client, before they are processed)
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1322000996
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MembershipManagerImpl.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * 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 final 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, null, null);
+ }
+
+ public MembershipManagerImpl(String groupId, String groupInstanceId, AssignorSelection assignorSelection) {
+ this.groupId = groupId;
+ this.state = MemberState.UNJOINED;
+ if (assignorSelection == null) {
+ setAssignorSelection(AssignorSelection.defaultAssignor());
+ } else {
+ setAssignorSelection(assignorSelection);
+ }
+ this.targetAssignments = new ArrayList<>();
+ this.groupInstanceId = Optional.ofNullable(groupInstanceId);
+ }
+
+ /**
+ * 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 (!this.state.equals(nextState) && !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) {
Review Comment:
Uhm actually seems that the heartbeat code you have will end up being simpler if we keep it like we have it now, with a single call to a self contained `updateState` that knows how to update the state based on a HB response (successful or failed). It would only require a single call to `memberState.updateState(response);` and that if/else above would be simpler. WDYT?
Totally ok with renaming to include onHeartbeatResponse as you suggested.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321737185
##########
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:
the server side change you have in mind seems the cleanest to reason about of course, but agree the it's a bit less incremental
--
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 #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1324405161
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MembershipManager.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * 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();
Review Comment:
nit: I would be great o add javadoc to all methods for consistency.
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MembershipManagerImpl.java:
##########
@@ -0,0 +1,240 @@
+/*
+ * 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.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 final 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;
+ /**
Review Comment:
nit: We usually put an empty line before the javadoc. At least, we do so on the server side.
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MembershipManagerImpl.java:
##########
@@ -0,0 +1,240 @@
+/*
+ * 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.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;
Review Comment:
ditto about the javadoc.
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MembershipManagerImpl.java:
##########
@@ -0,0 +1,240 @@
+/*
+ * 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.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 final 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;
+ /**
+ * Assignment that the member received from the server but hasn't completely processed
+ * yet.
+ */
+ private Optional<ConsumerGroupHeartbeatResponseData.Assignment> targetAssignment;
+ /**
+ * Latest assignment that the member received from the server while a {@link #targetAssignment}
+ * was in process.
+ */
+ private Optional<ConsumerGroupHeartbeatResponseData.Assignment> nextTargetAssignment;
+
+ public MembershipManagerImpl(String groupId) {
+ this(groupId, null, null);
+ }
+
+ public MembershipManagerImpl(String groupId, String groupInstanceId, AssignorSelection assignorSelection) {
+ this.groupId = groupId;
+ this.state = MemberState.UNJOINED;
+ if (assignorSelection == null) {
+ setAssignorSelection(AssignorSelection.defaultAssignor());
+ } else {
+ setAssignorSelection(assignorSelection);
+ }
+ this.groupInstanceId = Optional.ofNullable(groupInstanceId);
+ this.targetAssignment = Optional.empty();
+ this.nextTargetAssignment = Optional.empty();
+ }
+
+ /**
+ * 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 (!this.state.equals(nextState) && !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();
+ ConsumerGroupHeartbeatResponseData.Assignment assignment = response.assignment();
+ if (assignment != null) {
+ setTargetAssignment(assignment);
+ }
+ maybeTransitionToStable();
+ } else {
+ if (response.errorCode() == Errors.FENCED_MEMBER_EPOCH.code() || response.errorCode() == Errors.UNKNOWN_MEMBER_ID.code()) {
+ resetEpoch();
+ transitionTo(MemberState.FENCED);
+ } else if (response.errorCode() == Errors.UNRELEASED_INSTANCE_ID.code()) {
+ transitionTo(MemberState.FAILED);
+ }
+ // TODO: handle other errors here to update state accordingly, mainly making the
+ // distinction between the recoverable errors and the fatal ones, that should FAILED
+ // the member
+ }
+ }
+
+ /**
+ * Transition to {@link MemberState#STABLE} only if there are no target assignments left to
+ * reconcile. Transition to {@link MemberState#RECONCILING} otherwise.
+ */
+ private boolean maybeTransitionToStable() {
+ if (!hasPendingTargetAssignment()) {
+ transitionTo(MemberState.STABLE);
+ } else {
+ transitionTo(MemberState.RECONCILING);
+ }
+ return state.equals(MemberState.STABLE);
+ }
+
+ private void setTargetAssignment(ConsumerGroupHeartbeatResponseData.Assignment newTargetAssignment) {
+ if (!targetAssignment.isPresent()) {
+ targetAssignment = Optional.of(newTargetAssignment);
+ } else {
+ // Keep the latest next target assignment
+ nextTargetAssignment = Optional.of(newTargetAssignment);
+ }
+ }
+
+ private boolean hasPendingTargetAssignment() {
+ return targetAssignment.isPresent() || nextTargetAssignment.isPresent();
+ }
+
Review Comment:
nit: Extra empty line.
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AssignorSelection.java:
##########
@@ -0,0 +1,84 @@
+/*
+ * 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.Objects;
+
+/**
+ * Selection of a type of assignor used by a member to get partitions assigned as part of a
+ * consumer group. Currently supported assignors are:
+ * <li>SERVER assignors</li>
+ * <p/>
+ * Server assignors include a name of the server assignor selected, ex. uniform, range.
+ */
+public class AssignorSelection {
+ public enum Type { SERVER }
+
+ private final AssignorSelection.Type type;
+ private String serverAssignor;
+
+ private AssignorSelection(Type type, String serverAssignor) {
+ this.type = type;
+ if (type == Type.SERVER) {
+ this.serverAssignor = serverAssignor;
+ } else {
+ throw new IllegalArgumentException("Unsupported assignor type " + type);
+ }
+ }
+
+ public static AssignorSelection newServerAssignor(String serverAssignor) {
+ if (serverAssignor == null) {
+ throw new IllegalArgumentException("Selected server assignor name cannot be null");
+ }
+ if (serverAssignor.isEmpty()) {
+ throw new IllegalArgumentException("Selected server assignor name cannot be empty");
+ }
+ return new AssignorSelection(Type.SERVER, serverAssignor);
+ }
+
+ public static AssignorSelection defaultAssignor() {
+ // TODO: review default selection
+ return new AssignorSelection(Type.SERVER, "uniform");
Review Comment:
By default, we should not set any assignor and let the server use the preferred one.
--
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 #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321295756
##########
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:
Out of curiosity, do you know how we handle this in the current consumer?
--
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 #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321717875
##########
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:
The server sends an UNKNOWN_MEMBER_ID error when the received member id does not exist and the received member epoch is greater than 0. This would happen if the member is fenced or manually removed from the consumer group.
--
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] philipnee commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "philipnee (via GitHub)" <gi...@apache.org>.
philipnee commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1322009365
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MembershipManagerImpl.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * 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 final 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, null, null);
+ }
+
+ public MembershipManagerImpl(String groupId, String groupInstanceId, AssignorSelection assignorSelection) {
+ this.groupId = groupId;
+ this.state = MemberState.UNJOINED;
+ if (assignorSelection == null) {
+ setAssignorSelection(AssignorSelection.defaultAssignor());
+ } else {
+ setAssignorSelection(assignorSelection);
+ }
+ this.targetAssignments = new ArrayList<>();
+ this.groupInstanceId = Optional.ofNullable(groupInstanceId);
+ }
+
+ /**
+ * 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 (!this.state.equals(nextState) && !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) {
Review Comment:
the heartbeat code will also need to handle the error code, for example upon receiving NOT_COORDINATOR error, it will need to invalidate the current coordinator node, so it will need to decode the response. It is fine for heartbeat to pass the response to the response, but i'm just nit picking about doing the same thing twice.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1318645607
##########
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:
This is when the member receives a new assignment in the hearbeat response. It stays in thit state as long as it is processing the assignment (onPartitionsAssigned and onPartitionsRevoked callbacks execution + make the assignment received the `currentAssignment`).
This is the interesting state we've been talking a lot about, mainly because we will continue to receive heartbeat responses in this state, and we need to be able to process them correctly while processing the assignment.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1320184381
##########
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:
Seeing this now in sync with the conversation about leaving out client-side assignors for now, maybe better if I remove this unused `computeAssignment` here, and bring it back when we introduce the client-side assignors logic?
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321621168
##########
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:
Very interesting, we've had some conversations about this (without clear ideas yet) so I need to think about this more. In principle I don't see a reason why we couldn't process the 2 revocations above in parallel, given they relate to different partitions. It feels like the right thing to do, to take advantage of the incremental assignment when possible. That being said, it would definitely be a change in how we execute the callbacks. User's callbacks could be executed concurrently for distinct sets of partitions, which is something that does not happen today (they are invoked sequentially in the poll loop)
I will update this initial code to have the clearer var definitions for now: `currentAssignment`, `targetAssignment` and `nextTargetAssignment`. I will continue to think about how to handle assignments/revocations received while processing a previous one, which seems to be where we could consider the trade-offs of starting processing a `nextTargetAssignment` even if there is a `targetAssignment` being processed (when possible)
--
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] philipnee commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "philipnee (via GitHub)" <gi...@apache.org>.
philipnee commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1319012270
##########
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:
i think the kip says we need to rejoin with the same id : `Upon receiving the UNKNOWN_MEMBER_ID or FENCED_MEMBER_EPOCH error, the consumer abandon all its partitions and rejoins with the same member id and the epoch 0.`
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1319075024
##########
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:
Yes, I know it's not written like that but we had conversations saying that sending epoch 0 would be enough (that's why I want to double check)
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1319876418
##########
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:
Agree, it's in the end representing the state where the member is reconciling the current assignment with a target assignment received, and aligns better with the terms used in the KIP.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1319972998
##########
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:
Exactly, this will be for updating the state when exceptions occur in the user provided callbacks. I expect that this will be called from the assign/revoke partitions logic, and that it will transition the state to an unrecoverable FAILED.
Added the transition to FAILED, as I think we know already that's needed (to ensure same behaviour as the legacy consumer, that fails the poll loop if callbacks fail [here](https://github.com/apache/kafka/blob/84c49c6a098986f7da9f08dc53e6a31c13484c5b/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ConsumerCoordinator.java#L484)). I left the TODO with detaila to make sure we properly update the rest of the membership info in this scenario.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321733445
##########
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:
Agree that the actual process on the client should be sync to maintain the current contract. If we stick to the idea that we might continue to receive assignments while a previous one is not acked yet, keeping a nextTargetAssignment (latest one received), could be helpful maybe just to keep it handy (since we had to ignore it). If we move to the server sending assignments only when previous one is acked, then that nextTarget could be left out.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321522306
##########
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:
+1 on keeping it even in the UNKNOWN_MEMBER_ID scenario. Having the ability to differentiate restarts from brand new consumers seems to open the door for smarter behaviour.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1320085005
##########
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:
Sounds good, will leave the selection but supporting SERVER only 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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1319972998
##########
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:
Exactly, this will be for updating the state when exceptions occur in the user provided callbacks. I expect that this will be called from the revokePartitions logic, and that it will transition the state to an unrecoverable FAILED.
Added the transition to FAILED, as I think we know already that's needed, to ensure same behaviour as the legacy consumer, that fails the poll loop if callbacks fail [here](https://github.com/apache/kafka/blob/84c49c6a098986f7da9f08dc53e6a31c13484c5b/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ConsumerCoordinator.java#L484)). I left the TODO with detaila to make sure we properly update the rest of the membership info in this scenario.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1319005335
##########
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:
This is used in the case when a member receives UNKNOWN_MEMBER_ID or FENCED_MEMBER_EPOCH from the leader. I'm clearing the memberId mainly for consistency, to avoid keeping it in-memory when we know is wrong.
Just to double check with @dajac, in the case of these 2 errors, the member should rejoin (providing a member epoch 0), but I'm assuming there is no need to pass the previous member ID.
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1322027402
##########
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:
Removed the client assignor info from the comment too, just to make sure it reflects the reality of what the class currently supports
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321783061
##########
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:
Exactly. The user would get the `KafkaExpcetion` (hopefully do something about it), and call poll again, going through the whole join process
--
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 #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321290984
##########
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:
Actually, we could also keep the member id upon receiving UNKNOWN_MEMBER_ID. The coordinator will use it to recreate the new member. I think that it depends on whether we really want the ability to detect restarts of the consumer or not. If we really want to, keeping the member id would be better. What do you think?
--
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 #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321719789
##########
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:
I see. I suppose that if the user calls `poll` again after this, the consumer re-tried to join the group, 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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1318665973
##########
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:
Updated (and TODO removed)
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1319075024
##########
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:
Yes, I know it's not written like that but we had conversations saying that sending epoch 0 could be enough (that's why I want to double check)
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321537135
##########
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:
Agree, I removed what was related to client side assignors and computing assignment.
@kirktrue no worries with what you're working on. I do expect that you bring in the logic for invoking callbacks and computing assignment. I had only added here an unimplemented `computeAssignment` to propose delegating that responsibility to the `membershipManager` once you have access to this code. But we can discuss details when we get to the client assignors part. Removed the compute for now since we're not supporting it.
--
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] philipnee commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "philipnee (via GitHub)" <gi...@apache.org>.
philipnee commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321687226
##########
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:
it is not entirely clear to me when would a client receive the UNKNOWN_MEMBER_ID error - Obviously, it could be caused by client side bug of sending the wrong/invalid memberId. On the coordinator side, once a member is assigned to an id - I assume this shouldn't happen? Maybe when a group is being recreated or something?
--
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] philipnee commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "philipnee (via GitHub)" <gi...@apache.org>.
philipnee commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321926651
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MembershipManagerImpl.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * 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 final 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, null, null);
+ }
+
+ public MembershipManagerImpl(String groupId, String groupInstanceId, AssignorSelection assignorSelection) {
+ this.groupId = groupId;
+ this.state = MemberState.UNJOINED;
+ if (assignorSelection == null) {
+ setAssignorSelection(AssignorSelection.defaultAssignor());
+ } else {
+ setAssignorSelection(assignorSelection);
+ }
+ this.targetAssignments = new ArrayList<>();
+ this.groupInstanceId = Optional.ofNullable(groupInstanceId);
+ }
+
+ /**
+ * 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 (!this.state.equals(nextState) && !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) {
Review Comment:
WDYT about a more descriptive naming: like onHeartbeatResponse(). FWIW: in the heartbeat request manager, I also need to parse the error and response in order to update the local request state. (For exponential backoff). So would it make sense to break it into two functions like: onSuccessfulHeartbeatResponse and onErrorHeartbeatResponse. Let me know what do you think.
--
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] philipnee commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "philipnee (via GitHub)" <gi...@apache.org>.
philipnee commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1321933274
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MembershipManagerImpl.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * 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 final 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, null, null);
+ }
+
+ public MembershipManagerImpl(String groupId, String groupInstanceId, AssignorSelection assignorSelection) {
+ this.groupId = groupId;
+ this.state = MemberState.UNJOINED;
+ if (assignorSelection == null) {
+ setAssignorSelection(AssignorSelection.defaultAssignor());
+ } else {
+ setAssignorSelection(assignorSelection);
+ }
+ this.targetAssignments = new ArrayList<>();
+ this.groupInstanceId = Optional.ofNullable(groupInstanceId);
+ }
+
+ /**
+ * 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 (!this.state.equals(nextState) && !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) {
Review Comment:
in the request manager we would probably do:
{
if (response.errorCode() == NONE) {
.. update some local state
memberstate.onResponse(response)
} else {
memberstate.onError(response.errorcode);
}
--
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] lianetm commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1319899063
##########
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:
Totally, added the state and its valid transitions. I also updated the doc for both error states to reflect the differences.
--
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] lianetm commented on pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "lianetm (via GitHub)" <gi...@apache.org>.
lianetm commented on PR #14323:
URL: https://github.com/apache/kafka/pull/14323#issuecomment-1712000067
Thanks for the feedback @dajac, all comments addressed.
--
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] kirktrue commented on a diff in pull request #14323: KAFKA-15275 - Client state machine basic components, states and initial transitions
Posted by "kirktrue (via GitHub)" <gi...@apache.org>.
kirktrue commented on code in PR #14323:
URL: https://github.com/apache/kafka/pull/14323#discussion_r1320398056
##########
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:
Not sure we want to remove the class-level comments about client-side assignors. I don't have a strong preference either way.
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MemberState.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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 {
Review Comment:
toString() for debug logging, please? 🥺
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AssignorSelection.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.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:
`toString()` please?
##########
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:
Oof! I'm also working on the code for computing the assignment and invoking callbacks.
See #14357 and KAFKA-15276.
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/MemberState.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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 {
Review Comment:
toString() for debug logging, please? 🥺
--
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