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