catalogTests.cpp 35.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

D
dapan1121 已提交
16
#include <gtest/gtest.h>
17 18 19 20 21 22 23 24 25
#include <tglobal.h>
#include <iostream>
#pragma GCC diagnostic ignored "-Wwrite-strings"

#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"
#include "os.h"

D
dapan1121 已提交
26
#include "taos.h"
27
#include "tdef.h"
D
dapan1121 已提交
28 29 30
#include "tvariant.h"
#include "catalog.h"
#include "tep.h"
D
ut test  
dapan1121 已提交
31
#include "trpc.h"
D
dapan1121 已提交
32 33
#include "stub.h"
#include "addr_any.h"
D
dapan1121 已提交
34

D
dapan1121 已提交
35

36

D
dapan1121 已提交
37
namespace {
38

D
dapan1121 已提交
39 40 41
extern "C" int32_t ctgGetTableMetaFromCache(struct SCatalog* pCatalog, const SName* pTableName, STableMeta** pTableMeta, int32_t *exist);
extern "C" int32_t ctgUpdateTableMetaCache(struct SCatalog *pCatalog, STableMetaOutput *output);

D
dapan 已提交
42
void ctgTestSetPrepareTableMeta();
D
dapan1121 已提交
43 44
void ctgTestSetPrepareCTableMeta();
void ctgTestSetPrepareSTableMeta();
D
dapan1121 已提交
45
void ctgTestSetPrepareMultiSTableMeta();
D
dapan1121 已提交
46

D
dapan1121 已提交
47 48
bool ctgTestStop = false;
bool ctgTestEnableSleep = false;
D
dapan1121 已提交
49
bool ctgTestDeadLoop = false;
D
dapan1121 已提交
50
int32_t ctgTestPrintNum = 200000;
D
dapan1121 已提交
51
int32_t ctgTestMTRunSec = 30;
D
dapan1121 已提交
52

D
dapan1121 已提交
53 54
int32_t ctgTestCurrentVgVersion = 0;
int32_t ctgTestVgVersion = 1;
D
dapan1121 已提交
55
int32_t ctgTestVgNum = 10;
D
dapan1121 已提交
56 57 58 59
int32_t ctgTestColNum = 2;
int32_t ctgTestTagNum = 1;
int32_t ctgTestSVersion = 1;
int32_t ctgTestTVersion = 1;
D
dapan1121 已提交
60 61
int32_t ctgTestSuid = 2;
int64_t ctgTestDbId = 33;
D
dapan1121 已提交
62

63
uint64_t ctgTestClusterId = 0x1;
D
dapan1121 已提交
64 65
char *ctgTestDbname = "1.db1";
char *ctgTestTablename = "table1";
D
dapan1121 已提交
66 67
char *ctgTestCTablename = "ctable1";
char *ctgTestSTablename = "stable1";
D
dapan1121 已提交
68 69


D
ut test  
dapan1121 已提交
70
void sendCreateDbMsg(void *shandle, SEpSet *pEpSet) {
S
Shengliang Guan 已提交
71
  SCreateDbReq* pReq = (SCreateDbReq*)rpcMallocCont(sizeof(SCreateDbReq));
D
ut test  
dapan1121 已提交
72 73 74 75 76 77 78 79
  strcpy(pReq->db, "1.db1");
  pReq->numOfVgroups = htonl(2);
  pReq->cacheBlockSize = htonl(16);
  pReq->totalBlocks = htonl(10);
  pReq->daysPerFile = htonl(10);
  pReq->daysToKeep0 = htonl(3650);
  pReq->daysToKeep1 = htonl(3650);
  pReq->daysToKeep2 = htonl(3650);
S
Shengliang Guan 已提交
80 81
  pReq->minRows = htonl(100);
  pReq->maxRows = htonl(4096);
D
ut test  
dapan1121 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94
  pReq->commitTime = htonl(3600);
  pReq->fsyncPeriod = htonl(3000);
  pReq->walLevel = 1;
  pReq->precision = 0;
  pReq->compression = 2;
  pReq->replications = 1;
  pReq->quorum = 1;
  pReq->update = 0;
  pReq->cacheLastRow = 0;
  pReq->ignoreExist = 1;
  
  SRpcMsg rpcMsg = {0};
  rpcMsg.pCont = pReq;
S
Shengliang Guan 已提交
95
  rpcMsg.contLen = sizeof(SCreateDbReq);
H
Hongze Cheng 已提交
96
  rpcMsg.msgType = TDMT_MND_CREATE_DB;
D
ut test  
dapan1121 已提交
97 98 99 100 101 102 103 104

  SRpcMsg rpcRsp = {0};

  rpcSendRecv(shandle, pEpSet, &rpcMsg, &rpcRsp);

  ASSERT_EQ(rpcRsp.code, 0);
}

D
dapan1121 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
void ctgTestInitLogFile() {
  const char    *defaultLogFileNamePrefix = "taoslog";
  const int32_t  maxLogFileNum = 10;

  tsAsyncLog = 0;

  char temp[128] = {0};
  sprintf(temp, "%s/%s", tsLogDir, defaultLogFileNamePrefix);
  if (taosInitLog(temp, tsNumOfLogLines, maxLogFileNum) < 0) {
    printf("failed to open log file in directory:%s\n", tsLogDir);
  }

}

int32_t ctgTestGetVgNumFromVgVersion(int32_t vgVersion) {
  return ((vgVersion % 2) == 0) ? ctgTestVgNum - 2 : ctgTestVgNum;
}

void ctgTestBuildCTableMetaOutput(STableMetaOutput *output) {
  SName cn = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
  strcpy(cn.dbname, "db1");
  strcpy(cn.tname, ctgTestCTablename);

  SName sn = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
  strcpy(sn.dbname, "db1");
  strcpy(sn.tname, ctgTestSTablename);

  char tbFullName[TSDB_TABLE_FNAME_LEN];
  tNameExtractFullName(&cn, tbFullName);

D
dapan1121 已提交
135
  SET_META_TYPE_BOTH_TABLE(output->metaType);
D
dapan1121 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

  strcpy(output->ctbFname, tbFullName);

  tNameExtractFullName(&cn, tbFullName);
  strcpy(output->tbFname, tbFullName);

  output->ctbMeta.vgId = 9;
  output->ctbMeta.tableType = TSDB_CHILD_TABLE;
  output->ctbMeta.uid = 3;
  output->ctbMeta.suid = 2;

  output->tbMeta = (STableMeta *)calloc(1, sizeof(STableMeta) + sizeof(SSchema) * (ctgTestColNum + ctgTestColNum));
  output->tbMeta->vgId = 9;
  output->tbMeta->tableType = TSDB_SUPER_TABLE;
  output->tbMeta->uid = 2;
  output->tbMeta->suid = 2;

  output->tbMeta->tableInfo.numOfColumns = ctgTestColNum;
  output->tbMeta->tableInfo.numOfTags = ctgTestTagNum;
  
  output->tbMeta->sversion = ctgTestSVersion;
  output->tbMeta->tversion = ctgTestTVersion;
  
  SSchema *s = NULL;
  s = &output->tbMeta->schema[0];
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
  s->colId = 1;
  s->bytes = 8;
  strcpy(s->name, "ts");

  s = &output->tbMeta->schema[1];
  s->type = TSDB_DATA_TYPE_INT;
  s->colId = 2;
  s->bytes = 4;
  strcpy(s->name, "col1s");

  s = &output->tbMeta->schema[2];
  s->type = TSDB_DATA_TYPE_BINARY;
  s->colId = 3;
  s->bytes = 12;
  strcpy(s->name, "tag1s");
  
}

void ctgTestBuildDBVgroup(SDBVgroupInfo *dbVgroup) {
  static int32_t vgVersion = ctgTestVgVersion + 1;
  int32_t vgNum = 0;
  SVgroupInfo vgInfo = {0};
  
  dbVgroup->vgVersion = vgVersion++;
  
  ctgTestCurrentVgVersion = dbVgroup->vgVersion;
  
  dbVgroup->hashMethod = 0;
D
dapan1121 已提交
190
  dbVgroup->dbId = ctgTestDbId;
D
dapan1121 已提交
191 192 193 194 195 196 197 198 199 200 201 202
  dbVgroup->vgInfo = taosHashInit(ctgTestVgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);

  vgNum = ctgTestGetVgNumFromVgVersion(dbVgroup->vgVersion);
  uint32_t hashUnit = UINT32_MAX / vgNum;

  for (int32_t i = 0; i < vgNum; ++i) {
    vgInfo.vgId = i + 1;
    vgInfo.hashBegin = i * hashUnit;
    vgInfo.hashEnd = hashUnit * (i + 1) - 1;
    vgInfo.numOfEps = i % TSDB_MAX_REPLICA + 1;
    vgInfo.inUse = i % vgInfo.numOfEps;
    for (int32_t n = 0; n < vgInfo.numOfEps; ++n) {
S
Shengliang Guan 已提交
203
      SEpAddr *addr = &vgInfo.epAddr[n];
D
dapan1121 已提交
204 205 206 207 208 209 210 211
      strcpy(addr->fqdn, "a0");
      addr->port = htons(n + 22);
    }

    taosHashPut(dbVgroup->vgInfo, &vgInfo.vgId, sizeof(vgInfo.vgId), &vgInfo, sizeof(vgInfo));
  }
}

D
dapan1121 已提交
212
void ctgTestPrepareDbVgroups(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
D
dapan1121 已提交
213 214
  SUseDbRsp *rspMsg = NULL; //todo

D
dapan1121 已提交
215 216 217 218 219
  pRsp->code =0;
  pRsp->contLen = sizeof(SUseDbRsp) + ctgTestVgNum * sizeof(SVgroupInfo);
  pRsp->pCont = calloc(1, pRsp->contLen);
  rspMsg = (SUseDbRsp *)pRsp->pCont;
  strcpy(rspMsg->db, ctgTestDbname);
D
dapan1121 已提交
220 221
  rspMsg->vgVersion = htonl(ctgTestVgVersion);
  ctgTestCurrentVgVersion = ctgTestVgVersion;
D
dapan1121 已提交
222
  rspMsg->vgNum = htonl(ctgTestVgNum);
D
dapan1121 已提交
223
  rspMsg->hashMethod = 0;
D
dapan1121 已提交
224
  rspMsg->uid = htobe64(ctgTestDbId);
D
dapan1121 已提交
225 226 227 228 229 230

  SVgroupInfo *vg = NULL;
  uint32_t hashUnit = UINT32_MAX / ctgTestVgNum;
  for (int32_t i = 0; i < ctgTestVgNum; ++i) {
    vg = &rspMsg->vgroupInfo[i];

D
dapan1121 已提交
231 232 233
    vg->vgId = htonl(i + 1);
    vg->hashBegin = htonl(i * hashUnit);
    vg->hashEnd = htonl(hashUnit * (i + 1) - 1);
D
dapan1121 已提交
234 235 236
    vg->numOfEps = i % TSDB_MAX_REPLICA + 1;
    vg->inUse = i % vg->numOfEps;
    for (int32_t n = 0; n < vg->numOfEps; ++n) {
S
Shengliang Guan 已提交
237
      SEpAddr *addr = &vg->epAddr[n];
D
dapan1121 已提交
238
      strcpy(addr->fqdn, "a0");
D
dapan1121 已提交
239
      addr->port = htons(n + 22);
D
dapan1121 已提交
240 241 242
    }
  }

D
dapan1121 已提交
243 244 245 246 247 248
  vg->hashEnd = htonl(UINT32_MAX);

  return;
}


D
dapan1121 已提交
249 250


D
dapan1121 已提交
251
void ctgTestPrepareTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
252
  STableMetaRsp *rspMsg = NULL; //todo
D
dapan1121 已提交
253 254

  pRsp->code =0;
S
Shengliang Guan 已提交
255
  pRsp->contLen = sizeof(STableMetaRsp) + (ctgTestColNum + ctgTestTagNum) * sizeof(SSchema);
D
dapan1121 已提交
256
  pRsp->pCont = calloc(1, pRsp->contLen);
S
Shengliang Guan 已提交
257
  rspMsg = (STableMetaRsp *)pRsp->pCont;
D
dapan1121 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  sprintf(rspMsg->tbFname, "%s.%s", ctgTestDbname, ctgTestTablename);
  rspMsg->numOfTags = 0;
  rspMsg->numOfColumns = htonl(ctgTestColNum);
  rspMsg->precision = 1;
  rspMsg->tableType = TSDB_NORMAL_TABLE;
  rspMsg->update = 1;
  rspMsg->sversion = htonl(ctgTestSVersion);
  rspMsg->tversion = htonl(ctgTestTVersion);
  rspMsg->suid = 0;
  rspMsg->tuid = htobe64(0x0000000000000001);
  rspMsg->vgId = htonl(8);

  SSchema *s = NULL;
  s = &rspMsg->pSchema[0];
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
D
dapan1121 已提交
273
  s->colId = htonl(1);
D
dapan1121 已提交
274 275 276 277 278
  s->bytes = htonl(8);
  strcpy(s->name, "ts");

  s = &rspMsg->pSchema[1];
  s->type = TSDB_DATA_TYPE_INT;
D
dapan1121 已提交
279
  s->colId = htonl(2);
D
dapan1121 已提交
280 281 282 283 284 285 286 287
  s->bytes = htonl(4);
  strcpy(s->name, "col1");
  
  return;
}


void ctgTestPrepareCTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
288
  STableMetaRsp *rspMsg = NULL; //todo
D
dapan1121 已提交
289 290

  pRsp->code =0;
S
Shengliang Guan 已提交
291
  pRsp->contLen = sizeof(STableMetaRsp) + (ctgTestColNum + ctgTestTagNum) * sizeof(SSchema);
D
dapan1121 已提交
292
  pRsp->pCont = calloc(1, pRsp->contLen);
S
Shengliang Guan 已提交
293
  rspMsg = (STableMetaRsp *)pRsp->pCont;
D
dapan1121 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
  sprintf(rspMsg->tbFname, "%s.%s", ctgTestDbname, ctgTestCTablename);
  sprintf(rspMsg->stbFname, "%s.%s", ctgTestDbname, ctgTestSTablename);
  rspMsg->numOfTags = htonl(ctgTestTagNum);
  rspMsg->numOfColumns = htonl(ctgTestColNum);
  rspMsg->precision = 1;
  rspMsg->tableType = TSDB_CHILD_TABLE;
  rspMsg->update = 1;
  rspMsg->sversion = htonl(ctgTestSVersion);
  rspMsg->tversion = htonl(ctgTestTVersion);
  rspMsg->suid = htobe64(0x0000000000000002);
  rspMsg->tuid = htobe64(0x0000000000000003);
  rspMsg->vgId = htonl(9);

  SSchema *s = NULL;
  s = &rspMsg->pSchema[0];
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
D
dapan1121 已提交
310
  s->colId = htonl(1);
D
dapan1121 已提交
311 312 313 314 315
  s->bytes = htonl(8);
  strcpy(s->name, "ts");

  s = &rspMsg->pSchema[1];
  s->type = TSDB_DATA_TYPE_INT;
D
dapan1121 已提交
316
  s->colId = htonl(2);
D
dapan1121 已提交
317 318 319 320 321
  s->bytes = htonl(4);
  strcpy(s->name, "col1s");

  s = &rspMsg->pSchema[2];
  s->type = TSDB_DATA_TYPE_BINARY;
D
dapan1121 已提交
322
  s->colId = htonl(3);
D
dapan1121 已提交
323 324 325 326 327 328 329 330 331
  s->bytes = htonl(12);
  strcpy(s->name, "tag1s");

  
  return;
}


void ctgTestPrepareSTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
332
  STableMetaRsp *rspMsg = NULL; //todo
D
dapan1121 已提交
333 334

  pRsp->code =0;
S
Shengliang Guan 已提交
335
  pRsp->contLen = sizeof(STableMetaRsp) + (ctgTestColNum + ctgTestTagNum) * sizeof(SSchema);
D
dapan1121 已提交
336
  pRsp->pCont = calloc(1, pRsp->contLen);
S
Shengliang Guan 已提交
337
  rspMsg = (STableMetaRsp *)pRsp->pCont;
D
dapan1121 已提交
338 339 340 341 342 343 344 345 346
  sprintf(rspMsg->tbFname, "%s.%s", ctgTestDbname, ctgTestSTablename);
  sprintf(rspMsg->stbFname, "%s.%s", ctgTestDbname, ctgTestSTablename);
  rspMsg->numOfTags = htonl(ctgTestTagNum);
  rspMsg->numOfColumns = htonl(ctgTestColNum);
  rspMsg->precision = 1;
  rspMsg->tableType = TSDB_SUPER_TABLE;
  rspMsg->update = 1;
  rspMsg->sversion = htonl(ctgTestSVersion);
  rspMsg->tversion = htonl(ctgTestTVersion);
D
dapan1121 已提交
347 348
  rspMsg->suid = htobe64(ctgTestSuid);
  rspMsg->tuid = htobe64(ctgTestSuid);
D
dapan1121 已提交
349 350 351 352 353
  rspMsg->vgId = 0;

  SSchema *s = NULL;
  s = &rspMsg->pSchema[0];
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
D
dapan1121 已提交
354
  s->colId = htonl(1);
D
dapan1121 已提交
355 356 357 358 359
  s->bytes = htonl(8);
  strcpy(s->name, "ts");

  s = &rspMsg->pSchema[1];
  s->type = TSDB_DATA_TYPE_INT;
D
dapan1121 已提交
360
  s->colId = htonl(2);
D
dapan1121 已提交
361 362 363 364 365
  s->bytes = htonl(4);
  strcpy(s->name, "col1s");

  s = &rspMsg->pSchema[2];
  s->type = TSDB_DATA_TYPE_BINARY;
D
dapan1121 已提交
366
  s->colId = htonl(3);
D
dapan1121 已提交
367 368 369 370 371 372 373
  s->bytes = htonl(12);
  strcpy(s->name, "tag1s");

  
  return;
}

D
dapan1121 已提交
374
void ctgTestPrepareMultiSTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
375
  STableMetaRsp *rspMsg = NULL; //todo
D
dapan1121 已提交
376 377 378
  static int32_t idx = 1;

  pRsp->code =0;
S
Shengliang Guan 已提交
379
  pRsp->contLen = sizeof(STableMetaRsp) + (ctgTestColNum + ctgTestTagNum) * sizeof(SSchema);
D
dapan1121 已提交
380
  pRsp->pCont = calloc(1, pRsp->contLen);
S
Shengliang Guan 已提交
381
  rspMsg = (STableMetaRsp *)pRsp->pCont;
D
dapan1121 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
  sprintf(rspMsg->tbFname, "%s.%s_%d", ctgTestDbname, ctgTestSTablename, idx);
  sprintf(rspMsg->stbFname, "%s.%s_%d", ctgTestDbname, ctgTestSTablename, idx);
  rspMsg->numOfTags = htonl(ctgTestTagNum);
  rspMsg->numOfColumns = htonl(ctgTestColNum);
  rspMsg->precision = 1;
  rspMsg->tableType = TSDB_SUPER_TABLE;
  rspMsg->update = 1;
  rspMsg->sversion = htonl(ctgTestSVersion);
  rspMsg->tversion = htonl(ctgTestTVersion);
  rspMsg->suid = htobe64(ctgTestSuid + idx);
  rspMsg->tuid = htobe64(ctgTestSuid + idx);
  rspMsg->vgId = 0;

  SSchema *s = NULL;
  s = &rspMsg->pSchema[0];
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
  s->colId = htonl(1);
  s->bytes = htonl(8);
  strcpy(s->name, "ts");

  s = &rspMsg->pSchema[1];
  s->type = TSDB_DATA_TYPE_INT;
  s->colId = htonl(2);
  s->bytes = htonl(4);
  strcpy(s->name, "col1s");

  s = &rspMsg->pSchema[2];
  s->type = TSDB_DATA_TYPE_BINARY;
  s->colId = htonl(3);
  s->bytes = htonl(12);
  strcpy(s->name, "tag1s");

  ++idx;
  
  return;
}



D
dapan 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
void ctgTestPrepareDbVgroupsAndNormalMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestPrepareDbVgroups(shandle, pEpSet, pMsg, pRsp);
  
  ctgTestSetPrepareTableMeta();
  
  return;
}


void ctgTestPrepareDbVgroupsAndChildMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestPrepareDbVgroups(shandle, pEpSet, pMsg, pRsp);
  
  ctgTestSetPrepareCTableMeta();
  
  return;
}

void ctgTestPrepareDbVgroupsAndSuperMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestPrepareDbVgroups(shandle, pEpSet, pMsg, pRsp);
  
  ctgTestSetPrepareSTableMeta();
  
  return;
}

D
dapan1121 已提交
446 447 448 449 450 451 452 453
void ctgTestPrepareDbVgroupsAndMultiSuperMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestPrepareDbVgroups(shandle, pEpSet, pMsg, pRsp);
  
  ctgTestSetPrepareMultiSTableMeta();
  
  return;
}

D
dapan 已提交
454

D
dapan1121 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507

void ctgTestSetPrepareDbVgroups() {
  static Stub stub;
  stub.set(rpcSendRecv, ctgTestPrepareDbVgroups);
  {
    AddrAny any("libtransport.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
    for (const auto& f : result) {
      stub.set(f.second, ctgTestPrepareDbVgroups);
    }
  }
}

void ctgTestSetPrepareTableMeta() {
  static Stub stub;
  stub.set(rpcSendRecv, ctgTestPrepareTableMeta);
  {
    AddrAny any("libtransport.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
    for (const auto& f : result) {
      stub.set(f.second, ctgTestPrepareTableMeta);
    }
  }
}

void ctgTestSetPrepareCTableMeta() {
  static Stub stub;
  stub.set(rpcSendRecv, ctgTestPrepareCTableMeta);
  {
    AddrAny any("libtransport.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
    for (const auto& f : result) {
      stub.set(f.second, ctgTestPrepareCTableMeta);
    }
  }
}

void ctgTestSetPrepareSTableMeta() {
  static Stub stub;
  stub.set(rpcSendRecv, ctgTestPrepareSTableMeta);
  {
    AddrAny any("libtransport.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
    for (const auto& f : result) {
      stub.set(f.second, ctgTestPrepareSTableMeta);
    }
  }
}

D
dapan1121 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521
void ctgTestSetPrepareMultiSTableMeta() {
  static Stub stub;
  stub.set(rpcSendRecv, ctgTestPrepareMultiSTableMeta);
  {
    AddrAny any("libtransport.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
    for (const auto& f : result) {
      stub.set(f.second, ctgTestPrepareMultiSTableMeta);
    }
  }
}


D
dapan 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534
void ctgTestSetPrepareDbVgroupsAndNormalMeta() {
  static Stub stub;
  stub.set(rpcSendRecv, ctgTestPrepareDbVgroupsAndNormalMeta);
  {
    AddrAny any("libtransport.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
    for (const auto& f : result) {
      stub.set(f.second, ctgTestPrepareDbVgroupsAndNormalMeta);
    }
  }
}

D
dapan1121 已提交
535 536

void ctgTestSetPrepareDbVgroupsAndChildMeta() {
D
dapan1121 已提交
537
  static Stub stub;
D
dapan1121 已提交
538
  stub.set(rpcSendRecv, ctgTestPrepareDbVgroupsAndChildMeta);
D
dapan1121 已提交
539 540 541 542 543
  {
    AddrAny any("libtransport.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
    for (const auto& f : result) {
D
dapan 已提交
544
      stub.set(f.second, ctgTestPrepareDbVgroupsAndChildMeta);
D
dapan1121 已提交
545 546 547 548 549 550 551 552 553 554 555 556
    }
  }
}

void ctgTestSetPrepareDbVgroupsAndSuperMeta() {
  static Stub stub;
  stub.set(rpcSendRecv, ctgTestPrepareDbVgroupsAndSuperMeta);
  {
    AddrAny any("libtransport.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
    for (const auto& f : result) {
D
dapan 已提交
557
      stub.set(f.second, ctgTestPrepareDbVgroupsAndSuperMeta);
D
dapan1121 已提交
558 559 560 561
    }
  }
}

D
dapan1121 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574
void ctgTestSetPrepareDbVgroupsAndMultiSuperMeta() {
  static Stub stub;
  stub.set(rpcSendRecv, ctgTestPrepareDbVgroupsAndMultiSuperMeta);
  {
    AddrAny any("libtransport.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
    for (const auto& f : result) {
      stub.set(f.second, ctgTestPrepareDbVgroupsAndMultiSuperMeta);
    }
  }
}

D
dapan1121 已提交
575

576 577
}

D
dapan1121 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
void *ctgTestGetDbVgroupThread(void *param) {
  struct SCatalog* pCtg = (struct SCatalog*)param;
  int32_t code = 0;
  void *mockPointer = (void *)0x1;
  SArray *vgList = NULL;
  int32_t n = 0;
  
  while (!ctgTestStop) {
    code = catalogGetDBVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, ctgTestDbname, false, &vgList);
    if (code) {
      assert(0);
    }

    if (vgList) {
      taosArrayDestroy(vgList);
    }

    if (ctgTestEnableSleep) {
      usleep(rand()%5);
    }
D
dapan1121 已提交
598
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
      printf("Get:%d\n", n);
    }
  }

  return NULL;
}

void *ctgTestSetDbVgroupThread(void *param) {
  struct SCatalog* pCtg = (struct SCatalog*)param;
  int32_t code = 0;
  SDBVgroupInfo dbVgroup = {0};
  int32_t n = 0;
  
  while (!ctgTestStop) {
    ctgTestBuildDBVgroup(&dbVgroup);
    code = catalogUpdateDBVgroup(pCtg, ctgTestDbname, &dbVgroup);
    if (code) {
      assert(0);
    }

    if (ctgTestEnableSleep) {    
      usleep(rand()%5);
    }
D
dapan1121 已提交
622
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
      printf("Set:%d\n", n);
    }
  }

  return NULL;

}

void *ctgTestGetCtableMetaThread(void *param) {
  struct SCatalog* pCtg = (struct SCatalog*)param;
  int32_t code = 0;
  int32_t n = 0;
  STableMeta* tbMeta = NULL;
  int32_t exist = 0;

  SName cn = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
  strcpy(cn.dbname, "db1");
  strcpy(cn.tname, ctgTestCTablename);
  
  while (!ctgTestStop) {
    code = ctgGetTableMetaFromCache(pCtg, &cn, &tbMeta, &exist);
    if (code || 0 == exist) {
      assert(0);
    }

D
dapan1121 已提交
648 649
    tfree(tbMeta);

D
dapan1121 已提交
650 651 652 653
    if (ctgTestEnableSleep) {
      usleep(rand()%5);
    }
    
D
dapan1121 已提交
654
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
      printf("Get:%d\n", n);
    }
  }

  return NULL;
}

void *ctgTestSetCtableMetaThread(void *param) {
  struct SCatalog* pCtg = (struct SCatalog*)param;
  int32_t code = 0;
  SDBVgroupInfo dbVgroup = {0};
  int32_t n = 0;
  STableMetaOutput output = {0};

  ctgTestBuildCTableMetaOutput(&output);
  
  while (!ctgTestStop) {
    code = ctgUpdateTableMetaCache(pCtg, &output);
    if (code) {
      assert(0);
    }

    if (ctgTestEnableSleep) {    
      usleep(rand()%5);
    }
D
dapan1121 已提交
680
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
681 682 683 684
      printf("Set:%d\n", n);
    }
  }

D
dapan1121 已提交
685 686
  tfree(output.tbMeta);

D
dapan1121 已提交
687 688 689 690 691
  return NULL;

}


D
dapan 已提交
692
TEST(tableMeta, normalTable) {
D
dapan1121 已提交
693 694 695
  struct SCatalog* pCtg = NULL;
  void *mockPointer = (void *)0x1;
  SVgroupInfo vgInfo = {0};
696

D
dapan1121 已提交
697
  ctgTestSetPrepareDbVgroups();
D
dapan1121 已提交
698

699
  initQueryModuleMsgHandle();
D
ut test  
dapan1121 已提交
700

D
dapan1121 已提交
701
  //sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
D
dapan1121 已提交
702 703 704
  
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);
705

D
dapan1121 已提交
706
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan1121 已提交
707
  ASSERT_EQ(code, 0);
708

D
dapan1121 已提交
709
  SName n = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
H
Haojun Liao 已提交
710 711 712 713
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

  code = catalogGetTableHashVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &vgInfo);
D
dapan1121 已提交
714
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
715 716 717
  ASSERT_EQ(vgInfo.vgId, 8);
  ASSERT_EQ(vgInfo.numOfEps, 3);

D
dapan1121 已提交
718

D
dapan1121 已提交
719 720 721
  ctgTestSetPrepareTableMeta();

  STableMeta *tableMeta = NULL;
H
Haojun Liao 已提交
722
  code = catalogGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta);
D
dapan1121 已提交
723 724 725 726 727 728 729 730 731 732 733
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 8);
  ASSERT_EQ(tableMeta->tableType, TSDB_NORMAL_TABLE);
  ASSERT_EQ(tableMeta->sversion, ctgTestSVersion);
  ASSERT_EQ(tableMeta->tversion, ctgTestTVersion);
  ASSERT_EQ(tableMeta->tableInfo.numOfColumns, ctgTestColNum);
  ASSERT_EQ(tableMeta->tableInfo.numOfTags, 0);
  ASSERT_EQ(tableMeta->tableInfo.precision, 1);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);

  tableMeta = NULL;
H
Haojun Liao 已提交
734
  code = catalogGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta);
D
dapan1121 已提交
735 736 737 738 739 740 741 742 743 744
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 8);
  ASSERT_EQ(tableMeta->tableType, TSDB_NORMAL_TABLE);
  ASSERT_EQ(tableMeta->sversion, ctgTestSVersion);
  ASSERT_EQ(tableMeta->tversion, ctgTestTVersion);
  ASSERT_EQ(tableMeta->tableInfo.numOfColumns, ctgTestColNum);
  ASSERT_EQ(tableMeta->tableInfo.numOfTags, 0);
  ASSERT_EQ(tableMeta->tableInfo.precision, 1);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);

D
dapan1121 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
  SDbVgVersion *dbs = NULL;
  SSTableMetaVersion *stb = NULL;
  uint32_t dbNum = 0, stbNum = 0, allDbNum = 0, allStbNum = 0;
  int32_t i = 0;
  while (i < 5) {
    ++i;
    code = catalogGetExpiredDBs(pCtg, &dbs, &dbNum);
    ASSERT_EQ(code, 0);
    code = catalogGetExpiredSTables(pCtg, &stb, &stbNum);
    ASSERT_EQ(code, 0);
    
    if (dbNum) {
      printf("got expired db,dbId:%"PRId64"\n", dbs->dbId);
      free(dbs);
      dbs = NULL;
    } else {
      printf("no expired db\n");
    }

    if (stbNum) {
      printf("got expired stb,suid:%"PRId64"\n", stb->suid);
      free(stb);
      stb = NULL;
    } else {
      printf("no expired stb\n");
    }

    allDbNum += dbNum;
    allStbNum += stbNum;
    sleep(2);
  }
  
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 0);

D
dapan1121 已提交
780 781 782
  catalogDestroy();
}

D
dapan 已提交
783
TEST(tableMeta, childTableCase) {
D
dapan1121 已提交
784 785 786 787 788 789 790 791 792
  struct SCatalog* pCtg = NULL;
  void *mockPointer = (void *)0x1;
  SVgroupInfo vgInfo = {0};

  ctgTestSetPrepareDbVgroupsAndChildMeta();

  initQueryModuleMsgHandle();

  //sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
D
dapan1121 已提交
793 794
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
795
  
D
dapan1121 已提交
796
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan1121 已提交
797 798
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
799
  SName n = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
H
Haojun Liao 已提交
800 801
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestCTablename);
D
dapan1121 已提交
802 803

  STableMeta *tableMeta = NULL;
H
Haojun Liao 已提交
804
  code = catalogGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta);
D
dapan1121 已提交
805 806 807 808 809 810 811 812 813 814 815
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 9);
  ASSERT_EQ(tableMeta->tableType, TSDB_CHILD_TABLE);
  ASSERT_EQ(tableMeta->sversion, ctgTestSVersion);
  ASSERT_EQ(tableMeta->tversion, ctgTestTVersion);
  ASSERT_EQ(tableMeta->tableInfo.numOfColumns, ctgTestColNum);
  ASSERT_EQ(tableMeta->tableInfo.numOfTags, ctgTestTagNum);
  ASSERT_EQ(tableMeta->tableInfo.precision, 1);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);

  tableMeta = NULL;
H
Haojun Liao 已提交
816
  code = catalogGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta);
D
dapan1121 已提交
817 818 819 820 821 822 823 824 825 826 827
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 9);
  ASSERT_EQ(tableMeta->tableType, TSDB_CHILD_TABLE);
  ASSERT_EQ(tableMeta->sversion, ctgTestSVersion);
  ASSERT_EQ(tableMeta->tversion, ctgTestTVersion);
  ASSERT_EQ(tableMeta->tableInfo.numOfColumns, ctgTestColNum);
  ASSERT_EQ(tableMeta->tableInfo.numOfTags, ctgTestTagNum);
  ASSERT_EQ(tableMeta->tableInfo.precision, 1);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);

  tableMeta = NULL;
H
Haojun Liao 已提交
828 829 830

  strcpy(n.tname, ctgTestSTablename);
  code = catalogGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta);
D
dapan1121 已提交
831 832 833 834 835 836 837 838 839 840
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 0);
  ASSERT_EQ(tableMeta->tableType, TSDB_SUPER_TABLE);
  ASSERT_EQ(tableMeta->sversion, ctgTestSVersion);
  ASSERT_EQ(tableMeta->tversion, ctgTestTVersion);
  ASSERT_EQ(tableMeta->tableInfo.numOfColumns, ctgTestColNum);
  ASSERT_EQ(tableMeta->tableInfo.numOfTags, ctgTestTagNum);
  ASSERT_EQ(tableMeta->tableInfo.precision, 1);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);

D
dapan1121 已提交
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
  SDbVgVersion *dbs = NULL;
  SSTableMetaVersion *stb = NULL;
  uint32_t dbNum = 0, stbNum = 0, allDbNum = 0, allStbNum = 0;
  int32_t i = 0;
  while (i < 5) {
    ++i;
    code = catalogGetExpiredDBs(pCtg, &dbs, &dbNum);
    ASSERT_EQ(code, 0);
    code = catalogGetExpiredSTables(pCtg, &stb, &stbNum);
    ASSERT_EQ(code, 0);
    
    if (dbNum) {
      printf("got expired db,dbId:%"PRId64"\n", dbs->dbId);
      free(dbs);
      dbs = NULL;
    } else {
      printf("no expired db\n");
    }

    if (stbNum) {
      printf("got expired stb,suid:%"PRId64"\n", stb->suid);
      free(stb);
      stb = NULL;
    } else {
      printf("no expired stb\n");
    }

    allDbNum += dbNum;
    allStbNum += stbNum;
    sleep(2);
  }
  
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 1);


D
dapan1121 已提交
877 878 879
  catalogDestroy();
}

D
dapan 已提交
880
TEST(tableMeta, superTableCase) {
D
dapan1121 已提交
881 882 883 884 885 886 887 888
  struct SCatalog* pCtg = NULL;
  void *mockPointer = (void *)0x1;
  SVgroupInfo vgInfo = {0};

  ctgTestSetPrepareDbVgroupsAndSuperMeta();

  initQueryModuleMsgHandle();

D
dapan1121 已提交
889 890 891
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
892
  //sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
D
dapan1121 已提交
893
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan1121 已提交
894 895
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
896
  SName n = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
H
Haojun Liao 已提交
897 898
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestSTablename);
D
dapan1121 已提交
899 900

  STableMeta *tableMeta = NULL;
H
Haojun Liao 已提交
901
  code = catalogGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta);
D
dapan1121 已提交
902 903 904 905 906
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 0);
  ASSERT_EQ(tableMeta->tableType, TSDB_SUPER_TABLE);
  ASSERT_EQ(tableMeta->sversion, ctgTestSVersion);
  ASSERT_EQ(tableMeta->tversion, ctgTestTVersion);
D
dapan1121 已提交
907 908
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
909 910 911 912 913 914 915 916
  ASSERT_EQ(tableMeta->tableInfo.numOfColumns, ctgTestColNum);
  ASSERT_EQ(tableMeta->tableInfo.numOfTags, ctgTestTagNum);
  ASSERT_EQ(tableMeta->tableInfo.precision, 1);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);

  ctgTestSetPrepareCTableMeta();

  tableMeta = NULL;
H
Haojun Liao 已提交
917 918 919 920

  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestCTablename);
  code = catalogGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta);
D
dapan1121 已提交
921 922 923 924 925 926 927 928 929 930 931
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 9);
  ASSERT_EQ(tableMeta->tableType, TSDB_CHILD_TABLE);
  ASSERT_EQ(tableMeta->sversion, ctgTestSVersion);
  ASSERT_EQ(tableMeta->tversion, ctgTestTVersion);
  ASSERT_EQ(tableMeta->tableInfo.numOfColumns, ctgTestColNum);
  ASSERT_EQ(tableMeta->tableInfo.numOfTags, ctgTestTagNum);
  ASSERT_EQ(tableMeta->tableInfo.precision, 1);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);

  tableMeta = NULL;
D
dapan1121 已提交
932
  code = catalogRenewAndGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
933 934 935 936 937 938 939 940 941 942
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 9);
  ASSERT_EQ(tableMeta->tableType, TSDB_CHILD_TABLE);
  ASSERT_EQ(tableMeta->sversion, ctgTestSVersion);
  ASSERT_EQ(tableMeta->tversion, ctgTestTVersion);
  ASSERT_EQ(tableMeta->tableInfo.numOfColumns, ctgTestColNum);
  ASSERT_EQ(tableMeta->tableInfo.numOfTags, ctgTestTagNum);
  ASSERT_EQ(tableMeta->tableInfo.precision, 1);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);

D
dapan1121 已提交
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
  SDbVgVersion *dbs = NULL;
  SSTableMetaVersion *stb = NULL;
  uint32_t dbNum = 0, stbNum = 0, allDbNum = 0, allStbNum = 0;
  int32_t i = 0;
  while (i < 5) {
    ++i;
    code = catalogGetExpiredDBs(pCtg, &dbs, &dbNum);
    ASSERT_EQ(code, 0);
    code = catalogGetExpiredSTables(pCtg, &stb, &stbNum);
    ASSERT_EQ(code, 0);
    
    if (dbNum) {
      printf("got expired db,dbId:%"PRId64"\n", dbs->dbId);
      free(dbs);
      dbs = NULL;
    } else {
      printf("no expired db\n");
    }

    if (stbNum) {
      printf("got expired stb,suid:%"PRId64"\n", stb->suid);
      free(stb);
      stb = NULL;
    } else {
      printf("no expired stb\n");
    }

    allDbNum += dbNum;
    allStbNum += stbNum;
    sleep(2);
  }
  
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 1);
D
dapan1121 已提交
977 978 979


  catalogDestroy();
D
dapan1121 已提交
980
}
981

D
dapan 已提交
982 983 984 985 986 987 988 989 990 991
TEST(tableDistVgroup, normalTable) {
  struct SCatalog* pCtg = NULL;
  void *mockPointer = (void *)0x1;
  SVgroupInfo *vgInfo = NULL;
  SArray *vgList = NULL;

  ctgTestSetPrepareDbVgroupsAndNormalMeta();

  initQueryModuleMsgHandle();

D
dapan1121 已提交
992 993 994
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

D
dapan 已提交
995 996
  //sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
  
D
dapan1121 已提交
997
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan 已提交
998 999
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
1000
  SName n = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
H
Haojun Liao 已提交
1001 1002
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);
D
dapan 已提交
1003

H
Haojun Liao 已提交
1004
  code = catalogGetTableDistVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &vgList);
D
dapan 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), 1);
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 0);
  ASSERT_EQ(vgInfo->vgId, 8);
  ASSERT_EQ(vgInfo->numOfEps, 3);

  catalogDestroy();
}

TEST(tableDistVgroup, childTableCase) {
  struct SCatalog* pCtg = NULL;
  void *mockPointer = (void *)0x1;
  SVgroupInfo *vgInfo = NULL;
  SArray *vgList = NULL;

  ctgTestSetPrepareDbVgroupsAndChildMeta();

  initQueryModuleMsgHandle();

  //sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
  
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

  code = catalogGetHandle(ctgTestClusterId, &pCtg);
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
1032
  SName n = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
H
Haojun Liao 已提交
1033 1034 1035 1036
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestCTablename);

  code = catalogGetTableDistVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &vgList);
D
dapan 已提交
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), 1);
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 0);
  ASSERT_EQ(vgInfo->vgId, 9);
  ASSERT_EQ(vgInfo->numOfEps, 4);


  catalogDestroy();
}

TEST(tableDistVgroup, superTableCase) {
  struct SCatalog* pCtg = NULL;
  void *mockPointer = (void *)0x1;
  SVgroupInfo *vgInfo = NULL;
  SArray *vgList = NULL;

  ctgTestSetPrepareDbVgroupsAndSuperMeta();

  initQueryModuleMsgHandle();

D
dapan1121 已提交
1057 1058 1059
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

D
dapan 已提交
1060
  //sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
D
dapan1121 已提交
1061
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan 已提交
1062 1063
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
1064
  SName n = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
H
Haojun Liao 已提交
1065 1066
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestSTablename);
D
dapan 已提交
1067

H
Haojun Liao 已提交
1068
  code = catalogGetTableDistVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &vgList);
D
dapan 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), 10);
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 0);
  ASSERT_EQ(vgInfo->vgId, 1);
  ASSERT_EQ(vgInfo->numOfEps, 1);
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 1);
  ASSERT_EQ(vgInfo->vgId, 2);
  ASSERT_EQ(vgInfo->numOfEps, 2);
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 2);
  ASSERT_EQ(vgInfo->vgId, 3);
  ASSERT_EQ(vgInfo->numOfEps, 3);


  catalogDestroy();
}

D
dapan1121 已提交
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
TEST(dbVgroup, getSetDbVgroupCase) {
  struct SCatalog* pCtg = NULL;
  void *mockPointer = (void *)0x1;
  SVgroupInfo vgInfo = {0};
  SVgroupInfo *pvgInfo = NULL;
  SDBVgroupInfo dbVgroup = {0};
  SArray *vgList = NULL;

  ctgTestSetPrepareDbVgroupsAndNormalMeta();

  initQueryModuleMsgHandle();

  //sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
  
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

  code = catalogGetHandle(ctgTestClusterId, &pCtg);
  ASSERT_EQ(code, 0);

  SName n = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

  code = catalogGetDBVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, ctgTestDbname, false, &vgList);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), ctgTestVgNum);
  
  code = catalogGetTableHashVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &vgInfo);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
  ASSERT_EQ(vgInfo.numOfEps, 3);

  code = catalogGetTableDistVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &vgList);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), 1);
  pvgInfo = (SVgroupInfo *)taosArrayGet(vgList, 0);
  ASSERT_EQ(pvgInfo->vgId, 8);
  ASSERT_EQ(pvgInfo->numOfEps, 3);
  taosArrayDestroy(vgList);

  ctgTestBuildDBVgroup(&dbVgroup);
  code = catalogUpdateDBVgroup(pCtg, ctgTestDbname, &dbVgroup);
  ASSERT_EQ(code, 0);

  code = catalogGetTableHashVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &vgInfo);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 7);
  ASSERT_EQ(vgInfo.numOfEps, 2);

  code = catalogGetTableDistVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &vgList);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), 1);
  pvgInfo = (SVgroupInfo *)taosArrayGet(vgList, 0);
  ASSERT_EQ(pvgInfo->vgId, 8);
  ASSERT_EQ(pvgInfo->numOfEps, 3);
  taosArrayDestroy(vgList);
  
  catalogDestroy();
}

TEST(multiThread, getSetDbVgroupCase) {
  struct SCatalog* pCtg = NULL;
  void *mockPointer = (void *)0x1;
  SVgroupInfo vgInfo = {0};
  SVgroupInfo *pvgInfo = NULL;
  SDBVgroupInfo dbVgroup = {0};
  SArray *vgList = NULL;
D
dapan1121 已提交
1153
  ctgTestStop = false;
D
dapan1121 已提交
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185

  ctgTestInitLogFile();
  
  ctgTestSetPrepareDbVgroups();

  initQueryModuleMsgHandle();

  //sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
  
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

  code = catalogGetHandle(ctgTestClusterId, &pCtg);
  ASSERT_EQ(code, 0);

  SName n = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

  pthread_attr_t thattr;
  pthread_attr_init(&thattr);

  pthread_t thread1, thread2;
  pthread_create(&(thread1), &thattr, ctgTestSetDbVgroupThread, pCtg);

  sleep(1);
  pthread_create(&(thread1), &thattr, ctgTestGetDbVgroupThread, pCtg);

  while (true) {
    if (ctgTestDeadLoop) {
      sleep(1);
    } else {
D
dapan1121 已提交
1186
      sleep(ctgTestMTRunSec);
D
dapan1121 已提交
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
      break;
    }
  }
  
  ctgTestStop = true;
  sleep(1);
  
  catalogDestroy();
}

TEST(multiThread, ctableMeta) {
  struct SCatalog* pCtg = NULL;
  void *mockPointer = (void *)0x1;
  SVgroupInfo vgInfo = {0};
  SVgroupInfo *pvgInfo = NULL;
  SDBVgroupInfo dbVgroup = {0};
  SArray *vgList = NULL;
D
dapan1121 已提交
1204
  ctgTestStop = false;
D
dapan1121 已提交
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226

  ctgTestSetPrepareDbVgroupsAndChildMeta();

  initQueryModuleMsgHandle();

  //sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
  
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

  code = catalogGetHandle(ctgTestClusterId, &pCtg);
  ASSERT_EQ(code, 0);

  SName n = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

  pthread_attr_t thattr;
  pthread_attr_init(&thattr);

  pthread_t thread1, thread2;
  pthread_create(&(thread1), &thattr, ctgTestSetCtableMetaThread, pCtg);
D
dapan1121 已提交
1227 1228
  sleep(1);
  pthread_create(&(thread1), &thattr, ctgTestGetCtableMetaThread, pCtg);
D
dapan1121 已提交
1229 1230 1231 1232 1233

  while (true) {
    if (ctgTestDeadLoop) {
      sleep(1);
    } else {
D
dapan1121 已提交
1234
      sleep(ctgTestMTRunSec);
D
dapan1121 已提交
1235 1236 1237 1238 1239 1240 1241 1242 1243
      break;
    }
  }
  
  ctgTestStop = true;
  sleep(1);
  
  catalogDestroy();
}
D
dapan 已提交
1244

1245

D
dapan1121 已提交
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
TEST(rentTest, allRent) {
  struct SCatalog* pCtg = NULL;
  void *mockPointer = (void *)0x1;
  SVgroupInfo vgInfo = {0};
  SVgroupInfo *pvgInfo = NULL;
  SDBVgroupInfo dbVgroup = {0};
  SArray *vgList = NULL;
  ctgTestStop = false;
  SDbVgVersion *dbs = NULL;
  SSTableMetaVersion *stable = NULL;
  uint32_t num = 0;

  ctgTestSetPrepareDbVgroupsAndMultiSuperMeta();

  initQueryModuleMsgHandle();

  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

  code = catalogGetHandle(ctgTestClusterId, &pCtg);
  ASSERT_EQ(code, 0);


  SName n = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
  strcpy(n.dbname, "db1");

  for (int32_t i = 1; i <= 10; ++i) {
    sprintf(n.tname, "%s_%d", ctgTestSTablename, i);

    STableMeta *tableMeta = NULL;
    code = catalogGetSTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta);
    ASSERT_EQ(code, 0);
    ASSERT_EQ(tableMeta->vgId, 0);
    ASSERT_EQ(tableMeta->tableType, TSDB_SUPER_TABLE);
    ASSERT_EQ(tableMeta->sversion, ctgTestSVersion);
    ASSERT_EQ(tableMeta->tversion, ctgTestTVersion);
    ASSERT_EQ(tableMeta->uid, ctgTestSuid + i);
    ASSERT_EQ(tableMeta->suid, ctgTestSuid + i);
    ASSERT_EQ(tableMeta->tableInfo.numOfColumns, ctgTestColNum);
    ASSERT_EQ(tableMeta->tableInfo.numOfTags, ctgTestTagNum);
    ASSERT_EQ(tableMeta->tableInfo.precision, 1);
    ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);
    
    code = catalogGetExpiredDBs(pCtg, &dbs, &num);
    ASSERT_EQ(code, 0);
    printf("%d - expired dbNum:%d\n", i, num);
    if (dbs) {
      printf("%d - expired dbId:%"PRId64", vgVersion:%d\n", i, dbs->dbId, dbs->vgVersion);
      free(dbs);
      dbs = NULL;
    }
    
    code = catalogGetExpiredSTables(pCtg, &stable, &num);
    ASSERT_EQ(code, 0);
    printf("%d - expired stableNum:%d\n", i, num);
    if (stable) {
      for (int32_t n = 0; n < num; ++n) {
        printf("suid:%"PRId64", sversion:%d, tversion:%d\n", stable[n].suid, stable[n].sversion, stable[n].tversion);
      }
      free(stable);
      stable = NULL;
    }
    printf("*************************************************\n");
    
    sleep(2);
  }
  
  catalogDestroy();
}



1318 1319 1320 1321
int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
D
dapan1121 已提交
1322 1323 1324