Search in sources :

Example 1 with MasterServiceManager

use of co.cask.cdap.common.twill.MasterServiceManager in project cdap by caskdata.

the class MonitorHandler method getBootStatus.

// Return the status of CDAP services in JSON format
@Path("/system/services/status")
@GET
public void getBootStatus(HttpRequest request, HttpResponder responder) {
    Map<String, String> result = new HashMap<>();
    for (String service : serviceManagementMap.keySet()) {
        MasterServiceManager masterServiceManager = serviceManagementMap.get(service);
        if (masterServiceManager.isServiceEnabled() && masterServiceManager.canCheckStatus()) {
            String status = masterServiceManager.isServiceAvailable() ? STATUSOK : STATUSNOTOK;
            result.put(service, status);
        }
    }
    responder.sendJson(HttpResponseStatus.OK, result);
}
Also used : MasterServiceManager(co.cask.cdap.common.twill.MasterServiceManager) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with MasterServiceManager

use of co.cask.cdap.common.twill.MasterServiceManager in project cdap by caskdata.

the class MonitorHandler method updateServiceLogLevels.

/**
   * Update log levels for this service.
   */
@Path("system/services/{service-name}/loglevels")
@PUT
public void updateServiceLogLevels(HttpRequest request, HttpResponder responder, @PathParam("service-name") String serviceName) throws Exception {
    if (!serviceManagementMap.containsKey(serviceName)) {
        throw new NotFoundException(String.format("Invalid service name %s", serviceName));
    }
    MasterServiceManager masterServiceManager = serviceManagementMap.get(serviceName);
    if (!masterServiceManager.isServiceEnabled()) {
        throw new ForbiddenException(String.format("Failed to update log levels for service %s " + "because the service is not enabled", serviceName));
    }
    try {
        // we are decoding the body to Map<String, String> instead of Map<String, LogEntry.Level> here since Gson will
        // serialize invalid enum values to null, which is allowed for log level, instead of throw an Exception.
        masterServiceManager.updateServiceLogLevels(transformLogLevelsMap(decodeArguments(request)));
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (IllegalStateException ise) {
        throw new ServiceUnavailableException(String.format("Failed to update log levels for service %s " + "because the service may not be ready yet", serviceName));
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e.getMessage());
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Invalid Json in the body");
    }
}
Also used : ForbiddenException(co.cask.cdap.common.ForbiddenException) JsonSyntaxException(com.google.gson.JsonSyntaxException) MasterServiceManager(co.cask.cdap.common.twill.MasterServiceManager) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Example 3 with MasterServiceManager

use of co.cask.cdap.common.twill.MasterServiceManager in project cdap by caskdata.

the class MonitorHandler method getServiceSpec.

@Path("/system/services")
@GET
public void getServiceSpec(HttpRequest request, HttpResponder responder) throws Exception {
    List<SystemServiceMeta> response = Lists.newArrayList();
    SortedSet<String> services = new TreeSet<>(serviceManagementMap.keySet());
    List<String> serviceList = new ArrayList<>(services);
    for (String service : serviceList) {
        MasterServiceManager serviceManager = serviceManagementMap.get(service);
        if (serviceManager.isServiceEnabled()) {
            String logs = serviceManager.isLogAvailable() ? Constants.Monitor.STATUS_OK : Constants.Monitor.STATUS_NOTOK;
            String canCheck = serviceManager.canCheckStatus() ? (serviceManager.isServiceAvailable() ? STATUSOK : STATUSNOTOK) : NOTAPPLICABLE;
            //TODO: Add metric name for Event Rate monitoring
            response.add(new SystemServiceMeta(service, serviceManager.getDescription(), canCheck, logs, serviceManager.getMinInstances(), serviceManager.getMaxInstances(), getSystemServiceInstanceCount(service), serviceManager.getInstances()));
        }
    }
    responder.sendJson(HttpResponseStatus.OK, response);
}
Also used : SystemServiceMeta(co.cask.cdap.proto.SystemServiceMeta) MasterServiceManager(co.cask.cdap.common.twill.MasterServiceManager) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 4 with MasterServiceManager

use of co.cask.cdap.common.twill.MasterServiceManager in project cdap by caskdata.

the class MonitorHandler method restartInstances.

private void restartInstances(HttpResponder responder, String serviceName, int instanceId, boolean restartAll) throws Exception {
    long startTimeMs = System.currentTimeMillis();
    boolean isSuccess = true;
    if (!serviceManagementMap.containsKey(serviceName)) {
        throw new NotFoundException(String.format("Invalid service name %s", serviceName));
    }
    MasterServiceManager masterServiceManager = serviceManagementMap.get(serviceName);
    try {
        if (!masterServiceManager.isServiceEnabled()) {
            String message = String.format("Failed to restart instance for %s because the service is not enabled.", serviceName);
            LOG.debug(message);
            isSuccess = false;
            throw new ForbiddenException(message);
        }
        if (restartAll) {
            masterServiceManager.restartAllInstances();
        } else {
            if (instanceId < 0 || instanceId >= masterServiceManager.getInstances()) {
                throw new IllegalArgumentException();
            }
            masterServiceManager.restartInstances(instanceId);
        }
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (IllegalStateException ise) {
        String message = String.format("Failed to restart instance for %s because the service may not be ready yet", serviceName);
        LOG.debug(message, ise);
        isSuccess = false;
        throw new ServiceUnavailableException(message);
    } catch (IllegalArgumentException iex) {
        String message = String.format("Failed to restart instance %d for service: %s because invalid instance id", instanceId, serviceName);
        LOG.debug(message, iex);
        isSuccess = false;
        throw new BadRequestException(message);
    } catch (Exception ex) {
        LOG.warn(String.format("Exception when trying to restart instances for service %s", serviceName), ex);
        isSuccess = false;
        throw new Exception(String.format("Error restarting instance %d for service: %s", instanceId, serviceName));
    } finally {
        long endTimeMs = System.currentTimeMillis();
        if (restartAll) {
            serviceStore.setRestartAllInstancesRequest(serviceName, startTimeMs, endTimeMs, isSuccess);
        } else {
            serviceStore.setRestartInstanceRequest(serviceName, startTimeMs, endTimeMs, isSuccess, instanceId);
        }
    }
}
Also used : ForbiddenException(co.cask.cdap.common.ForbiddenException) MasterServiceManager(co.cask.cdap.common.twill.MasterServiceManager) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) JsonSyntaxException(com.google.gson.JsonSyntaxException) ForbiddenException(co.cask.cdap.common.ForbiddenException) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException)

Example 5 with MasterServiceManager

use of co.cask.cdap.common.twill.MasterServiceManager in project cdap by caskdata.

the class MonitorHandler method getServiceInstance.

/**
   * Returns the number of instances of CDAP Services
   */
@Path("/system/services/{service-name}/instances")
@GET
public void getServiceInstance(HttpRequest request, HttpResponder responder, @PathParam("service-name") String serviceName) throws Exception {
    JsonObject reply = new JsonObject();
    if (!serviceManagementMap.containsKey(serviceName)) {
        throw new NotFoundException(String.format("Invalid service name %s", serviceName));
    }
    MasterServiceManager serviceManager = serviceManagementMap.get(serviceName);
    if (serviceManager.isServiceEnabled()) {
        int actualInstance = serviceManagementMap.get(serviceName).getInstances();
        reply.addProperty("provisioned", actualInstance);
        reply.addProperty("requested", getSystemServiceInstanceCount(serviceName));
        responder.sendJson(HttpResponseStatus.OK, reply);
    } else {
        throw new ForbiddenException(String.format("Service %s is not enabled", serviceName));
    }
}
Also used : ForbiddenException(co.cask.cdap.common.ForbiddenException) MasterServiceManager(co.cask.cdap.common.twill.MasterServiceManager) JsonObject(com.google.gson.JsonObject) NotFoundException(co.cask.cdap.common.NotFoundException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

MasterServiceManager (co.cask.cdap.common.twill.MasterServiceManager)8 Path (javax.ws.rs.Path)7 ForbiddenException (co.cask.cdap.common.ForbiddenException)6 NotFoundException (co.cask.cdap.common.NotFoundException)6 BadRequestException (co.cask.cdap.common.BadRequestException)5 GET (javax.ws.rs.GET)4 ServiceUnavailableException (co.cask.cdap.common.ServiceUnavailableException)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 JsonObject (com.google.gson.JsonObject)2 PUT (javax.ws.rs.PUT)2 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)1 SystemServiceMeta (co.cask.cdap.proto.SystemServiceMeta)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1 POST (javax.ws.rs.POST)1