Unverified Commit 56162254 authored by Ge Jun's avatar Ge Jun Committed by GitHub

Merge pull request #565 from TousakaRin/circuit_breaker

circuit_breaker optimization 
parents 3dbd41cf ba77db4d
...@@ -63,7 +63,8 @@ CircuitBreaker::EmaErrorRecorder::EmaErrorRecorder(int window_size, ...@@ -63,7 +63,8 @@ CircuitBreaker::EmaErrorRecorder::EmaErrorRecorder(int window_size,
: _window_size(window_size) : _window_size(window_size)
, _max_error_percent(max_error_percent) , _max_error_percent(max_error_percent)
, _smooth(std::pow(EPSILON, 1.0/window_size)) , _smooth(std::pow(EPSILON, 1.0/window_size))
, _sample_count(0) , _sample_count_when_initializing(0)
, _error_count_when_initializing(0)
, _ema_error_cost(0) , _ema_error_cost(0)
, _ema_latency(0) { , _ema_latency(0) {
} }
...@@ -80,7 +81,17 @@ bool CircuitBreaker::EmaErrorRecorder::OnCallEnd(int error_code, ...@@ -80,7 +81,17 @@ bool CircuitBreaker::EmaErrorRecorder::OnCallEnd(int error_code,
healthy = UpdateErrorCost(latency, ema_latency); healthy = UpdateErrorCost(latency, ema_latency);
} }
if (_sample_count.fetch_add(1, butil::memory_order_relaxed) < _window_size) { // When the window is initializing, use error_rate to determine
// if it needs to be isolated.
if (_sample_count_when_initializing.load(butil::memory_order_relaxed) < _window_size &&
_sample_count_when_initializing.fetch_add(1, butil::memory_order_relaxed) < _window_size) {
if (error_code != 0) {
const int32_t error_count =
_error_count_when_initializing.fetch_add(1, butil::memory_order_relaxed);
return error_count < _window_size * _max_error_percent / 100;
}
// Because once OnCallEnd returned false, the node will be ioslated soon,
// so when error_code=0, we no longer check the error count.
return true; return true;
} }
...@@ -88,7 +99,8 @@ bool CircuitBreaker::EmaErrorRecorder::OnCallEnd(int error_code, ...@@ -88,7 +99,8 @@ bool CircuitBreaker::EmaErrorRecorder::OnCallEnd(int error_code,
} }
void CircuitBreaker::EmaErrorRecorder::Reset() { void CircuitBreaker::EmaErrorRecorder::Reset() {
_sample_count.store(0, butil::memory_order_relaxed); _sample_count_when_initializing.store(0, butil::memory_order_relaxed);
_error_count_when_initializing.store(0, butil::memory_order_relaxed);
_ema_error_cost.store(0, butil::memory_order_relaxed); _ema_error_cost.store(0, butil::memory_order_relaxed);
_ema_latency.store(0, butil::memory_order_relaxed); _ema_latency.store(0, butil::memory_order_relaxed);
} }
......
...@@ -74,7 +74,8 @@ private: ...@@ -74,7 +74,8 @@ private:
const int _max_error_percent; const int _max_error_percent;
const double _smooth; const double _smooth;
butil::atomic<int64_t> _sample_count; butil::atomic<int32_t> _sample_count_when_initializing;
butil::atomic<int32_t> _error_count_when_initializing;
butil::atomic<int64_t> _ema_error_cost; butil::atomic<int64_t> _ema_error_cost;
butil::atomic<int64_t> _ema_latency; butil::atomic<int64_t> _ema_latency;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment