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);
}
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));
}
}
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));
}
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));
}
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");
}
}
Aggregations