Skip to content

Commit caaf47c

Browse files
committed
fix ut
1 parent d381e86 commit caaf47c

2 files changed

Lines changed: 65 additions & 89 deletions

File tree

standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HMSHandler.java

Lines changed: 36 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,16 +1416,12 @@ public TruncateTableResponse truncate_table_req(TruncateTableRequest req)
14161416
}
14171417

14181418
@Override
1419-
public List<ExtendedTableInfo> get_tables_ext(final GetTablesExtRequest req) throws MetaException {
1419+
public List<ExtendedTableInfo> get_tables_ext(final GetTablesExtRequest req) throws TException {
14201420
req.setCatalog(req.isSetCatalog() ? req.getCatalog() : getDefaultCatalog(conf));
1421-
try {
1422-
return GetTableHandler.getTables(
1423-
() -> startTableFunction("get_tables_ext", req.getCatalog(), req.getDatabase(), req.getTableNamePattern()),
1424-
this, req,
1425-
t -> endFunction("get_tables_ext", t.getLeft() != null, t.getRight()));
1426-
} catch (Exception e) {
1427-
throw handleException(e).defaultMetaException();
1428-
}
1421+
return GetTableHandler.getTables(
1422+
() -> startTableFunction("get_tables_ext", req.getCatalog(), req.getDatabase(), req.getTableNamePattern()),
1423+
this, req,
1424+
t -> endFunction("get_tables_ext", t.getLeft() != null, t.getRight()));
14291425
}
14301426

14311427
@Override
@@ -1440,17 +1436,13 @@ public GetTableResult get_table_req(GetTableRequest req) throws MetaException,
14401436

14411437
@Override
14421438
public List<TableMeta> get_table_meta(String dbnames, String tblNames, List<String> tblTypes)
1443-
throws MetaException, NoSuchObjectException {
1439+
throws TException {
14441440
String[] parsedDbName = parseDbName(dbnames, conf);
1445-
try {
1446-
return GetTableHandler.getTables(
1447-
() -> startTableFunction("get_table_metas", parsedDbName[CAT_NAME], parsedDbName[DB_NAME], tblNames),
1448-
this, GetTableHandler.GetTableNamesRequest.fromDatabase(dbnames, conf)
1449-
.byType(tblTypes != null ? String.join(",", tblTypes) : null, tblNames).forTableMeta(),
1450-
t -> endFunction("get_table_metas", t.getLeft() != null, t.getRight(), join(t.getLeft(), ",")));
1451-
} catch (Exception e) {
1452-
throw handleException(e).defaultMetaException();
1453-
}
1441+
return GetTableHandler.getTables(
1442+
() -> startTableFunction("get_table_metas", parsedDbName[CAT_NAME], parsedDbName[DB_NAME], tblNames),
1443+
this, GetTableHandler.GetTableNamesRequest.fromDatabase(dbnames, conf)
1444+
.byType(tblTypes != null ? String.join(",", tblTypes) : null, tblNames).forTableMeta(),
1445+
t -> endFunction("get_table_metas", t.getLeft() != null, t.getRight(), join(t.getLeft(), ",")));
14541446
}
14551447

14561448
/**
@@ -1472,23 +1464,15 @@ public Table get_table_core(GetTableRequest getTableRequest) throws MetaExceptio
14721464
* are retrievable from the database specified by "dbnames."
14731465
* There is no guarantee of the order of the returned tables.
14741466
* If there are duplicate names, only one instance of the table will be returned.
1475-
* @throws MetaException
1476-
* @throws InvalidOperationException
1477-
* @throws UnknownDBException
1467+
* @throws TException
14781468
*/
14791469
@Override
14801470
public GetTablesResult get_table_objects_by_name_req(GetTablesRequest req) throws TException {
1481-
try {
1482-
List<Table> tables = GetTableHandler.getTables(
1483-
() -> startMultiTableFunction("get_multi_table", req.getDbName(), req.getTblNames()),
1484-
this, req,
1485-
t -> endFunction("get_multi_table", t.getLeft() != null, t.getRight(), join(req.getTblNames(), ",")));
1486-
return new GetTablesResult(tables);
1487-
} catch (Exception e) {
1488-
throw handleException(e)
1489-
.throwIfInstance(MetaException.class, InvalidOperationException.class, UnknownDBException.class)
1490-
.defaultMetaException();
1491-
}
1471+
List<Table> tables = GetTableHandler.getTables(
1472+
() -> startMultiTableFunction("get_multi_table", req.getDbName(), req.getTblNames()),
1473+
this, req,
1474+
t -> endFunction("get_multi_table", t.getLeft() != null, t.getRight(), join(req.getTblNames(), ",")));
1475+
return new GetTablesResult(tables);
14921476
}
14931477

14941478
@Override
@@ -1499,17 +1483,11 @@ public void update_creation_metadata(String catName, final String dbName, final
14991483
@Override
15001484
public List<String> get_table_names_by_filter(
15011485
final String dbName, final String filter, final short maxTables)
1502-
throws MetaException, InvalidOperationException, UnknownDBException {
1503-
try {
1504-
return GetTableHandler.getTables(
1505-
() -> startFunction("get_table_names_by_filter", ": db = " + dbName + ", filter = " + filter),
1506-
this, GetTableHandler.GetTableNamesRequest.fromDatabase(dbName, conf).byFilter(filter, maxTables),
1507-
t -> endFunction("get_table_names_by_filter", t.getLeft() != null, t.getRight(), join(t.getLeft(), ",")));
1508-
} catch (Exception e) {
1509-
throw handleException(e)
1510-
.throwIfInstance(MetaException.class, InvalidOperationException.class, UnknownDBException.class)
1511-
.defaultMetaException();
1512-
}
1486+
throws TException {
1487+
return GetTableHandler.getTables(
1488+
() -> startFunction("get_table_names_by_filter", ": db = " + dbName + ", filter = " + filter),
1489+
this, GetTableHandler.GetTableNamesRequest.fromDatabase(dbName, conf).byFilter(filter, maxTables),
1490+
t -> endFunction("get_table_names_by_filter", t.getLeft() != null, t.getRight(), join(t.getLeft(), ",")));
15131491
}
15141492

15151493
@Override
@@ -1989,7 +1967,7 @@ public PartitionValuesResponse get_partition_values(PartitionValuesRequest reque
19891967
return resps.getFirst();
19901968
} catch (Exception e) {
19911969
ex = e;
1992-
throw handleException(e).throwIfInstance(MetaException.class).defaultMetaException();
1970+
throw handleException(e).defaultMetaException();
19931971
} finally {
19941972
endFunction("get_partition_values", ex == null, ex, tableName.toString());
19951973
}
@@ -2174,27 +2152,19 @@ private void alter_table_core(String catName, String dbname, String name, Table
21742152

21752153
@Override
21762154
public List<String> get_tables(final String dbname, final String pattern)
2177-
throws MetaException {
2178-
try {
2179-
return GetTableHandler.getTables(() -> startFunction("get_tables", ": db=" + dbname + " pat=" + pattern),
2180-
this, GetTableHandler.GetTableNamesRequest.fromDatabase(dbname, conf).byPattern(pattern),
2181-
t -> endFunction("get_tables", t.getLeft() != null, t.getRight()));
2182-
} catch (Exception e) {
2183-
throw handleException(e).defaultMetaException();
2184-
}
2155+
throws TException {
2156+
return GetTableHandler.getTables(() -> startFunction("get_tables", ": db=" + dbname + " pat=" + pattern),
2157+
this, GetTableHandler.GetTableNamesRequest.fromDatabase(dbname, conf).byPattern(pattern),
2158+
t -> endFunction("get_tables", t.getLeft() != null, t.getRight()));
21852159
}
21862160

21872161
@Override
21882162
public List<String> get_tables_by_type(final String dbname, final String pattern, final String tableType)
2189-
throws MetaException {
2190-
try {
2191-
return GetTableHandler.getTables(
2192-
() -> startFunction("get_tables_by_type", ": db=" + dbname + " pat=" + pattern + ",type=" + tableType),
2193-
this, GetTableHandler.GetTableNamesRequest.fromDatabase(dbname, conf).byType(tableType, pattern),
2194-
t -> endFunction("get_tables_by_type", t.getLeft() != null, t.getRight()));
2195-
} catch (Exception e) {
2196-
throw handleException(e).defaultMetaException();
2197-
}
2163+
throws TException {
2164+
return GetTableHandler.getTables(
2165+
() -> startFunction("get_tables_by_type", ": db=" + dbname + " pat=" + pattern + ",type=" + tableType),
2166+
this, GetTableHandler.GetTableNamesRequest.fromDatabase(dbname, conf).byType(tableType, pattern),
2167+
t -> endFunction("get_tables_by_type", t.getLeft() != null, t.getRight()));
21982168
}
21992169

22002170
@Override
@@ -2235,14 +2205,10 @@ public List<String> get_materialized_views_for_rewriting(final String dbname)
22352205
}
22362206

22372207
@Override
2238-
public List<String> get_all_tables(final String dbname) throws MetaException {
2239-
try {
2240-
return GetTableHandler.getTables(() -> startFunction("get_all_tables", ": db=" + dbname),
2241-
this, GetTableHandler.GetTableNamesRequest.fromDatabase(dbname, conf),
2242-
t -> endFunction("get_all_tables", t.getLeft() != null, t.getRight()));
2243-
} catch (Exception e) {
2244-
throw handleException(e).defaultMetaException();
2245-
}
2208+
public List<String> get_all_tables(final String dbname) throws TException {
2209+
return GetTableHandler.getTables(() -> startFunction("get_all_tables", ": db=" + dbname),
2210+
this, GetTableHandler.GetTableNamesRequest.fromDatabase(dbname, conf),
2211+
t -> endFunction("get_all_tables", t.getLeft() != null, t.getRight()));
22462212
}
22472213

22482214
private List<FieldSchema> get_fields_with_environment_context_core(String db, String tableName, final EnvironmentContext envContext)

standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/handler/GetTableHandler.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.apache.commons.collections4.CollectionUtils;
3535
import org.apache.commons.lang3.tuple.Pair;
3636
import org.apache.hadoop.conf.Configuration;
37+
import org.apache.hadoop.hive.common.DatabaseName;
3738
import org.apache.hadoop.hive.common.StatsSetupConst;
3839
import org.apache.hadoop.hive.common.TableName;
3940
import org.apache.hadoop.hive.metastore.HMSHandler;
@@ -262,9 +263,19 @@ private Table getTableCore(GetTableRequest getTableRequest) throws MetaException
262263

263264
private List<Table> getTableObjects(GetTablesRequest req) throws TException {
264265
String catName = req.isSetCatName() ? req.getCatName() : getDefaultCatalog(conf);
265-
Database database = handler.get_database_core(catName, req.getDbName());
266-
if (isDatabaseRemote(database)) {
267-
return getRemoteTableObjectsInternal(req.getDbName(), req.getTblNames(), req.getTablesPattern());
266+
String dbName = req.getDbName();
267+
if (dbName == null || dbName.isEmpty()) {
268+
throw new UnknownDBException("DB name is null or empty");
269+
}
270+
try {
271+
Database database = handler.get_database_core(catName, dbName);
272+
if (isDatabaseRemote(database)) {
273+
return getRemoteTableObjectsInternal(database, req.getTblNames(), req.getTablesPattern());
274+
}
275+
} catch (NoSuchObjectException nse) {
276+
// The caller consumes UnknownDBException other than NoSuchObjectException
277+
// in case database doesn't exist
278+
throw new UnknownDBException("Could not find database " + DatabaseName.getQualified(catName, dbName));
268279
}
269280
return getTableObjectsInternal(req);
270281
}
@@ -279,26 +290,20 @@ private List<Table> filterTablesByName(List<Table> tables, List<String> tableNam
279290
return filteredTables;
280291
}
281292

282-
private List<Table> getRemoteTableObjectsInternal(String dbname, List<String> tableNames, String pattern) throws MetaException {
283-
String[] parsedDbName = parseDbName(dbname, conf);
293+
private List<Table> getRemoteTableObjectsInternal(Database db, List<String> tableNames, String pattern) throws MetaException {
284294
try {
285-
// retrieve tables from remote database
286-
Database db = handler.get_database_core(parsedDbName[CAT_NAME], parsedDbName[DB_NAME]);
287295
List<Table> tables = DataConnectorProviderFactory.getDataConnectorProvider(db).getTables(null);
288-
289296
// filtered out undesired tables
290297
if (tableNames != null) {
291298
tables = filterTablesByName(tables, tableNames);
292299
}
293-
294300
// set remote tables' local hive database reference
295301
for (Table table : tables) {
296-
table.setDbName(dbname);
302+
table.setDbName(db.getName());
297303
}
298-
299304
return FilterUtils.filterTablesIfEnabled(filterHook != null, filterHook, tables);
300305
} catch (Exception e) {
301-
LOG.warn("Unexpected exception while getting table(s) in remote database " + dbname , e);
306+
LOG.warn("Unexpected exception while getting table(s) in remote database " + db.getName() , e);
302307
if (isInTest) {
303308
// ignore the exception
304309
return new ArrayList<Table>();
@@ -327,9 +332,6 @@ private List<Table> getTableObjectsInternal(GetTablesRequest req)
327332
String catName = req.isSetCatName() ? req.getCatName() : getDefaultCatalog(conf);
328333
List<Table> tables = new ArrayList<>();
329334
int tableBatchSize = MetastoreConf.getIntVar(conf, MetastoreConf.ConfVars.BATCH_RETRIEVE_MAX);
330-
if (dbName == null || dbName.isEmpty()) {
331-
throw new UnknownDBException("DB name is null or empty");
332-
}
333335
List<String> tableNames = req.getTblNames();
334336
if(req.getTablesPattern() != null) {
335337
tables = ms.getTableObjectsByName(catName, dbName, tableNames, projectionsSpec, req.getTablesPattern());
@@ -458,13 +460,21 @@ private void fireReadDatabasePreEvent(final String name)
458460
private List<String> getTableNames(GetTableNamesRequest getNamesReq) throws TException {
459461
String catName = getNamesReq.catName;
460462
String dbname = getNamesReq.dbName;
463+
Database database = null;
461464
try {
462-
Database database = handler.get_database_core(catName, dbname);
463-
if (isDatabaseRemote(database)) {
465+
database = handler.get_database_core(catName, dbname);
466+
} catch (NoSuchObjectException nse) {
467+
throw new UnknownDBException("Could not find database " + DatabaseName.getQualified(catName, dbname));
468+
}
469+
if (isDatabaseRemote(database)) {
470+
try {
471+
handler.get_dataconnector_core(database.getConnector_name());
464472
return DataConnectorProviderFactory.getDataConnectorProvider(database).getTableNames();
473+
} catch (NoSuchObjectException nse) {
474+
// @TODO when the connector is dropped, we should drop the remote database as well
475+
// Return an empty list
476+
return new ArrayList<>();
465477
}
466-
} catch (Exception e) {
467-
throw newMetaException(e);
468478
}
469479

470480
List<String> names;

0 commit comments

Comments
 (0)