Search in sources :

Example 6 with MasterServiceManager

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

the class MonitorHandler method resetServiceLogLevels.

/**
   * Reset the log levels of the service.
   * All loggers will be reset to the level when the service started.
   */
@Path("system/services/{service-name}/resetloglevels")
@POST
public void resetServiceLogLevels(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 reset log levels for service %s " + "because the service is not enabled", serviceName));
    }
    try {
        Set<String> loggerNames = parseBody(request, SET_STRING_TYPE);
        masterServiceManager.resetServiceLogLevels(loggerNames == null ? Collections.<String>emptySet() : loggerNames);
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (IllegalStateException ise) {
        throw new ServiceUnavailableException(String.format("Failed to reset log levels for service %s " + "because the service may not be ready yet", serviceName));
    } 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) POST(javax.ws.rs.POST)

Example 7 with MasterServiceManager

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

the class MonitorHandler method monitor.

@Path("/system/services/{service-name}/status")
@GET
public void monitor(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("Service %s is not enabled", serviceName));
    }
    if (masterServiceManager.canCheckStatus() && masterServiceManager.isServiceAvailable()) {
        JsonObject json = new JsonObject();
        json.addProperty("status", STATUSOK);
        responder.sendJson(HttpResponseStatus.OK, json);
    } else if (masterServiceManager.canCheckStatus()) {
        JsonObject json = new JsonObject();
        json.addProperty("status", STATUSNOTOK);
        responder.sendJson(HttpResponseStatus.OK, json);
    } else {
        throw new BadRequestException("Operation not valid for this service");
    }
}
Also used : ForbiddenException(co.cask.cdap.common.ForbiddenException) MasterServiceManager(co.cask.cdap.common.twill.MasterServiceManager) NotFoundException(co.cask.cdap.common.NotFoundException) JsonObject(com.google.gson.JsonObject) BadRequestException(co.cask.cdap.common.BadRequestException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 8 with MasterServiceManager

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

the class MonitorHandler method setServiceInstance.

/**
   * Sets the number of instances of CDAP Services
   */
@Path("/system/services/{service-name}/instances")
@PUT
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void setServiceInstance(HttpRequest request, HttpResponder responder, @PathParam("service-name") final String serviceName) throws Exception {
    if (!serviceManagementMap.containsKey(serviceName)) {
        throw new NotFoundException(String.format("Invalid service name %s", serviceName));
    }
    MasterServiceManager serviceManager = serviceManagementMap.get(serviceName);
    int instances = getInstances(request);
    if (!serviceManager.isServiceEnabled()) {
        throw new ForbiddenException(String.format("Service %s is not enabled", serviceName));
    }
    int currentInstances = getSystemServiceInstanceCount(serviceName);
    if (instances < serviceManager.getMinInstances() || instances > serviceManager.getMaxInstances()) {
        String response = String.format("Instance count should be between [%s,%s]", serviceManager.getMinInstances(), serviceManager.getMaxInstances());
        throw new BadRequestException(response);
    } else if (instances == currentInstances) {
        responder.sendStatus(HttpResponseStatus.OK);
        return;
    }
    serviceStore.setServiceInstance(serviceName, instances);
    if (serviceManager.setInstances(instances)) {
        responder.sendStatus(HttpResponseStatus.OK);
    } else {
        throw new BadRequestException("Operation did not succeed");
    }
}
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) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

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