diff options
author | Rui Liu <rui.liu@mongodb.com> | 2021-09-08 11:21:30 +0100 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-09-22 09:40:23 +0000 |
commit | 27d9632d277e63f42752b117ac5e93ac4f0e37a7 (patch) | |
tree | 66c9ad8824969309cdd1ca7d0b9f0b264f924374 | |
parent | 7a0a6ec04debd09d2f6d14612beb14ce78b93ed1 (diff) | |
download | mongo-27d9632d277e63f42752b117ac5e93ac4f0e37a7.tar.gz |
SERVER-57570 Disable updating granularity on sharded time-series collection
-rw-r--r-- | jstests/sharding/timeseries_coll_mod.js | 59 | ||||
-rw-r--r-- | src/mongo/db/commands/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/db/commands/dbcommands.cpp | 18 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_collection_mod_cmd.cpp | 7 |
4 files changed, 82 insertions, 3 deletions
diff --git a/jstests/sharding/timeseries_coll_mod.js b/jstests/sharding/timeseries_coll_mod.js new file mode 100644 index 00000000000..a01cb0b32aa --- /dev/null +++ b/jstests/sharding/timeseries_coll_mod.js @@ -0,0 +1,59 @@ +/** + * Test $collMod command on a sharded timeseries collection. + * + * @tags: [ + * requires_fcv_51 + * ] + */ + +(function() { +"use strict"; + +load("jstests/core/timeseries/libs/timeseries.js"); + +const dbName = 'testDB'; +const collName = 'testColl'; +const timeField = 'tm'; +const metaField = 'mt'; +const viewNss = `${dbName}.${collName}`; + +const mongo = new ShardingTest({shards: 2, rs: {nodes: 2}}); +const mongos = mongo.s0; +const db = mongos.getDB(dbName); + +if (!TimeseriesTest.timeseriesCollectionsEnabled(mongo.shard0)) { + jsTestLog("Skipping test because the time-series collection feature flag is disabled"); + mongo.stop(); + return; +} + +if (!TimeseriesTest.shardedtimeseriesCollectionsEnabled(mongo.shard0)) { + jsTestLog("Skipping test because the sharded time-series collection feature flag is disabled"); + mongo.stop(); + return; +} + +assert.commandWorked( + db.createCollection(collName, {timeseries: {timeField: timeField, metaField: metaField}})); + +// granularity update works for unsharded time-series colleciton. +assert.commandWorked(db.runCommand({collMod: collName, timeseries: {granularity: 'minutes'}})); + +assert.commandWorked(mongos.adminCommand({enableSharding: dbName})); +assert.commandWorked(db[collName].createIndex({[metaField]: 1})); +assert.commandWorked(mongos.adminCommand({ + shardCollection: viewNss, + key: {[metaField]: 1}, +})); + +// timeField and metaField updates are disabled. +assert.commandFailedWithCode(db.runCommand({collMod: collName, timeseries: {timeField: 'x'}}), + 40415 /* Failed to parse */); +assert.commandFailedWithCode(db.runCommand({collMod: collName, timeseries: {metaField: 'x'}}), + 40415 /* Failed to parse */); +// granularity update is currently disabled for sharded time-series collection. +assert.commandFailedWithCode(db.runCommand({collMod: collName, timeseries: {granularity: 'hours'}}), + ErrorCodes.NotImplemented); + +mongo.stop(); +})();
\ No newline at end of file diff --git a/src/mongo/db/commands/SConscript b/src/mongo/db/commands/SConscript index cc726b7385f..3ba4712b8ad 100644 --- a/src/mongo/db/commands/SConscript +++ b/src/mongo/db/commands/SConscript @@ -375,6 +375,7 @@ env.Library( '$BUILD_DIR/mongo/executor/async_request_executor', '$BUILD_DIR/mongo/idl/feature_flag', '$BUILD_DIR/mongo/rpc/rewrite_state_change_errors', + '$BUILD_DIR/mongo/s/grid', '$BUILD_DIR/mongo/util/log_and_backoff', '$BUILD_DIR/mongo/util/net/http_client', 'core', diff --git a/src/mongo/db/commands/dbcommands.cpp b/src/mongo/db/commands/dbcommands.cpp index 7ce8384eeeb..fbf3dbaad96 100644 --- a/src/mongo/db/commands/dbcommands.cpp +++ b/src/mongo/db/commands/dbcommands.cpp @@ -99,6 +99,7 @@ #include "mongo/db/write_concern.h" #include "mongo/executor/async_request_executor.h" #include "mongo/logv2/log.h" +#include "mongo/s/grid.h" #include "mongo/scripting/engine.h" #include "mongo/util/fail_point.h" #include "mongo/util/future.h" @@ -631,6 +632,23 @@ public: !cmd->getChangeStreamPreAndPostImages().has_value()); } + // Updating granularity on sharded time-series collections is not allowed. + if (Grid::get(opCtx)->catalogClient() && cmd->getTimeseries() && + cmd->getTimeseries()->getGranularity()) { + auto& nss = cmd->getNamespace(); + auto bucketNss = + nss.isTimeseriesBucketsCollection() ? nss : nss.makeTimeseriesBucketsNamespace(); + try { + auto coll = Grid::get(opCtx)->catalogClient()->getCollection(opCtx, bucketNss); + uassert(ErrorCodes::NotImplemented, + str::stream() + << "Cannot update granularity of a sharded time-series collection.", + !coll.getTimeseriesFields()); + } catch (const ExceptionFor<ErrorCodes::NamespaceNotFound>&) { + // Collection is not sharded, skip check. + } + } + // If the target namespace refers to a time-series collection, we will redirect the // collection modification request to the underlying bucket collection. // Aliasing collMod on a time-series collection in this manner has a few advantages: diff --git a/src/mongo/s/commands/cluster_collection_mod_cmd.cpp b/src/mongo/s/commands/cluster_collection_mod_cmd.cpp index 0d895756b91..ac369c6a8ba 100644 --- a/src/mongo/s/commands/cluster_collection_mod_cmd.cpp +++ b/src/mongo/s/commands/cluster_collection_mod_cmd.cpp @@ -37,6 +37,7 @@ #include "mongo/db/coll_mod_reply_validation.h" #include "mongo/db/commands.h" #include "mongo/logv2/log.h" +#include "mongo/s/chunk_manager_targeter.h" #include "mongo/s/cluster_commands_helpers.h" #include "mongo/s/grid.h" @@ -91,12 +92,12 @@ public: "namespace"_attr = nss, "command"_attr = redact(cmdObj)); - auto routingInfo = - uassertStatusOK(Grid::get(opCtx)->catalogCache()->getCollectionRoutingInfo(opCtx, nss)); + const auto targeter = ChunkManagerTargeter(opCtx, nss); + const auto& routingInfo = targeter.getRoutingInfo(); auto shardResponses = scatterGatherVersionedTargetByRoutingTable( opCtx, cmd.getDbName(), - nss, + targeter.getNS(), routingInfo, applyReadWriteConcern( opCtx, |