4dfe26cd8b
Prior to this MR there were two GitHub related importers: * Github::Import: the main importer used for GitHub projects * Gitlab::GithubImport: importer that's somewhat confusingly used for importing Gitea projects (apparently they have a compatible API) This MR renames the Gitea importer to Gitlab::LegacyGithubImport and introduces a new GitHub importer in the Gitlab::GithubImport namespace. This new GitHub importer uses Sidekiq for importing multiple resources in parallel, though it also has the ability to import data sequentially should this be necessary. The new code is spread across the following directories: * lib/gitlab/github_import: this directory contains most of the importer code such as the classes used for importing resources. * app/workers/gitlab/github_import: this directory contains the Sidekiq workers, most of which simply use the code from the directory above. * app/workers/concerns/gitlab/github_import: this directory provides a few modules that are included in every GitHub importer worker. == Stages The import work is divided into separate stages, with each stage importing a specific set of data. Stages will schedule the work that needs to be performed, followed by scheduling a job for the "AdvanceStageWorker" worker. This worker will periodically check if all work is completed and schedule the next stage if this is the case. If work is not yet completed this worker will reschedule itself. Using this approach we don't have to block threads by calling `sleep()`, as doing so for large projects could block the thread from doing any work for many hours. == Retrying Work Workers will reschedule themselves whenever necessary. For example, hitting the GitHub API's rate limit will result in jobs rescheduling themselves. These jobs are not processed until the rate limit has been reset. == User Lookups Part of the importing process involves looking up user details in the GitHub API so we can map them to GitLab users. The old importer used an in-memory cache, but this obviously doesn't work when the work is spread across different threads. The new importer uses a Redis cache and makes sure we only perform API/database calls if absolutely necessary. Frequently used keys are refreshed, and lookup misses are also cached; removing the need for performing API/database calls if we know we don't have the data we're looking for. == Performance & Models The new importer in various places uses raw INSERT statements (as generated by `Gitlab::Database.bulk_insert`) instead of using Rails models. This allows us to bypass any validations and callbacks, drastically reducing the number of SQL queries and Gitaly RPC calls necessary to import projects. To ensure the code produces valid data the corresponding tests check if the produced rows are valid according to the model validation rules.
151 lines
4.6 KiB
Ruby
151 lines
4.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Gitlab
|
|
module GithubImport
|
|
module Caching
|
|
# The default timeout of the cache keys.
|
|
TIMEOUT = 24.hours.to_i
|
|
|
|
WRITE_IF_GREATER_SCRIPT = <<-EOF.strip_heredoc.freeze
|
|
local key, value, ttl = KEYS[1], tonumber(ARGV[1]), ARGV[2]
|
|
local existing = tonumber(redis.call("get", key))
|
|
|
|
if existing == nil or value > existing then
|
|
redis.call("set", key, value)
|
|
redis.call("expire", key, ttl)
|
|
return true
|
|
else
|
|
return false
|
|
end
|
|
EOF
|
|
|
|
# Reads a cache key.
|
|
#
|
|
# If the key exists and has a non-empty value its TTL is refreshed
|
|
# automatically.
|
|
#
|
|
# raw_key - The cache key to read.
|
|
# timeout - The new timeout of the key if the key is to be refreshed.
|
|
def self.read(raw_key, timeout: TIMEOUT)
|
|
key = cache_key_for(raw_key)
|
|
value = Redis::Cache.with { |redis| redis.get(key) }
|
|
|
|
if value.present?
|
|
# We refresh the expiration time so frequently used keys stick
|
|
# around, removing the need for querying the database as much as
|
|
# possible.
|
|
#
|
|
# A key may be empty when we looked up a GitHub user (for example) but
|
|
# did not find a matching GitLab user. In that case we _don't_ want to
|
|
# refresh the TTL so we automatically pick up the right data when said
|
|
# user were to register themselves on the GitLab instance.
|
|
Redis::Cache.with { |redis| redis.expire(key, timeout) }
|
|
end
|
|
|
|
value
|
|
end
|
|
|
|
# Reads an integer from the cache, or returns nil if no value was found.
|
|
#
|
|
# See Caching.read for more information.
|
|
def self.read_integer(raw_key, timeout: TIMEOUT)
|
|
value = read(raw_key, timeout: timeout)
|
|
|
|
value.to_i if value.present?
|
|
end
|
|
|
|
# Sets a cache key to the given value.
|
|
#
|
|
# key - The cache key to write.
|
|
# value - The value to set.
|
|
# timeout - The time after which the cache key should expire.
|
|
def self.write(raw_key, value, timeout: TIMEOUT)
|
|
key = cache_key_for(raw_key)
|
|
|
|
Redis::Cache.with do |redis|
|
|
redis.set(key, value, ex: timeout)
|
|
end
|
|
|
|
value
|
|
end
|
|
|
|
# Adds a value to a set.
|
|
#
|
|
# raw_key - The key of the set to add the value to.
|
|
# value - The value to add to the set.
|
|
# timeout - The new timeout of the key.
|
|
def self.set_add(raw_key, value, timeout: TIMEOUT)
|
|
key = cache_key_for(raw_key)
|
|
|
|
Redis::Cache.with do |redis|
|
|
redis.multi do |m|
|
|
m.sadd(key, value)
|
|
m.expire(key, timeout)
|
|
end
|
|
end
|
|
end
|
|
|
|
# Returns true if the given value is present in the set.
|
|
#
|
|
# raw_key - The key of the set to check.
|
|
# value - The value to check for.
|
|
def self.set_includes?(raw_key, value)
|
|
key = cache_key_for(raw_key)
|
|
|
|
Redis::Cache.with do |redis|
|
|
redis.sismember(key, value)
|
|
end
|
|
end
|
|
|
|
# Sets multiple keys to a given value.
|
|
#
|
|
# mapping - A Hash mapping the cache keys to their values.
|
|
# timeout - The time after which the cache key should expire.
|
|
def self.write_multiple(mapping, timeout: TIMEOUT)
|
|
Redis::Cache.with do |redis|
|
|
redis.multi do |multi|
|
|
mapping.each do |raw_key, value|
|
|
multi.set(cache_key_for(raw_key), value, ex: timeout)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
# Sets the expiration time of a key.
|
|
#
|
|
# raw_key - The key for which to change the timeout.
|
|
# timeout - The new timeout.
|
|
def self.expire(raw_key, timeout)
|
|
key = cache_key_for(raw_key)
|
|
|
|
Redis::Cache.with do |redis|
|
|
redis.expire(key, timeout)
|
|
end
|
|
end
|
|
|
|
# Sets a key to the given integer but only if the existing value is
|
|
# smaller than the given value.
|
|
#
|
|
# This method uses a Lua script to ensure the read and write are atomic.
|
|
#
|
|
# raw_key - The key to set.
|
|
# value - The new value for the key.
|
|
# timeout - The key timeout in seconds.
|
|
#
|
|
# Returns true when the key was overwritten, false otherwise.
|
|
def self.write_if_greater(raw_key, value, timeout: TIMEOUT)
|
|
key = cache_key_for(raw_key)
|
|
val = Redis::Cache.with do |redis|
|
|
redis
|
|
.eval(WRITE_IF_GREATER_SCRIPT, keys: [key], argv: [value, timeout])
|
|
end
|
|
|
|
val ? true : false
|
|
end
|
|
|
|
def self.cache_key_for(raw_key)
|
|
"#{Redis::Cache::CACHE_NAMESPACE}:#{raw_key}"
|
|
end
|
|
end
|
|
end
|
|
end
|