tsdbSmaTest.cpp 12.5 KB
Newer Older
C
Cary Xu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <gtest/gtest.h>
#include <taoserror.h>
#include <tglobal.h>
#include <iostream>

C
Cary Xu 已提交
21
#include <metaDef.h>
C
Cary Xu 已提交
22
#include <tmsg.h>
C
Cary Xu 已提交
23
#include <tsdbDef.h>
C
Cary Xu 已提交
24

C
Cary Xu 已提交
25 26 27 28 29 30
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"

C
Cary Xu 已提交
31 32 33 34 35
int main(int argc, char **argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

C
Cary Xu 已提交
36
TEST(testCase, tSma_Meta_Encode_Decode_Test) {
C
Cary Xu 已提交
37 38 39 40 41 42 43 44
  // encode
  STSma tSma = {0};
  tSma.version = 0;
  tSma.intervalUnit = TD_TIME_UNIT_DAY;
  tSma.interval = 1;
  tSma.slidingUnit = TD_TIME_UNIT_HOUR;
  tSma.sliding = 0;
  tstrncpy(tSma.indexName, "sma_index_test", TSDB_INDEX_NAME_LEN);
C
Cary Xu 已提交
45
  tstrncpy(tSma.timezone, "Asia/Shanghai", TD_TIMEZONE_LEN);
C
Cary Xu 已提交
46
  tSma.indexUid = 2345678910;
C
Cary Xu 已提交
47
  tSma.tableUid = 1234567890;
C
Cary Xu 已提交
48

C
Cary Xu 已提交
49 50
  STSmaWrapper tSmaWrapper = {.number = 1, .tSma = &tSma};
  uint32_t     bufLen = tEncodeTSmaWrapper(NULL, &tSmaWrapper);
C
Cary Xu 已提交
51

C
Cary Xu 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  void *buf = calloc(bufLen, 1);
  assert(buf != NULL);

  STSmaWrapper *pSW = (STSmaWrapper *)buf;
  uint32_t      len = tEncodeTSmaWrapper(&buf, &tSmaWrapper);

  EXPECT_EQ(len, bufLen);

  // decode
  STSmaWrapper dstTSmaWrapper = {0};
  void *       result = tDecodeTSmaWrapper(pSW, &dstTSmaWrapper);
  assert(result != NULL);

  EXPECT_EQ(tSmaWrapper.number, dstTSmaWrapper.number);

  for (int i = 0; i < tSmaWrapper.number; ++i) {
    STSma *pSma = tSmaWrapper.tSma + i;
    STSma *qSma = dstTSmaWrapper.tSma + i;

    EXPECT_EQ(pSma->version, qSma->version);
    EXPECT_EQ(pSma->intervalUnit, qSma->intervalUnit);
    EXPECT_EQ(pSma->slidingUnit, qSma->slidingUnit);
    EXPECT_STRCASEEQ(pSma->indexName, qSma->indexName);
C
Cary Xu 已提交
75
    EXPECT_STRCASEEQ(pSma->timezone, qSma->timezone);
C
Cary Xu 已提交
76
    EXPECT_EQ(pSma->indexUid, qSma->indexUid);
C
Cary Xu 已提交
77 78 79
    EXPECT_EQ(pSma->tableUid, qSma->tableUid);
    EXPECT_EQ(pSma->interval, qSma->interval);
    EXPECT_EQ(pSma->sliding, qSma->sliding);
C
Cary Xu 已提交
80 81
    EXPECT_EQ(pSma->exprLen, qSma->exprLen);
    EXPECT_STRCASEEQ(pSma->expr, qSma->expr);
C
Cary Xu 已提交
82 83
    EXPECT_EQ(pSma->tagsFilterLen, qSma->tagsFilterLen);
    EXPECT_STRCASEEQ(pSma->tagsFilter, qSma->tagsFilter);
C
Cary Xu 已提交
84 85 86
  }

  // resource release
C
Cary Xu 已提交
87 88
  tdDestroyTSma(&tSma);
  tdDestroyTSmaWrapper(&dstTSmaWrapper);
C
Cary Xu 已提交
89
}
C
Cary Xu 已提交
90

C
Cary Xu 已提交
91
#if 1
C
Cary Xu 已提交
92
TEST(testCase, tSma_metaDB_Put_Get_Del_Test) {
C
Cary Xu 已提交
93 94
  const char *   smaIndexName1 = "sma_index_test_1";
  const char *   smaIndexName2 = "sma_index_test_2";
C
Cary Xu 已提交
95 96
  const char *   timezone = "Asia/Shanghai";
  const char *   expr = "select count(a,b, top 20), from table interval 1d, sliding 1h;";
C
Cary Xu 已提交
97 98
  const char *   tagsFilter = "I'm tags filter";
  const char *   smaTestDir = "./smaTest";
C
Cary Xu 已提交
99 100 101
  const tb_uid_t tbUid = 1234567890;
  const int64_t  indexUid1 = 2000000001;
  const int64_t  indexUid2 = 2000000002;
C
Cary Xu 已提交
102
  const uint32_t nCntTSma = 2;
C
Cary Xu 已提交
103 104 105 106 107 108 109
  // encode
  STSma tSma = {0};
  tSma.version = 0;
  tSma.intervalUnit = TD_TIME_UNIT_DAY;
  tSma.interval = 1;
  tSma.slidingUnit = TD_TIME_UNIT_HOUR;
  tSma.sliding = 0;
C
Cary Xu 已提交
110
  tSma.indexUid = indexUid1;
C
Cary Xu 已提交
111
  tstrncpy(tSma.indexName, smaIndexName1, TSDB_INDEX_NAME_LEN);
C
Cary Xu 已提交
112
  tstrncpy(tSma.timezone, timezone, TD_TIMEZONE_LEN);
C
Cary Xu 已提交
113
  tSma.tableUid = tbUid;
C
Cary Xu 已提交
114 115 116 117 118

  tSma.exprLen = strlen(expr);
  tSma.expr = (char *)calloc(tSma.exprLen + 1, 1);
  tstrncpy(tSma.expr, expr, tSma.exprLen + 1);

C
Cary Xu 已提交
119 120 121
  tSma.tagsFilterLen = strlen(tagsFilter);
  tSma.tagsFilter = (char *)calloc(tSma.tagsFilterLen + 1, 1);
  tstrncpy(tSma.tagsFilter, tagsFilter, tSma.tagsFilterLen + 1);
C
Cary Xu 已提交
122 123

  SMeta *         pMeta = NULL;
C
Cary Xu 已提交
124
  STSma *         pSmaCfg = &tSma;
C
Cary Xu 已提交
125 126 127 128 129 130 131
  const SMetaCfg *pMetaCfg = &defaultMetaOptions;

  taosRemoveDir(smaTestDir);

  pMeta = metaOpen(smaTestDir, pMetaCfg, NULL);
  assert(pMeta != NULL);
  // save index 1
C
Cary Xu 已提交
132
  EXPECT_EQ(metaSaveSmaToDB(pMeta, pSmaCfg), 0);
C
Cary Xu 已提交
133

C
Cary Xu 已提交
134
  pSmaCfg->indexUid = indexUid2;
C
Cary Xu 已提交
135 136 137 138 139 140 141 142
  tstrncpy(pSmaCfg->indexName, smaIndexName2, TSDB_INDEX_NAME_LEN);
  pSmaCfg->version = 1;
  pSmaCfg->intervalUnit = TD_TIME_UNIT_HOUR;
  pSmaCfg->interval = 1;
  pSmaCfg->slidingUnit = TD_TIME_UNIT_MINUTE;
  pSmaCfg->sliding = 5;

  // save index 2
C
Cary Xu 已提交
143
  EXPECT_EQ(metaSaveSmaToDB(pMeta, pSmaCfg), 0);
C
Cary Xu 已提交
144 145

  // get value by indexName
C
Cary Xu 已提交
146
  STSma *qSmaCfg = NULL;
C
Cary Xu 已提交
147
  qSmaCfg = metaGetSmaInfoByIndex(pMeta, indexUid1);
C
Cary Xu 已提交
148 149
  assert(qSmaCfg != NULL);
  printf("name1 = %s\n", qSmaCfg->indexName);
C
Cary Xu 已提交
150
  printf("timezone1 = %s\n", qSmaCfg->timezone);
C
Cary Xu 已提交
151
  printf("expr1 = %s\n", qSmaCfg->expr != NULL ? qSmaCfg->expr : "");
C
Cary Xu 已提交
152
  printf("tagsFilter1 = %s\n", qSmaCfg->tagsFilter != NULL ? qSmaCfg->tagsFilter : "");
C
Cary Xu 已提交
153 154
  EXPECT_STRCASEEQ(qSmaCfg->indexName, smaIndexName1);
  EXPECT_EQ(qSmaCfg->tableUid, tSma.tableUid);
C
Cary Xu 已提交
155
  tdDestroyTSma(qSmaCfg);
C
Cary Xu 已提交
156
  tfree(qSmaCfg);
C
Cary Xu 已提交
157

C
Cary Xu 已提交
158
  qSmaCfg = metaGetSmaInfoByIndex(pMeta, indexUid2);
C
Cary Xu 已提交
159 160
  assert(qSmaCfg != NULL);
  printf("name2 = %s\n", qSmaCfg->indexName);
C
Cary Xu 已提交
161
  printf("timezone2 = %s\n", qSmaCfg->timezone);
C
Cary Xu 已提交
162
  printf("expr2 = %s\n", qSmaCfg->expr != NULL ? qSmaCfg->expr : "");
C
Cary Xu 已提交
163
  printf("tagsFilter2 = %s\n", qSmaCfg->tagsFilter != NULL ? qSmaCfg->tagsFilter : "");
C
Cary Xu 已提交
164 165
  EXPECT_STRCASEEQ(qSmaCfg->indexName, smaIndexName2);
  EXPECT_EQ(qSmaCfg->interval, tSma.interval);
C
Cary Xu 已提交
166
  tdDestroyTSma(qSmaCfg);
C
Cary Xu 已提交
167
  tfree(qSmaCfg);
C
Cary Xu 已提交
168

C
Cary Xu 已提交
169
  // get index name by table uid
C
Cary Xu 已提交
170 171 172 173
  SMSmaCursor *pSmaCur = metaOpenSmaCursor(pMeta, tbUid);
  assert(pSmaCur != NULL);
  uint32_t indexCnt = 0;
  while (1) {
C
Cary Xu 已提交
174
    const char *indexName = metaSmaCursorNext(pSmaCur);
C
Cary Xu 已提交
175 176 177 178 179 180
    if (indexName == NULL) {
      break;
    }
    printf("indexName = %s\n", indexName);
    ++indexCnt;
  }
C
Cary Xu 已提交
181
  EXPECT_EQ(indexCnt, nCntTSma);
C
Cary Xu 已提交
182 183
  metaCloseSmaCurosr(pSmaCur);

C
Cary Xu 已提交
184
  // get wrapper by table uid
C
Cary Xu 已提交
185
  STSmaWrapper *pSW = metaGetSmaInfoByTable(pMeta, tbUid);
C
Cary Xu 已提交
186
  assert(pSW != NULL);
C
Cary Xu 已提交
187
  EXPECT_EQ(pSW->number, nCntTSma);
C
Cary Xu 已提交
188
  EXPECT_STRCASEEQ(pSW->tSma->indexName, smaIndexName1);
C
Cary Xu 已提交
189 190
  EXPECT_STRCASEEQ(pSW->tSma->timezone, timezone);
  EXPECT_STRCASEEQ(pSW->tSma->expr, expr);
C
Cary Xu 已提交
191
  EXPECT_STRCASEEQ(pSW->tSma->tagsFilter, tagsFilter);
C
Cary Xu 已提交
192 193
  EXPECT_EQ(pSW->tSma->indexUid, indexUid1);
  EXPECT_EQ(pSW->tSma->tableUid, tbUid);
C
Cary Xu 已提交
194
  EXPECT_STRCASEEQ((pSW->tSma + 1)->indexName, smaIndexName2);
C
Cary Xu 已提交
195 196
  EXPECT_STRCASEEQ((pSW->tSma + 1)->timezone, timezone);
  EXPECT_STRCASEEQ((pSW->tSma + 1)->expr, expr);
C
Cary Xu 已提交
197
  EXPECT_STRCASEEQ((pSW->tSma + 1)->tagsFilter, tagsFilter);
C
Cary Xu 已提交
198 199
  EXPECT_EQ((pSW->tSma + 1)->indexUid, indexUid2);
  EXPECT_EQ((pSW->tSma + 1)->tableUid, tbUid);
C
Cary Xu 已提交
200

C
Cary Xu 已提交
201 202 203 204 205 206 207 208 209 210 211 212
  tdDestroyTSmaWrapper(pSW);
  tfree(pSW);

  // get all sma table uids
  SArray *pUids = metaGetSmaTbUids(pMeta, false);
  assert(pUids != NULL);
  for (uint32_t i = 0; i < taosArrayGetSize(pUids); ++i) {
    printf("metaGetSmaTbUids: uid[%" PRIu32 "] = %" PRIi64 "\n", i, *(tb_uid_t *)taosArrayGet(pUids, i));
    // printf("metaGetSmaTbUids: index[%" PRIu32 "] = %s", i, (char *)taosArrayGet(pUids, i));
  }
  EXPECT_EQ(taosArrayGetSize(pUids), 1);
  taosArrayDestroy(pUids);
C
Cary Xu 已提交
213

C
Cary Xu 已提交
214 215 216 217
  // resource release
  metaRemoveSmaFromDb(pMeta, smaIndexName1);
  metaRemoveSmaFromDb(pMeta, smaIndexName2);

C
Cary Xu 已提交
218
  tdDestroyTSma(&tSma);
C
Cary Xu 已提交
219
  metaClose(pMeta);
C
Cary Xu 已提交
220
}
C
Cary Xu 已提交
221
#endif
C
Cary Xu 已提交
222

C
Cary Xu 已提交
223
#if 1
C
Cary Xu 已提交
224 225
TEST(testCase, tSma_Data_Insert_Query_Test) {
  // step 1: prepare meta
C
Cary Xu 已提交
226 227 228
  const char *   smaIndexName1 = "sma_index_test_1";
  const char *   timezone = "Asia/Shanghai";
  const char *   expr = "select count(a,b, top 20), from table interval 1d, sliding 1h;";
C
Cary Xu 已提交
229
  const char *   tagsFilter = "where tags.location='Beijing' and tags.district='ChaoYang'";
C
Cary Xu 已提交
230 231 232
  const char *   smaTestDir = "./smaTest";
  const tb_uid_t tbUid = 1234567890;
  const int64_t  indexUid1 = 2000000001;
C
Cary Xu 已提交
233 234
  const int64_t  interval1 = 1;
  const int8_t   intervalUnit1 = TD_TIME_UNIT_DAY;
C
Cary Xu 已提交
235
  const uint32_t nCntTSma = 2;
C
Cary Xu 已提交
236 237 238
  TSKEY          skey1 = 1646987196;
  const int64_t  testSmaData1 = 100;
  const int64_t  testSmaData2 = 200;
C
Cary Xu 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
  // encode
  STSma tSma = {0};
  tSma.version = 0;
  tSma.intervalUnit = TD_TIME_UNIT_DAY;
  tSma.interval = 1;
  tSma.slidingUnit = TD_TIME_UNIT_HOUR;
  tSma.sliding = 0;
  tSma.indexUid = indexUid1;
  tstrncpy(tSma.indexName, smaIndexName1, TSDB_INDEX_NAME_LEN);
  tstrncpy(tSma.timezone, timezone, TD_TIMEZONE_LEN);
  tSma.tableUid = tbUid;

  tSma.exprLen = strlen(expr);
  tSma.expr = (char *)calloc(tSma.exprLen + 1, 1);
  tstrncpy(tSma.expr, expr, tSma.exprLen + 1);

  tSma.tagsFilterLen = strlen(tagsFilter);
  tSma.tagsFilter = (char *)calloc(tSma.tagsFilterLen + 1, 1);
  tstrncpy(tSma.tagsFilter, tagsFilter, tSma.tagsFilterLen + 1);

  SMeta *         pMeta = NULL;
  STSma *         pSmaCfg = &tSma;
  const SMetaCfg *pMetaCfg = &defaultMetaOptions;

  taosRemoveDir(smaTestDir);

  pMeta = metaOpen(smaTestDir, pMetaCfg, NULL);
  assert(pMeta != NULL);
  // save index 1
  EXPECT_EQ(metaSaveSmaToDB(pMeta, pSmaCfg), 0);

C
Cary Xu 已提交
270
  // step 2: insert data
C
Cary Xu 已提交
271 272 273 274
  STSmaDataWrapper *pSmaData = NULL;
  STsdb             tsdb = {0};
  STsdbCfg *        pCfg = &tsdb.config;

C
Cary Xu 已提交
275
  tsdb.pMeta = pMeta;
276 277 278 279 280 281 282 283
  tsdb.vgId = 2;
  tsdb.config.daysPerFile = 10;  // default days is 10
  tsdb.config.keep1 = 30;
  tsdb.config.keep2 = 90;
  tsdb.config.keep = 365;
  tsdb.config.precision = TSDB_TIME_PRECISION_MILLI;
  tsdb.config.update = TD_ROW_OVERWRITE_UPDATE;
  tsdb.config.compression = TWO_STAGE_COMP;
C
Cary Xu 已提交
284

C
Cary Xu 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
  switch (tsdb.config.precision) {
    case TSDB_TIME_PRECISION_MILLI:
      skey1 *= 1e3;
      break;
    case TSDB_TIME_PRECISION_MICRO:
      skey1 *= 1e6;
      break;
    case TSDB_TIME_PRECISION_NANO:
      skey1 *= 1e9;
      break;
    default:  // ms
      skey1 *= 1e3;
      break;
  }

C
Cary Xu 已提交
300 301
  char *msg = (char *)calloc(100, 1);
  EXPECT_EQ(tsdbUpdateSmaWindow(&tsdb, TSDB_SMA_TYPE_TIME_RANGE, msg), 0);
C
Cary Xu 已提交
302 303

  // init
C
Cary Xu 已提交
304 305 306 307 308 309
  int32_t allocCnt = 0;
  int32_t allocStep = 40960;
  int32_t buffer = 4096;
  void *  buf = NULL;
  EXPECT_EQ(tsdbMakeRoom(&buf, allocStep), 0);
  int32_t  bufSize = taosTSizeof(buf);
C
Cary Xu 已提交
310 311
  int32_t  numOfTables = 10;
  col_id_t numOfCols = 4096;
C
Cary Xu 已提交
312 313 314 315
  EXPECT_GT(numOfCols, 0);

  pSmaData = (STSmaDataWrapper *)buf;
  printf(">> allocate [%d] time to %d and addr is %p\n", ++allocCnt, bufSize, pSmaData);
C
Cary Xu 已提交
316 317 318
  pSmaData->skey = skey1;
  pSmaData->interval = interval1;
  pSmaData->intervalUnit = intervalUnit1;
319
  pSmaData->indexUid = indexUid1;
C
Cary Xu 已提交
320 321 322 323

  int32_t len = sizeof(STSmaDataWrapper);
  for (int32_t t = 0; t < numOfTables; ++t) {
    STSmaTbData *pTbData = (STSmaTbData *)POINTER_SHIFT(pSmaData, len);
C
Cary Xu 已提交
324
    pTbData->tableUid = tbUid + t;
C
Cary Xu 已提交
325 326 327 328 329 330 331 332 333 334 335 336

    int32_t tableDataLen = sizeof(STSmaTbData);
    for (col_id_t c = 0; c < numOfCols; ++c) {
      if (bufSize - len - tableDataLen < buffer) {
        EXPECT_EQ(tsdbMakeRoom(&buf, bufSize + allocStep), 0);
        pSmaData = (STSmaDataWrapper *)buf;
        pTbData = (STSmaTbData *)POINTER_SHIFT(pSmaData, len);
        bufSize = taosTSizeof(buf);
        printf(">> allocate [%d] time to %d and addr is %p\n", ++allocCnt, bufSize, pSmaData);
      }
      STSmaColData *pColData = (STSmaColData *)POINTER_SHIFT(pSmaData, len + tableDataLen);
      pColData->colId = c + PRIMARYKEY_TIMESTAMP_COL_ID;
C
Cary Xu 已提交
337

C
Cary Xu 已提交
338
      // TODO: fill col data
C
Cary Xu 已提交
339 340 341 342 343 344 345 346 347
      if ((c & 1) == 0) {
        pColData->blockSize = 8;
        memcpy(pColData->data, &testSmaData1, 8);
      } else {
        pColData->blockSize = 16;
        memcpy(pColData->data, &testSmaData1, 8);
        memcpy(POINTER_SHIFT(pColData->data, 8), &testSmaData2, 8);
      }

C
Cary Xu 已提交
348 349 350 351 352
      tableDataLen += (sizeof(STSmaColData) + pColData->blockSize);
    }
    pTbData->dataLen = (tableDataLen - sizeof(STSmaTbData));
    len += tableDataLen;
    // printf("bufSize=%d, len=%d, len of table[%d]=%d\n", bufSize, len, t, tableDataLen);
C
Cary Xu 已提交
353
  }
C
Cary Xu 已提交
354 355 356
  pSmaData->dataLen = (len - sizeof(STSmaDataWrapper));

  EXPECT_GE(bufSize, pSmaData->dataLen);
C
Cary Xu 已提交
357 358

  // execute
C
Cary Xu 已提交
359
  EXPECT_EQ(tsdbInsertTSmaData(&tsdb, (char *)pSmaData), TSDB_CODE_SUCCESS);
C
Cary Xu 已提交
360

C
Cary Xu 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373
  // step 3: query
  uint32_t checkDataCnt = 0;
  for (int32_t t = 0; t < numOfTables; ++t) {
    for (col_id_t c = 0; c < numOfCols; ++c) {
      EXPECT_EQ(tsdbGetTSmaData(&tsdb, NULL, indexUid1, interval1, intervalUnit1, tbUid + t,
                                c + PRIMARYKEY_TIMESTAMP_COL_ID, skey1, 1),
                TSDB_CODE_SUCCESS);
      ++checkDataCnt;
    }
  }
  
  printf("%s:%d The sma data check count for insert and query is %" PRIu32 "\n", __FILE__, __LINE__, checkDataCnt);

C
Cary Xu 已提交
374
  // release data
C
Cary Xu 已提交
375
  taosTZfree(buf);
C
Cary Xu 已提交
376 377 378
  // release meta
  tdDestroyTSma(&tSma);
  metaClose(pMeta);
C
Cary Xu 已提交
379 380 381 382
}
#endif

#pragma GCC diagnostic pop