Skip to content

Commit b7e4f38

Browse files
kezhenxu94ralf0131
authored andcommitted
[DUBBO-3746] Bugfix: infinite loop in AbstractRegistry when IOException. fix #3746 (#3748)
1 parent 5e77e59 commit b7e4f38

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.concurrent.ConcurrentMap;
4949
import java.util.concurrent.ExecutorService;
5050
import java.util.concurrent.Executors;
51+
import java.util.concurrent.atomic.AtomicInteger;
5152
import java.util.concurrent.atomic.AtomicLong;
5253
import java.util.concurrent.atomic.AtomicReference;
5354

@@ -60,6 +61,8 @@ public abstract class AbstractRegistry implements Registry {
6061
private static final char URL_SEPARATOR = ' ';
6162
// URL address separated regular expression for parsing the service provider URL list in the file cache
6263
private static final String URL_SPLIT = "\\s+";
64+
// Max times to retry to save properties to local cache file
65+
private static final int MAX_RETRY_TIMES_SAVE_PROPERTIES = 3;
6366
// Log output
6467
protected final Logger logger = LoggerFactory.getLogger(getClass());
6568
// 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 {
6972
// Is it synchronized to save the file
7073
private final boolean syncSaveFile;
7174
private final AtomicLong lastCacheChanged = new AtomicLong();
75+
private final AtomicInteger savePropertiesRetryTimes = new AtomicInteger();
7276
private final Set<URL> registered = new ConcurrentHashSet<>();
7377
private final ConcurrentMap<URL, Set<NotifyListener>> subscribed = new ConcurrentHashMap<>();
7478
private final ConcurrentMap<URL, Map<String, List<URL>>> notified = new ConcurrentHashMap<>();
@@ -174,12 +178,19 @@ public void doSaveProperties(long version) {
174178
}
175179
}
176180
} catch (Throwable e) {
181+
savePropertiesRetryTimes.incrementAndGet();
182+
if (savePropertiesRetryTimes.get() >= MAX_RETRY_TIMES_SAVE_PROPERTIES) {
183+
logger.warn("Failed to save registry cache file after retrying " + MAX_RETRY_TIMES_SAVE_PROPERTIES + " times, cause: " + e.getMessage(), e);
184+
savePropertiesRetryTimes.set(0);
185+
return;
186+
}
177187
if (version < lastCacheChanged.get()) {
188+
savePropertiesRetryTimes.set(0);
178189
return;
179190
} else {
180191
registryCacheExecutor.execute(new SaveProperties(lastCacheChanged.incrementAndGet()));
181192
}
182-
logger.warn("Failed to save registry cache file, cause: " + e.getMessage(), e);
193+
logger.warn("Failed to save registry cache file, will retry, cause: " + e.getMessage(), e);
183194
}
184195
}
185196

0 commit comments

Comments
 (0)