Search in sources :

Example 11 with PreviewStatus

use of io.cdap.cdap.app.preview.PreviewStatus in project cdap by cdapio.

the class DefaultPreviewManager method stopPreview.

@Override
public void stopPreview(@Name("applicationId") ApplicationId applicationId) throws Exception {
    accessEnforcer.enforce(applicationId, authenticationContext.getPrincipal(), ApplicationPermission.PREVIEW);
    PreviewStatus status = getStatus(applicationId);
    if (status.getStatus().isEndState()) {
        throw new BadRequestException(String.format("Preview run cannot be stopped. It is already in %s state.", status.getStatus().name()));
    }
    if (status.getStatus() == PreviewStatus.Status.WAITING) {
        previewStore.setPreviewStatus(applicationId, new PreviewStatus(PreviewStatus.Status.KILLED, status.getSubmitTime(), null, null, null));
        return;
    }
    previewRunStopper.stop(applicationId);
}
Also used : PreviewStatus(io.cdap.cdap.app.preview.PreviewStatus) BadRequestException(io.cdap.cdap.common.BadRequestException)

Example 12 with PreviewStatus

use of io.cdap.cdap.app.preview.PreviewStatus in project cdap by cdapio.

the class PreviewServiceMainTest method waitForPreview.

/**
 * Wait for preview to complete with a deadline
 */
private void waitForPreview(ApplicationId previewId) throws MalformedURLException, java.util.concurrent.TimeoutException, InterruptedException, java.util.concurrent.ExecutionException {
    URL statusUrl = getRouterBaseURI().resolve(String.format("/v3/namespaces/default/previews/%s/status", previewId.getApplication())).toURL();
    Tasks.waitFor(PreviewStatus.Status.COMPLETED, () -> {
        HttpResponse statusResponse = HttpRequests.execute(HttpRequest.get(statusUrl).build(), getHttpRequestConfig());
        if (statusResponse.getResponseCode() != 200) {
            return null;
        }
        PreviewStatus previewStatus = GSON.fromJson(statusResponse.getResponseBodyAsString(), PreviewStatus.class);
        return previewStatus.getStatus();
    }, 2, TimeUnit.MINUTES);
}
Also used : PreviewStatus(io.cdap.cdap.app.preview.PreviewStatus) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL)

Example 13 with PreviewStatus

use of io.cdap.cdap.app.preview.PreviewStatus in project cdap by cdapio.

the class DefaultPreviewStore method add.

@Override
public void add(ApplicationId applicationId, AppRequest appRequest, @Nullable Principal principal) {
    // PreviewStore is a singleton and we have to create gson for each operation since gson is not thread safe.
    Gson gson = new GsonBuilder().registerTypeAdapter(Schema.class, new SchemaTypeAdapter()).create();
    long timeInSeconds = RunIds.getTime(applicationId.getApplication(), TimeUnit.SECONDS);
    MDSKey mdsKey = new MDSKey.Builder().add(WAITING).add(timeInSeconds).add(applicationId.getNamespace()).add(applicationId.getApplication()).build();
    try {
        previewQueueTable.putDefaultVersion(mdsKey.getKey(), APPID, Bytes.toBytes(gson.toJson(applicationId)));
        previewQueueTable.putDefaultVersion(mdsKey.getKey(), CONFIG, Bytes.toBytes(gson.toJson(appRequest)));
        previewQueueTable.putDefaultVersion(mdsKey.getKey(), PRINCIPAL, Bytes.toBytes(gson.toJson(principal)));
        long submitTimeInMillis = RunIds.getTime(applicationId.getApplication(), TimeUnit.MILLISECONDS);
        setPreviewStatus(applicationId, new PreviewStatus(PreviewStatus.Status.WAITING, submitTimeInMillis, null, null, null));
    } catch (IOException e) {
        String message = String.format("Error while adding preview request with application '%s' in preview store.", applicationId);
        throw new RuntimeException(message, e);
    }
}
Also used : SchemaTypeAdapter(io.cdap.cdap.internal.io.SchemaTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Schema(io.cdap.cdap.api.data.schema.Schema) PreviewStatus(io.cdap.cdap.app.preview.PreviewStatus) Gson(com.google.gson.Gson) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException)

Example 14 with PreviewStatus

use of io.cdap.cdap.app.preview.PreviewStatus in project cdap by cdapio.

the class DefaultPreviewStoreTest method testPreviewInfo.

@Test
public void testPreviewInfo() throws IOException {
    // test non existing preview
    ApplicationId nonexist = new ApplicationId("ns1", "nonexist");
    Assert.assertNull(store.getProgramRunId(nonexist));
    Assert.assertNull(store.getPreviewStatus(nonexist));
    // test put and get
    ApplicationId applicationId = new ApplicationId("ns1", "app1");
    ProgramRunId runId = new ProgramRunId("ns1", "app1", ProgramType.WORKFLOW, "test", RunIds.generate().getId());
    PreviewStatus status = new PreviewStatus(PreviewStatus.Status.COMPLETED, System.currentTimeMillis(), null, 0L, System.currentTimeMillis());
    store.setProgramId(runId);
    store.setPreviewStatus(applicationId, status);
    Assert.assertEquals(runId, store.getProgramRunId(applicationId));
    Assert.assertEquals(status, store.getPreviewStatus(applicationId));
}
Also used : PreviewStatus(io.cdap.cdap.app.preview.PreviewStatus) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 15 with PreviewStatus

use of io.cdap.cdap.app.preview.PreviewStatus 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)

Aggregations

PreviewStatus (io.cdap.cdap.app.preview.PreviewStatus)32 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)18 Schema (io.cdap.cdap.api.data.schema.Schema)16 PreviewConfig (io.cdap.cdap.proto.artifact.preview.PreviewConfig)16 TimeoutException (java.util.concurrent.TimeoutException)16 Table (io.cdap.cdap.api.dataset.table.Table)14 PreviewManager (io.cdap.cdap.app.preview.PreviewManager)14 ETLBatchConfig (io.cdap.cdap.etl.proto.v2.ETLBatchConfig)14 ETLStage (io.cdap.cdap.etl.proto.v2.ETLStage)14 AppRequest (io.cdap.cdap.proto.artifact.AppRequest)14 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)12 IOException (java.io.IOException)12 ExecutionException (java.util.concurrent.ExecutionException)8 NamespaceAlreadyExistsException (io.cdap.cdap.common.NamespaceAlreadyExistsException)4 BasicThrowable (io.cdap.cdap.proto.BasicThrowable)4 ProgramId (io.cdap.cdap.proto.id.ProgramId)4 Path (java.nio.file.Path)4 ArtifactSelectorConfig (io.cdap.cdap.etl.proto.ArtifactSelectorConfig)3 ConnectionCreationRequest (io.cdap.cdap.etl.proto.connection.ConnectionCreationRequest)3 PluginInfo (io.cdap.cdap.etl.proto.connection.PluginInfo)3