Class: Gitlab::Database::Partitioning::PartitionManager

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/database/partitioning/partition_manager.rb

Constant Summary collapse

UnsafeToDetachPartitionError =
Class.new(StandardError)
LEASE_TIMEOUT =
1.hour
STATEMENT_TIMEOUT =
1.hour
MANAGEMENT_LEASE_KEY =
'database_partition_management_%s'
RETAIN_DETACHED_PARTITIONS_FOR =
1.week

Instance Method Summary collapse

Constructor Details

#initialize(model, connection: nil) ⇒ PartitionManager

Returns a new instance of PartitionManager.



16
17
18
19
20
# File 'lib/gitlab/database/partitioning/partition_manager.rb', line 16

def initialize(model, connection: nil)
  @model = model
  @connection = connection || model.connection
  @connection_name = @connection.pool.db_config.name
end

Instance Method Details

#sync_partitions(analyze: true) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gitlab/database/partitioning/partition_manager.rb', line 22

def sync_partitions(analyze: true)
  return skip_synching_partitions unless table_partitioned?

  Gitlab::AppLogger.info(
    message: "Checking state of dynamic postgres partitions",
    table_name: model.table_name,
    connection_name: @connection_name
  )

  only_with_exclusive_lease(model, lease_key: MANAGEMENT_LEASE_KEY) do
    model.partitioning_strategy.validate_and_fix

    partitions_to_create = missing_partitions
    partitions_to_detach = extra_partitions

    create(partitions_to_create) unless partitions_to_create.empty?
    detach(partitions_to_detach) unless partitions_to_detach.empty?

    run_analyze_on_partitioned_table if analyze
  end
rescue ArgumentError => e
  Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)
rescue StandardError => e
  Gitlab::AppLogger.error(
    message: "Failed to create / detach partition(s)",
    table_name: model.table_name,
    exception_class: e.class,
    exception_message: e.message,
    connection_name: @connection_name
  )
end