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.
*
* @author chen.ma
* @date 2021/6/24 21:19
*/
@Slf4j
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
* val:
* key: 192.168.20.227:8088_xxx
@ -82,12 +77,10 @@ public class ConfigCacheService {
*/
private synchronized static String getContentMd5IsNullPut(String groupKey, String clientIdentify) {
Map<String, CacheItem> cacheItemMap = Optional.ofNullable(CLIENT_CONFIG_CACHE.get(groupKey)).orElse(Maps.newHashMap());
CacheItem cacheItem = null;
if (CollUtil.isNotEmpty(cacheItemMap) && (cacheItem = cacheItemMap.get(clientIdentify)) != null) {
return cacheItem.md5;
}
if (CONFIG_SERVICE == null) {
CONFIG_SERVICE = ApplicationContextHolder.getBean(ConfigService.class);
}
@ -98,7 +91,6 @@ public class ConfigCacheService {
cacheItemMap.put(clientIdentify, cacheItem);
CLIENT_CONFIG_CACHE.put(groupKey, cacheItemMap);
}
return (cacheItem != null) ? cacheItem.md5 : Constants.NULL;
}
@ -106,14 +98,12 @@ public class ConfigCacheService {
if (CONFIG_SERVICE == null) {
CONFIG_SERVICE = ApplicationContextHolder.getBean(ConfigService.class);
}
String[] params = groupKey.split("\\+");
ConfigAllInfo config = CONFIG_SERVICE.findConfigRecentInfo(params);
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]);
throw new RuntimeException(errorMessage);
}
return Md5Util.getTpContentMd5(config);
}
@ -135,12 +125,10 @@ public class ConfigCacheService {
if (ipCacheItemMap != null && (item = ipCacheItemMap.get(ip)) != null) {
return item;
}
CacheItem tmp = new CacheItem(groupKey);
Map<String, CacheItem> cacheItemMap = Maps.newHashMap();
cacheItemMap.put(ip, tmp);
CLIENT_CONFIG_CACHE.putIfAbsent(groupKey, cacheItemMap);
return tmp;
}
@ -160,19 +148,14 @@ public class ConfigCacheService {
/**
* Remove config cache.
*
* @param groupKey + + IP
* @param groupKey tenant + item + IP
*/
public static void removeConfigCache(String groupKey) {
coarseRemove(groupKey);
}
/**
* Coarse remove.
*
* @param coarse
*/
private synchronized static void coarseRemove(String coarse) {
// 模糊搜索
// fuzzy search
List<String> identificationList = MapUtil.parseMapForFilter(CLIENT_CONFIG_CACHE, coarse);
for (String cacheMapKey : identificationList) {
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());
coarseRemove(observerMessage.message());
}
}
}

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

@ -31,9 +31,6 @@ import static cn.hippo4j.common.constant.Constants.WEIGHT_CONFIGS;
/**
* Config servlet inner.
*
* @author chen.ma
* @date 2021/6/22 23:13
*/
@Service
@RequiredArgsConstructor
@ -47,7 +44,7 @@ public class ConfigServletInner {
.build();
/**
* .
* Poll configuration.
*
* @param request
* @param response
@ -60,15 +57,14 @@ public class ConfigServletInner {
longPollingService.addLongPollingClient(request, response, clientMd5Map, probeRequestSize);
return HttpServletResponse.SC_OK + "";
}
return HttpServletResponse.SC_OK + "";
}
/**
* .
* Check whether the repeat request is repeated.
* <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
* @return
@ -80,8 +76,6 @@ public class ConfigServletInner {
deWeightCache.put(clientIdentify, System.currentTimeMillis());
return true;
}
return false;
}
}

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

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

Loading…
Cancel
Save