Skip to content

Commit efe07fd

Browse files
Merge branch '7353-decomposed-db-ha-poc' into 'master'
Patroni and PgBouncer support for decomposed Rails database Closes #7353 See merge request https://gitlab.com/gitlab-org/omnibus-gitlab/-/merge_requests/7379 Merged-by: Balasankar 'Balu' C <[email protected]> Approved-by: Andrew Patterson <[email protected]> Approved-by: Balasankar 'Balu' C <[email protected]> Co-authored-by: Hossein Pursultani <[email protected]>
2 parents 9ad7415 + 4e26d00 commit efe07fd

File tree

9 files changed

+55
-38
lines changed

9 files changed

+55
-38
lines changed

files/gitlab-cookbooks/gitlab/libraries/helpers/gitlab_rails.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ def public_attributes
88
'gitlab' => {
99
'gitlab_rails' => node['gitlab']['gitlab_rails'].select do |key, value|
1010
%w(db_database).include?(key)
11-
end
11+
end.merge(
12+
'databases' => node['gitlab']['gitlab_rails']['databases'].transform_values do |value|
13+
value['db_database']
14+
end
15+
)
1216
}
1317
}
1418
end

files/gitlab-cookbooks/pgbouncer/recipes/user.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,18 @@
3333
end
3434

3535
if pgb_helper.create_pgbouncer_user?('postgresql') || pgb_helper.create_pgbouncer_user?('patroni')
36-
pgbouncer_user 'rails' do
37-
helper lazy { PgHelper.new(node) }
38-
user node['postgresql']['pgbouncer_user']
39-
password node['postgresql']['pgbouncer_user_password']
40-
database node['gitlab']['gitlab_rails']['db_database']
41-
add_auth_function default_auth_query.eql?(auth_query)
42-
action :create
36+
ignored_databases = %w[geo]
37+
database_host = node['gitlab']['gitlab_rails']['db_host']
38+
databases = node['gitlab']['gitlab_rails']['databases'].select { |db, details| details['enable'] && details['db_host'] == database_host && !ignored_databases.include?(db) }
39+
40+
databases.each do |db, settings|
41+
pgbouncer_user "rails:#{db}" do
42+
helper lazy { PgHelper.new(node) }
43+
user node['postgresql']['pgbouncer_user']
44+
password node['postgresql']['pgbouncer_user_password']
45+
database settings['db_database']
46+
add_auth_function default_auth_query.eql?(auth_query)
47+
action :create
48+
end
4349
end
4450
end

files/gitlab-cookbooks/pgbouncer/resources/pgbouncer_user.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@
1616
helper new_resource.helper
1717
password "md5#{new_resource.password}"
1818
action :create
19-
notifies :run, "execute[Add pgbouncer auth function]", :immediately
2019
end
2120

2221
pgbouncer_auth_function = new_resource.helper.pg_shadow_lookup
2322

2423
execute 'Add pgbouncer auth function' do
2524
command %(/opt/gitlab/bin/#{new_resource.helper.service_cmd} -d #{new_resource.database} -c '#{pgbouncer_auth_function}')
2625
user new_resource.account_helper.postgresql_user
27-
not_if { new_resource.helper.has_function?(new_resource.database, "pg_shadow_lookup") }
28-
only_if { new_resource.add_auth_function }
29-
action :nothing
26+
only_if { new_resource.add_auth_function && new_resource.helper.is_running? && new_resource.helper.is_ready? }
27+
not_if { new_resource.helper.is_offline_or_readonly? || new_resource.helper.has_function?(new_resource.database, "pg_shadow_lookup") }
28+
action :run
3029
end
3130
end

files/gitlab-ctl-commands-ee/lib/pgbouncer.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@ def initialize(options, install_path, base_data_path)
4040
end
4141

4242
def update_databases(original = {})
43+
rails_databases = attributes.dig('gitlab', 'gitlab_rails', 'databases')&.values
44+
rails_databases = rails_databases ? rails_databases.uniq : [DEFAULT_RAILS_DATABASE]
45+
4346
updated = {}
4447

45-
if original.empty?
46-
original = {
47-
database => {}
48-
}
49-
end
48+
original = rails_databases.to_h { |db| [db, {}] } if original.empty?
5049

5150
original.each do |db, settings|
5251
settings.delete('password')
@@ -55,11 +54,18 @@ def update_databases(original = {})
5554

5655
settings['auth_user'] = settings.delete('user') if settings.key?('user')
5756

58-
if db == @database
57+
is_rails_db = rails_databases.include?(db)
58+
59+
if db == @database || is_rails_db
5960
settings['host'] = options['newhost'] if options['newhost']
6061
settings['port'] = options['port'] if options['port']
6162
settings['auth_user'] = options['user'] if options['user']
62-
settings['dbname'] = options['pg_database'] if options['pg_database']
63+
64+
if is_rails_db
65+
settings['dbname'] = db
66+
elsif options['pg_database']
67+
settings['dbname'] = options['pg_database']
68+
end
6369
end
6470

6571
settings.each do |setting, value|

spec/chef/cookbooks/gitlab-ee/resources/pgbouncer_user_spec.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,10 @@
1414

1515
it 'should create the pg_shadow_lookup function' do
1616
postgresql_user = chef_run.postgresql_user('pgbouncer-geo')
17-
expect(postgresql_user).to notify('execute[Add pgbouncer auth function]')
17+
expect(postgresql_user.username).to eq('pgbouncer-geo')
1818
resource = chef_run.execute('Add pgbouncer auth function')
1919
expect(resource.command).to match(%r{^/opt/gitlab/bin/gitlab-geo-psql -d fakedb-geo})
2020
end
21-
22-
it 'should not try and recreate the function' do
23-
expect(chef_run).not_to run_execute('Add pgbouncer auth function')
24-
end
2521
end
2622

2723
context 'create rails' do
@@ -36,13 +32,9 @@
3632

3733
it 'should create the pg_shadow_lookup function' do
3834
postgresql_user = chef_run.postgresql_user('pgbouncer-rails')
39-
expect(postgresql_user).to notify('execute[Add pgbouncer auth function]')
35+
expect(postgresql_user.username).to eq('pgbouncer-rails')
4036
resource = chef_run.execute('Add pgbouncer auth function')
4137
expect(resource.command).to match(%r{^/opt/gitlab/bin/gitlab-psql -d fakedb-rails})
4238
end
43-
44-
it 'should not try and recreate the function' do
45-
expect(chef_run).not_to run_execute('Add pgbouncer auth function')
46-
end
4739
end
4840
end

spec/chef/cookbooks/patroni/recipes/patroni_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@
236236
expect(chef_run).not_to run_execute('/opt/gitlab/embedded/bin/initdb -D /var/opt/gitlab/postgresql/data -E UTF8')
237237
expect(chef_run).to create_postgresql_user('gitlab')
238238
expect(chef_run).to create_postgresql_user('gitlab_replicator')
239-
expect(chef_run).to create_pgbouncer_user('rails')
239+
expect(chef_run).to create_pgbouncer_user('rails:ci')
240+
expect(chef_run).to create_pgbouncer_user('rails:main')
240241
expect(chef_run).to create_postgresql_database('gitlabhq_production')
241242
expect(chef_run).to enable_postgresql_extension('pg_trgm')
242243
expect(chef_run).to enable_postgresql_extension('btree_gist')

spec/chef/cookbooks/pgbouncer/recipes/pgbouncer_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@
433433

434434
it 'should create the pgbouncer user on the database' do
435435
expect(chef_run).to include_recipe('pgbouncer::user')
436-
expect(chef_run).to create_pgbouncer_user('rails').with(
436+
expect(chef_run).to create_pgbouncer_user('rails:main').with(
437437
password: 'fakeuserpassword'
438438
)
439439
end

spec/chef/cookbooks/pgbouncer/recipes/pgbouncer_user_spec.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
end
3838

3939
it 'should call pgbouncer_user with the correct values for rails' do
40-
expect(chef_run).to create_pgbouncer_user('rails').with(
40+
expect(chef_run).to create_pgbouncer_user('rails:main').with(
4141
database: 'gitlabhq_production',
4242
password: 'fakepassword',
4343
user: 'pgbouncer-rails',
@@ -64,7 +64,10 @@
6464
end
6565

6666
it 'should not create the pg_shadow_lookup function' do
67-
expect(chef_run).to create_pgbouncer_user('rails').with(
67+
expect(chef_run).to create_pgbouncer_user('rails:main').with(
68+
add_auth_function: false
69+
)
70+
expect(chef_run).to create_pgbouncer_user('rails:ci').with(
6871
add_auth_function: false
6972
)
7073
end
@@ -85,7 +88,8 @@
8588
end
8689

8790
it 'should not create the pgbouncer user' do
88-
expect(chef_run).not_to create_pgbouncer_user('rails')
91+
expect(chef_run).not_to create_pgbouncer_user('rails:main')
92+
expect(chef_run).not_to create_pgbouncer_user('rails:ci')
8993
expect(chef_run).not_to create_pgbouncer_user('geo')
9094
end
9195
end
@@ -112,7 +116,8 @@
112116
user: 'pgbouncer-geo',
113117
add_auth_function: true
114118
)
115-
expect(chef_run).not_to create_pgbouncer_user('rails')
119+
expect(chef_run).not_to create_pgbouncer_user('rails:main')
120+
expect(chef_run).not_to create_pgbouncer_user('rails:ci')
116121
end
117122
end
118123
end

spec/chef/gitlab-ctl-commands-ee/lib/pgbouncer_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
let(:fake_ohai) do
99
{
1010
'gitlab' => {
11-
'gitlab-rails' => {
12-
'db_database' => 'fake_database'
11+
'gitlab_rails' => {
12+
'db_database' => 'fake_database',
13+
'databases' => {
14+
'main' => 'fake_database',
15+
'ci' => 'fake_database_ci'
16+
},
1317
},
1418
},
1519
'pgbouncer' => {
@@ -94,7 +98,7 @@
9498

9599
it 'should generate an databases.ini with the default rails database' do
96100
expect(@obj.render).to eq(
97-
"[databases]\n\nfake_database = \n\n"
101+
"[databases]\n\nfake_database = dbname=fake_database\n\nfake_database_ci = dbname=fake_database_ci\n\n"
98102
)
99103
end
100104
end

0 commit comments

Comments
 (0)