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)));
}
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;
}
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);
}
}
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);
}
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());
}
Aggregations