Commit fee351d8 authored by Thong Kuah's avatar Thong Kuah

Log in Prometheus current number of host and index

This is done in 3 places which should not be called in high frequency
parent b5bc114d
---
title: 'DB Load Balancing: Log Prometheus current number of hosts and current index'
merge_request: 15440
author:
type: changed
......@@ -10,6 +10,10 @@ module Gitlab
@hosts = hosts.shuffle
@index = 0
@mutex = Mutex.new
@hosts_gauge = Gitlab::Metrics.gauge(:db_load_balancing_hosts, 'Current number of load balancing hosts')
@index_gauge = Gitlab::Metrics.gauge(:db_load_balancing_index, 'Current load balancing host index')
set_metrics!
end
def hosts
......@@ -29,13 +33,24 @@ module Gitlab
@hosts = hosts.shuffle
@index = 0
end
set_metrics!
end
# Sets metrics before returning next host
def next
next_host.tap do |_|
set_metrics!
end
end
private
# Returns the next available host.
#
# Returns a Gitlab::Database::LoadBalancing::Host instance, or nil if no
# hosts were available.
def next
def next_host
@mutex.synchronize do
break if @hosts.empty?
......@@ -53,6 +68,11 @@ module Gitlab
end
end
end
def set_metrics!
@hosts_gauge.set({}, @hosts.length)
@index_gauge.set({}, @index)
end
end
end
end
......
require 'spec_helper'
describe Gitlab::Database::LoadBalancing::HostList do
def expect_metrics(hosts, index)
expect(Gitlab::Metrics.registry.get(:db_load_balancing_hosts).get({})).to eq(hosts)
expect(Gitlab::Metrics.registry.get(:db_load_balancing_index).get({})).to eq(index)
end
before do
allow(Gitlab::Database)
.to receive(:create_connection_pool)
......@@ -17,6 +22,14 @@ describe Gitlab::Database::LoadBalancing::HostList do
described_class.new(hosts)
end
describe '#initialize' do
it 'sets metrics for current number of hosts and current index' do
host_list
expect_metrics(2, 0)
end
end
describe '#length' do
it 'returns the number of hosts in the list' do
expect(host_list.length).to eq(2)
......@@ -63,6 +76,7 @@ describe Gitlab::Database::LoadBalancing::HostList do
expect(host_list.length).to eq(1)
expect(host_list.hosts[0].host).to eq('foo')
expect_metrics(1, 0)
end
end
......@@ -74,14 +88,20 @@ describe Gitlab::Database::LoadBalancing::HostList do
it 'cycles through all available hosts' do
expect(host_list.next).to eq(host_list.hosts[0])
expect_metrics(2, 1)
expect(host_list.next).to eq(host_list.hosts[1])
expect_metrics(2, 0)
expect(host_list.next).to eq(host_list.hosts[0])
expect_metrics(2, 1)
end
it 'skips hosts that are offline' do
allow(host_list.hosts[0]).to receive(:online?).and_return(false)
expect(host_list.next).to eq(host_list.hosts[1])
expect_metrics(2, 0)
end
it 'returns nil if no hosts are online' do
......@@ -90,6 +110,7 @@ describe Gitlab::Database::LoadBalancing::HostList do
end
expect(host_list.next).to be_nil
expect_metrics(2, 0)
end
it 'returns nil if no hosts are available' 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