Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PYTHON-3459 Add log messages to Server selection spec #1511

Merged
merged 6 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pymongo/bulk.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def execute_command(
generator: Iterator[Any],
write_concern: WriteConcern,
session: Optional[ClientSession],
operation: Optional[str] = "TEST_OPERATION",
operation: str,
) -> dict[str, Any]:
"""Execute using write commands."""
# nModified is only reported for write commands, not legacy ops.
Expand Down Expand Up @@ -442,8 +442,8 @@ def retryable_bulk(
self.is_retryable,
retryable_bulk,
session,
operation,
bulk=self,
operation=operation,
operation_id=op_id,
)

Expand Down Expand Up @@ -559,7 +559,7 @@ def execute(
self,
write_concern: WriteConcern,
session: Optional[ClientSession],
operation: Optional[str] = "TEST_OPERATION",
operation: str,
) -> Any:
"""Execute operations."""
if not self.ops:
Expand All @@ -577,7 +577,7 @@ def execute(

client = self.collection.database.client
if not write_concern.acknowledged:
with client._conn_for_writes(session) as connection:
with client._conn_for_writes(session, operation) as connection:
self.execute_no_results(connection, generator, write_concern)
return None
else:
Expand Down
27 changes: 10 additions & 17 deletions pymongo/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,10 @@ def __init__(
else:
self.__create(name, kwargs, collation, session)

def _conn_for_reads(
self, session: ClientSession
) -> ContextManager[tuple[Connection, _ServerMode]]:
return self.__database.client._conn_for_reads(self._read_preference_for(session), session)

Jibola marked this conversation as resolved.
Show resolved Hide resolved
def _conn_for_writes(
self, session: Optional[ClientSession], operation: Optional[str] = None
self, session: Optional[ClientSession], operation: str
) -> ContextManager[Connection]:
return self.__database.client._conn_for_writes(session, operation=operation)
return self.__database.client._conn_for_writes(session, operation)

def _command(
self,
Expand Down Expand Up @@ -745,7 +740,7 @@ def gen() -> Iterator[tuple[int, Mapping[str, Any]]]:
write_concern = self._write_concern_for(session)
blk = _Bulk(self, ordered, bypass_document_validation, comment=comment)
blk.ops = list(gen())
blk.execute(write_concern, session=session)
blk.execute(write_concern, session, _Op.INSERT)
return InsertManyResult(inserted_ids, write_concern.acknowledged)

def _update(
Expand Down Expand Up @@ -837,6 +832,7 @@ def _update_retryable(
self,
criteria: Mapping[str, Any],
document: Union[Mapping[str, Any], _Pipeline],
operation: str,
upsert: bool = False,
multi: bool = False,
write_concern: Optional[WriteConcern] = None,
Expand All @@ -849,7 +845,6 @@ def _update_retryable(
session: Optional[ClientSession] = None,
let: Optional[Mapping[str, Any]] = None,
comment: Optional[Any] = None,
operation: Optional[str] = _Op.UPDATE,
) -> Optional[Mapping[str, Any]]:
"""Internal update / replace helper."""

Expand Down Expand Up @@ -879,7 +874,7 @@ def _update(
(write_concern or self.write_concern).acknowledged and not multi,
_update,
session,
operation=operation,
operation,
)

def replace_one(
Expand Down Expand Up @@ -971,6 +966,7 @@ def replace_one(
self._update_retryable(
filter,
replacement,
_Op.UPDATE,
upsert,
write_concern=write_concern,
bypass_doc_val=bypass_document_validation,
Expand All @@ -979,7 +975,6 @@ def replace_one(
session=session,
let=let,
comment=comment,
operation=_Op.UPDATE,
),
write_concern.acknowledged,
)
Expand Down Expand Up @@ -1083,6 +1078,7 @@ def update_one(
self._update_retryable(
filter,
update,
_Op.UPDATE,
upsert,
write_concern=write_concern,
bypass_doc_val=bypass_document_validation,
Expand All @@ -1092,7 +1088,6 @@ def update_one(
session=session,
let=let,
comment=comment,
operation=_Op.UPDATE,
),
write_concern.acknowledged,
)
Expand Down Expand Up @@ -1183,6 +1178,7 @@ def update_many(
self._update_retryable(
filter,
update,
_Op.UPDATE,
upsert,
multi=True,
write_concern=write_concern,
Expand All @@ -1193,7 +1189,6 @@ def update_many(
session=session,
let=let,
comment=comment,
operation=_Op.UPDATE,
),
write_concern.acknowledged,
)
Expand Down Expand Up @@ -1908,14 +1903,12 @@ def _retryable_non_cursor_read(
self,
func: Callable[[Optional[ClientSession], Server, Connection, Optional[_ServerMode]], T],
session: Optional[ClientSession],
operation: Optional[str] = "TEST_OPERATION",
operation: str,
) -> T:
"""Non-cursor read helper to handle implicit session creation."""
client = self.__database.client
with client._tmp_session(session) as s:
return client._retryable_read(
func, self._read_preference_for(s), s, operation=operation
)
return client._retryable_read(func, self._read_preference_for(s), s, operation)

def create_indexes(
self,
Expand Down
16 changes: 9 additions & 7 deletions pymongo/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,6 @@ def command(
codec_options: None = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
operation: Optional[str] = None,
**kwargs: Any,
) -> dict[str, Any]:
...
Expand All @@ -774,7 +773,6 @@ def command(
codec_options: CodecOptions[_CodecDocumentType] = ...,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
operation: Optional[str] = None,
**kwargs: Any,
) -> _CodecDocumentType:
...
Expand All @@ -790,7 +788,6 @@ def command(
codec_options: Optional[bson.codec_options.CodecOptions[_CodecDocumentType]] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
operation: Optional[str] = None,
**kwargs: Any,
) -> Union[dict[str, Any], _CodecDocumentType]:
"""Issue a MongoDB command.
Expand Down Expand Up @@ -886,7 +883,7 @@ def command(
if comment is not None:
kwargs["comment"] = comment

if operation is None and isinstance(command, str):
if isinstance(command, str):
command_name = command
else:
command_name = next(iter(command))
Expand Down Expand Up @@ -969,14 +966,19 @@ def cursor_command(

.. seealso:: The MongoDB documentation on `commands <https://dochub.mongodb.org/core/commands>`_.
"""
if isinstance(command, str):
command_name = command
else:
command_name = next(iter(command))

with self.__client._tmp_session(session, close=False) as tmp_session:
opts = codec_options or DEFAULT_CODEC_OPTIONS

if read_preference is None:
read_preference = (
tmp_session and tmp_session._txn_read_preference()
) or ReadPreference.PRIMARY
with self.__client._conn_for_reads(read_preference, tmp_session) as (
with self.__client._conn_for_reads(read_preference, tmp_session, command_name) as (
conn,
read_preference,
):
Expand Down Expand Up @@ -1010,8 +1012,8 @@ def cursor_command(
def _retryable_read_command(
self,
command: Union[str, MutableMapping[str, Any]],
operation: str,
session: Optional[ClientSession] = None,
operation: Optional[str] = None,
) -> dict[str, Any]:
"""Same as command but used for retryable read commands."""
read_preference = (session and session._txn_read_preference()) or ReadPreference.PRIMARY
Expand All @@ -1029,7 +1031,7 @@ def _cmd(
session=session,
)

return self.__client._retryable_read(_cmd, read_preference, session, operation=operation)
return self.__client._retryable_read(_cmd, read_preference, session, operation)

def _list_collections(
self,
Expand Down
Loading
Loading