2021-09-16 14:11:32 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module Partitioning
|
2022-02-01 13:17:05 -05:00
|
|
|
class TableWithoutModel < Gitlab::Database::SharedModel
|
|
|
|
include PartitionedTable
|
2021-09-16 14:11:32 -04:00
|
|
|
end
|
|
|
|
|
2021-10-27 11:13:41 -04:00
|
|
|
class << self
|
|
|
|
def register_models(models)
|
2021-11-09 13:13:13 -05:00
|
|
|
models.each do |model|
|
|
|
|
raise "#{model} should have partitioning strategy defined" unless model.respond_to?(:partitioning_strategy)
|
|
|
|
|
|
|
|
registered_models << model
|
|
|
|
end
|
2021-10-27 11:13:41 -04:00
|
|
|
end
|
2021-10-20 20:12:27 -04:00
|
|
|
|
2021-10-27 11:13:41 -04:00
|
|
|
def register_tables(tables)
|
|
|
|
registered_tables.merge(tables)
|
2021-10-20 20:12:27 -04:00
|
|
|
end
|
|
|
|
|
2021-11-10 13:12:35 -05:00
|
|
|
def sync_partitions_ignore_db_error
|
|
|
|
sync_partitions unless ENV['DISABLE_POSTGRES_PARTITION_CREATION_ON_STARTUP']
|
|
|
|
rescue ActiveRecord::ActiveRecordError, PG::Error
|
|
|
|
# ignore - happens when Rake tasks yet have to create a database, e.g. for testing
|
|
|
|
end
|
|
|
|
|
2022-03-13 17:09:30 -04:00
|
|
|
def sync_partitions(models_to_sync = registered_for_sync, only_on: nil)
|
2021-10-27 11:13:41 -04:00
|
|
|
Gitlab::AppLogger.info(message: 'Syncing dynamic postgres partitions')
|
2021-10-20 20:12:27 -04:00
|
|
|
|
2022-03-13 17:09:30 -04:00
|
|
|
Gitlab::Database::EachDatabase.each_model_connection(models_to_sync, only_on: only_on) do |model|
|
2021-10-27 11:13:41 -04:00
|
|
|
PartitionManager.new(model).sync_partitions
|
|
|
|
end
|
2021-10-20 20:12:27 -04:00
|
|
|
|
2021-10-27 11:13:41 -04:00
|
|
|
Gitlab::AppLogger.info(message: 'Finished sync of dynamic postgres partitions')
|
2021-10-20 20:12:27 -04:00
|
|
|
end
|
2021-10-01 05:11:45 -04:00
|
|
|
|
2021-10-27 11:13:41 -04:00
|
|
|
def report_metrics(models_to_monitor = registered_models)
|
|
|
|
partition_monitoring = PartitionMonitoring.new
|
|
|
|
|
|
|
|
Gitlab::Database::EachDatabase.each_model_connection(models_to_monitor) do |model|
|
|
|
|
partition_monitoring.report_metrics_for_model(model)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def drop_detached_partitions
|
|
|
|
Gitlab::AppLogger.info(message: 'Dropping detached postgres partitions')
|
|
|
|
|
|
|
|
Gitlab::Database::EachDatabase.each_database_connection do
|
|
|
|
DetachedPartitionDropper.new.perform
|
|
|
|
end
|
2021-10-20 20:12:27 -04:00
|
|
|
|
2021-10-27 11:13:41 -04:00
|
|
|
Gitlab::AppLogger.info(message: 'Finished dropping detached postgres partitions')
|
2021-10-20 20:12:27 -04:00
|
|
|
end
|
|
|
|
|
2021-10-27 11:13:41 -04:00
|
|
|
def registered_models
|
|
|
|
@registered_models ||= Set.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def registered_tables
|
|
|
|
@registered_tables ||= Set.new
|
|
|
|
end
|
|
|
|
|
2021-11-09 13:13:13 -05:00
|
|
|
private
|
|
|
|
|
2021-10-27 11:13:41 -04:00
|
|
|
def registered_for_sync
|
|
|
|
registered_models + registered_tables.map do |table|
|
2022-02-01 13:17:05 -05:00
|
|
|
table_without_model(**table)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def table_without_model(table_name:, partitioned_column:, strategy:, limit_connection_names: nil)
|
|
|
|
Class.new(TableWithoutModel).tap do |klass|
|
|
|
|
klass.table_name = table_name
|
|
|
|
klass.partitioned_by(partitioned_column, strategy: strategy)
|
|
|
|
klass.limit_connection_names = limit_connection_names
|
2021-10-27 11:13:41 -04:00
|
|
|
end
|
|
|
|
end
|
2021-10-01 05:11:45 -04:00
|
|
|
end
|
2021-09-16 14:11:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|