spymemcached :某個mc節點操做連續超時超過998次就 Auto-Reconne...

spymemcached 中,一個 mc client 與某一個 mc node 之間已創建的 connection 上,
若是執行操做屢屢超時(net.spy.memcached.OperationTimeoutException),
那麼,有一個計數器專門記錄超時次數(connected或reconnecting就清零)。
當某一個 mc node 的連續超時次數達到必定閾值(如 998 次),
就會……
  1. 關閉原來的 socket channel,並將該 mc node 放入重連隊列 reconnectQueue :lostConnection(mn),
  2. 遍歷 reconnectQueue ,逐一嘗試從新創建 socket 鏈接:attemptReconnects(),
  3. 而後無論連沒連上,rehash 隊列中的操做:redistributeOperations(retryOps)。(rehash 指從新計算hash值,看是否要鏈接其餘的節點)。
 
這個超時次數閾值,既能夠在 spring 裏以下這樣設置,也能夠採用默認值 998 次:
  <beanid="memcachedClient"class="net.spy.memcached.spring.MemcachedClientFactoryBean"> ……     <propertyname="opTimeout"value="1000"/>操做超時時間是1秒,若是不設置的話,默認是 DEFAULT_OPERATION_TIMEOUT,即2.5秒     <propertyname="timeoutExceptionThreshold"value="1998"/>設置超時次數上限是1998次,默認是DEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLD,即998次 ……   </bean>
 
相關代碼:
每一次 handle io over the connections 時,都會檢測一下這個 connection 對應的節點上登記的「連續超時次數」是否已經達到閾值:
MemcachedConnection.java
  private final int timeoutExceptionThreshold;
……
  timeoutExceptionThreshold = f.getTimeoutExceptionThreshold();
……
  /**
   * MemcachedClient calls this method to handle IO over the connections.
   */
  public void handleIO() throws IOException {
……
    // see if any connections blew up with large number of timeouts
    for (SelectionKey sk : selector.keys()) {
      MemcachedNode mn = (MemcachedNode) sk.attachment();
      if (mn.getContinuousTimeout() > timeoutExceptionThreshold) {//檢查一下
        getLogger().warn("%s exceeded continuous timeout threshold", sk);
        lostConnection(mn);//斷開鏈接,放入重連隊列
      }
    }
 
    if (!shutDown && !reconnectQueue.isEmpty()) {
      attemptReconnects();//從新鏈接
    }
    // rehash any operations that are in retry state
    redistributeOperations(retryOps);//
    retryOps.clear();
 
若是配置文件也沒配 timeoutExceptionThreshold ,初始化時也沒傳這個參數,
那麼 DefaultConnectionFactory 就取默認值 DEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLD,即998次(anyway,也許是1000次)
DefaultConnectionFactory.java
  /**
   * Maximum number + 2 of timeout exception for shutdown connection.
   */
  public static final int DEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLD = 998;
DefaultConnectionFactory.java
  public int getTimeoutExceptionThreshold() {
    return DEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLD;
  }
 
針對每個 memcache node 設置一個「連續超時次數」計數器:
TCPMemcachedNodeImpl.java
  // operation Future.get timeout counter
  private final AtomicInteger continuousTimeout = new AtomicInteger(0);
……
  public void setContinuousTimeout(boolean timedOut) {
    if (timedOut && isActive()) {
      continuousTimeout.incrementAndGet();
    } else {
      continuousTimeout.set(0);
    }
  }
MemcachedConnection.java
  /**
   * helper method: increase timeout count on node attached to this op.
   *
   * @param op
   */
  public static void opTimedOut(Operation op) {
    MemcachedConnection.setTimeout(op, true);
  }
 
 
參考資源:
1)智深,2012, spymemcached源碼深刻分析
2)2011, Issue 173:  Spymemcached fail reconnect to local memcahed under high load ..;
2) 電商課題V:分佈式鎖  (2012-11-17 22:16)
3) 電商課題:cookie防篡改  (2012-11-17 22:24)
4) 電商課題VI:分佈式Session  (2012-11-17 22:30)
5) 電商課題:RBAC權限控制  (2012-11-17 22:47)
6) 電商課題:冪等性  (2012-11-22 23:52)
7) 電商課題:客戶端的IP地址僞造、CDN、反向代理、獲取的那些事兒  (2012-09-19 01:17) 9) 電商課題VII:支付交易通常性準則  (2012-12-14 01:38)
贈圖一枚
http://ww3.sinaimg.cn/bmiddle/61b889f5jw1e0fm79k5fxj.jpg
相關文章
相關標籤/搜索