tsBufTest.cpp 11.8 KB
Newer Older
S
Shengliang Guan 已提交
1
#include "os.h"
H
hjxilinx 已提交
2 3 4 5 6 7
#include <gtest/gtest.h>
#include <cassert>
#include <iostream>

#include "taos.h"
#include "tsdb.h"
H
Haojun Liao 已提交
8
#include "qTsbuf.h"
H
hjxilinx 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include "tstoken.h"
#include "tutil.h"

namespace {
/**
 *
 * @param num  total number
 * @param step gap between two consecutive ts
 * @return
 */
int64_t* createTsList(int32_t num, int64_t start, int32_t step) {
  int64_t* pList = (int64_t*)malloc(num * sizeof(int64_t));

  for (int64_t i = 0; i < num; ++i) {
    pList[i] = start + i * step;
  }

  return pList;
}

// simple test
void simpleTest() {
H
Haojun Liao 已提交
31
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
32 33 34

  // write 10 ts points
  int32_t num = 10;
35 36 37
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;
  t.i64Key = 1;
H
hjxilinx 已提交
38 39

  int64_t* list = createTsList(10, 10000000, 30);
40
  tsBufAppend(pTSBuf, 0, &t, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
41
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
42 43

  EXPECT_EQ(pTSBuf->tsData.len, sizeof(int64_t) * num);
44
  EXPECT_EQ(tVariantCompare(&pTSBuf->block.tag, &t), 0);
H
hjxilinx 已提交
45 46 47 48 49 50
  EXPECT_EQ(pTSBuf->numOfVnodes, 1);

  tsBufFlush(pTSBuf);
  EXPECT_EQ(pTSBuf->tsData.len, 0);
  EXPECT_EQ(pTSBuf->block.numOfElem, num);

H
Haojun Liao 已提交
51
  tsBufDestroy(pTSBuf);
H
Haojun Liao 已提交
52 53

  free(list);
H
hjxilinx 已提交
54 55 56 57
}

// one large list of ts, the ts list need to be split into several small blocks
void largeTSTest() {
H
Haojun Liao 已提交
58
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
59 60 61

  // write 10 ts points
  int32_t num = 1000000;
62 63 64
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;
  t.i64Key = 1;
H
hjxilinx 已提交
65 66

  int64_t* list = createTsList(num, 10000000, 30);
67
  tsBufAppend(pTSBuf, 0, &t, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
68 69 70

  // the data has been flush to disk, no data in cache
  EXPECT_EQ(pTSBuf->tsData.len, 0);
71
  EXPECT_EQ(tVariantCompare(&pTSBuf->block.tag, &t), 0);
H
hjxilinx 已提交
72
  EXPECT_EQ(pTSBuf->numOfVnodes, 1);
H
hjxilinx 已提交
73
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
74 75 76 77 78

  tsBufFlush(pTSBuf);
  EXPECT_EQ(pTSBuf->tsData.len, 0);
  EXPECT_EQ(pTSBuf->block.numOfElem, num);

H
Haojun Liao 已提交
79
  tsBufDestroy(pTSBuf);
H
Haojun Liao 已提交
80
  free(list);
H
hjxilinx 已提交
81 82 83
}

void multiTagsTest() {
H
Haojun Liao 已提交
84
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
85 86

  int32_t num = 10000;
87 88 89
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;

H
hjxilinx 已提交
90 91 92 93 94 95
  int64_t start = 10000000;
  int32_t numOfTags = 50;
  int32_t step = 30;

  for (int32_t i = 0; i < numOfTags; ++i) {
    int64_t* list = createTsList(num, start, step);
96 97 98
    t.i64Key = i;

    tsBufAppend(pTSBuf, 0, &t, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
99 100 101 102 103
    free(list);

    start += step * num;
  }

H
hjxilinx 已提交
104
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
105 106
  EXPECT_EQ(pTSBuf->tsData.len, num * sizeof(int64_t));

107
  EXPECT_EQ(pTSBuf->block.tag.i64Key, numOfTags - 1);
H
hjxilinx 已提交
108 109 110 111 112 113
  EXPECT_EQ(pTSBuf->numOfVnodes, 1);

  tsBufFlush(pTSBuf);
  EXPECT_EQ(pTSBuf->tsData.len, 0);
  EXPECT_EQ(pTSBuf->block.numOfElem, num);

H
Haojun Liao 已提交
114
  tsBufDestroy(pTSBuf);
H
hjxilinx 已提交
115 116 117
}

void multiVnodeTagsTest() {
H
Haojun Liao 已提交
118
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
119 120 121 122 123 124 125 126 127 128

  int32_t num = 10000;
  int64_t start = 10000000;
  int32_t numOfTags = 50;
  int32_t step = 30;

  // 2000 vnodes
  for (int32_t j = 0; j < 20; ++j) {
    // vnodeId:0
    start = 10000000;
129 130 131
    tVariant t = {0};
    t.nType = TSDB_DATA_TYPE_BIGINT;

H
hjxilinx 已提交
132 133
    for (int32_t i = 0; i < numOfTags; ++i) {
      int64_t* list = createTsList(num, start, step);
134 135 136
      t.i64Key = i;

      tsBufAppend(pTSBuf, j, &t, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
137 138 139 140 141 142 143 144
      free(list);

      start += step * num;
    }

    EXPECT_EQ(pTSBuf->numOfVnodes, j + 1);
  }

H
hjxilinx 已提交
145
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
146
  EXPECT_EQ(pTSBuf->tsData.len, num * sizeof(int64_t));
147
  EXPECT_EQ(pTSBuf->block.tag.i64Key, numOfTags - 1);
H
hjxilinx 已提交
148 149 150

  EXPECT_EQ(pTSBuf->tsData.len, num * sizeof(int64_t));

151
  EXPECT_EQ(pTSBuf->block.tag.i64Key, numOfTags - 1);
H
hjxilinx 已提交
152 153 154 155 156

  tsBufFlush(pTSBuf);
  EXPECT_EQ(pTSBuf->tsData.len, 0);
  EXPECT_EQ(pTSBuf->block.numOfElem, num);

H
Haojun Liao 已提交
157
  tsBufDestroy(pTSBuf);
H
hjxilinx 已提交
158 159 160
}

void loadDataTest() {
H
Haojun Liao 已提交
161
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
162 163 164 165 166 167 168 169 170 171 172

  int32_t num = 10000;
  int64_t oldStart = 10000000;
  int32_t numOfTags = 50;
  int32_t step = 30;
  int32_t numOfVnode = 200;

  // 10000 vnodes
  for (int32_t j = 0; j < numOfVnode; ++j) {
    // vnodeId:0
    int64_t start = 10000000;
173 174 175
    tVariant t = {0};
    t.nType = TSDB_DATA_TYPE_BIGINT;

H
hjxilinx 已提交
176 177
    for (int32_t i = 0; i < numOfTags; ++i) {
      int64_t* list = createTsList(num, start, step);
178 179 180
      t.i64Key = i;

      tsBufAppend(pTSBuf, j, &t, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
181
      printf("%d - %" PRIu64 "\n", i, list[0]);
H
hjxilinx 已提交
182 183 184 185 186 187 188 189

      free(list);
      start += step * num;
    }

    EXPECT_EQ(pTSBuf->numOfVnodes, j + 1);
  }

H
hjxilinx 已提交
190
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
191 192

  EXPECT_EQ(pTSBuf->tsData.len, num * sizeof(int64_t));
193
  EXPECT_EQ(pTSBuf->block.tag.i64Key, numOfTags - 1);
H
hjxilinx 已提交
194 195 196

  EXPECT_EQ(pTSBuf->tsData.len, num * sizeof(int64_t));

197
  EXPECT_EQ(pTSBuf->block.tag.i64Key, numOfTags - 1);
H
hjxilinx 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

  tsBufFlush(pTSBuf);
  EXPECT_EQ(pTSBuf->tsData.len, 0);
  EXPECT_EQ(pTSBuf->block.numOfElem, num);

  // create from exists file
  STSBuf* pNewBuf = tsBufCreateFromFile(pTSBuf->path, false);
  EXPECT_EQ(pNewBuf->tsOrder, pTSBuf->tsOrder);
  EXPECT_EQ(pNewBuf->numOfVnodes, numOfVnode);
  EXPECT_EQ(pNewBuf->fileSize, pTSBuf->fileSize);

  EXPECT_EQ(pNewBuf->pData[0].info.offset, pTSBuf->pData[0].info.offset);
  EXPECT_EQ(pNewBuf->pData[0].info.numOfBlocks, pTSBuf->pData[0].info.numOfBlocks);
  EXPECT_EQ(pNewBuf->pData[0].info.compLen, pTSBuf->pData[0].info.compLen);

  EXPECT_STREQ(pNewBuf->path, pTSBuf->path);

  tsBufResetPos(pNewBuf);

  int64_t s = taosGetTimestampUs();
H
hjxilinx 已提交
218
  printf("start:%" PRIu64 "\n", s);
H
hjxilinx 已提交
219 220 221 222 223 224 225 226

  int32_t x = 0;
  while (tsBufNextPos(pNewBuf)) {
    STSElem elem = tsBufGetElem(pNewBuf);
    if (++x == 100000000) {
      break;
    }

H
hjxilinx 已提交
227
    //    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.vnode, elem.tag, elem.ts);
H
hjxilinx 已提交
228 229 230
  }

  int64_t e = taosGetTimestampUs();
H
hjxilinx 已提交
231
  printf("end:%" PRIu64 ", elapsed:%" PRIu64 ", total obj:%d\n", e, e - s, x);
H
Haojun Liao 已提交
232 233
  tsBufDestroy(pTSBuf);
  tsBufDestroy(pNewBuf);
H
hjxilinx 已提交
234 235 236 237 238 239 240 241 242 243 244 245
}

void randomIncTsTest() {}

void TSTraverse() {
  // 10000 vnodes
  int32_t num = 200000;
  int64_t oldStart = 10000000;
  int32_t numOfTags = 3;
  int32_t step = 30;
  int32_t numOfVnode = 2;

H
Haojun Liao 已提交
246
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
247 248 249 250

  for (int32_t j = 0; j < numOfVnode; ++j) {
    // vnodeId:0
    int64_t start = 10000000;
251 252 253
    tVariant t = {0};
    t.nType = TSDB_DATA_TYPE_BIGINT;

H
hjxilinx 已提交
254 255
    for (int32_t i = 0; i < numOfTags; ++i) {
      int64_t* list = createTsList(num, start, step);
256 257 258
      t.i64Key = i;

      tsBufAppend(pTSBuf, j, &t, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
259
      printf("%d - %d - %" PRIu64 ", %" PRIu64 "\n", j, i, list[0], list[num - 1]);
H
hjxilinx 已提交
260 261 262 263 264

      free(list);
      start += step * num;

      list = createTsList(num, start, step);
265
      tsBufAppend(pTSBuf, j, &t, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
266
      printf("%d - %d - %" PRIu64 ", %" PRIu64 "\n", j, i, list[0], list[num - 1]);
H
hjxilinx 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279
      free(list);

      start += step * num;
    }

    EXPECT_EQ(pTSBuf->numOfVnodes, j + 1);
  }

  tsBufResetPos(pTSBuf);

  ////////////////////////////////////////////////////////////////////////////////////////
  // reverse traverse
  int64_t s = taosGetTimestampUs();
H
hjxilinx 已提交
280
  printf("start:%" PRIu64 "\n", s);
H
hjxilinx 已提交
281

H
hjxilinx 已提交
282
  pTSBuf->cur.order = TSDB_ORDER_DESC;
H
hjxilinx 已提交
283 284 285 286 287

  // complete reverse traverse
  int32_t x = 0;
  while (tsBufNextPos(pTSBuf)) {
    STSElem elem = tsBufGetElem(pTSBuf);
H
hjxilinx 已提交
288
    //    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.vnode, elem.tag, elem.ts);
H
hjxilinx 已提交
289 290 291 292
  }

  // specify the data block with vnode and tags value
  tsBufResetPos(pTSBuf);
H
hjxilinx 已提交
293
  pTSBuf->cur.order = TSDB_ORDER_DESC;
H
hjxilinx 已提交
294 295 296 297

  int32_t startVnode = 1;
  int32_t startTag = 2;

298 299 300 301 302
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;
  t.i64Key = startTag;

  tsBufGetElemStartPos(pTSBuf, startVnode, &t);
H
hjxilinx 已提交
303 304 305 306

  int32_t totalOutput = 10;
  while (1) {
    STSElem elem = tsBufGetElem(pTSBuf);
307
    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.vnode, elem.tag.i64Key, elem.ts);
H
hjxilinx 已提交
308 309 310 311 312 313 314 315

    if (!tsBufNextPos(pTSBuf)) {
      break;
    }

    if (--totalOutput <= 0) {
      totalOutput = 10;

316 317 318
      startTag -= 1;
      t.i64Key = startTag;
      tsBufGetElemStartPos(pTSBuf, startVnode, &t);
H
hjxilinx 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332

      if (startTag == 0) {
        startVnode -= 1;
        startTag = 3;
      }

      if (startVnode < 0) {
        break;
      }
    }
  }

  /////////////////////////////////////////////////////////////////////////////////
  // traverse
H
hjxilinx 已提交
333
  pTSBuf->cur.order = TSDB_ORDER_ASC;
H
hjxilinx 已提交
334 335 336 337 338
  tsBufResetPos(pTSBuf);

  // complete forwards traverse
  while (tsBufNextPos(pTSBuf)) {
    STSElem elem = tsBufGetElem(pTSBuf);
H
hjxilinx 已提交
339
    //    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.vnode, elem.tag, elem.ts);
H
hjxilinx 已提交
340 341 342 343
  }

  // specify the data block with vnode and tags value
  tsBufResetPos(pTSBuf);
H
hjxilinx 已提交
344
  pTSBuf->cur.order = TSDB_ORDER_ASC;
H
hjxilinx 已提交
345 346 347

  startVnode = 1;
  startTag = 2;
348
  t.i64Key = startTag;
H
hjxilinx 已提交
349

350
  tsBufGetElemStartPos(pTSBuf, startVnode, &t);
H
hjxilinx 已提交
351 352 353 354

  totalOutput = 10;
  while (1) {
    STSElem elem = tsBufGetElem(pTSBuf);
355
    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.vnode, elem.tag.i64Key, elem.ts);
H
hjxilinx 已提交
356 357 358 359 360 361 362 363

    if (!tsBufNextPos(pTSBuf)) {
      break;
    }

    if (--totalOutput <= 0) {
      totalOutput = 10;

364 365 366
      startTag -= 1;
      t.i64Key = startTag;
      tsBufGetElemStartPos(pTSBuf, startVnode, &t);
H
hjxilinx 已提交
367 368 369 370 371 372 373 374 375 376 377

      if (startTag < 0) {
        startVnode -= 1;
        startTag = 3;
      }

      if (startVnode < 0) {
        break;
      }
    }
  }
H
Haojun Liao 已提交
378 379

  tsBufDestroy(pTSBuf);
H
hjxilinx 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393
}

void performanceTest() {}

void emptyTagTest() {}

void invalidFileTest() {
  const char* cmd = "touch /tmp/test";

  // create empty file
  system(cmd);

  STSBuf* pNewBuf = tsBufCreateFromFile("/tmp/test", true);
  EXPECT_TRUE(pNewBuf == NULL);
H
Haojun Liao 已提交
394
  tsBufDestroy(pNewBuf);
H
hjxilinx 已提交
395 396 397

  pNewBuf = tsBufCreateFromFile("/tmp/911", true);
  EXPECT_TRUE(pNewBuf == NULL);
H
Haojun Liao 已提交
398 399

  tsBufDestroy(pNewBuf);
H
hjxilinx 已提交
400 401 402
}

void mergeDiffVnodeBufferTest() {
H
Haojun Liao 已提交
403 404
  STSBuf* pTSBuf1 = tsBufCreate(true, TSDB_ORDER_ASC);
  STSBuf* pTSBuf2 = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
405 406 407 408 409

  int32_t step = 30;
  int32_t num = 1000;
  int32_t numOfTags = 10;

410 411 412
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;

H
hjxilinx 已提交
413 414 415 416
  // vnodeId:0
  int64_t start = 10000000;
  for (int32_t i = 0; i < numOfTags; ++i) {
    int64_t* list = createTsList(num, start, step);
417 418
    t.i64Key = i;

H
Haojun Liao 已提交
419 420
    tsBufAppend(pTSBuf1, 1, &t, (const char*)list, num * sizeof(int64_t));
    tsBufAppend(pTSBuf2, 9, &t, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
421 422 423 424 425 426 427 428

    free(list);

    start += step * num;
  }

  tsBufFlush(pTSBuf2);

H
Haojun Liao 已提交
429
  tsBufMerge(pTSBuf1, pTSBuf2);
H
hjxilinx 已提交
430 431 432 433 434
  EXPECT_EQ(pTSBuf1->numOfVnodes, 2);
  EXPECT_EQ(pTSBuf1->numOfTotal, numOfTags * 2 * num);

  tsBufDisplay(pTSBuf1);

H
Haojun Liao 已提交
435 436
  tsBufDestroy(pTSBuf2);
  tsBufDestroy(pTSBuf1);
H
hjxilinx 已提交
437 438 439
}

void mergeIdenticalVnodeBufferTest() {
H
Haojun Liao 已提交
440 441
  STSBuf* pTSBuf1 = tsBufCreate(true, TSDB_ORDER_ASC);
  STSBuf* pTSBuf2 = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
442

443 444 445
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;

H
hjxilinx 已提交
446 447 448 449 450 451 452 453
  int32_t step = 30;
  int32_t num = 1000;
  int32_t numOfTags = 10;

  // vnodeId:0
  int64_t start = 10000000;
  for (int32_t i = 0; i < numOfTags; ++i) {
    int64_t* list = createTsList(num, start, step);
454
    t.i64Key = i;
H
hjxilinx 已提交
455

456
    tsBufAppend(pTSBuf1, 12, &t, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
457 458 459 460 461 462 463 464
    free(list);

    start += step * num;
  }

  for (int32_t i = numOfTags; i < numOfTags * 2; ++i) {
    int64_t* list = createTsList(num, start, step);

465 466
    t.i64Key = i;
    tsBufAppend(pTSBuf2, 77, &t, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
467 468 469 470 471 472 473
    free(list);

    start += step * num;
  }

  tsBufFlush(pTSBuf2);

H
Haojun Liao 已提交
474
  tsBufMerge(pTSBuf1, pTSBuf2);
H
hjxilinx 已提交
475 476 477 478 479 480 481 482
  EXPECT_EQ(pTSBuf1->numOfVnodes, 1);
  EXPECT_EQ(pTSBuf1->numOfTotal, numOfTags * 2 * num);

  tsBufResetPos(pTSBuf1);
  while (tsBufNextPos(pTSBuf1)) {
    STSElem elem = tsBufGetElem(pTSBuf1);
    EXPECT_EQ(elem.vnode, 12);

483
    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.vnode, elem.tag.i64Key, elem.ts);
H
hjxilinx 已提交
484 485
  }

H
Haojun Liao 已提交
486 487
  tsBufDestroy(pTSBuf1);
  tsBufDestroy(pTSBuf2);
H
hjxilinx 已提交
488 489 490
}
}  // namespace

491 492

//TODO add binary tag value test case
H
hjxilinx 已提交
493 494 495 496 497 498 499
TEST(testCase, tsBufTest) {
  simpleTest();
  largeTSTest();
  multiTagsTest();
  multiVnodeTagsTest();
  loadDataTest();
  invalidFileTest();
500
//    randomIncTsTest();
H
hjxilinx 已提交
501 502 503 504
  TSTraverse();
  mergeDiffVnodeBufferTest();
  mergeIdenticalVnodeBufferTest();
}