Search in sources :

Example 6 with Request

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

the class LookupReferencesManagerTest method testRealModeWithMainThread.

@Test(timeout = 60_000L)
public void testRealModeWithMainThread() throws Exception {
    LookupReferencesManager lookupReferencesManager = new LookupReferencesManager(new LookupConfig(temporaryFolder.newFolder().getAbsolutePath()), mapper, druidLeaderClient, config);
    Map<String, Object> lookupMap = new HashMap<>();
    lookupMap.put("testMockForRealModeWithMainThread", container);
    String strResult = mapper.writeValueAsString(lookupMap);
    Request request = new Request(HttpMethod.GET, new URL("http://localhost:1234/xx"));
    EasyMock.expect(config.getLookupTier()).andReturn(LOOKUP_TIER).anyTimes();
    EasyMock.replay(config);
    EasyMock.expect(druidLeaderClient.makeRequest(HttpMethod.GET, "/druid/coordinator/v1/lookups/config/lookupTier?detailed=true")).andReturn(request);
    StringFullResponseHolder responseHolder = new StringFullResponseHolder(newEmptyResponse(HttpResponseStatus.OK), StandardCharsets.UTF_8).addChunk(strResult);
    EasyMock.expect(druidLeaderClient.go(request)).andReturn(responseHolder);
    EasyMock.replay(druidLeaderClient);
    lookupReferencesManager.start();
    Assert.assertTrue(lookupReferencesManager.mainThread.isAlive());
    LookupExtractorFactory lookupExtractorFactory = EasyMock.createMock(LookupExtractorFactory.class);
    EasyMock.expect(lookupExtractorFactory.start()).andReturn(true).once();
    EasyMock.expect(lookupExtractorFactory.destroy()).andReturn(true).once();
    EasyMock.replay(lookupExtractorFactory);
    Assert.assertEquals(Optional.empty(), lookupReferencesManager.get("test"));
    LookupExtractorFactoryContainer testContainer = new LookupExtractorFactoryContainer("0", lookupExtractorFactory);
    lookupReferencesManager.add("test", testContainer);
    while (!Optional.of(testContainer).equals(lookupReferencesManager.get("test"))) {
        Thread.sleep(100);
    }
    Assert.assertEquals(ImmutableSet.of("test", "testMockForRealModeWithMainThread"), lookupReferencesManager.getAllLookupNames());
    lookupReferencesManager.remove("test");
    while (lookupReferencesManager.get("test").isPresent()) {
        Thread.sleep(100);
    }
    Assert.assertEquals(ImmutableSet.of("testMockForRealModeWithMainThread"), lookupReferencesManager.getAllLookupNames());
    lookupReferencesManager.stop();
    Assert.assertFalse(lookupReferencesManager.mainThread.isAlive());
}
Also used : StringFullResponseHolder(org.apache.druid.java.util.http.client.response.StringFullResponseHolder) HashMap(java.util.HashMap) Request(org.apache.druid.java.util.http.client.Request) URL(java.net.URL) Test(org.junit.Test)

Example 7 with Request

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

the class LookupReferencesManagerTest method testUpdateWithLowerVersion.

@Test
public void testUpdateWithLowerVersion() throws Exception {
    LookupExtractorFactory lookupExtractorFactory1 = EasyMock.createNiceMock(LookupExtractorFactory.class);
    EasyMock.expect(lookupExtractorFactory1.start()).andReturn(true).once();
    LookupExtractorFactory lookupExtractorFactory2 = EasyMock.createNiceMock(LookupExtractorFactory.class);
    EasyMock.replay(lookupExtractorFactory1, lookupExtractorFactory2);
    Map<String, Object> lookupMap = new HashMap<>();
    lookupMap.put("testMockForUpdateWithLowerVersion", container);
    String strResult = mapper.writeValueAsString(lookupMap);
    Request request = new Request(HttpMethod.GET, new URL("http://localhost:1234/xx"));
    EasyMock.expect(config.getLookupTier()).andReturn(LOOKUP_TIER).anyTimes();
    EasyMock.replay(config);
    EasyMock.expect(druidLeaderClient.makeRequest(HttpMethod.GET, "/druid/coordinator/v1/lookups/config/lookupTier?detailed=true")).andReturn(request);
    StringFullResponseHolder responseHolder = new StringFullResponseHolder(newEmptyResponse(HttpResponseStatus.OK), StandardCharsets.UTF_8).addChunk(strResult);
    EasyMock.expect(druidLeaderClient.go(request)).andReturn(responseHolder);
    EasyMock.replay(druidLeaderClient);
    lookupReferencesManager.start();
    lookupReferencesManager.add("testName", new LookupExtractorFactoryContainer("1", lookupExtractorFactory1));
    lookupReferencesManager.handlePendingNotices();
    lookupReferencesManager.add("testName", new LookupExtractorFactoryContainer("0", lookupExtractorFactory2));
    lookupReferencesManager.handlePendingNotices();
    EasyMock.verify(lookupExtractorFactory1, lookupExtractorFactory2);
}
Also used : StringFullResponseHolder(org.apache.druid.java.util.http.client.response.StringFullResponseHolder) HashMap(java.util.HashMap) Request(org.apache.druid.java.util.http.client.Request) URL(java.net.URL) Test(org.junit.Test)

Example 8 with Request

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

the class DirectDruidClientTest method testRun.

@Test
public void testRun() throws Exception {
    final URL url = new URL(StringUtils.format("http://%s/druid/v2/", hostName));
    SettableFuture<InputStream> futureResult = SettableFuture.create();
    Capture<Request> capturedRequest = EasyMock.newCapture();
    EasyMock.expect(httpClient.go(EasyMock.capture(capturedRequest), EasyMock.<HttpResponseHandler>anyObject(), EasyMock.anyObject(Duration.class))).andReturn(futureResult).times(1);
    SettableFuture futureException = SettableFuture.create();
    EasyMock.expect(httpClient.go(EasyMock.capture(capturedRequest), EasyMock.<HttpResponseHandler>anyObject(), EasyMock.anyObject(Duration.class))).andReturn(futureException).times(1);
    EasyMock.expect(httpClient.go(EasyMock.capture(capturedRequest), EasyMock.<HttpResponseHandler>anyObject(), EasyMock.anyObject(Duration.class))).andReturn(SettableFuture.create()).atLeastOnce();
    EasyMock.replay(httpClient);
    DirectDruidClient client2 = new DirectDruidClient(new ReflectionQueryToolChestWarehouse(), QueryRunnerTestHelper.NOOP_QUERYWATCHER, new DefaultObjectMapper(), httpClient, "http", "foo2", new NoopServiceEmitter());
    QueryableDruidServer queryableDruidServer2 = new QueryableDruidServer(new DruidServer("test1", "localhost", null, 0, ServerType.HISTORICAL, DruidServer.DEFAULT_TIER, 0), client2);
    serverSelector.addServerAndUpdateSegment(queryableDruidServer2, serverSelector.getSegment());
    TimeBoundaryQuery query = Druids.newTimeBoundaryQueryBuilder().dataSource("test").build();
    query = query.withOverriddenContext(ImmutableMap.of(DirectDruidClient.QUERY_FAIL_TIME, Long.MAX_VALUE));
    Sequence s1 = client.run(QueryPlus.wrap(query));
    Assert.assertTrue(capturedRequest.hasCaptured());
    Assert.assertEquals(url, capturedRequest.getValue().getUrl());
    Assert.assertEquals(HttpMethod.POST, capturedRequest.getValue().getMethod());
    Assert.assertEquals(1, client.getNumOpenConnections());
    // simulate read timeout
    client.run(QueryPlus.wrap(query));
    Assert.assertEquals(2, client.getNumOpenConnections());
    futureException.setException(new ReadTimeoutException());
    Assert.assertEquals(1, client.getNumOpenConnections());
    // subsequent connections should work
    client.run(QueryPlus.wrap(query));
    client.run(QueryPlus.wrap(query));
    client.run(QueryPlus.wrap(query));
    Assert.assertTrue(client.getNumOpenConnections() == 4);
    // produce result for first connection
    futureResult.set(new ByteArrayInputStream(StringUtils.toUtf8("[{\"timestamp\":\"2014-01-01T01:02:03Z\", \"result\": 42.0}]")));
    List<Result> results = s1.toList();
    Assert.assertEquals(1, results.size());
    Assert.assertEquals(DateTimes.of("2014-01-01T01:02:03Z"), results.get(0).getTimestamp());
    Assert.assertEquals(3, client.getNumOpenConnections());
    client2.run(QueryPlus.wrap(query));
    client2.run(QueryPlus.wrap(query));
    Assert.assertEquals(2, client2.getNumOpenConnections());
    Assert.assertEquals(serverSelector.pick(null), queryableDruidServer2);
    EasyMock.verify(httpClient);
}
Also used : SettableFuture(com.google.common.util.concurrent.SettableFuture) ByteArrayInputStream(java.io.ByteArrayInputStream) PipedInputStream(java.io.PipedInputStream) InputStream(java.io.InputStream) ReadTimeoutException(org.jboss.netty.handler.timeout.ReadTimeoutException) Request(org.apache.druid.java.util.http.client.Request) QueryableDruidServer(org.apache.druid.client.selector.QueryableDruidServer) NoopServiceEmitter(org.apache.druid.server.metrics.NoopServiceEmitter) TimeBoundaryQuery(org.apache.druid.query.timeboundary.TimeBoundaryQuery) Sequence(org.apache.druid.java.util.common.guava.Sequence) URL(java.net.URL) QueryableDruidServer(org.apache.druid.client.selector.QueryableDruidServer) Result(org.apache.druid.query.Result) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) HttpResponseHandler(org.apache.druid.java.util.http.client.response.HttpResponseHandler) ReflectionQueryToolChestWarehouse(org.apache.druid.query.ReflectionQueryToolChestWarehouse) Test(org.junit.Test)

Example 9 with Request

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

the class DirectDruidClientTest method testCancel.

@Test
public void testCancel() {
    Capture<Request> capturedRequest = EasyMock.newCapture();
    ListenableFuture<Object> cancelledFuture = Futures.immediateCancelledFuture();
    SettableFuture<Object> cancellationFuture = SettableFuture.create();
    EasyMock.expect(httpClient.go(EasyMock.capture(capturedRequest), EasyMock.<HttpResponseHandler>anyObject(), EasyMock.anyObject(Duration.class))).andReturn(cancelledFuture).once();
    EasyMock.expect(httpClient.go(EasyMock.capture(capturedRequest), EasyMock.<HttpResponseHandler>anyObject(), EasyMock.anyObject(Duration.class))).andReturn(cancellationFuture).anyTimes();
    EasyMock.replay(httpClient);
    TimeBoundaryQuery query = Druids.newTimeBoundaryQueryBuilder().dataSource("test").build();
    query = query.withOverriddenContext(ImmutableMap.of(DirectDruidClient.QUERY_FAIL_TIME, Long.MAX_VALUE));
    cancellationFuture.set(new StatusResponseHolder(HttpResponseStatus.OK, new StringBuilder("cancelled")));
    Sequence results = client.run(QueryPlus.wrap(query));
    Assert.assertEquals(HttpMethod.POST, capturedRequest.getValue().getMethod());
    Assert.assertEquals(0, client.getNumOpenConnections());
    QueryInterruptedException exception = null;
    try {
        results.toList();
    } catch (QueryInterruptedException e) {
        exception = e;
    }
    Assert.assertNotNull(exception);
    EasyMock.verify(httpClient);
}
Also used : Request(org.apache.druid.java.util.http.client.Request) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) TimeBoundaryQuery(org.apache.druid.query.timeboundary.TimeBoundaryQuery) Sequence(org.apache.druid.java.util.common.guava.Sequence) HttpResponseHandler(org.apache.druid.java.util.http.client.response.HttpResponseHandler) QueryInterruptedException(org.apache.druid.query.QueryInterruptedException) Test(org.junit.Test)

Example 10 with Request

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

the class DirectDruidClientTest method testQueryTimeoutBeforeFuture.

@Test
public void testQueryTimeoutBeforeFuture() throws IOException, InterruptedException {
    SettableFuture<Object> timeoutFuture = SettableFuture.create();
    Capture<Request> capturedRequest = EasyMock.newCapture();
    final String queryId = "timeout-before-future";
    EasyMock.expect(httpClient.go(EasyMock.capture(capturedRequest), EasyMock.<HttpResponseHandler>anyObject(), EasyMock.anyObject(Duration.class))).andReturn(timeoutFuture).anyTimes();
    EasyMock.replay(httpClient);
    TimeBoundaryQuery query = Druids.newTimeBoundaryQueryBuilder().dataSource("test").build();
    query = query.withOverriddenContext(ImmutableMap.of(DirectDruidClient.QUERY_FAIL_TIME, System.currentTimeMillis() + 250, "queryId", queryId));
    Sequence results = client.run(QueryPlus.wrap(query));
    // incomplete result set
    PipedInputStream in = new PipedInputStream();
    final PipedOutputStream out = new PipedOutputStream(in);
    timeoutFuture.set(in);
    QueryTimeoutException actualException = null;
    try {
        out.write(StringUtils.toUtf8("[{\"timestamp\":\"2014-01-01T01:02:03Z\"}"));
        Thread.sleep(250);
        out.write(StringUtils.toUtf8("]"));
        out.close();
        results.toList();
    } catch (QueryTimeoutException e) {
        actualException = e;
    }
    Assert.assertNotNull(actualException);
    Assert.assertEquals("Query timeout", actualException.getErrorCode());
    Assert.assertEquals("url[http://localhost:8080/druid/v2/] timed out", actualException.getMessage());
    Assert.assertEquals(hostName, actualException.getHost());
    EasyMock.verify(httpClient);
}
Also used : QueryTimeoutException(org.apache.druid.query.QueryTimeoutException) Request(org.apache.druid.java.util.http.client.Request) PipedOutputStream(java.io.PipedOutputStream) TimeBoundaryQuery(org.apache.druid.query.timeboundary.TimeBoundaryQuery) Sequence(org.apache.druid.java.util.common.guava.Sequence) PipedInputStream(java.io.PipedInputStream) HttpResponseHandler(org.apache.druid.java.util.http.client.response.HttpResponseHandler) 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