Skip to content

Commit 9404ff1

Browse files
committed
PYTHON-525 Remove (_must)_use_master.
Left over from MasterSlaveConnection.
1 parent de23b63 commit 9404ff1

File tree

6 files changed

+56
-75
lines changed

6 files changed

+56
-75
lines changed

pymongo/collection.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ def process_cursor(cursor):
909909
'tag_sets': self.tag_sets,
910910
'secondary_acceptable_latency_ms': (
911911
self.secondary_acceptable_latency_ms),
912-
'_use_master': not self.read_preference}
912+
}
913913
command_kwargs.update(kwargs)
914914

915915
result, conn_id = self.__database._command(
@@ -1290,7 +1290,7 @@ def aggregate(self, pipeline, **kwargs):
12901290
'tag_sets': self.tag_sets,
12911291
'secondary_acceptable_latency_ms': (
12921292
self.secondary_acceptable_latency_ms),
1293-
'_use_master': not self.read_preference}
1293+
}
12941294

12951295
command_kwargs.update(kwargs)
12961296
result, conn_id = self.__database._command(
@@ -1365,7 +1365,6 @@ def group(self, key, condition, initial, reduce, finalize=None, **kwargs):
13651365
tag_sets=self.tag_sets,
13661366
secondary_acceptable_latency_ms=(
13671367
self.secondary_acceptable_latency_ms),
1368-
_use_master=not self.read_preference,
13691368
**kwargs)["retval"]
13701369

13711370
def rename(self, new_name, **kwargs):
@@ -1465,20 +1464,14 @@ def map_reduce(self, map, reduce, out, full_response=False, **kwargs):
14651464
raise TypeError("'out' must be an instance of "
14661465
"%s or dict" % (basestring.__name__,))
14671466

1468-
if isinstance(out, dict) and out.get('inline'):
1469-
must_use_master = False
1470-
else:
1471-
must_use_master = True
1472-
14731467
response = self.__database.command("mapreduce", self.__name,
14741468
uuid_subtype=self.uuid_subtype,
14751469
map=map, reduce=reduce,
14761470
read_preference=self.read_preference,
14771471
tag_sets=self.tag_sets,
14781472
secondary_acceptable_latency_ms=(
14791473
self.secondary_acceptable_latency_ms),
1480-
out=out, _use_master=must_use_master,
1481-
**kwargs)
1474+
out=out, **kwargs)
14821475

14831476
if full_response or not response.get('result'):
14841477
return response
@@ -1528,7 +1521,6 @@ def inline_map_reduce(self, map, reduce, full_response=False, **kwargs):
15281521
tag_sets=self.tag_sets,
15291522
secondary_acceptable_latency_ms=(
15301523
self.secondary_acceptable_latency_ms),
1531-
_use_master=not self.read_preference,
15321524
map=map, reduce=reduce,
15331525
out={"inline": 1}, **kwargs)
15341526

pymongo/cursor.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ def __init__(self, collection, spec=None, fields=None, skip=0, limit=0,
7070
await_data=False, partial=False, manipulate=True,
7171
read_preference=ReadPreference.PRIMARY,
7272
tag_sets=[{}], secondary_acceptable_latency_ms=None,
73-
exhaust=False, compile_re=True, _must_use_master=False,
74-
_uuid_subtype=None):
73+
exhaust=False, compile_re=True, _uuid_subtype=None):
7574
"""Create a new cursor.
7675
7776
Should not be called directly by application developers - see
@@ -152,7 +151,6 @@ def __init__(self, collection, spec=None, fields=None, skip=0, limit=0,
152151
self.__secondary_acceptable_latency_ms = secondary_acceptable_latency_ms
153152
self.__tz_aware = collection.database.connection.tz_aware
154153
self.__compile_re = compile_re
155-
self.__must_use_master = _must_use_master
156154
self.__uuid_subtype = _uuid_subtype or collection.uuid_subtype
157155

158156
self.__data = deque()
@@ -238,8 +236,7 @@ def _clone(self, deepcopy=True):
238236
"batch_size", "max_scan", "as_class",
239237
"manipulate", "read_preference", "tag_sets",
240238
"secondary_acceptable_latency_ms",
241-
"must_use_master", "uuid_subtype", "compile_re",
242-
"query_flags")
239+
"uuid_subtype", "compile_re", "query_flags")
243240
data = dict((k, v) for k, v in self.__dict__.iteritems()
244241
if k.startswith('_Cursor__') and k[9:] in values_to_clone)
245242
if deepcopy:
@@ -699,7 +696,6 @@ def count(self, with_limit_and_skip=False):
699696
command['tag_sets'] = self.__tag_sets
700697
command['secondary_acceptable_latency_ms'] = (
701698
self.__secondary_acceptable_latency_ms)
702-
command['_use_master'] = not self.__read_preference
703699
if self.__max_time_ms is not None:
704700
command["maxTimeMS"] = self.__max_time_ms
705701
if self.__comment:
@@ -755,7 +751,6 @@ def distinct(self, key):
755751
options['tag_sets'] = self.__tag_sets
756752
options['secondary_acceptable_latency_ms'] = (
757753
self.__secondary_acceptable_latency_ms)
758-
options['_use_master'] = not self.__read_preference
759754
if self.__max_time_ms is not None:
760755
options['maxTimeMS'] = self.__max_time_ms
761756
if self.__comment:
@@ -861,12 +856,13 @@ def __send_message(self, message):
861856
client = self.__collection.database.connection
862857

863858
if message:
864-
kwargs = {"_must_use_master": self.__must_use_master}
865-
kwargs["read_preference"] = self.__read_preference
866-
kwargs["tag_sets"] = self.__tag_sets
867-
kwargs["secondary_acceptable_latency_ms"] = (
868-
self.__secondary_acceptable_latency_ms)
869-
kwargs['exhaust'] = self.__exhaust
859+
kwargs = {
860+
"read_preference": self.__read_preference,
861+
"tag_sets": self.__tag_sets,
862+
"secondary_acceptable_latency_ms":
863+
self.__secondary_acceptable_latency_ms,
864+
"exhaust": self.__exhaust,
865+
}
870866
if self.__connection_id is not None:
871867
kwargs["_connection_to_use"] = self.__connection_id
872868

pymongo/database.py

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
ConfigurationError,
2727
InvalidName,
2828
OperationFailure)
29-
from pymongo import read_preferences as rp
29+
from pymongo.read_preferences import (ReadPreference,
30+
modes, secondary_ok_commands)
3031

3132

3233
def _check_name(name):
@@ -276,59 +277,56 @@ def _command(self, command, value=1,
276277
"""
277278

278279
if isinstance(command, basestring):
280+
command_name = command.lower()
279281
command = SON([(command, value)])
282+
else:
283+
command_name = command.keys()[0].lower()
284+
285+
orig = mode = kwargs.pop('read_preference', self.read_preference)
286+
tags = kwargs.pop('tag_sets', self.tag_sets)
287+
latency = kwargs.pop('secondary_acceptable_latency_ms',
288+
self.secondary_acceptable_latency_ms)
289+
as_class = kwargs.pop('as_class', None)
280290

281-
command_name = command.keys()[0].lower()
282-
must_use_master = kwargs.pop('_use_master', False)
283-
if command_name not in rp.secondary_ok_commands:
284-
must_use_master = True
291+
if command_name not in secondary_ok_commands:
292+
mode = ReadPreference.PRIMARY
285293

286294
# Special-case: mapreduce can go to secondaries only if inline
287-
if command_name == 'mapreduce':
295+
elif command_name == 'mapreduce':
288296
out = command.get('out') or kwargs.get('out')
289297
if not isinstance(out, dict) or not out.get('inline'):
290-
must_use_master = True
298+
mode = ReadPreference.PRIMARY
291299

292300
# Special-case: aggregate with $out cannot go to secondaries.
293-
if command_name == 'aggregate':
301+
elif command_name == 'aggregate':
294302
for stage in kwargs.get('pipeline', []):
295303
if '$out' in stage:
296-
must_use_master = True
304+
mode = ReadPreference.PRIMARY
297305
break
298306

299-
extra_opts = {
300-
'as_class': kwargs.pop('as_class', None),
301-
'_must_use_master': must_use_master,
302-
'_uuid_subtype': uuid_subtype
303-
}
304-
305-
extra_opts['read_preference'] = kwargs.pop(
306-
'read_preference',
307-
self.read_preference)
308-
extra_opts['tag_sets'] = kwargs.pop(
309-
'tag_sets',
310-
self.tag_sets)
311-
extra_opts['secondary_acceptable_latency_ms'] = kwargs.pop(
312-
'secondary_acceptable_latency_ms',
313-
self.secondary_acceptable_latency_ms)
314-
extra_opts['compile_re'] = compile_re
315-
316-
fields = kwargs.get('fields')
307+
# Warn if mode will override read_preference.
308+
if mode != orig:
309+
warnings.warn("%s does not support %s read preference "
310+
"and will be routed to the primary instead." %
311+
(command_name, modes[orig]), UserWarning)
312+
tags = [{}]
313+
latency = None
314+
315+
fields = kwargs.pop('fields', None)
317316
if fields is not None and not isinstance(fields, dict):
318-
kwargs['fields'] = helpers._fields_list_to_dict(fields)
317+
fields = helpers._fields_list_to_dict(fields)
319318

320319
command.update(kwargs)
321320

322-
# Warn if must_use_master will override read_preference.
323-
if (extra_opts['read_preference'] != rp.ReadPreference.PRIMARY and
324-
extra_opts['_must_use_master']):
325-
warnings.warn("%s does not support %s read preference "
326-
"and will be routed to the primary instead." %
327-
(command_name,
328-
rp.modes[extra_opts['read_preference']]),
329-
UserWarning)
330-
331-
cursor = self["$cmd"].find(command, **extra_opts).limit(-1)
321+
cursor = self["$cmd"].find(command,
322+
fields=fields,
323+
limit=-1,
324+
as_class=as_class,
325+
read_preference=mode,
326+
tag_sets=tags,
327+
secondary_acceptable_latency_ms=latency,
328+
compile_re=compile_re,
329+
_uuid_subtype=uuid_subtype)
332330
for doc in cursor:
333331
result = doc
334332

@@ -437,7 +435,8 @@ def collection_names(self, include_system_collections=True):
437435
- `include_system_collections` (optional): if ``False`` list
438436
will not include system collections (e.g ``system.indexes``)
439437
"""
440-
results = self["system.namespaces"].find(_must_use_master=True)
438+
results = self["system.namespaces"].find(
439+
read_preference=ReadPreference.PRIMARY)
441440
names = [r["name"] for r in results]
442441
names = [n[len(self.__name) + 1:] for n in names
443442
if n.startswith(self.__name + ".") and "$" not in n]

pymongo/mongo_client.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,10 +1165,7 @@ def __send_and_receive(self, message, sock_info):
11651165
sock_info.close()
11661166
raise
11671167

1168-
# we just ignore _must_use_master here: it's only relevant for
1169-
# MasterSlaveConnection instances.
1170-
def _send_message_with_response(self, message,
1171-
_must_use_master=False, **kwargs):
1168+
def _send_message_with_response(self, message, **kwargs):
11721169
"""Send a message to Mongo and return the response.
11731170
11741171
Sends the given message and returns the response.

pymongo/mongo_replica_set_client.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,8 +1566,8 @@ def __try_read(self, member, msg, **kwargs):
15661566
host, port = member.host
15671567
raise AutoReconnect("%s:%d: %s" % (host, port, why))
15681568

1569-
def _send_message_with_response(self, msg, _connection_to_use=None,
1570-
_must_use_master=False, **kwargs):
1569+
def _send_message_with_response(self, msg,
1570+
_connection_to_use=None, **kwargs):
15711571
"""Send a message to Mongo and return the response.
15721572
15731573
Sends the given message and returns (host used, response).
@@ -1576,16 +1576,12 @@ def _send_message_with_response(self, msg, _connection_to_use=None,
15761576
- `msg`: (request_id, data) pair making up the message to send
15771577
- `_connection_to_use`: Optional (host, port) of member for message,
15781578
used by Cursor for getMore and killCursors messages.
1579-
- `_must_use_master`: If True, send to primary.
15801579
"""
15811580
self._ensure_connected()
15821581

15831582
rs_state = self.__get_rs_state()
15841583
tag_sets = kwargs.get('tag_sets', [{}])
15851584
mode = kwargs.get('read_preference', ReadPreference.PRIMARY)
1586-
if _must_use_master:
1587-
mode = ReadPreference.PRIMARY
1588-
tag_sets = [{}]
15891585

15901586
if not rs_state.primary_member:
15911587
# If we were initialized with _connect=False then connect now.
@@ -1639,7 +1635,7 @@ def _send_message_with_response(self, msg, _connection_to_use=None,
16391635
pinned_member.host,
16401636
self.__try_read(pinned_member, msg, **kwargs))
16411637
except AutoReconnect, why:
1642-
if _must_use_master or mode == ReadPreference.PRIMARY:
1638+
if mode == ReadPreference.PRIMARY:
16431639
self.disconnect()
16441640
raise
16451641
else:

test/test_replica_set_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,8 @@ def test_copy_db(self):
480480
c.copy_database("pymongo_test", "pymongo_test1",
481481
username="mike", password="password")
482482
self.assertTrue("pymongo_test1" in c.database_names())
483-
res = c.pymongo_test1.test.find_one(_must_use_master=True)
483+
res = c.pymongo_test1.test.find_one(
484+
read_preference=ReadPreference.PRIMARY)
484485
self.assertEqual("bar", res["foo"])
485486
finally:
486487
# Cleanup

0 commit comments

Comments
 (0)