Skip to content

Refactor responsibilities for LockWritesManager / lock_writes rake

Rutger Wessels requested to merge 384905-less-responsibility-rake-task into master

What does this MR do and why?

We have a rake task that will set a write lock (trigger) on tables that should not be updated after a Gitlab instance is running on two databases. For example, users table in ci database will be locked for writes and ci_pipelines table in main as well.

This rake tasks will be needed as a in-between step when moving a Gitlab instance to decomposed database setup (two databases: main and ci).

This rake task does two things:

  • Create the list of databases and tables that need this write lock
  • Call Gitlab::Database::LockWritesManager for each of these tables.

That is too much responsibilty for the rake task.

So this MR will introduce a new class Gitlab::Database::TablesLocker (inspired by similar Gitlab::Database::TablesTruncate). Resulting in these changes:

  • The rake task is now only responsible for creating and executing Gitlab::Database::TablesLocker
  • The Gitlab::Database::TablesLocker will now create the list of tables and call the Gitlab::Database::LockWritesManager
  • Specs have been moved accordingly

How to set up and validate locally

  • Ensure there are no table locks in place (rake gitlab:db:unlock_writes)
  • Using psql console (gdk psql), insert some fake data in a ci table and in a main table. Both should work (no tables locked)
    • insert into ci_namespace_mirrors (namespace_id, traversal_ids) values (2000, ARRAY[1]);
    • insert into shards (name) values ('some_shard');
  • Do the same for 'ci' database: gdk psql -d gitlabhq_development_ci
  • Now run rake gitlab:db:lock_writes
  • And now try to insert data in both databases
    • insert into ci_namespace_mirrors (namespace_id, traversal_ids) values (2001, ARRAY[1]); (fail in main db, works in ci db)
    • insert into shards (name) values ('some_other_shard'); (fail in ci db, works in main db)

MR acceptance checklist

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

Related to #384905 (closed)

Edited by Rutger Wessels

Merge request reports