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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.hadoop
.hbase
.client
;
20 import static org
.apache
.hadoop
.hbase
.TableName
.META_TABLE_NAME
;
21 import static org
.apache
.hadoop
.hbase
.regionserver
.storefiletracker
.StoreFileTrackerFactory
.TRACKER_IMPL
;
22 import static org
.hamcrest
.CoreMatchers
.instanceOf
;
23 import static org
.hamcrest
.MatcherAssert
.assertThat
;
24 import static org
.junit
.Assert
.assertEquals
;
25 import static org
.junit
.Assert
.assertFalse
;
26 import static org
.junit
.Assert
.assertTrue
;
27 import static org
.junit
.Assert
.fail
;
29 import java
.util
.ArrayList
;
30 import java
.util
.Collections
;
31 import java
.util
.List
;
32 import java
.util
.concurrent
.ExecutionException
;
33 import java
.util
.regex
.Pattern
;
34 import org
.apache
.hadoop
.hbase
.ClientMetaTableAccessor
;
35 import org
.apache
.hadoop
.hbase
.DoNotRetryIOException
;
36 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
37 import org
.apache
.hadoop
.hbase
.HRegionLocation
;
38 import org
.apache
.hadoop
.hbase
.TableName
;
39 import org
.apache
.hadoop
.hbase
.regionserver
.storefiletracker
.StoreFileTrackerFactory
;
40 import org
.apache
.hadoop
.hbase
.testclassification
.ClientTests
;
41 import org
.apache
.hadoop
.hbase
.testclassification
.LargeTests
;
42 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
43 import org
.junit
.ClassRule
;
44 import org
.junit
.Test
;
45 import org
.junit
.experimental
.categories
.Category
;
46 import org
.junit
.runner
.RunWith
;
47 import org
.junit
.runners
.Parameterized
;
50 * Class to test asynchronous table admin operations.
51 * @see TestAsyncTableAdminApi2 This test and it used to be joined it was taking longer than our
52 * ten minute timeout so they were split.
54 @RunWith(Parameterized
.class)
55 @Category({ LargeTests
.class, ClientTests
.class })
56 public class TestAsyncTableAdminApi3
extends TestAsyncAdminBase
{
58 public static final HBaseClassTestRule CLASS_RULE
=
59 HBaseClassTestRule
.forClass(TestAsyncTableAdminApi3
.class);
62 public void testTableExist() throws Exception
{
64 exist
= admin
.tableExists(tableName
).get();
66 TEST_UTIL
.createTable(tableName
, FAMILY
);
67 exist
= admin
.tableExists(tableName
).get();
69 exist
= admin
.tableExists(TableName
.META_TABLE_NAME
).get();
71 // meta table already exists
72 exist
= admin
.tableExists(TableName
.META_TABLE_NAME
).get();
77 public void testListTables() throws Exception
{
78 int numTables
= admin
.listTableDescriptors().get().size();
79 final TableName tableName1
= TableName
.valueOf(tableName
.getNameAsString() + "1");
80 final TableName tableName2
= TableName
.valueOf(tableName
.getNameAsString() + "2");
81 final TableName tableName3
= TableName
.valueOf(tableName
.getNameAsString() + "3");
82 TableName
[] tables
= new TableName
[] { tableName1
, tableName2
, tableName3
};
83 for (int i
= 0; i
< tables
.length
; i
++) {
84 createTableWithDefaultConf(tables
[i
]);
87 List
<TableDescriptor
> tableDescs
= admin
.listTableDescriptors().get();
88 int size
= tableDescs
.size();
89 assertTrue(size
>= tables
.length
);
90 for (int i
= 0; i
< tables
.length
&& i
< size
; i
++) {
91 boolean found
= false;
92 for (int j
= 0; j
< size
; j
++) {
93 if (tableDescs
.get(j
).getTableName().equals(tables
[i
])) {
98 assertTrue("Not found: " + tables
[i
], found
);
101 List
<TableName
> tableNames
= admin
.listTableNames().get();
102 size
= tableNames
.size();
103 assertTrue(size
== (numTables
+ tables
.length
));
104 for (int i
= 0; i
< tables
.length
&& i
< size
; i
++) {
105 boolean found
= false;
106 for (int j
= 0; j
< size
; j
++) {
107 if (tableNames
.get(j
).equals(tables
[i
])) {
112 assertTrue("Not found: " + tables
[i
], found
);
115 tableNames
= new ArrayList
<TableName
>(tables
.length
+ 1);
116 tableDescs
= admin
.listTableDescriptors(tableNames
).get();
117 size
= tableDescs
.size();
118 assertEquals(0, size
);
120 Collections
.addAll(tableNames
, tables
);
121 tableNames
.add(TableName
.META_TABLE_NAME
);
122 tableDescs
= admin
.listTableDescriptors(tableNames
).get();
123 size
= tableDescs
.size();
124 assertEquals(tables
.length
+ 1, size
);
125 for (int i
= 0, j
= 0; i
< tables
.length
&& j
< size
; i
++, j
++) {
126 assertTrue("tableName should be equal in order",
127 tableDescs
.get(j
).getTableName().equals(tables
[i
]));
129 assertTrue(tableDescs
.get(size
- 1).getTableName().equals(TableName
.META_TABLE_NAME
));
131 for (int i
= 0; i
< tables
.length
; i
++) {
132 admin
.disableTable(tables
[i
]).join();
133 admin
.deleteTable(tables
[i
]).join();
136 tableDescs
= admin
.listTableDescriptors(true).get();
137 assertTrue("Not found system tables", tableDescs
.size() > 0);
138 tableNames
= admin
.listTableNames(true).get();
139 assertTrue("Not found system tables", tableNames
.size() > 0);
143 public void testGetTableDescriptor() throws Exception
{
144 byte[][] families
= { FAMILY
, FAMILY_0
, FAMILY_1
};
145 TableDescriptorBuilder builder
= TableDescriptorBuilder
.newBuilder(tableName
);
146 for (byte[] family
: families
) {
147 builder
.setColumnFamily(ColumnFamilyDescriptorBuilder
.of(family
));
149 TableDescriptor desc
= builder
.build();
150 admin
.createTable(desc
).join();
151 TableDescriptor confirmedHtd
= admin
.getDescriptor(tableName
).get();
152 //HBASE-26246 introduced persist of store file tracker into table descriptor
153 desc
= TableDescriptorBuilder
.newBuilder(desc
).setValue(TRACKER_IMPL
,
154 StoreFileTrackerFactory
.getStoreFileTrackerName(TEST_UTIL
.getConfiguration())).
156 assertEquals(0, TableDescriptor
.COMPARATOR
.compare(desc
, confirmedHtd
));
160 public void testDisableAndEnableTable() throws Exception
{
161 createTableWithDefaultConf(tableName
);
162 AsyncTable
<?
> table
= ASYNC_CONN
.getTable(tableName
);
163 final byte[] row
= Bytes
.toBytes("row");
164 final byte[] qualifier
= Bytes
.toBytes("qualifier");
165 final byte[] value
= Bytes
.toBytes("value");
166 Put put
= new Put(row
);
167 put
.addColumn(FAMILY
, qualifier
, value
);
168 table
.put(put
).join();
169 Get get
= new Get(row
);
170 get
.addColumn(FAMILY
, qualifier
);
171 table
.get(get
).get();
173 this.admin
.disableTable(tableName
).join();
174 assertTrue("Table must be disabled.", TEST_UTIL
.getHBaseCluster().getMaster()
175 .getTableStateManager().isTableState(tableName
, TableState
.State
.DISABLED
));
176 assertEquals(TableState
.State
.DISABLED
, TestAsyncTableAdminApi
.getStateFromMeta(tableName
));
178 // Test that table is disabled
180 get
.addColumn(FAMILY
, qualifier
);
183 table
.get(get
).get();
184 } catch (ExecutionException e
) {
188 // verify that scan encounters correct exception
190 table
.scanAll(new Scan()).get();
191 } catch (ExecutionException e
) {
195 this.admin
.enableTable(tableName
).join();
196 assertTrue("Table must be enabled.", TEST_UTIL
.getHBaseCluster().getMaster()
197 .getTableStateManager().isTableState(tableName
, TableState
.State
.ENABLED
));
198 assertEquals(TableState
.State
.ENABLED
, TestAsyncTableAdminApi
.getStateFromMeta(tableName
));
200 // Test that table is enabled
202 table
.get(get
).get();
203 } catch (Exception e
) {
207 // meta table can not be disabled.
209 admin
.disableTable(TableName
.META_TABLE_NAME
).get();
210 fail("meta table can not be disabled");
211 } catch (ExecutionException e
) {
212 Throwable cause
= e
.getCause();
213 assertThat(cause
, instanceOf(DoNotRetryIOException
.class));
218 public void testDisableAndEnableTables() throws Exception
{
219 final TableName tableName1
= TableName
.valueOf(tableName
.getNameAsString() + "1");
220 final TableName tableName2
= TableName
.valueOf(tableName
.getNameAsString() + "2");
221 createTableWithDefaultConf(tableName1
);
222 createTableWithDefaultConf(tableName2
);
223 AsyncTable
<?
> table1
= ASYNC_CONN
.getTable(tableName1
);
224 AsyncTable
<?
> table2
= ASYNC_CONN
.getTable(tableName1
);
226 final byte[] row
= Bytes
.toBytes("row");
227 final byte[] qualifier
= Bytes
.toBytes("qualifier");
228 final byte[] value
= Bytes
.toBytes("value");
229 Put put
= new Put(row
);
230 put
.addColumn(FAMILY
, qualifier
, value
);
231 table1
.put(put
).join();
232 table2
.put(put
).join();
233 Get get
= new Get(row
);
234 get
.addColumn(FAMILY
, qualifier
);
235 table1
.get(get
).get();
236 table2
.get(get
).get();
238 admin
.listTableNames(Pattern
.compile(tableName
.getNameAsString() + ".*"), false).get()
239 .forEach(t
-> admin
.disableTable(t
).join());
241 // Test that tables are disabled
243 get
.addColumn(FAMILY
, qualifier
);
246 table1
.get(get
).get();
247 } catch (ExecutionException e
) {
254 table2
.get(get
).get();
255 } catch (ExecutionException e
) {
259 assertEquals(TableState
.State
.DISABLED
, TestAsyncTableAdminApi
.getStateFromMeta(tableName1
));
260 assertEquals(TableState
.State
.DISABLED
, TestAsyncTableAdminApi
.getStateFromMeta(tableName2
));
262 admin
.listTableNames(Pattern
.compile(tableName
.getNameAsString() + ".*"), false).get()
263 .forEach(t
-> admin
.enableTable(t
).join());
265 // Test that tables are enabled
267 table1
.get(get
).get();
268 } catch (Exception e
) {
272 table2
.get(get
).get();
273 } catch (Exception e
) {
277 assertEquals(TableState
.State
.ENABLED
, TestAsyncTableAdminApi
.getStateFromMeta(tableName1
));
278 assertEquals(TableState
.State
.ENABLED
, TestAsyncTableAdminApi
.getStateFromMeta(tableName2
));
282 public void testEnableTableRetainAssignment() throws Exception
{
283 byte[][] splitKeys
= { new byte[] { 1, 1, 1 }, new byte[] { 2, 2, 2 }, new byte[] { 3, 3, 3 },
284 new byte[] { 4, 4, 4 }, new byte[] { 5, 5, 5 }, new byte[] { 6, 6, 6 },
285 new byte[] { 7, 7, 7 }, new byte[] { 8, 8, 8 }, new byte[] { 9, 9, 9 } };
286 int expectedRegions
= splitKeys
.length
+ 1;
287 createTableWithDefaultConf(tableName
, splitKeys
);
289 AsyncTable
<AdvancedScanResultConsumer
> metaTable
= ASYNC_CONN
.getTable(META_TABLE_NAME
);
290 List
<HRegionLocation
> regions
= ClientMetaTableAccessor
291 .getTableHRegionLocations(metaTable
, tableName
).get();
293 "Tried to create " + expectedRegions
+ " regions " + "but only found " + regions
.size(),
294 expectedRegions
, regions
.size());
297 admin
.disableTable(tableName
).join();
298 // Enable table, use retain assignment to assign regions.
299 admin
.enableTable(tableName
).join();
301 List
<HRegionLocation
> regions2
= ClientMetaTableAccessor
302 .getTableHRegionLocations(metaTable
, tableName
).get();
303 // Check the assignment.
304 assertEquals(regions
.size(), regions2
.size());
305 assertTrue(regions2
.containsAll(regions
));
309 public void testIsTableEnabledAndDisabled() throws Exception
{
310 createTableWithDefaultConf(tableName
);
311 assertTrue(admin
.isTableEnabled(tableName
).get());
312 assertFalse(admin
.isTableDisabled(tableName
).get());
313 admin
.disableTable(tableName
).join();
314 assertFalse(admin
.isTableEnabled(tableName
).get());
315 assertTrue(admin
.isTableDisabled(tableName
).get());
317 // meta table is always enabled
318 assertTrue(admin
.isTableEnabled(TableName
.META_TABLE_NAME
).get());
319 assertFalse(admin
.isTableDisabled(TableName
.META_TABLE_NAME
).get());
323 public void testIsTableAvailable() throws Exception
{
324 createTableWithDefaultConf(tableName
);
325 TEST_UTIL
.waitTableAvailable(tableName
);
326 assertTrue(admin
.isTableAvailable(tableName
).get());
327 assertTrue(admin
.isTableAvailable(TableName
.META_TABLE_NAME
).get());