use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.
the class Utils method checkUpdateInstances.
public static List<ServiceChangeEvent.OneInstanceUpdate> checkUpdateInstances(ServiceInstancesByProto oldVal, ServiceInstancesByProto newVal) {
Map<String, Instance> oldIns = oldVal.getInstances().stream().collect(Collectors.toMap(Instance::getId, instance -> instance));
Map<String, Instance> newIns = newVal.getInstances().stream().collect(Collectors.toMap(Instance::getId, instance -> instance));
List<ServiceChangeEvent.OneInstanceUpdate> ret = new LinkedList<>();
oldIns.forEach((id, instance) -> {
Instance ins = newIns.get(id);
if (ins == null) {
return;
}
if (!Objects.equals(ins.getRevision(), instance.getRevision())) {
ret.add(new ServiceChangeEvent.OneInstanceUpdate(instance, ins));
}
});
return ret;
}
use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.
the class Utils method checkDeleteInstances.
public static List<Instance> checkDeleteInstances(ServiceInstancesByProto oldVal, ServiceInstancesByProto newVal) {
Set<Instance> oldIns = new HashSet<>(oldVal.getInstances());
Set<Instance> newIns = new HashSet<>(newVal.getInstances());
List<Instance> ret = new LinkedList<>();
for (Instance instance : oldIns) {
if (!newIns.contains(instance)) {
ret.add(instance);
}
}
return ret;
}
use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.
the class MetadataRouter method router.
@Override
public RouteResult router(RouteInfo routeInfo, ServiceInstances instances) throws PolarisException {
FailOverType failOverType = config.getMetadataFailOverType();
Map<String, String> svcMetadata = instances.getMetadata();
if (MapUtils.isNotEmpty(svcMetadata)) {
if (svcMetadata.containsKey(KEY_METADATA_FAILOVER_TYPE)) {
String value = svcMetadata.get(KEY_METADATA_FAILOVER_TYPE);
if (valueToFailoverType.containsKey(value)) {
failOverType = valueToFailoverType.get(value);
}
}
}
MetadataFailoverType metadataFailoverType = routeInfo.getMetadataFailoverType();
if (null != metadataFailoverType) {
failOverType = inputToFailoverType.get(metadataFailoverType);
}
boolean availableInsFlag;
Map<String, String> reqMetadata = routeInfo.getDestService().getMetadata();
List<Instance> instanceList = new ArrayList<>();
for (Instance ins : instances.getInstances()) {
availableInsFlag = true;
// 要满足请求中的metadata K-V全部存在于实例的metadata中
for (Map.Entry<String, String> entry : reqMetadata.entrySet()) {
if (ins.getMetadata().containsKey(entry.getKey()) && ins.getMetadata().get(entry.getKey()).equals(entry.getValue())) {
continue;
}
availableInsFlag = false;
break;
}
if (availableInsFlag) {
instanceList.add(ins);
}
}
if (!CollectionUtils.isEmpty(instanceList)) {
return new RouteResult(instanceList, RouteResult.State.Next);
}
switch(failOverType) {
case all:
return new RouteResult(instances.getInstances(), RouteResult.State.Next);
case others:
return new RouteResult(addNotContainKeyIns(instances, reqMetadata), RouteResult.State.Next);
default:
// 默认不降级
throw new PolarisException(ErrorCode.METADATA_MISMATCH, String.format("can not find any instance by service %s", routeInfo.getDestService()));
}
}
use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.
the class NearbyRouter method hasHealthyInstances.
private CheckResult hasHealthyInstances(ServiceInstances svcInstances, Map<Level, StatusDimension> dimensions, LocationLevel targetLevel, Map<LocationLevel, String> clientInfo) {
String clientZone = "";
String clientRegion = "";
String clientCampus = "";
if (null != clientInfo) {
clientZone = clientInfo.get(LocationLevel.zone);
clientRegion = clientInfo.get(LocationLevel.region);
clientCampus = clientInfo.get(LocationLevel.campus);
}
CheckResult checkResult = new CheckResult();
for (Instance instance : svcInstances.getInstances()) {
switch(targetLevel) {
case zone:
if (clientZone.equals("") || clientZone.equals(instance.getZone())) {
checkResult.instances.add(instance);
if (isHealthyInstance(instance, dimensions)) {
checkResult.healthyInstanceCount++;
}
}
break;
case campus:
if (clientCampus.equals("") || clientCampus.equals(instance.getCampus())) {
checkResult.instances.add(instance);
if (isHealthyInstance(instance, dimensions)) {
checkResult.healthyInstanceCount++;
}
}
break;
case region:
if (clientRegion.equals("") || clientRegion.equals(instance.getRegion())) {
checkResult.instances.add(instance);
if (isHealthyInstance(instance, dimensions)) {
checkResult.healthyInstanceCount++;
}
}
break;
default:
checkResult.instances.add(instance);
if (isHealthyInstance(instance, dimensions)) {
checkResult.healthyInstanceCount++;
}
break;
}
}
return checkResult;
}
use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.
the class RuleBasedRouter method populateSubsetsFromDest.
/**
* populateSubsetsFromDest 根据destination中的规则填充分组列表
*
* @param instances 实例信息
* @param dest 目标规则
* @param subsetsMap 实例分组
* @return 是否成功加入subset列表
*/
private boolean populateSubsetsFromDest(ServiceInstances instances, RoutingProto.Destination dest, Map<Integer, PrioritySubsets> subsetsMap, Map<String, String> multiEnvRouterParamMap) {
// 获取subset
List<Instance> oriInstances = instances.getInstances();
List<Instance> filteredInstances = new ArrayList<>();
for (Instance ins : oriInstances) {
if (!matchMetadata(dest.getMetadataMap(), ins.getMetadata(), false, multiEnvRouterParamMap)) {
continue;
}
filteredInstances.add(ins);
}
// subset中无实例
if (CollectionUtils.isEmpty(filteredInstances)) {
return false;
}
// 根据优先级填充subset列表
int priority = dest.getPriority().getValue();
int weight = dest.getWeight().getValue();
PrioritySubsets weightedSubsets = subsetsMap.get(priority);
if (weightedSubsets == null) {
PrioritySubsets prioritySubsets = new PrioritySubsets();
WeightedSubset weightedSubset = new WeightedSubset();
weightedSubset.setInstances(filteredInstances);
weightedSubset.setWeight(weight);
prioritySubsets.setSubsets(new ArrayList<>(Collections.singletonList(weightedSubset)));
prioritySubsets.setTotalWeight(weight);
subsetsMap.put(priority, prioritySubsets);
} else {
WeightedSubset weightedSubset = new WeightedSubset();
weightedSubset.setInstances(filteredInstances);
weightedSubset.setWeight(weight);
weightedSubsets.getSubsets().add(weightedSubset);
weightedSubsets.setTotalWeight(weightedSubsets.getTotalWeight() + weight);
}
return true;
}
Aggregations