Search in sources :

Example 66 with StatusResponseHolder

use of org.apache.druid.java.util.http.client.response.StatusResponseHolder in project druid by druid-io.

the class DirectDruidClient method cancelQuery.

private void cancelQuery(Query<T> query, String cancelUrl) {
    Runnable cancelRunnable = () -> {
        try {
            Future<StatusResponseHolder> responseFuture = httpClient.go(new Request(HttpMethod.DELETE, new URL(cancelUrl)).setContent(objectMapper.writeValueAsBytes(query)).setHeader(HttpHeaders.Names.CONTENT_TYPE, isSmile ? SmileMediaTypes.APPLICATION_JACKSON_SMILE : MediaType.APPLICATION_JSON), StatusResponseHandler.getInstance(), Duration.standardSeconds(1));
            Runnable checkRunnable = () -> {
                try {
                    if (!responseFuture.isDone()) {
                        log.error("Error cancelling query[%s]", query);
                    }
                    StatusResponseHolder response = responseFuture.get();
                    if (response.getStatus().getCode() >= 500) {
                        log.error("Error cancelling query[%s]: queriable node returned status[%d] [%s].", query, response.getStatus().getCode(), response.getStatus().getReasonPhrase());
                    }
                } catch (ExecutionException | InterruptedException e) {
                    log.error(e, "Error cancelling query[%s]", query);
                }
            };
            queryCancellationExecutor.schedule(checkRunnable, 5, TimeUnit.SECONDS);
        } catch (IOException e) {
            log.error(e, "Error cancelling query[%s]", query);
        }
    };
    queryCancellationExecutor.submit(cancelRunnable);
}
Also used : Request(org.apache.druid.java.util.http.client.Request) Future(java.util.concurrent.Future) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL)

Example 67 with StatusResponseHolder

use of org.apache.druid.java.util.http.client.response.StatusResponseHolder in project druid by druid-io.

the class CommonCacheNotifier method start.

public void start() {
    exec.submit(() -> {
        while (!Thread.interrupted()) {
            try {
                LOG.debug(callerName + ":Waiting for cache update notification");
                Pair<String, byte[]> update = updateQueue.take();
                String authorizer = update.lhs;
                byte[] serializedMap = update.rhs;
                BasicAuthDBConfig authorizerConfig = itemConfigMap.get(update.lhs);
                if (!authorizerConfig.isEnableCacheNotifications()) {
                    continue;
                }
                LOG.debug(callerName + ":Sending cache update notifications");
                // Best effort, if a notification fails, the remote node will eventually poll to update its state
                // We wait for responses however, to avoid flooding remote nodes with notifications.
                List<ListenableFuture<StatusResponseHolder>> futures = sendUpdate(authorizer, serializedMap);
                try {
                    List<StatusResponseHolder> responses = Futures.allAsList(futures).get(authorizerConfig.getCacheNotificationTimeout(), TimeUnit.MILLISECONDS);
                    for (StatusResponseHolder response : responses) {
                        LOG.debug(callerName + ":Got status: " + response.getStatus());
                    }
                } catch (Exception e) {
                    LOG.makeAlert(e, callerName + ":Failed to get response for cache notification.").emit();
                }
                LOG.debug(callerName + ":Received responses for cache update notifications.");
            } catch (InterruptedException e) {
                LOG.noStackTrace().info(e, "%s: Interrupted while handling updates for cachedUserMaps.", callerName);
            } catch (Throwable t) {
                LOG.makeAlert(t, callerName + ":Error occured while handling updates for cachedUserMaps.").emit();
            }
        }
    });
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) MalformedURLException(java.net.MalformedURLException)

Example 68 with StatusResponseHolder

use of org.apache.druid.java.util.http.client.response.StatusResponseHolder in project druid by druid-io.

the class CommonCacheNotifier method sendUpdate.

private List<ListenableFuture<StatusResponseHolder>> sendUpdate(String updatedAuthenticatorPrefix, byte[] serializedEntity) {
    List<ListenableFuture<StatusResponseHolder>> futures = new ArrayList<>();
    for (NodeRole nodeRole : NODE_TYPES) {
        DruidNodeDiscovery nodeDiscovery = discoveryProvider.getForNodeRole(nodeRole);
        Collection<DiscoveryDruidNode> nodes = nodeDiscovery.getAllNodes();
        for (DiscoveryDruidNode node : nodes) {
            URL listenerURL = getListenerURL(node.getDruidNode(), StringUtils.format(baseUrl, StringUtils.urlEncode(updatedAuthenticatorPrefix)));
            // best effort, if this fails, remote node will poll and pick up the update eventually
            Request req = new Request(HttpMethod.POST, listenerURL);
            req.setContent(MediaType.APPLICATION_JSON, serializedEntity);
            BasicAuthDBConfig itemConfig = itemConfigMap.get(updatedAuthenticatorPrefix);
            ListenableFuture<StatusResponseHolder> future = httpClient.go(req, new ResponseHandler(), Duration.millis(itemConfig.getCacheNotificationTimeout()));
            futures.add(future);
        }
    }
    return futures;
}
Also used : HttpResponseHandler(org.apache.druid.java.util.http.client.response.HttpResponseHandler) DruidNodeDiscovery(org.apache.druid.discovery.DruidNodeDiscovery) ArrayList(java.util.ArrayList) Request(org.apache.druid.java.util.http.client.Request) URL(java.net.URL) NodeRole(org.apache.druid.discovery.NodeRole) DiscoveryDruidNode(org.apache.druid.discovery.DiscoveryDruidNode) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder)

Example 69 with StatusResponseHolder

use of org.apache.druid.java.util.http.client.response.StatusResponseHolder in project druid by druid-io.

the class WorkerHolder method assignTask.

public boolean assignTask(Task task) {
    if (disabled.get()) {
        log.info("Received task[%s] assignment on worker[%s] when worker is disabled.", task.getId(), worker.getHost());
        return false;
    }
    URL url = TaskRunnerUtils.makeWorkerURL(worker, "/druid-internal/v1/worker/assignTask");
    int numTries = config.getAssignRequestMaxRetries();
    try {
        return RetryUtils.retry(() -> {
            try {
                final StatusResponseHolder response = httpClient.go(new Request(HttpMethod.POST, url).addHeader(HttpHeaders.Names.CONTENT_TYPE, SmileMediaTypes.APPLICATION_JACKSON_SMILE).setContent(smileMapper.writeValueAsBytes(task)), StatusResponseHandler.getInstance(), config.getAssignRequestHttpTimeout().toStandardDuration()).get();
                if (response.getStatus().getCode() == 200) {
                    return true;
                } else {
                    throw new RE("Failed to assign task[%s] to worker[%s]. Response Code[%s] and Message[%s]. Retrying...", task.getId(), worker.getHost(), response.getStatus().getCode(), response.getContent());
                }
            } catch (ExecutionException ex) {
                throw new RE(ex, "Request to assign task[%s] to worker[%s] failed. Retrying...", task.getId(), worker.getHost());
            }
        }, e -> !(e instanceof InterruptedException), numTries);
    } catch (Exception ex) {
        log.info("Not sure whether task[%s] was successfully assigned to worker[%s].", task.getId(), worker.getHost());
        return true;
    }
}
Also used : RE(org.apache.druid.java.util.common.RE) Request(org.apache.druid.java.util.http.client.Request) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

StatusResponseHolder (org.apache.druid.java.util.http.client.response.StatusResponseHolder)69 URL (java.net.URL)50 Request (org.apache.druid.java.util.http.client.Request)42 ISE (org.apache.druid.java.util.common.ISE)33 ExecutionException (java.util.concurrent.ExecutionException)13 Test (org.junit.Test)12 Lifecycle (org.apache.druid.java.util.common.lifecycle.Lifecycle)10 Map (java.util.Map)8 ArrayList (java.util.ArrayList)6 TypeReference (com.fasterxml.jackson.core.type.TypeReference)5 IOException (java.io.IOException)5 RE (org.apache.druid.java.util.common.RE)5 ChannelException (org.jboss.netty.channel.ChannelException)5 List (java.util.List)4 ExecutorService (java.util.concurrent.ExecutorService)4 Future (java.util.concurrent.Future)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3