xylz,imxylz

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

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

          Session連接

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

          Session失效問題

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

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

          解決方案

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

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

          public static org.apache.zookeeper.ZooKeeper getZooKeeper() {
            if (zookeeper == null) {
              synchronized (ZookeeperClient.class) {
                if (zookeeper == null) {
                  latch = new CountDownLatch(1);
                  zookeeper = buildClient();//如果失敗,下次還有成功的機(jī)會
                  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客戶端的單實例。保證總是有機(jī)會重建客戶端以及只有一個單例(這是因為Zookeeper客戶端是線程安全的)。

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

          在構(gòu)建客戶端的時候是需要設(shè)置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),那么久銷毀已經(jīng)建立的客戶端實例,重新生成一個客戶端實例。

          /**
           * 關(guān)閉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é)點(diǎn)的問題,因此如果有相關(guān)的需求,業(yè)務(wù)系統(tǒng)(應(yīng)用程序)需要自己修復(fù)問題。

          測試方案

          根據(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 閱讀(28645) 評論(8)  編輯  收藏 所屬分類: J2EE技術(shù)

          評論

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

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

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

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

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

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

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

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


          ©2009-2014 IMXYLZ
          主站蜘蛛池模板: 阿拉善左旗| 通城县| 青铜峡市| 乾安县| 泰兴市| 同德县| 繁峙县| 尼木县| 临夏县| 台中市| 崇文区| 沁源县| 长白| 兴义市| 通山县| 明水县| 饶河县| 景宁| 威海市| 漳平市| 文化| 岳阳县| 杭锦后旗| 忻城县| 赤城县| 饶平县| 东光县| 岑溪市| 铁岭县| 亚东县| 漠河县| 兰考县| 历史| 土默特右旗| 慈利县| 乐山市| 惠水县| 象州县| 新邵县| 长葛市| 高邮市|