rabbitmq-cluster 0.0.18 → 0.0.19

Sign up to get free protection for your applications and to get access to all the features.
File without changes
File without changes
File without changes
@@ -7,39 +7,43 @@ class RabbitMQ::Cluster
7
7
 
8
8
  def self.build
9
9
  new(
10
- ::Etcd::Client.new(uri: ENV['ETCD_HOST'])
10
+ ::Etcd::Client.new(host: ENV['ETCD_HOST'])
11
11
  )
12
12
  end
13
13
 
14
14
  def initialize(client)
15
15
  self.client = client
16
- client.connect
17
16
  end
18
17
 
19
- def aquire_lock
20
- with_retry do
21
- sleep 1 until lock = client.update('/rabbitmq/lock', true, false)
22
- end
18
+ def acquire_lock
19
+ client.compare_and_swap('/rabbitmq/lock', value: true, prevValue: false)
23
20
  yield
21
+ rescue ::Etcd::TestFailed
22
+ client.watch('/rabbitmq/lock')
23
+ retry
24
24
  ensure
25
- client.update('/rabbitmq/lock', false, true)
25
+ client.set('/rabbitmq/lock', value: false)
26
26
  end
27
27
 
28
28
  def nodes
29
29
  with_retry do
30
- (client.get('/rabbitmq/nodes') || {}).values.sort
30
+ begin
31
+ client.get('/rabbitmq/nodes').children.map(&:value).sort
32
+ rescue ::Etcd::KeyNotFound
33
+ []
34
+ end
31
35
  end
32
36
  end
33
37
 
34
38
  def register(node_name, wait=nil)
35
39
  with_retry(wait) do
36
- client.set(key_for(node_name), node_name, ttl: 10)
40
+ client.set(key_for(node_name), value: node_name, ttl: 10)
37
41
  end
38
42
  end
39
43
 
40
44
  def erlang_cookie
41
45
  with_retry do
42
- client.get('/rabbitmq/erlang_cookie')
46
+ client.get('/rabbitmq/erlang_cookie').value
43
47
  end
44
48
  end
45
49
 
@@ -47,7 +51,7 @@ class RabbitMQ::Cluster
47
51
  with_retry do
48
52
  client.set(
49
53
  '/rabbitmq/erlang_cookie',
50
- erlang_cookie
54
+ value: erlang_cookie
51
55
  )
52
56
  end
53
57
  end
@@ -62,7 +62,7 @@ class RabbitMQ::Cluster
62
62
  end
63
63
 
64
64
  def remove_stopped_nodes
65
- etcd.aquire_lock do
65
+ etcd.acquire_lock do
66
66
  stopped_nodes.each do |node_name|
67
67
  system("rabbitmqctl forget_cluster_node #{node_name}")
68
68
  end
@@ -1,5 +1,5 @@
1
1
  module RabbitMQ
2
2
  class Cluster
3
- VERSION = "0.0.18"
3
+ VERSION = "0.0.19"
4
4
  end
5
5
  end
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency 'rabbitmq_manager'
22
- spec.add_dependency 'etcd-rb'
22
+ spec.add_dependency 'etcd'
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.5"
25
25
  spec.add_development_dependency "rake"
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe RabbitMQ::Cluster::Etcd do
4
- let(:etcd_client) { double(:etcd, connect: true) }
4
+ let(:etcd_client) { double(:etcd) }
5
5
  subject { described_class.new(etcd_client) }
6
6
 
7
7
  describe '.build' do
@@ -18,16 +18,25 @@ describe RabbitMQ::Cluster::Etcd do
18
18
  describe '#nodes' do
19
19
  it 'returns the list of nodes registed in etcd' do
20
20
  allow(etcd_client).to receive(:get).with('/rabbitmq/nodes').and_return(
21
- {
22
- "/rabbitmq/nodes/rabbit@rabbit1" => "rabbit@rabbit1",
23
- "/rabbitmq/nodes/rabbit@rabbit2" => "rabbit@rabbit2"
24
- }
21
+ double(:response,
22
+ children: [
23
+ double(:response, value: "rabbit@rabbit1"),
24
+ double(:response, value: "rabbit@rabbit2")
25
+ ]
26
+ )
25
27
  )
26
28
  expect(subject.nodes).to eq ["rabbit@rabbit1", "rabbit@rabbit2"]
27
29
  end
28
30
 
29
31
  it 'returns an empty array if there are no nodes registered' do
30
- allow(etcd_client).to receive(:get).with('/rabbitmq/nodes')
32
+ allow(etcd_client).to receive(:get).with('/rabbitmq/nodes').and_return(
33
+ double(:response, children: [])
34
+ )
35
+ expect(subject.nodes).to eq []
36
+ end
37
+
38
+ it 'returns an empty array if the nodes directory has not been created' do
39
+ allow(etcd_client).to receive(:get).with('/rabbitmq/nodes').and_raise(Etcd::KeyNotFound)
31
40
  expect(subject.nodes).to eq []
32
41
  end
33
42
  end
@@ -39,7 +48,7 @@ describe RabbitMQ::Cluster::Etcd do
39
48
  expect(etcd_client).to receive(:set)
40
49
  .with(
41
50
  "/rabbitmq/nodes/#{nodename}",
42
- nodename,
51
+ value: nodename,
43
52
  ttl: 10
44
53
  )
45
54
  subject.register(nodename)
@@ -63,7 +72,7 @@ describe RabbitMQ::Cluster::Etcd do
63
72
  before do
64
73
  allow(etcd_client).to receive(:get)
65
74
  .with('/rabbitmq/erlang_cookie')
66
- .and_return(erlang_cookie)
75
+ .and_return(double(:response, value: erlang_cookie))
67
76
  end
68
77
 
69
78
  it 'has a getter' do
@@ -74,55 +83,50 @@ describe RabbitMQ::Cluster::Etcd do
74
83
  expect(etcd_client).to receive(:set)
75
84
  .with(
76
85
  '/rabbitmq/erlang_cookie',
77
- erlang_cookie
86
+ value: erlang_cookie
78
87
  )
79
88
  subject.erlang_cookie = erlang_cookie
80
89
  end
81
90
  end
82
91
 
83
- describe '#aquire_lock' do
84
- let(:thingy) { double(run: nil) }
92
+ describe '#acquire_lock' do
85
93
  before do
86
- allow(etcd_client).to receive(:update).with('/rabbitmq/lock', false, true).and_return(true)
87
- allow(etcd_client).to receive(:update).with('/rabbitmq/lock', true, false).and_return(true)
94
+ allow(etcd_client).to receive(:compare_and_swap).with('/rabbitmq/lock', value: true, prevValue: false)
95
+ allow(etcd_client).to receive(:set).with('/rabbitmq/lock', value: false)
88
96
  end
89
97
 
90
98
  describe 'when we can get the lock' do
91
99
  it 'runs the code' do
92
- expect(etcd_client).to receive(:update).with('/rabbitmq/lock', true, false).and_return(true)
93
- expect(thingy).to receive(:run)
94
-
95
- subject.aquire_lock { thingy.run }
100
+ expect(etcd_client).to receive(:compare_and_swap).with('/rabbitmq/lock', value: true, prevValue: false)
101
+ expect { |b| subject.acquire_lock(&b) }.to yield_control
96
102
  end
97
103
 
98
104
  it 'gives the lock back when its done' do
99
- expect(etcd_client).to receive(:update).with('/rabbitmq/lock', false, true).and_return(true)
100
-
101
- subject.aquire_lock { thingy.run }
105
+ expect(etcd_client).to receive(:set).with('/rabbitmq/lock', value: false)
106
+ expect { |b| subject.acquire_lock(&b) }.to yield_control
102
107
  end
103
108
  end
104
109
 
105
110
  describe "when we can't get the lock" do
106
- it 'retries till the lock can be aquired' do
107
- expect(etcd_client).to receive(:update)
108
- .with('/rabbitmq/lock', true, false)
109
- .at_least(3).times
110
- .and_return(false, false, true)
111
- expect(thingy).to receive(:run)
112
-
113
- subject.aquire_lock { thingy.run }
111
+ it 'retries till the lock can be acquired' do
112
+ allow(etcd_client).to receive(:watch)
113
+ call_count = 0
114
+ expect(etcd_client).to receive(:compare_and_swap).with('/rabbitmq/lock', value: true, prevValue: false) do
115
+ call_count += 1
116
+ raise Etcd::TestFailed unless call_count == 3
117
+ end
118
+ expect { |b| subject.acquire_lock(&b) }.to yield_control
114
119
  end
115
120
  end
116
121
 
117
122
  describe 'when something explodes' do
118
123
  before do
119
- allow(etcd_client).to receive(:update).with('/rabbitmq/lock', false, true).and_return(true)
120
- allow(etcd_client).to receive(:update).with('/rabbitmq/lock', true, false).and_return(true)
124
+ allow(etcd_client).to receive(:compare_and_swap)
121
125
  end
122
126
 
123
127
  it 'gives the lock back' do
124
- expect(etcd_client).to receive(:update).with('/rabbitmq/lock', false, true).and_return(true)
125
- expect { subject.aquire_lock { fail } }.to raise_error
128
+ expect(etcd_client).to receive(:set).with('/rabbitmq/lock', value: false)
129
+ expect { subject.acquire_lock { fail } }.to raise_error
126
130
  end
127
131
  end
128
132
  end
@@ -229,7 +229,7 @@ class FakeEtcd
229
229
  self.nodes = []
230
230
  end
231
231
 
232
- def aquire_lock
232
+ def acquire_lock
233
233
  yield
234
234
  end
235
235
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rabbitmq-cluster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.18
4
+ version: 0.0.19
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-07-12 00:00:00.000000000 Z
12
+ date: 2014-07-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rabbitmq_manager
@@ -28,7 +28,7 @@ dependencies:
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
- name: etcd-rb
31
+ name: etcd
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
@@ -148,7 +148,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
148
148
  version: '0'
149
149
  segments:
150
150
  - 0
151
- hash: -1450647500986372266
151
+ hash: -1414004565124595041
152
152
  required_rubygems_version: !ruby/object:Gem::Requirement
153
153
  none: false
154
154
  requirements:
@@ -157,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
157
  version: '0'
158
158
  segments:
159
159
  - 0
160
- hash: -1450647500986372266
160
+ hash: -1414004565124595041
161
161
  requirements: []
162
162
  rubyforge_project:
163
163
  rubygems_version: 1.8.25