diff options
author | Jason Zhang <jason.zhang@mongodb.com> | 2023-05-17 02:23:47 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-05-17 03:15:10 +0000 |
commit | 06797deec0c5806e56a70a7d156556e0b028da7f (patch) | |
tree | 07788fd471fdf4584b7b4b944c867d78932b88ec /jstests/sharding/query | |
parent | b4b4b310cd5fe22d33a749eca4e43c42c2adf76c (diff) | |
download | mongo-06797deec0c5806e56a70a7d156556e0b028da7f.tar.gz |
SERVER-69922 Implement explain for updateOne, deleteOne, and findAndModify
Diffstat (limited to 'jstests/sharding/query')
-rw-r--r-- | jstests/sharding/query/collation_targeting.js | 19 | ||||
-rw-r--r-- | jstests/sharding/query/collation_targeting_inherited.js | 17 | ||||
-rw-r--r-- | jstests/sharding/query/explain_cmd.js | 12 | ||||
-rw-r--r-- | jstests/sharding/query/explain_find_and_modify_sharded.js | 62 |
4 files changed, 79 insertions, 31 deletions
diff --git a/jstests/sharding/query/collation_targeting.js b/jstests/sharding/query/collation_targeting.js index 602c63c990a..329e65567c5 100644 --- a/jstests/sharding/query/collation_targeting.js +++ b/jstests/sharding/query/collation_targeting.js @@ -190,11 +190,10 @@ if (WriteWithoutShardKeyTestUtil.isWriteWithoutShardKeyFeatureEnabled(testDB)) { coll.findAndModify({query: {a: "foo"}, update: {$set: {b: 1}}, collation: caseInsensitive}); assert(res.a === "foo" || res.a === "FOO"); - // TODO: SERVER-69925 Implement explain for findAndModify. - // assert.throws(function() { - // coll.explain().findAndModify( - // {query: {a: "foo"}, update: {$set: {b: 1}}, collation: caseInsensitive}); - // }); + explain = coll.explain().findAndModify( + {query: {a: "foo"}, update: {$set: {b: 1}}, collation: caseInsensitive}); + assert.commandWorked(explain); + assert.eq(1, explain.queryPlanner.winningPlan.shards.length); } else { // Sharded findAndModify on strings with non-simple collation should fail, because findAndModify // must target a single shard. @@ -318,9 +317,13 @@ if (WriteWithoutShardKeyTestUtil.isWriteWithoutShardKeyFeatureEnabled(testDB)) { assert.eq(1, writeRes.nRemoved); let afterNumDocsMatch = coll.find({a: "foo"}).collation(caseInsensitive).count(); assert.eq(beforeNumDocsMatch - 1, afterNumDocsMatch); + + explain = coll.explain().remove({a: "foo"}, {justOne: true, collation: caseInsensitive}); + assert.commandWorked(explain); + assert.eq(1, explain.queryPlanner.winningPlan.shards.length); + coll.insert(a_foo); coll.insert(a_FOO); - // TODO: SERVER-69924 Implement explain for deleteOne } else { // A single remove (justOne: true) must be single-shard or an exact-ID query. A query is // exact-ID if it contains an equality on _id and either has the collection default collation or @@ -421,7 +424,9 @@ if (WriteWithoutShardKeyTestUtil.isWriteWithoutShardKeyFeatureEnabled(testDB)) { writeRes = assert.commandWorked(coll.update({a: "foo"}, {$set: {b: 1}}, {collation: caseInsensitive})); assert.eq(1, writeRes.nMatched); - // TODO: SERVER-69922 Implement explain for updateOne + explain = coll.explain().update({a: "foo"}, {$set: {b: 1}}, {collation: caseInsensitive}); + assert.commandWorked(explain); + assert.eq(1, explain.queryPlanner.winningPlan.shards.length); } else { // A single (non-multi) update must be single-shard or an exact-ID query. A query is exact-ID if // it contains an equality on _id and either has the collection default collation or _id is not diff --git a/jstests/sharding/query/collation_targeting_inherited.js b/jstests/sharding/query/collation_targeting_inherited.js index 7662baeee0f..4cc9e23765d 100644 --- a/jstests/sharding/query/collation_targeting_inherited.js +++ b/jstests/sharding/query/collation_targeting_inherited.js @@ -208,11 +208,10 @@ assert.eq(1, explain.queryPlanner.winningPlan.shards.length); if (WriteWithoutShardKeyTestUtil.isWriteWithoutShardKeyFeatureEnabled(testDB)) { let res = collCaseInsensitive.findAndModify({query: {a: "foo"}, update: {$set: {b: 1}}}); assert(res.a === "foo" || res.a === "FOO"); - - // TODO: SERVER-69925 Implement explain for findAndModify. - // assert.throws(function() { - // collCaseInsensitive.explain().findAndModify({query: {a: "foo"}, update: {$set: {b: 1}}}); - // }); + explain = + collCaseInsensitive.explain().findAndModify({query: {a: "foo"}, update: {$set: {b: 1}}}); + assert.commandWorked(explain); + assert.eq(1, explain.queryPlanner.winningPlan.shards.length); } else { // Sharded findAndModify on strings with non-simple collation inherited from the collection // default should fail, because findAndModify must target a single shard. @@ -340,7 +339,9 @@ if (WriteWithoutShardKeyTestUtil.isWriteWithoutShardKeyFeatureEnabled(testDB)) { assert.eq(1, writeRes.nRemoved); let afterNumDocsMatch = collCaseInsensitive.find({a: "foo"}).collation(caseInsensitive).count(); assert.eq(beforeNumDocsMatch - 1, afterNumDocsMatch); - // TODO: SERVER-69924 Implement explain for deleteOne + explain = collCaseInsensitive.explain().remove({a: "foo"}, {justOne: true}); + assert.commandWorked(explain); + assert.eq(1, explain.queryPlanner.winningPlan.shards.length); // Re-insert documents for later test cases. collCaseInsensitive.insert(a_foo); @@ -451,7 +452,9 @@ assert.eq(1, explain.queryPlanner.winningPlan.shards.length); if (WriteWithoutShardKeyTestUtil.isWriteWithoutShardKeyFeatureEnabled(testDB)) { writeRes = assert.commandWorked(collCaseInsensitive.update({a: "foo"}, {$set: {b: 1}})); assert.eq(1, writeRes.nMatched); - // TODO: SERVER-69922 Implement explain for updateOne + explain = collCaseInsensitive.explain().update({a: "foo"}, {$set: {b: 1}}); + assert.commandWorked(explain); + assert.eq(1, explain.queryPlanner.winningPlan.shards.length); } else { // A single (non-multi) update must be single-shard or an exact-ID query. A query is exact-ID if // it diff --git a/jstests/sharding/query/explain_cmd.js b/jstests/sharding/query/explain_cmd.js index 544c426f546..249c7e30f42 100644 --- a/jstests/sharding/query/explain_cmd.js +++ b/jstests/sharding/query/explain_cmd.js @@ -135,15 +135,21 @@ assert.eq(explain.queryPlanner.winningPlan.shards.length, 1); // Check that the upsert didn't actually happen. assert.eq(0, collSharded.count({a: 10})); +// Sharded updateOne that does not target a single shard can now be executed with a two phase +// write protocol that will target at most 1 matching document. if (WriteWithoutShardKeyTestUtil.isWriteWithoutShardKeyFeatureEnabled(collSharded.getDB())) { // Explain an upsert operation which cannot be targeted and verify that it is successful. - // TODO SERVER-69922: Verify expected response. explain = db.runCommand({ explain: {update: collSharded.getName(), updates: [{q: {b: 10}, u: {b: 10}, upsert: true}]}, verbosity: "allPlansExecution" }); - assert.commandWorked(explain, tojson(explain)); - assert.eq(explain.queryPlanner.winningPlan.shards.length, 2); + assert(explain.queryPlanner); + assert(explain.executionStats); + assert.eq(explain.queryPlanner.winningPlan.stage, "SHARD_WRITE"); + assert.eq(explain.queryPlanner.winningPlan.inputStage.winningPlan.stage, "SHARD_MERGE"); + assert.eq(explain.executionStats.executionStages.stage, "SHARD_WRITE"); + assert.eq(explain.executionStats.inputStage.executionStages.stage, "SHARD_MERGE"); + // Check that the upsert didn't actually happen. assert.eq(0, collSharded.count({b: 10})); } else { diff --git a/jstests/sharding/query/explain_find_and_modify_sharded.js b/jstests/sharding/query/explain_find_and_modify_sharded.js index 65f5dc17d6c..29dab239f5e 100644 --- a/jstests/sharding/query/explain_find_and_modify_sharded.js +++ b/jstests/sharding/query/explain_find_and_modify_sharded.js @@ -5,6 +5,8 @@ (function() { 'use strict'; +load("jstests/sharding/updateOne_without_shard_key/libs/write_without_shard_key_test_util.js"); + var collName = 'explain_find_and_modify'; // Create a cluster with 2 shards. @@ -37,21 +39,53 @@ assert.commandWorked(testDB.adminCommand( var res; -// Queries that do not involve the shard key are invalid. -res = testDB.runCommand( - {explain: {findAndModify: collName, query: {b: 1}, remove: true}, verbosity: 'queryPlanner'}); -assert.commandFailed(res); +// Sharded updateOne that does not target a single shard can now be executed with a two phase +// write protocol that will target at most 1 matching document. +if (WriteWithoutShardKeyTestUtil.isWriteWithoutShardKeyFeatureEnabled(testDB)) { + res = assert.commandWorked(testDB.runCommand({ + explain: {findAndModify: collName, query: {b: 1}, remove: true}, + verbosity: 'queryPlanner' + })); -// Queries that have non-equality queries on the shard key are invalid. -res = testDB.runCommand({ - explain: { - findAndModify: collName, - query: {a: {$gt: 5}}, - update: {$inc: {b: 7}}, - }, - verbosity: 'allPlansExecution' -}); -assert.commandFailed(res); + assert(res.queryPlanner); + assert(!res.executionStats); + assert.eq(res.queryPlanner.winningPlan.stage, "SHARD_WRITE"); + assert.eq(res.queryPlanner.winningPlan.inputStage.winningPlan.stage, "SHARD_MERGE"); + + res = assert.commandWorked(testDB.runCommand({ + explain: { + findAndModify: collName, + query: {a: {$gt: 5}}, + update: {$inc: {b: 7}}, + }, + verbosity: 'allPlansExecution' + })); + + assert(res.queryPlanner); + assert(res.executionStats); + assert.eq(res.queryPlanner.winningPlan.stage, "SHARD_WRITE"); + assert.eq(res.queryPlanner.winningPlan.inputStage.winningPlan.stage, "SHARD_MERGE"); + assert.eq(res.executionStats.executionStages.stage, "SHARD_WRITE"); + assert.eq(res.executionStats.inputStage.executionStages.stage, "SHARD_MERGE"); +} else { + // Queries that do not involve the shard key are invalid. + res = testDB.runCommand({ + explain: {findAndModify: collName, query: {b: 1}, remove: true}, + verbosity: 'queryPlanner' + }); + assert.commandFailed(res); + + // Queries that have non-equality queries on the shard key are invalid. + res = testDB.runCommand({ + explain: { + findAndModify: collName, + query: {a: {$gt: 5}}, + update: {$inc: {b: 7}}, + }, + verbosity: 'allPlansExecution' + }); + assert.commandFailed(res); +} // Asserts that the explain command ran on the specified shard and used the given stage // for performing the findAndModify command. |