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

Signed-off-by: Wellington Chevreuil <wchevreuil@apache.org>
This commit is contained in:
Horváth Dóra 2021-10-18 11:06:32 +02:00 committed by GitHub
parent 1866992246
commit 6d0777a551
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 2221 additions and 1208 deletions

View File

@ -126,7 +126,8 @@ public class DemoClient {
TProtocol protocol = new TBinaryProtocol(transport, true, true); TProtocol protocol = new TBinaryProtocol(transport, true, true);
Hbase.Client client = new Hbase.Client(protocol); 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. // Scan all tables, look for the demo table and delete it.
System.out.println("scanning tables..."); System.out.println("scanning tables...");
@ -134,7 +135,7 @@ public class DemoClient {
for (ByteBuffer name : client.getTableNames()) { for (ByteBuffer name : client.getTableNames()) {
System.out.println(" found: " + ClientUtils.utf8(name.array())); 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)) { if (client.isTableEnabled(name)) {
System.out.println(" disabling table: " + ClientUtils.utf8(name.array())); System.out.println(" disabling table: " + ClientUtils.utf8(name.array()));
client.disableTable(name); client.disableTable(name);
@ -158,22 +159,35 @@ public class DemoClient {
col.timeToLive = Integer.MAX_VALUE; col.timeToLive = Integer.MAX_VALUE;
columns.add(col); columns.add(col);
System.out.println("creating table: " + ClientUtils.utf8(t)); System.out.println("creating table: " + ClientUtils.utf8(demoTable.array()));
try { try {
client.createTable(ByteBuffer.wrap(t), columns); client.createTable(demoTable, columns);
client.createTable(disabledTable, columns);
} catch (AlreadyExists ae) { } catch (AlreadyExists ae) {
System.out.println("WARN: " + ae.message); System.out.println("WARN: " + ae.message);
} }
System.out.println("column families in " + ClientUtils.utf8(t) + ": "); System.out.println("column families in " + ClientUtils.utf8(demoTable.array()) + ": ");
Map<ByteBuffer, ColumnDescriptor> columnMap = client.getColumnDescriptors(ByteBuffer.wrap(t)); Map<ByteBuffer, ColumnDescriptor> columnMap = client.getColumnDescriptors(demoTable);
for (ColumnDescriptor col2 : columnMap.values()) { for (ColumnDescriptor col2 : columnMap.values()) {
System.out.println(" column: " + ClientUtils.utf8(col2.name.array()) + ", maxVer: " System.out.println(" column: " + ClientUtils.utf8(col2.name.array()) + ", maxVer: "
+ col2.maxVersions); + 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; Map<ByteBuffer, ByteBuffer> dummyAttributes = null;
boolean writeToWal = false; boolean writeToWal = false;
@ -190,27 +204,27 @@ public class DemoClient {
mutations = new ArrayList<>(1); mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
ByteBuffer.wrap(invalid), writeToWal)); ByteBuffer.wrap(invalid), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("foo")), client.mutateRow(demoTable, ByteBuffer.wrap(bytes("foo")),
mutations, dummyAttributes); mutations, dummyAttributes);
// this row name is valid utf8 // this row name is valid utf8
mutations = new ArrayList<>(1); mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
ByteBuffer.wrap(valid), writeToWal)); 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 // non-utf8 is now allowed in row names because HBase stores values as binary
mutations = new ArrayList<>(1); mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
ByteBuffer.wrap(invalid), writeToWal)); 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 // Run a scanner on the rows we just created
ArrayList<ByteBuffer> columnNames = new ArrayList<>(); ArrayList<ByteBuffer> columnNames = new ArrayList<>();
columnNames.add(ByteBuffer.wrap(bytes("entry:"))); columnNames.add(ByteBuffer.wrap(bytes("entry:")));
System.out.println("Starting scanner..."); 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); dummyAttributes);
while (true) { while (true) {
@ -234,9 +248,9 @@ public class DemoClient {
mutations = new ArrayList<>(1); mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("unused:")), mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("unused:")),
ByteBuffer.wrap(bytes("DELETE_ME")), writeToWal)); ByteBuffer.wrap(bytes("DELETE_ME")), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes); client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes)); printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
client.deleteAllRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes); client.deleteAllRow(demoTable, ByteBuffer.wrap(row), dummyAttributes);
// sleep to force later timestamp // sleep to force later timestamp
try { try {
@ -250,8 +264,8 @@ public class DemoClient {
ByteBuffer.wrap(bytes("0")), writeToWal)); ByteBuffer.wrap(bytes("0")), writeToWal));
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
ByteBuffer.wrap(bytes("FOO")), writeToWal)); ByteBuffer.wrap(bytes("FOO")), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes); client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes)); printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
Mutation m; Mutation m;
mutations = new ArrayList<>(2); mutations = new ArrayList<>(2);
@ -263,16 +277,16 @@ public class DemoClient {
m.column = ByteBuffer.wrap(bytes("entry:num")); m.column = ByteBuffer.wrap(bytes("entry:num"));
m.value = ByteBuffer.wrap(bytes("-1")); m.value = ByteBuffer.wrap(bytes("-1"));
mutations.add(m); mutations.add(m);
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes); client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes)); printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
mutations = new ArrayList<>(); mutations = new ArrayList<>();
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")), mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")),
ByteBuffer.wrap(bytes(Integer.toString(i))), writeToWal)); ByteBuffer.wrap(bytes(Integer.toString(i))), writeToWal));
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:sqr")), mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:sqr")),
ByteBuffer.wrap(bytes(Integer.toString(i * i))), writeToWal)); ByteBuffer.wrap(bytes(Integer.toString(i * i))), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes); client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes)); printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
// sleep to force later timestamp // sleep to force later timestamp
try { try {
@ -289,11 +303,11 @@ public class DemoClient {
m = new Mutation(); m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:sqr")); m.column = ByteBuffer.wrap(bytes("entry:sqr"));
m.isDelete = true; 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 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); ByteBuffer.wrap(bytes("entry:num")), 10, dummyAttributes);
printVersions(ByteBuffer.wrap(row), versions); printVersions(ByteBuffer.wrap(row), versions);
@ -302,7 +316,7 @@ public class DemoClient {
System.exit(-1); 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); ByteBuffer.wrap(bytes("entry:foo")), dummyAttributes);
if (!result.isEmpty()) { if (!result.isEmpty()) {
@ -316,7 +330,7 @@ public class DemoClient {
// scan all rows/columnNames // scan all rows/columnNames
columnNames.clear(); 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("column with name: " + new String(col2.name.array()));
System.out.println(col2.toString()); System.out.println(col2.toString());
@ -324,7 +338,7 @@ public class DemoClient {
} }
System.out.println("Starting scanner..."); 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); ByteBuffer.wrap(bytes("00040")), columnNames, dummyAttributes);
while (true) { while (true) {

View File

@ -28,6 +28,7 @@ import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
@ -202,6 +203,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 // ThriftServerRunner.compact should be deprecated and replaced with methods specific to
// table and region. // table and region.
@Override @Override

View File

@ -245,6 +245,14 @@ service Hbase {
list<Text> getTableNames() list<Text> getTableNames()
throws (1:IOError io) 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. * 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.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.CompatibilityFactory; import org.apache.hadoop.hbase.CompatibilityFactory;
@ -738,6 +739,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 @Test
public void testMetricsWithException() throws Exception { public void testMetricsWithException() throws Exception {
String rowkey = "row1"; String rowkey = "row1";