Commit 75f89a5a authored by zhujiashun's avatar zhujiashun

revived_from_all_failed: optimize code

parent f0224fcc
......@@ -62,12 +62,12 @@ bool DefaultClusterRecoverPolicy::StopRecoverIfNecessary() {
return true;
}
int DefaultClusterRecoverPolicy::GetUsableServerCount(
uint64_t DefaultClusterRecoverPolicy::GetUsableServerCount(
int64_t now_ms, const std::vector<ServerId>& server_list) {
if (now_ms - _usable_cache_time_ms < FLAGS_detect_available_server_interval_ms) {
return _usable_cache;
}
int usable = 0;
uint64_t usable = 0;
size_t n = server_list.size();
SocketUniquePtr ptr;
for (size_t i = 0; i < n; ++i) {
......@@ -90,7 +90,7 @@ bool DefaultClusterRecoverPolicy::DoReject(const std::vector<ServerId>& server_l
return false;
}
int64_t now_ms = butil::gettimeofday_ms();
int usable = GetUsableServerCount(now_ms, server_list);
uint64_t usable = GetUsableServerCount(now_ms, server_list);
if (_last_usable != usable) {
std::unique_lock<butil::Mutex> mu(_mutex);
if (_last_usable != usable) {
......@@ -98,8 +98,7 @@ bool DefaultClusterRecoverPolicy::DoReject(const std::vector<ServerId>& server_l
_last_usable_change_time_ms = now_ms;
}
}
int rand = butil::fast_rand_less_than(_minimum_working_instances);
if (rand >= usable) {
if (butil::fast_rand_less_than(_minimum_working_instances) >= usable) {
return true;
}
return false;
......
......@@ -62,16 +62,16 @@ public:
bool StopRecoverIfNecessary();
private:
int GetUsableServerCount(int64_t now_ms, const std::vector<ServerId>& server_list);
uint64_t GetUsableServerCount(int64_t now_ms, const std::vector<ServerId>& server_list);
private:
bool _recovering;
int64_t _minimum_working_instances;
butil::Mutex _mutex;
int64_t _last_usable;
uint64_t _last_usable;
int64_t _last_usable_change_time_ms;
int64_t _hold_time_ms;
int64_t _usable_cache;
uint64_t _usable_cache;
int64_t _usable_cache_time_ms;
};
......
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