HBASE-22002 Remove the deprecated methods in Admin interface
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / client / TestAsyncTableAdminApi3.java
blob750749dc0e1d487a53a645f09c1760078aa64a44
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
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.List;
29 import java.util.Optional;
30 import java.util.concurrent.ExecutionException;
31 import java.util.regex.Pattern;
32 import org.apache.hadoop.hbase.AsyncMetaTableAccessor;
33 import org.apache.hadoop.hbase.DoNotRetryIOException;
34 import org.apache.hadoop.hbase.HBaseClassTestRule;
35 import org.apache.hadoop.hbase.HRegionLocation;
36 import org.apache.hadoop.hbase.TableName;
37 import org.apache.hadoop.hbase.client.TableDescriptorBuilder.ModifyableTableDescriptor;
38 import org.apache.hadoop.hbase.testclassification.ClientTests;
39 import org.apache.hadoop.hbase.testclassification.LargeTests;
40 import org.apache.hadoop.hbase.util.Bytes;
41 import org.junit.ClassRule;
42 import org.junit.Test;
43 import org.junit.experimental.categories.Category;
44 import org.junit.runner.RunWith;
45 import org.junit.runners.Parameterized;
47 /**
48 * Class to test asynchronous table admin operations.
49 * @see TestAsyncTableAdminApi2 This test and it used to be joined it was taking longer than our
50 * ten minute timeout so they were split.
52 @RunWith(Parameterized.class)
53 @Category({ LargeTests.class, ClientTests.class })
54 public class TestAsyncTableAdminApi3 extends TestAsyncAdminBase {
55 @ClassRule
56 public static final HBaseClassTestRule CLASS_RULE =
57 HBaseClassTestRule.forClass(TestAsyncTableAdminApi3.class);
59 @Test
60 public void testTableExist() throws Exception {
61 boolean exist;
62 exist = admin.tableExists(tableName).get();
63 assertFalse(exist);
64 TEST_UTIL.createTable(tableName, FAMILY);
65 exist = admin.tableExists(tableName).get();
66 assertTrue(exist);
67 exist = admin.tableExists(TableName.META_TABLE_NAME).get();
68 assertTrue(exist);
69 // meta table already exists
70 exist = admin.tableExists(TableName.META_TABLE_NAME).get();
71 assertTrue(exist);
74 @Test
75 public void testListTables() throws Exception {
76 int numTables = admin.listTableDescriptors().get().size();
77 final TableName tableName1 = TableName.valueOf(tableName.getNameAsString() + "1");
78 final TableName tableName2 = TableName.valueOf(tableName.getNameAsString() + "2");
79 final TableName tableName3 = TableName.valueOf(tableName.getNameAsString() + "3");
80 TableName[] tables = new TableName[] { tableName1, tableName2, tableName3 };
81 for (int i = 0; i < tables.length; i++) {
82 createTableWithDefaultConf(tables[i]);
85 List<TableDescriptor> tableDescs = admin.listTableDescriptors().get();
86 int size = tableDescs.size();
87 assertTrue(size >= tables.length);
88 for (int i = 0; i < tables.length && i < size; i++) {
89 boolean found = false;
90 for (int j = 0; j < size; j++) {
91 if (tableDescs.get(j).getTableName().equals(tables[i])) {
92 found = true;
93 break;
96 assertTrue("Not found: " + tables[i], found);
99 List<TableName> tableNames = admin.listTableNames().get();
100 size = tableNames.size();
101 assertTrue(size == (numTables + tables.length));
102 for (int i = 0; i < tables.length && i < size; i++) {
103 boolean found = false;
104 for (int j = 0; j < size; j++) {
105 if (tableNames.get(j).equals(tables[i])) {
106 found = true;
107 break;
110 assertTrue("Not found: " + tables[i], found);
113 for (int i = 0; i < tables.length; i++) {
114 admin.disableTable(tables[i]).join();
115 admin.deleteTable(tables[i]).join();
118 tableDescs = admin.listTableDescriptors(true).get();
119 assertTrue("Not found system tables", tableDescs.size() > 0);
120 tableNames = admin.listTableNames(true).get();
121 assertTrue("Not found system tables", tableNames.size() > 0);
124 @Test
125 public void testGetTableDescriptor() throws Exception {
126 byte[][] families = { FAMILY, FAMILY_0, FAMILY_1 };
127 TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
128 for (byte[] family : families) {
129 builder.setColumnFamily(ColumnFamilyDescriptorBuilder.of(family));
131 TableDescriptor desc = builder.build();
132 admin.createTable(desc).join();
133 ModifyableTableDescriptor modifyableDesc = ((ModifyableTableDescriptor) desc);
134 TableDescriptor confirmedHtd = admin.getDescriptor(tableName).get();
135 assertEquals(0, modifyableDesc.compareTo((ModifyableTableDescriptor) confirmedHtd));
138 @Test
139 public void testDisableAndEnableTable() throws Exception {
140 createTableWithDefaultConf(tableName);
141 AsyncTable<?> table = ASYNC_CONN.getTable(tableName);
142 final byte[] row = Bytes.toBytes("row");
143 final byte[] qualifier = Bytes.toBytes("qualifier");
144 final byte[] value = Bytes.toBytes("value");
145 Put put = new Put(row);
146 put.addColumn(FAMILY, qualifier, value);
147 table.put(put).join();
148 Get get = new Get(row);
149 get.addColumn(FAMILY, qualifier);
150 table.get(get).get();
152 this.admin.disableTable(tableName).join();
153 assertTrue("Table must be disabled.", TEST_UTIL.getHBaseCluster().getMaster()
154 .getTableStateManager().isTableState(tableName, TableState.State.DISABLED));
155 assertEquals(TableState.State.DISABLED, TestAsyncTableAdminApi.getStateFromMeta(tableName));
157 // Test that table is disabled
158 get = new Get(row);
159 get.addColumn(FAMILY, qualifier);
160 boolean ok = false;
161 try {
162 table.get(get).get();
163 } catch (ExecutionException e) {
164 ok = true;
166 ok = false;
167 // verify that scan encounters correct exception
168 try {
169 table.scanAll(new Scan()).get();
170 } catch (ExecutionException e) {
171 ok = true;
173 assertTrue(ok);
174 this.admin.enableTable(tableName).join();
175 assertTrue("Table must be enabled.", TEST_UTIL.getHBaseCluster().getMaster()
176 .getTableStateManager().isTableState(tableName, TableState.State.ENABLED));
177 assertEquals(TableState.State.ENABLED, TestAsyncTableAdminApi.getStateFromMeta(tableName));
179 // Test that table is enabled
180 try {
181 table.get(get).get();
182 } catch (Exception e) {
183 ok = false;
185 assertTrue(ok);
186 // meta table can not be disabled.
187 try {
188 admin.disableTable(TableName.META_TABLE_NAME).get();
189 fail("meta table can not be disabled");
190 } catch (ExecutionException e) {
191 Throwable cause = e.getCause();
192 assertThat(cause, instanceOf(DoNotRetryIOException.class));
196 @Test
197 public void testDisableAndEnableTables() throws Exception {
198 final TableName tableName1 = TableName.valueOf(tableName.getNameAsString() + "1");
199 final TableName tableName2 = TableName.valueOf(tableName.getNameAsString() + "2");
200 createTableWithDefaultConf(tableName1);
201 createTableWithDefaultConf(tableName2);
202 AsyncTable<?> table1 = ASYNC_CONN.getTable(tableName1);
203 AsyncTable<?> table2 = ASYNC_CONN.getTable(tableName1);
205 final byte[] row = Bytes.toBytes("row");
206 final byte[] qualifier = Bytes.toBytes("qualifier");
207 final byte[] value = Bytes.toBytes("value");
208 Put put = new Put(row);
209 put.addColumn(FAMILY, qualifier, value);
210 table1.put(put).join();
211 table2.put(put).join();
212 Get get = new Get(row);
213 get.addColumn(FAMILY, qualifier);
214 table1.get(get).get();
215 table2.get(get).get();
217 admin.listTableNames(Pattern.compile(tableName.getNameAsString() + ".*"), false).get()
218 .forEach(t -> admin.disableTable(t).join());
220 // Test that tables are disabled
221 get = new Get(row);
222 get.addColumn(FAMILY, qualifier);
223 boolean ok = false;
224 try {
225 table1.get(get).get();
226 } catch (ExecutionException e) {
227 ok = true;
229 assertTrue(ok);
231 ok = false;
232 try {
233 table2.get(get).get();
234 } catch (ExecutionException e) {
235 ok = true;
237 assertTrue(ok);
238 assertEquals(TableState.State.DISABLED, TestAsyncTableAdminApi.getStateFromMeta(tableName1));
239 assertEquals(TableState.State.DISABLED, TestAsyncTableAdminApi.getStateFromMeta(tableName2));
241 admin.listTableNames(Pattern.compile(tableName.getNameAsString() + ".*"), false).get()
242 .forEach(t -> admin.enableTable(t).join());
244 // Test that tables are enabled
245 try {
246 table1.get(get).get();
247 } catch (Exception e) {
248 ok = false;
250 try {
251 table2.get(get).get();
252 } catch (Exception e) {
253 ok = false;
255 assertTrue(ok);
256 assertEquals(TableState.State.ENABLED, TestAsyncTableAdminApi.getStateFromMeta(tableName1));
257 assertEquals(TableState.State.ENABLED, TestAsyncTableAdminApi.getStateFromMeta(tableName2));
260 @Test
261 public void testEnableTableRetainAssignment() throws Exception {
262 byte[][] splitKeys = { new byte[] { 1, 1, 1 }, new byte[] { 2, 2, 2 }, new byte[] { 3, 3, 3 },
263 new byte[] { 4, 4, 4 }, new byte[] { 5, 5, 5 }, new byte[] { 6, 6, 6 },
264 new byte[] { 7, 7, 7 }, new byte[] { 8, 8, 8 }, new byte[] { 9, 9, 9 } };
265 int expectedRegions = splitKeys.length + 1;
266 createTableWithDefaultConf(tableName, splitKeys);
268 AsyncTable<AdvancedScanResultConsumer> metaTable = ASYNC_CONN.getTable(META_TABLE_NAME);
269 List<HRegionLocation> regions =
270 AsyncMetaTableAccessor.getTableHRegionLocations(metaTable, Optional.of(tableName)).get();
271 assertEquals(
272 "Tried to create " + expectedRegions + " regions " + "but only found " + regions.size(),
273 expectedRegions, regions.size());
275 // Disable table.
276 admin.disableTable(tableName).join();
277 // Enable table, use retain assignment to assign regions.
278 admin.enableTable(tableName).join();
280 List<HRegionLocation> regions2 =
281 AsyncMetaTableAccessor.getTableHRegionLocations(metaTable, Optional.of(tableName)).get();
282 // Check the assignment.
283 assertEquals(regions.size(), regions2.size());
284 assertTrue(regions2.containsAll(regions));
287 @Test
288 public void testIsTableEnabledAndDisabled() throws Exception {
289 createTableWithDefaultConf(tableName);
290 assertTrue(admin.isTableEnabled(tableName).get());
291 assertFalse(admin.isTableDisabled(tableName).get());
292 admin.disableTable(tableName).join();
293 assertFalse(admin.isTableEnabled(tableName).get());
294 assertTrue(admin.isTableDisabled(tableName).get());
296 // meta table is always enabled
297 assertTrue(admin.isTableEnabled(TableName.META_TABLE_NAME).get());
298 assertFalse(admin.isTableDisabled(TableName.META_TABLE_NAME).get());
301 @Test
302 public void testIsTableAvailable() throws Exception {
303 createTableWithDefaultConf(tableName);
304 TEST_UTIL.waitTableAvailable(tableName);
305 assertTrue(admin.isTableAvailable(tableName).get());
306 assertTrue(admin.isTableAvailable(TableName.META_TABLE_NAME).get());