Repository: hive
Updated Branches:
  refs/heads/branch-3 96b5ceb62 -> a92749484


HIVE-20212: Hiveserver2 in http mode emitting metric 
default.General.open_connections incorrectly (Jesus Camacho Rodriguez, reviewed 
by Zoltan Haindrich)


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

Branch: refs/heads/branch-3
Commit: a927494847d5eac5dc1adbbcd39bbb0b8aa30ec0
Parents: 96b5ceb
Author: Jesus Camacho Rodriguez <[email protected]>
Authored: Fri Jul 27 15:30:26 2018 -0700
Committer: Jesus Camacho Rodriguez <[email protected]>
Committed: Fri Jul 27 15:32:21 2018 -0700

----------------------------------------------------------------------
 .../cli/thrift/ThriftHttpCLIService.java        | 71 +++++++++++---------
 1 file changed, 41 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/a9274948/service/src/java/org/apache/hive/service/cli/thrift/ThriftHttpCLIService.java
----------------------------------------------------------------------
diff --git 
a/service/src/java/org/apache/hive/service/cli/thrift/ThriftHttpCLIService.java 
b/service/src/java/org/apache/hive/service/cli/thrift/ThriftHttpCLIService.java
index d4ea7ab..95d78f8 100644
--- 
a/service/src/java/org/apache/hive/service/cli/thrift/ThriftHttpCLIService.java
+++ 
b/service/src/java/org/apache/hive/service/cli/thrift/ThriftHttpCLIService.java
@@ -23,8 +23,6 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.SynchronousQueue;
 import java.util.concurrent.TimeUnit;
 
-import javax.servlet.ServletContextEvent;
-import javax.servlet.ServletContextListener;
 import javax.ws.rs.HttpMethod;
 
 import org.apache.hadoop.hive.common.metrics.common.Metrics;
@@ -43,6 +41,9 @@ import org.apache.thrift.TProcessor;
 import org.apache.thrift.protocol.TBinaryProtocol;
 import org.apache.thrift.protocol.TProtocolFactory;
 import org.apache.thrift.server.TServlet;
+import org.eclipse.jetty.io.Connection;
+import org.eclipse.jetty.io.EndPoint;
+import org.eclipse.jetty.server.Connector;
 import org.eclipse.jetty.server.HttpConfiguration;
 import org.eclipse.jetty.server.HttpConnectionFactory;
 import org.eclipse.jetty.server.Server;
@@ -94,7 +95,21 @@ public class ThriftHttpCLIService extends ThriftCLIService {
           
hiveConf.getIntVar(ConfVars.HIVE_SERVER2_THRIFT_HTTP_RESPONSE_HEADER_SIZE);
       conf.setRequestHeaderSize(requestHeaderSize);
       conf.setResponseHeaderSize(responseHeaderSize);
-      final HttpConnectionFactory http = new HttpConnectionFactory(conf);
+      final HttpConnectionFactory http = new HttpConnectionFactory(conf) {
+        public Connection newConnection(Connector connector, EndPoint 
endPoint) {
+          Connection connection = super.newConnection(connector, endPoint);
+          connection.addListener(new Connection.Listener() {
+            public void onOpened(Connection connection) {
+              openConnection();
+            }
+
+            public void onClosed(Connection connection) {
+              closeConnection();
+            }
+          });
+          return connection;
+        }
+      };
 
       boolean useSsl = hiveConf.getBoolVar(ConfVars.HIVE_SERVER2_USE_SSL);
       String schemeName = useSsl ? "https" : "http";
@@ -156,33 +171,6 @@ public class ThriftHttpCLIService extends ThriftCLIService 
{
         LOG.warn("XSRF filter disabled");
       }
 
-      context.addEventListener(new ServletContextListener() {
-        @Override
-        public void contextInitialized(ServletContextEvent 
servletContextEvent) {
-          Metrics metrics = MetricsFactory.getInstance();
-          if (metrics != null) {
-            try {
-              metrics.incrementCounter(MetricsConstant.OPEN_CONNECTIONS);
-              
metrics.incrementCounter(MetricsConstant.CUMULATIVE_CONNECTION_COUNT);
-            } catch (Exception e) {
-              LOG.warn("Error reporting HS2 open connection operation to 
Metrics system", e);
-            }
-          }
-        }
-
-        @Override
-        public void contextDestroyed(ServletContextEvent servletContextEvent) {
-          Metrics metrics = MetricsFactory.getInstance();
-          if (metrics != null) {
-            try {
-              metrics.decrementCounter(MetricsConstant.OPEN_CONNECTIONS);
-            } catch (Exception e) {
-              LOG.warn("Error reporting HS2 close connection operation to 
Metrics system", e);
-            }
-          }
-        }
-      });
-
       final String httpPath = 
getHttpPath(hiveConf.getVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_HTTP_PATH));
 
       if (HiveConf.getBoolVar(hiveConf, 
ConfVars.HIVE_SERVER2_THRIFT_HTTP_COMPRESSION_ENABLED)) {
@@ -209,6 +197,29 @@ public class ThriftHttpCLIService extends ThriftCLIService 
{
     }
   }
 
+  private void openConnection() {
+    Metrics metrics = MetricsFactory.getInstance();
+    if (metrics != null) {
+      try {
+        metrics.incrementCounter(MetricsConstant.OPEN_CONNECTIONS);
+        metrics.incrementCounter(MetricsConstant.CUMULATIVE_CONNECTION_COUNT);
+      } catch (Exception e) {
+        LOG.warn("Error reporting HS2 open connection operation to Metrics 
system", e);
+      }
+    }
+  }
+
+  private void closeConnection() {
+    Metrics metrics = MetricsFactory.getInstance();
+    if (metrics != null) {
+      try {
+        metrics.decrementCounter(MetricsConstant.OPEN_CONNECTIONS);
+      } catch (Exception e) {
+        LOG.warn("Error reporting HS2 close connection operation to Metrics 
system", e);
+      }
+    }
+  }
+
   @Override
   public void run() {
     try {

Reply via email to