LeaderSessionTracker
public class LeaderSessionTracker extends UpgradeableSessionTracker
{
// 日志记录对象
private static final Logger LOG = LoggerFactory.getLogger(LeaderSessionTracker.class);
// 会话追踪实现--用于全局会话追踪
private final SessionTrackerImpl globalSessionTracker;
// 服务id
private final long serverId;
public LeaderSessionTracker(
SessionExpirer expirer,
ConcurrentMap<Long, Integer> sessionsWithTimeouts,
int tickTime, long id, boolean localSessionsEnabled, ZooKeeperServerListener listener)
{
// 全局会话追踪对象
this.globalSessionTracker
= new SessionTrackerImpl(expirer, sessionsWithTimeouts, tickTime, id, listener);
// 支持本地会话
this.localSessionsEnabled = localSessionsEnabled;
// 如果支持本地会话
if (this.localSessionsEnabled)
{
// 创建本地会话追踪对象
createLocalSessionTracker(expirer, tickTime, id, listener);
}
// 服务id
serverId = id;
}
// 移除会话ID
public void removeSession(long sessionId)
{
// 追踪本地会话时,先从本地会话追踪对象中移除会话
if (localSessionTracker != null)
{
localSessionTracker.removeSession(sessionId);
}
// 从全局会话追踪对象中移除会话
globalSessionTracker.removeSession(sessionId);
}
// 开始
public void start()
{
// 通过全局会话追踪对象执行开始
globalSessionTracker.start();
// 通过本地会话追踪对象执行开始
if (localSessionTracker != null)
{
localSessionTracker.start();
}
}
public void shutdown()
{
// 通过本地会话追踪对象执行关闭
if (localSessionTracker != null)
{
localSessionTracker.shutdown();
}
// 全局会话追踪对象执行关闭
globalSessionTracker.shutdown();
}
// 全局会话追踪对象中是否追踪此会话
public boolean isGlobalSession(long sessionId)
{
return globalSessionTracker.isTrackingSession(sessionId);
}
// 追踪会话
public boolean trackSession(long sessionId, int sessionTimeout)
{
// 全局会话追踪对象执行追踪会话
boolean tracked = globalSessionTracker.trackSession(sessionId, sessionTimeout);
if (localSessionsEnabled && tracked)
{
LOG.info("Tracking global session 0x{}", Long.toHexString(sessionId));
}
return tracked;
}
// 提交会话--会话id,超时时间
public synchronized boolean commitSession(long sessionId, int sessionTimeout)
{
// 通过全局会话追踪对象执行提交会话
boolean added = globalSessionTracker.commitSession(sessionId, sessionTimeout);
if (added)
{
LOG.info("Committing global session 0x{}", Long.toHexString(sessionId));
}
// 如果支持本地会话
if (localSessionsEnabled)
{
// 从本地会话追踪对象中移除会话
removeLocalSession(sessionId);
// 完成会话升级
finishedUpgrading(sessionId);
}
return added;
}
// touch会话
public boolean touchSession(long sessionId, int sessionTimeout)
{
// 本地会话追踪对象存在,且本地会话追踪对象中执行了touch
if (localSessionTracker != null && localSessionTracker.touchSession(sessionId, sessionTimeout))
{
// 返回
return true;
}
// 通过全局会话追踪对象执行touch
return globalSessionTracker.touchSession(sessionId, sessionTimeout);
}
// 创建会话
public long createSession(int sessionTimeout)
{
// 如果支持本地会话
if (localSessionsEnabled)
{
// 通过本地会话追踪对象执行会话创建
return localSessionTracker.createSession(sessionTimeout);
}
// 通过全局会话追踪对象执行会话创建
return globalSessionTracker.createSession(sessionTimeout);
}
// 从会话id中获取服务id
public static long getServerIdFromSessionId(long sessionId)
{
return sessionId >> 56;
}
// 检查会话
public void checkSession(
long sessionId, Object owner)
throws SessionExpiredException, SessionMovedException, UnknownSessionException
{
// 本地会话追踪对象存在
if (localSessionTracker != null)
{
try
{
// 通过本地会话追踪对象执行检查
localSessionTracker.checkSession(sessionId, owner);
// 如果会话id不是全局会话
if (!isGlobalSession(sessionId))
{
// 返回
return;
}
}
catch (UnknownSessionException e)
{
}
}
try
{
// 通过全局会话追踪对象执行会话检查
globalSessionTracker.checkSession(sessionId, owner);
return;
}
catch (UnknownSessionException e)
{
}
// 如果不支持本地会话
// 获得绘画id中得到的服务id不是本类对象中记录的服务id
if (!localSessionsEnabled || (getServerIdFromSessionId(sessionId) == serverId))
{
// 抛出会话超期异常
throw new SessionExpiredException();
}
}
// 检查全局会话
public void checkGlobalSession(
long sessionId, Object owner) throws SessionExpiredException, SessionMovedException
{
try
{
// 通过全局会话追踪对象执行会话检查
globalSessionTracker.checkSession(sessionId, owner);
}
catch (UnknownSessionException e)
{
throw new SessionExpiredException();
}
}
// 设置会话拥有者
public void setOwner(long sessionId, Object owner) throws SessionExpiredException
{
// 本地会话追踪对象存在下
if (localSessionTracker != null)
{
try
{
// 通过本地会话追踪对象设置会话拥有者
localSessionTracker.setOwner(sessionId, owner);
return;
}
catch (SessionExpiredException e)
{
}
}
// 通过全局会话追踪对象设置会话拥有者
globalSessionTracker.setOwner(sessionId, owner);
}
// 打印会话
public void dumpSessions(PrintWriter pwriter)
{
if (localSessionTracker != null)
{
pwriter.print("Local ");
// 打印本地会话追踪对象
localSessionTracker.dumpSessions(pwriter);
pwriter.print("Global ");
}
// 打印全局会话追踪对象
globalSessionTracker.dumpSessions(pwriter);
}
// 设置会话关闭标志
public void setSessionClosing(long sessionId)
{
// 通过本地会话追踪对象设置会话关闭标志
if (localSessionTracker != null)
{
localSessionTracker.setSessionClosing(sessionId);
}
// 通过全局会话追踪对象设置会话关闭标志
globalSessionTracker.setSessionClosing(sessionId);
}
// 获得会话超期Map
public Map<Long, Set<Long>> getSessionExpiryMap()
{
Map<Long, Set<Long>> sessionExpiryMap;
// 通过本地会话追踪对象得到会话超期Map
if (localSessionTracker != null)
{
sessionExpiryMap = localSessionTracker.getSessionExpiryMap();
}
else
{
sessionExpiryMap = new TreeMap<Long, Set<Long>>();
}
// 再将全局会话追踪对象里得到的会话超期Map也汇集到一起
sessionExpiryMap.putAll(globalSessionTracker.getSessionExpiryMap());
return sessionExpiryMap;
}
// 全局会话集合
public Set<Long> globalSessions()
{
// 从全局会话追踪对象中得到的全局会话集合
return globalSessionTracker.globalSessions();
}
}