tsBufTest.cpp 11.9 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"

12 13
#pragma GCC diagnostic ignored "-Werror=unused-function"

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

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

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

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

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

H
Haojun Liao 已提交
53
  tsBufDestroy(pTSBuf);
H
Haojun Liao 已提交
54 55

  free(list);
H
hjxilinx 已提交
56 57 58 59
}

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

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

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

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

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

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

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

  int32_t num = 10000;
89 90 91
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;

H
hjxilinx 已提交
92 93 94 95 96 97
  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 已提交
98
    t.i64 = i;
99 100

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

    start += step * num;
  }

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

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

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

H
Haojun Liao 已提交
116
  tsBufDestroy(pTSBuf);
H
hjxilinx 已提交
117 118 119
}

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

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

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

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

      start += step * num;
    }

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

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

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

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

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

H
Haojun Liao 已提交
159
  tsBufDestroy(pTSBuf);
H
hjxilinx 已提交
160 161 162
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      start += step * num;
    }

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

  tsBufResetPos(pTSBuf);

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

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

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

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

  int32_t startVnode = 1;
  int32_t startTag = 2;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  tsBufDestroy(pNewBuf);
H
hjxilinx 已提交
402 403 404
}

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

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

412 413 414
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;

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

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

    free(list);

    start += step * num;
  }

  tsBufFlush(pTSBuf2);

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

  tsBufDisplay(pTSBuf1);

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

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

445 446 447
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;

H
hjxilinx 已提交
448 449 450 451 452 453 454 455
  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 已提交
456
    t.i64 = i;
H
hjxilinx 已提交
457

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

    start += step * num;
  }

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

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

    start += step * num;
  }

  tsBufFlush(pTSBuf2);

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

  tsBufResetPos(pTSBuf1);
481 482

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

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

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

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

500 501

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