From d95e486c8cd4942fc0f54c74b0f9e8c61f387780 Mon Sep 17 00:00:00 2001 From: Samuel Beaulieu Date: Wed, 30 May 2018 16:54:43 -0500 Subject: [PATCH] Fixing method remove_excess_vms Before this fix, if the pool had an excessive number of 'pending' vms, but was not full of 'ready' vms, we would end up clearing all the 'pending' vms regardless, which could bring us back under the max pool size. This fix makes sure we only remove the excess, see unit test for an example --- lib/vmpooler/pool_manager.rb | 4 +++- spec/unit/pool_manager_spec.rb | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/vmpooler/pool_manager.rb b/lib/vmpooler/pool_manager.rb index 4bb95da..9f4e7d7 100644 --- a/lib/vmpooler/pool_manager.rb +++ b/lib/vmpooler/pool_manager.rb @@ -631,8 +631,10 @@ module Vmpooler move_vm_queue(pool['name'], next_vm, 'ready', 'completed') end if total > ready - $redis.smembers("vmpooler__pending__#{pool['name']}").each do |vm| + max_to_remove = total - pool['size'] + $redis.smembers("vmpooler__pending__#{pool['name']}").each_with_index do |vm, i| move_vm_queue(pool['name'], vm, 'pending', 'completed') + if max_to_remove == (i+1) then break end end end end diff --git a/spec/unit/pool_manager_spec.rb b/spec/unit/pool_manager_spec.rb index a93207a..0d5a2ed 100644 --- a/spec/unit/pool_manager_spec.rb +++ b/spec/unit/pool_manager_spec.rb @@ -1669,7 +1669,18 @@ EOT subject.remove_excess_vms(config[:pools][0], provider, 2, 5) end + + it 'should remove excess pending vms, but only the excess' do + create_pending_vm(pool,'vm1') + create_pending_vm(pool,'vm2') + create_pending_vm(pool,'vm3') + create_pending_vm(pool,'vm4') + expect(subject).to receive(:move_vm_queue).exactly(3).times + + subject.remove_excess_vms(config[:pools][0], provider, 1, 5) + end end + end describe 'prepare_template' do