清理因客户端强制关闭而产生的无效配置缓存.

pull/84/head
chen.ma 3 years ago
parent 5ba89d970a
commit 10b013abea

@ -2,6 +2,9 @@ package cn.hippo4j.config.service;
import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.constant.Constants; import cn.hippo4j.common.constant.Constants;
import cn.hippo4j.common.design.observer.AbstractSubjectCenter;
import cn.hippo4j.common.design.observer.Observer;
import cn.hippo4j.common.design.observer.ObserverMessage;
import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.common.toolkit.JSONUtil;
import cn.hippo4j.common.toolkit.Md5Util; import cn.hippo4j.common.toolkit.Md5Util;
import cn.hippo4j.config.event.LocalDataChangeEvent; import cn.hippo4j.config.event.LocalDataChangeEvent;
@ -32,7 +35,11 @@ import java.util.concurrent.atomic.AtomicInteger;
@Slf4j @Slf4j
public class ConfigCacheService { public class ConfigCacheService {
private static ConfigService CONFIG_SERVICE = null; private static ConfigService CONFIG_SERVICE;
static {
AbstractSubjectCenter.register(AbstractSubjectCenter.SubjectType.CLEAR_CONFIG_CACHE, new ClearConfigCache());
}
/** /**
* TODO: remove * TODO: remove
@ -138,13 +145,35 @@ public class ConfigCacheService {
* *
* @param groupKey + + IP * @param groupKey + + IP
*/ */
public synchronized static void removeConfigCache(String groupKey) { public static void removeConfigCache(String groupKey) {
coarseRemove(groupKey);
}
/**
* Coarse remove.
*
* @param coarse
*/
private synchronized static void coarseRemove(String coarse) {
// 模糊搜索 // 模糊搜索
List<String> identificationList = MapUtil.parseMapForFilter(CLIENT_CONFIG_CACHE, groupKey); List<String> identificationList = MapUtil.parseMapForFilter(CLIENT_CONFIG_CACHE, coarse);
for (String cacheMapKey : identificationList) { for (String cacheMapKey : identificationList) {
Map<String, CacheItem> removeCacheItem = CLIENT_CONFIG_CACHE.remove(cacheMapKey); Map<String, CacheItem> removeCacheItem = CLIENT_CONFIG_CACHE.remove(cacheMapKey);
log.info("Remove invalidated config cache. config info :: {}", JSONUtil.toJSONString(removeCacheItem)); log.info("Remove invalidated config cache. config info :: {}", JSONUtil.toJSONString(removeCacheItem));
} }
} }
/**
* This is an observer, clear config cache.
*/
static class ClearConfigCache implements Observer<String> {
@Override
public void accept(ObserverMessage<String> observerMessage) {
log.info("Clean up the configuration cache. Key :: {}", observerMessage.message());
coarseRemove(observerMessage.message());
}
}
} }

@ -1,5 +1,6 @@
package cn.hippo4j.discovery.core; package cn.hippo4j.discovery.core;
import cn.hippo4j.common.design.observer.AbstractSubjectCenter;
import cn.hippo4j.common.model.InstanceInfo; import cn.hippo4j.common.model.InstanceInfo;
import cn.hippo4j.common.model.InstanceInfo.InstanceStatus; import cn.hippo4j.common.model.InstanceInfo.InstanceStatus;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
@ -244,18 +245,22 @@ public class BaseInstanceRegistry implements InstanceRegistry<InstanceInfo> {
for (Lease<InstanceInfo> expiredLease : expiredLeases) { for (Lease<InstanceInfo> expiredLease : expiredLeases) {
String appName = expiredLease.getHolder().getAppName(); String appName = expiredLease.getHolder().getAppName();
String id = expiredLease.getHolder().getInstanceId(); String id = expiredLease.getHolder().getInstanceId();
internalCancel(appName, id, false); String identify = expiredLease.getHolder().getIdentify();
internalCancel(appName, id, identify, false);
} }
} }
protected boolean internalCancel(String appName, String id, boolean isReplication) { protected boolean internalCancel(String appName, String id, String identify, boolean isReplication) {
read.lock(); read.lock();
try { try {
Map<String, Lease<InstanceInfo>> registerMap = registry.get(appName); Map<String, Lease<InstanceInfo>> registerMap = registry.get(appName);
if (!CollectionUtils.isEmpty(registerMap)) { if (!CollectionUtils.isEmpty(registerMap)) {
registerMap.remove(id); registerMap.remove(id);
AbstractSubjectCenter.notify(AbstractSubjectCenter.SubjectType.CLEAR_CONFIG_CACHE, () -> identify);
log.info("Clean up unhealthy nodes. Node id :: {}", id); log.info("Clean up unhealthy nodes. Node id :: {}", id);
} }
} finally { } finally {
read.unlock(); read.unlock();
} }

Loading…
Cancel
Save