Class HeartbeatRequestManager

java.lang.Object
org.apache.kafka.clients.consumer.internals.HeartbeatRequestManager
All Implemented Interfaces:
RequestManager

public class HeartbeatRequestManager extends Object implements RequestManager

Manages the request creation and response handling for the heartbeat. The module creates a ConsumerGroupHeartbeatRequest using the state stored in the 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 MembershipManager and handle any errors.

The manager will try to send a heartbeat when the member is in MemberState.STABLE, MemberState.JOINING, or MemberState.RECONCILING. Which mean the member is either in a stable group, is trying to join a group, or is in the process of reconciling the assignment changes.

If the member got kick out of a group, it will try to give up the current assignment by invoking OnPartitionsLost because reattempting to join again with a zero epoch.

If the member does not have groupId configured or encountering fatal exceptions, a heartbeat will not be sent.

If the coordinator not is not found, we will skip sending the heartbeat and try to find a coordinator first.

If the heartbeat failed due to retriable errors, such as, TimeoutException. The subsequent attempt will be backoff exponentially.

When the member completes the assignment reconciliation, the HeartbeatRequestManager.HeartbeatRequestState will be reset so that a heartbeat will be sent in the next event loop.

See HeartbeatRequestManager.HeartbeatRequestState for more details.

  • Constructor Details

  • Method Details

    • poll

      public NetworkClientDelegate.PollResult poll(long currentTimeMs)
      This will build a heartbeat request if one must be sent, determined based on the member state. A heartbeat is sent in the following situations:
      1. Member is part of the consumer group or wants to join it.
      2. The heartbeat interval has expired, or the member is in a state that indicates that it should heartbeat without waiting for the interval.
      This will also determine 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 there's no need to send a heartbeat.
      2. If the member cannot send a heartbeat due to either exponential backoff, it will return the remaining time left on the backoff timer.
      3. If the member's heartbeat timer has not expired, It will return the remaining time left on the heartbeat timer.
      4. If the member can send a heartbeat, the timer is set to the current heartbeat interval.
      Specified by:
      poll in interface RequestManager
      Parameters:
      currentTimeMs - The current system time at which the method was called; useful for determining if time-sensitive operations should be performed
      Returns:
      NetworkClientDelegate.PollResult that includes a heartbeat request if one must be sent, and the time to wait until the next poll.
    • membershipManager

      public MembershipManager membershipManager()
      Returns the MembershipManager that this request manager is using to track the state of the group. This is provided so that the ApplicationEventProcessor can access the state for querying or updating.
    • maximumTimeToWait

      public long maximumTimeToWait(long currentTimeMs)
      Returns the delay for which the application thread can safely wait before it should be responsive to results from the request managers. For example, the subscription state can change when heartbeats are sent, so blocking for longer than the heartbeat interval might mean the application thread is not responsive to changes.

      In the event that heartbeats are currently being skipped, this still returns the next heartbeat delay rather than Long.MAX_VALUE so that the application thread remains responsive.

      Specified by:
      maximumTimeToWait in interface RequestManager
      Parameters:
      currentTimeMs - The current system time at which the method was called; useful for determining if time-sensitive operations should be performed
      Returns:
      The maximum delay in milliseconds
    • resetPollTimer

      public void resetPollTimer(long pollMs)
      When consumer polls, we need to reset the pollTimer. If the poll timer has expired, we rejoin when the user repoll the consumer.