|
20 | 20 | import com.alipay.oceanbase.hbase.util.OHRegionMetrics;
|
21 | 21 | import com.alipay.oceanbase.hbase.util.ObHTableTestUtil;
|
22 | 22 | import com.alipay.oceanbase.hbase.exception.FeatureNotSupportedException;
|
| 23 | +import com.alipay.oceanbase.hbase.util.ResultSetPrinter; |
23 | 24 | import com.alipay.oceanbase.rpc.exception.ObTableException;
|
24 | 25 | import com.alipay.oceanbase.rpc.protocol.payload.ResultCodes;
|
25 | 26 | import org.apache.hadoop.conf.Configuration;
|
26 |
| -import org.apache.hadoop.hbase.RegionMetrics; |
27 |
| -import org.apache.hadoop.hbase.ServerName; |
28 |
| -import org.apache.hadoop.hbase.TableName; |
| 27 | +import org.apache.hadoop.hbase.*; |
29 | 28 | import org.apache.hadoop.hbase.client.*;
|
30 | 29 | import org.apache.hadoop.hbase.util.Bytes;
|
31 | 30 | import org.apache.hadoop.hbase.util.Pair;
|
|
39 | 38 | import java.util.ArrayList;
|
40 | 39 | import java.util.LinkedList;
|
41 | 40 | import java.util.List;
|
42 |
| -import java.util.concurrent.CountDownLatch; |
43 |
| -import java.util.concurrent.ExecutorService; |
44 |
| -import java.util.concurrent.Executors; |
| 41 | +import java.util.concurrent.*; |
45 | 42 |
|
46 | 43 | import static com.alipay.oceanbase.hbase.constants.OHConstants.HBASE_HTABLE_TEST_LOAD_ENABLE;
|
47 | 44 | import static org.apache.hadoop.hbase.util.Bytes.toBytes;
|
48 | 45 | import static org.junit.Assert.*;
|
49 | 46 | import static org.junit.Assert.assertFalse;
|
| 47 | +import static com.alipay.oceanbase.hbase.util.ObHTableSecondaryPartUtil.*; |
50 | 48 |
|
51 | 49 | public class OHTableAdminInterfaceTest {
|
52 | 50 | public OHTablePool setUpLoadPool() throws IOException {
|
@@ -689,4 +687,290 @@ private void batchInsert(int rows, String tablegroup) throws Exception {
|
689 | 687 | Object[] results = new Object[batchLsit.size()];
|
690 | 688 | table.batch(batchLsit, results);
|
691 | 689 | }
|
| 690 | + |
| 691 | + @Test |
| 692 | + public void testCreateDeleteTable() throws Exception { |
| 693 | + TableName tableName = TableName.valueOf("testCreateTable"); |
| 694 | + byte[] cf1 = Bytes.toBytes("cf1"); |
| 695 | + byte[] cf2 = Bytes.toBytes("cf2"); |
| 696 | + byte[] cf3 = Bytes.toBytes("cf3"); |
| 697 | + Configuration conf = ObHTableTestUtil.newConfiguration(); |
| 698 | + Connection connection = ConnectionFactory.createConnection(conf); |
| 699 | + Admin admin = connection.getAdmin(); |
| 700 | + |
| 701 | + // 1. construct htable desc and column family desc |
| 702 | + HColumnDescriptor hcd1 = new HColumnDescriptor(cf1); |
| 703 | + hcd1.setMaxVersions(2); |
| 704 | + hcd1.setTimeToLive(172800); |
| 705 | + |
| 706 | + HColumnDescriptor hcd2 = new HColumnDescriptor(cf2); |
| 707 | + hcd2.setMaxVersions(1); |
| 708 | + hcd2.setTimeToLive(86400); |
| 709 | + |
| 710 | + HColumnDescriptor hcd3 = new HColumnDescriptor(cf3); |
| 711 | + |
| 712 | + // 2. execute create table and check exists |
| 713 | + HTableDescriptor htd = new HTableDescriptor(tableName); |
| 714 | + htd.addFamily(hcd1); |
| 715 | + htd.addFamily(hcd2); |
| 716 | + htd.addFamily(hcd3); |
| 717 | + admin.createTable(htd); |
| 718 | + |
| 719 | + // 3. check table creation success and correctness |
| 720 | + assertTrue(admin.tableExists(tableName)); |
| 721 | + // TODO: show create table, need to be replace by getDescriptor |
| 722 | + java.sql.Connection conn = ObHTableTestUtil.getConnection(); |
| 723 | + String selectSql = "show create table " + tableName.getNameAsString() + "$" + cf1; |
| 724 | + System.out.println("execute sql: " + selectSql); |
| 725 | + java.sql.ResultSet resultSet = conn.createStatement().executeQuery(selectSql); |
| 726 | + ResultSetPrinter.print(resultSet); |
| 727 | + |
| 728 | + selectSql = "show create table " + tableName.getNameAsString() + "$" + cf2; |
| 729 | + System.out.println("execute sql: " + selectSql); |
| 730 | + resultSet = conn.createStatement().executeQuery(selectSql); |
| 731 | + ResultSetPrinter.print(resultSet); |
| 732 | + |
| 733 | + selectSql = "show create table " + tableName.getNameAsString() + "$" + cf3; |
| 734 | + System.out.println("execute sql: " + selectSql); |
| 735 | + resultSet = conn.createStatement().executeQuery(selectSql); |
| 736 | + ResultSetPrinter.print(resultSet); |
| 737 | + |
| 738 | + |
| 739 | + // 4. test put/get some data |
| 740 | + Table table = connection.getTable(tableName); |
| 741 | + Put put = new Put(toBytes("Key" + 1)); |
| 742 | + put.addColumn(cf1, "c1".getBytes(), "hello world".getBytes()); |
| 743 | + put.addColumn(cf2, "c2".getBytes(), "hello world".getBytes()); |
| 744 | + put.addColumn(cf3, "c3".getBytes(), "hello world".getBytes()); |
| 745 | + table.put(put); |
| 746 | + |
| 747 | + Scan scan = new Scan(); |
| 748 | + ResultScanner resultScanner = table.getScanner(scan); |
| 749 | + List<Cell> cells = getCellsFromScanner(resultScanner); |
| 750 | + Assert.assertEquals(3, cells.size()); |
| 751 | + |
| 752 | + // 5. disable and delete table |
| 753 | + admin.disableTable(tableName); |
| 754 | + admin.deleteTable(tableName); |
| 755 | + |
| 756 | + // 5. test table exists after delete |
| 757 | + admin.tableExists(tableName); |
| 758 | + |
| 759 | + // 6. recreate and delete table |
| 760 | + admin.createTable(htd); |
| 761 | + admin.disableTable(tableName); |
| 762 | + admin.deleteTable(tableName); |
| 763 | + } |
| 764 | + |
| 765 | + void testConcurCreateDelTablesHelper(List<TableName> tableNames, Boolean ignoreException) throws Exception { |
| 766 | + Configuration conf = ObHTableTestUtil.newConfiguration(); |
| 767 | + Connection connection = ConnectionFactory.createConnection(conf); |
| 768 | + Admin admin = connection.getAdmin(); |
| 769 | + int tableNums = tableNames.size(); |
| 770 | + |
| 771 | + // use some column family desc |
| 772 | + byte[] cf1 = Bytes.toBytes("cf1"); |
| 773 | + byte[] cf2 = Bytes.toBytes("cf2"); |
| 774 | + byte[] cf3 = Bytes.toBytes("cf3"); |
| 775 | + HColumnDescriptor hcd1 = new HColumnDescriptor(cf1); |
| 776 | + hcd1.setMaxVersions(2); |
| 777 | + hcd1.setTimeToLive(172800); |
| 778 | + HColumnDescriptor hcd2 = new HColumnDescriptor(cf2); |
| 779 | + hcd1.setMaxVersions(1); |
| 780 | + hcd1.setTimeToLive(86400); |
| 781 | + HColumnDescriptor hcd3 = new HColumnDescriptor(cf3); |
| 782 | + |
| 783 | + // 1. generate create table task, one task per table |
| 784 | + List<Callable<Void>> tasks = new ArrayList<>(); |
| 785 | + for (int i = 0; i < tableNums; i++) { |
| 786 | + int finalI = i; |
| 787 | + tasks.add(()->{ |
| 788 | + HTableDescriptor htd = new HTableDescriptor(tableNames.get(finalI)); |
| 789 | + htd.addFamily(hcd1); |
| 790 | + htd.addFamily(hcd2); |
| 791 | + htd.addFamily(hcd3); |
| 792 | + try { |
| 793 | + admin.createTable(htd); |
| 794 | + } catch (Exception e) { |
| 795 | + System.out.println(e); |
| 796 | + if (!ignoreException) { |
| 797 | + throw e; |
| 798 | + } |
| 799 | + } |
| 800 | + return null; |
| 801 | + }); |
| 802 | + } |
| 803 | + |
| 804 | + // 2. execute concurrent create table tasks |
| 805 | + ExecutorService executorService = Executors.newFixedThreadPool(tableNums); |
| 806 | + executorService.invokeAll(tasks); |
| 807 | + executorService.shutdown(); |
| 808 | + executorService.awaitTermination(1, TimeUnit.MINUTES); |
| 809 | + |
| 810 | + // 3. check table create success |
| 811 | + for (int i = 0; i < tableNames.size(); i++) { |
| 812 | + TableName tableName = tableNames.get(i); |
| 813 | + assertTrue(admin.tableExists(tableName)); |
| 814 | + } |
| 815 | + |
| 816 | + // 4. test put/get/delete some data |
| 817 | + for (int i = 0; i < tableNames.size(); i++) { |
| 818 | + Table table = connection.getTable(tableNames.get(i)); |
| 819 | + Put put = new Put(toBytes("Key" + 1)); |
| 820 | + put.addColumn(cf1, "c1".getBytes(), "hello world".getBytes()); |
| 821 | + put.addColumn(cf2, "c2".getBytes(), "hello world".getBytes()); |
| 822 | + put.addColumn(cf3, "c3".getBytes(), "hello world".getBytes()); |
| 823 | + table.put(put); |
| 824 | + |
| 825 | + Scan scan = new Scan(); |
| 826 | + ResultScanner resultScanner = table.getScanner(scan); |
| 827 | + List<Cell> cells = getCellsFromScanner(resultScanner); |
| 828 | + Assert.assertEquals(3, cells.size()); |
| 829 | + |
| 830 | + table.delete(new Delete(toBytes("Key" + 1))); |
| 831 | + } |
| 832 | + |
| 833 | + // 4. disable all tables; |
| 834 | + for (int i = 0; i < tableNames.size(); i++) { |
| 835 | + TableName tableName = tableNames.get(i); |
| 836 | + admin.disableTable(tableName); |
| 837 | + } |
| 838 | + |
| 839 | + // 5. generate delete table task |
| 840 | + List<Callable<Void>> delTasks = new ArrayList<>(); |
| 841 | + for (int i = 0; i < tableNums; i++) { |
| 842 | + int finalI = i; |
| 843 | + delTasks.add(()->{ |
| 844 | + try { |
| 845 | + admin.deleteTable(tableNames.get(finalI)); |
| 846 | + } catch (Exception e) { |
| 847 | + System.out.println(e); |
| 848 | + if (!ignoreException) { |
| 849 | + throw e; |
| 850 | + } |
| 851 | + } |
| 852 | + return null; |
| 853 | + }); |
| 854 | + } |
| 855 | + |
| 856 | + // 6. execute concurrent delete table tasks |
| 857 | + ExecutorService delExecutorService = Executors.newFixedThreadPool(tableNums); |
| 858 | + delExecutorService.invokeAll(delTasks); |
| 859 | + delExecutorService.shutdown(); |
| 860 | + delExecutorService.awaitTermination(1, TimeUnit.MINUTES); |
| 861 | + |
| 862 | + // 7. check table deletion success |
| 863 | + for (int i = 0; i < tableNames.size(); i++) { |
| 864 | + TableName tableName = tableNames.get(i); |
| 865 | + assertFalse(admin.tableExists(tableName)); |
| 866 | + } |
| 867 | + } |
| 868 | + |
| 869 | + // 2. test concurrent create or delete different table |
| 870 | + // step1: create different tables concurrently, it must succeed |
| 871 | + // step2: execute put/read/delete on created table |
| 872 | + // step3: delete different tables concurrently, it must succeed |
| 873 | + @Test |
| 874 | + public void testConcurCreateDelTables() throws Exception { |
| 875 | + final int tableNums = 20; |
| 876 | + List<TableName> tableNames = new ArrayList<>(); |
| 877 | + for (int i = 0; i < tableNums; i++) { |
| 878 | + tableNames.add(TableName.valueOf("testConcurCreateTable" + i)); |
| 879 | + } |
| 880 | + testConcurCreateDelTablesHelper(tableNames, false); |
| 881 | + } |
| 882 | + |
| 883 | + // 3. test concurrent create or delete same table |
| 884 | + // step1: create one table concurrently, only one table was successfully created |
| 885 | + // step2: execute put/read/delete on created table |
| 886 | + // step3: delete one table concurrently, the table will be deleted successfully |
| 887 | + @Test |
| 888 | + public void testConcurCreateOneTable() throws Exception { |
| 889 | + final int taskNum = 20; |
| 890 | + TableName tableName = TableName.valueOf("testConcurCreateOneTable"); |
| 891 | + List<TableName> tableNames = new ArrayList<>(); |
| 892 | + for (int i = 0; i < taskNum; i++) { |
| 893 | + tableNames.add(tableName); |
| 894 | + } |
| 895 | + testConcurCreateDelTablesHelper(tableNames, true); |
| 896 | + } |
| 897 | + |
| 898 | + // 4. test the performance of concurrent create/delete table |
| 899 | + @Test |
| 900 | + public void testConcurCreateDelPerf() throws Exception { |
| 901 | + final int tableNums = 100; |
| 902 | + List<TableName> tableNames = new ArrayList<>(); |
| 903 | + Configuration conf = ObHTableTestUtil.newConfiguration(); |
| 904 | + Connection connection = ConnectionFactory.createConnection(conf); |
| 905 | + Admin admin = connection.getAdmin(); |
| 906 | + byte[] cf1 = Bytes.toBytes("cf1"); |
| 907 | + byte[] cf2 = Bytes.toBytes("cf2"); |
| 908 | + byte[] cf3 = Bytes.toBytes("cf3"); |
| 909 | + HColumnDescriptor hcd1 = new HColumnDescriptor(cf1); |
| 910 | + hcd1.setMaxVersions(2); |
| 911 | + hcd1.setTimeToLive(172800); |
| 912 | + HColumnDescriptor hcd2 = new HColumnDescriptor(cf2); |
| 913 | + hcd1.setMaxVersions(1); |
| 914 | + hcd1.setTimeToLive(86400); |
| 915 | + HColumnDescriptor hcd3 = new HColumnDescriptor(cf3); |
| 916 | + |
| 917 | + for (int i = 0; i < tableNums; i++) { |
| 918 | + tableNames.add(TableName.valueOf("testConcurCreateDelPerf" + i)); |
| 919 | + } |
| 920 | + |
| 921 | + List<Callable<Void>> tasks = new ArrayList<>(); |
| 922 | + for (int i = 0; i < tableNums; i++) { |
| 923 | + int finalI = i; |
| 924 | + tasks.add(()->{ |
| 925 | + HTableDescriptor htd = new HTableDescriptor(tableNames.get(finalI)); |
| 926 | + htd.addFamily(hcd1); |
| 927 | + htd.addFamily(hcd2); |
| 928 | + htd.addFamily(hcd3); |
| 929 | + try { |
| 930 | + admin.createTable(htd); |
| 931 | + } catch (Exception e) { |
| 932 | + System.out.println(e); |
| 933 | + } |
| 934 | + return null; |
| 935 | + }); |
| 936 | + } |
| 937 | + |
| 938 | + // 2. execute concurrent create table tasks |
| 939 | + long start = System.currentTimeMillis(); |
| 940 | + ExecutorService executorService = Executors.newFixedThreadPool(tableNums); |
| 941 | + executorService.invokeAll(tasks); |
| 942 | + executorService.shutdown(); |
| 943 | + executorService.awaitTermination(2, TimeUnit.MINUTES); |
| 944 | + long duration = System.currentTimeMillis() - start; |
| 945 | + System.out.println("create " + tableNums + " tables cost " + duration + " ms."); |
| 946 | + |
| 947 | + // 3. disable all tables; |
| 948 | + for (int i = 0; i < tableNames.size(); i++) { |
| 949 | + TableName tableName = tableNames.get(i); |
| 950 | + admin.disableTable(tableName); |
| 951 | + } |
| 952 | + |
| 953 | + // 4. generate delete table task |
| 954 | + List<Callable<Void>> delTasks = new ArrayList<>(); |
| 955 | + for (int i = 0; i < tableNums; i++) { |
| 956 | + int finalI = i; |
| 957 | + delTasks.add(()->{ |
| 958 | + try { |
| 959 | + admin.deleteTable(tableNames.get(finalI)); |
| 960 | + } catch (Exception e) { |
| 961 | + System.out.println(e); |
| 962 | + } |
| 963 | + return null; |
| 964 | + }); |
| 965 | + } |
| 966 | + |
| 967 | + // 6. execute concurrent delete table tasks |
| 968 | + start = System.currentTimeMillis(); |
| 969 | + ExecutorService delExecutorService = Executors.newFixedThreadPool(tableNums); |
| 970 | + delExecutorService.invokeAll(delTasks); |
| 971 | + delExecutorService.shutdown(); |
| 972 | + delExecutorService.awaitTermination(1, TimeUnit.MINUTES); |
| 973 | + duration = System.currentTimeMillis() - start; |
| 974 | + System.out.println("delete " + tableNums + " tables cost " + duration + " ms."); |
| 975 | + } |
692 | 976 | }
|
0 commit comments