Search in sources :

Example 16 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class JankyServersTest method testHttpsSilentServer.

@Test
public void testHttpsSilentServer() throws Throwable {
    final Lifecycle lifecycle = new Lifecycle();
    try {
        final HttpClientConfig config = HttpClientConfig.builder().withSslContext(SSLContext.getDefault()).withSslHandshakeTimeout(new Duration(100)).build();
        final HttpClient client = HttpClientInit.createClient(config, lifecycle);
        final ListenableFuture<StatusResponseHolder> response = client.go(new Request(HttpMethod.GET, new URL(StringUtils.format("https://localhost:%d/", silentServerSocket.getLocalPort()))), StatusResponseHandler.getInstance());
        Throwable e = null;
        try {
            response.get();
        } catch (ExecutionException e1) {
            e = e1.getCause();
        }
        Assert.assertTrue("ChannelException thrown by 'get'", e instanceof ChannelException);
    } finally {
        lifecycle.stop();
    }
}
Also used : Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) Duration(org.joda.time.Duration) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL) ChannelException(org.jboss.netty.channel.ChannelException) Test(org.junit.Test)

Example 17 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class JankyServersTest method testHttpConnectionClosingServer.

@Test
public void testHttpConnectionClosingServer() throws Throwable {
    final Lifecycle lifecycle = new Lifecycle();
    try {
        final HttpClientConfig config = HttpClientConfig.builder().build();
        final HttpClient client = HttpClientInit.createClient(config, lifecycle);
        final ListenableFuture<StatusResponseHolder> response = client.go(new Request(HttpMethod.GET, new URL(StringUtils.format("http://localhost:%d/", closingServerSocket.getLocalPort()))), StatusResponseHandler.getInstance());
        Throwable e = null;
        try {
            response.get();
        } catch (ExecutionException e1) {
            e = e1.getCause();
            e1.printStackTrace();
        }
        Assert.assertTrue("ChannelException thrown by 'get'", isChannelClosedException(e));
    } finally {
        lifecycle.stop();
    }
}
Also used : Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL) Test(org.junit.Test)

Example 18 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class HttpClientInit method createBootstrap.

private static ClientBootstrap createBootstrap(Lifecycle lifecycle, Timer timer, int bossPoolSize, int workerPoolSize) {
    final NioClientBossPool bossPool = new NioClientBossPool(Executors.newCachedThreadPool(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("HttpClient-Netty-Boss-%s").build()), bossPoolSize, timer, ThreadNameDeterminer.CURRENT);
    final NioWorkerPool workerPool = new NioWorkerPool(Executors.newCachedThreadPool(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("HttpClient-Netty-Worker-%s").build()), workerPoolSize, ThreadNameDeterminer.CURRENT);
    final ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(bossPool, workerPool));
    bootstrap.setOption("keepAlive", true);
    bootstrap.setPipelineFactory(new HttpClientPipelineFactory());
    InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());
    try {
        lifecycle.addMaybeStartHandler(new Lifecycle.Handler() {

            @Override
            public void start() {
            }

            @Override
            public void stop() {
                bootstrap.releaseExternalResources();
            }
        });
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return bootstrap;
}
Also used : NioClientBossPool(org.jboss.netty.channel.socket.nio.NioClientBossPool) NioWorkerPool(org.jboss.netty.channel.socket.nio.NioWorkerPool) NioClientSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) HttpClientPipelineFactory(org.apache.druid.java.util.http.client.netty.HttpClientPipelineFactory) Slf4JLoggerFactory(org.jboss.netty.logging.Slf4JLoggerFactory)

Example 19 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class NamespacedExtractorModuleTest method setUp.

@Before
public void setUp() throws Exception {
    final Map<Class<? extends ExtractionNamespace>, CacheGenerator<?>> factoryMap = ImmutableMap.of(UriExtractionNamespace.class, new UriCacheGenerator(ImmutableMap.of("file", new LocalFileTimestampVersionFinder())), JdbcExtractionNamespace.class, new JdbcCacheGenerator());
    lifecycle = new Lifecycle();
    lifecycle.start();
    NoopServiceEmitter noopServiceEmitter = new NoopServiceEmitter();
    scheduler = new CacheScheduler(noopServiceEmitter, factoryMap, new OnHeapNamespaceExtractionCacheManager(lifecycle, noopServiceEmitter, new NamespaceExtractionConfig()));
}
Also used : CacheGenerator(org.apache.druid.query.lookup.namespace.CacheGenerator) OnHeapNamespaceExtractionCacheManager(org.apache.druid.server.lookup.namespace.cache.OnHeapNamespaceExtractionCacheManager) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) ExtractionNamespace(org.apache.druid.query.lookup.namespace.ExtractionNamespace) UriExtractionNamespace(org.apache.druid.query.lookup.namespace.UriExtractionNamespace) JdbcExtractionNamespace(org.apache.druid.query.lookup.namespace.JdbcExtractionNamespace) LocalFileTimestampVersionFinder(org.apache.druid.segment.loading.LocalFileTimestampVersionFinder) NoopServiceEmitter(org.apache.druid.server.metrics.NoopServiceEmitter) CacheScheduler(org.apache.druid.server.lookup.namespace.cache.CacheScheduler) Before(org.junit.Before)

Example 20 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class JdbcExtractionNamespaceTest method setup.

@Before
public void setup() throws Exception {
    lifecycle = new Lifecycle();
    updates = new AtomicLong(0L);
    updateLock = new ReentrantLock(true);
    closer = Closer.create();
    setupTeardownService = MoreExecutors.listeningDecorator(Execs.multiThreaded(2, "JDBCExtractionNamespaceTeardown--%s"));
    final ListenableFuture<Handle> setupFuture = setupTeardownService.submit(new Callable<Handle>() {

        @Override
        public Handle call() {
            final Handle handle = derbyConnectorRule.getConnector().getDBI().open();
            Assert.assertEquals(0, handle.createStatement(StringUtils.format("CREATE TABLE %s (%s TIMESTAMP, %s VARCHAR(64), %s VARCHAR(64), %s VARCHAR(64))", TABLE_NAME, TS_COLUMN, FILTER_COLUMN, KEY_NAME, VAL_NAME)).setQueryTimeout(1).execute());
            handle.createStatement(StringUtils.format("TRUNCATE TABLE %s", TABLE_NAME)).setQueryTimeout(1).execute();
            handle.commit();
            closer.register(new Closeable() {

                @Override
                public void close() throws IOException {
                    handle.createStatement("DROP TABLE " + TABLE_NAME).setQueryTimeout(1).execute();
                    final ListenableFuture future = setupTeardownService.submit(new Runnable() {

                        @Override
                        public void run() {
                            handle.close();
                        }
                    });
                    try (Closeable closeable = new Closeable() {

                        @Override
                        public void close() {
                            future.cancel(true);
                        }
                    }) {
                        future.get(10, TimeUnit.SECONDS);
                    } catch (InterruptedException | ExecutionException | TimeoutException e) {
                        throw new IOException("Error closing handle", e);
                    }
                }
            });
            closer.register(new Closeable() {

                @Override
                public void close() {
                    if (scheduler == null) {
                        return;
                    }
                    Assert.assertEquals(0, scheduler.getActiveEntries());
                }
            });
            for (Map.Entry<String, String[]> entry : RENAMES.entrySet()) {
                try {
                    String key = entry.getKey();
                    String value = entry.getValue()[0];
                    String filter = entry.getValue()[1];
                    insertValues(handle, key, value, filter, "2015-01-01 00:00:00");
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    throw new RuntimeException(e);
                }
            }
            NoopServiceEmitter noopServiceEmitter = new NoopServiceEmitter();
            scheduler = new CacheScheduler(noopServiceEmitter, ImmutableMap.of(JdbcExtractionNamespace.class, new CacheGenerator<JdbcExtractionNamespace>() {

                private final JdbcCacheGenerator delegate = new JdbcCacheGenerator();

                @Override
                public CacheScheduler.VersionedCache generateCache(final JdbcExtractionNamespace namespace, final CacheScheduler.EntryImpl<JdbcExtractionNamespace> id, final String lastVersion, final CacheScheduler scheduler) throws InterruptedException {
                    updateLock.lockInterruptibly();
                    try {
                        log.debug("Running cache generator");
                        try {
                            return delegate.generateCache(namespace, id, lastVersion, scheduler);
                        } finally {
                            updates.incrementAndGet();
                        }
                    } finally {
                        updateLock.unlock();
                    }
                }
            }), new OnHeapNamespaceExtractionCacheManager(lifecycle, noopServiceEmitter, new NamespaceExtractionConfig()));
            try {
                lifecycle.start();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            closer.register(new Closeable() {

                @Override
                public void close() throws IOException {
                    final ListenableFuture future = setupTeardownService.submit(new Runnable() {

                        @Override
                        public void run() {
                            lifecycle.stop();
                        }
                    });
                    try (final Closeable closeable = new Closeable() {

                        @Override
                        public void close() {
                            future.cancel(true);
                        }
                    }) {
                        future.get(30, TimeUnit.SECONDS);
                    } catch (InterruptedException | ExecutionException | TimeoutException e) {
                        throw new IOException("Error stopping lifecycle", e);
                    }
                }
            });
            return handle;
        }
    });
    try (final Closeable ignore = () -> setupFuture.cancel(true)) {
        handleRef = setupFuture.get(10, TimeUnit.SECONDS);
    }
    Assert.assertNotNull(handleRef);
}
Also used : JdbcCacheGenerator(org.apache.druid.server.lookup.namespace.JdbcCacheGenerator) CacheGenerator(org.apache.druid.query.lookup.namespace.CacheGenerator) Closeable(java.io.Closeable) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) JdbcCacheGenerator(org.apache.druid.server.lookup.namespace.JdbcCacheGenerator) NamespaceExtractionConfig(org.apache.druid.server.lookup.namespace.NamespaceExtractionConfig) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) NoopServiceEmitter(org.apache.druid.server.metrics.NoopServiceEmitter) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Handle(org.skife.jdbi.v2.Handle) AtomicLong(java.util.concurrent.atomic.AtomicLong) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) JdbcExtractionNamespace(org.apache.druid.query.lookup.namespace.JdbcExtractionNamespace) Before(org.junit.Before)

Aggregations

Lifecycle (org.apache.druid.java.util.common.lifecycle.Lifecycle)46 Test (org.junit.Test)21 Injector (com.google.inject.Injector)12 URL (java.net.URL)12 ExecutionException (java.util.concurrent.ExecutionException)12 StatusResponseHolder (org.apache.druid.java.util.http.client.response.StatusResponseHolder)10 Before (org.junit.Before)10 ExecutorService (java.util.concurrent.ExecutorService)6 Binder (com.google.inject.Binder)5 Module (com.google.inject.Module)5 OutputStream (java.io.OutputStream)5 Properties (java.util.Properties)5 ManageLifecycle (org.apache.druid.guice.ManageLifecycle)5 NoopServiceEmitter (org.apache.druid.server.metrics.NoopServiceEmitter)5 ChannelException (org.jboss.netty.channel.ChannelException)5 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)4 IOException (java.io.IOException)4 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Provides (com.google.inject.Provides)3