Skip to content

Commit 7aa7dbd

Browse files
shenyunlongZhou-jw
authored andcommitted
[Test] add create/delete table testcases
1 parent c7b23e4 commit 7aa7dbd

File tree

1 file changed

+292
-6
lines changed

1 file changed

+292
-6
lines changed

src/test/java/com/alipay/oceanbase/hbase/OHTableAdminInterfaceTest.java

Lines changed: 292 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,31 @@
2020
import com.alipay.oceanbase.hbase.util.OHRegionMetrics;
2121
import com.alipay.oceanbase.hbase.util.ObHTableTestUtil;
2222
import com.alipay.oceanbase.hbase.exception.FeatureNotSupportedException;
23+
import com.alipay.oceanbase.hbase.util.ResultSetPrinter;
2324
import com.alipay.oceanbase.rpc.exception.ObTableException;
2425
import com.alipay.oceanbase.rpc.protocol.payload.ResultCodes;
2526
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.*;
2928
import org.apache.hadoop.hbase.client.*;
3029
import org.apache.hadoop.hbase.util.Bytes;
3130
import org.apache.hadoop.hbase.util.Pair;
31+
3232
import org.junit.Assert;
3333
import org.junit.Test;
3434

35+
3536
import java.io.IOException;
3637
import java.sql.Statement;
3738
import java.util.ArrayList;
3839
import java.util.LinkedList;
3940
import java.util.List;
40-
import java.util.concurrent.CountDownLatch;
41-
import java.util.concurrent.ExecutorService;
42-
import java.util.concurrent.Executors;
41+
import java.util.concurrent.*;
4342

4443
import static com.alipay.oceanbase.hbase.constants.OHConstants.HBASE_HTABLE_TEST_LOAD_ENABLE;
4544
import static org.apache.hadoop.hbase.util.Bytes.toBytes;
4645
import static org.junit.Assert.*;
4746
import static org.junit.Assert.assertFalse;
47+
import static com.alipay.oceanbase.hbase.util.ObHTableSecondaryPartUtil.*;
4848

4949
public class OHTableAdminInterfaceTest {
5050
public OHTablePool setUpLoadPool() throws IOException {
@@ -802,4 +802,290 @@ private void enDisableRead(int rows, String tablegroup) throws Exception {
802802
Object[] results = new Object[batchLsit.size()];
803803
table.batch(batchLsit, results);
804804
}
805+
806+
@Test
807+
public void testCreateDeleteTable() throws Exception {
808+
TableName tableName = TableName.valueOf("testCreateTable");
809+
byte[] cf1 = Bytes.toBytes("cf1");
810+
byte[] cf2 = Bytes.toBytes("cf2");
811+
byte[] cf3 = Bytes.toBytes("cf3");
812+
Configuration conf = ObHTableTestUtil.newConfiguration();
813+
Connection connection = ConnectionFactory.createConnection(conf);
814+
Admin admin = connection.getAdmin();
815+
816+
// 1. construct htable desc and column family desc
817+
HColumnDescriptor hcd1 = new HColumnDescriptor(cf1);
818+
hcd1.setMaxVersions(2);
819+
hcd1.setTimeToLive(172800);
820+
821+
HColumnDescriptor hcd2 = new HColumnDescriptor(cf2);
822+
hcd2.setMaxVersions(1);
823+
hcd2.setTimeToLive(86400);
824+
825+
HColumnDescriptor hcd3 = new HColumnDescriptor(cf3);
826+
827+
// 2. execute create table and check exists
828+
HTableDescriptor htd = new HTableDescriptor(tableName);
829+
htd.addFamily(hcd1);
830+
htd.addFamily(hcd2);
831+
htd.addFamily(hcd3);
832+
admin.createTable(htd);
833+
834+
// 3. check table creation success and correctness
835+
assertTrue(admin.tableExists(tableName));
836+
// TODO: show create table, need to be replace by getDescriptor
837+
java.sql.Connection conn = ObHTableTestUtil.getConnection();
838+
String selectSql = "show create table " + tableName.getNameAsString() + "$" + cf1;
839+
System.out.println("execute sql: " + selectSql);
840+
java.sql.ResultSet resultSet = conn.createStatement().executeQuery(selectSql);
841+
ResultSetPrinter.print(resultSet);
842+
843+
selectSql = "show create table " + tableName.getNameAsString() + "$" + cf2;
844+
System.out.println("execute sql: " + selectSql);
845+
resultSet = conn.createStatement().executeQuery(selectSql);
846+
ResultSetPrinter.print(resultSet);
847+
848+
selectSql = "show create table " + tableName.getNameAsString() + "$" + cf3;
849+
System.out.println("execute sql: " + selectSql);
850+
resultSet = conn.createStatement().executeQuery(selectSql);
851+
ResultSetPrinter.print(resultSet);
852+
853+
854+
// 4. test put/get some data
855+
Table table = connection.getTable(tableName);
856+
Put put = new Put(toBytes("Key" + 1));
857+
put.addColumn(cf1, "c1".getBytes(), "hello world".getBytes());
858+
put.addColumn(cf2, "c2".getBytes(), "hello world".getBytes());
859+
put.addColumn(cf3, "c3".getBytes(), "hello world".getBytes());
860+
table.put(put);
861+
862+
Scan scan = new Scan();
863+
ResultScanner resultScanner = table.getScanner(scan);
864+
List<Cell> cells = getCellsFromScanner(resultScanner);
865+
Assert.assertEquals(3, cells.size());
866+
867+
// 5. disable and delete table
868+
admin.disableTable(tableName);
869+
admin.deleteTable(tableName);
870+
871+
// 5. test table exists after delete
872+
admin.tableExists(tableName);
873+
874+
// 6. recreate and delete table
875+
admin.createTable(htd);
876+
admin.disableTable(tableName);
877+
admin.deleteTable(tableName);
878+
}
879+
880+
void testConcurCreateDelTablesHelper(List<TableName> tableNames, Boolean ignoreException) throws Exception {
881+
Configuration conf = ObHTableTestUtil.newConfiguration();
882+
Connection connection = ConnectionFactory.createConnection(conf);
883+
Admin admin = connection.getAdmin();
884+
int tableNums = tableNames.size();
885+
886+
// use some column family desc
887+
byte[] cf1 = Bytes.toBytes("cf1");
888+
byte[] cf2 = Bytes.toBytes("cf2");
889+
byte[] cf3 = Bytes.toBytes("cf3");
890+
HColumnDescriptor hcd1 = new HColumnDescriptor(cf1);
891+
hcd1.setMaxVersions(2);
892+
hcd1.setTimeToLive(172800);
893+
HColumnDescriptor hcd2 = new HColumnDescriptor(cf2);
894+
hcd1.setMaxVersions(1);
895+
hcd1.setTimeToLive(86400);
896+
HColumnDescriptor hcd3 = new HColumnDescriptor(cf3);
897+
898+
// 1. generate create table task, one task per table
899+
List<Callable<Void>> tasks = new ArrayList<>();
900+
for (int i = 0; i < tableNums; i++) {
901+
int finalI = i;
902+
tasks.add(()->{
903+
HTableDescriptor htd = new HTableDescriptor(tableNames.get(finalI));
904+
htd.addFamily(hcd1);
905+
htd.addFamily(hcd2);
906+
htd.addFamily(hcd3);
907+
try {
908+
admin.createTable(htd);
909+
} catch (Exception e) {
910+
System.out.println(e);
911+
if (!ignoreException) {
912+
throw e;
913+
}
914+
}
915+
return null;
916+
});
917+
}
918+
919+
// 2. execute concurrent create table tasks
920+
ExecutorService executorService = Executors.newFixedThreadPool(tableNums);
921+
executorService.invokeAll(tasks);
922+
executorService.shutdown();
923+
executorService.awaitTermination(1, TimeUnit.MINUTES);
924+
925+
// 3. check table create success
926+
for (int i = 0; i < tableNames.size(); i++) {
927+
TableName tableName = tableNames.get(i);
928+
assertTrue(admin.tableExists(tableName));
929+
}
930+
931+
// 4. test put/get/delete some data
932+
for (int i = 0; i < tableNames.size(); i++) {
933+
Table table = connection.getTable(tableNames.get(i));
934+
Put put = new Put(toBytes("Key" + 1));
935+
put.addColumn(cf1, "c1".getBytes(), "hello world".getBytes());
936+
put.addColumn(cf2, "c2".getBytes(), "hello world".getBytes());
937+
put.addColumn(cf3, "c3".getBytes(), "hello world".getBytes());
938+
table.put(put);
939+
940+
Scan scan = new Scan();
941+
ResultScanner resultScanner = table.getScanner(scan);
942+
List<Cell> cells = getCellsFromScanner(resultScanner);
943+
Assert.assertEquals(3, cells.size());
944+
945+
table.delete(new Delete(toBytes("Key" + 1)));
946+
}
947+
948+
// 4. disable all tables;
949+
for (int i = 0; i < tableNames.size(); i++) {
950+
TableName tableName = tableNames.get(i);
951+
admin.disableTable(tableName);
952+
}
953+
954+
// 5. generate delete table task
955+
List<Callable<Void>> delTasks = new ArrayList<>();
956+
for (int i = 0; i < tableNums; i++) {
957+
int finalI = i;
958+
delTasks.add(()->{
959+
try {
960+
admin.deleteTable(tableNames.get(finalI));
961+
} catch (Exception e) {
962+
System.out.println(e);
963+
if (!ignoreException) {
964+
throw e;
965+
}
966+
}
967+
return null;
968+
});
969+
}
970+
971+
// 6. execute concurrent delete table tasks
972+
ExecutorService delExecutorService = Executors.newFixedThreadPool(tableNums);
973+
delExecutorService.invokeAll(delTasks);
974+
delExecutorService.shutdown();
975+
delExecutorService.awaitTermination(1, TimeUnit.MINUTES);
976+
977+
// 7. check table deletion success
978+
for (int i = 0; i < tableNames.size(); i++) {
979+
TableName tableName = tableNames.get(i);
980+
assertFalse(admin.tableExists(tableName));
981+
}
982+
}
983+
984+
// 2. test concurrent create or delete different table
985+
// step1: create different tables concurrently, it must succeed
986+
// step2: execute put/read/delete on created table
987+
// step3: delete different tables concurrently, it must succeed
988+
@Test
989+
public void testConcurCreateDelTables() throws Exception {
990+
final int tableNums = 20;
991+
List<TableName> tableNames = new ArrayList<>();
992+
for (int i = 0; i < tableNums; i++) {
993+
tableNames.add(TableName.valueOf("testConcurCreateTable" + i));
994+
}
995+
testConcurCreateDelTablesHelper(tableNames, false);
996+
}
997+
998+
// 3. test concurrent create or delete same table
999+
// step1: create one table concurrently, only one table was successfully created
1000+
// step2: execute put/read/delete on created table
1001+
// step3: delete one table concurrently, the table will be deleted successfully
1002+
@Test
1003+
public void testConcurCreateOneTable() throws Exception {
1004+
final int taskNum = 20;
1005+
TableName tableName = TableName.valueOf("testConcurCreateOneTable");
1006+
List<TableName> tableNames = new ArrayList<>();
1007+
for (int i = 0; i < taskNum; i++) {
1008+
tableNames.add(tableName);
1009+
}
1010+
testConcurCreateDelTablesHelper(tableNames, true);
1011+
}
1012+
1013+
// 4. test the performance of concurrent create/delete table
1014+
@Test
1015+
public void testConcurCreateDelPerf() throws Exception {
1016+
final int tableNums = 100;
1017+
List<TableName> tableNames = new ArrayList<>();
1018+
Configuration conf = ObHTableTestUtil.newConfiguration();
1019+
Connection connection = ConnectionFactory.createConnection(conf);
1020+
Admin admin = connection.getAdmin();
1021+
byte[] cf1 = Bytes.toBytes("cf1");
1022+
byte[] cf2 = Bytes.toBytes("cf2");
1023+
byte[] cf3 = Bytes.toBytes("cf3");
1024+
HColumnDescriptor hcd1 = new HColumnDescriptor(cf1);
1025+
hcd1.setMaxVersions(2);
1026+
hcd1.setTimeToLive(172800);
1027+
HColumnDescriptor hcd2 = new HColumnDescriptor(cf2);
1028+
hcd1.setMaxVersions(1);
1029+
hcd1.setTimeToLive(86400);
1030+
HColumnDescriptor hcd3 = new HColumnDescriptor(cf3);
1031+
1032+
for (int i = 0; i < tableNums; i++) {
1033+
tableNames.add(TableName.valueOf("testConcurCreateDelPerf" + i));
1034+
}
1035+
1036+
List<Callable<Void>> tasks = new ArrayList<>();
1037+
for (int i = 0; i < tableNums; i++) {
1038+
int finalI = i;
1039+
tasks.add(()->{
1040+
HTableDescriptor htd = new HTableDescriptor(tableNames.get(finalI));
1041+
htd.addFamily(hcd1);
1042+
htd.addFamily(hcd2);
1043+
htd.addFamily(hcd3);
1044+
try {
1045+
admin.createTable(htd);
1046+
} catch (Exception e) {
1047+
System.out.println(e);
1048+
}
1049+
return null;
1050+
});
1051+
}
1052+
1053+
// 2. execute concurrent create table tasks
1054+
long start = System.currentTimeMillis();
1055+
ExecutorService executorService = Executors.newFixedThreadPool(tableNums);
1056+
executorService.invokeAll(tasks);
1057+
executorService.shutdown();
1058+
executorService.awaitTermination(2, TimeUnit.MINUTES);
1059+
long duration = System.currentTimeMillis() - start;
1060+
System.out.println("create " + tableNums + " tables cost " + duration + " ms.");
1061+
1062+
// 3. disable all tables;
1063+
for (int i = 0; i < tableNames.size(); i++) {
1064+
TableName tableName = tableNames.get(i);
1065+
admin.disableTable(tableName);
1066+
}
1067+
1068+
// 4. generate delete table task
1069+
List<Callable<Void>> delTasks = new ArrayList<>();
1070+
for (int i = 0; i < tableNums; i++) {
1071+
int finalI = i;
1072+
delTasks.add(()->{
1073+
try {
1074+
admin.deleteTable(tableNames.get(finalI));
1075+
} catch (Exception e) {
1076+
System.out.println(e);
1077+
}
1078+
return null;
1079+
});
1080+
}
1081+
1082+
// 6. execute concurrent delete table tasks
1083+
start = System.currentTimeMillis();
1084+
ExecutorService delExecutorService = Executors.newFixedThreadPool(tableNums);
1085+
delExecutorService.invokeAll(delTasks);
1086+
delExecutorService.shutdown();
1087+
delExecutorService.awaitTermination(1, TimeUnit.MINUTES);
1088+
duration = System.currentTimeMillis() - start;
1089+
System.out.println("delete " + tableNums + " tables cost " + duration + " ms.");
1090+
}
8051091
}

0 commit comments

Comments
 (0)