use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by caskdata.
the class TaskWorkerServiceTest method testRestartAfterMultipleExecutions.
@Test
public void testRestartAfterMultipleExecutions() throws IOException {
CConfiguration cConf = createCConf();
SConfiguration sConf = createSConf();
cConf.setInt(Constants.TaskWorker.CONTAINER_KILL_AFTER_REQUEST_COUNT, 2);
cConf.setInt(Constants.TaskWorker.CONTAINER_KILL_AFTER_DURATION_SECOND, 0);
TaskWorkerService taskWorkerService = new TaskWorkerService(cConf, sConf, new InMemoryDiscoveryService(), (namespaceId, retryStrategy) -> null, new NoOpMetricsCollectionService());
serviceCompletionFuture = TaskWorkerTestUtil.getServiceCompletionFuture(taskWorkerService);
// start the service
taskWorkerService.startAndWait();
InetSocketAddress addr = taskWorkerService.getBindAddress();
URI uri = URI.create(String.format("http://%s:%s", addr.getHostName(), addr.getPort()));
// Post valid request
String want = "100";
RunnableTaskRequest req = RunnableTaskRequest.getBuilder(TestRunnableClass.class.getName()).withParam(want).build();
String reqBody = GSON.toJson(req);
HttpResponse response = HttpRequests.execute(HttpRequest.post(uri.resolve("/v3Internal/worker/run").toURL()).withBody(reqBody).build(), new DefaultHttpRequestConfig(false));
response = HttpRequests.execute(HttpRequest.post(uri.resolve("/v3Internal/worker/run").toURL()).withBody(reqBody).build(), new DefaultHttpRequestConfig(false));
TaskWorkerTestUtil.waitForServiceCompletion(serviceCompletionFuture);
Assert.assertEquals(Service.State.TERMINATED, taskWorkerService.state());
}
use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by caskdata.
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 caskdata.
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 caskdata.
the class ValidationHandler method validateRemotely.
private void validateRemotely(HttpServiceRequest request, HttpServiceResponder responder, String namespace) throws IOException {
String validationRequestString = StandardCharsets.UTF_8.decode(request.getContent()).toString();
RemoteValidationRequest remoteValidationRequest = new RemoteValidationRequest(namespace, validationRequestString);
RunnableTaskRequest runnableTaskRequest = RunnableTaskRequest.getBuilder(RemoteValidationTask.class.getName()).withParam(GSON.toJson(remoteValidationRequest)).build();
try {
byte[] bytes = getContext().runTask(runnableTaskRequest);
responder.sendString(Bytes.toString(bytes));
} catch (RemoteExecutionException e) {
RemoteTaskException remoteTaskException = e.getCause();
responder.sendError(getExceptionCode(remoteTaskException.getRemoteExceptionClassName(), remoteTaskException.getMessage(), namespace), remoteTaskException.getMessage());
} catch (Exception e) {
responder.sendError(HttpURLConnection.HTTP_INTERNAL_ERROR, e.getMessage());
}
}
use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by caskdata.
the class ConnectionHandler method executeRemotely.
/**
* Common method for all remote executions.
* Remote request is created, executed and response is added to {@link HttpServiceResponder}
* @param namespace namespace string
* @param request Serialized request string
* @param connection {@link Connection} details if present
* @param remoteExecutionTaskClass Remote execution task class
* @param responder {@link HttpServiceResponder} for the http request.
*/
private void executeRemotely(String namespace, String request, @Nullable Connection connection, Class<? extends RemoteConnectionTaskBase> remoteExecutionTaskClass, HttpServiceResponder responder) {
RemoteConnectionRequest remoteRequest = new RemoteConnectionRequest(namespace, request, connection);
RunnableTaskRequest runnableTaskRequest = RunnableTaskRequest.getBuilder(remoteExecutionTaskClass.getName()).withParam(GSON.toJson(remoteRequest)).build();
try {
byte[] bytes = getContext().runTask(runnableTaskRequest);
responder.sendString(new String(bytes, StandardCharsets.UTF_8));
} catch (RemoteExecutionException e) {
// TODO CDAP-18787 - Handle other exceptions
RemoteTaskException remoteTaskException = e.getCause();
responder.sendError(getExceptionCode(remoteTaskException.getRemoteExceptionClassName(), remoteTaskException.getMessage(), namespace), remoteTaskException.getMessage());
} catch (Exception e) {
responder.sendError(HttpURLConnection.HTTP_INTERNAL_ERROR, e.getMessage());
}
}
Aggregations