Java Code Examples for org.apache.zookeeper.ZooKeeper#getSessionId()
The following examples show how to use
org.apache.zookeeper.ZooKeeper#getSessionId() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZkTestBase.java From helix with Apache License 2.0 | 6 votes |
protected void simulateSessionExpiry(ZkConnection zkConnection) throws IOException, InterruptedException { ZooKeeper oldZookeeper = zkConnection.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(zkConnection.getServers(), oldZookeeper.getSessionTimeout(), watcher, oldZookeeper.getSessionId(), oldZookeeper.getSessionPasswd()); LOG.info("New sessionId = " + newZookeeper.getSessionId()); // Thread.sleep(3000); newZookeeper.close(); Thread.sleep(10000); oldZookeeper = zkConnection.getZookeeper(); LOG.info("After session expiry sessionId = " + oldZookeeper.getSessionId()); }
Example 2
Source File: ThriftTransactionServerTest.java From phoenix-tephra with Apache License 2.0 | 6 votes |
private void expireZkSession(ZKClientService zkClientService) throws Exception { ZooKeeper zooKeeper = zkClientService.getZooKeeperSupplier().get(); final SettableFuture<?> connectFuture = SettableFuture.create(); Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.SyncConnected) { connectFuture.set(null); } } }; // Create another Zookeeper session with the same sessionId so that the original one expires. ZooKeeper dupZookeeper = new ZooKeeper(zkClientService.getConnectString(), zooKeeper.getSessionTimeout(), watcher, zooKeeper.getSessionId(), zooKeeper.getSessionPasswd()); connectFuture.get(30, TimeUnit.SECONDS); Assert.assertEquals("Failed to re-create current session", dupZookeeper.getState(), ZooKeeper.States.CONNECTED); dupZookeeper.close(); }
Example 3
Source File: KillZKSession.java From twill with Apache License 2.0 | 6 votes |
/** * Kills a Zookeeper client to simulate failure scenarious during testing. * Callee will provide the amount of time to wait before it's considered failure * to kill a client. * * @param client that needs to be killed. * @param connectionString of Quorum * @param maxMs time in millisecond specifying the max time to kill a client. * @throws IOException When there is IO error * @throws InterruptedException When call has been interrupted. */ public static void kill(ZooKeeper client, String connectionString, int maxMs) throws IOException, InterruptedException { final CountDownLatch latch = new CountDownLatch(1); ZooKeeper zk = new ZooKeeper(connectionString, maxMs, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.SyncConnected) { latch.countDown(); } } }, client.getSessionId(), client.getSessionPasswd()); try { Preconditions.checkState(latch.await(maxMs, TimeUnit.MILLISECONDS), "Fail to kill ZK connection."); } finally { zk.close(); } }
Example 4
Source File: TestZooKeeperClient.java From distributedlog with Apache License 2.0 | 6 votes |
private void expireZooKeeperSession(ZooKeeper zk, int timeout) throws IOException, InterruptedException, KeeperException { final CountDownLatch latch = new CountDownLatch(1); ZooKeeper newZk = new ZooKeeper(zkServers, timeout, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getType() == EventType.None && event.getState() == KeeperState.SyncConnected) { latch.countDown(); } }}, zk.getSessionId(), zk.getSessionPasswd()); if (!latch.await(timeout, TimeUnit.MILLISECONDS)) { throw KeeperException.create(KeeperException.Code.CONNECTIONLOSS); } newZk.close(); }
Example 5
Source File: LocalZooKeeperConnectionService.java From pulsar with Apache License 2.0 | 6 votes |
public static String createIfAbsent(ZooKeeper zk, String path, byte[] data, CreateMode createMode, boolean gc) throws KeeperException, InterruptedException { String pathCreated = null; try { pathCreated = zk.create(path, data, Ids.OPEN_ACL_UNSAFE, createMode); } catch (NodeExistsException e) { // OK LOG.debug("Create skipped for existing znode: path={}", path); } // reset if what exists is the ephemeral garbage. if (gc && (pathCreated == null) && CreateMode.EPHEMERAL.equals(createMode)) { Stat stat = zk.exists(path, false); if (stat != null && zk.getSessionId() != stat.getEphemeralOwner()) { deleteIfExists(zk, path, -1); pathCreated = zk.create(path, data, Ids.OPEN_ACL_UNSAFE, createMode); } } return pathCreated; }
Example 6
Source File: ConnectionState.java From xian with Apache License 2.0 | 5 votes |
/** * Return the current session id */ public long getSessionId() { long sessionId = 0; try { ZooKeeper zk = zooKeeper.getZooKeeper(); if (zk != null) { sessionId = zk.getSessionId(); } } catch (Exception e) { // Ignore the exception } return sessionId; }
Example 7
Source File: ZkTestHelper.java From helix with Apache License 2.0 | 5 votes |
/** * 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())); }
Example 8
Source File: TestZooKeeperClient.java From distributedlog with Apache License 2.0 | 5 votes |
@Test(timeout = 60000) public void testZooKeeperReconnection() throws Exception { int sessionTimeoutMs = 100; ZooKeeperClient zkc = clientBuilder(sessionTimeoutMs).zkAclId(null).build(); ZooKeeper zk = zkc.get(); long sessionId = zk.getSessionId(); ZooKeeperClientUtils.expireSession(zkc, zkServers, 2 * sessionTimeoutMs); ZooKeeper newZk = zkc.get(); while (!ZooKeeper.States.CONNECTED.equals(newZk.getState())) { TimeUnit.MILLISECONDS.sleep(sessionTimeoutMs / 2); } long newSessionId = newZk.getSessionId(); assertTrue(newZk == zk); assertFalse(sessionId == newSessionId); }
Example 9
Source File: ZkTestHelper.java From helix with Apache License 2.0 | 5 votes |
/** * 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())); }
Example 10
Source File: TestZooKeeperClient.java From distributedlog with Apache License 2.0 | 5 votes |
@Test(timeout = 60000) public void testZooKeeperReconnection() throws Exception { int sessionTimeoutMs = 100; ZooKeeperClient zkc = clientBuilder(sessionTimeoutMs).zkAclId(null).build(); ZooKeeper zk = zkc.get(); long sessionId = zk.getSessionId(); ZooKeeperClientUtils.expireSession(zkc, zkServers, 2 * sessionTimeoutMs); ZooKeeper newZk = zkc.get(); while (!ZooKeeper.States.CONNECTED.equals(newZk.getState())) { TimeUnit.MILLISECONDS.sleep(sessionTimeoutMs / 2); } long newSessionId = newZk.getSessionId(); assertTrue(newZk == zk); assertFalse(sessionId == newSessionId); }
Example 11
Source File: ZkUtils.java From pulsar with Apache License 2.0 | 5 votes |
/** * Check if the provided <i>path</i> exists or not and wait it expired if possible. * * @param zk the zookeeper client instance * @param path the zookeeper path * @param sessionTimeoutMs session timeout in milliseconds * @return true if path exists, otherwise return false * @throws KeeperException when failed to access zookeeper * @throws InterruptedException interrupted when waiting for znode to be expired */ public static boolean checkNodeAndWaitExpired(ZooKeeper zk, String path, long sessionTimeoutMs) throws KeeperException, InterruptedException { final CountDownLatch prevNodeLatch = new CountDownLatch(1); Watcher zkPrevNodeWatcher = watchedEvent -> { // check for prev node deletion. if (EventType.NodeDeleted == watchedEvent.getType()) { prevNodeLatch.countDown(); } }; Stat stat = zk.exists(path, zkPrevNodeWatcher); if (null != stat) { // if the ephemeral owner isn't current zookeeper client // wait for it to be expired if (stat.getEphemeralOwner() != zk.getSessionId()) { log.info("Previous znode : {} still exists, so waiting {} ms for znode deletion", path, sessionTimeoutMs); if (!prevNodeLatch.await(sessionTimeoutMs, TimeUnit.MILLISECONDS)) { throw new NodeExistsException(path); } else { return false; } } return true; } else { return false; } }
Example 12
Source File: TestReadOnlyZKClient.java From hbase with Apache License 2.0 | 5 votes |
@Test public void testSessionExpire() throws Exception { assertArrayEquals(DATA, RO_ZK.get(PATH).get()); ZooKeeper zk = RO_ZK.zookeeper; long sessionId = zk.getSessionId(); UTIL.getZkCluster().getZooKeeperServers().get(0).closeSession(sessionId); // should not reach keep alive so still the same instance assertSame(zk, RO_ZK.zookeeper); byte[] got = RO_ZK.get(PATH).get(); assertArrayEquals(DATA, got); assertNotNull(RO_ZK.zookeeper); assertNotSame(zk, RO_ZK.zookeeper); assertNotEquals(sessionId, RO_ZK.zookeeper.getSessionId()); }
Example 13
Source File: ZkTestBase.java From helix with Apache License 2.0 | 4 votes |
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()); }
Example 14
Source File: ZkTestHelper.java From helix with Apache License 2.0 | 4 votes |
public static void expireSession(RealmAwareZkClient client) throws Exception { final CountDownLatch waitNewSession = new CountDownLatch(1); final ZkClient zkClient = (ZkClient) client; IZkStateListener listener = new IZkStateListener() { @Override public void handleStateChanged(KeeperState state) throws Exception { LOG.info("IZkStateListener#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); waitNewSession.countDown(); } @Override public void handleSessionEstablishmentError(Throwable var1) throws Exception { } }; 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.assertFalse(newSessionId.equals(oldSessionId), "Fail to expire current session, zk: " + curZookeeper); }
Example 15
Source File: ZkTestHelper.java From helix with Apache License 2.0 | 4 votes |
/** * 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())); }
Example 16
Source File: ZkTestHelper.java From helix with Apache License 2.0 | 4 votes |
public static void expireSession(RealmAwareZkClient client) throws Exception { final CountDownLatch waitNewSession = new CountDownLatch(1); final ZkClient zkClient = (ZkClient) client; IZkStateListener listener = new IZkStateListener() { @Override public void handleStateChanged(KeeperState state) throws Exception { LOG.info("IZkStateListener#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); waitNewSession.countDown(); } @Override public void handleSessionEstablishmentError(Throwable var1) throws Exception { } }; 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.assertFalse(newSessionId.equals(oldSessionId), "Fail to expire current session, zk: " + curZookeeper); }
Example 17
Source File: HBaseTestingUtility.java From hbase with Apache License 2.0 | 4 votes |
/** * Expire a ZooKeeper session as recommended in ZooKeeper documentation * http://hbase.apache.org/book.html#trouble.zookeeper * There are issues when doing this: * [1] http://www.mail-archive.com/[email protected]/msg01942.html * [2] https://issues.apache.org/jira/browse/ZOOKEEPER-1105 * * @param nodeZK - the ZK watcher to expire * @param checkStatus - true to check if we can create a Table with the * current configuration. */ public void expireSession(ZKWatcher nodeZK, boolean checkStatus) throws Exception { Configuration c = new Configuration(this.conf); String quorumServers = ZKConfig.getZKQuorumServersString(c); ZooKeeper zk = nodeZK.getRecoverableZooKeeper().getZooKeeper(); byte[] password = zk.getSessionPasswd(); long sessionID = zk.getSessionId(); // Expiry seems to be asynchronous (see comment from P. Hunt in [1]), // so we create a first watcher to be sure that the // event was sent. We expect that if our watcher receives the event // other watchers on the same machine will get is as well. // When we ask to close the connection, ZK does not close it before // we receive all the events, so don't have to capture the event, just // closing the connection should be enough. ZooKeeper monitor = new ZooKeeper(quorumServers, 1000, new org.apache.zookeeper.Watcher(){ @Override public void process(WatchedEvent watchedEvent) { LOG.info("Monitor ZKW received event="+watchedEvent); } } , sessionID, password); // Making it expire ZooKeeper newZK = new ZooKeeper(quorumServers, 1000, EmptyWatcher.instance, sessionID, password); //ensure that we have connection to the server before closing down, otherwise //the close session event will be eaten out before we start CONNECTING state long start = System.currentTimeMillis(); while (newZK.getState() != States.CONNECTED && System.currentTimeMillis() - start < 1000) { Thread.sleep(1); } newZK.close(); LOG.info("ZK Closed Session 0x" + Long.toHexString(sessionID)); // Now closing & waiting to be sure that the clients get it. monitor.close(); if (checkStatus) { getConnection().getTable(TableName.META_TABLE_NAME).close(); } }
Example 18
Source File: DefaultZKClientService.java From twill with Apache License 2.0 | 4 votes |
@Override public Long getSessionId() { ZooKeeper zk = zooKeeper.get(); return zk == null ? null : zk.getSessionId(); }
Example 19
Source File: TephraZKClientService.java From phoenix-tephra with Apache License 2.0 | 4 votes |
@Override public Long getSessionId() { ZooKeeper zk = zooKeeper.get(); return zk == null ? null : zk.getSessionId(); }
Example 20
Source File: ZkTestHelper.java From helix with Apache License 2.0 | 4 votes |
/** * 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())); }