summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYu Jin Kang Park <yujin.kang@mongodb.com>2022-03-16 10:34:26 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-03-16 11:28:15 +0000
commit539165defa601981015f0e89eba9a0d957c3b031 (patch)
treeb8f0129bc044b9829d6ef767ef3234d2c5ee672a
parent6184015b89d61a890d11f682d172c8473b60b513 (diff)
downloadmongo-539165defa601981015f0e89eba9a0d957c3b031.tar.gz
SERVER-63255 Fix dbStats output for non-existing database to match existing database output fields
-rw-r--r--jstests/core/dbstats.js13
-rw-r--r--src/mongo/db/catalog/database_impl.cpp2
-rw-r--r--src/mongo/db/commands/dbcommands.cpp26
3 files changed, 32 insertions, 9 deletions
diff --git a/jstests/core/dbstats.js b/jstests/core/dbstats.js
index a7cbd9c6bb8..559b6c11774 100644
--- a/jstests/core/dbstats.js
+++ b/jstests/core/dbstats.js
@@ -84,4 +84,17 @@ if (!isMongoS) {
assert.eq(2, dbStats.collections, tojson(dbStats)); // testColl + system.views
assert.eq(1, dbStats.views, tojson(dbStats));
}
+
+// Check that the output for non-existing database and the output for empty database
+// have the same fields.
+const testEmptyAndNonExistingDB = db.getSiblingDB(jsTestName() + "_non_existing_and_empty");
+testEmptyAndNonExistingDB.dropDatabase();
+
+const statsNonExistingDB = testEmptyAndNonExistingDB.runCommand({dbStats: 1, freeStorage: 1});
+testEmptyAndNonExistingDB.runCommand({create: "test_empty_collection"});
+
+const statsEmptyDB = testEmptyAndNonExistingDB.runCommand({dbStats: 1, freeStorage: 1});
+assert.sameMembers(Object.keys(statsNonExistingDB),
+ Object.keys(statsEmptyDB),
+ "dbStats for non-existing and empty dbs should return the same fields");
})();
diff --git a/src/mongo/db/catalog/database_impl.cpp b/src/mongo/db/catalog/database_impl.cpp
index 5e092f5d5b1..c46c8f76235 100644
--- a/src/mongo/db/catalog/database_impl.cpp
+++ b/src/mongo/db/catalog/database_impl.cpp
@@ -369,6 +369,8 @@ void DatabaseImpl::getStats(OperationContext* opCtx,
return true;
});
+ // Make sure that the same fields are returned for non-existing dbs
+ // in `DBStats::errmsgRun`
output->appendNumber("collections", nCollections);
output->appendNumber("views", nViews);
output->appendNumber("objects", objects);
diff --git a/src/mongo/db/commands/dbcommands.cpp b/src/mongo/db/commands/dbcommands.cpp
index 4a2caafb16d..2267c619692 100644
--- a/src/mongo/db/commands/dbcommands.cpp
+++ b/src/mongo/db/commands/dbcommands.cpp
@@ -663,22 +663,30 @@ public:
Database* db = autoDb.getDb();
if (!db) {
- // TODO: This preserves old behaviour where we used to create an empty database
- // metadata even when the database is accessed for read. Without this several
- // unit-tests will fail, which are fairly easy to fix. If backwards compatibility
- // is not needed for the missing DB case, we can just do the same that's done in
- // CollectionStats.
+ // This preserves old behavior where we used to create an empty database even when the
+ // database was accessed for a read. Changing this behavior would impact users that have
+ // learned to depend on it, so we continue to support it. Ensure that these fields match
+ // exactly the fields in `DatabaseImpl::getStats`.
+
result.appendNumber("collections", 0);
result.appendNumber("views", 0);
result.appendNumber("objects", 0);
result.append("avgObjSize", 0);
result.appendNumber("dataSize", 0);
result.appendNumber("storageSize", 0);
- result.appendNumber("totalSize", 0);
- result.appendNumber("indexes", 0);
- result.appendNumber("indexSize", 0);
+ if (includeFreeStorage) {
+ result.appendNumber("freeStorageSize", 0);
+ result.appendNumber("indexes", 0);
+ result.appendNumber("indexSize", 0);
+ result.appendNumber("indexFreeStorageSize", 0);
+ result.appendNumber("totalSize", 0);
+ result.appendNumber("totalFreeStorageSize", 0);
+ } else {
+ result.appendNumber("indexes", 0);
+ result.appendNumber("indexSize", 0);
+ result.appendNumber("totalSize", 0);
+ }
result.appendNumber("scaleFactor", scale);
- result.appendNumber("fileSize", 0);
if (!getGlobalServiceContext()->getStorageEngine()->isEphemeral()) {
result.appendNumber("fsUsedSize", 0);
result.appendNumber("fsTotalSize", 0);