Search in sources :

Example 1 with InputStreamResponseHandler

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

the class JettyTest method testNumConnectionsMetricHttp.

@Test
public void testNumConnectionsMetricHttp() throws Exception {
    String text = "hello";
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(out)) {
        gzipOutputStream.write(text.getBytes(Charset.defaultCharset()));
    }
    Request request = new Request(HttpMethod.GET, new URL("http://localhost:" + port + "/latched/hello"));
    request.setHeader("Content-Encoding", "gzip");
    request.setContent(MediaType.TEXT_PLAIN, out.toByteArray());
    JettyServerModule jsm = injector.getInstance(JettyServerModule.class);
    latchedRequestState.reset();
    waitForJettyServerModuleActiveConnectionsZero(jsm);
    Assert.assertEquals(0, jsm.getActiveConnections());
    ListenableFuture<InputStream> go = client.go(request, new InputStreamResponseHandler());
    latchedRequestState.clientWaitForServerToStartRequest();
    Assert.assertEquals(1, jsm.getActiveConnections());
    latchedRequestState.clientReadyToFinishRequest();
    go.get();
    waitForJettyServerModuleActiveConnectionsZero(jsm);
    Assert.assertEquals(0, jsm.getActiveConnections());
}
Also used : InputStreamResponseHandler(org.apache.druid.java.util.http.client.response.InputStreamResponseHandler) GZIPOutputStream(java.util.zip.GZIPOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) Request(org.apache.druid.java.util.http.client.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JettyServerModule(org.apache.druid.server.initialization.jetty.JettyServerModule) URL(java.net.URL) Test(org.junit.Test)

Example 2 with InputStreamResponseHandler

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

the class HttpRemoteTaskRunner method streamTaskReports.

@Override
public Optional<ByteSource> streamTaskReports(String taskId) {
    // Read on tasks is safe
    @SuppressWarnings("GuardedBy") HttpRemoteTaskRunnerWorkItem taskRunnerWorkItem = tasks.get(taskId);
    Worker worker = null;
    if (taskRunnerWorkItem != null && taskRunnerWorkItem.getState() != HttpRemoteTaskRunnerWorkItem.State.COMPLETE) {
        worker = taskRunnerWorkItem.getWorker();
    }
    if (worker == null || !workers.containsKey(worker.getHost())) {
        // Worker is not running this task, it might be available in deep storage
        return Optional.absent();
    } else {
        // Worker is still running this task
        TaskLocation taskLocation = taskRunnerWorkItem.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 : InputStream(java.io.InputStream) Request(org.apache.druid.java.util.http.client.Request) IOException(java.io.IOException) TaskLocation(org.apache.druid.indexer.TaskLocation) URL(java.net.URL) InputStreamResponseHandler(org.apache.druid.java.util.http.client.response.InputStreamResponseHandler) Worker(org.apache.druid.indexing.worker.Worker) ByteSource(com.google.common.io.ByteSource) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with InputStreamResponseHandler

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

the class HttpRemoteTaskRunner method streamTaskLog.

@Override
public Optional<ByteSource> streamTaskLog(String taskId, long offset) {
    // Read on tasks is safe
    @SuppressWarnings("GuardedBy") HttpRemoteTaskRunnerWorkItem taskRunnerWorkItem = tasks.get(taskId);
    Worker worker = null;
    if (taskRunnerWorkItem != null && taskRunnerWorkItem.getState() != HttpRemoteTaskRunnerWorkItem.State.COMPLETE) {
        worker = taskRunnerWorkItem.getWorker();
    }
    if (worker == null || !workers.containsKey(worker.getHost())) {
        // Worker is not running this task, it might be available in deep storage
        return Optional.absent();
    } else {
        // Worker is still running this task
        final URL url = TaskRunnerUtils.makeWorkerURL(worker, "/druid/worker/v1/task/%s/log?offset=%s", taskId, Long.toString(offset));
        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 : InputStream(java.io.InputStream) Request(org.apache.druid.java.util.http.client.Request) IOException(java.io.IOException) URL(java.net.URL) InputStreamResponseHandler(org.apache.druid.java.util.http.client.response.InputStreamResponseHandler) Worker(org.apache.druid.indexing.worker.Worker) ByteSource(com.google.common.io.ByteSource) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with InputStreamResponseHandler

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

the class HttpShuffleClient method fetchSegmentFile.

@Override
public File fetchSegmentFile(File partitionDir, String supervisorTaskId, GenericPartitionLocation location) throws IOException {
    // Create a local buffer since this class is not thread-safe.
    // Note that this method can be called by different threads at the same time with ThreadingTaskRunner.
    final byte[] buffer = new byte[BUFFER_SIZE];
    final File zippedFile = new File(partitionDir, StringUtils.format("temp_%s", location.getSubTaskId()));
    final URI uri = location.toIntermediaryDataServerURI(supervisorTaskId);
    FileUtils.copyLarge(uri, u -> {
        try {
            return httpClient.go(new Request(HttpMethod.GET, u.toURL()), new InputStreamResponseHandler()).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new IOException(e);
        }
    }, zippedFile, buffer, t -> t instanceof IOException, NUM_FETCH_RETRIES, StringUtils.format("Failed to fetch file[%s]", uri));
    final File unzippedDir = new File(partitionDir, StringUtils.format("unzipped_%s", location.getSubTaskId()));
    try {
        FileUtils.mkdirp(unzippedDir);
        CompressionUtils.unzip(zippedFile, unzippedDir);
    } finally {
        if (!zippedFile.delete()) {
            LOG.warn("Failed to delete temp file[%s]", zippedFile);
        }
    }
    return unzippedDir;
}
Also used : InputStreamResponseHandler(org.apache.druid.java.util.http.client.response.InputStreamResponseHandler) Request(org.apache.druid.java.util.http.client.Request) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File) URI(java.net.URI)

Example 5 with InputStreamResponseHandler

use of org.apache.druid.java.util.http.client.response.InputStreamResponseHandler 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)

Aggregations

Request (org.apache.druid.java.util.http.client.Request)10 InputStreamResponseHandler (org.apache.druid.java.util.http.client.response.InputStreamResponseHandler)10 IOException (java.io.IOException)8 URL (java.net.URL)8 InputStream (java.io.InputStream)7 ExecutionException (java.util.concurrent.ExecutionException)5 Test (org.junit.Test)5 ByteSource (com.google.common.io.ByteSource)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3 StringWriter (java.io.StringWriter)2 TaskLocation (org.apache.druid.indexer.TaskLocation)2 Worker (org.apache.druid.indexing.worker.Worker)2 JettyServerModule (org.apache.druid.server.initialization.jetty.JettyServerModule)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 File (java.io.File)1 URI (java.net.URI)1 CountDownLatch (java.util.concurrent.CountDownLatch)1