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 2021/02/22 22:49:44 UTC

[GitHub] [helix] xyuanlu commented on a change in pull request #1645: Use thread pool for batched call back handling events

xyuanlu commented on a change in pull request #1645:
URL: https://github.com/apache/helix/pull/1645#discussion_r580648551



##########
File path: helix-core/src/main/java/org/apache/helix/manager/zk/CallbackEventTPFactory.java
##########
@@ -0,0 +1,76 @@
+package org.apache.helix.manager.zk;
+
+
+
+/*
+ * 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.
+ */
+
+// returns a thread pool object given a HelixManager identifier
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+
+
+public class CallbackEventTPFactory {
+  static private final ReadWriteLock _lock = new ReentrantReadWriteLock();
+  static private Map<Integer, ThreadPoolExecutor> _managerToCallBackThreadPoolMap = new HashMap();
+
+  public static ThreadPoolExecutor getThreadPool(int mapHash) {
+    // should not use general lock for read
+    _lock.readLock().lock();
+    ThreadPoolExecutor result;
+    if (_managerToCallBackThreadPoolMap.containsKey(mapHash)) {
+      result = _managerToCallBackThreadPoolMap.get(mapHash);
+      _lock.readLock().unlock();
+    } else {
+      _lock.readLock().unlock();
+      result = getAndCreateThreadPool(mapHash);
+    }
+    return result;
+  }
+
+  public static ThreadPoolExecutor getAndCreateThreadPool(int mapHash) {
+    ThreadPoolExecutor result;
+    _lock.writeLock().lock();
+    // first check if the key is already in the map
+    if (_managerToCallBackThreadPoolMap.containsKey(mapHash)) {
+      // downgrade to read lock
+      _lock.readLock().lock();
+      result = _managerToCallBackThreadPoolMap.get(mapHash);
+      _lock.readLock().unlock();
+    } else {
+      ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
+          .setNameFormat(String.format("CallbackHandlerExecutorService - %s ", mapHash)).build();
+      result = new ThreadPoolExecutor(5, 5, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<>(),

Review comment:
       TFTR. Updated.

##########
File path: helix-core/src/main/java/org/apache/helix/manager/zk/CallbackEventTPFactory.java
##########
@@ -0,0 +1,76 @@
+package org.apache.helix.manager.zk;
+
+
+
+/*
+ * 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.
+ */
+
+// returns a thread pool object given a HelixManager identifier
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+
+
+public class CallbackEventTPFactory {

Review comment:
       TFTR. The threadpool is one-one mapping to HelixManager, not executor. I think having a factory will provide a layer of abstraction for thread pool creation, so each CallbackEventExecutor don't need to know if the thread pool for the helixManager is already created. CallbackEventExecutor could just call `getOrCreateThreadPool ` in its constructor. 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

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



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