Search in sources :

Example 1 with PreviewRequest

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

the class RemotePreviewRequestFetcher method fetch.

@Override
public Optional<PreviewRequest> fetch() throws IOException, UnauthorizedException {
    HttpRequest request = remoteClientInternal.requestBuilder(HttpMethod.POST, "requests/pull").withBody(ByteBuffer.wrap(pollerInfoProvider.get())).build();
    HttpResponse httpResponse = remoteClientInternal.execute(request);
    if (httpResponse.getResponseCode() == 200) {
        PreviewRequest previewRequest = GSON.fromJson(httpResponse.getResponseBodyAsString(), PreviewRequest.class);
        if (previewRequest != null && previewRequest.getPrincipal() != null) {
            SecurityRequestContext.setUserId(previewRequest.getPrincipal().getName());
            SecurityRequestContext.setUserCredential(previewRequest.getPrincipal().getFullCredential());
        }
        return Optional.ofNullable(previewRequest);
    }
    throw new IOException(String.format("Received status code:%s and body: %s", httpResponse.getResponseCode(), httpResponse.getResponseBodyAsString(Charsets.UTF_8)));
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) IOException(java.io.IOException) PreviewRequest(io.cdap.cdap.app.preview.PreviewRequest)

Example 2 with PreviewRequest

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

the class DefaultPreviewStore method getAllInWaitingState.

@Override
public List<PreviewRequest> getAllInWaitingState() {
    // 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();
    byte[] startRowKey = new MDSKey.Builder().add(WAITING).build().getKey();
    byte[] stopRowKey = new MDSKey(Bytes.stopKeyForPrefix(startRowKey)).getKey();
    List<PreviewRequest> result = new ArrayList<>();
    try (Scanner scanner = previewQueueTable.scan(startRowKey, stopRowKey, null, null, null)) {
        Row indexRow;
        while ((indexRow = scanner.next()) != null) {
            Map<byte[], byte[]> columns = indexRow.getColumns();
            AppRequest request = gson.fromJson(Bytes.toString(columns.get(CONFIG)), AppRequest.class);
            ApplicationId applicationId = gson.fromJson(Bytes.toString(columns.get(APPID)), ApplicationId.class);
            Principal principal = gson.fromJson(Bytes.toString(columns.get(PRINCIPAL)), Principal.class);
            result.add(new PreviewRequest(applicationId, request, principal));
        }
    } catch (IOException e) {
        throw new RuntimeException("Error while listing the waiting preview requests.", e);
    }
    return result;
}
Also used : Scanner(io.cdap.cdap.api.dataset.table.Scanner) GsonBuilder(com.google.gson.GsonBuilder) Schema(io.cdap.cdap.api.data.schema.Schema) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException) AppRequest(io.cdap.cdap.proto.artifact.AppRequest) SchemaTypeAdapter(io.cdap.cdap.internal.io.SchemaTypeAdapter) Row(io.cdap.cdap.api.dataset.table.Row) PreviewRequest(io.cdap.cdap.app.preview.PreviewRequest) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Principal(io.cdap.cdap.proto.security.Principal)

Example 3 with PreviewRequest

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

the class PreviewHttpHandlerInternal method poll.

@POST
@Path("/requests/pull")
public void poll(FullHttpRequest request, HttpResponder responder) {
    byte[] pollerInfo = Bytes.toBytes(request.content().nioBuffer());
    PreviewRequest previewRequest = previewManager.poll(pollerInfo).orElse(null);
    if (previewRequest != null) {
        LOG.debug("Send preview request {} to poller {}", previewRequest.getProgram(), Bytes.toString(pollerInfo));
        responder.sendString(HttpResponseStatus.OK, GSON.toJson(previewRequest));
    } else {
        responder.sendStatus(HttpResponseStatus.OK);
    }
}
Also used : PreviewRequest(io.cdap.cdap.app.preview.PreviewRequest) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 4 with PreviewRequest

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

the class PreviewRunnerServiceTest method testStopPreview.

@Test
public void testStopPreview() throws InterruptedException, ExecutionException, TimeoutException {
    MockPreviewRunner mockRunner = new MockPreviewRunner();
    MockPreviewRequestFetcher fetcher = new MockPreviewRequestFetcher();
    PreviewRunnerService runnerService = new PreviewRunnerService(createCConf(), fetcher, mockRunner);
    runnerService.startAndWait();
    ProgramId programId = NamespaceId.DEFAULT.app("app").program(ProgramType.WORKFLOW, "workflow");
    fetcher.addRequest(new PreviewRequest(programId, null, null));
    Tasks.waitFor(true, () -> mockRunner.requests.get(programId) != null, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    runnerService.stopAndWait();
    Tasks.waitFor(PreviewStatus.Status.KILLED, () -> mockRunner.requests.get(programId).status.getStatus(), 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    Tasks.waitFor(Service.State.TERMINATED, runnerService::state, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
}
Also used : ProgramId(io.cdap.cdap.proto.id.ProgramId) PreviewRequest(io.cdap.cdap.app.preview.PreviewRequest) Test(org.junit.Test)

Example 5 with PreviewRequest

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

the class DefaultPreviewRequestQueueTest method testPreviewRequestQueue.

@Test
public void testPreviewRequestQueue() {
    PreviewConfig previewConfig = new PreviewConfig("WordCount", ProgramType.WORKFLOW, null, null);
    AppRequest<?> testRequest = new AppRequest<>(new ArtifactSummary("test", "1.0"), null, previewConfig);
    byte[] pollerInfo = Bytes.toBytes("runner-1");
    Optional<PreviewRequest> requestOptional = previewRequestQueue.poll(pollerInfo);
    Assert.assertFalse(requestOptional.isPresent());
    ApplicationId app1 = new ApplicationId("default", RunIds.generate().getId());
    PreviewRequest request = new PreviewRequest(app1, testRequest, null);
    previewRequestQueue.add(request);
    requestOptional = previewRequestQueue.poll(pollerInfo);
    Assert.assertTrue(requestOptional.isPresent());
    request = requestOptional.get();
    ProgramId programId1 = new ProgramId(app1, ProgramType.WORKFLOW, "WordCount");
    Assert.assertEquals(programId1, request.getProgram());
    Principal principal = new Principal("userFoo", Principal.PrincipalType.USER, new Credential("userFooCredential", Credential.CredentialType.EXTERNAL));
    PreviewRequest requestWithPrinciple = new PreviewRequest(app1, testRequest, principal);
    previewRequestQueue.add(requestWithPrinciple);
    requestOptional = previewRequestQueue.poll(pollerInfo);
    Assert.assertTrue(requestOptional.isPresent());
    request = requestOptional.get();
    Assert.assertTrue(request.getPrincipal().equals(principal));
    requestOptional = previewRequestQueue.poll(pollerInfo);
    Assert.assertFalse(requestOptional.isPresent());
    ApplicationId app2 = new ApplicationId("default", RunIds.generate().getId());
    request = new PreviewRequest(app2, testRequest, null);
    previewRequestQueue.add(request);
    Assert.assertEquals(0, previewRequestQueue.positionOf(app2));
    ApplicationId app3 = new ApplicationId("default", RunIds.generate().getId());
    request = new PreviewRequest(app3, testRequest, null);
    previewRequestQueue.add(request);
    Assert.assertEquals(1, previewRequestQueue.positionOf(app3));
    ApplicationId app4 = new ApplicationId("default", RunIds.generate().getId());
    request = new PreviewRequest(app4, testRequest, null);
    boolean exceptionThrown = false;
    try {
        previewRequestQueue.add(request);
    } catch (IllegalStateException e) {
        exceptionThrown = true;
    }
    Assert.assertTrue(exceptionThrown);
    requestOptional = previewRequestQueue.poll(pollerInfo);
    Assert.assertTrue(requestOptional.isPresent());
    request = requestOptional.get();
    ProgramId programId2 = new ProgramId(app2, ProgramType.WORKFLOW, "WordCount");
    Assert.assertEquals(programId2, request.getProgram());
    requestOptional = previewRequestQueue.poll(pollerInfo);
    Assert.assertTrue(requestOptional.isPresent());
    request = requestOptional.get();
    ProgramId programId3 = new ProgramId(app3, ProgramType.WORKFLOW, "WordCount");
    Assert.assertEquals(programId3, request.getProgram());
    requestOptional = previewRequestQueue.poll(pollerInfo);
    Assert.assertFalse(requestOptional.isPresent());
}
Also used : Credential(io.cdap.cdap.proto.security.Credential) ProgramId(io.cdap.cdap.proto.id.ProgramId) AppRequest(io.cdap.cdap.proto.artifact.AppRequest) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) PreviewRequest(io.cdap.cdap.app.preview.PreviewRequest) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) PreviewConfig(io.cdap.cdap.proto.artifact.preview.PreviewConfig) Principal(io.cdap.cdap.proto.security.Principal) Test(org.junit.Test)

Aggregations

PreviewRequest (io.cdap.cdap.app.preview.PreviewRequest)10 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)5 ProgramId (io.cdap.cdap.proto.id.ProgramId)5 IOException (java.io.IOException)4 Test (org.junit.Test)4 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)3 AppRequest (io.cdap.cdap.proto.artifact.AppRequest)3 PreviewConfig (io.cdap.cdap.proto.artifact.preview.PreviewConfig)3 Principal (io.cdap.cdap.proto.security.Principal)3 Credential (io.cdap.cdap.proto.security.Credential)2 ExecutionException (java.util.concurrent.ExecutionException)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 Schema (io.cdap.cdap.api.data.schema.Schema)1 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)1 Row (io.cdap.cdap.api.dataset.table.Row)1 Scanner (io.cdap.cdap.api.dataset.table.Scanner)1 PreviewMessage (io.cdap.cdap.app.preview.PreviewMessage)1 PreviewStatus (io.cdap.cdap.app.preview.PreviewStatus)1 ProgramController (io.cdap.cdap.app.runtime.ProgramController)1