Package org.apache.pulsar.broker.service
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default range size used for hashing.static final intThe value used to indicate that sticky key hash is not set. -
Method Summary
Modifier and TypeMethodDescriptionaddConsumer(Consumer consumer) Add a new consumer.Get the current mappings of hash range to consumer.Get key hash ranges handled by each consumer.org.apache.pulsar.client.api.RangeGet the full range of hash values used by this selector.default intmakeStickyKeyHash(byte[] stickyKey) Make a hash from the sticky key.removeConsumer(Consumer consumer) Remove the consumer.default Consumerselect(byte[] stickyKey) Select a consumer by sticky key.select(int hash) Select a consumer by hash.
-
Field Details
-
DEFAULT_RANGE_SIZE
static final int DEFAULT_RANGE_SIZEThe 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_SETThe 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
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
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 thegetKeyHashRange()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
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
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
-