(POOLER-52) Add recovery to vSphere connections

The generic connection pooler is only responsible for managing the connection
objects, however the providers themselves are responsible for ensuring that the
connection is alive/healthy etc.  Previously, the older vSphere helper would
reconnect however this was lost when the connection pooler was introduced.  This
commit adds a method that checks the connection before use, and then reconnects
if the connection is in a bad state.
This commit is contained in:
Glenn Sarti 2017-05-12 14:54:30 -07:00
parent df783f0ed0
commit 85b0f035aa
3 changed files with 148 additions and 12 deletions

View file

@ -16,6 +16,47 @@ describe 'GenericConnectionPool' do
) { connection_object }
}
describe "When consuming a pool object" do
let(:pool_size) { 1 }
let(:pool_timeout) { 1 }
let(:connection_object) {{
connection: 'connection'
}}
it 'should return a connection object when grabbing one from the pool' do
subject.with_metrics do |conn_pool_object|
expect(conn_pool_object).to be(connection_object)
end
end
it 'should return the same connection object when calling the pool multiple times' do
subject.with_metrics do |conn_pool_object|
expect(conn_pool_object).to be(connection_object)
end
subject.with_metrics do |conn_pool_object|
expect(conn_pool_object).to be(connection_object)
end
subject.with_metrics do |conn_pool_object|
expect(conn_pool_object).to be(connection_object)
end
end
it 'should preserve connection state across mulitple pool calls' do
new_connection = 'new_connection'
# Ensure the connection is not modified
subject.with_metrics do |conn_pool_object|
expect(conn_pool_object).to be(connection_object)
expect(conn_pool_object[:connection]).to_not eq(new_connection)
# Change the connection
conn_pool_object[:connection] = new_connection
end
# Ensure the connection is modified
subject.with_metrics do |conn_pool_object|
expect(conn_pool_object).to be(connection_object)
expect(conn_pool_object[:connection]).to eq(new_connection)
end
end
end
describe "#with_metrics" do
before(:each) do

View file

@ -75,6 +75,10 @@ EOT
subject { Vmpooler::PoolManager::Provider::VSphere.new(config, logger, metrics, 'vsphere', provider_options) }
before(:each) do
allow(subject).to receive(:vsphere_connection_ok?).and_return(true)
end
describe '#name' do
it 'should be vsphere' do
expect(subject.name).to eq('vsphere')
@ -878,6 +882,69 @@ EOT
end
# vSphere helper methods
describe '#ensured_vsphere_connection' do
let(:config) { YAML.load(<<-EOT
---
:config:
:providers:
:vsphere:
# Drop the connection pool timeout way down for spec tests so they fail fast
connection_pool_timeout: 1
connection_pool_size: 1
:pools:
EOT
)
}
let(:connection1) { mock_RbVmomi_VIM_Connection(connection_options) }
let(:connection2) { mock_RbVmomi_VIM_Connection(connection_options) }
before(:each) do
allow(subject).to receive(:connect_to_vsphere).and_return(connection1)
end
# This is to ensure that the pool_size of 1 is in effect
it 'should return the same connection object when calling the pool multiple times' do
subject.connection_pool.with_metrics do |pool_object|
expect(pool_object[:connection]).to be(connection1)
end
subject.connection_pool.with_metrics do |pool_object|
expect(pool_object[:connection]).to be(connection1)
end
subject.connection_pool.with_metrics do |pool_object|
expect(pool_object[:connection]).to be(connection1)
end
end
context 'when the connection breaks' do
before(:each) do
# Emulate the connection state being good, then bad, then good again
expect(subject).to receive(:vsphere_connection_ok?).and_return(true, false, true)
expect(subject).to receive(:connect_to_vsphere).and_return(connection1, connection2)
end
it 'should restore the connection' do
subject.connection_pool.with_metrics do |pool_object|
# This line needs to be added to all instances of the connection_pool allocation
connection = subject.ensured_vsphere_connection(pool_object)
expect(connection).to be(connection1)
end
subject.connection_pool.with_metrics do |pool_object|
connection = subject.ensured_vsphere_connection(pool_object)
# The second connection would have failed. This test ensures that a
# new connection object was created.
expect(connection).to be(connection2)
end
subject.connection_pool.with_metrics do |pool_object|
connection = subject.ensured_vsphere_connection(pool_object)
expect(connection).to be(connection2)
end
end
end
end
describe '#connect_to_vsphere' do
before(:each) do
allow(RbVmomi::VIM).to receive(:connect).and_return(connection)