Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add business data match #204

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions assembly.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@
<include>**/**</include>
</includes>
</fileSet>
<fileSet>
<directory>chaosblade-exec-spi/target/classes</directory>
<outputDirectory></outputDirectory>
<includes>
<include>**/**</include>
</includes>
</fileSet>

<!-- plugins -->
<fileSet>
Expand Down
5 changes: 5 additions & 0 deletions chaosblade-exec-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@


<dependencies>
<dependency>
<groupId>com.alibaba.chaosblade</groupId>
<artifactId>chaosblade-exec-spi</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@ public void beforeAdvice(String targetName, ClassLoader classLoader, String clas
return;
}
EnhancerModel model = doBeforeAdvice(classLoader, className, object, method, methodArguments);
model = addModelMatchers(classLoader, className, object, method, methodArguments, model, targetName);
if (model == null) {
return;
}
model.setTarget(targetName).setMethod(method).setObject(object).setMethodArguments(methodArguments);
Injector.inject(model);
}

public EnhancerModel addModelMatchers(ClassLoader classLoader, String className, Object object, Method method, Object[] methodArguments, EnhancerModel model, String targetName) throws Exception {
return model;
}

/**
* Do handle enhancer
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.alibaba.chaosblade.exec.common.aop.matcher.busi;

import com.alibaba.chaosblade.exec.common.aop.CustomMatcher;
import com.alibaba.chaosblade.exec.common.util.BusinessParamUtil;

import java.util.List;
import java.util.Map;

/**
* @author wufunc@gmail.com
*/
public class BusinessParamMatcher implements CustomMatcher {
private static final BusinessParamMatcher INSTANCE = new BusinessParamMatcher();

private BusinessParamMatcher() {
}

public static BusinessParamMatcher getInstance() {
return INSTANCE;
}

@Override
public boolean match(String commandValue, Object originValue) {
Map<String, String> businessData = (Map<String, String>) originValue;
List<BusinessParamUtil.BusinessParam> businessParams = BusinessParamUtil.parseFromJsonStr(commandValue);
for (BusinessParamUtil.BusinessParam businessParam : businessParams) {
if (!businessData.containsKey(businessParam.getKey())) {
return false;
}
if (!businessData.get(businessParam.getKey()).equals(businessParam.getValue())) {
return false;
}
}
return true;
}

@Override
public boolean regexMatch(String commandValue, Object originValue) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.alibaba.chaosblade.exec.common.center;


import java.util.*;

public class DefaultSPIServiceManager implements SPIServiceManager {
private static Map<String, List<Object>> spiMap;

static {
spiMap = new HashMap<String, List<Object>>();
}

@Override
public void load() {

}
@Override
public List<Object> getServices(String className, ClassLoader classLoader) {
if (spiMap.containsKey(className)) {
return spiMap.get(className);
}
synchronized (this) {
if (spiMap.containsKey(className)) {
return spiMap.get(className);
}
List<Object> services = loadService(className, classLoader);
spiMap.put(className, services);
return services;
}
}

public List<Object> loadService(String className, ClassLoader classLoader) {
Class clazz;
try {
clazz = classLoader.loadClass(className);
} catch (ClassNotFoundException e) {
return Collections.EMPTY_LIST;
}
ServiceLoader serviceLoader = ServiceLoader.load(clazz, classLoader);
List<Object> objects = new ArrayList<Object>();
for (Object object : serviceLoader) {
objects.add(object);
}
return objects;
}

@Override
public void unload() {
spiMap.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class ManagerFactory {
*/
private static ListenerManager listenerManager = new DefaultListenerManager();

private static SPIServiceManager spiServiceManager = new DefaultSPIServiceManager();

public static StatusManager getStatusManager() {
return statusManager;
}
Expand All @@ -46,10 +48,15 @@ public static ListenerManager getListenerManager() {
return listenerManager;
}

public static SPIServiceManager spiServiceManager() {
return spiServiceManager;
}

public static void load() {
modelSpecManager.load();
listenerManager.load();
statusManager.load();
spiServiceManager.load();
}

/**
Expand All @@ -59,5 +66,6 @@ public static void unload() {
statusManager.unload();
modelSpecManager.unload();
listenerManager.unload();
spiServiceManager.unload();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.alibaba.chaosblade.exec.common.center;

import java.util.List;

public interface SPIServiceManager extends ManagerService{
List<Object> getServices(String className, ClassLoader classLoader);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
public interface ModelConstant {
String JVM_TARGET = "jvm";

String HTTP_TARGET = "http";

String HTTP_URL_MATCHER_NAME = "uri";

/**
* The name of effect percent matcher
*/
Expand All @@ -36,4 +40,9 @@ public interface ModelConstant {
* The flag of regex pattern
*/
String REGEX_PATTERN_FLAG = "regex-pattern";

/**
* the flag of buisness params
*/
String BUSINESS_PARAMS = "b-params";
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
package com.alibaba.chaosblade.exec.common.context;

import java.util.Map;

/**
* @author shizhi.zhu@qunar.com
*/
public class ThreadLocalContext {

private static ThreadLocalContext DEFAULT = new ThreadLocalContext();
private InheritableThreadLocal<Object> local = new InheritableThreadLocal<Object>();
private InheritableThreadLocal<Content> local = new InheritableThreadLocal<Content>();

public static ThreadLocalContext getInstance() {
return DEFAULT;
}

public void set(Object value) {
public void set(Content value) {
local.set(value);
}

public Object get() {
public Content get() {
return local.get();
}

public static class Content{
private StackTraceElement[] stackTraceElements;
private Map<String, Map<String, String>> businessData;

public StackTraceElement[] getStackTraceElements() {
return stackTraceElements;
}

public void setStackTraceElements(StackTraceElement[] stackTraceElements) {
this.stackTraceElements = stackTraceElements;
}

public Map<String, Map<String, String>> getBusinessData() {
return businessData;
}

public void settValue(Map<String, Map<String, String>> businessData) {
this.businessData = businessData;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.alibaba.chaosblade.exec.common.model.action.returnv.UnsupportedReturnTypeException;
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherModel;
import com.alibaba.chaosblade.exec.common.util.JsonUtil;
import com.alibaba.chaosblade.exec.common.util.ModelUtil;
import com.alibaba.chaosblade.exec.common.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -163,9 +164,13 @@ private static boolean compare(Model model, EnhancerModel enhancerModel) {
continue;
}
}

return false;
}
// business param match
if (keyName.equals(ModelConstant.BUSINESS_PARAMS)) {
Map<String, Map<String, String>> expMap = (Map<String, Map<String, String>>) value;
value = expMap.get(ModelUtil.getIdentifier(model));
}
// custom match
if (keyName.endsWith(ModelConstant.REGEX_PATTERN_FLAG) ? customMatcher.regexMatch(String.valueOf(entry.getValue()), value) : customMatcher.match(String.valueOf(entry.getValue()), value)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.alibaba.chaosblade.exec.common.model.matcher;

import com.alibaba.chaosblade.exec.common.aop.PredicateResult;
import com.alibaba.chaosblade.exec.common.constant.ModelConstant;
import com.alibaba.chaosblade.exec.common.util.BusinessParamUtil;

import java.util.List;

/**
* @author wufunc@gmail.com
*/
public class BusinessParamsMatcherSpec extends BasePredicateMatcherSpec {
@Override
public String getName() {
return ModelConstant.BUSINESS_PARAMS;
}

@Override
public String getDesc() {
return "business parmas";
}

@Override
public boolean noArgs() {
return false;
}

@Override
public boolean required() {
return false;
}

@Override
public PredicateResult predicate(MatcherModel matcherModel) {
String bParam = matcherModel.get(ModelConstant.BUSINESS_PARAMS);
List<BusinessParamUtil.BusinessParam> params = BusinessParamUtil.parseFromJsonStr(bParam);
if (params == null || params.isEmpty()) {
return PredicateResult.fail(getName() + " illegal json");
}
return PredicateResult.success();
}
}
Loading