add UPSWeightedRandomPolicy __add_server() and __remove_server()

This commit is contained in:
holmes1412
2021-01-25 16:53:55 +08:00
parent dba49133d8
commit 5381c12365
2 changed files with 33 additions and 6 deletions
+29 -5
View File
@@ -511,8 +511,8 @@ void UPSGroupPolicy::__add_server(EndpointAddress *addr)
}
pthread_mutex_lock(&group->mutex);
this->recover_one_server(addr);
addr->group = group;
this->recover_one_server(addr);
if (addr->params.server_type == 0)
{
group->mains.push_back(addr);
@@ -614,14 +614,39 @@ const EndpointAddress *UPSGroupPolicy::consistent_hash_with_group(unsigned int h
return this->check_and_get(addr);
}
void UPSWeightedRandomPolicy::__add_server(EndpointAddress *addr)
{
UPSGroupPolicy::__add_server(addr);
if (addr->params.server_type == 0)
{
this->total_weight += addr->params.weight;
this->servers.push_back(addr);
}
return;
}
int UPSWeightedRandomPolicy::__remove_server(const std::string& address)
{
const auto map_it = this->server_map.find(address);
if (map_it != this->server_map.cend())
{
for (EndpointAddress *addr : map_it->second)
{
if (addr->params.server_type == 0)
this->total_weight -= addr->params.weight;
}
}
return UPSGroupPolicy::__remove_server(address);
}
const EndpointAddress *UPSWeightedRandomPolicy::first_stradegy(const ParsedURI& uri)
{
int x = 0;
int s = 0;
size_t idx;
int temp_weight;
temp_weight = this->total_weight;
int temp_weight = this->total_weight;
if (temp_weight > 0)
x = rand() % temp_weight;
@@ -661,7 +686,6 @@ const EndpointAddress *UPSWeightedRandomPolicy::another_stradegy(const ParsedURI
return this->check_and_get(addr);
}
void UPSWeightedRandomPolicy::recover_one_server(const EndpointAddress *addr)
{
this->nalives++;
+4 -1
View File
@@ -168,10 +168,11 @@ private:
}
// override: select() add_server() remove_server()
virtual bool select(const ParsedURI& uri, EndpointAddress **addr);
protected:
virtual void __add_server(EndpointAddress *addr);
virtual int __remove_server(const std::string& address);
protected:
const EndpointAddress *consistent_hash_with_group(unsigned int hash) const;
inline const EndpointAddress *check_and_get(const EndpointAddress *addr) const // check_get_weak
@@ -212,6 +213,8 @@ protected:
private:
virtual void recover_one_server(const EndpointAddress *addr);
virtual void fuse_one_server(const EndpointAddress *addr);
virtual void __add_server(EndpointAddress *addr);
virtual int __remove_server(const std::string& address);
};
using select_t = std::function<unsigned int (const char *, const char *, const char *)>;