Search in sources :

Example 1 with NodeLoad

use of org.opencastproject.serviceregistry.api.SystemLoad.NodeLoad in project opencast by opencast.

the class ServiceRegistryJpaImpl method getHostLoads.

/**
 * Gets a map of hosts to the number of jobs currently loading that host
 *
 * @param em
 *          the entity manager
 *
 * @return the map of hosts to job counts
 */
SystemLoad getHostLoads(EntityManager em) {
    final SystemLoad systemLoad = new SystemLoad();
    // Find all jobs that are currently running on any given host, or get all of them
    Query q = em.createNamedQuery("ServiceRegistration.hostloads");
    List<Integer> statuses = new LinkedList<Integer>();
    for (Status status : JOB_STATUSES_INFLUENCING_LOAD_BALANCING) {
        statuses.add(status.ordinal());
    }
    q.setParameter("statuses", statuses);
    // Note: This is used in the query to filter out workflow jobs.
    // These jobs are load balanced by the workflow service directly.
    q.setParameter("workflow_type", TYPE_WORKFLOW);
    // Accumulate the numbers for relevant job statuses per host
    for (Object result : q.getResultList()) {
        Object[] resultArray = (Object[]) result;
        String host = String.valueOf(resultArray[0]);
        Status status = Status.values()[(int) resultArray[1]];
        float load = ((Number) resultArray[2]).floatValue();
        // Only queued, and running jobs are adding to the load, so every other status is discarded
        if (status == null || !JOB_STATUSES_INFLUENCING_LOAD_BALANCING.contains(status)) {
            load = 0.0f;
        }
        // Add the service registration
        NodeLoad serviceLoad;
        if (systemLoad.containsHost(host)) {
            serviceLoad = systemLoad.get(host);
            serviceLoad.setLoadFactor(serviceLoad.getLoadFactor() + load);
        } else {
            serviceLoad = new NodeLoad(host, load);
        }
        systemLoad.addNodeLoad(serviceLoad);
    }
    // This is important, otherwise services which have no current load are not listed in the output!
    for (HostRegistration h : getHostRegistrations(em)) {
        if (!systemLoad.containsHost(h.getBaseUrl())) {
            systemLoad.addNodeLoad(new NodeLoad(h.getBaseUrl(), 0.0f));
        }
    }
    return systemLoad;
}
Also used : HttpStatus(org.apache.http.HttpStatus) Status(org.opencastproject.job.api.Job.Status) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) SystemLoad(org.opencastproject.serviceregistry.api.SystemLoad) LinkedList(java.util.LinkedList) NodeLoad(org.opencastproject.serviceregistry.api.SystemLoad.NodeLoad) HostRegistration(org.opencastproject.serviceregistry.api.HostRegistration)

Example 2 with NodeLoad

use of org.opencastproject.serviceregistry.api.SystemLoad.NodeLoad in project opencast by opencast.

the class ServiceRegistryInMemoryImpl method getCurrentHostLoads.

@Override
public SystemLoad getCurrentHostLoads() {
    SystemLoad systemLoad = new SystemLoad();
    for (String host : hosts.keySet()) {
        NodeLoad node = new NodeLoad();
        node.setHost(host);
        for (ServiceRegistration service : services.get(host)) {
            if (service.isInMaintenanceMode() || !service.isOnline()) {
                continue;
            }
            Set<Job> hostJobs = jobHosts.get(service);
            float loadSum = 0.0f;
            if (hostJobs != null) {
                for (Job job : hostJobs) {
                    if (job.getStatus() != null && JOB_STATUSES_INFLUENCING_LOAD_BALANCING.contains(job.getStatus())) {
                        loadSum += job.getJobLoad();
                    }
                }
            }
            node.setLoadFactor(node.getLoadFactor() + loadSum);
        }
        systemLoad.addNodeLoad(node);
    }
    return systemLoad;
}
Also used : JaxbJob(org.opencastproject.job.api.JaxbJob) Job(org.opencastproject.job.api.Job) NodeLoad(org.opencastproject.serviceregistry.api.SystemLoad.NodeLoad)

Example 3 with NodeLoad

use of org.opencastproject.serviceregistry.api.SystemLoad.NodeLoad in project opencast by opencast.

the class ServiceRegistryJpaImpl method getMaxLoadOnNode.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.serviceregistry.api.ServiceRegistry#getMaxLoadOnNode(java.lang.String)
 */
@Override
public NodeLoad getMaxLoadOnNode(String host) throws ServiceRegistryException, NotFoundException {
    Query query = null;
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        query = em.createNamedQuery("HostRegistration.getMaxLoadByHostName");
        query.setParameter("host", host);
        return new NodeLoad(host, ((Number) query.getSingleResult()).floatValue());
    } catch (NoResultException e) {
        throw new NotFoundException(e);
    } 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) NotFoundException(org.opencastproject.util.NotFoundException) 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) NodeLoad(org.opencastproject.serviceregistry.api.SystemLoad.NodeLoad)

Example 4 with NodeLoad

use of org.opencastproject.serviceregistry.api.SystemLoad.NodeLoad in project opencast by opencast.

the class ServiceRegistryJpaImpl method getMaxLoads.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.serviceregistry.api.ServiceRegistry#getMaxLoads()
 */
@Override
public SystemLoad getMaxLoads() throws ServiceRegistryException {
    final SystemLoad loads = new SystemLoad();
    for (HostRegistration host : getHostRegistrations()) {
        NodeLoad load = new NodeLoad(host.getBaseUrl(), host.getMaxLoad());
        loads.addNodeLoad(load);
    }
    return loads;
}
Also used : HostRegistration(org.opencastproject.serviceregistry.api.HostRegistration) SystemLoad(org.opencastproject.serviceregistry.api.SystemLoad) NodeLoad(org.opencastproject.serviceregistry.api.SystemLoad.NodeLoad)

Example 5 with NodeLoad

use of org.opencastproject.serviceregistry.api.SystemLoad.NodeLoad in project opencast by opencast.

the class AbstractJobProducer method isReadyToAccept.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.job.api.JobProducer#isReadyToAccept(org.opencastproject.job.api.Job)
 */
@Override
public boolean isReadyToAccept(Job job) throws ServiceRegistryException, UndispatchableJobException {
    if (!jobType.equals(job.getJobType())) {
        logger.debug("Invalid job type submitted: {}", job.getJobType());
        return false;
    }
    NodeLoad maxload;
    try {
        maxload = getServiceRegistry().getMaxLoadOnNode(getServiceRegistry().getRegistryHostname());
    } catch (NotFoundException e) {
        throw new ServiceRegistryException(e);
    }
    // Note: We are not adding the job load in the next line because it is already accounted for in the load values we
    // get back from the service registry.
    float currentLoad = getServiceRegistry().getOwnLoad();
    /* Note that this first clause looks at the *job's*, the other two look at the *node's* load
     * We're assuming that if this case is true, then we're also the most powerful node in the system for this service,
     * per the current job dispatching code in ServiceRegistryJpaImpl */
    if (job.getJobLoad() > maxload.getLoadFactor() && acceptJobLoadsExeedingMaxLoad) {
        logger.warn("Accepting job {} of type {} with load {} even though load of {} is above this node's limit of {}.", new Object[] { job.getId(), job.getJobType(), df.format(job.getJobLoad()), df.format(currentLoad), df.format(maxload.getLoadFactor()) });
        logger.warn("This is a configuration issue that you should resolve in a production system!");
        return true;
    } else if (currentLoad > maxload.getLoadFactor()) {
        logger.debug("Declining job {} of type {} with load {} because load of {} would exceed this node's limit of {}.", new Object[] { job.getId(), job.getJobType(), df.format(job.getJobLoad()), df.format(currentLoad), df.format(maxload.getLoadFactor()) });
        return false;
    } else {
        logger.debug("Accepting job {} of type {} with load {} because load of {} is within this node's limit of {}.", new Object[] { job.getId(), job.getJobType(), df.format(job.getJobLoad()), df.format(currentLoad), df.format(maxload.getLoadFactor()) });
        return true;
    }
}
Also used : NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) NodeLoad(org.opencastproject.serviceregistry.api.SystemLoad.NodeLoad)

Aggregations

NodeLoad (org.opencastproject.serviceregistry.api.SystemLoad.NodeLoad)6 Query (javax.persistence.Query)2 TypedQuery (javax.persistence.TypedQuery)2 HostRegistration (org.opencastproject.serviceregistry.api.HostRegistration)2 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)2 SystemLoad (org.opencastproject.serviceregistry.api.SystemLoad)2 NotFoundException (org.opencastproject.util.NotFoundException)2 URISyntaxException (java.net.URISyntaxException)1 LinkedList (java.util.LinkedList)1 EntityManager (javax.persistence.EntityManager)1 NoResultException (javax.persistence.NoResultException)1 PersistenceException (javax.persistence.PersistenceException)1 RollbackException (javax.persistence.RollbackException)1 HttpStatus (org.apache.http.HttpStatus)1 JaxbJob (org.opencastproject.job.api.JaxbJob)1 Job (org.opencastproject.job.api.Job)1 Status (org.opencastproject.job.api.Job.Status)1 TrustedHttpClientException (org.opencastproject.security.api.TrustedHttpClientException)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1