Search in sources :

Example 41 with Request

use of com.metamx.http.client.Request 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) {
                Throwables.propagate(t);
            }
            latch.countDown();
        }
    });
    latch.await(5, TimeUnit.SECONDS);
}
Also used : InputStreamResponseHandler(com.metamx.http.client.response.InputStreamResponseHandler) StringWriter(java.io.StringWriter) Request(com.metamx.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 42 with Request

use of com.metamx.http.client.Request in project druid by druid-io.

the class LookupCoordinatorManagerTest method testUpdateAllOnHost.

@Test
public void testUpdateAllOnHost() throws Exception {
    final HttpResponseHandler<InputStream, InputStream> responseHandler = EasyMock.createStrictMock(HttpResponseHandler.class);
    final URL url = LookupCoordinatorManager.getLookupsURL(HostAndPort.fromString("localhost"));
    final SettableFuture<InputStream> future = SettableFuture.create();
    future.set(new ByteArrayInputStream(StringUtils.toUtf8(mapper.writeValueAsString(ImmutableMap.of("status", "accepted", LookupModule.FAILED_UPDATES_KEY, ImmutableMap.of())))));
    EasyMock.expect(client.go(EasyMock.<Request>anyObject(), EasyMock.<SequenceInputStreamResponseHandler>anyObject(), EasyMock.<Duration>anyObject())).andReturn(future).once();
    EasyMock.replay(client, responseHandler);
    final LookupCoordinatorManager manager = new LookupCoordinatorManager(client, discoverer, mapper, configManager, lookupCoordinatorManagerConfig) {

        @Override
        HttpResponseHandler<InputStream, InputStream> makeResponseHandler(final AtomicInteger returnCode, final AtomicReference<String> reasonString) {
            returnCode.set(200);
            reasonString.set("");
            return responseHandler;
        }
    };
    manager.updateAllOnHost(url, SINGLE_LOOKUP_MAP);
    EasyMock.verify(client, responseHandler);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(com.metamx.http.client.Request) Duration(org.joda.time.Duration) AtomicReference(java.util.concurrent.atomic.AtomicReference) SequenceInputStreamResponseHandler(com.metamx.http.client.response.SequenceInputStreamResponseHandler) URL(java.net.URL) Test(org.junit.Test)

Example 43 with Request

use of com.metamx.http.client.Request in project druid by druid-io.

the class LookupCoordinatorManagerTest method testUpdateAllOnHostFailsWhenServerReturnsWeird.

@Test
public void testUpdateAllOnHostFailsWhenServerReturnsWeird() throws Exception {
    final HttpResponseHandler<InputStream, InputStream> responseHandler = EasyMock.createStrictMock(HttpResponseHandler.class);
    final String failedLookup = "failedLookup";
    final URL url = LookupCoordinatorManager.getLookupsURL(HostAndPort.fromString("localhost"));
    final SettableFuture<InputStream> future = SettableFuture.create();
    future.set(new ByteArrayInputStream(StringUtils.toUtf8(mapper.writeValueAsString(ImmutableMap.of("status", "accepted")))));
    EasyMock.expect(client.go(EasyMock.<Request>anyObject(), EasyMock.<SequenceInputStreamResponseHandler>anyObject(), EasyMock.<Duration>anyObject())).andReturn(future).once();
    EasyMock.replay(client, responseHandler);
    final LookupCoordinatorManager manager = new LookupCoordinatorManager(client, discoverer, mapper, configManager, lookupCoordinatorManagerConfig) {

        @Override
        HttpResponseHandler<InputStream, InputStream> makeResponseHandler(final AtomicInteger returnCode, final AtomicReference<String> reasonString) {
            returnCode.set(200);
            reasonString.set("");
            return responseHandler;
        }
    };
    expectedException.expectMessage(String.format("Update result did not have field for [%s]", LookupModule.FAILED_UPDATES_KEY));
    try {
        manager.updateAllOnHost(url, SINGLE_LOOKUP_MAP);
    } finally {
        EasyMock.verify(client, responseHandler);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(com.metamx.http.client.Request) Duration(org.joda.time.Duration) AtomicReference(java.util.concurrent.atomic.AtomicReference) SequenceInputStreamResponseHandler(com.metamx.http.client.response.SequenceInputStreamResponseHandler) URL(java.net.URL) Test(org.junit.Test)

Example 44 with Request

use of com.metamx.http.client.Request in project druid by druid-io.

the class LookupCoordinatorManagerTest method testDeleteAllTier.

@Test
public void testDeleteAllTier() throws Exception {
    final HttpResponseHandler<InputStream, InputStream> responseHandler = EasyMock.createStrictMock(HttpResponseHandler.class);
    final LookupCoordinatorManager manager = new LookupCoordinatorManager(client, discoverer, mapper, configManager, lookupCoordinatorManagerConfig) {

        @Override
        HttpResponseHandler<InputStream, InputStream> makeResponseHandler(final AtomicInteger returnCode, final AtomicReference<String> reasonString) {
            returnCode.set(200);
            reasonString.set("");
            return responseHandler;
        }
    };
    final HostAndPort hostAndPort = HostAndPort.fromParts("someHost", 8080);
    final Collection<String> drop = ImmutableList.of("lookup1");
    EasyMock.expect(discoverer.getNodes(LookupModule.getTierListenerPath(LOOKUP_TIER))).andReturn(ImmutableList.of(hostAndPort)).once();
    final SettableFuture<InputStream> future = SettableFuture.create();
    future.set(new ByteArrayInputStream(new byte[0]));
    EasyMock.expect(client.go(EasyMock.<Request>anyObject(), EasyMock.<SequenceInputStreamResponseHandler>anyObject(), EasyMock.<Duration>anyObject())).andReturn(future).once();
    EasyMock.replay(client, discoverer, responseHandler);
    manager.deleteAllOnTier(LOOKUP_TIER, drop);
    EasyMock.verify(client, discoverer, responseHandler);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(com.metamx.http.client.Request) AtomicReference(java.util.concurrent.atomic.AtomicReference) Duration(org.joda.time.Duration) SequenceInputStreamResponseHandler(com.metamx.http.client.response.SequenceInputStreamResponseHandler) HostAndPort(com.google.common.net.HostAndPort) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 45 with Request

use of com.metamx.http.client.Request in project druid by druid-io.

the class LookupCoordinatorManager method deleteOnHost.

void deleteOnHost(final URL url) throws ExecutionException, InterruptedException, IOException {
    final AtomicInteger returnCode = new AtomicInteger(0);
    final AtomicReference<String> reasonString = new AtomicReference<>(null);
    LOG.debug("Dropping %s", url);
    try (final InputStream result = httpClient.go(new Request(HttpMethod.DELETE, url).addHeader(HttpHeaders.Names.ACCEPT, SmileMediaTypes.APPLICATION_JACKSON_SMILE), makeResponseHandler(returnCode, reasonString), lookupCoordinatorManagerConfig.getHostDeleteTimeout()).get()) {
        // 404 is ok here, that means it was already deleted
        if (!httpStatusIsSuccess(returnCode.get()) && !httpStatusIsNotFound(returnCode.get())) {
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                StreamUtils.copyAndClose(result, baos);
            } catch (IOException e2) {
                LOG.warn(e2, "Error reading response from [%s]", url);
            }
            throw new IOException(String.format("Bad lookup delete request to [%s] : [%d] : [%s]  Response: [%s]", url, returnCode.get(), reasonString.get(), StringUtils.fromUtf8(baos.toByteArray())));
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Delete to [%s] : Status: %s reason: [%s]", url, returnCode.get(), reasonString.get());
            }
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InputStream(java.io.InputStream) Request(com.metamx.http.client.Request) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

Request (com.metamx.http.client.Request)47 URL (java.net.URL)38 Test (org.junit.Test)37 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)12 FullResponseHandler (com.metamx.http.client.response.FullResponseHandler)12 InputStream (java.io.InputStream)12 IOException (java.io.IOException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Duration (org.joda.time.Duration)9 StatusResponseHolder (com.metamx.http.client.response.StatusResponseHolder)8 Map (java.util.Map)8 SequenceInputStreamResponseHandler (com.metamx.http.client.response.SequenceInputStreamResponseHandler)7 DateTime (org.joda.time.DateTime)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 HttpClient (com.metamx.http.client.HttpClient)4 HttpResponseHandler (com.metamx.http.client.response.HttpResponseHandler)4 StatusResponseHandler (com.metamx.http.client.response.StatusResponseHandler)4 List (java.util.List)4