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

Queries which specify sort and batch size can generate results out of order, if documents concurrently updated

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.0.9, 3.1.9
    • Affects Version/s: None
    • Component/s: Querying
    • Fully Compatible
    • ALL
    • Quint 9 09/18/15

      The KeepMutationsNode query solution node is added to the incorrect place in the query solution tree for "split limited sort" plans (which are generated when a sort and a positive ntoreturn are specified). As a consequence, if the documents to be returned are concurrently updated, the KeepMutationsStage can inject these documents into the result stream out of order.

      Affects versions released since 2.5.5.

      Reproduce with the following:

      db.foo.drop();
      for (var i = 0; i < 6; ++i) {
          db.foo.insert({a: 1, b: i, c: 1});
      }
      assert.commandWorked(db.adminCommand({setParameter: 1, internalQueryExecYieldIterations: 1}));
      db.foo.ensureIndex({a: 1});
      startParallelShell("for (;;) { db.foo.update({}, {$inc: {c: 1}}, {multi: true}); }");
      for (;;) {
          var results = db.foo.find({a: 1}).sort({b: 1}).batchSize(2).toArray();
          var resultsSorted = results.concat().sort(function(lhs, rhs) { return lhs.b - rhs.b; });
          assert.eq(results, resultsSorted);
      }
      

      Output generated:

      assert: [[
      	{
      		"_id" : ObjectId("55d2789ecce4f5bfbe04948f"),
      		"a" : 1,
      		"b" : 0,
      		"c" : 3
      	},
      	{
      		"_id" : ObjectId("55d2789ecce4f5bfbe049490"),
      		"a" : 1,
      		"b" : 1,
      		"c" : 3
      	},
      	{
      		"_id" : ObjectId("55d2789ecce4f5bfbe04948f"),
      		"a" : 1,
      		"b" : 0,
      		"c" : 3
      	},
      	{
      		"_id" : ObjectId("55d2789ecce4f5bfbe049490"),
      		"a" : 1,
      		"b" : 1,
      		"c" : 3
      	},
      	{
      		"_id" : ObjectId("55d2789ecce4f5bfbe049491"),
      		"a" : 1,
      		"b" : 2,
      		"c" : 3
      	},
      	{
      		"_id" : ObjectId("55d2789ecce4f5bfbe049492"),
      		"a" : 1,
      		"b" : 3,
      		"c" : 3
      	},
      	{
      		"_id" : ObjectId("55d2789ecce4f5bfbe049493"),
      		"a" : 1,
      		"b" : 4,
      		"c" : 3
      	},
      	{
      		"_id" : ObjectId("55d2789ecce4f5bfbe049494"),
      		"a" : 1,
      		"b" : 5,
      		"c" : 3
      	}
      ]] != [[
      	{
      		"_id" : ObjectId("55d2789ecce4f5bfbe04948f"),
      		"a" : 1,
      		"b" : 0,
      		"c" : 3
      	},
      	{
      		"_id" : ObjectId("55d2789ecce4f5bfbe04948f"),
      		"a" : 1,
      		"b" : 0,
      		"c" : 3
      	},
      	{
      		"_id" : ObjectId("55d2789ecce4f5bfbe049490"),
      		"a" : 1,
      		"b" : 1,
      		"c" : 3
      	},
      	{
      		"_id" : ObjectId("55d2789ecce4f5bfbe049490"),
      		"a" : 1,
      		"b" : 1,
      		"c" : 3
      	},
      	{
      		"_id" : ObjectId("55d2789ecce4f5bfbe049491"),
      		"a" : 1,
      		"b" : 2,
      		"c" : 3
      	},
      	{
      		"_id" : ObjectId("55d2789ecce4f5bfbe049492"),
      		"a" : 1,
      		"b" : 3,
      		"c" : 3
      	},
      	{
      		"_id" : ObjectId("55d2789ecce4f5bfbe049493"),
      		"a" : 1,
      		"b" : 4,
      		"c" : 3
      	},
      	{
      		"_id" : ObjectId("55d2789ecce4f5bfbe049494"),
      		"a" : 1,
      		"b" : 5,
      		"c" : 3
      	}
      ]] are not equal : undefined
      

            Assignee:
            david.storch@mongodb.com David Storch
            Reporter:
            rassi J Rassi
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: