consistent_hashr 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Superfeedr
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,21 @@
1
+ = ConsistentHashr
2
+
3
+ A small module to do easy consistent hashing.
4
+
5
+ == Usage
6
+
7
+ hashr = ConsistentHashr.new()
8
+
9
+ hashr.add_server("s1", "server1")
10
+ hashr.add_server("s2", "server2")
11
+ hashr.add_server("s3", "server3")
12
+ hashr.add_server("s4", "server4")
13
+ hashr.add_server("s5", "server5")
14
+
15
+ hashr.get(key) # Should return the same key consistently when new servers are added.
16
+
17
+ == Implementation
18
+
19
+ Based on "Mike Perham's":http://www.mikeperham.com/ "code":http://www.mikeperham.com/2009/01/14/consistent-hashing-in-memcache-client/ and tests. I thought this layer didn't belong to any specific client, this is why I extracted it.
20
+
21
+ Greatly inspired by "Tom White's consistent hashing explaination":http://weblogs.java.net/blog/tomwhite/archive/2007/11/consistent_hash.html.
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "consistent_hashr"
8
+ gem.summary = %Q{A ruby gem to do consistent hashing}
9
+ gem.description = %Q{A simple gem to perform consistent hashing}
10
+ gem.email = "julien.genestoux@gmail.com"
11
+ gem.homepage = "http://github.com/superfeedr/consistent_hashr"
12
+ gem.authors = ["julien"]
13
+ gem.add_development_dependency "rspec", ">= 0"
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ task :default => :test
21
+
22
+ require 'rake/rdoctask'
23
+ Rake::RDocTask.new do |rdoc|
24
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
25
+
26
+ rdoc.rdoc_dir = 'rdoc'
27
+ rdoc.title = "consistent_hashr #{version}"
28
+ rdoc.rdoc_files.include('README*')
29
+ rdoc.rdoc_files.include('lib/**/*.rb')
30
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,47 @@
1
+ require 'zlib'
2
+
3
+ class ConsistentHashr
4
+
5
+ ##
6
+ # Creates a new Hasher. Servers should be a hash.
7
+ def initialize(_servers = {}, _number_of_replicas = 50)
8
+ @circle = {}
9
+ @number_of_replicas = _number_of_replicas
10
+ _servers.each do |name, server|
11
+ add_server(name, server)
12
+ end
13
+ end
14
+
15
+ ##
16
+ # Computes a key
17
+ def hash_key(key)
18
+ Zlib.crc32("#{key}")
19
+ end
20
+
21
+ ##
22
+ # Adds a server to the circle
23
+ def add_server(_name, _server)
24
+ @number_of_replicas.times do |t|
25
+ @circle[hash_key("#{_name}+#{t}")] = _server
26
+ end
27
+ end
28
+
29
+ ##
30
+ # Returns the server for the provided key
31
+ def get(key)
32
+ return nil if @circle.empty?
33
+ return @circle.first.last if @circle.size == 1
34
+
35
+ hash = hash_key(key)
36
+
37
+ # If the key is there, let's return it.
38
+ return @circle[hash] if @circle[hash]
39
+
40
+ # If not, we need to find the next closest from it!
41
+ hash = @circle.keys.select() { |k| k > hash }.sort.first
42
+
43
+ return @circle.first.last if hash.nil?
44
+ return @circle[hash]
45
+ end
46
+
47
+ end
@@ -0,0 +1,24 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe ConsistentHashr do
4
+ before(:each) do
5
+ @servers = {:s1 => "s1", :s2 => "s2", :s3 => "s3", :s4 => "s4", :s5 => "s5", :s6 => "s6"}
6
+ @hashr = ConsistentHashr.new(@servers)
7
+ end
8
+
9
+ it "should not change more than 75% of keys" do
10
+ keys = []
11
+ 1000.times do |idx|
12
+ keys << rand(100000).to_s
13
+ end
14
+ before = keys.map() { |k| @hashr.get(k)}
15
+
16
+ @hashr.add_server(:s7, "s7")
17
+
18
+ after = keys.map() { |k| @hashr.get(k)}
19
+
20
+ diff = before.zip(after).find_all {|a| a[0] == a[1] }.size
21
+ diff.should > keys.size*0.7
22
+ end
23
+
24
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format progress
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,3 @@
1
+ require "rubygems"
2
+ require File.dirname(__FILE__) + "/../lib/consistent_hashr"
3
+
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: consistent_hashr
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - julien
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-10 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ description: A simple gem to perform consistent hashing
33
+ email: julien.genestoux@gmail.com
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - LICENSE
40
+ - README.rdoc
41
+ files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ - Rakefile
45
+ - VERSION
46
+ - lib/consistent_hashr.rb
47
+ - spec/consistent_hashr_spec.rb
48
+ - spec/spec.opts
49
+ - spec/spec_helper.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/superfeedr/consistent_hashr
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --charset=UTF-8
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.3.6
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: A ruby gem to do consistent hashing
80
+ test_files:
81
+ - spec/consistent_hashr_spec.rb
82
+ - spec/spec_helper.rb