Search in sources :

Example 1 with Lifecycle

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

the class CuratorModule method makeCurator.

@Provides
@LazySingleton
public CuratorFramework makeCurator(CuratorConfig config, EnsembleProvider ensembleProvider, Lifecycle lifecycle) throws IOException {
    final CuratorFramework framework = CuratorFrameworkFactory.builder().ensembleProvider(ensembleProvider).sessionTimeoutMs(config.getZkSessionTimeoutMs()).retryPolicy(new BoundedExponentialBackoffRetry(BASE_SLEEP_TIME_MS, MAX_SLEEP_TIME_MS, MAX_RETRIES)).compressionProvider(new PotentiallyGzippedCompressionProvider(config.getEnableCompression())).aclProvider(config.getEnableAcl() ? new SecuredACLProvider() : new DefaultACLProvider()).build();
    lifecycle.addHandler(new Lifecycle.Handler() {

        @Override
        public void start() throws Exception {
            log.info("Starting Curator");
            framework.start();
        }

        @Override
        public void stop() {
            log.info("Stopping Curator");
            framework.close();
        }
    });
    return framework;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) DefaultACLProvider(org.apache.curator.framework.imps.DefaultACLProvider) IOException(java.io.IOException) BoundedExponentialBackoffRetry(org.apache.curator.retry.BoundedExponentialBackoffRetry) LazySingleton(io.druid.guice.LazySingleton) Provides(com.google.inject.Provides)

Example 2 with Lifecycle

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

the class JettyServerModule method initializeServer.

static void initializeServer(Injector injector, Lifecycle lifecycle, final Server server) {
    JettyServerInitializer initializer = injector.getInstance(JettyServerInitializer.class);
    try {
        initializer.initialize(server, injector);
    } catch (ConfigurationException e) {
        throw new ProvisionException(Iterables.getFirst(e.getErrorMessages(), null).getMessage());
    }
    lifecycle.addHandler(new Lifecycle.Handler() {

        @Override
        public void start() throws Exception {
            server.start();
        }

        @Override
        public void stop() {
            try {
                server.stop();
            } catch (Exception e) {
                log.warn(e, "Unable to stop Jetty server.");
            }
        }
    });
}
Also used : ProvisionException(com.google.inject.ProvisionException) ConfigurationException(com.google.inject.ConfigurationException) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) ServletException(javax.servlet.ServletException) ProvisionException(com.google.inject.ProvisionException) ConfigurationException(com.google.inject.ConfigurationException)

Example 3 with Lifecycle

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

the class MockMemcachedClient method testBasicInjection.

@Test
public void testBasicInjection() throws Exception {
    final MemcachedCacheConfig config = new MemcachedCacheConfig() {

        @Override
        public String getHosts() {
            return "127.0.0.1:22";
        }
    };
    Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.of(new Module() {

        @Override
        public void configure(Binder binder) {
            binder.bindConstant().annotatedWith(Names.named("serviceName")).to("druid/test/memcached");
            binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
            binder.bind(MemcachedCacheConfig.class).toInstance(config);
            binder.bind(Cache.class).toProvider(MemcachedProviderWithConfig.class).in(ManageLifecycle.class);
        }
    }));
    Lifecycle lifecycle = injector.getInstance(Lifecycle.class);
    lifecycle.start();
    try {
        Cache cache = injector.getInstance(Cache.class);
        Assert.assertEquals(MemcachedCache.class, cache.getClass());
    } finally {
        lifecycle.stop();
    }
}
Also used : Binder(com.google.inject.Binder) Injector(com.google.inject.Injector) ManageLifecycle(io.druid.guice.ManageLifecycle) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) Module(com.google.inject.Module) Test(org.junit.Test)

Example 4 with Lifecycle

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

the class ZkCoordinatorTest method testInjector.

@Test
public void testInjector() throws Exception {
    Injector injector = Guice.createInjector(new Module() {

        @Override
        public void configure(Binder binder) {
            binder.bind(ObjectMapper.class).toInstance(jsonMapper);
            binder.bind(SegmentLoaderConfig.class).toInstance(new SegmentLoaderConfig() {

                @Override
                public File getInfoDir() {
                    return infoDir;
                }

                @Override
                public int getNumLoadingThreads() {
                    return 5;
                }

                @Override
                public int getAnnounceIntervalMillis() {
                    return 50;
                }
            });
            binder.bind(ZkPathsConfig.class).toInstance(new ZkPathsConfig() {

                @Override
                public String getBase() {
                    return "/druid";
                }
            });
            binder.bind(DruidServerMetadata.class).toInstance(new DruidServerMetadata("dummyServer", "dummyHost", 0, "dummyType", "normal", 0));
            binder.bind(DataSegmentAnnouncer.class).toInstance(announcer);
            binder.bind(CuratorFramework.class).toInstance(curator);
            binder.bind(ServerManager.class).toInstance(serverManager);
            binder.bind(ScheduledExecutorFactory.class).toInstance(ScheduledExecutors.createFactory(new Lifecycle()));
        }
    });
    ZkCoordinator zkCoordinator = injector.getInstance(ZkCoordinator.class);
    List<DataSegment> segments = Lists.newLinkedList();
    for (int i = 0; i < COUNT; ++i) {
        segments.add(makeSegment("test" + i, "1", new Interval("P1d/2011-04-01")));
        segments.add(makeSegment("test" + i, "1", new Interval("P1d/2011-04-02")));
        segments.add(makeSegment("test" + i, "2", new Interval("P1d/2011-04-02")));
        segments.add(makeSegment("test_two" + i, "1", new Interval("P1d/2011-04-01")));
        segments.add(makeSegment("test_two" + i, "1", new Interval("P1d/2011-04-02")));
    }
    Collections.sort(segments);
    for (DataSegment segment : segments) {
        writeSegmentToCache(segment);
    }
    checkCache(segments);
    Assert.assertTrue(serverManager.getDataSourceCounts().isEmpty());
    zkCoordinator.start();
    Assert.assertTrue(!serverManager.getDataSourceCounts().isEmpty());
    for (int i = 0; i < COUNT; ++i) {
        Assert.assertEquals(3L, serverManager.getDataSourceCounts().get("test" + i).longValue());
        Assert.assertEquals(2L, serverManager.getDataSourceCounts().get("test_two" + i).longValue());
    }
    Assert.assertEquals(5 * COUNT, announceCount.get());
    zkCoordinator.stop();
    for (DataSegment segment : segments) {
        deleteSegmentFromCache(segment);
    }
    Assert.assertEquals(0, infoDir.listFiles().length);
    Assert.assertTrue(infoDir.delete());
}
Also used : Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) DataSegment(io.druid.timeline.DataSegment) Binder(com.google.inject.Binder) Injector(com.google.inject.Injector) ZkPathsConfig(io.druid.server.initialization.ZkPathsConfig) SegmentLoaderConfig(io.druid.segment.loading.SegmentLoaderConfig) Module(com.google.inject.Module) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 5 with Lifecycle

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

the class CliPeon method run.

@Override
public void run() {
    try {
        Injector injector = makeInjector();
        try {
            final Lifecycle lifecycle = initLifecycle(injector);
            final Thread hook = new Thread(new Runnable() {

                @Override
                public void run() {
                    log.info("Running shutdown hook");
                    lifecycle.stop();
                }
            });
            Runtime.getRuntime().addShutdownHook(hook);
            injector.getInstance(ExecutorLifecycle.class).join();
            // Sanity check to help debug unexpected non-daemon threads
            final Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
            for (Thread thread : threadSet) {
                if (!thread.isDaemon() && thread != Thread.currentThread()) {
                    log.info("Thread [%s] is non daemon.", thread);
                }
            }
            // Explicitly call lifecycle stop, dont rely on shutdown hook.
            lifecycle.stop();
            Runtime.getRuntime().removeShutdownHook(hook);
        } catch (Throwable t) {
            log.error(t, "Error when starting up.  Failing.");
            System.exit(1);
        }
        log.info("Finished peon task");
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Injector(com.google.inject.Injector) ManageLifecycle(io.druid.guice.ManageLifecycle) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) ExecutorLifecycle(io.druid.indexing.worker.executor.ExecutorLifecycle) ExecutorLifecycle(io.druid.indexing.worker.executor.ExecutorLifecycle) IOException(java.io.IOException)

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