-
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.
Add the common part of Xds flow control plugin
Signed-off-by: hanbingleixue <[email protected]>
- Loading branch information
1 parent
723b3e4
commit 19a02d0
Showing
33 changed files
with
2,514 additions
and
15 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
88 changes: 88 additions & 0 deletions
88
...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,88 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* whether to use secure protocol to invoke downstream service with xds route, example: http or https | ||
*/ | ||
@ConfigFieldKey("enabled-springcloud-xds-route-secure") | ||
private boolean enabledSpringCloudXdsRouteSecure; | ||
|
||
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; | ||
} | ||
|
||
public boolean isEnabledSpringCloudXdsRouteSecure() { | ||
return enabledSpringCloudXdsRouteSecure; | ||
} | ||
|
||
public void setEnabledSpringCloudXdsRouteSecure(boolean enabledSpringCloudXdsRouteSecure) { | ||
this.enabledSpringCloudXdsRouteSecure = enabledSpringCloudXdsRouteSecure; | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
...ontrol-common/src/main/java/io/sermant/flowcontrol/common/core/match/XdsMatchManager.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,120 @@ | ||
/* | ||
* 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.XdsFlowControlHandler; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* XdsMatchManager, Get BusinessEntity based on xDS routing rules | ||
* | ||
* @author daizhenyu | ||
* @since 2024-08-29 | ||
**/ | ||
public enum XdsMatchManager { | ||
/** | ||
* 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); | ||
List<XdsRoute> routes = | ||
XdsFlowControlHandler.INSTANCE.getServiceRouteByServiceName(serviceName); | ||
Optional<XdsRoute> matchedRouteOptional = getMatchedRoute(requestEntity, routes); | ||
if (!matchedRouteOptional.isPresent()) { | ||
return scenario; | ||
} | ||
XdsRoute matchedRoute = matchedRouteOptional.get(); | ||
XdsRouteAction routeAction = matchedRoute.getRouteAction(); | ||
String cluster = routeAction.getCluster(); | ||
if (routeAction.isWeighted()) { | ||
cluster = selectClusterByWeight(routeAction.getWeightedClusters()); | ||
} | ||
scenario.setRouteName(matchedRoute.getName()); | ||
scenario.setClusterName(cluster); | ||
return scenario; | ||
} | ||
|
||
private Optional<XdsRoute> getMatchedRoute(RequestEntity requestEntity, List<XdsRoute> routes) { | ||
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 selectClusterByWeight(XdsWeightedClusters weightedClusters) { | ||
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.