public boolean isTrackingSession(long sessionId) { return isLocalSession(sessionId) || isGlobalSession(sessionId); }
@Override protected void startSessionTracker() { upgradeableSessionTracker = (UpgradeableSessionTracker) sessionTracker; upgradeableSessionTracker.start(); }
private Request makeUpgradeRequest(long sessionId) { // Make sure to atomically check local session status, upgrade // session, and make the session creation request. This is to // avoid another thread upgrading the session in parallel. synchronized (upgradeableSessionTracker) { if (upgradeableSessionTracker.isLocalSession(sessionId)) { int timeout = upgradeableSessionTracker.upgradeSession(sessionId); ByteBuffer to = ByteBuffer.allocate(4); to.putInt(timeout); return new Request( null, sessionId, 0, OpCode.createSession, to, null); } } return null; }
public boolean checkIfValidGlobalSession(long sess, int to) { if (self.areLocalSessionsEnabled() && !upgradeableSessionTracker.isGlobalSession(sess)) { return false; } return sessionTracker.touchSession(sess, to); }
@Override protected void revalidateSession(ServerCnxn cnxn, long sessionId, int sessionTimeout) throws IOException { if (upgradeableSessionTracker.isLocalSession(sessionId)) { super.revalidateSession(cnxn, sessionId, sessionTimeout); } else { getLearner().validateSession(cnxn, sessionId, sessionTimeout); } }
trackSession(sessionId, timeout);
@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())); }
@Override protected void setLocalSessionFlag(Request si) { // We need to set isLocalSession to tree for these type of request // so that the request processor can process them correctly. switch (si.type) { case OpCode.createSession: if (self.areLocalSessionsEnabled()) { // All new sessions local by default. si.setLocalSession(true); } break; case OpCode.closeSession: String reqType = "global"; if (upgradeableSessionTracker.isLocalSession(si.sessionId)) { si.setLocalSession(true); reqType = "local"; } LOG.info("Submitting " + reqType + " closeSession request" + " for session 0x" + Long.toHexString(si.sessionId)); break; default: break; } }
!upgradeableSessionTracker.isLocalSession(request.sessionId)) { return null;