Interface StickyKeyConsumerSelector

All Known Implementing Classes:
ConsistentHashingStickyKeyConsumerSelector, HashRangeAutoSplitStickyKeyConsumerSelector, HashRangeExclusiveStickyKeyConsumerSelector

public interface StickyKeyConsumerSelector
Abstraction for selecting the same consumer based on a key. This interface provides methods to add and remove consumers, select a consumer based on a sticky key or hash, and retrieve the hash range assignments for consumers. This is used by the Key_Shared implementation.
  • Field Details

    • DEFAULT_RANGE_SIZE

      static final int DEFAULT_RANGE_SIZE
      The default range size used for hashing. This should be a power of 2 so that it's compatible with all implementations.
      See Also:
    • STICKY_KEY_HASH_NOT_SET

      static final int STICKY_KEY_HASH_NOT_SET
      The value used to indicate that sticky key hash is not set. This value cannot be -1 since some of the data structures require non-negative values.
      See Also:
  • Method Details

    • addConsumer

      Add a new consumer.
      Parameters:
      consumer - the new consumer to be added
      Returns:
      a CompletableFuture that completes with the result of impacted consumers. The result contains information about the existing consumers whose hash ranges were affected by the addition of the new consumer.
    • removeConsumer

      Optional<ImpactedConsumersResult> removeConsumer(Consumer consumer)
      Remove the consumer.
      Parameters:
      consumer - the consumer to be removed
      Returns:
      the result of impacted consumers. The result contains information about the existing consumers whose hash ranges were affected by the removal of the consumer.
    • select

      default Consumer select(byte[] stickyKey)
      Select a consumer by sticky key.
      Parameters:
      stickyKey - the sticky key to select the consumer
      Returns:
      the selected consumer
    • makeStickyKeyHash

      default int makeStickyKeyHash(byte[] stickyKey)
      Make a hash from the sticky key. The hash value is in the range returned by the getKeyHashRange() method instead of in the full range of integers. In other words, this returns the "slot".
      Parameters:
      stickyKey - the sticky key to hash
      Returns:
      the generated hash value
    • select

      Consumer select(int hash)
      Select a consumer by hash.
      Parameters:
      hash - the hash corresponding to the sticky key
      Returns:
      the selected consumer
    • getKeyHashRange

      org.apache.pulsar.client.api.Range getKeyHashRange()
      Get the full range of hash values used by this selector. The upper bound is exclusive.
      Returns:
      the full range of hash values
    • getConsumerKeyHashRanges

      default Map<Consumer,List<org.apache.pulsar.client.api.Range>> getConsumerKeyHashRanges()
      Get key hash ranges handled by each consumer.
      Returns:
      a map where the key is a consumer and the value is a list of hash ranges it is receiving messages for
    • getConsumerHashAssignmentsSnapshot

      ConsumerHashAssignmentsSnapshot getConsumerHashAssignmentsSnapshot()
      Get the current mappings of hash range to consumer.
      Returns:
      a snapshot of the consumer hash assignments