tsBufTest.cpp 11.8 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 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#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
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;
H
Haojun Liao 已提交
37
  t.i64 = 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);
45
  EXPECT_EQ(pTSBuf->numOfGroups, 1);
H
hjxilinx 已提交
46 47 48 49 50

  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
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;
H
Haojun Liao 已提交
64
  t.i64 = 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);
72
  EXPECT_EQ(pTSBuf->numOfGroups, 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);
H
Haojun Liao 已提交
96
    t.i64 = i;
97 98

    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));

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

  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);
H
Haojun Liao 已提交
134
      t.i64 = i;
135 136

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

      start += step * num;
    }

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

H
hjxilinx 已提交
145
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
146
  EXPECT_EQ(pTSBuf->tsData.len, num * sizeof(int64_t));
H
Haojun Liao 已提交
147
  EXPECT_EQ(pTSBuf->block.tag.i64, numOfTags - 1);
H
hjxilinx 已提交
148 149 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 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);
H
Haojun Liao 已提交
178
      t.i64 = i;
179 180

      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

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

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

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

  EXPECT_EQ(pTSBuf->tsData.len, num * sizeof(int64_t));
H
Haojun Liao 已提交
193
  EXPECT_EQ(pTSBuf->block.tag.i64, numOfTags - 1);
H
hjxilinx 已提交
194 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 201 202 203 204 205

  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);
206
  EXPECT_EQ(pNewBuf->numOfGroups, numOfVnode);
H
hjxilinx 已提交
207 208 209 210 211 212 213 214 215 216 217
  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);
H
Haojun Liao 已提交
256
      t.i64 = i;
257 258

      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
      free(list);

      start += step * num;
    }

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

  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
  tVariant t = {0};
  t.nType = TSDB_DATA_TYPE_BIGINT;
H
Haojun Liao 已提交
300
  t.i64 = startTag;
301 302

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

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

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

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

316
      startTag -= 1;
H
Haojun Liao 已提交
317
      t.i64 = startTag;
318
      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;
H
Haojun Liao 已提交
348
  t.i64 = startTag;
H
hjxilinx 已提交
349

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

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

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

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

364
      startTag -= 1;
H
Haojun Liao 已提交
365
      t.i64 = startTag;
366
      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);
H
Haojun Liao 已提交
417
    t.i64 = i;
418

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);
430
  EXPECT_EQ(pTSBuf1->numOfGroups, 2);
H
hjxilinx 已提交
431 432 433 434
  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);
H
Haojun Liao 已提交
454
    t.i64 = 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);

H
Haojun Liao 已提交
465
    t.i64 = i;
466
    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);
475
  EXPECT_EQ(pTSBuf1->numOfGroups, 2);
H
hjxilinx 已提交
476 477 478
  EXPECT_EQ(pTSBuf1->numOfTotal, numOfTags * 2 * num);

  tsBufResetPos(pTSBuf1);
479 480

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

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

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

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

498 499

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