Search in sources :

Example 6 with AbstractListener

use of io.cdap.cdap.internal.app.runtime.AbstractListener in project cdap by cdapio.

the class AbstractProgramRuntimeService method updateRuntimeInfo.

/**
 * Updates the runtime info cache by adding the given {@link RuntimeInfo} if it does not exist.
 *
 * @param info information about the running program
 */
@VisibleForTesting
void updateRuntimeInfo(RuntimeInfo info) {
    // Add the runtime info if it does not exist in the cache.
    Lock lock = runtimeInfosLock.writeLock();
    lock.lock();
    try {
        if (runtimeInfos.contains(info.getType(), info.getController().getRunId())) {
            LOG.debug("RuntimeInfo already exists: {}", info.getController().getProgramRunId());
            cleanupRuntimeInfo(info);
            return;
        }
        runtimeInfos.put(info.getType(), info.getController().getRunId(), info);
    } finally {
        lock.unlock();
    }
    LOG.debug("Added RuntimeInfo: {}", info.getController().getProgramRunId());
    ProgramController controller = info.getController();
    controller.addListener(new AbstractListener() {

        @Override
        public void init(ProgramController.State currentState, @Nullable Throwable cause) {
            if (COMPLETED_STATES.contains(currentState)) {
                remove(info);
            }
        }

        @Override
        public void completed() {
            remove(info);
        }

        @Override
        public void killed() {
            remove(info);
        }

        @Override
        public void error(Throwable cause) {
            remove(info);
        }
    }, Threads.SAME_THREAD_EXECUTOR);
}
Also used : AbstractListener(io.cdap.cdap.internal.app.runtime.AbstractListener) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 7 with AbstractListener

use of io.cdap.cdap.internal.app.runtime.AbstractListener in project cdap by cdapio.

the class DefaultPreviewRunner method startPreview.

@Override
public Future<PreviewRequest> startPreview(PreviewRequest previewRequest) throws Exception {
    ProgramId programId = previewRequest.getProgram();
    long submitTimeMillis = RunIds.getTime(programId.getApplication(), TimeUnit.MILLISECONDS);
    previewStarted(programId);
    AppRequest<?> request = previewRequest.getAppRequest();
    if (request == null) {
        // This shouldn't happen
        throw new IllegalStateException("Preview request shouldn't have an empty application request");
    }
    ArtifactSummary artifactSummary = request.getArtifact();
    ApplicationId preview = programId.getParent();
    try {
        namespaceAdmin.create(new NamespaceMeta.Builder().setName(programId.getNamespaceId()).build());
    } catch (NamespaceAlreadyExistsException e) {
        LOG.debug("Namespace {} already exists.", programId.getNamespaceId());
    }
    DataTracerFactoryProvider.setDataTracerFactory(preview, dataTracerFactory);
    String config = request.getConfig() == null ? null : GSON.toJson(request.getConfig());
    PreviewConfig previewConfig = previewRequest.getAppRequest().getPreview();
    PreferencesDetail preferences = preferencesFetcher.get(programId, true);
    Map<String, String> userProps = new HashMap<>(preferences.getProperties());
    if (previewConfig != null) {
        userProps.putAll(previewConfig.getRuntimeArgs());
    }
    try {
        LOG.debug("Deploying preview application for {}", programId);
        applicationLifecycleService.deployApp(preview.getParent(), preview.getApplication(), preview.getVersion(), artifactSummary, config, NOOP_PROGRAM_TERMINATOR, null, request.canUpdateSchedules(), true, userProps);
    } catch (Exception e) {
        PreviewStatus previewStatus = new PreviewStatus(PreviewStatus.Status.DEPLOY_FAILED, submitTimeMillis, new BasicThrowable(e), null, null);
        previewTerminated(programId, previewStatus);
        throw e;
    }
    LOG.debug("Starting preview for {}", programId);
    ProgramController controller = programLifecycleService.start(programId, userProps, false, true);
    long startTimeMillis = System.currentTimeMillis();
    AtomicBoolean timeout = new AtomicBoolean();
    CompletableFuture<PreviewRequest> resultFuture = new CompletableFuture<>();
    controller.addListener(new AbstractListener() {

        @Override
        public void init(ProgramController.State currentState, @Nullable Throwable cause) {
            switch(currentState) {
                case STARTING:
                case ALIVE:
                case STOPPING:
                    setStatus(programId, new PreviewStatus(PreviewStatus.Status.RUNNING, submitTimeMillis, null, startTimeMillis, null));
                    break;
                case COMPLETED:
                    terminated(PreviewStatus.Status.COMPLETED, null);
                    break;
                case KILLED:
                    terminated(PreviewStatus.Status.KILLED, null);
                    break;
                case ERROR:
                    terminated(PreviewStatus.Status.RUN_FAILED, cause);
                    break;
            }
        }

        @Override
        public void completed() {
            terminated(PreviewStatus.Status.COMPLETED, null);
        }

        @Override
        public void killed() {
            terminated(timeout.get() ? PreviewStatus.Status.KILLED_BY_TIMER : PreviewStatus.Status.KILLED, null);
        }

        @Override
        public void error(Throwable cause) {
            terminated(PreviewStatus.Status.RUN_FAILED, cause);
        }

        /**
         * Handle termination of program run.
         *
         * @param status the termination status
         * @param failureCause if the program was terminated due to error, this carries the failure cause
         */
        private void terminated(PreviewStatus.Status status, @Nullable Throwable failureCause) {
            PreviewStatus previewStatus = new PreviewStatus(status, submitTimeMillis, failureCause == null ? null : new BasicThrowable(failureCause), startTimeMillis, System.currentTimeMillis());
            previewTerminated(programId, previewStatus);
            if (failureCause == null) {
                resultFuture.complete(previewRequest);
            } else {
                resultFuture.completeExceptionally(failureCause);
            }
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    trackPreviewTimeout(previewRequest, timeout, resultFuture);
    PreviewMessage message = new PreviewMessage(PreviewMessage.Type.PROGRAM_RUN_ID, programId.getParent(), GSON.toJsonTree(controller.getProgramRunId()));
    previewDataPublisher.publish(programId.getParent(), message);
    return resultFuture;
}
Also used : HashMap(java.util.HashMap) PreferencesDetail(io.cdap.cdap.proto.PreferencesDetail) CompletableFuture(java.util.concurrent.CompletableFuture) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) AbstractListener(io.cdap.cdap.internal.app.runtime.AbstractListener) NamespaceAlreadyExistsException(io.cdap.cdap.common.NamespaceAlreadyExistsException) ProgramController(io.cdap.cdap.app.runtime.ProgramController) PreviewMessage(io.cdap.cdap.app.preview.PreviewMessage) ProgramId(io.cdap.cdap.proto.id.ProgramId) TimeoutException(java.util.concurrent.TimeoutException) NamespaceAlreadyExistsException(io.cdap.cdap.common.NamespaceAlreadyExistsException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) PreviewStatus(io.cdap.cdap.app.preview.PreviewStatus) BasicThrowable(io.cdap.cdap.proto.BasicThrowable) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) BasicThrowable(io.cdap.cdap.proto.BasicThrowable) PreviewRequest(io.cdap.cdap.app.preview.PreviewRequest) PreviewConfig(io.cdap.cdap.proto.artifact.preview.PreviewConfig)

Example 8 with AbstractListener

use of io.cdap.cdap.internal.app.runtime.AbstractListener in project cdap by cdapio.

the class MapReduceRunnerTestBase method waitForCompletion.

private boolean waitForCompletion(ProgramController controller) throws InterruptedException {
    final AtomicBoolean success = new AtomicBoolean(false);
    final CountDownLatch completion = new CountDownLatch(1);
    controller.addListener(new AbstractListener() {

        @Override
        public void completed() {
            success.set(true);
            completion.countDown();
        }

        @Override
        public void error(Throwable cause) {
            completion.countDown();
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    // MR tests can run for long time.
    completion.await(10, TimeUnit.MINUTES);
    return success.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AbstractListener(io.cdap.cdap.internal.app.runtime.AbstractListener) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 9 with AbstractListener

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

the class MapReduceRunnerTestBase method waitForCompletion.

private boolean waitForCompletion(ProgramController controller) throws InterruptedException {
    final AtomicBoolean success = new AtomicBoolean(false);
    final CountDownLatch completion = new CountDownLatch(1);
    controller.addListener(new AbstractListener() {

        @Override
        public void completed() {
            success.set(true);
            completion.countDown();
        }

        @Override
        public void error(Throwable cause) {
            completion.countDown();
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    // MR tests can run for long time.
    completion.await(10, TimeUnit.MINUTES);
    return success.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AbstractListener(io.cdap.cdap.internal.app.runtime.AbstractListener) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 10 with AbstractListener

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

the class WorkflowTest method testOneActionWorkflow.

@Test(timeout = 120 * 1000L)
public void testOneActionWorkflow() throws Exception {
    final ApplicationWithPrograms app = AppFabricTestHelper.deployApplicationWithManager(OneActionWorkflowApp.class, TEMP_FOLDER_SUPPLIER);
    final Injector injector = AppFabricTestHelper.getInjector();
    final ProgramDescriptor programDescriptor = Iterators.filter(app.getPrograms().iterator(), input -> input.getProgramId().getType() == ProgramType.WORKFLOW).next();
    final SettableFuture<String> completion = SettableFuture.create();
    final ProgramController controller = AppFabricTestHelper.submit(app, programDescriptor.getSpecification().getClassName(), new BasicArguments(), TEMP_FOLDER_SUPPLIER);
    controller.addListener(new AbstractListener() {

        @Override
        public void init(ProgramController.State currentState, @Nullable Throwable cause) {
            LOG.info("Initializing");
            ArtifactId artifactId = controller.getProgramRunId().getNamespaceId().artifact("test", "1.0").toApiArtifactId();
            setStartAndRunning(injector.getInstance(Store.class), controller.getProgramRunId().getParent(), controller.getProgramRunId().getRun(), artifactId);
        }

        @Override
        public void completed() {
            LOG.info("Completed");
            completion.set("Completed");
        }

        @Override
        public void error(Throwable cause) {
            LOG.info("Error", cause);
            completion.setException(cause);
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    String run = completion.get();
    Assert.assertEquals("Completed", run);
}
Also used : NonUniqueProgramsInWorkflowApp(io.cdap.cdap.NonUniqueProgramsInWorkflowApp) Supplier(com.google.common.base.Supplier) XSlowTests(io.cdap.cdap.test.XSlowTests) LoggerFactory(org.slf4j.LoggerFactory) ApplicationWithPrograms(io.cdap.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms) SettableFuture(com.google.common.util.concurrent.SettableFuture) ProgramType(io.cdap.cdap.proto.ProgramType) Iterators(com.google.common.collect.Iterators) WorkflowSchedulesWithSameNameApp(io.cdap.cdap.WorkflowSchedulesWithSameNameApp) ScheduleAppWithMissingWorkflow(io.cdap.cdap.ScheduleAppWithMissingWorkflow) AppFabricTestHelper(io.cdap.cdap.internal.AppFabricTestHelper) After(org.junit.After) SystemArguments(io.cdap.cdap.internal.app.runtime.SystemArguments) ClassRule(org.junit.ClassRule) Nullable(javax.annotation.Nullable) WorkflowApp(io.cdap.cdap.WorkflowApp) Threads(org.apache.twill.common.Threads) OneActionWorkflowApp(io.cdap.cdap.OneActionWorkflowApp) ProgramController(io.cdap.cdap.app.runtime.ProgramController) Logger(org.slf4j.Logger) MissingSparkWorkflowApp(io.cdap.cdap.MissingSparkWorkflowApp) ImmutableMap(com.google.common.collect.ImmutableMap) RunIds(io.cdap.cdap.common.app.RunIds) BufferedWriter(java.io.BufferedWriter) ProgramId(io.cdap.cdap.proto.id.ProgramId) FileWriter(java.io.FileWriter) Throwables(com.google.common.base.Throwables) ProgramDescriptor(io.cdap.cdap.app.program.ProgramDescriptor) MissingMapReduceWorkflowApp(io.cdap.cdap.MissingMapReduceWorkflowApp) Test(org.junit.Test) IOException(java.io.IOException) Category(org.junit.experimental.categories.Category) File(java.io.File) NonUniqueProgramsInWorkflowWithForkApp(io.cdap.cdap.NonUniqueProgramsInWorkflowWithForkApp) Injector(com.google.inject.Injector) Store(io.cdap.cdap.app.store.Store) AbstractListener(io.cdap.cdap.internal.app.runtime.AbstractListener) TimeUnit(java.util.concurrent.TimeUnit) AppWithAnonymousWorkflow(io.cdap.cdap.AppWithAnonymousWorkflow) ProfileId(io.cdap.cdap.proto.id.ProfileId) Assert(org.junit.Assert) Collections(java.util.Collections) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) TemporaryFolder(org.junit.rules.TemporaryFolder) BasicArguments(io.cdap.cdap.internal.app.runtime.BasicArguments) ProgramController(io.cdap.cdap.app.runtime.ProgramController) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) ApplicationWithPrograms(io.cdap.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms) Injector(com.google.inject.Injector) AbstractListener(io.cdap.cdap.internal.app.runtime.AbstractListener) ProgramDescriptor(io.cdap.cdap.app.program.ProgramDescriptor) BasicArguments(io.cdap.cdap.internal.app.runtime.BasicArguments) Test(org.junit.Test)

Aggregations

AbstractListener (io.cdap.cdap.internal.app.runtime.AbstractListener)26 ProgramController (io.cdap.cdap.app.runtime.ProgramController)16 ProgramId (io.cdap.cdap.proto.id.ProgramId)10 IOException (java.io.IOException)10 BasicArguments (io.cdap.cdap.internal.app.runtime.BasicArguments)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 ExecutionException (java.util.concurrent.ExecutionException)8 Test (org.junit.Test)8 Injector (com.google.inject.Injector)6 ProgramDescriptor (io.cdap.cdap.app.program.ProgramDescriptor)6 SystemArguments (io.cdap.cdap.internal.app.runtime.SystemArguments)6 File (java.io.File)6 Supplier (com.google.common.base.Supplier)4 Throwables (com.google.common.base.Throwables)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Iterators (com.google.common.collect.Iterators)4 SettableFuture (com.google.common.util.concurrent.SettableFuture)4 AppWithAnonymousWorkflow (io.cdap.cdap.AppWithAnonymousWorkflow)4 MissingMapReduceWorkflowApp (io.cdap.cdap.MissingMapReduceWorkflowApp)4 MissingSparkWorkflowApp (io.cdap.cdap.MissingSparkWorkflowApp)4