From d770d9245c3c3abb3cebf4eda3778ca4595f1542 Mon Sep 17 00:00:00 2001 From: ma-xiao-guang-64 Date: Mon, 5 Mar 2018 10:34:41 +0800 Subject: [PATCH] [Dubbo-#1362] cache provider always lru cache (#1396) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * #1362修复 #1362修复 * #1362修复 #1362修复 * #1362修复 #1362修复 * 修改bug 修改bug * 修改bug 修改bug * 添加cache的测试用例 添加cache的测试用例 --- .../alibaba/dubbo/config/cache/CacheTest.java | 71 ++++++++++++++----- .../com/alibaba/dubbo/cache/CacheFactory.java | 3 +- .../dubbo/cache/filter/CacheFilter.java | 2 +- .../cache/support/AbstractCacheFactory.java | 5 +- 4 files changed, 59 insertions(+), 22 deletions(-) diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/cache/CacheTest.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/cache/CacheTest.java index 7632d3b28f5..8cf94efe4f2 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/cache/CacheTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/cache/CacheTest.java @@ -16,23 +16,28 @@ */ package com.alibaba.dubbo.config.cache; -import com.alibaba.dubbo.config.ApplicationConfig; -import com.alibaba.dubbo.config.ProtocolConfig; -import com.alibaba.dubbo.config.ReferenceConfig; -import com.alibaba.dubbo.config.RegistryConfig; -import com.alibaba.dubbo.config.ServiceConfig; - +import com.alibaba.dubbo.cache.Cache; +import com.alibaba.dubbo.cache.CacheFactory; +import com.alibaba.dubbo.cache.support.threadlocal.ThreadLocalCache; +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.common.extension.ExtensionLoader; +import com.alibaba.dubbo.config.*; +import com.alibaba.dubbo.rpc.Invocation; +import com.alibaba.dubbo.rpc.RpcInvocation; import junit.framework.TestCase; import org.junit.Assert; import org.junit.Test; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + /** * CacheTest */ public class CacheTest extends TestCase { - @Test - public void testCache() throws Exception { + private void testCache(String type) throws Exception { ServiceConfig service = new ServiceConfig(); service.setApplication(new ApplicationConfig("cache-provider")); service.setRegistry(new RegistryConfig("N/A")); @@ -45,6 +50,12 @@ public void testCache() throws Exception { reference.setApplication(new ApplicationConfig("cache-consumer")); reference.setInterface(CacheService.class); reference.setUrl("dubbo://127.0.0.1:29582?scope=remote&cache=true"); + + MethodConfig method = new MethodConfig(); + method.setName("findCache"); + method.setCache(type); + reference.setMethods(Arrays.asList(method)); + CacheService cacheService = reference.get(); try { // verify cache, same result is returned for multiple invocations (in fact, the return value increases @@ -57,19 +68,21 @@ public void testCache() throws Exception { Thread.sleep(100); } - // default cache.size is 1000 for LRU, should have cache expired if invoke more than 1001 times - for (int n = 0; n < 1001; n++) { - String pre = null; - for (int i = 0; i < 10; i++) { - String result = cacheService.findCache(String.valueOf(n)); - Assert.assertTrue(pre == null || pre.equals(result)); - pre = result; + if ("lru".equals(type)) { + // default cache.size is 1000 for LRU, should have cache expired if invoke more than 1001 times + for (int n = 0; n < 1001; n++) { + String pre = null; + for (int i = 0; i < 10; i++) { + String result = cacheService.findCache(String.valueOf(n)); + Assert.assertTrue(pre == null || pre.equals(result)); + pre = result; + } } - } - // verify if the first cache item is expired in LRU cache - String result = cacheService.findCache("0"); - Assert.assertFalse(fix == null || fix.equals(result)); + // verify if the first cache item is expired in LRU cache + String result = cacheService.findCache("0"); + Assert.assertFalse(fix == null || fix.equals(result)); + } } finally { reference.destroy(); } @@ -78,4 +91,24 @@ public void testCache() throws Exception { } } + @Test + public void testCache() throws Exception { + testCache("lru"); + testCache("threadlocal"); + } + + @Test + public void testCacheProvider() throws Exception { + CacheFactory cacheFactory = (CacheFactory) ExtensionLoader.getExtensionLoader(CacheFactory.class).getAdaptiveExtension(); + + Map parameters = new HashMap(); + parameters.put("findCache.cache", "threadlocal"); + URL url = new URL("dubbo", "127.0.0.1", 29582, "com.alibaba.dubbo.config.cache.CacheService", parameters); + + Invocation invocation = new RpcInvocation("findCache", new Class[]{String.class}, new String[]{"0"}, null, null); + + Cache cache = cacheFactory.getCache(url, invocation); + Assert.assertTrue(cache instanceof ThreadLocalCache); + } + } diff --git a/dubbo-filter/dubbo-filter-cache/src/main/java/com/alibaba/dubbo/cache/CacheFactory.java b/dubbo-filter/dubbo-filter-cache/src/main/java/com/alibaba/dubbo/cache/CacheFactory.java index d2bc56d99b3..df31f7da9f5 100644 --- a/dubbo-filter/dubbo-filter-cache/src/main/java/com/alibaba/dubbo/cache/CacheFactory.java +++ b/dubbo-filter/dubbo-filter-cache/src/main/java/com/alibaba/dubbo/cache/CacheFactory.java @@ -19,6 +19,7 @@ import com.alibaba.dubbo.common.URL; import com.alibaba.dubbo.common.extension.Adaptive; import com.alibaba.dubbo.common.extension.SPI; +import com.alibaba.dubbo.rpc.Invocation; /** * CacheFactory @@ -27,6 +28,6 @@ public interface CacheFactory { @Adaptive("cache") - Cache getCache(URL url); + Cache getCache(URL url, Invocation invocation); } diff --git a/dubbo-filter/dubbo-filter-cache/src/main/java/com/alibaba/dubbo/cache/filter/CacheFilter.java b/dubbo-filter/dubbo-filter-cache/src/main/java/com/alibaba/dubbo/cache/filter/CacheFilter.java index 8b434739794..f1f6668bd98 100644 --- a/dubbo-filter/dubbo-filter-cache/src/main/java/com/alibaba/dubbo/cache/filter/CacheFilter.java +++ b/dubbo-filter/dubbo-filter-cache/src/main/java/com/alibaba/dubbo/cache/filter/CacheFilter.java @@ -43,7 +43,7 @@ public void setCacheFactory(CacheFactory cacheFactory) { public Result invoke(Invoker invoker, Invocation invocation) throws RpcException { if (cacheFactory != null && ConfigUtils.isNotEmpty(invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.CACHE_KEY))) { - Cache cache = cacheFactory.getCache(invoker.getUrl().addParameter(Constants.METHOD_KEY, invocation.getMethodName())); + Cache cache = cacheFactory.getCache(invoker.getUrl(), invocation); if (cache != null) { String key = StringUtils.toArgumentString(invocation.getArguments()); Object value = cache.get(key); diff --git a/dubbo-filter/dubbo-filter-cache/src/main/java/com/alibaba/dubbo/cache/support/AbstractCacheFactory.java b/dubbo-filter/dubbo-filter-cache/src/main/java/com/alibaba/dubbo/cache/support/AbstractCacheFactory.java index 06713da5637..7a6558e6987 100644 --- a/dubbo-filter/dubbo-filter-cache/src/main/java/com/alibaba/dubbo/cache/support/AbstractCacheFactory.java +++ b/dubbo-filter/dubbo-filter-cache/src/main/java/com/alibaba/dubbo/cache/support/AbstractCacheFactory.java @@ -18,7 +18,9 @@ import com.alibaba.dubbo.cache.Cache; import com.alibaba.dubbo.cache.CacheFactory; +import com.alibaba.dubbo.common.Constants; import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.rpc.Invocation; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -30,7 +32,8 @@ public abstract class AbstractCacheFactory implements CacheFactory { private final ConcurrentMap caches = new ConcurrentHashMap(); - public Cache getCache(URL url) { + public Cache getCache(URL url, Invocation invocation) { + url = url.addParameter(Constants.METHOD_KEY, invocation.getMethodName()); String key = url.toFullString(); Cache cache = caches.get(key); if (cache == null) {