Search in sources :

Example 6 with ProgramNotFoundException

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

Example 7 with ProgramNotFoundException

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

Example 8 with ProgramNotFoundException

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

the class PreferencesClient method deleteProgramPreferences.

/**
 * Deletes Preferences at the Program Level.
 *
 * @param program Program Id
 * @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 void deleteProgramPreferences(ProgramId program) throws IOException, UnauthenticatedException, ProgramNotFoundException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), String.format("/apps/%s/%s/%s/preferences", program.getApplication(), program.getType().getCategoryName(), program.getProgram()));
    HttpResponse response = restClient.execute(HttpMethod.DELETE, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ProgramNotFoundException(program);
    }
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) URL(java.net.URL)

Example 9 with ProgramNotFoundException

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

the class PreferencesClient method setProgramPreferences.

/**
 * Sets Preferences at the Program Level.
 *
 * @param program Program Id
 * @param preferences 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 void setProgramPreferences(ProgramId program, Map<String, String> preferences) throws IOException, UnauthenticatedException, ProgramNotFoundException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), String.format("/apps/%s/%s/%s/preferences", program.getApplication(), program.getType().getCategoryName(), program.getProgram()));
    HttpResponse response = restClient.execute(HttpMethod.PUT, url, GSON.toJson(preferences), null, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ProgramNotFoundException(program);
    }
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) URL(java.net.URL)

Example 10 with ProgramNotFoundException

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

the class ScheduleTaskRunner method execute.

/**
 * Executes a program without blocking until its completion.
 *
 * @return a {@link ListenableFuture} object that completes when the program completes
 */
public ListenableFuture<?> execute(final ProgramId id, Map<String, String> sysArgs, Map<String, String> userArgs) throws Exception {
    ProgramRuntimeService.RuntimeInfo runtimeInfo;
    String originalUserId = SecurityRequestContext.getUserId();
    try {
        // if the program has a namespace user configured then set that user in the security request context.
        // See: CDAP-7396
        String nsPrincipal = namespaceQueryAdmin.get(id.getNamespaceId()).getConfig().getPrincipal();
        if (nsPrincipal != null && SecurityUtil.isKerberosEnabled(cConf)) {
            SecurityRequestContext.setUserId(new KerberosName(nsPrincipal).getServiceName());
        }
        runtimeInfo = lifecycleService.startInternal(id, sysArgs, userArgs, false);
    } catch (ProgramNotFoundException | ApplicationNotFoundException e) {
        throw new TaskExecutionException(String.format(UserMessages.getMessage(UserErrors.PROGRAM_NOT_FOUND), id), e, false);
    } finally {
        SecurityRequestContext.setUserId(originalUserId);
    }
    final ProgramController controller = runtimeInfo.getController();
    final CountDownLatch latch = new CountDownLatch(1);
    controller.addListener(new AbstractListener() {

        @Override
        public void init(ProgramController.State state, @Nullable Throwable cause) {
            if (state == ProgramController.State.COMPLETED) {
                completed();
            }
            if (state == ProgramController.State.ERROR) {
                error(controller.getFailureCause());
            }
        }

        @Override
        public void killed() {
            latch.countDown();
        }

        @Override
        public void completed() {
            latch.countDown();
        }

        @Override
        public void error(Throwable cause) {
            latch.countDown();
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    return executorService.submit(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            latch.await();
            return null;
        }
    });
}
Also used : ProgramController(co.cask.cdap.app.runtime.ProgramController) KerberosName(org.apache.hadoop.security.authentication.util.KerberosName) CountDownLatch(java.util.concurrent.CountDownLatch) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) AbstractListener(co.cask.cdap.internal.app.runtime.AbstractListener) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) ProgramRuntimeService(co.cask.cdap.app.runtime.ProgramRuntimeService)

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