|
| 1 | +/** |
| 2 | + * Tests that: |
| 3 | + * 1. Reads and writes to the admin database are forbidden within single replica set transactions |
| 4 | + * on sharded clusters. |
| 5 | + * 2. Read and writes to the config database are forbidden from mongos within single replica set |
| 6 | + * transactions on sharded clusters. |
| 7 | + * 3. Reads and writes to the config.transactions namespace are forbidden within single replica set |
| 8 | + * transactions on sharded clusters, BUT read and writes to other namespaces in the config |
| 9 | + * database are allowed. |
| 10 | + * |
| 11 | + * @tags: [requires_find_command] |
| 12 | + */ |
| 13 | + |
| 14 | +(function() { |
| 15 | +"use strict"; |
| 16 | + |
| 17 | +load("jstests/sharding/libs/sharded_transactions_helpers.js"); |
| 18 | + |
| 19 | +const st = new ShardingTest({shards: 1}); |
| 20 | +const mongosSession = st.s.startSession(); |
| 21 | +const shardSession = st.shard0.getDB("test").getMongo().startSession(); |
| 22 | +const collName = "banned_txn_dbs"; |
| 23 | + |
| 24 | +jsTest.log("Verify that read and write operations within transactions are forbidden for the " + |
| 25 | + "admin database within a transaction."); |
| 26 | + |
| 27 | +const adminColl = shardSession.getDatabase("admin")[collName]; |
| 28 | + |
| 29 | +shardSession.startTransaction(); |
| 30 | +let error = assert.throws(() => adminColl.find().itcount()); |
| 31 | +assert.commandFailedWithCode(error, ErrorCodes.OperationNotSupportedInTransaction); |
| 32 | +assert.commandFailedWithCode(shardSession.abortTransaction_forTesting(), |
| 33 | + ErrorCodes.NoSuchTransaction); |
| 34 | + |
| 35 | +shardSession.startTransaction(); |
| 36 | +assert.commandFailedWithCode(adminColl.insert({}), ErrorCodes.OperationNotSupportedInTransaction); |
| 37 | +assert.commandFailedWithCode(shardSession.abortTransaction_forTesting(), |
| 38 | + ErrorCodes.NoSuchTransaction); |
| 39 | + |
| 40 | +jsTestLog("Verify that read and write operations within transactions are forbidden for the " + |
| 41 | + "config database when accessed through mongos."); |
| 42 | + |
| 43 | +const mongosConfigDB = mongosSession.getDatabase("config"); |
| 44 | +const clusterColls = [ |
| 45 | + mongosConfigDB["test"], |
| 46 | + mongosConfigDB["actionlog"], |
| 47 | + mongosConfigDB["transaction_coords"], |
| 48 | + mongosConfigDB["transactions"] |
| 49 | +]; |
| 50 | + |
| 51 | +mongosSession.startTransaction(); |
| 52 | +clusterColls.forEach((coll) => { |
| 53 | + error = assert.throws(() => coll.find().itcount()); |
| 54 | + assert.commandFailedWithCode(error, ErrorCodes.OperationNotSupportedInTransaction); |
| 55 | +}); |
| 56 | + |
| 57 | +mongosSession.endSession(); |
| 58 | + |
| 59 | +jsTestLog("Verify that read operations within transactions work fine for the config database " + |
| 60 | + "when not config.transactions (and directly accessed through the shard)."); |
| 61 | + |
| 62 | +const configDB = shardSession.getDatabase("config"); |
| 63 | +const shardColls = [configDB["test"], configDB["actionlog"], configDB["transaction_coords"]]; |
| 64 | + |
| 65 | +shardSession.startTransaction(); |
| 66 | +shardColls.forEach((coll) => { |
| 67 | + coll.find().itcount(); |
| 68 | +}); |
| 69 | + |
| 70 | +jsTestLog("Verify that read operations will not work for the config.transactions namespace."); |
| 71 | + |
| 72 | +const shardCollTransactions = configDB["transactions"]; |
| 73 | +error = assert.throws(() => shardCollTransactions.find().itcount()); |
| 74 | +assert.commandFailedWithCode(error, ErrorCodes.OperationNotSupportedInTransaction); |
| 75 | + |
| 76 | +shardSession.endSession(); |
| 77 | +st.stop(); |
| 78 | +}()); |
0 commit comments