Class WatchManagerOptimized
- java.lang.Object
- 
- org.apache.zookeeper.server.watch.WatchManagerOptimized
 
- 
- All Implemented Interfaces:
- IDeadWatcherListener,- IWatchManager
 
 public class WatchManagerOptimized extends Object implements IWatchManager, IDeadWatcherListener Optimized in memory and time complexity, compared to WatchManager, both the memory consumption and time complexity improved a lot, but it cannot efficiently remove the watcher when the session or socket is closed, for majority use case this is not a problem. Changed made compared to WatchManager: - Use HashSet and BitSet to store the watchers to find a balance between memory usage and time complexity - Use ReadWriteLock instead of synchronized to reduce lock retention - Lazily clean up the closed watchers
- 
- 
Constructor SummaryConstructors Constructor Description WatchManagerOptimized()
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddWatch(String path, Watcher watcher)Add watch to specific path.booleancontainsWatcher(String path, Watcher watcher)Used in the OpCode.checkWatches, which is a read operation, since read and write requests are exclusively processed, we don't need to hold lock here.voiddumpWatches(PrintWriter pwriter, boolean byPath)String representation of watches.Map<Watcher,Set<String>>getWatcher2PathesMap()May cause OOM if there are lots of watches, might better to forbid it in this class.WatchesReportgetWatches()Returns a watch report.WatchesPathReportgetWatchesByPath()Iterate through ConcurrentHashMap is 'safe', it will reflect the state of the map at the time iteration began, may miss update while iterating, given this is used in the commands to get a general idea of the watches state, we don't care about missing some update.WatchesSummarygetWatchesSummary()Returns a watch summary.voidprocessDeadWatchers(Set<Integer> deadWatchers)Entry for WatcherCleaner to remove dead watchersbooleanremoveWatcher(String path, Watcher watcher)Removes the specified watcher for the given path.voidremoveWatcher(Watcher watcher)The entry to remove the watcher when the cnxn is closed.voidshutdown()Clean up the watch manager.intsize()Get the size of watchers.StringtoString()WatcherOrBitSettriggerWatch(String path, Watcher.Event.EventType type, List<ACL> acl)Distribute the watch event for the given path.WatcherOrBitSettriggerWatch(String path, Watcher.Event.EventType type, List<ACL> acl, WatcherOrBitSet suppress)Distribute the watch event for the given path, but ignore those suppressed ones.- 
Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 - 
Methods inherited from interface org.apache.zookeeper.server.watch.IWatchManageraddWatch, getRecursiveWatchQty
 
- 
 
- 
- 
- 
Method Detail- 
addWatchpublic boolean addWatch(String path, Watcher watcher) Description copied from interface:IWatchManagerAdd watch to specific path.- Specified by:
- addWatchin interface- IWatchManager
- Parameters:
- path- znode path
- watcher- watcher object reference
- Returns:
- true if the watcher added is not already present
 
 - 
containsWatcherpublic boolean containsWatcher(String path, Watcher watcher) Used in the OpCode.checkWatches, which is a read operation, since read and write requests are exclusively processed, we don't need to hold lock here. Different from addWatch this method doesn't mutate any state, so we don't need to hold read lock to avoid dead watcher (cnxn closed) being added to the watcher manager. It's possible that before we lazily clean up the dead watcher, this will return true, but since the cnxn is closed, the response will dropped as well, so it doesn't matter.- Specified by:
- containsWatcherin interface- IWatchManager
- Parameters:
- path- znode path
- watcher- watcher object reference
- Returns:
- true if the watcher exists, false otherwise
 
 - 
removeWatcherpublic boolean removeWatcher(String path, Watcher watcher) Description copied from interface:IWatchManagerRemoves the specified watcher for the given path.- Specified by:
- removeWatcherin interface- IWatchManager
- Parameters:
- path- znode path
- watcher- watcher object reference
- Returns:
- true if the watcher successfully removed, false otherwise
 
 - 
removeWatcherpublic void removeWatcher(Watcher watcher) Description copied from interface:IWatchManagerThe entry to remove the watcher when the cnxn is closed.- Specified by:
- removeWatcherin interface- IWatchManager
- Parameters:
- watcher- watcher object reference
 
 - 
processDeadWatcherspublic void processDeadWatchers(Set<Integer> deadWatchers) Entry for WatcherCleaner to remove dead watchers- Specified by:
- processDeadWatchersin interface- IDeadWatcherListener
- Parameters:
- deadWatchers- the watchers need to be removed
 
 - 
triggerWatchpublic WatcherOrBitSet triggerWatch(String path, Watcher.Event.EventType type, List<ACL> acl) Description copied from interface:IWatchManagerDistribute the watch event for the given path.- Specified by:
- triggerWatchin interface- IWatchManager
- Parameters:
- path- znode path
- type- the watch event type
- acl- ACL of the znode in path
- Returns:
- the watchers have been notified
 
 - 
triggerWatchpublic WatcherOrBitSet triggerWatch(String path, Watcher.Event.EventType type, List<ACL> acl, WatcherOrBitSet suppress) Description copied from interface:IWatchManagerDistribute the watch event for the given path, but ignore those suppressed ones.- Specified by:
- triggerWatchin interface- IWatchManager
- Parameters:
- path- znode path
- type- the watch event type
- suppress- the suppressed watcher set
- Returns:
- the watchers have been notified
 
 - 
sizepublic int size() Description copied from interface:IWatchManagerGet the size of watchers.- Specified by:
- sizein interface- IWatchManager
- Returns:
- the watchers number managed in this class.
 
 - 
shutdownpublic void shutdown() Description copied from interface:IWatchManagerClean up the watch manager.- Specified by:
- shutdownin interface- IWatchManager
 
 - 
getWatchesSummarypublic WatchesSummary getWatchesSummary() Description copied from interface:IWatchManagerReturns a watch summary.- Specified by:
- getWatchesSummaryin interface- IWatchManager
- Returns:
- watch summary
- See Also:
- WatchesSummary
 
 - 
getWatchespublic WatchesReport getWatches() Description copied from interface:IWatchManagerReturns a watch report.- Specified by:
- getWatchesin interface- IWatchManager
- Returns:
- watch report
- See Also:
- WatchesReport
 
 - 
getWatchesByPathpublic WatchesPathReport getWatchesByPath() Iterate through ConcurrentHashMap is 'safe', it will reflect the state of the map at the time iteration began, may miss update while iterating, given this is used in the commands to get a general idea of the watches state, we don't care about missing some update.- Specified by:
- getWatchesByPathin interface- IWatchManager
- Returns:
- watch report
- See Also:
- WatchesPathReport
 
 - 
getWatcher2PathesMappublic Map<Watcher,Set<String>> getWatcher2PathesMap() May cause OOM if there are lots of watches, might better to forbid it in this class.
 - 
dumpWatchespublic void dumpWatches(PrintWriter pwriter, boolean byPath) Description copied from interface:IWatchManagerString representation of watches. Warning, may be large!- Specified by:
- dumpWatchesin interface- IWatchManager
- Parameters:
- pwriter- the writer to dump the watches
- byPath- iff true output watches by paths, otw output watches by connection
 
 
- 
 
-