diff options
author | Maddie Zechar <mez2113@columbia.edu> | 2021-05-24 16:37:46 +0000 |
---|---|---|
committer | Maddie Zechar <mez2113@columbia.edu> | 2021-06-24 22:29:28 +0000 |
commit | 7145bb5e73547c2ddc17012b5b6ebaaab8952546 (patch) | |
tree | 8032390096e3025656c29acef56758b32e662fa8 | |
parent | 70397073ac3fb14afc19f69bebf0f67cab041a16 (diff) | |
download | mongo-7145bb5e73547c2ddc17012b5b6ebaaab8952546.tar.gz |
SERVER-57258: Add deprecation msg for mapReduce()
-rw-r--r-- | jstests/noPassthrough/deprecated_map_reduce.js | 134 | ||||
-rw-r--r-- | src/mongo/db/commands/map_reduce_agg.cpp | 11 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_map_reduce_agg.cpp | 11 |
3 files changed, 153 insertions, 3 deletions
diff --git a/jstests/noPassthrough/deprecated_map_reduce.js b/jstests/noPassthrough/deprecated_map_reduce.js new file mode 100644 index 00000000000..008e132cf90 --- /dev/null +++ b/jstests/noPassthrough/deprecated_map_reduce.js @@ -0,0 +1,134 @@ +// The map reduce command is deprecated in 5.0. +// +// In this test, we run the map reduce command multiple times. +// We want to make sure that the deprecation warning message is only logged once despite +// the multiple invocations in an effort to not clutter the dev's console. +// More specifically, we expect to only log 1/127 of mapReduce() events. + +(function() { +"use strict"; +load("jstests/libs/log.js"); // For findMatchingLogLine, findMatchingLogLines + +jsTest.log('Test standalone'); +const caseInsensitive = { + collation: {locale: "simple", strength: 2} +}; +const standalone = MongoRunner.runMongod({}); +const dbName = 'test'; +const collName = "test_map_reduce_command_deprecation_messaging"; +const db = standalone.getDB(dbName); +const coll = db.getCollection(collName); +const fieldMatcher = { + msg: + "The map reduce command is deprecated. For more information, see https://docs.mongodb.com/manual/core/map-reduce/" +}; + +function mapFunc() { + emit(this.cust_id, this.amount); +} +function reduceFunc(key, values) { + return Array.sum(values); +} + +coll.drop(); +assert.commandWorked(coll.insert({cust_id: "A", amount: 100, status: "B"})); +assert.commandWorked(coll.insert({cust_id: "A", amount: 200, status: "B"})); +assert.commandWorked(coll.insert({cust_id: "B", amount: 50, status: "B"})); + +// Assert that deprecation msg is not logged before map reduce command is even run. +var globalLogs = db.adminCommand({getLog: 'global'}); +var matchingLogLines = [...findMatchingLogLines(globalLogs.log, fieldMatcher)]; +assert.eq(matchingLogLines.length, 0, matchingLogLines); + +assert.commandWorked(db.runCommand( + {mapReduce: collName, map: mapFunc, reduce: reduceFunc, query: {b: 2}, out: "order_totals"})); + +assert.commandWorked(coll.insert({cust_id: "B", amount: 50, status: "B"})); + +assert.commandWorked(db.runCommand( + {mapReduce: collName, map: mapFunc, reduce: reduceFunc, query: {b: 2}, out: "order_totals"})); + +assert.commandWorked(coll.insert({cust_id: "A", amount: 200, status: "B"})); + +assert.commandWorked(db.runCommand( + {mapReduce: collName, map: mapFunc, reduce: reduceFunc, query: {"B": 2}, out: "order_totals"})); + +// Now that we have ran map reduce command, make sure the deprecation message is logged once. +globalLogs = db.adminCommand({getLog: 'global'}); +matchingLogLines = [...findMatchingLogLines(globalLogs.log, fieldMatcher)]; +assert.eq(matchingLogLines.length, 1, matchingLogLines); +MongoRunner.stopMongod(standalone); + +jsTest.log('Test cluster'); + +const st = new ShardingTest({shards: 2, mongos: 1}); + +const session = st.s.getDB("test").getMongo().startSession(); +const mongosDB = session.getDatabase("test"); +const mongosColl = mongosDB.testing; + +mongosColl.drop(); +assert.commandWorked(mongosDB.createCollection(mongosColl.getName(), caseInsensitive)); + +assert.commandWorked(mongosColl.insert({cust_id: "A", amount: 100, status: "B"})); +assert.commandWorked(mongosColl.insert({cust_id: "A", amount: 200, status: "B"})); +assert.commandWorked(mongosColl.insert({cust_id: "B", amount: 50, status: "B"})); +assert.commandWorked(mongosColl.insert({cust_id: "A", amount: 10, status: "B"})); +assert.commandWorked(mongosColl.insert({cust_id: "A", amount: 20, status: "B"})); +assert.commandWorked(mongosColl.insert({cust_id: "B", amount: 5, status: "B"})); + +assert.commandWorked(st.s0.adminCommand({enableSharding: mongosDB.getName()})); +st.ensurePrimaryShard(db.getName(), st.shard0.shardName); +assert.commandWorked( + st.s0.adminCommand({shardCollection: mongosColl.getFullName(), key: {_id: 1}})); + +// Assert that deprecation msg is not logged before map reduce command is even run. +globalLogs = mongosDB.adminCommand({getLog: 'global'}); +matchingLogLines = [...findMatchingLogLines(globalLogs.log, fieldMatcher)]; +assert.eq(matchingLogLines.length, 0, matchingLogLines); + +// Check the logs of the primary shard of the mongos. +globalLogs = st.shard0.getDB("test").adminCommand({getLog: 'global'}); +matchingLogLines = [...findMatchingLogLines(globalLogs.log, fieldMatcher)]; +assert.eq(matchingLogLines.length, 0, matchingLogLines); + +assert.commandWorked(mongosDB.runCommand({ + mapReduce: mongosColl.getName(), + map: mapFunc, + reduce: reduceFunc, + query: {b: 2}, + out: "order_totals" +})); + +assert.commandWorked(mongosColl.insert({cust_id: "B", amount: 50, status: "B"})); + +assert.commandWorked(mongosDB.runCommand({ + mapReduce: mongosColl.getName(), + map: mapFunc, + reduce: reduceFunc, + query: {b: 2}, + out: "order_totals" +})); + +assert.commandWorked(mongosColl.insert({cust_id: "A", amount: 200, status: "B"})); + +assert.commandWorked(mongosDB.runCommand({ + mapReduce: mongosColl.getName(), + map: mapFunc, + reduce: reduceFunc, + query: {"B": 2}, + out: "order_totals" +})); + +// Now that we have ran map reduce command, make sure the deprecation message is logged once. +globalLogs = mongosDB.adminCommand({getLog: 'global'}); +matchingLogLines = [...findMatchingLogLines(globalLogs.log, fieldMatcher)]; +assert.eq(matchingLogLines.length, 1, matchingLogLines); + +// Check the logs of the primary shard of the mongos. +globalLogs = st.shard0.getDB("test").adminCommand({getLog: 'global'}); +matchingLogLines = [...findMatchingLogLines(globalLogs.log, fieldMatcher)]; +assert.eq(matchingLogLines.length, 0, matchingLogLines); + +st.stop(); +})(); diff --git a/src/mongo/db/commands/map_reduce_agg.cpp b/src/mongo/db/commands/map_reduce_agg.cpp index 43a66fb2314..b75a18a4a77 100644 --- a/src/mongo/db/commands/map_reduce_agg.cpp +++ b/src/mongo/db/commands/map_reduce_agg.cpp @@ -26,7 +26,7 @@ * exception statement from all source files in the program, then also delete * it in the license file. */ - +#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kCommand #include "mongo/platform/basic.h" #include <boost/intrusive_ptr.hpp> @@ -53,10 +53,12 @@ #include "mongo/db/query/explain_common.h" #include "mongo/db/query/map_reduce_output_format.h" #include "mongo/db/query/plan_executor_factory.h" +#include "mongo/logv2/log.h" namespace mongo::map_reduce_agg { namespace { +Rarely _sampler; auto makeExpressionContext(OperationContext* opCtx, const MapReduceCommandRequest& parsedMr, @@ -112,6 +114,13 @@ bool runAggregationMapReduce(OperationContext* opCtx, const BSONObj& cmd, BSONObjBuilder& result, boost::optional<ExplainOptions::Verbosity> verbosity) { + + if (_sampler.tick()) { + LOGV2_WARNING(5725801, + "The map reduce command is deprecated. For more information, see " + "https://docs.mongodb.com/manual/core/map-reduce/"); + } + auto exhaustPipelineIntoBSONArray = [](auto&& exec) { BSONArrayBuilder bab; BSONObj obj; diff --git a/src/mongo/s/commands/cluster_map_reduce_agg.cpp b/src/mongo/s/commands/cluster_map_reduce_agg.cpp index 9d16d22513f..7cba5b51bd5 100644 --- a/src/mongo/s/commands/cluster_map_reduce_agg.cpp +++ b/src/mongo/s/commands/cluster_map_reduce_agg.cpp @@ -26,7 +26,7 @@ * exception statement from all source files in the program, then also delete * it in the license file. */ - +#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kCommand #include "mongo/platform/basic.h" #include "mongo/bson/bsonobj.h" @@ -47,6 +47,7 @@ #include "mongo/db/query/explain_common.h" #include "mongo/db/query/getmore_request.h" #include "mongo/db/query/map_reduce_output_format.h" +#include "mongo/logv2/log.h" #include "mongo/s/catalog_cache.h" #include "mongo/s/cluster_commands_helpers.h" #include "mongo/s/commands/cluster_map_reduce_agg.h" @@ -55,7 +56,7 @@ namespace mongo { namespace { - +Rarely _sampler; auto makeExpressionContext(OperationContext* opCtx, const MapReduceCommandRequest& parsedMr, const ChunkManager& cm, @@ -152,6 +153,12 @@ bool runAggregationMapReduce(OperationContext* opCtx, auto resolvedOutNss = NamespaceString{hasOutDB ? *hasOutDB : parsedMr.getNamespace().db(), parsedMr.getOutOptions().getCollectionName()}; + if (_sampler.tick()) { + LOGV2_WARNING(5725800, + "The map reduce command is deprecated. For more information, see " + "https://docs.mongodb.com/manual/core/map-reduce/"); + } + if (parsedMr.getOutOptions().getOutputType() != OutputType::InMemory) { involvedNamespaces.insert(resolvedOutNss); } |