You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2022/10/13 11:50:30 UTC

[GitHub] [rocketmq] aaron-ai commented on a diff in pull request #5306: [ISSUE #5305] fix proxy TopicRouteService cache bug

aaron-ai commented on code in PR #5306:
URL: https://github.com/apache/rocketmq/pull/5306#discussion_r994542098


##########
proxy/src/test/java/org/apache/rocketmq/proxy/service/route/ClusterTopicRouteServiceTest.java:
##########
@@ -67,4 +77,45 @@ public void testGetTopicRouteForProxy() throws Throwable {
         assertEquals(1, proxyTopicRouteData.getBrokerDatas().size());
         assertEquals(addressList, proxyTopicRouteData.getBrokerDatas().get(0).getBrokerAddrs().get(MixAll.MASTER_ID));
     }
+
+    @Test
+    public void testTopicRouteCaffeineCache() throws InterruptedException {
+        String key = "abc";
+        String value = key;
+        final AtomicBoolean throwException = new AtomicBoolean();
+        ThreadPoolExecutor cacheRefreshExecutor = ThreadPoolMonitor.createAndMonitor(
+            10, 10, 30L, TimeUnit.SECONDS, "test", 10);
+        LoadingCache<String /* topicName */, String> topicCache = Caffeine.newBuilder().maximumSize(30).
+            refreshAfterWrite(2, TimeUnit.SECONDS).executor(cacheRefreshExecutor).build(new CacheLoader<String, String>() {
+                @Override public @Nullable String load(@NonNull String key) throws Exception {
+                    try {
+                        if (throwException.get()) {
+                            throw new RuntimeException();
+                        } else {
+                            throwException.set(true);
+                            return value;
+                        }
+                    } catch (Exception e) {
+                        if (TopicRouteHelper.isTopicNotExistError(e)) {
+                            return "";
+                        }
+                        throw e;
+                    }
+                }
+
+                @Override
+                public @Nullable String reload(@NonNull String key, @NonNull String oldValue) throws Exception {
+                    try {
+                        return load(key);
+                    } catch (Exception e) {
+                        return oldValue;
+                    }
+                }
+            });
+
+        System.out.println(topicCache.get(key));

Review Comment:
   Use logging system rather than standard output in java



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org