Search in sources :

Example 16 with PreviewConfig

use of io.cdap.cdap.proto.artifact.preview.PreviewConfig 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 17 with PreviewConfig

use of io.cdap.cdap.proto.artifact.preview.PreviewConfig in project cdap by caskdata.

the class DefaultPreviewStoreTest method testPreviewWaitingRequests.

@Test
public void testPreviewWaitingRequests() throws Exception {
    byte[] pollerInfo = Bytes.toBytes("runner-1");
    PreviewConfig previewConfig = new PreviewConfig("WordCount", ProgramType.WORKFLOW, null, null);
    AppRequest<?> testRequest = new AppRequest<>(new ArtifactSummary("test", "1.0"), null, previewConfig);
    Assert.assertEquals(0, store.getAllInWaitingState().size());
    RunId id1 = RunIds.generate();
    ApplicationId applicationId = new ApplicationId("ns1", id1.getId());
    store.add(applicationId, testRequest, null);
    List<PreviewRequest> allWaiting = store.getAllInWaitingState();
    Assert.assertEquals(1, allWaiting.size());
    AppRequest appRequest = allWaiting.get(0).getAppRequest();
    Assert.assertNotNull(appRequest);
    Assert.assertNotNull(appRequest.getPreview());
    Assert.assertEquals("WordCount", appRequest.getPreview().getProgramName());
    store.setPreviewRequestPollerInfo(applicationId, pollerInfo);
    Assert.assertEquals(0, store.getAllInWaitingState().size());
    // add 2 requests to the queue
    ApplicationId applicationId2 = new ApplicationId("ns1", RunIds.generate().getId());
    store.add(applicationId2, testRequest, null);
    ApplicationId applicationId3 = new ApplicationId("ns1", RunIds.generate().getId());
    store.add(applicationId3, testRequest, null);
    allWaiting = store.getAllInWaitingState();
    Assert.assertEquals(2, allWaiting.size());
    Assert.assertEquals(applicationId2, allWaiting.get(0).getProgram().getParent());
    Assert.assertEquals(applicationId3, allWaiting.get(1).getProgram().getParent());
    store.setPreviewRequestPollerInfo(applicationId2, pollerInfo);
    allWaiting = store.getAllInWaitingState();
    Assert.assertEquals(1, allWaiting.size());
    Assert.assertEquals(applicationId3, allWaiting.get(0).getProgram().getParent());
    store.setPreviewRequestPollerInfo(applicationId3, pollerInfo);
    allWaiting = store.getAllInWaitingState();
    Assert.assertEquals(0, allWaiting.size());
    // Add a preview request that has a principle associated with it
    ApplicationId applicationId4 = new ApplicationId("ns1", RunIds.generate().getId());
    Principal principal = new Principal("userForApplicationId4", Principal.PrincipalType.USER, new Credential("userForApplicationId4Credential", Credential.CredentialType.EXTERNAL));
    store.add(applicationId4, testRequest, principal);
    allWaiting = store.getAllInWaitingState();
    Assert.assertEquals(1, allWaiting.size());
    Assert.assertEquals(applicationId4, allWaiting.get(0).getProgram().getParent());
    Assert.assertTrue(allWaiting.get(0).getPrincipal().equals(principal));
    store.setPreviewRequestPollerInfo(applicationId4, pollerInfo);
    allWaiting = store.getAllInWaitingState();
    Assert.assertEquals(0, allWaiting.size());
}
Also used : Credential(io.cdap.cdap.proto.security.Credential) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) RunId(org.apache.twill.api.RunId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) PreviewRequest(io.cdap.cdap.app.preview.PreviewRequest) PreviewConfig(io.cdap.cdap.proto.artifact.preview.PreviewConfig) Principal(io.cdap.cdap.proto.security.Principal) AppRequest(io.cdap.cdap.proto.artifact.AppRequest) Test(org.junit.Test)

Example 18 with PreviewConfig

use of io.cdap.cdap.proto.artifact.preview.PreviewConfig in project cdap by caskdata.

the class DefaultPreviewStoreTest method testPreviewTTL.

@Test
public void testPreviewTTL() throws Exception {
    PreviewConfig previewConfig = new PreviewConfig("WordCount", ProgramType.WORKFLOW, null, null);
    AppRequest<?> testRequest = new AppRequest<>(new ArtifactSummary("test", "1.0"), null, previewConfig);
    String firstApplication = RunIds.generate(System.currentTimeMillis() - 5000).getId();
    ApplicationId firstApplicationId = new ApplicationId(NamespaceMeta.DEFAULT.getName(), firstApplication);
    String secondApplication = RunIds.generate(System.currentTimeMillis() - 4000).getId();
    ApplicationId secondApplicationId = new ApplicationId(NamespaceMeta.DEFAULT.getName(), secondApplication);
    String thirdApplication = RunIds.generate(System.currentTimeMillis()).getId();
    ApplicationId thirdApplicationId = new ApplicationId(NamespaceMeta.DEFAULT.getName(), thirdApplication);
    store.add(firstApplicationId, testRequest, null);
    store.add(secondApplicationId, testRequest, null);
    store.add(thirdApplicationId, testRequest, null);
    // set poller info so that it gets removed from WAITING state
    store.setPreviewRequestPollerInfo(firstApplicationId, null);
    // put data for the first application
    store.put(firstApplicationId, "mytracer", "key1", "value1");
    store.put(firstApplicationId, "mytracer", "key1", 2);
    // get the data for first application and logger name "mytracer"
    Map<String, List<JsonElement>> firstApplicationData = store.get(firstApplicationId, "mytracer");
    // key1 and key2 are two keys inserted for the first application.
    Assert.assertEquals(1, firstApplicationData.size());
    Assert.assertEquals("value1", firstApplicationData.get("key1").get(0).getAsString());
    Assert.assertEquals(2, firstApplicationData.get("key1").get(1).getAsInt());
    // secondApplication and thirdApplication are in waiting state
    Assert.assertEquals(2, store.getAllInWaitingState().size());
    // Delete data for firstApplication as well as secondApplication
    store.deleteExpiredData(2);
    firstApplicationData = store.get(firstApplicationId, "mytracer");
    Assert.assertEquals(0, firstApplicationData.size());
    // only thirdApplication should be in waiting now
    Assert.assertEquals(1, store.getAllInWaitingState().size());
    Assert.assertEquals(thirdApplicationId, store.getAllInWaitingState().iterator().next().getProgram().getParent());
}
Also used : ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) List(java.util.List) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) PreviewConfig(io.cdap.cdap.proto.artifact.preview.PreviewConfig) AppRequest(io.cdap.cdap.proto.artifact.AppRequest) Test(org.junit.Test)

Aggregations

PreviewConfig (io.cdap.cdap.proto.artifact.preview.PreviewConfig)18 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)15 AppRequest (io.cdap.cdap.proto.artifact.AppRequest)14 PreviewStatus (io.cdap.cdap.app.preview.PreviewStatus)9 TimeoutException (java.util.concurrent.TimeoutException)9 Schema (io.cdap.cdap.api.data.schema.Schema)8 Table (io.cdap.cdap.api.dataset.table.Table)8 PreviewManager (io.cdap.cdap.app.preview.PreviewManager)8 ETLStage (io.cdap.cdap.etl.proto.v2.ETLStage)8 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)7 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)7 ETLBatchConfig (io.cdap.cdap.etl.proto.v2.ETLBatchConfig)7 Test (org.junit.Test)6 IOException (java.io.IOException)4 ExecutionException (java.util.concurrent.ExecutionException)4 PreviewRequest (io.cdap.cdap.app.preview.PreviewRequest)3 ProgramId (io.cdap.cdap.proto.id.ProgramId)3 List (java.util.List)3 TypeToken (com.google.common.reflect.TypeToken)2 JsonElement (com.google.gson.JsonElement)2