diff options
author | Nathan Myers <nathan.myers@10gen.com> | 2017-10-17 14:07:38 -0400 |
---|---|---|
committer | Nathan Myers <nathan.myers@10gen.com> | 2017-10-17 14:07:38 -0400 |
commit | aeabbf96ff3c2990f553ba0a5e6e1d18ebddab2f (patch) | |
tree | cfb67393fadfd03d0c40d5082711da5dfa44edc4 /src/mongo/s/catalog | |
parent | bd0c03a8816985f74f7bd24245aa81f9cb5b37f7 (diff) | |
download | mongo-aeabbf96ff3c2990f553ba0a5e6e1d18ebddab2f.tar.gz |
SERVER-31191 Plumb Collection UUIDs through catalog cache
Diffstat (limited to 'src/mongo/s/catalog')
-rw-r--r-- | src/mongo/s/catalog/type_shard_collection.cpp | 61 | ||||
-rw-r--r-- | src/mongo/s/catalog/type_shard_collection.h | 45 | ||||
-rw-r--r-- | src/mongo/s/catalog/type_shard_collection_test.cpp | 54 |
3 files changed, 90 insertions, 70 deletions
diff --git a/src/mongo/s/catalog/type_shard_collection.cpp b/src/mongo/s/catalog/type_shard_collection.cpp index 186b35fbb44..3a98a15f7ba 100644 --- a/src/mongo/s/catalog/type_shard_collection.cpp +++ b/src/mongo/s/catalog/type_shard_collection.cpp @@ -42,8 +42,8 @@ namespace mongo { const std::string ShardCollectionType::ConfigNS = NamespaceString::kShardConfigCollectionsCollectionName.toString(); -const BSONField<std::string> ShardCollectionType::uuid("_id"); -const BSONField<std::string> ShardCollectionType::ns("ns"); +const BSONField<std::string> ShardCollectionType::ns("_id"); +const BSONField<UUID> ShardCollectionType::uuid("uuid"); const BSONField<OID> ShardCollectionType::epoch("epoch"); const BSONField<BSONObj> ShardCollectionType::keyPattern("key"); const BSONField<BSONObj> ShardCollectionType::defaultCollation("defaultCollation"); @@ -52,28 +52,20 @@ const BSONField<bool> ShardCollectionType::refreshing("refreshing"); const BSONField<Date_t> ShardCollectionType::lastRefreshedCollectionVersion( "lastRefreshedCollectionVersion"); -ShardCollectionType::ShardCollectionType(const NamespaceString& uuid, - const NamespaceString& nss, - const OID& epoch, +ShardCollectionType::ShardCollectionType(NamespaceString nss, + boost::optional<UUID> uuid, + OID epoch, const KeyPattern& keyPattern, const BSONObj& defaultCollation, - const bool& unique) - : _uuid(uuid), - _nss(nss), - _epoch(epoch), + bool unique) + : _nss(std::move(nss)), + _uuid(uuid), + _epoch(std::move(epoch)), _keyPattern(keyPattern.toBSON()), _defaultCollation(defaultCollation.getOwned()), _unique(unique) {} StatusWith<ShardCollectionType> ShardCollectionType::fromBSON(const BSONObj& source) { - NamespaceString uuid; - { - std::string ns; - Status status = bsonExtractStringField(source, ShardCollectionType::uuid.name(), &ns); - if (!status.isOK()) - return status; - uuid = NamespaceString{ns}; - } NamespaceString nss; { @@ -85,6 +77,23 @@ StatusWith<ShardCollectionType> ShardCollectionType::fromBSON(const BSONObj& sou nss = NamespaceString{ns}; } + boost::optional<UUID> uuid; + { + BSONElement uuidElem; + Status status = bsonExtractTypedField( + source, ShardCollectionType::uuid.name(), BSONType::BinData, &uuidElem); + if (status.isOK()) { + auto uuidWith = UUID::parse(uuidElem); + if (!uuidWith.isOK()) + return uuidWith.getStatus(); + uuid = uuidWith.getValue(); + } else if (status == ErrorCodes::NoSuchKey) { + // The field is not set, which is okay. + } else { + return status; + } + } + OID epoch; { BSONElement oidElem; @@ -134,7 +143,8 @@ StatusWith<ShardCollectionType> ShardCollectionType::fromBSON(const BSONObj& sou } } - ShardCollectionType shardCollectionType(uuid, nss, epoch, pattern, collation, unique); + ShardCollectionType shardCollectionType( + std::move(nss), uuid, std::move(epoch), pattern, collation, unique); // Below are optional fields. @@ -170,8 +180,10 @@ StatusWith<ShardCollectionType> ShardCollectionType::fromBSON(const BSONObj& sou BSONObj ShardCollectionType::toBSON() const { BSONObjBuilder builder; - builder.append(uuid.name(), _uuid.ns()); builder.append(ns.name(), _nss.ns()); + if (_uuid) { + _uuid->appendToBuilder(&builder, uuid.name()); + } builder.append(epoch.name(), _epoch); builder.append(keyPattern.name(), _keyPattern.toBSON()); @@ -196,19 +208,18 @@ std::string ShardCollectionType::toString() const { return toBSON().toString(); } -void ShardCollectionType::setUUID(const NamespaceString& uuid) { - invariant(uuid.isValid()); +void ShardCollectionType::setUUID(UUID uuid) { _uuid = uuid; } -void ShardCollectionType::setNss(const NamespaceString& nss) { +void ShardCollectionType::setNss(NamespaceString nss) { invariant(nss.isValid()); - _nss = nss; + _nss = std::move(nss); } -void ShardCollectionType::setEpoch(const OID& epoch) { +void ShardCollectionType::setEpoch(OID epoch) { invariant(epoch.isSet()); - _epoch = epoch; + _epoch = std::move(epoch); } void ShardCollectionType::setKeyPattern(const KeyPattern& keyPattern) { diff --git a/src/mongo/s/catalog/type_shard_collection.h b/src/mongo/s/catalog/type_shard_collection.h index aa17812e980..8129943ade0 100644 --- a/src/mongo/s/catalog/type_shard_collection.h +++ b/src/mongo/s/catalog/type_shard_collection.h @@ -35,6 +35,7 @@ #include "mongo/db/keypattern.h" #include "mongo/db/namespace_string.h" #include "mongo/s/chunk_version.h" +#include "mongo/util/uuid.h" namespace mongo { @@ -50,8 +51,8 @@ class StatusWith; * * Expected shard server config.collections collection format: * { - * "_id" : "foo.bar", // will become UUID when available - * "ns" : "foo.bar", + * "_id" : "foo.bar", + * "uuid" : UUID, // optional in 3.6 * "epoch" : ObjectId("58b6fd76132358839e409e47"), // will remove when UUID becomes available * "key" : { * "_id" : 1 @@ -60,8 +61,8 @@ class StatusWith; * "locale" : "fr_CA" * }, * "unique" : false, - * "refreshing" : true, - * "lastRefreshedCollectionVersion" : Timestamp(1, 0) + * "refreshing" : true, // optional + * "lastRefreshedCollectionVersion" : Timestamp(1, 0) // optional * } * */ @@ -70,8 +71,8 @@ public: // Name of the collections collection on the shard server. static const std::string ConfigNS; - static const BSONField<std::string> uuid; - static const BSONField<std::string> ns; + static const BSONField<std::string> ns; // "_id" + static const BSONField<UUID> uuid; static const BSONField<OID> epoch; static const BSONField<BSONObj> keyPattern; static const BSONField<BSONObj> defaultCollation; @@ -79,12 +80,12 @@ public: static const BSONField<bool> refreshing; static const BSONField<Date_t> lastRefreshedCollectionVersion; - explicit ShardCollectionType(const NamespaceString& uuid, - const NamespaceString& nss, - const OID& epoch, - const KeyPattern& keyPattern, - const BSONObj& defaultCollation, - const bool& unique); + ShardCollectionType(NamespaceString nss, + boost::optional<UUID> uuid, + OID epoch, + const KeyPattern& keyPattern, + const BSONObj& defaultCollation, + bool unique); /** * Constructs a new ShardCollectionType object from BSON. Also does validation of the contents. @@ -101,20 +102,20 @@ public: */ std::string toString() const; - const NamespaceString& getUUID() const { - return _uuid; - } - void setUUID(const NamespaceString& uuid); - const NamespaceString& getNss() const { return _nss; } - void setNss(const NamespaceString& nss); + void setNss(NamespaceString nss); + + const boost::optional<UUID> getUUID() const { + return _uuid; + } + void setUUID(UUID uuid); const OID& getEpoch() const { return _epoch; } - void setEpoch(const OID& epoch); + void setEpoch(OID epoch); const KeyPattern& getKeyPattern() const { return _keyPattern; @@ -152,12 +153,12 @@ public: } private: - // Will become the UUID when available. Currently a duplicate of '_nss'. - NamespaceString _uuid; - // The full namespace (with the database prefix). NamespaceString _nss; + // The UUID of the collection, if known. + boost::optional<UUID> _uuid; + // Uniquely identifies this instance of the collection, in case of drop/create. OID _epoch; diff --git a/src/mongo/s/catalog/type_shard_collection_test.cpp b/src/mongo/s/catalog/type_shard_collection_test.cpp index 51a8b6758ca..5d31d5940ad 100644 --- a/src/mongo/s/catalog/type_shard_collection_test.cpp +++ b/src/mongo/s/catalog/type_shard_collection_test.cpp @@ -48,11 +48,12 @@ const BSONObj kDefaultCollation = BSON("locale" TEST(ShardCollectionType, ToFromBSON) { const OID epoch = OID::gen(); + const UUID uuid = UUID::gen(); const ChunkVersion lastRefreshedCollectionVersion(2, 0, epoch); BSONObjBuilder builder; - builder.append(ShardCollectionType::uuid.name(), kNss.ns()); builder.append(ShardCollectionType::ns.name(), kNss.ns()); + uuid.appendToBuilder(&builder, ShardCollectionType::uuid.name()); builder.append(ShardCollectionType::epoch(), epoch); builder.append(ShardCollectionType::keyPattern.name(), kKeyPattern); builder.append(ShardCollectionType::defaultCollation(), kDefaultCollation); @@ -64,8 +65,9 @@ TEST(ShardCollectionType, ToFromBSON) { ShardCollectionType shardCollectionType = assertGet(ShardCollectionType::fromBSON(obj)); - ASSERT_EQUALS(shardCollectionType.getUUID(), kNss); ASSERT_EQUALS(shardCollectionType.getNss(), kNss); + ASSERT(shardCollectionType.getUUID()); + ASSERT_EQUALS(*shardCollectionType.getUUID(), uuid); ASSERT_EQUALS(shardCollectionType.getEpoch(), epoch); ASSERT_BSONOBJ_EQ(shardCollectionType.getKeyPattern().toBSON(), kKeyPattern); ASSERT_BSONOBJ_EQ(shardCollectionType.getDefaultCollation(), kDefaultCollation); @@ -81,7 +83,6 @@ TEST(ShardCollectionType, ToFromShardBSONWithoutOptionals) { const OID epoch = OID::gen(); BSONObjBuilder builder; - builder.append(ShardCollectionType::uuid.name(), kNss.ns()); builder.append(ShardCollectionType::ns.name(), kNss.ns()); builder.append(ShardCollectionType::epoch(), epoch); builder.append(ShardCollectionType::keyPattern.name(), kKeyPattern); @@ -91,12 +92,13 @@ TEST(ShardCollectionType, ToFromShardBSONWithoutOptionals) { ShardCollectionType shardCollectionType = assertGet(ShardCollectionType::fromBSON(obj)); - ASSERT_EQUALS(shardCollectionType.getUUID(), kNss); ASSERT_EQUALS(shardCollectionType.getNss(), kNss); ASSERT_EQUALS(shardCollectionType.getEpoch(), epoch); ASSERT_BSONOBJ_EQ(shardCollectionType.getKeyPattern().toBSON(), kKeyPattern); ASSERT_BSONOBJ_EQ(shardCollectionType.getDefaultCollation(), kDefaultCollation); ASSERT_EQUALS(shardCollectionType.getUnique(), true); + ASSERT_FALSE(shardCollectionType.hasRefreshing()); + ASSERT_FALSE(shardCollectionType.hasLastRefreshedCollectionVersion()); ASSERT_BSONOBJ_EQ(obj, shardCollectionType.toBSON()); } @@ -106,38 +108,45 @@ TEST(ShardCollectionType, FromEmptyBSON) { ASSERT_FALSE(status.isOK()); } -TEST(ShardCollectionType, FromBSONNoUUIDFails) { - BSONObj obj = - BSON(ShardCollectionType::ns(kNss.ns()) << ShardCollectionType::keyPattern(kKeyPattern)); - - StatusWith<ShardCollectionType> status = ShardCollectionType::fromBSON(obj); - ASSERT_EQUALS(status.getStatus().code(), ErrorCodes::NoSuchKey); - ASSERT_STRING_CONTAINS(status.getStatus().reason(), ShardCollectionType::uuid()); +TEST(ShardCollectionType, FromBSONNoUUIDIsOK) { + BSONObjBuilder builder; + builder.append(ShardCollectionType::ns.name(), kNss.ns()); + builder.append(ShardCollectionType::epoch(), OID::gen()); + builder.append(ShardCollectionType::keyPattern(), kKeyPattern); + builder.append(ShardCollectionType::unique(), true); + StatusWith<ShardCollectionType> status = ShardCollectionType::fromBSON(builder.obj()); + ASSERT_OK(status.getStatus()); + ASSERT_FALSE(status.getValue().getUUID()); } TEST(ShardCollectionType, FromBSONNoNSFails) { - BSONObj obj = - BSON(ShardCollectionType::uuid(kNss.ns()) << ShardCollectionType::keyPattern(kKeyPattern)); - - StatusWith<ShardCollectionType> status = ShardCollectionType::fromBSON(obj); + BSONObjBuilder builder; + UUID::gen().appendToBuilder(&builder, ShardCollectionType::uuid.name()); + builder.append(ShardCollectionType::epoch(), OID::gen()); + builder.append(ShardCollectionType::keyPattern(), kKeyPattern); + builder.append(ShardCollectionType::unique(), true); + StatusWith<ShardCollectionType> status = ShardCollectionType::fromBSON(builder.obj()); ASSERT_EQUALS(status.getStatus().code(), ErrorCodes::NoSuchKey); ASSERT_STRING_CONTAINS(status.getStatus().reason(), ShardCollectionType::ns()); } TEST(ShardCollectionType, FromBSONNoEpochFails) { - BSONObj obj = BSON(ShardCollectionType::uuid(kNss.ns()) << ShardCollectionType::ns(kNss.ns())); - - StatusWith<ShardCollectionType> status = ShardCollectionType::fromBSON(obj); + BSONObjBuilder builder; + builder.append(ShardCollectionType::ns.name(), kNss.ns()); + UUID::gen().appendToBuilder(&builder, ShardCollectionType::uuid.name()); + builder.append(ShardCollectionType::keyPattern(), kKeyPattern); + builder.append(ShardCollectionType::unique(), true); + StatusWith<ShardCollectionType> status = ShardCollectionType::fromBSON(builder.obj()); ASSERT_EQUALS(status.getStatus().code(), ErrorCodes::NoSuchKey); ASSERT_STRING_CONTAINS(status.getStatus().reason(), ShardCollectionType::epoch()); } TEST(ShardCollectionType, FromBSONNoShardKeyFails) { BSONObjBuilder builder; - builder.append(ShardCollectionType::uuid.name(), kNss.ns()); builder.append(ShardCollectionType::ns.name(), kNss.ns()); + UUID::gen().appendToBuilder(&builder, ShardCollectionType::uuid.name()); builder.append(ShardCollectionType::epoch(), OID::gen()); - + builder.append(ShardCollectionType::unique(), true); StatusWith<ShardCollectionType> status = ShardCollectionType::fromBSON(builder.obj()); ASSERT_EQUALS(status.getStatus().code(), ErrorCodes::NoSuchKey); ASSERT_STRING_CONTAINS(status.getStatus().reason(), ShardCollectionType::keyPattern()); @@ -145,12 +154,11 @@ TEST(ShardCollectionType, FromBSONNoShardKeyFails) { TEST(ShardCollectionType, FromBSONNoUniqueFails) { BSONObjBuilder builder; - builder.append(ShardCollectionType::uuid.name(), kNss.ns()); builder.append(ShardCollectionType::ns.name(), kNss.ns()); + UUID::gen().appendToBuilder(&builder, ShardCollectionType::uuid.name()); builder.append(ShardCollectionType::epoch(), OID::gen()); builder.append(ShardCollectionType::keyPattern.name(), kKeyPattern); builder.append(ShardCollectionType::defaultCollation(), kDefaultCollation); - StatusWith<ShardCollectionType> status = ShardCollectionType::fromBSON(builder.obj()); ASSERT_EQUALS(status.getStatus().code(), ErrorCodes::NoSuchKey); ASSERT_STRING_CONTAINS(status.getStatus().reason(), ShardCollectionType::unique()); @@ -158,8 +166,8 @@ TEST(ShardCollectionType, FromBSONNoUniqueFails) { TEST(ShardCollectionType, FromBSONNoDefaultCollationIsOK) { BSONObjBuilder builder; - builder.append(ShardCollectionType::uuid.name(), kNss.ns()); builder.append(ShardCollectionType::ns.name(), kNss.ns()); + UUID::gen().appendToBuilder(&builder, ShardCollectionType::uuid.name()); builder.append(ShardCollectionType::epoch(), OID::gen()); builder.append(ShardCollectionType::keyPattern.name(), kKeyPattern); builder.append(ShardCollectionType::unique(), true); |