Skip to content

Commit f1a385e

Browse files
committed
feat:support fault injection. (#629)
1 parent 3b3c893 commit f1a385e

File tree

39 files changed

+1475
-12
lines changed

39 files changed

+1475
-12
lines changed

polaris-common/polaris-config-default/src/main/resources/conf/default-config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ consumer:
331331
chain:
332332
# 开启了服务预热插件,可以支持多个动态权重调整插件同时生效
333333
# - warmup
334+
# 故障注入相关配置
335+
fault:
336+
# 是否开启故障注入
337+
enable: true
334338
# 被调方配置
335339
provider:
336340
# Switch of registration

polaris-common/polaris-config/src/main/java/com/tencent/polaris/api/config/consumer/ConsumerConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,11 @@ public interface ConsumerConfig extends Verifier {
9393
Map<String, ? extends DiscoveryConfig> getDiscoveryConfigMap();
9494

9595
WeightAdjustConfig getWeightAdjust();
96+
97+
/**
98+
* consumer.fault前缀开头的所有配置
99+
*
100+
* @return
101+
*/
102+
FaultConfig getFault();
96103
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making polaris-java available.
3+
*
4+
* Copyright (C) 2021 Tencent. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.api.config.consumer;
19+
20+
import com.tencent.polaris.api.config.verify.Verifier;
21+
22+
/**
23+
* 故障注入配置
24+
*
25+
* @author Haotian Zhang
26+
*/
27+
public interface FaultConfig extends Verifier {
28+
29+
/**
30+
* 是否启用故障注入
31+
* consumer.fault.enable
32+
*
33+
* @return 启用鉴权
34+
*/
35+
boolean isEnable();
36+
}

polaris-common/polaris-config/src/main/java/com/tencent/polaris/factory/config/consumer/ConsumerConfigImpl.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.fasterxml.jackson.annotation.JsonIgnore;
2121
import com.fasterxml.jackson.annotation.JsonProperty;
2222
import com.tencent.polaris.api.config.consumer.ConsumerConfig;
23+
import com.tencent.polaris.api.config.consumer.FaultConfig;
2324
import com.tencent.polaris.api.config.consumer.WeightAdjustConfig;
2425
import com.tencent.polaris.api.utils.CollectionUtils;
2526
import com.tencent.polaris.factory.util.ConfigUtils;
@@ -67,6 +68,9 @@ public class ConsumerConfigImpl implements ConsumerConfig {
6768
@JsonProperty
6869
private WeightAdjustConfigImpl weightAdjust;
6970

71+
@JsonProperty
72+
private FaultConfigImpl fault;
73+
7074
@Override
7175
public LocalCacheConfigImpl getLocalCache() {
7276
return localCache;
@@ -132,6 +136,11 @@ public WeightAdjustConfig getWeightAdjust() {
132136
return weightAdjust;
133137
}
134138

139+
@Override
140+
public FaultConfig getFault() {
141+
return fault;
142+
}
143+
135144
@Override
136145
public void verify() {
137146
ConfigUtils.validateNull(localCache, "localCache");
@@ -140,6 +149,7 @@ public void verify() {
140149
ConfigUtils.validateNull(circuitBreaker, "circuitBreaker");
141150
ConfigUtils.validateNull(outlierDetection, "outlierDetection");
142151
ConfigUtils.validateNull(weightAdjust, "weightAdjust");
152+
ConfigUtils.validateNull(fault, "fault");
143153

144154
localCache.verify();
145155
serviceRouter.verify();
@@ -149,6 +159,7 @@ public void verify() {
149159
subscribe.verify();
150160
zeroProtection.verify();
151161
weightAdjust.verify();
162+
fault.verify();
152163
if (CollectionUtils.isNotEmpty(discoveries)) {
153164
for (DiscoveryConfigImpl discoveryConfig : discoveries) {
154165
discoveryConfig.verify();
@@ -183,6 +194,9 @@ public void setDefault(Object defaultObject) {
183194
if (null == weightAdjust) {
184195
weightAdjust = new WeightAdjustConfigImpl();
185196
}
197+
if (null == fault) {
198+
fault = new FaultConfigImpl();
199+
}
186200
if (null != defaultObject) {
187201
ConsumerConfig consumerConfig = (ConsumerConfig) defaultObject;
188202
localCache.setDefault(consumerConfig.getLocalCache());
@@ -193,6 +207,7 @@ public void setDefault(Object defaultObject) {
193207
subscribe.setDefault(consumerConfig.getSubscribe());
194208
zeroProtection.setDefault(consumerConfig.getZeroProtection());
195209
weightAdjust.setDefault(consumerConfig.getWeightAdjust());
210+
fault.setDefault(consumerConfig.getFault());
196211
if (CollectionUtils.isNotEmpty(discoveries)) {
197212
for (DiscoveryConfigImpl discoveryConfig : discoveries) {
198213
discoveryConfig.setDefault(consumerConfig.getDiscoveries().get(0));
@@ -204,14 +219,19 @@ public void setDefault(Object defaultObject) {
204219
}
205220

206221
@Override
207-
@SuppressWarnings("checkstyle:all")
208222
public String toString() {
209223
return "ConsumerConfigImpl{" +
210224
"localCache=" + localCache +
211225
", serviceRouter=" + serviceRouter +
212226
", loadbalancer=" + loadbalancer +
213227
", circuitBreaker=" + circuitBreaker +
214228
", outlierDetection=" + outlierDetection +
229+
", subscribe=" + subscribe +
230+
", zeroProtection=" + zeroProtection +
231+
", discoveries=" + discoveries +
232+
", discoveryConfigMap=" + discoveryConfigMap +
233+
", weightAdjust=" + weightAdjust +
234+
", fault=" + fault +
215235
'}';
216236
}
217237
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making polaris-java available.
3+
*
4+
* Copyright (C) 2021 Tencent. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.factory.config.consumer;
19+
20+
import com.fasterxml.jackson.annotation.JsonProperty;
21+
import com.tencent.polaris.api.config.consumer.FaultConfig;
22+
import com.tencent.polaris.factory.util.ConfigUtils;
23+
24+
/**
25+
* Implementation of {@link FaultConfig}.
26+
*
27+
* @author Haotian Zhang
28+
*/
29+
public class FaultConfigImpl implements FaultConfig {
30+
31+
@JsonProperty
32+
private Boolean enable;
33+
34+
@Override
35+
public boolean isEnable() {
36+
return enable;
37+
}
38+
39+
public void setEnable(Boolean enable) {
40+
this.enable = enable;
41+
}
42+
43+
@Override
44+
public void verify() {
45+
ConfigUtils.validateNull(enable, "consumer.fault.enable");
46+
}
47+
48+
@Override
49+
public void setDefault(Object defaultObject) {
50+
if (null != defaultObject) {
51+
FaultConfig faultConfig = (FaultConfig) defaultObject;
52+
if (null == enable) {
53+
setEnable(faultConfig.isEnable());
54+
}
55+
}
56+
}
57+
}

polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/exception/ErrorCode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ public enum ErrorCode {
130130
*/
131131
CLIENT_CIRCUIT_BREAKING(1020),
132132

133+
/**
134+
* client resource has been fault injected
135+
*/
136+
CLIENT_FAULT_INJECTED(1021),
137+
133138
/**
134139
* 内部错误:连续错误
135140
*/

polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/pojo/ServiceEventKey.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public enum EventType {
5353
LOSSLESS,
5454
BLOCK_ALLOW_RULE,
5555
TRAFFIC_MIRRORING,
56+
FAULT_INJECTION,
5657
}
5758

5859
private final ServiceKey serviceKey;

polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/utils/ClassUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static boolean isClassPresent(String className) {
4242
} catch (ClassNotFoundException e) {
4343
return false;
4444
} catch (Throwable throwable) {
45-
LOG.warn("Failed to check class present", throwable);
45+
LOG.warn("Failed to check class {} present", className, throwable);
4646
return false;
4747
}
4848
}

polaris-factory/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
<artifactId>polaris-auth-factory</artifactId>
5252
<version>${project.version}</version>
5353
</dependency>
54+
<dependency>
55+
<groupId>com.tencent.polaris</groupId>
56+
<artifactId>polaris-fault-factory</artifactId>
57+
<version>${project.version}</version>
58+
</dependency>
5459

5560
<!--测试依赖插件-->
5661
<dependency>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>polaris-fault</artifactId>
7+
<groupId>com.tencent.polaris</groupId>
8+
<version>${revision}</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>polaris-fault-api</artifactId>
14+
<name>Polaris Fault API</name>
15+
<description>Polaris Fault API JAR</description>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.tencent.polaris</groupId>
20+
<artifactId>polaris-plugin-api</artifactId>
21+
<version>${project.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.tencent.polaris</groupId>
25+
<artifactId>polaris-client</artifactId>
26+
<version>${project.version}</version>
27+
</dependency>
28+
</dependencies>
29+
</project>

0 commit comments

Comments
 (0)