Search in sources :

Example 16 with ServiceRegistration

use of org.opencastproject.serviceregistry.api.ServiceRegistration in project opencast by opencast.

the class ServiceRegistryJpaImpl method getServiceRegistrationsByLoad.

/**
 * Returns a filtered list of service registrations, containing only those that are online, not in maintenance mode,
 * and with a specific service type, ordered by load.
 *
 * @param jobType
 *          the job type for which the services registrations are filtered
 * @param serviceRegistrations
 *          the complete list of service registrations
 * @param hostRegistrations
 *          the complete list of available host registrations
 * @param systemLoad
 */
protected List<ServiceRegistration> getServiceRegistrationsByLoad(String jobType, List<ServiceRegistration> serviceRegistrations, List<HostRegistration> hostRegistrations, final SystemLoad systemLoad) {
    final List<String> hostBaseUrls = $(hostRegistrations).map(toBaseUrl).toList();
    final List<ServiceRegistration> filteredList = new ArrayList<ServiceRegistration>();
    logger.debug("Finding services to dispatch job of type {}", jobType);
    for (ServiceRegistration service : serviceRegistrations) {
        // Skip service if host not available
        if (!hostBaseUrls.contains(service.getHost())) {
            logger.trace("Not considering {} because it's host {} is not available for dispatching", service, service.getHost());
            continue;
        }
        // Skip services that are not of the requested type
        if (!jobType.equals(service.getServiceType())) {
            logger.trace("Not considering {} because it is of the wrong job type", service);
            continue;
        }
        // Skip services that are in error state
        if (service.getServiceState() == ERROR) {
            logger.trace("Not considering {} because it is in error state", service);
            continue;
        }
        // Skip services that are in maintenance mode
        if (service.isInMaintenanceMode()) {
            logger.trace("Not considering {} because it is in maintenance mode", service);
            continue;
        }
        // Skip services that are marked as offline
        if (!service.isOnline()) {
            logger.trace("Not considering {} because it is currently offline", service);
            continue;
        }
        // We found a candidate service
        logger.debug("Adding candidate service {} for processing of job of type '{}'", service, jobType);
        filteredList.add(service);
    }
    // Sort the list by capacity
    Collections.sort(filteredList, new LoadComparator(systemLoad));
    return filteredList;
}
Also used : ArrayList(java.util.ArrayList) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration)

Example 17 with ServiceRegistration

use of org.opencastproject.serviceregistry.api.ServiceRegistration in project opencast by opencast.

the class ServiceRegistryJpaImpl method disableHost.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.serviceregistry.api.ServiceRegistry#disableHost(String)
 */
@Override
public void disableHost(String host) throws ServiceRegistryException, NotFoundException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        HostRegistrationJpaImpl hostRegistration = fetchHostRegistration(em, host);
        if (hostRegistration == null) {
            throw new NotFoundException("Host '" + host + "' is not currently registered, so it can not be disabled");
        } else {
            hostRegistration.setActive(false);
            for (ServiceRegistration serviceRegistration : getServiceRegistrationsByHost(host)) {
                ServiceRegistrationJpaImpl registration = (ServiceRegistrationJpaImpl) serviceRegistration;
                registration.setActive(false);
                em.merge(registration);
                servicesStatistics.updateService(registration);
            }
            em.merge(hostRegistration);
        }
        logger.info("Disabling {}", host);
        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 18 with ServiceRegistration

use of org.opencastproject.serviceregistry.api.ServiceRegistration 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 19 with ServiceRegistration

use of org.opencastproject.serviceregistry.api.ServiceRegistration in project opencast by opencast.

the class ServiceRegistryJpaImpl method getServiceRegistrationsByLoad.

/**
 * Do not look at this, it will burn your eyes! This is due to JPA's inability to do a left outer join with join
 * conditions.
 *
 * {@inheritDoc}
 *
 * @see org.opencastproject.serviceregistry.api.ServiceRegistry#getServiceRegistrationsByLoad(java.lang.String)
 */
@Override
public List<ServiceRegistration> getServiceRegistrationsByLoad(String serviceType) throws ServiceRegistryException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        SystemLoad loadByHost = getHostLoads(em);
        List<HostRegistration> hostRegistrations = getHostRegistrations();
        List<ServiceRegistration> serviceRegistrations = getServiceRegistrationsByType(serviceType);
        return getServiceRegistrationsByLoad(serviceType, serviceRegistrations, hostRegistrations, loadByHost);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) HostRegistration(org.opencastproject.serviceregistry.api.HostRegistration) SystemLoad(org.opencastproject.serviceregistry.api.SystemLoad) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration)

Example 20 with ServiceRegistration

use of org.opencastproject.serviceregistry.api.ServiceRegistration 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)

Aggregations

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