Search in sources :

Example 6 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)

Example 7 with InputStreamResponseHandler

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

the class JettyTest method testChunkNotFinalized.

// Tests that threads are not stuck when partial chunk is not finalized
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=424107
@Test
@Ignore
public // above bug is not fixed in jetty for gzip encoding, and the chunk is still finalized instead of throwing exception.
void testChunkNotFinalized() throws Exception {
    ListenableFuture<InputStream> go = client.go(new Request(HttpMethod.GET, new URL("http://localhost:" + port + "/exception/exception")), new InputStreamResponseHandler());
    try {
        StringWriter writer = new StringWriter();
        IOUtils.copy(go.get(), writer, "utf-8");
        Assert.fail("Should have thrown Exception");
    } catch (IOException e) {
    // Expected.
    }
}
Also used : InputStreamResponseHandler(org.apache.druid.java.util.http.client.response.InputStreamResponseHandler) StringWriter(java.io.StringWriter) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) Request(org.apache.druid.java.util.http.client.Request) IOException(java.io.IOException) URL(java.net.URL) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with InputStreamResponseHandler

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

the class JettyTest method testThreadNotStuckOnException.

@Test
public void testThreadNotStuckOnException() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    Executors.newSingleThreadExecutor().execute(new Runnable() {

        @Override
        public void run() {
            try {
                ListenableFuture<InputStream> go = client.go(new Request(HttpMethod.GET, new URL("http://localhost:" + port + "/exception/exception")), new InputStreamResponseHandler());
                StringWriter writer = new StringWriter();
                IOUtils.copy(go.get(), writer, "utf-8");
            } catch (IOException e) {
            // Expected.
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
            latch.countDown();
        }
    });
    latch.await(5, TimeUnit.SECONDS);
}
Also used : InputStreamResponseHandler(org.apache.druid.java.util.http.client.response.InputStreamResponseHandler) StringWriter(java.io.StringWriter) Request(org.apache.druid.java.util.http.client.Request) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) URL(java.net.URL) Test(org.junit.Test)

Example 9 with InputStreamResponseHandler

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

the class JettyTest method testGzipRequestDecompression.

@Test
public void testGzipRequestDecompression() 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.POST, new URL("http://localhost:" + port + "/return"));
    request.setHeader("Content-Encoding", "gzip");
    request.setContent(MediaType.TEXT_PLAIN, out.toByteArray());
    Assert.assertEquals(text, new String(IOUtils.toByteArray(client.go(request, new InputStreamResponseHandler()).get()), Charset.defaultCharset()));
}
Also used : InputStreamResponseHandler(org.apache.druid.java.util.http.client.response.InputStreamResponseHandler) GZIPOutputStream(java.util.zip.GZIPOutputStream) Request(org.apache.druid.java.util.http.client.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL) Test(org.junit.Test)

Example 10 with InputStreamResponseHandler

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

the class JettyTest method testNumConnectionsMetricHttps.

@Test
public void testNumConnectionsMetricHttps() 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("https://localhost:" + tlsPort + "/latched/hello"));
    request.setHeader("Content-Encoding", "gzip");
    request.setContent(MediaType.TEXT_PLAIN, out.toByteArray());
    HttpClient client;
    try {
        client = HttpClientInit.createClient(sslConfig, lifecycle);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    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) HttpClient(org.apache.druid.java.util.http.client.HttpClient) 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) IOException(java.io.IOException) Test(org.junit.Test)

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