2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-06 14:09:01 -05:00
|
|
|
# An InternalId is a strictly monotone sequence of integers
|
2018-03-12 11:51:38 -04:00
|
|
|
# generated for a given scope and usage.
|
2018-03-06 14:09:01 -05:00
|
|
|
#
|
2018-08-01 05:03:14 -04:00
|
|
|
# The monotone sequence may be broken if an ID is explicitly provided
|
|
|
|
# to `.track_greatest_and_save!` or `#track_greatest`.
|
|
|
|
#
|
2018-03-12 11:51:38 -04:00
|
|
|
# For example, issues use their project to scope internal ids:
|
|
|
|
# In that sense, scope is "project" and usage is "issues".
|
|
|
|
# Generated internal ids for an issue are unique per project.
|
|
|
|
#
|
|
|
|
# See InternalId#usage enum for available usages.
|
|
|
|
#
|
|
|
|
# In order to leverage InternalId for other usages, the idea is to
|
|
|
|
# * Add `usage` value to enum
|
|
|
|
# * (Optionally) add columns to `internal_ids` if needed for scope.
|
2019-03-28 09:17:42 -04:00
|
|
|
class InternalId < ApplicationRecord
|
2019-09-20 11:06:12 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2018-03-06 14:09:01 -05:00
|
|
|
belongs_to :project
|
2018-04-20 10:00:15 -04:00
|
|
|
belongs_to :namespace
|
2018-03-06 14:09:01 -05:00
|
|
|
|
2020-08-24 17:10:17 -04:00
|
|
|
enum usage: Enums::InternalId.usage_resources
|
2018-03-06 14:09:01 -05:00
|
|
|
|
|
|
|
validates :usage, presence: true
|
|
|
|
|
|
|
|
# Increments #last_value and saves the record
|
|
|
|
#
|
2018-03-12 11:51:38 -04:00
|
|
|
# The operation locks the record and gathers a `ROW SHARE` lock (in PostgreSQL).
|
|
|
|
# As such, the increment is atomic and safe to be called concurrently.
|
2018-05-28 06:44:07 -04:00
|
|
|
def increment_and_save!
|
2018-08-01 05:03:14 -04:00
|
|
|
update_and_save { self.last_value = (last_value || 0) + 1 }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Increments #last_value with new_value if it is greater than the current,
|
|
|
|
# and saves the record
|
|
|
|
#
|
|
|
|
# The operation locks the record and gathers a `ROW SHARE` lock (in PostgreSQL).
|
|
|
|
# As such, the increment is atomic and safe to be called concurrently.
|
|
|
|
def track_greatest_and_save!(new_value)
|
|
|
|
update_and_save { self.last_value = [last_value || 0, new_value].max }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_and_save(&block)
|
2018-03-06 14:09:01 -05:00
|
|
|
lock!
|
2018-08-01 05:03:14 -04:00
|
|
|
yield
|
2019-09-20 11:06:12 -04:00
|
|
|
update_and_save_counter.increment(usage: usage, changed: last_value_changed?)
|
2018-03-06 14:09:01 -05:00
|
|
|
save!
|
|
|
|
last_value
|
|
|
|
end
|
|
|
|
|
2019-10-15 17:06:25 -04:00
|
|
|
# Instrumentation to track for-update locks
|
2019-09-20 11:06:12 -04:00
|
|
|
def update_and_save_counter
|
|
|
|
strong_memoize(:update_and_save_counter) do
|
|
|
|
Gitlab::Metrics.counter(:gitlab_internal_id_for_update_lock, 'Number of ROW SHARE (FOR UPDATE) locks on individual records from internal_ids')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-06 14:09:01 -05:00
|
|
|
class << self
|
2018-08-01 05:03:14 -04:00
|
|
|
def track_greatest(subject, scope, usage, new_value, init)
|
2020-11-10 19:08:58 -05:00
|
|
|
InternalIdGenerator.new(subject, scope, usage, init)
|
|
|
|
.track_greatest(new_value)
|
2018-08-01 05:03:14 -04:00
|
|
|
end
|
|
|
|
|
2018-03-12 11:51:38 -04:00
|
|
|
def generate_next(subject, scope, usage, init)
|
2020-11-10 19:08:58 -05:00
|
|
|
InternalIdGenerator.new(subject, scope, usage, init)
|
|
|
|
.generate
|
2019-03-22 11:51:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset(subject, scope, usage, value)
|
|
|
|
InternalIdGenerator.new(subject, scope, usage)
|
|
|
|
.reset(value)
|
2018-03-12 11:51:38 -04:00
|
|
|
end
|
2018-03-13 11:53:55 -04:00
|
|
|
|
2019-01-02 10:50:37 -05:00
|
|
|
# Flushing records is generally safe in a sense that those
|
|
|
|
# records are going to be re-created when needed.
|
|
|
|
#
|
|
|
|
# A filter condition has to be provided to not accidentally flush
|
|
|
|
# records for all projects.
|
|
|
|
def flush_records!(filter)
|
|
|
|
raise ArgumentError, "filter cannot be empty" if filter.blank?
|
|
|
|
|
|
|
|
where(filter).delete_all
|
|
|
|
end
|
2018-03-12 11:51:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class InternalIdGenerator
|
|
|
|
# Generate next internal id for a given scope and usage.
|
2018-03-06 14:09:01 -05:00
|
|
|
#
|
|
|
|
# For currently supported usages, see #usage enum.
|
|
|
|
#
|
|
|
|
# The method implements a locking scheme that has the following properties:
|
2018-03-12 11:51:38 -04:00
|
|
|
# 1) Generated sequence of internal ids is unique per (scope and usage)
|
2018-03-06 14:09:01 -05:00
|
|
|
# 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.
|
2018-03-12 11:51:38 -04:00
|
|
|
#
|
2020-11-10 19:08:58 -05:00
|
|
|
# subject: The instance or class we're generating an internal id for.
|
2018-03-12 11:51:38 -04:00
|
|
|
# scope: Attributes that define the scope for id generation.
|
2020-11-10 19:08:58 -05:00
|
|
|
# Valid keys are `project/project_id` and `namespace/namespace_id`.
|
2018-03-12 11:51:38 -04:00
|
|
|
# usage: Symbol to define the usage of the internal id, see InternalId.usages
|
2020-11-10 19:08:58 -05:00
|
|
|
# init: Proc that accepts the subject and the scope and returns Integer|NilClass
|
|
|
|
attr_reader :subject, :scope, :scope_attrs, :usage, :init
|
2018-03-14 08:42:03 -04:00
|
|
|
|
2020-11-10 19:08:58 -05:00
|
|
|
def initialize(subject, scope, usage, init = nil)
|
2018-03-12 11:51:38 -04:00
|
|
|
@subject = subject
|
|
|
|
@scope = scope
|
|
|
|
@usage = usage
|
2020-11-10 19:08:58 -05:00
|
|
|
@init = init
|
2018-03-12 10:38:56 -04:00
|
|
|
|
2018-03-16 08:34:08 -04:00
|
|
|
raise ArgumentError, 'Scope is not well-defined, need at least one column for scope (given: 0)' if scope.empty?
|
2018-03-12 10:38:56 -04:00
|
|
|
|
2018-03-16 08:34:08 -04:00
|
|
|
unless InternalId.usages.has_key?(usage.to_s)
|
2018-03-14 08:42:03 -04:00
|
|
|
raise ArgumentError, "Usage '#{usage}' is unknown. Supported values are #{InternalId.usages.keys} from InternalId.usages"
|
2018-03-12 10:38:56 -04:00
|
|
|
end
|
2018-03-12 11:51:38 -04:00
|
|
|
end
|
2018-03-06 14:09:01 -05:00
|
|
|
|
2018-03-12 11:51:38 -04:00
|
|
|
# Generates next internal id and returns it
|
2019-03-22 11:51:15 -04:00
|
|
|
# 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).
|
2020-11-10 19:08:58 -05:00
|
|
|
def generate
|
2019-01-02 10:05:40 -05:00
|
|
|
subject.transaction do
|
2018-03-06 14:09:01 -05:00
|
|
|
# Create a record in internal_ids if one does not yet exist
|
2018-03-16 08:34:08 -04:00
|
|
|
# and increment its last value
|
2018-03-12 11:51:38 -04:00
|
|
|
#
|
|
|
|
# Note this will acquire a ROW SHARE lock on the InternalId record
|
2020-11-10 19:08:58 -05:00
|
|
|
record.increment_and_save!
|
2018-03-06 14:09:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-22 11:51:15 -04:00
|
|
|
# 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
|
|
|
|
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
|
|
|
|
|
2018-08-01 05:03:14 -04:00
|
|
|
# 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
|
2020-11-10 19:08:58 -05:00
|
|
|
def track_greatest(new_value)
|
2019-01-02 10:05:40 -05:00
|
|
|
subject.transaction do
|
2020-11-10 19:08:58 -05:00
|
|
|
record.track_greatest_and_save!(new_value)
|
2018-08-01 05:03:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-10 19:08:58 -05:00
|
|
|
def record
|
|
|
|
@record ||= (lookup || create_record)
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_lock(&block)
|
|
|
|
record.with_lock(&block)
|
|
|
|
end
|
|
|
|
|
2018-03-06 14:09:01 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
# Retrieve InternalId record for (project, usage) combination, if it exists
|
2018-03-12 11:51:38 -04:00
|
|
|
def lookup
|
|
|
|
InternalId.find_by(**scope, usage: usage_value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def usage_value
|
|
|
|
@usage_value ||= InternalId.usages[usage.to_s]
|
2018-03-06 14:09:01 -05:00
|
|
|
end
|
|
|
|
|
2018-03-12 11:51:38 -04:00
|
|
|
# Create InternalId record for (scope, usage) combination, if it doesn't exist
|
2018-03-06 14:09:01 -05:00
|
|
|
#
|
2018-03-12 11:51:38 -04:00
|
|
|
# We blindly insert without synchronization. If another process
|
2018-03-06 14:09:01 -05:00
|
|
|
# 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.
|
2020-11-10 19:08:58 -05:00
|
|
|
def create_record
|
|
|
|
raise ArgumentError, 'Cannot initialize without init!' unless init
|
|
|
|
|
|
|
|
instance = subject.is_a?(::Class) ? nil : subject
|
|
|
|
|
2019-01-02 10:05:40 -05:00
|
|
|
subject.transaction(requires_new: true) do
|
2018-03-12 11:51:38 -04:00
|
|
|
InternalId.create!(
|
|
|
|
**scope,
|
|
|
|
usage: usage_value,
|
2020-11-10 19:08:58 -05:00
|
|
|
last_value: init.call(instance, scope) || 0
|
2018-03-12 11:51:38 -04:00
|
|
|
)
|
2018-03-06 14:09:01 -05:00
|
|
|
end
|
2018-03-12 11:51:38 -04:00
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
lookup
|
2018-03-06 14:09:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|