Class ConsistentHashing

java.lang.Object
io.fluxcapacitor.common.ConsistentHashing

public class ConsistentHashing extends Object
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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static 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 as fallsInRange(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).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ConsistentHashing

      public ConsistentHashing()
  • Method Details

    • computeSegment

      public static int computeSegment(String routingKey)
      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

      public 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.
      Parameters:
      routingKey - the routing key to hash
      maxSegments - 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 hash
      hashFunction - a custom hash function
      maxSegments - the number of available segments
      Returns:
      the segment index
    • fallsInRange

      public static boolean fallsInRange(String routingKey, int[] segmentRange)
      Checks if the segment computed from the routing key falls within the specified segment range.
      Parameters:
      routingKey - the routing key
      segmentRange - an array of two integers [start, end)
      Returns:
      true if the segment is within the range
    • fallsInRange

      public static boolean fallsInRange(String routingKey, int[] segmentRange, int maxSegments)
      Same as fallsInRange(String, int[]), but uses a custom max segment size.
      Parameters:
      routingKey - the routing key
      segmentRange - 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 index
      segmentRange - 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