Git Product home page Git Product logo

fauxflake's Introduction

Build Status Coverage Status Latest Version License

##What is this? Fauxflake is an easily embeddable, decentralized, k-ordered unique ID generator. It can use the same encoded ID format as Twitter's Snowflake or Boundary's Flake implementations as well as any other customized encoding without too much effort. The fauxflake-core module has no external dependencies and is meant to be about as light as possible while still delivering useful functionality. Essentially, if you want to be able to generate a unique identifier across your infrastructure with reasonable assurances about collisions, then you might find this useful.

##The Problem You've run into one or more of the following scenarios:

  • You need to generate a unique identifier, perhaps for an RDBMS primary key or for a NoSQL storage abstraction, and you need it done fast.
  • You need to be able to guarantee the loose orderings of unique keys when more than one machine is generating them.
  • A centralized key generation service seems too heavy or is otherwise unacceptable.
  • You've thought about clock drift, server time running backwards, or any of a number of horrible things you read here that make relying on simple system timestamps for anything absolutely horrifying.

##Where can I get it? You can find this module in the Maven Central repository by using your favorite build tool: ###Maven

<dependency>
  <groupId>com.github.rholder.fauxflake</groupId>
  <artifactId>fauxflake-core</artifactId>
  <version>1.1.0</version>
</dependency>

###Gradle

compile "com.github.rholder.fauxflake:fauxflake-core:1.1.0"

##Quickstart Let's say you want to generate a lexicographically sortable, Twitter compatible identifier, as a String. You could do that with the following snippet:

    IdGenerator snowflake = IdGenerators.newSnowflakeIdGenerator();
    String id = snowflake.generateId(1000).asString();

The Snowflake and Flake IdGenerator instances are both thread-safe and intended to be used as singletons.

##Customization The MachineIdProvider is meant to be customized when the "good enough" guarantees coming from the MacMachineIdProvider and the MacPidMachineIdProvideraren't really good enough for your use case and you need more strictness in how those are being set. For instance, by just swapping in your own unique machine identifier from a system property, you can get an actual universal guarantee among all of your machines if each are given a unique number on startup:

public class CustomMachineIdProvider implements MachineIdProvider {
    @Override
    public long getMachineId() {
        return Long.valueOf(System.getProperty("my.app.machine.id"));
    }
}

You might want to note that a Snowflake ID only has 1024 possible unique machine identifiers since the specification calls for reserving 10 bits for this information.

##Additional Documentation Javadoc can be found here.

##Building from source The Fauxflake project uses a Gradle-based build system. In the instructions below, ./gradlew is invoked from the root of the source tree and serves as a cross-platform, self-contained bootstrap mechanism for the build. The only prerequisites are Git and JDK 1.6+.

check out sources

git clone git://github.com/rholder/fauxflake.git

compile and test, build all jars

./gradlew build

install all jars into your local Maven cache

./gradlew install

##License The Fauxflake module is released under version 2.0 of the Apache License.

##References

fauxflake's People

Contributors

rholder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

fauxflake's Issues

docker container causes same id to be generated every time

For some reason whenever I run an application using fauxflake inside a docker,
it always generates the same id (this is a single long running instance of a linux container),
even if the container stays up and has net=host set.

private static IdGenerator snowflake = IdGenerators.newSnowflakeIdGenerator();
snowflake.generateId(1000).asString();

always yields ffffffffffc01000

works perfectly outside the container though.

EncodingProviders must use abs(machine id)

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.

duplicate ids being generated

I was a bit surprised to see that this code in a unit test produces two duplicates:

        IdGenerator snowflake = IdGenerators.newSnowflakeIdGenerator();
        for (int i = 0; i < 10; i++) {
            String id = snowflake.generateId(1000).asString();
            System.out.println(id);
        }

output:

ffffffffffed8000
ffffffffffed8000
ffffffffffed8001
ffffffffffed8002
ffffffffffed8003
ffffffffffed8004
ffffffffffed8005
ffffffffffed8006
ffffffffffed8000
ffffffffffed8001

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.