Skip to content

Update Gitlab::Database::Count to work with decomposed database

Krasimir Angelov requested to merge 345279-decomposed-db-count into master

What does this MR do and why?

When counting records with Gitlab::Database::Count split models by database so that we can wrap usage of PgClass in Gitlab::Database::SharedModel.using_connection using the correct connection.

Also use model.connection instead of ActiveRecord::Base.connection.

Related to #345279 (closed).

How to set up and validate locally

  1. Setup your environment for multiple databases as described in https://docs.gitlab.com/ee/development/database/multiple_databases.html#configdatabaseyml.
  2. Load the schema, e.g. gdk psql -d my_ci_database < db/structure.sql.
  3. Connect to the ci database - gdk psql -d my_ci_database.
  4. Create some ci_instance_variables records:
    insert into ci_instance_variables values (1, 1, false, false, 'TEST_DECOMPOSED_1', null, null), (2, 1, false, false, 'TEST_DECOMPOSED_2', null, null);
  5. Analyze ci_instance_variables - analyze ci_instance_variables;
    1. @alipniagov : also run similar analyze on your primary DB, so pg_class would have data.
  6. While on master open Rails console with GITLAB_USE_MODEL_LOAD_BALANCING=true bundle exec rails console
  7. Check counts with Gitlab::Database::Count.approximate_counts([Ci::InstanceVariable], strategies: [Gitlab::Database::Count::ReltuplesCountStrategy]), you should see 0 as a count.
  8. Switch to this branch - git checkout 345279-decomposed-db-count
  9. open Rails console with GITLAB_USE_MODEL_LOAD_BALANCING=true bundle exec rails console
  10. Check counts with Gitlab::Database::Count.approximate_counts([Ci::InstanceVariable], strategies: [Gitlab::Database::Count::ReltuplesCountStrategy]), this time you should see 2 as a count.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Aleksei Lipniagov

Merge request reports