Skip to content

EncodingProviders must use abs(machine id) #5

@jonross

Description

@jonross

Negative machine IDs are possible, and SnowflakeEncodingProvider left-shifts these without checking, resulting in a small negative number that is OR'ed with the timestamp, erasing the timestamp. Example:

    static class FixedIdProvider implements MachineIdProvider {
        final long id;
        FixedIdProvider(long id) {
            this.id = id;
        }
        public long getMachineId() {
            return id;
        }
    }

    public static void main(String[] args) throws Exception
    {
        IdGenerator g1 = newSnowflakeIdGenerator(new FixedIdProvider(1));
        System.out.println(g1.generateId(0).asString());
        IdGenerator g2 = newSnowflakeIdGenerator(new FixedIdProvider(-1));
        System.out.println(g2.generateId(0).asString());
    }

Output:

080f6b18b1001000
fffffffffffff000

Creating a wrapper on e.g. MacPidMachineIdProvider is an easy temporary workaround.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions