getShardIdsForRange [MinKey, MaxKey] fast path missing early return, re-scans all chunks

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: 7.0.0, 8.0.0, 8.3.0
    • Component/s: None
    • None
    • Catalog and Routing
    • ALL
    • 1
    • 🟩 Routing and Topology
    • None
    • None
    • None
    • None
    • None
    • None

      Description

      The [MinKey, MaxKey] fast path in ChunkManager::getShardIdsForRange() is missing its return;, so it populates the output and then falls through into forEachOverlappingChunk(), which re-scans every chunk in the collection. The optimization is fully defeated. The comment ("return all shard ids right away") confirms
      the return was intended.

      chunk_manager.cpp#L844-L850

            if (!_clusterTime && ChunkMap::allElementsAreOfType(BSONType::minKey, min) &&
                ChunkMap::allElementsAreOfType(BSONType::maxKey, max)) {
                _rt->optRt->getAllShardIds(shardIds);
                if (chunkRanges) {
                    getAllChunkRanges(chunkRanges);
                }
            }   // <-- missing `return;` (line 850)
        

      Impact

      Performance only; results stay correct. Turns ~O(#shards) into an O(#chunks) full scan on a common query shape (whole-collection targeting).

      Fix

      Add return; at line 850.

            Assignee:
            Unassigned
            Reporter:
            Sergi Mateo Bellido
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: