Commit 06dfc9ab authored by Ash McKenzie's avatar Ash McKenzie

Prefix classes/modules & methods with Geo or geo_

Also included is a small refactor aimed at
improving performance and readability.
parent 419a758b
...@@ -5,22 +5,33 @@ module EE ...@@ -5,22 +5,33 @@ module EE
module GitHttpClientController module GitHttpClientController
extend ActiveSupport::Concern extend ActiveSupport::Concern
# This module is responsible for determining if an incoming secondary bound # This module is responsible for determining if an incoming Geo secondary
# HTTP request should be redirected to the primary. # bound HTTP request should be redirected to the Primary.
# #
# Why? A secondary is not allowed to perform any write actions, so any # Why? A secondary is not allowed to perform any write actions, so any
# request of this type need to be sent through to the primary. By # request of this type needs to be sent through to the Primary. By
# redirecting within code, we allow clients to git pull/push using their # redirecting within code, we allow clients to git pull/push using their
# secondary git remote without needing an additional primary remote. # secondary git remote without needing an additional primary remote.
# #
# The method for redirection *must* happen as early as possible in the
# request. For example, putting the redirection logic in #access_check
# will not work because the git client will not accept a 302 in response
# to verifying credentials.
#
# Current secondary HTTP requests to redirect: - # Current secondary HTTP requests to redirect: -
# #
# * git pull (repository is not replicated)
# * GET /namespace/repo.git/info/refs?service=git-upload-pack
#
# * git lfs pull (repository is not replicated)
# * GET /namespace/repo.git/gitlab-lfs/objects/<oid>
#
# * git push # * git push
# * GET /repo.git/info/refs?service=git-receive-pack # * GET /namespace/repo.git/info/refs?service=git-receive-pack
# * POST /repo.git/git-receive-pack # * POST /namespace/repo.git/git-receive-pack
# #
# * git lfs push (usually happens automatically as part of a `git push`) # * git lfs push (usually happens automatically as part of a `git push`)
# * POST /repo.git/info/lfs/objects/batch (and we examine # * POST /namespace/repo.git/info/lfs/objects/batch (and we examine
# params[:operation] to ensure we're dealing with an upload request) # params[:operation] to ensure we're dealing with an upload request)
# #
# For more detail, see the following links: # For more detail, see the following links:
...@@ -30,13 +41,13 @@ module EE ...@@ -30,13 +41,13 @@ module EE
# #
prepended do prepended do
prepend_before_action do prepend_before_action do
redirect_to(primary_full_url) if redirect? redirect_to(geo_primary_full_url) if geo_redirect?
end end
end end
private private
class RouteHelper class GeoRouteHelper
attr_reader :controller_name, :action_name attr_reader :controller_name, :action_name
CONTROLLER_AND_ACTIONS_TO_REDIRECT = { CONTROLLER_AND_ACTIONS_TO_REDIRECT = {
...@@ -92,11 +103,11 @@ module EE ...@@ -92,11 +103,11 @@ module EE
end end
end end
class GitLFSHelper class GeoGitLFSHelper
MINIMUM_GIT_LFS_VERSION = '2.4.2'.freeze MINIMUM_GIT_LFS_VERSION = '2.4.2'.freeze
def initialize(route_helper, operation, current_version) def initialize(geo_route_helper, operation, current_version)
@route_helper = route_helper @geo_route_helper = geo_route_helper
@operation = operation @operation = operation
@current_version = current_version @current_version = current_version
end end
...@@ -110,8 +121,7 @@ module EE ...@@ -110,8 +121,7 @@ module EE
end end
def redirect? def redirect?
return false unless route_helper.match?('lfs_api', 'batch') return true if batch_upload?
return true if upload?
false false
end end
...@@ -124,15 +134,19 @@ module EE ...@@ -124,15 +134,19 @@ module EE
private private
attr_reader :route_helper, :operation, :current_version attr_reader :geo_route_helper, :operation, :current_version
def incorrect_version_message def incorrect_version_message
translation = _("You need git-lfs version %{min_git_lfs_version} (or greater) to continue. Please visit https://git-lfs.github.com") translation = _("You need git-lfs version %{min_git_lfs_version} (or greater) to continue. Please visit https://git-lfs.github.com")
translation % { min_git_lfs_version: MINIMUM_GIT_LFS_VERSION } translation % { min_git_lfs_version: MINIMUM_GIT_LFS_VERSION }
end end
def upload? def batch_request?
operation == 'upload' geo_route_helper.match?('lfs_api', 'batch')
end
def batch_upload?
batch_request? && operation == 'upload'
end end
def wanted_version def wanted_version
...@@ -140,52 +154,45 @@ module EE ...@@ -140,52 +154,45 @@ module EE
end end
end end
def route_helper def geo_route_helper
@route_helper ||= RouteHelper.new(controller_name, action_name, params[:service]) @geo_route_helper ||= GeoRouteHelper.new(controller_name, action_name, params[:service])
end end
def git_lfs_helper def geo_git_lfs_helper
# params[:operation] explained: https://github.com/git-lfs/git-lfs/blob/master/docs/api/batch.md#requests # params[:operation] explained: https://github.com/git-lfs/git-lfs/blob/master/docs/api/batch.md#requests
@git_lfs_helper ||= GitLFSHelper.new(route_helper, params[:operation], request.headers['User-Agent']) @geo_git_lfs_helper ||= GeoGitLFSHelper.new(geo_route_helper, params[:operation], request.headers['User-Agent'])
end end
def request_fullpath_for_primary def geo_request_fullpath_for_primary
relative_url_root = ::Gitlab.config.gitlab.relative_url_root.chomp('/') relative_url_root = ::Gitlab.config.gitlab.relative_url_root.chomp('/')
request.fullpath.sub(relative_url_root, '') request.fullpath.sub(relative_url_root, '')
end end
def primary_full_url def geo_primary_full_url
path = File.join(secondary_referrer_path_prefix, request_fullpath_for_primary) path = File.join(geo_secondary_referrer_path_prefix, geo_request_fullpath_for_primary)
::Gitlab::Utils.append_path(::Gitlab::Geo.primary_node.internal_url, path) ::Gitlab::Utils.append_path(::Gitlab::Geo.primary_node.internal_url, path)
end end
def secondary_referrer_path_prefix def geo_secondary_referrer_path_prefix
File.join(::Gitlab::Geo::GitPushHttp::PATH_PREFIX, ::Gitlab::Geo.current_node.id.to_s) File.join(::Gitlab::Geo::GitPushHttp::PATH_PREFIX, ::Gitlab::Geo.current_node.id.to_s)
end end
def redirect? def geo_redirect?
# Don't redirect if we're not a secondary with a primary
return false unless ::Gitlab::Geo.secondary_with_primary? return false unless ::Gitlab::Geo.secondary_with_primary?
return true if geo_route_helper.redirect?
# Redirect as the request matches RouteHelper::CONTROLLER_AND_ACTIONS_TO_REDIRECT if geo_git_lfs_helper.redirect?
return true if route_helper.redirect? return true if geo_git_lfs_helper.version_ok?
# Look to redirect, as we're an LFS batch upload request
if git_lfs_helper.redirect?
# Redirect as git-lfs version is at least 2.4.2
return true if git_lfs_helper.version_ok?
# git-lfs 2.4.2 is really only required for requests that involve # git-lfs 2.4.2 is really only required for requests that involve
# redirection, so we only render if it's an LFS upload operation # redirection, so we only render if it's an LFS upload operation
# #
render(git_lfs_helper.incorrect_version_response) render(geo_git_lfs_helper.incorrect_version_response)
# Don't redirect
return false return false
end end
# Don't redirect
false false
end end
end end
......
...@@ -8,6 +8,7 @@ module EE ...@@ -8,6 +8,7 @@ module EE
prepended do prepended do
helpers do helpers do
include ::Gitlab::Utils::StrongMemoize
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
override :lfs_authentication_url override :lfs_authentication_url
...@@ -17,22 +18,27 @@ module EE ...@@ -17,22 +18,27 @@ module EE
override :ee_post_receive_response_hook override :ee_post_receive_response_hook
def ee_post_receive_response_hook(response) def ee_post_receive_response_hook(response)
response.add_basic_message(geo_secondary_lag_message) if ::Gitlab::Geo.primary? response.add_basic_message(geo_secondary_lag_message) if geo_display_secondary_lag_message?
end end
def geo_secondary_lag_message def geo_display_secondary_lag_message?
lag = current_replication_lag ::Gitlab::Geo.primary? && geo_current_replication_lag.to_i > 0
return if lag.to_i <= 0 end
"Current replication lag: #{lag} seconds" def geo_secondary_lag_message
"Current replication lag: #{geo_current_replication_lag} seconds"
end end
def current_replication_lag def geo_current_replication_lag
fetch_geo_node_referrer&.status&.db_replication_lag_seconds strong_memoize(:geo_current_replication_lag) do
geo_referred_node&.status&.db_replication_lag_seconds
end
end end
def fetch_geo_node_referrer def geo_referred_node
::Gitlab::Geo::GitPushHttp.new(params[:identifier], params[:gl_repository]).fetch_referrer_node strong_memoize(:geo_referred_node) do
::Gitlab::Geo::GitPushHttp.new(params[:identifier], params[:gl_repository]).fetch_referrer_node
end
end end
override :check_allowed override :check_allowed
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment