您好,登錄后才能下訂單哦!
今天繼續源碼分析,分析一下org.apache.zookeeper.server下的WatchManager類。
WatcherManager類用于管理watchers和相應的觸發器。
//watchTable表示從節點路徑到watcher集合的映射
private final HashMap<String, HashSet<Watcher>> watchTable =
new HashMap<String, HashSet<Watcher>>();
//watch3Paths則表示從watcher到所有節點路徑集合的映射
private final HashMap<Watcher, HashSet<String>> watch3Paths =
new HashMap<Watcher, HashSet<String>>();
size方法是同步的,因此在多線程環境下是安全的,其主要作用是獲取watchTable的大小,即遍歷watchTable的值集合,每個集合大小累加。
synchronized int size(){
int result = 0;
for(Set<Watcher> watches : watchTable.values()) {
result += watches.size();
}
return result;
}
addWatch方法同樣是同步的,主要用來更新類里面的上面提到的兩個集合屬性。
synchronized void addWatch(String path, Watcher watcher) {
//通過傳入的path(節點路徑)從watchTable獲取相應的watcher集合
HashSet<Watcher> list = watchTable.get(path);
if (list == null) { //watcher是否為空,若為空
// don't waste memory if there are few watches on a node
// rehash when the 4th entry is added, doubling size thereafter
// seems like a good compromise
//新生成watcher集合,并將路徑path和此集合添加至watchTable中
list = new HashSet<Watcher>(4);
watchTable.put(path, list);
}
//將傳入的watcher添加至watcher集合,即完成了path和watcher添加至watchTable的步驟
list.add(watcher);
//通過傳入的watcher從watch3Paths中獲取相應的path集合
HashSet<String> paths = watch3Paths.get(watcher);
if (paths == null) {// 判斷path集合是否為空,若為空
// cnxns typically have many watches, so use default cap here
//新生成path集合,并將watcher和paths添加至watch3Paths中
paths = new HashSet<String>();
watch3Paths.put(watcher, paths);
}
// 將傳入的path(節點路徑)添加至path集合,即完成了path和watcher添加至watch3Paths的步驟
paths.add(path);
}
removeWatcher用作從watch3Paths和watchTable中中移除該watcher
synchronized void removeWatcher(Watcher watcher) {
//從wach3Paths中移除watcher,并返回watcher對應的path集合
HashSet<String> paths = watch3Paths.remove(watcher);
if (paths == null) {
return;
}
for (String p : paths) {
//從watcherTable中根據路徑取出相應的watcher集合
HashSet<Watcher> list = watchTable.get(p);
if (list != null) {
// 從list中移除該watcher
list.remove(watcher);
// 移除后list為空,則從watchTable中移出path
if (list.size() == 0) {
watchTable.remove(p);
}
}
}
}
該方法主要用于觸發watch事件,并對事件進行處理。
Set<Watcher> triggerWatch(String path, EventType type) {
return triggerWatch(path, type, null);
}
Set<Watcher> triggerWatch(String path, EventType type, Set<Watcher> supress) {
// 根據事件類型、連接狀態、節點路徑創建WatchedEvent
WatchedEvent e = new WatchedEvent(type,
KeeperState.SyncConnected, path);
HashSet<Watcher> watchers;
synchronized (this) {
// 從watcher表中移除path,并返回其對應的watcher集合
watchers = watchTable.remove(path);
if (watchers == null || watchers.isEmpty()) {
if (LOG.isTraceEnabled()) {
ZooTrace.logTraceMessage(LOG,
ZooTrace.EVENT_DELIVERY_TRACE_MASK,
"No watchers for " + path);
}
return null;
}
// 遍歷watcher集合
for (Watcher w : watchers) {
// 根據watcher從watcher表中取出路徑集合
HashSet<String> paths = watch3Paths.get(w);
if (paths != null) {
// 如果paths不為空,則移除傳入路徑path
paths.remove(path);
}
}
}
// 遍歷watcher集合
for (Watcher w : watchers) {
if (supress != null && supress.contains(w)) {
continue;
}
// watcher進行處理
w.process(e);
}
return watchers;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。