Commit 403dc77e authored by Alper Akgun's avatar Alper Akgun Committed by Ash McKenzie

Harden deployment counters

Use a more specialized index and use start and finish parameters
parent e20b5649
---
title: Optimize deployment counters for last 28 days
merge_request: 35892
author:
type: performance
# frozen_string_literal: true
class AddIndexOnIdAndStatusAndCreatedAtToDeployments < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_index :deployments, [:id, :status, :created_at]
remove_concurrent_index :deployments, [:id, :status]
end
def down
add_concurrent_index :deployments, [:id, :status]
remove_concurrent_index :deployments, [:id, :status, :created_at]
end
end
......@@ -18986,7 +18986,7 @@ CREATE INDEX index_deployments_on_environment_id_and_iid_and_project_id ON publi
CREATE INDEX index_deployments_on_environment_id_and_status ON public.deployments USING btree (environment_id, status);
CREATE INDEX index_deployments_on_id_and_status ON public.deployments USING btree (id, status);
CREATE INDEX index_deployments_on_id_and_status_and_created_at ON public.deployments USING btree (id, status, created_at);
CREATE INDEX index_deployments_on_id_where_cluster_id_present ON public.deployments USING btree (id) WHERE (cluster_id IS NOT NULL);
......@@ -23639,6 +23639,7 @@ COPY "schema_migrations" (version) FROM STDIN;
20200702123805
20200703154822
20200704143633
20200704161600
20200706005325
20200706170536
20200707071941
......
......@@ -84,9 +84,9 @@ module Gitlab
auto_devops_enabled: count(::ProjectAutoDevops.enabled),
auto_devops_disabled: count(::ProjectAutoDevops.disabled),
deploy_keys: count(DeployKey),
deployments: count(Deployment),
successful_deployments: count(Deployment.success),
failed_deployments: count(Deployment.failed),
deployments: deployment_count(Deployment),
successful_deployments: deployment_count(Deployment.success),
failed_deployments: deployment_count(Deployment.failed),
environments: count(::Environment),
clusters: count(::Clusters::Cluster),
clusters_enabled: count(::Clusters::Cluster.enabled),
......@@ -169,9 +169,9 @@ module Gitlab
def system_usage_data_monthly
{
counts_monthly: {
deployments: count(Deployment.where(last_28_days_time_period)),
successful_deployments: count(Deployment.success.where(last_28_days_time_period)),
failed_deployments: count(Deployment.failed.where(last_28_days_time_period)),
deployments: deployment_count(Deployment.where(last_28_days_time_period)),
successful_deployments: deployment_count(Deployment.success.where(last_28_days_time_period)),
failed_deployments: deployment_count(Deployment.failed.where(last_28_days_time_period)),
personal_snippets: count(PersonalSnippet.where(last_28_days_time_period)),
project_snippets: count(ProjectSnippet.where(last_28_days_time_period))
}.tap do |data|
......@@ -616,12 +616,26 @@ module Gitlab
end
end
def deployment_minimum_id
strong_memoize(:deployment_minimum_id) do
::Deployment.minimum(:id)
end
end
def deployment_maximum_id
strong_memoize(:deployment_maximum_id) do
::Deployment.maximum(:id)
end
end
def clear_memoized
clear_memoization(:issue_minimum_id)
clear_memoization(:issue_maximum_id)
clear_memoization(:user_minimum_id)
clear_memoization(:user_maximum_id)
clear_memoization(:unique_visit_service)
clear_memoization(:deployment_minimum_id)
clear_memoization(:deployment_maximum_id)
end
# rubocop: disable CodeReuse/ActiveRecord
......@@ -645,6 +659,10 @@ module Gitlab
def filtered_omniauth_provider_names
omniauth_provider_names.reject { |name| name.starts_with?('ldap') }
end
def deployment_count(relation)
count relation, start: deployment_minimum_id, finish: deployment_maximum_id
end
end
end
end
......
......@@ -18,7 +18,10 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
end
it 'clears memoized values' do
%i(issue_minimum_id issue_maximum_id user_minimum_id user_maximum_id unique_visit_service).each do |key|
values = %i(issue_minimum_id issue_maximum_id
user_minimum_id user_maximum_id unique_visit_service
deployment_minimum_id deployment_maximum_id)
values.each do |key|
expect(described_class).to receive(:clear_memoization).with(key)
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