Search in sources :

Example 6 with AbstractListener

use of co.cask.cdap.internal.app.runtime.AbstractListener 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.start(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)

Example 7 with AbstractListener

use of co.cask.cdap.internal.app.runtime.AbstractListener in project cdap by caskdata.

the class DefaultPreviewRunner method startPreview.

@Override
public void startPreview(PreviewRequest<?> previewRequest) throws Exception {
    namespaceAdmin.create(new NamespaceMeta.Builder().setName(previewRequest.getProgram().getNamespaceId()).build());
    programId = previewRequest.getProgram();
    AppRequest<?> request = previewRequest.getAppRequest();
    ArtifactSummary artifactSummary = request.getArtifact();
    ApplicationId preview = programId.getParent();
    DataTracerFactoryProvider.setDataTracerFactory(preview, dataTracerFactory);
    String config = request.getConfig() == null ? null : GSON.toJson(request.getConfig());
    try {
        applicationLifecycleService.deployApp(preview.getParent(), preview.getApplication(), preview.getVersion(), artifactSummary, config, NOOP_PROGRAM_TERMINATOR, null, request.canUpdateSchedules());
    } catch (Exception e) {
        this.status = new PreviewStatus(PreviewStatus.Status.DEPLOY_FAILED, new BasicThrowable(e), null, null);
        throw e;
    }
    final PreviewConfig previewConfig = previewRequest.getAppRequest().getPreview();
    ProgramController controller = programLifecycleService.start(programId, previewConfig == null ? Collections.<String, String>emptyMap() : previewConfig.getRuntimeArgs(), false);
    controller.addListener(new AbstractListener() {

        @Override
        public void init(ProgramController.State currentState, @Nullable Throwable cause) {
            setStatus(new PreviewStatus(PreviewStatus.Status.RUNNING, null, System.currentTimeMillis(), null));
            // Only have timer if there is a timeout setting.
            if (previewConfig.getTimeout() != null) {
                timer = new Timer();
                final int timeOutMinutes = previewConfig.getTimeout();
                timer.schedule(new TimerTask() {

                    @Override
                    public void run() {
                        try {
                            LOG.info("Stopping the preview since it has reached running time: {} mins.", timeOutMinutes);
                            stopPreview();
                            killedByTimer = true;
                        } catch (Exception e) {
                            LOG.debug("Error shutting down the preview run with id: {}", programId);
                        }
                    }
                }, timeOutMinutes * 60 * 1000);
            }
        }

        @Override
        public void completed() {
            setStatus(new PreviewStatus(PreviewStatus.Status.COMPLETED, null, status.getStartTime(), System.currentTimeMillis()));
            shutDownUnrequiredServices();
        }

        @Override
        public void killed() {
            if (!killedByTimer) {
                setStatus(new PreviewStatus(PreviewStatus.Status.KILLED, null, status.getStartTime(), System.currentTimeMillis()));
            } else {
                setStatus(new PreviewStatus(PreviewStatus.Status.KILLED_BY_TIMER, null, status.getStartTime(), System.currentTimeMillis()));
            }
            shutDownUnrequiredServices();
        }

        @Override
        public void error(Throwable cause) {
            setStatus(new PreviewStatus(PreviewStatus.Status.RUN_FAILED, new BasicThrowable(cause), status.getStartTime(), System.currentTimeMillis()));
            shutDownUnrequiredServices();
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    runId = controller.getProgramRunId();
}
Also used : ProgramController(co.cask.cdap.app.runtime.ProgramController) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) Timer(java.util.Timer) TimerTask(java.util.TimerTask) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) PreviewStatus(co.cask.cdap.app.preview.PreviewStatus) BasicThrowable(co.cask.cdap.proto.BasicThrowable) AbstractListener(co.cask.cdap.internal.app.runtime.AbstractListener) ApplicationId(co.cask.cdap.proto.id.ApplicationId) BasicThrowable(co.cask.cdap.proto.BasicThrowable) PreviewConfig(co.cask.cdap.proto.artifact.preview.PreviewConfig)

Example 8 with AbstractListener

use of co.cask.cdap.internal.app.runtime.AbstractListener in project cdap by caskdata.

the class DefaultProgramWorkflowRunner method blockForCompletion.

/**
   * Adds a listener to the {@link ProgramController} and blocks for completion.
   *
   * @param closeable a {@link Closeable} to call when the program execution completed
   * @param controller the {@link ProgramController} for the program
   * @throws Exception if the execution failed
   */
private void blockForCompletion(final Closeable closeable, final ProgramController controller) throws Exception {
    // Execute the program.
    final SettableFuture<Void> completion = SettableFuture.create();
    controller.addListener(new AbstractListener() {

        @Override
        public void init(ProgramController.State currentState, @Nullable Throwable cause) {
            switch(currentState) {
                case COMPLETED:
                    completed();
                    break;
                case KILLED:
                    killed();
                    break;
                case ERROR:
                    error(cause);
                    break;
            }
        }

        @Override
        public void completed() {
            Closeables.closeQuietly(closeable);
            nodeStates.put(nodeId, new WorkflowNodeState(nodeId, NodeStatus.COMPLETED, controller.getRunId().getId(), null));
            completion.set(null);
        }

        @Override
        public void killed() {
            Closeables.closeQuietly(closeable);
            nodeStates.put(nodeId, new WorkflowNodeState(nodeId, NodeStatus.KILLED, controller.getRunId().getId(), null));
            completion.set(null);
        }

        @Override
        public void error(Throwable cause) {
            Closeables.closeQuietly(closeable);
            nodeStates.put(nodeId, new WorkflowNodeState(nodeId, NodeStatus.FAILED, controller.getRunId().getId(), cause));
            completion.setException(cause);
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    // Block for completion.
    try {
        completion.get();
    } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof Exception) {
            throw (Exception) cause;
        }
        throw Throwables.propagate(cause);
    } catch (InterruptedException e) {
        try {
            Futures.getUnchecked(controller.stop());
        } catch (Throwable t) {
        // no-op
        }
        // reset the interrupt
        Thread.currentThread().interrupt();
    }
}
Also used : ProgramController(co.cask.cdap.app.runtime.ProgramController) WorkflowNodeState(co.cask.cdap.api.workflow.WorkflowNodeState) AbstractListener(co.cask.cdap.internal.app.runtime.AbstractListener) ExecutionException(java.util.concurrent.ExecutionException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with AbstractListener

use of co.cask.cdap.internal.app.runtime.AbstractListener in project cdap by caskdata.

the class WorkerProgramRunnerTest method startProgram.

private ProgramController startProgram(ApplicationWithPrograms app, Class<?> programClass) throws Throwable {
    final AtomicReference<Throwable> errorCause = new AtomicReference<>();
    final ProgramController controller = AppFabricTestHelper.submit(app, programClass.getName(), new BasicArguments(), TEMP_FOLDER_SUPPLIER);
    runningPrograms.add(controller);
    controller.addListener(new AbstractListener() {

        @Override
        public void error(Throwable cause) {
            errorCause.set(cause);
        }

        @Override
        public void killed() {
            errorCause.set(new RuntimeException("Killed"));
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    Tasks.waitFor(ProgramController.State.ALIVE, new Callable<ProgramController.State>() {

        @Override
        public ProgramController.State call() throws Exception {
            Throwable t = errorCause.get();
            if (t != null) {
                Throwables.propagateIfInstanceOf(t, Exception.class);
                throw Throwables.propagate(t);
            }
            return controller.getState();
        }
    }, 30, TimeUnit.SECONDS);
    return controller;
}
Also used : ProgramController(co.cask.cdap.app.runtime.ProgramController) AtomicReference(java.util.concurrent.atomic.AtomicReference) AbstractListener(co.cask.cdap.internal.app.runtime.AbstractListener) BasicArguments(co.cask.cdap.internal.app.runtime.BasicArguments) IOException(java.io.IOException)

Example 10 with AbstractListener

use of co.cask.cdap.internal.app.runtime.AbstractListener in project cdap by caskdata.

the class AbstractProgramRuntimeService method monitorProgram.

/**
   * Starts monitoring a running program.
   *
   * @param runtimeInfo information about the running program
   * @param cleanUpTask task to run when program finished
   */
private void monitorProgram(final RuntimeInfo runtimeInfo, final Runnable cleanUpTask) {
    final ProgramController controller = runtimeInfo.getController();
    controller.addListener(new AbstractListener() {

        @Override
        public void init(ProgramController.State currentState, @Nullable Throwable cause) {
            if (!COMPLETED_STATES.contains(currentState)) {
                add(runtimeInfo);
            } else {
                cleanUpTask.run();
            }
        }

        @Override
        public void completed() {
            remove(runtimeInfo, cleanUpTask);
        }

        @Override
        public void killed() {
            remove(runtimeInfo, cleanUpTask);
        }

        @Override
        public void error(Throwable cause) {
            remove(runtimeInfo, cleanUpTask);
        }
    }, Threads.SAME_THREAD_EXECUTOR);
}
Also used : AbstractListener(co.cask.cdap.internal.app.runtime.AbstractListener)

Aggregations

AbstractListener (co.cask.cdap.internal.app.runtime.AbstractListener)13 ProgramController (co.cask.cdap.app.runtime.ProgramController)9 BasicArguments (co.cask.cdap.internal.app.runtime.BasicArguments)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ProgramDescriptor (co.cask.cdap.app.program.ProgramDescriptor)3 BasicThrowable (co.cask.cdap.proto.BasicThrowable)3 Test (org.junit.Test)3 ProgramRuntimeService (co.cask.cdap.app.runtime.ProgramRuntimeService)2 ApplicationWithPrograms (co.cask.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms)2 ApplicationId (co.cask.cdap.proto.id.ApplicationId)2 ProgramId (co.cask.cdap.proto.id.ProgramId)2 Predicate (com.google.common.base.Predicate)2 Supplier (com.google.common.base.Supplier)2 Injector (com.google.inject.Injector)2 Closeable (java.io.Closeable)2 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)1 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)1