Search in sources :

Example 11 with ProgramNotFoundException

use of io.cdap.cdap.common.ProgramNotFoundException in project cdap by caskdata.

the class ProgramClient method stopAll.

/**
 * Stops all currently running programs.
 */
public void stopAll(NamespaceId namespace) throws IOException, UnauthenticatedException, InterruptedException, TimeoutException, UnauthorizedException, ApplicationNotFoundException, BadRequestException {
    List<ApplicationRecord> allApps = applicationClient.list(namespace);
    for (ApplicationRecord applicationRecord : allApps) {
        ApplicationId appId = new ApplicationId(namespace.getNamespace(), applicationRecord.getName(), applicationRecord.getAppVersion());
        List<ProgramRecord> programRecords = applicationClient.listPrograms(appId);
        for (ProgramRecord programRecord : programRecords) {
            try {
                ProgramId program = appId.program(programRecord.getType(), programRecord.getName());
                String status = this.getStatus(program);
                if (!status.equals("STOPPED")) {
                    try {
                        this.stop(program);
                    } catch (IOException ioe) {
                        // ProgramClient#stop calls RestClient, which throws an IOException if the HTTP response code is 400,
                        // which can be due to the program already being stopped when calling stop on it.
                        // Most likely, there was a race condition that the program stopped between the time we checked its
                        // status and calling the stop method.
                        LOG.warn("Program {} is already stopped, proceeding even though the following exception is raised.", program, ioe);
                    }
                    this.waitForStatus(program, ProgramStatus.STOPPED, 60, TimeUnit.SECONDS);
                }
            } catch (ProgramNotFoundException e) {
            // IGNORE
            }
        }
    }
}
Also used : ProgramRecord(io.cdap.cdap.proto.ProgramRecord) IOException(java.io.IOException) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) ProgramNotFoundException(io.cdap.cdap.common.ProgramNotFoundException) ApplicationRecord(io.cdap.cdap.proto.ApplicationRecord)

Example 12 with ProgramNotFoundException

use of io.cdap.cdap.common.ProgramNotFoundException in project cdap by caskdata.

the class ProgramClient method getProgramLogs.

/**
 * Gets the logs of a program.
 *
 * @param program the program
 * @param start start time of the time range of desired logs
 * @param stop end time of the time range of desired logs
 * @return the logs of the program
 * @throws IOException if a network error occurred
 * @throws NotFoundException if the application or program could not be found
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public String getProgramLogs(ProgramId program, long start, long stop) throws IOException, NotFoundException, UnauthenticatedException, UnauthorizedException {
    String path = String.format("apps/%s/%s/%s/logs?start=%d&stop=%d&escape=false", program.getApplication(), program.getType().getCategoryName(), program.getProgram(), start, stop);
    URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), path);
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken());
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ProgramNotFoundException(program);
    }
    return new String(response.getResponseBody(), Charsets.UTF_8);
}
Also used : HttpResponse(io.cdap.common.http.HttpResponse) ProgramNotFoundException(io.cdap.cdap.common.ProgramNotFoundException) URL(java.net.URL)

Example 13 with ProgramNotFoundException

use of io.cdap.cdap.common.ProgramNotFoundException in project cdap by caskdata.

the class ProgramClient method stop.

/**
 * Stops a program.
 *
 * @param programId the program to stop
 * @throws IOException if a network error occurred
 * @throws ProgramNotFoundException if the program with specified name could not be found
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws BadRequestException if an attempt is made to stop a program that is either not running or
 *                             was started by a workflow
 */
public void stop(ProgramId programId) throws IOException, ProgramNotFoundException, UnauthenticatedException, UnauthorizedException, BadRequestException {
    String path = String.format("apps/%s/versions/%s/%s/%s/stop", programId.getApplication(), programId.getVersion(), programId.getType().getCategoryName(), programId.getProgram());
    URL url = config.resolveNamespacedURLV3(programId.getNamespaceId(), path);
    // Required to add body even if runtimeArgs is null to avoid 411 error for Http POST
    HttpRequest.Builder request = HttpRequest.post(url).withBody("");
    HttpResponse response = restClient.execute(request.build(), config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ProgramNotFoundException(programId);
    }
    if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(response.getResponseCode() + ": " + response.getResponseBodyAsString());
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) BadRequestException(io.cdap.cdap.common.BadRequestException) ProgramNotFoundException(io.cdap.cdap.common.ProgramNotFoundException) URL(java.net.URL)

Example 14 with ProgramNotFoundException

use of io.cdap.cdap.common.ProgramNotFoundException in project cdap by caskdata.

the class ProgramClient method getRuntimeArgs.

/**
 * Gets the runtime args of a program.
 *
 * @param program the program
 * @return runtime args of the program
 * @throws IOException if a network error occurred
 * @throws ProgramNotFoundException if the application or program could not be found
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public Map<String, String> getRuntimeArgs(ProgramId program) throws IOException, UnauthenticatedException, ProgramNotFoundException, UnauthorizedException {
    String path = String.format("apps/%s/versions/%s/%s/%s/runtimeargs", program.getApplication(), program.getVersion(), program.getType().getCategoryName(), program.getProgram());
    URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), path);
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ProgramNotFoundException(program);
    }
    return ObjectResponse.fromJsonBody(response, new TypeToken<Map<String, String>>() {
    }).getResponseObject();
}
Also used : TypeToken(com.google.common.reflect.TypeToken) HttpResponse(io.cdap.common.http.HttpResponse) ProgramNotFoundException(io.cdap.cdap.common.ProgramNotFoundException) URL(java.net.URL)

Example 15 with ProgramNotFoundException

use of io.cdap.cdap.common.ProgramNotFoundException in project cdap by caskdata.

the class PreferencesClient method getProgramPreferences.

/**
 * Returns the Preferences stored at the Program Level.
 *
 * @param program Program Id
 * @param resolved Set to True if collapsed/resolved properties are desired
 * @return map of key-value pairs
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws ProgramNotFoundException if the requested program is not found
 */
public Map<String, String> getProgramPreferences(ProgramId program, boolean resolved) throws IOException, UnauthenticatedException, ProgramNotFoundException, UnauthorizedException {
    String res = Boolean.toString(resolved);
    URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), String.format("/apps/%s/%s/%s/preferences?resolved=%s", program.getApplication(), program.getType().getCategoryName(), program.getProgram(), res));
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ProgramNotFoundException(program);
    }
    return ObjectResponse.fromJsonBody(response, new TypeToken<Map<String, String>>() {
    }).getResponseObject();
}
Also used : TypeToken(com.google.common.reflect.TypeToken) HttpResponse(io.cdap.common.http.HttpResponse) ProgramNotFoundException(io.cdap.cdap.common.ProgramNotFoundException) URL(java.net.URL)

Aggregations

ProgramNotFoundException (io.cdap.cdap.common.ProgramNotFoundException)17 HttpResponse (io.cdap.common.http.HttpResponse)11 URL (java.net.URL)10 NotFoundException (io.cdap.cdap.common.NotFoundException)5 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)5 ProgramId (io.cdap.cdap.proto.id.ProgramId)5 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)4 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)4 HttpRequest (io.cdap.common.http.HttpRequest)4 WorkflowSpecification (io.cdap.cdap.api.workflow.WorkflowSpecification)3 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)3 List (java.util.List)3 TypeToken (com.google.common.reflect.TypeToken)2 Inject (com.google.inject.Inject)2 InstanceNotFoundException (io.cdap.cdap.api.dataset.InstanceNotFoundException)2 ScheduleNotFoundException (io.cdap.cdap.internal.app.runtime.schedule.ScheduleNotFoundException)2 ProgramType (io.cdap.cdap.proto.ProgramType)2 WorkflowNodeStateDetail (io.cdap.cdap.proto.WorkflowNodeStateDetail)2 IOException (java.io.IOException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1