|
| 1 | +# Copyright (C) 2014-2015 MongoDB, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +require 'set' |
| 16 | + |
| 17 | +module Mongo |
| 18 | + class Cluster |
| 19 | + |
| 20 | + # A manager that sends kill cursors operations at regular intervals to close |
| 21 | + # cursors that have been garbage collected without being exhausted. |
| 22 | + # |
| 23 | + # @since 2.3.0 |
| 24 | + class CursorReaper |
| 25 | + extend Forwardable |
| 26 | + include Retryable |
| 27 | + |
| 28 | + # The default time interval for the cursor reaper to send pending kill cursors operations. |
| 29 | + # |
| 30 | + # @since 2.3.0 |
| 31 | + FREQUENCY = 1.freeze |
| 32 | + |
| 33 | + # Create a cursor reaper. |
| 34 | + # |
| 35 | + # @example Create a CursorReaper. |
| 36 | + # Mongo::Cluster::CursorReaper.new(cluster) |
| 37 | + # |
| 38 | + # @api private |
| 39 | + # |
| 40 | + # @since 2.3.0 |
| 41 | + def initialize |
| 42 | + @to_kill = {} |
| 43 | + @active_cursors = Set.new |
| 44 | + @mutex = Mutex.new |
| 45 | + end |
| 46 | + |
| 47 | + # Start the cursor reaper's thread. |
| 48 | + # |
| 49 | + # @example Start the cursor reaper's thread. |
| 50 | + # reaper.run! |
| 51 | + # |
| 52 | + # @api private |
| 53 | + # |
| 54 | + # @since 2.3.0 |
| 55 | + def run! |
| 56 | + @thread && @thread.alive? ? @thread : start! |
| 57 | + end |
| 58 | + alias :restart! :run! |
| 59 | + |
| 60 | + # Schedule a kill cursors operation to be eventually executed. |
| 61 | + # |
| 62 | + # @example Schedule a kill cursors operation. |
| 63 | + # cursor_reaper.schedule_kill_cursor(id, op_spec, server) |
| 64 | + # |
| 65 | + # @param [ Integer ] id The id of the cursor to kill. |
| 66 | + # @param [ Hash ] op_spec The spec for the kill cursors op. |
| 67 | + # @param [ Mongo::Server ] server The server to send the kill cursors operation to. |
| 68 | + # |
| 69 | + # @api private |
| 70 | + # |
| 71 | + # @since 2.3.0 |
| 72 | + def schedule_kill_cursor(id, op_spec, server) |
| 73 | + @mutex.synchronize do |
| 74 | + if @active_cursors.include?(id) |
| 75 | + @to_kill[server] ||= Set.new |
| 76 | + @to_kill[server] << op_spec |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + # Register a cursor id as active. |
| 82 | + # |
| 83 | + # @example Register a cursor as active. |
| 84 | + # cursor_reaper.register_cursor(id) |
| 85 | + # |
| 86 | + # @param [ Integer ] id The id of the cursor to register as active. |
| 87 | + # |
| 88 | + # @api private |
| 89 | + # |
| 90 | + # @since 2.3.0 |
| 91 | + def register_cursor(id) |
| 92 | + if id && id > 0 |
| 93 | + @mutex.synchronize do |
| 94 | + @active_cursors << id |
| 95 | + end |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + # Unregister a cursor id, indicating that it's no longer active. |
| 100 | + # |
| 101 | + # @example Unregister a cursor. |
| 102 | + # cursor_reaper.unregister_cursor(id) |
| 103 | + # |
| 104 | + # @param [ Integer ] id The id of the cursor to unregister. |
| 105 | + # |
| 106 | + # @api private |
| 107 | + # |
| 108 | + # @since 2.3.0 |
| 109 | + def unregister_cursor(id) |
| 110 | + @mutex.synchronize do |
| 111 | + @active_cursors.delete(id) |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + # Stop the cursor reaper's thread. |
| 116 | + # |
| 117 | + # @example Stop the cursor reaper's thread. |
| 118 | + # reaper.stop! |
| 119 | + # |
| 120 | + # @api private |
| 121 | + # |
| 122 | + # @since 2.3.0 |
| 123 | + def stop! |
| 124 | + @thread.kill && @thread.stop? |
| 125 | + end |
| 126 | + |
| 127 | + # Execute all pending kill cursors operations. |
| 128 | + # |
| 129 | + # @example Execute pending kill cursors operations. |
| 130 | + # cursor_reaper.kill_cursors |
| 131 | + # |
| 132 | + # @api private |
| 133 | + # |
| 134 | + # @since 2.3.0 |
| 135 | + def kill_cursors |
| 136 | + to_kill_copy = {} |
| 137 | + active_cursors_copy = [] |
| 138 | + |
| 139 | + @mutex.synchronize do |
| 140 | + to_kill_copy = @to_kill.dup |
| 141 | + active_cursors_copy = @active_cursors.dup |
| 142 | + @to_kill = {} |
| 143 | + end |
| 144 | + |
| 145 | + to_kill_copy.each do |server, op_specs| |
| 146 | + op_specs.each do |op_spec| |
| 147 | + if server.features.find_command_enabled? |
| 148 | + Cursor::Builder::KillCursorsCommand.update_cursors(op_spec, active_cursors_copy.to_a) |
| 149 | + if Cursor::Builder::KillCursorsCommand.get_cursors_list(op_spec).size > 0 |
| 150 | + Operation::Commands::Command.new(op_spec).execute(server) |
| 151 | + end |
| 152 | + else |
| 153 | + Cursor::Builder::OpKillCursors.update_cursors(op_spec, active_cursors_copy.to_a) |
| 154 | + if Cursor::Builder::OpKillCursors.get_cursors_list(op_spec).size > 0 |
| 155 | + Operation::KillCursors.new(op_spec).execute(server) |
| 156 | + end |
| 157 | + end |
| 158 | + end |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + private |
| 163 | + |
| 164 | + def start! |
| 165 | + @thread = Thread.new(FREQUENCY) do |i| |
| 166 | + loop do |
| 167 | + sleep(i) |
| 168 | + kill_cursors |
| 169 | + end |
| 170 | + end |
| 171 | + end |
| 172 | + end |
| 173 | + end |
| 174 | +end |
0 commit comments