Class: Mongo::Cluster::CursorReaper Private

Inherits:
Object
  • Object
show all
Includes:
Retryable
Defined in:
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

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 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



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

def initialize
  @to_kill = {}
  @active_cursors = Set.new
  @mutex = Mutex.new
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



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

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



80
81
82
83
84
85
86
# File 'lib/mongo/cluster/reapers/cursor_reaper.rb', line 80

def register_cursor(id)
  if id && id > 0
    @mutex.synchronize do
      @active_cursors << id
    end
  end
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



61
62
63
64
65
66
67
68
# File 'lib/mongo/cluster/reapers/cursor_reaper.rb', line 61

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

#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



98
99
100
101
102
# File 'lib/mongo/cluster/reapers/cursor_reaper.rb', line 98

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