Skip to content

Commit 9d7e411

Browse files
authored
Merge pull request #348 from DataObjects-NET/6.0-batching-problem
fix batching problem of skipping some tasks
2 parents af8a46e + 9d06aa2 commit 9d7e411

File tree

6 files changed

+244
-62
lines changed

6 files changed

+244
-62
lines changed

ChangeLog/6.0.12_dev.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[main] Addressed DataTypeCollection.Add method issue of wrong adding storage-specifid types to the collection
22
[main] Addressed rare issue of entities expire management when SessionOption.NonTransactionalReads is on
3+
[main] Fixed issue of skipping persist or query tasks during batching
34
[sqlserver] Sql error messages for British English are correctly parsed

Orm/Xtensive.Orm.Tests/Storage/CommandProcessing/BatchingCommandProcessorParametersManagement.cs

+169-14
Original file line numberDiff line numberDiff line change
@@ -602,13 +602,16 @@ public void InsertTest01()
602602
using (var session = Domain.OpenSession())
603603
using (var counter = new CommandCounter(session))
604604
using (var transaction = session.OpenTransaction()) {
605+
var before = session.Query.All<ALotOfFieldsEntityValid>().Count();
606+
605607
_ = new ALotOfFieldsEntityValid();
606608

607609
using (counter.Attach()) {
608610
Assert.DoesNotThrow(() => session.SaveChanges());
609611
}
610612

611613
Assert.That(counter.Count, Is.EqualTo(1));
614+
Assert.That(session.Query.All<ALotOfFieldsEntityValid>().Count(), Is.EqualTo(before + 1));
612615
}
613616
}
614617

@@ -618,6 +621,8 @@ public void InsertTest02()
618621
using (var session = Domain.OpenSession())
619622
using (var counter = new CommandCounter(session))
620623
using (var transaction = session.OpenTransaction()) {
624+
var before = session.Query.All<ALotOfFieldsEntityValid>().Count();
625+
621626
_ = new ALotOfFieldsEntityValid();
622627
_ = new ALotOfFieldsEntityValid();
623628
_ = new ALotOfFieldsEntityValid();
@@ -631,6 +636,8 @@ public void InsertTest02()
631636
var expectedCommandCount = Math.Ceiling(
632637
Math.Ceiling(4 * Domain.Model.Types[type].Fields.Count / (decimal) StorageLimit));
633638
Assert.That(counter.Count, Is.EqualTo(expectedCommandCount));
639+
640+
Assert.That(session.Query.All<ALotOfFieldsEntityValid>().Count(), Is.EqualTo(before + 4));
634641
}
635642
}
636643

@@ -849,11 +856,13 @@ public void UpdateRegularTest01()
849856
}
850857

851858
[Test]
852-
public void PartitialExecutionAllowedTest01()
859+
public void PartialExecutionAllowedTest01()
853860
{
854861
using (var session = Domain.OpenSession())
855862
using (var counter = new CommandCounter(session))
856863
using (var transaction = session.OpenTransaction()) {
864+
var countBefore = session.Query.All<ALotOfFieldsEntityValid>().Count();
865+
857866
_ = new ALotOfFieldsEntityValid();
858867
_ = new ALotOfFieldsEntityValid();
859868
_ = new ALotOfFieldsEntityValid();
@@ -867,15 +876,44 @@ public void PartitialExecutionAllowedTest01()
867876

868877
counter.Reset();
869878
using (counter.Attach()) {
870-
Assert.That(session.Query.All<ALotOfFieldsEntityValid>().Count(), Is.EqualTo(5));
879+
Assert.That(session.Query.All<ALotOfFieldsEntityValid>().Count(), Is.EqualTo(countBefore + 3));
871880
}
872881

873882
Assert.That(counter.Count, Is.EqualTo(2));
874883
}
875884
}
876885

877886
[Test]
878-
public void PartitialExecutionAllowedTest02()
887+
public async Task PartialExecutionAllowedAsyncTest01()
888+
{
889+
using (var session = Domain.OpenSession())
890+
using (var counter = new CommandCounter(session))
891+
using (var transaction = session.OpenTransaction()) {
892+
var countBefore = session.Query.All<ALotOfFieldsEntityValid>().Count();
893+
894+
_ = new ALotOfFieldsEntityValid();
895+
_ = new ALotOfFieldsEntityValid();
896+
_ = new ALotOfFieldsEntityValid();
897+
898+
//persist by query causes allowPartialExecution = true;
899+
using (counter) {
900+
session.Persist(PersistReason.Query);
901+
}
902+
903+
Assert.That(counter.Count, Is.EqualTo(0));
904+
905+
counter.Reset();
906+
using (counter.Attach()) {
907+
var result = (await session.Query.All<ALotOfFieldsEntityValid>().AsAsync()).ToArray().Length;
908+
Assert.That(result, Is.EqualTo(countBefore + 3));
909+
}
910+
911+
Assert.That(counter.Count, Is.EqualTo(2));
912+
}
913+
}
914+
915+
[Test]
916+
public void PartialExecutionAllowedTest02()
879917
{
880918
using (var session = Domain.OpenSession())
881919
using (var counter = new CommandCounter(session))
@@ -901,33 +939,76 @@ public void PartitialExecutionAllowedTest02()
901939
Assert.That(counter.Count, Is.EqualTo(1));
902940
counter.Reset();
903941
using (counter.Attach()) {
904-
Assert.That(session.Query.All<NormalAmountOfFieldsEntity>().Count(), Is.EqualTo(26));
942+
Assert.That(session.Query.All<NormalAmountOfFieldsEntity>().Count(), Is.EqualTo(batchSize + 1));
905943
}
906944
Assert.That(counter.Count, Is.EqualTo(1));
907945
}
908946
}
909947

910948
[Test]
911-
public void PartialExecutionDeniedTest01()
949+
public async Task PartialExecutionAllowedAsyncTest02()
912950
{
913951
using (var session = Domain.OpenSession())
914952
using (var counter = new CommandCounter(session))
915953
using (var transaction = session.OpenTransaction()) {
916-
_ = new ALotOfFieldsEntityValid();
917-
_ = new ALotOfFieldsEntityValid();
918-
_ = new ALotOfFieldsEntityValid();
954+
var batchSize = session.Configuration.BatchSize;
955+
var currentBatchCapacity = batchSize;
919956

920-
//manual persist causes allowPartialExecution = false;
957+
Console.WriteLine(batchSize);
958+
// one complete batch;
959+
while (currentBatchCapacity > 0) {
960+
_ = new NormalAmountOfFieldsEntity();
961+
currentBatchCapacity--;
962+
}
963+
964+
// extra task to have extra batch
965+
_ = new NormalAmountOfFieldsEntity();
966+
967+
//persist by query causes allowPartialExecution = true;
921968
using (counter.Attach()) {
922-
session.SaveChanges();
969+
session.Persist(PersistReason.Query);
923970
}
924971

972+
Assert.That(counter.Count, Is.EqualTo(1));
973+
counter.Reset();
974+
using (counter.Attach()) {
975+
var result = (await session.Query.All<NormalAmountOfFieldsEntity>().AsAsync()).ToArray().Length;
976+
Assert.That(result, Is.EqualTo(batchSize + 1));
977+
}
978+
Assert.That(counter.Count, Is.EqualTo(1));
979+
}
980+
}
981+
982+
[Test]
983+
public void PartialExecutionAllowedTest03()
984+
{
985+
using (var session = Domain.OpenSession())
986+
using (var counter = new CommandCounter(session))
987+
using (var transaction = session.OpenTransaction()) {
988+
var batchSize = session.Configuration.BatchSize;
989+
var currentBatchCapacity = batchSize;
990+
991+
Console.WriteLine(batchSize);
992+
// one complete batch;
993+
while (currentBatchCapacity > 0) {
994+
_ = new NormalAmountOfFieldsEntity();
995+
currentBatchCapacity--;
996+
}
997+
998+
// extra task to have extra batch
999+
_ = new NormalAmountOfFieldsEntity();
1000+
1001+
//persist by query causes allowPartialExecution = true;
1002+
using (counter.Attach()) {
1003+
var result = session.Query.All<NormalAmountOfFieldsEntity>().ToArray();
1004+
Assert.That(result.Length, Is.EqualTo(batchSize + 1));
1005+
}
9251006
Assert.That(counter.Count, Is.EqualTo(2));
9261007
}
9271008
}
9281009

9291010
[Test]
930-
public void PartialExecutionDeniedTest02()
1011+
public async Task PartialExecutionAllowedAsyncTest03()
9311012
{
9321013
using (var session = Domain.OpenSession())
9331014
using (var counter = new CommandCounter(session))
@@ -947,14 +1028,45 @@ public void PartialExecutionDeniedTest02()
9471028

9481029
//persist by query causes allowPartialExecution = true;
9491030
using (counter.Attach()) {
950-
session.Query.All<NormalAmountOfFieldsEntity>().Run();
1031+
var result = (await session.Query.All<NormalAmountOfFieldsEntity>().AsAsync()).ToArray();
1032+
Assert.That(result.Length, Is.EqualTo(batchSize + 1));
9511033
}
9521034
Assert.That(counter.Count, Is.EqualTo(2));
9531035
}
9541036
}
9551037

9561038
[Test]
957-
public async Task PartialExecutionDeniedAsyncTest02()
1039+
public void PartialExecutionAllowedTest04()
1040+
{
1041+
using (var session = Domain.OpenSession())
1042+
using (var counter = new CommandCounter(session))
1043+
using (var transaction = session.OpenTransaction()) {
1044+
var countBefore = session.Query.All<ALotOfFieldsEntityValid>().Count();
1045+
1046+
var batchSize = session.Configuration.BatchSize;
1047+
var currentBatchCapacity = batchSize;
1048+
1049+
Console.WriteLine(batchSize);
1050+
// one complete batch;
1051+
while (currentBatchCapacity > 0) {
1052+
_ = new ALotOfFieldsEntityValid();
1053+
currentBatchCapacity--;
1054+
}
1055+
1056+
// extra task to have extra batch
1057+
_ = new ALotOfFieldsEntityValid();
1058+
1059+
//persist by query causes allowPartialExecution = true;
1060+
using (counter.Attach()) {
1061+
var result = session.Query.All<ALotOfFieldsEntityValid>().ToArray();
1062+
Assert.That(result.Length, Is.EqualTo(countBefore + batchSize + 1));
1063+
}
1064+
Assert.That(counter.Count, Is.EqualTo(13));
1065+
}
1066+
}
1067+
1068+
[Test]
1069+
public async Task PartialExecutionAllowedAsyncTest04()
9581070
{
9591071
using (var session = Domain.OpenSession())
9601072
using (var counter = new CommandCounter(session))
@@ -974,8 +1086,51 @@ public async Task PartialExecutionDeniedAsyncTest02()
9741086

9751087
//persist by query causes allowPartialExecution = true;
9761088
using (counter.Attach()) {
977-
(await session.Query.All<NormalAmountOfFieldsEntity>().AsAsync()).Run();
1089+
var result = (await session.Query.All<NormalAmountOfFieldsEntity>().AsAsync()).ToArray();
1090+
Assert.That(result.Length, Is.EqualTo(batchSize + 1));
1091+
}
1092+
Assert.That(counter.Count, Is.EqualTo(2));
1093+
}
1094+
}
1095+
1096+
[Test]
1097+
public void PartialExecutionAllowedTest05()
1098+
{
1099+
Require.ProviderIs(StorageProvider.SqlServer);
1100+
1101+
var sessionOf25ItemsBatch = new SessionConfiguration(WellKnown.Sessions.Default, SessionOptions.Default | SessionOptions.AutoActivation) { BatchSize = 25 };
1102+
using (var session = Domain.OpenSession(sessionOf25ItemsBatch))
1103+
using (var tx = session.OpenTransaction()) {
1104+
Assert.AreEqual(0, session.Query.All<OneHundredFieldsEntity>().Count());
1105+
1106+
for (var i = 0; i < session.Configuration.BatchSize; i++) {
1107+
var item = new OneHundredFieldsEntity();
1108+
1109+
for (var j = 1; j < 98; j++) {
1110+
item["Value" + j] = i;
1111+
}
1112+
}
1113+
1114+
var count = session.Query.All<OneHundredFieldsEntity>().ToArray().Length;
1115+
Assert.AreEqual(session.Configuration.BatchSize, count);
1116+
}
1117+
}
1118+
1119+
[Test]
1120+
public void PartialExecutionDeniedTest01()
1121+
{
1122+
using (var session = Domain.OpenSession())
1123+
using (var counter = new CommandCounter(session))
1124+
using (var transaction = session.OpenTransaction()) {
1125+
_ = new ALotOfFieldsEntityValid();
1126+
_ = new ALotOfFieldsEntityValid();
1127+
_ = new ALotOfFieldsEntityValid();
1128+
1129+
//manual persist causes allowPartialExecution = false;
1130+
using (counter.Attach()) {
1131+
session.SaveChanges();
9781132
}
1133+
9791134
Assert.That(counter.Count, Is.EqualTo(2));
9801135
}
9811136
}

Orm/Xtensive.Orm.Tests/Storage/CommandProcessing/ParametersManagementModel.cs

+13
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ public class ALotOfFieldsEntityVersionized : Entity
107107
// fields are added dynamically
108108
}
109109

110+
[HierarchyRoot]
111+
public class OneHundredFieldsEntity : Entity
112+
{
113+
[Field, Key]
114+
public int Id { get; private set; }
115+
116+
// fields are added dynamically
117+
}
118+
110119

111120
[HierarchyRoot(InheritanceSchema.ClassTable)]
112121
public class SeveralPersistActionsEntityValidA : Entity
@@ -174,6 +183,10 @@ public void OnDefinitionsBuilt(BuildingContext context, DomainModelDef model)
174183
_ = t.DefineField(fieldName, typeof(int));
175184
}
176185

186+
t = model.Types[typeof(OneHundredFieldsEntity)];
187+
foreach (var fieldName in GetFieldNames(98)) {
188+
_ = t.DefineField(fieldName, typeof(int));
189+
}
177190

178191
// each entitity field count is valid
179192
// overall entity field count is valid

0 commit comments

Comments
 (0)