Skip to content
Snippets Groups Projects
default_health_check.go 4.62 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/extension"
	"github.com/apache/dubbo-go/common/logger"
	"github.com/apache/dubbo-go/protocol"
)

const (
	HEALTH_CHECKER                             = "health.checker"
	DEFAULT_HEALTH_CHECKER                     = "default"
	OUTSTANDING_REQUEST_COUNT_LIMIT_KEY        = "outstanding.request.limit"
	SUCCESSIVE_FAILED_REQUEST_THRESHOLD_KEY    = "successive.failed.threshold"
	DEFAULT_SUCCESSIVE_FAILED_THRESHOLD        = 5
	CIRCUIT_TRIPPED_TIMEOUT_FACTOR_KEY         = "circuit.tripped.timeout.factor"
	DEFAULT_SUCCESSIVE_FAILED_REQUEST_MAX_DIFF = 5
	DEFAULT_CIRCUIT_TRIPPED_TIMEOUT_FACTOR     = 1000
CodingSinger's avatar
CodingSinger committed
	MAX_CIRCUIT_TRIPPED_TIMEOUT_IN_MS          = 30000
CodingSinger's avatar
CodingSinger committed
)

func init() {
	extension.SethealthChecker(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
	// outStandingRequestConutLimit
	outStandingRequestConutLimit int32
	// requestSuccessiveFailureThreshold
	requestSuccessiveFailureThreshold int32
	// requestSuccessiveFailureThreshold
	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 {
	urlStatus := protocol.GetURLStatus(invoker.GetUrl())
CodingSinger's avatar
CodingSinger committed
	if c.isCircuitBreakerTripped(urlStatus) || urlStatus.GetActive() > c.outStandingRequestConutLimit {
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()
CodingSinger's avatar
CodingSinger committed
	diff := successiveFailureCount - c.requestSuccessiveFailureThreshold
CodingSinger's avatar
CodingSinger committed
	if diff < 0 {
		return 0
	} else if diff > DEFAULT_SUCCESSIVE_FAILED_REQUEST_MAX_DIFF {
		diff = DEFAULT_SUCCESSIVE_FAILED_REQUEST_MAX_DIFF
	}
	sleepWindow := (1 << diff) * DEFAULT_CIRCUIT_TRIPPED_TIMEOUT_FACTOR
CodingSinger's avatar
CodingSinger committed
	if sleepWindow > MAX_CIRCUIT_TRIPPED_TIMEOUT_IN_MS {
		sleepWindow = MAX_CIRCUIT_TRIPPED_TIMEOUT_IN_MS
CodingSinger's avatar
CodingSinger committed
	}
	return int64(sleepWindow)
}

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{
CodingSinger's avatar
CodingSinger committed
		outStandingRequestConutLimit:      int32(url.GetParamInt(OUTSTANDING_REQUEST_COUNT_LIMIT_KEY, math.MaxInt32)),
		requestSuccessiveFailureThreshold: int32(url.GetParamInt(SUCCESSIVE_FAILED_REQUEST_THRESHOLD_KEY, DEFAULT_SUCCESSIVE_FAILED_REQUEST_MAX_DIFF)),
		circuitTrippedTimeoutFactor:       int32(url.GetParamInt(CIRCUIT_TRIPPED_TIMEOUT_FACTOR_KEY, DEFAULT_CIRCUIT_TRIPPED_TIMEOUT_FACTOR)),