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-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.
|
2018-03-06 14:09:01 -05:00
|
|
|
class InternalId < ActiveRecord::Base
|
|
|
|
belongs_to :project
|
2018-04-20 10:00:15 -04:00
|
|
|
belongs_to :namespace
|
2018-03-06 14:09:01 -05:00
|
|
|
|
2018-04-20 10:00:15 -04:00
|
|
|
enum usage: { issues: 0, merge_requests: 1, deployments: 2, milestones: 3, epics: 4 }
|
2018-03-06 14:09:01 -05:00
|
|
|
|
|
|
|
validates :usage, presence: true
|
|
|
|
|
2018-03-13 11:53:55 -04:00
|
|
|
REQUIRED_SCHEMA_VERSION = 20180305095250
|
|
|
|
|
2018-03-06 14:09:01 -05:00
|
|
|
# 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-04-16 05:19:56 -04:00
|
|
|
#
|
|
|
|
# If a `maximum_iid` is passed in, this overrides the incremented value if it's
|
|
|
|
# greater than that. This can be used to correct the increment value if necessary.
|
|
|
|
def increment_and_save!(maximum_iid)
|
2018-03-06 14:09:01 -05:00
|
|
|
lock!
|
2018-04-16 05:19:56 -04:00
|
|
|
self.last_value = [(last_value || 0) + 1, (maximum_iid || 0) + 1].max
|
2018-03-06 14:09:01 -05:00
|
|
|
save!
|
|
|
|
last_value
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
2018-03-12 11:51:38 -04:00
|
|
|
def generate_next(subject, scope, usage, init)
|
2018-03-13 11:53:55 -04:00
|
|
|
# Shortcut if `internal_ids` table is not available (yet)
|
|
|
|
# This can be the case in other (unrelated) migration specs
|
|
|
|
return (init.call(subject) || 0) + 1 unless available?
|
|
|
|
|
2018-03-12 11:51:38 -04:00
|
|
|
InternalIdGenerator.new(subject, scope, usage, init).generate
|
|
|
|
end
|
2018-03-13 11:53:55 -04:00
|
|
|
|
|
|
|
def available?
|
|
|
|
@available_flag ||= ActiveRecord::Migrator.current_version >= REQUIRED_SCHEMA_VERSION # rubocop:disable Gitlab/PredicateMemoization
|
|
|
|
end
|
|
|
|
|
|
|
|
# Flushes cached information about schema
|
|
|
|
def reset_column_information
|
|
|
|
@available_flag = nil
|
|
|
|
super
|
|
|
|
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
|
|
|
#
|
|
|
|
# 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
|
2018-03-13 11:53:55 -04:00
|
|
|
# init: Block that gets called to initialize InternalId record if not present
|
2018-03-16 08:34:08 -04:00
|
|
|
# Make sure to not throw exceptions in the absence of records (if this is expected).
|
2018-03-12 11:51:38 -04:00
|
|
|
attr_reader :subject, :scope, :init, :scope_attrs, :usage
|
2018-03-14 08:42:03 -04:00
|
|
|
|
2018-03-12 11:51:38 -04:00
|
|
|
def initialize(subject, scope, usage, init)
|
|
|
|
@subject = subject
|
|
|
|
@scope = scope
|
2018-03-13 11:53:55 -04:00
|
|
|
@init = init
|
2018-03-12 11:51:38 -04:00
|
|
|
@usage = usage
|
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
|
|
|
|
def generate
|
|
|
|
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
|
2018-04-16 05:19:56 -04:00
|
|
|
|
|
|
|
# Note we always calculate the maximum iid present here and
|
|
|
|
# pass it in to correct the InternalId entry if it's last_value is off.
|
|
|
|
#
|
|
|
|
# This can happen in a transition phase where both `AtomicInternalId` and
|
|
|
|
# `NonatomicInternalId` code runs (e.g. during a deploy).
|
|
|
|
#
|
|
|
|
# This is subject to be cleaned up with the 10.8 release:
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab-ce/issues/45389.
|
|
|
|
(lookup || create_record).increment_and_save!(maximum_iid)
|
2018-03-06 14:09:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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.
|
2018-03-12 11:51:38 -04:00
|
|
|
def create_record
|
|
|
|
subject.transaction(requires_new: true) do
|
|
|
|
InternalId.create!(
|
|
|
|
**scope,
|
|
|
|
usage: usage_value,
|
2018-04-16 05:19:56 -04:00
|
|
|
last_value: maximum_iid
|
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
|
2018-04-16 05:19:56 -04:00
|
|
|
|
|
|
|
def maximum_iid
|
|
|
|
@maximum_iid ||= init.call(subject) || 0
|
|
|
|
end
|
2018-03-06 14:09:01 -05:00
|
|
|
end
|
|
|
|
end
|