From 4101d7f427b18a09e0443916983557bf5ca7cd19 Mon Sep 17 00:00:00 2001 From: WuLang <48200100+wulangcode@users.noreply.github.com> Date: Mon, 7 Nov 2022 22:02:00 +0800 Subject: [PATCH] style:hippo4j-discovery CheckStyle (#928) --- .../controller/ApplicationController.java | 7 ++- .../discovery/core/BaseInstanceRegistry.java | 49 ++++++++++--------- .../java/cn/hippo4j/discovery/core/Lease.java | 4 +- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/hippo4j-server/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/controller/ApplicationController.java b/hippo4j-server/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/controller/ApplicationController.java index a72b197a..089ed82a 100644 --- a/hippo4j-server/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/controller/ApplicationController.java +++ b/hippo4j-server/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/controller/ApplicationController.java @@ -25,7 +25,12 @@ import cn.hippo4j.discovery.core.InstanceRegistry; import cn.hippo4j.discovery.core.Lease; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import java.util.List; diff --git a/hippo4j-server/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/BaseInstanceRegistry.java b/hippo4j-server/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/BaseInstanceRegistry.java index ad6ed740..54bf589b 100644 --- a/hippo4j-server/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/BaseInstanceRegistry.java +++ b/hippo4j-server/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/BaseInstanceRegistry.java @@ -21,11 +21,13 @@ import cn.hippo4j.common.design.builder.ThreadFactoryBuilder; import cn.hippo4j.common.design.observer.AbstractSubjectCenter; import cn.hippo4j.common.model.InstanceInfo; import cn.hippo4j.common.model.InstanceInfo.InstanceStatus; +import cn.hippo4j.common.toolkit.CollectionUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.TimerTask; @@ -48,26 +50,21 @@ import static cn.hippo4j.common.constant.Constants.SCHEDULED_THREAD_CORE_NUM; @Service public class BaseInstanceRegistry implements InstanceRegistry { - private final int containerSize = 1024; + private static final int CONTAINER_SIZE = 1024; - private final ConcurrentHashMap>> registry = new ConcurrentHashMap(containerSize); + private final ConcurrentHashMap>> registry = new ConcurrentHashMap<>(CONTAINER_SIZE); @Override public List> listInstance(String appName) { Map> appNameLeaseMap = registry.get(appName); - if (CollectionUtils.isEmpty(appNameLeaseMap)) { - return new ArrayList<>(); - } - List> appNameLeaseList = new ArrayList<>(); - appNameLeaseMap.values().forEach(each -> appNameLeaseList.add(each)); - return appNameLeaseList; + return CollectionUtils.isEmpty(appNameLeaseMap) ? Collections.emptyList() : new ArrayList<>(appNameLeaseMap.values()); } @Override public void register(InstanceInfo registrant) { Map> registerMap = registry.get(registrant.getAppName()); if (registerMap == null) { - ConcurrentHashMap> registerNewMap = new ConcurrentHashMap(12); + ConcurrentHashMap> registerNewMap = new ConcurrentHashMap<>(); registerMap = registry.putIfAbsent(registrant.getAppName(), registerNewMap); if (registerMap == null) { registerMap = registerNewMap; @@ -98,8 +95,11 @@ public class BaseInstanceRegistry implements InstanceRegistry { String appName = instanceRenew.getAppName(); String instanceId = instanceRenew.getInstanceId(); Map> registryMap = registry.get(appName); - Lease leaseToRenew; - if (registryMap == null || (leaseToRenew = registryMap.get(instanceId)) == null) { + if (registryMap == null) { + return false; + } + Lease leaseToRenew = registryMap.get(instanceId); + if (leaseToRenew == null) { return false; } leaseToRenew.renew(); @@ -111,20 +111,20 @@ public class BaseInstanceRegistry implements InstanceRegistry { String appName = info.getAppName(); String instanceId = info.getInstanceId(); Map> leaseMap = registry.get(appName); - if (CollectionUtils.isEmpty(leaseMap)) { + if (CollectionUtil.isNotEmpty(leaseMap)) { + Lease remove = leaseMap.remove(instanceId); + if (remove != null) { + log.info("Remove unhealthy node, node ID: {}", instanceId); + } else { + log.warn("Failed to remove unhealthy node, no instance found: {}", instanceId); + } + } else { log.warn("Failed to remove unhealthy node, no application found: {}", appName); - return; - } - Lease remove = leaseMap.remove(instanceId); - if (remove == null) { - log.warn("Failed to remove unhealthy node, no instance found: {}", instanceId); - return; } - log.info("Remove unhealthy node, node ID: {}", instanceId); } public void evict(long additionalLeaseMs) { - List> expiredLeases = new ArrayList(); + List> expiredLeases = new ArrayList<>(); for (Map.Entry>> groupEntry : registry.entrySet()) { Map> leaseMap = groupEntry.getValue(); if (leaseMap != null) { @@ -146,7 +146,7 @@ public class BaseInstanceRegistry implements InstanceRegistry { protected boolean internalCancel(String appName, String id, String identify) { Map> registerMap = registry.get(appName); - if (!CollectionUtils.isEmpty(registerMap)) { + if (CollectionUtil.isNotEmpty(registerMap)) { registerMap.remove(id); AbstractSubjectCenter.notify(AbstractSubjectCenter.SubjectType.CLEAR_CONFIG_CACHE, () -> identify); log.info("Clean up unhealthy nodes. Node id: {}", id); @@ -154,6 +154,9 @@ public class BaseInstanceRegistry implements InstanceRegistry { return true; } + /** + * EvictionTask + */ public class EvictionTask extends TimerTask { private final AtomicLong lastExecutionNanosRef = new AtomicLong(0L); @@ -177,7 +180,7 @@ public class BaseInstanceRegistry implements InstanceRegistry { } long elapsedMs = TimeUnit.NANOSECONDS.toMillis(currNanos - lastNanos); long compensationTime = elapsedMs - EVICTION_INTERVAL_TIMER_IN_MS; - return compensationTime <= 0L ? 0L : compensationTime; + return Math.max(compensationTime, 0L); } long getCurrentTimeNano() { @@ -193,7 +196,7 @@ public class BaseInstanceRegistry implements InstanceRegistry { .daemon(true) .build()); - private final AtomicReference evictionTaskRef = new AtomicReference(); + private final AtomicReference evictionTaskRef = new AtomicReference<>(); public void postInit() { evictionTaskRef.set(new BaseInstanceRegistry.EvictionTask()); diff --git a/hippo4j-server/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/Lease.java b/hippo4j-server/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/Lease.java index ea76264a..0d796f9e 100644 --- a/hippo4j-server/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/Lease.java +++ b/hippo4j-server/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/Lease.java @@ -42,13 +42,13 @@ public class Lease { private long duration; - public static final int DEFAULT_DURATION_IN_SECS = 90; + public static final long DEFAULT_DURATION_IN_SECS = 90 * 1000L; public Lease(T r) { holder = r; registrationTimestamp = System.currentTimeMillis(); lastUpdateTimestamp = registrationTimestamp; - duration = DEFAULT_DURATION_IN_SECS * 1000; + duration = DEFAULT_DURATION_IN_SECS; } public void renew() {