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!
This commit is contained in:
cheddar 2013-07-23 14:55:36 -07:00
parent 85a35201b7
commit 02ffd805c8
12 changed files with 119 additions and 102 deletions

View File

@ -34,6 +34,7 @@ import com.google.inject.name.Names;
import com.metamx.common.ISE; import com.metamx.common.ISE;
import com.metamx.common.logger.Logger; import com.metamx.common.logger.Logger;
import com.metamx.druid.guice.LazySingleton; import com.metamx.druid.guice.LazySingleton;
import com.metamx.druid.guice.ManageLifecycle;
import com.metamx.emitter.EmittingLogger; import com.metamx.emitter.EmittingLogger;
import com.metamx.emitter.core.Emitter; import com.metamx.emitter.core.Emitter;
import com.metamx.emitter.service.ServiceEmitter; import com.metamx.emitter.service.ServiceEmitter;
@ -71,7 +72,7 @@ public class EmitterModule implements Module
} }
@Provides @Provides
@LazySingleton @ManageLifecycle
public ServiceEmitter getServiceEmitter(Supplier<DruidNodeConfig> configSupplier, Emitter emitter) public ServiceEmitter getServiceEmitter(Supplier<DruidNodeConfig> configSupplier, Emitter emitter)
{ {
final DruidNodeConfig config = configSupplier.get(); final DruidNodeConfig config = configSupplier.get();

View File

@ -28,6 +28,7 @@ import com.google.inject.util.Providers;
import com.metamx.common.lifecycle.Lifecycle; import com.metamx.common.lifecycle.Lifecycle;
import com.metamx.druid.guice.JsonConfigProvider; import com.metamx.druid.guice.JsonConfigProvider;
import com.metamx.druid.guice.LazySingleton; import com.metamx.druid.guice.LazySingleton;
import com.metamx.druid.guice.ManageLifecycle;
import com.metamx.emitter.core.Emitter; import com.metamx.emitter.core.Emitter;
import com.metamx.emitter.core.HttpPostEmitter; import com.metamx.emitter.core.HttpPostEmitter;
import com.metamx.http.client.HttpClientConfig; import com.metamx.http.client.HttpClientConfig;
@ -49,7 +50,7 @@ public class HttpEmitterModule implements Module
binder.bind(SSLContext.class).toProvider(Providers.<SSLContext>of(null)).in(LazySingleton.class); binder.bind(SSLContext.class).toProvider(Providers.<SSLContext>of(null)).in(LazySingleton.class);
} }
@Provides @LazySingleton @Named("http") @Provides @ManageLifecycle @Named("http")
public Emitter getEmitter(Supplier<HttpEmitterConfig> config, @Nullable SSLContext sslContext, Lifecycle lifecycle) public Emitter getEmitter(Supplier<HttpEmitterConfig> config, @Nullable SSLContext sslContext, Lifecycle lifecycle)
{ {
final HttpClientConfig.Builder builder = HttpClientConfig final HttpClientConfig.Builder builder = HttpClientConfig

View File

@ -25,6 +25,7 @@ import com.google.common.base.Function;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.io.Closeables; import com.google.common.io.Closeables;
import com.google.inject.Binder;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.google.inject.Module; import com.google.inject.Module;
@ -390,7 +391,21 @@ public class Initialization
new DruidGuiceExtensions(), new DruidGuiceExtensions(),
new JacksonModule(), new JacksonModule(),
new PropertiesModule("runtime.properties"), 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<Object> actualModules = Lists.newArrayList(); List<Object> actualModules = Lists.newArrayList();

View File

@ -20,12 +20,13 @@
package com.metamx.druid.initialization; package com.metamx.druid.initialization;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Supplier;
import com.google.inject.Binder; import com.google.inject.Binder;
import com.google.inject.Module; import com.google.inject.Module;
import com.google.inject.Provides; import com.google.inject.Provides;
import com.google.inject.name.Named; import com.google.inject.name.Named;
import com.metamx.druid.guice.JsonConfigProvider; 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.Emitter;
import com.metamx.emitter.core.LoggingEmitter; import com.metamx.emitter.core.LoggingEmitter;
import com.metamx.emitter.core.LoggingEmitterConfig; import com.metamx.emitter.core.LoggingEmitterConfig;
@ -42,9 +43,9 @@ public class LogEmitterModule implements Module
JsonConfigProvider.bind(binder, "druid.emitter.logging", LoggingEmitterConfig.class); JsonConfigProvider.bind(binder, "druid.emitter.logging", LoggingEmitterConfig.class);
} }
@Provides @LazySingleton @Named(EMITTER_TYPE) @Provides @ManageLifecycle @Named(EMITTER_TYPE)
public Emitter makeEmitter(LoggingEmitterConfig config, ObjectMapper jsonMapper) public Emitter makeEmitter(Supplier<LoggingEmitterConfig> config, ObjectMapper jsonMapper)
{ {
return new LoggingEmitter(config, jsonMapper); return new LoggingEmitter(config.get(), jsonMapper);
} }
} }

View File

@ -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<ConfigManager>
{
private final DbConnector dbConnector;
private final Supplier<DbTablesConfig> dbTables;
private final Supplier<ConfigManagerConfig> config;
private final Lifecycle lifecycle;
@Inject
ConfigManagerProvider(
DbConnector dbConnector,
Supplier<DbTablesConfig> dbTables,
Supplier<ConfigManagerConfig> 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);
}
}

View File

@ -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();
}
}

View File

@ -10,6 +10,7 @@ public class DruidGuiceExtensions implements Module
@Override @Override
public void configure(Binder binder) public void configure(Binder binder)
{ {
binder.requireExplicitBindings();
binder.bindScope(LazySingleton.class, DruidScopes.SINGLETON); binder.bindScope(LazySingleton.class, DruidScopes.SINGLETON);
} }
} }

View File

@ -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<DbTablesConfig> dbTables,
final Supplier<ConfigManagerConfig> 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);
}
}

View File

@ -10,6 +10,8 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
/** /**
* Marks the object to be managed by {@link com.metamx.common.lifecycle.Lifecycle} * 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 }) @Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RUNTIME) @Retention(RUNTIME)

View File

@ -61,7 +61,7 @@
<dependency> <dependency>
<groupId>com.metamx</groupId> <groupId>com.metamx</groupId>
<artifactId>emitter</artifactId> <artifactId>emitter</artifactId>
<version>0.2.3</version> <version>0.2.5-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.metamx</groupId> <groupId>com.metamx</groupId>

View File

@ -8,6 +8,7 @@ import com.google.inject.TypeLiteral;
import com.metamx.common.concurrent.ScheduledExecutorFactory; import com.metamx.common.concurrent.ScheduledExecutorFactory;
import com.metamx.common.concurrent.ScheduledExecutors; import com.metamx.common.concurrent.ScheduledExecutors;
import com.metamx.common.lifecycle.Lifecycle; import com.metamx.common.lifecycle.Lifecycle;
import com.metamx.druid.client.ServerInventoryView;
import com.metamx.druid.client.ServerInventoryViewConfig; import com.metamx.druid.client.ServerInventoryViewConfig;
import com.metamx.druid.client.indexing.IndexingService; import com.metamx.druid.client.indexing.IndexingService;
import com.metamx.druid.client.indexing.IndexingServiceClient; 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.DiscoverySelector;
import com.metamx.druid.client.selector.Server; import com.metamx.druid.client.selector.Server;
import com.metamx.druid.concurrent.Execs; 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.DatabaseRuleManager;
import com.metamx.druid.db.DatabaseRuleManagerConfig; import com.metamx.druid.db.DatabaseRuleManagerConfig;
import com.metamx.druid.db.DatabaseRuleManagerProvider; import com.metamx.druid.db.DatabaseRuleManagerProvider;
import com.metamx.druid.db.DatabaseSegmentManager; import com.metamx.druid.db.DatabaseSegmentManager;
import com.metamx.druid.db.DatabaseSegmentManagerConfig; import com.metamx.druid.db.DatabaseSegmentManagerConfig;
import com.metamx.druid.db.DatabaseSegmentManagerProvider; 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.MasterRedirectInfo;
import com.metamx.druid.http.RedirectInfo; import com.metamx.druid.http.RedirectInfo;
import com.metamx.druid.initialization.ZkPathsConfig; import com.metamx.druid.initialization.ZkPathsConfig;
import com.metamx.druid.master.DruidMaster;
import com.metamx.druid.master.DruidMasterConfig; import com.metamx.druid.master.DruidMasterConfig;
import com.metamx.druid.master.LoadQueueTaskMaster; import com.metamx.druid.master.LoadQueueTaskMaster;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.x.discovery.ServiceDiscovery; import org.apache.curator.x.discovery.ServiceDiscovery;
import org.apache.curator.x.discovery.ServiceInstance; import org.apache.curator.x.discovery.ServiceInstance;
import org.apache.curator.x.discovery.ServiceProvider; import org.apache.curator.x.discovery.ServiceProvider;
import org.skife.jdbi.v2.IDBI;
import java.io.IOException; import java.io.IOException;
@ -51,12 +46,12 @@ public class MasterModule implements Module
ConfigProvider.bind(binder, ZkPathsConfig.class); ConfigProvider.bind(binder, ZkPathsConfig.class);
ConfigProvider.bind(binder, ServerInventoryViewConfig.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.segment", DatabaseSegmentManagerConfig.class);
JsonConfigProvider.bind(binder, "druid.manager.rules", DatabaseRuleManagerConfig.class); JsonConfigProvider.bind(binder, "druid.manager.rules", DatabaseRuleManagerConfig.class);
binder.bind(DruidMaster.class);
binder.bind(ServerInventoryView.class);
binder.bind(DatabaseSegmentManager.class) binder.bind(DatabaseSegmentManager.class)
.toProvider(DatabaseSegmentManagerProvider.class) .toProvider(DatabaseSegmentManagerProvider.class)
.in(ManageLifecycle.class); .in(ManageLifecycle.class);
@ -65,10 +60,6 @@ public class MasterModule implements Module
.toProvider(DatabaseRuleManagerProvider.class) .toProvider(DatabaseRuleManagerProvider.class)
.in(ManageLifecycle.class); .in(ManageLifecycle.class);
binder.bind(ConfigManager.class)
.toProvider(ConfigManagerProvider.class)
.in(ManageLifecycle.class);
binder.bind(new TypeLiteral<DiscoverySelector<Server>>(){}) binder.bind(new TypeLiteral<DiscoverySelector<Server>>(){})
.annotatedWith(IndexingService.class) .annotatedWith(IndexingService.class)
.to(IndexingServiceSelector.class) .to(IndexingServiceSelector.class)
@ -107,12 +98,6 @@ public class MasterModule implements Module
return serviceDiscovery.serviceProviderBuilder().serviceName(config.getMergerServiceName()).build(); return serviceDiscovery.serviceProviderBuilder().serviceName(config.getMergerServiceName()).build();
} }
@Provides @LazySingleton
public IDBI getDbi(final DbConnector dbConnector)
{
return dbConnector.getDBI();
}
@Provides @LazySingleton @Provides @LazySingleton
public LoadQueueTaskMaster getLoadQueueTaskMaster(CuratorFramework curator, ObjectMapper jsonMapper) public LoadQueueTaskMaster getLoadQueueTaskMaster(CuratorFramework curator, ObjectMapper jsonMapper)
{ {

View File

@ -29,7 +29,9 @@ import com.metamx.common.logger.Logger;
import com.metamx.druid.curator.CuratorModule; import com.metamx.druid.curator.CuratorModule;
import com.metamx.druid.curator.discovery.DiscoveryModule; import com.metamx.druid.curator.discovery.DiscoveryModule;
import com.metamx.druid.curator.discovery.ServiceAnnouncer; import com.metamx.druid.curator.discovery.ServiceAnnouncer;
import com.metamx.druid.guice.DbConnectorModule;
import com.metamx.druid.guice.HttpClientModule; import com.metamx.druid.guice.HttpClientModule;
import com.metamx.druid.guice.JacksonConfigManagerModule;
import com.metamx.druid.guice.LifecycleModule; import com.metamx.druid.guice.LifecycleModule;
import com.metamx.druid.guice.MasterModule; import com.metamx.druid.guice.MasterModule;
import com.metamx.druid.guice.ServerModule; import com.metamx.druid.guice.ServerModule;
@ -63,6 +65,8 @@ public class MasterMain
new LifecycleModule(Key.get(MonitorScheduler.class), Key.get(DruidMaster.class)), new LifecycleModule(Key.get(MonitorScheduler.class), Key.get(DruidMaster.class)),
EmitterModule.class, EmitterModule.class,
HttpClientModule.class, HttpClientModule.class,
DbConnectorModule.class,
JacksonConfigManagerModule.class,
CuratorModule.class, CuratorModule.class,
new MetricsModule(), new MetricsModule(),
DiscoveryModule.class, DiscoveryModule.class,