Record Class BijectiveNodeIdMapping

java.lang.Object
java.lang.Record
io.kroxylicious.proxy.internal.routing.BijectiveNodeIdMapping
All Implemented Interfaces:
NodeIdMapping

public record BijectiveNodeIdMapping(Map<String,Integer> routeIds, int totalRoutes) extends Record implements NodeIdMapping
Bijective node ID mapping: V = id + S × t, where V is the virtual ID, t is the target node ID, S is the total number of routes, and id is the route's configured identifier.

Example with two routes (S=2) whose upstream clusters each have node IDs 0, 1, 2:

   route[0] (id=0): V = 0 + 2×0, 0 + 2×1, 0 + 2×2  →  0, 2, 4
   route[1] (id=1): V = 1 + 2×0, 1 + 2×1, 1 + 2×2  →  1, 3, 5
 
  • Constructor Details

    • BijectiveNodeIdMapping

      public BijectiveNodeIdMapping(Map<String,Integer> routeIds, int totalRoutes)
      Creates an instance of a BijectiveNodeIdMapping record class.
      Parameters:
      routeIds - the value for the routeIds record component
      totalRoutes - the value for the totalRoutes record component
  • Method Details

    • toVirtual

      public int toVirtual(String route, int targetNodeId)
      Description copied from interface: NodeIdMapping
      Translates a target-cluster node ID to a virtual node ID.

      Negative values (Kafka protocol sentinels such as -1 "no leader") are returned unchanged and must not be fed into the translation formula.

      Specified by:
      toVirtual in interface NodeIdMapping
      Parameters:
      route - the route name
      targetNodeId - the node ID on the target cluster
      Returns:
      the virtual node ID, or targetNodeId unchanged if negative
    • fromVirtual

      public int fromVirtual(String route, int virtualNodeId)
      Description copied from interface: NodeIdMapping
      Translates a virtual node ID back to its target-cluster node ID, given the route. The route is always supplied by the router via RouterContext.sendRequest(io.kroxylicious.proxy.topology.VirtualNode, org.apache.kafka.common.message.RequestHeaderData, org.apache.kafka.common.protocol.ApiMessage).

      As with NodeIdMapping.toVirtual(java.lang.String, int), negative values are returned unchanged.

      Specified by:
      fromVirtual in interface NodeIdMapping
      Parameters:
      route - the route name
      virtualNodeId - the virtual node ID
      Returns:
      the target-cluster node ID, or virtualNodeId unchanged if negative
    • fromVirtual

      public NodeIdMapping.RouteAndNode fromVirtual(int virtualNodeId)
      Description copied from interface: NodeIdMapping
      Translates a virtual node ID back to its route and target-cluster node ID. Only works for dedicated (one-to-one) mappings where the route can be recovered from the virtual node ID alone.

      Behaviour is undefined for negative inputs; proxy port node IDs are always non-negative and this method is never called with a sentinel value.

      Specified by:
      fromVirtual in interface NodeIdMapping
      Parameters:
      virtualNodeId - the virtual node ID
      Returns:
      the route name and target node ID
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • routeIds

      public Map<String,Integer> routeIds()
      Returns the value of the routeIds record component.
      Returns:
      the value of the routeIds record component
    • totalRoutes

      public int totalRoutes()
      Returns the value of the totalRoutes record component.
      Returns:
      the value of the totalRoutes record component