Skip to content

Commit 4ec96cd

Browse files
committed
[Test] add defensive test for htable ddl
1 parent d192cd4 commit 4ec96cd

File tree

3 files changed

+225
-32
lines changed

3 files changed

+225
-32
lines changed

src/main/java/com/alipay/oceanbase/hbase/util/OHAdmin.java

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,19 @@ public TableDescriptor getDescriptor(TableName tableName) throws IOException {
174174

175175
@Override
176176
public void createTable(TableDescriptor tableDescriptor) throws IOException {
177-
throw new FeatureNotSupportedException("does not support yet");
178-
// OHConnectionConfiguration connectionConf = new OHConnectionConfiguration(conf);
179-
// ObTableClient tableClient = ObTableClientManager.getOrCreateObTableClientByTableName(tableDescriptor.getTableName(), connectionConf);
180-
// OHCreateTableExecutor executor = new OHCreateTableExecutor(tableClient);
181-
// try {
182-
// executor.createTable(tableDescriptor, null);
183-
// } catch (IOException e) {
184-
// if (e.getCause() instanceof ObTableTransportException
185-
// && ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
186-
// throw new TimeoutIOException(e.getCause());
187-
// } else {
188-
// throw e;
189-
// }
190-
// }
177+
OHConnectionConfiguration connectionConf = new OHConnectionConfiguration(conf);
178+
ObTableClient tableClient = ObTableClientManager.getOrCreateObTableClientByTableName(tableDescriptor.getTableName(), connectionConf);
179+
OHCreateTableExecutor executor = new OHCreateTableExecutor(tableClient);
180+
try {
181+
executor.createTable(tableDescriptor, null);
182+
} catch (IOException e) {
183+
if (e.getCause() instanceof ObTableTransportException
184+
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
185+
throw new TimeoutIOException(e.getCause());
186+
} else {
187+
throw e;
188+
}
189+
}
191190
}
192191

193192
@Override
@@ -207,20 +206,19 @@ public Future<Void> createTableAsync(TableDescriptor tableDescriptor, byte[][] b
207206

208207
@Override
209208
public void deleteTable(TableName tableName) throws IOException {
210-
throw new FeatureNotSupportedException("does not support yet");
211-
// OHConnectionConfiguration connectionConf = new OHConnectionConfiguration(conf);
212-
// ObTableClient tableClient = ObTableClientManager.getOrCreateObTableClientByTableName(tableName, connectionConf);
213-
// OHDeleteTableExecutor executor = new OHDeleteTableExecutor(tableClient);
214-
// try {
215-
// executor.deleteTable(tableName.getNameAsString());
216-
// } catch (IOException e) {
217-
// if (e.getCause() instanceof ObTableTransportException
218-
// && ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
219-
// throw new TimeoutIOException(e.getCause());
220-
// } else {
221-
// throw e;
222-
// }
223-
// }
209+
OHConnectionConfiguration connectionConf = new OHConnectionConfiguration(conf);
210+
ObTableClient tableClient = ObTableClientManager.getOrCreateObTableClientByTableName(tableName, connectionConf);
211+
OHDeleteTableExecutor executor = new OHDeleteTableExecutor(tableClient);
212+
try {
213+
executor.deleteTable(tableName.getNameAsString());
214+
} catch (IOException e) {
215+
if (e.getCause() instanceof ObTableTransportException
216+
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
217+
throw new TimeoutIOException(e.getCause());
218+
} else {
219+
throw e;
220+
}
221+
}
224222
}
225223

226224
@Override

src/main/java/com/alipay/oceanbase/hbase/util/OHCreateTableExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void createTable(TableDescriptor tableDescriptor, byte[][] splitKeys) thr
5353
final ObTableMetaRequest request = new ObTableMetaRequest();
5454
request.setMetaType(getMetaType());
5555
Map<String, Object> requestData = new HashMap<>();
56-
requestData.put("htable_name", tableDescriptor.getTableName().getName());
56+
requestData.put("htable_name", tableDescriptor.getTableName().getNameAsString());
5757
Map<String, Map<String, Integer>> columnFamilies = new HashMap<>();
5858
for (ColumnFamilyDescriptor columnDescriptor : tableDescriptor.getColumnFamilies()) {
5959
Map<String, Integer> columnFamily = new HashMap<>();

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

Lines changed: 198 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636

3737
import java.io.IOException;
38+
import java.sql.SQLException;
3839
import java.sql.Statement;
3940
import java.util.ArrayList;
4041
import java.util.LinkedList;
@@ -780,17 +781,17 @@ public void testCreateDeleteTable() throws Exception {
780781
assertTrue(admin.tableExists(tableName));
781782
// TODO: show create table, need to be replace by getDescriptor
782783
java.sql.Connection conn = ObHTableTestUtil.getConnection();
783-
String selectSql = "show create table " + tableName.getNameAsString() + "$" + cf1;
784+
String selectSql = "show create table " + tableName.getNameAsString() + "$" + Bytes.toString(cf1);
784785
System.out.println("execute sql: " + selectSql);
785786
java.sql.ResultSet resultSet = conn.createStatement().executeQuery(selectSql);
786787
ResultSetPrinter.print(resultSet);
787788

788-
selectSql = "show create table " + tableName.getNameAsString() + "$" + cf2;
789+
selectSql = "show create table " + tableName.getNameAsString() + "$" + Bytes.toString(cf2);
789790
System.out.println("execute sql: " + selectSql);
790791
resultSet = conn.createStatement().executeQuery(selectSql);
791792
ResultSetPrinter.print(resultSet);
792793

793-
selectSql = "show create table " + tableName.getNameAsString() + "$" + cf3;
794+
selectSql = "show create table " + tableName.getNameAsString() + "$" + Bytes.toString(cf3);
794795
System.out.println("execute sql: " + selectSql);
795796
resultSet = conn.createStatement().executeQuery(selectSql);
796797
ResultSetPrinter.print(resultSet);
@@ -1033,4 +1034,198 @@ public void testConcurCreateDelPerf() throws Exception {
10331034
duration = System.currentTimeMillis() - start;
10341035
System.out.println("delete " + tableNums + " tables cost " + duration + " ms.");
10351036
}
1037+
1038+
@Test
1039+
public void testHTableDDLDefense() throws Exception {
1040+
TableName tableName = TableName.valueOf("testHTableDefense");
1041+
byte[] cf1 = Bytes.toBytes("cf1");
1042+
byte[] cf2 = Bytes.toBytes("cf2");
1043+
Configuration conf = ObHTableTestUtil.newConfiguration();
1044+
Connection connection = ConnectionFactory.createConnection(conf);
1045+
Admin admin = connection.getAdmin();
1046+
1047+
// 1. construct htable desc and column family desc
1048+
HColumnDescriptor hcd1 = new HColumnDescriptor(cf1);
1049+
hcd1.setMaxVersions(2);
1050+
hcd1.setTimeToLive(172800);
1051+
HColumnDescriptor hcd2 = new HColumnDescriptor(cf2);
1052+
hcd1.setMaxVersions(1);
1053+
hcd1.setTimeToLive(86400);
1054+
java.sql.Connection conn = ObHTableTestUtil.getConnection();
1055+
1056+
// 2. execute create table and check exists
1057+
try {
1058+
HTableDescriptor htd = new HTableDescriptor(tableName);
1059+
htd.addFamily(hcd1);
1060+
htd.addFamily(hcd2);
1061+
admin.createTable(htd);
1062+
assertTrue(admin.tableExists(tableName));
1063+
1064+
/// execute the following ddl stmt in created by admin table, should be prohibited
1065+
// 4. alter table add constraint
1066+
try {
1067+
String sql = "alter table testHTableDefense$cf1 ADD CONSTRAINT cons1 CHECK(T < 0)";
1068+
System.out.println("execute sql: " + sql);
1069+
conn.createStatement().execute(sql);
1070+
} catch (SQLException e) {
1071+
e.printStackTrace();
1072+
Assert.assertEquals(1235, e.getErrorCode());
1073+
Assert.assertEquals("user ddl with created_by attribute not supported", e.getMessage());
1074+
}
1075+
1076+
// 5. alter table add index
1077+
try {
1078+
String sql = "alter table testHTableDefense$cf1 ADD INDEX idx_1(T)";
1079+
System.out.println("execute sql: " + sql);
1080+
conn.createStatement().execute(sql);
1081+
} catch (SQLException e) {
1082+
e.printStackTrace();
1083+
Assert.assertEquals(1235, e.getErrorCode());
1084+
Assert.assertEquals("user ddl with created_by attribute not supported", e.getMessage());
1085+
}
1086+
1087+
// 5. alter table add fk
1088+
try {
1089+
String sql = "alter table testHTableDefense$cf1 MODIFY COLUMN V LONGTEXT";
1090+
System.out.println("execute sql: " + sql);
1091+
conn.createStatement().execute(sql);
1092+
} catch (SQLException e) {
1093+
e.printStackTrace();
1094+
Assert.assertEquals(1235, e.getErrorCode());
1095+
Assert.assertEquals("user ddl with created_by attribute not supported", e.getMessage());
1096+
}
1097+
1098+
// 6. alter table modify column to lob
1099+
try {
1100+
String sql = "alter table testHTableDefense$cf1 ADD CONSTRAINT hbase_fk_1 FOREIGN KEY(K) REFERENCES testHTableDefense$cf2(K)";
1101+
System.out.println("execute sql: " + sql);
1102+
conn.createStatement().execute(sql);
1103+
} catch (SQLException e) {
1104+
e.printStackTrace();
1105+
Assert.assertEquals(1235, e.getErrorCode());
1106+
Assert.assertEquals("user ddl with created_by attribute not supported", e.getMessage());
1107+
}
1108+
1109+
// 7. create trigger
1110+
try {
1111+
String sql = " CREATE TRIGGER hbase_trigger_1" +
1112+
" AFTER INSERT ON testHTableDefense$cf1 FOR EACH ROW" +
1113+
" BEGIN END";
1114+
System.out.println("execute sql: " + sql);
1115+
conn.createStatement().execute(sql);
1116+
} catch (SQLException e) {
1117+
e.printStackTrace();
1118+
Assert.assertEquals(1235, e.getErrorCode());
1119+
Assert.assertEquals("user ddl with created_by attribute not supported", e.getMessage());
1120+
}
1121+
1122+
// 8. create view
1123+
try {
1124+
String sql = " CREATE VIEW hbase_view_1 as select * from testHTableDefense$cf1";
1125+
System.out.println("execute sql: " + sql);
1126+
conn.createStatement().execute(sql);
1127+
} catch (SQLException e) {
1128+
e.printStackTrace();
1129+
Assert.assertEquals(1235, e.getErrorCode());
1130+
Assert.assertEquals("user ddl with created_by attribute not supported", e.getMessage());
1131+
}
1132+
1133+
// 9. alter view
1134+
try {
1135+
String sql = "ALTER VIEW hbase_view_1 as select * from testHTableDefense$cf1";
1136+
System.out.println("execute sql: " + sql);
1137+
conn.createStatement().execute(sql);
1138+
} catch (SQLException e) {
1139+
e.printStackTrace();
1140+
Assert.assertEquals(1235, e.getErrorCode());
1141+
Assert.assertEquals("user ddl with created_by attribute not supported", e.getMessage());
1142+
}
1143+
1144+
// 10. create index
1145+
try {
1146+
String sql = " CREATE INDEX testHTableDefense$cf1_idx_T on testHTableDefense$cf1(T)";
1147+
System.out.println("execute sql: " + sql);
1148+
conn.createStatement().execute(sql);
1149+
} catch (SQLException e) {
1150+
e.printStackTrace();
1151+
Assert.assertEquals(1235, e.getErrorCode());
1152+
Assert.assertEquals("user ddl with created_by attribute not supported", e.getMessage());
1153+
}
1154+
1155+
1156+
// 11. explicit create table and specify created_by:admin, should be prohibited
1157+
try {
1158+
String sql = "CREATE TABLE testHTableDefense$cf3(a int primary key) kv_attributes ='{\"Hbase\": {\"CREATED_BY\": \"ADMIN\"}}'";
1159+
System.out.println("execute sql: " + sql);
1160+
conn.createStatement().execute(sql);
1161+
} catch (SQLException e) {
1162+
e.printStackTrace();
1163+
Assert.assertEquals(1235, e.getErrorCode());
1164+
Assert.assertEquals("user ddl with created_by attribute not supported", e.getMessage());
1165+
}
1166+
1167+
// 12. alter table to created_by:admin, should be prohibited
1168+
try {
1169+
String sql1 = "CREATE TABLE testHTableDefense$cf3(a int primary key)";
1170+
System.out.println("execute sql: " + sql1);
1171+
conn.createStatement().execute(sql1);
1172+
String sql2 = "alter table testHTableDefense$cf3 kv_attributes ='{\"Hbase\": {\"CREATED_BY\": \"ADMIN\"}}'";
1173+
System.out.println("execute sql: " + sql2);
1174+
conn.createStatement().execute(sql2);
1175+
} catch (SQLException e) {
1176+
e.printStackTrace();
1177+
Assert.assertEquals(1235, e.getErrorCode());
1178+
Assert.assertEquals("alter table kv attributes to created by admin not supported", e.getMessage());
1179+
// clean table
1180+
String sql3 = "drop table if exists testHTableDefense$cf3";
1181+
System.out.println("execute sql: " + sql3);
1182+
conn.createStatement().execute(sql3);
1183+
}
1184+
1185+
// 13. disable a htable did not created by admin is not suppported
1186+
try {
1187+
String sql1 = "CREATE TABLEGROUP IF NOT EXISTS testHTableDefense2";
1188+
System.out.println("execute sql: " + sql1);
1189+
conn.createStatement().execute(sql1);
1190+
String sql2 = "CREATE TABLE IF NOT EXISTS testHTableDefense2$cf4(a int primary key) kv_attributes ='{\"Hbase\": {}}' TABLEGROUP=testHTableDefense2";
1191+
System.out.println("execute sql: " + sql2);
1192+
conn.createStatement().execute(sql2);
1193+
admin.disableTable(TableName.valueOf("testHTableDefense2"));
1194+
} catch (Exception e) {
1195+
e.printStackTrace();
1196+
Assert.assertEquals(-4007, ((ObTableException)e.getCause()).getErrorCode());
1197+
1198+
}
1199+
1200+
// 14. delete a htable did not created by admin is not suppported
1201+
try {
1202+
String sql1 = "CREATE TABLEGROUP IF NOT EXISTS testHTableDefense2";
1203+
System.out.println("execute sql: " + sql1);
1204+
conn.createStatement().execute(sql1);
1205+
String sql2 = "CREATE TABLE IF NOT EXISTS testHTableDefense2$cf5(a int primary key) kv_attributes ='{\"Hbase\": {}}' TABLEGROUP=testHTableDefense2";
1206+
System.out.println("execute sql: " + sql2);
1207+
conn.createStatement().execute(sql2);
1208+
admin.deleteTable(TableName.valueOf("testHTableDefense2"));
1209+
} catch (Exception e) {
1210+
e.printStackTrace();
1211+
Assert.assertEquals(-4007, ((ObTableException)e.getCause()).getErrorCode());
1212+
}
1213+
1214+
} catch (Exception e) {
1215+
e.printStackTrace();
1216+
assertTrue(false);
1217+
} finally {
1218+
admin.disableTable(tableName);
1219+
admin.deleteTable(tableName);
1220+
String sql1 = "DROP TABLE IF EXISTS testHTableDefense2$cf4";
1221+
System.out.println("execute sql: " + sql1);
1222+
conn.createStatement().execute(sql1);
1223+
String sql2 = "DROP TABLE IF EXISTS testHTableDefense2$cf5";
1224+
System.out.println("execute sql: " + sql2);
1225+
conn.createStatement().execute(sql2);
1226+
String sql3 = "DROP TABLEGROUP IF EXISTS testHTableDefense2";
1227+
System.out.println("execute sql: " + sql3);
1228+
conn.createStatement().execute(sql3);
1229+
}
1230+
}
10361231
}

0 commit comments

Comments
 (0)