您好,登錄后才能下訂單哦!
今天繼續(xù)源碼分析,分析一下org.apache.zookeeper.server下的WatchManager類。
WatcherManager類用于管理watchers和相應(yīng)的觸發(fā)器。
//watchTable表示從節(jié)點(diǎn)路徑到watcher集合的映射
private final HashMap<String, HashSet<Watcher>> watchTable =
new HashMap<String, HashSet<Watcher>>();
//watch3Paths則表示從watcher到所有節(jié)點(diǎn)路徑集合的映射
private final HashMap<Watcher, HashSet<String>> watch3Paths =
new HashMap<Watcher, HashSet<String>>();
size方法是同步的,因此在多線程環(huán)境下是安全的,其主要作用是獲取watchTable的大小,即遍歷watchTable的值集合,每個(gè)集合大小累加。
synchronized int size(){
int result = 0;
for(Set<Watcher> watches : watchTable.values()) {
result += watches.size();
}
return result;
}
addWatch方法同樣是同步的,主要用來更新類里面的上面提到的兩個(gè)集合屬性。
synchronized void addWatch(String path, Watcher watcher) {
//通過傳入的path(節(jié)點(diǎn)路徑)從watchTable獲取相應(yīng)的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中獲取相應(yīng)的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(節(jié)點(diǎn)路徑)添加至path集合,即完成了path和watcher添加至watch3Paths的步驟
paths.add(path);
}
removeWatcher用作從watch3Paths和watchTable中中移除該watcher
synchronized void removeWatcher(Watcher watcher) {
//從wach3Paths中移除watcher,并返回watcher對(duì)應(yīng)的path集合
HashSet<String> paths = watch3Paths.remove(watcher);
if (paths == null) {
return;
}
for (String p : paths) {
//從watcherTable中根據(jù)路徑取出相應(yīng)的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);
}
}
}
}
該方法主要用于觸發(fā)watch事件,并對(duì)事件進(jìn)行處理。
Set<Watcher> triggerWatch(String path, EventType type) {
return triggerWatch(path, type, null);
}
Set<Watcher> triggerWatch(String path, EventType type, Set<Watcher> supress) {
// 根據(jù)事件類型、連接狀態(tài)、節(jié)點(diǎn)路徑創(chuàng)建WatchedEvent
WatchedEvent e = new WatchedEvent(type,
KeeperState.SyncConnected, path);
HashSet<Watcher> watchers;
synchronized (this) {
// 從watcher表中移除path,并返回其對(duì)應(yīng)的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) {
// 根據(jù)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進(jìn)行處理
w.process(e);
}
return watchers;
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。