tangledbytes / nodejs-snowflake

Generate time sortable 64 bits unique ids for distributed systems (inspired from twitter snowflake)
https://www.npmjs.com/package/nodejs-snowflake
Apache License 2.0
176 stars 15 forks source link

Duplicated value #19

Closed teyou closed 1 year ago

teyou commented 1 year ago

To replicate:


const uuids = new Map();
for (let index = 0; index < 100000; index++) {
    const cid = new Snowflake({ custom_epoch: 1262304000000 });
    const uuid = cid.getUniqueID();
    if (uuids.has(uuid)) {
        const prevIndex = uuids.get(uuid)
        console.error(`duplicate uuid: ${uuid.toString()} with index: ${prevIndex}`)
        break;
    }
    uuids.set(uuid, index);
    console.log(`${index} --> ${uuid.toString()}`)
}

Sample output

0 --> 1728003927441882112
1 --> 1728003927475132416
2 --> 1728003927474664448
3 --> 1728003927474048000
4 --> 1728003927476345856
5 --> 1728003927475515392
6 --> 1728003927473547264
7 --> 1728003927475712000
8 --> 1728003927473036288
9 --> 1728003927474329600
10 --> 1728003927473372160
11 --> 1728003927476232192
12 --> 1728003927474273280
13 --> 1728003927474632704
14 --> 1728003927474380800
duplicate uuid: 1728003927474664448 with index: 2

@utkarsh-pro any idea where's wrong?

tangledbytes commented 1 year ago

Hi, thanks for reporting the issue. This is not quite the way the library is supposed to be consumed. new Snowflake returns a stateful object which ensures to never return duplicate values but if these objects are being generated in each iteration then the invariant won't hold true.

Can you try the followiing:

const uuids = new Map();
+ const cid = new Snowflake({ custom_epoch: 1262304000000 });
for (let index = 0; index < 100000; index++) {
-   const cid = new Snowflake({ custom_epoch: 1262304000000 });
    const uuid = cid.getUniqueID();
    if (uuids.has(uuid)) {
        const prevIndex = uuids.get(uuid)
        console.error(`duplicate uuid: ${uuid.toString()} with index: ${prevIndex}`)
        break;
    }
    uuids.set(uuid, index);
    console.log(`${index} --> ${uuid.toString()}`)
}
tangledbytes commented 1 year ago

There is a similar test that I run in the CI, you can check out that as well: https://github.com/utkarsh-pro/nodejs-snowflake/blob/666d94b8ca6f5fe1820c08bce42d1768c6fcc5d3/tests/snowflake_core.rs#L144

teyou commented 1 year ago

@utkarsh-pro ah, got it!

Thanks for the prompt reply!