Search in sources :

Example 16 with ConflictException

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

the class RemoteDatasetOpExecutor method doRequest.

private HttpResponse doRequest(DatasetId datasetInstanceId, String opName, @Nullable String body) throws IOException, ConflictException, UnauthorizedException {
    String path = String.format("namespaces/%s/data/datasets/%s/admin/%s", datasetInstanceId.getNamespace(), datasetInstanceId.getEntityName(), opName);
    LOG.trace("executing POST on {} with body {}", path, body);
    try {
        HttpRequest.Builder builder = remoteClient.requestBuilder(HttpMethod.POST, path);
        if (body != null) {
            builder.withBody(body);
        }
        String userId = authenticationContext.getPrincipal().getName();
        if (userId != null) {
            builder.addHeader(Constants.Security.Headers.USER_ID, userId);
        }
        HttpResponse httpResponse = remoteClient.execute(builder.build());
        LOG.trace("executed POST on {} with body {}: {}", path, body, httpResponse.getResponseCode());
        verifyResponse(httpResponse);
        return httpResponse;
    } catch (Exception e) {
        LOG.trace("Caught exception for POST on {} with body {}", path, body, e);
        throw e;
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) IOException(java.io.IOException) ConflictException(io.cdap.cdap.common.ConflictException) HandlerException(io.cdap.cdap.common.HandlerException)

Example 17 with ConflictException

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

the class WorkflowHttpHandler method resumeWorkflowRun.

@POST
@Path("/apps/{app-id}/workflows/{workflow-name}/runs/{run-id}/resume")
public void resumeWorkflowRun(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("workflow-name") String workflowName, @PathParam("run-id") String runId) throws Exception {
    ProgramController controller = getProgramController(namespaceId, appId, workflowName, runId);
    if (controller.getState() == ProgramController.State.ALIVE) {
        throw new ConflictException("Program is already running");
    }
    controller.resume().get();
    responder.sendString(HttpResponseStatus.OK, "Program run resumed.");
}
Also used : ProgramController(io.cdap.cdap.app.runtime.ProgramController) ConflictException(io.cdap.cdap.common.ConflictException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 18 with ConflictException

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

the class WorkflowHttpHandler method suspendWorkflowRun.

@POST
@Path("/apps/{app-id}/workflows/{workflow-name}/runs/{run-id}/suspend")
public void suspendWorkflowRun(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("workflow-name") String workflowName, @PathParam("run-id") String runId) throws Exception {
    ProgramController controller = getProgramController(namespaceId, appId, workflowName, runId);
    if (controller.getState() == ProgramController.State.SUSPENDED) {
        throw new ConflictException("Program run already suspended");
    }
    controller.suspend().get();
    responder.sendString(HttpResponseStatus.OK, "Program run suspended.");
}
Also used : ProgramController(io.cdap.cdap.app.runtime.ProgramController) ConflictException(io.cdap.cdap.common.ConflictException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 19 with ConflictException

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

the class ProgramLifecycleHttpHandler method startPrograms.

/**
 * Starts all programs that are passed into the data. The data is an array of JSON objects
 * where each object must contain the following three elements: appId, programType, and programId
 * (flow name, service name, etc.). In additional, each object can contain an optional runtimeargs element,
 * which is a map of arguments to start the program with.
 * <p>
 * Example input:
 * <pre><code>
 * [{"appId": "App1", "programType": "Service", "programId": "Service1"},
 * {"appId": "App1", "programType": "Mapreduce", "programId": "MapReduce2", "runtimeargs":{"arg1":"val1"}}]
 * </code></pre>
 * </p><p>
 * The response will be an array of JsonObjects each of which will contain the three input parameters
 * as well as a "statusCode" field which maps to the status code for the data in that JsonObjects.
 * </p><p>
 * If an error occurs in the input (for the example above, App2 does not exist), then all JsonObjects for which the
 * parameters have a valid status will have the status field but all JsonObjects for which the parameters do not have
 * a valid status will have an error message and statusCode.
 * </p><p>
 * For example, if there is no App2 in the data above, then the response would be 200 OK with following possible data:
 * </p>
 * <pre><code>
 * [{"appId": "App1", "programType": "Service", "programId": "Service1", "statusCode": 200},
 * {"appId": "App2", "programType": "Mapreduce", "programId": "Mapreduce2",
 *  "statusCode":404, "error": "App: App2 not found"}]
 * </code></pre>
 */
@POST
@Path("/start")
@AuditPolicy({ AuditDetail.REQUEST_BODY, AuditDetail.RESPONSE_BODY })
public void startPrograms(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
    List<BatchProgramStart> programs = validateAndGetBatchInput(request, BATCH_STARTS_TYPE);
    List<BatchProgramResult> output = new ArrayList<>(programs.size());
    for (BatchProgramStart program : programs) {
        ProgramId programId = new ProgramId(namespaceId, program.getAppId(), program.getProgramType(), program.getProgramId());
        try {
            String runId = lifecycleService.run(programId, program.getRuntimeargs(), false).getId();
            output.add(new BatchProgramResult(program, HttpResponseStatus.OK.code(), null, runId));
        } catch (NotFoundException e) {
            output.add(new BatchProgramResult(program, HttpResponseStatus.NOT_FOUND.code(), e.getMessage()));
        } catch (BadRequestException e) {
            output.add(new BatchProgramResult(program, HttpResponseStatus.BAD_REQUEST.code(), e.getMessage()));
        } catch (ConflictException e) {
            output.add(new BatchProgramResult(program, HttpResponseStatus.CONFLICT.code(), e.getMessage()));
        }
    }
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(output));
}
Also used : ConflictException(io.cdap.cdap.common.ConflictException) BatchProgramResult(io.cdap.cdap.proto.BatchProgramResult) ArrayList(java.util.ArrayList) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) BadRequestException(io.cdap.cdap.common.BadRequestException) BatchProgramStart(io.cdap.cdap.proto.BatchProgramStart) ProgramId(io.cdap.cdap.proto.id.ProgramId) Path(javax.ws.rs.Path) AuditPolicy(io.cdap.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST)

Example 20 with ConflictException

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

the class SystemProgramManagementService method startPrograms.

private void startPrograms(Map<ProgramId, Arguments> enabledProgramsMap) {
    for (ProgramId programId : enabledProgramsMap.keySet()) {
        Map<String, String> overrides = enabledProgramsMap.get(programId).asMap();
        LOG.debug("Starting program {} with args {}", programId, overrides);
        try {
            programLifecycleService.start(programId, overrides, false, false);
        } catch (ConflictException ex) {
            // Ignore if the program is already running.
            LOG.debug("Program {} is already running.", programId);
        } catch (Exception ex) {
            LOG.warn("Could not start program {} , will be retried in next run.", programId, ex);
        }
    }
}
Also used : ConflictException(io.cdap.cdap.common.ConflictException) ProgramId(io.cdap.cdap.proto.id.ProgramId) ConflictException(io.cdap.cdap.common.ConflictException)

Aggregations

ConflictException (io.cdap.cdap.common.ConflictException)36 NotFoundException (io.cdap.cdap.common.NotFoundException)14 IOException (java.io.IOException)10 POST (javax.ws.rs.POST)10 Path (javax.ws.rs.Path)10 BadRequestException (io.cdap.cdap.common.BadRequestException)8 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)8 ProgramId (io.cdap.cdap.proto.id.ProgramId)8 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)8 JsonSyntaxException (com.google.gson.JsonSyntaxException)6 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)6 AccessException (io.cdap.cdap.api.security.AccessException)6 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)6 ArtifactAlreadyExistsException (io.cdap.cdap.common.ArtifactAlreadyExistsException)6 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)6 File (java.io.File)6 InvalidArtifactException (io.cdap.cdap.common.InvalidArtifactException)5 NotImplementedException (io.cdap.cdap.common.NotImplementedException)5 ServiceException (io.cdap.cdap.common.ServiceException)5 WriteConflictException (io.cdap.cdap.internal.app.runtime.artifact.WriteConflictException)5