use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by cdapio.
the class TaskWorkerServiceTest method testConcurrentRequests.
@Test
public void testConcurrentRequests() throws Exception {
InetSocketAddress addr = taskWorkerService.getBindAddress();
URI uri = URI.create(String.format("http://%s:%s", addr.getHostName(), addr.getPort()));
RunnableTaskRequest request = RunnableTaskRequest.getBuilder(TestRunnableClass.class.getName()).withParam("1000").build();
String reqBody = GSON.toJson(request);
List<Callable<HttpResponse>> calls = new ArrayList<>();
int concurrentRequests = 2;
for (int i = 0; i < concurrentRequests; i++) {
calls.add(() -> HttpRequests.execute(HttpRequest.post(uri.resolve("/v3Internal/worker/run").toURL()).withBody(reqBody).build(), new DefaultHttpRequestConfig(false)));
}
List<Future<HttpResponse>> responses = Executors.newFixedThreadPool(concurrentRequests).invokeAll(calls);
int okResponse = 0;
int conflictResponse = 0;
for (int i = 0; i < concurrentRequests; i++) {
if (responses.get(i).get().getResponseCode() == HttpResponseStatus.OK.code()) {
okResponse++;
} else if (responses.get(i).get().getResponseCode() == HttpResponseStatus.TOO_MANY_REQUESTS.code()) {
conflictResponse++;
}
}
TaskWorkerTestUtil.waitForServiceCompletion(serviceCompletionFuture);
Assert.assertEquals(1, okResponse);
Assert.assertEquals(concurrentRequests, okResponse + conflictResponse);
Assert.assertEquals(Service.State.TERMINATED, taskWorkerService.state());
}
use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by cdapio.
the class RemoteConfigurator method config.
@Override
public ListenableFuture<ConfigResponse> config() {
try {
RunnableTaskRequest request = RunnableTaskRequest.getBuilder(ConfiguratorTask.class.getName()).withParam(GSON.toJson(deploymentInfo)).build();
byte[] result = remoteTaskExecutor.runTask(request);
return Futures.immediateFuture(GSON.fromJson(new String(result, StandardCharsets.UTF_8), DefaultConfigResponse.class));
} catch (Exception ex) {
return Futures.immediateFailedFuture(ex);
}
}
use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by cdapio.
the class TaskWorkerHttpHandlerInternal method run.
@POST
@Path("/run")
public void run(FullHttpRequest request, HttpResponder responder) {
if (!hasInflightRequest.compareAndSet(false, true)) {
responder.sendStatus(HttpResponseStatus.TOO_MANY_REQUESTS);
return;
}
requestProcessedCount.incrementAndGet();
long startTime = System.currentTimeMillis();
String className = null;
try {
RunnableTaskRequest runnableTaskRequest = GSON.fromJson(request.content().toString(StandardCharsets.UTF_8), RunnableTaskRequest.class);
className = getTaskClassName(runnableTaskRequest);
RunnableTaskContext runnableTaskContext = runnableTaskLauncher.launchRunnableTask(runnableTaskRequest);
responder.sendContent(HttpResponseStatus.OK, new RunnableTaskBodyProducer(runnableTaskContext, stopper, new TaskDetails(true, className, startTime)), new DefaultHttpHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM));
} catch (ClassNotFoundException | ClassCastException ex) {
responder.sendString(HttpResponseStatus.BAD_REQUEST, exceptionToJson(ex), EmptyHttpHeaders.INSTANCE);
// Since the user class is not even loaded, no user code ran, hence it's ok to not terminate the runner
stopper.accept(false, new TaskDetails(false, className, startTime));
} catch (Exception ex) {
LOG.error("Failed to run task {}", request.content().toString(StandardCharsets.UTF_8), ex);
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, exceptionToJson(ex), EmptyHttpHeaders.INSTANCE);
// Potentially ran user code, hence terminate the runner.
stopper.accept(true, new TaskDetails(false, className, startTime));
}
}
use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by cdapio.
the class SystemAppTask method run.
@Override
public void run(RunnableTaskContext context) throws Exception {
ArtifactId systemAppArtifactId = context.getArtifactId();
if (systemAppArtifactId == null) {
throw new IllegalArgumentException("Missing artifactId from the system app task request");
}
LOG.debug("Received system app task for artifact {}", systemAppArtifactId);
Injector injector = createInjector(cConf);
ArtifactRepository artifactRepository = injector.getInstance(ArtifactRepository.class);
Impersonator impersonator = injector.getInstance(Impersonator.class);
String systemAppNamespace = context.getNamespace();
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.from(systemAppNamespace), systemAppArtifactId.getName(), systemAppArtifactId.getVersion());
ArtifactLocalizerClient localizerClient = injector.getInstance(ArtifactLocalizerClient.class);
File artifactLocation = localizerClient.getUnpackedArtifactLocation(Artifacts.toProtoArtifactId(new NamespaceId(systemAppNamespace), systemAppArtifactId));
EntityImpersonator classLoaderImpersonator = new EntityImpersonator(artifactId.toEntityId(), impersonator);
try (CloseableClassLoader artifactClassLoader = artifactRepository.createArtifactClassLoader(new ArtifactDescriptor(artifactId.getNamespace().getId(), artifactId.toArtifactId(), Locations.toLocation(artifactLocation)), classLoaderImpersonator);
SystemAppTaskContext systemAppTaskContext = buildTaskSystemAppContext(injector, systemAppNamespace, systemAppArtifactId, artifactClassLoader)) {
RunnableTaskRequest taskRequest = context.getEmbeddedRequest();
String taskClassName = taskRequest.getClassName();
if (taskClassName == null) {
LOG.debug("No system app task to execute");
return;
}
LOG.debug("Requested to run system app task {}", taskClassName);
Class<?> clazz = artifactClassLoader.loadClass(taskClassName);
if (!(RunnableTask.class.isAssignableFrom(clazz))) {
throw new ClassCastException(String.format("%s is not a RunnableTask", taskClassName));
}
LOG.debug("Launching system app task {}", taskClassName);
RunnableTask runnableTask = (RunnableTask) injector.getInstance(clazz);
RunnableTaskContext runnableTaskContext = new RunnableTaskContext(taskRequest.getParam().getSimpleParam(), null, null, null, systemAppTaskContext) {
@Override
public void writeResult(byte[] data) throws IOException {
context.writeResult(data);
}
@Override
public void setTerminateOnComplete(boolean terminate) {
context.setTerminateOnComplete(terminate);
}
@Override
public boolean isTerminateOnComplete() {
return context.isTerminateOnComplete();
}
};
runnableTask.run(runnableTaskContext);
LOG.debug("System app task completed {}", taskClassName);
}
}
use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by cdapio.
the class RunnableTaskLauncherTest method testLaunchRunnableTask.
@Test
public void testLaunchRunnableTask() throws Exception {
String want = "test-want";
RunnableTaskRequest request = RunnableTaskRequest.getBuilder(TestRunnableTask.class.getName()).withParam(want).build();
RunnableTaskLauncher launcher = new RunnableTaskLauncher(CConfiguration.create());
ByteBuffer got = launcher.launchRunnableTask(request).getResult();
Assert.assertEquals(want, StandardCharsets.UTF_8.decode(got).toString());
}
Aggregations