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
|
/**
* Test the BenchRun performance tool with multi-tenancy enabled.
* @tags: [
* uses_multiple_connections,
* requires_fcv_62
* ]
*/
(function() {
"use strict";
// Create a replica set with multi-tenancy enabled.
const replSetTest = ReplSetTest({nodes: 1});
replSetTest.startSet({
setParameter: {
multitenancySupport: true,
featureFlagSecurityToken: true,
featureFlagRequireTenantID: true
}
});
replSetTest.initiate();
const primary = replSetTest.getPrimary();
const idColl = primary.getDB("test").id;
const tenantId = ObjectId();
// Get the 'test' database associated with the tenant 'tenantId'.
const tenantDB = (() => {
const tokenConn = new Mongo(primary.host);
const user = ObjectId().str;
// Create and the login to the root user such that the '$tenant' can be used.
assert.commandWorked(
tokenConn.getDB("admin").runCommand({createUser: "root", pwd: "pwd", roles: ["root"]}));
assert(tokenConn.getDB("admin").auth("root", "pwd"));
// Create the user with the required privileges.
assert.commandWorked(tokenConn.getDB("$external").runCommand({
createUser: user,
'$tenant': tenantId,
roles: [{role: 'readWriteAnyDatabase', db: 'admin'}]
}));
// Set the provided tenant id into the security token for the user.
tokenConn._setSecurityToken(
_createSecurityToken({user: user, db: '$external', tenant: tenantId}));
// Logout the root user to avoid multiple authentication.
tokenConn.getDB("admin").logout();
// Return the tenant database.
return tokenConn.getDB("test");
})();
//
// Test the 'insert' operation for the tenant 'tenantId'.
//
benchRunSync({
ops: [{
op: "insert",
writeCmd: true,
ns: idColl.getFullName(),
tenantId: tenantId,
doc: {
_id: {"#RAND_INT": [0, 10]},
}
}],
parallel: 1,
seconds: 1, // Sleep for 1 second before calling 'BenchFinish'.
host: primary.host
});
// Verify that the 'BenchRun' inserted the required documents for the tenant 'tenantId'.
let response = tenantDB.id.find().sort({_id: 1}).toArray();
assert.eq(response.length, 10);
for (let id = 0; id < 10; id++) {
assert.eq(response[id], {_id: id});
}
//
// Test the 'update' operation for the tenant 'tenantId'.
//
benchRunSync({
ops: [{
op: "update",
writeCmd: true,
ns: idColl.getFullName(),
tenantId: tenantId,
query: {_id: {"#RAND_INT": [0, 10]}},
update: {$set: {state: "updated"}}
}],
parallel: 1,
seconds: 1, // Sleep for 1 second before calling 'BenchFinish'.
host: primary.host
});
// Verify that the 'BenchRun' updated the required documents for the tenant 'tenantId'.
response = tenantDB.id.find().sort({_id: 1}).toArray();
assert.eq(response.length, 10);
for (let id = 0; id < 10; id++) {
assert.eq(response[id], {_id: id, state: "updated"});
}
//
// Test the 'find' operation for the tenant 'tenantId'.
//
let benchStats = benchRunSync({
ops: [{
op: "find",
readCmd: true,
ns: idColl.getFullName(),
tenantId: tenantId,
expected: 10, // There should be 10 documents in the collection.
handleError: true // Get 'errCount' if 'find' documents count is not equals to 'expected'.
}],
parallel: 1,
seconds: 2, // Sleep for 2 second before calling 'BenchFinish'.
host: primary.host
});
assert.eq(benchStats.errCount, 0);
//
// Test the 'findOne' operation for the tenant 'tenantId'.
//
benchStats = benchRunSync({
ops: [{
op: "findOne",
query: {_id: 5},
readCmd: true,
ns: idColl.getFullName(),
tenantId: tenantId,
expectedDoc: {_id: 5, state: "updated"}, // This is the expected returned document.
handleError: true // Get 'errCount' if returned document is not the same as 'expectedDoc'.
}],
parallel: 1,
seconds: 1, // Sleep for 2 second before calling 'BenchFinish'.
host: primary.host
});
assert.eq(benchStats.errCount, 0);
//
// Test the 'remove' operation for the tenant 'tenantId'.
//
benchRunSync({
ops: [{
op: "remove",
writeCmd: true,
ns: idColl.getFullName(),
tenantId: tenantId,
query: {_id: {"#RAND_INT": [0, 10]}},
}],
parallel: 1,
seconds: 1,
host: primary.host
});
// Verify that all documents from the collection have been removed.
response = tenantDB.id.find().toArray();
assert.eq(response.length, 0);
//
// Test the 'createIndex' operation for the tenant 'tenantId'.
//
benchRunSync({
ops: [{
op: "createIndex",
writeCmd: true,
ns: idColl.getFullName(),
tenantId: tenantId,
key: {newId: 1},
}],
parallel: 1,
seconds: 1,
host: primary.host
});
// Verify that the required index for the collection has been created.
response = tenantDB.id.getIndexes();
assert.eq(response.some(index => index.name === "newId_1"), true);
//
// Test the 'dropIndex' operation for the tenant 'tenantId'.
//
benchRunSync({
ops: [{
op: "dropIndex",
writeCmd: true,
ns: idColl.getFullName(),
tenantId: tenantId,
key: {newId: 1},
}],
parallel: 1,
seconds: 1,
host: primary.host
});
// Verify that the required index from the collection has been dropped.
response = tenantDB.id.getIndexes();
assert.eq(response.some(index => index.name === "newId_1"), false);
replSetTest.stopSet();
})();
|