HBASE-23049 TableDescriptors#getAll should return the tables ordering by the name which contain namespace (#639)

Signed-off-by: stack <stack@apache.org>
This commit is contained in:
Guanghao Zhang 2019-09-19 21:20:42 +08:00 committed by GitHub
parent 1655360951
commit 343a6a7913
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 8 deletions

View File

@ -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<String, TableDescriptor> getAll()
throws IOException;
Map<String, TableDescriptor> getAll() throws IOException;
/**
* Add or update descriptor

View File

@ -271,7 +271,6 @@ public class TableStateManager {
private void fixTableStates(TableDescriptors tableDescriptors, Connection connection)
throws IOException {
Map<String, TableDescriptor> allDescriptors = tableDescriptors.getAll();
Map<String, TableState> 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<String, TableDescriptor> 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);

View File

@ -276,7 +276,7 @@ public class FSTableDescriptors implements TableDescriptors {
if (fsvisited && usecache) {
for (Map.Entry<TableName, TableDescriptor> 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;
}

View File

@ -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<String, TableDescriptor> 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<String, TableDescriptor> entry : tables.entrySet()) {
assertEquals(tableNamesOrdered[i], entry.getKey());
assertEquals(tableNamesOrdered[i],
entry.getValue().getTableName().getNameWithNamespaceInclAsString());
i++;
}
}
@Test