Skip to content

Commit

Permalink
bugfix: the issue of possible infinite loop when cleaning up expired …
Browse files Browse the repository at this point in the history
…metadata info
  • Loading branch information
funky-eyes committed Jan 22, 2025
1 parent 0fa7cbe commit beb6b95
Showing 1 changed file with 11 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import static org.apache.dubbo.common.constants.CommonConstants.METADATA_INFO_CACHE_SIZE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.REGISTRY_LOCAL_FILE_CACHE_ENABLED;
import static org.apache.dubbo.common.constants.CommonConstants.REMOTE_METADATA_STORAGE_TYPE;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.INTERNAL_ERROR;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.REGISTRY_FAILED_LOAD_METADATA;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_CLUSTER_KEY;
import static org.apache.dubbo.metadata.RevisionResolver.EMPTY_REVISION;
Expand Down Expand Up @@ -111,25 +110,10 @@ private AbstractServiceDiscovery(ApplicationModel applicationModel, String servi
registryURL.getParameter(METADATA_INFO_CACHE_EXPIRE_KEY, DEFAULT_METADATA_INFO_CACHE_EXPIRE);
int metadataInfoCacheSize =
registryURL.getParameter(METADATA_INFO_CACHE_SIZE_KEY, DEFAULT_METADATA_INFO_CACHE_SIZE);
this.refreshCacheFuture = applicationModel
.getFrameworkModel()
.getBeanFactory()
.getBean(FrameworkExecutorRepository.class)
.getSharedScheduledExecutor()
.schedule(
() -> {
try {
removeExpiredMetadataInfo(metadataInfoCacheSize, metadataInfoCacheExpireTime);
} catch (Throwable t) {
logger.error(
INTERNAL_ERROR, "", "", "Error occurred when clean up metadata info cache.", t);
}
},
metadataInfoCacheExpireTime / 2,
TimeUnit.MILLISECONDS);
startRefreshCache(metadataInfoCacheExpireTime / 2, metadataInfoCacheSize, metadataInfoCacheExpireTime);
}

public void removeExpiredMetadataInfo(int metadataInfoCacheSize, int metadataInfoCacheExpireTime) {
private void removeExpiredMetadataInfo(int metadataInfoCacheSize, int metadataInfoCacheExpireTime) {
Long time = null;
if (metadataInfos.size() > metadataInfoCacheSize) {
List<MetadataInfoStat> values = new ArrayList<>(metadataInfos.values());
Expand All @@ -138,19 +122,27 @@ public void removeExpiredMetadataInfo(int metadataInfoCacheSize, int metadataInf
time = System.currentTimeMillis() - v.getUpdateTime();
if (time > metadataInfoCacheExpireTime) {
metadataInfos.remove(v.metadataInfo.getRevision(), v);
time = null;
} else {
break;
}
}
}
startRefreshCache(
time == null ? metadataInfoCacheExpireTime / 2 : time,
metadataInfoCacheSize,
metadataInfoCacheExpireTime);
}

private void startRefreshCache(long time, int metadataInfoCacheSize, int metadataInfoCacheExpireTime) {
this.refreshCacheFuture = applicationModel
.getFrameworkModel()
.getBeanFactory()
.getBean(FrameworkExecutorRepository.class)
.getSharedScheduledExecutor()
.schedule(
() -> removeExpiredMetadataInfo(metadataInfoCacheSize, metadataInfoCacheExpireTime),
time == null ? metadataInfoCacheExpireTime / 2 : time,
time,
TimeUnit.MILLISECONDS);
}

Expand Down

0 comments on commit beb6b95

Please sign in to comment.