Skip to content
Snippets Groups Projects
default_health_check.go 4.94 KiB
Newer Older
CodingSinger's avatar
CodingSinger committed
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 healthcheck
CodingSinger's avatar
CodingSinger committed

import (
	"math"
)

import (
	"github.com/apache/dubbo-go/cluster/router"
	"github.com/apache/dubbo-go/common"
	"github.com/apache/dubbo-go/common/constant"
CodingSinger's avatar
CodingSinger committed
	"github.com/apache/dubbo-go/common/extension"
	"github.com/apache/dubbo-go/common/logger"
	"github.com/apache/dubbo-go/protocol"
)

func init() {
watermelo's avatar
watermelo committed
	extension.SetHealthChecker(constant.DEFAULT_HEALTH_CHECKER, NewDefaultHealthChecker)
CodingSinger's avatar
CodingSinger committed
// 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.
CodingSinger's avatar
CodingSinger committed
type DefaultHealthChecker struct {
CodingSinger's avatar
CodingSinger committed
	// the limit of outstanding request
CodingSinger's avatar
CodingSinger committed
	outStandingRequestConutLimit int32
CodingSinger's avatar
CodingSinger committed
	// the threshold of successive-failure-request
CodingSinger's avatar
CodingSinger committed
	requestSuccessiveFailureThreshold int32
CodingSinger's avatar
CodingSinger committed
	// value of circuit-tripped timeout factor
CodingSinger's avatar
CodingSinger committed
	circuitTrippedTimeoutFactor int32
CodingSinger's avatar
CodingSinger committed
// IsHealthy evaluates the healthy state on the given Invoker based on the number of successive bad request
// and the current active request
CodingSinger's avatar
CodingSinger committed
func (c *DefaultHealthChecker) IsHealthy(invoker protocol.Invoker) bool {
Ian Luo's avatar
Ian Luo committed
	if !invoker.IsAvailable() {
		return false
	}

CodingSinger's avatar
CodingSinger committed
	urlStatus := protocol.GetURLStatus(invoker.GetUrl())
	if c.isCircuitBreakerTripped(urlStatus) || urlStatus.GetActive() > c.GetOutStandingRequestCountLimit() {
CodingSinger's avatar
CodingSinger committed
		logger.Debugf("Invoker [%s] is currently in circuitbreaker tripped state", invoker.GetUrl().Key())
		return false
	}
	return true
}
CodingSinger's avatar
CodingSinger committed

// isCircuitBreakerTripped determine whether the invoker is in the tripped state by the number of successive bad request
CodingSinger's avatar
CodingSinger committed
func (c *DefaultHealthChecker) isCircuitBreakerTripped(status *protocol.RPCStatus) bool {
	circuitBreakerTimeout := c.getCircuitBreakerTimeout(status)
	currentTime := protocol.CurrentTimeMillis()
	if circuitBreakerTimeout <= 0 {
		return false
	}
	return circuitBreakerTimeout > currentTime
}

CodingSinger's avatar
CodingSinger committed
// getCircuitBreakerTimeout get the timestamp recovered from tripped state, the unit is millisecond
CodingSinger's avatar
CodingSinger committed
func (c *DefaultHealthChecker) getCircuitBreakerTimeout(status *protocol.RPCStatus) int64 {
	sleepWindow := c.getCircuitBreakerSleepWindowTime(status)
	if sleepWindow <= 0 {
		return 0
	}
	return status.GetLastRequestFailedTimestamp() + sleepWindow
}

CodingSinger's avatar
CodingSinger committed
// getCircuitBreakerSleepWindowTime get the sleep window time of invoker, the unit is millisecond
CodingSinger's avatar
CodingSinger committed
func (c *DefaultHealthChecker) getCircuitBreakerSleepWindowTime(status *protocol.RPCStatus) int64 {

	successiveFailureCount := status.GetSuccessiveRequestFailureCount()
	diff := successiveFailureCount - c.GetRequestSuccessiveFailureThreshold()
CodingSinger's avatar
CodingSinger committed
	if diff < 0 {
		return 0
	} else if diff > constant.DEFAULT_SUCCESSIVE_FAILED_REQUEST_MAX_DIFF {
		diff = constant.DEFAULT_SUCCESSIVE_FAILED_REQUEST_MAX_DIFF
CodingSinger's avatar
CodingSinger committed
	}
watermelo's avatar
watermelo committed
	sleepWindow := (1 << uint(diff)) * c.GetCircuitTrippedTimeoutFactor()
	if sleepWindow > constant.MAX_CIRCUIT_TRIPPED_TIMEOUT_IN_MS {
		sleepWindow = constant.MAX_CIRCUIT_TRIPPED_TIMEOUT_IN_MS
CodingSinger's avatar
CodingSinger committed
	}
	return int64(sleepWindow)
}

// GetRequestSuccessiveFailureThreshold return the requestSuccessiveFailureThreshold bound to this DefaultHealthChecker
func (c *DefaultHealthChecker) GetRequestSuccessiveFailureThreshold() int32 {
	return c.requestSuccessiveFailureThreshold
}

// GetCircuitTrippedTimeoutFactor return the circuitTrippedTimeoutFactor bound to this DefaultHealthChecker
func (c *DefaultHealthChecker) GetCircuitTrippedTimeoutFactor() int32 {
	return c.circuitTrippedTimeoutFactor
}

// GetOutStandingRequestCountLimit return the outStandingRequestConutLimit bound to this DefaultHealthChecker
func (c *DefaultHealthChecker) GetOutStandingRequestCountLimit() int32 {
	return c.outStandingRequestConutLimit
}

CodingSinger's avatar
CodingSinger committed
// NewDefaultHealthChecker constructs a new DefaultHealthChecker based on the url
CodingSinger's avatar
CodingSinger committed
func NewDefaultHealthChecker(url *common.URL) router.HealthChecker {
	return &DefaultHealthChecker{
		outStandingRequestConutLimit:      url.GetParamInt32(constant.OUTSTANDING_REQUEST_COUNT_LIMIT_KEY, math.MaxInt32),
		requestSuccessiveFailureThreshold: url.GetParamInt32(constant.SUCCESSIVE_FAILED_REQUEST_THRESHOLD_KEY, constant.DEFAULT_SUCCESSIVE_FAILED_REQUEST_MAX_DIFF),
		circuitTrippedTimeoutFactor:       url.GetParamInt32(constant.CIRCUIT_TRIPPED_TIMEOUT_FACTOR_KEY, constant.DEFAULT_CIRCUIT_TRIPPED_TIMEOUT_FACTOR),