Class: Mongo::Cluster::CursorReaper Private

Inherits:
Object
  • Object
show all
Includes:
Retryable
Defined in:
build/ruby-driver-master/lib/mongo/cluster/reapers/cursor_reaper.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.

A manager that sends kill cursors operations at regular intervals to close cursors that have been garbage collected without being exhausted.

Since:

  • 2.3.0

Constant Summary collapse

FREQUENCY =

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

The default time interval for the cursor reaper to send pending kill cursors operations.

Since:

  • 2.3.0

1.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Retryable

#legacy_write_with_retry, #nro_write_with_retry, #read_with_one_retry, #read_with_retry, #read_with_retry_cursor, #write_with_retry

Constructor Details

#initialize(cluster) ⇒ CursorReaper

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.

Create a cursor reaper.

Parameters:

  • cluster (Cluster)

    The cluster.

Since:

  • 2.3.0



42
43
44
45
46
47
# File 'build/ruby-driver-master/lib/mongo/cluster/reapers/cursor_reaper.rb', line 42

def initialize(cluster)
  @cluster = cluster
  @to_kill = {}
  @active_cursor_ids = Set.new
  @mutex = Mutex.new
end

Instance Attribute Details

#clusterObject (readonly)

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.

Since:

  • 2.3.0



49
50
51
# File 'build/ruby-driver-master/lib/mongo/cluster/reapers/cursor_reaper.rb', line 49

def cluster
  @cluster
end

Instance Method Details

#kill_cursorsObject Also known as: execute, flush

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 all pending kill cursors operations.

Examples:

Execute pending kill cursors operations.

cursor_reaper.kill_cursors

Since:

  • 2.3.0



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
# File 'build/ruby-driver-master/lib/mongo/cluster/reapers/cursor_reaper.rb', line 121

def kill_cursors
  # TODO optimize this to batch kill cursor operations for the same
  # server/database/collection instead of killing each cursor
  # individually.

  loop do
    server_address_str = nil

    kill_spec = @mutex.synchronize do
      # Find a server that has any cursors scheduled for destruction.
      server_address_str, specs =
        @to_kill.detect { |server_address_str, specs| specs.any? }

      if specs.nil?
        # All servers have empty specs, nothing to do.
        return
      end

      # Note that this mutates the spec in the queue.
      # If the kill cursor operation fails, we don't attempt to
      # kill that cursor again.
      spec = specs.take(1).tap do |arr|
        specs.subtract(arr)
      end.first

      unless @active_cursor_ids.include?(spec.cursor_id)
        # The cursor was already killed, typically because it has
        # been iterated to completion. Remove the kill spec from
        # our records without doing any more work.
        spec = nil
      end

      spec
    end

    # If there was a spec to kill but its cursor was already killed,
    # look for another spec.
    next unless kill_spec

    # We could also pass kill_spec directly into the KillCursors
    # operation, though this would make that operation have a
    # different API from all of the other ones which accept hashes.
    spec = {
      cursor_ids: [kill_spec.cursor_id],
      coll_name: kill_spec.coll_name,
      db_name: kill_spec.db_name,
    }
    op = Operation::KillCursors.new(spec)

    server = cluster.servers.detect do |server|
      server.address.seed == server_address_str
    end

    unless server
      # TODO We currently don't have a server for the address that the
      # cursor is associated with. We should leave the cursor in the
      # queue to be killed at a later time (when the server comes back).
      next
    end

    options = {
      server_api: server.options[:server_api],
      service_id: kill_spec.service_id,
    }
    op.execute(server, context: Operation::Context.new(options: options))
  end
end

#register_cursor(id) ⇒ Object

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.

Register a cursor id as active.

Examples:

Register a cursor as active.

cursor_reaper.register_cursor(id)

Parameters:

  • id (Integer)

    The id of the cursor to register as active.

Since:

  • 2.3.0



77
78
79
80
81
82
83
84
85
86
87
88
# File 'build/ruby-driver-master/lib/mongo/cluster/reapers/cursor_reaper.rb', line 77

def register_cursor(id)
  if id.nil?
    raise ArgumentError, 'register_cursor called with nil cursor_id'
  end
  if id == 0
    raise ArgumentError, 'register_cursor called with cursor_id=0'
  end

  @mutex.synchronize do
    @active_cursor_ids << id
  end
end

#schedule_kill_cursor(kill_spec, server) ⇒ Object

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.

Schedule a kill cursors operation to be eventually executed.

Parameters:

Since:

  • 2.3.0



58
59
60
61
62
63
64
65
# File 'build/ruby-driver-master/lib/mongo/cluster/reapers/cursor_reaper.rb', line 58

def schedule_kill_cursor(kill_spec, server)
  @mutex.synchronize do
    if @active_cursor_ids.include?(kill_spec.cursor_id)
      @to_kill[server.address.seed] ||= Set.new
      @to_kill[server.address.seed] << kill_spec
    end
  end
end

#unregister_cursor(id) ⇒ Object

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.

Unregister a cursor id, indicating that it's no longer active.

Examples:

Unregister a cursor.

cursor_reaper.unregister_cursor(id)

Parameters:

  • id (Integer)

    The id of the cursor to unregister.

Since:

  • 2.3.0



100
101
102
103
104
105
106
107
108
109
110
111
# File 'build/ruby-driver-master/lib/mongo/cluster/reapers/cursor_reaper.rb', line 100

def unregister_cursor(id)
  if id.nil?
    raise ArgumentError, 'unregister_cursor called with nil cursor_id'
  end
  if id == 0
    raise ArgumentError, 'unregister_cursor called with cursor_id=0'
  end

  @mutex.synchronize do
    @active_cursor_ids.delete(id)
  end
end