catalogTests.cpp 43.8 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
#include <tglobal.h>
#include <iostream>
S
Shengliang Guan 已提交
19
#include "os.h"
20

S
Shengliang Guan 已提交
21 22
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
23 24 25
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"
S
Shengliang Guan 已提交
26
#pragma GCC diagnostic ignored "-Wformat"
27

S
Shengliang Guan 已提交
28 29 30
#include "addr_any.h"
#include "catalog.h"
#include "stub.h"
D
dapan1121 已提交
31
#include "taos.h"
32
#include "tdef.h"
D
dapan1121 已提交
33
#include "tep.h"
D
ut test  
dapan1121 已提交
34
#include "trpc.h"
S
Shengliang Guan 已提交
35
#include "tvariant.h"
36

D
dapan1121 已提交
37
namespace {
38

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

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

S
Shengliang Guan 已提交
49 50 51
bool    ctgTestStop = false;
bool    ctgTestEnableSleep = false;
bool    ctgTestDeadLoop = false;
D
dapan1121 已提交
52
int32_t ctgTestPrintNum = 200000;
D
dapan1121 已提交
53
int32_t ctgTestMTRunSec = 5;
D
dapan1121 已提交
54

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

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

D
ut test  
dapan1121 已提交
71
void sendCreateDbMsg(void *shandle, SEpSet *pEpSet) {
S
Shengliang Guan 已提交
72
  SCreateDbReq *pReq = (SCreateDbReq *)rpcMallocCont(sizeof(SCreateDbReq));
D
ut test  
dapan1121 已提交
73 74 75 76 77 78 79 80
  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 已提交
81 82
  pReq->minRows = htonl(100);
  pReq->maxRows = htonl(4096);
D
ut test  
dapan1121 已提交
83 84 85 86 87 88 89 90 91 92
  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;
S
Shengliang Guan 已提交
93

D
ut test  
dapan1121 已提交
94 95
  SRpcMsg rpcMsg = {0};
  rpcMsg.pCont = pReq;
S
Shengliang Guan 已提交
96
  rpcMsg.contLen = sizeof(SCreateDbReq);
H
Hongze Cheng 已提交
97
  rpcMsg.msgType = TDMT_MND_CREATE_DB;
D
ut test  
dapan1121 已提交
98 99 100 101 102 103 104 105

  SRpcMsg rpcRsp = {0};

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

  ASSERT_EQ(rpcRsp.code, 0);
}

D
dapan1121 已提交
106
void ctgTestInitLogFile() {
S
Shengliang Guan 已提交
107 108
  const char   *defaultLogFileNamePrefix = "taoslog";
  const int32_t maxLogFileNum = 10;
D
dapan1121 已提交
109 110

  tsAsyncLog = 0;
D
dapan1121 已提交
111
  qDebugFlag = 159;
D
dapan1121 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

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

D
dapan 已提交
133 134 135 136
  char db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(&cn, db);

  strcpy(output->dbFName, db);
D
dapan1121 已提交
137
  SET_META_TYPE_BOTH_TABLE(output->metaType);
D
dapan1121 已提交
138

D
dapan1121 已提交
139 140
  strcpy(output->ctbName, cn.tname);
  strcpy(output->tbName, sn.tname);
D
dapan1121 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154

  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;
S
Shengliang Guan 已提交
155

D
dapan1121 已提交
156 157
  output->tbMeta->sversion = ctgTestSVersion;
  output->tbMeta->tversion = ctgTestTVersion;
S
Shengliang Guan 已提交
158

D
dapan1121 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  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");
}

D
dapan 已提交
179
void ctgTestBuildDBVgroup(SDBVgroupInfo **pdbVgroup) {
D
dapan1121 已提交
180
  static int32_t vgVersion = ctgTestVgVersion + 1;
S
Shengliang Guan 已提交
181 182
  int32_t        vgNum = 0;
  SVgroupInfo    vgInfo = {0};
D
dapan 已提交
183
  SDBVgroupInfo *dbVgroup = (SDBVgroupInfo *)calloc(1, sizeof(SDBVgroupInfo));
S
Shengliang Guan 已提交
184

D
dapan1121 已提交
185
  dbVgroup->vgVersion = vgVersion++;
S
Shengliang Guan 已提交
186

D
dapan1121 已提交
187
  ctgTestCurrentVgVersion = dbVgroup->vgVersion;
S
Shengliang Guan 已提交
188

D
dapan1121 已提交
189
  dbVgroup->hashMethod = 0;
D
dapan1121 已提交
190
  dbVgroup->vgHash = taosHashInit(ctgTestVgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
D
dapan1121 已提交
191 192 193 194 195 196 197 198

  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;
H
Haojun Liao 已提交
199 200 201 202
    vgInfo.epset.numOfEps = i % TSDB_MAX_REPLICA + 1;
    vgInfo.epset.inUse = i % vgInfo.epset.numOfEps;
    for (int32_t n = 0; n < vgInfo.epset.numOfEps; ++n) {
      SEp *addr = &vgInfo.epset.eps[n];
D
dapan1121 已提交
203 204 205 206
      strcpy(addr->fqdn, "a0");
      addr->port = htons(n + 22);
    }

D
dapan1121 已提交
207
    taosHashPut(dbVgroup->vgHash, &vgInfo.vgId, sizeof(vgInfo.vgId), &vgInfo, sizeof(vgInfo));
D
dapan1121 已提交
208
  }
D
dapan 已提交
209 210

  *pdbVgroup = dbVgroup;
D
dapan1121 已提交
211 212
}

D
dapan1121 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

void ctgTestBuildSTableMetaRsp(STableMetaRsp *rspMsg) {
  strcpy(rspMsg->dbFName, ctgTestDbname);
  sprintf(rspMsg->tbName, "%s", ctgTestSTablename);
  sprintf(rspMsg->stbName, "%s", ctgTestSTablename);
  rspMsg->numOfTags = ctgTestTagNum;
  rspMsg->numOfColumns = ctgTestColNum;
  rspMsg->precision = 1 + 1;
  rspMsg->tableType = TSDB_SUPER_TABLE;
  rspMsg->update = 1 + 1;
  rspMsg->sversion = ctgTestSVersion + 1;
  rspMsg->tversion = ctgTestTVersion + 1;
  rspMsg->suid = ctgTestSuid + 1;
  rspMsg->tuid = ctgTestSuid + 1;
  rspMsg->vgId = 1;

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

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

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

  return;
}


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

S
Shengliang Guan 已提交
255
  pRsp->code = 0;
D
dapan1121 已提交
256 257 258 259
  pRsp->contLen = sizeof(SUseDbRsp) + ctgTestVgNum * sizeof(SVgroupInfo);
  pRsp->pCont = calloc(1, pRsp->contLen);
  rspMsg = (SUseDbRsp *)pRsp->pCont;
  strcpy(rspMsg->db, ctgTestDbname);
D
dapan1121 已提交
260 261
  rspMsg->vgVersion = htonl(ctgTestVgVersion);
  ctgTestCurrentVgVersion = ctgTestVgVersion;
D
dapan1121 已提交
262
  rspMsg->vgNum = htonl(ctgTestVgNum);
D
dapan1121 已提交
263
  rspMsg->hashMethod = 0;
D
dapan1121 已提交
264
  rspMsg->uid = htobe64(ctgTestDbId);
D
dapan1121 已提交
265 266

  SVgroupInfo *vg = NULL;
S
Shengliang Guan 已提交
267
  uint32_t     hashUnit = UINT32_MAX / ctgTestVgNum;
D
dapan1121 已提交
268 269 270
  for (int32_t i = 0; i < ctgTestVgNum; ++i) {
    vg = &rspMsg->vgroupInfo[i];

D
dapan1121 已提交
271 272 273
    vg->vgId = htonl(i + 1);
    vg->hashBegin = htonl(i * hashUnit);
    vg->hashEnd = htonl(hashUnit * (i + 1) - 1);
H
Haojun Liao 已提交
274 275 276 277
    vg->epset.numOfEps = i % TSDB_MAX_REPLICA + 1;
    vg->epset.inUse = i % vg->epset.numOfEps;
    for (int32_t n = 0; n < vg->epset.numOfEps; ++n) {
      SEp *addr = &vg->epset.eps[n];
D
dapan1121 已提交
278
      strcpy(addr->fqdn, "a0");
D
dapan1121 已提交
279
      addr->port = htons(n + 22);
D
dapan1121 已提交
280 281 282
    }
  }

D
dapan1121 已提交
283 284 285 286 287 288
  vg->hashEnd = htonl(UINT32_MAX);

  return;
}

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

S
Shengliang Guan 已提交
291
  pRsp->code = 0;
S
Shengliang Guan 已提交
292
  pRsp->contLen = sizeof(STableMetaRsp) + (ctgTestColNum + ctgTestTagNum) * sizeof(SSchema);
D
dapan1121 已提交
293
  pRsp->pCont = calloc(1, pRsp->contLen);
S
Shengliang Guan 已提交
294
  rspMsg = (STableMetaRsp *)pRsp->pCont;
D
dapan1121 已提交
295 296
  strcpy(rspMsg->dbFName, ctgTestDbname);
  strcpy(rspMsg->tbName, ctgTestTablename);
D
dapan1121 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310
  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 已提交
311
  s->colId = htonl(1);
D
dapan1121 已提交
312 313 314 315 316
  s->bytes = htonl(8);
  strcpy(s->name, "ts");

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

D
dapan1121 已提交
321 322 323 324
  return;
}

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

S
Shengliang Guan 已提交
327
  pRsp->code = 0;
S
Shengliang Guan 已提交
328
  pRsp->contLen = sizeof(STableMetaRsp) + (ctgTestColNum + ctgTestTagNum) * sizeof(SSchema);
D
dapan1121 已提交
329
  pRsp->pCont = calloc(1, pRsp->contLen);
S
Shengliang Guan 已提交
330
  rspMsg = (STableMetaRsp *)pRsp->pCont;
D
dapan1121 已提交
331 332 333
  strcpy(rspMsg->dbFName, ctgTestDbname);
  strcpy(rspMsg->tbName, ctgTestCTablename);
  strcpy(rspMsg->stbName, ctgTestSTablename);
D
dapan1121 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347
  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 已提交
348
  s->colId = htonl(1);
D
dapan1121 已提交
349 350 351 352 353
  s->bytes = htonl(8);
  strcpy(s->name, "ts");

  s = &rspMsg->pSchema[1];
  s->type = TSDB_DATA_TYPE_INT;
D
dapan1121 已提交
354
  s->colId = htonl(2);
D
dapan1121 已提交
355 356 357 358 359
  s->bytes = htonl(4);
  strcpy(s->name, "col1s");

  s = &rspMsg->pSchema[2];
  s->type = TSDB_DATA_TYPE_BINARY;
D
dapan1121 已提交
360
  s->colId = htonl(3);
D
dapan1121 已提交
361 362 363 364 365 366 367
  s->bytes = htonl(12);
  strcpy(s->name, "tag1s");

  return;
}

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

S
Shengliang Guan 已提交
370
  pRsp->code = 0;
S
Shengliang Guan 已提交
371
  pRsp->contLen = sizeof(STableMetaRsp) + (ctgTestColNum + ctgTestTagNum) * sizeof(SSchema);
D
dapan1121 已提交
372
  pRsp->pCont = calloc(1, pRsp->contLen);
S
Shengliang Guan 已提交
373
  rspMsg = (STableMetaRsp *)pRsp->pCont;
D
dapan1121 已提交
374 375 376
  strcpy(rspMsg->dbFName, ctgTestDbname);
  strcpy(rspMsg->tbName, ctgTestSTablename);  
  strcpy(rspMsg->stbName, ctgTestSTablename);  
D
dapan1121 已提交
377 378 379 380 381 382 383
  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 已提交
384 385
  rspMsg->suid = htobe64(ctgTestSuid);
  rspMsg->tuid = htobe64(ctgTestSuid);
D
dapan1121 已提交
386 387 388 389 390
  rspMsg->vgId = 0;

  SSchema *s = NULL;
  s = &rspMsg->pSchema[0];
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
D
dapan1121 已提交
391
  s->colId = htonl(1);
D
dapan1121 已提交
392 393 394 395 396
  s->bytes = htonl(8);
  strcpy(s->name, "ts");

  s = &rspMsg->pSchema[1];
  s->type = TSDB_DATA_TYPE_INT;
D
dapan1121 已提交
397
  s->colId = htonl(2);
D
dapan1121 已提交
398 399 400 401 402
  s->bytes = htonl(4);
  strcpy(s->name, "col1s");

  s = &rspMsg->pSchema[2];
  s->type = TSDB_DATA_TYPE_BINARY;
D
dapan1121 已提交
403
  s->colId = htonl(3);
D
dapan1121 已提交
404 405 406 407 408 409
  s->bytes = htonl(12);
  strcpy(s->name, "tag1s");

  return;
}

D
dapan1121 已提交
410
void ctgTestPrepareMultiSTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
411
  STableMetaRsp *rspMsg = NULL;  // todo
D
dapan1121 已提交
412 413
  static int32_t idx = 1;

S
Shengliang Guan 已提交
414
  pRsp->code = 0;
S
Shengliang Guan 已提交
415
  pRsp->contLen = sizeof(STableMetaRsp) + (ctgTestColNum + ctgTestTagNum) * sizeof(SSchema);
D
dapan1121 已提交
416
  pRsp->pCont = calloc(1, pRsp->contLen);
S
Shengliang Guan 已提交
417
  rspMsg = (STableMetaRsp *)pRsp->pCont;
D
dapan1121 已提交
418
  strcpy(rspMsg->dbFName, ctgTestDbname);
D
dapan 已提交
419
  sprintf(rspMsg->tbName, "%s_%d", ctgTestSTablename, idx);
D
dapan1121 已提交
420
  sprintf(rspMsg->stbName, "%s_%d", ctgTestSTablename, idx);
D
dapan1121 已提交
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 446 447 448 449 450 451
  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;
S
Shengliang Guan 已提交
452

D
dapan1121 已提交
453 454 455
  return;
}

D
dapan 已提交
456 457
void ctgTestPrepareDbVgroupsAndNormalMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestPrepareDbVgroups(shandle, pEpSet, pMsg, pRsp);
S
Shengliang Guan 已提交
458

D
dapan 已提交
459
  ctgTestSetPrepareTableMeta();
S
Shengliang Guan 已提交
460

D
dapan 已提交
461 462 463 464 465
  return;
}

void ctgTestPrepareDbVgroupsAndChildMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestPrepareDbVgroups(shandle, pEpSet, pMsg, pRsp);
S
Shengliang Guan 已提交
466

D
dapan 已提交
467
  ctgTestSetPrepareCTableMeta();
S
Shengliang Guan 已提交
468

D
dapan 已提交
469 470 471 472 473
  return;
}

void ctgTestPrepareDbVgroupsAndSuperMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestPrepareDbVgroups(shandle, pEpSet, pMsg, pRsp);
S
Shengliang Guan 已提交
474

D
dapan 已提交
475
  ctgTestSetPrepareSTableMeta();
S
Shengliang Guan 已提交
476

D
dapan 已提交
477 478 479
  return;
}

D
dapan1121 已提交
480 481
void ctgTestPrepareDbVgroupsAndMultiSuperMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestPrepareDbVgroups(shandle, pEpSet, pMsg, pRsp);
S
Shengliang Guan 已提交
482

D
dapan1121 已提交
483
  ctgTestSetPrepareMultiSTableMeta();
S
Shengliang Guan 已提交
484

D
dapan1121 已提交
485 486 487
  return;
}

D
dapan1121 已提交
488 489 490 491
void ctgTestSetPrepareDbVgroups() {
  static Stub stub;
  stub.set(rpcSendRecv, ctgTestPrepareDbVgroups);
  {
S
Shengliang Guan 已提交
492 493
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
494
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
S
Shengliang Guan 已提交
495
    for (const auto &f : result) {
D
dapan1121 已提交
496 497 498 499 500 501 502 503 504
      stub.set(f.second, ctgTestPrepareDbVgroups);
    }
  }
}

void ctgTestSetPrepareTableMeta() {
  static Stub stub;
  stub.set(rpcSendRecv, ctgTestPrepareTableMeta);
  {
S
Shengliang Guan 已提交
505 506
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
507
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
S
Shengliang Guan 已提交
508
    for (const auto &f : result) {
D
dapan1121 已提交
509 510 511 512 513 514 515 516 517
      stub.set(f.second, ctgTestPrepareTableMeta);
    }
  }
}

void ctgTestSetPrepareCTableMeta() {
  static Stub stub;
  stub.set(rpcSendRecv, ctgTestPrepareCTableMeta);
  {
S
Shengliang Guan 已提交
518 519
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
520
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
S
Shengliang Guan 已提交
521
    for (const auto &f : result) {
D
dapan1121 已提交
522 523 524 525 526 527 528 529 530
      stub.set(f.second, ctgTestPrepareCTableMeta);
    }
  }
}

void ctgTestSetPrepareSTableMeta() {
  static Stub stub;
  stub.set(rpcSendRecv, ctgTestPrepareSTableMeta);
  {
S
Shengliang Guan 已提交
531 532
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
533
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
S
Shengliang Guan 已提交
534
    for (const auto &f : result) {
D
dapan1121 已提交
535 536 537 538 539
      stub.set(f.second, ctgTestPrepareSTableMeta);
    }
  }
}

D
dapan1121 已提交
540 541 542 543
void ctgTestSetPrepareMultiSTableMeta() {
  static Stub stub;
  stub.set(rpcSendRecv, ctgTestPrepareMultiSTableMeta);
  {
S
Shengliang Guan 已提交
544 545
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
546
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
S
Shengliang Guan 已提交
547
    for (const auto &f : result) {
D
dapan1121 已提交
548 549 550 551 552
      stub.set(f.second, ctgTestPrepareMultiSTableMeta);
    }
  }
}

D
dapan 已提交
553 554 555 556
void ctgTestSetPrepareDbVgroupsAndNormalMeta() {
  static Stub stub;
  stub.set(rpcSendRecv, ctgTestPrepareDbVgroupsAndNormalMeta);
  {
S
Shengliang Guan 已提交
557 558
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan 已提交
559
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
S
Shengliang Guan 已提交
560
    for (const auto &f : result) {
D
dapan 已提交
561 562 563 564 565
      stub.set(f.second, ctgTestPrepareDbVgroupsAndNormalMeta);
    }
  }
}

D
dapan1121 已提交
566
void ctgTestSetPrepareDbVgroupsAndChildMeta() {
D
dapan1121 已提交
567
  static Stub stub;
D
dapan1121 已提交
568
  stub.set(rpcSendRecv, ctgTestPrepareDbVgroupsAndChildMeta);
D
dapan1121 已提交
569
  {
S
Shengliang Guan 已提交
570 571
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
572
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
S
Shengliang Guan 已提交
573
    for (const auto &f : result) {
D
dapan 已提交
574
      stub.set(f.second, ctgTestPrepareDbVgroupsAndChildMeta);
D
dapan1121 已提交
575 576 577 578 579 580 581 582
    }
  }
}

void ctgTestSetPrepareDbVgroupsAndSuperMeta() {
  static Stub stub;
  stub.set(rpcSendRecv, ctgTestPrepareDbVgroupsAndSuperMeta);
  {
S
Shengliang Guan 已提交
583 584
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
585
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
S
Shengliang Guan 已提交
586
    for (const auto &f : result) {
D
dapan 已提交
587
      stub.set(f.second, ctgTestPrepareDbVgroupsAndSuperMeta);
D
dapan1121 已提交
588 589 590 591
    }
  }
}

D
dapan1121 已提交
592 593 594 595
void ctgTestSetPrepareDbVgroupsAndMultiSuperMeta() {
  static Stub stub;
  stub.set(rpcSendRecv, ctgTestPrepareDbVgroupsAndMultiSuperMeta);
  {
S
Shengliang Guan 已提交
596 597
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
598
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
S
Shengliang Guan 已提交
599
    for (const auto &f : result) {
D
dapan1121 已提交
600 601 602 603 604
      stub.set(f.second, ctgTestPrepareDbVgroupsAndMultiSuperMeta);
    }
  }
}

S
Shengliang Guan 已提交
605
}  // namespace
606

D
dapan1121 已提交
607
void *ctgTestGetDbVgroupThread(void *param) {
S
Shengliang Guan 已提交
608 609 610 611 612 613
  struct SCatalog *pCtg = (struct SCatalog *)param;
  int32_t          code = 0;
  void            *mockPointer = (void *)0x1;
  SArray          *vgList = NULL;
  int32_t          n = 0;

D
dapan1121 已提交
614 615 616 617 618 619 620 621 622 623 624
  while (!ctgTestStop) {
    code = catalogGetDBVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, ctgTestDbname, false, &vgList);
    if (code) {
      assert(0);
    }

    if (vgList) {
      taosArrayDestroy(vgList);
    }

    if (ctgTestEnableSleep) {
S
Shengliang Guan 已提交
625
      usleep(rand() % 5);
D
dapan1121 已提交
626
    }
D
dapan1121 已提交
627
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
628 629 630 631 632 633 634
      printf("Get:%d\n", n);
    }
  }

  return NULL;
}

D
dapan1121 已提交
635
void *ctgTestSetSameDbVgroupThread(void *param) {
S
Shengliang Guan 已提交
636 637
  struct SCatalog *pCtg = (struct SCatalog *)param;
  int32_t          code = 0;
D
dapan 已提交
638
  SDBVgroupInfo    *dbVgroup = NULL;
S
Shengliang Guan 已提交
639 640
  int32_t          n = 0;

D
dapan1121 已提交
641 642
  while (!ctgTestStop) {
    ctgTestBuildDBVgroup(&dbVgroup);
D
dapan1121 已提交
643
    code = catalogUpdateDBVgroup(pCtg, ctgTestDbname, ctgTestDbId, dbVgroup);
D
dapan1121 已提交
644 645 646 647
    if (code) {
      assert(0);
    }

S
Shengliang Guan 已提交
648 649
    if (ctgTestEnableSleep) {
      usleep(rand() % 5);
D
dapan1121 已提交
650
    }
D
dapan1121 已提交
651
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
652 653 654 655 656 657 658
      printf("Set:%d\n", n);
    }
  }

  return NULL;
}

D
dapan1121 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684

void *ctgTestSetDiffDbVgroupThread(void *param) {
  struct SCatalog *pCtg = (struct SCatalog *)param;
  int32_t          code = 0;
  SDBVgroupInfo    *dbVgroup = NULL;
  int32_t          n = 0;

  while (!ctgTestStop) {
    ctgTestBuildDBVgroup(&dbVgroup);
    code = catalogUpdateDBVgroup(pCtg, ctgTestDbname, ctgTestDbId++, dbVgroup);
    if (code) {
      assert(0);
    }

    if (ctgTestEnableSleep) {
      usleep(rand() % 5);
    }
    if (++n % ctgTestPrintNum == 0) {
      printf("Set:%d\n", n);
    }
  }

  return NULL;
}


D
dapan1121 已提交
685
void *ctgTestGetCtableMetaThread(void *param) {
S
Shengliang Guan 已提交
686 687 688 689 690
  struct SCatalog *pCtg = (struct SCatalog *)param;
  int32_t          code = 0;
  int32_t          n = 0;
  STableMeta      *tbMeta = NULL;
  int32_t          exist = 0;
D
dapan1121 已提交
691 692 693 694

  SName cn = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
  strcpy(cn.dbname, "db1");
  strcpy(cn.tname, ctgTestCTablename);
S
Shengliang Guan 已提交
695

D
dapan1121 已提交
696 697 698 699 700 701
  while (!ctgTestStop) {
    code = ctgGetTableMetaFromCache(pCtg, &cn, &tbMeta, &exist);
    if (code || 0 == exist) {
      assert(0);
    }

D
dapan1121 已提交
702 703
    tfree(tbMeta);

D
dapan1121 已提交
704
    if (ctgTestEnableSleep) {
S
Shengliang Guan 已提交
705
      usleep(rand() % 5);
D
dapan1121 已提交
706
    }
S
Shengliang Guan 已提交
707

D
dapan1121 已提交
708
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
709 710 711 712 713 714 715 716
      printf("Get:%d\n", n);
    }
  }

  return NULL;
}

void *ctgTestSetCtableMetaThread(void *param) {
S
Shengliang Guan 已提交
717 718 719 720
  struct SCatalog *pCtg = (struct SCatalog *)param;
  int32_t          code = 0;
  SDBVgroupInfo    dbVgroup = {0};
  int32_t          n = 0;
D
dapan1121 已提交
721 722 723
  STableMetaOutput output = {0};

  ctgTestBuildCTableMetaOutput(&output);
S
Shengliang Guan 已提交
724

D
dapan1121 已提交
725 726 727 728 729 730
  while (!ctgTestStop) {
    code = ctgUpdateTableMetaCache(pCtg, &output);
    if (code) {
      assert(0);
    }

S
Shengliang Guan 已提交
731 732
    if (ctgTestEnableSleep) {
      usleep(rand() % 5);
D
dapan1121 已提交
733
    }
D
dapan1121 已提交
734
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
735 736 737 738
      printf("Set:%d\n", n);
    }
  }

D
dapan1121 已提交
739 740
  tfree(output.tbMeta);

D
dapan1121 已提交
741 742 743
  return NULL;
}

D
dapan 已提交
744

D
dapan 已提交
745
TEST(tableMeta, normalTable) {
S
Shengliang Guan 已提交
746 747 748
  struct SCatalog *pCtg = NULL;
  void            *mockPointer = (void *)0x1;
  SVgroupInfo      vgInfo = {0};
749

D
dapan1121 已提交
750 751
  ctgTestInitLogFile();

D
dapan1121 已提交
752
  ctgTestSetPrepareDbVgroups();
D
dapan1121 已提交
753

754
  initQueryModuleMsgHandle();
D
ut test  
dapan1121 已提交
755

S
Shengliang Guan 已提交
756 757
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);

D
dapan1121 已提交
758 759
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);
760

D
dapan1121 已提交
761
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan1121 已提交
762
  ASSERT_EQ(code, 0);
763

D
dapan1121 已提交
764
  SName n = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
H
Haojun Liao 已提交
765 766 767 768
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

  code = catalogGetTableHashVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &vgInfo);
D
dapan1121 已提交
769
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
770
  ASSERT_EQ(vgInfo.vgId, 8);
H
Haojun Liao 已提交
771
  ASSERT_EQ(vgInfo.epset.numOfEps, 3);
D
dapan1121 已提交
772 773 774 775

  ctgTestSetPrepareTableMeta();

  STableMeta *tableMeta = NULL;
H
Haojun Liao 已提交
776
  code = catalogGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta);
D
dapan1121 已提交
777 778 779 780 781 782 783 784 785 786 787
  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 已提交
788
  code = catalogGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta);
D
dapan1121 已提交
789 790 791 792 793 794 795 796 797 798
  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);

S
Shengliang Guan 已提交
799
  SDbVgVersion       *dbs = NULL;
D
dapan1121 已提交
800
  SSTableMetaVersion *stb = NULL;
S
Shengliang Guan 已提交
801 802
  uint32_t            dbNum = 0, stbNum = 0, allDbNum = 0, allStbNum = 0;
  int32_t             i = 0;
D
dapan1121 已提交
803 804 805 806 807 808
  while (i < 5) {
    ++i;
    code = catalogGetExpiredDBs(pCtg, &dbs, &dbNum);
    ASSERT_EQ(code, 0);
    code = catalogGetExpiredSTables(pCtg, &stb, &stbNum);
    ASSERT_EQ(code, 0);
S
Shengliang Guan 已提交
809

D
dapan1121 已提交
810
    if (dbNum) {
S
Shengliang Guan 已提交
811
      printf("got expired db,dbId:%" PRId64 "\n", dbs->dbId);
D
dapan1121 已提交
812 813 814 815 816 817 818
      free(dbs);
      dbs = NULL;
    } else {
      printf("no expired db\n");
    }

    if (stbNum) {
D
dapan 已提交
819
      printf("got expired stb,suid:%" PRId64 ",dbFName:%s, stbName:%s\n", stb->suid, stb->dbFName, stb->stbName);
D
dapan1121 已提交
820 821 822 823 824 825 826 827 828 829
      free(stb);
      stb = NULL;
    } else {
      printf("no expired stb\n");
    }

    allDbNum += dbNum;
    allStbNum += stbNum;
    sleep(2);
  }
S
Shengliang Guan 已提交
830

D
dapan1121 已提交
831 832 833
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 0);

D
dapan1121 已提交
834 835 836
  catalogDestroy();
}

D
dapan 已提交
837
TEST(tableMeta, childTableCase) {
S
Shengliang Guan 已提交
838 839 840
  struct SCatalog *pCtg = NULL;
  void            *mockPointer = (void *)0x1;
  SVgroupInfo      vgInfo = {0};
D
dapan1121 已提交
841

D
dapan1121 已提交
842 843
  ctgTestInitLogFile();

D
dapan1121 已提交
844 845 846 847
  ctgTestSetPrepareDbVgroupsAndChildMeta();

  initQueryModuleMsgHandle();

S
Shengliang Guan 已提交
848
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
D
dapan1121 已提交
849 850
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);
S
Shengliang Guan 已提交
851

D
dapan1121 已提交
852
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan1121 已提交
853 854
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
855
  SName n = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
H
Haojun Liao 已提交
856 857
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestCTablename);
D
dapan1121 已提交
858 859

  STableMeta *tableMeta = NULL;
H
Haojun Liao 已提交
860
  code = catalogGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta);
D
dapan1121 已提交
861 862 863 864 865 866 867 868 869 870 871
  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 已提交
872
  code = catalogGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta);
D
dapan1121 已提交
873 874 875 876 877 878 879 880 881 882 883
  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 已提交
884 885 886

  strcpy(n.tname, ctgTestSTablename);
  code = catalogGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta);
D
dapan1121 已提交
887 888 889 890 891 892 893 894 895 896
  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);

S
Shengliang Guan 已提交
897
  SDbVgVersion       *dbs = NULL;
D
dapan1121 已提交
898
  SSTableMetaVersion *stb = NULL;
S
Shengliang Guan 已提交
899 900
  uint32_t            dbNum = 0, stbNum = 0, allDbNum = 0, allStbNum = 0;
  int32_t             i = 0;
D
dapan1121 已提交
901 902 903 904 905 906
  while (i < 5) {
    ++i;
    code = catalogGetExpiredDBs(pCtg, &dbs, &dbNum);
    ASSERT_EQ(code, 0);
    code = catalogGetExpiredSTables(pCtg, &stb, &stbNum);
    ASSERT_EQ(code, 0);
S
Shengliang Guan 已提交
907

D
dapan1121 已提交
908
    if (dbNum) {
S
Shengliang Guan 已提交
909
      printf("got expired db,dbId:%" PRId64 "\n", dbs->dbId);
D
dapan1121 已提交
910 911 912 913 914 915 916
      free(dbs);
      dbs = NULL;
    } else {
      printf("no expired db\n");
    }

    if (stbNum) {
D
dapan 已提交
917
      printf("got expired stb,suid:%" PRId64 ",dbFName:%s, stbName:%s\n", stb->suid, stb->dbFName, stb->stbName);      
D
dapan1121 已提交
918 919 920 921 922 923 924 925 926 927
      free(stb);
      stb = NULL;
    } else {
      printf("no expired stb\n");
    }

    allDbNum += dbNum;
    allStbNum += stbNum;
    sleep(2);
  }
S
Shengliang Guan 已提交
928

D
dapan1121 已提交
929 930 931
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 1);

D
dapan1121 已提交
932 933 934
  catalogDestroy();
}

D
dapan 已提交
935
TEST(tableMeta, superTableCase) {
S
Shengliang Guan 已提交
936 937 938
  struct SCatalog *pCtg = NULL;
  void            *mockPointer = (void *)0x1;
  SVgroupInfo      vgInfo = {0};
D
dapan1121 已提交
939 940 941 942 943

  ctgTestSetPrepareDbVgroupsAndSuperMeta();

  initQueryModuleMsgHandle();

D
dapan1121 已提交
944 945 946
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

S
Shengliang Guan 已提交
947
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
D
dapan1121 已提交
948
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan1121 已提交
949 950
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
951
  SName n = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
H
Haojun Liao 已提交
952 953
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestSTablename);
D
dapan1121 已提交
954 955

  STableMeta *tableMeta = NULL;
H
Haojun Liao 已提交
956
  code = catalogGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta);
D
dapan1121 已提交
957 958 959 960 961
  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 已提交
962 963
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
964 965 966 967 968 969 970 971
  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 已提交
972 973 974 975

  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestCTablename);
  code = catalogGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta);
D
dapan1121 已提交
976 977 978 979 980 981 982 983 984 985 986
  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 已提交
987
  code = catalogRenewAndGetTableMeta(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
988 989 990 991 992 993 994 995 996 997
  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);

S
Shengliang Guan 已提交
998
  SDbVgVersion       *dbs = NULL;
D
dapan1121 已提交
999
  SSTableMetaVersion *stb = NULL;
S
Shengliang Guan 已提交
1000 1001
  uint32_t            dbNum = 0, stbNum = 0, allDbNum = 0, allStbNum = 0;
  int32_t             i = 0;
D
dapan1121 已提交
1002 1003 1004 1005 1006 1007
  while (i < 5) {
    ++i;
    code = catalogGetExpiredDBs(pCtg, &dbs, &dbNum);
    ASSERT_EQ(code, 0);
    code = catalogGetExpiredSTables(pCtg, &stb, &stbNum);
    ASSERT_EQ(code, 0);
S
Shengliang Guan 已提交
1008

D
dapan1121 已提交
1009
    if (dbNum) {
S
Shengliang Guan 已提交
1010
      printf("got expired db,dbId:%" PRId64 "\n", dbs->dbId);
D
dapan1121 已提交
1011 1012 1013 1014 1015 1016 1017
      free(dbs);
      dbs = NULL;
    } else {
      printf("no expired db\n");
    }

    if (stbNum) {
D
dapan 已提交
1018 1019
      printf("got expired stb,suid:%" PRId64 ",dbFName:%s, stbName:%s\n", stb->suid, stb->dbFName, stb->stbName);
      
D
dapan1121 已提交
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
      free(stb);
      stb = NULL;
    } else {
      printf("no expired stb\n");
    }

    allDbNum += dbNum;
    allStbNum += stbNum;
    sleep(2);
  }
S
Shengliang Guan 已提交
1030

D
dapan1121 已提交
1031 1032
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 1);
D
dapan1121 已提交
1033 1034

  catalogDestroy();
D
dapan1121 已提交
1035
}
1036

D
dapan1121 已提交
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 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 1153 1154
TEST(tableMeta, rmStbMeta) {
  struct SCatalog *pCtg = NULL;
  void            *mockPointer = (void *)0x1;
  SVgroupInfo      vgInfo = {0};

  ctgTestInitLogFile();

  ctgTestSetPrepareDbVgroupsAndSuperMeta();

  initQueryModuleMsgHandle();

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

  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
  ASSERT_EQ(code, 0);

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

  STableMeta *tableMeta = NULL;
  code = catalogGetTableMeta(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);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
  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 = catalogRemoveSTableMeta(pCtg, "1.db1", ctgTestSTablename, ctgTestSuid);
  ASSERT_EQ(code, 0);

  ASSERT_EQ(ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_DB_NUM), 1);
  ASSERT_EQ(ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_META_NUM), 0);
  ASSERT_EQ(ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_STB_NUM), 0);
  ASSERT_EQ(ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_DB_RENT_NUM), 1);
  ASSERT_EQ(ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_STB_RENT_NUM), 0);
  
  catalogDestroy();
}

TEST(tableMeta, updateStbMeta) {
  struct SCatalog *pCtg = NULL;
  void            *mockPointer = (void *)0x1;
  SVgroupInfo      vgInfo = {0};

  ctgTestInitLogFile();

  ctgTestSetPrepareDbVgroupsAndSuperMeta();

  initQueryModuleMsgHandle();

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

  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
  ASSERT_EQ(code, 0);

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

  STableMeta *tableMeta = NULL;
  code = catalogGetTableMeta(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);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
  ASSERT_EQ(tableMeta->tableInfo.numOfColumns, ctgTestColNum);
  ASSERT_EQ(tableMeta->tableInfo.numOfTags, ctgTestTagNum);
  ASSERT_EQ(tableMeta->tableInfo.precision, 1);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);

  tfree(tableMeta);

  STableMetaRsp rsp = {0};
  ctgTestBuildSTableMetaRsp(&rsp);

  code = catalogUpdateSTableMeta(pCtg, &rsp);
  ASSERT_EQ(code, 0);

  ASSERT_EQ(ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_DB_NUM), 1);
  ASSERT_EQ(ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_META_NUM), 1);
  ASSERT_EQ(ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_STB_NUM), 1);
  ASSERT_EQ(ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_DB_RENT_NUM), 1);
  ASSERT_EQ(ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_STB_RENT_NUM), 1);

  code = catalogGetTableMeta(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 + 1);
  ASSERT_EQ(tableMeta->tversion, ctgTestTVersion + 1);
  ASSERT_EQ(tableMeta->uid, ctgTestSuid + 1);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid + 1);
  ASSERT_EQ(tableMeta->tableInfo.numOfColumns, ctgTestColNum);
  ASSERT_EQ(tableMeta->tableInfo.numOfTags, ctgTestTagNum);
  ASSERT_EQ(tableMeta->tableInfo.precision, 1 + 1);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);

  tfree(tableMeta);
  
  catalogDestroy();
}



D
dapan 已提交
1155
TEST(tableDistVgroup, normalTable) {
S
Shengliang Guan 已提交
1156 1157 1158 1159
  struct SCatalog *pCtg = NULL;
  void            *mockPointer = (void *)0x1;
  SVgroupInfo     *vgInfo = NULL;
  SArray          *vgList = NULL;
D
dapan 已提交
1160 1161 1162 1163 1164

  ctgTestSetPrepareDbVgroupsAndNormalMeta();

  initQueryModuleMsgHandle();

D
dapan1121 已提交
1165 1166 1167
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

S
Shengliang Guan 已提交
1168 1169
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);

D
dapan1121 已提交
1170
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan 已提交
1171 1172
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
1173
  SName n = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
H
Haojun Liao 已提交
1174 1175
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);
D
dapan 已提交
1176

H
Haojun Liao 已提交
1177
  code = catalogGetTableDistVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &vgList);
D
dapan 已提交
1178 1179 1180 1181
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), 1);
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 0);
  ASSERT_EQ(vgInfo->vgId, 8);
H
Haojun Liao 已提交
1182
  ASSERT_EQ(vgInfo->epset.numOfEps, 3);
D
dapan 已提交
1183 1184 1185 1186 1187

  catalogDestroy();
}

TEST(tableDistVgroup, childTableCase) {
S
Shengliang Guan 已提交
1188 1189 1190 1191
  struct SCatalog *pCtg = NULL;
  void            *mockPointer = (void *)0x1;
  SVgroupInfo     *vgInfo = NULL;
  SArray          *vgList = NULL;
D
dapan 已提交
1192 1193 1194 1195 1196

  ctgTestSetPrepareDbVgroupsAndChildMeta();

  initQueryModuleMsgHandle();

S
Shengliang Guan 已提交
1197 1198
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);

D
dapan 已提交
1199 1200 1201 1202 1203 1204
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

D
dapan1121 已提交
1205
  SName n = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
H
Haojun Liao 已提交
1206 1207 1208 1209
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestCTablename);

  code = catalogGetTableDistVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &vgList);
D
dapan 已提交
1210 1211 1212 1213
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), 1);
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 0);
  ASSERT_EQ(vgInfo->vgId, 9);
H
Haojun Liao 已提交
1214
  ASSERT_EQ(vgInfo->epset.numOfEps, 4);
D
dapan 已提交
1215 1216 1217 1218 1219

  catalogDestroy();
}

TEST(tableDistVgroup, superTableCase) {
S
Shengliang Guan 已提交
1220 1221 1222 1223
  struct SCatalog *pCtg = NULL;
  void            *mockPointer = (void *)0x1;
  SVgroupInfo     *vgInfo = NULL;
  SArray          *vgList = NULL;
D
dapan 已提交
1224 1225 1226 1227 1228

  ctgTestSetPrepareDbVgroupsAndSuperMeta();

  initQueryModuleMsgHandle();

D
dapan1121 已提交
1229 1230 1231
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

S
Shengliang Guan 已提交
1232
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
D
dapan1121 已提交
1233
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan 已提交
1234 1235
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
1236
  SName n = {.type = TSDB_TABLE_NAME_T, .acctId = 1};
H
Haojun Liao 已提交
1237 1238
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestSTablename);
D
dapan 已提交
1239

H
Haojun Liao 已提交
1240
  code = catalogGetTableDistVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &vgList);
D
dapan 已提交
1241 1242 1243 1244
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), 10);
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 0);
  ASSERT_EQ(vgInfo->vgId, 1);
H
Haojun Liao 已提交
1245
  ASSERT_EQ(vgInfo->epset.numOfEps, 1);
D
dapan 已提交
1246 1247
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 1);
  ASSERT_EQ(vgInfo->vgId, 2);
H
Haojun Liao 已提交
1248
  ASSERT_EQ(vgInfo->epset.numOfEps, 2);
D
dapan 已提交
1249 1250
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 2);
  ASSERT_EQ(vgInfo->vgId, 3);
H
Haojun Liao 已提交
1251
  ASSERT_EQ(vgInfo->epset.numOfEps, 3);
D
dapan 已提交
1252 1253 1254 1255

  catalogDestroy();
}

D
dapan1121 已提交
1256
TEST(dbVgroup, getSetDbVgroupCase) {
S
Shengliang Guan 已提交
1257 1258 1259 1260
  struct SCatalog *pCtg = NULL;
  void            *mockPointer = (void *)0x1;
  SVgroupInfo      vgInfo = {0};
  SVgroupInfo     *pvgInfo = NULL;
D
dapan 已提交
1261
  SDBVgroupInfo    *dbVgroup = NULL;
S
Shengliang Guan 已提交
1262
  SArray          *vgList = NULL;
D
dapan1121 已提交
1263

D
dapan 已提交
1264 1265
  ctgTestInitLogFile();

D
dapan1121 已提交
1266 1267 1268 1269
  ctgTestSetPrepareDbVgroupsAndNormalMeta();

  initQueryModuleMsgHandle();

S
Shengliang Guan 已提交
1270 1271
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);

D
dapan1121 已提交
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
  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);
S
Shengliang Guan 已提交
1285

D
dapan1121 已提交
1286 1287 1288
  code = catalogGetTableHashVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &vgInfo);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
H
Haojun Liao 已提交
1289
  ASSERT_EQ(vgInfo.epset.numOfEps, 3);
D
dapan1121 已提交
1290 1291 1292 1293 1294 1295

  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);
H
Haojun Liao 已提交
1296
  ASSERT_EQ(pvgInfo->epset.numOfEps, 3);
D
dapan1121 已提交
1297 1298 1299
  taosArrayDestroy(vgList);

  ctgTestBuildDBVgroup(&dbVgroup);
D
dapan1121 已提交
1300
  code = catalogUpdateDBVgroup(pCtg, ctgTestDbname, ctgTestDbId, dbVgroup);
D
dapan1121 已提交
1301 1302 1303 1304 1305
  ASSERT_EQ(code, 0);

  code = catalogGetTableHashVgroup(pCtg, mockPointer, (const SEpSet *)mockPointer, &n, &vgInfo);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 7);
H
Haojun Liao 已提交
1306
  ASSERT_EQ(vgInfo.epset.numOfEps, 2);
D
dapan1121 已提交
1307 1308 1309 1310 1311 1312

  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);
H
Haojun Liao 已提交
1313
  ASSERT_EQ(pvgInfo->epset.numOfEps, 3);
D
dapan1121 已提交
1314
  taosArrayDestroy(vgList);
S
Shengliang Guan 已提交
1315

D
dapan1121 已提交
1316 1317 1318
  catalogDestroy();
}

D
dapan1121 已提交
1319
TEST(multiThread, getSetRmSameDbVgroup) {
S
Shengliang Guan 已提交
1320 1321 1322 1323 1324 1325
  struct SCatalog *pCtg = NULL;
  void            *mockPointer = (void *)0x1;
  SVgroupInfo      vgInfo = {0};
  SVgroupInfo     *pvgInfo = NULL;
  SDBVgroupInfo    dbVgroup = {0};
  SArray          *vgList = NULL;
D
dapan1121 已提交
1326
  ctgTestStop = false;
D
dapan1121 已提交
1327 1328

  ctgTestInitLogFile();
S
Shengliang Guan 已提交
1329

D
dapan1121 已提交
1330 1331 1332 1333
  ctgTestSetPrepareDbVgroups();

  initQueryModuleMsgHandle();

S
Shengliang Guan 已提交
1334 1335
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);

D
dapan1121 已提交
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
  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;
D
dapan1121 已提交
1350
  pthread_create(&(thread1), &thattr, ctgTestSetSameDbVgroupThread, pCtg);
D
dapan1121 已提交
1351 1352

  sleep(1);
D
dapan1121 已提交
1353
  pthread_create(&(thread2), &thattr, ctgTestGetDbVgroupThread, pCtg);
D
dapan1121 已提交
1354 1355 1356 1357 1358

  while (true) {
    if (ctgTestDeadLoop) {
      sleep(1);
    } else {
D
dapan1121 已提交
1359
      sleep(ctgTestMTRunSec);
D
dapan1121 已提交
1360 1361 1362
      break;
    }
  }
S
Shengliang Guan 已提交
1363

D
dapan1121 已提交
1364 1365
  ctgTestStop = true;
  sleep(1);
S
Shengliang Guan 已提交
1366

D
dapan1121 已提交
1367 1368 1369
  catalogDestroy();
}

D
dapan1121 已提交
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
TEST(multiThread, getSetRmDiffDbVgroup) {
  struct SCatalog *pCtg = NULL;
  void            *mockPointer = (void *)0x1;
  SVgroupInfo      vgInfo = {0};
  SVgroupInfo     *pvgInfo = NULL;
  SDBVgroupInfo    dbVgroup = {0};
  SArray          *vgList = NULL;
  ctgTestStop = false;

  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, ctgTestSetDiffDbVgroupThread, pCtg);

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

  while (true) {
    if (ctgTestDeadLoop) {
      sleep(1);
    } else {
      sleep(ctgTestMTRunSec);
      break;
    }
  }

  ctgTestStop = true;
  sleep(1);

  catalogDestroy();
}


D
dapan 已提交
1422

D
dapan1121 已提交
1423
TEST(multiThread, ctableMeta) {
S
Shengliang Guan 已提交
1424 1425 1426 1427 1428 1429
  struct SCatalog *pCtg = NULL;
  void            *mockPointer = (void *)0x1;
  SVgroupInfo      vgInfo = {0};
  SVgroupInfo     *pvgInfo = NULL;
  SDBVgroupInfo    dbVgroup = {0};
  SArray          *vgList = NULL;
D
dapan1121 已提交
1430
  ctgTestStop = false;
D
dapan1121 已提交
1431

D
dapan 已提交
1432 1433
  ctgTestInitLogFile();

D
dapan1121 已提交
1434 1435 1436 1437
  ctgTestSetPrepareDbVgroupsAndChildMeta();

  initQueryModuleMsgHandle();

S
Shengliang Guan 已提交
1438 1439
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);

D
dapan1121 已提交
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
  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 已提交
1455 1456
  sleep(1);
  pthread_create(&(thread1), &thattr, ctgTestGetCtableMetaThread, pCtg);
D
dapan1121 已提交
1457 1458 1459 1460 1461

  while (true) {
    if (ctgTestDeadLoop) {
      sleep(1);
    } else {
D
dapan1121 已提交
1462
      sleep(ctgTestMTRunSec);
D
dapan1121 已提交
1463 1464 1465
      break;
    }
  }
S
Shengliang Guan 已提交
1466

D
dapan1121 已提交
1467
  ctgTestStop = true;
D
dapan 已提交
1468
  sleep(2);
S
Shengliang Guan 已提交
1469

D
dapan1121 已提交
1470 1471
  catalogDestroy();
}
D
dapan 已提交
1472

D
dapan 已提交
1473 1474


D
dapan1121 已提交
1475
TEST(rentTest, allRent) {
S
Shengliang Guan 已提交
1476 1477 1478 1479 1480 1481
  struct SCatalog *pCtg = NULL;
  void            *mockPointer = (void *)0x1;
  SVgroupInfo      vgInfo = {0};
  SVgroupInfo     *pvgInfo = NULL;
  SDBVgroupInfo    dbVgroup = {0};
  SArray          *vgList = NULL;
D
dapan1121 已提交
1482
  ctgTestStop = false;
S
Shengliang Guan 已提交
1483
  SDbVgVersion       *dbs = NULL;
D
dapan1121 已提交
1484
  SSTableMetaVersion *stable = NULL;
S
Shengliang Guan 已提交
1485
  uint32_t            num = 0;
D
dapan1121 已提交
1486

D
dapan 已提交
1487 1488
  ctgTestInitLogFile();

D
dapan1121 已提交
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
  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);
S
Shengliang Guan 已提交
1518

D
dapan1121 已提交
1519 1520 1521 1522
    code = catalogGetExpiredDBs(pCtg, &dbs, &num);
    ASSERT_EQ(code, 0);
    printf("%d - expired dbNum:%d\n", i, num);
    if (dbs) {
S
Shengliang Guan 已提交
1523
      printf("%d - expired dbId:%" PRId64 ", vgVersion:%d\n", i, dbs->dbId, dbs->vgVersion);
D
dapan1121 已提交
1524 1525 1526
      free(dbs);
      dbs = NULL;
    }
S
Shengliang Guan 已提交
1527

D
dapan1121 已提交
1528 1529 1530 1531 1532
    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) {
D
dapan 已提交
1533
        printf("suid:%" PRId64 ", dbFName:%s, stbName:%s, sversion:%d, tversion:%d\n", stable[n].suid, stable[n].dbFName, stable[n].stbName, stable[n].sversion, stable[n].tversion);
D
dapan1121 已提交
1534 1535 1536 1537 1538
      }
      free(stable);
      stable = NULL;
    }
    printf("*************************************************\n");
S
Shengliang Guan 已提交
1539

D
dapan1121 已提交
1540 1541
    sleep(2);
  }
S
Shengliang Guan 已提交
1542

D
dapan1121 已提交
1543 1544 1545
  catalogDestroy();
}

S
Shengliang Guan 已提交
1546
int main(int argc, char **argv) {
1547 1548 1549
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
D
dapan1121 已提交
1550

D
dapan1121 已提交
1551
#pragma GCC diagnostic pop