diff options
| author | Allison Easton <allison.easton@mongodb.com> | 2022-09-20 07:18:04 +0000 |
|---|---|---|
| committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-09-20 07:55:42 +0000 |
| commit | 8bd3a016765aa131b9235f5f5e6a2b65690721f9 (patch) | |
| tree | 8bd79af8746303a70c5d9d1e7f2fe36ffa1e7587 /src | |
| parent | 9efc698c77962029620d62669047f23026766f61 (diff) | |
| download | mongo-8bd3a016765aa131b9235f5f5e6a2b65690721f9.tar.gz | |
SERVER-69400 Rename global_indexes_cache.* to global_index_cache.*
Diffstat (limited to 'src')
| -rw-r--r-- | src/mongo/s/SConscript | 3 | ||||
| -rw-r--r-- | src/mongo/s/comparable_index_version_test.cpp | 2 | ||||
| -rw-r--r-- | src/mongo/s/global_index_cache.cpp | 84 | ||||
| -rw-r--r-- | src/mongo/s/global_index_cache.h | 90 | ||||
| -rw-r--r-- | src/mongo/s/global_indexes_cache.cpp | 120 | ||||
| -rw-r--r-- | src/mongo/s/global_indexes_cache.h | 125 |
6 files changed, 176 insertions, 248 deletions
diff --git a/src/mongo/s/SConscript b/src/mongo/s/SConscript index 27a52ef0ccf..6997dfdbc58 100644 --- a/src/mongo/s/SConscript +++ b/src/mongo/s/SConscript @@ -279,9 +279,8 @@ env.Library( 'chunk.cpp', 'client/shard_factory.cpp', 'client/shard_registry.cpp', - 'global_indexes_cache.cpp', - 'grid.cpp', 'global_index_cache.cpp', + 'grid.cpp', 'shard_key_pattern.cpp', ], LIBDEPS=[ diff --git a/src/mongo/s/comparable_index_version_test.cpp b/src/mongo/s/comparable_index_version_test.cpp index 61ca871112e..af15197f339 100644 --- a/src/mongo/s/comparable_index_version_test.cpp +++ b/src/mongo/s/comparable_index_version_test.cpp @@ -27,7 +27,7 @@ * it in the license file. */ -#include "mongo/s/global_indexes_cache.h" +#include "mongo/s/global_index_cache.h" #include "mongo/unittest/unittest.h" namespace mongo { diff --git a/src/mongo/s/global_index_cache.cpp b/src/mongo/s/global_index_cache.cpp index 782e8300edc..c7c1b603c0b 100644 --- a/src/mongo/s/global_index_cache.cpp +++ b/src/mongo/s/global_index_cache.cpp @@ -64,4 +64,88 @@ void GlobalIndexesCache::clear() { _indexes.clear(); } +AtomicWord<uint64_t> ComparableIndexVersion::_epochDisambiguatingSequenceNumSource{1ULL}; +AtomicWord<uint64_t> ComparableIndexVersion::_forcedRefreshSequenceNumSource{1ULL}; + +ComparableIndexVersion ComparableIndexVersion::makeComparableIndexVersion( + const CollectionIndexes& version) { + return ComparableIndexVersion(_forcedRefreshSequenceNumSource.load(), + version, + _epochDisambiguatingSequenceNumSource.fetchAndAdd(1)); +} + +ComparableIndexVersion ComparableIndexVersion::makeComparableIndexVersionForForcedRefresh() { + return ComparableIndexVersion(_forcedRefreshSequenceNumSource.addAndFetch(2) - 1, + boost::none, + _epochDisambiguatingSequenceNumSource.fetchAndAdd(1)); +} + +void ComparableIndexVersion::setCollectionIndexes(const CollectionIndexes& version) { + _indexVersion = version; +} + +std::string ComparableIndexVersion::toString() const { + BSONObjBuilder builder; + if (_indexVersion) + builder.append("collectionIndexes"_sd, _indexVersion->toBSONForLogging()); + else + builder.append("collectionIndexes"_sd, "None"); + + builder.append("forcedRefreshSequenceNum"_sd, static_cast<int64_t>(_forcedRefreshSequenceNum)); + builder.append("epochDisambiguatingSequenceNum"_sd, + static_cast<int64_t>(_epochDisambiguatingSequenceNum)); + + return builder.obj().toString(); +} + +bool ComparableIndexVersion::operator==(const ComparableIndexVersion& other) const { + if (_forcedRefreshSequenceNum != other._forcedRefreshSequenceNum) + return false; // Values with different forced refresh sequence numbers are always + // considered different, regardless of the underlying versions + if (_forcedRefreshSequenceNum == 0) + return true; // Only default constructed values have _forcedRefreshSequenceNum == 0 and + // they are always equal + + // Relying on the boost::optional<CollectionIndexes>::operator== comparison + return _indexVersion == other._indexVersion; +} + +bool ComparableIndexVersion::operator<(const ComparableIndexVersion& other) const { + if (_forcedRefreshSequenceNum < other._forcedRefreshSequenceNum) + return true; // Values with different forced refresh sequence numbers are always considered + // different, regardless of the underlying versions + if (_forcedRefreshSequenceNum > other._forcedRefreshSequenceNum) + return false; // Values with different forced refresh sequence numbers are always + // considered different, regardless of the underlying versions + if (_forcedRefreshSequenceNum == 0) + return false; // Only default constructed values have _forcedRefreshSequenceNum == 0 and + // they are always equal + if (_indexVersion.has_value() != other._indexVersion.has_value()) + return _epochDisambiguatingSequenceNum < + other._epochDisambiguatingSequenceNum; // One side is not initialised, but the other + // is, which can only happen if one side is + // ForForcedRefresh and the other is made from + // makeComparableIndexVersion. In this case, use + // the _epochDisambiguatingSequenceNum to see + // which one is more recent. + if (!_indexVersion.has_value()) + return _epochDisambiguatingSequenceNum < + other._epochDisambiguatingSequenceNum; // Both sides are not initialised, which can + // only happen if both were created from + // ForForcedRefresh. In this case, use the + // _epochDisambiguatingSequenceNum to see which + // one is more recent. + + if (_indexVersion->getTimestamp() == other._indexVersion->getTimestamp()) { + if (!_indexVersion->isSet() && !other._indexVersion->isSet()) { + return false; + } else if (_indexVersion->isSet() && other._indexVersion->isSet()) { + return _indexVersion->indexVersion() < other._indexVersion->indexVersion(); + } + } else if (_indexVersion->isSet() && other._indexVersion->isSet()) { + return _indexVersion->getTimestamp() < other._indexVersion->getTimestamp(); + } + return _epochDisambiguatingSequenceNum < other._epochDisambiguatingSequenceNum; +} + } // namespace mongo diff --git a/src/mongo/s/global_index_cache.h b/src/mongo/s/global_index_cache.h index ad95e91259b..f312d8b8df3 100644 --- a/src/mongo/s/global_index_cache.h +++ b/src/mongo/s/global_index_cache.h @@ -31,6 +31,7 @@ #include "mongo/db/namespace_string.h" #include "mongo/s/catalog/type_index_catalog_gen.h" +#include "mongo/s/index_version.h" namespace mongo { @@ -59,4 +60,93 @@ private: boost::optional<Timestamp> _indexVersion; IndexCatalogTypeMap _indexes; }; + +/** + * Constructed to be used exclusively by the CatalogCache as a vector clock (Time) to drive + * IndexCache's lookups. + * + * The CollectionIndexes class contains a timestamp for the collection generation which resets to 0 + * if the collection is dropped, in which case the versions cannot be compared. + * + * This class wraps a CollectionIndexes object with a node-local sequence number + * (_epochDisambiguatingSequenceNum) that allows the comparision. + * + */ +class ComparableIndexVersion { +public: + /** + * Creates a ComparableIndexVersion that wraps the given CollectionIndexes. + * Each object created through this method will have a local sequence number greater than the + * previously created ones. + */ + static ComparableIndexVersion makeComparableIndexVersion(const CollectionIndexes& version); + + /** + * Creates a new instance which will artificially be greater than any previously created + * ComparableIndexVersion and smaller than any instance created afterwards. Used as means to + * cause the collections cache to attempt a refresh in situations where causal consistency + * cannot be inferred. + */ + static ComparableIndexVersion makeComparableIndexVersionForForcedRefresh(); + + /** + * Empty constructor needed by the ReadThroughCache. + * + * Instances created through this constructor will be always less then the ones created through + * the two static constructors, but they do not carry any meaningful value and can only be used + * for comparison purposes. + */ + ComparableIndexVersion() = default; + + std::string toString() const; + + bool operator==(const ComparableIndexVersion& other) const; + + bool operator!=(const ComparableIndexVersion& other) const { + return !(*this == other); + } + + /** + * In case the two compared instances have different generations, the most recently created one + * will be greater, otherwise the comparision will be driven by the index versions of the + * underlying CollectionIndexes. + */ + bool operator<(const ComparableIndexVersion& other) const; + + bool operator>(const ComparableIndexVersion& other) const { + return other < *this; + } + + bool operator<=(const ComparableIndexVersion& other) const { + return !(*this > other); + } + + bool operator>=(const ComparableIndexVersion& other) const { + return !(*this < other); + } + +private: + friend class CatalogCache; + + static AtomicWord<uint64_t> _epochDisambiguatingSequenceNumSource; + static AtomicWord<uint64_t> _forcedRefreshSequenceNumSource; + + ComparableIndexVersion(uint64_t forcedRefreshSequenceNum, + boost::optional<CollectionIndexes> version, + uint64_t epochDisambiguatingSequenceNum) + : _forcedRefreshSequenceNum(forcedRefreshSequenceNum), + _indexVersion(std::move(version)), + _epochDisambiguatingSequenceNum(epochDisambiguatingSequenceNum) {} + + void setCollectionIndexes(const CollectionIndexes& version); + + uint64_t _forcedRefreshSequenceNum{0}; + + boost::optional<CollectionIndexes> _indexVersion; + + // Locally incremented sequence number that allows to compare two colection versions with + // different generations. Each new ComparableIndexVersion will have a greater sequence number + // than the ones created before. + uint64_t _epochDisambiguatingSequenceNum{0}; +}; } // namespace mongo diff --git a/src/mongo/s/global_indexes_cache.cpp b/src/mongo/s/global_indexes_cache.cpp deleted file mode 100644 index 686d9d99f96..00000000000 --- a/src/mongo/s/global_indexes_cache.cpp +++ /dev/null @@ -1,120 +0,0 @@ -/** - * Copyright (C) 2022-present MongoDB, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the Server Side Public License, version 1, - * as published by MongoDB, Inc. - * - * 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 - * Server Side Public License for more details. - * - * You should have received a copy of the Server Side Public License - * along with this program. If not, see - * <http://www.mongodb.com/licensing/server-side-public-license>. - * - * 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 Server Side 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. - */ - -#include "mongo/s/global_indexes_cache.h" - -#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kSharding - -namespace mongo { - -AtomicWord<uint64_t> ComparableIndexVersion::_epochDisambiguatingSequenceNumSource{1ULL}; -AtomicWord<uint64_t> ComparableIndexVersion::_forcedRefreshSequenceNumSource{1ULL}; - -ComparableIndexVersion ComparableIndexVersion::makeComparableIndexVersion( - const CollectionIndexes& version) { - return ComparableIndexVersion(_forcedRefreshSequenceNumSource.load(), - version, - _epochDisambiguatingSequenceNumSource.fetchAndAdd(1)); -} - -ComparableIndexVersion ComparableIndexVersion::makeComparableIndexVersionForForcedRefresh() { - return ComparableIndexVersion(_forcedRefreshSequenceNumSource.addAndFetch(2) - 1, - boost::none, - _epochDisambiguatingSequenceNumSource.fetchAndAdd(1)); -} - -void ComparableIndexVersion::setCollectionIndexes(const CollectionIndexes& version) { - _indexVersion = version; -} - -std::string ComparableIndexVersion::toString() const { - BSONObjBuilder builder; - if (_indexVersion) - builder.append("collectionIndexes"_sd, _indexVersion->toBSONForLogging()); - else - builder.append("collectionIndexes"_sd, "None"); - - builder.append("forcedRefreshSequenceNum"_sd, static_cast<int64_t>(_forcedRefreshSequenceNum)); - builder.append("epochDisambiguatingSequenceNum"_sd, - static_cast<int64_t>(_epochDisambiguatingSequenceNum)); - - return builder.obj().toString(); -} - -bool ComparableIndexVersion::operator==(const ComparableIndexVersion& other) const { - if (_forcedRefreshSequenceNum != other._forcedRefreshSequenceNum) - return false; // Values with different forced refresh sequence numbers are always - // considered different, regardless of the underlying versions - if (_forcedRefreshSequenceNum == 0) - return true; // Only default constructed values have _forcedRefreshSequenceNum == 0 and - // they are always equal - - // Relying on the boost::optional<CollectionIndexes>::operator== comparison - return _indexVersion == other._indexVersion; -} - -bool ComparableIndexVersion::operator<(const ComparableIndexVersion& other) const { - if (_forcedRefreshSequenceNum < other._forcedRefreshSequenceNum) - return true; // Values with different forced refresh sequence numbers are always considered - // different, regardless of the underlying versions - if (_forcedRefreshSequenceNum > other._forcedRefreshSequenceNum) - return false; // Values with different forced refresh sequence numbers are always - // considered different, regardless of the underlying versions - if (_forcedRefreshSequenceNum == 0) - return false; // Only default constructed values have _forcedRefreshSequenceNum == 0 and - // they are always equal - if (_indexVersion.has_value() != other._indexVersion.has_value()) - return _epochDisambiguatingSequenceNum < - other._epochDisambiguatingSequenceNum; // One side is not initialised, but the other - // is, which can only happen if one side is - // ForForcedRefresh and the other is made from - // makeComparableIndexVersion. In this case, use - // the _epochDisambiguatingSequenceNum to see - // which one is more recent. - if (!_indexVersion.has_value()) - return _epochDisambiguatingSequenceNum < - other._epochDisambiguatingSequenceNum; // Both sides are not initialised, which can - // only happen if both were created from - // ForForcedRefresh. In this case, use the - // _epochDisambiguatingSequenceNum to see which - // one is more recent. - - if (_indexVersion->getTimestamp() == other._indexVersion->getTimestamp()) { - if (!_indexVersion->isSet() && !other._indexVersion->isSet()) { - return false; - } else if (_indexVersion->isSet() && other._indexVersion->isSet()) { - return _indexVersion->indexVersion() < other._indexVersion->indexVersion(); - } - } else if (_indexVersion->isSet() && other._indexVersion->isSet()) { - return _indexVersion->getTimestamp() < other._indexVersion->getTimestamp(); - } - return _epochDisambiguatingSequenceNum < other._epochDisambiguatingSequenceNum; -} - -} // namespace mongo diff --git a/src/mongo/s/global_indexes_cache.h b/src/mongo/s/global_indexes_cache.h deleted file mode 100644 index 76dc1b10574..00000000000 --- a/src/mongo/s/global_indexes_cache.h +++ /dev/null @@ -1,125 +0,0 @@ -/** - * Copyright (C) 2022-present MongoDB, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the Server Side Public License, version 1, - * as published by MongoDB, Inc. - * - * 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 - * Server Side Public License for more details. - * - * You should have received a copy of the Server Side Public License - * along with this program. If not, see - * <http://www.mongodb.com/licensing/server-side-public-license>. - * - * 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 Server Side 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 "mongo/s/index_version.h" - -namespace mongo { - -/** - * Constructed to be used exclusively by the CatalogCache as a vector clock (Time) to drive - * IndexCache's lookups. - * - * The CollectionIndexes class contains a timestamp for the collection generation which resets to 0 - * if the collection is dropped, in which case the versions cannot be compared. - * - * This class wraps a CollectionIndexes object with a node-local sequence number - * (_epochDisambiguatingSequenceNum) that allows the comparision. - * - */ -class ComparableIndexVersion { -public: - /** - * Creates a ComparableIndexVersion that wraps the given CollectionIndexes. - * Each object created through this method will have a local sequence number greater than the - * previously created ones. - */ - static ComparableIndexVersion makeComparableIndexVersion(const CollectionIndexes& version); - - /** - * Creates a new instance which will artificially be greater than any previously created - * ComparableIndexVersion and smaller than any instance created afterwards. Used as means to - * cause the collections cache to attempt a refresh in situations where causal consistency - * cannot be inferred. - */ - static ComparableIndexVersion makeComparableIndexVersionForForcedRefresh(); - - /** - * Empty constructor needed by the ReadThroughCache. - * - * Instances created through this constructor will be always less then the ones created through - * the two static constructors, but they do not carry any meaningful value and can only be used - * for comparison purposes. - */ - ComparableIndexVersion() = default; - - std::string toString() const; - - bool operator==(const ComparableIndexVersion& other) const; - - bool operator!=(const ComparableIndexVersion& other) const { - return !(*this == other); - } - - /** - * In case the two compared instances have different generations, the most recently created one - * will be greater, otherwise the comparision will be driven by the index versions of the - * underlying CollectionIndexes. - */ - bool operator<(const ComparableIndexVersion& other) const; - - bool operator>(const ComparableIndexVersion& other) const { - return other < *this; - } - - bool operator<=(const ComparableIndexVersion& other) const { - return !(*this > other); - } - - bool operator>=(const ComparableIndexVersion& other) const { - return !(*this < other); - } - -private: - friend class CatalogCache; - - static AtomicWord<uint64_t> _epochDisambiguatingSequenceNumSource; - static AtomicWord<uint64_t> _forcedRefreshSequenceNumSource; - - ComparableIndexVersion(uint64_t forcedRefreshSequenceNum, - boost::optional<CollectionIndexes> version, - uint64_t epochDisambiguatingSequenceNum) - : _forcedRefreshSequenceNum(forcedRefreshSequenceNum), - _indexVersion(std::move(version)), - _epochDisambiguatingSequenceNum(epochDisambiguatingSequenceNum) {} - - void setCollectionIndexes(const CollectionIndexes& version); - - uint64_t _forcedRefreshSequenceNum{0}; - - boost::optional<CollectionIndexes> _indexVersion; - - // Locally incremented sequence number that allows to compare two colection versions with - // different generations. Each new ComparableIndexVersion will have a greater sequence number - // than the ones created before. - uint64_t _epochDisambiguatingSequenceNum{0}; -}; - -} // namespace mongo |
