地址:http://wiki.nginx.org/HttpUpstreamConsistentHash
首先声明一个命令:
static ngx_command_t ngx_http_upstream_consistent_hash_commands[] = {
{ ngx_string("consistent_hash"),
NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
ngx_http_upstream_consistent_hash,
0,
0,
NULL },
ngx_null_command
};
看下命令的处理函数:ngx_http_upstream_consistent_hash
ngx_str_t *value;
ngx_http_script_compile_t sc;
ngx_http_upstream_srv_conf_t *uscf;
ngx_http_upstream_consistent_hash_srv_conf_t *uchscf;
value = cf->args->elts;
uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
uchscf = ngx_http_conf_upstream_srv_conf(uscf,
ngx_http_upstream_consistent_hash_module);
ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
sc.cf = cf;
sc.source = &value[1];
sc.lengths = &uchscf->lengths;
sc.values = &uchscf->values;
sc.complete_lengths = 1;
sc.complete_values = 1;
if (ngx_http_script_compile(&sc) != NGX_OK) {
return NGX_CONF_ERROR;
}
uscf->peer.init_upstream = ngx_http_upstream_init_consistent_hash;
uscf->flags = NGX_HTTP_UPSTREAM_CREATE
|NGX_HTTP_UPSTREAM_WEIGHT;
这个模块获取upstream模块的配置,设置了peer.init_upstream为函数ngx_http_upstream_init_consistent_hash,这个函数用来初始化upstream的,ngx_http_upstream_init_consistent_hash(ngx_conf_t *cf,
函数会根据服务器的IP和端口号进行一致性哈希计算,
ngx_int_t
ngx_http_upstream_init_consistent_hash(ngx_conf_t *cf,
ngx_http_upstream_srv_conf_t *us)
{
……
us->peer.init = ngx_http_upstream_init_consistent_hash_peer;
……
for (i = 0; i < us->servers->nelts; i++) {
for (j = 0; j < server[i].naddrs; j++) {
for (k = 0; k < ((MMC_CONSISTENT_POINTS * server[i].weight) / server[i].naddrs); k++) {
ngx_snprintf(hash_data, HASH_DATA_LENGTH, "%V-%ui%Z", &server[i].addrs[j].name, k);
continuum->nodes[continuum->nnodes].sockaddr = server[i].addrs[j].sockaddr;
continuum->nodes[continuum->nnodes].socklen = server[i].addrs[j].socklen;
continuum->nodes[continuum->nnodes].name = server[i].addrs[j].name;
continuum->nodes[continuum->nnodes].name.data[server[i].addrs[j].name.len] = 0;
continuum->nodes[continuum->nnodes].point = ngx_crc32_long(hash_data, ngx_strlen(hash_data));
continuum->nnodes++;
}
}
}
//排序
qsort(continuum->nodes, continuum->nnodes,
sizeof(ngx_http_upstream_consistent_hash_node),
(const void*) ngx_http_upstream_consistent_hash_compare_continuum_nodes);
……
}
可以看到,是根据服务器的IP和端口号及索引做ngx_crc32_long计算;
这里也指定了ngx_http_upstream_init_consistent_hash_peer函数为peer的init函数,这个在每次连接的时候都会执行,作用是根据key来查找服务器,
static ngx_int_t
ngx_http_upstream_init_consistent_hash_peer(ngx_http_request_t *r,
ngx_http_upstream_srv_conf_t *us)
{
……
uchscf = ngx_http_conf_upstream_srv_conf(us,
ngx_http_upstream_consistent_hash_module);
uchpd = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_consistent_hash_peer_data_t));
r->upstream->peer.data = uchpd->peers;
uchpd->peers = us->peer.data;
//编译变量
if (ngx_http_script_run(r, &evaluated_key_to_hash,
uchscf->lengths->elts, 0, uchscf->values->elts) == NULL)
{
return NGX_ERROR;
}
//根据key计算hash值
uchpd->point =
ngx_crc32_long(evaluated_key_to_hash.data, evaluated_key_to_hash.len);
r->upstream->peer.free = ngx_http_upstream_free_consistent_hash_peer;
//获取哪个服务器作计算节点
r->upstream->peer.get = ngx_http_upstream_get_consistent_hash_peer;
……
}