2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-24 12:17:04 -04:00
|
|
|
module Gitlab
|
|
|
|
# Class for counting and caching the number of issuables per state.
|
|
|
|
class IssuablesCountForState
|
2018-09-20 15:32:54 -04:00
|
|
|
# The name of the Gitlab::SafeRequestStore cache key.
|
2017-08-24 12:17:04 -04:00
|
|
|
CACHE_KEY = :issuables_count_for_state
|
2021-09-01 11:10:20 -04:00
|
|
|
# The expiration time for the Rails cache.
|
2021-09-10 11:11:12 -04:00
|
|
|
CACHE_EXPIRES_IN = 1.hour
|
2021-09-01 11:10:20 -04:00
|
|
|
THRESHOLD = 1000
|
2017-08-24 12:17:04 -04:00
|
|
|
|
|
|
|
# The state values that can be safely casted to a Symbol.
|
|
|
|
STATES = %w[opened closed merged all].freeze
|
|
|
|
|
2021-09-01 11:10:20 -04:00
|
|
|
attr_reader :project, :finder
|
2020-08-11 23:10:17 -04:00
|
|
|
|
|
|
|
def self.declarative_policy_class
|
|
|
|
'IssuablePolicy'
|
|
|
|
end
|
|
|
|
|
2017-08-24 12:17:04 -04:00
|
|
|
# finder - The finder class to use for retrieving the issuables.
|
2020-10-14 14:08:47 -04:00
|
|
|
# fast_fail - restrict counting to a shorter period, degrading gracefully on
|
|
|
|
# failure
|
2021-09-01 11:10:20 -04:00
|
|
|
def initialize(finder, project = nil, fast_fail: false, store_in_redis_cache: false)
|
2017-08-24 12:17:04 -04:00
|
|
|
@finder = finder
|
2020-08-11 23:10:17 -04:00
|
|
|
@project = project
|
2020-10-14 14:08:47 -04:00
|
|
|
@fast_fail = fast_fail
|
2018-09-20 15:32:54 -04:00
|
|
|
@cache = Gitlab::SafeRequestStore[CACHE_KEY] ||= initialize_cache
|
2021-09-01 11:10:20 -04:00
|
|
|
@store_in_redis_cache = store_in_redis_cache
|
2017-08-24 12:17:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def for_state_or_opened(state = nil)
|
|
|
|
self[state || :opened]
|
|
|
|
end
|
|
|
|
|
2020-10-14 14:08:47 -04:00
|
|
|
def fast_fail?
|
|
|
|
!!@fast_fail
|
|
|
|
end
|
|
|
|
|
2020-08-11 23:10:17 -04:00
|
|
|
# Define method for each state
|
|
|
|
STATES.each do |state|
|
|
|
|
define_method(state) { self[state] }
|
|
|
|
end
|
|
|
|
|
2017-08-24 12:17:04 -04:00
|
|
|
# Returns the count for the given state.
|
|
|
|
#
|
|
|
|
# state - The name of the state as either a String or a Symbol.
|
|
|
|
#
|
|
|
|
# Returns an Integer.
|
|
|
|
def [](state)
|
|
|
|
state = state.to_sym if cast_state_to_symbol?(state)
|
|
|
|
|
|
|
|
cache_for_finder[state] || 0
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def cache_for_finder
|
2021-09-01 11:10:20 -04:00
|
|
|
cached_counts = Rails.cache.read(redis_cache_key, cache_options) if cache_issues_count?
|
|
|
|
|
|
|
|
cached_counts ||= @cache[finder]
|
|
|
|
return cached_counts if cached_counts.empty?
|
|
|
|
|
|
|
|
if cache_issues_count? && cached_counts.values.all? { |count| count >= THRESHOLD }
|
|
|
|
Rails.cache.write(redis_cache_key, cached_counts, cache_options)
|
|
|
|
end
|
|
|
|
|
|
|
|
cached_counts
|
2017-08-24 12:17:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cast_state_to_symbol?(state)
|
|
|
|
state.is_a?(String) && STATES.include?(state)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_cache
|
2020-10-14 14:08:47 -04:00
|
|
|
Hash.new { |hash, finder| hash[finder] = perform_count(finder) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_count(finder)
|
|
|
|
return finder.count_by_state unless fast_fail?
|
|
|
|
|
|
|
|
fast_count_by_state_attempt!
|
|
|
|
|
|
|
|
# Determining counts when referring to issuable titles or descriptions can
|
|
|
|
# be very expensive, and involve the database reading gigabytes of data
|
|
|
|
# for a relatively minor piece of functionality. This may slow index pages
|
|
|
|
# by seconds in the best case, or lead to a statement timeout in the worst
|
|
|
|
# case.
|
|
|
|
#
|
|
|
|
# In time, we may be able to use elasticsearch or postgresql tsv columns
|
|
|
|
# to perform the calculation more efficiently. Until then, use a shorter
|
|
|
|
# timeout and return -1 as a sentinel value if it is triggered
|
|
|
|
begin
|
2021-03-23 05:09:17 -04:00
|
|
|
ApplicationRecord.with_fast_read_statement_timeout do
|
2020-10-14 14:08:47 -04:00
|
|
|
finder.count_by_state
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::QueryCanceled => err
|
|
|
|
fast_count_by_state_failure!
|
|
|
|
|
|
|
|
Gitlab::ErrorTracking.track_exception(
|
|
|
|
err,
|
|
|
|
params: finder.params,
|
|
|
|
current_user_id: finder.current_user&.id,
|
|
|
|
issue_url: 'https://gitlab.com/gitlab-org/gitlab/-/issues/249180'
|
|
|
|
)
|
|
|
|
|
|
|
|
Hash.new(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fast_count_by_state_attempt!
|
|
|
|
Gitlab::Metrics.counter(
|
|
|
|
:gitlab_issuable_fast_count_by_state_total,
|
|
|
|
"Count of total calls to IssuableFinder#count_by_state with fast failure"
|
|
|
|
).increment
|
|
|
|
end
|
|
|
|
|
|
|
|
def fast_count_by_state_failure!
|
|
|
|
Gitlab::Metrics.counter(
|
|
|
|
:gitlab_issuable_fast_count_by_state_failures_total,
|
|
|
|
"Count of failed calls to IssuableFinder#count_by_state with fast failure"
|
|
|
|
).increment
|
2017-08-24 12:17:04 -04:00
|
|
|
end
|
2021-09-01 11:10:20 -04:00
|
|
|
|
|
|
|
def cache_issues_count?
|
|
|
|
@store_in_redis_cache &&
|
|
|
|
finder.instance_of?(IssuesFinder) &&
|
|
|
|
parent_group.present? &&
|
|
|
|
!params_include_filters?
|
|
|
|
end
|
|
|
|
|
|
|
|
def parent_group
|
|
|
|
finder.params.group
|
|
|
|
end
|
|
|
|
|
|
|
|
def redis_cache_key
|
|
|
|
['group', parent_group&.id, 'issues']
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_options
|
|
|
|
{ expires_in: CACHE_EXPIRES_IN }
|
|
|
|
end
|
|
|
|
|
|
|
|
def params_include_filters?
|
|
|
|
non_filtering_params = %i[
|
|
|
|
scope state sort group_id include_subgroups
|
2022-03-07 07:16:11 -05:00
|
|
|
attempt_group_search_optimizations non_archived issue_types
|
2021-09-01 11:10:20 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
finder.params.except(*non_filtering_params).values.any?
|
|
|
|
end
|
2017-08-24 12:17:04 -04:00
|
|
|
end
|
|
|
|
end
|