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
module LoadBalancing
# Configuration settings for a single LoadBalancer instance.
class Configuration
attr_reader :connection_specification_name, :connection_db_config
attr_accessor :hosts, :max_replication_difference,
:max_replication_lag_time, :replica_check_interval,
:service_discovery, :model
:service_discovery
# Creates a configuration object for the given ActiveRecord model.
def self.for_model(model)
......@@ -45,6 +47,8 @@ module Gitlab
end
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_lag_time = 60.0
@replica_check_interval = 60.0
......
......@@ -27,7 +27,7 @@ module Gitlab
HostList.new(configuration.hosts.map { |addr| Host.new(addr, self) })
end
@name = @configuration.model.connection_db_config.name.to_sym
@name = @configuration.connection_db_config.name.to_sym
end
def primary_only?
......@@ -252,7 +252,7 @@ module Gitlab
# leverage that.
def pool
ActiveRecord::Base.connection_handler.retrieve_connection_pool(
@configuration.model.connection_specification_name,
@configuration.connection_specification_name,
role: ActiveRecord::Base.writing_role,
shard: ActiveRecord::Base.default_shard
) || raise(::ActiveRecord::ConnectionNotEstablished)
......
......@@ -42,7 +42,7 @@ module Gitlab
@model.class_attribute(:sticking)
@model.connection = ConnectionProxy.new(lb)
@model.sticking = Sticking.new(lb)
@model.sticking = Sticking.new(lb, @model)
end
def setup_service_discovery
......
......@@ -10,9 +10,9 @@ module Gitlab
# the primary.
EXPIRATION = 30
def initialize(load_balancer)
def initialize(load_balancer, model)
@load_balancer = load_balancer
@model = load_balancer.configuration.model
@model = model
end
# Unsticks or continues sticking the current request.
......
......@@ -7,7 +7,10 @@ RSpec.describe Gitlab::Database::LoadBalancing::Configuration do
config = ActiveRecord::DatabaseConfigurations::HashConfig
.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
describe '.for_model' do
......
......@@ -23,7 +23,10 @@ RSpec.describe Gitlab::Database::LoadBalancing::Setup do
env_name: 'test',
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)
.to receive(:new)
......
......@@ -4,7 +4,7 @@ require 'spec_helper'
RSpec.describe Gitlab::Database::LoadBalancing::Sticking, :redis do
let(:sticking) do
described_class.new(ActiveRecord::Base.connection.load_balancer)
described_class.new(ActiveRecord::Base.connection.load_balancer, ActiveRecord::Base)
end
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