Skip to content

Commit

Permalink
fix:限流规则兼容逻辑的匹配问题 (#207)
Browse files Browse the repository at this point in the history
* feat: 修改版本说明

* feat: 修改版本号为1.7.3

* feat: 限流规则支持多个同时生效

* feat:单测调整小配额,减少单CPU错误概率

* feat: 修改并去掉CL5的License

* fix: 修复base64的在oraclejdk下用例跑失败的问题

* 修改端口号,防止用例端口冲突

* fix: 修复端口号变更导致的用例失败问题

* feat: 解决prometheus同一个进程2个SDKContext无法生效的问题

* fix: 修复兼容逻辑的匹配问题,应该是前缀匹配,而不是全匹配

* feat: 方法的比较增加allmatch的剔除,allmatch不属于方法级限流规则

* feat:将入参MatchArguments改成arguments

* fix: 修复buildmethod传参错误的问题
  • Loading branch information
andrewshan authored Sep 9, 2022
1 parent f048d7c commit 3b2bba9
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import com.tencent.polaris.factory.ConfigAPIFactory;
import com.tencent.polaris.factory.api.DiscoveryAPIFactory;
import com.tencent.polaris.ratelimit.api.core.LimitAPI;
import com.tencent.polaris.ratelimit.api.rpc.MatchArgument;
import com.tencent.polaris.ratelimit.api.rpc.Argument;
import com.tencent.polaris.ratelimit.api.rpc.QuotaRequest;
import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse;
import com.tencent.polaris.ratelimit.api.rpc.QuotaResultCode;
Expand Down Expand Up @@ -224,9 +224,9 @@ public void handle(HttpExchange exchange) throws IOException {
quotaRequest.setNamespace(NAMESPACE_DEFAULT);
quotaRequest.setService(ECHO_SERVICE_NAME);
quotaRequest.setMethod("/echo");
Set<MatchArgument> matchArgumentSet = new HashSet<>();
Set<Argument> matchArgumentSet = new HashSet<>();
for (Map.Entry<String, String> entry : parameters.entrySet()) {
MatchArgument matchArgument = MatchArgument.buildQuery(entry.getKey(), entry.getValue());
Argument matchArgument = Argument.buildQuery(entry.getKey(), entry.getValue());
matchArgumentSet.add(matchArgument);
}
quotaRequest.setArguments(matchArgumentSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.Map;
import java.util.Objects;

public class MatchArgument {
public class Argument {

public enum ArgumentType {
CUSTOM, METHOD, HEADER, QUERY, CALLER_SERVICE, CALLER_IP
Expand All @@ -33,7 +33,7 @@ public enum ArgumentType {

private final String value;

private MatchArgument(ArgumentType type, String key, String value) {
private Argument(ArgumentType type, String key, String value) {
this.type = type;
this.key = key;
this.value = value;
Expand All @@ -51,37 +51,37 @@ public String getValue() {
return value;
}

public static MatchArgument buildCustom(String key, String value) {
return new MatchArgument(ArgumentType.CUSTOM, StringUtils.defaultString(key), StringUtils.defaultString(value));
public static Argument buildCustom(String key, String value) {
return new Argument(ArgumentType.CUSTOM, StringUtils.defaultString(key), StringUtils.defaultString(value));
}

public static MatchArgument buildMethod(String method) {
return new MatchArgument(ArgumentType.METHOD, "", StringUtils.defaultString(method));
public static Argument buildMethod(String method) {
return new Argument(ArgumentType.METHOD, "", StringUtils.defaultString(method));
}

public static MatchArgument buildHeader(String headerKey, String headerValue) {
return new MatchArgument(ArgumentType.HEADER, StringUtils.defaultString(headerKey),
public static Argument buildHeader(String headerKey, String headerValue) {
return new Argument(ArgumentType.HEADER, StringUtils.defaultString(headerKey),
StringUtils.defaultString(headerValue));
}

public static MatchArgument buildQuery(String queryKey, String queryValue) {
return new MatchArgument(ArgumentType.QUERY, StringUtils.defaultString(queryKey),
public static Argument buildQuery(String queryKey, String queryValue) {
return new Argument(ArgumentType.QUERY, StringUtils.defaultString(queryKey),
StringUtils.defaultString(queryValue));
}

public static MatchArgument buildCallerService(String namespace, String service) {
return new MatchArgument(ArgumentType.CALLER_SERVICE, StringUtils.defaultString(namespace),
public static Argument buildCallerService(String namespace, String service) {
return new Argument(ArgumentType.CALLER_SERVICE, StringUtils.defaultString(namespace),
StringUtils.defaultString(service));
}

public static MatchArgument buildCallerIP(String callerIP) {
return new MatchArgument(ArgumentType.CALLER_IP, "", StringUtils.defaultString(callerIP));
public static Argument buildCallerIP(String callerIP) {
return new Argument(ArgumentType.CALLER_IP, "", StringUtils.defaultString(callerIP));
}

public static MatchArgument fromLabel(String labelKey, String labelValue) {
public static Argument fromLabel(String labelKey, String labelValue) {
labelKey = StringUtils.defaultString(labelKey);
if (StringUtils.equals(labelKey, RateLimitConsts.LABEL_KEY_METHOD)) {
return buildMethod(labelKey);
return buildMethod(labelValue);
}
if (StringUtils.equals(labelKey, RateLimitConsts.LABEL_KEY_CALLER_IP)) {
return buildCallerIP(labelValue);
Expand Down Expand Up @@ -135,10 +135,10 @@ public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof MatchArgument)) {
if (!(o instanceof Argument)) {
return false;
}
MatchArgument that = (MatchArgument) o;
Argument that = (Argument) o;
return type == that.type &&
Objects.equals(key, that.key) &&
Objects.equals(value, that.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class QuotaRequest extends RequestBaseEntity {

private String method;

private Set<MatchArgument> arguments = new HashSet<>();
private Set<Argument> arguments = new HashSet<>();

private int count = 1;

Expand Down Expand Up @@ -62,7 +62,7 @@ public void setService(String service) {
@Deprecated
public Map<String, String> getLabels() {
Map<String, String> values = new HashMap<>();
for (MatchArgument matchArgument : arguments) {
for (Argument matchArgument : arguments) {
matchArgument.toLabel(values);
}
return values;
Expand All @@ -77,7 +77,7 @@ public void setLabels(Map<String, String> labels) {
return;
}
for (Map.Entry<String, String> entry : labels.entrySet()) {
arguments.add(MatchArgument.fromLabel(entry.getKey(), entry.getValue()));
arguments.add(Argument.fromLabel(entry.getKey(), entry.getValue()));
}
}

Expand All @@ -97,11 +97,11 @@ public void setMethod(String method) {
this.method = method;
}

public Set<MatchArgument> getArguments() {
public Set<Argument> getArguments() {
return arguments;
}

public void setArguments(Set<MatchArgument> arguments) {
public void setArguments(Set<Argument> arguments) {
if (CollectionUtils.isEmpty(arguments)) {
this.arguments = Collections.emptySet();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.tencent.polaris.api.pojo.RegistryCacheValue;
import com.tencent.polaris.api.pojo.ServiceEventKey.EventType;
import com.tencent.polaris.api.utils.CollectionUtils;
import com.tencent.polaris.api.utils.RuleUtils;
import com.tencent.polaris.api.utils.StringUtils;
import com.tencent.polaris.client.pb.ModelProto.MatchString;
import com.tencent.polaris.client.pb.RateLimitProto.MatchArgument;
Expand Down Expand Up @@ -78,7 +79,7 @@ private static int getRuleLevel(Rule rule) {
if (CollectionUtils.isNotEmpty(argumentsList)) {
return RULE_ARGUMENT_LEVEL + argumentsList.size();
}
if (null != method) {
if (null != method && !RuleUtils.isMatchAllValue(method)) {
return RULE_METHOD_LEVEL;
}
return RULE_SERVICE_LEVEL;
Expand Down Expand Up @@ -112,23 +113,23 @@ private List<Rule> unifiedRules(List<Rule> rules) {
// transfer the labels to arguments
List<MatchArgument> arguments = new ArrayList<>();
for (Map.Entry<String, MatchString> entry : rule.getLabelsMap().entrySet()) {
String labelKey = entry.getKey();
String labelKey = StringUtils.defaultString(entry.getKey());
if (StringUtils.equals(labelKey, RateLimitConsts.LABEL_KEY_METHOD)) {
arguments.add(MatchArgument.newBuilder().setType(Type.METHOD).setValue(entry.getValue()).build());
} else if (StringUtils.equals(labelKey, RateLimitConsts.LABEL_KEY_CALLER_IP)) {
arguments
.add(MatchArgument.newBuilder().setType(Type.CALLER_IP).setValue(entry.getValue()).build());
} else if (StringUtils.equals(labelKey, RateLimitConsts.LABEL_KEY_HEADER)) {
} else if (labelKey.startsWith(RateLimitConsts.LABEL_KEY_HEADER)) {
arguments
.add(MatchArgument.newBuilder().setType(Type.HEADER)
.setKey(labelKey.substring(RateLimitConsts.LABEL_KEY_HEADER.length()))
.setValue(entry.getValue()).build());
} else if (StringUtils.equals(labelKey, RateLimitConsts.LABEL_KEY_QUERY)) {
} else if (labelKey.startsWith(RateLimitConsts.LABEL_KEY_QUERY)) {
arguments
.add(MatchArgument.newBuilder().setType(Type.QUERY)
.setKey(labelKey.substring(RateLimitConsts.LABEL_KEY_QUERY.length()))
.setValue(entry.getValue()).build());
} else if (StringUtils.equals(labelKey, RateLimitConsts.LABEL_KEY_CALLER_SERVICE)) {
} else if (labelKey.startsWith(RateLimitConsts.LABEL_KEY_CALLER_SERVICE)) {
arguments
.add(MatchArgument.newBuilder().setType(Type.CALLER_SERVICE)
.setKey(labelKey.substring(RateLimitConsts.LABEL_KEY_CALLER_SERVICE.length()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import com.tencent.polaris.client.pb.ModelProto.MatchString;
import com.tencent.polaris.client.pb.ModelProto.MatchString.MatchStringType;
import com.tencent.polaris.client.pb.RateLimitProto;
import com.tencent.polaris.client.pb.RateLimitProto.MatchArgument;
import com.tencent.polaris.client.pb.RateLimitProto.RateLimit;
import com.tencent.polaris.client.pb.RateLimitProto.Rule;
import com.tencent.polaris.logging.LoggerFactory;
Expand Down Expand Up @@ -167,10 +166,10 @@ private static String formatLabelsToStr(CommonQuotaRequest request, InitCriteria
}
}
}
List<MatchArgument> argumentsList = rule.getArgumentsList();
List<RateLimitProto.MatchArgument> argumentsList = rule.getArgumentsList();
List<String> tmpList = new ArrayList<>();
Map<Integer, Map<String, String>> arguments = request.getArguments();
for (MatchArgument matchArgument : argumentsList) {
for (RateLimitProto.MatchArgument matchArgument : argumentsList) {
String labelValue;
MatchString matcher = matchArgument.getValue();
if (regexCombine && matcher.getType() != MatchStringType.EXACT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.tencent.polaris.client.flow.BaseFlow;
import com.tencent.polaris.client.flow.DefaultFlowControlParam;
import com.tencent.polaris.client.flow.FlowControlParam;
import com.tencent.polaris.ratelimit.api.rpc.MatchArgument;
import com.tencent.polaris.ratelimit.api.rpc.Argument;
import com.tencent.polaris.ratelimit.api.rpc.QuotaRequest;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -59,12 +59,12 @@ public CommonQuotaRequest(QuotaRequest quotaRequest, Configuration configuration
BaseFlow.buildFlowControlParam(quotaRequest, configuration, flowControlParam);
}

private Map<Integer, Map<String, String>> parseArguments(Collection<MatchArgument> arguments) {
private Map<Integer, Map<String, String>> parseArguments(Collection<Argument> arguments) {
Map<Integer, Map<String, String>> argumentMap = new HashMap<>();
if (CollectionUtils.isEmpty(arguments)) {
return argumentMap;
}
for (MatchArgument argument : arguments) {
for (Argument argument : arguments) {
Map<String, String> stringMatchArgumentMap = argumentMap
.computeIfAbsent(argument.getType().ordinal(), k -> new HashMap<>());
stringMatchArgumentMap.put(argument.getKey(), argument.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.tencent.polaris.client.pb.RateLimitProto.Rule.Type;
import com.tencent.polaris.client.util.Utils;
import com.tencent.polaris.ratelimit.api.core.LimitAPI;
import com.tencent.polaris.ratelimit.api.rpc.Argument;
import com.tencent.polaris.ratelimit.api.rpc.QuotaRequest;
import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse;
import com.tencent.polaris.ratelimit.api.rpc.QuotaResultCode;
Expand Down Expand Up @@ -110,9 +111,9 @@ private static void testQuotaAcquire(LimitAPI limitAPI, Map<String, String> labe
QuotaRequest payRequest = new QuotaRequest();
payRequest.setNamespace(Consts.NAMESPACE_TEST);
payRequest.setService(Consts.LOCAL_LIMIT_SERVICE);
Set<com.tencent.polaris.ratelimit.api.rpc.MatchArgument> matchArgumentSet = new HashSet<>();
Set<Argument> matchArgumentSet = new HashSet<>();
for (Map.Entry<String, String> entry : labels.entrySet()) {
com.tencent.polaris.ratelimit.api.rpc.MatchArgument matchArgument = com.tencent.polaris.ratelimit.api.rpc.MatchArgument
Argument matchArgument = Argument
.buildCustom(entry.getKey(), entry.getValue());
matchArgumentSet.add(matchArgument);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.tencent.polaris.client.pb.RateLimitProto.Rule.Type;
import com.tencent.polaris.client.util.Utils;
import com.tencent.polaris.ratelimit.api.core.LimitAPI;
import com.tencent.polaris.ratelimit.api.rpc.Argument;
import com.tencent.polaris.ratelimit.api.rpc.QuotaRequest;
import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse;
import com.tencent.polaris.ratelimit.api.rpc.QuotaResultCode;
Expand Down Expand Up @@ -135,8 +136,8 @@ private QuotaResponse quotaAcquire(LimitAPI limitAPI, String method, String head
payRequest.setMethod(method);
}
if (null != headerValue) {
Set<com.tencent.polaris.ratelimit.api.rpc.MatchArgument> matchArguments = new HashSet<>();
matchArguments.add(com.tencent.polaris.ratelimit.api.rpc.MatchArgument
Set<Argument> matchArguments = new HashSet<>();
matchArguments.add(Argument
.buildHeader(Consts.HEADER_KEY, headerValue));
payRequest.setArguments(matchArguments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.tencent.polaris.client.pb.RateLimitProto.Rule.Type;
import com.tencent.polaris.client.util.Utils;
import com.tencent.polaris.ratelimit.api.core.LimitAPI;
import com.tencent.polaris.ratelimit.api.rpc.Argument;
import com.tencent.polaris.ratelimit.api.rpc.QuotaRequest;
import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse;
import com.tencent.polaris.ratelimit.api.rpc.QuotaResultCode;
Expand Down Expand Up @@ -160,8 +161,8 @@ private QuotaResponse quotaAcquire(LimitAPI limitAPI, String service, String met
payRequest.setMethod(method);
}
if (null != headerValue) {
Set<com.tencent.polaris.ratelimit.api.rpc.MatchArgument> matchArguments = new HashSet<>();
matchArguments.add(com.tencent.polaris.ratelimit.api.rpc.MatchArgument
Set<Argument> matchArguments = new HashSet<>();
matchArguments.add(Argument
.buildHeader(Consts.HEADER_KEY, headerValue));
payRequest.setArguments(matchArguments);
}
Expand Down

0 comments on commit 3b2bba9

Please sign in to comment.