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