Class: Mongo::Cluster::CursorReaper

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Retryable
Defined in:
lib/mongo/cluster/cursor_reaper.rb

Overview

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

FREQUENCY =

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

Since:

  • 2.3.0

1.freeze

Instance Method Summary collapse

Methods included from Retryable

#read_with_one_retry, #read_with_retry, #write_with_retry

Constructor Details

#initializeCursorReaper

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.

Examples:

Create a CursorReaper.

Mongo::Cluster::CursorReaper.new(cluster)

Since:

  • 2.3.0



41
42
43
44
45
46
# File 'lib/mongo/cluster/cursor_reaper.rb', line 41

def initialize
  @to_kill = {}
  @active_cursors = Set.new
  @mutex = Mutex.new
  @thread = nil
end

Instance Method Details

#kill_cursorsObject

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



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
# File 'lib/mongo/cluster/cursor_reaper.rb', line 136

def kill_cursors
  to_kill_copy = {}
  active_cursors_copy = []

  @mutex.synchronize do
    to_kill_copy = @to_kill.dup
    active_cursors_copy = @active_cursors.dup
    @to_kill = {}
  end

  to_kill_copy.each do |server, op_specs|
    op_specs.each do |op_spec|
      if server.features.find_command_enabled?
        Cursor::Builder::KillCursorsCommand.update_cursors(op_spec, active_cursors_copy.to_a)
        if Cursor::Builder::KillCursorsCommand.get_cursors_list(op_spec).size > 0
          Operation::Commands::Command.new(op_spec).execute(server)
        end
      else
        Cursor::Builder::OpKillCursors.update_cursors(op_spec, active_cursors_copy.to_a)
        if Cursor::Builder::OpKillCursors.get_cursors_list(op_spec).size > 0
          Operation::KillCursors.new(op_spec).execute(server)
        end
      end
    end
  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



92
93
94
95
96
97
98
# File 'lib/mongo/cluster/cursor_reaper.rb', line 92

def register_cursor(id)
  if id && id > 0
    @mutex.synchronize do
      @active_cursors << id
    end
  end
end

#run!Object Also known as: restart!

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.

Start the cursor reaper's thread.

Examples:

Start the cursor reaper's thread.

reaper.run!

Since:

  • 2.3.0



56
57
58
# File 'lib/mongo/cluster/cursor_reaper.rb', line 56

def run!
  @thread && @thread.alive? ? @thread : start!
end

#schedule_kill_cursor(id, op_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.

Examples:

Schedule a kill cursors operation.

cursor_reaper.schedule_kill_cursor(id, op_spec, server)

Parameters:

  • id (Integer)

    The id of the cursor to kill.

  • op_spec (Hash)

    The spec for the kill cursors op.

  • server (Mongo::Server)

    The server to send the kill cursors operation to.

Since:

  • 2.3.0



73
74
75
76
77
78
79
80
# File 'lib/mongo/cluster/cursor_reaper.rb', line 73

def schedule_kill_cursor(id, op_spec, server)
  @mutex.synchronize do
    if @active_cursors.include?(id)
      @to_kill[server] ||= Set.new
      @to_kill[server] << op_spec
    end
  end
end

#stop!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.

Stop the cursor reaper's thread.

Examples:

Stop the cursor reaper's thread.

reaper.stop!

Since:

  • 2.3.0



124
125
126
# File 'lib/mongo/cluster/cursor_reaper.rb', line 124

def stop!
  @thread.kill && @thread.stop?
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



110
111
112
113
114
# File 'lib/mongo/cluster/cursor_reaper.rb', line 110

def unregister_cursor(id)
  @mutex.synchronize do
    @active_cursors.delete(id)
  end
end