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

Sharded mapReduce returns wrong result

    • Type: Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.2.4
    • Component/s: MapReduce, Sharding
    • None
    • Query
    • ALL
    • Hide
      var st = new ShardingTest({
              shards: {rs0: {nodes: 1}, rs1: {nodes: 1}},
              mongos: 1,
          });
      
      var dbName = "wc-test";
      var mongos = st.s;
      var db = mongos.getDB(dbName);
      var collName = 'leaves';
      var coll = db[collName];
      function createShards() {
          var numberDoc = 20;
          db.adminCommand({movePrimary: db.toString(), to: st._shardNames[0]});
          db.adminCommand({enablesharding: db.toString()});
          coll.ensureIndex({x: 1}, {unique: true});
          db.adminCommand({shardCollection: coll.toString(), key: {x: 1}});
          st.configRS.awaitLastOpCommitted();
          db.adminCommand({split: coll.getFullName(), middle: {x: numberDoc / 2}});
      
          db.adminCommand({
              moveChunk: coll.getFullName(),
              find: {x: numberDoc / 2},
              to: st._shardNames[1],
          });
      
          for (var i = 0; i < numberDoc; i++) {
              coll.insert({x: i});
          }
          assert.eq(coll.count(), numberDoc);
      }
      
      createShards();
      coll.insert({x: -3, tags: ["a", "b"]});
      coll.insert({x: -7, tags: ["b", "c"]});
      coll.insert({x: 23, tags: ["c", "a"]});
      coll.insert({x: 27, tags: ["b", "c"]});
      
      printjson(db.runCommand({
                  mapReduce: collName,
                  map: function() {
                      if(!this.tags) {
                          return;
                      }
                      this.tags.forEach(function(z) {
                          emit(z, 1);
                      });
                  },
                  reduce: function(key, values) {
                      return {
                          count: values.length
                      };
                  },
                  out: {inline: 1}
              }));
      
      Show
      var st = new ShardingTest({ shards: {rs0: {nodes: 1}, rs1: {nodes: 1}}, mongos: 1, }); var dbName = "wc-test"; var mongos = st.s; var db = mongos.getDB(dbName); var collName = 'leaves'; var coll = db[collName]; function createShards() { var numberDoc = 20; db.adminCommand({movePrimary: db.toString(), to: st._shardNames[0]}); db.adminCommand({enablesharding: db.toString()}); coll.ensureIndex({x: 1}, {unique: true}); db.adminCommand({shardCollection: coll.toString(), key: {x: 1}}); st.configRS.awaitLastOpCommitted(); db.adminCommand({split: coll.getFullName(), middle: {x: numberDoc / 2}}); db.adminCommand({ moveChunk: coll.getFullName(), find: {x: numberDoc / 2}, to: st._shardNames[1], }); for (var i = 0; i < numberDoc; i++) { coll.insert({x: i}); } assert.eq(coll.count(), numberDoc); } createShards(); coll.insert({x: -3, tags: ["a", "b"]}); coll.insert({x: -7, tags: ["b", "c"]}); coll.insert({x: 23, tags: ["c", "a"]}); coll.insert({x: 27, tags: ["b", "c"]}); printjson(db.runCommand({ mapReduce: collName, map: function() { if(!this.tags) { return; } this.tags.forEach(function(z) { emit(z, 1); }); }, reduce: function(key, values) { return { count: values.length }; }, out: {inline: 1} }));
    • None
    • 0
    • None
    • None
    • None
    • None
    • None
    • None

      I ran the following mapReduce query and the output was incorrect. I received the results shown below. If you remove the "createShards();" call in the repro, you'll see that you get the expected results. Similarly, if you change the x values in the tagged documents to all be on the same shard you also get the expected results.

      Received:

      	"results" : [
      		{
      			"_id" : "a",
      			"value" : {
      				"count" : 2
      			}
      		},
      		{
      			"_id" : "b",
      			"value" : {
      				"count" : 2
      			}
      		},
      		{
      			"_id" : "c",
      			"value" : {
      				"count" : 2
      			}
      		}
      	],
      

      Expected:

      	"results" : [
      		{
      			"_id" : "a",
      			"value" : {
      				"count" : 2
      			}
      		},
      		{
      			"_id" : "b",
      			"value" : {
      				"count" : 3
      			}
      		},
      		{
      			"_id" : "c",
      			"value" : {
      				"count" : 3
      			}
      		}
      	],
      

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            judah.schvimer@mongodb.com Judah Schvimer
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: