Commit 773de4a3 authored by Kamil Trzciński's avatar Kamil Trzciński

Add `connection_specification_name/db_config` to `LB::Configuration`

This is in preparation to allow asynchronous handling of replicas
and re-use of primary connection in a load balancing context.
parent f83a51fc
...@@ -5,9 +5,11 @@ module Gitlab ...@@ -5,9 +5,11 @@ module Gitlab
module LoadBalancing module LoadBalancing
# Configuration settings for a single LoadBalancer instance. # Configuration settings for a single LoadBalancer instance.
class Configuration class Configuration
attr_reader :connection_specification_name, :connection_db_config
attr_accessor :hosts, :max_replication_difference, attr_accessor :hosts, :max_replication_difference,
:max_replication_lag_time, :replica_check_interval, :max_replication_lag_time, :replica_check_interval,
:service_discovery, :model :service_discovery
# Creates a configuration object for the given ActiveRecord model. # Creates a configuration object for the given ActiveRecord model.
def self.for_model(model) def self.for_model(model)
...@@ -45,6 +47,8 @@ module Gitlab ...@@ -45,6 +47,8 @@ module Gitlab
end end
def initialize(model, hosts = []) def initialize(model, hosts = [])
@connection_specification_name = model.connection_specification_name
@connection_db_config = model.connection_db_config
@max_replication_difference = 8.megabytes @max_replication_difference = 8.megabytes
@max_replication_lag_time = 60.0 @max_replication_lag_time = 60.0
@replica_check_interval = 60.0 @replica_check_interval = 60.0
......
...@@ -27,7 +27,7 @@ module Gitlab ...@@ -27,7 +27,7 @@ module Gitlab
HostList.new(configuration.hosts.map { |addr| Host.new(addr, self) }) HostList.new(configuration.hosts.map { |addr| Host.new(addr, self) })
end end
@name = @configuration.model.connection_db_config.name.to_sym @name = @configuration.connection_db_config.name.to_sym
end end
def primary_only? def primary_only?
...@@ -252,7 +252,7 @@ module Gitlab ...@@ -252,7 +252,7 @@ module Gitlab
# leverage that. # leverage that.
def pool def pool
ActiveRecord::Base.connection_handler.retrieve_connection_pool( ActiveRecord::Base.connection_handler.retrieve_connection_pool(
@configuration.model.connection_specification_name, @configuration.connection_specification_name,
role: ActiveRecord::Base.writing_role, role: ActiveRecord::Base.writing_role,
shard: ActiveRecord::Base.default_shard shard: ActiveRecord::Base.default_shard
) || raise(::ActiveRecord::ConnectionNotEstablished) ) || raise(::ActiveRecord::ConnectionNotEstablished)
......
...@@ -42,7 +42,7 @@ module Gitlab ...@@ -42,7 +42,7 @@ module Gitlab
@model.class_attribute(:sticking) @model.class_attribute(:sticking)
@model.connection = ConnectionProxy.new(lb) @model.connection = ConnectionProxy.new(lb)
@model.sticking = Sticking.new(lb) @model.sticking = Sticking.new(lb, @model)
end end
def setup_service_discovery def setup_service_discovery
......
...@@ -10,9 +10,9 @@ module Gitlab ...@@ -10,9 +10,9 @@ module Gitlab
# the primary. # the primary.
EXPIRATION = 30 EXPIRATION = 30
def initialize(load_balancer) def initialize(load_balancer, model)
@load_balancer = load_balancer @load_balancer = load_balancer
@model = load_balancer.configuration.model @model = model
end end
# Unsticks or continues sticking the current request. # Unsticks or continues sticking the current request.
......
...@@ -7,7 +7,10 @@ RSpec.describe Gitlab::Database::LoadBalancing::Configuration do ...@@ -7,7 +7,10 @@ RSpec.describe Gitlab::Database::LoadBalancing::Configuration do
config = ActiveRecord::DatabaseConfigurations::HashConfig config = ActiveRecord::DatabaseConfigurations::HashConfig
.new('main', 'test', configuration_hash) .new('main', 'test', configuration_hash)
double(:model, connection_db_config: config) double(:model,
connection_db_config: config,
connection_specification_name: ActiveRecord::Base.connection_specification_name
)
end end
describe '.for_model' do describe '.for_model' do
......
...@@ -23,7 +23,10 @@ RSpec.describe Gitlab::Database::LoadBalancing::Setup do ...@@ -23,7 +23,10 @@ RSpec.describe Gitlab::Database::LoadBalancing::Setup do
env_name: 'test', env_name: 'test',
name: 'main' name: 'main'
) )
model = double(:model, connection_db_config: config) model = double(:model,
connection_db_config: config,
connection_specification_name: ActiveRecord::Base.connection_specification_name
)
expect(ActiveRecord::DatabaseConfigurations::HashConfig) expect(ActiveRecord::DatabaseConfigurations::HashConfig)
.to receive(:new) .to receive(:new)
......
...@@ -4,7 +4,7 @@ require 'spec_helper' ...@@ -4,7 +4,7 @@ require 'spec_helper'
RSpec.describe Gitlab::Database::LoadBalancing::Sticking, :redis do RSpec.describe Gitlab::Database::LoadBalancing::Sticking, :redis do
let(:sticking) do let(:sticking) do
described_class.new(ActiveRecord::Base.connection.load_balancer) described_class.new(ActiveRecord::Base.connection.load_balancer, ActiveRecord::Base)
end end
after do after do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment