PYTHON-4557 - Fix write log messages for retried commands (#2260)

This commit is contained in:
Noah Stapp 2025-04-03 15:33:11 -04:00 committed by GitHub
parent b40223938c
commit e7c0814512
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 60 additions and 6 deletions

View File

@ -2840,7 +2840,7 @@ class _ClientConnectionRetryable(Generic[T]):
_debug_log(
_COMMAND_LOGGER,
message=f"Retrying write attempt number {self._attempt_number}",
clientId=self._client.client_id,
clientId=self._client._topology_settings._topology_id,
commandName=self._operation,
operationId=self._operation_id,
)

View File

@ -2826,7 +2826,7 @@ class _ClientConnectionRetryable(Generic[T]):
_debug_log(
_COMMAND_LOGGER,
message=f"Retrying write attempt number {self._attempt_number}",
clientId=self._client.client_id,
clientId=self._client._topology_settings._topology_id,
commandName=self._operation,
operationId=self._operation_id,
)

View File

@ -102,7 +102,14 @@ class TestLogger(AsyncIntegrationTest):
await self.db.test.insert_one({"x": "1"})
async with self.fail_point(
{"mode": {"times": 1}, "data": {"failCommands": ["find"], "closeConnection": True}}
{
"mode": {"times": 1},
"data": {
"failCommands": ["find"],
"errorCode": 10107,
"errorLabels": ["RetryableWriteError"],
},
}
):
with self.assertLogs("pymongo.command", level="DEBUG") as cm:
await self.db.test.find_one({"x": "1"})
@ -110,7 +117,27 @@ class TestLogger(AsyncIntegrationTest):
retry_messages = [
r.getMessage() for r in cm.records if "Retrying read attempt" in r.getMessage()
]
print(retry_messages)
self.assertEqual(len(retry_messages), 1)
@async_client_context.require_failCommand_fail_point
@async_client_context.require_retryable_writes
async def test_logging_retry_write_attempts(self):
async with self.fail_point(
{
"mode": {"times": 1},
"data": {
"errorCode": 10107,
"errorLabels": ["RetryableWriteError"],
"failCommands": ["insert"],
},
}
):
with self.assertLogs("pymongo.command", level="DEBUG") as cm:
await self.db.test.insert_one({"x": "1"})
retry_messages = [
r.getMessage() for r in cm.records if "Retrying write attempt" in r.getMessage()
]
self.assertEqual(len(retry_messages), 1)

View File

@ -101,7 +101,14 @@ class TestLogger(IntegrationTest):
self.db.test.insert_one({"x": "1"})
with self.fail_point(
{"mode": {"times": 1}, "data": {"failCommands": ["find"], "closeConnection": True}}
{
"mode": {"times": 1},
"data": {
"failCommands": ["find"],
"errorCode": 10107,
"errorLabels": ["RetryableWriteError"],
},
}
):
with self.assertLogs("pymongo.command", level="DEBUG") as cm:
self.db.test.find_one({"x": "1"})
@ -109,7 +116,27 @@ class TestLogger(IntegrationTest):
retry_messages = [
r.getMessage() for r in cm.records if "Retrying read attempt" in r.getMessage()
]
print(retry_messages)
self.assertEqual(len(retry_messages), 1)
@client_context.require_failCommand_fail_point
@client_context.require_retryable_writes
def test_logging_retry_write_attempts(self):
with self.fail_point(
{
"mode": {"times": 1},
"data": {
"errorCode": 10107,
"errorLabels": ["RetryableWriteError"],
"failCommands": ["insert"],
},
}
):
with self.assertLogs("pymongo.command", level="DEBUG") as cm:
self.db.test.insert_one({"x": "1"})
retry_messages = [
r.getMessage() for r in cm.records if "Retrying write attempt" in r.getMessage()
]
self.assertEqual(len(retry_messages), 1)