Search in sources :

Example 11 with ServiceRegistrationJpaImpl

use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl in project opencast by opencast.

the class ServiceRegistryJpaImpl method updateServiceForFailover.

/**
 * Update the jobs failure history and the service status with the given information. All these data are then use for
 * the jobs failover strategy. Only the terminated job (with FAILED or FINISHED status) are taken into account.
 *
 * @param job
 *          the current job that failed/succeeded
 * @throws ServiceRegistryException
 * @throws IllegalArgumentException
 */
private void updateServiceForFailover(JpaJob job) throws IllegalArgumentException, ServiceRegistryException {
    if (job.getStatus() != Status.FAILED && job.getStatus() != Status.FINISHED)
        return;
    job.setStatus(job.getStatus(), job.getFailureReason());
    // At this point, the only possible states for the current service are NORMAL and WARNING,
    // the services in ERROR state will not be chosen by the dispatcher
    ServiceRegistrationJpaImpl currentService = job.getProcessorServiceRegistration();
    if (currentService == null)
        return;
    EntityManager em = emf.createEntityManager();
    try {
        em = emf.createEntityManager();
        // Job is finished with a failure
        if (job.getStatus() == FAILED && !DATA.equals(job.getFailureReason())) {
            // Services in WARNING or ERROR state triggered by current job
            List<ServiceRegistrationJpaImpl> relatedWarningOrErrorServices = getRelatedWarningErrorServices(job);
            // Before this job failed there was at least one job failed with this job signature on any service
            if (relatedWarningOrErrorServices.size() > 0) {
                for (ServiceRegistrationJpaImpl relatedService : relatedWarningOrErrorServices) {
                    // Skip current service from the list
                    if (currentService.equals(relatedService))
                        continue;
                    // Reset the WARNING job to NORMAL
                    if (relatedService.getServiceState() == WARNING) {
                        logger.info("State reset to NORMAL for related service {} on host {}", relatedService.getServiceType(), relatedService.getHost());
                        relatedService.setServiceState(NORMAL, job.toJob().getSignature());
                    } else // Reset the ERROR job to WARNING
                    if (relatedService.getServiceState() == ERROR) {
                        logger.info("State reset to WARNING for related service {} on host {}", relatedService.getServiceType(), relatedService.getHost());
                        relatedService.setServiceState(WARNING, relatedService.getWarningStateTrigger());
                    }
                    updateServiceState(em, relatedService);
                }
            } else // This is the first job with this signature failing on any service
            {
                // Set the current service to WARNING state
                if (currentService.getServiceState() == NORMAL) {
                    logger.info("State set to WARNING for current service {} on host {}", currentService.getServiceType(), currentService.getHost());
                    currentService.setServiceState(WARNING, job.toJob().getSignature());
                    updateServiceState(em, currentService);
                } else // The current service already is in WARNING state and max attempts is reached
                if (getHistorySize(currentService) >= maxAttemptsBeforeErrorState) {
                    logger.info("State set to ERROR for current service {} on host {}", currentService.getServiceType(), currentService.getHost());
                    currentService.setServiceState(ERROR, job.toJob().getSignature());
                    updateServiceState(em, currentService);
                }
            }
        } else // Job is finished without failure
        if (job.getStatus() == Status.FINISHED) {
            // If the service was in warning state reset to normal state
            if (currentService.getServiceState() == WARNING) {
                logger.info("State reset to NORMAL for current service {} on host {}", currentService.getServiceType(), currentService.getHost());
                currentService.setServiceState(NORMAL);
                updateServiceState(em, currentService);
            }
            // Services in WARNING state triggered by current job
            List<ServiceRegistrationJpaImpl> relatedWarningServices = getRelatedWarningServices(job);
            // Sets all related services to error state
            for (ServiceRegistrationJpaImpl relatedService : relatedWarningServices) {
                logger.info("State set to ERROR for related service {} on host {}", currentService.getServiceType(), currentService.getHost());
                relatedService.setServiceState(ERROR, job.toJob().getSignature());
                updateServiceState(em, relatedService);
            }
        }
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl)

Example 12 with ServiceRegistrationJpaImpl

use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl in project opencast by opencast.

the class ServiceRegistryJpaImpl method enableHost.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.serviceregistry.api.ServiceRegistry#enableHost(String)
 */
@Override
public void enableHost(String host) throws ServiceRegistryException, NotFoundException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        // Find the existing registrations for this host and if it exists, update it
        HostRegistrationJpaImpl hostRegistration = fetchHostRegistration(em, host);
        if (hostRegistration == null) {
            throw new NotFoundException("Host '" + host + "' is currently not registered, so it can not be enabled");
        } else {
            hostRegistration.setActive(true);
            em.merge(hostRegistration);
        }
        logger.info("Enabling {}", host);
        tx.commit();
        tx.begin();
        for (ServiceRegistration serviceRegistration : getServiceRegistrationsByHost(host)) {
            ServiceRegistrationJpaImpl registration = (ServiceRegistrationJpaImpl) serviceRegistration;
            registration.setActive(true);
            em.merge(registration);
            servicesStatistics.updateService(registration);
        }
        tx.commit();
        hostsStatistics.updateHost(hostRegistration);
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        if (tx != null && tx.isActive()) {
            tx.rollback();
        }
        throw new ServiceRegistryException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl) HostRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.HostRegistrationJpaImpl) URISyntaxException(java.net.URISyntaxException) NoResultException(javax.persistence.NoResultException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration)

Example 13 with ServiceRegistrationJpaImpl

use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl in project opencast by opencast.

the class ServiceRegistryJpaImpl method getServiceStatistics.

/**
 * Gets performance and runtime statistics for each known service registration.
 * For the statistics, only jobs created within the time interval [startDate, endDate] are being considered
 *
 * @param startDate
 *          Only jobs created after this data are considered for statistics
 * @param endDate
 *          Only jobs created before this data are considered for statistics
 * @return the service statistics
 * @throws ServiceRegistryException
 *           if there is a problem accessing the service registry
 */
private List<ServiceStatistics> getServiceStatistics(Date startDate, Date endDate) throws ServiceRegistryException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        Map<Long, JaxbServiceStatistics> statsMap = new HashMap<Long, JaxbServiceStatistics>();
        // Make sure we also include the services that have no processing history so far
        List<ServiceRegistrationJpaImpl> services = em.createNamedQuery("ServiceRegistration.getAll").getResultList();
        for (ServiceRegistrationJpaImpl s : services) {
            statsMap.put(s.getId(), new JaxbServiceStatistics(s));
        }
        Query query = em.createNamedQuery("ServiceRegistration.statistics");
        query.setParameter("minDateCreated", startDate, TemporalType.TIMESTAMP);
        query.setParameter("maxDateCreated", endDate, TemporalType.TIMESTAMP);
        List queryResults = query.getResultList();
        for (Object result : queryResults) {
            Object[] oa = (Object[]) result;
            Number serviceRegistrationId = ((Number) oa[0]);
            if (serviceRegistrationId == null || serviceRegistrationId.longValue() == 0)
                continue;
            Status status = Status.values()[((Number) oa[1]).intValue()];
            Number count = (Number) oa[2];
            Number meanQueueTime = (Number) oa[3];
            Number meanRunTime = (Number) oa[4];
            // The statistics query returns a cartesian product, so we need to iterate over them to build up the objects
            JaxbServiceStatistics stats = statsMap.get(serviceRegistrationId.longValue());
            if (stats == null)
                continue;
            // the status will be null if there are no jobs at all associated with this service registration
            if (status != null) {
                switch(status) {
                    case RUNNING:
                        stats.setRunningJobs(count.intValue());
                        break;
                    case QUEUED:
                    case DISPATCHING:
                        stats.setQueuedJobs(count.intValue());
                        break;
                    case FINISHED:
                        stats.setMeanRunTime(meanRunTime.longValue());
                        stats.setMeanQueueTime(meanQueueTime.longValue());
                        stats.setFinishedJobs(count.intValue());
                        break;
                    default:
                        break;
                }
            }
        }
        List<ServiceStatistics> stats = new ArrayList<ServiceStatistics>(statsMap.values());
        Collections.sort(stats, new Comparator<ServiceStatistics>() {

            @Override
            public int compare(ServiceStatistics o1, ServiceStatistics o2) {
                ServiceRegistration reg1 = o1.getServiceRegistration();
                ServiceRegistration reg2 = o2.getServiceRegistration();
                int typeComparison = reg1.getServiceType().compareTo(reg2.getServiceType());
                return typeComparison == 0 ? reg1.getHost().compareTo(reg2.getHost()) : typeComparison;
            }
        });
        return stats;
    } catch (Exception e) {
        throw new ServiceRegistryException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : HttpStatus(org.apache.http.HttpStatus) Status(org.opencastproject.job.api.Job.Status) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl) URISyntaxException(java.net.URISyntaxException) NoResultException(javax.persistence.NoResultException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) ServiceStatistics(org.opencastproject.serviceregistry.api.ServiceStatistics) JaxbServiceStatistics(org.opencastproject.serviceregistry.api.JaxbServiceStatistics) EntityManager(javax.persistence.EntityManager) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) JaxbServiceStatistics(org.opencastproject.serviceregistry.api.JaxbServiceStatistics) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration)

Example 14 with ServiceRegistrationJpaImpl

use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl in project opencast by opencast.

the class ServiceRegistryJpaImpl method getRelatedWarningErrorServices.

/**
 * Gets the services in WARNING or ERROR state triggered by this job
 *
 * @param job
 *          the given job to get the related services
 * @return a list of services triggered by the job
 * @throws IllegalArgumentException
 *           if the given job was null
 * @throws ServiceRegistryException
 *           if the there was a problem with the query
 */
private List<ServiceRegistrationJpaImpl> getRelatedWarningErrorServices(JpaJob job) throws ServiceRegistryException {
    if (job == null)
        throw new IllegalArgumentException("job must not be null!");
    Query query = null;
    EntityManager em = null;
    logger.debug("Try to get the services in WARNING or ERROR state triggered by this job {} failed", job.toJob().getSignature());
    try {
        em = emf.createEntityManager();
        // TODO: modify the query to avoid to go through the list here
        query = em.createNamedQuery("ServiceRegistration.relatedservices.warning_error");
        query.setParameter("serviceType", job.getJobType());
        List<ServiceRegistrationJpaImpl> jpaServices = new ArrayList<ServiceRegistrationJpaImpl>();
        @SuppressWarnings("unchecked") List<ServiceRegistrationJpaImpl> serviceResults = query.getResultList();
        for (ServiceRegistrationJpaImpl relatedService : serviceResults) {
            if (relatedService.getServiceState() == WARNING && relatedService.getWarningStateTrigger() == job.toJob().getSignature()) {
                jpaServices.add(relatedService);
            }
            if (relatedService.getServiceState() == ERROR && relatedService.getErrorStateTrigger() == job.toJob().getSignature()) {
                jpaServices.add(relatedService);
            }
        }
        return jpaServices;
    } catch (NoResultException e) {
        return null;
    } catch (Exception e) {
        throw new ServiceRegistryException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) ArrayList(java.util.ArrayList) ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl) NoResultException(javax.persistence.NoResultException) URISyntaxException(java.net.URISyntaxException) NoResultException(javax.persistence.NoResultException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Example 15 with ServiceRegistrationJpaImpl

use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl in project opencast by opencast.

the class ServiceRegistryJpaImpl method sanitize.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.serviceregistry.api.ServiceRegistry#sanitize(java.lang.String, java.lang.String)
 */
@Override
public void sanitize(String serviceType, String host) throws NotFoundException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        ServiceRegistrationJpaImpl service = getServiceRegistration(em, serviceType, host);
        if (service == null)
            throw new NotFoundException("");
        logger.info("State reset to NORMAL for service {} on host {} through santize method", service.getServiceType(), service.getHost());
        service.setServiceState(NORMAL);
        updateServiceState(em, service);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl)

Aggregations

ServiceRegistrationJpaImpl (org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl)16 EntityManager (javax.persistence.EntityManager)9 NoResultException (javax.persistence.NoResultException)8 NotFoundException (org.opencastproject.util.NotFoundException)8 PersistenceException (javax.persistence.PersistenceException)7 RollbackException (javax.persistence.RollbackException)7 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)7 URISyntaxException (java.net.URISyntaxException)6 Job (org.opencastproject.job.api.Job)6 TrustedHttpClientException (org.opencastproject.security.api.TrustedHttpClientException)6 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)6 ConfigurationException (org.osgi.service.cm.ConfigurationException)6 ArrayList (java.util.ArrayList)5 EntityTransaction (javax.persistence.EntityTransaction)5 Query (javax.persistence.Query)4 TypedQuery (javax.persistence.TypedQuery)4 LinkedList (java.util.LinkedList)3 Test (org.junit.Test)3 ServiceRegistration (org.opencastproject.serviceregistry.api.ServiceRegistration)3 HostRegistrationJpaImpl (org.opencastproject.serviceregistry.impl.jpa.HostRegistrationJpaImpl)3