zookeeper源碼之臨時節點管理
阿新 • • 發佈:2018-01-31
void oid str 樹結構 sta string zookeepe for style
配置存儲不僅維護了一個樹結構,還實現了臨時節點的功能,臨時節點的生命周期和客戶端會話綁定在一起,客戶端會話失效,則這個節點就會被自動清除。
DataTree內部維護了一個hastable結構,key為sessionid,value為該session創建的臨時節點。客戶端會話失效,其創建的臨時節點都會被刪除。
private final Map<Long, HashSet<String>> ephemerals = new ConcurrentHashMap<Long, HashSet<String>>(); public HashSet<String> getEphemerals(longsessionId) { HashSet<String> retv = ephemerals.get(sessionId); ... HashSet<String> cloned = null; synchronized (retv) { cloned = (HashSet<String>) retv.clone(); } return cloned; } public String createNode(String path, bytedata[], List<ACL> acl, long ephemeralOwner, long zxid, long time) throws KeeperException.NoNodeException, KeeperException.NodeExistsException { ... if (ephemeralOwner != 0) { HashSet<String> list = ephemerals.get(ephemeralOwner);if (list == null) { list = new HashSet<String>(); ephemerals.put(ephemeralOwner, list); } synchronized (list) { list.add(path); } } } ... } public void deleteNode(String path, long zxid) throws KeeperException.NoNodeException { ... DataNode node = nodes.get(path); ... long eowner = node.stat.getEphemeralOwner(); if (eowner != 0) { HashSet<String> nodes = ephemerals.get(eowner); if (nodes != null) { synchronized (nodes) { nodes.remove(path); } } } ... } void killSession(long session, long zxid) { ... HashSet<String> list = ephemerals.remove(session); if (list != null) { for (String path : list) { ... deleteNode(path, zxid); ... } } }
zookeeper源碼之臨時節點管理