summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/mongo/bson/bson_field.h14
-rw-r--r--src/mongo/db/catalog/coll_mod.cpp6
-rw-r--r--src/mongo/db/catalog/uuid_catalog_test.cpp23
-rw-r--r--src/mongo/db/s/migration_source_manager.cpp2
-rw-r--r--src/mongo/db/s/migration_source_manager.h4
-rw-r--r--src/mongo/util/uuid.h13
6 files changed, 37 insertions, 25 deletions
diff --git a/src/mongo/bson/bson_field.h b/src/mongo/bson/bson_field.h
index 7e33df63ca2..1933dce26ed 100644
--- a/src/mongo/bson/bson_field.h
+++ b/src/mongo/bson/bson_field.h
@@ -29,6 +29,8 @@
#include <string>
+#include <boost/optional.hpp>
+
#include "mongo/bson/bsonobj.h"
namespace mongo {
@@ -88,10 +90,9 @@ private:
template <typename T>
class BSONField {
public:
- BSONField(const std::string& name) : _name(name), _defaultSet(false) {}
+ BSONField(const std::string& name) : _name(name) {}
- BSONField(const std::string& name, const T& defaultVal)
- : _name(name), _default(defaultVal), _defaultSet(true) {}
+ BSONField(const std::string& name, const T& defaultVal) : _name(name), _default(defaultVal) {}
BSONFieldValue<T> make(const T& t) const {
return BSONFieldValue<T>(_name, t);
@@ -106,11 +107,11 @@ public:
}
const T& getDefault() const {
- return _default;
+ return *_default;
}
bool hasDefault() const {
- return _defaultSet;
+ return bool(_default);
}
std::string operator()() const {
@@ -133,8 +134,7 @@ public:
private:
std::string _name;
- T _default;
- bool _defaultSet;
+ boost::optional<T> _default;
};
} // namespace mongo
diff --git a/src/mongo/db/catalog/coll_mod.cpp b/src/mongo/db/catalog/coll_mod.cpp
index ac85e60f37f..b264a01da5c 100644
--- a/src/mongo/db/catalog/coll_mod.cpp
+++ b/src/mongo/db/catalog/coll_mod.cpp
@@ -486,7 +486,7 @@ void _updateDatabaseUUIDSchemaVersion(OperationContext* opCtx,
if (needUUIDAdded) {
if (collToUUID.find(collNSS.coll().toString()) != collToUUID.end()) {
// This is a sharded collection. Use the UUID generated by the config server.
- uuid = collToUUID[collNSS.coll().toString()];
+ uuid = collToUUID.at(collNSS.coll().toString());
} else {
// This is an unsharded collection. Generate a UUID.
uuid = UUID::gen();
@@ -608,8 +608,8 @@ void updateUUIDSchemaVersion(OperationContext* opCtx, bool upgrade) {
<< collType.getNs().ns()
<< " to have a UUID, but it did not",
collType.getUUID());
- dbToCollToUUID[collType.getNs().db().toString()][collType.getNs().coll().toString()] =
- *collType.getUUID();
+ dbToCollToUUID[collType.getNs().db().toString()].emplace(
+ collType.getNs().coll().toString(), *collType.getUUID());
}
}
diff --git a/src/mongo/db/catalog/uuid_catalog_test.cpp b/src/mongo/db/catalog/uuid_catalog_test.cpp
index b5361d0dcbb..20115a42642 100644
--- a/src/mongo/db/catalog/uuid_catalog_test.cpp
+++ b/src/mongo/db/catalog/uuid_catalog_test.cpp
@@ -41,14 +41,21 @@ using namespace mongo;
*/
class UUIDCatalogTest : public unittest::Test {
public:
- UUIDCatalogTest() : nss("testdb", "testcol"), col(stdx::make_unique<CollectionMock>(nss)) {
- std::array<CollectionUUID, 3> sortedUUIDs = {
- CollectionUUID::gen(), CollectionUUID::gen(), CollectionUUID::gen()};
-
- std::sort(sortedUUIDs.begin(), sortedUUIDs.end());
- colUUID = sortedUUIDs[1];
- prevUUID = sortedUUIDs[0];
- nextUUID = sortedUUIDs[2];
+ UUIDCatalogTest()
+ : nss("testdb", "testcol"),
+ col(stdx::make_unique<CollectionMock>(nss)),
+ colUUID(CollectionUUID::gen()),
+ nextUUID(CollectionUUID::gen()),
+ prevUUID(CollectionUUID::gen()) {
+ if (prevUUID > colUUID)
+ std::swap(prevUUID, colUUID);
+ if (colUUID > nextUUID)
+ std::swap(colUUID, nextUUID);
+ if (prevUUID > colUUID)
+ std::swap(prevUUID, colUUID);
+ ASSERT_GT(colUUID, prevUUID);
+ ASSERT_GT(nextUUID, colUUID);
+
// Register dummy collection in catalog.
catalog.onCreateCollection(&opCtx, &col, colUUID);
}
diff --git a/src/mongo/db/s/migration_source_manager.cpp b/src/mongo/db/s/migration_source_manager.cpp
index f2618d75107..fc341dc0673 100644
--- a/src/mongo/db/s/migration_source_manager.cpp
+++ b/src/mongo/db/s/migration_source_manager.cpp
@@ -164,7 +164,7 @@ MigrationSourceManager::MigrationSourceManager(OperationContext* opCtx,
"cannot move chunks for a collection that doesn't exist",
autoColl.getCollection());
- UUID collectionUUID;
+ boost::optional<UUID> collectionUUID;
if (autoColl.getCollection()->uuid()) {
collectionUUID = autoColl.getCollection()->uuid().value();
}
diff --git a/src/mongo/db/s/migration_source_manager.h b/src/mongo/db/s/migration_source_manager.h
index 3568e20d0a5..d6a6df981c0 100644
--- a/src/mongo/db/s/migration_source_manager.h
+++ b/src/mongo/db/s/migration_source_manager.h
@@ -28,6 +28,8 @@
#pragma once
+#include <boost/optional.hpp>
+
#include "mongo/base/disallow_copying.h"
#include "mongo/db/s/collection_sharding_state.h"
#include "mongo/s/move_chunk_request.h"
@@ -220,7 +222,7 @@ private:
// The UUID of the the collection whose chunks are being moved. Default to empty if the
// collection doesn't have UUID.
- UUID _collectionUuid;
+ boost::optional<UUID> _collectionUuid;
// The chunk cloner source. Only available if there is an active migration going on. To set and
// remove it, global S lock needs to be acquired first in order to block all logOp calls and
diff --git a/src/mongo/util/uuid.h b/src/mongo/util/uuid.h
index 61f55ab044c..3eedf7db04f 100644
--- a/src/mongo/util/uuid.h
+++ b/src/mongo/util/uuid.h
@@ -76,11 +76,6 @@ public:
static constexpr int kNumBytes = sizeof(UUIDStorage);
/**
- * Creates an empty UUID.
- */
- UUID() = default;
-
- /**
* Generate a new random v4 UUID per RFC 4122.
*/
static UUID gen();
@@ -186,6 +181,14 @@ public:
private:
UUID(const UUIDStorage& uuid) : _uuid(uuid) {}
+ /**
+ * Should never be used, as the resulting UUID will not be unique.
+ * The exception is in code generated by the IDL compiler, which itself ensures
+ * such an invalid value cannot propagate.
+ */
+ friend class LogicalSessionId;
+ UUID() = default;
+
UUIDStorage _uuid{}; // UUID in network byte order
};