diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/TableDescriptors.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/TableDescriptors.java index 5787f66203c..2537e7f83e7 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/TableDescriptors.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/TableDescriptors.java @@ -48,11 +48,12 @@ public interface TableDescriptors { /** * Get Map of all TableDescriptors. Populates the descriptor cache as a * side effect. + * Notice: the key of map is the table name which contains namespace. It was generated by + * {@link TableName#getNameWithNamespaceInclAsString()}. * @return Map of all descriptors. * @throws IOException */ - Map getAll() - throws IOException; + Map getAll() throws IOException; /** * Add or update descriptor diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/TableStateManager.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/TableStateManager.java index 580e726b873..1eb04169208 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/TableStateManager.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/TableStateManager.java @@ -271,7 +271,6 @@ public class TableStateManager { private void fixTableStates(TableDescriptors tableDescriptors, Connection connection) throws IOException { - Map allDescriptors = tableDescriptors.getAll(); Map states = new HashMap<>(); // NOTE: Full hbase:meta table scan! MetaTableAccessor.fullScanTables(connection, new MetaTableAccessor.Visitor() { @@ -282,15 +281,15 @@ public class TableStateManager { return true; } }); - for (Map.Entry entry : allDescriptors.entrySet()) { - TableName tableName = TableName.valueOf(entry.getKey()); + for (TableDescriptor tableDesc : tableDescriptors.getAll().values()) { + TableName tableName = tableDesc.getTableName(); if (TableName.isMetaTableName(tableName)) { // This table is always enabled. No fixup needed. No entry in hbase:meta needed. // Call through to fixTableState though in case a super class wants to do something. fixTableState(new TableState(tableName, TableState.State.ENABLED)); continue; } - TableState tableState = states.get(entry.getKey()); + TableState tableState = states.get(tableName.getNameAsString()); if (tableState == null) { LOG.warn(tableName + " has no table state in hbase:meta, assuming ENABLED"); MetaTableAccessor.updateTableState(connection, tableName, TableState.State.ENABLED); diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/FSTableDescriptors.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/FSTableDescriptors.java index 3dce0de4afc..8ae5b4b8ddd 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/FSTableDescriptors.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/FSTableDescriptors.java @@ -276,7 +276,7 @@ public class FSTableDescriptors implements TableDescriptors { if (fsvisited && usecache) { for (Map.Entry entry: this.cache.entrySet()) { - tds.put(entry.getKey().toString(), entry.getValue()); + tds.put(entry.getKey().getNameWithNamespaceInclAsString(), entry.getValue()); } // add hbase:meta to the response tds.put(this.metaTableDescriptor.getTableName().getNameAsString(), metaTableDescriptor); @@ -295,7 +295,7 @@ public class FSTableDescriptors implements TableDescriptors { allvisited = false; continue; } else { - tds.put(htd.getTableName().getNameAsString(), htd); + tds.put(htd.getTableName().getNameWithNamespaceInclAsString(), htd); } fsvisited = allvisited; } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestFSTableDescriptors.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestFSTableDescriptors.java index 11c7bcd005f..a0c37f27d4e 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestFSTableDescriptors.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestFSTableDescriptors.java @@ -306,7 +306,33 @@ public class TestFSTableDescriptors { assertEquals("getAll() didn't return all TableDescriptors, expected: " + (count + 1) + " got: " + htds.getAll().size(), count + 1, htds.getAll().size()); + } + @Test + public void testGetAllOrdering() throws Exception { + final String name = "testGetAllOrdering"; + FileSystem fs = FileSystem.get(UTIL.getConfiguration()); + Path rootDir = new Path(UTIL.getDataTestDir(), name); + FSTableDescriptors tds = new FSTableDescriptorsTest(UTIL.getConfiguration(), fs, rootDir); + + String[] tableNames = new String[] { "foo", "bar", "foo:bar", "bar:foo" }; + for (String tableName : tableNames) { + tds.createTableDescriptor( + TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName)).build()); + } + + Map tables = tds.getAll(); + assertEquals(4, tables.size()); + + String[] tableNamesOrdered = + new String[] { "bar:foo", "default:bar", "default:foo", "foo:bar" }; + int i = 0; + for (Map.Entry entry : tables.entrySet()) { + assertEquals(tableNamesOrdered[i], entry.getKey()); + assertEquals(tableNamesOrdered[i], + entry.getValue().getTableName().getNameWithNamespaceInclAsString()); + i++; + } } @Test