Auditor is sometimes marking as failed a bookie switching from available to read-only mode

…hing from available to read-only mode

Author: Matteo Merli <mmerli@apache.org>

Reviewers: Enrico Olivelli <eolivelli@gmail.com>, Guo Sijie <sijie@apache.org>

Closes #37 from merlimat/bk-919
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookieWatcher.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookieWatcher.java
index 6f8e20d..0142827 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookieWatcher.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookieWatcher.java
@@ -314,7 +314,7 @@
     private static class ReadOnlyBookieWatcher implements Watcher, ChildrenCallback {
 
         private final static Logger LOG = LoggerFactory.getLogger(ReadOnlyBookieWatcher.class);
-        private HashSet<BookieSocketAddress> readOnlyBookies = new HashSet<BookieSocketAddress>();
+        private volatile HashSet<BookieSocketAddress> readOnlyBookies = new HashSet<BookieSocketAddress>();
         private BookKeeper bk;
         private String readOnlyBookieRegPath;
 
@@ -363,7 +363,7 @@
 
         void notifyBookiesChanged(final BookiesListener listener) throws BKException {
             try {
-                bk.getZkHandle().getChildren(this.readOnlyBookieRegPath, new Watcher() {
+                List<String> children = bk.getZkHandle().getChildren(this.readOnlyBookieRegPath, new Watcher() {
                     public void process(WatchedEvent event) {
                         // listen children changed event from ZooKeeper
                         if (event.getType() == EventType.NodeChildrenChanged) {
@@ -371,6 +371,10 @@
                         }
                     }
                 });
+
+                // Update the list of read-only bookies
+                HashSet<BookieSocketAddress> newReadOnlyBookies = convertToBookieAddresses(children);
+                readOnlyBookies = newReadOnlyBookies;
             } catch (KeeperException ke) {
                 logger.error("Error registering watcher with zookeeper", ke);
                 throw new BKException.ZKException();