debian-mirror-gitlab/spec/lib/gitlab/database/partitioning_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

255 lines
8.5 KiB
Ruby
Raw Normal View History

2021-11-11 11:23:49 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Database::Partitioning do
2021-12-11 22:18:48 +05:30
include Database::PartitioningHelpers
include Database::TableSchemaHelpers
let(:connection) { ApplicationRecord.connection }
around do |example|
previously_registered_models = described_class.registered_models.dup
2023-03-17 16:20:25 +05:30
described_class.instance_variable_set(:@registered_models, Set.new)
2021-12-11 22:18:48 +05:30
previously_registered_tables = described_class.registered_tables.dup
2023-03-17 16:20:25 +05:30
described_class.instance_variable_set(:@registered_tables, Set.new)
2021-12-11 22:18:48 +05:30
example.run
2023-03-17 16:20:25 +05:30
described_class.instance_variable_set(:@registered_models, previously_registered_models)
described_class.instance_variable_set(:@registered_tables, previously_registered_tables)
2021-12-11 22:18:48 +05:30
end
describe '.register_models' do
context 'ensure that the registered models have partitioning strategy' do
it 'fails when partitioning_strategy is not specified for the model' do
model = Class.new(ApplicationRecord)
expect { described_class.register_models([model]) }.to raise_error /should have partitioning strategy defined/
end
end
end
describe '.sync_partitions_ignore_db_error' do
it 'calls sync_partitions' do
expect(described_class).to receive(:sync_partitions)
described_class.sync_partitions_ignore_db_error
end
[ActiveRecord::ActiveRecordError, PG::Error].each do |error|
context "when #{error} is raised" do
before do
expect(described_class).to receive(:sync_partitions)
.and_raise(error)
end
it 'ignores it' do
described_class.sync_partitions_ignore_db_error
end
end
end
context 'when DISABLE_POSTGRES_PARTITION_CREATION_ON_STARTUP is set' do
before do
stub_env('DISABLE_POSTGRES_PARTITION_CREATION_ON_STARTUP', '1')
end
it 'does not call sync_partitions' do
2022-10-11 01:57:18 +05:30
expect(described_class).not_to receive(:sync_partitions)
2021-12-11 22:18:48 +05:30
described_class.sync_partitions_ignore_db_error
end
end
end
2021-11-11 11:23:49 +05:30
describe '.sync_partitions' do
2022-10-11 01:57:18 +05:30
let(:ci_connection) { Ci::ApplicationRecord.connection }
2021-12-11 22:18:48 +05:30
let(:table_names) { %w[partitioning_test1 partitioning_test2] }
let(:models) do
table_names.map do |table_name|
Class.new(ApplicationRecord) do
include PartitionedTable
self.table_name = table_name
partitioned_by :created_at, strategy: :monthly
end
end
end
before do
table_names.each do |table_name|
connection.execute(<<~SQL)
CREATE TABLE #{table_name} (
id serial not null,
created_at timestamptz not null,
PRIMARY KEY (id, created_at))
PARTITION BY RANGE (created_at);
SQL
end
end
it 'manages partitions for each given model' do
2022-08-27 11:52:29 +05:30
expect { described_class.sync_partitions(models) }
2021-12-11 22:18:48 +05:30
.to change { find_partitions(table_names.first).size }.from(0)
.and change { find_partitions(table_names.last).size }.from(0)
end
2021-11-11 11:23:49 +05:30
2022-10-11 01:57:18 +05:30
context 'with multiple databases' do
before do
table_names.each do |table_name|
ci_connection.execute("DROP TABLE IF EXISTS #{table_name}")
ci_connection.execute(<<~SQL)
CREATE TABLE #{table_name} (
id serial not null,
created_at timestamptz not null,
PRIMARY KEY (id, created_at))
PARTITION BY RANGE (created_at);
SQL
end
end
after do
table_names.each do |table_name|
ci_connection.execute("DROP TABLE IF EXISTS #{table_name}")
end
end
it 'creates partitions in each database' do
skip_if_multiple_databases_not_setup
expect { described_class.sync_partitions(models) }
.to change { find_partitions(table_names.first, conn: connection).size }.from(0)
.and change { find_partitions(table_names.last, conn: connection).size }.from(0)
.and change { find_partitions(table_names.first, conn: ci_connection).size }.from(0)
.and change { find_partitions(table_names.last, conn: ci_connection).size }.from(0)
end
end
2021-11-11 11:23:49 +05:30
context 'when no partitioned models are given' do
2021-12-11 22:18:48 +05:30
it 'manages partitions for each registered model' do
described_class.register_models([models.first])
2022-11-25 23:54:43 +05:30
described_class.register_tables(
[
{
table_name: table_names.last,
partitioned_column: :created_at,
strategy: :monthly
}
])
2021-11-11 11:23:49 +05:30
2021-12-11 22:18:48 +05:30
expect { described_class.sync_partitions }
.to change { find_partitions(table_names.first).size }.from(0)
.and change { find_partitions(table_names.last).size }.from(0)
end
end
2022-05-07 20:08:51 +05:30
context 'when only a specific database is requested' do
2022-10-11 01:57:18 +05:30
let(:ci_model) do
Class.new(Ci::ApplicationRecord) do
include PartitionedTable
self.table_name = 'partitioning_test3'
partitioned_by :created_at, strategy: :monthly
end
end
2022-05-07 20:08:51 +05:30
before do
2022-10-11 01:57:18 +05:30
(table_names + ['partitioning_test3']).each do |table_name|
ci_connection.execute("DROP TABLE IF EXISTS #{table_name}")
ci_connection.execute(<<~SQL)
CREATE TABLE #{table_name} (
id serial not null,
created_at timestamptz not null,
PRIMARY KEY (id, created_at))
PARTITION BY RANGE (created_at);
SQL
end
end
after do
(table_names + ['partitioning_test3']).each do |table_name|
ci_connection.execute("DROP TABLE IF EXISTS #{table_name}")
end
2022-05-07 20:08:51 +05:30
end
it 'manages partitions for models for the given database', :aggregate_failures do
2022-10-11 01:57:18 +05:30
skip_if_multiple_databases_not_setup
expect { described_class.sync_partitions([models.first, ci_model], only_on: 'ci') }
.to change { find_partitions(ci_model.table_name, conn: ci_connection).size }.from(0)
2022-05-07 20:08:51 +05:30
2022-10-11 01:57:18 +05:30
expect(find_partitions(models.first.table_name).size).to eq(0)
expect(find_partitions(models.first.table_name, conn: ci_connection).size).to eq(0)
expect(find_partitions(ci_model.table_name).size).to eq(0)
2022-05-07 20:08:51 +05:30
end
end
2021-12-11 22:18:48 +05:30
end
describe '.report_metrics' do
let(:model1) { double('model') }
let(:model2) { double('model') }
let(:partition_monitoring_class) { described_class::PartitionMonitoring }
context 'when no partitioned models are given' do
it 'reports metrics for each registered model' do
expect_next_instance_of(partition_monitoring_class) do |partition_monitor|
expect(partition_monitor).to receive(:report_metrics_for_model).with(model1)
expect(partition_monitor).to receive(:report_metrics_for_model).with(model2)
end
expect(Gitlab::Database::EachDatabase).to receive(:each_model_connection)
.with(described_class.__send__(:registered_models))
.and_yield(model1)
.and_yield(model2)
2021-11-11 11:23:49 +05:30
2021-12-11 22:18:48 +05:30
described_class.report_metrics
2021-11-11 11:23:49 +05:30
end
end
context 'when partitioned models are given' do
2021-12-11 22:18:48 +05:30
it 'reports metrics for each given model' do
expect_next_instance_of(partition_monitoring_class) do |partition_monitor|
expect(partition_monitor).to receive(:report_metrics_for_model).with(model1)
expect(partition_monitor).to receive(:report_metrics_for_model).with(model2)
end
2021-11-11 11:23:49 +05:30
2021-12-11 22:18:48 +05:30
expect(Gitlab::Database::EachDatabase).to receive(:each_model_connection)
.with([model1, model2])
.and_yield(model1)
.and_yield(model2)
2021-11-11 11:23:49 +05:30
2021-12-11 22:18:48 +05:30
described_class.report_metrics([model1, model2])
2021-11-11 11:23:49 +05:30
end
end
end
2021-11-18 22:05:49 +05:30
describe '.drop_detached_partitions' do
2021-12-11 22:18:48 +05:30
let(:table_names) { %w[detached_test_partition1 detached_test_partition2] }
before do
table_names.each do |table_name|
connection.create_table("#{Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA}.#{table_name}")
2021-11-18 22:05:49 +05:30
2021-12-11 22:18:48 +05:30
Postgresql::DetachedPartition.create!(table_name: table_name, drop_after: 1.year.ago)
2021-11-18 22:05:49 +05:30
end
2021-12-11 22:18:48 +05:30
end
2021-11-18 22:05:49 +05:30
2021-12-11 22:18:48 +05:30
it 'drops detached partitions for each database' do
expect(Gitlab::Database::EachDatabase).to receive(:each_database_connection).and_yield
expect { described_class.drop_detached_partitions }
.to change { Postgresql::DetachedPartition.count }.from(2).to(0)
.and change { table_exists?(table_names.first) }.from(true).to(false)
.and change { table_exists?(table_names.last) }.from(true).to(false)
2021-11-18 22:05:49 +05:30
end
2021-12-11 22:18:48 +05:30
def table_exists?(table_name)
table_oid(table_name).present?
2021-11-18 22:05:49 +05:30
end
end
2021-11-11 11:23:49 +05:30
end