Search in sources :

Example 6 with StatusResponseHolder

use of com.metamx.http.client.response.StatusResponseHolder in project druid by druid-io.

the class EventReceiverFirehoseTestClient method postEvents.

/**
   * post events from the collection and return the count of events accepted
   *
   * @param events Collection of events to be posted
   *
   * @return
   */
public int postEvents(Collection<Map<String, Object>> events, ObjectMapper objectMapper, String mediaType) {
    try {
        StatusResponseHolder response = httpClient.go(new Request(HttpMethod.POST, new URL(getURL())).setContent(mediaType, objectMapper.writeValueAsBytes(events)), responseHandler).get();
        if (!response.getStatus().equals(HttpResponseStatus.OK)) {
            throw new ISE("Error while posting events to url[%s] status[%s] content[%s]", getURL(), response.getStatus(), response.getContent());
        }
        Map<String, Integer> responseData = objectMapper.readValue(response.getContent(), new TypeReference<Map<String, Integer>>() {
        });
        return responseData.get("eventCount");
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Request(com.metamx.http.client.Request) StatusResponseHolder(com.metamx.http.client.response.StatusResponseHolder) ISE(io.druid.java.util.common.ISE) Map(java.util.Map) URL(java.net.URL)

Example 7 with StatusResponseHolder

use of com.metamx.http.client.response.StatusResponseHolder in project druid by druid-io.

the class OverlordResourceTestClient method submitSupervisor.

public String submitSupervisor(String spec) {
    try {
        StatusResponseHolder response = httpClient.go(new Request(HttpMethod.POST, new URL(getIndexerURL() + "supervisor")).setContent("application/json", spec.getBytes()), responseHandler).get();
        if (!response.getStatus().equals(HttpResponseStatus.OK)) {
            throw new ISE("Error while submitting supervisor to overlord, response [%s %s]", response.getStatus(), response.getContent());
        }
        Map<String, String> responseData = jsonMapper.readValue(response.getContent(), new TypeReference<Map<String, String>>() {
        });
        String id = responseData.get("id");
        LOG.info("Submitted supervisor with id[%s]", id);
        return id;
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Request(com.metamx.http.client.Request) StatusResponseHolder(com.metamx.http.client.response.StatusResponseHolder) ISE(io.druid.java.util.common.ISE) Map(java.util.Map) URL(java.net.URL)

Example 8 with StatusResponseHolder

use of com.metamx.http.client.response.StatusResponseHolder in project druid by druid-io.

the class OverlordResourceTestClient method shutdownSupervisor.

public void shutdownSupervisor(String id) {
    try {
        StatusResponseHolder response = httpClient.go(new Request(HttpMethod.POST, new URL(String.format("%ssupervisor/%s/shutdown", getIndexerURL(), id))), responseHandler).get();
        if (!response.getStatus().equals(HttpResponseStatus.OK)) {
            throw new ISE("Error while shutting down supervisor, response [%s %s]", response.getStatus(), response.getContent());
        }
        LOG.info("Shutdown supervisor with id[%s]", id);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Request(com.metamx.http.client.Request) StatusResponseHolder(com.metamx.http.client.response.StatusResponseHolder) ISE(io.druid.java.util.common.ISE) URL(java.net.URL)

Example 9 with StatusResponseHolder

use of com.metamx.http.client.response.StatusResponseHolder in project druid by druid-io.

the class OverlordResourceTestClient method getTaskStatus.

public TaskStatus.Status getTaskStatus(String taskID) {
    try {
        StatusResponseHolder response = makeRequest(HttpMethod.GET, String.format("%stask/%s/status", getIndexerURL(), URLEncoder.encode(taskID, "UTF-8")));
        LOG.info("Index status response" + response.getContent());
        Map<String, Object> responseData = jsonMapper.readValue(response.getContent(), new TypeReference<Map<String, Object>>() {
        });
        //TODO: figure out a better way to parse the response...
        String status = (String) ((Map) responseData.get("status")).get("status");
        return TaskStatus.Status.valueOf(status);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : StatusResponseHolder(com.metamx.http.client.response.StatusResponseHolder) Map(java.util.Map)

Example 10 with StatusResponseHolder

use of com.metamx.http.client.response.StatusResponseHolder in project druid by druid-io.

the class RemoteTaskActionClient method submit.

@Override
public <RetType> RetType submit(TaskAction<RetType> taskAction) throws IOException {
    log.info("Performing action for task[%s]: %s", task.getId(), taskAction);
    byte[] dataToSend = jsonMapper.writeValueAsBytes(new TaskActionHolder(task, taskAction));
    final RetryPolicy retryPolicy = retryPolicyFactory.makeRetryPolicy();
    while (true) {
        try {
            final Server server;
            final URI serviceUri;
            try {
                server = getServiceInstance();
                serviceUri = makeServiceUri(server);
            } catch (Exception e) {
                // Want to retry, so throw an IOException.
                throw new IOException("Failed to locate service uri", e);
            }
            final StatusResponseHolder response;
            log.info("Submitting action for task[%s] to overlord[%s]: %s", task.getId(), serviceUri, taskAction);
            try {
                response = httpClient.go(new Request(HttpMethod.POST, serviceUri.toURL()).setContent(MediaType.APPLICATION_JSON, dataToSend), new StatusResponseHandler(Charsets.UTF_8)).get();
            } catch (Exception e) {
                Throwables.propagateIfInstanceOf(e.getCause(), IOException.class);
                Throwables.propagateIfInstanceOf(e.getCause(), ChannelException.class);
                throw Throwables.propagate(e);
            }
            if (response.getStatus().getCode() / 100 == 2) {
                final Map<String, Object> responseDict = jsonMapper.readValue(response.getContent(), new TypeReference<Map<String, Object>>() {
                });
                return jsonMapper.convertValue(responseDict.get("result"), taskAction.getReturnTypeReference());
            } else {
                // Want to retry, so throw an IOException.
                throw new IOException(String.format("Scary HTTP status returned: %s. Check your overlord[%s] logs for exceptions.", response.getStatus(), server.getHost()));
            }
        } catch (IOException | ChannelException e) {
            log.warn(e, "Exception submitting action for task[%s]", task.getId());
            final Duration delay = retryPolicy.getAndIncrementRetryDelay();
            if (delay == null) {
                throw e;
            } else {
                try {
                    final long sleepTime = jitter(delay.getMillis());
                    log.info("Will try again in [%s].", new Duration(sleepTime).toString());
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e2) {
                    throw Throwables.propagate(e2);
                }
            }
        }
    }
}
Also used : Server(io.druid.client.selector.Server) Request(com.metamx.http.client.Request) Duration(org.joda.time.Duration) IOException(java.io.IOException) URI(java.net.URI) ChannelException(org.jboss.netty.channel.ChannelException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) StatusResponseHolder(com.metamx.http.client.response.StatusResponseHolder) StatusResponseHandler(com.metamx.http.client.response.StatusResponseHandler) RetryPolicy(io.druid.indexing.common.RetryPolicy) Map(java.util.Map) ChannelException(org.jboss.netty.channel.ChannelException)

Aggregations

StatusResponseHolder (com.metamx.http.client.response.StatusResponseHolder)15 Request (com.metamx.http.client.Request)10 URL (java.net.URL)6 StatusResponseHandler (com.metamx.http.client.response.StatusResponseHandler)5 IOException (java.io.IOException)5 Map (java.util.Map)5 Test (org.junit.Test)4 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 ISE (io.druid.java.util.common.ISE)3 HttpResponseHandler (com.metamx.http.client.response.HttpResponseHandler)2 RetryPolicyFactory (io.druid.indexing.common.RetryPolicyFactory)2 NoopTask (io.druid.indexing.common.task.NoopTask)2 Task (io.druid.indexing.common.task.Task)2 RE (io.druid.java.util.common.RE)2 QueryInterruptedException (io.druid.query.QueryInterruptedException)2 HashMap (java.util.HashMap)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 EasyMock.anyObject (org.easymock.EasyMock.anyObject)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)1