|
| 1 | +/** |
| 2 | + * Unified test that makes sure calling setFeatureCompatibilityVersion with |
| 3 | + * {downgradeOnDiskChanges: true} is able to successfully downgrade all expected on-disk changes in |
| 4 | + * one invocation. |
| 5 | + */ |
| 6 | +(function() { |
| 7 | +"use strict"; |
| 8 | + |
| 9 | +load('jstests/multiVersion/libs/multi_rs.js'); |
| 10 | +load('jstests/multiVersion/libs/multi_cluster.js'); |
| 11 | + |
| 12 | +let dbpath = MongoRunner.dataPath + jsTestName(); |
| 13 | +resetDbpath(dbpath); |
| 14 | + |
| 15 | +const latest = "latest"; |
| 16 | +const lastContinuous = "last-continuous"; |
| 17 | + |
| 18 | +/** |
| 19 | + * Each new feature that adds downgrade logic to the setFeatureCompatibilityVersion command with |
| 20 | + * {downgradeOnDiskChanges: true} should add their test cases to this test file. Each test case |
| 21 | + * should follow this 'dummyTest' template and implement the following three test functions: |
| 22 | + * this.onDiskChangesBeforeDowngrade: This function will be called before the FCV downgrade to |
| 23 | + * introduce durable on-disk changes that will be downgraded as part the FCV downgrade. |
| 24 | + * this.validateAfterFCVDowngrade: This function will be called after the FCV downgrade to validate |
| 25 | + * the downgrade of the incompatible on-disk changes introduced in onDiskChangesBeforeDowngrade. |
| 26 | + * this.validateAfterBinaryDowngrade: This function will be called after the binary downgrade to |
| 27 | + * 'last-continuous' binaries to validate the downgrade of the incompatible on-disk changes |
| 28 | + * introduced in onDiskChangesBeforeDowngrade. |
| 29 | + * |
| 30 | + * Each new test should also be added to the downgradeOnDiskChangesTests list below. |
| 31 | + */ |
| 32 | +function dummyTest() { |
| 33 | + const documents = [{_id: "dummy"}]; |
| 34 | + this.onDiskChangesBeforeDowngrade = function(conn) { |
| 35 | + jsTestLog("Running onDiskChangesBeforeDowngrade of dummyTest"); |
| 36 | + let testDB = conn.getDB("test"); |
| 37 | + assert.commandWorked(testDB.runCommand({insert: "dummy", documents: documents})); |
| 38 | + }; |
| 39 | + |
| 40 | + this.validateAfterFCVDowngrade = function(conn) { |
| 41 | + jsTestLog("Running validateAfterFCVDowngrade of dummyTest"); |
| 42 | + let testDB = conn.getDB("test"); |
| 43 | + let res = testDB.dummy.find({}); |
| 44 | + assert.sameMembers(res.toArray(), documents, () => tojson(res)); |
| 45 | + }; |
| 46 | + |
| 47 | + this.validateAfterBinaryDowngrade = function(conn) { |
| 48 | + jsTestLog("Running validateAfterBinaryDowngrade of dummyTest"); |
| 49 | + this.validateAfterFCVDowngrade(conn); |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +const downgradeOnDiskChangesTests = [ |
| 54 | + new dummyTest(), |
| 55 | +]; |
| 56 | + |
| 57 | +function runStandaloneTest() { |
| 58 | + jsTestLog("Running standalone test"); |
| 59 | + |
| 60 | + let conn; |
| 61 | + let adminDB; |
| 62 | + |
| 63 | + // A 'latest' binary standalone should default to 'latestFCV'. |
| 64 | + conn = MongoRunner.runMongod({dbpath: dbpath, binVersion: latest}); |
| 65 | + assert.neq( |
| 66 | + null, conn, "mongod was unable to start up with version=" + latest + " and no data files"); |
| 67 | + adminDB = conn.getDB("admin"); |
| 68 | + checkFCV(adminDB, latestFCV); |
| 69 | + |
| 70 | + jsTestLog("Introducing on-disk changes to be downgraded in FCV downgrade"); |
| 71 | + for (let test of downgradeOnDiskChangesTests) { |
| 72 | + test.onDiskChangesBeforeDowngrade(conn); |
| 73 | + } |
| 74 | + |
| 75 | + jsTestLog( |
| 76 | + "Test that setFeatureCompatibilityVersion succeeds with {downgradeOnDiskChanges: true}"); |
| 77 | + assert.commandWorked(adminDB.runCommand( |
| 78 | + {setFeatureCompatibilityVersion: lastContinuousFCV, downgradeOnDiskChanges: true})); |
| 79 | + checkFCV(adminDB, lastContinuousFCV); |
| 80 | + checkLog.contains(conn, "Downgrading on-disk format"); |
| 81 | + |
| 82 | + jsTestLog("Validating on-disk changes after FCV downgrade"); |
| 83 | + for (let test of downgradeOnDiskChangesTests) { |
| 84 | + test.validateAfterFCVDowngrade(conn); |
| 85 | + } |
| 86 | + |
| 87 | + MongoRunner.stopMongod(conn); |
| 88 | + |
| 89 | + // Test that the node can restart with a last-continuous binary. |
| 90 | + conn = MongoRunner.runMongod({dbpath: dbpath, binVersion: lastContinuous, noCleanData: true}); |
| 91 | + assert.neq(null, |
| 92 | + conn, |
| 93 | + "mongod was unable to start up with binary version=" + lastContinuous + |
| 94 | + " and featureCompatibilityVersion=" + lastContinuousFCV); |
| 95 | + adminDB = conn.getDB("admin"); |
| 96 | + checkFCV(adminDB, lastContinuousFCV); |
| 97 | + |
| 98 | + jsTestLog("Validating on-disk changes after binary downgrade"); |
| 99 | + for (let test of downgradeOnDiskChangesTests) { |
| 100 | + test.validateAfterBinaryDowngrade(conn); |
| 101 | + } |
| 102 | + |
| 103 | + MongoRunner.stopMongod(conn); |
| 104 | + |
| 105 | + // Test that the node can restart with a latest binary. |
| 106 | + conn = MongoRunner.runMongod({dbpath: dbpath, binVersion: latest, noCleanData: true}); |
| 107 | + assert.neq(null, |
| 108 | + conn, |
| 109 | + "mongod was unable to start up with binary version=" + latest + |
| 110 | + " and featureCompatibilityVersion=" + lastContinuousFCV); |
| 111 | + adminDB = conn.getDB("admin"); |
| 112 | + checkFCV(adminDB, lastContinuousFCV); |
| 113 | + |
| 114 | + // Test that the FCV can be upgraded back to 'latestFCV'. |
| 115 | + assert.commandWorked(adminDB.runCommand({setFeatureCompatibilityVersion: latestFCV})); |
| 116 | + checkFCV(adminDB, latestFCV); |
| 117 | + |
| 118 | + MongoRunner.stopMongod(conn); |
| 119 | +} |
| 120 | + |
| 121 | +function runReplicaSetTest() { |
| 122 | + jsTestLog("Running replica set test"); |
| 123 | + |
| 124 | + // 'latest' binary replica set. |
| 125 | + let rst = new ReplSetTest({nodes: 2, nodeOpts: {binVersion: latest}}); |
| 126 | + rst.startSet(); |
| 127 | + rst.initiateWithHighElectionTimeout(); |
| 128 | + let primaryAdminDB = rst.getPrimary().getDB("admin"); |
| 129 | + let secondaryAdminDB = rst.getSecondary().getDB("admin"); |
| 130 | + |
| 131 | + // FCV should default to 'latestFCV' on primary and secondary in a 'latest' binary replica set. |
| 132 | + checkFCV(primaryAdminDB, latestFCV); |
| 133 | + rst.awaitReplication(); |
| 134 | + checkFCV(secondaryAdminDB, latestFCV); |
| 135 | + |
| 136 | + jsTestLog("Introducing on-disk changes to be downgraded in FCV downgrade"); |
| 137 | + for (let test of downgradeOnDiskChangesTests) { |
| 138 | + test.onDiskChangesBeforeDowngrade(rst.getPrimary()); |
| 139 | + } |
| 140 | + |
| 141 | + jsTestLog( |
| 142 | + "Test that setFeatureCompatibilityVersion succeeds with {downgradeOnDiskChanges: true} " + |
| 143 | + "and propogates to the secondary"); |
| 144 | + assert.commandWorked(primaryAdminDB.runCommand( |
| 145 | + {setFeatureCompatibilityVersion: lastContinuousFCV, downgradeOnDiskChanges: true})); |
| 146 | + checkFCV(primaryAdminDB, lastContinuousFCV); |
| 147 | + rst.awaitReplication(); |
| 148 | + checkFCV(secondaryAdminDB, lastContinuousFCV); |
| 149 | + |
| 150 | + jsTestLog("Validating on-disk changes after FCV downgrade"); |
| 151 | + for (let test of downgradeOnDiskChangesTests) { |
| 152 | + test.validateAfterFCVDowngrade(rst.getPrimary()); |
| 153 | + } |
| 154 | + |
| 155 | + // Test that the cluster can restart with a last-continuous binary. |
| 156 | + rst.upgradeSet({binVersion: lastContinuous}); |
| 157 | + primaryAdminDB = rst.getPrimary().getDB("admin"); |
| 158 | + secondaryAdminDB = rst.getSecondary().getDB("admin"); |
| 159 | + checkFCV(primaryAdminDB, lastContinuousFCV); |
| 160 | + checkFCV(secondaryAdminDB, lastContinuousFCV); |
| 161 | + |
| 162 | + jsTestLog("Validating on-disk changes after binary downgrade"); |
| 163 | + for (let test of downgradeOnDiskChangesTests) { |
| 164 | + test.validateAfterBinaryDowngrade(rst.getPrimary()); |
| 165 | + } |
| 166 | + |
| 167 | + // Test that the cluster can restart with a latest binary. |
| 168 | + rst.upgradeSet({binVersion: latest}); |
| 169 | + primaryAdminDB = rst.getPrimary().getDB("admin"); |
| 170 | + secondaryAdminDB = rst.getSecondary().getDB("admin"); |
| 171 | + checkFCV(primaryAdminDB, lastContinuousFCV); |
| 172 | + checkFCV(secondaryAdminDB, lastContinuousFCV); |
| 173 | + |
| 174 | + // Test that the FCV can be upgraded back to 'latestFCV'. |
| 175 | + assert.commandWorked(primaryAdminDB.runCommand({setFeatureCompatibilityVersion: latestFCV})); |
| 176 | + checkFCV(primaryAdminDB, latestFCV); |
| 177 | + rst.awaitReplication(); |
| 178 | + checkFCV(secondaryAdminDB, latestFCV); |
| 179 | + |
| 180 | + rst.stopSet(); |
| 181 | +} |
| 182 | + |
| 183 | +function runShardingTest() { |
| 184 | + jsTestLog("Running sharding test"); |
| 185 | + |
| 186 | + // A 'latest' binary cluster started with clean data files will set FCV to 'latestFCV'. |
| 187 | + let st = |
| 188 | + new ShardingTest({shards: {rs0: {nodes: [{binVersion: latest}, {binVersion: latest}]}}}); |
| 189 | + let mongosAdminDB = st.s.getDB("admin"); |
| 190 | + let configPrimaryAdminDB = st.configRS.getPrimary().getDB("admin"); |
| 191 | + let shardPrimaryAdminDB = st.rs0.getPrimary().getDB("admin"); |
| 192 | + |
| 193 | + checkFCV(configPrimaryAdminDB, latestFCV); |
| 194 | + checkFCV(shardPrimaryAdminDB, latestFCV); |
| 195 | + |
| 196 | + jsTestLog("Introducing on-disk changes to be downgraded in FCV downgrade"); |
| 197 | + for (let test of downgradeOnDiskChangesTests) { |
| 198 | + test.onDiskChangesBeforeDowngrade(st.s); |
| 199 | + } |
| 200 | + |
| 201 | + jsTestLog( |
| 202 | + "Test that setFeatureCompatibilityVersion succeeds with {downgradeOnDiskChanges: true} " + |
| 203 | + "on mongos"); |
| 204 | + assert.commandWorked(mongosAdminDB.runCommand( |
| 205 | + {setFeatureCompatibilityVersion: lastContinuousFCV, downgradeOnDiskChanges: true})); |
| 206 | + |
| 207 | + // FCV propagates to config and shard. |
| 208 | + checkFCV(configPrimaryAdminDB, lastContinuousFCV); |
| 209 | + checkFCV(shardPrimaryAdminDB, lastContinuousFCV); |
| 210 | + |
| 211 | + jsTestLog("Validating on-disk changes after FCV downgrade"); |
| 212 | + for (let test of downgradeOnDiskChangesTests) { |
| 213 | + test.validateAfterFCVDowngrade(st.s); |
| 214 | + } |
| 215 | + |
| 216 | + // Test that the cluster can restart with a last-continuous binary. |
| 217 | + st.upgradeCluster(lastContinuous, {waitUntilStable: true}); |
| 218 | + configPrimaryAdminDB = st.configRS.getPrimary().getDB("admin"); |
| 219 | + shardPrimaryAdminDB = st.rs0.getPrimary().getDB("admin"); |
| 220 | + checkFCV(configPrimaryAdminDB, lastContinuousFCV); |
| 221 | + checkFCV(shardPrimaryAdminDB, lastContinuousFCV); |
| 222 | + |
| 223 | + jsTestLog("Validating on-disk changes after binary downgrade"); |
| 224 | + for (let test of downgradeOnDiskChangesTests) { |
| 225 | + test.validateAfterBinaryDowngrade(st.s); |
| 226 | + } |
| 227 | + |
| 228 | + // Test that the cluster can restart with a latest binary. |
| 229 | + st.upgradeCluster(latest, {waitUntilStable: true}); |
| 230 | + configPrimaryAdminDB = st.configRS.getPrimary().getDB("admin"); |
| 231 | + shardPrimaryAdminDB = st.rs0.getPrimary().getDB("admin"); |
| 232 | + checkFCV(configPrimaryAdminDB, lastContinuousFCV); |
| 233 | + checkFCV(shardPrimaryAdminDB, lastContinuousFCV); |
| 234 | + |
| 235 | + // Test that the FCV can be upgraded back to 'latestFCV'. |
| 236 | + mongosAdminDB = st.s.getDB("admin"); |
| 237 | + assert.commandWorked(mongosAdminDB.runCommand({setFeatureCompatibilityVersion: latestFCV})); |
| 238 | + checkFCV(configPrimaryAdminDB, latestFCV); |
| 239 | + checkFCV(shardPrimaryAdminDB, latestFCV); |
| 240 | + |
| 241 | + st.stop(); |
| 242 | +} |
| 243 | + |
| 244 | +runStandaloneTest(); |
| 245 | +runReplicaSetTest(); |
| 246 | +runShardingTest(); |
| 247 | +})(); |
0 commit comments