catalogTests.cpp 65.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 <iostream>

S
Shengliang Guan 已提交
19 20
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
21 22 23
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"
S
Shengliang Guan 已提交
24
#pragma GCC diagnostic ignored "-Wformat"
25
#include <addr_any.h>
26

wafwerar's avatar
wafwerar 已提交
27 28 29
#ifdef WINDOWS
#define TD_USE_WINSOCK
#endif
S
Shengliang Guan 已提交
30
#include "catalog.h"
H
Haojun Liao 已提交
31
#include "catalogInt.h"
H
Hongze Cheng 已提交
32
#include "os.h"
S
Shengliang Guan 已提交
33
#include "stub.h"
D
dapan1121 已提交
34
#include "taos.h"
H
Haojun Liao 已提交
35
#include "tdatablock.h"
36
#include "tdef.h"
H
Hongze Cheng 已提交
37
#include "tglobal.h"
D
ut test  
dapan1121 已提交
38
#include "trpc.h"
S
Shengliang Guan 已提交
39
#include "tvariant.h"
40

D
dapan1121 已提交
41
namespace {
42

H
Hongze Cheng 已提交
43
extern "C" int32_t ctgdGetClusterCacheNum(struct SCatalog *pCatalog, int32_t type);
D
dapan1121 已提交
44 45
extern "C" int32_t ctgdEnableDebug(char *option);
extern "C" int32_t ctgdGetStatNum(char *option, void *res);
D
dapan1121 已提交
46

D
dapan 已提交
47 48 49 50 51
void ctgTestSetRspTableMeta();
void ctgTestSetRspCTableMeta();
void ctgTestSetRspSTableMeta();
void ctgTestSetRspMultiSTableMeta();

H
Hongze Cheng 已提交
52
// extern "C" SCatalogMgmt gCtgMgmt;
D
dapan 已提交
53 54 55 56 57 58 59

enum {
  CTGT_RSP_VGINFO = 1,
  CTGT_RSP_TBMETA,
  CTGT_RSP_CTBMETA,
  CTGT_RSP_STBMETA,
  CTGT_RSP_MSTBMETA,
D
dapan1121 已提交
60
  CTGT_RSP_TBMETA_NOT_EXIST,
D
dapan 已提交
61
};
D
dapan1121 已提交
62

S
Shengliang Guan 已提交
63 64
bool    ctgTestStop = false;
bool    ctgTestEnableSleep = false;
D
dapan1121 已提交
65
bool    ctgTestEnableLog = true;
S
Shengliang Guan 已提交
66
bool    ctgTestDeadLoop = false;
D
dapan1121 已提交
67
int32_t ctgTestPrintNum = 10000;
D
dapan1121 已提交
68
int32_t ctgTestMTRunSec = 5;
D
dapan1121 已提交
69

S
Shengliang Guan 已提交
70 71 72 73 74 75 76 77
int32_t  ctgTestCurrentVgVersion = 0;
int32_t  ctgTestVgVersion = 1;
int32_t  ctgTestVgNum = 10;
int32_t  ctgTestColNum = 2;
int32_t  ctgTestTagNum = 1;
int32_t  ctgTestSVersion = 1;
int32_t  ctgTestTVersion = 1;
int32_t  ctgTestSuid = 2;
D
dapan1121 已提交
78
uint64_t ctgTestDbId = 33;
D
dapan1121 已提交
79
uint64_t ctgTestNormalTblUid = 1;
D
dapan1121 已提交
80

81
uint64_t ctgTestClusterId = 0x1;
S
Shengliang Guan 已提交
82 83 84 85
char    *ctgTestDbname = "1.db1";
char    *ctgTestTablename = "table1";
char    *ctgTestCTablename = "ctable1";
char    *ctgTestSTablename = "stable1";
D
dapan1121 已提交
86 87 88
char    *ctgTestCurrentCTableName = NULL;
char    *ctgTestCurrentTableName = NULL;
char    *ctgTestCurrentSTableName = NULL;
D
dapan1121 已提交
89

D
dapan1121 已提交
90
int32_t ctgTestRspFunc[100] = {0};
D
dapan 已提交
91 92
int32_t ctgTestRspIdx = 0;

D
ut test  
dapan1121 已提交
93
void sendCreateDbMsg(void *shandle, SEpSet *pEpSet) {
S
Shengliang Guan 已提交
94 95 96
  SCreateDbReq createReq = {0};
  strcpy(createReq.db, "1.db1");
  createReq.numOfVgroups = 2;
S
Shengliang Guan 已提交
97 98 99
  createReq.buffer = -1;
  createReq.pageSize = -1;
  createReq.pages = -1;
S
Shengliang Guan 已提交
100 101 102 103
  createReq.daysPerFile = 10;
  createReq.daysToKeep0 = 3650;
  createReq.daysToKeep1 = 3650;
  createReq.daysToKeep2 = 3650;
S
Shengliang Guan 已提交
104 105
  createReq.minRows = 100;
  createReq.maxRows = 4096;
106
  createReq.walFsyncPeriod = 3000;
S
Shengliang Guan 已提交
107 108 109 110
  createReq.walLevel = 1;
  createReq.precision = 0;
  createReq.compression = 2;
  createReq.replications = 1;
S
Shengliang Guan 已提交
111
  createReq.strict = 1;
112
  createReq.cacheLast = 0;
S
Shengliang Guan 已提交
113 114 115 116 117
  createReq.ignoreExist = 1;

  int32_t contLen = tSerializeSCreateDbReq(NULL, 0, &createReq);
  void   *pReq = rpcMallocCont(contLen);
  tSerializeSCreateDbReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
118

D
ut test  
dapan1121 已提交
119 120
  SRpcMsg rpcMsg = {0};
  rpcMsg.pCont = pReq;
S
Shengliang Guan 已提交
121
  rpcMsg.contLen = contLen;
H
Hongze Cheng 已提交
122
  rpcMsg.msgType = TDMT_MND_CREATE_DB;
D
ut test  
dapan1121 已提交
123 124 125 126 127 128 129

  SRpcMsg rpcRsp = {0};
  rpcSendRecv(shandle, pEpSet, &rpcMsg, &rpcRsp);

  ASSERT_EQ(rpcRsp.code, 0);
}

D
dapan1121 已提交
130
void ctgTestInitLogFile() {
D
dapan1121 已提交
131 132 133
  if (!ctgTestEnableLog) {
    return;
  }
H
Hongze Cheng 已提交
134

S
Shengliang Guan 已提交
135 136
  const char   *defaultLogFileNamePrefix = "taoslog";
  const int32_t maxLogFileNum = 10;
D
dapan1121 已提交
137 138

  tsAsyncLog = 0;
D
dapan1121 已提交
139
  qDebugFlag = 159;
wafwerar's avatar
wafwerar 已提交
140
  strcpy(tsLogDir, TD_LOG_DIR_PATH);
D
dapan1121 已提交
141

D
dapan1121 已提交
142 143 144
  ctgdEnableDebug("api");
  ctgdEnableDebug("meta");
  ctgdEnableDebug("cache");
H
Hongze Cheng 已提交
145

S
Shengliang Guan 已提交
146
  if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) {
D
dapan1121 已提交
147 148 149 150 151 152 153 154 155
    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) {
H
Hongze Cheng 已提交
156
  SName cn = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
157 158 159
  strcpy(cn.dbname, "db1");
  strcpy(cn.tname, ctgTestCTablename);

H
Hongze Cheng 已提交
160
  SName sn = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
161 162 163
  strcpy(sn.dbname, "db1");
  strcpy(sn.tname, ctgTestSTablename);

D
dapan 已提交
164 165 166 167
  char db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(&cn, db);

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

D
dapan1121 已提交
170 171
  strcpy(output->ctbName, cn.tname);
  strcpy(output->tbName, sn.tname);
D
dapan1121 已提交
172 173 174 175 176 177

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

H
Hongze Cheng 已提交
178 179
  output->tbMeta =
      (STableMeta *)taosMemoryCalloc(1, sizeof(STableMeta) + sizeof(SSchema) * (ctgTestColNum + ctgTestColNum));
D
dapan1121 已提交
180 181 182 183 184 185 186
  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 已提交
187

D
dapan1121 已提交
188 189
  output->tbMeta->sversion = ctgTestSVersion;
  output->tbMeta->tversion = ctgTestTVersion;
S
Shengliang Guan 已提交
190

D
dapan1121 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  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
dapan1121 已提交
211
void ctgTestBuildDBVgroup(SDBVgInfo **pdbVgroup) {
D
dapan1121 已提交
212
  static int32_t vgVersion = ctgTestVgVersion + 1;
S
Shengliang Guan 已提交
213 214
  int32_t        vgNum = 0;
  SVgroupInfo    vgInfo = {0};
H
Hongze Cheng 已提交
215
  SDBVgInfo     *dbVgroup = (SDBVgInfo *)taosMemoryCalloc(1, sizeof(SDBVgInfo));
S
Shengliang Guan 已提交
216

D
dapan1121 已提交
217
  dbVgroup->vgVersion = vgVersion++;
S
Shengliang Guan 已提交
218

D
dapan1121 已提交
219
  ctgTestCurrentVgVersion = dbVgroup->vgVersion;
S
Shengliang Guan 已提交
220

D
dapan1121 已提交
221
  dbVgroup->hashMethod = 0;
222 223
  dbVgroup->hashPrefix = 0;
  dbVgroup->hashSuffix = 0;
D
dapan1121 已提交
224
  dbVgroup->vgHash = taosHashInit(ctgTestVgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
D
dapan1121 已提交
225 226 227 228 229 230 231 232

  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;
L
Liu Jicong 已提交
233 234 235 236
    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 已提交
237
      strcpy(addr->fqdn, "a0");
S
Shengliang Guan 已提交
238
      addr->port = n + 22;
D
dapan1121 已提交
239 240
    }

D
dapan1121 已提交
241
    taosHashPut(dbVgroup->vgHash, &vgInfo.vgId, sizeof(vgInfo.vgId), &vgInfo, sizeof(vgInfo));
D
dapan1121 已提交
242
  }
D
dapan 已提交
243 244

  *pdbVgroup = dbVgroup;
D
dapan1121 已提交
245 246
}

D
dapan1121 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259
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->sversion = ctgTestSVersion + 1;
  rspMsg->tversion = ctgTestTVersion + 1;
  rspMsg->suid = ctgTestSuid + 1;
  rspMsg->tuid = ctgTestSuid + 1;
  rspMsg->vgId = 1;
H
Hongze Cheng 已提交
260

wafwerar's avatar
wafwerar 已提交
261
  rspMsg->pSchemas = (SSchema *)taosMemoryCalloc(rspMsg->numOfTags + rspMsg->numOfColumns, sizeof(SSchema));
D
dapan1121 已提交
262 263

  SSchema *s = NULL;
S
Shengliang Guan 已提交
264
  s = &rspMsg->pSchemas[0];
D
dapan1121 已提交
265 266 267 268 269
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
  s->colId = 1;
  s->bytes = 8;
  strcpy(s->name, "ts");

S
Shengliang Guan 已提交
270
  s = &rspMsg->pSchemas[1];
D
dapan1121 已提交
271 272 273 274 275
  s->type = TSDB_DATA_TYPE_INT;
  s->colId = 2;
  s->bytes = 4;
  strcpy(s->name, "col1s");

S
Shengliang Guan 已提交
276
  s = &rspMsg->pSchemas[2];
D
dapan1121 已提交
277 278 279 280 281 282 283 284
  s->type = TSDB_DATA_TYPE_BINARY;
  s->colId = 3;
  s->bytes = 12 + 1;
  strcpy(s->name, "tag1s");

  return;
}

D
dapan 已提交
285
void ctgTestRspDbVgroups(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
286 287 288
  SUseDbRsp usedbRsp = {0};
  strcpy(usedbRsp.db, ctgTestDbname);
  usedbRsp.vgVersion = ctgTestVgVersion;
D
dapan1121 已提交
289
  ctgTestCurrentVgVersion = ctgTestVgVersion;
S
Shengliang Guan 已提交
290 291 292 293
  usedbRsp.vgNum = ctgTestVgNum;
  usedbRsp.hashMethod = 0;
  usedbRsp.uid = ctgTestDbId;
  usedbRsp.pVgroupInfos = taosArrayInit(usedbRsp.vgNum, sizeof(SVgroupInfo));
D
dapan1121 已提交
294

S
Shengliang Guan 已提交
295
  uint32_t hashUnit = UINT32_MAX / ctgTestVgNum;
D
dapan1121 已提交
296
  for (int32_t i = 0; i < ctgTestVgNum; ++i) {
S
Shengliang Guan 已提交
297 298 299 300 301 302 303 304
    SVgroupInfo vg = {0};
    vg.vgId = i + 1;
    vg.hashBegin = i * hashUnit;
    vg.hashEnd = hashUnit * (i + 1) - 1;
    if (i == ctgTestVgNum - 1) {
      vg.hashEnd = htonl(UINT32_MAX);
    }

L
Liu Jicong 已提交
305 306 307 308
    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 已提交
309
      strcpy(addr->fqdn, "a0");
S
Shengliang Guan 已提交
310
      addr->port = n + 22;
D
dapan1121 已提交
311
    }
S
Shengliang Guan 已提交
312 313

    taosArrayPush(usedbRsp.pVgroupInfos, &vg);
D
dapan1121 已提交
314 315
  }

S
Shengliang Guan 已提交
316 317 318
  int32_t contLen = tSerializeSUseDbRsp(NULL, 0, &usedbRsp);
  void   *pReq = rpcMallocCont(contLen);
  tSerializeSUseDbRsp(pReq, contLen, &usedbRsp);
D
dapan1121 已提交
319

S
Shengliang Guan 已提交
320 321 322
  pRsp->code = 0;
  pRsp->contLen = contLen;
  pRsp->pCont = pReq;
D
dapan1121 已提交
323 324
}

D
dapan 已提交
325
void ctgTestRspTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
326 327 328 329 330 331 332 333 334 335
  STableMetaRsp metaRsp = {0};
  strcpy(metaRsp.dbFName, ctgTestDbname);
  strcpy(metaRsp.tbName, ctgTestTablename);
  metaRsp.numOfTags = 0;
  metaRsp.numOfColumns = ctgTestColNum;
  metaRsp.precision = 1;
  metaRsp.tableType = TSDB_NORMAL_TABLE;
  metaRsp.sversion = ctgTestSVersion;
  metaRsp.tversion = ctgTestTVersion;
  metaRsp.suid = 0;
D
dapan1121 已提交
336
  metaRsp.tuid = ctgTestNormalTblUid++;
S
Shengliang Guan 已提交
337
  metaRsp.vgId = 8;
wafwerar's avatar
wafwerar 已提交
338
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc((metaRsp.numOfTags + metaRsp.numOfColumns) * sizeof(SSchema));
D
dapan1121 已提交
339 340

  SSchema *s = NULL;
S
Shengliang Guan 已提交
341
  s = &metaRsp.pSchemas[0];
D
dapan1121 已提交
342
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
S
Shengliang Guan 已提交
343 344
  s->colId = 1;
  s->bytes = 8;
D
dapan1121 已提交
345 346
  strcpy(s->name, "ts");

S
Shengliang Guan 已提交
347
  s = &metaRsp.pSchemas[1];
D
dapan1121 已提交
348
  s->type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
349 350
  s->colId = 2;
  s->bytes = 4;
D
dapan1121 已提交
351
  strcpy(s->name, "col1");
S
Shengliang Guan 已提交
352

S
Shengliang Guan 已提交
353 354 355 356 357 358 359 360 361
  int32_t contLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  void   *pReq = rpcMallocCont(contLen);
  tSerializeSTableMetaRsp(pReq, contLen, &metaRsp);

  pRsp->code = 0;
  pRsp->contLen = contLen;
  pRsp->pCont = pReq;

  tFreeSTableMetaRsp(&metaRsp);
D
dapan1121 已提交
362 363
}

D
dapan1121 已提交
364 365 366 367
void ctgTestRspTableMetaNotExist(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  pRsp->code = CTG_ERR_CODE_TABLE_NOT_EXIST;
}

D
dapan 已提交
368
void ctgTestRspCTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
369 370
  STableMetaRsp metaRsp = {0};
  strcpy(metaRsp.dbFName, ctgTestDbname);
D
dapan1121 已提交
371
  strcpy(metaRsp.tbName, ctgTestCurrentCTableName ? ctgTestCurrentCTableName : ctgTestCTablename);
S
Shengliang Guan 已提交
372 373 374 375 376 377 378 379 380 381
  strcpy(metaRsp.stbName, ctgTestSTablename);
  metaRsp.numOfTags = ctgTestTagNum;
  metaRsp.numOfColumns = ctgTestColNum;
  metaRsp.precision = 1;
  metaRsp.tableType = TSDB_CHILD_TABLE;
  metaRsp.sversion = ctgTestSVersion;
  metaRsp.tversion = ctgTestTVersion;
  metaRsp.suid = 0x0000000000000002;
  metaRsp.tuid = 0x0000000000000003;
  metaRsp.vgId = 9;
wafwerar's avatar
wafwerar 已提交
382
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc((metaRsp.numOfTags + metaRsp.numOfColumns) * sizeof(SSchema));
D
dapan1121 已提交
383 384

  SSchema *s = NULL;
S
Shengliang Guan 已提交
385
  s = &metaRsp.pSchemas[0];
D
dapan1121 已提交
386
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
S
Shengliang Guan 已提交
387 388
  s->colId = 1;
  s->bytes = 8;
D
dapan1121 已提交
389 390
  strcpy(s->name, "ts");

S
Shengliang Guan 已提交
391
  s = &metaRsp.pSchemas[1];
D
dapan1121 已提交
392
  s->type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
393 394
  s->colId = 2;
  s->bytes = 4;
D
dapan1121 已提交
395 396
  strcpy(s->name, "col1s");

S
Shengliang Guan 已提交
397
  s = &metaRsp.pSchemas[2];
D
dapan1121 已提交
398
  s->type = TSDB_DATA_TYPE_BINARY;
S
Shengliang Guan 已提交
399 400
  s->colId = 3;
  s->bytes = 12;
D
dapan1121 已提交
401 402
  strcpy(s->name, "tag1s");

S
Shengliang Guan 已提交
403 404 405 406 407 408 409 410 411
  int32_t contLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  void   *pReq = rpcMallocCont(contLen);
  tSerializeSTableMetaRsp(pReq, contLen, &metaRsp);

  pRsp->code = 0;
  pRsp->contLen = contLen;
  pRsp->pCont = pReq;

  tFreeSTableMetaRsp(&metaRsp);
D
dapan1121 已提交
412 413
}

D
dapan 已提交
414
void ctgTestRspSTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
415 416
  STableMetaRsp metaRsp = {0};
  strcpy(metaRsp.dbFName, ctgTestDbname);
D
dapan1121 已提交
417
  strcpy(metaRsp.tbName, ctgTestCurrentSTableName ? ctgTestCurrentSTableName : ctgTestSTablename);
S
Shengliang Guan 已提交
418 419 420 421 422 423 424 425
  strcpy(metaRsp.stbName, ctgTestSTablename);
  metaRsp.numOfTags = ctgTestTagNum;
  metaRsp.numOfColumns = ctgTestColNum;
  metaRsp.precision = 1;
  metaRsp.tableType = TSDB_SUPER_TABLE;
  metaRsp.sversion = ctgTestSVersion;
  metaRsp.tversion = ctgTestTVersion;
  metaRsp.suid = ctgTestSuid;
D
dapan1121 已提交
426
  metaRsp.tuid = ctgTestSuid++;
S
Shengliang Guan 已提交
427
  metaRsp.vgId = 0;
wafwerar's avatar
wafwerar 已提交
428
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc((metaRsp.numOfTags + metaRsp.numOfColumns) * sizeof(SSchema));
D
dapan1121 已提交
429 430

  SSchema *s = NULL;
S
Shengliang Guan 已提交
431
  s = &metaRsp.pSchemas[0];
D
dapan1121 已提交
432
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
S
Shengliang Guan 已提交
433 434
  s->colId = 1;
  s->bytes = 8;
D
dapan1121 已提交
435 436
  strcpy(s->name, "ts");

S
Shengliang Guan 已提交
437
  s = &metaRsp.pSchemas[1];
D
dapan1121 已提交
438
  s->type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
439 440
  s->colId = 2;
  s->bytes = 4;
D
dapan1121 已提交
441 442
  strcpy(s->name, "col1s");

S
Shengliang Guan 已提交
443
  s = &metaRsp.pSchemas[2];
D
dapan1121 已提交
444
  s->type = TSDB_DATA_TYPE_BINARY;
S
Shengliang Guan 已提交
445 446
  s->colId = 3;
  s->bytes = 12;
D
dapan1121 已提交
447 448
  strcpy(s->name, "tag1s");

S
Shengliang Guan 已提交
449 450 451 452 453 454 455 456 457
  int32_t contLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  void   *pReq = rpcMallocCont(contLen);
  tSerializeSTableMetaRsp(pReq, contLen, &metaRsp);

  pRsp->code = 0;
  pRsp->contLen = contLen;
  pRsp->pCont = pReq;

  tFreeSTableMetaRsp(&metaRsp);
D
dapan1121 已提交
458 459
}

D
dapan 已提交
460
void ctgTestRspMultiSTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
D
dapan1121 已提交
461 462
  static int32_t idx = 1;

S
Shengliang Guan 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475
  STableMetaRsp metaRsp = {0};
  strcpy(metaRsp.dbFName, ctgTestDbname);
  sprintf(metaRsp.tbName, "%s_%d", ctgTestSTablename, idx);
  sprintf(metaRsp.stbName, "%s_%d", ctgTestSTablename, idx);
  metaRsp.numOfTags = ctgTestTagNum;
  metaRsp.numOfColumns = ctgTestColNum;
  metaRsp.precision = 1;
  metaRsp.tableType = TSDB_SUPER_TABLE;
  metaRsp.sversion = ctgTestSVersion;
  metaRsp.tversion = ctgTestTVersion;
  metaRsp.suid = ctgTestSuid + idx;
  metaRsp.tuid = ctgTestSuid + idx;
  metaRsp.vgId = 0;
wafwerar's avatar
wafwerar 已提交
476
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc((metaRsp.numOfTags + metaRsp.numOfColumns) * sizeof(SSchema));
D
dapan1121 已提交
477 478

  SSchema *s = NULL;
S
Shengliang Guan 已提交
479
  s = &metaRsp.pSchemas[0];
D
dapan1121 已提交
480
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
S
Shengliang Guan 已提交
481 482
  s->colId = 1;
  s->bytes = 8;
D
dapan1121 已提交
483 484
  strcpy(s->name, "ts");

S
Shengliang Guan 已提交
485
  s = &metaRsp.pSchemas[1];
D
dapan1121 已提交
486
  s->type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
487 488
  s->colId = 2;
  s->bytes = 4;
D
dapan1121 已提交
489 490
  strcpy(s->name, "col1s");

S
Shengliang Guan 已提交
491
  s = &metaRsp.pSchemas[2];
D
dapan1121 已提交
492
  s->type = TSDB_DATA_TYPE_BINARY;
S
Shengliang Guan 已提交
493 494
  s->colId = 3;
  s->bytes = 12;
D
dapan1121 已提交
495 496 497
  strcpy(s->name, "tag1s");

  ++idx;
S
Shengliang Guan 已提交
498

S
Shengliang Guan 已提交
499 500 501 502 503 504 505
  int32_t contLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  void   *pReq = rpcMallocCont(contLen);
  tSerializeSTableMetaRsp(pReq, contLen, &metaRsp);

  pRsp->code = 0;
  pRsp->contLen = contLen;
  pRsp->pCont = pReq;
D
dapan1121 已提交
506

S
Shengliang Guan 已提交
507 508
  tFreeSTableMetaRsp(&metaRsp);
}
S
Shengliang Guan 已提交
509

D
dapan 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
void ctgTestRspByIdx(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  switch (ctgTestRspFunc[ctgTestRspIdx]) {
    case CTGT_RSP_VGINFO:
      ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);
      break;
    case CTGT_RSP_TBMETA:
      ctgTestRspTableMeta(shandle, pEpSet, pMsg, pRsp);
      break;
    case CTGT_RSP_CTBMETA:
      ctgTestRspCTableMeta(shandle, pEpSet, pMsg, pRsp);
      break;
    case CTGT_RSP_STBMETA:
      ctgTestRspSTableMeta(shandle, pEpSet, pMsg, pRsp);
      break;
    case CTGT_RSP_MSTBMETA:
      ctgTestRspMultiSTableMeta(shandle, pEpSet, pMsg, pRsp);
      break;
D
dapan1121 已提交
527 528 529
    case CTGT_RSP_TBMETA_NOT_EXIST:
      ctgTestRspTableMetaNotExist(shandle, pEpSet, pMsg, pRsp);
      break;
D
dapan 已提交
530 531 532 533 534
    default:
      break;
  }

  ctgTestRspIdx++;
S
Shengliang Guan 已提交
535

D
dapan 已提交
536 537 538
  return;
}

D
dapan 已提交
539 540 541 542
void ctgTestRspDbVgroupsAndNormalMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);

  ctgTestSetRspTableMeta();
S
Shengliang Guan 已提交
543

D
dapan 已提交
544 545 546
  return;
}

D
dapan 已提交
547 548
void ctgTestRspDbVgroupsAndChildMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);
S
Shengliang Guan 已提交
549

D
dapan 已提交
550
  ctgTestSetRspCTableMeta();
S
Shengliang Guan 已提交
551

D
dapan 已提交
552 553 554
  return;
}

D
dapan 已提交
555 556
void ctgTestRspDbVgroupsAndSuperMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);
S
Shengliang Guan 已提交
557

D
dapan 已提交
558
  ctgTestSetRspSTableMeta();
S
Shengliang Guan 已提交
559

D
dapan1121 已提交
560 561 562
  return;
}

D
dapan 已提交
563 564
void ctgTestRspDbVgroupsAndMultiSuperMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);
S
Shengliang Guan 已提交
565

D
dapan 已提交
566
  ctgTestSetRspMultiSTableMeta();
S
Shengliang Guan 已提交
567

D
dapan1121 已提交
568 569 570
  return;
}

D
dapan 已提交
571
void ctgTestSetRspDbVgroups() {
D
dapan1121 已提交
572
  static Stub stub;
D
dapan 已提交
573
  stub.set(rpcSendRecv, ctgTestRspDbVgroups);
D
dapan1121 已提交
574
  {
wafwerar's avatar
wafwerar 已提交
575
#ifdef WINDOWS
H
Hongze Cheng 已提交
576 577
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
578 579 580
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
581 582
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
583
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
584
#endif
S
Shengliang Guan 已提交
585
    for (const auto &f : result) {
D
dapan 已提交
586
      stub.set(f.second, ctgTestRspDbVgroups);
D
dapan1121 已提交
587 588 589 590
    }
  }
}

D
dapan 已提交
591
void ctgTestSetRspTableMeta() {
D
dapan1121 已提交
592
  static Stub stub;
D
dapan 已提交
593
  stub.set(rpcSendRecv, ctgTestRspTableMeta);
D
dapan1121 已提交
594
  {
wafwerar's avatar
wafwerar 已提交
595
#ifdef WINDOWS
H
Hongze Cheng 已提交
596 597
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
598 599 600
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
601 602
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
603
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
604
#endif
S
Shengliang Guan 已提交
605
    for (const auto &f : result) {
D
dapan 已提交
606
      stub.set(f.second, ctgTestRspTableMeta);
D
dapan1121 已提交
607 608 609 610
    }
  }
}

D
dapan 已提交
611
void ctgTestSetRspCTableMeta() {
D
dapan1121 已提交
612
  static Stub stub;
D
dapan 已提交
613
  stub.set(rpcSendRecv, ctgTestRspCTableMeta);
D
dapan1121 已提交
614
  {
wafwerar's avatar
wafwerar 已提交
615
#ifdef WINDOWS
H
Hongze Cheng 已提交
616 617
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
618 619 620
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
621 622
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
623
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
624
#endif
S
Shengliang Guan 已提交
625
    for (const auto &f : result) {
D
dapan 已提交
626
      stub.set(f.second, ctgTestRspCTableMeta);
D
dapan1121 已提交
627 628 629 630
    }
  }
}

D
dapan 已提交
631
void ctgTestSetRspSTableMeta() {
D
dapan1121 已提交
632
  static Stub stub;
D
dapan 已提交
633
  stub.set(rpcSendRecv, ctgTestRspSTableMeta);
D
dapan1121 已提交
634
  {
wafwerar's avatar
wafwerar 已提交
635
#ifdef WINDOWS
H
Hongze Cheng 已提交
636 637
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
638 639 640
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
641 642
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
643
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
644
#endif
S
Shengliang Guan 已提交
645
    for (const auto &f : result) {
D
dapan 已提交
646
      stub.set(f.second, ctgTestRspSTableMeta);
D
dapan1121 已提交
647 648 649 650
    }
  }
}

D
dapan 已提交
651
void ctgTestSetRspMultiSTableMeta() {
D
dapan1121 已提交
652
  static Stub stub;
D
dapan 已提交
653
  stub.set(rpcSendRecv, ctgTestRspMultiSTableMeta);
D
dapan1121 已提交
654
  {
wafwerar's avatar
wafwerar 已提交
655
#ifdef WINDOWS
H
Hongze Cheng 已提交
656 657
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
658 659 660
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
661 662
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
663
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
664
#endif
S
Shengliang Guan 已提交
665
    for (const auto &f : result) {
D
dapan 已提交
666
      stub.set(f.second, ctgTestRspMultiSTableMeta);
D
dapan1121 已提交
667 668 669 670
    }
  }
}

D
dapan 已提交
671
void ctgTestSetRspByIdx() {
D
dapan1121 已提交
672
  static Stub stub;
D
dapan 已提交
673
  stub.set(rpcSendRecv, ctgTestRspByIdx);
D
dapan1121 已提交
674
  {
wafwerar's avatar
wafwerar 已提交
675
#ifdef WINDOWS
H
Hongze Cheng 已提交
676 677
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
678 679 680
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
681 682
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
683
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
684
#endif
S
Shengliang Guan 已提交
685
    for (const auto &f : result) {
D
dapan 已提交
686
      stub.set(f.second, ctgTestRspByIdx);
D
dapan1121 已提交
687 688 689 690
    }
  }
}

D
dapan 已提交
691
void ctgTestSetRspDbVgroupsAndNormalMeta() {
D
dapan 已提交
692
  static Stub stub;
D
dapan 已提交
693
  stub.set(rpcSendRecv, ctgTestRspDbVgroupsAndNormalMeta);
D
dapan 已提交
694
  {
wafwerar's avatar
wafwerar 已提交
695
#ifdef WINDOWS
H
Hongze Cheng 已提交
696 697
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
698 699 700
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
701 702
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan 已提交
703
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
704
#endif
S
Shengliang Guan 已提交
705
    for (const auto &f : result) {
D
dapan 已提交
706
      stub.set(f.second, ctgTestRspDbVgroupsAndNormalMeta);
D
dapan 已提交
707 708 709 710
    }
  }
}

D
dapan 已提交
711
void ctgTestSetRspDbVgroupsAndChildMeta() {
D
dapan1121 已提交
712
  static Stub stub;
D
dapan 已提交
713
  stub.set(rpcSendRecv, ctgTestRspDbVgroupsAndChildMeta);
D
dapan1121 已提交
714
  {
wafwerar's avatar
wafwerar 已提交
715
#ifdef WINDOWS
H
Hongze Cheng 已提交
716 717
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
718 719 720
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
721 722
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
723
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
724
#endif
S
Shengliang Guan 已提交
725
    for (const auto &f : result) {
D
dapan 已提交
726
      stub.set(f.second, ctgTestRspDbVgroupsAndChildMeta);
D
dapan1121 已提交
727 728 729 730
    }
  }
}

D
dapan 已提交
731
void ctgTestSetRspDbVgroupsAndSuperMeta() {
D
dapan1121 已提交
732
  static Stub stub;
D
dapan 已提交
733
  stub.set(rpcSendRecv, ctgTestRspDbVgroupsAndSuperMeta);
D
dapan1121 已提交
734
  {
wafwerar's avatar
wafwerar 已提交
735
#ifdef WINDOWS
H
Hongze Cheng 已提交
736 737
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
738 739 740
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
741 742
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
743
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
744
#endif
S
Shengliang Guan 已提交
745
    for (const auto &f : result) {
D
dapan 已提交
746
      stub.set(f.second, ctgTestRspDbVgroupsAndSuperMeta);
D
dapan1121 已提交
747 748 749 750
    }
  }
}

D
dapan 已提交
751
void ctgTestSetRspDbVgroupsAndMultiSuperMeta() {
D
dapan1121 已提交
752
  static Stub stub;
D
dapan 已提交
753
  stub.set(rpcSendRecv, ctgTestRspDbVgroupsAndMultiSuperMeta);
D
dapan1121 已提交
754
  {
wafwerar's avatar
wafwerar 已提交
755
#ifdef WINDOWS
H
Hongze Cheng 已提交
756 757
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
758 759 760
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
761 762
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
763
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
764
#endif
S
Shengliang Guan 已提交
765
    for (const auto &f : result) {
D
dapan 已提交
766
      stub.set(f.second, ctgTestRspDbVgroupsAndMultiSuperMeta);
D
dapan1121 已提交
767 768 769 770
    }
  }
}

S
Shengliang Guan 已提交
771
}  // namespace
772

D
dapan1121 已提交
773
void *ctgTestGetDbVgroupThread(void *param) {
H
Hongze Cheng 已提交
774 775
  struct SCatalog  *pCtg = (struct SCatalog *)param;
  int32_t           code = 0;
D
dapan1121 已提交
776
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
777 778
  SArray           *vgList = NULL;
  int32_t           n = 0;
S
Shengliang Guan 已提交
779

D
dapan1121 已提交
780
  while (!ctgTestStop) {
781
    code = catalogGetDBVgList(pCtg, mockPointer, ctgTestDbname, &vgList);
D
dapan1121 已提交
782 783 784 785 786 787 788 789 790
    if (code) {
      assert(0);
    }

    if (vgList) {
      taosArrayDestroy(vgList);
    }

    if (ctgTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
791
      taosUsleep(taosRand() % 5);
D
dapan1121 已提交
792
    }
D
dapan1121 已提交
793
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
794 795 796 797 798 799 800
      printf("Get:%d\n", n);
    }
  }

  return NULL;
}

D
dapan1121 已提交
801
void *ctgTestSetSameDbVgroupThread(void *param) {
S
Shengliang Guan 已提交
802 803
  struct SCatalog *pCtg = (struct SCatalog *)param;
  int32_t          code = 0;
D
dapan1121 已提交
804
  SDBVgInfo       *dbVgroup = NULL;
S
Shengliang Guan 已提交
805 806
  int32_t          n = 0;

D
dapan1121 已提交
807 808
  while (!ctgTestStop) {
    ctgTestBuildDBVgroup(&dbVgroup);
D
dapan1121 已提交
809
    code = catalogUpdateDBVgInfo(pCtg, ctgTestDbname, ctgTestDbId, dbVgroup);
D
dapan1121 已提交
810 811 812 813
    if (code) {
      assert(0);
    }

S
Shengliang Guan 已提交
814
    if (ctgTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
815
      taosUsleep(taosRand() % 5);
D
dapan1121 已提交
816
    }
D
dapan1121 已提交
817
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
818 819 820 821 822 823 824
      printf("Set:%d\n", n);
    }
  }

  return NULL;
}

D
dapan1121 已提交
825 826 827
void *ctgTestSetDiffDbVgroupThread(void *param) {
  struct SCatalog *pCtg = (struct SCatalog *)param;
  int32_t          code = 0;
H
Hongze Cheng 已提交
828
  SDBVgInfo       *dbVgroup = NULL;
D
dapan1121 已提交
829 830 831 832
  int32_t          n = 0;

  while (!ctgTestStop) {
    ctgTestBuildDBVgroup(&dbVgroup);
D
dapan1121 已提交
833
    code = catalogUpdateDBVgInfo(pCtg, ctgTestDbname, ctgTestDbId++, dbVgroup);
D
dapan1121 已提交
834 835 836 837 838
    if (code) {
      assert(0);
    }

    if (ctgTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
839
      taosUsleep(taosRand() % 5);
D
dapan1121 已提交
840 841 842 843 844 845 846 847 848
    }
    if (++n % ctgTestPrintNum == 0) {
      printf("Set:%d\n", n);
    }
  }

  return NULL;
}

D
dapan1121 已提交
849
void *ctgTestGetCtableMetaThread(void *param) {
S
Shengliang Guan 已提交
850 851 852 853
  struct SCatalog *pCtg = (struct SCatalog *)param;
  int32_t          code = 0;
  int32_t          n = 0;
  STableMeta      *tbMeta = NULL;
D
dapan1121 已提交
854
  bool             inCache = false;
D
dapan1121 已提交
855

H
Hongze Cheng 已提交
856
  SName cn = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
857 858
  strcpy(cn.dbname, "db1");
  strcpy(cn.tname, ctgTestCTablename);
S
Shengliang Guan 已提交
859

D
dapan1121 已提交
860 861 862 863
  SCtgTbMetaCtx ctx = {0};
  ctx.pName = &cn;
  ctx.flag = CTG_FLAG_UNKNOWN_STB;

D
dapan1121 已提交
864
  while (!ctgTestStop) {
D
dapan1121 已提交
865
    code = ctgReadTbMetaFromCache(pCtg, &ctx, &tbMeta);
D
dapan1121 已提交
866
    if (code || !inCache) {
D
dapan1121 已提交
867 868 869
      assert(0);
    }

wafwerar's avatar
wafwerar 已提交
870
    taosMemoryFreeClear(tbMeta);
D
dapan1121 已提交
871

D
dapan1121 已提交
872
    if (ctgTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
873
      taosUsleep(taosRand() % 5);
D
dapan1121 已提交
874
    }
S
Shengliang Guan 已提交
875

D
dapan1121 已提交
876
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
877 878 879 880 881 882 883 884
      printf("Get:%d\n", n);
    }
  }

  return NULL;
}

void *ctgTestSetCtableMetaThread(void *param) {
H
Hongze Cheng 已提交
885 886 887 888
  struct SCatalog  *pCtg = (struct SCatalog *)param;
  int32_t           code = 0;
  SDBVgInfo         dbVgroup = {0};
  int32_t           n = 0;
D
dapan 已提交
889
  STableMetaOutput *output = NULL;
D
dapan1121 已提交
890

D
dapan1121 已提交
891
  SCtgCacheOperation operation = {0};
H
Hongze Cheng 已提交
892

D
dapan1121 已提交
893
  operation.opId = CTG_OP_UPDATE_TB_META;
S
Shengliang Guan 已提交
894

D
dapan1121 已提交
895
  while (!ctgTestStop) {
wafwerar's avatar
wafwerar 已提交
896
    output = (STableMetaOutput *)taosMemoryMalloc(sizeof(STableMetaOutput));
D
dapan 已提交
897 898
    ctgTestBuildCTableMetaOutput(output);

D
dapan1121 已提交
899
    SCtgUpdateTbMetaMsg *msg = (SCtgUpdateTbMetaMsg *)taosMemoryMalloc(sizeof(SCtgUpdateTbMetaMsg));
D
dapan1121 已提交
900
    msg->pCtg = pCtg;
D
dapan1121 已提交
901
    msg->pMeta = output;
D
dapan1121 已提交
902
    operation.data = msg;
D
dapan1121 已提交
903

D
dapan1121 已提交
904
    code = ctgOpUpdateTbMeta(&operation);
D
dapan1121 已提交
905 906 907 908
    if (code) {
      assert(0);
    }

S
Shengliang Guan 已提交
909
    if (ctgTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
910
      taosUsleep(taosRand() % 5);
D
dapan1121 已提交
911
    }
D
dapan1121 已提交
912
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
913 914 915 916 917 918 919
      printf("Set:%d\n", n);
    }
  }

  return NULL;
}

D
dapan1121 已提交
920
#if 1
D
dapan 已提交
921

D
dapan 已提交
922
TEST(tableMeta, normalTable) {
H
Hongze Cheng 已提交
923
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
924
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
925
  SVgroupInfo       vgInfo = {0};
926

D
dapan1121 已提交
927 928
  ctgTestInitLogFile();

D
dapan 已提交
929
  ctgTestSetRspDbVgroups();
D
dapan1121 已提交
930

931
  initQueryModuleMsgHandle();
D
ut test  
dapan1121 已提交
932

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

D
dapan1121 已提交
935 936
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);
937

D
dapan1121 已提交
938
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan1121 已提交
939
  ASSERT_EQ(code, 0);
940

H
Hongze Cheng 已提交
941
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
942 943 944
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

D
dapan1121 已提交
945
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
946
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
947
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
948
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
949

D
dapan1121 已提交
950
  while (0 == ctgdGetClusterCacheNum(pCtg, CTG_DBG_DB_NUM)) {
wafwerar's avatar
wafwerar 已提交
951
    taosMsleep(50);
D
dapan 已提交
952
  }
H
Hongze Cheng 已提交
953

D
dapan 已提交
954
  ctgTestSetRspTableMeta();
D
dapan1121 已提交
955 956

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
957
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
958 959 960
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 8);
  ASSERT_EQ(tableMeta->tableType, TSDB_NORMAL_TABLE);
D
dapan1121 已提交
961
  ASSERT_EQ(tableMeta->uid, ctgTestNormalTblUid - 1);
D
dapan1121 已提交
962 963 964 965 966 967 968
  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
dapan 已提交
969
  while (true) {
D
dapan1121 已提交
970
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
971
    if (0 == n) {
wafwerar's avatar
wafwerar 已提交
972
      taosMsleep(50);
D
dapan 已提交
973 974 975
    } else {
      break;
    }
D
dapan 已提交
976 977
  }

D
dapan1121 已提交
978
  tableMeta = NULL;
D
dapan1121 已提交
979
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
980 981 982 983 984 985 986 987 988 989
  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);

H
Hongze Cheng 已提交
990
  SDbVgVersion   *dbs = NULL;
D
dapan1121 已提交
991
  SSTableVersion *stb = NULL;
H
Hongze Cheng 已提交
992 993
  uint32_t        dbNum = 0, stbNum = 0, allDbNum = 0, allStbNum = 0;
  int32_t         i = 0;
D
dapan1121 已提交
994 995 996 997 998 999
  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 已提交
1000

D
dapan1121 已提交
1001
    if (dbNum) {
S
Shengliang Guan 已提交
1002
      printf("got expired db,dbId:%" PRId64 "\n", dbs->dbId);
wafwerar's avatar
wafwerar 已提交
1003
      taosMemoryFree(dbs);
D
dapan1121 已提交
1004 1005 1006 1007 1008 1009
      dbs = NULL;
    } else {
      printf("no expired db\n");
    }

    if (stbNum) {
D
dapan 已提交
1010
      printf("got expired stb,suid:%" PRId64 ",dbFName:%s, stbName:%s\n", stb->suid, stb->dbFName, stb->stbName);
wafwerar's avatar
wafwerar 已提交
1011
      taosMemoryFree(stb);
D
dapan1121 已提交
1012 1013 1014 1015 1016 1017 1018
      stb = NULL;
    } else {
      printf("no expired stb\n");
    }

    allDbNum += dbNum;
    allStbNum += stbNum;
wafwerar's avatar
wafwerar 已提交
1019
    taosSsleep(2);
D
dapan1121 已提交
1020
  }
S
Shengliang Guan 已提交
1021

D
dapan1121 已提交
1022 1023 1024
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 0);

D
dapan1121 已提交
1025
  catalogDestroy();
D
dapan 已提交
1026
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
D
dapan1121 已提交
1027 1028
}

D
dapan 已提交
1029
TEST(tableMeta, childTableCase) {
H
Hongze Cheng 已提交
1030
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1031
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
1032
  SVgroupInfo       vgInfo = {0};
D
dapan1121 已提交
1033

D
dapan1121 已提交
1034 1035
  ctgTestInitLogFile();

D
dapan 已提交
1036
  ctgTestSetRspDbVgroupsAndChildMeta();
D
dapan1121 已提交
1037 1038 1039

  initQueryModuleMsgHandle();

S
Shengliang Guan 已提交
1040
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
D
dapan1121 已提交
1041 1042
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);
S
Shengliang Guan 已提交
1043

D
dapan1121 已提交
1044
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan1121 已提交
1045 1046
  ASSERT_EQ(code, 0);

H
Hongze Cheng 已提交
1047
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
1048 1049
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestCTablename);
D
dapan1121 已提交
1050 1051

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1052
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
  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
dapan 已提交
1063
  while (true) {
D
dapan1121 已提交
1064
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
1065
    if (0 == n) {
wafwerar's avatar
wafwerar 已提交
1066
      taosMsleep(50);
D
dapan 已提交
1067 1068 1069
    } else {
      break;
    }
D
dapan 已提交
1070 1071
  }

D
dapan1121 已提交
1072
  tableMeta = NULL;
D
dapan1121 已提交
1073
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
  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);

wafwerar's avatar
wafwerar 已提交
1084
  taosMemoryFreeClear(tableMeta);
H
Haojun Liao 已提交
1085 1086

  strcpy(n.tname, ctgTestSTablename);
D
dapan1121 已提交
1087
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
  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);

H
Hongze Cheng 已提交
1098
  SDbVgVersion   *dbs = NULL;
D
dapan1121 已提交
1099
  SSTableVersion *stb = NULL;
H
Hongze Cheng 已提交
1100 1101
  uint32_t        dbNum = 0, stbNum = 0, allDbNum = 0, allStbNum = 0;
  int32_t         i = 0;
D
dapan1121 已提交
1102 1103 1104 1105 1106 1107
  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 已提交
1108

D
dapan1121 已提交
1109
    if (dbNum) {
S
Shengliang Guan 已提交
1110
      printf("got expired db,dbId:%" PRId64 "\n", dbs->dbId);
wafwerar's avatar
wafwerar 已提交
1111
      taosMemoryFree(dbs);
D
dapan1121 已提交
1112 1113 1114 1115 1116 1117
      dbs = NULL;
    } else {
      printf("no expired db\n");
    }

    if (stbNum) {
S
Shengliang Guan 已提交
1118
      printf("got expired stb,suid:%" PRId64 ",dbFName:%s, stbName:%s\n", stb->suid, stb->dbFName, stb->stbName);
wafwerar's avatar
wafwerar 已提交
1119
      taosMemoryFree(stb);
D
dapan1121 已提交
1120 1121 1122 1123 1124 1125 1126
      stb = NULL;
    } else {
      printf("no expired stb\n");
    }

    allDbNum += dbNum;
    allStbNum += stbNum;
wafwerar's avatar
wafwerar 已提交
1127
    taosSsleep(2);
D
dapan1121 已提交
1128
  }
S
Shengliang Guan 已提交
1129

D
dapan1121 已提交
1130 1131 1132
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 1);

D
dapan1121 已提交
1133
  catalogDestroy();
D
dapan 已提交
1134
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
D
dapan1121 已提交
1135 1136
}

D
dapan 已提交
1137
TEST(tableMeta, superTableCase) {
H
Hongze Cheng 已提交
1138
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1139
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
1140
  SVgroupInfo       vgInfo = {0};
D
dapan1121 已提交
1141

D
dapan 已提交
1142
  ctgTestSetRspDbVgroupsAndSuperMeta();
D
dapan1121 已提交
1143 1144 1145

  initQueryModuleMsgHandle();

D
dapan1121 已提交
1146 1147 1148
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

S
Shengliang Guan 已提交
1149
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
D
dapan1121 已提交
1150
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan1121 已提交
1151 1152
  ASSERT_EQ(code, 0);

H
Hongze Cheng 已提交
1153
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
1154 1155
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestSTablename);
D
dapan1121 已提交
1156 1157

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1158
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1159 1160 1161 1162 1163
  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 已提交
1164 1165
  ASSERT_EQ(tableMeta->uid, ctgTestSuid - 1);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid - 1);
D
dapan1121 已提交
1166 1167 1168 1169 1170
  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
dapan 已提交
1171
  while (true) {
D
dapan1121 已提交
1172
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
1173
    if (0 == n) {
wafwerar's avatar
wafwerar 已提交
1174
      taosMsleep(50);
D
dapan 已提交
1175 1176 1177
    } else {
      break;
    }
D
dapan 已提交
1178 1179 1180
  }

  ctgTestSetRspCTableMeta();
D
dapan1121 已提交
1181 1182

  tableMeta = NULL;
H
Haojun Liao 已提交
1183 1184 1185

  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestCTablename);
D
dapan1121 已提交
1186
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
  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
dapan 已提交
1197
  while (true) {
D
dapan1121 已提交
1198
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
1199
    if (2 != n) {
wafwerar's avatar
wafwerar 已提交
1200
      taosMsleep(50);
D
dapan 已提交
1201 1202 1203
    } else {
      break;
    }
D
dapan 已提交
1204 1205
  }

D
dapan1121 已提交
1206
  tableMeta = NULL;
D
dapan1121 已提交
1207
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
  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);

H
Hongze Cheng 已提交
1218
  SDbVgVersion   *dbs = NULL;
D
dapan1121 已提交
1219
  SSTableVersion *stb = NULL;
H
Hongze Cheng 已提交
1220 1221
  uint32_t        dbNum = 0, stbNum = 0, allDbNum = 0, allStbNum = 0;
  int32_t         i = 0;
D
dapan1121 已提交
1222 1223 1224 1225 1226 1227
  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 已提交
1228

D
dapan1121 已提交
1229
    if (dbNum) {
S
Shengliang Guan 已提交
1230
      printf("got expired db,dbId:%" PRId64 "\n", dbs->dbId);
wafwerar's avatar
wafwerar 已提交
1231
      taosMemoryFree(dbs);
D
dapan1121 已提交
1232 1233 1234 1235 1236 1237
      dbs = NULL;
    } else {
      printf("no expired db\n");
    }

    if (stbNum) {
D
dapan 已提交
1238
      printf("got expired stb,suid:%" PRId64 ",dbFName:%s, stbName:%s\n", stb->suid, stb->dbFName, stb->stbName);
S
Shengliang Guan 已提交
1239

wafwerar's avatar
wafwerar 已提交
1240
      taosMemoryFree(stb);
D
dapan1121 已提交
1241 1242 1243 1244 1245 1246 1247
      stb = NULL;
    } else {
      printf("no expired stb\n");
    }

    allDbNum += dbNum;
    allStbNum += stbNum;
wafwerar's avatar
wafwerar 已提交
1248
    taosSsleep(2);
D
dapan1121 已提交
1249
  }
S
Shengliang Guan 已提交
1250

D
dapan1121 已提交
1251 1252
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 1);
D
dapan1121 已提交
1253 1254

  catalogDestroy();
D
dapan 已提交
1255
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
D
dapan1121 已提交
1256
}
1257

D
dapan1121 已提交
1258
TEST(tableMeta, rmStbMeta) {
H
Hongze Cheng 已提交
1259
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1260
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
1261
  SVgroupInfo       vgInfo = {0};
D
dapan1121 已提交
1262 1263 1264

  ctgTestInitLogFile();

D
dapan 已提交
1265
  ctgTestSetRspDbVgroupsAndSuperMeta();
D
dapan1121 已提交
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275

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

H
Hongze Cheng 已提交
1276
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1277 1278 1279 1280
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestSTablename);

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1281
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1282 1283 1284 1285 1286
  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 已提交
1287 1288
  ASSERT_EQ(tableMeta->uid, ctgTestSuid - 1);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid - 1);
D
dapan1121 已提交
1289 1290 1291 1292 1293
  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
dapan 已提交
1294
  while (true) {
D
dapan1121 已提交
1295
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
1296
    if (0 == n) {
wafwerar's avatar
wafwerar 已提交
1297
      taosMsleep(50);
D
dapan 已提交
1298 1299 1300
    } else {
      break;
    }
D
dapan 已提交
1301 1302
  }

D
dapan1121 已提交
1303
  code = catalogRemoveStbMeta(pCtg, "1.db1", ctgTestDbId, ctgTestSTablename, ctgTestSuid - 1);
D
dapan1121 已提交
1304 1305
  ASSERT_EQ(code, 0);

D
dapan 已提交
1306
  while (true) {
D
dapan1121 已提交
1307 1308
    int32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
    int32_t m = ctgdGetClusterCacheNum(pCtg, CTG_DBG_STB_RENT_NUM);
D
dapan 已提交
1309
    if (n || m) {
wafwerar's avatar
wafwerar 已提交
1310
      taosMsleep(50);
D
dapan 已提交
1311 1312 1313
    } else {
      break;
    }
D
dapan 已提交
1314 1315
  }

D
dapan1121 已提交
1316 1317 1318 1319 1320
  ASSERT_EQ(ctgdGetClusterCacheNum(pCtg, CTG_DBG_DB_NUM), 1);
  ASSERT_EQ(ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM), 0);
  ASSERT_EQ(ctgdGetClusterCacheNum(pCtg, CTG_DBG_STB_NUM), 0);
  ASSERT_EQ(ctgdGetClusterCacheNum(pCtg, CTG_DBG_DB_RENT_NUM), 1);
  ASSERT_EQ(ctgdGetClusterCacheNum(pCtg, CTG_DBG_STB_RENT_NUM), 0);
S
Shengliang Guan 已提交
1321

D
dapan1121 已提交
1322
  catalogDestroy();
D
dapan 已提交
1323
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
D
dapan1121 已提交
1324 1325 1326
}

TEST(tableMeta, updateStbMeta) {
H
Hongze Cheng 已提交
1327
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1328
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
1329
  SVgroupInfo       vgInfo = {0};
D
dapan1121 已提交
1330 1331 1332

  ctgTestInitLogFile();

D
dapan 已提交
1333
  ctgTestSetRspDbVgroupsAndSuperMeta();
D
dapan1121 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343

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

H
Hongze Cheng 已提交
1344
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1345 1346 1347 1348
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestSTablename);

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1349
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1350 1351 1352 1353 1354
  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 已提交
1355 1356
  ASSERT_EQ(tableMeta->uid, ctgTestSuid - 1);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid - 1);
D
dapan1121 已提交
1357 1358 1359 1360 1361
  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
dapan 已提交
1362
  while (true) {
D
dapan1121 已提交
1363
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
1364
    if (0 == n) {
wafwerar's avatar
wafwerar 已提交
1365
      taosMsleep(50);
D
dapan 已提交
1366 1367 1368
    } else {
      break;
    }
D
dapan 已提交
1369 1370
  }

wafwerar's avatar
wafwerar 已提交
1371
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1372 1373 1374 1375

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

D
dapan1121 已提交
1376
  code = catalogUpdateTableMeta(pCtg, &rsp);
D
dapan1121 已提交
1377
  ASSERT_EQ(code, 0);
wafwerar's avatar
wafwerar 已提交
1378
  taosMemoryFreeClear(rsp.pSchemas);
D
dapan1121 已提交
1379

D
dapan 已提交
1380 1381
  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
1382
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan 已提交
1383
    if (n != 3) {
wafwerar's avatar
wafwerar 已提交
1384
      taosMsleep(50);
D
dapan 已提交
1385 1386 1387 1388 1389
    } else {
      break;
    }
  }

D
dapan1121 已提交
1390 1391 1392 1393 1394
  ASSERT_EQ(ctgdGetClusterCacheNum(pCtg, CTG_DBG_DB_NUM), 1);
  ASSERT_EQ(ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM), 1);
  ASSERT_EQ(ctgdGetClusterCacheNum(pCtg, CTG_DBG_STB_NUM), 1);
  ASSERT_EQ(ctgdGetClusterCacheNum(pCtg, CTG_DBG_DB_RENT_NUM), 1);
  ASSERT_EQ(ctgdGetClusterCacheNum(pCtg, CTG_DBG_STB_RENT_NUM), 1);
D
dapan1121 已提交
1395

D
dapan1121 已提交
1396
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
  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);

wafwerar's avatar
wafwerar 已提交
1409
  taosMemoryFreeClear(tableMeta);
S
Shengliang Guan 已提交
1410

D
dapan1121 已提交
1411
  catalogDestroy();
D
dapan 已提交
1412
  memset(&gCtgMgmt.stat, 0, sizeof(gCtgMgmt.stat));
D
dapan1121 已提交
1413 1414
}

D
dapan1121 已提交
1415
TEST(refreshGetMeta, normal2normal) {
H
Hongze Cheng 已提交
1416
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1417
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
1418 1419
  SVgroupInfo       vgInfo = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
1420 1421 1422 1423 1424 1425 1426 1427

  ctgTestInitLogFile();

  memset(ctgTestRspFunc, 0, sizeof(ctgTestRspFunc));
  ctgTestRspIdx = 0;
  ctgTestRspFunc[0] = CTGT_RSP_VGINFO;
  ctgTestRspFunc[1] = CTGT_RSP_TBMETA;
  ctgTestRspFunc[2] = CTGT_RSP_TBMETA;
H
Hongze Cheng 已提交
1428

D
dapan1121 已提交
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
  ctgTestSetRspByIdx();

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

H
Hongze Cheng 已提交
1441
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1442 1443 1444
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

D
dapan1121 已提交
1445
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
1446 1447
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
1448
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
1449 1450 1451

  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
1452
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan1121 已提交
1453 1454 1455
    if (n > 0) {
      break;
    }
wafwerar's avatar
wafwerar 已提交
1456
    taosMsleep(50);
D
dapan1121 已提交
1457 1458 1459
  }

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1460
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1461 1462 1463
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 8);
  ASSERT_EQ(tableMeta->tableType, TSDB_NORMAL_TABLE);
H
Hongze Cheng 已提交
1464
  ASSERT_EQ(tableMeta->uid, ctgTestNormalTblUid - 1);
D
dapan1121 已提交
1465 1466 1467 1468 1469 1470
  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);
wafwerar's avatar
wafwerar 已提交
1471
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1472

D
dapan1121 已提交
1473
  while (0 == ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) {
wafwerar's avatar
wafwerar 已提交
1474
    taosMsleep(50);
D
dapan1121 已提交
1475 1476
  }

D
dapan1121 已提交
1477
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
1478 1479 1480
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 8);
  ASSERT_EQ(tableMeta->tableType, TSDB_NORMAL_TABLE);
H
Hongze Cheng 已提交
1481
  ASSERT_EQ(tableMeta->uid, ctgTestNormalTblUid - 1);
D
dapan1121 已提交
1482 1483 1484 1485 1486 1487
  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);
wafwerar's avatar
wafwerar 已提交
1488
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1489 1490 1491 1492 1493 1494

  catalogDestroy();
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
}

TEST(refreshGetMeta, normal2notexist) {
H
Hongze Cheng 已提交
1495
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1496
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
1497 1498
  SVgroupInfo       vgInfo = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
1499 1500 1501 1502 1503 1504 1505 1506

  ctgTestInitLogFile();

  memset(ctgTestRspFunc, 0, sizeof(ctgTestRspFunc));
  ctgTestRspIdx = 0;
  ctgTestRspFunc[0] = CTGT_RSP_VGINFO;
  ctgTestRspFunc[1] = CTGT_RSP_TBMETA;
  ctgTestRspFunc[2] = CTGT_RSP_TBMETA_NOT_EXIST;
H
Hongze Cheng 已提交
1507

D
dapan1121 已提交
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
  ctgTestSetRspByIdx();

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

H
Hongze Cheng 已提交
1520
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1521 1522 1523
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

D
dapan1121 已提交
1524
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
1525 1526
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
1527
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
1528 1529 1530

  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
1531
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan1121 已提交
1532 1533 1534
    if (n > 0) {
      break;
    }
wafwerar's avatar
wafwerar 已提交
1535
    taosMsleep(50);
D
dapan1121 已提交
1536 1537 1538
  }

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1539
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1540 1541 1542
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 8);
  ASSERT_EQ(tableMeta->tableType, TSDB_NORMAL_TABLE);
H
Hongze Cheng 已提交
1543
  ASSERT_EQ(tableMeta->uid, ctgTestNormalTblUid - 1);
D
dapan1121 已提交
1544 1545 1546 1547 1548 1549
  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);
wafwerar's avatar
wafwerar 已提交
1550
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1551

D
dapan1121 已提交
1552
  while (0 == ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) {
wafwerar's avatar
wafwerar 已提交
1553
    taosMsleep(50);
D
dapan1121 已提交
1554 1555
  }

D
dapan1121 已提交
1556
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
1557 1558 1559 1560 1561 1562 1563 1564
  ASSERT_EQ(code, CTG_ERR_CODE_TABLE_NOT_EXIST);
  ASSERT_TRUE(tableMeta == NULL);

  catalogDestroy();
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
}

TEST(refreshGetMeta, normal2child) {
H
Hongze Cheng 已提交
1565
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1566
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
1567 1568
  SVgroupInfo       vgInfo = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
1569 1570 1571 1572 1573 1574 1575 1576 1577

  ctgTestInitLogFile();

  memset(ctgTestRspFunc, 0, sizeof(ctgTestRspFunc));
  ctgTestRspIdx = 0;
  ctgTestRspFunc[0] = CTGT_RSP_VGINFO;
  ctgTestRspFunc[1] = CTGT_RSP_TBMETA;
  ctgTestRspFunc[2] = CTGT_RSP_CTBMETA;
  ctgTestRspFunc[3] = CTGT_RSP_STBMETA;
H
Hongze Cheng 已提交
1578

D
dapan1121 已提交
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
  ctgTestSetRspByIdx();

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

H
Hongze Cheng 已提交
1591
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1592 1593 1594 1595 1596
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);
  ctgTestCurrentCTableName = ctgTestTablename;
  ctgTestCurrentSTableName = ctgTestSTablename;

D
dapan1121 已提交
1597
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
1598 1599
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
1600
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
1601 1602 1603

  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
1604
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan1121 已提交
1605 1606 1607
    if (n > 0) {
      break;
    }
wafwerar's avatar
wafwerar 已提交
1608
    taosMsleep(50);
D
dapan1121 已提交
1609 1610 1611
  }

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1612
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1613 1614 1615
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 8);
  ASSERT_EQ(tableMeta->tableType, TSDB_NORMAL_TABLE);
H
Hongze Cheng 已提交
1616
  ASSERT_EQ(tableMeta->uid, ctgTestNormalTblUid - 1);
D
dapan1121 已提交
1617 1618 1619 1620 1621 1622
  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);
wafwerar's avatar
wafwerar 已提交
1623
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1624

D
dapan1121 已提交
1625
  while (0 == ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) {
wafwerar's avatar
wafwerar 已提交
1626
    taosMsleep(50);
D
dapan1121 已提交
1627 1628
  }

D
dapan1121 已提交
1629
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
1630 1631 1632 1633 1634 1635 1636 1637 1638
  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);
wafwerar's avatar
wafwerar 已提交
1639
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1640 1641 1642 1643 1644 1645 1646 1647

  catalogDestroy();
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
  ctgTestCurrentCTableName = NULL;
  ctgTestCurrentSTableName = NULL;
}

TEST(refreshGetMeta, stable2child) {
H
Hongze Cheng 已提交
1648
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1649
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
1650 1651
  SVgroupInfo       vgInfo = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661

  ctgTestInitLogFile();

  memset(ctgTestRspFunc, 0, sizeof(ctgTestRspFunc));
  ctgTestRspIdx = 0;
  ctgTestRspFunc[0] = CTGT_RSP_VGINFO;
  ctgTestRspFunc[1] = CTGT_RSP_STBMETA;
  ctgTestRspFunc[2] = CTGT_RSP_STBMETA;
  ctgTestRspFunc[3] = CTGT_RSP_CTBMETA;
  ctgTestRspFunc[4] = CTGT_RSP_STBMETA;
H
Hongze Cheng 已提交
1662

D
dapan1121 已提交
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
  ctgTestSetRspByIdx();

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

H
Hongze Cheng 已提交
1675
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1676 1677 1678 1679 1680
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);
  ctgTestCurrentSTableName = ctgTestTablename;
  ctgTestCurrentCTableName = ctgTestTablename;

D
dapan1121 已提交
1681
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
1682 1683
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
1684
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
1685 1686 1687

  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
1688
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan1121 已提交
1689 1690 1691
    if (n > 0) {
      break;
    }
wafwerar's avatar
wafwerar 已提交
1692
    taosMsleep(50);
D
dapan1121 已提交
1693 1694 1695
  }

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1696
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
  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 - 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);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);
wafwerar's avatar
wafwerar 已提交
1708
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1709

D
dapan1121 已提交
1710
  while (0 == ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) {
wafwerar's avatar
wafwerar 已提交
1711
    taosMsleep(50);
D
dapan1121 已提交
1712 1713 1714
  }

  ctgTestCurrentSTableName = ctgTestSTablename;
D
dapan1121 已提交
1715
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
1716 1717 1718 1719 1720 1721 1722 1723 1724
  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);
wafwerar's avatar
wafwerar 已提交
1725
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1726 1727 1728 1729 1730 1731 1732 1733

  catalogDestroy();
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
  ctgTestCurrentCTableName = NULL;
  ctgTestCurrentSTableName = NULL;
}

TEST(refreshGetMeta, stable2stable) {
H
Hongze Cheng 已提交
1734
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1735
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
1736 1737
  SVgroupInfo       vgInfo = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747

  ctgTestInitLogFile();

  memset(ctgTestRspFunc, 0, sizeof(ctgTestRspFunc));
  ctgTestRspIdx = 0;
  ctgTestRspFunc[0] = CTGT_RSP_VGINFO;
  ctgTestRspFunc[1] = CTGT_RSP_STBMETA;
  ctgTestRspFunc[2] = CTGT_RSP_STBMETA;
  ctgTestRspFunc[3] = CTGT_RSP_STBMETA;
  ctgTestRspFunc[4] = CTGT_RSP_STBMETA;
H
Hongze Cheng 已提交
1748

D
dapan1121 已提交
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
  ctgTestSetRspByIdx();

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

H
Hongze Cheng 已提交
1761
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1762 1763 1764 1765
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);
  ctgTestCurrentSTableName = ctgTestTablename;

D
dapan1121 已提交
1766
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
1767 1768
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
1769
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
1770 1771 1772

  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
1773
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan1121 已提交
1774 1775 1776
    if (n > 0) {
      break;
    }
wafwerar's avatar
wafwerar 已提交
1777
    taosMsleep(50);
D
dapan1121 已提交
1778 1779 1780
  }

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1781
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
  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 - 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);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);
wafwerar's avatar
wafwerar 已提交
1793
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1794

D
dapan1121 已提交
1795
  while (0 == ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) {
wafwerar's avatar
wafwerar 已提交
1796
    taosMsleep(50);
D
dapan1121 已提交
1797 1798
  }

D
dapan1121 已提交
1799
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
  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 - 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);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);
wafwerar's avatar
wafwerar 已提交
1811
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1812 1813 1814 1815 1816 1817 1818 1819

  catalogDestroy();
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
  ctgTestCurrentCTableName = NULL;
  ctgTestCurrentSTableName = NULL;
}

TEST(refreshGetMeta, child2stable) {
H
Hongze Cheng 已提交
1820
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1821
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
1822 1823
  SVgroupInfo       vgInfo = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833

  ctgTestInitLogFile();

  memset(ctgTestRspFunc, 0, sizeof(ctgTestRspFunc));
  ctgTestRspIdx = 0;
  ctgTestRspFunc[0] = CTGT_RSP_VGINFO;
  ctgTestRspFunc[1] = CTGT_RSP_CTBMETA;
  ctgTestRspFunc[2] = CTGT_RSP_STBMETA;
  ctgTestRspFunc[3] = CTGT_RSP_STBMETA;
  ctgTestRspFunc[4] = CTGT_RSP_STBMETA;
H
Hongze Cheng 已提交
1834

D
dapan1121 已提交
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
  ctgTestSetRspByIdx();

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

H
Hongze Cheng 已提交
1847
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1848 1849 1850 1851 1852
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);
  ctgTestCurrentCTableName = ctgTestTablename;
  ctgTestCurrentSTableName = ctgTestSTablename;

D
dapan1121 已提交
1853
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
1854 1855
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
1856
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
1857 1858 1859

  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
1860
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan1121 已提交
1861 1862 1863
    if (n > 0) {
      break;
    }
wafwerar's avatar
wafwerar 已提交
1864
    taosMsleep(50);
D
dapan1121 已提交
1865 1866 1867
  }

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1868
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1869 1870 1871 1872 1873 1874 1875 1876 1877
  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);
wafwerar's avatar
wafwerar 已提交
1878
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1879

D
dapan1121 已提交
1880
  while (2 != ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) {
wafwerar's avatar
wafwerar 已提交
1881
    taosMsleep(50);
D
dapan1121 已提交
1882 1883 1884
  }

  ctgTestCurrentSTableName = ctgTestTablename;
D
dapan1121 已提交
1885
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
  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 - 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);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);
wafwerar's avatar
wafwerar 已提交
1897
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1898 1899 1900 1901 1902 1903 1904

  catalogDestroy();
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
  ctgTestCurrentCTableName = NULL;
  ctgTestCurrentSTableName = NULL;
}

D
dapan 已提交
1905
TEST(tableDistVgroup, normalTable) {
H
Hongze Cheng 已提交
1906
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1907
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
1908 1909
  SVgroupInfo      *vgInfo = NULL;
  SArray           *vgList = NULL;
D
dapan 已提交
1910

D
dapan 已提交
1911 1912 1913 1914 1915 1916 1917
  ctgTestInitLogFile();

  memset(ctgTestRspFunc, 0, sizeof(ctgTestRspFunc));
  ctgTestRspIdx = 0;
  ctgTestRspFunc[0] = CTGT_RSP_VGINFO;
  ctgTestRspFunc[1] = CTGT_RSP_TBMETA;
  ctgTestRspFunc[2] = CTGT_RSP_VGINFO;
H
Hongze Cheng 已提交
1918

D
dapan 已提交
1919
  ctgTestSetRspByIdx();
D
dapan 已提交
1920 1921 1922

  initQueryModuleMsgHandle();

D
dapan1121 已提交
1923 1924 1925
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

D
dapan1121 已提交
1928
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan 已提交
1929 1930
  ASSERT_EQ(code, 0);

H
Hongze Cheng 已提交
1931
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
1932 1933
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);
D
dapan 已提交
1934

D
dapan1121 已提交
1935
  code = catalogGetTableDistVgInfo(pCtg, mockPointer, &n, &vgList);
D
dapan 已提交
1936 1937 1938 1939
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), 1);
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 0);
  ASSERT_EQ(vgInfo->vgId, 8);
L
Liu Jicong 已提交
1940
  ASSERT_EQ(vgInfo->epSet.numOfEps, 3);
D
dapan 已提交
1941 1942

  catalogDestroy();
D
dapan 已提交
1943
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
D
dapan 已提交
1944 1945 1946
}

TEST(tableDistVgroup, childTableCase) {
H
Hongze Cheng 已提交
1947
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1948
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
1949 1950
  SVgroupInfo      *vgInfo = NULL;
  SArray           *vgList = NULL;
D
dapan 已提交
1951

D
dapan 已提交
1952 1953 1954 1955 1956 1957 1958 1959
  ctgTestInitLogFile();

  memset(ctgTestRspFunc, 0, sizeof(ctgTestRspFunc));
  ctgTestRspIdx = 0;
  ctgTestRspFunc[0] = CTGT_RSP_VGINFO;
  ctgTestRspFunc[1] = CTGT_RSP_CTBMETA;
  ctgTestRspFunc[2] = CTGT_RSP_STBMETA;
  ctgTestRspFunc[3] = CTGT_RSP_VGINFO;
H
Hongze Cheng 已提交
1960

D
dapan 已提交
1961
  ctgTestSetRspByIdx();
D
dapan 已提交
1962 1963 1964

  initQueryModuleMsgHandle();

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

D
dapan 已提交
1967 1968 1969 1970 1971 1972
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

H
Hongze Cheng 已提交
1973
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
1974 1975 1976
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestCTablename);

D
dapan1121 已提交
1977
  code = catalogGetTableDistVgInfo(pCtg, mockPointer, &n, &vgList);
D
dapan 已提交
1978 1979 1980 1981
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), 1);
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 0);
  ASSERT_EQ(vgInfo->vgId, 9);
L
Liu Jicong 已提交
1982
  ASSERT_EQ(vgInfo->epSet.numOfEps, 4);
D
dapan 已提交
1983 1984

  catalogDestroy();
D
dapan 已提交
1985
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
D
dapan 已提交
1986 1987 1988
}

TEST(tableDistVgroup, superTableCase) {
H
Hongze Cheng 已提交
1989
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1990
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
1991 1992
  SVgroupInfo      *vgInfo = NULL;
  SArray           *vgList = NULL;
D
dapan 已提交
1993

D
dapan 已提交
1994 1995 1996 1997 1998 1999 2000 2001 2002
  ctgTestInitLogFile();

  memset(ctgTestRspFunc, 0, sizeof(ctgTestRspFunc));
  ctgTestRspIdx = 0;
  ctgTestRspFunc[0] = CTGT_RSP_VGINFO;
  ctgTestRspFunc[1] = CTGT_RSP_STBMETA;
  ctgTestRspFunc[2] = CTGT_RSP_STBMETA;
  ctgTestRspFunc[3] = CTGT_RSP_VGINFO;

H
Hongze Cheng 已提交
2003
  ctgTestSetRspByIdx();
D
dapan 已提交
2004 2005 2006

  initQueryModuleMsgHandle();

D
dapan1121 已提交
2007 2008 2009
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

S
Shengliang Guan 已提交
2010
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
D
dapan1121 已提交
2011
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan 已提交
2012 2013
  ASSERT_EQ(code, 0);

H
Hongze Cheng 已提交
2014
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
2015 2016
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestSTablename);
D
dapan 已提交
2017

D
dapan1121 已提交
2018
  code = catalogGetTableDistVgInfo(pCtg, mockPointer, &n, &vgList);
D
dapan 已提交
2019 2020 2021 2022
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), 10);
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 0);
  ASSERT_EQ(vgInfo->vgId, 1);
L
Liu Jicong 已提交
2023
  ASSERT_EQ(vgInfo->epSet.numOfEps, 1);
D
dapan 已提交
2024 2025
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 1);
  ASSERT_EQ(vgInfo->vgId, 2);
L
Liu Jicong 已提交
2026
  ASSERT_EQ(vgInfo->epSet.numOfEps, 2);
D
dapan 已提交
2027 2028
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 2);
  ASSERT_EQ(vgInfo->vgId, 3);
L
Liu Jicong 已提交
2029
  ASSERT_EQ(vgInfo->epSet.numOfEps, 3);
D
dapan 已提交
2030 2031

  catalogDestroy();
D
dapan 已提交
2032
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
D
dapan 已提交
2033 2034
}

D
dapan1121 已提交
2035
TEST(dbVgroup, getSetDbVgroupCase) {
H
Hongze Cheng 已提交
2036
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2037
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
2038 2039 2040 2041
  SVgroupInfo       vgInfo = {0};
  SVgroupInfo      *pvgInfo = NULL;
  SDBVgInfo        *dbVgroup = NULL;
  SArray           *vgList = NULL;
D
dapan1121 已提交
2042

D
dapan 已提交
2043 2044
  ctgTestInitLogFile();

D
dapan 已提交
2045 2046 2047 2048 2049 2050 2051
  memset(ctgTestRspFunc, 0, sizeof(ctgTestRspFunc));
  ctgTestRspIdx = 0;
  ctgTestRspFunc[0] = CTGT_RSP_VGINFO;
  ctgTestRspFunc[1] = CTGT_RSP_TBMETA;

  ctgTestSetRspByIdx();

D
dapan1121 已提交
2052 2053
  initQueryModuleMsgHandle();

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

D
dapan1121 已提交
2056 2057 2058 2059 2060 2061
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

H
Hongze Cheng 已提交
2062
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
2063 2064 2065
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

2066
  code = catalogGetDBVgList(pCtg, mockPointer, ctgTestDbname, &vgList);
D
dapan1121 已提交
2067 2068
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), ctgTestVgNum);
S
Shengliang Guan 已提交
2069

D
dapan1121 已提交
2070 2071
  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
2072
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan1121 已提交
2073 2074 2075
    if (n > 0) {
      break;
    }
wafwerar's avatar
wafwerar 已提交
2076
    taosMsleep(50);
D
dapan 已提交
2077 2078
  }

D
dapan1121 已提交
2079
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
2080 2081
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
2082
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
2083

D
dapan1121 已提交
2084
  code = catalogGetTableDistVgInfo(pCtg, mockPointer, &n, &vgList);
D
dapan1121 已提交
2085 2086 2087 2088
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), 1);
  pvgInfo = (SVgroupInfo *)taosArrayGet(vgList, 0);
  ASSERT_EQ(pvgInfo->vgId, 8);
L
Liu Jicong 已提交
2089
  ASSERT_EQ(pvgInfo->epSet.numOfEps, 3);
D
dapan1121 已提交
2090 2091 2092
  taosArrayDestroy(vgList);

  ctgTestBuildDBVgroup(&dbVgroup);
D
dapan1121 已提交
2093
  code = catalogUpdateDBVgInfo(pCtg, ctgTestDbname, ctgTestDbId, dbVgroup);
D
dapan1121 已提交
2094 2095
  ASSERT_EQ(code, 0);

D
dapan 已提交
2096 2097
  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
2098
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan 已提交
2099
    if (n != 3) {
wafwerar's avatar
wafwerar 已提交
2100
      taosMsleep(50);
D
dapan 已提交
2101 2102 2103 2104 2105
    } else {
      break;
    }
  }

D
dapan1121 已提交
2106
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
2107 2108
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 7);
L
Liu Jicong 已提交
2109
  ASSERT_EQ(vgInfo.epSet.numOfEps, 2);
D
dapan1121 已提交
2110

D
dapan1121 已提交
2111
  code = catalogGetTableDistVgInfo(pCtg, mockPointer, &n, &vgList);
D
dapan1121 已提交
2112 2113 2114 2115
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), 1);
  pvgInfo = (SVgroupInfo *)taosArrayGet(vgList, 0);
  ASSERT_EQ(pvgInfo->vgId, 8);
L
Liu Jicong 已提交
2116
  ASSERT_EQ(pvgInfo->epSet.numOfEps, 3);
D
dapan1121 已提交
2117
  taosArrayDestroy(vgList);
S
Shengliang Guan 已提交
2118

D
dapan1121 已提交
2119
  catalogDestroy();
D
dapan 已提交
2120
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
D
dapan1121 已提交
2121 2122
}

D
dapan1121 已提交
2123
TEST(multiThread, getSetRmSameDbVgroup) {
H
Hongze Cheng 已提交
2124
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2125
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
2126 2127 2128 2129
  SVgroupInfo       vgInfo = {0};
  SVgroupInfo      *pvgInfo = NULL;
  SDBVgInfo         dbVgroup = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
2130
  ctgTestStop = false;
D
dapan1121 已提交
2131 2132

  ctgTestInitLogFile();
S
Shengliang Guan 已提交
2133

D
dapan 已提交
2134
  ctgTestSetRspDbVgroups();
D
dapan1121 已提交
2135 2136 2137

  initQueryModuleMsgHandle();

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

D
dapan1121 已提交
2140 2141 2142 2143 2144 2145
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

H
Hongze Cheng 已提交
2146
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
2147 2148 2149
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

wafwerar's avatar
wafwerar 已提交
2150 2151
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
2152

wafwerar's avatar
wafwerar 已提交
2153 2154
  TdThread thread1, thread2;
  taosThreadCreate(&(thread1), &thattr, ctgTestSetSameDbVgroupThread, pCtg);
D
dapan1121 已提交
2155

wafwerar's avatar
wafwerar 已提交
2156
  taosSsleep(1);
wafwerar's avatar
wafwerar 已提交
2157
  taosThreadCreate(&(thread2), &thattr, ctgTestGetDbVgroupThread, pCtg);
D
dapan1121 已提交
2158 2159 2160

  while (true) {
    if (ctgTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
2161
      taosSsleep(1);
D
dapan1121 已提交
2162
    } else {
wafwerar's avatar
wafwerar 已提交
2163
      taosSsleep(ctgTestMTRunSec);
D
dapan1121 已提交
2164 2165 2166
      break;
    }
  }
S
Shengliang Guan 已提交
2167

D
dapan1121 已提交
2168
  ctgTestStop = true;
wafwerar's avatar
wafwerar 已提交
2169
  taosSsleep(1);
S
Shengliang Guan 已提交
2170

D
dapan1121 已提交
2171
  catalogDestroy();
D
dapan 已提交
2172
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
D
dapan1121 已提交
2173 2174
}

D
dapan1121 已提交
2175
TEST(multiThread, getSetRmDiffDbVgroup) {
H
Hongze Cheng 已提交
2176
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2177
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
2178 2179 2180 2181
  SVgroupInfo       vgInfo = {0};
  SVgroupInfo      *pvgInfo = NULL;
  SDBVgInfo         dbVgroup = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
2182 2183 2184 2185
  ctgTestStop = false;

  ctgTestInitLogFile();

D
dapan 已提交
2186
  ctgTestSetRspDbVgroups();
D
dapan1121 已提交
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197

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

H
Hongze Cheng 已提交
2198
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
2199 2200 2201
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

wafwerar's avatar
wafwerar 已提交
2202 2203
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
2204

wafwerar's avatar
wafwerar 已提交
2205 2206
  TdThread thread1, thread2;
  taosThreadCreate(&(thread1), &thattr, ctgTestSetDiffDbVgroupThread, pCtg);
D
dapan1121 已提交
2207

wafwerar's avatar
wafwerar 已提交
2208
  taosSsleep(1);
wafwerar's avatar
wafwerar 已提交
2209
  taosThreadCreate(&(thread2), &thattr, ctgTestGetDbVgroupThread, pCtg);
D
dapan1121 已提交
2210 2211 2212

  while (true) {
    if (ctgTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
2213
      taosSsleep(1);
D
dapan1121 已提交
2214
    } else {
wafwerar's avatar
wafwerar 已提交
2215
      taosSsleep(ctgTestMTRunSec);
D
dapan1121 已提交
2216 2217 2218 2219 2220
      break;
    }
  }

  ctgTestStop = true;
wafwerar's avatar
wafwerar 已提交
2221
  taosSsleep(1);
D
dapan1121 已提交
2222 2223

  catalogDestroy();
D
dapan 已提交
2224
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
D
dapan1121 已提交
2225 2226
}

D
dapan1121 已提交
2227
TEST(multiThread, ctableMeta) {
H
Hongze Cheng 已提交
2228
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2229
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
2230 2231 2232 2233
  SVgroupInfo       vgInfo = {0};
  SVgroupInfo      *pvgInfo = NULL;
  SDBVgInfo         dbVgroup = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
2234
  ctgTestStop = false;
D
dapan1121 已提交
2235

D
dapan 已提交
2236 2237
  ctgTestInitLogFile();

D
dapan 已提交
2238
  ctgTestSetRspDbVgroupsAndChildMeta();
D
dapan1121 已提交
2239 2240 2241

  initQueryModuleMsgHandle();

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

D
dapan1121 已提交
2244 2245 2246 2247 2248 2249
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

H
Hongze Cheng 已提交
2250
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
2251 2252 2253
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

wafwerar's avatar
wafwerar 已提交
2254 2255
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
2256

wafwerar's avatar
wafwerar 已提交
2257 2258
  TdThread thread1, thread2;
  taosThreadCreate(&(thread1), &thattr, ctgTestSetCtableMetaThread, pCtg);
wafwerar's avatar
wafwerar 已提交
2259
  taosSsleep(1);
wafwerar's avatar
wafwerar 已提交
2260
  taosThreadCreate(&(thread1), &thattr, ctgTestGetCtableMetaThread, pCtg);
D
dapan1121 已提交
2261 2262 2263

  while (true) {
    if (ctgTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
2264
      taosSsleep(1);
D
dapan1121 已提交
2265
    } else {
wafwerar's avatar
wafwerar 已提交
2266
      taosSsleep(ctgTestMTRunSec);
D
dapan1121 已提交
2267 2268 2269
      break;
    }
  }
S
Shengliang Guan 已提交
2270

D
dapan1121 已提交
2271
  ctgTestStop = true;
wafwerar's avatar
wafwerar 已提交
2272
  taosSsleep(2);
S
Shengliang Guan 已提交
2273

D
dapan1121 已提交
2274
  catalogDestroy();
D
dapan 已提交
2275
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
D
dapan1121 已提交
2276
}
D
dapan 已提交
2277

D
dapan1121 已提交
2278
TEST(rentTest, allRent) {
H
Hongze Cheng 已提交
2279
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2280
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)0x1;
H
Hongze Cheng 已提交
2281 2282 2283 2284
  SVgroupInfo       vgInfo = {0};
  SVgroupInfo      *pvgInfo = NULL;
  SDBVgInfo         dbVgroup = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
2285
  ctgTestStop = false;
H
Hongze Cheng 已提交
2286
  SDbVgVersion   *dbs = NULL;
D
dapan1121 已提交
2287
  SSTableVersion *stable = NULL;
H
Hongze Cheng 已提交
2288
  uint32_t        num = 0;
D
dapan1121 已提交
2289

D
dapan 已提交
2290 2291
  ctgTestInitLogFile();

D
dapan 已提交
2292
  ctgTestSetRspDbVgroupsAndMultiSuperMeta();
D
dapan1121 已提交
2293 2294 2295 2296 2297 2298 2299 2300 2301

  initQueryModuleMsgHandle();

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

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

H
Hongze Cheng 已提交
2302
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
2303 2304 2305 2306 2307 2308
  strcpy(n.dbname, "db1");

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

    STableMeta *tableMeta = NULL;
D
dapan1121 已提交
2309
    code = catalogGetSTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320
    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 已提交
2321

D
dapan1121 已提交
2322
    while (ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM) < i) {
wafwerar's avatar
wafwerar 已提交
2323
      taosMsleep(50);
D
dapan 已提交
2324 2325
    }

D
dapan1121 已提交
2326 2327 2328 2329
    code = catalogGetExpiredDBs(pCtg, &dbs, &num);
    ASSERT_EQ(code, 0);
    printf("%d - expired dbNum:%d\n", i, num);
    if (dbs) {
S
Shengliang Guan 已提交
2330
      printf("%d - expired dbId:%" PRId64 ", vgVersion:%d\n", i, dbs->dbId, dbs->vgVersion);
wafwerar's avatar
wafwerar 已提交
2331
      taosMemoryFree(dbs);
D
dapan1121 已提交
2332 2333
      dbs = NULL;
    }
S
Shengliang Guan 已提交
2334

D
dapan1121 已提交
2335 2336 2337 2338 2339
    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) {
S
Shengliang Guan 已提交
2340 2341
        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 已提交
2342
      }
wafwerar's avatar
wafwerar 已提交
2343
      taosMemoryFree(stable);
D
dapan1121 已提交
2344 2345 2346
      stable = NULL;
    }
    printf("*************************************************\n");
S
Shengliang Guan 已提交
2347

wafwerar's avatar
wafwerar 已提交
2348
    taosSsleep(2);
D
dapan1121 已提交
2349
  }
S
Shengliang Guan 已提交
2350

D
dapan1121 已提交
2351
  catalogDestroy();
D
dapan 已提交
2352
  memset(&gCtgMgmt, 0, sizeof(gCtgMgmt));
D
dapan1121 已提交
2353 2354
}

D
dapan1121 已提交
2355 2356
#endif

S
Shengliang Guan 已提交
2357
int main(int argc, char **argv) {
2358 2359 2360
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
D
dapan1121 已提交
2361

D
dapan1121 已提交
2362
#pragma GCC diagnostic pop