From 10b013abea8f4b6455473b172fb8f917c8ff2974 Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Thu, 30 Dec 2021 23:56:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=90=86=E5=9B=A0=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E5=BC=BA=E5=88=B6=E5=85=B3=E9=97=AD=E8=80=8C=E4=BA=A7?= =?UTF-8?q?=E7=94=9F=E7=9A=84=E6=97=A0=E6=95=88=E9=85=8D=E7=BD=AE=E7=BC=93?= =?UTF-8?q?=E5=AD=98.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/service/ConfigCacheService.java | 35 +++++++++++++++++-- .../discovery/core/BaseInstanceRegistry.java | 9 +++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/hippo4j-config/src/main/java/cn/hippo4j/config/service/ConfigCacheService.java b/hippo4j-config/src/main/java/cn/hippo4j/config/service/ConfigCacheService.java index f4b6744a..5744399d 100644 --- a/hippo4j-config/src/main/java/cn/hippo4j/config/service/ConfigCacheService.java +++ b/hippo4j-config/src/main/java/cn/hippo4j/config/service/ConfigCacheService.java @@ -2,6 +2,9 @@ package cn.hippo4j.config.service; import cn.hippo4j.common.config.ApplicationContextHolder; 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.Md5Util; import cn.hippo4j.config.event.LocalDataChangeEvent; @@ -32,7 +35,11 @@ import java.util.concurrent.atomic.AtomicInteger; @Slf4j 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 操作待重构 @@ -138,13 +145,35 @@ public class ConfigCacheService { * * @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 identificationList = MapUtil.parseMapForFilter(CLIENT_CONFIG_CACHE, groupKey); + List identificationList = MapUtil.parseMapForFilter(CLIENT_CONFIG_CACHE, coarse); for (String cacheMapKey : identificationList) { Map removeCacheItem = CLIENT_CONFIG_CACHE.remove(cacheMapKey); log.info("Remove invalidated config cache. config info :: {}", JSONUtil.toJSONString(removeCacheItem)); } } + /** + * This is an observer, clear config cache. + */ + static class ClearConfigCache implements Observer { + + @Override + public void accept(ObserverMessage observerMessage) { + log.info("Clean up the configuration cache. Key :: {}", observerMessage.message()); + coarseRemove(observerMessage.message()); + } + + } + } diff --git a/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/BaseInstanceRegistry.java b/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/BaseInstanceRegistry.java index 857c7229..afdc1caf 100644 --- a/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/BaseInstanceRegistry.java +++ b/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/BaseInstanceRegistry.java @@ -1,5 +1,6 @@ package cn.hippo4j.discovery.core; +import cn.hippo4j.common.design.observer.AbstractSubjectCenter; import cn.hippo4j.common.model.InstanceInfo; import cn.hippo4j.common.model.InstanceInfo.InstanceStatus; import com.google.common.cache.CacheBuilder; @@ -244,18 +245,22 @@ public class BaseInstanceRegistry implements InstanceRegistry { for (Lease expiredLease : expiredLeases) { String appName = expiredLease.getHolder().getAppName(); 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(); try { Map> registerMap = registry.get(appName); if (!CollectionUtils.isEmpty(registerMap)) { registerMap.remove(id); + AbstractSubjectCenter.notify(AbstractSubjectCenter.SubjectType.CLEAR_CONFIG_CACHE, () -> identify); + log.info("Clean up unhealthy nodes. Node id :: {}", id); } + } finally { read.unlock(); }