Search in sources :

Example 86 with ServiceRegistryException

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

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

the class ServiceRegistryJpaImpl method activate.

public void activate(ComponentContext cc) {
    logger.info("Activate service registry");
    // Find this host's url
    if (cc == null || StringUtils.isBlank(cc.getBundleContext().getProperty(OpencastConstants.SERVER_URL_PROPERTY))) {
        hostName = UrlSupport.DEFAULT_BASE_URL;
    } else {
        hostName = cc.getBundleContext().getProperty(OpencastConstants.SERVER_URL_PROPERTY);
    }
    // Check hostname for sanity. It should be the hosts URL with protocol but without any part of the service paths.
    if (hostName.endsWith("/")) {
        logger.warn("The configured value of {} ends with '/'. This is very likely a configuration error which could " + "lead to services not working properly. Note that this configuration should not contain any part of " + "the service paths.", OpencastConstants.SERVER_URL_PROPERTY);
    }
    // Clean all undispatchable jobs that were orphaned when this host was last deactivated
    cleanUndispatchableJobs(hostName);
    // Register JMX beans with statistics
    try {
        List<ServiceStatistics> serviceStatistics = getServiceStatistics();
        hostsStatistics = new HostsStatistics(serviceStatistics);
        servicesStatistics = new ServicesStatistics(hostName, serviceStatistics);
        jobsStatistics = new JobsStatistics(hostName);
        jmxBeans.add(JmxUtil.registerMXBean(hostsStatistics, JMX_HOSTS_STATISTICS_TYPE));
        jmxBeans.add(JmxUtil.registerMXBean(servicesStatistics, JMX_SERVICES_STATISTICS_TYPE));
        jmxBeans.add(JmxUtil.registerMXBean(jobsStatistics, JMX_JOBS_STATISTICS_TYPE));
    } catch (ServiceRegistryException e) {
        logger.error("Error registering JMX statistic beans", e);
    }
    // Find the jobs URL
    if (cc == null || StringUtils.isBlank(cc.getBundleContext().getProperty("org.opencastproject.jobs.url"))) {
        jobHost = hostName;
    } else {
        jobHost = cc.getBundleContext().getProperty("org.opencastproject.jobs.url");
    }
    // Register this host
    try {
        float maxLoad = Runtime.getRuntime().availableProcessors();
        if (cc != null && StringUtils.isNotBlank(cc.getBundleContext().getProperty(OPT_MAXLOAD))) {
            try {
                maxLoad = Float.parseFloat(cc.getBundleContext().getProperty(OPT_MAXLOAD));
                logger.info("Max load has been manually to {}", maxLoad);
            } catch (NumberFormatException e) {
                logger.warn("Configuration key '{}' is not an integer. Falling back to the number of cores ({})", OPT_MAXLOAD, maxLoad);
            }
        }
        logger.info("Node maximum load set to {}", maxLoad);
        String address = InetAddress.getByName(URI.create(hostName).getHost()).getHostAddress();
        long maxMemory = Runtime.getRuntime().maxMemory();
        int cores = Runtime.getRuntime().availableProcessors();
        registerHost(hostName, address, maxMemory, cores, maxLoad);
    } catch (Exception e) {
        throw new IllegalStateException("Unable to register host " + hostName + " in the service registry", e);
    }
    // Track any services from this host that need to be added to the service registry
    if (cc != null) {
        try {
            tracker = new RestServiceTracker(cc.getBundleContext());
            tracker.open(true);
        } catch (InvalidSyntaxException e) {
            logger.error("Invalid filter syntax:", e);
            throw new IllegalStateException(e);
        }
    }
    // Whether a service accepts a job whose load exceeds the host’s max load
    if (cc != null) {
        acceptJobLoadsExeedingMaxLoad = getOptContextProperty(cc, ACCEPT_JOB_LOADS_EXCEEDING_PROPERTY).map(Strings.toBool).getOrElse(DEFAULT_ACCEPT_JOB_LOADS_EXCEEDING);
    }
    systemLoad = getHostLoads(emf.createEntityManager());
}
Also used : JobsStatistics(org.opencastproject.serviceregistry.impl.jmx.JobsStatistics) HostsStatistics(org.opencastproject.serviceregistry.impl.jmx.HostsStatistics) ServicesStatistics(org.opencastproject.serviceregistry.impl.jmx.ServicesStatistics) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) 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) ServiceStatistics(org.opencastproject.serviceregistry.api.ServiceStatistics) JaxbServiceStatistics(org.opencastproject.serviceregistry.api.JaxbServiceStatistics) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Example 88 with ServiceRegistryException

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

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

the class ServiceRegistryJpaImpl method updateJob.

private JpaJob updateJob(JpaJob job) throws ServiceRegistryException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        Job oldJob = getJob(job.getId());
        JpaJob jpaJob = updateInternal(em, job);
        if (!TYPE_WORKFLOW.equals(job.getJobType()) && job.getJobLoad() > 0.0f && job.getProcessorServiceRegistration() != null && job.getProcessorServiceRegistration().equals(getRegistryHostname())) {
            processCachedLoadChange(job);
        }
        // All WorkflowService Jobs will be ignored
        if (oldJob.getStatus() != job.getStatus() && !TYPE_WORKFLOW.equals(job.getJobType())) {
            updateServiceForFailover(job);
        }
        return jpaJob;
    } catch (PersistenceException e) {
        throw new ServiceRegistryException(e);
    } catch (NotFoundException e) {
        throw new ServiceRegistryException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) PersistenceException(javax.persistence.PersistenceException) NotFoundException(org.opencastproject.util.NotFoundException) JpaJob.fnToJob(org.opencastproject.job.jpa.JpaJob.fnToJob) Job(org.opencastproject.job.api.Job) JpaJob(org.opencastproject.job.jpa.JpaJob) JpaJob(org.opencastproject.job.jpa.JpaJob) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Example 90 with ServiceRegistryException

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

the class ServiceRegistryJpaImpl method getJpaJob.

private JpaJob getJpaJob(long id) throws NotFoundException, ServiceRegistryException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        JpaJob jpaJob = em.find(JpaJob.class, id);
        if (jpaJob == null) {
            throw new NotFoundException("Job " + id + " not found");
        }
        // JPA's caches can be out of date if external changes (e.g. another node in the cluster) have been made to
        // this row in the database
        em.refresh(jpaJob);
        setJobUri(jpaJob);
        return jpaJob;
    } catch (Exception e) {
        if (e instanceof NotFoundException) {
            throw (NotFoundException) e;
        } else {
            throw new ServiceRegistryException(e);
        }
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) NotFoundException(org.opencastproject.util.NotFoundException) JpaJob(org.opencastproject.job.jpa.JpaJob) 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)

Aggregations

ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)99 NotFoundException (org.opencastproject.util.NotFoundException)61 ConfigurationException (org.osgi.service.cm.ConfigurationException)41 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)30 URISyntaxException (java.net.URISyntaxException)29 Job (org.opencastproject.job.api.Job)29 PersistenceException (javax.persistence.PersistenceException)26 RollbackException (javax.persistence.RollbackException)26 TrustedHttpClientException (org.opencastproject.security.api.TrustedHttpClientException)26 NoResultException (javax.persistence.NoResultException)25 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)25 IOException (java.io.IOException)24 ArrayList (java.util.ArrayList)24 EntityManager (javax.persistence.EntityManager)22 MediaPackage (org.opencastproject.mediapackage.MediaPackage)20 URI (java.net.URI)16 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)14 DistributionException (org.opencastproject.distribution.api.DistributionException)13 Attachment (org.opencastproject.mediapackage.Attachment)12 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)12