1
- // Tests writeConcern metrics in the serverStatus output.
1
+ // Tests writeConcern metrics in the serverStatus output for a replica set .
2
2
// @tags : [
3
3
// requires_persistence,
4
4
// requires_replication,
5
5
// ]
6
6
// Verifies that the server status response has the fields that we expect.
7
- function verifyServerStatusFields ( serverStatusResponse ) {
7
+ export function verifyServerStatusFields ( serverStatusResponse ) {
8
8
assert ( serverStatusResponse . hasOwnProperty ( "opWriteConcernCounters" ) ,
9
9
"Expected the serverStatus response to have a 'opWriteConcernCounters' field\n" +
10
10
tojson ( serverStatusResponse ) ) ;
@@ -21,7 +21,7 @@ function verifyServerStatusFields(serverStatusResponse) {
21
21
22
22
// Verifies that the given path of the server status response is incremented in the way we
23
23
// expect, and no other changes occurred. This function modifies its inputs.
24
- function verifyServerStatusChange ( initialStats , newStats , paths , expectedIncrement ) {
24
+ export function verifyServerStatusChange ( initialStats , newStats , paths , expectedIncrement ) {
25
25
paths . forEach ( path => {
26
26
// Traverse to the parent of the changed element.
27
27
let pathComponents = path . split ( "." ) ;
@@ -66,6 +66,16 @@ function verifyServerStatusChange(initialStats, newStats, paths, expectedIncreme
66
66
", initialStats: " + tojson ( initialStats ) + ", newStats: " + tojson ( newStats ) ) ;
67
67
}
68
68
69
+ // Generate commands that will be using default write concern.
70
+ export function generateCmdsWithNoWCProvided ( cmd ) {
71
+ return [
72
+ cmd ,
73
+ // Missing 'w' field will be filled with default write concern.
74
+ Object . assign ( Object . assign ( { } , cmd ) , { writeConcern : { j : true } } ) ,
75
+ Object . assign ( Object . assign ( { } , cmd ) , { writeConcern : { wtimeout : 2000 } } )
76
+ ] ;
77
+ }
78
+
69
79
let rst ;
70
80
let primary ;
71
81
let secondary ;
@@ -104,23 +114,28 @@ function resetCollection(setupCommand) {
104
114
}
105
115
106
116
function testWriteConcernMetrics ( cmd , opName , inc , isPSASet , setupCommand ) {
117
+ jsTestLog ( "Testing " + opName + " - IsPSA: " + isPSASet ) ;
107
118
initializeReplicaSet ( isPSASet ) ;
108
119
109
120
// Run command with no writeConcern and no CWWC set.
110
121
resetCollection ( setupCommand ) ;
111
- let serverStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
112
- verifyServerStatusFields ( serverStatus ) ;
113
- assert . commandWorked ( testDB . runCommand ( cmd ) ) ;
114
- let newStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
115
- verifyServerStatusChange ( serverStatus . opWriteConcernCounters ,
116
- newStatus . opWriteConcernCounters ,
117
- [
118
- opName +
119
- ( isPSASet ? ".noneInfo.implicitDefault.wnum.1"
120
- : ".noneInfo.implicitDefault.wmajority" ) ,
121
- opName + ".none"
122
- ] ,
123
- inc ) ;
122
+ const cmdsWithNoWCProvided = generateCmdsWithNoWCProvided ( cmd ) ;
123
+ let serverStatus , newStatus ;
124
+ cmdsWithNoWCProvided . forEach ( cmd => {
125
+ serverStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
126
+ verifyServerStatusFields ( serverStatus ) ;
127
+ assert . commandWorked ( testDB . runCommand ( cmd ) ) ;
128
+ newStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
129
+ verifyServerStatusChange ( serverStatus . opWriteConcernCounters ,
130
+ newStatus . opWriteConcernCounters ,
131
+ [
132
+ opName +
133
+ ( isPSASet ? ".noneInfo.implicitDefault.wnum.1"
134
+ : ".noneInfo.implicitDefault.wmajority" ) ,
135
+ opName + ".none"
136
+ ] ,
137
+ inc ) ;
138
+ } ) ;
124
139
125
140
// Run command with no writeConcern with CWWC set to majority.
126
141
resetCollection ( setupCommand ) ;
@@ -129,40 +144,31 @@ function testWriteConcernMetrics(cmd, opName, inc, isPSASet, setupCommand) {
129
144
defaultWriteConcern : { w : "majority" } ,
130
145
writeConcern : { w : "majority" }
131
146
} ) ) ;
132
- serverStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
133
- verifyServerStatusFields ( serverStatus ) ;
134
- assert . commandWorked ( testDB . runCommand ( cmd ) ) ;
135
- newStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
136
- verifyServerStatusChange ( serverStatus . opWriteConcernCounters ,
137
- newStatus . opWriteConcernCounters ,
138
- [ opName + ".noneInfo.CWWC.wmajority" , opName + ".none" ] ,
139
- inc ) ;
147
+ cmdsWithNoWCProvided . forEach ( cmd => {
148
+ serverStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
149
+ verifyServerStatusFields ( serverStatus ) ;
150
+ assert . commandWorked ( testDB . runCommand ( cmd ) ) ;
151
+ newStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
152
+ verifyServerStatusChange ( serverStatus . opWriteConcernCounters ,
153
+ newStatus . opWriteConcernCounters ,
154
+ [ opName + ".noneInfo.CWWC.wmajority" , opName + ".none" ] ,
155
+ inc ) ;
156
+ } ) ;
140
157
141
158
// Run command with no writeConcern with CWWC set to w:1.
142
159
resetCollection ( setupCommand ) ;
143
160
assert . commandWorked ( primary . adminCommand (
144
161
{ setDefaultRWConcern : 1 , defaultWriteConcern : { w : 1 } , writeConcern : { w : "majority" } } ) ) ;
145
- serverStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
146
- verifyServerStatusFields ( serverStatus ) ;
147
- assert . commandWorked ( testDB . runCommand ( cmd ) ) ;
148
- newStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
149
- verifyServerStatusChange ( serverStatus . opWriteConcernCounters ,
150
- newStatus . opWriteConcernCounters ,
151
- [ opName + ".noneInfo.CWWC.wnum.1" , opName + ".none" ] ,
152
- inc ) ;
153
-
154
- // Run command with no writeConcern and with CWWC set to j:true.
155
- resetCollection ( setupCommand ) ;
156
- assert . commandWorked ( primary . adminCommand (
157
- { setDefaultRWConcern : 1 , defaultWriteConcern : { j : true } , writeConcern : { w : "majority" } } ) ) ;
158
- serverStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
159
- verifyServerStatusFields ( serverStatus ) ;
160
- assert . commandWorked ( testDB . runCommand ( cmd ) ) ;
161
- newStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
162
- verifyServerStatusChange ( serverStatus . opWriteConcernCounters ,
163
- newStatus . opWriteConcernCounters ,
164
- [ opName + ".noneInfo.CWWC.wnum.1" , opName + ".none" ] ,
165
- inc ) ;
162
+ cmdsWithNoWCProvided . forEach ( cmd => {
163
+ serverStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
164
+ verifyServerStatusFields ( serverStatus ) ;
165
+ assert . commandWorked ( testDB . runCommand ( cmd ) ) ;
166
+ newStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
167
+ verifyServerStatusChange ( serverStatus . opWriteConcernCounters ,
168
+ newStatus . opWriteConcernCounters ,
169
+ [ opName + ".noneInfo.CWWC.wnum.1" , opName + ".none" ] ,
170
+ inc ) ;
171
+ } ) ;
166
172
167
173
// Run command with no writeConcern and with CWWC set with (w: "myTag").
168
174
resetCollection ( setupCommand ) ;
@@ -171,82 +177,79 @@ function testWriteConcernMetrics(cmd, opName, inc, isPSASet, setupCommand) {
171
177
defaultWriteConcern : { w : "myTag" } ,
172
178
writeConcern : { w : "majority" }
173
179
} ) ) ;
174
- serverStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
175
- verifyServerStatusFields ( serverStatus ) ;
176
- assert . commandWorked ( testDB . runCommand ( cmd ) ) ;
177
- newStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
178
- verifyServerStatusChange ( serverStatus . opWriteConcernCounters ,
179
- newStatus . opWriteConcernCounters ,
180
- [ opName + ".noneInfo.CWWC.wtag.myTag" , opName + ".none" ] ,
181
- inc ) ;
182
-
183
- // Run command with writeConcern {j: true}. This should be counted as having no 'w' value.
184
- resetCollection ( setupCommand ) ;
185
- serverStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
186
- verifyServerStatusFields ( serverStatus ) ;
187
- assert . commandWorked (
188
- testDB . runCommand ( Object . assign ( Object . assign ( { } , cmd ) , { writeConcern : { j : true } } ) ) ) ;
189
- newStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
190
- verifyServerStatusChange ( serverStatus . opWriteConcernCounters ,
191
- newStatus . opWriteConcernCounters ,
192
- [ opName + ".noneInfo.implicitDefault.wnum.1" , opName + ".none" ] ,
193
- inc ) ;
180
+ cmdsWithNoWCProvided . forEach ( cmd => {
181
+ serverStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
182
+ verifyServerStatusFields ( serverStatus ) ;
183
+ assert . commandWorked ( testDB . runCommand ( cmd ) ) ;
184
+ newStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
185
+ verifyServerStatusChange ( serverStatus . opWriteConcernCounters ,
186
+ newStatus . opWriteConcernCounters ,
187
+ [ opName + ".noneInfo.CWWC.wtag.myTag" , opName + ".none" ] ,
188
+ inc ) ;
189
+ } ) ;
194
190
195
191
// Run command with writeConcern {w: "majority"}.
196
192
resetCollection ( setupCommand ) ;
197
- serverStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
193
+ serverStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
198
194
verifyServerStatusFields ( serverStatus ) ;
199
195
assert . commandWorked (
200
196
testDB . runCommand ( Object . assign ( Object . assign ( { } , cmd ) , { writeConcern : { w : "majority" } } ) ) ) ;
201
- newStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
197
+ newStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
202
198
verifyServerStatusChange ( serverStatus . opWriteConcernCounters ,
203
199
newStatus . opWriteConcernCounters ,
204
200
[ opName + ".wmajority" ] ,
205
201
inc ) ;
206
202
207
203
// Run command with writeConcern {w: 0}.
208
204
resetCollection ( setupCommand ) ;
209
- serverStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
205
+ serverStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
210
206
verifyServerStatusFields ( serverStatus ) ;
211
207
assert . commandWorked (
212
208
testDB . runCommand ( Object . assign ( Object . assign ( { } , cmd ) , { writeConcern : { w : 0 } } ) ) ) ;
213
- newStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
214
- verifyServerStatusChange ( serverStatus . opWriteConcernCounters ,
215
- newStatus . opWriteConcernCounters ,
216
- [ opName + ".wnum.0" ] ,
217
- inc ) ;
209
+ assert . soon ( ( ) => {
210
+ newStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
211
+ try {
212
+ verifyServerStatusChange ( serverStatus . opWriteConcernCounters ,
213
+ newStatus . opWriteConcernCounters ,
214
+ [ opName + ".wnum.0" ] ,
215
+ inc ) ;
216
+ } catch ( e ) {
217
+ return false ;
218
+ }
219
+ return true ;
220
+ } ) ;
218
221
219
222
// Run command with writeConcern {w: 1}.
220
223
resetCollection ( setupCommand ) ;
221
- serverStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
224
+ serverStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
222
225
verifyServerStatusFields ( serverStatus ) ;
223
226
assert . commandWorked (
224
227
testDB . runCommand ( Object . assign ( Object . assign ( { } , cmd ) , { writeConcern : { w : 1 } } ) ) ) ;
225
- newStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
228
+ newStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
226
229
verifyServerStatusChange ( serverStatus . opWriteConcernCounters ,
227
230
newStatus . opWriteConcernCounters ,
228
231
[ opName + ".wnum.1" ] ,
229
232
inc ) ;
230
233
231
234
// Run command with writeConcern {w: 2}.
232
235
resetCollection ( setupCommand ) ;
233
- serverStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
236
+ serverStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
234
237
verifyServerStatusFields ( serverStatus ) ;
235
238
assert . commandWorked (
236
239
testDB . runCommand ( Object . assign ( Object . assign ( { } , cmd ) , { writeConcern : { w : 2 } } ) ) ) ;
237
- newStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
240
+ newStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
238
241
verifyServerStatusChange ( serverStatus . opWriteConcernCounters ,
239
242
newStatus . opWriteConcernCounters ,
240
243
[ opName + ".wnum.2" ] ,
241
244
inc ) ;
242
245
243
246
// Run command with writeConcern {w: "myTag"}.
244
247
resetCollection ( setupCommand ) ;
245
- serverStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
248
+ serverStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
246
249
verifyServerStatusFields ( serverStatus ) ;
247
250
assert . commandWorked (
248
251
testDB . runCommand ( Object . assign ( Object . assign ( { } , cmd ) , { writeConcern : { w : "myTag" } } ) ) ) ;
249
- newStatus = assert . commandWorked ( testDB . adminCommand ( { serverStatus : 1 } ) ) ;
252
+ newStatus = assert . commandWorked ( primary . adminCommand ( { serverStatus : 1 } ) ) ;
250
253
verifyServerStatusChange ( serverStatus . opWriteConcernCounters ,
251
254
newStatus . opWriteConcernCounters ,
252
255
[ opName + ".wtag.myTag" ] ,
@@ -300,4 +303,4 @@ for (const isPSASet of [true, false]) {
300
303
1 ,
301
304
isPSASet ,
302
305
{ insert : collName , documents : [ { _id : 0 } ] } ) ;
303
- }
306
+ }
0 commit comments