[SERVER-70592] Have clusterQueryWithoutShardKey apply the sort order if findAndModify supplies one Created: 14/Oct/22  Updated: 29/Oct/23  Resolved: 20/Mar/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 7.0.0-rc0

Type: Task Priority: Major - P3
Reporter: Jason Zhang Assignee: Jason Zhang
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Sprint: Sharding 2022-12-12, Sharding NYC 2023-01-09, Sharding NYC 2023-02-20, Sharding NYC 2023-03-06, Sharding NYC 2023-03-20, Sharding NYC 2023-04-03
Participants:

 Comments   
Comment by Githook User [ 20/Mar/23 ]

Author:

{'name': 'Jason Zhang', 'email': 'jason.zhang@mongodb.com', 'username': 'jz1242'}

Message: SERVER-70592 Have clusterQueryWithoutShardKey apply the sort order if findAndModify supplies one
Branch: master
https://github.com/mongodb/mongo/commit/35dd182c3e4029465a95000409f575cf781d20b9

Comment by Sanika Phanse (Inactive) [ 05/Jan/23 ]

Chatted with Nick on Query, came up with an approach:

Alternative approach:

  • Project on $sortKey like the ARM does, and directly call the comparator.
Comment by Sanika Phanse (Inactive) [ 04/Jan/23 ]

Investigated approach #2 today, the ARM seems pretty deeply tied with the implementation of cluster_find & cluster_aggregation. Doing a bit more final investigation to see if there's a way to isolate the ARM sorting layer, but I'll likely pursue option #1. There might be advantages to setting returning the $sortKey value from each shard along with the target document as it would allow doing a direct binary comparison between documents.

Comment by Sanika Phanse (Inactive) [ 04/Jan/23 ]

Currently, AsyncResultsMerger gathers and sorts results from open cursors in a multi-shard find command or aggregation.

In the current implementation of _clusterQueryWithoutShardKey, we collect a set of matching documents from an ARS and select one as the target doc.

Two options come to mind here:
1) Use the internal comparator in AsyncResultsMerger to compare the set of matching documents by sort key
2) Layer the AsycnResultsMerger on top of _clusterQueryWithoutShardKey's ARS with a merge cursors stage using this

Comment by Sanika Phanse (Inactive) [ 03/Jan/23 ]
{sort: true}

can only be set for findAndModify commands (update/delete do not support this field). If a collation is spec'd in this findAndModify command, use it during the final sorting as well.

Comment by Sanika Phanse (Inactive) [ 03/Jan/23 ]

High level approach:

In _clusterQueryWithoutShardKey,

Generated at Thu Feb 08 06:16:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.