|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * com.oceanbase:obkv-table-client |
| 4 | + * %% |
| 5 | + * Copyright (C) 2021 - 2023 OceanBase |
| 6 | + * %% |
| 7 | + * OBKV Table 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.rpc; |
| 19 | + |
| 20 | +import com.alipay.oceanbase.rpc.mutation.*; |
| 21 | +import com.alipay.oceanbase.rpc.protocol.payload.ObPayload; |
| 22 | +import com.alipay.oceanbase.rpc.protocol.payload.impl.execute.mutate.ObTableQueryAndMutateRequest; |
| 23 | +import com.alipay.oceanbase.rpc.table.api.TableBatchOps; |
| 24 | +import com.alipay.oceanbase.rpc.table.api.TableQuery; |
| 25 | +import com.alipay.oceanbase.rpc.util.ObTableClientTestUtil; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import java.sql.Connection; |
| 30 | +import java.sql.PreparedStatement; |
| 31 | +import java.sql.ResultSet; |
| 32 | +import java.sql.Statement; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +import static com.alipay.oceanbase.rpc.mutation.MutationFactory.colVal; |
| 36 | +import static org.junit.Assert.*; |
| 37 | + |
| 38 | +/** |
| 39 | + CREATE TABLE IF NOT EXISTS `test_p99` ( |
| 40 | + `c1` bigint(20) NOT NULL, |
| 41 | + `c2` bigint(20) DEFAULT NULL, |
| 42 | + `c3` varchar(20) DEFAULT "hello", |
| 43 | + PRIMARY KEY (`c1`) |
| 44 | + ); |
| 45 | + **/ |
| 46 | +public class ObTableP99Test { |
| 47 | + ObTableClient client; |
| 48 | + private static Connection conn = null; |
| 49 | + public static String tableName = "test_p99"; |
| 50 | + public static String insertSqlType = "TABLEAPI INSERT"; |
| 51 | + public static String selectSqlType = "TABLEAPI SELECT"; |
| 52 | + public static String deleteSqlType = "TABLEAPI DELETE"; |
| 53 | + public static String updateSqlType = "TABLEAPI UPDATE"; |
| 54 | + public static String replaceSqlType = "TABLEAPI REPLACE"; |
| 55 | + public static String queryAndMutateSqlType = "TABLEAPI QUERY AND MUTATE"; |
| 56 | + public static String otherSqlType = "TABLEAPI OTHER"; |
| 57 | + |
| 58 | + @Before |
| 59 | + public void setup() throws Exception { |
| 60 | + final ObTableClient obTableClient = ObTableClientTestUtil.newTestClient(); |
| 61 | + obTableClient.init(); |
| 62 | + this.client = obTableClient; |
| 63 | + client.addRowKeyElement(tableName, new String[] { "c1" }); |
| 64 | + conn = ObTableClientTestUtil.getConnection(); |
| 65 | + } |
| 66 | + |
| 67 | + private static long getResultCount(String sqlType) throws Exception { |
| 68 | + PreparedStatement ps = conn.prepareStatement("select * from oceanbase.gv$ob_query_response_time_histogram " + |
| 69 | + "where sql_type=" + "\"" +sqlType +"\""); |
| 70 | + ResultSet rs = ps.executeQuery(); |
| 71 | + long totalCnt = 0L; |
| 72 | + while (rs.next()) { |
| 73 | + totalCnt += rs.getLong("count"); |
| 74 | + } |
| 75 | + ps.close(); |
| 76 | + return totalCnt; |
| 77 | + } |
| 78 | + |
| 79 | + private static void flushHistogram() throws Exception { |
| 80 | + Statement statement = conn.createStatement(); |
| 81 | + statement.execute("alter system set query_response_time_flush = true;"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testInsert() throws Exception { |
| 86 | + try { |
| 87 | + // single insert |
| 88 | + client.insert(tableName).setRowKey(colVal("c1", 1L)) |
| 89 | + .addMutateColVal(colVal("c2", 1L)) |
| 90 | + .execute(); |
| 91 | + assertEquals(1, getResultCount(insertSqlType)); |
| 92 | + client.insert(tableName).setRowKey(colVal("c1", 2L)) |
| 93 | + .addMutateColVal(colVal("c2", 1L)) |
| 94 | + .execute(); |
| 95 | + assertEquals(2, getResultCount(insertSqlType)); |
| 96 | + |
| 97 | + // single insertOrUpdate |
| 98 | + flushHistogram(); |
| 99 | + Thread.sleep(100); |
| 100 | + client.insertOrUpdate(tableName).setRowKey(colVal("c1", 1L)) |
| 101 | + .addMutateColVal(colVal("c2", 1L)) |
| 102 | + .execute(); |
| 103 | + assertEquals(1, getResultCount(insertSqlType)); |
| 104 | + client.insertOrUpdate(tableName).setRowKey(colVal("c1", 2L)) |
| 105 | + .addMutateColVal(colVal("c2", 1L)) |
| 106 | + .execute(); |
| 107 | + assertEquals(2, getResultCount(insertSqlType)); |
| 108 | + |
| 109 | + // multi insert |
| 110 | + flushHistogram(); |
| 111 | + Thread.sleep(100); |
| 112 | + BatchOperation batch1 = client.batchOperation(tableName); |
| 113 | + Insert ins_0 = client.insert(tableName).setRowKey(colVal("c1", 1L)) |
| 114 | + .addMutateColVal(colVal("c2", 1L)); |
| 115 | + batch1.addOperation(ins_0).execute(); |
| 116 | + assertEquals(1, getResultCount(insertSqlType)); |
| 117 | + batch1.addOperation(ins_0).execute(); |
| 118 | + assertEquals(2, getResultCount(insertSqlType)); |
| 119 | + |
| 120 | + // multi insertOrUpdate |
| 121 | + flushHistogram(); |
| 122 | + Thread.sleep(100); |
| 123 | + BatchOperation batch2 = client.batchOperation(tableName); |
| 124 | + InsertOrUpdate insUp_0 = client.insertOrUpdate(tableName).setRowKey(colVal("c1", 1L)) |
| 125 | + .addMutateColVal(colVal("c2", 1L)); |
| 126 | + batch2.addOperation(insUp_0).execute(); |
| 127 | + assertEquals(1, getResultCount(insertSqlType)); |
| 128 | + batch2.addOperation(insUp_0).execute(); |
| 129 | + assertEquals(2, getResultCount(insertSqlType)); |
| 130 | + |
| 131 | + } finally { |
| 132 | + client.delete(tableName).setRowKey(colVal("c1", 1L)).execute(); |
| 133 | + client.delete(tableName).setRowKey(colVal("c1", 2L)).execute(); |
| 134 | + flushHistogram(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testSelect() throws Exception { |
| 140 | + try { |
| 141 | + client.insert(tableName).setRowKey(colVal("c1", 1L)) |
| 142 | + .addMutateColVal(colVal("c2", 1L)) |
| 143 | + .execute(); |
| 144 | + assertEquals(1, getResultCount(insertSqlType)); |
| 145 | + |
| 146 | + // single get |
| 147 | + flushHistogram(); |
| 148 | + Thread.sleep(100); |
| 149 | + client.get(tableName, 1L, new String[]{"c1"}); |
| 150 | + assertEquals(1, getResultCount(selectSqlType)); |
| 151 | + client.get(tableName, 1L, new String[]{"c1"}); |
| 152 | + assertEquals(2, getResultCount(selectSqlType)); |
| 153 | + |
| 154 | + // multi get |
| 155 | + flushHistogram(); |
| 156 | + Thread.sleep(100); |
| 157 | + TableBatchOps batchOps = client.batch(tableName); |
| 158 | + batchOps.get(1L, new String[]{"c1"}); |
| 159 | + batchOps.execute(); |
| 160 | + assertEquals(1, getResultCount(selectSqlType)); |
| 161 | + batchOps.execute(); |
| 162 | + assertEquals(2, getResultCount(selectSqlType)); |
| 163 | + |
| 164 | + // sync query |
| 165 | + flushHistogram(); |
| 166 | + Thread.sleep(100); |
| 167 | + TableQuery tableQuery = client.query(tableName); |
| 168 | + tableQuery.addScanRange(new Object[] { 0L,}, new Object[] { 1L,}); |
| 169 | + tableQuery.select("c1"); |
| 170 | + tableQuery.execute(); |
| 171 | + assertEquals(1, getResultCount(selectSqlType)); |
| 172 | + tableQuery.execute(); |
| 173 | + assertEquals(2, getResultCount(selectSqlType)); |
| 174 | + |
| 175 | + // async query |
| 176 | + flushHistogram(); |
| 177 | + Thread.sleep(100); |
| 178 | + tableQuery.asyncExecute(); |
| 179 | + assertEquals(1, getResultCount(selectSqlType)); |
| 180 | + tableQuery.asyncExecute(); |
| 181 | + assertEquals(2, getResultCount(selectSqlType)); |
| 182 | + } finally { |
| 183 | + client.delete(tableName).setRowKey(colVal("c1", 1L)).execute(); |
| 184 | + flushHistogram(); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void testDelete() throws Exception { |
| 190 | + try { |
| 191 | + client.insert(tableName).setRowKey(colVal("c1", 1L)) |
| 192 | + .addMutateColVal(colVal("c2", 1L)) |
| 193 | + .execute(); |
| 194 | + client.insert(tableName).setRowKey(colVal("c1", 2L)) |
| 195 | + .addMutateColVal(colVal("c2", 1L)) |
| 196 | + .execute(); |
| 197 | + assertEquals(2, getResultCount(insertSqlType)); |
| 198 | + |
| 199 | + // single delete |
| 200 | + flushHistogram(); |
| 201 | + Thread.sleep(100); |
| 202 | + client.delete(tableName).setRowKey(colVal("c1", 1L)).execute(); |
| 203 | + assertEquals(1, getResultCount(deleteSqlType)); |
| 204 | + client.delete(tableName).setRowKey(colVal("c1", 2L)).execute(); |
| 205 | + assertEquals(2, getResultCount(deleteSqlType)); |
| 206 | + |
| 207 | + // multi delete |
| 208 | + flushHistogram(); |
| 209 | + Thread.sleep(100); |
| 210 | + BatchOperation batch = client.batchOperation(tableName); |
| 211 | + Delete del_0 = client.delete(tableName).setRowKey(colVal("c1", 1L)); |
| 212 | + batch.addOperation(del_0).execute(); |
| 213 | + assertEquals(1, getResultCount(deleteSqlType)); |
| 214 | + batch.addOperation(del_0).execute(); |
| 215 | + assertEquals(2, getResultCount(deleteSqlType)); |
| 216 | + } finally { |
| 217 | + client.delete(tableName).setRowKey(colVal("c1", 1L)).execute(); |
| 218 | + client.delete(tableName).setRowKey(colVal("c1", 2L)).execute(); |
| 219 | + flushHistogram(); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + @Test |
| 224 | + public void testUpdate() throws Exception { |
| 225 | + try { |
| 226 | + client.insert(tableName).setRowKey(colVal("c1", 1L)) |
| 227 | + .addMutateColVal(colVal("c2", 1L)) |
| 228 | + .execute(); |
| 229 | + client.insert(tableName).setRowKey(colVal("c1", 2L)) |
| 230 | + .addMutateColVal(colVal("c2", 1L)) |
| 231 | + .execute(); |
| 232 | + assertEquals(2, getResultCount(insertSqlType)); |
| 233 | + |
| 234 | + // single update |
| 235 | + flushHistogram(); |
| 236 | + Thread.sleep(100); |
| 237 | + client.update(tableName).setRowKey(colVal("c1", 1L)) |
| 238 | + .addMutateColVal(colVal("c2", 2L)) |
| 239 | + .execute(); |
| 240 | + assertEquals(1, getResultCount(updateSqlType)); |
| 241 | + client.update(tableName).setRowKey(colVal("c1", 2L)) |
| 242 | + .addMutateColVal(colVal("c2", 2L)) |
| 243 | + .execute(); |
| 244 | + assertEquals(2, getResultCount(updateSqlType)); |
| 245 | + |
| 246 | + // single increment |
| 247 | + flushHistogram(); |
| 248 | + Thread.sleep(100); |
| 249 | + client.increment(tableName).setRowKey(colVal("c1", 1L)) |
| 250 | + .addMutateColVal(colVal("c2", 2L)) |
| 251 | + .execute(); |
| 252 | + assertEquals(1, getResultCount(updateSqlType)); |
| 253 | + client.increment(tableName).setRowKey(colVal("c1", 2L)) |
| 254 | + .addMutateColVal(colVal("c2", 2L)) |
| 255 | + .execute(); |
| 256 | + assertEquals(2, getResultCount(updateSqlType)); |
| 257 | + |
| 258 | + // single append |
| 259 | + flushHistogram(); |
| 260 | + Thread.sleep(100); |
| 261 | + client.append(tableName).setRowKey(colVal("c1", 1L)) |
| 262 | + .addMutateColVal(colVal("c3", "world")) |
| 263 | + .execute(); |
| 264 | + assertEquals(1, getResultCount(updateSqlType)); |
| 265 | + client.append(tableName).setRowKey(colVal("c1", 2L)) |
| 266 | + .addMutateColVal(colVal("c3", "world")) |
| 267 | + .execute(); |
| 268 | + assertEquals(2, getResultCount(updateSqlType)); |
| 269 | + |
| 270 | + // multi update |
| 271 | + flushHistogram(); |
| 272 | + Thread.sleep(100); |
| 273 | + BatchOperation batch1 = client.batchOperation(tableName); |
| 274 | + Update ins_0 = client.update(tableName).setRowKey(colVal("c1", 1L)) |
| 275 | + .addMutateColVal(colVal("c2", 1L)); |
| 276 | + batch1.addOperation(ins_0).execute(); |
| 277 | + assertEquals(1, getResultCount(updateSqlType)); |
| 278 | + batch1.addOperation(ins_0).execute(); |
| 279 | + assertEquals(2, getResultCount(updateSqlType)); |
| 280 | + |
| 281 | + // multi increment |
| 282 | + flushHistogram(); |
| 283 | + Thread.sleep(100); |
| 284 | + BatchOperation batch2 = client.batchOperation(tableName); |
| 285 | + Increment inc_0 = client.increment(tableName).setRowKey(colVal("c1", 1L)) |
| 286 | + .addMutateColVal(colVal("c2", 1L)); |
| 287 | + batch2.addOperation(inc_0).execute(); |
| 288 | + assertEquals(1, getResultCount(updateSqlType)); |
| 289 | + batch2.addOperation(inc_0).execute(); |
| 290 | + assertEquals(2, getResultCount(updateSqlType)); |
| 291 | + |
| 292 | + // multi append |
| 293 | + flushHistogram(); |
| 294 | + Thread.sleep(100); |
| 295 | + BatchOperation batch3 = client.batchOperation(tableName); |
| 296 | + Append app_0 = client.append(tableName).setRowKey(colVal("c1", 1L)) |
| 297 | + .addMutateColVal(colVal("c3", "llo")); |
| 298 | + batch3.addOperation(app_0).execute(); |
| 299 | + assertEquals(1, getResultCount(updateSqlType)); |
| 300 | + batch3.addOperation(app_0).execute(); |
| 301 | + assertEquals(2, getResultCount(updateSqlType)); |
| 302 | + } finally { |
| 303 | + client.delete(tableName).setRowKey(colVal("c1", 1L)).execute(); |
| 304 | + client.delete(tableName).setRowKey(colVal("c1", 2L)).execute(); |
| 305 | + flushHistogram(); |
| 306 | + } |
| 307 | + } |
| 308 | + |
| 309 | + @Test |
| 310 | + public void testReplace() throws Exception { |
| 311 | + try { |
| 312 | + client.insert(tableName).setRowKey(colVal("c1", 1L)) |
| 313 | + .addMutateColVal(colVal("c2", 1L)) |
| 314 | + .execute(); |
| 315 | + client.insert(tableName).setRowKey(colVal("c1", 2L)) |
| 316 | + .addMutateColVal(colVal("c2", 1L)) |
| 317 | + .execute(); |
| 318 | + assertEquals(2, getResultCount(insertSqlType)); |
| 319 | + flushHistogram(); |
| 320 | + Thread.sleep(100); |
| 321 | + client.replace(tableName).setRowKey(colVal("c1", 1L)) |
| 322 | + .addMutateColVal(colVal("c2", 2L)) |
| 323 | + .execute(); |
| 324 | + assertEquals(1, getResultCount(replaceSqlType)); |
| 325 | + client.replace(tableName).setRowKey(colVal("c1", 2L)) |
| 326 | + .addMutateColVal(colVal("c2", 2L)) |
| 327 | + .execute(); |
| 328 | + assertEquals(2, getResultCount(replaceSqlType)); |
| 329 | + |
| 330 | + // multi replace |
| 331 | + flushHistogram(); |
| 332 | + Thread.sleep(100); |
| 333 | + BatchOperation batch = client.batchOperation(tableName); |
| 334 | + Replace rep_0 = client.replace(tableName).setRowKey(colVal("c1", 1L)) |
| 335 | + .addMutateColVal(colVal("c2", 1L)); |
| 336 | + batch.addOperation(rep_0).execute(); |
| 337 | + assertEquals(1, getResultCount(replaceSqlType)); |
| 338 | + batch.addOperation(rep_0).execute(); |
| 339 | + assertEquals(2, getResultCount(replaceSqlType)); |
| 340 | + } finally { |
| 341 | + client.delete(tableName).setRowKey(colVal("c1", 1L)).execute(); |
| 342 | + client.delete(tableName).setRowKey(colVal("c1", 2L)).execute(); |
| 343 | + flushHistogram(); |
| 344 | + } |
| 345 | + } |
| 346 | + |
| 347 | + @Test |
| 348 | + public void testQueryAndMutate() throws Exception { |
| 349 | + try { |
| 350 | + flushHistogram(); |
| 351 | + Thread.sleep(100); |
| 352 | + TableQuery tableQuery = client.query(tableName); |
| 353 | + tableQuery.addScanRange(new Object[] { 0L,}, new Object[] { 1L,}); |
| 354 | + tableQuery.select("c1"); |
| 355 | + ObTableQueryAndMutateRequest request_0 = client.obTableQueryAndAppend(tableQuery, |
| 356 | + new String[] { "c3" }, new Object[] {"_append0" }, true); |
| 357 | + client.execute(request_0); |
| 358 | + assertEquals(1, getResultCount(queryAndMutateSqlType)); |
| 359 | + } finally { |
| 360 | + client.delete(tableName).setRowKey(colVal("c1", 1L)).execute(); |
| 361 | + flushHistogram(); |
| 362 | + } |
| 363 | + } |
| 364 | + |
| 365 | + @Test |
| 366 | + public void testOther() throws Exception { |
| 367 | + try { |
| 368 | + flushHistogram(); |
| 369 | + Thread.sleep(100); |
| 370 | + BatchOperation batch = client.batchOperation(tableName); |
| 371 | + Insert ins_0 = client.insert(tableName).setRowKey(colVal("c1", 1L)) |
| 372 | + .addMutateColVal(colVal("c2", 1L)); |
| 373 | + Replace rep_0 = client.replace(tableName).setRowKey(colVal("c1", 2L)) |
| 374 | + .addMutateColVal(colVal("c2", 1L)); |
| 375 | + batch.addOperation(ins_0, rep_0).execute(); |
| 376 | + assertEquals(1, getResultCount(otherSqlType)); |
| 377 | + batch.addOperation(ins_0, rep_0).execute(); |
| 378 | + assertEquals(2, getResultCount(otherSqlType)); |
| 379 | + } finally { |
| 380 | + client.delete(tableName).setRowKey(colVal("c1", 1L)).execute(); |
| 381 | + client.delete(tableName).setRowKey(colVal("c1", 2L)).execute(); |
| 382 | + flushHistogram(); |
| 383 | + } |
| 384 | + } |
| 385 | +} |
0 commit comments