算法介紹
當后端是緩存服務器時,經常使用一致性哈希算法來進行負載均衡。
使用一致性哈希的好處在於,增減集群的緩存服務器時,只有少量的緩存會失效,回源量較小。
在nginx+ats / haproxy+squid等CDN架構中,nginx/haproxy所使用的負載均衡算法便是一致性哈希。
我們舉個例子來說明一致性哈希的好處。
假設后端集群包含三台緩存服務器,A、B、C。
請求r1、r2落在A上。
請求r3、r4落在B上。
請求r5、r6落在C上。
使用一致性哈希時,當緩存服務器B宕機時,r1/r2會仍然落在A上,r5/r6會仍然落在C上,
也就是說這兩台服務器上的緩存都不會失效。r3/r4會被重新分配給A或者C,並產生回源。
使用其它算法,當緩存服務器B宕機時,r1/r2不再落在A上,r5/r6不再落在C上了。
也就是說A、B、C上的緩存都失效了,所有的請求都要回源。
這里不介紹一致性哈希算法的基本原理,如果不了解,先花個10分鍾看下這篇文章:
http://www.codeproject.com/Articles/56138/Consistent-hashing
在分析模塊代碼之前,先來看下nginx所實現的一致性哈希算法。
1. 初始化upstream塊
主要工作是創建和初始化真實節點、創建和初始化虛擬節點。
其中真實節點是使用round robin的方法創建的。
Q:總共有多少個虛擬節點,一個真實節點對應多少個虛擬節點?
累加真實節點的權重,算出總的權重值total_weight,虛擬節點的個數一般為total_weight * 160。
一個權重為weight的真實節點,對應的虛擬節點數為weight * 160。
Q:對於每一個真實節點,是如何創建其對應的虛擬節點的?
1. 真實節點的server成員是其server指令的第一個參數,首先把它解析為HOST和PORT。
base_hash = crc32(HOST 0 PORT)
一個真實節點對應weight * 160個虛擬節點,對於每個虛擬節點來說,base_hash都是一樣的。
2. 為了使每個虛擬節點的hash值都不同,又引入了PREV_HASH,它是上一個虛擬節點的hash值。
hash = crc32(base_hash PREV_HASH)
3. 虛擬節點的server成員,指向真實節點的server成員。如此一來,通過比較虛擬節點和真實節點的
server成員是否相同,可以判斷它們是否是相對應的。
創建和初始化好虛擬節點數組后,對其中的虛擬節點按照hash值進行排序,對於hash值相同的虛擬節點,只保留第一個。
經過上述步驟,我們得到一個所有虛擬節點組成的數組,其元素的hash值有序而不重復。也就是說,ring建立起來了。
2. 初始話請求的負載均衡數據
根據hash指令第一個參數的實時值KEY,KEY一般是$host$uri之類的,計算出本次請求的哈希值。
hash = crc32(KEY)
根據請求的哈希值,在虛擬節點數組中,找到“順時針方向”最近的一個虛擬節點,其索引為i。
什么叫順時針方向最近?就是point[i - 1].hash < hash <= point[i].hash。
本次請求就落在該虛擬節點上了,之后交由其對應的真實節點來處理。
3. 選取真實節點
在peer.init中,已經知道請求落在哪個虛擬節點上了。
在peer.get中,需要查找虛擬節點對應的真實節點。
根據虛擬節點的server成員,在真實節點數組中查找server成員相同的、可用的真實節點。
如果找不到,那么沿着順時針方向,繼續查找下一個虛擬節點對應的真實節點。
如果找到了一個,那么就是它了。
如果找到了多個,使用輪詢的方法從中選取一個。
4. 缺陷和改進
一個虛擬節點和一個真實節點,是依據它們的server成員來關聯的。
這會出現一種情況,一個虛擬節點對應了多個真實節點,因為:
如果server指令的第一個參數為域名,可能解析為多個真實節點,那么這些真實節點的server成員都是一樣的。
對於一個請求,計算其KEY的hash值,順時針找到最近的虛擬節點后,發現該虛擬節點對應了多個真實節點。
使用哪個真實節點呢?本模塊就使用輪詢的方法,來從多個真實節點中選一個。
但我們知道使用一致性哈希的場景中,真實節點一般是緩存服務器。
一個虛擬節點對應多個真實節點,會導致一個文件被緩存在多個緩存服務器上。
這會增加磁盤的使用量,以及回源量,顯然不是我們希望看到的。
解決這個問題的方法其實很簡單,就是虛擬節點和真實節點通過name成員來建立關聯。
因為就算對應同一條server配置,server的第一個參數為域名,各個真實節點的name成員也是唯一的。
這樣一來,找到了一個虛擬節點,就能找到一個唯一的真實節點,不會有上述問題了。
數據結構
1. 真實節點
就是采用round robin算法所創建的后端服務器,類型為ngx_http_upstream_rr_peer_t。
需要注意的是,如果server指令的第一個參數是IP和端口,那么一條server指令只對應一個真實節點。
如果server指令的第一個參數是域名,一條server指令可能對應多個真實節點。
它們的server成員是相同的,可以通過name成員區分。
- struct ngx_http_upstream_rr_peer_s {
- struct sockaddr *sockaddr; /* 后端服務器的地址 */
- socklen_t socklen; /* 地址的長度*/
- ngx_str_t name; /* 后端服務器地址的字符串,server.addrs[i].name */
- ngx_str_t server; /* server的名稱,server.name */
- ngx_int_t current_weight; /* 當前的權重,動態調整,初始值為0 */
- ngx_int_t effective_weight; /* 有效的權重,會因為失敗而降低 */
- ngx_int_t weight; /* 配置項指定的權重,固定值 */
- ngx_uint_t conns; /* 當前連接數 */
- ngx_uint_t fails; /* "一段時間內",已經失敗的次數 */
- time_t accessed; /* 最近一次失敗的時間點 */
- time_t checked; /* 用於檢查是否超過了"一段時間" */
- ngx_uint_t max_fails; /* "一段時間內",最大的失敗次數,固定值 */
- time_t fail_timeout; /* "一段時間"的值,固定值 */
- ngx_uint_t down; /* 服務器永久不可用的標志 */
- ...
- ngx_http_upstream_rr_peer_t *next; /* 指向下一個后端,用於構成鏈表 */
- ...
- } ngx_http_upstream_rr_peer_t;
ngx_http_upstream_rr_peers_t表示一組后端服務器,比如一個后端集群。
- struct ngx_http_upstream_rr_peers_s {
- ngx_uint_t number; /* 后端服務器的數量 */
- ...
- ngx_uint_t total_weight; /* 所有后端服務器權重的累加值 */
- unsigned single:1; /* 是否只有一台后端服務器 */
- unsigned weighted:1; /* 是否使用權重 */
- ngx_str_t *name; /* upstream配置塊的名稱 */
- ngx_http_upstream_rr_peers_t *next; /* backup服務器集群 */
- ngx_http_upstream_rr_peer_t *peer; /* 后端服務器組成的鏈表 */
- };
2. 虛擬節點
一個真實節點,一般會對應weight * 160個虛擬節點。
虛擬節點的server成員,指向它所歸屬的真實節點的server成員,如此一來找到了一個虛擬節點后,
就能找到其歸屬的真實節點。
但這里有一個問題,通過一個虛擬節點的server成員,可能會找到多個真實節點,而不是一個。
因為如果server指令的第一個參數為域名,那么多個真實節點的server成員都是一樣的。
- typedef struct {
- uint32_t hash; /* 虛擬節點的哈希值 */
- ngx_str_t *server; /* 虛擬節點歸屬的真實節點,對應真實節點的server成員 */
- } ngx_http_upstream_chash_point_t;
- typedef struct {
- ngx_uint_t number; /* 虛擬節點的個數 */
- ngx_http_upstream_chash_point_t point[1]; /* 虛擬節點的數組 */
- } ngx_http_upstream_chash_points_t;
- typedef struct {
- ngx_http_complex_value_t key; /* 關聯hash指令的第一個參數,用於計算請求的hash值 */
- ngx_http_upstream_chash_points_t *points; /* 虛擬節點的數組 */
- } ngx_http_upstream_chash_points_t;
3. 請求的一致性哈希數據
- typedef struct {
- /* the round robin data must be first */
- ngx_http_upstream_rr_peer_data_t rrp; /* round robin的per request負載均衡數據 */
- ngx_http_upstream_hash_srv_conf_t *conf; /* server配置塊 */
- ngx_str_t key; /* 對於本次請求,hash指令的第一個參數的具體值,用於計算本次請求的哈希值 */
- ngx_uint_t tries; /* 已經嘗試的虛擬節點數 */
- ngx_uint_t rehash; /* 本算法不使用此成員 */
- uint32_t hash; /* 根據請求的哈希值,找到順時方向最近的一個虛擬節點,hash為該虛擬節點在數組中的索引 */
- ngx_event_get_peer_pt get_rr_peer; /* round robin算法的peer.get函數 */
- } ngx_http_upstream_hash_peer_data_t;
round robin的per request負載均衡數據。
- typedef struct {
- ngx_http_upstream_rr_peers_t *peers; /* 后端集群 */
- ngx_http_upstream_rr_peer_t *current; /* 當前使用的后端服務器 */
- uintptr_t *tried; /* 指向后端服務器的位圖 */
- uintptr_t data; /* 當后端服務器的數量較少時,用於存放其位圖 */
- } ngx_http_upstream_rr_peer_data_t;
指令的解析函數
在一個upstream配置塊中,如果有hash指令,且它只帶一個參數,則使用的負載均衡算法為哈希算法,比如:
hash $host$uri;
在一個upstream配置塊中,如果有hash指令,且它帶了兩個參數,且第二個參數為consistent,則使用的
負載均衡算法為一致性哈希算法,比如:
hash $host$uri consistent;
這說明hash指令所屬的模塊ngx_http_upstream_hash_module同時實現了兩種負載均衡算法,而實際上
哈希算法、一致性哈希算法完全可以用兩個獨立的模塊來實現,它們本身並沒有多少關聯。
哈希算法的實現比較簡單,類似之前分析過的ip_hash,接下來分析的是一致性哈希算法。
hash指令的解析函數主要做了:
把hash指令的第一個參數,關聯到一個ngx_http_complex_value_t變量,之后可以通過該變量獲取參數的實時值。
指定此upstream塊中server指令支持的屬性。
根據hash指令攜帶的參數來判斷是使用哈希算法,還是一致性哈希算法。如果hash指令的第二個參數為"consistent",
則表示使用一致性哈希算法,指定upstream塊的初始化函數uscf->peer.init_upstream。
- static char *ngx_http_upstream_hash(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
- {
- ngx_http_upstream_hash_srv_conf_t *hcf = conf;
- ngx_str_t *value;
- ngx_http_upstream_srv_conf_t *uscf;
- ngx_http_compile_complex_value_t ccv;
- value = cf->args->elts;
- ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
- /* 把hash指令的第一個參數,關聯到一個ngx_http_complex_value_t變量,
- * 之后可以通過該變量獲取參數的實時值。
- */
- ccv.cf = conf;
- ccv.value = &value[1];
- ccv.complex_value = &hcf->key;
- if (ngx_http_compile_complex_value(&ccv) != NGX_OK)
- return NGX_CONF_ERROR;
- /* 獲取所在的upstream{}塊 */
- uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
- if (uscf->peer.init_upstream)
- ngx_conf_log_error(NGX_LOG_WARN, cf, 0, "load balancing method redefined");
- /* 指定此upstream塊中server指令支持的屬性 */
- uscf->flags = NGX_HTTP_UPSTREAM_CREATE
- | NGX_HTTP_UPSTREAM_WEIGHT
- | NGX_HTTP_UPSTREAM_MAX_FAILS
- | NGX_HTTP_UPSTREAM_FAIL_TIMEOUT
- | NGX_HTTP_UPSTREAM_DOWN;
- /* 根據hash指令攜帶的參數來判斷是使用哈希算法,還是一致性哈希算法。
- * 每種算法都有自己的upstream塊初始化函數。
- */
- if (cf->args->nelts == 2)
- uscf->peer.init_upstream = ngx_http_upstream_init_hash;
- else if (ngx_strcmp(value[2].data, "consistent") == 0)
- uscf->peer.init_upstream = ngx_http_upstream_init_chash;
- else
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"", &value[2]);
- return NGX_CONF_OK;
- }
初始化upstream塊
執行完指令的解析函數后,緊接着會調用所有HTTP模塊的init main conf函數。
在執行ngx_http_upstream_module的init main conf函數時,會調用所有upstream塊的初始化函數。
對於使用一致性哈希的upstream塊,其初始化函數(peer.init_upstream)就是上一步中指定
ngx_http_upstream_init_chash,它主要做了:
調用round robin的upstream塊初始化函數來創建和初始化真實節點
指定per request的負載均衡初始化函數peer.init
創建和初始化虛擬節點數組,使該數組中的虛擬節點有序而不重復
- static ngx_int_t ngx_http_upstream_init_chash(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
- {
- u_char *host, *port, c;
- size_t host_len, port_len, size;
- uint32_t hash, base_hash;
- ngx_str_t *server;
- ngx_uint_t npoints, i, j;
- ngx_http_upstream_rr_peer_t *peer;
- ngx_http_upstream_rr_peers_t *peers;
- ngx_http_upstream_chash_points_t *points;
- ngx_http_upstream_hash_srv_conf_t *hcf;
- union {
- uint32_t value;
- u_char byte[4];
- } prev_hash;
- /* 使用round robin的upstream塊初始化函數,創建和初始化真實節點 */
- if (ngx_http_upstream_init_round_robin(cf, us) != NGX_OK)
- return NGX_ERROR:
- /* 重新設置per request的負載均衡初始化函數 */
- us->peer.init = ngx_http_upstream_init_chash_peer;
- peers = us->peer.data; /* 真實節點的集群 */
- npoints = peers->total_weight * 160;
- /* 一共創建npoints個虛擬節點 */
- size = sizeof(ngx_http_upstream_chash_points_t) +
- sizeof(ngx_http_upstream_chash_point_t) * (npoints - 1);
- points = ngx_palloc(cf->pool, size);
- if (points == NULL)
- return NGX_ERROR;
- points->number = 0;
- /* 初始化所有的虛擬節點 */
- for (peer = peers->peer; peer; peer = peer->next) {
- server = &peer->server; /* server指令的第一個參數, server.name */
- /* Hash expression is compatible with Cache::Memcached::Fast:
- * crc32(HOST 0 PORT PREV_HASH).
- */
- if (server->len >= 5 && ngx_strncasecmp(server->data, (u_char *) "unix:", 5) == 0)
- {
- host = server->data + 5;
- host_len = server->len - 5;
- port = NULL;
- port_len = 0;
- goto done;
- }
- /* 把每個peer的server成員,解析為HOST和PORT */
- for (j = 0; j < server->len; j++) {
- c = server->data[server->len - j - 1];
- if (c == ":") {
- host = server->data;
- host_len = server->len - j - 1;
- port = server->data + server->len - j;
- port_len = j;
- goto done;
- }
- if (c < '0' || c > '9') /* 表示沒有指定端口 */
- break;
- }
- host = server->data;
- host_len = server->len;
- port = NULL;
- port_len = 0;
- done:
- /* 根據解析peer的server成員所得的HOST和PORT,計算虛擬節點的base_hash值 */
- ngx_crc32_init(base_hash);
- ngx_crc32_update(&base_hash, host, host_len);
- ngx_crc32_update(&base_hash, (u_char *) "", 1); /* 空字符串包含字符\0 */
- ngx_crc32_update(&base_hash, port, port_len);
- /* 對於歸屬同一個真實節點的虛擬節點,它們的base_hash值相同,而prev_hash不同 */
- prev_hash.value = 0;
- npoints = peer->weight * 160;
- for (j = 0; j < npoints; j++) {
- hash = base_hash;
- ngx_crc32_update(&hash, prev_hash.byte, 4);
- ngx_crc32_final(hash);
- points->point[points->number].hash = hash; /* 虛擬節點的哈希值 */
- points->point[points->number].server = server; /* 虛擬節點所歸屬的真實節點,對應真實節點的server成員 */
- points->number++;
- #if (NGX_HAVE_LITTLE_ENDIAN)
- prev_hash.value = hash;
- #else
- prev_hash.byte[0] = (u_char) (hash & 0xff);
- prev_hash.byte[1] = (u_char) ((hash >> 8) & 0xff);
- prev_hash.byte[2] = (u_char) ((hash >> 16) & 0xff);
- prev_hash.byte[3] = (u_char) ((hash >> 24) & 0xff);
- #endif
- }
- }
- /* 使用快速排序,使虛擬節點數組的元素,按照其hash值從小到大有序 */
- ngx_qsort(points->point, points->number, sizeof(ngx_http_upstream_chash_point_t),
- ngx_http_upstream_chash_cmp_points);
- /* 如果虛擬節點數組中,有多個元素的hash值相同,只保留第一個 */
- for (i = 0, j = 1; j < points->number; j++)
- if (points->point[i].hash != points->point[j].hash)
- points->point[++i] = points->point[j];
- /* 經過上述步驟后,虛擬節點數組中的元素,有序而不重復 */
- points->number = i + 1;
- hcf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_hash_module);
- hcf->points = points; /* 保存虛擬節點數組 */
- return NGX_OK;
- }
- static int ngx_libc_cdel ngx_http_upstream_chash_cmp_points(const void *one, const void *two)
- {
- ngx_http_upstream_chash_point_t *first = (ngx_http_upstream_chash_point_t *) one;
- ngx_http_upstream_chash_point_t *second = (ngx_http_upstream_chash_point_t *) two;
- if (first->hash < second->hash)
- return -1;
- else if (first->hash > second->hash)
- return 1;
- else
- return 0;
- }
初始化請求的負載均衡數據
收到一個請求后,一般使用的反向代理模塊(upstream模塊)為ngx_http_proxy_module,
其NGX_HTTP_CONTENT_PHASE階段的處理函數為ngx_http_proxy_handler,在初始化upstream機制的
ngx_http_upstream_init_request函數中,調用在第二步中指定的peer.init,主要用於初始化請求的負載均衡數據。
對於一致性哈希,peer.init實例為ngx_http_upstream_init_chash_peer,主要做了:
首先調用hash算法的per request負載均衡初始化函數,創建和初始化請求的負載均衡數據。
重新指定peer.get,用於選取一個真實節點來處理本次請求。
獲取的本請求對應的hash指令的第一個參數值,計算請求的hash值。
尋找第一個hash值大於等於請求的哈希值的虛擬節點,即尋找“順時針方向最近”的一個虛擬節點。
- static ngx_int_t ngx_http_upstream_init_chash_peer(ngx_http_request_t *r, ngx_http_upstream_srv_conf_t *us)
- {
- uint32_t hash;
- ngx_http_upstream_hash_srv_conf_t *hcf;
- ngx_http_upstream_hash_peer_data_t *hp;
- /* 調用hash算法的per request負載均衡初始化函數,創建和初始化請求的負載均衡數據 */
- if (ngx_http_upstream_init_hash_peer(r, us) != NGX_OK)
- return NGX_ERROR;
- /* 重新指定peer.get,用於選取一個真實節點 */
- r->upstream->peer.get = ngx_http_upstream_get_chash_peer;
- hp = r->upstream->peer.data;
- hcf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_hash_module);
- /* 根據獲取的本請求對應的hash指令的第一個參數值,計算請求的hash值 */
- hash = ngx_crc32_long(hp->key.data, hp->key.len);
- /* 根據請求的hash值,找到順時針方向最近的一個虛擬節點,hp->hash記錄此虛擬節點
- * 在數組中的索引。
- */
- hp->hash = ngx_http_upstream_find_chash_point(hcf->points, hash);
- return NGX_OK:
- }
hash算法的per request負載均衡初始化函數。
- static ngx_int_t ngx_http_upstream_init_hash_peer(ngx_http_request_t *r, ngx_http_upstream_srv_conf_t *us)
- {
- ngx_http_upstream_hash_srv_conf_t *hcf;
- ngx_http_upstream_hash_peer_data_t *hp;
- hp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_hash_peer_data_t));
- if (hp == NULL)
- return NGX_ERROR:
- /* 調用round robin的per request負載均衡初始化函數 */
- r->upstream->peer.data = &hp->rrp;
- if (ngx_http_upstream_init_round_robin_peer(r, us) != NGX_OK)
- return NGX_ERROR;
- r->upstream->peer.get = ngx_http_upstream_get_hash_peer;
- hcf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_hash_module);
- /* 獲取本請求對應的hash指令的第一個參數值,用於計算請求的hash值 */
- if (ngx_http_complex_value(r, &hcf->key, &hp->key) != NGX_OK)
- return NGX_ERROR;
- ...
- hp->conf = hcf;
- hp->tries = 0;
- hp->rehash = 0;
- hp->hash = 0;
- hp->get_rr_peer = ngx_http_upstream_get_round_robin_peer; /* round robin的peer.get函數 */
- return NGX_OK;
- }
我們知道虛擬節點數組是有序的,事先已按照虛擬節點的hash值從小到大排序好了。
現在使用二分查找,尋找第一個hash值大於等於請求的哈希值的虛擬節點,即“順時針方向最近”的一個虛擬節點。
- static ngx_uint_t ngx_http_upstream_find_chash_point(ngx_http_upstream_chash_points_t *points, uint32_t hash)
- {
- ngx_uint_t i, j, k;
- ngx_http_upstream_chash_point_t *point;
- /* find first point >= hash */
- point = &points->point[0];
- i = 0;
- j = points->number;'
- while(i < j) {
- k = (i + j) / 2;
- if (hash > point[k].hash)
- i = k + 1;
- else if (hash < point[k].hash)
- j = k;
- else
- return k;
- }
- return i;
- }
選取一個真實節點
一般upstream塊中會有多個真實節點,那么對於本次請求,要選定哪一個真實節點呢?
對於一致性哈希算法,選取真實節點的peer.get函數為ngx_http_upstream_get_chash_peer。
其實在peer.init中,已經找到了該請求對應的虛擬節點了:
根據請求對應的hash指令的第一個參數值,計算請求的hash值。
尋找第一個哈希值大於等於請求的hash值的虛擬節點,即“順時針方向最近”的一個虛擬節點。
在peer.get中,需查找此虛擬節點對應的真實節點。
根據虛擬節點的server成員,在真實節點數組中查找server成員一樣的且可用的真實節點。
如果找不到,那么沿着順時針方向,繼續查找下一個虛擬節點對應的真實節點。
如果找到一個真實節點,那么就是它了。
如果找到多個真實節點,使用輪詢的方法從中選取一個。
- static ngx_http_upstream_get_chash_peer(ngx_peer_connection_t *pc, void *data)
- {
- ngx_http_upstream_hash_peer_data_t *hp = data; /* 請求的負載均衡數據 */
- time_t now;
- intptr_t m;
- ngx_str_t *server;
- ngx_int_t total;
- ngx_uint_t i, n, best_i;
- ngx_http_upstream_rr_peer_t *peer, *best;
- ngx_http_upstream_chash_point_t *point;
- ngx_http_upstream_chash_points_t *points;
- ngx_http_upstream_hash_srv_conf_t *hcf;
- ...
- pc->cached = 0;
- pc->connection = NULL:
- now = ngx_time();
- hcf = hp->conf;
- points = hcf->points; /* 虛擬節點數組 */
- point = &points->point[0]; /* 指向第一個虛擬節點 */
- for ( ; ; ) {
- /* 在peer.init中,已根據請求的哈希值,找到順時針方向最近的一個虛擬節點,
- * hash為該虛擬節點在數組中的索引。
- * 一開始hash值肯定小於number,之后每嘗試一個虛擬節點后,hash++。取模是為了防止越界訪問。
- */
- server = point[hp->hash % points->number].server;
- best = NULL;
- best_i = 0;
- total = 0;
- /* 遍歷真實節點數組,尋找可用的、該虛擬節點歸屬的真實節點(server成員相同),
- * 如果有多個真實節點同時符合條件,那么使用輪詢來從中選取一個真實節點。
- */
- for (peer = hp->rrp.peers->peer, i = 0; peer; peer = peer->next, i++) {
- /* 檢查此真實節點在狀態位圖中對應的位,為1時表示不可用 */
- n = i / (8 * sizeof(uintptr_t));
- m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
- if (hp->rrp.tried[n] & m)
- continue;
- /* server指令中攜帶了down屬性,表示后端永久不可用 */
- if (peer->down)
- continue;
- /* 如果真實節點的server成員和虛擬節點的不同,表示虛擬節點不屬於此真實節點 */
- if (peer->server.len != server->len ||
- ngx_strncmp(peer->server.data, server->data, server->len) != 0)
- continue;
- /* 在一段時間內,如果此真實節點的失敗次數,超過了允許的最大值,那么不允許使用了 */
- if (peer->max_fails
- && peer->fails >= peer->max_fails
- && now - peer->checked <= peer->fail_timeout)
- continue;
- peer->current_weight += peer->effective_weight; /* 對每個真實節點,增加其當前權重 */
- total += peer->effective_weight; /* 累加所有真實節點的有效權重 */
- /* 如果之前此真實節點發生了失敗,會減小其effective_weight來降低它的權重。
- * 此后又通過增加其effective_weight來恢復它的權重。
- */
- if (peer->effective_weight < peer->weight)
- peer->effective_weight++;
- /* 選取當前權重最大者,作為本次選定的真實節點 */
- if (best == NULL || peer->current_weight > best->current_weight) {
- best = peer;
- best_i = i;
- }
- }
- /* 如果選定了一個真實節點 */
- if (best) {
- best->current_weight -= total; /* 如果使用了輪詢,需要降低選定節點的當前權重 */
- goto found;
- }
- hp->hash++; /* 增加虛擬節點的索引,即“沿着順時針方向” */
- hp->tries++; /* 已經嘗試的虛擬節點數 */
- /* 如果把所有的虛擬節點都嘗試了一遍,還找不到可用的真實節點 */
- if (hp->tries >= points->number)
- return NGX_BUSY;
- }
- found: /* 找到了和虛擬節點相對應的、可用的真實節點了 */
- hp->rrp.current = best; /* 選定的真實節點 */
- /* 保存選定的后端服務器的地址,之后會向這個地址發起連接 */
- pc->sockaddr = peer->sockaddr;
- pc->socklen = peer->socklen;
- pc->name = &peer->name;
- best->conns++;
- /* 更新checked時間 */
- if (now - best->checked > best->fail_timeout)
- best->checked = now;
- n = best_i / (8 * sizeof(uintptr_t));
- m = (uintptr_t) 1 << best_i % (8 * sizeof(uintptr_t));
- /* 對於本次請求,如果之后需要再次選取真實節點,不能再選取同一個了 */
- hp->rrp->tried[n] |= m;
- return NGX_OK;
- }
轉載於:https://my.oschina.net/zhangjie830621/blog/653082