use of org.apache.druid.java.util.http.client.response.StringFullResponseHolder in project druid by druid-io.
the class HttpIndexingServiceClient method getTotalWorkerCapacityWithAutoScale.
@Override
public int getTotalWorkerCapacityWithAutoScale() {
try {
final StringFullResponseHolder response = druidLeaderClient.go(druidLeaderClient.makeRequest(HttpMethod.GET, "/druid/indexer/v1/totalWorkerCapacity").setHeader("Content-Type", MediaType.APPLICATION_JSON));
if (!response.getStatus().equals(HttpResponseStatus.OK)) {
throw new ISE("Error while getting total worker capacity. status[%s] content[%s]", response.getStatus(), response.getContent());
}
final IndexingTotalWorkerCapacityInfo indexingTotalWorkerCapacityInfo = jsonMapper.readValue(response.getContent(), new TypeReference<IndexingTotalWorkerCapacityInfo>() {
});
return indexingTotalWorkerCapacityInfo.getMaximumCapacityWithAutoScale();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.druid.java.util.http.client.response.StringFullResponseHolder in project druid by druid-io.
the class HttpIndexingServiceClient method runTask.
@Override
public String runTask(String taskId, Object taskObject) {
try {
// Warning, magic: here we may serialize ClientTaskQuery objects, but OverlordResource.taskPost() deserializes
// Task objects from the same data. See the comment for ClientTaskQuery for details.
final StringFullResponseHolder response = druidLeaderClient.go(druidLeaderClient.makeRequest(HttpMethod.POST, "/druid/indexer/v1/task").setContent(MediaType.APPLICATION_JSON, jsonMapper.writeValueAsBytes(taskObject)));
if (!response.getStatus().equals(HttpResponseStatus.OK)) {
if (!Strings.isNullOrEmpty(response.getContent())) {
throw new ISE("Failed to post task[%s] with error[%s].", taskId, response.getContent());
} else {
throw new ISE("Failed to post task[%s]. Please check overlord log", taskId);
}
}
final Map<String, Object> resultMap = jsonMapper.readValue(response.getContent(), JacksonUtils.TYPE_REFERENCE_MAP_STRING_OBJECT);
final String returnedTaskId = (String) resultMap.get("task");
Preconditions.checkState(taskId.equals(returnedTaskId), "Got a different taskId[%s]. Expected taskId[%s]", returnedTaskId, taskId);
return taskId;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.druid.java.util.http.client.response.StringFullResponseHolder in project druid by druid-io.
the class HttpIndexingServiceClient method cancelTask.
@Override
public String cancelTask(String taskId) {
try {
final StringFullResponseHolder response = druidLeaderClient.go(druidLeaderClient.makeRequest(HttpMethod.POST, StringUtils.format("/druid/indexer/v1/task/%s/shutdown", StringUtils.urlEncode(taskId))));
if (!response.getStatus().equals(HttpResponseStatus.OK)) {
throw new ISE("Failed to cancel task[%s]", taskId);
}
final Map<String, Object> resultMap = jsonMapper.readValue(response.getContent(), JacksonUtils.TYPE_REFERENCE_MAP_STRING_OBJECT);
final String cancelledTaskId = (String) resultMap.get("task");
Preconditions.checkNotNull(cancelledTaskId, "Null task id returned for task[%s]", taskId);
Preconditions.checkState(taskId.equals(cancelledTaskId), "Requested to cancel task[%s], but another task[%s] was cancelled!", taskId, cancelledTaskId);
return cancelledTaskId;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.druid.java.util.http.client.response.StringFullResponseHolder in project druid by druid-io.
the class HttpIndexingServiceClientTest method testSample.
@Test
public void testSample() throws Exception {
final SamplerResponse samplerResponse = new SamplerResponse(2, 2, ImmutableList.of(new SamplerResponse.SamplerResponseRow(ImmutableMap.of("time", "2020-01-01", "x", "123", "y", "456"), ImmutableMap.of("time", "2020-01-01", "x", "123", "y", "456"), false, null)));
final SamplerSpec samplerSpec = new SamplerSpec() {
@Override
public SamplerResponse sample() {
return samplerResponse;
}
};
HttpResponse response = EasyMock.createMock(HttpResponse.class);
EasyMock.expect(response.getStatus()).andReturn(HttpResponseStatus.OK).anyTimes();
EasyMock.expect(response.getContent()).andReturn(new BigEndianHeapChannelBuffer(0));
EasyMock.replay(response);
StringFullResponseHolder responseHolder = new StringFullResponseHolder(response, StandardCharsets.UTF_8).addChunk(jsonMapper.writeValueAsString(samplerResponse));
EasyMock.expect(druidLeaderClient.go(EasyMock.anyObject(Request.class))).andReturn(responseHolder).anyTimes();
EasyMock.expect(druidLeaderClient.makeRequest(HttpMethod.POST, "/druid/indexer/v1/sampler")).andReturn(new Request(HttpMethod.POST, new URL("http://localhost:8090/druid/indexer/v1/sampler"))).anyTimes();
EasyMock.replay(druidLeaderClient);
final SamplerResponse actualResponse = httpIndexingServiceClient.sample(samplerSpec);
Assert.assertEquals(samplerResponse, actualResponse);
EasyMock.verify(druidLeaderClient, response);
}
use of org.apache.druid.java.util.http.client.response.StringFullResponseHolder in project druid by druid-io.
the class HttpIndexingServiceClientTest method testGetTaskReportEmpty.
@Test
public void testGetTaskReportEmpty() throws Exception {
String taskId = "testTaskId";
HttpResponse response = EasyMock.createMock(HttpResponse.class);
EasyMock.expect(response.getStatus()).andReturn(HttpResponseStatus.OK).anyTimes();
EasyMock.expect(response.getContent()).andReturn(new BigEndianHeapChannelBuffer(0));
EasyMock.replay(response);
StringFullResponseHolder responseHolder = new StringFullResponseHolder(response, StandardCharsets.UTF_8).addChunk("");
EasyMock.expect(druidLeaderClient.go(EasyMock.anyObject(Request.class))).andReturn(responseHolder).anyTimes();
EasyMock.expect(druidLeaderClient.makeRequest(HttpMethod.GET, "/druid/indexer/v1/task/testTaskId/reports")).andReturn(new Request(HttpMethod.GET, new URL("http://localhost:8090/druid/indexer/v1/task/testTaskId/reports"))).anyTimes();
EasyMock.replay(druidLeaderClient);
final Map<String, Object> actualResponse = httpIndexingServiceClient.getTaskReport(taskId);
Assert.assertNull(actualResponse);
EasyMock.verify(druidLeaderClient, response);
}
Aggregations