Add database slave connection support

- Add new attributes for database slave connection
- Change db_uri method to generate slave connection db_uri

Implements: blueprint sql-slave-connection-support
Change-Id: I9996e4944e58264b5195d625c98adaf28a04acd7
This commit is contained in:
Min Min Ren 2015-07-09 02:04:39 -05:00
parent f6ab635369
commit 375cdcde14
3 changed files with 57 additions and 3 deletions

View File

@ -76,6 +76,9 @@ default['openstack']['endpoints']['db']['scheme'] = nil
default['openstack']['endpoints']['db']['port'] = '3306'
default['openstack']['endpoints']['db']['path'] = nil
default['openstack']['endpoints']['db']['bind_interface'] = nil
default['openstack']['endpoints']['db']['enabled_slave'] = false
default['openstack']['endpoints']['db']['slave_host'] = '127.0.0.1'
default['openstack']['endpoints']['db']['slave_port'] = '3316'
# Default database attributes
default['openstack']['db']['server_role'] = 'os-ops-database'
@ -141,6 +144,9 @@ node['openstack']['common']['services'].each do |service, project|
default['openstack']['db'][service]['username'] = project
default['openstack']['db'][service]['options'] = node['openstack']['db']['options']
default['openstack']['db'][service]['slave_host'] = node['openstack']['endpoints']['db']['slave_host']
default['openstack']['db'][service]['slave_port'] = node['openstack']['endpoints']['db']['slave_port']
case service
when 'dashboard'
default['openstack']['db'][service]['migrate'] = true

View File

@ -52,11 +52,17 @@ module ::Openstack
end
# Shortcut to get the SQLAlchemy DB URI for a named service
def db_uri(service, user, pass) # rubocop:disable MethodLength, CyclomaticComplexity
def db_uri(service, user, pass, is_slave = false) # rubocop:disable MethodLength, CyclomaticComplexity
info = db(service)
return unless info
host = info['host']
port = info['port'].to_s
if is_slave
host = info['slave_host']
port = info['slave_port'].to_s
else
host = info['host']
port = info['port'].to_s
end
type = info['service_type']
name = info['db_name']
options = info['options'][type]

View File

@ -403,6 +403,48 @@ describe 'openstack-common::set_endpoints_by_interface' do
).to eq(expected)
end
end
it 'returns compute slave db info hash when service found for default mysql' do
node.set['openstack']['endpoints']['db']['enabled_slave'] = true
allow(subject).to receive(:node).and_return(chef_run.node)
expected = 'mysql://user:pass@127.0.0.1:3316/nova?charset=utf8'
expect(
subject.db_uri('compute', 'user', 'pass', true)
).to eq(expected)
end
it 'returns block-storage slave db info hash when service found for db2 with options' do
node.set['openstack']['endpoints']['db']['enabled_slave'] = true
node.set['openstack']['db']['service_type'] = 'db2'
node.set['openstack']['db']['options'] = { 'db2' => '?options' }
allow(subject).to receive(:node).and_return(chef_run.node)
expected = 'ibm_db_sa://user:pass@127.0.0.1:3316/cinder?options'
expect(
subject.db_uri('block-storage', 'user', 'pass', true)
).to eq(expected)
end
it 'returns image slave db info hash when service found for mariadb' do
node.set['openstack']['db']['service_type'] = 'mariadb'
node.set['openstack']['endpoints']['db']['enabled_slave'] = true
allow(subject).to receive(:node).and_return(chef_run.node)
expected = 'mysql://user:pass@127.0.0.1:3316/glance?charset=utf8'
expect(
subject.db_uri('image', 'user', 'pass', true)
).to eq(expected)
end
%w(galera percona-cluster).each do |db|
it "returns network slave db info hash when service found for #{db}" do
node.set['openstack']['db']['service_type'] = db
node.set['openstack']['endpoints']['db']['enabled_slave'] = true
allow(subject).to receive(:node).and_return(chef_run.node)
expected = 'mysql://user:pass@127.0.0.1:3316/neutron?charset=utf8'
expect(
subject.db_uri('network', 'user', 'pass', true)
).to eq(expected)
end
end
end
describe '#address' do