Skip to content

Commit 8c5da42

Browse files
committed
CDRIVER-3035 allow bulk OP_INSERT for w:0 insert_many
Since libmongoc requires MongoDB 3.0+, OP_INSERT is only used for w:0 writes. The allow_bulk_op_insert flag is removed since there is no need to limit OP_INSERT to single inserts.
1 parent 85f0427 commit 8c5da42

File tree

6 files changed

+67
-31
lines changed

6 files changed

+67
-31
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,7 @@ mongoc_bulk_operation_insert_with_opts (mongoc_bulk_operation_t *bulk,
382382
document,
383383
&insert_opts.extra,
384384
bulk->flags,
385-
bulk->operation_id,
386-
!mongoc_write_concern_is_acknowledged (bulk->write_concern));
385+
bulk->operation_id);
387386

388387
_mongoc_array_append_val (&bulk->commands, command);
389388

src/libmongoc/src/mongoc/mongoc-collection.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,8 +1517,7 @@ mongoc_collection_insert_bulk (mongoc_collection_t *collection,
15171517
NULL,
15181518
NULL,
15191519
write_flags,
1520-
++collection->client->cluster.operation_id,
1521-
true);
1520+
++collection->client->cluster.operation_id);
15221521

15231522
for (i = 0; i < n_documents; i++) {
15241523
_mongoc_write_command_insert_append (&command, documents[i]);
@@ -1634,8 +1633,7 @@ mongoc_collection_insert_one (mongoc_collection_t *collection,
16341633
&command,
16351634
document,
16361635
&insert_one_opts.extra,
1637-
++collection->client->cluster.operation_id,
1638-
false);
1636+
++collection->client->cluster.operation_id);
16391637

16401638
command.flags.bypass_document_validation = insert_one_opts.bypass;
16411639
_mongoc_collection_write_command_execute_idl (
@@ -1717,8 +1715,7 @@ mongoc_collection_insert_many (mongoc_collection_t *collection,
17171715
&command,
17181716
NULL,
17191717
&insert_many_opts.extra,
1720-
++collection->client->cluster.operation_id,
1721-
false);
1718+
++collection->client->cluster.operation_id);
17221719

17231720
command.flags.ordered = insert_many_opts.ordered;
17241721
command.flags.bypass_document_validation = insert_many_opts.bypass;

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ _mongoc_write_command_insert_legacy (mongoc_write_command_t *command,
245245
uint32_t idx = 0;
246246
int32_t max_msg_size;
247247
int32_t max_bson_obj_size;
248-
bool singly;
249248
bson_reader_t *reader;
250249
const bson_t *bson;
251250
bool eof;
@@ -265,8 +264,6 @@ _mongoc_write_command_insert_legacy (mongoc_write_command_t *command,
265264
max_bson_obj_size = mongoc_server_stream_max_bson_obj_size (server_stream);
266265
max_msg_size = mongoc_server_stream_max_msg_size (server_stream);
267266

268-
singly = !command->u.insert.allow_bulk_op_insert;
269-
270267
if (!command->n_documents) {
271268
bson_set_error (error,
272269
MONGOC_ERROR_COLLECTION,
@@ -303,8 +300,7 @@ _mongoc_write_command_insert_legacy (mongoc_write_command_t *command,
303300
/* send the batch so far (if any) and return the error */
304301
break;
305302
}
306-
} else if ((n_docs_in_batch == 1 && singly) ||
307-
size > (max_msg_size - bson->len)) {
303+
} else if (size > (max_msg_size - bson->len)) {
308304
/* batch is full, send it and then start the next batch */
309305
has_more = true;
310306
break;

src/libmongoc/src/mongoc/mongoc-write-command-private.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ typedef struct {
5858
mongoc_bulk_write_flags_t flags;
5959
int64_t operation_id;
6060
bson_t cmd_opts;
61-
union {
62-
struct {
63-
bool allow_bulk_op_insert;
64-
} insert;
65-
} u;
6661
} mongoc_write_command_t;
6762

6863

@@ -112,14 +107,12 @@ _mongoc_write_command_init_insert (mongoc_write_command_t *command,
112107
const bson_t *document,
113108
const bson_t *cmd_opts,
114109
mongoc_bulk_write_flags_t flags,
115-
int64_t operation_id,
116-
bool allow_bulk_op_insert);
110+
int64_t operation_id);
117111
void
118112
_mongoc_write_command_init_insert_idl (mongoc_write_command_t *command,
119113
const bson_t *document,
120114
const bson_t *cmd_opts,
121-
int64_t operation_id,
122-
bool allow_bulk_op_insert);
115+
int64_t operation_id);
123116
void
124117
_mongoc_write_command_init_delete (mongoc_write_command_t *command,
125118
const bson_t *selectors,

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,7 @@ _mongoc_write_command_init_insert (mongoc_write_command_t *command, /* IN */
198198
const bson_t *document, /* IN */
199199
const bson_t *cmd_opts, /* IN */
200200
mongoc_bulk_write_flags_t flags, /* IN */
201-
int64_t operation_id, /* IN */
202-
bool allow_bulk_op_insert) /* IN */
201+
int64_t operation_id) /* IN */
203202
{
204203
ENTRY;
205204

@@ -208,7 +207,6 @@ _mongoc_write_command_init_insert (mongoc_write_command_t *command, /* IN */
208207
_mongoc_write_command_init_bulk (
209208
command, MONGOC_WRITE_COMMAND_INSERT, flags, operation_id, cmd_opts);
210209

211-
command->u.insert.allow_bulk_op_insert = (uint8_t) allow_bulk_op_insert;
212210
/* must handle NULL document from mongoc_collection_insert_bulk */
213211
if (document) {
214212
_mongoc_write_command_insert_append (command, document);
@@ -222,8 +220,7 @@ void
222220
_mongoc_write_command_init_insert_idl (mongoc_write_command_t *command,
223221
const bson_t *document,
224222
const bson_t *cmd_opts,
225-
int64_t operation_id,
226-
bool allow_bulk_op_insert)
223+
int64_t operation_id)
227224
{
228225
mongoc_bulk_write_flags_t flags = MONGOC_BULK_WRITE_FLAGS_INIT;
229226

@@ -234,7 +231,6 @@ _mongoc_write_command_init_insert_idl (mongoc_write_command_t *command,
234231
_mongoc_write_command_init_bulk (
235232
command, MONGOC_WRITE_COMMAND_INSERT, flags, operation_id, cmd_opts);
236233

237-
command->u.insert.allow_bulk_op_insert = (uint8_t) allow_bulk_op_insert;
238234
/* must handle NULL document from mongoc_collection_insert_bulk */
239235
if (document) {
240236
_mongoc_write_command_insert_append (command, document);

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

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ test_split_insert (void)
5151
docs[0],
5252
NULL,
5353
write_flags,
54-
++client->cluster.operation_id,
55-
true);
54+
++client->cluster.operation_id);
5655

5756
for (i = 1; i < 3000; i++) {
5857
_mongoc_write_command_insert_append (&command, docs[i]);
@@ -127,7 +126,7 @@ test_invalid_write_concern (void)
127126
doc = BCON_NEW ("_id", BCON_INT32 (0));
128127

129128
_mongoc_write_command_init_insert (
130-
&command, doc, NULL, write_flags, ++client->cluster.operation_id, true);
129+
&command, doc, NULL, write_flags, ++client->cluster.operation_id);
131130
_mongoc_write_result_init (&result);
132131
server_stream =
133132
mongoc_cluster_stream_for_writes (&client->cluster, NULL, NULL, &error);
@@ -524,6 +523,59 @@ test_opmsg_disconnect_mid_batch (void)
524523
test_opmsg_disconnect_mid_batch_helper (WIRE_VERSION_OP_MSG - 1);
525524
}
526525

526+
static void
527+
test_w0_legacy_insert_many (void)
528+
{
529+
mock_server_t *server;
530+
mongoc_client_t *client;
531+
mongoc_collection_t *coll;
532+
bson_t **docs;
533+
bson_error_t error;
534+
future_t *future;
535+
request_t *request;
536+
bson_t opts;
537+
mongoc_write_concern_t *wc;
538+
539+
/* wire version will use OP_INSERT for w:0 insert many (since no OP_MSG) */
540+
server = mock_server_new ();
541+
mock_server_auto_ismaster (server,
542+
"{'ismaster': true,"
543+
" 'maxWireVersion': 5}");
544+
mock_server_run (server);
545+
546+
docs = bson_malloc (sizeof (bson_t *) * 2);
547+
docs[0] = BCON_NEW ("x", BCON_INT32 (1));
548+
docs[1] = BCON_NEW ("x", BCON_INT32 (2));
549+
550+
client = mongoc_client_new_from_uri (mock_server_get_uri (server));
551+
coll = mongoc_client_get_collection (client, "db", "coll");
552+
553+
/* Add unacknowldged write concern */
554+
bson_init (&opts);
555+
wc = mongoc_write_concern_new ();
556+
mongoc_write_concern_set_w (wc, 0);
557+
mongoc_write_concern_append (wc, &opts);
558+
mongoc_write_concern_destroy (wc);
559+
560+
future = future_collection_insert_many (
561+
coll, (const bson_t **) docs, 2, &opts, NULL, &error);
562+
/* Mock server receives one OP_INSERT with two documents */
563+
request = mock_server_receives_bulk_insert (server, "db.coll", 0, 2);
564+
BSON_ASSERT (request);
565+
566+
mock_server_replies_ok_and_destroys (request);
567+
BSON_ASSERT (future_get_bool (future));
568+
569+
future_destroy (future);
570+
bson_destroy (docs[0]);
571+
bson_destroy (docs[1]);
572+
bson_free (docs);
573+
bson_destroy (&opts);
574+
mongoc_collection_destroy (coll);
575+
mongoc_client_destroy (client);
576+
mock_server_destroy (server);
577+
}
578+
527579
void
528580
test_write_command_install (TestSuite *suite)
529581
{
@@ -544,4 +596,7 @@ test_write_command_install (TestSuite *suite)
544596
TestSuite_AddMockServerTest (suite,
545597
"/WriteCommand/insert_disconnect_mid_batch",
546598
test_opmsg_disconnect_mid_batch);
599+
TestSuite_AddMockServerTest (suite,
600+
"/WriteCommand/w0_legacy_insert_many",
601+
test_w0_legacy_insert_many);
547602
}

0 commit comments

Comments
 (0)