4848import java .util .concurrent .ConcurrentMap ;
4949import java .util .concurrent .ExecutorService ;
5050import java .util .concurrent .Executors ;
51+ import java .util .concurrent .atomic .AtomicInteger ;
5152import java .util .concurrent .atomic .AtomicLong ;
5253import 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