-
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.
router plugin: xDS router common module
Signed-off-by: daizhenyu <[email protected]>
- Loading branch information
Showing
13 changed files
with
1,192 additions
and
0 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
100 changes: 100 additions & 0 deletions
100
...mant-router/router-common/src/main/java/io/sermant/router/common/utils/XdsRouterUtil.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,100 @@ | ||
/* | ||
* 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.router.common.utils; | ||
|
||
import io.sermant.core.config.ConfigManager; | ||
import io.sermant.core.plugin.config.ServiceMeta; | ||
import io.sermant.core.service.ServiceManager; | ||
import io.sermant.core.service.xds.XdsCoreService; | ||
import io.sermant.core.service.xds.XdsServiceDiscovery; | ||
import io.sermant.core.service.xds.entity.ServiceInstance; | ||
import io.sermant.core.service.xds.entity.XdsLocality; | ||
import io.sermant.core.utils.NetworkUtils; | ||
import io.sermant.core.utils.StringUtils; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
/** | ||
* XdsRouterUtil | ||
* | ||
* @author daizhenyu | ||
* @since 2024-08-29 | ||
**/ | ||
public class XdsRouterUtil { | ||
private static XdsServiceDiscovery serviceDiscovery = ServiceManager.getService(XdsCoreService.class) | ||
.getXdsServiceDiscovery(); | ||
|
||
private static XdsLocality xdsLocality; | ||
|
||
private XdsRouterUtil() { | ||
} | ||
|
||
/** | ||
* get XdsLocality of self-service | ||
* | ||
* @return XdsLocality | ||
*/ | ||
public static Optional<XdsLocality> getLocalityInfoOfSelfService() { | ||
if (xdsLocality != null) { | ||
return Optional.of(xdsLocality); | ||
} | ||
synchronized (XdsRouterUtil.class) { | ||
if (xdsLocality != null) { | ||
return Optional.of(xdsLocality); | ||
} | ||
Set<ServiceInstance> serviceInstances = serviceDiscovery | ||
.getServiceInstance(ConfigManager.getConfig(ServiceMeta.class).getService()); | ||
String podIp = NetworkUtils.getKubernetesPodIp(); | ||
if (StringUtils.isEmpty(podIp)) { | ||
return Optional.empty(); | ||
} | ||
Optional<ServiceInstance> serviceInstance = matchServiceInstanceByPodIp(serviceInstances, podIp); | ||
if (serviceInstance.isPresent()) { | ||
Optional<XdsLocality> validXdsLocality = createValidXdsLocality(serviceInstance.get().getMetaData()); | ||
xdsLocality = validXdsLocality.orElse(null); | ||
return validXdsLocality; | ||
} | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
private static Optional<ServiceInstance> matchServiceInstanceByPodIp(Set<ServiceInstance> serviceInstances, | ||
String podIp) { | ||
return serviceInstances.stream() | ||
.filter(serviceInstance -> podIp.equals(serviceInstance.getHost())) | ||
.findFirst(); | ||
} | ||
|
||
private static Optional<XdsLocality> createValidXdsLocality(Map<String, String> metaData) { | ||
XdsLocality locality = new XdsLocality(); | ||
String region = metaData.get("region"); | ||
if (StringUtils.isEmpty(region)) { | ||
return Optional.empty(); | ||
} | ||
locality.setRegion(region); | ||
String zone = metaData.get("zone"); | ||
String subZone = metaData.get("sub_zone"); | ||
if (StringUtils.isEmpty(zone) && !StringUtils.isEmpty(subZone)) { | ||
return Optional.empty(); | ||
} | ||
locality.setZone(zone); | ||
locality.setSubZone(subZone); | ||
return Optional.of(locality); | ||
} | ||
} |
231 changes: 231 additions & 0 deletions
231
...ant-router/router-common/src/main/java/io/sermant/router/common/xds/XdsRouterHandler.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,231 @@ | ||
/* | ||
* 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.router.common.xds; | ||
|
||
import io.sermant.core.service.ServiceManager; | ||
import io.sermant.core.service.xds.XdsCoreService; | ||
import io.sermant.core.service.xds.XdsRouteService; | ||
import io.sermant.core.service.xds.XdsServiceDiscovery; | ||
import io.sermant.core.service.xds.entity.ServiceInstance; | ||
import io.sermant.core.service.xds.entity.XdsClusterLoadAssigment; | ||
import io.sermant.core.service.xds.entity.XdsHeaderMatcher; | ||
import io.sermant.core.service.xds.entity.XdsLocality; | ||
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.router.common.utils.XdsRouterUtil; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Random; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* XdsRouterHandler | ||
* | ||
* @author daizhenyu | ||
* @since 2024-08-29 | ||
**/ | ||
public enum XdsRouterHandler { | ||
/** | ||
* singleton | ||
*/ | ||
INSTANCE; | ||
|
||
private final Random random = new Random(); | ||
|
||
private final XdsRouteService routeService; | ||
|
||
private final XdsServiceDiscovery serviceDiscovery; | ||
|
||
/** | ||
* constructor | ||
*/ | ||
XdsRouterHandler() { | ||
XdsCoreService xdsCoreService = ServiceManager.getService(XdsCoreService.class); | ||
routeService = xdsCoreService.getXdsRouteService(); | ||
serviceDiscovery = xdsCoreService.getXdsServiceDiscovery(); | ||
} | ||
|
||
/** | ||
* getServiceInstanceByXdsRoute | ||
* | ||
* @param serviceName service name | ||
* @param path request path | ||
* @return serviceInstance | ||
*/ | ||
public Set<ServiceInstance> getServiceInstanceByXdsRoute(String serviceName, String path) { | ||
return getMatchedServiceInstance(serviceName, path, null, MatchType.PATH); | ||
} | ||
|
||
/** | ||
* getServiceInstanceByXdsRoute | ||
* | ||
* @param serviceName service name | ||
* @param headers request headers | ||
* @return serviceInstance | ||
*/ | ||
public Set<ServiceInstance> getServiceInstanceByXdsRoute(String serviceName, Map<String, String> headers) { | ||
return getMatchedServiceInstance(serviceName, null, headers, MatchType.HEADER); | ||
} | ||
|
||
/** | ||
* getServiceInstanceByXdsRoute | ||
* | ||
* @param serviceName service name | ||
* @param path request path | ||
* @param headers request headers | ||
* @return serviceInstance | ||
*/ | ||
public Set<ServiceInstance> getServiceInstanceByXdsRoute(String serviceName, String path, | ||
Map<String, String> headers) { | ||
return getMatchedServiceInstance(serviceName, path, headers, MatchType.BOTH); | ||
} | ||
|
||
private Set<ServiceInstance> getMatchedServiceInstance(String serviceName, String path, | ||
Map<String, String> headers, MatchType matchType) { | ||
List<XdsRoute> routes = routeService.getServiceRoute(serviceName); | ||
XdsRoute matchedRoute = null; | ||
|
||
for (XdsRoute route : routes) { | ||
XdsRouteMatch routeMatch = route.getRouteMatch(); | ||
boolean pathMatched = matchType == MatchType.PATH || matchType == MatchType.BOTH; | ||
boolean headerMatched = matchType == MatchType.HEADER || matchType == MatchType.BOTH; | ||
|
||
// check path matching | ||
if (pathMatched && !isPathMatched(routeMatch.getPathMatcher(), path)) { | ||
continue; | ||
} | ||
|
||
// check head matching | ||
if (headerMatched && !isHeadersMatched(routeMatch.getHeaderMatchers(), headers)) { | ||
continue; | ||
} | ||
matchedRoute = route; | ||
break; | ||
} | ||
|
||
if (matchedRoute == null) { | ||
return serviceDiscovery.getServiceInstance(serviceName); | ||
} | ||
return handleXdsRoute(matchedRoute, serviceName); | ||
} | ||
|
||
private Set<ServiceInstance> handleXdsRoute(XdsRoute route, String serviceName) { | ||
// select cluster | ||
XdsRouteAction routeAction = route.getRouteAction(); | ||
String cluster = routeAction.getCluster(); | ||
if (routeAction.isWeighted()) { | ||
cluster = selectClusterByWeight(routeAction.getWeightedClusters()); | ||
} | ||
|
||
// get service instance of cluster | ||
Optional<XdsClusterLoadAssigment> loadAssigmentOptional = | ||
serviceDiscovery.getClusterServiceInstance(cluster); | ||
if (!loadAssigmentOptional.isPresent()) { | ||
return serviceDiscovery.getServiceInstance(serviceName); | ||
} | ||
XdsClusterLoadAssigment clusterLoadAssigment = loadAssigmentOptional.get(); | ||
|
||
if (!routeService.isLocalityRoute(clusterLoadAssigment.getClusterName())) { | ||
Set<ServiceInstance> serviceInstances = getServiceInstanceOfCluster(clusterLoadAssigment); | ||
return serviceInstances.isEmpty() ? serviceDiscovery.getServiceInstance(serviceName) : serviceInstances; | ||
} | ||
|
||
// get locality info of self-service and route by locality | ||
Optional<XdsLocality> localityInfoOfSelfService = XdsRouterUtil.getLocalityInfoOfSelfService(); | ||
if (localityInfoOfSelfService.isPresent()) { | ||
Set<ServiceInstance> serviceInstances = getServiceInstanceOfLocalityCluster(clusterLoadAssigment, | ||
localityInfoOfSelfService.get()); | ||
if (!serviceInstances.isEmpty()) { | ||
return serviceInstances; | ||
} | ||
} | ||
|
||
Set<ServiceInstance> serviceInstances = getServiceInstanceOfCluster(clusterLoadAssigment); | ||
return serviceInstances.isEmpty() ? serviceDiscovery.getServiceInstance(serviceName) : serviceInstances; | ||
} | ||
|
||
private Set<ServiceInstance> getServiceInstanceOfLocalityCluster(XdsClusterLoadAssigment clusterLoadAssigment, | ||
XdsLocality locality) { | ||
return clusterLoadAssigment.getLocalityInstances().entrySet().stream() | ||
.filter(xdsLocalitySetEntry -> xdsLocalitySetEntry.getKey().equals(locality)) | ||
.flatMap(xdsLocalitySetEntry -> xdsLocalitySetEntry.getValue().stream()) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
private Set<ServiceInstance> getServiceInstanceOfCluster(XdsClusterLoadAssigment clusterLoadAssigment) { | ||
return clusterLoadAssigment.getLocalityInstances().entrySet().stream() | ||
.flatMap(instanceEntry -> instanceEntry.getValue().stream()) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
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 = random.nextInt(totalWeight); | ||
|
||
int currentWeight = 0; | ||
for (XdsClusterWeight clusterWeight : clusters) { | ||
currentWeight += clusterWeight.getWeight(); | ||
if (randomWeight < currentWeight) { | ||
return clusterWeight.getClusterName(); | ||
} | ||
} | ||
return StringUtils.EMPTY; | ||
} | ||
|
||
/** | ||
* XdsRouterHandler | ||
* | ||
* @author daizhenyu | ||
* @since 2024-08-29 | ||
**/ | ||
public enum MatchType { | ||
/** | ||
* path match | ||
*/ | ||
PATH, | ||
/** | ||
* header match | ||
*/ | ||
HEADER, | ||
/** | ||
* path and header match | ||
*/ | ||
BOTH | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...t-router/router-common/src/main/java/io/sermant/router/common/xds/lb/XdsLoadBalancer.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,37 @@ | ||
/* | ||
* 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.router.common.xds.lb; | ||
|
||
import io.sermant.core.service.xds.entity.ServiceInstance; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* XdsLoadBalancer | ||
* | ||
* @author daizhenyu | ||
* @since 2024-08-30 | ||
**/ | ||
public interface XdsLoadBalancer { | ||
/** | ||
* select instance by loadbalancer | ||
* | ||
* @param instances service instance | ||
* @return selected instance | ||
*/ | ||
ServiceInstance selectInstance(List<ServiceInstance> instances); | ||
} |
Oops, something went wrong.