Skip to content

Commit 5aa389c

Browse files
committed
RUBY-1104 Change name to CursorReaper
1 parent 216c8ff commit 5aa389c

File tree

5 files changed

+69
-54
lines changed

5 files changed

+69
-54
lines changed

lib/mongo/cluster.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
require 'mongo/cluster/topology'
16-
require 'mongo/cluster/cursor_manager'
16+
require 'mongo/cluster/cursor_reaper'
1717

1818
module Mongo
1919

@@ -43,7 +43,7 @@ class Cluster
4343
attr_reader :topology
4444

4545
def_delegators :topology, :replica_set?, :replica_set_name, :sharded?, :single?, :unknown?
46-
def_delegators :@cursor_manager, :register_cursor, :schedule_kill_cursor, :unregister_cursor
46+
def_delegators :@cursor_reaper, :register_cursor, :schedule_kill_cursor, :unregister_cursor
4747

4848
# Determine if this cluster of servers is equal to another object. Checks the
4949
# servers currently in the cluster, not what was configured.
@@ -116,8 +116,8 @@ def initialize(seeds, monitoring, options = Options::Redacted.new)
116116

117117
seeds.each{ |seed| add(seed) }
118118

119-
@cursor_manager = CursorManager.new(self)
120-
@cursor_manager.run
119+
@cursor_reaper = CursorReaper.new(self)
120+
@cursor_reaper.run
121121

122122
ObjectSpace.define_finalizer(self, self.class.finalize(pools))
123123
end
@@ -136,10 +136,10 @@ def initialize(seeds, monitoring, options = Options::Redacted.new)
136136
# @since 2.2.0
137137
def self.finalize(pools)
138138
proc do
139+
cursor_reaper.kill_cursors
139140
pools.values.each do |pool|
140141
pool.disconnect!
141142
end
142-
cursor_manager.kill_cursors
143143
end
144144
end
145145

lib/mongo/cluster/cursor_manager.rb renamed to lib/mongo/cluster/cursor_reaper.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ class Cluster
2121
# cursors that have been garbage collected without being exhausted.
2222
#
2323
# @since 2.3.0
24-
class CursorManager
24+
class CursorReaper
2525
extend Forwardable
2626
include Retryable
2727

28-
# @return [ Mongo::Cluster ] The cluster associated with this cursor manager.
28+
# @return [ Mongo::Cluster ] The cluster associated with this cursor reaper.
2929
attr_reader :cluster
3030

31-
# The default time interval for the cursor manager to send pending kill cursors operations.
31+
# The default time interval for the cursor reaper to send pending kill cursors operations.
3232
#
3333
# @since 2.3.0
3434
FREQUENCY = 1.freeze
3535

36-
# Create a cursor manager.
36+
# Create a cursor reaper.
3737
#
38-
# @example Create a CursorManager.
39-
# Mongo::Cluster::CursorManager.new(cluster)
38+
# @example Create a CursorReaper.
39+
# Mongo::Cluster::CursorReaper.new(cluster)
4040
#
4141
# @api private
4242
#
@@ -48,16 +48,16 @@ def initialize(cluster)
4848
@cluster = cluster
4949
end
5050

51-
# Start the cursor manager's reaper thread.
51+
# Start the cursor reapers's thread.
5252
#
53-
# @example Start the cursor manager's reaper thread.
54-
# manager.run
53+
# @example Start the cursor reaper's thread.
54+
# reaper.run
5555
#
5656
# @api private
5757
#
5858
# @since 2.3.0
5959
def run
60-
@reaper ||= Thread.new(FREQUENCY) do |i|
60+
@thread ||= Thread.new(FREQUENCY) do |i|
6161
loop do
6262
sleep(i)
6363
kill_cursors
@@ -68,7 +68,7 @@ def run
6868
# Schedule a kill cursors operation to be eventually executed.
6969
#
7070
# @example Schedule a kill cursors operation.
71-
# manager.schedule_kill_cursor(id, op_spec, server)
71+
# cursor_reaper.schedule_kill_cursor(id, op_spec, server)
7272
#
7373
# @param [ Integer ] id The id of the cursor to kill.
7474
# @param [ Hash ] op_spec The spec for the kill cursors op.
@@ -89,7 +89,7 @@ def schedule_kill_cursor(id, op_spec, server)
8989
# Register a cursor id as active.
9090
#
9191
# @example Register a cursor as active.
92-
# manager.register_cursor(id)
92+
# cursor_reaper.register_cursor(id)
9393
#
9494
# @param [ Integer ] id The id of the cursor to register as active.
9595
#
@@ -107,7 +107,7 @@ def register_cursor(id)
107107
# Unregister a cursor id, indicating that it's no longer active.
108108
#
109109
# @example Unregister a cursor.
110-
# manager.unregister_cursor(id)
110+
# cursor_reaper.unregister_cursor(id)
111111
#
112112
# @param [ Integer ] id The id of the cursor to unregister.
113113
#
@@ -123,7 +123,7 @@ def unregister_cursor(id)
123123
# Execute all pending kill cursors operations.
124124
#
125125
# @example Execute pending kill cursors operations.
126-
# manager.kill_cursors
126+
# cursor_reaper.kill_cursors
127127
#
128128
# @api private
129129
#

lib/mongo/cursor.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ def initialize(view, result, server)
6565
server))
6666
end
6767

68+
69+
# Finalize the cursor for garbage collection. Schedules this cursor to be included
70+
# in a killCursors operation executed by the Cluster's CursorReaper.
71+
#
72+
# @example Finalize the cluster.
73+
# Cursor.finalize(id, cluster, op, server)
74+
#
75+
# @param [ Integer ] cursor_id The cursor's id.
76+
# @param [ Mongo::Cluster ] cluster The cluster associated with this cursor and its server.
77+
# @param [ Hash ] op_spec The killCursors operation specification.
78+
# @param [ Mongo::Server ] server The server to send the killCursors operation to.
79+
#
80+
# @return [ Proc ] The Finalizer.
81+
#
82+
# @since 2.3.0
6883
def self.finalize(cursor_id, cluster, op_spec, server)
6984
proc { cluster.schedule_kill_cursor(cursor_id, op_spec, server) }
7085
end

spec/mongo/cluster/cursor_manager_spec.rb renamed to spec/mongo/cluster/cursor_reaper_spec.rb

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
require 'spec_helper'
22

3-
describe Mongo::Cluster::CursorManager do
3+
describe Mongo::Cluster::CursorReaper do
44

55
after do
66
authorized_collection.delete_many
77
end
88

9-
let(:manager) do
9+
let(:reaper) do
1010
described_class.new(authorized_client.cluster)
1111
end
1212

1313
let(:active_cursors) do
14-
manager.instance_variable_get(:@active_cursors)
14+
reaper.instance_variable_get(:@active_cursors)
1515
end
1616

1717
describe '#intialize' do
1818

1919
it 'initializes a hash for servers and their kill cursors ops' do
20-
expect(manager.instance_variable_get(:@to_kill)).to be_a(Hash)
20+
expect(reaper.instance_variable_get(:@to_kill)).to be_a(Hash)
2121
end
2222

2323
it 'initializes a set for the list of active cursors' do
24-
expect(manager.instance_variable_get(:@active_cursors)).to be_a(Set)
24+
expect(reaper.instance_variable_get(:@active_cursors)).to be_a(Set)
2525
end
2626
end
2727

2828
describe '#run' do
2929

3030
it 'starts a thread calling #kill_cursors' do
31-
manager.run
32-
expect(manager.instance_variable_get(:@reaper)).to be_a(Thread)
31+
reaper.run
32+
expect(reaper.instance_variable_get(:@thread)).to be_a(Thread)
3333
end
3434

3535
context 'when run is called more than once' do
3636

3737
let!(:reaper_thread) do
38-
manager.run
39-
manager.instance_variable_get(:@reaper)
38+
reaper.run
39+
reaper.instance_variable_get(:@reaper)
4040
end
4141

4242
it 'only starts a thread once' do
43-
manager.run
44-
expect(manager.instance_variable_get(:@reaper)).to be(reaper_thread)
43+
reaper.run
44+
expect(reaper.instance_variable_get(:@reaper)).to be(reaper_thread)
4545
end
4646
end
4747

@@ -51,17 +51,17 @@
5151
let(:cursor_id) { 1 }
5252
let(:op_spec_1) { double('op_spec_1') }
5353
let(:op_spec_2) { double('op_spec_2') }
54-
let(:to_kill) { manager.instance_variable_get(:@to_kill)}
54+
let(:to_kill) { reaper.instance_variable_get(:@to_kill)}
5555

5656
before do
57-
manager.register_cursor(cursor_id)
58-
manager.schedule_kill_cursor(cursor_id, op_spec_1, server)
59-
manager.run
60-
sleep(Mongo::Cluster::CursorManager::FREQUENCY + 0.5)
57+
reaper.register_cursor(cursor_id)
58+
reaper.schedule_kill_cursor(cursor_id, op_spec_1, server)
59+
reaper.run
60+
sleep(Mongo::Cluster::CursorReaper::FREQUENCY + 0.5)
6161
end
6262

6363
it 'executes the ops in the thread' do
64-
expect(manager.instance_variable_get(:@to_kill).size).to eq(0)
64+
expect(reaper.instance_variable_get(:@to_kill).size).to eq(0)
6565
end
6666
end
6767
end
@@ -72,18 +72,18 @@
7272
let(:cursor_id) { 1 }
7373
let(:op_spec_1) { double('op_spec_1') }
7474
let(:op_spec_2) { double('op_spec_2') }
75-
let(:to_kill) { manager.instance_variable_get(:@to_kill)}
75+
let(:to_kill) { reaper.instance_variable_get(:@to_kill)}
7676

7777
context 'when the cursor is on the list of active cursors' do
7878

7979
before do
80-
manager.register_cursor(cursor_id)
80+
reaper.register_cursor(cursor_id)
8181
end
8282

8383
context 'when there is not a list already for the server' do
8484

8585
before do
86-
manager.schedule_kill_cursor(cursor_id, op_spec_1, server)
86+
reaper.schedule_kill_cursor(cursor_id, op_spec_1, server)
8787
end
8888

8989
it 'initializes the list of op specs to a set' do
@@ -95,8 +95,8 @@
9595
context 'when there is a list of ops already for the server' do
9696

9797
before do
98-
manager.schedule_kill_cursor(cursor_id, op_spec_1, server)
99-
manager.schedule_kill_cursor(cursor_id, op_spec_2, server)
98+
reaper.schedule_kill_cursor(cursor_id, op_spec_1, server)
99+
reaper.schedule_kill_cursor(cursor_id, op_spec_2, server)
100100
end
101101

102102
it 'adds the op to the server list' do
@@ -107,7 +107,7 @@
107107
context 'when the same op is added more than once' do
108108

109109
before do
110-
manager.schedule_kill_cursor(cursor_id, op_spec_2, server)
110+
reaper.schedule_kill_cursor(cursor_id, op_spec_2, server)
111111
end
112112

113113
it 'does not allow duplicates ops for a server' do
@@ -121,7 +121,7 @@
121121
context 'when the cursor is not on the list of active cursors' do
122122

123123
before do
124-
manager.schedule_kill_cursor(cursor_id, op_spec_1, server)
124+
reaper.schedule_kill_cursor(cursor_id, op_spec_1, server)
125125
end
126126

127127
it 'does not add the kill cursors op spec to the list' do
@@ -133,7 +133,7 @@
133133
describe '#register_cursor' do
134134

135135
before do
136-
manager.register_cursor(cursor_id)
136+
reaper.register_cursor(cursor_id)
137137
end
138138

139139
context 'when the cursor id is nil' do
@@ -175,8 +175,8 @@
175175
context 'when the cursor id is in the active cursors list' do
176176

177177
before do
178-
manager.register_cursor(2)
179-
manager.unregister_cursor(2)
178+
reaper.register_cursor(2)
179+
reaper.unregister_cursor(2)
180180
end
181181

182182
it 'removes the cursor id' do

spec/mongo/cursor_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@
241241

242242
before do
243243
authorized_collection.insert_many(documents)
244-
cursor_manager.schedule_kill_cursor(cursor.id,
244+
cursor_reaper.schedule_kill_cursor(cursor.id,
245245
cursor.send(:kill_cursors_op_spec),
246246
cursor.instance_variable_get(:@server))
247247
end
@@ -263,13 +263,13 @@
263263
view.instance_variable_get(:@cursor)
264264
end
265265

266-
let(:cursor_manager) do
267-
authorized_client.cluster.instance_variable_get(:@cursor_manager)
266+
let(:cursor_reaper) do
267+
authorized_client.cluster.instance_variable_get(:@cursor_reaper)
268268
end
269269

270270

271271
it 'schedules a kill cursors op' do
272-
sleep(Mongo::Cluster::CursorManager::FREQUENCY + 0.5)
272+
sleep(Mongo::Cluster::CursorReaper::FREQUENCY + 0.5)
273273
expect {
274274
cursor.to_a
275275
}.to raise_exception(Mongo::Error::OperationFailure)
@@ -278,7 +278,7 @@
278278
context 'when the cursor is unregistered before the kill cursors operations are executed' do
279279

280280
it 'does not send a kill cursors operation for the unregistered cursor' do
281-
cursor_manager.unregister_cursor(cursor.id)
281+
cursor_reaper.unregister_cursor(cursor.id)
282282
expect(cursor.to_a.size).to eq(documents.size)
283283
end
284284
end
@@ -312,13 +312,13 @@
312312
view.to_enum
313313
end
314314

315-
let(:cursor_manager) do
316-
authorized_collection.client.cluster.instance_variable_get(:@cursor_manager)
315+
let(:cursor_reaper) do
316+
authorized_collection.client.cluster.instance_variable_get(:@cursor_reaper)
317317
end
318318

319319
it 'removes the cursor id from the active cursors tracked by the cluster cursor manager' do
320320
enum.next
321-
expect(cursor_manager.instance_variable_get(:@active_cursors)).not_to include(cursor_id)
321+
expect(cursor_reaper.instance_variable_get(:@active_cursors)).not_to include(cursor_id)
322322
end
323323
end
324324
end

0 commit comments

Comments
 (0)