Skip to content

Reconfigure DB load balancing connection on code reload

What does this MR do and why?

In development, when code is reloaded, we lose the connection class attribute. So we need to setup a to_prepare block to reconfigure DB load balancing.

Also add logging for when DB load balancing setup runs.

Note: whether code reloads depends on config.cache_classes, and config.reload_classes_only_on_change. In production, config.cache_classes is true, so no code reloading happens there.

Changelog: fixed

Related issue: #356349 (closed)

To replicate:

  1. Setup multiple databases
  2. Run the following in the Rails console:
[1] pry(main)> Ci::Pipeline.count
   (2.0ms)  SELECT COUNT(*) FROM "ci_pipelines" /*application:console,db_config_name:ci,line:(pry):1:in `__pry__'*/
=> 263
[2] pry(main)> reload!
Reloading...
[3] pry(main)> Ci::Pipeline.count
   (1.0ms)  SELECT COUNT(*) FROM "ci_pipelines" /*application:console,db_config_name:ci,line:(pry):3:in `__pry__'*/
=> 263

Before this fix, the db_config_name will change to main.

An alternative replication method is #356353 (comment 902255686)

Screenshots or screen recordings

These are strongly recommended to assist reviewers and reduce the time to merge your change.

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

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 Thong Kuah

Merge request reports