IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    nginx模块分析(59)—upstream一致性hash模块

    cjhust发表于 2017-04-27 16:55:26
    love 0

    1、知识百科

    当后端是缓存服务器时,经常使用一致性hash算法来进行负载均衡,使用一致性hash的好处在于,增减集群的缓存服务器时,只有少量的缓存会失效,回源量较小。

    假设后端集群包含三台缓存服务器,A、B、C。

    请求r1、r2落在A上。

    请求r3、r4落在B上。

    请求r5、r6落在C上。

    使用一致性哈希时,当缓存服务器B宕机时,r1/r2会仍然落在A上,r5/r6会仍然落在C上,也就是说这两台服务器上的缓存都不会失效,r3/r4会被重新分配给A或者C,并产生回源。

    2、数据结构

    ngx_http_upstream_chash_point_t

    typedef struct {

        uint32_t                            hash;                           //虚拟节点的hash值

        ngx_str_t                          *server;                       //虚拟节点归属的真实节点

    } ngx_http_upstream_chash_point_t;

     

    ngx_http_upstream_chash_points_t

    typedef struct {

        ngx_uint_t                          number;                    //虚拟节点的个数

        ngx_http_upstream_chash_point_t     point[1];   //虚拟节点的数组

    } ngx_http_upstream_chash_points_t;


    ngx_http_upstream_hash_srv_conf_t

    typedef struct {

        ngx_http_complex_value_t            key;              //组合key,用于计算请求的hash值

        ngx_http_upstream_chash_points_t   *points;    //一致性hash,total_weight *160

    } ngx_http_upstream_hash_srv_conf_t;


    ngx_http_upstream_hash_peer_data_t

    typedef struct {

        /* the round robin data must be first */

        ngx_http_upstream_rr_peer_data_t    rrp;

        ngx_http_upstream_hash_srv_conf_t  *conf;

        ngx_str_t                           key;                           //存放组合变量的值

        ngx_uint_t                          tries;

    uint32_t                            hash;                       //根据key计算出来的hash值

    } ngx_http_upstream_hash_peer_data_t;

    3、源码分析

    ngx_http_upstream_hash_create_conf(ngx_conf_t *cf)

    函数功能:分配空间。


    ngx_http_upstream_hash(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)

    函数功能:处理hash指令,这里只分析hash $uri consistent,即一致性hash模块。

    uscf->peer.init_upstream = ngx_http_upstream_init_chash; 


    ngx_http_upstream_init_chash(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)

    函数功能:srv_init(),配置init upstream。


    ngx_http_upstream_get_chash_peer(ngx_peer_connection_t *pc, void *data)

    函数功能:根据一致性hash算法,找到后端的peer。


    4、测试验证

    基础环境:

    样本10000条URI,去重后8640。

     

    测试目标:

    一致性hash算法

     

    命中率算法:

    根据样本生成uri和后端的映射关系,如uri /a的后端是9001。

    调整配置后,如果uri /a的后端还是9001,则HIT,否则MISS。

     

    基准配置:

        server 127.0.0.1:9001;

        server 127.0.0.1:9002;

        server 127.0.0.1:9003;

        server 127.0.0.1:9004;

        server 127.0.0.1:9005;

        server 127.0.0.1:9006;

        server 127.0.0.1:9007;

        server 127.0.0.1:9008;

        server 127.0.0.1:9009;

    server 127.0.0.1:9010;

    对比测试

    nginx模块分析(59)—upstream一致性hash模块 - cjhust - 我一直在努力

    5、参考资料

    nginx官方说明:

    http://nginx.org/en/docs/http/ngx_http_upstream_module.html

    http://nginx.org/en/docs/http/ngx_http_proxy_module.html

    负载均衡模块特性:

    https://www.nginx.com/resources/admin-guide/load-balancer/

    一致性hash模块分析:

    http://blog.csdn.net/zhangskd/article/details/50256111

    一致性hash算法介绍:

    https://www.codeproject.com/Articles/56138/Consistent-hashing



沪ICP备19023445号-2号
友情链接