Skip to content

Commit 00673ff

Browse files
xcasparRinaisSuper
authored andcommitted
feature: add support for debug mode
1 parent f14f7cc commit 00673ff

File tree

14 files changed

+141
-47
lines changed

14 files changed

+141
-47
lines changed

chaosblade-exec-common/src/main/java/com/alibaba/chaosblade/exec/common/aop/BeforeEnhancer.java

-4
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,10 @@
2121
import com.alibaba.chaosblade.exec.common.center.ManagerFactory;
2222
import com.alibaba.chaosblade.exec.common.injection.Injector;
2323

24-
import org.slf4j.Logger;
25-
import org.slf4j.LoggerFactory;
26-
2724
/**
2825
* @author Changjun Xiao
2926
*/
3027
public abstract class BeforeEnhancer implements Enhancer {
31-
private static final Logger LOGGER = LoggerFactory.getLogger(BeforeEnhancer.class);
3228

3329
/**
3430
* Do fault-inject

chaosblade-exec-common/src/main/java/com/alibaba/chaosblade/exec/common/model/action/connpool/WaitingTriggerConnectionPoolFullExecutor.java

+6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
import javax.sql.DataSource;
44

5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
58
/**
69
* @author Changjun Xiao
710
*/
811
public abstract class WaitingTriggerConnectionPoolFullExecutor extends AbstractConnPoolFullExecutor {
12+
private static final Logger LOGGER = LoggerFactory.getLogger(WaitingTriggerConnectionPoolFullExecutor.class);
13+
914
protected Object dataSource;
1015
/**
1116
* Whether the rule of the experiment is received.
@@ -58,6 +63,7 @@ public DataSource getDataSource() {
5863
public void setDataSource(Object dataSource) {
5964
if (isExpReceived() && this.dataSource == null) {
6065
this.dataSource = dataSource;
66+
LOGGER.debug("trigger druid connection pool full");
6167
triggerFull();
6268
}
6369
}

chaosblade-exec-common/src/main/java/com/alibaba/chaosblade/exec/common/model/action/threadpool/WaitingTriggerThreadPoolFullExecutor.java

+6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.alibaba.chaosblade.exec.common.model.action.threadpool;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
36
/**
47
* @author Changjun Xiao
58
*/
69
public abstract class WaitingTriggerThreadPoolFullExecutor extends AbstractThreadPoolFullExecutor {
710

11+
private static final Logger LOGGER = LoggerFactory.getLogger(WaitingTriggerThreadPoolFullExecutor.class);
812
/**
913
* Whether the rule of the experiment is received.
1014
*/
@@ -30,13 +34,15 @@ protected synchronized void triggerThreadPoolFull() {
3034
if (isRunning()) {
3135
return;
3236
}
37+
LOGGER.debug("trigger thread pool full");
3338
full(getThreadPoolExecutor());
3439
}
3540

3641
@Override
3742
public void revoke() {
3843
super.revoke();
3944
doRevoke();
45+
LOGGER.debug("has revoked thread pool full");
4046
}
4147

4248
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 1999-2019 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.chaosblade.exec.common.util;
18+
19+
import ch.qos.logback.classic.Level;
20+
import ch.qos.logback.classic.LoggerContext;
21+
import org.slf4j.ILoggerFactory;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
25+
/**
26+
* @author Changjun Xiao
27+
*/
28+
public class LogUtil {
29+
30+
/**
31+
* Set log level to debug
32+
*/
33+
public static void setDebug() {
34+
setLogLevel("DEBUG");
35+
}
36+
37+
/**
38+
* Set log level
39+
*
40+
* @param level DEBUG
41+
*/
42+
public static void setLogLevel(String level) {
43+
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
44+
if (loggerFactory instanceof LoggerContext) {
45+
LoggerContext loggerContext = (LoggerContext)loggerFactory;
46+
Logger logger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
47+
((ch.qos.logback.classic.Logger)logger).setLevel(Level.toLevel(level));
48+
return;
49+
}
50+
throw new IllegalStateException("not support the log context object");
51+
}
52+
}

chaosblade-exec-plugin/chaosblade-exec-plugin-druid/src/main/java/com/alibaba/chaosblade/exec/plugin/druid/DruidDataSourceEnhancer.java

+4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ public class DruidDataSourceEnhancer extends BeforeEnhancer {
2020
public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, Object object, Method method,
2121
Object[] methodArguments) throws Exception {
2222
if (object != null && DataSource.class.isInstance(object)) {
23+
LOGGER.debug("match the druid dataSource, object: {}", className);
2324
DruidConnectionPoolFullExecutor.INSTANCE.setDataSource(object);
25+
} else {
26+
LOGGER.debug("the object is null or is not instance of DataSource class, object: {}", object ==
27+
null ? null : object.getClass().getName());
2428
}
2529
return null;
2630
}

chaosblade-exec-plugin/chaosblade-exec-plugin-dubbo/src/main/java/com/alibaba/chaosblade/exec/plugin/dubbo/DubboEnhancer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
8484
int timeout = getTimeout(methodName, object, invocation);
8585
matcherModel.add(DubboConstant.TIMEOUT_KEY, timeout + "");
8686

87-
LOGGER.info("dubbo matchers: {}", JSON.toJSONString(matcherModel));
87+
LOGGER.debug("dubbo matchers: {}", JSON.toJSONString(matcherModel));
8888

8989
EnhancerModel enhancerModel = new EnhancerModel(classLoader, matcherModel);
9090
enhancerModel.setTimeoutExecutor(createTimeoutExecutor(classLoader, timeout));

chaosblade-exec-plugin/chaosblade-exec-plugin-http/src/main/java/com/alibaba/chaosblade/exec/plugin/http/HttpEnhancer.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616

1717
package com.alibaba.chaosblade.exec.plugin.http;
1818

19+
import java.lang.reflect.Method;
20+
1921
import com.alibaba.chaosblade.exec.common.aop.BeforeEnhancer;
2022
import com.alibaba.chaosblade.exec.common.aop.EnhancerModel;
2123
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherModel;
24+
import com.alibaba.fastjson.JSON;
25+
2226
import org.slf4j.Logger;
2327
import org.slf4j.LoggerFactory;
2428

25-
import java.lang.reflect.Method;
26-
2729
/**
2830
* @Author yuhan
2931
* @package: com.alibaba.chaosblade.exec.plugin.http
@@ -40,6 +42,7 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
4042
throws Exception {
4143
MatcherModel matcherModel = new MatcherModel();
4244
matcherModel.add(HttpConstant.URI_KEY, getUrl(methodArguments));
45+
LOGGER.debug("http matchers: {}", JSON.toJSONString(matcherModel));
4346
EnhancerModel enhancerModel = new EnhancerModel(classLoader, matcherModel);
4447
postDoBeforeAdvice(enhancerModel);
4548
return new EnhancerModel(classLoader, matcherModel);
@@ -49,13 +52,14 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
4952

5053
/**
5154
* 获取Http Url
55+
*
5256
* @param object
5357
* @return
5458
*/
55-
protected abstract String getUrl(Object [] object) throws Exception;
59+
protected abstract String getUrl(Object[] object) throws Exception;
5660

57-
public String getUrl(String url){
58-
if(url.contains("?")){
61+
public String getUrl(String url) {
62+
if (url.contains("?")) {
5963
return url.substring(0, url.indexOf("?"));
6064
}
6165
return url;

chaosblade-exec-plugin/chaosblade-exec-plugin-http/src/main/java/com/alibaba/chaosblade/exec/plugin/http/httpclient4/HttpClient4Enhancer.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ protected void postDoBeforeAdvice(EnhancerModel enhancerModel) {
2323
@Override
2424
protected String getUrl(Object [] object) throws Exception {
2525
Object httpRequestBase = object[1];
26-
Method method = methodMap.get(getMehodName());
26+
Method method = methodMap.get(getMethodName());
2727
if (null == method) {
2828
method = object[1].getClass().getMethod(getURI, null);
29-
methodMap.put(getMehodName(), method);
29+
methodMap.put(getMethodName(), method);
3030
}
3131
if (null != method) {
3232
Object invoke = method.invoke(httpRequestBase, null);
@@ -37,7 +37,7 @@ protected String getUrl(Object [] object) throws Exception {
3737
return null;
3838
}
3939

40-
public String getMehodName(){
40+
public String getMethodName(){
4141
return HTTPCLIENT4 + getURI;
4242
}
4343
}

chaosblade-exec-plugin/chaosblade-exec-plugin-jvm/src/main/java/com/alibaba/chaosblade/exec/plugin/jvm/oom/executor/JvmOomExecutor.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.alibaba.chaosblade.exec.plugin.jvm.JvmConstant;
1010
import com.alibaba.chaosblade.exec.plugin.jvm.StoppableActionExecutor;
1111
import com.alibaba.chaosblade.exec.plugin.jvm.oom.JvmMemoryArea;
12+
import com.alibaba.fastjson.JSON;
1213

1314
import org.slf4j.Logger;
1415
import org.slf4j.LoggerFactory;
@@ -22,10 +23,8 @@
2223
*/
2324
public abstract class JvmOomExecutor implements ActionExecutor, StoppableActionExecutor {
2425

25-
protected AtomicBoolean started = new AtomicBoolean(false);
26-
2726
protected static final Logger LOGGER = LoggerFactory.getLogger(JvmOomExecutor.class);
28-
27+
protected AtomicBoolean started = new AtomicBoolean(false);
2928
protected ExecutorService executorService;
3029

3130
/**
@@ -49,6 +48,8 @@ protected void handleThrowable(Throwable throwable) {
4948
public void run(final EnhancerModel enhancerModel) throws Exception {
5049
if (started.compareAndSet(false, true)) {
5150
JvmOomConfiguration jvmOomConfiguration = parse(enhancerModel);
51+
LOGGER.debug("run jvm oom configuration: {}", JSON.toJSONString(jvmOomConfiguration));
52+
5253
executorService = Executors.newFixedThreadPool(jvmOomConfiguration.getThreadCount());
5354
executorService.submit(new Runnable() {
5455
@Override
@@ -65,6 +66,7 @@ public void run() {
6566
public void stop(EnhancerModel enhancerModel) throws Exception {
6667
if (started.compareAndSet(true, false)) {
6768
JvmOomConfiguration jvmOomConfiguration = parse(enhancerModel);
69+
LOGGER.debug("stop jvm oom configuration: {}", JSON.toJSONString(jvmOomConfiguration));
6870
if (executorService == null) { return; }
6971
safelyShutdownExecutor(executorService);
7072
innerStop(enhancerModel);
@@ -109,6 +111,7 @@ private JvmOomConfiguration parse(EnhancerModel enhancerModel) {
109111

110112
private static class JvmOomConfiguration {
111113
private boolean enabledSystemGc;
114+
private Integer threadCount;
112115

113116
public Integer getThreadCount() {
114117
return threadCount;
@@ -118,8 +121,6 @@ public void setThreadCount(Integer threadCount) {
118121
this.threadCount = threadCount;
119122
}
120123

121-
private Integer threadCount;
122-
123124
public boolean isEnabledSystemGc() {
124125
return enabledSystemGc;
125126
}

chaosblade-exec-plugin/chaosblade-exec-plugin-mysql/src/main/java/com/alibaba/chaosblade/exec/plugin/mysql/MysqlEnhancer.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.alibaba.chaosblade.exec.common.util.ReflectUtil;
2525
import com.alibaba.chaosblade.exec.common.util.SQLParserUtil;
2626
import com.alibaba.chaosblade.exec.common.util.SQLParserUtil.SqlType;
27+
import com.alibaba.fastjson.JSON;
2728

2829
import org.slf4j.Logger;
2930
import org.slf4j.LoggerFactory;
@@ -40,7 +41,7 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
4041
Method method, Object[] methodArguments)
4142
throws Exception {
4243
if (methodArguments == null || object == null || methodArguments.length != 10) {
43-
LOGGER.info("The necessary parameters is null or length is not equal 10, {}",
44+
LOGGER.warn("The necessary parameters is null or length is not equal 10, {}",
4445
methodArguments != null ? methodArguments.length : null);
4546
return null;
4647
}
@@ -85,6 +86,7 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
8586
if (port != null) {
8687
matcherModel.add(MysqlConstant.PORT_MATCHER_NAME, port.toString());
8788
}
89+
LOGGER.debug("mysql matchers: {}", JSON.toJSONString(matcherModel));
8890
return new EnhancerModel(classLoader, matcherModel);
8991
}
9092
}

chaosblade-exec-plugin/chaosblade-exec-plugin-postgrelsql/src/main/java/com/alibaba/chaosblade/exec/plugin/postgrelsql/PostgrelsqlEnhancer.java

+12-17
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.alibaba.chaosblade.exec.plugin.postgrelsql;
1818

19-
import java.lang.reflect.Field;
2019
import java.lang.reflect.Method;
2120

2221
import com.alibaba.chaosblade.exec.common.aop.BeforeEnhancer;
@@ -25,6 +24,7 @@
2524
import com.alibaba.chaosblade.exec.common.util.ReflectUtil;
2625
import com.alibaba.chaosblade.exec.common.util.SQLParserUtil;
2726
import com.alibaba.chaosblade.exec.common.util.SQLParserUtil.SqlType;
27+
import com.alibaba.fastjson.JSON;
2828

2929
import org.slf4j.Logger;
3030
import org.slf4j.LoggerFactory;
@@ -36,18 +36,14 @@ public class PostgrelsqlEnhancer extends BeforeEnhancer {
3636

3737
private static final Logger LOGGER = LoggerFactory.getLogger(PostgrelsqlEnhancer.class);
3838

39-
4039
/**
41-
*
4240
* Object[] methodArguments
43-
* Query query,
44-
* ParameterList parameters,
45-
* ResultHandler handler,
46-
* int maxRows,
47-
* int fetchSize,
48-
* int flags
49-
*
50-
*
41+
* Query query,
42+
* ParameterList parameters,
43+
* ResultHandler handler,
44+
* int maxRows,
45+
* int fetchSize,
46+
* int flags
5147
*/
5248
@Override
5349
public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, Object object,
@@ -60,28 +56,26 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
6056
}
6157
Object query = methodArguments[0];
6258

63-
Object pgStream = ReflectUtil.getSuperclassFieldValue(object,"pgStream",false);
64-
Object hostSpec = ReflectUtil.getFieldValue(pgStream,"hostSpec",false);
59+
Object pgStream = ReflectUtil.getSuperclassFieldValue(object, "pgStream", false);
60+
Object hostSpec = ReflectUtil.getFieldValue(pgStream, "hostSpec", false);
6561
String host = ReflectUtil.getFieldValue(hostSpec, "host", false);
6662
Integer port = ReflectUtil.getFieldValue(hostSpec, "port", false);
6763

68-
6964
String sql = null;
7065
if (query != null) {
7166
//获取执行的sql
7267
boolean isPreparedStatement = ReflectUtil.isAssignableFrom(classLoader, query.getClass(),
7368
"org.postgresql.core.Query");
7469
if (isPreparedStatement) {
75-
sql = ReflectUtil.invokeMethod(query, "getNativeSql", new Object[0],false);
70+
sql = ReflectUtil.invokeMethod(query, "getNativeSql", new Object[0], false);
7671
}
7772
} else {
7873
//sql = (String)query;
7974
}
8075

8176
String table = SQLParserUtil.findTableName(sql);
8277
SqlType type = SQLParserUtil.getSqlType(sql);
83-
String database = ReflectUtil.getFieldValue(object,"database",false);
84-
78+
String database = ReflectUtil.getFieldValue(object, "database", false);
8579

8680
MatcherModel matcherModel = new MatcherModel();
8781
matcherModel.add(PostgrelsqlConstant.HOST_MATCHER_NAME, host);
@@ -93,6 +87,7 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
9387
if (port != null) {
9488
matcherModel.add(PostgrelsqlConstant.PORT_MATCHER_NAME, port.toString());
9589
}
90+
LOGGER.debug("postgrelsql matchers: {}", JSON.toJSONString(matcherModel));
9691
return new EnhancerModel(classLoader, matcherModel);
9792
}
9893
}

0 commit comments

Comments
 (0)