2020-05-24 23:13:21 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Metrics::Samplers::DatabaseSampler do
|
2020-06-23 00:09:42 +05:30
|
|
|
subject { described_class.new }
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it_behaves_like 'metrics sampler', 'DATABASE_SAMPLER'
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
describe '#sample' do
|
2022-01-26 12:08:38 +05:30
|
|
|
let(:main_labels) do
|
|
|
|
{
|
|
|
|
class: 'ActiveRecord::Base',
|
|
|
|
host: ApplicationRecord.database.config['host'],
|
|
|
|
port: ApplicationRecord.database.config['port'],
|
|
|
|
db_config_name: 'main'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:ci_labels) do
|
|
|
|
{
|
|
|
|
class: 'Ci::ApplicationRecord',
|
|
|
|
host: Ci::ApplicationRecord.database.config['host'],
|
|
|
|
port: Ci::ApplicationRecord.database.config['port'],
|
|
|
|
db_config_name: 'ci'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:main_replica_labels) do
|
|
|
|
{
|
|
|
|
class: 'ActiveRecord::Base',
|
|
|
|
host: 'main-replica-host',
|
|
|
|
port: 2345,
|
|
|
|
db_config_name: 'main_replica'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:ci_replica_labels) do
|
|
|
|
{
|
|
|
|
class: 'Ci::ApplicationRecord',
|
|
|
|
host: 'ci-replica-host',
|
|
|
|
port: 3456,
|
|
|
|
db_config_name: 'ci_replica'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
before do
|
|
|
|
described_class::METRIC_DESCRIPTIONS.each_key do |metric|
|
|
|
|
allow(subject.metrics[metric]).to receive(:set)
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
allow(Gitlab::Database).to receive(:database_base_models)
|
|
|
|
.and_return({ main: ActiveRecord::Base, ci: Ci::ApplicationRecord })
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
context 'when all base models are connected', :add_ci_connection do
|
|
|
|
it 'samples connection pool statistics for all primaries' do
|
|
|
|
expect_metrics_with_labels(main_labels)
|
|
|
|
expect_metrics_with_labels(ci_labels)
|
|
|
|
|
|
|
|
subject.sample
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
context 'when replica hosts are configured' do
|
2022-03-02 08:16:31 +05:30
|
|
|
let(:main_load_balancer) { ApplicationRecord.load_balancer }
|
2022-01-26 12:08:38 +05:30
|
|
|
let(:main_replica_host) { main_load_balancer.host }
|
|
|
|
|
|
|
|
let(:ci_load_balancer) { double(:load_balancer, host_list: ci_host_list, configuration: configuration) }
|
|
|
|
let(:configuration) { double(:configuration, primary_connection_specification_name: 'Ci::ApplicationRecord') }
|
|
|
|
let(:ci_host_list) { double(:host_list, hosts: [ci_replica_host]) }
|
|
|
|
let(:ci_replica_host) { double(:host, connection: ci_connection) }
|
|
|
|
let(:ci_connection) { double(:connection, pool: Ci::ApplicationRecord.connection_pool) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Database::LoadBalancing).to receive(:each_load_balancer)
|
|
|
|
.and_return([main_load_balancer, ci_load_balancer].to_enum)
|
|
|
|
|
|
|
|
allow(main_load_balancer).to receive(:primary_only?).and_return(false)
|
|
|
|
allow(ci_load_balancer).to receive(:primary_only?).and_return(false)
|
|
|
|
|
|
|
|
allow(main_replica_host).to receive(:host).and_return('main-replica-host')
|
|
|
|
allow(ci_replica_host).to receive(:host).and_return('ci-replica-host')
|
|
|
|
|
|
|
|
allow(main_replica_host).to receive(:port).and_return(2345)
|
|
|
|
allow(ci_replica_host).to receive(:port).and_return(3456)
|
|
|
|
|
|
|
|
allow(Gitlab::Database).to receive(:db_config_name)
|
|
|
|
.with(main_replica_host.connection)
|
|
|
|
.and_return('main_replica')
|
|
|
|
|
|
|
|
allow(Gitlab::Database).to receive(:db_config_name)
|
|
|
|
.with(ci_replica_host.connection)
|
|
|
|
.and_return('ci_replica')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'samples connection pool statistics for primaries and replicas' do
|
|
|
|
expect_metrics_with_labels(main_labels)
|
|
|
|
expect_metrics_with_labels(ci_labels)
|
|
|
|
expect_metrics_with_labels(main_replica_labels)
|
|
|
|
expect_metrics_with_labels(ci_replica_labels)
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
subject.sample
|
|
|
|
end
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a base model is not connected', :add_ci_connection do
|
|
|
|
before do
|
|
|
|
allow(Ci::ApplicationRecord).to receive(:connected?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'records no samples for that primary' do
|
|
|
|
expect_metrics_with_labels(main_labels)
|
|
|
|
expect_no_metrics_with_labels(ci_labels)
|
|
|
|
|
|
|
|
subject.sample
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the base model has replica connections' do
|
2022-03-02 08:16:31 +05:30
|
|
|
let(:main_load_balancer) { ApplicationRecord.load_balancer }
|
2022-01-26 12:08:38 +05:30
|
|
|
let(:main_replica_host) { main_load_balancer.host }
|
|
|
|
|
|
|
|
let(:ci_load_balancer) { double(:load_balancer, host_list: ci_host_list, configuration: configuration) }
|
|
|
|
let(:configuration) { double(:configuration, primary_connection_specification_name: 'Ci::ApplicationRecord') }
|
|
|
|
let(:ci_host_list) { double(:host_list, hosts: [ci_replica_host]) }
|
|
|
|
let(:ci_replica_host) { double(:host, connection: ci_connection) }
|
|
|
|
let(:ci_connection) { double(:connection, pool: Ci::ApplicationRecord.connection_pool) }
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
before do
|
2022-01-26 12:08:38 +05:30
|
|
|
allow(Gitlab::Database::LoadBalancing).to receive(:each_load_balancer)
|
|
|
|
.and_return([main_load_balancer, ci_load_balancer].to_enum)
|
|
|
|
|
|
|
|
allow(main_load_balancer).to receive(:primary_only?).and_return(false)
|
|
|
|
allow(ci_load_balancer).to receive(:primary_only?).and_return(false)
|
|
|
|
|
|
|
|
allow(main_replica_host).to receive(:host).and_return('main-replica-host')
|
|
|
|
allow(ci_replica_host).to receive(:host).and_return('ci-replica-host')
|
|
|
|
|
|
|
|
allow(main_replica_host).to receive(:port).and_return(2345)
|
|
|
|
allow(ci_replica_host).to receive(:port).and_return(3456)
|
|
|
|
|
|
|
|
allow(Gitlab::Database).to receive(:db_config_name)
|
|
|
|
.with(main_replica_host.connection)
|
|
|
|
.and_return('main_replica')
|
|
|
|
|
|
|
|
allow(Gitlab::Database).to receive(:db_config_name)
|
|
|
|
.with(ci_replica_host.connection)
|
|
|
|
.and_return('ci_replica')
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
it 'still records the replica metrics' do
|
|
|
|
expect_metrics_with_labels(main_labels)
|
|
|
|
expect_metrics_with_labels(main_replica_labels)
|
|
|
|
expect_no_metrics_with_labels(ci_labels)
|
|
|
|
expect_metrics_with_labels(ci_replica_labels)
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
subject.sample
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
def expect_metrics_with_labels(labels)
|
|
|
|
expect(subject.metrics[:size]).to receive(:set).with(labels, a_value >= 1)
|
|
|
|
expect(subject.metrics[:connections]).to receive(:set).with(labels, a_value >= 1)
|
|
|
|
expect(subject.metrics[:busy]).to receive(:set).with(labels, a_value >= 1)
|
|
|
|
expect(subject.metrics[:dead]).to receive(:set).with(labels, a_value >= 0)
|
|
|
|
expect(subject.metrics[:waiting]).to receive(:set).with(labels, a_value >= 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def expect_no_metrics_with_labels(labels)
|
|
|
|
described_class::METRIC_DESCRIPTIONS.each_key do |metric|
|
|
|
|
expect(subject.metrics[metric]).not_to receive(:set).with(labels, anything)
|
|
|
|
end
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
end
|