diff options
author | Geert Bosch <geert@mongodb.com> | 2016-02-26 20:00:32 -0500 |
---|---|---|
committer | Geert Bosch <geert@mongodb.com> | 2016-03-11 07:39:38 -0500 |
commit | 7ed2327abb1168d7f89e3cd05c8740eee20dcdde (patch) | |
tree | 566797ce7b4491de3bfab8b548c006b29dfc00ef | |
parent | 81e73bae1413668fc53183ac693d87341561dc0f (diff) | |
download | mongo-7ed2327abb1168d7f89e3cd05c8740eee20dcdde.tar.gz |
SERVER-21681: Use cursor statistics to derive index sizes for inMemory
-rw-r--r-- | src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp index 52fff5ffdd8..de36010d7a2 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp @@ -391,7 +391,41 @@ Status WiredTigerIndex::touch(OperationContext* txn) const { long long WiredTigerIndex::getSpaceUsedBytes(OperationContext* txn) const { - WiredTigerSession* session = WiredTigerRecoveryUnit::get(txn)->getSession(txn); + auto ru = WiredTigerRecoveryUnit::get(txn); + WiredTigerSession* session = ru->getSession(txn); + + if (ru->getSessionCache()->isEphemeral()) { + // For ephemeral case, use cursor statistics + const auto statsUri = "statistics:" + uri(); + + // Helper function to retrieve stats and check for errors + auto getStats = [&](int key) -> int64_t { + StatusWith<int64_t> result = WiredTigerUtil::getStatisticsValueAs<int64_t>( + session->getSession(), statsUri, "statistics=(fast)", key); + if (!result.isOK()) { + if (result.getStatus().code() == ErrorCodes::CursorNotFound) + return 0; // ident gone, so return 0 + + uassertStatusOK(result.getStatus()); + } + return result.getValue(); + }; + + auto inserts = getStats(WT_STAT_DSRC_CURSOR_INSERT); + auto removes = getStats(WT_STAT_DSRC_CURSOR_REMOVE); + auto insertBytes = getStats(WT_STAT_DSRC_CURSOR_INSERT_BYTES); + + if (inserts == 0 or removes >= inserts) + return 0; + + // Rough approximation of index size as average entry size times number of entries. + // May be off if key sizes change significantly over the life time of the collection, + // but is the best we can do currrently with the statistics available. + auto bytesPerEntry = (insertBytes + inserts - 1) / inserts; // round up + auto numEntries = inserts - removes; + return numEntries * bytesPerEntry; + } + return static_cast<long long>(WiredTigerUtil::getIdentSize(session->getSession(), _uri)); } |