Search in sources :

Example 6 with ServiceRegistration

use of org.opencastproject.serviceregistry.api.ServiceRegistration in project opencast by opencast.

the class JobTest method testDuplicateHandlerRegistrations.

@Test
public void testDuplicateHandlerRegistrations() throws Exception {
    String url = "http://type1handler:8080";
    serviceRegistry.registerHost(url, "127.0.0.1", 1024, 1, 1);
    String receiptType = "type1";
    // we should start with no handlers
    List<ServiceRegistration> hosts = serviceRegistry.getServiceRegistrationsByLoad(receiptType);
    assertEquals(0, hosts.size());
    // register a handler
    serviceRegistry.registerService(receiptType, url, PATH);
    hosts = serviceRegistry.getServiceRegistrationsByLoad("type1");
    assertEquals(1, hosts.size());
    assertEquals(url, hosts.get(0).getHost());
    // set the host to be in maintenance mode
    serviceRegistry.setMaintenanceStatus(url, true);
    hosts = serviceRegistry.getServiceRegistrationsByLoad("type1");
    assertEquals(0, hosts.size());
    // re-register the host. this should not unset the maintenance mode and should not throw an exception
    serviceRegistry.unregisterHost(url);
    serviceRegistry.registerHost(url, "127.0.0.1", 1024, 1, 1);
    serviceRegistry.registerService(receiptType, url, PATH);
    // zero because it's still in maintenance mode
    assertEquals(0, serviceRegistry.getServiceRegistrationsByLoad(receiptType).size());
    // unregister
    serviceRegistry.unRegisterService(receiptType, url);
    hosts = serviceRegistry.getServiceRegistrationsByLoad("type1");
    assertEquals(0, hosts.size());
}
Also used : Arrays.mkString(org.opencastproject.util.data.Arrays.mkString) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration) Test(org.junit.Test)

Example 7 with ServiceRegistration

use of org.opencastproject.serviceregistry.api.ServiceRegistration in project opencast by opencast.

the class ServiceRegistrationTest method testServiceRegistrationsByLoad.

@Test
public void testServiceRegistrationsByLoad() throws Exception {
    List<ServiceRegistration> services = serviceRegistry.getServiceRegistrations();
    List<HostRegistration> hosts = serviceRegistry.getHostRegistrations();
    SystemLoad hostLoads = serviceRegistry.getHostLoads(serviceRegistry.emf.createEntityManager());
    List<ServiceRegistration> availableServices = serviceRegistry.getServiceRegistrationsByLoad(JOB_TYPE_1, services, hosts, hostLoads);
    // Make sure all hosts are available for processing
    Assert.assertEquals(3, availableServices.size());
    // Create a job and mark it as running.
    Job job = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, false, null);
    job.setStatus(Job.Status.RUNNING);
    job = serviceRegistry.updateJob(job);
    // Recalculate the number of available services
    hostLoads = serviceRegistry.getHostLoads(serviceRegistry.emf.createEntityManager());
    availableServices = serviceRegistry.getServiceRegistrationsByLoad(JOB_TYPE_1, services, hosts, hostLoads);
    // Since the host load is not taken into account, still all tree services should show up
    Assert.assertEquals(3, availableServices.size());
    // Recalculate the number of available services after ignoring a host
    hosts.remove(regType1Remotehost1.getHostRegistration());
    availableServices = serviceRegistry.getServiceRegistrationsByLoad(JOB_TYPE_1, services, hosts, hostLoads);
    // Since host 1 is now ignored, only two more services should show up
    Assert.assertEquals(2, availableServices.size());
}
Also used : HostRegistration(org.opencastproject.serviceregistry.api.HostRegistration) SystemLoad(org.opencastproject.serviceregistry.api.SystemLoad) Job(org.opencastproject.job.api.Job) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration) Test(org.junit.Test)

Example 8 with ServiceRegistration

use of org.opencastproject.serviceregistry.api.ServiceRegistration in project opencast by opencast.

the class ServiceRegistrationTest method testHostCapacity.

@Test
public void testHostCapacity() throws Exception {
    List<ServiceRegistration> services = serviceRegistry.getServiceRegistrations();
    List<HostRegistration> hosts = serviceRegistry.getHostRegistrations();
    SystemLoad hostLoads = serviceRegistry.getHostLoads(serviceRegistry.emf.createEntityManager());
    List<ServiceRegistration> availableServices = serviceRegistry.getServiceRegistrationsWithCapacity(JOB_TYPE_1, services, hosts, hostLoads);
    // Make sure all hosts are available for processing
    Assert.assertEquals(3, availableServices.size());
    // Create a job and mark it as running.
    Job job = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, false, null);
    job.setStatus(Job.Status.RUNNING);
    job = serviceRegistry.updateJob(job);
    // Recalculate the number of available services
    hostLoads = serviceRegistry.getHostLoads(serviceRegistry.emf.createEntityManager());
    availableServices = serviceRegistry.getServiceRegistrationsWithCapacity(JOB_TYPE_1, services, hosts, hostLoads);
    // Since host 1 is now maxed out, only two more services should show up
    Assert.assertEquals(2, availableServices.size());
    // Recalculate the number of available services after ignoring a host
    hosts.remove(regType1Remotehost1.getHostRegistration());
    availableServices = serviceRegistry.getServiceRegistrationsWithCapacity(JOB_TYPE_1, services, hosts, hostLoads);
    // Since remote host 1 is now ignored, only one more service should show up
    Assert.assertEquals(1, availableServices.size());
}
Also used : HostRegistration(org.opencastproject.serviceregistry.api.HostRegistration) SystemLoad(org.opencastproject.serviceregistry.api.SystemLoad) Job(org.opencastproject.job.api.Job) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration) Test(org.junit.Test)

Example 9 with ServiceRegistration

use of org.opencastproject.serviceregistry.api.ServiceRegistration in project opencast by opencast.

the class ServiceRegistryJpaImpl method getServiceRegistrationsWithCapacity.

/**
 * Returns a filtered list of service registrations, containing only those that are online, not in maintenance mode,
 * and with a specific service type that are running on a host which is not already maxed out.
 *
 * @param serviceRegistrations
 *          the complete list of service registrations
 * @param hostRegistrations
 *          the complete list of available host registrations
 * @param systemLoad
 *          the map of hosts to the number of running jobs
 * @param jobType
 *          the job type for which the services registrations are filtered
 */
protected List<ServiceRegistration> getServiceRegistrationsWithCapacity(String jobType, List<ServiceRegistration> serviceRegistrations, List<HostRegistration> hostRegistrations, final SystemLoad systemLoad) {
    final List<String> hostBaseUrls = $(hostRegistrations).map(toBaseUrl).toList();
    final List<ServiceRegistration> filteredList = new ArrayList<ServiceRegistration>();
    for (ServiceRegistration service : serviceRegistrations) {
        // Skip service if host not available
        if (!hostBaseUrls.contains(service.getHost())) {
            logger.trace("Not considering {} because it's host {} is not available for dispatching", service, service.getHost());
            continue;
        }
        // Skip services that are not of the requested type
        if (!jobType.equals(service.getServiceType())) {
            logger.trace("Not considering {} because it is of the wrong job type", service);
            continue;
        }
        // Skip services that are in error state
        if (service.getServiceState() == ERROR) {
            logger.trace("Not considering {} because it is in error state", service);
            continue;
        }
        // Skip services that are in maintenance mode
        if (service.isInMaintenanceMode()) {
            logger.trace("Not considering {} because it is in maintenance mode", service);
            continue;
        }
        // Skip services that are marked as offline
        if (!service.isOnline()) {
            logger.trace("Not considering {} because it is currently offline", service);
            continue;
        }
        // Determine the maximum load for this host
        Float hostLoadMax = null;
        for (HostRegistration host : hostRegistrations) {
            if (host.getBaseUrl().equals(service.getHost())) {
                hostLoadMax = host.getMaxLoad();
                break;
            }
        }
        if (hostLoadMax == null)
            logger.warn("Unable to determine max load for host {}", service.getHost());
        // Determine the current load for this host
        Float hostLoad = systemLoad.get(service.getHost()).getLoadFactor();
        if (hostLoad == null)
            logger.warn("Unable to determine current load for host {}", service.getHost());
        // Is this host suited for processing?
        if (hostLoad == null || hostLoadMax == null || hostLoad < hostLoadMax) {
            logger.debug("Adding candidate service {} for processing of jobs of type '{}'", service, jobType);
            filteredList.add(service);
        }
    }
    // Sort the list by capacity
    Collections.sort(filteredList, new LoadComparator(systemLoad));
    return filteredList;
}
Also used : ArrayList(java.util.ArrayList) HostRegistration(org.opencastproject.serviceregistry.api.HostRegistration) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration)

Example 10 with ServiceRegistration

use of org.opencastproject.serviceregistry.api.ServiceRegistration in project opencast by opencast.

the class OaiPmhPublicationRestServiceTest method testPublishUsingRemoteService.

@Test
public void testPublishUsingRemoteService() throws Exception {
    final MediaPackage mp = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
    mp.addCreator(CREATOR);
    // 
    final ServiceRegistry registry = EasyMock.createNiceMock(ServiceRegistry.class);
    final ServiceRegistration registration = EasyMock.createNiceMock(ServiceRegistration.class);
    EasyMock.expect(registration.getHost()).andReturn(url.getProtocol() + "://" + url.getHost() + ":" + url.getPort()).anyTimes();
    EasyMock.expect(registration.getPath()).andReturn(url.getPath()).anyTimes();
    EasyMock.expect(registry.getServiceRegistrationsByLoad(EasyMock.anyString())).andReturn(ListBuilders.SIA.mk(registration)).anyTimes();
    EasyMock.replay(registry, registration);
    final OaiPmhPublicationServiceRemoteImpl remote = new OaiPmhPublicationServiceRemoteImpl();
    remote.setTrustedHttpClient(new TestHttpClient());
    remote.setRemoteServiceManager(registry);
    // 
    final Job job = remote.publish(mp, "mmp", new HashSet<String>(), new HashSet<String>(), false);
    assertEquals(job.getUri(), JOB_URI);
}
Also used : OaiPmhPublicationServiceRemoteImpl(org.opencastproject.publication.oaipmh.remote.OaiPmhPublicationServiceRemoteImpl) MediaPackage(org.opencastproject.mediapackage.MediaPackage) ServiceRegistry(org.opencastproject.serviceregistry.api.ServiceRegistry) Job(org.opencastproject.job.api.Job) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration) Test(org.junit.Test)

Aggregations

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