Search in sources :

Example 26 with StatusResponseHolder

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

the class ITWikipediaQueryTest method testQueryLaningLaneIsLimited.

@Test
public void testQueryLaningLaneIsLimited() throws Exception {
    ITRetryUtil.retryUntil(() -> {
        // the broker is configured with a manually defined query lane, 'one' with limit 1
        // -Ddruid.query.scheduler.laning.type=manual
        // -Ddruid.query.scheduler.laning.lanes.one=1
        // by issuing 50 queries, at least 1 of them will succeed on 'one', and at least 1 of them will overlap enough to
        // get limited.
        // It's possible but unlikely that these queries execute in a way that none of them overlap, so we
        // retry this test a few times to compensate for this.
        final int numQueries = 50;
        List<Future<StatusResponseHolder>> futures = new ArrayList<>(numQueries);
        for (int i = 0; i < numQueries; i++) {
            futures.add(queryClient.queryAsync(queryHelper.getQueryURL(config.getBrokerUrl()), getQueryBuilder().build()));
        }
        int success = 0;
        int limited = 0;
        for (Future<StatusResponseHolder> future : futures) {
            StatusResponseHolder status = future.get();
            if (status.getStatus().getCode() == QueryCapacityExceededException.STATUS_CODE) {
                limited++;
                Assert.assertTrue(status.getContent().contains(QueryCapacityExceededException.makeLaneErrorMessage("one", 1)));
            } else if (status.getStatus().getCode() == HttpResponseStatus.OK.getCode()) {
                success++;
            }
        }
        try {
            Assert.assertTrue(success > 0);
            Assert.assertTrue(limited > 0);
            return true;
        } catch (AssertionError ae) {
            LOG.error(ae, "Got assertion error in testQueryLaningLaneIsLimited");
            return false;
        }
    }, true, 5000, 3, "testQueryLaningLaneIsLimited");
    // test another to make sure we can still issue one query at a time
    StatusResponseHolder followUp = queryClient.queryAsync(queryHelper.getQueryURL(config.getBrokerUrl()), getQueryBuilder().build()).get();
    Assert.assertEquals(followUp.getStatus().getCode(), HttpResponseStatus.OK.getCode());
    StatusResponseHolder andAnother = queryClient.queryAsync(queryHelper.getQueryURL(config.getBrokerUrl()), getQueryBuilder().build()).get();
    Assert.assertEquals(andAnother.getStatus().getCode(), HttpResponseStatus.OK.getCode());
}
Also used : ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) Test(org.testng.annotations.Test)

Example 27 with StatusResponseHolder

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

the class ITWikipediaQueryTest method testQueryLaningWithNoLane.

@Test
public void testQueryLaningWithNoLane() throws Exception {
    // the broker is configured with a manually defined query lane, 'one' with limit 1
    // -Ddruid.query.scheduler.laning.type=manual
    // -Ddruid.query.scheduler.laning.lanes.one=1
    // these queries will not belong to the lane so none of them should be limited
    final int numQueries = 50;
    List<Future<StatusResponseHolder>> futures = new ArrayList<>(numQueries);
    for (int i = 0; i < numQueries; i++) {
        futures.add(queryClient.queryAsync(queryHelper.getQueryURL(config.getBrokerUrl()), getQueryBuilder().context(ImmutableMap.of("queryId", UUID.randomUUID().toString())).build()));
    }
    int success = 0;
    int limited = 0;
    for (Future<StatusResponseHolder> future : futures) {
        StatusResponseHolder status = future.get();
        if (status.getStatus().getCode() == QueryCapacityExceededException.STATUS_CODE) {
            limited++;
        } else if (status.getStatus().getCode() == HttpResponseStatus.OK.getCode()) {
            success++;
        }
    }
    Assert.assertTrue(success > 0);
    Assert.assertEquals(limited, 0);
}
Also used : ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) Test(org.testng.annotations.Test)

Example 28 with StatusResponseHolder

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

the class ITHighAvailabilityTest method testSelfDiscovery.

private int testSelfDiscovery(Collection<DiscoveryDruidNode> nodes) throws MalformedURLException, ExecutionException, InterruptedException {
    int count = 0;
    for (DiscoveryDruidNode node : nodes) {
        final String location = StringUtils.format("http://%s:%s/status/selfDiscovered", config.isDocker() ? config.getDockerHost() : node.getDruidNode().getHost(), node.getDruidNode().getPlaintextPort());
        LOG.info("testing self discovery %s", location);
        StatusResponseHolder response = httpClient.go(new Request(HttpMethod.GET, new URL(location)), StatusResponseHandler.getInstance()).get();
        LOG.info("%s responded with %s", location, response.getStatus().getCode());
        Assert.assertEquals(response.getStatus(), HttpResponseStatus.OK);
        count++;
    }
    return count;
}
Also used : DiscoveryDruidNode(org.apache.druid.discovery.DiscoveryDruidNode) Request(org.apache.druid.java.util.http.client.Request) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) URL(java.net.URL)

Example 29 with StatusResponseHolder

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

the class FriendlyServersTest method testFriendlyProxyHttpServer.

@Test
public void testFriendlyProxyHttpServer() throws Exception {
    final AtomicReference<String> requestContent = new AtomicReference<>();
    final ExecutorService exec = Executors.newSingleThreadExecutor();
    final ServerSocket serverSocket = new ServerSocket(0);
    exec.submit(new Runnable() {

        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                try (Socket clientSocket = serverSocket.accept();
                    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), StandardCharsets.UTF_8));
                    OutputStream out = clientSocket.getOutputStream()) {
                    StringBuilder request = new StringBuilder();
                    String line;
                    while (!"".equals((line = in.readLine()))) {
                        request.append(line).append("\r\n");
                    }
                    requestContent.set(request.toString());
                    out.write("HTTP/1.1 200 OK\r\n\r\n".getBytes(StandardCharsets.UTF_8));
                    while (!in.readLine().equals("")) {
                    // skip lines
                    }
                    out.write("HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\nhello!".getBytes(StandardCharsets.UTF_8));
                } catch (Exception e) {
                    Assert.fail(e.toString());
                }
            }
        }
    });
    final Lifecycle lifecycle = new Lifecycle();
    try {
        final HttpClientConfig config = HttpClientConfig.builder().withHttpProxyConfig(new HttpClientProxyConfig("localhost", serverSocket.getLocalPort(), "bob", "sally")).build();
        final HttpClient client = HttpClientInit.createClient(config, lifecycle);
        final StatusResponseHolder response = client.go(new Request(HttpMethod.GET, new URL("http://anotherHost:8080/")), StatusResponseHandler.getInstance()).get();
        Assert.assertEquals(200, response.getStatus().getCode());
        Assert.assertEquals("hello!", response.getContent());
        Assert.assertEquals("CONNECT anotherHost:8080 HTTP/1.1\r\nProxy-Authorization: Basic Ym9iOnNhbGx5\r\n", requestContent.get());
    } finally {
        exec.shutdownNow();
        serverSocket.close();
        lifecycle.stop();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) AtomicReference(java.util.concurrent.atomic.AtomicReference) ServerSocket(java.net.ServerSocket) ChannelException(org.jboss.netty.channel.ChannelException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL) ExecutorService(java.util.concurrent.ExecutorService) BufferedReader(java.io.BufferedReader) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 30 with StatusResponseHolder

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

the class FriendlyServersTest method testFriendlyHttpServer.

@Test
public void testFriendlyHttpServer() throws Exception {
    final ExecutorService exec = Executors.newSingleThreadExecutor();
    final ServerSocket serverSocket = new ServerSocket(0);
    exec.submit(new Runnable() {

        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                try (Socket clientSocket = serverSocket.accept();
                    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), StandardCharsets.UTF_8));
                    OutputStream out = clientSocket.getOutputStream()) {
                    while (!in.readLine().equals("")) {
                    // skip lines
                    }
                    out.write("HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\nhello!".getBytes(StandardCharsets.UTF_8));
                } catch (Exception e) {
                // Suppress
                }
            }
        }
    });
    final Lifecycle lifecycle = new Lifecycle();
    try {
        final HttpClientConfig config = HttpClientConfig.builder().build();
        final HttpClient client = HttpClientInit.createClient(config, lifecycle);
        final StatusResponseHolder response = client.go(new Request(HttpMethod.GET, new URL(StringUtils.format("http://localhost:%d/", serverSocket.getLocalPort()))), StatusResponseHandler.getInstance()).get();
        Assert.assertEquals(200, response.getStatus().getCode());
        Assert.assertEquals("hello!", response.getContent());
    } finally {
        exec.shutdownNow();
        serverSocket.close();
        lifecycle.stop();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) ServerSocket(java.net.ServerSocket) ChannelException(org.jboss.netty.channel.ChannelException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL) ExecutorService(java.util.concurrent.ExecutorService) BufferedReader(java.io.BufferedReader) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

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