| 1 |
import itertools |
| 2 |
import sys |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
if sys.version_info[:2] < (3, 7): |
| 9 |
print('ERROR: This script requires Python >= 3.7, not:') |
| 10 |
print(sys.version) |
| 11 |
print('Usage: python3.7 mongos-pin-auto-tests.py > mongos-pin-auto.yml') |
| 12 |
exit(1) |
| 13 |
|
| 14 |
HEADER = '''# Autogenerated tests that transient errors in a transaction unpin the session. |
| 15 |
# See mongos-pin-auto-tests.py |
| 16 |
runOn: |
| 17 |
- |
| 18 |
minServerVersion: "4.1.8" |
| 19 |
topology: ["sharded"] |
| 20 |
# serverless proxy doesn't append error labels to errors in transactions |
| 21 |
# caused by failpoints (CLOUDP-88216) |
| 22 |
serverless: "forbid" |
| 23 |
|
| 24 |
database_name: &database_name "transaction-tests" |
| 25 |
collection_name: &collection_name "test" |
| 26 |
|
| 27 |
data: &data |
| 28 |
- {_id: 1} |
| 29 |
- {_id: 2} |
| 30 |
|
| 31 |
tests: |
| 32 |
- description: remain pinned after non-transient Interrupted error on insertOne |
| 33 |
useMultipleMongoses: true |
| 34 |
operations: |
| 35 |
- &startTransaction |
| 36 |
name: startTransaction |
| 37 |
object: session0 |
| 38 |
- &initialCommand |
| 39 |
name: insertOne |
| 40 |
object: collection |
| 41 |
arguments: |
| 42 |
session: session0 |
| 43 |
document: {_id: 3} |
| 44 |
result: |
| 45 |
insertedId: 3 |
| 46 |
- name: targetedFailPoint |
| 47 |
object: testRunner |
| 48 |
arguments: |
| 49 |
session: session0 |
| 50 |
failPoint: |
| 51 |
configureFailPoint: failCommand |
| 52 |
mode: {times: 1} |
| 53 |
data: |
| 54 |
failCommands: ["insert"] |
| 55 |
errorCode: 11601 |
| 56 |
- name: insertOne |
| 57 |
object: collection |
| 58 |
arguments: |
| 59 |
session: session0 |
| 60 |
document: |
| 61 |
_id: 4 |
| 62 |
result: |
| 63 |
errorLabelsOmit: ["TransientTransactionError", "UnknownTransactionCommitResult"] |
| 64 |
errorCodeName: Interrupted |
| 65 |
- &assertSessionPinned |
| 66 |
name: assertSessionPinned |
| 67 |
object: testRunner |
| 68 |
arguments: |
| 69 |
session: session0 |
| 70 |
- &commitTransaction |
| 71 |
name: commitTransaction |
| 72 |
object: session0 |
| 73 |
|
| 74 |
expectations: |
| 75 |
- command_started_event: |
| 76 |
command: |
| 77 |
insert: *collection_name |
| 78 |
documents: |
| 79 |
- _id: 3 |
| 80 |
ordered: true |
| 81 |
readConcern: |
| 82 |
lsid: session0 |
| 83 |
txnNumber: |
| 84 |
$numberLong: "1" |
| 85 |
startTransaction: true |
| 86 |
autocommit: false |
| 87 |
writeConcern: |
| 88 |
command_name: insert |
| 89 |
database_name: *database_name |
| 90 |
- command_started_event: |
| 91 |
command: |
| 92 |
insert: *collection_name |
| 93 |
documents: |
| 94 |
- _id: 4 |
| 95 |
ordered: true |
| 96 |
readConcern: |
| 97 |
lsid: session0 |
| 98 |
txnNumber: |
| 99 |
$numberLong: "1" |
| 100 |
startTransaction: |
| 101 |
autocommit: false |
| 102 |
writeConcern: |
| 103 |
command_name: insert |
| 104 |
database_name: *database_name |
| 105 |
- command_started_event: |
| 106 |
command: |
| 107 |
commitTransaction: 1 |
| 108 |
lsid: session0 |
| 109 |
txnNumber: |
| 110 |
$numberLong: "1" |
| 111 |
startTransaction: |
| 112 |
autocommit: false |
| 113 |
writeConcern: |
| 114 |
recoveryToken: 42 |
| 115 |
command_name: commitTransaction |
| 116 |
database_name: admin |
| 117 |
|
| 118 |
outcome: &outcome |
| 119 |
collection: |
| 120 |
data: |
| 121 |
- {_id: 1} |
| 122 |
- {_id: 2} |
| 123 |
- {_id: 3} |
| 124 |
|
| 125 |
- description: unpin after transient error within a transaction |
| 126 |
useMultipleMongoses: true |
| 127 |
operations: |
| 128 |
- &startTransaction |
| 129 |
name: startTransaction |
| 130 |
object: session0 |
| 131 |
- &initialCommand |
| 132 |
name: insertOne |
| 133 |
object: collection |
| 134 |
arguments: |
| 135 |
session: session0 |
| 136 |
document: |
| 137 |
_id: 3 |
| 138 |
result: |
| 139 |
insertedId: 3 |
| 140 |
- name: targetedFailPoint |
| 141 |
object: testRunner |
| 142 |
arguments: |
| 143 |
session: session0 |
| 144 |
failPoint: |
| 145 |
configureFailPoint: failCommand |
| 146 |
mode: { times: 1 } |
| 147 |
data: |
| 148 |
failCommands: ["insert"] |
| 149 |
closeConnection: true |
| 150 |
- name: insertOne |
| 151 |
object: collection |
| 152 |
arguments: |
| 153 |
session: session0 |
| 154 |
document: |
| 155 |
_id: 4 |
| 156 |
result: |
| 157 |
errorLabelsContain: ["TransientTransactionError"] |
| 158 |
errorLabelsOmit: ["UnknownTransactionCommitResult"] |
| 159 |
# Session unpins from the first mongos after the insert error and |
| 160 |
# abortTransaction succeeds immediately on any mongos. |
| 161 |
- &assertSessionUnpinned |
| 162 |
name: assertSessionUnpinned |
| 163 |
object: testRunner |
| 164 |
arguments: |
| 165 |
session: session0 |
| 166 |
- &abortTransaction |
| 167 |
name: abortTransaction |
| 168 |
object: session0 |
| 169 |
|
| 170 |
expectations: |
| 171 |
- command_started_event: |
| 172 |
command: |
| 173 |
insert: *collection_name |
| 174 |
documents: |
| 175 |
- _id: 3 |
| 176 |
ordered: true |
| 177 |
readConcern: |
| 178 |
lsid: session0 |
| 179 |
txnNumber: |
| 180 |
$numberLong: "1" |
| 181 |
startTransaction: true |
| 182 |
autocommit: false |
| 183 |
writeConcern: |
| 184 |
command_name: insert |
| 185 |
database_name: *database_name |
| 186 |
- command_started_event: |
| 187 |
command: |
| 188 |
insert: *collection_name |
| 189 |
documents: |
| 190 |
- _id: 4 |
| 191 |
ordered: true |
| 192 |
readConcern: |
| 193 |
lsid: session0 |
| 194 |
txnNumber: |
| 195 |
$numberLong: "1" |
| 196 |
startTransaction: |
| 197 |
autocommit: false |
| 198 |
writeConcern: |
| 199 |
command_name: insert |
| 200 |
database_name: *database_name |
| 201 |
- command_started_event: |
| 202 |
command: |
| 203 |
abortTransaction: 1 |
| 204 |
lsid: session0 |
| 205 |
txnNumber: |
| 206 |
$numberLong: "1" |
| 207 |
startTransaction: |
| 208 |
autocommit: false |
| 209 |
writeConcern: |
| 210 |
recoveryToken: 42 |
| 211 |
command_name: abortTransaction |
| 212 |
database_name: admin |
| 213 |
|
| 214 |
outcome: &outcome |
| 215 |
collection: |
| 216 |
data: *data |
| 217 |
|
| 218 |
# The rest of the tests in this file test every operation type against |
| 219 |
# multiple types of transient errors (connection and error code).''' |
| 220 |
|
| 221 |
TEMPLATE = ''' |
| 222 |
- description: {test_name} {error_name} error on {op_name} {command_name} |
| 223 |
useMultipleMongoses: true |
| 224 |
operations: |
| 225 |
- *startTransaction |
| 226 |
- *initialCommand |
| 227 |
- name: targetedFailPoint |
| 228 |
object: testRunner |
| 229 |
arguments: |
| 230 |
session: session0 |
| 231 |
failPoint: |
| 232 |
configureFailPoint: failCommand |
| 233 |
mode: {{times: 1}} |
| 234 |
data: |
| 235 |
failCommands: ["{command_name}"] |
| 236 |
{error_data} |
| 237 |
- name: {op_name} |
| 238 |
object: {object_name} |
| 239 |
arguments: |
| 240 |
session: session0 |
| 241 |
{op_args} |
| 242 |
result: |
| 243 |
{error_labels}: ["TransientTransactionError"] |
| 244 |
- *{assertion} |
| 245 |
- *abortTransaction |
| 246 |
outcome: *outcome |
| 247 |
''' |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
OPS = { |
| 252 |
|
| 253 |
'insertOne': ('insert', 'collection', r'document: {_id: 4}'), |
| 254 |
'insertMany': ('insert', 'collection', r'documents: [{_id: 4}, {_id: 5}]'), |
| 255 |
'updateOne': ('update', 'collection', r'''filter: {_id: 1} |
| 256 |
update: {$inc: {x: 1}}'''), |
| 257 |
'replaceOne': ('update', 'collection', r'''filter: {_id: 1} |
| 258 |
replacement: {y: 1}'''), |
| 259 |
'updateMany': ('update', 'collection', r'''filter: {_id: {$gte: 1}} |
| 260 |
update: {$set: {z: 1}}'''), |
| 261 |
'deleteOne': ('delete', 'collection', r'filter: {_id: 1}'), |
| 262 |
'deleteMany': ('delete', 'collection', r'filter: {_id: {$gte: 1}}'), |
| 263 |
'findOneAndDelete': ('findAndModify', 'collection', r'filter: {_id: 1}'), |
| 264 |
'findOneAndUpdate': ('findAndModify', 'collection', r'''filter: {_id: 1} |
| 265 |
update: {$inc: {x: 1}} |
| 266 |
returnDocument: Before'''), |
| 267 |
'findOneAndReplace': ('findAndModify', 'collection', r'''filter: {_id: 1} |
| 268 |
replacement: {y: 1} |
| 269 |
returnDocument: Before'''), |
| 270 |
|
| 271 |
'bulkWrite insert': ('insert', 'collection', r'''requests: |
| 272 |
- name: insertOne |
| 273 |
arguments: |
| 274 |
document: {_id: 1}'''), |
| 275 |
'bulkWrite update': ('update', 'collection', r'''requests: |
| 276 |
- name: updateOne |
| 277 |
arguments: |
| 278 |
filter: {_id: 1} |
| 279 |
update: {$set: {x: 1}}'''), |
| 280 |
'bulkWrite delete': ('delete', 'collection', r'''requests: |
| 281 |
- name: deleteOne |
| 282 |
arguments: |
| 283 |
filter: {_id: 1}'''), |
| 284 |
|
| 285 |
'find': ('find', 'collection', r'filter: {_id: 1}'), |
| 286 |
'countDocuments': ('aggregate', 'collection', r'filter: {}'), |
| 287 |
'aggregate': ('aggregate', 'collection', r'pipeline: []'), |
| 288 |
'distinct': ('distinct', 'collection', r'fieldName: _id'), |
| 289 |
|
| 290 |
'runCommand': ( |
| 291 |
'insert', |
| 292 |
r'''database |
| 293 |
command_name: insert''', |
| 294 |
r'''command: |
| 295 |
insert: *collection_name |
| 296 |
documents: |
| 297 |
- _id : 1'''), |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
NON_TRANSIENT_ERRORS = { |
| 302 |
'Interrupted': 'errorCode: 11601', |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
TRANSIENT_ERRORS = { |
| 307 |
'connection': 'closeConnection: true', |
| 308 |
'ShutdownInProgress': 'errorCode: 91', |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
def create_pin_test(op_name, error_name): |
| 313 |
test_name = 'remain pinned after non-transient' |
| 314 |
assertion = 'assertSessionPinned' |
| 315 |
error_labels = 'errorLabelsOmit' |
| 316 |
command_name, object_name, op_args = OPS[op_name] |
| 317 |
error_data = NON_TRANSIENT_ERRORS[error_name] |
| 318 |
if op_name.startswith('bulkWrite'): |
| 319 |
op_name = 'bulkWrite' |
| 320 |
return TEMPLATE.format(**locals()) |
| 321 |
|
| 322 |
|
| 323 |
def create_unpin_test(op_name, error_name): |
| 324 |
test_name = 'unpin after transient' |
| 325 |
assertion = 'assertSessionUnpinned' |
| 326 |
error_labels = 'errorLabelsContain' |
| 327 |
command_name, object_name, op_args = OPS[op_name] |
| 328 |
error_data = TRANSIENT_ERRORS[error_name] |
| 329 |
if op_name.startswith('bulkWrite'): |
| 330 |
op_name = 'bulkWrite' |
| 331 |
return TEMPLATE.format(**locals()) |
| 332 |
|
| 333 |
tests = [] |
| 334 |
for op_name, error_name in itertools.product(OPS, NON_TRANSIENT_ERRORS): |
| 335 |
tests.append(create_pin_test(op_name, error_name)) |
| 336 |
for op_name, error_name in itertools.product(OPS, TRANSIENT_ERRORS): |
| 337 |
tests.append(create_unpin_test(op_name, error_name)) |
| 338 |
|
| 339 |
print(HEADER) |
| 340 |
print(''.join(tests)) |