|
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.relational.it.query.recent.informationschema; |
| 21 | + |
| 22 | +import org.apache.iotdb.commons.conf.CommonDescriptor; |
| 23 | +import org.apache.iotdb.db.queryengine.execution.QueryState; |
| 24 | +import org.apache.iotdb.it.env.EnvFactory; |
| 25 | +import org.apache.iotdb.it.framework.IoTDBTestRunner; |
| 26 | +import org.apache.iotdb.itbase.category.TableLocalStandaloneIT; |
| 27 | +import org.apache.iotdb.itbase.env.BaseEnv; |
| 28 | + |
| 29 | +import org.junit.AfterClass; |
| 30 | +import org.junit.Assert; |
| 31 | +import org.junit.BeforeClass; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.experimental.categories.Category; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | + |
| 36 | +import java.sql.Connection; |
| 37 | +import java.sql.ResultSet; |
| 38 | +import java.sql.ResultSetMetaData; |
| 39 | +import java.sql.SQLException; |
| 40 | +import java.sql.Statement; |
| 41 | + |
| 42 | +import static org.apache.iotdb.commons.schema.column.ColumnHeaderConstant.END_TIME_TABLE_MODEL; |
| 43 | +import static org.apache.iotdb.commons.schema.column.ColumnHeaderConstant.NUMS; |
| 44 | +import static org.apache.iotdb.commons.schema.column.ColumnHeaderConstant.STATEMENT_TABLE_MODEL; |
| 45 | +import static org.apache.iotdb.commons.schema.column.ColumnHeaderConstant.STATE_TABLE_MODEL; |
| 46 | +import static org.apache.iotdb.commons.schema.column.ColumnHeaderConstant.USER_TABLE_MODEL; |
| 47 | +import static org.apache.iotdb.commons.schema.table.InformationSchema.getSchemaTables; |
| 48 | +import static org.apache.iotdb.db.it.utils.TestUtils.createUser; |
| 49 | +import static org.apache.iotdb.itbase.env.BaseEnv.TABLE_SQL_DIALECT; |
| 50 | +import static org.junit.Assert.fail; |
| 51 | + |
| 52 | +@RunWith(IoTDBTestRunner.class) |
| 53 | +@Category({TableLocalStandaloneIT.class}) |
| 54 | +// This IT will run at least 60s, so we only run it in 1C1D |
| 55 | +public class IoTDBCurrentQueriesIT { |
| 56 | + private static final int CURRENT_QUERIES_COLUMN_NUM = |
| 57 | + getSchemaTables().get("current_queries").getColumnNum(); |
| 58 | + private static final int QUERIES_COSTS_HISTOGRAM_COLUMN_NUM = |
| 59 | + getSchemaTables().get("queries_costs_histogram").getColumnNum(); |
| 60 | + private static final String ADMIN_NAME = |
| 61 | + CommonDescriptor.getInstance().getConfig().getDefaultAdminName(); |
| 62 | + private static final String ADMIN_PWD = |
| 63 | + CommonDescriptor.getInstance().getConfig().getAdminPassword(); |
| 64 | + |
| 65 | + @BeforeClass |
| 66 | + public static void setUp() throws Exception { |
| 67 | + EnvFactory.getEnv().getConfig().getDataNodeConfig().setQueryCostStatWindow(1); |
| 68 | + EnvFactory.getEnv().initClusterEnvironment(); |
| 69 | + createUser("test", "test123123456"); |
| 70 | + } |
| 71 | + |
| 72 | + @AfterClass |
| 73 | + public static void tearDown() throws Exception { |
| 74 | + EnvFactory.getEnv().cleanClusterEnvironment(); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testCurrentQueries() { |
| 79 | + try { |
| 80 | + Connection connection = |
| 81 | + EnvFactory.getEnv().getConnection(ADMIN_NAME, ADMIN_PWD, BaseEnv.TABLE_SQL_DIALECT); |
| 82 | + Statement statement = connection.createStatement(); |
| 83 | + statement.execute("USE information_schema"); |
| 84 | + |
| 85 | + // 1. query current_queries table |
| 86 | + String sql = "SELECT * FROM current_queries"; |
| 87 | + ResultSet resultSet = statement.executeQuery(sql); |
| 88 | + ResultSetMetaData metaData = resultSet.getMetaData(); |
| 89 | + Assert.assertEquals(CURRENT_QUERIES_COLUMN_NUM, metaData.getColumnCount()); |
| 90 | + int rowNum = 0; |
| 91 | + while (resultSet.next()) { |
| 92 | + Assert.assertEquals(QueryState.RUNNING.name(), resultSet.getString(STATE_TABLE_MODEL)); |
| 93 | + Assert.assertEquals(null, resultSet.getString(END_TIME_TABLE_MODEL)); |
| 94 | + Assert.assertEquals(sql, resultSet.getString(STATEMENT_TABLE_MODEL)); |
| 95 | + Assert.assertEquals(ADMIN_NAME, resultSet.getString(USER_TABLE_MODEL)); |
| 96 | + rowNum++; |
| 97 | + } |
| 98 | + Assert.assertEquals(1, rowNum); |
| 99 | + resultSet.close(); |
| 100 | + |
| 101 | + // 2. query queries_costs_histogram table |
| 102 | + sql = "SELECT * FROM queries_costs_histogram"; |
| 103 | + resultSet = statement.executeQuery(sql); |
| 104 | + metaData = resultSet.getMetaData(); |
| 105 | + Assert.assertEquals(QUERIES_COSTS_HISTOGRAM_COLUMN_NUM, metaData.getColumnCount()); |
| 106 | + rowNum = 0; |
| 107 | + int queriesCount = 0; |
| 108 | + while (resultSet.next()) { |
| 109 | + int nums = resultSet.getInt(NUMS); |
| 110 | + if (nums > 0) { |
| 111 | + queriesCount++; |
| 112 | + } |
| 113 | + rowNum++; |
| 114 | + } |
| 115 | + Assert.assertEquals(1, queriesCount); |
| 116 | + Assert.assertEquals(61, rowNum); |
| 117 | + |
| 118 | + // 3. requery current_queries table |
| 119 | + sql = "SELECT * FROM current_queries"; |
| 120 | + resultSet = statement.executeQuery(sql); |
| 121 | + metaData = resultSet.getMetaData(); |
| 122 | + Assert.assertEquals(CURRENT_QUERIES_COLUMN_NUM, metaData.getColumnCount()); |
| 123 | + rowNum = 0; |
| 124 | + int finishedQueries = 0; |
| 125 | + while (resultSet.next()) { |
| 126 | + if (QueryState.FINISHED.name().equals(resultSet.getString(STATE_TABLE_MODEL))) { |
| 127 | + finishedQueries++; |
| 128 | + } |
| 129 | + rowNum++; |
| 130 | + } |
| 131 | + // three rows in the result, 2 FINISHED and 1 RUNNING |
| 132 | + Assert.assertEquals(3, rowNum); |
| 133 | + Assert.assertEquals(2, finishedQueries); |
| 134 | + resultSet.close(); |
| 135 | + |
| 136 | + // 4. test the expired QueryInfo was evicted |
| 137 | + Thread.sleep(61_001); |
| 138 | + resultSet = statement.executeQuery(sql); |
| 139 | + rowNum = 0; |
| 140 | + while (resultSet.next()) { |
| 141 | + rowNum++; |
| 142 | + } |
| 143 | + // one row in the result, current query |
| 144 | + Assert.assertEquals(1, rowNum); |
| 145 | + resultSet.close(); |
| 146 | + |
| 147 | + sql = "SELECT * FROM queries_costs_histogram"; |
| 148 | + resultSet = statement.executeQuery(sql); |
| 149 | + queriesCount = 0; |
| 150 | + while (resultSet.next()) { |
| 151 | + int nums = resultSet.getInt(NUMS); |
| 152 | + if (nums > 0) { |
| 153 | + queriesCount++; |
| 154 | + } |
| 155 | + } |
| 156 | + // the last current_queries table query was recorded, others are evicted |
| 157 | + Assert.assertEquals(1, queriesCount); |
| 158 | + } catch (Exception e) { |
| 159 | + fail(e.getMessage()); |
| 160 | + } |
| 161 | + |
| 162 | + // 5. test privilege |
| 163 | + testPrivilege(); |
| 164 | + } |
| 165 | + |
| 166 | + private void testPrivilege() { |
| 167 | + // 1. test current_queries table |
| 168 | + try (Connection connection = |
| 169 | + EnvFactory.getEnv().getConnection("test", "test123123456", TABLE_SQL_DIALECT); |
| 170 | + Statement statement = connection.createStatement()) { |
| 171 | + String sql = "SELECT * FROM information_schema.current_queries"; |
| 172 | + |
| 173 | + // another user executes a query |
| 174 | + try (Connection connection2 = |
| 175 | + EnvFactory.getEnv().getConnection(ADMIN_NAME, ADMIN_PWD, BaseEnv.TABLE_SQL_DIALECT)) { |
| 176 | + ResultSet resultSet = connection2.createStatement().executeQuery(sql); |
| 177 | + resultSet.close(); |
| 178 | + } catch (Exception e) { |
| 179 | + fail(e.getMessage()); |
| 180 | + } |
| 181 | + |
| 182 | + // current user query current_queries table |
| 183 | + ResultSet resultSet = statement.executeQuery(sql); |
| 184 | + int rowNum = 0; |
| 185 | + while (resultSet.next()) { |
| 186 | + rowNum++; |
| 187 | + } |
| 188 | + // only current query in the result |
| 189 | + Assert.assertEquals(1, rowNum); |
| 190 | + } catch (SQLException e) { |
| 191 | + fail(e.getMessage()); |
| 192 | + } |
| 193 | + |
| 194 | + // 2. test queries_costs_histogram table |
| 195 | + try (Connection connection = |
| 196 | + EnvFactory.getEnv().getConnection("test", "test123123456", TABLE_SQL_DIALECT); |
| 197 | + Statement statement = connection.createStatement()) { |
| 198 | + statement.executeQuery("SELECT * FROM information_schema.queries_costs_histogram"); |
| 199 | + } catch (SQLException e) { |
| 200 | + Assert.assertEquals( |
| 201 | + "803: Access Denied: No permissions for this operation, please add privilege SYSTEM", |
| 202 | + e.getMessage()); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments