Search in sources :

Example 1 with ModificationsSupplier

use of com.evolveum.midpoint.repo.api.RepositoryService.ModificationsSupplier in project midpoint by Evolveum.

the class RepoConcurrencyTest method test150WorkBucketsReplace.

/**
 * Here we test concurrent work bucket delegation using
 * {@link RepositoryService#modifyObjectDynamically(Class, String, Collection, ModificationsSupplier, RepoModifyOptions, OperationResult)}
 * method.
 */
@Test
public void test150WorkBucketsReplace() throws Exception {
    int THREADS = 8;
    long DURATION = 30_000L;
    TaskType task = new TaskType(prismContext).name("test150").beginActivityState().beginActivity().beginBucketing().bucketsProcessingRole(BucketsProcessingRoleType.COORDINATOR).<ActivityStateType>end().<TaskActivityStateType>end().end();
    OperationResult result = new OperationResult("test150WorkBucketsReplace");
    String oid = plainRepositoryService.addObject(task.asPrismObject(), null, result);
    displayValue("object added", oid);
    logger.info("Starting worker threads");
    List<WorkerThread> threads = new ArrayList<>();
    for (int i = 0; i < THREADS; i++) {
        final int threadIndex = i;
        WorkerThread thread = new WorkerThread(i) {

            @Override
            void runOnce(OperationResult result) throws Exception {
                ModificationsSupplier<TaskType> modificationSupplier = task -> prismContext.deltaFor(TaskType.class).item(TaskType.F_ACTIVITY_STATE, F_ACTIVITY, F_BUCKETING, F_BUCKET).replaceRealValues(getBucketsToReplace(task)).asItemDeltas();
                plainRepositoryService.modifyObjectDynamically(TaskType.class, oid, null, modificationSupplier, null, result);
            }

            private List<WorkBucketType> getBucketsToReplace(TaskType task) {
                List<WorkBucketType> currentBuckets = getCurrentBuckets(task);
                List<WorkBucketType> newBuckets = CloneUtil.cloneCollectionMembers(currentBuckets);
                newBuckets.add(getNextBucket(currentBuckets));
                return newBuckets;
            }

            private WorkBucketType getNextBucket(List<WorkBucketType> currentBuckets) {
                return new WorkBucketType(prismContext).sequentialNumber(getLastBucketNumber(currentBuckets) + 1).state(WorkBucketStateType.DELEGATED).workerRef(String.valueOf(threadIndex), TaskType.COMPLEX_TYPE);
            }

            private List<WorkBucketType> getCurrentBuckets(TaskType task) {
                return task.getActivityState().getActivity().getBucketing().getBucket();
            }

            private int getLastBucketNumber(List<WorkBucketType> buckets) {
                return buckets.stream().mapToInt(WorkBucketType::getSequentialNumber).max().orElse(0);
            }

            @Override
            String description() {
                return "Bucket computer thread #" + threadIndex;
            }
        };
        thread.start();
        threads.add(thread);
    }
    waitForThreads(threads, DURATION);
    PrismObject<TaskType> taskAfter = plainRepositoryService.getObject(TaskType.class, oid, null, result);
    displayValue("task after", taskAfter);
    assertCorrectBucketSequence(taskAfter.asObjectable().getActivityState().getActivity().getBucketing().getBucket());
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) com.evolveum.midpoint.xml.ns._public.common.common_3(com.evolveum.midpoint.xml.ns._public.common.common_3) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.testng.annotations.Test) F_BUCKET(com.evolveum.midpoint.xml.ns._public.common.common_3.ActivityBucketingStateType.F_BUCKET) DebugUtil(com.evolveum.midpoint.util.DebugUtil) ModificationsSupplier(com.evolveum.midpoint.repo.api.RepositoryService.ModificationsSupplier) AssertJUnit.assertTrue(org.testng.AssertJUnit.assertTrue) ArrayList(java.util.ArrayList) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) SupportedDatabase(com.evolveum.midpoint.repo.sqlbase.SupportedDatabase) ObjectTypeUtil(com.evolveum.midpoint.schema.util.ObjectTypeUtil) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JdbcRepositoryConfiguration(com.evolveum.midpoint.repo.sqlbase.JdbcRepositoryConfiguration) SqlRepoTestUtil(com.evolveum.midpoint.repo.sql.testing.SqlRepoTestUtil) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService) com.evolveum.midpoint.prism(com.evolveum.midpoint.prism) CloneUtil(com.evolveum.midpoint.prism.util.CloneUtil) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) XmlTypeConverter(com.evolveum.midpoint.prism.xml.XmlTypeConverter) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) Collection(java.util.Collection) F_BUCKETING(com.evolveum.midpoint.xml.ns._public.common.common_3.ActivityStateType.F_BUCKETING) Collectors(java.util.stream.Collectors) File(java.io.File) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) LoggingUtils(com.evolveum.midpoint.util.logging.LoggingUtils) List(java.util.List) RepoModifyOptions(com.evolveum.midpoint.repo.api.RepoModifyOptions) SearchResultList(com.evolveum.midpoint.schema.SearchResultList) PolyStringType(com.evolveum.prism.xml.ns._public.types_3.PolyStringType) ContextConfiguration(org.springframework.test.context.ContextConfiguration) PropertyDelta(com.evolveum.midpoint.prism.delta.PropertyDelta) ObjectTypes(com.evolveum.midpoint.schema.constants.ObjectTypes) F_ACTIVITY(com.evolveum.midpoint.xml.ns._public.common.common_3.ActivityStateType.F_ACTIVITY) QName(javax.xml.namespace.QName) AssertJUnit.assertEquals(org.testng.AssertJUnit.assertEquals) ArrayList(java.util.ArrayList) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) ArrayList(java.util.ArrayList) List(java.util.List) SearchResultList(com.evolveum.midpoint.schema.SearchResultList) Test(org.testng.annotations.Test)

Example 2 with ModificationsSupplier

use of com.evolveum.midpoint.repo.api.RepositoryService.ModificationsSupplier in project midpoint by Evolveum.

the class RepoConcurrencyTest method test140WorkBucketsAdd.

/**
 * Here we test concurrent work bucket creation using
 * {@link RepositoryService#modifyObjectDynamically(Class, String, Collection, ModificationsSupplier, RepoModifyOptions, OperationResult)}
 * method.
 */
@Test
public void test140WorkBucketsAdd() throws Exception {
    int THREADS = 8;
    long DURATION = 30_000L;
    TaskType task = new TaskType(prismContext).name("test140").beginActivityState().beginActivity().beginBucketing().bucketsProcessingRole(BucketsProcessingRoleType.COORDINATOR).<ActivityStateType>end().<TaskActivityStateType>end().end();
    OperationResult result = new OperationResult("test140WorkBucketsAdd");
    String oid = plainRepositoryService.addObject(task.asPrismObject(), null, result);
    displayValue("object added", oid);
    logger.info("Starting worker threads");
    List<WorkerThread> threads = new ArrayList<>();
    for (int i = 0; i < THREADS; i++) {
        final int threadIndex = i;
        WorkerThread thread = new WorkerThread(i) {

            @Override
            void runOnce(OperationResult result) throws Exception {
                ModificationsSupplier<TaskType> modificationSupplier = task -> prismContext.deltaFor(TaskType.class).item(TaskType.F_ACTIVITY_STATE, F_ACTIVITY, F_BUCKETING, F_BUCKET).add(getNextBucket(task)).asItemDeltas();
                plainRepositoryService.modifyObjectDynamically(TaskType.class, oid, null, modificationSupplier, null, result);
            }

            private WorkBucketType getNextBucket(TaskType task) {
                int lastBucketNumber = task.getActivityState() != null ? getLastBucketNumber(task.getActivityState().getActivity().getBucketing().getBucket()) : 0;
                return new WorkBucketType(prismContext).sequentialNumber(lastBucketNumber + 1).state(WorkBucketStateType.DELEGATED).workerRef(String.valueOf(threadIndex), TaskType.COMPLEX_TYPE);
            }

            private int getLastBucketNumber(List<WorkBucketType> buckets) {
                return buckets.stream().mapToInt(WorkBucketType::getSequentialNumber).max().orElse(0);
            }

            @Override
            String description() {
                return "Bucket computer thread #" + threadIndex;
            }
        };
        thread.start();
        threads.add(thread);
    }
    waitForThreads(threads, DURATION);
    PrismObject<TaskType> taskAfter = plainRepositoryService.getObject(TaskType.class, oid, null, result);
    displayValue("task after", taskAfter);
    assertCorrectBucketSequence(taskAfter.asObjectable().getActivityState().getActivity().getBucketing().getBucket());
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) com.evolveum.midpoint.xml.ns._public.common.common_3(com.evolveum.midpoint.xml.ns._public.common.common_3) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.testng.annotations.Test) F_BUCKET(com.evolveum.midpoint.xml.ns._public.common.common_3.ActivityBucketingStateType.F_BUCKET) DebugUtil(com.evolveum.midpoint.util.DebugUtil) ModificationsSupplier(com.evolveum.midpoint.repo.api.RepositoryService.ModificationsSupplier) AssertJUnit.assertTrue(org.testng.AssertJUnit.assertTrue) ArrayList(java.util.ArrayList) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) SupportedDatabase(com.evolveum.midpoint.repo.sqlbase.SupportedDatabase) ObjectTypeUtil(com.evolveum.midpoint.schema.util.ObjectTypeUtil) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JdbcRepositoryConfiguration(com.evolveum.midpoint.repo.sqlbase.JdbcRepositoryConfiguration) SqlRepoTestUtil(com.evolveum.midpoint.repo.sql.testing.SqlRepoTestUtil) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService) com.evolveum.midpoint.prism(com.evolveum.midpoint.prism) CloneUtil(com.evolveum.midpoint.prism.util.CloneUtil) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) XmlTypeConverter(com.evolveum.midpoint.prism.xml.XmlTypeConverter) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) Collection(java.util.Collection) F_BUCKETING(com.evolveum.midpoint.xml.ns._public.common.common_3.ActivityStateType.F_BUCKETING) Collectors(java.util.stream.Collectors) File(java.io.File) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) LoggingUtils(com.evolveum.midpoint.util.logging.LoggingUtils) List(java.util.List) RepoModifyOptions(com.evolveum.midpoint.repo.api.RepoModifyOptions) SearchResultList(com.evolveum.midpoint.schema.SearchResultList) PolyStringType(com.evolveum.prism.xml.ns._public.types_3.PolyStringType) ContextConfiguration(org.springframework.test.context.ContextConfiguration) PropertyDelta(com.evolveum.midpoint.prism.delta.PropertyDelta) ObjectTypes(com.evolveum.midpoint.schema.constants.ObjectTypes) F_ACTIVITY(com.evolveum.midpoint.xml.ns._public.common.common_3.ActivityStateType.F_ACTIVITY) QName(javax.xml.namespace.QName) AssertJUnit.assertEquals(org.testng.AssertJUnit.assertEquals) ArrayList(java.util.ArrayList) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) ArrayList(java.util.ArrayList) List(java.util.List) SearchResultList(com.evolveum.midpoint.schema.SearchResultList) Test(org.testng.annotations.Test)

Aggregations

com.evolveum.midpoint.prism (com.evolveum.midpoint.prism)2 ItemDelta (com.evolveum.midpoint.prism.delta.ItemDelta)2 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)2 PropertyDelta (com.evolveum.midpoint.prism.delta.PropertyDelta)2 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)2 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)2 CloneUtil (com.evolveum.midpoint.prism.util.CloneUtil)2 XmlTypeConverter (com.evolveum.midpoint.prism.xml.XmlTypeConverter)2 RepoModifyOptions (com.evolveum.midpoint.repo.api.RepoModifyOptions)2 RepositoryService (com.evolveum.midpoint.repo.api.RepositoryService)2 ModificationsSupplier (com.evolveum.midpoint.repo.api.RepositoryService.ModificationsSupplier)2 SqlRepoTestUtil (com.evolveum.midpoint.repo.sql.testing.SqlRepoTestUtil)2 JdbcRepositoryConfiguration (com.evolveum.midpoint.repo.sqlbase.JdbcRepositoryConfiguration)2 SupportedDatabase (com.evolveum.midpoint.repo.sqlbase.SupportedDatabase)2 SearchResultList (com.evolveum.midpoint.schema.SearchResultList)2 ObjectTypes (com.evolveum.midpoint.schema.constants.ObjectTypes)2 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)2 ObjectTypeUtil (com.evolveum.midpoint.schema.util.ObjectTypeUtil)2 DebugUtil (com.evolveum.midpoint.util.DebugUtil)2 LoggingUtils (com.evolveum.midpoint.util.logging.LoggingUtils)2