Skip to content

Commit

Permalink
清理因客户端强制关闭而产生的无效配置缓存.
Browse files Browse the repository at this point in the history
  • Loading branch information
magestacks committed Dec 30, 2021
1 parent 5ba89d9 commit 10b013a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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;
Expand Down Expand Up @@ -32,7 +35,11 @@
@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 操作待重构
Expand Down Expand Up @@ -138,13 +145,35 @@ public static synchronized Integer getTotal() {
*
* @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) {
Map<String, CacheItem> 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<String> {

@Override
public void accept(ObserverMessage<String> observerMessage) {
log.info("Clean up the configuration cache. Key :: {}", observerMessage.message());
coarseRemove(observerMessage.message());
}

}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -244,18 +245,22 @@ public void evict(long additionalLeaseMs) {
for (Lease<InstanceInfo> 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<String, Lease<InstanceInfo>> 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();
}
Expand Down

0 comments on commit 10b013a

Please sign in to comment.