use of io.cdap.cdap.proto.id.SystemServiceId in project cdap by caskdata.
the class MonitorClient method getSystemServiceInstances.
/**
* Gets the number of instances the system service is running on.
*
* @param serviceName name of the system service
* @return number of instances the system service is running on
* @throws IOException if a network error occurred
* @throws NotFoundException if the system service with the specified name was not found
* @throws ServiceNotEnabledException if the system service is not currently enabled
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public int getSystemServiceInstances(String serviceName) throws IOException, NotFoundException, ServiceNotEnabledException, UnauthenticatedException, UnauthorizedException {
URL url = config.resolveURL(String.format("system/services/%s/instances", serviceName));
HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_FORBIDDEN);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(new SystemServiceId(serviceName));
} else if (response.getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN) {
throw new ServiceNotEnabledException(serviceName);
}
return ObjectResponse.fromJsonBody(response, Instances.class).getResponseObject().getInstances();
}
use of io.cdap.cdap.proto.id.SystemServiceId in project cdap by caskdata.
the class MonitorClient method getSystemServiceStatus.
/**
* Gets the status of a system service.
*
* @param serviceName Name of the system service
* @return status of the system service
* @throws IOException if a network error occurred
* @throws NotFoundException if the system service with the specified name could not be found
* @throws BadRequestException if the operation was not valid for the system service
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public String getSystemServiceStatus(String serviceName) throws IOException, NotFoundException, BadRequestException, UnauthenticatedException, UnauthorizedException {
URL url = config.resolveURL(String.format("system/services/%s/status", serviceName));
HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
String responseBody = new String(response.getResponseBody());
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(new SystemServiceId(serviceName));
} else if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException(responseBody);
}
Map<String, String> status = GSON.fromJson(responseBody, new TypeToken<Map<String, String>>() {
}.getType());
return status.get("status");
}
use of io.cdap.cdap.proto.id.SystemServiceId 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(FullHttpRequest request, HttpResponder responder, @PathParam("service-name") 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 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");
}
}
use of io.cdap.cdap.proto.id.SystemServiceId 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));
}
SystemServiceId systemServiceId = new SystemServiceId(serviceName);
contextAccessEnforcer.enforce(systemServiceId, ApplicationPermission.EXECUTE);
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);
}
}
}
use of io.cdap.cdap.proto.id.SystemServiceId in project cdap by caskdata.
the class MonitorHandler method getLatestRestartServiceInstanceStatus.
/**
* Send request to get the status of latest restart instances request for a CDAP system service.
*/
@Path("/system/services/{service-name}/latest-restart")
@GET
public void getLatestRestartServiceInstanceStatus(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));
}
SystemServiceId systemServiceId = new SystemServiceId(serviceName);
contextAccessEnforcer.enforce(systemServiceId, StandardPermission.GET);
try {
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(serviceStore.getLatestRestartInstancesRequest(serviceName)));
} catch (IllegalStateException ex) {
throw new NotFoundException(String.format("No restart instances request found or %s", serviceName));
}
}
Aggregations