-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathcluster_server_parameter_utils.js
864 lines (773 loc) · 40.5 KB
/
cluster_server_parameter_utils.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
/**
* Util functions used by cluster server parameter tests.
*
* When adding new cluster server parameter, do the following:
* If it's test-only, add its definition to kTestOnlyClusterParameters.
* Otherwise, add to kNonTestOnlyClusterParameters.
* The keyname will be the name of the cluster-wide server parameter,
* its value will be an object with at least two keys named:
* * 'default': Properties to expect on an unset CWSP
* * 'testValues': List of two other valid values of the CWSP to set for testing purposes
*
* An additional property 'featureFlag' may also be set if the parameter depends on a featureFlag.
* Use the name of the featureFlag if it is required in order to consider the parameter.
* Prefix the name with a bang '!' if it is only considered when the featureFlag is disabled.
*
* Analogously, the 'minFCV' and 'setParameters' properties may be set if the parameter can be
* tested exclusively since a certain FCV or when a server parameter has a certain value.
*
* CWSPs with the 'isSetInternally' property are set by the cluster itself, and generally not by
* the user. We don't set them nor check their value from generic tests, as we can not assume that
* they will remain at a given value. However, we check that they exist, i.e. that we can get them.
*/
import {FeatureFlagUtil} from "jstests/libs/feature_flag_util.js";
import {
makeUnsignedSecurityToken,
runCommandWithSecurityToken
} from "jstests/libs/multitenancy_utils.js";
import {ReplSetTest} from "jstests/libs/replsettest.js";
import {ShardingTest} from "jstests/libs/shardingtest.js";
export const kNonTestOnlyClusterParameters = {
changeStreamOptions: {
default: {preAndPostImages: {expireAfterSeconds: 'off'}},
testValues: [
{preAndPostImages: {expireAfterSeconds: 30}},
{preAndPostImages: {expireAfterSeconds: 20}}
],
setParameters: {'multitenancySupport': false},
},
changeStreams: {
default: {expireAfterSeconds: NumberLong(3600)},
testValues: [{expireAfterSeconds: 30}, {expireAfterSeconds: 10}],
},
defaultMaxTimeMS: {
default: {readOperations: 0},
testValues: [{readOperations: 42}, {readOperations: 60000}],
},
addOrRemoveShardInProgress: {
default: {inProgress: false},
testValues: [{inProgress: true}, {inProgress: false}],
isSetInternally: true,
},
shardedClusterCardinalityForDirectConns: {
default: {hasTwoOrMoreShards: false},
testValues: [{hasTwoOrMoreShards: true}, {hasTwoOrMoreShards: false}],
isSetInternally: true,
},
configServerReadPreferenceForCatalogQueries: {
default: {mustAlwaysUseNearest: false},
testValues: [{mustAlwaysUseNearest: true}, {mustAlwaysUseNearest: false}],
},
pauseMigrationsDuringMultiUpdates: {
default: {enabled: false},
testValues: [{enabled: true}, {enabled: false}],
isSetInternally: true,
},
internalQueryCutoffForSampleFromRandomCursor: {
default: {sampleCutoff: 0.05},
testValues: [{sampleCutoff: 0.5}, {sampleCutoff: 0.001}],
},
fleCompactionOptions: {
default: {
maxCompactionSize: NumberInt(268435456),
maxAnchorCompactionSize: NumberInt(268435456),
maxESCEntriesPerCompactionDelete: NumberInt(350000)
},
testValues: [
{
maxCompactionSize: NumberInt(123),
maxAnchorCompactionSize: NumberInt(231),
maxESCEntriesPerCompactionDelete: NumberInt(312)
},
{
maxCompactionSize: NumberInt(10000000),
maxAnchorCompactionSize: NumberInt(10000000),
maxESCEntriesPerCompactionDelete: NumberInt(350000)
}
],
},
internalSearchOptions: {
default: {oversubscriptionFactor: 1.064, batchSizeGrowthFactor: 1.5},
testValues: [
{oversubscriptionFactor: 5, batchSizeGrowthFactor: 4.5},
{oversubscriptionFactor: 1, batchSizeGrowthFactor: 1}
],
minFCV: '8.1',
},
};
export const kTestOnlyClusterParameters = {
cwspTestNeedsFeatureFlagClusterWideToaster: {
default: {intData: 16},
testValues: [{intData: 17}, {intData: 18}],
featureFlag: 'ClusterWideToaster',
},
testStrClusterParameter: {
default: {strData: 'off'},
testValues: [{strData: 'on'}, {strData: 'sleep'}],
},
testIntClusterParameter: {
default: {intData: 16},
testValues: [{intData: 17}, {intData: 18}],
},
testBoolClusterParameter: {
default: {boolData: false},
testValues: [{boolData: true}, {boolData: false}],
},
};
export const kOmittedInFTDCClusterParameterNames = ['testBoolClusterParameter'];
export const kAllClusterParameters =
Object.assign({}, kNonTestOnlyClusterParameters, kTestOnlyClusterParameters);
export const kAllClusterParameterNames = Object.keys(kAllClusterParameters);
export const kAllClusterParameterSetInternallyNames =
kAllClusterParameterNames.filter(name => kAllClusterParameters[name].isSetInternally);
export const kAllClusterParameterDefaults = kAllClusterParameterNames.map(
(name) => Object.assign({_id: name}, kAllClusterParameters[name].default));
export const kAllClusterParameterValues1 = kAllClusterParameterNames.map(
(name) => Object.assign({_id: name}, kAllClusterParameters[name].testValues[0]));
export const kAllClusterParameterValues2 = kAllClusterParameterNames.map(
(name) => Object.assign({_id: name}, kAllClusterParameters[name].testValues[1]));
export const kNonTestOnlyClusterParameterDefaults =
Object.keys(kNonTestOnlyClusterParameters)
.map((name) => Object.assign({_id: name}, kAllClusterParameters[name].default));
export function considerParameter(paramName, conn) {
// {featureFlag: 'name'} indicates that the CWSP should only be considered with the FF enabled.
function validateFeatureFlag(cp) {
if (cp.featureFlag) {
return FeatureFlagUtil.isPresentAndEnabled(conn, cp.featureFlag);
}
return true;
}
// {minFCV: "X.Y"} indicates that the CWSP should only be considered when the FCV is greater
// than or equal to the specified version.
function validateMinFCV(cp) {
if (cp.minFCV) {
const fcvDoc =
conn.getDB("admin").system.version.findOne({_id: "featureCompatibilityVersion"});
return MongoRunner.compareBinVersions(fcvDoc.version, cp.minFCV) >= 0;
}
return true;
}
// A dictionary of 'setParameters' that should be validated while considering the current CWSP.
function validateSetParameter(cp) {
if (cp.setParameters) {
for (let [param, value] of Object.entries(cp.setParameters)) {
const resp = assert.commandWorked(conn.adminCommand({getParameter: 1, [param]: 1}));
assert(typeof resp[param] === typeof value,
"Cowardly refusing to compare parameter " + param + " of type " +
typeof resp[param] + " to value of type " + typeof value);
if (resp[param] !== value) {
return false;
}
}
}
return true;
}
const cp = kAllClusterParameters[paramName] || {};
return validateFeatureFlag(cp) && validateMinFCV(cp) && validateSetParameter(cp);
}
// Set the log level for get/setClusterParameter logging to appear.
export function setupNode(conn) {
const adminDB = conn.getDB('admin');
adminDB.setLogLevel(2);
}
export function setupReplicaSet(rst) {
setupNode(rst.getPrimary());
rst.getSecondaries().forEach(function(secondary) {
setupNode(secondary);
});
}
export function setupSharded(st) {
setupNode(st.s0);
const shards = [st.rs0, st.rs1, st.rs2];
shards.forEach(function(shard) {
setupReplicaSet(shard);
});
// Wait for FCV to fully replicate on all shards before performing test commands.
st.awaitReplicationOnShards();
}
// Upserts config.clusterParameters document with w:majority via setClusterParameter.
export function runSetClusterParameter(conn, update, tenantId) {
const paramName = update._id;
if (!considerParameter(paramName, conn)) {
return;
}
if (kAllClusterParameterSetInternallyNames.includes(paramName)) {
// Skip setting parameters used internally by the cluster,
// as this may break the cluster in ways generic tests aren't prepared to handle.
return;
}
let updateCopy = Object.assign({}, update);
delete updateCopy._id;
delete updateCopy.clusterParameterTime;
const setClusterParameterDoc = {
[paramName]: updateCopy,
};
const adminDB = conn.getDB('admin');
const tenantToken =
tenantId ? makeUnsignedSecurityToken(tenantId, {expectPrefix: false}) : undefined;
assert.commandWorked(runCommandWithSecurityToken(
tenantToken, adminDB, {setClusterParameter: setClusterParameterDoc}));
}
// Runs getClusterParameter on a specific mongod or mongos node and returns true/false depending
// on whether the expected values were returned.
export function runGetClusterParameterNode(conn,
getClusterParameterArgs,
allExpectedClusterParameters,
tenantId = undefined,
omitInFTDC = false,
omittedInFTDCClusterParameters = []) {
const tenantToken =
tenantId ? makeUnsignedSecurityToken(tenantId, {expectPrefix: false}) : undefined;
const adminDB = conn.getDB('admin');
// Filter out parameters that we don't care about.
if (Array.isArray(getClusterParameterArgs)) {
getClusterParameterArgs =
getClusterParameterArgs.filter((name) => considerParameter(name, conn));
} else if ((typeof getClusterParameterArgs === 'string') &&
!considerParameter(getClusterParameterArgs, conn)) {
return true;
}
// Update getClusterParameter command and expectedClusterParameters based on whether
// omitInFTDC has been set.
let getClusterParameterCmd = {getClusterParameter: getClusterParameterArgs};
let expectedClusterParameters = allExpectedClusterParameters.slice();
if (omitInFTDC) {
getClusterParameterCmd['omitInFTDC'] = true;
expectedClusterParameters =
allExpectedClusterParameters.filter(expectedClusterParameter => {
return !omittedInFTDCClusterParameters.find(
(testOnlyParameter) => testOnlyParameter == expectedClusterParameter._id);
});
}
const actualClusterParameters = assert
.commandWorked(runCommandWithSecurityToken(
tenantToken, adminDB, getClusterParameterCmd))
.clusterParameters;
// Reindex actual based on name, and remove irrelevant field.
let actual = {};
actualClusterParameters.forEach(function(acp) {
actual[acp._id] = acp;
delete actual[acp._id].clusterParameterTime;
});
for (let i = 0; i < expectedClusterParameters.length; i++) {
if (!considerParameter(expectedClusterParameters[i]._id, conn)) {
continue;
}
const id = expectedClusterParameters[i]._id;
if (actual[id] === undefined) {
jsTest.log('Expected to retreive ' + id + ' but it was not returned');
return false;
}
if (kAllClusterParameterSetInternallyNames.includes(expectedClusterParameters[i]._id)) {
// Skip validation of parameters used internally by the cluster,
// as their values could change for reasons outside the control of the tests.
continue;
}
if (bsonWoCompare(expectedClusterParameters[i], actual[id]) !== 0) {
jsTest.log('Server parameter mismatch on node: ' + conn.host + '\n' +
'Expected: ' + tojson(expectedClusterParameters[i]) + '\n' +
'Actual: ' + tojson(actual[id]));
return false;
}
}
// Finally, if omitInFTDC is true, assert that all redacted cluster parameters are not
// in the reply.
if (omitInFTDC) {
for (let i = 0; i < omittedInFTDCClusterParameters.length; i++) {
if (!considerParameter(omittedInFTDCClusterParameters[i], conn)) {
continue;
}
if (actual[omittedInFTDCClusterParameters[i]] !== undefined) {
jsTest.log('getClusterParameter returned parameter ' +
omittedInFTDCClusterParameters[i] +
', Actual reply: ' + tojson(actual[omittedInFTDCClusterParameters[i]]));
return false;
}
}
}
return true;
}
// Runs getClusterParameter on each replica set node and asserts that the response matches the
// expected parameter objects on at least a majority of nodes.
export function runGetClusterParameterReplicaSet(rst,
getClusterParameterArgs,
expectedClusterParameters,
tenantId = undefined,
omitInFTDC = false,
omittedInFTDCClusterParameters = []) {
let numMatches = 0;
const numTotalNodes = rst.getSecondaries().length + 1;
if (runGetClusterParameterNode(rst.getPrimary(),
getClusterParameterArgs,
expectedClusterParameters,
tenantId,
omitInFTDC,
omittedInFTDCClusterParameters)) {
numMatches++;
}
rst.awaitReplication();
rst.getSecondaries().forEach(function(secondary) {
if (runGetClusterParameterNode(secondary,
getClusterParameterArgs,
expectedClusterParameters,
tenantId,
omitInFTDC,
omittedInFTDCClusterParameters)) {
numMatches++;
}
});
assert((numMatches / numTotalNodes) > 0.5);
}
// Runs getClusterParameter on mongos, each mongod in each shard replica set, and each mongod in
// the config server replica set.
export function runGetClusterParameterSharded(st,
getClusterParameterArgs,
expectedClusterParameters,
tenantId = undefined,
omitInFTDC = false,
omittedInFTDCClusterParameters = []) {
const shards = [st.rs0, st.rs1, st.rs2];
shards.forEach((shard) => shard.awaitReplication());
assert(runGetClusterParameterNode(st.s0,
getClusterParameterArgs,
expectedClusterParameters,
tenantId,
omitInFTDC,
omittedInFTDCClusterParameters));
runGetClusterParameterReplicaSet(st.configRS,
getClusterParameterArgs,
expectedClusterParameters,
tenantId,
omitInFTDC,
omittedInFTDCClusterParameters);
shards.forEach(function(shard) {
runGetClusterParameterReplicaSet(shard,
getClusterParameterArgs,
expectedClusterParameters,
tenantId,
omitInFTDC,
omittedInFTDCClusterParameters);
});
}
// Tests valid usages of set/getClusterParameter and verifies that the expected values are returned.
export function testValidClusterParameterCommands(conn) {
if (conn instanceof ReplSetTest) {
// Run getClusterParameter in list format and '*' with omitInFTDC = true and ensure that
// it does not return any parameters that should be omitted for FTDC.
runGetClusterParameterReplicaSet(conn,
kAllClusterParameterNames,
kAllClusterParameterDefaults,
undefined,
true /* omitInFTDC */,
kOmittedInFTDCClusterParameterNames);
runGetClusterParameterReplicaSet(conn,
'*',
kAllClusterParameterDefaults,
undefined,
true /* omitInFTDC */,
kOmittedInFTDCClusterParameterNames);
// Run getClusterParameter in list format and '*' and ensure it returns all default values
// on all nodes in the replica set.
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames, kAllClusterParameterDefaults);
runGetClusterParameterReplicaSet(conn, '*', kAllClusterParameterDefaults);
// For each parameter, run setClusterParameter and verify that getClusterParameter
// returns the updated value on all nodes in the replica set.
for (let i = 0; i < kAllClusterParameterNames.length; i++) {
runSetClusterParameter(conn.getPrimary(), kAllClusterParameterValues1[i]);
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames[i], [kAllClusterParameterValues1[i]]);
// Verify that document updates are also handled properly.
runSetClusterParameter(conn.getPrimary(), kAllClusterParameterValues2[i]);
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames[i], [kAllClusterParameterValues2[i]]);
}
// Finally, run getClusterParameter in list format and '*' and ensure that they now all
// return updated values.
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames, kAllClusterParameterValues2);
runGetClusterParameterReplicaSet(conn, '*', kAllClusterParameterValues2);
} else if (conn instanceof ShardingTest) {
// Run getClusterParameter in list format and '*' and ensure it returns all default values
// on all nodes in the sharded cluster.
runGetClusterParameterSharded(
conn, kAllClusterParameterNames, kAllClusterParameterDefaults);
runGetClusterParameterSharded(conn, '*', kAllClusterParameterDefaults);
// For each parameter, simulate setClusterParameter and verify that getClusterParameter
// returns the updated value on all nodes in the sharded cluster.
for (let i = 0; i < kAllClusterParameterNames.length; i++) {
runSetClusterParameter(conn.s0, kAllClusterParameterValues1[i]);
runGetClusterParameterSharded(
conn, kAllClusterParameterNames[i], [kAllClusterParameterValues1[i]]);
// Verify that document updates are also handled properly.
runSetClusterParameter(conn.s0, kAllClusterParameterValues2[i]);
runGetClusterParameterSharded(
conn, kAllClusterParameterNames[i], [kAllClusterParameterValues2[i]]);
}
// Finally, run getClusterParameter in list format and '*' and ensure that they now all
// return updated values.
runGetClusterParameterSharded(conn, kAllClusterParameterNames, kAllClusterParameterValues2);
runGetClusterParameterSharded(conn, '*', kAllClusterParameterValues2);
} else { // Standalone
// Run getClusterParameter in list format and '*' and ensure it returns all default values.
assert(runGetClusterParameterNode(
conn, kAllClusterParameterNames, kAllClusterParameterDefaults));
assert(runGetClusterParameterNode(conn, '*', kAllClusterParameterDefaults));
// For each parameter, run setClusterParameter and verify that getClusterParameter
// returns the updated value.
for (let i = 0; i < kAllClusterParameterNames.length; i++) {
runSetClusterParameter(conn, kAllClusterParameterValues1[i]);
assert(runGetClusterParameterNode(
conn, kAllClusterParameterNames[i], [kAllClusterParameterValues1[i]]));
// Verify that document updates are also handled properly.
runSetClusterParameter(conn, kAllClusterParameterValues2[i]);
assert(runGetClusterParameterNode(
conn, kAllClusterParameterNames[i], [kAllClusterParameterValues2[i]]));
}
// Finally, run getClusterParameter in list format and '*' and ensure that they now return
// updated values.
assert(runGetClusterParameterNode(
conn, kAllClusterParameterNames, kAllClusterParameterValues2));
assert(runGetClusterParameterNode(conn, '*', kAllClusterParameterValues2));
}
}
export const tenantId1 = ObjectId();
export const tenantId2 = ObjectId();
// Tests valid usages of set/getClusterParameter on a serverless replica set and verifies that the
// expected values are returned.
export function testValidServerlessClusterParameterCommands(conn) {
assert(conn instanceof ReplSetTest);
assert(
!considerParameter("changeStreamOptions", conn.getPrimary()),
"As changeStreamOptions is not supported for serverless configurations, the feature flag ServerlessChangeStreams must be enabled to run this test.");
// Run getClusterParameter in list format and '*' and ensure it returns all default values
// on all nodes in the replica set, with and without tenantId set.
runGetClusterParameterReplicaSet(conn, kAllClusterParameterNames, kAllClusterParameterDefaults);
runGetClusterParameterReplicaSet(conn, '*', kAllClusterParameterDefaults);
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames, kAllClusterParameterDefaults, tenantId1);
runGetClusterParameterReplicaSet(conn, '*', kAllClusterParameterDefaults, tenantId1);
// Run getClusterParameter in list format and '*' and ensure that it filters out parameters
// that should be omitted in FTDC when omitInFTDC is set for a specific tenant.
runGetClusterParameterReplicaSet(conn,
kAllClusterParameterNames,
kAllClusterParameterDefaults,
tenantId1,
true /* omitInFTDC */,
kOmittedInFTDCClusterParameterNames);
runGetClusterParameterReplicaSet(conn,
'*',
kAllClusterParameterDefaults,
tenantId1,
true /* omitInFTDC */,
kOmittedInFTDCClusterParameterNames);
// For each parameter, run setClusterParameter and verify that getClusterParameter
// returns the updated value on all nodes in the replica set for the tenant it was set on.
for (let i = 0; i < kAllClusterParameterNames.length; i++) {
// Test inserts for tenant1 and default tenant.
runSetClusterParameter(conn.getPrimary(), kAllClusterParameterValues1[i], tenantId1);
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames[i], [kAllClusterParameterValues1[i]], tenantId1);
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames[i], [kAllClusterParameterDefaults[i]], tenantId2);
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames[i], [kAllClusterParameterDefaults[i]]);
runSetClusterParameter(conn.getPrimary(), kAllClusterParameterValues2[i]);
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames[i], [kAllClusterParameterValues2[i]]);
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames[i], [kAllClusterParameterValues1[i]], tenantId1);
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames[i], [kAllClusterParameterDefaults[i]], tenantId2);
// Test updates for tenant1 and default tenant.
runSetClusterParameter(conn.getPrimary(), kAllClusterParameterValues1[i]);
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames[i], [kAllClusterParameterValues1[i]]);
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames[i], [kAllClusterParameterValues1[i]], tenantId1);
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames[i], [kAllClusterParameterDefaults[i]], tenantId2);
runSetClusterParameter(conn.getPrimary(), kAllClusterParameterValues2[i], tenantId1);
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames[i], [kAllClusterParameterValues1[i]]);
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames[i], [kAllClusterParameterValues2[i]], tenantId1);
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames[i], [kAllClusterParameterDefaults[i]], tenantId2);
}
// Finally, run getClusterParameter in list format and '*' and ensure that they now all
// return updated values.
runGetClusterParameterReplicaSet(conn, kAllClusterParameterNames, kAllClusterParameterValues1);
runGetClusterParameterReplicaSet(conn, '*', kAllClusterParameterValues1);
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames, kAllClusterParameterValues2, tenantId1);
runGetClusterParameterReplicaSet(conn, '*', kAllClusterParameterValues2, tenantId1);
runGetClusterParameterReplicaSet(
conn, kAllClusterParameterNames, kAllClusterParameterDefaults, tenantId2);
runGetClusterParameterReplicaSet(conn, '*', kAllClusterParameterDefaults, tenantId2);
}
// Assert that explicitly getting a disabled cluster server parameter fails on a node.
export function testExplicitDisabledGetClusterParameter(conn, tenantId) {
const tenantToken =
tenantId ? makeUnsignedSecurityToken(tenantId, {expectPrefix: false}) : undefined;
const adminDB = conn.getDB('admin');
assert.commandFailedWithCode(
runCommandWithSecurityToken(
tenantToken, adminDB, {getClusterParameter: "testIntClusterParameter"}),
ErrorCodes.BadValue);
assert.commandFailedWithCode(
runCommandWithSecurityToken(tenantToken, adminDB, {
getClusterParameter: ["changeStreamOptions", "testIntClusterParameter"]
}),
ErrorCodes.BadValue);
}
// Tests that disabled cluster server parameters return errors or are filtered out as appropriate
// by get/setClusterParameter.
export function testDisabledClusterParameters(conn, tenantId) {
const tenantToken =
tenantId ? makeUnsignedSecurityToken(tenantId, {expectPrefix: false}) : undefined;
if (conn instanceof ReplSetTest) {
// Assert that explicitly setting a disabled cluster server parameter fails.
const adminDB = conn.getPrimary().getDB('admin');
assert.commandFailedWithCode(
runCommandWithSecurityToken(tenantToken, adminDB, {
setClusterParameter: {testIntClusterParameter: {intData: 5}}
}),
ErrorCodes.BadValue);
// Assert that explicitly getting a disabled cluster server parameter fails on the primary.
testExplicitDisabledGetClusterParameter(conn.getPrimary(), tenantId);
// Assert that explicitly getting a disabled cluster server parameter fails on secondaries.
conn.getSecondaries().forEach(function(secondary) {
testExplicitDisabledGetClusterParameter(secondary, tenantId);
});
// Assert that getClusterParameter: '*' succeeds but only returns enabled cluster
// parameters.
runGetClusterParameterReplicaSet(conn, '*', kNonTestOnlyClusterParameterDefaults, tenantId);
} else if (conn instanceof ShardingTest) {
// Assert that explicitly setting a disabled cluster server parameter fails.
const adminDB = conn.s0.getDB('admin');
assert.commandFailedWithCode(
runCommandWithSecurityToken(tenantToken, adminDB, {
setClusterParameter: {testIntClusterParameter: {intData: 5}}
}),
ErrorCodes.BadValue);
// Assert that explicitly getting a disabled cluster server parameter fails on mongos.
testExplicitDisabledGetClusterParameter(conn.s0, tenantId);
// Assert that explicitly getting a disabled cluster server parameter on each shard replica
// set and the config replica set fails.
const shards = [conn.rs0, conn.rs1, conn.rs2];
const configRS = conn.configRS;
shards.forEach(function(shard) {
testExplicitDisabledGetClusterParameter(shard.getPrimary(), tenantId);
shard.getSecondaries().forEach(function(secondary) {
testExplicitDisabledGetClusterParameter(secondary, tenantId);
});
});
testExplicitDisabledGetClusterParameter(configRS.getPrimary());
configRS.getSecondaries().forEach(function(secondary) {
testExplicitDisabledGetClusterParameter(secondary, tenantId);
});
// Assert that getClusterParameter: '*' succeeds but only returns enabled cluster
// parameters.
runGetClusterParameterSharded(conn, '*', kNonTestOnlyClusterParameterDefaults, tenantId);
} else { // Standalone
// Assert that explicitly setting a disabled cluster server parameter fails.
const adminDB = conn.getDB('admin');
assert.commandFailedWithCode(
runCommandWithSecurityToken(tenantToken, adminDB, {
setClusterParameter: {testIntClusterParameter: {intData: 5}}
}),
ErrorCodes.BadValue);
// Assert that explicitly getting a disabled cluster server parameter fails.
testExplicitDisabledGetClusterParameter(conn, tenantId);
// Assert that getClusterParameter: '*' succeeds but only returns enabled cluster
// parameters.
assert(
runGetClusterParameterNode(conn, '*', kNonTestOnlyClusterParameterDefaults, tenantId));
}
}
// Tests that invalid uses of getClusterParameter fails on a given node.
export function testInvalidGetClusterParameter(conn, tenantId) {
const tenantToken =
tenantId ? makeUnsignedSecurityToken(tenantId, {expectPrefix: false}) : undefined;
const adminDB = conn.getDB('admin');
// Assert that specifying a nonexistent parameter returns an error.
assert.commandFailedWithCode(
runCommandWithSecurityToken(
tenantToken, adminDB, {getClusterParameter: "nonexistentParam"}),
ErrorCodes.NoSuchKey);
assert.commandFailedWithCode(
runCommandWithSecurityToken(
tenantToken, adminDB, {getClusterParameter: ["nonexistentParam"]}),
ErrorCodes.NoSuchKey);
assert.commandFailedWithCode(
runCommandWithSecurityToken(tenantToken, adminDB, {
getClusterParameter: ["testIntClusterParameter", "nonexistentParam"]
}),
ErrorCodes.NoSuchKey);
assert.commandFailedWithCode(
runCommandWithSecurityToken(tenantToken, adminDB, {getClusterParameter: []}),
ErrorCodes.BadValue);
}
// Tests that invalid uses of set/getClusterParameter fail with the appropriate errors.
export function testInvalidClusterParameterCommands(conn, tenantId) {
const tenantToken =
tenantId ? makeUnsignedSecurityToken(tenantId, {expectPrefix: false}) : undefined;
if (conn instanceof ReplSetTest) {
const adminDB = conn.getPrimary().getDB('admin');
// Assert that invalid uses of getClusterParameter fail on the primary.
testInvalidGetClusterParameter(conn.getPrimary(), tenantId);
// Assert that setting a nonexistent parameter on the primary returns an error.
assert.commandFailed(runCommandWithSecurityToken(
tenantToken, adminDB, {setClusterParameter: {nonexistentParam: {intData: 5}}}));
// Assert that running setClusterParameter with a scalar value fails.
assert.commandFailed(runCommandWithSecurityToken(
tenantToken, adminDB, {setClusterParameter: {testIntClusterParameter: 5}}));
conn.getSecondaries().forEach(function(secondary) {
// Assert that setClusterParameter cannot be run on a secondary.
const secondaryAdminDB = secondary.getDB('admin');
assert.commandFailedWithCode(
runCommandWithSecurityToken(tenantToken, secondaryAdminDB, {
setClusterParameter: {testIntClusterParameter: {intData: 5}}
}),
ErrorCodes.NotWritablePrimary);
// Assert that invalid uses of getClusterParameter fail on secondaries.
testInvalidGetClusterParameter(secondary, tenantId);
});
// Assert that invalid direct writes to <tenantId>_config.clusterParameters fail.
assert.commandFailed(conn.getPrimary().getDB("config").clusterParameters.insert({
_id: 'testIntClusterParameter',
foo: 'bar',
clusterParameterTime: {"$timestamp": {t: 0, i: 0}}
}));
} else if (conn instanceof ShardingTest) {
const adminDB = conn.s0.getDB('admin');
// Assert that invalid uses of getClusterParameter fail on mongos.
testInvalidGetClusterParameter(conn.s0, tenantId);
// Assert that setting a nonexistent parameter on the mongos returns an error.
assert.commandFailed(runCommandWithSecurityToken(
tenantToken, adminDB, {setClusterParameter: {nonexistentParam: {intData: 5}}}));
// Assert that running setClusterParameter with a scalar value fails.
assert.commandFailed(runCommandWithSecurityToken(
tenantToken, adminDB, {setClusterParameter: {testIntClusterParameter: 5}}));
const shards = [conn.rs0, conn.rs1, conn.rs2];
shards.forEach(function(shard) {
// Assert that setClusterParameter cannot be run directly on a shard primary.
const shardPrimaryAdmin = shard.getPrimary().getDB('admin');
assert.commandFailedWithCode(
runCommandWithSecurityToken(tenantToken, shardPrimaryAdmin, {
setClusterParameter: {testIntClusterParameter: {intData: 5}}
}),
ErrorCodes.NotImplemented);
// Assert that invalid forms of getClusterParameter fail on the shard primary.
testInvalidGetClusterParameter(shard.getPrimary(), tenantId);
shard.getSecondaries().forEach(function(secondary) {
// Assert that setClusterParameter cannot be run on a shard secondary.
const shardSecondaryAdmin = secondary.getDB('admin');
assert.commandFailedWithCode(
runCommandWithSecurityToken(tenantToken, shardSecondaryAdmin, {
setClusterParameter: {testIntClusterParameter: {intData: 5}}
}),
ErrorCodes.NotWritablePrimary);
// Assert that invalid forms of getClusterParameter fail on shard secondaries.
testInvalidGetClusterParameter(secondary, tenantId);
});
});
// Assert that setClusterParameter cannot be run directly on the configsvr primary.
const configRS = conn.configRS;
const configPrimaryAdmin = configRS.getPrimary().getDB('admin');
assert.commandFailedWithCode(
runCommandWithSecurityToken(tenantToken, configPrimaryAdmin, {
setClusterParameter: {testIntClusterParameter: {intData: 5}}
}),
ErrorCodes.NotImplemented);
// Assert that invalid forms of getClusterParameter fail on the configsvr primary.
testInvalidGetClusterParameter(configRS.getPrimary(), tenantId);
configRS.getSecondaries().forEach(function(secondary) {
// Assert that setClusterParameter cannot be run on a configsvr secondary.
const configSecondaryAdmin = secondary.getDB('admin');
assert.commandFailedWithCode(
runCommandWithSecurityToken(tenantToken, configSecondaryAdmin, {
setClusterParameter: {testIntClusterParameter: {intData: 5}}
}),
ErrorCodes.NotWritablePrimary);
// Assert that invalid forms of getClusterParameter fail on configsvr secondaries.
testInvalidGetClusterParameter(secondary, tenantId);
});
// Assert that invalid direct writes to <tenantId>_config.clusterParameters fail.
assert.commandFailed(configRS.getPrimary().getDB("config").clusterParameters.insert({
_id: 'testIntClusterParameter',
foo: 'bar',
clusterParameterTime: {"$timestamp": {t: 0, i: 0}}
}));
shards.forEach(function(shard) {
assert.commandFailed(shard.getPrimary().getDB("config").clusterParameters.insert({
_id: 'testIntClusterParameter',
foo: 'bar',
clusterParameterTime: {"$timestamp": {t: 0, i: 0}}
}));
});
} else { // Standalone
const adminDB = conn.getDB('admin');
// Assert that invalid uses of getClusterParameter fail.
testInvalidGetClusterParameter(conn, tenantId);
// Assert that setting a nonexistent parameter returns an error.
assert.commandFailed(runCommandWithSecurityToken(
tenantToken, adminDB, {setClusterParameter: {nonexistentParam: {intData: 5}}}));
// Assert that running setClusterParameter with a scalar value fails.
assert.commandFailed(runCommandWithSecurityToken(
tenantToken, adminDB, {setClusterParameter: {testIntClusterParameter: 5}}));
// Assert that invalid direct writes to <tenantId>_config.clusterParameters fail.
assert.commandFailed(conn.getDB("config").clusterParameters.insert({
_id: 'testIntClusterParameter',
foo: 'bar',
clusterParameterTime: {"$timestamp": {t: 0, i: 0}}
}));
}
}
// name => name of cluster parameter to get
// expectedValue => document that should be equal to document describing CP's value, excluding the
// _id
function checkGetClusterParameterMatch(db, tenantToken, name, expectedValue) {
const cps = assert
.commandWorked(
runCommandWithSecurityToken(tenantToken, db, {getClusterParameter: name}))
.clusterParameters;
// confirm we got the document we were looking for.
assert.eq(cps.length, 1);
let actualCp = cps[0];
assert.eq(actualCp._id, name);
// confirm the value is expected.
// remove the id and clusterParameterTime fields
delete actualCp._id;
delete actualCp.clusterParameterTime;
assert(bsonWoCompare(actualCp, expectedValue) !== 0,
'Server parameter mismatch for parameter ' +
'\n' +
'Expected: ' + tojson(expectedValue) + '\n' +
'Actual: ' + tojson(actualCp));
}
// Tests that getClusterParameter: "*" all have an _id element, and that all the parameters
// it returns match the result of directly querying with getClusterParameter: <paramName>.
export function testGetClusterParameterStar(conn, tenantId) {
let adminDB, getClusterParameterFn;
if (conn instanceof ReplSetTest) {
adminDB = conn.getPrimary().getDB('admin');
} else if (conn instanceof ShardingTest) {
adminDB = conn.s0.getDB('admin');
} else {
adminDB = conn.getDB('admin');
}
const tenantToken =
tenantId ? makeUnsignedSecurityToken(tenantId, {expectPrefix: false}) : undefined;
const allParameters = assert
.commandWorked(runCommandWithSecurityToken(
tenantToken, adminDB, {getClusterParameter: '*'}))
.clusterParameters;
for (const param of allParameters) {
assert(param.hasOwnProperty("_id"),
'Entry in {getClusterParameter: "*"} result is missing _id key:\n' + tojson(param));
const name = param["_id"];
checkGetClusterParameterMatch(adminDB, tenantToken, name, param);
}
}