-
Notifications
You must be signed in to change notification settings - Fork 8.2k
customcontrol-add behavior limits and conflict warning logs with new unit tests #3567
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
Open
z-soulx
wants to merge
2
commits into
alibaba:1.8
Choose a base branch
from
z-soulx:1.8_customcontrol
base: 1.8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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
40 changes: 40 additions & 0 deletions
40
...ava/com/alibaba/csp/sentinel/slots/block/flow/LoggingTrafficShapingControllerFactory.java
This file contains hidden or 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,40 @@ | ||
| /* | ||
| * Copyright 1999-2024 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 | ||
| * | ||
| * https://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.slots.block.flow; | ||
|
|
||
| import com.alibaba.csp.sentinel.log.RecordLog; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class LoggingTrafficShapingControllerFactory implements TrafficShapingControllerFactory { | ||
|
|
||
| private final TrafficShapingControllerFactory delegate; | ||
|
|
||
| public LoggingTrafficShapingControllerFactory(TrafficShapingControllerFactory delegate) { | ||
| this.delegate = Objects.requireNonNull(delegate, "delegate must be not null."); | ||
| } | ||
|
|
||
| @Override | ||
| public TrafficShapingController create(FlowRule rule) { | ||
| RecordLog.debug("Creating traffic shaping controller '" + delegate.getClass().getName() + "' for rule: " + rule); | ||
| return delegate.create(rule); | ||
| } | ||
|
|
||
| @Override | ||
| public int getControlBehavior() { | ||
| return delegate.getControlBehavior(); | ||
| } | ||
| } |
101 changes: 101 additions & 0 deletions
101
...ain/java/com/alibaba/csp/sentinel/slots/block/flow/TrafficShapingControllerFactories.java
This file contains hidden or 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,101 @@ | ||
| /* | ||
| * Copyright 1999-2024 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 | ||
| * | ||
| * https://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.slots.block.flow; | ||
|
|
||
| import com.alibaba.csp.sentinel.log.RecordLog; | ||
| import com.alibaba.csp.sentinel.slots.block.RuleConstant; | ||
| import com.alibaba.csp.sentinel.spi.SpiLoader; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.BinaryOperator; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class TrafficShapingControllerFactories { | ||
|
|
||
| private static final Map<Integer, TrafficShapingControllerFactory> FACTORIES; | ||
|
|
||
| static { | ||
| FACTORIES = initFactories(); | ||
| } | ||
|
|
||
| /** | ||
| * Using existing factory if the factory with the same control behavior already exists, because the existing factory has higher priority | ||
| */ | ||
| private static BinaryOperator<TrafficShapingControllerFactory> usingExisting() { | ||
| return (existing, replacement) -> { | ||
| RecordLog.warn("[TrafficShapingControllerFactories] Duplicate control behavior [{}], " + | ||
| "using existing factory [{}], ignoring [{}]", | ||
| existing.getControlBehavior(), | ||
| existing.getClass().getName(), | ||
| replacement.getClass().getName()); | ||
| return existing; | ||
| }; | ||
| } | ||
|
|
||
| private static TrafficShapingControllerFactory logging(TrafficShapingControllerFactory factory) { | ||
| return new LoggingTrafficShapingControllerFactory(factory); | ||
| } | ||
|
|
||
| /** | ||
| * Validates the control behavior namespace of a factory. | ||
| */ | ||
| private static void validateControlBehavior(TrafficShapingControllerFactory factory) { | ||
| int controlBehavior = factory.getControlBehavior(); | ||
|
|
||
| // User-defined factories must not use reserved range [0, 255] | ||
| if (!factory.isBuiltIn() && isReservedControlBehavior(controlBehavior)) { | ||
| throw new IllegalArgumentException(String.format( | ||
| "Invalid control behavior [%d] for factory [%s]. " + | ||
| "Control behavior values in range [0, %d] are reserved for Sentinel built-in implementations. " + | ||
| "User-defined factories must use values >= %d to ensure compatibility with future Sentinel upgrades.", | ||
| controlBehavior, | ||
| factory.getClass().getName(), | ||
| RuleConstant.CONTROL_BEHAVIOR_USER_DEFINED_MIN - 1, | ||
| RuleConstant.CONTROL_BEHAVIOR_USER_DEFINED_MIN)); | ||
| } | ||
|
|
||
| RecordLog.info("[TrafficShapingControllerFactories] Registered factory [{}] for control behavior [{}]", | ||
| factory.getClass().getName(), controlBehavior); | ||
| } | ||
|
|
||
| private static Map<Integer, TrafficShapingControllerFactory> initFactories() { | ||
| List<TrafficShapingControllerFactory> factories = SpiLoader.of(TrafficShapingControllerFactory.class) | ||
| .loadInstanceListSorted(); | ||
| // Validate all factories | ||
| for (TrafficShapingControllerFactory factory : factories) { | ||
| validateControlBehavior(factory); | ||
| } | ||
| return factories.stream() | ||
| .collect(Collectors.toMap( | ||
| TrafficShapingControllerFactory::getControlBehavior, | ||
| TrafficShapingControllerFactories::logging, | ||
| usingExisting(), | ||
| HashMap::new)); | ||
| } | ||
|
|
||
| public static TrafficShapingControllerFactory get(int controlBehavior) { | ||
| return FACTORIES.get(controlBehavior); | ||
| } | ||
|
|
||
| private TrafficShapingControllerFactories() { | ||
| } | ||
|
|
||
| public static boolean isReservedControlBehavior(int controlBehavior) { | ||
| return controlBehavior < RuleConstant.CONTROL_BEHAVIOR_USER_DEFINED_MIN; | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
.../main/java/com/alibaba/csp/sentinel/slots/block/flow/TrafficShapingControllerFactory.java
This file contains hidden or 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,49 @@ | ||
| /* | ||
| * Copyright 1999-2024 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 | ||
| * | ||
| * https://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.slots.block.flow; | ||
|
|
||
| /** | ||
| * a factory interface to create TrafficShapingController instance | ||
| */ | ||
| public interface TrafficShapingControllerFactory { | ||
|
|
||
| /** | ||
| * create a TrafficShapingController instance | ||
| * @param rule flow rule | ||
| * @return a new TrafficShapingController instance | ||
| */ | ||
| TrafficShapingController create(FlowRule rule); | ||
|
|
||
| /** | ||
| * get the factory control behavior | ||
| * @return the control behavior | ||
| */ | ||
| int getControlBehavior(); | ||
|
|
||
| /** | ||
| * Indicates whether this factory is a built-in Sentinel implementation. | ||
| * Built-in factories are allowed to use control behavior values in the reserved range [0, 255]. | ||
| * User-defined factories should return {@code false} (default) and use values >= 256. | ||
| * | ||
| * This method is used internally for validation during factory registration to ensure | ||
| * proper namespace separation and prevent conflicts. | ||
| * | ||
| * @return {@code true} if this is a Sentinel built-in factory, {@code false} for user-defined implementations | ||
| */ | ||
| default boolean isBuiltIn() { | ||
| return false; | ||
| } | ||
| } | ||
39 changes: 39 additions & 0 deletions
39
...n/java/com/alibaba/csp/sentinel/slots/block/flow/controller/DefaultControllerFactory.java
This file contains hidden or 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,39 @@ | ||
| /* | ||
| * Copyright 1999-2024 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 | ||
| * | ||
| * https://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.slots.block.flow.controller; | ||
|
|
||
| import com.alibaba.csp.sentinel.slots.block.RuleConstant; | ||
| import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; | ||
| import com.alibaba.csp.sentinel.slots.block.flow.TrafficShapingController; | ||
| import com.alibaba.csp.sentinel.slots.block.flow.TrafficShapingControllerFactory; | ||
|
|
||
| public class DefaultControllerFactory implements TrafficShapingControllerFactory { | ||
|
|
||
| @Override | ||
| public TrafficShapingController create(FlowRule rule) { | ||
| return new DefaultController(rule.getCount(), rule.getGrade()); | ||
| } | ||
|
|
||
| @Override | ||
| public int getControlBehavior() { | ||
| return RuleConstant.CONTROL_BEHAVIOR_DEFAULT; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isBuiltIn() { | ||
| return true; | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
...ava/com/alibaba/csp/sentinel/slots/block/flow/controller/ThrottlingControllerFactory.java
This file contains hidden or 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,39 @@ | ||
| /* | ||
| * Copyright 1999-2024 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 | ||
| * | ||
| * https://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.slots.block.flow.controller; | ||
|
|
||
| import com.alibaba.csp.sentinel.slots.block.RuleConstant; | ||
| import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; | ||
| import com.alibaba.csp.sentinel.slots.block.flow.TrafficShapingController; | ||
| import com.alibaba.csp.sentinel.slots.block.flow.TrafficShapingControllerFactory; | ||
|
|
||
| public class ThrottlingControllerFactory implements TrafficShapingControllerFactory { | ||
|
|
||
| @Override | ||
| public TrafficShapingController create(FlowRule rule) { | ||
| return new ThrottlingController(rule.getMaxQueueingTimeMs(), rule.getCount()); | ||
| } | ||
|
|
||
| @Override | ||
| public int getControlBehavior() { | ||
| return RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isBuiltIn() { | ||
| return true; | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
...in/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpControllerFactory.java
This file contains hidden or 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,42 @@ | ||
| /* | ||
| * Copyright 1999-2024 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 | ||
| * | ||
| * https://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.slots.block.flow.controller; | ||
|
|
||
| import com.alibaba.csp.sentinel.config.SentinelConfig; | ||
| import com.alibaba.csp.sentinel.slots.block.RuleConstant; | ||
| import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; | ||
| import com.alibaba.csp.sentinel.slots.block.flow.TrafficShapingController; | ||
| import com.alibaba.csp.sentinel.slots.block.flow.TrafficShapingControllerFactory; | ||
|
|
||
| public class WarmUpControllerFactory implements TrafficShapingControllerFactory { | ||
|
|
||
| static volatile int coldFactor = SentinelConfig.coldFactor(); | ||
|
|
||
| @Override | ||
| public TrafficShapingController create(FlowRule rule) { | ||
| return new WarmUpController(rule.getCount(), rule.getWarmUpPeriodSec(), coldFactor); | ||
| } | ||
|
|
||
| @Override | ||
| public int getControlBehavior() { | ||
| return RuleConstant.CONTROL_BEHAVIOR_WARM_UP; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isBuiltIn() { | ||
| return true; | ||
| } | ||
| } |
Oops, something went wrong.
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.
I think built in impl could be override and improve by user, it seems no need to limit
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 main point here is to give a light warning—keeping it flexible while still notifying the user.
For instance, the default is false (not built-in). If a user forces the override, it means they are aware of the warning and its consequences.
Thought process:
I spent a lot of time thinking about this. As a beginner, without any restrictions or warnings, maintaining and ensuring compatibility down the line could be a real challenge. I considered three options:
Fully open: great flexibility, but tough to maintain and keep compatible.
Light restriction (this one): default false, can still be overridden. ☑️
Fully restricted: only custom interfaces with strict enforcement, but might limit third-party flexibility and design.