From 02ffd805c8f3f90afa6e8ac3b790c29235ed8721 Mon Sep 17 00:00:00 2001 From: cheddar Date: Tue, 23 Jul 2013 14:55:36 -0700 Subject: [PATCH] 1) Move DbConnector into its own module 2) Move JacksonConfig stuff into its own module 3) Fix LogEmitterModule to inject properly with a Supplier<> 4) Set Guice to require explicit bindings, to protect from weirdness like was initially experienced with LoggingEmitterConfig 5) Master fires up and appears to all work with Guice! Whoot! --- .../druid/initialization/EmitterModule.java | 3 +- .../initialization/HttpEmitterModule.java | 3 +- .../druid/initialization/Initialization.java | 17 ++++- .../initialization/LogEmitterModule.java | 9 ++- .../druid/config/ConfigManagerProvider.java | 74 ------------------- .../metamx/druid/guice/DbConnectorModule.java | 29 ++++++++ .../druid/guice/DruidGuiceExtensions.java | 1 + .../guice/JacksonConfigManagerModule.java | 52 +++++++++++++ .../metamx/druid/guice/ManageLifecycle.java | 2 + pom.xml | 2 +- .../com/metamx/druid/guice/MasterModule.java | 25 ++----- .../com/metamx/druid/http/MasterMain.java | 4 + 12 files changed, 119 insertions(+), 102 deletions(-) delete mode 100644 common/src/main/java/com/metamx/druid/config/ConfigManagerProvider.java create mode 100644 common/src/main/java/com/metamx/druid/guice/DbConnectorModule.java create mode 100644 common/src/main/java/com/metamx/druid/guice/JacksonConfigManagerModule.java diff --git a/client/src/main/java/com/metamx/druid/initialization/EmitterModule.java b/client/src/main/java/com/metamx/druid/initialization/EmitterModule.java index 7b2602efb3a..c3b1259934e 100644 --- a/client/src/main/java/com/metamx/druid/initialization/EmitterModule.java +++ b/client/src/main/java/com/metamx/druid/initialization/EmitterModule.java @@ -34,6 +34,7 @@ import com.google.inject.name.Names; import com.metamx.common.ISE; import com.metamx.common.logger.Logger; import com.metamx.druid.guice.LazySingleton; +import com.metamx.druid.guice.ManageLifecycle; import com.metamx.emitter.EmittingLogger; import com.metamx.emitter.core.Emitter; import com.metamx.emitter.service.ServiceEmitter; @@ -71,7 +72,7 @@ public class EmitterModule implements Module } @Provides - @LazySingleton + @ManageLifecycle public ServiceEmitter getServiceEmitter(Supplier configSupplier, Emitter emitter) { final DruidNodeConfig config = configSupplier.get(); diff --git a/client/src/main/java/com/metamx/druid/initialization/HttpEmitterModule.java b/client/src/main/java/com/metamx/druid/initialization/HttpEmitterModule.java index 9f9c5eb5de0..7c2c9f2ca69 100644 --- a/client/src/main/java/com/metamx/druid/initialization/HttpEmitterModule.java +++ b/client/src/main/java/com/metamx/druid/initialization/HttpEmitterModule.java @@ -28,6 +28,7 @@ import com.google.inject.util.Providers; import com.metamx.common.lifecycle.Lifecycle; import com.metamx.druid.guice.JsonConfigProvider; import com.metamx.druid.guice.LazySingleton; +import com.metamx.druid.guice.ManageLifecycle; import com.metamx.emitter.core.Emitter; import com.metamx.emitter.core.HttpPostEmitter; import com.metamx.http.client.HttpClientConfig; @@ -49,7 +50,7 @@ public class HttpEmitterModule implements Module binder.bind(SSLContext.class).toProvider(Providers.of(null)).in(LazySingleton.class); } - @Provides @LazySingleton @Named("http") + @Provides @ManageLifecycle @Named("http") public Emitter getEmitter(Supplier config, @Nullable SSLContext sslContext, Lifecycle lifecycle) { final HttpClientConfig.Builder builder = HttpClientConfig diff --git a/client/src/main/java/com/metamx/druid/initialization/Initialization.java b/client/src/main/java/com/metamx/druid/initialization/Initialization.java index aa82268290e..8d9e90a1069 100644 --- a/client/src/main/java/com/metamx/druid/initialization/Initialization.java +++ b/client/src/main/java/com/metamx/druid/initialization/Initialization.java @@ -25,6 +25,7 @@ import com.google.common.base.Function; import com.google.common.base.Throwables; import com.google.common.collect.Lists; import com.google.common.io.Closeables; +import com.google.inject.Binder; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Module; @@ -390,7 +391,21 @@ public class Initialization new DruidGuiceExtensions(), new JacksonModule(), new PropertiesModule("runtime.properties"), - new ConfigModule() + new ConfigModule(), + new Module() + { + @Override + public void configure(Binder binder) + { + binder.bind(DruidSecondaryModule.class); + + for (Object module : modules) { + if (module instanceof Class) { + binder.bind((Class) module); + } + } + } + } ); List actualModules = Lists.newArrayList(); diff --git a/client/src/main/java/com/metamx/druid/initialization/LogEmitterModule.java b/client/src/main/java/com/metamx/druid/initialization/LogEmitterModule.java index e64c259098b..5e9b7bd71a7 100644 --- a/client/src/main/java/com/metamx/druid/initialization/LogEmitterModule.java +++ b/client/src/main/java/com/metamx/druid/initialization/LogEmitterModule.java @@ -20,12 +20,13 @@ package com.metamx.druid.initialization; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Supplier; import com.google.inject.Binder; import com.google.inject.Module; import com.google.inject.Provides; import com.google.inject.name.Named; import com.metamx.druid.guice.JsonConfigProvider; -import com.metamx.druid.guice.LazySingleton; +import com.metamx.druid.guice.ManageLifecycle; import com.metamx.emitter.core.Emitter; import com.metamx.emitter.core.LoggingEmitter; import com.metamx.emitter.core.LoggingEmitterConfig; @@ -42,9 +43,9 @@ public class LogEmitterModule implements Module JsonConfigProvider.bind(binder, "druid.emitter.logging", LoggingEmitterConfig.class); } - @Provides @LazySingleton @Named(EMITTER_TYPE) - public Emitter makeEmitter(LoggingEmitterConfig config, ObjectMapper jsonMapper) + @Provides @ManageLifecycle @Named(EMITTER_TYPE) + public Emitter makeEmitter(Supplier config, ObjectMapper jsonMapper) { - return new LoggingEmitter(config, jsonMapper); + return new LoggingEmitter(config.get(), jsonMapper); } } diff --git a/common/src/main/java/com/metamx/druid/config/ConfigManagerProvider.java b/common/src/main/java/com/metamx/druid/config/ConfigManagerProvider.java deleted file mode 100644 index 032d2668152..00000000000 --- a/common/src/main/java/com/metamx/druid/config/ConfigManagerProvider.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Druid - a distributed column store. - * Copyright (C) 2012, 2013 Metamarkets Group Inc. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package com.metamx.druid.config; - -import com.google.common.base.Supplier; -import com.google.inject.Inject; -import com.google.inject.Provider; -import com.metamx.common.lifecycle.Lifecycle; -import com.metamx.druid.db.DbConnector; -import com.metamx.druid.db.DbTablesConfig; - -/** - */ -public class ConfigManagerProvider implements Provider -{ - private final DbConnector dbConnector; - private final Supplier dbTables; - private final Supplier config; - private final Lifecycle lifecycle; - - @Inject - ConfigManagerProvider( - DbConnector dbConnector, - Supplier dbTables, - Supplier config, - Lifecycle lifecycle - ) - { - this.dbConnector = dbConnector; - this.dbTables = dbTables; - this.config = config; - this.lifecycle = lifecycle; - } - - @Override - public ConfigManager get() - { - lifecycle.addHandler( - new Lifecycle.Handler() - { - @Override - public void start() throws Exception - { - dbConnector.createConfigTable(); - } - - @Override - public void stop() - { - - } - } - ); - - return new ConfigManager(dbConnector.getDBI(), dbTables, config); - } -} diff --git a/common/src/main/java/com/metamx/druid/guice/DbConnectorModule.java b/common/src/main/java/com/metamx/druid/guice/DbConnectorModule.java new file mode 100644 index 00000000000..624bd030175 --- /dev/null +++ b/common/src/main/java/com/metamx/druid/guice/DbConnectorModule.java @@ -0,0 +1,29 @@ +package com.metamx.druid.guice; + +import com.google.inject.Binder; +import com.google.inject.Module; +import com.google.inject.Provides; +import com.metamx.druid.db.DbConnector; +import com.metamx.druid.db.DbConnectorConfig; +import com.metamx.druid.db.DbTablesConfig; +import org.skife.jdbi.v2.IDBI; + +/** + */ +public class DbConnectorModule implements Module +{ + @Override + public void configure(Binder binder) + { + JsonConfigProvider.bind(binder, "druid.db.tables", DbTablesConfig.class); + JsonConfigProvider.bind(binder, "druid.db.connector", DbConnectorConfig.class); + + binder.bind(DbConnector.class); + } + + @Provides @LazySingleton + public IDBI getDbi(final DbConnector dbConnector) + { + return dbConnector.getDBI(); + } +} diff --git a/common/src/main/java/com/metamx/druid/guice/DruidGuiceExtensions.java b/common/src/main/java/com/metamx/druid/guice/DruidGuiceExtensions.java index e999b725aa2..1010e56188c 100644 --- a/common/src/main/java/com/metamx/druid/guice/DruidGuiceExtensions.java +++ b/common/src/main/java/com/metamx/druid/guice/DruidGuiceExtensions.java @@ -10,6 +10,7 @@ public class DruidGuiceExtensions implements Module @Override public void configure(Binder binder) { + binder.requireExplicitBindings(); binder.bindScope(LazySingleton.class, DruidScopes.SINGLETON); } } diff --git a/common/src/main/java/com/metamx/druid/guice/JacksonConfigManagerModule.java b/common/src/main/java/com/metamx/druid/guice/JacksonConfigManagerModule.java new file mode 100644 index 00000000000..05838c04383 --- /dev/null +++ b/common/src/main/java/com/metamx/druid/guice/JacksonConfigManagerModule.java @@ -0,0 +1,52 @@ +package com.metamx.druid.guice; + +import com.google.common.base.Supplier; +import com.google.inject.Binder; +import com.google.inject.Module; +import com.google.inject.Provides; +import com.metamx.common.lifecycle.Lifecycle; +import com.metamx.druid.config.ConfigManager; +import com.metamx.druid.config.ConfigManagerConfig; +import com.metamx.druid.config.JacksonConfigManager; +import com.metamx.druid.db.DbConnector; +import com.metamx.druid.db.DbTablesConfig; + +/** + */ +public class JacksonConfigManagerModule implements Module +{ + @Override + public void configure(Binder binder) + { + JsonConfigProvider.bind(binder, "druid.manager.config", ConfigManagerConfig.class); + binder.bind(JacksonConfigManager.class); + } + + @Provides @ManageLifecycle + public ConfigManager getConfigManager( + final DbConnector dbConnector, + final Supplier dbTables, + final Supplier config, + final Lifecycle lifecycle + ) + { + lifecycle.addHandler( + new Lifecycle.Handler() + { + @Override + public void start() throws Exception + { + dbConnector.createConfigTable(); + } + + @Override + public void stop() + { + + } + } + ); + + return new ConfigManager(dbConnector.getDBI(), dbTables, config); + } +} diff --git a/common/src/main/java/com/metamx/druid/guice/ManageLifecycle.java b/common/src/main/java/com/metamx/druid/guice/ManageLifecycle.java index 35752a64b0d..24e269ae00b 100644 --- a/common/src/main/java/com/metamx/druid/guice/ManageLifecycle.java +++ b/common/src/main/java/com/metamx/druid/guice/ManageLifecycle.java @@ -10,6 +10,8 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; /** * Marks the object to be managed by {@link com.metamx.common.lifecycle.Lifecycle} + * + * This Scope gets defined by {@link com.metamx.druid.guice.LifecycleModule} */ @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RUNTIME) diff --git a/pom.xml b/pom.xml index 206dfd9d74c..95436f18d3f 100644 --- a/pom.xml +++ b/pom.xml @@ -61,7 +61,7 @@ com.metamx emitter - 0.2.3 + 0.2.5-SNAPSHOT com.metamx diff --git a/server/src/main/java/com/metamx/druid/guice/MasterModule.java b/server/src/main/java/com/metamx/druid/guice/MasterModule.java index 1a3b693fc94..5a4f38d0917 100644 --- a/server/src/main/java/com/metamx/druid/guice/MasterModule.java +++ b/server/src/main/java/com/metamx/druid/guice/MasterModule.java @@ -8,6 +8,7 @@ import com.google.inject.TypeLiteral; import com.metamx.common.concurrent.ScheduledExecutorFactory; import com.metamx.common.concurrent.ScheduledExecutors; import com.metamx.common.lifecycle.Lifecycle; +import com.metamx.druid.client.ServerInventoryView; import com.metamx.druid.client.ServerInventoryViewConfig; import com.metamx.druid.client.indexing.IndexingService; import com.metamx.druid.client.indexing.IndexingServiceClient; @@ -15,28 +16,22 @@ import com.metamx.druid.client.indexing.IndexingServiceSelector; import com.metamx.druid.client.selector.DiscoverySelector; import com.metamx.druid.client.selector.Server; import com.metamx.druid.concurrent.Execs; -import com.metamx.druid.config.ConfigManager; -import com.metamx.druid.config.ConfigManagerConfig; -import com.metamx.druid.config.ConfigManagerProvider; import com.metamx.druid.db.DatabaseRuleManager; import com.metamx.druid.db.DatabaseRuleManagerConfig; import com.metamx.druid.db.DatabaseRuleManagerProvider; import com.metamx.druid.db.DatabaseSegmentManager; import com.metamx.druid.db.DatabaseSegmentManagerConfig; import com.metamx.druid.db.DatabaseSegmentManagerProvider; -import com.metamx.druid.db.DbConnector; -import com.metamx.druid.db.DbConnectorConfig; -import com.metamx.druid.db.DbTablesConfig; import com.metamx.druid.http.MasterRedirectInfo; import com.metamx.druid.http.RedirectInfo; import com.metamx.druid.initialization.ZkPathsConfig; +import com.metamx.druid.master.DruidMaster; import com.metamx.druid.master.DruidMasterConfig; import com.metamx.druid.master.LoadQueueTaskMaster; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.x.discovery.ServiceDiscovery; import org.apache.curator.x.discovery.ServiceInstance; import org.apache.curator.x.discovery.ServiceProvider; -import org.skife.jdbi.v2.IDBI; import java.io.IOException; @@ -51,12 +46,12 @@ public class MasterModule implements Module ConfigProvider.bind(binder, ZkPathsConfig.class); ConfigProvider.bind(binder, ServerInventoryViewConfig.class); - JsonConfigProvider.bind(binder, "druid.db.tables", DbTablesConfig.class); - JsonConfigProvider.bind(binder, "druid.db.connector", DbConnectorConfig.class); - JsonConfigProvider.bind(binder, "druid.manager.config", ConfigManagerConfig.class); JsonConfigProvider.bind(binder, "druid.manager.segment", DatabaseSegmentManagerConfig.class); JsonConfigProvider.bind(binder, "druid.manager.rules", DatabaseRuleManagerConfig.class); + binder.bind(DruidMaster.class); + binder.bind(ServerInventoryView.class); + binder.bind(DatabaseSegmentManager.class) .toProvider(DatabaseSegmentManagerProvider.class) .in(ManageLifecycle.class); @@ -65,10 +60,6 @@ public class MasterModule implements Module .toProvider(DatabaseRuleManagerProvider.class) .in(ManageLifecycle.class); - binder.bind(ConfigManager.class) - .toProvider(ConfigManagerProvider.class) - .in(ManageLifecycle.class); - binder.bind(new TypeLiteral>(){}) .annotatedWith(IndexingService.class) .to(IndexingServiceSelector.class) @@ -107,12 +98,6 @@ public class MasterModule implements Module return serviceDiscovery.serviceProviderBuilder().serviceName(config.getMergerServiceName()).build(); } - @Provides @LazySingleton - public IDBI getDbi(final DbConnector dbConnector) - { - return dbConnector.getDBI(); - } - @Provides @LazySingleton public LoadQueueTaskMaster getLoadQueueTaskMaster(CuratorFramework curator, ObjectMapper jsonMapper) { diff --git a/server/src/main/java/com/metamx/druid/http/MasterMain.java b/server/src/main/java/com/metamx/druid/http/MasterMain.java index aef21c936b2..bc4fc6189f6 100644 --- a/server/src/main/java/com/metamx/druid/http/MasterMain.java +++ b/server/src/main/java/com/metamx/druid/http/MasterMain.java @@ -29,7 +29,9 @@ import com.metamx.common.logger.Logger; import com.metamx.druid.curator.CuratorModule; import com.metamx.druid.curator.discovery.DiscoveryModule; import com.metamx.druid.curator.discovery.ServiceAnnouncer; +import com.metamx.druid.guice.DbConnectorModule; import com.metamx.druid.guice.HttpClientModule; +import com.metamx.druid.guice.JacksonConfigManagerModule; import com.metamx.druid.guice.LifecycleModule; import com.metamx.druid.guice.MasterModule; import com.metamx.druid.guice.ServerModule; @@ -63,6 +65,8 @@ public class MasterMain new LifecycleModule(Key.get(MonitorScheduler.class), Key.get(DruidMaster.class)), EmitterModule.class, HttpClientModule.class, + DbConnectorModule.class, + JacksonConfigManagerModule.class, CuratorModule.class, new MetricsModule(), DiscoveryModule.class,