debian-mirror-gitlab/lib/gitlab/database/partitioning/monthly_strategy.rb

103 lines
3.1 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module Gitlab
module Database
module Partitioning
class MonthlyStrategy
2021-09-30 23:02:18 +05:30
attr_reader :model, :partitioning_key, :retain_for
2020-07-28 23:09:34 +05:30
# We create this many partitions in the future
HEADROOM = 6.months
delegate :table_name, to: :model
2021-09-30 23:02:18 +05:30
def initialize(model, partitioning_key, retain_for: nil)
2020-07-28 23:09:34 +05:30
@model = model
@partitioning_key = partitioning_key
2021-09-30 23:02:18 +05:30
@retain_for = retain_for
2020-07-28 23:09:34 +05:30
end
def current_partitions
2021-01-29 00:20:46 +05:30
Gitlab::Database::PostgresPartition.for_parent_table(table_name).map do |partition|
TimePartition.from_sql(table_name, partition.name, partition.condition)
2020-07-28 23:09:34 +05:30
end
end
# Check the currently existing partitions and determine which ones are missing
def missing_partitions
desired_partitions - current_partitions
end
2021-09-30 23:02:18 +05:30
def extra_partitions
current_partitions - desired_partitions
end
2020-07-28 23:09:34 +05:30
private
def desired_partitions
[].tap do |parts|
min_date, max_date = relevant_range
2021-09-30 23:02:18 +05:30
if pruning_old_partitions? && min_date <= oldest_active_date
min_date = oldest_active_date.beginning_of_month
else
parts << partition_for(upper_bound: min_date)
end
2020-07-28 23:09:34 +05:30
while min_date < max_date
next_date = min_date.next_month
parts << partition_for(lower_bound: min_date, upper_bound: next_date)
min_date = next_date
end
end
end
# This determines the relevant time range for which we expect to have data
# (and therefore need to create partitions for).
#
# Note: We typically expect the first partition to be half-unbounded, i.e.
# to start from MINVALUE to a specific date `x`. The range returned
# does not include the range of the first, half-unbounded partition.
def relevant_range
2021-09-30 23:02:18 +05:30
if (first_partition = current_partitions.min)
2020-07-28 23:09:34 +05:30
# Case 1: First partition starts with MINVALUE, i.e. from is nil -> start with first real partition
# Case 2: Rather unexpectedly, first partition does not start with MINVALUE, i.e. from is not nil
# In this case, use first partition beginning as a start
min_date = first_partition.from || first_partition.to
end
2021-09-30 23:02:18 +05:30
if pruning_old_partitions?
min_date ||= oldest_active_date
end
2020-07-28 23:09:34 +05:30
# In case we don't have a partition yet
min_date ||= Date.today
min_date = min_date.beginning_of_month
max_date = Date.today.end_of_month + HEADROOM
[min_date, max_date]
end
def partition_for(lower_bound: nil, upper_bound:)
TimePartition.new(table_name, lower_bound, upper_bound)
end
2021-09-30 23:02:18 +05:30
def pruning_old_partitions?
2021-10-27 15:23:28 +05:30
retain_for.present?
2021-09-30 23:02:18 +05:30
end
def oldest_active_date
(Date.today - retain_for).beginning_of_month
end
2020-07-28 23:09:34 +05:30
def connection
ActiveRecord::Base.connection
end
end
end
end
end