diff --git itests/hive-unit/src/test/java/org/apache/hive/jdbc/miniHS2/TestHs2ConnectionMetrics.java itests/hive-unit/src/test/java/org/apache/hive/jdbc/miniHS2/TestHs2ConnectionMetrics.java new file mode 100644 index 0000000000..ca828a2427 --- /dev/null +++ itests/hive-unit/src/test/java/org/apache/hive/jdbc/miniHS2/TestHs2ConnectionMetrics.java @@ -0,0 +1,69 @@ +/* + * 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.hive.jdbc.miniHS2; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.hadoop.hive.common.metrics.MetricsTestUtils; +import org.apache.hadoop.hive.common.metrics.common.MetricsConstant; +import org.apache.hadoop.hive.conf.HiveConf; + +import org.junit.AfterClass; +import org.junit.Test; + +/** + * Abstract class for connection metrics tests. Implementors of this class are + * {@link TestHs2ConnectionMetricsBinary} and {@link TestHs2ConnectionMetricsBinary}. These two + * classes are responsible for testing the connection metrics either in binary or in http mode. + */ +public abstract class TestHs2ConnectionMetrics { + + protected static MiniHS2 miniHS2; + protected static Map confOverlay = new HashMap<>(); + + protected static final String USERNAME = System.getProperty("user.name"); + protected static final String PASSWORD = "foo"; + + public static void setup() throws Exception { + miniHS2 = new MiniHS2(new HiveConf()); + + confOverlay.put(HiveConf.ConfVars.HIVE_SUPPORT_CONCURRENCY.varname, "false"); + confOverlay.put(HiveConf.ConfVars.SEMANTIC_ANALYZER_HOOK.varname, TestHs2Metrics.MetricCheckingHook.class.getName()); + confOverlay.put(HiveConf.ConfVars.HIVE_SERVER2_METRICS_ENABLED.varname, "true"); + + miniHS2.start(confOverlay); + } + + @AfterClass + public static void tearDown() { + miniHS2.stop(); + } + + + protected void verifyConnectionMetrics(String metricsJson, int expectedOpenConnections, + int expectedCumulativeConnections) throws Exception { + MetricsTestUtils.verifyMetricsJson(metricsJson, MetricsTestUtils.COUNTER, + MetricsConstant.OPEN_CONNECTIONS, expectedOpenConnections); + MetricsTestUtils.verifyMetricsJson(metricsJson, MetricsTestUtils.COUNTER, + MetricsConstant.CUMULATIVE_CONNECTION_COUNT, expectedCumulativeConnections); + } + + public abstract void testOpenConnectionMetrics() throws Exception; + +} diff --git itests/hive-unit/src/test/java/org/apache/hive/jdbc/miniHS2/TestHs2ConnectionMetricsBinary.java itests/hive-unit/src/test/java/org/apache/hive/jdbc/miniHS2/TestHs2ConnectionMetricsBinary.java new file mode 100644 index 0000000000..f494d94416 --- /dev/null +++ itests/hive-unit/src/test/java/org/apache/hive/jdbc/miniHS2/TestHs2ConnectionMetricsBinary.java @@ -0,0 +1,72 @@ +/* + * 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.hive.jdbc.miniHS2; + +import java.io.IOException; + +import org.apache.hadoop.hive.common.metrics.common.MetricsFactory; +import org.apache.hadoop.hive.common.metrics.metrics2.CodahaleMetrics; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hive.beeline.BeeLine; + +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test the connection metrics using BeeLine client, when HS2 is started in binary mode. + */ +public class TestHs2ConnectionMetricsBinary extends TestHs2ConnectionMetrics { + + @BeforeClass + public static void setup() throws Exception { + confOverlay.clear(); + confOverlay.put(HiveConf.ConfVars.HIVE_SERVER2_TRANSPORT_MODE.varname, "binary"); + confOverlay.put(HiveConf.ConfVars.HIVE_SERVER2_ENABLE_DOAS.varname, "false"); + TestHs2ConnectionMetrics.setup(); + } + + + @Test + @Override + public void testOpenConnectionMetrics() throws Exception { + + CodahaleMetrics metrics = (CodahaleMetrics) MetricsFactory.getInstance(); + String beelineArgs[] = { "-u", miniHS2.getBaseJdbcURL() + "default", "-n", USERNAME, + "-p", PASSWORD, "-e", "show tables;" }; + + BeeLine beeLine = openBeeLineConnection(beelineArgs); + + verifyConnectionMetrics(metrics.dumpJson(), 1, 1); + beeLine.close(); + verifyConnectionMetrics(metrics.dumpJson(), 0, 1); + + beeLine = openBeeLineConnection(beelineArgs); + verifyConnectionMetrics(metrics.dumpJson(), 1, 2); + beeLine.close(); + verifyConnectionMetrics(metrics.dumpJson(), 0, 2); + + + } + + private BeeLine openBeeLineConnection(String[] beelineArgs) throws IOException { + BeeLine beeLine = new BeeLine(); + beeLine.begin(beelineArgs, null); + return beeLine; + } + +} diff --git itests/hive-unit/src/test/java/org/apache/hive/jdbc/miniHS2/TestHs2ConnectionMetricsHttp.java itests/hive-unit/src/test/java/org/apache/hive/jdbc/miniHS2/TestHs2ConnectionMetricsHttp.java new file mode 100644 index 0000000000..3cccdd109b --- /dev/null +++ itests/hive-unit/src/test/java/org/apache/hive/jdbc/miniHS2/TestHs2ConnectionMetricsHttp.java @@ -0,0 +1,106 @@ +/* + * 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.hive.jdbc.miniHS2; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.hadoop.hive.common.metrics.common.MetricsFactory; +import org.apache.hadoop.hive.common.metrics.metrics2.CodahaleMetrics; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hive.jdbc.HttpBasicAuthInterceptor; +import org.apache.hive.service.rpc.thrift.TCLIService; +import org.apache.hive.service.rpc.thrift.TCloseSessionReq; +import org.apache.hive.service.rpc.thrift.TOpenSessionReq; +import org.apache.hive.service.rpc.thrift.TOpenSessionResp; +import org.apache.hive.service.rpc.thrift.TSessionHandle; +import org.apache.http.client.CookieStore; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.protocol.TProtocol; +import org.apache.thrift.transport.THttpClient; +import org.apache.thrift.transport.TTransport; + +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test the connection metrics using an HttpClient, when HS2 is start in http mode. + */ +public class TestHs2ConnectionMetricsHttp extends TestHs2ConnectionMetrics { + + @BeforeClass + public static void setup() throws Exception { + confOverlay.clear(); + confOverlay.put(HiveConf.ConfVars.HIVE_SERVER2_TRANSPORT_MODE.varname, "http"); + confOverlay.put(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_HTTP_PATH.varname, "cliservice"); + TestHs2ConnectionMetrics.setup(); + } + + @Test + @Override + public void testOpenConnectionMetrics() throws Exception { + CodahaleMetrics metrics = (CodahaleMetrics) MetricsFactory.getInstance(); + + TCLIService.Client httpClient = getHttpClient(); + TOpenSessionReq openSessionReq = new TOpenSessionReq(); + TOpenSessionResp tOpenSessionResp = httpClient.OpenSession(openSessionReq); + verifyConnectionMetrics(metrics.dumpJson(), 0, 1); + TSessionHandle sessionHandle = tOpenSessionResp.getSessionHandle(); + + TCloseSessionReq closeSessionReq = new TCloseSessionReq(sessionHandle); + httpClient.CloseSession(closeSessionReq); + verifyConnectionMetrics(metrics.dumpJson(), 0, 2); + + tOpenSessionResp = httpClient.OpenSession(openSessionReq); + verifyConnectionMetrics(metrics.dumpJson(), 0, 3); + sessionHandle = tOpenSessionResp.getSessionHandle(); + + closeSessionReq = new TCloseSessionReq(sessionHandle); + httpClient.CloseSession(closeSessionReq); + verifyConnectionMetrics(metrics.dumpJson(), 0, 4); + + } + + private TCLIService.Client getHttpClient() throws Exception { + DefaultHttpClient httpClient = new DefaultHttpClient(); + + Map headers = new HashMap<>(); + headers.put("Connection", "close"); + httpClient.addRequestInterceptor(new BasicHttpRequestInterceptor(USERNAME, PASSWORD, null, + null, false, headers)); + + TTransport transport = new THttpClient(getHttpUrl(), httpClient); + TProtocol protocol = new TBinaryProtocol(transport); + return new TCLIService.Client(protocol); + } + + private String getHttpUrl() { + return "http://" + miniHS2.getHost() + ":" + miniHS2.getHttpPort() + "/cliservice/"; + + } + + private class BasicHttpRequestInterceptor extends HttpBasicAuthInterceptor { + BasicHttpRequestInterceptor(String userName, String password, CookieStore cookieStore, + String cn, boolean isSSL, + Map additionalHeaders) { + super(userName, password, cookieStore, cn, isSSL, additionalHeaders, null); + } + } + +}