Search in sources :

Example 16 with RunnableTaskRequest

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());
}
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 17 with RunnableTaskRequest

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);
    }
}
Also used : ConfiguratorTask(io.cdap.cdap.internal.app.worker.ConfiguratorTask) RunnableTaskRequest(io.cdap.cdap.api.service.worker.RunnableTaskRequest)

Example 18 with RunnableTaskRequest

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));
    }
}
Also used : DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) RunnableTaskContext(io.cdap.cdap.api.service.worker.RunnableTaskContext) RunnableTaskRequest(io.cdap.cdap.api.service.worker.RunnableTaskRequest) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 19 with RunnableTaskRequest

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);
    }
}
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 20 with RunnableTaskRequest

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());
}
Also used : ByteBuffer(java.nio.ByteBuffer) RunnableTaskRequest(io.cdap.cdap.api.service.worker.RunnableTaskRequest) Test(org.junit.Test)

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