Repository: hive
Updated Branches:
  refs/heads/master 56ce47aeb -> 1103486af


HIVE-10499 Ensure Session/ZooKeeperClient instances are closed (Jimmy, reviewed 
by Szehon)


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

Branch: refs/heads/master
Commit: 1103486af8ec17eb67e24b8e9dc279205ffc9ee3
Parents: 56ce47a
Author: Jimmy Xiang <jxi...@cloudera.com>
Authored: Wed Apr 22 16:06:00 2015 -0700
Committer: Jimmy Xiang <jxi...@cloudera.com>
Committed: Wed Apr 29 08:46:51 2015 -0700

----------------------------------------------------------------------
 .../hive/jdbc/ZooKeeperHiveClientHelper.java    |  2 +-
 .../service/cli/session/HiveSessionImpl.java    | 14 +++++-
 .../service/cli/session/SessionManager.java     | 49 +++++++++++++-------
 .../apache/hive/service/server/HiveServer2.java | 18 +++----
 4 files changed, 55 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/1103486a/jdbc/src/java/org/apache/hive/jdbc/ZooKeeperHiveClientHelper.java
----------------------------------------------------------------------
diff --git a/jdbc/src/java/org/apache/hive/jdbc/ZooKeeperHiveClientHelper.java 
b/jdbc/src/java/org/apache/hive/jdbc/ZooKeeperHiveClientHelper.java
index 496c820..e24b3dc 100644
--- a/jdbc/src/java/org/apache/hive/jdbc/ZooKeeperHiveClientHelper.java
+++ b/jdbc/src/java/org/apache/hive/jdbc/ZooKeeperHiveClientHelper.java
@@ -64,8 +64,8 @@ public class ZooKeeperHiveClientHelper {
     CuratorFramework zooKeeperClient =
         CuratorFrameworkFactory.builder().connectString(zooKeeperEnsemble)
             .retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();
-    zooKeeperClient.start();
     try {
+      zooKeeperClient.start();
       serverHosts = zooKeeperClient.getChildren().forPath("/" + 
zooKeeperNamespace);
       // Remove the znodes we've already tried from this list
       serverHosts.removeAll(connParams.getRejectedHostZnodePaths());

http://git-wip-us.apache.org/repos/asf/hive/blob/1103486a/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java
----------------------------------------------------------------------
diff --git 
a/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java 
b/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java
index f14b974..cc3e807 100644
--- a/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java
+++ b/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java
@@ -563,10 +563,22 @@ public class HiveSessionImpl implements HiveSession {
       if (null != hiveHist) {
         hiveHist.closeStream();
       }
-      sessionState.close();
+      try {
+        sessionState.close();
+      } finally {
+        sessionState = null;
+      }
     } catch (IOException ioe) {
       throw new HiveSQLException("Failure to close", ioe);
     } finally {
+      if (sessionState != null) {
+        try {
+          sessionState.close();
+        } catch (Throwable t) {
+          LOG.warn("Error closing session", t);
+        }
+        sessionState = null;
+      }
       release(true);
     }
   }

http://git-wip-us.apache.org/repos/asf/hive/blob/1103486a/service/src/java/org/apache/hive/service/cli/session/SessionManager.java
----------------------------------------------------------------------
diff --git 
a/service/src/java/org/apache/hive/service/cli/session/SessionManager.java 
b/service/src/java/org/apache/hive/service/cli/session/SessionManager.java
index e02997a..36a30b1 100644
--- a/service/src/java/org/apache/hive/service/cli/session/SessionManager.java
+++ b/service/src/java/org/apache/hive/service/cli/session/SessionManager.java
@@ -257,6 +257,12 @@ public class SessionManager extends CompositeService {
     try {
       session.open(sessionConf);
     } catch (Exception e) {
+      try {
+        session.close();
+      } catch (Throwable t) {
+        LOG.warn("Error closing session", t);
+      }
+      session = null;
       throw new HiveSQLException("Failed to open new session: " + e, e);
     }
     if (isOperationLogEnabled) {
@@ -265,6 +271,12 @@ public class SessionManager extends CompositeService {
     try {
       executeSessionHooks(session);
     } catch (Exception e) {
+      try {
+        session.close();
+      } catch (Throwable t) {
+        LOG.warn("Error closing session", t);
+      }
+      session = null;
       throw new HiveSQLException("Failed to execute session hooks", e);
     }
     handleToSession.put(session.getSessionHandle(), session);
@@ -276,23 +288,26 @@ public class SessionManager extends CompositeService {
     if (session == null) {
       throw new HiveSQLException("Session does not exist!");
     }
-    session.close();
-    // Shutdown HiveServer2 if it has been deregistered from ZooKeeper and has 
no active sessions
-    if (!(hiveServer2 == null) && 
(hiveConf.getBoolVar(ConfVars.HIVE_SERVER2_SUPPORT_DYNAMIC_SERVICE_DISCOVERY))
-        && (!hiveServer2.isRegisteredWithZooKeeper())) {
-      // Asynchronously shutdown this instance of HiveServer2,
-      // if there are no active client sessions
-      if (getOpenSessionCount() == 0) {
-        LOG.info("This instance of HiveServer2 has been removed from the list 
of server "
-            + "instances available for dynamic service discovery. "
-            + "The last client session has ended - will shutdown now.");
-        Thread shutdownThread = new Thread() {
-          @Override
-          public void run() {
-            hiveServer2.stop();
-          }
-        };
-        shutdownThread.start();
+    try {
+      session.close();
+    } finally {
+      // Shutdown HiveServer2 if it has been deregistered from ZooKeeper and 
has no active sessions
+      if (!(hiveServer2 == null) && 
(hiveConf.getBoolVar(ConfVars.HIVE_SERVER2_SUPPORT_DYNAMIC_SERVICE_DISCOVERY))
+          && (!hiveServer2.isRegisteredWithZooKeeper())) {
+        // Asynchronously shutdown this instance of HiveServer2,
+        // if there are no active client sessions
+        if (getOpenSessionCount() == 0) {
+          LOG.info("This instance of HiveServer2 has been removed from the 
list of server "
+              + "instances available for dynamic service discovery. "
+              + "The last client session has ended - will shutdown now.");
+          Thread shutdownThread = new Thread() {
+            @Override
+            public void run() {
+              hiveServer2.stop();
+            }
+          };
+          shutdownThread.start();
+        }
       }
     }
   }

http://git-wip-us.apache.org/repos/asf/hive/blob/1103486a/service/src/java/org/apache/hive/service/server/HiveServer2.java
----------------------------------------------------------------------
diff --git a/service/src/java/org/apache/hive/service/server/HiveServer2.java 
b/service/src/java/org/apache/hive/service/server/HiveServer2.java
index 222cb45..dc2217f 100644
--- a/service/src/java/org/apache/hive/service/server/HiveServer2.java
+++ b/service/src/java/org/apache/hive/service/server/HiveServer2.java
@@ -325,21 +325,21 @@ public class HiveServer2 extends CompositeService {
         }
         break;
       } catch (Throwable throwable) {
+        if (server != null) {
+          try {
+            server.stop();
+          } catch (Throwable t) {
+            LOG.info("Exception caught when calling stop of HiveServer2 before 
retrying start", t);
+          } finally {
+            server = null;
+          }
+        }
         if (++attempts >= maxAttempts) {
           throw new Error("Max start attempts " + maxAttempts + " exhausted", 
throwable);
         } else {
           LOG.warn("Error starting HiveServer2 on attempt " + attempts
               + ", will retry in 60 seconds", throwable);
           try {
-            if (server != null) {
-              server.stop();
-              server = null;
-            }
-          } catch (Exception e) {
-            LOG.info(
-                "Exception caught when calling stop of HiveServer2 before" + " 
retrying start", e);
-          }
-          try {
             Thread.sleep(60L * 1000L);
           } catch (InterruptedException e) {
             Thread.currentThread().interrupt();

Reply via email to