summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArun Banala <arun.banala@mongodb.com>2021-10-04 20:20:28 +0100
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-10-05 07:59:51 +0000
commit9833bf4830fbfba67cbea4a6ae42d58bd6866a6f (patch)
treea8d547b1d03cb4fc7aa8fd99410f8329144c3210
parentb322eb31ae089275f15a2a6ad4b4a08685e0a956 (diff)
downloadmongo-9833bf4830fbfba67cbea4a6ae42d58bd6866a6f.tar.gz
SERVER-60452 Fix validation when the collection does not exist
-rw-r--r--jstests/sharding/timeseries_shard_collection.js236
-rw-r--r--src/mongo/db/s/shardsvr_create_collection_command.cpp37
2 files changed, 144 insertions, 129 deletions
diff --git a/jstests/sharding/timeseries_shard_collection.js b/jstests/sharding/timeseries_shard_collection.js
index c6c92811730..e8d578f15cb 100644
--- a/jstests/sharding/timeseries_shard_collection.js
+++ b/jstests/sharding/timeseries_shard_collection.js
@@ -170,131 +170,145 @@ if (TimeseriesTest.shardedtimeseriesCollectionsEnabled(st.shard0)) {
}
// Shard key on the hashed field.
- (function hashAndTimeShardKey() {
- assert.commandWorked(
- sDB.createCollection('ts', {timeseries: {timeField: 'time', metaField: 'hostId'}}));
-
- assert.commandWorked(st.s.adminCommand({enableSharding: 'test'}));
-
- // Only range is allowed on time field.
- assert.commandFailedWithCode(st.s.adminCommand({
- shardCollection: 'test.ts',
- key: {time: 'hashed'},
- timeseries: {timeField: 'time', metaField: 'hostId'},
- }),
- ErrorCodes.BadValue);
-
- let coll = sDB.getCollection('ts');
- assert.commandWorked(coll.insert([
- {hostId: 10, time: ISODate(`1901-01-01`)},
- {hostId: 11, time: ISODate(`1902-01-01`)},
- ]));
- assert.commandWorked(coll.createIndex({hostId: 'hashed'}));
-
- assert.commandWorked(st.s.adminCommand({
- shardCollection: 'test.ts',
- key: {hostId: 'hashed'},
- timeseries: {timeField: 'time', metaField: 'hostId'}
- }));
-
- validateBucketsCollectionSharded({
- collName: 'ts',
- shardKey: {meta: 'hashed'},
- timeSeriesParams: {timeField: 'time', metaField: 'hostId'}
- });
-
- assert.eq(coll.find().itcount(), 2); // Validate count after sharding.
- let insertCount = timeseriesInsert(coll);
- assert.eq(coll.find().itcount(), insertCount + 2);
- coll.drop();
- assert.commandWorked(
- sDB.createCollection('ts', {timeseries: {timeField: 'time', metaField: 'hostId'}}));
- assert.commandWorked(st.s.adminCommand({enableSharding: 'test'}));
+ function runShardKeyPatternValidation(collectionExists) {
+ (function hashAndTimeShardKey() {
+ if (collectionExists) {
+ assert.commandWorked(sDB.createCollection(
+ 'ts', {timeseries: {timeField: 'time', metaField: 'hostId'}}));
+ }
- // Sharding key with hashed meta field and time field.
- assert.commandWorked(st.s.adminCommand({
- shardCollection: 'test.ts',
- key: {hostId: 'hashed', time: 1},
- timeseries: {timeField: 'time', metaField: 'hostId'},
- numInitialChunks: 2
- }));
+ // Only range is allowed on time field.
+ assert.commandFailedWithCode(st.s.adminCommand({
+ shardCollection: 'test.ts',
+ key: {time: 'hashed'},
+ timeseries: {timeField: 'time', metaField: 'hostId'},
+ }),
+ ErrorCodes.BadValue);
- coll = sDB.getCollection('ts');
- assert.eq(coll.find().itcount(), 0);
- insertCount = timeseriesInsert(coll);
- assert.eq(coll.find().itcount(), insertCount);
- coll.drop();
- })();
+ if (!collectionExists) {
+ assert.commandWorked(sDB.createCollection(
+ 'ts', {timeseries: {timeField: 'time', metaField: 'hostId'}}));
+ }
+ let coll = sDB.getCollection('ts');
+ assert.commandWorked(coll.insert([
+ {hostId: 10, time: ISODate(`1901-01-01`)},
+ {hostId: 11, time: ISODate(`1902-01-01`)},
+ ]));
+ assert.commandWorked(coll.createIndex({hostId: 'hashed'}));
- // Test that invalid shard keys fail.
- (function invalidShardKeyPatterns() {
- assert.commandWorked(
- sDB.createCollection('ts', {timeseries: {timeField: 'time', metaField: 'hostId'}}));
- assert.commandWorked(st.s.adminCommand({enableSharding: 'test'}));
+ assert.commandWorked(st.s.adminCommand({
+ shardCollection: 'test.ts',
+ key: {hostId: 'hashed'},
+ timeseries: {timeField: 'time', metaField: 'hostId'}
+ }));
+
+ validateBucketsCollectionSharded({
+ collName: 'ts',
+ shardKey: {meta: 'hashed'},
+ timeSeriesParams: {timeField: 'time', metaField: 'hostId'}
+ });
+
+ assert.eq(coll.find().itcount(), 2); // Validate count after sharding.
+ let insertCount = timeseriesInsert(coll);
+ assert.eq(coll.find().itcount(), insertCount + 2);
+ coll.drop();
+
+ if (collectionExists) {
+ assert.commandWorked(sDB.createCollection(
+ 'ts', {timeseries: {timeField: 'time', metaField: 'hostId'}}));
+ }
+ assert.commandWorked(st.s.adminCommand({enableSharding: 'test'}));
+
+ // Sharding key with hashed meta field and time field.
+ assert.commandWorked(st.s.adminCommand({
+ shardCollection: 'test.ts',
+ key: {hostId: 'hashed', time: 1},
+ timeseries: {timeField: 'time', metaField: 'hostId'},
+ numInitialChunks: 2
+ }));
+
+ coll = sDB.getCollection('ts');
+ assert.eq(coll.find().itcount(), 0);
+ insertCount = timeseriesInsert(coll);
+ assert.eq(coll.find().itcount(), insertCount);
+ coll.drop();
+ })();
+
+ // Test that invalid shard keys fail.
+ (function invalidShardKeyPatterns() {
+ if (collectionExists) {
+ assert.commandWorked(sDB.createCollection(
+ 'ts', {timeseries: {timeField: 'time', metaField: 'hostId'}}));
+ }
+
+ // No other fields, including _id, are allowed in the shard key pattern
+ assert.commandFailedWithCode(st.s.adminCommand({
+ shardCollection: 'test.ts',
+ key: {_id: 1},
+ timeseries: {timeField: 'time', metaField: 'hostId'},
+ }),
+ 5914001);
- // No other fields, including _id, are allowed in the shard key pattern
- assert.commandFailedWithCode(st.s.adminCommand({
- shardCollection: 'test.ts',
- key: {_id: 1},
- timeseries: {timeField: 'time', metaField: 'hostId'},
- }),
- 5914001);
+ assert.commandFailedWithCode(st.s.adminCommand({
+ shardCollection: 'test.ts',
+ key: {_id: 1, time: 1},
+ timeseries: {timeField: 'time', metaField: 'hostId'},
+ }),
+ 5914001);
- assert.commandFailedWithCode(st.s.adminCommand({
- shardCollection: 'test.ts',
- key: {_id: 1, time: 1},
- timeseries: {timeField: 'time', metaField: 'hostId'},
- }),
- 5914001);
+ assert.commandFailedWithCode(st.s.adminCommand({
+ shardCollection: 'test.ts',
+ key: {_id: 1, hostId: 1},
+ timeseries: {timeField: 'time', metaField: 'hostId'},
+ }),
+ 5914001);
- assert.commandFailedWithCode(st.s.adminCommand({
- shardCollection: 'test.ts',
- key: {_id: 1, hostId: 1},
- timeseries: {timeField: 'time', metaField: 'hostId'},
- }),
- 5914001);
+ assert.commandFailedWithCode(st.s.adminCommand({
+ shardCollection: 'test.ts',
+ key: {a: 1},
+ timeseries: {timeField: 'time', metaField: 'hostId'},
+ }),
+ 5914001);
- assert.commandFailedWithCode(st.s.adminCommand({
- shardCollection: 'test.ts',
- key: {a: 1},
- timeseries: {timeField: 'time', metaField: 'hostId'},
- }),
- 5914001);
+ // Shared key where time is not the last field in shard key should fail.
+ assert.commandFailedWithCode(st.s.adminCommand({
+ shardCollection: 'test.ts',
+ key: {time: 1, hostId: 1},
+ timeseries: {timeField: 'time', metaField: 'hostId'}
+ }),
+ 5914000);
+ assert(sDB.getCollection("ts").drop());
+ })();
- // Shared key where time is not the last field in shard key should fail.
- assert.commandFailedWithCode(st.s.adminCommand({
- shardCollection: 'test.ts',
- key: {time: 1, hostId: 1},
- timeseries: {timeField: 'time', metaField: 'hostId'}
- }),
- 5914000);
- sDB.dropDatabase();
- })();
+ (function noMetaFieldTimeseries() {
+ if (collectionExists) {
+ assert.commandWorked(sDB.createCollection('ts', {timeseries: {timeField: 'time'}}));
+ }
- (function noMetaFieldTimeseries() {
- assert.commandWorked(sDB.createCollection('ts', {timeseries: {timeField: 'time'}}));
- assert.commandWorked(st.s.adminCommand({enableSharding: 'test'}));
+ assert.commandFailedWithCode(st.s.adminCommand({
+ shardCollection: 'test.ts',
+ key: {_id: 1},
+ timeseries: {timeField: 'time'},
+ }),
+ 5914001);
- assert.commandFailedWithCode(st.s.adminCommand({
- shardCollection: 'test.ts',
- key: {_id: 1},
- timeseries: {timeField: 'time'},
- }),
- 5914001);
+ assert.commandFailedWithCode(st.s.adminCommand({
+ shardCollection: 'test.ts',
+ key: {a: 1},
+ timeseries: {timeField: 'time'},
+ }),
+ 5914001);
- assert.commandFailedWithCode(st.s.adminCommand({
- shardCollection: 'test.ts',
- key: {a: 1},
- timeseries: {timeField: 'time'},
- }),
- 5914001);
+ assert.commandWorked(st.s.adminCommand(
+ {shardCollection: 'test.ts', key: {time: 1}, timeseries: {timeField: 'time'}}));
- assert.commandWorked(st.s.adminCommand(
- {shardCollection: 'test.ts', key: {time: 1}, timeseries: {timeField: 'time'}}));
+ assert(sDB.getCollection("ts").drop());
+ })();
+ }
- sDB.dropDatabase();
- })();
+ runShardKeyPatternValidation(true);
+ runShardKeyPatternValidation(false);
} else {
(function timeseriesCollectionsCannotBeSharded() {
diff --git a/src/mongo/db/s/shardsvr_create_collection_command.cpp b/src/mongo/db/s/shardsvr_create_collection_command.cpp
index 5c26855ab41..7a5202860a8 100644
--- a/src/mongo/db/s/shardsvr_create_collection_command.cpp
+++ b/src/mongo/db/s/shardsvr_create_collection_command.cpp
@@ -107,24 +107,25 @@ public:
<< nss << "' collection",
timeseries::optionsAreEqual(*createCmdRequest.getTimeseries(),
*bucketsColl->getTimeseriesOptions()));
- auto timeField = createCmdRequest.getTimeseries()->getTimeField();
- auto metaField = createCmdRequest.getTimeseries()->getMetaField();
- BSONObjIterator iter{*createCmdRequest.getShardKey()};
- while (auto elem = iter.next()) {
- if (elem.fieldNameStringData() == timeField) {
- uassert(5914000,
- str::stream()
- << "the time field '" << timeField
- << "' can be only at the end of the shard key pattern",
- !iter.more());
- } else {
- uassert(5914001,
- str::stream() << "only the time field or meta field can be "
- "part of shard key pattern",
- metaField &&
- (elem.fieldNameStringData() == *metaField ||
- elem.fieldNameStringData().startsWith(*metaField + ".")));
- }
+ }
+
+ auto timeField = createCmdRequest.getTimeseries()->getTimeField();
+ auto metaField = createCmdRequest.getTimeseries()->getMetaField();
+ BSONObjIterator iter{*createCmdRequest.getShardKey()};
+ while (auto elem = iter.next()) {
+ if (elem.fieldNameStringData() == timeField) {
+ uassert(5914000,
+ str::stream()
+ << "the time field '" << timeField
+ << "' can be only at the end of the shard key pattern",
+ !iter.more());
+ } else {
+ uassert(5914001,
+ str::stream() << "only the time field or meta field can be "
+ "part of shard key pattern",
+ metaField &&
+ (elem.fieldNameStringData() == *metaField ||
+ elem.fieldNameStringData().startsWith(*metaField + ".")));
}
}
nss = bucketsNs;