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


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManager.java:
##########
@@ -0,0 +1,324 @@
+/*
+ * 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.clients.CommonClientConfigs;
+import org.apache.kafka.clients.consumer.ConsumerConfig;
+import org.apache.kafka.common.KafkaException;
+import org.apache.kafka.common.errors.GroupAuthorizationException;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.ConsumerGroupHeartbeatRequest;
+import org.apache.kafka.common.requests.ConsumerGroupHeartbeatResponse;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.common.utils.Timer;
+import org.slf4j.Logger;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Manages the request creation and response handling for the heartbeat. The 
module creates a {@link ConsumerGroupHeartbeatRequest}
+ * using the state stored in the {@link MembershipManager} and enqueue it to 
the network queue to be sent out. Once
+ * the response is received, the module will update the state in the {@link 
MembershipManager} and handle any errors.
+ *
+ * The manager only emits heartbeat when the member is in a group, tries to 
join a group, or tries rejoin the group.
+ * If the member does not have groupId configured, got kicked out of the 
group, or encountering fatal exceptions, the
+ * heartbeat will not be sent.
+ *
+ * If the coordinator not is not found, we will skip sending the heartbeat and 
tries to find a coordinator first.
+ *
+ * If the heartbeat failed due to retriable errors, such as, TimeoutException. 
 The subsequent attempt will be backoff
+ * exponentially.
+ *
+ * If the member completes the assignment changes, i.e. revocation and 
assignment, a heartbeat request will be sent in
+ * the next event loop.
+ * {@link HeartbeatRequestState} for more details.
+ */
+public class HeartbeatRequestManager implements RequestManager {
+    private final Logger logger;
+    private final Set<Errors> fatalErrors = new HashSet<>(Arrays.asList(
+        Errors.GROUP_AUTHORIZATION_FAILED,
+        Errors.INVALID_REQUEST,
+        Errors.GROUP_MAX_SIZE_REACHED,
+        Errors.UNSUPPORTED_ASSIGNOR,
+        Errors.UNRELEASED_INSTANCE_ID));
+
+    private final int rebalanceTimeoutMs;
+
+    private final CoordinatorRequestManager coordinatorRequestManager;
+    private final SubscriptionState subscriptions;
+    private final HeartbeatRequestState heartbeatRequestState;
+    private final MembershipManager membershipManager;
+    private final ErrorEventHandler nonRetriableErrorHandler;
+
+    public HeartbeatRequestManager(
+        final Time time,
+        final LogContext logContext,
+        final ConsumerConfig config,
+        final CoordinatorRequestManager coordinatorRequestManager,
+        final SubscriptionState subscriptions,
+        final MembershipManager membershipManager,
+        final ErrorEventHandler nonRetriableErrorHandler) {
+        this.coordinatorRequestManager = coordinatorRequestManager;
+        this.logger = logContext.logger(getClass());
+        this.subscriptions = subscriptions;
+        this.membershipManager = membershipManager;
+        this.nonRetriableErrorHandler = nonRetriableErrorHandler;
+        this.rebalanceTimeoutMs = 
config.getInt(CommonClientConfigs.MAX_POLL_INTERVAL_MS_CONFIG);
+        long retryBackoffMs = 
config.getLong(ConsumerConfig.RETRY_BACKOFF_MS_CONFIG);
+        long retryBackoffMaxMs = 
config.getLong(ConsumerConfig.RETRY_BACKOFF_MAX_MS_CONFIG);
+        this.heartbeatRequestState = new HeartbeatRequestState(logContext, 
time, 0, retryBackoffMs,
+            retryBackoffMaxMs, rebalanceTimeoutMs);
+    }
+
+    // Visible for testing
+    HeartbeatRequestManager(
+        final LogContext logContext,
+        final ConsumerConfig config,
+        final CoordinatorRequestManager coordinatorRequestManager,
+        final SubscriptionState subscriptions,
+        final MembershipManager membershipManager,
+        final HeartbeatRequestState heartbeatRequestState,
+        final ErrorEventHandler nonRetriableErrorHandler) {
+        this.logger = logContext.logger(this.getClass());
+        this.subscriptions = subscriptions;
+        this.rebalanceTimeoutMs = 
config.getInt(CommonClientConfigs.MAX_POLL_INTERVAL_MS_CONFIG);
+        this.coordinatorRequestManager = coordinatorRequestManager;
+        this.heartbeatRequestState = heartbeatRequestState;
+        this.membershipManager = membershipManager;
+        this.nonRetriableErrorHandler = nonRetriableErrorHandler;
+    }
+
+    /**
+     * Determines the maximum wait time until the next poll based on the 
member's state.

Review Comment:
   True, but the poll is much more than just determining the wait time, so I 
would add to this something like " it builds the heartbeat request, including 
the logic for handling the responses"



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManager.java:
##########
@@ -0,0 +1,324 @@
+/*
+ * 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.clients.CommonClientConfigs;
+import org.apache.kafka.clients.consumer.ConsumerConfig;
+import org.apache.kafka.common.KafkaException;
+import org.apache.kafka.common.errors.GroupAuthorizationException;
+import org.apache.kafka.common.message.ConsumerGroupHeartbeatRequestData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.ConsumerGroupHeartbeatRequest;
+import org.apache.kafka.common.requests.ConsumerGroupHeartbeatResponse;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.common.utils.Timer;
+import org.slf4j.Logger;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Manages the request creation and response handling for the heartbeat. The 
module creates a {@link ConsumerGroupHeartbeatRequest}
+ * using the state stored in the {@link MembershipManager} and enqueue it to 
the network queue to be sent out. Once
+ * the response is received, the module will update the state in the {@link 
MembershipManager} and handle any errors.
+ *
+ * The manager only emits heartbeat when the member is in a group, tries to 
join a group, or tries rejoin the group.
+ * If the member does not have groupId configured, got kicked out of the 
group, or encountering fatal exceptions, the
+ * heartbeat will not be sent.
+ *
+ * If the coordinator not is not found, we will skip sending the heartbeat and 
tries to find a coordinator first.
+ *
+ * If the heartbeat failed due to retriable errors, such as, TimeoutException. 
 The subsequent attempt will be backoff
+ * exponentially.
+ *
+ * If the member completes the assignment changes, i.e. revocation and 
assignment, a heartbeat request will be sent in
+ * the next event loop.
+ * {@link HeartbeatRequestState} for more details.
+ */
+public class HeartbeatRequestManager implements RequestManager {
+    private final Logger logger;
+    private final Set<Errors> fatalErrors = new HashSet<>(Arrays.asList(
+        Errors.GROUP_AUTHORIZATION_FAILED,
+        Errors.INVALID_REQUEST,
+        Errors.GROUP_MAX_SIZE_REACHED,
+        Errors.UNSUPPORTED_ASSIGNOR,
+        Errors.UNRELEASED_INSTANCE_ID));
+
+    private final int rebalanceTimeoutMs;
+
+    private final CoordinatorRequestManager coordinatorRequestManager;
+    private final SubscriptionState subscriptions;
+    private final HeartbeatRequestState heartbeatRequestState;
+    private final MembershipManager membershipManager;
+    private final ErrorEventHandler nonRetriableErrorHandler;
+
+    public HeartbeatRequestManager(
+        final Time time,
+        final LogContext logContext,
+        final ConsumerConfig config,
+        final CoordinatorRequestManager coordinatorRequestManager,
+        final SubscriptionState subscriptions,
+        final MembershipManager membershipManager,
+        final ErrorEventHandler nonRetriableErrorHandler) {
+        this.coordinatorRequestManager = coordinatorRequestManager;
+        this.logger = logContext.logger(getClass());
+        this.subscriptions = subscriptions;
+        this.membershipManager = membershipManager;
+        this.nonRetriableErrorHandler = nonRetriableErrorHandler;
+        this.rebalanceTimeoutMs = 
config.getInt(CommonClientConfigs.MAX_POLL_INTERVAL_MS_CONFIG);
+        long retryBackoffMs = 
config.getLong(ConsumerConfig.RETRY_BACKOFF_MS_CONFIG);
+        long retryBackoffMaxMs = 
config.getLong(ConsumerConfig.RETRY_BACKOFF_MAX_MS_CONFIG);
+        this.heartbeatRequestState = new HeartbeatRequestState(logContext, 
time, 0, retryBackoffMs,
+            retryBackoffMaxMs, rebalanceTimeoutMs);
+    }
+
+    // Visible for testing
+    HeartbeatRequestManager(
+        final LogContext logContext,
+        final ConsumerConfig config,
+        final CoordinatorRequestManager coordinatorRequestManager,
+        final SubscriptionState subscriptions,
+        final MembershipManager membershipManager,
+        final HeartbeatRequestState heartbeatRequestState,
+        final ErrorEventHandler nonRetriableErrorHandler) {
+        this.logger = logContext.logger(this.getClass());
+        this.subscriptions = subscriptions;
+        this.rebalanceTimeoutMs = 
config.getInt(CommonClientConfigs.MAX_POLL_INTERVAL_MS_CONFIG);
+        this.coordinatorRequestManager = coordinatorRequestManager;
+        this.heartbeatRequestState = heartbeatRequestState;
+        this.membershipManager = membershipManager;
+        this.nonRetriableErrorHandler = nonRetriableErrorHandler;
+    }
+
+    /**
+     * Determines the maximum wait time until the next poll based on the 
member's state.
+     * 1. If the member is without a coordinator or is in a failed state, the 
timer is set to Long.MAX_VALUE, as

Review Comment:
   This format won't show as a proper list in the java doc, we should use 
`<ol>` tags (similar for the empty lines)



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

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

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

Reply via email to