You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by ma...@apache.org on 2015/12/11 03:44:25 UTC

[47/52] [abbrv] phoenix git commit: PHOENIX-2496 Attempt to gracefully close in ShutdownHook, but still exit promptly.

PHOENIX-2496 Attempt to gracefully close in ShutdownHook, but still exit promptly.

The JVM will not exit until all ShutdownHooks have completed.
Blocking until a query thread completes might be on the order
of minutes which is undesirable. Try to clean up, but exit quickly.

Introduces configuration property 'phoenix.shutdown.timeoutMs'
with a default value of 5000 (5 seconds).


Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/b74215f8
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/b74215f8
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/b74215f8

Branch: refs/heads/calcite
Commit: b74215f822e38dfb7eb2d40bed1a6c82be8ae2f2
Parents: 578979a
Author: Josh Elser <el...@apache.org>
Authored: Tue Dec 8 10:59:38 2015 -0500
Committer: Thomas D'Silva <td...@salesforce.com>
Committed: Tue Dec 8 14:10:41 2015 -0800

----------------------------------------------------------------------
 .../org/apache/phoenix/jdbc/PhoenixDriver.java  | 43 +++++++++++++++++++-
 .../org/apache/phoenix/query/QueryServices.java |  1 +
 .../phoenix/query/QueryServicesOptions.java     |  1 +
 3 files changed, 44 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/b74215f8/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDriver.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDriver.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDriver.java
index bb0b1e2..0f5d4aa 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDriver.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDriver.java
@@ -23,16 +23,26 @@ import java.sql.SQLException;
 import java.util.Properties;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import javax.annotation.concurrent.GuardedBy;
 
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseConfiguration;
 import org.apache.phoenix.query.ConnectionQueryServices;
 import org.apache.phoenix.query.ConnectionQueryServicesImpl;
 import org.apache.phoenix.query.ConnectionlessQueryServicesImpl;
+import org.apache.phoenix.query.HBaseFactoryProvider;
 import org.apache.phoenix.query.QueryServices;
 import org.apache.phoenix.query.QueryServicesImpl;
+import org.apache.phoenix.query.QueryServicesOptions;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -64,7 +74,38 @@ public final class PhoenixDriver extends PhoenixEmbeddedDriver {
                 Runtime.getRuntime().addShutdownHook(new Thread() {
                     @Override
                     public void run() {
-                        closeInstance(INSTANCE);
+                        final Configuration config =
+                            HBaseFactoryProvider.getConfigurationFactory().getConfiguration();
+                        final ExecutorService svc = Executors.newSingleThreadExecutor();
+                        Future<?> future = svc.submit(new Runnable() {
+                            @Override
+                            public void run() {
+                                closeInstance(INSTANCE);
+                            }
+                        });
+
+                        // Pull the timeout value (default 5s).
+                        long millisBeforeShutdown = config.getLong(
+                                QueryServices.DRIVER_SHUTDOWN_TIMEOUT_MS,
+                                QueryServicesOptions.DEFAULT_DRIVER_SHUTDOWN_TIMEOUT_MS);
+
+                        // Close with a timeout. If this is running, we know the JVM wants to
+                        // go down. There may be other threads running that are holding the lock.
+                        // We don't want to be blocked on them (for the normal HBase retry policy).
+                        //
+                        // We don't care about any exceptions, we're going down anyways.
+                        try {
+                            future.get(millisBeforeShutdown, TimeUnit.MILLISECONDS);
+                        } catch (ExecutionException e) {
+                            logger.warn("Failed to close instance", e);
+                        } catch (InterruptedException e) {
+                            logger.warn("Interrupted waiting to close instance", e);
+                        } catch (TimeoutException e) {
+                            logger.warn("Timed out waiting to close instance", e);
+                        }
+
+                        // We're going down, but try to clean up.
+                        svc.shutdown();
                     }
                 });
 

http://git-wip-us.apache.org/repos/asf/phoenix/blob/b74215f8/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServices.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServices.java b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServices.java
index ca78be0..1b05334 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServices.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServices.java
@@ -52,6 +52,7 @@ public interface QueryServices extends SQLCloseable {
     public static final String CONSISTENCY_ATTRIB = "phoenix.connection.consistency";
     // joni byte regex engine setting
     public static final String USE_BYTE_BASED_REGEX_ATTRIB = "phoenix.regex.byteBased";
+    public static final String DRIVER_SHUTDOWN_TIMEOUT_MS = "phoenix.shutdown.timeoutMs";
 
     /**
 	 * max size to spool the the result into

http://git-wip-us.apache.org/repos/asf/phoenix/blob/b74215f8/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServicesOptions.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServicesOptions.java b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServicesOptions.java
index d42c1f7..13a7beb 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServicesOptions.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServicesOptions.java
@@ -109,6 +109,7 @@ public class QueryServicesOptions {
     public static final boolean DEFAULT_USE_INDEXES = true; // Use indexes
     public static final boolean DEFAULT_IMMUTABLE_ROWS = false; // Tables rows may be updated
     public static final boolean DEFAULT_DROP_METADATA = true; // Drop meta data also.
+    public static final long DEFAULT_DRIVER_SHUTDOWN_TIMEOUT_MS = 5  * 1000; // Time to wait in ShutdownHook to exit gracefully.
 
     public final static int DEFAULT_MUTATE_BATCH_SIZE = 1000; // Batch size for UPSERT SELECT and DELETE
 	// The only downside of it being out-of-sync is that the parallelization of the scan won't be as balanced as it could be.