Package io.fluxcapacitor.common
Class ConsistentHashing
java.lang.Object
io.fluxcapacitor.common.ConsistentHashing
Utility class for computing segment assignments using consistent hashing.
This class maps routing keys to numeric segments using a hash function, typically to support partitioned processing or distribution across consumers in Flux.
Segments are defined as integer ranges, and a typical range is from 0 (inclusive) to MAX_SEGMENT
(exclusive),
where MAX_SEGMENT
is defined in Position
.
Key Features
- Computes deterministic segment for a given routing key
- Supports customizable hash functions
- Checks if a segment falls within a range
- Detects whether a segment range is empty
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic int
computeSegment
(String routingKey) Computes the segment for a given routing key using the default hash function and the default maximum segment size.static int
computeSegment
(String routingKey, int maxSegments) Computes the segment for a given routing key and maximum number of segments, using the default hash function.static int
computeSegment
(String routingKey, Function<String, Integer> hashFunction, int maxSegments) Computes the segment using a custom hash function and max segment size.static boolean
fallsInRange
(int segment, int[] segmentRange) Checks if a given segment index falls within a specified segment range.static boolean
fallsInRange
(String routingKey, int[] segmentRange) Checks if the segment computed from the routing key falls within the specified segment range.static boolean
fallsInRange
(String routingKey, int[] segmentRange, int maxSegments) Same asfallsInRange(String, int[])
, but uses a custom max segment size.static boolean
isEmptyRange
(int[] segmentRange) Checks whether the segment range is empty (i.e., start == end).
-
Constructor Details
-
ConsistentHashing
public ConsistentHashing()
-
-
Method Details
-
computeSegment
Computes the segment for a given routing key using the default hash function and the default maximum segment size.- Parameters:
routingKey
- the routing key to hash- Returns:
- the segment index
-
computeSegment
Computes the segment for a given routing key and maximum number of segments, using the default hash function.- Parameters:
routingKey
- the routing key to hashmaxSegments
- the number of available segments- Returns:
- the segment index
-
computeSegment
public static int computeSegment(String routingKey, Function<String, Integer> hashFunction, int maxSegments) Computes the segment using a custom hash function and max segment size.- Parameters:
routingKey
- the routing key to hashhashFunction
- a custom hash functionmaxSegments
- the number of available segments- Returns:
- the segment index
-
fallsInRange
Checks if the segment computed from the routing key falls within the specified segment range.- Parameters:
routingKey
- the routing keysegmentRange
- an array of two integers [start, end)- Returns:
true
if the segment is within the range
-
fallsInRange
Same asfallsInRange(String, int[])
, but uses a custom max segment size.- Parameters:
routingKey
- the routing keysegmentRange
- segment range [start, end)maxSegments
- number of available segments- Returns:
true
if the computed segment falls in the range
-
fallsInRange
public static boolean fallsInRange(int segment, int[] segmentRange) Checks if a given segment index falls within a specified segment range.- Parameters:
segment
- the segment indexsegmentRange
- segment range [start, end)- Returns:
true
if the segment is within the range
-
isEmptyRange
public static boolean isEmptyRange(int[] segmentRange) Checks whether the segment range is empty (i.e., start == end).- Parameters:
segmentRange
- an array of two integers [start, end)- Returns:
true
if the range is empty
-