Search in sources :

Example 1 with InvalidEventMessageException

use of cz.metacentrum.perun.engine.exceptions.InvalidEventMessageException in project perun by CESNET.

the class EventProcessorImpl method receiveEvent.

@Override
public void receiveEvent(String event) {
    log.info("Current pool size BEFORE event processing:" + schedulingPool.getSize());
    log.debug("Event " + event + " is going to be resolved...");
    Task task = null;
    try {
        task = eventParser.parseEvent(event);
    } catch (InvalidEventMessageException e) {
        log.error(e.toString());
    } catch (ServiceNotExistsException e) {
        log.error(e.toString());
    } catch (InternalErrorException e) {
        log.error(e.toString());
    } catch (PrivilegeException e) {
        log.error(e.toString());
    }
    if (task == null) {
        return;
    }
    // FIXME: Disabled because it can cause race condition. See RT#33803
    if (false) {
        // if (event.contains("forceit")) { // TODO: Move string constant to
        // a properties file
        log.debug("\t Facility[" + task.getFacility() + "]");
        log.debug("\t Resolved ExecService[" + task.getExecService() + "]");
        if (task != null && task.getFacility() != null && task.getExecService() != null) {
            // log.debug("SCHEDULING vie Force Service Propagation: ExecService["
            // + execService.getId() + "] : Facility[" + results.getRight()
            // + "]");
            schedulingPool.addToPool(task);
            final Task ntask = task;
            taskExecutorEventProcessor.execute(new Runnable() {

                @Override
                public void run() {
                    try {
                        taskScheduler.propagateService(ntask, new Date(System.currentTimeMillis()));
                    } catch (InternalErrorException e) {
                        log.error(e.toString());
                    }
                }
            });
        }
        log.debug("POOL SIZE:" + schedulingPool.getSize());
    }
    log.debug("\t Facility[" + task.getFacility() + "]");
    log.debug("\t Resolved ExecService[" + task.getExecService() + "]");
    if (task.getFacility() != null && task.getExecService() != null) {
        // log.debug("ADD to POOL: ExecService[" +
        // results.getLeft().getId() + "] : Facility[" +
        // results.getRight() + "]");
        Task currentTask = schedulingPool.getTaskById(task.getId());
        if (currentTask == null) {
            // task.setSourceUpdated(false);
            schedulingPool.addToPool(task);
            currentTask = task;
        } else {
            // currentTask.setSourceUpdated(true);
            log.debug("Resetting current task destination list to {}", task.getDestinations());
            currentTask.setDestinations(task.getDestinations());
            currentTask.setPropagationForced(task.isPropagationForced());
        }
        if (currentTask.isPropagationForced()) {
            final Task ntask = currentTask;
            try {
                taskExecutorEventProcessor.execute(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            taskScheduler.propagateService(ntask, new Date(System.currentTimeMillis()));
                        } catch (InternalErrorException e) {
                            log.error(e.toString());
                        }
                    }
                });
            } catch (Exception e) {
                log.error("Error queuing task to executor: " + e.toString());
            }
        }
    }
    log.debug("POOL SIZE:" + schedulingPool.getSize());
    log.info("Current pool size AFTER event processing:" + schedulingPool.getSize());
}
Also used : ServiceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException) Task(cz.metacentrum.perun.taskslib.model.Task) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) InvalidEventMessageException(cz.metacentrum.perun.engine.exceptions.InvalidEventMessageException) Date(java.util.Date) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ServiceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException) InvalidEventMessageException(cz.metacentrum.perun.engine.exceptions.InvalidEventMessageException) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException)

Example 2 with InvalidEventMessageException

use of cz.metacentrum.perun.engine.exceptions.InvalidEventMessageException in project perun by CESNET.

the class EventParserImpl method parseEvent.

@Override
public Task 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] New format:
		 * "task|[engine_id]|[task_id][is_forced]|[exec_service]|[facility]|[destination_list]|[dependency_list]"
		 * 
		 */
    // String eventParsingPattern =
    // "^event\\|([0-9]{1,6})\\|\\[([a-zA-Z0-9: ]+)\\]\\[([^\\]]+)\\]\\[(.*)\\]$";
    String eventParsingPattern = "^task\\|([0-9]{1,6})\\|\\[([0-9]+)\\]\\[([^\\]]+)\\]\\|\\[([^\\|]+)\\]\\|\\[([^\\|]+)\\]\\|\\[([^\\|]+)\\]\\|\\[(.*)\\]$";
    Pattern pattern = Pattern.compile(eventParsingPattern);
    Matcher matcher = pattern.matcher(event);
    boolean matchFound = matcher.find();
    if (matchFound) {
        log.debug("Message format matched ok...");
        String thisEngineID = matcher.group(1);
        // compare it...
        try {
            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"));
            }
        } catch (Exception e) {
            throw new InvalidEventMessageException("Wrong Engine ID: parse exception", e);
        }
        // Data should provide information regarding the target ExecService
        // (Processing rule).
        String eventTaskId = matcher.group(2);
        String eventIsForced = matcher.group(3);
        String eventExecService = matcher.group(4);
        String eventFacility = matcher.group(5);
        String eventDestinationList = matcher.group(6);
        String eventDependencyList = matcher.group(7);
        // check possible enconding
        if (!eventExecService.startsWith("ExecService")) {
            eventExecService = new String(Base64.decodeBase64(eventExecService));
        }
        if (!eventExecService.startsWith("ExecService")) {
            throw new InvalidEventMessageException("Wrong exec service: parse exception");
        }
        if (!eventFacility.startsWith("Facility")) {
            eventFacility = new String(Base64.decodeBase64(eventFacility));
        }
        if (!eventFacility.startsWith("Facility")) {
            throw new InvalidEventMessageException("Wrong facility: parse exception");
        }
        if (!eventDestinationList.startsWith("Destinations")) {
            eventDestinationList = new String(Base64.decodeBase64(eventDestinationList));
        }
        if (!eventDestinationList.startsWith("Destinations")) {
            throw new InvalidEventMessageException("Wrong destination list: parse exception");
        }
        log.debug("Event data to be parsed: task id " + eventTaskId + ", forced " + eventIsForced + ", facility " + eventFacility + ", exec service " + eventExecService + ", destination list " + eventDestinationList + ", dependency list " + eventDependencyList);
        // Prepare variables
        Facility facility;
        ExecService execService;
        List<Destination> destinationList = new ArrayList<Destination>();
        // resolve facility
        // deserialize event data
        List<PerunBean> listOfBeans = AuditParser.parseLog(eventFacility);
        try {
            facility = (Facility) listOfBeans.get(0);
        } catch (Exception e) {
            throw new InvalidEventMessageException("Could not resolve facility from event [" + eventFacility + "]", e);
        }
        // resolve exec service
        // deserialize event data
        listOfBeans = AuditParser.parseLog(eventExecService);
        try {
            execService = (ExecService) listOfBeans.get(0);
        } catch (Exception e) {
            throw new InvalidEventMessageException("Could not resolve exec service from event [" + eventExecService + "]", e);
        }
        // resolve list of destinations
        listOfBeans = AuditParser.parseLog(eventDestinationList);
        log.debug("Found list of destination beans: " + listOfBeans);
        // return new Pair<ExecService, Facility>(execService, facility);
        try {
            for (PerunBean bean : listOfBeans) {
                destinationList.add((Destination) bean);
            }
        } catch (Exception e) {
            throw new InvalidEventMessageException("Could not resolve list of destinations from event.", e);
        }
        Task task = new Task();
        task.setId(Integer.parseInt(eventTaskId));
        task.setFacility(facility);
        task.setExecService(execService);
        task.setDestinations(destinationList);
        task.setDelay(execService.getDefaultDelay());
        task.setRecurrence(execService.getDefaultRecurrence());
        task.setPropagationForced(Boolean.parseBoolean(eventIsForced));
        // resolve list of dependencies
        if (eventDependencyList != null) {
            for (String token : eventDependencyList.split("[\t ]*,[\t ]*")) {
                if (token.length() > 0) {
                    try {
                        dependenciesResolver.addDependency(task, Integer.parseInt(token));
                    } catch (Exception e) {
                        throw new InvalidEventMessageException("Invalid dependency in event: " + token);
                    }
                }
            }
        }
        return task;
    } else {
        throw new InvalidEventMessageException("Invalid message format: Message[" + event + "]");
    }
}
Also used : Pattern(java.util.regex.Pattern) Destination(cz.metacentrum.perun.core.api.Destination) Task(cz.metacentrum.perun.taskslib.model.Task) Matcher(java.util.regex.Matcher) ExecService(cz.metacentrum.perun.taskslib.model.ExecService) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ServiceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException) InvalidEventMessageException(cz.metacentrum.perun.engine.exceptions.InvalidEventMessageException) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) PerunBean(cz.metacentrum.perun.core.api.PerunBean) Facility(cz.metacentrum.perun.core.api.Facility) InvalidEventMessageException(cz.metacentrum.perun.engine.exceptions.InvalidEventMessageException)

Aggregations

InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)2 ServiceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException)2 InvalidEventMessageException (cz.metacentrum.perun.engine.exceptions.InvalidEventMessageException)2 Task (cz.metacentrum.perun.taskslib.model.Task)2 Destination (cz.metacentrum.perun.core.api.Destination)1 Facility (cz.metacentrum.perun.core.api.Facility)1 PerunBean (cz.metacentrum.perun.core.api.PerunBean)1 ExecService (cz.metacentrum.perun.taskslib.model.ExecService)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1