Esempio n. 1
1
  /**
   * expire zk session asynchronously
   *
   * @param zkClient
   * @throws Exception
   */
  public static void asyncExpireSession(final ZkClient zkClient) throws Exception {
    ZkConnection connection = ((ZkConnection) zkClient.getConnection());
    ZooKeeper curZookeeper = connection.getZookeeper();
    LOG.info("Before expiry. sessionId: " + Long.toHexString(curZookeeper.getSessionId()));

    Watcher watcher =
        new Watcher() {
          @Override
          public void process(WatchedEvent event) {
            LOG.info("Process watchEvent: " + event);
          }
        };

    final ZooKeeper dupZookeeper =
        new ZooKeeper(
            connection.getServers(),
            curZookeeper.getSessionTimeout(),
            watcher,
            curZookeeper.getSessionId(),
            curZookeeper.getSessionPasswd());
    // wait until connected, then close
    while (dupZookeeper.getState() != States.CONNECTED) {
      Thread.sleep(10);
    }
    dupZookeeper.close();

    connection = (ZkConnection) zkClient.getConnection();
    curZookeeper = connection.getZookeeper();

    // System.err.println("zk: " + oldZookeeper);
    LOG.info("After expiry. sessionId: " + Long.toHexString(curZookeeper.getSessionId()));
  }
Esempio n. 2
0
  public static void disconnectSession(final ZkClient zkClient) throws Exception {
    IZkStateListener listener =
        new IZkStateListener() {
          @Override
          public void handleStateChanged(KeeperState state) throws Exception {
            // System.err.println("disconnectSession handleStateChanged. state: " + state);
          }

          @Override
          public void handleNewSession() throws Exception {
            // make sure zkclient is connected again
            zkClient.waitUntilConnected();

            ZkConnection connection = ((ZkConnection) zkClient.getConnection());
            ZooKeeper curZookeeper = connection.getZookeeper();

            LOG.info(
                "handleNewSession. sessionId: " + Long.toHexString(curZookeeper.getSessionId()));
          }
        };

    zkClient.subscribeStateChanges(listener);
    ZkConnection connection = ((ZkConnection) zkClient.getConnection());
    ZooKeeper curZookeeper = connection.getZookeeper();
    LOG.info("Before expiry. sessionId: " + Long.toHexString(curZookeeper.getSessionId()));

    Watcher watcher =
        new Watcher() {
          @Override
          public void process(WatchedEvent event) {
            LOG.info("Process watchEvent: " + event);
          }
        };

    final ZooKeeper dupZookeeper =
        new ZooKeeper(
            connection.getServers(),
            curZookeeper.getSessionTimeout(),
            watcher,
            curZookeeper.getSessionId(),
            curZookeeper.getSessionPasswd());
    // wait until connected, then close
    while (dupZookeeper.getState() != States.CONNECTED) {
      Thread.sleep(10);
    }
    dupZookeeper.close();

    connection = (ZkConnection) zkClient.getConnection();
    curZookeeper = connection.getZookeeper();
    zkClient.unsubscribeStateChanges(listener);

    // System.err.println("zk: " + oldZookeeper);
    LOG.info("After expiry. sessionId: " + Long.toHexString(curZookeeper.getSessionId()));
  }
Esempio n. 3
0
  /**
   * Expire current zk session and wait for {@link IZkStateListener#handleNewSession()} invoked
   *
   * @param zkClient
   * @throws Exception
   */
  public static void expireSession(final ZkClient zkClient) throws Exception {
    final CountDownLatch waitNewSession = new CountDownLatch(1);

    IZkStateListener listener =
        new IZkStateListener() {
          @Override
          public void handleStateChanged(KeeperState state) throws Exception {
            LOG.info("IZkStateListener#handleStateChanged, state: " + state);
          }

          @Override
          public void handleNewSession() throws Exception {
            // make sure zkclient is connected again
            zkClient.waitUntilConnected();

            ZkConnection connection = ((ZkConnection) zkClient.getConnection());
            ZooKeeper curZookeeper = connection.getZookeeper();

            LOG.info(
                "handleNewSession. sessionId: " + Long.toHexString(curZookeeper.getSessionId()));
            waitNewSession.countDown();
          }
        };

    zkClient.subscribeStateChanges(listener);

    ZkConnection connection = ((ZkConnection) zkClient.getConnection());
    ZooKeeper curZookeeper = connection.getZookeeper();
    String oldSessionId = Long.toHexString(curZookeeper.getSessionId());
    LOG.info("Before session expiry. sessionId: " + oldSessionId + ", zk: " + curZookeeper);

    Watcher watcher =
        new Watcher() {
          @Override
          public void process(WatchedEvent event) {
            LOG.info("Watcher#process, event: " + event);
          }
        };

    final ZooKeeper dupZookeeper =
        new ZooKeeper(
            connection.getServers(),
            curZookeeper.getSessionTimeout(),
            watcher,
            curZookeeper.getSessionId(),
            curZookeeper.getSessionPasswd());
    // wait until connected, then close
    while (dupZookeeper.getState() != States.CONNECTED) {
      Thread.sleep(10);
    }
    Assert.assertEquals(
        dupZookeeper.getState(),
        States.CONNECTED,
        "Fail to connect to zk using current session info");
    dupZookeeper.close();

    // make sure session expiry really happens
    waitNewSession.await();
    zkClient.unsubscribeStateChanges(listener);

    connection = (ZkConnection) zkClient.getConnection();
    curZookeeper = connection.getZookeeper();

    String newSessionId = Long.toHexString(curZookeeper.getSessionId());
    LOG.info("After session expiry. sessionId: " + newSessionId + ", zk: " + curZookeeper);
    Assert.assertNotSame(
        newSessionId, oldSessionId, "Fail to expire current session, zk: " + curZookeeper);
  }