Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-27207

Find on view with sort through mongos may incorrectly return empty result set

    Details

    • Backwards Compatibility:
      Fully Compatible
    • Operating System:
      ALL
    • Backport Completed:
    • Sprint:
      Query 2016-12-12

      Description

      A find() with a sort() on a mongos may return an empty result set:

      mongos> db.c.insert({x:1})
      WriteResult({ "nInserted" : 1 })
      mongos> db.c.find()
      { "_id" : ObjectId("583dc54dade9d2265ce2b196"), "x" : 1 }
      mongos> db.createView("cView", "c", [{$match : { x : {$gt : 0}}}])
      { "ok" : 1 }
      mongos> db.cView.find()
      { "_id" : ObjectId("583dc54dade9d2265ce2b196"), "x" : 1 }
      mongos> db.cView.find().sort({_id:1})
      

      There's a simple workaround, which is to use the aggregation pipeline:

      mongos> db.cView.aggregate([{ $sort: {_id:1}}])
      { "_id" : ObjectId("583dc54dade9d2265ce2b196"), "x" : 1 }
      

        Issue Links

          Activity

          Hide
          kyle.suarez Kyle Suarez added a comment -

          In AsyncResultsMerger::handleBatchResponse(), we detect the view kickback error code from the primary shard and fill out a ClusterQueryResult with the new resolved view. However, we only fill out the docBuffer of the remote. We don't touch the merge queue in any way, so it's left empty. If the query has a sort, we call AsyncResultsMerger::nextReadySorted(), which consults the top of the merge queue. Since it's empty, it returns an empty result.

          There are a few ways we could fix this. Off the top of my head, we could also push remoteIndex to _mergeQueue when populating the result's view definition; or we could change the code to not go down the sorted codepath if a view's been discovered. I leave the strategy up to David Storch.

          Show
          kyle.suarez Kyle Suarez added a comment - In AsyncResultsMerger::handleBatchResponse() , we detect the view kickback error code from the primary shard and fill out a ClusterQueryResult with the new resolved view. However, we only fill out the docBuffer of the remote . We don't touch the merge queue in any way, so it's left empty. If the query has a sort, we call AsyncResultsMerger::nextReadySorted() , which consults the top of the merge queue. Since it's empty, it returns an empty result. There are a few ways we could fix this. Off the top of my head, we could also push remoteIndex to _mergeQueue when populating the result's view definition; or we could change the code to not go down the sorted codepath if a view's been discovered. I leave the strategy up to David Storch .
          Hide
          xgen-internal-githook Githook User added a comment -

          Author:

          {u'username': u'ksuarz', u'name': u'Kyle Suarez', u'email': u'kyle.suarez@mongodb.com'}

          Message: SERVER-27207 populate merge queue in AsyncResultsMerger when encountering ResolvedView
          Branch: master
          https://github.com/mongodb/mongo/commit/1db6b26bcfdc67992c52739f50deacd31edc4fc0

          Show
          xgen-internal-githook Githook User added a comment - Author: {u'username': u'ksuarz', u'name': u'Kyle Suarez', u'email': u'kyle.suarez@mongodb.com'} Message: SERVER-27207 populate merge queue in AsyncResultsMerger when encountering ResolvedView Branch: master https://github.com/mongodb/mongo/commit/1db6b26bcfdc67992c52739f50deacd31edc4fc0
          Hide
          xgen-internal-githook Githook User added a comment -

          Author:

          {u'username': u'ksuarz', u'name': u'Kyle Suarez', u'email': u'kyle.suarez@mongodb.com'}

          Message: SERVER-27207 populate merge queue in AsyncResultsMerger when encountering ResolvedView

          (cherry picked from commit 1db6b26bcfdc67992c52739f50deacd31edc4fc0)
          Branch: v3.4
          https://github.com/mongodb/mongo/commit/9e941332d0c15cf498298de32d843bd6f409ddf7

          Show
          xgen-internal-githook Githook User added a comment - Author: {u'username': u'ksuarz', u'name': u'Kyle Suarez', u'email': u'kyle.suarez@mongodb.com'} Message: SERVER-27207 populate merge queue in AsyncResultsMerger when encountering ResolvedView (cherry picked from commit 1db6b26bcfdc67992c52739f50deacd31edc4fc0) Branch: v3.4 https://github.com/mongodb/mongo/commit/9e941332d0c15cf498298de32d843bd6f409ddf7

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              14 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                  Agile