summaryrefslogtreecommitdiff
path: root/src/mongo/s/database_version.h
diff options
context:
space:
mode:
authorSergi Mateo Bellido <sergi.mateo-bellido@mongodb.com>2020-11-27 08:44:34 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-12-01 09:31:40 +0000
commitecd54f7d5a7d56baf9509e83f0393565369b1936 (patch)
tree4b1dd06243c976ea2abe3719fb7f123b2c00e523 /src/mongo/s/database_version.h
parentf8f6d95d4ef13b9530509e82dcba6a6a17a6b83f (diff)
downloadmongo-ecd54f7d5a7d56baf9509e83f0393565369b1936.tar.gz
SERVER-52587 Making collection and database instances comparable through cluster time (no more epochs)
Part 1: setting up the environment to start comparing DatabaseVersions through timestamps. Changelog: - Moving the ComparableDatabaseVersion class to the DatabaseVersion header. - Using ComparableDatabaseVersions in two places where we were doing DatabaseVersion comparsions.
Diffstat (limited to 'src/mongo/s/database_version.h')
-rw-r--r--src/mongo/s/database_version.h85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/mongo/s/database_version.h b/src/mongo/s/database_version.h
index 3daac2d53b8..adaa1ff3963 100644
--- a/src/mongo/s/database_version.h
+++ b/src/mongo/s/database_version.h
@@ -33,6 +33,16 @@
namespace mongo {
+/**
+ * This class is used to represent a specific version of a Database.
+ *
+ * Currently it is implemented as a (uuid, [timestamp,] lastMod) triplet, where the
+ * timestamp is optional in versions prior 4.9. The uuid is going to be removed soon,
+ * since they are not comparable (that's the reason why there is a ComparableDatabaseVersion class).
+ *
+ * Once uuids are gone, relational operators should be implemented in this class.
+ *
+ */
class DatabaseVersion : private DatabaseVersionBase {
public:
using DatabaseVersionBase::getLastMod;
@@ -83,4 +93,79 @@ public:
}
};
+
+/**
+ * The DatabaseVersion class contains a UUID that is not comparable,
+ * in fact is impossible to compare two different DatabaseVersion that have different UUIDs.
+ *
+ * This class wrap a DatabaseVersion object to make it always comparable by timestamping it with a
+ * node-local sequence number (_uuidDisambiguatingSequenceNum).
+ *
+ * This class class should go away once a cluster-wide comparable DatabaseVersion will be
+ * implemented.
+ */
+class ComparableDatabaseVersion {
+public:
+ /**
+ * Creates a ComparableDatabaseVersion that wraps the given DatabaseVersion.
+ * Each object created through this method will have a local sequence number greater than the
+ * previously created ones.
+ */
+ static ComparableDatabaseVersion makeComparableDatabaseVersion(const DatabaseVersion& version);
+
+ /**
+ * Empty constructor needed by the ReadThroughCache.
+ *
+ * Instances created through this constructor will be always less then the ones created through
+ * the static constructor.
+ */
+ ComparableDatabaseVersion() = default;
+
+ const DatabaseVersion& getVersion() const {
+ return *_dbVersion;
+ }
+
+ BSONObj toBSONForLogging() const;
+
+ bool operator==(const ComparableDatabaseVersion& other) const;
+
+ bool operator!=(const ComparableDatabaseVersion& other) const {
+ return !(*this == other);
+ }
+
+ /**
+ * In case the two compared instances have different UUIDs, the most recently created one will
+ * be greater, otherwise the comparison will be driven by the lastMod field of the underlying
+ * DatabaseVersion.
+ */
+ bool operator<(const ComparableDatabaseVersion& other) const;
+
+ bool operator>(const ComparableDatabaseVersion& other) const {
+ return other < *this;
+ }
+
+ bool operator<=(const ComparableDatabaseVersion& other) const {
+ return !(*this > other);
+ }
+
+ bool operator>=(const ComparableDatabaseVersion& other) const {
+ return !(*this < other);
+ }
+
+private:
+ static AtomicWord<uint64_t> _uuidDisambiguatingSequenceNumSource;
+
+ ComparableDatabaseVersion(const DatabaseVersion& version,
+ uint64_t uuidDisambiguatingSequenceNum)
+ : _dbVersion(version), _uuidDisambiguatingSequenceNum(uuidDisambiguatingSequenceNum) {}
+
+ boost::optional<DatabaseVersion> _dbVersion;
+
+ // Locally incremented sequence number that allows to compare two database versions with
+ // different UUIDs. Each new comparableDatabaseVersion will have a greater sequence number then
+ // the ones created before.
+ uint64_t _uuidDisambiguatingSequenceNum{0};
+};
+
+
} // namespace mongo