Class WatchManagerOptimized
java.lang.Object
org.apache.zookeeper.server.watch.WatchManagerOptimized
- All Implemented Interfaces:
IDeadWatcherListener
,IWatchManager
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
Add watch to specific path.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.void
dumpWatches
(PrintWriter pwriter, boolean byPath) String representation of watches.May cause OOM if there are lots of watches, might better to forbid it in this class.Returns a watch report.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.Returns a watch summary.void
processDeadWatchers
(Set<Integer> deadWatchers) Entry for WatcherCleaner to remove dead watchersboolean
removeWatcher
(String path, Watcher watcher) Removes the specified watcher for the given path.void
removeWatcher
(Watcher watcher) The entry to remove the watcher when the cnxn is closed.void
shutdown()
Clean up the watch manager.int
size()
Get the size of watchers.toString()
triggerWatch
(String path, Watcher.Event.EventType type, long zxid, List<ACL> acl) Distribute the watch event for the given path.triggerWatch
(String path, Watcher.Event.EventType type, long zxid, List<ACL> acl, WatcherOrBitSet suppress) Distribute the watch event for the given path, but ignore those suppressed ones.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.apache.zookeeper.server.watch.IWatchManager
addWatch, containsWatcher, removeWatcher
-
Constructor Details
-
WatchManagerOptimized
public WatchManagerOptimized()
-
-
Method Details
-
addWatch
Description copied from interface:IWatchManager
Add watch to specific path.- Specified by:
addWatch
in interfaceIWatchManager
- Parameters:
path
- znode pathwatcher
- watcher object reference- Returns:
- true if the watcher added is not already present
-
containsWatcher
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:
containsWatcher
in interfaceIWatchManager
- Parameters:
path
- znode pathwatcher
- watcher object reference- Returns:
- true if the watcher exists, false otherwise
-
removeWatcher
Description copied from interface:IWatchManager
Removes the specified watcher for the given path.- Specified by:
removeWatcher
in interfaceIWatchManager
- Parameters:
path
- znode pathwatcher
- watcher object reference- Returns:
- true if the watcher successfully removed, false otherwise
-
removeWatcher
Description copied from interface:IWatchManager
The entry to remove the watcher when the cnxn is closed.- Specified by:
removeWatcher
in interfaceIWatchManager
- Parameters:
watcher
- watcher object reference
-
processDeadWatchers
Entry for WatcherCleaner to remove dead watchers- Specified by:
processDeadWatchers
in interfaceIDeadWatcherListener
- Parameters:
deadWatchers
- the watchers need to be removed
-
triggerWatch
public WatcherOrBitSet triggerWatch(String path, Watcher.Event.EventType type, long zxid, List<ACL> acl) Description copied from interface:IWatchManager
Distribute the watch event for the given path.- Specified by:
triggerWatch
in interfaceIWatchManager
- Parameters:
path
- znode pathtype
- the watch event typezxid
- the zxid for the corresponding change that triggered this eventacl
- ACL of the znode in path- Returns:
- the watchers have been notified
-
triggerWatch
public WatcherOrBitSet triggerWatch(String path, Watcher.Event.EventType type, long zxid, List<ACL> acl, WatcherOrBitSet suppress) Description copied from interface:IWatchManager
Distribute the watch event for the given path, but ignore those suppressed ones.- Specified by:
triggerWatch
in interfaceIWatchManager
- Parameters:
path
- znode pathtype
- the watch event typezxid
- the zxid for the corresponding change that triggered this eventsuppress
- the suppressed watcher set- Returns:
- the watchers have been notified
-
size
public int size()Description copied from interface:IWatchManager
Get the size of watchers.- Specified by:
size
in interfaceIWatchManager
- Returns:
- the watchers number managed in this class.
-
shutdown
public void shutdown()Description copied from interface:IWatchManager
Clean up the watch manager.- Specified by:
shutdown
in interfaceIWatchManager
-
getWatchesSummary
Description copied from interface:IWatchManager
Returns a watch summary.- Specified by:
getWatchesSummary
in interfaceIWatchManager
- Returns:
- watch summary
- See Also:
-
getWatches
Description copied from interface:IWatchManager
Returns a watch report.- Specified by:
getWatches
in interfaceIWatchManager
- Returns:
- watch report
- See Also:
-
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:
getWatchesByPath
in interfaceIWatchManager
- Returns:
- watch report
- See Also:
-
getWatcher2PathesMap
May cause OOM if there are lots of watches, might better to forbid it in this class. -
dumpWatches
Description copied from interface:IWatchManager
String representation of watches. Warning, may be large!- Specified by:
dumpWatches
in interfaceIWatchManager
- Parameters:
pwriter
- the writer to dump the watchesbyPath
- iff true output watches by paths, otw output watches by connection
-
toString
-