1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
/**
* This file contains tests for mongo/db/commands/mr.h
*/
#include "mongo/db/commands/mr.h"
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "mongo/db/catalog/collection_options.h"
#include "mongo/db/client.h"
#include "mongo/db/commands.h"
#include "mongo/db/dbdirectclient.h"
#include "mongo/db/json.h"
#include "mongo/db/op_observer_noop.h"
#include "mongo/db/op_observer_registry.h"
#include "mongo/db/repl/drop_pending_collection_reaper.h"
#include "mongo/db/repl/replication_coordinator_mock.h"
#include "mongo/db/repl/storage_interface_impl.h"
#include "mongo/db/service_context_d_test_fixture.h"
#include "mongo/rpc/factory.h"
#include "mongo/rpc/get_status_from_command_result.h"
#include "mongo/scripting/dbdirectclient_factory.h"
#include "mongo/scripting/engine.h"
#include "mongo/unittest/unittest.h"
namespace mongo {
namespace {
/**
* Tests for mr::Config
*/
/**
* Helper function to verify field of mr::Config::OutputOptions.
*/
template <typename T>
void _compareOutputOptionField(const std::string& dbname,
const std::string& cmdObjStr,
const std::string& fieldName,
const T& actual,
const T& expected) {
if (actual == expected)
return;
FAIL(str::stream() << "parseOutputOptions(\"" << dbname << ", " << cmdObjStr << "): "
<< fieldName
<< ": Expected: "
<< expected
<< ". Actual: "
<< actual);
}
/**
* Returns string representation of mr::Config::OutputType
*/
std::string _getOutTypeString(mr::Config::OutputType outType) {
switch (outType) {
case mr::Config::REPLACE:
return "REPLACE";
case mr::Config::MERGE:
return "MERGE";
case mr::Config::REDUCE:
return "REDUCE";
case mr::Config::INMEMORY:
return "INMEMORY";
}
MONGO_UNREACHABLE;
}
/**
* Test helper function to check expected result of parseOutputOptions.
*/
void _testConfigParseOutputOptions(const std::string& dbname,
const std::string& cmdObjStr,
const std::string& expectedOutDb,
const std::string& expectedCollectionName,
const std::string& expectedFinalNamespace,
bool expectedOutNonAtomic,
mr::Config::OutputType expectedOutType) {
const BSONObj cmdObj = fromjson(cmdObjStr);
mr::Config::OutputOptions outputOptions = mr::Config::parseOutputOptions(dbname, cmdObj);
_compareOutputOptionField(dbname, cmdObjStr, "outDb", outputOptions.outDB, expectedOutDb);
_compareOutputOptionField(
dbname, cmdObjStr, "collectionName", outputOptions.collectionName, expectedCollectionName);
_compareOutputOptionField(dbname,
cmdObjStr,
"finalNamespace",
outputOptions.finalNamespace.ns(),
expectedFinalNamespace);
_compareOutputOptionField(
dbname, cmdObjStr, "outNonAtomic", outputOptions.outNonAtomic, expectedOutNonAtomic);
_compareOutputOptionField(dbname,
cmdObjStr,
"outType",
_getOutTypeString(outputOptions.outType),
_getOutTypeString(expectedOutType));
}
/**
* Tests for mr::Config::parseOutputOptions.
*/
TEST(ConfigOutputOptionsTest, parseOutputOptions) {
// Missing 'out' field.
ASSERT_THROWS(mr::Config::parseOutputOptions("mydb", fromjson("{}")), AssertionException);
// 'out' must be either string or object.
ASSERT_THROWS(mr::Config::parseOutputOptions("mydb", fromjson("{out: 99}")),
AssertionException);
// 'out.nonAtomic' is not supported with normal, replace or inline.
ASSERT_THROWS(mr::Config::parseOutputOptions(
"mydb", fromjson("{out: {normal: 'mycoll', nonAtomic: true}}")),
AssertionException);
ASSERT_THROWS(mr::Config::parseOutputOptions(
"mydb", fromjson("{out: {replace: 'mycoll', nonAtomic: true}}")),
AssertionException);
ASSERT_THROWS(mr::Config::parseOutputOptions(
"mydb", fromjson("{out: {inline: 'mycoll', nonAtomic: true}}")),
AssertionException);
// Unknown output specifer.
ASSERT_THROWS(
mr::Config::parseOutputOptions("mydb", fromjson("{out: {no_such_out_type: 'mycoll'}}")),
AssertionException);
// 'out' is string.
_testConfigParseOutputOptions(
"mydb", "{out: 'mycoll'}", "", "mycoll", "mydb.mycoll", false, mr::Config::REPLACE);
// 'out' is object.
_testConfigParseOutputOptions("mydb",
"{out: {normal: 'mycoll'}}",
"",
"mycoll",
"mydb.mycoll",
false,
mr::Config::REPLACE);
// 'out.db' overrides dbname parameter
_testConfigParseOutputOptions("mydb1",
"{out: {replace: 'mycoll', db: 'mydb2'}}",
"mydb2",
"mycoll",
"mydb2.mycoll",
false,
mr::Config::REPLACE);
// 'out.nonAtomic' is supported with merge and reduce.
_testConfigParseOutputOptions("mydb",
"{out: {merge: 'mycoll', nonAtomic: true}}",
"",
"mycoll",
"mydb.mycoll",
true,
mr::Config::MERGE);
_testConfigParseOutputOptions("mydb",
"{out: {reduce: 'mycoll', nonAtomic: true}}",
"",
"mycoll",
"mydb.mycoll",
true,
mr::Config::REDUCE);
// inline
_testConfigParseOutputOptions("mydb1",
"{out: {inline: 'mycoll', db: 'mydb2'}}",
"mydb2",
"",
"",
false,
mr::Config::INMEMORY);
// Order should not matter in fields of 'out' object.
_testConfigParseOutputOptions("mydb1",
"{out: {db: 'mydb2', normal: 'mycoll'}}",
"mydb2",
"mycoll",
"mydb2.mycoll",
false,
mr::Config::REPLACE);
_testConfigParseOutputOptions("mydb1",
"{out: {db: 'mydb2', replace: 'mycoll'}}",
"mydb2",
"mycoll",
"mydb2.mycoll",
false,
mr::Config::REPLACE);
_testConfigParseOutputOptions("mydb1",
"{out: {nonAtomic: true, merge: 'mycoll'}}",
"",
"mycoll",
"mydb1.mycoll",
true,
mr::Config::MERGE);
_testConfigParseOutputOptions("mydb1",
"{out: {nonAtomic: true, reduce: 'mycoll'}}",
"",
"mycoll",
"mydb1.mycoll",
true,
mr::Config::REDUCE);
_testConfigParseOutputOptions("mydb1",
"{out: {db: 'mydb2', inline: 'mycoll'}}",
"mydb2",
"",
"",
false,
mr::Config::INMEMORY);
}
TEST(ConfigTest, ParseCollation) {
std::string dbname = "myDB";
BSONObj collation = BSON("locale"
<< "en_US");
BSONObjBuilder bob;
bob.append("mapReduce", "myCollection");
bob.appendCode("map", "function() { emit(0, 1); }");
bob.appendCode("reduce", "function(k, v) { return {count: 0}; }");
bob.append("out", "outCollection");
bob.append("collation", collation);
BSONObj cmdObj = bob.obj();
mr::Config config(dbname, cmdObj);
ASSERT_BSONOBJ_EQ(config.collation, collation);
}
TEST(ConfigTest, ParseNoCollation) {
std::string dbname = "myDB";
BSONObjBuilder bob;
bob.append("mapReduce", "myCollection");
bob.appendCode("map", "function() { emit(0, 1); }");
bob.appendCode("reduce", "function(k, v) { return {count: 0}; }");
bob.append("out", "outCollection");
BSONObj cmdObj = bob.obj();
mr::Config config(dbname, cmdObj);
ASSERT_BSONOBJ_EQ(config.collation, BSONObj());
}
TEST(ConfigTest, CollationNotAnObjectFailsToParse) {
std::string dbname = "myDB";
BSONObjBuilder bob;
bob.append("mapReduce", "myCollection");
bob.appendCode("map", "function() { emit(0, 1); }");
bob.appendCode("reduce", "function(k, v) { return {count: 0}; }");
bob.append("out", "outCollection");
bob.append("collation", "en_US");
BSONObj cmdObj = bob.obj();
ASSERT_THROWS(mr::Config(dbname, cmdObj), AssertionException);
}
/**
* OpObserver for mapReduce test fixture.
*/
class MapReduceOpObserver : public OpObserverNoop {
public:
/**
* This function is called whenever mapReduce inserts documents into a temporary output
* collection.
*/
void onInserts(OperationContext* opCtx,
const NamespaceString& nss,
OptionalCollectionUUID uuid,
std::vector<InsertStatement>::const_iterator begin,
std::vector<InsertStatement>::const_iterator end,
bool fromMigrate) override;
/**
* Tracks the temporary collections mapReduces creates.
*/
void onCreateCollection(OperationContext* opCtx,
Collection* coll,
const NamespaceString& collectionName,
const CollectionOptions& options,
const BSONObj& idIndex,
const OplogSlot& createOpTime) override;
repl::OpTime onDropCollection(OperationContext* opCtx,
const NamespaceString& collectionName,
OptionalCollectionUUID uuid,
std::uint64_t numRecords,
CollectionDropType dropType) override;
// Hook for onInserts. Defaults to a no-op function but may be overridden to inject exceptions
// while mapReduce inserts its results into the temporary output collection.
std::function<void()> onInsertsFn = [] {};
// Holds namespaces of temporary collections created by mapReduce.
std::vector<NamespaceString> tempNamespaces;
const repl::OpTime dropOpTime = {Timestamp(Seconds(100), 1U), 1LL};
};
void MapReduceOpObserver::onInserts(OperationContext* opCtx,
const NamespaceString& nss,
OptionalCollectionUUID uuid,
std::vector<InsertStatement>::const_iterator begin,
std::vector<InsertStatement>::const_iterator end,
bool fromMigrate) {
onInsertsFn();
}
void MapReduceOpObserver::onCreateCollection(OperationContext*,
Collection*,
const NamespaceString& collectionName,
const CollectionOptions& options,
const BSONObj&,
const OplogSlot&) {
if (!options.temp) {
return;
}
tempNamespaces.push_back(collectionName);
}
repl::OpTime MapReduceOpObserver::onDropCollection(OperationContext* opCtx,
const NamespaceString& collectionName,
OptionalCollectionUUID uuid,
std::uint64_t numRecords,
const CollectionDropType dropType) {
// If the oplog is not disabled for this namespace, then we need to reserve an op time for the
// drop.
if (!repl::ReplicationCoordinator::get(opCtx)->isOplogDisabledFor(opCtx, collectionName)) {
OpObserver::Times::get(opCtx).reservedOpTimes.push_back(dropOpTime);
}
return {};
}
/**
* Test fixture for MapReduceCommand.
*/
class MapReduceCommandTest : public ServiceContextMongoDTest {
public:
static const NamespaceString inputNss;
static const NamespaceString outputNss;
private:
void setUp() override;
void tearDown() override;
protected:
/**
* Looks up the current ReplicationCoordinator.
* The result is cast to a ReplicationCoordinatorMock to provide access to test features.
*/
repl::ReplicationCoordinatorMock* _getReplCoord() const;
/**
* Creates a mapReduce command object that reads from 'inputNss' and writes results to
* 'outputNss'.
*/
BSONObj _makeCmdObj(StringData mapCode, StringData reduceCode);
/**
* Runs a mapReduce command.
* Ensures that temporary collections created by mapReduce no longer exist on success.
*/
Status _runCommand(StringData mapCode, StringData reduceCode);
/**
* Checks that temporary collections created during mapReduce have been dropped.
* This is made a separate test helper to handle cases where mapReduce is unable to remove
* its temporary collections.
*/
void _assertTemporaryCollectionsAreDropped();
ServiceContext::UniqueOperationContext _opCtx;
repl::StorageInterfaceImpl _storage;
MapReduceOpObserver* _opObserver = nullptr;
};
const NamespaceString MapReduceCommandTest::inputNss("myDB.myCollection");
const NamespaceString MapReduceCommandTest::outputNss(inputNss.getSisterNS("outCollection"));
void MapReduceCommandTest::setUp() {
ServiceContextMongoDTest::setUp();
ScriptEngine::setup();
auto service = getServiceContext();
DBDirectClientFactory::get(service).registerImplementation(
[](OperationContext* opCtx) { return std::make_unique<DBDirectClient>(opCtx); });
repl::ReplicationCoordinator::set(service,
std::make_unique<repl::ReplicationCoordinatorMock>(service));
repl::DropPendingCollectionReaper::set(
service,
stdx::make_unique<repl::DropPendingCollectionReaper>(repl::StorageInterface::get(service)));
// Set up an OpObserver to track the temporary collections mapReduce creates.
auto opObserver = std::make_unique<MapReduceOpObserver>();
_opObserver = opObserver.get();
auto opObserverRegistry = dynamic_cast<OpObserverRegistry*>(service->getOpObserver());
opObserverRegistry->addObserver(std::move(opObserver));
_opCtx = cc().makeOperationContext();
// Transition to PRIMARY so that the server can accept writes.
ASSERT_OK(_getReplCoord()->setFollowerMode(repl::MemberState::RS_PRIMARY));
// Create collection with one document.
CollectionOptions collectionOptions;
collectionOptions.uuid = UUID::gen();
ASSERT_OK(_storage.createCollection(_opCtx.get(), inputNss, collectionOptions));
}
void MapReduceCommandTest::tearDown() {
_opCtx = {};
_opObserver = nullptr;
ScriptEngine::dropScopeCache();
ServiceContextMongoDTest::tearDown();
}
repl::ReplicationCoordinatorMock* MapReduceCommandTest::_getReplCoord() const {
auto replCoord = repl::ReplicationCoordinator::get(_opCtx.get());
ASSERT(replCoord) << "No ReplicationCoordinator installed";
auto replCoordMock = dynamic_cast<repl::ReplicationCoordinatorMock*>(replCoord);
ASSERT(replCoordMock) << "Unexpected type for installed ReplicationCoordinator";
return replCoordMock;
}
BSONObj MapReduceCommandTest::_makeCmdObj(StringData mapCode, StringData reduceCode) {
BSONObjBuilder bob;
bob.append("mapReduce", inputNss.coll());
bob.appendCode("map", mapCode);
bob.appendCode("reduce", reduceCode);
bob.append("out", outputNss.coll());
return bob.obj();
}
Status MapReduceCommandTest::_runCommand(StringData mapCode, StringData reduceCode) {
auto command = CommandHelpers::findCommand("mapReduce");
ASSERT(command) << "Unable to look up mapReduce command";
auto request = OpMsgRequest::fromDBAndBody(inputNss.db(), _makeCmdObj(mapCode, reduceCode));
auto replyBuilder = rpc::makeReplyBuilder(rpc::Protocol::kOpMsg);
auto result = CommandHelpers::runCommandDirectly(_opCtx.get(), request);
auto status = getStatusFromCommandResult(result);
if (!status.isOK()) {
return status.withContext(str::stream() << "mapReduce command failed: " << request.body);
}
_assertTemporaryCollectionsAreDropped();
return Status::OK();
}
void MapReduceCommandTest::_assertTemporaryCollectionsAreDropped() {
for (const auto& tempNss : _opObserver->tempNamespaces) {
ASSERT_EQUALS(ErrorCodes::NamespaceNotFound,
_storage.getCollectionCount(_opCtx.get(), tempNss))
<< "mapReduce did not remove temporary collection on success: " << tempNss.ns();
}
}
TEST_F(MapReduceCommandTest, MapIdToValue) {
auto sourceDoc = BSON("_id" << 0);
ASSERT_OK(_storage.insertDocument(_opCtx.get(), inputNss, {sourceDoc, Timestamp(0)}, 1LL));
auto mapCode = "function() { emit(this._id, this._id); }"_sd;
auto reduceCode = "function(k, v) { return Array.sum(v); }"_sd;
ASSERT_OK(_runCommand(mapCode, reduceCode));
auto targetDoc = BSON("_id" << 0 << "value" << 0);
ASSERT_BSONOBJ_EQ(targetDoc,
unittest::assertGet(_storage.findSingleton(_opCtx.get(), outputNss)));
}
TEST_F(MapReduceCommandTest, DropTemporaryCollectionsOnInsertError) {
auto sourceDoc = BSON("_id" << 0);
ASSERT_OK(_storage.insertDocument(_opCtx.get(), inputNss, {sourceDoc, Timestamp(0)}, 1LL));
_opObserver->onInsertsFn = [] { uasserted(ErrorCodes::OperationFailed, ""); };
auto mapCode = "function() { emit(this._id, this._id); }"_sd;
auto reduceCode = "function(k, v) { return Array.sum(v); }"_sd;
ASSERT_EQ(_runCommand(mapCode, reduceCode), ErrorCodes::OperationFailed);
// Temporary collections created by mapReduce will be removed on failure if the server is able
// to accept writes.
_assertTemporaryCollectionsAreDropped();
}
TEST_F(MapReduceCommandTest, PrimaryStepDownPreventsTemporaryCollectionDrops) {
auto sourceDoc = BSON("_id" << 0);
ASSERT_OK(_storage.insertDocument(_opCtx.get(), inputNss, {sourceDoc, Timestamp(0)}, 1LL));
_opObserver->onInsertsFn = [this] {
ASSERT_OK(_getReplCoord()->setFollowerMode(repl::MemberState::RS_SECONDARY));
uasserted(ErrorCodes::OperationFailed, "");
};
auto mapCode = "function() { emit(this._id, this._id); }"_sd;
auto reduceCode = "function(k, v) { return Array.sum(v); }"_sd;
ASSERT_EQ(_runCommand(mapCode, reduceCode), ErrorCodes::OperationFailed);
// Temporary collections should still be present because the server will not accept writes after
// stepping down.
for (const auto& tempNss : _opObserver->tempNamespaces) {
ASSERT_OK(_storage.getCollectionCount(_opCtx.get(), tempNss).getStatus())
<< "missing mapReduce temporary collection: " << tempNss;
}
}
} // namespace
} // namespace mongo
|