diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 21:42:06 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 21:42:06 +0300 |
commit | 6e4e1050d9dba2b7b2523fdd1768823ab85feef4 (patch) | |
tree | 78be5963ec075d80116a932011d695dd33910b4e /lib/gitlab/diff | |
parent | 1ce776de4ae122aba3f349c02c17cebeaa8ecf07 (diff) |
Add latest changes from gitlab-org/gitlab@13-3-stable-ee
Diffstat (limited to 'lib/gitlab/diff')
-rw-r--r-- | lib/gitlab/diff/formatters/base_formatter.rb | 1 | ||||
-rw-r--r-- | lib/gitlab/diff/highlight_cache.rb | 57 | ||||
-rw-r--r-- | lib/gitlab/diff/stats_cache.rb | 7 |
3 files changed, 24 insertions, 41 deletions
diff --git a/lib/gitlab/diff/formatters/base_formatter.rb b/lib/gitlab/diff/formatters/base_formatter.rb index 31eeadc45f7..e24150a2330 100644 --- a/lib/gitlab/diff/formatters/base_formatter.rb +++ b/lib/gitlab/diff/formatters/base_formatter.rb @@ -10,7 +10,6 @@ module Gitlab attr_reader :base_sha attr_reader :start_sha attr_reader :head_sha - attr_reader :position_type def initialize(attrs) if diff_file = attrs[:diff_file] diff --git a/lib/gitlab/diff/highlight_cache.rb b/lib/gitlab/diff/highlight_cache.rb index ccf09b37b9b..0c3b6b72313 100644 --- a/lib/gitlab/diff/highlight_cache.rb +++ b/lib/gitlab/diff/highlight_cache.rb @@ -3,7 +3,6 @@ module Gitlab module Diff class HighlightCache - include Gitlab::Metrics::Methods include Gitlab::Utils::StrongMemoize EXPIRATION = 1.week @@ -12,19 +11,6 @@ module Gitlab delegate :diffable, to: :@diff_collection delegate :diff_options, to: :@diff_collection - define_histogram :gitlab_redis_diff_caching_memory_usage_bytes do - docstring 'Redis diff caching memory usage by key' - buckets [100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000] - end - - define_counter :gitlab_redis_diff_caching_hit do - docstring 'Redis diff caching hits' - end - - define_counter :gitlab_redis_diff_caching_miss do - docstring 'Redis diff caching misses' - end - def initialize(diff_collection) @diff_collection = diff_collection end @@ -117,7 +103,10 @@ module Gitlab def record_memory_usage(memory_usage) if memory_usage - self.class.gitlab_redis_diff_caching_memory_usage_bytes.observe({}, memory_usage) + current_transaction&.observe(:gitlab_redis_diff_caching_memory_usage_bytes, memory_usage) do + docstring 'Redis diff caching memory usage by key' + buckets [100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000] + end end end @@ -163,34 +152,24 @@ module Gitlab end def compose_data(json_data) - if ::Feature.enabled?(:gzip_diff_cache, default_enabled: true) - # #compress returns ASCII-8BIT, so we need to force the encoding to - # UTF-8 before caching it in redis, else we risk encoding mismatch - # errors. - # - ActiveSupport::Gzip.compress(json_data).force_encoding("UTF-8") - else - json_data - end + # #compress returns ASCII-8BIT, so we need to force the encoding to + # UTF-8 before caching it in redis, else we risk encoding mismatch + # errors. + # + ActiveSupport::Gzip.compress(json_data).force_encoding("UTF-8") rescue Zlib::GzipFile::Error json_data end def extract_data(data) - # Since when we deploy this code, we'll be dealing with an already - # populated cache full of data that isn't gzipped, we want to also - # check to see if the data is gzipped before we attempt to #decompress - # it, thus we check the first 2 bytes for "\x1F\x8B" to confirm it is - # a gzipped string. While a non-gzipped string will raise a - # Zlib::GzipFile::Error, which we're rescuing, we don't want to count - # on rescue for control flow. This check can be removed in the release - # after this change is released. + # Since we could be dealing with an already populated cache full of data + # that isn't gzipped, we want to also check to see if the data is + # gzipped before we attempt to #decompress it, thus we check the first + # 2 bytes for "\x1F\x8B" to confirm it is a gzipped string. While a + # non-gzipped string will raise a Zlib::GzipFile::Error, which we're + # rescuing, we don't want to count on rescue for control flow. # - if ::Feature.enabled?(:gzip_diff_cache, default_enabled: true) && data[0..1] == "\x1F\x8B" - ActiveSupport::Gzip.decompress(data) - else - data - end + data[0..1] == "\x1F\x8B" ? ActiveSupport::Gzip.decompress(data) : data rescue Zlib::GzipFile::Error data end @@ -206,6 +185,10 @@ module Gitlab # @diff_collection.raw_diff_files end + + def current_transaction + ::Gitlab::Metrics::Transaction.current + end end end end diff --git a/lib/gitlab/diff/stats_cache.rb b/lib/gitlab/diff/stats_cache.rb index f38fb21d497..eb0ef4200dc 100644 --- a/lib/gitlab/diff/stats_cache.rb +++ b/lib/gitlab/diff/stats_cache.rb @@ -3,11 +3,11 @@ module Gitlab module Diff class StatsCache - include Gitlab::Metrics::Methods include Gitlab::Utils::StrongMemoize EXPIRATION = 1.week - VERSION = 1 + # The DiffStats#as_json representation is tied to the Gitaly protobuf version + VERSION = Gem.loaded_specs['gitaly'].version.to_s def initialize(cachable_key:) @cachable_key = cachable_key @@ -29,7 +29,8 @@ module Gitlab return if cache.exist?(key) return unless stats - cache.write(key, stats.as_json, expires_in: EXPIRATION) + cache.write(key, stats.map(&:to_h).as_json, expires_in: EXPIRATION) + clear_memoization(:cached_values) end def clear |