zookeeper-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [zookeeper] eolivelli commented on a change in pull request #136: [ZOOKEEPER-1416] Persistent Recursive Watch
Date Wed, 02 Oct 2019 10:49:01 GMT
eolivelli commented on a change in pull request #136: [ZOOKEEPER-1416] Persistent Recursive
Watch
URL: https://github.com/apache/zookeeper/pull/136#discussion_r330484514
 
 

 ##########
 File path: src/java/main/org/apache/zookeeper/server/WatchManager.java
 ##########
 @@ -40,50 +40,110 @@
 class WatchManager {
     private static final Logger LOG = LoggerFactory.getLogger(WatchManager.class);
 
-    private final Map<String, Set<Watcher>> watchTable =
-        new HashMap<String, Set<Watcher>>();
+    enum Type {
+        STANDARD() {
+            @Override
+            boolean isPersistent() {
+                return false;
+            }
+
+            @Override
+            boolean isRecursive() {
+                return false;
+            }
+        },
+        PERSISTENT() {
+            @Override
+            boolean isPersistent() {
+                return true;
+            }
+
+            @Override
+            boolean isRecursive() {
+                return false;
+            }
+        },
+        PERSISTENT_RECURSIVE() {
+            @Override
+            boolean isPersistent() {
+                return true;
+            }
+
+            @Override
+            boolean isRecursive() {
+                return true;
+            }
+        }
+        ;
+
+        abstract boolean isPersistent();
+        abstract boolean isRecursive();
+    }
+
+    private final Map<String, Map<Watcher, Type>> watchTable =
+        new HashMap<>();
 
     private final Map<Watcher, Set<String>> watch2Paths =
-        new HashMap<Watcher, Set<String>>();
+        new HashMap<>();
+
+    private int recursiveWatchQty = 0;    // guarded by sync
+
+    // visible for testing
+    synchronized int getRecursiveWatchQty() {
+        return recursiveWatchQty;
+    }
 
     synchronized int size(){
         int result = 0;
-        for(Set<Watcher> watches : watchTable.values()) {
+        for(Map<Watcher, Type> watches : watchTable.values()) {
             result += watches.size();
         }
         return result;
     }
 
-    synchronized void addWatch(String path, Watcher watcher) {
-        Set<Watcher> list = watchTable.get(path);
+    synchronized void addWatch(String path, Watcher watcher, WatchManager.Type type) {
+        Map<Watcher, Type> list = watchTable.get(path);
         if (list == null) {
             // 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
-            list = new HashSet<Watcher>(4);
+            list = new HashMap<>(4);
             watchTable.put(path, list);
         }
-        list.add(watcher);
+        Type previousType = list.put(watcher, type);
+        if (safeIsRecursive(previousType)) {
+            --recursiveWatchQty;
+        }
+        if (type.isRecursive()) {
+            ++recursiveWatchQty;
+        }
 
         Set<String> paths = watch2Paths.get(watcher);
         if (paths == null) {
             // cnxns typically have many watches, so use default cap here
-            paths = new HashSet<String>();
+            paths = new HashSet<>();
             watch2Paths.put(watcher, paths);
         }
         paths.add(path);
     }
 
+    private boolean safeIsRecursive(Type type) {
 
 Review comment:
   nit: static ?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message