Coder Social home page Coder Social logo

peschkaj / rustflakes Goto Github PK

View Code? Open in Web Editor NEW
67.0 8.0 10.0 7.72 MB

An ordered ID generation service for .NET that is generator aware making it ideal for distributed ID generation. The implementation is heavily derivative of Boundary's flake.

License: Apache License 2.0

C# 100.00%

rustflakes's Introduction

rustflakes

An ordered ID generation service for .NET that is generator aware making it ideal for distributed ID generation. The implementation is heavily derivative of Boundary's flake.

Identifiers

Identifiers are generated as 128-bit numbers:

  • 64-bit timestamp as milliseconds since the dawn of time (January 1, 1970)
  • 48-bit worker identifier
  • 16-bit sequence number that is incremented when more than one identifier is requested in the same millisecond and reset to 0 when the clock moves forward

Roadmap

  • Bulk generation
  • Sample service implementation

Questions

How do I use this?

Take a look at the ConsoleFlakes implementation. Or, if you're lazy:

var o = new RustFlakes.Oxidation(new byte[] {0, 1, 2, 3, 4, 5});
var id = o.Oxidize();

Create a new RustFlakes.Oxidation object with some kind of generator identifier - this might be a machine identifier, MAC address, or random identifier that's generated from a separate service every time you call it.

What should I use as the worker identifier?

I've been known to pull the MAC address of the first active ethernet adapter. It doesn't matter what you're using so long as it's guaranteed to be unique per generator. You could pull the last 6 bytes of the CPU identifier if that suited you.

While machine identity should be relatively meaningless in a distributed system, that doesn't mean we can't use an arbitrary indicator to achieve distinction between functioning nodes in a given time range. If you're afraid of MAC address spoofing, then you should be able to work something out.

6 bytes gives you a lot of room for creativity. I suggest arbitrarily incrementing a number that you store in an S3 bucket. You could regenerate your worker identifier 281,474,976,710,656 times before you run out of unique values. Maybe this guy

When should I use this?

When you want time-based ordered IDs generated in many locations and sent to many locations. When you can't generate sequential identifiers yourself (Windows Azure SQL Database, I'm looking at you).

How is this different from XYZ?

It's probably not. I wrote this one afternoon in a hotel room because I was sick of thinking about T-SQL. A key differentiator between rustflakes and some other .NET based ID generators is that I made certain assumptions.

  1. Other people are smarter than me. I borrowed from their work.
  2. While machine identity isn't needed or desirable for some parts of a distributed system, they work great as arbitrary node identifiers. I don't make you choose a mechanism for your node identifier, but I don't foist my bad decisions on you either.
  3. There is no 3

Thanks

  • Dave Liebers for making sure the bit shifting works correctly in ShortOxidizer.
  • Mike Haboustak for providing unit tests and a thorough re-working of the oxidizers so they now work in 128, 96, and 64-bit forms.

rustflakes's People

Contributors

arobson avatar frenkan avatar mrchief avatar peschkaj 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  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  avatar

rustflakes's Issues

SqlServerBigIntOxidation

I'm trying to understand how this specific oxidation works. Ideally I'd like to reduce the number of negative IDs generated.

Couldn't the method look like:

public new long Oxidize()
{
    return unchecked((long)base.Oxidize());
}

Instead of subtracting long.MaxValue? I'm quite new to UUID generation.

RustFlakes assembly needs a strong name

If you're concerned about managing the private key, perhaps this might be worth considering. On the other hand, I certainly understand if you side with those folks that are against signing OSS binaries. Just curious as to what the eventual plan will be.

And of course, I can fork and sign it myself or go the ILDASM route.

128-bit C# decimal

The code for the 128-bit implementation uses System.Decimal to store the flake, but decimal only has 96 bits of internal storage. A real 128-bit number will overflow it.
http://msdn.microsoft.com/en-us/library/bb1c1a6x.aspx

Also, line 67, you can't simulate a 64-bit left shift by adding UInt64.MaxValue. You have to multiply by (UInt64.MaxValue+1).

An example, using UInt16:
(0xA << 0x10) = 0xA0000

(0xA * (0xFFFF+1)) = 0xA0000

(0xA + 0xFFFF) = 0x10009

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.