Skip to content

Commit 258fbe3

Browse files
committed
CDRIVER-3366 reset max_commit_time_ms in txn_opts_cleanup
1 parent 36c499b commit 258fbe3

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

src/libmongoc/src/mongoc/mongoc-client-session-private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define TRANSIENT_TXN_ERR "TransientTransactionError"
2727
#define UNKNOWN_COMMIT_RESULT "UnknownTransactionCommitResult"
2828
#define MAX_TIME_MS_EXPIRED "MaxTimeMSExpired"
29+
#define DEFAULT_MAX_COMMIT_TIME_MS 0
2930

3031
#define MONGOC_DEFAULT_WTIMEOUT_FOR_COMMIT_RETRY 10000
3132

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#define SESSION_NEVER_USED (-1)
2727

2828
#define WITH_TXN_TIMEOUT_MS (120 * 1000)
29-
#define DEFAULT_MAX_COMMIT_TIME_MS 0
3029

3130
static void
3231
txn_opts_set (mongoc_transaction_opt_t *opts,
@@ -64,6 +63,7 @@ txn_opts_cleanup (mongoc_transaction_opt_t *opts)
6463
opts->read_concern = NULL;
6564
opts->write_concern = NULL;
6665
opts->read_prefs = NULL;
66+
opts->max_commit_time_ms = DEFAULT_MAX_COMMIT_TIME_MS;
6767
}
6868

6969

@@ -1246,6 +1246,7 @@ mongoc_client_session_abort_transaction (mongoc_client_session_t *session,
12461246
case MONGOC_TRANSACTION_STARTING:
12471247
/* we sent no commands, not actually started on server */
12481248
session->txn.state = MONGOC_TRANSACTION_ABORTED;
1249+
txn_opts_cleanup (&session->txn.opts);
12491250
RETURN (true);
12501251
case MONGOC_TRANSACTION_IN_PROGRESS:
12511252
session->txn.state = MONGOC_TRANSACTION_ENDING;

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,62 @@ test_selected_server_is_pinned_to_mongos (void *ctx)
968968
mongoc_uri_destroy (uri);
969969
}
970970

971+
static void
972+
test_max_commit_time_ms_is_reset (void *ctx)
973+
{
974+
mock_rs_t *rs;
975+
mongoc_uri_t *uri = NULL;
976+
mongoc_client_t *client = NULL;
977+
mongoc_transaction_opt_t *txn_opts = NULL;
978+
mongoc_session_opt_t *session_opts = NULL;
979+
mongoc_client_session_t *session = NULL;
980+
bson_error_t error;
981+
bool r;
982+
983+
rs = mock_rs_with_autoismaster (WIRE_VERSION_4_2,
984+
true /* has primary */,
985+
2 /* secondaries */,
986+
0 /* arbiters */);
987+
988+
mock_rs_run (rs);
989+
uri = mongoc_uri_copy (mock_rs_get_uri (rs));
990+
991+
client = mongoc_client_new_from_uri (uri);
992+
BSON_ASSERT (client);
993+
994+
txn_opts = mongoc_transaction_opts_new ();
995+
session_opts = mongoc_session_opts_new ();
996+
997+
session = mongoc_client_start_session (client, session_opts, &error);
998+
ASSERT_OR_PRINT (session, error);
999+
1000+
mongoc_transaction_opts_set_max_commit_time_ms (txn_opts, 1);
1001+
1002+
r = mongoc_client_session_start_transaction (session, txn_opts, &error);
1003+
ASSERT_OR_PRINT (r, error);
1004+
BSON_ASSERT (1 == session->txn.opts.max_commit_time_ms);
1005+
1006+
r = mongoc_client_session_abort_transaction (session, &error);
1007+
ASSERT_OR_PRINT (r, error);
1008+
BSON_ASSERT (DEFAULT_MAX_COMMIT_TIME_MS == session->txn.opts.max_commit_time_ms);
1009+
1010+
mongoc_transaction_opts_set_max_commit_time_ms (txn_opts, DEFAULT_MAX_COMMIT_TIME_MS);
1011+
1012+
r = mongoc_client_session_start_transaction (session, txn_opts, &error);
1013+
ASSERT_OR_PRINT (r, error);
1014+
BSON_ASSERT (DEFAULT_MAX_COMMIT_TIME_MS == session->txn.opts.max_commit_time_ms);
1015+
1016+
r = mongoc_client_session_abort_transaction (session, &error);
1017+
ASSERT_OR_PRINT (r, error);
1018+
1019+
mongoc_session_opts_destroy (session_opts);
1020+
mongoc_transaction_opts_destroy (txn_opts);
1021+
mongoc_client_session_destroy (session);
1022+
mongoc_client_destroy (client);
1023+
mongoc_uri_destroy (uri);
1024+
mock_rs_destroy (rs);
1025+
}
1026+
9711027
void
9721028
test_transactions_install (TestSuite *suite)
9731029
{
@@ -1043,4 +1099,10 @@ test_transactions_install (TestSuite *suite)
10431099
test_framework_skip_if_no_sessions,
10441100
test_framework_skip_if_max_wire_version_less_than_8,
10451101
test_framework_skip_if_not_mongos);
1102+
TestSuite_AddFull (suite,
1103+
"/transactions/max_commit_time_ms_is_reset",
1104+
test_max_commit_time_ms_is_reset,
1105+
NULL,
1106+
NULL,
1107+
NULL);
10461108
}

0 commit comments

Comments
 (0)