dbwong commented on code in PR #1430:
URL: https://github.com/apache/phoenix/pull/1430#discussion_r910646774


##########
phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixHAExecutorServiceProvider.java:
##########
@@ -0,0 +1,309 @@
+/*
+ * 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.phoenix.jdbc;
+
+import static 
org.apache.phoenix.monitoring.GlobalClientMetrics.GLOBAL_HA_PARALLEL_POOL1_TASK_END_TO_END_TIME;
+import static 
org.apache.phoenix.monitoring.GlobalClientMetrics.GLOBAL_HA_PARALLEL_POOL1_TASK_EXECUTED_COUNTER;
+import static 
org.apache.phoenix.monitoring.GlobalClientMetrics.GLOBAL_HA_PARALLEL_POOL1_TASK_EXECUTION_TIME;
+import static 
org.apache.phoenix.monitoring.GlobalClientMetrics.GLOBAL_HA_PARALLEL_POOL1_TASK_QUEUE_WAIT_TIME;
+import static 
org.apache.phoenix.monitoring.GlobalClientMetrics.GLOBAL_HA_PARALLEL_POOL1_TASK_REJECTED_COUNTER;
+import static 
org.apache.phoenix.monitoring.GlobalClientMetrics.GLOBAL_HA_PARALLEL_POOL2_TASK_END_TO_END_TIME;
+import static 
org.apache.phoenix.monitoring.GlobalClientMetrics.GLOBAL_HA_PARALLEL_POOL2_TASK_EXECUTED_COUNTER;
+import static 
org.apache.phoenix.monitoring.GlobalClientMetrics.GLOBAL_HA_PARALLEL_POOL2_TASK_EXECUTION_TIME;
+import static 
org.apache.phoenix.monitoring.GlobalClientMetrics.GLOBAL_HA_PARALLEL_POOL2_TASK_QUEUE_WAIT_TIME;
+import static 
org.apache.phoenix.monitoring.GlobalClientMetrics.GLOBAL_HA_PARALLEL_POOL2_TASK_REJECTED_COUNTER;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.FutureTask;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.RejectedExecutionHandler;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
+import org.apache.hadoop.hbase.util.Threads;
+import org.apache.phoenix.monitoring.GlobalClientMetrics;
+import 
org.apache.phoenix.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import 
org.apache.phoenix.thirdparty.com.google.common.annotations.VisibleForTesting;
+import org.apache.phoenix.thirdparty.com.google.common.collect.ImmutableList;
+
+/**
+ * Provides a bounded and configurable executor service for {@link 
ParallelPhoenixConnection} and
+ * related infra. Provides a lazily initialized singleton executor service to 
be used for all
+ * ParallelPhoenixConnections Also provides visibility into the capacity of 
ExecutorService
+ * {@link PhoenixHAExecutorServiceProvider#hasCapacity(Properties)}
+ */
+public class PhoenixHAExecutorServiceProvider {
+
+    public static final String HA_MAX_POOL_SIZE = "phoenix.ha.max.pool.size";
+    public static final String DEFAULT_HA_MAX_POOL_SIZE = "30";
+    public static final String HA_MAX_QUEUE_SIZE = "phoenix.ha.max.queue.size";
+    public static final String DEFAULT_HA_MAX_QUEUE_SIZE = "300";
+    public static final String HA_THREADPOOL_QUEUE_BACKOFF_THRESHOLD =
+            "phoenix.ha.threadpool.queue.backoff.threshold";
+    public static final String DEFAULT_HA_THREADPOOL_QUEUE_BACKOFF_THRESHOLD = 
"0.9";
+
+    private static final Logger LOGGER =
+            LoggerFactory.getLogger(PhoenixHAExecutorServiceProvider.class);
+
+    // Can make configurable if needed
+    private static final int KEEP_ALIVE_TIME_SECONDS = 120;
+
+    private static volatile List<ExecutorService> INSTANCE = null;
+
+    private PhoenixHAExecutorServiceProvider() {
+    }
+
+    public static List<ExecutorService> get(Properties properties) {
+        if (INSTANCE == null) {
+            synchronized (PhoenixHAExecutorServiceProvider.class) {
+                if (INSTANCE == null) {
+                    INSTANCE = initThreadPool(properties);
+                }
+            }
+        }
+        return INSTANCE;
+    }
+
+    @VisibleForTesting
+    static synchronized void resetExecutor() {
+        INSTANCE = null;
+    }
+
+    /**
+     * Checks if the underlying executorServices have sufficient available 
capacity based on
+     * {@link #HA_THREADPOOL_QUEUE_BACKOFF_THRESHOLD}. Monitors the capacity 
of the blockingqueues
+     * linked with the executor services.
+     * @param properties
+     * @return true if queue is less than {@link 
#HA_THREADPOOL_QUEUE_BACKOFF_THRESHOLD} full
+     */
+    public static List<Boolean> hasCapacity(Properties properties) {
+        if (INSTANCE == null) {
+            return ImmutableList.of(Boolean.TRUE, Boolean.TRUE);
+        }
+        double backoffThreshold =
+                
Double.parseDouble(properties.getProperty(HA_THREADPOOL_QUEUE_BACKOFF_THRESHOLD,
+                    DEFAULT_HA_THREADPOOL_QUEUE_BACKOFF_THRESHOLD));
+        int i = 0;
+        List<Boolean> executorCapacities = new ArrayList<>();
+        for (ExecutorService executor : INSTANCE) {
+            double queueSize = ((ThreadPoolExecutor) 
executor).getQueue().size();
+            double queueRemainingCapacity =
+                    ((ThreadPoolExecutor) 
executor).getQueue().remainingCapacity();
+            double queueCapacity = queueSize + queueRemainingCapacity;
+            boolean hasCapacity = ((queueSize / queueCapacity) < 
backoffThreshold);
+            if (hasCapacity) {
+                if (LOGGER.isDebugEnabled()) {
+                    LOGGER.info("PhoenixHAExecutorServiceProvider 
ThreadPoolExecutor[" + i

Review Comment:
   Kept the !hasCapacity block.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to