-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1700 from hanbingleixue/xds-flowcontrol
Add the common module with XDS circuit breaker and rate limiting function
- Loading branch information
Showing
34 changed files
with
2,448 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,20 @@ | ||
# FlowControl configuration | ||
flow.control.plugin: | ||
useCseRule: true # whether to configure cse rules | ||
enable-start-monitor: false # whether to enable indicator monitoring | ||
enable-system-adaptive: false # whether to enable system adaptive flow control | ||
enable-system-rule: false # whether to enable system rule flow control | ||
# whether to configure cse rules | ||
useCseRule: true | ||
# whether to enable indicator monitoring | ||
enable-start-monitor: false | ||
# whether to enable system adaptive flow control | ||
enable-system-adaptive: false | ||
# whether to enable system rule flow control | ||
enable-system-rule: false | ||
xds.flow.control.config: | ||
# Whether to enable Xds flow control | ||
enable: false | ||
retry: | ||
# The specified response status codes that need to be retried. Retry will be performed when the response's status | ||
# code matches one of the specified codes. | ||
x-sermant-retriable-status-codes: | ||
# The specified response header names that need to be retried. Retry will be performed when the response contains | ||
# the specified headers. | ||
x-sermant-retriable-header-names: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...ntrol-common/src/main/java/io/sermant/flowcontrol/common/config/XdsFlowControlConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (C) 2021-2022 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package io.sermant.flowcontrol.common.config; | ||
|
||
import io.sermant.core.config.common.ConfigFieldKey; | ||
import io.sermant.core.config.common.ConfigTypeKey; | ||
import io.sermant.core.plugin.config.PluginConfig; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* retry configuration class | ||
* | ||
* @author zhouss | ||
* @since 2022-01-28 | ||
*/ | ||
@ConfigTypeKey("xds.flow.control.config") | ||
public class XdsFlowControlConfig implements PluginConfig { | ||
/** | ||
* Specify the response code for retry, and retry will be executed when the response code is included | ||
*/ | ||
@ConfigFieldKey("retry.x-sermant-retriable-status-codes") | ||
private List<String> retryStatusCodes; | ||
|
||
/** | ||
* Specify the response code for retry, and retry will be executed when the response header is included | ||
*/ | ||
@ConfigFieldKey("retry.x-sermant-retriable-header-names") | ||
private List<String> retryHeaderNames; | ||
|
||
/** | ||
* xds flow control switch | ||
*/ | ||
private boolean enable; | ||
|
||
public List<String> getRetryStatusCodes() { | ||
return retryStatusCodes; | ||
} | ||
|
||
public void setRetryStatusCodes(List<String> retryStatusCodes) { | ||
this.retryStatusCodes = retryStatusCodes; | ||
} | ||
|
||
public List<String> getRetryHeaderNames() { | ||
return retryHeaderNames; | ||
} | ||
|
||
public void setRetryHeaderNames(List<String> retryHeaderNames) { | ||
this.retryHeaderNames = retryHeaderNames; | ||
} | ||
|
||
public boolean isEnable() { | ||
return enable; | ||
} | ||
|
||
public void setEnable(boolean enable) { | ||
this.enable = enable; | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
...l-common/src/main/java/io/sermant/flowcontrol/common/core/match/XdsRouteMatchManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.sermant.flowcontrol.common.core.match; | ||
|
||
import io.sermant.core.service.xds.entity.XdsHeaderMatcher; | ||
import io.sermant.core.service.xds.entity.XdsPathMatcher; | ||
import io.sermant.core.service.xds.entity.XdsRoute; | ||
import io.sermant.core.service.xds.entity.XdsRouteAction; | ||
import io.sermant.core.service.xds.entity.XdsRouteAction.XdsClusterWeight; | ||
import io.sermant.core.service.xds.entity.XdsRouteAction.XdsWeightedClusters; | ||
import io.sermant.core.service.xds.entity.XdsRouteMatch; | ||
import io.sermant.core.utils.CollectionUtils; | ||
import io.sermant.core.utils.StringUtils; | ||
import io.sermant.flowcontrol.common.entity.FlowControlScenario; | ||
import io.sermant.flowcontrol.common.entity.RequestEntity; | ||
import io.sermant.flowcontrol.common.util.RandomUtil; | ||
import io.sermant.flowcontrol.common.xds.handler.XdsHandler; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* XdsRouteMatchManager, Get BusinessEntity based on xDS routing rules | ||
* | ||
* @author zhp | ||
* @since 2024-12-20 | ||
**/ | ||
public enum XdsRouteMatchManager { | ||
/** | ||
* singleton | ||
*/ | ||
INSTANCE; | ||
|
||
/** | ||
* get matched scenario information | ||
* | ||
* @param requestEntity request-information | ||
* @param serviceName service name | ||
* @return matched business information | ||
*/ | ||
public FlowControlScenario getMatchedScenarioInfo(RequestEntity requestEntity, String serviceName) { | ||
FlowControlScenario scenario = new FlowControlScenario(); | ||
scenario.setServiceName(serviceName); | ||
Optional<XdsRoute> matchedRouteOptional = getMatchedRoute(requestEntity, serviceName); | ||
if (!matchedRouteOptional.isPresent()) { | ||
return scenario; | ||
} | ||
XdsRoute matchedRoute = matchedRouteOptional.get(); | ||
scenario.setRouteName(matchedRoute.getName()); | ||
scenario.setClusterName(selectClusterByRoute(matchedRoute)); | ||
return scenario; | ||
} | ||
|
||
private Optional<XdsRoute> getMatchedRoute(RequestEntity requestEntity, String serviceName) { | ||
List<XdsRoute> routes = | ||
XdsHandler.INSTANCE.getServiceRouteByServiceName(serviceName); | ||
for (XdsRoute route : routes) { | ||
XdsRouteMatch routeMatch = route.getRouteMatch(); | ||
|
||
// check path matching | ||
if (!isPathMatched(routeMatch.getPathMatcher(), requestEntity.getApiPath())) { | ||
continue; | ||
} | ||
|
||
// check head matching | ||
if (!isHeadersMatched(routeMatch.getHeaderMatchers(), requestEntity.getHeaders())) { | ||
continue; | ||
} | ||
return Optional.of(route); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private boolean isPathMatched(XdsPathMatcher matcher, String path) { | ||
return matcher.isMatch(path); | ||
} | ||
|
||
private boolean isHeadersMatched(List<XdsHeaderMatcher> matchers, Map<String, String> headers) { | ||
return matchers.stream() | ||
.allMatch(xdsHeaderMatcher -> xdsHeaderMatcher.isMatch(headers)); | ||
} | ||
|
||
private String selectClusterByRoute(XdsRoute matchedRoute) { | ||
XdsRouteAction routeAction = matchedRoute.getRouteAction(); | ||
String cluster = routeAction.getCluster(); | ||
if (!routeAction.isWeighted() || routeAction.getWeightedClusters() == null) { | ||
return cluster; | ||
} | ||
XdsWeightedClusters weightedClusters = routeAction.getWeightedClusters(); | ||
List<XdsClusterWeight> clusters = weightedClusters.getClusters(); | ||
int totalWeight = weightedClusters.getTotalWeight(); | ||
if (CollectionUtils.isEmpty(clusters) || totalWeight == 0) { | ||
return StringUtils.EMPTY; | ||
} | ||
int randomWeight = RandomUtil.randomInt(totalWeight); | ||
|
||
int currentWeight = 0; | ||
for (XdsClusterWeight clusterWeight : clusters) { | ||
currentWeight += clusterWeight.getWeight(); | ||
if (randomWeight < currentWeight) { | ||
return clusterWeight.getClusterName(); | ||
} | ||
} | ||
return StringUtils.EMPTY; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.