Search in sources :

Example 6 with RunnableTaskRequest

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());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) SConfiguration(io.cdap.cdap.common.conf.SConfiguration) HttpResponse(io.cdap.common.http.HttpResponse) NoOpMetricsCollectionService(io.cdap.cdap.common.metrics.NoOpMetricsCollectionService) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) InMemoryDiscoveryService(org.apache.twill.discovery.InMemoryDiscoveryService) URI(java.net.URI) RunnableTaskRequest(io.cdap.cdap.api.service.worker.RunnableTaskRequest) Test(org.junit.Test)

Example 7 with RunnableTaskRequest

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());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) ArrayList(java.util.ArrayList) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) URI(java.net.URI) RunnableTaskRequest(io.cdap.cdap.api.service.worker.RunnableTaskRequest) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 8 with RunnableTaskRequest

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);
    }
}
Also used : ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) EntityImpersonator(io.cdap.cdap.security.impersonation.EntityImpersonator) SystemAppTaskContext(io.cdap.cdap.api.service.worker.SystemAppTaskContext) ArtifactRepository(io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository) CloseableClassLoader(io.cdap.cdap.api.artifact.CloseableClassLoader) Impersonator(io.cdap.cdap.security.impersonation.Impersonator) EntityImpersonator(io.cdap.cdap.security.impersonation.EntityImpersonator) ArtifactDescriptor(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor) Injector(com.google.inject.Injector) RunnableTaskContext(io.cdap.cdap.api.service.worker.RunnableTaskContext) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) RunnableTask(io.cdap.cdap.api.service.worker.RunnableTask) ArtifactLocalizerClient(io.cdap.cdap.internal.app.worker.sidecar.ArtifactLocalizerClient) File(java.io.File) RunnableTaskRequest(io.cdap.cdap.api.service.worker.RunnableTaskRequest)

Example 9 with RunnableTaskRequest

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());
    }
}
Also used : RemoteExecutionException(io.cdap.cdap.api.service.worker.RemoteExecutionException) RemoteTaskException(io.cdap.cdap.api.service.worker.RemoteTaskException) RunnableTaskRequest(io.cdap.cdap.api.service.worker.RunnableTaskRequest) RemoteExecutionException(io.cdap.cdap.api.service.worker.RemoteExecutionException) AccessException(io.cdap.cdap.api.security.AccessException) RemoteTaskException(io.cdap.cdap.api.service.worker.RemoteTaskException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException)

Example 10 with RunnableTaskRequest

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());
    }
}
Also used : RemoteExecutionException(io.cdap.cdap.api.service.worker.RemoteExecutionException) RemoteTaskException(io.cdap.cdap.api.service.worker.RemoteTaskException) RunnableTaskRequest(io.cdap.cdap.api.service.worker.RunnableTaskRequest) RemoteTaskException(io.cdap.cdap.api.service.worker.RemoteTaskException) ConnectionNotFoundException(io.cdap.cdap.etl.proto.connection.ConnectionNotFoundException) RemoteExecutionException(io.cdap.cdap.api.service.worker.RemoteExecutionException) ValidationException(io.cdap.cdap.etl.api.validation.ValidationException) IOException(java.io.IOException) ConnectionBadRequestException(io.cdap.cdap.etl.proto.connection.ConnectionBadRequestException)

Aggregations

RunnableTaskRequest (io.cdap.cdap.api.service.worker.RunnableTaskRequest)36 Test (org.junit.Test)24 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)14 HttpResponse (io.cdap.common.http.HttpResponse)12 MetricValues (io.cdap.cdap.api.metrics.MetricValues)10 InetSocketAddress (java.net.InetSocketAddress)10 URI (java.net.URI)10 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)4 RemoteExecutionException (io.cdap.cdap.api.service.worker.RemoteExecutionException)4 RemoteTaskException (io.cdap.cdap.api.service.worker.RemoteTaskException)4 RunnableTaskContext (io.cdap.cdap.api.service.worker.RunnableTaskContext)4 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)4 SConfiguration (io.cdap.cdap.common.conf.SConfiguration)4 NoOpMetricsCollectionService (io.cdap.cdap.common.metrics.NoOpMetricsCollectionService)4 IOException (java.io.IOException)4 InMemoryDiscoveryService (org.apache.twill.discovery.InMemoryDiscoveryService)4 Gson (com.google.gson.Gson)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 Injector (com.google.inject.Injector)2 ArtifactVersion (io.cdap.cdap.api.artifact.ArtifactVersion)2