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

Conversation

NoahStapp
Copy link
Contributor

@NoahStapp NoahStapp commented Feb 6, 2024

For additional context, the spec for server selection logging requires that every time we select a server, we log the operation we plan to send to the server. Since we do not have a unified operations layer (see PYTHON-1833), this requires a large number of scattered changes to pass the operation name all the way down to server selection.

The spec tests are quite minimal, presumably because they assume drivers have a unified operations layer that makes logging operation names easy. I've added additional unified format tests for most of the operations we have separate handlers for. Depending on other driver implementations, these tests may be useful--I'll open a DRIVERS ticket to start the discussion for possible inclusion in the spec tests.

@NoahStapp NoahStapp requested a review from a team as a code owner February 6, 2024 22:55
@NoahStapp NoahStapp requested review from Jibola and removed request for a team February 6, 2024 22:55
@@ -44,6 +45,35 @@
_IndexKeyHint = Union[str, _IndexList]


class _Operations(str, enum.Enum):
ABORT_TRANSACTION_OP = "abortTransaction"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd optimzie for shorter names here. We can ditch the _OP suffix since these are all nested a named enum. We could also shorten _Operations to _Op so these become: _Op.ABORT

@@ -970,6 +979,7 @@ def replace_one(
session=session,
let=let,
comment=comment,
operation="replace",
Copy link
Member

Choose a reason for hiding this comment

The 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).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be update, good catch.

Comment on lines 1716 to 1719
server = topology.select_server_by_address(tuple(address), operation="killCursors") # type: ignore[arg-type]
else:
# Application called close_cursor() with no address.
server = topology.select_server(writable_server_selector)
server = topology.select_server(writable_server_selector, operation="killCursors")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should killCursors be in the Enum?

@NoahStapp NoahStapp requested a review from ShaneHarvey February 7, 2024 18:32
pymongo/bulk.py Outdated
@@ -408,6 +408,7 @@ def execute_command(
generator: Iterator[Any],
write_concern: WriteConcern,
session: Optional[ClientSession],
operation: Optional[str] = "TEST_OPERATION",
Copy link
Contributor

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 type str.

Copy link
Contributor

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 :)

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor

@Jibola Jibola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! Left a few comments

pymongo/bulk.py Outdated
@@ -408,6 +408,7 @@ def execute_command(
generator: Iterator[Any],
write_concern: WriteConcern,
session: Optional[ClientSession],
operation: Optional[str] = "TEST_OPERATION",
Copy link
Contributor

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 :)

@@ -786,6 +790,7 @@ def command(
codec_options: Optional[bson.codec_options.CodecOptions[_CodecDocumentType]] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
operation: Optional[str] = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll have to update the docstring here.

if read_preference is None:
read_preference = (session and session._txn_read_preference()) or ReadPreference.PRIMARY
with self.__client._conn_for_reads(read_preference, session) as (
with self.__client._conn_for_reads(read_preference, session, operation=command_name) as (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we pass command_name here instead of operation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When explicitly using database.command, we always want to use the command name in the command document. I've removed operation entirely here as it's unneeded.

test/unified_format.py Outdated Show resolved Hide resolved
@NoahStapp NoahStapp requested a review from Jibola February 7, 2024 20:07
Copy link
Contributor

@Jibola Jibola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great Job. I've got one question on a deleted method, but other than that, LGTM.

pymongo/collection.py Show resolved Hide resolved
@NoahStapp NoahStapp requested a review from Jibola February 8, 2024 17:10
@NoahStapp NoahStapp merged commit f8f98dd into mongodb:standardized-logging Feb 8, 2024
68 checks passed
@NoahStapp NoahStapp deleted the PYTHON-3459 branch February 8, 2024 20:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants