infopark-politics 0.8.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/infopark-politics.gemspec +1 -1
- data/lib/politics/static_queue_worker.rb +11 -5
- data/spec/static_queue_worker_spec.rb +181 -146
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
Mzc2YjVhN2QyNGFkNGYyMWZiZmIyNGUwMGE3MTRjZTYzYmU2ZWQzOA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MDVhZWYxNjk3OGUxMDAzNjNiNzg5YThlYjA5NThiNDZkMDY0ZjJhYg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YzBmZjU0ODE5MmVhY2VmZDM0ZTNiM2M5OTdiZjczMjNmYjQ3ZDcxNzk3ZGJi
|
10
|
+
NWQ3OWRlNjA2ZGUyYjg2YWYwYjUyYjc4ZWM3MjA5NDAyNGNmYzRiYzY0ZjUy
|
11
|
+
ZDA0YTFkZDY5MjQ2YmRhZGVmNzQzNGJhYWRiZGZkMmQwYmEyZjI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YzIxOWJlNGVmZDVmOWRkNmQ5Mzg0NmE3MWRhZDQxZTFhMzBmZmZlZDI1ODBj
|
14
|
+
OGZkNDllODc4ZDA5OWZkNDFiNGNlNjVhY2Q4ZGU0OGY2NmY3NzBiYzU5Mjg1
|
15
|
+
NWZhYzQyNmY1YzJjOWUxNGQ3NDBiYzI3NWM1N2IwYzBmNjhkODg=
|
data/infopark-politics.gemspec
CHANGED
@@ -6,7 +6,7 @@ end
|
|
6
6
|
|
7
7
|
Gem::Specification.new do |gem|
|
8
8
|
gem.name = "infopark-politics"
|
9
|
-
gem.version = "0.8.
|
9
|
+
gem.version = "0.8.1"
|
10
10
|
gem.summary = "Algorithms and Tools for Distributed Computing in Ruby."
|
11
11
|
gem.description = ""
|
12
12
|
gem.authors = ["Mike Perham", "Tilo Prütz"]
|
@@ -103,12 +103,15 @@ module Politics
|
|
103
103
|
end
|
104
104
|
|
105
105
|
if restart_wanted?
|
106
|
+
log.info "restart triggered"
|
106
107
|
as_dictator { populate_followers_to_stop }
|
107
108
|
# keeping leader state as long as there are followers to stop
|
108
109
|
while !followers_to_stop.empty?
|
110
|
+
log.info "waiting fo workers to stop: #{followers_to_stop}"
|
109
111
|
relax(until_next_iteration / 2)
|
110
112
|
seize_leadership
|
111
113
|
end
|
114
|
+
log.info "leader exiting due to trigger"
|
112
115
|
exit 0
|
113
116
|
end
|
114
117
|
end
|
@@ -126,7 +129,10 @@ module Politics
|
|
126
129
|
log.debug "delivering bucket request"
|
127
130
|
bucket_spec = next_bucket(requestor_uri, context)
|
128
131
|
if !bucket_spec[0] && @followers_to_stop.include?(requestor_uri)
|
129
|
-
|
132
|
+
# the leader stops its own process and must not be killed by its worker
|
133
|
+
if requestor_uri != uri
|
134
|
+
bucket_spec = [:stop, 0]
|
135
|
+
end
|
130
136
|
@followers_to_stop.delete(requestor_uri)
|
131
137
|
end
|
132
138
|
bucket_spec
|
@@ -166,6 +172,10 @@ module Politics
|
|
166
172
|
raise "Please provide a method ”find_workers” returning a list of all other worker URIs"
|
167
173
|
end
|
168
174
|
|
175
|
+
def restart_wanted?
|
176
|
+
memcache_client.get(restart_flag)
|
177
|
+
end
|
178
|
+
|
169
179
|
private
|
170
180
|
|
171
181
|
attr_reader :iteration_end, :memcache_client
|
@@ -177,10 +187,6 @@ module Politics
|
|
177
187
|
def before_perform_leader_duties
|
178
188
|
end
|
179
189
|
|
180
|
-
def restart_wanted?
|
181
|
-
memcache_client.get(restart_flag)
|
182
|
-
end
|
183
|
-
|
184
190
|
def bucket_process(bucket, sleep_time)
|
185
191
|
case bucket
|
186
192
|
when nil
|
@@ -40,46 +40,46 @@ describe UninitializedWorker do
|
|
40
40
|
|
41
41
|
describe "when initializing" do
|
42
42
|
it "should register the removal of the leadership as exit handler" do
|
43
|
-
worker.
|
43
|
+
expect(worker).to receive(:at_exit).ordered {|&h| h}
|
44
44
|
handler = worker.register_worker('worker', 10, :iteration_length => 10)
|
45
45
|
|
46
|
-
worker.
|
47
|
-
worker.
|
46
|
+
expect(worker).to receive(:internal_cleanup).ordered
|
47
|
+
expect(worker).to receive(:cleanup).ordered
|
48
48
|
handler.call
|
49
49
|
end
|
50
50
|
|
51
51
|
it "should have a druby url" do
|
52
52
|
worker.register_worker('worker', 10, :iteration_length => 10)
|
53
|
-
worker.uri.
|
53
|
+
expect(worker.uri).to match(%r|druby://.*:[0-9]+|)
|
54
54
|
end
|
55
55
|
|
56
56
|
it "should not have a hostname" do
|
57
|
-
worker.send(:hostname).
|
57
|
+
expect(worker.send(:hostname)).to be_nil
|
58
58
|
end
|
59
59
|
|
60
60
|
it "should not have a bucket_request_context" do
|
61
|
-
worker.send(:bucket_request_context).
|
61
|
+
expect(worker.send(:bucket_request_context)).to be_nil
|
62
62
|
end
|
63
63
|
|
64
64
|
context "when it has a hostname" do
|
65
65
|
before do
|
66
|
-
worker.
|
66
|
+
allow(worker).to receive(:hostname).and_return '127.0.0.1'
|
67
67
|
end
|
68
68
|
|
69
69
|
it "should use it" do
|
70
70
|
worker.register_worker('worker', 10, :iteration_length => 10)
|
71
|
-
worker.uri.
|
71
|
+
expect(worker.uri).to match(%r|druby://127.0.0.1:[0-9]+|)
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
75
75
|
context "when it does not have a hostname" do
|
76
76
|
before do
|
77
|
-
worker.
|
77
|
+
allow(worker).to receive(:hostname).and_return nil
|
78
78
|
end
|
79
79
|
|
80
80
|
it "should use the systems hostname" do
|
81
81
|
worker.register_worker('worker', 10, :iteration_length => 10)
|
82
|
-
worker.uri.
|
82
|
+
expect(worker.uri).to match(%r|druby://#{`hostname -f`.chomp}:[0-9]+|)
|
83
83
|
end
|
84
84
|
end
|
85
85
|
end
|
@@ -94,54 +94,54 @@ describe Worker do
|
|
94
94
|
end
|
95
95
|
|
96
96
|
it "should return time to next iteration even if nominate was not completed" do
|
97
|
-
worker.until_next_iteration.
|
97
|
+
expect(worker.until_next_iteration).to eq(0)
|
98
98
|
end
|
99
99
|
|
100
100
|
it "should give access to the uri" do
|
101
|
-
worker.uri.
|
101
|
+
expect(worker.uri).to match(%r(^druby://))
|
102
102
|
end
|
103
103
|
|
104
104
|
it "should be alive" do
|
105
|
-
worker.
|
105
|
+
expect(worker).to be_alive
|
106
106
|
end
|
107
107
|
|
108
108
|
describe "when processing bucket" do
|
109
109
|
before do
|
110
|
-
DRbObject.
|
110
|
+
allow(DRbObject).to receive(:new).with(nil, worker.uri).
|
111
111
|
and_return(@worker_drb = double('drb', :alive? => true))
|
112
112
|
end
|
113
113
|
|
114
114
|
it "should raise an error if it is not alive via Drb" do
|
115
|
-
@worker_drb.
|
116
|
-
|
117
|
-
@worker_drb.
|
118
|
-
|
115
|
+
allow(@worker_drb).to receive(:alive?).and_raise("drb error")
|
116
|
+
expect {worker.start}.to raise_error(/cannot reach self/)
|
117
|
+
allow(@worker_drb).to receive(:alive?).and_return(false)
|
118
|
+
expect {worker.start}.to raise_error(/not alive/)
|
119
119
|
end
|
120
120
|
|
121
121
|
describe "" do
|
122
122
|
before do
|
123
|
-
worker.
|
124
|
-
worker.
|
125
|
-
worker.
|
123
|
+
allow(worker).to receive(:until_next_iteration).and_return 666
|
124
|
+
allow(worker).to receive(:nominate)
|
125
|
+
allow(worker).to receive(:loop?).and_return true, true, true, false
|
126
126
|
end
|
127
127
|
|
128
128
|
it "should relax until next iteration on MemCache errors during nomination" do
|
129
|
-
worker.
|
130
|
-
worker.
|
129
|
+
expect(worker).to receive(:nominate).exactly(4).and_raise Dalli::DalliError.new("Buh!")
|
130
|
+
expect(worker).to receive(:relax).with(666).exactly(4).times
|
131
131
|
|
132
132
|
worker.start
|
133
133
|
end
|
134
134
|
|
135
135
|
it "should relax until next iteration on MemCache errors during request for leader" do
|
136
|
-
worker.
|
137
|
-
worker.
|
136
|
+
expect(worker).to receive(:leader_uri).exactly(4).and_raise(Dalli::DalliError.new("Buh"))
|
137
|
+
expect(worker).to receive(:relax).with(666).exactly(4).times
|
138
138
|
|
139
139
|
worker.start
|
140
140
|
end
|
141
141
|
|
142
142
|
describe "as leader" do
|
143
143
|
before do
|
144
|
-
worker.
|
144
|
+
allow(worker).to receive(:leader?).and_return true
|
145
145
|
allow(worker).to receive(:bucket_process) { sleep 0.5 }
|
146
146
|
allow(worker).to receive(:relax)
|
147
147
|
allow(worker).to receive(:leader).and_return(double('leader', bucket_request: nil))
|
@@ -149,37 +149,37 @@ describe Worker do
|
|
149
149
|
|
150
150
|
it "performs leader duties in a separate thread" do
|
151
151
|
# four iterations of 0.5 seconds → two leader threads of one second
|
152
|
-
worker.
|
152
|
+
expect(worker).to receive(:perform_leader_duties).exactly(2).times { sleep 1 }
|
153
153
|
worker.start
|
154
154
|
end
|
155
155
|
end
|
156
156
|
|
157
157
|
describe "as follower" do
|
158
158
|
before do
|
159
|
-
worker.
|
160
|
-
worker.
|
161
|
-
worker.
|
162
|
-
@leader.
|
159
|
+
allow(worker).to receive(:leader?).and_return false
|
160
|
+
allow(worker).to receive(:leader_uri).and_return "the leader"
|
161
|
+
allow(worker).to receive(:leader).and_return(@leader = double('leader'))
|
162
|
+
allow(@leader).to receive(:bucket_request).and_return([1, 0])
|
163
163
|
end
|
164
164
|
|
165
165
|
it "should get the bucket to process from the leader at every iteration" do
|
166
|
-
worker.
|
167
|
-
@leader.
|
166
|
+
expect(worker).to receive(:leader).exactly(4).times.and_return @leader
|
167
|
+
expect(@leader).to receive(:bucket_request).with(worker.uri, nil).exactly(4).times.
|
168
168
|
and_return([1, 2])
|
169
169
|
worker.start
|
170
170
|
end
|
171
171
|
|
172
172
|
it "should send the bucket_request_context with the bucket request" do
|
173
|
-
worker.
|
174
|
-
worker.
|
175
|
-
@leader.
|
173
|
+
allow(worker).to receive(:loop?).and_return false
|
174
|
+
allow(worker).to receive(:bucket_request_context).and_return "the context"
|
175
|
+
expect(@leader).to receive(:bucket_request).with(worker.uri, "the context")
|
176
176
|
worker.start
|
177
177
|
end
|
178
178
|
|
179
179
|
it "should exit on :stop bucket" do
|
180
|
-
@leader.
|
181
|
-
worker.
|
182
|
-
worker.
|
180
|
+
expect(@leader).to receive(:bucket_request).ordered.once.and_return([:stop, 0])
|
181
|
+
expect(worker).to receive(:exit).with(0).ordered do
|
182
|
+
expect(worker).to receive(:loop?).and_return false
|
183
183
|
end
|
184
184
|
worker.start
|
185
185
|
end
|
@@ -190,73 +190,107 @@ describe Worker do
|
|
190
190
|
describe "when handling a bucket request" do
|
191
191
|
describe "as leader" do
|
192
192
|
before do
|
193
|
-
worker.
|
193
|
+
allow(worker).to receive(:leader?).and_return true
|
194
|
+
allow(worker).to receive(:uri).and_return leader
|
194
195
|
end
|
195
196
|
|
196
|
-
|
197
|
-
|
198
|
-
|
197
|
+
subject { worker.bucket_request(requestor, context) }
|
198
|
+
|
199
|
+
let(:requestor) { 'requestor' }
|
200
|
+
let(:context) { 'context' }
|
201
|
+
let(:leader) { 'leader' }
|
202
|
+
|
203
|
+
it "delivers the bucket" do
|
204
|
+
expect(worker).to receive(:next_bucket).with("requestor", "context").and_return "the bucket"
|
205
|
+
expect(subject).to eq("the bucket")
|
199
206
|
end
|
200
207
|
|
201
208
|
describe "when no buckets are left" do
|
202
209
|
before do
|
203
|
-
worker.
|
210
|
+
allow(worker).to receive(:find_workers).and_return(["1", "2", "3", leader])
|
204
211
|
worker.populate_followers_to_stop
|
205
|
-
DRbObject.
|
212
|
+
allow(DRbObject).to receive(:new).and_return(double('o', :alive? => true))
|
206
213
|
end
|
207
214
|
|
208
|
-
|
209
|
-
|
210
|
-
|
215
|
+
context "when requestor is in followers_to_stop list" do
|
216
|
+
context "when requestor is another worker" do
|
217
|
+
let(:requestor) { '2' }
|
218
|
+
|
219
|
+
it "delivers the :stop bucket" do
|
220
|
+
expect(subject).to eq([:stop, 0])
|
221
|
+
end
|
222
|
+
|
223
|
+
it "removes the requestor from the followers_to_stop list" do
|
224
|
+
expect {
|
225
|
+
subject
|
226
|
+
}.to change {
|
227
|
+
worker.followers_to_stop
|
228
|
+
}.from(["1", "2", "3", leader]).to(["1", "3", leader])
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
context "when requestor is leaders worker" do
|
233
|
+
let(:requestor) { leader }
|
211
234
|
|
212
|
-
|
213
|
-
|
235
|
+
it "does not deliver the :stop bucket" do
|
236
|
+
expect(subject[0]).to be_nil
|
237
|
+
end
|
238
|
+
|
239
|
+
it "removes the requestor from the followers_to_stop list" do
|
240
|
+
expect {
|
241
|
+
subject
|
242
|
+
}.to change {
|
243
|
+
worker.followers_to_stop
|
244
|
+
}.from(["1", "2", "3", leader]).to(["1", "2", "3"])
|
245
|
+
end
|
246
|
+
end
|
214
247
|
end
|
215
248
|
|
216
|
-
|
217
|
-
|
218
|
-
|
249
|
+
context "when requestor is not in followers_to_stop list" do
|
250
|
+
it "does not deliver the :stop bucket" do
|
251
|
+
expect(subject[0]).to be_nil
|
252
|
+
end
|
219
253
|
end
|
220
254
|
end
|
221
255
|
end
|
222
256
|
|
223
257
|
describe "as follower" do
|
224
258
|
before do
|
225
|
-
worker.
|
259
|
+
allow(worker).to receive(:leader?).and_return false
|
226
260
|
end
|
227
261
|
|
228
262
|
it "should deliver the :not_leader bucket" do
|
229
|
-
worker.bucket_request("requestor", nil)[0].
|
263
|
+
expect(worker.bucket_request("requestor", nil)[0]).to eq(:not_leader)
|
230
264
|
end
|
231
265
|
end
|
232
266
|
end
|
233
267
|
|
234
268
|
describe "when determining if restart is wanted" do
|
235
269
|
it "should return true if the restart flag is set in memcache" do
|
236
|
-
memcache_client.
|
237
|
-
worker.
|
270
|
+
expect(memcache_client).to receive(:get).with('worker_restart').and_return true
|
271
|
+
expect(worker).to be_restart_wanted
|
238
272
|
end
|
239
273
|
|
240
274
|
it "should return false if the restart flag is not set in memcache" do
|
241
|
-
memcache_client.
|
242
|
-
worker.
|
243
|
-
memcache_client.
|
244
|
-
worker.
|
275
|
+
expect(memcache_client).to receive(:get).with('worker_restart').and_return false
|
276
|
+
expect(worker).not_to be_restart_wanted
|
277
|
+
expect(memcache_client).to receive(:get).with('worker_restart').and_return nil
|
278
|
+
expect(worker).not_to be_restart_wanted
|
245
279
|
end
|
246
280
|
end
|
247
281
|
|
248
282
|
describe "when performing leader duties" do
|
249
283
|
before do
|
250
|
-
worker.
|
251
|
-
worker.
|
252
|
-
worker.
|
253
|
-
worker.
|
254
|
-
worker.
|
255
|
-
worker.
|
284
|
+
allow(worker).to receive(:until_next_iteration).and_return 0
|
285
|
+
allow(worker).to receive(:leader?).and_return true
|
286
|
+
allow(worker).to receive(:dictatorship_length).and_return 666
|
287
|
+
allow(worker).to receive(:iteration_length).and_return 5
|
288
|
+
allow(worker).to receive(:find_workers).and_return []
|
289
|
+
allow(worker).to receive(:initialize_buckets)
|
256
290
|
end
|
257
291
|
|
258
292
|
it "performs before_perform_leader_duties callback" do
|
259
|
-
worker.
|
293
|
+
expect(worker).to receive(:before_perform_leader_duties)
|
260
294
|
worker.perform_leader_duties
|
261
295
|
end
|
262
296
|
|
@@ -265,52 +299,52 @@ describe Worker do
|
|
265
299
|
end
|
266
300
|
|
267
301
|
it "should initialize buckets as dictator" do
|
268
|
-
worker.
|
269
|
-
worker.
|
270
|
-
worker.
|
302
|
+
expect(worker).to receive(:seize_leadership).with(666).ordered
|
303
|
+
expect(worker).to receive(:initialize_buckets).ordered
|
304
|
+
expect(worker).to receive(:seize_leadership).ordered
|
271
305
|
worker.perform_leader_duties
|
272
306
|
end
|
273
307
|
|
274
308
|
describe "as long as there are buckets" do
|
275
309
|
before do
|
276
|
-
worker.
|
277
|
-
worker.
|
310
|
+
allow(worker).to receive(:buckets).and_return([1], [2], [3], [4], [])
|
311
|
+
allow(worker).to receive(:relax)
|
278
312
|
end
|
279
313
|
|
280
314
|
it "should update buckets periodically" do
|
281
|
-
worker.
|
315
|
+
expect(worker).to receive(:update_buckets).exactly(4).times
|
282
316
|
worker.perform_leader_duties
|
283
317
|
end
|
284
318
|
|
285
319
|
it "should relax half of the time to the next iteration" do
|
286
|
-
worker.
|
287
|
-
worker.
|
320
|
+
allow(worker).to receive(:until_next_iteration).and_return(6)
|
321
|
+
expect(worker).to receive(:relax).with(3).exactly(4).times
|
288
322
|
worker.perform_leader_duties
|
289
323
|
end
|
290
324
|
|
291
325
|
it "should seize the leadership periodically" do
|
292
|
-
worker.
|
326
|
+
expect(worker).to receive(:seize_leadership).at_least(4).times
|
293
327
|
worker.perform_leader_duties
|
294
328
|
end
|
295
329
|
|
296
330
|
it "should seize the leadership periodically even if restart is wanted" do
|
297
|
-
worker.
|
298
|
-
worker.
|
299
|
-
worker.
|
331
|
+
allow(worker).to receive(:restart_wanted?).and_return true
|
332
|
+
allow(worker).to receive(:exit)
|
333
|
+
expect(worker).to receive(:seize_leadership).at_least(4).times
|
300
334
|
worker.perform_leader_duties
|
301
335
|
end
|
302
336
|
|
303
337
|
it "should not update buckets if restart is wanted" do
|
304
|
-
worker.
|
305
|
-
worker.
|
306
|
-
worker.
|
338
|
+
allow(worker).to receive(:restart_wanted?).and_return true
|
339
|
+
allow(worker).to receive(:exit)
|
340
|
+
expect(worker).not_to receive(:update_buckets)
|
307
341
|
worker.perform_leader_duties
|
308
342
|
end
|
309
343
|
end
|
310
344
|
|
311
345
|
describe "if there are no more buckets" do
|
312
346
|
before do
|
313
|
-
worker.
|
347
|
+
allow(worker).to receive(:buckets).and_return([])
|
314
348
|
end
|
315
349
|
|
316
350
|
context "when restart is wanted" do
|
@@ -320,36 +354,37 @@ describe Worker do
|
|
320
354
|
end
|
321
355
|
|
322
356
|
it "populates the followers_to_stop list before evaluating it" do
|
323
|
-
worker.
|
324
|
-
worker.
|
357
|
+
expect(worker).to receive(:populate_followers_to_stop).ordered.once
|
358
|
+
expect(worker).to receive(:followers_to_stop).ordered.and_return []
|
325
359
|
worker.perform_leader_duties
|
326
360
|
end
|
327
361
|
|
328
362
|
context "as long as there are followers to stop" do
|
329
363
|
before do
|
330
|
-
|
331
|
-
worker.
|
364
|
+
# followers_to_stop is called twice due to logging
|
365
|
+
allow(worker).to receive(:followers_to_stop).and_return([1], [1], [2], [2], [])
|
366
|
+
allow(worker).to receive(:relax)
|
332
367
|
end
|
333
368
|
|
334
369
|
it "relaxes half of the time to the next iteration" do
|
335
|
-
worker.
|
336
|
-
worker.
|
370
|
+
allow(worker).to receive(:until_next_iteration).and_return(6)
|
371
|
+
expect(worker).to receive(:relax).with(3).exactly(2).times
|
337
372
|
worker.perform_leader_duties
|
338
373
|
end
|
339
374
|
|
340
375
|
it "seizes the leadership periodically" do
|
341
|
-
worker.
|
376
|
+
expect(worker).to receive(:seize_leadership).at_least(2).times
|
342
377
|
worker.perform_leader_duties
|
343
378
|
end
|
344
379
|
end
|
345
380
|
|
346
381
|
context "if there are no more followers to stop" do
|
347
382
|
before do
|
348
|
-
worker.
|
383
|
+
allow(worker).to receive(:followers_to_stop).and_return([])
|
349
384
|
end
|
350
385
|
|
351
386
|
it "exits" do
|
352
|
-
worker.
|
387
|
+
expect(worker).to receive(:exit).with(0)
|
353
388
|
worker.perform_leader_duties
|
354
389
|
end
|
355
390
|
end
|
@@ -361,8 +396,8 @@ describe Worker do
|
|
361
396
|
end
|
362
397
|
|
363
398
|
it "does not populate the followers_to_stop list if restart is not wanted" do
|
364
|
-
worker.
|
365
|
-
worker.
|
399
|
+
allow(worker).to receive(:restart_wanted?).and_return false
|
400
|
+
expect(worker).not_to receive(:populate_followers_to_stop)
|
366
401
|
worker.perform_leader_duties
|
367
402
|
end
|
368
403
|
end
|
@@ -371,133 +406,133 @@ describe Worker do
|
|
371
406
|
|
372
407
|
describe "when seizing leadership" do
|
373
408
|
before do
|
374
|
-
worker.
|
375
|
-
worker.
|
376
|
-
worker.
|
409
|
+
allow(worker).to receive(:uri).and_return('myself')
|
410
|
+
allow(worker).to receive(:iteration_length).and_return 123
|
411
|
+
allow(worker).to receive(:token).and_return('dcc-group')
|
377
412
|
end
|
378
413
|
|
379
414
|
it "should set itself to leader" do
|
380
|
-
memcache_client.
|
415
|
+
expect(memcache_client).to receive(:set).with(anything(), 'myself', anything())
|
381
416
|
worker.seize_leadership
|
382
417
|
end
|
383
418
|
|
384
419
|
it "should seize the leadership for the amount of seconds given" do
|
385
|
-
memcache_client.
|
420
|
+
expect(memcache_client).to receive(:set).with(anything(), anything(), 666)
|
386
421
|
worker.seize_leadership 666
|
387
422
|
end
|
388
423
|
|
389
424
|
it "should seize the leadership for iteration_length if no duration is given" do
|
390
|
-
memcache_client.
|
425
|
+
expect(memcache_client).to receive(:set).with(anything(), anything(), 123)
|
391
426
|
worker.seize_leadership
|
392
427
|
end
|
393
428
|
|
394
429
|
it "should seize the leadership for the worker's group" do
|
395
|
-
memcache_client.
|
430
|
+
expect(memcache_client).to receive(:set).with('dcc-group', anything(), anything())
|
396
431
|
worker.seize_leadership
|
397
432
|
end
|
398
433
|
|
399
434
|
it "should have the next iteration exactly when the seized leadership ends" do
|
400
435
|
now = Time.now
|
401
|
-
Time.
|
436
|
+
allow(Time).to receive(:now).and_return now
|
402
437
|
end_of_leadership = now + 666
|
403
438
|
|
404
439
|
worker.seize_leadership 666
|
405
|
-
worker.until_next_iteration.
|
440
|
+
expect(worker.until_next_iteration).to eq(666)
|
406
441
|
|
407
442
|
worker.seize_leadership
|
408
|
-
worker.until_next_iteration.
|
443
|
+
expect(worker.until_next_iteration).to eq(123)
|
409
444
|
|
410
445
|
worker.seize_leadership 6
|
411
|
-
worker.until_next_iteration.
|
446
|
+
expect(worker.until_next_iteration).to eq(6)
|
412
447
|
end
|
413
448
|
end
|
414
449
|
|
415
450
|
describe "when creating next bucket" do
|
416
451
|
it "should set the sleep time to sleep_until_next_bucket_time" do
|
417
|
-
worker.
|
418
|
-
worker.next_bucket('', nil)[1].
|
452
|
+
expect(worker).to receive(:sleep_until_next_bucket_time).and_return 'the sleep time'
|
453
|
+
expect(worker.next_bucket('', nil)[1]).to eq('the sleep time')
|
419
454
|
end
|
420
455
|
end
|
421
456
|
|
422
457
|
describe "when computing the sleep_until_next_bucket_time" do
|
423
458
|
before do
|
424
|
-
worker.
|
425
|
-
worker.
|
459
|
+
allow(worker).to receive(:iteration_length).and_return 10
|
460
|
+
allow(worker).to receive(:until_next_iteration).and_return 6
|
426
461
|
end
|
427
462
|
|
428
463
|
it "should set the sleep time to half the time until_next_iteration" do
|
429
|
-
worker.sleep_until_next_bucket_time.
|
464
|
+
expect(worker.sleep_until_next_bucket_time).to eq(3)
|
430
465
|
end
|
431
466
|
|
432
467
|
it "should set the sleep time to at least 1 second" do
|
433
|
-
worker.
|
434
|
-
worker.sleep_until_next_bucket_time.
|
468
|
+
allow(worker).to receive(:until_next_iteration).and_return 0.6
|
469
|
+
expect(worker.sleep_until_next_bucket_time).to eq(1)
|
435
470
|
end
|
436
471
|
|
437
472
|
it "should set the sleep time to at most a half of the interation_length" do
|
438
|
-
worker.
|
439
|
-
worker.sleep_until_next_bucket_time.
|
473
|
+
allow(worker).to receive(:until_next_iteration).and_return 60
|
474
|
+
expect(worker.sleep_until_next_bucket_time).to eq(5)
|
440
475
|
end
|
441
476
|
end
|
442
477
|
|
443
478
|
describe "when providing leader object" do
|
444
479
|
before do
|
445
|
-
worker.
|
480
|
+
allow(worker).to receive(:until_next_iteration).and_return 0
|
446
481
|
end
|
447
482
|
|
448
483
|
it "should return a drb object with the leader uri" do
|
449
|
-
worker.
|
450
|
-
DRbObject.
|
451
|
-
worker.leader.
|
484
|
+
allow(worker).to receive(:leader_uri).and_return("leader's uri")
|
485
|
+
expect(DRbObject).to receive(:new).with(nil, "leader's uri").and_return "leader"
|
486
|
+
expect(worker.leader).to eq("leader")
|
452
487
|
end
|
453
488
|
|
454
489
|
it "should try three times to get the leader on anarchy (no leader)" do
|
455
|
-
worker.
|
490
|
+
expect(worker).to receive(:leader_uri).at_least(3).times.and_return nil
|
456
491
|
worker.leader rescue nil
|
457
492
|
end
|
458
493
|
|
459
494
|
it "should raise an error when leader cannot be determined during anarchy" do
|
460
|
-
worker.
|
461
|
-
|
495
|
+
allow(worker).to receive(:leader_uri).and_return nil
|
496
|
+
expect {worker.leader}.to raise_error(/cannot determine leader/)
|
462
497
|
end
|
463
498
|
|
464
499
|
it "should sleep until next iteration before retrying to get leader" do
|
465
|
-
worker.
|
466
|
-
worker.
|
467
|
-
worker.
|
500
|
+
allow(worker).to receive(:leader_uri).and_return nil
|
501
|
+
allow(worker).to receive(:until_next_iteration).and_return 666
|
502
|
+
expect(worker).to receive(:relax).with(666).exactly(2).times
|
468
503
|
worker.leader rescue nil
|
469
504
|
end
|
470
505
|
end
|
471
506
|
|
472
507
|
describe "when cleaning up" do
|
473
508
|
before do
|
474
|
-
worker.
|
509
|
+
allow(worker).to receive(:group_name).and_return('the group')
|
475
510
|
end
|
476
511
|
|
477
512
|
describe "as leader" do
|
478
513
|
before do
|
479
|
-
worker.
|
480
|
-
memcache_client.
|
514
|
+
allow(worker).to receive(:leader?).and_return true
|
515
|
+
allow(memcache_client).to receive(:delete)
|
481
516
|
end
|
482
517
|
|
483
518
|
it "should remove the leadership token from memcache" do
|
484
|
-
memcache_client.
|
519
|
+
expect(memcache_client).to receive(:delete).with('the group_token')
|
485
520
|
worker.send(:internal_cleanup)
|
486
521
|
end
|
487
522
|
|
488
523
|
it "should remove the restart wanted flag from memcache" do
|
489
|
-
memcache_client.
|
524
|
+
expect(memcache_client).to receive(:delete).with('the group_restart')
|
490
525
|
worker.send(:internal_cleanup)
|
491
526
|
end
|
492
527
|
end
|
493
528
|
|
494
529
|
describe "as follower" do
|
495
530
|
before do
|
496
|
-
worker.
|
531
|
+
allow(worker).to receive(:leader?).and_return false
|
497
532
|
end
|
498
533
|
|
499
534
|
it "should not remove anything from memcache" do
|
500
|
-
memcache_client.
|
535
|
+
expect(memcache_client).not_to receive(:delete)
|
501
536
|
worker.send(:internal_cleanup)
|
502
537
|
end
|
503
538
|
end
|
@@ -505,39 +540,39 @@ describe Worker do
|
|
505
540
|
|
506
541
|
describe "when populating followers_to_stop" do
|
507
542
|
before do
|
508
|
-
worker.
|
509
|
-
DRbObject.
|
543
|
+
allow(worker).to receive(:find_workers).and_return(%w(a b c))
|
544
|
+
allow(DRbObject).to receive(:new).and_return(double('o', :alive? => true))
|
510
545
|
end
|
511
546
|
|
512
547
|
it "should add all visible workers" do
|
513
548
|
worker.populate_followers_to_stop
|
514
|
-
worker.followers_to_stop.
|
549
|
+
expect(worker.followers_to_stop).to match_array(%w(a b c))
|
515
550
|
end
|
516
551
|
end
|
517
552
|
|
518
553
|
describe "when delivering followers_to_stop" do
|
519
554
|
before do
|
520
|
-
worker.
|
555
|
+
allow(worker).to receive(:find_workers).and_return(%w(a b c))
|
521
556
|
worker.populate_followers_to_stop
|
522
|
-
DRbObject.
|
557
|
+
allow(DRbObject).to receive(:new).and_return(double('o', :alive? => true))
|
523
558
|
end
|
524
559
|
|
525
560
|
it "should return the actual followers_to_stop" do
|
526
|
-
worker.followers_to_stop.
|
561
|
+
expect(worker.followers_to_stop).to match_array(%w(a b c))
|
527
562
|
end
|
528
563
|
|
529
564
|
it "should not deliver entries that are not reachable at the moment" do
|
530
|
-
DRbObject.
|
531
|
-
DRbObject.
|
532
|
-
x.
|
533
|
-
worker.followers_to_stop.
|
565
|
+
allow(DRbObject).to receive(:new).with(nil, 'a').and_return(double('o', :alive? => false))
|
566
|
+
allow(DRbObject).to receive(:new).with(nil, 'b').and_return(x = double('o'))
|
567
|
+
allow(x).to receive(:alive?).and_raise DRb::DRbConnError.new('nix da')
|
568
|
+
expect(worker.followers_to_stop).to eq(%w(c))
|
534
569
|
end
|
535
570
|
|
536
571
|
it "should not remove unreachable entries from the list - maybe they reappear" do
|
537
|
-
DRbObject.
|
538
|
-
worker.followers_to_stop.
|
539
|
-
DRbObject.
|
540
|
-
worker.followers_to_stop.
|
572
|
+
allow(DRbObject).to receive(:new).with(nil, 'a').and_return(double('o', :alive? => false))
|
573
|
+
expect(worker.followers_to_stop).to match_array(%w(b c))
|
574
|
+
allow(DRbObject).to receive(:new).with(nil, 'a').and_return(double('o', :alive? => true))
|
575
|
+
expect(worker.followers_to_stop).to match_array(%w(a b c))
|
541
576
|
end
|
542
577
|
end
|
543
578
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: infopark-politics
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Perham
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-04-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|