Class: Gitlab::Database::Partitioning::PartitionCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/database/partitioning/partition_creator.rb

Constant Summary collapse

LEASE_TIMEOUT =
1.minute
LEASE_KEY =
'database_partition_creation_%s'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(models = self.class.models) ⇒ PartitionCreator

Returns a new instance of PartitionCreator.


22
23
24
# File 'lib/gitlab/database/partitioning/partition_creator.rb', line 22

def initialize(models = self.class.models)
  @models = models
end

Instance Attribute Details

#modelsObject (readonly)

Returns the value of attribute models


20
21
22
# File 'lib/gitlab/database/partitioning/partition_creator.rb', line 20

def models
  @models
end

Class Method Details

.modelsObject


13
14
15
# File 'lib/gitlab/database/partitioning/partition_creator.rb', line 13

def self.models
  @models ||= Set.new
end

.register(model) ⇒ Object

Raises:

  • (ArgumentError)

7
8
9
10
11
# File 'lib/gitlab/database/partitioning/partition_creator.rb', line 7

def self.register(model)
  raise ArgumentError, "Only models with a #partitioning_strategy can be registered." unless model.respond_to?(:partitioning_strategy)

  models << model
end

Instance Method Details

#create_partitionsObject


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gitlab/database/partitioning/partition_creator.rb', line 26

def create_partitions
  Gitlab::AppLogger.info("Checking state of dynamic postgres partitions")

  models.each do |model|
    # Double-checking before getting the lease:
    # The prevailing situation is no missing partitions
    next if missing_partitions(model).empty?

    only_with_exclusive_lease(model) do
      partitions_to_create = missing_partitions(model)

      next if partitions_to_create.empty?

      create(model, partitions_to_create)
    end
  rescue => e
    Gitlab::AppLogger.error("Failed to create partition(s) for #{model.table_name}: #{e.class}: #{e.message}")
  end
end