欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Zookeeper源碼中session管理的示例分析

Zookeeper源碼中session管理的示例分析,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

公司主營(yíng)業(yè)務(wù):成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。創(chuàng)新互聯(lián)推出翠屏免費(fèi)做網(wǎng)站回饋大家。

一、session的創(chuàng)建

//ZookeeperServer.java
//第617行
long createSession(ServerCnxn cnxn, byte passwd[], int timeout) {
    long sessionId = sessionTracker.createSession(timeout);
    
    //省略部分代碼
}

//SessionTrackerImpl.java
//第236行
synchronized public long createSession(int sessionTimeout) {
    addSession(nextSessionId, sessionTimeout);
    return nextSessionId++;
}

//SessionTrackerImpl.java
//第241行
synchronized public void addSession(long id, int sessionTimeout) {
    //保存sessionId和過(guò)期時(shí)間的關(guān)系
    sessionsWithTimeout.put(id, sessionTimeout);
	//如果session不存在,就新建一個(gè)
    if (sessionsById.get(id) == null) {
        SessionImpl s = new SessionImpl(id, sessionTimeout, 0);
        sessionsById.put(id, s);
        //省略日志打印
    } else {
        //省略日志打印
    }
    //將session按照一定規(guī)則聚合
    touchSession(id, sessionTimeout);
}

//SessionTrackerImpl.java
//第166行
synchronized public boolean touchSession(long sessionId, int timeout) {
    if (LOG.isTraceEnabled()) {
        //省略日志打印
    }
    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);
    //如果當(dāng)前session的過(guò)期時(shí)間大于這個(gè)值,不需要操作
    if (s.tickTime >= expireTime) {
        // Nothing needs to be done
        return true;
    }
    //將session從舊的桶中移出,并放入(剩余超時(shí)時(shí)間更長(zhǎng)的)新的桶
    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;
}

//SessionTrackerImpl.java
//第89行
private long roundToInterval(long time) {
    //expirationInterval就是zookeeper的心跳周期(tickTime),默認(rèn)值是3000
    //這段計(jì)算的意思是將過(guò)期時(shí)間每3000ms分一個(gè)段
    //比如200ms、500ms、3000ms返回0,3001ms、5000ms返回3000
    //由于這里的time是加了Time.currentElapsedTime()的,所以不會(huì)出現(xiàn)0的情況
    return (time / expirationInterval + 1) * expirationInterval;
}

二、session激活

//ZookeeperServer.java
//第728行
public void submitRequest(Request si) {
    //省略部分代碼
    
    try {
        touch(si.cnxn);
        
        //省略部分代碼
    } catch (MissingSessionException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Dropping request: " + e.getMessage());
        }
    } catch (RequestProcessorException e) {
        LOG.error("Unable to process request:" + e.getMessage(), e);
    }
}

//ZookeeperServer.java
//第368行
void touch(ServerCnxn cnxn) throws MissingSessionException {
    //省略部分代碼
    if (!sessionTracker.touchSession(id, to)) {
        throw new MissingSessionException(
            "No session with sessionid 0x" + Long.toHexString(id)
            + " exists, probably expired and removed");
    }
}

zookeeper服務(wù)端響應(yīng)客戶端的請(qǐng)求時(shí),都會(huì)調(diào)用submitRequest方法,最終會(huì)調(diào)用到touchSession方法,這里會(huì)將session移動(dòng)到新的桶中

三、session的過(guò)期

//SessionTrackerImpl.java
//第142行
synchronized public void run() {
    try {
        while (running) {
            currentTime = Time.currentElapsedTime();
            //在SessionTrackerImpl初始化的時(shí)候,會(huì)給nextExpirationTime賦一個(gè)初值
            //nextExpirationTime = roundToInterval(Time.currentElapsedTime());
            if (nextExpirationTime > currentTime) {
                this.wait(nextExpirationTime - currentTime);
                continue;
            }
            SessionSet set;
            //如果到達(dá)了過(guò)期時(shí)間,則移除對(duì)應(yīng)桶中的所有session
            set = sessionSets.remove(nextExpirationTime);
            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
//第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
//第329行
private void close(long sessionId) {
    submitRequest(null, sessionId, OpCode.closeSession, 0, null, null);
}

//PrepRequestProcessor.java
//第294行
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) {
        //省略代碼
            
        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<String> es = zks.getZKDatabase().getEphemerals(request.sessionId);
            //刪除session關(guān)聯(lián)的所有臨時(shí)節(jié)點(diǎn)
            synchronized (zks.outstandingChanges) {
                //zookeeper的大部分操作都會(huì)記錄并放入列表
                for (ChangeRecord c : zks.outstandingChanges) {
                    //c.stat == null表示這是刪除操作
                    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;
        
         //省略代碼
    }
}

看完上述內(nèi)容,你們掌握Z(yǔ)ookeeper源碼中session管理的示例分析的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

標(biāo)題名稱:Zookeeper源碼中session管理的示例分析
分享路徑:http://www.chinadenli.net/article12/piijgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管網(wǎng)站排名企業(yè)建站ChatGPT做網(wǎng)站網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)