zookeeper源码解析--会话管理--UpgradeableSessionTracker

UpgradeableSessionTracker

// 日志对象
private static final Logger LOG = LoggerFactory.getLogger(UpgradeableSessionTracker.class);
// 本地会话id及其超时信息
private ConcurrentMap<Long, Integer> localSessionsWithTimeouts;
// 升级中会话id及其超时信息
private ConcurrentMap<Long, Integer> upgradingSessions;
// 本地会话追踪对象
protected LocalSessionTracker localSessionTracker;
// 支持本地会话标志
protected boolean localSessionsEnabled;
public void start() 
{
}

public void createLocalSessionTracker(
	SessionExpirer expirer, int tickTime, long id, ZooKeeperServerListener listener) 
{
	// 本地会话及其超时时间
    this.localSessionsWithTimeouts = new ConcurrentHashMap<Long, Integer>();
    // 本地会话追踪
    this.localSessionTracker = new LocalSessionTracker(expirer, this.localSessionsWithTimeouts, tickTime, id, listener);
    // 升级中会话及其超时时间
    this.upgradingSessions = new ConcurrentHashMap<Long, Integer>();
}

// 可以追踪本地会话 与 全局会话
public boolean isTrackingSession(long sessionId) 
{
    return isLocalSession(sessionId) || isGlobalSession(sessionId);
}

// 本地会话最终存在,且在本地会话追踪中被追踪
public boolean isLocalSession(long sessionId) 
{
    return localSessionTracker != null && localSessionTracker.isTrackingSession(sessionId);
}

// 支持本地会话
@Override
public boolean isLocalSessionsEnabled() 
{
    return localSessionsEnabled;
}

// 升级中会话容器存在,且会话id存在于容器中
public boolean isUpgradingSession(long sessionId) 
{
    return upgradingSessions != null && upgradingSessions.containsKey(sessionId);
}

// 完成会话升级,将会话id从升级中容器中移除
public void finishedUpgrading(long sessionId) 
{
    if (upgradingSessions != null) 
    {
        upgradingSessions.remove(sessionId);
    }
}

// 判断是否为全局会话
public abstract boolean isGlobalSession(long sessionId);
// 会话升级
public int upgradeSession(long sessionId) 
{
    if (localSessionsWithTimeouts == null) 
    {
        return -1;
    }
    
    // 要升级的会话得在本地会话id及其超时时间容器中存在
    // 现在将其移出
    Integer timeout = localSessionsWithTimeouts.remove(sessionId);
    if (timeout != null) 
    {
        LOG.info("Upgrading session 0x{}", Long.toHexString(sessionId));
        // 追踪会话
        trackSession(sessionId, timeout);
        // 将会话id及其超时时间放入升级中容器暂存
        upgradingSessions.put(sessionId, timeout);
        // 从本地会话追踪对象中移除会话id
        localSessionTracker.removeSession(sessionId);
        return timeout;
    }
    
    return -1;
}

// 移除本地会话
protected void removeLocalSession(long sessionId) 
{
    if (localSessionTracker == null) 
    {
        return;
    }
    
    // 从本地会话追踪对象中移除会话
    localSessionTracker.removeSession(sessionId);
}

// 全局会话检查
public void checkGlobalSession(long sessionId, Object owner) 
	throws KeeperException.SessionExpiredException, KeeperException.SessionMovedException 
{
    throw new UnsupportedOperationException();
}

// 本地会话数量
public long getLocalSessionCount() 
{
    if (localSessionsWithTimeouts == null) 
    {
        return 0;
    }
    
    return localSessionsWithTimeouts.size();
}

// 本地会话集合,从本地会话追踪对象得到
public Set<Long> localSessions() 
{
    return (localSessionTracker == null) ? Collections.<Long>emptySet() : localSessionTracker.localSessions();
}

猜你喜欢

转载自blog.csdn.net/x13262608581/article/details/122726247
今日推荐