Search in sources :

Example 1 with MasterServiceManager

use of io.cdap.cdap.common.twill.MasterServiceManager in project cdap by caskdata.

the class MonitorHandlerAuthorizationTest method createMonitorHandler.

private static MonitorHandler createMonitorHandler(Authorizable authorizable, List<Permission> requiredPermissions) {
    Map<String, MasterServiceManager> masterServiceManagerMap = new HashMap<>();
    MasterServiceManager mockMasterServiceManager = mock(MasterServiceManager.class);
    when(mockMasterServiceManager.getInstances()).thenReturn(1);
    when(mockMasterServiceManager.isServiceEnabled()).thenReturn(true);
    when(mockMasterServiceManager.getMinInstances()).thenReturn(1);
    when(mockMasterServiceManager.getMaxInstances()).thenReturn(1);
    when(mockMasterServiceManager.setInstances(any(Integer.class))).thenReturn(true);
    masterServiceManagerMap.put(SERVICE_NAME, mockMasterServiceManager);
    ServiceStore mockServiceStore = mock(ServiceStore.class);
    InMemoryAccessController inMemoryAccessController = new InMemoryAccessController();
    inMemoryAccessController.grant(authorizable, MASTER_PRINCIPAL, Collections.unmodifiableSet(new HashSet<>(requiredPermissions)));
    AuthenticationContext authenticationContext = new AuthenticationTestContext();
    DefaultContextAccessEnforcer contextAccessEnforcer = new DefaultContextAccessEnforcer(authenticationContext, inMemoryAccessController);
    return new MonitorHandler(masterServiceManagerMap, mockServiceStore, contextAccessEnforcer);
}
Also used : AuthenticationContext(io.cdap.cdap.security.spi.authentication.AuthenticationContext) MasterServiceManager(io.cdap.cdap.common.twill.MasterServiceManager) ServiceStore(io.cdap.cdap.app.store.ServiceStore) HashMap(java.util.HashMap) MonitorHandler(io.cdap.cdap.gateway.handlers.MonitorHandler) InMemoryAccessController(io.cdap.cdap.security.authorization.InMemoryAccessController) AuthenticationTestContext(io.cdap.cdap.security.auth.context.AuthenticationTestContext) DefaultContextAccessEnforcer(io.cdap.cdap.security.authorization.DefaultContextAccessEnforcer) HashSet(java.util.HashSet)

Example 2 with MasterServiceManager

use of io.cdap.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));
    }
    SystemServiceId systemServiceId = new SystemServiceId(serviceName);
    contextAccessEnforcer.enforce(systemServiceId, StandardPermission.GET);
    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.toString());
    } else {
        throw new ForbiddenException(String.format("Service %s is not enabled", serviceName));
    }
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) ForbiddenException(io.cdap.cdap.common.ForbiddenException) MasterServiceManager(io.cdap.cdap.common.twill.MasterServiceManager) JsonObject(com.google.gson.JsonObject) NotFoundException(io.cdap.cdap.common.NotFoundException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 3 with MasterServiceManager

use of io.cdap.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) {
    contextAccessEnforcer.enforceOnParent(EntityType.SYSTEM_SERVICE, InstanceId.SELF, StandardPermission.LIST);
    Map<String, String> result = new HashMap<>();
    for (String service : serviceManagementMap.keySet()) {
        MasterServiceManager masterServiceManager = serviceManagementMap.get(service);
        if (masterServiceManager.isServiceEnabled()) {
            String status = masterServiceManager.isServiceAvailable() ? STATUSOK : STATUSNOTOK;
            result.put(service, status);
        }
    }
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(result));
}
Also used : MasterServiceManager(io.cdap.cdap.common.twill.MasterServiceManager) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 4 with MasterServiceManager

use of io.cdap.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) {
    contextAccessEnforcer.enforceOnParent(EntityType.SYSTEM_SERVICE, InstanceId.SELF, StandardPermission.LIST);
    List<SystemServiceMeta> response = new ArrayList<>();
    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.isServiceAvailable() ? STATUSOK : STATUSNOTOK;
            // 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, GSON.toJson(response));
}
Also used : SystemServiceMeta(io.cdap.cdap.proto.SystemServiceMeta) MasterServiceManager(io.cdap.cdap.common.twill.MasterServiceManager) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 5 with MasterServiceManager

use of io.cdap.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(FullHttpRequest 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));
    }
    SystemServiceId systemServiceId = new SystemServiceId(serviceName);
    contextAccessEnforcer.enforce(systemServiceId, StandardPermission.UPDATE);
    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 : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) ForbiddenException(io.cdap.cdap.common.ForbiddenException) MasterServiceManager(io.cdap.cdap.common.twill.MasterServiceManager) NotFoundException(io.cdap.cdap.common.NotFoundException) BadRequestException(io.cdap.cdap.common.BadRequestException) Path(javax.ws.rs.Path) AuditPolicy(io.cdap.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Aggregations

MasterServiceManager (io.cdap.cdap.common.twill.MasterServiceManager)9 Path (javax.ws.rs.Path)7 ForbiddenException (io.cdap.cdap.common.ForbiddenException)6 NotFoundException (io.cdap.cdap.common.NotFoundException)6 SystemServiceId (io.cdap.cdap.proto.id.SystemServiceId)6 BadRequestException (io.cdap.cdap.common.BadRequestException)4 GET (javax.ws.rs.GET)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 ServiceUnavailableException (io.cdap.cdap.common.ServiceUnavailableException)3 HashMap (java.util.HashMap)2 PUT (javax.ws.rs.PUT)2 JsonObject (com.google.gson.JsonObject)1 ServiceStore (io.cdap.cdap.app.store.ServiceStore)1 AuditPolicy (io.cdap.cdap.common.security.AuditPolicy)1 MonitorHandler (io.cdap.cdap.gateway.handlers.MonitorHandler)1 SystemServiceMeta (io.cdap.cdap.proto.SystemServiceMeta)1 AuthenticationTestContext (io.cdap.cdap.security.auth.context.AuthenticationTestContext)1 DefaultContextAccessEnforcer (io.cdap.cdap.security.authorization.DefaultContextAccessEnforcer)1 InMemoryAccessController (io.cdap.cdap.security.authorization.InMemoryAccessController)1 AuthenticationContext (io.cdap.cdap.security.spi.authentication.AuthenticationContext)1