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;
}
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.");
}
}
});
}
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();
}
}
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());
}
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);
}
}
Aggregations