Search in sources :

Example 6 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class ApplicationClientTestRun method testAppUpdate.

@Test
public void testAppUpdate() throws Exception {
    String artifactName = "cfg-programs";
    ArtifactId artifactIdV1 = NamespaceId.DEFAULT.artifact(artifactName, "1.0.0");
    ArtifactId artifactIdV2 = NamespaceId.DEFAULT.artifact(artifactName, "2.0.0");
    ApplicationId appId = NamespaceId.DEFAULT.app("ProgramsApp");
    artifactClient.add(NamespaceId.DEFAULT, artifactName, Files.newInputStreamSupplier(createAppJarFile(ConfigurableProgramsApp.class)), "1.0.0");
    artifactClient.add(NamespaceId.DEFAULT, artifactName, Files.newInputStreamSupplier(createAppJarFile(ConfigurableProgramsApp2.class)), "2.0.0");
    try {
        // deploy the app with just the worker
        ConfigurableProgramsApp.Programs conf = new ConfigurableProgramsApp.Programs(null, "worker1", "stream1", "dataset1");
        AppRequest<ConfigurableProgramsApp.Programs> request = new AppRequest<>(new ArtifactSummary(artifactIdV1.getArtifact(), artifactIdV1.getVersion()), conf);
        appClient.deploy(appId, request);
        // should only have the worker
        Assert.assertTrue(appClient.listPrograms(appId, ProgramType.FLOW).isEmpty());
        Assert.assertEquals(1, appClient.listPrograms(appId, ProgramType.WORKER).size());
        // update to use just the flow
        conf = new ConfigurableProgramsApp.Programs("flow1", null, "stream1", "dataset1");
        request = new AppRequest<>(new ArtifactSummary(artifactIdV1.getArtifact(), artifactIdV1.getVersion()), conf);
        appClient.update(appId, request);
        // should only have the flow
        Assert.assertTrue(appClient.listPrograms(appId, ProgramType.WORKER).isEmpty());
        Assert.assertEquals(1, appClient.listPrograms(appId, ProgramType.FLOW).size());
        // check nonexistent app is not found
        try {
            appClient.update(NamespaceId.DEFAULT.app("ghost"), request);
            Assert.fail();
        } catch (NotFoundException e) {
        // expected
        }
        // check different artifact name is invalid
        request = new AppRequest<>(new ArtifactSummary("ghost", artifactIdV1.getVersion()), conf);
        try {
            appClient.update(appId, request);
            Assert.fail();
        } catch (BadRequestException e) {
        // expected
        }
        // check nonexistent artifact is not found
        request = new AppRequest<>(new ArtifactSummary(artifactIdV1.getArtifact(), "0.0.1"), conf);
        try {
            appClient.update(appId, request);
            Assert.fail();
        } catch (NotFoundException e) {
        // expected
        }
        // update artifact version. This version uses a different app class with that can add a service
        ConfigurableProgramsApp2.Programs conf2 = new ConfigurableProgramsApp2.Programs(null, null, "stream1", "dataset1", "service2");
        AppRequest<ConfigurableProgramsApp2.Programs> request2 = new AppRequest<>(new ArtifactSummary(artifactIdV2.getArtifact(), artifactIdV2.getVersion()), conf2);
        appClient.update(appId, request2);
        // should only have a single service
        Assert.assertTrue(appClient.listPrograms(appId, ProgramType.WORKER).isEmpty());
        Assert.assertTrue(appClient.listPrograms(appId, ProgramType.FLOW).isEmpty());
        Assert.assertEquals(1, appClient.listPrograms(appId, ProgramType.SERVICE).size());
    } finally {
        appClient.delete(appId);
        appClient.waitForDeleted(appId, 30, TimeUnit.SECONDS);
        artifactClient.delete(artifactIdV1);
        artifactClient.delete(artifactIdV2);
    }
}
Also used : ArtifactId(co.cask.cdap.proto.id.ArtifactId) ConfigurableProgramsApp(co.cask.cdap.client.app.ConfigurableProgramsApp) DatasetModuleNotFoundException(co.cask.cdap.common.DatasetModuleNotFoundException) DatasetNotFoundException(co.cask.cdap.common.DatasetNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) AppRequest(co.cask.cdap.proto.artifact.AppRequest) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ConfigurableProgramsApp2(co.cask.cdap.client.app.ConfigurableProgramsApp2) BadRequestException(co.cask.cdap.common.BadRequestException) ApplicationId(co.cask.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 7 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class WorkflowStatsSLAHttpHandler method workflowStats.

/**
   * Returns the statistics for a given workflow.
   *
   * @param request The request
   * @param responder The responder
   * @param namespaceId The namespace the application is in
   * @param appId The application the workflow is in
   * @param workflowId The workflow that needs to have it stats shown
   * @param start The start time of the range
   * @param end The end time of the range
   * @param percentiles The list of percentile values on which visibility is needed
   */
@GET
@Path("apps/{app-id}/workflows/{workflow-id}/statistics")
public void workflowStats(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("workflow-id") String workflowId, @QueryParam("start") @DefaultValue("now-1d") String start, @QueryParam("end") @DefaultValue("now") String end, @QueryParam("percentile") @DefaultValue("90.0") List<Double> percentiles) throws Exception {
    long startTime = TimeMathParser.parseTimeInSeconds(start);
    long endTime = TimeMathParser.parseTimeInSeconds(end);
    if (startTime < 0) {
        throw new BadRequestException("Invalid start time. The time you entered was : " + startTime);
    } else if (endTime < 0) {
        throw new BadRequestException("Invalid end time. The time you entered was : " + endTime);
    } else if (endTime < startTime) {
        throw new BadRequestException("Start time : " + startTime + " cannot be larger than end time : " + endTime);
    }
    for (double i : percentiles) {
        if (i < 0.0 || i > 100.0) {
            throw new BadRequestException("Percentile values have to be greater than or equal to 0 and" + " less than or equal to 100. Invalid input was " + Double.toString(i));
        }
    }
    WorkflowId workflow = new WorkflowId(namespaceId, appId, workflowId);
    WorkflowStatistics workflowStatistics = store.getWorkflowStatistics(workflow, startTime, endTime, percentiles);
    if (workflowStatistics == null) {
        responder.sendString(HttpResponseStatus.OK, "There are no statistics associated with this workflow : " + workflowId + " in the specified time range.");
        return;
    }
    responder.sendJson(HttpResponseStatus.OK, workflowStatistics);
}
Also used : BadRequestException(co.cask.cdap.common.BadRequestException) WorkflowId(co.cask.cdap.proto.id.WorkflowId) WorkflowStatistics(co.cask.cdap.proto.WorkflowStatistics) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 8 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class ProgramLifecycleService method issueStop.

/**
   * Issues a command to stop the specified {@link RunId} of the specified {@link ProgramId} and returns a
   * {@link ListenableFuture} with the {@link ProgramController} for it.
   * Clients can wait for completion of the {@link ListenableFuture}.
   *
   * @param programId the {@link ProgramId program} to issue a stop for
   * @param runId the runId of the program run to stop. If null, all runs of the program as returned by
   *              {@link ProgramRuntimeService} are stopped.
   * @return a list of {@link ListenableFuture} with a {@link ProgramController} that clients can wait on for stop
   *         to complete.
   * @throws NotFoundException if the app, program or run was not found
   * @throws BadRequestException if an attempt is made to stop a program that is either not running or
   *                             was started by a workflow
   * @throws UnauthorizedException if the user issuing the command is not authorized to stop the program. To stop a
   *                               program, a user requires {@link Action#EXECUTE} permission on the program.
   */
public List<ListenableFuture<ProgramController>> issueStop(ProgramId programId, @Nullable String runId) throws Exception {
    authorizationEnforcer.enforce(programId, authenticationContext.getPrincipal(), Action.EXECUTE);
    List<ProgramRuntimeService.RuntimeInfo> runtimeInfos = findRuntimeInfo(programId, runId);
    if (runtimeInfos.isEmpty()) {
        if (!store.applicationExists(programId.getParent())) {
            throw new ApplicationNotFoundException(programId.getParent());
        } else if (!store.programExists(programId)) {
            throw new ProgramNotFoundException(programId);
        } else if (runId != null) {
            ProgramRunId programRunId = programId.run(runId);
            // Check if the program is running and is started by the Workflow
            RunRecordMeta runRecord = store.getRun(programId, runId);
            if (runRecord != null && runRecord.getProperties().containsKey("workflowrunid") && runRecord.getStatus().equals(ProgramRunStatus.RUNNING)) {
                String workflowRunId = runRecord.getProperties().get("workflowrunid");
                throw new BadRequestException(String.format("Cannot stop the program '%s' started by the Workflow " + "run '%s'. Please stop the Workflow.", programRunId, workflowRunId));
            }
            throw new NotFoundException(programRunId);
        }
        throw new BadRequestException(String.format("Program '%s' is not running.", programId));
    }
    List<ListenableFuture<ProgramController>> futures = new ArrayList<>();
    for (ProgramRuntimeService.RuntimeInfo runtimeInfo : runtimeInfos) {
        futures.add(runtimeInfo.getController().stop());
    }
    return futures;
}
Also used : RuntimeInfo(co.cask.cdap.app.runtime.ProgramRuntimeService.RuntimeInfo) RunRecordMeta(co.cask.cdap.internal.app.store.RunRecordMeta) ArrayList(java.util.ArrayList) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) RuntimeInfo(co.cask.cdap.app.runtime.ProgramRuntimeService.RuntimeInfo) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) ProgramRuntimeService(co.cask.cdap.app.runtime.ProgramRuntimeService)

Example 9 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class ProgramLifecycleService method suspendResumeSchedule.

/**
   * Performs an action (suspend/resume) on the given schedule
   *
   * @param scheduleId Id of the schedule
   * @param action the action to perform
   * @throws Exception if the given action is invalid or failed to perform a valid action on the schedule
   */
public void suspendResumeSchedule(ScheduleId scheduleId, String action) throws Exception {
    boolean doEnable;
    if (action.equals("disable") || action.equals("suspend")) {
        doEnable = false;
    } else if (action.equals("enable") || action.equals("resume")) {
        doEnable = true;
    } else {
        throw new BadRequestException("Action for schedules may only be 'enable', 'disable', 'suspend', or 'resume' but is'" + action + "'");
    }
    ProgramSchedule schedule = scheduler.getSchedule(scheduleId);
    authorizationEnforcer.enforce(schedule.getProgramId(), authenticationContext.getPrincipal(), Action.EXECUTE);
    if (doEnable) {
        scheduler.enableSchedule(scheduleId);
    } else {
        scheduler.disableSchedule(scheduleId);
    }
}
Also used : ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) BadRequestException(co.cask.cdap.common.BadRequestException)

Example 10 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class StreamClient method create.

/**
   * Creates a stream with {@link StreamProperties} properties.
   *
   * @param newStreamId ID of the new stream to create
   * @param properties {@link StreamProperties} for the new stream
   * @throws IOException if a network error occurred
   * @throws BadRequestException if the provided stream ID was invalid
   */
public void create(StreamId newStreamId, @Nullable StreamProperties properties) throws IOException, BadRequestException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(newStreamId.getParent(), String.format("streams/%s", newStreamId.getStream()));
    HttpRequest.Builder builder = HttpRequest.put(url);
    if (properties != null) {
        builder = builder.withBody(GSON.toJson(properties));
    }
    HttpResponse response = restClient.execute(builder.build(), config.getAccessToken(), HttpURLConnection.HTTP_BAD_REQUEST);
    if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException("Bad request: " + response.getResponseBodyAsString());
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) BadRequestException(co.cask.cdap.common.BadRequestException) URL(java.net.URL)

Aggregations

BadRequestException (co.cask.cdap.common.BadRequestException)82 Path (javax.ws.rs.Path)29 NotFoundException (co.cask.cdap.common.NotFoundException)25 HttpResponse (co.cask.common.http.HttpResponse)17 JsonSyntaxException (com.google.gson.JsonSyntaxException)17 URL (java.net.URL)17 POST (javax.ws.rs.POST)17 NamespaceId (co.cask.cdap.proto.id.NamespaceId)16 IOException (java.io.IOException)15 ChannelBufferInputStream (org.jboss.netty.buffer.ChannelBufferInputStream)13 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)12 HttpRequest (co.cask.common.http.HttpRequest)12 InputStreamReader (java.io.InputStreamReader)11 Reader (java.io.Reader)11 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)9 ApplicationId (co.cask.cdap.proto.id.ApplicationId)8 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)7 ProgramType (co.cask.cdap.proto.ProgramType)7 ProgramId (co.cask.cdap.proto.id.ProgramId)6 Test (org.junit.Test)6