|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.hive.ql.processor; |
| 20 | + |
| 21 | +import org.apache.hadoop.hive.conf.HiveConf; |
| 22 | +import org.apache.hadoop.hive.metastore.conf.MetastoreConf; |
| 23 | +import org.apache.hadoop.hive.metastore.utils.TestTxnDbUtil; |
| 24 | +import org.apache.hive.jdbc.miniHS2.MiniHS2; |
| 25 | +import org.junit.AfterClass; |
| 26 | +import org.junit.Assert; |
| 27 | +import org.junit.BeforeClass; |
| 28 | +import org.junit.Test; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | + |
| 32 | +import java.io.File; |
| 33 | +import java.net.URI; |
| 34 | + |
| 35 | +import java.sql.Connection; |
| 36 | +import java.sql.DriverManager; |
| 37 | +import java.sql.ResultSet; |
| 38 | +import java.sql.Statement; |
| 39 | +import java.util.Arrays; |
| 40 | +import java.util.HashMap; |
| 41 | +import java.util.HashSet; |
| 42 | +import java.util.Map; |
| 43 | +import java.util.Set; |
| 44 | +import java.util.Collections; |
| 45 | + |
| 46 | +import java.util.concurrent.LinkedBlockingQueue; |
| 47 | +import java.util.concurrent.ThreadPoolExecutor; |
| 48 | +import java.util.concurrent.TimeUnit; |
| 49 | + |
| 50 | +public class TestShowProcessList { |
| 51 | + protected static final Logger LOG = LoggerFactory.getLogger(TestShowProcessList.class); |
| 52 | + |
| 53 | + private static MiniHS2 miniHS2 = null; |
| 54 | + private static HiveConf conf; |
| 55 | + private static String user; |
| 56 | + private static ThreadPoolExecutor executor; |
| 57 | + |
| 58 | + static HiveConf defaultConf() throws Exception { |
| 59 | + String confDir = "../../data/conf/llap/"; |
| 60 | + HiveConf.setHiveSiteLocation(new URI("file://" + new File(confDir).toURI().getPath() + "/hive-site.xml").toURL()); |
| 61 | + System.out.println("Setting hive-site: " + HiveConf.getHiveSiteLocation()); |
| 62 | + HiveConf defaultConf = new HiveConf(); |
| 63 | + defaultConf.addResource(new URI("file://" + new File(confDir).toURI().getPath() + "/tez-site.xml").toURL()); |
| 64 | + return defaultConf; |
| 65 | + } |
| 66 | + |
| 67 | + @BeforeClass |
| 68 | + public static void beforeTest() throws Exception { |
| 69 | + conf = defaultConf(); |
| 70 | + user = System.getProperty("user.name"); |
| 71 | + conf.setVar(HiveConf.ConfVars.USERS_IN_ADMIN_ROLE, user); |
| 72 | + conf.setVar(HiveConf.ConfVars.HIVE_AUTHORIZATION_MANAGER, |
| 73 | + "org.apache.hadoop.hive.ql.security.authorization.plugin.sqlstd.SQLStdHiveAuthorizerFactory"); |
| 74 | + String suffix = TestShowProcessList.class.getCanonicalName() + "-" + System.currentTimeMillis(); |
| 75 | + String dir = new File(System.getProperty("java.io.tmpdir") + File.separator + suffix).getPath() |
| 76 | + .replaceAll("\\\\", "/") + "/warehouse"; |
| 77 | + conf.set(MetastoreConf.ConfVars.WAREHOUSE.name(), dir); |
| 78 | + TestTxnDbUtil.setConfValues(conf); |
| 79 | + TestTxnDbUtil.prepDb(conf); |
| 80 | + MiniHS2.cleanupLocalDir(); |
| 81 | + Class.forName(MiniHS2.getJdbcDriverName()); |
| 82 | + miniHS2 = new MiniHS2(conf, MiniHS2.MiniClusterType.LLAP); |
| 83 | + Map<String, String> confOverlay = new HashMap<>(); |
| 84 | + miniHS2.start(confOverlay); |
| 85 | + |
| 86 | + executor = new ThreadPoolExecutor(5, 10, 50, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>()); |
| 87 | + } |
| 88 | + |
| 89 | + @AfterClass |
| 90 | + public static void afterTest() throws Exception { |
| 91 | + if (miniHS2 != null && miniHS2.isStarted()) { |
| 92 | + miniHS2.stop(); |
| 93 | + } |
| 94 | + TestTxnDbUtil.cleanDb(conf); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testQueries() throws Exception { |
| 99 | + int connections = 10; |
| 100 | + //Initiate 10 parallel connections, each with a query that begins a transaction. |
| 101 | + for (int i = 0; i < connections; i++) { |
| 102 | + executor.submit(() -> { |
| 103 | + try (Connection con = DriverManager.getConnection(miniHS2.getJdbcURL(), user, "bar"); |
| 104 | + Statement stmt = con.createStatement()) { |
| 105 | + stmt.execute("drop database if exists DB_" + Thread.currentThread().threadId() + " cascade"); |
| 106 | + } catch (Exception exception) { |
| 107 | + LOG.error(exception.getMessage()); |
| 108 | + LOG.error(Arrays.toString(exception.getStackTrace())); |
| 109 | + } |
| 110 | + }); |
| 111 | + } |
| 112 | + Set<Integer> txnIds = new HashSet<>(); |
| 113 | + try (Connection testCon = DriverManager.getConnection(miniHS2.getJdbcURL(), user, "bar"); |
| 114 | + Statement s = testCon.createStatement()) { |
| 115 | + while (executor.getActiveCount() > 0) { |
| 116 | + // retrieve txnIds from show processlist output |
| 117 | + txnIds.addAll(getTxnIdsFromShowProcesslist(s)); |
| 118 | + } |
| 119 | + } |
| 120 | + System.out.println(txnIds); |
| 121 | + // max txnId should be equal to the number of connections |
| 122 | + int maxTxnId = Collections.max(txnIds); |
| 123 | + Assert.assertEquals(maxTxnId, connections); |
| 124 | + } |
| 125 | + |
| 126 | + private static Set<Integer> getTxnIdsFromShowProcesslist(Statement s) { |
| 127 | + Set<Integer> txnIds = new HashSet<>(); |
| 128 | + try (ResultSet rs = s.executeQuery("show processlist")) { |
| 129 | + while (rs.next()) { |
| 130 | + int txnId = Integer.parseInt(rs.getString("Txn ID")); |
| 131 | + // TxnId can be 0 because the query has not yet opened txn when show processlist is run. |
| 132 | + if (txnId > 0) { |
| 133 | + txnIds.add(txnId); |
| 134 | + } |
| 135 | + } |
| 136 | + } catch (Exception exception) { |
| 137 | + LOG.error("Exception when checking hive state", exception); |
| 138 | + LOG.error(Arrays.toString(exception.getStackTrace())); |
| 139 | + |
| 140 | + } |
| 141 | + return txnIds; |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments