Search in sources :

Example 6 with ServiceNotExistsException

use of cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException in project perun by CESNET.

the class TaskSchedulerImpl method refetchTaskInformation.

private void refetchTaskInformation(Task task) throws FacilityNotExistsException, InternalErrorException, PrivilegeException, ServiceNotExistsException {
    // reread facility
    log.debug("Rereading facility and  exec service for task {}", task.getId());
    Facility dbFacility = perun.getFacilitiesManagerBl().getFacilityById(perunSession, task.getFacilityId());
    if (dbFacility == null) {
        throw new FacilityNotExistsException("No facility with id " + task.getFacilityId());
    }
    Boolean taskModified = false;
    if (!dbFacility.equals(task.getFacility())) {
        task.setFacility(dbFacility);
        taskModified = true;
    }
    // reread exec service (and service)
    ExecService dbExecService = generalServiceManager.getExecService(perunSession, task.getExecServiceId());
    if (dbExecService == null) {
        throw new ServiceNotExistsException("No exec service with id " + task.getExecServiceId());
    }
    if (!dbExecService.equals(task.getExecService())) {
        task.setExecService(dbExecService);
        taskModified = true;
    }
    if (taskModified) {
        log.debug("Task components have changed, updating task {}", task.getId());
        schedulingPool.setTaskStatus(task, task.getStatus());
    }
}
Also used : ServiceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException) ExecService(cz.metacentrum.perun.taskslib.model.ExecService) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) Facility(cz.metacentrum.perun.core.api.Facility)

Example 7 with ServiceNotExistsException

use of cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException in project perun by CESNET.

the class TaskSchedulerImpl method scheduleTask.

// TODO ensure dependant tasks with scope DESTINATION go to the same engine
@Override
public Boolean scheduleTask(Task task) {
    ExecService execService = task.getExecService();
    Facility facility = task.getFacility();
    Date time = new Date(System.currentTimeMillis());
    DispatcherQueue dispatcherQueue = null;
    if (task.getStatus().equals(TaskStatus.PROCESSING) && !task.isPropagationForced()) {
        log.debug("Task {} already processing, will not schedule again.", task.toString());
        return true;
    }
    log.debug("Scheduling TASK " + task.toString());
    try {
        dispatcherQueue = schedulingPool.getQueueForTask(task);
        log.debug("Task {} is assigned to queue {}", task.getId(), (dispatcherQueue == null) ? "null" : dispatcherQueue.getClientID());
    } catch (InternalErrorException e) {
        log.warn("Task {} is not assigned to any queue", task.getId());
    }
    // check if the engine is still registered
    if (dispatcherQueue != null && !dispatcherQueuePool.isThereDispatcherQueueForClient(dispatcherQueue.getClientID())) {
        dispatcherQueue = null;
    }
    if (dispatcherQueue == null) {
        // where should we send the task?
        dispatcherQueue = dispatcherQueuePool.getAvailableQueue();
        if (dispatcherQueue != null) {
            try {
                schedulingPool.setQueueForTask(task, dispatcherQueue);
            } catch (InternalErrorException e) {
                log.error("Could not set client queue for task {}: {}", task.getId(), e.getMessage());
                return true;
            }
            log.debug("Assigned new queue " + dispatcherQueue.getQueueName() + " to task " + task.getId());
        } else {
            // bad luck...
            log.error("Task " + task.toString() + " has no engine assigned and there are no engines registered...");
            return true;
        }
    }
    log.debug("Facility to be processed: " + facility.getId() + ", ExecService to be processed: " + execService.getId());
    Boolean abortTask = false;
    try {
        refetchTaskInformation(task);
        List<Service> assignedServices = perun.getServicesManagerBl().getAssignedServices(perunSession, task.getFacility());
        if (!assignedServices.contains(execService.getService())) {
            log.debug("Task {} has no longer service {} assigned, aborting.", task.getId(), execService.getId());
            abortTask = true;
        }
    } catch (FacilityNotExistsException e1) {
        log.debug("Facility {} for task {} no longer exists, aborting", facility.getId(), task.getId());
        abortTask = true;
    } catch (ServiceNotExistsException e1) {
        log.debug("Service {} for task {} no longer exists, aborting", execService.getId(), task.getId());
        abortTask = true;
    } catch (InternalErrorException e1) {
        log.error("Error checking facility or exec service for updates, task will not run now: {}", e1.getMessage());
        return true;
    } catch (PrivilegeException e1) {
        log.error("Error checking facility or exec service for updates, task will not run now: {}", e1.getMessage());
        return true;
    }
    // GEN tasks that we depend on...
    if (abortTask && execService.getExecServiceType() == ExecServiceType.GENERATE) {
        // GEN tasks may be aborted immediately
        abortTask(task);
        return false;
    }
    // do not perform further checks for task that is going to be aborted
    if (!abortTask) {
        log.debug("Is the execService ID:" + execService.getId() + " enabled globally?");
        if (execService.isEnabled()) {
            log.debug("   Yes, it is globally enabled.");
        } else {
            log.debug("   No, execService ID: " + execService.getId() + " is not enabled globally. Task will not run.");
            return true;
        }
        log.debug("   Is the execService ID: " + execService.getId() + " denied on facility ID:" + facility.getId() + "?");
        try {
            if (!denialsResolver.isExecServiceDeniedOnFacility(execService, facility)) {
                log.debug("   No, it is not.");
            } else {
                log.debug("   Yes, the execService ID: " + execService.getId() + " is denied on facility ID: " + facility.getId() + ". Task will not run.");
                return true;
            }
        } catch (InternalErrorException e) {
            log.error("Error getting disabled status for execService, task will not run now.");
            return true;
        }
    }
    List<ExecService> dependantServices = null;
    List<Pair<ExecService, DependencyScope>> dependencies = null;
    // If any of the ExecServices that depends on this one is running
    // PROCESSING
    // we will put the ExecService,Facility pair back to the pool.
    // #################################################################################
    log.debug("   Is there any execService that depends on [" + execService.getId() + "] in \"PROCESSING\" state?");
    dependantServices = dependenciesResolver.listDependantServices(execService);
    boolean proceed = true;
    for (ExecService dependantService : dependantServices) {
        Task dependantServiceTask = schedulingPool.getTask(dependantService, facility);
        if (dependantServiceTask != null) {
            if (dependantServiceTask.getStatus().equals(TaskStatus.PROCESSING)) {
                log.debug("   There is a service [" + dependantServiceTask.getId() + "] running that depends on this one [" + execService + "], so we put this to sleep...");
                // schedulingPool.addToPool(new Pair<ExecService,
                // Facility>(execService, facility));
                proceed = false;
                break;
            }
        // This is probably wrong, so commenting out:
        //   1) if there is some dispatcher queue active at the moment, dispatcherQueue is not null
        //   2) if there is no dispatcher queue available, no point in setting the same queue another task has
        //   3) the dependency should be handled the other way round, from dependant to dependent
        /*
				 * try {
				 *	if (dispatcherQueue == null &&
				 *			schedulingPool.getQueueForTask(dependantServiceTask) != null) {
				 *		schedulingPool.setQueueForTask(task, schedulingPool.getQueueForTask(dependantServiceTask));
				 *	}
				 * } catch (InternalErrorException e) {
				 *	log.debug("    Failed to set destination queue for task. This is weird, aborting.");
				 *	proceed = false;
				 *}
				 */
        }
    }
    if (proceed) {
        log.debug("   No, it is not. No dependent service is running, we can proceed.");
        // If it is an ExecService of type SEND, we have to check its
        // dependencies.
        // We can skip this for GENERATE type (it has no dependencies by
        // design).
        // ########################################################################
        log.debug("   Check whether the execService [" + execService.getId() + "] is of type SEND");
        if (execService.getExecServiceType().equals(ExecServiceType.SEND)) {
            log.debug("   Well, it is, so we have to check it's dependencies.");
            // We check the status of all the ExecServices this ExecService
            // depends on.
            //
            //
            // Current approach disregards any SEND/GENERATE differences.
            // Dependency on a GENERATE service is being treated as same as
            // any other SEND dependency
            // but for a small exception regarding ERROR and DONE states,
            // see below:
            //
            // If the dependency is in one of the following states, we do:
            // NONE Schedule it and wait (put this [ExecService,Facility]
            // pair back to the SchedulingPool for a while).
            // PROCESSING Wait
            // ERROR IF dependency is GENERATE THEN DO
            // Schedule it and wait (put this [ExecService,Facility] pair
            // back to the SchedulingPool for a while).
            // ELSE IF dependency is SEND THEN DO
            // End with ERROR. (no point in trying, something is probably
            // amiss on destination nodes...)
            // ELSE
            // throw new IllegalArgumentException
            // FI
            // DONE IF dependency is GENERATE THEN DO
            // Schedule it and wait (put this [ExecService,Facility] pair
            // back to the SchedulingPool for a while).
            //
            // It might look like we get an infinite loop where GENERATE
            // will be in DONE and then rescheduled again and again.
            // It is not so because PropagationMaintainer sets its state to
            // NONE as soon as the SEND, that depends on it,
            // enters either DONE or ERROR states (one of its finite
            // states).
            // ELSE IF dependency is SEND THEN DO
            // Proceed (Yes, no need to schedule this dependency, it is done
            // already and we don't care for how long it has been so at this
            // point.)
            // ELSE
            // throw new IllegalArgumentException
            // FI
            // :-)
            // #######################################################################################################
            proceed = true;
            dependencies = dependenciesResolver.listDependenciesAndScope(execService);
            log.debug("   We are about to loop over execService [" + execService.getId() + "] dependencies.");
            log.debug("   Number of dependencies:" + dependencies);
            DispatcherQueue dependencyQueue = null;
            for (Pair<ExecService, DependencyScope> dependencyPair : dependencies) {
                ExecService dependency = dependencyPair.getLeft();
                DependencyScope dependencyScope = dependencyPair.getRight();
                Task dependencyServiceTask = schedulingPool.getTask(dependency, facility);
                if (dependencyServiceTask == null) {
                    if (abortTask) {
                        log.info("   Task {} is going to be aborted, the dependency exec service {} will not be scheduled now.", task.getId(), dependency.getId());
                    } else {
                        // Dependency being NULL is equivalent to being in NONE
                        // state.
                        log.info("   Last Task [dependency:" + dependency.getId() + ", facility:" + facility.getId() + "] was NULL, we are gonna propagate.");
                        if (scheduleItAndWait(dependency, facility, execService, dispatcherQueue, time)) {
                        // task sucessfully scheduled, nothing to do
                        } else {
                        // TODO: task aborted - maybe set this one to error?
                        }
                    }
                    proceed = false;
                } else {
                    boolean wasDependencyServiceTaskForced = dependencyServiceTask.isPropagationForced();
                    dependencyServiceTask.setPropagationForced(task.isPropagationForced());
                    switch(dependencyServiceTask.getStatus()) {
                        case DONE:
                            switch(dependency.getExecServiceType()) {
                                case GENERATE:
                                    if (task.isSourceUpdated()) {
                                        // we need to reschedule the GEN task as the source data has changed
                                        log.debug("   Dependency ID " + dependency.getId() + " is in DONE and is going to be rescheduled as we need fresh data.");
                                        rescheduleTask(dependencyServiceTask, execService, dispatcherQueue);
                                        proceed = false;
                                    } else {
                                        log.debug("   Dependency ID " + dependency.getId() + " is in DONE and it is of type GENERATE, we can proceed.");
                                        // Nothing, we can proceed...
                                        try {
                                            dependencyQueue = schedulingPool.getQueueForTask(dependencyServiceTask);
                                        } catch (InternalErrorException e) {
                                            log.error("Could not get queue for task {}", dependencyServiceTask.getId());
                                        }
                                    }
                                    break;
                                case SEND:
                                    log.debug("   Dependency ID " + dependencyServiceTask.getId() + " is in DONE and it is of type SEND, we can proceed.");
                                    // Nothing, we can proceed...
                                    break;
                                default:
                                    throw new IllegalArgumentException("Unknown ExecService type. Expected GENERATE or SEND.");
                            }
                            break;
                        case ERROR:
                            switch(dependency.getExecServiceType()) {
                                case GENERATE:
                                    log.info("   Dependency ID " + dependencyServiceTask.getId() + " is in ERROR and it is of type GENERATE, we are gonna propagate.");
                                    // scheduleItAndWait(dependency, facility,
                                    // execService, dispatcherQueue);
                                    // try to run the generate task again
                                    rescheduleTask(dependencyServiceTask, execService, dispatcherQueue);
                                    proceed = false;
                                    break;
                                case SEND:
                                    log.info("   Dependency ID " + dependencyServiceTask.getId() + " is in ERROR and it is of type SEND, we are gonna end with ERROR.");
                                    proceed = false;
                                    // We end Task with error immediately.
                                    schedulingPool.setTaskStatus(task, TaskStatus.ERROR);
                                    // facility);
                                    break;
                                default:
                                    throw new IllegalArgumentException("Unknown ExecService type. Expected GENERATE or SEND.");
                            }
                            break;
                        case NONE:
                            log.info("   Last Task {} [dependency:" + dependency.getId() + ", facility:" + facility.getId() + "] was NONE, we are gonna propagate.", dependencyServiceTask.getId());
                            rescheduleTask(dependencyServiceTask, execService, dispatcherQueue);
                            proceed = false;
                            break;
                        case PLANNED:
                            log.info("   Dependency ID " + dependencyServiceTask.getId() + " is in PLANNED so we are gonna wait.");
                            // justWait(facility, execService);
                            if (dependencyScope.equals(DependencyScope.SERVICE)) {
                                proceed = false;
                            } else {
                                try {
                                    dependencyQueue = schedulingPool.getQueueForTask(dependencyServiceTask);
                                } catch (InternalErrorException e) {
                                    log.error("Could not get queue for task {}", dependencyServiceTask.getId());
                                }
                            }
                            break;
                        case PROCESSING:
                            log.info("   Dependency ID " + dependencyServiceTask.getId() + " is in PROCESSING so we are gonna wait.");
                            // justWait(facility, execService);
                            if (dependencyScope.equals(DependencyScope.SERVICE)) {
                                proceed = false;
                            } else {
                                try {
                                    dependencyQueue = schedulingPool.getQueueForTask(dependencyServiceTask);
                                } catch (InternalErrorException e) {
                                    log.error("Could not get queue for task {}", dependencyServiceTask.getId());
                                }
                            }
                            if (dependencyServiceTask.isPropagationForced() && !wasDependencyServiceTaskForced) {
                                // reschedule dependant only if originally was not forced !!!
                                rescheduleTask(dependencyServiceTask, execService, dispatcherQueue);
                            // XXX - should we proceed here?
                            }
                            break;
                        default:
                            throw new IllegalArgumentException("Unknown Task status. Expected DONE, ERROR, NONE, PLANNED or PROCESSING.");
                    }
                }
            }
            // #########################################
            if (proceed) {
                if (abortTask) {
                    // the SEND task is going to be aborted now
                    abortTask(task);
                    return false;
                } else {
                    if (dependencyQueue != null && dependencyQueue != dispatcherQueue) {
                        log.debug("Changing task {} destination queue to {} to match dependency task", task.getId(), dependencyQueue.getClientID());
                        try {
                            schedulingPool.setQueueForTask(task, dependencyQueue);
                        } catch (InternalErrorException e) {
                            log.error("Could not change task {} destination queue: {}", task.getId(), e.getMessage());
                        }
                    }
                    log.info("   SCHEDULING task [" + task.getId() + "], execService [" + execService.getId() + "] facility [" + facility.getId() + "] as PLANNED.");
                    task.setSchedule(time);
                    schedulingPool.setTaskStatus(task, TaskStatus.PLANNED);
                    sendToEngine(task);
                }
            // manipulateTasks(execService, facility, task);
            } else {
                if (abortTask) {
                    // the SEND task is going to be aborted now
                    abortTask(task);
                    return false;
                } else {
                    // If we can not proceed, we just end here.
                    // ########################################
                    // The current ExecService,Facility pair should be sleeping
                    // in SchedulingPool at the moment...
                    log.info("   Task {} state set to NONE, will be scheduled again at the next cycle.", task.getId());
                    schedulingPool.setTaskStatus(task, TaskStatus.NONE);
                }
            }
        } else if (execService.getExecServiceType().equals(ExecServiceType.GENERATE)) {
            log.debug("   Well, it is not. ExecService of type GENERATE does not have any dependencies by design, so we schedule it immediately.");
            log.info("   SCHEDULING task [" + task.getId() + "], execService [" + execService.getId() + "] facility [" + facility.getId() + "] as PLANNED.");
            task.setSchedule(time);
            schedulingPool.setTaskStatus(task, TaskStatus.PLANNED);
            sendToEngine(task);
        // manipulateTasks(execService, facility, task);
        } else {
            throw new IllegalArgumentException("Unknown ExecService type. Expected GENERATE or SEND.");
        }
    } else {
        log.debug("   We do not proceed, we put the task [" + task.getId() + "], [" + execService.getId() + "] execService to sleep.");
    }
    return true;
}
Also used : Task(cz.metacentrum.perun.taskslib.model.Task) DependencyScope(cz.metacentrum.perun.taskslib.dao.ExecServiceDependencyDao.DependencyScope) ExecService(cz.metacentrum.perun.taskslib.model.ExecService) ExecService(cz.metacentrum.perun.taskslib.model.ExecService) Service(cz.metacentrum.perun.core.api.Service) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Date(java.util.Date) ServiceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException) DispatcherQueue(cz.metacentrum.perun.dispatcher.jms.DispatcherQueue) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) Facility(cz.metacentrum.perun.core.api.Facility) Pair(cz.metacentrum.perun.core.api.Pair)

Example 8 with ServiceNotExistsException

use of cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException in project perun by CESNET.

the class ServicesManagerImpl method getServicesFromServicesPackage.

public List<Service> getServicesFromServicesPackage(PerunSession sess, ServicesPackage servicesPackage) throws InternalErrorException {
    try {
        List<Service> services = new ArrayList<Service>();
        List<Integer> servicesId = jdbc.query("select service_id as id from service_service_packages where package_id=?", Utils.ID_MAPPER, servicesPackage.getId());
        for (Integer serviceId : servicesId) {
            try {
                services.add(getServiceById(sess, serviceId));
            } catch (ServiceNotExistsException ex) {
                throw new InternalErrorException(ex);
            }
        }
        return services;
    } catch (RuntimeException ex) {
        throw new InternalErrorException(ex);
    }
}
Also used : ServiceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException) ArrayList(java.util.ArrayList) Service(cz.metacentrum.perun.core.api.Service) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Aggregations

ServiceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException)8 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)5 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)5 ExecService (cz.metacentrum.perun.taskslib.model.ExecService)5 Service (cz.metacentrum.perun.core.api.Service)4 Facility (cz.metacentrum.perun.core.api.Facility)3 FacilityNotExistsException (cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException)3 Task (cz.metacentrum.perun.taskslib.model.Task)3 Date (java.util.Date)3 Destination (cz.metacentrum.perun.core.api.Destination)2 DispatcherQueue (cz.metacentrum.perun.dispatcher.jms.DispatcherQueue)2 InvalidEventMessageException (cz.metacentrum.perun.engine.exceptions.InvalidEventMessageException)2 ArrayList (java.util.ArrayList)2 Pair (cz.metacentrum.perun.core.api.Pair)1 PerunBean (cz.metacentrum.perun.core.api.PerunBean)1 ServiceExistsException (cz.metacentrum.perun.core.api.exceptions.ServiceExistsException)1 DependencyScope (cz.metacentrum.perun.taskslib.dao.ExecServiceDependencyDao.DependencyScope)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Transactional (org.springframework.transaction.annotation.Transactional)1