Search in sources :

Example 1 with SystemLoad

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

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

the class ServiceRegistrationTest method testServiceRegistrationsByLoad.

@Test
public void testServiceRegistrationsByLoad() throws Exception {
    List<ServiceRegistration> services = serviceRegistry.getServiceRegistrations();
    List<HostRegistration> hosts = serviceRegistry.getHostRegistrations();
    SystemLoad hostLoads = serviceRegistry.getHostLoads(serviceRegistry.emf.createEntityManager());
    List<ServiceRegistration> availableServices = serviceRegistry.getServiceRegistrationsByLoad(JOB_TYPE_1, services, hosts, hostLoads);
    // Make sure all hosts are available for processing
    Assert.assertEquals(3, availableServices.size());
    // Create a job and mark it as running.
    Job job = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, false, null);
    job.setStatus(Job.Status.RUNNING);
    job = serviceRegistry.updateJob(job);
    // Recalculate the number of available services
    hostLoads = serviceRegistry.getHostLoads(serviceRegistry.emf.createEntityManager());
    availableServices = serviceRegistry.getServiceRegistrationsByLoad(JOB_TYPE_1, services, hosts, hostLoads);
    // Since the host load is not taken into account, still all tree services should show up
    Assert.assertEquals(3, availableServices.size());
    // Recalculate the number of available services after ignoring a host
    hosts.remove(regType1Remotehost1.getHostRegistration());
    availableServices = serviceRegistry.getServiceRegistrationsByLoad(JOB_TYPE_1, services, hosts, hostLoads);
    // Since host 1 is now ignored, only two more services should show up
    Assert.assertEquals(2, availableServices.size());
}
Also used : HostRegistration(org.opencastproject.serviceregistry.api.HostRegistration) SystemLoad(org.opencastproject.serviceregistry.api.SystemLoad) Job(org.opencastproject.job.api.Job) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration) Test(org.junit.Test)

Example 3 with SystemLoad

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

the class ServiceRegistrationTest method testHostCapacity.

@Test
public void testHostCapacity() throws Exception {
    List<ServiceRegistration> services = serviceRegistry.getServiceRegistrations();
    List<HostRegistration> hosts = serviceRegistry.getHostRegistrations();
    SystemLoad hostLoads = serviceRegistry.getHostLoads(serviceRegistry.emf.createEntityManager());
    List<ServiceRegistration> availableServices = serviceRegistry.getServiceRegistrationsWithCapacity(JOB_TYPE_1, services, hosts, hostLoads);
    // Make sure all hosts are available for processing
    Assert.assertEquals(3, availableServices.size());
    // Create a job and mark it as running.
    Job job = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, false, null);
    job.setStatus(Job.Status.RUNNING);
    job = serviceRegistry.updateJob(job);
    // Recalculate the number of available services
    hostLoads = serviceRegistry.getHostLoads(serviceRegistry.emf.createEntityManager());
    availableServices = serviceRegistry.getServiceRegistrationsWithCapacity(JOB_TYPE_1, services, hosts, hostLoads);
    // Since host 1 is now maxed out, only two more services should show up
    Assert.assertEquals(2, availableServices.size());
    // Recalculate the number of available services after ignoring a host
    hosts.remove(regType1Remotehost1.getHostRegistration());
    availableServices = serviceRegistry.getServiceRegistrationsWithCapacity(JOB_TYPE_1, services, hosts, hostLoads);
    // Since remote host 1 is now ignored, only one more service should show up
    Assert.assertEquals(1, availableServices.size());
}
Also used : HostRegistration(org.opencastproject.serviceregistry.api.HostRegistration) SystemLoad(org.opencastproject.serviceregistry.api.SystemLoad) Job(org.opencastproject.job.api.Job) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration) Test(org.junit.Test)

Example 4 with SystemLoad

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

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

the class ServiceRegistryEndpoint method getMaxLoadOnNode.

@GET
@Path("maxload")
@Produces(MediaType.TEXT_XML)
@RestQuery(name = "maxload", description = "Returns the maximum load that servers in this service registry can execute concurrently.  " + "If there is only one server in this service registry this will be the maximum load that one server is able to handle at one time.  " + "If it is a distributed install across many servers then this number will be the maximum load the cluster can process concurrently.", returnDescription = "The maximum load of the cluster or server", restParameters = { @RestParameter(name = "host", isRequired = false, type = Type.STRING, description = "The host you want to know the maximum load for.") }, reponses = { @RestResponse(responseCode = SC_OK, description = "Maximum load for the cluster.") })
public Response getMaxLoadOnNode(@QueryParam("host") String host) throws NotFoundException {
    try {
        if (StringUtils.isEmpty(host)) {
            return Response.ok(serviceRegistry.getMaxLoads()).build();
        } else {
            SystemLoad systemLoad = new SystemLoad();
            systemLoad.addNodeLoad(serviceRegistry.getMaxLoadOnNode(host));
            return Response.ok(systemLoad).build();
        }
    } catch (ServiceRegistryException e) {
        throw new WebApplicationException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) SystemLoad(org.opencastproject.serviceregistry.api.SystemLoad) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Aggregations

SystemLoad (org.opencastproject.serviceregistry.api.SystemLoad)6 HostRegistration (org.opencastproject.serviceregistry.api.HostRegistration)5 ServiceRegistration (org.opencastproject.serviceregistry.api.ServiceRegistration)3 Test (org.junit.Test)2 Job (org.opencastproject.job.api.Job)2 NodeLoad (org.opencastproject.serviceregistry.api.SystemLoad.NodeLoad)2 LinkedList (java.util.LinkedList)1 EntityManager (javax.persistence.EntityManager)1 Query (javax.persistence.Query)1 TypedQuery (javax.persistence.TypedQuery)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 HttpStatus (org.apache.http.HttpStatus)1 Status (org.opencastproject.job.api.Job.Status)1 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)1 RestQuery (org.opencastproject.util.doc.rest.RestQuery)1