Skip to content

CDRIVER-4718 store recoveryToken for Load Balanced topology #1417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/libmongoc/src/mongoc/mongoc-cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,14 +493,32 @@ mongoc_cluster_run_command_opquery (mongoc_cluster_t *cluster,
RETURN (ret);
}

bool
static bool
_in_sharded_txn (const mongoc_client_session_t *session)
{
return session && _mongoc_client_session_in_txn_or_ending (session) &&
_mongoc_topology_get_type (session->client->topology) ==
MONGOC_TOPOLOGY_SHARDED;
}

static bool
_in_sharded_or_loadbalanced_txn (const mongoc_client_session_t *session)
{
if (!session) {
return false;
}

if (!_mongoc_client_session_in_txn_or_ending (session)) {
return false;
}

mongoc_topology_description_type_t type =
_mongoc_topology_get_type (session->client->topology);

return (type == MONGOC_TOPOLOGY_SHARDED) ||
(type == MONGOC_TOPOLOGY_LOAD_BALANCED);
}

static void
_handle_txn_error_labels (bool cmd_ret,
const bson_error_t *cmd_err,
Expand Down Expand Up @@ -655,7 +673,7 @@ mongoc_cluster_run_command_monitored (mongoc_cluster_t *cluster,

_handle_txn_error_labels (retval, error, cmd, reply);

if (retval && _in_sharded_txn (cmd->session) &&
if (retval && _in_sharded_or_loadbalanced_txn (cmd->session) &&
bson_iter_init_find (&iter, reply, "recoveryToken")) {
bson_destroy (cmd->session->recovery_token);
if (BSON_ITER_HOLDS_DOCUMENT (&iter)) {
Expand Down Expand Up @@ -894,7 +912,7 @@ _stream_run_hello (mongoc_cluster_t *cluster,
if (negotiate_sasl_supported_mechs) {
bsonParse (reply,
find (allOf (key ("ok"), isFalse), //
do({
do ({
/* hello response returned ok: 0. According to
* auth spec: "If the hello of the MongoDB
* Handshake fails with an error, drivers MUST
Expand Down
99 changes: 99 additions & 0 deletions src/libmongoc/tests/test-mongoc-loadbalanced.c
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,98 @@ skip_if_not_loadbalanced (void)
return test_framework_is_loadbalanced () ? 1 : 0;
}

static void
store_last_command_started_callback (const mongoc_apm_command_started_t *event)
{
bson_t **last_command = mongoc_apm_command_started_get_context (event);
const bson_t *cmd = mongoc_apm_command_started_get_command (event);
bson_destroy (*last_command);
*last_command = bson_copy (cmd);
}

// `test_loadbalanced_sends_recoveryToken` is a regression test for
// CDRIVER-4718. Ensure that a `recoveryToken` is included in the outgoing
// `commitTransaction` and `abortTransaction` commands when connected to a load
// balanced cluster.
static void
test_loadbalanced_sends_recoveryToken (void *unused)
{
mongoc_client_t *client;
bson_error_t error;
bson_t *last_command = NULL;

BSON_UNUSED (unused);

client = test_framework_new_default_client ();
// Set a callback to store the most recent command started.
{
mongoc_apm_callbacks_t *cbs = mongoc_apm_callbacks_new ();
mongoc_apm_set_command_started_cb (cbs,
store_last_command_started_callback);
mongoc_client_set_apm_callbacks (client, cbs, &last_command);
mongoc_apm_callbacks_destroy (cbs);
}

mongoc_client_session_t *session =
mongoc_client_start_session (client, NULL /* opts */, &error);
ASSERT_OR_PRINT (session, error);

mongoc_collection_t *coll =
mongoc_client_get_collection (client, "db", "coll");

// Commit a transaction. Expect `commitTransaction` to include
// `recoveryToken`.
{
bool ok = mongoc_client_session_start_transaction (
session, NULL /* opts */, &error);
ASSERT_OR_PRINT (ok, error);

bson_t *insert_opts = tmp_bson ("{}");
ok = mongoc_client_session_append (session, insert_opts, &error);
ASSERT_OR_PRINT (ok, error);

ok = mongoc_collection_insert_one (
coll, tmp_bson ("{}"), insert_opts, NULL, &error);
ASSERT_OR_PRINT (ok, error);

ok = mongoc_client_session_commit_transaction (
session, NULL /* reply */, &error);
ASSERT_OR_PRINT (ok, error);

ASSERT_MATCH (
last_command,
"{'commitTransaction': 1, 'recoveryToken': { '$exists': true } }");
}

// Abort a transaction. Expect `abortTransaction` to include
// `recoveryToken`.
{
bool ok = mongoc_client_session_start_transaction (
session, NULL /* opts */, &error);
ASSERT_OR_PRINT (ok, error);

bson_t *insert_opts = tmp_bson ("{}");
ok = mongoc_client_session_append (session, insert_opts, &error);
ASSERT_OR_PRINT (ok, error);

ok = mongoc_collection_insert_one (
coll, tmp_bson ("{}"), insert_opts, NULL, &error);
ASSERT_OR_PRINT (ok, error);

ok = mongoc_client_session_abort_transaction (session, &error);
ASSERT_OR_PRINT (ok, error);

ASSERT_MATCH (
last_command,
"{'abortTransaction': 1, 'recoveryToken': { '$exists': true } }");
}

mongoc_collection_destroy (coll);
mongoc_client_session_destroy (session);
mongoc_client_destroy (client);
bson_destroy (last_command);
}

void
test_loadbalanced_install (TestSuite *suite)
{
Expand Down Expand Up @@ -893,4 +985,11 @@ test_loadbalanced_install (TestSuite *suite)
suite,
"/loadbalanced/post_handshake_error_clears_pool",
test_post_handshake_error_clears_pool);

TestSuite_AddFull (suite,
"/loadbalanced/sends_recoveryToken",
test_loadbalanced_sends_recoveryToken,
NULL /* ctx */,
NULL /* dtor */,
skip_if_not_loadbalanced);
}