From 2c19c8ee87ba44694c399034c97d5562f3453da6 Mon Sep 17 00:00:00 2001 From: holmes1412 Date: Wed, 11 Aug 2021 21:32:52 +0800 Subject: [PATCH] update codes --- src/nameservice/UpstreamPolicies.cc | 22 +++++++++++----------- src/nameservice/UpstreamPolicies.h | 2 +- src/nameservice/WFServiceGovernance.cc | 5 +---- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/nameservice/UpstreamPolicies.cc b/src/nameservice/UpstreamPolicies.cc index 4e9617e0..a660ea4a 100644 --- a/src/nameservice/UpstreamPolicies.cc +++ b/src/nameservice/UpstreamPolicies.cc @@ -106,7 +106,7 @@ UPSGroupPolicy::~UPSGroupPolicy() } } -inline bool UPSGroupPolicy::is_alive_or_group_alive(const EndpointAddress *addr) const +inline bool UPSGroupPolicy::is_alive(const EndpointAddress *addr) const { UPSAddrParams *params = static_cast(addr->params); return ((params->group_id < 0 && @@ -203,9 +203,9 @@ bool UPSGroupPolicy::select(const ParsedURI& uri, WFNSTracing *tracing, * false: means addr maybe group-alive. * If addr is not available, get one from addr->group. */ -inline EndpointAddress *UPSGroupPolicy::check_and_get(EndpointAddress *addr, - bool flag, - WFNSTracing *tracing) +EndpointAddress *UPSGroupPolicy::check_and_get(EndpointAddress *addr, + bool flag, + WFNSTracing *tracing) { UPSAddrParams *params = static_cast(addr->params); @@ -365,6 +365,11 @@ int UPSGroupPolicy::remove_server_locked(const std::string& address) //std::lock_guard lock(group->mutex); pthread_mutex_lock(&group->mutex); + if (addr->fail_count < params->max_fails) + this->fuse_one_server(addr); + else + this->remove_server_from_breaker(addr); + if (params->server_type == 0) group->weight -= params->weight; @@ -377,11 +382,6 @@ int UPSGroupPolicy::remove_server_locked(const std::string& address) } } - if (addr->fail_count < params->max_fails) - this->fuse_one_server(addr); - else - this->remove_server_from_breaker(addr); - this->server_list_change(addr, REMOVE_SERVER); remove_list.push_back(addr); pthread_mutex_unlock(&group->mutex); @@ -428,7 +428,7 @@ EndpointAddress *UPSGroupPolicy::consistent_hash_with_group(unsigned int hash) for (EndpointAddress *server : this->servers) { - if (this->is_alive_or_group_alive(server)) + if (this->is_alive(server)) { params = static_cast(server->params); @@ -550,7 +550,7 @@ EndpointAddress *UPSWeightedRandomPolicy::another_strategy(const ParsedURI& uri, for (EndpointAddress *server : this->servers) { - if (this->is_alive_or_group_alive(server)) + if (this->is_alive(server)) { addr = server; params = static_cast(server->params); diff --git a/src/nameservice/UpstreamPolicies.h b/src/nameservice/UpstreamPolicies.h index 0b3b952a..29b89174 100644 --- a/src/nameservice/UpstreamPolicies.h +++ b/src/nameservice/UpstreamPolicies.h @@ -78,7 +78,7 @@ protected: EndpointAddress *check_and_get(EndpointAddress *addr, bool flag, WFNSTracing *tracing); - bool is_alive_or_group_alive(const EndpointAddress *addr) const; + bool is_alive(const EndpointAddress *addr) const; }; class UPSWeightedRandomPolicy : public UPSGroupPolicy diff --git a/src/nameservice/WFServiceGovernance.cc b/src/nameservice/WFServiceGovernance.cc index 57af3d7c..f5e7e4e2 100644 --- a/src/nameservice/WFServiceGovernance.cc +++ b/src/nameservice/WFServiceGovernance.cc @@ -258,15 +258,12 @@ void WFServiceGovernance::failed(RouteManager::RouteResult *result, server = (EndpointAddress *)tracing->data; pthread_rwlock_wrlock(&this->rwlock); - size_t fail_count = ++server->fail_count; - if (--server->ref == 0) delete server; - else if (fail_count == server->params->max_fails) + else if (++server->fail_count == server->params->max_fails) this->fuse_server_to_breaker(server); pthread_rwlock_unlock(&this->rwlock); - this->WFNSPolicy::failed(result, tracing, target); }