Commit e6d62f50 authored by Thong Kuah's avatar Thong Kuah

Merge branch 'extend-decomposition-metric' into 'master'

Extend decomposition prometheus metric with information about CI

See merge request gitlab-org/gitlab!85178
parents a205ea1a 4f58363c
...@@ -78,15 +78,15 @@ module Gitlab ...@@ -78,15 +78,15 @@ module Gitlab
end end
def primary_model_or_model_if_enabled def primary_model_or_model_if_enabled
if force_no_sharing_primary_model? if use_dedicated_connection?
@model @model
else else
@primary_model || @model @primary_model || @model
end end
end end
def force_no_sharing_primary_model? def use_dedicated_connection?
return false unless @primary_model # Doesn't matter since we don't have an overriding primary model return true unless @primary_model # We can only use dedicated connection, if re-use of connections is disabled
return false unless ::Gitlab::SafeRequestStore.active? return false unless ::Gitlab::SafeRequestStore.active?
::Gitlab::SafeRequestStore.fetch(:force_no_sharing_primary_model) do ::Gitlab::SafeRequestStore.fetch(:force_no_sharing_primary_model) do
......
...@@ -27,9 +27,15 @@ module Gitlab ...@@ -27,9 +27,15 @@ module Gitlab
# to reduce amount of labels sort schemas used # to reduce amount of labels sort schemas used
gitlab_schemas = gitlab_schemas.to_a.sort.join(",") gitlab_schemas = gitlab_schemas.to_a.sort.join(",")
# Temporary feature to observe relation of `gitlab_schemas` to `db_config_name`
# depending on primary model
ci_dedicated_primary_connection = ::Ci::ApplicationRecord.connection_class? &&
::Ci::ApplicationRecord.load_balancer.configuration.use_dedicated_connection?
schemas_metrics.increment({ schemas_metrics.increment({
gitlab_schemas: gitlab_schemas, gitlab_schemas: gitlab_schemas,
db_config_name: db_config_name db_config_name: db_config_name,
ci_dedicated_primary_connection: ci_dedicated_primary_connection
}) })
end end
......
...@@ -17,7 +17,7 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana ...@@ -17,7 +17,7 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana
process_sql(ActiveRecord::Base, "SELECT 1 FROM projects") process_sql(ActiveRecord::Base, "SELECT 1 FROM projects")
end end
context 'properly observes all queries', :add_ci_connection do context 'properly observes all queries', :add_ci_connection, :request_store do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
where do where do
...@@ -28,7 +28,8 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana ...@@ -28,7 +28,8 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana
expectations: { expectations: {
gitlab_schemas: "gitlab_main", gitlab_schemas: "gitlab_main",
db_config_name: "main" db_config_name: "main"
} },
setup: nil
}, },
"for query accessing gitlab_ci and gitlab_main" => { "for query accessing gitlab_ci and gitlab_main" => {
model: ApplicationRecord, model: ApplicationRecord,
...@@ -36,7 +37,8 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana ...@@ -36,7 +37,8 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana
expectations: { expectations: {
gitlab_schemas: "gitlab_ci,gitlab_main", gitlab_schemas: "gitlab_ci,gitlab_main",
db_config_name: "main" db_config_name: "main"
} },
setup: nil
}, },
"for query accessing gitlab_ci and gitlab_main the gitlab_schemas is always ordered" => { "for query accessing gitlab_ci and gitlab_main the gitlab_schemas is always ordered" => {
model: ApplicationRecord, model: ApplicationRecord,
...@@ -44,7 +46,8 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana ...@@ -44,7 +46,8 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana
expectations: { expectations: {
gitlab_schemas: "gitlab_ci,gitlab_main", gitlab_schemas: "gitlab_ci,gitlab_main",
db_config_name: "main" db_config_name: "main"
} },
setup: nil
}, },
"for query accessing CI database" => { "for query accessing CI database" => {
model: Ci::ApplicationRecord, model: Ci::ApplicationRecord,
...@@ -53,6 +56,62 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana ...@@ -53,6 +56,62 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana
gitlab_schemas: "gitlab_ci", gitlab_schemas: "gitlab_ci",
db_config_name: "ci" db_config_name: "ci"
} }
},
"for query accessing CI database with re-use and disabled sharing" => {
model: Ci::ApplicationRecord,
sql: "SELECT 1 FROM ci_builds",
expectations: {
gitlab_schemas: "gitlab_ci",
db_config_name: "ci",
ci_dedicated_primary_connection: true
},
setup: ->(_) do
skip_if_multiple_databases_not_setup
stub_env('GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci', 'main')
stub_feature_flags(force_no_sharing_primary_model: true)
end
},
"for query accessing CI database with re-use and enabled sharing" => {
model: Ci::ApplicationRecord,
sql: "SELECT 1 FROM ci_builds",
expectations: {
gitlab_schemas: "gitlab_ci",
db_config_name: "ci",
ci_dedicated_primary_connection: false
},
setup: ->(_) do
skip_if_multiple_databases_not_setup
stub_env('GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci', 'main')
stub_feature_flags(force_no_sharing_primary_model: false)
end
},
"for query accessing CI database without re-use and disabled sharing" => {
model: Ci::ApplicationRecord,
sql: "SELECT 1 FROM ci_builds",
expectations: {
gitlab_schemas: "gitlab_ci",
db_config_name: "ci",
ci_dedicated_primary_connection: true
},
setup: ->(_) do
skip_if_multiple_databases_not_setup
stub_env('GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci', nil)
stub_feature_flags(force_no_sharing_primary_model: true)
end
},
"for query accessing CI database without re-use and enabled sharing" => {
model: Ci::ApplicationRecord,
sql: "SELECT 1 FROM ci_builds",
expectations: {
gitlab_schemas: "gitlab_ci",
db_config_name: "ci",
ci_dedicated_primary_connection: true
},
setup: ->(_) do
skip_if_multiple_databases_not_setup
stub_env('GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci', nil)
stub_feature_flags(force_no_sharing_primary_model: false)
end
} }
} }
end end
...@@ -63,8 +122,15 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana ...@@ -63,8 +122,15 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana
end end
it do it do
stub_env('GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci', nil)
instance_eval(&setup) if setup
allow(::Ci::ApplicationRecord.load_balancer).to receive(:configuration)
.and_return(Gitlab::Database::LoadBalancing::Configuration.for_model(::Ci::ApplicationRecord))
expect(described_class.schemas_metrics).to receive(:increment) expect(described_class.schemas_metrics).to receive(:increment)
.with(expectations).and_call_original .with({ ci_dedicated_primary_connection: anything }.merge(expectations)).and_call_original
process_sql(model, sql) process_sql(model, sql)
end end
......
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