CARVIEW |
Navigation Menu
-
Notifications
You must be signed in to change notification settings - Fork 445
Issue13 circuit breaker implementation #18
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
Conversation
8919dc1
to
f6cdcb8
Compare
core/base/rule.go
Outdated
@@ -6,4 +6,6 @@ type SentinelRule interface { | |||
fmt.Stringer | |||
|
|||
ResourceName() string | |||
|
|||
PassCheck(ctx *EntryContext, node StatNode, AcquireCount uint32) bool |
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.
In my opinion, the rule entity per se should not do rule checking. Wrapping the checking logic (and the rule entity itself) in the corresponding rule checker (i.e. TrafficShapingController
, CircuitBreaker
) might be better.
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.
Thanks for your advice. I would decouple the checking logic and wrap related logic in CircuitBreaker.
core/circuitbreaker/rule.go
Outdated
} | ||
|
||
// The base struct of circuit breaker rule | ||
type BreakerRuleBase struct { |
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.
Maybe the rule entity should be separated from the de-facto "CircuitBreaker"?
core/circuitbreaker/rule.go
Outdated
TimeSilent int64 `json:"timeSilent"` | ||
SampleCount uint32 `json:"sampleCount"` | ||
IntervalInMs uint32 `json:"intervalInMs"` | ||
Stat sbase.Metric `json:"stat,omitempty"` |
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.
Actually using the original Metric
here is not enough (but okay for legacy rules). For circuit breaking we need specialized data structures for the target metric type (e.g. RT or error ratio) which support arbitrary statistic time span (maybe not backed by sliding window).
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.
For MPV, will only support some simple circuit breaker. Based on my design, we could add other robust and accurate circuit breaker in the future.
core/circuitbreaker/rule_manager.go
Outdated
} | ||
) | ||
|
||
type RuleManager struct { |
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.
Is it necessary to have a RuleManager
wrapper here?
f6cdcb8
to
647d431
Compare
b1504e7
to
b0c5688
Compare
b0c5688
to
7c9478c
Compare
7c9478c
to
65a8eaf
Compare
65a8eaf
to
f640e4b
Compare
b96b0ff
to
bd39224
Compare
afb4f3c
to
4357d97
Compare
// return the strategy type | ||
BreakerStrategy() BreakerStrategy | ||
|
||
isApplicable() bool |
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.
What does "applicable" mean? valid?
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.
What does "applicable" mean? valid?
Valid and can be converted to circuit breaker.
core/circuit_breaker/rule.go
Outdated
// resource name | ||
Resource string `json:"resource"` | ||
Strategy BreakerStrategy `json:"strategy"` | ||
// silent status, all requests would be broken during silent phase. |
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.
In circuit breaking it's often called a timeout period.
core/circuit_breaker/rule.go
Outdated
) | ||
|
||
// The base interface of circuit breaker rule | ||
type BreakerRule interface { |
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.
Maybe it's better to be a CircuitBreakerRule
? (or just Rule
according to the convention of Go)
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.
Maybe it's better to be a
CircuitBreakerRule
? (or justRule
according to the convention of Go)
Just Rule might make sense.
package name has indicated the circuit_breaker.
core/circuit_breaker/rule.go
Outdated
func (b *BreakerRuleBase) String() string { | ||
r, err := json.Marshal(b) | ||
if err != nil { | ||
// feedback string |
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.
fallback string?
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.
Yes, I will fix here
core/circuit_breaker/rule.go
Outdated
// the threshold of rt(ms) | ||
Threshold float64 `json:"threshold"` | ||
// the count of request exceed the threshold | ||
PassCount int64 `json:"pass"` |
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 passCount
is real-time stat of the generated circuit breaker, not the rule entity, so maybe it's not needed in the rule entity?
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.
Yes, I would fix here.
type CircuitBreaker interface { | ||
getRule() BreakerRule | ||
|
||
CanPass(ctx *base.EntryContext, node base.StatNode, acquireCount uint32) *base.TokenResult |
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.
In fact this is a legacy bad design in Java versions. We may improve the interface later to make it a real "circuit breaker":)
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.
In fact this is a legacy bad design in Java versions. We may improve the interface later to make it a real "circuit breaker":)
Make sense.
How about rename to "CheckBreaker"?
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.
Using CircuitBreaker
is okay. We may improve the interface later :)
|
||
func newErrorRatioCircuitBreaker(rule *errorRatioBreakerRule) *errorRatioCircuitBreaker { | ||
resNode := stat.GetResourceNode(rule.Resource) | ||
metric := resNode.GetOrCreateSlidingWindowMetric(rule.SampleCount, rule.IntervalInMs) |
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.
What if the resNode
is nil? (i.e. when the resource has not been visited yet)
Note: This problem could be eliminated in later versions, as we're planning to create individual stat structures for circuit breakers, rather than use the universal ResourceNode.
4357d97
to
e33e54d
Compare
f7e0555
to
49f6b77
Compare
49f6b77
to
6243260
Compare
Could you please resolve the conflicts? |
6243260
to
2f3e11c
Compare
2f3e11c
to
cd9dfff
Compare
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.
LGTM, we could improve the design later
Describe what this PR does / why we need it
Add circuit breaker implementation. Including circuitBreaker、breakerRule、breakerManager.
The design could refer to 熔断降级设计
Does this pull request fix one issue?
Fixes #13