Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of session Management SessionTrackerImpl in zk

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "the method of session management SessionTrackerImpl in zk". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the method of session management SessionTrackerImpl in zk".

SessionTrackerImpl contains SessionTracker interfaces that implement SessionImpl inheritance Session (in SessionTracker)

SessionImpl is the session attribute collection class

Attribute

SessionsById

The corresponding map of sessionid and session

SessionExpiryQueue

Session expiration queue

SessionsWithTimeout

Sessionid corresponding expiration time

NextSessionId

Next sessionid

Expirer

Let the session expire and get the service id

Running

In operation

The class ExpiryQueue involved

SessionImpl-like

SessionExpirer-like

The main session management functions are:

1 session expiration

2 set session timeout

3 generate the next session id

Method

Convert time- > sessions map to time- > sessionIds mapping

GetSessionExpiryMap

Public synchronized Map getSessionExpiryMap () {/ / Convert time-> sessions map to time-> session IDs map Map expiryMap = sessionExpiryQueue.getExpiryMap (); Map sessionExpiryMap = new TreeMap (); for (Entry e: expiryMap.entrySet ()) {Set ids = new HashSet (); sessionExpiryMap.put (e.getKey (), ids); for (SessionImpl s: e.getValue ()) {ids.add (s.sessionId) }} return sessionExpiryMap;}

Run timeout session check thread, update timeout

Public void run () {try {while (running) {/ / No timeout, long waitTime = sessionExpiryQueue.getWaitTime (); / / Thread waits for a period of time if (waitTime > 0) {Thread.sleep (waitTime); continue } / / has timed out, take out the session in the session queue to clean up for (SessionImpl s: sessionExpiryQueue.poll ()) {ServerMetrics.getMetrics () .STALE_SESSIONS_EXPIRED.add (1); / / set the flag to SessionImpl isClosing true setSessionClosing (s.sessionId) / / zookeeperServer let session expire expirer.expire (s);} catch (InterruptedException e) {handleException (this.getName (), e);} LOG.info ("SessionTrackerImpl exited loop!");}

Method

Description

GetSessionTimeout

Get the timeout through sessionId

SetSessionClosing

Set session close

RemoveSession

Remove the session through sessionId, as well as the mapping diagram associated with the session

CreateSession (int sessionTimeout)

Create a session to specify the expiration time

TrackSession (sessionId, sessionTimeout)

Get sessionId, call trackSession (sessionId, sessionTimeout); create session with method to update session timeout

@ Overridepublic synchronized boolean trackSession (long id, int sessionTimeout) {boolean added = false; SessionImpl session = sessionsById.get (id); / / add a new session object if (session = = null) {session = new SessionImpl (id, sessionTimeout);} / / findbugs2.0.3 complains about get after put. / /? Why / / long term strategy would be use computeIfAbsent after JDK 1.8 SessionImpl existedSession = sessionsById.putIfAbsent (id, session); if (existedSession! = null) {session = existedSession;} else {added = true; if (LOG.isDebugEnabled ()) {LOG.debug ("Adding session 0x {}", Long.toHexString (id)) }} if (LOG.isTraceEnabled ()) {String actionStr = added? "Adding": "Existing"; ZooTrace.logTraceMessage (LOG, ZooTrace.SESSION_TRACE_MASK, "SessionTrackerImpl -" + actionStr + "session 0x" + Long.toHexString (id) + "" + sessionTimeout);} / / there is update timeout updateSessionExpiry (session, sessionTimeout); return added;}

Method

Description

Involving abnormality

CommitSession (long id, int sessionTimeout)

Submit session

UnknownSessionException

IsTrackingSession (long sessionId)

Whether the session can be tracked, indicating that the session is valid

SessionExpiredException

CheckSession (long sessionId, Object owner)

Check session information through the session and the owner

SessionMovedException

Bucket-splitting strategy

Assign sessions with different timeout times to different buckets, manage them effectively and divide buckets according to time intervals.

Private long roundToNextInterval (long time) {

Return (time / expirationInterval + 1) * expirationInterval

}

How to keep the session active after the session is established

How are problem sessions migrated?

The methods of session activation are public synchronized boolean touchSession (long sessionId, int timeout) {/ / get session SessionImpl s = sessionsById.get (sessionId); if (s = = null) {logTraceTouchInvalidSession (sessionId, timeout); return false;} / / close if (s.isClosing ()) {logTraceTouchClosingSession (sessionId, timeout); return false } / / Update session moves session from the old expiration bucket to the new bucket updateSessionExpiry (s, timeout); return true;}

When will client issue an activation session request?

1 when the client sends a request to the server, including the read and write request, the session will be activated

2 when the client finds that it has not received any communication within the sessionTimeout/3 time, it will initiate a ping request to the server

After the server receives the request, the session activation operation will be performed.

Timeout detection strategy

For session timeout detection, zookeeper is responsible for using SessionTracker, and sessionTracker uses separate threads (timeout check threads)

Special session timeout detection, that is, cleaning up the remaining sessions in the session bucket one by one.

If a session is activated, zookeeper migrates it from the previous session bucket to the next session bucket

If session n of ExpirationTIme 1 is migrated to ExpirationTime n bucket, the session left in ExpirationTime 1 will not be activated.

The timeout detector periodically checks the remaining unmigrated sessions in this session bucket and only checks at a specified point in time (ExpirationTime 1 expiration time 2)

ExpirationTime3,...n) this helps to improve performance

@ Overridepublic void run () {try {while (running) {long waitTime = sessionExpiryQueue.getWaitTime (); if (waitTime > 0) {Thread.sleep (waitTime); continue;} for (SessionImpl s: sessionExpiryQueue.poll ()) {ServerMetrics.getMetrics () .STALE_SESSIONS_EXPIRED.add (1) SetSessionClosing (s.sessionId); expirer.expire (s);}} catch (InterruptedException e) {handleException (this.getName (), e);} LOG.info ("SessionTrackerImpl exited loop!");} at this point, I believe you have a deeper understanding of "the method of session management SessionTrackerImpl in zk". You might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report