|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * OBKV HBase Client Framework |
| 4 | + * %% |
| 5 | + * Copyright (C) 2022 OceanBase Group |
| 6 | + * %% |
| 7 | + * OBKV HBase Client Framework is licensed under Mulan PSL v2. |
| 8 | + * You can use this software according to the terms and conditions of the Mulan PSL v2. |
| 9 | + * You may obtain a copy of Mulan PSL v2 at: |
| 10 | + * http://license.coscl.org.cn/MulanPSL2 |
| 11 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, |
| 12 | + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, |
| 13 | + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. |
| 14 | + * See the Mulan PSL v2 for more details. |
| 15 | + * #L% |
| 16 | + */ |
| 17 | + |
| 18 | +package com.alipay.oceanbase.hbase; |
| 19 | + |
| 20 | +import org.apache.hadoop.conf.Configuration; |
| 21 | +import org.apache.hadoop.hbase.client.*; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import java.sql.*; |
| 26 | + |
| 27 | +import static com.alipay.oceanbase.hbase.ObHTableTestUtil.*; |
| 28 | +import static com.alipay.oceanbase.hbase.constants.OHConstants.*; |
| 29 | +import static org.apache.hadoop.hbase.util.Bytes.toBytes; |
| 30 | +import static org.junit.Assert.*; |
| 31 | + |
| 32 | +public class OHTableModeTest extends HTableTestBase { |
| 33 | + public static String tableName = "test"; |
| 34 | + public static String mysqlCompatMode = "mysql"; |
| 35 | + public static String oracleCompatMode = "oracle"; |
| 36 | + |
| 37 | + public static String allKVMode = "ALL"; |
| 38 | + public static String tableKVMode = "TABLEAPI"; |
| 39 | + public static String hbaseKVMode = "HBASE"; |
| 40 | + public static String RedisKVMode = "REDIS"; |
| 41 | + public static String noneKVMode = "NONE"; |
| 42 | + |
| 43 | + public static String tpUnit = "tpUnit"; |
| 44 | + public static String tpPool = "tpPool"; |
| 45 | + public static String tpTenant = "tpTenant"; |
| 46 | + |
| 47 | + public static String hbaseUnit = "hbaseUnit"; |
| 48 | + public static String hbasePool = "hbasePool"; |
| 49 | + public static String hbaseTenant = "hbaseTenant"; |
| 50 | + |
| 51 | + public static String tableUnit = "tableUnit"; |
| 52 | + public static String tablePool = "tablePool"; |
| 53 | + public static String tableTenant = "tableTenant"; |
| 54 | + |
| 55 | + public static String redisUnit = "redisUnit"; |
| 56 | + public static String redisPool = "redisPool"; |
| 57 | + public static String redisTenant = "redisTenant"; |
| 58 | + |
| 59 | + @Before |
| 60 | + public void setup() throws Exception { |
| 61 | + } |
| 62 | + |
| 63 | + public static String extractUserName(String input) { |
| 64 | + int atSymbolIndex = input.indexOf("@"); |
| 65 | + if (atSymbolIndex != -1) { |
| 66 | + return input.substring(0, atSymbolIndex); |
| 67 | + } else { |
| 68 | + return ""; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public static String extractClusterName(String input) { |
| 73 | + int hashSymbolIndex = input.lastIndexOf("#"); |
| 74 | + if (hashSymbolIndex != -1) { |
| 75 | + return input.substring(hashSymbolIndex + 1); |
| 76 | + } else { |
| 77 | + return ""; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public void createTable(String userName, String tenantName) throws Exception { |
| 82 | + String user = userName + "@" + tenantName; |
| 83 | + String url = "jdbc:mysql://" + JDBC_IP + ":" + JDBC_PORT + "/ " + "test" + "?" + |
| 84 | + "rewriteBatchedStatements=TRUE&" + |
| 85 | + "allowMultiQueries=TRUE&" + |
| 86 | + "useLocalSessionState=TRUE&" + |
| 87 | + "useUnicode=TRUE&" + |
| 88 | + "characterEncoding=utf-8&" + |
| 89 | + "socketTimeout=3000000&" + |
| 90 | + "connectTimeout=60000"; |
| 91 | + Connection conn = DriverManager.getConnection(url, user, PASSWORD); |
| 92 | + Statement statement = conn.createStatement(); |
| 93 | + statement.execute("CREATE TABLE IF NOT EXISTS `test$family1` (" + |
| 94 | + " `K` varbinary(1024) NOT NULL," + |
| 95 | + " `Q` varbinary(256) NOT NULL," + |
| 96 | + " `T` bigint(20) NOT NULL," + |
| 97 | + " `V` varbinary(1024) DEFAULT NULL," + |
| 98 | + " PRIMARY KEY (`K`, `Q`, `T`)" + |
| 99 | + ");"); |
| 100 | + } |
| 101 | + |
| 102 | + public void createResourceUnit(String unitName) throws Exception { |
| 103 | + Connection conn = ObHTableTestUtil.getSysConnection(); |
| 104 | + Statement statement = conn.createStatement(); |
| 105 | + statement.execute("create resource unit " + unitName + " max_cpu 1, memory_size '1G';"); |
| 106 | + } |
| 107 | + |
| 108 | + public void createResourcePool(String unitName, String poolName) throws Exception { |
| 109 | + createResourceUnit(unitName); |
| 110 | + Connection conn = ObHTableTestUtil.getSysConnection(); |
| 111 | + Statement statement = conn.createStatement(); |
| 112 | + statement.execute("create resource pool " + poolName + " unit = '" + unitName + "', unit_num = 1;"); |
| 113 | + } |
| 114 | + |
| 115 | + public void createTenant(String unitName, String poolName, String tenantName, String compatMode, String kvMode) throws Exception { |
| 116 | + createResourcePool(unitName, poolName); |
| 117 | + Connection conn = ObHTableTestUtil.getSysConnection(); |
| 118 | + Statement statement = conn.createStatement(); |
| 119 | + statement.execute("create tenant " + tenantName + " replica_num = 1, resource_pool_list=('"+ poolName +"') " + |
| 120 | + "set ob_tcp_invited_nodes='%', ob_compatibility_mode='" + compatMode + "', ob_kv_mode='" + kvMode + "';"); |
| 121 | + } |
| 122 | + |
| 123 | + public void dropResourceUnit(String unitName) throws Exception { |
| 124 | + Connection conn = ObHTableTestUtil.getSysConnection(); |
| 125 | + Statement statement = conn.createStatement(); |
| 126 | + statement.execute("drop resource unit if exists " + unitName + ";"); |
| 127 | + } |
| 128 | + |
| 129 | + public void dropResourcePool(String poolName) throws Exception { |
| 130 | + Connection conn = ObHTableTestUtil.getSysConnection(); |
| 131 | + Statement statement = conn.createStatement(); |
| 132 | + statement.execute("drop resource pool if exists " + poolName + ";"); |
| 133 | + } |
| 134 | + |
| 135 | + public void dropTenant(String tenantName) throws Exception { |
| 136 | + Connection conn = ObHTableTestUtil.getSysConnection(); |
| 137 | + Statement statement = conn.createStatement(); |
| 138 | + statement.execute("drop tenant if exists " + tenantName + " force;"); |
| 139 | + } |
| 140 | + |
| 141 | + public static Configuration newConfiguration(String fullName) { |
| 142 | + Configuration conf = new Configuration(); |
| 143 | + conf.set(HBASE_OCEANBASE_FULL_USER_NAME, fullName); |
| 144 | + conf.set(HBASE_OCEANBASE_PASSWORD, PASSWORD); |
| 145 | + if (ODP_MODE) { |
| 146 | + // ODP mode |
| 147 | + conf.set(HBASE_OCEANBASE_ODP_ADDR, ODP_ADDR); |
| 148 | + conf.setInt(HBASE_OCEANBASE_ODP_PORT, ODP_PORT); |
| 149 | + conf.setBoolean(HBASE_OCEANBASE_ODP_MODE, ODP_MODE); |
| 150 | + conf.set(HBASE_OCEANBASE_DATABASE, DATABASE); |
| 151 | + } else { |
| 152 | + // OCP mode |
| 153 | + conf.set(HBASE_OCEANBASE_PARAM_URL, PARAM_URL); |
| 154 | + conf.set(HBASE_OCEANBASE_SYS_USER_NAME, SYS_USER_NAME); |
| 155 | + conf.set(HBASE_OCEANBASE_SYS_PASSWORD, SYS_PASSWORD); |
| 156 | + } |
| 157 | + return conf; |
| 158 | + } |
| 159 | + |
| 160 | + public OHTableClient createClient(String fullName) { |
| 161 | + return new OHTableClient(tableName, newConfiguration(fullName)); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void testTpTenant() throws Exception { |
| 166 | + try { |
| 167 | + createTenant(tpUnit, tpPool, tpTenant, mysqlCompatMode, noneKVMode); |
| 168 | + |
| 169 | + String tenantName = tpTenant; |
| 170 | + String userName = extractUserName(FULL_USER_NAME); |
| 171 | + String clusterName = extractClusterName(FULL_USER_NAME); |
| 172 | + String fullName = userName + "@" + tenantName + "#" + clusterName; |
| 173 | + |
| 174 | + createTable(userName, tenantName); |
| 175 | + |
| 176 | + hTable = createClient(fullName); |
| 177 | + ((OHTableClient) hTable).init(); |
| 178 | + |
| 179 | + Exception thrown = assertThrows( |
| 180 | + Exception.class, |
| 181 | + () -> { |
| 182 | + String key = "putKey"; |
| 183 | + String family = "family1"; |
| 184 | + String column = "putColumn1"; |
| 185 | + long timestamp = System.currentTimeMillis(); |
| 186 | + String value = "value"; |
| 187 | + Put put = new Put(toBytes(key)); |
| 188 | + put.add(family.getBytes(), column.getBytes(), timestamp, toBytes(value)); |
| 189 | + hTable.put(put); |
| 190 | + } |
| 191 | + ); |
| 192 | + System.out.println(thrown.getCause().getMessage()); |
| 193 | + assertTrue(thrown.getCause().getMessage().contains("[-4007][OB_NOT_SUPPORTED][As the ob_kv_mode variable has been set to 'NONE', your current interfaces not supported]")); |
| 194 | + } finally { |
| 195 | + dropTenant(tpTenant); |
| 196 | + dropResourcePool(tpPool); |
| 197 | + dropResourceUnit(tpUnit); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + public void testHbaseTenant() throws Exception { |
| 203 | + try { |
| 204 | + createTenant(hbaseUnit, hbasePool, hbaseTenant, mysqlCompatMode, hbaseKVMode); |
| 205 | + |
| 206 | + String tenantName = hbaseTenant; |
| 207 | + String userName = extractUserName(FULL_USER_NAME); |
| 208 | + String clusterName = extractClusterName(FULL_USER_NAME); |
| 209 | + String fullName = userName + "@" + tenantName + "#" + clusterName; |
| 210 | + |
| 211 | + createTable(userName, tenantName); |
| 212 | + |
| 213 | + hTable = createClient(fullName); |
| 214 | + ((OHTableClient) hTable).init(); |
| 215 | + |
| 216 | + String key = "putKey"; |
| 217 | + String family = "family1"; |
| 218 | + String column = "putColumn1"; |
| 219 | + long timestamp = System.currentTimeMillis(); |
| 220 | + String value = "value"; |
| 221 | + Put put = new Put(toBytes(key)); |
| 222 | + put.add(family.getBytes(), column.getBytes(), timestamp, toBytes(value)); |
| 223 | + hTable.put(put); |
| 224 | + } finally { |
| 225 | + dropTenant(hbaseTenant); |
| 226 | + dropResourcePool(hbasePool); |
| 227 | + dropResourceUnit(hbaseUnit); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + @Test |
| 232 | + public void testTableTenant() throws Exception { |
| 233 | + try { |
| 234 | + String tenantName = tableTenant; |
| 235 | + createTenant(tableUnit, tablePool, tenantName, mysqlCompatMode, tableKVMode); |
| 236 | + |
| 237 | + String userName = extractUserName(FULL_USER_NAME); |
| 238 | + String clusterName = extractClusterName(FULL_USER_NAME); |
| 239 | + String fullName = userName + "@" + tenantName + "#" + clusterName; |
| 240 | + |
| 241 | + createTable(userName, tenantName); |
| 242 | + |
| 243 | + hTable = createClient(fullName); |
| 244 | + ((OHTableClient) hTable).init(); |
| 245 | + |
| 246 | + Exception thrown = assertThrows( |
| 247 | + Exception.class, |
| 248 | + () -> { |
| 249 | + String key = "putKey"; |
| 250 | + String family = "family1"; |
| 251 | + String column = "putColumn1"; |
| 252 | + long timestamp = System.currentTimeMillis(); |
| 253 | + String value = "value"; |
| 254 | + Put put = new Put(toBytes(key)); |
| 255 | + put.add(family.getBytes(), column.getBytes(), timestamp, toBytes(value)); |
| 256 | + hTable.put(put); |
| 257 | + } |
| 258 | + ); |
| 259 | + System.out.println(thrown.getCause().getMessage()); |
| 260 | + assertTrue(thrown.getCause().getMessage().contains("[-4007][OB_NOT_SUPPORTED][As the ob_kv_mode variable has been set to 'TABLEAPI', your current interfaces not supported]")); |
| 261 | + } finally { |
| 262 | + dropTenant(tableTenant); |
| 263 | + dropResourcePool(tablePool); |
| 264 | + dropResourceUnit(tableUnit); |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + @Test |
| 269 | + public void testRedisTenant() throws Exception { |
| 270 | + try { |
| 271 | + String tenantName = redisTenant; |
| 272 | + createTenant(redisUnit, redisPool, tenantName, mysqlCompatMode, RedisKVMode); |
| 273 | + |
| 274 | + String userName = extractUserName(FULL_USER_NAME); |
| 275 | + String clusterName = extractClusterName(FULL_USER_NAME); |
| 276 | + String fullName = userName + "@" + tenantName + "#" + clusterName; |
| 277 | + |
| 278 | + createTable(userName, tenantName); |
| 279 | + |
| 280 | + hTable = createClient(fullName); |
| 281 | + ((OHTableClient) hTable).init(); |
| 282 | + |
| 283 | + Exception thrown = assertThrows( |
| 284 | + Exception.class, |
| 285 | + () -> { |
| 286 | + String key = "putKey"; |
| 287 | + String family = "family1"; |
| 288 | + String column = "putColumn1"; |
| 289 | + long timestamp = System.currentTimeMillis(); |
| 290 | + String value = "value"; |
| 291 | + Put put = new Put(toBytes(key)); |
| 292 | + put.add(family.getBytes(), column.getBytes(), timestamp, toBytes(value)); |
| 293 | + hTable.put(put); |
| 294 | + } |
| 295 | + ); |
| 296 | + System.out.println(thrown.getCause().getMessage()); |
| 297 | + assertTrue(thrown.getCause().getMessage().contains("[-4007][OB_NOT_SUPPORTED][As the ob_kv_mode variable has been set to 'REDIS', your current interfaces not supported]")); |
| 298 | + } finally { |
| 299 | + dropTenant(redisTenant); |
| 300 | + dropResourcePool(redisPool); |
| 301 | + dropResourceUnit(redisUnit); |
| 302 | + } |
| 303 | + } |
| 304 | +} |
0 commit comments