You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by ph...@apache.org on 2014/01/24 07:18:33 UTC

svn commit: r1560904 - in /zookeeper/trunk: ./ src/ src/java/main/org/apache/zookeeper/ src/java/main/org/apache/zookeeper/server/ src/java/test/org/apache/zookeeper/

Author: phunt
Date: Fri Jan 24 06:18:32 2014
New Revision: 1560904

URL: http://svn.apache.org/r1560904
Log:
ZOOKEEPER-442. need a way to remove watches that are no longer of interest (Rakesh R, Daniel Gómez Ferro via phunt)

Added:
    zookeeper/trunk/src/java/main/org/apache/zookeeper/WatchDeregistration.java
    zookeeper/trunk/src/java/test/org/apache/zookeeper/RemoveWatchesTest.java
Modified:
    zookeeper/trunk/CHANGES.txt
    zookeeper/trunk/src/java/main/org/apache/zookeeper/ClientCnxn.java
    zookeeper/trunk/src/java/main/org/apache/zookeeper/KeeperException.java
    zookeeper/trunk/src/java/main/org/apache/zookeeper/Watcher.java
    zookeeper/trunk/src/java/main/org/apache/zookeeper/ZooDefs.java
    zookeeper/trunk/src/java/main/org/apache/zookeeper/ZooKeeper.java
    zookeeper/trunk/src/java/main/org/apache/zookeeper/server/DataTree.java
    zookeeper/trunk/src/java/main/org/apache/zookeeper/server/FinalRequestProcessor.java
    zookeeper/trunk/src/java/main/org/apache/zookeeper/server/PrepRequestProcessor.java
    zookeeper/trunk/src/java/main/org/apache/zookeeper/server/Request.java
    zookeeper/trunk/src/java/main/org/apache/zookeeper/server/WatchManager.java
    zookeeper/trunk/src/java/main/org/apache/zookeeper/server/ZKDatabase.java
    zookeeper/trunk/src/zookeeper.jute

Modified: zookeeper/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/trunk/CHANGES.txt?rev=1560904&r1=1560903&r2=1560904&view=diff
==============================================================================
--- zookeeper/trunk/CHANGES.txt (original)
+++ zookeeper/trunk/CHANGES.txt Fri Jan 24 06:18:32 2014
@@ -20,6 +20,9 @@ NEW FEATURES:
 
   ZOOKEEPER-1691. Add a flag to disable standalone mode (Helen Hastings via michim)
 
+  ZOOKEEPER-442. need a way to remove watches that are no longer of
+  interest (Rakesh R, Daniel Gómez Ferro via phunt)
+
 BUGFIXES:
 
   ZOOKEEPER-786. Exception in ZooKeeper.toString

Modified: zookeeper/trunk/src/java/main/org/apache/zookeeper/ClientCnxn.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/ClientCnxn.java?rev=1560904&r1=1560903&r2=1560904&view=diff
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/ClientCnxn.java (original)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/ClientCnxn.java Fri Jan 24 06:18:32 2014
@@ -28,10 +28,13 @@ import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.net.SocketAddress;
 import java.nio.ByteBuffer;
+import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Map;
 import java.util.Random;
 import java.util.Set;
+import java.util.Map.Entry;
 import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.concurrent.LinkedBlockingQueue;
 
@@ -50,6 +53,7 @@ import org.apache.zookeeper.AsyncCallbac
 import org.apache.zookeeper.AsyncCallback.StatCallback;
 import org.apache.zookeeper.AsyncCallback.StringCallback;
 import org.apache.zookeeper.AsyncCallback.VoidCallback;
+import org.apache.zookeeper.KeeperException.Code;
 import org.apache.zookeeper.OpResult.ErrorResult;
 import org.apache.zookeeper.Watcher.Event;
 import org.apache.zookeeper.Watcher.Event.EventType;
@@ -257,6 +261,8 @@ public class ClientCnxn {
 
         public boolean readOnly;
 
+        WatchDeregistration watchDeregistration;
+
         /** Convenience ctor */
         Packet(RequestHeader requestHeader, ReplyHeader replyHeader,
                Record request, Record response,
@@ -457,21 +463,35 @@ public class ClientCnxn {
         }
 
         public void queueEvent(WatchedEvent event) {
+            queueEvent(event, null);
+        }
+
+        private void queueEvent(WatchedEvent event,
+                Set<Watcher> materializedWatchers) {
             if (event.getType() == EventType.None
                     && sessionState == event.getState()) {
                 return;
             }
             sessionState = event.getState();
-
-            // materialize the watchers based on the event
-            WatcherSetEventPair pair = new WatcherSetEventPair(
-                    watcher.materialize(event.getState(), event.getType(),
-                            event.getPath()),
-                            event);
+            final Set<Watcher> watchers;
+            if (materializedWatchers == null) {
+                // materialize the watchers based on the event
+                watchers = watcher.materialize(event.getState(),
+                        event.getType(), event.getPath());
+            } else {
+                watchers = new HashSet<Watcher>();
+                watchers.addAll(materializedWatchers);
+            }
+            WatcherSetEventPair pair = new WatcherSetEventPair(watchers, event);
             // queue the pair (watch set & event) for later processing
             waitingEvents.add(pair);
         }
 
+        public void queueCallback(AsyncCallback cb, int rc, String path,
+                Object ctx) {
+            waitingEvents.add(new LocalCallback(cb, rc, path, ctx));
+        }
+
        public void queuePacket(Packet packet) {
           if (wasKilled) {
              synchronized (waitingEvents) {
@@ -525,7 +545,31 @@ public class ClientCnxn {
                           LOG.error("Error while calling watcher ", t);
                       }
                   }
-              } else {
+                } else if (event instanceof LocalCallback) {
+                    LocalCallback lcb = (LocalCallback) event;
+                    if (lcb.cb instanceof StatCallback) {
+                        ((StatCallback) lcb.cb).processResult(lcb.rc, lcb.path,
+                                lcb.ctx, null);
+                    } else if (lcb.cb instanceof DataCallback) {
+                        ((DataCallback) lcb.cb).processResult(lcb.rc, lcb.path,
+                                lcb.ctx, null, null);
+                    } else if (lcb.cb instanceof ACLCallback) {
+                        ((ACLCallback) lcb.cb).processResult(lcb.rc, lcb.path,
+                                lcb.ctx, null, null);
+                    } else if (lcb.cb instanceof ChildrenCallback) {
+                        ((ChildrenCallback) lcb.cb).processResult(lcb.rc,
+                                lcb.path, lcb.ctx, null);
+                    } else if (lcb.cb instanceof Children2Callback) {
+                        ((Children2Callback) lcb.cb).processResult(lcb.rc,
+                                lcb.path, lcb.ctx, null, null);
+                    } else if (lcb.cb instanceof StringCallback) {
+                        ((StringCallback) lcb.cb).processResult(lcb.rc,
+                                lcb.path, lcb.ctx, null);
+                    } else {
+                        ((VoidCallback) lcb.cb).processResult(lcb.rc, lcb.path,
+                                lcb.ctx);
+                    }
+                } else {
                   Packet p = (Packet) event;
                   int rc = 0;
                   String clientPath = p.clientPath;
@@ -646,8 +690,34 @@ public class ClientCnxn {
     }
 
     private void finishPacket(Packet p) {
+        int err = p.replyHeader.getErr();
         if (p.watchRegistration != null) {
-            p.watchRegistration.register(p.replyHeader.getErr());
+            p.watchRegistration.register(err);
+        }
+        // Add all the removed watch events to the event queue, so that the
+        // clients will be notified with 'Data/Child WatchRemoved' event type.
+        if (p.watchDeregistration != null) {
+            Map<EventType, Set<Watcher>> materializedWatchers = null;
+            try {
+                materializedWatchers = p.watchDeregistration.unregister(err);
+                for (Entry<EventType, Set<Watcher>> entry : materializedWatchers
+                        .entrySet()) {
+                    Set<Watcher> watchers = entry.getValue();
+                    if (watchers.size() > 0) {
+                        queueEvent(p.watchDeregistration.getClientPath(), err,
+                                watchers, entry.getKey());
+                        // ignore connectionloss when removing from local
+                        // session
+                        p.replyHeader.setErr(Code.OK.intValue());
+                    }
+                }
+            } catch (KeeperException.NoWatcherException nwe) {
+                LOG.error("Failed to find watcher!", nwe);
+                p.replyHeader.setErr(nwe.code().intValue());
+            } catch (KeeperException ke) {
+                LOG.error("Exception when removing watcher", ke);
+                p.replyHeader.setErr(ke.code().intValue());
+            }
         }
 
         if (p.cb == null) {
@@ -661,6 +731,22 @@ public class ClientCnxn {
         }
     }
 
+    void queueEvent(String clientPath, int err,
+            Set<Watcher> materializedWatchers, EventType eventType) {
+        KeeperState sessionState = KeeperState.SyncConnected;
+        if (KeeperException.Code.SESSIONEXPIRED.intValue() == err
+                || KeeperException.Code.CONNECTIONLOSS.intValue() == err) {
+            sessionState = Event.KeeperState.Disconnected;
+        }
+        WatchedEvent event = new WatchedEvent(eventType, sessionState,
+                clientPath);
+        eventThread.queueEvent(event, materializedWatchers);
+    }
+
+    void queueCallback(AsyncCallback cb, int rc, String path, Object ctx) {
+        eventThread.queueCallback(cb, rc, path, ctx);
+    }
+
     private void conLossPacket(Packet p) {
         if (p.replyHeader == null) {
             return;
@@ -1360,9 +1446,16 @@ public class ClientCnxn {
     public ReplyHeader submitRequest(RequestHeader h, Record request,
             Record response, WatchRegistration watchRegistration)
             throws InterruptedException {
+        return submitRequest(h, request, response, watchRegistration, null);
+    }
+
+    public ReplyHeader submitRequest(RequestHeader h, Record request,
+            Record response, WatchRegistration watchRegistration,
+            WatchDeregistration watchDeregistration)
+            throws InterruptedException {
         ReplyHeader r = new ReplyHeader();
         Packet packet = queuePacket(h, r, request, response, null, null, null,
-                    null, watchRegistration);
+                null, watchRegistration, watchDeregistration);
         synchronized (packet) {
             while (!packet.finished) {
                 packet.wait();
@@ -1394,8 +1487,15 @@ public class ClientCnxn {
 
     Packet queuePacket(RequestHeader h, ReplyHeader r, Record request,
             Record response, AsyncCallback cb, String clientPath,
-            String serverPath, Object ctx, WatchRegistration watchRegistration)
-    {
+            String serverPath, Object ctx, WatchRegistration watchRegistration) {
+        return queuePacket(h, r, request, response, cb, clientPath, serverPath,
+                ctx, watchRegistration, null);
+    }
+
+    Packet queuePacket(RequestHeader h, ReplyHeader r, Record request,
+            Record response, AsyncCallback cb, String clientPath,
+            String serverPath, Object ctx, WatchRegistration watchRegistration,
+            WatchDeregistration watchDeregistration) {
         Packet packet = null;
 
         // Note that we do not generate the Xid for the packet yet. It is
@@ -1407,6 +1507,7 @@ public class ClientCnxn {
             packet.ctx = ctx;
             packet.clientPath = clientPath;
             packet.serverPath = serverPath;
+            packet.watchDeregistration = watchDeregistration;
             if (!state.isAlive() || closing) {
                 conLossPacket(packet);
             } else {
@@ -1435,4 +1536,18 @@ public class ClientCnxn {
     States getState() {
         return state;
     }
+
+    private static class LocalCallback {
+        private final AsyncCallback cb;
+        private final int rc;
+        private final String path;
+        private final Object ctx;
+
+        public LocalCallback(AsyncCallback cb, int rc, String path, Object ctx) {
+            this.cb = cb;
+            this.rc = rc;
+            this.path = path;
+            this.ctx = ctx;
+        }
+    }
 }

Modified: zookeeper/trunk/src/java/main/org/apache/zookeeper/KeeperException.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/KeeperException.java?rev=1560904&r1=1560903&r2=1560904&view=diff
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/KeeperException.java (original)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/KeeperException.java Fri Jan 24 06:18:32 2014
@@ -137,6 +137,8 @@ public abstract class KeeperException ex
                 return new NotReadOnlyException();
             case EPHEMERALONLOCALSESSION:
                 return new EphemeralOnLocalSessionException();
+            case NOWATCHER:
+                return new NoWatcherException();
             case OK:
             default:
                 throw new IllegalArgumentException("Invalid exception code");
@@ -372,7 +374,9 @@ public abstract class KeeperException ex
         /** State-changing request is passed to read-only server */
         NOTREADONLY (-119),
         /** Attempt to create ephemeral node on a local session */
-        EPHEMERALONLOCALSESSION (EphemeralOnLocalSession);
+        EPHEMERALONLOCALSESSION (EphemeralOnLocalSession),
+        /** Attempts to remove a non-existing watcher */
+        NOWATCHER (-123);
 
         private static final Map<Integer,Code> lookup
             = new HashMap<Integer,Code>();
@@ -455,6 +459,8 @@ public abstract class KeeperException ex
                 return "Not a read-only call";
             case EPHEMERALONLOCALSESSION:
                 return "Ephemeral node on local session";
+            case NOWATCHER:
+                return "No such watcher";
             default:
                 return "Unknown error " + code;
         }
@@ -768,4 +774,17 @@ public abstract class KeeperException ex
             super(Code.UNIMPLEMENTED);
         }
     }
+
+    /**
+     * @see Code#NOWATCHER
+     */
+    public static class NoWatcherException extends KeeperException {
+        public NoWatcherException() {
+            super(Code.NOWATCHER);
+        }
+
+        public NoWatcherException(String path) {
+            super(Code.NOWATCHER, path);
+        }
+    }
 }

Added: zookeeper/trunk/src/java/main/org/apache/zookeeper/WatchDeregistration.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/WatchDeregistration.java?rev=1560904&view=auto
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/WatchDeregistration.java (added)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/WatchDeregistration.java Fri Jan 24 06:18:32 2014
@@ -0,0 +1,70 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper;
+
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.zookeeper.Watcher.WatcherType;
+import org.apache.zookeeper.Watcher.Event.EventType;
+import org.apache.zookeeper.ZooKeeper.ZKWatchManager;
+
+/**
+ * Handles the special case of removing watches which has registered for a
+ * client path
+ */
+public class WatchDeregistration {
+
+    private final String clientPath;
+    private final Watcher watcher;
+    private final WatcherType watcherType;
+    private final boolean local;
+    private final ZKWatchManager zkManager;
+
+    public WatchDeregistration(String clientPath, Watcher watcher,
+            WatcherType watcherType, boolean local, ZKWatchManager zkManager) {
+        this.clientPath = clientPath;
+        this.watcher = watcher;
+        this.watcherType = watcherType;
+        this.local = local;
+        this.zkManager = zkManager;
+    }
+
+    /**
+     * Unregistering watcher that was added on path.
+     * 
+     * @param rc
+     *            the result code of the operation that attempted to remove
+     *            watch on the path.
+     */
+    public Map<EventType, Set<Watcher>> unregister(int rc)
+            throws KeeperException {
+        return zkManager.removeWatcher(clientPath, watcher, watcherType, local,
+                rc);
+    }
+
+    /**
+     * Returns client path which has specified for unregistering its watcher
+     * 
+     * @return client path
+     */
+    public String getClientPath() {
+        return clientPath;
+    }
+}

Modified: zookeeper/trunk/src/java/main/org/apache/zookeeper/Watcher.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/Watcher.java?rev=1560904&r1=1560903&r2=1560904&view=diff
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/Watcher.java (original)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/Watcher.java Fri Jan 24 06:18:32 2014
@@ -117,7 +117,9 @@ public interface Watcher {
             NodeCreated (1),
             NodeDeleted (2),
             NodeDataChanged (3),
-            NodeChildrenChanged (4);
+            NodeChildrenChanged (4),
+            DataWatchRemoved (5),
+            ChildWatchRemoved (6);
 
             private final int intValue;     // Integer representation of value
                                             // for sending over wire
@@ -137,6 +139,8 @@ public interface Watcher {
                     case  2: return EventType.NodeDeleted;
                     case  3: return EventType.NodeDataChanged;
                     case  4: return EventType.NodeChildrenChanged;
+                    case  5: return EventType.DataWatchRemoved;
+                    case  6: return EventType.ChildWatchRemoved;
 
                     default:
                         throw new RuntimeException("Invalid integer value for conversion to EventType");
@@ -145,5 +149,38 @@ public interface Watcher {
         }
     }
 
+    /**
+     * Enumeration of types of watchers
+     */
+    public enum WatcherType {
+        Children(1), Data(2), Any(3);
+
+        // Integer representation of value
+        private final int intValue;
+
+        private WatcherType(int intValue) {
+            this.intValue = intValue;
+        }
+
+        public int getIntValue() {
+            return intValue;
+        }
+
+        public static WatcherType fromInt(int intValue) {
+            switch (intValue) {
+            case 1:
+                return WatcherType.Children;
+            case 2:
+                return WatcherType.Data;
+            case 3:
+                return WatcherType.Any;
+
+            default:
+                throw new RuntimeException(
+                        "Invalid integer value for conversion to WatcherType");
+            }
+        }
+    }
+
     abstract public void process(WatchedEvent event);
 }

Modified: zookeeper/trunk/src/java/main/org/apache/zookeeper/ZooDefs.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/ZooDefs.java?rev=1560904&r1=1560903&r2=1560904&view=diff
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/ZooDefs.java (original)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/ZooDefs.java Fri Jan 24 06:18:32 2014
@@ -60,7 +60,9 @@ public class ZooDefs {
         public final int create2 = 15;
 
         public final int reconfig = 16;
-        
+
+        public final int removeWatches = 17;
+
         public final int auth = 100;
 
         public final int setWatches = 101;

Modified: zookeeper/trunk/src/java/main/org/apache/zookeeper/ZooKeeper.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/ZooKeeper.java?rev=1560904&r1=1560903&r2=1560904&view=diff
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/ZooKeeper.java (original)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/ZooKeeper.java Fri Jan 24 06:18:32 2014
@@ -38,7 +38,11 @@ import org.apache.zookeeper.AsyncCallbac
 import org.apache.zookeeper.AsyncCallback.StatCallback;
 import org.apache.zookeeper.AsyncCallback.StringCallback;
 import org.apache.zookeeper.AsyncCallback.VoidCallback;
+import org.apache.zookeeper.KeeperException.Code;
+import org.apache.zookeeper.KeeperException.NoWatcherException;
 import org.apache.zookeeper.OpResult.ErrorResult;
+import org.apache.zookeeper.Watcher.Event.EventType;
+import org.apache.zookeeper.Watcher.WatcherType;
 import org.apache.zookeeper.client.ConnectStringParser;
 import org.apache.zookeeper.client.StaticHostProvider;
 import org.apache.zookeeper.client.ZooKeeperSaslClient;
@@ -61,6 +65,7 @@ import org.apache.zookeeper.proto.GetChi
 import org.apache.zookeeper.proto.GetDataRequest;
 import org.apache.zookeeper.proto.GetDataResponse;
 import org.apache.zookeeper.proto.ReconfigRequest;
+import org.apache.zookeeper.proto.RemoveWatchesRequest;
 import org.apache.zookeeper.proto.ReplyHeader;
 import org.apache.zookeeper.proto.RequestHeader;
 import org.apache.zookeeper.proto.SetACLRequest;
@@ -219,7 +224,7 @@ public class ZooKeeper {
      * the public methods will not be exposed as part of the ZooKeeper client
      * API.
      */
-    private static class ZKWatchManager implements ClientWatchManager {
+    static class ZKWatchManager implements ClientWatchManager {
         private final Map<String, Set<Watcher>> dataWatches =
             new HashMap<String, Set<Watcher>>();
         private final Map<String, Set<Watcher>> existWatches =
@@ -235,6 +240,178 @@ public class ZooKeeper {
             }
         }
 
+        public Map<EventType, Set<Watcher>> removeWatcher(String clientPath,
+                Watcher watcher, WatcherType watcherType, boolean local, int rc)
+                throws KeeperException {
+            // Validate the provided znode path contains the given watcher of
+            // watcherType
+            containsWatcher(clientPath, watcher, watcherType);
+
+            Map<EventType, Set<Watcher>> removedWatchers = new HashMap<EventType, Set<Watcher>>();
+            HashSet<Watcher> childWatchersToRem = new HashSet<Watcher>();
+            removedWatchers
+                    .put(EventType.ChildWatchRemoved, childWatchersToRem);
+            HashSet<Watcher> dataWatchersToRem = new HashSet<Watcher>();
+            removedWatchers.put(EventType.DataWatchRemoved, dataWatchersToRem);
+            boolean removedWatcher = false;
+            switch (watcherType) {
+            case Children: {
+                synchronized (childWatches) {
+                    removedWatcher = removeWatches(childWatches, watcher,
+                            clientPath, local, rc, childWatchersToRem);
+                }
+                break;
+            }
+            case Data: {
+                synchronized (dataWatches) {
+                    removedWatcher = removeWatches(dataWatches, watcher,
+                            clientPath, local, rc, dataWatchersToRem);
+                }
+
+                synchronized (existWatches) {
+                    boolean removedDataWatcher = removeWatches(existWatches,
+                            watcher, clientPath, local, rc, dataWatchersToRem);
+                    removedWatcher |= removedDataWatcher;
+                }
+                break;
+            }
+            case Any: {
+                synchronized (childWatches) {
+                    removedWatcher = removeWatches(childWatches, watcher,
+                            clientPath, local, rc, childWatchersToRem);
+                }
+
+                synchronized (dataWatches) {
+                    boolean removedDataWatcher = removeWatches(dataWatches,
+                            watcher, clientPath, local, rc, dataWatchersToRem);
+                    removedWatcher |= removedDataWatcher;
+                }
+                synchronized (existWatches) {
+                    boolean removedDataWatcher = removeWatches(existWatches,
+                            watcher, clientPath, local, rc, dataWatchersToRem);
+                    removedWatcher |= removedDataWatcher;
+                }
+            }
+            }
+            // Watcher function doesn't exists for the specified params
+            if (!removedWatcher) {
+                throw new KeeperException.NoWatcherException(clientPath);
+            }
+            return removedWatchers;
+        }
+
+        private boolean contains(String path, Watcher watcherObj,
+                Map<String, Set<Watcher>> pathVsWatchers) {
+            boolean watcherExists = true;
+            if (pathVsWatchers == null || pathVsWatchers.size() == 0) {
+                watcherExists = false;
+            } else {
+                Set<Watcher> watchers = pathVsWatchers.get(path);
+                if (watchers == null) {
+                    watcherExists = false;
+                } else if (watcherObj == null) {
+                    watcherExists = watchers.size() > 0;
+                } else {
+                    watcherExists = watchers.contains(watcherObj);
+                }
+            }
+            return watcherExists;
+        }
+
+        /**
+         * Validate the provided znode path contains the given watcher and
+         * watcherType
+         * 
+         * @param path
+         *            - client path
+         * @param watcher
+         *            - watcher object reference
+         * @param watcherType
+         *            - type of the watcher
+         * @throws NoWatcherException
+        */
+        void containsWatcher(String path, Watcher watcher,
+                WatcherType watcherType) throws NoWatcherException{
+            boolean containsWatcher = false;
+            switch (watcherType) {
+            case Children: {
+                synchronized (childWatches) {
+                    containsWatcher = contains(path, watcher, childWatches);
+                }
+                break;
+            }
+            case Data: {
+                synchronized (dataWatches) {
+                    containsWatcher = contains(path, watcher, dataWatches);
+                }
+
+                synchronized (existWatches) {
+                    boolean contains_temp = contains(path, watcher,
+                            existWatches);
+                    containsWatcher |= contains_temp;
+                }
+                break;
+            }
+            case Any: {
+                synchronized (childWatches) {
+                    containsWatcher = contains(path, watcher, childWatches);
+                }
+
+                synchronized (dataWatches) {
+                    boolean contains_temp = contains(path, watcher, dataWatches);
+                    containsWatcher |= contains_temp;
+                }
+                synchronized (existWatches) {
+                    boolean contains_temp = contains(path, watcher,
+                            existWatches);
+                    containsWatcher |= contains_temp;
+                }
+            }
+            }
+            // Watcher function doesn't exists for the specified params
+            if (!containsWatcher) {
+                throw new KeeperException.NoWatcherException(path);
+            }
+        }
+
+        private boolean removeWatches(Map<String, Set<Watcher>> pathVsWatcher,
+                Watcher watcher, String path, boolean local, int rc,
+                Set<Watcher> removedWatchers) throws KeeperException {
+            if (!local && rc != Code.OK.intValue()) {
+                throw KeeperException
+                        .create(KeeperException.Code.get(rc), path);
+            }
+            boolean success = false;
+            // When local flag is true, remove watchers for the given path
+            // irrespective of rc. Otherwise shouldn't remove watchers locally
+            // when sees failure from server.
+            if (rc == Code.OK.intValue() || (local && rc != Code.OK.intValue())) {
+                // Remove all the watchers for the given path
+                if (watcher == null) {
+                    Set<Watcher> pathWatchers = pathVsWatcher.remove(path);
+                    if (pathWatchers != null) {
+                        // found path watchers
+                        removedWatchers.addAll(pathWatchers);
+                        success = true;
+                    }
+                } else {
+                    Set<Watcher> watchers = pathVsWatcher.get(path);
+                    if (watchers != null) {
+                        if (watchers.remove(watcher)) {
+                            // found path watcher
+                            removedWatchers.add(watcher);
+                            // cleanup <path vs watchlist>
+                            if (watchers.size() <= 0) {
+                                pathVsWatcher.remove(path);
+                            }
+                            success = true;
+                        }
+                    }
+                }
+            }
+            return success;
+        }
+        
         /* (non-Javadoc)
          * @see org.apache.zookeeper.ClientWatchManager#materialize(Event.KeeperState, 
          *                                                        Event.EventType, java.lang.String)
@@ -2135,6 +2312,95 @@ public class ZooKeeper {
                 clientPath, serverPath, ctx, null);
     }
 
+    /**
+     * For the given znode path, removes the specified watcher of given
+     * watcherType.
+     * 
+     * <p>
+     * If watcher is null, all watchers for the given path and watcherType will
+     * be removed. Otherwise only the specified watcher corresponding to the
+     * passed path and watcherType will be removed.
+     * <p>
+     * A successful call guarantees that, the removed watcher won't be
+     * triggered.
+     * <p>
+     * 
+     * @param path
+     *            - the path of the node
+     * @param watcher
+     *            - a concrete watcher or null to remove all watchers for the
+     *            given path and watcherType
+     * @param watcherType
+     *            - the type of watcher to be removed
+     * @param local
+     *            - whether the watcher can be removed locally when there is no
+     *            server connection
+     * @throws InterruptedException
+     *             if the server transaction is interrupted.
+     * @throws KeeperException.NoWatcher
+     *             if no watcher exists that match the specified parameters
+     * @throws KeeperException
+     *             if the server signals an error with a non-zero error code.
+     * @throws IllegalArgumentException
+     *             if an invalid path is specified
+     * 
+     * @since 3.5.0
+     */
+    public void removeWatches(String path, Watcher watcher,
+            WatcherType watcherType, boolean local)
+            throws InterruptedException, KeeperException {
+        PathUtils.validatePath(path);
+        final String clientPath = path;
+        // Validating the existence of watcher.
+        watchManager.containsWatcher(clientPath, watcher, watcherType);
+        final String serverPath = prependChroot(clientPath);
+
+        WatchDeregistration wcb = new WatchDeregistration(clientPath, watcher,
+                watcherType, local, watchManager);
+        RequestHeader h = new RequestHeader();
+        h.setType(ZooDefs.OpCode.removeWatches);
+        RemoveWatchesRequest request = new RemoveWatchesRequest();
+        request.setPath(serverPath);
+        request.setType(watcherType.getIntValue());
+
+        ReplyHeader r = cnxn.submitRequest(h, request, null, null, wcb);
+        if (r.getErr() != 0) {
+            throw KeeperException.create(KeeperException.Code.get(r.getErr()),
+                    clientPath);
+        }
+    }
+
+    /**
+     * The asynchronous version of removeWatches.
+     * 
+     * @see #removeWatches
+     */
+    public void removeWatches(String path, Watcher watcher,
+            WatcherType watcherType, boolean local, VoidCallback cb, Object ctx) {
+        final String clientPath = path;
+        PathUtils.validatePath(clientPath);
+
+        // Validating the existence of watcher.
+        try {
+            watchManager.containsWatcher(clientPath, watcher, watcherType);
+        } catch (NoWatcherException nwe) {
+            LOG.error("Failed to find watcher!", nwe);
+            cb.processResult(nwe.code().intValue(), clientPath, ctx);
+            return;
+        }
+
+        final String serverPath = prependChroot(clientPath);
+        WatchDeregistration wcb = new WatchDeregistration(clientPath, watcher,
+                watcherType, local, watchManager);
+        RequestHeader h = new RequestHeader();
+        h.setType(ZooDefs.OpCode.removeWatches);
+        RemoveWatchesRequest request = new RemoveWatchesRequest();
+        request.setPath(serverPath);
+        request.setType(watcherType.getIntValue());
+        cnxn.queuePacket(h, new ReplyHeader(), request, null, cb, clientPath,
+                serverPath, ctx, null, wcb);
+    }
+
     public States getState() {
         return cnxn.getState();
     }

Modified: zookeeper/trunk/src/java/main/org/apache/zookeeper/server/DataTree.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/server/DataTree.java?rev=1560904&r1=1560903&r2=1560904&view=diff
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/server/DataTree.java (original)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/server/DataTree.java Fri Jan 24 06:18:32 2014
@@ -22,7 +22,6 @@ import java.io.IOException;
 import java.io.PrintWriter;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -43,6 +42,7 @@ import org.apache.zookeeper.Quotas;
 import org.apache.zookeeper.StatsTrack;
 import org.apache.zookeeper.WatchedEvent;
 import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.Watcher.WatcherType;
 import org.apache.zookeeper.ZooDefs;
 import org.apache.zookeeper.Watcher.Event;
 import org.apache.zookeeper.Watcher.Event.EventType;
@@ -1362,4 +1362,19 @@ public class DataTree {
             }
         }
     }
+
+    public void removeWatch(String path, WatcherType type, Watcher watcher) {
+        switch (type) {
+        case Children:
+            this.childWatches.removeWatcher(path, watcher);
+            break;
+        case Data:
+            this.dataWatches.removeWatcher(path, watcher);
+            break;
+        case Any:
+            this.childWatches.removeWatcher(path, watcher);
+            this.dataWatches.removeWatcher(path, watcher);
+            break;
+        }
+    }
 }

Modified: zookeeper/trunk/src/java/main/org/apache/zookeeper/server/FinalRequestProcessor.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/server/FinalRequestProcessor.java?rev=1560904&r1=1560903&r2=1560904&view=diff
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/server/FinalRequestProcessor.java (original)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/server/FinalRequestProcessor.java Fri Jan 24 06:18:32 2014
@@ -27,6 +27,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.apache.zookeeper.KeeperException;
 import org.apache.zookeeper.MultiResponse;
+import org.apache.zookeeper.Watcher.WatcherType;
 import org.apache.zookeeper.ZooDefs;
 import org.apache.zookeeper.KeeperException.Code;
 import org.apache.zookeeper.KeeperException.SessionMovedException;
@@ -45,6 +46,7 @@ import org.apache.zookeeper.proto.GetChi
 import org.apache.zookeeper.proto.GetChildrenResponse;
 import org.apache.zookeeper.proto.GetDataRequest;
 import org.apache.zookeeper.proto.GetDataResponse;
+import org.apache.zookeeper.proto.RemoveWatchesRequest;
 import org.apache.zookeeper.proto.ReplyHeader;
 import org.apache.zookeeper.proto.SetACLResponse;
 import org.apache.zookeeper.proto.SetDataResponse;
@@ -391,6 +393,16 @@ public class FinalRequestProcessor imple
                 rsp = new GetChildren2Response(children, stat);
                 break;
             }
+            case OpCode.removeWatches: {
+                lastOp = "REMW";
+                RemoveWatchesRequest removeWatches = new RemoveWatchesRequest();
+                ByteBufferInputStream.byteBuffer2Record(request.request,
+                        removeWatches);
+                WatcherType type = WatcherType.fromInt(removeWatches.getType());
+                zks.getZKDatabase().removeWatch(removeWatches.getPath(), type,
+                        cnxn);
+                break;
+            }
             }
         } catch (SessionMovedException e) {
             // session moved is a connection level error, we need to tear

Modified: zookeeper/trunk/src/java/main/org/apache/zookeeper/server/PrepRequestProcessor.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/server/PrepRequestProcessor.java?rev=1560904&r1=1560903&r2=1560904&view=diff
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/server/PrepRequestProcessor.java (original)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/server/PrepRequestProcessor.java Fri Jan 24 06:18:32 2014
@@ -833,6 +833,7 @@ public class PrepRequestProcessor extend
             case OpCode.getChildren2:
             case OpCode.ping:
             case OpCode.setWatches:
+            case OpCode.removeWatches:
                 zks.sessionTracker.checkSession(request.sessionId,
                         request.getOwner());
                 break;

Modified: zookeeper/trunk/src/java/main/org/apache/zookeeper/server/Request.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/server/Request.java?rev=1560904&r1=1560903&r2=1560904&view=diff
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/server/Request.java (original)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/server/Request.java Fri Jan 24 06:18:32 2014
@@ -150,6 +150,7 @@ public class Request {
         case OpCode.setData:
         case OpCode.setWatches:
         case OpCode.sync:
+        case OpCode.removeWatches:
             return true;
         default:
             return false;
@@ -224,6 +225,8 @@ public class Request {
             return "error";
         case OpCode.reconfig:
            return "reconfig";
+        case OpCode.removeWatches:
+            return "removeWatches";
         default:
             return "unknown " + op;
         }

Modified: zookeeper/trunk/src/java/main/org/apache/zookeeper/server/WatchManager.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/server/WatchManager.java?rev=1560904&r1=1560903&r2=1560904&view=diff
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/server/WatchManager.java (original)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/server/WatchManager.java Fri Jan 24 06:18:32 2014
@@ -168,4 +168,20 @@ class WatchManager {
             }
         }
     }
+
+    synchronized void removeWatcher(String path, Watcher watcher) {
+        HashSet<String> paths = watch2Paths.get(watcher);
+        if (paths == null) {
+            return;
+        }
+        paths.remove(path);
+        HashSet<Watcher> list = watchTable.get(path);
+        if (list == null) {
+            return;
+        }
+        list.remove(watcher);
+        if (list.size() == 0) {
+            watchTable.remove(path);
+        }
+    }
 }

Modified: zookeeper/trunk/src/java/main/org/apache/zookeeper/server/ZKDatabase.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/server/ZKDatabase.java?rev=1560904&r1=1560903&r2=1560904&view=diff
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/server/ZKDatabase.java (original)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/server/ZKDatabase.java Fri Jan 24 06:18:32 2014
@@ -38,6 +38,7 @@ import org.apache.jute.Record;
 import org.apache.zookeeper.KeeperException;
 import org.apache.zookeeper.KeeperException.NoNodeException;
 import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.Watcher.WatcherType;
 import org.apache.zookeeper.ZooDefs;
 import org.apache.zookeeper.data.ACL;
 import org.apache.zookeeper.data.Stat;
@@ -576,4 +577,18 @@ public class ZKDatabase {
     public void setSnapshotSizeFactor(double snapshotSizeFactor) {
         this.snapshotSizeFactor = snapshotSizeFactor;
     }
+
+    /**
+     * Remove watch from the datatree
+     * 
+     * @param path
+     *            node to remove watches from
+     * @param type
+     *            type of watcher to remove
+     * @param watcher
+     *            watcher function to remove
+     */
+    public void removeWatch(String path, WatcherType type, Watcher watcher) {
+        dataTree.removeWatch(path, type, watcher);
+    }
 }

Added: zookeeper/trunk/src/java/test/org/apache/zookeeper/RemoveWatchesTest.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/test/org/apache/zookeeper/RemoveWatchesTest.java?rev=1560904&view=auto
==============================================================================
--- zookeeper/trunk/src/java/test/org/apache/zookeeper/RemoveWatchesTest.java (added)
+++ zookeeper/trunk/src/java/test/org/apache/zookeeper/RemoveWatchesTest.java Fri Jan 24 06:18:32 2014
@@ -0,0 +1,762 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.zookeeper.KeeperException.Code;
+import org.apache.zookeeper.Watcher.Event.EventType;
+import org.apache.zookeeper.Watcher.WatcherType;
+import org.apache.zookeeper.ZooDefs.Ids;
+import org.apache.zookeeper.test.ClientBase;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Verifies removing watches using ZooKeeper client apis
+ */
+@RunWith(Parameterized.class)
+public class RemoveWatchesTest extends ClientBase {
+    private static final Logger LOG = LoggerFactory
+            .getLogger(RemoveWatchesTest.class);
+    private ZooKeeper zk1 = null;
+    private ZooKeeper zk2 = null;
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        zk1 = createClient();
+        zk2 = createClient();
+    }
+
+    @Override
+    public void tearDown() throws Exception {
+        if (zk1 != null)
+            zk1.close();
+        if (zk2 != null)
+            zk2.close();
+        super.tearDown();
+    }
+
+    private final boolean useAsync;
+
+    public RemoveWatchesTest(boolean useAsync) {
+        this.useAsync = useAsync;
+    }
+
+    @Parameters
+    public static Collection<Object[]> configs() {
+        return Arrays.asList(new Object[][] { { false }, { true }, });
+    }
+
+    private void removeWatches(ZooKeeper zk, String path, Watcher watcher,
+            WatcherType watcherType, boolean local, KeeperException.Code rc)
+            throws InterruptedException, KeeperException {
+        LOG.info(
+                "Sending removeWatches req using zk {} path: {} type: {} watcher: {} ",
+                new Object[] { zk, path, watcherType, watcher });
+        if (useAsync) {
+            MyCallback c1 = new MyCallback(rc.intValue(), path);
+            zk.removeWatches(path, watcher, watcherType, local, c1, null);
+            Assert.assertTrue("Didn't succeeds removeWatch operation",
+                    c1.matches());
+            if (KeeperException.Code.OK.intValue() != c1.rc) {
+                KeeperException ke = KeeperException
+                        .create(KeeperException.Code.get(c1.rc));
+                throw ke;
+            }
+        } else {
+            zk.removeWatches(path, watcher, watcherType, local);
+        }
+    }
+
+    /**
+     * Test verifies removal of single watcher when there is server connection
+     */
+    @Test(timeout = 90000)
+    public void testRemoveSingleWatcher() throws Exception {
+        zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
+        zk1.create("/node2", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
+        MyWatcher w1 = new MyWatcher("/node1", 1);
+        LOG.info("Adding data watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        Assert.assertNotNull("Didn't set data watches",
+                zk2.exists("/node1", w1));
+        MyWatcher w2 = new MyWatcher("/node2", 1);
+        LOG.info("Adding data watcher {} on path {}", new Object[] { w2,
+                "/node1" });
+        Assert.assertNotNull("Didn't set data watches",
+                zk2.exists("/node2", w2));
+        removeWatches(zk2, "/node1", w1, WatcherType.Data, false, Code.OK);
+        // closing session should remove ephemeral nodes and trigger data
+        // watches if any
+        if (zk1 != null) {
+            zk1.close();
+            zk1 = null;
+        }
+        Assert.assertTrue("Didn't remove data watcher", w1.matches());
+        Assert.assertFalse("Should have removed data watcher", w2.matches());
+
+        List<EventType> events = w1.getEventsAfterWatchRemoval();
+        Assert.assertFalse(
+                "Shouldn't get NodeDeletedEvent after watch removal",
+                events.contains(EventType.NodeDeleted));
+        Assert.assertEquals(
+                "Shouldn't get NodeDeletedEvent after watch removal", 0,
+                events.size());
+    }
+
+    /**
+     * Test verifies removal of multiple data watchers when there is server
+     * connection
+     */
+    @Test(timeout = 90000)
+    public void testMultipleDataWatchers() throws IOException,
+            InterruptedException, KeeperException {
+        zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
+        MyWatcher w1 = new MyWatcher("/node1", 1);
+        LOG.info("Adding data watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        Assert.assertNotNull("Didn't set data watches",
+                zk2.exists("/node1", w1));
+        MyWatcher w2 = new MyWatcher("/node1", 1);
+        LOG.info("Adding data watcher {} on path {}", new Object[] { w2,
+                "/node1" });
+        Assert.assertNotNull("Didn't set data watches",
+                zk2.exists("/node1", w2));
+        removeWatches(zk2, "/node1", w2, WatcherType.Data, false, Code.OK);
+        // closing session should remove ephemeral nodes and trigger data
+        // watches if any
+        if (zk1 != null) {
+            zk1.close();
+            zk1 = null;
+        }
+        Assert.assertTrue("Didn't remove data watcher", w2.matches());
+        Assert.assertFalse("Should have removed data watcher", w1.matches());
+
+        List<EventType> events = w2.getEventsAfterWatchRemoval();
+        Assert.assertEquals(
+                "Shouldn't get NodeDeletedEvent after watch removal", 0,
+                events.size());
+    }
+
+    /**
+     * Test verifies removal of multiple child watchers when there is server
+     * connection
+     */
+    @Test(timeout = 90000)
+    public void testMultipleChildWatchers() throws IOException,
+            InterruptedException, KeeperException {
+        zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+        MyWatcher w1 = new MyWatcher("/node1", 1);
+        LOG.info("Adding child watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        zk2.getChildren("/node1", w1);
+        MyWatcher w2 = new MyWatcher("/node1", 1);
+        LOG.info("Adding child watcher {} on path {}", new Object[] { w2,
+                "/node1" });
+        zk2.getChildren("/node1", w2);
+        removeWatches(zk2, "/node1", w2, WatcherType.Children, false, Code.OK);
+        Assert.assertTrue("Didn't remove child watcher", w2.matches());
+        Assert.assertFalse("Should have removed child watcher", w1.matches());
+        // create child to see NodeChildren notification
+        zk1.create("/node1/node2", null, Ids.OPEN_ACL_UNSAFE,
+                CreateMode.PERSISTENT);
+        // waiting for child watchers to be notified
+        int count = 30;
+        while (count > 0) {
+            if (w1.getEventsAfterWatchRemoval().size() > 0) {
+                break;
+            }
+            count--;
+            Thread.sleep(100);
+        }
+        // watcher2
+        List<EventType> events = w2.getEventsAfterWatchRemoval();
+        Assert.assertEquals("Shouldn't get NodeChildrenChanged event", 0,
+                events.size());
+    }
+
+    /**
+     * Test verifies null watcher with WatcherType.Any - remove all the watchers
+     * data, child, exists
+     */
+    @Test(timeout = 90000)
+    public void testRemoveAllWatchers() throws IOException,
+            InterruptedException, KeeperException {
+        zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+        MyWatcher w1 = new MyWatcher("/node1", 2);
+        MyWatcher w2 = new MyWatcher("/node1", 2);
+        LOG.info("Adding data watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        Assert.assertNotNull("Didn't set data watches",
+                zk2.exists("/node1", w1));
+        LOG.info("Adding data watcher {} on path {}", new Object[] { w2,
+                "/node1" });
+        Assert.assertNotNull("Didn't set data watches",
+                zk2.exists("/node1", w2));
+        LOG.info("Adding child watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        zk2.getChildren("/node1", w1);
+        LOG.info("Adding child watcher {} on path {}", new Object[] { w2,
+                "/node1" });
+        zk2.getChildren("/node1", w2);
+        removeWatches(zk2, "/node1", null, WatcherType.Any, false, Code.OK);
+        zk1.create("/node1/child", null, Ids.OPEN_ACL_UNSAFE,
+                CreateMode.EPHEMERAL);
+        Assert.assertTrue("Didn't remove data watcher", w1.matches());
+        Assert.assertTrue("Didn't remove child watcher", w2.matches());
+    }
+
+    /**
+     * Test verifies null watcher with WatcherType.Data - remove all data
+     * watchers. Child watchers shouldn't be removed
+     */
+    @Test(timeout = 90000)
+    public void testRemoveAllDataWatchers() throws IOException,
+            InterruptedException, KeeperException {
+        zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+        MyWatcher w1 = new MyWatcher("/node1", 1);
+        MyWatcher w2 = new MyWatcher("/node1", 1);
+        LOG.info("Adding data watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        Assert.assertNotNull("Didn't set data watches",
+                zk2.exists("/node1", w1));
+        LOG.info("Adding data watcher {} on path {}", new Object[] { w2,
+                "/node1" });
+        Assert.assertNotNull("Didn't set data watches",
+                zk2.exists("/node1", w2));
+        LOG.info("Adding child watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        zk2.getChildren("/node1", w1);
+        LOG.info("Adding child watcher {} on path {}", new Object[] { w2,
+                "/node1" });
+        zk2.getChildren("/node1", w2);
+        removeWatches(zk2, "/node1", null, WatcherType.Data, false, Code.OK);
+        zk1.create("/node1/child", null, Ids.OPEN_ACL_UNSAFE,
+                CreateMode.EPHEMERAL);
+        Assert.assertTrue("Didn't remove data watcher", w1.matches());
+        Assert.assertTrue("Didn't remove data watcher", w2.matches());
+        // waiting for child watchers to be notified
+        int count = 10;
+        while (count > 0) {
+            if (w1.getEventsAfterWatchRemoval().size() > 0
+                    && w2.getEventsAfterWatchRemoval().size() > 0) {
+                break;
+            }
+            count--;
+            Thread.sleep(1000);
+        }
+        // watcher1
+        List<EventType> events = w1.getEventsAfterWatchRemoval();
+        Assert.assertEquals("Didn't get NodeChildrenChanged event", 1,
+                events.size());
+        Assert.assertTrue("Didn't get NodeChildrenChanged event",
+                events.contains(EventType.NodeChildrenChanged));
+        // watcher2
+        events = w2.getEventsAfterWatchRemoval();
+        Assert.assertEquals("Didn't get NodeChildrenChanged event", 1,
+                events.size());
+        Assert.assertTrue("Didn't get NodeChildrenChanged event",
+                events.contains(EventType.NodeChildrenChanged));
+    }
+
+    /**
+     * Test verifies null watcher with WatcherType.Children - remove all child
+     * watchers. Data watchers shouldn't be removed
+     */
+    @Test(timeout = 90000)
+    public void testRemoveAllChildWatchers() throws IOException,
+            InterruptedException, KeeperException {
+        zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+        MyWatcher w1 = new MyWatcher("/node1", 1);
+        MyWatcher w2 = new MyWatcher("/node1", 1);
+        LOG.info("Adding data watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        Assert.assertNotNull("Didn't set data watches",
+                zk2.exists("/node1", w1));
+        LOG.info("Adding data watcher {} on path {}", new Object[] { w2,
+                "/node1" });
+        Assert.assertNotNull("Didn't set data watches",
+                zk2.exists("/node1", w2));
+        LOG.info("Adding child watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        zk2.getChildren("/node1", w1);
+        LOG.info("Adding child watcher {} on path {}", new Object[] { w2,
+                "/node1" });
+        zk2.getChildren("/node1", w2);
+        removeWatches(zk2, "/node1", null, WatcherType.Children, false, Code.OK);
+        zk1.setData("/node1", "test".getBytes(), -1);
+        Assert.assertTrue("Didn't remove child watcher", w1.matches());
+        Assert.assertTrue("Didn't remove child watcher", w2.matches());
+        // waiting for child watchers to be notified
+        int count = 10;
+        while (count > 0) {
+            if (w1.getEventsAfterWatchRemoval().size() > 0
+                    && w2.getEventsAfterWatchRemoval().size() > 0) {
+                break;
+            }
+            count--;
+            Thread.sleep(1000);
+        }
+        // watcher1
+        List<EventType> events = w1.getEventsAfterWatchRemoval();
+        Assert.assertEquals("Didn't get NodeDataChanged event", 1,
+                events.size());
+        Assert.assertTrue("Didn't get NodeDataChanged event",
+                events.contains(EventType.NodeDataChanged));
+        // watcher2
+        events = w2.getEventsAfterWatchRemoval();
+        Assert.assertEquals("Didn't get NodeDataChanged event", 1,
+                events.size());
+        Assert.assertTrue("Didn't get NodeDataChanged event",
+                events.contains(EventType.NodeDataChanged));
+    }
+
+    /**
+     * Test verifies given watcher doesn't exists!
+     */
+    @Test(timeout = 90000)
+    public void testNoWatcherException() throws IOException,
+            InterruptedException, KeeperException {
+        zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+        MyWatcher w1 = new MyWatcher("/node1", 2);
+        MyWatcher w2 = new MyWatcher("/node1", 2);
+        LOG.info("Adding data watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        Assert.assertNotNull("Didn't set data watches",
+                zk2.exists("/node1", w1));
+        LOG.info("Adding data watcher {} on path {}", new Object[] { w2,
+                "/node1" });
+        Assert.assertNull("Didn't set data watches", zk2.exists("/node2", w2));
+        LOG.info("Adding child watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        zk2.getChildren("/node1", w1);
+        LOG.info("Adding child watcher {} on path {}", new Object[] { w2,
+                "/node1" });
+        zk2.getChildren("/node1", w2);
+
+        // New Watcher which will be used for removal
+        MyWatcher w3 = new MyWatcher("/node1", 2);
+
+        try {
+            removeWatches(zk2, "/node1", w3, WatcherType.Any, false,
+                    Code.NOWATCHER);
+            Assert.fail("Should throw exception as given watcher doesn't exists");
+        } catch (KeeperException.NoWatcherException nwe) {
+            // expected
+        }
+        try {
+            removeWatches(zk2, "/node1", w3, WatcherType.Children, false,
+                    Code.NOWATCHER);
+            Assert.fail("Should throw exception as given watcher doesn't exists");
+        } catch (KeeperException.NoWatcherException nwe) {
+            // expected
+        }
+        try {
+            removeWatches(zk2, "/node1", w3, WatcherType.Data, false,
+                    Code.NOWATCHER);
+            Assert.fail("Should throw exception as given watcher doesn't exists");
+        } catch (KeeperException.NoWatcherException nwe) {
+            // expected
+        }
+        try {
+            removeWatches(zk2, "/nonexists", w3, WatcherType.Data, false,
+                    Code.NOWATCHER);
+            Assert.fail("Should throw exception as given watcher doesn't exists");
+        } catch (KeeperException.NoWatcherException nwe) {
+            // expected
+        }
+        try {
+            removeWatches(zk2, "/nonexists", null, WatcherType.Data, false,
+                    Code.NOWATCHER);
+            Assert.fail("Should throw exception as given watcher doesn't exists");
+        } catch (KeeperException.NoWatcherException nwe) {
+            // expected
+        }
+    }
+
+    /**
+     * Test verifies WatcherType.Any - removes only the configured data watcher
+     * function
+     */
+    @Test(timeout = 90000)
+    public void testRemoveAnyDataWatcher() throws Exception {
+        zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+        MyWatcher w1 = new MyWatcher("/node1", 1);
+        MyWatcher w2 = new MyWatcher("/node1", 2);
+        // Add multiple data watches
+        LOG.info("Adding data watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        Assert.assertNotNull("Didn't set data watches",
+                zk2.exists("/node1", w1));
+        LOG.info("Adding data watcher {} on path {}", new Object[] { w2,
+                "/node1" });
+        Assert.assertNotNull("Didn't set data watches",
+                zk2.exists("/node1", w2));
+        // Add child watch
+        LOG.info("Adding child watcher {} on path {}", new Object[] { w2,
+                "/node1" });
+        zk2.getChildren("/node1", w2);
+        removeWatches(zk2, "/node1", w1, WatcherType.Any, false, Code.OK);
+        Assert.assertTrue("Didn't remove data watcher", w1.matches());
+        Assert.assertFalse("Shouldn't remove child watcher", w2.matches());
+    }
+
+    /**
+     * Test verifies WatcherType.Any - removes only the configured child watcher
+     * function
+     */
+    @Test(timeout = 90000)
+    public void testRemoveAnyChildWatcher() throws Exception {
+        zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+        MyWatcher w1 = new MyWatcher("/node1", 2);
+        MyWatcher w2 = new MyWatcher("/node1", 1);
+        LOG.info("Adding data watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        Assert.assertNotNull("Didn't set data watches",
+                zk2.exists("/node1", w1));
+        // Add multiple child watches
+        LOG.info("Adding child watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        zk2.getChildren("/node1", w2);
+        LOG.info("Adding child watcher {} on path {}", new Object[] { w2,
+                "/node1" });
+        zk2.getChildren("/node1", w1);
+        removeWatches(zk2, "/node1", w2, WatcherType.Any, false, Code.OK);
+        Assert.assertTrue("Didn't remove child watcher", w2.matches());
+        Assert.assertFalse("Shouldn't remove data watcher", w1.matches());
+    }
+
+    /**
+     * Test verifies when there is no server connection. Remove watches when
+     * local=true, otw should retain it
+     */
+    @Test(timeout = 90000)
+    public void testRemoveWatcherWhenNoConnection() throws Exception {
+        zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+        MyWatcher w1 = new MyWatcher("/node1", 2);
+        MyWatcher w2 = new MyWatcher("/node1", 1);
+        LOG.info("Adding data watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        Assert.assertNotNull("Didn't set data watches",
+                zk2.exists("/node1", w1));
+        // Add multiple child watches
+        LOG.info("Adding child watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        zk2.getChildren("/node1", w1);
+        LOG.info("Adding child watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        zk2.getChildren("/node1", w2);
+        stopServer();
+        removeWatches(zk2, "/node1", w2, WatcherType.Any, true, Code.OK);
+        Assert.assertTrue("Didn't remove child watcher", w2.matches());
+        Assert.assertFalse("Shouldn't remove data watcher", w1.matches());
+        try {
+            removeWatches(zk2, "/node1", w1, WatcherType.Any, false,
+                    Code.CONNECTIONLOSS);
+            Assert.fail("Should throw exception as last watch removal requires server connection");
+        } catch (KeeperException.ConnectionLossException nwe) {
+            // expected
+        }
+        Assert.assertFalse("Shouldn't remove data watcher", w1.matches());
+
+        // when local=true, here if connection not available, simply removes
+        // from local session
+        removeWatches(zk2, "/node1", w1, WatcherType.Any, true, Code.OK);
+        Assert.assertTrue("Didn't remove data watcher", w1.matches());
+    }
+
+    /**
+     * Test verifies many pre-node watchers. Also, verifies internal
+     * datastructure 'watchManager.existWatches'
+     */
+    @Test(timeout = 90000)
+    public void testManyPreNodeWatchers() throws Exception {
+        int count = 50;
+        List<MyWatcher> wList = new ArrayList<MyWatcher>(count);
+        MyWatcher w;
+        String path = "/node";
+        // Exists watcher
+        for (int i = 0; i < count; i++) {
+            final String nodePath = path + i;
+            w = new MyWatcher(nodePath, 1);
+            wList.add(w);
+            LOG.info("Adding pre node watcher {} on path {}", new Object[] { w,
+                    nodePath });
+            zk1.exists(nodePath, w);
+        }
+        Assert.assertEquals("Failed to add watchers!", count, zk1
+                .getExistWatches().size());
+        for (int i = 0; i < count; i++) {
+            final MyWatcher watcher = wList.get(i);
+            removeWatches(zk1, path + i, watcher, WatcherType.Data, false,
+                    Code.OK);
+            Assert.assertTrue("Didn't remove data watcher", watcher.matches());
+        }
+        Assert.assertEquals("Didn't remove watch references!", 0, zk1
+                .getExistWatches().size());
+    }
+
+    /**
+     * Test verifies many child watchers. Also, verifies internal datastructure
+     * 'watchManager.childWatches'
+     */
+    @Test(timeout = 90000)
+    public void testManyChildWatchers() throws Exception {
+        int count = 50;
+        List<MyWatcher> wList = new ArrayList<MyWatcher>(count);
+        MyWatcher w;
+        String path = "/node";
+
+        // Child watcher
+        for (int i = 0; i < count; i++) {
+            String nodePath = path + i;
+            zk1.create(nodePath, null, Ids.OPEN_ACL_UNSAFE,
+                    CreateMode.PERSISTENT);
+            nodePath += "/";
+        }
+        for (int i = 0; i < count; i++) {
+            String nodePath = path + i;
+            w = new MyWatcher(path + i, 1);
+            wList.add(w);
+            LOG.info("Adding child watcher {} on path {}", new Object[] { w,
+                    nodePath });
+            zk1.getChildren(nodePath, w);
+            nodePath += "/";
+        }
+        Assert.assertEquals("Failed to add watchers!", count, zk1
+                .getChildWatches().size());
+        for (int i = 0; i < count; i++) {
+            final MyWatcher watcher = wList.get(i);
+            removeWatches(zk1, path + i, watcher, WatcherType.Children, false,
+                    Code.OK);
+            Assert.assertTrue("Didn't remove child watcher", watcher.matches());
+        }
+        Assert.assertEquals("Didn't remove watch references!", 0, zk1
+                .getChildWatches().size());
+    }
+
+    /**
+     * Test verifies many data watchers. Also, verifies internal datastructure
+     * 'watchManager.dataWatches'
+     */
+    @Test(timeout = 90000)
+    public void testManyDataWatchers() throws Exception {
+        int count = 50;
+        List<MyWatcher> wList = new ArrayList<MyWatcher>(count);
+        MyWatcher w;
+        String path = "/node";
+
+        // Data watcher
+        for (int i = 0; i < count; i++) {
+            String nodePath = path + i;
+            w = new MyWatcher(path + i, 1);
+            wList.add(w);
+            zk1.create(nodePath, null, Ids.OPEN_ACL_UNSAFE,
+                    CreateMode.PERSISTENT);
+            LOG.info("Adding data watcher {} on path {}", new Object[] { w,
+                    nodePath });
+            zk1.getData(nodePath, w, null);
+            nodePath += "/";
+        }
+        Assert.assertEquals("Failed to add watchers!", count, zk1
+                .getDataWatches().size());
+        for (int i = 0; i < count; i++) {
+            final MyWatcher watcher = wList.get(i);
+            removeWatches(zk1, path + i, watcher, WatcherType.Data, false,
+                    Code.OK);
+            Assert.assertTrue("Didn't remove data watcher", watcher.matches());
+        }
+        Assert.assertEquals("Didn't remove watch references!", 0, zk1
+                .getDataWatches().size());
+    }
+
+    /**
+     * Test verifies removal of many watchers locally when no connection and
+     * WatcherType#Any. Also, verifies internal watchManager datastructures
+     */
+    @Test(timeout = 90000)
+    public void testManyWatchersWhenNoConnection() throws Exception {
+        int count = 3;
+        List<MyWatcher> wList = new ArrayList<MyWatcher>(count);
+        MyWatcher w;
+        String path = "/node";
+
+        // Child watcher
+        for (int i = 0; i < count; i++) {
+            String nodePath = path + i;
+            zk1.create(nodePath, null, Ids.OPEN_ACL_UNSAFE,
+                    CreateMode.PERSISTENT);
+            nodePath += "/";
+        }
+        for (int i = 0; i < count; i++) {
+            String nodePath = path + i;
+            w = new MyWatcher(path + i, 2);
+            wList.add(w);
+            LOG.info("Adding child watcher {} on path {}", new Object[] { w,
+                    nodePath });
+            zk1.getChildren(nodePath, w);
+            nodePath += "/";
+        }
+        Assert.assertEquals("Failed to add watchers!", count, zk1
+                .getChildWatches().size());
+
+        // Data watcher
+        for (int i = 0; i < count; i++) {
+            String nodePath = path + i;
+            w = wList.get(i);
+            LOG.info("Adding data watcher {} on path {}", new Object[] { w,
+                    nodePath });
+            zk1.getData(nodePath, w, null);
+            nodePath += "/";
+        }
+        Assert.assertEquals("Failed to add watchers!", count, zk1
+                .getDataWatches().size());
+        stopServer();
+        for (int i = 0; i < count; i++) {
+            final MyWatcher watcher = wList.get(i);
+            removeWatches(zk1, path + i, watcher, WatcherType.Any, true,
+                    Code.OK);
+            Assert.assertTrue("Didn't remove watcher", watcher.matches());
+        }
+        Assert.assertEquals("Didn't remove watch references!", 0, zk1
+                .getChildWatches().size());
+        Assert.assertEquals("Didn't remove watch references!", 0, zk1
+                .getDataWatches().size());
+    }
+
+    /**
+     * Test verifies removing watcher having namespace
+     */
+    @Test(timeout = 90000)
+    public void testChRootRemoveWatcher() throws Exception {
+        // creating the subtree for chRoot clients.
+        String chRoot = "/appsX";
+        zk1.create("/appsX", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+        if (zk1 != null) {
+            zk1.close();
+        }
+        if (zk2 != null) {
+            zk2.close();
+        }
+        // Creating chRoot client.
+        zk1 = createClient(this.hostPort + chRoot);
+        zk2 = createClient(this.hostPort + chRoot);
+
+        LOG.info("Creating child znode /node1 using chRoot client");
+        zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+        MyWatcher w1 = new MyWatcher("/node1", 2);
+        MyWatcher w2 = new MyWatcher("/node1", 1);
+        LOG.info("Adding data watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        Assert.assertNotNull("Didn't set data watches",
+                zk2.exists("/node1", w1));
+        // Add multiple child watches
+        LOG.info("Adding child watcher {} on path {}", new Object[] { w1,
+                "/node1" });
+        zk2.getChildren("/node1", w2);
+        LOG.info("Adding child watcher {} on path {}", new Object[] { w2,
+                "/node1" });
+        zk2.getChildren("/node1", w1);
+        removeWatches(zk2, "/node1", w1, WatcherType.Any, false, Code.OK);
+        Assert.assertTrue("Didn't remove child watcher", w1.matches());
+        Assert.assertFalse("Shouldn't remove data watcher", w2.matches());
+    }
+
+    private class MyWatcher implements Watcher {
+        private final String path;
+        private String eventPath;
+        private CountDownLatch latch;
+        private List<EventType> eventsAfterWatchRemoval = new ArrayList<EventType>();
+        public MyWatcher(String path, int count) {
+            this.path = path;
+            latch = new CountDownLatch(count);
+        }
+
+        public void process(WatchedEvent event) {
+            LOG.debug("Event path : {}, eventPath : {}"
+                    + new Object[] { path, event.getPath() });
+            this.eventPath = event.getPath();
+            // notifies watcher removal
+            if (latch.getCount() == 0) {
+                if (event.getType() != EventType.None) {
+                    eventsAfterWatchRemoval.add(event.getType());
+                }
+            }
+            if (event.getType() == EventType.ChildWatchRemoved
+                    || event.getType() == EventType.DataWatchRemoved) {
+                latch.countDown();
+            }
+        }
+
+        public boolean matches() throws InterruptedException {
+            if (!latch.await(CONNECTION_TIMEOUT/5, TimeUnit.MILLISECONDS)) {
+                LOG.error("Failed waiting to remove the watches");
+                return false;
+            }
+            LOG.debug("Client path : {} eventPath : {}", new Object[] { path,
+                    eventPath });
+            return path.equals(eventPath);
+        }
+
+        public List<EventType> getEventsAfterWatchRemoval() {
+            return eventsAfterWatchRemoval;
+        }
+    }
+
+    private class MyCallback implements AsyncCallback.VoidCallback {
+        private final String path;
+        private final int rc;
+        private String eventPath;
+        int eventRc;
+        private CountDownLatch latch = new CountDownLatch(1);
+
+        public MyCallback(int rc, String path) {
+            this.rc = rc;
+            this.path = path;
+        }
+
+        @Override
+        public void processResult(int rc, String eventPath, Object ctx) {
+            System.out.println("latch:" + path + " " + eventPath);
+            this.eventPath = eventPath;
+            this.eventRc = rc;
+            this.latch.countDown();
+        }
+
+        public boolean matches() throws InterruptedException {
+            if (!latch.await(CONNECTION_TIMEOUT/5, TimeUnit.MILLISECONDS)) {
+                return false;
+            }
+            return path.equals(eventPath) && rc == eventRc;
+        }
+    }
+}

Modified: zookeeper/trunk/src/zookeeper.jute
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/zookeeper.jute?rev=1560904&r1=1560903&r2=1560904&view=diff
==============================================================================
--- zookeeper/trunk/src/zookeeper.jute (original)
+++ zookeeper/trunk/src/zookeeper.jute Fri Jan 24 06:18:32 2014
@@ -213,6 +213,10 @@ module org.apache.zookeeper.proto {
         vector<org.apache.zookeeper.data.ACL> acl;
         org.apache.zookeeper.data.Stat stat;
     }
+    class RemoveWatchesRequest {
+        ustring path;
+        int type;
+    }
 }
 
 module org.apache.zookeeper.server.quorum {