Skip to content

Commit 2175557

Browse files
author
rstam
committed
CSHARP-693: InsertBatch must call GLE after all sub-batches even if WriteConcern is Unacknowledged but ContinueOnError is false.
1 parent 87cd1dd commit 2175557

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

MongoDB.Driver/MongoCollection.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,8 +1177,25 @@ public virtual IEnumerable<WriteConcernResult> InsertBatch(
11771177
if (message.MessageLength > connection.ServerInstance.MaxMessageLength)
11781178
{
11791179
byte[] lastDocument = message.RemoveLastDocument(bsonBuffer);
1180-
var intermediateResult = connection.SendMessage(bsonBuffer, message, writeConcern, _database.Name);
1181-
if (writeConcern.Enabled) { results.Add(intermediateResult); }
1180+
1181+
if (writeConcern.Enabled || (options.Flags & InsertFlags.ContinueOnError) != 0)
1182+
{
1183+
var intermediateResult = connection.SendMessage(bsonBuffer, message, writeConcern, _database.Name);
1184+
if (writeConcern.Enabled) { results.Add(intermediateResult); }
1185+
}
1186+
else
1187+
{
1188+
// if WriteConcern is disabled and ContinueOnError is false we have to check for errors and stop if sub-batch has error
1189+
try
1190+
{
1191+
connection.SendMessage(bsonBuffer, message, WriteConcern.Acknowledged, _database.Name);
1192+
}
1193+
catch (WriteConcernException)
1194+
{
1195+
return null;
1196+
}
1197+
}
1198+
11821199
message.ResetBatch(bsonBuffer, lastDocument);
11831200
}
11841201
}

MongoDB.DriverUnitTests/MongoCollectionTests.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,80 @@ public void TestInsertBatchContinueOnError()
10841084
}
10851085
}
10861086

1087+
[Test]
1088+
public void TestInsertBatchMultipleBatchesWriteConcernDisabledContinueOnErrorFalse()
1089+
{
1090+
var collectionName = Configuration.TestCollection.Name;
1091+
var collectionSettings = new MongoCollectionSettings { WriteConcern = WriteConcern.Unacknowledged };
1092+
var collection = Configuration.TestDatabase.GetCollection<BsonDocument>(collectionName, collectionSettings);
1093+
if (collection.Exists()) { collection.Drop(); }
1094+
1095+
using (Configuration.TestDatabase.RequestStart())
1096+
{
1097+
var maxMessageLength = Configuration.TestServer.RequestConnection.ServerInstance.MaxMessageLength;
1098+
1099+
var filler = new string('x', maxMessageLength / 3); // after overhead results in two documents per sub-batch
1100+
var documents = new BsonDocument[]
1101+
{
1102+
// first sub-batch
1103+
new BsonDocument { { "_id", 1 }, { "filler", filler } },
1104+
new BsonDocument { { "_id", 2 }, { "filler", filler } },
1105+
// second sub-batch
1106+
new BsonDocument { { "_id", 3 }, { "filler", filler } },
1107+
new BsonDocument { { "_id", 3 }, { "filler", filler } }, // duplicate _id error
1108+
// third sub-batch
1109+
new BsonDocument { { "_id", 4 }, { "filler", filler } },
1110+
new BsonDocument { { "_id", 5 }, { "filler", filler } },
1111+
};
1112+
1113+
var options = new MongoInsertOptions { Flags = InsertFlags.None }; // no ContinueOnError
1114+
collection.InsertBatch(documents, options);
1115+
1116+
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 1)));
1117+
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 2)));
1118+
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 3)));
1119+
Assert.AreEqual(0, collection.Count(Query.EQ("_id", 4)));
1120+
Assert.AreEqual(0, collection.Count(Query.EQ("_id", 5)));
1121+
}
1122+
}
1123+
1124+
[Test]
1125+
public void TestInsertBatchMultipleBatchesWriteConcernDisabledContinueOnErrorTrue()
1126+
{
1127+
var collectionName = Configuration.TestCollection.Name;
1128+
var collectionSettings = new MongoCollectionSettings { WriteConcern = WriteConcern.Unacknowledged };
1129+
var collection = Configuration.TestDatabase.GetCollection<BsonDocument>(collectionName, collectionSettings);
1130+
if (collection.Exists()) { collection.Drop(); }
1131+
1132+
using (Configuration.TestDatabase.RequestStart())
1133+
{
1134+
var maxMessageLength = Configuration.TestServer.RequestConnection.ServerInstance.MaxMessageLength;
1135+
1136+
var filler = new string('x', maxMessageLength / 3); // after overhead results in two documents per sub-batch
1137+
var documents = new BsonDocument[]
1138+
{
1139+
// first sub-batch
1140+
new BsonDocument { { "_id", 1 }, { "filler", filler } },
1141+
new BsonDocument { { "_id", 2 }, { "filler", filler } },
1142+
// second sub-batch
1143+
new BsonDocument { { "_id", 3 }, { "filler", filler } },
1144+
new BsonDocument { { "_id", 3 }, { "filler", filler } }, // duplicate _id error
1145+
// third sub-batch
1146+
new BsonDocument { { "_id", 4 }, { "filler", filler } },
1147+
new BsonDocument { { "_id", 5 }, { "filler", filler } },
1148+
};
1149+
1150+
var options = new MongoInsertOptions { Flags = InsertFlags.ContinueOnError };
1151+
collection.InsertBatch(documents, options);
1152+
1153+
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 1)));
1154+
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 2)));
1155+
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 3)));
1156+
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 4)));
1157+
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 5)));
1158+
}
1159+
}
1160+
10871161
[Test]
10881162
public void TestIsCappedFalse()
10891163
{

0 commit comments

Comments
 (0)