summaryrefslogtreecommitdiff
path: root/jstests/sharding/exact_shard_key_target.js
diff options
context:
space:
mode:
authorGreg Studer <greg@10gen.com>2014-06-03 13:57:13 -0400
committerGreg Studer <greg@10gen.com>2014-06-05 17:47:21 -0400
commit777de742ee578b62b12ded8381aadf98dfa9fa5f (patch)
tree9a8c6a004b78476fca336d094e5945a5526664e8 /jstests/sharding/exact_shard_key_target.js
parentadcf6601f49b8afbe5b0c2b23d0f83ceeeca1fa1 (diff)
downloadmongo-777de742ee578b62b12ded8381aadf98dfa9fa5f.tar.gz
SERVER-14138 mongos incorrectly targets multiple shards for nested field shard keys
Diffstat (limited to 'jstests/sharding/exact_shard_key_target.js')
-rw-r--r--jstests/sharding/exact_shard_key_target.js74
1 files changed, 74 insertions, 0 deletions
diff --git a/jstests/sharding/exact_shard_key_target.js b/jstests/sharding/exact_shard_key_target.js
new file mode 100644
index 00000000000..bc416166866
--- /dev/null
+++ b/jstests/sharding/exact_shard_key_target.js
@@ -0,0 +1,74 @@
+//
+// Verifies that shard key targeted update/delete operations go to exactly one shard when targeted
+// by nested shard keys.
+// SERVER-14138
+//
+
+var st = new ShardingTest({ shards : 2, verbose : 4 });
+st.stopBalancer();
+
+var mongos = st.s0;
+var coll = mongos.getCollection("foo.bar");
+var admin = mongos.getDB("admin");
+var shards = mongos.getDB("config").shards.find().toArray();
+
+assert.commandWorked(admin.runCommand({ enableSharding : coll.getDB().getName() }));
+printjson(admin.runCommand({ movePrimary : coll.getDB().getName(), to : shards[0]._id }));
+assert.commandWorked(admin.runCommand({ shardCollection: coll.getFullName(),
+ key: { "a.b": 1 } }));
+assert.commandWorked(admin.runCommand({ split: coll.getFullName(), middle: { "a.b": 0 } }));
+assert.commandWorked(admin.runCommand({ moveChunk: coll.getFullName(),
+ find: { "a.b": 0 },
+ to: shards[1]._id }));
+
+st.printShardingStatus();
+
+//
+// JustOne remove
+coll.remove({});
+assert.writeOK(coll.insert({ _id : 1, a : { b : -1 } }));
+assert.writeOK(coll.insert({ _id : 2, a : { b : 1 } }));
+// Need orphaned data to see the impact
+assert.writeOK(st.shard0.getCollection(coll.toString()).insert({ _id : 3, a : { b : 1 } }));
+assert.eq(1, coll.remove({ a : { b : 1 } }, { justOne : true }).nRemoved);
+assert.eq(2, st.shard0.getCollection(coll.toString()).count() +
+ st.shard1.getCollection(coll.toString()).count() );
+
+//
+// Non-multi update
+coll.remove({});
+assert.writeOK(coll.insert({ _id : 1, a : { b : 1 } }));
+assert.writeOK(coll.insert({ _id : 2, a : { b : -1 } }));
+// Need orphaned data to see the impact
+assert.writeOK(st.shard0.getCollection(coll.toString()).insert({ _id : 3, a : { b : 1 } }));
+assert.eq(1, coll.update({ a : { b : 1 } }, { $set : { updated : true } },
+ { multi : false }).nMatched);
+assert.eq(1, st.shard0.getCollection(coll.toString()).count({ updated : true }) +
+ st.shard1.getCollection(coll.toString()).count({ updated : true }) );
+
+//
+// Successive upserts (save()-style)
+coll.remove({});
+assert.writeOK(coll.update({ _id : 1 }, { _id : 1, a : { b : 1 } }, { upsert : true }));
+assert.writeOK(coll.update({ _id : 1 }, { _id : 1, a : { b : 1 } }, { upsert : true }));
+assert.eq(1, st.shard0.getCollection(coll.toString()).count() +
+ st.shard1.getCollection(coll.toString()).count() );
+
+//
+// Successive upserts (replacement-style)
+coll.remove({});
+assert.writeOK(coll.update({ a : { b : 1 } }, { a : { b : 1 } }, { upsert : true }));
+assert.writeOK(coll.update({ a : { b : 1 } }, { a : { b : 1 } }, { upsert : true }));
+assert.eq(1, st.shard0.getCollection(coll.toString()).count() +
+ st.shard1.getCollection(coll.toString()).count() );
+
+//
+// Successive upserts ($op-style)
+coll.remove({});
+assert.writeOK(coll.update({ a : { b : 1 } }, { $set : { upserted : true } }, { upsert : true }));
+assert.writeOK(coll.update({ a : { b : 1 } }, { $set : { upserted : true } }, { upsert : true }));
+assert.eq(1, st.shard0.getCollection(coll.toString()).count() +
+ st.shard1.getCollection(coll.toString()).count() );
+
+jsTest.log("DONE!");
+st.stop(); \ No newline at end of file