Search in sources :

Example 1 with FacilityNotExistsException

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

the class Utils method generateAllResourcesToWriter.

/**
	 * Method generate all Resources to the text for using in LDIF.
	 * Write all these information to writer in perunInitializer object.
	 *
	 * @param perunInitializer need to be loaded to get all needed dependencies
	 *
	 * @throws InternalErrorException if some problem with initializer or objects in perun-core
	 * @throws IOException if some problem with writer
	 */
public static void generateAllResourcesToWriter(PerunInitializer perunInitializer) throws InternalErrorException, IOException {
    //Load basic variables
    if (perunInitializer == null)
        throw new InternalErrorException("PerunInitializer must be loaded before using in generating methods!");
    PerunSession perunSession = perunInitializer.getPerunSession();
    PerunBl perun = perunInitializer.getPerunBl();
    BufferedWriter writer = perunInitializer.getOutputWriter();
    //first get all Vos
    List<Vo> vos = perun.getVosManagerBl().getVos(perunSession);
    //Then from every Vo get all assigned resources and write their data to the writer
    for (Vo vo : vos) {
        List<Resource> resources;
        resources = perun.getResourcesManagerBl().getResources(perunSession, vo);
        for (Resource resource : resources) {
            //Read facility attribute entityID and write it for the resource if exists
            Facility facility = null;
            try {
                facility = perun.getFacilitiesManagerBl().getFacilityById(perunSession, resource.getFacilityId());
            } catch (FacilityNotExistsException ex) {
                throw new InternalErrorException("Can't found facility of this resource " + resource, ex);
            }
            Attribute entityIDAttr = null;
            try {
                entityIDAttr = perun.getAttributesManagerBl().getAttribute(perunSession, facility, AttributesManager.NS_FACILITY_ATTR_DEF + ":entityID");
            } catch (AttributeNotExistsException | WrongAttributeAssignmentException ex) {
                throw new InternalErrorException("Problem with loading entityID attribute of facility " + facility, ex);
            }
            String dn = "dn: ";
            String oc1 = "objectclass: top";
            String oc3 = "objectclass: perunResource";
            String cn = "cn: ";
            String perunVoId = "perunVoId: ";
            String perunFacilityId = "perunFacilityId: ";
            String perunResourceId = "perunResourceId: ";
            String description = "description: ";
            String entityID = "entityID: ";
            perunVoId += String.valueOf(resource.getVoId());
            perunFacilityId += String.valueOf(resource.getFacilityId());
            perunResourceId += String.valueOf(resource.getId());
            dn += "perunResourceId=" + resource.getId() + ",perunVoId=" + resource.getVoId() + ",dc=perun,dc=cesnet,dc=cz";
            cn += resource.getName();
            String descriptionValue = resource.getDescription();
            if (descriptionValue != null) {
                if (descriptionValue.matches("^[ ]*$"))
                    descriptionValue = null;
            }
            writer.write(dn + '\n');
            writer.write(oc1 + '\n');
            writer.write(oc3 + '\n');
            writer.write(cn + '\n');
            writer.write(perunResourceId + '\n');
            if (descriptionValue != null)
                writer.write(description + descriptionValue + '\n');
            writer.write(perunVoId + '\n');
            writer.write(perunFacilityId + '\n');
            if (entityIDAttr.getValue() != null)
                writer.write(entityID + (String) entityIDAttr.getValue() + '\n');
            //ADD resources which group is assigned to
            List<Group> associatedGroups = perun.getResourcesManagerBl().getAssignedGroups(perunSession, resource);
            for (Group g : associatedGroups) {
                writer.write("assignedGroupId: " + g.getId());
                writer.write('\n');
            }
            writer.write('\n');
        }
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) PerunSession(cz.metacentrum.perun.core.api.PerunSession) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) Resource(cz.metacentrum.perun.core.api.Resource) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) BufferedWriter(java.io.BufferedWriter) Vo(cz.metacentrum.perun.core.api.Vo) Facility(cz.metacentrum.perun.core.api.Facility)

Example 2 with FacilityNotExistsException

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

the class PropagationMaintainerImpl method rescheduleErrorTasks.

private void rescheduleErrorTasks() {
    log.info("I am gonna list tasks in ERROR and reschedule if necessary...");
    for (Task task : schedulingPool.getErrorTasks()) {
        if (task.getEndTime() == null) {
            log.error("RECOVERY FROM INCONSISTENT STATE: ERROR task does not have end_time! Setting end_time to task.getDelay + 1.");
            // getDelay is in minutes, therefore we multiply it with 60*1000
            Date endTime = new Date(System.currentTimeMillis() - ((task.getDelay() + 1) * 60000));
            task.setEndTime(endTime);
        }
        int howManyMinutesAgo = (int) (System.currentTimeMillis() - task.getEndTime().getTime()) / 1000 / 60;
        if (howManyMinutesAgo < 0) {
            log.error("RECOVERY FROM INCONSISTENT STATE: ERROR task appears to have ended in future.");
            Date endTime = new Date(System.currentTimeMillis() - ((task.getDelay() + 1) * 60000));
            task.setEndTime(endTime);
            howManyMinutesAgo = task.getDelay() + 1;
        }
        log.info("TASK [" + task + "] in ERROR state completed " + howManyMinutesAgo + " minutes ago.");
        // XXX - apparently this is not what the authors had in mind,
        // commented out
        // check and set recurrence
        // int recurrence = task.getRecurrence() - 1;
        // if(recurrence < 0) {
        // // no more retries, sorry
        // log.info("TASK [ " + task +
        // "] in ERROR state has no more retries, bailing out.");
        // continue;
        // }
        // task.setRecurrence(recurrence);
        // If DELAY time has passed, we reschedule...
        int recurrence = task.getRecurrence() + 1;
        if (recurrence > task.getExecService().getDefaultRecurrence() && howManyMinutesAgo < 60 * 12 && !task.isSourceUpdated()) {
            log.info("TASK [ " + task + "] in ERROR state has no more retries, bailing out.");
        } else if (howManyMinutesAgo >= recurrence * task.getDelay() || task.isSourceUpdated()) {
            // check if service is still assigned on facility
            try {
                List<Service> assignedServices = perun.getServicesManager().getAssignedServices(perunSession, task.getFacility());
                if (assignedServices.contains(task.getExecService().getService())) {
                    ExecService execService = task.getExecService();
                    Facility facility = task.getFacility();
                    if (recurrence > execService.getDefaultRecurrence()) {
                        // this ERROR task is rescheduled for being here too long
                        task.setRecurrence(0);
                        task.setDestinations(null);
                        log.info("TASK id " + task.getId() + " is in ERROR state long enough, ");
                    }
                    task.setRecurrence(recurrence);
                    log.info("TASK [" + task + "] in ERROR state is going to be rescheduled: taskScheduler.propagateService(execService:ID " + execService.getId() + ", new Date(System.currentTimeMillis()), facility:ID " + facility.getId() + ");");
                    // taskScheduler.propagateService(task, new
                    // Date(System.currentTimeMillis()));
                    taskScheduler.scheduleTask(task);
                    log.info("TASK [" + task + "] in ERROR state has been rescheduled.");
                    // Also (to be sure) reschedule all Tasks that depend on
                    // this Task
                    //
                    // While engine starts in state GEN = ERROR, SEND = DONE
                    // => GEN will be rescheduled but without this SEND will
                    // never be propagated
                    List<ExecService> dependantServices = dependenciesResolver.listDependantServices(execService);
                    for (ExecService dependantService : dependantServices) {
                        Task dependantTask = schedulingPool.getTask(dependantService, facility);
                        if (dependantTask == null) {
                            dependantTask = new Task();
                            dependantTask.setExecService(dependantService);
                            dependantTask.setFacility(facility);
                            dependantTask.setRecurrence(dependantService.getDefaultRecurrence());
                            schedulingPool.addToPool(dependantTask, schedulingPool.getQueueForTask(task));
                            taskScheduler.scheduleTask(dependantTask);
                            log.info("{} was rescheduled because it depends on {}", dependantTask, task);
                        }
                    }
                } else {
                    // delete this tasks (SEND and GEN) because service is
                    // no longer assigned to facility
                    schedulingPool.removeTask(task);
                    log.warn("Removed TASK {} from database, beacuse service is no longer assigned to this facility.", task.toString());
                }
            } catch (FacilityNotExistsException e) {
                schedulingPool.removeTask(task);
                log.error("Removed TASK {} from database, facility no longer exists.", task.getId());
            } catch (InternalErrorException e) {
                log.error("{}", e);
            } catch (PrivilegeException e) {
                log.error("Consistency error. {}", e);
            }
        }
    }
/*
		 * Original implementation:
		 *
		 * //TODO: Take into account Recurrence! for (Task task :
		 * taskManager.listAllTasksInState(TaskStatus.ERROR,
		 * Integer.parseInt(propertiesBean.getProperty("engine.unique.id")))) {
		 * if (task.getEndTime() == null) { log.error(
		 * "RECOVERY FROM INCONSISTATE STATE: ERROR task does not have end_time! Setting end_time to task.getDelay + 1."
		 * ); // getDelay is in minutes, therefore we multiply it with 60*1000
		 * Date endTime = new Date(System.currentTimeMillis() -
		 * ((task.getDelay() + 1) * 60000)); task.setEndTime(endTime);
		 * taskManager.updateTask(task,
		 * Integer.parseInt(propertiesBean.getProperty("engine.unique.id"))); }
		 * int howManyMinutesAgo = (int) (System.currentTimeMillis() -
		 * task.getEndTime().getTime()) / 1000 / 60; log.info("TASK [" + task +
		 * "] in ERROR state completed " + howManyMinutesAgo + " minutes ago.");
		 * //If DELAY time has passed, we reschedule... if (howManyMinutesAgo >=
		 * task.getDelay()) { //check if service is still assigned on facility
		 * try { List<Service> assignedServices =
		 * Rpc.ServicesManager.getAssignedServices(engineManager.getRpcCaller(),
		 * task.getFacility());
		 * if(assignedServices.contains(task.getExecService().getService())) {
		 * try { taskManager.updateTask(task,
		 * Integer.parseInt(propertiesBean.getProperty("engine.unique.id")));
		 * ExecService execService = task.getExecService(); Facility facility =
		 * task.getFacility(); log.info("TASK [" + task +
		 * "] in ERROR state is going to be rescheduled: taskScheduler.propagateService(execService:ID "
		 * + execService.getId() +
		 * ", new Date(System.currentTimeMillis()), facility:ID " +
		 * facility.getId() + ");"); taskScheduler.propagateService(execService,
		 * new Date(System.currentTimeMillis()), facility); log.info("TASK [" +
		 * task + "] in ERROR state has been rescheduled.");
		 *
		 * //Also (to be sure) reschedule all execServices which depends on this
		 * exec service // //While engine starts in state GEN = ERROR, SEND =
		 * DONE => GEN will be rescheduled but without this SEND will never be
		 * propagated List<ExecService> dependentExecServices =
		 * Rpc.GeneralServiceManager
		 * .listExecServicesDependingOn(engineManager.getRpcCaller(),
		 * execService); if(dependentExecServices != null) { for(ExecService
		 * dependantExecService : dependentExecServices) {
		 * taskScheduler.propagateService(dependantExecService, new
		 * Date(System.currentTimeMillis()), facility);
		 * log.info("{} was rescheduled because it depends on {}",
		 * dependantExecService, execService); } }
		 *
		 * } catch (InternalErrorException e) { log.error(e.toString(), e); } }
		 * else { //delete this tasks (SEND and GEN) because service is no
		 * longer assigned to facility List<ExecService> execServicesGenAndSend
		 * =
		 * Rpc.GeneralServiceManager.listExecServices(engineManager.getRpcCaller
		 * (), task.getExecService().getService().getId()); for(ExecService
		 * execService : execServicesGenAndSend) { Task taskToDelete =
		 * taskManager.getTask(execService, task.getFacility(),
		 * Integer.parseInt(propertiesBean.getProperty("engine.unique.id")));
		 * if(taskToDelete!= null) {
		 * resultManager.clearByTask(taskToDelete.getId(),
		 * Integer.parseInt(propertiesBean.getProperty("engine.unique.id")));
		 * taskManager.removeTask(taskToDelete.getId(),
		 * Integer.parseInt(propertiesBean.getProperty("engine.unique.id"))); }
		 * } } } catch(PrivilegeException ex) {
		 * log.error("Consistency error. {}", ex); }
		 * catch(FacilityNotExistsException ex) {
		 * log.error("Consistency error - found task for non-existing facility. {}"
		 * , ex); } catch(ServiceNotExistsException ex) {
		 * log.error("Consistency error - found task for non-existing service. {}"
		 * , ex); } catch(InternalErrorException ex) { log.error("{}", ex); } }
		 * }
		 */
}
Also used : Task(cz.metacentrum.perun.taskslib.model.Task) ExecService(cz.metacentrum.perun.taskslib.model.ExecService) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) ArrayList(java.util.ArrayList) List(java.util.List) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) Facility(cz.metacentrum.perun.core.api.Facility) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Date(java.util.Date)

Example 3 with FacilityNotExistsException

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

the class TaskSchedulerImpl method sendToEngine.

private void sendToEngine(Task task) {
    DispatcherQueue dispatcherQueue;
    try {
        dispatcherQueue = schedulingPool.getQueueForTask(task);
    } catch (InternalErrorException e1) {
        log.error("No engine set for task " + task.toString() + ", could not send it!");
        return;
    }
    if (dispatcherQueue == null) {
        // where should we send the task?
        if (dispatcherQueuePool.poolSize() > 0) {
            dispatcherQueue = dispatcherQueuePool.getPool().iterator().next();
            try {
                schedulingPool.setQueueForTask(task, dispatcherQueue);
            } catch (InternalErrorException e) {
                log.error("Could not assign new queue for task {}: {}", task.getId(), e);
                return;
            }
            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;
        }
    }
    // task|[engine_id]|[task_id][is_forced][exec_service_id][facility]|[destination_list]|[dependency_list]
    // - the task|[engine_id] part is added by dispatcherQueue
    List<Destination> destinations = task.getDestinations();
    if (destinations == null || destinations.isEmpty()) {
        log.debug("No destinations for task " + task.toString() + ", trying to query the database...");
        try {
            initPerunSession();
            destinations = perun.getServicesManager().getDestinations(perunSession, task.getExecService().getService(), task.getFacility());
        } catch (ServiceNotExistsException e) {
            log.error("No destinations found for task " + task.getId());
            task.setEndTime(new Date(System.currentTimeMillis()));
            schedulingPool.setTaskStatus(task, TaskStatus.ERROR);
            return;
        } catch (FacilityNotExistsException e) {
            log.error("Facility for task {} does not exist...", task.getId());
            task.setEndTime(new Date(System.currentTimeMillis()));
            schedulingPool.setTaskStatus(task, TaskStatus.ERROR);
            return;
        } catch (PrivilegeException e) {
            log.error("Privilege error accessing the database: " + e.getMessage());
            task.setEndTime(new Date(System.currentTimeMillis()));
            schedulingPool.setTaskStatus(task, TaskStatus.ERROR);
            return;
        } catch (InternalErrorException e) {
            log.error("Internal error: " + e.getMessage());
            task.setEndTime(new Date(System.currentTimeMillis()));
            schedulingPool.setTaskStatus(task, TaskStatus.ERROR);
            return;
        }
    }
    log.debug("Fetched destinations: " + ((destinations == null) ? "[]" : destinations.toString()));
    task.setDestinations(destinations);
    StringBuilder destinations_s = new StringBuilder("Destinations [");
    if (destinations != null) {
        for (Destination destination : destinations) {
            destinations_s.append(destination.serializeToString() + ", ");
        }
    }
    destinations_s.append("]");
    String dependencies = "";
    dispatcherQueue.sendMessage("[" + task.getId() + "][" + task.isPropagationForced() + "]|[" + fixStringSeparators(task.getExecService().serializeToString()) + "]|[" + fixStringSeparators(task.getFacility().serializeToString()) + "]|[" + fixStringSeparators(destinations_s.toString()) + "]|[" + dependencies + "]");
    task.setStartTime(new Date(System.currentTimeMillis()));
    task.setEndTime(null);
    schedulingPool.setTaskStatus(task, TaskStatus.PROCESSING);
}
Also used : Destination(cz.metacentrum.perun.core.api.Destination) ServiceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException) DispatcherQueue(cz.metacentrum.perun.dispatcher.jms.DispatcherQueue) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Date(java.util.Date)

Example 4 with FacilityNotExistsException

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

the class FacilitiesManagerBlImpl method createFacility.

public Facility createFacility(PerunSession sess, Facility facility) throws InternalErrorException, FacilityExistsException {
    //check facility name, it can contain only a-zA-Z.0-9_-
    if (!facility.getName().matches("^[ a-zA-Z.0-9_-]+$")) {
        throw new InternalErrorException(new IllegalArgumentException("Wrong facility name, facility name can contain only a-Z0-9.-_ and space characters"));
    }
    //check if facility have uniq name
    try {
        this.getFacilityByName(sess, facility.getName());
        throw new FacilityExistsException(facility);
    } catch (FacilityNotExistsException ex) {
    /* OK */
    }
    // create facility
    facility = getFacilitiesManagerImpl().createFacility(sess, facility);
    getPerunBl().getAuditer().log(sess, "Facility created {}.", facility);
    //set creator as Facility manager
    if (sess.getPerunPrincipal().getUser() != null) {
        try {
            addAdmin(sess, facility, sess.getPerunPrincipal().getUser());
        } catch (AlreadyAdminException ex) {
            throw new ConsistencyErrorException("Add manager to newly created Facility failed because there is particular manager already assigned", ex);
        }
    } else {
        log.warn("Can't set Facility manager during creating of the Facility. User from perunSession is null. {} {}", facility, sess);
    }
    return facility;
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) FacilityExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityExistsException) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) AlreadyAdminException(cz.metacentrum.perun.core.api.exceptions.AlreadyAdminException)

Example 5 with FacilityNotExistsException

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

the class AttributesManagerBlImpl method getResourceRequiredAttributes.

public List<Attribute> getResourceRequiredAttributes(PerunSession sess, Resource resourceToGetServicesFrom, Resource resource, Member member, boolean workWithUserAttributes) throws InternalErrorException, WrongAttributeAssignmentException {
    this.checkMemberIsFromTheSameVoLikeResource(sess, member, resource);
    List<Attribute> attributes = new ArrayList<Attribute>();
    attributes.addAll(getAttributesManagerImpl().getRequiredAttributes(sess, resourceToGetServicesFrom, resource, member));
    if (workWithUserAttributes) {
        User user;
        Facility facility;
        try {
            user = getPerunBl().getUsersManagerBl().getUserById(sess, member.getUserId());
            facility = getPerunBl().getFacilitiesManagerBl().getFacilityById(sess, resource.getFacilityId());
        } catch (UserNotExistsException e) {
            throw new ConsistencyErrorException("Member has non-existent user.", e);
        } catch (FacilityNotExistsException e) {
            throw new ConsistencyErrorException("Resource has non-existent facility.", e);
        }
        attributes.addAll(getAttributesManagerImpl().getRequiredAttributes(sess, resourceToGetServicesFrom, facility, user));
        attributes.addAll(getAttributesManagerImpl().getRequiredAttributes(sess, resourceToGetServicesFrom, user));
        attributes.addAll(getAttributesManagerImpl().getRequiredAttributes(sess, resourceToGetServicesFrom, member));
    }
    return attributes;
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) User(cz.metacentrum.perun.core.api.User) Attribute(cz.metacentrum.perun.core.api.Attribute) RichAttribute(cz.metacentrum.perun.core.api.RichAttribute) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) ArrayList(java.util.ArrayList) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) Facility(cz.metacentrum.perun.core.api.Facility)

Aggregations

FacilityNotExistsException (cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException)11 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)8 Facility (cz.metacentrum.perun.core.api.Facility)7 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)5 Attribute (cz.metacentrum.perun.core.api.Attribute)4 ExecService (cz.metacentrum.perun.taskslib.model.ExecService)4 ArrayList (java.util.ArrayList)4 User (cz.metacentrum.perun.core.api.User)3 ServiceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException)3 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)3 Date (java.util.Date)3 Group (cz.metacentrum.perun.core.api.Group)2 PerunSession (cz.metacentrum.perun.core.api.PerunSession)2 Resource (cz.metacentrum.perun.core.api.Resource)2 Service (cz.metacentrum.perun.core.api.Service)2 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)2 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)2 MemberNotExistsException (cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException)2 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)2 DispatcherQueue (cz.metacentrum.perun.dispatcher.jms.DispatcherQueue)2