|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * OBKV HBase Client Framework |
| 4 | + * %% |
| 5 | + * Copyright (C) 2025 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.secondary; |
| 19 | + |
| 20 | +import com.alipay.oceanbase.hbase.OHTableClient; |
| 21 | +import com.alipay.oceanbase.hbase.util.ObHTableTestUtil; |
| 22 | +import com.alipay.oceanbase.hbase.util.TableTemplateManager; |
| 23 | +import org.apache.hadoop.hbase.CellUtil; |
| 24 | +import org.apache.hadoop.hbase.KeyValue; |
| 25 | +import org.apache.hadoop.hbase.client.*; |
| 26 | +import org.apache.hadoop.hbase.filter.CompareFilter; |
| 27 | +import org.apache.hadoop.hbase.util.Bytes; |
| 28 | +import org.junit.*; |
| 29 | + |
| 30 | +import java.util.LinkedHashMap; |
| 31 | +import java.util.LinkedList; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +import static com.alipay.oceanbase.hbase.util.ObHTableSecondaryPartUtil.*; |
| 36 | +import static com.alipay.oceanbase.hbase.util.ObHTableTestUtil.FOR_EACH; |
| 37 | +import static org.apache.hadoop.hbase.util.Bytes.toBytes; |
| 38 | +import static org.junit.Assert.*; |
| 39 | +import static org.junit.Assert.assertTrue; |
| 40 | + |
| 41 | +public class OHTableSecondaryPartCellTTLTest { |
| 42 | + private static List<String> tableNames = new LinkedList<String>(); |
| 43 | + private static Map<String, List<String>> group2tableNames = new LinkedHashMap<>(); |
| 44 | + |
| 45 | + @BeforeClass |
| 46 | + public static void before() throws Exception { |
| 47 | + openDistributedExecute(); |
| 48 | + for (TableTemplateManager.TableType type : TableTemplateManager.CELL_TTL_TABLES) { |
| 49 | + createTables(type, tableNames, group2tableNames, true); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @AfterClass |
| 54 | + public static void finish() throws Exception { |
| 55 | + closeDistributedExecute(); |
| 56 | +// dropTables(tableNames, group2tableNames); |
| 57 | + } |
| 58 | + |
| 59 | + @Before |
| 60 | + public void prepareCase() throws Exception { |
| 61 | + truncateTables(tableNames, group2tableNames); |
| 62 | + } |
| 63 | + |
| 64 | + public static void testCellTTL(String tableName) throws Exception { |
| 65 | + OHTableClient hTable = ObHTableTestUtil.newOHTableClient(getTableName(tableName)); |
| 66 | + hTable.init(); |
| 67 | + |
| 68 | + String key1 = "key1"; |
| 69 | + String column1 = "cf1"; |
| 70 | + String column2 = "cf2"; |
| 71 | + String column3 = "cf3"; |
| 72 | + String family = getColumnFamilyName(tableName); |
| 73 | + String value1 = "v1"; |
| 74 | + String value2 = "v2"; |
| 75 | + String app = "app"; |
| 76 | + |
| 77 | + Result r; |
| 78 | + Put put1 = new Put(key1.getBytes()); |
| 79 | + put1.addColumn(family.getBytes(), column1.getBytes(), toBytes(11L)); |
| 80 | + put1.setTTL(5000); |
| 81 | + Put put2 = new Put(key1.getBytes()); |
| 82 | + put2.addColumn(family.getBytes(), column1.getBytes(), toBytes(22L)); |
| 83 | + put2.addColumn(family.getBytes(), column2.getBytes(), toBytes(33L)); |
| 84 | + put2.setTTL(10000); |
| 85 | + Put put3 = new Put(key1.getBytes()); |
| 86 | + put3.addColumn(family.getBytes(), column1.getBytes(), toBytes(11L)); |
| 87 | + put3.setTTL(-3000); |
| 88 | + Put put4 = new Put(key1.getBytes()); |
| 89 | + put4.addColumn(family.getBytes(), column1.getBytes(), toBytes(11L)); |
| 90 | + put4.setTTL(0); |
| 91 | + Put errorPut = new Put(key1.getBytes()); |
| 92 | + errorPut.addColumn("family1".getBytes(), column1.getBytes(), toBytes(11L)); |
| 93 | + errorPut.setTTL(10); |
| 94 | + |
| 95 | + Get get = new Get(key1.getBytes()); |
| 96 | + get.addFamily(family.getBytes()); |
| 97 | + get.setMaxVersions(10); |
| 98 | + try { |
| 99 | + hTable.put(errorPut); |
| 100 | + } catch (Exception e) { |
| 101 | + assertTrue(e.getCause().toString().contains("Unknown column 'TTL'")); |
| 102 | + } |
| 103 | + // test put and get |
| 104 | + hTable.put(put1); |
| 105 | + hTable.put(put2); |
| 106 | + hTable.put(put3); |
| 107 | + hTable.put(put4); |
| 108 | + r = hTable.get(get); |
| 109 | + assertEquals(3, r.size()); |
| 110 | + Thread.sleep(5000); |
| 111 | + r = hTable.get(get); |
| 112 | + assertEquals(2, r.size()); |
| 113 | + Thread.sleep(5000); |
| 114 | + r = hTable.get(get); |
| 115 | + assertEquals(0, r.size()); |
| 116 | + |
| 117 | + // test increment |
| 118 | + hTable.put(put1); |
| 119 | + hTable.put(put2); |
| 120 | + Thread.sleep(1000); |
| 121 | + Increment increment = new Increment(key1.getBytes()); |
| 122 | + increment.addColumn(family.getBytes(), column1.getBytes(), 1L); |
| 123 | + increment.addColumn(family.getBytes(), column2.getBytes(), 2L); |
| 124 | + increment.addColumn(family.getBytes(), column3.getBytes(), 5L); |
| 125 | + increment.setTTL(-5000); |
| 126 | + hTable.increment(increment); |
| 127 | + increment.setTTL(5000); |
| 128 | + hTable.increment(increment); |
| 129 | + get.setMaxVersions(1); |
| 130 | + r = hTable.get(get); |
| 131 | + |
| 132 | + assertEquals(3, r.size()); |
| 133 | + assertEquals(23L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 134 | + column1.getBytes()).get(0)))); |
| 135 | + assertEquals(35L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 136 | + column2.getBytes()).get(0)))); |
| 137 | + assertEquals(5L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 138 | + column3.getBytes()).get(0)))); |
| 139 | + |
| 140 | + Thread.sleep(10000); |
| 141 | + r = hTable.get(get); |
| 142 | + assertEquals(0, r.size()); |
| 143 | + |
| 144 | + increment = new Increment(key1.getBytes()); |
| 145 | + increment.addColumn(family.getBytes(), column1.getBytes(), 1L); |
| 146 | + increment.addColumn(family.getBytes(), column2.getBytes(), 2L); |
| 147 | + increment.setTTL(5000); |
| 148 | + hTable.increment(increment); |
| 149 | + r = hTable.get(get); |
| 150 | + |
| 151 | + assertEquals(2, r.size()); |
| 152 | + assertEquals(1L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 153 | + column1.getBytes()).get(0)))); |
| 154 | + assertEquals(2L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 155 | + column2.getBytes()).get(0)))); |
| 156 | + |
| 157 | + Thread.sleep(5000); |
| 158 | + r = hTable.get(get); |
| 159 | + assertEquals(0, r.size()); |
| 160 | + |
| 161 | + hTable.put(put1); |
| 162 | + hTable.put(put2); |
| 163 | + increment.addColumn(family.getBytes(), column1.getBytes(), 4L); |
| 164 | + hTable.increment(increment); |
| 165 | + r = hTable.get(get); |
| 166 | + assertEquals(2, r.size()); |
| 167 | + assertEquals(26L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 168 | + column1.getBytes()).get(0)))); |
| 169 | + assertEquals(35L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 170 | + column2.getBytes()).get(0)))); |
| 171 | + |
| 172 | + // test append |
| 173 | + Thread.sleep(10000); |
| 174 | + r = hTable.get(get); |
| 175 | + assertEquals(0, r.size()); |
| 176 | + |
| 177 | + put3 = new Put(key1.getBytes()); |
| 178 | + put3.addColumn(family.getBytes(), column1.getBytes(), toBytes(value1)); |
| 179 | + put3.addColumn(family.getBytes(), column2.getBytes(), toBytes(value2)); |
| 180 | + put3.setTTL(10000); |
| 181 | + hTable.put(put3); |
| 182 | + Append append = new Append(key1.getBytes()); |
| 183 | + KeyValue kv = new KeyValue(key1.getBytes(), family.getBytes(), column1.getBytes(), |
| 184 | + app.getBytes()); |
| 185 | + append.add(kv); |
| 186 | + append.setTTL(-3000); |
| 187 | + hTable.append(append); |
| 188 | + append.setTTL(3000); |
| 189 | + hTable.append(append); |
| 190 | + |
| 191 | + r = hTable.get(get); |
| 192 | + assertEquals(2, r.size()); |
| 193 | + assertEquals( |
| 194 | + value1 + app, |
| 195 | + Bytes.toString(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 196 | + column1.getBytes()).get(0)))); |
| 197 | + |
| 198 | + Thread.sleep(3000); |
| 199 | + r = hTable.get(get); |
| 200 | + assertEquals(2, r.size()); |
| 201 | + assertEquals( |
| 202 | + value1, |
| 203 | + Bytes.toString(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 204 | + column1.getBytes()).get(0)))); |
| 205 | + |
| 206 | + Thread.sleep(7000); |
| 207 | + r = hTable.get(get); |
| 208 | + assertEquals(0, r.size()); |
| 209 | + |
| 210 | + append.add(family.getBytes(), column1.getBytes(), app.getBytes()); |
| 211 | + hTable.append(append); |
| 212 | + r = hTable.get(get); |
| 213 | + assertEquals(1, r.size()); |
| 214 | + assertEquals( |
| 215 | + app, |
| 216 | + Bytes.toString(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 217 | + column1.getBytes()).get(0)))); |
| 218 | + |
| 219 | + Thread.sleep(3000); |
| 220 | + append.add(family.getBytes(), column2.getBytes(), app.getBytes()); |
| 221 | + hTable.append(append); |
| 222 | + r = hTable.get(get); |
| 223 | + assertEquals(2, r.size()); |
| 224 | + assertEquals( |
| 225 | + app, |
| 226 | + Bytes.toString(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 227 | + column1.getBytes()).get(0)))); |
| 228 | + assertEquals( |
| 229 | + app, |
| 230 | + Bytes.toString(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 231 | + column2.getBytes()).get(0)))); |
| 232 | + |
| 233 | + // test checkAndMutate |
| 234 | + Thread.sleep(3000); |
| 235 | + r = hTable.get(get); |
| 236 | + assertEquals(0, r.size()); |
| 237 | + hTable.put(put1); |
| 238 | + RowMutations rowMutations = new RowMutations(key1.getBytes()); |
| 239 | + rowMutations.add(put2); |
| 240 | + Delete delete = new Delete(key1.getBytes()); |
| 241 | + delete.addColumn(family.getBytes(), column1.getBytes()); |
| 242 | + rowMutations.add(delete); |
| 243 | + boolean succ = hTable.checkAndMutate(key1.getBytes(), family.getBytes(), |
| 244 | + column1.getBytes(), CompareFilter.CompareOp.EQUAL, toBytes(11L), rowMutations); |
| 245 | + assertTrue(succ); |
| 246 | + r = hTable.get(get); |
| 247 | + assertEquals(r.size(), 2); |
| 248 | + assertEquals(22L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 249 | + column1.getBytes()).get(0)))); |
| 250 | + assertEquals(33L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 251 | + column2.getBytes()).get(0)))); |
| 252 | + |
| 253 | + Thread.sleep(10000); |
| 254 | + r = hTable.get(get); |
| 255 | + assertEquals(r.size(), 0); |
| 256 | + |
| 257 | + hTable.put(put1); |
| 258 | + rowMutations = new RowMutations(key1.getBytes()); |
| 259 | + put4 = new Put(key1.getBytes()); |
| 260 | + put4.addColumn(family.getBytes(), column1.getBytes(), toBytes(22L)); |
| 261 | + put4.addColumn(family.getBytes(), column2.getBytes(), toBytes(33L)); |
| 262 | + put4.setTTL(10000); |
| 263 | + rowMutations.add(put4); |
| 264 | + succ = hTable.checkAndMutate(key1.getBytes(), family.getBytes(), column1.getBytes(), |
| 265 | + CompareFilter.CompareOp.EQUAL, toBytes(1L), rowMutations); |
| 266 | + assertFalse(succ); |
| 267 | + succ = hTable.checkAndMutate(key1.getBytes(), family.getBytes(), column1.getBytes(), |
| 268 | + CompareFilter.CompareOp.EQUAL, toBytes(11L), rowMutations); |
| 269 | + assertTrue(succ); |
| 270 | + |
| 271 | + r = hTable.get(get); |
| 272 | + assertEquals(r.size(), 2); |
| 273 | + assertEquals(22L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 274 | + column1.getBytes()).get(0)))); |
| 275 | + assertEquals(33L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 276 | + column2.getBytes()).get(0)))); |
| 277 | + |
| 278 | + Thread.sleep(5000); |
| 279 | + r = hTable.get(get); |
| 280 | + assertEquals(2, r.size()); |
| 281 | + assertEquals(22L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 282 | + column1.getBytes()).get(0)))); |
| 283 | + assertEquals(33L, Bytes.toLong(CellUtil.cloneValue(r.getColumnCells(family.getBytes(), |
| 284 | + column2.getBytes()).get(0)))); |
| 285 | + |
| 286 | + Thread.sleep(5000); |
| 287 | + r = hTable.get(get); |
| 288 | + assertEquals(r.size(), 0); |
| 289 | + put1 = new Put(key1.getBytes()); |
| 290 | + put1.addColumn(family.getBytes(), column1.getBytes(), toBytes(11L)); |
| 291 | + hTable.put(put1); |
| 292 | + |
| 293 | + increment = new Increment(key1.getBytes()); |
| 294 | + increment.addColumn(family.getBytes(), column1.getBytes(), 1L); |
| 295 | + hTable.increment(increment); |
| 296 | + r = hTable.get(get); |
| 297 | + assertEquals(r.size(), 1); |
| 298 | + assertEquals(Bytes.toLong(r.raw()[0].getValue()), 12L); |
| 299 | + get.setMaxVersions(10); |
| 300 | + r = hTable.get(get); |
| 301 | + assertEquals(r.size(), 2); |
| 302 | + |
| 303 | + put1.setTTL(-100); |
| 304 | + hTable.put(put1); |
| 305 | + Delete delete1 = new Delete(key1.getBytes()); |
| 306 | + delete1.addColumn(family.getBytes(), column1.getBytes()); |
| 307 | + hTable.delete(delete1); |
| 308 | + get.setMaxVersions(10); |
| 309 | + r = hTable.get(get); |
| 310 | + assertEquals(r.size(), 1); |
| 311 | + assertEquals(Bytes.toLong(r.raw()[0].getValue()), 11L); |
| 312 | + hTable.close(); |
| 313 | + } |
| 314 | + |
| 315 | + public static void testCellTTLSQL(String tableName) throws Exception { |
| 316 | + OHTableClient hTable = ObHTableTestUtil.newOHTableClient(getTableName(tableName)); |
| 317 | + hTable.init(); |
| 318 | + |
| 319 | + String key1 = "key1"; |
| 320 | + String column1 = "cf1"; |
| 321 | + String column2 = "cf2"; |
| 322 | + String column3 = "cf3"; |
| 323 | + String family = getColumnFamilyName(tableName); |
| 324 | + String value1 = "v1"; |
| 325 | + String value2 = "v2"; |
| 326 | + String app = "app"; |
| 327 | + |
| 328 | + Result r; |
| 329 | + Put put1 = new Put(key1.getBytes()); |
| 330 | + put1.addColumn(family.getBytes(), column1.getBytes(), toBytes(11L)); |
| 331 | + put1.setTTL(5000); |
| 332 | + Put put2 = new Put(key1.getBytes()); |
| 333 | + put2.addColumn(family.getBytes(), column1.getBytes(), toBytes(22L)); |
| 334 | + put2.addColumn(family.getBytes(), column2.getBytes(), toBytes(33L)); |
| 335 | + put2.setTTL(10000); |
| 336 | + Put put3 = new Put(key1.getBytes()); |
| 337 | + put3.addColumn(family.getBytes(), column1.getBytes(), toBytes(11L)); |
| 338 | + put3.setTTL(-3000); |
| 339 | + Put put4 = new Put(key1.getBytes()); |
| 340 | + put4.addColumn(family.getBytes(), column1.getBytes(), toBytes(11L)); |
| 341 | + put4.setTTL(0); |
| 342 | + Put errorPut = new Put(key1.getBytes()); |
| 343 | + errorPut.addColumn("family1".getBytes(), column1.getBytes(), toBytes(11L)); |
| 344 | + errorPut.setTTL(10); |
| 345 | + |
| 346 | + // test put and get |
| 347 | + hTable.put(put1); |
| 348 | + hTable.put(put2); |
| 349 | + hTable.put(put3); |
| 350 | + hTable.put(put4); |
| 351 | + |
| 352 | + Assert.assertEquals(5, getSQLTableRowCnt(tableName)); |
| 353 | + Thread.sleep(10000); |
| 354 | + openTTLExecute(); |
| 355 | + |
| 356 | + checkUtilTimeout(()-> { |
| 357 | + try { |
| 358 | + return getRunningNormalTTLTaskCnt() == 0; |
| 359 | + } catch (Exception e) { |
| 360 | + throw new RuntimeException(e); |
| 361 | + } |
| 362 | + }, 50000, 3000); |
| 363 | + |
| 364 | + Assert.assertEquals(0, getSQLTableRowCnt(tableName)); |
| 365 | + |
| 366 | + // 6. close ttl knob |
| 367 | + closeTTLExecute(); |
| 368 | + hTable.close(); |
| 369 | + } |
| 370 | + |
| 371 | + @Test |
| 372 | + public void testCellTTL() throws Throwable { |
| 373 | + FOR_EACH(tableNames, OHTableSecondaryPartCellTTLTest::testCellTTL); |
| 374 | + } |
| 375 | + |
| 376 | + @Test |
| 377 | + public void testCellTTLSQL() throws Throwable { |
| 378 | + FOR_EACH(tableNames, OHTableSecondaryPartCellTTLTest::testCellTTLSQL); |
| 379 | + } |
| 380 | + |
| 381 | +} |
0 commit comments