-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathnative_tenant_data_isolation_concurrent_lock_ops.js
268 lines (229 loc) · 10.9 KB
/
native_tenant_data_isolation_concurrent_lock_ops.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Test that two tenants can hold an X/IX db/collection lock without blocking each others even with
// having same db and collection names.
import {configureFailPoint} from "jstests/libs/fail_point_util.js";
import {Thread} from "jstests/libs/parallelTester.js";
import {ReplSetTest} from "jstests/libs/replsettest.js";
const rst = new ReplSetTest({
nodes: 3,
nodeOptions:
{auth: '', setParameter: {multitenancySupport: true, featureFlagSecurityToken: true}}
});
rst.startSet({keyFile: 'jstests/libs/key1'});
rst.initiate();
const primary = rst.getPrimary();
const adminDb = primary.getDB('admin');
// Prepare an authenticated user for testing.
// Must be authenticated as a user with ActionType::useTenant in order to use security token
assert.commandWorked(adminDb.runCommand({createUser: 'admin', pwd: 'pwd', roles: ['root']}));
assert(adminDb.auth('admin', 'pwd'));
const kTenant = ObjectId();
const kOtherTenant = ObjectId();
const kDbName = 'myDb';
const kCollName = 'myColl';
const testDb = primary.getDB(kDbName);
const securityToken = _createTenantToken({tenant: kTenant});
const otherSecurityToken = _createTenantToken({tenant: kOtherTenant});
/**
* Configure a failpoint which will block two threads that will be holding locks and check
* to see if a lock is currently being held for the given tenant whether at the collection or
* database level.
*/
function checkConcurrentLockDifferentTenant(
primary, tenantA, tenantB, fpName, lockCheckName, func, expectedLockMode = "X") {
let fp = configureFailPoint(primary, fpName);
let t1 = new Thread(func, primary.host, kCollName, kDbName, securityToken);
t1.start();
waitForLock(tenantA.str, lockCheckName, expectedLockMode);
let t2 = new Thread(func, primary.host, kCollName, kDbName, otherSecurityToken);
t2.start();
waitForLock(tenantB.str, lockCheckName, expectedLockMode);
fp.off();
t1.join();
t2.join();
}
/**
* Run the lockInfo command and wait for a given nss that a lock is being held.
*/
function waitForLock(nss, resource, expectedLockMode) {
assert.soon(() => {
let lockInfo = assert.commandWorked(adminDb.runCommand({lockInfo: 1})).lockInfo;
for (let i = 0; i < lockInfo.length; i++) {
let resourceId = lockInfo[i].resourceId;
const mode = lockInfo[i].granted[0].mode;
if (resourceId.includes(resource) && resourceId.includes(nss) &&
mode === expectedLockMode) {
return true;
}
}
return false;
});
}
// Check that collmods can run concurrently for two different tenants with the same db name and
// collection name.
{
function collModThreadFunc(host, collectionName, dbName, token) {
const db = new Mongo(host);
const adminDb = db.getDB('admin');
db._setSecurityToken(token);
assert(adminDb.auth('admin', 'pwd'));
let res = assert.commandWorked(db.getDB(dbName).runCommand(
{collMod: collectionName, "index": {"keyPattern": {c: 1}, expireAfterSeconds: 100}}));
assert.eq(50, res.expireAfterSeconds_old, tojson(res));
assert.eq(100, res.expireAfterSeconds_new, tojson(res));
}
primary._setSecurityToken(securityToken);
assert.commandWorked(testDb.createCollection(kCollName));
primary._setSecurityToken(otherSecurityToken);
assert.commandWorked(testDb.createCollection(kCollName));
// Create the index used for collMod
primary._setSecurityToken(securityToken);
let res = assert.commandWorked(testDb.runCommand({
createIndexes: kCollName,
indexes: [{key: {c: 1}, name: "indexA", expireAfterSeconds: 50}]
}));
assert.eq(2, res.numIndexesAfter, tojson(res));
primary._setSecurityToken(otherSecurityToken);
res = assert.commandWorked(testDb.runCommand({
createIndexes: kCollName,
indexes: [{key: {c: 1}, name: "indexA", expireAfterSeconds: 50}]
}));
assert.eq(2, res.numIndexesAfter, tojson(res));
checkConcurrentLockDifferentTenant(primary,
kTenant,
kOtherTenant,
"hangAfterDatabaseLock",
"Collection",
collModThreadFunc,
"X");
primary._setSecurityToken(securityToken);
assert.commandWorked(testDb.runCommand({dropDatabase: 1}));
primary._setSecurityToken(otherSecurityToken);
assert.commandWorked(testDb.runCommand({dropDatabase: 1}));
}
// Check that drop database can run concurrently for two different tenants with the same db name and
// collection name.
{
function dropDBThreadFunc(host, collName, dbName, token) {
const db = new Mongo(host);
const adminDb = db.getDB('admin');
db._setSecurityToken(token);
assert(adminDb.auth('admin', 'pwd'));
assert.commandWorked(db.getDB(dbName).runCommand({dropDatabase: 1}));
// Verify we deleted the DB
const collsAfterDropDb = assert.commandWorked(db.getDB(dbName).runCommand(
{listCollections: 1, nameOnly: true, filter: {name: collName}}));
assert.eq(0,
collsAfterDropDb.cursor.firstBatch.length,
tojson(collsAfterDropDb.cursor.firstBatch));
}
primary._setSecurityToken(securityToken);
assert.commandWorked(testDb.createCollection(kCollName));
primary._setSecurityToken(otherSecurityToken);
assert.commandWorked(testDb.createCollection(kCollName));
checkConcurrentLockDifferentTenant(primary,
kTenant,
kOtherTenant,
"dropDatabaseHangHoldingLock",
"Database",
dropDBThreadFunc,
"X");
primary._setSecurityToken(securityToken);
assert.commandWorked(testDb.runCommand({dropDatabase: 1}));
primary._setSecurityToken(otherSecurityToken);
assert.commandWorked(testDb.runCommand({dropDatabase: 1}));
}
// Check that drop collection can run concurrently for two different tenants with the same db name
// and collection name
{
function dropCollThreadFunc(host, collName, dbName, token) {
const db = new Mongo(host);
const adminDb = db.getDB('admin');
db._setSecurityToken(token);
assert(adminDb.auth('admin', 'pwd'));
assert.commandWorked(db.getDB(dbName).runCommand({drop: collName}));
// Verify we deleted the collection.
const collsAfterDropCollection = assert.commandWorked(db.getDB(dbName).runCommand(
{listCollections: 1, nameOnly: true, filter: {name: collName}}));
assert.eq(0,
collsAfterDropCollection.cursor.firstBatch.length,
tojson(collsAfterDropCollection.cursor.firstBatch));
}
primary._setSecurityToken(securityToken);
assert.commandWorked(testDb.createCollection(kCollName));
primary._setSecurityToken(otherSecurityToken);
assert.commandWorked(testDb.createCollection(kCollName));
checkConcurrentLockDifferentTenant(primary,
kTenant,
kOtherTenant,
"hangDuringDropCollection",
"Collection",
dropCollThreadFunc,
"IX");
primary._setSecurityToken(securityToken);
assert.commandWorked(testDb.runCommand({dropDatabase: 1}));
primary._setSecurityToken(otherSecurityToken);
assert.commandWorked(testDb.runCommand({dropDatabase: 1}));
}
// Check that create index can run concurrently for two different tenants with the same db name and
// collection name
{
function createIndexThreadFunc(host, collName, dbName, token) {
const db = new Mongo(host);
const adminDb = db.getDB('admin');
db._setSecurityToken(token);
assert(adminDb.auth('admin', 'pwd'));
let res = assert.commandWorked(db.getDB(dbName).runCommand({
createIndexes: collName,
indexes: [{key: {a: 1}, name: "indexA"}, {key: {b: 1}, name: "indexB"}]
}));
assert.eq(3, res.numIndexesAfter, tojson(res));
}
primary._setSecurityToken(securityToken);
assert.commandWorked(testDb.createCollection(kCollName));
assert.commandWorked(testDb.runCommand({insert: kCollName, documents: [{_id: 0, a: 1, b: 1}]}));
primary._setSecurityToken(otherSecurityToken);
assert.commandWorked(testDb.createCollection(kCollName));
assert.commandWorked(testDb.runCommand({insert: kCollName, documents: [{_id: 0, a: 1, b: 1}]}));
checkConcurrentLockDifferentTenant(primary,
kTenant,
kOtherTenant,
"hangAfterIndexBuildDumpsInsertsFromBulkLock",
"Database",
createIndexThreadFunc,
"IX");
primary._setSecurityToken(securityToken);
assert.commandWorked(testDb.runCommand({dropDatabase: 1}));
primary._setSecurityToken(otherSecurityToken);
assert.commandWorked(testDb.runCommand({dropDatabase: 1}));
}
// Check that collection validation can run concurrently for two different tenants with the same db
// name and collection name
{
function collValidateThreadFunc(host, collName, dbName, token) {
const db = new Mongo(host);
const adminDb = db.getDB('admin');
db._setSecurityToken(token);
assert(adminDb.auth('admin', 'pwd'));
const validateRes = assert.commandWorked(db.getDB(dbName).runCommand({validate: collName}));
assert(validateRes.valid, tojson(validateRes));
}
primary._setSecurityToken(securityToken);
assert.commandWorked(testDb.createCollection(kCollName));
assert.commandWorked(testDb.runCommand({insert: kCollName, documents: [{_id: 0, a: 1, b: 1}]}));
primary._setSecurityToken(otherSecurityToken);
assert.commandWorked(testDb.createCollection(kCollName));
assert.commandWorked(testDb.runCommand({insert: kCollName, documents: [{_id: 0, a: 1, b: 1}]}));
checkConcurrentLockDifferentTenant(primary,
kTenant,
kOtherTenant,
"hangDuringValidationInitialization",
"Collection",
collValidateThreadFunc,
"X" /*Not a background validation*/);
primary._setSecurityToken(securityToken);
assert.commandWorked(testDb.runCommand({dropDatabase: 1}));
primary._setSecurityToken(otherSecurityToken);
assert.commandWorked(testDb.runCommand({dropDatabase: 1}));
}
primary._setSecurityToken(undefined);
rst.stopSet();