-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathring.rb
More file actions
142 lines (123 loc) · 3.55 KB
/
Copy pathring.rb
File metadata and controls
142 lines (123 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# frozen_string_literal: true
require 'digest/sha1'
require 'zlib'
module Dalli
class Ring
POINTS_PER_SERVER = 160 # this is the default in libmemcached
attr_accessor :servers, :continuum
def initialize(servers, options)
@servers = servers
@continuum = nil
if servers.size > 1
total_weight = servers.inject(0) { |memo, srv| memo + srv.weight }
continuum = []
servers.each do |server|
entry_count_for(server, servers.size, total_weight).times do |idx|
hash = Digest::SHA1.hexdigest("#{server.name}:#{idx}")
value = Integer("0x#{hash[0..7]}")
continuum << Dalli::Ring::Entry.new(value, server)
end
end
@continuum = continuum.sort_by(&:value)
end
threadsafe! unless options[:threadsafe] == false
@failover = options[:failover] != false
end
def server_for_key(key)
if @continuum
hkey = hash_for(key)
20.times do |try|
entryidx = binary_search(@continuum, hkey)
server = @continuum[entryidx].server
return server if server.alive?
break unless @failover
hkey = hash_for("#{try}#{key}")
end
else
server = @servers.first
return server if server && server.alive?
end
raise Dalli::RingError, "No server available"
end
def lock
@servers.each(&:lock!)
begin
return yield
ensure
@servers.each(&:unlock!)
end
end
private
def threadsafe!
@servers.each do |s|
s.extend(Dalli::Threadsafe)
end
end
def hash_for(key)
Zlib.crc32(key)
end
def entry_count_for(server, total_servers, total_weight)
((total_servers * POINTS_PER_SERVER * server.weight) / Float(total_weight)).floor
end
# Native extension to perform the binary search within the continuum
# space. Fallback to a pure Ruby version if the compilation doesn't work.
# optional for performance and only necessary if you are using multiple
# memcached servers.
begin
require 'inline'
inline do |builder|
builder.c <<-EOM
int binary_search(VALUE ary, unsigned int r) {
long upper = RARRAY_LEN(ary) - 1;
long lower = 0;
long idx = 0;
ID value = rb_intern("value");
VALUE continuumValue;
unsigned int l;
while (lower <= upper) {
idx = (lower + upper) / 2;
continuumValue = rb_funcall(RARRAY_PTR(ary)[idx], value, 0);
l = NUM2UINT(continuumValue);
if (l == r) {
return idx;
}
else if (l > r) {
upper = idx - 1;
}
else {
lower = idx + 1;
}
}
return upper;
}
EOM
end
rescue LoadError
# Find the closest index in the Ring with value <= the given value
def binary_search(ary, value)
upper = ary.size - 1
lower = 0
while (lower <= upper) do
idx = (lower + upper) / 2
comp = ary[idx].value <=> value
if comp == 0
return idx
elsif comp > 0
upper = idx - 1
else
lower = idx + 1
end
end
upper
end
end
class Entry
attr_reader :value
attr_reader :server
def initialize(val, srv)
@value = val
@server = srv
end
end
end
end