use of com.tencent.polaris.api.plugin.route.LocationLevel in project polaris-java by polarismesh.
the class NearbyRouter method refreshLocationInfo.
private void refreshLocationInfo() {
Map<LocationLevel, String> clientLocationInfo = new HashMap<>();
for (LocationLevel key : LocationLevel.values()) {
if (valueContext.getValue(key.name()) != null) {
clientLocationInfo.put(key, valueContext.getValue(key.name()));
}
}
locationInfo.set(clientLocationInfo);
LOG.debug("[refreshLocationInfo] locationInfo={}", clientLocationInfo);
}
use of com.tencent.polaris.api.plugin.route.LocationLevel in project polaris-java by polarismesh.
the class NearbyRouter method router.
@Override
public RouteResult router(RouteInfo routeInfo, ServiceInstances serviceInstances) throws PolarisException {
// 先获取最低可用就近级别
LocationLevel minAvailableLevel = config.getMatchLevel();
if (null == minAvailableLevel) {
minAvailableLevel = defaultMinLevel;
}
LocationLevel minLevel = minAvailableLevel;
if (null != routeInfo.getNextRouterInfo()) {
if (null != routeInfo.getNextRouterInfo().getLocationLevel()) {
minLevel = routeInfo.getNextRouterInfo().getLocationLevel();
}
if (null != routeInfo.getNextRouterInfo().getMinAvailableLevel()) {
minAvailableLevel = routeInfo.getNextRouterInfo().getMinAvailableLevel();
}
}
LocationLevel maxLevel = config.getMaxMatchLevel();
if (null == maxLevel) {
maxLevel = LocationLevel.all;
}
Map<LocationLevel, String> clientLocationInfo = locationInfo.get();
if (minLevel.ordinal() >= maxLevel.ordinal()) {
List<Instance> instances = selectInstances(serviceInstances, minAvailableLevel, clientLocationInfo);
if (CollectionUtils.isEmpty(instances)) {
throw new PolarisException(ErrorCode.LOCATION_MISMATCH, String.format("can not find any instance by level %s", minLevel.name()));
}
// 已经循环了一圈
return new RouteResult(selectInstances(serviceInstances, minAvailableLevel, clientLocationInfo), RouteResult.State.Next);
}
CheckResult checkResult = new CheckResult();
for (int i = minLevel.ordinal(); i <= maxLevel.ordinal(); i++) {
LocationLevel curLevel = LocationLevel.values()[i];
checkResult = hasHealthyInstances(serviceInstances, routeInfo.getStatusDimensions(), curLevel, clientLocationInfo);
checkResult.curLevel = curLevel;
if (!CollectionUtils.isEmpty(checkResult.instances)) {
break;
} else {
minAvailableLevel = curLevel;
}
}
if (CollectionUtils.isEmpty(checkResult.instances)) {
throw new PolarisException(ErrorCode.LOCATION_MISMATCH, String.format("can not find any instance by level %s", checkResult.curLevel.name()));
}
if (!config.isEnableDegradeByUnhealthyPercent() || checkResult.curLevel == LocationLevel.all) {
return new RouteResult(checkResult.instances, RouteResult.State.Next);
}
int healthyInstanceCount = checkResult.healthyInstanceCount;
double actualHealthyPercent = (double) healthyInstanceCount / (double) serviceInstances.getInstances().size();
if (actualHealthyPercent <= healthyPercentToDegrade) {
LOG.debug("[shouldDegrade] enableDegradeByUnhealthyPercent = {},unhealthyPercentToDegrade={}," + "healthyPercent={},isStrict={},matchLevel={}", config.isEnableDegradeByUnhealthyPercent(), config.getUnhealthyPercentToDegrade(), actualHealthyPercent, config.isStrictNearby(), checkResult.curLevel);
RouteResult result = new RouteResult(checkResult.instances, RouteResult.State.Retry);
result.getNextRouterInfo().setLocationLevel(nextLevel(checkResult.curLevel));
result.getNextRouterInfo().setMinAvailableLevel(minAvailableLevel);
return result;
}
return new RouteResult(checkResult.instances, RouteResult.State.Next);
}
Aggregations