In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Zookeeper source code in session management example analysis, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
1. Creation of session / / ZookeeperServer.java// line 617 long createSession (ServerCnxn cnxn, byte passwd [], int timeout) {long sessionId = sessionTracker.createSession (timeout); / / omitting part of the code} / / SessionTrackerImpl.java// line 236 synchronized public long createSession (int sessionTimeout) {addSession (nextSessionId, sessionTimeout); return nextSessionId++ } / / Line 241 of SessionTrackerImpl.java// synchronized public void addSession (long id, int sessionTimeout) {/ / Save the relationship between sessionId and expiration time sessionsWithTimeout.put (id, sessionTimeout); / / if session does not exist, create a new if (sessionsById.get (id) = = null) {SessionImpl s = new SessionImpl (id, sessionTimeout, 0); sessionsById.put (id, s) / / omit log printing} else {/ / omit log printing} / / aggregate session according to certain rules touchSession (id, sessionTimeout);} / / SessionTrackerImpl.java// line 166 synchronized public boolean touchSession (long sessionId, int timeout) {if (LOG.isTraceEnabled ()) {/ / omit log print} SessionImpl s = sessionsById.get (sessionId) / / Return false, if the session doesn't exists or marked as closing if (s = = null | | s.isClosing ()) {return false;} long expireTime = roundToInterval (Time.currentElapsedTime () + timeout); / / if the current session expiration time is greater than this value, you do not need to operate if (s.tickTime > = expireTime) {/ / Nothing needs to be done return true } / / remove session from the old bucket and put it into the new bucket (the remaining timeout is longer) SessionSet set = sessionSets.get (s.tickTime); if (set! = null) {set.sessions.remove (s);} s.tickTime = expireTime; set = sessionSets.get (s.tickTime); if (set = = null) {set = new SessionSet (); sessionSets.put (expireTime, set) } set.sessions.add (s); return true } / / Line 89 of SessionTrackerImpl.java// private long roundToInterval (long time) {/ / expirationInterval is the heartbeat cycle (tickTime) of zookeeper. The default value is 3000 / /. This calculation means to divide the expiration time into a segment / / for example, 200ms, 500ms, 3000ms returns 0meme 3001ms, 5000ms returns 3000 / / since Time.currentElapsedTime () is added to the time here, return (time / expirationInterval + 1) * expirationInterval will not occur. Session activation / / ZookeeperServer.java// line 728 public void submitRequest (Request si) {/ / omit part of the code try {touch (si.cnxn); / / omit part of code} catch (MissingSessionException e) {if (LOG.isDebugEnabled ()) {LOG.debug ("Dropping request:" + e.getMessage ()) }} catch (RequestProcessorException e) {LOG.error ("Unable to process request:" + e.getMessage (), e) }} / / Line 368 of ZookeeperServer.java// void touch (ServerCnxn cnxn) throws MissingSessionException {/ / omit some code if (! sessionTracker.touchSession (id, to)) {throw new MissingSessionException ("No session with sessionid 0x" + Long.toHexString (id) + "exists, probably expired and removed");}}
When the zookeeper server responds to the client's request, the submitRequest method is called, and eventually the touchSession method is called, where the session is moved to the new bucket
3. Expiration of session / / SessionTrackerImpl.java// Line 142 synchronized public void run () {try {while (running) {currentTime = Time.currentElapsedTime (); / / when SessionTrackerImpl is initialized, nextExpirationTime is assigned an initial value / / nextExpirationTime = roundToInterval (Time.currentElapsedTime ()); if (nextExpirationTime > currentTime) {this.wait (nextExpirationTime-currentTime) Continue;} SessionSet set; / / if the expiration time is reached, remove all session set = sessionSets.remove (nextExpirationTime) in the corresponding bucket; if (set! = null) {for (SessionImpl s: set.sessions) {setSessionClosing (s.sessionId) Expirer.expire (s);}} nextExpirationTime + = expirationInterval;}} catch (InterruptedException e) {handleException (this.getName (), e);} LOG.info ("SessionTrackerImpl exited loop!");} / / ZookeeperServer.java// line 353 public void expire (Session session) {long sessionId = session.getSessionId () LOG.info ("Expiring session 0x" + Long.toHexString (sessionId) + ", timeout of" + session.getTimeout () + "ms exceeded"); close (sessionId);} / / ZookeeperServer.java// line 329 private void close (long sessionId) {submitRequest (null, sessionId, OpCode.closeSession, 0, null, null) } / / Line 294 of PrepRequestProcessor.java// protected void pRequest2Txn (int type, long zxid, Request request, Record record, boolean deserialize) throws KeeperException, IOException, RequestProcessorException {request.hdr = new TxnHeader (request.sessionId, request.cxid, zxid, Time.currentWallTime (), type) Switch (type) {/ / omit the code case OpCode.closeSession: / / We don't want to do this check since the session expiration thread / / queues up this operation without being the session owner. / / this request is the last of the session so it should be ok / / zks.sessionTracker.checkSession (request.sessionId, request.getOwner ()); HashSet es = zks.getZKDatabase () .getEphemerals (request.sessionId) / / deleting all temporary nodes associated with session synchronized (zks.outstandingChanges) {/ / zookeeper most operations are recorded and put into the list for (ChangeRecord c: zks.outstandingChanges) {/ / c.stat = = null indicates that this is the delete operation if (c.stat = = Null) {es.remove (c.path) } else if (c.stat.getEphemeralOwner () = = request.sessionId) {es.add (c.path) } for (String path3Delete: es) {addChangeRecord (new ChangeRecord (request.hdr.getZxid (), path3Delete, null, 0, null));} zks.sessionTracker.setSessionClosing (request.sessionId) } LOG.info ("Processed session termination for sessionid: 0x" + Long.toHexString (request.sessionId)); break; / / omit code}} after reading the above, have you mastered the method of example analysis of session management in Zookeeper source code? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.