共计 4560 个字符,预计需要花费 12 分钟才能阅读完成。
序
本文次要钻研一下 dubbo-go 的 DefaultHealthChecker
DefaultHealthChecker
dubbo-go-v1.4.2/cluster/router/healthcheck/default_health_check.go
func init() {extension.SethealthChecker(constant.DEFAULT_HEALTH_CHECKER, NewDefaultHealthChecker)
}
// DefaultHealthChecker is the default implementation of HealthChecker, which determines the health status of
// the invoker based on the number of successive bad request and the current active request.
type DefaultHealthChecker struct {
// the limit of outstanding request
outStandingRequestConutLimit int32
// the threshold of successive-failure-request
requestSuccessiveFailureThreshold int32
// value of circuit-tripped timeout factor
circuitTrippedTimeoutFactor int32
}
- DefaultHealthChecker 定义了 outStandingRequestConutLimit、requestSuccessiveFailureThreshold、circuitTrippedTimeoutFactor 属性
NewDefaultHealthChecker
dubbo-go-v1.4.2/cluster/router/healthcheck/default_health_check.go
// NewDefaultHealthChecker constructs a new DefaultHealthChecker based on the url
func NewDefaultHealthChecker(url *common.URL) router.HealthChecker {
return &DefaultHealthChecker{outStandingRequestConutLimit: int32(url.GetParamInt(constant.OUTSTANDING_REQUEST_COUNT_LIMIT_KEY, math.MaxInt32)),
requestSuccessiveFailureThreshold: int32(url.GetParamInt(constant.SUCCESSIVE_FAILED_REQUEST_THRESHOLD_KEY, constant.DEFAULT_SUCCESSIVE_FAILED_REQUEST_MAX_DIFF)),
circuitTrippedTimeoutFactor: int32(url.GetParamInt(constant.CIRCUIT_TRIPPED_TIMEOUT_FACTOR_KEY, constant.DEFAULT_CIRCUIT_TRIPPED_TIMEOUT_FACTOR)),
}
}
- NewDefaultHealthChecker 实例化 DefaultHealthChecker
IsHealthy
dubbo-go-v1.4.2/cluster/router/healthcheck/default_health_check.go
// IsHealthy evaluates the healthy state on the given Invoker based on the number of successive bad request
// and the current active request
func (c *DefaultHealthChecker) IsHealthy(invoker protocol.Invoker) bool {urlStatus := protocol.GetURLStatus(invoker.GetUrl())
if c.isCircuitBreakerTripped(urlStatus) || urlStatus.GetActive() > c.GetOutStandingRequestConutLimit() {logger.Debugf("Invoker [%s] is currently in circuitbreaker tripped state", invoker.GetUrl().Key())
return false
}
return true
}
- IsHealthy 办法通过 protocol.GetURLStatus(invoker.GetUrl())获取 urlStatus,之后依据 c.isCircuitBreakerTripped(urlStatus)或者
urlStatus.GetActive() > c.GetOutStandingRequestConutLimit()
判断是否处于 circuitbreaker tripped 状态
isCircuitBreakerTripped
dubbo-go-v1.4.2/cluster/router/healthcheck/default_health_check.go
// isCircuitBreakerTripped determine whether the invoker is in the tripped state by the number of successive bad request
func (c *DefaultHealthChecker) isCircuitBreakerTripped(status *protocol.RPCStatus) bool {circuitBreakerTimeout := c.getCircuitBreakerTimeout(status)
currentTime := protocol.CurrentTimeMillis()
if circuitBreakerTimeout <= 0 {return false}
return circuitBreakerTimeout > currentTime
}
- isCircuitBreakerTripped 办法通过 c.getCircuitBreakerTimeout(status)获取 circuitBreakerTimeout,若 circuitBreakerTimeout 小于等于 0 返回 false,否则判断 circuitBreakerTimeout 是否大于 currentTime
getCircuitBreakerTimeout
dubbo-go-v1.4.2/cluster/router/healthcheck/default_health_check.go
// getCircuitBreakerTimeout get the timestamp recovered from tripped state, the unit is millisecond
func (c *DefaultHealthChecker) getCircuitBreakerTimeout(status *protocol.RPCStatus) int64 {sleepWindow := c.getCircuitBreakerSleepWindowTime(status)
if sleepWindow <= 0 {return 0}
return status.GetLastRequestFailedTimestamp() + sleepWindow}
- getCircuitBreakerTimeout 办法先获取 sleepWindow,若 sleepWindow 小于等于 0 则返回 0,否则返回 status.GetLastRequestFailedTimestamp() + sleepWindow
getCircuitBreakerSleepWindowTime
dubbo-go-v1.4.2/cluster/router/healthcheck/default_health_check.go
// getCircuitBreakerSleepWindowTime get the sleep window time of invoker, the unit is millisecond
func (c *DefaultHealthChecker) getCircuitBreakerSleepWindowTime(status *protocol.RPCStatus) int64 {successiveFailureCount := status.GetSuccessiveRequestFailureCount()
diff := successiveFailureCount - c.GetRequestSuccessiveFailureThreshold()
if diff < 0 {return 0} else if diff > constant.DEFAULT_SUCCESSIVE_FAILED_REQUEST_MAX_DIFF {diff = constant.DEFAULT_SUCCESSIVE_FAILED_REQUEST_MAX_DIFF}
sleepWindow := (1 << diff) * c.GetCircuitTrippedTimeoutFactor()
if sleepWindow > constant.MAX_CIRCUIT_TRIPPED_TIMEOUT_IN_MS {sleepWindow = constant.MAX_CIRCUIT_TRIPPED_TIMEOUT_IN_MS}
return int64(sleepWindow)
}
- getCircuitBreakerSleepWindowTime 办法通过比照 status.GetSuccessiveRequestFailureCount()及 c.GetRequestSuccessiveFailureThreshold()计算 diff,进而依据 c.GetCircuitTrippedTimeoutFactor()计算 sleepWindow
小结
DefaultHealthChecker 定义了 outStandingRequestConutLimit、requestSuccessiveFailureThreshold、circuitTrippedTimeoutFactor 属性;IsHealthy 办法通过 protocol.GetURLStatus(invoker.GetUrl())获取 urlStatus,之后依据 c.isCircuitBreakerTripped(urlStatus)或者 urlStatus.GetActive() > c.GetOutStandingRequestConutLimit()
判断是否处于 circuitbreaker tripped 状态
doc
- default_health_check