diff options
author | William Schultz <william.schultz@mongodb.com> | 2020-02-04 21:49:45 +0000 |
---|---|---|
committer | evergreen <evergreen@mongodb.com> | 2020-02-04 21:49:45 +0000 |
commit | 7fa0a642fa7e99740b83e61040b01498dfb889eb (patch) | |
tree | 39c84b3c1a2deae115193c7471a93450f9e950ad /src/mongo/db/repl/repl_set_config.h | |
parent | e79dc5d3c154e1cbc5d071f5e3725ea7c9563179 (diff) | |
download | mongo-7fa0a642fa7e99740b83e61040b01498dfb889eb.tar.gz |
SERVER-45082 Add ConfigVersionAndTerm structure for comparing replica set configs
Diffstat (limited to 'src/mongo/db/repl/repl_set_config.h')
-rw-r--r-- | src/mongo/db/repl/repl_set_config.h | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/mongo/db/repl/repl_set_config.h b/src/mongo/db/repl/repl_set_config.h index d5f065d5f7b..b82f0ed5e22 100644 --- a/src/mongo/db/repl/repl_set_config.h +++ b/src/mongo/db/repl/repl_set_config.h @@ -48,6 +48,67 @@ class BSONObj; namespace repl { /** + * A structure that stores a ReplSetConfig (version, term) pair. + * + * This can be used to compare two ReplSetConfig objects to determine which is logically newer. + */ +class ConfigVersionAndTerm { +public: + ConfigVersionAndTerm() : _version(0), _term(OpTime::kUninitializedTerm) {} + ConfigVersionAndTerm(int version, long long term) : _version(version), _term(term) {} + + inline bool operator==(const ConfigVersionAndTerm& rhs) const { + // If term of either item is uninitialized (-1), then we ignore terms entirely and only + // compare versions. + if (_term == OpTime::kUninitializedTerm || rhs._term == OpTime::kUninitializedTerm) { + return _version == rhs._version; + } + // Compare term first, then the versions. + return std::tie(_term, _version) == std::tie(rhs._term, rhs._version); + } + + inline bool operator<(const ConfigVersionAndTerm& rhs) const { + // If term of either item is uninitialized (-1), then we ignore terms entirely and only + // compare versions. This allows force reconfigs, which set the config term to -1, to + // override other configs by using a high config version. + if (_term == OpTime::kUninitializedTerm || rhs._term == OpTime::kUninitializedTerm) { + return _version < rhs._version; + } + // Compare term first, then the versions. + return std::tie(_term, _version) < std::tie(rhs._term, rhs._version); + } + + inline bool operator!=(const ConfigVersionAndTerm& rhs) const { + return !(*this == rhs); + } + + inline bool operator<=(const ConfigVersionAndTerm& rhs) const { + return *this < rhs || *this == rhs; + } + + inline bool operator>(const ConfigVersionAndTerm& rhs) const { + return !(*this <= rhs); + } + + inline bool operator>=(const ConfigVersionAndTerm& rhs) const { + return !(*this < rhs); + } + + // TODO (SERVER-45082): Implement string conversion. + std::string toString() const { + return ""; + }; + + friend std::ostream& operator<<(std::ostream& out, const ConfigVersionAndTerm& cvt) { + return out << cvt.toString(); + } + +private: + long long _version; + long long _term; +}; + +/** * Representation of the configuration information about a particular replica set. */ class ReplSetConfig { |