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());
}
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);
}
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));
}
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));
}
}
}
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));
}
Aggregations