-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fb36404
PYTHON-3459 Add log messages to Server selection spec
NoahStapp e66f424
Add more tests
NoahStapp a3883e4
Typecheck fixes for tests
NoahStapp 733c480
_Operations -> _Op
NoahStapp a99bce6
Make operation name always required when selecting a server
NoahStapp ea46c4e
Test operation fixes
NoahStapp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,7 @@ | |
UpdateOne, | ||
_IndexKeyHint, | ||
_IndexList, | ||
_Operations, | ||
) | ||
from pymongo.read_preferences import ReadPreference, _ServerMode | ||
from pymongo.results import ( | ||
|
@@ -257,8 +258,10 @@ def _conn_for_reads( | |
) -> ContextManager[tuple[Connection, _ServerMode]]: | ||
return self.__database.client._conn_for_reads(self._read_preference_for(session), session) | ||
|
||
def _conn_for_writes(self, session: Optional[ClientSession]) -> ContextManager[Connection]: | ||
return self.__database.client._conn_for_writes(session) | ||
def _conn_for_writes( | ||
self, session: Optional[ClientSession], operation: Optional[str] = None | ||
) -> ContextManager[Connection]: | ||
return self.__database.client._conn_for_writes(session, operation=operation) | ||
|
||
def _command( | ||
self, | ||
|
@@ -336,7 +339,7 @@ def __create( | |
if "size" in options: | ||
options["size"] = float(options["size"]) | ||
cmd.update(options) | ||
with self._conn_for_writes(session) as conn: | ||
with self._conn_for_writes(session, operation=_Operations.CREATE_OP) as conn: | ||
if qev2_required and conn.max_wire_version < 21: | ||
raise ConfigurationError( | ||
"Driver support of Queryable Encryption is incompatible with server. " | ||
|
@@ -558,7 +561,7 @@ def bulk_write( | |
raise TypeError(f"{request!r} is not a valid request") from None | ||
|
||
write_concern = self._write_concern_for(session) | ||
bulk_api_result = blk.execute(write_concern, session) | ||
bulk_api_result = blk.execute(write_concern, session, _Operations.INSERT_OP) | ||
if bulk_api_result is not None: | ||
return BulkWriteResult(bulk_api_result, True) | ||
return BulkWriteResult({}, False) | ||
|
@@ -598,7 +601,9 @@ def _insert_command( | |
|
||
_check_write_command_response(result) | ||
|
||
self.__database.client._retryable_write(acknowledged, _insert_command, session) | ||
self.__database.client._retryable_write( | ||
acknowledged, _insert_command, session, operation=_Operations.INSERT_OP | ||
) | ||
|
||
if not isinstance(doc, RawBSONDocument): | ||
return doc.get("_id") | ||
|
@@ -844,6 +849,7 @@ def _update_retryable( | |
session: Optional[ClientSession] = None, | ||
let: Optional[Mapping[str, Any]] = None, | ||
comment: Optional[Any] = None, | ||
operation: Optional[str] = _Operations.UPDATE_OP, | ||
) -> Optional[Mapping[str, Any]]: | ||
"""Internal update / replace helper.""" | ||
|
||
|
@@ -870,7 +876,10 @@ def _update( | |
) | ||
|
||
return self.__database.client._retryable_write( | ||
(write_concern or self.write_concern).acknowledged and not multi, _update, session | ||
(write_concern or self.write_concern).acknowledged and not multi, | ||
_update, | ||
session, | ||
operation=operation, | ||
) | ||
|
||
def replace_one( | ||
|
@@ -970,6 +979,7 @@ def replace_one( | |
session=session, | ||
let=let, | ||
comment=comment, | ||
operation="replace", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this spec require this to be "replace" or should it instead be "update" (the command name). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be |
||
), | ||
write_concern.acknowledged, | ||
) | ||
|
@@ -1082,6 +1092,7 @@ def update_one( | |
session=session, | ||
let=let, | ||
comment=comment, | ||
operation=_Operations.UPDATE_OP, | ||
), | ||
write_concern.acknowledged, | ||
) | ||
|
@@ -1182,6 +1193,7 @@ def update_many( | |
session=session, | ||
let=let, | ||
comment=comment, | ||
operation=_Operations.UPDATE_OP, | ||
), | ||
write_concern.acknowledged, | ||
) | ||
|
@@ -1319,7 +1331,10 @@ def _delete( | |
) | ||
|
||
return self.__database.client._retryable_write( | ||
(write_concern or self.write_concern).acknowledged and not multi, _delete, session | ||
(write_concern or self.write_concern).acknowledged and not multi, | ||
_delete, | ||
session, | ||
operation=_Operations.DELETE_OP, | ||
) | ||
|
||
def delete_one( | ||
|
@@ -1798,7 +1813,7 @@ def _cmd( | |
cmd.update(kwargs) | ||
return self._count_cmd(session, conn, read_preference, cmd, collation=None) | ||
|
||
return self._retryable_non_cursor_read(_cmd, None) | ||
return self._retryable_non_cursor_read(_cmd, None, operation=_Operations.COUNT_OP) | ||
|
||
def count_documents( | ||
self, | ||
|
@@ -1887,17 +1902,20 @@ def _cmd( | |
return 0 | ||
return result["n"] | ||
|
||
return self._retryable_non_cursor_read(_cmd, session) | ||
return self._retryable_non_cursor_read(_cmd, session, _Operations.COUNT_OP) | ||
|
||
def _retryable_non_cursor_read( | ||
self, | ||
func: Callable[[Optional[ClientSession], Server, Connection, Optional[_ServerMode]], T], | ||
session: Optional[ClientSession], | ||
operation: Optional[str] = "TEST_OPERATION", | ||
) -> 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) | ||
return client._retryable_read( | ||
func, self._read_preference_for(s), s, operation=operation | ||
) | ||
|
||
def create_indexes( | ||
self, | ||
|
@@ -1960,7 +1978,7 @@ def __create_indexes( | |
command (like maxTimeMS) can be passed as keyword arguments. | ||
""" | ||
names = [] | ||
with self._conn_for_writes(session) as conn: | ||
with self._conn_for_writes(session, operation=_Operations.CREATE_INDEXES_OP) as conn: | ||
supports_quorum = conn.max_wire_version >= 9 | ||
|
||
def gen_indexes() -> Iterator[Mapping[str, Any]]: | ||
|
@@ -2200,7 +2218,7 @@ def drop_index( | |
cmd.update(kwargs) | ||
if comment is not None: | ||
cmd["comment"] = comment | ||
with self._conn_for_writes(session) as conn: | ||
with self._conn_for_writes(session, operation=_Operations.DROP_INDEXES_OP) as conn: | ||
self._command( | ||
conn, | ||
cmd, | ||
|
@@ -2277,7 +2295,9 @@ def _cmd( | |
return cmd_cursor | ||
|
||
with self.__database.client._tmp_session(session, False) as s: | ||
return self.__database.client._retryable_read(_cmd, read_pref, s) | ||
return self.__database.client._retryable_read( | ||
_cmd, read_pref, s, operation=_Operations.LIST_INDEXES_OP | ||
) | ||
|
||
def index_information( | ||
self, | ||
|
@@ -2367,6 +2387,7 @@ def list_search_indexes( | |
cmd.get_read_preference(session), # type: ignore[arg-type] | ||
session, | ||
retryable=not cmd._performs_write, | ||
operation=_Operations.LIST_SEARCH_INDEX_OP, | ||
) | ||
|
||
def create_search_index( | ||
|
@@ -2435,7 +2456,7 @@ def gen_indexes() -> Iterator[Mapping[str, Any]]: | |
cmd = {"createSearchIndexes": self.name, "indexes": list(gen_indexes())} | ||
cmd.update(kwargs) | ||
|
||
with self._conn_for_writes(session) as conn: | ||
with self._conn_for_writes(session, operation=_Operations.CREATE_SEARCH_INDEXES_OP) as conn: | ||
resp = self._command( | ||
conn, | ||
cmd, | ||
|
@@ -2469,7 +2490,7 @@ def drop_search_index( | |
cmd.update(kwargs) | ||
if comment is not None: | ||
cmd["comment"] = comment | ||
with self._conn_for_writes(session) as conn: | ||
with self._conn_for_writes(session, operation=_Operations.DROP_SEARCH_INDEXES_OP) as conn: | ||
self._command( | ||
conn, | ||
cmd, | ||
|
@@ -2505,7 +2526,7 @@ def update_search_index( | |
cmd.update(kwargs) | ||
if comment is not None: | ||
cmd["comment"] = comment | ||
with self._conn_for_writes(session) as conn: | ||
with self._conn_for_writes(session, operation=_Operations.UPDATE_SEARCH_INDEX_OP) as conn: | ||
self._command( | ||
conn, | ||
cmd, | ||
|
@@ -2589,6 +2610,7 @@ def _aggregate( | |
cmd.get_read_preference(session), # type: ignore[arg-type] | ||
session, | ||
retryable=not cmd._performs_write, | ||
operation=_Operations.AGGREGATE_OP, | ||
) | ||
|
||
def aggregate( | ||
|
@@ -2925,7 +2947,7 @@ def rename( | |
cmd["comment"] = comment | ||
write_concern = self._write_concern_for_cmd(cmd, session) | ||
|
||
with self._conn_for_writes(session) as conn: | ||
with self._conn_for_writes(session, operation=_Operations.RENAME_OP) as conn: | ||
with self.__database.client._tmp_session(session) as s: | ||
return conn.command( | ||
"admin", | ||
|
@@ -3006,7 +3028,7 @@ def _cmd( | |
user_fields={"values": 1}, | ||
)["values"] | ||
|
||
return self._retryable_non_cursor_read(_cmd, session) | ||
return self._retryable_non_cursor_read(_cmd, session, operation=_Operations.DISTINCT_OP) | ||
|
||
def _write_concern_for_cmd( | ||
self, cmd: Mapping[str, Any], session: Optional[ClientSession] | ||
|
@@ -3090,7 +3112,10 @@ def _find_and_modify( | |
return out.get("value") | ||
|
||
return self.__database.client._retryable_write( | ||
write_concern.acknowledged, _find_and_modify, session | ||
write_concern.acknowledged, | ||
_find_and_modify, | ||
session, | ||
operation=_Operations.FIND_AND_MODIFY_OP, | ||
) | ||
|
||
def find_one_and_delete( | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just have this be
_Op.TEST
?Additionally, for typing, is there a case where we would pass
operation=None
because if not, we could also type this to just be of typestr
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found a case reading through, so ignore the second part :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Agree we should remove "TEST_OPERATION" and make this a required arg.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made
operation
required everywhere it appears for consistency with the spec and for ease of future refactors/development.