summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArun Banala <arun.banala@mongodb.com>2021-11-29 12:59:18 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-01-07 15:28:33 +0000
commit9dde75ce972cf230b5afcc3a2427d80b3d7b5e47 (patch)
tree67105e982efaaab851838853f9698553379c7d0c
parent660480ddf90b1d4b61bab72dd307ff869ce59e2d (diff)
downloadmongo-9dde75ce972cf230b5afcc3a2427d80b3d7b5e47.tar.gz
SERVER-61590 Add null check before accesssing time-series options in the shardCollection code path
(cherry picked from commit fcea6b10c2ea966433cf009382bf432c9afe1e20)
-rw-r--r--jstests/sharding/timeseries_shard_collection.js21
-rw-r--r--src/mongo/db/s/shardsvr_create_collection_command.cpp25
2 files changed, 37 insertions, 9 deletions
diff --git a/jstests/sharding/timeseries_shard_collection.js b/jstests/sharding/timeseries_shard_collection.js
index c831180164a..6a8c95421da 100644
--- a/jstests/sharding/timeseries_shard_collection.js
+++ b/jstests/sharding/timeseries_shard_collection.js
@@ -311,6 +311,27 @@ if (TimeseriesTest.shardedtimeseriesCollectionsEnabled(st.shard0)) {
runShardKeyPatternValidation(true);
runShardKeyPatternValidation(false);
+ // Verify that the shardCollection command fails if the 'system.buckets' collection does not
+ // have time-series options.
+ sDB.getCollection("ts").drop();
+ sDB.createCollection("system.buckets.ts");
+ assert.commandFailedWithCode(st.s.adminCommand({
+ shardCollection: 'test.ts',
+ key: {time: 1},
+ }),
+ 6159000);
+ assert.commandFailedWithCode(
+ st.s.adminCommand(
+ {shardCollection: 'test.ts', key: {time: 1}, timeseries: {timeField: 'time'}}),
+ 6159000);
+
+ // Cannot shard a system namespace.
+ assert.commandFailedWithCode(st.s.adminCommand({
+ shardCollection: 'test.system.bucket.ts',
+ key: {time: 1},
+ }),
+ ErrorCodes.IllegalOperation);
+
} else {
(function timeseriesCollectionsCannotBeSharded() {
assert.commandFailedWithCode(
diff --git a/src/mongo/db/s/shardsvr_create_collection_command.cpp b/src/mongo/db/s/shardsvr_create_collection_command.cpp
index d57d746ca27..03738167b38 100644
--- a/src/mongo/db/s/shardsvr_create_collection_command.cpp
+++ b/src/mongo/db/s/shardsvr_create_collection_command.cpp
@@ -200,16 +200,23 @@ CreateCollectionResponse createCollection(OperationContext* opCtx,
feature_flags::gFeatureFlagShardedTimeSeries.isEnabled(
serverGlobalParams.featureCompatibility));
- if (!createCmdRequest.getTimeseries()) {
- createCmdRequest.setTimeseries(bucketsColl->getTimeseriesOptions());
- } else if (bucketsColl) {
- uassert(5731500,
- str::stream() << "the 'timeseries' spec provided must match that of exists '"
- << nss << "' collection",
- timeseries::optionsAreEqual(*createCmdRequest.getTimeseries(),
- *bucketsColl->getTimeseriesOptions()));
+ if (bucketsColl) {
+ uassert(6159000,
+ str::stream() << "the collection '" << bucketsNs
+ << "' does not have 'timeseries' options",
+ bucketsColl->getTimeseriesOptions());
+
+ if (createCmdRequest.getTimeseries()) {
+ uassert(5731500,
+ str::stream()
+ << "the 'timeseries' spec provided must match that of exists '" << nss
+ << "' collection",
+ timeseries::optionsAreEqual(*createCmdRequest.getTimeseries(),
+ *bucketsColl->getTimeseriesOptions()));
+ } else {
+ createCmdRequest.setTimeseries(bucketsColl->getTimeseriesOptions());
+ }
}
-
auto timeField = createCmdRequest.getTimeseries()->getTimeField();
auto metaField = createCmdRequest.getTimeseries()->getMetaField();
BSONObjIterator iter{*createCmdRequest.getShardKey()};