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);
}
}
});
}
}
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);
}
}
}
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);
}
}
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"));
}
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));
}
}
Aggregations