Welcome to mirror list, hosted at ThFree Co, Russian Federation.

internal_id_generator.rb « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e578df76ec581cc2aa52d4e5f528c63746456aa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# frozen_string_literal: true

class InternalIdGenerator
  # Generate next internal id for a given scope and usage.
  #
  # For currently supported usages, see #usage enum.
  #
  # The method implements a locking scheme that has the following properties:
  # 1) Generated sequence of internal ids is unique per (scope and usage)
  # 2) The method is thread-safe and may be used in concurrent threads/processes.
  # 3) The generated sequence is gapless.
  # 4) In the absence of a record in the internal_ids table, one will be created
  #    and last_value will be calculated on the fly.
  #
  # subject: The instance we're generating an internal id for. Gets passed to init if called.
  # scope: Attributes that define the scope for id generation.
  # usage: Symbol to define the usage of the internal id, see InternalId.usages
  attr_reader :subject, :scope, :scope_attrs, :usage

  def initialize(subject, scope, usage)
    @subject = subject
    @scope = scope
    @usage = usage

    raise ArgumentError, 'Scope is not well-defined, need at least one column for scope (given: 0)' if scope.empty?

    unless InternalId.usages.has_key?(usage.to_s)
      raise ArgumentError, "Usage '#{usage}' is unknown. Supported values are #{InternalId.usages.keys} from InternalId.usages"
    end
  end

  # Generates next internal id and returns it
  # init: Block that gets called to initialize InternalId record if not present
  #       Make sure to not throw exceptions in the absence of records (if this is expected).
  def generate(init)
    subject.transaction do
      # Create a record in internal_ids if one does not yet exist
      # and increment its last value
      #
      # Note this will acquire a ROW SHARE lock on the InternalId record
      (lookup || create_record(init)).increment_and_save!
    end
  end

  # Reset tries to rewind to `value-1`. This will only succeed,
  # if `value` stored in database is equal to `last_value`.
  # value: The expected last_value to decrement
  # rubocop: disable CodeReuse/ActiveRecord
  def reset(value)
    return false unless value

    updated =
      InternalId
      .where(**scope, usage: usage_value)
      .where(last_value: value)
      .update_all('last_value = last_value - 1')

    updated > 0
  end
  # rubocop: enable CodeReuse/ActiveRecord

  # Create a record in internal_ids if one does not yet exist
  # and set its new_value if it is higher than the current last_value
  #
  # Note this will acquire a ROW SHARE lock on the InternalId record
  def track_greatest(init, new_value)
    subject.transaction do
      (lookup || create_record(init)).track_greatest_and_save!(new_value)
    end
  end

  private

  # Retrieve InternalId record for (project, usage) combination, if it exists
  def lookup
    InternalId.find_by(**scope, usage: usage_value) # rubocop: disable CodeReuse/ActiveRecord
  end

  def usage_value
    @usage_value ||= InternalId.usages[usage.to_s]
  end

  # Create InternalId record for (scope, usage) combination, if it doesn't exist
  #
  # We blindly insert without synchronization. If another process
  # was faster in doing this, we'll realize once we hit the unique key constraint
  # violation. We can safely roll-back the nested transaction and perform
  # a lookup instead to retrieve the record.
  def create_record(init)
    subject.transaction(requires_new: true) do
      InternalId.create!(
        **scope,
        usage: usage_value,
        last_value: init.call(subject) || 0
      )
    end
  rescue ActiveRecord::RecordNotUnique
    lookup
  end
end