Skip to content

Commit c98d0e0

Browse files
committed
Change scheduler's LockRunner into a locket client
Renamed this to Locket::Client and changed its initialization args so it can be used where the 'owner' of a key isn't already known. This enables Diego::Client to use it to retrieve the bosh instance id of the bbs server that is currently active.
1 parent 4bc537c commit c98d0e0

File tree

6 files changed

+48
-51
lines changed

6 files changed

+48
-51
lines changed

lib/cloud_controller/deployment_updater/scheduler.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'cloud_controller/deployment_updater/dispatcher'
22
require 'locket/lock_worker'
3-
require 'locket/lock_runner'
3+
require 'locket/client'
44

55
module VCAP::CloudController
66
module DeploymentUpdater
@@ -24,18 +24,20 @@ def start
2424
return
2525
end
2626

27-
lock_runner = Locket::LockRunner.new(
28-
key: config.get(:deployment_updater, :lock_key),
29-
owner: config.get(:deployment_updater, :lock_owner),
27+
client = Locket::Client.new(
3028
host: config.get(:locket, :host),
3129
port: config.get(:locket, :port),
3230
client_ca_path: config.get(:locket, :ca_file),
3331
client_key_path: config.get(:locket, :key_file),
3432
client_cert_path: config.get(:locket, :cert_file),
3533
)
36-
lock_worker = Locket::LockWorker.new(lock_runner)
34+
lock_worker = Locket::LockWorker.new(client)
3735

38-
lock_worker.acquire_lock_and_repeatedly_call(&update_step)
36+
lock_worker.acquire_lock_and_repeatedly_call(
37+
owner: config.get(:deployment_updater, :lock_owner),
38+
key: config.get(:deployment_updater, :lock_key),
39+
&update_step
40+
)
3941
end
4042
end
4143

lib/locket/lock_runner.rb renamed to lib/locket/client.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
require 'locket/locket_services_pb'
33

44
module Locket
5-
class LockRunner
5+
class Client
66
class Error < StandardError
77
end
88

9-
def initialize(key:, owner:, host:, port:, client_ca_path:, client_cert_path:, client_key_path:)
9+
def initialize(host:, port:, client_ca_path:, client_cert_path:, client_key_path:)
1010
client_ca = File.open(client_ca_path).read
1111
client_key = File.open(client_key_path).read
1212
client_cert = File.open(client_cert_path).read
@@ -16,18 +16,15 @@ def initialize(key:, owner:, host:, port:, client_ca_path:, client_cert_path:, c
1616
GRPC::Core::ChannelCredentials.new(client_ca, client_key, client_cert)
1717
)
1818
@lock_acquired = false
19-
20-
@key = key
21-
@owner = owner
2219
end
2320

24-
def start
21+
def start(owner:, key:)
2522
raise Error.new('Cannot start more than once') if @thread
2623

2724
@thread = Thread.new do
2825
loop do
2926
begin
30-
service.lock(build_lock_request)
27+
service.lock(build_lock_request(owner: owner, key: key))
3128
logger.debug("Acquired lock '#{key}' for owner '#{owner}'")
3229
@lock_acquired = true
3330
rescue GRPC::BadStatus => e
@@ -52,7 +49,7 @@ def lock_acquired?
5249

5350
attr_reader :service, :lock_acquired, :key, :owner
5451

55-
def build_lock_request
52+
def build_lock_request(owner:, key:)
5653
Models::LockRequest.new(
5754
{
5855
resource: {

lib/locket/lock_worker.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module Locket
22
class LockWorker
3-
def initialize(lock_runner)
4-
@lock_runner = lock_runner
3+
def initialize(client)
4+
@client = client
55
end
66

7-
def acquire_lock_and_repeatedly_call(&block)
8-
@lock_runner.start
7+
def acquire_lock_and_repeatedly_call(owner:, key:, &block)
8+
@client.start(owner: owner, key: key)
99
loop do
10-
if @lock_runner.lock_acquired?
10+
if @client.lock_acquired?
1111
yield block
1212
else
1313
sleep 1

spec/unit/lib/cloud_controller/deployment_updater/scheduler_spec.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ module VCAP::CloudController
2323
end
2424

2525
describe '#start' do
26-
let(:lock_runner) { instance_double(Locket::LockRunner, start: nil, lock_acquired?: nil) }
26+
let(:client) { instance_double(Locket::Client, start: nil, lock_acquired?: nil) }
2727
let(:lock_worker) { instance_double(Locket::LockWorker) }
2828
let(:logger) { instance_double(Steno::Logger, info: nil, debug: nil, error: nil) }
2929
let(:statsd_client) { instance_double(Statsd) }
3030
let(:prometheus_updater) { instance_double(VCAP::CloudController::Metrics::PrometheusUpdater) }
3131

3232
before do
33-
allow(Locket::LockRunner).to receive(:new).and_return(lock_runner)
33+
allow(Locket::Client).to receive(:new).and_return(client)
3434
allow(Locket::LockWorker).to receive(:new).and_return(lock_worker)
3535
allow(Steno).to receive(:logger).and_return(logger)
3636

@@ -43,20 +43,18 @@ module VCAP::CloudController
4343
allow(prometheus_updater).to receive(:report_deployment_duration)
4444
end
4545

46-
it 'correctly configures a LockRunner and uses it to initialize a LockWorker' do
46+
it 'correctly configures a Client and uses it to initialize a LockWorker' do
4747
DeploymentUpdater::Scheduler.start
4848

49-
expect(Locket::LockRunner).to have_received(:new).with(
50-
key: TestConfig.config_instance.get(:deployment_updater, :lock_key),
51-
owner: TestConfig.config_instance.get(:deployment_updater, :lock_owner),
49+
expect(Locket::Client).to have_received(:new).with(
5250
host: TestConfig.config_instance.get(:locket, :host),
5351
port: TestConfig.config_instance.get(:locket, :port),
5452
client_ca_path: TestConfig.config_instance.get(:locket, :ca_file),
5553
client_key_path: TestConfig.config_instance.get(:locket, :key_file),
5654
client_cert_path: TestConfig.config_instance.get(:locket, :cert_file),
5755
)
5856

59-
expect(Locket::LockWorker).to have_received(:new).with(lock_runner)
57+
expect(Locket::LockWorker).to have_received(:new).with(client)
6058
end
6159

6260
context 'when locket is not configured' do
@@ -73,8 +71,8 @@ module VCAP::CloudController
7371
it 'doesnt start any lock machinery' do
7472
DeploymentUpdater::Scheduler.start
7573

76-
expect(Locket::LockRunner).not_to have_received(:new)
77-
expect(Locket::LockWorker).not_to have_received(:new).with(lock_runner)
74+
expect(Locket::Client).not_to have_received(:new)
75+
expect(Locket::LockWorker).not_to have_received(:new).with(client)
7876
end
7977

8078
it 'runs the DeploymentUpdater::Dispatcher sleeps for the configured frequency' do

spec/unit/lib/locket/lock_runner_spec.rb renamed to spec/unit/lib/locket/client_spec.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require 'support/paths'
2-
require 'locket/lock_runner'
2+
require 'locket/client'
33
require 'rspec/wait'
44

5-
RSpec.describe Locket::LockRunner do
5+
RSpec.describe Locket::Client do
66
let(:locket_service) { instance_double(Models::Locket::Stub) }
77
let(:key) { 'lock-key' }
88
let(:owner) { 'lock-owner' }
@@ -22,9 +22,7 @@
2222
end
2323

2424
let(:client) do
25-
Locket::LockRunner.new(
26-
key: key,
27-
owner: owner,
25+
Locket::Client.new(
2826
host: host,
2927
port: port,
3028
client_ca_path: client_ca_path,
@@ -56,19 +54,19 @@
5654
allow(locket_service).to receive(:lock)
5755
allow(client).to receive(:sleep)
5856

59-
client.start
57+
client.start(owner: owner, key: key)
6058

6159
wait_for(locket_service).to have_received(:lock).with(lock_request).at_least(3).times
6260
end
6361

6462
it 'raises an error when restarted after it has already been started' do
6563
allow(locket_service).to receive(:lock)
6664

67-
client.start
65+
client.start(owner: owner, key: key)
6866

6967
expect {
70-
client.start
71-
}.to raise_error(Locket::LockRunner::Error, 'Cannot start more than once')
68+
client.start(owner: owner, key: key)
69+
}.to raise_error(Locket::Client::Error, 'Cannot start more than once')
7270
end
7371
end
7472

@@ -92,7 +90,7 @@
9290
client.instance_variable_set(:@lock_acquired, true)
9391
allow(locket_service).to receive(:lock).and_raise(error)
9492

95-
client.start
93+
client.start(owner: owner, key: key)
9694

9795
wait_for(fake_logger).to have_received(:debug).with("Failed to acquire lock '#{key}' for owner '#{owner}': #{error.message}").at_least(:once)
9896
wait_for(-> { client.lock_acquired? }).to be(false)
@@ -103,7 +101,7 @@
103101
it 'reports that it has a lock' do
104102
allow(locket_service).to receive(:lock).and_return(Models::LockResponse)
105103

106-
client.start
104+
client.start(owner: owner, key: key)
107105

108106
wait_for(fake_logger).to have_received(:debug).with("Acquired lock '#{key}' for owner '#{owner}'").at_least(:once)
109107
wait_for(-> { client.lock_acquired? }).to be(true)

spec/unit/lib/locket/lock_worker_spec.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
require 'spec_helper'
22
require 'locket/lock_worker'
3-
require 'locket/lock_runner'
3+
require 'locket/client'
44

55
RSpec.describe Locket::LockWorker do
6-
let(:lock_runner) { instance_double(Locket::LockRunner, start: nil, lock_acquired?: nil) }
7-
subject(:lock_worker) { Locket::LockWorker.new(lock_runner) }
6+
let(:client) { instance_double(Locket::Client, start: nil, lock_acquired?: nil) }
7+
let(:key) { 'lock-key' }
8+
let(:owner) { 'lock-owner' }
9+
subject(:lock_worker) { Locket::LockWorker.new(client) }
810

911
describe '#acquire_lock_and' do
1012
before do
@@ -13,32 +15,32 @@
1315
allow(lock_worker).to receive(:sleep) # dont use real time please
1416
end
1517

16-
it 'should start the LockRunner' do
17-
lock_worker.acquire_lock_and_repeatedly_call {}
18+
it 'should start the Client' do
19+
lock_worker.acquire_lock_and_repeatedly_call(owner: owner, key: key, &{})
1820

19-
expect(lock_runner).to have_received(:start)
21+
expect(client).to have_received(:start)
2022
end
2123

2224
describe 'when it does not have the lock' do
2325
it 'does not yield to the block if it does not have the lock' do
24-
allow(lock_runner).to receive(:lock_acquired?).and_return(false)
26+
allow(client).to receive(:lock_acquired?).and_return(false)
2527

26-
expect { |b| lock_worker.acquire_lock_and_repeatedly_call(&b) }.not_to yield_control
28+
expect { |b| lock_worker.acquire_lock_and_repeatedly_call(owner: owner, key: key, &b) }.not_to yield_control
2729
end
2830

2931
it 'sleeps before attempting to check the lock status again' do
30-
allow(lock_runner).to receive(:lock_acquired?).and_return(false)
32+
allow(client).to receive(:lock_acquired?).and_return(false)
3133

32-
lock_worker.acquire_lock_and_repeatedly_call {}
34+
lock_worker.acquire_lock_and_repeatedly_call(owner: owner, key: key, &{})
3335
expect(lock_worker).to have_received(:sleep).with(1)
3436
end
3537
end
3638

3739
describe 'when it does not have the lock' do
3840
it 'yields to the block if it does have the lock' do
39-
allow(lock_runner).to receive(:lock_acquired?).and_return(true)
41+
allow(client).to receive(:lock_acquired?).and_return(true)
4042

41-
expect { |b| lock_worker.acquire_lock_and_repeatedly_call(&b) }.to yield_control
43+
expect { |b| lock_worker.acquire_lock_and_repeatedly_call(owner: owner, key: key, &b) }.to yield_control
4244
end
4345
end
4446
end

0 commit comments

Comments
 (0)