diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java index 31cdbf8d467..cfe3299eb89 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java @@ -48,6 +48,7 @@ import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; @@ -60,6 +61,8 @@ public abstract class AbstractRegistry implements Registry { private static final char URL_SEPARATOR = ' '; // URL address separated regular expression for parsing the service provider URL list in the file cache private static final String URL_SPLIT = "\\s+"; + // Max times to retry to save properties to local cache file + private static final int MAX_RETRY_TIMES_SAVE_PROPERTIES = 3; // Log output protected final Logger logger = LoggerFactory.getLogger(getClass()); // Local disk cache, where the special key value.registries records the list of registry centers, and the others are the list of notified service providers @@ -69,6 +72,7 @@ public abstract class AbstractRegistry implements Registry { // Is it synchronized to save the file private final boolean syncSaveFile; private final AtomicLong lastCacheChanged = new AtomicLong(); + private final AtomicInteger savePropertiesRetryTimes = new AtomicInteger(); private final Set registered = new ConcurrentHashSet<>(); private final ConcurrentMap> subscribed = new ConcurrentHashMap<>(); private final ConcurrentMap>> notified = new ConcurrentHashMap<>(); @@ -174,12 +178,19 @@ public void doSaveProperties(long version) { } } } catch (Throwable e) { + savePropertiesRetryTimes.incrementAndGet(); + if (savePropertiesRetryTimes.get() >= MAX_RETRY_TIMES_SAVE_PROPERTIES) { + logger.warn("Failed to save registry cache file after retrying " + MAX_RETRY_TIMES_SAVE_PROPERTIES + " times, cause: " + e.getMessage(), e); + savePropertiesRetryTimes.set(0); + return; + } if (version < lastCacheChanged.get()) { + savePropertiesRetryTimes.set(0); return; } else { registryCacheExecutor.execute(new SaveProperties(lastCacheChanged.incrementAndGet())); } - logger.warn("Failed to save registry cache file, cause: " + e.getMessage(), e); + logger.warn("Failed to save registry cache file, will retry, cause: " + e.getMessage(), e); } }