Skip to content

Commit 186679f

Browse files
authored
HBASE-27444 Add tool commands list_enabled_tables and list_disabled_tables (#4849)
Signed-off-by: Duo Zhang <zhangduo@apache.org>
1 parent 1bebf86 commit 186679f

File tree

18 files changed

+460
-1
lines changed

18 files changed

+460
-1
lines changed

hbase-client/src/main/java/org/apache/hadoop/hbase/client/Admin.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ default List<TableDescriptor> listTableDescriptors(Pattern pattern) throws IOExc
158158
List<TableDescriptor> listTableDescriptors(Pattern pattern, boolean includeSysTables)
159159
throws IOException;
160160

161+
/**
162+
* List all enabled or disabled tables
163+
* @param isEnabled is true means return enabled tables, false means return disabled tables
164+
* @return a list of enabled or disabled tables
165+
*/
166+
List<TableDescriptor> listTableDescriptorsByState(boolean isEnabled) throws IOException;
167+
161168
/**
162169
* List all of the names of userspace tables.
163170
* @return TableName[] table names
@@ -184,6 +191,14 @@ default TableName[] listTableNames(Pattern pattern) throws IOException {
184191
*/
185192
TableName[] listTableNames(Pattern pattern, boolean includeSysTables) throws IOException;
186193

194+
/**
195+
* List all enabled or disabled table names
196+
* @param isEnabled is true means return enabled table names, false means return disabled table
197+
* names
198+
* @return a list of enabled or disabled table names
199+
*/
200+
List<TableName> listTableNamesByState(boolean isEnabled) throws IOException;
201+
187202
/**
188203
* Get a table descriptor.
189204
* @param tableName as a {@link TableName}

hbase-client/src/main/java/org/apache/hadoop/hbase/client/AdminOverAsyncAdmin.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ public List<TableDescriptor> listTableDescriptors(Pattern pattern, boolean inclu
146146
return get(admin.listTableDescriptors(pattern, includeSysTables));
147147
}
148148

149+
@Override
150+
public List<TableDescriptor> listTableDescriptorsByState(boolean isEnabled) throws IOException {
151+
return get(admin.listTableDescriptorsByState(isEnabled));
152+
}
153+
149154
@Override
150155
public TableName[] listTableNames() throws IOException {
151156
return get(admin.listTableNames()).toArray(new TableName[0]);
@@ -156,6 +161,11 @@ public TableName[] listTableNames(Pattern pattern, boolean includeSysTables) thr
156161
return get(admin.listTableNames(pattern, includeSysTables)).toArray(new TableName[0]);
157162
}
158163

164+
@Override
165+
public List<TableName> listTableNamesByState(boolean isEnabled) throws IOException {
166+
return get(admin.listTableNamesByState(isEnabled));
167+
}
168+
159169
@Override
160170
public TableDescriptor getDescriptor(TableName tableName)
161171
throws TableNotFoundException, IOException {

hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ CompletableFuture<List<TableDescriptor>> listTableDescriptors(Pattern pattern,
111111
*/
112112
CompletableFuture<List<TableDescriptor>> listTableDescriptorsByNamespace(String name);
113113

114+
/**
115+
* List all enabled or disabled table descriptors
116+
* @param isEnabled is true means return enabled table descriptors, false means return disabled
117+
* table descriptors
118+
* @return a list of table names wrapped by a {@link CompletableFuture}.
119+
*/
120+
CompletableFuture<List<TableDescriptor>> listTableDescriptorsByState(boolean isEnabled);
121+
114122
/**
115123
* List all of the names of userspace tables.
116124
* @return a list of table names wrapped by a {@link CompletableFuture}.
@@ -135,6 +143,14 @@ default CompletableFuture<List<TableName>> listTableNames() {
135143
*/
136144
CompletableFuture<List<TableName>> listTableNames(Pattern pattern, boolean includeSysTables);
137145

146+
/**
147+
* List all enabled or disabled table names
148+
* @param isEnabled is true means return enabled table names, false means return disabled table
149+
* names
150+
* @return a list of table names wrapped by a {@link CompletableFuture}.
151+
*/
152+
CompletableFuture<List<TableName>> listTableNamesByState(boolean isEnabled);
153+
138154
/**
139155
* Get list of table names by namespace.
140156
* @param name namespace name

hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncHBaseAdmin.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ public CompletableFuture<List<TableDescriptor>> listTableDescriptorsByNamespace(
104104
return wrap(rawAdmin.listTableDescriptorsByNamespace(name));
105105
}
106106

107+
@Override
108+
public CompletableFuture<List<TableDescriptor>> listTableDescriptorsByState(boolean isEnabled) {
109+
return wrap(rawAdmin.listTableDescriptorsByState(isEnabled));
110+
}
111+
107112
@Override
108113
public CompletableFuture<List<TableName>> listTableNames(boolean includeSysTables) {
109114
return wrap(rawAdmin.listTableNames(includeSysTables));
@@ -115,6 +120,11 @@ public CompletableFuture<List<TableName>> listTableNames(Pattern pattern,
115120
return wrap(rawAdmin.listTableNames(pattern, includeSysTables));
116121
}
117122

123+
@Override
124+
public CompletableFuture<List<TableName>> listTableNamesByState(boolean isEnabled) {
125+
return wrap(rawAdmin.listTableNamesByState(isEnabled));
126+
}
127+
118128
@Override
119129
public CompletableFuture<List<TableName>> listTableNamesByNamespace(String name) {
120130
return wrap(rawAdmin.listTableNamesByNamespace(name));

hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,12 @@
223223
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListNamespacesResponse;
224224
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByNamespaceRequest;
225225
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByNamespaceResponse;
226+
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByStateRequest;
227+
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByStateResponse;
226228
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableNamesByNamespaceRequest;
227229
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableNamesByNamespaceResponse;
230+
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableNamesByStateRequest;
231+
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableNamesByStateResponse;
228232
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MajorCompactionTimestampForRegionRequest;
229233
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MajorCompactionTimestampRequest;
230234
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MajorCompactionTimestampResponse;
@@ -563,6 +567,17 @@ public CompletableFuture<List<TableName>> listTableNames(Pattern pattern,
563567
return getTableNames(RequestConverter.buildGetTableNamesRequest(pattern, includeSysTables));
564568
}
565569

570+
@Override
571+
public CompletableFuture<List<TableName>> listTableNamesByState(boolean isEnabled) {
572+
return this.<List<TableName>> newMasterCaller()
573+
.action((controller, stub) -> this.<ListTableNamesByStateRequest,
574+
ListTableNamesByStateResponse, List<TableName>> call(controller, stub,
575+
ListTableNamesByStateRequest.newBuilder().setIsEnabled(isEnabled).build(),
576+
(s, c, req, done) -> s.listTableNamesByState(c, req, done),
577+
(resp) -> ProtobufUtil.toTableNameList(resp.getTableNamesList())))
578+
.call();
579+
}
580+
566581
private CompletableFuture<List<TableName>> getTableNames(GetTableNamesRequest request) {
567582
return this.<List<TableName>> newMasterCaller()
568583
.action((controller, stub) -> this.<GetTableNamesRequest, GetTableNamesResponse,
@@ -583,6 +598,17 @@ ListTableDescriptorsByNamespaceResponse, List<TableDescriptor>> call(controller,
583598
.call();
584599
}
585600

601+
@Override
602+
public CompletableFuture<List<TableDescriptor>> listTableDescriptorsByState(boolean isEnabled) {
603+
return this.<List<TableDescriptor>> newMasterCaller()
604+
.action((controller, stub) -> this.<ListTableDescriptorsByStateRequest,
605+
ListTableDescriptorsByStateResponse, List<TableDescriptor>> call(controller, stub,
606+
ListTableDescriptorsByStateRequest.newBuilder().setIsEnabled(isEnabled).build(),
607+
(s, c, req, done) -> s.listTableDescriptorsByState(c, req, done),
608+
(resp) -> ProtobufUtil.toTableDescriptorList(resp)))
609+
.call();
610+
}
611+
586612
@Override
587613
public CompletableFuture<List<TableName>> listTableNamesByNamespace(String name) {
588614
return this.<List<TableName>> newMasterCaller()

hbase-client/src/main/java/org/apache/hadoop/hbase/shaded/protobuf/ProtobufUtil.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.GetTableDescriptorsResponse;
199199
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListNamespaceDescriptorsResponse;
200200
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByNamespaceResponse;
201+
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByStateResponse;
201202
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MajorCompactionTimestampResponse;
202203
import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos;
203204
import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos;
@@ -506,6 +507,18 @@ public static List<TableDescriptor> toTableDescriptorList(GetTableDescriptorsRes
506507
.collect(Collectors.toList());
507508
}
508509

510+
/**
511+
* Get a list of TableDescriptor from ListTableDescriptorsByNamespaceResponse protobuf
512+
* @param proto the ListTableDescriptorsByNamespaceResponse
513+
* @return a list of TableDescriptor
514+
*/
515+
public static List<TableDescriptor>
516+
toTableDescriptorList(ListTableDescriptorsByStateResponse proto) {
517+
if (proto == null) return new ArrayList<>();
518+
return proto.getTableSchemaList().stream().map(ProtobufUtil::toTableDescriptor)
519+
.collect(Collectors.toList());
520+
}
521+
509522
/**
510523
* get the split keys in form "byte [][]" from a CreateTableRequest proto
511524
* @param proto the CreateTableRequest

hbase-protocol-shaded/src/main/protobuf/server/master/Master.proto

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,14 @@ message GetTableDescriptorsResponse {
512512
repeated TableSchema table_schema = 1;
513513
}
514514

515+
message ListTableDescriptorsByStateRequest {
516+
required bool is_enabled = 1;
517+
}
518+
519+
message ListTableDescriptorsByStateResponse {
520+
repeated TableSchema table_schema = 1;
521+
}
522+
515523
message GetTableNamesRequest {
516524
optional string regex = 1;
517525
optional bool include_sys_tables = 2 [default=false];
@@ -522,6 +530,14 @@ message GetTableNamesResponse {
522530
repeated TableName table_names = 1;
523531
}
524532

533+
message ListTableNamesByStateRequest {
534+
required bool is_enabled = 1;
535+
}
536+
537+
message ListTableNamesByStateResponse {
538+
repeated TableName table_names = 1;
539+
}
540+
525541
message GetTableStateRequest {
526542
required TableName table_name = 1;
527543
}
@@ -770,10 +786,18 @@ service MasterService {
770786
rpc GetTableDescriptors(GetTableDescriptorsRequest)
771787
returns(GetTableDescriptorsResponse);
772788

789+
/** List all enabled or disabled table descriptors. */
790+
rpc ListTableDescriptorsByState(ListTableDescriptorsByStateRequest)
791+
returns(ListTableDescriptorsByStateResponse);
792+
773793
/** Get the list of table names. */
774794
rpc GetTableNames(GetTableNamesRequest)
775795
returns(GetTableNamesResponse);
776796

797+
/** List all enabled or disabled table names. */
798+
rpc ListTableNamesByState(ListTableNamesByStateRequest)
799+
returns(ListTableNamesByStateResponse);
800+
777801
/** Return cluster status. */
778802
rpc GetClusterStatus(GetClusterStatusRequest)
779803
returns(GetClusterStatusResponse);

hbase-server/src/main/java/org/apache/hadoop/hbase/master/MasterRpcServices.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,12 @@
283283
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListNamespacesResponse;
284284
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByNamespaceRequest;
285285
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByNamespaceResponse;
286+
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByStateRequest;
287+
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByStateResponse;
286288
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableNamesByNamespaceRequest;
287289
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableNamesByNamespaceResponse;
290+
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableNamesByStateRequest;
291+
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableNamesByStateResponse;
288292
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MajorCompactionTimestampForRegionRequest;
289293
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MajorCompactionTimestampRequest;
290294
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MajorCompactionTimestampResponse;
@@ -1173,6 +1177,31 @@ public GetTableDescriptorsResponse getTableDescriptors(RpcController c,
11731177
}
11741178
}
11751179

1180+
@Override
1181+
public ListTableDescriptorsByStateResponse listTableDescriptorsByState(RpcController controller,
1182+
ListTableDescriptorsByStateRequest request) throws ServiceException {
1183+
try {
1184+
server.checkInitialized();
1185+
List<TableDescriptor> descriptors = server.listTableDescriptors(null, null, null, false);
1186+
1187+
ListTableDescriptorsByStateResponse.Builder builder =
1188+
ListTableDescriptorsByStateResponse.newBuilder();
1189+
if (descriptors != null && descriptors.size() > 0) {
1190+
// Add the table descriptors to the response
1191+
TableState.State state =
1192+
request.getIsEnabled() ? TableState.State.ENABLED : TableState.State.DISABLED;
1193+
for (TableDescriptor htd : descriptors) {
1194+
if (server.getTableStateManager().isTableState(htd.getTableName(), state)) {
1195+
builder.addTableSchema(ProtobufUtil.toTableSchema(htd));
1196+
}
1197+
}
1198+
}
1199+
return builder.build();
1200+
} catch (IOException ioe) {
1201+
throw new ServiceException(ioe);
1202+
}
1203+
}
1204+
11761205
/**
11771206
* Get list of userspace table names
11781207
* @param controller Unused (set to null).
@@ -1202,6 +1231,29 @@ public GetTableNamesResponse getTableNames(RpcController controller, GetTableNam
12021231
}
12031232
}
12041233

1234+
@Override
1235+
public ListTableNamesByStateResponse listTableNamesByState(RpcController controller,
1236+
ListTableNamesByStateRequest request) throws ServiceException {
1237+
try {
1238+
server.checkServiceStarted();
1239+
List<TableName> tableNames = server.listTableNames(null, null, false);
1240+
ListTableNamesByStateResponse.Builder builder = ListTableNamesByStateResponse.newBuilder();
1241+
if (tableNames != null && tableNames.size() > 0) {
1242+
// Add the disabled table names to the response
1243+
TableState.State state =
1244+
request.getIsEnabled() ? TableState.State.ENABLED : TableState.State.DISABLED;
1245+
for (TableName table : tableNames) {
1246+
if (server.getTableStateManager().isTableState(table, state)) {
1247+
builder.addTableNames(ProtobufUtil.toProtoTableName(table));
1248+
}
1249+
}
1250+
}
1251+
return builder.build();
1252+
} catch (IOException e) {
1253+
throw new ServiceException(e);
1254+
}
1255+
}
1256+
12051257
@Override
12061258
public GetTableStateResponse getTableState(RpcController controller, GetTableStateRequest request)
12071259
throws ServiceException {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.master;
19+
20+
import org.apache.hadoop.hbase.HBaseClassTestRule;
21+
import org.apache.hadoop.hbase.HBaseTestingUtil;
22+
import org.apache.hadoop.hbase.TableName;
23+
import org.apache.hadoop.hbase.client.Admin;
24+
import org.apache.hadoop.hbase.client.TableDescriptor;
25+
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
26+
import org.apache.hadoop.hbase.testclassification.MasterTests;
27+
import org.apache.hadoop.hbase.testclassification.MediumTests;
28+
import org.junit.AfterClass;
29+
import org.junit.Assert;
30+
import org.junit.BeforeClass;
31+
import org.junit.ClassRule;
32+
import org.junit.Test;
33+
import org.junit.experimental.categories.Category;
34+
35+
@Category({ MasterTests.class, MediumTests.class })
36+
public class TestListTablesByState {
37+
@ClassRule
38+
public static final HBaseClassTestRule CLASS_RULE =
39+
HBaseClassTestRule.forClass(TestListTablesByState.class);
40+
41+
private static HBaseTestingUtil UTIL;
42+
private static Admin ADMIN;
43+
private final static int SLAVES = 1;
44+
45+
@BeforeClass
46+
public static void setUpBeforeClass() throws Exception {
47+
UTIL = new HBaseTestingUtil();
48+
UTIL.startMiniCluster(SLAVES);
49+
ADMIN = UTIL.getAdmin();
50+
}
51+
52+
@Test
53+
public void testListTableNamesByState() throws Exception {
54+
TableName testTableName = TableName.valueOf("test");
55+
TableDescriptor testTableDesc = TableDescriptorBuilder.newBuilder(testTableName).build();
56+
ADMIN.createTable(testTableDesc);
57+
ADMIN.disableTable(testTableName);
58+
Assert.assertEquals(ADMIN.listTableNamesByState(false).get(0), testTableName);
59+
ADMIN.enableTable(testTableName);
60+
Assert.assertEquals(ADMIN.listTableNamesByState(true).get(0), testTableName);
61+
}
62+
63+
@Test
64+
public void testListTableDescriptorByState() throws Exception {
65+
TableName testTableName = TableName.valueOf("test");
66+
TableDescriptor testTableDesc = TableDescriptorBuilder.newBuilder(testTableName).build();
67+
ADMIN.createTable(testTableDesc);
68+
ADMIN.disableTable(testTableName);
69+
Assert.assertEquals(ADMIN.listTableDescriptorsByState(false).get(0), testTableDesc);
70+
ADMIN.enableTable(testTableName);
71+
Assert.assertEquals(ADMIN.listTableDescriptorsByState(true).get(0), testTableDesc);
72+
}
73+
74+
@AfterClass
75+
public static void tearDownAfterClass() throws Exception {
76+
if (ADMIN != null) {
77+
ADMIN.close();
78+
}
79+
if (UTIL != null) {
80+
UTIL.shutdownMiniCluster();
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)