-
Notifications
You must be signed in to change notification settings - Fork 188
/
ConcurrencyTest.java
795 lines (663 loc) · 34.7 KB
/
ConcurrencyTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/*
* Copyright (c) 2010-2019 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.repo.sql;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import javax.xml.namespace.QName;
import org.hibernate.Session;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.Test;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.delta.PropertyDelta;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.prism.xml.XmlTypeConverter;
import com.evolveum.midpoint.repo.sql.testing.SqlRepoTestUtil;
import com.evolveum.midpoint.schema.SearchResultList;
import com.evolveum.midpoint.schema.constants.ObjectTypes;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
/**
* @author Pavol Mederly
*/
@ContextConfiguration(locations = { "../../../../../ctx-test.xml" })
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class ConcurrencyTest extends BaseSQLRepoTest {
private static final long WAIT_FOR_THREAD_NATURAL_STOP_TIME = 300000;
@Override
public void initSystem() throws Exception {
Session session = getFactory().openSession();
session.doWork(connection -> System.out.println(">>>> Connection.TRANSACTION_* (transaction isolation level): " + connection.getTransactionIsolation()));
session.close();
}
@Test
public void test001TwoWriters_OneAttributeEach__NoReader() throws Exception {
PropertyModifierThread[] mts = new PropertyModifierThread[] {
new PropertyModifierThread(1, UserType.F_GIVEN_NAME, true, null, true),
new PropertyModifierThread(2, UserType.F_FAMILY_NAME, true, null, true),
};
concurrencyUniversal("Test1", 30000L, 500L, mts, null);
}
@Test
public void test002FourWriters_OneAttributeEach__NoReader() throws Exception {
PropertyModifierThread[] mts = new PropertyModifierThread[] {
new PropertyModifierThread(1, UserType.F_GIVEN_NAME, true, null, true),
new PropertyModifierThread(2, UserType.F_FAMILY_NAME, true, null, true),
new PropertyModifierThread(3, UserType.F_DESCRIPTION, false, null, true),
new PropertyModifierThread(4, UserType.F_EMAIL_ADDRESS, false, null, true)
};
concurrencyUniversal("Test2", 60000L, 500L, mts, null);
}
@Test
public void test003OneWriter_TwoAttributes__OneReader() throws Exception {
PropertyModifierThread[] mts = new PropertyModifierThread[] {
new PropertyModifierThread(1, UserType.F_GIVEN_NAME, true,
ItemPath.create(UserType.F_ASSIGNMENT, 1L, AssignmentType.F_DESCRIPTION),
true)
};
Checker checker = (iteration, oid) -> {
PrismObject<UserType> userRetrieved = repositoryService.getObject(UserType.class, oid, null, new OperationResult("dummy"));
String givenName = userRetrieved.asObjectable().getGivenName().getOrig();
String assignmentDescription = userRetrieved.asObjectable().getAssignment().get(0).getDescription();
logger.info("[" + iteration + "] givenName = " + givenName + ", assignment description = " + assignmentDescription);
if (!givenName.equals(assignmentDescription)) {
String msg = "Inconsistent object state: GivenName = " + givenName + ", assignment description = " + assignmentDescription;
logger.error(msg);
throw new AssertionError(msg);
}
};
concurrencyUniversal("Test3", 60000L, 0L, mts, checker);
}
@Test
public void test004TwoWriters_TwoAttributesEach__OneReader() throws Exception {
PropertyModifierThread[] mts = new PropertyModifierThread[] {
new PropertyModifierThread(1, UserType.F_GIVEN_NAME, true,
ItemPath.create(UserType.F_ASSIGNMENT, 1L, AssignmentType.F_DESCRIPTION),
true),
new PropertyModifierThread(2, UserType.F_FAMILY_NAME, true,
ItemPath.create(UserType.F_ASSIGNMENT, 1L, AssignmentType.F_CONSTRUCTION),
true),
};
Checker checker = (iteration, oid) -> {
PrismObject<UserType> userRetrieved = repositoryService.getObject(UserType.class, oid, null, new OperationResult("dummy"));
String givenName = userRetrieved.asObjectable().getGivenName().getOrig();
String familyName = userRetrieved.asObjectable().getFamilyName().getOrig();
String assignmentDescription = userRetrieved.asObjectable().getAssignment().get(0).getDescription();
String referenceDescription = userRetrieved.asObjectable().getAssignment().get(0).getConstruction().getDescription();
logger.info("[" + iteration + "] givenName = " + givenName + ", assignment description = " + assignmentDescription + ", familyName = " + familyName + ", referenceDescription = " + referenceDescription);
if (!givenName.equals(assignmentDescription)) {
String msg = "Inconsistent object state: GivenName = " + givenName + ", assignment description = " + assignmentDescription;
logger.error(msg);
throw new AssertionError(msg);
}
if (!familyName.equals(referenceDescription)) {
String msg = "Inconsistent object state: FamilyName = " + familyName + ", account construction description = " + referenceDescription;
logger.error(msg);
throw new AssertionError(msg);
}
};
concurrencyUniversal("Test4", 60000L, 0L, mts, checker);
}
@FunctionalInterface
private interface Checker {
void check(int iteration, String oid) throws Exception;
}
private void concurrencyUniversal(String name, long duration, long waitStep, PropertyModifierThread[] modifierThreads, Checker checker) throws Exception {
final File file = new File("src/test/resources/concurrency/user.xml");
PrismObject<UserType> user = prismContext.parseObject(file);
user.asObjectable().setName(new PolyStringType(name));
OperationResult result = new OperationResult("Concurrency Test");
String oid = repositoryService.addObject(user, null, result);
logger.info("*** Object added: {} ***", oid);
logger.info("*** Starting modifier threads ***");
for (PropertyModifierThread mt : modifierThreads) {
mt.setObject(UserType.class, oid);
mt.start();
}
logger.info("*** Waiting {} ms ***", duration);
long startTime = System.currentTimeMillis();
int readIteration = 1;
main:
while (System.currentTimeMillis() - startTime < duration) {
if (checker != null) {
checker.check(readIteration, oid);
}
if (waitStep > 0L) {
Thread.sleep(waitStep);
}
for (PropertyModifierThread mt : modifierThreads) {
if (!mt.isAlive()) {
logger.error("At least one of threads died prematurely, finishing waiting.");
break main;
}
}
readIteration++;
}
for (PropertyModifierThread mt : modifierThreads) {
mt.stop = true; // stop the threads
System.out.println("Thread " + mt.id + " has done " + mt.counter.get() + " iterations");
logger.info("Thread " + mt.id + " has done " + mt.counter.get() + " iterations");
}
// we do not have to wait for the threads to be stopped, just examine their results
Thread.sleep(1000); // give the threads a chance to finish (before repo will be shut down)
for (PropertyModifierThread mt : modifierThreads) {
logger.info("Modifier thread {} finished with an exception", mt.id, mt.threadResult);
}
for (PropertyModifierThread mt : modifierThreads) {
if (mt.threadResult != null) {
throw new AssertionError("Modifier thread " + mt.id + " finished with an exception: " + mt.threadResult, mt.threadResult);
}
}
}
abstract class WorkerThread extends Thread {
int id;
Class<? extends ObjectType> objectClass; // object to modify
String oid; // object to modify
String lastVersion = null;
volatile Throwable threadResult;
AtomicInteger counter = new AtomicInteger(0);
Integer limit;
WorkerThread(int id) {
this.id = id;
}
WorkerThread(int id, int limit) {
this.id = id;
this.limit = limit;
}
public volatile boolean stop = false;
@Override
public void run() {
try {
while (!stop && (limit == null || counter.intValue() < limit)) {
OperationResult result = new OperationResult("run");
logger.info(" --- Iteration number {} for {} ---", counter.incrementAndGet(), description());
runOnce(result);
}
} catch (Throwable t) {
LoggingUtils.logException(logger, "Got exception: " + t, t);
threadResult = t;
}
}
abstract void runOnce(OperationResult result) throws Exception;
abstract String description();
public void setObject(Class<? extends ObjectType> objectClass, String oid) {
this.objectClass = objectClass;
this.oid = oid;
}
@Override
public String toString() {
return description() + " @" + counter;
}
}
class PropertyModifierThread extends WorkerThread {
final ItemPath attribute1; // attribute to modify
final ItemPath attribute2; // attribute to modify
boolean poly;
boolean checkValue;
PropertyModifierThread(int id, ItemPath attribute1, boolean poly, ItemPath attribute2, boolean checkValue) {
super(id);
this.attribute1 = attribute1;
this.attribute2 = attribute2;
this.poly = poly;
this.setName("Modifier for " + attributeNames());
this.checkValue = checkValue;
}
private String attributeNames() {
return lastName(attribute1) + (attribute2 != null ? "/" + lastName(attribute2) : "");
}
@Override
String description() {
return attributeNames();
}
private String lastName(ItemPath path) {
QName lastName = path.lastName();
return lastName != null ? lastName.getLocalPart() : "?";
}
void runOnce(OperationResult result) {
PrismObjectDefinition<?> userPrismDefinition = prismContext.getSchemaRegistry().findObjectDefinitionByCompileTimeClass(UserType.class);
String prefix = lastName(attribute1);
String dataWritten = "[" + prefix + ":" + counter.get() + "]";
PrismPropertyDefinition<?> propertyDefinition1 = userPrismDefinition.findPropertyDefinition(attribute1);
if (propertyDefinition1 == null) {
throw new IllegalArgumentException("No definition for " + attribute1 + " in " + userPrismDefinition);
}
PropertyDelta delta1 = prismContext.deltaFactory().property().create(attribute1, propertyDefinition1);
//noinspection unchecked
delta1.setRealValuesToReplace(poly ? new PolyString(dataWritten) : dataWritten);
List<ItemDelta> deltas = new ArrayList<>();
deltas.add(delta1);
ItemDefinition propertyDefinition2;
if (attribute2 != null) {
propertyDefinition2 = userPrismDefinition.findItemDefinition(attribute2);
if (propertyDefinition2 == null) {
throw new IllegalArgumentException("No definition for " + attribute2 + " in " + userPrismDefinition);
}
ItemDelta delta2;
if (propertyDefinition2 instanceof PrismContainerDefinition) {
//noinspection unchecked
delta2 = prismContext.deltaFactory().container().create(attribute2, (PrismContainerDefinition) propertyDefinition2);
} else {
//noinspection unchecked
delta2 = prismContext.deltaFactory().property().create(attribute2, (PrismPropertyDefinition) propertyDefinition2);
}
if (ConstructionType.COMPLEX_TYPE.equals(propertyDefinition2.getTypeName())) {
ConstructionType act = new ConstructionType();
act.setDescription(dataWritten);
//noinspection unchecked
delta2.setValueToReplace(act.asPrismContainerValue());
} else {
//noinspection unchecked
delta2.setValueToReplace(prismContext.itemFactory().createPropertyValue(dataWritten));
}
deltas.add(delta2);
} else {
propertyDefinition2 = null;
}
try {
repositoryService.modifyObject(UserType.class, oid, deltas, result);
result.computeStatus();
if (result.isError()) {
logger.error("Error found in operation result:\n{}", result.debugDump());
throw new IllegalStateException("Error found in operation result");
}
} catch (Exception e) {
String msg = "modifyObject failed while modifying attribute(s) " + attributeNames() + " to value " + dataWritten;
throw new RuntimeException(msg, e);
}
if (checkValue) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// ignore
}
PrismObject<UserType> user;
try {
user = repositoryService.getObject(UserType.class, oid, null, result);
} catch (Exception e) {
String msg = "getObject failed while getting attribute(s) " + attributeNames();
throw new RuntimeException(msg, e);
}
// check the attribute
String dataRead;
if (poly) {
dataRead = user.findProperty(attribute1).getRealValue(PolyString.class).getOrig();
} else {
dataRead = user.findProperty(attribute1).getRealValue(String.class);
}
if (!dataWritten.equals(dataRead)) {
threadResult = new RuntimeException("Data read back (" + dataRead + ") does not match the data written (" + dataWritten + ") on attribute " + attribute1);
logger.error("compare failed", threadResult);
stop = true;
return;
}
if (attribute2 != null) {
if (ConstructionType.COMPLEX_TYPE.equals(propertyDefinition2.getTypeName())) {
dataRead = ((ConstructionType) user.findContainer(attribute2).getValue().getValue()).getDescription();
} else {
dataRead = user.findProperty(attribute2).getRealValue(String.class);
}
if (!dataWritten.equals(dataRead)) {
threadResult = new RuntimeException("Data read back (" + dataRead + ") does not match the data written (" + dataWritten + ") on attribute " + attribute2);
logger.error("compare failed", threadResult);
stop = true;
return;
}
}
String currentVersion = user.getVersion();
String versionError = SqlRepoTestUtil.checkVersionProgress(lastVersion, currentVersion);
if (versionError != null) {
threadResult = new RuntimeException(versionError);
logger.error(versionError);
stop = true;
return;
}
lastVersion = currentVersion;
}
}
}
abstract class AddObjectsThread<T extends ObjectType> extends WorkerThread {
String description;
AddObjectsThread(int id, String description, int limit) {
super(id, limit);
this.description = description;
this.setName("Executor: " + description);
}
@Override
String description() {
return description;
}
void runOnce(OperationResult result) throws Exception {
repositoryService.addObject(getObjectToAdd(), null, result);
}
protected abstract PrismObject<T> getObjectToAdd();
}
abstract class DeleteObjectsThread<T extends ObjectType> extends WorkerThread {
private Class<T> objectClass;
String description;
DeleteObjectsThread(int id, Class<T> objectClass, String description) {
super(id);
this.objectClass = objectClass;
this.description = description;
this.setName("Executor: " + description);
}
@Override
String description() {
return description;
}
void runOnce(OperationResult result) throws Exception {
String oidToDelete = getOidToDelete();
if (oidToDelete != null) {
repositoryService.deleteObject(objectClass, oidToDelete, result);
} else {
stop = true;
}
}
protected abstract String getOidToDelete();
}
abstract class DeltaExecutionThread extends WorkerThread {
String description;
DeltaExecutionThread(int id, Class<? extends ObjectType> objectClass, String oid, String description) {
super(id);
setObject(objectClass, oid);
this.description = description;
this.setName("Executor: " + description);
}
@Override
String description() {
return description;
}
abstract Collection<ItemDelta<?, ?>> getItemDeltas() throws Exception;
void runOnce(OperationResult result) throws Exception {
repositoryService.modifyObject(objectClass, oid, getItemDeltas(), result);
}
}
@Test
public void test010SearchIterative() throws Exception {
String name = "Test10";
final String newFullName = "new-full-name";
final File file = new File("src/test/resources/concurrency/user.xml");
PrismObject<UserType> user = prismContext.parseObject(file);
user.asObjectable().setName(new PolyStringType(name));
final OperationResult result = new OperationResult("Concurrency Test10");
String oid = repositoryService.addObject(user, null, result);
repositoryService.searchObjectsIterative(UserType.class,
prismContext.queryFor(UserType.class)
.item(UserType.F_NAME).eqPoly(name).matchingOrig().build(),
(object, parentResult) -> {
logger.info("Handling " + object + "...");
ObjectDelta delta = prismContext.deltaFactory().object()
.createModificationReplaceProperty(UserType.class, object.getOid(),
UserType.F_FULL_NAME, new PolyString(newFullName));
try {
//noinspection unchecked
repositoryService.modifyObject(UserType.class,
object.getOid(),
delta.getModifications(),
parentResult);
} catch (Exception e) {
throw new RuntimeException("Exception in handle method", e);
}
return true;
},
null, true, result);
PrismObject<UserType> reloaded = repositoryService.getObject(UserType.class, oid, null, result);
assertEquals("Full name was not changed", newFullName, reloaded.asObjectable().getFullName().getOrig());
}
@Test
public void test100AddOperationExecution() throws Exception {
if (getConfiguration().isUsingH2()) {
return; // TODO
}
int THREADS = 8;
long DURATION = 30000L;
UserType user = new UserType(prismContext).name("jack");
OperationResult result = new OperationResult("test100AddOperationExecution");
String oid = repositoryService.addObject(user.asPrismObject(), null, result);
displayValue("object added", oid);
logger.info("Starting worker threads");
List<DeltaExecutionThread> threads = new ArrayList<>();
for (int i = 0; i < THREADS; i++) {
final int threadIndex = i;
DeltaExecutionThread thread = new DeltaExecutionThread(i, UserType.class, oid, "operationExecution adder #" + i) {
@Override
Collection<ItemDelta<?, ?>> getItemDeltas() throws Exception {
return prismContext.deltaFor(UserType.class)
.item(UserType.F_OPERATION_EXECUTION).add(
new OperationExecutionType(prismContext)
.channel(threadIndex + ":" + counter)
.timestamp(XmlTypeConverter.createXMLGregorianCalendar(new Date())))
.asItemDeltas();
}
};
thread.start();
threads.add(thread);
}
waitForThreads(threads, DURATION);
}
@Test
public void test110AddAssignments() throws Exception {
if (getConfiguration().isUsingH2()) {
// Because of:
// Caused by: javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [com.evolveum.midpoint.repo.sql.data.common.container.RAssignment#RContainerId{44c3e25d-e790-4142-958d-ff7ff3ff3a9f, 62}]
// at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:118)
// at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
// at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164)
// at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1443)
// at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:493)
// at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3207)
// at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2413)
// at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:473)
// at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:156)
// at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
// at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)
// at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)
return; // TODO
}
int THREADS = 8;
long DURATION = 30000L;
UserType user = new UserType(prismContext).name("alice");
OperationResult result = new OperationResult("test110AddAssignments");
String oid = repositoryService.addObject(user.asPrismObject(), null, result);
displayValue("object added", oid);
logger.info("Starting worker threads");
List<DeltaExecutionThread> threads = new ArrayList<>();
for (int i = 0; i < THREADS; i++) {
final int threadIndex = i;
DeltaExecutionThread thread = new DeltaExecutionThread(i, UserType.class, oid, "assignment adder #" + i) {
@Override
Collection<ItemDelta<?, ?>> getItemDeltas() throws Exception {
return prismContext.deltaFor(UserType.class)
.item(UserType.F_ASSIGNMENT).add(
new AssignmentType(prismContext)
.targetRef("0000-" + threadIndex + "-" + counter, OrgType.COMPLEX_TYPE))
.asItemDeltas();
}
};
thread.start();
threads.add(thread);
}
waitForThreads(threads, DURATION);
PrismObject<UserType> userAfter = repositoryService.getObject(UserType.class, oid, null, result);
displayValue("user after", userAfter);
}
@Test
public void test120AddApproverRef() throws Exception {
int THREADS = 4;
long DURATION = 30_000L;
final String DELEGATED_REF_FORMAT = "oid-%d-%s";
RoleType role = new RoleType(prismContext).name("judge");
OperationResult result = new OperationResult("test120AddApproverRef");
String oid = repositoryService.addObject(role.asPrismObject(), null, result);
displayValue("object added", oid);
logger.info("Starting worker threads");
List<DeltaExecutionThread> threads = new ArrayList<>();
for (int i = 0; i < THREADS; i++) {
final int threadIndex = i;
DeltaExecutionThread thread = new DeltaExecutionThread(i, RoleType.class, oid, "approverRef adder #" + i) {
@Override
Collection<ItemDelta<?, ?>> getItemDeltas() throws Exception {
return prismContext.deltaFor(RoleType.class)
.item(RoleType.F_DELEGATED_REF).add(
ObjectTypeUtil.createObjectRef(String.format(DELEGATED_REF_FORMAT, threadIndex, counter), ObjectTypes.USER))
.asItemDeltas();
}
};
thread.start();
threads.add(thread);
}
waitForThreads(threads, DURATION);
PrismObject<RoleType> roleAfter = repositoryService.getObject(RoleType.class, oid, null, result);
displayValue("role after", roleAfter);
int totalExecutions = threads.stream().mapToInt(t -> t.counter.get()).sum();
int totalApprovers = roleAfter.asObjectable().getDelegatedRef().size();
System.out.println("Total executions: " + totalExecutions);
System.out.println("Approvers: " + totalApprovers);
List<String> failures = new ArrayList<>();
for (DeltaExecutionThread thread : threads) {
for (int i = 1; i <= thread.counter.get(); i++) {
String expected = String.format(DELEGATED_REF_FORMAT, thread.id, i);
List<String> matchingOids = roleAfter.asObjectable().getDelegatedRef().stream()
.map(ObjectReferenceType::getOid)
.filter(refOid -> refOid.equals(expected))
.collect(Collectors.toList());
if (matchingOids.size() != 1) {
failures.add("Wrong # of occurrences of " + expected + ": " + matchingOids);
}
}
}
System.out.println("Failures:");
failures.forEach(line -> System.out.println(" - " + line));
assertEquals("Wrong # of approvers", totalExecutions, totalApprovers);
assertEquals("Failures are there", 0, failures.size());
}
@Test
public void test130AddDeleteObjects() throws Exception {
int ADD_THREADS = 4;
int DELETE_THREADS = 4;
int OBJECTS_PER_THREAD = 100;
long TIMEOUT = 30000L;
OperationResult result = new OperationResult("test130DeleteObjects");
SearchResultList<PrismObject<UserType>> users = repositoryService
.searchObjects(UserType.class, null, null, result);
for (PrismObject<UserType> user : users) {
repositoryService.deleteObject(UserType.class, user.getOid(), result);
}
assertEquals("Wrong # of users at the beginning", 0,
repositoryService.countObjects(UserType.class, null, null, result));
logger.info("Starting ADD worker threads");
repositoryService.getPerformanceMonitor().clearGlobalPerformanceInformation();
List<AddObjectsThread<UserType>> addThreads = new ArrayList<>();
for (int i = 0; i < ADD_THREADS; i++) {
int threadIndex = i;
AddObjectsThread<UserType> thread = new AddObjectsThread<UserType>(i, "adder #" + i, OBJECTS_PER_THREAD) {
@Override
protected PrismObject<UserType> getObjectToAdd() {
return new UserType(prismContext).name(String.format("user-%d-%06d", threadIndex, counter.intValue())).asPrismObject();
}
};
thread.start();
addThreads.add(thread);
}
waitForThreadsFinish(addThreads, TIMEOUT);
System.out.println("Add performance information:\n" + repositoryService.getPerformanceMonitor().getGlobalPerformanceInformation().debugDump());
SearchResultList<PrismObject<UserType>> objectsCreated = repositoryService
.searchObjects(UserType.class, null, null, result);
assertEquals("Wrong # of users after creation", ADD_THREADS * OBJECTS_PER_THREAD, objectsCreated.size());
logger.info("Starting DELETE worker threads");
repositoryService.getPerformanceMonitor().clearGlobalPerformanceInformation();
AtomicInteger objectsPointer = new AtomicInteger(0);
List<DeleteObjectsThread<UserType>> deleteThreads = new ArrayList<>();
for (int i = 0; i < DELETE_THREADS; i++) {
DeleteObjectsThread<UserType> thread = new DeleteObjectsThread<UserType>(i, UserType.class, "deleter #" + i) {
@Override
protected String getOidToDelete() {
int pointer = objectsPointer.getAndIncrement();
if (pointer < objectsCreated.size()) {
return objectsCreated.get(pointer).getOid();
} else {
return null;
}
}
};
thread.start();
deleteThreads.add(thread);
}
waitForThreadsFinish(deleteThreads, TIMEOUT);
System.out.println("Delete performance information:\n" + repositoryService.getPerformanceMonitor().getGlobalPerformanceInformation().debugDump());
assertEquals("Wrong # of users after deletion", 0,
repositoryService.countObjects(UserType.class, null, null, result));
}
private void waitForThreadsFinish(List<? extends WorkerThread> threads, long timeout) throws InterruptedException {
logger.info("*** Waiting until finish, at most {} ms ***", timeout);
long startTime = System.currentTimeMillis();
main:
while (System.currentTimeMillis() - startTime < timeout) {
for (WorkerThread thread : threads) {
if (thread.isAlive()) {
Thread.sleep(100);
continue main;
} else if (thread.threadResult != null) {
throw new AssertionError("Thread " + thread + " failed with " + thread.threadResult, thread.threadResult);
}
}
return;
}
List<WorkerThread> alive = threads.stream().filter(Thread::isAlive).collect(Collectors.toList());
assertTrue("These threads did not finish in " + timeout + " millis: " + alive, alive.isEmpty());
}
private void waitForThreads(List<? extends WorkerThread> threads, long duration) throws InterruptedException {
logger.info("*** Waiting {} ms ***", duration);
long startTime = System.currentTimeMillis();
main:
while (System.currentTimeMillis() - startTime < duration) {
for (WorkerThread thread : threads) {
if (!thread.isAlive()) {
logger.error("At least one of threads died prematurely, finishing waiting.");
break main;
}
}
Thread.sleep(100);
}
for (WorkerThread thread : threads) {
thread.stop = true; // stop the threads
System.out.println("Thread " + thread.id + " has done " + thread.counter.get() + " iterations");
logger.info("Thread " + thread.id + " has done " + thread.counter.get() + " iterations");
}
long start = System.currentTimeMillis();
boolean anyAlive = true;
while (anyAlive && System.currentTimeMillis() - start < WAIT_FOR_THREAD_NATURAL_STOP_TIME) {
anyAlive = threads.stream().anyMatch(Thread::isAlive);
Thread.sleep(100);
}
List<String> alive = threads.stream().filter(Thread::isAlive).map(Thread::getName).collect(Collectors.toList());
assertTrue("Some threads had not stopped in given time: " + alive, alive.isEmpty());
for (WorkerThread thread : threads) {
logger.info("Modifier thread " + thread.id + " finished with an exception: ", thread.threadResult);
}
for (WorkerThread thread : threads) {
if (thread.threadResult != null) {
throw new AssertionError("Modifier thread " + thread.id + " finished with an exception: " + thread.threadResult, thread.threadResult);
}
}
}
private SqlRepositoryConfiguration getConfiguration() {
return ((SqlRepositoryServiceImpl) repositoryService).getConfiguration();
}
}