|
19 | 19 | import com.mongodb.ConnectionString; |
20 | 20 | import com.mongodb.Function; |
21 | 21 | import com.mongodb.MongoClientSettings; |
| 22 | +import com.mongodb.MongoException; |
22 | 23 | import com.mongodb.MongoServerException; |
23 | 24 | import com.mongodb.MongoWriteConcernException; |
24 | 25 | import com.mongodb.ServerAddress; |
|
34 | 35 | import com.mongodb.internal.connection.TestCommandListener; |
35 | 36 | import com.mongodb.internal.connection.TestConnectionPoolListener; |
36 | 37 | import com.mongodb.internal.event.ConfigureFailPointCommandListener; |
| 38 | +import com.mongodb.lang.Nullable; |
37 | 39 | import org.bson.BsonDocument; |
38 | 40 | import org.bson.BsonInt32; |
39 | 41 | import org.bson.Document; |
| 42 | +import org.junit.jupiter.api.Disabled; |
40 | 43 | import org.junit.jupiter.api.Test; |
41 | 44 |
|
42 | 45 | import java.util.HashSet; |
|
55 | 58 | import static com.mongodb.ClusterFixture.isSharded; |
56 | 59 | import static com.mongodb.ClusterFixture.isStandalone; |
57 | 60 | import static com.mongodb.ClusterFixture.serverVersionAtLeast; |
| 61 | +import static com.mongodb.MongoException.RETRYABLE_ERROR_LABEL; |
| 62 | +import static com.mongodb.MongoException.SYSTEM_OVERLOADED_ERROR_LABEL; |
58 | 63 | import static com.mongodb.client.Fixture.getDefaultDatabaseName; |
59 | 64 | import static com.mongodb.client.Fixture.getMongoClientSettingsBuilder; |
60 | 65 | import static com.mongodb.client.Fixture.getMultiMongosMongoClientSettingsBuilder; |
|
66 | 71 | import static java.util.Collections.singletonList; |
67 | 72 | import static java.util.concurrent.TimeUnit.SECONDS; |
68 | 73 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 74 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
69 | 75 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
70 | 76 | import static org.junit.jupiter.api.Assertions.assertThrows; |
71 | 77 | import static org.junit.jupiter.api.Assertions.assertTrue; |
@@ -333,6 +339,179 @@ public static <R> void retriesOnSameMongosWhenAnotherNotAvailable( |
333 | 339 | } |
334 | 340 | } |
335 | 341 |
|
| 342 | + /** |
| 343 | + * <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-writes/tests/README.md#case-1-test-that-drivers-return-the-correct-error-when-receiving-only-errors-without-nowritesperformed"> |
| 344 | + * 6. Test error propagation after encountering multiple errors. |
| 345 | + * Case 1: Test that drivers return the correct error when receiving only errors without NoWritesPerformed</a>. |
| 346 | + */ |
| 347 | + @Test |
| 348 | + @Disabled("TODO-BACKPRESSURE Valentin Enable when implementing JAVA-6055") |
| 349 | + void errorPropagationAfterEncounteringMultipleErrorsCase1() throws Exception { |
| 350 | + errorPropagationAfterEncounteringMultipleErrorsCase1(MongoClients::create); |
| 351 | + } |
| 352 | + |
| 353 | + public static void errorPropagationAfterEncounteringMultipleErrorsCase1(final Function<MongoClientSettings, MongoClient> clientCreator) |
| 354 | + throws Exception { |
| 355 | + BsonDocument configureFailPoint = BsonDocument.parse( |
| 356 | + "{\n" |
| 357 | + + " configureFailPoint: 'failCommand',\n" |
| 358 | + + " mode: {'times': 1},\n" |
| 359 | + + " data: {\n" |
| 360 | + + " failCommands: ['insert'],\n" |
| 361 | + + " errorLabels: ['" + RETRYABLE_ERROR_LABEL + "', '" + SYSTEM_OVERLOADED_ERROR_LABEL + "'],\n" |
| 362 | + + " errorCode: 91\n" |
| 363 | + + " }\n" |
| 364 | + + "}\n"); |
| 365 | + BsonDocument configureFailPointFromListener = BsonDocument.parse( |
| 366 | + "{\n" |
| 367 | + + " configureFailPoint: \"failCommand\",\n" |
| 368 | + + " mode: 'alwaysOn',\n" |
| 369 | + + " data: {\n" |
| 370 | + + " failCommands: ['insert'],\n" |
| 371 | + + " errorCode: 10107,\n" |
| 372 | + + " errorLabels: ['" + RETRYABLE_ERROR_LABEL + "', '" + SYSTEM_OVERLOADED_ERROR_LABEL + "']\n" |
| 373 | + + " }\n" |
| 374 | + + "}\n"); |
| 375 | + Predicate<CommandEvent> configureFailPointEventMatcher = event -> { |
| 376 | + if (event instanceof CommandFailedEvent) { |
| 377 | + CommandFailedEvent commandFailedEvent = (CommandFailedEvent) event; |
| 378 | + MongoException cause = assertInstanceOf(MongoException.class, commandFailedEvent.getThrowable()); |
| 379 | + return cause.getCode() == 91; |
| 380 | + } |
| 381 | + return false; |
| 382 | + }; |
| 383 | + errorPropagationAfterEncounteringMultipleErrors( |
| 384 | + clientCreator, |
| 385 | + configureFailPoint, |
| 386 | + configureFailPointFromListener, |
| 387 | + configureFailPointEventMatcher, |
| 388 | + 10107, |
| 389 | + null); |
| 390 | + } |
| 391 | + |
| 392 | + /** |
| 393 | + * <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-writes/tests/README.md#case-2-test-that-drivers-return-the-correct-error-when-receiving-only-errors-with-nowritesperformed"> |
| 394 | + * 6. Test error propagation after encountering multiple errors. |
| 395 | + * Case 2: Test that drivers return the correct error when receiving only errors with NoWritesPerformed</a>. |
| 396 | + */ |
| 397 | + @Test |
| 398 | + void errorPropagationAfterEncounteringMultipleErrorsCase2() throws Exception { |
| 399 | + errorPropagationAfterEncounteringMultipleErrorsCase2(MongoClients::create); |
| 400 | + } |
| 401 | + |
| 402 | + public static void errorPropagationAfterEncounteringMultipleErrorsCase2(final Function<MongoClientSettings, MongoClient> clientCreator) |
| 403 | + throws Exception { |
| 404 | + BsonDocument configureFailPoint = BsonDocument.parse( |
| 405 | + "{\n" |
| 406 | + + " configureFailPoint: 'failCommand',\n" |
| 407 | + + " mode: {'times': 1},\n" |
| 408 | + + " data: {\n" |
| 409 | + + " failCommands: ['insert'],\n" |
| 410 | + + " errorLabels: ['" + RETRYABLE_ERROR_LABEL + "', '" + SYSTEM_OVERLOADED_ERROR_LABEL |
| 411 | + + "', '" + NO_WRITES_PERFORMED_ERROR_LABEL + "'],\n" |
| 412 | + + " errorCode: 91\n" |
| 413 | + + " }\n" |
| 414 | + + "}\n"); |
| 415 | + BsonDocument configureFailPointFromListener = BsonDocument.parse( |
| 416 | + "{\n" |
| 417 | + + " configureFailPoint: \"failCommand\",\n" |
| 418 | + + " mode: 'alwaysOn',\n" |
| 419 | + + " data: {\n" |
| 420 | + + " failCommands: ['insert'],\n" |
| 421 | + + " errorCode: 10107,\n" |
| 422 | + + " errorLabels: ['" + RETRYABLE_ERROR_LABEL + "', '" + SYSTEM_OVERLOADED_ERROR_LABEL |
| 423 | + + "', '" + NO_WRITES_PERFORMED_ERROR_LABEL + "'],\n" |
| 424 | + + " }\n" |
| 425 | + + "}\n"); |
| 426 | + Predicate<CommandEvent> configureFailPointEventMatcher = event -> { |
| 427 | + if (event instanceof CommandFailedEvent) { |
| 428 | + CommandFailedEvent commandFailedEvent = (CommandFailedEvent) event; |
| 429 | + MongoException cause = assertInstanceOf(MongoException.class, commandFailedEvent.getThrowable()); |
| 430 | + return cause.getCode() == 91; |
| 431 | + } |
| 432 | + return false; |
| 433 | + }; |
| 434 | + errorPropagationAfterEncounteringMultipleErrors( |
| 435 | + clientCreator, |
| 436 | + configureFailPoint, |
| 437 | + configureFailPointFromListener, |
| 438 | + configureFailPointEventMatcher, |
| 439 | + 91, |
| 440 | + null); |
| 441 | + } |
| 442 | + |
| 443 | + /** |
| 444 | + * <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-writes/tests/README.md#case-3-test-that-drivers-return-the-correct-error-when-receiving-some-errors-with-nowritesperformed-and-some-without-nowritesperformed"> |
| 445 | + * 6. Test error propagation after encountering multiple errors. |
| 446 | + * Case 3: Test that drivers return the correct error when receiving some errors with NoWritesPerformed and some without NoWritesPerformed</a>. |
| 447 | + */ |
| 448 | + @Test |
| 449 | + @Disabled("TODO-BACKPRESSURE Valentin Enable when implementing JAVA-6055, fails on MongoDB 6.0") |
| 450 | + void errorPropagationAfterEncounteringMultipleErrorsCase3() throws Exception { |
| 451 | + errorPropagationAfterEncounteringMultipleErrorsCase3(MongoClients::create); |
| 452 | + } |
| 453 | + |
| 454 | + public static void errorPropagationAfterEncounteringMultipleErrorsCase3(final Function<MongoClientSettings, MongoClient> clientCreator) |
| 455 | + throws Exception { |
| 456 | + BsonDocument configureFailPoint = BsonDocument.parse( |
| 457 | + "{\n" |
| 458 | + + " configureFailPoint: 'failCommand',\n" |
| 459 | + + " mode: {'times': 1},\n" |
| 460 | + + " data: {\n" |
| 461 | + + " failCommands: ['insert'],\n" |
| 462 | + + " errorLabels: ['" + RETRYABLE_ERROR_LABEL + "', '" + SYSTEM_OVERLOADED_ERROR_LABEL + "'],\n" |
| 463 | + + " errorCode: 91\n" |
| 464 | + + " }\n" |
| 465 | + + "}\n"); |
| 466 | + BsonDocument configureFailPointFromListener = BsonDocument.parse( |
| 467 | + "{\n" |
| 468 | + + " configureFailPoint: \"failCommand\",\n" |
| 469 | + + " mode: 'alwaysOn',\n" |
| 470 | + + " data: {\n" |
| 471 | + + " failCommands: ['insert'],\n" |
| 472 | + + " errorCode: 91,\n" |
| 473 | + + " errorLabels: ['" + RETRYABLE_ERROR_LABEL + "', '" + SYSTEM_OVERLOADED_ERROR_LABEL |
| 474 | + + "', '" + NO_WRITES_PERFORMED_ERROR_LABEL + "'],\n" |
| 475 | + + " }\n" |
| 476 | + + "}\n"); |
| 477 | + Predicate<CommandEvent> configureFailPointEventMatcher = event -> event instanceof CommandFailedEvent; |
| 478 | + errorPropagationAfterEncounteringMultipleErrors( |
| 479 | + clientCreator, |
| 480 | + configureFailPoint, |
| 481 | + configureFailPointFromListener, |
| 482 | + configureFailPointEventMatcher, |
| 483 | + 91, |
| 484 | + NO_WRITES_PERFORMED_ERROR_LABEL); |
| 485 | + } |
| 486 | + |
| 487 | + /** |
| 488 | + * @param unexpectedErrorLabel {@code null} means there is no expectation. |
| 489 | + */ |
| 490 | + private static void errorPropagationAfterEncounteringMultipleErrors( |
| 491 | + final Function<MongoClientSettings, MongoClient> clientCreator, |
| 492 | + final BsonDocument configureFailPoint, |
| 493 | + final BsonDocument configureFailPointFromListener, |
| 494 | + final Predicate<CommandEvent> configureFailPointEventMatcher, |
| 495 | + final int expectedErrorCode, |
| 496 | + @Nullable final String unexpectedErrorLabel) throws Exception { |
| 497 | + assumeTrue(serverVersionAtLeast(6, 0)); |
| 498 | + assumeTrue(isDiscoverableReplicaSet()); |
| 499 | + try (ConfigureFailPointCommandListener commandListener = new ConfigureFailPointCommandListener( |
| 500 | + configureFailPointFromListener, getPrimary(), configureFailPointEventMatcher); |
| 501 | + MongoClient client = clientCreator.apply(getMongoClientSettingsBuilder() |
| 502 | + .retryWrites(true) |
| 503 | + .addCommandListener(commandListener) |
| 504 | + .build()); |
| 505 | + FailPoint ignored = FailPoint.enable(configureFailPoint, getPrimary())) { |
| 506 | + MongoCollection<Document> collection = dropAndGetCollection("errorPropagationAfterEncounteringMultipleErrors", client); |
| 507 | + MongoException e = assertThrows(MongoException.class, () -> collection.insertOne(new Document())); |
| 508 | + assertEquals(expectedErrorCode, e.getCode()); |
| 509 | + if (unexpectedErrorLabel != null) { |
| 510 | + assertFalse(e.hasErrorLabel(unexpectedErrorLabel)); |
| 511 | + } |
| 512 | + } |
| 513 | + } |
| 514 | + |
336 | 515 | private static MongoCollection<Document> dropAndGetCollection(final String name, final MongoClient client) { |
337 | 516 | MongoCollection<Document> result = client.getDatabase(getDefaultDatabaseName()).getCollection(name); |
338 | 517 | result.drop(); |
|
0 commit comments