Skip to content

Commit e6cf2a3

Browse files
committed
CDRIVER-3239 respect must_stop in _mongoc_write_opmsg
1 parent 15a1985 commit e6cf2a3

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/libmongoc/src/mongoc/mongoc-write-command.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,9 @@ _mongoc_write_opmsg (mongoc_write_command_t *command,
618618

619619
if (!ret) {
620620
result->failed = true;
621+
/* Conservatively set must_stop to true. Per CDRIVER-3305 we
622+
* shouldn't stop for unordered bulk writes, but also need to check
623+
* if the server stream was invalidated per CDRIVER-3306. */
621624
result->must_stop = true;
622625
}
623626

@@ -631,7 +634,7 @@ _mongoc_write_opmsg (mongoc_write_command_t *command,
631634
bson_destroy (&reply);
632635
}
633636
/* While we have more documents to write */
634-
} while (payload_total_offset < command->payload.len);
637+
} while (payload_total_offset < command->payload.len && !result->must_stop);
635638

636639
bson_destroy (&cmd);
637640
mongoc_cmd_parts_cleanup (&parts);

src/libmongoc/tests/test-mongoc-write-commands.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,72 @@ test_split_opquery_with_options (void)
458458
mock_server_destroy (server);
459459
}
460460

461+
static void
462+
test_opmsg_disconnect_mid_batch_helper (int wire_version)
463+
{
464+
mock_server_t *server;
465+
mongoc_client_t *client;
466+
mongoc_collection_t *coll;
467+
bson_t **docs;
468+
int i;
469+
bson_error_t error;
470+
future_t *future;
471+
request_t *request;
472+
int n_docs;
473+
474+
/* Use a reduced maxBsonObjectSize, and wire version for OP_QUERY */
475+
const char *ismaster = "{'ok': 1.0,"
476+
" 'ismaster': true,"
477+
" 'minWireVersion': 0,"
478+
" 'maxWireVersion': %d,"
479+
" 'maxBsonObjectSize': 100}";
480+
481+
server = mock_server_new ();
482+
mock_server_auto_ismaster (server, ismaster, wire_version);
483+
mock_server_run (server);
484+
485+
/* create enough documents for two batches. Note, because of our wonky
486+
* batch splitting behavior (to be fixed in CDRIVER-3310) we need add 16K
487+
* of documents. After CDRIVER-3310, we'll need to update this test. */
488+
n_docs = ((BSON_OBJECT_ALLOWANCE) / tmp_bson ("{ '_id': 1 }")->len) + 1;
489+
docs = bson_malloc (sizeof (bson_t *) * n_docs);
490+
for (i = 0; i < n_docs; i++) {
491+
docs[i] = BCON_NEW ("_id", BCON_INT64 (i));
492+
}
493+
494+
client = mongoc_client_new_from_uri (mock_server_get_uri (server));
495+
mongoc_client_set_error_api (client, MONGOC_ERROR_API_VERSION_2);
496+
coll = mongoc_client_get_collection (client, "db", "coll");
497+
498+
future = future_collection_insert_many (
499+
coll, (const bson_t **) docs, n_docs, NULL, NULL, &error);
500+
/* Mock server recieves first insert. */
501+
request = mock_server_receives_request (server);
502+
BSON_ASSERT (request);
503+
mock_server_hangs_up (request);
504+
request_destroy (request);
505+
506+
BSON_ASSERT (!future_get_bool (future));
507+
future_destroy (future);
508+
ASSERT_ERROR_CONTAINS (
509+
error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_SOCKET, "socket error");
510+
511+
for (i = 0; i < n_docs; i++) {
512+
bson_destroy (docs[i]);
513+
}
514+
bson_free (docs);
515+
mongoc_collection_destroy (coll);
516+
mongoc_client_destroy (client);
517+
mock_server_destroy (server);
518+
}
519+
520+
static void
521+
test_opmsg_disconnect_mid_batch (void)
522+
{
523+
test_opmsg_disconnect_mid_batch_helper (WIRE_VERSION_OP_MSG);
524+
test_opmsg_disconnect_mid_batch_helper (WIRE_VERSION_OP_MSG - 1);
525+
}
526+
461527
void
462528
test_write_command_install (TestSuite *suite)
463529
{
@@ -475,4 +541,7 @@ test_write_command_install (TestSuite *suite)
475541
TestSuite_AddMockServerTest (suite,
476542
"/WriteCommand/split_opquery_with_options",
477543
test_split_opquery_with_options);
544+
TestSuite_AddMockServerTest (suite,
545+
"/WriteCommand/insert_disconnect_mid_batch",
546+
test_opmsg_disconnect_mid_batch);
478547
}

0 commit comments

Comments
 (0)