summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/mongo/db/SConscript12
-rw-r--r--src/mongo/db/key_generator.cpp (renamed from src/mongo/db/keys_collection_cache_reader_and_updater.cpp)27
-rw-r--r--src/mongo/db/key_generator.h (renamed from src/mongo/db/keys_collection_cache_reader_and_updater.h)23
-rw-r--r--src/mongo/db/key_generator_update_test.cpp (renamed from src/mongo/db/keys_collection_cache_reader_and_updater_test.cpp)100
-rw-r--r--src/mongo/db/keys_collection_cache.cpp (renamed from src/mongo/db/keys_collection_cache_reader.cpp)16
-rw-r--r--src/mongo/db/keys_collection_cache.h40
-rw-r--r--src/mongo/db/keys_collection_cache_reader.h73
-rw-r--r--src/mongo/db/keys_collection_cache_test.cpp (renamed from src/mongo/db/keys_collection_cache_reader_test.cpp)64
-rw-r--r--src/mongo/db/keys_collection_manager.cpp15
-rw-r--r--src/mongo/db/keys_collection_manager.h6
10 files changed, 128 insertions, 248 deletions
diff --git a/src/mongo/db/SConscript b/src/mongo/db/SConscript
index 85edb77edbc..6e9ac5f0aaa 100644
--- a/src/mongo/db/SConscript
+++ b/src/mongo/db/SConscript
@@ -1364,8 +1364,8 @@ env.Library(
target='keys_collection_manager',
source=[
'keys_collection_manager.cpp',
- 'keys_collection_cache_reader.cpp',
- 'keys_collection_cache_reader_and_updater.cpp',
+ 'keys_collection_cache.cpp',
+ 'key_generator.cpp',
],
LIBDEPS=[
'logical_clock',
@@ -1494,9 +1494,9 @@ env.CppUnitTest(
)
env.CppUnitTest(
- target='keys_collection_cache_reader_test',
+ target='keys_collection_cache_test',
source=[
- 'keys_collection_cache_reader_test.cpp',
+ 'keys_collection_cache_test.cpp',
],
LIBDEPS=[
'keys_collection_manager',
@@ -1506,9 +1506,9 @@ env.CppUnitTest(
)
env.CppUnitTest(
- target='keys_collection_cache_reader_and_updater_test',
+ target='key_generator_update_test',
source=[
- 'keys_collection_cache_reader_and_updater_test.cpp',
+ 'key_generator_update_test.cpp',
],
LIBDEPS=[
'keys_collection_manager',
diff --git a/src/mongo/db/keys_collection_cache_reader_and_updater.cpp b/src/mongo/db/key_generator.cpp
index 349c4f36f3c..588380117dd 100644
--- a/src/mongo/db/keys_collection_cache_reader_and_updater.cpp
+++ b/src/mongo/db/key_generator.cpp
@@ -28,7 +28,7 @@
#include "mongo/platform/basic.h"
-#include "mongo/db/keys_collection_cache_reader_and_updater.h"
+#include "mongo/db/key_generator.h"
#include "mongo/client/read_preference.h"
#include "mongo/db/keys_collection_client.h"
@@ -68,15 +68,12 @@ LogicalTime addSeconds(const LogicalTime& logicalTime, const Seconds& seconds) {
} // unnamed namespace
-KeysCollectionCacheReaderAndUpdater::KeysCollectionCacheReaderAndUpdater(
- std::string purpose, KeysCollectionClient* client, Seconds keyValidForInterval)
- : KeysCollectionCacheReader(purpose, client),
- _client(client),
- _purpose(std::move(purpose)),
- _keyValidForInterval(keyValidForInterval) {}
+KeyGenerator::KeyGenerator(std::string purpose,
+ KeysCollectionClient* client,
+ Seconds keyValidForInterval)
+ : _client(client), _purpose(std::move(purpose)), _keyValidForInterval(keyValidForInterval) {}
-StatusWith<KeysCollectionDocument> KeysCollectionCacheReaderAndUpdater::refresh(
- OperationContext* opCtx) {
+Status KeyGenerator::generateNewKeysIfNeeded(OperationContext* opCtx) {
if (MONGO_FAIL_POINT(disableKeyGeneration)) {
return {ErrorCodes::FailPointEnabled, "key generation disabled"};
@@ -139,16 +136,6 @@ StatusWith<KeysCollectionDocument> KeysCollectionCacheReaderAndUpdater::refresh(
}
}
- return KeysCollectionCacheReader::refresh(opCtx);
-}
-
-StatusWith<KeysCollectionDocument> KeysCollectionCacheReaderAndUpdater::getKey(
- const LogicalTime& forThisTime) {
- return KeysCollectionCacheReader::getKey(forThisTime);
-}
-
-StatusWith<KeysCollectionDocument> KeysCollectionCacheReaderAndUpdater::getKeyById(
- long long keyId, const LogicalTime& forThisTime) {
- return KeysCollectionCacheReader::getKeyById(keyId, forThisTime);
+ return Status::OK();
}
} // namespace mongo
diff --git a/src/mongo/db/keys_collection_cache_reader_and_updater.h b/src/mongo/db/key_generator.h
index fc079ae4334..99b387e3771 100644
--- a/src/mongo/db/keys_collection_cache_reader_and_updater.h
+++ b/src/mongo/db/key_generator.h
@@ -30,38 +30,31 @@
#include <string>
-#include "mongo/db/keys_collection_cache_reader.h"
#include "mongo/util/duration.h"
namespace mongo {
+class OperationContext;
class KeysCollectionClient;
/**
- * Keeps a local cache of the keys with the ability to refresh. The refresh method also makes sure
- * that there will be valid keys available to sign the current cluster time and there will be
- * another key ready after the current key expires.
+ * Checks to make sure that there will be valid keys available to sign the current cluster time,
+ * and that there will be another key ready after the current key expires. Generates keys if they
+ * are necessary.
*
* Assumptions and limitations:
* - assumes that user does not manually update the keys collection.
* - assumes that current process is the config primary.
*/
-class KeysCollectionCacheReaderAndUpdater : public KeysCollectionCacheReader {
+class KeyGenerator {
public:
- KeysCollectionCacheReaderAndUpdater(std::string purpose,
- KeysCollectionClient* client,
- Seconds keyValidForInterval);
- ~KeysCollectionCacheReaderAndUpdater() = default;
+ KeyGenerator(std::string purpose, KeysCollectionClient* client, Seconds keyValidForInterval);
+ ~KeyGenerator() = default;
/**
* Check if there are new documents expiresAt > latestKeyDoc.expiresAt.
*/
- StatusWith<KeysCollectionDocument> refresh(OperationContext* opCtx) override;
-
- StatusWith<KeysCollectionDocument> getKey(const LogicalTime& forThisTime) override;
-
- StatusWith<KeysCollectionDocument> getKeyById(long long keyId,
- const LogicalTime& forThisTime) override;
+ Status generateNewKeysIfNeeded(OperationContext* opCtx);
private:
KeysCollectionClient* const _client;
diff --git a/src/mongo/db/keys_collection_cache_reader_and_updater_test.cpp b/src/mongo/db/key_generator_update_test.cpp
index aa2cf7b6b5c..79b26326f18 100644
--- a/src/mongo/db/keys_collection_cache_reader_and_updater_test.cpp
+++ b/src/mongo/db/key_generator_update_test.cpp
@@ -32,7 +32,7 @@
#include <string>
#include "mongo/db/jsobj.h"
-#include "mongo/db/keys_collection_cache_reader_and_updater.h"
+#include "mongo/db/key_generator.h"
#include "mongo/db/keys_collection_client_sharded.h"
#include "mongo/db/keys_collection_document.h"
#include "mongo/db/logical_clock.h"
@@ -48,7 +48,7 @@
namespace mongo {
-class CacheUpdaterTest : public ConfigServerTestFixture {
+class KeyGeneratorUpdateTest : public ConfigServerTestFixture {
protected:
void setUp() override {
ConfigServerTestFixture::setUp();
@@ -73,21 +73,14 @@ private:
std::unique_ptr<KeysCollectionClient> _catalogClient;
};
-TEST_F(CacheUpdaterTest, ShouldCreate2KeysFromEmpty) {
- KeysCollectionCacheReaderAndUpdater updater("dummy", catalogClient(), Seconds(5));
+TEST_F(KeyGeneratorUpdateTest, ShouldCreate2KeysFromEmpty) {
+ KeyGenerator generator("dummy", catalogClient(), Seconds(5));
const LogicalTime currentTime(LogicalTime(Timestamp(100, 2)));
LogicalClock::get(operationContext())->setClusterTimeFromTrustedSource(currentTime);
- {
- auto keyStatus = updater.refresh(operationContext());
- ASSERT_OK(keyStatus.getStatus());
-
- const auto key = keyStatus.getValue();
- ASSERT_EQ(currentTime.asTimestamp().asLL() + 1, key.getKeyId());
- ASSERT_EQ("dummy", key.getPurpose());
- ASSERT_EQ(Timestamp(110, 0), key.getExpiresAt().asTimestamp());
- }
+ auto generateStatus = generator.generateNewKeysIfNeeded(operationContext());
+ ASSERT_OK(generateStatus);
auto allKeys = getKeys(operationContext());
@@ -106,20 +99,20 @@ TEST_F(CacheUpdaterTest, ShouldCreate2KeysFromEmpty) {
ASSERT_NE(key1.getKey(), key2.getKey());
}
-TEST_F(CacheUpdaterTest, ShouldPropagateWriteError) {
- KeysCollectionCacheReaderAndUpdater updater("dummy", catalogClient(), Seconds(5));
+TEST_F(KeyGeneratorUpdateTest, ShouldPropagateWriteError) {
+ KeyGenerator generator("dummy", catalogClient(), Seconds(5));
const LogicalTime currentTime(LogicalTime(Timestamp(100, 2)));
LogicalClock::get(operationContext())->setClusterTimeFromTrustedSource(currentTime);
FailPointEnableBlock failWriteBlock("failCollectionInserts");
- auto keyStatus = updater.refresh(operationContext());
- ASSERT_EQ(ErrorCodes::FailPointEnabled, keyStatus.getStatus());
+ auto generateStatus = generator.generateNewKeysIfNeeded(operationContext());
+ ASSERT_EQ(ErrorCodes::FailPointEnabled, generateStatus);
}
-TEST_F(CacheUpdaterTest, ShouldCreateAnotherKeyIfOnlyOneKeyExists) {
- KeysCollectionCacheReaderAndUpdater updater("dummy", catalogClient(), Seconds(5));
+TEST_F(KeyGeneratorUpdateTest, ShouldCreateAnotherKeyIfOnlyOneKeyExists) {
+ KeyGenerator generator("dummy", catalogClient(), Seconds(5));
LogicalClock::get(operationContext())
->setClusterTimeFromTrustedSource(LogicalTime(Timestamp(100, 2)));
@@ -142,15 +135,8 @@ TEST_F(CacheUpdaterTest, ShouldCreateAnotherKeyIfOnlyOneKeyExists) {
auto currentTime = LogicalClock::get(operationContext())->getClusterTime();
- {
- auto keyStatus = updater.refresh(operationContext());
- ASSERT_OK(keyStatus.getStatus());
-
- const auto key = keyStatus.getValue();
- ASSERT_EQ(currentTime.asTimestamp().asLL(), key.getKeyId());
- ASSERT_EQ("dummy", key.getPurpose());
- ASSERT_EQ(Timestamp(110, 0), key.getExpiresAt().asTimestamp());
- }
+ auto generateStatus = generator.generateNewKeysIfNeeded(operationContext());
+ ASSERT_OK(generateStatus);
{
auto allKeys = getKeys(operationContext());
@@ -172,8 +158,8 @@ TEST_F(CacheUpdaterTest, ShouldCreateAnotherKeyIfOnlyOneKeyExists) {
}
}
-TEST_F(CacheUpdaterTest, ShouldCreateAnotherKeyIfNoValidKeyAfterCurrent) {
- KeysCollectionCacheReaderAndUpdater updater("dummy", catalogClient(), Seconds(5));
+TEST_F(KeyGeneratorUpdateTest, ShouldCreateAnotherKeyIfNoValidKeyAfterCurrent) {
+ KeyGenerator generator("dummy", catalogClient(), Seconds(5));
LogicalClock::get(operationContext())
->setClusterTimeFromTrustedSource(LogicalTime(Timestamp(108, 2)));
@@ -206,16 +192,8 @@ TEST_F(CacheUpdaterTest, ShouldCreateAnotherKeyIfNoValidKeyAfterCurrent) {
auto currentTime = LogicalClock::get(operationContext())->getClusterTime();
- {
- auto keyStatus = updater.refresh(operationContext());
- ASSERT_OK(keyStatus.getStatus());
-
- const auto key = keyStatus.getValue();
- ASSERT_EQ(currentTime.asTimestamp().asLL(), key.getKeyId());
- ASSERT_EQ("dummy", key.getPurpose());
- ASSERT_EQ(Timestamp(115, 0), key.getExpiresAt().asTimestamp());
- }
-
+ auto generateStatus = generator.generateNewKeysIfNeeded(operationContext());
+ ASSERT_OK(generateStatus);
auto allKeys = getKeys(operationContext());
@@ -263,8 +241,8 @@ TEST_F(CacheUpdaterTest, ShouldCreateAnotherKeyIfNoValidKeyAfterCurrent) {
}
}
-TEST_F(CacheUpdaterTest, ShouldCreate2KeysIfAllKeysAreExpired) {
- KeysCollectionCacheReaderAndUpdater updater("dummy", catalogClient(), Seconds(5));
+TEST_F(KeyGeneratorUpdateTest, ShouldCreate2KeysIfAllKeysAreExpired) {
+ KeyGenerator generator("dummy", catalogClient(), Seconds(5));
LogicalClock::get(operationContext())
->setClusterTimeFromTrustedSource(LogicalTime(Timestamp(120, 2)));
@@ -297,15 +275,8 @@ TEST_F(CacheUpdaterTest, ShouldCreate2KeysIfAllKeysAreExpired) {
auto currentTime = LogicalClock::get(operationContext())->getClusterTime();
- {
- auto keyStatus = updater.refresh(operationContext());
- ASSERT_OK(keyStatus.getStatus());
-
- const auto key = keyStatus.getValue();
- ASSERT_EQ(currentTime.asTimestamp().asLL() + 1, key.getKeyId());
- ASSERT_EQ("dummy", key.getPurpose());
- ASSERT_EQ(Timestamp(130, 0), key.getExpiresAt().asTimestamp());
- }
+ auto generateStatus = generator.generateNewKeysIfNeeded(operationContext());
+ ASSERT_OK(generateStatus);
auto allKeys = getKeys(operationContext());
@@ -367,11 +338,11 @@ TEST_F(CacheUpdaterTest, ShouldCreate2KeysIfAllKeysAreExpired) {
}
}
-TEST_F(CacheUpdaterTest, ShouldNotCreateNewKeyIfThereAre2UnexpiredKeys) {
- KeysCollectionCacheReaderAndUpdater updater("dummy", catalogClient(), Seconds(5));
+TEST_F(KeyGeneratorUpdateTest, ShouldNotCreateNewKeyIfThereAre2UnexpiredKeys) {
+ KeyGenerator generator("dummy", catalogClient(), Seconds(5));
- LogicalClock::get(operationContext())
- ->setClusterTimeFromTrustedSource(LogicalTime(Timestamp(100, 2)));
+ const LogicalTime currentTime(LogicalTime(Timestamp(100, 2)));
+ LogicalClock::get(operationContext())->setClusterTimeFromTrustedSource(currentTime);
KeysCollectionDocument origKey1(
1, "dummy", TimeProofService::generateRandomKey(), LogicalTime(Timestamp(105, 0)));
@@ -399,15 +370,8 @@ TEST_F(CacheUpdaterTest, ShouldNotCreateNewKeyIfThereAre2UnexpiredKeys) {
ASSERT_EQ(Timestamp(110, 0), key2.getExpiresAt().asTimestamp());
}
- {
- auto keyStatus = updater.refresh(operationContext());
- ASSERT_OK(keyStatus.getStatus());
-
- const auto key = keyStatus.getValue();
- ASSERT_EQ(2, key.getKeyId());
- ASSERT_EQ("dummy", key.getPurpose());
- ASSERT_EQ(Timestamp(110, 0), key.getExpiresAt().asTimestamp());
- }
+ auto generateStatus = generator.generateNewKeysIfNeeded(operationContext());
+ ASSERT_OK(generateStatus);
auto allKeys = getKeys(operationContext());
@@ -433,8 +397,8 @@ TEST_F(CacheUpdaterTest, ShouldNotCreateNewKeyIfThereAre2UnexpiredKeys) {
}
}
-TEST_F(CacheUpdaterTest, ShouldNotCreateKeysWithDisableKeyGenerationFailPoint) {
- KeysCollectionCacheReaderAndUpdater updater("dummy", catalogClient(), Seconds(5));
+TEST_F(KeyGeneratorUpdateTest, ShouldNotCreateKeysWithDisableKeyGenerationFailPoint) {
+ KeyGenerator generator("dummy", catalogClient(), Seconds(5));
const LogicalTime currentTime(LogicalTime(Timestamp(100, 0)));
LogicalClock::get(operationContext())->setClusterTimeFromTrustedSource(currentTime);
@@ -442,8 +406,8 @@ TEST_F(CacheUpdaterTest, ShouldNotCreateKeysWithDisableKeyGenerationFailPoint) {
{
FailPointEnableBlock failKeyGenerationBlock("disableKeyGeneration");
- auto keyStatus = updater.refresh(operationContext());
- ASSERT_EQ(ErrorCodes::FailPointEnabled, keyStatus.getStatus());
+ auto generateStatus = generator.generateNewKeysIfNeeded(operationContext());
+ ASSERT_EQ(ErrorCodes::FailPointEnabled, generateStatus);
}
auto allKeys = getKeys(operationContext());
diff --git a/src/mongo/db/keys_collection_cache_reader.cpp b/src/mongo/db/keys_collection_cache.cpp
index a05f5c23233..0491d69397f 100644
--- a/src/mongo/db/keys_collection_cache_reader.cpp
+++ b/src/mongo/db/keys_collection_cache.cpp
@@ -28,7 +28,7 @@
#include "mongo/platform/basic.h"
-#include "mongo/db/keys_collection_cache_reader.h"
+#include "mongo/db/keys_collection_cache.h"
#include "mongo/db/keys_collection_client.h"
#include "mongo/db/keys_collection_document.h"
@@ -36,11 +36,10 @@
namespace mongo {
-KeysCollectionCacheReader::KeysCollectionCacheReader(std::string purpose,
- KeysCollectionClient* client)
+KeysCollectionCache::KeysCollectionCache(std::string purpose, KeysCollectionClient* client)
: _purpose(std::move(purpose)), _client(client) {}
-StatusWith<KeysCollectionDocument> KeysCollectionCacheReader::refresh(OperationContext* opCtx) {
+StatusWith<KeysCollectionDocument> KeysCollectionCache::refresh(OperationContext* opCtx) {
LogicalTime newerThanThis;
{
@@ -71,8 +70,8 @@ StatusWith<KeysCollectionDocument> KeysCollectionCacheReader::refresh(OperationC
return _cache.crbegin()->second;
}
-StatusWith<KeysCollectionDocument> KeysCollectionCacheReader::getKeyById(
- long long keyId, const LogicalTime& forThisTime) {
+StatusWith<KeysCollectionDocument> KeysCollectionCache::getKeyById(long long keyId,
+ const LogicalTime& forThisTime) {
stdx::lock_guard<stdx::mutex> lk(_cacheMutex);
for (auto iter = _cache.lower_bound(forThisTime); iter != _cache.cend(); ++iter) {
@@ -89,8 +88,7 @@ StatusWith<KeysCollectionDocument> KeysCollectionCacheReader::getKeyById(
<< keyId};
}
-StatusWith<KeysCollectionDocument> KeysCollectionCacheReader::getKey(
- const LogicalTime& forThisTime) {
+StatusWith<KeysCollectionDocument> KeysCollectionCache::getKey(const LogicalTime& forThisTime) {
stdx::lock_guard<stdx::mutex> lk(_cacheMutex);
auto iter = _cache.upper_bound(forThisTime);
@@ -103,7 +101,7 @@ StatusWith<KeysCollectionDocument> KeysCollectionCacheReader::getKey(
return iter->second;
}
-void KeysCollectionCacheReader::resetCache() {
+void KeysCollectionCache::resetCache() {
// keys that read with non majority readConcern level can be rolled back.
if (!_client->supportsMajorityReads()) {
_cache.clear();
diff --git a/src/mongo/db/keys_collection_cache.h b/src/mongo/db/keys_collection_cache.h
index 1a7f0a4d1e0..091e1906fdc 100644
--- a/src/mongo/db/keys_collection_cache.h
+++ b/src/mongo/db/keys_collection_cache.h
@@ -28,35 +28,47 @@
#pragma once
+#include <map>
+
#include "mongo/base/status_with.h"
#include "mongo/db/keys_collection_document.h"
+#include "mongo/db/operation_context.h"
+#include "mongo/stdx/mutex.h"
namespace mongo {
-class LogicalTime;
-class OperationContext;
+class KeysCollectionClient;
+/**
+ * Keeps a local cache of the keys with the ability to refresh.
+ *
+ * Note: This assumes that user does not manually update the keys collection.
+ */
class KeysCollectionCache {
public:
- virtual ~KeysCollectionCache() = default;
+ KeysCollectionCache(std::string purpose, KeysCollectionClient* client);
+ ~KeysCollectionCache() = default;
/**
- * Refreshes cache and returns the latest key seen.
+ * Check if there are new documents expiresAt > latestKeyDoc.expiresAt.
*/
- virtual StatusWith<KeysCollectionDocument> refresh(OperationContext* opCtx) = 0;
+ StatusWith<KeysCollectionDocument> refresh(OperationContext* opCtx);
- /**
- * Returns the key in the cache that has the smallest expiresAt value that is also greater than
- * the forThisTime argument.
- */
- virtual StatusWith<KeysCollectionDocument> getKey(const LogicalTime& forThisTime) = 0;
+ StatusWith<KeysCollectionDocument> getKey(const LogicalTime& forThisTime);
+ StatusWith<KeysCollectionDocument> getKeyById(long long keyId, const LogicalTime& forThisTime);
/**
- * Returns the key in the cache that matches the keyId and expiresAt value to be no less than
- * the forThisTime argument.
+ * Resets the cache of keys if the client doesnt allow readConcern level:majority reads.
+ * This method intended to be called on the rollback of the node.
*/
- virtual StatusWith<KeysCollectionDocument> getKeyById(long long keyId,
- const LogicalTime& forThisTime) = 0;
+ void resetCache();
+
+private:
+ const std::string _purpose;
+ KeysCollectionClient* const _client;
+
+ stdx::mutex _cacheMutex;
+ std::map<LogicalTime, KeysCollectionDocument> _cache; // expiresAt -> KeysDocument
};
} // namespace mongo
diff --git a/src/mongo/db/keys_collection_cache_reader.h b/src/mongo/db/keys_collection_cache_reader.h
deleted file mode 100644
index 038c24384c8..00000000000
--- a/src/mongo/db/keys_collection_cache_reader.h
+++ /dev/null
@@ -1,73 +0,0 @@
-/**
- * Copyright (C) 2017 MongoDB, Inc.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * As a special exception, the copyright holders give permission to link the
- * code of portions of this program with the OpenSSL library under certain
- * conditions as described in each individual source file and distribute
- * linked combinations including the program with the OpenSSL library. You
- * must comply with the GNU Affero General Public License in all respects for
- * all of the code used other than as permitted herein. If you modify file(s)
- * with this exception, you may extend this exception to your version of the
- * file(s), but you are not obligated to do so. If you do not wish to do so,
- * delete this exception statement from your version. If you delete this
- * exception statement from all source files in the program, then also delete
- * it in the license file.
- */
-
-#pragma once
-
-#include <map>
-
-#include "mongo/db/keys_collection_cache.h"
-#include "mongo/stdx/mutex.h"
-
-namespace mongo {
-
-class KeysCollectionClient;
-
-/**
- * Keeps a local cache of the keys with the ability to refresh.
- *
- * Note: This assumes that user does not manually update the keys collection.
- */
-class KeysCollectionCacheReader : public KeysCollectionCache {
-public:
- KeysCollectionCacheReader(std::string purpose, KeysCollectionClient* client);
- ~KeysCollectionCacheReader() = default;
-
- /**
- * Check if there are new documents expiresAt > latestKeyDoc.expiresAt.
- */
- StatusWith<KeysCollectionDocument> refresh(OperationContext* opCtx) override;
-
- StatusWith<KeysCollectionDocument> getKey(const LogicalTime& forThisTime) override;
- StatusWith<KeysCollectionDocument> getKeyById(long long keyId,
- const LogicalTime& forThisTime) override;
-
- /**
- * Resets the cache of keys if the client doesnt allow readConcern level:majority reads.
- * This method intended to be called on the rollback of the node.
- */
- void resetCache();
-
-private:
- const std::string _purpose;
- KeysCollectionClient* const _client;
-
- stdx::mutex _cacheMutex;
- std::map<LogicalTime, KeysCollectionDocument> _cache; // expiresAt -> KeysDocument
-};
-
-} // namespace mongo
diff --git a/src/mongo/db/keys_collection_cache_reader_test.cpp b/src/mongo/db/keys_collection_cache_test.cpp
index 599c9351808..6f50180ac8d 100644
--- a/src/mongo/db/keys_collection_cache_reader_test.cpp
+++ b/src/mongo/db/keys_collection_cache_test.cpp
@@ -29,7 +29,7 @@
#include "mongo/platform/basic.h"
#include "mongo/db/jsobj.h"
-#include "mongo/db/keys_collection_cache_reader.h"
+#include "mongo/db/keys_collection_cache.h"
#include "mongo/db/keys_collection_client_sharded.h"
#include "mongo/db/keys_collection_document.h"
#include "mongo/db/operation_context.h"
@@ -41,7 +41,7 @@
namespace mongo {
-class CacheReaderTest : public ConfigServerTestFixture {
+class CacheTest : public ConfigServerTestFixture {
protected:
void setUp() override {
ConfigServerTestFixture::setUp();
@@ -58,29 +58,29 @@ private:
std::unique_ptr<KeysCollectionClient> _catalogClient;
};
-TEST_F(CacheReaderTest, ErrorsIfCacheIsEmpty) {
- KeysCollectionCacheReader reader("test", catalogClient());
- auto status = reader.getKey(LogicalTime(Timestamp(1, 0))).getStatus();
+TEST_F(CacheTest, ErrorsIfCacheIsEmpty) {
+ KeysCollectionCache cache("test", catalogClient());
+ auto status = cache.getKey(LogicalTime(Timestamp(1, 0))).getStatus();
ASSERT_EQ(ErrorCodes::KeyNotFound, status.code());
ASSERT_FALSE(status.reason().empty());
}
-TEST_F(CacheReaderTest, RefreshErrorsIfCacheIsEmpty) {
- KeysCollectionCacheReader reader("test", catalogClient());
- auto status = reader.refresh(operationContext()).getStatus();
+TEST_F(CacheTest, RefreshErrorsIfCacheIsEmpty) {
+ KeysCollectionCache cache("test", catalogClient());
+ auto status = cache.refresh(operationContext()).getStatus();
ASSERT_EQ(ErrorCodes::KeyNotFound, status.code());
ASSERT_FALSE(status.reason().empty());
}
-TEST_F(CacheReaderTest, GetKeyShouldReturnCorrectKeyAfterRefresh) {
- KeysCollectionCacheReader reader("test", catalogClient());
+TEST_F(CacheTest, GetKeyShouldReturnCorrectKeyAfterRefresh) {
+ KeysCollectionCache cache("test", catalogClient());
KeysCollectionDocument origKey1(
1, "test", TimeProofService::generateRandomKey(), LogicalTime(Timestamp(105, 0)));
ASSERT_OK(insertToConfigCollection(
operationContext(), NamespaceString(KeysCollectionDocument::ConfigNS), origKey1.toBSON()));
- auto refreshStatus = reader.refresh(operationContext());
+ auto refreshStatus = cache.refresh(operationContext());
ASSERT_OK(refreshStatus.getStatus());
{
@@ -91,7 +91,7 @@ TEST_F(CacheReaderTest, GetKeyShouldReturnCorrectKeyAfterRefresh) {
ASSERT_EQ(Timestamp(105, 0), key.getExpiresAt().asTimestamp());
}
- auto status = reader.getKey(LogicalTime(Timestamp(1, 0)));
+ auto status = cache.getKey(LogicalTime(Timestamp(1, 0)));
ASSERT_OK(status.getStatus());
{
@@ -103,15 +103,15 @@ TEST_F(CacheReaderTest, GetKeyShouldReturnCorrectKeyAfterRefresh) {
}
}
-TEST_F(CacheReaderTest, GetKeyShouldReturnErrorIfNoKeyIsValidForGivenTime) {
- KeysCollectionCacheReader reader("test", catalogClient());
+TEST_F(CacheTest, GetKeyShouldReturnErrorIfNoKeyIsValidForGivenTime) {
+ KeysCollectionCache cache("test", catalogClient());
KeysCollectionDocument origKey1(
1, "test", TimeProofService::generateRandomKey(), LogicalTime(Timestamp(105, 0)));
ASSERT_OK(insertToConfigCollection(
operationContext(), NamespaceString(KeysCollectionDocument::ConfigNS), origKey1.toBSON()));
- auto refreshStatus = reader.refresh(operationContext());
+ auto refreshStatus = cache.refresh(operationContext());
ASSERT_OK(refreshStatus.getStatus());
{
@@ -122,12 +122,12 @@ TEST_F(CacheReaderTest, GetKeyShouldReturnErrorIfNoKeyIsValidForGivenTime) {
ASSERT_EQ(Timestamp(105, 0), key.getExpiresAt().asTimestamp());
}
- auto status = reader.getKey(LogicalTime(Timestamp(110, 0)));
+ auto status = cache.getKey(LogicalTime(Timestamp(110, 0)));
ASSERT_EQ(ErrorCodes::KeyNotFound, status.getStatus());
}
-TEST_F(CacheReaderTest, GetKeyShouldReturnOldestKeyPossible) {
- KeysCollectionCacheReader reader("test", catalogClient());
+TEST_F(CacheTest, GetKeyShouldReturnOldestKeyPossible) {
+ KeysCollectionCache cache("test", catalogClient());
KeysCollectionDocument origKey0(
0, "test", TimeProofService::generateRandomKey(), LogicalTime(Timestamp(100, 0)));
@@ -144,7 +144,7 @@ TEST_F(CacheReaderTest, GetKeyShouldReturnOldestKeyPossible) {
ASSERT_OK(insertToConfigCollection(
operationContext(), NamespaceString(KeysCollectionDocument::ConfigNS), origKey2.toBSON()));
- auto refreshStatus = reader.refresh(operationContext());
+ auto refreshStatus = cache.refresh(operationContext());
ASSERT_OK(refreshStatus.getStatus());
{
@@ -155,7 +155,7 @@ TEST_F(CacheReaderTest, GetKeyShouldReturnOldestKeyPossible) {
ASSERT_EQ(Timestamp(110, 0), key.getExpiresAt().asTimestamp());
}
- auto keyStatus = reader.getKey(LogicalTime(Timestamp(103, 1)));
+ auto keyStatus = cache.getKey(LogicalTime(Timestamp(103, 1)));
ASSERT_OK(keyStatus.getStatus());
{
@@ -167,8 +167,8 @@ TEST_F(CacheReaderTest, GetKeyShouldReturnOldestKeyPossible) {
}
}
-TEST_F(CacheReaderTest, RefreshShouldNotGetKeysForOtherPurpose) {
- KeysCollectionCacheReader reader("test", catalogClient());
+TEST_F(CacheTest, RefreshShouldNotGetKeysForOtherPurpose) {
+ KeysCollectionCache cache("test", catalogClient());
KeysCollectionDocument origKey0(
0, "dummy", TimeProofService::generateRandomKey(), LogicalTime(Timestamp(100, 0)));
@@ -176,10 +176,10 @@ TEST_F(CacheReaderTest, RefreshShouldNotGetKeysForOtherPurpose) {
operationContext(), NamespaceString(KeysCollectionDocument::ConfigNS), origKey0.toBSON()));
{
- auto refreshStatus = reader.refresh(operationContext());
+ auto refreshStatus = cache.refresh(operationContext());
ASSERT_EQ(ErrorCodes::KeyNotFound, refreshStatus.getStatus());
- auto emptyKeyStatus = reader.getKey(LogicalTime(Timestamp(50, 0)));
+ auto emptyKeyStatus = cache.getKey(LogicalTime(Timestamp(50, 0)));
ASSERT_EQ(ErrorCodes::KeyNotFound, emptyKeyStatus.getStatus());
}
@@ -189,7 +189,7 @@ TEST_F(CacheReaderTest, RefreshShouldNotGetKeysForOtherPurpose) {
operationContext(), NamespaceString(KeysCollectionDocument::ConfigNS), origKey1.toBSON()));
{
- auto refreshStatus = reader.refresh(operationContext());
+ auto refreshStatus = cache.refresh(operationContext());
ASSERT_OK(refreshStatus.getStatus());
auto key = refreshStatus.getValue();
@@ -199,7 +199,7 @@ TEST_F(CacheReaderTest, RefreshShouldNotGetKeysForOtherPurpose) {
ASSERT_EQ(Timestamp(105, 0), key.getExpiresAt().asTimestamp());
}
- auto keyStatus = reader.getKey(LogicalTime(Timestamp(60, 1)));
+ auto keyStatus = cache.getKey(LogicalTime(Timestamp(60, 1)));
ASSERT_OK(keyStatus.getStatus());
{
@@ -211,8 +211,8 @@ TEST_F(CacheReaderTest, RefreshShouldNotGetKeysForOtherPurpose) {
}
}
-TEST_F(CacheReaderTest, RefreshCanIncrementallyGetNewKeys) {
- KeysCollectionCacheReader reader("test", catalogClient());
+TEST_F(CacheTest, RefreshCanIncrementallyGetNewKeys) {
+ KeysCollectionCache cache("test", catalogClient());
KeysCollectionDocument origKey0(
0, "test", TimeProofService::generateRandomKey(), LogicalTime(Timestamp(100, 0)));
@@ -220,7 +220,7 @@ TEST_F(CacheReaderTest, RefreshCanIncrementallyGetNewKeys) {
operationContext(), NamespaceString(KeysCollectionDocument::ConfigNS), origKey0.toBSON()));
{
- auto refreshStatus = reader.refresh(operationContext());
+ auto refreshStatus = cache.refresh(operationContext());
ASSERT_OK(refreshStatus.getStatus());
@@ -230,7 +230,7 @@ TEST_F(CacheReaderTest, RefreshCanIncrementallyGetNewKeys) {
ASSERT_EQ("test", key.getPurpose());
ASSERT_EQ(Timestamp(100, 0), key.getExpiresAt().asTimestamp());
- auto keyStatus = reader.getKey(LogicalTime(Timestamp(112, 1)));
+ auto keyStatus = cache.getKey(LogicalTime(Timestamp(112, 1)));
ASSERT_EQ(ErrorCodes::KeyNotFound, keyStatus.getStatus());
}
@@ -245,7 +245,7 @@ TEST_F(CacheReaderTest, RefreshCanIncrementallyGetNewKeys) {
operationContext(), NamespaceString(KeysCollectionDocument::ConfigNS), origKey2.toBSON()));
{
- auto refreshStatus = reader.refresh(operationContext());
+ auto refreshStatus = cache.refresh(operationContext());
ASSERT_OK(refreshStatus.getStatus());
auto key = refreshStatus.getValue();
@@ -256,7 +256,7 @@ TEST_F(CacheReaderTest, RefreshCanIncrementallyGetNewKeys) {
}
{
- auto keyStatus = reader.getKey(LogicalTime(Timestamp(108, 1)));
+ auto keyStatus = cache.getKey(LogicalTime(Timestamp(108, 1)));
auto key = keyStatus.getValue();
ASSERT_EQ(2, key.getKeyId());
diff --git a/src/mongo/db/keys_collection_manager.cpp b/src/mongo/db/keys_collection_manager.cpp
index fc5a138020a..889b822477c 100644
--- a/src/mongo/db/keys_collection_manager.cpp
+++ b/src/mongo/db/keys_collection_manager.cpp
@@ -31,8 +31,8 @@
#include "mongo/db/keys_collection_manager.h"
-#include "mongo/db/keys_collection_cache_reader.h"
-#include "mongo/db/keys_collection_cache_reader_and_updater.h"
+#include "mongo/db/key_generator.h"
+#include "mongo/db/keys_collection_cache.h"
#include "mongo/db/keys_collection_client.h"
#include "mongo/db/logical_clock.h"
#include "mongo/db/logical_time.h"
@@ -165,19 +165,18 @@ void KeysCollectionManager::stopMonitoring() {
void KeysCollectionManager::enableKeyGenerator(OperationContext* opCtx, bool doEnable) {
if (doEnable) {
_refresher.switchFunc(opCtx, [this](OperationContext* opCtx) {
- KeysCollectionCacheReaderAndUpdater keyGenerator(
- _purpose, _client.get(), _keyValidForInterval);
- auto keyGenerationStatus = keyGenerator.refresh(opCtx);
+ KeyGenerator keyGenerator(_purpose, _client.get(), _keyValidForInterval);
+ auto keyGenerationStatus = keyGenerator.generateNewKeysIfNeeded(opCtx);
- if (ErrorCodes::isShutdownError(keyGenerationStatus.getStatus().code())) {
- return keyGenerationStatus;
+ if (ErrorCodes::isShutdownError(keyGenerationStatus.code())) {
+ return StatusWith<KeysCollectionDocument>(keyGenerationStatus);
}
// An error encountered by the keyGenerator should not prevent refreshing the cache
auto cacheRefreshStatus = _keysCache.refresh(opCtx);
if (!keyGenerationStatus.isOK()) {
- return keyGenerationStatus;
+ return StatusWith<KeysCollectionDocument>(keyGenerationStatus);
}
return cacheRefreshStatus;
diff --git a/src/mongo/db/keys_collection_manager.h b/src/mongo/db/keys_collection_manager.h
index 3cdac7b9bf2..5826e2228ee 100644
--- a/src/mongo/db/keys_collection_manager.h
+++ b/src/mongo/db/keys_collection_manager.h
@@ -31,8 +31,8 @@
#include <memory>
#include "mongo/base/status_with.h"
-#include "mongo/db/keys_collection_cache_reader.h"
-#include "mongo/db/keys_collection_cache_reader_and_updater.h"
+#include "mongo/db/key_generator.h"
+#include "mongo/db/keys_collection_cache.h"
#include "mongo/db/keys_collection_document.h"
#include "mongo/stdx/functional.h"
#include "mongo/stdx/mutex.h"
@@ -192,7 +192,7 @@ private:
const Seconds _keyValidForInterval;
// No mutex needed since the members below have their own mutexes.
- KeysCollectionCacheReader _keysCache;
+ KeysCollectionCache _keysCache;
PeriodicRunner _refresher;
};