forked from mongodb/mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SERVER-77656 Add multitenancy rollback test
- Loading branch information
Showing
14 changed files
with
431 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* Test of a successfull replica set rollback for basic CRUD operations in multitenancy environment | ||
* with featureFlagRequireTenantId. This test is modeled from rollback_crud_ops_sequence.js. | ||
*/ | ||
load('jstests/replsets/libs/rollback_test.js'); | ||
|
||
(function() { | ||
"use strict"; | ||
|
||
const kColl = "bar"; | ||
const tenantA = ObjectId(); | ||
const tenantB = ObjectId(); | ||
|
||
const insertDocs = function(db, coll, tenant, documents) { | ||
assert.commandWorked(db.runCommand({insert: coll, documents, '$tenant': tenant})); | ||
}; | ||
|
||
const updateDocs = function(db, coll, tenant, updates) { | ||
assert.commandWorked(db.runCommand({update: coll, updates, '$tenant': tenant})); | ||
}; | ||
|
||
const deleteMany = function(db, coll, tenant, query) { | ||
assert.commandWorked(db.runCommand({ | ||
delete: coll, | ||
deletes: [ | ||
{q: query, limit: 0}, | ||
], | ||
'$tenant': tenant | ||
|
||
})); | ||
}; | ||
|
||
const validateCounts = function(db, coll, tenant, expect) { | ||
for (let expected of expect) { | ||
let res = db.runCommand({count: coll, query: expected.q, '$tenant': tenant}); | ||
assert.eq(res.n, expected.n); | ||
} | ||
}; | ||
|
||
// Helper function for verifying contents at the end of the test. | ||
const checkFinalResults = function(db) { | ||
validateCounts(db, kColl, tenantA, [ | ||
{q: {q: 70}, n: 0}, | ||
{q: {q: 40}, n: 2}, | ||
{q: {a: 'foo'}, n: 3}, | ||
{q: {q: {$gt: -1}}, n: 6}, | ||
{q: {txt: 'foo'}, n: 1}, | ||
{q: {q: 4}, n: 0} | ||
]); | ||
|
||
validateCounts(db, kColl, tenantB, [{q: {q: 1}, n: 1}, {q: {q: 40}, n: 0}]); | ||
|
||
let res = db.runCommand({find: kColl, filter: {q: 0}, '$tenant': tenantA}); | ||
assert.eq(res.cursor.firstBatch.length, 1); | ||
assert.eq(res.cursor.firstBatch[0].y, 33); | ||
|
||
res = db.runCommand({find: 'kap', '$tenant': tenantA}); | ||
assert.eq(res.cursor.firstBatch.length, 1); | ||
|
||
res = db.runCommand({find: 'kap2', '$tenant': tenantA}); | ||
assert.eq(res.cursor.firstBatch.length, 0); | ||
}; | ||
|
||
function setFastGetMoreEnabled(node) { | ||
assert.commandWorked( | ||
node.adminCommand({configureFailPoint: 'setSmallOplogGetMoreMaxTimeMS', mode: 'alwaysOn'}), | ||
`Failed to enable setSmallOplogGetMoreMaxTimeMS failpoint.`); | ||
} | ||
|
||
function setUpRst() { | ||
const replSet = new ReplSetTest({ | ||
nodes: 3, | ||
useBridge: true, | ||
nodeOptions: {setParameter: {multitenancySupport: true, featureFlagRequireTenantID: true}} | ||
}); | ||
replSet.startSet(); | ||
replSet.nodes.forEach(setFastGetMoreEnabled); | ||
|
||
let config = replSet.getReplSetConfig(); | ||
config.members[2].priority = 0; | ||
config.settings = {chainingAllowed: false}; | ||
replSet.initiateWithHighElectionTimeout(config); | ||
// Tiebreaker's replication is paused for most of the test, avoid falling off the oplog. | ||
replSet.nodes.forEach((node) => { | ||
assert.commandWorked(node.adminCommand({replSetResizeOplog: 1, minRetentionHours: 2})); | ||
}); | ||
|
||
assert.eq(replSet.nodes.length, | ||
3, | ||
"Mismatch between number of data bearing nodes and test configuration."); | ||
|
||
return replSet; | ||
} | ||
|
||
const replSet = setUpRst(); | ||
const rollbackTest = new RollbackTest("MultitenancyRollbackTest", replSet); | ||
|
||
const rollbackNode = rollbackTest.getPrimary(); | ||
rollbackNode.setSecondaryOk(); | ||
const syncSource = rollbackTest.getSecondary(); | ||
syncSource.setSecondaryOk(); | ||
|
||
const rollbackNodeDB = rollbackNode.getDB("foo"); | ||
const syncSourceDB = syncSource.getDB("foo"); | ||
|
||
// Insert initial data for both nodes. | ||
insertDocs(rollbackNodeDB, kColl, tenantA, [{q: -2}, {q: 0}, {q: 1, a: "foo"}]); | ||
insertDocs(rollbackNodeDB, kColl, tenantB, [{q: 1}, {q: 40, a: "foo"}]); | ||
insertDocs(rollbackNodeDB, kColl, tenantA, [ | ||
{q: 2, a: "foo", x: 1}, | ||
{q: 3, bb: 9, a: "foo"}, | ||
{q: 40, a: 1}, | ||
{q: 40, a: 2}, | ||
{q: 70, txt: 'willremove'} | ||
]); | ||
|
||
// Testing capped collection. | ||
rollbackNodeDB.createCollection("kap", {'$tenant': tenantA, capped: true, size: 5000}); | ||
insertDocs(rollbackNodeDB, 'kap', tenantA, [{foo: 1}]); | ||
// Going back to empty on capped is a special case and must be tested. | ||
rollbackNodeDB.createCollection("kap2", {'$tenant': tenantA, capped: true, size: 5000}); | ||
|
||
rollbackTest.awaitReplication(); | ||
rollbackTest.transitionToRollbackOperations(); | ||
|
||
// These operations are only done on 'rollbackNode' and should eventually be rolled back. | ||
insertDocs(rollbackNodeDB, kColl, tenantA, [{q: 4}]); | ||
updateDocs(rollbackNodeDB, kColl, tenantA, [ | ||
{q: {q: 3}, u: {q: 3, rb: true}}, | ||
]); | ||
insertDocs(rollbackNodeDB, kColl, tenantB, [{q: 1, foo: 2}]); | ||
deleteMany(rollbackNodeDB, kColl, tenantA, {q: 40}); | ||
updateDocs(rollbackNodeDB, kColl, tenantA, [ | ||
{q: {q: 2}, u: {q: 39, rb: true}}, | ||
]); | ||
|
||
// Rolling back a delete will involve reinserting the item(s). | ||
deleteMany(rollbackNodeDB, kColl, tenantA, {q: 1}); | ||
updateDocs(rollbackNodeDB, kColl, tenantA, [ | ||
{q: {q: 0}, u: {$inc: {y: 1}}}, | ||
]); | ||
insertDocs(rollbackNodeDB, 'kap', tenantA, [{foo: 2}]); | ||
insertDocs(rollbackNodeDB, 'kap2', tenantA, [{foo: 2}]); | ||
|
||
// Create a collection (need to roll back the whole thing). | ||
insertDocs(rollbackNodeDB, 'newcoll', tenantA, [{a: true}]); | ||
// Create a new empty collection (need to roll back the whole thing). | ||
assert.commandWorked(rollbackNodeDB.createCollection("abc", {'$tenant': tenantA})); | ||
|
||
rollbackTest.transitionToSyncSourceOperationsBeforeRollback(); | ||
|
||
// Insert new data into syncSource so that rollbackNode enters rollback when it is reconnected. | ||
// These operations should not be rolled back. | ||
insertDocs(syncSourceDB, kColl, tenantA, [{txt: 'foo'}]); | ||
deleteMany(syncSourceDB, kColl, tenantA, {q: 70}); | ||
updateDocs(syncSourceDB, kColl, tenantA, [ | ||
{q: {q: 0}, u: {$inc: {y: 33}}}, | ||
]); | ||
deleteMany(syncSourceDB, kColl, tenantB, {q: 40}); | ||
|
||
rollbackTest.transitionToSyncSourceOperationsDuringRollback(); | ||
rollbackTest.transitionToSteadyStateOperations(); | ||
|
||
rollbackTest.awaitReplication(); | ||
checkFinalResults(rollbackNodeDB); | ||
checkFinalResults(syncSourceDB); | ||
|
||
rollbackTest.stop(); | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Copyright (C) 2023-present MongoDB, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the Server Side Public License, version 1, | ||
* as published by MongoDB, Inc. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* Server Side Public License for more details. | ||
* | ||
* You should have received a copy of the Server Side Public License | ||
* along with this program. If not, see | ||
* <http://www.mongodb.com/licensing/server-side-public-license>. | ||
* | ||
* As a special exception, the copyright holders give permission to link the | ||
* code of portions of this program with the OpenSSL library under certain | ||
* conditions as described in each individual source file and distribute | ||
* linked combinations including the program with the OpenSSL library. You | ||
* must comply with the Server Side Public License in all respects for | ||
* all of the code used other than as permitted herein. If you modify file(s) | ||
* with this exception, you may extend this exception to your version of the | ||
* file(s), but you are not obligated to do so. If you do not wish to do so, | ||
* delete this exception statement from your version. If you delete this | ||
* exception statement from all source files in the program, then also delete | ||
* it in the license file. | ||
*/ | ||
|
||
#include "mongo/db/serverless/multitenancy_check.h" | ||
|
||
#include "mongo/db/client.h" | ||
#include "mongo/db/service_context.h" | ||
|
||
namespace mongo { | ||
|
||
const ServiceContext::Decoration<std::unique_ptr<MultitenancyCheck>> MultitenancyCheck::get = | ||
ServiceContext::declareDecoration<std::unique_ptr<MultitenancyCheck>>(); | ||
|
||
MultitenancyCheck::MultitenancyCheck(bool multitenancySupport) | ||
: _multitenancySupport(multitenancySupport) {} | ||
|
||
void MultitenancyCheck::checkDollarTenantField(const BSONObj& body) const { | ||
uassert(ErrorCodes::InvalidOptions, | ||
"Multitenancy not enabled, cannot set $tenant in command body", | ||
_multitenancySupport || !body["$tenant"_sd]); | ||
} | ||
|
||
const MultitenancyCheck* MultitenancyCheck::getPtr() { | ||
if (!hasGlobalServiceContext()) { | ||
// globalServiceContext is not always set for unit tests | ||
return nullptr; | ||
} | ||
|
||
return MultitenancyCheck::get(getGlobalServiceContext()).get(); | ||
} | ||
|
||
void setUpMultitenancyCheck(ServiceContext* serviceContext, bool multitenancySupport) { | ||
auto& multitenancyCheck = MultitenancyCheck::get(serviceContext); | ||
|
||
multitenancyCheck = std::make_unique<MultitenancyCheck>(multitenancySupport); | ||
} | ||
|
||
} // namespace mongo |
Oops, something went wrong.