Search in sources :

Example 6 with HostRegistration

use of org.opencastproject.serviceregistry.api.HostRegistration 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 7 with HostRegistration

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

the class YouTubeV3PublicationServiceImplTest method before.

@Before
public void before() throws Exception {
    youTubeService = createMock(YouTubeAPIVersion3Service.class);
    youTubeService.initialize(anyObject(ClientCredentials.class));
    expectLastCall();
    orgDirectory = createMock(OrganizationDirectoryService.class);
    security = createMock(SecurityService.class);
    registry = createMock(ServiceRegistry.class);
    List<HostRegistration> hosts = new LinkedList<HostRegistration>();
    HostRegistration host = new HostRegistrationInMemory("localhost", "localhost", 1.0F, 1, 1024L);
    hosts.add(host);
    expect(registry.getHostRegistrations()).andReturn(hosts).anyTimes();
    userDirectoryService = createMock(UserDirectoryService.class);
    workspace = createMock(Workspace.class);
    // 
    service = new YouTubeV3PublicationServiceImpl(youTubeService);
    service.setOrganizationDirectoryService(orgDirectory);
    service.setSecurityService(security);
    service.setServiceRegistry(registry);
    service.setUserDirectoryService(userDirectoryService);
    service.setWorkspace(workspace);
}
Also used : HostRegistrationInMemory(org.opencastproject.serviceregistry.api.HostRegistrationInMemory) SecurityService(org.opencastproject.security.api.SecurityService) HostRegistration(org.opencastproject.serviceregistry.api.HostRegistration) ServiceRegistry(org.opencastproject.serviceregistry.api.ServiceRegistry) ClientCredentials(org.opencastproject.publication.youtube.auth.ClientCredentials) LinkedList(java.util.LinkedList) OrganizationDirectoryService(org.opencastproject.security.api.OrganizationDirectoryService) UserDirectoryService(org.opencastproject.security.api.UserDirectoryService) Workspace(org.opencastproject.workspace.api.Workspace) Before(org.junit.Before)

Example 8 with HostRegistration

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

the class ServerEndpoint method getServers.

@GET
@Path("servers.json")
@Produces(MediaType.APPLICATION_JSON)
@RestQuery(description = "Returns the list of servers", name = "servers", restParameters = { @RestParameter(name = "limit", description = "The maximum number of items to return per page", isRequired = false, type = INTEGER), @RestParameter(name = "offset", description = "The offset", isRequired = false, type = INTEGER), @RestParameter(name = "filter", description = "Filter results by hostname, status or free text query", isRequired = false, type = STRING), @RestParameter(name = "sort", description = "The sort order.  May include any " + "of the following: COMPLETED (jobs), CORES, HOSTNAME, MAINTENANCE, MEANQUEUETIME (mean for jobs), " + "MEANRUNTIME (mean for jobs), ONLINE, QUEUED (jobs), RUNNING (jobs)." + "The suffix must be :ASC for ascending or :DESC for descending sort order (e.g. HOSTNAME:DESC).", isRequired = false, type = STRING) }, reponses = { @RestResponse(description = "Returns the list of jobs from Opencast", responseCode = HttpServletResponse.SC_OK) }, returnDescription = "The list of servers")
public Response getServers(@QueryParam("limit") final int limit, @QueryParam("offset") final int offset, @QueryParam("filter") String filter, @QueryParam("sort") String sort) throws Exception {
    ServersListQuery query = new ServersListQuery();
    EndpointUtil.addRequestFiltersToQuery(filter, query);
    query.setLimit(limit);
    query.setOffset(offset);
    List<JSONObject> servers = new ArrayList<>();
    // Get service statistics for all hosts and services
    List<ServiceStatistics> servicesStatistics = serviceRegistry.getServiceStatistics();
    for (HostRegistration server : serviceRegistry.getHostRegistrations()) {
        // Calculate statistics per server
        long jobsCompleted = 0;
        int jobsRunning = 0;
        int jobsQueued = 0;
        long sumMeanRuntime = 0;
        long sumMeanQueueTime = 0;
        int totalServiceOnHost = 0;
        int offlineJobProducerServices = 0;
        int totalJobProducerServices = 0;
        Set<String> serviceTypes = new HashSet<>();
        for (ServiceStatistics serviceStat : servicesStatistics) {
            if (server.getBaseUrl().equals(serviceStat.getServiceRegistration().getHost())) {
                totalServiceOnHost++;
                jobsCompleted += serviceStat.getFinishedJobs();
                jobsRunning += serviceStat.getRunningJobs();
                jobsQueued += serviceStat.getQueuedJobs();
                // mean time values are given in milliseconds,
                // we should convert them to seconds,
                // because the adminNG UI expect it in this format
                sumMeanRuntime += TimeUnit.MILLISECONDS.toSeconds(serviceStat.getMeanRunTime());
                sumMeanQueueTime += TimeUnit.MILLISECONDS.toSeconds(serviceStat.getMeanQueueTime());
                if (!serviceStat.getServiceRegistration().isOnline() && serviceStat.getServiceRegistration().isJobProducer()) {
                    offlineJobProducerServices++;
                    totalJobProducerServices++;
                } else if (serviceStat.getServiceRegistration().isJobProducer()) {
                    totalJobProducerServices++;
                }
                serviceTypes.add(serviceStat.getServiceRegistration().getServiceType());
            }
        }
        long meanRuntime = totalServiceOnHost > 0 ? Math.round((double) sumMeanRuntime / totalServiceOnHost) : 0L;
        long meanQueueTime = totalServiceOnHost > 0 ? Math.round((double) sumMeanQueueTime / totalServiceOnHost) : 0L;
        boolean vOnline = server.isOnline();
        boolean vMaintenance = server.isMaintenanceMode();
        String vHostname = server.getBaseUrl();
        int vCores = server.getCores();
        if (query.getHostname().isSome() && !StringUtils.equalsIgnoreCase(vHostname, query.getHostname().get()))
            continue;
        if (query.getStatus().isSome()) {
            if (StringUtils.equalsIgnoreCase(ServersListProvider.SERVER_STATUS_ONLINE, query.getStatus().get()) && !vOnline)
                continue;
            if (StringUtils.equalsIgnoreCase(ServersListProvider.SERVER_STATUS_OFFLINE, query.getStatus().get()) && vOnline)
                continue;
            if (StringUtils.equalsIgnoreCase(ServersListProvider.SERVER_STATUS_MAINTENANCE, query.getStatus().get()) && !vMaintenance)
                continue;
        }
        if (query.getFreeText().isSome() && !StringUtils.containsIgnoreCase(vHostname, query.getFreeText().get()) && !StringUtils.containsIgnoreCase(server.getIpAddress(), query.getFreeText().get()))
            continue;
        JSONObject jsonServer = new JSONObject();
        jsonServer.put(KEY_ONLINE, vOnline && offlineJobProducerServices <= totalJobProducerServices / 2);
        jsonServer.put(KEY_MAINTENANCE, vMaintenance);
        jsonServer.put(KEY_HOSTNAME, vHostname);
        jsonServer.put(KEY_CORES, vCores);
        jsonServer.put(KEY_RUNNING, jobsRunning);
        jsonServer.put(KEY_QUEUED, jobsQueued);
        jsonServer.put(KEY_COMPLETED, jobsCompleted);
        jsonServer.put(KEY_MEAN_RUN_TIME, meanRuntime);
        jsonServer.put(KEY_MEAN_QUEUE_TIME, meanQueueTime);
        servers.add(jsonServer);
    }
    // Sorting
    Sort sortKey = Sort.HOSTNAME;
    Boolean ascending = true;
    if (StringUtils.isNotBlank(sort)) {
        try {
            SortCriterion sortCriterion = RestUtils.parseSortQueryParameter(sort).iterator().next();
            sortKey = Sort.valueOf(sortCriterion.getFieldName().toUpperCase());
            ascending = SearchQuery.Order.Ascending == sortCriterion.getOrder() || SearchQuery.Order.None == sortCriterion.getOrder();
        } catch (WebApplicationException ex) {
            logger.warn("Failed to parse sort criterion \"{}\", invalid format.", sort);
        } catch (IllegalArgumentException ex) {
            logger.warn("Can not apply sort criterion \"{}\", no field with this name.", sort);
        }
    }
    JSONArray jsonList = new JSONArray();
    if (!servers.isEmpty()) {
        Collections.sort(servers, new ServerComparator(sortKey, ascending));
        jsonList.addAll(new SmartIterator(query.getLimit().getOrElse(0), query.getOffset().getOrElse(0)).applyLimitAndOffset(servers));
    }
    return RestUtils.okJsonList(getServersListAsJson(jsonList), query.getOffset().getOrElse(0), query.getLimit().getOrElse(0), servers.size());
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) SmartIterator(org.opencastproject.util.SmartIterator) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) ServiceStatistics(org.opencastproject.serviceregistry.api.ServiceStatistics) JSONObject(org.json.simple.JSONObject) SortCriterion(org.opencastproject.matterhorn.search.SortCriterion) HostRegistration(org.opencastproject.serviceregistry.api.HostRegistration) ServersListQuery(org.opencastproject.index.service.resources.list.query.ServersListQuery) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 9 with HostRegistration

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

the class LoadUtil method checkJobFitsCluster.

public static void checkJobFitsCluster(float load, String loadType, List<HostRegistration> hosts) {
    boolean processable = false;
    if (hosts != null) {
        for (HostRegistration host : hosts) {
            if (host.getMaxLoad() >= load) {
                logger.trace("Host " + host.toString() + " can process jobs of type " + loadType + " with load " + load);
                processable = true;
                break;
            }
        }
    }
    if (!processable) {
        logger.warn("No hosts found that can process jobs of type {} with load {}", loadType, load);
    }
}
Also used : HostRegistration(org.opencastproject.serviceregistry.api.HostRegistration)

Example 10 with HostRegistration

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

the class ServersListProvider method getList.

@Override
public Map<String, String> getList(String listName, ResourceListQuery query, Organization organization) throws ListProviderException {
    Map<String, String> list = new HashMap<String, String>();
    if (StringUtils.equalsIgnoreCase(LIST_STATUS, listName)) {
        list.put(SERVER_STATUS_ONLINE, SERVER_STATUS_LABEL_ONLINE);
        list.put(SERVER_STATUS_OFFLINE, SERVER_STATUS_LABEL_OFFLINE);
        list.put(SERVER_STATUS_MAINTENANCE, SERVER_STATUS_LABEL_MAINTENANCE);
        return list;
    }
    ServersListQuery serversQuery;
    try {
        serversQuery = (ServersListQuery) query;
    } catch (ClassCastException ex) {
        serversQuery = new ServersListQuery(query);
    }
    Option<String> fHostname = serversQuery.getHostname();
    Option<String> fStatus = serversQuery.getStatus();
    List<HostRegistration> allServers;
    try {
        allServers = serviceRegistry.getHostRegistrations();
    } catch (ServiceRegistryException e) {
        throw new ListProviderException("Not able to get the list of the hosts from the services registry");
    }
    for (HostRegistration server : allServers) {
        boolean vOnline = server.isOnline();
        boolean vMaintenance = server.isMaintenanceMode();
        String vHostname = server.getBaseUrl();
        if (fHostname.isSome() && !StringUtils.equalsIgnoreCase(StringUtils.trimToEmpty(fHostname.get()), vHostname))
            continue;
        if (fStatus.isSome()) {
            switch(StringUtils.trimToEmpty(fStatus.get())) {
                case SERVER_STATUS_ONLINE:
                    if (!vOnline)
                        continue;
                    break;
                case SERVER_STATUS_OFFLINE:
                    if (vOnline)
                        continue;
                    break;
                case SERVER_STATUS_MAINTENANCE:
                    if (!vMaintenance)
                        continue;
                    break;
                default:
                    break;
            }
        }
        switch(listName) {
            case LIST_HOSTNAME:
            default:
                list.put(vHostname, vHostname);
                break;
        }
    }
    return list;
}
Also used : HashMap(java.util.HashMap) HostRegistration(org.opencastproject.serviceregistry.api.HostRegistration) ListProviderException(org.opencastproject.index.service.exception.ListProviderException) ServersListQuery(org.opencastproject.index.service.resources.list.query.ServersListQuery) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Aggregations

HostRegistration (org.opencastproject.serviceregistry.api.HostRegistration)11 SystemLoad (org.opencastproject.serviceregistry.api.SystemLoad)5 ServiceRegistration (org.opencastproject.serviceregistry.api.ServiceRegistration)4 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)2 Before (org.junit.Before)2 Test (org.junit.Test)2 ServersListQuery (org.opencastproject.index.service.resources.list.query.ServersListQuery)2 Job (org.opencastproject.job.api.Job)2 ServiceRegistry (org.opencastproject.serviceregistry.api.ServiceRegistry)2 NodeLoad (org.opencastproject.serviceregistry.api.SystemLoad.NodeLoad)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 EntityManager (javax.persistence.EntityManager)1 Query (javax.persistence.Query)1 TypedQuery (javax.persistence.TypedQuery)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1