From fca4f3f22ef5073031c3bf9352ea4eb0ade30e6c Mon Sep 17 00:00:00 2001 From: Colin Stolley Date: Tue, 27 Sep 2022 14:20:58 +0000 Subject: SERVER-69845: Implement column store size on disk. --- .../storage/wiredtiger/wiredtiger_column_store.cpp | 21 ++++++++----- .../db/storage/wiredtiger/wiredtiger_index.cpp | 35 ++-------------------- .../db/storage/wiredtiger/wiredtiger_util.cpp | 31 +++++++++++++++++++ src/mongo/db/storage/wiredtiger/wiredtiger_util.h | 2 ++ 4 files changed, 49 insertions(+), 40 deletions(-) diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_column_store.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_column_store.cpp index b3aef5dc0a6..1caf0b67158 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_column_store.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_column_store.cpp @@ -426,17 +426,22 @@ bool WiredTigerColumnStore::isEmpty(OperationContext* opCtx) { } long long WiredTigerColumnStore::getSpaceUsedBytes(OperationContext* opCtx) const { - // TODO: SERVER-65980. - // For now we just return this so that tests can successfully obtain collection-level stats on - // a collection with a columnstore index. - return 27017; + dassert(opCtx->lockState()->isReadLocked()); + auto ru = WiredTigerRecoveryUnit::get(opCtx); + WT_SESSION* s = ru->getSession()->getSession(); + + if (ru->getSessionCache()->isEphemeral()) { + return static_cast(WiredTigerUtil::getEphemeralIdentSize(s, _uri)); + } + return static_cast(WiredTigerUtil::getIdentSize(s, _uri)); } long long WiredTigerColumnStore::getFreeStorageBytes(OperationContext* opCtx) const { - // TODO: SERVER-65980. - // For now we just fake this so that tests can successfully obtain collection-level stats on a - // collection with a columnstore index. - return 27017; + dassert(opCtx->lockState()->isReadLocked()); + auto ru = WiredTigerRecoveryUnit::get(opCtx); + WiredTigerSession* session = ru->getSession(); + + return static_cast(WiredTigerUtil::getIdentReuseSize(session->getSession(), _uri)); } Status WiredTigerColumnStore::compact(OperationContext* opCtx) { diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp index 8e5df7fdf82..ce6f7bb0a84 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp @@ -509,41 +509,12 @@ void WiredTigerIndex::printIndexEntryMetadata(OperationContext* opCtx, long long WiredTigerIndex::getSpaceUsedBytes(OperationContext* opCtx) const { dassert(opCtx->lockState()->isReadLocked()); auto ru = WiredTigerRecoveryUnit::get(opCtx); - WiredTigerSession* session = ru->getSession(); + WT_SESSION* s = ru->getSession()->getSession(); 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 { - auto result = WiredTigerUtil::getStatisticsValue( - 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 || 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(WiredTigerUtil::getEphemeralIdentSize(s, _uri)); } - - return static_cast(WiredTigerUtil::getIdentSize(session->getSession(), _uri)); + return static_cast(WiredTigerUtil::getIdentSize(s, _uri)); } long long WiredTigerIndex::getFreeStorageBytes(OperationContext* opCtx) const { diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp index a3a9ec4109d..ab4623f5441 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp @@ -500,6 +500,37 @@ int64_t WiredTigerUtil::getIdentSize(WT_SESSION* s, const std::string& uri) { return result.getValue(); } +int64_t WiredTigerUtil::getEphemeralIdentSize(WT_SESSION* s, const std::string& uri) { + // 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 { + auto result = getStatisticsValue(s, 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 || 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; +} + int64_t WiredTigerUtil::getIdentReuseSize(WT_SESSION* s, const std::string& uri) { auto result = WiredTigerUtil::getStatisticsValue( s, "statistics:" + uri, "statistics=(fast)", WT_STAT_DSRC_BLOCK_REUSE_BYTES); diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_util.h b/src/mongo/db/storage/wiredtiger/wiredtiger_util.h index dbca93e6bc6..b17dfcb6f85 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_util.h +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_util.h @@ -272,6 +272,8 @@ public: const std::string& config, int statisticsKey); + static int64_t getEphemeralIdentSize(WT_SESSION* s, const std::string& uri); + static int64_t getIdentSize(WT_SESSION* s, const std::string& uri); /** -- cgit v1.2.1