Skip to content

Commit b97a1ba

Browse files
authored
Merge pull request #21 from lambdaliu/slow_half_open
feat: slow down select half open instance
2 parents edea6c4 + 695b48d commit b97a1ba

File tree

12 files changed

+65
-34
lines changed

12 files changed

+65
-34
lines changed

polaris/engine/circuit_breaker_executor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void CircuitBreakerExecutor::TimingCircuitBreak(CircuitBreakerExecutor* executor
5757
all_service_contexts.clear();
5858
// 设置定时任务
5959
executor->reactor_.AddTimingTask(
60-
new TimingFuncTask<CircuitBreakerExecutor>(TimingCircuitBreak, executor, 500));
60+
new TimingFuncTask<CircuitBreakerExecutor>(TimingCircuitBreak, executor, 1000));
6161
}
6262

6363
} // namespace polaris

polaris/model/model.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,7 @@ void Service::SetCircuitBreakerData(const CircuitBreakerData& circuit_breaker_da
995995
impl_->have_half_open_data_ = true;
996996
} else {
997997
impl_->have_half_open_data_ = false;
998+
impl_->try_half_open_count_ = 20;
998999
}
9991000
}
10001001

@@ -1020,6 +1021,19 @@ ReturnCode Service::TryChooseHalfOpenInstance(std::set<Instance*>& instances, In
10201021
if (!impl_->have_half_open_data_ || instances.empty()) {
10211022
return kReturnInstanceNotFound;
10221023
}
1024+
// 控制释放半开节点的评论,有半开节点以后每20个请求
1025+
// 且距离上次释放半开节点超过2s就释放1个半开请求
1026+
if (++impl_->try_half_open_count_ < 20) {
1027+
return kReturnInstanceNotFound; // 距离上次释放不足20个正常请求
1028+
}
1029+
uint64_t last_half_open_time = impl_->last_half_open_time_.Load();
1030+
uint64_t current_time = Time::GetCurrentTimeMs();
1031+
if (current_time < last_half_open_time + 2000 ||
1032+
!impl_->last_half_open_time_.Cas(last_half_open_time, current_time)) {
1033+
return kReturnInstanceNotFound; // 距离上一次释放半开间隔不足2s
1034+
}
1035+
impl_->try_half_open_count_ = 0;
1036+
10231037
std::set<Instance*>::iterator split_it = instances.begin();
10241038
std::advance(split_it, rand() % instances.size());
10251039
std::set<Instance*>::iterator instance_it;

polaris/model/model_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ class ServiceImpl {
300300

301301
// 半开优先分配数据
302302
sync::Mutex half_open_lock_;
303+
sync::Atomic<uint64_t> last_half_open_time_;
304+
sync::Atomic<int> try_half_open_count_;
303305
bool have_half_open_data_;
304306
std::map<std::string, int> half_open_data_; // 存储半开分配数据
305307

polaris/plugin/circuit_breaker/circuit_breaker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static const char kChainEnableKey[] = "enable";
3535
static const bool kChainEnableDefault = true;
3636

3737
static const char kChainCheckPeriodKey[] = "checkPeriod";
38-
static const uint64_t kChainCheckPeriodDefault = 500;
38+
static const uint64_t kChainCheckPeriodDefault = 1000;
3939

4040
// 用于传入是否开启探测插件
4141
static const char kDetectorDisableKey[] = "detectorDisable";

polaris/plugin/circuit_breaker/error_count.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ ReturnCode ErrorCountCircuitBreaker::TimingCircuitBreak(
140140
error_count_status.error_count = 0;
141141
error_count_status.last_update_time = current_time;
142142
}
143-
} else if (error_count_status.last_update_time + 20 * sleep_window_ <= current_time) {
143+
} else if (error_count_status.last_access_time + 100 * sleep_window_ <= current_time) {
144144
// 兜底:如果访问量一定时间达不到要求,则重新熔断
145145
if (instances_status->TranslateStatus(it->first, kCircuitBreakerHalfOpen,
146146
kCircuitBreakerOpen)) {

polaris/plugin/circuit_breaker/error_count.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525

2626
namespace polaris {
2727

28-
class Config;
29-
class Context;
30-
3128
struct ErrorCountStatus {
3229
CircuitBreakerStatus status;
3330
int error_count;

polaris/plugin/circuit_breaker/error_rate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ ReturnCode ErrorRateCircuitBreaker::TimingCircuitBreak(
175175
error_rate_status.ClearBuckets(metric_num_buckets_);
176176
}
177177
} else if (err_req > request_count_after_half_open_ - success_count_after_half_open_ ||
178-
error_rate_status.last_update_time + 20 * sleep_window_ <= current_time) {
178+
error_rate_status.last_access_time + 100 * sleep_window_ <= current_time) {
179179
// 达到重新熔断条件
180180
if (instances_status->TranslateStatus(it->first, kCircuitBreakerHalfOpen,
181181
kCircuitBreakerOpen)) {

polaris/plugin/circuit_breaker/error_rate.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525

2626
namespace polaris {
2727

28-
class Config;
29-
class Context;
30-
3128
struct ErrorRateBucket {
3229
ErrorRateBucket() : total_count(0), error_count(0), bucket_time(0) {}
3330
int total_count;

polaris/plugin/health_checker/http_detector.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ ReturnCode HttpHealthChecker::Init(Config* config, Context* /*context*/) {
3636

3737
request_path_ = config->GetStringOrDefault(kHttpRequestPathKey, kHttpRequestPathDefault);
3838
if (request_path_.empty() || request_path_[0] != '/') {
39-
POLARIS_LOG(LOG_ERROR, "outlier detector[%s] config %s invalid", kPluginHttpHealthChecker,
39+
POLARIS_LOG(LOG_ERROR, "health checker[%s] config %s invalid", kPluginHttpHealthChecker,
4040
kHttpRequestPathKey);
4141
return kReturnInvalidConfig;
4242
}
4343
timeout_ms_ = config->GetMsOrDefault(HealthCheckerConfig::kTimeoutKey,
4444
HealthCheckerConfig::kTimeoutDefault);
45-
4645
return kReturnOk;
4746
}
4847

polaris/plugin/health_checker/tcp_detector.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,17 @@ ReturnCode TcpHealthChecker::Init(Config* config, Context* /*context*/) {
3636

3737
std::string send_package = config->GetStringOrDefault(kTcpSendPackageKey, kTcpSendPackageDefault);
3838
if (!send_package.empty() && !Utils::HexStringToBytes(send_package, &send_package_)) {
39-
POLARIS_LOG(LOG_ERROR, "outlier detector[%s] config %s hexstring to bytes failed",
39+
POLARIS_LOG(LOG_ERROR, "health checker[%s] config %s hexstring to bytes failed",
4040
kPluginTcpHealthChecker, kTcpSendPackageKey);
4141
return kReturnInvalidConfig;
4242
}
4343
std::string receive_package =
4444
config->GetStringOrDefault(kTcpReceivePackageKey, kTcpReceivePackageDefault);
4545
if (!receive_package.empty() && !Utils::HexStringToBytes(receive_package, &receive_package_)) {
46-
POLARIS_LOG(LOG_ERROR, "outlier detector[%s] config %s hexstring to bytes failed",
46+
POLARIS_LOG(LOG_ERROR, "health checker[%s] config %s hexstring to bytes failed",
4747
kPluginTcpHealthChecker, kTcpReceivePackageKey);
4848
return kReturnInvalidConfig;
4949
}
50-
5150
timeout_ms_ = config->GetMsOrDefault(HealthCheckerConfig::kTimeoutKey,
5251
HealthCheckerConfig::kTimeoutDefault);
5352
return kReturnOk;

0 commit comments

Comments
 (0)