HBASE-26284 Add HBase Thrift API to get all table names along with wh… (#3693)

Signed-off-by: Norbert Kalmar <nkalmar@cloudera.com>
Signed-off-by: Wellington Chevreuil <wchevreuil@apache.org>
This commit is contained in:
Horváth Dóra 2021-10-11 14:04:03 +02:00 committed by GitHub
parent 82ccd33186
commit ce44e16939
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 2221 additions and 1208 deletions

View File

@ -123,7 +123,8 @@ public class DemoClient {
TProtocol protocol = new TBinaryProtocol(transport, true, true);
Hbase.Client client = new Hbase.Client(protocol);
byte[] t = bytes("demo_table");
ByteBuffer demoTable = ByteBuffer.wrap(bytes("demo_table"));
ByteBuffer disabledTable = ByteBuffer.wrap(bytes("disabled_table"));
// Scan all tables, look for the demo table and delete it.
System.out.println("scanning tables...");
@ -131,7 +132,7 @@ public class DemoClient {
for (ByteBuffer name : client.getTableNames()) {
System.out.println(" found: " + ClientUtils.utf8(name.array()));
if (ClientUtils.utf8(name.array()).equals(ClientUtils.utf8(t))) {
if (name.equals(demoTable) || name.equals(disabledTable)) {
if (client.isTableEnabled(name)) {
System.out.println(" disabling table: " + ClientUtils.utf8(name.array()));
client.disableTable(name);
@ -155,22 +156,35 @@ public class DemoClient {
col.timeToLive = Integer.MAX_VALUE;
columns.add(col);
System.out.println("creating table: " + ClientUtils.utf8(t));
System.out.println("creating table: " + ClientUtils.utf8(demoTable.array()));
try {
client.createTable(ByteBuffer.wrap(t), columns);
client.createTable(demoTable, columns);
client.createTable(disabledTable, columns);
} catch (AlreadyExists ae) {
System.out.println("WARN: " + ae.message);
}
System.out.println("column families in " + ClientUtils.utf8(t) + ": ");
Map<ByteBuffer, ColumnDescriptor> columnMap = client.getColumnDescriptors(ByteBuffer.wrap(t));
System.out.println("column families in " + ClientUtils.utf8(demoTable.array()) + ": ");
Map<ByteBuffer, ColumnDescriptor> columnMap = client.getColumnDescriptors(demoTable);
for (ColumnDescriptor col2 : columnMap.values()) {
System.out.println(" column: " + ClientUtils.utf8(col2.name.array()) + ", maxVer: "
+ col2.maxVersions);
}
if (client.isTableEnabled(disabledTable)){
System.out.println("disabling table: " + ClientUtils.utf8(disabledTable.array()));
client.disableTable(disabledTable);
}
System.out.println("list tables with enabled statuses : ");
Map<ByteBuffer, Boolean> statusMap = client.getTableNamesWithIsTableEnabled();
for (Map.Entry<ByteBuffer, Boolean> entry : statusMap.entrySet()) {
System.out.println(" Table: " + ClientUtils.utf8(entry.getKey().array()) +
", is enabled: " + entry.getValue());
}
Map<ByteBuffer, ByteBuffer> dummyAttributes = null;
boolean writeToWal = false;
@ -187,27 +201,27 @@ public class DemoClient {
mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
ByteBuffer.wrap(invalid), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("foo")),
client.mutateRow(demoTable, ByteBuffer.wrap(bytes("foo")),
mutations, dummyAttributes);
// this row name is valid utf8
mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
ByteBuffer.wrap(valid), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(valid), mutations, dummyAttributes);
client.mutateRow(demoTable, ByteBuffer.wrap(valid), mutations, dummyAttributes);
// non-utf8 is now allowed in row names because HBase stores values as binary
mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
ByteBuffer.wrap(invalid), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(invalid), mutations, dummyAttributes);
client.mutateRow(demoTable, ByteBuffer.wrap(invalid), mutations, dummyAttributes);
// Run a scanner on the rows we just created
ArrayList<ByteBuffer> columnNames = new ArrayList<>();
columnNames.add(ByteBuffer.wrap(bytes("entry:")));
System.out.println("Starting scanner...");
int scanner = client.scannerOpen(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("")), columnNames,
int scanner = client.scannerOpen(demoTable, ByteBuffer.wrap(bytes("")), columnNames,
dummyAttributes);
while (true) {
@ -231,9 +245,9 @@ public class DemoClient {
mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("unused:")),
ByteBuffer.wrap(bytes("DELETE_ME")), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
client.deleteAllRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes);
client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
client.deleteAllRow(demoTable, ByteBuffer.wrap(row), dummyAttributes);
// sleep to force later timestamp
try {
@ -247,8 +261,8 @@ public class DemoClient {
ByteBuffer.wrap(bytes("0")), writeToWal));
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
ByteBuffer.wrap(bytes("FOO")), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
Mutation m;
mutations = new ArrayList<>(2);
@ -260,16 +274,16 @@ public class DemoClient {
m.column = ByteBuffer.wrap(bytes("entry:num"));
m.value = ByteBuffer.wrap(bytes("-1"));
mutations.add(m);
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
mutations = new ArrayList<>();
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")),
ByteBuffer.wrap(bytes(Integer.toString(i))), writeToWal));
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:sqr")),
ByteBuffer.wrap(bytes(Integer.toString(i * i))), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
// sleep to force later timestamp
try {
@ -286,11 +300,11 @@ public class DemoClient {
m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:sqr"));
m.isDelete = true;
client.mutateRowTs(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, 1,
client.mutateRowTs(demoTable, ByteBuffer.wrap(row), mutations, 1,
dummyAttributes); // shouldn't override latest
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
List<TCell> versions = client.getVer(ByteBuffer.wrap(t), ByteBuffer.wrap(row),
List<TCell> versions = client.getVer(demoTable, ByteBuffer.wrap(row),
ByteBuffer.wrap(bytes("entry:num")), 10, dummyAttributes);
printVersions(ByteBuffer.wrap(row), versions);
@ -299,7 +313,7 @@ public class DemoClient {
System.exit(-1);
}
List<TCell> result = client.get(ByteBuffer.wrap(t), ByteBuffer.wrap(row),
List<TCell> result = client.get(demoTable, ByteBuffer.wrap(row),
ByteBuffer.wrap(bytes("entry:foo")), dummyAttributes);
if (!result.isEmpty()) {
@ -313,7 +327,7 @@ public class DemoClient {
// scan all rows/columnNames
columnNames.clear();
for (ColumnDescriptor col2 : client.getColumnDescriptors(ByteBuffer.wrap(t)).values()) {
for (ColumnDescriptor col2 : client.getColumnDescriptors(demoTable).values()) {
System.out.println("column with name: " + new String(col2.name.array()));
System.out.println(col2.toString());
@ -321,7 +335,7 @@ public class DemoClient {
}
System.out.println("Starting scanner...");
scanner = client.scannerOpenWithStop(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("00020")),
scanner = client.scannerOpenWithStop(demoTable, ByteBuffer.wrap(bytes("00020")),
ByteBuffer.wrap(bytes("00040")), columnNames, dummyAttributes);
while (true) {

View File

@ -28,6 +28,7 @@ import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@ -203,6 +204,20 @@ public class ThriftHBaseServiceHandler extends HBaseServiceHandler implements Hb
}
}
@Override
public Map<ByteBuffer, Boolean> getTableNamesWithIsTableEnabled() throws IOError {
try {
HashMap<ByteBuffer, Boolean> tables = new HashMap<>();
for (ByteBuffer tableName: this.getTableNames()) {
tables.put(tableName, this.isTableEnabled(tableName));
}
return tables;
} catch (IOError e) {
LOG.warn(e.getMessage(), e);
throw getIOError(e);
}
}
// ThriftServerRunner.compact should be deprecated and replaced with methods specific to
// table and region.
@Override

View File

@ -261,6 +261,14 @@ service Hbase {
list<Text> getTableNames()
throws (1:IOError io)
/**
* List all the userspace tables and their enabled or disabled flags.
*
* @return list of tables with is enabled flags
*/
map<Text,bool> getTableNamesWithIsTableEnabled()
throws (1:IOError io)
/**
* List all the column families assoicated with a table.
*

View File

@ -32,6 +32,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.CompatibilityFactory;
@ -725,6 +726,35 @@ public class TestThriftServer {
}
}
@Test
public void testGetTableNamesWithStatus() throws Exception{
ThriftHBaseServiceHandler handler =
new ThriftHBaseServiceHandler(UTIL.getConfiguration(),
UserProvider.instantiate(UTIL.getConfiguration()));
createTestTables(handler);
assertEquals(2, handler.getTableNamesWithIsTableEnabled().size());
assertEquals(2, countTablesByStatus(true, handler));
handler.disableTable(tableBname);
assertEquals(1, countTablesByStatus(true, handler));
assertEquals(1, countTablesByStatus(false, handler));
assertEquals(2, handler.getTableNamesWithIsTableEnabled().size());
handler.enableTable(tableBname);
assertEquals(2, countTablesByStatus(true, handler));
dropTestTables(handler);
}
private static int countTablesByStatus(Boolean isEnabled, Hbase.Iface handler) throws Exception {
AtomicInteger counter = new AtomicInteger(0);
handler.getTableNamesWithIsTableEnabled().forEach(
(table, tableStatus) -> {
if (tableStatus.equals(isEnabled)) counter.getAndIncrement();
});
return counter.get();
}
@Test
public void testMetricsWithException() throws Exception {
String rowkey = "row1";