diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-14 03:09:30 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-14 03:09:30 +0300 |
commit | 8957ace3159e5369a700a77614493ed6a8a98f93 (patch) | |
tree | 98ff5be0caa30cfebb4e0cd0ae2ceaf21ce92eb4 /lib/gitlab/github_import | |
parent | 232e0a31f1e5d5b3a788dfc3dba8f8d41df36bf9 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/github_import')
-rw-r--r-- | lib/gitlab/github_import/caching.rb | 151 | ||||
-rw-r--r-- | lib/gitlab/github_import/issuable_finder.rb | 4 | ||||
-rw-r--r-- | lib/gitlab/github_import/label_finder.rb | 4 | ||||
-rw-r--r-- | lib/gitlab/github_import/milestone_finder.rb | 4 | ||||
-rw-r--r-- | lib/gitlab/github_import/page_counter.rb | 4 | ||||
-rw-r--r-- | lib/gitlab/github_import/parallel_scheduling.rb | 6 | ||||
-rw-r--r-- | lib/gitlab/github_import/user_finder.rb | 10 |
7 files changed, 16 insertions, 167 deletions
diff --git a/lib/gitlab/github_import/caching.rb b/lib/gitlab/github_import/caching.rb deleted file mode 100644 index b08f133794f..00000000000 --- a/lib/gitlab/github_import/caching.rb +++ /dev/null @@ -1,151 +0,0 @@ -# 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 diff --git a/lib/gitlab/github_import/issuable_finder.rb b/lib/gitlab/github_import/issuable_finder.rb index c81603a1aa9..136531505ea 100644 --- a/lib/gitlab/github_import/issuable_finder.rb +++ b/lib/gitlab/github_import/issuable_finder.rb @@ -23,7 +23,7 @@ module Gitlab # # This method will return `nil` if no ID could be found. def database_id - val = Caching.read(cache_key) + val = Gitlab::Cache::Import::Caching.read(cache_key) val.to_i if val.present? end @@ -32,7 +32,7 @@ module Gitlab # # database_id - The ID of the corresponding database row. def cache_database_id(database_id) - Caching.write(cache_key, database_id) + Gitlab::Cache::Import::Caching.write(cache_key, database_id) end private diff --git a/lib/gitlab/github_import/label_finder.rb b/lib/gitlab/github_import/label_finder.rb index cad39e48e43..39e669dbba4 100644 --- a/lib/gitlab/github_import/label_finder.rb +++ b/lib/gitlab/github_import/label_finder.rb @@ -15,7 +15,7 @@ module Gitlab # Returns the label ID for the given name. def id_for(name) - Caching.read_integer(cache_key_for(name)) + Gitlab::Cache::Import::Caching.read_integer(cache_key_for(name)) end # rubocop: disable CodeReuse/ActiveRecord @@ -27,7 +27,7 @@ module Gitlab hash[cache_key_for(name)] = id end - Caching.write_multiple(mapping) + Gitlab::Cache::Import::Caching.write_multiple(mapping) end # rubocop: enable CodeReuse/ActiveRecord diff --git a/lib/gitlab/github_import/milestone_finder.rb b/lib/gitlab/github_import/milestone_finder.rb index a157a1e1ff5..d9290e36ea1 100644 --- a/lib/gitlab/github_import/milestone_finder.rb +++ b/lib/gitlab/github_import/milestone_finder.rb @@ -18,7 +18,7 @@ module Gitlab def id_for(issuable) return unless issuable.milestone_number - Caching.read_integer(cache_key_for(issuable.milestone_number)) + Gitlab::Cache::Import::Caching.read_integer(cache_key_for(issuable.milestone_number)) end # rubocop: disable CodeReuse/ActiveRecord @@ -30,7 +30,7 @@ module Gitlab hash[cache_key_for(iid)] = id end - Caching.write_multiple(mapping) + Gitlab::Cache::Import::Caching.write_multiple(mapping) end # rubocop: enable CodeReuse/ActiveRecord diff --git a/lib/gitlab/github_import/page_counter.rb b/lib/gitlab/github_import/page_counter.rb index a3e7b3c1afc..3b4fd42ba2a 100644 --- a/lib/gitlab/github_import/page_counter.rb +++ b/lib/gitlab/github_import/page_counter.rb @@ -19,12 +19,12 @@ module Gitlab # # Returns true if the page number was overwritten, false otherwise. def set(page) - Caching.write_if_greater(cache_key, page) + Gitlab::Cache::Import::Caching.write_if_greater(cache_key, page) end # Returns the current value from the cache. def current - Caching.read_integer(cache_key) || 1 + Gitlab::Cache::Import::Caching.read_integer(cache_key) || 1 end end end diff --git a/lib/gitlab/github_import/parallel_scheduling.rb b/lib/gitlab/github_import/parallel_scheduling.rb index 849a66d47ed..cabc615ea11 100644 --- a/lib/gitlab/github_import/parallel_scheduling.rb +++ b/lib/gitlab/github_import/parallel_scheduling.rb @@ -42,7 +42,7 @@ module Gitlab # still scheduling duplicates while. Since all work has already been # completed those jobs will just cycle through any remaining pages while # not scheduling anything. - Caching.expire(already_imported_cache_key, 15.minutes.to_i) + Gitlab::Cache::Import::Caching.expire(already_imported_cache_key, 15.minutes.to_i) retval end @@ -112,14 +112,14 @@ module Gitlab def already_imported?(object) id = id_for_already_imported_cache(object) - Caching.set_includes?(already_imported_cache_key, id) + Gitlab::Cache::Import::Caching.set_includes?(already_imported_cache_key, id) end # Marks the given object as "already imported". def mark_as_imported(object) id = id_for_already_imported_cache(object) - Caching.set_add(already_imported_cache_key, id) + Gitlab::Cache::Import::Caching.set_add(already_imported_cache_key, id) end # Returns the ID to use for the cache used for checking if an object has diff --git a/lib/gitlab/github_import/user_finder.rb b/lib/gitlab/github_import/user_finder.rb index 51a532437bd..9da986ae921 100644 --- a/lib/gitlab/github_import/user_finder.rb +++ b/lib/gitlab/github_import/user_finder.rb @@ -102,11 +102,11 @@ module Gitlab def email_for_github_username(username) cache_key = EMAIL_FOR_USERNAME_CACHE_KEY % username - email = Caching.read(cache_key) + email = Gitlab::Cache::Import::Caching.read(cache_key) unless email user = client.user(username) - email = Caching.write(cache_key, user.email) if user + email = Gitlab::Cache::Import::Caching.write(cache_key, user.email) if user end email @@ -125,7 +125,7 @@ module Gitlab def id_for_github_id(id) gitlab_id = query_id_for_github_id(id) || nil - Caching.write(ID_CACHE_KEY % id, gitlab_id) + Gitlab::Cache::Import::Caching.write(ID_CACHE_KEY % id, gitlab_id) end # Queries and caches the GitLab user ID for a GitHub email, if one was @@ -133,7 +133,7 @@ module Gitlab def id_for_github_email(email) gitlab_id = query_id_for_github_email(email) || nil - Caching.write(ID_FOR_EMAIL_CACHE_KEY % email, gitlab_id) + Gitlab::Cache::Import::Caching.write(ID_FOR_EMAIL_CACHE_KEY % email, gitlab_id) end # rubocop: disable CodeReuse/ActiveRecord @@ -155,7 +155,7 @@ module Gitlab # 1. A boolean indicating if the key was present or not. # 2. The ID as an Integer, or nil in case no ID could be found. def read_id_from_cache(key) - value = Caching.read(key) + value = Gitlab::Cache::Import::Caching.read(key) exists = !value.nil? number = value.to_i |