Skip to content
代码片段 群组 项目
未验证 提交 7671e348 编辑于 作者: Simon Tomlinson's avatar Simon Tomlinson 提交者: GitLab
浏览文件

Merge branch 'morefice/drop-big-detached-partitions-on-weekends' into 'master'

No related branches found
No related tags found
2 合并请求!3031Merge per-main-jh to main-jh by luzhiyuan,!3030Merge per-main-jh to main-jh
...@@ -13,6 +13,7 @@ class PartitionManager ...@@ -13,6 +13,7 @@ class PartitionManager
STATEMENT_TIMEOUT = 1.hour STATEMENT_TIMEOUT = 1.hour
MANAGEMENT_LEASE_KEY = 'database_partition_management_%s' MANAGEMENT_LEASE_KEY = 'database_partition_management_%s'
RETAIN_DETACHED_PARTITIONS_FOR = 1.week RETAIN_DETACHED_PARTITIONS_FOR = 1.week
MAX_PARTITION_SIZE = 150.gigabytes
def initialize(model, connection: nil) def initialize(model, connection: nil)
@model = model @model = model
...@@ -120,8 +121,7 @@ def detach_one_partition(partition) ...@@ -120,8 +121,7 @@ def detach_one_partition(partition)
connection.execute partition.to_detach_sql connection.execute partition.to_detach_sql
Postgresql::DetachedPartition.create!(table_name: partition.partition_name, schedule_detached_partition_cleanup(partition)
drop_after: RETAIN_DETACHED_PARTITIONS_FOR.from_now)
Gitlab::AppLogger.info( Gitlab::AppLogger.info(
message: "Detached Partition", message: "Detached Partition",
...@@ -245,6 +245,33 @@ def parent_table_has_loose_foreign_key? ...@@ -245,6 +245,33 @@ def parent_table_has_loose_foreign_key?
has_loose_foreign_key?(model.table_name) has_loose_foreign_key?(model.table_name)
end end
strong_memoize_attr :parent_table_has_loose_foreign_key? strong_memoize_attr :parent_table_has_loose_foreign_key?
def schedule_detached_partition_cleanup(partition)
identifier = identifier(partition)
if above_threshold?(identifier)
Postgresql::DetachedPartition.create!(
table_name: partition.partition_name,
drop_after: RETAIN_DETACHED_PARTITIONS_FOR.from_now.next_occurring(:saturday)
)
else
Postgresql::DetachedPartition.create!(
table_name: partition.partition_name,
drop_after: RETAIN_DETACHED_PARTITIONS_FOR.from_now
)
end
end
def above_threshold?(identifier)
Gitlab::Database::PostgresPartition
.for_identifier(identifier)
.above_threshold(MAX_PARTITION_SIZE)
.exists?
end
def identifier(partition)
"#{Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA}.#{partition.partition_name}"
end
end end
end end
end end
......
...@@ -352,6 +352,27 @@ def num_tables ...@@ -352,6 +352,27 @@ def num_tables
expect { subject }.not_to change { find_partitions(my_model.table_name).size } expect { subject }.not_to change { find_partitions(my_model.table_name).size }
end end
end end
context 'when scheduling partition drops for large partitions' do
before do
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:above_threshold?).and_return(true)
end
end
it 'schedules large partitions for weekend drops' do
next_saturday = Date.parse('2021-07-03')
expect(Postgresql::DetachedPartition).to receive(:create!).with(
table_name: "#{partitioned_table_name}_202104",
drop_after: anything
) do |args|
expect(args[:drop_after].to_date).to eq(next_saturday)
end
subject
end
end
end end
describe 'analyze partitioned table' do describe 'analyze partitioned table' do
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册