Commit 4f58363c authored by Kamil Trzciński's avatar Kamil Trzciński

Extend decomposition prometheus metric with information about CI

This adds to `ci_dedicated_primary_connection="true"`
to `gitlab_database_decomposition_gitlab_schemas_used`
to indicate whether the given schemas are evaluated in context
of a Phase 4 rollout.

This allows us to monitor whether partial Phase 4 rollout
properly assigns connections to schemas.

Changelog: added
parent 6e4c079d
......@@ -78,15 +78,15 @@ module Gitlab
end
def primary_model_or_model_if_enabled
if force_no_sharing_primary_model?
if use_dedicated_connection?
@model
else
@primary_model || @model
end
end
def force_no_sharing_primary_model?
return false unless @primary_model # Doesn't matter since we don't have an overriding primary model
def use_dedicated_connection?
return true unless @primary_model # We can only use dedicated connection, if re-use of connections is disabled
return false unless ::Gitlab::SafeRequestStore.active?
::Gitlab::SafeRequestStore.fetch(:force_no_sharing_primary_model) do
......
......@@ -27,9 +27,15 @@ module Gitlab
# to reduce amount of labels sort schemas used
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({
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
......
......@@ -17,7 +17,7 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana
process_sql(ActiveRecord::Base, "SELECT 1 FROM projects")
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
where do
......@@ -28,7 +28,8 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana
expectations: {
gitlab_schemas: "gitlab_main",
db_config_name: "main"
}
},
setup: nil
},
"for query accessing gitlab_ci and gitlab_main" => {
model: ApplicationRecord,
......@@ -36,7 +37,8 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana
expectations: {
gitlab_schemas: "gitlab_ci,gitlab_main",
db_config_name: "main"
}
},
setup: nil
},
"for query accessing gitlab_ci and gitlab_main the gitlab_schemas is always ordered" => {
model: ApplicationRecord,
......@@ -44,7 +46,8 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana
expectations: {
gitlab_schemas: "gitlab_ci,gitlab_main",
db_config_name: "main"
}
},
setup: nil
},
"for query accessing CI database" => {
model: Ci::ApplicationRecord,
......@@ -53,6 +56,62 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana
gitlab_schemas: "gitlab_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
......@@ -63,8 +122,15 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana
end
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)
.with(expectations).and_call_original
.with({ ci_dedicated_primary_connection: anything }.merge(expectations)).and_call_original
process_sql(model, sql)
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