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
.hamcrest
.CoreMatchers
.instanceOf
;
22 import static org
.junit
.Assert
.assertEquals
;
23 import static org
.junit
.Assert
.assertFalse
;
24 import static org
.junit
.Assert
.assertThat
;
25 import static org
.junit
.Assert
.assertTrue
;
26 import static org
.junit
.Assert
.fail
;
28 import java
.util
.ArrayList
;
29 import java
.util
.Collections
;
30 import java
.util
.List
;
31 import java
.util
.concurrent
.ExecutionException
;
32 import java
.util
.regex
.Pattern
;
33 import org
.apache
.hadoop
.hbase
.AsyncMetaTableAccessor
;
34 import org
.apache
.hadoop
.hbase
.DoNotRetryIOException
;
35 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
36 import org
.apache
.hadoop
.hbase
.HRegionLocation
;
37 import org
.apache
.hadoop
.hbase
.TableName
;
38 import org
.apache
.hadoop
.hbase
.client
.TableDescriptorBuilder
.ModifyableTableDescriptor
;
39 import org
.apache
.hadoop
.hbase
.testclassification
.ClientTests
;
40 import org
.apache
.hadoop
.hbase
.testclassification
.LargeTests
;
41 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
42 import org
.junit
.ClassRule
;
43 import org
.junit
.Test
;
44 import org
.junit
.experimental
.categories
.Category
;
45 import org
.junit
.runner
.RunWith
;
46 import org
.junit
.runners
.Parameterized
;
49 * Class to test asynchronous table admin operations.
50 * @see TestAsyncTableAdminApi2 This test and it used to be joined it was taking longer than our
51 * ten minute timeout so they were split.
53 @RunWith(Parameterized
.class)
54 @Category({ LargeTests
.class, ClientTests
.class })
55 public class TestAsyncTableAdminApi3
extends TestAsyncAdminBase
{
57 public static final HBaseClassTestRule CLASS_RULE
=
58 HBaseClassTestRule
.forClass(TestAsyncTableAdminApi3
.class);
61 public void testTableExist() throws Exception
{
63 exist
= admin
.tableExists(tableName
).get();
65 TEST_UTIL
.createTable(tableName
, FAMILY
);
66 exist
= admin
.tableExists(tableName
).get();
68 exist
= admin
.tableExists(TableName
.META_TABLE_NAME
).get();
70 // meta table already exists
71 exist
= admin
.tableExists(TableName
.META_TABLE_NAME
).get();
76 public void testListTables() throws Exception
{
77 int numTables
= admin
.listTableDescriptors().get().size();
78 final TableName tableName1
= TableName
.valueOf(tableName
.getNameAsString() + "1");
79 final TableName tableName2
= TableName
.valueOf(tableName
.getNameAsString() + "2");
80 final TableName tableName3
= TableName
.valueOf(tableName
.getNameAsString() + "3");
81 TableName
[] tables
= new TableName
[] { tableName1
, tableName2
, tableName3
};
82 for (int i
= 0; i
< tables
.length
; i
++) {
83 createTableWithDefaultConf(tables
[i
]);
86 List
<TableDescriptor
> tableDescs
= admin
.listTableDescriptors().get();
87 int size
= tableDescs
.size();
88 assertTrue(size
>= tables
.length
);
89 for (int i
= 0; i
< tables
.length
&& i
< size
; i
++) {
90 boolean found
= false;
91 for (int j
= 0; j
< size
; j
++) {
92 if (tableDescs
.get(j
).getTableName().equals(tables
[i
])) {
97 assertTrue("Not found: " + tables
[i
], found
);
100 List
<TableName
> tableNames
= admin
.listTableNames().get();
101 size
= tableNames
.size();
102 assertTrue(size
== (numTables
+ tables
.length
));
103 for (int i
= 0; i
< tables
.length
&& i
< size
; i
++) {
104 boolean found
= false;
105 for (int j
= 0; j
< size
; j
++) {
106 if (tableNames
.get(j
).equals(tables
[i
])) {
111 assertTrue("Not found: " + tables
[i
], found
);
114 tableNames
= new ArrayList
<TableName
>(tables
.length
+ 1);
115 tableDescs
= admin
.listTableDescriptors(tableNames
).get();
116 size
= tableDescs
.size();
117 assertEquals(0, size
);
119 Collections
.addAll(tableNames
, tables
);
120 tableNames
.add(TableName
.META_TABLE_NAME
);
121 tableDescs
= admin
.listTableDescriptors(tableNames
).get();
122 size
= tableDescs
.size();
123 assertEquals(tables
.length
+ 1, size
);
124 for (int i
= 0, j
= 0; i
< tables
.length
&& j
< size
; i
++, j
++) {
125 assertTrue("tableName should be equal in order",
126 tableDescs
.get(j
).getTableName().equals(tables
[i
]));
128 assertTrue(tableDescs
.get(size
- 1).getTableName().equals(TableName
.META_TABLE_NAME
));
130 for (int i
= 0; i
< tables
.length
; i
++) {
131 admin
.disableTable(tables
[i
]).join();
132 admin
.deleteTable(tables
[i
]).join();
135 tableDescs
= admin
.listTableDescriptors(true).get();
136 assertTrue("Not found system tables", tableDescs
.size() > 0);
137 tableNames
= admin
.listTableNames(true).get();
138 assertTrue("Not found system tables", tableNames
.size() > 0);
142 public void testGetTableDescriptor() throws Exception
{
143 byte[][] families
= { FAMILY
, FAMILY_0
, FAMILY_1
};
144 TableDescriptorBuilder builder
= TableDescriptorBuilder
.newBuilder(tableName
);
145 for (byte[] family
: families
) {
146 builder
.setColumnFamily(ColumnFamilyDescriptorBuilder
.of(family
));
148 TableDescriptor desc
= builder
.build();
149 admin
.createTable(desc
).join();
150 ModifyableTableDescriptor modifyableDesc
= ((ModifyableTableDescriptor
) desc
);
151 TableDescriptor confirmedHtd
= admin
.getDescriptor(tableName
).get();
152 assertEquals(0, modifyableDesc
.compareTo((ModifyableTableDescriptor
) confirmedHtd
));
156 public void testDisableAndEnableTable() throws Exception
{
157 createTableWithDefaultConf(tableName
);
158 AsyncTable
<?
> table
= ASYNC_CONN
.getTable(tableName
);
159 final byte[] row
= Bytes
.toBytes("row");
160 final byte[] qualifier
= Bytes
.toBytes("qualifier");
161 final byte[] value
= Bytes
.toBytes("value");
162 Put put
= new Put(row
);
163 put
.addColumn(FAMILY
, qualifier
, value
);
164 table
.put(put
).join();
165 Get get
= new Get(row
);
166 get
.addColumn(FAMILY
, qualifier
);
167 table
.get(get
).get();
169 this.admin
.disableTable(tableName
).join();
170 assertTrue("Table must be disabled.", TEST_UTIL
.getHBaseCluster().getMaster()
171 .getTableStateManager().isTableState(tableName
, TableState
.State
.DISABLED
));
172 assertEquals(TableState
.State
.DISABLED
, TestAsyncTableAdminApi
.getStateFromMeta(tableName
));
174 // Test that table is disabled
176 get
.addColumn(FAMILY
, qualifier
);
179 table
.get(get
).get();
180 } catch (ExecutionException e
) {
184 // verify that scan encounters correct exception
186 table
.scanAll(new Scan()).get();
187 } catch (ExecutionException e
) {
191 this.admin
.enableTable(tableName
).join();
192 assertTrue("Table must be enabled.", TEST_UTIL
.getHBaseCluster().getMaster()
193 .getTableStateManager().isTableState(tableName
, TableState
.State
.ENABLED
));
194 assertEquals(TableState
.State
.ENABLED
, TestAsyncTableAdminApi
.getStateFromMeta(tableName
));
196 // Test that table is enabled
198 table
.get(get
).get();
199 } catch (Exception e
) {
203 // meta table can not be disabled.
205 admin
.disableTable(TableName
.META_TABLE_NAME
).get();
206 fail("meta table can not be disabled");
207 } catch (ExecutionException e
) {
208 Throwable cause
= e
.getCause();
209 assertThat(cause
, instanceOf(DoNotRetryIOException
.class));
214 public void testDisableAndEnableTables() throws Exception
{
215 final TableName tableName1
= TableName
.valueOf(tableName
.getNameAsString() + "1");
216 final TableName tableName2
= TableName
.valueOf(tableName
.getNameAsString() + "2");
217 createTableWithDefaultConf(tableName1
);
218 createTableWithDefaultConf(tableName2
);
219 AsyncTable
<?
> table1
= ASYNC_CONN
.getTable(tableName1
);
220 AsyncTable
<?
> table2
= ASYNC_CONN
.getTable(tableName1
);
222 final byte[] row
= Bytes
.toBytes("row");
223 final byte[] qualifier
= Bytes
.toBytes("qualifier");
224 final byte[] value
= Bytes
.toBytes("value");
225 Put put
= new Put(row
);
226 put
.addColumn(FAMILY
, qualifier
, value
);
227 table1
.put(put
).join();
228 table2
.put(put
).join();
229 Get get
= new Get(row
);
230 get
.addColumn(FAMILY
, qualifier
);
231 table1
.get(get
).get();
232 table2
.get(get
).get();
234 admin
.listTableNames(Pattern
.compile(tableName
.getNameAsString() + ".*"), false).get()
235 .forEach(t
-> admin
.disableTable(t
).join());
237 // Test that tables are disabled
239 get
.addColumn(FAMILY
, qualifier
);
242 table1
.get(get
).get();
243 } catch (ExecutionException e
) {
250 table2
.get(get
).get();
251 } catch (ExecutionException e
) {
255 assertEquals(TableState
.State
.DISABLED
, TestAsyncTableAdminApi
.getStateFromMeta(tableName1
));
256 assertEquals(TableState
.State
.DISABLED
, TestAsyncTableAdminApi
.getStateFromMeta(tableName2
));
258 admin
.listTableNames(Pattern
.compile(tableName
.getNameAsString() + ".*"), false).get()
259 .forEach(t
-> admin
.enableTable(t
).join());
261 // Test that tables are enabled
263 table1
.get(get
).get();
264 } catch (Exception e
) {
268 table2
.get(get
).get();
269 } catch (Exception e
) {
273 assertEquals(TableState
.State
.ENABLED
, TestAsyncTableAdminApi
.getStateFromMeta(tableName1
));
274 assertEquals(TableState
.State
.ENABLED
, TestAsyncTableAdminApi
.getStateFromMeta(tableName2
));
278 public void testEnableTableRetainAssignment() throws Exception
{
279 byte[][] splitKeys
= { new byte[] { 1, 1, 1 }, new byte[] { 2, 2, 2 }, new byte[] { 3, 3, 3 },
280 new byte[] { 4, 4, 4 }, new byte[] { 5, 5, 5 }, new byte[] { 6, 6, 6 },
281 new byte[] { 7, 7, 7 }, new byte[] { 8, 8, 8 }, new byte[] { 9, 9, 9 } };
282 int expectedRegions
= splitKeys
.length
+ 1;
283 createTableWithDefaultConf(tableName
, splitKeys
);
285 AsyncTable
<AdvancedScanResultConsumer
> metaTable
= ASYNC_CONN
.getTable(META_TABLE_NAME
);
286 List
<HRegionLocation
> regions
= AsyncMetaTableAccessor
287 .getTableHRegionLocations(metaTable
, tableName
).get();
289 "Tried to create " + expectedRegions
+ " regions " + "but only found " + regions
.size(),
290 expectedRegions
, regions
.size());
293 admin
.disableTable(tableName
).join();
294 // Enable table, use retain assignment to assign regions.
295 admin
.enableTable(tableName
).join();
297 List
<HRegionLocation
> regions2
= AsyncMetaTableAccessor
298 .getTableHRegionLocations(metaTable
, tableName
).get();
299 // Check the assignment.
300 assertEquals(regions
.size(), regions2
.size());
301 assertTrue(regions2
.containsAll(regions
));
305 public void testIsTableEnabledAndDisabled() throws Exception
{
306 createTableWithDefaultConf(tableName
);
307 assertTrue(admin
.isTableEnabled(tableName
).get());
308 assertFalse(admin
.isTableDisabled(tableName
).get());
309 admin
.disableTable(tableName
).join();
310 assertFalse(admin
.isTableEnabled(tableName
).get());
311 assertTrue(admin
.isTableDisabled(tableName
).get());
313 // meta table is always enabled
314 assertTrue(admin
.isTableEnabled(TableName
.META_TABLE_NAME
).get());
315 assertFalse(admin
.isTableDisabled(TableName
.META_TABLE_NAME
).get());
319 public void testIsTableAvailable() throws Exception
{
320 createTableWithDefaultConf(tableName
);
321 TEST_UTIL
.waitTableAvailable(tableName
);
322 assertTrue(admin
.isTableAvailable(tableName
).get());
323 assertTrue(admin
.isTableAvailable(TableName
.META_TABLE_NAME
).get());