Search in sources :

Example 16 with StringFullResponseHolder

use of org.apache.druid.java.util.http.client.response.StringFullResponseHolder in project hive by apache.

the class DruidStorageHandlerUtils method getResponseFromCurrentLeader.

static StringFullResponseHolder getResponseFromCurrentLeader(HttpClient client, Request request, StringFullResponseHandler fullResponseHandler) throws ExecutionException, InterruptedException {
    StringFullResponseHolder responseHolder = client.go(request, fullResponseHandler).get();
    if (HttpResponseStatus.TEMPORARY_REDIRECT.equals(responseHolder.getStatus())) {
        String redirectUrlStr = responseHolder.getResponse().headers().get("Location");
        LOG.debug("Request[%s] received redirect response to location [%s].", request.getUrl(), redirectUrlStr);
        final URL redirectUrl;
        try {
            redirectUrl = new URL(redirectUrlStr);
        } catch (MalformedURLException ex) {
            throw new ExecutionException(String.format("Malformed redirect location is found in response from url[%s], new location[%s].", request.getUrl(), redirectUrlStr), ex);
        }
        responseHolder = client.go(withUrl(request, redirectUrl), fullResponseHandler).get();
    }
    return responseHolder;
}
Also used : StringFullResponseHolder(org.apache.druid.java.util.http.client.response.StringFullResponseHolder) MalformedURLException(java.net.MalformedURLException) GenericUDFToString(org.apache.hadoop.hive.ql.udf.generic.GenericUDFToString) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL)

Example 17 with StringFullResponseHolder

use of org.apache.druid.java.util.http.client.response.StringFullResponseHolder in project hive by apache.

the class DruidKafkaUtils method updateKafkaIngestionSpec.

static void updateKafkaIngestionSpec(String overlordAddress, KafkaSupervisorSpec spec) {
    try {
        String task = JSON_MAPPER.writeValueAsString(spec);
        CONSOLE.printInfo("submitting kafka Spec {}", task);
        LOG.info("submitting kafka Supervisor Spec {}", task);
        StringFullResponseHolder response = DruidStorageHandlerUtils.getResponseFromCurrentLeader(DruidStorageHandler.getHttpClient(), new Request(HttpMethod.POST, new URL(String.format("http://%s/druid/indexer/v1/supervisor", overlordAddress))).setContent("application/json", JSON_MAPPER.writeValueAsBytes(spec)), new StringFullResponseHandler(Charset.forName("UTF-8")));
        if (response.getStatus().equals(HttpResponseStatus.OK)) {
            String msg = String.format("Kafka Supervisor for [%s] Submitted Successfully to druid.", spec.getDataSchema().getDataSource());
            LOG.info(msg);
            CONSOLE.printInfo(msg);
        } else {
            throw new IOException(String.format("Unable to update Kafka Ingestion for Druid status [%d] full response [%s]", response.getStatus().getCode(), response.getContent()));
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : StringFullResponseHolder(org.apache.druid.java.util.http.client.response.StringFullResponseHolder) StringFullResponseHandler(org.apache.druid.java.util.http.client.response.StringFullResponseHandler) Request(org.apache.druid.java.util.http.client.Request) IOException(java.io.IOException) URL(java.net.URL) IOException(java.io.IOException)

Example 18 with StringFullResponseHolder

use of org.apache.druid.java.util.http.client.response.StringFullResponseHolder in project druid by apache.

the class CoordinatorRuleManagerTest method mockClient.

private DruidLeaderClient mockClient() {
    final Map<String, List<Rule>> rules = ImmutableMap.of(DATASOURCE1, ImmutableList.of(new ForeverLoadRule(null)), DATASOURCE2, ImmutableList.of(new ForeverLoadRule(null), new IntervalDropRule(Intervals.of("2020-01-01/2020-01-02"))), "datasource3", ImmutableList.of(new PeriodLoadRule(new Period("P1M"), true, null), new ForeverDropRule()), TieredBrokerConfig.DEFAULT_RULE_NAME, ImmutableList.of(new ForeverLoadRule(ImmutableMap.of("__default", 2))));
    final StringFullResponseHolder holder = EasyMock.niceMock(StringFullResponseHolder.class);
    EasyMock.expect(holder.getStatus()).andReturn(HttpResponseStatus.OK);
    try {
        EasyMock.expect(holder.getContent()).andReturn(objectMapper.writeValueAsString(rules));
        final DruidLeaderClient client = EasyMock.niceMock(DruidLeaderClient.class);
        EasyMock.expect(client.go(EasyMock.anyObject())).andReturn(holder);
        EasyMock.replay(holder, client);
        return client;
    } catch (IOException | InterruptedException e) {
        throw new RuntimeException(e);
    }
}
Also used : Period(org.joda.time.Period) IntervalDropRule(org.apache.druid.server.coordinator.rules.IntervalDropRule) DruidLeaderClient(org.apache.druid.discovery.DruidLeaderClient) IOException(java.io.IOException) StringFullResponseHolder(org.apache.druid.java.util.http.client.response.StringFullResponseHolder) ForeverDropRule(org.apache.druid.server.coordinator.rules.ForeverDropRule) ForeverLoadRule(org.apache.druid.server.coordinator.rules.ForeverLoadRule) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) PeriodLoadRule(org.apache.druid.server.coordinator.rules.PeriodLoadRule)

Example 19 with StringFullResponseHolder

use of org.apache.druid.java.util.http.client.response.StringFullResponseHolder in project druid by apache.

the class HttpIndexingServiceClient method getTotalWorkerCapacity.

@Override
public int getTotalWorkerCapacity() {
    try {
        final StringFullResponseHolder response = druidLeaderClient.go(druidLeaderClient.makeRequest(HttpMethod.GET, "/druid/indexer/v1/workers").setHeader("Content-Type", MediaType.APPLICATION_JSON));
        if (!response.getStatus().equals(HttpResponseStatus.OK)) {
            throw new ISE("Error while getting available cluster capacity. status[%s] content[%s]", response.getStatus(), response.getContent());
        }
        final Collection<IndexingWorkerInfo> workers = jsonMapper.readValue(response.getContent(), new TypeReference<Collection<IndexingWorkerInfo>>() {
        });
        return workers.stream().mapToInt(workerInfo -> workerInfo.getWorker().getCapacity()).sum();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Logger(org.apache.druid.java.util.common.logger.Logger) Iterables(com.google.common.collect.Iterables) Inject(com.google.inject.Inject) HttpMethod(org.jboss.netty.handler.codec.http.HttpMethod) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) Interval(org.joda.time.Interval) MediaType(javax.ws.rs.core.MediaType) IdUtils(org.apache.druid.common.utils.IdUtils) Map(java.util.Map) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Nullable(javax.annotation.Nullable) DateTimes(org.apache.druid.java.util.common.DateTimes) JacksonUtils(org.apache.druid.java.util.common.jackson.JacksonUtils) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) StringUtils(org.apache.druid.java.util.common.StringUtils) Set(java.util.Set) ISE(org.apache.druid.java.util.common.ISE) IOException(java.io.IOException) TaskStatusPlus(org.apache.druid.indexer.TaskStatusPlus) HttpResponseStatus(org.jboss.netty.handler.codec.http.HttpResponseStatus) List(java.util.List) DataSegment(org.apache.druid.timeline.DataSegment) Preconditions(com.google.common.base.Preconditions) StringFullResponseHolder(org.apache.druid.java.util.http.client.response.StringFullResponseHolder) Collections(java.util.Collections) DruidLeaderClient(org.apache.druid.discovery.DruidLeaderClient) StringFullResponseHolder(org.apache.druid.java.util.http.client.response.StringFullResponseHolder) Collection(java.util.Collection) ISE(org.apache.druid.java.util.common.ISE) IOException(java.io.IOException)

Example 20 with StringFullResponseHolder

use of org.apache.druid.java.util.http.client.response.StringFullResponseHolder in project druid by apache.

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)

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