Class Fetcher<K,V>

java.lang.Object
org.apache.kafka.clients.consumer.internals.AbstractFetch
org.apache.kafka.clients.consumer.internals.Fetcher<K,V>
All Implemented Interfaces:
Closeable, AutoCloseable

public class Fetcher<K,V> extends AbstractFetch
This class manages the fetching process with the brokers.

Thread-safety: Requests and responses of Fetcher may be processed by different threads since heartbeat thread may process responses. Other operations are single-threaded and invoked only from the thread polling the consumer.

  • If a response handler accesses any shared state of the Fetcher (e.g. FetchSessionHandler), all access to that state must be synchronized on the Fetcher instance.
  • If a response handler accesses any shared state of the coordinator (e.g. SubscriptionState), it is assumed that all access to that state is synchronized on the coordinator instance by the caller.
  • At most one request is pending for each node at any time. Nodes with pending requests are tracked and updated after processing the response. This ensures that any state (e.g. epoch) updated while processing responses on one thread are visible while creating the subsequent request on a different thread.
  • Constructor Details

  • Method Details

    • isUnavailable

      protected boolean isUnavailable(org.apache.kafka.common.Node node)
      Description copied from class: AbstractFetch
      Check if the node is disconnected and unavailable for immediate reconnection (i.e. if it is in reconnect backoff window following the disconnect).
      Specified by:
      isUnavailable in class AbstractFetch
      Parameters:
      node - Node to check for availability
      See Also:
      • NetworkClientUtils.isUnavailable(KafkaClient, Node, Time)
    • maybeThrowAuthFailure

      protected void maybeThrowAuthFailure(org.apache.kafka.common.Node node)
      Description copied from class: AbstractFetch
      Checks for an authentication error on a given node and throws the exception if it exists.
      Specified by:
      maybeThrowAuthFailure in class AbstractFetch
      Parameters:
      node - Node to check for a previous AuthenticationException; if found it is thrown
      See Also:
      • NetworkClientUtils.maybeThrowAuthFailure(KafkaClient, Node)
    • clearBufferedDataForUnassignedPartitions

      public void clearBufferedDataForUnassignedPartitions(Collection<org.apache.kafka.common.TopicPartition> assignedPartitions)
    • sendFetches

      public int sendFetches()
      Set up a fetch request for any node that we have assigned partitions for which doesn't already have an in-flight fetch or pending fetch data.
      Returns:
      number of fetches sent
    • maybeCloseFetchSessions

      protected void maybeCloseFetchSessions(org.apache.kafka.common.utils.Timer timer)
    • collectFetch

      public Fetch<K,V> collectFetch()
    • closeInternal

      protected void closeInternal(org.apache.kafka.common.utils.Timer timer)
      This method is called by AbstractFetch.close(Timer) which is guarded by the IdempotentCloser) such as to only be executed once the first time that any of the AbstractFetch.close() methods are called. Subclasses can override this method without the need for extra synchronization at the instance level.

      Note: this method is synchronized to reinstitute the 3.5 behavior:

      Shared states (e.g. sessionHandlers) could be accessed by multiple threads (such as heartbeat thread), hence, it is necessary to acquire a lock on the fetcher instance before modifying the states.
      Overrides:
      closeInternal in class AbstractFetch
      Parameters:
      timer - Timer to enforce time limit