Skip to content

Commit

Permalink
修改review意见
Browse files Browse the repository at this point in the history
  • Loading branch information
hanbingleixue committed Apr 9, 2023
1 parent 53e844d commit a1648ef
Show file tree
Hide file tree
Showing 47 changed files with 518 additions and 241 deletions.
8 changes: 4 additions & 4 deletions sermant-plugins/sermant-service-removal/config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# 离群实例摘除插件配置
removal.config:
expireTimes: 60000 # 实例调用信息过期时间。单位:毫秒
expireTime: 60000 # 实例调用信息过期时间。单位:毫秒
exceptions: # 任务服务调用失败的异常情况
- com.alibaba.dubbo.remoting.TimeoutException
- org.apache.dubbo.remoting.TimeoutException
- java.util.concurrent.TimeoutException
- java.net.SocketTimeoutException
enableRemoval: false # 离群实例摘除开关
recoveryTimes: 30000 # 实例摘除之后的恢复时间
windowsTimes: 1000 # 离群实例异常统计窗口大小。单位:毫秒
recoveryTime: 30000 # 实例摘除之后的恢复时间
windowsTime: 1000 # 离群实例异常统计窗口大小。单位:毫秒
windowsNum: 10 # 离群实例异常统计窗口个数
rules: # 离群实例摘除规则,scaleUpLimit:离群实例摘除上限比例。minInstanceNum:最小实例数。
- { scaleUpLimit: 0.6, minInstanceNum: 1, errorRate: 0.6 }
- { key: default-rule, scaleUpLimit: 0.6, minInstanceNum: 1, errorRate: 0.6 }
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private DubboCache() {
* @param interfaceName 接口名
* @param serviceName 服务名
*/
public static void putApplication(String interfaceName, String serviceName) {
public static void putService(String interfaceName, String serviceName) {
SERVICE_CACHE.put(interfaceName, serviceName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@

package com.huaweicloud.sermant.cache;

import com.huaweicloud.sermant.config.RemovalRule;
import com.huaweicloud.sermant.core.common.LoggerFactory;
import com.huaweicloud.sermant.common.RemovalConstants;
import com.huaweicloud.sermant.entity.InstanceInfo;
import com.huaweicloud.sermant.entity.RequestInfo;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

/**
* 服务实例缓存类
Expand All @@ -38,10 +35,6 @@ public class InstanceCache {
*/
public static final Map<String, InstanceInfo> INSTANCE_MAP = new ConcurrentHashMap<>();

private static final String CONNECTOR = ":";

private static final Logger LOGGER = LoggerFactory.getLogger();

private InstanceCache() {
}

Expand All @@ -51,57 +44,17 @@ private InstanceCache() {
* @param requestInfo 服务调用信息
*/
public static void saveInstanceInfo(RequestInfo requestInfo) {
String key = requestInfo.getHost() + CONNECTOR + requestInfo.getPort();
String key = requestInfo.getHost() + RemovalConstants.CONNECTOR + requestInfo.getPort();
InstanceInfo info = INSTANCE_MAP.computeIfAbsent(key, s -> {
InstanceInfo instanceInfo = new InstanceInfo();
instanceInfo.setHost(requestInfo.getHost());
instanceInfo.setPort(requestInfo.getPort());
return instanceInfo;
});
if (!requestInfo.isResult()) {
if (!requestInfo.isSuccess()) {
info.getRequestFailNum().getAndIncrement();
}
info.getRequestNum().getAndIncrement();
info.setLastInvokeTime(requestInfo.getRequestTime());
}

/**
* 判断是否需要摘除实例
*
* @param info 实例信息
* @param rule 离群实例摘除规则
* @return 是否需要摘除实例
*/
public static boolean isNeedRemoval(InstanceInfo info, RemovalRule rule) {
AtomicInteger requestCount = new AtomicInteger();
AtomicInteger requestFailCount = new AtomicInteger();
if (info.getCountDataList() == null || info.getCountDataList().isEmpty()) {
return false;
}
info.getCountDataList().forEach(requestCountData -> {
requestCount.getAndAdd(requestCountData.getRequestNum());
requestFailCount.getAndAdd(requestCountData.getRequestFailNum());
});
float errorRate = 0.0f;
if (requestCount.get() != 0) {
errorRate = (float) requestFailCount.get() / requestCount.get();
}
return errorRate >= rule.getErrorRate();
}

/**
* 摘除实例恢复
*/
public static void recovery() {
if (INSTANCE_MAP.isEmpty()) {
return;
}
for (Map.Entry<String, InstanceInfo> entry : INSTANCE_MAP.entrySet()) {
InstanceInfo info = entry.getValue();
if (info != null && info.getRemovalStatus().get() && System.currentTimeMillis() > info.getRecoveryTime()
&& info.getRemovalStatus().compareAndSet(true, false)) {
LOGGER.info("The removal strength has reached the recovery time, and the removal is canceled");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.huaweicloud.sermant.config.RemovalConfig;
import com.huaweicloud.sermant.config.RemovalRule;
import com.huaweicloud.sermant.core.config.ConfigManager;
import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager;
import com.huaweicloud.sermant.core.utils.StringUtils;

import java.util.HashMap;
Expand All @@ -38,19 +38,31 @@ public class RuleCache {
*/
protected static final Map<String, RemovalRule> RULE_MAP = new HashMap<>();

private static final RemovalRule RULE;
private static RemovalRule rule;

private static final RemovalConfig REMOVAL_CONFIG = ConfigManager.getConfig(RemovalConfig.class);
private static final String DEFAULT_RULE_NAME = "default-rule";

private static final RemovalConfig REMOVAL_CONFIG = PluginConfigManager.getConfig(RemovalConfig.class);

private RuleCache() {
}

static {
List<RemovalRule> rules = REMOVAL_CONFIG.getRules();
updateCache(REMOVAL_CONFIG);
}

/**
* 更新缓存信息
*
* @param removalConfig 离群实例摘除配置
*/
public static void updateCache(RemovalConfig removalConfig) {
cleanCache();
List<RemovalRule> rules = removalConfig.getRules();
if (rules != null && !rules.isEmpty()) {
rules.forEach(removalRule -> RULE_MAP.put(StringUtils.getString(removalRule.getKey()), removalRule));
}
RULE = RULE_MAP.get(StringUtils.EMPTY);
rule = RULE_MAP.get(DEFAULT_RULE_NAME);
}

/**
Expand All @@ -60,6 +72,13 @@ private RuleCache() {
* @return 离群实例摘除规则
*/
public static Optional<RemovalRule> getRule(String key) {
return Optional.ofNullable(RULE_MAP.getOrDefault(key, RULE));
return Optional.ofNullable(RULE_MAP.getOrDefault(key, rule));
}

/**
* 清除缓存
*/
public static void cleanCache() {
RuleCache.RULE_MAP.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.sermant.common;

/**
* 常量类
*
* @author zhp
* @since 2023-04-04
*/
public class RemovalConstants {
/**
* Dubbo服务名称KEY
*/
public static final String APPLICATION_KEY = "application";

/**
* 连接符
*/
public static final String CONNECTOR = ":";

/**
* 时间窗口大小
*/
public static final int WINDOWS_TIME = 1000;

/**
* 窗口个数
*/
public static final int WINDOWS_NUM = 10;

/**
* 插件名称
*/
public static final String PLUGIN_NAME = "serment-service-removal";

/**
* 动态配置KEY
*/
public static final String DYNAMIC_CONFIG_KEY = "sermant.removal.config";

private RemovalConstants() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.huaweicloud.sermant.config;

import com.huaweicloud.sermant.common.RemovalConstants;
import com.huaweicloud.sermant.core.config.common.ConfigTypeKey;
import com.huaweicloud.sermant.core.plugin.config.PluginConfig;

Expand All @@ -32,12 +33,12 @@ public class RemovalConfig implements PluginConfig {
/**
* 实例过期时间
*/
private int expireTimes;
private int expireTime;

/**
* 恢复时间
*/
private int recoveryTimes;
private int recoveryTime;

/**
* 支持的异常类型
Expand All @@ -52,29 +53,37 @@ public class RemovalConfig implements PluginConfig {
/**
* 窗口时间
*/
private int windowsTimes;
private int windowsTime = RemovalConstants.WINDOWS_TIME;

/**
* 窗口数量
*/
private int windowsNum;
private int windowsNum = RemovalConstants.WINDOWS_NUM;

private List<RemovalRule> rules;

public int getExpireTimes() {
return expireTimes;
public int getExpireTime() {
return expireTime;
}

public void setExpireTimes(int expireTimes) {
this.expireTimes = expireTimes;
public void setExpireTime(int expireTime) {
this.expireTime = expireTime;
}

public int getRecoveryTimes() {
return recoveryTimes;
public int getRecoveryTime() {
return recoveryTime;
}

public void setRecoveryTimes(int recoveryTimes) {
this.recoveryTimes = recoveryTimes;
public void setRecoveryTime(int recoveryTime) {
this.recoveryTime = recoveryTime;
}

public int getWindowsTime() {
return windowsTime;
}

public void setWindowsTime(int windowsTime) {
this.windowsTime = windowsTime;
}

public List<String> getExceptions() {
Expand All @@ -93,14 +102,6 @@ public void setEnableRemoval(boolean enableRemoval) {
this.enableRemoval = enableRemoval;
}

public int getWindowsTimes() {
return windowsTimes;
}

public void setWindowsTimes(int windowsTimes) {
this.windowsTimes = windowsTimes;
}

public int getWindowsNum() {
return windowsNum;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public class RemovalRule {
/**
* 服务名称或者接口名称
* 服务名称
*/
private String key;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.huaweicloud.sermant.declarer;

import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer;
import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer;
import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher;
import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher;
Expand All @@ -27,7 +28,7 @@
* @author zhp
* @since 2023-03-16
*/
public class AlibabaDubboClusterUtilsDeclarer extends AbstractSwitchDeclarer {
public class AlibabaDubboClusterUtilsDeclarer extends AbstractPluginDeclarer {
private static final String ENHANCE_CLASS = "com.alibaba.dubbo.rpc.cluster.support.ClusterUtils";

private static final String INTERCEPT_CLASS = AlibabaDubboClusterUtilsInterceptor.class.getCanonicalName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.huaweicloud.sermant.declarer;

import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer;
import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer;
import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher;
import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher;
Expand All @@ -27,7 +28,7 @@
* @author zhp
* @since 2023-02-17
*/
public class AlibabaDubboDirectoryDeclarer extends AbstractSwitchDeclarer {
public class AlibabaDubboDirectoryDeclarer extends AbstractPluginDeclarer {
private static final String ENHANCE_CLASS = "com.alibaba.dubbo.rpc.cluster.directory.AbstractDirectory";

private static final String INTERCEPT_CLASS = AlibabaDubboDirectoryInterceptor.class.getCanonicalName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.huaweicloud.sermant.declarer;

import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer;
import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer;
import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher;
import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher;
Expand All @@ -27,7 +28,7 @@
* @author zhp
* @since 2023-02-17
*/
public class AlibabaDubboInvokeDeclarer extends AbstractSwitchDeclarer {
public class AlibabaDubboInvokeDeclarer extends AbstractPluginDeclarer {
private static final String ENHANCE_CLASS = "com.alibaba.dubbo.rpc.protocol.dubbo.DubboInvoker";

private static final String INTERCEPT_CLASS = AlibabaDubboInvokeInterceptor.class.getCanonicalName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.huaweicloud.sermant.declarer;

import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer;
import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer;
import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher;
import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher;
Expand All @@ -27,7 +28,7 @@
* @author zhp
* @since 2023-03-16
*/
public class ApacheDubboClusterUtilsDeclarer extends AbstractSwitchDeclarer {
public class ApacheDubboClusterUtilsDeclarer extends AbstractPluginDeclarer {
private static final String ENHANCE_CLASS = "org.apache.dubbo.rpc.cluster.support.ClusterUtils";

private static final String INTERCEPT_CLASS = ApacheDubboClusterUtilsInterceptor.class.getCanonicalName();
Expand Down
Loading

0 comments on commit a1648ef

Please sign in to comment.