Database support
This document provides details and examples on how to implement database support for a Linux package component. The architecture blueprint provides the design and definitions.
Level 1
- Add the new database-related configuration attributes to
gitlab.rb. Do not forget to updategitlab.rb.template. - Update the Chef recipe to consume the configuration attributes. At this level, the requirement is to pass down the attributes to the component, generally its through configuration files or command-line arguments.
For example in registry cookbook:
registry['database']attribute is added togitlab.rb(seeattributes/default.rb).- The configuration template uses the attribute to configure registry (see
templates/default/registry-config.yml.erb).
Level 2
Add dependency to
postgresqlandpgbouncercookbooks. Usedependsinmetadata.rb. This ensures that requirements are met and their Chef custom resources are available to the cookbook.Create a
database_objectscustom resource inresources/directory of the cookbook with the defaultnothingaction (a no-op action) and acreateaction. Thecreateaction can leverage the existingpostgresqlcustom resources to set up the required database objects for the component.See:
postgresql_userpostgresql_databasepostgresql_schemapostgresql_extensionpostgresql_query
A
database_objectsresource of must create all of the required database objects for a component. It must not assume that another cookbook creates some of the objects that it needs.Create a
database_migrationscustom resource inresources/directory of the cookbook with the defaultnothingaction (a no-op action) and arunaction. Therunaction executes the commands for database migrations of the component.The migrations depend on all of the required database objects, which the
createaction of thedatabase_objectsresource provides.Create a
database_objectsrecipe in the cookbook that uses thedatabase_objectsresource, and include it from the database service cookbooks. Do not create database objects from the component’s ownenableordefaultrecipe. Include the recipe from both:postgresql/recipes/standalone.rb, for standalone PostgreSQL nodes.patroni/recipes/enable.rb, for Patroni nodes. Patroni nodes skippostgresql::standalonebecause PostgreSQL configuration is delegated to Patroni, so an include instandalone.rbalone does not run on them.
Only the PostgreSQL node has access to the
postgresqlsuperuser that can create users, databases, schemas, and extensions. When the component runs on a different node than PostgreSQL, it can’t create its database objects. Gate theinclude_recipewith apostgresqlattribute (for example,postgresql['registry']['auto_create']) so that administrators can opt out.Create a
database_migrationsrecipe in the cookbook that uses thedatabase_migrationsresource, and include it from the component’senablerecipe. Migrations run on the component node, and connect to the database with the component’s own credentials. Guard the resource so that it runs only when the database is enabled and reachable, because the database objects might not exist yet. For example, when PostgreSQL runs on another node that has not been reconfigured yet.
If the component only needs a PostgreSQL role, database, and extensions on
the GitLab-managed PostgreSQL cluster, consider the
postgresql['component_databases'] framework
instead of a custom database_objects resource. It creates the database
objects on both standalone PostgreSQL and Patroni nodes without
component-specific code in the postgresql or patroni cookbooks.
Examples
All of the following code blocks are provided as examples. You may need to make adjustments to ensure that they meet your requirements.
Example 1: Registry database objects
The following example shows a database_objects resource in the registry
cookbook defined in registry/resources/database_objects.rb.
Notice how it uses custom resources from the postgresql cookbook to create
the required database objects.
# registry/resources/database_objects.rb
unified_mode true
property :pg_helper, [GeoPgHelper, PgHelper], required: true, sensitive: true
default_action :nothing
action :nothing do
end
action :create do
host = node['registry']['database']['host']
port = node['registry']['database']['port']
database_name = node['registry']['database']['database_name']
username = node['registry']['database']['username']
password = node['registry']['database']['password']
postgresql_user username do
password "md5#{password}" unless password.nil?
action :create
end
postgresql_database database_name do
database_socket host
database_port port
owner username
helper new_resource.pg_helper
action :create
end
postgresql_extension 'btree_gist' do
database database_name
action :enable
end
endExample 2: Registry database migrations
The following example shows a database_migrations resource in the registry
cookbook defined in registry/resources/database_objects.rb.
Notice how the resource accepts additional parameters. Parameters help
support different migration scenarios, for example separation of pre-deployment
or post-deployment migrations. It also uses MigrationHelper
to decide whether to run a migration or not.
# registry/resources/database_migrations.rb
unified_mode true
property :name, name_property: true
property :direction, Symbol, default: :up
property :dry_run, [true, false], default: false
property :force, [true, false], default: false
property :limit, [Integer, nil], default: nil
property :skip_post_deployment, [true, false], default: false
default_action :nothing
action :nothing do
end
action :run do
# MigrationHelper is not implemented. It contains general-purpose helper
# methods for managing migrations, for example if a specific component
# migrations can run or not.
#
# See: "Helper class for general database migration requirements"
migration_helper = MigrationHelper.new(node)
account_helper = AccountHelper.new(node)
logfiles_helper = LogfilesHelper.new(node)
logging_settings = logfiles_helper.logging_settings('registry')
bash_hide_env "migrate registry database: #{new_resource.name}" do
code <<-EOH
set -e
LOG_FILE="#{logging_settings[:log_directory]}/db-migrations-$(date +%Y-%m-%d-%H-%M-%S).log"
umask 077
/opt/gitlab/embedded/bin/registry \
#{new_resource.direction} \
#{"--dry-run" if new_resource.dry_run} \
#{"--limit #{new_resource.limit}" unless new_resource.limit.nil?} \
... \
#{working_dir}/config.yml \
2>& 1 | tee ${LOG_FILE}
STATUS=${PIPESTATUS[0]}
chown #{account_helper.gitlab_user}:#{account_helper.gitlab_group} ${LOG_FILE}
exit $STATUS
EOH
not_if { migration_helper.run_migration?('registry') }
end
endExample 3: Use database objects and migrations of Registry
The resources defined in the previous examples are used in two separate
recipes of the registry cookbook, because they run on different nodes.
The registry/recipes/database_objects.rb recipe creates the database objects.
It runs on the PostgreSQL node only:
# registry/recipes/database_objects.rb
# CAUTION: This recipe is only to be used by the PostgreSQL node
# because it's the only node which has access to the `postgresql`
# user with permission to create database objects.
pg_helper = PgHelper.new(node)
registry_database_objects 'default' do
pg_helper pg_helper
action :create
only_if { node.dig('postgresql', 'enable') }
not_if { pg_helper.replica? }
endThe postgresql cookbook includes this recipe, gated by the
postgresql['registry']['auto_create'] attribute:
# postgresql/recipes/standalone.rb
# ...
include_recipe 'registry::database_objects' if node.dig('postgresql', 'registry', 'auto_create')The registry cookbook does not yet follow the Patroni part of Level 2:
patroni/recipes/enable.rb does not include registry::database_objects.
On Patroni nodes, the registry database objects are not created
automatically and must be created manually. New components must include
their database_objects recipe from patroni/recipes/enable.rb as well.
The registry/recipes/database_migrations.rb recipe runs the migrations. The
registry/recipes/enable.rb recipe includes it, so it runs on the node where
the registry runs:
# registry/recipes/database_migrations.rb
registry_helper = RegistryHelper.new(node)
registry_database_migrations 'registry' do
action :run
only_if { registry_helper.should_run_migrations? }
end# registry/recipes/enable.rb
# ...
include_recipe 'registry::database_migrations'RegistryHelper#should_run_migrations? checks that the registry database is
enabled, that registry['auto_migrate'] is set, and that the database is
reachable. Because the two recipes can run on different nodes, migrations do
not rely on a notifies from the database objects resource. Instead, they
run on any reconfigure of the registry node after the database objects exist.
Example 4: Parameterized database objects resource for Rails
The following example shows how a single implementation of the database objects for Rails application can satisfy the requirements of the decomposed database model.
In this example the logical database is passed as the resource name and is
used to lookup settings of each database from the configuration. The settings
are passed to postgresql custom resources. This is particularly useful when
the majority of implementation of can be reused to replace two or more resources.
# gitlab/resources/database_objects.rb
unified_mode true
property :pg_helper, [GeoPgHelper, PgHelper], required: true, sensitive: true
default_action :nothing
action :nothing do
end
action :create do
global_database_settings = {
# ...
port: node['postgresql']['port']
host: node['gitlab']['gitlab_rails']['db_host']
# ...
}
database_settings = node['gitlab']['gitlab_rails']['databases'][new_resource.resource_name]
database_settings = global_database_settings.merge(database_settings) unless database_settings.nil?
username = database_settings[:username]
password = database_settings[:password]
database_name = database_settings[:database_name]
host = database_settings[:host]
port = database_settings[:port]
postgresql_user username do
password "md5#{password}" unless password.nil?
action :create
end
postgresql_database database_name do
database_socket host
database_port port
owner username
helper new_resource.pg_helper
action :create
end
postgresql_extension 'btree_gist' do
database database_name
action :enable
end
endAnd this is how it is used in gitlab/recipes/default.rb:
gitlabe_database_objects 'main' do
pg_helper pg_helper
action :create
only_if { node['gitlab']['gitlab_rails']['databases']['main']['enable'] ... }
end
gitlabe_database_objects 'ci' do
pg_helper pg_helper
action :create
only_if { node['gitlab']['gitlab_rails']['databases']['ci']['enable'] ... }
endLevel 3
Add a new attribute for PgBouncer user. Make sure that this attribute is mapped to the existing
pgbouncer['databases']setting and can consume it. This attribute is used to create a dedicated PgBouncer user for the component as opposed to reusing the existing Rails user, the same as what Praefect currently does.It is very important that we do not introduce any breaking changes to
gitlab.rb. The current user settings must work without any change.Use
pgbouncer_usercustom resource frompgbouncercookbook to create the dedicated PgBouncer user for the component. Use the attribute that is described in the previous step.
Level 4
Add a new attribute for the component to specify the name of the Consul service of the database cluster. This is either the name of the scope of the Patroni cluster (when automatic service registry for Patroni, i.e.
patroni['register_service'], is enabled) or the name of the Consul service that is configured manually without the Linux package.Use
database_watchcustom resource(Needs Implementation) to define a new Consul watch for the database cluster service. It notifies PgBouncer to update the logical database endpoint when the leader of the cluster changes. Pass the name of the Consul service, logical database, and any other PgBouncer options as parameters to the watch.
All database_watch resources must be placed in the consul cookbook. As
opposed to the previous levels, this is the only place where database-related
resources are concentrated in one cookbook, consul, and not managed in
the same cookbooks as their associated components.
The reason for this exception is that the watches run on the PgBouncer nodes,
where pgbouncer_role is used. All components, except PgBouncer and Consul,
are disabled. Note that this is in line with existing user configuration since
it is the recommended configuration for PgBouncer node.
We don’t want to introduce any breaking changes into gitlab.rb.
Considerations
- No other resource should be involved with database setup.
- All custom resources must be idempotent. For example they must not fail when an object already exist even though they are created or ran in another cookbook. Instead they must be able to update the current state of the database objects, configuration, or migrations based on the new user inputs.
- In HA mode, given that multiple physical nodes are involved, the Linux package may encounter certain limitations to provide full automation of the configuration. This is an acceptable limitation.
Bridge the gap
Currently not all of the custom resources or helper classes are available. Even if they are, they may require some adjustments to meet the specified requirements. Here are some examples.
Reorganize the existing database operations
This model requires some adjustments in postgresql, patroni, and gitlab
cookbooks. For example, the database_objects resource that is defined in the
gitlab cookbook is used directly in the postgresql and patroni recipes.
It should move to a database_objects recipe in the gitlab cookbook that
the postgresql and patroni cookbooks include. The registry cookbook
follows this pattern for the postgresql cookbook, but not yet for patroni.
The database service cookbooks (postgresql and patroni) should not deal with
database migrations and must delegate them to the application cookbooks (for
example, gitlab, registry, and praefect). However, custom resources of
postgresql cookbook assume they run on the PostgreSQL node and use the UNIX
socket to connect to the database server. For this reason, the creation of
database objects must be triggered from the postgresql cookbook, which
includes the database_objects recipe of each application cookbook. See
Example 3.
The same is true for pgbouncer cookbook. Currently the only PgBouncer user is
created in the users recipe of this cookbook. This can change as well to allow
each component cookbook to create its own PgBouncer users.
Support dedicated PgBouncer user for databases
The current pgbouncer cookbook mostly supports multiple databases.
The pgbouncer cookbook only creates PgBouncer users for the main Rails database. This
is why non-Rails applications connect with the same PgBouncer user created for Rails.
We can currently set up PgBouncer support for decomposed Rails databases sharing the same user. But for Praefect or Registry, we need additional work to create dedicated PgBouncer users.
A shared user does not mean connection settings for each database must be the same. It only means that multiple databases use the same user for PgBouncer connection.
Delay the population of PgBouncer database configuration
The implementation of gitlab-ctl pgb-notify supports multiple
databases. It is generic enough that, as long as the PgBouncer users are created,
it can update databases.ini from the databases.json file.
However, when PgBouncer users are pulled into individual cookbooks, the initial
databases.ini that is created or updated in gitlab-ctl reconfigure may not
be valid because it references PgBouncer users that are not created yet.
We should be able to fix this by delaying the action on Chef resource that calls
gitlab-ctl pgb-notify.
Configurable Consul watch for databases
Consul cluster can be shared between multiple Patroni clusters (using different
scopes, such as patroni['scope']), but updating PgBouncer configuration is still
problematic because the Consul watch scripts are not fully configurable.
The current implementation has several limitations:
The Linux package uses
postgresqlservice that is explicitly defined inconsulcookbook. This service, that is currently being used to notify PgBouncer, is a leftover of the transition from RepMgr to Patroni. It must be replaced with the Consul service that Patroni registers. Whenpatroni['register_service']is enabled Patroni registers a Consul service withpatroni['scope']parameter and the tagmaster,primary,replica, orstandby-leaderdepending on the node’s role.The current failover script is associated to a Consul watch for
postgresqlservice and is not capable of handling multiple databases because database name can not be changed.
We need to extend the current Linux package capability to use Consul watches to track Patroni services, find cluster leaders, and notify PgBouncer with a parameterized failover script.
In order to do this we implement database_watch custom resource in consul
cookbook. It defines a database-specific Consul watch for database a cluster
service and passes the required information to a parameterized failover script
to notify PgBouncer. The key attributes of this resource are:
The service name, that specifies which database cluster must be watched. It could be the scope of the Patroni cluster when
patroni['register_service']is enabled or a Consul service name when it is manually configured.The database name that specifies which logical databases should be reconfigured when the database cluster leader changes.
Helper class for general database migration requirements
MigrationHelper(Needs implementation) implements general
requirements of database migrations, including the central switch for enabling
or disabling auto-migrations. It can also provides the mapping between the
existing and new configuration attributes.