Skip to content

Multi DB support for loose foreign keys

Adam Hegyi requested to merge 341513-multi-db-support-for-lfk into master

What does this MR do and why?

This change adjusts the LooseForeignKeys::CleanupWorker to work with multiple databases. To keep the delay minimal, we switched the scheduling to 1 minute (it was 5 minutes).

When CI and Main databases are configured:

  • In even minutes, run the worker for LooseForeignKeys::DeletedRecord records in the Main database.
  • In odd minutes, run the worker for LooseForeignKeys::DeletedRecord records in the CI database.

When only one database (Main) is configured

  • Invoke the worker every minute.

Additionally, we add the connection name to the extra metadata which will be logged in kibana.

How to test it:

  1. Setup two databases (2nd one can be PG.ai).
  2. Run rails console: GITLAB_USE_MODEL_LOAD_BALANCING=true rails c
  3. Verify which DB is picked (changes every minute): LooseForeignKeys::CleanupWorker.new.send(:connection_with_name)
  4. Run the worker: LooseForeignKeys::CleanupWorker.new.perform

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 #341513 (closed)

Edited by Adam Hegyi

Merge request reports