Search in sources :

Example 1 with ZkConnection

use of org.apache.helix.zookeeper.zkclient.ZkConnection in project helix by apache.

the class ZkTestBase method simulateSessionExpiry.

protected void simulateSessionExpiry(HelixZkClient client) throws IOException, InterruptedException, IOException {
    ZkClient zkClient = (ZkClient) client;
    IZkStateListener listener = new IZkStateListener() {

        @Override
        public void handleStateChanged(Watcher.Event.KeeperState state) throws Exception {
            LOG.info("In Old connection, state changed:" + state);
        }

        @Override
        public void handleNewSession(final String sessionId) throws Exception {
            LOG.info("In Old connection, new session: {}.", sessionId);
        }

        @Override
        public void handleSessionEstablishmentError(Throwable var1) throws Exception {
        }
    };
    zkClient.subscribeStateChanges(listener);
    ZkConnection connection = ((ZkConnection) zkClient.getConnection());
    ZooKeeper oldZookeeper = connection.getZookeeper();
    LOG.info("Old sessionId = " + oldZookeeper.getSessionId());
    Watcher watcher = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            LOG.info("In New connection, process event:" + event);
        }
    };
    ZooKeeper newZookeeper = new ZooKeeper(connection.getServers(), oldZookeeper.getSessionTimeout(), watcher, oldZookeeper.getSessionId(), oldZookeeper.getSessionPasswd());
    LOG.info("New sessionId = " + newZookeeper.getSessionId());
    // Thread.sleep(3000);
    newZookeeper.close();
    Thread.sleep(10000);
    connection = (ZkConnection) zkClient.getConnection();
    oldZookeeper = connection.getZookeeper();
    LOG.info("After session expiry sessionId = " + oldZookeeper.getSessionId());
}
Also used : ZkClient(org.apache.helix.zookeeper.impl.client.ZkClient) HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) IZkStateListener(org.apache.helix.zookeeper.zkclient.IZkStateListener) Watcher(org.apache.zookeeper.Watcher) ZkConnection(org.apache.helix.zookeeper.zkclient.ZkConnection)

Example 2 with ZkConnection

use of org.apache.helix.zookeeper.zkclient.ZkConnection in project helix by apache.

the class ZkTestHelper method asyncExpireSession.

/**
 * expire zk session asynchronously
 * @param client
 * @throws Exception
 */
public static void asyncExpireSession(RealmAwareZkClient client) throws Exception {
    final ZkClient zkClient = (ZkClient) client;
    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()));
}
Also used : HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient) ZkClient(org.apache.helix.zookeeper.zkclient.ZkClient) WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) ZkConnection(org.apache.helix.zookeeper.zkclient.ZkConnection)

Example 3 with ZkConnection

use of org.apache.helix.zookeeper.zkclient.ZkConnection in project helix by apache.

the class ZkTestHelper method disconnectSession.

/**
 * Expire current zk session and wait for {@link IZkStateListener#handleNewSession(String)} invoked
 * @param client
 * @throws Exception
 */
public static void disconnectSession(HelixZkClient client) throws Exception {
    final ZkClient zkClient = (ZkClient) client;
    IZkStateListener listener = new IZkStateListener() {

        @Override
        public void handleStateChanged(KeeperState state) throws Exception {
        // System.err.println("disconnectSession handleStateChanged. state: " + state);
        }

        @Override
        public void handleNewSession(final String sessionId) throws Exception {
            // make sure zkclient is connected again
            zkClient.waitUntilConnected(HelixZkClient.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS);
            LOG.info("handleNewSession. sessionId: {}.", sessionId);
        }

        @Override
        public void handleSessionEstablishmentError(Throwable var1) throws Exception {
        }
    };
    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()));
}
Also used : HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient) ZkClient(org.apache.helix.zookeeper.zkclient.ZkClient) WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) IZkStateListener(org.apache.helix.zookeeper.zkclient.IZkStateListener) Watcher(org.apache.zookeeper.Watcher) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) ZkConnection(org.apache.helix.zookeeper.zkclient.ZkConnection)

Example 4 with ZkConnection

use of org.apache.helix.zookeeper.zkclient.ZkConnection in project helix by apache.

the class ZkTestHelper method asyncExpireSession.

/**
 * expire zk session asynchronously
 * @param client
 * @throws Exception
 */
public static void asyncExpireSession(RealmAwareZkClient client) throws Exception {
    final ZkClient zkClient = (ZkClient) client;
    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()));
}
Also used : HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient) ZkClient(org.apache.helix.zookeeper.zkclient.ZkClient) WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) ZkConnection(org.apache.helix.zookeeper.zkclient.ZkConnection)

Example 5 with ZkConnection

use of org.apache.helix.zookeeper.zkclient.ZkConnection in project helix by apache.

the class ZkTestHelper method disconnectSession.

/**
 * Expire current zk session and wait for {@link IZkStateListener#handleNewSession(String)} invoked
 * @param client
 * @throws Exception
 */
public static void disconnectSession(HelixZkClient client) throws Exception {
    final ZkClient zkClient = (ZkClient) client;
    IZkStateListener listener = new IZkStateListener() {

        @Override
        public void handleStateChanged(KeeperState state) throws Exception {
        // System.err.println("disconnectSession handleStateChanged. state: " + state);
        }

        @Override
        public void handleNewSession(final String sessionId) throws Exception {
            // make sure zkclient is connected again
            zkClient.waitUntilConnected(HelixZkClient.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS);
            LOG.info("handleNewSession. sessionId: {}.", sessionId);
        }

        @Override
        public void handleSessionEstablishmentError(Throwable var1) throws Exception {
        }
    };
    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()));
}
Also used : HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient) ZkClient(org.apache.helix.zookeeper.zkclient.ZkClient) WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) IZkStateListener(org.apache.helix.zookeeper.zkclient.IZkStateListener) Watcher(org.apache.zookeeper.Watcher) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) ZkConnection(org.apache.helix.zookeeper.zkclient.ZkConnection)

Aggregations

ZkConnection (org.apache.helix.zookeeper.zkclient.ZkConnection)13 ZooKeeper (org.apache.zookeeper.ZooKeeper)12 HelixZkClient (org.apache.helix.zookeeper.api.client.HelixZkClient)10 Watcher (org.apache.zookeeper.Watcher)10 RealmAwareZkClient (org.apache.helix.zookeeper.api.client.RealmAwareZkClient)8 ZkClient (org.apache.helix.zookeeper.zkclient.ZkClient)8 WatchedEvent (org.apache.zookeeper.WatchedEvent)8 IZkStateListener (org.apache.helix.zookeeper.zkclient.IZkStateListener)6 KeeperState (org.apache.zookeeper.Watcher.Event.KeeperState)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 HashMap (java.util.HashMap)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 ZkClientException (org.apache.helix.zookeeper.exception.ZkClientException)2 ManagementFactory (java.lang.management.ManagementFactory)1 Random (java.util.Random)1 UUID (java.util.UUID)1 ExecutorService (java.util.concurrent.ExecutorService)1