This repository has been archived by the owner on Sep 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
nameserver.rb
235 lines (187 loc) · 6.28 KB
/
nameserver.rb
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
module Gizzard
module ParallelMap
def parallel_map(enumerable, &block)
enumerable.map do |elem|
Thread.new { Thread.current[:result] = block.call(elem) }
end.map do |thread|
thread.join
thread[:result]
end
end
end
class Shard < Struct.new(:info, :children, :weight)
class << self
def canonical_table_prefix(enum, table_id = nil, base_prefix = "shard")
enum_s = "%0.4i" % enum
table_id_s = table_id.nil? ? nil : table_id < 0 ? "n#{table_id.abs}" : table_id.to_s
[base_prefix, table_id_s, enum_s].compact.join('_')
end
def parse_enumeration(table_prefix)
if match = table_prefix.match(/\d{3,}/)
match[0].to_i
else
raise "Cannot derive enumeration!"
end
end
end
VIRTUAL_SHARD_TYPES = [
"FailingOverShard",
"ReplicatingShard",
"ReadOnlyShard",
"WriteOnlyShard",
"BlockedShard",
]
REPLICATING_SHARD_TYPES = ["ReplicatingShard", "FailingOverShard"]
INVALID_COPY_TYPES = ["ReadOnlyShard", "WriteOnlyShard", "BlockedShard"]
SHARD_SUFFIXES = {
"FailingOverShard" => 'replicating',
"ReplicatingShard" => 'replicating',
"ReadOnlyShard" => 'read_only',
"WriteOnlyShard" => 'write_only',
"BlockedShard" => 'blocked'
}
def id; info.id end
def hostname; id.hostname end
def table_prefix; id.table_prefix end
def class_name; info.class_name end
def source_type; info.source_type end
def destination_type; info.destination_type end
def busy; info.busy end
def template
child_templates = children.map {|c| c.template }
ShardTemplate.new(info.class_name,
id.hostname,
weight,
info.source_type,
info.destination_type,
child_templates)
end
def enumeration
self.class.parse_enumeration(table_prefix)
end
def canonical_shard_id_map(base_prefix = "shard", table_id = nil, enum = nil)
enum ||= self.enumeration
base = Shard.canonical_table_prefix(enum, table_id, base_prefix)
suffix = SHARD_SUFFIXES[class_name.split('.').last]
canonical_name = [base, suffix].compact.join('_')
canonical_id = ShardId.new(self.hostname, canonical_name)
children.inject(canonical_id => self.id) do |m, c|
m.update c.canonical_shard_id_map(base_prefix, table_id, enum)
end
end
end
class Nameserver
include ParallelMap
DEFAULT_PORT = 7917
DEFAULT_RETRIES = 20
PARALLELISM = 10
attr_reader :hosts, :logfile, :dryrun, :framed
alias dryrun? dryrun
def initialize(*hosts)
options = hosts.last.is_a?(Hash) ? hosts.pop : {}
@retries = options[:retries] || DEFAULT_RETRIES
@logfile = options[:log] || "/tmp/gizzmo.log"
@dryrun = options[:dry_run] || false
@framed = options[:framed] || false
@hosts = hosts.flatten
end
def get_shards(ids)
ids.map {|id| with_retry { client.get_shard(id) } }
end
def reload_updated_forwardings
parallel_map all_clients do |c|
with_retry { c.reload_updated_forwardings }
end
end
def reload_config
parallel_map all_clients do |c|
with_retry { c.reload_config }
end
end
def copy_shard(from_shard_id, to_shard_id)
c = random_client
with_retry { c.copy_shard(from_shard_id, to_shard_id) }
end
def repair_shards(*shards)
c = random_client
with_retry { c.repair_shards(*shards) }
end
def diff_shards(*shards)
c = random_client
with_retry { c.diff_shards(*shards) }
end
def respond_to?(method)
client.respond_to? method or super
end
def method_missing(method, *args, &block)
if client.respond_to?(method)
with_retry { client.send(method, *args, &block) }
else
super
end
end
def manifest(*table_ids)
Manifest.new(self, table_ids)
end
private
def client
@client ||= create_client(hosts.first)
end
def all_clients
@all_clients ||= hosts.map {|host| create_client(host) }
end
def random_client
all_clients[rand(all_clients.length)]
end
def create_client(host)
host, port = host.split(":")
port ||= DEFAULT_PORT
Manager.new(host, port.to_i, logfile, framed, dryrun)
end
private
def with_retry
times ||= @retries
yield
rescue ThriftClient::Simple::ThriftException, NoMethodError, Gizzard::GizzardException => e
raise if e.is_a? Gizzard::GizzardException and e.message !~ /Communications link failure/
times -= 1
(times < 0) ? raise : (sleep 2; retry)
end
class Manifest
include ParallelMap
attr_reader :forwardings, :links, :shard_infos, :trees, :templates
def initialize(nameserver, table_ids)
states = nameserver.dump_nameserver(table_ids)
@forwardings = states.map {|s| s.forwardings }.flatten
@links = states.map {|s| s.links }.flatten.inject({}) do |h, link|
(h[link.up_id] ||= []) << [link.down_id, link.weight]; h
end
@shard_infos = states.map {|s| s.shards }.flatten.inject({}) do |h, shard|
h.update shard.id => shard
end
@trees = @forwardings.inject({}) do |h, forwarding|
h.update forwarding => build_tree(forwarding.shard_id)
end
@templates = @trees.inject({}) do |h, (forwarding, shard)|
(h[shard.template] ||= []) << forwarding; h
end
end
private
def get_filtered_forwardings(nameserver, filter)
return filter[:forwardings] if filter[:forwardings]
forwardings = nameserver.get_forwardings
if table_id = filter[:table_id]
forwardings.reject! {|f| f.table_id != table_id }
end
forwardings
end
def build_tree(shard_id, link_weight=ShardTemplate::DEFAULT_WEIGHT)
children = (links[shard_id] || []).map do |(child_id, child_weight)|
build_tree(child_id, child_weight)
end
info = shard_infos[shard_id] or raise "shard info not found for: #{shard_id}"
Shard.new(info, children, link_weight)
end
end
end
end