Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 12 additions & 11 deletions pymongo/asynchronous/mongo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2048,17 +2048,18 @@ async def _retryable_read(
retryable = bool(
retryable and self.options.retry_reads and not (session and session.in_transaction)
)
return await self._retry_internal(
func,
session,
None,
operation,
is_read=True,
address=address,
read_pref=read_pref,
retryable=retryable,
operation_id=operation_id,
)
async with self._tmp_session(session) as s:
return await self._retry_internal(
func,
s,
None,
operation,
is_read=True,
address=address,
read_pref=read_pref,
retryable=retryable,
operation_id=operation_id,
)

async def _retryable_write(
self,
Expand Down
23 changes: 12 additions & 11 deletions pymongo/synchronous/mongo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2044,17 +2044,18 @@ def _retryable_read(
retryable = bool(
retryable and self.options.retry_reads and not (session and session.in_transaction)
)
return self._retry_internal(
func,
session,
None,
operation,
is_read=True,
address=address,
read_pref=read_pref,
retryable=retryable,
operation_id=operation_id,
)
with self._tmp_session(session) as s:
return self._retry_internal(
func,
s,
None,
operation,
is_read=True,
address=address,
read_pref=read_pref,
retryable=retryable,
operation_id=operation_id,
)

def _retryable_write(
self,
Expand Down
32 changes: 32 additions & 0 deletions test/asynchronous/test_retryable_reads.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,38 @@ async def test_retryable_reads_are_retried_on_the_same_mongos_when_no_others_are
# Assert that both events occurred on the same mongos.
assert listener.succeeded_events[0].connection_id == listener.failed_events[0].connection_id

@async_client_context.require_failCommand_fail_point
async def test_retryable_reads_are_retried_on_the_same_implicit_session(self):
fail_command = {
"configureFailPoint": "failCommand",
"mode": {"times": 1},
"data": {"failCommands": ["count"], "errorCode": 6},
}

listener = OvertCommandListener()
client = await self.async_rs_or_single_client(
directConnection=False,
event_listeners=[listener],
retryReads=True,
)

await async_set_fail_point(client, fail_command)

await client.t.t.estimated_document_count()

# Disable failpoint.
fail_command["mode"] = "off"
await async_set_fail_point(client, fail_command)

# Assert that both events occurred on the same session.
lsids = [
event.command["lsid"]
for event in listener.started_events
if event.command_name == "count"
]
self.assertEqual(len(lsids), 2)
self.assertEqual(lsids[0], lsids[1])


if __name__ == "__main__":
unittest.main()
32 changes: 32 additions & 0 deletions test/test_retryable_reads.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,38 @@ def test_retryable_reads_are_retried_on_the_same_mongos_when_no_others_are_avail
# Assert that both events occurred on the same mongos.
assert listener.succeeded_events[0].connection_id == listener.failed_events[0].connection_id

@client_context.require_failCommand_fail_point
def test_retryable_reads_are_retried_on_the_same_implicit_session(self):
fail_command = {
"configureFailPoint": "failCommand",
"mode": {"times": 1},
"data": {"failCommands": ["count"], "errorCode": 6},
}

listener = OvertCommandListener()
client = self.rs_or_single_client(
directConnection=False,
event_listeners=[listener],
retryReads=True,
)

set_fail_point(client, fail_command)
Copy link
Member

Choose a reason for hiding this comment

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

Let's use self.fail_point() here.


client.t.t.estimated_document_count()
Copy link
Member

Choose a reason for hiding this comment

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

Can we extend this test to cover a few other operations as well?


# Disable failpoint.
fail_command["mode"] = "off"
set_fail_point(client, fail_command)

# Assert that both events occurred on the same session.
lsids = [
event.command["lsid"]
for event in listener.started_events
if event.command_name == "count"
]
self.assertEqual(len(lsids), 2)
self.assertEqual(lsids[0], lsids[1])
Copy link
Member

Choose a reason for hiding this comment

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

Should we fix PYTHON-2391 first? Otherwise this test doesn't prove the fix works correctly since first.command is the same dict instance as second.command.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we should merge the PR for PYTHON-2391 (#2545) first to verify this works correctly.



if __name__ == "__main__":
unittest.main()
Loading