Extract resource ID constants and methods to ResourceIdUtils from ResourceRateLimitConfig
This commit is contained in:
@@ -33,6 +33,7 @@ import we.stats.ratelimit.ResourceRateLimitConfigService;
|
|||||||
import we.util.Consts;
|
import we.util.Consts;
|
||||||
import we.util.DateTimeUtils;
|
import we.util.DateTimeUtils;
|
||||||
import we.util.NetworkUtils;
|
import we.util.NetworkUtils;
|
||||||
|
import we.util.ResourceIdUtils;
|
||||||
import we.util.ThreadContext;
|
import we.util.ThreadContext;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
@@ -113,22 +114,22 @@ public class FlowStatSchedConfig extends SchedConfig {
|
|||||||
resourceTimeWindowStats.forEach(
|
resourceTimeWindowStats.forEach(
|
||||||
rtws -> {
|
rtws -> {
|
||||||
String resource = rtws.getResourceId();
|
String resource = rtws.getResourceId();
|
||||||
String app = null, pi = null, node = ResourceRateLimitConfig.NODE, service = null, path = null;
|
String app = null, pi = null, node = ResourceIdUtils.NODE, service = null, path = null;
|
||||||
int type = ResourceRateLimitConfig.Type.NODE, id = 0;
|
int type = ResourceRateLimitConfig.Type.NODE, id = 0;
|
||||||
ResourceRateLimitConfig c = resourceRateLimitConfigService.getResourceRateLimitConfig(resource);
|
ResourceRateLimitConfig c = resourceRateLimitConfigService.getResourceRateLimitConfig(resource);
|
||||||
|
|
||||||
if (c == null) { // _global, service, app, app+service, ip, ip+service
|
if (c == null) { // _global, service, app, app+service, ip, ip+service
|
||||||
node = ResourceRateLimitConfig.getNode(resource);
|
node = ResourceIdUtils.getNode(resource);
|
||||||
if (node != null && node.equals(ResourceRateLimitConfig.NODE)) {
|
if (node != null && node.equals(ResourceIdUtils.NODE)) {
|
||||||
} else {
|
} else {
|
||||||
service = ResourceRateLimitConfig.getService(resource);
|
service = ResourceIdUtils.getService(resource);
|
||||||
app = ResourceRateLimitConfig.getApp(resource);
|
app = ResourceIdUtils.getApp(resource);
|
||||||
pi = ResourceRateLimitConfig.getIp(resource);
|
pi = ResourceIdUtils.getIp(resource);
|
||||||
if (service == null) {
|
if (service == null) {
|
||||||
if (app == null) {
|
if (app == null) {
|
||||||
type = ResourceRateLimitConfig.Type.IP;
|
type = ResourceRateLimitConfig.Type.IP;
|
||||||
} else {
|
} else {
|
||||||
ResourceRateLimitConfig appConfig = resourceRateLimitConfigService.getResourceRateLimitConfig(ResourceRateLimitConfig.APP_DEFAULT_RESOURCE);
|
ResourceRateLimitConfig appConfig = resourceRateLimitConfigService.getResourceRateLimitConfig(ResourceIdUtils.APP_DEFAULT_RESOURCE);
|
||||||
if (appConfig != null && appConfig.isEnable()) {
|
if (appConfig != null && appConfig.isEnable()) {
|
||||||
type = ResourceRateLimitConfig.Type.APP_DEFAULT;
|
type = ResourceRateLimitConfig.Type.APP_DEFAULT;
|
||||||
} else {
|
} else {
|
||||||
@@ -183,7 +184,7 @@ public class FlowStatSchedConfig extends SchedConfig {
|
|||||||
|
|
||||||
String r = null;
|
String r = null;
|
||||||
if (type == ResourceRateLimitConfig.Type.NODE) {
|
if (type == ResourceRateLimitConfig.Type.NODE) {
|
||||||
r = ResourceRateLimitConfig.NODE;
|
r = ResourceIdUtils.NODE;
|
||||||
} else if (type == ResourceRateLimitConfig.Type.SERVICE_DEFAULT || type == ResourceRateLimitConfig.Type.SERVICE) {
|
} else if (type == ResourceRateLimitConfig.Type.SERVICE_DEFAULT || type == ResourceRateLimitConfig.Type.SERVICE) {
|
||||||
r = service;
|
r = service;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ import we.stats.ratelimit.ResourceRateLimitConfig;
|
|||||||
import we.util.Consts;
|
import we.util.Consts;
|
||||||
import we.util.DateTimeUtils;
|
import we.util.DateTimeUtils;
|
||||||
import we.util.JacksonUtils;
|
import we.util.JacksonUtils;
|
||||||
|
import we.util.ResourceIdUtils;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -72,11 +73,11 @@ public class FlowControlController {
|
|||||||
long currentTimeSlot = flowStat.currentTimeSlotId();
|
long currentTimeSlot = flowStat.currentTimeSlotId();
|
||||||
long startTimeSlot = currentTimeSlot - recent * 1000;
|
long startTimeSlot = currentTimeSlot - recent * 1000;
|
||||||
TimeWindowStat timeWindowStat = null;
|
TimeWindowStat timeWindowStat = null;
|
||||||
List<ResourceTimeWindowStat> wins = flowStat.getResourceTimeWindowStats(ResourceRateLimitConfig.NODE_RESOURCE, startTimeSlot, currentTimeSlot, recent);
|
List<ResourceTimeWindowStat> wins = flowStat.getResourceTimeWindowStats(ResourceIdUtils.NODE_RESOURCE, startTimeSlot, currentTimeSlot, recent);
|
||||||
if (wins == null || wins.isEmpty()) {
|
if (wins == null || wins.isEmpty()) {
|
||||||
result.put("rps", 0);
|
result.put("rps", 0);
|
||||||
} else {
|
} else {
|
||||||
concurrents = flowStat.getConcurrentRequests(ResourceRateLimitConfig.NODE_RESOURCE);
|
concurrents = flowStat.getConcurrentRequests(ResourceIdUtils.NODE_RESOURCE);
|
||||||
result.put("concurrents", concurrents);
|
result.put("concurrents", concurrents);
|
||||||
timeWindowStat = wins.get(0).getWindows().get(0);
|
timeWindowStat = wins.get(0).getWindows().get(0);
|
||||||
BigDecimal winrps = timeWindowStat.getRps();
|
BigDecimal winrps = timeWindowStat.getRps();
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ public class FlowControlFilter extends FizzWebFilter {
|
|||||||
log.info("{} exceed {} flow limit, blocked by maximum QPS", traceId, blockedResourceId, LogService.BIZ_ID, traceId);
|
log.info("{} exceed {} flow limit, blocked by maximum QPS", traceId, blockedResourceId, LogService.BIZ_ID, traceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
ResourceRateLimitConfig c = resourceRateLimitConfigService.getResourceRateLimitConfig(ResourceRateLimitConfig.NODE_RESOURCE);
|
ResourceRateLimitConfig c = resourceRateLimitConfigService.getResourceRateLimitConfig(ResourceIdUtils.NODE_RESOURCE);
|
||||||
String rt = c.responseType, rc = c.responseContent;
|
String rt = c.responseType, rc = c.responseContent;
|
||||||
c = resourceRateLimitConfigService.getResourceRateLimitConfig(blockedResourceId);
|
c = resourceRateLimitConfigService.getResourceRateLimitConfig(blockedResourceId);
|
||||||
if (c != null) {
|
if (c != null) {
|
||||||
@@ -192,15 +192,15 @@ public class FlowControlFilter extends FizzWebFilter {
|
|||||||
private List<ResourceConfig> getResourceConfigItselfAndParents(ResourceConfig rc, List<ResourceConfig> rcs) {
|
private List<ResourceConfig> getResourceConfigItselfAndParents(ResourceConfig rc, List<ResourceConfig> rcs) {
|
||||||
boolean check = false;
|
boolean check = false;
|
||||||
String rcId = rc.getResourceId();
|
String rcId = rc.getResourceId();
|
||||||
String rcApp = ResourceRateLimitConfig.getApp(rcId);
|
String rcApp = ResourceIdUtils.getApp(rcId);
|
||||||
String rcIp = ResourceRateLimitConfig.getIp(rcId);
|
String rcIp = ResourceIdUtils.getIp(rcId);
|
||||||
List<ResourceConfig> result = new ArrayList<>();
|
List<ResourceConfig> result = new ArrayList<>();
|
||||||
for (int i = rcs.size() - 1; i > -1; i--) {
|
for (int i = rcs.size() - 1; i > -1; i--) {
|
||||||
ResourceConfig r = rcs.get(i);
|
ResourceConfig r = rcs.get(i);
|
||||||
String id = r.getResourceId();
|
String id = r.getResourceId();
|
||||||
String app = ResourceRateLimitConfig.getApp(id);
|
String app = ResourceIdUtils.getApp(id);
|
||||||
String ip = ResourceRateLimitConfig.getIp(id);
|
String ip = ResourceIdUtils.getIp(id);
|
||||||
String path = ResourceRateLimitConfig.getPath(id);
|
String path = ResourceIdUtils.getPath(id);
|
||||||
if (check) {
|
if (check) {
|
||||||
if (rcIp != null) {
|
if (rcIp != null) {
|
||||||
if (ip != null) {
|
if (ip != null) {
|
||||||
@@ -239,12 +239,12 @@ public class FlowControlFilter extends FizzWebFilter {
|
|||||||
List<ResourceConfig> resourceConfigs = new ArrayList<>(9);
|
List<ResourceConfig> resourceConfigs = new ArrayList<>(9);
|
||||||
StringBuilder b = ThreadContext.getStringBuilder();
|
StringBuilder b = ThreadContext.getStringBuilder();
|
||||||
|
|
||||||
checkRateLimitConfigAndAddTo(resourceConfigs, b, null, null, ResourceRateLimitConfig.NODE, null, null, null);
|
checkRateLimitConfigAndAddTo(resourceConfigs, b, null, null, ResourceIdUtils.NODE, null, null, null);
|
||||||
checkRateLimitConfigAndAddTo(resourceConfigs, b, null, null, null, service, null, ResourceRateLimitConfig.SERVICE_DEFAULT);
|
checkRateLimitConfigAndAddTo(resourceConfigs, b, null, null, null, service, null, ResourceIdUtils.SERVICE_DEFAULT);
|
||||||
checkRateLimitConfigAndAddTo(resourceConfigs, b, null, null, null, service, path, null);
|
checkRateLimitConfigAndAddTo(resourceConfigs, b, null, null, null, service, path, null);
|
||||||
|
|
||||||
if (app != null) {
|
if (app != null) {
|
||||||
checkRateLimitConfigAndAddTo(resourceConfigs, b, app, null, null, null, null, ResourceRateLimitConfig.APP_DEFAULT);
|
checkRateLimitConfigAndAddTo(resourceConfigs, b, app, null, null, null, null, ResourceIdUtils.APP_DEFAULT);
|
||||||
checkRateLimitConfigAndAddTo(resourceConfigs, b, app, null, null, service, null, null);
|
checkRateLimitConfigAndAddTo(resourceConfigs, b, app, null, null, service, null, null);
|
||||||
checkRateLimitConfigAndAddTo(resourceConfigs, b, app, null, null, service, path, null);
|
checkRateLimitConfigAndAddTo(resourceConfigs, b, app, null, null, service, path, null);
|
||||||
}
|
}
|
||||||
@@ -262,7 +262,7 @@ public class FlowControlFilter extends FizzWebFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void checkRateLimitConfigAndAddTo(List<ResourceConfig> resourceConfigs, StringBuilder b, String app, String ip, String node, String service, String path, String defaultRateLimitConfigId) {
|
private void checkRateLimitConfigAndAddTo(List<ResourceConfig> resourceConfigs, StringBuilder b, String app, String ip, String node, String service, String path, String defaultRateLimitConfigId) {
|
||||||
ResourceRateLimitConfig.buildResourceIdTo(b, app, ip, node, service, path);
|
ResourceIdUtils.buildResourceIdTo(b, app, ip, node, service, path);
|
||||||
String resourceId = b.toString();
|
String resourceId = b.toString();
|
||||||
checkRateLimitConfigAndAddTo(resourceConfigs, resourceId, defaultRateLimitConfigId);
|
checkRateLimitConfigAndAddTo(resourceConfigs, resourceId, defaultRateLimitConfigId);
|
||||||
b.delete(0, b.length());
|
b.delete(0, b.length());
|
||||||
@@ -276,21 +276,21 @@ public class FlowControlFilter extends FizzWebFilter {
|
|||||||
rc = new ResourceConfig(resource, rateLimitConfig.concurrents, rateLimitConfig.qps);
|
rc = new ResourceConfig(resource, rateLimitConfig.concurrents, rateLimitConfig.qps);
|
||||||
resourceConfigs.add(rc);
|
resourceConfigs.add(rc);
|
||||||
} else {
|
} else {
|
||||||
String node = ResourceRateLimitConfig.getNode(resource);
|
String node = ResourceIdUtils.getNode(resource);
|
||||||
if (node != null && node.equals(ResourceRateLimitConfig.NODE)) {
|
if (node != null && node.equals(ResourceIdUtils.NODE)) {
|
||||||
rc = new ResourceConfig(resource, 0, 0);
|
rc = new ResourceConfig(resource, 0, 0);
|
||||||
}
|
}
|
||||||
if (defaultRateLimitConfigId != null) {
|
if (defaultRateLimitConfigId != null) {
|
||||||
if (defaultRateLimitConfigId.equals(ResourceRateLimitConfig.SERVICE_DEFAULT)) {
|
if (defaultRateLimitConfigId.equals(ResourceIdUtils.SERVICE_DEFAULT)) {
|
||||||
rc = new ResourceConfig(resource, 0, 0);
|
rc = new ResourceConfig(resource, 0, 0);
|
||||||
rateLimitConfig = resourceRateLimitConfigService.getResourceRateLimitConfig(ResourceRateLimitConfig.SERVICE_DEFAULT_RESOURCE);
|
rateLimitConfig = resourceRateLimitConfigService.getResourceRateLimitConfig(ResourceIdUtils.SERVICE_DEFAULT_RESOURCE);
|
||||||
if (rateLimitConfig != null && rateLimitConfig.isEnable()) {
|
if (rateLimitConfig != null && rateLimitConfig.isEnable()) {
|
||||||
rc.setMaxCon(rateLimitConfig.concurrents);
|
rc.setMaxCon(rateLimitConfig.concurrents);
|
||||||
rc.setMaxQPS(rateLimitConfig.qps);
|
rc.setMaxQPS(rateLimitConfig.qps);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (defaultRateLimitConfigId.equals(ResourceRateLimitConfig.APP_DEFAULT)) {
|
if (defaultRateLimitConfigId.equals(ResourceIdUtils.APP_DEFAULT)) {
|
||||||
rateLimitConfig = resourceRateLimitConfigService.getResourceRateLimitConfig(ResourceRateLimitConfig.APP_DEFAULT_RESOURCE);
|
rateLimitConfig = resourceRateLimitConfigService.getResourceRateLimitConfig(ResourceIdUtils.APP_DEFAULT_RESOURCE);
|
||||||
if (rateLimitConfig != null && rateLimitConfig.isEnable()) {
|
if (rateLimitConfig != null && rateLimitConfig.isEnable()) {
|
||||||
rc = new ResourceConfig(resource, rateLimitConfig.concurrents, rateLimitConfig.qps);
|
rc = new ResourceConfig(resource, rateLimitConfig.concurrents, rateLimitConfig.qps);
|
||||||
}
|
}
|
||||||
@@ -310,7 +310,7 @@ public class FlowControlFilter extends FizzWebFilter {
|
|||||||
prevPrev = resourceConfigs.get(sz - 2).getResourceId();
|
prevPrev = resourceConfigs.get(sz - 2).getResourceId();
|
||||||
|
|
||||||
if (rateLimitConfig.type == ResourceRateLimitConfig.Type.APP) {
|
if (rateLimitConfig.type == ResourceRateLimitConfig.Type.APP) {
|
||||||
String app = ResourceRateLimitConfig.getApp(prev);
|
String app = ResourceIdUtils.getApp(prev);
|
||||||
if (rateLimitConfig.path == null) {
|
if (rateLimitConfig.path == null) {
|
||||||
if (rateLimitConfig.service != null && app == null) {
|
if (rateLimitConfig.service != null && app == null) {
|
||||||
something4(resourceConfigs, rateLimitConfig.app, null, null);
|
something4(resourceConfigs, rateLimitConfig.app, null, null);
|
||||||
@@ -320,11 +320,11 @@ public class FlowControlFilter extends FizzWebFilter {
|
|||||||
something4(resourceConfigs, rateLimitConfig.app, null, null);
|
something4(resourceConfigs, rateLimitConfig.app, null, null);
|
||||||
something4(resourceConfigs, rateLimitConfig.app, null, rateLimitConfig.service);
|
something4(resourceConfigs, rateLimitConfig.app, null, rateLimitConfig.service);
|
||||||
} else {
|
} else {
|
||||||
String service = ResourceRateLimitConfig.getService(prev);
|
String service = ResourceIdUtils.getService(prev);
|
||||||
if (service == null) {
|
if (service == null) {
|
||||||
something4(resourceConfigs, rateLimitConfig.app, null, rateLimitConfig.service);
|
something4(resourceConfigs, rateLimitConfig.app, null, rateLimitConfig.service);
|
||||||
} else {
|
} else {
|
||||||
app = ResourceRateLimitConfig.getApp(prevPrev);
|
app = ResourceIdUtils.getApp(prevPrev);
|
||||||
if (app == null) {
|
if (app == null) {
|
||||||
something4(resourceConfigs, rateLimitConfig.app, null, null);
|
something4(resourceConfigs, rateLimitConfig.app, null, null);
|
||||||
}
|
}
|
||||||
@@ -336,21 +336,21 @@ public class FlowControlFilter extends FizzWebFilter {
|
|||||||
|
|
||||||
if (rateLimitConfig.service == null && rateLimitConfig.path == null) {
|
if (rateLimitConfig.service == null && rateLimitConfig.path == null) {
|
||||||
} else if (rateLimitConfig.path == null) {
|
} else if (rateLimitConfig.path == null) {
|
||||||
String ip = ResourceRateLimitConfig.getIp(prev);
|
String ip = ResourceIdUtils.getIp(prev);
|
||||||
if (ip == null) {
|
if (ip == null) {
|
||||||
something4(resourceConfigs, null, rateLimitConfig.ip, null);
|
something4(resourceConfigs, null, rateLimitConfig.ip, null);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
String ip = ResourceRateLimitConfig.getIp(prev);
|
String ip = ResourceIdUtils.getIp(prev);
|
||||||
if (ip == null) {
|
if (ip == null) {
|
||||||
something4(resourceConfigs, null, rateLimitConfig.ip, null);
|
something4(resourceConfigs, null, rateLimitConfig.ip, null);
|
||||||
something4(resourceConfigs, null, rateLimitConfig.ip, rateLimitConfig.service);
|
something4(resourceConfigs, null, rateLimitConfig.ip, rateLimitConfig.service);
|
||||||
} else {
|
} else {
|
||||||
String service = ResourceRateLimitConfig.getService(prev);
|
String service = ResourceIdUtils.getService(prev);
|
||||||
if (service == null) {
|
if (service == null) {
|
||||||
something4(resourceConfigs, null, rateLimitConfig.ip, rateLimitConfig.service);
|
something4(resourceConfigs, null, rateLimitConfig.ip, rateLimitConfig.service);
|
||||||
} else {
|
} else {
|
||||||
ip = ResourceRateLimitConfig.getIp(prevPrev);
|
ip = ResourceIdUtils.getIp(prevPrev);
|
||||||
if (ip == null) {
|
if (ip == null) {
|
||||||
something4(resourceConfigs, null, rateLimitConfig.ip, null);
|
something4(resourceConfigs, null, rateLimitConfig.ip, null);
|
||||||
}
|
}
|
||||||
@@ -362,7 +362,7 @@ public class FlowControlFilter extends FizzWebFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void something4(List<ResourceConfig> resourceConfigs, String app, String ip, String service) {
|
private void something4(List<ResourceConfig> resourceConfigs, String app, String ip, String service) {
|
||||||
String r = ResourceRateLimitConfig.buildResourceId(app, ip, null, service, null);
|
String r = ResourceIdUtils.buildResourceId(app, ip, null, service, null);
|
||||||
ResourceConfig rc = new ResourceConfig(r, 0, 0);
|
ResourceConfig rc = new ResourceConfig(r, 0, 0);
|
||||||
resourceConfigs.add(rc);
|
resourceConfigs.add(rc);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,9 +19,11 @@ package we.stats.ratelimit;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import we.util.Consts;
|
|
||||||
import we.util.JacksonUtils;
|
import we.util.JacksonUtils;
|
||||||
import we.util.Utils;
|
|
||||||
|
import static we.util.ResourceIdUtils.APP_DEFAULT;
|
||||||
|
import static we.util.ResourceIdUtils.NODE;
|
||||||
|
import static we.util.ResourceIdUtils.SERVICE_DEFAULT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author hongqiaowei
|
* @author hongqiaowei
|
||||||
@@ -39,18 +41,6 @@ public class ResourceRateLimitConfig {
|
|||||||
static final byte IP = 7;
|
static final byte IP = 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final String NODE = "_global";
|
|
||||||
|
|
||||||
public static final String NODE_RESOURCE = buildResourceId(null, null, NODE, null, null);
|
|
||||||
|
|
||||||
public static final String SERVICE_DEFAULT = "service_default";
|
|
||||||
|
|
||||||
public static final String SERVICE_DEFAULT_RESOURCE = buildResourceId(null, null, null, SERVICE_DEFAULT, null);
|
|
||||||
|
|
||||||
public static final String APP_DEFAULT = "app_default";
|
|
||||||
|
|
||||||
public static final String APP_DEFAULT_RESOURCE = buildResourceId(APP_DEFAULT, null, null, null, null);
|
|
||||||
|
|
||||||
public boolean isDeleted = false;
|
public boolean isDeleted = false;
|
||||||
|
|
||||||
public int id;
|
public int id;
|
||||||
@@ -145,62 +135,6 @@ public class ResourceRateLimitConfig {
|
|||||||
return resourceId;
|
return resourceId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String buildResourceId(String app, String ip, String node, String service, String path) {
|
|
||||||
StringBuilder b = new StringBuilder(32);
|
|
||||||
buildResourceIdTo(b, app, ip, node, service, path);
|
|
||||||
return b.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void buildResourceIdTo(StringBuilder b, String app, String ip, String node, String service, String path) {
|
|
||||||
b.append(app == null ? Consts.S.EMPTY : app) .append(Consts.S.SQUARE);
|
|
||||||
b.append(ip == null ? Consts.S.EMPTY : ip) .append(Consts.S.SQUARE);
|
|
||||||
b.append(node == null ? Consts.S.EMPTY : node) .append(Consts.S.SQUARE);
|
|
||||||
b.append(service == null ? Consts.S.EMPTY : service) .append(Consts.S.SQUARE);
|
|
||||||
b.append(path == null ? Consts.S.EMPTY : path);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getApp(String resource) {
|
|
||||||
int i = resource.indexOf(Consts.S.SQUARE);
|
|
||||||
if (i == 0) {
|
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
return resource.substring(0, i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getIp(String resource) {
|
|
||||||
String extract = Utils.extract(resource, Consts.S.SQUARE, 1);
|
|
||||||
if (extract.equals(Consts.S.EMPTY)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return extract;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getNode(String resource) {
|
|
||||||
String extract = Utils.extract(resource, Consts.S.SQUARE, 2);
|
|
||||||
if (extract.equals(Consts.S.EMPTY)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return extract;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getService(String resource) {
|
|
||||||
String extract = Utils.extract(resource, Consts.S.SQUARE, 3);
|
|
||||||
if (extract.equals(Consts.S.EMPTY)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return extract;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getPath(String resource) {
|
|
||||||
int i = resource.lastIndexOf(Consts.S.SQUARE);
|
|
||||||
if (i == resource.length() - 1) {
|
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
return resource.substring(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return JacksonUtils.writeValueAsString(this);
|
return JacksonUtils.writeValueAsString(this);
|
||||||
|
|||||||
94
fizz-core/src/main/java/we/util/ResourceIdUtils.java
Normal file
94
fizz-core/src/main/java/we/util/ResourceIdUtils.java
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2021 the original author or authors.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package we.util;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resource ID utility
|
||||||
|
* Extracted from {@link we.stats.ratelimit.ResourceRateLimitConfig}
|
||||||
|
*
|
||||||
|
* @author zhongjie
|
||||||
|
*/
|
||||||
|
public class ResourceIdUtils {
|
||||||
|
|
||||||
|
public static final String NODE = "_global";
|
||||||
|
|
||||||
|
public static final String NODE_RESOURCE = buildResourceId(null, null, NODE, null, null);
|
||||||
|
|
||||||
|
public static final String SERVICE_DEFAULT = "service_default";
|
||||||
|
|
||||||
|
public static final String SERVICE_DEFAULT_RESOURCE = buildResourceId(null, null, null, SERVICE_DEFAULT, null);
|
||||||
|
|
||||||
|
public static final String APP_DEFAULT = "app_default";
|
||||||
|
|
||||||
|
public static final String APP_DEFAULT_RESOURCE = buildResourceId(APP_DEFAULT, null, null, null, null);
|
||||||
|
|
||||||
|
public static String buildResourceId(String app, String ip, String node, String service, String path) {
|
||||||
|
StringBuilder b = new StringBuilder(32);
|
||||||
|
buildResourceIdTo(b, app, ip, node, service, path);
|
||||||
|
return b.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void buildResourceIdTo(StringBuilder b, String app, String ip, String node, String service, String path) {
|
||||||
|
b.append(app == null ? Consts.S.EMPTY : app) .append(Consts.S.SQUARE);
|
||||||
|
b.append(ip == null ? Consts.S.EMPTY : ip) .append(Consts.S.SQUARE);
|
||||||
|
b.append(node == null ? Consts.S.EMPTY : node) .append(Consts.S.SQUARE);
|
||||||
|
b.append(service == null ? Consts.S.EMPTY : service) .append(Consts.S.SQUARE);
|
||||||
|
b.append(path == null ? Consts.S.EMPTY : path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getApp(String resource) {
|
||||||
|
int i = resource.indexOf(Consts.S.SQUARE);
|
||||||
|
if (i == 0) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return resource.substring(0, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getIp(String resource) {
|
||||||
|
String extract = Utils.extract(resource, Consts.S.SQUARE, 1);
|
||||||
|
if (extract.equals(Consts.S.EMPTY)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return extract;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getNode(String resource) {
|
||||||
|
String extract = Utils.extract(resource, Consts.S.SQUARE, 2);
|
||||||
|
if (extract.equals(Consts.S.EMPTY)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return extract;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getService(String resource) {
|
||||||
|
String extract = Utils.extract(resource, Consts.S.SQUARE, 3);
|
||||||
|
if (extract.equals(Consts.S.EMPTY)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return extract;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getPath(String resource) {
|
||||||
|
int i = resource.lastIndexOf(Consts.S.SQUARE);
|
||||||
|
if (i == resource.length() - 1) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return resource.substring(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,6 +17,7 @@ import we.stats.ratelimit.ResourceRateLimitConfig;
|
|||||||
import we.stats.ratelimit.ResourceRateLimitConfigService;
|
import we.stats.ratelimit.ResourceRateLimitConfigService;
|
||||||
import we.util.JacksonUtils;
|
import we.util.JacksonUtils;
|
||||||
import we.util.ReflectionUtils;
|
import we.util.ReflectionUtils;
|
||||||
|
import we.util.ResourceIdUtils;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -64,7 +65,7 @@ public class FlowControlFilterTests {
|
|||||||
Map<String, ResourceRateLimitConfig> map = resourceRateLimitConfigService.getResourceRateLimitConfigMap();
|
Map<String, ResourceRateLimitConfig> map = resourceRateLimitConfigService.getResourceRateLimitConfigMap();
|
||||||
|
|
||||||
ResourceRateLimitConfig config = JacksonUtils.readValue("{\"concurrents\":66,\"enable\":1,\"id\":1,\"isDeleted\":0,\"resource\":\"_global\",\"type\":1}", ResourceRateLimitConfig.class);
|
ResourceRateLimitConfig config = JacksonUtils.readValue("{\"concurrents\":66,\"enable\":1,\"id\":1,\"isDeleted\":0,\"resource\":\"_global\",\"type\":1}", ResourceRateLimitConfig.class);
|
||||||
map.put(ResourceRateLimitConfig.NODE_RESOURCE, config);
|
map.put(ResourceIdUtils.NODE_RESOURCE, config);
|
||||||
|
|
||||||
config = JacksonUtils.readValue("{\"concurrents\":33,\"enable\":1,\"id\":2,\"isDeleted\":0, \"service\":\"xservice\", \"path\":\"/ypath\", \"type\":4}", ResourceRateLimitConfig.class);
|
config = JacksonUtils.readValue("{\"concurrents\":33,\"enable\":1,\"id\":2,\"isDeleted\":0, \"service\":\"xservice\", \"path\":\"/ypath\", \"type\":4}", ResourceRateLimitConfig.class);
|
||||||
map.put(config.getResourceId(), config);
|
map.put(config.getResourceId(), config);
|
||||||
@@ -92,12 +93,12 @@ public class FlowControlFilterTests {
|
|||||||
|
|
||||||
// System.err.println(JacksonUtils.writeValueAsString(flowStat.resourceStats));
|
// System.err.println(JacksonUtils.writeValueAsString(flowStat.resourceStats));
|
||||||
|
|
||||||
String xservice = ResourceRateLimitConfig.buildResourceId(null, null, null, "xservice", null);
|
String xservice = ResourceIdUtils.buildResourceId(null, null, null, "xservice", null);
|
||||||
List<ResourceTimeWindowStat> resourceTimeWindowStats = flowStat.getResourceTimeWindowStats(xservice, startTimeSlot, currentTimeSlot, 10);
|
List<ResourceTimeWindowStat> resourceTimeWindowStats = flowStat.getResourceTimeWindowStats(xservice, startTimeSlot, currentTimeSlot, 10);
|
||||||
TimeWindowStat win = resourceTimeWindowStats.get(0).getWindows().get(0);
|
TimeWindowStat win = resourceTimeWindowStats.get(0).getWindows().get(0);
|
||||||
assertEquals(win.getCompReqs(), 1);
|
assertEquals(win.getCompReqs(), 1);
|
||||||
|
|
||||||
String xserviceYpath = ResourceRateLimitConfig.buildResourceId(null, null, null, "xservice", "/ypath");
|
String xserviceYpath = ResourceIdUtils.buildResourceId(null, null, null, "xservice", "/ypath");
|
||||||
resourceTimeWindowStats = flowStat.getResourceTimeWindowStats(xserviceYpath, startTimeSlot, currentTimeSlot, 10);
|
resourceTimeWindowStats = flowStat.getResourceTimeWindowStats(xserviceYpath, startTimeSlot, currentTimeSlot, 10);
|
||||||
win = resourceTimeWindowStats.get(0).getWindows().get(0);
|
win = resourceTimeWindowStats.get(0).getWindows().get(0);
|
||||||
assertEquals(win.getCompReqs(), 1);
|
assertEquals(win.getCompReqs(), 1);
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
|||||||
import we.redis.RedisProperties;
|
import we.redis.RedisProperties;
|
||||||
import we.redis.RedisServerConfiguration;
|
import we.redis.RedisServerConfiguration;
|
||||||
import we.redis.RedisTemplateConfiguration;
|
import we.redis.RedisTemplateConfiguration;
|
||||||
import we.util.JacksonUtils;
|
import we.util.ResourceIdUtils;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ public class ResourceRateLimitConfigServiceTests {
|
|||||||
stringRedisTemplate.opsForHash().put("fizz_rate_limit", "2", "{\"concurrents\":66,\"enable\":1,\"id\":2,\"isDeleted\":0,\"resource\":\"service_default\",\"type\":2}");
|
stringRedisTemplate.opsForHash().put("fizz_rate_limit", "2", "{\"concurrents\":66,\"enable\":1,\"id\":2,\"isDeleted\":0,\"resource\":\"service_default\",\"type\":2}");
|
||||||
stringRedisTemplate.opsForHash().put("fizz_rate_limit", "3", "{\"concurrents\":88,\"enable\":1,\"id\":3,\"isDeleted\":0, \"type\":6, \"app\":\"xapp\", \"service\":\"yservice\" }");
|
stringRedisTemplate.opsForHash().put("fizz_rate_limit", "3", "{\"concurrents\":88,\"enable\":1,\"id\":3,\"isDeleted\":0, \"type\":6, \"app\":\"xapp\", \"service\":\"yservice\" }");
|
||||||
resourceRateLimitConfigService.init();
|
resourceRateLimitConfigService.init();
|
||||||
ResourceRateLimitConfig resourceRateLimitConfig = resourceRateLimitConfigService.getResourceRateLimitConfig(ResourceRateLimitConfig.SERVICE_DEFAULT_RESOURCE);
|
ResourceRateLimitConfig resourceRateLimitConfig = resourceRateLimitConfigService.getResourceRateLimitConfig(ResourceIdUtils.SERVICE_DEFAULT_RESOURCE);
|
||||||
// Map<String, ResourceRateLimitConfig> resourceRateLimitConfigMap = resourceRateLimitConfigService.getResourceRateLimitConfigMap();
|
// Map<String, ResourceRateLimitConfig> resourceRateLimitConfigMap = resourceRateLimitConfigService.getResourceRateLimitConfigMap();
|
||||||
// System.err.println(JacksonUtils.writeValueAsString(resourceRateLimitConfigMap));
|
// System.err.println(JacksonUtils.writeValueAsString(resourceRateLimitConfigMap));
|
||||||
assertEquals(resourceRateLimitConfig.concurrents, 66);
|
assertEquals(resourceRateLimitConfig.concurrents, 66);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package we.stats.ratelimit;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import we.util.JacksonUtils;
|
import we.util.JacksonUtils;
|
||||||
|
import we.util.ResourceIdUtils;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
@@ -25,7 +26,7 @@ public class ResourceRateLimitConfigTests {
|
|||||||
String resourceId = c.getResourceId();
|
String resourceId = c.getResourceId();
|
||||||
assertEquals("^^_global^^", resourceId);
|
assertEquals("^^_global^^", resourceId);
|
||||||
|
|
||||||
String node = ResourceRateLimitConfig.getNode(resourceId);
|
String node = ResourceIdUtils.getNode(resourceId);
|
||||||
assertEquals("_global", node);
|
assertEquals("_global", node);
|
||||||
|
|
||||||
resourceRateLimitConfigJson = "{\"concurrents\":1000,\"enable\":1,\"id\":1,\"isDeleted\":0,\"qps\":500, \"type\":2, \"resource\":\"service_default\" }";
|
resourceRateLimitConfigJson = "{\"concurrents\":1000,\"enable\":1,\"id\":1,\"isDeleted\":0,\"qps\":500, \"type\":2, \"resource\":\"service_default\" }";
|
||||||
@@ -38,10 +39,10 @@ public class ResourceRateLimitConfigTests {
|
|||||||
resourceId = c.getResourceId();
|
resourceId = c.getResourceId();
|
||||||
assertEquals("^^^xservice^", resourceId);
|
assertEquals("^^^xservice^", resourceId);
|
||||||
|
|
||||||
resourceId = ResourceRateLimitConfig.buildResourceId(null, null, ResourceRateLimitConfig.NODE, null, null);
|
resourceId = ResourceIdUtils.buildResourceId(null, null, ResourceIdUtils.NODE, null, null);
|
||||||
assertEquals("^^_global^^", resourceId);
|
assertEquals("^^_global^^", resourceId);
|
||||||
|
|
||||||
resourceId = ResourceRateLimitConfig.buildResourceId(null, "192.168.1.1", null, "xservice", null);
|
resourceId = ResourceIdUtils.buildResourceId(null, "192.168.1.1", null, "xservice", null);
|
||||||
assertEquals("^192.168.1.1^^xservice^", resourceId);
|
assertEquals("^192.168.1.1^^xservice^", resourceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user