use of cz.metacentrum.perun.taskslib.exceptions.TaskStoreException in project perun by CESNET.
the class EventProcessorImpl method receiveEvent.
@Override
public void receiveEvent(String event) {
log.debug("Event {} is going to be resolved.", event);
Task task = null;
try {
task = eventParser.parseEvent(event);
} catch (InvalidEventMessageException | InternalErrorException e) {
log.error(e.toString());
}
if (task == null) {
log.debug("Task not found in event {}", event);
return;
}
task.setStatus(Task.TaskStatus.PLANNED);
log.info("Current pool size BEFORE event processing: {}", schedulingPool.getSize());
log.debug("\t Resolved Facility[{}]", task.getFacility());
log.debug("\t Resolved Service[{}]", task.getService());
if (task.getFacility() != null && task.getService() != null) {
log.debug("[{}] Check if Task exist in SchedulingPool: {}", task.getId(), task);
Task currentTask = schedulingPool.getTask(task.getId());
if (currentTask == null) {
log.debug("[{}] Task not found in SchedulingPool.", task.getId());
try {
schedulingPool.addTask(task);
} catch (TaskStoreException e) {
log.error("Could not save Task {} into Engine SchedulingPool because of {}, it will be ignored", task, e);
// FIXME - should probably report ERROR back to dispatcher...
}
} else {
// since we always remove Task from pool at the end and Dispatcher doesn't send partial Destinations,
// we don't need to update existing Task object !! Let engine finish the processing.
log.debug("[{}] Task found in SchedulingPool, message skipped.", task.getId(), currentTask);
}
}
log.debug("[{}] POOL SIZE: {}", task.getId(), schedulingPool.getSize());
log.info("[{}] Current pool size AFTER event processing: {}", task.getId(), schedulingPool.getSize());
}
use of cz.metacentrum.perun.taskslib.exceptions.TaskStoreException in project perun by CESNET.
the class PropagationMaintainerImpl method abortTask.
private void abortTask(Task task, TaskStatus status) {
log.warn("[{}] Task {} found in unexpected state, switching to {} ", task.getId(), task, status);
task.setStatus(status);
Task removed = null;
try {
removed = schedulingPool.removeTask(task);
// the removal from pool also cancels all task futures, which in turn
// makes the completion service collect the task and remove it from its executingTasks map
} catch (TaskStoreException e) {
log.error("[{}] Failed during removal of Task {} from SchedulingPool: {}", task.getId(), task, e);
}
if (removed != null) {
// some kind of inconsistency and we don't want to spam dispatcher - it will mark it as error on its own
try {
jmsQueueManager.reportTaskStatus(task.getId(), task.getStatus(), System.currentTimeMillis());
} catch (JMSException | InterruptedException e) {
log.error("[{}] Error trying to reportTaskStatus of {} to Dispatcher: {}", task.getId(), task, e);
}
} else {
log.error("[{}] Stale Task {} was not removed and not reported to dispatcher.", task.getId(), task);
log.error(" - This is nonsense - why we abort the Task taken from AllTasks but we can remove it from it ??");
}
}
use of cz.metacentrum.perun.taskslib.exceptions.TaskStoreException in project perun by CESNET.
the class SchedulingPoolImpl method reloadTasks.
@Override
public void reloadTasks() {
log.debug("Going to reload Tasks from database...");
this.clear();
EngineMessageProducer queue = engineMessageProducerFactory.getProducer();
for (Task task : tasksManagerBl.listAllTasks(sess)) {
try {
// just add DB Task to in-memory structure
addToPool(task);
} catch (TaskStoreException e) {
log.error("Adding Task {} and Queue {} into SchedulingPool failed, so the Task will be lost.", task, queue);
}
// done/error tasks will be rescheduled later by periodic jobs of PropagationMaintainer !!
if (Arrays.asList(TaskStatus.WAITING, TaskStatus.PLANNED, TaskStatus.GENERATING, TaskStatus.SENDING).contains(task.getStatus())) {
if (task.getStatus().equals(TaskStatus.WAITING)) {
// reset timestamp to 'now' for WAITING Tasks, since scheduling task
// sets this to all tasks except the waiting
task.setSchedule(LocalDateTime.now());
tasksManagerBl.updateTask(sess, task);
}
scheduleTask(task, 0);
}
}
log.debug("Reload of Tasks from database finished.");
}
use of cz.metacentrum.perun.taskslib.exceptions.TaskStoreException in project perun by CESNET.
the class SchedulingPoolImpl method scheduleTask.
@Override
public void scheduleTask(Task task, int delayCount) {
// check if service/facility exists
boolean removeTask = false;
try {
Service service = perun.getServicesManager().getServiceById(sess, task.getServiceId());
Facility facility = perun.getFacilitiesManager().getFacilityById(sess, task.getFacilityId());
task.setService(service);
task.setFacility(facility);
} catch (ServiceNotExistsException e) {
log.error("[{}] Task NOT added to waiting queue, service not exists: {}.", task.getId(), task);
removeTask = true;
} catch (FacilityNotExistsException e) {
log.error("[{}] Task NOT added to waiting queue, facility not exists: {}.", task.getId(), task);
removeTask = true;
} catch (InternalErrorException | PrivilegeException e) {
log.error("[{}] {}", task.getId(), e);
}
if (!task.getService().isEnabled() || ((PerunBl) perun).getServicesManagerBl().isServiceBlockedOnFacility(task.getService(), task.getFacility())) {
log.error("[{}] Task NOT added to waiting queue, service is blocked: {}.", task.getId(), task);
// do not change Task status or any other data !
if (!removeTask)
return;
}
try {
List<Destination> destinations = perun.getServicesManager().getDestinations(sess, task.getService(), task.getFacility());
if (destinations != null && !destinations.isEmpty()) {
Iterator<Destination> iter = destinations.iterator();
while (iter.hasNext()) {
Destination dest = iter.next();
if (((PerunBl) perun).getServicesManagerBl().isServiceBlockedOnDestination(task.getService(), dest.getId())) {
iter.remove();
}
}
if (destinations.isEmpty()) {
// All service destinations were blocked -> Task is denied to be sent to engine just like
// when service is blocked globally in Perun or on facility as a whole.
log.debug("[{}] Task NOT added to waiting queue, all its destinations are blocked.", task.getId());
if (!removeTask)
return;
}
}
} catch (ServiceNotExistsException e) {
log.error("[{}] Task NOT added to waiting queue, service not exists: {}.", task.getId(), task);
removeTask = true;
} catch (FacilityNotExistsException e) {
log.error("[{}] Task NOT added to waiting queue, facility not exists: {}.", task.getId(), task);
removeTask = true;
} catch (InternalErrorException | PrivilegeException e) {
log.error("[{}] {}", task.getId(), e);
}
try {
List<Service> assignedServices = perun.getServicesManager().getAssignedServices(sess, task.getFacility());
if (!assignedServices.contains(task.getService())) {
log.debug("[{}] Task NOT added to waiting queue, service is not assigned to facility any more: {}.", task.getId(), task);
if (!removeTask)
return;
}
} catch (FacilityNotExistsException e) {
removeTask = true;
log.error("[{}] Task removed from database, facility no longer exists: {}.", task.getId(), task);
} catch (InternalErrorException | PrivilegeException e) {
log.error("[{}] Unable to check Service assignment to Facility: {}", task.getId(), e.getMessage());
}
if (removeTask) {
// in memory task belongs to non existent facility/service - remove it and return
try {
removeTask(task);
return;
} catch (TaskStoreException e) {
log.error("[{}] Unable to remove Task from pool: {}.", task.getId(), e);
return;
}
}
// Task is eligible for running - create new schedule
task.setSourceUpdated(false);
long newTaskDelay = 0;
if (!task.isPropagationForced()) {
// normal tasks are delayed
try {
newTaskDelay = Long.parseLong(dispatcherProperties.getProperty("dispatcher.task.delay.time"));
} catch (NumberFormatException e) {
log.warn("Could not parse value of dispatcher.task.delay.time property. Using default.");
newTaskDelay = 30000;
}
}
if (task.isPropagationForced()) {
delayCount = 0;
}
if (delayCount < 0) {
try {
delayCount = Integer.parseInt(dispatcherProperties.getProperty("dispatcher.task.delay.count"));
} catch (NumberFormatException e) {
log.warn("Could not parse value of dispatcher.task.delay.count property. Using default.");
delayCount = 4;
}
}
TaskSchedule schedule = new TaskSchedule(newTaskDelay, task);
schedule.setBase(System.currentTimeMillis());
schedule.setDelayCount(delayCount);
// Task was newly planned for propagation, switch state.
if (!task.getStatus().equals(TaskStatus.WAITING)) {
task.setStatus(TaskStatus.WAITING);
task.setSchedule(LocalDateTime.now());
// clear previous timestamps
task.setSentToEngine((LocalDateTime) null);
task.setStartTime((LocalDateTime) null);
task.setGenStartTime((LocalDateTime) null);
task.setSendStartTime((LocalDateTime) null);
task.setEndTime((LocalDateTime) null);
task.setGenEndTime((LocalDateTime) null);
task.setSendEndTime((LocalDateTime) null);
tasksManagerBl.updateTask(sess, task);
}
boolean added = false;
if (schedule.getTask().isPropagationForced()) {
added = waitingForcedTasksQueue.add(schedule);
} else {
added = waitingTasksQueue.add(schedule);
}
if (!added) {
log.error("[{}] Task could not be added to waiting queue. Shouldn't ever happen. Look to javadoc of DelayQueue. {}", task.getId(), schedule);
} else {
log.debug("[{}] Task was added to waiting queue: {}", task.getId(), schedule);
}
}
use of cz.metacentrum.perun.taskslib.exceptions.TaskStoreException in project perun by CESNET.
the class EventProcessor method createTaskFromEvent.
/**
* Creates Task from Event data. Tries to resolve Service and Facility pairs from Event.
* Events for non existing entities are discarded.
*
* @param event Event to parse
* @throws ServiceNotExistsException When Service from Event doesn't exists anymore
* @throws InvalidEventMessageException When Message has invalid format.
* @throws InternalErrorException When implementation fails
* @throws PrivilegeException When dispatcher lack privileges to call core methods
*/
private void createTaskFromEvent(Event event) throws ServiceNotExistsException, InvalidEventMessageException, PrivilegeException {
Map<Facility, Set<Service>> resolvedServices = eventServiceResolver.resolveEvent(event.getData());
for (Entry<Facility, Set<Service>> map : resolvedServices.entrySet()) {
Facility facility = map.getKey();
for (Service service : map.getValue()) {
if (!service.isEnabled()) {
log.debug("Service not enabled: {}.", service);
continue;
}
if (((PerunBl) perun).getServicesManagerBl().isServiceBlockedOnFacility(service, facility)) {
log.debug("Service blocked on Facility: {} , {}.", service, facility);
continue;
}
// Check if all destinations are not blocked
try {
// init session
try {
if (sess == null) {
sess = perun.getPerunSession(new PerunPrincipal(dispatcherProperties.getProperty("perun.principal.name"), dispatcherProperties.getProperty("perun.principal.extSourceName"), dispatcherProperties.getProperty("perun.principal.extSourceType")), new PerunClient());
}
} catch (InternalErrorException e1) {
log.error("Error establishing perun session to create Task from Event: ", e1);
continue;
}
List<Destination> destinations = perun.getServicesManager().getDestinations(sess, service, facility);
if (destinations != null && !destinations.isEmpty()) {
Iterator<Destination> iter = destinations.iterator();
while (iter.hasNext()) {
Destination dest = iter.next();
if (((PerunBl) perun).getServicesManagerBl().isServiceBlockedOnDestination(service, dest.getId())) {
iter.remove();
}
}
if (destinations.isEmpty()) {
// All service destinations were blocked -> Task is denied to be sent to engine just like
// when service is blocked globally in Perun or on facility as a whole.
log.debug("{} blocked on all destinations on {}.", service, facility);
continue;
}
}
} catch (ServiceNotExistsException e) {
log.error("Service not exist: {}.", service);
} catch (FacilityNotExistsException e) {
log.error("Facility not exist: {}.", facility);
} catch (InternalErrorException | PrivilegeException e) {
log.error("{}", e);
}
// check for presence of task for this <Service, Facility> pair
// NOTE: this must be atomic enough to not create duplicate
// tasks in schedulingPool (are we running in parallel
// here?)
boolean isForced = determineForcedPropagation(event);
Task task = schedulingPool.getTask(facility, service);
if (task != null) {
// there already is a task in schedulingPool
// signal that task needs to regenerate data and be forced next time
task.setDestinations(null);
task.setSourceUpdated(true);
if (isForced)
task.setPropagationForced(true);
task.setRecurrence(0);
log.debug("[{}] Task is already in pool. Re-setting source updated and forced flags, {}.", task.getId(), task);
} else {
// no such task yet, create one
task = new Task();
task.setFacility(facility);
task.setService(service);
task.setStatus(TaskStatus.WAITING);
task.setRecurrence(0);
task.setDelay(service.getDelay());
task.setSchedule(LocalDateTime.now());
task.setSourceUpdated(false);
task.setPropagationForced(isForced);
try {
schedulingPool.addToPool(task);
log.debug("[{}] New Task added to pool. {}.", task.getId(), task);
} catch (TaskStoreException e) {
log.error("[{}] Could not add Task to pool. Task {} will be lost: {}", task.getId(), task, e);
}
schedulingPool.scheduleTask(task, -1);
}
}
}
}
Aggregations