Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(POOLER-150) Synchronize checkout operations for API #339

Merged
merged 1 commit into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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