Search in sources :

Example 1 with PreviewMessage

use of io.cdap.cdap.app.preview.PreviewMessage in project cdap by caskdata.

the class DefaultPreviewRunner method setStatus.

private void setStatus(ProgramId programId, PreviewStatus previewStatus) {
    LOG.debug("Setting preview status for {} to {}", programId, previewStatus.getStatus());
    PreviewMessage message = new PreviewMessage(PreviewMessage.Type.STATUS, programId.getParent(), GSON.toJsonTree(previewStatus));
    previewDataPublisher.publish(programId.getParent(), message);
}
Also used : PreviewMessage(io.cdap.cdap.app.preview.PreviewMessage)

Example 2 with PreviewMessage

use of io.cdap.cdap.app.preview.PreviewMessage in project cdap by caskdata.

the class PreviewDataSubscriberService method processMessages.

@Override
protected void processMessages(StructuredTableContext structuredTableContext, Iterator<ImmutablePair<String, PreviewMessage>> messages) throws Exception {
    Map<PreviewMessage.Type, PreviewMessageProcessor> processors = new HashMap<>();
    // Loop over all fetched messages and process them with corresponding PreviewMessageProcessor
    while (messages.hasNext()) {
        ImmutablePair<String, PreviewMessage> next = messages.next();
        String messageId = next.getFirst();
        PreviewMessage message = next.getSecond();
        PreviewMessageProcessor processor = processors.computeIfAbsent(message.getType(), type -> {
            switch(type) {
                case DATA:
                    return new PreviewDataProcessor();
                case STATUS:
                    return new PreviewStatusWriter();
                case PROGRAM_RUN_ID:
                    return new PreviewProgramRunIdWriter();
                default:
                    return null;
            }
        });
        // noinspection ConstantConditions
        if (processor == null) {
            LOG.warn("Unsupported preview message type {}. Message ignored.", message.getType());
            continue;
        }
        try {
            processor.processMessage(message);
            errorCount = 0;
        } catch (Exception e) {
            if (messageId.equals(erroredMessageId)) {
                errorCount++;
                if (errorCount >= maxRetriesOnError) {
                    LOG.warn("Skipping preview message {} after processing it has caused {} consecutive errors: {}", message, errorCount, e.getMessage());
                    continue;
                }
            } else {
                erroredMessageId = messageId;
                errorCount = 1;
            }
            throw e;
        }
    }
}
Also used : HashMap(java.util.HashMap) PreviewMessage(io.cdap.cdap.app.preview.PreviewMessage)

Example 3 with PreviewMessage

use of io.cdap.cdap.app.preview.PreviewMessage in project cdap by caskdata.

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 4 with PreviewMessage

use of io.cdap.cdap.app.preview.PreviewMessage in project cdap by caskdata.

the class DefaultDataTracer method info.

@Override
public void info(String propertyName, Object propertyValue) {
    PreviewDataPayload payload = new PreviewDataPayload(applicationId, tracerName, propertyName, propertyValue);
    PreviewMessage message = new PreviewMessage(PreviewMessage.Type.DATA, applicationId, GSON.toJsonTree(payload));
    previewDataPublisher.publish(applicationId, message);
}
Also used : PreviewMessage(io.cdap.cdap.app.preview.PreviewMessage)

Aggregations

PreviewMessage (io.cdap.cdap.app.preview.PreviewMessage)4 HashMap (java.util.HashMap)2 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)1 PreviewRequest (io.cdap.cdap.app.preview.PreviewRequest)1 PreviewStatus (io.cdap.cdap.app.preview.PreviewStatus)1 ProgramController (io.cdap.cdap.app.runtime.ProgramController)1 NamespaceAlreadyExistsException (io.cdap.cdap.common.NamespaceAlreadyExistsException)1 AbstractListener (io.cdap.cdap.internal.app.runtime.AbstractListener)1 BasicThrowable (io.cdap.cdap.proto.BasicThrowable)1 NamespaceMeta (io.cdap.cdap.proto.NamespaceMeta)1 PreferencesDetail (io.cdap.cdap.proto.PreferencesDetail)1 PreviewConfig (io.cdap.cdap.proto.artifact.preview.PreviewConfig)1 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)1 ProgramId (io.cdap.cdap.proto.id.ProgramId)1 IOException (java.io.IOException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1