Search in sources :

Example 6 with Instance

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;
}
Also used : ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto) Level(com.tencent.polaris.api.pojo.StatusDimension.Level) Logger(org.slf4j.Logger) ServiceChangeEvent(com.tencent.polaris.api.pojo.ServiceChangeEvent) CircuitBreakerStatus(com.tencent.polaris.api.pojo.CircuitBreakerStatus) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) StatusDimension(com.tencent.polaris.api.pojo.StatusDimension) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) Objects(java.util.Objects) List(java.util.List) Matcher(java.util.regex.Matcher) Map(java.util.Map) StringUtils(com.tencent.polaris.api.utils.StringUtils) Pattern(java.util.regex.Pattern) LinkedList(java.util.LinkedList) Instance(com.tencent.polaris.api.pojo.Instance) ServiceChangeEvent(com.tencent.polaris.api.pojo.ServiceChangeEvent) Instance(com.tencent.polaris.api.pojo.Instance) LinkedList(java.util.LinkedList)

Example 7 with Instance

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;
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 8 with Instance

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()));
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) Instance(com.tencent.polaris.api.pojo.Instance) RouteResult(com.tencent.polaris.api.plugin.route.RouteResult) MetadataFailoverType(com.tencent.polaris.api.rpc.MetadataFailoverType) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with Instance

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;
}
Also used : Utils.isHealthyInstance(com.tencent.polaris.client.util.Utils.isHealthyInstance) Instance(com.tencent.polaris.api.pojo.Instance)

Example 10 with Instance

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;
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance) ArrayList(java.util.ArrayList)

Aggregations

Instance (com.tencent.polaris.api.pojo.Instance)53 ArrayList (java.util.ArrayList)15 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)14 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)12 Server (com.netflix.loadbalancer.Server)9 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)9 PolarisServer (com.tencent.cloud.common.pojo.PolarisServer)6 Configuration (com.tencent.polaris.api.config.Configuration)6 GetOneInstanceRequest (com.tencent.polaris.api.rpc.GetOneInstanceRequest)6 HashMap (java.util.HashMap)6 DefaultServiceInstances (com.tencent.polaris.api.pojo.DefaultServiceInstances)5 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)5 ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)5 HashSet (java.util.HashSet)5 LinkedList (java.util.LinkedList)5 Map (java.util.Map)5 PolarisException (com.tencent.polaris.api.exception.PolarisException)4 ResultKey (com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult.ResultKey)4 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)4 StatusDimension (com.tencent.polaris.api.pojo.StatusDimension)4