nginx學習 - ip_hash的hash算法


直接看代碼:

162     for ( ;; ) {
163 
164         for (i = 0; i < 3; i++) {  
165             hash = (hash * 113 + iphp->addr[i]) % 6271;  //iphp->addr[i]為ip的點分十進制法的第i段
166         }
167 
168         p = hash % iphp->rrp.peers->number;
169 
170         n = p / (8 * sizeof(uintptr_t));
171         m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t));
172 
173         if (!(iphp->rrp.tried[n] & m)) {
174 
175             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
176                            "get ip hash peer, hash: %ui %04XA", p, m);
177 
178             peer = &iphp->rrp.peers->peer[p];
179 
180             /* ngx_lock_mutex(iphp->rrp.peers->mutex); */
181 
182             if (!peer->down) {
183 
184                 if (peer->max_fails == 0 || peer->fails < peer->max_fails) {
185                     break;
186                 }
187 
188                 if (now - peer->accessed > peer->fail_timeout) {
189                     peer->fails = 0;
190                     break;
191                 }
192             }
193 
194             iphp->rrp.tried[n] |= m;
195 
196             /* ngx_unlock_mutex(iphp->rrp.peers->mutex); */
197 
198             pc->tries--;
199         }
200 
201         if (++iphp->tries >= 20) {
202             return iphp->get_rr_peer(pc, &iphp->rrp);
203         }
204     }

 主要代碼在紅色三行。

1、for循環 i取 012三個值,而ip的點分十進制表示方法將ip分成四段(如:192.168.1.1),但是這里循環時只是將ip的前三個端作為參數加入hash函數。這樣做的目的是保證ip地址前三位相同的用戶經過hash計算將分配到相同的后端server。

作者的這個考慮是極為可取的,因此ip地址前三位相同通常意味着來着同一個局域網或者相鄰區域,使用相同的后端服務讓nginx在一定程度上更具有一致性。

2、哈希函數:hash = (hash * 113 + iphp->addr[i]) % 6271

作者使用了這樣一個極為簡單的hash函數,當然目的是為了性能。而這樣一個hash函數的效果如何呢?我們來測試一下。

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const double per = 0.5; //閾值,后端server命中個數與平均值偏離超過該比例則輸出相關信息

int main() {
  srand((unsigned)time(NULL));
  int peer_number; //后端server節點數
  peer_number= rand() % 6271 + 1; //隨機產生
  int* result = new int[peer_number]; //存放每個后端server命中次數
  for(int i = 0; i < peer_number; i++) { //初始化
    result[i] = 0;
  }

  int total_num = 1000000;  //進行hash的總次數
  int total_num_temp = total_num;
  while(total_num_temp-->0) {
    int rand_num[3];
    for(int i = 0; i < 3; i++) {
      rand_num[i]= rand()%255;  //隨機生成三個數作為ip地址前三段
      //        cout << i << ": " << rand_num[i] <<endl;
    }
    int hash = 89;
    for(int i = 0; i < 3; i++) {
      hash = (hash * 113 + rand_num[i]) % 6271; //hash運算
    }
    hash = hash % peer_number;
    result[hash]++; //統計hash值命中
  }

  // 設定一個閾值per,當每個server命中次數與平均值偏差超過該比例時記錄。
  int avg = total_num/peer_number; 
  int max =(int) ((double)avg * (1 + per));
  int min =(int) ((double)avg * (1 - per));
  for(int i = 0; i < peer_number; i++) {
    //cout<<i<<": "<<result[i]<< endl;
    if (result[i] > max || result[i] < min){
      for(int j = 0; j < peer_number; j++) {
        cout<<j<<": "<<result[j]<< endl;
      }
      cout << "avg: " << avg << ", max: " << max << ", min:  " <<
        min << ", i: " << i << ", result[i]: " << result[i] <<endl;
      cout << peer_number << endl;
      return 1;
    }
  }
  delete []result;
}

通過上面比較粗糙的代碼,可以驗證,該hash函數的效果並不是很好,產生的的分布並不是太均衡。但這在nginx選擇后端server這樣的應用場景已經足夠,關鍵是其簡單性。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM