@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); } }
@POST @Path("unregisterhost") @RestQuery(name = "unregisterhost", description = "Removes a server from the cluster.", returnDescription = "No content.", restParameters = { @RestParameter(name = "host", isRequired = true, description = "The host name, including the http(s) protocol", type = Type.STRING) }, reponses = { @RestResponse(responseCode = SC_NO_CONTENT, description = "The host was removed successfully") }) public Response unregister(@FormParam("host") String host) { try { serviceRegistry.unregisterHost(host); return Response.status(Status.NO_CONTENT).build(); } catch (ServiceRegistryException e) { if (e.getCause() instanceof IllegalArgumentException) { return Response.status(Status.NOT_FOUND).entity(e.getMessage()).build(); } throw new WebApplicationException(e); } }
public String set(String baseUrl, boolean maintenanceMode) { try { serviceRegistry.setMaintenanceStatus(baseUrl, maintenanceMode); if (maintenanceMode) { return baseUrl + " is now in maintenance mode\n"; } else { return baseUrl + " has returned to service\n"; } } catch (ServiceRegistryException e) { return "Error setting maintenance mode: " + e.getMessage() + "\n"; } catch (NotFoundException e) { return "Error setting maintenance mode, host " + baseUrl + " not found"; } }
private Job findJob(long jobId) throws NotFoundException, IncidentServiceException { try { return getServiceRegistry().getJob(jobId); } catch (NotFoundException e) { logger.info("Job with Id {} does not exist", jobId); throw e; } catch (ServiceRegistryException e) { logger.error("Could not retrieve job {}: {}", jobId, e.getMessage()); throw new IncidentServiceException(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); } }
public String list() { try { StringBuilder sb = new StringBuilder(); for (ServiceRegistration reg : serviceRegistry.getServiceRegistrations()) { sb.append(reg.getServiceType()); sb.append("@"); sb.append(reg.getHost()); if (reg.isInMaintenanceMode()) { sb.append(" (maintenance mode)"); } sb.append("\n"); } return sb.toString(); } catch (ServiceRegistryException e) { return "Error: " + e.getMessage() + "\n"; } }
@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); } }
break; } catch (ServiceRegistryException e) { logger.warn("Error polling service registry for the status of {}: {}", job, e.getMessage()); } catch (JobCanceledException e) { logger.warn("Job {} got canceled", job);
/** * {@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(); } }
private void suspendWaiterJob() { if (this.waiterJobId.isSome()) { try { final Job waiter = serviceRegistry.getJob(waiterJobId.get()); waiter.setStatus(Job.Status.WAITING); List<Long> blockedForJobs = new LinkedList<Long>(); for (Job j : jobs) { try { if (setBlockerJob(j, waiter)) { blockedForJobs.add(j.getId()); } } catch (OptimisticLockException e) { // Try again, this happens if the job finishes before we get here // If the same exception happens again then we're in a very weird state if (setBlockerJob(j, waiter)) { blockedForJobs.add(j.getId()); } } } waiter.setBlockedJobIds(blockedForJobs); this.serviceRegistry.updateJob(waiter); } catch (ServiceRegistryException e) { logger.warn("Unable to put {} into a waiting state, this may cause a deadlock: {}", waiterJobId, e.getMessage()); } catch (NotFoundException e) { logger.warn("Unable to put {} into a waiting state, job not found by the service registry. This may cause a deadlock: {}", waiterJobId, e.getMessage()); } } else { logger.debug("No waiting job set, unable to put waiting job into waiting state"); } }
/** * {@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"); }
private void wakeWaiterJob() { if (this.waiterJobId.isSome()) { try { final Job waiter = serviceRegistry.getJob(waiterJobId.get()); waiter.setStatus(Job.Status.RUNNING); for (Job j : jobs) { Job updatedJob = this.serviceRegistry.getJob(j.getId()); updatedJob.removeBlockingJobId(); // FYI not updating local j in jobs collection this.serviceRegistry.updateJob(updatedJob); } waiter.removeBlockedJobsIds(); this.serviceRegistry.updateJob(waiter); } catch (ServiceRegistryException e) { logger.warn("Unable to put {} into a waiting state, this may cause a deadlock: {}", waiterJobId, e.getMessage()); } catch (NotFoundException e) { logger.warn("Unable to put {} into a waiting state, job not found by the service registry. This may cause a deadlock: {}", waiterJobId, e.getMessage()); } } else { logger.debug("No waiting job set, unable to put waiting job into waiting state"); } }
/** * {@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#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#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(); } }