diff --git a/server/src/main/java/io/druid/guice/CacheModule.java b/server/src/main/java/io/druid/guice/CacheModule.java new file mode 100644 index 00000000000..aa8389f1325 --- /dev/null +++ b/server/src/main/java/io/druid/guice/CacheModule.java @@ -0,0 +1,53 @@ +/* + * Licensed to Metamarkets Group Inc. (Metamarkets) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Metamarkets licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.druid.guice; + +import com.google.inject.Binder; +import com.google.inject.Key; +import com.google.inject.Module; +import com.google.inject.name.Names; +import io.druid.client.cache.Cache; +import io.druid.client.cache.CacheProvider; +import io.druid.guice.annotations.Global; + +public class CacheModule implements Module +{ + + public static final String DRUID_GLOBAL_CACHE_PREFIX = "druid.cache"; + + public final String prefix; + + public CacheModule() + { + this.prefix = DRUID_GLOBAL_CACHE_PREFIX; + } + + public CacheModule(String prefix) + { + this.prefix = prefix; + } + + @Override + public void configure(Binder binder) + { + binder.bind(Cache.class).toProvider(Key.get(CacheProvider.class, Global.class)).in(ManageLifecycle.class); + JsonConfigProvider.bind(binder, prefix, CacheProvider.class, Global.class); + } +} diff --git a/server/src/test/java/io/druid/client/cache/MemcachedCacheTest.java b/server/src/test/java/io/druid/client/cache/MemcachedCacheTest.java index 520c5282655..2b25b7a393a 100644 --- a/server/src/test/java/io/druid/client/cache/MemcachedCacheTest.java +++ b/server/src/test/java/io/druid/client/cache/MemcachedCacheTest.java @@ -37,6 +37,7 @@ import com.metamx.metrics.AbstractMonitor; import com.metamx.metrics.MonitorScheduler; import io.druid.collections.ResourceHolder; import io.druid.collections.StupidResourceHolder; +import io.druid.guice.CacheModule; import io.druid.guice.GuiceInjectors; import io.druid.guice.JsonConfigProvider; import io.druid.guice.ManageLifecycle; @@ -175,8 +176,6 @@ public class MemcachedCacheTest final String uuid = UUID.randomUUID().toString(); System.setProperty(uuid + ".type", "memcached"); System.setProperty(uuid + ".hosts", "localhost"); - final MonitorScheduler monitorScheduler = EasyMock.createNiceMock(MonitorScheduler.class); - EasyMock.replay(monitorScheduler); final Injector injector = Initialization.makeInjectorWithModules( GuiceInjectors.makeStartupInjector(), ImmutableList.of( new Module() @@ -184,7 +183,9 @@ public class MemcachedCacheTest @Override public void configure(Binder binder) { - binder.bind(MonitorScheduler.class).toInstance(monitorScheduler); + binder.bindConstant().annotatedWith(Names.named("serviceName")).to("druid/test/memcached"); + binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0); + binder.bind(Cache.class).toProvider(CacheProvider.class); JsonConfigProvider.bind(binder, uuid, CacheProvider.class); } diff --git a/services/src/main/java/io/druid/cli/CliBroker.java b/services/src/main/java/io/druid/cli/CliBroker.java index 989e8871fac..7b6743128e0 100644 --- a/services/src/main/java/io/druid/cli/CliBroker.java +++ b/services/src/main/java/io/druid/cli/CliBroker.java @@ -33,6 +33,7 @@ import io.druid.client.cache.CacheProvider; import io.druid.client.selector.CustomTierSelectorStrategyConfig; import io.druid.client.selector.ServerSelectorStrategy; import io.druid.client.selector.TierSelectorStrategy; +import io.druid.guice.CacheModule; import io.druid.guice.Jerseys; import io.druid.guice.JsonConfigProvider; import io.druid.guice.LazySingleton; @@ -89,9 +90,9 @@ public class CliBroker extends ServerRunnable binder.bind(BrokerServerView.class).in(LazySingleton.class); binder.bind(TimelineServerView.class).to(BrokerServerView.class).in(LazySingleton.class); - binder.bind(Cache.class).toProvider(CacheProvider.class).in(ManageLifecycle.class); - JsonConfigProvider.bind(binder, "druid.cache", CacheProvider.class); JsonConfigProvider.bind(binder, "druid.broker.cache", CacheConfig.class); + binder.install(new CacheModule()); + JsonConfigProvider.bind(binder, "druid.broker.select", TierSelectorStrategy.class); JsonConfigProvider.bind(binder, "druid.broker.select.tier.custom", CustomTierSelectorStrategyConfig.class); JsonConfigProvider.bind(binder, "druid.broker.balancer", ServerSelectorStrategy.class); diff --git a/services/src/main/java/io/druid/cli/CliHistorical.java b/services/src/main/java/io/druid/cli/CliHistorical.java index 86799890e51..873e89c449b 100644 --- a/services/src/main/java/io/druid/cli/CliHistorical.java +++ b/services/src/main/java/io/druid/cli/CliHistorical.java @@ -27,6 +27,7 @@ import io.druid.client.cache.Cache; import io.druid.client.cache.CacheConfig; import io.druid.client.cache.CacheMonitor; import io.druid.client.cache.CacheProvider; +import io.druid.guice.CacheModule; import io.druid.guice.Jerseys; import io.druid.guice.JsonConfigProvider; import io.druid.guice.LazySingleton; @@ -83,11 +84,10 @@ public class CliHistorical extends ServerRunnable Jerseys.addResource(binder, HistoricalResource.class); LifecycleModule.register(binder, QueryResource.class); - LifecycleModule.register(binder, ZkCoordinator.class); + LifecycleModule.register(binder, ZkCoordinator.class); - binder.bind(Cache.class).toProvider(CacheProvider.class).in(ManageLifecycle.class); - JsonConfigProvider.bind(binder, "druid.cache", CacheProvider.class); JsonConfigProvider.bind(binder, "druid.historical.cache", CacheConfig.class); + binder.install(new CacheModule()); MetricsModule.register(binder, CacheMonitor.class); } }