Search in sources :

Example 21 with Lifecycle

use of io.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(String.format("CREATE TABLE %s (%s TIMESTAMP, %s VARCHAR(64), %s VARCHAR(64))", tableName, tsColumn_, keyName, valName)).setQueryTimeout(1).execute());
            handle.createStatement(String.format("TRUNCATE TABLE %s", tableName)).setQueryTimeout(1).execute();
            handle.commit();
            closer.register(new Closeable() {

                @Override
                public void close() throws IOException {
                    handle.createStatement("DROP TABLE " + tableName).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() throws IOException {
                            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() throws IOException {
                    if (scheduler == null) {
                        return;
                    }
                    Assert.assertEquals(0, scheduler.getActiveEntries());
                }
            });
            for (Map.Entry<String, String> entry : renames.entrySet()) {
                try {
                    insertValues(handle, entry.getKey(), entry.getValue(), "2015-01-01 00:00:00");
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    throw Throwables.propagate(e);
                }
            }
            NoopServiceEmitter noopServiceEmitter = new NoopServiceEmitter();
            scheduler = new CacheScheduler(noopServiceEmitter, ImmutableMap.<Class<? extends ExtractionNamespace>, ExtractionNamespaceCacheFactory<?>>of(JDBCExtractionNamespace.class, new ExtractionNamespaceCacheFactory<JDBCExtractionNamespace>() {

                private final JDBCExtractionNamespaceCacheFactory delegate = new JDBCExtractionNamespaceCacheFactory();

                @Override
                public CacheScheduler.VersionedCache populateCache(final JDBCExtractionNamespace namespace, final CacheScheduler.EntryImpl<JDBCExtractionNamespace> id, final String lastVersion, final CacheScheduler scheduler) throws InterruptedException {
                    updateLock.lockInterruptibly();
                    try {
                        log.debug("Running cache populator");
                        try {
                            return delegate.populateCache(namespace, id, lastVersion, scheduler);
                        } finally {
                            updates.incrementAndGet();
                        }
                    } finally {
                        updateLock.unlock();
                    }
                }
            }), new OnHeapNamespaceExtractionCacheManager(lifecycle, noopServiceEmitter));
            try {
                lifecycle.start();
            } catch (Exception e) {
                throw Throwables.propagate(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() throws IOException {
                            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 closeable = new Closeable() {

        @Override
        public void close() throws IOException {
            if (!setupFuture.isDone() && !setupFuture.cancel(true) && !setupFuture.isDone()) {
                throw new IOException("Unable to stop future");
            }
        }
    }) {
        handleRef = setupFuture.get(10, TimeUnit.SECONDS);
    }
    Assert.assertNotNull(handleRef);
}
Also used : Closeable(java.io.Closeable) ExtractionNamespaceCacheFactory(io.druid.query.lookup.namespace.ExtractionNamespaceCacheFactory) JDBCExtractionNamespaceCacheFactory(io.druid.server.lookup.namespace.JDBCExtractionNamespaceCacheFactory) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) JDBCExtractionNamespaceCacheFactory(io.druid.server.lookup.namespace.JDBCExtractionNamespaceCacheFactory) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) NoopServiceEmitter(io.druid.server.metrics.NoopServiceEmitter) JDBCExtractionNamespace(io.druid.query.lookup.namespace.JDBCExtractionNamespace) 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) Before(org.junit.Before)

Example 22 with Lifecycle

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

the class PrioritizedExecutorServiceTest method setUp.

@Before
public void setUp() throws Exception {
    exec = PrioritizedExecutorService.create(new Lifecycle(), new DruidProcessingConfig() {

        @Override
        public String getFormatString() {
            return "test";
        }

        @Override
        public int getNumThreads() {
            return 1;
        }

        @Override
        public boolean isFifo() {
            return useFifo;
        }
    });
    latch = new CountDownLatch(1);
    finishLatch = new CountDownLatch(3);
}
Also used : Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) CountDownLatch(java.util.concurrent.CountDownLatch) Before(org.junit.Before)

Example 23 with Lifecycle

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

the class AsyncQueryForwardingServletTest method setup.

@Before
public void setup() throws Exception {
    setProperties();
    Injector injector = setupInjector();
    final DruidNode node = injector.getInstance(Key.get(DruidNode.class, Self.class));
    port = node.getPort();
    port1 = SocketUtil.findOpenPortFrom(port + 1);
    port2 = SocketUtil.findOpenPortFrom(port1 + 1);
    lifecycle = injector.getInstance(Lifecycle.class);
    lifecycle.start();
    ClientHolder holder = injector.getInstance(ClientHolder.class);
    client = holder.getClient();
}
Also used : Injector(com.google.inject.Injector) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) Self(io.druid.guice.annotations.Self) Before(org.junit.Before)

Example 24 with Lifecycle

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

the class GuiceRunnable method initLifecycle.

public Lifecycle initLifecycle(Injector injector) {
    try {
        LogLevelAdjuster.register();
        final Lifecycle lifecycle = injector.getInstance(Lifecycle.class);
        final StartupLoggingConfig startupLoggingConfig = injector.getInstance(StartupLoggingConfig.class);
        log.info("Starting up with processors[%,d], memory[%,d].", Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().totalMemory());
        if (startupLoggingConfig.isLogProperties()) {
            final Set<String> maskProperties = Sets.newHashSet(startupLoggingConfig.getMaskProperties());
            final Properties props = injector.getInstance(Properties.class);
            for (String propertyName : Ordering.natural().sortedCopy(props.stringPropertyNames())) {
                String property = props.getProperty(propertyName);
                for (String masked : maskProperties) {
                    if (propertyName.contains(masked)) {
                        property = "<masked>";
                        break;
                    }
                }
                log.info("* %s: %s", propertyName, property);
            }
        }
        try {
            lifecycle.start();
        } catch (Throwable t) {
            log.error(t, "Error when starting up.  Failing.");
            System.exit(1);
        }
        return lifecycle;
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : StartupLoggingConfig(io.druid.server.log.StartupLoggingConfig) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) Properties(java.util.Properties)

Example 25 with Lifecycle

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

the class ServerRunnable method run.

@Override
public void run() {
    final Injector injector = makeInjector();
    final Lifecycle lifecycle = initLifecycle(injector);
    try {
        lifecycle.join();
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Injector(com.google.inject.Injector) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle)

Aggregations

Lifecycle (io.druid.java.util.common.lifecycle.Lifecycle)25 Injector (com.google.inject.Injector)10 Before (org.junit.Before)9 Test (org.junit.Test)8 Module (com.google.inject.Module)6 Binder (com.google.inject.Binder)5 NoopServiceEmitter (io.druid.server.metrics.NoopServiceEmitter)5 ExtractionNamespaceCacheFactory (io.druid.query.lookup.namespace.ExtractionNamespaceCacheFactory)4 IOException (java.io.IOException)4 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 ManageLifecycle (io.druid.guice.ManageLifecycle)3 ExtractionNamespace (io.druid.query.lookup.namespace.ExtractionNamespace)3 OnHeapNamespaceExtractionCacheManager (io.druid.server.lookup.namespace.cache.OnHeapNamespaceExtractionCacheManager)3 ExecutionException (java.util.concurrent.ExecutionException)3 Provides (com.google.inject.Provides)2 Self (io.druid.guice.annotations.Self)2 Sequence (io.druid.java.util.common.guava.Sequence)2 CountAggregatorFactory (io.druid.query.aggregation.CountAggregatorFactory)2 JDBCExtractionNamespace (io.druid.query.lookup.namespace.JDBCExtractionNamespace)2 URIExtractionNamespace (io.druid.query.lookup.namespace.URIExtractionNamespace)2