Skip to content

Commit 55c5aec

Browse files
committed
PYTHON-2707 Eliminate the use of 'slave'
1 parent dee74f2 commit 55c5aec

13 files changed

+101
-94
lines changed

pymongo/aggregation.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,18 @@ def _check_compat(sock_info):
9393
"""Check whether the server version in-use supports aggregation."""
9494
pass
9595

96-
def _process_result(self, result, session, server, sock_info, slave_ok):
96+
def _process_result(
97+
self, result, session, server, sock_info, secondary_ok):
9798
if self._result_processor:
9899
self._result_processor(
99-
result, session, server, sock_info, slave_ok)
100+
result, session, server, sock_info, secondary_ok)
100101

101102
def get_read_preference(self, session):
102103
if self._performs_write:
103104
return ReadPreference.PRIMARY
104105
return self._target._read_preference_for(session)
105106

106-
def get_cursor(self, session, server, sock_info, slave_ok):
107+
def get_cursor(self, session, server, sock_info, secondary_ok):
107108
# Ensure command compatibility.
108109
self._check_compat(sock_info)
109110

@@ -136,7 +137,7 @@ def get_cursor(self, session, server, sock_info, slave_ok):
136137
result = sock_info.command(
137138
self._database.name,
138139
cmd,
139-
slave_ok,
140+
secondary_ok,
140141
self.get_read_preference(session),
141142
self._target.codec_options,
142143
parse_write_concern_error=True,
@@ -147,7 +148,7 @@ def get_cursor(self, session, server, sock_info, slave_ok):
147148
client=self._database.client,
148149
user_fields=self._user_fields)
149150

150-
self._process_result(result, session, server, sock_info, slave_ok)
151+
self._process_result(result, session, server, sock_info, secondary_ok)
151152

152153
# Extract cursor from result or mock/fake one if necessary.
153154
if 'cursor' in result:

pymongo/change_stream.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ def _aggregation_pipeline(self):
148148
full_pipeline.extend(self._pipeline)
149149
return full_pipeline
150150

151-
def _process_result(self, result, session, server, sock_info, slave_ok):
151+
def _process_result(
152+
self, result, session, server, sock_info, secondary_ok):
152153
"""Callback that caches the postBatchResumeToken or
153154
startAtOperationTime from a changeStream aggregate command response
154155
containing an empty batch of change documents.

pymongo/collection.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def _socket_for_reads(self, session):
197197
def _socket_for_writes(self, session):
198198
return self.__database.client._socket_for_writes(session)
199199

200-
def _command(self, sock_info, command, slave_ok=False,
200+
def _command(self, sock_info, command, secondary_ok=False,
201201
read_preference=None,
202202
codec_options=None, check=True, allowable_errors=None,
203203
read_concern=None,
@@ -211,7 +211,7 @@ def _command(self, sock_info, command, slave_ok=False,
211211
:Parameters:
212212
- `sock_info` - A SocketInfo instance.
213213
- `command` - The command itself, as a SON instance.
214-
- `slave_ok`: whether to set the SlaveOkay wire protocol bit.
214+
- `secondary_ok`: whether to set the secondaryOkay wire protocol bit.
215215
- `codec_options` (optional) - An instance of
216216
:class:`~bson.codec_options.CodecOptions`.
217217
- `check`: raise OperationFailure if there are errors
@@ -238,7 +238,7 @@ def _command(self, sock_info, command, slave_ok=False,
238238
return sock_info.command(
239239
self.__database.name,
240240
command,
241-
slave_ok,
241+
secondary_ok,
242242
read_preference or self._read_preference_for(session),
243243
codec_options or self.codec_options,
244244
check,
@@ -1629,13 +1629,13 @@ def parallel_scan(self, num_cursors, session=None, **kwargs):
16291629
('numCursors', num_cursors)])
16301630
cmd.update(kwargs)
16311631

1632-
with self._socket_for_reads(session) as (sock_info, slave_ok):
1632+
with self._socket_for_reads(session) as (sock_info, secondary_ok):
16331633
# We call sock_info.command here directly, instead of
16341634
# calling self._command to avoid using an implicit session.
16351635
result = sock_info.command(
16361636
self.__database.name,
16371637
cmd,
1638-
slave_ok,
1638+
secondary_ok,
16391639
self._read_preference_for(session),
16401640
self.codec_options,
16411641
read_concern=self.read_concern,
@@ -1651,14 +1651,14 @@ def parallel_scan(self, num_cursors, session=None, **kwargs):
16511651

16521652
return cursors
16531653

1654-
def _count_cmd(self, session, sock_info, slave_ok, cmd, collation):
1654+
def _count_cmd(self, session, sock_info, secondary_ok, cmd, collation):
16551655
"""Internal count command helper."""
16561656
# XXX: "ns missing" checks can be removed when we drop support for
16571657
# MongoDB 3.0, see SERVER-17051.
16581658
res = self._command(
16591659
sock_info,
16601660
cmd,
1661-
slave_ok,
1661+
secondary_ok,
16621662
allowable_errors=["ns missing"],
16631663
codec_options=self.__write_response_codec_options,
16641664
read_concern=self.read_concern,
@@ -1672,20 +1672,20 @@ def _count(self, cmd, collation=None, session=None):
16721672
"""Internal count helper."""
16731673
# XXX: "ns missing" checks can be removed when we drop support for
16741674
# MongoDB 3.0, see SERVER-17051.
1675-
def _cmd(session, server, sock_info, slave_ok):
1675+
def _cmd(session, server, sock_info, secondary_ok):
16761676
return self._count_cmd(
1677-
session, sock_info, slave_ok, cmd, collation)
1677+
session, sock_info, secondary_ok, cmd, collation)
16781678

16791679
return self.__database.client._retryable_read(
16801680
_cmd, self._read_preference_for(session), session)
16811681

16821682
def _aggregate_one_result(
1683-
self, sock_info, slave_ok, cmd, collation, session):
1683+
self, sock_info, secondary_ok, cmd, collation, session):
16841684
"""Internal helper to run an aggregate that returns a single result."""
16851685
result = self._command(
16861686
sock_info,
16871687
cmd,
1688-
slave_ok,
1688+
secondary_ok,
16891689
allowable_errors=[26], # Ignore NamespaceNotFound.
16901690
codec_options=self.__write_response_codec_options,
16911691
read_concern=self.read_concern,
@@ -1719,7 +1719,7 @@ def estimated_document_count(self, **kwargs):
17191719
raise ConfigurationError(
17201720
'estimated_document_count does not support sessions')
17211721

1722-
def _cmd(session, server, sock_info, slave_ok):
1722+
def _cmd(session, server, sock_info, secondary_ok):
17231723
if sock_info.max_wire_version >= 12:
17241724
# MongoDB 4.9+
17251725
pipeline = [
@@ -1731,15 +1731,15 @@ def _cmd(session, server, sock_info, slave_ok):
17311731
('cursor', {})])
17321732
cmd.update(kwargs)
17331733
result = self._aggregate_one_result(
1734-
sock_info, slave_ok, cmd, collation=None, session=session)
1734+
sock_info, secondary_ok, cmd, collation=None, session=session)
17351735
if not result:
17361736
return 0
17371737
return int(result['n'])
17381738
else:
17391739
# MongoDB < 4.9
17401740
cmd = SON([('count', self.__name)])
17411741
cmd.update(kwargs)
1742-
return self._count_cmd(None, sock_info, slave_ok, cmd, None)
1742+
return self._count_cmd(None, sock_info, secondary_ok, cmd, None)
17431743

17441744
return self.__database.client._retryable_read(
17451745
_cmd, self.read_preference, None)
@@ -1816,9 +1816,9 @@ def count_documents(self, filter, session=None, **kwargs):
18161816
collation = validate_collation_or_none(kwargs.pop('collation', None))
18171817
cmd.update(kwargs)
18181818

1819-
def _cmd(session, server, sock_info, slave_ok):
1819+
def _cmd(session, server, sock_info, secondary_ok):
18201820
result = self._aggregate_one_result(
1821-
sock_info, slave_ok, cmd, collation, session)
1821+
sock_info, secondary_ok, cmd, collation, session)
18221822
if not result:
18231823
return 0
18241824
return result['n']
@@ -2294,12 +2294,12 @@ def list_indexes(self, session=None):
22942294
read_pref = ((session and session._txn_read_preference())
22952295
or ReadPreference.PRIMARY)
22962296

2297-
def _cmd(session, server, sock_info, slave_ok):
2297+
def _cmd(session, server, sock_info, secondary_ok):
22982298
cmd = SON([("listIndexes", self.__name), ("cursor", {})])
22992299
if sock_info.max_wire_version > 2:
23002300
with self.__database.client._tmp_session(session, False) as s:
23012301
try:
2302-
cursor = self._command(sock_info, cmd, slave_ok,
2302+
cursor = self._command(sock_info, cmd, secondary_ok,
23032303
read_pref,
23042304
codec_options,
23052305
session=s)["cursor"]
@@ -2315,7 +2315,7 @@ def _cmd(session, server, sock_info, slave_ok):
23152315
else:
23162316
res = message._first_batch(
23172317
sock_info, self.__database.name, "system.indexes",
2318-
{"ns": self.__full_name}, 0, slave_ok, codec_options,
2318+
{"ns": self.__full_name}, 0, secondary_ok, codec_options,
23192319
read_pref, cmd,
23202320
self.database.client._event_listeners)
23212321
cursor = res["cursor"]
@@ -2676,8 +2676,8 @@ def group(self, key, condition, initial, reduce, finalize=None, **kwargs):
26762676
collation = validate_collation_or_none(kwargs.pop('collation', None))
26772677
cmd.update(kwargs)
26782678

2679-
with self._socket_for_reads(session=None) as (sock_info, slave_ok):
2680-
return self._command(sock_info, cmd, slave_ok,
2679+
with self._socket_for_reads(session=None) as (sock_info, secondary_ok):
2680+
return self._command(sock_info, cmd, secondary_ok,
26812681
collation=collation,
26822682
user_fields={'retval': 1})["retval"]
26832683

@@ -2780,9 +2780,9 @@ def distinct(self, key, filter=None, session=None, **kwargs):
27802780
kwargs["query"] = filter
27812781
collation = validate_collation_or_none(kwargs.pop('collation', None))
27822782
cmd.update(kwargs)
2783-
def _cmd(session, server, sock_info, slave_ok):
2783+
def _cmd(session, server, sock_info, secondary_ok):
27842784
return self._command(
2785-
sock_info, cmd, slave_ok, read_concern=self.read_concern,
2785+
sock_info, cmd, secondary_ok, read_concern=self.read_concern,
27862786
collation=collation, session=session,
27872787
user_fields={"values": 1})["values"]
27882788

@@ -2809,7 +2809,7 @@ def _map_reduce(self, map, reduce, out, session, read_pref, **kwargs):
28092809
or read_pref)
28102810

28112811
with self.__database.client._socket_for_reads(read_pref, session) as (
2812-
sock_info, slave_ok):
2812+
sock_info, secondary_ok):
28132813
if (sock_info.max_wire_version >= 4 and
28142814
('readConcern' not in cmd) and
28152815
inline):
@@ -2822,7 +2822,7 @@ def _map_reduce(self, map, reduce, out, session, read_pref, **kwargs):
28222822
write_concern = None
28232823

28242824
return self._command(
2825-
sock_info, cmd, slave_ok, read_pref,
2825+
sock_info, cmd, secondary_ok, read_pref,
28262826
read_concern=read_concern,
28272827
write_concern=write_concern,
28282828
collation=collation, session=session,

pymongo/cursor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171

7272
_QUERY_OPTIONS = {
7373
"tailable_cursor": 2,
74-
"slave_okay": 4,
74+
"secondary_okay": 4,
7575
"oplog_replay": 8,
7676
"no_timeout": 16,
7777
"await_data": 32,

pymongo/database.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,9 @@ def watch(self, pipeline=None, full_document=None, resume_after=None,
620620
batch_size, collation, start_at_operation_time, session,
621621
start_after)
622622

623-
def _command(self, sock_info, command, slave_ok=False, value=1, check=True,
624-
allowable_errors=None, read_preference=ReadPreference.PRIMARY,
623+
def _command(self, sock_info, command, secondary_ok=False, value=1,
624+
check=True, allowable_errors=None,
625+
read_preference=ReadPreference.PRIMARY,
625626
codec_options=DEFAULT_CODEC_OPTIONS,
626627
write_concern=None,
627628
parse_write_concern_error=False, session=None, **kwargs):
@@ -634,7 +635,7 @@ def _command(self, sock_info, command, slave_ok=False, value=1, check=True,
634635
return sock_info.command(
635636
self.__name,
636637
command,
637-
slave_ok,
638+
secondary_ok,
638639
read_preference,
639640
codec_options,
640641
check,
@@ -748,8 +749,8 @@ def command(self, command, value=1, check=True,
748749
read_preference = ((session and session._txn_read_preference())
749750
or ReadPreference.PRIMARY)
750751
with self.__client._socket_for_reads(
751-
read_preference, session) as (sock_info, slave_ok):
752-
return self._command(sock_info, command, slave_ok, value,
752+
read_preference, session) as (sock_info, secondary_ok):
753+
return self._command(sock_info, command, secondary_ok, value,
753754
check, allowable_errors, read_preference,
754755
codec_options, session=session, **kwargs)
755756

@@ -761,15 +762,15 @@ def _retryable_read_command(self, command, value=1, check=True,
761762
read_preference = ((session and session._txn_read_preference())
762763
or ReadPreference.PRIMARY)
763764

764-
def _cmd(session, server, sock_info, slave_ok):
765-
return self._command(sock_info, command, slave_ok, value,
765+
def _cmd(session, server, sock_info, secondary_ok):
766+
return self._command(sock_info, command, secondary_ok, value,
766767
check, allowable_errors, read_preference,
767768
codec_options, session=session, **kwargs)
768769

769770
return self.__client._retryable_read(
770771
_cmd, read_preference, session)
771772

772-
def _list_collections(self, sock_info, slave_okay, session,
773+
def _list_collections(self, sock_info, secondary_okay, session,
773774
read_preference, **kwargs):
774775
"""Internal listCollections helper."""
775776

@@ -782,7 +783,7 @@ def _list_collections(self, sock_info, slave_okay, session,
782783
with self.__client._tmp_session(
783784
session, close=False) as tmp_session:
784785
cursor = self._command(
785-
sock_info, cmd, slave_okay,
786+
sock_info, cmd, secondary_okay,
786787
read_preference=read_preference,
787788
session=tmp_session)["cursor"]
788789
cmd_cursor = CommandCursor(
@@ -804,7 +805,7 @@ def _list_collections(self, sock_info, slave_okay, session,
804805
cmd = SON([("aggregate", "system.namespaces"),
805806
("pipeline", pipeline),
806807
("cursor", kwargs.get("cursor", {}))])
807-
cursor = self._command(sock_info, cmd, slave_okay)["cursor"]
808+
cursor = self._command(sock_info, cmd, secondary_okay)["cursor"]
808809
cmd_cursor = CommandCursor(coll, cursor, sock_info.address)
809810
cmd_cursor._maybe_pin_connection(sock_info)
810811
return cmd_cursor
@@ -833,9 +834,9 @@ def list_collections(self, session=None, filter=None, **kwargs):
833834
read_pref = ((session and session._txn_read_preference())
834835
or ReadPreference.PRIMARY)
835836

836-
def _cmd(session, server, sock_info, slave_okay):
837+
def _cmd(session, server, sock_info, secondary_okay):
837838
return self._list_collections(
838-
sock_info, slave_okay, session, read_preference=read_pref,
839+
sock_info, secondary_okay, session, read_preference=read_pref,
839840
**kwargs)
840841

841842
return self.__client._retryable_read(

pymongo/message.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def _maybe_add_read_preference(spec, read_preference):
106106
# problems with mongos versions that don't support read preferences. Also,
107107
# for maximum backwards compatibility, don't add $readPreference for
108108
# secondaryPreferred unless tags or maxStalenessSeconds are in use (setting
109-
# the slaveOkay bit has the same effect).
109+
# the secondaryOkay bit has the same effect).
110110
if mode and (
111111
mode != ReadPreference.SECONDARY_PREFERRED.mode or
112112
len(document) > 1):
@@ -333,10 +333,10 @@ def as_command(self, sock_info):
333333
self._as_command = cmd, self.db
334334
return self._as_command
335335

336-
def get_message(self, set_slave_ok, sock_info, use_cmd=False):
337-
"""Get a query message, possibly setting the slaveOk bit."""
338-
if set_slave_ok:
339-
# Set the slaveOk bit.
336+
def get_message(self, set_secondary_ok, sock_info, use_cmd=False):
337+
"""Get a query message, possibly setting the secondaryOk bit."""
338+
if set_secondary_ok:
339+
# Set the secondaryOk bit.
340340
flags = self.flags | 4
341341
else:
342342
flags = self.flags
@@ -349,7 +349,7 @@ def get_message(self, set_slave_ok, sock_info, use_cmd=False):
349349
if sock_info.op_msg_enabled:
350350
request_id, msg, size, _ = _op_msg(
351351
0, spec, self.db, self.read_preference,
352-
set_slave_ok, False, self.codec_options,
352+
set_secondary_ok, False, self.codec_options,
353353
ctx=sock_info.compression_context)
354354
return request_id, msg, size
355355
ns = _UJOIN % (self.db, "$cmd")
@@ -717,13 +717,13 @@ def _op_msg_uncompressed(flags, command, identifier, docs, check_keys, opts):
717717
_op_msg_uncompressed = _cmessage._op_msg
718718

719719

720-
def _op_msg(flags, command, dbname, read_preference, slave_ok, check_keys,
720+
def _op_msg(flags, command, dbname, read_preference, secondary_ok, check_keys,
721721
opts, ctx=None):
722722
"""Get a OP_MSG message."""
723723
command['$db'] = dbname
724724
# getMore commands do not send $readPreference.
725725
if read_preference is not None and "$readPreference" not in command:
726-
if slave_ok and not read_preference.mode:
726+
if secondary_ok and not read_preference.mode:
727727
command["$readPreference"] = (
728728
ReadPreference.PRIMARY_PREFERRED.document)
729729
else:
@@ -1717,7 +1717,7 @@ def unpack(cls, msg):
17171717

17181718

17191719
def _first_batch(sock_info, db, coll, query, ntoreturn,
1720-
slave_ok, codec_options, read_preference, cmd, listeners):
1720+
secondary_ok, codec_options, read_preference, cmd, listeners):
17211721
"""Simple query helper for retrieving a first (and possibly only) batch."""
17221722
query = _Query(
17231723
0, db, coll, 0, query, None, codec_options,
@@ -1729,7 +1729,7 @@ def _first_batch(sock_info, db, coll, query, ntoreturn,
17291729
if publish:
17301730
start = datetime.datetime.now()
17311731

1732-
request_id, msg, max_doc_size = query.get_message(slave_ok, sock_info)
1732+
request_id, msg, max_doc_size = query.get_message(secondary_ok, sock_info)
17331733

17341734
if publish:
17351735
encoding_duration = datetime.datetime.now() - start

0 commit comments

Comments
 (0)