gelatin 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8dcd7c5c1f3a4c208becadf439bfa5dd40e590ce
4
+ data.tar.gz: d3101d363020db7e05d35c4c3427851cc22e9eef
5
+ SHA512:
6
+ metadata.gz: 69a8096919e9035cd8133ecc60a15c8f777b0bf16342a1017bcd99e755826e05baa99d99d6a8a421b639b84e70c185e93edf339def7f93c312f2374eb54a5408
7
+ data.tar.gz: 521b8c6645b5b78bdc581cd11b0e1948f4d9e6cc08e566975cfff998411da24d937df6e12acec3067d0c75b0d7ba598966ee76fcda70e6ee73818d6f66fd5ee6
data/.editorconfig ADDED
@@ -0,0 +1,12 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
10
+
11
+ [*.{sh,markdown}]
12
+ indent_size = 4
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gelatin.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Eduardo Mourão
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # Gelatin: A Jump Consistent Hash Ruby Gem
2
+
3
+ [![Build Status](https://travis-ci.org/eduardordm/gelatin.svg?branch=master)](https://travis-ci.org/eduardordm/gelatin)
4
+
5
+ This gem exposes the Google's consistent hash algorithm to ruby. It uses the
6
+ original, mostly unmodified, implementation as described by
7
+ ["A Fast, Minimal Memory, Consistent Hash Algorithm"](http://arxiv.org/abs/1406.2294).
8
+
9
+ You can use this gem as a consistent hasher or to create a ring of servers like the ones used in
10
+ Amazon DynamoDB, Cassandra, etc. You could use it as some sort of novel cluster implementation also.
11
+ It will evenly distribute your clients among the servers and remap them when the number of nodes changes.
12
+
13
+ It containst no collision detection if you plan to use this as the main hasher for object storage, you might
14
+ need add aditional steps in your underlying storage implementation.
15
+
16
+ The performance is good: In a ring of 1.8mi servers and 2mi clients it returned the desired server within < 0.01 ms
17
+ on my 4 year old laptop. As long as Array#[] stays O(1) you should be fine.
18
+
19
+ "Google has not applied for patent protection for this algorithm, and, as of this writing, has no
20
+ plans to. Rather, it wishes to contribute this algorithm to the community."
21
+
22
+
23
+ ## Installation
24
+
25
+ Add this line to your application's Gemfile:
26
+
27
+ ```ruby
28
+ gem 'gelatin'
29
+ ```
30
+
31
+ And then execute:
32
+
33
+ $ bundle
34
+
35
+ Or install it yourself as:
36
+
37
+ $ gem install gelatin
38
+
39
+ ## Usage
40
+
41
+ If you need just a simple hasher:
42
+
43
+ ```ruby
44
+ require 'jch'
45
+
46
+ key = rand(2**32..2**64-1)
47
+
48
+ # The hash method takes two BIGNUMs.
49
+ # the first is the key to which you are trying to find a node to.
50
+ # The second is the quantity of available nodes.
51
+ my_node = hash(key, 1000000)
52
+ ```
53
+
54
+ If you are using this as a Server Ring (main purpose of this gem):
55
+
56
+ ```ruby
57
+ sh = Gelatin::Ring.new
58
+ (1...254).each do |i|
59
+ sh.add Gelatin::Node.new("192.168.0.#{i}")
60
+ end
61
+ puts sh.get("localhost".unpack('q*').first) # returns the server for this key
62
+ ```
63
+
64
+ You might use the same hasher to hash long strings like file paths, but you need to split the string into 64bit chunks.
65
+
66
+ ## Development
67
+
68
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
69
+
70
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/gelatin.
75
+
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
80
+
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ require 'rake/extensiontask'
4
+
5
+ Rake::ExtensionTask.new("jch") do |ext|
6
+ ext.ext_dir = "ext/jch"
7
+ end
8
+
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << "test"
11
+ t.libs << "lib"
12
+ t.test_files = FileList['test/**/*_test.rb', 'test/**/*_bench.rb']
13
+ end
14
+
15
+ task default: %w(compile test)
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gelatin"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ require "mkmf"
2
+
3
+ have_library('stdc++');
4
+
5
+ $CFLAGS << " -Wall"
6
+
7
+ create_makefile "jch/jch"
data/ext/jch/jch.c ADDED
@@ -0,0 +1,30 @@
1
+ #include <ruby.h>
2
+
3
+ const double JUMP = (double)(1LL << 31);
4
+
5
+ int32_t
6
+ JumpConsistentHashImpl(uint64_t key, int32_t num_buckets)
7
+ {
8
+ int64_t b = -1, j = 0;
9
+ while (j < num_buckets) {
10
+ b = j;
11
+ key = key * 2862933555777941757ULL + 1;
12
+ j = (b + 1) * (JUMP / (double)((key >> 33) + 1));
13
+ }
14
+ return b;
15
+ }
16
+
17
+ static
18
+ VALUE
19
+ JumpConsistentHash(VALUE t, VALUE v_key, VALUE v_num_buckets)
20
+ {
21
+ return INT2NUM(JumpConsistentHashImpl(NUM2ULL(v_key),
22
+ NUM2UINT(v_num_buckets)));
23
+ }
24
+
25
+ void
26
+ Init_jch(void)
27
+ {
28
+ VALUE m_jch = rb_define_module("Jch");
29
+ rb_define_method(m_jch, "hash", JumpConsistentHash, 2);
30
+ }
data/gelatin.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gelatin/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gelatin"
8
+ spec.version = Gelatin::VERSION
9
+ spec.authors = ["Eduardo Mourão"]
10
+ spec.email = ["eduardo.a20@gmail.com"]
11
+
12
+ spec.summary = %q{Gelatin: A Jump Consistent Hash Ruby Gem}
13
+ spec.description = %q{Gelatin: A Jump Consistent Hash Ruby Gem}
14
+ spec.homepage = "http://github.com/eduardordm/gelatin"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split("\n")
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+ spec.extensions = ["ext/jch/extconf.rb"]
22
+
23
+ spec.add_dependency "rake-compiler"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest"
28
+ spec.add_development_dependency "rubocop"
29
+ end
@@ -0,0 +1,7 @@
1
+ module Gelatin
2
+ module Hashable
3
+ def key
4
+ self.to_s.unpack('q*').first
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ module Gelatin
2
+ # Node is a PORO
3
+ class Node
4
+ attr_accessor :addr
5
+
6
+ def initialize(addr)
7
+ @addr = addr
8
+ end
9
+
10
+ def to_s
11
+ @addr
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,29 @@
1
+ require 'jch'
2
+
3
+ module Gelatin
4
+ # Defines the hasher class
5
+ # http://arxiv.org/pdf/1406.2294.pdf
6
+ class Ring
7
+ include Jch
8
+
9
+ def initialize
10
+ @nodes = []
11
+ end
12
+
13
+ def add(node)
14
+ @nodes << node
15
+ end
16
+
17
+ def delete(node)
18
+ @nodes.delete node
19
+ end
20
+
21
+ # Key should be some sort of information about the object.
22
+ # If it's a file, maybe you could use its relative path.
23
+ # If the object can fit in a uint64, you could use the object
24
+ # itself as the key.
25
+ def get(key)
26
+ @nodes[hash(key, @nodes.size)]
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Gelatin
2
+ VERSION = "0.1.0"
3
+ end
data/lib/gelatin.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "gelatin/version"
2
+ require "gelatin/hashable"
3
+ require "gelatin/node"
4
+ require "gelatin/ring"
5
+
6
+ module Gelatin
7
+ end
data/lib/jch.bundle ADDED
Binary file
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class GelatinTest < Minitest::Test
4
+ def test_that_it_has_a_version_number
5
+ refute_nil ::Gelatin::VERSION
6
+ end
7
+ end
data/test/jch_test.rb ADDED
@@ -0,0 +1,45 @@
1
+ require 'test_helper'
2
+ require 'jch'
3
+
4
+ class JchTest < Minitest::Test
5
+ include Jch
6
+
7
+ def test_hasher_consistency
8
+ key = rand(2**32..2**64-1)
9
+ nodes = rand(1000..100000)
10
+ node_a = hash(key, nodes)
11
+ node_b = hash(key, nodes)
12
+ assert_includes (0...nodes), node_a
13
+ assert_includes (0...nodes), node_b
14
+ assert_equal node_a, node_b
15
+ end
16
+
17
+ def test_rotation_on_removal
18
+ key = rand(2**32..2**64-1)
19
+ num_nodes = 10
20
+ last_node = 0
21
+ while(num_nodes > 1)
22
+ current_node = hash(key, num_nodes)
23
+ next_node = hash(key, num_nodes-=1)
24
+ if(current_node != next_node) # rotation should happen
25
+ assert next_node < current_node
26
+ last_node = next_node # we need to test if the last node is 0 at the end
27
+ end
28
+ end
29
+ assert_equal 0, last_node
30
+ end
31
+
32
+ def test_rotation_on_increase
33
+ key = rand(2**32..2**64-1)
34
+ num_nodes = 1
35
+ while(num_nodes < 10000)
36
+ current_node = hash(key, num_nodes)
37
+ next_node = hash(key, num_nodes+=1)
38
+ if(current_node != next_node) # better distribution should happen as space gets larger
39
+ assert next_node > current_node
40
+ last_node = next_node
41
+ end
42
+ end
43
+ refute_equal 0, last_node
44
+ end
45
+ end
data/test/ring.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'test_helper'
2
+
3
+ class RingTest < Minitest::Test
4
+ def test_that_it_finds_a_server
5
+ sh = Gelatin::Ring.new
6
+ (1...254).each do |i|
7
+ sh.add Gelatin::Node.new("192.168.0.#{i}")
8
+ end
9
+ refute_nil sh.get(Item.new.key)
10
+ assert valid_v4? sh.get(Item.new.key).addr
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ class BenchRing < Minitest::Benchmark
4
+ def bench_ring_google_numbers
5
+ ring = Gelatin::Ring.new
6
+
7
+ assert_performance_constant 0.1800000 do
8
+ ring.add(Gelatin::Node.new("192.168.0.1"))
9
+ end
10
+
11
+ assert_performance_constant 0.10000 do
12
+ (1..100000).each { ring.get(Item.new.key) }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'gelatin'
3
+
4
+ require 'minitest/autorun'
5
+ require "minitest/benchmark"
6
+
7
+ require 'securerandom'
8
+
9
+ class Item
10
+ include Gelatin::Hashable
11
+ def to_s
12
+ ::SecureRandom.base64
13
+ end
14
+ end
15
+
16
+ def valid_v4?(addr)
17
+ if /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/ =~ addr
18
+ return $~.captures.all? {|i| i.to_i < 256}
19
+ end
20
+ return false
21
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gelatin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eduardo Mourão
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake-compiler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: 'Gelatin: A Jump Consistent Hash Ruby Gem'
84
+ email:
85
+ - eduardo.a20@gmail.com
86
+ executables: []
87
+ extensions:
88
+ - ext/jch/extconf.rb
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".editorconfig"
92
+ - ".gitignore"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - ext/jch/extconf.rb
101
+ - ext/jch/jch.c
102
+ - gelatin.gemspec
103
+ - lib/gelatin.rb
104
+ - lib/gelatin/hashable.rb
105
+ - lib/gelatin/node.rb
106
+ - lib/gelatin/ring.rb
107
+ - lib/gelatin/version.rb
108
+ - lib/jch.bundle
109
+ - test/gelatin_test.rb
110
+ - test/jch_test.rb
111
+ - test/ring.rb
112
+ - test/ring_bench.rb
113
+ - test/test_helper.rb
114
+ homepage: http://github.com/eduardordm/gelatin
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.4.5.1
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: 'Gelatin: A Jump Consistent Hash Ruby Gem'
138
+ test_files: []