Skip to content

Conversation

@louyuting
Copy link
Collaborator

@louyuting louyuting commented Jan 1, 2020

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

@louyuting louyuting force-pushed the Issue13-circuitbreaker-slot branch 3 times, most recently from 8919dc1 to f6cdcb8 Compare January 3, 2020 09:45

ResourceName() string

PassCheck(ctx *EntryContext, node StatNode, AcquireCount uint32) bool
Copy link
Member

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.

Copy link
Collaborator Author

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.

}

// The base struct of circuit breaker rule
type BreakerRuleBase struct {
Copy link
Member

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"?

TimeSilent int64 `json:"timeSilent"`
SampleCount uint32 `json:"sampleCount"`
IntervalInMs uint32 `json:"intervalInMs"`
Stat sbase.Metric `json:"stat,omitempty"`
Copy link
Member

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).

Copy link
Collaborator Author

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.

}
)

type RuleManager struct {
Copy link
Member

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?

@sczyh30 sczyh30 added the to-review PRs to review label Jan 7, 2020
@louyuting louyuting force-pushed the Issue13-circuitbreaker-slot branch from f6cdcb8 to 647d431 Compare January 11, 2020 09:14
@louyuting louyuting changed the title Issue13-circuitbreaker-slot Issue13 circuit breaker slot and general property model Jan 11, 2020
@louyuting louyuting force-pushed the Issue13-circuitbreaker-slot branch 3 times, most recently from b1504e7 to b0c5688 Compare January 20, 2020 03:56
@louyuting louyuting changed the title Issue13 circuit breaker slot and general property model Issue13 circuit breaker implement Jan 20, 2020
@louyuting louyuting force-pushed the Issue13-circuitbreaker-slot branch from b0c5688 to 7c9478c Compare January 20, 2020 14:03
@louyuting louyuting requested a review from sczyh30 January 20, 2020 14:06
@louyuting louyuting removed the to-review PRs to review label Feb 12, 2020
@sczyh30 sczyh30 added area/circuit-breaking Issues or PRs related to circuit breaking to-review PRs to review labels Feb 12, 2020
@louyuting louyuting force-pushed the Issue13-circuitbreaker-slot branch from 7c9478c to 65a8eaf Compare February 13, 2020 11:05
@louyuting louyuting removed the to-review PRs to review label Feb 13, 2020
@louyuting louyuting added this to the 0.2.0 milestone Feb 13, 2020
@louyuting louyuting force-pushed the Issue13-circuitbreaker-slot branch from 65a8eaf to f640e4b Compare February 13, 2020 13:49
@louyuting louyuting removed the request for review from sczyh30 February 15, 2020 06:25
@louyuting louyuting self-assigned this Feb 15, 2020
@louyuting louyuting changed the title Issue13 circuit breaker implement Issue13 circuit breaker implementation Feb 15, 2020
@louyuting louyuting force-pushed the Issue13-circuitbreaker-slot branch 4 times, most recently from b96b0ff to bd39224 Compare February 18, 2020 17:02
@louyuting louyuting added the to-review PRs to review label Feb 19, 2020
@louyuting louyuting force-pushed the Issue13-circuitbreaker-slot branch from afb4f3c to 4357d97 Compare February 20, 2020 15:04
// return the strategy type
BreakerStrategy() BreakerStrategy

isApplicable() bool
Copy link
Member

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?

Copy link
Collaborator Author

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.

// resource name
Resource string `json:"resource"`
Strategy BreakerStrategy `json:"strategy"`
// silent status, all requests would be broken during silent phase.
Copy link
Member

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.

)

// The base interface of circuit breaker rule
type BreakerRule interface {
Copy link
Member

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)

Copy link
Collaborator Author

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)

Just Rule might make sense.
package name has indicated the circuit_breaker.

func (b *BreakerRuleBase) String() string {
r, err := json.Marshal(b)
if err != nil {
// feedback string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fallback string?

Copy link
Collaborator Author

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

// the threshold of rt(ms)
Threshold float64 `json:"threshold"`
// the count of request exceed the threshold
PassCount int64 `json:"pass"`
Copy link
Member

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?

Copy link
Collaborator Author

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
Copy link
Member

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":)

Copy link
Collaborator Author

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"?

Copy link
Member

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)
Copy link
Member

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.

@louyuting louyuting force-pushed the Issue13-circuitbreaker-slot branch from 4357d97 to e33e54d Compare March 8, 2020 08:23
@louyuting louyuting requested a review from sczyh30 March 8, 2020 14:09
@louyuting louyuting force-pushed the Issue13-circuitbreaker-slot branch from f7e0555 to 49f6b77 Compare March 15, 2020 14:48
@sczyh30 sczyh30 removed this from the 0.2.0 milestone Mar 17, 2020
@louyuting louyuting force-pushed the Issue13-circuitbreaker-slot branch from 49f6b77 to 6243260 Compare March 23, 2020 16:37
@louyuting louyuting added this to the 0.3.0 milestone Mar 23, 2020
@sczyh30
Copy link
Member

sczyh30 commented Apr 8, 2020

Could you please resolve the conflicts?

@louyuting louyuting force-pushed the Issue13-circuitbreaker-slot branch from 6243260 to 2f3e11c Compare April 8, 2020 11:02
@louyuting louyuting force-pushed the Issue13-circuitbreaker-slot branch from 2f3e11c to cd9dfff Compare April 8, 2020 11:08
Copy link
Member

@sczyh30 sczyh30 left a 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

@sczyh30 sczyh30 merged commit caa4e5f into alibaba:master Apr 8, 2020
@sczyh30 sczyh30 removed the to-review PRs to review label Apr 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/circuit-breaking Issues or PRs related to circuit breaking

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Circuit breaking implementation

2 participants