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;
}
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());
}
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());
}
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;
}
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);
}
}
Aggregations