Search in sources :

Example 21 with TokenResult

use of com.alibaba.csp.sentinel.cluster.TokenResult in project Sentinel by alibaba.

the class ConcurrentClusterFlowChecker method releaseConcurrentToken.

public static TokenResult releaseConcurrentToken(/*@Valid*/
long tokenId) {
    TokenCacheNode node = TokenCacheNodeManager.getTokenCacheNode(tokenId);
    if (node == null) {
        RecordLog.info("[ConcurrentClusterFlowChecker] Token<{}> is already released", tokenId);
        return new TokenResult(TokenResultStatus.ALREADY_RELEASE);
    }
    FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(node.getFlowId());
    if (rule == null) {
        RecordLog.info("[ConcurrentClusterFlowChecker] Fail to get rule by flowId<{}>", node.getFlowId());
        return new TokenResult(TokenResultStatus.NO_RULE_EXISTS);
    }
    if (TokenCacheNodeManager.removeTokenCacheNode(tokenId) == null) {
        RecordLog.info("[ConcurrentClusterFlowChecker] Token<{}> is already released for flowId<{}>", tokenId, node.getFlowId());
        return new TokenResult(TokenResultStatus.ALREADY_RELEASE);
    }
    int acquireCount = node.getAcquireCount();
    AtomicInteger nowCalls = CurrentConcurrencyManager.get(node.getFlowId());
    nowCalls.getAndAdd(-1 * acquireCount);
    ClusterServerStatLogUtil.log("concurrent|release|" + rule.getClusterConfig().getFlowId(), acquireCount);
    return new TokenResult(TokenResultStatus.RELEASE_OK);
}
Also used : TokenCacheNode(com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent.TokenCacheNode) TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule)

Example 22 with TokenResult

use of com.alibaba.csp.sentinel.cluster.TokenResult in project Sentinel by alibaba.

the class ParamFlowChecker method passClusterCheck.

private static boolean passClusterCheck(ResourceWrapper resourceWrapper, ParamFlowRule rule, int count, Object value) {
    try {
        Collection<Object> params = toCollection(value);
        TokenService clusterService = pickClusterService();
        if (clusterService == null) {
            // pass in need.
            return fallbackToLocalOrPass(resourceWrapper, rule, count, params);
        }
        TokenResult result = clusterService.requestParamToken(rule.getClusterConfig().getFlowId(), count, params);
        switch(result.getStatus()) {
            case TokenResultStatus.OK:
                return true;
            case TokenResultStatus.BLOCKED:
                return false;
            default:
                return fallbackToLocalOrPass(resourceWrapper, rule, count, params);
        }
    } catch (Throwable ex) {
        RecordLog.warn("[ParamFlowChecker] Request cluster token for parameter unexpected failed", ex);
        return fallbackToLocalOrPass(resourceWrapper, rule, count, value);
    }
}
Also used : TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) TokenService(com.alibaba.csp.sentinel.cluster.TokenService)

Aggregations

TokenResult (com.alibaba.csp.sentinel.cluster.TokenResult)22 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)11 Test (org.junit.Test)6 TokenService (com.alibaba.csp.sentinel.cluster.TokenService)4 RateLimitDescriptor (io.envoyproxy.envoy.api.v2.ratelimit.RateLimitDescriptor)3 RateLimitDescriptor (io.envoyproxy.envoy.extensions.common.ratelimit.v3.RateLimitDescriptor)3 RateLimitResponse (io.envoyproxy.envoy.service.ratelimit.v2.RateLimitResponse)3 RateLimitResponse (io.envoyproxy.envoy.service.ratelimit.v3.RateLimitResponse)3 ArrayList (java.util.ArrayList)3 TokenResultStatus (com.alibaba.csp.sentinel.cluster.TokenResultStatus)2 TokenCacheNode (com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent.TokenCacheNode)2 ClusterMetric (com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterMetric)2 ClusterRequest (com.alibaba.csp.sentinel.cluster.request.ClusterRequest)2 ParamFlowRequestData (com.alibaba.csp.sentinel.cluster.request.data.ParamFlowRequestData)2 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)2 Tuple2 (com.alibaba.csp.sentinel.util.function.Tuple2)2 RateLimitRequest (io.envoyproxy.envoy.service.ratelimit.v2.RateLimitRequest)2 Code (io.envoyproxy.envoy.service.ratelimit.v2.RateLimitResponse.Code)2 RateLimitRequest (io.envoyproxy.envoy.service.ratelimit.v3.RateLimitRequest)2 StreamObserver (io.grpc.stub.StreamObserver)2