public synchronized long getSessionId() { return zk == null ? -1 : zk.getSessionId(); }
public String getSessionId() { ZooKeeper current = zk; if (current != null) { return String.format("0x%x", current.getSessionId()); } return ""; }
public String getSessionId() { ZooKeeper current = zk; if (current != null) { return String.format("0x%x", current.getSessionId()); } return ""; }
@VisibleForTesting synchronized long getZKSessionIdForTests() { if (zkClient != null) { return zkClient.getSessionId(); } else { return -1; } }
public synchronized void reconnectAfterExpiration() throws IOException, KeeperException, InterruptedException { if (zk != null) { LOG.info("Closing dead ZooKeeper connection, session" + " was: 0x"+Long.toHexString(zk.getSessionId())); zk.close(); // reset the ZooKeeper connection zk = null; } checkZk(); LOG.info("Recreated a ZooKeeper, session" + " is: 0x"+Long.toHexString(zk.getSessionId())); }
/** * The callbacks and watchers pass a reference to the ZK client * which made the original call. We don't want to take action * based on any callbacks from prior clients after we quit * the election. * @param ctx the ZK client passed into the watcher * @return true if it matches the current client */ private synchronized boolean isStaleClient(Object ctx) { Preconditions.checkNotNull(ctx); if (zkClient != (ZooKeeper)ctx) { LOG.warn("Ignoring stale result from old client with sessionId {}", String.format("0x%08x", ((ZooKeeper)ctx).getSessionId())); return true; } return false; }
/** */ public String getZkSessionId() { if (rtState.zkClient != null && rtState.zkClient.zk() != null) return Long.toHexString(rtState.zkClient.zk().getSessionId()); else return null; } }
LOG.debug("Closing session: 0x" + Long.toHexString(getSessionId())); LOG.info("Session: 0x" + Long.toHexString(getSessionId()) + " closed");
private void verifyUnexpectedBeans(Set<ObjectName> children) { if (allClients != null) { for (ZooKeeper zkc : allClients) { Iterator<ObjectName> childItr = children.iterator(); while (childItr.hasNext()) { ObjectName clientBean = childItr.next(); if (clientBean.toString().contains( getHexSessionId(zkc.getSessionId()))) { LOG.info("found name:" + zkc.getSessionId() + " client bean:" + clientBean.toString()); childItr.remove(); } } } } for (ObjectName bean : children) { LOG.info("unexpected:" + bean.toString()); } Assert.assertEquals("Unexpected bean exists!", 0, children.size()); }
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(); }
private static String createLockNodeV3(ZooKeeper zk, String lockPath, String clientId) throws Exception { return zk.create(getLockPathPrefixV3(lockPath, clientId, zk.getSessionId()), serializeClientId(clientId), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); }
@Test public void directCheckUpgradeSessionTest() throws IOException, InterruptedException, KeeperException { final ZooKeeper zk = createClient(); String path = "/directcheckupgradesession"; zk.create(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); QuorumZooKeeperServer server = getConnectedServer(zk.getSessionId()); Assert.assertNotNull("unable to find server interlocutor", server); Request readRequest = makeGetDataRequest(path, zk.getSessionId()); Request createRequest = makeCreateRequest(path + "/e", zk.getSessionId()); Assert.assertNull("tried to upgrade on a read", server.checkUpgradeSession(readRequest)); Assert.assertNotNull("failed to upgrade on a create", server.checkUpgradeSession(createRequest)); Assert.assertNull("tried to upgrade after successful promotion", server.checkUpgradeSession(createRequest)); }
@Test public void ephemeralCreateMultiOpTest() throws KeeperException, InterruptedException, IOException { final ZooKeeper zk = createClient(); String data = "test"; String path = "/ephemeralcreatemultiop"; zk.create(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); QuorumZooKeeperServer server = getConnectedServer(zk.getSessionId()); Assert.assertNotNull("unable to find server interlocutor", server); UpgradeableSessionTracker sessionTracker = (UpgradeableSessionTracker)server.getSessionTracker(); Assert.assertFalse("session already global", sessionTracker.isGlobalSession(zk.getSessionId())); List<OpResult> multi = null; try { multi = zk.multi(Arrays.asList( Op.setData(path, data.getBytes(), 0), Op.create(path + "/e", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL), Op.create(path + "/p", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), Op.create(path + "/q", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL) )); } catch (KeeperException.SessionExpiredException e) { // the scenario that inspired this unit test Assert.fail("received session expired for a session promotion in a multi-op"); } Assert.assertNotNull(multi); Assert.assertEquals(4, multi.size()); Assert.assertEquals(data, new String(zk.getData(path + "/e", false, null))); Assert.assertEquals(data, new String(zk.getData(path + "/p", false, null))); Assert.assertEquals(data, new String(zk.getData(path + "/q", false, null))); Assert.assertTrue("session not promoted", sessionTracker.isGlobalSession(zk.getSessionId())); }
@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()); }
Assert.assertSame("should be in r/o mode", States.CONNECTEDREADONLY, zk .getState()); long fakeId = zk.getSessionId(); LOG.info("Connected as r/o mode with state {} and session id {}", zk.getState(), fakeId); zk.getState()); LOG.info("Connected as rw mode with state {} and session id {}", zk.getState(), zk.getSessionId()); zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Assert.assertFalse("fake session and real session have same id", zk .getSessionId() == fakeId); zk.close();
@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); }
@Test public void testOnlyUpgradeSessionOnce() throws IOException, InterruptedException, KeeperException { // create a client, and create an ephemeral node to trigger the // upgrading process final String node = "/node-1"; ZooKeeper zk = new ZooKeeper("127.0.0.1:" + clientPorts[0], ClientBase.CONNECTION_TIMEOUT, this); waitForOne(zk, States.CONNECTED); long sessionId = zk.getSessionId(); QuorumZooKeeperServer server = (QuorumZooKeeperServer) mt[0].main.quorumPeer.getActiveServer(); Request create1 = createEphemeralRequest("/data-1", sessionId); Request create2 = createEphemeralRequest("/data-2", sessionId); Assert.assertNotNull("failed to upgrade on a ephemeral create", server.checkUpgradeSession(create1)); Assert.assertNull("tried to upgrade again", server.checkUpgradeSession(create2)); // clean al the setups and close the zk zk.close(); }
watcher.waitForConnected(CONNECTION_TIMEOUT); long localSessionId2 = zk.getSessionId();
Assert.assertEquals(0, stat.getVersion()); Assert.assertEquals(0, stat.getAversion()); Assert.assertEquals(zk.getSessionId(), stat.getEphemeralOwner()); Assert.assertEquals(childname.length(), stat.getDataLength()); Assert.assertEquals(0, stat.getNumChildren());
Assert.assertEquals(0, stat.getVersion()); Assert.assertEquals(0, stat.getAversion()); Assert.assertEquals(zk.getSessionId(), stat.getEphemeralOwner()); Assert.assertEquals(childname.length(), stat.getDataLength()); Assert.assertEquals(0, stat.getNumChildren());