Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20230317004428_migrate_daily_redis_hll_events_to_weekly_aggregation_spec.rb « migrations « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 21cdb6afa2417c50c6514cc28b187b0d402cab05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# frozen_string_literal: true

require 'spec_helper'
require_migration!

RSpec.describe MigrateDailyRedisHllEventsToWeeklyAggregation, :migration, :clean_gitlab_redis_cache, feature_category: :service_ping do
  it 'calls HLLRedisCounter.known_events to get list of events' do
    expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:known_events).and_call_original.at_least(1).time

    migrate!
  end

  describe '#redis_key' do
    let(:date) { Date.today }

    context 'with daily aggregation' do
      let(:date_formatted) { date.strftime('%G-%j') }
      let(:event) { { aggregation: 'daily', name: 'wiki_action' } }

      it 'returns correct key' do
        existing_key = "#{date_formatted}-{hll_counters}_wiki_action"

        expect(described_class.new.redis_key(event, date, event[:aggregation])).to eq(existing_key)
      end
    end

    context 'with weekly aggregation' do
      let(:date_formatted) { date.strftime('%G-%V') }
      let(:event) { { aggregation: 'weekly', name: 'weekly_action' } }

      it 'returns correct key' do
        existing_key = "{hll_counters}_weekly_action-#{date_formatted}"

        expect(described_class.new.redis_key(event, date, event[:aggregation])).to eq(existing_key)
      end
    end
  end

  context 'with weekly events' do
    let(:events) { [{ aggregation: 'weekly', name: 'weekly_action' }] }

    before do
      allow(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:known_events).and_return(events)
    end

    it 'does not migrate weekly events' do
      Gitlab::Redis::SharedState.with do |redis|
        expect(redis).not_to receive(:pfmerge)
        expect(redis).not_to receive(:expire)
      end

      migrate!
    end
  end

  context 'with daily events' do
    let(:daily_expiry) { 29.days }
    let(:weekly_expiry) { Gitlab::UsageDataCounters::HLLRedisCounter::DEFAULT_WEEKLY_KEY_EXPIRY_LENGTH }

    it 'migrates with correct parameters', :aggregate_failures do
      events = [{ aggregation: 'daily', name: 'g_project_management_epic_blocked_removed' }]
      allow(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:known_events).and_return(events)

      event = events.first.dup.tap { |e| e[:aggregation] = 'weekly' }
      # For every day in the last 30 days, add a value to the daily key with daily expiry (including today)
      31.times do |i|
        key = described_class.new.send(:redis_key, event, Date.today - i.days, :weekly)
        Gitlab::Redis::HLL.add(key: key, value: i, expiry: daily_expiry)
      end

      migrate!

      new_key = described_class.new.send(:redis_key, event, Date.today, :weekly)
      # for the current week we should have value eq to the day of the week (ie. 1 for Monday, 2 for Tuesday, etc.)
      first_week_days = Date.today.cwday
      expect(Gitlab::Redis::HLL.count(keys: new_key)).to eq(first_week_days)
      expect(with_redis { |r| r.ttl(new_key) }).to be_within(600).of(weekly_expiry)

      full_weeks = (31 - first_week_days) / 7
      # for the next full weeks we should have value eq to 7 (ie. 7 days in a week)
      (1..full_weeks).each do |i|
        new_key = described_class.new.send(:redis_key, event, Date.today - i.weeks, :weekly)
        expect(Gitlab::Redis::HLL.count(keys: new_key)).to eq(7)
        expect(with_redis { |r| r.ttl(new_key) }).to be_within(600).of(weekly_expiry)
      end

      # for the last week we should have value eq to amount of rest of the days affected
      last_week_days = 31 - ((full_weeks * 7) + first_week_days)
      unless last_week_days.zero?
        last_week = full_weeks + 1
        new_key = described_class.new.send(:redis_key, event, Date.today - last_week.weeks, :weekly)
        expect(Gitlab::Redis::HLL.count(keys: new_key)).to eq(last_week_days)
        expect(with_redis { |r| r.ttl(new_key) }).to be_within(600).of(weekly_expiry)
      end
    end
  end

  def with_redis(&block)
    Gitlab::Redis::SharedState.with(&block)
  end
end