-
Notifications
You must be signed in to change notification settings - Fork 0
Add Jump Consistent Hash implementation with tests #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import ConsistentHash from "../"; | ||
|
|
||
| const ch = new ConsistentHash({ virtualNodes: 100 }); | ||
| ch.addNode("server1"); | ||
| ch.addNode("server2"); | ||
|
|
||
| const node = ch.getNode("my-key"); | ||
| console.log(`Key is assigned to node: ${node}`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| const JumpConsistentHash = require("../jump-hash.js").default; | ||
|
jkyberneees marked this conversation as resolved.
|
||
|
|
||
| const jch = new JumpConsistentHash(16); | ||
| const idx = jch.getIndex("user-123"); | ||
|
|
||
| console.log(`Index for 'user-123': ${idx}`); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import JumpConsistentHash from "../jump-hash.js"; | ||
|
|
||
| const jch = new JumpConsistentHash(16); | ||
| const idx = jch.getIndex("user-123"); | ||
|
|
||
| console.log(`Index for 'user-123': ${idx}`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export { default as ConsistentHash } from "./consistent-hash.js"; | ||
| export { default as JumpConsistentHash } from "./jump-hash.js"; | ||
|
|
||
| // Expose the default export to match runtime default export (ConsistentHash) | ||
| import ConsistentHashDefault from "./consistent-hash.js"; | ||
| export default ConsistentHashDefault; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /** | ||
| * JumpConsistentHash maps string keys to an integer index in [0, N) | ||
| * using the Jump Consistent Hash algorithm by Lamping & Veach. | ||
| */ | ||
| declare class JumpConsistentHash { | ||
| /** | ||
| * Create a new JumpConsistentHash instance. | ||
| * | ||
| * @param indexes - Total number of indexes (buckets), must be a positive integer. | ||
| */ | ||
| constructor(indexes: number); | ||
|
|
||
| /** | ||
| * Update number of indexes (buckets). | ||
| * | ||
| * @param indexes - Positive integer number of buckets. | ||
| */ | ||
| setIndexes(indexes: number): void; | ||
|
|
||
| /** | ||
| * Get current number of indexes (buckets). | ||
| */ | ||
| size(): number; | ||
|
|
||
| /** | ||
| * Compute stable index in [0, indexes) for the given key. | ||
| * | ||
| * @param key - Non-empty string key. | ||
| * @returns Index between 0 (inclusive) and indexes (exclusive). | ||
| */ | ||
| getIndex(key: string): number; | ||
| } | ||
|
|
||
| export default JumpConsistentHash; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import crypto from "crypto"; | ||
|
|
||
| /** | ||
| * JumpConsistentHash maps string keys to an integer index in [0, N) | ||
| * using the Jump Consistent Hash algorithm by Lamping & Veach. | ||
| * | ||
| * Usage: | ||
| * const jch = new JumpConsistentHash(16) | ||
| * const idx = jch.getIndex('some-key') // 0..15 | ||
| */ | ||
| class JumpConsistentHash { | ||
| /** | ||
| * @param {number} indexes - Total number of indexes (buckets), must be >= 1 integer | ||
| */ | ||
| constructor(indexes) { | ||
| this.setIndexes(indexes); | ||
| } | ||
|
|
||
| /** | ||
| * Update number of indexes (buckets). | ||
| * @param {number} indexes | ||
| */ | ||
| setIndexes(indexes) { | ||
| if (!Number.isInteger(indexes) || indexes <= 0) { | ||
| throw new Error("Indexes must be a positive integer"); | ||
| } | ||
| this.indexes = indexes; | ||
| } | ||
|
|
||
| /** | ||
| * @returns {number} Current number of indexes (buckets) | ||
| */ | ||
| size() { | ||
| return this.indexes; | ||
| } | ||
|
|
||
| /** | ||
| * Compute stable index in [0, indexes) for the given key. | ||
| * @param {string} key | ||
| * @returns {number} | ||
| */ | ||
| getIndex(key) { | ||
| if (!key || typeof key !== "string") { | ||
| throw new Error("Key must be a non-empty string"); | ||
| } | ||
| const k = this.#hash64(key); | ||
| return this.#jumpHash(k, this.indexes); | ||
| } | ||
|
|
||
| /** | ||
| * Hash string to unsigned 64-bit BigInt using SHA-1 (first 8 bytes). | ||
| * Endianness choice is arbitrary but consistent (little-endian). | ||
| * @param {string} key | ||
| * @returns {bigint} | ||
| */ | ||
| #hash64(key) { | ||
| const digest = crypto.createHash("sha1").update(key).digest(); | ||
|
jkyberneees marked this conversation as resolved.
|
||
| let x = 0n; | ||
| const len = Math.min(8, digest.length); | ||
| for (let i = 0; i < len; i++) { | ||
| x |= BigInt(digest[i]) << BigInt(8 * i); | ||
| } | ||
| return x; | ||
| } | ||
|
|
||
| /** | ||
| * Jump Consistent Hash (Lamping & Veach) implemented with 64-bit arithmetic. | ||
| * @param {bigint} key - 64-bit unsigned key | ||
| * @param {number} buckets - number of buckets (indexes) | ||
| * @returns {number} | ||
| */ | ||
| #jumpHash(key, buckets) { | ||
| // Constants per paper | ||
| const mul = 2862933555777941757n; | ||
| let b = -1; | ||
| let j = 0; | ||
| while (j < buckets) { | ||
| b = j; | ||
| key = (key * mul + 1n) & ((1n << 64n) - 1n); // mod 2^64 | ||
| // (key >> 33) fits in 31 bits => safe to convert to Number | ||
| const r = Number(key >> 33n) + 1; | ||
| j = Math.floor((b + 1) * (2147483648 / r)); | ||
| } | ||
| return b; | ||
| } | ||
| } | ||
|
|
||
| export default JumpConsistentHash; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { describe, test, expect } from "bun:test"; | ||
| import JumpConsistentHash from "./jump-hash.js"; | ||
|
|
||
| describe("JumpConsistentHash", () => { | ||
| describe("constructor & size()", () => { | ||
| test("initializes with provided number of indexes", () => { | ||
| const jch = new JumpConsistentHash(16); | ||
| expect(jch.size()).toBe(16); | ||
| }); | ||
|
|
||
| test("throws for invalid indexes in constructor", () => { | ||
| expect(() => new JumpConsistentHash()).toThrow("Indexes must be a positive integer"); | ||
| expect(() => new JumpConsistentHash(0)).toThrow("Indexes must be a positive integer"); | ||
| expect(() => new JumpConsistentHash(-1)).toThrow("Indexes must be a positive integer"); | ||
| expect(() => new JumpConsistentHash(1.2)).toThrow("Indexes must be a positive integer"); | ||
| }); | ||
|
|
||
| test("setIndexes() updates and validates", () => { | ||
| const jch = new JumpConsistentHash(4); | ||
| expect(jch.size()).toBe(4); | ||
| jch.setIndexes(32); | ||
| expect(jch.size()).toBe(32); | ||
| expect(() => jch.setIndexes(0)).toThrow("Indexes must be a positive integer"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getIndex()", () => { | ||
| test("throws for empty key", () => { | ||
| const jch = new JumpConsistentHash(8); | ||
| expect(() => jch.getIndex("")).toThrow("Key must be a non-empty string"); | ||
| }); | ||
|
|
||
| test("returns index within range", () => { | ||
| const jch = new JumpConsistentHash(8); | ||
| const idx = jch.getIndex("alpha"); | ||
| expect(idx).toBeGreaterThanOrEqual(0); | ||
| expect(idx).toBeLessThan(8); | ||
| }); | ||
|
|
||
| test("deterministic for same key", () => { | ||
| const jch = new JumpConsistentHash(8); | ||
| const k = "user-12345"; | ||
| const a = jch.getIndex(k); | ||
| const b = jch.getIndex(k); | ||
| expect(a).toBe(b); | ||
| }); | ||
|
|
||
| test("one bucket always maps to 0", () => { | ||
| const jch = new JumpConsistentHash(1); | ||
| const keys = ["a", "b", "c", "d", "e"]; | ||
| for (const k of keys) { | ||
| expect(jch.getIndex(k)).toBe(0); | ||
| } | ||
| }); | ||
|
|
||
| test("changes reflect with updated bucket count", () => { | ||
| const jch = new JumpConsistentHash(4); | ||
| const k = "remap-key"; | ||
| const before = jch.getIndex(k); | ||
| expect(before).toBeGreaterThanOrEqual(0); | ||
| expect(before).toBeLessThan(4); | ||
| jch.setIndexes(9); | ||
| const after = jch.getIndex(k); | ||
| expect(after).toBeGreaterThanOrEqual(0); | ||
| expect(after).toBeLessThan(9); | ||
| }); | ||
|
|
||
| test("distributes keys relatively evenly", () => { | ||
| const buckets = 8; | ||
| const jch = new JumpConsistentHash(buckets); | ||
| const totalKeys = 10000; | ||
| const counts = new Array(buckets).fill(0); | ||
| for (let i = 0; i < totalKeys; i++) { | ||
| const idx = jch.getIndex(`key-${i}`); | ||
| counts[idx]++; | ||
| } | ||
| const avg = totalKeys / buckets; | ||
| const tolerance = avg * 0.25; // 25% tolerance | ||
| for (const c of counts) { | ||
| expect(Math.abs(c - avg)).toBeLessThanOrEqual(tolerance); | ||
| } | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.