diff --git a/.github/workflows/intergration_spring_test.yml b/.github/workflows/intergration_spring_test.yml index 0ec804be11..02c9140e78 100644 --- a/.github/workflows/intergration_spring_test.yml +++ b/.github/workflows/intergration_spring_test.yml @@ -8,24 +8,29 @@ on: - main - develop jobs: - download-cse-and-upload: - name: download cse and upload + download-cse-and-cache: + name: download cse and cache runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: download cse and upload - run: | - export ROOT_PATH=$(pwd) - bash ./sermant-integration-tests/scripts/downloadCse.sh - - uses: actions/upload-artifact@v3 + - name: cache local cse + uses: actions/cache@v3 with: - name: local-cse path: Local-CSE-2.1.3-linux-amd64.zip - retention-days: 1 + key: ${{ runner.os }}-local-cse + restore-keys: | + ${{ runner.os }}-local-cse + - name: download cse + run: | + num=`ls -l | grep Local-CSE-2.1.3-linux-amd64.zip | wc -l`; + if [ $num -eq 0 ];then + export ROOT_PATH=$(pwd) + bash ./sermant-integration-tests/scripts/downloadCse.sh + fi test-for-spring-1-5-x: name: Test for spring 1.5.x runs-on: ubuntu-latest - needs: [download-cse-and-upload] + needs: [download-cse-and-cache] strategy: matrix: include: @@ -40,10 +45,13 @@ jobs: java-version: '8' distribution: 'adopt' cache: maven - - name: download-cse - uses: actions/download-artifact@v3 + - name: download local cse + uses: actions/cache@v3 with: - name: local-cse + path: Local-CSE-2.1.3-linux-amd64.zip + key: ${{ runner.os }}-local-cse + restore-keys: | + ${{ runner.os }}-local-cse - name: start cse run: | export ROOT_PATH=$(pwd) @@ -93,7 +101,7 @@ jobs: uses: actions/upload-artifact@v3 if: failure() with: - name: (test-for-spring-1-5-x)-(${{ matrix.springBootVersion }}-${{ matrix.springCloudVersion }})-logs + name: (${{ github.job }})-(${{ matrix.springBootVersion }}-${{ matrix.springCloudVersion }})-logs path: | ./*.log ./logs/**/*.log @@ -102,7 +110,7 @@ jobs: test-for-spring: name: Test for spring runs-on: ubuntu-latest - needs: [download-cse-and-upload] + needs: [download-cse-and-cache] strategy: matrix: include: @@ -129,10 +137,13 @@ jobs: java-version: '8' distribution: 'adopt' cache: maven - - name: download-cse - uses: actions/download-artifact@v3 + - name: download local cse + uses: actions/cache@v3 with: - name: local-cse + path: Local-CSE-2.1.3-linux-amd64.zip + key: ${{ runner.os }}-local-cse + restore-keys: | + ${{ runner.os }}-local-cse - name: start cse run: | export ROOT_PATH=$(pwd) @@ -187,7 +198,7 @@ jobs: uses: actions/upload-artifact@v3 if: failure() with: - name: (test-for-spring)-(${{ matrix.springBootVersion }}-${{ matrix.springCloudVersion }})-logs + name: (${{ github.job }})-(${{ matrix.springBootVersion }}-${{ matrix.springCloudVersion }})-logs path: | ./*.log ./logs/**/*.log @@ -196,7 +207,7 @@ jobs: test-for-spring-nacos-config: name: Test for spring nacos config runs-on: ubuntu-latest - needs: [download-cse-and-upload] + needs: [download-cse-and-cache] strategy: matrix: include: @@ -235,10 +246,13 @@ jobs: curl -o nacos-server-1.4.2.tar.gz -L https://github.com/alibaba/nacos/releases/download/1.4.2/nacos-server-1.4.2.tar.gz tar -zxf nacos-server-1.4.2.tar.gz bash nacos/bin/startup.sh -m standalone - - name: download-cse - uses: actions/download-artifact@v3 + - name: download local cse + uses: actions/cache@v3 with: - name: local-cse + path: Local-CSE-2.1.3-linux-amd64.zip + key: ${{ runner.os }}-local-cse + restore-keys: | + ${{ runner.os }}-local-cse - name: start cse run: | export ROOT_PATH=$(pwd) @@ -293,7 +307,7 @@ jobs: uses: actions/upload-artifact@v3 if: failure() with: - name: (test-for-spring-nacos-config)-(${{ matrix.springBootVersion }}-${{ matrix.springCloudVersion}}-${{ matrix.nacosVersion }})-logs + name: (${{ github.job }})-(${{ matrix.springBootVersion }}-${{ matrix.springCloudVersion}}-${{ matrix.nacosVersion }})-logs path: | ./*.log ./logs/**/*.log @@ -302,7 +316,7 @@ jobs: test-for-spring-zk-config: name: Test for spring zk config runs-on: ubuntu-latest - needs: [download-cse-and-upload] + needs: [download-cse-and-cache] strategy: matrix: include: @@ -336,10 +350,13 @@ jobs: curl -o apache-zookeeper-3.6.3-bin.tar.gz -L https://dlcdn.apache.org/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz tar -zxf apache-zookeeper-3.6.3-bin.tar.gz bash apache-zookeeper-3.6.3-bin/bin/zkServer.sh start apache-zookeeper-3.6.3-bin/conf/zoo_sample.cfg - - name: download-cse - uses: actions/download-artifact@v3 + - name: download local cse + uses: actions/cache@v3 with: - name: local-cse + path: Local-CSE-2.1.3-linux-amd64.zip + key: ${{ runner.os }}-local-cse + restore-keys: | + ${{ runner.os }}-local-cse - name: start cse run: | export ROOT_PATH=$(pwd) @@ -394,7 +411,7 @@ jobs: uses: actions/upload-artifact@v3 if: failure() with: - name: (test-for-spring-zk-config)-(${{ matrix.springBootVersion }}-${{ matrix.springCloudVersion }})-logs + name: (${{ github.job }})-(${{ matrix.springBootVersion }}-${{ matrix.springCloudVersion }})-logs path: | ./*.log ./logs/**/*.log diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/cache/TimedConcurrentMapCache.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/cache/TimedConcurrentMapCache.java index b31834a74c..6b5a916983 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/cache/TimedConcurrentMapCache.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/cache/TimedConcurrentMapCache.java @@ -76,8 +76,7 @@ public TimedConcurrentMapCache(int maxSize, long evictTimeMs) { @Override public void put(K key, V value) { - if (super.size() > maxSize) { - scheduledThreadPoolExecutor.submit(this::removeEvictedCache); + if (super.size() >= maxSize) { if (checkEvict()) { LOGGER.fine(String.format(Locale.ENGLISH, "[TimedConcurrentMapCache] can not put key, because capacity has been max (%s)", maxSize)); diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/config/FlowControlConfig.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/config/FlowControlConfig.java index a4bbd46617..5800d9f10c 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/config/FlowControlConfig.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/config/FlowControlConfig.java @@ -18,7 +18,6 @@ package com.huawei.flowcontrol.common.config; import com.huawei.flowcontrol.common.enums.FlowFramework; -import com.huawei.flowcontrol.common.enums.MetricSendWay; import com.huaweicloud.sermant.core.config.common.ConfigTypeKey; import com.huaweicloud.sermant.core.plugin.config.PluginConfig; @@ -176,11 +175,6 @@ public class FlowControlConfig implements PluginConfig { */ private boolean needThrowBizException = false; - /** - * 指标数据发送方式 默认KAFKA - */ - private MetricSendWay sendWay = MetricSendWay.KAFKA; - /** * 等待指标数据写入文件的等待时间 单位MS */ @@ -436,14 +430,6 @@ public void setMetricSleepTimeMs(long metricSleepTimeMs) { this.metricSleepTimeMs = metricSleepTimeMs; } - public MetricSendWay getSendWay() { - return sendWay; - } - - public void setSendWay(MetricSendWay sendWay) { - this.sendWay = sendWay; - } - public boolean isAdaptPass() { return adaptPass; } diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/config/KafkaConnectBySslSwitch.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/config/KafkaConnectBySslSwitch.java deleted file mode 100644 index cac924d0e5..0000000000 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/config/KafkaConnectBySslSwitch.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2022-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. - * 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.huawei.flowcontrol.common.config; - -import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; - -import java.util.Properties; - -/** - * kafka是否通过ssl认证开关类 - * - * @author zhouss - * @since 2022-02-10 - */ -public class KafkaConnectBySslSwitch { - private KafkaConnectBySslSwitch() { - } - - /** - * 删除非ssl认证的配置属性 - * - * @param properties Properties对象 - */ - public static void delKey(Properties properties) { - final FlowControlConfig flowControlConfig = PluginConfigManager.getPluginConfig(FlowControlConfig.class); - if (!flowControlConfig.isKafkaSsl()) { - properties.remove(ConfigConst.KAFKA_JAAS_CONFIG_CONST); - properties.remove(ConfigConst.KAFKA_SASL_MECHANISM_CONST); - properties.remove(ConfigConst.KAFKA_SECURITY_PROTOCOL_CONST); - properties.remove(ConfigConst.KAFKA_SSL_TRUSTSTORE_LOCATION_CONST); - properties.remove(ConfigConst.KAFKA_SSL_TRUSTSTORE_PASSWORD_CONST); - properties.remove(ConfigConst.KAFKA_IDENTIFICATION_ALGORITHM_CONST); - } - } -} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/config/KafkaConst.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/config/KafkaConst.java deleted file mode 100644 index f6282fd174..0000000000 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/config/KafkaConst.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2022-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. - * 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.huawei.flowcontrol.common.config; - -import com.huawei.flowcontrol.common.util.PluginConfigUtil; - -import org.apache.kafka.clients.producer.ProducerConfig; - -import java.util.Properties; - -/** - * kafka配置 - * - * @author zhouss - * @since 2022-01-28 - */ -public class KafkaConst { - private KafkaConst() { - } - - /** - * 生产者配置 - * - * @return Properties - */ - public static Properties producerConfig() { - Properties properties = new Properties(); - properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, - PluginConfigUtil.getValueByKey(ConfigConst.KAFKA_BOOTSTRAP_SERVERS)); - properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, - PluginConfigUtil.getValueByKey(ConfigConst.KAFKA_KEY_SERIALIZER)); - properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, - PluginConfigUtil.getValueByKey(ConfigConst.KAFKA_VALUE_SERIALIZER)); - - // producer需要server接收到数据之后发出的确认接收的信号 ack 0,1,all - properties.put(ProducerConfig.ACKS_CONFIG, - PluginConfigUtil.getValueByKey(ConfigConst.KAFKA_ACKS)); - - // 控制生产者发送请求最大大小,默认1M (这个参数和Kafka主机的message.max.bytes 参数有关系) - properties.put(ProducerConfig.MAX_REQUEST_SIZE_CONFIG, - PluginConfigUtil.getValueByKey(ConfigConst.KAFKA_MAX_REQUEST_SIZE)); - - // 生产者内存缓冲区大小 - properties.put(ProducerConfig.BUFFER_MEMORY_CONFIG, - PluginConfigUtil.getValueByKey(ConfigConst.KAFKA_BUFFER_MEMORY)); - - // 重发消息次数 - properties.put(ProducerConfig.RETRIES_CONFIG, - PluginConfigUtil.getValueByKey(ConfigConst.KAFKA_RETRIES)); - - // 客户端将等待请求的响应的最大时间 - properties.put(ProducerConfig.REQUEST_TIMEOUT_MS_CONFIG, - PluginConfigUtil.getValueByKey(ConfigConst.KAFKA_REQUEST_TIMEOUT_MS)); - - // 最大阻塞时间,超过则抛出异常 - properties.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, - PluginConfigUtil.getValueByKey(ConfigConst.KAFKA_MAX_BLOCK_MS)); - - // 通过ssl证书连接kafka配置 - properties.put(ConfigConst.KAFKA_JAAS_CONFIG_CONST, - PluginConfigUtil.getValueByKey(ConfigConst.KAFKA_JAAS_CONFIG_CONST)); - properties.put(ConfigConst.KAFKA_SASL_MECHANISM_CONST, - PluginConfigUtil.getValueByKey(ConfigConst.KAFKA_SASL_MECHANISM_CONST)); - properties.put(ConfigConst.KAFKA_SECURITY_PROTOCOL_CONST, - PluginConfigUtil.getValueByKey(ConfigConst.KAFKA_SECURITY_PROTOCOL_CONST)); - properties.put(ConfigConst.KAFKA_SSL_TRUSTSTORE_LOCATION_CONST, - PluginConfigUtil.getValueByKey(ConfigConst.KAFKA_SSL_TRUSTSTORE_LOCATION_CONST)); - properties.put(ConfigConst.KAFKA_SSL_TRUSTSTORE_PASSWORD_CONST, - PluginConfigUtil.getValueByKey(ConfigConst.KAFKA_SSL_TRUSTSTORE_PASSWORD_CONST)); - properties.put(ConfigConst.KAFKA_IDENTIFICATION_ALGORITHM_CONST, - PluginConfigUtil.getValueByKey(ConfigConst.KAFKA_IDENTIFICATION_ALGORITHM_CONST)); - KafkaConnectBySslSwitch.delKey(properties); - return properties; - } -} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/core/match/MatchedCache.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/core/match/MatchedCache.java index 6534f60b58..2966df19bd 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/core/match/MatchedCache.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/core/match/MatchedCache.java @@ -17,13 +17,13 @@ package com.huawei.flowcontrol.common.core.match; -import com.huawei.flowcontrol.common.core.ResolverManager; -import com.huawei.flowcontrol.common.core.resolver.AbstractResolver; -import com.huawei.flowcontrol.common.core.resolver.listener.ConfigUpdateListener; import com.huawei.flowcontrol.common.cache.Cache; import com.huawei.flowcontrol.common.cache.TimedConcurrentMapCache; import com.huawei.flowcontrol.common.config.CommonConst; import com.huawei.flowcontrol.common.config.FlowControlConfig; +import com.huawei.flowcontrol.common.core.ResolverManager; +import com.huawei.flowcontrol.common.core.resolver.AbstractResolver; +import com.huawei.flowcontrol.common.core.resolver.listener.ConfigUpdateListener; import com.huawei.flowcontrol.common.entity.RequestEntity; import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/enums/KafkaProducerEnum.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/enums/KafkaProducerEnum.java deleted file mode 100644 index 0d013114b0..0000000000 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/enums/KafkaProducerEnum.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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. - * 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.huawei.flowcontrol.common.enums; - -import com.huawei.flowcontrol.common.config.KafkaConst; - -import org.apache.kafka.clients.producer.KafkaProducer; -import org.apache.kafka.common.KafkaException; - -/** - * kafka生产者枚举单例 - * - * @author Zhang Hu - * @since 2021-01-14 - */ -public enum KafkaProducerEnum { - /** - * 流控kafka的生产者 - */ - KAFKA_PRODUCER; - - private KafkaProducer kafkaProducer; - - KafkaProducerEnum() { - try { - // 必须添加不然会加载不到KafkaProducer - Thread.currentThread().setContextClassLoader(null); - kafkaProducer = new KafkaProducer(KafkaConst.producerConfig()); - } catch (KafkaException ignored) { - // ignored - } - } - - public KafkaProducer getKafkaProducer() { - return KafkaProducerEnum.KAFKA_PRODUCER.kafkaProducer; - } -} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/enums/MetricSendWay.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/enums/MetricSendWay.java deleted file mode 100644 index 01f452d922..0000000000 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/enums/MetricSendWay.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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. - * 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.huawei.flowcontrol.common.enums; - -import com.huawei.flowcontrol.common.metric.MetricSender; -import com.huawei.flowcontrol.common.metric.SimpleKafkaMetricSender; - -/** - * 指标数据发送方式 - * - * @author zhouss - * @since 2021-12-07 - */ -public enum MetricSendWay { - /** - * Kafka发送方式 - */ - KAFKA(new SimpleKafkaMetricSender()); - - private final MetricSender sender; - - MetricSendWay(MetricSender sender) { - this.sender = sender; - } - - public MetricSender getSender() { - return sender; - } -} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/metric/AbstractMetricSender.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/metric/AbstractMetricSender.java deleted file mode 100644 index 131a9d3ea4..0000000000 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/metric/AbstractMetricSender.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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. - * 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.huawei.flowcontrol.common.metric; - -import com.huawei.flowcontrol.common.metric.provider.MetricProvider; - -import com.huaweicloud.sermant.core.common.LoggerFactory; - -import com.alibaba.fastjson.JSONObject; - -import java.util.logging.Logger; - -/** - * 抽象发送器 - * - * @author zhouss - * @since 2021-12-07 - */ -public abstract class AbstractMetricSender implements MetricSender { - private static final Logger LOGGER = LoggerFactory.getLogger(); - - @Override - public void sendMetric(MetricProvider metricProvider) { - if (metricProvider == null) { - return; - } - final Object data = metricProvider.buildMetric(); - if (data == null) { - return; - } - LOGGER.fine("[MetricSender] metric message=" + JSONObject.toJSONString(data)); - sendMetric(data); - } -} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/metric/NettyMetricSender.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/metric/NettyMetricSender.java deleted file mode 100644 index 1ec5ace4d2..0000000000 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/metric/NettyMetricSender.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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. - * 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.huawei.flowcontrol.common.metric; - -import com.huaweicloud.sermant.core.service.ServiceManager; -import com.huaweicloud.sermant.core.service.send.api.GatewayClient; - -import java.io.UnsupportedEncodingException; - -/** - * netty指标发送 基于agent core 核心功能 {@link GatewayClient} - * - * @author zhouss - * @since 2021-12-07 - */ -public class NettyMetricSender extends AbstractMetricSender { - /** - * 关联backend的数据类型 - */ - private static final int FLOW_CONTROL_METRIC_TYPE = 11; - - private final GatewayClient gatewayClient; - - /** - * netty发送器构造 - */ - public NettyMetricSender() { - this.gatewayClient = ServiceManager.getService(GatewayClient.class); - } - - @Override - public void sendMetric(Object data) { - try { - gatewayClient.send(String.valueOf(data).getBytes("UTF-8"), FLOW_CONTROL_METRIC_TYPE); - } catch (UnsupportedEncodingException ignored) { - // ignored , it would not happen - } - } -} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/metric/SimpleKafkaMetricSender.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/metric/SimpleKafkaMetricSender.java deleted file mode 100644 index 57c0f85cba..0000000000 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/metric/SimpleKafkaMetricSender.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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. - * 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.huawei.flowcontrol.common.metric; - -import com.huawei.flowcontrol.common.config.ConfigConst; -import com.huawei.flowcontrol.common.util.KafkaProducerUtil; -import com.huawei.flowcontrol.common.util.PluginConfigUtil; - -/** - * 发送sentinel客户端流控数据消息 - * - * @author zhouss - * @since 2021-01-28 - */ -public class SimpleKafkaMetricSender extends AbstractMetricSender { - @Override - public void sendMetric(Object data) { - // 调用kafka发送消息 - KafkaProducerUtil.sendMessage(PluginConfigUtil.getValueByKey(ConfigConst.KAFKA_METRIC_TOPIC), data.toString()); - } -} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/metric/provider/MetricProvider.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/metric/provider/MetricProvider.java deleted file mode 100644 index f6fa52988f..0000000000 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/metric/provider/MetricProvider.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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. - * 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.huawei.flowcontrol.common.metric.provider; - -/** - * 指标数据提供 - * - * @author zhouss - * @since 2021-12-07 - */ -public interface MetricProvider { - /** - * 提供指标数据 - * - * @return 指标数据 - */ - Object buildMetric(); -} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/util/KafkaProducerUtil.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/util/KafkaProducerUtil.java deleted file mode 100644 index 44f1910d04..0000000000 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/util/KafkaProducerUtil.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (C) 2022-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. - * 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.huawei.flowcontrol.common.util; - -import com.huawei.flowcontrol.common.enums.KafkaProducerEnum; - -import com.huaweicloud.sermant.core.common.LoggerFactory; - -import org.apache.kafka.clients.producer.Callback; -import org.apache.kafka.clients.producer.KafkaProducer; -import org.apache.kafka.clients.producer.ProducerRecord; -import org.apache.kafka.clients.producer.RecordMetadata; -import org.apache.kafka.common.KafkaException; - -import java.util.logging.Logger; - -/** - * kafka消息生产工具类 - * - * @author zhouss - * @since 2022-02-10 - */ -public class KafkaProducerUtil { - private static final Logger LOGGER = LoggerFactory.getLogger(); - - private KafkaProducerUtil() { - } - - /** - * 使用kafka发送流控数据和心跳数据 - * - * @param topic kafka的topic - * @param msg 流控和心跳数据 - */ - public static void sendMessage(String topic, String msg) { - KafkaProducer producer = KafkaProducerEnum.KAFKA_PRODUCER.getKafkaProducer(); - try { - sendRecord(topic, msg, producer); - } finally { - // 此处需要保证数据实时性, 保证心跳正常 - producer.flush(); - } - } - - private static void sendRecord(final String topic, String msg, - KafkaProducer producer) { - ProducerRecord record; - try { - record = new ProducerRecord(topic, null, msg); - - // 异步回调通发送数据 - producer.send(record, new Callback() { - @Override - public void onCompletion(RecordMetadata metadata, Exception exception) { - if (exception != null) { - LOGGER.severe("[KafkaProducerUtil] kafka exception in " + topic + exception.getMessage()); - } - } - }); - } catch (KafkaException e) { - LOGGER.severe("[KafkaProducerUtil] sendMessage() exception " + e); - } - } -} - diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/util/PluginConfigUtil.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/util/PluginConfigUtil.java deleted file mode 100644 index 5d64f9670f..0000000000 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/util/PluginConfigUtil.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (C) 2022-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. - * 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.huawei.flowcontrol.common.util; - -import com.huawei.flowcontrol.common.config.ConfigConst; -import com.huawei.flowcontrol.common.config.FlowControlConfig; -import com.huawei.flowcontrol.common.config.KafkaConnectBySslSwitch; - -import com.huaweicloud.sermant.core.common.LoggerFactory; -import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; - -import java.util.Locale; -import java.util.Map; -import java.util.Properties; -import java.util.logging.Logger; - -/** - * 配置信息工具类 - * - * @author zhouss - * @since 2022-02-11 - */ -public class PluginConfigUtil { - private static final Logger LOGGER = LoggerFactory.getLogger(); - - private static Properties properties; - - private PluginConfigUtil() { - } - - static { - // 待后续接入配置中心 - setPropertiesFromFlowControlConfig(PluginConfigManager.getPluginConfig(FlowControlConfig.class)); - } - - /** - * 通过Key获取Value - * - * @param key key值 - * @return String 通过key获取value - */ - public static String getValueByKey(String key) { - final Object property = properties.get(key); - if (property == null) { - return ""; - } - return String.valueOf(property).trim(); - } - - /** - * 将配置类的配置设置到properties,减少代码修改 - */ - private static void setPropertiesFromFlowControlConfig(FlowControlConfig flowControlConfig) { - properties = new Properties(); - setValueForProperties(flowControlConfig); - LOGGER.info("All flow-control config following."); - for (Map.Entry entry : properties.entrySet()) { - LOGGER.info(String.format(Locale.ENGLISH, "%s = %s", entry.getKey(), entry.getValue())); - } - } - - private static void setValueForProperties(FlowControlConfig flowControlConfig) { - properties.put(ConfigConst.DEFAULT_HEARTBEAT_INTERVAL, flowControlConfig.getHeartbeatInterval()); - properties.put(ConfigConst.DEFAULT_METRIC_INTERVAL, flowControlConfig.getMetricInterval()); - properties.put(ConfigConst.METRIC_INITIAL_DURATION, flowControlConfig.getMetricInitialDuration()); - properties.put(ConfigConst.METRIC_MAX_LINE, flowControlConfig.getMetricMaxLine()); - properties.put(ConfigConst.METRIC_SLEEP_TIME, flowControlConfig.getMetricSleepTime()); - properties.put(ConfigConst.KAFKA_BOOTSTRAP_SERVERS, flowControlConfig.getKafkaBootstrapServers()); - properties.put(ConfigConst.KAFKA_KEY_SERIALIZER, flowControlConfig.getKafkaKeySerializer()); - properties.put(ConfigConst.KAFKA_VALUE_SERIALIZER, flowControlConfig.getKafkaValueSerializer()); - properties.put(ConfigConst.KAFKA_METRIC_TOPIC, flowControlConfig.getKafkaMetricTopic()); - properties.put(ConfigConst.KAFKA_HEARTBEAT_TOPIC, flowControlConfig.getKafkaHeartbeatTopic()); - properties.put(ConfigConst.KAFKA_ACKS, flowControlConfig.getKafkaAcks()); - properties.put(ConfigConst.KAFKA_MAX_REQUEST_SIZE, flowControlConfig.getKafkaMaxRequestSize()); - properties.put(ConfigConst.KAFKA_BUFFER_MEMORY, flowControlConfig.getKafkaBufferMemory()); - properties.put(ConfigConst.KAFKA_RETRIES, flowControlConfig.getKafkaRetries()); - properties.put(ConfigConst.KAFKA_REQUEST_TIMEOUT_MS, flowControlConfig.getKafkaRequestTimeoutMs()); - properties.put(ConfigConst.KAFKA_MAX_BLOCK_MS, flowControlConfig.getKafkaMaxBlockMs()); - properties.put(ConfigConst.KAFKA_JAAS_CONFIG_CONST, flowControlConfig.getKafkaJaasConfig()); - properties.put(ConfigConst.KAFKA_SASL_MECHANISM_CONST, flowControlConfig.getKafkaSaslMechanism()); - properties.put(ConfigConst.KAFKA_SECURITY_PROTOCOL_CONST, flowControlConfig.getKafkaSecurityProtocol()); - properties.put(ConfigConst.KAFKA_SSL_TRUSTSTORE_LOCATION_CONST, - flowControlConfig.getKafkaSslTruststoreLocation()); - properties.put(ConfigConst.KAFKA_SSL_TRUSTSTORE_PASSWORD_CONST, - flowControlConfig.getKafkaSslTruststorePassword()); - properties.put(ConfigConst.KAFKA_IDENTIFICATION_ALGORITHM_CONST, - flowControlConfig.getKafkaIdentificationAlgorithm()); - properties.put(ConfigConst.CONFIG_KIE_ADDRESS, flowControlConfig.getConfigKieAddress()); - KafkaConnectBySslSwitch.delKey(properties); - } -} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/util/ReflectUtils.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/util/ReflectUtils.java index aae30e4d39..0743d1e118 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/util/ReflectUtils.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/util/ReflectUtils.java @@ -23,6 +23,8 @@ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.Locale; import java.util.Map; import java.util.Optional; @@ -81,7 +83,12 @@ public static Optional invokeTargetMethod(Object target, String methodNa final Optional method = REFLECT_METHOD_CACHE .computeIfAbsent(formatReflectKey(target, methodName), fn -> { try { - return Optional.of(target.getClass().getDeclaredMethod(methodName, parameterTypes)); + final Method declaredMethod = target.getClass().getDeclaredMethod(methodName, parameterTypes); + AccessController.doPrivileged((PrivilegedAction) () -> { + declaredMethod.setAccessible(true); + return Boolean.TRUE; + }); + return Optional.of(declaredMethod); } catch (NoSuchMethodException ex) { LoggerFactory.getLogger().warning(String.format(Locale.ENGLISH, "No such method [%s] at class [%s]", methodName, target.getClass().getName())); diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/BaseTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/BaseTest.java deleted file mode 100644 index a829fe63b0..0000000000 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/BaseTest.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * 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. - * 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.huawei.flowcontrol.common; - -import com.huawei.flowcontrol.common.config.FlowControlConfig; - -import com.huaweicloud.sermant.core.config.ConfigManager; -import com.huaweicloud.sermant.core.config.common.BaseConfig; -import com.huaweicloud.sermant.core.plugin.config.ServiceMeta; -import com.huaweicloud.sermant.core.service.ServiceManager; -import com.huaweicloud.sermant.core.service.dynamicconfig.config.DynamicConfig; -import com.huaweicloud.sermant.core.service.heartbeat.config.HeartbeatConfig; -import com.huaweicloud.sermant.core.service.send.config.BackendConfig; - -import org.junit.BeforeClass; - -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.util.Map; - -/** - * 测试基础化基类 - * - * @author zhouss - * @since 2021-12-25 - */ -public class BaseTest { - /** - * 统一配置缓存 - */ - protected static Map configManagerMap; - - /** - * 当前classloader - */ - protected static ClassLoader currentClassLoader; - - protected BaseTest() { - } - - /** - * 初始化 - * - * @throws Exception 初始化失败抛出 - */ - @BeforeClass - public static void init() throws Exception { - currentClassLoader = Thread.currentThread().getContextClassLoader(); - final Class agentConfigManagerClass = currentClassLoader.loadClass(BackendConfig.class.getName()); - final Field nettyServerPort = agentConfigManagerClass.getDeclaredField("prot"); - setFieldValue(nettyServerPort, "6888", null); - - final Field nettyServerIp = agentConfigManagerClass.getDeclaredField("ip"); - setFieldValue(nettyServerIp, "127.0.0.1", null); - - final Class configManagerClass = currentClassLoader.loadClass(ConfigManager.class.getName()); - final Field configMap = configManagerClass.getDeclaredField("CONFIG_MAP"); - configMap.setAccessible(true); - removeFinalModify(configMap); - final Object obj = configMap.get(null); - if (obj instanceof Map) { - configManagerMap = (Map) obj; - configManagerMap.put("heartbeat", new HeartbeatConfig()); - configManagerMap.put("dynamic.config", new DynamicConfig()); - configManagerMap.put("service.meta", new ServiceMeta()); - ServiceManager.initServices(); - configManagerMap.put("flow.control.plugin", new FlowControlConfig()); - } - } - - /** - * 移除final修饰符 - * - * @param field 字段 - * @throws NoSuchFieldException 无该字段抛出 - * @throws IllegalAccessException 无法拿到该字段抛出 - */ - 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); - } - - /** - * 反射设置字段值 - * - * @param field 字段 - * @param value 值 - * @param target 目前类 静态属性为null - * @throws IllegalAccessException 找不到抛出异常 - */ - protected static void setFieldValue(Field field, Object value, Object target) throws IllegalAccessException { - field.setAccessible(true); - field.set(target, value); - } -} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/cache/ConcurrentMapCacheTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/cache/ConcurrentMapCacheTest.java new file mode 100644 index 0000000000..b817191576 --- /dev/null +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/cache/ConcurrentMapCacheTest.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2022-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. + * 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.huawei.flowcontrol.common.cache; + +import static org.junit.Assert.*; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.concurrent.ConcurrentHashMap; + +/** + * 缓存测试 + * + * @author zhouss + * @since 2022-08-29 + */ +public class ConcurrentMapCacheTest { + /** + * 缓存测试 + */ + @Test + public void test() { + String key = "test"; + String value = "val"; + final ConcurrentMapCache cache = new ConcurrentMapCache<>(); + cache.put(key, value); + assertEquals(cache.get(key), value); + cache.evict(key); + assertNull(cache.get(key)); + cache.put(key, value); + assertEquals(1, cache.size()); + cache.release(); + assertEquals(0, cache.size()); + assertTrue(cache.getCacheTarget() instanceof ConcurrentHashMap); + } +} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/cache/TimedConcurrentMapCacheTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/cache/TimedConcurrentMapCacheTest.java new file mode 100644 index 0000000000..18402eaa14 --- /dev/null +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/cache/TimedConcurrentMapCacheTest.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2022-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. + * 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.huawei.flowcontrol.common.cache; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import com.huaweicloud.sermant.core.utils.ReflectUtils; + +import org.junit.Test; + +import java.util.concurrent.ConcurrentHashMap; + +/** + * 定时清理缓存测试 + * + * @author zhouss + * @since 2022-08-29 + */ +public class TimedConcurrentMapCacheTest { + /** + * 测试 + */ + @Test + public void test() throws InterruptedException { + int maxSize = 3; + long evictMs = 500L; + final Key key = new Key(); + String value = "val"; + final TimedConcurrentMapCache cache = new TimedConcurrentMapCache<>(maxSize, evictMs); + + // 基本测试 + cache.put(key, value); + assertEquals(cache.get(key), value); + cache.evict(key); + assertNull(cache.get(key)); + cache.put(key, value); + assertEquals(1, cache.size()); + cache.release(); + assertEquals(0, cache.size()); + assertTrue(cache.getCacheTarget() instanceof ConcurrentHashMap); + + // 过期测试 + cache.put(new Key(), value); + cache.put(new Key(), value); + cache.put(new Key(), value); + ReflectUtils.invokeMethod(cache, "removeEvictedCache", null, null); + cache.put(new Key(), value); + assertEquals(3, cache.size()); + Thread.sleep(evictMs); + ReflectUtils.invokeMethod(cache, "removeEvictedCache", null, null); + assertEquals(0, cache.size()); + } + + static class Key implements Timed { + private long timestamp = System.currentTimeMillis(); + + @Override + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } + + @Override + public long getTimestamp() { + return timestamp; + } + } +} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/ResolverManagerTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/ResolverManagerTest.java new file mode 100644 index 0000000000..83be7f3661 --- /dev/null +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/ResolverManagerTest.java @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2022-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. + * 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.huawei.flowcontrol.common.core; + +import static org.junit.Assert.*; + +import com.huawei.flowcontrol.common.core.resolver.AbstractResolver; +import com.huawei.flowcontrol.common.core.resolver.AbstractRuleResolverTest; +import com.huawei.flowcontrol.common.core.resolver.BulkheadRuleResolver; +import com.huawei.flowcontrol.common.core.resolver.BulkheadRuleResolverTest; +import com.huawei.flowcontrol.common.core.resolver.CircuitBreakerRuleResolver; +import com.huawei.flowcontrol.common.core.resolver.CircuitBreakerRuleResolverTest; +import com.huawei.flowcontrol.common.core.resolver.FaultRuleResolver; +import com.huawei.flowcontrol.common.core.resolver.FaultRuleResolverTest; +import com.huawei.flowcontrol.common.core.resolver.InstanceIsolationRuleResolver; +import com.huawei.flowcontrol.common.core.resolver.InstanceIsolationRuleResolverTest; +import com.huawei.flowcontrol.common.core.resolver.RateLimitingRuleResolver; +import com.huawei.flowcontrol.common.core.resolver.RateLimitingRuleResolverTest; +import com.huawei.flowcontrol.common.core.resolver.RetryResolver; +import com.huawei.flowcontrol.common.core.resolver.RetryResolverTest; +import com.huawei.flowcontrol.common.core.rule.AbstractRule; +import com.huawei.flowcontrol.common.core.rule.BulkheadRule; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Map; + +/** + * 测试规则管理器 + * + * @author zhouss + * @since 2022-08-29 + */ +public class ResolverManagerTest { + private final ResolverManager instance = ResolverManager.INSTANCE; + + @Before + public void init() { + final Map> resolversMap = instance.getResolversMap(); + resolversMap.put(BulkheadRuleResolver.CONFIG_KEY + ".", new BulkheadRuleResolver()); + resolversMap.put(CircuitBreakerRuleResolver.CONFIG_KEY + ".", new CircuitBreakerRuleResolver()); + resolversMap.put(InstanceIsolationRuleResolver.CONFIG_KEY + ".", new InstanceIsolationRuleResolver()); + resolversMap.put(FaultRuleResolver.CONFIG_KEY + ".", new FaultRuleResolver()); + resolversMap.put(RateLimitingRuleResolver.CONFIG_KEY + ".", new RateLimitingRuleResolver()); + resolversMap.put(RetryResolver.CONFIG_KEY + ".", new RetryResolver()); + } + + @Test + public void testIsTarget() { + Assert.assertTrue(instance.isTarget(BulkheadRuleResolver.CONFIG_KEY + ".")); + Assert.assertTrue(instance.isTarget(CircuitBreakerRuleResolver.CONFIG_KEY + ".")); + Assert.assertTrue(instance.isTarget(InstanceIsolationRuleResolver.CONFIG_KEY + ".")); + Assert.assertTrue(instance.isTarget(FaultRuleResolver.CONFIG_KEY + ".")); + Assert.assertTrue(instance.isTarget(RateLimitingRuleResolver.CONFIG_KEY + ".")); + Assert.assertTrue(instance.isTarget(RetryResolver.CONFIG_KEY + ".")); + } + + /** + * 测试解析 + */ + @Test + public void testResolve() { + testTargetResolver(new BulkheadRuleResolverTest()); + testTargetResolver(new InstanceIsolationRuleResolverTest()); + testTargetResolver(new CircuitBreakerRuleResolverTest()); + testTargetResolver(new FaultRuleResolverTest()); + testTargetResolver(new RetryResolverTest()); + testTargetResolver(new RateLimitingRuleResolverTest()); + } + + private void testTargetResolver(AbstractRuleResolverTest resolverTest) { + instance.resolve(resolverTest.getKey(), resolverTest.getValue(), false); + resolverTest.checkAttrs((T) instance.getResolver(resolverTest.getConfigKey()) + .getRules().get(resolverTest.getBusinessKey())); + Assert.assertTrue(instance.hasMatchedRule(resolverTest.getBusinessKey())); + } +} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/RuleUtilsTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/RuleUtilsTest.java new file mode 100644 index 0000000000..e4bf946122 --- /dev/null +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/RuleUtilsTest.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2022-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. + * 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.huawei.flowcontrol.common.core; + +import com.huawei.flowcontrol.common.core.resolver.BulkheadRuleResolverTest; +import com.huawei.flowcontrol.common.core.rule.BulkheadRule; + +import org.junit.Assert; +import org.junit.Test; + +/** + * 规则工具类测试 + * + * @author zhouss + * @since 2022-08-29 + */ +public class RuleUtilsTest { + /** + * 测试工具类获取规则 + */ + @Test + public void testGetRule() { + final ResolverManagerTest resolverManagerTest = new ResolverManagerTest(); + resolverManagerTest.init(); + resolverManagerTest.testResolve(); + final BulkheadRuleResolverTest bulkheadRuleResolverTest = new BulkheadRuleResolverTest(); + final BulkheadRule rule = RuleUtils + .getRule(bulkheadRuleResolverTest.getBusinessKey(), bulkheadRuleResolverTest.getConfigKey(), + BulkheadRule.class); + Assert.assertNotNull(rule); + } +} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/AbstractRuleResolverTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/AbstractRuleResolverTest.java new file mode 100644 index 0000000000..67d96b341e --- /dev/null +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/AbstractRuleResolverTest.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2022-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. + * 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.huawei.flowcontrol.common.core.resolver; + +import com.huawei.flowcontrol.common.core.rule.AbstractRule; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Optional; + +/** + * 规则解析测试 + * + * @author zhouss + * @since 2022-08-29 + */ +public abstract class AbstractRuleResolverTest { + @Test + public void test() { + final AbstractResolver resolver = getResolver(); + Assert.assertEquals(resolver.getConfigKey(), getConfigKey()); + final Optional test = resolver.parseRule(getKey(), getValue(), true, false); + Assert.assertTrue(test.isPresent()); + checkAttrs(test.get()); + final Optional delete = resolver.parseRule(getKey(), getValue(), true, true); + Assert.assertFalse(delete.isPresent()); + } + + /** + * 下发配置键 + * + * @return 配置键 + */ + public String getKey() { + return getConfigKey() + "." + getBusinessKey(); + } + + /** + * 获取业务场景名 + * + * @return 业务场景名 + */ + public String getBusinessKey() { + return "test"; + } + + /** + * 获取解析器 + * + * @return 解析器 + */ + public abstract AbstractResolver getResolver(); + + /** + * 获取配置键 + * + * @return 配置键 + */ + public abstract String getConfigKey(); + + /** + * 获取配置值 + * + * @return 配置 + */ + public abstract String getValue(); + + /** + * 判断属性是否正确 + * + * @param rule 解析的对象 + */ + public abstract void checkAttrs(T rule); +} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/BulkheadRuleResolverTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/BulkheadRuleResolverTest.java new file mode 100644 index 0000000000..93b7aeabb9 --- /dev/null +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/BulkheadRuleResolverTest.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2022-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. + * 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.huawei.flowcontrol.common.core.resolver; + +import com.huawei.flowcontrol.common.core.rule.BulkheadRule; + +import org.junit.Assert; + +/** + * bulkhead测试 + * + * @author zhouss + * @since 2022-08-29 + */ +public class BulkheadRuleResolverTest extends AbstractRuleResolverTest { + private static final int CONFIG_CALLS = 2; + + private static final long MAX_WAIT = 4000L; + + @Override + public AbstractResolver getResolver() { + return new BulkheadRuleResolver(); + } + + @Override + public String getConfigKey() { + return BulkheadRuleResolver.CONFIG_KEY; + } + + @Override + public String getValue() { + return "maxConcurrentCalls: \"2\"\n" + + "maxWaitDuration: 4S"; + } + + @Override + public void checkAttrs(BulkheadRule rule) { + Assert.assertEquals(CONFIG_CALLS, rule.getMaxConcurrentCalls()); + Assert.assertEquals(MAX_WAIT, rule.getParsedMaxWaitDuration()); + } +} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/CircuitBreakerRuleResolverTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/CircuitBreakerRuleResolverTest.java new file mode 100644 index 0000000000..c84c481047 --- /dev/null +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/CircuitBreakerRuleResolverTest.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2022-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. + * 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.huawei.flowcontrol.common.core.resolver; + +import com.huawei.flowcontrol.common.core.rule.CircuitBreakerRule; + +import org.junit.Assert; + +/** + * 熔断规则解析测试 + * + * @author zhouss + * @since 2022-08-29 + */ +public class CircuitBreakerRuleResolverTest extends AbstractRuleResolverTest { + private static final int CONFIG_FAILURE_RATE = 80; + + private static final int CONFIG_MIN_NUM_CALLS = 2; + + private static final int CONFIG_SLIDING_WINDOW_SIZE = 10000; + + private static final String CONFIG_SLIDING_WINDOW_TYPE = "time"; + + private static final int CONFIG_SLOW_CALL_DURATION_THRESHOLD = 100; + + private static final int CONFIG_SLOW_CALL_RATE_THRESHOLD = 60; + + private static final long CONFIG_WAIT_DURATION_IN_OPEN_STATE = 10000L; + + private static final double DELTA = 1 >> 6; + + @Override + public AbstractResolver getResolver() { + return new CircuitBreakerRuleResolver(); + } + + @Override + public String getConfigKey() { + return CircuitBreakerRuleResolver.CONFIG_KEY; + } + + @Override + public String getValue() { + return "failureRateThreshold: 80\n" + + "minimumNumberOfCalls: 2\n" + + "name: 熔断\n" + + "slidingWindowSize: 10000\n" + + "slidingWindowType: time\n" + + "slowCallDurationThreshold: \"100\"\n" + + "slowCallRateThreshold: 60\n" + + "waitDurationInOpenState: 10s"; + } + + @Override + public void checkAttrs(CircuitBreakerRule rule) { + Assert.assertEquals(CONFIG_FAILURE_RATE, rule.getFailureRateThreshold(), DELTA); + Assert.assertEquals(CONFIG_MIN_NUM_CALLS, rule.getMinimumNumberOfCalls()); + Assert.assertEquals(CONFIG_SLIDING_WINDOW_SIZE, rule.getParsedSlidingWindowSize()); + Assert.assertEquals(CONFIG_SLIDING_WINDOW_TYPE, rule.getSlidingWindowType()); + Assert.assertEquals(CONFIG_SLOW_CALL_DURATION_THRESHOLD, rule.getParsedSlowCallDurationThreshold()); + Assert.assertEquals(CONFIG_SLOW_CALL_RATE_THRESHOLD, rule.getSlowCallRateThreshold(), DELTA); + Assert.assertEquals(CONFIG_WAIT_DURATION_IN_OPEN_STATE, rule.getParsedWaitDurationInOpenState()); + } +} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/FaultRuleResolverTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/FaultRuleResolverTest.java new file mode 100644 index 0000000000..f253d864b6 --- /dev/null +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/FaultRuleResolverTest.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2022-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. + * 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.huawei.flowcontrol.common.core.resolver; + +import com.huawei.flowcontrol.common.core.constants.RuleConstants; +import com.huawei.flowcontrol.common.core.rule.fault.FaultRule; + +import org.junit.Assert; + +/** + * 错误注入解析器测试 + * + * @author zhouss + * @since 2022-08-29 + */ +public class FaultRuleResolverTest extends AbstractRuleResolverTest { + private static final long CONFIG_DELAY_TIME = 5000L; + + private static final int CONFIG_PERCENT = 50; + + private static final String CONFIG_FALLBACK_TYPE = RuleConstants.FAULT_RULE_FALLBACK_NULL_TYPE; + + private static final String CONFIG_TYPE = RuleConstants.FAULT_RULE_DELAY_TYPE; + + @Override + public AbstractResolver getResolver() { + return new FaultRuleResolver(); + } + + @Override + public String getConfigKey() { + return FaultRuleResolver.CONFIG_KEY; + } + + @Override + public String getValue() { + return "type: delay\n" + + "percentage: 50\n" + + "fallbackType: ReturnNull\n" + + "forceClosed: true\n" + + "delayTime: 5S"; + } + + @Override + public void checkAttrs(FaultRule rule) { + Assert.assertEquals(CONFIG_DELAY_TIME, rule.getParsedDelayTime()); + Assert.assertEquals(CONFIG_PERCENT, rule.getPercentage()); + Assert.assertEquals(CONFIG_FALLBACK_TYPE, rule.getFallbackType()); + Assert.assertEquals(CONFIG_TYPE, rule.getType()); + Assert.assertTrue(rule.isForceClosed()); + } +} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/InstanceIsolationRuleResolverTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/InstanceIsolationRuleResolverTest.java new file mode 100644 index 0000000000..89a0a89bc0 --- /dev/null +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/InstanceIsolationRuleResolverTest.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2022-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. + * 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.huawei.flowcontrol.common.core.resolver; + +import static org.junit.Assert.*; + +import com.huawei.flowcontrol.common.core.rule.CircuitBreakerRule; + +/** + * 隔离仓测试 + * + * @author zhouss + * @since 2022-08-29 + */ +public class InstanceIsolationRuleResolverTest extends CircuitBreakerRuleResolverTest { + @Override + public String getConfigKey() { + return InstanceIsolationRuleResolver.CONFIG_KEY; + } + + @Override + public AbstractResolver getResolver() { + return new InstanceIsolationRuleResolver(); + } +} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/RateLimitingRuleResolverTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/RateLimitingRuleResolverTest.java new file mode 100644 index 0000000000..22682e6e76 --- /dev/null +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/RateLimitingRuleResolverTest.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2022-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. + * 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.huawei.flowcontrol.common.core.resolver; + +import static org.junit.Assert.*; + +import com.huawei.flowcontrol.common.core.rule.RateLimitingRule; + +import org.junit.Assert; + +/** + * 限流规则解析 + * + * @author zhouss + * @since 2022-08-29 + */ +public class RateLimitingRuleResolverTest extends AbstractRuleResolverTest { + private static final long CONFIG_LIMIT_REFRESH_PERIOD = 1000L; + + private static final long CONFIG_RATE = 999L; + + @Override + public AbstractResolver getResolver() { + return new RateLimitingRuleResolver(); + } + + @Override + public String getConfigKey() { + return RateLimitingRuleResolver.CONFIG_KEY; + } + + @Override + public String getValue() { + return "limitRefreshPeriod: \"1000\"\n" + + "name: flow\n" + + "rate: \"999\""; + } + + @Override + public void checkAttrs(RateLimitingRule rule) { + Assert.assertEquals(CONFIG_LIMIT_REFRESH_PERIOD, rule.getParsedLimitRefreshPeriod()); + Assert.assertEquals(CONFIG_RATE, rule.getRate()); + } +} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/RetryResolverTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/RetryResolverTest.java new file mode 100644 index 0000000000..2f860acd1c --- /dev/null +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/RetryResolverTest.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2022-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. + * 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.huawei.flowcontrol.common.core.resolver; + +import com.huawei.flowcontrol.common.core.rule.RetryRule; + +import org.junit.Assert; + +/** + * 重试规则 + * + * @author zhouss + * @since 2022-08-29 + */ +public class RetryResolverTest extends AbstractRuleResolverTest { + private static final long CONFIG_WAIT_DURATION = 2000L; + + private static final String CONFIG_RETRY_STRATEGY = "FixedInterval"; + + private static final int CONFIG_MAX_ATTEMPTS = 2; + + private static final String CONFIG_RETRY_ON_RESPONSE_STATUS = "500"; + + @Override + public AbstractResolver getResolver() { + return new RetryResolver(); + } + + @Override + public String getConfigKey() { + return RetryResolver.CONFIG_KEY; + } + + @Override + public String getValue() { + return "waitDuration: \"2000\"\n" + + "retryStrategy: FixedInterval\n" + + "maxAttempts: 2\n" + + "retryOnResponseStatus:\n" + + "- 500"; + } + + @Override + public void checkAttrs(RetryRule rule) { + Assert.assertEquals(CONFIG_WAIT_DURATION, rule.getParsedWaitDuration()); + Assert.assertEquals(CONFIG_RETRY_STRATEGY, rule.getRetryStrategy()); + Assert.assertEquals(CONFIG_MAX_ATTEMPTS, rule.getMaxAttempts()); + Assert.assertTrue(rule.getRetryOnResponseStatus().contains(CONFIG_RETRY_ON_RESPONSE_STATUS)); + } +} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/RuleResolverTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/RuleResolverTest.java index f20bd39ed0..a5c0059f4a 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/RuleResolverTest.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/core/resolver/RuleResolverTest.java @@ -36,6 +36,8 @@ public void test() { testResolver(new BulkheadRuleResolver()); testResolver(new CircuitBreakerRuleResolver()); testResolver(new RetryResolver()); + testResolver(new FaultRuleResolver()); + testResolver(new InstanceIsolationRuleResolver()); } private void testResolver(AbstractResolver resolver) { diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/entity/RequestEntityTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/entity/RequestEntityTest.java new file mode 100644 index 0000000000..2c063ddfac --- /dev/null +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/entity/RequestEntityTest.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2022-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. + * 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.huawei.flowcontrol.common.entity; + +import static org.junit.Assert.assertEquals; + +import com.huawei.flowcontrol.common.entity.HttpRequestEntity.Builder; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * 测试一些hashcode + * + * @author zhouss + * @since 2022-08-29 + */ +public class RequestEntityTest { + /** + * 测试hash + */ + @Test + public void test() { + String serviceName = "test"; + String apiPath = "/api"; + final Builder builder1 = new Builder(); + builder1.setServiceName(serviceName); + builder1.setHeaders(buildHeaders()); + builder1.setApiPath(apiPath); + + final Builder builder2 = new Builder(); + builder2.setServiceName(serviceName); + builder2.setHeaders(buildHeaders()); + builder2.setApiPath(apiPath); + + assertEquals(builder2.build(), builder1.build()); + } + + private Map buildHeaders() { + final Map headers = new HashMap<>(); + headers.put("token", "aasdjosajdosadpsakdpsakpdjsjdnsa"); + headers.put("api", "/test/aaa"); + return headers; + } +} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/factory/FlowControlThreadFactoryTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/factory/FlowControlThreadFactoryTest.java new file mode 100644 index 0000000000..5b3f217cfd --- /dev/null +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/factory/FlowControlThreadFactoryTest.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2022-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. + * 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.huawei.flowcontrol.common.factory; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * 线程工厂测试 + * + * @author zhouss + * @since 2022-08-29 + */ +public class FlowControlThreadFactoryTest { + @Test + public void test() { + final String threadName = "thread-name"; + final ExecutorService executorService = Executors + .newFixedThreadPool(1, new FlowControlThreadFactory(threadName)); + executorService.execute(() -> { + Assert.assertEquals(threadName, Thread.currentThread().getName()); + }); + executorService.shutdown(); + } +} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/handler/retry/RetryContextTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/handler/retry/RetryContextTest.java new file mode 100644 index 0000000000..2ecf6b65c0 --- /dev/null +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/handler/retry/RetryContextTest.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2022-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. + * 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.huawei.flowcontrol.common.handler.retry; + +import com.huawei.flowcontrol.common.core.rule.RetryRule; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Set; + +/** + * 重试测试 + * + * @author zhouss + * @since 2022-08-29 + */ +public class RetryContextTest { + /** + * mark测试 + */ + @Test + public void testMark() { + RetryContext.INSTANCE.markRetry(buildRetry()); + Assert.assertTrue(RetryContext.INSTANCE.isMarkedRetry()); + RetryContext.INSTANCE.remove(); + Assert.assertFalse(RetryContext.INSTANCE.isMarkedRetry()); + } + + /** + * 测试重试策略 + */ + @Test + public void testRetryPolicy() { + int maxRetry = 8; + final RetryRule retryRule = new RetryRule(); + retryRule.setRetryOnSame(maxRetry); + retryRule.setFailAfterMaxAttempts(true); + RetryContext.INSTANCE.buildRetryPolicy(retryRule); + Assert.assertFalse(RetryContext.INSTANCE.isPolicyNeedRetry()); + + // 开始模拟重试 + final Object instance = new Object(); + for (int i = 0; i < maxRetry; i++) { + muteRetry(instance); + } + + // 超过最大重试次数则重试完成 + RetryContext.INSTANCE.updateServiceInstance(instance); + Assert.assertFalse(RetryContext.INSTANCE.isPolicyNeedRetry()); + RetryContext.INSTANCE.remove(); + } + + private void muteRetry(Object instance) { + RetryContext.INSTANCE.updateServiceInstance(instance); + Assert.assertTrue(RetryContext.INSTANCE.isPolicyNeedRetry()); + } + + private Retry buildRetry() { + return new Retry() { + @Override + public boolean needRetry(Set statusList, Object result) { + return false; + } + + @Override + public Class[] retryExceptions() { + return new Class[0]; + } + + @Override + public RetryFramework retryType() { + return RetryFramework.ALIBABA_DUBBO; + } + }; + } +} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/init/FlowControlInitServiceImplTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/init/FlowControlInitServiceImplTest.java index 0b1022b6ab..6ec52736cc 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/init/FlowControlInitServiceImplTest.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/init/FlowControlInitServiceImplTest.java @@ -17,8 +17,6 @@ package com.huawei.flowcontrol.common.init; -import com.huawei.flowcontrol.common.BaseTest; - import org.junit.Test; /** @@ -27,7 +25,7 @@ * @author zhouss * @since 2022-03-03 */ -public class FlowControlInitServiceImplTest extends BaseTest { +public class FlowControlInitServiceImplTest { /** * 测试初始化流程 */ diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/metric/MetricSender.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/util/FilterUtilTest.java similarity index 51% rename from sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/metric/MetricSender.java rename to sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/util/FilterUtilTest.java index 4dd74dd5d2..edcfa6f0e2 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/metric/MetricSender.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/util/FilterUtilTest.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2021-2022 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2022-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. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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, @@ -15,28 +15,28 @@ * */ -package com.huawei.flowcontrol.common.metric; +package com.huawei.flowcontrol.common.util; -import com.huawei.flowcontrol.common.metric.provider.MetricProvider; +import static org.junit.Assert.*; + +import org.junit.Assert; +import org.junit.Test; /** - * 指标发送器 + * 路径格式化测试 * * @author zhouss - * @since 2021-12-07 + * @since 2022-08-29 */ -public interface MetricSender { - /** - * 发送指标数据 - * - * @param data 发送数据 - */ - void sendMetric(Object data); - +public class FilterUtilTest { /** - * 基于提供provider发送数据 - * - * @param metricProvider 指标provider + * 测试 */ - void sendMetric(MetricProvider metricProvider); + @Test + public void test() { + String context = "/context"; + String path = "/api"; + final String url = FilterUtil.filterTarget("\\/api", "\\/context"); + Assert.assertEquals(url, context + path); + } } diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/util/ReflectUtilsTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/util/ReflectUtilsTest.java new file mode 100644 index 0000000000..f12e9b3354 --- /dev/null +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/util/ReflectUtilsTest.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2022-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. + * 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.huawei.flowcontrol.common.util; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Optional; + +/** + * 反射工具类测试 + * + * @author zhouss + * @since 2022-08-29 + */ +public class ReflectUtilsTest { + /** + * 测试定义类 + */ + @Test + public void testDefine() { + final Optional> aClass = ReflectUtils.defineClass("com.est.error"); + Assert.assertFalse(aClass.isPresent()); + final Optional> stringClazz = ReflectUtils.defineClass("java.lang.String"); + Assert.assertTrue(stringClazz.isPresent()); + } + + @Test + public void testInvokeMethod() { + String test = " test "; + final Optional trim = ReflectUtils.invokeTargetMethod(test, "trim", null, null); + Assert.assertTrue(trim.isPresent()); + Assert.assertEquals(trim.get(), test.trim()); + final TestMethod testMethod = new TestMethod(); + final Optional privateMethod = ReflectUtils.invokeTargetMethod(testMethod, "privateMethod", null, null); + Assert.assertTrue(privateMethod.isPresent()); + Assert.assertEquals(privateMethod.get(), "private"); + String name = "Toy"; + final Optional hello = ReflectUtils + .invokeTargetMethod(testMethod, "hello", new Class[]{String.class}, new Object[]{name}); + Assert.assertTrue(hello.isPresent()); + Assert.assertEquals(hello.get(), "hello " + name); + } + + static class TestMethod { + private String privateMethod() { + return "private"; + } + + protected String hello(String name) { + return "hello " + name; + } + } +} diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/util/StringUtilsTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/util/StringUtilsTest.java index c21c4f5da0..2ca0495474 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/util/StringUtilsTest.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/test/java/com/huawei/flowcontrol/common/util/StringUtilsTest.java @@ -34,7 +34,7 @@ public class StringUtilsTest { public void testEqual() { String key = "test"; Assert.assertTrue(StringUtils.equal(key, key)); - Assert.assertTrue(StringUtils.equalIgnoreCase(key, "com.huawei.sermant.core.Test")); + Assert.assertTrue(StringUtils.equalIgnoreCase(key, "Test")); Assert.assertTrue(StringUtils.equalIgnoreCase(key, "teST")); Assert.assertTrue(StringUtils.isEmpty("")); Assert.assertTrue(StringUtils.contains(key, "tes"));