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

6
#include "qTsbuf.h"
H
hjxilinx 已提交
7 8
#include "taos.h"
#include "tsdb.h"
9
#include "ttoken.h"
H
hjxilinx 已提交
10 11
#include "tutil.h"

S
Shengliang Guan 已提交
12 13 14
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
15

H
hjxilinx 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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 已提交
35
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
36 37 38

  // write 10 ts points
  int32_t num = 10;
39 40
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;
H
Haojun Liao 已提交
41
  t.i64 = 1;
H
hjxilinx 已提交
42 43

  int64_t* list = createTsList(10, 10000000, 30);
44
  tsBufAppend(pTSBuf, 0, &t, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
45
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
46 47

  EXPECT_EQ(pTSBuf->tsData.len, sizeof(int64_t) * num);
48
  EXPECT_EQ(tVariantCompare(&pTSBuf->block.tag, &t), 0);
49
  EXPECT_EQ(pTSBuf->numOfGroups, 1);
H
hjxilinx 已提交
50 51 52 53 54

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

H
Haojun Liao 已提交
55
  tsBufDestroy(pTSBuf);
H
Haojun Liao 已提交
56 57

  free(list);
H
hjxilinx 已提交
58 59 60 61
}

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

  // write 10 ts points
  int32_t num = 1000000;
66 67
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;
H
Haojun Liao 已提交
68
  t.i64 = 1;
H
hjxilinx 已提交
69 70

  int64_t* list = createTsList(num, 10000000, 30);
71
  tsBufAppend(pTSBuf, 0, &t, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
72 73 74

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

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

H
Haojun Liao 已提交
83
  tsBufDestroy(pTSBuf);
H
Haojun Liao 已提交
84
  free(list);
H
hjxilinx 已提交
85 86 87
}

void multiTagsTest() {
H
Haojun Liao 已提交
88
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
89 90

  int32_t num = 10000;
91 92 93
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;

H
hjxilinx 已提交
94 95 96 97 98 99
  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);
H
Haojun Liao 已提交
100
    t.i64 = i;
101 102

    tsBufAppend(pTSBuf, 0, &t, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
103 104 105 106 107
    free(list);

    start += step * num;
  }

H
hjxilinx 已提交
108
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
109 110
  EXPECT_EQ(pTSBuf->tsData.len, num * sizeof(int64_t));

H
Haojun Liao 已提交
111
  EXPECT_EQ(pTSBuf->block.tag.i64, numOfTags - 1);
112
  EXPECT_EQ(pTSBuf->numOfGroups, 1);
H
hjxilinx 已提交
113 114 115 116 117

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

H
Haojun Liao 已提交
118
  tsBufDestroy(pTSBuf);
H
hjxilinx 已提交
119 120 121
}

void multiVnodeTagsTest() {
H
Haojun Liao 已提交
122
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
123 124 125 126 127 128 129 130 131 132

  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;
133 134 135
    tVariant t = {0};
    t.nType = TSDB_DATA_TYPE_BIGINT;

H
hjxilinx 已提交
136 137
    for (int32_t i = 0; i < numOfTags; ++i) {
      int64_t* list = createTsList(num, start, step);
H
Haojun Liao 已提交
138
      t.i64 = i;
139 140

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

      start += step * num;
    }

146
    EXPECT_EQ(pTSBuf->numOfGroups, j + 1);
H
hjxilinx 已提交
147 148
  }

H
hjxilinx 已提交
149
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
150
  EXPECT_EQ(pTSBuf->tsData.len, num * sizeof(int64_t));
H
Haojun Liao 已提交
151
  EXPECT_EQ(pTSBuf->block.tag.i64, numOfTags - 1);
H
hjxilinx 已提交
152 153 154

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

H
Haojun Liao 已提交
155
  EXPECT_EQ(pTSBuf->block.tag.i64, numOfTags - 1);
H
hjxilinx 已提交
156 157 158 159 160

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

H
Haojun Liao 已提交
161
  tsBufDestroy(pTSBuf);
H
hjxilinx 已提交
162 163 164
}

void loadDataTest() {
H
Haojun Liao 已提交
165
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
166 167 168 169 170 171 172 173 174 175 176

  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;
177 178 179
    tVariant t = {0};
    t.nType = TSDB_DATA_TYPE_BIGINT;

H
hjxilinx 已提交
180 181
    for (int32_t i = 0; i < numOfTags; ++i) {
      int64_t* list = createTsList(num, start, step);
H
Haojun Liao 已提交
182
      t.i64 = i;
183 184

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

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

191
    EXPECT_EQ(pTSBuf->numOfGroups, j + 1);
H
hjxilinx 已提交
192 193
  }

H
hjxilinx 已提交
194
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
195 196

  EXPECT_EQ(pTSBuf->tsData.len, num * sizeof(int64_t));
H
Haojun Liao 已提交
197
  EXPECT_EQ(pTSBuf->block.tag.i64, numOfTags - 1);
H
hjxilinx 已提交
198 199 200

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

H
Haojun Liao 已提交
201
  EXPECT_EQ(pTSBuf->block.tag.i64, numOfTags - 1);
H
hjxilinx 已提交
202 203 204 205 206 207 208 209

  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);
210
  EXPECT_EQ(pNewBuf->numOfGroups, numOfVnode);
H
hjxilinx 已提交
211 212 213 214 215 216 217 218 219 220 221
  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 已提交
222
  printf("start:%" PRIu64 "\n", s);
H
hjxilinx 已提交
223 224 225 226 227 228 229 230

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

H
hjxilinx 已提交
231
    //    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.vnode, elem.tag, elem.ts);
H
hjxilinx 已提交
232 233 234
  }

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

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 已提交
250
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
251 252 253 254

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

H
hjxilinx 已提交
258 259
    for (int32_t i = 0; i < numOfTags; ++i) {
      int64_t* list = createTsList(num, start, step);
H
Haojun Liao 已提交
260
      t.i64 = i;
261 262

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

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

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

      start += step * num;
    }

276
    EXPECT_EQ(pTSBuf->numOfGroups, j + 1);
H
hjxilinx 已提交
277 278 279 280 281 282 283
  }

  tsBufResetPos(pTSBuf);

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

H
hjxilinx 已提交
286
  pTSBuf->cur.order = TSDB_ORDER_DESC;
H
hjxilinx 已提交
287 288 289 290 291

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

  // specify the data block with vnode and tags value
  tsBufResetPos(pTSBuf);
H
hjxilinx 已提交
297
  pTSBuf->cur.order = TSDB_ORDER_DESC;
H
hjxilinx 已提交
298 299 300 301

  int32_t startVnode = 1;
  int32_t startTag = 2;

302 303
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;
H
Haojun Liao 已提交
304
  t.i64 = startTag;
305 306

  tsBufGetElemStartPos(pTSBuf, startVnode, &t);
H
hjxilinx 已提交
307 308 309 310

  int32_t totalOutput = 10;
  while (1) {
    STSElem elem = tsBufGetElem(pTSBuf);
H
Haojun Liao 已提交
311
    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.id, elem.tag->i64, elem.ts);
H
hjxilinx 已提交
312 313 314 315 316 317 318 319

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

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

320
      startTag -= 1;
H
Haojun Liao 已提交
321
      t.i64 = startTag;
322
      tsBufGetElemStartPos(pTSBuf, startVnode, &t);
H
hjxilinx 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336

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

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

  /////////////////////////////////////////////////////////////////////////////////
  // traverse
H
hjxilinx 已提交
337
  pTSBuf->cur.order = TSDB_ORDER_ASC;
H
hjxilinx 已提交
338 339 340 341 342
  tsBufResetPos(pTSBuf);

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

  // specify the data block with vnode and tags value
  tsBufResetPos(pTSBuf);
H
hjxilinx 已提交
348
  pTSBuf->cur.order = TSDB_ORDER_ASC;
H
hjxilinx 已提交
349 350 351

  startVnode = 1;
  startTag = 2;
H
Haojun Liao 已提交
352
  t.i64 = startTag;
H
hjxilinx 已提交
353

354
  tsBufGetElemStartPos(pTSBuf, startVnode, &t);
H
hjxilinx 已提交
355 356 357 358

  totalOutput = 10;
  while (1) {
    STSElem elem = tsBufGetElem(pTSBuf);
H
Haojun Liao 已提交
359
    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.id, elem.tag->i64, elem.ts);
H
hjxilinx 已提交
360 361 362 363 364 365 366 367

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

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

368
      startTag -= 1;
H
Haojun Liao 已提交
369
      t.i64 = startTag;
370
      tsBufGetElemStartPos(pTSBuf, startVnode, &t);
H
hjxilinx 已提交
371 372 373 374 375 376 377 378 379 380 381

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

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

  tsBufDestroy(pTSBuf);
H
hjxilinx 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397
}

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 已提交
398
  tsBufDestroy(pNewBuf);
H
hjxilinx 已提交
399 400 401

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

  tsBufDestroy(pNewBuf);
H
hjxilinx 已提交
404 405 406
}

void mergeDiffVnodeBufferTest() {
H
Haojun Liao 已提交
407 408
  STSBuf* pTSBuf1 = tsBufCreate(true, TSDB_ORDER_ASC);
  STSBuf* pTSBuf2 = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
409 410 411 412 413

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

414 415 416
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;

H
hjxilinx 已提交
417 418 419 420
  // vnodeId:0
  int64_t start = 10000000;
  for (int32_t i = 0; i < numOfTags; ++i) {
    int64_t* list = createTsList(num, start, step);
H
Haojun Liao 已提交
421
    t.i64 = i;
422

H
Haojun Liao 已提交
423 424
    tsBufAppend(pTSBuf1, 1, &t, (const char*)list, num * sizeof(int64_t));
    tsBufAppend(pTSBuf2, 9, &t, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
425 426 427 428 429 430 431 432

    free(list);

    start += step * num;
  }

  tsBufFlush(pTSBuf2);

H
Haojun Liao 已提交
433
  tsBufMerge(pTSBuf1, pTSBuf2);
434
  EXPECT_EQ(pTSBuf1->numOfGroups, 2);
H
hjxilinx 已提交
435 436 437 438
  EXPECT_EQ(pTSBuf1->numOfTotal, numOfTags * 2 * num);

  tsBufDisplay(pTSBuf1);

H
Haojun Liao 已提交
439 440
  tsBufDestroy(pTSBuf2);
  tsBufDestroy(pTSBuf1);
H
hjxilinx 已提交
441 442 443
}

void mergeIdenticalVnodeBufferTest() {
H
Haojun Liao 已提交
444 445
  STSBuf* pTSBuf1 = tsBufCreate(true, TSDB_ORDER_ASC);
  STSBuf* pTSBuf2 = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
446

447 448 449
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;

H
hjxilinx 已提交
450 451 452 453 454 455 456 457
  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);
H
Haojun Liao 已提交
458
    t.i64 = i;
H
hjxilinx 已提交
459

460
    tsBufAppend(pTSBuf1, 12, &t, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
461 462 463 464 465 466 467 468
    free(list);

    start += step * num;
  }

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

H
Haojun Liao 已提交
469
    t.i64 = i;
470
    tsBufAppend(pTSBuf2, 77, &t, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
471 472 473 474 475 476 477
    free(list);

    start += step * num;
  }

  tsBufFlush(pTSBuf2);

H
Haojun Liao 已提交
478
  tsBufMerge(pTSBuf1, pTSBuf2);
479
  EXPECT_EQ(pTSBuf1->numOfGroups, 2);
H
hjxilinx 已提交
480 481 482
  EXPECT_EQ(pTSBuf1->numOfTotal, numOfTags * 2 * num);

  tsBufResetPos(pTSBuf1);
483 484

  int32_t count = 0;
H
hjxilinx 已提交
485 486
  while (tsBufNextPos(pTSBuf1)) {
    STSElem elem = tsBufGetElem(pTSBuf1);
487 488

    if (count++ < numOfTags * num) {
489
      EXPECT_EQ(elem.id, 12);
490
    } else {
491
      EXPECT_EQ(elem.id, 77);
492
    }
H
hjxilinx 已提交
493

H
Haojun Liao 已提交
494
    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.id, elem.tag->i64, elem.ts);
H
hjxilinx 已提交
495 496
  }

H
Haojun Liao 已提交
497 498
  tsBufDestroy(pTSBuf1);
  tsBufDestroy(pTSBuf2);
H
hjxilinx 已提交
499 500 501
}
}  // namespace

502 503

//TODO add binary tag value test case
H
hjxilinx 已提交
504 505 506 507 508 509 510
TEST(testCase, tsBufTest) {
  simpleTest();
  largeTSTest();
  multiTagsTest();
  multiVnodeTagsTest();
  loadDataTest();
  invalidFileTest();
511
//    randomIncTsTest();
H
hjxilinx 已提交
512 513 514 515
  TSTraverse();
  mergeDiffVnodeBufferTest();
  mergeIdenticalVnodeBufferTest();
}