2017 © Pedro Peláez
 

library cruftflake

This package provides a PHP implementation of Twitter Snoflake ID Generator algoritm

image

snaper/cruftflake

This package provides a PHP implementation of Twitter Snoflake ID Generator algoritm

  • Tuesday, September 26, 2017
  • by snaper
  • Repository
  • 1 Watchers
  • 0 Stars
  • 60 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 25 Forks
  • 0 Open issues
  • 1 Versions
  • 3 % Grown

The README.md

CruftFlake

Build Status ProjectStatus, (*1)

A stab at a version of Twitter Snowflake but in PHP with a simple ZeroMQ interface (rather than Thrift)., (*2)

Implementation

This project was motivated by personal curiosity and also my inability to get Twitter's project to build., (*3)

The implementation copies Twitter - generating 64 bit IDs., (*4)

  • time - 41 bits
  • configured machine ID - 10 bits
  • sequence number - 12 bits

Has a custom epoch that means it can generate IDs until 2081-09-06 (not the same epoch as Snowflake)., (*5)

ZooKeeper for config coordination (optional)

Each machine must have a unique 10 bit machine ID. ZooKeeper can be used to keep a list of all registered machine IDs, allowing new machines to self-assign a new unique machine ID., (*6)

When using ZooKeeper, when a new node starts up for the first time it must be able to contact the ZooKeeper cluster and create a new node. It will look at all the existing nodes and then (if it can't find its own Mac Address) attempt to claim a free one., (*7)

I was using Ephemeral nodes for this - similar(ish) to a lock pattern but this had the issue that the node needed to remain connected to ZK throughout its lifetime -- this way it doesn't., (*8)

The downside is that potentially the 1024 possible machine IDs will "fill up" and need to be manually pruned., (*9)

Running

Git clone and then execute composer update to install the dependencies. You should run the tests to verify things are OK:, (*10)

vendor/bin/phpunit

There are two scripts provided for playing about with., (*11)

  1. The generator (the server), (*12)

    ./examples/cruftflake.php, (*13)

Or to specify a manually configured machine ID:, (*14)

./examples/cruftflake.php -m 1
  1. A client that will generate N IDs and dump to STDOUT, (*15)

    ./examples/client.php -n 100, (*16)

The Versions

26/09 2017

dev-master

9999999-dev

This package provides a PHP implementation of Twitter Snoflake ID Generator algoritm

  Sources   Download

The Requires

  • php >=5.3.3

 

The Development Requires

by Dave Gardner

uuid snowflake zookeeper