From f4600bb330c1ed7975fe4ec7562dbda40efb2d8f Mon Sep 17 00:00:00 2001 From: James Wahlin Date: Wed, 14 Sep 2016 14:45:45 -0400 Subject: SERVER-25948 dbStats should indicate number of views --- jstests/core/dbstats.js | 91 +++++++++++++++++++++++++++ src/mongo/db/catalog/database.cpp | 10 ++- src/mongo/db/commands/dbcommands.cpp | 1 + src/mongo/s/commands/cluster_db_stats_cmd.cpp | 3 +- 4 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 jstests/core/dbstats.js diff --git a/jstests/core/dbstats.js b/jstests/core/dbstats.js new file mode 100644 index 00000000000..1547f06fd8a --- /dev/null +++ b/jstests/core/dbstats.js @@ -0,0 +1,91 @@ +// Confirms that the dbStats command returns expected content. + +(function() { + "use strict"; + + function serverIsMongos() { + const res = db.runCommand("ismaster"); + assert.commandWorked(res); + return res.msg === "isdbgrid"; + } + + const isMongoS = serverIsMongos(); + const isMMAPv1 = jsTest.options().storageEngine === "mmapv1"; + + let testDB = db.getSiblingDB("dbstats_js"); + assert.commandWorked(testDB.dropDatabase()); + + let coll = testDB["testColl"]; + assert.commandWorked(coll.createIndex({x: 1})); + const doc = {_id: 1, x: 1}; + assert.writeOK(coll.insert(doc)); + + let dbStats = testDB.runCommand({dbStats: 1}); + assert.commandWorked(dbStats); + + if (isMMAPv1) { + if (isMongoS) { + // When this test is run against mongoS with the mmapV1 storage engine the 'objects' and + // 'indexes' counts will vary depending on whether 'testColl' is sharded and on the # of + // shards (due to inclusion of system.indexes & system.namespaces counts). + assert(dbStats.hasOwnProperty("objects"), tojson(dbStats)); + assert(dbStats.hasOwnProperty("indexes"), tojson(dbStats)); + } else { + assert.eq(7, + dbStats.objects, + tojson(dbStats)); // Includes testColl, system.indexes & system.namespaces + assert.eq(2, dbStats.indexes, tojson(dbStats)); + } + // 'dataSize' and 'avgObjSize' include document padding space under MMAPv1. + assert(dbStats.hasOwnProperty("dataSize"), tojson(dbStats)); + assert(dbStats.hasOwnProperty("avgObjSize"), tojson(dbStats)); + } else { + assert.eq(1, dbStats.objects, tojson(dbStats)); // Includes testColl only + const dataSize = Object.bsonsize(doc); + assert.eq(dataSize, dbStats.avgObjSize, tojson(dbStats)); + assert.eq(dataSize, dbStats.dataSize, tojson(dbStats)); + + // Index count will vary on mongoS if an additional index is needed to support sharding. + if (isMongoS) { + assert(dbStats.hasOwnProperty("indexes"), tojson(dbStats)); + } else { + assert.eq(2, dbStats.indexes, tojson(dbStats)); + } + } + + assert(dbStats.hasOwnProperty("storageSize"), tojson(dbStats)); + assert(dbStats.hasOwnProperty("numExtents"), tojson(dbStats)); + assert(dbStats.hasOwnProperty("indexSize"), tojson(dbStats)); + + // Confirm extentFreeList field existence. Displayed for mongoD running MMAPv1 and for mongoS + // regardless of storage engine. + if (isMMAPv1 || isMongoS) { + assert(dbStats.hasOwnProperty("extentFreeList"), tojson(dbStats)); + assert(dbStats.extentFreeList.hasOwnProperty("num"), tojson(dbStats)); + assert(dbStats.extentFreeList.hasOwnProperty("totalSize"), tojson(dbStats)); + } + + // Confirm collection and view counts on mongoD + if (!isMongoS) { + assert.eq(testDB.getName(), dbStats.db, tojson(dbStats)); + + // We wait to add a view until this point as it allows more exact testing of avgObjSize for + // WiredTiger above. Having more than 1 document would require floating point comparison. + assert.commandWorked(testDB.createView("testView", "testColl", [])); + + dbStats = testDB.runCommand({dbStats: 1}); + assert.commandWorked(dbStats); + + if (isMMAPv1) { + assert.eq( + 4, + dbStats.collections, + tojson(dbStats)); // testColl + system.views + system.indexes + system.namespaces + } else { + assert.eq(2, dbStats.collections, tojson(dbStats)); // testColl + system.views + } + + assert.eq(1, dbStats.views, tojson(dbStats)); + } + +})(); diff --git a/src/mongo/db/catalog/database.cpp b/src/mongo/db/catalog/database.cpp index 005a4f23abf..85d3eab7f34 100644 --- a/src/mongo/db/catalog/database.cpp +++ b/src/mongo/db/catalog/database.cpp @@ -322,7 +322,8 @@ void Database::getStats(OperationContext* opCtx, BSONObjBuilder* output, double list collections; _dbEntry->getCollectionNamespaces(&collections); - long long ncollections = 0; + long long nCollections = 0; + long long nViews = 0; long long objects = 0; long long size = 0; long long storageSize = 0; @@ -337,7 +338,7 @@ void Database::getStats(OperationContext* opCtx, BSONObjBuilder* output, double if (!collection) continue; - ncollections += 1; + nCollections += 1; objects += collection->numRecords(opCtx); size += collection->dataSize(opCtx); @@ -349,7 +350,10 @@ void Database::getStats(OperationContext* opCtx, BSONObjBuilder* output, double indexSize += collection->getIndexSize(opCtx); } - output->appendNumber("collections", ncollections); + getViewCatalog()->iterate(opCtx, [&](const ViewDefinition& view) { nViews += 1; }); + + output->appendNumber("collections", nCollections); + output->appendNumber("views", nViews); output->appendNumber("objects", objects); output->append("avgObjSize", objects == 0 ? 0 : double(size) / double(objects)); output->appendNumber("dataSize", size / scale); diff --git a/src/mongo/db/commands/dbcommands.cpp b/src/mongo/db/commands/dbcommands.cpp index 9720775b0cf..f73166e42fa 100644 --- a/src/mongo/db/commands/dbcommands.cpp +++ b/src/mongo/db/commands/dbcommands.cpp @@ -1038,6 +1038,7 @@ public: // is not needed for the missing DB case, we can just do the same that's done in // CollectionStats. result.appendNumber("collections", 0); + result.appendNumber("views", 0); result.appendNumber("objects", 0); result.append("avgObjSize", 0); result.appendNumber("dataSize", 0); diff --git a/src/mongo/s/commands/cluster_db_stats_cmd.cpp b/src/mongo/s/commands/cluster_db_stats_cmd.cpp index 4661cac2b7b..1546fe4482e 100644 --- a/src/mongo/s/commands/cluster_db_stats_cmd.cpp +++ b/src/mongo/s/commands/cluster_db_stats_cmd.cpp @@ -83,8 +83,7 @@ public: } } - // TODO: need to find a good way to get this - // result.appendNumber( "collections" , ncollections ); + // TODO SERVER-26110: Add aggregated 'collections' and 'views' metrics. output.appendNumber("objects", objects); // avgObjSize on mongod is not scaled based on the argument to db.stats(), so we use -- cgit v1.2.1