File: read_write_retryable.rb

package info (click to toggle)
ruby-mongo 2.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,020 kB
  • sloc: ruby: 110,810; makefile: 5
file content (108 lines) | stat: -rw-r--r-- 3,633 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
# frozen_string_literal: true
# rubocop:todo all

# Copyright (C) 2019-2022 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Mongo
  class Error

    # A module encapsulating functionality to indicate whether errors are
    # retryable.
    #
    # @note Although methods of this module are part of the public API,
    #   the fact that these methods are defined on this module and not on
    #   the classes which include this module is not part of the public API.
    #
    # @api semipublic
    module ReadWriteRetryable

      # Error codes and code names that should result in a failing write
      # being retried.
      #
      # @api private
      WRITE_RETRY_ERRORS = [
        {:code_name => 'HostUnreachable', :code => 6},
        {:code_name => 'HostNotFound', :code => 7},
        {:code_name => 'NetworkTimeout', :code => 89},
        {:code_name => 'ShutdownInProgress', :code => 91},
        {:code_name => 'PrimarySteppedDown', :code => 189},
        {:code_name => 'ExceededTimeLimit', :code => 262},
        {:code_name => 'SocketException', :code => 9001},
        {:code_name => 'NotMaster', :code => 10107},
        {:code_name => 'InterruptedAtShutdown', :code => 11600},
        {:code_name => 'InterruptedDueToReplStateChange', :code => 11602},
        {:code_name => 'NotPrimaryNoSecondaryOk', :code => 13435},
        {:code_name => 'NotMasterOrSecondary', :code => 13436},
      ].freeze

      # These are magic error messages that could indicate a master change.
      #
      # @api private
      WRITE_RETRY_MESSAGES = [
        'not master',
        'node is recovering',
      ].freeze

      # These are magic error messages that could indicate a cluster
      # reconfiguration behind a mongos.
      #
      # @api private
      RETRY_MESSAGES = WRITE_RETRY_MESSAGES + [
        'transport error',
        'socket exception',
        "can't connect",
        'connect failed',
        'error querying',
        'could not get last error',
        'connection attempt failed',
        'interrupted at shutdown',
        'unknown replica set',
        'dbclient error communicating with server'
      ].freeze

      # Whether the error is a retryable error according to the legacy
      # read retry logic.
      #
      # @return [ true, false ]
      #
      # @deprecated
      def retryable?
        write_retryable? ||
        code.nil? && RETRY_MESSAGES.any?{ |m| message.include?(m) }
      end

      # Whether the error is a retryable error according to the modern retryable
      # reads and retryable writes specifications.
      #
      # This method is also used by the legacy retryable write logic to determine
      # whether an error is a retryable one.
      #
      # @return [ true, false ]
      def write_retryable?
        write_retryable_code? ||
        code.nil? && WRITE_RETRY_MESSAGES.any? { |m| message.include?(m) }
      end

      private def write_retryable_code?
        if code
          WRITE_RETRY_ERRORS.any? { |e| e[:code] == code }
        else
          # return false rather than nil
          false
        end
      end
    end
  end
end