|
| 1 | +/* |
| 2 | + * Tests valid/invalid rename operations over timeseries collections |
| 3 | + * |
| 4 | + * @tags: [ |
| 5 | + * uses_rename, |
| 6 | + * requires_timeseries, |
| 7 | + * # the rename command is not idempotent |
| 8 | + * requires_non_retryable_commands, |
| 9 | + * # rename only works across databases with same primary shard |
| 10 | + * # TODO SERVER-90096: change this tag with a more specific one |
| 11 | + * assumes_balancer_off, |
| 12 | + * ] |
| 13 | + */ |
| 14 | + |
| 15 | +import {FeatureFlagUtil} from "jstests/libs/feature_flag_util.js"; |
| 16 | +import {FixtureHelpers} from "jstests/libs/fixture_helpers.js"; |
| 17 | + |
| 18 | +const dbName = db.getName(); |
| 19 | +const otherDbName = `${dbName}_other`; |
| 20 | +const collName = `coll_${jsTestName()}`; |
| 21 | +const bucketsCollName = `system.buckets.${collName}`; |
| 22 | +const timeseriesOpts = { |
| 23 | + timeseries: {timeField: "time"} |
| 24 | +}; |
| 25 | + |
| 26 | +// TODO SERVER-101595 get rid of all the code protected behind this flag |
| 27 | +const viewlessTimeseriesEnabled = |
| 28 | + FeatureFlagUtil.isPresentAndEnabled(db, "CreateViewlessTimeseriesCollections"); |
| 29 | + |
| 30 | +function setupEnv() { |
| 31 | + db.dropDatabase(); |
| 32 | + db.getSiblingDB(otherDbName).dropDatabase(); |
| 33 | + |
| 34 | + // TODO SERVER-89086 remove the following workaround once the underlying problem is fixed. |
| 35 | + if (FixtureHelpers.isMongos(db) || TestData.testingReplicaSetEndpoint) { |
| 36 | + // On sharded cluster rename throw NamespaceNotFound if the target database does not exists |
| 37 | + // yet. Thus we need to manually pre-create it. |
| 38 | + // Additionally rename only works across database with the same primary shard. Thus create |
| 39 | + // second database on the same primary shard. |
| 40 | + assert.commandWorked(db.adminCommand({enableSharding: dbName})); |
| 41 | + assert.commandWorked(db.adminCommand({ |
| 42 | + enableSharding: otherDbName, |
| 43 | + primaryShard: db.getSiblingDB(dbName).getDatabasePrimaryShardId() |
| 44 | + })); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// Insert measurements -> rename -> check measurements/buckets have not changed |
| 49 | +function checkSuccessfullRename(fromDb, fromColl, toDb, toColl) { |
| 50 | + const srcNss = fromDb + (viewlessTimeseriesEnabled ? '.' : '.system.buckets.') + fromColl; |
| 51 | + const srcColl = db.getSiblingDB(fromDb)[fromColl]; |
| 52 | + const dstNss = toDb + (viewlessTimeseriesEnabled ? '.' : '.system.buckets.') + toColl; |
| 53 | + const dstColl = db.getSiblingDB(toDb)[toColl]; |
| 54 | + |
| 55 | + // 4 measurements falling into 3 different buckets |
| 56 | + const measurements = [ |
| 57 | + {time: ISODate("2019-01-30T07:30:10.957Z"), meta: 0}, |
| 58 | + {time: ISODate("2019-01-30T07:30:11.957Z"), meta: 1}, |
| 59 | + {time: ISODate("2020-01-30T07:30:11.957Z"), meta: 2}, |
| 60 | + {time: ISODate("2021-01-30T07:30:11.957Z"), meta: 3} |
| 61 | + ]; |
| 62 | + |
| 63 | + for (let i = 0; i < measurements.length; i++) { |
| 64 | + assert.commandWorked(srcColl.insert(measurements[i])); |
| 65 | + } |
| 66 | + |
| 67 | + const beforeBuckets = |
| 68 | + viewlessTimeseriesEnabled ? srcColl.find().rawData().toArray() : srcColl.find().toArray(); |
| 69 | + |
| 70 | + assert.commandWorked(db.adminCommand({renameCollection: srcNss, to: dstNss})); |
| 71 | + |
| 72 | + if (!viewlessTimeseriesEnabled) { |
| 73 | + // Fix view definitions following rename |
| 74 | + assert.commandWorked(db.getSiblingDB(fromDb).runCommand({drop: fromColl})); |
| 75 | + assert.commandWorked(db.getSiblingDB(toDb).createCollection(toColl, timeseriesOpts)); |
| 76 | + } |
| 77 | + |
| 78 | + const afterBuckets = |
| 79 | + viewlessTimeseriesEnabled ? dstColl.find().rawData().toArray() : dstColl.find().toArray(); |
| 80 | + assert.eq(beforeBuckets, afterBuckets); |
| 81 | + |
| 82 | + const afterMeasurements = dstColl.find({}, {_id: 0}).sort({time: 1}).toArray(); |
| 83 | + assert.eq(afterMeasurements, measurements); |
| 84 | +} |
| 85 | + |
| 86 | +function runSystemBucketsTests(targetDbName) { |
| 87 | + { |
| 88 | + jsTest.log("Renaming a timeseries collection using the main namespace is not supported"); |
| 89 | + |
| 90 | + setupEnv(); |
| 91 | + assert.commandWorked(db.createCollection(collName, timeseriesOpts)); |
| 92 | + assert.commandFailedWithCode( |
| 93 | + db.adminCommand( |
| 94 | + {renameCollection: `${dbName}.${collName}`, to: `${targetDbName}.newColl`}), |
| 95 | + // TODO SERVER-89100 unify error code between sharded clusters and replicaset |
| 96 | + [ErrorCodes.IllegalOperation, ErrorCodes.CommandNotSupportedOnView]); |
| 97 | + } |
| 98 | + { |
| 99 | + jsTest.log( |
| 100 | + "Renaming a simple collection to a bucket collection without timeseries options fails"); |
| 101 | + setupEnv(); |
| 102 | + assert.commandWorked(db.createCollection(collName)); |
| 103 | + const res = db.adminCommand({ |
| 104 | + renameCollection: `${dbName}.${collName}`, |
| 105 | + to: `${targetDbName}.system.buckets.newColl` |
| 106 | + }); |
| 107 | + assert.commandFailedWithCode(res, [ErrorCodes.IllegalOperation]); |
| 108 | + } |
| 109 | + { |
| 110 | + jsTest.log( |
| 111 | + "Renaming a timeseries collection using the main namespace with a target bucket collection fail"); |
| 112 | + setupEnv(); |
| 113 | + assert.commandWorked(db.createCollection(collName, timeseriesOpts)); |
| 114 | + assert.commandFailedWithCode( |
| 115 | + db.adminCommand({ |
| 116 | + renameCollection: `${dbName}.${collName}`, |
| 117 | + to: `${dbName}.system.buckets.newColl` |
| 118 | + }), |
| 119 | + // TODO SERVER-89100 unify error code between sharded clusters and replicaset |
| 120 | + [ErrorCodes.IllegalOperation, ErrorCodes.CommandNotSupportedOnView]); |
| 121 | + } |
| 122 | + { |
| 123 | + jsTest.log("Renaming a timeseries bucket collection to a normal collection fail"); |
| 124 | + setupEnv(); |
| 125 | + assert.commandWorked(db.createCollection(collName, timeseriesOpts)); |
| 126 | + assert.commandFailedWithCode( |
| 127 | + db.adminCommand( |
| 128 | + {renameCollection: `${dbName}.${bucketsCollName}`, to: `${targetDbName}.newColl`}), |
| 129 | + ErrorCodes.IllegalOperation); |
| 130 | + } |
| 131 | + |
| 132 | + { |
| 133 | + setupEnv(); |
| 134 | + assert.commandWorked(db.createCollection(collName, timeseriesOpts)); |
| 135 | + const isUnsharded = |
| 136 | + db.getSiblingDB('config').collections.countDocuments( |
| 137 | + {_id: `${dbName}.${bucketsCollName}`, unsplittable: {$ne: true}}) == 0; |
| 138 | + // Rename across db is not supported for sharded collections |
| 139 | + if (dbName == targetDbName || isUnsharded) { |
| 140 | + jsTest.log( |
| 141 | + "Renaming a timeseries bucket collection to another bucket collection works"); |
| 142 | + checkSuccessfullRename(dbName, collName, targetDbName, 'newColl'); |
| 143 | + } else { |
| 144 | + jsTest.log("Renaming a sharded timeseries bucket collection to a different db fails"); |
| 145 | + |
| 146 | + assert.commandFailedWithCode(db.adminCommand({ |
| 147 | + renameCollection: `${dbName}.${bucketsCollName}`, |
| 148 | + to: `${targetDbName}.system.buckets.newColl` |
| 149 | + }), |
| 150 | + ErrorCodes.CommandFailed); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +function runViewLessTimeseriesTests(targetDbName) { |
| 156 | + { |
| 157 | + setupEnv(); |
| 158 | + assert.commandWorked(db.createCollection(collName, timeseriesOpts)); |
| 159 | + const isUnsharded = db.getSiblingDB('config').collections.countDocuments( |
| 160 | + {_id: `${dbName}.${collName}`, unsplittable: {$ne: true}}) == 0; |
| 161 | + |
| 162 | + // Rename across db is not supported for sharded collections |
| 163 | + if (dbName == targetDbName || isUnsharded) { |
| 164 | + jsTest.log("Renaming a viewless timeseries collection works"); |
| 165 | + checkSuccessfullRename(dbName, collName, targetDbName, 'newColl'); |
| 166 | + } else { |
| 167 | + jsTest.log("Renaming a sharded viewless timeseries collection to a different db fails"); |
| 168 | + assert.commandFailedWithCode(db.adminCommand({ |
| 169 | + renameCollection: `${dbName}.${bucketsCollName}`, |
| 170 | + to: `${targetDbName}.newColl` |
| 171 | + }), |
| 172 | + ErrorCodes.IllegalOperation); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + { |
| 177 | + jsTest.log("Renaming a viewless timeseries collection to a target bucket collection fails"); |
| 178 | + setupEnv(); |
| 179 | + assert.commandWorked(db.createCollection(collName)); |
| 180 | + const res = db.adminCommand({ |
| 181 | + renameCollection: `${dbName}.${collName}`, |
| 182 | + to: `${targetDbName}.system.buckets.newColl` |
| 183 | + }); |
| 184 | + assert.commandFailedWithCode(res, [ErrorCodes.IllegalOperation]); |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +jsTest.log("Run test cases with rename within same database"); |
| 189 | +if (viewlessTimeseriesEnabled) { |
| 190 | + runViewLessTimeseriesTests(dbName); |
| 191 | +} else { |
| 192 | + runSystemBucketsTests(dbName); |
| 193 | +} |
| 194 | + |
| 195 | +jsTest.log("Run test cases with rename across different databases"); |
| 196 | +if (viewlessTimeseriesEnabled) { |
| 197 | + runViewLessTimeseriesTests(otherDbName); |
| 198 | +} else { |
| 199 | + runSystemBucketsTests(otherDbName); |
| 200 | +} |
0 commit comments