diff options
| author | Kevin Albertson <kevin.albertson@10gen.com> | 2016-08-19 17:39:56 -0400 |
|---|---|---|
| committer | Kyle Suarez <kyle.suarez@mongodb.com> | 2016-08-19 17:44:29 -0400 |
| commit | 9efbffec2c4be8c597a6d39ba3474f5d4a110454 (patch) | |
| tree | 896138bdcd31977321fadffe6f8575d37bc2ddb6 | |
| parent | 3ff28718ccd78d876beafcaa437890dc08b71a42 (diff) | |
| download | mongo-9efbffec2c4be8c597a6d39ba3474f5d4a110454.tar.gz | |
SERVER-21168 Update top on database drop
| -rw-r--r-- | jstests/noPassthroughWithMongod/top_drop.js | 35 | ||||
| -rw-r--r-- | src/mongo/db/catalog/database.cpp | 4 | ||||
| -rw-r--r-- | src/mongo/db/stats/top.cpp | 10 | ||||
| -rw-r--r-- | src/mongo/db/stats/top.h | 2 |
4 files changed, 46 insertions, 5 deletions
diff --git a/jstests/noPassthroughWithMongod/top_drop.js b/jstests/noPassthroughWithMongod/top_drop.js new file mode 100644 index 00000000000..e31129b4a4d --- /dev/null +++ b/jstests/noPassthroughWithMongod/top_drop.js @@ -0,0 +1,35 @@ +/** + * Checks that top removes entries after dropping a collection or database. + * TODO(SERVER-21167): Move this test from noPassthrough to core. + */ +(function() { + let topDB = db.getSiblingDB("topdrop"); + assert.commandWorked(topDB.dropDatabase()); + + // Asserts that the output of top contains exactly these collection entries for topDB. + function checkTopEntries(expectedEntries) { + let res = topDB.adminCommand("top"); + assert.commandWorked(res); + let namesFromTotals = Object.keys(res.totals).filter(function(ns) { + return ns.startsWith(topDB.getName() + "."); + }); + let expectedEntryNames = expectedEntries.map(function(coll) { + return coll.getFullName(); + }); + assert.eq(namesFromTotals.sort(), expectedEntryNames.sort()); + } + + // Create a few entries in top. + assert.writeOK(topDB.coll1.insert({})); + assert.writeOK(topDB.coll2.insert({})); + assert.writeOK(topDB.coll3.insert({})); + checkTopEntries([topDB.coll1, topDB.coll2, topDB.coll3]); + + // Check that dropping a collection removes that collection but leaves the others. + assert.commandWorked(topDB.runCommand({drop: "coll3"})); + checkTopEntries([topDB.coll1, topDB.coll2]); + + // Check that dropping the database removes the remaining collections. + assert.commandWorked(topDB.dropDatabase()); + checkTopEntries([]); +}()); diff --git a/src/mongo/db/catalog/database.cpp b/src/mongo/db/catalog/database.cpp index 8f88ff86e31..41746be60ef 100644 --- a/src/mongo/db/catalog/database.cpp +++ b/src/mongo/db/catalog/database.cpp @@ -632,6 +632,10 @@ void Database::dropDatabase(OperationContext* txn, Database* db) { audit::logDropDatabase(txn->getClient(), name); + for (auto&& coll : *db) { + Top::get(txn->getClient()->getServiceContext()).collectionDropped(coll->ns().ns(), true); + } + dbHolder().close(txn, name); db = NULL; // d is now deleted diff --git a/src/mongo/db/stats/top.cpp b/src/mongo/db/stats/top.cpp index 30e47a6e1be..8bfe3431f6d 100644 --- a/src/mongo/db/stats/top.cpp +++ b/src/mongo/db/stats/top.cpp @@ -84,8 +84,6 @@ void Top::record(OperationContext* txn, return; auto hashedNs = UsageMap::HashedKey(ns); - - // cout << "record: " << ns << "\t" << op << "\t" << command << endl; stdx::lock_guard<SimpleMutex> lk(_lock); if ((command || logicalOp == LogicalOp::opQuery) && ns == _lastDropped) { @@ -142,10 +140,14 @@ void Top::_record(OperationContext* txn, } } -void Top::collectionDropped(StringData ns) { +void Top::collectionDropped(StringData ns, bool databaseDropped) { stdx::lock_guard<SimpleMutex> lk(_lock); _usage.erase(ns); - _lastDropped = ns.toString(); + if (!databaseDropped) { + // If a collection drop occurred, there will be a subsequent call to record for this + // collection namespace which must be ignored. This does not apply to a database drop. + _lastDropped = ns.toString(); + } } void Top::cloneMap(Top::UsageMap& out) const { diff --git a/src/mongo/db/stats/top.h b/src/mongo/db/stats/top.h index a7d543e0ae0..c613a624e4d 100644 --- a/src/mongo/db/stats/top.h +++ b/src/mongo/db/stats/top.h @@ -99,7 +99,7 @@ public: void cloneMap(UsageMap& out) const; - void collectionDropped(StringData ns); + void collectionDropped(StringData ns, bool databaseDropped = false); /** * Appends the collection-level latency statistics |
