Java Code Examples for com.alibaba.csp.sentinel.slotchain.ResourceWrapper#getName()

The following examples show how to use com.alibaba.csp.sentinel.slotchain.ResourceWrapper#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GatewayFlowSlot.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
private void checkGatewayParamFlow(ResourceWrapper resourceWrapper, int count, Object... args)
    throws BlockException {
    if (args == null) {
        return;
    }

    List<ParamFlowRule> rules = GatewayRuleManager.getConvertedParamRules(resourceWrapper.getName());
    if (rules == null || rules.isEmpty()) {
        return;
    }

    for (ParamFlowRule rule : rules) {
        // Initialize the parameter metrics.
        ParameterMetricStorage.initParamMetricsFor(resourceWrapper, rule);

        if (!ParamFlowChecker.passCheck(resourceWrapper, rule, count, args)) {
            String triggeredParam = "";
            if (args.length > rule.getParamIdx()) {
                Object value = args[rule.getParamIdx()];
                triggeredParam = String.valueOf(value);
            }
            throw new ParamFlowException(resourceWrapper.getName(), triggeredParam, rule);
        }
    }
}
 
Example 2
Source File: ParamFlowSlot.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
void checkFlow(ResourceWrapper resourceWrapper, int count, Object... args) throws BlockException {
    if (args == null) {
        return;
    }
    if (!ParamFlowRuleManager.hasRules(resourceWrapper.getName())) {
        return;
    }
    List<ParamFlowRule> rules = ParamFlowRuleManager.getRulesOfResource(resourceWrapper.getName());

    for (ParamFlowRule rule : rules) {
        applyRealParamIdx(rule, args.length);

        // Initialize the parameter metrics.
        ParameterMetricStorage.initParamMetricsFor(resourceWrapper, rule);

        if (!ParamFlowChecker.passCheck(resourceWrapper, rule, count, args)) {
            String triggeredParam = "";
            if (args.length > rule.getParamIdx()) {
                Object value = args[rule.getParamIdx()];
                triggeredParam = String.valueOf(value);
            }
            throw new ParamFlowException(resourceWrapper.getName(), triggeredParam, rule);
        }
    }
}
 
Example 3
Source File: ParameterMetricStorage.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
/**
 * Init the parameter metric and index map for given resource.
 * Package-private for test.
 *
 * @param resourceWrapper resource to init
 * @param rule            relevant rule
 */
public static void initParamMetricsFor(ResourceWrapper resourceWrapper, /*@Valid*/ ParamFlowRule rule) {
    if (resourceWrapper == null || resourceWrapper.getName() == null) {
        return;
    }
    String resourceName = resourceWrapper.getName();
    ParameterMetric metric;
    // Assume that the resource is valid.
    if ((metric = metricsMap.get(resourceName)) == null) {
        synchronized (LOCK) {
            if ((metric = metricsMap.get(resourceName)) == null) {
                metric = new ParameterMetric();
                metricsMap.put(resourceWrapper.getName(), metric);
                RecordLog.info("[ParameterMetricStorage] Creating parameter metric for: " + resourceWrapper.getName());
            }
        }
    }
    metric.initialize(rule);
}
 
Example 4
Source File: GatewayFlowSlot.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private void checkGatewayParamFlow(ResourceWrapper resourceWrapper, int count, Object... args)
    throws BlockException {
    if (args == null) {
        return;
    }

    List<ParamFlowRule> rules = GatewayRuleManager.getConvertedParamRules(resourceWrapper.getName());
    if (rules == null || rules.isEmpty()) {
        return;
    }

    for (ParamFlowRule rule : rules) {
        // Initialize the parameter metrics.
        ParameterMetricStorage.initParamMetricsFor(resourceWrapper, rule);

        if (!ParamFlowChecker.passCheck(resourceWrapper, rule, count, args)) {
            String triggeredParam = "";
            if (args.length > rule.getParamIdx()) {
                Object value = args[rule.getParamIdx()];
                triggeredParam = String.valueOf(value);
            }
            throw new ParamFlowException(resourceWrapper.getName(), triggeredParam, rule);
        }
    }
}
 
Example 5
Source File: ParamFlowSlot.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
void checkFlow(ResourceWrapper resourceWrapper, int count, Object... args) throws BlockException {
    if (args == null) {
        return;
    }
    if (!ParamFlowRuleManager.hasRules(resourceWrapper.getName())) {
        return;
    }
    List<ParamFlowRule> rules = ParamFlowRuleManager.getRulesOfResource(resourceWrapper.getName());

    for (ParamFlowRule rule : rules) {
        applyRealParamIdx(rule, args.length);

        // Initialize the parameter metrics.
        ParameterMetricStorage.initParamMetricsFor(resourceWrapper, rule);

        if (!ParamFlowChecker.passCheck(resourceWrapper, rule, count, args)) {
            String triggeredParam = "";
            if (args.length > rule.getParamIdx()) {
                Object value = args[rule.getParamIdx()];
                triggeredParam = String.valueOf(value);
            }
            throw new ParamFlowException(resourceWrapper.getName(), triggeredParam, rule);
        }
    }
}
 
Example 6
Source File: ParameterMetricStorage.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
/**
 * Init the parameter metric and index map for given resource.
 * Package-private for test.
 *
 * @param resourceWrapper resource to init
 * @param rule            relevant rule
 */
public static void initParamMetricsFor(ResourceWrapper resourceWrapper, /*@Valid*/ ParamFlowRule rule) {
    if (resourceWrapper == null || resourceWrapper.getName() == null) {
        return;
    }
    String resourceName = resourceWrapper.getName();
    ParameterMetric metric;
    // Assume that the resource is valid.
    if ((metric = metricsMap.get(resourceName)) == null) {
        synchronized (LOCK) {
            if ((metric = metricsMap.get(resourceName)) == null) {
                metric = new ParameterMetric();
                metricsMap.put(resourceWrapper.getName(), metric);
                RecordLog.info("[ParameterMetricStorage] Creating parameter metric for: " + resourceWrapper.getName());
            }
        }
    }
    metric.initialize(rule);
}
 
Example 7
Source File: MetricExitCallback.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Override
public void onExit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
    for (MetricExtension m : MetricExtensionProvider.getMetricExtensions()) {
        if (context.getCurEntry().getBlockError() != null) {
            continue;
        }
        String resource = resourceWrapper.getName();
        long realRt = TimeUtil.currentTimeMillis() - context.getCurEntry().getCreateTimestamp();
        m.addRt(resource, realRt, args);
        m.addSuccess(resource, count, args);
        m.decreaseThreadNum(resource, args);

        Throwable ex = context.getCurEntry().getError();
        if (ex != null) {
            m.addException(resource, count, ex);
        }
    }
}
 
Example 8
Source File: ParameterMetricStorage.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
public static ParameterMetric getParamMetric(ResourceWrapper resourceWrapper) {
    if (resourceWrapper == null || resourceWrapper.getName() == null) {
        return null;
    }
    return metricsMap.get(resourceWrapper.getName());
}
 
Example 9
Source File: SystemRuleManager.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
/**
 * Apply {@link SystemRule} to the resource. Only inbound traffic will be checked.
 *
 * @param resourceWrapper the resource.
 * @throws BlockException when any system rule's threshold is exceeded.
 */
public static void checkSystem(ResourceWrapper resourceWrapper) throws BlockException {
    // Ensure the checking switch is on.
    if (!checkSystemStatus.get()) {
        return;
    }

    // for inbound traffic only
    if (resourceWrapper.getType() != EntryType.IN) {
        return;
    }

    // total qps
    double currentQps = Constants.ENTRY_NODE == null ? 0.0 : Constants.ENTRY_NODE.successQps();
    if (currentQps > qps) {
        throw new SystemBlockException(resourceWrapper.getName(), "qps");
    }

    // total thread
    int currentThread = Constants.ENTRY_NODE == null ? 0 : Constants.ENTRY_NODE.curThreadNum();
    if (currentThread > maxThread) {
        throw new SystemBlockException(resourceWrapper.getName(), "thread");
    }

    double rt = Constants.ENTRY_NODE == null ? 0 : Constants.ENTRY_NODE.avgRt();
    if (rt > maxRt) {
        throw new SystemBlockException(resourceWrapper.getName(), "rt");
    }

    // load. BBR algorithm.
    if (highestSystemLoadIsSet && getCurrentSystemAvgLoad() > highestSystemLoad) {
        if (!checkBbr(currentThread)) {
            throw new SystemBlockException(resourceWrapper.getName(), "load");
        }
    }

    // cpu usage
    if (highestCpuUsageIsSet && getCurrentCpuUsage() > highestCpuUsage) {
        if (!checkBbr(currentThread)) {
            throw new SystemBlockException(resourceWrapper.getName(), "cpu");
        }
    }
}
 
Example 10
Source File: ParameterMetricStorage.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
public static ParameterMetric getParamMetric(ResourceWrapper resourceWrapper) {
    if (resourceWrapper == null || resourceWrapper.getName() == null) {
        return null;
    }
    return metricsMap.get(resourceWrapper.getName());
}
 
Example 11
Source File: SystemRuleManager.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
/**
 * Apply {@link SystemRule} to the resource. Only inbound traffic will be checked.
 *
 * @param resourceWrapper the resource.
 * @throws BlockException when any system rule's threshold is exceeded.
 */
public static void checkSystem(ResourceWrapper resourceWrapper) throws BlockException {
    if (resourceWrapper == null) {
        return;
    }
    // Ensure the checking switch is on.
    if (!checkSystemStatus.get()) {
        return;
    }

    // for inbound traffic only
    if (resourceWrapper.getEntryType() != EntryType.IN) {
        return;
    }

    // total qps
    double currentQps = Constants.ENTRY_NODE == null ? 0.0 : Constants.ENTRY_NODE.successQps();
    if (currentQps > qps) {
        throw new SystemBlockException(resourceWrapper.getName(), "qps");
    }

    // total thread
    int currentThread = Constants.ENTRY_NODE == null ? 0 : Constants.ENTRY_NODE.curThreadNum();
    if (currentThread > maxThread) {
        throw new SystemBlockException(resourceWrapper.getName(), "thread");
    }

    double rt = Constants.ENTRY_NODE == null ? 0 : Constants.ENTRY_NODE.avgRt();
    if (rt > maxRt) {
        throw new SystemBlockException(resourceWrapper.getName(), "rt");
    }

    // load. BBR algorithm.
    if (highestSystemLoadIsSet && getCurrentSystemAvgLoad() > highestSystemLoad) {
        if (!checkBbr(currentThread)) {
            throw new SystemBlockException(resourceWrapper.getName(), "load");
        }
    }

    // cpu usage
    if (highestCpuUsageIsSet && getCurrentCpuUsage() > highestCpuUsage) {
        throw new SystemBlockException(resourceWrapper.getName(), "cpu");
    }
}