Commit 75f89a5a authored by zhujiashun's avatar zhujiashun

revived_from_all_failed: optimize code

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