mirror of
https://github.com/sogou/workflow.git
synced 2026-02-08 01:33:17 +08:00
Change some descriptions. (#60)
* change some descriptions Co-authored-by: XieHan <XieHan@sogou-inc.com>
This commit is contained in:
@@ -172,11 +172,9 @@ struct AddressParams
|
||||
* - If max_fails is set to 1, it means server would out of upstream selection in 30 seconds when failed only once
|
||||
*/
|
||||
unsigned int max_fails; ///< [1, INT32_MAX] max_fails = 0 means max_fails = 1
|
||||
unsigned short weight; ///< [1, 65535] weight = 0 means weight = 1. only for master
|
||||
#define SERVER_TYPE_MASTER 0
|
||||
#define SERVER_TYPE_SLAVE 1
|
||||
int server_type; ///< default is SERVER_TYPE_MASTER
|
||||
int group_id; ///< -1 means no group. Slave without group will backup for any master
|
||||
unsigned short weight; ///< [1, 65535] weight = 0 means weight = 1. only for main server
|
||||
int server_type; ///< 0 for main and 1 for backup
|
||||
int group_id; ///< -1 means no group. Backup without group will backup for any main node
|
||||
};
|
||||
~~~
|
||||
大多数参数的作用一眼了然。其中endpoint_params和dns相关参数,可以覆盖全局的配置。
|
||||
@@ -190,7 +188,7 @@ struct AddressParams
|
||||
max_fails参数为最大出错次数,如果选取目标连续出错达到max_fails则熔断,如果upstream的try_another属性为false,则任务失败,
|
||||
在任务callback里,get_state()=WFT_STATE_TASK_ERROR,get_error()=WFT_ERR_UPSTREAM_UNAVAILABLE。
|
||||
如果try_another为true,并且所有server都熔断的话,会得到同样错误。熔断时间为30秒。
|
||||
server_type和group_id用于主备功能。所有upstream必需有type为MASTER的server,否则upstream不可用。
|
||||
类型为SLAVE的server,会在同group_id的MASTER熔断情况下被使用。
|
||||
server_type和group_id用于主备功能。所有upstream必需有type为0(主节点)的server,否则upstream不可用。
|
||||
类型为1(备份节点)的server,会在同group_id的主节点熔断情况下被使用。
|
||||
|
||||
更多upstream功能查询:[about-upstream.md](./about-upstream.md)。
|
||||
|
||||
@@ -85,7 +85,7 @@ http_task->start();
|
||||
基本原理
|
||||
1. 随机选择一个目标
|
||||
2. 如果try_another配置为true,那么将在所有存活的目标中随机选择一个
|
||||
3. 仅在master中选择,选中目标所在group的主备和无group的备都视为有效的可选对象
|
||||
3. 仅在main中选择,选中目标所在group的主备和无group的备都视为有效的可选对象
|
||||
|
||||
### 例2 在多个目标中按照权重大小随机访问
|
||||
配置一个本地反向代理,将本地发出的weighted.random所有请求按照5/20/1的权重分配打到3个目标server上
|
||||
@@ -107,7 +107,7 @@ http_task->start();
|
||||
基本原理
|
||||
1. 按照权重分配,随机选择一个目标,权重越大概率越大
|
||||
2. 如果try_another配置为true,那么将在所有存活的目标中按照权重分配随机选择一个
|
||||
3. 仅在master中选择,选中目标所在group的主备和无group的备都视为有效的可选对象
|
||||
3. 仅在main中选择,选中目标所在group的主备和无group的备都视为有效的可选对象
|
||||
|
||||
### 例3 在多个目标中按照框架默认的一致性哈希访问
|
||||
~~~cpp
|
||||
@@ -125,11 +125,11 @@ auto *http_task = WFTaskFactory::create_http_task("http://abc.local/service/meth
|
||||
http_task->start();
|
||||
~~~
|
||||
基本原理
|
||||
1. 每1个master视为16个虚拟节点
|
||||
1. 每1个main视为16个虚拟节点
|
||||
2. 框架会使用std::hash对所有节点的address+虚拟index进行运算,作为一致性哈希的node值
|
||||
3. 框架会使用std::hash对path+query+fragment进行运算,作为一致性哈希data值
|
||||
4. 每次都选择存活node最近的值作为目标
|
||||
5. 对于每一个master、只要有存活group内master/有存活group内slave/有存活no group slave,即视为存活
|
||||
5. 对于每一个main、只要有存活group内main/有存活group内backup/有存活no group backup,即视为存活
|
||||
|
||||
### 例4 自定义一致性哈希函数
|
||||
~~~cpp
|
||||
@@ -183,8 +183,8 @@ auto *http_task = WFTaskFactory::create_http_task("http://xyz.cdn/sompath?key=so
|
||||
http_task->start();
|
||||
~~~
|
||||
基本原理
|
||||
1. 框架首先依据用户提供的普通选取函数、按照取模,在master列表中确定选取
|
||||
2. 对于每一个master、只要有存活group内master/有存活group内slave/有存活no group slave,即视为存活
|
||||
1. 框架首先依据用户提供的普通选取函数、按照取模,在main列表中确定选取
|
||||
2. 对于每一个main、只要有存活group内main/有存活group内backup/有存活no group backup,即视为存活
|
||||
3. 如果选中目标不再存活且try_another设为true,将再使用一致性哈希函数进行二次选取
|
||||
4. 如果触发二次选取,一致性哈希将保证一定会选择一个存活目标、除非全部机器都被熔断掉
|
||||
|
||||
@@ -195,10 +195,10 @@ UpstreamManager::upstream_create_weighted_random(
|
||||
true);//一主一备这项设什么没区别
|
||||
|
||||
AddressParams address_params = ADDRESS_PARAMS_DEFAULT;
|
||||
address_params.server_type = SERVER_TYPE_MASTER;
|
||||
UpstreamManager::upstream_add_server("simple.name", "master01.test.ted.bj.sogou", &address_params);//主
|
||||
address_params.server_type = SERVER_TYPE_SLAVE;
|
||||
UpstreamManager::upstream_add_server("simple.name", "slave01.test.ted.gd.sogou", &address_params);//备
|
||||
address_params.server_type = 0;
|
||||
UpstreamManager::upstream_add_server("simple.name", "main01.test.ted.bj.sogou", &address_params);//主
|
||||
address_params.server_type = 1;
|
||||
UpstreamManager::upstream_add_server("simple.name", "backup01.test.ted.gd.sogou", &address_params);//备
|
||||
|
||||
auto *http_task = WFTaskFactory::create_http_task("http://simple.name/request", 0, 0, nullptr);
|
||||
auto *redis_task = WFTaskFactory::create_redis_task("redis://simple.name/2", 0, nullptr);
|
||||
@@ -219,33 +219,33 @@ UpstreamManager::upstream_create_consistent_hash(
|
||||
nullptr);//nullptr代表使用框架默认的一致性哈希函数
|
||||
|
||||
AddressParams address_params = ADDRESS_PARAMS_DEFAULT;
|
||||
address_params.server_type = SERVER_TYPE_MASTER;
|
||||
address_params.server_type = 0;
|
||||
address_params.group_id = 1001;
|
||||
UpstreamManager::upstream_add_server("abc.local", "192.168.2.100:8081", &address_params);//master in group 1001
|
||||
address_params.server_type = SERVER_TYPE_SLAVE;
|
||||
UpstreamManager::upstream_add_server("abc.local", "192.168.2.100:8081", &address_params);//main in group 1001
|
||||
address_params.server_type = 1;
|
||||
address_params.group_id = 1001;
|
||||
UpstreamManager::upstream_add_server("abc.local", "192.168.2.100:8082", &address_params);//slave for group 1001
|
||||
address_params.server_type = SERVER_TYPE_MASTER;
|
||||
UpstreamManager::upstream_add_server("abc.local", "192.168.2.100:8082", &address_params);//backup for group 1001
|
||||
address_params.server_type = 0;
|
||||
address_params.group_id = 1002;
|
||||
UpstreamManager::upstream_add_server("abc.local", "master01.test.ted.bj.sogou", &address_params);//master in group 1002
|
||||
address_params.server_type = SERVER_TYPE_SLAVE;
|
||||
UpstreamManager::upstream_add_server("abc.local", "main01.test.ted.bj.sogou", &address_params);//main in group 1002
|
||||
address_params.server_type = 1;
|
||||
address_params.group_id = 1002;
|
||||
UpstreamManager::upstream_add_server("abc.local", "slave01.test.ted.gd.sogou", &address_params);//slave for group 1002
|
||||
address_params.server_type = SERVER_TYPE_SLAVE;
|
||||
UpstreamManager::upstream_add_server("abc.local", "backup01.test.ted.gd.sogou", &address_params);//backup for group 1002
|
||||
address_params.server_type = 1;
|
||||
address_params.group_id = -1;
|
||||
UpstreamManager::upstream_add_server("abc.local", "test.sogou.com:8080", &address_params);//slave for no group mean slave for all group and no group
|
||||
UpstreamManager::upstream_add_server("abc.local", "abc.sogou.com");//master, no group
|
||||
UpstreamManager::upstream_add_server("abc.local", "test.sogou.com:8080", &address_params);//backup for no group mean backup for all group and no group
|
||||
UpstreamManager::upstream_add_server("abc.local", "abc.sogou.com");//main, no group
|
||||
|
||||
auto *http_task = WFTaskFactory::create_http_task("http://abc.local/service/method", 0, 0, nullptr);
|
||||
http_task->start();
|
||||
~~~
|
||||
基本原理
|
||||
1. 组号-1代表无组,这种目标不属于任何组
|
||||
2. 无组的master之间是平等的,甚至可以视为同一个组。但与有组的master之间是隔离的
|
||||
3. 无组的slave可以为全局任何组目标/任何无组目标作为备
|
||||
2. 无组的main之间是平等的,甚至可以视为同一个组。但与有组的main之间是隔离的
|
||||
3. 无组的backup可以为全局任何组目标/任何无组目标作为备
|
||||
4. 组号可以区分哪些主备是在一起工作的
|
||||
5. 不同组之间的备是相互隔离的,只为本组的master服务
|
||||
6. 添加目标的默认组号-1,type为master
|
||||
5. 不同组之间的备是相互隔离的,只为本组的main服务
|
||||
6. 添加目标的默认组号-1,type为0,表示主节点。
|
||||
|
||||
# Upstream选择策略
|
||||
|
||||
@@ -287,8 +287,6 @@ struct AddressParams
|
||||
unsigned int dns_ttl_min;
|
||||
unsigned int max_fails;
|
||||
unsigned short weight;
|
||||
#define SERVER_TYPE_MASTER 0
|
||||
#define SERVER_TYPE_SLAVE 1
|
||||
int server_type;
|
||||
int group_id;
|
||||
};
|
||||
@@ -299,8 +297,8 @@ static constexpr struct AddressParams ADDRESS_PARAMS_DEFAULT =
|
||||
.dns_ttl_default = 12 * 3600,
|
||||
.dns_ttl_min = 180,
|
||||
.max_fails = 200,
|
||||
.weight = 1, //only for master of UPSTREAM_WEIGHTED_RANDOM
|
||||
.server_type = SERVER_TYPE_MASTER,
|
||||
.weight = 1, //only for main of UPSTREAM_WEIGHTED_RANDOM
|
||||
.server_type = 0,
|
||||
.group_id = -1,
|
||||
};
|
||||
~~~
|
||||
@@ -309,7 +307,7 @@ static constexpr struct AddressParams ADDRESS_PARAMS_DEFAULT =
|
||||
* dns_ttl_default:dns cache中默认的ttl,单位秒,默认12小时,dns cache是针对当前进程的,即进程退出就会消失,配置也仅对当前进程有效
|
||||
* dns_ttl_min:dns最短生效时间,单位秒,默认3分钟,用于在通信失败重试时是否进行重新dns的决策
|
||||
* max_fails:触发熔断的【连续】失败次数(注:每次通信成功,计数会清零)
|
||||
* weight:权重,默认1,仅对master有效,用于Upstream随机策略选取,权重越大越容易被选中;其他策略下此参数无意义
|
||||
* weight:权重,默认1,仅对main有效,用于Upstream随机策略选取,权重越大越容易被选中;其他策略下此参数无意义
|
||||
* server_type:主备配置,默认主。无论什么时刻,同组的主优先级永远高于其他的备
|
||||
* group_id:分组依据,默认-1。-1代表无分组(游离),游离的备可视为任何主的备,有组的备优先级永远高于游离的备。
|
||||
|
||||
@@ -343,8 +341,8 @@ Address在熔断期间,一旦被策略选中,Upstream会根据具体配置
|
||||
2. 一致性哈希策略,全部目标都处于熔断期
|
||||
3. 手动策略 && try_another==true,全部目标都处于熔断期
|
||||
4. 手动策略 && try_another==false,且同时满足下面三个条件:
|
||||
1). select函数选中的master处于熔断期,,且游离的备都处于熔断期
|
||||
2). 这个master是游离的主,或者这个master所在的group其他目标都处于熔断期
|
||||
1). select函数选中的main处于熔断期,,且游离的备都处于熔断期
|
||||
2). 这个main是游离的主,或者这个main所在的group其他目标都处于熔断期
|
||||
3). 所有游离的备都处于熔断期
|
||||
|
||||
# Upstream端口优先级
|
||||
|
||||
@@ -96,7 +96,7 @@ public:
|
||||
struct rb_node rb;
|
||||
std::mutex mutex;
|
||||
std::vector<UpstreamAddress *> masters;
|
||||
std::vector<UpstreamAddress *> slaves;
|
||||
std::vector<UpstreamAddress *> backups;
|
||||
struct list_head breaker_list;
|
||||
std::atomic<int> nbreak;
|
||||
std::atomic<int> nalive;
|
||||
@@ -115,7 +115,7 @@ public:
|
||||
}
|
||||
|
||||
const UpstreamAddress *get_one();
|
||||
const UpstreamAddress *get_one_slave();
|
||||
const UpstreamAddress *get_one_backup();
|
||||
};
|
||||
|
||||
class Upstream
|
||||
@@ -173,28 +173,28 @@ const UpstreamAddress *UpstreamGroup::get_one()
|
||||
return master;
|
||||
}
|
||||
|
||||
std::random_shuffle(this->slaves.begin(), this->slaves.end());
|
||||
for (const auto *slave : this->slaves)
|
||||
std::random_shuffle(this->backups.begin(), this->backups.end());
|
||||
for (const auto *backup : this->backups)
|
||||
{
|
||||
if (slave->fail_count < slave->params.max_fails)
|
||||
return slave;
|
||||
if (backup->fail_count < backup->params.max_fails)
|
||||
return backup;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const UpstreamAddress *UpstreamGroup::get_one_slave()
|
||||
const UpstreamAddress *UpstreamGroup::get_one_backup()
|
||||
{
|
||||
if (this->nalive == 0)
|
||||
return NULL;
|
||||
|
||||
std::lock_guard<std::mutex> lock(this->mutex);
|
||||
|
||||
std::random_shuffle(this->slaves.begin(), this->slaves.end());
|
||||
for (const auto *slave : this->slaves)
|
||||
std::random_shuffle(this->backups.begin(), this->backups.end());
|
||||
for (const auto *backup : this->backups)
|
||||
{
|
||||
if (slave->fail_count < slave->params.max_fails)
|
||||
return slave;
|
||||
if (backup->fail_count < backup->params.max_fails)
|
||||
return backup;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
@@ -283,7 +283,7 @@ void Upstream::lose_one_server(UpstreamGroup *group, const UpstreamAddress *ua)
|
||||
if (--group->nalive == 0 && ua->params.group_id >= 0)
|
||||
available_weight_ -= group->weight;
|
||||
|
||||
if (ua->params.group_id < 0 && ua->params.server_type == SERVER_TYPE_MASTER)
|
||||
if (ua->params.group_id < 0 && ua->params.server_type == 0)
|
||||
available_weight_ -= ua->params.weight;
|
||||
}
|
||||
|
||||
@@ -292,7 +292,7 @@ void Upstream::gain_one_server(UpstreamGroup *group, const UpstreamAddress *ua)
|
||||
if (group->nalive++ == 0 && ua->params.group_id >= 0)
|
||||
available_weight_ += group->weight;
|
||||
|
||||
if (ua->params.group_id < 0 && ua->params.server_type == SERVER_TYPE_MASTER)
|
||||
if (ua->params.group_id < 0 && ua->params.server_type == 0)
|
||||
available_weight_ += ua->params.weight;
|
||||
}
|
||||
|
||||
@@ -325,7 +325,7 @@ int Upstream::add(UpstreamAddress *ua)
|
||||
rb_insert_color(&group->rb, &group_map_);
|
||||
}
|
||||
|
||||
if (ua->params.server_type == SERVER_TYPE_MASTER)
|
||||
if (ua->params.server_type == 0)
|
||||
{
|
||||
total_weight_ += ua->params.weight;
|
||||
masters_.push_back(ua);
|
||||
@@ -334,13 +334,13 @@ int Upstream::add(UpstreamAddress *ua)
|
||||
group->mutex.lock();
|
||||
gain_one_server(group, ua);
|
||||
ua->group = group;
|
||||
if (ua->params.server_type == SERVER_TYPE_MASTER)
|
||||
if (ua->params.server_type == 0)
|
||||
{
|
||||
group->weight += ua->params.weight;
|
||||
group->masters.push_back(ua);
|
||||
}
|
||||
else
|
||||
group->slaves.push_back(ua);
|
||||
group->backups.push_back(ua);
|
||||
|
||||
group->mutex.unlock();
|
||||
|
||||
@@ -359,13 +359,13 @@ int Upstream::del(const std::string& address)
|
||||
auto *group = ua->group;
|
||||
std::vector<UpstreamAddress *> *vec;
|
||||
|
||||
if (ua->params.server_type == SERVER_TYPE_MASTER)
|
||||
if (ua->params.server_type == 0)
|
||||
{
|
||||
total_weight_ -= ua->params.weight;
|
||||
vec = &group->masters;
|
||||
}
|
||||
else
|
||||
vec = &group->slaves;
|
||||
vec = &group->backups;
|
||||
|
||||
std::lock_guard<std::mutex> lock(group->mutex);
|
||||
|
||||
@@ -373,7 +373,7 @@ int Upstream::del(const std::string& address)
|
||||
if (ua->fail_count < ua->params.max_fails)
|
||||
lose_one_server(group, ua);
|
||||
|
||||
if (ua->params.server_type == SERVER_TYPE_MASTER)
|
||||
if (ua->params.server_type == 0)
|
||||
group->weight -= ua->params.weight;
|
||||
|
||||
for (auto it = vec->begin(); it != vec->end(); ++it)
|
||||
@@ -679,7 +679,7 @@ const UpstreamAddress *Upstream::get(const ParsedURI& uri)
|
||||
}
|
||||
|
||||
if (!ua)
|
||||
ua = default_group_->get_one_slave();//get one slave from group[-1]
|
||||
ua = default_group_->get_one_backup();//get one backup from group[-1]
|
||||
|
||||
return ua;
|
||||
}
|
||||
|
||||
@@ -58,11 +58,9 @@ struct AddressParams
|
||||
* - If max_fails is set to 1, it means server would out of upstream selection in 30 seconds when failed only once
|
||||
*/
|
||||
unsigned int max_fails; ///< [1, INT32_MAX] max_fails = 0 means max_fails = 1
|
||||
unsigned short weight; ///< [1, 65535] weight = 0 means weight = 1. only for master
|
||||
#define SERVER_TYPE_MASTER 0
|
||||
#define SERVER_TYPE_SLAVE 1
|
||||
int server_type; ///< default is SERVER_TYPE_MASTER
|
||||
int group_id; ///< -1 means no group. Slave without group will backup for any master
|
||||
unsigned short weight; ///< [1, 65535] weight = 0 means weight = 1. only for main server
|
||||
int server_type; ///< 0 for main and 1 for backup
|
||||
int group_id; ///< -1 means no group. Backup without group will be backup for any main
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -75,7 +73,7 @@ static constexpr struct AddressParams ADDRESS_PARAMS_DEFAULT =
|
||||
.dns_ttl_min = 180,
|
||||
.max_fails = 200,
|
||||
.weight = 1,
|
||||
.server_type = SERVER_TYPE_MASTER,
|
||||
.server_type = 0, /* 0 for main and 1 for backup. */
|
||||
.group_id = -1,
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user