Search in sources :

Example 1 with TaskActivityStateType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.TaskActivityStateType 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 TaskActivityStateType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.TaskActivityStateType 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)

Example 3 with TaskActivityStateType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.TaskActivityStateType in project midpoint by Evolveum.

the class ActivityTreePurger method purgeInSubtasks.

/**
 * Purges activity states in the subtasks. Deletes those subtasks if possible.
 *
 * @return true if there are no subtasks left
 */
private boolean purgeInSubtasks(Task parent, OperationResult result) throws CommonException {
    List<? extends Task> subtasks = parent.listSubtasks(true, result);
    for (Iterator<? extends Task> iterator = subtasks.iterator(); iterator.hasNext(); ) {
        Task subtask = iterator.next();
        TaskActivityStateType taskActivityState = subtask.getActivitiesStateOrClone();
        if (taskActivityState == null) {
            LOGGER.error("Non-activity related subtask {} of {}. Please resolve manually.", subtask, parent);
            continue;
        }
        boolean canDeleteSubtask = purgeInTasksRecursively(ActivityPath.fromBean(taskActivityState.getLocalRoot()), subtask, result);
        if (canDeleteSubtask) {
            LOGGER.info("Deleting obsolete subtask {}", subtask);
            beans.taskManager.deleteTask(subtask.getOid(), result);
            iterator.remove();
        }
    }
    return subtasks.isEmpty();
}
Also used : RunningTask(com.evolveum.midpoint.task.api.RunningTask) Task(com.evolveum.midpoint.task.api.Task) TaskActivityStateType(com.evolveum.midpoint.xml.ns._public.common.common_3.TaskActivityStateType)

Example 4 with TaskActivityStateType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.TaskActivityStateType in project midpoint by Evolveum.

the class NodeDownCleaner method releaseAllBucketsOfWorker.

/**
 * We don't want this task to hold its buckets forever. So let's release them.
 *
 * (There's a slight risk if the task is restarted in the meanwhile, i.e. before learning that node is down and this
 * moment. But let us live with this risk for now.)
 */
private void releaseAllBucketsOfWorker(OperationResult result) throws ObjectNotFoundException, SchemaException {
    TaskActivityStateType state = task.getActivitiesStateOrClone();
    if (state == null || state.getTaskRole() != TaskRoleType.WORKER) {
        return;
    }
    if (parent == null) {
        LOGGER.warn("No parent for worker {} (root {}) ?", task, root);
        return;
    }
    ActivityPath activityPath = ActivityPath.fromBean(Objects.requireNonNull(state.getLocalRoot(), "No local root in " + task));
    LOGGER.info("Returning all buckets from {} (coordinator {})", task, parent);
    beans.bucketingManager.releaseAllWorkBucketsFromWorker(parent.getOid(), task.getOid(), activityPath, null, result);
}
Also used : ActivityPath(com.evolveum.midpoint.schema.util.task.ActivityPath) TaskActivityStateType(com.evolveum.midpoint.xml.ns._public.common.common_3.TaskActivityStateType)

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