Search in sources :

Example 1 with PropagationTaskExecStatus

use of org.apache.syncope.common.lib.types.PropagationTaskExecStatus in project syncope by apache.

the class PriorityPropagationTaskExecutor method doExecute.

@Override
protected void doExecute(final Collection<PropagationTaskTO> tasks, final PropagationReporter reporter, final boolean nullPriorityAsync) {
    Map<PropagationTaskTO, ExternalResource> taskToResource = tasks.stream().collect(Collectors.toMap(Function.identity(), task -> resourceDAO.find(task.getResource())));
    List<PropagationTaskTO> prioritizedTasks = tasks.stream().filter(task -> taskToResource.get(task).getPropagationPriority() != null).collect(Collectors.toList());
    Collections.sort(prioritizedTasks, new PriorityComparator(taskToResource));
    LOG.debug("Propagation tasks sorted by priority, for serial execution: {}", prioritizedTasks);
    Collection<PropagationTaskTO> concurrentTasks = tasks.stream().filter(task -> !prioritizedTasks.contains(task)).collect(Collectors.toSet());
    LOG.debug("Propagation tasks for concurrent execution: {}", concurrentTasks);
    // first process priority resources sequentially and fail as soon as any propagation failure is reported
    prioritizedTasks.forEach(task -> {
        TaskExec execution = null;
        PropagationTaskExecStatus execStatus;
        try {
            execution = newPropagationTaskCallable(task, reporter).call();
            execStatus = PropagationTaskExecStatus.valueOf(execution.getStatus());
        } catch (Exception e) {
            LOG.error("Unexpected exception", e);
            execStatus = PropagationTaskExecStatus.FAILURE;
        }
        if (execStatus != PropagationTaskExecStatus.SUCCESS) {
            throw new PropagationException(task.getResource(), execution == null ? null : execution.getMessage());
        }
    });
    // then process non-priority resources concurrently...
    CompletionService<TaskExec> completionService = new ExecutorCompletionService<>(executor);
    Map<PropagationTaskTO, Future<TaskExec>> nullPriority = new HashMap<>(concurrentTasks.size());
    concurrentTasks.forEach(task -> {
        try {
            nullPriority.put(task, completionService.submit(newPropagationTaskCallable(task, reporter)));
        } catch (Exception e) {
            LOG.error("Unexpected exception", e);
        }
    });
    // ...waiting for all callables to complete, if async processing was not required
    if (!nullPriority.isEmpty()) {
        if (nullPriorityAsync) {
            nullPriority.forEach((task, exec) -> {
                reporter.onSuccessOrNonPriorityResourceFailures(task, PropagationTaskExecStatus.CREATED, null, null, null);
            });
        } else {
            final Set<Future<TaskExec>> nullPriorityFutures = new HashSet<>(nullPriority.values());
            try {
                executor.submit(() -> {
                    while (!nullPriorityFutures.isEmpty()) {
                        try {
                            nullPriorityFutures.remove(completionService.take());
                        } catch (Exception e) {
                            LOG.error("Unexpected exception", e);
                        }
                    }
                }).get(60, TimeUnit.SECONDS);
            } catch (Exception e) {
                LOG.error("Unexpected exception", e);
            } finally {
                nullPriorityFutures.forEach(future -> {
                    future.cancel(true);
                });
                nullPriorityFutures.clear();
                nullPriority.clear();
            }
        }
    }
}
Also used : PropagationTaskExecStatus(org.apache.syncope.common.lib.types.PropagationTaskExecStatus) HashMap(java.util.HashMap) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) Function(java.util.function.Function) PropagationTaskCallable(org.apache.syncope.core.provisioning.api.propagation.PropagationTaskCallable) CompletionService(java.util.concurrent.CompletionService) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) PropagationTaskTO(org.apache.syncope.common.lib.to.PropagationTaskTO) Map(java.util.Map) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) Collection(java.util.Collection) Resource(javax.annotation.Resource) PropagationException(org.apache.syncope.core.provisioning.api.propagation.PropagationException) Set(java.util.Set) PropagationReporter(org.apache.syncope.core.provisioning.api.propagation.PropagationReporter) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) TimeUnit(java.util.concurrent.TimeUnit) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) List(java.util.List) Comparator(java.util.Comparator) ApplicationContextProvider(org.apache.syncope.core.spring.ApplicationContextProvider) Collections(java.util.Collections) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) PropagationTaskTO(org.apache.syncope.common.lib.to.PropagationTaskTO) HashMap(java.util.HashMap) PropagationTaskExecStatus(org.apache.syncope.common.lib.types.PropagationTaskExecStatus) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) PropagationException(org.apache.syncope.core.provisioning.api.propagation.PropagationException) PropagationException(org.apache.syncope.core.provisioning.api.propagation.PropagationException) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) Future(java.util.concurrent.Future) HashSet(java.util.HashSet)

Aggregations

Serializable (java.io.Serializable)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 CompletionService (java.util.concurrent.CompletionService)1 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Resource (javax.annotation.Resource)1 PropagationTaskTO (org.apache.syncope.common.lib.to.PropagationTaskTO)1 PropagationTaskExecStatus (org.apache.syncope.common.lib.types.PropagationTaskExecStatus)1 ExternalResource (org.apache.syncope.core.persistence.api.entity.resource.ExternalResource)1 TaskExec (org.apache.syncope.core.persistence.api.entity.task.TaskExec)1