Skip to content

Commit

Permalink
Merge pull request #1369 from lilai23/codecheck
Browse files Browse the repository at this point in the history
codecheck问题修改
  • Loading branch information
Sherlockhan authored Nov 24, 2023
2 parents 7acdac8 + 0d7de82 commit b3d1022
Show file tree
Hide file tree
Showing 43 changed files with 208 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .github/actions/scenarios/backend/memory/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ runs:
- name: start test
shell: bash
run: |
python -m unittest ./sermant-integration-tests/scripts/testBackend.py
python -m unittest ./sermant-integration-tests/scripts/test_backend.py
- name: stop backend
shell: bash
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/scenarios/backend/redis/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ runs:
- name: start test
shell: bash
run: |
python -m unittest ./sermant-integration-tests/scripts/testBackend.py
python -m unittest ./sermant-integration-tests/scripts/test_backend.py
- name: if failure then upload error log
uses: actions/upload-artifact@v3
if: ${{ failure() || cancelled() }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public class FeiShuHookConfig extends WebhookConfigImpl {
private FeiShuHookConfig() {
}

/**
* 获取飞书的webhook配置
*
* @return 飞书的webhook配置
*/
public static WebHookConfig getInstance() {
return CONFIG;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public WelinkHookClient() {
* webhook 事件推送
*
* @param events 事件信息
* @return
* @return 推送是否成功
*/
@Override
public boolean doNotify(List<QueryResultEventInfoEntity> events) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public class WelinkHookConfig extends WebhookConfigImpl {
private WelinkHookConfig() {
}

/**
* 获取Welink的webhook配置
*
* @return Welink的webhook配置
*/
public static WebHookConfig getInstance() {
return CONFIG;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,10 @@ private Optional<String> inject(ObjectNode body) {
* env.sermant.io/[key2]:[value2]
* 则在环境变量map中添加 key1:value1 和 key2:value2,共两个环境变量
* 本函数本真不强制是否使用labels还是annotations,只需要srcMap代表label/annotations下的map即可。
*/
*
* @param srcMap 源环境变量kv
* @param tgtEnv 处理后的环境变量kv
*/
private void setEnvByMap(JsonNode srcMap, Map<String, String> tgtEnv) {
Iterator<String> labelIter = srcMap.fieldNames();
int prefixLength = SERMANT_ENV_PREFIX.length();
Expand Down Expand Up @@ -363,7 +366,7 @@ private void injectVolumes(ArrayNode arrayNode, JsonNode specPath) {
}

private void injectLifecycle(ArrayNode arrayNode, Map<String, String> env, JsonNode containerNode,
String containerPath) {
String containerPath) {
boolean enableSpring = getNotEmptyValue(env, ENABLE_SPRING_KEY, true, Boolean::parseBoolean);
boolean enableGraceShutDown = getNotEmptyValue(env, ENABLE_GRACE_SHUTDOWN_KEY, true,
Boolean::parseBoolean);
Expand Down Expand Up @@ -401,7 +404,7 @@ private void injectLifecycle(ArrayNode arrayNode, Map<String, String> env, JsonN
}

private void injectReadinessProbe(ArrayNode arrayNode, Map<String, String> env, JsonNode containerNode,
String containerPath) {
String containerPath) {
int periodSeconds = getNotEmptyValue(env, K8S_READINESS_WAIT_TIME_KEY, 1, Integer::parseInt);
boolean enableSpring = getNotEmptyValue(env, ENABLE_SPRING_KEY, true, Boolean::parseBoolean);
boolean enableHealthCheck = getNotEmptyValue(env, ENABLE_HEALTH_CHECK_KEY, false, Boolean::parseBoolean);
Expand All @@ -428,7 +431,7 @@ private void injectReadinessProbe(ArrayNode arrayNode, Map<String, String> env,
}

private void injectEnvFrom(ArrayNode arrayNode, Map<String, String> env, JsonNode containerNode,
String containerPath) {
String containerPath) {
String configMap = getNotEmptyValue(env, ENV_CONFIG_MAP_REF_KEY, envFrom, value -> value);
if (!StringUtils.hasText(configMap)) {
return;
Expand All @@ -444,7 +447,7 @@ private void injectEnvFrom(ArrayNode arrayNode, Map<String, String> env, JsonNod
}

private void injectEnv(ArrayNode arrayNode, Map<String, String> env, JsonNode containerNode, String containerPath,
Map<String, String> annotationEnv) {
Map<String, String> annotationEnv) {
// 覆盖容器的env节点
ObjectNode envNode = arrayNode.addObject();
envNode.put(JSON_OPERATION_KEY, JSON_OPERATION_ADD);
Expand Down Expand Up @@ -508,7 +511,7 @@ private String getJavaToolOptions(String injectOptions, String originOptions) {
}

private void injectVolumeMounts(ArrayNode arrayNode, Map<String, String> env, JsonNode containerNode,
String containerPath) {
String containerPath) {
// 向容器新增volumeMounts节点
ObjectNode containerVolumeNode = putOrAddObject(arrayNode, containerNode, VOLUME_MOUNTS_PATH,
containerPath + VOLUME_MOUNTS_PATH);
Expand Down
10 changes: 8 additions & 2 deletions sermant-integration-tests/scripts/AgentLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

package com.huaweicloud.sermant.script;

import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
Expand All @@ -22,6 +24,8 @@

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* AgentLoader mainClass
Expand All @@ -30,6 +34,8 @@
* @since 2023-09-26
*/
public class AgentLoader {
private static Logger logger = Logger.getLogger("com.huaweicloud.sermant.script.AgentLoader");

private AgentLoader() {
}

Expand Down Expand Up @@ -58,8 +64,8 @@ public static void main(String[] args) {
virtualMachine.detach();
}
}
} catch (AgentInitializationException | IOException | AgentLoadException | AttachNotSupportedException ignore) {
// ignore
} catch (AgentInitializationException | IOException | AgentLoadException | AttachNotSupportedException e) {
logger.log(Level.WARNING, "Load sermant agent fail, exception", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void updateConfigSources() {

private void updateAllConfigSources(Map<String, Object> changeConfigs, String eventKey) {
final Map<String, Object> configMap = allConfigSources
.getOrDefault(eventKey, new HashMap<>(changeConfigs.size()));
.getOrDefault(eventKey, new HashMap<>(changeConfigs.size()));
configMap.putAll(changeConfigs);
allConfigSources.put(eventKey, configMap);
sortedKeys.removeIf(timestampKey -> timestampKey.key.equals(eventKey));
Expand Down Expand Up @@ -126,6 +126,16 @@ public int compareTo(TimestampKey target) {
}
return Long.compare(this.timestamp, target.timestamp);
}

@Override
public boolean equals(Object obj) {
return super.equals(obj);
}

@Override
public int hashCode() {
return super.hashCode();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ private void shutdownConfigService() {
return;
}
final Optional<Object> configService = ReflectUtils.getFieldValue(nacosContextRefresher, "configService");
configService.ifPresent(o -> ReflectUtils.invokeMethod(o, "shutDown", null, null));
configService.ifPresent(object -> ReflectUtils.invokeMethod(object, "shutDown", null, null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* @since 2022-07-21
*/
public class MatchedCache {
private Cache<RequestEntity, Set<String>> cache;
private volatile Cache<RequestEntity, Set<String>> cache;

/**
* 构造器
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2022 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (C) 2021-2022 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.
Expand Down Expand Up @@ -48,6 +48,16 @@ public abstract class AbstractRule extends Configurable implements Rule, Compara
*/
protected int order = 0;

@Override
public boolean equals(Object obj) {
return super.equals(obj);
}

@Override
public int hashCode() {
return super.hashCode();
}

@Override
public int compareTo(AbstractRule target) {
return order - target.order;
Expand All @@ -69,7 +79,7 @@ public boolean isInValid() {
/**
* 字符串转long类型毫秒
*
* @param time 时间字符串 1000 -> 1000毫秒 -1000 -> 负1000毫秒 1000S(s) 1000秒 -> 1000000毫秒
* @param time 时间字符串 1000->1000毫秒 -1000->负1000毫秒 1000S(s) 1000秒->1000000毫秒
* @param defaultValue 默认值
* @return 转换值
* @throws IllegalArgumentException 参数不合法抛出
Expand Down Expand Up @@ -100,7 +110,7 @@ private Optional<Duration> tryParseWithDuration(String time) {
return Optional.of(Duration.parse(DURATION_PREFIX + time));
} catch (DateTimeException ex) {
LoggerFactory.getLogger().warning(String.format(Locale.ENGLISH,
"Parsed time [%s] to duration failed!", time));
"Parsed time [%s] to duration failed!", time));
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ private boolean isNeedFault(long curReqCount) {
/**
* 朴素贝叶斯概率模型
*
* @param curReqCount 当前请求数量
* @return 核对触发概率
*/
private boolean checkPercent(long curReqCount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ public class HttpRequestEntity extends AbstractRequestEntity {

private String method;

/**
* 空请求构造
*/
public HttpRequestEntity() {
}

private void setPathInfo(String pathInfo) {
this.pathInfo = pathInfo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
* @since 2022-02-10
*/
public abstract class AbstractRetry extends ReflectMethodCacheSupport implements Retry {
/**
* 重试异常类
*/
protected Class<? extends Throwable>[] classes;

/**
Expand All @@ -54,7 +57,7 @@ protected final Class<? extends Throwable>[] findClass(String[] classNames) {
result.add(Class.forName(className, false, Thread.currentThread().getContextClassLoader()));
} catch (ClassNotFoundException exception) {
LoggerFactory.getLogger().info(String.format(Locale.ENGLISH,
"Can not find retry exception class %s", className));
"Can not find retry exception class %s", className));
}
}
return result.toArray(new Class[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public enum RetryContext {

private final ThreadLocal<RetryPolicy> policyThreadLocal = new ThreadLocal<>();

/**
* 获取当前线程的重试机制
*
* @return 当前线程的重试机制
*/
public Retry getRetry() {
return retryThreadLocal.get();
}
Expand Down Expand Up @@ -85,6 +90,11 @@ public boolean isPolicyNeedRetry() {
return retryPolicy.isRetry() && retryPolicy.needRetry();
}

/**
* 获取当前线程的重试策略
*
* @return 当前线程的重试策略
*/
public RetryPolicy getRetryPolicy() {
return policyThreadLocal.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ private Optional<HttpRequestEntity> convertToHttpEntity(HttpServletRequest reque
if (request == null) {
return Optional.empty();
}
String uri = request.getRequestURI();
return Optional.of(new HttpRequestEntity.Builder()
.setRequestType(RequestType.SERVER)
.setPathInfo(request.getPathInfo())
.setServletPath(request.getRequestURI())
.setServletPath(uri)
.setHeaders(getHeaders(request))
.setMethod(request.getMethod())
.setServiceName(request.getHeader(ConfigConst.FLOW_REMOTE_SERVICE_NAME_HEADER_KEY))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.huawei.flowcontrol.config;

import com.huawei.flowcontrol.common.config.FlowControlConfig;
import com.huawei.flowcontrol.common.core.constants.CseConstants;
import com.huawei.flowcontrol.common.entity.FlowControlServiceMeta;
import com.huawei.flowcontrol.common.config.FlowControlConfig;

import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor;
Expand All @@ -34,6 +34,10 @@ public class AlibabaDubboConfigInterceptor extends AbstractInterceptor {
/**
* 此处代码与{@link ApacheDubboConfigInterceptor}相同
* <p>由于使用的是不同的权限定名框架,因此<h3>不可抽出,且不可放在除拦截器之外的类执行该段逻辑(类加载器问题)</h3></p>
*
* @param context 执行上下文
* @return 执行上下文
* @throws Exception 执行异常
*/
@Override
public ExecuteContext before(ExecuteContext context) throws Exception {
Expand All @@ -43,26 +47,26 @@ public ExecuteContext before(ExecuteContext context) throws Exception {
return context;
}
FlowControlServiceMeta.getInstance().setVersion(com.alibaba.dubbo.common.utils.ConfigUtils.getProperty(
CseConstants.KEY_DUBBO_VERSION,
CseConstants.DEFAULT_DUBBO_VERSION));
CseConstants.KEY_DUBBO_VERSION,
CseConstants.DEFAULT_DUBBO_VERSION));
FlowControlServiceMeta.getInstance().setProject(com.alibaba.dubbo.common.utils.ConfigUtils.getProperty(
CseConstants.KEY_DUBBO_KIE_PROJECT,
CseConstants.DEFAULT_PROJECT));
CseConstants.KEY_DUBBO_KIE_PROJECT,
CseConstants.DEFAULT_PROJECT));
FlowControlServiceMeta.getInstance().setServiceName(com.alibaba.dubbo.common.utils.ConfigUtils.getProperty(
CseConstants.KEY_DUBBO_SERVICE_NAME,
CseConstants.DEFAULT_DUBBO_SERVICE_NAME));
CseConstants.KEY_DUBBO_SERVICE_NAME,
CseConstants.DEFAULT_DUBBO_SERVICE_NAME));
FlowControlServiceMeta.getInstance().setEnvironment(com.alibaba.dubbo.common.utils.ConfigUtils.getProperty(
CseConstants.KEY_DUBBO_ENVIRONMENT,
CseConstants.DEFAULT_DUBBO_ENVIRONMENT));
CseConstants.KEY_DUBBO_ENVIRONMENT,
CseConstants.DEFAULT_DUBBO_ENVIRONMENT));
FlowControlServiceMeta.getInstance().setApp(com.alibaba.dubbo.common.utils.ConfigUtils.getProperty(
CseConstants.KEY_DUBBO_APP_NAME,
CseConstants.DEFAULT_DUBBO_APP_NAME));
CseConstants.KEY_DUBBO_APP_NAME,
CseConstants.DEFAULT_DUBBO_APP_NAME));
FlowControlServiceMeta.getInstance().setCustomLabel(com.alibaba.dubbo.common.utils.ConfigUtils.getProperty(
CseConstants.KEY_DUBBO_CUSTOM_LABEL,
CseConstants.DEFAULT_CUSTOM_LABEL));
CseConstants.KEY_DUBBO_CUSTOM_LABEL,
CseConstants.DEFAULT_CUSTOM_LABEL));
FlowControlServiceMeta.getInstance().setCustomLabelValue(com.alibaba.dubbo.common.utils.ConfigUtils.getProperty(
CseConstants.KEY_DUBBO_CUSTOM_LABEL_VALUE,
CseConstants.DEFAULT_CUSTOM_LABEL_VALUE));
CseConstants.KEY_DUBBO_CUSTOM_LABEL_VALUE,
CseConstants.DEFAULT_CUSTOM_LABEL_VALUE));
return context;
}

Expand Down
Loading

0 comments on commit b3d1022

Please sign in to comment.