Format some core code of config

pull/233/head
chen.ma 3 years ago
parent d9de555a26
commit 7d014306c4

@ -45,9 +45,6 @@ import java.util.concurrent.atomic.AtomicInteger;
/** /**
* Config cache service. * Config cache service.
*
* @author chen.ma
* @date 2021/6/24 21:19
*/ */
@Slf4j @Slf4j
public class ConfigCacheService { public class ConfigCacheService {
@ -59,8 +56,6 @@ public class ConfigCacheService {
} }
/** /**
* TODO: remove
* <p>
* key: message-produce+dynamic-threadpool-example+prescription+192.168.20.227:8088_xxx * key: message-produce+dynamic-threadpool-example+prescription+192.168.20.227:8088_xxx
* val: * val:
* key: 192.168.20.227:8088_xxx * key: 192.168.20.227:8088_xxx
@ -82,12 +77,10 @@ public class ConfigCacheService {
*/ */
private synchronized static String getContentMd5IsNullPut(String groupKey, String clientIdentify) { private synchronized static String getContentMd5IsNullPut(String groupKey, String clientIdentify) {
Map<String, CacheItem> cacheItemMap = Optional.ofNullable(CLIENT_CONFIG_CACHE.get(groupKey)).orElse(Maps.newHashMap()); Map<String, CacheItem> cacheItemMap = Optional.ofNullable(CLIENT_CONFIG_CACHE.get(groupKey)).orElse(Maps.newHashMap());
CacheItem cacheItem = null; CacheItem cacheItem = null;
if (CollUtil.isNotEmpty(cacheItemMap) && (cacheItem = cacheItemMap.get(clientIdentify)) != null) { if (CollUtil.isNotEmpty(cacheItemMap) && (cacheItem = cacheItemMap.get(clientIdentify)) != null) {
return cacheItem.md5; return cacheItem.md5;
} }
if (CONFIG_SERVICE == null) { if (CONFIG_SERVICE == null) {
CONFIG_SERVICE = ApplicationContextHolder.getBean(ConfigService.class); CONFIG_SERVICE = ApplicationContextHolder.getBean(ConfigService.class);
} }
@ -98,7 +91,6 @@ public class ConfigCacheService {
cacheItemMap.put(clientIdentify, cacheItem); cacheItemMap.put(clientIdentify, cacheItem);
CLIENT_CONFIG_CACHE.put(groupKey, cacheItemMap); CLIENT_CONFIG_CACHE.put(groupKey, cacheItemMap);
} }
return (cacheItem != null) ? cacheItem.md5 : Constants.NULL; return (cacheItem != null) ? cacheItem.md5 : Constants.NULL;
} }
@ -106,14 +98,12 @@ public class ConfigCacheService {
if (CONFIG_SERVICE == null) { if (CONFIG_SERVICE == null) {
CONFIG_SERVICE = ApplicationContextHolder.getBean(ConfigService.class); CONFIG_SERVICE = ApplicationContextHolder.getBean(ConfigService.class);
} }
String[] params = groupKey.split("\\+"); String[] params = groupKey.split("\\+");
ConfigAllInfo config = CONFIG_SERVICE.findConfigRecentInfo(params); ConfigAllInfo config = CONFIG_SERVICE.findConfigRecentInfo(params);
if (config == null || StringUtils.isEmpty(config.getTpId())) { if (config == null || StringUtils.isEmpty(config.getTpId())) {
String errorMessage = String.format("config is null. tpId :: %s, itemId :: %s, tenantId :: %s", params[0], params[1], params[2]); String errorMessage = String.format("config is null. tpId :: %s, itemId :: %s, tenantId :: %s", params[0], params[1], params[2]);
throw new RuntimeException(errorMessage); throw new RuntimeException(errorMessage);
} }
return Md5Util.getTpContentMd5(config); return Md5Util.getTpContentMd5(config);
} }
@ -135,12 +125,10 @@ public class ConfigCacheService {
if (ipCacheItemMap != null && (item = ipCacheItemMap.get(ip)) != null) { if (ipCacheItemMap != null && (item = ipCacheItemMap.get(ip)) != null) {
return item; return item;
} }
CacheItem tmp = new CacheItem(groupKey); CacheItem tmp = new CacheItem(groupKey);
Map<String, CacheItem> cacheItemMap = Maps.newHashMap(); Map<String, CacheItem> cacheItemMap = Maps.newHashMap();
cacheItemMap.put(ip, tmp); cacheItemMap.put(ip, tmp);
CLIENT_CONFIG_CACHE.putIfAbsent(groupKey, cacheItemMap); CLIENT_CONFIG_CACHE.putIfAbsent(groupKey, cacheItemMap);
return tmp; return tmp;
} }
@ -160,19 +148,14 @@ public class ConfigCacheService {
/** /**
* Remove config cache. * Remove config cache.
* *
* @param groupKey + + IP * @param groupKey tenant + item + IP
*/ */
public static void removeConfigCache(String groupKey) { public static void removeConfigCache(String groupKey) {
coarseRemove(groupKey); coarseRemove(groupKey);
} }
/**
* Coarse remove.
*
* @param coarse
*/
private synchronized static void coarseRemove(String coarse) { private synchronized static void coarseRemove(String coarse) {
// 模糊搜索 // fuzzy search
List<String> identificationList = MapUtil.parseMapForFilter(CLIENT_CONFIG_CACHE, coarse); 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);
@ -190,7 +173,5 @@ public class ConfigCacheService {
log.info("Clean up the configuration cache. Key :: {}", observerMessage.message()); log.info("Clean up the configuration cache. Key :: {}", observerMessage.message());
coarseRemove(observerMessage.message()); coarseRemove(observerMessage.message());
} }
} }
} }

@ -22,9 +22,6 @@ import cn.hippo4j.config.event.LocalDataChangeEvent;
/** /**
* Config change publisher. * Config change publisher.
*
* @author chen.ma
* @date 2021/6/24 23:34
*/ */
public class ConfigChangePublisher { public class ConfigChangePublisher {
@ -36,5 +33,4 @@ public class ConfigChangePublisher {
public static void notifyConfigChange(LocalDataChangeEvent event) { public static void notifyConfigChange(LocalDataChangeEvent event) {
NotifyCenter.publishEvent(event); NotifyCenter.publishEvent(event);
} }
} }

@ -31,9 +31,6 @@ import static cn.hippo4j.common.constant.Constants.WEIGHT_CONFIGS;
/** /**
* Config servlet inner. * Config servlet inner.
*
* @author chen.ma
* @date 2021/6/22 23:13
*/ */
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@ -47,7 +44,7 @@ public class ConfigServletInner {
.build(); .build();
/** /**
* . * Poll configuration.
* *
* @param request * @param request
* @param response * @param response
@ -60,15 +57,14 @@ public class ConfigServletInner {
longPollingService.addLongPollingClient(request, response, clientMd5Map, probeRequestSize); longPollingService.addLongPollingClient(request, response, clientMd5Map, probeRequestSize);
return HttpServletResponse.SC_OK + ""; return HttpServletResponse.SC_OK + "";
} }
return HttpServletResponse.SC_OK + ""; return HttpServletResponse.SC_OK + "";
} }
/** /**
* . * Check whether the repeat request is repeated.
* <p> * <p>
* 使, . * When a user proposes to deploy in the company environment, the same request will be called repeatedly.
* . , . * This problem belongs to an extremely individual scenario. Since it cannot be reproduced, so first solve the problem in this way.
* *
* @param request * @param request
* @return * @return
@ -80,8 +76,6 @@ public class ConfigServletInner {
deWeightCache.put(clientIdentify, System.currentTimeMillis()); deWeightCache.put(clientIdentify, System.currentTimeMillis());
return true; return true;
} }
return false; return false;
} }
} }

@ -48,9 +48,6 @@ import static cn.hippo4j.common.constant.Constants.GROUP_KEY_DELIMITER;
/** /**
* Long polling service. * Long polling service.
*
* @author chen.ma
* @date 2021/6/22 23:14
*/ */
@Slf4j @Slf4j
@Service @Service
@ -70,11 +67,8 @@ public class LongPollingService {
public LongPollingService() { public LongPollingService() {
allSubs = new ConcurrentLinkedQueue(); allSubs = new ConcurrentLinkedQueue();
ConfigExecutor.scheduleLongPolling(new StatTask(), 0L, 30L, TimeUnit.SECONDS); ConfigExecutor.scheduleLongPolling(new StatTask(), 0L, 30L, TimeUnit.SECONDS);
NotifyCenter.registerToPublisher(LocalDataChangeEvent.class, NotifyCenter.ringBufferSize); NotifyCenter.registerToPublisher(LocalDataChangeEvent.class, NotifyCenter.ringBufferSize);
NotifyCenter.registerSubscriber(new AbstractSubscriber() { NotifyCenter.registerSubscriber(new AbstractSubscriber() {
@Override @Override
@ -120,15 +114,13 @@ public class LongPollingService {
@Override @Override
public void run() { public void run() {
try { try {
for (Iterator<ClientLongPolling> iter = allSubs.iterator(); iter.hasNext();) { for (Iterator<ClientLongPolling> iter = allSubs.iterator(); iter.hasNext(); ) {
ClientLongPolling clientSub = iter.next(); ClientLongPolling clientSub = iter.next();
String identity = groupKey + GROUP_KEY_DELIMITER + identify; String identity = groupKey + GROUP_KEY_DELIMITER + identify;
List<String> parseMapForFilter = Lists.newArrayList(identity); List<String> parseMapForFilter = Lists.newArrayList(identity);
if (StrUtil.isBlank(identify)) { if (StrUtil.isBlank(identify)) {
parseMapForFilter = MapUtil.parseMapForFilter(clientSub.clientMd5Map, groupKey); parseMapForFilter = MapUtil.parseMapForFilter(clientSub.clientMd5Map, groupKey);
} }
parseMapForFilter.forEach(each -> { parseMapForFilter.forEach(each -> {
if (clientSub.clientMd5Map.containsKey(each)) { if (clientSub.clientMd5Map.containsKey(each)) {
getRetainIps().put(clientSub.clientIdentify, System.currentTimeMillis()); getRetainIps().put(clientSub.clientIdentify, System.currentTimeMillis());
@ -158,7 +150,6 @@ public class LongPollingService {
String appName = req.getHeader(CLIENT_APP_NAME_HEADER); String appName = req.getHeader(CLIENT_APP_NAME_HEADER);
String noHangUpFlag = req.getHeader(LongPollingService.LONG_POLLING_NO_HANG_UP_HEADER); String noHangUpFlag = req.getHeader(LongPollingService.LONG_POLLING_NO_HANG_UP_HEADER);
int delayTime = SwitchService.getSwitchInteger(SwitchService.FIXED_DELAY_TIME, 500); int delayTime = SwitchService.getSwitchInteger(SwitchService.FIXED_DELAY_TIME, 500);
long timeout = Math.max(10000, Long.parseLong(str) - delayTime); long timeout = Math.max(10000, Long.parseLong(str) - delayTime);
if (isFixedPolling()) { if (isFixedPolling()) {
timeout = Math.max(10000, getFixedPollingInterval()); timeout = Math.max(10000, getFixedPollingInterval());
@ -172,12 +163,9 @@ public class LongPollingService {
return; return;
} }
} }
String clientIdentify = RequestUtil.getClientIdentify(req); String clientIdentify = RequestUtil.getClientIdentify(req);
final AsyncContext asyncContext = req.startAsync(); final AsyncContext asyncContext = req.startAsync();
asyncContext.setTimeout(0L); asyncContext.setTimeout(0L);
ConfigExecutor.executeLongPolling(new ClientLongPolling(asyncContext, clientMd5Map, clientIdentify, probeRequestSize, timeout - delayTime, appName)); ConfigExecutor.executeLongPolling(new ClientLongPolling(asyncContext, clientMd5Map, clientIdentify, probeRequestSize, timeout - delayTime, appName));
} }
@ -218,7 +206,6 @@ public class LongPollingService {
try { try {
getRetainIps().put(ClientLongPolling.this.clientIdentify, System.currentTimeMillis()); getRetainIps().put(ClientLongPolling.this.clientIdentify, System.currentTimeMillis());
allSubs.remove(ClientLongPolling.this); allSubs.remove(ClientLongPolling.this);
if (isFixedPolling()) { if (isFixedPolling()) {
List<String> changedGroups = Md5ConfigUtil.compareMd5((HttpServletRequest) asyncContext.getRequest(), clientMd5Map); List<String> changedGroups = Md5ConfigUtil.compareMd5((HttpServletRequest) asyncContext.getRequest(), clientMd5Map);
if (changedGroups.size() > 0) { if (changedGroups.size() > 0) {
@ -232,9 +219,7 @@ public class LongPollingService {
} catch (Exception ex) { } catch (Exception ex) {
log.error("Long polling error :: {}", ex.getMessage(), ex); log.error("Long polling error :: {}", ex.getMessage(), ex);
} }
}, timeoutTime, TimeUnit.MILLISECONDS); }, timeoutTime, TimeUnit.MILLISECONDS);
allSubs.add(this); allSubs.add(this);
} }
@ -262,9 +247,7 @@ public class LongPollingService {
asyncContext.complete(); asyncContext.complete();
return; return;
} }
HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse(); HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse();
try { try {
String respStr = buildRespStr(changedGroups); String respStr = buildRespStr(changedGroups);
response.setHeader("Pragma", "no-cache"); response.setHeader("Pragma", "no-cache");
@ -278,7 +261,6 @@ public class LongPollingService {
asyncContext.complete(); asyncContext.complete();
} }
} }
} }
public Map<String, Long> getRetainIps() { public Map<String, Long> getRetainIps() {
@ -346,5 +328,4 @@ public class LongPollingService {
private static int getFixedPollingInterval() { private static int getFixedPollingInterval() {
return SwitchService.getSwitchInteger(SwitchService.FIXED_POLLING_INTERVAL, FIXED_POLLING_INTERVAL_MS); return SwitchService.getSwitchInteger(SwitchService.FIXED_POLLING_INTERVAL, FIXED_POLLING_INTERVAL_MS);
} }
} }

@ -25,9 +25,6 @@ import java.util.Map;
/** /**
* Switch service. * Switch service.
*
* @author chen.ma
* @date 2021/6/23 18:23
*/ */
@Slf4j @Slf4j
@Service @Service
@ -64,5 +61,4 @@ public class SwitchService {
} }
return rtn; return rtn;
} }
} }

Loading…
Cancel
Save