Make `LoadBalancer` to re-use very similar connection
What does this MR do and why?
This allows to have two LBs configured, each of them using a different set of replicas, but the same primary connection to handle transaction visibility.
Challenges:
- this does not yet have any Feature Flag behavior
- this makes me a small thrills as it works only for
model.connection
, but if anything would domodel.retrieve_connection
this would cause a problems as a new connection would be open
Related to: #341451 (closed), based on !72694 (closed).
Validation
Requires decomposed CI configured in GDK/GCK.
- Disabled:
# GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci unset
$ bin/rails console
[4] pry(main)> Ci::InstanceVariable.last
...
Ci::InstanceVariable Load (14.9ms) SELECT "ci_instance_variables".* FROM "ci_instance_variables" ORDER BY "ci_instance_variables"."id" DESC LIMIT 1 # application:console,db_config_name:ci_replica,line:/data/cache/bundle-2.7.2/ruby/2.7.0/gems/marginalia-1.10.0/lib/marginalia/comment.rb:25:in `block in construct_comment'
=> db_config_name:ci_replica expected
[6] pry(main)> Ci::InstanceVariable.create!(key: 'aa', value: 'bb')
...
TRANSACTION (0.4ms) COMMIT # application:console,db_config_name:ci,line:/data/cache/bundle-2.7.2/ruby/2.7.0/gems/marginalia-1.10.0/lib/marginalia/comment.rb:25:in `block in construct_comment'
=> db_config_name:ci expected
- Enabled
export GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci=main
$ bin/rails console
[1] pry(main)> Ci::InstanceVariable.last
Ci::InstanceVariable Load (0.8ms) SELECT "ci_instance_variables".* FROM "ci_instance_variables" ORDER BY "ci_instance_variables"."id" DESC LIMIT 1 # application:console,db_config_name:ci_replica,line:/data/cache/bundle-2.7.2/ruby/2.7.0/gems/marginalia-1.10.0/lib/marginalia/comment.rb:25:in `block in construct_comment'
=> db_config_name:ci_replica expected
[3] pry(main)> Ci::InstanceVariable.create!(key: 'aa2', value: 'bb')
...
TRANSACTION (0.5ms) COMMIT # application:console,db_config_name:main,line:/data/cache/bundle-2.7.2/ruby/2.7.0/gems/marginalia-1.10.0/lib/marginalia/comment.rb:25:in `block in construct_comment'
=> db_config_name:main expected
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.
Edited by Kamil Trzciński (Back 2025-01-01)