Giter VIP home page Giter VIP logo

global_uid's Introduction

GlobalUID is deprecated and will no longer be maintained.

Zendesk is working on a new unique ID solution that has made this library redundant, and will no longer be actively maintained. Internal staff are recommended to use this private transitional library.


Global UID Plugin

Build status

Summary

This gem allows you to generate global IDs as described in this article from Flickr for your Ruby on Rails applications. It does this by patching ActiveRecord::Base and ActiveRecord::Migration so that new models retrieve their ID from one of the configured alloc servers (short for ID allocation databases), this functionality is opt-out, not opt-in. The databases responsible for allocating your identifiers (aka id_servers) should have their auto_increment_increment and auto_increment_offset setting configured globally.

This gem only supports MySQL databases and the documentation is written with that in mind but the concept could be applied to others.

Resilience

The gem will iterate over the configured id_servers, choose one at random and attempt a connection, returning the ID from the first alloc server to successfully respond. If there's a connection failure, the server will be removed from the pool and retried after the configured connection_retry period. If there's an allocation failure, the server will be removed from the pool and retried after 60 seconds.

In either failure scenario, the configured notifier will be called and the gem will move through the list of configured id_servers until it gets a successful response. If no ID can be retrieved, a NoServersAvailableException is thrown and the connection state is reset so that subsequent requests will retry immediately.

Installation

Add it to your gemfile and run bundle install:

gem "global_uid"

Configuration

First configure some databases in database.yml in the normal way.

id_server_1:
  adapter: mysql2
  host: id_server_db1.prod
  port: 3306

id_server_2:
  adapter: mysql2
  host: id_server_db2.prod
  port: 3306

Then setup these servers, and other defaults in your environment.rb:

GlobalUid.configure do |config|
  config.id_servers = [ 'id_server_1', 'id_server_2' ]
  config.increment_by = 5
}

For a full list of configuration options, and their defaults, see GlobalUid::Configuration

Migration

Migrations will now add global_uid tables for you by default. They will also change your primary keys from signature "PRIMARY KEY AUTO_INCREMENT NOT NULL" to "PRIMARY KEY NOT NULL".

If you'd like to disable this behavior, you can by setting use_global_uid to false as show below:

class CreateFoos < ActiveRecord::Migration
  def self.up
    create_table :foos, use_global_uid: false do |t|

Model-level stuff

If you want GlobalUIDs created, you don't have to do anything except set up the GlobalUID tables with your migration. Everything will be taken care you. It's calm, and soothing like aloe. It's the Rails way.

Disabling global uid per table

class Foo < ActiveRecord::Base
  disable_global_uid
end

Taking matters into your own hands:

class Foo < ActiveRecord::Base
  disable_global_uid

  def before_create
    self.id = generate_uid()
    # other stuff
    ....
  end
end

If you're using a non standard uid table then pass that in.

generate_uid(uid_table: '<name>')

Testing

mysql is a required and can be installed with brew install [email protected]. If already installed, it's expected to be running with the defaults ([email protected]:3306). Set the MYSQL_URL environment variable if you're using something different.

This gem uses minitest and the test suite can be run with bundle exec rake test.

If you want to run a particular scenario, it can be done by passing the line number in, e.g. bundle exec ruby test/global_uid_test.rb -l 18

Copyright (c) 2010 Zendesk, released under the MIT license

global_uid's People

Contributors

adammw avatar bastien avatar benhass avatar bquorning avatar dasch avatar dragonfax avatar gabetax avatar ggrossman avatar grosser avatar morten avatar mriddle avatar osheroff avatar pschambacher avatar rhardih avatar staugaard avatar steved avatar

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.