diff options
author | Arun Banala <arun.banala@mongodb.com> | 2021-10-04 20:20:28 +0100 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-10-19 16:35:42 +0000 |
commit | a35ea4d9afd9583c4c1efed5f68dedb1afd21fff (patch) | |
tree | 46195f23a31eb73f21a2fe649b7ea402206cd8ab /jstests | |
parent | 40bd16c4e5b044d3ddad2ff2292108f6d6ad0fa3 (diff) | |
download | mongo-a35ea4d9afd9583c4c1efed5f68dedb1afd21fff.tar.gz |
SERVER-60452 Fix validation when the collection does not exist
(cherry picked from commit 9833bf4830fbfba67cbea4a6ae42d58bd6866a6f)
Diffstat (limited to 'jstests')
-rw-r--r-- | jstests/sharding/timeseries_shard_collection.js | 236 |
1 files changed, 125 insertions, 111 deletions
diff --git a/jstests/sharding/timeseries_shard_collection.js b/jstests/sharding/timeseries_shard_collection.js index 80c538237a1..c831180164a 100644 --- a/jstests/sharding/timeseries_shard_collection.js +++ b/jstests/sharding/timeseries_shard_collection.js @@ -171,131 +171,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() { |