Skip to content

Commit

Permalink
Merge pull request #339 from mattkirby/pooler_150
Browse files Browse the repository at this point in the history
(POOLER-150) Synchronize checkout operations for API
  • Loading branch information
kevpl authored Oct 22, 2019
2 parents 79bd140 + 30bf207 commit a6e3e3a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ git logs & PR history.

### Fixed
- Correctly detect create\_linked\_clone on a pool level (POOLER-147)
- Ensure that checkout operations are synchronized

# [0.7.0](https://github.com/puppetlabs/vmpooler/compare/0.6.3...0.7.0)

Expand Down
3 changes: 2 additions & 1 deletion bin/vmpooler
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ if torun.include? 'api'
api = Thread.new do
thr = Vmpooler::API.new
redis = Vmpooler.new_redis(redis_host, redis_port, redis_password)
thr.helpers.configure(config, redis, metrics)
checkoutlock = Mutex.new
thr.helpers.configure(config, redis, metrics, checkoutlock)
thr.helpers.execute!
end
torun_threads << api
Expand Down
3 changes: 2 additions & 1 deletion lib/vmpooler/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ def initialize
use Vmpooler::API::Reroute
use Vmpooler::API::V1

def configure(config, redis, metrics)
def configure(config, redis, metrics, checkoutlock)
self.settings.set :config, config
self.settings.set :redis, redis
self.settings.set :metrics, metrics
self.settings.set :checkoutlock, checkoutlock
end

def execute!
Expand Down
30 changes: 18 additions & 12 deletions lib/vmpooler/api/v1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def need_token!
validate_token(backend)
end

def checkoutlock
Vmpooler::API::settings.checkoutlock
end

def fetch_single_vm(template)
template_backends = [template]
aliases = Vmpooler::API.settings.config[:alias]
Expand Down Expand Up @@ -67,21 +71,23 @@ def fetch_single_vm(template)
end
end

template_backends.each do |template_backend|
vms = backend.smembers("vmpooler__ready__#{template_backend}")
next if vms.empty?
vms.reverse.each do |vm|
ready = vm_ready?(vm, config['domain'])
if ready
backend.smove("vmpooler__ready__#{template_backend}", "vmpooler__running__#{template_backend}", vm)
return [vm, template_backend, template]
else
backend.smove("vmpooler__ready__#{template_backend}", "vmpooler__completed__#{template_backend}", vm)
metrics.increment("checkout.nonresponsive.#{template_backend}")
checkoutlock.synchronize do
template_backends.each do |template_backend|
vms = backend.smembers("vmpooler__ready__#{template_backend}")
next if vms.empty?
vms.reverse.each do |vm|
ready = vm_ready?(vm, config['domain'])
if ready
backend.smove("vmpooler__ready__#{template_backend}", "vmpooler__running__#{template_backend}", vm)
return [vm, template_backend, template]
else
backend.smove("vmpooler__ready__#{template_backend}", "vmpooler__completed__#{template_backend}", vm)
metrics.increment("checkout.nonresponsive.#{template_backend}")
end
end
end
[nil, nil, nil]
end
[nil, nil, nil]
end

def return_vm_to_ready_state(template, vm)
Expand Down
2 changes: 2 additions & 0 deletions spec/integration/api/v1/vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ def app()
}
let(:current_time) { Time.now }
let(:vmname) { 'abcdefghijkl' }
let(:checkoutlock) { Mutex.new }

before(:each) do
app.settings.set :config, config
app.settings.set :redis, redis
app.settings.set :metrics, metrics
app.settings.set :config, auth: false
app.settings.set :checkoutlock, checkoutlock
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
end

Expand Down
2 changes: 2 additions & 0 deletions spec/integration/api/v1/vm_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ def app()

let(:current_time) { Time.now }
let(:socket) { double('socket') }
let(:checkoutlock) { Mutex.new }

before(:each) do
app.settings.set :config, config
app.settings.set :redis, redis
app.settings.set :metrics, metrics
app.settings.set :config, auth: false
app.settings.set :checkoutlock, checkoutlock
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
end

Expand Down

0 comments on commit a6e3e3a

Please sign in to comment.