You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@helix.apache.org by GitBox <gi...@apache.org> on 2023/01/12 01:16:13 UTC

[GitHub] [helix] rahulrane50 commented on a diff in pull request #2338: Implement a thread-safe listener container for zkclient

rahulrane50 commented on code in PR #2338:
URL: https://github.com/apache/helix/pull/2338#discussion_r1067611766


##########
zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ListenerContainer.java:
##########
@@ -0,0 +1,186 @@
+package org.apache.helix.zookeeper.zkclient;
+
+/*
+ * 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.
+ */
+
+import com.google.common.annotations.VisibleForTesting;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.function.Consumer;
+
+
+/**
+ * A thread-safe container wrapper class for Listeners registered to ZkClient.
+ * It stores 2 types of listener separately, one-time listener and persistent listener. The former ones are removed
+ * right after its consumption, while the latter need to be explicitly removed. 
+ * @param <T> the type of listener
+ */
+public class ListenerContainer<T> {
+  private final ReentrantLock _lock = new ReentrantLock(true);

Review Comment:
   I see that this lock is used only once in below APIs. just commented there and also would like to know if this is really needed?



##########
zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ListenerContainer.java:
##########
@@ -0,0 +1,186 @@
+package org.apache.helix.zookeeper.zkclient;
+
+/*
+ * 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.
+ */
+
+import com.google.common.annotations.VisibleForTesting;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.function.Consumer;
+
+
+/**
+ * A thread-safe container wrapper class for Listeners registered to ZkClient.
+ * It stores 2 types of listener separately, one-time listener and persistent listener. The former ones are removed
+ * right after its consumption, while the latter need to be explicitly removed. 
+ * @param <T> the type of listener
+ */
+public class ListenerContainer<T> {
+  private final ReentrantLock _lock = new ReentrantLock(true);
+  private final Map<String, Set<T>> _persistentListener = new ConcurrentHashMap<>();
+  private final Map<String, Set<T>> _onetimeListener = new ConcurrentHashMap<>();
+
+  /**
+   * Add listener to the container with specified key.
+   * @param key the key to register to
+   * @param listener the listener to register
+   * @param persistListener true if the listener is persistent
+   */
+  public void addListener(String key, T listener, boolean persistListener) {
+    addListener(key, listener, persistListener ? _persistentListener : _onetimeListener);
+  }
+
+  /**
+   * Remove listener from the container.
+   * This operation removes both one-time and persistent listener from the specified key.
+   * @param key the key to remove
+   * @param listener the listener to remove
+   */
+  public void removeListener(String key, T listener) {
+    _lock.lock();
+    try {
+      removeFromListenerMap(key, listener, _persistentListener);
+      removeFromListenerMap(key, listener, _onetimeListener);
+    } finally {
+      _lock.unlock();
+    }
+  }
+
+  /**
+   * Remove listener from the container.
+   * @param key the key to remove
+   * @param listener the listener to remove
+   * @param persistListener true if remove the persistent listener, otherwise remove the one-time listener
+   */
+  public void removeListener(String key, T listener, boolean persistListener) {
+    _lock.lock();
+    try {
+      if (persistListener) {
+        removeFromListenerMap(key, listener, _persistentListener);
+      } else {
+        removeFromListenerMap(key, listener, _onetimeListener);
+      }
+    } finally {
+      _lock.unlock();
+    }
+  }
+
+  /**
+   * Remove listeners from the container.
+   * @param key the key to remove
+   * @param listeners the listeners to remove
+   * @param persistListener true if remove the persistent listener, otherwise remove the one-time listener
+   */
+  public void removeListeners(String key, Collection<T> listeners, boolean persistListener) {
+    _lock.lock();
+    try {
+      if (persistListener) {
+        removeFromListenerMap(key, listeners, _persistentListener);
+      } else {
+        removeFromListenerMap(key, listeners, _onetimeListener);
+      }
+    } finally {
+      _lock.unlock();
+    }
+  }
+
+  /**
+   * Consume the listeners registered to the given key. The given consumer is applied to all one-time and persistent

Review Comment:
   Umm this are great APIs but just few questions : 
   1. Does it make sense or would there be scenarios where both persistent as well as one time listeners will be added for same key?
   2. If above case can be true then it would be interesting to know the behaviour of having multiple listeners for same key. My guess is that should be okay but just calling this out.



##########
zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ListenerContainer.java:
##########
@@ -0,0 +1,186 @@
+package org.apache.helix.zookeeper.zkclient;
+
+/*
+ * 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.
+ */
+
+import com.google.common.annotations.VisibleForTesting;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.function.Consumer;
+
+
+/**
+ * A thread-safe container wrapper class for Listeners registered to ZkClient.
+ * It stores 2 types of listener separately, one-time listener and persistent listener. The former ones are removed
+ * right after its consumption, while the latter need to be explicitly removed. 
+ * @param <T> the type of listener
+ */
+public class ListenerContainer<T> {
+  private final ReentrantLock _lock = new ReentrantLock(true);
+  private final Map<String, Set<T>> _persistentListener = new ConcurrentHashMap<>();
+  private final Map<String, Set<T>> _onetimeListener = new ConcurrentHashMap<>();
+
+  /**
+   * Add listener to the container with specified key.
+   * @param key the key to register to
+   * @param listener the listener to register
+   * @param persistListener true if the listener is persistent
+   */
+  public void addListener(String key, T listener, boolean persistListener) {
+    addListener(key, listener, persistListener ? _persistentListener : _onetimeListener);
+  }
+
+  /**
+   * Remove listener from the container.
+   * This operation removes both one-time and persistent listener from the specified key.
+   * @param key the key to remove
+   * @param listener the listener to remove
+   */
+  public void removeListener(String key, T listener) {
+    _lock.lock();
+    try {
+      removeFromListenerMap(key, listener, _persistentListener);
+      removeFromListenerMap(key, listener, _onetimeListener);
+    } finally {
+      _lock.unlock();
+    }
+  }
+
+  /**
+   * Remove listener from the container.
+   * @param key the key to remove
+   * @param listener the listener to remove
+   * @param persistListener true if remove the persistent listener, otherwise remove the one-time listener
+   */
+  public void removeListener(String key, T listener, boolean persistListener) {
+    _lock.lock();
+    try {
+      if (persistListener) {
+        removeFromListenerMap(key, listener, _persistentListener);
+      } else {
+        removeFromListenerMap(key, listener, _onetimeListener);
+      }
+    } finally {
+      _lock.unlock();
+    }
+  }
+
+  /**
+   * Remove listeners from the container.
+   * @param key the key to remove
+   * @param listeners the listeners to remove
+   * @param persistListener true if remove the persistent listener, otherwise remove the one-time listener
+   */
+  public void removeListeners(String key, Collection<T> listeners, boolean persistListener) {
+    _lock.lock();
+    try {
+      if (persistListener) {
+        removeFromListenerMap(key, listeners, _persistentListener);
+      } else {
+        removeFromListenerMap(key, listeners, _onetimeListener);
+      }
+    } finally {
+      _lock.unlock();
+    }
+  }
+
+  /**
+   * Consume the listeners registered to the given key. The given consumer is applied to all one-time and persistent
+   * listeners with the given key.
+   * The one-time listeners are removed after this operation, as a result, this operation is NOT idempotent.
+   * @param key the key of the listeners
+   * @param consumer the consuming action for the listeners
+   */
+  public void consumeListeners(String key, Consumer<? super T> consumer) {
+    Set<T> onetimeListeners;
+    Set<T> persistentListeners;
+    _lock.lock();
+    try {
+      // remove one-time listeners
+      onetimeListeners = _onetimeListener.remove(key);
+    } finally {
+      _lock.unlock();
+    }
+    if (onetimeListeners == null) {
+      onetimeListeners = Collections.emptySet();
+    }
+    persistentListeners = _persistentListener.getOrDefault(key, Collections.emptySet());

Review Comment:
   just curious, why do we need lock for onetimelisteners and not here? Also we can just use getOrDefault for onetimelisteners as well right?



-- 
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.

To unsubscribe, e-mail: reviews-unsubscribe@helix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@helix.apache.org
For additional commands, e-mail: reviews-help@helix.apache.org