Skip to content

CDRIVER-5819 always assign operation_id when bulk client changes #1803

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 1 commit into from
Dec 4, 2024
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
14 changes: 7 additions & 7 deletions src/libmongoc/src/mongoc/mongoc-bulk-operation.c
Original file line number Diff line number Diff line change
Expand Up @@ -885,14 +885,14 @@ mongoc_bulk_operation_set_client (mongoc_bulk_operation_t *bulk, void *client)
BSON_ASSERT (bulk->session->client == client);
}

bulk->client = (mongoc_client_t *) client;

/* if you call set_client, bulk was likely made by mongoc_bulk_operation_new,
* not mongoc_collection_create_bulk_operation_with_opts(), so operation_id
* is 0. */
if (!bulk->operation_id) {
bulk->operation_id = ++bulk->client->cluster.operation_id;
/* NOP if the client is not changing; otherwise, assign it and increment and
* fetch its operation_id. */
if ((void *) bulk->client == client) {
return;
}

bulk->client = (mongoc_client_t *) client;
bulk->operation_id = ++bulk->client->cluster.operation_id;
}


Expand Down
43 changes: 43 additions & 0 deletions src/libmongoc/tests/test-mongoc-bulk.c
Original file line number Diff line number Diff line change
Expand Up @@ -4726,6 +4726,46 @@ test_bulk_write_set_client_after_operation (void)
mongoc_client_destroy (client);
}

// regression test for CDRIVER-5819
static void
test_bulk_write_set_client_updates_operation_id_when_client_changes (void)
{
mock_server_t *mock_server;
mongoc_client_t *client, *client2;
mongoc_bulk_operation_t *bulk;
int64_t last_operation_id;

mock_server = mock_server_with_auto_hello (WIRE_VERSION_MIN);
mock_server_run (mock_server);

client = test_framework_client_new_from_uri (mock_server_get_uri (mock_server), NULL);
BSON_ASSERT (client);
bulk = mongoc_bulk_operation_new (true /* ordered */);

// operation_id is fetched from the client
mongoc_bulk_operation_set_client (bulk, client);
ASSERT_CMPINT64 (bulk->operation_id, ==, client->cluster.operation_id);
last_operation_id = bulk->operation_id;

// operation_id is not changed when the client remains the same
mongoc_bulk_operation_set_client (bulk, client);
ASSERT_CMPINT64 (bulk->operation_id, ==, last_operation_id);
ASSERT_CMPINT64 (bulk->operation_id, ==, client->cluster.operation_id);

// operation_id is updated when the client changes
client2 = test_framework_client_new_from_uri (mock_server_get_uri (mock_server), NULL);
BSON_ASSERT (client2);

mongoc_bulk_operation_set_client (bulk, client2);
ASSERT_CMPINT64 (bulk->operation_id, !=, last_operation_id);
ASSERT_CMPINT64 (bulk->operation_id, ==, client2->cluster.operation_id);

mongoc_bulk_operation_destroy (bulk);
mongoc_client_destroy (client);
mongoc_client_destroy (client2);
mock_server_destroy (mock_server);
}

void
test_bulk_install (TestSuite *suite)
{
Expand Down Expand Up @@ -4901,4 +4941,7 @@ test_bulk_install (TestSuite *suite)
/* Require server 4.2 for failCommand appName */
test_framework_skip_if_max_wire_version_less_than_8);
TestSuite_AddLive (suite, "/BulkOperation/set_client_after_operation", test_bulk_write_set_client_after_operation);
TestSuite_AddMockServerTest (suite,
"/BulkOperation/set_client_updates_operation_id_when_client_changes",
test_bulk_write_set_client_updates_operation_id_when_client_changes);
}