catalogTests.cpp 85.5 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
extern "C" int32_t ctgdGetStatNum(char *option, void *res);
D
dapan1121 已提交
45

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

D
dapan1121 已提交
51 52
extern int32_t  clientConnRefPool;

D
dapan 已提交
53 54 55 56 57 58
enum {
  CTGT_RSP_VGINFO = 1,
  CTGT_RSP_TBMETA,
  CTGT_RSP_CTBMETA,
  CTGT_RSP_STBMETA,
  CTGT_RSP_MSTBMETA,
D
dapan1121 已提交
59
  CTGT_RSP_INDEXINFO_E,
D
dapan1121 已提交
60 61 62
  CTGT_RSP_USERAUTH,
  CTGT_RSP_TBLCFG,
  CTGT_RSP_TBLINDEX,
D
dapan1121 已提交
63 64 65 66
  CTGT_RSP_DBCFG,
  CTGT_RSP_QNODELIST,
  CTGT_RSP_UDF,
  CTGT_RSP_SVRVER,
D
dapan1121 已提交
67
  CTGT_RSP_DNODElIST,
D
dapan1121 已提交
68
  CTGT_RSP_TBMETA_NOT_EXIST,
D
dapan 已提交
69
};
D
dapan1121 已提交
70

S
Shengliang Guan 已提交
71 72
bool    ctgTestStop = false;
bool    ctgTestEnableSleep = false;
D
dapan1121 已提交
73
bool    ctgTestEnableLog = true;
S
Shengliang Guan 已提交
74
bool    ctgTestDeadLoop = false;
D
dapan1121 已提交
75
int32_t ctgTestPrintNum = 10000;
D
dapan1121 已提交
76
int32_t ctgTestMTRunSec = 5;
D
dapan1121 已提交
77

S
Shengliang Guan 已提交
78 79 80 81 82
int32_t  ctgTestCurrentVgVersion = 0;
int32_t  ctgTestVgVersion = 1;
int32_t  ctgTestVgNum = 10;
int32_t  ctgTestColNum = 2;
int32_t  ctgTestTagNum = 1;
D
dapan1121 已提交
83
int32_t  ctgTestQnodeNum = 3;
D
dapan1121 已提交
84
int32_t  ctgTestIndexNum = 3;
D
dapan1121 已提交
85 86
int32_t  ctgTestFuncNum = 2;
int32_t  ctgTestFuncType = 3;
S
Shengliang Guan 已提交
87 88 89
int32_t  ctgTestSVersion = 1;
int32_t  ctgTestTVersion = 1;
int32_t  ctgTestSuid = 2;
D
dapan1121 已提交
90
uint64_t ctgTestDbId = 33;
D
dapan1121 已提交
91
uint64_t ctgTestNormalTblUid = 1;
D
dapan1121 已提交
92

93
uint64_t ctgTestClusterId = 0x1;
S
Shengliang Guan 已提交
94 95 96 97
char    *ctgTestDbname = "1.db1";
char    *ctgTestTablename = "table1";
char    *ctgTestCTablename = "ctable1";
char    *ctgTestSTablename = "stable1";
D
dapan1121 已提交
98
char    *ctgTestUsername = "user1";
D
dapan1121 已提交
99 100 101
char    *ctgTestCurrentCTableName = NULL;
char    *ctgTestCurrentTableName = NULL;
char    *ctgTestCurrentSTableName = NULL;
D
dapan1121 已提交
102

D
dapan1121 已提交
103
int32_t ctgTestRspFunc[100] = {0};
D
dapan 已提交
104 105
int32_t ctgTestRspIdx = 0;

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

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

D
ut test  
dapan1121 已提交
132 133
  SRpcMsg rpcMsg = {0};
  rpcMsg.pCont = pReq;
S
Shengliang Guan 已提交
134
  rpcMsg.contLen = contLen;
H
Hongze Cheng 已提交
135
  rpcMsg.msgType = TDMT_MND_CREATE_DB;
D
ut test  
dapan1121 已提交
136 137 138 139 140 141 142

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

  ASSERT_EQ(rpcRsp.code, 0);
}

D
dapan1121 已提交
143
void ctgTestInitLogFile() {
D
dapan1121 已提交
144 145 146
  if (!ctgTestEnableLog) {
    return;
  }
H
Hongze Cheng 已提交
147

S
Shengliang Guan 已提交
148 149
  const char   *defaultLogFileNamePrefix = "taoslog";
  const int32_t maxLogFileNum = 10;
D
dapan1121 已提交
150 151

  tsAsyncLog = 0;
D
dapan1121 已提交
152
  qDebugFlag = 159;
wafwerar's avatar
wafwerar 已提交
153
  strcpy(tsLogDir, TD_LOG_DIR_PATH);
D
dapan1121 已提交
154

D
dapan1121 已提交
155 156 157 158
  ctgdEnableDebug("api", true);
  ctgdEnableDebug("meta", true);
  ctgdEnableDebug("cache", true);
  ctgdEnableDebug("lock", true);
H
Hongze Cheng 已提交
159

S
Shengliang Guan 已提交
160
  if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) {
D
dapan1121 已提交
161 162 163 164 165 166 167 168 169
    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 已提交
170
  SName cn = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
171 172 173
  strcpy(cn.dbname, "db1");
  strcpy(cn.tname, ctgTestCTablename);

H
Hongze Cheng 已提交
174
  SName sn = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
175 176 177
  strcpy(sn.dbname, "db1");
  strcpy(sn.tname, ctgTestSTablename);

D
dapan 已提交
178 179 180 181
  char db[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(&cn, db);

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

D
dapan1121 已提交
184 185
  strcpy(output->ctbName, cn.tname);
  strcpy(output->tbName, sn.tname);
D
dapan1121 已提交
186 187 188 189 190 191

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

H
Hongze Cheng 已提交
192 193
  output->tbMeta =
      (STableMeta *)taosMemoryCalloc(1, sizeof(STableMeta) + sizeof(SSchema) * (ctgTestColNum + ctgTestColNum));
D
dapan1121 已提交
194 195 196 197 198 199 200
  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 已提交
201

D
dapan1121 已提交
202 203
  output->tbMeta->sversion = ctgTestSVersion;
  output->tbMeta->tversion = ctgTestTVersion;
S
Shengliang Guan 已提交
204

D
dapan1121 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  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 已提交
225
void ctgTestBuildDBVgroup(SDBVgInfo **pdbVgroup) {
D
dapan1121 已提交
226
  static int32_t vgVersion = ctgTestVgVersion + 1;
S
Shengliang Guan 已提交
227 228
  int32_t        vgNum = 0;
  SVgroupInfo    vgInfo = {0};
H
Hongze Cheng 已提交
229
  SDBVgInfo     *dbVgroup = (SDBVgInfo *)taosMemoryCalloc(1, sizeof(SDBVgInfo));
S
Shengliang Guan 已提交
230

D
dapan1121 已提交
231
  dbVgroup->vgVersion = vgVersion++;
S
Shengliang Guan 已提交
232

D
dapan1121 已提交
233
  ctgTestCurrentVgVersion = dbVgroup->vgVersion;
S
Shengliang Guan 已提交
234

D
dapan1121 已提交
235
  dbVgroup->hashMethod = 0;
236 237
  dbVgroup->hashPrefix = 0;
  dbVgroup->hashSuffix = 0;
D
dapan1121 已提交
238
  dbVgroup->vgHash = taosHashInit(ctgTestVgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
D
dapan1121 已提交
239 240 241 242 243 244 245 246

  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 已提交
247 248 249 250
    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 已提交
251
      strcpy(addr->fqdn, "a0");
S
Shengliang Guan 已提交
252
      addr->port = n + 22;
D
dapan1121 已提交
253 254
    }

D
dapan1121 已提交
255
    taosHashPut(dbVgroup->vgHash, &vgInfo.vgId, sizeof(vgInfo.vgId), &vgInfo, sizeof(vgInfo));
D
dapan1121 已提交
256
  }
D
dapan 已提交
257 258

  *pdbVgroup = dbVgroup;
D
dapan1121 已提交
259 260
}

D
dapan1121 已提交
261 262 263 264 265 266 267 268 269 270
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 已提交
271 272
  rspMsg->suid = ctgTestSuid;
  rspMsg->tuid = ctgTestSuid;
D
dapan1121 已提交
273
  rspMsg->vgId = 1;
H
Hongze Cheng 已提交
274

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

  SSchema *s = NULL;
S
Shengliang Guan 已提交
278
  s = &rspMsg->pSchemas[0];
D
dapan1121 已提交
279 280 281 282 283
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
  s->colId = 1;
  s->bytes = 8;
  strcpy(s->name, "ts");

S
Shengliang Guan 已提交
284
  s = &rspMsg->pSchemas[1];
D
dapan1121 已提交
285 286 287 288 289
  s->type = TSDB_DATA_TYPE_INT;
  s->colId = 2;
  s->bytes = 4;
  strcpy(s->name, "col1s");

S
Shengliang Guan 已提交
290
  s = &rspMsg->pSchemas[2];
D
dapan1121 已提交
291 292 293 294 295 296 297 298
  s->type = TSDB_DATA_TYPE_BINARY;
  s->colId = 3;
  s->bytes = 12 + 1;
  strcpy(s->name, "tag1s");

  return;
}

D
dapan 已提交
299
void ctgTestRspDbVgroups(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
300 301
  rpcFreeCont(pMsg->pCont);
  
S
Shengliang Guan 已提交
302 303 304
  SUseDbRsp usedbRsp = {0};
  strcpy(usedbRsp.db, ctgTestDbname);
  usedbRsp.vgVersion = ctgTestVgVersion;
D
dapan1121 已提交
305
  ctgTestCurrentVgVersion = ctgTestVgVersion;
S
Shengliang Guan 已提交
306 307 308 309
  usedbRsp.vgNum = ctgTestVgNum;
  usedbRsp.hashMethod = 0;
  usedbRsp.uid = ctgTestDbId;
  usedbRsp.pVgroupInfos = taosArrayInit(usedbRsp.vgNum, sizeof(SVgroupInfo));
D
dapan1121 已提交
310

S
Shengliang Guan 已提交
311
  uint32_t hashUnit = UINT32_MAX / ctgTestVgNum;
D
dapan1121 已提交
312
  for (int32_t i = 0; i < ctgTestVgNum; ++i) {
S
Shengliang Guan 已提交
313 314 315 316 317 318 319 320
    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 已提交
321 322 323 324
    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 已提交
325
      strcpy(addr->fqdn, "a0");
S
Shengliang Guan 已提交
326
      addr->port = n + 22;
D
dapan1121 已提交
327
    }
D
dapan1121 已提交
328
    vg.numOfTable = i % 2;
S
Shengliang Guan 已提交
329 330

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

S
Shengliang Guan 已提交
333 334 335
  int32_t contLen = tSerializeSUseDbRsp(NULL, 0, &usedbRsp);
  void   *pReq = rpcMallocCont(contLen);
  tSerializeSUseDbRsp(pReq, contLen, &usedbRsp);
D
dapan1121 已提交
336

S
Shengliang Guan 已提交
337 338 339
  pRsp->code = 0;
  pRsp->contLen = contLen;
  pRsp->pCont = pReq;
340 341

  taosArrayDestroy(usedbRsp.pVgroupInfos);
D
dapan1121 已提交
342 343
}

D
dapan 已提交
344
void ctgTestRspTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
345 346
  rpcFreeCont(pMsg->pCont);
  
S
Shengliang Guan 已提交
347 348 349 350 351 352 353 354 355 356
  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 已提交
357
  metaRsp.tuid = ctgTestNormalTblUid++;
S
Shengliang Guan 已提交
358
  metaRsp.vgId = 8;
wafwerar's avatar
wafwerar 已提交
359
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc((metaRsp.numOfTags + metaRsp.numOfColumns) * sizeof(SSchema));
D
dapan1121 已提交
360 361

  SSchema *s = NULL;
S
Shengliang Guan 已提交
362
  s = &metaRsp.pSchemas[0];
D
dapan1121 已提交
363
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
S
Shengliang Guan 已提交
364 365
  s->colId = 1;
  s->bytes = 8;
D
dapan1121 已提交
366 367
  strcpy(s->name, "ts");

S
Shengliang Guan 已提交
368
  s = &metaRsp.pSchemas[1];
D
dapan1121 已提交
369
  s->type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
370 371
  s->colId = 2;
  s->bytes = 4;
D
dapan1121 已提交
372
  strcpy(s->name, "col1");
S
Shengliang Guan 已提交
373

S
Shengliang Guan 已提交
374 375 376 377 378 379 380 381 382
  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 已提交
383 384
}

D
dapan1121 已提交
385
void ctgTestRspTableMetaNotExist(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
386 387
  rpcFreeCont(pMsg->pCont);
  
D
dapan1121 已提交
388 389 390
  pRsp->code = CTG_ERR_CODE_TABLE_NOT_EXIST;
}

D
dapan 已提交
391
void ctgTestRspCTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
392 393
  rpcFreeCont(pMsg->pCont);
  
S
Shengliang Guan 已提交
394 395
  STableMetaRsp metaRsp = {0};
  strcpy(metaRsp.dbFName, ctgTestDbname);
D
dapan1121 已提交
396
  strcpy(metaRsp.tbName, ctgTestCurrentCTableName ? ctgTestCurrentCTableName : ctgTestCTablename);
S
Shengliang Guan 已提交
397 398 399 400 401 402 403 404 405 406
  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 已提交
407
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc((metaRsp.numOfTags + metaRsp.numOfColumns) * sizeof(SSchema));
D
dapan1121 已提交
408 409

  SSchema *s = NULL;
S
Shengliang Guan 已提交
410
  s = &metaRsp.pSchemas[0];
D
dapan1121 已提交
411
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
S
Shengliang Guan 已提交
412 413
  s->colId = 1;
  s->bytes = 8;
D
dapan1121 已提交
414 415
  strcpy(s->name, "ts");

S
Shengliang Guan 已提交
416
  s = &metaRsp.pSchemas[1];
D
dapan1121 已提交
417
  s->type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
418 419
  s->colId = 2;
  s->bytes = 4;
D
dapan1121 已提交
420 421
  strcpy(s->name, "col1s");

S
Shengliang Guan 已提交
422
  s = &metaRsp.pSchemas[2];
D
dapan1121 已提交
423
  s->type = TSDB_DATA_TYPE_BINARY;
S
Shengliang Guan 已提交
424 425
  s->colId = 3;
  s->bytes = 12;
D
dapan1121 已提交
426 427
  strcpy(s->name, "tag1s");

S
Shengliang Guan 已提交
428 429 430 431 432 433 434 435 436
  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 已提交
437 438
}

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

  SSchema *s = NULL;
S
Shengliang Guan 已提交
458
  s = &metaRsp.pSchemas[0];
D
dapan1121 已提交
459
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
S
Shengliang Guan 已提交
460 461
  s->colId = 1;
  s->bytes = 8;
D
dapan1121 已提交
462 463
  strcpy(s->name, "ts");

S
Shengliang Guan 已提交
464
  s = &metaRsp.pSchemas[1];
D
dapan1121 已提交
465
  s->type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
466 467
  s->colId = 2;
  s->bytes = 4;
D
dapan1121 已提交
468 469
  strcpy(s->name, "col1s");

S
Shengliang Guan 已提交
470
  s = &metaRsp.pSchemas[2];
D
dapan1121 已提交
471
  s->type = TSDB_DATA_TYPE_BINARY;
S
Shengliang Guan 已提交
472 473
  s->colId = 3;
  s->bytes = 12;
D
dapan1121 已提交
474 475
  strcpy(s->name, "tag1s");

S
Shengliang Guan 已提交
476 477 478 479 480 481 482 483 484
  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 已提交
485 486
}

D
dapan 已提交
487
void ctgTestRspMultiSTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
488 489
  rpcFreeCont(pMsg->pCont);
  
D
dapan1121 已提交
490 491
  static int32_t idx = 1;

S
Shengliang Guan 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504
  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 已提交
505
  metaRsp.pSchemas = (SSchema *)taosMemoryMalloc((metaRsp.numOfTags + metaRsp.numOfColumns) * sizeof(SSchema));
D
dapan1121 已提交
506 507

  SSchema *s = NULL;
S
Shengliang Guan 已提交
508
  s = &metaRsp.pSchemas[0];
D
dapan1121 已提交
509
  s->type = TSDB_DATA_TYPE_TIMESTAMP;
S
Shengliang Guan 已提交
510 511
  s->colId = 1;
  s->bytes = 8;
D
dapan1121 已提交
512 513
  strcpy(s->name, "ts");

S
Shengliang Guan 已提交
514
  s = &metaRsp.pSchemas[1];
D
dapan1121 已提交
515
  s->type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
516 517
  s->colId = 2;
  s->bytes = 4;
D
dapan1121 已提交
518 519
  strcpy(s->name, "col1s");

S
Shengliang Guan 已提交
520
  s = &metaRsp.pSchemas[2];
D
dapan1121 已提交
521
  s->type = TSDB_DATA_TYPE_BINARY;
S
Shengliang Guan 已提交
522 523
  s->colId = 3;
  s->bytes = 12;
D
dapan1121 已提交
524 525 526
  strcpy(s->name, "tag1s");

  ++idx;
S
Shengliang Guan 已提交
527

S
Shengliang Guan 已提交
528 529 530 531 532 533 534
  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 已提交
535

S
Shengliang Guan 已提交
536 537
  tFreeSTableMetaRsp(&metaRsp);
}
S
Shengliang Guan 已提交
538

D
dapan1121 已提交
539 540

void ctgTestRspErrIndexInfo(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
541 542
  rpcFreeCont(pMsg->pCont);
  
D
dapan1121 已提交
543 544 545 546 547 548
  pRsp->code = TSDB_CODE_MND_DB_INDEX_NOT_EXIST;
  pRsp->contLen = 0;
  pRsp->pCont = NULL;
}


D
dapan1121 已提交
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
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);
}

D
dapan1121 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
void ctgTestRspDBCfg(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  rpcFreeCont(pMsg->pCont);
  
  static int32_t idx = 1;

  SDbCfgRsp dbRsp = {0};
  dbRsp.numOfVgroups = ctgTestVgNum;
  
  int32_t contLen = tSerializeSDbCfgRsp(NULL, 0, &dbRsp);
  void   *pReq = rpcMallocCont(contLen);
  tSerializeSDbCfgRsp(pReq, contLen, &dbRsp);

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

void ctgTestRspQnodeList(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  rpcFreeCont(pMsg->pCont);

  SQnodeListRsp qlistRsp = {0};
  qlistRsp.qnodeList = taosArrayInit(10, sizeof(SQueryNodeLoad));
  for (int32_t i = 0; i < ctgTestQnodeNum; ++i) {
    SQueryNodeLoad nodeLoad = {0};
    nodeLoad.addr.nodeId = i;
    
    (void)taosArrayPush(qlistRsp.qnodeList, &nodeLoad);
  }

  int32_t rspLen = tSerializeSQnodeListRsp(NULL, 0, &qlistRsp);
  void   *pReq = rpcMallocCont(rspLen);
  tSerializeSQnodeListRsp(pReq, rspLen, &qlistRsp);

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

  tFreeSQnodeListRsp(&qlistRsp);
}

void ctgTestRspUdfInfo(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  rpcFreeCont(pMsg->pCont);

  SRetrieveFuncRsp funcRsp = {0};
  funcRsp.numOfFuncs = 1;
  funcRsp.pFuncInfos = taosArrayInit(1, sizeof(SFuncInfo));
675
  funcRsp.pFuncVersions = taosArrayInit(1, sizeof(int32_t));
D
dapan1121 已提交
676 677 678 679 680
  SFuncInfo funcInfo = {0};
  strcpy(funcInfo.name, "func1");
  funcInfo.funcType = ctgTestFuncType;
    
  (void)taosArrayPush(funcRsp.pFuncInfos, &funcInfo);
681 682 683
  int32_t version = 0;
  (void)taosArrayPush(funcRsp.pFuncVersions, &version);
  
D
dapan1121 已提交
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
  int32_t rspLen = tSerializeSRetrieveFuncRsp(NULL, 0, &funcRsp);
  void   *pReq = rpcMallocCont(rspLen);
  tSerializeSRetrieveFuncRsp(pReq, rspLen, &funcRsp);

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

  tFreeSRetrieveFuncRsp(&funcRsp);
}

void ctgTestRspSvrVer(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  rpcFreeCont(pMsg->pCont);

  SServerVerRsp verRsp = {0};
  strcpy(verRsp.ver, "1.0");
  
  int32_t rspLen = tSerializeSServerVerRsp(NULL, 0, &verRsp);
  void   *pReq = rpcMallocCont(rspLen);
  tSerializeSServerVerRsp(pReq, rspLen, &verRsp);

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

D
dapan1121 已提交
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
void ctgTestRspDndeList(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  rpcFreeCont(pMsg->pCont);

  SDnodeListRsp dRsp = {0};
  dRsp.dnodeList = taosArrayInit(1, sizeof(SEpSet));
  SEpSet epSet = {0};
  epSet.numOfEps = 1;
  tstrncpy(epSet.eps[0].fqdn, "localhost", TSDB_FQDN_LEN);
  epSet.eps[0].port = 6030;
  
  (void)taosArrayPush(dRsp.dnodeList, &epSet);
  
  int32_t rspLen = tSerializeSDnodeListRsp(NULL, 0, &dRsp);
  void   *pReq = rpcMallocCont(rspLen);
  tSerializeSDnodeListRsp(pReq, rspLen, &dRsp);

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

  tFreeSDnodeListRsp(&dRsp);
}


D
dapan1121 已提交
734 735 736 737 738 739 740 741 742 743 744 745 746

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;
D
dapan1121 已提交
747 748 749 750 751 752 753 754 755 756 757 758
    case TDMT_MND_GET_DB_CFG:
      ctgTestRspDBCfg(shandle, pEpSet, pMsg, pRsp);
      break;
    case TDMT_MND_QNODE_LIST:
      ctgTestRspQnodeList(shandle, pEpSet, pMsg, pRsp);  
      break;
    case TDMT_MND_RETRIEVE_FUNC:
      ctgTestRspUdfInfo(shandle, pEpSet, pMsg, pRsp);
      break;
    case TDMT_MND_SERVER_VERSION:
      ctgTestRspSvrVer(shandle, pEpSet, pMsg, pRsp);
      break;
D
dapan1121 已提交
759 760 761
    case TDMT_MND_DNODE_LIST:
      ctgTestRspDndeList(shandle, pEpSet, pMsg, pRsp);
      break;
D
dapan1121 已提交
762 763 764 765 766 767 768 769
    default:
      break;
  }

  return;
}


D
dapan 已提交
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
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 已提交
787 788 789
    case CTGT_RSP_INDEXINFO_E:
      ctgTestRspErrIndexInfo(shandle, pEpSet, pMsg, pRsp);
      break;
D
dapan1121 已提交
790 791 792 793 794 795
    case CTGT_RSP_USERAUTH:
      ctgTestRspUserAuth(shandle, pEpSet, pMsg, pRsp);
      break;
    case CTGT_RSP_TBLCFG:
      ctgTestRspTableCfg(shandle, pEpSet, pMsg, pRsp);
      break;
D
dapan1121 已提交
796 797 798
    case CTGT_RSP_TBMETA_NOT_EXIST:
      ctgTestRspTableMetaNotExist(shandle, pEpSet, pMsg, pRsp);
      break;
D
dapan1121 已提交
799 800 801
    case CTGT_RSP_TBLINDEX:
      ctgTestRspTableIndex(shandle, pEpSet, pMsg, pRsp);
      break;
D
dapan1121 已提交
802 803 804 805 806 807 808 809 810 811 812 813
    case CTGT_RSP_DBCFG:
      ctgTestRspDBCfg(shandle, pEpSet, pMsg, pRsp);
      break;
    case CTGT_RSP_QNODELIST:
      ctgTestRspQnodeList(shandle, pEpSet, pMsg, pRsp);
      break;
    case CTGT_RSP_UDF:
      ctgTestRspUdfInfo(shandle, pEpSet, pMsg, pRsp);
      break;
    case CTGT_RSP_SVRVER:
      ctgTestRspSvrVer(shandle, pEpSet, pMsg, pRsp);
      break;
D
dapan1121 已提交
814 815 816
    case CTGT_RSP_DNODElIST:
      ctgTestRspDndeList(shandle, pEpSet, pMsg, pRsp);
      break;
D
dapan 已提交
817
    default:
D
dapan1121 已提交
818
      ctgTestRspAuto(shandle, pEpSet, pMsg, pRsp);
D
dapan 已提交
819 820 821 822
      break;
  }

  ctgTestRspIdx++;
S
Shengliang Guan 已提交
823

D
dapan 已提交
824 825 826
  return;
}

D
dapan 已提交
827 828 829 830
void ctgTestRspDbVgroupsAndNormalMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);

  ctgTestSetRspTableMeta();
S
Shengliang Guan 已提交
831

D
dapan 已提交
832 833 834
  return;
}

D
dapan 已提交
835 836
void ctgTestRspDbVgroupsAndChildMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);
S
Shengliang Guan 已提交
837

D
dapan 已提交
838
  ctgTestSetRspCTableMeta();
S
Shengliang Guan 已提交
839

D
dapan 已提交
840 841 842
  return;
}

D
dapan 已提交
843 844
void ctgTestRspDbVgroupsAndSuperMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);
S
Shengliang Guan 已提交
845

D
dapan 已提交
846
  ctgTestSetRspSTableMeta();
S
Shengliang Guan 已提交
847

D
dapan1121 已提交
848 849 850
  return;
}

D
dapan 已提交
851 852
void ctgTestRspDbVgroupsAndMultiSuperMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);
S
Shengliang Guan 已提交
853

D
dapan 已提交
854
  ctgTestSetRspMultiSTableMeta();
S
Shengliang Guan 已提交
855

D
dapan1121 已提交
856 857 858
  return;
}

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

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

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

D
dapan 已提交
919
void ctgTestSetRspSTableMeta() {
D
dapan1121 已提交
920
  static Stub stub;
D
dapan 已提交
921
  stub.set(rpcSendRecv, ctgTestRspSTableMeta);
D
dapan1121 已提交
922
  {
wafwerar's avatar
wafwerar 已提交
923
#ifdef WINDOWS
H
Hongze Cheng 已提交
924 925
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
926 927 928
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
929 930
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
931
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
932
#endif
S
Shengliang Guan 已提交
933
    for (const auto &f : result) {
D
dapan 已提交
934
      stub.set(f.second, ctgTestRspSTableMeta);
D
dapan1121 已提交
935 936 937 938
    }
  }
}

D
dapan 已提交
939
void ctgTestSetRspMultiSTableMeta() {
D
dapan1121 已提交
940
  static Stub stub;
D
dapan 已提交
941
  stub.set(rpcSendRecv, ctgTestRspMultiSTableMeta);
D
dapan1121 已提交
942
  {
wafwerar's avatar
wafwerar 已提交
943
#ifdef WINDOWS
H
Hongze Cheng 已提交
944 945
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
946 947 948
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
949 950
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
951
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
952
#endif
S
Shengliang Guan 已提交
953
    for (const auto &f : result) {
D
dapan 已提交
954
      stub.set(f.second, ctgTestRspMultiSTableMeta);
D
dapan1121 已提交
955 956 957 958
    }
  }
}

D
dapan 已提交
959
void ctgTestSetRspByIdx() {
D
dapan1121 已提交
960
  static Stub stub;
D
dapan 已提交
961
  stub.set(rpcSendRecv, ctgTestRspByIdx);
D
dapan1121 已提交
962
  {
wafwerar's avatar
wafwerar 已提交
963
#ifdef WINDOWS
H
Hongze Cheng 已提交
964 965
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
966 967 968
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
969 970
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
971
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
972
#endif
S
Shengliang Guan 已提交
973
    for (const auto &f : result) {
D
dapan 已提交
974
      stub.set(f.second, ctgTestRspByIdx);
D
dapan1121 已提交
975 976 977 978
    }
  }
}

D
dapan 已提交
979
void ctgTestSetRspDbVgroupsAndNormalMeta() {
D
dapan 已提交
980
  static Stub stub;
D
dapan 已提交
981
  stub.set(rpcSendRecv, ctgTestRspDbVgroupsAndNormalMeta);
D
dapan 已提交
982
  {
wafwerar's avatar
wafwerar 已提交
983
#ifdef WINDOWS
H
Hongze Cheng 已提交
984 985
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
986 987 988
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
989 990
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan 已提交
991
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
992
#endif
S
Shengliang Guan 已提交
993
    for (const auto &f : result) {
D
dapan 已提交
994
      stub.set(f.second, ctgTestRspDbVgroupsAndNormalMeta);
D
dapan 已提交
995 996 997 998
    }
  }
}

D
dapan 已提交
999
void ctgTestSetRspDbVgroupsAndChildMeta() {
D
dapan1121 已提交
1000
  static Stub stub;
D
dapan 已提交
1001
  stub.set(rpcSendRecv, ctgTestRspDbVgroupsAndChildMeta);
D
dapan1121 已提交
1002
  {
wafwerar's avatar
wafwerar 已提交
1003
#ifdef WINDOWS
H
Hongze Cheng 已提交
1004 1005
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
1006 1007 1008
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
1009 1010
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
1011
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
1012
#endif
S
Shengliang Guan 已提交
1013
    for (const auto &f : result) {
D
dapan 已提交
1014
      stub.set(f.second, ctgTestRspDbVgroupsAndChildMeta);
D
dapan1121 已提交
1015 1016 1017 1018
    }
  }
}

D
dapan 已提交
1019
void ctgTestSetRspDbVgroupsAndSuperMeta() {
D
dapan1121 已提交
1020
  static Stub stub;
D
dapan 已提交
1021
  stub.set(rpcSendRecv, ctgTestRspDbVgroupsAndSuperMeta);
D
dapan1121 已提交
1022
  {
wafwerar's avatar
wafwerar 已提交
1023
#ifdef WINDOWS
H
Hongze Cheng 已提交
1024 1025
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
1026 1027 1028
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
1029 1030
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
1031
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
1032
#endif
S
Shengliang Guan 已提交
1033
    for (const auto &f : result) {
D
dapan 已提交
1034
      stub.set(f.second, ctgTestRspDbVgroupsAndSuperMeta);
D
dapan1121 已提交
1035 1036 1037 1038
    }
  }
}

D
dapan 已提交
1039
void ctgTestSetRspDbVgroupsAndMultiSuperMeta() {
D
dapan1121 已提交
1040
  static Stub stub;
D
dapan 已提交
1041
  stub.set(rpcSendRecv, ctgTestRspDbVgroupsAndMultiSuperMeta);
D
dapan1121 已提交
1042
  {
wafwerar's avatar
wafwerar 已提交
1043
#ifdef WINDOWS
H
Hongze Cheng 已提交
1044 1045
    AddrAny                       any;
    std::map<std::string, void *> result;
wafwerar's avatar
wafwerar 已提交
1046 1047 1048
    any.get_func_addr("rpcSendRecv", result);
#endif
#ifdef LINUX
S
Shengliang Guan 已提交
1049 1050
    AddrAny                       any("libtransport.so");
    std::map<std::string, void *> result;
D
dapan1121 已提交
1051
    any.get_global_func_addr_dynsym("^rpcSendRecv$", result);
wafwerar's avatar
wafwerar 已提交
1052
#endif
S
Shengliang Guan 已提交
1053
    for (const auto &f : result) {
D
dapan 已提交
1054
      stub.set(f.second, ctgTestRspDbVgroupsAndMultiSuperMeta);
D
dapan1121 已提交
1055 1056 1057 1058
    }
  }
}

S
Shengliang Guan 已提交
1059
}  // namespace
1060

D
dapan1121 已提交
1061
void *ctgTestGetDbVgroupThread(void *param) {
H
Hongze Cheng 已提交
1062 1063
  struct SCatalog  *pCtg = (struct SCatalog *)param;
  int32_t           code = 0;
D
dapan1121 已提交
1064 1065
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
1066 1067
  SArray           *vgList = NULL;
  int32_t           n = 0;
S
Shengliang Guan 已提交
1068

D
dapan1121 已提交
1069
  while (!ctgTestStop) {
1070
    code = catalogGetDBVgList(pCtg, mockPointer, ctgTestDbname, &vgList);
D
dapan1121 已提交
1071
    if (code) {
D
dapan1121 已提交
1072
      printf("code:%x\n", code);
D
dapan1121 已提交
1073 1074 1075 1076 1077 1078 1079 1080
      assert(0);
    }

    if (vgList) {
      taosArrayDestroy(vgList);
    }

    if (ctgTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
1081
      taosUsleep(taosRand() % 5);
D
dapan1121 已提交
1082
    }
D
dapan1121 已提交
1083
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
1084 1085 1086 1087 1088 1089 1090
      printf("Get:%d\n", n);
    }
  }

  return NULL;
}

D
dapan1121 已提交
1091
void *ctgTestSetSameDbVgroupThread(void *param) {
S
Shengliang Guan 已提交
1092 1093
  struct SCatalog *pCtg = (struct SCatalog *)param;
  int32_t          code = 0;
D
dapan1121 已提交
1094
  SDBVgInfo       *dbVgroup = NULL;
S
Shengliang Guan 已提交
1095 1096
  int32_t          n = 0;

D
dapan1121 已提交
1097 1098
  while (!ctgTestStop) {
    ctgTestBuildDBVgroup(&dbVgroup);
D
dapan1121 已提交
1099
    code = catalogUpdateDBVgInfo(pCtg, ctgTestDbname, ctgTestDbId, dbVgroup);
D
dapan1121 已提交
1100 1101 1102 1103
    if (code) {
      assert(0);
    }

S
Shengliang Guan 已提交
1104
    if (ctgTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
1105
      taosUsleep(taosRand() % 5);
D
dapan1121 已提交
1106
    }
D
dapan1121 已提交
1107
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
1108 1109 1110 1111 1112 1113 1114
      printf("Set:%d\n", n);
    }
  }

  return NULL;
}

D
dapan1121 已提交
1115 1116 1117
void *ctgTestSetDiffDbVgroupThread(void *param) {
  struct SCatalog *pCtg = (struct SCatalog *)param;
  int32_t          code = 0;
H
Hongze Cheng 已提交
1118
  SDBVgInfo       *dbVgroup = NULL;
D
dapan1121 已提交
1119 1120 1121 1122
  int32_t          n = 0;

  while (!ctgTestStop) {
    ctgTestBuildDBVgroup(&dbVgroup);
D
dapan1121 已提交
1123
    code = catalogUpdateDBVgInfo(pCtg, ctgTestDbname, ctgTestDbId++, dbVgroup);
D
dapan1121 已提交
1124 1125 1126 1127 1128
    if (code) {
      assert(0);
    }

    if (ctgTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
1129
      taosUsleep(taosRand() % 5);
D
dapan1121 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138
    }
    if (++n % ctgTestPrintNum == 0) {
      printf("Set:%d\n", n);
    }
  }

  return NULL;
}

D
dapan1121 已提交
1139
void *ctgTestGetCtableMetaThread(void *param) {
S
Shengliang Guan 已提交
1140 1141 1142 1143
  struct SCatalog *pCtg = (struct SCatalog *)param;
  int32_t          code = 0;
  int32_t          n = 0;
  STableMeta      *tbMeta = NULL;
D
dapan1121 已提交
1144
  bool             inCache = false;
D
dapan1121 已提交
1145

H
Hongze Cheng 已提交
1146
  SName cn = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1147 1148
  strcpy(cn.dbname, "db1");
  strcpy(cn.tname, ctgTestCTablename);
S
Shengliang Guan 已提交
1149

D
dapan1121 已提交
1150 1151 1152 1153
  SCtgTbMetaCtx ctx = {0};
  ctx.pName = &cn;
  ctx.flag = CTG_FLAG_UNKNOWN_STB;

D
dapan1121 已提交
1154
  while (!ctgTestStop) {
D
dapan1121 已提交
1155
    code = ctgReadTbMetaFromCache(pCtg, &ctx, &tbMeta);
D
dapan1121 已提交
1156
    if (code || NULL == tbMeta) {
D
dapan1121 已提交
1157 1158 1159
      assert(0);
    }

wafwerar's avatar
wafwerar 已提交
1160
    taosMemoryFreeClear(tbMeta);
D
dapan1121 已提交
1161

D
dapan1121 已提交
1162
    if (ctgTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
1163
      taosUsleep(taosRand() % 5);
D
dapan1121 已提交
1164
    }
S
Shengliang Guan 已提交
1165

D
dapan1121 已提交
1166
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
1167 1168 1169 1170 1171 1172 1173 1174
      printf("Get:%d\n", n);
    }
  }

  return NULL;
}

void *ctgTestSetCtableMetaThread(void *param) {
H
Hongze Cheng 已提交
1175 1176 1177 1178
  struct SCatalog  *pCtg = (struct SCatalog *)param;
  int32_t           code = 0;
  SDBVgInfo         dbVgroup = {0};
  int32_t           n = 0;
D
dapan 已提交
1179
  STableMetaOutput *output = NULL;
D
dapan1121 已提交
1180

D
dapan1121 已提交
1181
  SCtgCacheOperation operation = {0};
H
Hongze Cheng 已提交
1182

D
dapan1121 已提交
1183
  operation.opId = CTG_OP_UPDATE_TB_META;
S
Shengliang Guan 已提交
1184

D
dapan1121 已提交
1185
  while (!ctgTestStop) {
wafwerar's avatar
wafwerar 已提交
1186
    output = (STableMetaOutput *)taosMemoryMalloc(sizeof(STableMetaOutput));
D
dapan 已提交
1187 1188
    ctgTestBuildCTableMetaOutput(output);

D
dapan1121 已提交
1189
    SCtgUpdateTbMetaMsg *msg = (SCtgUpdateTbMetaMsg *)taosMemoryMalloc(sizeof(SCtgUpdateTbMetaMsg));
D
dapan1121 已提交
1190
    msg->pCtg = pCtg;
D
dapan1121 已提交
1191
    msg->pMeta = output;
D
dapan1121 已提交
1192
    operation.data = msg;
D
dapan1121 已提交
1193

D
dapan1121 已提交
1194
    code = ctgOpUpdateTbMeta(&operation);
D
dapan1121 已提交
1195 1196 1197 1198
    if (code) {
      assert(0);
    }

S
Shengliang Guan 已提交
1199
    if (ctgTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
1200
      taosUsleep(taosRand() % 5);
D
dapan1121 已提交
1201
    }
D
dapan1121 已提交
1202
    if (++n % ctgTestPrintNum == 0) {
D
dapan1121 已提交
1203 1204 1205 1206 1207 1208 1209
      printf("Set:%d\n", n);
    }
  }

  return NULL;
}

D
dapan 已提交
1210

D
dapan1121 已提交
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
void ctgTestFetchRows(TAOS_RES *result, int32_t *rows) {
  TAOS_ROW    row;
  int         num_fields = taos_num_fields(result);
  TAOS_FIELD *fields = taos_fetch_fields(result);
  char        temp[256];

  // fetch the records row by row
  while ((row = taos_fetch_row(result))) {
    (*rows)++;
    memset(temp, 0, sizeof(temp));
    taos_print_row(temp, row, fields, num_fields);
    printf("\t[%s]\n", temp);
  }
}

void ctgTestExecQuery(TAOS    * taos, char* sql, bool fetch, int32_t *rows) {
  TAOS_RES *result = taos_query(taos, sql);
  int code = taos_errno(result);
  ASSERT_EQ(code, 0);

  if (fetch) {
    ctgTestFetchRows(result, rows);
  }
  
  taos_free_result(result);
}


D
dapan 已提交
1239
TEST(tableMeta, normalTable) {
H
Hongze Cheng 已提交
1240 1241
  struct SCatalog  *pCtg = NULL;
  SVgroupInfo       vgInfo = {0};
D
dapan1121 已提交
1242 1243
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
1244

D
dapan1121 已提交
1245 1246
  ctgTestInitLogFile();

D
dapan 已提交
1247
  ctgTestSetRspDbVgroups();
D
dapan1121 已提交
1248

1249
  initQueryModuleMsgHandle();
D
ut test  
dapan1121 已提交
1250

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

D
dapan1121 已提交
1253 1254
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);
1255

D
dapan1121 已提交
1256
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan1121 已提交
1257
  ASSERT_EQ(code, 0);
1258

H
Hongze Cheng 已提交
1259
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
1260 1261 1262
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

D
dapan1121 已提交
1263
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
1264
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
1265
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
1266
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
1267

D
dapan1121 已提交
1268 1269 1270 1271 1272 1273 1274 1275
  while (true) {
    uint64_t n = 0;
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
    if (n != 1) {
      taosMsleep(50);
    } else {
      break;
    }
D
dapan 已提交
1276
  }
H
Hongze Cheng 已提交
1277

D
dapan1121 已提交
1278 1279
  memset(&vgInfo, 0, sizeof(vgInfo));
  bool exists = false;
D
dapan1121 已提交
1280
  code = catalogGetCachedTableHashVgroup(pCtg, &n, &vgInfo, &exists);
D
dapan1121 已提交
1281 1282 1283 1284 1285
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
  ASSERT_EQ(exists, true);

D
dapan 已提交
1286
  ctgTestSetRspTableMeta();
D
dapan1121 已提交
1287 1288

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1289
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1290 1291 1292
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 8);
  ASSERT_EQ(tableMeta->tableType, TSDB_NORMAL_TABLE);
D
dapan1121 已提交
1293
  ASSERT_EQ(tableMeta->uid, ctgTestNormalTblUid - 1);
D
dapan1121 已提交
1294 1295 1296 1297 1298 1299 1300
  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);

1301 1302
  taosMemoryFree(tableMeta);

D
dapan 已提交
1303
  while (true) {
D
dapan1121 已提交
1304
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
1305
    if (0 == n) {
wafwerar's avatar
wafwerar 已提交
1306
      taosMsleep(50);
D
dapan 已提交
1307 1308 1309
    } else {
      break;
    }
D
dapan 已提交
1310 1311
  }

D
dapan1121 已提交
1312
  tableMeta = NULL;
D
dapan1121 已提交
1313
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
  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);

1324 1325
  taosMemoryFree(tableMeta);

D
dapan1121 已提交
1326
  tableMeta = NULL;
D
dapan1121 已提交
1327
  catalogGetCachedTableMeta(pCtg, &n, &tableMeta);
D
dapan1121 已提交
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
  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 已提交
1338
  SDbVgVersion   *dbs = NULL;
D
dapan1121 已提交
1339
  SSTableVersion *stb = NULL;
H
Hongze Cheng 已提交
1340 1341
  uint32_t        dbNum = 0, stbNum = 0, allDbNum = 0, allStbNum = 0;
  int32_t         i = 0;
D
dapan1121 已提交
1342 1343 1344 1345 1346 1347
  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 已提交
1348

D
dapan1121 已提交
1349
    if (dbNum) {
S
Shengliang Guan 已提交
1350
      printf("got expired db,dbId:%" PRId64 "\n", dbs->dbId);
wafwerar's avatar
wafwerar 已提交
1351
      taosMemoryFree(dbs);
D
dapan1121 已提交
1352 1353 1354 1355 1356 1357
      dbs = NULL;
    } else {
      printf("no expired db\n");
    }

    if (stbNum) {
D
dapan 已提交
1358
      printf("got expired stb,suid:%" PRId64 ",dbFName:%s, stbName:%s\n", stb->suid, stb->dbFName, stb->stbName);
wafwerar's avatar
wafwerar 已提交
1359
      taosMemoryFree(stb);
D
dapan1121 已提交
1360 1361 1362 1363 1364 1365 1366
      stb = NULL;
    } else {
      printf("no expired stb\n");
    }

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

D
dapan1121 已提交
1370 1371 1372
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 0);

D
dapan1121 已提交
1373 1374 1375
  catalogDestroy();
}

D
dapan 已提交
1376
TEST(tableMeta, childTableCase) {
H
Hongze Cheng 已提交
1377
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1378 1379
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
1380
  SVgroupInfo       vgInfo = {0};
D
dapan1121 已提交
1381

D
dapan1121 已提交
1382 1383
  ctgTestInitLogFile();

D
dapan 已提交
1384
  ctgTestSetRspDbVgroupsAndChildMeta();
D
dapan1121 已提交
1385 1386 1387

  initQueryModuleMsgHandle();

S
Shengliang Guan 已提交
1388
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
D
dapan1121 已提交
1389 1390
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);
S
Shengliang Guan 已提交
1391

D
dapan1121 已提交
1392
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan1121 已提交
1393 1394
  ASSERT_EQ(code, 0);

H
Hongze Cheng 已提交
1395
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
1396 1397
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestCTablename);
D
dapan1121 已提交
1398 1399

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1400
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
  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);

1411 1412
  taosMemoryFree(tableMeta);

D
dapan 已提交
1413
  while (true) {
D
dapan1121 已提交
1414
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
1415
    if (0 == n) {
wafwerar's avatar
wafwerar 已提交
1416
      taosMsleep(50);
D
dapan 已提交
1417 1418 1419
    } else {
      break;
    }
D
dapan 已提交
1420 1421
  }

D
dapan1121 已提交
1422
  tableMeta = NULL;
D
dapan1121 已提交
1423
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
  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 已提交
1434
  taosMemoryFreeClear(tableMeta);
H
Haojun Liao 已提交
1435 1436

  strcpy(n.tname, ctgTestSTablename);
D
dapan1121 已提交
1437
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
  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);

1448 1449
  taosMemoryFree(tableMeta);

H
Hongze Cheng 已提交
1450
  SDbVgVersion   *dbs = NULL;
D
dapan1121 已提交
1451
  SSTableVersion *stb = NULL;
H
Hongze Cheng 已提交
1452 1453
  uint32_t        dbNum = 0, stbNum = 0, allDbNum = 0, allStbNum = 0;
  int32_t         i = 0;
D
dapan1121 已提交
1454 1455 1456 1457 1458 1459
  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 已提交
1460

D
dapan1121 已提交
1461
    if (dbNum) {
S
Shengliang Guan 已提交
1462
      printf("got expired db,dbId:%" PRId64 "\n", dbs->dbId);
wafwerar's avatar
wafwerar 已提交
1463
      taosMemoryFree(dbs);
D
dapan1121 已提交
1464 1465 1466 1467 1468 1469
      dbs = NULL;
    } else {
      printf("no expired db\n");
    }

    if (stbNum) {
S
Shengliang Guan 已提交
1470
      printf("got expired stb,suid:%" PRId64 ",dbFName:%s, stbName:%s\n", stb->suid, stb->dbFName, stb->stbName);
wafwerar's avatar
wafwerar 已提交
1471
      taosMemoryFree(stb);
D
dapan1121 已提交
1472 1473 1474 1475 1476 1477 1478
      stb = NULL;
    } else {
      printf("no expired stb\n");
    }

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

D
dapan1121 已提交
1482 1483 1484
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 1);

D
dapan1121 已提交
1485 1486 1487
  catalogDestroy();
}

D
dapan 已提交
1488
TEST(tableMeta, superTableCase) {
H
Hongze Cheng 已提交
1489
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1490 1491
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
1492
  SVgroupInfo       vgInfo = {0};
D
dapan1121 已提交
1493

D
dapan 已提交
1494
  ctgTestSetRspDbVgroupsAndSuperMeta();
D
dapan1121 已提交
1495 1496 1497

  initQueryModuleMsgHandle();

D
dapan1121 已提交
1498 1499 1500
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

S
Shengliang Guan 已提交
1501
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
D
dapan1121 已提交
1502
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan1121 已提交
1503 1504
  ASSERT_EQ(code, 0);

H
Hongze Cheng 已提交
1505
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
1506 1507
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestSTablename);
D
dapan1121 已提交
1508 1509

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1510
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1511 1512 1513 1514 1515
  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 已提交
1516 1517
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
1518 1519 1520 1521 1522
  ASSERT_EQ(tableMeta->tableInfo.numOfColumns, ctgTestColNum);
  ASSERT_EQ(tableMeta->tableInfo.numOfTags, ctgTestTagNum);
  ASSERT_EQ(tableMeta->tableInfo.precision, 1);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);

1523 1524
  taosMemoryFree(tableMeta);

D
dapan 已提交
1525
  while (true) {
D
dapan1121 已提交
1526
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
1527
    if (0 == n) {
wafwerar's avatar
wafwerar 已提交
1528
      taosMsleep(50);
D
dapan 已提交
1529 1530 1531
    } else {
      break;
    }
D
dapan 已提交
1532 1533
  }

D
dapan1121 已提交
1534
  tableMeta = NULL;
D
dapan1121 已提交
1535
  code = catalogGetCachedSTableMeta(pCtg, &n, &tableMeta);
D
dapan1121 已提交
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
  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 已提交
1549
  ctgTestSetRspCTableMeta();
D
dapan1121 已提交
1550 1551

  tableMeta = NULL;
H
Haojun Liao 已提交
1552 1553 1554

  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestCTablename);
D
dapan1121 已提交
1555
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
  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);

1566 1567
  taosMemoryFree(tableMeta);

D
dapan 已提交
1568
  while (true) {
D
dapan1121 已提交
1569
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
1570
    if (2 != n) {
wafwerar's avatar
wafwerar 已提交
1571
      taosMsleep(50);
D
dapan 已提交
1572 1573 1574
    } else {
      break;
    }
D
dapan 已提交
1575 1576
  }

D
dapan1121 已提交
1577
  tableMeta = NULL;
D
dapan1121 已提交
1578
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
  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);

1589 1590
  taosMemoryFree(tableMeta);

H
Hongze Cheng 已提交
1591
  SDbVgVersion   *dbs = NULL;
D
dapan1121 已提交
1592
  SSTableVersion *stb = NULL;
H
Hongze Cheng 已提交
1593 1594
  uint32_t        dbNum = 0, stbNum = 0, allDbNum = 0, allStbNum = 0;
  int32_t         i = 0;
D
dapan1121 已提交
1595 1596 1597 1598 1599 1600
  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 已提交
1601

D
dapan1121 已提交
1602
    if (dbNum) {
S
Shengliang Guan 已提交
1603
      printf("got expired db,dbId:%" PRId64 "\n", dbs->dbId);
wafwerar's avatar
wafwerar 已提交
1604
      taosMemoryFree(dbs);
D
dapan1121 已提交
1605 1606 1607 1608 1609 1610
      dbs = NULL;
    } else {
      printf("no expired db\n");
    }

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

wafwerar's avatar
wafwerar 已提交
1613
      taosMemoryFree(stb);
D
dapan1121 已提交
1614 1615 1616 1617 1618 1619 1620
      stb = NULL;
    } else {
      printf("no expired stb\n");
    }

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

D
dapan1121 已提交
1624 1625
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 1);
D
dapan1121 已提交
1626 1627

  catalogDestroy();
D
dapan1121 已提交
1628
}
1629

D
dapan1121 已提交
1630
TEST(tableMeta, rmStbMeta) {
H
Hongze Cheng 已提交
1631
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1632 1633
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
1634
  SVgroupInfo       vgInfo = {0};
D
dapan1121 已提交
1635 1636 1637

  ctgTestInitLogFile();

D
dapan 已提交
1638
  ctgTestSetRspDbVgroupsAndSuperMeta();
D
dapan1121 已提交
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648

  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 已提交
1649
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1650 1651 1652 1653
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestSTablename);

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1654
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1655 1656 1657 1658 1659
  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 已提交
1660 1661
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
1662 1663 1664 1665 1666
  ASSERT_EQ(tableMeta->tableInfo.numOfColumns, ctgTestColNum);
  ASSERT_EQ(tableMeta->tableInfo.numOfTags, ctgTestTagNum);
  ASSERT_EQ(tableMeta->tableInfo.precision, 1);
  ASSERT_EQ(tableMeta->tableInfo.rowSize, 12);

1667 1668
  taosMemoryFree(tableMeta);

D
dapan 已提交
1669
  while (true) {
D
dapan1121 已提交
1670
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
1671
    if (0 == n) {
wafwerar's avatar
wafwerar 已提交
1672
      taosMsleep(50);
D
dapan 已提交
1673 1674 1675
    } else {
      break;
    }
D
dapan 已提交
1676 1677
  }

D
dapan1121 已提交
1678
  code = catalogRemoveStbMeta(pCtg, "1.db1", ctgTestDbId, ctgTestSTablename, ctgTestSuid);
D
dapan1121 已提交
1679 1680
  ASSERT_EQ(code, 0);

D
dapan 已提交
1681
  while (true) {
D
dapan1121 已提交
1682 1683
    int32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
    int32_t m = ctgdGetClusterCacheNum(pCtg, CTG_DBG_STB_RENT_NUM);
D
dapan 已提交
1684
    if (n || m) {
wafwerar's avatar
wafwerar 已提交
1685
      taosMsleep(50);
D
dapan 已提交
1686 1687 1688
    } else {
      break;
    }
D
dapan 已提交
1689 1690
  }

D
dapan1121 已提交
1691 1692 1693 1694 1695
  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 已提交
1696

D
dapan1121 已提交
1697 1698 1699 1700
  catalogDestroy();
}

TEST(tableMeta, updateStbMeta) {
H
Hongze Cheng 已提交
1701
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1702 1703
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
1704
  SVgroupInfo       vgInfo = {0};
D
dapan1121 已提交
1705 1706 1707

  ctgTestInitLogFile();

D
dapan 已提交
1708
  ctgTestSetRspDbVgroupsAndSuperMeta();
D
dapan1121 已提交
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718

  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 已提交
1719
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1720 1721 1722 1723
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestSTablename);

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1724
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1725 1726 1727 1728 1729
  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 已提交
1730 1731
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
1732 1733 1734 1735 1736
  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 已提交
1737
  while (true) {
D
dapan1121 已提交
1738
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
D
dapan 已提交
1739
    if (0 == n) {
wafwerar's avatar
wafwerar 已提交
1740
      taosMsleep(50);
D
dapan 已提交
1741 1742 1743
    } else {
      break;
    }
D
dapan 已提交
1744 1745
  }

wafwerar's avatar
wafwerar 已提交
1746
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1747 1748 1749 1750

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

D
dapan1121 已提交
1751
  code = catalogUpdateTableMeta(pCtg, &rsp);
D
dapan1121 已提交
1752
  ASSERT_EQ(code, 0);
wafwerar's avatar
wafwerar 已提交
1753
  taosMemoryFreeClear(rsp.pSchemas);
D
dapan1121 已提交
1754

D
dapan 已提交
1755 1756
  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
1757
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan 已提交
1758
    if (n != 3) {
wafwerar's avatar
wafwerar 已提交
1759
      taosMsleep(50);
D
dapan 已提交
1760 1761 1762 1763 1764
    } else {
      break;
    }
  }

D
dapan1121 已提交
1765 1766 1767 1768 1769
  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 已提交
1770

D
dapan1121 已提交
1771
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1772 1773 1774 1775 1776
  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 已提交
1777 1778
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
1779 1780 1781 1782 1783
  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 已提交
1784
  taosMemoryFreeClear(tableMeta);
S
Shengliang Guan 已提交
1785

D
dapan1121 已提交
1786 1787 1788
  catalogDestroy();
}

D
dapan1121 已提交
1789 1790
TEST(getIndexInfo, notExists) {
  struct SCatalog  *pCtg = NULL;
1791 1792
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
D
dapan1121 已提交
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
  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);
1815 1816
  
  catalogDestroy();
D
dapan1121 已提交
1817 1818
}

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

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

D
dapan1121 已提交
1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
  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 已提交
1846
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1847 1848 1849
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

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

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

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1865
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1866 1867 1868
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 8);
  ASSERT_EQ(tableMeta->tableType, TSDB_NORMAL_TABLE);
H
Hongze Cheng 已提交
1869
  ASSERT_EQ(tableMeta->uid, ctgTestNormalTblUid - 1);
D
dapan1121 已提交
1870 1871 1872 1873 1874 1875
  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 已提交
1876
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1877

D
dapan1121 已提交
1878
  while (0 == ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) {
wafwerar's avatar
wafwerar 已提交
1879
    taosMsleep(50);
D
dapan1121 已提交
1880 1881
  }

D
dapan1121 已提交
1882
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
1883 1884 1885
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 8);
  ASSERT_EQ(tableMeta->tableType, TSDB_NORMAL_TABLE);
H
Hongze Cheng 已提交
1886
  ASSERT_EQ(tableMeta->uid, ctgTestNormalTblUid - 1);
D
dapan1121 已提交
1887 1888 1889 1890 1891 1892
  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 已提交
1893
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1894 1895 1896 1897 1898

  catalogDestroy();
}

TEST(refreshGetMeta, normal2notexist) {
H
Hongze Cheng 已提交
1899
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
1900 1901
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
1902 1903
  SVgroupInfo       vgInfo = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
1904 1905 1906 1907 1908 1909 1910 1911

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

D
dapan1121 已提交
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
  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 已提交
1925
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
1926 1927 1928
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

D
dapan1121 已提交
1929
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
1930 1931
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
1932
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
1933 1934 1935

  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
1936
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan1121 已提交
1937 1938 1939
    if (n > 0) {
      break;
    }
wafwerar's avatar
wafwerar 已提交
1940
    taosMsleep(50);
D
dapan1121 已提交
1941 1942 1943
  }

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
1944
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
1945 1946 1947
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 8);
  ASSERT_EQ(tableMeta->tableType, TSDB_NORMAL_TABLE);
H
Hongze Cheng 已提交
1948
  ASSERT_EQ(tableMeta->uid, ctgTestNormalTblUid - 1);
D
dapan1121 已提交
1949 1950 1951 1952 1953 1954
  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 已提交
1955
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1956

D
dapan1121 已提交
1957
  while (0 == ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) {
wafwerar's avatar
wafwerar 已提交
1958
    taosMsleep(50);
D
dapan1121 已提交
1959 1960
  }

D
dapan1121 已提交
1961
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
1962 1963 1964 1965 1966 1967 1968
  ASSERT_EQ(code, CTG_ERR_CODE_TABLE_NOT_EXIST);
  ASSERT_TRUE(tableMeta == NULL);

  catalogDestroy();
}

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

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

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

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

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

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
2017
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
2018 2019 2020
  ASSERT_EQ(code, 0);
  ASSERT_EQ(tableMeta->vgId, 8);
  ASSERT_EQ(tableMeta->tableType, TSDB_NORMAL_TABLE);
H
Hongze Cheng 已提交
2021
  ASSERT_EQ(tableMeta->uid, ctgTestNormalTblUid - 1);
D
dapan1121 已提交
2022 2023 2024 2025 2026 2027
  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 已提交
2028
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
2029

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

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

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

D
dapan1121 已提交
2051

D
dapan1121 已提交
2052
TEST(refreshGetMeta, stable2child) {
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_STBMETA;
  ctgTestRspFunc[2] = CTGT_RSP_STBMETA;
  ctgTestRspFunc[3] = CTGT_RSP_CTBMETA;
  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);
  ctgTestCurrentSTableName = ctgTestTablename;
  ctgTestCurrentCTableName = ctgTestTablename;

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
  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 已提交
2108 2109
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
2110 2111 2112 2113
  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 已提交
2114
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
2115

D
dapan1121 已提交
2116
  while (0 == ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) {
wafwerar's avatar
wafwerar 已提交
2117
    taosMsleep(50);
D
dapan1121 已提交
2118 2119 2120
  }

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

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

TEST(refreshGetMeta, stable2stable) {
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 = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153

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

D
dapan1121 已提交
2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166
  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 已提交
2167
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
2168 2169 2170 2171
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);
  ctgTestCurrentSTableName = ctgTestTablename;

D
dapan1121 已提交
2172
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
2173 2174
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
2175
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
2176 2177 2178

  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
2179
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan1121 已提交
2180 2181 2182
    if (n > 0) {
      break;
    }
wafwerar's avatar
wafwerar 已提交
2183
    taosMsleep(50);
D
dapan1121 已提交
2184 2185 2186
  }

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
2187
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
2188 2189 2190 2191 2192
  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 已提交
2193 2194
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
2195 2196 2197 2198
  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 已提交
2199
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
2200

D
dapan1121 已提交
2201
  while (0 == ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) {
wafwerar's avatar
wafwerar 已提交
2202
    taosMsleep(50);
D
dapan1121 已提交
2203 2204
  }

D
dapan1121 已提交
2205
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
2206 2207 2208 2209 2210
  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 已提交
2211 2212
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
2213 2214 2215 2216
  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 已提交
2217
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
2218 2219 2220 2221 2222 2223 2224

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

TEST(refreshGetMeta, child2stable) {
H
Hongze Cheng 已提交
2225
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2226 2227
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2228 2229
  SVgroupInfo       vgInfo = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
2230 2231 2232 2233 2234 2235 2236 2237 2238 2239

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

D
dapan1121 已提交
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
  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 已提交
2253
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
2254 2255 2256 2257 2258
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);
  ctgTestCurrentCTableName = ctgTestTablename;
  ctgTestCurrentSTableName = ctgTestSTablename;

D
dapan1121 已提交
2259
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
2260 2261
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
2262
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
2263 2264 2265

  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
2266
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan1121 已提交
2267 2268 2269
    if (n > 0) {
      break;
    }
wafwerar's avatar
wafwerar 已提交
2270
    taosMsleep(50);
D
dapan1121 已提交
2271 2272 2273
  }

  STableMeta *tableMeta = NULL;
D
dapan1121 已提交
2274
  code = catalogGetTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
2275 2276 2277 2278 2279 2280 2281 2282 2283
  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 已提交
2284
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
2285

D
dapan1121 已提交
2286
  while (2 != ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM)) {
wafwerar's avatar
wafwerar 已提交
2287
    taosMsleep(50);
D
dapan1121 已提交
2288 2289 2290
  }

  ctgTestCurrentSTableName = ctgTestTablename;
D
dapan1121 已提交
2291
  code = catalogRefreshGetTableMeta(pCtg, mockPointer, &n, &tableMeta, 0);
D
dapan1121 已提交
2292 2293 2294 2295 2296
  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 已提交
2297 2298
  ASSERT_EQ(tableMeta->uid, ctgTestSuid);
  ASSERT_EQ(tableMeta->suid, ctgTestSuid);
D
dapan1121 已提交
2299 2300 2301 2302
  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 已提交
2303
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
2304 2305 2306 2307 2308 2309

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

D
dapan 已提交
2310
TEST(tableDistVgroup, normalTable) {
H
Hongze Cheng 已提交
2311
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2312 2313
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2314 2315
  SVgroupInfo      *vgInfo = NULL;
  SArray           *vgList = NULL;
D
dapan 已提交
2316

D
dapan 已提交
2317 2318 2319 2320 2321 2322 2323
  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 已提交
2324

D
dapan 已提交
2325
  ctgTestSetRspByIdx();
D
dapan 已提交
2326 2327 2328

  initQueryModuleMsgHandle();

D
dapan1121 已提交
2329 2330 2331
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

D
dapan1121 已提交
2334
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan 已提交
2335 2336
  ASSERT_EQ(code, 0);

H
Hongze Cheng 已提交
2337
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
2338 2339
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);
D
dapan 已提交
2340

D
dapan1121 已提交
2341
  code = catalogGetTableDistVgInfo(pCtg, mockPointer, &n, &vgList);
D
dapan1121 已提交
2342
  ASSERT_TRUE(code != 0);
D
dapan 已提交
2343 2344 2345 2346 2347

  catalogDestroy();
}

TEST(tableDistVgroup, childTableCase) {
H
Hongze Cheng 已提交
2348
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2349 2350
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2351 2352
  SVgroupInfo      *vgInfo = NULL;
  SArray           *vgList = NULL;
D
dapan 已提交
2353

D
dapan 已提交
2354 2355 2356 2357 2358 2359 2360 2361
  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 已提交
2362

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

  initQueryModuleMsgHandle();

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

D
dapan 已提交
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}};
H
Haojun Liao 已提交
2376 2377 2378
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestCTablename);

D
dapan1121 已提交
2379
  code = catalogGetTableDistVgInfo(pCtg, mockPointer, &n, &vgList);
D
dapan1121 已提交
2380
  ASSERT_TRUE(code != 0);
D
dapan 已提交
2381 2382 2383 2384 2385

  catalogDestroy();
}

TEST(tableDistVgroup, superTableCase) {
H
Hongze Cheng 已提交
2386
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2387 2388
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2389 2390
  SVgroupInfo      *vgInfo = NULL;
  SArray           *vgList = NULL;
D
dapan 已提交
2391

D
dapan 已提交
2392 2393 2394 2395 2396 2397 2398 2399 2400
  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 已提交
2401
  ctgTestSetRspByIdx();
D
dapan 已提交
2402 2403 2404

  initQueryModuleMsgHandle();

D
dapan1121 已提交
2405 2406 2407
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

S
Shengliang Guan 已提交
2408
  // sendCreateDbMsg(pConn->pTransporter, &pConn->pAppInfo->mgmtEp.epSet);
D
dapan1121 已提交
2409
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan 已提交
2410 2411
  ASSERT_EQ(code, 0);

H
Hongze Cheng 已提交
2412
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
2413 2414
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestSTablename);
D
dapan 已提交
2415

D
dapan1121 已提交
2416
  code = catalogGetTableDistVgInfo(pCtg, mockPointer, &n, &vgList);
D
dapan 已提交
2417 2418 2419 2420
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), 10);
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 0);
  ASSERT_EQ(vgInfo->vgId, 1);
L
Liu Jicong 已提交
2421
  ASSERT_EQ(vgInfo->epSet.numOfEps, 1);
D
dapan 已提交
2422 2423
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 1);
  ASSERT_EQ(vgInfo->vgId, 2);
L
Liu Jicong 已提交
2424
  ASSERT_EQ(vgInfo->epSet.numOfEps, 2);
D
dapan 已提交
2425 2426
  vgInfo = (SVgroupInfo *)taosArrayGet(vgList, 2);
  ASSERT_EQ(vgInfo->vgId, 3);
L
Liu Jicong 已提交
2427
  ASSERT_EQ(vgInfo->epSet.numOfEps, 3);
D
dapan 已提交
2428

2429 2430
  taosArrayDestroy(vgList);

D
dapan 已提交
2431 2432 2433
  catalogDestroy();
}

D
dapan1121 已提交
2434
TEST(dbVgroup, getSetDbVgroupCase) {
H
Hongze Cheng 已提交
2435
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2436 2437
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2438 2439 2440 2441
  SVgroupInfo       vgInfo = {0};
  SVgroupInfo      *pvgInfo = NULL;
  SDBVgInfo        *dbVgroup = NULL;
  SArray           *vgList = NULL;
D
dapan1121 已提交
2442

D
dapan 已提交
2443 2444
  ctgTestInitLogFile();

D
dapan 已提交
2445 2446 2447 2448 2449 2450 2451
  memset(ctgTestRspFunc, 0, sizeof(ctgTestRspFunc));
  ctgTestRspIdx = 0;
  ctgTestRspFunc[0] = CTGT_RSP_VGINFO;
  ctgTestRspFunc[1] = CTGT_RSP_TBMETA;

  ctgTestSetRspByIdx();

D
dapan1121 已提交
2452 2453
  initQueryModuleMsgHandle();

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

D
dapan1121 已提交
2456 2457 2458 2459 2460 2461
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

H
Hongze Cheng 已提交
2462
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
2463 2464 2465
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

2466
  code = catalogGetDBVgList(pCtg, mockPointer, ctgTestDbname, &vgList);
D
dapan1121 已提交
2467 2468
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize((const SArray *)vgList), ctgTestVgNum);
S
Shengliang Guan 已提交
2469

2470 2471
  taosArrayDestroy(vgList);

D
dapan1121 已提交
2472 2473
  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
2474
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan1121 已提交
2475 2476 2477
    if (n > 0) {
      break;
    }
wafwerar's avatar
wafwerar 已提交
2478
    taosMsleep(50);
D
dapan 已提交
2479 2480
  }

D
dapan1121 已提交
2481
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
2482 2483
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 8);
L
Liu Jicong 已提交
2484
  ASSERT_EQ(vgInfo.epSet.numOfEps, 3);
D
dapan1121 已提交
2485

D
dapan1121 已提交
2486
  code = catalogGetTableDistVgInfo(pCtg, mockPointer, &n, &vgList);
D
dapan1121 已提交
2487
  ASSERT_TRUE(code != 0);
D
dapan1121 已提交
2488

D
dapan1121 已提交
2489 2490 2491
  int32_t dbVer = 0;
  int64_t dbId = 0;
  int32_t tbNum = 0;
D
dapan1121 已提交
2492 2493
  int64_t stateTs = 0;
  code = catalogGetDBVgVersion(pCtg, ctgTestDbname, &dbVer, &dbId, &tbNum, &stateTs);
D
dapan1121 已提交
2494 2495 2496 2497 2498
  ASSERT_EQ(code, 0);
  ASSERT_EQ(dbVer, ctgTestVgVersion);
  ASSERT_EQ(dbId, ctgTestDbId);
  ASSERT_EQ(tbNum, ctgTestVgNum / 2);

D
dapan1121 已提交
2499
  ctgTestBuildDBVgroup(&dbVgroup);
D
dapan1121 已提交
2500
  code = catalogUpdateDBVgInfo(pCtg, ctgTestDbname, ctgTestDbId, dbVgroup);
D
dapan1121 已提交
2501 2502
  ASSERT_EQ(code, 0);

D
dapan 已提交
2503 2504
  while (true) {
    uint64_t n = 0;
D
dapan1121 已提交
2505
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
D
dapan 已提交
2506
    if (n != 3) {
wafwerar's avatar
wafwerar 已提交
2507
      taosMsleep(50);
D
dapan 已提交
2508 2509 2510 2511 2512
    } else {
      break;
    }
  }

D
dapan1121 已提交
2513
  code = catalogGetTableHashVgroup(pCtg, mockPointer, &n, &vgInfo);
D
dapan1121 已提交
2514 2515
  ASSERT_EQ(code, 0);
  ASSERT_EQ(vgInfo.vgId, 7);
L
Liu Jicong 已提交
2516
  ASSERT_EQ(vgInfo.epSet.numOfEps, 2);
D
dapan1121 已提交
2517

D
dapan1121 已提交
2518
  code = catalogGetTableDistVgInfo(pCtg, mockPointer, &n, &vgList);
D
dapan1121 已提交
2519
  ASSERT_TRUE(code != 0);
S
Shengliang Guan 已提交
2520

D
dapan1121 已提交
2521 2522 2523
  catalogDestroy();
}

D
dapan1121 已提交
2524
TEST(multiThread, getSetRmSameDbVgroup) {
H
Hongze Cheng 已提交
2525
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2526 2527
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2528 2529 2530 2531
  SVgroupInfo       vgInfo = {0};
  SVgroupInfo      *pvgInfo = NULL;
  SDBVgInfo         dbVgroup = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
2532
  ctgTestStop = false;
D
dapan1121 已提交
2533 2534

  ctgTestInitLogFile();
S
Shengliang Guan 已提交
2535

D
dapan 已提交
2536
  ctgTestSetRspDbVgroups();
D
dapan1121 已提交
2537 2538 2539

  initQueryModuleMsgHandle();

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

D
dapan1121 已提交
2542 2543 2544 2545 2546 2547
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

H
Hongze Cheng 已提交
2548
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
2549 2550 2551
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

wafwerar's avatar
wafwerar 已提交
2552 2553
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
2554

wafwerar's avatar
wafwerar 已提交
2555 2556
  TdThread thread1, thread2;
  taosThreadCreate(&(thread1), &thattr, ctgTestSetSameDbVgroupThread, pCtg);
D
dapan1121 已提交
2557

wafwerar's avatar
wafwerar 已提交
2558
  taosSsleep(1);
wafwerar's avatar
wafwerar 已提交
2559
  taosThreadCreate(&(thread2), &thattr, ctgTestGetDbVgroupThread, pCtg);
D
dapan1121 已提交
2560 2561 2562

  while (true) {
    if (ctgTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
2563
      taosSsleep(1);
D
dapan1121 已提交
2564
    } else {
wafwerar's avatar
wafwerar 已提交
2565
      taosSsleep(ctgTestMTRunSec);
D
dapan1121 已提交
2566 2567 2568
      break;
    }
  }
S
Shengliang Guan 已提交
2569

D
dapan1121 已提交
2570
  ctgTestStop = true;
wafwerar's avatar
wafwerar 已提交
2571
  taosSsleep(1);
S
Shengliang Guan 已提交
2572

D
dapan1121 已提交
2573 2574 2575
  catalogDestroy();
}

D
dapan1121 已提交
2576
TEST(multiThread, getSetRmDiffDbVgroup) {
H
Hongze Cheng 已提交
2577
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2578 2579
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2580 2581 2582 2583
  SVgroupInfo       vgInfo = {0};
  SVgroupInfo      *pvgInfo = NULL;
  SDBVgInfo         dbVgroup = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
2584 2585 2586 2587
  ctgTestStop = false;

  ctgTestInitLogFile();

D
dapan 已提交
2588
  ctgTestSetRspDbVgroups();
D
dapan1121 已提交
2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599

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

wafwerar's avatar
wafwerar 已提交
2604 2605
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
2606

wafwerar's avatar
wafwerar 已提交
2607 2608
  TdThread thread1, thread2;
  taosThreadCreate(&(thread1), &thattr, ctgTestSetDiffDbVgroupThread, pCtg);
D
dapan1121 已提交
2609

wafwerar's avatar
wafwerar 已提交
2610
  taosSsleep(1);
wafwerar's avatar
wafwerar 已提交
2611
  taosThreadCreate(&(thread2), &thattr, ctgTestGetDbVgroupThread, pCtg);
D
dapan1121 已提交
2612 2613 2614

  while (true) {
    if (ctgTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
2615
      taosSsleep(1);
D
dapan1121 已提交
2616
    } else {
wafwerar's avatar
wafwerar 已提交
2617
      taosSsleep(ctgTestMTRunSec);
D
dapan1121 已提交
2618 2619 2620 2621 2622
      break;
    }
  }

  ctgTestStop = true;
wafwerar's avatar
wafwerar 已提交
2623
  taosSsleep(1);
D
dapan1121 已提交
2624 2625 2626 2627

  catalogDestroy();
}

D
dapan1121 已提交
2628
TEST(multiThread, ctableMeta) {
H
Hongze Cheng 已提交
2629
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2630 2631
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2632 2633 2634 2635
  SVgroupInfo       vgInfo = {0};
  SVgroupInfo      *pvgInfo = NULL;
  SDBVgInfo         dbVgroup = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
2636
  ctgTestStop = false;
D
dapan1121 已提交
2637

D
dapan 已提交
2638 2639
  ctgTestInitLogFile();

D
dapan 已提交
2640
  ctgTestSetRspDbVgroupsAndChildMeta();
D
dapan1121 已提交
2641 2642 2643

  initQueryModuleMsgHandle();

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

D
dapan1121 已提交
2646 2647 2648 2649 2650 2651
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

H
Hongze Cheng 已提交
2652
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
2653 2654 2655
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestTablename);

wafwerar's avatar
wafwerar 已提交
2656 2657
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
2658

wafwerar's avatar
wafwerar 已提交
2659 2660
  TdThread thread1, thread2;
  taosThreadCreate(&(thread1), &thattr, ctgTestSetCtableMetaThread, pCtg);
wafwerar's avatar
wafwerar 已提交
2661
  taosSsleep(1);
wafwerar's avatar
wafwerar 已提交
2662
  taosThreadCreate(&(thread1), &thattr, ctgTestGetCtableMetaThread, pCtg);
D
dapan1121 已提交
2663 2664 2665

  while (true) {
    if (ctgTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
2666
      taosSsleep(1);
D
dapan1121 已提交
2667
    } else {
wafwerar's avatar
wafwerar 已提交
2668
      taosSsleep(ctgTestMTRunSec);
D
dapan1121 已提交
2669 2670 2671
      break;
    }
  }
S
Shengliang Guan 已提交
2672

D
dapan1121 已提交
2673
  ctgTestStop = true;
wafwerar's avatar
wafwerar 已提交
2674
  taosSsleep(2);
S
Shengliang Guan 已提交
2675

D
dapan1121 已提交
2676 2677
  catalogDestroy();
}
D
dapan 已提交
2678

D
dapan1121 已提交
2679
TEST(rentTest, allRent) {
H
Hongze Cheng 已提交
2680
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2681 2682
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2683 2684 2685 2686
  SVgroupInfo       vgInfo = {0};
  SVgroupInfo      *pvgInfo = NULL;
  SDBVgInfo         dbVgroup = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
2687
  ctgTestStop = false;
H
Hongze Cheng 已提交
2688
  SDbVgVersion   *dbs = NULL;
D
dapan1121 已提交
2689
  SSTableVersion *stable = NULL;
H
Hongze Cheng 已提交
2690
  uint32_t        num = 0;
D
dapan1121 已提交
2691

D
dapan 已提交
2692 2693
  ctgTestInitLogFile();

D
dapan 已提交
2694
  ctgTestSetRspDbVgroupsAndMultiSuperMeta();
D
dapan1121 已提交
2695 2696 2697 2698 2699 2700 2701 2702 2703

  initQueryModuleMsgHandle();

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

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

H
Hongze Cheng 已提交
2704
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
D
dapan1121 已提交
2705 2706 2707 2708 2709 2710
  strcpy(n.dbname, "db1");

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

    STableMeta *tableMeta = NULL;
D
dapan1121 已提交
2711
    code = catalogGetSTableMeta(pCtg, mockPointer, &n, &tableMeta);
D
dapan1121 已提交
2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722
    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 已提交
2723

2724 2725
    taosMemoryFree(tableMeta);

D
dapan1121 已提交
2726
    while (ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM) < i) {
wafwerar's avatar
wafwerar 已提交
2727
      taosMsleep(50);
D
dapan 已提交
2728 2729
    }

D
dapan1121 已提交
2730 2731 2732 2733
    code = catalogGetExpiredDBs(pCtg, &dbs, &num);
    ASSERT_EQ(code, 0);
    printf("%d - expired dbNum:%d\n", i, num);
    if (dbs) {
S
Shengliang Guan 已提交
2734
      printf("%d - expired dbId:%" PRId64 ", vgVersion:%d\n", i, dbs->dbId, dbs->vgVersion);
wafwerar's avatar
wafwerar 已提交
2735
      taosMemoryFree(dbs);
D
dapan1121 已提交
2736 2737
      dbs = NULL;
    }
S
Shengliang Guan 已提交
2738

D
dapan1121 已提交
2739 2740 2741 2742 2743
    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 已提交
2744 2745
        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 已提交
2746
      }
wafwerar's avatar
wafwerar 已提交
2747
      taosMemoryFree(stable);
D
dapan1121 已提交
2748 2749 2750
      stable = NULL;
    }
    printf("*************************************************\n");
S
Shengliang Guan 已提交
2751

wafwerar's avatar
wafwerar 已提交
2752
    taosSsleep(2);
D
dapan1121 已提交
2753
  }
S
Shengliang Guan 已提交
2754

D
dapan1121 已提交
2755 2756 2757
  catalogDestroy();
}

D
dapan1121 已提交
2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806
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;
D
dapan1121 已提交
2807
  bool exists = false;
D
dapan1121 已提交
2808
  code = catalogChkAuthFromCache(pCtg, ctgTestUsername, ctgTestDbname, AUTH_TYPE_READ, &pass, &exists);
D
dapan1121 已提交
2809 2810 2811
  ASSERT_EQ(code, 0);
  ASSERT_EQ(exists, false);
  
D
dapan1121 已提交
2812 2813 2814 2815
  code = catalogChkAuth(pCtg, mockPointer, ctgTestUsername, ctgTestDbname, AUTH_TYPE_READ, &pass);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(pass, true);

D
dapan1121 已提交
2816 2817 2818 2819 2820 2821 2822 2823 2824 2825
  while (true) {
    uint64_t n = 0;
    ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);
    if (n != 1) {
      taosMsleep(50);
    } else {
      break;
    }
  }

D
dapan1121 已提交
2826
  code = catalogChkAuthFromCache(pCtg, ctgTestUsername, ctgTestDbname, AUTH_TYPE_READ, &pass, &exists);
D
dapan1121 已提交
2827 2828 2829 2830
  ASSERT_EQ(code, 0);
  ASSERT_EQ(pass, true);
  ASSERT_EQ(exists, true);

D
dapan1121 已提交
2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907
  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 已提交
2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034
TEST(apiTest, catalogGetDBCfg_test) {
  struct SCatalog  *pCtg = NULL;
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;

  ctgTestInitLogFile();

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

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

  SDbCfgInfo cfgInfo = {0};
  code = catalogGetDBCfg(pCtg, mockPointer, ctgTestDbname, &cfgInfo);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(cfgInfo.numOfVgroups, ctgTestVgNum);

  catalogDestroy();
}

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

  ctgTestInitLogFile();

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

  ctgTestSetRspByIdx();

  initQueryModuleMsgHandle();

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

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

  SArray *qnodeList = taosArrayInit(10, sizeof(SQueryNodeLoad));
  code = catalogGetQnodeList(pCtg, mockPointer, qnodeList);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize(qnodeList), ctgTestQnodeNum);

  for (int32_t i = 0; i < ctgTestQnodeNum; ++i) {
    SQueryNodeLoad * pLoad = (SQueryNodeLoad *)taosArrayGet(qnodeList, i);
    ASSERT_EQ(pLoad->addr.nodeId, i);
  }

  catalogDestroy();
}


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

  ctgTestInitLogFile();

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

  ctgTestSetRspByIdx();

  initQueryModuleMsgHandle();

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

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

  SFuncInfo funcInfo = {0};
  code = catalogGetUdfInfo(pCtg, mockPointer, "func1", &funcInfo);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(funcInfo.funcType, ctgTestFuncType);

  catalogDestroy();
}


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

  ctgTestInitLogFile();

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

  ctgTestSetRspByIdx();

  initQueryModuleMsgHandle();

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

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

  char* ver = NULL;
  code = catalogGetServerVersion(pCtg, mockPointer, &ver);
  ASSERT_EQ(code, 0);
  ASSERT_TRUE(0 == strcmp(ver, "1.0"));

  catalogDestroy();
}

D
dapan1121 已提交
3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097
TEST(apiTest, catalogUpdateTableIndex_test) {
  struct SCatalog  *pCtg = NULL;
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;

  ctgTestInitLogFile();

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

  ctgTestSetRspByIdx();

  initQueryModuleMsgHandle();

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

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

  STableIndexRsp rsp = {0};
  strcpy(rsp.dbFName, ctgTestDbname);
  strcpy(rsp.tbName, ctgTestSTablename);
  rsp.suid = ctgTestSuid;
  rsp.version = 1;
  code = catalogUpdateTableIndex(pCtg, &rsp);
  ASSERT_EQ(code, 0);

  catalogDestroy();
}


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

  ctgTestInitLogFile();

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

  ctgTestSetRspByIdx();

  initQueryModuleMsgHandle();

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

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

  SArray* pList = NULL;
  code = catalogGetDnodeList(pCtg, mockPointer, &pList);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(taosArrayGetSize(pList), 1);

  taosArrayDestroy(pList);

  catalogDestroy();
}
D
dapan1121 已提交
3098

D
dapan1121 已提交
3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150
#ifdef INTEGRATION_TEST
TEST(intTest, autoCreateTableTest) {
  struct SCatalog  *pCtg = NULL;

  TAOS     *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_TRUE(NULL != taos);   

  ctgdEnableDebug("api", true);
  ctgdEnableDebug("meta", true);
  ctgdEnableDebug("cache", true);
  ctgdEnableDebug("lock", true);
  
  ctgTestExecQuery(taos, "drop database if exists db1", false, NULL);
  ctgTestExecQuery(taos, "create database db1", false, NULL);
  ctgTestExecQuery(taos, "create stable db1.st1 (ts timestamp, f1 int) tags(tg1 int)", false, NULL);
  ctgTestExecQuery(taos, "insert into db1.tb1 using db1.st1 tags(1) values(now, 1)", false, NULL);

  ctgdGetOneHandle(&pCtg);
  
  while (true) {
    uint32_t n = ctgdGetClusterCacheNum(pCtg, CTG_DBG_META_NUM);
    if (2 != n) {
      taosMsleep(50);
    } else {
      break;
    }
  }

  uint64_t n = 0, m = 0;
  ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&n);

  ctgTestExecQuery(taos, "insert into db1.tb1 using db1.st1 tags(1) values(now, 2)", false, NULL);

  ctgTestExecQuery(taos, "insert into db1.tb1 values(now, 3)", false, NULL);

  taosMsleep(1000);
  ctgdGetStatNum("runtime.numOfOpDequeue", (void *)&m);

  ASSERT_EQ(n, m);

  ctgdEnableDebug("stopUpdate", true);
  ctgTestExecQuery(taos, "alter table db1.st1 add column f2 double", false, NULL);

  ctgdEnableDebug("stopUpdate", false);
  
  ctgTestExecQuery(taos, "insert into db1.tb1 (ts, f1) values(now, 4)", false, NULL);
  
  taos_close(taos);
}

#endif

D
dapan1121 已提交
3151

S
Shengliang Guan 已提交
3152
int main(int argc, char **argv) {
3153 3154 3155
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
D
dapan1121 已提交
3156

D
dapan1121 已提交
3157
#pragma GCC diagnostic pop