Search in sources :

Example 11 with ProgramNotFoundException

use of co.cask.cdap.common.ProgramNotFoundException 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);
    // See if the program is running as per the runtime service
    Map<RunId, RuntimeInfo> runtimeInfos = findRuntimeInfo(programId, runId);
    Map<ProgramRunId, RunRecordMeta> activeRunRecords = getActiveRuns(programId, runId);
    if (runtimeInfos.isEmpty() && activeRunRecords.isEmpty()) {
        // Error out if no run information from runtime service and from run record
        if (!store.applicationExists(programId.getParent())) {
            throw new ApplicationNotFoundException(programId.getParent());
        } else if (!store.programExists(programId)) {
            throw new ProgramNotFoundException(programId);
        }
        throw new BadRequestException(String.format("Program '%s' is not running.", programId));
    }
    // Stop the running program based on a combination of runtime info and run record
    // It's possible that some of them are not yet available from the runtimeService due to timing
    // differences between the run record was created vs being added to runtimeService
    // So we retry in a loop for up to 3 seconds max to cater for those cases
    Set<String> pendingStops = Stream.concat(runtimeInfos.keySet().stream().map(RunId::getId), activeRunRecords.keySet().stream().map(ProgramRunId::getRun)).collect(Collectors.toSet());
    List<ListenableFuture<ProgramController>> futures = new ArrayList<>();
    Stopwatch stopwatch = new Stopwatch().start();
    while (!pendingStops.isEmpty() && stopwatch.elapsedTime(TimeUnit.SECONDS) < 3L) {
        Iterator<String> iterator = pendingStops.iterator();
        while (iterator.hasNext()) {
            ProgramRunId activeRunId = programId.run(iterator.next());
            RunRecordMeta runRecord = activeRunRecords.get(activeRunId);
            if (runRecord == null) {
                runRecord = store.getRun(activeRunId);
            }
            // Check if the program is actually started from workflow and the workflow is running
            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.", activeRunId, workflowRunId));
            }
            RuntimeInfo runtimeInfo = runtimeService.lookup(programId, RunIds.fromString(activeRunId.getRun()));
            if (runtimeInfo != null) {
                futures.add(runtimeInfo.getController().stop());
                iterator.remove();
            }
        }
        if (!pendingStops.isEmpty()) {
            // If not able to stop all of them, meaning there are some runs that doesn't have a runtime info, due to
            // either the run was already finished or the run hasn't been registered with the runtime service.
            // We'll get the active runs again and filter it by the pending stops. Stop will be retried for those.
            Set<String> finalPendingStops = pendingStops;
            activeRunRecords = getActiveRuns(programId, runId).entrySet().stream().filter(e -> finalPendingStops.contains(e.getKey().getRun())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
            pendingStops = activeRunRecords.keySet().stream().map(ProgramRunId::getRun).collect(Collectors.toSet());
            if (!pendingStops.isEmpty()) {
                TimeUnit.MILLISECONDS.sleep(200);
            }
        }
    }
    return futures;
}
Also used : RuntimeInfo(co.cask.cdap.app.runtime.ProgramRuntimeService.RuntimeInfo) RunRecordMeta(co.cask.cdap.internal.app.store.RunRecordMeta) ArrayList(java.util.ArrayList) Stopwatch(com.google.common.base.Stopwatch) LogEntry(org.apache.twill.api.logging.LogEntry) 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) RunId(org.apache.twill.api.RunId) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException)

Example 12 with ProgramNotFoundException

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

the class ProgramClient method getStatus.

/**
 * Gets the status of a program
 * @param programId the program
 * @return the status of the program (e.g. STOPPED, STARTING, RUNNING)
 * @throws IOException if a network error occurred
 * @throws ProgramNotFoundException if the program with the specified name could not be found
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public String getStatus(ProgramId programId) throws IOException, ProgramNotFoundException, UnauthenticatedException, UnauthorizedException {
    String path = String.format("apps/%s/versions/%s/%s/%s/status", programId.getApplication(), programId.getVersion(), programId.getType().getCategoryName(), programId.getProgram());
    URL url = config.resolveNamespacedURLV3(programId.getNamespaceId(), path);
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (HttpURLConnection.HTTP_NOT_FOUND == response.getResponseCode()) {
        throw new ProgramNotFoundException(programId);
    }
    Map<String, String> responseObject = ObjectResponse.<Map<String, String>>fromJsonBody(response, MAP_STRING_STRING_TYPE, GSON).getResponseObject();
    return responseObject.get("status");
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) URL(java.net.URL)

Example 13 with ProgramNotFoundException

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

the class ProgramClient method start.

/**
 * Starts a program using specified runtime arguments.
 *
 * @param program the program to start
 * @param debug true to start in debug mode
 * @param runtimeArgs runtime arguments to pass to the program
 * @throws IOException
 * @throws ProgramNotFoundException
 * @throws UnauthenticatedException
 * @throws UnauthorizedException
 */
public void start(ProgramId program, boolean debug, @Nullable Map<String, String> runtimeArgs) throws IOException, ProgramNotFoundException, UnauthenticatedException, UnauthorizedException {
    String action = debug ? "debug" : "start";
    String path = String.format("apps/%s/versions/%s/%s/%s/%s", program.getApplication(), program.getVersion(), program.getType().getCategoryName(), program.getProgram(), action);
    URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), path);
    HttpRequest.Builder request = HttpRequest.post(url);
    if (runtimeArgs != null) {
        request.withBody(GSON.toJson(runtimeArgs));
    }
    HttpResponse response = restClient.execute(request.build(), config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ProgramNotFoundException(program);
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) URL(java.net.URL)

Example 14 with ProgramNotFoundException

use of co.cask.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(co.cask.common.http.HttpResponse) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) URL(java.net.URL)

Example 15 with ProgramNotFoundException

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

the class WorkflowHttpHandler method getWorkflowSpecForValidRun.

/**
 * Get the {@link WorkflowSpecification} if valid application id, workflow id, and runid are provided.
 * @param namespaceId the namespace id
 * @param applicationId the application id
 * @param workflowId the workflow id
 * @param runId the runid of the workflow
 * @return the specifications for the Workflow
 * @throws NotFoundException is thrown when the application, workflow, or runid is not found
 */
private WorkflowSpecification getWorkflowSpecForValidRun(String namespaceId, String applicationId, String workflowId, String runId) throws NotFoundException {
    ApplicationId appId = new ApplicationId(namespaceId, applicationId);
    ApplicationSpecification appSpec = store.getApplication(appId);
    if (appSpec == null) {
        throw new ApplicationNotFoundException(appId);
    }
    WorkflowSpecification workflowSpec = appSpec.getWorkflows().get(workflowId);
    ProgramId programId = new ProgramId(namespaceId, applicationId, ProgramType.WORKFLOW, workflowId);
    if (workflowSpec == null) {
        throw new ProgramNotFoundException(programId);
    }
    if (store.getRun(programId.run(runId)) == null) {
        throw new NotFoundException(new ProgramRunId(programId.getNamespace(), programId.getApplication(), programId.getType(), programId.getProgram(), runId));
    }
    return workflowSpec;
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) WorkflowSpecification(co.cask.cdap.api.workflow.WorkflowSpecification) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) InstanceNotFoundException(co.cask.cdap.api.dataset.InstanceNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ProgramId(co.cask.cdap.proto.id.ProgramId) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException)

Aggregations

ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)17 HttpResponse (co.cask.common.http.HttpResponse)10 URL (java.net.URL)10 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)6 ApplicationId (co.cask.cdap.proto.id.ApplicationId)5 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)4 ProgramId (co.cask.cdap.proto.id.ProgramId)3 ProgramRunId (co.cask.cdap.proto.id.ProgramRunId)3 InstanceNotFoundException (co.cask.cdap.api.dataset.InstanceNotFoundException)2 WorkflowSpecification (co.cask.cdap.api.workflow.WorkflowSpecification)2 NotFoundException (co.cask.cdap.common.NotFoundException)2 HttpRequest (co.cask.common.http.HttpRequest)2 TypeToken (com.google.common.reflect.TypeToken)2 ProgramController (co.cask.cdap.app.runtime.ProgramController)1 ProgramRuntimeService (co.cask.cdap.app.runtime.ProgramRuntimeService)1 RuntimeInfo (co.cask.cdap.app.runtime.ProgramRuntimeService.RuntimeInfo)1 BadRequestException (co.cask.cdap.common.BadRequestException)1 AbstractListener (co.cask.cdap.internal.app.runtime.AbstractListener)1 RunRecordMeta (co.cask.cdap.internal.app.store.RunRecordMeta)1 ApplicationRecord (co.cask.cdap.proto.ApplicationRecord)1