1) The Master compiles and runs with Guice bindings!!!!

2) The service discovery stuff really needs to be reworked.  It's unhappy now.
This commit is contained in:
cheddar 2013-07-19 16:29:54 -07:00
parent e885cb5a4d
commit 85a35201b7
12 changed files with 92 additions and 45 deletions

View File

@ -265,7 +265,8 @@ public class Initialization
throw Throwables.propagate(e);
}
}
}
},
Lifecycle.Stage.LAST
);
return serviceDiscovery;

View File

@ -76,4 +76,17 @@ public class DbConnectorConfig
public String getValidationQuery() {
return validationQuery;
}
@Override
public String toString()
{
return "DbConnectorConfig{" +
"createTables=" + createTables +
", connectURI='" + connectURI + '\'' +
", user='" + user + '\'' +
", password=****" +
", useValidationQuery=" + useValidationQuery +
", validationQuery='" + validationQuery + '\'' +
'}';
}
}

View File

@ -3,6 +3,7 @@ package com.metamx.druid.guice;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.Binder;
import com.google.inject.Inject;
import com.google.inject.Key;
import com.google.inject.Module;
import com.metamx.druid.jackson.Json;
import com.metamx.druid.jackson.Smile;
@ -46,6 +47,7 @@ public class DruidSecondaryModule implements Module
binder.install(new DruidGuiceExtensions());
binder.bind(Properties.class).toInstance(properties);
binder.bind(ConfigurationObjectFactory.class).toInstance(factory);
binder.bind(ObjectMapper.class).to(Key.get(ObjectMapper.class, Json.class));
binder.bind(ObjectMapper.class).annotatedWith(Json.class).toInstance(jsonMapper);
binder.bind(ObjectMapper.class).annotatedWith(Smile.class).toInstance(smileMapper);
binder.bind(Validator.class).toInstance(validator);

View File

@ -86,7 +86,7 @@ public class JsonConfigProvider<T> implements Provider<Supplier<T>>
}
catch (RuntimeException e) {
// When a runtime exception gets thrown out, this provider will get called again if the object is asked for again.
// This will have the same failed result, 'cause when it's called no parameters will have actually changed. For
// This will have the same failed result, 'cause when it's called no parameters will have actually changed.
// Guice will then report the same error multiple times, which is pretty annoying. Cache a null supplier and
// return that instead. This is technically enforcing a singleton, but such is life.
retVal = Suppliers.ofInstance(null);

View File

@ -52,10 +52,15 @@ public class JsonConfigurator
final String propValue = props.getProperty(prop);
Object value;
try {
value = jsonMapper.readValue(propValue, Object.class);
// If it's a String Jackson wants it to be quoted, so check if it's not an object or array and quote.
String modifiedPropValue = propValue;
if (! (modifiedPropValue.startsWith("[") || modifiedPropValue.startsWith("{"))) {
modifiedPropValue = String.format("\"%s\"", modifiedPropValue);
}
value = jsonMapper.readValue(modifiedPropValue, Object.class);
}
catch (IOException e) {
log.debug("Unable to parse [%s]=[%s] as a json object, using as is.", prop, propValue);
log.info(e, "Unable to parse [%s]=[%s] as a json object, using as is.", prop, propValue);
value = propValue;
}
@ -89,6 +94,8 @@ public class JsonConfigurator
);
}
log.info("Loaded class[%s] as [%s]", clazz, config);
return config;
}
}

View File

@ -62,27 +62,17 @@ public class LifecycleModule implements Module
@Provides @LazySingleton
public Lifecycle getLifecycle(final Injector injector)
{
Lifecycle lifecycle = new Lifecycle();
scope.setLifecycle(lifecycle);
lifecycle.addHandler(
new Lifecycle.Handler()
{
@Override
public void start() throws Exception
{
for (Key<?> key : eagerClasses) {
injector.getInstance(key); // Pull the key so as to "eagerly" load up the class.
}
}
@Override
public void stop()
{
}
Lifecycle lifecycle = new Lifecycle(){
@Override
public void start() throws Exception
{
for (Key<?> key : eagerClasses) {
injector.getInstance(key); // Pull the key so as to "eagerly" load up the class.
}
);
super.start();
}
};
scope.setLifecycle(lifecycle);
return lifecycle;
}

View File

@ -40,7 +40,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<metamx.java-util.version>0.23.0-SNAPSHOT</metamx.java-util.version>
<apache.curator.version>2.0.2-21-22</apache.curator.version>
<apache.curator.version>2.1.0-incubating</apache.curator.version>
</properties>
<modules>

View File

@ -9,6 +9,7 @@ import com.metamx.druid.client.selector.Server;
import org.apache.curator.x.discovery.ServiceInstance;
import org.apache.curator.x.discovery.ServiceProvider;
import javax.annotation.Nullable;
import java.io.IOException;
/**
@ -21,7 +22,7 @@ public class IndexingServiceSelector implements DiscoverySelector<Server>
@Inject
public IndexingServiceSelector(
@IndexingService ServiceProvider serviceProvider
@Nullable @IndexingService ServiceProvider serviceProvider
) {
this.serviceProvider = serviceProvider;
}

View File

@ -33,6 +33,7 @@ import com.metamx.common.lifecycle.LifecycleStop;
import com.metamx.common.logger.Logger;
import com.metamx.druid.concurrent.Execs;
import com.metamx.druid.guice.ManageLifecycle;
import com.metamx.druid.jackson.Json;
import com.metamx.druid.master.rules.PeriodLoadRule;
import com.metamx.druid.master.rules.Rule;
import org.joda.time.DateTime;
@ -129,7 +130,7 @@ public class DatabaseRuleManager
@Inject
public DatabaseRuleManager(
ObjectMapper jsonMapper,
@Json ObjectMapper jsonMapper,
Supplier<DatabaseRuleManagerConfig> config,
Supplier<DbTablesConfig> dbTables,
IDBI dbi

View File

@ -21,6 +21,7 @@ package com.metamx.druid.db;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.metamx.common.lifecycle.Lifecycle;
@ -54,25 +55,30 @@ public class DatabaseRuleManagerProvider implements Provider<DatabaseRuleManager
@Override
public DatabaseRuleManager get()
{
lifecycle.addHandler(
new Lifecycle.Handler()
{
@Override
public void start() throws Exception
try {
lifecycle.addMaybeStartHandler(
new Lifecycle.Handler()
{
dbConnector.createRulesTable();
DatabaseRuleManager.createDefaultRule(
dbConnector.getDBI(), dbTables.get().getRulesTable(), config.get().getDefaultTier(), jsonMapper
);
}
@Override
public void start() throws Exception
{
dbConnector.createRulesTable();
DatabaseRuleManager.createDefaultRule(
dbConnector.getDBI(), dbTables.get().getRulesTable(), config.get().getDefaultTier(), jsonMapper
);
}
@Override
public void stop()
{
@Override
public void stop()
{
}
}
}
);
);
}
catch (Exception e) {
throw Throwables.propagate(e);
}
return new DatabaseRuleManager(jsonMapper, config, dbTables, dbConnector.getDBI());
}

View File

@ -34,9 +34,12 @@ 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;
/**
*/
public class MasterModule implements Module
@ -78,6 +81,29 @@ public class MasterModule implements Module
@Provides @LazySingleton @IndexingService
public ServiceProvider getServiceProvider(DruidMasterConfig config, ServiceDiscovery<Void> serviceDiscovery)
{
// TODO: This service discovery stuff is really really janky. It needs to be reworked.
if (config.getMergerServiceName() == null) {
return new ServiceProvider()
{
@Override
public void start() throws Exception
{
}
@Override
public ServiceInstance getInstance() throws Exception
{
return null;
}
@Override
public void close() throws IOException
{
}
};
}
return serviceDiscovery.serviceProviderBuilder().serviceName(config.getMergerServiceName()).build();
}

View File

@ -446,17 +446,17 @@ public class DruidMaster
curator, ZKPaths.makePath(zkPaths.getMasterPath(), MASTER_OWNER_NODE), config.getHost()
);
newLeaderLatch.attachListener(
newLeaderLatch.addListener(
new LeaderLatchListener()
{
@Override
public void becomeMaster()
public void isLeader()
{
DruidMaster.this.becomeMaster();
}
@Override
public void stopBeingMaster()
public void notLeader()
{
DruidMaster.this.stopBeingMaster();
}