killbill-aplcache

util: update net.sf.ehcache artifact This also enables

3/16/2015 2:44:58 PM

Details

util/pom.xml 3(+1 -2)

diff --git a/util/pom.xml b/util/pom.xml
index 854e540..f04d2ec 100644
--- a/util/pom.xml
+++ b/util/pom.xml
@@ -88,8 +88,7 @@
         </dependency>
         <dependency>
             <groupId>net.sf.ehcache</groupId>
-            <artifactId>ehcache-core</artifactId>
-            <type>jar</type>
+            <artifactId>ehcache</artifactId>
         </dependency>
         <dependency>
             <groupId>org.antlr</groupId>
diff --git a/util/src/main/java/org/killbill/billing/util/cache/CacheControllerDispatcherProvider.java b/util/src/main/java/org/killbill/billing/util/cache/CacheControllerDispatcherProvider.java
index c41cddb..f980ad7 100644
--- a/util/src/main/java/org/killbill/billing/util/cache/CacheControllerDispatcherProvider.java
+++ b/util/src/main/java/org/killbill/billing/util/cache/CacheControllerDispatcherProvider.java
@@ -1,7 +1,9 @@
 /*
  * Copyright 2010-2013 Ning, Inc.
+ * Copyright 2014-2015 Groupon, Inc
+ * Copyright 2014-2015 The Billing Project, LLC
  *
- * Ning licenses this file to you under the Apache License, version 2.0
+ * The Billing Project 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:
  *
@@ -24,17 +26,21 @@ import javax.inject.Inject;
 import javax.inject.Provider;
 
 import org.killbill.billing.util.cache.Cachable.CacheType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Function;
 import com.google.common.collect.Collections2;
-import net.sf.ehcache.Cache;
+import com.google.common.collect.ImmutableList;
 import net.sf.ehcache.CacheManager;
-import net.sf.ehcache.Element;
+import net.sf.ehcache.Ehcache;
 import net.sf.ehcache.loader.CacheLoader;
 
 // Build the abstraction layer between EhCache and Kill Bill
 public class CacheControllerDispatcherProvider implements Provider<CacheControllerDispatcher> {
 
+    private static final Logger logger = LoggerFactory.getLogger(CacheControllerDispatcherProvider.class);
+
     private final CacheManager cacheManager;
 
     @Inject
@@ -59,7 +65,11 @@ public class CacheControllerDispatcherProvider implements Provider<CacheControll
     }
 
     private Collection<EhCacheBasedCacheController<Object, Object>> getCacheControllersForCacheName(final String name, final CacheType cacheType) {
-        final Cache cache = cacheManager.getCache(name);
+        final Ehcache cache = cacheManager.getEhcache(name);
+        if (cache == null) {
+            logger.warn("No cache configured for name {}", name);
+            return ImmutableList.<EhCacheBasedCacheController<Object, Object>>of();
+        }
         // The CacheLoaders were registered in EhCacheCacheManagerProvider
         return Collections2.transform(cache.getRegisteredCacheLoaders(), new Function<CacheLoader, EhCacheBasedCacheController<Object, Object>>() {
             @Override
diff --git a/util/src/main/java/org/killbill/billing/util/cache/EhCacheBasedCacheController.java b/util/src/main/java/org/killbill/billing/util/cache/EhCacheBasedCacheController.java
index eb20466..ce26eb3 100644
--- a/util/src/main/java/org/killbill/billing/util/cache/EhCacheBasedCacheController.java
+++ b/util/src/main/java/org/killbill/billing/util/cache/EhCacheBasedCacheController.java
@@ -1,7 +1,9 @@
 /*
  * Copyright 2010-2012 Ning, Inc.
+ * Copyright 2014-2015 Groupon, Inc
+ * Copyright 2014-2015 The Billing Project, LLC
  *
- * Ning licenses this file to you under the Apache License, version 2.0
+ * The Billing Project 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:
  *
@@ -18,15 +20,15 @@ package org.killbill.billing.util.cache;
 
 import org.killbill.billing.util.cache.Cachable.CacheType;
 
-import net.sf.ehcache.Cache;
+import net.sf.ehcache.Ehcache;
 import net.sf.ehcache.Element;
 
 public class EhCacheBasedCacheController<K, V> implements CacheController<K, V> {
 
-    private final Cache cache;
+    private final Ehcache cache;
     private final CacheType cacheType;
 
-    public EhCacheBasedCacheController(final Cache cache, final CacheType cacheType) {
+    public EhCacheBasedCacheController(final Ehcache cache, final CacheType cacheType) {
         this.cache = cache;
         this.cacheType = cacheType;
     }
diff --git a/util/src/main/java/org/killbill/billing/util/cache/EhCacheCacheManagerProvider.java b/util/src/main/java/org/killbill/billing/util/cache/EhCacheCacheManagerProvider.java
index 3698c4b..b632255 100644
--- a/util/src/main/java/org/killbill/billing/util/cache/EhCacheCacheManagerProvider.java
+++ b/util/src/main/java/org/killbill/billing/util/cache/EhCacheCacheManagerProvider.java
@@ -1,7 +1,9 @@
 /*
  * Copyright 2010-2012 Ning, Inc.
+ * Copyright 2014-2015 Groupon, Inc
+ * Copyright 2014-2015 The Billing Project, LLC
  *
- * Ning licenses this file to you under the Apache License, version 2.0
+ * The Billing Project 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:
  *
@@ -27,19 +29,28 @@ import javax.inject.Provider;
 
 import org.killbill.billing.util.config.CacheConfig;
 import org.killbill.xmlloader.UriAccessor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-import net.sf.ehcache.Cache;
+import com.codahale.metrics.MetricRegistry;
+import com.codahale.metrics.ehcache.InstrumentedEhcache;
+import net.sf.ehcache.CacheException;
 import net.sf.ehcache.CacheManager;
+import net.sf.ehcache.Ehcache;
 import net.sf.ehcache.loader.CacheLoader;
 
 // EhCache specific provider
 public class EhCacheCacheManagerProvider implements Provider<CacheManager> {
 
+    private static final Logger logger = LoggerFactory.getLogger(EhCacheCacheManagerProvider.class);
+
+    private final MetricRegistry metricRegistry;
     private final CacheConfig cacheConfig;
     private final Collection<BaseCacheLoader> cacheLoaders = new LinkedList<BaseCacheLoader>();
 
     @Inject
-    public EhCacheCacheManagerProvider(final CacheConfig cacheConfig,
+    public EhCacheCacheManagerProvider(final MetricRegistry metricRegistry,
+                                       final CacheConfig cacheConfig,
                                        final RecordIdCacheLoader recordIdCacheLoader,
                                        final AccountRecordIdCacheLoader accountRecordIdCacheLoader,
                                        final TenantRecordIdCacheLoader tenantRecordIdCacheLoader,
@@ -49,6 +60,7 @@ public class EhCacheCacheManagerProvider implements Provider<CacheManager> {
                                        final TenantCatalogCacheLoader tenantCatalogCacheLoader,
                                        final TenantOverdueConfigCacheLoader tenantOverdueConfigCacheLoader,
                                        final TenantKVCacheLoader tenantKVCacheLoader) {
+        this.metricRegistry = metricRegistry;
         this.cacheConfig = cacheConfig;
         cacheLoaders.add(recordIdCacheLoader);
         cacheLoaders.add(accountRecordIdCacheLoader);
@@ -67,22 +79,30 @@ public class EhCacheCacheManagerProvider implements Provider<CacheManager> {
         try {
             final InputStream inputStream = UriAccessor.accessUri(cacheConfig.getCacheConfigLocation());
             cacheManager = CacheManager.create(inputStream);
-        } catch (IOException e) {
+        } catch (final IOException e) {
             throw new RuntimeException(e);
-        } catch (URISyntaxException e) {
+        } catch (final URISyntaxException e) {
             throw new RuntimeException(e);
         }
 
         for (final BaseCacheLoader cacheLoader : cacheLoaders) {
             cacheLoader.init();
 
-            final Cache cache = cacheManager.getCache(cacheLoader.getCacheType().getCacheName());
+            final Ehcache cache = cacheManager.getEhcache(cacheLoader.getCacheType().getCacheName());
 
             // Make sure we start from a clean state - this is mainly useful for tests
             for (final CacheLoader existingCacheLoader : cache.getRegisteredCacheLoaders()) {
                 cache.unregisterCacheLoader(existingCacheLoader);
             }
             cache.registerCacheLoader(cacheLoader);
+
+            // Instrument the cache
+            final Ehcache decoratedCache = InstrumentedEhcache.instrument(metricRegistry, cache);
+            try {
+                cacheManager.replaceCacheWithDecoratedCache(cache, decoratedCache);
+            } catch (final CacheException e) {
+                logger.warn("Unable to instrument cache {}: {}", cache.getName(), e.getMessage());
+            }
         }
         return cacheManager;
     }