Skip to content

Commit b41d000

Browse files
committed
Merge pull request #597 from estolfo/RUBY-891-standalone
RUBY-891 Single topology type servers and slaveOk bit
2 parents 1b66b0e + c4126d3 commit b41d000

File tree

6 files changed

+41
-9
lines changed

6 files changed

+41
-9
lines changed

lib/mongo/cluster.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ def add(host)
6969
unless addresses.include?(address)
7070
log_debug([ "Adding #{address.to_s} to the cluster." ])
7171
addresses.push(address)
72-
server = Server.new(address, event_listeners, options)
72+
server = Server.new(address, event_listeners,
73+
options.merge(slave_ok: @slave_ok))
7374
@servers.push(server)
7475
server
7576
end
@@ -90,6 +91,7 @@ def initialize(seeds, options = {})
9091
@event_listeners = Event::Listeners.new
9192
@options = options.freeze
9293
@topology = Topology.initial(seeds, options)
94+
@slave_ok = @topology.single? unless options[:read]
9395

9496
subscribe_to(Event::SERVER_ADDED, Event::ServerAdded.new(self))
9597
subscribe_to(Event::SERVER_REMOVED, Event::ServerRemoved.new(self))

lib/mongo/cluster/topology/single.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ def replica_set_name; nil; end
9494
#
9595
# @since 2.0.0
9696
def servers(servers, name = nil)
97-
[ servers.detect do |server|
98-
!server.unknown? && !server.arbiter? && !server.ghost?
99-
end ]
97+
[ servers.detect { |server| !server.unknown? } ]
10098
end
10199

102100
# A single topology is not sharded.

lib/mongo/operation/read_preferrable.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,27 @@ module ReadPreferrable
2222

2323
private
2424

25+
def update_selector(context)
26+
if context.mongos? && read_pref = read.to_mongos
27+
selector.merge(:$readPreference => read_pref)
28+
else
29+
selector
30+
end
31+
end
32+
33+
def update_options(context)
34+
if context.slave_ok?
35+
options.merge(flags: [:slave_ok])
36+
elsif !context.mongos? && read.slave_ok?
37+
options.merge(flags: [:slave_ok])
38+
else
39+
options
40+
end
41+
end
42+
2543
def message(context)
26-
sel = (context.mongos? && read_pref = read.to_mongos) ?
27-
selector.merge(:$readPreference => read_pref) : selector
28-
opts = context.standalone? || read.slave_ok? ?
29-
options.merge(flags: [:slave_ok]) : options
44+
sel = update_selector(context)
45+
opts = update_options(context)
3046
Protocol::Query.new(db_name, query_coll, sel, opts)
3147
end
3248
end

lib/mongo/server.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,17 @@ def matches_tag_set?(tag_set)
159159
tags[k] && tags[k] == tag_set[k]
160160
end
161161
end
162+
163+
# Whether the slaveOk bit must be set for all queries sent to the server.
164+
#
165+
# @example Does the slaveOk bit need to be set for all queries sent to the server.
166+
# server.slave_ok?
167+
#
168+
# @return [ true, false ] If the slaveOk bit needs to be set for all queries.
169+
#
170+
# @since 2.0.0
171+
def slave_ok?
172+
options[:slave_ok]
173+
end
162174
end
163175
end

lib/mongo/server/context.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class Context
3333
:mongos?,
3434
:primary?,
3535
:secondary?,
36-
:standalone?
36+
:standalone?,
37+
:slave_ok?
3738

3839
# Instantiate a server context.
3940
#

spec/support/shared/operation.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
allow(cxt).to receive(:primary?) { true }
4444
allow(cxt).to receive(:secondary?) { false }
4545
allow(cxt).to receive(:standalone?) { false }
46+
allow(cxt).to receive(:slave_ok?) { false }
4647
end
4748
end
4849
let(:secondary_context) do
@@ -54,6 +55,7 @@
5455
allow(cxt).to receive(:secondary?) { true }
5556
allow(cxt).to receive(:primary?) { false }
5657
allow(cxt).to receive(:standalone?) { false }
58+
allow(cxt).to receive(:slave_ok?) { false }
5759
end
5860
end
5961
let(:primary_context_2_4_version) do
@@ -64,6 +66,7 @@
6466
allow(cxt).to receive(:primary?) { true }
6567
allow(cxt).to receive(:secondary?) { false }
6668
allow(cxt).to receive(:standalone?) { false }
69+
allow(cxt).to receive(:slave_ok?) { false }
6770
allow(cxt).to receive(:features) { features_2_4 }
6871
end
6972
end

0 commit comments

Comments
 (0)