2021-09-16 15:12:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-01-19 21:14:54 +00:00
|
|
|
class LooseForeignKeys::DeletedRecord < Gitlab::Database::SharedModel
|
2021-12-10 15:10:24 +00:00
|
|
|
PARTITION_DURATION = 1.day
|
|
|
|
|
|
|
|
include PartitionedTable
|
|
|
|
|
2021-10-21 15:12:54 +00:00
|
|
|
self.primary_key = :id
|
2021-12-10 15:10:24 +00:00
|
|
|
self.ignored_columns = %i[partition]
|
|
|
|
|
|
|
|
partitioned_by :partition, strategy: :sliding_list,
|
|
|
|
next_partition_if: -> (active_partition) do
|
2022-05-06 15:09:03 +00:00
|
|
|
return false if Feature.disabled?(:lfk_automatic_partition_creation)
|
2021-12-10 15:10:24 +00:00
|
|
|
|
|
|
|
oldest_record_in_partition = LooseForeignKeys::DeletedRecord
|
|
|
|
.select(:id, :created_at)
|
|
|
|
.for_partition(active_partition)
|
|
|
|
.order(:id)
|
|
|
|
.limit(1)
|
|
|
|
.take
|
|
|
|
|
|
|
|
oldest_record_in_partition.present? && oldest_record_in_partition.created_at < PARTITION_DURATION.ago
|
|
|
|
end,
|
|
|
|
detach_partition_if: -> (partition) do
|
2022-05-06 15:09:03 +00:00
|
|
|
return false if Feature.disabled?(:lfk_automatic_partition_dropping)
|
2021-12-10 15:10:24 +00:00
|
|
|
|
|
|
|
!LooseForeignKeys::DeletedRecord
|
|
|
|
.for_partition(partition)
|
|
|
|
.status_pending
|
|
|
|
.exists?
|
|
|
|
end
|
2021-10-21 15:12:54 +00:00
|
|
|
|
|
|
|
scope :for_table, -> (table) { where(fully_qualified_table_name: table) }
|
2021-12-10 15:10:24 +00:00
|
|
|
scope :for_partition, -> (partition) { where(partition: partition) }
|
2021-11-10 00:11:48 +00:00
|
|
|
scope :consume_order, -> { order(:partition, :consume_after, :id) }
|
2021-10-21 15:12:54 +00:00
|
|
|
|
|
|
|
enum status: { pending: 1, processed: 2 }, _prefix: :status
|
|
|
|
|
|
|
|
def self.load_batch_for_table(table, batch_size)
|
2021-12-10 15:10:24 +00:00
|
|
|
# selecting partition as partition_number to workaround the sliding partitioning column ignore
|
|
|
|
select(arel_table[Arel.star], arel_table[:partition].as('partition_number'))
|
|
|
|
.for_table(table)
|
2021-10-21 15:12:54 +00:00
|
|
|
.status_pending
|
2021-11-10 00:11:48 +00:00
|
|
|
.consume_order
|
2021-10-21 15:12:54 +00:00
|
|
|
.limit(batch_size)
|
|
|
|
.to_a
|
|
|
|
end
|
|
|
|
|
2022-02-07 09:12:13 +00:00
|
|
|
def self.mark_records_processed(records)
|
|
|
|
update_by_partition(records) do |partitioned_scope|
|
|
|
|
partitioned_scope.update_all(status: :processed)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reschedule(records, consume_after)
|
|
|
|
update_by_partition(records) do |partitioned_scope|
|
|
|
|
partitioned_scope.update_all(consume_after: consume_after, cleanup_attempts: 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.increment_attempts(records)
|
|
|
|
update_by_partition(records) do |partitioned_scope|
|
|
|
|
# Naive incrementing of the cleanup_attempts is good enough for us.
|
|
|
|
partitioned_scope.update_all('cleanup_attempts = cleanup_attempts + 1')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.update_by_partition(records)
|
2021-11-10 00:11:48 +00:00
|
|
|
update_count = 0
|
2021-10-21 15:12:54 +00:00
|
|
|
|
2022-02-07 09:12:13 +00:00
|
|
|
# Run a query for each partition to optimize the row lookup by primary key (partition, id)
|
|
|
|
records.group_by(&:partition_number).each do |partition, records_within_partition|
|
|
|
|
partitioned_scope = status_pending
|
2021-12-10 15:10:24 +00:00
|
|
|
.for_partition(partition)
|
2021-11-10 00:11:48 +00:00
|
|
|
.where(id: records_within_partition.pluck(:id))
|
2022-02-07 09:12:13 +00:00
|
|
|
|
|
|
|
update_count += yield(partitioned_scope)
|
2021-11-10 00:11:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
update_count
|
2021-10-21 15:12:54 +00:00
|
|
|
end
|
2022-02-07 09:12:13 +00:00
|
|
|
|
|
|
|
private_class_method :update_by_partition
|
2021-09-16 15:12:47 +00:00
|
|
|
end
|