You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2018/12/26 18:57:33 UTC

[GitHub] kaijianding commented on a change in pull request #6683: fix huge number of watches in zk issue

kaijianding commented on a change in pull request #6683: fix huge number of watches in zk issue
URL: https://github.com/apache/incubator-druid/pull/6683#discussion_r244035225
 
 

 ##########
 File path: server/src/main/java/org/apache/druid/curator/announcement/NodeAnnouncer.java
 ##########
 @@ -0,0 +1,346 @@
+/*
+ * 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.druid.curator.announcement;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Throwables;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.api.transaction.CuratorTransaction;
+import org.apache.curator.framework.api.transaction.CuratorTransactionFinal;
+import org.apache.curator.framework.recipes.cache.ChildData;
+import org.apache.curator.framework.recipes.cache.NodeCache;
+import org.apache.curator.framework.recipes.cache.NodeCacheListener;
+import org.apache.curator.utils.ZKPaths;
+import org.apache.druid.java.util.common.IAE;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.guava.CloseQuietly;
+import org.apache.druid.java.util.common.io.Closer;
+import org.apache.druid.java.util.common.lifecycle.LifecycleStart;
+import org.apache.druid.java.util.common.lifecycle.LifecycleStop;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.data.Stat;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+/**
+ * Announces single node on Zookeeper and only watch this node, which is different with Announcer
+ */
+public class NodeAnnouncer
+{
+  private static final Logger log = new Logger(NodeAnnouncer.class);
+
+  private final CuratorFramework curator;
+
+  private final List<Announceable> toAnnounce = new ArrayList<>();
+  private final List<Announceable> toUpdate = new ArrayList<>();
+  private final ConcurrentMap<String, NodeCache> listeners = new ConcurrentHashMap<>();
+  private final ConcurrentMap<String, byte[]> announcements = new ConcurrentHashMap<>();
+  private final List<String> parentsIBuilt = new CopyOnWriteArrayList<String>();
+
+  private boolean started = false;
+
+  public NodeAnnouncer(CuratorFramework curator)
+  {
+    this.curator = curator;
+  }
+
+  @VisibleForTesting
+  Set<String> getAddedPaths()
+  {
+    return announcements.keySet();
+  }
+
+  @LifecycleStart
+  public void start()
+  {
+    log.info("Starting announcer");
+    synchronized (toAnnounce) {
+      if (started) {
+        return;
+      }
+
+      started = true;
+
+      for (Announceable announceable : toAnnounce) {
+        announce(announceable.path, announceable.bytes, announceable.removeParentsIfCreated);
+      }
+      toAnnounce.clear();
+
+      for (Announceable announceable : toUpdate) {
+        update(announceable.path, announceable.bytes);
+      }
+      toUpdate.clear();
+    }
+  }
+
+  @LifecycleStop
+  public void stop()
+  {
+    log.info("Stopping announcer");
+    synchronized (toAnnounce) {
+      if (!started) {
+        return;
+      }
+
+      started = false;
+
+      Closer closer = Closer.create();
+      for (NodeCache cache : listeners.values()) {
+        closer.register(cache);
+      }
+      CloseQuietly.close(closer);
+
+      for (String announcementPath : announcements.keySet()) {
+        unannounce(announcementPath);
+      }
+
+      if (!parentsIBuilt.isEmpty()) {
+        CuratorTransaction transaction = curator.inTransaction();
+        for (String parent : parentsIBuilt) {
+          try {
+            transaction = transaction.delete().forPath(parent).and();
+          }
+          catch (Exception e) {
+            log.info(e, "Unable to delete parent[%s], boooo.", parent);
+          }
+        }
+        try {
+          ((CuratorTransactionFinal) transaction).commit();
+        }
+        catch (Exception e) {
+          log.info(e, "Unable to commit transaction. Please feed the hamsters");
+        }
+      }
+    }
+  }
+
+  /**
+   * Like announce(path, bytes, true).
+   */
+  public void announce(String path, byte[] bytes)
+  {
+    announce(path, bytes, true);
+  }
+
+  /**
+   * Announces the provided bytes at the given path.  Announcement means that it will create an ephemeral node
+   * and monitor it to make sure that it always exists until it is unannounced or this object is closed.
+   *
+   * @param path                  The path to announce at
+   * @param bytes                 The payload to announce
+   * @param removeParentIfCreated remove parent of "path" if we had created that parent
+   */
+  public void announce(String path, byte[] bytes, boolean removeParentIfCreated)
+  {
+    synchronized (toAnnounce) {
+      if (!started) {
+        toAnnounce.add(new Announceable(path, bytes, removeParentIfCreated));
+        return;
+      }
+    }
+
+    final ZKPaths.PathAndNode pathAndNode = ZKPaths.getPathAndNode(path);
+
+    final String parentPath = pathAndNode.getPath();
+    boolean buildParentPath = false;
+
+    byte[] value = announcements.get(path);
+
+    if (value == null) {
+      try {
+        if (curator.checkExists().forPath(parentPath) == null) {
+          buildParentPath = true;
+        }
+      }
+      catch (Exception e) {
+        log.debug(e, "Problem checking if the parent existed, ignoring.");
+      }
+
+      // Synchronize to make sure that I only create a listener once.
+      synchronized (toAnnounce) {
+        if (!listeners.containsKey(path)) {
+          final NodeCache cache = new NodeCache(curator, path, true);
+          cache.getListenable().addListener(
+              new NodeCacheListener()
+              {
+                @Override
+                public void nodeChanged() throws Exception
+                {
+                  ChildData currentData = cache.getCurrentData();
+                  if (currentData == null) {
+                    final byte[] value = announcements.get(path);
+                    if (value != null) {
+                      log.info("Node[%s] dropped, reinstating.", path);
 
 Review comment:
   like someone dropped it from zookeeper cli

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org