Skip to content

Commit 6017794

Browse files
authored
CDRIVER-5819 always assign operation_id when bulk client changes (#1803)
1 parent c3603b7 commit 6017794

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/libmongoc/src/mongoc/mongoc-bulk-operation.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -885,14 +885,14 @@ mongoc_bulk_operation_set_client (mongoc_bulk_operation_t *bulk, void *client)
885885
BSON_ASSERT (bulk->session->client == client);
886886
}
887887

888-
bulk->client = (mongoc_client_t *) client;
889-
890-
/* if you call set_client, bulk was likely made by mongoc_bulk_operation_new,
891-
* not mongoc_collection_create_bulk_operation_with_opts(), so operation_id
892-
* is 0. */
893-
if (!bulk->operation_id) {
894-
bulk->operation_id = ++bulk->client->cluster.operation_id;
888+
/* NOP if the client is not changing; otherwise, assign it and increment and
889+
* fetch its operation_id. */
890+
if ((void *) bulk->client == client) {
891+
return;
895892
}
893+
894+
bulk->client = (mongoc_client_t *) client;
895+
bulk->operation_id = ++bulk->client->cluster.operation_id;
896896
}
897897

898898

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4726,6 +4726,46 @@ test_bulk_write_set_client_after_operation (void)
47264726
mongoc_client_destroy (client);
47274727
}
47284728

4729+
// regression test for CDRIVER-5819
4730+
static void
4731+
test_bulk_write_set_client_updates_operation_id_when_client_changes (void)
4732+
{
4733+
mock_server_t *mock_server;
4734+
mongoc_client_t *client, *client2;
4735+
mongoc_bulk_operation_t *bulk;
4736+
int64_t last_operation_id;
4737+
4738+
mock_server = mock_server_with_auto_hello (WIRE_VERSION_MIN);
4739+
mock_server_run (mock_server);
4740+
4741+
client = test_framework_client_new_from_uri (mock_server_get_uri (mock_server), NULL);
4742+
BSON_ASSERT (client);
4743+
bulk = mongoc_bulk_operation_new (true /* ordered */);
4744+
4745+
// operation_id is fetched from the client
4746+
mongoc_bulk_operation_set_client (bulk, client);
4747+
ASSERT_CMPINT64 (bulk->operation_id, ==, client->cluster.operation_id);
4748+
last_operation_id = bulk->operation_id;
4749+
4750+
// operation_id is not changed when the client remains the same
4751+
mongoc_bulk_operation_set_client (bulk, client);
4752+
ASSERT_CMPINT64 (bulk->operation_id, ==, last_operation_id);
4753+
ASSERT_CMPINT64 (bulk->operation_id, ==, client->cluster.operation_id);
4754+
4755+
// operation_id is updated when the client changes
4756+
client2 = test_framework_client_new_from_uri (mock_server_get_uri (mock_server), NULL);
4757+
BSON_ASSERT (client2);
4758+
4759+
mongoc_bulk_operation_set_client (bulk, client2);
4760+
ASSERT_CMPINT64 (bulk->operation_id, !=, last_operation_id);
4761+
ASSERT_CMPINT64 (bulk->operation_id, ==, client2->cluster.operation_id);
4762+
4763+
mongoc_bulk_operation_destroy (bulk);
4764+
mongoc_client_destroy (client);
4765+
mongoc_client_destroy (client2);
4766+
mock_server_destroy (mock_server);
4767+
}
4768+
47294769
void
47304770
test_bulk_install (TestSuite *suite)
47314771
{
@@ -4901,4 +4941,7 @@ test_bulk_install (TestSuite *suite)
49014941
/* Require server 4.2 for failCommand appName */
49024942
test_framework_skip_if_max_wire_version_less_than_8);
49034943
TestSuite_AddLive (suite, "/BulkOperation/set_client_after_operation", test_bulk_write_set_client_after_operation);
4944+
TestSuite_AddMockServerTest (suite,
4945+
"/BulkOperation/set_client_updates_operation_id_when_client_changes",
4946+
test_bulk_write_set_client_updates_operation_id_when_client_changes);
49044947
}

0 commit comments

Comments
 (0)