Search in sources :

Example 1 with InstanceByProto

use of com.tencent.polaris.client.pojo.InstanceByProto in project polaris-java by polarismesh.

the class InMemoryRegistry method updateInstances.

@Override
public void updateInstances(ServiceUpdateRequest request) {
    Collection<InstanceProperty> instanceProperties = request.getProperties();
    if (CollectionUtils.isEmpty(instanceProperties)) {
        return;
    }
    RegistryCacheValue cacheValue = getResource(new ServiceEventKey(request.getServiceKey(), EventType.INSTANCE), true, true);
    if (null == cacheValue) {
        // 服务不存在,忽略
        return;
    }
    for (InstanceProperty instanceProperty : instanceProperties) {
        InstanceByProto instance = (InstanceByProto) instanceProperty.getInstance();
        InstanceLocalValue instanceLocalValue = instance.getInstanceLocalValue();
        Map<String, Object> properties = instanceProperty.getProperties();
        LOG.info("update instance properties for instance {}, properties {}", instance.getId(), properties);
        for (Map.Entry<String, Object> entry : properties.entrySet()) {
            switch(entry.getKey()) {
                case InstanceProperty.PROPERTY_CIRCUIT_BREAKER_STATUS:
                    onCircuitBreakStatus(entry.getValue(), instanceLocalValue, instance);
                    break;
                case InstanceProperty.PROPERTY_DETECT_RESULT:
                    instanceLocalValue.setDetectResult((DetectResult) entry.getValue());
                    break;
                default:
                    break;
            }
        }
    }
}
Also used : InstanceByProto(com.tencent.polaris.client.pojo.InstanceByProto) RegistryCacheValue(com.tencent.polaris.api.pojo.RegistryCacheValue) InstanceLocalValue(com.tencent.polaris.api.pojo.InstanceLocalValue) InstanceProperty(com.tencent.polaris.api.plugin.registry.InstanceProperty) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 2 with InstanceByProto

use of com.tencent.polaris.client.pojo.InstanceByProto 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 3 with InstanceByProto

use of com.tencent.polaris.client.pojo.InstanceByProto in project polaris-java by polarismesh.

the class AbstractStateMachine method openToHalfOpen.

@Override
public boolean openToHalfOpen(Instance instance, StatusDimension statusDimension, Parameter parameter) {
    String cbName = parameter.getCircuitBreakerName();
    CircuitBreakerStatus cbStatus = instance.getCircuitBreakerStatus(statusDimension);
    if (null == cbStatus || !cbStatus.getCircuitBreaker().equals(cbName) || cbStatus.getStatus() != CircuitBreakerStatus.Status.OPEN) {
        return false;
    }
    boolean detectSuccess = false;
    if (instance instanceof InstanceByProto) {
        DetectResult detectResult = ((InstanceByProto) instance).getDetectResult();
        detectSuccess = null != detectResult && detectResult.getRetStatus() == RetStatus.RetSuccess;
    }
    // 清空halfOpen的计数器
    HalfOpenCounter halfOpenCounter = getHalfOpenCounter(parameter.getPluginId(), instance);
    if (null == halfOpenCounter) {
        return false;
    }
    long startTimeMs = cbStatus.getStartTimeMs();
    HalfOpenConfig halfOpenConfig = getHalfOpenConfigOnHalfOpen(instance, statusDimension);
    boolean matched;
    if (detectSuccess && halfOpenConfig.getWhenToDetect() != When.never) {
        matched = true;
    } else {
        matched = parameter.getCurrentTimeMs() - startTimeMs >= halfOpenConfig.getSleepWindowMs();
    }
    if (matched) {
        // 时间窗已经过去, 则恢复半开
        halfOpenCounter.resetHalfOpen(statusDimension);
    }
    return matched;
}
Also used : InstanceByProto(com.tencent.polaris.client.pojo.InstanceByProto) DetectResult(com.tencent.polaris.api.pojo.DetectResult) CircuitBreakerStatus(com.tencent.polaris.api.pojo.CircuitBreakerStatus)

Example 4 with InstanceByProto

use of com.tencent.polaris.client.pojo.InstanceByProto in project polaris-java by polarismesh.

the class AbstractStateMachine method getHalfOpenCounter.

protected HalfOpenCounter getHalfOpenCounter(int pluginId, Instance instance) {
    InstanceByProto instanceByProto = (InstanceByProto) instance;
    InstanceLocalValue instanceLocalValue = instanceByProto.getInstanceLocalValue();
    if (null == instanceLocalValue) {
        return null;
    }
    Object pluginValue = instanceLocalValue.getPluginValue(pluginId, null);
    if (null == pluginValue) {
        return null;
    }
    return (HalfOpenCounter) pluginValue;
}
Also used : InstanceByProto(com.tencent.polaris.client.pojo.InstanceByProto) InstanceLocalValue(com.tencent.polaris.api.pojo.InstanceLocalValue)

Example 5 with InstanceByProto

use of com.tencent.polaris.client.pojo.InstanceByProto 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)

Aggregations

InstanceByProto (com.tencent.polaris.client.pojo.InstanceByProto)6 InstanceLocalValue (com.tencent.polaris.api.pojo.InstanceLocalValue)4 CircuitBreakerConfig (com.tencent.polaris.api.config.consumer.CircuitBreakerConfig)2 OutlierDetectionConfig (com.tencent.polaris.api.config.consumer.OutlierDetectionConfig)2 Instance (com.tencent.polaris.api.pojo.Instance)2 RetStatus (com.tencent.polaris.api.pojo.RetStatus)2 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)2 StatusDimension (com.tencent.polaris.api.pojo.StatusDimension)2 RecoverConfig (com.tencent.polaris.client.pb.CircuitBreakerProto.RecoverConfig)2 HalfOpenConfig (com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenConfig)2 HalfOpenCounter (com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenCounter)2 DefaultPlugins (com.tencent.polaris.api.config.plugin.DefaultPlugins)1 PluginConfigProvider (com.tencent.polaris.api.config.plugin.PluginConfigProvider)1 Verifier (com.tencent.polaris.api.config.verify.Verifier)1 Destroyable (com.tencent.polaris.api.control.Destroyable)1 ErrorCode (com.tencent.polaris.api.exception.ErrorCode)1 PolarisException (com.tencent.polaris.api.exception.PolarisException)1 PluginType (com.tencent.polaris.api.plugin.PluginType)1 CircuitBreakResult (com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult)1 CircuitBreaker (com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreaker)1