diff options
-rw-r--r-- | jstests/sharding/libs/track_unsharded_collections_helpers.js | 81 | ||||
-rw-r--r-- | src/mongo/db/s/resharding/resharding_coordinator_service.cpp | 1 | ||||
-rw-r--r-- | src/mongo/db/s/resharding/resharding_coordinator_test.cpp | 1 | ||||
-rw-r--r-- | src/mongo/db/s/shardsvr_shard_collection.cpp | 8 | ||||
-rw-r--r-- | src/mongo/s/catalog/sharding_catalog_client_impl.cpp | 3 | ||||
-rw-r--r-- | src/mongo/s/catalog/type_collection.cpp | 60 | ||||
-rw-r--r-- | src/mongo/s/catalog/type_collection.h | 44 | ||||
-rw-r--r-- | src/mongo/s/catalog/type_collection_test.cpp | 171 |
8 files changed, 12 insertions, 357 deletions
diff --git a/jstests/sharding/libs/track_unsharded_collections_helpers.js b/jstests/sharding/libs/track_unsharded_collections_helpers.js deleted file mode 100644 index 739118f9c0b..00000000000 --- a/jstests/sharding/libs/track_unsharded_collections_helpers.js +++ /dev/null @@ -1,81 +0,0 @@ -/** - * Common helpers for testing operations on unsharded collections. - */ - -function getNewNs(dbName) { - if (typeof getNewNs.counter == 'undefined') { - getNewNs.counter = 0; - } - getNewNs.counter++; - const collName = "ns" + getNewNs.counter; - return [collName, dbName + "." + collName]; -} - -function checkInStorageCatalog({dbName, collName, type, shardConn}) { - const query = {name: collName, type}; - assert.eq(1, - shardConn.getDB(dbName).getCollectionInfos(query).length, - "Current contents of storage catalog on " + shardConn + ": " + - tojson(shardConn.getDB(dbName).getCollectionInfos()) + - ", query: " + tojson(query)); -} - -function checkNotInStorageCatalog({dbName, collName, shardConn}) { - const query = {name: collName}; - assert.eq(0, - shardConn.getDB(dbName).getCollectionInfos(query).length, - "Current contents of storage catalog on " + shardConn + ": " + - tojson(shardConn.getDB(dbName).getCollectionInfos()) + - ", query: " + tojson(query)); -} - -function checkInShardingCatalog({ns, shardKey, unique, distributionMode, numChunks, mongosConn}) { - const configDB = mongosConn.getDB("config"); - - // Check the collection entry. - const collQuery = {}; - collQuery._id = ns; - collQuery["key." + shardKey] = 1; - collQuery.unique = unique; - collQuery.distributionMode = distributionMode; - assert.neq(null, - configDB.collections.findOne(collQuery), - "Current contents of config.collections: " + - tojson(configDB.collections.find().toArray()) + ", query: " + tojson(collQuery)); - - // Check the chunk entries. - const chunkQuery = {}; - chunkQuery.ns = ns; - chunkQuery["min." + shardKey] = {$exists: true}; - chunkQuery["max." + shardKey] = {$exists: true}; - assert.eq(numChunks, - configDB.chunks.count(chunkQuery), - "Current contents of config.chunks: " + tojson(configDB.chunks.find().toArray()) + - ", query: " + tojson(chunkQuery)); -} - -function checkNotInShardingCatalog({ns, mongosConn}) { - const configDB = mongosConn.getDB("config"); - - assert.eq( - null, - configDB.collections.findOne({_id: ns}), - "Current contents of config.collections: " + tojson(configDB.collections.find().toArray())); - assert.eq(0, - configDB.chunks.count({ns: ns}), - "Current contents of config.chunks: " + tojson(configDB.chunks.find().toArray())); -} - -function setFailpoint(failpointName, conn) { - assert.commandWorked(conn.adminCommand({ - configureFailPoint: failpointName, - mode: "alwaysOn", - })); -} - -function unsetFailpoint(failpointName, conn) { - assert.commandWorked(conn.adminCommand({ - configureFailPoint: failpointName, - mode: "off", - })); -} diff --git a/src/mongo/db/s/resharding/resharding_coordinator_service.cpp b/src/mongo/db/s/resharding/resharding_coordinator_service.cpp index d9ef0c52d6c..ecf4adfbf5d 100644 --- a/src/mongo/db/s/resharding/resharding_coordinator_service.cpp +++ b/src/mongo/db/s/resharding/resharding_coordinator_service.cpp @@ -491,7 +491,6 @@ CollectionType createTempReshardingCollectionType( collType.setKeyPattern(coordinatorDoc.getReshardingKey()); collType.setDefaultCollation(collation); collType.setUnique(false); - collType.setDistributionMode(CollectionType::DistributionMode::kSharded); TypeCollectionReshardingFields tempEntryReshardingFields(coordinatorDoc.get_id()); tempEntryReshardingFields.setState(coordinatorDoc.getState()); diff --git a/src/mongo/db/s/resharding/resharding_coordinator_test.cpp b/src/mongo/db/s/resharding/resharding_coordinator_test.cpp index b19a9073eea..43b32b724ae 100644 --- a/src/mongo/db/s/resharding/resharding_coordinator_test.cpp +++ b/src/mongo/db/s/resharding/resharding_coordinator_test.cpp @@ -115,7 +115,6 @@ protected: coordinatorDoc.getNss(), std::move(epoch), lastUpdated, std::move(uuid)); collType.setKeyPattern(shardKey); collType.setUnique(false); - collType.setDistributionMode(CollectionType::DistributionMode::kSharded); if (reshardingFields) collType.setReshardingFields(std::move(reshardingFields.get())); diff --git a/src/mongo/db/s/shardsvr_shard_collection.cpp b/src/mongo/db/s/shardsvr_shard_collection.cpp index a90e9319037..ec172a3b1dc 100644 --- a/src/mongo/db/s/shardsvr_shard_collection.cpp +++ b/src/mongo/db/s/shardsvr_shard_collection.cpp @@ -124,13 +124,6 @@ boost::optional<CollectionType> checkIfCollectionAlreadyShardedWithSameOptions( newColl.setDefaultCollation(*request.getCollation()); newColl.setUnique(request.getUnique()); - // Set the distributionMode to "sharded" because this CollectionType represents the requested - // target state for the collection after shardCollection. The requested CollectionType will be - // compared with the existing CollectionType below, and if the existing CollectionType either - // does not have a distributionMode (FCV 4.2) or has distributionMode "sharded" (FCV 4.4), the - // collection will be considered to already be in its target state. - newColl.setDistributionMode(CollectionType::DistributionMode::kSharded); - // If the collection is already sharded, fail if the deduced options in this request do not // match the options the collection was originally sharded with. uassert(ErrorCodes::AlreadyInitialized, @@ -485,7 +478,6 @@ void updateShardingCatalogEntryForCollection( coll.setDefaultCollation(defaultCollator->getSpec().toBSON()); } coll.setUnique(unique); - coll.setDistributionMode(CollectionType::DistributionMode::kSharded); uassertStatusOK(ShardingCatalogClientImpl::updateShardingCatalogEntryForCollection( opCtx, nss, coll, true /*upsert*/)); diff --git a/src/mongo/s/catalog/sharding_catalog_client_impl.cpp b/src/mongo/s/catalog/sharding_catalog_client_impl.cpp index b61d8f13b00..3dd27e755d4 100644 --- a/src/mongo/s/catalog/sharding_catalog_client_impl.cpp +++ b/src/mongo/s/catalog/sharding_catalog_client_impl.cpp @@ -368,8 +368,7 @@ std::vector<NamespaceString> ShardingCatalogClientImpl::getAllShardedCollections std::vector<NamespaceString> collectionsToReturn; for (const auto& coll : collectionsOnConfig) { - if (coll.getDropped() || - coll.getDistributionMode() == CollectionType::DistributionMode::kUnsharded) { + if (coll.getDropped()) { continue; } diff --git a/src/mongo/s/catalog/type_collection.cpp b/src/mongo/s/catalog/type_collection.cpp index 6b42f7aadcb..605c161977c 100644 --- a/src/mongo/s/catalog/type_collection.cpp +++ b/src/mongo/s/catalog/type_collection.cpp @@ -42,8 +42,6 @@ namespace mongo { const NamespaceString CollectionType::ConfigNS("config.collections"); -const BSONField<std::string> CollectionType::distributionMode("distributionMode"); - CollectionType::CollectionType(NamespaceString nss, OID epoch, Date_t updatedAt, UUID uuid) : CollectionTypeBase(std::move(nss), std::move(updatedAt)) { setEpoch(std::move(epoch)); @@ -63,58 +61,12 @@ CollectionType::CollectionType(const BSONObj& obj) { getPre50CompatibleKeyPattern() || getDropped()); } -StatusWith<CollectionType> CollectionType::fromBSON(const BSONObj& source) { - auto swColl = [&] { - try { - return StatusWith<CollectionType>(CollectionType(source)); - } catch (const DBException& ex) { - return StatusWith<CollectionType>(ex.toStatus()); - } - }(); - - if (!swColl.isOK()) - return swColl; - - CollectionType coll = std::move(swColl.getValue()); - - { - std::string collDistributionMode; - Status status = - bsonExtractStringField(source, distributionMode.name(), &collDistributionMode); - if (status.isOK()) { - if (collDistributionMode == "unsharded") { - coll._distributionMode = DistributionMode::kUnsharded; - } else if (collDistributionMode == "sharded") { - coll._distributionMode = DistributionMode::kSharded; - } else { - return {ErrorCodes::FailedToParse, - str::stream() << "Unknown distribution mode " << collDistributionMode}; - } - } else if (status == ErrorCodes::NoSuchKey) { - // In v4.4, distributionMode can be missing in which case it is presumed "sharded" - } else { - return status; - } - } - - return StatusWith<CollectionType>(coll); -} - -BSONObj CollectionType::toBSON() const { - BSONObjBuilder builder; - serialize(&builder); - - if (_distributionMode) { - if (*_distributionMode == DistributionMode::kUnsharded) { - builder.append(distributionMode.name(), "unsharded"); - } else if (*_distributionMode == DistributionMode::kSharded) { - builder.append(distributionMode.name(), "sharded"); - } else { - MONGO_UNREACHABLE; - } +StatusWith<CollectionType> CollectionType::fromBSON(const BSONObj& obj) { + try { + return CollectionType(obj); + } catch (const DBException& ex) { + return ex.toStatus(); } - - return builder.obj(); } std::string CollectionType::toString() const { @@ -144,7 +96,7 @@ bool CollectionType::hasSameOptions(const CollectionType& other) const { other.getKeyPattern().toBSON()) && SimpleBSONObjComparator::kInstance.evaluate(getDefaultCollation() == other.getDefaultCollation()) && - getUnique() == other.getUnique() && getDistributionMode() == other.getDistributionMode(); + getUnique() == other.getUnique(); } } // namespace mongo diff --git a/src/mongo/s/catalog/type_collection.h b/src/mongo/s/catalog/type_collection.h index ae3b0cb659c..6cb529db88e 100644 --- a/src/mongo/s/catalog/type_collection.h +++ b/src/mongo/s/catalog/type_collection.h @@ -55,7 +55,6 @@ using ReshardingFields = TypeCollectionReshardingFields; * "unique" : false, * "uuid" : UUID, * "noBalance" : false, - * "distributionMode" : "unsharded|sharded", * // Only populated if the collection is currently undergoing a resharding operation. * "reshardingFields" : { * "uuid" : UUID, @@ -103,40 +102,19 @@ public: using CollectionTypeBase::setReshardingFields; using CollectionTypeBase::setUnique; using CollectionTypeBase::setUpdatedAt; + using CollectionTypeBase::toBSON; // Name of the collections collection in the config server. static const NamespaceString ConfigNS; - static const BSONField<std::string> distributionMode; - - CollectionType() = default; - CollectionType(NamespaceString nss, OID epoch, Date_t updatedAt, UUID uuid); explicit CollectionType(const BSONObj& obj); - /** - * Constructs a new CollectionType object from BSON. Also does validation of the contents. - * - * Dropped collections accumulate in the collections list, through 3.6, so that - * mongos <= 3.4.x, when it retrieves the list from the config server, can delete its - * cache entries for dropped collections. See SERVER-27475, SERVER-27474 - */ - static StatusWith<CollectionType> fromBSON(const BSONObj& source); - - enum class DistributionMode { - kUnsharded, - kSharded, - }; - - /** - * Returns the BSON representation of the entry. - */ - BSONObj toBSON() const; - - /** - * Returns a std::string representation of the current internal state. - */ + CollectionType() = default; + + static StatusWith<CollectionType> fromBSON(const BSONObj& obj); + std::string toString() const; const OID& getEpoch() const { @@ -167,19 +145,7 @@ public: return !getNoBalance(); } - DistributionMode getDistributionMode() const { - return _distributionMode.get_value_or(DistributionMode::kSharded); - } - void setDistributionMode(DistributionMode distributionMode) { - _distributionMode = distributionMode; - } - bool hasSameOptions(const CollectionType& other) const; - -private: - // New field in v4.4; optional in v4.4 for backwards compatibility with v4.2. Whether the - // collection is unsharded or sharded. If missing, implies sharded. - boost::optional<DistributionMode> _distributionMode; }; } // namespace mongo diff --git a/src/mongo/s/catalog/type_collection_test.cpp b/src/mongo/s/catalog/type_collection_test.cpp index 89155459611..a9c675f1cf3 100644 --- a/src/mongo/s/catalog/type_collection_test.cpp +++ b/src/mongo/s/catalog/type_collection_test.cpp @@ -139,177 +139,6 @@ TEST(CollectionType, DefaultCollationSerializesCorrectly) { << "fr_CA")); } -TEST(CollectionType, MissingDistributionModeImpliesDistributionModeSharded) { - const OID oid = OID::gen(); - StatusWith<CollectionType> status = CollectionType::fromBSON( - BSON(CollectionType::kNssFieldName << "db.coll" << CollectionType::kEpochFieldName << oid - << CollectionType::kUpdatedAtFieldName - << Date_t::fromMillisSinceEpoch(1) - << CollectionType::kKeyPatternFieldName << BSON("a" << 1) - << CollectionType::kUniqueFieldName << true)); - ASSERT_TRUE(status.isOK()); - - CollectionType coll = status.getValue(); - ASSERT(CollectionType::DistributionMode::kSharded == coll.getDistributionMode()); - - // Since the distributionMode was not explicitly set, it does not get serialized. - BSONObj serialized = coll.toBSON(); - ASSERT_FALSE(serialized["distributionMode"]); -} - -TEST(CollectionType, DistributionModeUnshardedParses) { - const OID oid = OID::gen(); - StatusWith<CollectionType> status = CollectionType::fromBSON( - BSON(CollectionType::kNssFieldName << "db.coll" << CollectionType::kEpochFieldName << oid - << CollectionType::kUpdatedAtFieldName - << Date_t::fromMillisSinceEpoch(1) - << CollectionType::kKeyPatternFieldName << BSON("a" << 1) - << CollectionType::kUniqueFieldName << true - << CollectionType::distributionMode("unsharded"))); - ASSERT_TRUE(status.isOK()); - - CollectionType coll = status.getValue(); - ASSERT(CollectionType::DistributionMode::kUnsharded == coll.getDistributionMode()); - - BSONObj serialized = coll.toBSON(); - ASSERT("unsharded" == serialized["distributionMode"].str()); -} - -TEST(CollectionType, DistributionModeShardedParses) { - const OID oid = OID::gen(); - StatusWith<CollectionType> status = CollectionType::fromBSON( - BSON(CollectionType::kNssFieldName << "db.coll" << CollectionType::kEpochFieldName << oid - << CollectionType::kUpdatedAtFieldName - << Date_t::fromMillisSinceEpoch(1) - << CollectionType::kKeyPatternFieldName << BSON("a" << 1) - << CollectionType::kUniqueFieldName << true - << CollectionType::distributionMode("sharded"))); - ASSERT_TRUE(status.isOK()); - - CollectionType coll = status.getValue(); - ASSERT(CollectionType::DistributionMode::kSharded == coll.getDistributionMode()); - - BSONObj serialized = coll.toBSON(); - ASSERT("sharded" == serialized["distributionMode"].str()); -} - -TEST(CollectionType, UnknownDistributionModeFailsToParse) { - const OID oid = OID::gen(); - StatusWith<CollectionType> status = CollectionType::fromBSON( - BSON(CollectionType::kNssFieldName << "db.coll" << CollectionType::kEpochFieldName << oid - << CollectionType::kUpdatedAtFieldName - << Date_t::fromMillisSinceEpoch(1) - << CollectionType::kKeyPatternFieldName << BSON("a" << 1) - << CollectionType::kUniqueFieldName << true - << CollectionType::distributionMode("badvalue"))); - ASSERT_EQUALS(ErrorCodes::FailedToParse, status.getStatus()); -} - -TEST(CollectionType, HasSameOptionsReturnsTrueIfBothDistributionModesExplicitlySetToUnsharded) { - const auto collType1 = uassertStatusOK(CollectionType::fromBSON( - BSON(CollectionType::kNssFieldName << "db.coll" << CollectionType::kEpochFieldName - << OID::gen() << CollectionType::kUpdatedAtFieldName - << Date_t::fromMillisSinceEpoch(1) - << CollectionType::kKeyPatternFieldName << BSON("a" << 1) - << CollectionType::kUniqueFieldName << true - << CollectionType::distributionMode("unsharded")))); - - const auto collType2 = uassertStatusOK(CollectionType::fromBSON( - BSON(CollectionType::kNssFieldName << "db.coll" << CollectionType::kEpochFieldName - << OID::gen() << CollectionType::kUpdatedAtFieldName - << Date_t::fromMillisSinceEpoch(1) - << CollectionType::kKeyPatternFieldName << BSON("a" << 1) - << CollectionType::kUniqueFieldName << true - << CollectionType::distributionMode("unsharded")))); - - ASSERT(collType1.hasSameOptions(collType2)); - ASSERT(collType2.hasSameOptions(collType1)); -} - -TEST(CollectionType, HasSameOptionsReturnsTrueIfBothDistributionModesExplicitlySetToSharded) { - const auto collType1 = uassertStatusOK(CollectionType::fromBSON( - BSON(CollectionType::kNssFieldName << "db.coll" << CollectionType::kEpochFieldName - << OID::gen() << CollectionType::kUpdatedAtFieldName - << Date_t::fromMillisSinceEpoch(1) - << CollectionType::kKeyPatternFieldName << BSON("a" << 1) - << CollectionType::kUniqueFieldName << true - << CollectionType::distributionMode("sharded")))); - - const auto collType2 = uassertStatusOK(CollectionType::fromBSON( - BSON(CollectionType::kNssFieldName << "db.coll" << CollectionType::kEpochFieldName - << OID::gen() << CollectionType::kUpdatedAtFieldName - << Date_t::fromMillisSinceEpoch(1) - << CollectionType::kKeyPatternFieldName << BSON("a" << 1) - << CollectionType::kUniqueFieldName << true - << CollectionType::distributionMode("sharded")))); - - ASSERT(collType1.hasSameOptions(collType2)); - ASSERT(collType2.hasSameOptions(collType1)); -} - -TEST( - CollectionType, - HasSameOptionsReturnsFalseIfOneDistributionModeExplicitlySetToUnshardedAndOtherExplicitlySetToSharded) { - const auto collType1 = uassertStatusOK(CollectionType::fromBSON( - BSON(CollectionType::kNssFieldName << "db.coll" << CollectionType::kEpochFieldName - << OID::gen() << CollectionType::kUpdatedAtFieldName - << Date_t::fromMillisSinceEpoch(1) - << CollectionType::kKeyPatternFieldName << BSON("a" << 1) - << CollectionType::kUniqueFieldName << true - << CollectionType::distributionMode("unsharded")))); - - const auto collType2 = uassertStatusOK(CollectionType::fromBSON( - BSON(CollectionType::kNssFieldName << "db.coll" << CollectionType::kEpochFieldName - << OID::gen() << CollectionType::kUpdatedAtFieldName - << Date_t::fromMillisSinceEpoch(1) - << CollectionType::kKeyPatternFieldName << BSON("a" << 1) - << CollectionType::kUniqueFieldName << true - << CollectionType::distributionMode("sharded")))); - - ASSERT(!collType1.hasSameOptions(collType2)); - ASSERT(!collType2.hasSameOptions(collType1)); -} - -TEST(CollectionType, - HasSameOptionsReturnsTrueIfOneDistributionModeExplicitlySetToShardedAndOtherIsNotSet) { - const auto collType1 = uassertStatusOK(CollectionType::fromBSON( - BSON(CollectionType::kNssFieldName << "db.coll" << CollectionType::kEpochFieldName - << OID::gen() << CollectionType::kUpdatedAtFieldName - << Date_t::fromMillisSinceEpoch(1) - << CollectionType::kKeyPatternFieldName << BSON("a" << 1) - << CollectionType::kUniqueFieldName << true - << CollectionType::distributionMode("sharded")))); - - const auto collType2 = uassertStatusOK(CollectionType::fromBSON( - BSON(CollectionType::kNssFieldName << "db.coll" << CollectionType::kEpochFieldName - << OID::gen() << CollectionType::kUpdatedAtFieldName - << Date_t::fromMillisSinceEpoch(1) - << CollectionType::kKeyPatternFieldName << BSON("a" << 1) - << CollectionType::kUniqueFieldName << true))); - - ASSERT(collType1.hasSameOptions(collType2)); - ASSERT(collType2.hasSameOptions(collType1)); -} - -TEST(CollectionType, HasSameOptionsReturnsTrueIfNeitherDistributionModeExplicitlySet) { - const auto collType1 = uassertStatusOK(CollectionType::fromBSON( - BSON(CollectionType::kNssFieldName << "db.coll" << CollectionType::kEpochFieldName - << OID::gen() << CollectionType::kUpdatedAtFieldName - << Date_t::fromMillisSinceEpoch(1) - << CollectionType::kKeyPatternFieldName << BSON("a" << 1) - << CollectionType::kUniqueFieldName << true))); - - const auto collType2 = uassertStatusOK(CollectionType::fromBSON( - BSON(CollectionType::kNssFieldName << "db.coll" << CollectionType::kEpochFieldName - << OID::gen() << CollectionType::kUpdatedAtFieldName - << Date_t::fromMillisSinceEpoch(1) - << CollectionType::kKeyPatternFieldName << BSON("a" << 1) - << CollectionType::kUniqueFieldName << true))); - - ASSERT(collType1.hasSameOptions(collType2)); - ASSERT(collType2.hasSameOptions(collType1)); -} - TEST(CollectionType, Pre22Format) { CollectionType coll = assertGet( CollectionType::fromBSON(BSON("_id" |