File: retryable_errors_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (286 lines) | stat: -rw-r--r-- 7,577 bytes parent folder | download
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

describe 'Failing retryable operations' do
  # Requirement for fail point
  min_server_fcv '4.0'

  let(:subscriber) { Mrss::EventSubscriber.new }

  let(:client_options) do
    {}
  end

  let(:client) do
    authorized_client.with(client_options).tap do |client|
      client.subscribe(Mongo::Monitoring::COMMAND, subscriber)
    end
  end

  let(:collection) do
    client['retryable-errors-spec']
  end

  context 'when operation fails' do
    require_topology :replica_set


    let(:clear_fail_point_command) do
      {
        configureFailPoint: 'failCommand',
        mode: 'off',
      }
    end

    after do
      ClusterTools.instance.direct_client_for_each_data_bearing_server do |client|
        client.use(:admin).database.command(clear_fail_point_command)
      end
    end

    let(:collection) do
      client['retryable-errors-spec', read: {mode: :secondary_preferred}]
    end

    let(:first_server) do
      client.cluster.servers_list.detect do |server|
        server.address.seed == events.first.address.seed
      end
    end

    let(:second_server) do
      client.cluster.servers_list.detect do |server|
        server.address.seed == events.last.address.seed
      end
    end

    shared_context 'read operation' do
      let(:fail_point_command) do
        {
          configureFailPoint: 'failCommand',
          mode: {times: 1},
          data: {
            failCommands: ['find'],
            errorCode: 11600,
          },
        }
      end

      let(:set_fail_point) do
        client.cluster.servers_list.each do |server|
          server.monitor.stop!
        end

        ClusterTools.instance.direct_client_for_each_data_bearing_server do |client|
          client.use(:admin).database.command(fail_point_command)
        end
      end

      let(:operation_exception) do
        set_fail_point

        begin
          collection.find(a: 1).to_a
        rescue Mongo::Error::OperationFailure::Family => exception
        else
          fail('Expected operation to fail')
        end

        puts exception.message

        exception
      end

      let(:events) do
        subscriber.command_started_events('find')
      end
    end

    shared_context 'write operation' do
      let(:fail_point_command) do
        command = {
          configureFailPoint: 'failCommand',
          mode: {times: 2},
          data: {
            failCommands: ['insert'],
            errorCode: 11600,
          },
        }

        if ClusterConfig.instance.short_server_version >= '4.4'
          # Server versions 4.4 and newer will add the RetryableWriteError
          # label to all retryable errors, and the driver must not add the label
          # if it is not already present.
          command[:data][:errorLabels] = ['RetryableWriteError']
        end

        command
      end

      let(:set_fail_point) do
        client.use(:admin).database.command(fail_point_command)
      end

      let(:operation_exception) do
        set_fail_point

        begin
          collection.insert_one(a: 1)
        rescue Mongo::Error::OperationFailure::Family => exception
        else
          fail('Expected operation to fail')
        end

        #puts exception.message

        exception
      end

      let(:events) do
        subscriber.command_started_events('insert')
      end
    end

    shared_examples_for 'failing retry' do

      it 'indicates second attempt' do
        expect(operation_exception.message).to include('attempt 2')
        expect(operation_exception.message).not_to include('attempt 1')
        expect(operation_exception.message).not_to include('attempt 3')
      end

      it 'publishes two events' do
        operation_exception

        expect(events.length).to eq(2)
      end
    end

    shared_examples_for 'failing single attempt' do

      it 'does not indicate attempt' do
        expect(operation_exception.message).not_to include('attempt 1')
        expect(operation_exception.message).not_to include('attempt 2')
        expect(operation_exception.message).not_to include('attempt 3')
      end

      it 'publishes one event' do
        operation_exception

        expect(events.length).to eq(1)
      end
    end

    shared_examples_for 'failing retry on the same server' do
      it 'is reported on the server of the second attempt' do
        expect(operation_exception.message).to include(second_server.address.seed)
      end
    end

    shared_examples_for 'failing retry on a different server' do
      it 'is reported on the server of the second attempt' do
        expect(operation_exception.message).not_to include(first_server.address.seed)
        expect(operation_exception.message).to include(second_server.address.seed)
      end

      it 'marks servers used in both attempts unknown' do
        operation_exception

        expect(first_server).to be_unknown

        expect(second_server).to be_unknown
      end

      it 'publishes events for the different server addresses' do

        expect(events.length).to eq(2)
        expect(events.first.address.seed).not_to eq(events.last.address.seed)
      end
    end

    shared_examples_for 'modern retry' do
      it 'indicates modern retry' do
        expect(operation_exception.message).to include('modern retry')
        expect(operation_exception.message).not_to include('legacy retry')
        expect(operation_exception.message).not_to include('retries disabled')
      end
    end

    shared_examples_for 'legacy retry' do
      it 'indicates legacy retry' do
        expect(operation_exception.message).to include('legacy retry')
        expect(operation_exception.message).not_to include('modern retry')
        expect(operation_exception.message).not_to include('retries disabled')
      end
    end

    shared_examples_for 'disabled retry' do
      it 'indicates retries are disabled' do
        expect(operation_exception.message).to include('retries disabled')
        expect(operation_exception.message).not_to include('legacy retry')
        expect(operation_exception.message).not_to include('modern retry')
      end
    end

    context 'when read is retried and retry fails' do
      include_context 'read operation'

      context 'modern read retries' do
        require_wired_tiger_on_36

        let(:client_options) do
          {retry_reads: true}
        end

        it_behaves_like 'failing retry'
        it_behaves_like 'modern retry'
      end

      context 'legacy read retries' do
        let(:client_options) do
          {retry_reads: false, read_retry_interval: 0}
        end

        it_behaves_like 'failing retry'
        it_behaves_like 'legacy retry'
      end
    end

    context 'when read retries are disabled' do
      let(:client_options) do
        {retry_reads: false, max_read_retries: 0}
      end

      include_context 'read operation'

      it_behaves_like 'failing single attempt'
      it_behaves_like 'disabled retry'
    end

    context 'when write is retried and retry fails' do
      include_context 'write operation'

      context 'modern write retries' do
        require_wired_tiger_on_36

        let(:client_options) do
          {retry_writes: true}
        end

        it_behaves_like 'failing retry'
        it_behaves_like 'modern retry'
      end

      context 'legacy write' do
        let(:client_options) do
          {retry_writes: false}
        end

        it_behaves_like 'failing retry'
        it_behaves_like 'legacy retry'
      end
    end
  end
end