diff options
author | Robert Guo <robert.guo@10gen.com> | 2016-01-27 11:04:46 -0500 |
---|---|---|
committer | Robert Guo <robert.guo@10gen.com> | 2016-01-27 11:04:46 -0500 |
commit | c9fd26cd87a30b9db88dac42a1d2c480fc8bc308 (patch) | |
tree | e9ec09bb57385b8023bf41141d544a843676882b | |
parent | ffe490c80ecea03b02a0b20d55915c4efd2805e8 (diff) | |
download | mongo-c9fd26cd87a30b9db88dac42a1d2c480fc8bc308.tar.gz |
SERVER-22156 dbhash should cache hashes at the collection level
-rw-r--r-- | src/mongo/db/commands/dbhash.cpp | 32 | ||||
-rw-r--r-- | src/mongo/db/commands/dbhash.h | 8 |
2 files changed, 27 insertions, 13 deletions
diff --git a/src/mongo/db/commands/dbhash.cpp b/src/mongo/db/commands/dbhash.cpp index aac2937b8f3..c38919a61db 100644 --- a/src/mongo/db/commands/dbhash.cpp +++ b/src/mongo/db/commands/dbhash.cpp @@ -39,6 +39,7 @@ #include "mongo/db/client.h" #include "mongo/db/commands.h" #include "mongo/db/db_raii.h" +#include "mongo/db/namespace_string.h" #include "mongo/db/query/internal_plans.h" #include "mongo/util/log.h" #include "mongo/util/md5.hpp" @@ -57,7 +58,8 @@ DBHashCmd dbhashCmd; void logOpForDbHash(OperationContext* txn, const char* ns) { - dbhashCmd.wipeCacheForCollection(txn, ns); + NamespaceString nsString(ns); + dbhashCmd.wipeCacheForCollection(txn, nsString); } // ---- @@ -78,9 +80,11 @@ std::string DBHashCmd::hashCollection(OperationContext* opCtx, bool* fromCache) { stdx::unique_lock<stdx::mutex> cachedHashedLock(_cachedHashedMutex, stdx::defer_lock); - if (isCachable(fullCollectionName)) { + NamespaceString ns(fullCollectionName); + + if (isCachable(ns)) { cachedHashedLock.lock(); - string hash = _cachedHashed[fullCollectionName]; + string hash = _cachedHashed[ns.db().toString()][ns.coll().toString()]; if (hash.size() > 0) { *fromCache = true; return hash; @@ -132,7 +136,7 @@ std::string DBHashCmd::hashCollection(OperationContext* opCtx, string hash = digestToString(d); if (cachedHashedLock.owns_lock()) { - _cachedHashed[fullCollectionName] = hash; + _cachedHashed[ns.db().toString()][ns.coll().toString()] = hash; } return hash; @@ -217,18 +221,26 @@ bool DBHashCmd::run(OperationContext* txn, return 1; } -void DBHashCmd::wipeCacheForCollection(OperationContext* txn, StringData ns) { +void DBHashCmd::wipeCacheForCollection(OperationContext* txn, const NamespaceString& ns) { if (!isCachable(ns)) return; - std::string nsOwned = ns.toString(); - txn->recoveryUnit()->onCommit([this, txn, nsOwned] { + txn->recoveryUnit()->onCommit([this, txn, ns] { stdx::lock_guard<stdx::mutex> lk(_cachedHashedMutex); - _cachedHashed.erase(nsOwned); + if (ns.isCommand()) { + // The <dbName>.$cmd namespace can represent a command that + // modifies the entire database, e.g. dropDatabase, so we remove + // the cached entries for all collections in the database. + _cachedHashed.erase(ns.db().toString()); + } else { + _cachedHashed[ns.db().toString()].erase(ns.coll().toString()); + } + + }); } -bool DBHashCmd::isCachable(StringData ns) const { - return ns.startsWith("config."); +bool DBHashCmd::isCachable(const NamespaceString& ns) const { + return ns.isConfigDB(); } } diff --git a/src/mongo/db/commands/dbhash.h b/src/mongo/db/commands/dbhash.h index 8b566f98327..8fc964b6b12 100644 --- a/src/mongo/db/commands/dbhash.h +++ b/src/mongo/db/commands/dbhash.h @@ -35,6 +35,8 @@ namespace mongo { +class NamespaceString; + void logOpForDbHash(OperationContext* txn, const char* ns); class DBHashCmd : public Command { @@ -58,7 +60,7 @@ public: std::string& errmsg, BSONObjBuilder& result); - void wipeCacheForCollection(OperationContext* txn, StringData ns); + void wipeCacheForCollection(OperationContext* txn, const NamespaceString& ns); private: /** @@ -66,14 +68,14 @@ private: */ class DBHashLogOpHandler; - bool isCachable(StringData ns) const; + bool isCachable(const NamespaceString& ns) const; std::string hashCollection(OperationContext* opCtx, Database* db, const std::string& fullCollectionName, bool* fromCache); - std::map<std::string, std::string> _cachedHashed; + std::map<std::string, std::map<std::string, std::string>> _cachedHashed; stdx::mutex _cachedHashedMutex; }; } |