|
29 | 29 | import org.apache.hadoop.hbase.client.*;
|
30 | 30 | import org.apache.hadoop.hbase.util.Bytes;
|
31 | 31 | import org.apache.hadoop.hbase.util.Pair;
|
| 32 | + |
32 | 33 | import org.junit.Assert;
|
33 | 34 | import org.junit.Test;
|
34 | 35 |
|
| 36 | + |
35 | 37 | import java.io.IOException;
|
36 | 38 | import java.sql.Statement;
|
37 | 39 | import java.util.ArrayList;
|
@@ -268,6 +270,121 @@ public void testGetStartEndKeysOHTablePoolLoadNon() throws Exception {
|
268 | 270 | Assert.assertEquals(0, startEndKeys.getSecond()[0].length);
|
269 | 271 | }
|
270 | 272 |
|
| 273 | + @Test |
| 274 | + public void testAdminEnDisableTable() throws Exception { |
| 275 | + java.sql.Connection conn = ObHTableTestUtil.getConnection(); |
| 276 | + Statement st = conn.createStatement(); |
| 277 | + st.execute("CREATE TABLEGROUP IF NOT EXISTS test_multi_cf SHARDING = 'ADAPTIVE';\n" + |
| 278 | + "\n" + |
| 279 | + "CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group1` (\n" + |
| 280 | + " `K` varbinary(1024) NOT NULL,\n" + |
| 281 | + " `Q` varbinary(256) NOT NULL,\n" + |
| 282 | + " `T` bigint(20) NOT NULL,\n" + |
| 283 | + " `V` varbinary(1024) DEFAULT NULL,\n" + |
| 284 | + " PRIMARY KEY (`K`, `Q`, `T`)\n" + |
| 285 | + ") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" + |
| 286 | + "\n" + |
| 287 | + "CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group2` (\n" + |
| 288 | + " `K` varbinary(1024) NOT NULL,\n" + |
| 289 | + " `Q` varbinary(256) NOT NULL,\n" + |
| 290 | + " `T` bigint(20) NOT NULL,\n" + |
| 291 | + " `V` varbinary(1024) DEFAULT NULL,\n" + |
| 292 | + " PRIMARY KEY (`K`, `Q`, `T`)\n" + |
| 293 | + ") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" + |
| 294 | + "\n" + |
| 295 | + "CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group3` (\n" + |
| 296 | + " `K` varbinary(1024) NOT NULL,\n" + |
| 297 | + " `Q` varbinary(256) NOT NULL,\n" + |
| 298 | + " `T` bigint(20) NOT NULL,\n" + |
| 299 | + " `V` varbinary(1024) DEFAULT NULL,\n" + |
| 300 | + " PRIMARY KEY (`K`, `Q`, `T`)\n" + |
| 301 | + ") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" + |
| 302 | + "\n" + |
| 303 | + "CREATE DATABASE IF NOT EXISTS `n1`;\n" + |
| 304 | + "use `n1`;\n" + |
| 305 | + "CREATE TABLEGROUP IF NOT EXISTS `n1:test` SHARDING = 'ADAPTIVE';\n" + |
| 306 | + "CREATE TABLE IF NOT EXISTS `n1:test$family_group` (\n" + |
| 307 | + " `K` varbinary(1024) NOT NULL,\n" + |
| 308 | + " `Q` varbinary(256) NOT NULL,\n" + |
| 309 | + " `T` bigint(20) NOT NULL,\n" + |
| 310 | + " `V` varbinary(1024) DEFAULT NULL,\n" + |
| 311 | + " PRIMARY KEY (`K`, `Q`, `T`)\n" + |
| 312 | + ") TABLEGROUP = `n1:test`;" + |
| 313 | + "\n" + |
| 314 | + "CREATE TABLE IF NOT EXISTS `n1:test$family1` (\n" + |
| 315 | + " `K` varbinary(1024) NOT NULL,\n" + |
| 316 | + " `Q` varbinary(256) NOT NULL,\n" + |
| 317 | + " `T` bigint(20) NOT NULL,\n" + |
| 318 | + " `V` varbinary(1024) DEFAULT NULL,\n" + |
| 319 | + " PRIMARY KEY (`K`, `Q`, `T`)\n" + |
| 320 | + ") TABLEGROUP = `n1:test`;"); |
| 321 | + Configuration conf = ObHTableTestUtil.newConfiguration(); |
| 322 | + Connection connection = ConnectionFactory.createConnection(conf); |
| 323 | + Admin admin = connection.getAdmin(); |
| 324 | + admin.disableTable(TableName.valueOf("test_multi_cf")); |
| 325 | + assertTrue(admin.tableExists(TableName.valueOf("n1", "test"))); |
| 326 | + assertTrue(admin.tableExists(TableName.valueOf("test_multi_cf"))); |
| 327 | + // disable a non-existed table |
| 328 | + IOException thrown = assertThrows(IOException.class, |
| 329 | + () -> { |
| 330 | + admin.disableTable(TableName.valueOf("tablegroup_not_exists")); |
| 331 | + }); |
| 332 | + assertTrue(thrown.getCause() instanceof ObTableException); |
| 333 | + Assert.assertEquals(ResultCodes.OB_TABLEGROUP_NOT_EXIST.errorCode, ((ObTableException) thrown.getCause()).getErrorCode()); |
| 334 | + |
| 335 | + // write an enabled table, should succeed |
| 336 | + batchInsert(10, "test_multi_ch"); |
| 337 | + // disable a disabled table |
| 338 | + thrown = assertThrows(IOException.class, |
| 339 | + () -> { |
| 340 | + admin.disableTable(TableName.valueOf("test_multi_cf")); |
| 341 | + }); |
| 342 | + assertTrue(thrown.getCause() instanceof ObTableException); |
| 343 | + Assert.assertEquals(ResultCodes.OB_KV_TABLE_NOT_DISABLED.errorCode, ((ObTableException) thrown.getCause()).getErrorCode()); |
| 344 | + |
| 345 | + // write an enabled table, should fail |
| 346 | + batchInsert(10, "test_multi_ch"); |
| 347 | + enDisableRead(10, "test_multi_ch"); |
| 348 | + |
| 349 | + // enable a disabled table |
| 350 | + admin.enableTable(TableName.valueOf("test_multi_cf")); |
| 351 | + |
| 352 | + // write an enabled table, should succeed |
| 353 | + batchInsert(10, "test_multi_ch"); |
| 354 | + enDisableRead(10, "test_multi_ch"); |
| 355 | + |
| 356 | + // enable an enabled table |
| 357 | + thrown = assertThrows(IOException.class, |
| 358 | + () -> { |
| 359 | + admin.disableTable(TableName.valueOf("n1", "test"); |
| 360 | + }); |
| 361 | + assertTrue(thrown.getCause() instanceof ObTableException); |
| 362 | + Assert.assertEquals(ResultCodes.OB_KV_TABLE_NOT_ENABLED.errorCode, ((ObTableException) thrown.getCause()).getErrorCode()); |
| 363 | + |
| 364 | + admin.deleteTable(TableName.valueOf("n1", "test")); |
| 365 | + admin.deleteTable(TableName.valueOf("test_multi_cf")); |
| 366 | + assertFalse(admin.tableExists(TableName.valueOf("n1", "test"))); |
| 367 | + assertFalse(admin.tableExists(TableName.valueOf("test_multi_cf"))); |
| 368 | + } |
| 369 | + |
| 370 | + private void enDisableRead(int rows, String tablegroup) throws Exception { |
| 371 | + Configuration conf = ObHTableTestUtil.newConfiguration(); |
| 372 | + Connection connection = ConnectionFactory.createConnection(conf); |
| 373 | + Table table = connection.getTable(TableName.valueOf(tablegroup)); |
| 374 | + List<Row> batchLsit = new LinkedList<>(); |
| 375 | + for (int i = 0; i < rows; ++i) { |
| 376 | + Get get = new Get(toBytes("Key" + i)); |
| 377 | + batchLsit.add(get); |
| 378 | + if (i % 100 == 0) { // 100 rows one batch to avoid OB_TIMEOUT |
| 379 | + Object[] results = new Object[batchLsit.size()]; |
| 380 | + table.batch(batchLsit, results); |
| 381 | + batchLsit.clear(); |
| 382 | + } |
| 383 | + } |
| 384 | + Object[] results = new Object[batchLsit.size()]; |
| 385 | + table.batch(batchLsit, results); |
| 386 | + } |
| 387 | + |
271 | 388 | @Test
|
272 | 389 | public void testAdminGetRegionMetrics() throws Exception {
|
273 | 390 | java.sql.Connection conn = ObHTableTestUtil.getConnection();
|
|
0 commit comments