Search in sources :

Example 1 with RunnableTaskRequest

use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by caskdata.

the class RunnableTaskLauncherTest method testRunnableTaskRequestJsonConversion.

@Test
public void testRunnableTaskRequestJsonConversion() {
    ArtifactId artifactId = new ArtifactId("test-artifact", new ArtifactVersion("1.0"), ArtifactScope.SYSTEM);
    RunnableTaskRequest request = RunnableTaskRequest.getBuilder("taskClassName").withParam("param").withNamespace("n1").withArtifact(artifactId).build();
    Gson gson = new Gson();
    String requestJson = gson.toJson(request);
    RunnableTaskRequest requestFromJson = gson.fromJson(requestJson, RunnableTaskRequest.class);
    Assert.assertEquals(request.getClassName(), requestFromJson.getClassName());
    Assert.assertEquals(request.getNamespace(), requestFromJson.getNamespace());
    Assert.assertEquals(request.getParam(), requestFromJson.getParam());
    Assert.assertEquals(request.getArtifactId(), requestFromJson.getArtifactId());
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) Gson(com.google.gson.Gson) RunnableTaskRequest(io.cdap.cdap.api.service.worker.RunnableTaskRequest) Test(org.junit.Test)

Example 2 with RunnableTaskRequest

use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by caskdata.

the class TaskWorkerMetricsTest method testWrappedRequest.

@Test
public void testWrappedRequest() throws IOException {
    String taskClassName = TaskWorkerServiceTest.TestRunnableClass.class.getName();
    String wrappedClassName = "testClassName";
    RunnableTaskRequest req = RunnableTaskRequest.getBuilder(taskClassName).withParam("100").withEmbeddedTaskRequest(RunnableTaskRequest.getBuilder(wrappedClassName).build()).build();
    String reqBody = GSON.toJson(req);
    HttpResponse response = HttpRequests.execute(HttpRequest.post(uri.resolve("/v3Internal/worker/run").toURL()).withBody(reqBody).build(), new DefaultHttpRequestConfig(false));
    TaskWorkerTestUtil.waitForServiceCompletion(taskWorkerStateFuture);
    Assert.assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    Assert.assertSame(1, published.size());
    // check the metrics are present
    MetricValues metricValues = published.get(0);
    Assert.assertTrue(hasMetric(metricValues, Constants.Metrics.TaskWorker.REQUEST_COUNT));
    Assert.assertTrue(hasMetric(metricValues, Constants.Metrics.TaskWorker.REQUEST_LATENCY_MS));
    // check the clz tag is set correctly
    Assert.assertEquals(wrappedClassName, metricValues.getTags().get(Constants.Metrics.Tag.CLASS));
}
Also used : DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpResponse(io.cdap.common.http.HttpResponse) MetricValues(io.cdap.cdap.api.metrics.MetricValues) RunnableTaskRequest(io.cdap.cdap.api.service.worker.RunnableTaskRequest) Test(org.junit.Test)

Example 3 with RunnableTaskRequest

use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by caskdata.

the class TaskWorkerMetricsTest method testSimpleRequest.

@Test
public void testSimpleRequest() throws IOException {
    String taskClassName = TaskWorkerServiceTest.TestRunnableClass.class.getName();
    RunnableTaskRequest req = RunnableTaskRequest.getBuilder(taskClassName).withParam("100").build();
    String reqBody = GSON.toJson(req);
    HttpResponse response = HttpRequests.execute(HttpRequest.post(uri.resolve("/v3Internal/worker/run").toURL()).withBody(reqBody).build(), new DefaultHttpRequestConfig(false));
    TaskWorkerTestUtil.waitForServiceCompletion(taskWorkerStateFuture);
    Assert.assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    Assert.assertSame(1, published.size());
    // check the metrics are present
    MetricValues metricValues = published.get(0);
    Assert.assertTrue(hasMetric(metricValues, Constants.Metrics.TaskWorker.REQUEST_LATENCY_MS));
    Assert.assertTrue(hasMetric(metricValues, Constants.Metrics.TaskWorker.REQUEST_COUNT));
    // check the clz tag is set correctly
    Assert.assertEquals(taskClassName, metricValues.getTags().get("clz"));
}
Also used : DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpResponse(io.cdap.common.http.HttpResponse) MetricValues(io.cdap.cdap.api.metrics.MetricValues) RunnableTaskRequest(io.cdap.cdap.api.service.worker.RunnableTaskRequest) Test(org.junit.Test)

Example 4 with RunnableTaskRequest

use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by caskdata.

the class TaskWorkerServiceTest method testStartAndStopWithInvalidRequest.

@Test
public void testStartAndStopWithInvalidRequest() throws Exception {
    InetSocketAddress addr = taskWorkerService.getBindAddress();
    URI uri = URI.create(String.format("http://%s:%s", addr.getHostName(), addr.getPort()));
    // Post invalid request
    RunnableTaskRequest noClassReq = RunnableTaskRequest.getBuilder("NoClass").build();
    String reqBody = GSON.toJson(noClassReq);
    HttpResponse response = HttpRequests.execute(HttpRequest.post(uri.resolve("/v3Internal/worker/run").toURL()).withBody(reqBody).build(), new DefaultHttpRequestConfig(false));
    Assert.assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode());
    BasicThrowable basicThrowable;
    basicThrowable = GSON.fromJson(response.getResponseBodyAsString(), BasicThrowable.class);
    Assert.assertTrue(basicThrowable.getClassName().contains("java.lang.ClassNotFoundException"));
    Assert.assertNotNull(basicThrowable.getMessage());
    Assert.assertTrue(basicThrowable.getMessage().contains("NoClass"));
    Assert.assertNotEquals(basicThrowable.getStackTraces().length, 0);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpResponse(io.cdap.common.http.HttpResponse) BasicThrowable(io.cdap.cdap.proto.BasicThrowable) URI(java.net.URI) RunnableTaskRequest(io.cdap.cdap.api.service.worker.RunnableTaskRequest) Test(org.junit.Test)

Example 5 with RunnableTaskRequest

use of io.cdap.cdap.api.service.worker.RunnableTaskRequest in project cdap by caskdata.

the class TaskWorkerServiceTest method testStartAndStopWithValidRequest.

@Test
public void testStartAndStopWithValidRequest() throws IOException {
    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));
    TaskWorkerTestUtil.waitForServiceCompletion(serviceCompletionFuture);
    Assert.assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    Assert.assertEquals(want, response.getResponseBodyAsString());
    Assert.assertEquals(Service.State.TERMINATED, taskWorkerService.state());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpResponse(io.cdap.common.http.HttpResponse) URI(java.net.URI) RunnableTaskRequest(io.cdap.cdap.api.service.worker.RunnableTaskRequest) Test(org.junit.Test)

Aggregations

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