-
Notifications
You must be signed in to change notification settings - Fork 8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature]finish cluster concurrent flow control rule checker #1631
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2926e28
add cluster concurrent flow control param into flow rule
yunfeiyanggzq f7d9354
add cluster concurrent flow control interface into TokenService
yunfeiyanggzq 5c496af
finish concurrent flow control checker
yunfeiyanggzq 1f0ce3b
unit tests for cluster concurrent flow control checker
yunfeiyanggzq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...ult/src/main/java/com/alibaba/csp/sentinel/cluster/flow/ConcurrentClusterFlowChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.csp.sentinel.cluster.flow; | ||
|
||
import com.alibaba.csp.sentinel.cluster.TokenResult; | ||
import com.alibaba.csp.sentinel.cluster.TokenResultStatus; | ||
import com.alibaba.csp.sentinel.cluster.flow.rule.ClusterFlowRuleManager; | ||
import com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent.CurrentConcurrencyManager; | ||
import com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent.TokenCacheNode; | ||
import com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent.TokenCacheNodeManager; | ||
import com.alibaba.csp.sentinel.cluster.server.log.ClusterServerStatLogUtil; | ||
import com.alibaba.csp.sentinel.log.RecordLog; | ||
import com.alibaba.csp.sentinel.slots.block.ClusterRuleConstant; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* @author yunfeiyanggzq | ||
*/ | ||
final public class ConcurrentClusterFlowChecker { | ||
|
||
public static double calcGlobalThreshold(FlowRule rule) { | ||
double count = rule.getCount(); | ||
switch (rule.getClusterConfig().getThresholdType()) { | ||
case ClusterRuleConstant.FLOW_THRESHOLD_GLOBAL: | ||
return count; | ||
case ClusterRuleConstant.FLOW_THRESHOLD_AVG_LOCAL: | ||
default: | ||
int connectedCount = ClusterFlowRuleManager.getConnectedCount(rule.getClusterConfig().getFlowId()); | ||
return count * connectedCount; | ||
} | ||
} | ||
|
||
public static TokenResult acquireConcurrentToken(/*@Valid*/ String clientAddress, FlowRule rule, int acquireCount) { | ||
long flowId = rule.getClusterConfig().getFlowId(); | ||
AtomicInteger nowCalls = CurrentConcurrencyManager.get(flowId); | ||
if (nowCalls == null) { | ||
RecordLog.warn("[ConcurrentClusterFlowChecker] Fail to get nowCalls by flowId<{}>", flowId); | ||
return new TokenResult(TokenResultStatus.FAIL); | ||
} | ||
|
||
// check before enter the lock to improve the efficiency | ||
if (nowCalls.get() + acquireCount > calcGlobalThreshold(rule)) { | ||
ClusterServerStatLogUtil.log("concurrent|block|" + flowId, acquireCount); | ||
return new TokenResult(TokenResultStatus.BLOCKED); | ||
} | ||
|
||
// ensure the atomicity of operations | ||
// lock different nowCalls to improve the efficiency | ||
synchronized (nowCalls) { | ||
// check again whether the request can pass. | ||
if (nowCalls.get() + acquireCount > calcGlobalThreshold(rule)) { | ||
ClusterServerStatLogUtil.log("concurrent|block|" + flowId, acquireCount); | ||
return new TokenResult(TokenResultStatus.BLOCKED); | ||
} else { | ||
nowCalls.getAndAdd(acquireCount); | ||
} | ||
} | ||
ClusterServerStatLogUtil.log("concurrent|pass|" + flowId, acquireCount); | ||
TokenCacheNode node = TokenCacheNode.generateTokenCacheNode(rule, acquireCount, clientAddress); | ||
TokenCacheNodeManager.putTokenCacheNode(node.getTokenId(), node); | ||
TokenResult tokenResult = new TokenResult(TokenResultStatus.OK); | ||
tokenResult.setTokenId(node.getTokenId()); | ||
return tokenResult; | ||
} | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may need to pay attention to the memory and GC footprint when there are large amount of requests coming.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the max memory used is decided by rules*maxConcurrency, I think memory used is controllable.every token information is small and the token can be replaced fastly during the process of acquiring and releasing, there may be some young gc in the case you mention, I think it can be acceptable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may provide benchmark of the scenario. Frequent YGC may need optimizing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will do it, Thanks !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the process is too short, so the image is not statistically accurate.
the code is as follows:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
每秒请求量越小gc越少