Search in sources :

Example 86 with Request

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

the class RemoteTaskRunner method streamTaskReports.

@Override
public Optional<ByteSource> streamTaskReports(final String taskId) {
    final ZkWorker zkWorker = findWorkerRunningTask(taskId);
    if (zkWorker == null) {
        // Worker is not running this task, it might be available in deep storage
        return Optional.absent();
    } else {
        TaskLocation taskLocation = runningTasks.get(taskId).getLocation();
        final URL url = TaskRunnerUtils.makeTaskLocationURL(taskLocation, "/druid/worker/v1/chat/%s/liveReports", taskId);
        return Optional.of(new ByteSource() {

            @Override
            public InputStream openStream() throws IOException {
                try {
                    return httpClient.go(new Request(HttpMethod.GET, url), new InputStreamResponseHandler()).get();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } catch (ExecutionException e) {
                    // Unwrap if possible
                    Throwables.propagateIfPossible(e.getCause(), IOException.class);
                    throw new RuntimeException(e);
                }
            }
        });
    }
}
Also used : InputStreamResponseHandler(org.apache.druid.java.util.http.client.response.InputStreamResponseHandler) InputStream(java.io.InputStream) Request(org.apache.druid.java.util.http.client.Request) ByteSource(com.google.common.io.ByteSource) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TaskLocation(org.apache.druid.indexer.TaskLocation) URL(java.net.URL)

Example 87 with Request

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

the class RemoteTaskRunner method shutdown.

/**
 * Finds the worker running the task and forwards the shutdown signal to the worker.
 *
 * @param taskId - task id to shutdown
 */
@Override
public void shutdown(final String taskId, String reason) {
    log.info("Shutdown [%s] because: [%s]", taskId, reason);
    if (!lifecycleLock.awaitStarted(1, TimeUnit.SECONDS)) {
        log.info("This TaskRunner is stopped or not yet started. Ignoring shutdown command for task: %s", taskId);
    } else if (pendingTasks.remove(taskId) != null) {
        pendingTaskPayloads.remove(taskId);
        log.info("Removed task from pending queue: %s", taskId);
    } else if (completeTasks.containsKey(taskId)) {
        cleanup(taskId);
    } else {
        final ZkWorker zkWorker = findWorkerRunningTask(taskId);
        if (zkWorker == null) {
            log.info("Can't shutdown! No worker running task %s", taskId);
            return;
        }
        URL url = null;
        try {
            url = TaskRunnerUtils.makeWorkerURL(zkWorker.getWorker(), "/druid/worker/v1/task/%s/shutdown", taskId);
            final StatusResponseHolder response = httpClient.go(new Request(HttpMethod.POST, url), StatusResponseHandler.getInstance(), shutdownTimeout).get();
            log.info("Sent shutdown message to worker: %s, status %s, response: %s", zkWorker.getWorker().getHost(), response.getStatus(), response.getContent());
            if (!HttpResponseStatus.OK.equals(response.getStatus())) {
                log.error("Shutdown failed for %s! Are you sure the task was running?", taskId);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new RE(e, "Interrupted posting shutdown to [%s] for task [%s]", url, taskId);
        } catch (Exception e) {
            throw new RE(e, "Error in handling post to [%s] for task [%s]", zkWorker.getWorker().getHost(), taskId);
        }
    }
}
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) URL(java.net.URL) TimeoutException(java.util.concurrent.TimeoutException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 88 with Request

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

the class WorkerTaskRunnerQueryAdapter method sendRequestToWorker.

private void sendRequestToWorker(String workerHost, WorkerTaskRunner.ActionType action) {
    WorkerTaskRunner workerTaskRunner = getWorkerTaskRunner();
    if (workerTaskRunner == null) {
        throw new RE("Task Runner does not support enable/disable worker actions");
    }
    Optional<ImmutableWorkerInfo> workerInfo = Iterables.tryFind(workerTaskRunner.getWorkers(), entry -> entry.getWorker().getHost().equals(workerHost));
    if (!workerInfo.isPresent()) {
        throw new RE("Worker on host %s does not exists", workerHost);
    }
    String actionName = WorkerTaskRunner.ActionType.ENABLE.equals(action) ? "enable" : "disable";
    final URL workerUrl = TaskRunnerUtils.makeWorkerURL(workerInfo.get().getWorker(), "/druid/worker/v1/%s", actionName);
    try {
        final StatusResponseHolder response = httpClient.go(new Request(HttpMethod.POST, workerUrl), StatusResponseHandler.getInstance()).get();
        log.info("Sent %s action request to worker: %s, status: %s, response: %s", action, workerHost, response.getStatus(), response.getContent());
        if (!HttpResponseStatus.OK.equals(response.getStatus())) {
            throw new RE("Action [%s] failed for worker [%s] with status %s(%s)", action, workerHost, response.getStatus().getCode(), response.getStatus().getReasonPhrase());
        }
    } catch (ExecutionException | InterruptedException | TimeoutException e) {
        Throwables.propagate(e);
    }
}
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) TimeoutException(io.netty.handler.timeout.TimeoutException)

Example 89 with Request

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

the class KinesisIndexTaskClientTest method testPauseWithSubsequentGetOffsets.

@Test
public void testPauseWithSubsequentGetOffsets() throws Exception {
    Capture<Request> captured = Capture.newInstance();
    Capture<Request> captured2 = Capture.newInstance();
    Capture<Request> captured3 = Capture.newInstance();
    EasyMock.expect(responseHolder.getStatus()).andReturn(HttpResponseStatus.ACCEPTED);
    EasyMock.expect(responseHolder.getContent()).andReturn("\"PAUSED\"").times(2).andReturn("{\"0\":1, \"1\":10}").anyTimes();
    EasyMock.expect(httpClient.go(EasyMock.capture(captured), EasyMock.anyObject(ObjectOrErrorResponseHandler.class), EasyMock.eq(TEST_HTTP_TIMEOUT))).andReturn(okResponseHolder());
    EasyMock.expect(httpClient.go(EasyMock.capture(captured2), EasyMock.anyObject(ObjectOrErrorResponseHandler.class), EasyMock.eq(TEST_HTTP_TIMEOUT))).andReturn(okResponseHolder());
    EasyMock.expect(httpClient.go(EasyMock.capture(captured3), EasyMock.anyObject(ObjectOrErrorResponseHandler.class), EasyMock.eq(TEST_HTTP_TIMEOUT))).andReturn(okResponseHolder());
    replayAll();
    Map<String, String> results = client.pause(TEST_ID);
    verifyAll();
    Request request = captured.getValue();
    Assert.assertEquals(HttpMethod.POST, request.getMethod());
    Assert.assertEquals(new URL("http://test-host:1234/druid/worker/v1/chat/test-id/pause"), request.getUrl());
    Assert.assertTrue(request.getHeaders().get("X-Druid-Task-Id").contains("test-id"));
    request = captured2.getValue();
    Assert.assertEquals(HttpMethod.GET, request.getMethod());
    Assert.assertEquals(new URL("http://test-host:1234/druid/worker/v1/chat/test-id/status"), request.getUrl());
    request = captured3.getValue();
    Assert.assertEquals(HttpMethod.GET, request.getMethod());
    Assert.assertEquals(new URL("http://test-host:1234/druid/worker/v1/chat/test-id/offsets/current"), request.getUrl());
    Assert.assertEquals(2, results.size());
    Assert.assertEquals("1", results.get("0"));
    Assert.assertEquals("10", results.get("1"));
}
Also used : Request(org.apache.druid.java.util.http.client.Request) ObjectOrErrorResponseHandler(org.apache.druid.java.util.http.client.response.ObjectOrErrorResponseHandler) URL(java.net.URL) Test(org.junit.Test)

Example 90 with Request

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

the class KinesisIndexTaskClientTest method testGetCurrentOffsetsAsync.

@Test
public void testGetCurrentOffsetsAsync() throws Exception {
    final int numRequests = TEST_IDS.size();
    Capture<Request> captured = Capture.newInstance(CaptureType.ALL);
    EasyMock.expect(responseHolder.getStatus()).andReturn(HttpResponseStatus.OK).anyTimes();
    EasyMock.expect(responseHolder.getContent()).andReturn("{\"0\":\"1\"}").anyTimes();
    EasyMock.expect(httpClient.go(EasyMock.capture(captured), EasyMock.anyObject(ObjectOrErrorResponseHandler.class), EasyMock.eq(TEST_HTTP_TIMEOUT))).andReturn(okResponseHolder()).times(numRequests);
    replayAll();
    List<URL> expectedUrls = new ArrayList<>();
    List<ListenableFuture<Map<String, String>>> futures = new ArrayList<>();
    for (String testId : TEST_IDS) {
        expectedUrls.add(new URL(StringUtils.format(URL_FORMATTER, TEST_HOST, TEST_PORT, testId, "offsets/current")));
        futures.add(client.getCurrentOffsetsAsync(testId, false));
    }
    List<Map<String, String>> responses = Futures.allAsList(futures).get();
    verifyAll();
    List<Request> requests = captured.getValues();
    Assert.assertEquals(numRequests, requests.size());
    Assert.assertEquals(numRequests, responses.size());
    for (int i = 0; i < numRequests; i++) {
        Assert.assertEquals(HttpMethod.GET, requests.get(i).getMethod());
        Assert.assertTrue("unexpectedURL", expectedUrls.contains(requests.get(i).getUrl()));
        Assert.assertEquals(Maps.newLinkedHashMap(ImmutableMap.of("0", "1")), responses.get(i));
    }
}
Also used : Request(org.apache.druid.java.util.http.client.Request) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) URL(java.net.URL) Test(org.junit.Test)

Aggregations

Request (org.apache.druid.java.util.http.client.Request)148 URL (java.net.URL)129 Test (org.junit.Test)84 StatusResponseHolder (org.apache.druid.java.util.http.client.response.StatusResponseHolder)42 ISE (org.apache.druid.java.util.common.ISE)29 StringFullResponseHolder (org.apache.druid.java.util.http.client.response.StringFullResponseHolder)28 ObjectOrErrorResponseHandler (org.apache.druid.java.util.http.client.response.ObjectOrErrorResponseHandler)24 IOException (java.io.IOException)23 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)22 ArrayList (java.util.ArrayList)22 HashMap (java.util.HashMap)14 Map (java.util.Map)14 ExecutionException (java.util.concurrent.ExecutionException)14 InputStream (java.io.InputStream)12 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)11 RE (org.apache.druid.java.util.common.RE)10 InputStreamResponseHandler (org.apache.druid.java.util.http.client.response.InputStreamResponseHandler)10 BigEndianHeapChannelBuffer (org.jboss.netty.buffer.BigEndianHeapChannelBuffer)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 HttpResponseHandler (org.apache.druid.java.util.http.client.response.HttpResponseHandler)8