@SuppressWarnings("unchecked") protected List<Object[]> getAvgOperations(EntityManager em) throws ServiceRegistryException { Query query = null; try { query = em.createNamedQuery("Job.avgOperation"); return query.getResultList(); } catch (Exception e) { throw new ServiceRegistryException(e); } }
@SuppressWarnings("unchecked") List<Object[]> getCountPerHostService(EntityManager em) throws ServiceRegistryException { Query query = null; try { query = em.createNamedQuery("Job.countPerHostService"); return query.getResultList(); } catch (Exception e) { throw new ServiceRegistryException(e); } }
@Override public List<String> getJobPayloads(String operation) throws ServiceRegistryException { EntityManager em = emf.createEntityManager(); try { TypedQuery<String> query = em.createNamedQuery("Job.payload", String.class); query.setParameter("operation", operation); logger.debug("Requesting job payloads using query: {}", query); return query.getResultList(); } catch (Exception e) { throw new ServiceRegistryException(e); } }
/** * {@inheritDoc} * * @see org.opencastproject.serviceregistry.api.ServiceRegistry#countOfAbnormalServices() */ @Override public long countOfAbnormalServices() throws ServiceRegistryException { EntityManager em = null; try { em = emf.createEntityManager(); Query query = em.createNamedQuery("ServiceRegistration.countNotNormal"); Number count = (Number) query.getSingleResult(); return count.longValue(); } catch (Exception e) { throw new ServiceRegistryException(e); } finally { if (em != null) em.close(); } }
/** * {@inheritDoc} * * @see org.opencastproject.serviceregistry.api.ServiceRegistry#getMaxLoadOnNode(java.lang.String) */ @Override public NodeLoad getMaxLoadOnNode(String host) throws ServiceRegistryException { if (hosts.containsKey(host)) { return new NodeLoad(host, hosts.get(host).getMaxLoad()); } throw new ServiceRegistryException("Unable to find host " + host + " in service registry"); }
/** * {@inheritDoc} * * @see org.opencastproject.serviceregistry.api.ServiceRegistry#countByOperation(java.lang.String, java.lang.String, * Status) */ @Override public long countByOperation(String serviceType, String operation, Status status) throws ServiceRegistryException { EntityManager em = null; try { em = emf.createEntityManager(); Query query = em.createNamedQuery("Job.countByOperation"); query.setParameter("status", status.ordinal()); query.setParameter("serviceType", serviceType); query.setParameter("operation", operation); Number countResult = (Number) query.getSingleResult(); return countResult.longValue(); } catch (Exception e) { throw new ServiceRegistryException(e); } finally { if (em != null) em.close(); } }
/** * {@inheritDoc} * * @see org.opencastproject.serviceregistry.api.ServiceRegistry#countByHost(java.lang.String, java.lang.String, * Status) */ @Override public long countByHost(String serviceType, String host, Status status) throws ServiceRegistryException { EntityManager em = null; try { em = emf.createEntityManager(); Query query = em.createNamedQuery("Job.countByHost"); query.setParameter("status", status.ordinal()); query.setParameter("serviceType", serviceType); query.setParameter("host", host); Number countResult = (Number) query.getSingleResult(); return countResult.longValue(); } catch (Exception e) { throw new ServiceRegistryException(e); } finally { if (em != null) em.close(); } }
/** * {@inheritDoc} * * @see org.opencastproject.serviceregistry.api.ServiceRegistry#count(java.lang.String, java.lang.String, * java.lang.String, Status) */ @Override public long count(String serviceType, String host, String operation, Status status) throws ServiceRegistryException { if (StringUtils.isBlank(serviceType) || StringUtils.isBlank(host) || StringUtils.isBlank(operation) || status == null) throw new IllegalArgumentException("service type, host, operation, and status must be provided"); Query query = null; EntityManager em = null; try { em = emf.createEntityManager(); query = em.createNamedQuery("Job.fullMonty"); query.setParameter("status", status.ordinal()); query.setParameter("serviceType", serviceType); query.setParameter("operation", operation); Number countResult = (Number) query.getSingleResult(); return countResult.longValue(); } catch (Exception e) { throw new ServiceRegistryException(e); } finally { if (em != null) em.close(); } }
/** * Return dispatchable job ids, where the job status is RESTART or QUEUED and the job id is listed in the given set. * * @param em the entity manager * @param jobIds set with job id's interested in * @return list with dispatchable job id's from the given set, with job status RESTART or QUEUED * @throws ServiceRegistryException if there is a problem communicating with the jobs database */ protected List<Long> getDispatchableJobsWithIdFilter(EntityManager em, Set<Long> jobIds) throws ServiceRegistryException { if (jobIds == null || jobIds.isEmpty()) return Collections.EMPTY_LIST; Query query = null; try { query = em.createNamedQuery("Job.dispatchable.status.idfilter"); query.setParameter("jobids", dispatchPriorityList.keySet()); query.setParameter("statuses", Arrays.asList(Status.RESTART.ordinal(), Status.QUEUED.ordinal())); return query.getResultList(); } catch (Exception e) { throw new ServiceRegistryException(e); } }
/** * {@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(); } }
/** * Gets the failed jobs history for the given service registration * * @param serviceRegistration * @return the failed jobs history size * @throws IllegalArgumentException * if parameter is null * @throws ServiceRegistryException */ private int getHistorySize(ServiceRegistration serviceRegistration) throws IllegalArgumentException, ServiceRegistryException { if (serviceRegistration == null) throw new IllegalArgumentException("serviceRegistration must not be null!"); Query query = null; EntityManager em = null; logger.debug("Calculating count of jobs who failed due to service {}", serviceRegistration.toString()); try { em = emf.createEntityManager(); query = em.createNamedQuery("Job.count.history.failed"); query.setParameter("serviceType", serviceRegistration.getServiceType()); query.setParameter("host", serviceRegistration.getHost()); Number number = (Number) query.getSingleResult(); return number.intValue(); } catch (Exception e) { throw new ServiceRegistryException(e); } finally { if (em != null) em.close(); } }
return query.getResultList(); } catch (Exception e) { throw new ServiceRegistryException(e);
throw new ServiceRegistryException(e);
/** * {@inheritDoc} * * @see org.opencastproject.serviceregistry.api.ServiceRegistry#registerService(java.lang.String, java.lang.String, * java.lang.String, boolean) */ @Override public ServiceRegistration registerService(String serviceType, String host, String path, boolean jobProducer) throws ServiceRegistryException { HostRegistrationInMemory hostRegistration = hosts.get(host); if (hostRegistration == null) { throw new ServiceRegistryException(new NotFoundException("Host " + host + " was not found")); } List<ServiceRegistrationInMemoryImpl> servicesOnHost = services.get(host); if (servicesOnHost == null) { servicesOnHost = new ArrayList<ServiceRegistrationInMemoryImpl>(); services.put(host, servicesOnHost); } ServiceRegistrationInMemoryImpl registration = new ServiceRegistrationInMemoryImpl(serviceType, host, path, jobProducer); servicesOnHost.add(registration); return registration; }
private void deleteChildJobs(EntityManager em, EntityTransaction tx, long jobId) throws ServiceRegistryException { List<Job> childJobs = getChildJobs(jobId); if (childJobs.isEmpty()) { logger.debug("No child jobs of job '{}' found to delete.", jobId); return; } logger.debug("Start deleting child jobs of job '{}'", jobId); try { for (int i = childJobs.size() - 1; i >= 0; i--) { Job job = childJobs.get(i); JpaJob jobToDelete = em.find(JpaJob.class, job.getId()); em.remove(jobToDelete); removeFromLoadCache(job.getId()); logger.debug("Job '{}' deleted", job.getId()); } logger.debug("Deleted all child jobs of job '{}'", jobId); } catch (Exception e) { logger.error("Unable to remove child jobs from {}: {}", jobId, e); if (tx.isActive()) { tx.rollback(); } throw new ServiceRegistryException(e); } }
/** * {@inheritDoc} * * @see org.opencastproject.job.api.AbstractJobProducer#process(org.opencastproject.job.api.Job) */ @Override protected String process(Job job) throws Exception { Operation op = null; String operation = job.getOperation(); List<String> arguments = job.getArguments(); try { op = Operation.valueOf(operation); switch (op) { case Waveform: Track track = (Track) MediaPackageElementParser.getFromXml(arguments.get(0)); int pixelsPerMinute = Integer.parseInt(arguments.get(1)); int minWidth = Integer.parseInt(arguments.get(2)); int maxWidth = Integer.parseInt(arguments.get(3)); int height = Integer.parseInt(arguments.get(4)); Attachment waveformMpe = extractWaveform(track, pixelsPerMinute, minWidth, maxWidth, height); return MediaPackageElementParser.getAsXml(waveformMpe); default: throw new ServiceRegistryException("This service can't handle operations of type '" + op + "'"); } } catch (IndexOutOfBoundsException e) { throw new ServiceRegistryException("This argument list for operation '" + op + "' does not meet expectations", e); } catch (MediaPackageException | WaveformServiceException e) { throw new ServiceRegistryException("Error handling operation '" + op + "'", e); } }
/** * {@inheritDoc} * * @see org.opencastproject.serviceregistry.api.ServiceRegistry#getActiveJobs() */ @Override public List<Job> getActiveJobs() throws ServiceRegistryException { List<Status> statuses = new ArrayList<Status>(); for (Status status : Status.values()) { if (status.isActive()) statuses.add(status); } EntityManager em = null; try { em = emf.createEntityManager(); List<JpaJob> jpaJobs = getJobsByStatus(em, statuses.toArray(new Status[statuses.size()])); List<Job> jobs = new ArrayList<Job>(jpaJobs.size()); for (JpaJob jpaJob : jpaJobs) { jobs.add(jpaJob.toJob()); } return jobs; } catch (Exception e) { throw new ServiceRegistryException(e); } finally { if (em != null) em.close(); } }
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(); } }
/** * {@inheritDoc} * * @see org.opencastproject.job.api.AbstractJobProducer#process(org.opencastproject.job.api.Job) */ @Override protected String process(Job job) throws Exception { Operation op = null; String operation = job.getOperation(); List<String> arguments = job.getArguments(); try { op = Operation.valueOf(operation); switch (op) { case TimelinePreview: Track track = (Track) MediaPackageElementParser .getFromXml(arguments.get(0)); int imageCount = Integer.parseInt(arguments.get(1)); Attachment timelinePreviewsMpe = generatePreviewImages(job, track, imageCount); return MediaPackageElementParser.getAsXml(timelinePreviewsMpe); default: throw new IllegalStateException("Don't know how to handle operation '" + operation + "'"); } } catch (IllegalArgumentException e) { throw new ServiceRegistryException("This service can't handle operations of type '" + op + "'", e); } catch (IndexOutOfBoundsException e) { throw new ServiceRegistryException("This argument list for operation '" + op + "' does not meet expectations", e); } catch (Exception e) { throw new ServiceRegistryException("Error handling operation '" + op + "'", e); } }
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().getHost().equals(getRegistryHostname())) { processCachedLoadChange(job); } // All WorkflowService Jobs will be ignored if (oldJob.getStatus() != job.getStatus() && !TYPE_WORKFLOW.equals(job.getJobType())) { updateServiceForFailover(em, job); } return jpaJob; } catch (PersistenceException e) { throw new ServiceRegistryException(e); } catch (NotFoundException e) { // Just in case, remove from cache if there removeFromLoadCache(job.getId()); throw new ServiceRegistryException(e); } finally { if (em != null) em.close(); } }