Skip to content

Commit

Permalink
1. 多次校验文件是否存在
Browse files Browse the repository at this point in the history
2. 恢复之前的校验顺序
3. 抽象通用逻辑
  • Loading branch information
PleaseGiveMeTheCoke committed Apr 2, 2024
1 parent 46b5cc9 commit 90a9eaa
Showing 1 changed file with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -870,34 +870,50 @@ public void checkLocalConfig(CacheData cacheData) {
// Check if a failover file exists for the specified dataId, group, and tenant.
File file = LocalConfigInfoProcessor.getFailoverFile(envName, dataId, group, tenant);

// not using local config info, but a failover file exists
boolean failOverFileCreated = !cacheData.isUseLocalConfigInfo() && file.exists();

// using local config info, but there is a change in local configuration
boolean failOverFileChanged = cacheData.isUseLocalConfigInfo() && file.exists() && cacheData.getLocalConfigInfoVersion() != file.lastModified();

// using local config info, but the failover file is deleted
boolean failOverFileDeleted = cacheData.isUseLocalConfigInfo() && !file.exists();

if (failOverFileCreated || failOverFileChanged) {
// load and use the file content
String content = LocalConfigInfoProcessor.getFailover(envName, dataId, group, tenant);
final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
cacheData.setUseLocalConfigInfo(true);
cacheData.setLocalConfigInfoVersion(file.lastModified());
cacheData.setContent(content);
LOGGER.warn(
"[{}] [failover-change] failover file {}. dataId={}, group={}, tenant={}, md5={}, content={}",
failOverFileCreated ? "created" : "changed", envName, dataId, group, tenant, md5, ContentUtils.truncateContent(content));
if (isFailOverFileCreated(cacheData, file)) {
loadAndUse(cacheData, file, true);
return;
}

if (failOverFileDeleted) {
if (isFailOverFileDeleted(cacheData, file)) {
// switch back to server config.
cacheData.setUseLocalConfigInfo(false);
LOGGER.warn("[{}] [failover-change] failover file deleted. dataId={}, group={}, tenant={}", envName,
dataId, group, tenant);
return;
}

if (isFailOverFileChanged(cacheData, file)) {
loadAndUse(cacheData, file, false);
}
}

private void loadAndUse(CacheData cacheData, File file, boolean failOverFileCreated) {
String content = LocalConfigInfoProcessor.getFailover(cacheData.envName, cacheData.dataId, cacheData.group, tenant);
final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
cacheData.setUseLocalConfigInfo(true);
cacheData.setLocalConfigInfoVersion(file.lastModified());
cacheData.setContent(content);
LOGGER.warn(
"[{}] [failover-change] failover file {}. dataId={}, group={}, tenant={}, md5={}, content={}",
failOverFileCreated ? "created" : "changed",
cacheData.envName, cacheData.dataId, cacheData.group, tenant, md5, ContentUtils.truncateContent(content));
}

private boolean isFailOverFileCreated(CacheData cacheData, File file) {
// not using local config info, but a failover file exists
return !cacheData.isUseLocalConfigInfo() && file.exists();
}

private boolean isFailOverFileChanged(CacheData cacheData, File file) {
// using local config info, but there is a change in local configuration
return cacheData.isUseLocalConfigInfo() && file.exists() && cacheData.getLocalConfigInfoVersion() != file.lastModified();
}

private boolean isFailOverFileDeleted(CacheData cacheData, File file) {
// using local config info, but the failover file is deleted
return cacheData.isUseLocalConfigInfo() && !file.exists();
}

private ExecutorService ensureSyncExecutor(String taskId) {
if (!multiTaskExecutor.containsKey(taskId)) {
Expand Down

0 comments on commit 90a9eaa

Please sign in to comment.