Search in sources :

Example 96 with NotFoundException

use of io.cdap.cdap.common.NotFoundException in project cdap by cdapio.

the class ProgramLifecycleHttpHandler method getStatus.

/**
 * Returns status of a type specified by the type{flows,workflows,mapreduce,spark,services,schedules}.
 */
@GET
@Path("/apps/{app-id}/versions/{version-id}/{program-type}/{program-id}/status")
public void getStatus(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("version-id") String versionId, @PathParam("program-type") String type, @PathParam("program-id") String programId) throws Exception {
    ApplicationId applicationId = new ApplicationId(namespaceId, appId, versionId);
    if (SCHEDULES.equals(type)) {
        JsonObject json = new JsonObject();
        ScheduleId scheduleId = applicationId.schedule(programId);
        ApplicationSpecification appSpec = store.getApplication(applicationId);
        if (appSpec == null) {
            throw new NotFoundException(applicationId);
        }
        json.addProperty("status", programScheduleService.getStatus(scheduleId).toString());
        responder.sendJson(HttpResponseStatus.OK, json.toString());
        return;
    }
    ProgramType programType = getProgramType(type);
    ProgramId program = applicationId.program(programType, programId);
    ProgramStatus programStatus = lifecycleService.getProgramStatus(program);
    Map<String, String> status = ImmutableMap.of("status", programStatus.name());
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(status));
}
Also used : ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) JsonObject(com.google.gson.JsonObject) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) ProgramType(io.cdap.cdap.proto.ProgramType) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) ProgramId(io.cdap.cdap.proto.id.ProgramId) ProgramStatus(io.cdap.cdap.proto.ProgramStatus) BatchProgramStatus(io.cdap.cdap.proto.BatchProgramStatus) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 97 with NotFoundException

use of io.cdap.cdap.common.NotFoundException in project cdap by cdapio.

the class ProvisionerHttpHandler method getProvisioner.

@GET
@Path("/provisioners/{provisioner-name}")
public void getProvisioner(HttpRequest request, HttpResponder responder, @PathParam("provisioner-name") String provisionerName) throws NotFoundException {
    ProvisionerDetail provisionerDetail = provisioningService.getProvisionerDetail(provisionerName);
    if (provisionerDetail == null) {
        throw new NotFoundException(String.format("Provisioner %s not found", provisionerName));
    }
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(provisionerDetail));
}
Also used : NotFoundException(io.cdap.cdap.common.NotFoundException) ProvisionerDetail(io.cdap.cdap.proto.provisioner.ProvisionerDetail) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 98 with NotFoundException

use of io.cdap.cdap.common.NotFoundException in project cdap by cdapio.

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));
    }
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) NotFoundException(io.cdap.cdap.common.NotFoundException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 99 with NotFoundException

use of io.cdap.cdap.common.NotFoundException in project cdap by cdapio.

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)

Example 100 with NotFoundException

use of io.cdap.cdap.common.NotFoundException in project cdap by cdapio.

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);
        }
    }
}
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) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) ForbiddenException(io.cdap.cdap.common.ForbiddenException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) JsonSyntaxException(com.google.gson.JsonSyntaxException) BadRequestException(io.cdap.cdap.common.BadRequestException) NotFoundException(io.cdap.cdap.common.NotFoundException)

Aggregations

NotFoundException (io.cdap.cdap.common.NotFoundException)266 HttpResponse (io.cdap.common.http.HttpResponse)86 URL (java.net.URL)76 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)54 ProgramId (io.cdap.cdap.proto.id.ProgramId)52 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)50 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)50 Path (javax.ws.rs.Path)50 BadRequestException (io.cdap.cdap.common.BadRequestException)42 ProgramNotFoundException (io.cdap.cdap.common.ProgramNotFoundException)42 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)36 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)36 IOException (java.io.IOException)36 GET (javax.ws.rs.GET)36 Test (org.junit.Test)30 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)28 HashMap (java.util.HashMap)28 ProgramSpecification (io.cdap.cdap.api.ProgramSpecification)26 ConflictException (io.cdap.cdap.common.ConflictException)26 ArrayList (java.util.ArrayList)26