Search in sources :

Example 11 with Instance

use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.

the class DefaultRouterAPI method processLoadBalance.

@Override
public ProcessLoadBalanceResponse processLoadBalance(ProcessLoadBalanceRequest request) throws PolarisException {
    checkAvailable("EngineAPI");
    RouterValidator.validateProcessLoadBalanceRequest(request);
    String lbPolicy = request.getLbPolicy();
    if (StringUtils.isBlank(lbPolicy)) {
        lbPolicy = extensions.getConfiguration().getConsumer().getLoadbalancer().getType();
    }
    LoadBalancer loadBalancer = (LoadBalancer) extensions.getPlugins().getPlugin(PluginTypes.LOAD_BALANCER.getBaseType(), lbPolicy);
    Instance instance = BaseFlow.processLoadBalance(loadBalancer, request.getCriteria(), request.getDstInstances());
    return new ProcessLoadBalanceResponse(instance);
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance) ProcessLoadBalanceResponse(com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse) LoadBalancer(com.tencent.polaris.api.plugin.loadbalance.LoadBalancer)

Example 12 with Instance

use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.

the class AsyncRateLimitConnector method getServiceInstance.

/**
 * 一致性hash保证被调正常的情况下,拿到的都是同一个节点
 *
 * @param remoteCluster 远程集群信息
 * @return 节点标识
 */
private HostIdentifier getServiceInstance(Extensions extensions, ServiceKey remoteCluster, String hashValue) {
    Instance instance = BaseFlow.commonGetOneInstance(extensions, remoteCluster, coreRouters, LoadBalanceConfig.LOAD_BALANCE_RING_HASH, "grpc", hashValue);
    if (instance == null) {
        LOG.error("can not found any instance by serviceKye:{}", remoteCluster);
        return null;
    }
    String host = instance.getHost();
    int port = instance.getPort();
    return new HostIdentifier(host, port);
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance)

Example 13 with Instance

use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.

the class ErrRateCircuitBreaker method stat.

@Override
public boolean stat(InstanceGauge gauge) {
    InstanceByProto instance = ChangeStateUtils.getInstance(gauge, localRegistry);
    if (null == instance) {
        return false;
    }
    InstanceLocalValue instanceLocalValue = instance.getInstanceLocalValue();
    if (null == instanceLocalValue) {
        return false;
    }
    ConfigSet<Config> configSet = CircuitBreakUtils.getConfigSet(gauge, this);
    StatusDimension statusDimension = ChangeStateUtils.buildStatusDimension(gauge, configSet.getLevel());
    if (CircuitBreakUtils.instanceClose(instance, statusDimension)) {
        Object pluginValue = instanceLocalValue.getPluginValue(id, create);
        ErrRateCounter errRateCounter = (ErrRateCounter) pluginValue;
        SliceWindow metricWindow = errRateCounter.getSliceWindow(statusDimension);
        metricWindow.addGauge((bucket -> {
            bucket.addMetric(Dimension.keyRequestCount.ordinal(), 1);
            if (gauge.getRetStatus() == RetStatus.RetFail) {
                return bucket.addMetric(Dimension.keyFailCount.ordinal(), 1);
            }
            return bucket.getMetric(Dimension.keyFailCount.ordinal());
        }));
    } else if (CircuitBreakUtils.instanceHalfOpen(instance, statusDimension)) {
        // 半开计数器
        Object pluginValue = instanceLocalValue.getPluginValue(id, create);
        HalfOpenCounter consecutiveCounter = (HalfOpenCounter) pluginValue;
        RetStatus retStatus = gauge.getRetStatus();
        return consecutiveCounter.triggerHalfOpenConversion(statusDimension, retStatus, configSet.getHalfOpenConfig());
    }
    return false;
}
Also used : CircuitBreakResult(com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult) DefaultPlugins(com.tencent.polaris.api.config.plugin.DefaultPlugins) SliceWindow(com.tencent.polaris.plugins.circuitbreaker.common.stat.SliceWindow) InstanceLocalValue(com.tencent.polaris.api.pojo.InstanceLocalValue) DefaultFlowControlParam(com.tencent.polaris.client.flow.DefaultFlowControlParam) InitContext(com.tencent.polaris.api.plugin.common.InitContext) Destroyable(com.tencent.polaris.api.control.Destroyable) OutlierDetectionConfig(com.tencent.polaris.api.config.consumer.OutlierDetectionConfig) RuleIdentifier(com.tencent.polaris.plugins.circuitbreaker.common.RuleIdentifier) PluginTypes(com.tencent.polaris.api.plugin.common.PluginTypes) InstanceByProto(com.tencent.polaris.client.pojo.InstanceByProto) Function(java.util.function.Function) FlowControlParam(com.tencent.polaris.client.flow.FlowControlParam) CbPolicy(com.tencent.polaris.client.pb.CircuitBreakerProto.CbPolicy) RetStatus(com.tencent.polaris.api.pojo.RetStatus) HalfOpenCounter(com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenCounter) ConfigSetLocator(com.tencent.polaris.plugins.circuitbreaker.common.ConfigSetLocator) ConfigSet(com.tencent.polaris.plugins.circuitbreaker.common.ConfigSet) DestinationSet(com.tencent.polaris.client.pb.CircuitBreakerProto.DestinationSet) RecoverConfig(com.tencent.polaris.client.pb.CircuitBreakerProto.RecoverConfig) CircuitBreakUtils(com.tencent.polaris.plugins.circuitbreaker.common.CircuitBreakUtils) LocalRegistry(com.tencent.polaris.api.plugin.registry.LocalRegistry) ErrorCode(com.tencent.polaris.api.exception.ErrorCode) InstanceGauge(com.tencent.polaris.api.pojo.InstanceGauge) CollectionUtils(com.tencent.polaris.api.utils.CollectionUtils) PluginConfigProvider(com.tencent.polaris.api.config.plugin.PluginConfigProvider) CircuitBreaker(com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreaker) CircuitBreakerConfig(com.tencent.polaris.api.config.consumer.CircuitBreakerConfig) Verifier(com.tencent.polaris.api.config.verify.Verifier) Collection(java.util.Collection) Extensions(com.tencent.polaris.api.plugin.compose.Extensions) StatusDimension(com.tencent.polaris.api.pojo.StatusDimension) ChangeStateUtils(com.tencent.polaris.plugins.circuitbreaker.common.ChangeStateUtils) ErrRateConfig(com.tencent.polaris.client.pb.CircuitBreakerProto.CbPolicy.ErrRateConfig) HalfOpenConfig(com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenConfig) PluginType(com.tencent.polaris.api.plugin.PluginType) StateMachine(com.tencent.polaris.plugins.circuitbreaker.common.StateMachine) ConfigGroup(com.tencent.polaris.plugins.circuitbreaker.common.ConfigGroup) RuleDestinationResult(com.tencent.polaris.plugins.circuitbreaker.common.CircuitBreakUtils.RuleDestinationResult) PolarisException(com.tencent.polaris.api.exception.PolarisException) Instance(com.tencent.polaris.api.pojo.Instance) Subset(com.tencent.polaris.api.pojo.Subset) InstanceByProto(com.tencent.polaris.client.pojo.InstanceByProto) RetStatus(com.tencent.polaris.api.pojo.RetStatus) OutlierDetectionConfig(com.tencent.polaris.api.config.consumer.OutlierDetectionConfig) RecoverConfig(com.tencent.polaris.client.pb.CircuitBreakerProto.RecoverConfig) CircuitBreakerConfig(com.tencent.polaris.api.config.consumer.CircuitBreakerConfig) ErrRateConfig(com.tencent.polaris.client.pb.CircuitBreakerProto.CbPolicy.ErrRateConfig) HalfOpenConfig(com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenConfig) InstanceLocalValue(com.tencent.polaris.api.pojo.InstanceLocalValue) StatusDimension(com.tencent.polaris.api.pojo.StatusDimension) HalfOpenCounter(com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenCounter) SliceWindow(com.tencent.polaris.plugins.circuitbreaker.common.stat.SliceWindow)

Example 14 with Instance

use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.

the class ChangeStateUtils method getInstance.

/**
 * 获取实例ID
 *
 * @param instanceGauge 实例统计数据
 * @param localRegistry 本地缓存插件
 * @return ID
 */
public static InstanceByProto getInstance(InstanceGauge instanceGauge, LocalRegistry localRegistry) {
    ServiceEventKey serviceEventKey = new ServiceEventKey(new ServiceKey(instanceGauge.getNamespace(), instanceGauge.getService()), EventType.INSTANCE);
    ResourceFilter resourceFilter = new ResourceFilter(serviceEventKey, true, true);
    ServiceInstances instances = localRegistry.getInstances(resourceFilter);
    if (!instances.isInitialized()) {
        return null;
    }
    ServiceInstancesByProto serviceInstancesByProto = (ServiceInstancesByProto) instances;
    Instance instance = instanceGauge.getInstance();
    if (instance instanceof InstanceByProto) {
        return (InstanceByProto) instance;
    }
    InstanceByProto instanceByProto;
    String instanceId = instanceGauge.getInstanceId();
    if (StringUtils.isNotBlank(instanceId)) {
        instanceByProto = serviceInstancesByProto.getInstance(instanceId);
    } else {
        Node node = new Node(instanceGauge.getHost(), instanceGauge.getPort());
        instanceByProto = serviceInstancesByProto.getInstanceByNode(node);
    }
    if (null != instanceByProto) {
        instanceGauge.setInstance(instanceByProto);
    }
    return instanceByProto;
}
Also used : ResourceFilter(com.tencent.polaris.api.plugin.registry.ResourceFilter) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) InstanceByProto(com.tencent.polaris.client.pojo.InstanceByProto) Instance(com.tencent.polaris.api.pojo.Instance) Node(com.tencent.polaris.client.pojo.Node) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto)

Example 15 with Instance

use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.

the class ChangeStateUtils method buildCircuitBreakResult.

/**
 * 构建熔断结果
 *
 * @param stateMachine 熔断状态机
 * @param instances 实例集
 * @param parameter 熔断参数
 * @param <T> 配置类型
 * @return 结果
 */
public static <T extends Verifier> CircuitBreakResult buildCircuitBreakResult(StateMachine<T> stateMachine, Collection<Instance> instances, StateMachine.Parameter parameter) {
    long curTimeMs = System.currentTimeMillis();
    CircuitBreakResult result = new CircuitBreakResult(curTimeMs, parameter.getHalfOpenMaxReqCount());
    String cbName = parameter.getCircuitBreakerName();
    for (Instance instance : instances) {
        Set<StatusDimension> statusDimensions = stateMachine.getStatusDimensions(instance, parameter);
        if (CollectionUtils.isEmpty(statusDimensions)) {
            continue;
        }
        String instanceId = instance.getId();
        for (StatusDimension statusDimension : statusDimensions) {
            if (stateMachine.closeToOpen(instance, statusDimension, parameter)) {
                result.getInstancesToOpen().put(new ResultKey(instanceId, statusDimension), instance);
                LOG.info("circuitBreaker: instance {} and dimension {} changed from close to open by {}", instanceId, statusDimension, cbName);
                continue;
            }
            if (stateMachine.openToHalfOpen(instance, statusDimension, parameter)) {
                result.getInstancesToHalfOpen().put(new ResultKey(instanceId, statusDimension), instance);
                LOG.info("circuitBreaker: instance {} and dimension {} changed from open to halfOpen by {}", instanceId, statusDimension, cbName);
                continue;
            }
            if (stateMachine.halfOpenToOpen(instance, statusDimension, parameter)) {
                result.getInstancesToOpen().put(new ResultKey(instanceId, statusDimension), instance);
                LOG.info("circuitBreaker: instance {} and dimension {} changed from halfOpen to open by {}", instanceId, statusDimension, cbName);
                continue;
            }
            if (stateMachine.halfOpenToClose(instance, statusDimension, parameter)) {
                result.getInstancesToClose().put(new ResultKey(instanceId, statusDimension), instance);
                LOG.info("circuitBreaker: instance {} and dimension {} changed from halfOpen to close by {}", instanceId, statusDimension, cbName);
            }
        }
    }
    return result;
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance) StatusDimension(com.tencent.polaris.api.pojo.StatusDimension) CircuitBreakResult(com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult) ResultKey(com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult.ResultKey)

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