|
| 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.metastore.handler; |
| 20 | + |
| 21 | +import java.nio.file.Paths; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.apache.hadoop.conf.Configuration; |
| 26 | +import org.apache.hadoop.fs.Path; |
| 27 | +import org.apache.hadoop.hive.common.TableName; |
| 28 | +import org.apache.hadoop.hive.metastore.HiveMetaStoreClient; |
| 29 | +import org.apache.hadoop.hive.metastore.TableType; |
| 30 | +import org.apache.hadoop.hive.metastore.TransactionalMetaStoreEventListener; |
| 31 | +import org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest; |
| 32 | +import org.apache.hadoop.hive.metastore.api.MetaException; |
| 33 | +import org.apache.hadoop.hive.metastore.api.NoSuchObjectException; |
| 34 | +import org.apache.hadoop.hive.metastore.api.Table; |
| 35 | +import org.apache.hadoop.hive.metastore.client.MetaStoreClientTest; |
| 36 | +import org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder; |
| 37 | +import org.apache.hadoop.hive.metastore.client.builder.TableBuilder; |
| 38 | +import org.apache.hadoop.hive.metastore.conf.MetastoreConf; |
| 39 | +import org.apache.hadoop.hive.metastore.events.DropDatabaseEvent; |
| 40 | +import org.apache.hadoop.hive.metastore.events.DropTableEvent; |
| 41 | +import org.apache.hadoop.hive.metastore.minihms.AbstractMetaStoreService; |
| 42 | +import org.apache.thrift.TException; |
| 43 | +import org.datanucleus.exceptions.NucleusException; |
| 44 | +import org.junit.After; |
| 45 | +import org.junit.Before; |
| 46 | +import org.junit.BeforeClass; |
| 47 | +import org.junit.Test; |
| 48 | +import org.junit.experimental.categories.Category; |
| 49 | +import org.junit.runner.RunWith; |
| 50 | +import org.junit.runners.Parameterized; |
| 51 | + |
| 52 | +import static org.junit.Assert.assertEquals; |
| 53 | +import static org.junit.Assert.assertNotNull; |
| 54 | +import static org.junit.Assert.fail; |
| 55 | + |
| 56 | +@RunWith(Parameterized.class) |
| 57 | +@Category(MetastoreCheckinTest.class) |
| 58 | +public class TestRetryOnException extends MetaStoreClientTest { |
| 59 | + protected static final String DB_NAME = "testdroptable"; |
| 60 | + protected static final String TABLE_NAME = "test_drop_table"; |
| 61 | + private final String testTempDir = |
| 62 | + Paths.get(System.getProperty("java.io.tmpdir"), "testDropTable").toString(); |
| 63 | + private AbstractMetaStoreService metaStore; |
| 64 | + private HiveMetaStoreClient client; |
| 65 | + |
| 66 | + public TestRetryOnException(String name, AbstractMetaStoreService metaStore) { |
| 67 | + this.metaStore = metaStore; |
| 68 | + } |
| 69 | + |
| 70 | + @BeforeClass |
| 71 | + public static void startMetaStores() { |
| 72 | + Map<MetastoreConf.ConfVars, String> msConf = new HashMap<>(); |
| 73 | + Map<String, String> extraConf = new HashMap<>(); |
| 74 | + extraConf.put("metastore.transactional.event.listeners", AssertExTransactionListener.class.getName()); |
| 75 | + startMetaStores(msConf, extraConf); |
| 76 | + } |
| 77 | + |
| 78 | + @Before |
| 79 | + public void setUp() throws Exception { |
| 80 | + AssertExTransactionListener.resetTest(); |
| 81 | + client = metaStore.getClient(); |
| 82 | + cleanDB(); |
| 83 | + createDB(DB_NAME); |
| 84 | + AssertExTransactionListener.startTest(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testRetryOnException() throws Exception { |
| 89 | + String location = metaStore.getExternalWarehouseRoot() + "/" + TABLE_NAME; |
| 90 | + new TableBuilder() |
| 91 | + .setDbName(DB_NAME) |
| 92 | + .setTableName(TABLE_NAME) |
| 93 | + .addCol("test_id", "int", "test col id") |
| 94 | + .addCol("test_value", "string", "test col value") |
| 95 | + .addTableParam("param_key", "param_value") |
| 96 | + .setType(TableType.EXTERNAL_TABLE.name()) |
| 97 | + .addTableParam("EXTERNAL", "TRUE") |
| 98 | + .addStorageDescriptorParam("sd_param_key", "sd_param_value") |
| 99 | + .setSerdeName(TABLE_NAME) |
| 100 | + .setStoredAsSubDirectories(false) |
| 101 | + .addSerdeParam("serde_param_key", "serde_param_value") |
| 102 | + .setLocation(location) |
| 103 | + .create(client, metaStore.getConf()); |
| 104 | + |
| 105 | + Table table = client.getTable(DB_NAME, TABLE_NAME); |
| 106 | + assertNotNull(table); |
| 107 | + String tableName = TableName.getQualified(table.getCatName(), table.getDbName(), table.getTableName()); |
| 108 | + |
| 109 | + assertEquals(0, AssertExTransactionListener.getCalls(tableName)); |
| 110 | + client.dropTable(DB_NAME, TABLE_NAME); |
| 111 | + assertEquals(AssertExTransactionListener.maxRetries, AssertExTransactionListener.getCalls(tableName)); |
| 112 | + try { |
| 113 | + client.getTable(DB_NAME, TABLE_NAME); |
| 114 | + fail("Table " + tableName + " should be dropped"); |
| 115 | + } catch (NoSuchObjectException nse) { |
| 116 | + // expected |
| 117 | + } |
| 118 | + |
| 119 | + assertEquals(0, AssertExTransactionListener.getCalls(DB_NAME)); |
| 120 | + client.dropDatabase(DB_NAME); |
| 121 | + assertEquals(AssertExTransactionListener.maxRetries, AssertExTransactionListener.getCalls(tableName)); |
| 122 | + try { |
| 123 | + client.getDatabase(DB_NAME); |
| 124 | + fail("Database " + DB_NAME + " should be dropped"); |
| 125 | + } catch (NoSuchObjectException nse) { |
| 126 | + // expected |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public static class AssertExTransactionListener extends TransactionalMetaStoreEventListener { |
| 131 | + static Map<String, Integer> calls = new HashMap<>(); |
| 132 | + static int maxRetries = 5; |
| 133 | + static boolean startChecking = false; |
| 134 | + |
| 135 | + public AssertExTransactionListener(Configuration config) { |
| 136 | + super(config); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void onDropTable(DropTableEvent tableEvent) throws MetaException { |
| 141 | + Table table = tableEvent.getTable(); |
| 142 | + String tableName = TableName.getQualified(table.getCatName(), table.getDbName(), table.getTableName()); |
| 143 | + Integer pre = calls.putIfAbsent(tableName, 1); |
| 144 | + if (pre != null) { |
| 145 | + calls.put(tableName, pre + 1); |
| 146 | + } |
| 147 | + // For a drop table call, we should see 5(maxRetries) retries in RetryingHMSHandler |
| 148 | + assertException(startChecking && calls.get(tableName) < maxRetries); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void onDropDatabase(DropDatabaseEvent dbEvent) throws MetaException { |
| 153 | + String dbName = dbEvent.getDatabase().getName(); |
| 154 | + Integer pre = calls.putIfAbsent(dbName, 1); |
| 155 | + if (pre != null) { |
| 156 | + calls.put(dbName, pre + 1); |
| 157 | + } |
| 158 | + assertException(startChecking && calls.get(dbName) < maxRetries); |
| 159 | + } |
| 160 | + |
| 161 | + private void assertException(boolean throwException) throws MetaException { |
| 162 | + if (throwException) { |
| 163 | + MetaException exception = new MetaException("An exception for RetryingHMSHandler to retry"); |
| 164 | + exception.initCause(new NucleusException("Non-fatal exception")); |
| 165 | + throw exception; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + static void startTest() { |
| 170 | + startChecking = true; |
| 171 | + calls.clear(); |
| 172 | + } |
| 173 | + |
| 174 | + static void resetTest() { |
| 175 | + startChecking = false; |
| 176 | + calls.clear(); |
| 177 | + } |
| 178 | + |
| 179 | + static int getCalls(String key) { |
| 180 | + return calls.get(key) != null ? calls.get(key) : 0; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + @After |
| 185 | + public void tearDown() throws Exception { |
| 186 | + try { |
| 187 | + if (client != null) { |
| 188 | + try { |
| 189 | + client.close(); |
| 190 | + } catch (Exception e) { |
| 191 | + } |
| 192 | + } |
| 193 | + } finally { |
| 194 | + client = null; |
| 195 | + } |
| 196 | + |
| 197 | + Path path = new Path(testTempDir); |
| 198 | + path.getFileSystem(metaStore.getConf()).delete(path, true); |
| 199 | + } |
| 200 | + |
| 201 | + protected void cleanDB() throws Exception{ |
| 202 | + client.dropDatabase(DB_NAME, true, true, true); |
| 203 | + metaStore.cleanWarehouseDirs(); |
| 204 | + } |
| 205 | + |
| 206 | + protected void createDB(String dbName) throws TException { |
| 207 | + new DatabaseBuilder(). |
| 208 | + setName(dbName). |
| 209 | + create(client, metaStore.getConf()); |
| 210 | + } |
| 211 | +} |
0 commit comments