Search in sources :

Example 6 with DefaultHttpRequestConfig

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

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

the class RemoteHealthCheckFetcher method getHealthDetails.

/**
 * Get the application detail for the given application id
 */
public Map<String, Object> getHealthDetails(String serviceName, String instanceName) throws IOException, NotFoundException, UnauthorizedException {
    /* Need to get the substring inside the serviceName:
    EX: serviceName = "cdap-bundle-test-v7-health-check-appfabric-service",
        instanceName = "bundle-test-v7",   instanceNameLength = length("bundle-test-v7-")
        updatedServiceName = "health.check.appfabric.service"
        The same as Constants.AppFabricHealthCheck.APP_FABRIC_HEALTH_CHECK_SERVICE
    */
    int instanceNameLength = instanceName.length() + 1;
    String updatedServiceName = "";
    if (serviceName.lastIndexOf(instanceName) >= 0) {
        updatedServiceName = serviceName.substring(serviceName.lastIndexOf(instanceName) + instanceNameLength).replace("-", ".");
    } else {
        updatedServiceName = serviceName.replace("-", ".");
    }
    remoteClient = remoteClientFactory.createRemoteClient(updatedServiceName, new DefaultHttpRequestConfig(false), Constants.Gateway.API_VERSION_3);
    String url = String.format("health/{serviceName}", updatedServiceName);
    HttpRequest.Builder requestBuilder = remoteClient.requestBuilder(HttpMethod.GET, url);
    HttpResponse httpResponse;
    httpResponse = execute(requestBuilder.build());
    Optional<Map<String, Object>> healthData = GSON.fromJson(httpResponse.getResponseBodyAsString(), Optional.class);
    return healthData.orElseGet(HashMap::new);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HashMap(java.util.HashMap) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpResponse(io.cdap.common.http.HttpResponse) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with DefaultHttpRequestConfig

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

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

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

the class ArtifactCacheHttpHandlerInternal method fetchArtifact.

@GET
@Path("peers/{peer}/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}")
public void fetchArtifact(HttpRequest request, HttpResponder responder, @PathParam("peer") String peer, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion) throws Exception {
    ArtifactId artifactId = new ArtifactId(namespaceId, artifactName, artifactVersion);
    try {
        String endpoint = tetheringStore.getPeer(peer).getEndpoint();
        RemoteClientFactory factory = new RemoteClientFactory(new NoOpDiscoveryServiceClient(endpoint), new NoOpInternalAuthenticator(), remoteAuthenticator);
        HttpRequestConfig config = new DefaultHttpRequestConfig(true);
        RemoteClient remoteClient = factory.createRemoteClient("", config, Constants.Gateway.INTERNAL_API_VERSION_3);
        File artifactPath = cache.getArtifact(artifactId, peer, remoteClient);
        Location artifactLocation = Locations.toLocation(artifactPath);
        responder.sendContent(HttpResponseStatus.OK, new LocationBodyProducer(artifactLocation), new DefaultHttpHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM));
    } catch (Exception ex) {
        if (ex instanceof HttpErrorStatusProvider) {
            HttpResponseStatus status = HttpResponseStatus.valueOf(((HttpErrorStatusProvider) ex).getStatusCode());
            responder.sendString(status, exceptionToJson(ex));
        } else {
            responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, exceptionToJson(ex));
        }
    }
}
Also used : RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) NoOpInternalAuthenticator(io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator) HttpErrorStatusProvider(io.cdap.cdap.api.common.HttpErrorStatusProvider) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) LocationBodyProducer(io.cdap.cdap.common.http.LocationBodyProducer) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) RemoteClient(io.cdap.cdap.common.internal.remote.RemoteClient) File(java.io.File) Location(org.apache.twill.filesystem.Location) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 10 with DefaultHttpRequestConfig

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

the class OAuthServiceTest method makeGetCall.

private HttpResponse makeGetCall(String endpoint) throws IOException {
    URL url = serviceURI.resolve(String.format("v1/oauth/%s", endpoint)).toURL();
    HttpRequest request = HttpRequest.builder(HttpMethod.GET, url).build();
    return HttpRequests.execute(request, new DefaultHttpRequestConfig(false));
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) 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