Skip to content

Commit 4858777

Browse files
committed
CDRIVER-3293 unpin on transient txn error
1 parent f3a3185 commit 4858777

File tree

5 files changed

+44
-20
lines changed

5 files changed

+44
-20
lines changed

src/libmongoc/src/mongoc/mongoc-client-session.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,15 @@ _mongoc_client_session_handle_reply (mongoc_client_session_t *session,
637637
return;
638638
}
639639

640+
if (mongoc_error_has_label (reply, "TransientTransactionError")) {
641+
/* Transaction Spec: "Drivers MUST unpin a ClientSession when a command
642+
* within a transaction, including commitTransaction and abortTransaction,
643+
* fails with a TransientTransactionError". If the server reply included
644+
* a TransientTransactionError, we unpin here. If a network error caused
645+
* us to add a label client-side, we unpin in network_error_reply. */
646+
session->server_id = 0;
647+
}
648+
640649
while (bson_iter_next (&iter)) {
641650
if (!strcmp (bson_iter_key (&iter), "$clusterTime") &&
642651
BSON_ITER_HOLDS_DOCUMENT (&iter)) {

src/libmongoc/src/mongoc/mongoc-cluster.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,19 +2779,27 @@ network_error_reply (bson_t *reply, mongoc_cmd_t *cmd)
27792779
{
27802780
bson_t labels;
27812781

2782-
if (!reply) {
2783-
return;
2782+
if (reply) {
2783+
bson_init (reply);
27842784
}
2785-
2786-
bson_init (reply);
2787-
27882785
/* Transactions Spec defines TransientTransactionError: "Any
27892786
* network error or server selection error encountered running any
27902787
* command besides commitTransaction in a transaction. In the case
27912788
* of command errors, the server adds the label; in the case of
27922789
* network errors or server selection errors where the client
27932790
* receives no server reply, the client adds the label." */
27942791
if (_mongoc_client_session_in_txn (cmd->session) && !cmd->is_txn_finish) {
2792+
/* Transaction Spec: "Drivers MUST unpin a ClientSession when a command
2793+
* within a transaction, including commitTransaction and abortTransaction,
2794+
* fails with a TransientTransactionError". If we're about to add
2795+
* a TransientTransactionError label due to a client side error then we
2796+
* unpin. If commitTransaction/abortTransation includes a label in the
2797+
* server reply, we unpin in _mongoc_client_session_handle_reply. */
2798+
cmd->session->server_id = 0;
2799+
if (!reply) {
2800+
return;
2801+
}
2802+
27952803
BSON_APPEND_ARRAY_BEGIN (reply, "errorLabels", &labels);
27962804
BSON_APPEND_UTF8 (&labels, "0", TRANSIENT_TXN_ERR);
27972805
bson_append_array_end (reply, &labels);

src/libmongoc/tests/json/transactions/mongos-recovery-token.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@
268268
},
269269
{
270270
"description": "commitTransaction retry fails on new mongos",
271-
"skipReason": "currently failing",
272271
"useMultipleMongoses": true,
273272
"clientOptions": {
274273
"heartbeatFrequencyMS": 30000

src/libmongoc/tests/json/transactions/pin-mongos.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@
827827
},
828828
{
829829
"description": "remain pinned after non-transient error on commit",
830-
"skipReason": "currently failing",
830+
"skipReason": "blocked on SPEC-1320",
831831
"useMultipleMongoses": true,
832832
"operations": [
833833
{
@@ -1072,7 +1072,6 @@
10721072
},
10731073
{
10741074
"description": "unpin after transient error within a transaction and commit",
1075-
"skipReason": "currently failing",
10761075
"useMultipleMongoses": true,
10771076
"clientOptions": {
10781077
"heartbeatFrequencyMS": 30000

src/libmongoc/tests/test-mongoc-transactions.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,29 @@ _disable_failpoints (json_test_ctx_t *ctx, const char *host_str)
6868
ctx->test_framework_uri, host_str);
6969

7070
/* Some transactions tests have a failCommand for "isMaster" repeat seven
71-
* times.
72-
* Repeat this seven times. */
71+
* times. Repeat this seven times. And set a reduced server selection timeout
72+
* so we don't hang on failed ismaster commands. */
73+
mongoc_uri_set_option_as_int32 (
74+
uri, MONGOC_URI_SERVERSELECTIONTIMEOUTMS, 500);
75+
7376
for (i = 0; i < 7; i++) {
74-
client = mongoc_client_new_from_uri (uri);
77+
bool ret;
7578

76-
ASSERT_OR_PRINT (
77-
mongoc_client_command_simple (
78-
client,
79-
"admin",
80-
tmp_bson ("{'configureFailPoint': 'failCommand', 'mode': 'off'}"),
81-
NULL,
82-
NULL,
83-
&error),
84-
error);
79+
client = mongoc_client_new_from_uri (uri);
80+
ret = mongoc_client_command_simple (
81+
client,
82+
"admin",
83+
tmp_bson ("{'configureFailPoint': 'failCommand', 'mode': 'off'}"),
84+
NULL,
85+
NULL,
86+
&error);
87+
if (!ret) {
88+
/* Tests that fail with isMaster also fail to disable the failpoint
89+
* (since we run isMaster when opening the connection). Ignore those
90+
* errors. */
91+
BSON_ASSERT (NULL !=
92+
strstr (error.message, "No suitable servers found"));
93+
}
8594
mongoc_client_destroy (client);
8695
}
8796
mongoc_uri_destroy (uri);

0 commit comments

Comments
 (0)