consistent-hash-js icon indicating copy to clipboard operation
consistent-hash-js copied to clipboard

Not really consistent

Open aillusions opened this issue 1 year ago • 4 comments

this implementation has bugs below is test case

test('Should getHash - be same always be same', async () => { for (let i = 0; i < 100; i++) { const ring = ['aaaa', 'bbbb', 'cccc']; const consistentHash = new ConsistentHash(ring); expect(consistentHash.get('123123123')).toEqual('cccc'); } });

aillusions avatar Sep 24 '23 17:09 aillusions

Hi, sorry for the delay. The constructor does not support an array argument. I think what you're trying to do accomplish is:

const assert = require('assert');
for (let i = 0; i < 100; i++) {
  const hash = new ConsistentHash({ distribution: 'uniform' });
  ['aaaa', 'bbbb', 'cccc'].forEach((node) => hash.add(node));
  assert.strictEqual(hash.get('123123123'), 'bbbb');
}

It might be convenient to have a nodes option though.

andrasq avatar Oct 08 '23 13:10 andrasq

I added a nodes option to constructor options; if that works for you I'll publish it as version 1.3.0. I also added the above sample code to the unit tests to verify that many uniform hash rings all return the same node to handle the same input.

Still need to specify distribution: 'uniform' though, the default is random (for historical reasons), and random distribution hash rings will return different nodes for the same input.

andrasq avatar Oct 08 '23 13:10 andrasq

Do you plan on publishing 1.2.0 (and any other future versions) to npm?

mattdeboard avatar Oct 25 '23 18:10 mattdeboard

thanks for the nudge, published 1.2.1

andrasq avatar Oct 26 '23 05:10 andrasq