Search in sources :

Example 1 with ConfigSet

use of com.tencent.polaris.plugins.circuitbreaker.common.ConfigSet 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 2 with ConfigSet

use of com.tencent.polaris.plugins.circuitbreaker.common.ConfigSet in project polaris-java by polarismesh.

the class ConsecutiveCircuitBreaker method init.

@Override
public void init(InitContext ctx) throws PolarisException {
    CircuitBreakerConfig circuitBreakerConfig = ctx.getConfig().getConsumer().getCircuitBreaker();
    OutlierDetectionConfig outlierDetection = ctx.getConfig().getConsumer().getOutlierDetection();
    HalfOpenConfig halfOpenConfig = new HalfOpenConfig(circuitBreakerConfig, outlierDetection);
    Config cfg = circuitBreakerConfig.getPluginConfig(getName(), Config.class);
    if (cfg == null) {
        throw new PolarisException(ErrorCode.INVALID_CONFIG, String.format("plugin %s config is missing", getName()));
    }
    ConfigSet<Config> configSet = new ConfigSet<>(StatusDimension.Level.SERVICE, false, halfOpenConfig, cfg);
    configGroup = new ConfigGroup<>(configSet);
    stateMachine = new StateMachineImpl(configGroup, id, this);
    flowControlParam = new DefaultFlowControlParam(ctx.getConfig().getGlobal().getAPI());
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) OutlierDetectionConfig(com.tencent.polaris.api.config.consumer.OutlierDetectionConfig) DefaultFlowControlParam(com.tencent.polaris.client.flow.DefaultFlowControlParam) HalfOpenConfig(com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenConfig) ConsecutiveErrConfig(com.tencent.polaris.client.pb.CircuitBreakerProto.CbPolicy.ConsecutiveErrConfig) OutlierDetectionConfig(com.tencent.polaris.api.config.consumer.OutlierDetectionConfig) RecoverConfig(com.tencent.polaris.client.pb.CircuitBreakerProto.RecoverConfig) CircuitBreakerConfig(com.tencent.polaris.api.config.consumer.CircuitBreakerConfig) HalfOpenConfig(com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenConfig) CircuitBreakerConfig(com.tencent.polaris.api.config.consumer.CircuitBreakerConfig) ConfigSet(com.tencent.polaris.plugins.circuitbreaker.common.ConfigSet)

Example 3 with ConfigSet

use of com.tencent.polaris.plugins.circuitbreaker.common.ConfigSet in project polaris-java by polarismesh.

the class ErrRateCircuitBreaker method init.

@Override
public void init(InitContext ctx) throws PolarisException {
    CircuitBreakerConfig circuitBreakerConfig = ctx.getConfig().getConsumer().getCircuitBreaker();
    OutlierDetectionConfig outlierDetection = ctx.getConfig().getConsumer().getOutlierDetection();
    metricWindowMs = circuitBreakerConfig.getCheckPeriod();
    HalfOpenConfig halfOpenConfig = new HalfOpenConfig(circuitBreakerConfig, outlierDetection);
    Config cfg = circuitBreakerConfig.getPluginConfig(getName(), Config.class);
    if (cfg == null) {
        throw new PolarisException(ErrorCode.INVALID_CONFIG, String.format("plugin %s config is missing", getName()));
    }
    ConfigSet<Config> configSet = new ConfigSet<>(StatusDimension.Level.SERVICE, false, halfOpenConfig, cfg);
    create = new Function<Integer, Object>() {

        @Override
        public Object apply(Integer integer) {
            return new ErrRateCounter(metricWindowName, configSet.getPlugConfig(), getBucketIntervalMs());
        }
    };
    configGroup = new ConfigGroup<>(configSet);
    stateMachine = new StateMachineImpl(configGroup, id, this, metricWindowMs);
    flowControlParam = new DefaultFlowControlParam(ctx.getConfig().getGlobal().getAPI());
}
Also used : OutlierDetectionConfig(com.tencent.polaris.api.config.consumer.OutlierDetectionConfig) HalfOpenConfig(com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenConfig) 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) PolarisException(com.tencent.polaris.api.exception.PolarisException) DefaultFlowControlParam(com.tencent.polaris.client.flow.DefaultFlowControlParam) CircuitBreakerConfig(com.tencent.polaris.api.config.consumer.CircuitBreakerConfig) ConfigSet(com.tencent.polaris.plugins.circuitbreaker.common.ConfigSet)

Aggregations

CircuitBreakerConfig (com.tencent.polaris.api.config.consumer.CircuitBreakerConfig)3 OutlierDetectionConfig (com.tencent.polaris.api.config.consumer.OutlierDetectionConfig)3 PolarisException (com.tencent.polaris.api.exception.PolarisException)3 DefaultFlowControlParam (com.tencent.polaris.client.flow.DefaultFlowControlParam)3 RecoverConfig (com.tencent.polaris.client.pb.CircuitBreakerProto.RecoverConfig)3 ConfigSet (com.tencent.polaris.plugins.circuitbreaker.common.ConfigSet)3 HalfOpenConfig (com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenConfig)3 ErrRateConfig (com.tencent.polaris.client.pb.CircuitBreakerProto.CbPolicy.ErrRateConfig)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 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 InitContext (com.tencent.polaris.api.plugin.common.InitContext)1 PluginTypes (com.tencent.polaris.api.plugin.common.PluginTypes)1 Extensions (com.tencent.polaris.api.plugin.compose.Extensions)1 LocalRegistry (com.tencent.polaris.api.plugin.registry.LocalRegistry)1