10.4.3. Hash Code Calculation Rules for Clients


Specific rules must be followed by clients when calculating the hash code for a server.
When virtual nodes are disabled:

When clients receive the base hash code of a server, it must be normalized to locate the exact position on the hash wheel. This normalization process includes:

  • Passing the base hash code to the hash function.
  • Calculations to avoid negative values.
The result is a number that indicates the node's position in the hash wheel.
The following code displays how the normalization process is carried out:
public static int getNormalizedHash(int nodeBaseHashCode, Hash hashFct) {
   return hashFct.hash(nodeBaseHashCode) & Integer.MAX_VALUE; 
}
Copy to Clipboard Toggle word wrap
When virtual nodes are enabled:

Each node represents N different virtual modes. Therefore, to calculate the hash code for each virtual node, use the numbers between 0 and N-1 and apply the following process:

Procedure 10.1. Hash Code Calculation with Virtual Nodes

  1. For the virtual node with the ID "0", use the following code to obtain the hash code:
    public static int getNormalizedHash(int nodeBaseHashCode, Hash hashFct) {
       return hashFct.hash(nodeBaseHashCode) & Integer.MAX_VALUE; 
    }
    
    Copy to Clipboard Toggle word wrap
  2. For all subsequent virtual nodes (with IDs from "1" to "N-1"), execute the following code:
    public static int virtualNodeHashCode(int nodeBaseHashCode, int id, Hash hashFct) {
       int virtualNodeBaseHashCode = id;
       virtualNodeBaseHashCode = 31 * virtualNodeBaseHashCode + nodeBaseHashCode;
       return getNormalizedHash(virtualNodeBaseHashCode, hashFct);
    }
    
    Copy to Clipboard Toggle word wrap
Back to top
Red Hat logoGithubredditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Theme

© 2025 Red Hat