Search in sources :

Example 26 with DefaultHttpRequestConfig

use of io.cdap.cdap.common.http.DefaultHttpRequestConfig in project cdap by cdapio.

the class TaskWorkerServiceTest method testPeriodicRestartWithInflightRequest.

@Test
public void testPeriodicRestartWithInflightRequest() throws IOException {
    CConfiguration cConf = createCConf();
    SConfiguration sConf = createSConf();
    cConf.setInt(Constants.TaskWorker.CONTAINER_KILL_AFTER_REQUEST_COUNT, 10);
    cConf.setInt(Constants.TaskWorker.CONTAINER_KILL_AFTER_DURATION_SECOND, 2);
    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 = "5000";
    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));
    Assert.assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    Assert.assertEquals(want, response.getResponseBodyAsString());
    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 27 with DefaultHttpRequestConfig

use of io.cdap.cdap.common.http.DefaultHttpRequestConfig in project cdap by cdapio.

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 28 with DefaultHttpRequestConfig

use of io.cdap.cdap.common.http.DefaultHttpRequestConfig 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 29 with DefaultHttpRequestConfig

use of io.cdap.cdap.common.http.DefaultHttpRequestConfig in project cdap by cdapio.

the class LogHttpHandlerTest method doGet.

/**
 * Performs a get call on the given path from the log query server.
 */
private HttpResponse doGet(String path) throws IOException {
    Discoverable discoverable = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(Constants.Service.LOG_QUERY)).pick(10, TimeUnit.SECONDS);
    Assert.assertNotNull(discoverable);
    // Path is literal, hence replacing the "%" with "%%" for formatter
    URL url = URIScheme.createURI(discoverable, path.replace("%", "%%")).toURL();
    return HttpRequests.execute(HttpRequest.get(url).build(), new DefaultHttpRequestConfig(false));
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) URL(java.net.URL) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy)

Example 30 with DefaultHttpRequestConfig

use of io.cdap.cdap.common.http.DefaultHttpRequestConfig in project cdap by cdapio.

the class ArtifactHttpHandlerTestBase method getArtifactLocationPath.

/**
 * Get the location path of the given artifact.
 */
String getArtifactLocationPath(ArtifactId artifactId) throws ArtifactNotFoundException, IOException {
    URL endpoint = getEndPoint(String.format("%s/namespaces/%s/artifacts/%s/versions/%s/location", Constants.Gateway.INTERNAL_API_VERSION_3, artifactId.getNamespace(), artifactId.getArtifact(), artifactId.getVersion())).toURL();
    HttpResponse httpResponse = HttpRequests.execute(HttpRequest.get(endpoint).build(), new DefaultHttpRequestConfig(false));
    int responseCode = httpResponse.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ArtifactNotFoundException(artifactId);
    }
    Assert.assertEquals(HttpURLConnection.HTTP_OK, responseCode);
    return httpResponse.getResponseBodyAsString();
}
Also used : DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpResponse(io.cdap.common.http.HttpResponse) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) URL(java.net.URL)

Aggregations

DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)82 HttpResponse (io.cdap.common.http.HttpResponse)62 URL (java.net.URL)56 Test (org.junit.Test)50 HttpRequest (io.cdap.common.http.HttpRequest)24 RunnableTaskRequest (io.cdap.cdap.api.service.worker.RunnableTaskRequest)14 URI (java.net.URI)14 InetSocketAddress (java.net.InetSocketAddress)12 Discoverable (org.apache.twill.discovery.Discoverable)12 RandomEndpointStrategy (io.cdap.cdap.common.discovery.RandomEndpointStrategy)8 Gson (com.google.gson.Gson)7 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)6 ETLBatchConfig (io.cdap.cdap.etl.proto.v2.ETLBatchConfig)6 ETLStage (io.cdap.cdap.etl.proto.v2.ETLStage)6 HttpRequestConfig (io.cdap.common.http.HttpRequestConfig)6 File (java.io.File)6 SConfiguration (io.cdap.cdap.common.conf.SConfiguration)4 ResolvingDiscoverable (io.cdap.cdap.common.discovery.ResolvingDiscoverable)4 NoOpInternalAuthenticator (io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator)4 RemoteClient (io.cdap.cdap.common.internal.remote.RemoteClient)4