This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new 1899389  fix cache was deleted when startup (#7484)
1899389 is described below

commit 1899389d6f36be344cd202b42069667cb6488db9
Author: Wu Zhiguo <[email protected]>
AuthorDate: Thu Apr 29 19:35:17 2021 +0800

    fix cache was deleted when startup (#7484)
---
 .../support/CacheableFailbackRegistry.java         | 43 ++++++++--------------
 1 file changed, 15 insertions(+), 28 deletions(-)

diff --git 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/CacheableFailbackRegistry.java
 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/CacheableFailbackRegistry.java
index 5fb3f84..16e0f89 100644
--- 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/CacheableFailbackRegistry.java
+++ 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/CacheableFailbackRegistry.java
@@ -56,9 +56,7 @@ import static 
org.apache.dubbo.common.constants.CommonConstants.PATH_SEPARATOR;
 import static 
org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_SEPARATOR_ENCODED;
 import static org.apache.dubbo.common.constants.RegistryConstants.CATEGORY_KEY;
 import static 
org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL;
-import static 
org.apache.dubbo.common.constants.RegistryConstants.OVERRIDE_PROTOCOL;
-import static 
org.apache.dubbo.common.constants.RegistryConstants.ROUTE_PROTOCOL;
-import static 
org.apache.dubbo.common.constants.RegistryConstants.ROUTE_SCRIPT_PROTOCOL;
+import static 
org.apache.dubbo.common.constants.RegistryConstants.PROVIDERS_CATEGORY;
 import static 
org.apache.dubbo.common.url.component.DubboServiceAddressURL.PROVIDER_FIRST_KEYS;
 
 /**
@@ -112,9 +110,9 @@ public abstract class CacheableFailbackRegistry extends 
FailbackRegistry {
 
     protected void evictURLCache(URL url) {
         Map<String, ServiceAddressURL> oldURLs = stringUrls.remove(url);
-        logger.info("Evicting urls for service " + url.getServiceKey() + ", 
size " + oldURLs.size());
         try {
             if (oldURLs != null && oldURLs.size() > 0) {
+                logger.info("Evicting urls for service " + url.getServiceKey() 
+ ", size " + oldURLs.size());
                 Long currentTimestamp = System.currentTimeMillis();
                 for (Map.Entry<String, ServiceAddressURL> entry : 
oldURLs.entrySet()) {
                     waitForRemove.put(entry.getValue(), currentTimestamp);
@@ -164,40 +162,28 @@ public abstract class CacheableFailbackRegistry extends 
FailbackRegistry {
             }
         }
 
+        evictURLCache(consumer);
         stringUrls.put(consumer, newURLs);
 
-        // destroy used urls
-        try {
-            if (oldURLs != null && oldURLs.size() > 0) {
-                Long currentTimestamp = System.currentTimeMillis();
-                for (Map.Entry<String, ServiceAddressURL> entry : 
oldURLs.entrySet()) {
-                    waitForRemove.put(entry.getValue(), currentTimestamp);
-                }
-                if (CollectionUtils.isNotEmptyMap(waitForRemove)) {
-                    if (semaphore.tryAcquire()) {
-                        cacheRemovalScheduler.schedule(new RemovalTask(), 
cacheRemovalTaskIntervalInMillis, TimeUnit.MILLISECONDS);
-                    }
-                }
-            }
-        } catch (Exception e) {
-            logger.warn("Failed to evict url for " + consumer, e);
-        }
-
         return new ArrayList<>(newURLs.values());
     }
 
     protected List<URL> toUrlsWithEmpty(URL consumer, String path, 
Collection<String> providers) {
-        List<URL> urls;
-        if (CollectionUtils.isEmpty(providers)) {
-            urls = new ArrayList<>(1);
+        List<URL> urls = new ArrayList<>(1);
+        boolean isProviderPath = path.endsWith(PROVIDERS_CATEGORY);
+        if (isProviderPath) {
+            if (CollectionUtils.isNotEmpty(providers)) {
+                urls = toUrlsWithoutEmpty(consumer, providers);
+            } else {
+                // clear cache on empty notification: unsubscribe or provider 
offline
+                evictURLCache(consumer);
+            }
         } else {
-            String rawProvider = providers.iterator().next();
-            if (rawProvider.startsWith(OVERRIDE_PROTOCOL) || 
rawProvider.startsWith(ROUTE_PROTOCOL) || 
rawProvider.startsWith(ROUTE_SCRIPT_PROTOCOL)) {
+            if (CollectionUtils.isNotEmpty(providers)) {
                 urls = toConfiguratorsWithoutEmpty(consumer, providers);
-            } else {
-                urls = toUrlsWithoutEmpty(consumer, providers);
             }
         }
+
         if (urls.isEmpty()) {
             int i = path.lastIndexOf(PATH_SEPARATOR);
             String category = i < 0 ? path : path.substring(i + 1);
@@ -207,6 +193,7 @@ public abstract class CacheableFailbackRegistry extends 
FailbackRegistry {
                     .build();
             urls.add(empty);
         }
+
         return urls;
     }
 

Reply via email to