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

multi_store.rb « redis « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f930a0040bca567b49cecc67d41caab6d402cf19 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# frozen_string_literal: true

module Gitlab
  module Redis
    class MultiStore
      include Gitlab::Utils::StrongMemoize

      class ReadFromPrimaryError < StandardError
        def message
          'Value not found on the redis primary store. Read from the redis secondary store successful.'
        end
      end
      class MethodMissingError < StandardError
        def message
          'Method missing. Falling back to execute method on the redis secondary store.'
        end
      end

      attr_reader :primary_store, :secondary_store, :instance_name

      FAILED_TO_READ_ERROR_MESSAGE = 'Failed to read from the redis primary_store.'
      FAILED_TO_WRITE_ERROR_MESSAGE = 'Failed to write to the redis primary_store.'

      READ_COMMANDS = %i(
        get
        mget
        smembers
        scard
      ).freeze

      WRITE_COMMANDS = %i(
        set
        setnx
        setex
        sadd
        srem
        del
        pipelined
        flushdb
      ).freeze

      def initialize(primary_store, secondary_store, instance_name = nil)
        @primary_store = primary_store
        @secondary_store = secondary_store
        @instance_name = instance_name

        validate_stores!
      end

      READ_COMMANDS.each do |name|
        define_method(name) do |*args, &block|
          if multi_store_enabled?
            read_command(name, *args, &block)
          else
            secondary_store.send(name, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
          end
        end
      end

      WRITE_COMMANDS.each do |name|
        define_method(name) do |*args, &block|
          if multi_store_enabled?
            write_command(name, *args, &block)
          else
            secondary_store.send(name, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
          end
        end
      end

      def method_missing(...)
        return @instance.send(...) if @instance # rubocop:disable GitlabSecurity/PublicSend

        log_method_missing(...)

        secondary_store.send(...) # rubocop:disable GitlabSecurity/PublicSend
      end

      def respond_to_missing?(command_name, include_private = false)
        true
      end

      # This is needed because of Redis::Rack::Connection is requiring Redis::Store
      # https://github.com/redis-store/redis-rack/blob/a833086ba494083b6a384a1a4e58b36573a9165d/lib/redis/rack/connection.rb#L15
      # Done similarly in https://github.com/lsegal/yard/blob/main/lib/yard/templates/template.rb#L122
      def is_a?(klass)
        return true if klass == secondary_store.class

        super(klass)
      end
      alias_method :kind_of?, :is_a?

      def to_s
        if multi_store_enabled?
          primary_store.to_s
        else
          secondary_store.to_s
        end
      end

      private

      def log_method_missing(command_name, *_args)
        log_error(MethodMissingError.new, command_name)
        increment_method_missing_count(command_name)
      end

      def read_command(command_name, *args, &block)
        if @instance
          send_command(@instance, command_name, *args, &block)
        else
          read_one_with_fallback(command_name, *args, &block)
        end
      end

      def write_command(command_name, *args, &block)
        if @instance
          send_command(@instance, command_name, *args, &block)
        else
          write_both(command_name, *args, &block)
        end
      end

      def read_one_with_fallback(command_name, *args, &block)
        begin
          value = send_command(primary_store, command_name, *args, &block)
        rescue StandardError => e
          log_error(e, command_name,
            multi_store_error_message: FAILED_TO_READ_ERROR_MESSAGE)
        end

        value ||= fallback_read(command_name, *args, &block)

        value
      end

      def fallback_read(command_name, *args, &block)
        value = send_command(secondary_store, command_name, *args, &block)

        if value
          log_error(ReadFromPrimaryError.new, command_name)
          increment_read_fallback_count(command_name)
        end

        value
      end

      def write_both(command_name, *args, &block)
        begin
          send_command(primary_store, command_name, *args, &block)
        rescue StandardError => e
          log_error(e, command_name,
            multi_store_error_message: FAILED_TO_WRITE_ERROR_MESSAGE)
        end

        send_command(secondary_store, command_name, *args, &block)
      end

      def multi_store_enabled?
        Feature.enabled?(:use_multi_store, default_enabled: :yaml) && !same_redis_store?
      end

      def same_redis_store?
        strong_memoize(:same_redis_store) do
          # <Redis client v4.4.0 for redis:///path_to/redis/redis.socket/5>"
          primary_store.inspect == secondary_store.inspect
        end
      end

      # rubocop:disable GitlabSecurity/PublicSend
      def send_command(redis_instance, command_name, *args, &block)
        if block_given?
          # Make sure that block is wrapped and executed only on the redis instance that is executing the block
          redis_instance.send(command_name, *args) do |*params|
            with_instance(redis_instance, *params, &block)
          end
        else
          redis_instance.send(command_name, *args)
        end
      end
      # rubocop:enable GitlabSecurity/PublicSend

      def with_instance(instance, *params)
        @instance = instance

        yield(*params)
      ensure
        @instance = nil
      end

      def increment_read_fallback_count(command_name)
        @read_fallback_counter ||= Gitlab::Metrics.counter(:gitlab_redis_multi_store_read_fallback_total, 'Client side Redis MultiStore reading fallback')
        @read_fallback_counter.increment(command: command_name, instance_name: instance_name)
      end

      def increment_method_missing_count(command_name)
        @method_missing_counter ||= Gitlab::Metrics.counter(:gitlab_redis_multi_store_method_missing_total, 'Client side Redis MultiStore method missing')
        @method_missing_counter.increment(command: command_name, innamece_name: instance_name)
      end

      def validate_stores!
        raise ArgumentError, 'primary_store is required' unless primary_store
        raise ArgumentError, 'secondary_store is required' unless secondary_store
        raise ArgumentError, 'invalid primary_store' unless primary_store.is_a?(::Redis)
        raise ArgumentError, 'invalid secondary_store' unless secondary_store.is_a?(::Redis)
      end

      def log_error(exception, command_name, extra = {})
        Gitlab::ErrorTracking.log_exception(
          exception,
          command_name: command_name,
          extra: extra.merge(instance_name: instance_name))
      end
    end
  end
end