Commit 5118ca34 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu Committed by Alex Kalderimis

Enable Zeitwerk autoloader

Switches from Rails classic autoloader to Zeitwerk
parent ad862f26
......@@ -2305,7 +2305,6 @@ Gitlab/NamespacedClass:
- 'ee/app/workers/sync_security_reports_to_report_approval_rules_worker.rb'
- 'ee/app/workers/update_all_mirrors_worker.rb'
- 'ee/app/workers/update_max_seats_used_for_gitlab_com_subscriptions_worker.rb'
- 'ee/lib/generators/rails/geo_migration_generator.rb'
- 'ee/lib/gitlab/path_locks_finder.rb'
- 'ee/spec/support/elastic_query_name_inspector.rb'
- 'ee/spec/support/ssh_keygen.rb'
......
......@@ -32,7 +32,7 @@ module Gitlab
require_dependency Rails.root.join('lib/gitlab/middleware/rack_multipart_tempfile_factory')
require_dependency Rails.root.join('lib/gitlab/runtime')
config.autoloader = :classic
config.autoloader = :zeitwerk
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
......@@ -86,7 +86,11 @@ module Gitlab
# Rake tasks ignore the eager loading settings, so we need to set the
# autoload paths explicitly
config.autoload_paths = config.eager_load_paths.dup
# These are only used in Rake tasks so we don't need to add these to eager_load_paths
config.autoload_paths.push("#{config.root}/lib/generators")
Gitlab.ee { config.autoload_paths.push("#{config.root}/ee/lib/generators") }
Gitlab.jh { config.autoload_paths.push("#{config.root}/jh/lib/generators") }
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
......
# frozen_string_literal: true
require_dependency 'gitlab'
......@@ -5,15 +5,11 @@ Rails.autoloaders.each do |autoloader|
# that do not define Ruby classes / modules
autoloader.ignore(Rails.root.join('lib/support'))
# Ignore generators since these are loaded manually by Rails
# https://github.com/rails/rails/blob/v6.1.3.2/railties/lib/rails/command/behavior.rb#L56-L65
autoloader.ignore(Rails.root.join('lib/generators'))
autoloader.ignore(Rails.root.join('ee/lib/generators')) if Gitlab.ee?
# Mailer previews are also loaded manually by Rails
# Mailer previews are loaded manually by Rails
# https://github.com/rails/rails/blob/v6.1.3.2/actionmailer/lib/action_mailer/preview.rb#L121-L125
autoloader.ignore(Rails.root.join('app/mailers/previews'))
autoloader.ignore(Rails.root.join('ee/app/mailers/previews')) if Gitlab.ee?
autoloader.ignore(Rails.root.join('jh/app/mailers/previews')) if Gitlab.jh?
autoloader.inflector.inflect(
'api' => 'API',
......
......@@ -389,7 +389,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
namespace :design_management do
namespace :designs, path: 'designs/:design_id(/:sha)', constraints: -> (params) { params[:sha].nil? || Gitlab::Git.commit_id?(params[:sha]) } do
resource :raw_image, only: :show
resources :resized_image, only: :show, constraints: -> (params) { DesignManagement::DESIGN_IMAGE_SIZES.include?(params[:id]) }
resources :resized_image, only: :show, constraints: -> (params) { ::DesignManagement::DESIGN_IMAGE_SIZES.include?(params[:id]) }
end
end
......
......@@ -3,12 +3,6 @@
require 'settingslogic'
require 'digest/md5'
# We can not use `Rails.root` here, as this file might be loaded without the
# full Rails environment being loaded. We can not use `require_relative` either,
# as Rails uses `load` for `require_dependency` (used when loading the Rails
# environment). This could then lead to this file being loaded twice.
require_dependency File.expand_path('../lib/gitlab', __dir__)
class Settings < Settingslogic
source ENV.fetch('GITLAB_CONFIG') { Pathname.new(File.expand_path('..', __dir__)).join('config/gitlab.yml') }
namespace ENV.fetch('GITLAB_ENV') { Rails.env }
......
......@@ -15,9 +15,9 @@ module EE
end
def geo_connection_stats
return [] unless Geo::TrackingBase.connected?
return [] unless ::Geo::TrackingBase.connected?
[{ labels: labels_for_class(Geo::TrackingBase), stats: Geo::TrackingBase.connection_pool.stat }]
[{ labels: labels_for_class(::Geo::TrackingBase), stats: ::Geo::TrackingBase.connection_pool.stat }]
end
end
end
......
......@@ -4,7 +4,8 @@ require 'rails/generators'
require 'rails/generators/active_record'
require 'rails/generators/active_record/migration/migration_generator'
class GeoMigrationGenerator < ActiveRecord::Generators::MigrationGenerator
module GeoMigration
class GeoMigrationGenerator < ActiveRecord::Generators::MigrationGenerator
source_root File.join(Rails.root, 'generator_templates/active_record/migration')
def create_migration_file
......@@ -12,4 +13,5 @@ class GeoMigrationGenerator < ActiveRecord::Generators::MigrationGenerator
validate_file_name!
migration_template @migration_template, "ee/db/geo/migrate/#{file_name}.rb"
end
end
end
......@@ -11,7 +11,7 @@ module GemExtensions
@relation_delegate_cache2 = {} # rubocop:disable Gitlab/ModuleWithInstanceVariables
[
DisableJoins::Relation
::GemExtensions::ActiveRecord::DisableJoins::Relation
].each do |klass|
delegate = Class.new(klass) do
include ::ActiveRecord::Delegation::ClassSpecificRelation
......
......@@ -17,7 +17,7 @@ module Gitlab
ALLOWED_OPERATIONS = %w(count distinct_count).freeze
source_root File.expand_path('templates', __dir__)
source_root File.expand_path('usage_metric/templates', __dir__)
class_option :ee, type: :boolean, optional: true, default: false, desc: 'Indicates if instrumentation is for EE'
class_option :type, type: :string, desc: "Metric type, must be one of: #{ALLOWED_SUPERCLASSES.keys.join(', ')}"
......
......@@ -2,7 +2,7 @@
require 'rails/generators'
module Rails
module PostDeploymentMigration
class PostDeploymentMigrationGenerator < Rails::Generators::NamedBase
def create_migration_file
timestamp = Time.now.utc.strftime('%Y%m%d%H%M%S')
......
......@@ -29,11 +29,11 @@ module Gitlab
@host = host
@port = port
@load_balancer = load_balancer
@pool = load_balancer.create_replica_connection_pool(LoadBalancing.pool_size, host, port)
@pool = load_balancer.create_replica_connection_pool(::Gitlab::Database::LoadBalancing.pool_size, host, port)
@online = true
@last_checked_at = Time.zone.now
interval = LoadBalancing.replica_check_interval
interval = ::Gitlab::Database::LoadBalancing.replica_check_interval
@intervals = (interval..(interval * 2)).step(0.5).to_a
end
......@@ -54,7 +54,7 @@ module Gitlab
end
def offline!
LoadBalancing::Logger.warn(
::Gitlab::Database::LoadBalancing::Logger.warn(
event: :host_offline,
message: 'Marking host as offline',
db_host: @host,
......@@ -72,14 +72,14 @@ module Gitlab
refresh_status
if @online
LoadBalancing::Logger.info(
::Gitlab::Database::LoadBalancing::Logger.info(
event: :host_online,
message: 'Host is online after replica status check',
db_host: @host,
db_port: @port
)
else
LoadBalancing::Logger.warn(
::Gitlab::Database::LoadBalancing::Logger.warn(
event: :host_offline,
message: 'Host is offline after replica status check',
db_host: @host,
......@@ -108,7 +108,7 @@ module Gitlab
def replication_lag_below_threshold?
if (lag_time = replication_lag_time)
lag_time <= LoadBalancing.max_replication_lag_time
lag_time <= ::Gitlab::Database::LoadBalancing.max_replication_lag_time
else
false
end
......@@ -125,7 +125,7 @@ module Gitlab
# only do this if we haven't replicated in a while so we only need
# to connect to the primary when truly necessary.
if (lag_size = replication_lag_size)
lag_size <= LoadBalancing.max_replication_difference
lag_size <= ::Gitlab::Database::LoadBalancing.max_replication_difference
else
false
end
......
......@@ -18,9 +18,9 @@ module Gitlab
# namespace - The namespace to use for sticking.
# id - The identifier to use for sticking.
def self.stick_or_unstick(env, namespace, id)
return unless LoadBalancing.enable?
return unless ::Gitlab::Database::LoadBalancing.enable?
Sticking.unstick_or_continue_sticking(namespace, id)
::Gitlab::Database::LoadBalancing::Sticking.unstick_or_continue_sticking(namespace, id)
env[STICK_OBJECT] ||= Set.new
env[STICK_OBJECT] << [namespace, id]
......@@ -56,7 +56,7 @@ module Gitlab
namespaces_and_ids = sticking_namespaces_and_ids(env)
namespaces_and_ids.each do |namespace, id|
Sticking.unstick_or_continue_sticking(namespace, id)
::Gitlab::Database::LoadBalancing::Sticking.unstick_or_continue_sticking(namespace, id)
end
end
......@@ -65,17 +65,17 @@ module Gitlab
namespaces_and_ids = sticking_namespaces_and_ids(env)
namespaces_and_ids.each do |namespace, id|
Sticking.stick_if_necessary(namespace, id)
::Gitlab::Database::LoadBalancing::Sticking.stick_if_necessary(namespace, id)
end
end
def clear
load_balancer.release_host
Session.clear_session
::Gitlab::Database::LoadBalancing::Session.clear_session
end
def load_balancer
LoadBalancing.proxy.load_balancer
::Gitlab::Database::LoadBalancing.proxy.load_balancer
end
# Determines the sticking namespace and identifier based on the Rack
......
......@@ -106,7 +106,7 @@ module Gitlab
end
def design?
klass == DesignManagement::Design
klass == ::DesignManagement::Design
end
def diff_commit_user?
......
......@@ -66,28 +66,28 @@ module Gitlab
def call(env)
method = env['REQUEST_METHOD'].downcase
method = 'INVALID' unless HTTP_METHODS.key?(method)
started = Gitlab::Metrics::System.monotonic_time
started = ::Gitlab::Metrics::System.monotonic_time
health_endpoint = health_endpoint?(env['PATH_INFO'])
status = 'undefined'
begin
status, headers, body = @app.call(env)
elapsed = Gitlab::Metrics::System.monotonic_time - started
elapsed = ::Gitlab::Metrics::System.monotonic_time - started
if !health_endpoint && Gitlab::Metrics.record_duration_for_status?(status)
RequestsRackMiddleware.http_request_duration_seconds.observe({ method: method }, elapsed)
if !health_endpoint && ::Gitlab::Metrics.record_duration_for_status?(status)
self.class.http_request_duration_seconds.observe({ method: method }, elapsed)
end
[status, headers, body]
rescue StandardError
RequestsRackMiddleware.rack_uncaught_errors_count.increment
self.class.rack_uncaught_errors_count.increment
raise
ensure
if health_endpoint
RequestsRackMiddleware.http_health_requests_total.increment(status: status.to_s, method: method)
self.class.http_health_requests_total.increment(status: status.to_s, method: method)
else
RequestsRackMiddleware.http_requests_total.increment(
self.class.http_requests_total.increment(
status: status.to_s,
method: method,
feature_category: feature_category.presence || FEATURE_CATEGORY_DEFAULT
......
......@@ -23,7 +23,7 @@ module Gitlab
def safe_sample
sample
rescue StandardError => e
Gitlab::AppLogger.warn("#{self.class}: #{e}, stopping")
::Gitlab::AppLogger.warn("#{self.class}: #{e}, stopping")
stop
end
......
......@@ -40,7 +40,7 @@ module Gitlab
end
def current_transaction
Transaction.current
::Gitlab::Metrics::Transaction.current
end
end
end
......
......@@ -65,7 +65,7 @@ module Gitlab
private
def current_transaction
Transaction.current
::Gitlab::Metrics::Transaction.current
end
def metric_cache_operation_duration_seconds
......
......@@ -6,10 +6,10 @@ module Gitlab
attach_to :active_record
def sql(event)
return if !Transaction.current || event.payload.fetch(:cached, event.payload[:name] == 'CACHE')
return if !::Gitlab::QueryLimiting::Transaction.current || event.payload.fetch(:cached, event.payload[:name] == 'CACHE')
Transaction.current.increment
Transaction.current.executed_sql(event.payload[:sql])
::Gitlab::QueryLimiting::Transaction.current.increment
::Gitlab::QueryLimiting::Transaction.current.executed_sql(event.payload[:sql])
end
end
end
......
......@@ -13,7 +13,7 @@ module Gitlab
end
def call(env)
transaction, retval = Transaction.run do
transaction, retval = ::Gitlab::QueryLimiting::Transaction.run do
@app.call(env)
end
......
# frozen_string_literal: true
module Gitlab
module Usage
module Metrics
module Aggregates
UNION_OF_AGGREGATED_METRICS = 'OR'
INTERSECTION_OF_AGGREGATED_METRICS = 'AND'
ALLOWED_METRICS_AGGREGATIONS = [UNION_OF_AGGREGATED_METRICS, INTERSECTION_OF_AGGREGATED_METRICS].freeze
AGGREGATED_METRICS_PATH = Rails.root.join('config/metrics/aggregates/*.yml')
AggregatedMetricError = Class.new(StandardError)
UnknownAggregationOperator = Class.new(AggregatedMetricError)
UnknownAggregationSource = Class.new(AggregatedMetricError)
DisallowedAggregationTimeFrame = Class.new(AggregatedMetricError)
DATABASE_SOURCE = 'database'
REDIS_SOURCE = 'redis'
SOURCES = {
DATABASE_SOURCE => Sources::PostgresHll,
REDIS_SOURCE => Sources::RedisHll
}.freeze
end
end
end
end
......@@ -4,23 +4,6 @@ module Gitlab
module Usage
module Metrics
module Aggregates
UNION_OF_AGGREGATED_METRICS = 'OR'
INTERSECTION_OF_AGGREGATED_METRICS = 'AND'
ALLOWED_METRICS_AGGREGATIONS = [UNION_OF_AGGREGATED_METRICS, INTERSECTION_OF_AGGREGATED_METRICS].freeze
AGGREGATED_METRICS_PATH = Rails.root.join('config/metrics/aggregates/*.yml')
AggregatedMetricError = Class.new(StandardError)
UnknownAggregationOperator = Class.new(AggregatedMetricError)
UnknownAggregationSource = Class.new(AggregatedMetricError)
DisallowedAggregationTimeFrame = Class.new(AggregatedMetricError)
DATABASE_SOURCE = 'database'
REDIS_SOURCE = 'redis'
SOURCES = {
DATABASE_SOURCE => Sources::PostgresHll,
REDIS_SOURCE => Sources::RedisHll
}.freeze
class Aggregate
include Gitlab::Usage::TimeFrame
......
# frozen_string_literal: true
module Gitlab
module Usage
module Metrics
module Aggregates
module Sources
UnionNotAvailable = Class.new(AggregatedMetricError)
end
end
end
end
end
......@@ -5,8 +5,6 @@ module Gitlab
module Metrics
module Aggregates
module Sources
UnionNotAvailable = Class.new(AggregatedMetricError)
class RedisHll
extend Calculations::Intersection
def self.calculate_metrics_union(metric_names:, start_date:, end_date:, recorded_at: nil)
......
......@@ -18,9 +18,11 @@ require_relative '../config/settings'
require_relative 'support/rspec'
require 'active_support/all'
ActiveSupport::Dependencies.autoload_paths << 'lib'
ActiveSupport::Dependencies.autoload_paths << 'ee/lib'
ActiveSupport::Dependencies.autoload_paths << 'jh/lib'
unless ActiveSupport::Dependencies.autoload_paths.frozen?
ActiveSupport::Dependencies.autoload_paths << 'lib'
ActiveSupport::Dependencies.autoload_paths << 'ee/lib'
ActiveSupport::Dependencies.autoload_paths << 'jh/lib'
end
ActiveSupport::XmlMini.backend = 'Nokogiri'
......
......@@ -183,18 +183,17 @@ RSpec.describe Gitlab::Database::LoadBalancing::RackMiddleware, :redis do
describe '#clear' do
it 'clears the currently used host and session' do
lb = double(:lb)
session = double(:session)
session = spy(:session)
allow(middleware).to receive(:load_balancer).and_return(lb)
expect(lb).to receive(:release_host)
stub_const('Gitlab::Database::LoadBalancing::RackMiddleware::Session',
session)
expect(session).to receive(:clear_session)
stub_const('Gitlab::Database::LoadBalancing::Session', session)
middleware.clear
expect(session).to have_received(:clear_session)
end
end
......
......@@ -2,7 +2,7 @@
require 'fast_spec_helper'
ActiveSupport::Dependencies.autoload_paths << 'app/services'
require_relative '../../app/services/service_response'
RSpec.describe ServiceResponse do
describe '.success' do
......
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