catalogTests.cpp 74.4 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
H
Haojun Liao 已提交
30
#include "catalogInt.h"
31
#include "catalog.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 52 53 54 55 56 57
void ctgTestSetRspTableMeta();
void ctgTestSetRspCTableMeta();
void ctgTestSetRspSTableMeta();
void ctgTestSetRspMultiSTableMeta();

enum {
  CTGT_RSP_VGINFO = 1,
  CTGT_RSP_TBMETA,
  CTGT_RSP_CTBMETA,
  CTGT_RSP_STBMETA,
  CTGT_RSP_MSTBMETA,
D
dapan1121 已提交
58
  CTGT_RSP_INDEXINFO_E,
D
dapan1121 已提交
59 60 61
  CTGT_RSP_USERAUTH,
  CTGT_RSP_TBLCFG,
  CTGT_RSP_TBLINDEX,
D
dapan1121 已提交
62
  CTGT_RSP_TBMETA_NOT_EXIST,
D
dapan 已提交
63
};
D
dapan1121 已提交
64

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

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

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

D
dapan1121 已提交
94
int32_t ctgTestRspFunc[100] = {0};
D
dapan 已提交
95 96
int32_t ctgTestRspIdx = 0;

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

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

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

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

  ASSERT_EQ(rpcRsp.code, 0);
}

D
dapan1121 已提交
134
void ctgTestInitLogFile() {
D
dapan1121 已提交
135 136 137
  if (!ctgTestEnableLog) {
    return;
  }
H
Hongze Cheng 已提交
138

S
Shengliang Guan 已提交
139 140
  const char   *defaultLogFileNamePrefix = "taoslog";
  const int32_t maxLogFileNum = 10;
D
dapan1121 已提交
141 142

  tsAsyncLog = 0;
D
dapan1121 已提交
143
  qDebugFlag = 159;
wafwerar's avatar
wafwerar 已提交
144
  strcpy(tsLogDir, TD_LOG_DIR_PATH);
D
dapan1121 已提交
145

D
dapan1121 已提交
146 147 148
  ctgdEnableDebug("api");
  ctgdEnableDebug("meta");
  ctgdEnableDebug("cache");
D
dapan1121 已提交
149
  ctgdEnableDebug("lock");
H
Hongze Cheng 已提交
150

S
Shengliang Guan 已提交
151
  if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) {
D
dapan1121 已提交
152 153 154 155 156 157 158 159 160
    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 已提交
161
  SName cn = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
162 163 164
  strcpy(cn.dbname, "db1");
  strcpy(cn.tname, ctgTestCTablename);

H
Hongze Cheng 已提交
165
  SName sn = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
166 167 168
  strcpy(sn.dbname, "db1");
  strcpy(sn.tname, ctgTestSTablename);

D
dapan 已提交
169 170 171 172
  char db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(&cn, db);

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

D
dapan1121 已提交
175 176
  strcpy(output->ctbName, cn.tname);
  strcpy(output->tbName, sn.tname);
D
dapan1121 已提交
177 178 179 180 181 182

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

H
Hongze Cheng 已提交
183 184
  output->tbMeta =
      (STableMeta *)taosMemoryCalloc(1, sizeof(STableMeta) + sizeof(SSchema) * (ctgTestColNum + ctgTestColNum));
D
dapan1121 已提交
185 186 187 188 189 190 191
  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 已提交
192

D
dapan1121 已提交
193 194
  output->tbMeta->sversion = ctgTestSVersion;
  output->tbMeta->tversion = ctgTestTVersion;
S
Shengliang Guan 已提交
195

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

D
dapan1121 已提交
222
  dbVgroup->vgVersion = vgVersion++;
S
Shengliang Guan 已提交
223

D
dapan1121 已提交
224
  ctgTestCurrentVgVersion = dbVgroup->vgVersion;
S
Shengliang Guan 已提交
225

D
dapan1121 已提交
226
  dbVgroup->hashMethod = 0;
227 228
  dbVgroup->hashPrefix = 0;
  dbVgroup->hashSuffix = 0;
D
dapan1121 已提交
229
  dbVgroup->vgHash = taosHashInit(ctgTestVgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
D
dapan1121 已提交
230 231 232 233 234 235 236 237

  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 已提交
238 239 240 241
    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 已提交
242
      strcpy(addr->fqdn, "a0");
S
Shengliang Guan 已提交
243
      addr->port = n + 22;
D
dapan1121 已提交
244 245
    }

D
dapan1121 已提交
246
    taosHashPut(dbVgroup->vgHash, &vgInfo.vgId, sizeof(vgInfo.vgId), &vgInfo, sizeof(vgInfo));
D
dapan1121 已提交
247
  }
D
dapan 已提交
248 249

  *pdbVgroup = dbVgroup;
D
dapan1121 已提交
250 251
}

D
dapan1121 已提交
252 253 254 255 256 257 258 259 260 261
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;
D
dapan1121 已提交
262 263
  rspMsg->suid = ctgTestSuid;
  rspMsg->tuid = ctgTestSuid;
D
dapan1121 已提交
264
  rspMsg->vgId = 1;
H
Hongze Cheng 已提交
265

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

  SSchema *s = NULL;
S
Shengliang Guan 已提交
269
  s = &rspMsg->pSchemas[0];
D
dapan1121 已提交
270 271 272 273 274
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
  s->colId = 1;
  s->bytes = 8;
  strcpy(s->name, "ts");

S
Shengliang Guan 已提交
275
  s = &rspMsg->pSchemas[1];
D
dapan1121 已提交
276 277 278 279 280
  s->type = TSDB_DATA_TYPE_INT;
  s->colId = 2;
  s->bytes = 4;
  strcpy(s->name, "col1s");

S
Shengliang Guan 已提交
281
  s = &rspMsg->pSchemas[2];
D
dapan1121 已提交
282 283 284 285 286 287 288 289
  s->type = TSDB_DATA_TYPE_BINARY;
  s->colId = 3;
  s->bytes = 12 + 1;
  strcpy(s->name, "tag1s");

  return;
}

D
dapan 已提交
290
void ctgTestRspDbVgroups(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
291 292
  rpcFreeCont(pMsg->pCont);
  
S
Shengliang Guan 已提交
293 294 295
  SUseDbRsp usedbRsp = {0};
  strcpy(usedbRsp.db, ctgTestDbname);
  usedbRsp.vgVersion = ctgTestVgVersion;
D
dapan1121 已提交
296
  ctgTestCurrentVgVersion = ctgTestVgVersion;
S
Shengliang Guan 已提交
297 298 299 300
  usedbRsp.vgNum = ctgTestVgNum;
  usedbRsp.hashMethod = 0;
  usedbRsp.uid = ctgTestDbId;
  usedbRsp.pVgroupInfos = taosArrayInit(usedbRsp.vgNum, sizeof(SVgroupInfo));
D
dapan1121 已提交
301

S
Shengliang Guan 已提交
302
  uint32_t hashUnit = UINT32_MAX / ctgTestVgNum;
D
dapan1121 已提交
303
  for (int32_t i = 0; i < ctgTestVgNum; ++i) {
S
Shengliang Guan 已提交
304 305 306 307 308 309 310 311
    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 已提交
312 313 314 315
    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 已提交
316
      strcpy(addr->fqdn, "a0");
S
Shengliang Guan 已提交
317
      addr->port = n + 22;
D
dapan1121 已提交
318
    }
D
dapan1121 已提交
319
    vg.numOfTable = i % 2;
S
Shengliang Guan 已提交
320 321

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

S
Shengliang Guan 已提交
324 325 326
  int32_t contLen = tSerializeSUseDbRsp(NULL, 0, &usedbRsp);
  void   *pReq = rpcMallocCont(contLen);
  tSerializeSUseDbRsp(pReq, contLen, &usedbRsp);
D
dapan1121 已提交
327

S
Shengliang Guan 已提交
328 329 330
  pRsp->code = 0;
  pRsp->contLen = contLen;
  pRsp->pCont = pReq;
331 332

  taosArrayDestroy(usedbRsp.pVgroupInfos);
D
dapan1121 已提交
333 334
}

D
dapan 已提交
335
void ctgTestRspTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
336 337
  rpcFreeCont(pMsg->pCont);
  
S
Shengliang Guan 已提交
338 339 340 341 342 343 344 345 346 347
  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 已提交
348
  metaRsp.tuid = ctgTestNormalTblUid++;
S
Shengliang Guan 已提交
349
  metaRsp.vgId = 8;
wafwerar's avatar
wafwerar 已提交
350
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc((metaRsp.numOfTags + metaRsp.numOfColumns) * sizeof(SSchema));
D
dapan1121 已提交
351 352

  SSchema *s = NULL;
S
Shengliang Guan 已提交
353
  s = &metaRsp.pSchemas[0];
D
dapan1121 已提交
354
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
S
Shengliang Guan 已提交
355 356
  s->colId = 1;
  s->bytes = 8;
D
dapan1121 已提交
357 358
  strcpy(s->name, "ts");

S
Shengliang Guan 已提交
359
  s = &metaRsp.pSchemas[1];
D
dapan1121 已提交
360
  s->type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
361 362
  s->colId = 2;
  s->bytes = 4;
D
dapan1121 已提交
363
  strcpy(s->name, "col1");
S
Shengliang Guan 已提交
364

S
Shengliang Guan 已提交
365 366 367 368 369 370 371 372 373
  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 已提交
374 375
}

D
dapan1121 已提交
376
void ctgTestRspTableMetaNotExist(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
377 378
  rpcFreeCont(pMsg->pCont);
  
D
dapan1121 已提交
379 380 381
  pRsp->code = CTG_ERR_CODE_TABLE_NOT_EXIST;
}

D
dapan 已提交
382
void ctgTestRspCTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
383 384
  rpcFreeCont(pMsg->pCont);
  
S
Shengliang Guan 已提交
385 386
  STableMetaRsp metaRsp = {0};
  strcpy(metaRsp.dbFName, ctgTestDbname);
D
dapan1121 已提交
387
  strcpy(metaRsp.tbName, ctgTestCurrentCTableName ? ctgTestCurrentCTableName : ctgTestCTablename);
S
Shengliang Guan 已提交
388 389 390 391 392 393 394 395 396 397
  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 已提交
398
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc((metaRsp.numOfTags + metaRsp.numOfColumns) * sizeof(SSchema));
D
dapan1121 已提交
399 400

  SSchema *s = NULL;
S
Shengliang Guan 已提交
401
  s = &metaRsp.pSchemas[0];
D
dapan1121 已提交
402
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
S
Shengliang Guan 已提交
403 404
  s->colId = 1;
  s->bytes = 8;
D
dapan1121 已提交
405 406
  strcpy(s->name, "ts");

S
Shengliang Guan 已提交
407
  s = &metaRsp.pSchemas[1];
D
dapan1121 已提交
408
  s->type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
409 410
  s->colId = 2;
  s->bytes = 4;
D
dapan1121 已提交
411 412
  strcpy(s->name, "col1s");

S
Shengliang Guan 已提交
413
  s = &metaRsp.pSchemas[2];
D
dapan1121 已提交
414
  s->type = TSDB_DATA_TYPE_BINARY;
S
Shengliang Guan 已提交
415 416
  s->colId = 3;
  s->bytes = 12;
D
dapan1121 已提交
417 418
  strcpy(s->name, "tag1s");

S
Shengliang Guan 已提交
419 420 421 422 423 424 425 426 427
  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 已提交
428 429
}

D
dapan 已提交
430
void ctgTestRspSTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
431 432
  rpcFreeCont(pMsg->pCont);
  
S
Shengliang Guan 已提交
433 434
  STableMetaRsp metaRsp = {0};
  strcpy(metaRsp.dbFName, ctgTestDbname);
D
dapan1121 已提交
435
  strcpy(metaRsp.tbName, ctgTestCurrentSTableName ? ctgTestCurrentSTableName : ctgTestSTablename);
S
Shengliang Guan 已提交
436 437 438 439 440 441 442 443
  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 已提交
444
  metaRsp.tuid = ctgTestSuid+1;
S
Shengliang Guan 已提交
445
  metaRsp.vgId = 0;
wafwerar's avatar
wafwerar 已提交
446
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc((metaRsp.numOfTags + metaRsp.numOfColumns) * sizeof(SSchema));
D
dapan1121 已提交
447 448

  SSchema *s = NULL;
S
Shengliang Guan 已提交
449
  s = &metaRsp.pSchemas[0];
D
dapan1121 已提交
450
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
S
Shengliang Guan 已提交
451 452
  s->colId = 1;
  s->bytes = 8;
D
dapan1121 已提交
453 454
  strcpy(s->name, "ts");

S
Shengliang Guan 已提交
455
  s = &metaRsp.pSchemas[1];
D
dapan1121 已提交
456
  s->type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
457 458
  s->colId = 2;
  s->bytes = 4;
D
dapan1121 已提交
459 460
  strcpy(s->name, "col1s");

S
Shengliang Guan 已提交
461
  s = &metaRsp.pSchemas[2];
D
dapan1121 已提交
462
  s->type = TSDB_DATA_TYPE_BINARY;
S
Shengliang Guan 已提交
463 464
  s->colId = 3;
  s->bytes = 12;
D
dapan1121 已提交
465 466
  strcpy(s->name, "tag1s");

S
Shengliang Guan 已提交
467 468 469 470 471 472 473 474 475
  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 已提交
476 477
}

D
dapan 已提交
478
void ctgTestRspMultiSTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
479 480
  rpcFreeCont(pMsg->pCont);
  
D
dapan1121 已提交
481 482
  static int32_t idx = 1;

S
Shengliang Guan 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495
  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 已提交
496
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc((metaRsp.numOfTags + metaRsp.numOfColumns) * sizeof(SSchema));
D
dapan1121 已提交
497 498

  SSchema *s = NULL;
S
Shengliang Guan 已提交
499
  s = &metaRsp.pSchemas[0];
D
dapan1121 已提交
500
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
S
Shengliang Guan 已提交
501 502
  s->colId = 1;
  s->bytes = 8;
D
dapan1121 已提交
503 504
  strcpy(s->name, "ts");

S
Shengliang Guan 已提交
505
  s = &metaRsp.pSchemas[1];
D
dapan1121 已提交
506
  s->type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
507 508
  s->colId = 2;
  s->bytes = 4;
D
dapan1121 已提交
509 510
  strcpy(s->name, "col1s");

S
Shengliang Guan 已提交
511
  s = &metaRsp.pSchemas[2];
D
dapan1121 已提交
512
  s->type = TSDB_DATA_TYPE_BINARY;
S
Shengliang Guan 已提交
513 514
  s->colId = 3;
  s->bytes = 12;
D
dapan1121 已提交
515 516 517
  strcpy(s->name, "tag1s");

  ++idx;
S
Shengliang Guan 已提交
518

S
Shengliang Guan 已提交
519 520 521 522 523 524 525
  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 已提交
526

S
Shengliang Guan 已提交
527 528
  tFreeSTableMetaRsp(&metaRsp);
}
S
Shengliang Guan 已提交
529

D
dapan1121 已提交
530 531

void ctgTestRspErrIndexInfo(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
532 533
  rpcFreeCont(pMsg->pCont);
  
D
dapan1121 已提交
534 535 536 537 538 539
  pRsp->code = TSDB_CODE_MND_DB_INDEX_NOT_EXIST;
  pRsp->contLen = 0;
  pRsp->pCont = NULL;
}


D
dapan1121 已提交
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
void ctgTestRspUserAuth(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  rpcFreeCont(pMsg->pCont);

  SGetUserAuthRsp userRsp = {0};
  strcpy(userRsp.user, ctgTestUsername);
  userRsp.version = 1;
  userRsp.superAuth = 1;

  int32_t contLen = tSerializeSGetUserAuthRsp(NULL, 0, &userRsp);
  void   *pReq = rpcMallocCont(contLen);
  tSerializeSGetUserAuthRsp(pReq, contLen, &userRsp);

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

void ctgTestRspTableCfg(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  rpcFreeCont(pMsg->pCont);
  
  static int32_t idx = 1;

  STableCfgRsp tblRsp = {0};
  strcpy(tblRsp.tbName, ctgTestTablename);
  tblRsp.numOfColumns = ctgTestColNum;

  tblRsp.pSchemas = (SSchema *)taosMemoryMalloc((tblRsp.numOfTags + tblRsp.numOfColumns) * sizeof(SSchema));

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

  s = &tblRsp.pSchemas[1];
  s->type = TSDB_DATA_TYPE_INT;
  s->colId = 2;
  s->bytes = 4;
  strcpy(s->name, "col1");

  int32_t contLen = tSerializeSTableCfgRsp(NULL, 0, &tblRsp);
  void   *pReq = rpcMallocCont(contLen);
  tSerializeSTableCfgRsp(pReq, contLen, &tblRsp);

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

  tFreeSTableCfgRsp(&tblRsp);
}

void ctgTestRspTableIndex(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  rpcFreeCont(pMsg->pCont);
  
  static int32_t idx = 1;

  STableIndexRsp tblRsp = {0};
  strcpy(tblRsp.tbName, ctgTestSTablename);

  tblRsp.pIndex = taosArrayInit(ctgTestIndexNum, sizeof(STableIndexInfo));

  STableIndexInfo info = {0};
  for (int32_t i = 0; i < ctgTestIndexNum; ++i) {
    info.interval = 1 + i;
    info.expr = (char*)taosMemoryCalloc(1, 10);
    taosArrayPush(tblRsp.pIndex, &info);
  }
  
  int32_t contLen = tSerializeSTableIndexRsp(NULL, 0, &tblRsp);
  void   *pReq = rpcMallocCont(contLen);
  tSerializeSTableIndexRsp(pReq, contLen, &tblRsp);

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

  tFreeSTableIndexRsp(&tblRsp);
}


void ctgTestRspAuto(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  switch (pMsg->msgType) {
    case TDMT_MND_USE_DB:
      ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);
      break;
    case TDMT_VND_TABLE_CFG:
    case TDMT_MND_TABLE_CFG:
      ctgTestRspTableCfg(shandle, pEpSet, pMsg, pRsp);
      break;
    case TDMT_MND_GET_TABLE_INDEX:
      ctgTestRspTableIndex(shandle, pEpSet, pMsg, pRsp);
      break;
    default:
      break;
  }

  return;
}


D
dapan 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
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 已提交
658 659 660
    case CTGT_RSP_INDEXINFO_E:
      ctgTestRspErrIndexInfo(shandle, pEpSet, pMsg, pRsp);
      break;
D
dapan1121 已提交
661 662 663 664 665 666
    case CTGT_RSP_USERAUTH:
      ctgTestRspUserAuth(shandle, pEpSet, pMsg, pRsp);
      break;
    case CTGT_RSP_TBLCFG:
      ctgTestRspTableCfg(shandle, pEpSet, pMsg, pRsp);
      break;
D
dapan1121 已提交
667 668 669
    case CTGT_RSP_TBMETA_NOT_EXIST:
      ctgTestRspTableMetaNotExist(shandle, pEpSet, pMsg, pRsp);
      break;
D
dapan1121 已提交
670 671 672
    case CTGT_RSP_TBLINDEX:
      ctgTestRspTableIndex(shandle, pEpSet, pMsg, pRsp);
      break;
D
dapan 已提交
673
    default:
D
dapan1121 已提交
674
      ctgTestRspAuto(shandle, pEpSet, pMsg, pRsp);
D
dapan 已提交
675 676 677 678
      break;
  }

  ctgTestRspIdx++;
S
Shengliang Guan 已提交
679

D
dapan 已提交
680 681 682
  return;
}

D
dapan 已提交
683 684 685 686
void ctgTestRspDbVgroupsAndNormalMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);

  ctgTestSetRspTableMeta();
S
Shengliang Guan 已提交
687

D
dapan 已提交
688 689 690
  return;
}

D
dapan 已提交
691 692
void ctgTestRspDbVgroupsAndChildMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);
S
Shengliang Guan 已提交
693

D
dapan 已提交
694
  ctgTestSetRspCTableMeta();
S
Shengliang Guan 已提交
695

D
dapan 已提交
696 697 698
  return;
}

D
dapan 已提交
699 700
void ctgTestRspDbVgroupsAndSuperMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);
S
Shengliang Guan 已提交
701

D
dapan 已提交
702
  ctgTestSetRspSTableMeta();
S
Shengliang Guan 已提交
703

D
dapan1121 已提交
704 705 706
  return;
}

D
dapan 已提交
707 708
void ctgTestRspDbVgroupsAndMultiSuperMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);
S
Shengliang Guan 已提交
709

D
dapan 已提交
710
  ctgTestSetRspMultiSTableMeta();
S
Shengliang Guan 已提交
711

D
dapan1121 已提交
712 713 714
  return;
}

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

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

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

D
dapan 已提交
775
void ctgTestSetRspSTableMeta() {
D
dapan1121 已提交
776
  static Stub stub;
D
dapan 已提交
777
  stub.set(rpcSendRecv, ctgTestRspSTableMeta);
D
dapan1121 已提交
778
  {
wafwerar's avatar
wafwerar 已提交
779
#ifdef WINDOWS
H
Hongze Cheng 已提交
780 781
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
782 783 784
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
785 786
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
787
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
788
#endif
S
Shengliang Guan 已提交
789
    for (const auto &f : result) {
D
dapan 已提交
790
      stub.set(f.second, ctgTestRspSTableMeta);
D
dapan1121 已提交
791 792 793 794
    }
  }
}

D
dapan 已提交
795
void ctgTestSetRspMultiSTableMeta() {
D
dapan1121 已提交
796
  static Stub stub;
D
dapan 已提交
797
  stub.set(rpcSendRecv, ctgTestRspMultiSTableMeta);
D
dapan1121 已提交
798
  {
wafwerar's avatar
wafwerar 已提交
799
#ifdef WINDOWS
H
Hongze Cheng 已提交
800 801
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
802 803 804
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
805 806
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
807
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
808
#endif
S
Shengliang Guan 已提交
809
    for (const auto &f : result) {
D
dapan 已提交
810
      stub.set(f.second, ctgTestRspMultiSTableMeta);
D
dapan1121 已提交
811 812 813 814
    }
  }
}

D
dapan 已提交
815
void ctgTestSetRspByIdx() {
D
dapan1121 已提交
816
  static Stub stub;
D
dapan 已提交
817
  stub.set(rpcSendRecv, ctgTestRspByIdx);
D
dapan1121 已提交
818
  {
wafwerar's avatar
wafwerar 已提交
819
#ifdef WINDOWS
H
Hongze Cheng 已提交
820 821
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
822 823 824
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
825 826
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
827
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
828
#endif
S
Shengliang Guan 已提交
829
    for (const auto &f : result) {
D
dapan 已提交
830
      stub.set(f.second, ctgTestRspByIdx);
D
dapan1121 已提交
831 832 833 834
    }
  }
}

D
dapan 已提交
835
void ctgTestSetRspDbVgroupsAndNormalMeta() {
D
dapan 已提交
836
  static Stub stub;
D
dapan 已提交
837
  stub.set(rpcSendRecv, ctgTestRspDbVgroupsAndNormalMeta);
D
dapan 已提交
838
  {
wafwerar's avatar
wafwerar 已提交
839
#ifdef WINDOWS
H
Hongze Cheng 已提交
840 841
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
842 843 844
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
845 846
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan 已提交
847
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
848
#endif
S
Shengliang Guan 已提交
849
    for (const auto &f : result) {
D
dapan 已提交
850
      stub.set(f.second, ctgTestRspDbVgroupsAndNormalMeta);
D
dapan 已提交
851 852 853 854
    }
  }
}

D
dapan 已提交
855
void ctgTestSetRspDbVgroupsAndChildMeta() {
D
dapan1121 已提交
856
  static Stub stub;
D
dapan 已提交
857
  stub.set(rpcSendRecv, ctgTestRspDbVgroupsAndChildMeta);
D
dapan1121 已提交
858
  {
wafwerar's avatar
wafwerar 已提交
859
#ifdef WINDOWS
H
Hongze Cheng 已提交
860 861
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
862 863 864
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
865 866
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
867
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
868
#endif
S
Shengliang Guan 已提交
869
    for (const auto &f : result) {
D
dapan 已提交
870
      stub.set(f.second, ctgTestRspDbVgroupsAndChildMeta);
D
dapan1121 已提交
871 872 873 874
    }
  }
}

D
dapan 已提交
875
void ctgTestSetRspDbVgroupsAndSuperMeta() {
D
dapan1121 已提交
876
  static Stub stub;
D
dapan 已提交
877
  stub.set(rpcSendRecv, ctgTestRspDbVgroupsAndSuperMeta);
D
dapan1121 已提交
878
  {
wafwerar's avatar
wafwerar 已提交
879
#ifdef WINDOWS
H
Hongze Cheng 已提交
880 881
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
882 883 884
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
885 886
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
887
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
888
#endif
S
Shengliang Guan 已提交
889
    for (const auto &f : result) {
D
dapan 已提交
890
      stub.set(f.second, ctgTestRspDbVgroupsAndSuperMeta);
D
dapan1121 已提交
891 892 893 894
    }
  }
}

D
dapan 已提交
895
void ctgTestSetRspDbVgroupsAndMultiSuperMeta() {
D
dapan1121 已提交
896
  static Stub stub;
D
dapan 已提交
897
  stub.set(rpcSendRecv, ctgTestRspDbVgroupsAndMultiSuperMeta);
D
dapan1121 已提交
898
  {
wafwerar's avatar
wafwerar 已提交
899
#ifdef WINDOWS
H
Hongze Cheng 已提交
900 901
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
902 903 904
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
905 906
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
907
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
908
#endif
S
Shengliang Guan 已提交
909
    for (const auto &f : result) {
D
dapan 已提交
910
      stub.set(f.second, ctgTestRspDbVgroupsAndMultiSuperMeta);
D
dapan1121 已提交
911 912 913 914
    }
  }
}

S
Shengliang Guan 已提交
915
}  // namespace
916

D
dapan1121 已提交
917
void *ctgTestGetDbVgroupThread(void *param) {
H
Hongze Cheng 已提交
918 919
  struct SCatalog  *pCtg = (struct SCatalog *)param;
  int32_t           code = 0;
D
dapan1121 已提交
920 921
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
922 923
  SArray           *vgList = NULL;
  int32_t           n = 0;
S
Shengliang Guan 已提交
924

D
dapan1121 已提交
925
  while (!ctgTestStop) {
926
    code = catalogGetDBVgList(pCtg, mockPointer, ctgTestDbname, &vgList);
D
dapan1121 已提交
927
    if (code) {
D
dapan1121 已提交
928
      printf("code:%x\n", code);
D
dapan1121 已提交
929 930 931 932 933 934 935 936
      assert(0);
    }

    if (vgList) {
      taosArrayDestroy(vgList);
    }

    if (ctgTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
937
      taosUsleep(taosRand() % 5);
D
dapan1121 已提交
938
    }
D
dapan1121 已提交
939
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
940 941 942 943 944 945 946
      printf("Get:%d\n", n);
    }
  }

  return NULL;
}

D
dapan1121 已提交
947
void *ctgTestSetSameDbVgroupThread(void *param) {
S
Shengliang Guan 已提交
948 949
  struct SCatalog *pCtg = (struct SCatalog *)param;
  int32_t          code = 0;
D
dapan1121 已提交
950
  SDBVgInfo       *dbVgroup = NULL;
S
Shengliang Guan 已提交
951 952
  int32_t          n = 0;

D
dapan1121 已提交
953 954
  while (!ctgTestStop) {
    ctgTestBuildDBVgroup(&dbVgroup);
D
dapan1121 已提交
955
    code = catalogUpdateDBVgInfo(pCtg, ctgTestDbname, ctgTestDbId, dbVgroup);
D
dapan1121 已提交
956 957 958 959
    if (code) {
      assert(0);
    }

S
Shengliang Guan 已提交
960
    if (ctgTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
961
      taosUsleep(taosRand() % 5);
D
dapan1121 已提交
962
    }
D
dapan1121 已提交
963
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
964 965 966 967 968 969 970
      printf("Set:%d\n", n);
    }
  }

  return NULL;
}

D
dapan1121 已提交
971 972 973
void *ctgTestSetDiffDbVgroupThread(void *param) {
  struct SCatalog *pCtg = (struct SCatalog *)param;
  int32_t          code = 0;
H
Hongze Cheng 已提交
974
  SDBVgInfo       *dbVgroup = NULL;
D
dapan1121 已提交
975 976 977 978
  int32_t          n = 0;

  while (!ctgTestStop) {
    ctgTestBuildDBVgroup(&dbVgroup);
D
dapan1121 已提交
979
    code = catalogUpdateDBVgInfo(pCtg, ctgTestDbname, ctgTestDbId++, dbVgroup);
D
dapan1121 已提交
980 981 982 983 984
    if (code) {
      assert(0);
    }

    if (ctgTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
985
      taosUsleep(taosRand() % 5);
D
dapan1121 已提交
986 987 988 989 990 991 992 993 994
    }
    if (++n % ctgTestPrintNum == 0) {
      printf("Set:%d\n", n);
    }
  }

  return NULL;
}

D
dapan1121 已提交
995
void *ctgTestGetCtableMetaThread(void *param) {
S
Shengliang Guan 已提交
996 997 998 999
  struct SCatalog *pCtg = (struct SCatalog *)param;
  int32_t          code = 0;
  int32_t          n = 0;
  STableMeta      *tbMeta = NULL;
D
dapan1121 已提交
1000
  bool             inCache = false;
D
dapan1121 已提交
1001

H
Hongze Cheng 已提交
1002
  SName cn = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1003 1004
  strcpy(cn.dbname, "db1");
  strcpy(cn.tname, ctgTestCTablename);
S
Shengliang Guan 已提交
1005

D
dapan1121 已提交
1006 1007 1008 1009
  SCtgTbMetaCtx ctx = {0};
  ctx.pName = &cn;
  ctx.flag = CTG_FLAG_UNKNOWN_STB;

D
dapan1121 已提交
1010
  while (!ctgTestStop) {
D
dapan1121 已提交
1011
    code = ctgReadTbMetaFromCache(pCtg, &ctx, &tbMeta);
D
dapan1121 已提交
1012
    if (code || NULL == tbMeta) {
D
dapan1121 已提交
1013 1014 1015
      assert(0);
    }

wafwerar's avatar
wafwerar 已提交
1016
    taosMemoryFreeClear(tbMeta);
D
dapan1121 已提交
1017

D
dapan1121 已提交
1018
    if (ctgTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
1019
      taosUsleep(taosRand() % 5);
D
dapan1121 已提交
1020
    }
S
Shengliang Guan 已提交
1021

D
dapan1121 已提交
1022
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
1023 1024 1025 1026 1027 1028 1029 1030
      printf("Get:%d\n", n);
    }
  }

  return NULL;
}

void *ctgTestSetCtableMetaThread(void *param) {
H
Hongze Cheng 已提交
1031 1032 1033 1034
  struct SCatalog  *pCtg = (struct SCatalog *)param;
  int32_t           code = 0;
  SDBVgInfo         dbVgroup = {0};
  int32_t           n = 0;
D
dapan 已提交
1035
  STableMetaOutput *output = NULL;
D
dapan1121 已提交
1036

D
dapan1121 已提交
1037
  SCtgCacheOperation operation = {0};
H
Hongze Cheng 已提交
1038

D
dapan1121 已提交
1039
  operation.opId = CTG_OP_UPDATE_TB_META;
S
Shengliang Guan 已提交
1040

D
dapan1121 已提交
1041
  while (!ctgTestStop) {
wafwerar's avatar
wafwerar 已提交
1042
    output = (STableMetaOutput *)taosMemoryMalloc(sizeof(STableMetaOutput));
D
dapan 已提交
1043 1044
    ctgTestBuildCTableMetaOutput(output);

D
dapan1121 已提交
1045
    SCtgUpdateTbMetaMsg *msg = (SCtgUpdateTbMetaMsg *)taosMemoryMalloc(sizeof(SCtgUpdateTbMetaMsg));
D
dapan1121 已提交
1046
    msg->pCtg = pCtg;
D
dapan1121 已提交
1047
    msg->pMeta = output;
D
dapan1121 已提交
1048
    operation.data = msg;
D
dapan1121 已提交
1049

D
dapan1121 已提交
1050
    code = ctgOpUpdateTbMeta(&operation);
D
dapan1121 已提交
1051 1052 1053 1054
    if (code) {
      assert(0);
    }

S
Shengliang Guan 已提交
1055
    if (ctgTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
1056
      taosUsleep(taosRand() % 5);
D
dapan1121 已提交
1057
    }
D
dapan1121 已提交
1058
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
1059 1060 1061 1062 1063 1064 1065
      printf("Set:%d\n", n);
    }
  }

  return NULL;
}

D
dapan 已提交
1066

D
dapan 已提交
1067
TEST(tableMeta, normalTable) {
H
Hongze Cheng 已提交
1068 1069
  struct SCatalog  *pCtg = NULL;
  SVgroupInfo       vgInfo = {0};
D
dapan1121 已提交
1070 1071
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
1072

D
dapan1121 已提交
1073 1074
  ctgTestInitLogFile();

D
dapan 已提交
1075
  ctgTestSetRspDbVgroups();
D
dapan1121 已提交
1076

1077
  initQueryModuleMsgHandle();
D
ut test  
dapan1121 已提交
1078

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

D
dapan1121 已提交
1081 1082
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);
1083

D
dapan1121 已提交
1084
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan1121 已提交
1085
  ASSERT_EQ(code, 0);
1086

H
Hongze Cheng 已提交
1087
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
1088 1089 1090
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

D
dapan1121 已提交
1091
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
1092
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
1093
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
1094
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
1095

D
dapan1121 已提交
1096 1097 1098 1099 1100 1101 1102 1103
  while (true) {
    uint64_t n = 0;
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
    if (n != 1) {
      taosMsleep(50);
    } else {
      break;
    }
D
dapan 已提交
1104
  }
H
Hongze Cheng 已提交
1105

D
dapan1121 已提交
1106 1107 1108 1109 1110 1111 1112 1113
  memset(&vgInfo, 0, sizeof(vgInfo));
  bool exists = false;
  code = catalogGetCachedTableHashVgroup(pCtg, mockPointer, &n, &vgInfo, &exists);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
  ASSERT_EQ(exists, true);

D
dapan 已提交
1114
  ctgTestSetRspTableMeta();
D
dapan1121 已提交
1115 1116

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1117
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1118 1119 1120
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 8);
  ASSERT_EQ(tableMeta->tableType, TSDB_NORMAL_TABLE);
D
dapan1121 已提交
1121
  ASSERT_EQ(tableMeta->uid, ctgTestNormalTblUid - 1);
D
dapan1121 已提交
1122 1123 1124 1125 1126 1127 1128
  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);

1129 1130
  taosMemoryFree(tableMeta);

D
dapan 已提交
1131
  while (true) {
D
dapan1121 已提交
1132
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
1133
    if (0 == n) {
wafwerar's avatar
wafwerar 已提交
1134
      taosMsleep(50);
D
dapan 已提交
1135 1136 1137
    } else {
      break;
    }
D
dapan 已提交
1138 1139
  }

D
dapan1121 已提交
1140
  tableMeta = NULL;
D
dapan1121 已提交
1141
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
  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);

1152 1153
  taosMemoryFree(tableMeta);

D
dapan1121 已提交
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
  tableMeta = NULL;
  catalogGetCachedTableMeta(pCtg, mockPointer, &n, &tableMeta);
  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 已提交
1166
  SDbVgVersion   *dbs = NULL;
D
dapan1121 已提交
1167
  SSTableVersion *stb = NULL;
H
Hongze Cheng 已提交
1168 1169
  uint32_t        dbNum = 0, stbNum = 0, allDbNum = 0, allStbNum = 0;
  int32_t         i = 0;
D
dapan1121 已提交
1170 1171 1172 1173 1174 1175
  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 已提交
1176

D
dapan1121 已提交
1177
    if (dbNum) {
S
Shengliang Guan 已提交
1178
      printf("got expired db,dbId:%" PRId64 "\n", dbs->dbId);
wafwerar's avatar
wafwerar 已提交
1179
      taosMemoryFree(dbs);
D
dapan1121 已提交
1180 1181 1182 1183 1184 1185
      dbs = NULL;
    } else {
      printf("no expired db\n");
    }

    if (stbNum) {
D
dapan 已提交
1186
      printf("got expired stb,suid:%" PRId64 ",dbFName:%s, stbName:%s\n", stb->suid, stb->dbFName, stb->stbName);
wafwerar's avatar
wafwerar 已提交
1187
      taosMemoryFree(stb);
D
dapan1121 已提交
1188 1189 1190 1191 1192 1193 1194
      stb = NULL;
    } else {
      printf("no expired stb\n");
    }

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

D
dapan1121 已提交
1198 1199 1200
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 0);

D
dapan1121 已提交
1201 1202 1203
  catalogDestroy();
}

D
dapan 已提交
1204
TEST(tableMeta, childTableCase) {
H
Hongze Cheng 已提交
1205
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1206 1207
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
1208
  SVgroupInfo       vgInfo = {0};
D
dapan1121 已提交
1209

D
dapan1121 已提交
1210 1211
  ctgTestInitLogFile();

D
dapan 已提交
1212
  ctgTestSetRspDbVgroupsAndChildMeta();
D
dapan1121 已提交
1213 1214 1215

  initQueryModuleMsgHandle();

S
Shengliang Guan 已提交
1216
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
D
dapan1121 已提交
1217 1218
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);
S
Shengliang Guan 已提交
1219

D
dapan1121 已提交
1220
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan1121 已提交
1221 1222
  ASSERT_EQ(code, 0);

H
Hongze Cheng 已提交
1223
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
1224 1225
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestCTablename);
D
dapan1121 已提交
1226 1227

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1228
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
  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);

1239 1240
  taosMemoryFree(tableMeta);

D
dapan 已提交
1241
  while (true) {
D
dapan1121 已提交
1242
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
1243
    if (0 == n) {
wafwerar's avatar
wafwerar 已提交
1244
      taosMsleep(50);
D
dapan 已提交
1245 1246 1247
    } else {
      break;
    }
D
dapan 已提交
1248 1249
  }

D
dapan1121 已提交
1250
  tableMeta = NULL;
D
dapan1121 已提交
1251
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
  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 已提交
1262
  taosMemoryFreeClear(tableMeta);
H
Haojun Liao 已提交
1263 1264

  strcpy(n.tname, ctgTestSTablename);
D
dapan1121 已提交
1265
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
  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);

1276 1277
  taosMemoryFree(tableMeta);

H
Hongze Cheng 已提交
1278
  SDbVgVersion   *dbs = NULL;
D
dapan1121 已提交
1279
  SSTableVersion *stb = NULL;
H
Hongze Cheng 已提交
1280 1281
  uint32_t        dbNum = 0, stbNum = 0, allDbNum = 0, allStbNum = 0;
  int32_t         i = 0;
D
dapan1121 已提交
1282 1283 1284 1285 1286 1287
  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 已提交
1288

D
dapan1121 已提交
1289
    if (dbNum) {
S
Shengliang Guan 已提交
1290
      printf("got expired db,dbId:%" PRId64 "\n", dbs->dbId);
wafwerar's avatar
wafwerar 已提交
1291
      taosMemoryFree(dbs);
D
dapan1121 已提交
1292 1293 1294 1295 1296 1297
      dbs = NULL;
    } else {
      printf("no expired db\n");
    }

    if (stbNum) {
S
Shengliang Guan 已提交
1298
      printf("got expired stb,suid:%" PRId64 ",dbFName:%s, stbName:%s\n", stb->suid, stb->dbFName, stb->stbName);
wafwerar's avatar
wafwerar 已提交
1299
      taosMemoryFree(stb);
D
dapan1121 已提交
1300 1301 1302 1303 1304 1305 1306
      stb = NULL;
    } else {
      printf("no expired stb\n");
    }

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

D
dapan1121 已提交
1310 1311 1312
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 1);

D
dapan1121 已提交
1313 1314 1315
  catalogDestroy();
}

D
dapan 已提交
1316
TEST(tableMeta, superTableCase) {
H
Hongze Cheng 已提交
1317
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1318 1319
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
1320
  SVgroupInfo       vgInfo = {0};
D
dapan1121 已提交
1321

D
dapan 已提交
1322
  ctgTestSetRspDbVgroupsAndSuperMeta();
D
dapan1121 已提交
1323 1324 1325

  initQueryModuleMsgHandle();

D
dapan1121 已提交
1326 1327 1328
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

S
Shengliang Guan 已提交
1329
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
D
dapan1121 已提交
1330
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan1121 已提交
1331 1332
  ASSERT_EQ(code, 0);

H
Hongze Cheng 已提交
1333
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
1334 1335
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestSTablename);
D
dapan1121 已提交
1336 1337

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1338
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1339 1340 1341 1342 1343
  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 已提交
1344 1345
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
1346 1347 1348 1349 1350
  ASSERT_EQ(tableMeta->tableInfo.numOfColumns, ctgTestColNum);
  ASSERT_EQ(tableMeta->tableInfo.numOfTags, ctgTestTagNum);
  ASSERT_EQ(tableMeta->tableInfo.precision, 1);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);

1351 1352
  taosMemoryFree(tableMeta);

D
dapan 已提交
1353
  while (true) {
D
dapan1121 已提交
1354
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
1355
    if (0 == n) {
wafwerar's avatar
wafwerar 已提交
1356
      taosMsleep(50);
D
dapan 已提交
1357 1358 1359
    } else {
      break;
    }
D
dapan 已提交
1360 1361
  }

D
dapan1121 已提交
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
  tableMeta = NULL;
  code = catalogGetCachedSTableMeta(pCtg, mockPointer, &n, &tableMeta);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 0);
  ASSERT_EQ(tableMeta->tableType, TSDB_SUPER_TABLE);
  ASSERT_EQ(tableMeta->sversion, ctgTestSVersion);
  ASSERT_EQ(tableMeta->tversion, ctgTestTVersion);
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
  ASSERT_EQ(tableMeta->tableInfo.numOfColumns, ctgTestColNum);
  ASSERT_EQ(tableMeta->tableInfo.numOfTags, ctgTestTagNum);
  ASSERT_EQ(tableMeta->tableInfo.precision, 1);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);
  taosMemoryFree(tableMeta);

D
dapan 已提交
1377
  ctgTestSetRspCTableMeta();
D
dapan1121 已提交
1378 1379

  tableMeta = NULL;
H
Haojun Liao 已提交
1380 1381 1382

  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestCTablename);
D
dapan1121 已提交
1383
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
  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);

1394 1395
  taosMemoryFree(tableMeta);

D
dapan 已提交
1396
  while (true) {
D
dapan1121 已提交
1397
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
1398
    if (2 != n) {
wafwerar's avatar
wafwerar 已提交
1399
      taosMsleep(50);
D
dapan 已提交
1400 1401 1402
    } else {
      break;
    }
D
dapan 已提交
1403 1404
  }

D
dapan1121 已提交
1405
  tableMeta = NULL;
D
dapan1121 已提交
1406
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
  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);

1417 1418
  taosMemoryFree(tableMeta);

H
Hongze Cheng 已提交
1419
  SDbVgVersion   *dbs = NULL;
D
dapan1121 已提交
1420
  SSTableVersion *stb = NULL;
H
Hongze Cheng 已提交
1421 1422
  uint32_t        dbNum = 0, stbNum = 0, allDbNum = 0, allStbNum = 0;
  int32_t         i = 0;
D
dapan1121 已提交
1423 1424 1425 1426 1427 1428
  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 已提交
1429

D
dapan1121 已提交
1430
    if (dbNum) {
S
Shengliang Guan 已提交
1431
      printf("got expired db,dbId:%" PRId64 "\n", dbs->dbId);
wafwerar's avatar
wafwerar 已提交
1432
      taosMemoryFree(dbs);
D
dapan1121 已提交
1433 1434 1435 1436 1437 1438
      dbs = NULL;
    } else {
      printf("no expired db\n");
    }

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

wafwerar's avatar
wafwerar 已提交
1441
      taosMemoryFree(stb);
D
dapan1121 已提交
1442 1443 1444 1445 1446 1447 1448
      stb = NULL;
    } else {
      printf("no expired stb\n");
    }

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

D
dapan1121 已提交
1452 1453
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 1);
D
dapan1121 已提交
1454 1455

  catalogDestroy();
D
dapan1121 已提交
1456
}
1457

D
dapan1121 已提交
1458
TEST(tableMeta, rmStbMeta) {
H
Hongze Cheng 已提交
1459
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1460 1461
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
1462
  SVgroupInfo       vgInfo = {0};
D
dapan1121 已提交
1463 1464 1465

  ctgTestInitLogFile();

D
dapan 已提交
1466
  ctgTestSetRspDbVgroupsAndSuperMeta();
D
dapan1121 已提交
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476

  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 已提交
1477
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1478 1479 1480 1481
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestSTablename);

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1482
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1483 1484 1485 1486 1487
  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 已提交
1488 1489
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
1490 1491 1492 1493 1494
  ASSERT_EQ(tableMeta->tableInfo.numOfColumns, ctgTestColNum);
  ASSERT_EQ(tableMeta->tableInfo.numOfTags, ctgTestTagNum);
  ASSERT_EQ(tableMeta->tableInfo.precision, 1);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);

1495 1496
  taosMemoryFree(tableMeta);

D
dapan 已提交
1497
  while (true) {
D
dapan1121 已提交
1498
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
1499
    if (0 == n) {
wafwerar's avatar
wafwerar 已提交
1500
      taosMsleep(50);
D
dapan 已提交
1501 1502 1503
    } else {
      break;
    }
D
dapan 已提交
1504 1505
  }

D
dapan1121 已提交
1506
  code = catalogRemoveStbMeta(pCtg, "1.db1", ctgTestDbId, ctgTestSTablename, ctgTestSuid);
D
dapan1121 已提交
1507 1508
  ASSERT_EQ(code, 0);

D
dapan 已提交
1509
  while (true) {
D
dapan1121 已提交
1510 1511
    int32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
    int32_t m = ctgdGetClusterCacheNum(pCtg, CTG_DBG_STB_RENT_NUM);
D
dapan 已提交
1512
    if (n || m) {
wafwerar's avatar
wafwerar 已提交
1513
      taosMsleep(50);
D
dapan 已提交
1514 1515 1516
    } else {
      break;
    }
D
dapan 已提交
1517 1518
  }

D
dapan1121 已提交
1519 1520 1521 1522 1523
  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 已提交
1524

D
dapan1121 已提交
1525 1526 1527 1528
  catalogDestroy();
}

TEST(tableMeta, updateStbMeta) {
H
Hongze Cheng 已提交
1529
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1530 1531
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
1532
  SVgroupInfo       vgInfo = {0};
D
dapan1121 已提交
1533 1534 1535

  ctgTestInitLogFile();

D
dapan 已提交
1536
  ctgTestSetRspDbVgroupsAndSuperMeta();
D
dapan1121 已提交
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546

  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 已提交
1547
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1548 1549 1550 1551
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestSTablename);

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1552
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1553 1554 1555 1556 1557
  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 已提交
1558 1559
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
1560 1561 1562 1563 1564
  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 已提交
1565
  while (true) {
D
dapan1121 已提交
1566
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
1567
    if (0 == n) {
wafwerar's avatar
wafwerar 已提交
1568
      taosMsleep(50);
D
dapan 已提交
1569 1570 1571
    } else {
      break;
    }
D
dapan 已提交
1572 1573
  }

wafwerar's avatar
wafwerar 已提交
1574
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1575 1576 1577 1578

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

D
dapan1121 已提交
1579
  code = catalogUpdateTableMeta(pCtg, &rsp);
D
dapan1121 已提交
1580
  ASSERT_EQ(code, 0);
wafwerar's avatar
wafwerar 已提交
1581
  taosMemoryFreeClear(rsp.pSchemas);
D
dapan1121 已提交
1582

D
dapan 已提交
1583 1584
  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
1585
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan 已提交
1586
    if (n != 3) {
wafwerar's avatar
wafwerar 已提交
1587
      taosMsleep(50);
D
dapan 已提交
1588 1589 1590 1591 1592
    } else {
      break;
    }
  }

D
dapan1121 已提交
1593 1594 1595 1596 1597
  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 已提交
1598

D
dapan1121 已提交
1599
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1600 1601 1602 1603 1604
  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);
D
dapan1121 已提交
1605 1606
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
1607 1608 1609 1610 1611
  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 已提交
1612
  taosMemoryFreeClear(tableMeta);
S
Shengliang Guan 已提交
1613

D
dapan1121 已提交
1614 1615 1616
  catalogDestroy();
}

D
dapan1121 已提交
1617 1618
TEST(getIndexInfo, notExists) {
  struct SCatalog  *pCtg = NULL;
1619 1620
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
D
dapan1121 已提交
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
  SVgroupInfo       vgInfo = {0};
  SArray           *vgList = NULL;

  ctgTestInitLogFile();

  memset(ctgTestRspFunc, 0, sizeof(ctgTestRspFunc));
  ctgTestRspIdx = 0;
  ctgTestRspFunc[0] = CTGT_RSP_INDEXINFO_E;

  ctgTestSetRspByIdx();

  initQueryModuleMsgHandle();

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

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

  SIndexInfo info;
  code = catalogGetIndexMeta(pCtg, mockPointer, "index1", &info);
  ASSERT_TRUE(code != 0);
1643 1644
  
  catalogDestroy();
D
dapan1121 已提交
1645 1646
}

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

  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 已提交
1661

D
dapan1121 已提交
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
  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 已提交
1674
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1675 1676 1677
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

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

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

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1693
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1694 1695 1696
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 8);
  ASSERT_EQ(tableMeta->tableType, TSDB_NORMAL_TABLE);
H
Hongze Cheng 已提交
1697
  ASSERT_EQ(tableMeta->uid, ctgTestNormalTblUid - 1);
D
dapan1121 已提交
1698 1699 1700 1701 1702 1703
  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 已提交
1704
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1705

D
dapan1121 已提交
1706
  while (0 == ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) {
wafwerar's avatar
wafwerar 已提交
1707
    taosMsleep(50);
D
dapan1121 已提交
1708 1709
  }

D
dapan1121 已提交
1710
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
1711 1712 1713
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 8);
  ASSERT_EQ(tableMeta->tableType, TSDB_NORMAL_TABLE);
H
Hongze Cheng 已提交
1714
  ASSERT_EQ(tableMeta->uid, ctgTestNormalTblUid - 1);
D
dapan1121 已提交
1715 1716 1717 1718 1719 1720
  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 已提交
1721
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1722 1723 1724 1725 1726

  catalogDestroy();
}

TEST(refreshGetMeta, normal2notexist) {
H
Hongze Cheng 已提交
1727
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1728 1729
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
1730 1731
  SVgroupInfo       vgInfo = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
1732 1733 1734 1735 1736 1737 1738 1739

  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 已提交
1740

D
dapan1121 已提交
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
  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 已提交
1753
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1754 1755 1756
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

D
dapan1121 已提交
1757
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
1758 1759
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
1760
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
1761 1762 1763

  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
1764
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan1121 已提交
1765 1766 1767
    if (n > 0) {
      break;
    }
wafwerar's avatar
wafwerar 已提交
1768
    taosMsleep(50);
D
dapan1121 已提交
1769 1770 1771
  }

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1772
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1773 1774 1775
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 8);
  ASSERT_EQ(tableMeta->tableType, TSDB_NORMAL_TABLE);
H
Hongze Cheng 已提交
1776
  ASSERT_EQ(tableMeta->uid, ctgTestNormalTblUid - 1);
D
dapan1121 已提交
1777 1778 1779 1780 1781 1782
  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 已提交
1783
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1784

D
dapan1121 已提交
1785
  while (0 == ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) {
wafwerar's avatar
wafwerar 已提交
1786
    taosMsleep(50);
D
dapan1121 已提交
1787 1788
  }

D
dapan1121 已提交
1789
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
1790 1791 1792 1793 1794 1795 1796
  ASSERT_EQ(code, CTG_ERR_CODE_TABLE_NOT_EXIST);
  ASSERT_TRUE(tableMeta == NULL);

  catalogDestroy();
}

TEST(refreshGetMeta, normal2child) {
H
Hongze Cheng 已提交
1797
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1798 1799
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
1800 1801
  SVgroupInfo       vgInfo = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
1802 1803 1804 1805 1806 1807 1808 1809 1810

  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 已提交
1811

D
dapan1121 已提交
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
  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 已提交
1824
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1825 1826 1827 1828 1829
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);
  ctgTestCurrentCTableName = ctgTestTablename;
  ctgTestCurrentSTableName = ctgTestSTablename;

D
dapan1121 已提交
1830
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
1831 1832
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
1833
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
1834 1835 1836

  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
1837
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan1121 已提交
1838 1839 1840
    if (n > 0) {
      break;
    }
wafwerar's avatar
wafwerar 已提交
1841
    taosMsleep(50);
D
dapan1121 已提交
1842 1843 1844
  }

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1845
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1846 1847 1848
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 8);
  ASSERT_EQ(tableMeta->tableType, TSDB_NORMAL_TABLE);
H
Hongze Cheng 已提交
1849
  ASSERT_EQ(tableMeta->uid, ctgTestNormalTblUid - 1);
D
dapan1121 已提交
1850 1851 1852 1853 1854 1855
  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 已提交
1856
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1857

D
dapan1121 已提交
1858
  while (0 == ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) {
wafwerar's avatar
wafwerar 已提交
1859
    taosMsleep(50);
D
dapan1121 已提交
1860 1861
  }

D
dapan1121 已提交
1862
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
1863 1864 1865 1866 1867 1868 1869 1870 1871
  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 已提交
1872
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1873 1874 1875 1876 1877 1878

  catalogDestroy();
  ctgTestCurrentCTableName = NULL;
  ctgTestCurrentSTableName = NULL;
}

D
dapan1121 已提交
1879

D
dapan1121 已提交
1880
TEST(refreshGetMeta, stable2child) {
H
Hongze Cheng 已提交
1881
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1882 1883
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
1884 1885
  SVgroupInfo       vgInfo = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895

  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 已提交
1896

D
dapan1121 已提交
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908
  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 已提交
1909
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1910 1911 1912 1913 1914
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);
  ctgTestCurrentSTableName = ctgTestTablename;
  ctgTestCurrentCTableName = ctgTestTablename;

D
dapan1121 已提交
1915
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
1916 1917
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
1918
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
1919 1920 1921

  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
1922
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan1121 已提交
1923 1924 1925
    if (n > 0) {
      break;
    }
wafwerar's avatar
wafwerar 已提交
1926
    taosMsleep(50);
D
dapan1121 已提交
1927 1928 1929
  }

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1930
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1931 1932 1933 1934 1935
  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 已提交
1936 1937
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
1938 1939 1940 1941
  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 已提交
1942
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1943

D
dapan1121 已提交
1944
  while (0 == ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) {
wafwerar's avatar
wafwerar 已提交
1945
    taosMsleep(50);
D
dapan1121 已提交
1946 1947 1948
  }

  ctgTestCurrentSTableName = ctgTestSTablename;
D
dapan1121 已提交
1949
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
1950 1951 1952 1953 1954 1955 1956 1957 1958
  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 已提交
1959
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1960 1961 1962 1963 1964 1965 1966

  catalogDestroy();
  ctgTestCurrentCTableName = NULL;
  ctgTestCurrentSTableName = NULL;
}

TEST(refreshGetMeta, stable2stable) {
H
Hongze Cheng 已提交
1967
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1968 1969
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
1970 1971
  SVgroupInfo       vgInfo = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981

  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 已提交
1982

D
dapan1121 已提交
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
  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 已提交
1995
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1996 1997 1998 1999
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);
  ctgTestCurrentSTableName = ctgTestTablename;

D
dapan1121 已提交
2000
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
2001 2002
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
2003
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
2004 2005 2006

  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
2007
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan1121 已提交
2008 2009 2010
    if (n > 0) {
      break;
    }
wafwerar's avatar
wafwerar 已提交
2011
    taosMsleep(50);
D
dapan1121 已提交
2012 2013 2014
  }

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
2015
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
2016 2017 2018 2019 2020
  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 已提交
2021 2022
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
2023 2024 2025 2026
  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 已提交
2027
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
2028

D
dapan1121 已提交
2029
  while (0 == ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) {
wafwerar's avatar
wafwerar 已提交
2030
    taosMsleep(50);
D
dapan1121 已提交
2031 2032
  }

D
dapan1121 已提交
2033
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
2034 2035 2036 2037 2038
  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 已提交
2039 2040
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
2041 2042 2043 2044
  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 已提交
2045
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
2046 2047 2048 2049 2050 2051 2052

  catalogDestroy();
  ctgTestCurrentCTableName = NULL;
  ctgTestCurrentSTableName = NULL;
}

TEST(refreshGetMeta, child2stable) {
H
Hongze Cheng 已提交
2053
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2054 2055
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2056 2057
  SVgroupInfo       vgInfo = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067

  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 已提交
2068

D
dapan1121 已提交
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
  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 已提交
2081
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
2082 2083 2084 2085 2086
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);
  ctgTestCurrentCTableName = ctgTestTablename;
  ctgTestCurrentSTableName = ctgTestSTablename;

D
dapan1121 已提交
2087
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
2088 2089
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
2090
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
2091 2092 2093

  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
2094
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan1121 已提交
2095 2096 2097
    if (n > 0) {
      break;
    }
wafwerar's avatar
wafwerar 已提交
2098
    taosMsleep(50);
D
dapan1121 已提交
2099 2100 2101
  }

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
2102
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
2103 2104 2105 2106 2107 2108 2109 2110 2111
  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 已提交
2112
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
2113

D
dapan1121 已提交
2114
  while (2 != ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) {
wafwerar's avatar
wafwerar 已提交
2115
    taosMsleep(50);
D
dapan1121 已提交
2116 2117 2118
  }

  ctgTestCurrentSTableName = ctgTestTablename;
D
dapan1121 已提交
2119
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
2120 2121 2122 2123 2124
  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 已提交
2125 2126
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
2127 2128 2129 2130
  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 已提交
2131
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
2132 2133 2134 2135 2136 2137

  catalogDestroy();
  ctgTestCurrentCTableName = NULL;
  ctgTestCurrentSTableName = NULL;
}

D
dapan 已提交
2138
TEST(tableDistVgroup, normalTable) {
H
Hongze Cheng 已提交
2139
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2140 2141
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2142 2143
  SVgroupInfo      *vgInfo = NULL;
  SArray           *vgList = NULL;
D
dapan 已提交
2144

D
dapan 已提交
2145 2146 2147 2148 2149 2150 2151
  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 已提交
2152

D
dapan 已提交
2153
  ctgTestSetRspByIdx();
D
dapan 已提交
2154 2155 2156

  initQueryModuleMsgHandle();

D
dapan1121 已提交
2157 2158 2159
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

D
dapan1121 已提交
2162
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan 已提交
2163 2164
  ASSERT_EQ(code, 0);

H
Hongze Cheng 已提交
2165
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
2166 2167
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);
D
dapan 已提交
2168

D
dapan1121 已提交
2169
  code = catalogGetTableDistVgInfo(pCtg, mockPointer, &n, &vgList);
D
dapan1121 已提交
2170
  ASSERT_TRUE(code != 0);
D
dapan 已提交
2171 2172 2173 2174 2175

  catalogDestroy();
}

TEST(tableDistVgroup, childTableCase) {
H
Hongze Cheng 已提交
2176
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2177 2178
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2179 2180
  SVgroupInfo      *vgInfo = NULL;
  SArray           *vgList = NULL;
D
dapan 已提交
2181

D
dapan 已提交
2182 2183 2184 2185 2186 2187 2188 2189
  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 已提交
2190

D
dapan 已提交
2191
  ctgTestSetRspByIdx();
D
dapan 已提交
2192 2193 2194

  initQueryModuleMsgHandle();

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

D
dapan 已提交
2197 2198 2199 2200 2201 2202
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

H
Hongze Cheng 已提交
2203
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
2204 2205 2206
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestCTablename);

D
dapan1121 已提交
2207
  code = catalogGetTableDistVgInfo(pCtg, mockPointer, &n, &vgList);
D
dapan1121 已提交
2208
  ASSERT_TRUE(code != 0);
D
dapan 已提交
2209 2210 2211 2212 2213

  catalogDestroy();
}

TEST(tableDistVgroup, superTableCase) {
H
Hongze Cheng 已提交
2214
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2215 2216
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2217 2218
  SVgroupInfo      *vgInfo = NULL;
  SArray           *vgList = NULL;
D
dapan 已提交
2219

D
dapan 已提交
2220 2221 2222 2223 2224 2225 2226 2227 2228
  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 已提交
2229
  ctgTestSetRspByIdx();
D
dapan 已提交
2230 2231 2232

  initQueryModuleMsgHandle();

D
dapan1121 已提交
2233 2234 2235
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

S
Shengliang Guan 已提交
2236
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
D
dapan1121 已提交
2237
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan 已提交
2238 2239
  ASSERT_EQ(code, 0);

H
Hongze Cheng 已提交
2240
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
2241 2242
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestSTablename);
D
dapan 已提交
2243

D
dapan1121 已提交
2244
  code = catalogGetTableDistVgInfo(pCtg, mockPointer, &n, &vgList);
D
dapan 已提交
2245 2246 2247 2248
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), 10);
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 0);
  ASSERT_EQ(vgInfo->vgId, 1);
L
Liu Jicong 已提交
2249
  ASSERT_EQ(vgInfo->epSet.numOfEps, 1);
D
dapan 已提交
2250 2251
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 1);
  ASSERT_EQ(vgInfo->vgId, 2);
L
Liu Jicong 已提交
2252
  ASSERT_EQ(vgInfo->epSet.numOfEps, 2);
D
dapan 已提交
2253 2254
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 2);
  ASSERT_EQ(vgInfo->vgId, 3);
L
Liu Jicong 已提交
2255
  ASSERT_EQ(vgInfo->epSet.numOfEps, 3);
D
dapan 已提交
2256

2257 2258
  taosArrayDestroy(vgList);

D
dapan 已提交
2259 2260 2261
  catalogDestroy();
}

D
dapan1121 已提交
2262
TEST(dbVgroup, getSetDbVgroupCase) {
H
Hongze Cheng 已提交
2263
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2264 2265
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2266 2267 2268 2269
  SVgroupInfo       vgInfo = {0};
  SVgroupInfo      *pvgInfo = NULL;
  SDBVgInfo        *dbVgroup = NULL;
  SArray           *vgList = NULL;
D
dapan1121 已提交
2270

D
dapan 已提交
2271 2272
  ctgTestInitLogFile();

D
dapan 已提交
2273 2274 2275 2276 2277 2278 2279
  memset(ctgTestRspFunc, 0, sizeof(ctgTestRspFunc));
  ctgTestRspIdx = 0;
  ctgTestRspFunc[0] = CTGT_RSP_VGINFO;
  ctgTestRspFunc[1] = CTGT_RSP_TBMETA;

  ctgTestSetRspByIdx();

D
dapan1121 已提交
2280 2281
  initQueryModuleMsgHandle();

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

D
dapan1121 已提交
2284 2285 2286 2287 2288 2289
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

H
Hongze Cheng 已提交
2290
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
2291 2292 2293
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

2294
  code = catalogGetDBVgList(pCtg, mockPointer, ctgTestDbname, &vgList);
D
dapan1121 已提交
2295 2296
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), ctgTestVgNum);
S
Shengliang Guan 已提交
2297

2298 2299
  taosArrayDestroy(vgList);

D
dapan1121 已提交
2300 2301
  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
2302
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan1121 已提交
2303 2304 2305
    if (n > 0) {
      break;
    }
wafwerar's avatar
wafwerar 已提交
2306
    taosMsleep(50);
D
dapan 已提交
2307 2308
  }

D
dapan1121 已提交
2309
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
2310 2311
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
2312
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
2313

D
dapan1121 已提交
2314
  code = catalogGetTableDistVgInfo(pCtg, mockPointer, &n, &vgList);
D
dapan1121 已提交
2315
  ASSERT_TRUE(code != 0);
D
dapan1121 已提交
2316

D
dapan1121 已提交
2317 2318 2319 2320 2321 2322 2323 2324 2325
  int32_t dbVer = 0;
  int64_t dbId = 0;
  int32_t tbNum = 0;
  code = catalogGetDBVgVersion(pCtg, ctgTestDbname, &dbVer, &dbId, &tbNum);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(dbVer, ctgTestVgVersion);
  ASSERT_EQ(dbId, ctgTestDbId);
  ASSERT_EQ(tbNum, ctgTestVgNum / 2);

D
dapan1121 已提交
2326
  ctgTestBuildDBVgroup(&dbVgroup);
D
dapan1121 已提交
2327
  code = catalogUpdateDBVgInfo(pCtg, ctgTestDbname, ctgTestDbId, dbVgroup);
D
dapan1121 已提交
2328 2329
  ASSERT_EQ(code, 0);

D
dapan 已提交
2330 2331
  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
2332
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan 已提交
2333
    if (n != 3) {
wafwerar's avatar
wafwerar 已提交
2334
      taosMsleep(50);
D
dapan 已提交
2335 2336 2337 2338 2339
    } else {
      break;
    }
  }

D
dapan1121 已提交
2340
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
2341 2342
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 7);
L
Liu Jicong 已提交
2343
  ASSERT_EQ(vgInfo.epSet.numOfEps, 2);
D
dapan1121 已提交
2344

D
dapan1121 已提交
2345
  code = catalogGetTableDistVgInfo(pCtg, mockPointer, &n, &vgList);
D
dapan1121 已提交
2346
  ASSERT_TRUE(code != 0);
S
Shengliang Guan 已提交
2347

D
dapan1121 已提交
2348 2349 2350
  catalogDestroy();
}

D
dapan1121 已提交
2351
TEST(multiThread, getSetRmSameDbVgroup) {
H
Hongze Cheng 已提交
2352
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2353 2354
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2355 2356 2357 2358
  SVgroupInfo       vgInfo = {0};
  SVgroupInfo      *pvgInfo = NULL;
  SDBVgInfo         dbVgroup = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
2359
  ctgTestStop = false;
D
dapan1121 已提交
2360 2361

  ctgTestInitLogFile();
S
Shengliang Guan 已提交
2362

D
dapan 已提交
2363
  ctgTestSetRspDbVgroups();
D
dapan1121 已提交
2364 2365 2366

  initQueryModuleMsgHandle();

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

D
dapan1121 已提交
2369 2370 2371 2372 2373 2374
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

H
Hongze Cheng 已提交
2375
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
2376 2377 2378
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

wafwerar's avatar
wafwerar 已提交
2379 2380
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
2381

wafwerar's avatar
wafwerar 已提交
2382 2383
  TdThread thread1, thread2;
  taosThreadCreate(&(thread1), &thattr, ctgTestSetSameDbVgroupThread, pCtg);
D
dapan1121 已提交
2384

wafwerar's avatar
wafwerar 已提交
2385
  taosSsleep(1);
wafwerar's avatar
wafwerar 已提交
2386
  taosThreadCreate(&(thread2), &thattr, ctgTestGetDbVgroupThread, pCtg);
D
dapan1121 已提交
2387 2388 2389

  while (true) {
    if (ctgTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
2390
      taosSsleep(1);
D
dapan1121 已提交
2391
    } else {
wafwerar's avatar
wafwerar 已提交
2392
      taosSsleep(ctgTestMTRunSec);
D
dapan1121 已提交
2393 2394 2395
      break;
    }
  }
S
Shengliang Guan 已提交
2396

D
dapan1121 已提交
2397
  ctgTestStop = true;
wafwerar's avatar
wafwerar 已提交
2398
  taosSsleep(1);
S
Shengliang Guan 已提交
2399

D
dapan1121 已提交
2400 2401 2402
  catalogDestroy();
}

D
dapan1121 已提交
2403
TEST(multiThread, getSetRmDiffDbVgroup) {
H
Hongze Cheng 已提交
2404
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2405 2406
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2407 2408 2409 2410
  SVgroupInfo       vgInfo = {0};
  SVgroupInfo      *pvgInfo = NULL;
  SDBVgInfo         dbVgroup = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
2411 2412 2413 2414
  ctgTestStop = false;

  ctgTestInitLogFile();

D
dapan 已提交
2415
  ctgTestSetRspDbVgroups();
D
dapan1121 已提交
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426

  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 已提交
2427
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
2428 2429 2430
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

wafwerar's avatar
wafwerar 已提交
2431 2432
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
2433

wafwerar's avatar
wafwerar 已提交
2434 2435
  TdThread thread1, thread2;
  taosThreadCreate(&(thread1), &thattr, ctgTestSetDiffDbVgroupThread, pCtg);
D
dapan1121 已提交
2436

wafwerar's avatar
wafwerar 已提交
2437
  taosSsleep(1);
wafwerar's avatar
wafwerar 已提交
2438
  taosThreadCreate(&(thread2), &thattr, ctgTestGetDbVgroupThread, pCtg);
D
dapan1121 已提交
2439 2440 2441

  while (true) {
    if (ctgTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
2442
      taosSsleep(1);
D
dapan1121 已提交
2443
    } else {
wafwerar's avatar
wafwerar 已提交
2444
      taosSsleep(ctgTestMTRunSec);
D
dapan1121 已提交
2445 2446 2447 2448 2449
      break;
    }
  }

  ctgTestStop = true;
wafwerar's avatar
wafwerar 已提交
2450
  taosSsleep(1);
D
dapan1121 已提交
2451 2452 2453 2454

  catalogDestroy();
}

D
dapan1121 已提交
2455
TEST(multiThread, ctableMeta) {
H
Hongze Cheng 已提交
2456
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2457 2458
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2459 2460 2461 2462
  SVgroupInfo       vgInfo = {0};
  SVgroupInfo      *pvgInfo = NULL;
  SDBVgInfo         dbVgroup = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
2463
  ctgTestStop = false;
D
dapan1121 已提交
2464

D
dapan 已提交
2465 2466
  ctgTestInitLogFile();

D
dapan 已提交
2467
  ctgTestSetRspDbVgroupsAndChildMeta();
D
dapan1121 已提交
2468 2469 2470

  initQueryModuleMsgHandle();

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

D
dapan1121 已提交
2473 2474 2475 2476 2477 2478
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

H
Hongze Cheng 已提交
2479
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
2480 2481 2482
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

wafwerar's avatar
wafwerar 已提交
2483 2484
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
2485

wafwerar's avatar
wafwerar 已提交
2486 2487
  TdThread thread1, thread2;
  taosThreadCreate(&(thread1), &thattr, ctgTestSetCtableMetaThread, pCtg);
wafwerar's avatar
wafwerar 已提交
2488
  taosSsleep(1);
wafwerar's avatar
wafwerar 已提交
2489
  taosThreadCreate(&(thread1), &thattr, ctgTestGetCtableMetaThread, pCtg);
D
dapan1121 已提交
2490 2491 2492

  while (true) {
    if (ctgTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
2493
      taosSsleep(1);
D
dapan1121 已提交
2494
    } else {
wafwerar's avatar
wafwerar 已提交
2495
      taosSsleep(ctgTestMTRunSec);
D
dapan1121 已提交
2496 2497 2498
      break;
    }
  }
S
Shengliang Guan 已提交
2499

D
dapan1121 已提交
2500
  ctgTestStop = true;
wafwerar's avatar
wafwerar 已提交
2501
  taosSsleep(2);
S
Shengliang Guan 已提交
2502

D
dapan1121 已提交
2503 2504
  catalogDestroy();
}
D
dapan 已提交
2505

D
dapan1121 已提交
2506
TEST(rentTest, allRent) {
H
Hongze Cheng 已提交
2507
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2508 2509
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2510 2511 2512 2513
  SVgroupInfo       vgInfo = {0};
  SVgroupInfo      *pvgInfo = NULL;
  SDBVgInfo         dbVgroup = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
2514
  ctgTestStop = false;
H
Hongze Cheng 已提交
2515
  SDbVgVersion   *dbs = NULL;
D
dapan1121 已提交
2516
  SSTableVersion *stable = NULL;
H
Hongze Cheng 已提交
2517
  uint32_t        num = 0;
D
dapan1121 已提交
2518

D
dapan 已提交
2519 2520
  ctgTestInitLogFile();

D
dapan 已提交
2521
  ctgTestSetRspDbVgroupsAndMultiSuperMeta();
D
dapan1121 已提交
2522 2523 2524 2525 2526 2527 2528 2529 2530

  initQueryModuleMsgHandle();

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

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

H
Hongze Cheng 已提交
2531
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
2532 2533 2534 2535 2536 2537
  strcpy(n.dbname, "db1");

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

    STableMeta *tableMeta = NULL;
D
dapan1121 已提交
2538
    code = catalogGetSTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549
    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 已提交
2550

2551 2552
    taosMemoryFree(tableMeta);

D
dapan1121 已提交
2553
    while (ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM) < i) {
wafwerar's avatar
wafwerar 已提交
2554
      taosMsleep(50);
D
dapan 已提交
2555 2556
    }

D
dapan1121 已提交
2557 2558 2559 2560
    code = catalogGetExpiredDBs(pCtg, &dbs, &num);
    ASSERT_EQ(code, 0);
    printf("%d - expired dbNum:%d\n", i, num);
    if (dbs) {
S
Shengliang Guan 已提交
2561
      printf("%d - expired dbId:%" PRId64 ", vgVersion:%d\n", i, dbs->dbId, dbs->vgVersion);
wafwerar's avatar
wafwerar 已提交
2562
      taosMemoryFree(dbs);
D
dapan1121 已提交
2563 2564
      dbs = NULL;
    }
S
Shengliang Guan 已提交
2565

D
dapan1121 已提交
2566 2567 2568 2569 2570
    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 已提交
2571 2572
        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 已提交
2573
      }
wafwerar's avatar
wafwerar 已提交
2574
      taosMemoryFree(stable);
D
dapan1121 已提交
2575 2576 2577
      stable = NULL;
    }
    printf("*************************************************\n");
S
Shengliang Guan 已提交
2578

wafwerar's avatar
wafwerar 已提交
2579
    taosSsleep(2);
D
dapan1121 已提交
2580
  }
S
Shengliang Guan 已提交
2581

D
dapan1121 已提交
2582 2583 2584
  catalogDestroy();
}

D
dapan1121 已提交
2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715
TEST(apiTest, catalogRefreshDBVgInfo_test) {
  struct SCatalog  *pCtg = NULL;
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;

  ctgTestInitLogFile();

  memset(ctgTestRspFunc, 0, sizeof(ctgTestRspFunc));
  ctgTestRspIdx = 0;
  ctgTestRspFunc[0] = CTGT_RSP_VGINFO;

  ctgTestSetRspByIdx();

  initQueryModuleMsgHandle();

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

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

  code = catalogRefreshDBVgInfo(pCtg, mockPointer, ctgTestDbname);
  ASSERT_EQ(code, 0);

  catalogDestroy();
}

TEST(apiTest, catalogChkAuth_test) {
  struct SCatalog  *pCtg = NULL;
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;

  ctgTestInitLogFile();

  memset(ctgTestRspFunc, 0, sizeof(ctgTestRspFunc));
  ctgTestRspIdx = 0;
  ctgTestRspFunc[0] = CTGT_RSP_USERAUTH;

  ctgTestSetRspByIdx();

  initQueryModuleMsgHandle();

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

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

  bool pass = false;
  code = catalogChkAuth(pCtg, mockPointer, ctgTestUsername, ctgTestDbname, AUTH_TYPE_READ, &pass);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(pass, true);

  catalogDestroy();
}

TEST(apiTest, catalogRefreshGetTableCfg_test) {
  struct SCatalog  *pCtg = NULL;
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;

  ctgTestInitLogFile();

  memset(ctgTestRspFunc, 0, sizeof(ctgTestRspFunc));
  ctgTestRspIdx = 0;
  ctgTestRspFunc[0] = CTGT_RSP_VGINFO;
  ctgTestRspFunc[1] = CTGT_RSP_TBMETA;

  ctgTestSetRspByIdx();

  initQueryModuleMsgHandle();

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

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

  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);
  STableCfg *pCfg = NULL;

  code = catalogRefreshGetTableCfg(pCtg, mockPointer, &n, &pCfg);
  ASSERT_EQ(code, 0);
  ASSERT_TRUE(NULL != pCfg);
  ASSERT_EQ(pCfg->numOfColumns, ctgTestColNum);

  tFreeSTableCfgRsp((STableCfgRsp *)pCfg);
  taosMemoryFree(pCfg);

  catalogDestroy();
}

TEST(apiTest, catalogGetTableIndex_test) {
  struct SCatalog  *pCtg = NULL;
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;

  ctgTestInitLogFile();

  memset(ctgTestRspFunc, 0, sizeof(ctgTestRspFunc));
  ctgTestRspIdx = 0;
  ctgTestRspFunc[0] = CTGT_RSP_TBLINDEX;

  ctgTestSetRspByIdx();

  initQueryModuleMsgHandle();

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

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

  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);
  SArray *pRes = NULL;

  code = catalogGetTableIndex(pCtg, mockPointer, &n, &pRes);
  ASSERT_EQ(code, 0);
  ASSERT_TRUE(NULL != pRes);
  ASSERT_EQ(taosArrayGetSize(pRes), ctgTestIndexNum);

  taosArrayDestroyEx(pRes, tFreeSTableIndexInfo);

  catalogDestroy();
}


D
dapan1121 已提交
2716

S
Shengliang Guan 已提交
2717
int main(int argc, char **argv) {
2718 2719 2720
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
D
dapan1121 已提交
2721

D
dapan1121 已提交
2722
#pragma GCC diagnostic pop