Skip to content

Commit

Permalink
流控checkstyle修改
Browse files Browse the repository at this point in the history
  • Loading branch information
justforstudy-A committed Feb 23, 2022
1 parent 98324e2 commit 0f0a685
Show file tree
Hide file tree
Showing 34 changed files with 354 additions and 280 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.huawei.sermant.core.service.dynamicconfig.config.DynamicConfig;
import com.huawei.sermant.core.service.dynamicconfig.kie.config.KieDynamicConfig;
import com.huawei.sermant.core.service.heartbeat.HeartbeatConfig;

import org.junit.Assert;
import org.junit.BeforeClass;

Expand Down Expand Up @@ -69,7 +70,8 @@ public static void init() throws IllegalAccessException, NoSuchFieldException, C

final URL logbackSettingURL = BaseTest.class.getResource("/logback-test.xml");
Assert.assertNotNull(logbackSettingURL);
LoggerFactory.init(Collections.<String, Object>singletonMap(CommonConstant.LOG_SETTING_FILE_KEY, logbackSettingURL.getPath()));
LoggerFactory.init(
Collections.<String, Object>singletonMap(CommonConstant.LOG_SETTING_FILE_KEY, logbackSettingURL.getPath()));
}

/**
Expand All @@ -82,7 +84,7 @@ public static void init() throws IllegalAccessException, NoSuchFieldException, C
protected static void removeFinalModify(Field field) throws NoSuchFieldException, IllegalAccessException {
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field,field.getModifiers()&~Modifier.FINAL);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@
package com.huawei.flowcontrol.common.adapte.cse.converter;

/**
* 转换器
* 当前用于转换相关规则
* 转换器 当前用于转换相关规则
*
* @param <SOURCE> 源类型
* @param <TARGET> 目标类型
* @param <S> 源类型
* @param <T> 目标类型
* @author zhouss
* @since 2021-11-16
*/
public interface Converter<SOURCE, TARGET> {
public interface Converter<S, T> {
/**
* 转换
*
* @param source 源数据类型
* @return 目前数据
*/
TARGET convert(SOURCE source);
T convert(S source);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@
/**
* yaml格式字符串转换器
*
* @param <TARGET> 目标值
* @param <T> 目标值
* @author zhouss
* @since 2021-11-16
*/
public class YamlConverter<TARGET> implements Converter<String, TARGET> {
public class YamlConverter<T> implements Converter<String, T> {
private static final Logger LOGGER = LoggerFactory.getLogger();

protected final Yaml yaml;

/**
* 目标类类型
*/
private final Class<TARGET> targetClass;
private final Class<T> targetClass;

public YamlConverter(Class<TARGET> targetClass) {
public YamlConverter(Class<T> targetClass) {
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(PluginCollectorManager.class.getClassLoader());
this.targetClass = targetClass;
Expand All @@ -54,7 +54,7 @@ public YamlConverter(Class<TARGET> targetClass) {

@Override
@SuppressWarnings("checkstyle:IllegalCatch")
public TARGET convert(String source) {
public T convert(String source) {
if (targetClass == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,15 @@ public void setApp(String app) {
*
* @return boolean
*/
@SuppressWarnings("checkstyle:BooleanExpressionComplexity")
public boolean isReady() {
return serviceName != null && project != null
&& customLabel != null && customLabelValue != null
&& environment != null && app != null;
return isCustomInfoReady() && isServiceInfoReady();
}

private boolean isServiceInfoReady() {
return serviceName != null && environment != null && app != null;
}

private boolean isCustomInfoReady() {
return project != null && customLabel != null && customLabelValue != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class RawOperator extends HashMap<String, String> {
* 默认初始化大小
*/
private static final int DEFAULT_CAPACITY = 4;
private static final long serialVersionUID = 1930797351862384146L;

public RawOperator() {
super(DEFAULT_CAPACITY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public class CompareOperator implements Operator {
private static final Logger LOGGER = LoggerFactory.getLogger();

private static final double DOUBLE_EPSLON = 1e-6;
private static final double DOUBLE_EPSLON = 1e-6d;

private final Set<Character> operators = new HashSet<Character>();

Expand Down Expand Up @@ -74,7 +74,7 @@ private boolean compare(String targetValue, String operator, String num) {
} catch (NumberFormatException ex) {
// 转换失败的直接返回未匹配
LOGGER.warning(
String.format(Locale.ENGLISH, "Format number failed when convert %s and %s", targetValue, num));
String.format(Locale.ENGLISH, "Format number failed when convert %s and %s", targetValue, num));
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public abstract class AbstractResolver<T extends Configurable> {
/**
* 转换器
*/
private final Converter<String, T> converter;
private Converter<String, T> converter;

public AbstractResolver(String configKey) {
this(configKey, null);
Expand All @@ -70,7 +70,7 @@ public AbstractResolver(String configKey, Converter<String, T> converter) {
this.configKey = configKey;

// 线上SC目前治理策略仅支持yaml格式配置
this.converter = converter == null ? new YamlConverter<>(getRuleClass()) : converter;
this.converter = converter;
rules = new HashMap<String, T>();
}

Expand Down Expand Up @@ -150,6 +150,9 @@ public T parseRule(String businessKey, String value, boolean isOverride, boolean
rules.remove(businessKey);

// 2、转换配置
if (converter == null) {
converter = new YamlConverter<>(getRuleClass());
}
final T rule = converter.convert(value);
if (rule == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public class CircuitBreakerRule extends AbstractRule {
/**
* 默认失败错误率阈值
*/
public static final float DEFAULT_FAILURE_RATE_THRESHOLD = 50;
public static final float DEFAULT_FAILURE_RATE_THRESHOLD = 50f;

/**
* 默认慢调用阈值
*/
public static final float DEFAULT_SLOW_CALL_RATE_THRESHOLD = 100;
public static final float DEFAULT_SLOW_CALL_RATE_THRESHOLD = 100f;

/**
* 熔断间隔
Expand Down Expand Up @@ -170,7 +170,7 @@ public String getWaitDurationInOpenState() {
public void setWaitDurationInOpenState(String waitDurationInOpenState) {
this.waitDurationInOpenState = waitDurationInOpenState;
this.parsedWaitDurationInOpenState = parseLongTime(waitDurationInOpenState,
DEFAULT_WAIT_DURATION_IN_OPEN_STATUS_MS);
DEFAULT_WAIT_DURATION_IN_OPEN_STATUS_MS);
}

public String getSlowCallDurationThreshold() {
Expand All @@ -180,7 +180,7 @@ public String getSlowCallDurationThreshold() {
public void setSlowCallDurationThreshold(String slowCallDurationThreshold) {
this.slowCallDurationThreshold = slowCallDurationThreshold;
this.parsedSlowCallDurationThreshold = parseLongTime(slowCallDurationThreshold,
DEFAULT_SLOW_CALL_DURATION_THRESHOLD_MS);
DEFAULT_SLOW_CALL_DURATION_THRESHOLD_MS);
}

public int getPermittedNumberOfCallsInHalfOpenState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public class RetryRule extends AbstractRule {
/**
* 默认指数
*/
private static final float DEFAULT_MULTIPLIER = 2;
private static final float DEFAULT_MULTIPLIER = 2f;

/**
* 默认随机因子
*/
private static final double DEFAULT_RANDOMIZATION_FACTOR = 0.5;
private static final double DEFAULT_RANDOMIZATION_FACTOR = 0.5d;

/**
* 默认重试策略
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ public class CommonConst {
/**
* 百分比
*/
public static final double PERCENT = 100.0;
public static final double PERCENT = 100.0d;

/**
* 限流请求数转换
*/
public static final double RATE_DIV_POINT = 1000.0;
public static final double RATE_DIV_POINT = 1000.0d;

private CommonConst() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public class FlowControlConfig implements PluginConfig {
/**
* 流控框架类型
*/
private FlowFramework flowFramework = FlowFramework.RESILIENCE4j;
private FlowFramework flowFramework = FlowFramework.RESILIENCE;

/**
* 针对apache dubbo重试异常
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ public enum FlowFramework {
/**
* 基于resilience4j
*/
RESILIENCE4j;
RESILIENCE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,19 @@ protected final Class<? extends Throwable>[] getRetryExceptions() {
return classes;
}
synchronized (this) {
if (classes == null) {
final RetryFramework retryFramework = retryType();
final FlowControlConfig pluginConfig = PluginConfigManager.getPluginConfig(FlowControlConfig.class);
String[] retryExceptions;
if (retryFramework == RetryFramework.SPRING_CLOUD) {
retryExceptions = pluginConfig.getSpringRetryExceptions();
} else if (retryFramework == RetryFramework.ALIBABA_DUBBO) {
retryExceptions = pluginConfig.getAlibabaDubboRetryExceptions();
} else if (retryFramework == RetryFramework.APACHE_DUBBO) {
retryExceptions = pluginConfig.getApacheDubboRetryExceptions();
} else {
throw new IllegalArgumentException("Not supported retry framework! " + retryFramework);
}
classes = findClass(retryExceptions);
final RetryFramework retryFramework = retryType();
final FlowControlConfig pluginConfig = PluginConfigManager.getPluginConfig(FlowControlConfig.class);
String[] retryExceptions;
if (retryFramework == RetryFramework.SPRING_CLOUD) {
retryExceptions = pluginConfig.getSpringRetryExceptions();
} else if (retryFramework == RetryFramework.ALIBABA_DUBBO) {
retryExceptions = pluginConfig.getAlibabaDubboRetryExceptions();
} else if (retryFramework == RetryFramework.APACHE_DUBBO) {
retryExceptions = pluginConfig.getApacheDubboRetryExceptions();
} else {
throw new IllegalArgumentException("Not supported retry framework! " + retryFramework);
}
classes = findClass(retryExceptions);
}
return classes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
*/
public class FlowControlInitServiceImpl implements PluginService {
private final ExecutorService executorService = Executors.newSingleThreadExecutor(
new FlowControlThreadFactory("FLOW_CONTROL_INIT_THREAD"));
new FlowControlThreadFactory("FLOW_CONTROL_INIT_THREAD"));

private RuleSyncer ruleSyncer;

Expand All @@ -47,7 +47,7 @@ public void start() {
@Override
public void run() {
final FlowControlConfig pluginConfig = PluginConfigManager.getPluginConfig(FlowControlConfig.class);
if (pluginConfig.getFlowFramework() != FlowFramework.RESILIENCE4j) {
if (pluginConfig.getFlowFramework() != FlowFramework.RESILIENCE) {
return;
}
if (pluginConfig.isUseCseRule()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class AlibabaDubboInterceptor extends InterceptorSupporter implements Int
* @return DubboRequestEntity
*/
private static DubboRequestEntity convertToAlibabaDubboEntity(com.alibaba.dubbo.rpc.Invocation invocation) {
// invocation.getTargetServiceUniqueName
// 高版本使用api invocation.getTargetServiceUniqueName获取路径,此处使用版本加接口,达到的最终结果一致
String apiPath = ConvertUtils.buildApiPath(invocation.getInvoker().getInterface().getName(),
invocation.getAttachment(ConvertUtils.DUBBO_ATTACHMENT_VERSION), invocation.getMethodName());
return new DubboRequestEntity(apiPath, invocation.getAttachments());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class ApacheDubboInterceptor extends InterceptorSupporter implements Inte
* @return DubboRequestEntity
*/
private DubboRequestEntity convertToApacheDubboEntity(org.apache.dubbo.rpc.Invocation invocation) {
// invocation.getTargetServiceUniqueName
// 高版本使用api invocation.getTargetServiceUniqueName获取路径,此处使用版本加接口,达到的最终结果一致
String apiPath = ConvertUtils.buildApiPath(invocation.getInvoker().getInterface().getName(),
invocation.getAttachment(ConvertUtils.DUBBO_ATTACHMENT_VERSION), invocation.getMethodName());
return new DubboRequestEntity(apiPath, invocation.getAttachments());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class AlibabaDubboInvokerInterceptor extends InterceptorSupporter impleme
* @return DubboRequestEntity
*/
private DubboRequestEntity convertToAlibabaDubboEntity(Invocation invocation) {
// invocation.getTargetServiceUniqueName
// 高版本使用api invocation.getTargetServiceUniqueName获取路径,此处使用版本加接口,达到的最终结果一致
String apiPath = ConvertUtils.buildApiPath(invocation.getInvoker().getInterface().getName(),
invocation.getAttachment(ConvertUtils.DUBBO_ATTACHMENT_VERSION), invocation.getMethodName());
return new DubboRequestEntity(apiPath, invocation.getAttachments());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class ApacheDubboInvokerInterceptor extends InterceptorSupporter implemen
* @return DubboRequestEntity
*/
private DubboRequestEntity convertToApacheDubboEntity(Invocation invocation) {
// invocation.getTargetServiceUniqueName
// 高版本使用api invocation.getTargetServiceUniqueName获取路径,此处使用版本加接口,达到的最终结果一致
String apiPath = ConvertUtils.buildApiPath(invocation.getInvoker().getInterface().getName(),
invocation.getAttachment(ConvertUtils.DUBBO_ATTACHMENT_VERSION), invocation.getMethodName());
return new DubboRequestEntity(apiPath, invocation.getAttachments());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
*/

/**
* Based on com/alibaba/csp/sentinel/dashboard/auth/FakeAuthServiceImpl.java
* from the Alibaba Sentinel project.
* Based on com/alibaba/csp/sentinel/dashboard/auth/FakeAuthServiceImpl.java from the Alibaba Sentinel project.
*/

package com.huawei.flowcontrol.console.auth;
Expand All @@ -25,9 +24,10 @@
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Serializable;

@Component
@Primary
Expand All @@ -37,6 +37,7 @@ public class AuthServiceImpl implements AuthService<HttpServletRequest>, Seriali
* session 主键
*/
public static final String WEB_SESSION_KEY = "session_sentinel_admin";
private static final long serialVersionUID = 697982465930182380L;

@Override
public AuthUser getAuthUser(HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
*/

/**
* Based on com/alibaba/csp/sentinel/dashboard/auth/FakeAuthServiceImpl.java
* from the Alibaba Sentinel project.
* Based on com/alibaba/csp/sentinel/dashboard/auth/FakeAuthServiceImpl.java from the Alibaba Sentinel project.
*/

package com.huawei.flowcontrol.console.auth;
Expand All @@ -27,6 +26,7 @@

@Component
public class AuthUserImpl implements AuthUser, Serializable {
private static final long serialVersionUID = 1523440608989490204L;
private String username;

public void setUsername(String username) {
Expand Down
Loading

0 comments on commit 0f0a685

Please sign in to comment.