xylz,imxylz

          關注后端架構、中間件、分布式和并發(fā)編程

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            111 隨筆 :: 10 文章 :: 2680 評論 :: 0 Trackbacks

          Session連接

          Zookeeper客戶端和服務端維持一個長連接,每隔10s向服務端發(fā)送一個心跳,服務端返回客戶端一個響應。這就是一個Session連接,擁有全局唯一的session id。Session連接通常是一直有效,如果因為網(wǎng)絡原因斷開了連接,客戶端會使用相同的session id進行重連。由于服務端保留了session的各種狀態(tài),尤其是各種瞬時節(jié)點是否刪除依賴于session是否失效。

          Session失效問題

          通常客戶端主動關閉連接認為是一次session失效。另外也有可能因為其它未知原因,例如網(wǎng)絡超時導致的session失效問題。在服務端看來,無法區(qū)分session失效是何種情況,一次一旦發(fā)生session失效,一定時間后就會將session持有的所有watcher以及瞬時節(jié)點刪除。

          而對于Zookeeper客戶端而言,一旦發(fā)生失效不知道是否該重連,這涉及到watcher和瞬時節(jié)點問題,因此Zookeeper客戶端認為,一旦發(fā)生了seesion失效,那么就認為客戶端死掉了。從而所有操作都不能夠進行。參考 How should I handle SESSION_EXPIRED?

          解決方案

          對于只是簡單查詢服務的客戶端而言,session失效后只需要重新建立連接即可。而對于需要處理瞬時節(jié)點以及各種watcher的服務來說,應用程序需要處理session失效或者重連帶來的副作用。

          下面的邏輯提供了一種簡單的解決session重連的問題,這是指重新生成新的連接。

          public static org.apache.zookeeper.ZooKeeper getZooKeeper() {
            if (zookeeper == null) {
              synchronized (ZookeeperClient.class) {
                if (zookeeper == null) {
                  latch = new CountDownLatch(1);
                  zookeeper = buildClient();//如果失敗,下次還有成功的機會
                  long startTime = System.currentTimeMillis();
                  try {
                     latch.await(30, TimeUnit.SECONDS);
                  } catch (InterruptedException e) {
                     e.printStackTrace();
                  } finally {
                    final SocketAddress remoteAddres = zookeeper.testableRemoteSocketAddress();
                    System.out.println("[SUC-CORE] local host: " + zookeeper.testableLocalSocketAddress());
                    System.out.println("[SUC-CORE] remote host: " + remoteAddres);
                    System.out.println("[SUC-CORE] zookeeper session id: " + zookeeper.getSessionId());
                    final String remoteHost = remoteAddres != null ? ((InetSocketAddress) remoteAddres).getAddress().getHostAddress() : "";
                    System.out.println("[SUC-CORE] init cost: " + (System.currentTimeMillis() - startTime) + "(ms) " + remoteHost);
                    latch = null;
                  }
                }
              }
            }
            return zookeeper;
          }

          上面的代碼只是簡單的使用一個Double-Check來維持Zookeeper客戶端的單實例。保證總是有機會重建客戶端以及只有一個單例(這是因為Zookeeper客戶端是線程安全的)。

          例外上面的例子中繼承了org.apache.zookeeper.ZooKeeper類,以便能夠拿到session對于的服務端ip地址以及客戶端地址,方便調試問題。

          在構建客戶端的時候是需要設置session超時的時間,例如下面的代碼就是30秒。


          private static ZooKeeper buildClient() {
              final String rootPath = getRootPath();
              final String connectString = SystemConfig.getInstance().getString("zookeeper.ips",//
                      "192.168.10.1:2181,192.168.10.2:2181,192.168.10.3:2181,192.168.10.4:2181,192.168.10.5:2181");
              System.out.printf("[SUC-CORE] rootPath: %1s\n", rootPath);
              System.out.printf("[SUC-CORE] connectString:%1s\n", connectString);
              try {
                  return new ZooKeeper(connectString + rootPath, 30000, new SessionWatcher());
              } catch (IOException e) {
                  throw new RuntimeException("init zookeeper fail.", e);
              }
          }

          為了處理連接建立成功以及斷開問題,我們需要一個Watcher來處理此問題。

          static class SessionWatcherimplements Watcher {

              public void process(WatchedEvent event) {
                  if (event.getState() == KeeperState.SyncConnected) {
                      if (latch != null) {
                          latch.countDown();
                      }
                  } else if (event.getState() == KeeperState.Expired) {
                      System.out.println("[SUC-CORE] session expired. now rebuilding");

                      //session expired, may be never happending.
                      
          //close old client and rebuild new client
                      close();

                      getZooKeeper();
                  }
              }
          }


          一旦檢測到session失效了(Expired),那么久銷毀已經建立的客戶端實例,重新生成一個客戶端實例。

          /**
           * 關閉zookeeper連接,釋放資源
           
          */
          public static void close() {
              System.out.println("[SUC-CORE] close");
              if (zookeeper != null) {
                  try {
                      zookeeper.close();
                      zookeeper = null;
                  } catch (InterruptedException e) {
                      //ignore exception
                  }
              }
          }

          這是一種簡單的處理Session expired問題的方法,顯然這不會處理瞬時節(jié)點的問題,因此如果有相關的需求,業(yè)務系統(tǒng)(應用程序)需要自己修復問題。

          測試方案

          根據(jù)Is there an easy way to expire a session for testing? 提供的測試方法,寫一個簡單的例子試驗下。

          public static void main(String[] args) throws Exception {
            ZooKeeper zk = ZookeeperClient.getZooKeeper();
            long sessionId = zk.getSessionId();
            //
            final String rootPath = ZookeeperClient.getRootPath();
            final String connectString = SystemConfig.getInstance().getString("zookeeper.ips",//
                    "192.168.10.1:2181,192.168.10.2:2181,192.168.10.3:2181,192.168.10.4:2181,192.168.10.5:2181");
            // close the old connection
            new ZooKeeper(connectString + rootPath, 30000, null, sessionId, null).close();
            Thread.sleep(10000L);
            //

            
          // rebuild a new session
            long newSessionid = ZookeeperClient.getZooKeeper().getSessionId();

            // check the new session
            String status = newSessionid != sessionId ? "OK" : "FAIL";
            System.out.println(format("%s --> %s %s", sessionId, newSessionid, status));

            // close the client
            ZookeeperClient.getZooKeeper().close();
          }

          最后能夠自動處理session expired的問題。實驗中看到確實執(zhí)行了session expired的邏輯重新生成了新的session。



          ©2009-2014 IMXYLZ |求賢若渴
          posted on 2011-12-05 13:57 imxylz 閱讀(28637) 評論(8)  編輯  收藏 所屬分類: J2EE技術

          評論

          # re: 處理Zookeeper的session過期問題 2011-12-06 11:03 TBW
          xiexie !這個東西我還是要學的  回復  更多評論
            

          # re: 處理Zookeeper的session過期問題 2011-12-07 17:09 雪地靴
          源碼好復雜,轉載下。  回復  更多評論
            

          # re: 處理Zookeeper的session過期問題 2011-12-15 00:31 Tai
          艷兄永遠活在我們心中  回復  更多評論
            

          # re: 處理Zookeeper的session過期問題 2012-09-24 16:54 凌波微步
          每隔10s向服務端發(fā)送一個心跳
          說的不對!  回復  更多評論
            

          # re: 處理Zookeeper的session過期問題[未登錄] 2014-04-11 09:36 XX
          @凌波微步
          是的,這里不是固定10S。  回復  更多評論
            

          # re: 處理Zookeeper的session過期問題[未登錄] 2014-05-09 16:31 w
          學習學習  回復  更多評論
            

          # re: 處理Zookeeper的session過期問題 2014-08-07 18:52 iteblog
          寫的不錯,但是有個地方錯誤:每隔10s向服務端發(fā)送一個心跳
          這是不對的。多久向Zookeeper Server發(fā)送心跳和設置的Session有關。  回復  更多評論
            

          # re: 處理Zookeeper的session過期問題 2016-06-17 03:02 codewarrior
          "而對于Zookeeper客戶端而言,一旦發(fā)生失效不知道是否該重連",這句理解不對吧,Zookeeper wiki上明確寫了,當客戶端斷開后,客戶端無法得到SESSION_EXPIRED通知,因為鏈接斷了怎么發(fā)送消息?而自動重連是ZK client library自己進行的。換言之客戶端是無法知道自己是否失效的,SESSION_EXPIRED通知只有當重連成功后才能收到。  回復  更多評論
            


          ©2009-2014 IMXYLZ
          主站蜘蛛池模板: 房山区| 高要市| 太湖县| 化州市| 桂阳县| 北安市| 博湖县| 忻州市| 枣阳市| 会东县| 固原市| 博野县| 涟水县| 绥棱县| 天祝| 彰化市| 大城县| 祁连县| 于田县| 理塘县| 洛阳市| 湖口县| 台北县| 玉林市| 密山市| 贺兰县| 阳信县| 双辽市| 承德县| 罗山县| 长沙市| 三河市| 桐梓县| 张家川| 蚌埠市| 宁国市| 湘阴县| 黔江区| 惠来县| 黄梅县| 湖口县|