Search in sources :

Example 51 with Service

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

the class AttributesManagerEntryIntegrationTest method getRequiredResourceAttributesFromOneServiceWhenServiceNotExists.

@Test(expected = ServiceNotExistsException.class)
public void getRequiredResourceAttributesFromOneServiceWhenServiceNotExists() throws Exception {
    System.out.println(CLASS_NAME + "getRequiredResourceAttributesFromOneServiceWhenServiceNotExists");
    vo = setUpVo();
    facility = setUpFacility();
    resource = setUpResource();
    attributesManager.getRequiredAttributes(sess, new Service(), resource);
// shouldn't find service
}
Also used : Service(cz.metacentrum.perun.core.api.Service) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 52 with Service

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

the class AttributesManagerEntryIntegrationTest method getRequiredMemberResourceAttributesFromOneServiceWorkWithUserWhenServiceNotExists.

@Test(expected = ServiceNotExistsException.class)
public void getRequiredMemberResourceAttributesFromOneServiceWorkWithUserWhenServiceNotExists() throws Exception {
    System.out.println(CLASS_NAME + "getRequiredMemberResourceAttributesFromOneServiceWorkWithUserWhenServiceNotExists");
    vo = setUpVo();
    facility = setUpFacility();
    resource = setUpResource();
    member = setUpMember();
    attributesManager.getRequiredAttributes(sess, new Service(), resource, member, true);
// shouldn't find service
}
Also used : Service(cz.metacentrum.perun.core.api.Service) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 53 with Service

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

the class AttributesManagerEntryIntegrationTest method getRequiredMemberAttributesFromOneServiceWhenServiceNotExists.

@Test(expected = ServiceNotExistsException.class)
public void getRequiredMemberAttributesFromOneServiceWhenServiceNotExists() throws Exception {
    System.out.println(CLASS_NAME + "getRequiredMemberAttributesFromOneServiceWhenServiceNotExists");
    vo = setUpVo();
    member = setUpMember();
    attributesManager.getRequiredAttributes(sess, new Service(), member);
// shouldn't find service
}
Also used : Service(cz.metacentrum.perun.core.api.Service) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 54 with Service

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

the class EventExecServiceResolverImpl method parseEvent.

@Override
public Map<Facility, Set<ExecService>> parseEvent(String event) throws InvalidEventMessageException, ServiceNotExistsException, InternalErrorException, PrivilegeException {
    log.info("I am going to process event:" + event);
    /**
		 * Expected string format as on:
		 * https://projekty.ics.muni.cz/perunv3/trac
		 * /wiki/PerunEngineDispatcherController event|x|[timestamp][Event
		 * header][Event data]
		 */
    String eventParsingPattern = "^\\[([a-zA-Z0-9+: ]+)\\]\\[([^\\]]+)\\]\\[(.*)\\]$";
    Pattern pattern = Pattern.compile(eventParsingPattern);
    Matcher matcher = pattern.matcher(event);
    boolean matchFound = matcher.find();
    if (matchFound) {
        log.debug("Message format matched ok...");
        // NOT USED ANYMORE: not applicable in dispatcher
        // String thisEngineID = matcher.group(1);
        // // This should indeed match the current Engine instance ID, so
        // let's compare it...
        // if (Integer.parseInt(thisEngineID) != Integer.parseInt((String)
        // propertiesBean.get("engine.unique.id"))) {
        // throw new InvalidEventMessageException("Wrong Engine ID. Was:" +
        // thisEngineID + ", Expected:" +
        // propertiesBean.get("engine.unique.id"));
        // }
        // // Not being used at the moment.
        // String timeStamp = matcher.group(2);
        // Header should provide information regarding the target facility.
        String eventHeader = matcher.group(2);
        // We expect the string to contain something like this:
        // facility.id=2 ???
        // String headerParsingPattern = ".*facility.id\\=([0-9]+).*";
        // Pattern headerPattern = Pattern.compile(headerParsingPattern);
        // Matcher headerMatcher = headerPattern.matcher(eventHeader);
        /*
			 * boolean headerMatchFound = headerMatcher.find();
			 * if(!headerMatchFound) { throw new InvalidEventMessageException(
			 * "Invalid event header. It does not contain the expected facility.id=value..."
			 * ); } int facilityId = Integer.parseInt(matcher.group(1));
			 * PerunSession perunSession =
			 * engineManager.getPerunSession(propertiesBean
			 * .getProperty("perun.principal")); Facility facility = null; try {
			 * facility = facilitiesManager.getFacilityById(perunSession,
			 * facilityId); } catch (FacilityNotExistsException e) { throw new
			 * InvalidEventMessageException
			 * ("Facility with ID "+facilityId+"does not exist.", e); } catch
			 * (InternalErrorException e) { throw new
			 * InvalidEventMessageException("Unknown error...", e); } catch
			 * (PrivilegeException e) { throw new
			 * InvalidEventMessageException("Principal "
			 * +propertiesBean.getProperty
			 * ("perun.principal")+" is not allowed to access that facility. ",
			 * e); }
			 */
        // Data should provide information regarding the target ExecService
        // (Processing rule).
        String eventData = matcher.group(3);
        log.debug("Event data to be parsed:" + eventData);
        // GET All Beans (only PerunBeans) from message
        List<PerunBean> listOfBeans = new ArrayList<PerunBean>();
        listOfBeans = AuditParser.parseLog(eventData);
        // Prepare variables
        AttributeDefinition attributeDefinition = null;
        Attribute attribute = null;
        Facility facility = null;
        Resource resource = null;
        Group group = null;
        User user = null;
        Member member = null;
        Service service = null;
        Host host = null;
        // etc. ?
        for (PerunBean pb : listOfBeans) {
            if (pb instanceof AttributeDefinition && pb instanceof Attribute) {
                attribute = (Attribute) pb;
            } else if (pb instanceof Facility) {
                facility = (Facility) pb;
            } else if (pb instanceof Resource) {
                resource = (Resource) pb;
            } else if (pb instanceof Group) {
                group = (Group) pb;
            } else if (pb instanceof User) {
                user = (User) pb;
            } else if (pb instanceof Member) {
                member = (Member) pb;
            } else if (pb instanceof Service) {
                service = (Service) pb;
            } else if (pb instanceof Host) {
                host = (Host) pb;
            }
        }
        // If there is any attribute, so create AttributeDefinition
        if (attribute != null) {
            attributeDefinition = new AttributeDefinition(attribute);
            log.debug("Attribute found in event. {}.", attributeDefinition);
        }
        List<Facility> facilitiesResolvedFromEvent = new ArrayList<Facility>();
        List<Resource> resourcesResolvedFromEvent = new ArrayList<Resource>();
        List<Service> servicesResolvedFromEvent = new ArrayList<Service>();
        // =============== Resolve facilities from event======================
        PerunSession perunSession = perun.getPerunSession(new PerunPrincipal(dispatcherPropertiesBean.getProperty("perun.principal.name"), dispatcherPropertiesBean.getProperty("perun.principal.extSourceName"), dispatcherPropertiesBean.getProperty("perun.principal.extSourceType")), new PerunClient());
        // Try to find FACILITY in event
        if (facility != null) {
            try {
                log.debug("Facility found in event. {}.", facility);
                facilitiesResolvedFromEvent.add(facility);
                resourcesResolvedFromEvent.addAll(perun.getFacilitiesManager().getAssignedResources(perunSession, facility));
            } catch (FacilityNotExistsException ex) {
                log.debug("Non-existing facility found while resolving event. id={}", facility.getId());
            }
        } else {
            // Try to find RESOURCE in event
            if (resource != null) {
                resourcesResolvedFromEvent.add(resource);
            } else {
                // Try to find GROUP in event
                if (group != null) {
                    try {
                        resourcesResolvedFromEvent = perun.getResourcesManager().getAssignedResources(perunSession, group);
                    } catch (GroupNotExistsException ex) {
                        log.debug("Non-existing group found while resolving event. id={}", group.getId());
                    }
                } else {
                    // try to find USER in event
                    if (user != null) {
                        try {
                            resourcesResolvedFromEvent = perun.getUsersManager().getAllowedResources(perunSession, user);
                        } catch (UserNotExistsException ex) {
                            log.debug("Non-existing user found while resolving event. id={}", user.getId());
                        }
                    } else {
                        // try to find MEMBER in event
                        if (member != null) {
                            try {
                                resourcesResolvedFromEvent = perun.getResourcesManager().getAllowedResources(perunSession, member);
                            } catch (MemberNotExistsException ex) {
                                log.debug("Non-existing member found while resolving event. id={}", member.getId());
                            }
                        } else {
                            // try to find HOST in event
                            if (host != null) {
                                try {
                                    log.debug("Host found in event.id= {}.", host.getId());
                                    facility = perun.getFacilitiesManager().getFacilityForHost(perunSession, host);
                                    facilitiesResolvedFromEvent.add(facility);
                                    resourcesResolvedFromEvent.addAll(perun.getFacilitiesManager().getAssignedResources(perunSession, facility));
                                } catch (FacilityNotExistsException ex) {
                                    log.debug("Host on non-existing facility found while resolving event. Host id={}", host.getId());
                                } catch (HostNotExistsException ex) {
                                    log.debug("Non-existing host found while resolving event. id={}", host.getId());
                                }
                            } else {
                                log.warn("No match found for this event. Event={}", event);
                            }
                        }
                    }
                }
            }
        }
        // TODO resolve more than one service
        if (service != null) {
            servicesResolvedFromEvent.add(service);
        }
        //List<Pair<List<ExecService>, Facility>> pairs = new ArrayList<Pair<List<ExecService>, Facility>>();
        Map<Facility, Set<ExecService>> result = new HashMap<Facility, Set<ExecService>>();
        for (Resource r : resourcesResolvedFromEvent) {
            Facility facilityResolvedFromEvent;
            List<Service> servicesResolvedFromResource;
            try {
                facilityResolvedFromEvent = perun.getResourcesManager().getFacility(perunSession, r);
                servicesResolvedFromResource = perun.getResourcesManager().getAssignedServices(perunSession, r);
                // process only services resolved from event if any
                if (!servicesResolvedFromEvent.isEmpty())
                    servicesResolvedFromResource.retainAll(servicesResolvedFromEvent);
            } catch (ResourceNotExistsException ex) {
                log.debug("Non-existing resource found while resolving event. Resource={}", r);
                // skip to next resource
                continue;
            }
            for (Service s : servicesResolvedFromResource) {
                // TODO: Optimize with an SQL query...
                List<ExecService> execServicesGenAndSend = generalServiceManager.listExecServices(perunSession, s.getId());
                List<ExecService> execServices = new ArrayList<ExecService>();
                for (ExecService execService : execServicesGenAndSend) {
                    if (execService.getExecServiceType().equals(ExecServiceType.SEND)) {
                        execServices.add(execService);
                    }
                }
                if (attributeDefinition != null) {
                    // remove from future processing services
                    // which don't require the found attribute
                    // TODO (CHECKME) This method can raise
                    // ServiceNotExistsException. Is it ok? Or it must be
                    // catch?
                    List<AttributeDefinition> serviceRequiredAttributes = perun.getAttributesManager().getRequiredAttributesDefinition(perunSession, s);
                    if (!serviceRequiredAttributes.contains(attributeDefinition))
                        continue;
                }
                if (!result.containsKey(facilityResolvedFromEvent)) {
                    result.put(facilityResolvedFromEvent, new HashSet<ExecService>(execServices));
                } else {
                    result.get(facilityResolvedFromEvent).addAll(execServices);
                }
            }
        }
        log.info("I am going to return " + result.size() + " facilities.");
        return result;
    } else {
        throw new InvalidEventMessageException("Message[" + event + "]");
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) User(cz.metacentrum.perun.core.api.User) HashSet(java.util.HashSet) Set(java.util.Set) Matcher(java.util.regex.Matcher) Attribute(cz.metacentrum.perun.core.api.Attribute) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) PerunPrincipal(cz.metacentrum.perun.core.api.PerunPrincipal) Member(cz.metacentrum.perun.core.api.Member) Pattern(java.util.regex.Pattern) PerunSession(cz.metacentrum.perun.core.api.PerunSession) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) ExecService(cz.metacentrum.perun.taskslib.model.ExecService) HostNotExistsException(cz.metacentrum.perun.core.api.exceptions.HostNotExistsException) Resource(cz.metacentrum.perun.core.api.Resource) ExecService(cz.metacentrum.perun.taskslib.model.ExecService) Service(cz.metacentrum.perun.core.api.Service) Host(cz.metacentrum.perun.core.api.Host) ResourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ResourceNotExistsException) PerunBean(cz.metacentrum.perun.core.api.PerunBean) PerunClient(cz.metacentrum.perun.core.api.PerunClient) Facility(cz.metacentrum.perun.core.api.Facility) InvalidEventMessageException(cz.metacentrum.perun.dispatcher.exceptions.InvalidEventMessageException)

Example 55 with Service

use of cz.metacentrum.perun.core.api.Service 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)

Aggregations

Service (cz.metacentrum.perun.core.api.Service)66 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)44 Test (org.junit.Test)44 ArrayList (java.util.ArrayList)11 Facility (cz.metacentrum.perun.core.api.Facility)10 Destination (cz.metacentrum.perun.core.api.Destination)9 Member (cz.metacentrum.perun.core.api.Member)8 ExecService (cz.metacentrum.perun.taskslib.model.ExecService)8 User (cz.metacentrum.perun.core.api.User)6 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)6 Attribute (cz.metacentrum.perun.core.api.Attribute)5 BanOnFacility (cz.metacentrum.perun.core.api.BanOnFacility)5 Group (cz.metacentrum.perun.core.api.Group)5 Host (cz.metacentrum.perun.core.api.Host)5 Resource (cz.metacentrum.perun.core.api.Resource)5 RichDestination (cz.metacentrum.perun.core.api.RichDestination)5 RichUser (cz.metacentrum.perun.core.api.RichUser)5 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)4 PerunClient (cz.metacentrum.perun.core.api.PerunClient)4 PerunPrincipal (cz.metacentrum.perun.core.api.PerunPrincipal)4