Search in sources :

Example 6 with ServiceRegistrationJpaImpl

use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl in project opencast by opencast.

the class ServiceRegistrationTest method testScenarioManyJobsManyServices.

@Test
public void testScenarioManyJobsManyServices() throws Exception {
    Job job1Try1 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, true, null);
    Job job1Try2 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, true, null);
    Job job1Try3 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, true, null);
    Job job1Try4 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, true, null);
    List<String> list = new ArrayList<String>();
    list.add("test");
    Job job2Try1 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_2, list, null, true, null);
    Job job2Try2 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_2, list, null, true, null);
    Job job2Try3 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_2, list, null, true, null);
    serviceRegistry.maxAttemptsBeforeErrorState = 0;
    ServiceRegistrationJpaImpl updatedService1;
    ServiceRegistrationJpaImpl updatedService2;
    ServiceRegistrationJpaImpl updatedService3;
    // 1st try for job 1, failed on localhost
    job1Try1.setStatus(Status.FAILED);
    job1Try1.setJobType(regType1Localhost.getServiceType());
    job1Try1.setProcessingHost(regType1Localhost.getHost());
    job1Try1 = serviceRegistry.updateJob(job1Try1);
    updatedService1 = getUpdatedService(regType1Localhost);
    updatedService2 = getUpdatedService(regType1Remotehost1);
    updatedService3 = getUpdatedService(regType1Remotehost2);
    Assert.assertEquals(ServiceState.WARNING, updatedService1.getServiceState());
    Assert.assertEquals(ServiceState.NORMAL, updatedService2.getServiceState());
    Assert.assertEquals(ServiceState.NORMAL, updatedService3.getServiceState());
    Assert.assertEquals(0, updatedService1.getErrorStateTrigger());
    // 1st try for job 2, failed on localhost
    job2Try1.setStatus(Status.FAILED);
    job2Try1.setJobType(regType1Localhost.getServiceType());
    job2Try1.setProcessingHost(regType1Localhost.getHost());
    serviceRegistry.updateJob(job2Try1);
    updatedService1 = getUpdatedService(regType1Localhost);
    updatedService2 = getUpdatedService(regType1Remotehost1);
    updatedService3 = getUpdatedService(regType1Remotehost2);
    Assert.assertEquals(ServiceState.ERROR, updatedService1.getServiceState());
    Assert.assertEquals(ServiceState.NORMAL, updatedService2.getServiceState());
    Assert.assertEquals(ServiceState.NORMAL, updatedService3.getServiceState());
    // 2nd try for job 1, failed on remotehost1
    job1Try2.setStatus(Status.FAILED);
    job1Try2.setJobType(regType1Remotehost1.getServiceType());
    job1Try2.setProcessingHost(regType1Remotehost1.getHost());
    serviceRegistry.updateJob(job1Try2);
    updatedService1 = getUpdatedService(regType1Localhost);
    updatedService2 = getUpdatedService(regType1Remotehost1);
    updatedService3 = getUpdatedService(regType1Remotehost2);
    Assert.assertEquals(ServiceState.ERROR, updatedService1.getServiceState());
    Assert.assertEquals(ServiceState.WARNING, updatedService2.getServiceState());
    Assert.assertEquals(ServiceState.NORMAL, updatedService3.getServiceState());
    updatedService2.getWarningStateTrigger();
    Assert.assertEquals(0, updatedService2.getErrorStateTrigger());
    // 2nd try for job 2, failed on remotehost1
    job2Try2.setStatus(Status.FINISHED);
    job2Try2.setJobType(regType1Remotehost1.getServiceType());
    job2Try2.setProcessingHost(regType1Remotehost1.getHost());
    serviceRegistry.updateJob(job2Try2);
    updatedService1 = getUpdatedService(regType1Localhost);
    updatedService2 = getUpdatedService(regType1Remotehost1);
    updatedService3 = getUpdatedService(regType1Remotehost2);
    Assert.assertEquals(ServiceState.ERROR, updatedService1.getServiceState());
    Assert.assertEquals(ServiceState.NORMAL, updatedService2.getServiceState());
    Assert.assertEquals(ServiceState.NORMAL, updatedService3.getServiceState());
    Assert.assertEquals(0, updatedService2.getErrorStateTrigger());
    // 3rd try for job 1, failed on remotehost2
    job1Try3.setStatus(Status.FINISHED);
    job1Try3.setJobType(regType1Remotehost2.getServiceType());
    job1Try3.setProcessingHost(regType1Remotehost2.getHost());
    serviceRegistry.updateJob(job1Try3);
    updatedService1 = getUpdatedService(regType1Localhost);
    updatedService2 = getUpdatedService(regType1Remotehost1);
    updatedService3 = getUpdatedService(regType1Remotehost2);
    Assert.assertEquals(ServiceState.ERROR, updatedService1.getServiceState());
    Assert.assertEquals(ServiceState.NORMAL, updatedService2.getServiceState());
    Assert.assertEquals(ServiceState.NORMAL, updatedService3.getServiceState());
    Assert.assertEquals(0, updatedService3.getWarningStateTrigger());
    Assert.assertEquals(0, updatedService2.getErrorStateTrigger());
    // 3rd try for job2, failed on remotehost2
    job2Try3.setStatus(Status.FAILED);
    job2Try3.setJobType(regType1Remotehost2.getServiceType());
    job2Try3.setProcessingHost(regType1Remotehost2.getHost());
    serviceRegistry.updateJob(job2Try3);
    updatedService1 = getUpdatedService(regType1Localhost);
    updatedService2 = getUpdatedService(regType1Remotehost1);
    updatedService3 = getUpdatedService(regType1Remotehost2);
    Assert.assertEquals(ServiceState.WARNING, updatedService1.getServiceState());
    Assert.assertEquals(ServiceState.NORMAL, updatedService2.getServiceState());
    Assert.assertEquals(ServiceState.NORMAL, updatedService3.getServiceState());
    // 4rd try for job1, failed on remotehost2
    job1Try4.setStatus(Status.FAILED);
    job1Try4.setJobType(regType1Remotehost2.getServiceType());
    job1Try4.setProcessingHost(regType1Remotehost2.getHost());
    serviceRegistry.updateJob(job1Try4);
    updatedService1 = getUpdatedService(regType1Localhost);
    updatedService2 = getUpdatedService(regType1Remotehost1);
    updatedService3 = getUpdatedService(regType1Remotehost2);
    Assert.assertEquals(ServiceState.NORMAL, updatedService1.getServiceState());
    Assert.assertEquals(ServiceState.NORMAL, updatedService2.getServiceState());
    Assert.assertEquals(ServiceState.NORMAL, updatedService3.getServiceState());
}
Also used : ArrayList(java.util.ArrayList) ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl) Job(org.opencastproject.job.api.Job) Test(org.junit.Test)

Example 7 with ServiceRegistrationJpaImpl

use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl in project opencast by opencast.

the class ServiceRegistryJpaImpl method getRelatedWarningServices.

/**
 * Gets the services in WARNING 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> getRelatedWarningServices(JpaJob job) throws IllegalArgumentException, ServiceRegistryException {
    if (job == null)
        throw new IllegalArgumentException("job must not be null!");
    Query query = null;
    EntityManager em = null;
    logger.debug("Finding services put in WARNING state by job {}", 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");
        query.setParameter("serviceType", job.getJobType());
        List<ServiceRegistrationJpaImpl> jpaServices = new ArrayList<ServiceRegistrationJpaImpl>();
        @SuppressWarnings("unchecked") List<ServiceRegistrationJpaImpl> jobResults = query.getResultList();
        for (ServiceRegistrationJpaImpl relatedService : jobResults) {
            if (relatedService.getWarningStateTrigger() == 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();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) ArrayList(java.util.ArrayList) ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl) NoResultException(javax.persistence.NoResultException) URISyntaxException(java.net.URISyntaxException) NoResultException(javax.persistence.NoResultException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Example 8 with ServiceRegistrationJpaImpl

use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl in project opencast by opencast.

the class ServiceRegistryJpaImpl method updateServiceState.

/**
 * Internal method to update the service registration state, throwing unwrapped JPA exceptions.
 *
 * @param em
 *          the current entity manager
 * @param registration
 *          the service registration to update
 * @return the updated service registration
 * @throws PersistenceException
 *           if there is an exception thrown while persisting the job via JPA
 * @throws IllegalArgumentException
 */
private ServiceRegistration updateServiceState(EntityManager em, ServiceRegistrationJpaImpl registration) throws PersistenceException {
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        ServiceRegistrationJpaImpl fromDb;
        fromDb = em.find(ServiceRegistrationJpaImpl.class, registration.getId());
        if (fromDb == null) {
            throw new NoResultException();
        }
        fromDb.setServiceState(registration.getServiceState());
        fromDb.setStateChanged(registration.getStateChanged());
        fromDb.setWarningStateTrigger(registration.getWarningStateTrigger());
        fromDb.setErrorStateTrigger(registration.getErrorStateTrigger());
        tx.commit();
        servicesStatistics.updateService(registration);
        return registration;
    } catch (PersistenceException e) {
        if (tx.isActive()) {
            tx.rollback();
        }
        throw e;
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) PersistenceException(javax.persistence.PersistenceException) ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl) NoResultException(javax.persistence.NoResultException)

Example 9 with ServiceRegistrationJpaImpl

use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl in project opencast by opencast.

the class WorkflowServiceImplTest method setupJob.

private void setupJob(long id, String operation, ServiceRegistry mockServiceRegistry) throws ServiceRegistryException, NotFoundException {
    ServiceRegistrationJpaImpl serviceRegistrationJpaImpl = EasyMock.createMock(ServiceRegistrationJpaImpl.class);
    expect(serviceRegistrationJpaImpl.getHost()).andReturn("http://localhost:8080");
    expect(serviceRegistrationJpaImpl.getServiceType()).andReturn(operation);
    replay(serviceRegistrationJpaImpl);
    List<String> arguments = new LinkedList<String>();
    arguments.add(Long.toString(id));
    Job job = createNiceMock(Job.class);
    expect(job.getId()).andStubReturn(id);
    expect(job.getCreator()).andStubReturn(securityService.getUser().getUsername());
    expect(job.getOrganization()).andStubReturn(securityService.getOrganization().getId());
    expect(job.getOperation()).andStubReturn("RESUME");
    expect(job.getArguments()).andStubReturn(arguments);
    expect(job.isDispatchable()).andStubReturn(false);
    expect(job.getStatus()).andStubReturn(Job.Status.INSTANTIATED);
    replay(job);
    expect(mockServiceRegistry.getJob(id)).andReturn(job).anyTimes();
    expect(mockServiceRegistry.updateJob(job)).andReturn(job);
}
Also used : ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl) Job(org.opencastproject.job.api.Job) LinkedList(java.util.LinkedList)

Example 10 with ServiceRegistrationJpaImpl

use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl in project opencast by opencast.

the class ServiceRegistryJpaImpl method disableHost.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.serviceregistry.api.ServiceRegistry#disableHost(String)
 */
@Override
public void disableHost(String host) throws ServiceRegistryException, NotFoundException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        HostRegistrationJpaImpl hostRegistration = fetchHostRegistration(em, host);
        if (hostRegistration == null) {
            throw new NotFoundException("Host '" + host + "' is not currently registered, so it can not be disabled");
        } else {
            hostRegistration.setActive(false);
            for (ServiceRegistration serviceRegistration : getServiceRegistrationsByHost(host)) {
                ServiceRegistrationJpaImpl registration = (ServiceRegistrationJpaImpl) serviceRegistration;
                registration.setActive(false);
                em.merge(registration);
                servicesStatistics.updateService(registration);
            }
            em.merge(hostRegistration);
        }
        logger.info("Disabling {}", host);
        tx.commit();
        hostsStatistics.updateHost(hostRegistration);
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        if (tx != null && tx.isActive()) {
            tx.rollback();
        }
        throw new ServiceRegistryException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl) HostRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.HostRegistrationJpaImpl) URISyntaxException(java.net.URISyntaxException) NoResultException(javax.persistence.NoResultException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration)

Aggregations

ServiceRegistrationJpaImpl (org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl)16 EntityManager (javax.persistence.EntityManager)9 NoResultException (javax.persistence.NoResultException)8 NotFoundException (org.opencastproject.util.NotFoundException)8 PersistenceException (javax.persistence.PersistenceException)7 RollbackException (javax.persistence.RollbackException)7 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)7 URISyntaxException (java.net.URISyntaxException)6 Job (org.opencastproject.job.api.Job)6 TrustedHttpClientException (org.opencastproject.security.api.TrustedHttpClientException)6 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)6 ConfigurationException (org.osgi.service.cm.ConfigurationException)6 ArrayList (java.util.ArrayList)5 EntityTransaction (javax.persistence.EntityTransaction)5 Query (javax.persistence.Query)4 TypedQuery (javax.persistence.TypedQuery)4 LinkedList (java.util.LinkedList)3 Test (org.junit.Test)3 ServiceRegistration (org.opencastproject.serviceregistry.api.ServiceRegistration)3 HostRegistrationJpaImpl (org.opencastproject.serviceregistry.impl.jpa.HostRegistrationJpaImpl)3