Search in sources :

Example 46 with StringFullResponseHolder

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);
    }
}
Also used : StringFullResponseHolder(org.apache.druid.java.util.http.client.response.StringFullResponseHolder) ISE(org.apache.druid.java.util.common.ISE) IOException(java.io.IOException)

Example 47 with StringFullResponseHolder

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);
    }
}
Also used : StringFullResponseHolder(org.apache.druid.java.util.http.client.response.StringFullResponseHolder) ISE(org.apache.druid.java.util.common.ISE) IOException(java.io.IOException)

Example 48 with StringFullResponseHolder

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);
    }
}
Also used : StringFullResponseHolder(org.apache.druid.java.util.http.client.response.StringFullResponseHolder) ISE(org.apache.druid.java.util.common.ISE) IOException(java.io.IOException)

Example 49 with StringFullResponseHolder

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);
}
Also used : StringFullResponseHolder(org.apache.druid.java.util.http.client.response.StringFullResponseHolder) Request(org.apache.druid.java.util.http.client.Request) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) BigEndianHeapChannelBuffer(org.jboss.netty.buffer.BigEndianHeapChannelBuffer) URL(java.net.URL) Test(org.junit.Test)

Example 50 with StringFullResponseHolder

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);
}
Also used : StringFullResponseHolder(org.apache.druid.java.util.http.client.response.StringFullResponseHolder) Request(org.apache.druid.java.util.http.client.Request) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) BigEndianHeapChannelBuffer(org.jboss.netty.buffer.BigEndianHeapChannelBuffer) URL(java.net.URL) Test(org.junit.Test)

Aggregations

StringFullResponseHolder (org.apache.druid.java.util.http.client.response.StringFullResponseHolder)86 Test (org.junit.Test)56 URL (java.net.URL)50 Request (org.apache.druid.java.util.http.client.Request)49 HashMap (java.util.HashMap)32 IOException (java.io.IOException)31 BigEndianHeapChannelBuffer (org.jboss.netty.buffer.BigEndianHeapChannelBuffer)18 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)18 ISE (org.apache.druid.java.util.common.ISE)16 HttpClient (org.apache.druid.java.util.http.client.HttpClient)12 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)12 ArrayList (java.util.ArrayList)8 List (java.util.List)8 ChannelException (org.jboss.netty.channel.ChannelException)8 MalformedURLException (java.net.MalformedURLException)7 ExecutionException (java.util.concurrent.ExecutionException)7 HttpResponseStatus (org.jboss.netty.handler.codec.http.HttpResponseStatus)6 Duration (org.joda.time.Duration)6 Interval (org.joda.time.Interval)6 TypeReference (com.fasterxml.jackson.core.type.TypeReference)4