|
| 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; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.concurrent.Executors; |
| 23 | +import java.util.concurrent.ScheduledExecutorService; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | + |
| 26 | +import org.apache.hadoop.fs.FileSystem; |
| 27 | +import org.apache.hadoop.fs.Path; |
| 28 | +import org.apache.hadoop.hive.conf.HiveConf; |
| 29 | +import org.apache.hadoop.hive.ql.cache.results.QueryResultsCache; |
| 30 | +import org.apache.hadoop.hive.ql.session.SessionState; |
| 31 | +import org.apache.hive.testutils.HiveTestEnvSetup; |
| 32 | + |
| 33 | +import org.junit.After; |
| 34 | +import org.junit.AfterClass; |
| 35 | +import org.junit.Assert; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.BeforeClass; |
| 38 | +import org.junit.ClassRule; |
| 39 | +import org.junit.Rule; |
| 40 | +import org.junit.Test; |
| 41 | +import org.junit.rules.TestRule; |
| 42 | + |
| 43 | +import org.slf4j.Logger; |
| 44 | +import org.slf4j.LoggerFactory; |
| 45 | + |
| 46 | +public class TestCachedResults { |
| 47 | + |
| 48 | + private static final Logger LOG = LoggerFactory.getLogger(TestCachedResults.class); |
| 49 | + private static final long MAX_ALLOWED_CACHE_SIZE = 1_000_000L; |
| 50 | + |
| 51 | + private static final String Q_WINDOW = |
| 52 | + "SELECT t1.id, " |
| 53 | + + "SUM(t2.id) OVER (PARTITION BY t1.id % 10 ORDER BY t1.id) AS running_sum, " |
| 54 | + + "AVG(t2.id) OVER (PARTITION BY t1.id % 10) AS window_avg, " |
| 55 | + + "COUNT(t2.id) OVER (PARTITION BY t1.id % 10 ORDER BY t1.id) AS running_cnt " |
| 56 | + + "FROM tab t1 " |
| 57 | + + "JOIN tab t2 ON t1.id % 10 = t2.id % 10 " |
| 58 | + + "WHERE t1.id <= 300"; |
| 59 | + |
| 60 | + private static final String Q_JOIN = |
| 61 | + "SELECT base.id, base.bucket, agg.bucket_avg " |
| 62 | + + "FROM ( SELECT id, id % 10 AS bucket FROM tab WHERE id <= 500 ) base " |
| 63 | + + "JOIN ( " |
| 64 | + + " SELECT id % 10 AS bucket, AVG(id) AS bucket_avg, COUNT(*) AS cnt " |
| 65 | + + " FROM tab GROUP BY id % 10 " |
| 66 | + + ") agg ON base.bucket = agg.bucket " |
| 67 | + + "ORDER BY base.id"; |
| 68 | + |
| 69 | + private static final String Q_CTE = |
| 70 | + "WITH base AS ( " |
| 71 | + + " SELECT id, id % 2 AS is_even, id % 5 AS mod5, id % 10 AS mod10 FROM tab " |
| 72 | + + "), joined AS ( " |
| 73 | + + " SELECT a.id AS a_id, b.id AS b_id, a.mod5, a.mod10, (a.id * b.id) AS product " |
| 74 | + + " FROM base a JOIN base b ON a.mod5 = b.mod5 AND a.is_even = b.is_even " |
| 75 | + + " WHERE a.id <= 200 AND b.id <= 200 " |
| 76 | + + ") " |
| 77 | + + "SELECT mod5, mod10, COUNT(*) AS cnt, SUM(product) AS total_product, " |
| 78 | + + "MAX(product) AS max_product, MIN(a_id) AS min_a " |
| 79 | + + "FROM joined GROUP BY mod5, mod10 ORDER BY mod5, mod10"; |
| 80 | + |
| 81 | + @ClassRule |
| 82 | + public static HiveTestEnvSetup envSetup = new HiveTestEnvSetup(); |
| 83 | + |
| 84 | + private static HiveConf conf; |
| 85 | + |
| 86 | + private ScheduledExecutorService scheduler; |
| 87 | + private volatile long maxCacheSize; |
| 88 | + |
| 89 | + @BeforeClass |
| 90 | + public static void setUp() throws Exception { |
| 91 | + conf = envSetup.getTestCtx().hiveConf; |
| 92 | + HiveConf.setBoolVar(conf, HiveConf.ConfVars.HIVE_QUERY_RESULTS_CACHE_ENABLED, true); |
| 93 | + HiveConf.setVar(conf, HiveConf.ConfVars.HIVE_QUERY_RESULTS_CACHE_DIRECTORY, "/tmp/hive/cache"); |
| 94 | + HiveConf.setBoolVar(conf, HiveConf.ConfVars.HIVE_QUERY_RESULTS_CACHE_NONTRANSACTIONAL_TABLES_ENABLED, true); |
| 95 | + HiveConf.setLongVar(conf, HiveConf.ConfVars.HIVE_QUERY_RESULTS_CACHE_MAX_SIZE, MAX_ALLOWED_CACHE_SIZE); |
| 96 | + LOG.info("max allowed cache size: {}", conf.getLongVar(HiveConf.ConfVars.HIVE_QUERY_RESULTS_CACHE_MAX_SIZE)); |
| 97 | + HiveConf.setVar(conf, HiveConf.ConfVars.HIVE_AUTHORIZATION_MANAGER, |
| 98 | + "org.apache.hadoop.hive.ql.security.authorization.plugin.sqlstd.SQLStdHiveAuthorizerFactory"); |
| 99 | + HiveConf.setBoolVar(conf, HiveConf.ConfVars.HIVE_SUPPORT_CONCURRENCY, false); |
| 100 | + SessionState.start(conf); |
| 101 | + createAndPopulateTables(); |
| 102 | + } |
| 103 | + |
| 104 | + private static void createAndPopulateTables() throws Exception { |
| 105 | + IDriver driver = DriverFactory.newDriver(conf); |
| 106 | + driver.run("DROP TABLE IF EXISTS tab"); |
| 107 | + driver.run("CREATE TABLE tab (id INT)"); |
| 108 | + driver.run( |
| 109 | + "INSERT INTO TABLE tab SELECT pos + 1 AS id FROM ( " |
| 110 | + + "SELECT posexplode(split(space(999), ' ')) AS (pos, val) ) t"); |
| 111 | + } |
| 112 | + |
| 113 | + @AfterClass |
| 114 | + public static void afterClass() throws Exception { |
| 115 | + DriverFactory.newDriver(conf).run("DROP TABLE IF EXISTS tab"); |
| 116 | + } |
| 117 | + |
| 118 | + @Before |
| 119 | + public void beforeEach() { |
| 120 | + scheduler = Executors.newSingleThreadScheduledExecutor(); |
| 121 | + maxCacheSize = 0; |
| 122 | + } |
| 123 | + |
| 124 | + @After |
| 125 | + public void afterEach() throws Exception { |
| 126 | + QueryResultsCache.cleanupInstance(); |
| 127 | + Path cachePath = new Path(conf.getVar(HiveConf.ConfVars.HIVE_QUERY_RESULTS_CACHE_DIRECTORY)); |
| 128 | + try { |
| 129 | + FileSystem fs = cachePath.getFileSystem(conf); |
| 130 | + fs.delete(cachePath, true); |
| 131 | + } catch (IOException e) { |
| 132 | + LOG.warn("Failed to clean up cache directory: {}", cachePath, e); |
| 133 | + } |
| 134 | + scheduler.shutdownNow(); |
| 135 | + scheduler.awaitTermination(1, TimeUnit.SECONDS); |
| 136 | + } |
| 137 | + |
| 138 | + private void executeQueries(IDriver driver) throws Exception { |
| 139 | + for (int i = 0; i < 2; i++) { |
| 140 | + driver.run(Q_WINDOW); |
| 141 | + driver.run(Q_JOIN); |
| 142 | + driver.run(Q_CTE); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testSafeCacheWrite() throws Exception { |
| 148 | + runCacheScenario(true); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testUnsafeCacheWrite() throws Exception { |
| 153 | + runCacheScenario(false); |
| 154 | + } |
| 155 | + |
| 156 | + private void runCacheScenario(boolean safeCacheWrite) throws Exception { |
| 157 | + HiveConf.setBoolVar(conf, HiveConf.ConfVars.HIVE_QUERY_RESULTS_SAFE_CACHE_WRITE_ENABLED, safeCacheWrite); |
| 158 | + startCacheMonitor(1); |
| 159 | + executeQueries(DriverFactory.newDriver(conf)); |
| 160 | + stopCacheMonitor(); |
| 161 | + Assert.assertNotEquals("cache size should have grown", 0, maxCacheSize); |
| 162 | + if (safeCacheWrite) { |
| 163 | + LOG.info("Maximum cache size in safe mode: {}", maxCacheSize); |
| 164 | + Assert.assertFalse("max cache size should stay within limit", maxCacheSize > MAX_ALLOWED_CACHE_SIZE); |
| 165 | + } else { |
| 166 | + LOG.info("Maximum cache size in non-safe mode: {}", maxCacheSize); |
| 167 | + Assert.assertFalse("max cache size should exceed limit when unsafe", maxCacheSize < MAX_ALLOWED_CACHE_SIZE); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + private void startCacheMonitor(long intervalMs) { |
| 172 | + Path cachePath = new Path(conf.getVar(HiveConf.ConfVars.HIVE_QUERY_RESULTS_CACHE_DIRECTORY)); |
| 173 | + scheduler.scheduleAtFixedRate(() -> { |
| 174 | + try { |
| 175 | + long size = cachePath.getFileSystem(conf).getContentSummary(cachePath).getLength(); |
| 176 | + maxCacheSize = Math.max(maxCacheSize, size); |
| 177 | + } catch (IOException e) { |
| 178 | + LOG.debug("cache path not readable yet: {}", cachePath, e); |
| 179 | + } |
| 180 | + }, 0, intervalMs, TimeUnit.MILLISECONDS); |
| 181 | + } |
| 182 | + |
| 183 | + private void stopCacheMonitor() throws InterruptedException { |
| 184 | + scheduler.shutdown(); |
| 185 | + scheduler.awaitTermination(2, TimeUnit.SECONDS); |
| 186 | + } |
| 187 | +} |
0 commit comments