diff --git a/bin/vmpooler b/bin/vmpooler index f053651..69565aa 100755 --- a/bin/vmpooler +++ b/bin/vmpooler @@ -38,7 +38,7 @@ if torun.include? 'manager' Vmpooler::PoolManager.new( config, Vmpooler.new_logger(logger_file), - Vmpooler.redis_connection_pool(redis_host, redis_port, redis_password, redis_connection_pool_size, redis_connection_pool_timeout), + Vmpooler.redis_connection_pool(redis_host, redis_port, redis_password, redis_connection_pool_size, redis_connection_pool_timeout, metrics), metrics ).execute! end diff --git a/lib/vmpooler.rb b/lib/vmpooler.rb index ca87d4a..ffcfcd8 100644 --- a/lib/vmpooler.rb +++ b/lib/vmpooler.rb @@ -160,9 +160,16 @@ module Vmpooler pools end - def self.redis_connection_pool(host, port, password, size, timeout) - ConnectionPool.new(size: size, timeout: timeout) { - redis_connection(host, port, password) + def self.redis_connection_pool(host, port, password, size, timeout, metrics) + Vmpooler::PoolManager::GenericConnectionPool.new( + metrics: metrics, + metric_prefix: 'redis_connection_pool', + size: size, + timeout: timeout + ) { + connection = Concurrent::Hash.new + redis = redis_connection(host, port, password) + connection['connection'] = redis } end diff --git a/lib/vmpooler/generic_connection_pool.rb b/lib/vmpooler/generic_connection_pool.rb index 6ae916b..9e9fc0c 100644 --- a/lib/vmpooler/generic_connection_pool.rb +++ b/lib/vmpooler/generic_connection_pool.rb @@ -15,35 +15,17 @@ module Vmpooler @metric_prefix = 'connectionpool' if @metric_prefix.nil? || @metric_prefix == '' end - if Thread.respond_to?(:handle_interrupt) - # MRI - def with_metrics(options = {}) - Thread.handle_interrupt(Exception => :never) do - start = Time.now - conn = checkout(options) - timespan_ms = ((Time.now - start) * 1000).to_i - @metrics&.gauge(@metric_prefix + '.available', @available.length) - @metrics&.timing(@metric_prefix + '.waited', timespan_ms) - begin - Thread.handle_interrupt(Exception => :immediate) do - yield conn - end - ensure - checkin - @metrics&.gauge(@metric_prefix + '.available', @available.length) - end - end - end - else - # jruby 1.7.x - def with_metrics(options = {}) + def with_metrics(options = {}) + Thread.handle_interrupt(Exception => :never) do start = Time.now conn = checkout(options) timespan_ms = ((Time.now - start) * 1000).to_i @metrics&.gauge(@metric_prefix + '.available', @available.length) @metrics&.timing(@metric_prefix + '.waited', timespan_ms) begin - yield conn + Thread.handle_interrupt(Exception => :immediate) do + yield conn + end ensure checkin @metrics&.gauge(@metric_prefix + '.available', @available.length) diff --git a/lib/vmpooler/pool_manager.rb b/lib/vmpooler/pool_manager.rb index 5bb72f8..7ea69c8 100644 --- a/lib/vmpooler/pool_manager.rb +++ b/lib/vmpooler/pool_manager.rb @@ -48,7 +48,7 @@ module Vmpooler # Place pool configuration in redis so an API instance can discover running pool configuration def load_pools_to_redis - @redis.with do |redis| + @redis.with_metrics do |redis| previously_configured_pools = redis.smembers('vmpooler__pools') currently_configured_pools = [] config[:pools].each do |pool| @@ -80,7 +80,7 @@ module Vmpooler _check_pending_vm(vm, pool, timeout, provider) rescue StandardError => e $logger.log('s', "[!] [#{pool}] '#{vm}' #{timeout} #{provider} errored while checking a pending vm : #{e}") - @redis.with do |redis| + @redis.with_metrics do |redis| request_id = redis.hget("vmpooler__vm__#{vm}", 'request_id') fail_pending_vm(vm, pool, timeout, redis, request_id=request_id) end @@ -94,10 +94,10 @@ module Vmpooler return if mutex.locked? mutex.synchronize do - @redis.with do |redis| + @redis.with_metrics do |redis| request_id = redis.hget("vmpooler__vm__#{vm}", 'request_id') if provider.vm_ready?(pool, vm) - move_pending_vm_to_ready(vm, pool, request_id, redis) + move_pending_vm_to_ready(vm, pool, redis, request_id) else fail_pending_vm(vm, pool, timeout, redis, request_id = request_id) end @@ -138,13 +138,8 @@ module Vmpooler false end - def move_pending_vm_to_ready(vm, pool, request_id, redis) + def move_pending_vm_to_ready(vm, pool, redis, request_id = nil) clone_time = redis.hget('vmpooler__vm__' + vm, 'clone') - # return false unless clone_time - # if clone_time == 'QUEUED' - # $logger.log('s', "Waiting for clone_time. Got 'QUEUED'.") - # return false - # end finish = format('%