Class: Mongo::Retryable::ReadWorker Private

Inherits:
BaseWorker show all
Defined in:
lib/mongo/retryable/read_worker.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Implements the logic around retrying read operations.

Since:

  • 2.19.0

Instance Attribute Summary

Attributes inherited from BaseWorker

#retryable

Instance Method Summary collapse

Methods inherited from BaseWorker

#initialize

Constructor Details

This class inherits a constructor from Mongo::Retryable::BaseWorker

Instance Method Details

#read_with_one_retry(options = nil) { ... } ⇒ Result

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

This only retries read operations on socket errors.

Execute a read operation with a single retry on network errors.

This method is used by the driver for some of the internal housekeeping operations. Application-requested reads should use read_with_retry rather than this method.

Examples:

Execute the read.

read_with_one_retry do
  ...
end

Parameters:

  • options (Hash | nil) (defaults to: nil)

    Options.

Options Hash (options):

  • :retry_message (String)

    Message to log when retrying.

Yields:

  • Calls the provided block with no arguments

Returns:

  • (Result)

    The result of the operation.

Since:

  • 2.2.6



153
154
155
156
157
158
159
160
161
# File 'lib/mongo/retryable/read_worker.rb', line 153

def read_with_one_retry(options = nil)
  yield
rescue *retryable_exceptions, Error::PoolError => e
  raise e unless e.write_retryable?

  retry_message = options && options[:retry_message]
  log_retry(e, message: retry_message)
  yield
end

#read_with_retry(session = nil, server_selector = nil, context = nil, &block) ⇒ Result

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Execute a read operation with retrying.

This method performs server selection for the specified server selector and yields to the provided block, which should execute the initial query operation and return its result. The block will be passed the server selected for the operation. If the block raises an exception, and this exception corresponds to a read retryable error, and read retries are enabled for the client, this method will perform server selection again and yield to the block again (with potentially a different server). If the block returns successfully, the result of the block is returned.

If modern retry reads are on (which is the default), the initial read operation will be retried once. If legacy retry reads are on, the initial read operation will be retried zero or more times depending on the :max_read_retries client setting, the default for which is 1. To disable read retries, turn off modern read retries by setting retry_reads: false and set :max_read_retries to 0 on the client.

Examples:

Execute the read.

read_with_retry(session, server_selector) do |server|
  ...
end

Parameters:

  • session (Mongo::Session | nil) (defaults to: nil)

    The session that the operation is being run on.

  • server_selector (Mongo::ServerSelector::Selectable | nil) (defaults to: nil)

    Server selector for the operation.

  • context (Mongo::Operation::Context | nil) (defaults to: nil)

    Context for the read operation.

  • block (Proc)

    The block to execute.

Returns:

  • (Result)

    The result of the operation.

Since:

  • 2.19.0



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mongo/retryable/read_worker.rb', line 117

def read_with_retry(session = nil, server_selector = nil, context = nil, &block)
  if session.nil? && server_selector.nil?
    deprecated_legacy_read_with_retry(&block)
  elsif session&.retry_reads?
    modern_read_with_retry(session, server_selector, context, &block)
  elsif client.max_read_retries > 0
    legacy_read_with_retry(session, server_selector, context, &block)
  else
    read_without_retry(session, server_selector, &block)
  end
end

#read_with_retry_cursor(session, server_selector, view, context: nil, &block) ⇒ Cursor

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Execute a read operation returning a cursor with retrying.

This method performs server selection for the specified server selector and yields to the provided block, which should execute the initial query operation and return its result. The block will be passed the server selected for the operation. If the block raises an exception, and this exception corresponds to a read retryable error, and read retries are enabled for the client, this method will perform server selection again and yield to the block again (with potentially a different server). If the block returns successfully, the result of the block (which should be a Mongo::Operation::Result) is used to construct a Mongo::Cursor object for the result set. The cursor is then returned.

If modern retry reads are on (which is the default), the initial read operation will be retried once. If legacy retry reads are on, the initial read operation will be retried zero or more times depending on the :max_read_retries client setting, the default for which is 1. To disable read retries, turn off modern read retries by setting retry_reads: false and set :max_read_retries to 0 on the client.

Examples:

Execute a read returning a cursor.

cursor = read_with_retry_cursor(session, server_selector, view) do |server|
  # return a Mongo::Operation::Result
  ...
end

Parameters:

  • session (Mongo::Session)

    The session that the operation is being run on.

  • server_selector (Mongo::ServerSelector::Selectable)

    Server selector for the operation.

  • view (CollectionView)

    The CollectionView defining the query.

  • context (Operation::Context | nil) (defaults to: nil)

    the operation context to use with the cursor.

  • block (Proc)

    The block to execute.

Returns:

  • (Cursor)

    The cursor for the result set.

Since:

  • 2.19.0



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mongo/retryable/read_worker.rb', line 68

def read_with_retry_cursor(session, server_selector, view, context: nil, &block)
  read_with_retry(session, server_selector, context) do |server|
    result = yield server

    # RUBY-2367: This will be updated to allow the query cache to
    # cache cursors with multi-batch results.
    if QueryCache.enabled? && !view.collection.system_collection?
      CachingCursor.new(view, result, server, session: session, context: context)
    else
      Cursor.new(view, result, server, session: session, context: context)
    end
  end
end