catalogTests.cpp 85.3 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 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
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));
  SFuncInfo funcInfo = {0};
  strcpy(funcInfo.name, "func1");
  funcInfo.funcType = ctgTestFuncType;
    
  (void)taosArrayPush(funcRsp.pFuncInfos, &funcInfo);

  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 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
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 已提交
731 732 733 734 735 736 737 738 739 740 741 742 743

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 已提交
744 745 746 747 748 749 750 751 752 753 754 755
    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 已提交
756 757 758
    case TDMT_MND_DNODE_LIST:
      ctgTestRspDndeList(shandle, pEpSet, pMsg, pRsp);
      break;
D
dapan1121 已提交
759 760 761 762 763 764 765 766
    default:
      break;
  }

  return;
}


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

  ctgTestRspIdx++;
S
Shengliang Guan 已提交
820

D
dapan 已提交
821 822 823
  return;
}

D
dapan 已提交
824 825 826 827
void ctgTestRspDbVgroupsAndNormalMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);

  ctgTestSetRspTableMeta();
S
Shengliang Guan 已提交
828

D
dapan 已提交
829 830 831
  return;
}

D
dapan 已提交
832 833
void ctgTestRspDbVgroupsAndChildMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);
S
Shengliang Guan 已提交
834

D
dapan 已提交
835
  ctgTestSetRspCTableMeta();
S
Shengliang Guan 已提交
836

D
dapan 已提交
837 838 839
  return;
}

D
dapan 已提交
840 841
void ctgTestRspDbVgroupsAndSuperMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);
S
Shengliang Guan 已提交
842

D
dapan 已提交
843
  ctgTestSetRspSTableMeta();
S
Shengliang Guan 已提交
844

D
dapan1121 已提交
845 846 847
  return;
}

D
dapan 已提交
848 849
void ctgTestRspDbVgroupsAndMultiSuperMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) {
  ctgTestRspDbVgroups(shandle, pEpSet, pMsg, pRsp);
S
Shengliang Guan 已提交
850

D
dapan 已提交
851
  ctgTestSetRspMultiSTableMeta();
S
Shengliang Guan 已提交
852

D
dapan1121 已提交
853 854 855
  return;
}

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

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
1056
}  // namespace
1057

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

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

    if (vgList) {
      taosArrayDestroy(vgList);
    }

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

  return NULL;
}

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

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

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

  return NULL;
}

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

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

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

  return NULL;
}

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

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

D
dapan1121 已提交
1147 1148 1149 1150
  SCtgTbMetaCtx ctx = {0};
  ctx.pName = &cn;
  ctx.flag = CTG_FLAG_UNKNOWN_STB;

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

wafwerar's avatar
wafwerar 已提交
1157
    taosMemoryFreeClear(tbMeta);
D
dapan1121 已提交
1158

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

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

  return NULL;
}

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

D
dapan1121 已提交
1178
  SCtgCacheOperation operation = {0};
H
Hongze Cheng 已提交
1179

D
dapan1121 已提交
1180
  operation.opId = CTG_OP_UPDATE_TB_META;
S
Shengliang Guan 已提交
1181

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

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

D
dapan1121 已提交
1191
    code = ctgOpUpdateTbMeta(&operation);
D
dapan1121 已提交
1192 1193 1194 1195
    if (code) {
      assert(0);
    }

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

  return NULL;
}

D
dapan 已提交
1207

D
dapan1121 已提交
1208 1209 1210 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
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 已提交
1236
TEST(tableMeta, normalTable) {
H
Hongze Cheng 已提交
1237 1238
  struct SCatalog  *pCtg = NULL;
  SVgroupInfo       vgInfo = {0};
D
dapan1121 已提交
1239 1240
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
1241

D
dapan1121 已提交
1242 1243
  ctgTestInitLogFile();

D
dapan 已提交
1244
  ctgTestSetRspDbVgroups();
D
dapan1121 已提交
1245

1246
  initQueryModuleMsgHandle();
D
ut test  
dapan1121 已提交
1247

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

D
dapan1121 已提交
1250 1251
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);
1252

D
dapan1121 已提交
1253
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan1121 已提交
1254
  ASSERT_EQ(code, 0);
1255

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

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

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

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

D
dapan 已提交
1283
  ctgTestSetRspTableMeta();
D
dapan1121 已提交
1284 1285

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

1298 1299
  taosMemoryFree(tableMeta);

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

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

1321 1322
  taosMemoryFree(tableMeta);

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

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

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

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

D
dapan1121 已提交
1367 1368 1369
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 0);

D
dapan1121 已提交
1370 1371 1372
  catalogDestroy();
}

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

D
dapan1121 已提交
1379 1380
  ctgTestInitLogFile();

D
dapan 已提交
1381
  ctgTestSetRspDbVgroupsAndChildMeta();
D
dapan1121 已提交
1382 1383 1384

  initQueryModuleMsgHandle();

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

D
dapan1121 已提交
1389
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan1121 已提交
1390 1391
  ASSERT_EQ(code, 0);

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

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

1408 1409
  taosMemoryFree(tableMeta);

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

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

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

1445 1446
  taosMemoryFree(tableMeta);

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

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

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

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

D
dapan1121 已提交
1479 1480 1481
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 1);

D
dapan1121 已提交
1482 1483 1484
  catalogDestroy();
}

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

D
dapan 已提交
1491
  ctgTestSetRspDbVgroupsAndSuperMeta();
D
dapan1121 已提交
1492 1493 1494

  initQueryModuleMsgHandle();

D
dapan1121 已提交
1495 1496 1497
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

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

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

1520 1521
  taosMemoryFree(tableMeta);

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

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

  tableMeta = NULL;
H
Haojun Liao 已提交
1549 1550 1551

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

1563 1564
  taosMemoryFree(tableMeta);

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

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

1586 1587
  taosMemoryFree(tableMeta);

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

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

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

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

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

D
dapan1121 已提交
1621 1622
  ASSERT_EQ(allDbNum, 1);
  ASSERT_EQ(allStbNum, 1);
D
dapan1121 已提交
1623 1624

  catalogDestroy();
D
dapan1121 已提交
1625
}
1626

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

  ctgTestInitLogFile();

D
dapan 已提交
1635
  ctgTestSetRspDbVgroupsAndSuperMeta();
D
dapan1121 已提交
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645

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

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

1664 1665
  taosMemoryFree(tableMeta);

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

D
dapan1121 已提交
1675
  code = catalogRemoveStbMeta(pCtg, "1.db1", ctgTestDbId, ctgTestSTablename, ctgTestSuid);
D
dapan1121 已提交
1676 1677
  ASSERT_EQ(code, 0);

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

D
dapan1121 已提交
1688 1689 1690 1691 1692
  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 已提交
1693

D
dapan1121 已提交
1694 1695 1696 1697
  catalogDestroy();
}

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

  ctgTestInitLogFile();

D
dapan 已提交
1705
  ctgTestSetRspDbVgroupsAndSuperMeta();
D
dapan1121 已提交
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715

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

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

wafwerar's avatar
wafwerar 已提交
1743
  taosMemoryFreeClear(tableMeta);
D
dapan1121 已提交
1744 1745 1746 1747

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

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

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

D
dapan1121 已提交
1762 1763 1764 1765 1766
  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 已提交
1767

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

D
dapan1121 已提交
1783 1784 1785
  catalogDestroy();
}

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

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

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

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

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

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

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

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

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

  catalogDestroy();
}

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

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

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

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

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

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

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

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

  catalogDestroy();
}

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

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

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

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

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

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

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

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

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

D
dapan1121 已提交
2048

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

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

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

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

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

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

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

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

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

TEST(refreshGetMeta, stable2stable) {
H
Hongze Cheng 已提交
2136
  struct SCatalog  *pCtg = NULL;
D
dapan1121 已提交
2137 2138
  SRequestConnInfo connInfo = {0};  
  SRequestConnInfo *mockPointer = (SRequestConnInfo *)&connInfo;
H
Hongze Cheng 已提交
2139 2140
  SVgroupInfo       vgInfo = {0};
  SArray           *vgList = NULL;
D
dapan1121 已提交
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

D
dapan 已提交
2314 2315 2316 2317 2318 2319 2320
  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 已提交
2321

D
dapan 已提交
2322
  ctgTestSetRspByIdx();
D
dapan 已提交
2323 2324 2325

  initQueryModuleMsgHandle();

D
dapan1121 已提交
2326 2327 2328
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

D
dapan1121 已提交
2331
  code = catalogGetHandle(ctgTestClusterId, &pCtg);
D
dapan 已提交
2332 2333
  ASSERT_EQ(code, 0);

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

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

  catalogDestroy();
}

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

D
dapan 已提交
2351 2352 2353 2354 2355 2356 2357 2358
  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 已提交
2359

D
dapan 已提交
2360
  ctgTestSetRspByIdx();
D
dapan 已提交
2361 2362 2363

  initQueryModuleMsgHandle();

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

D
dapan 已提交
2366 2367 2368 2369 2370 2371
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

H
Hongze Cheng 已提交
2372
  SName n = {TSDB_TABLE_NAME_T, 1, {0}, {0}};
H
Haojun Liao 已提交
2373 2374 2375
  strcpy(n.dbname, "db1");
  strcpy(n.tname, ctgTestCTablename);

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

  catalogDestroy();
}

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

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

  initQueryModuleMsgHandle();

D
dapan1121 已提交
2402 2403 2404
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

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

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

2426 2427
  taosArrayDestroy(vgList);

D
dapan 已提交
2428 2429 2430
  catalogDestroy();
}

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

D
dapan 已提交
2440 2441
  ctgTestInitLogFile();

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

  ctgTestSetRspByIdx();

D
dapan1121 已提交
2449 2450
  initQueryModuleMsgHandle();

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

D
dapan1121 已提交
2453 2454 2455 2456 2457 2458
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

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

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

2467 2468
  taosArrayDestroy(vgList);

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

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

D
dapan1121 已提交
2483
  code = catalogGetTableDistVgInfo(pCtg, mockPointer, &n, &vgList);
D
dapan1121 已提交
2484
  ASSERT_TRUE(code != 0);
D
dapan1121 已提交
2485

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

D
dapan1121 已提交
2496
  ctgTestBuildDBVgroup(&dbVgroup);
D
dapan1121 已提交
2497
  code = catalogUpdateDBVgInfo(pCtg, ctgTestDbname, ctgTestDbId, dbVgroup);
D
dapan1121 已提交
2498 2499
  ASSERT_EQ(code, 0);

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

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

D
dapan1121 已提交
2515
  code = catalogGetTableDistVgInfo(pCtg, mockPointer, &n, &vgList);
D
dapan1121 已提交
2516
  ASSERT_TRUE(code != 0);
S
Shengliang Guan 已提交
2517

D
dapan1121 已提交
2518 2519 2520
  catalogDestroy();
}

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

  ctgTestInitLogFile();
S
Shengliang Guan 已提交
2532

D
dapan 已提交
2533
  ctgTestSetRspDbVgroups();
D
dapan1121 已提交
2534 2535 2536

  initQueryModuleMsgHandle();

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

D
dapan1121 已提交
2539 2540 2541 2542 2543 2544
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

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

wafwerar's avatar
wafwerar 已提交
2549 2550
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
2551

wafwerar's avatar
wafwerar 已提交
2552 2553
  TdThread thread1, thread2;
  taosThreadCreate(&(thread1), &thattr, ctgTestSetSameDbVgroupThread, pCtg);
D
dapan1121 已提交
2554

wafwerar's avatar
wafwerar 已提交
2555
  taosSsleep(1);
wafwerar's avatar
wafwerar 已提交
2556
  taosThreadCreate(&(thread2), &thattr, ctgTestGetDbVgroupThread, pCtg);
D
dapan1121 已提交
2557 2558 2559

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

D
dapan1121 已提交
2567
  ctgTestStop = true;
wafwerar's avatar
wafwerar 已提交
2568
  taosSsleep(1);
S
Shengliang Guan 已提交
2569

D
dapan1121 已提交
2570 2571 2572
  catalogDestroy();
}

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

  ctgTestInitLogFile();

D
dapan 已提交
2585
  ctgTestSetRspDbVgroups();
D
dapan1121 已提交
2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596

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

wafwerar's avatar
wafwerar 已提交
2601 2602
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
2603

wafwerar's avatar
wafwerar 已提交
2604 2605
  TdThread thread1, thread2;
  taosThreadCreate(&(thread1), &thattr, ctgTestSetDiffDbVgroupThread, pCtg);
D
dapan1121 已提交
2606

wafwerar's avatar
wafwerar 已提交
2607
  taosSsleep(1);
wafwerar's avatar
wafwerar 已提交
2608
  taosThreadCreate(&(thread2), &thattr, ctgTestGetDbVgroupThread, pCtg);
D
dapan1121 已提交
2609 2610 2611

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

  ctgTestStop = true;
wafwerar's avatar
wafwerar 已提交
2620
  taosSsleep(1);
D
dapan1121 已提交
2621 2622 2623 2624

  catalogDestroy();
}

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

D
dapan 已提交
2635 2636
  ctgTestInitLogFile();

D
dapan 已提交
2637
  ctgTestSetRspDbVgroupsAndChildMeta();
D
dapan1121 已提交
2638 2639 2640

  initQueryModuleMsgHandle();

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

D
dapan1121 已提交
2643 2644 2645 2646 2647 2648
  int32_t code = catalogInit(NULL);
  ASSERT_EQ(code, 0);

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

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

wafwerar's avatar
wafwerar 已提交
2653 2654
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
2655

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

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

D
dapan1121 已提交
2670
  ctgTestStop = true;
wafwerar's avatar
wafwerar 已提交
2671
  taosSsleep(2);
S
Shengliang Guan 已提交
2672

D
dapan1121 已提交
2673 2674
  catalogDestroy();
}
D
dapan 已提交
2675

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

D
dapan 已提交
2689 2690
  ctgTestInitLogFile();

D
dapan 已提交
2691
  ctgTestSetRspDbVgroupsAndMultiSuperMeta();
D
dapan1121 已提交
2692 2693 2694 2695 2696 2697 2698 2699 2700

  initQueryModuleMsgHandle();

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

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

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

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

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

2721 2722
    taosMemoryFree(tableMeta);

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

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

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

wafwerar's avatar
wafwerar 已提交
2749
    taosSsleep(2);
D
dapan1121 已提交
2750
  }
S
Shengliang Guan 已提交
2751

D
dapan1121 已提交
2752 2753 2754
  catalogDestroy();
}

D
dapan1121 已提交
2755 2756 2757 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
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 已提交
2804
  bool exists = false;
D
dapan1121 已提交
2805
  code = catalogChkAuthFromCache(pCtg, ctgTestUsername, ctgTestDbname, AUTH_TYPE_READ, &pass, &exists);
D
dapan1121 已提交
2806 2807 2808
  ASSERT_EQ(code, 0);
  ASSERT_EQ(exists, false);
  
D
dapan1121 已提交
2809 2810 2811 2812
  code = catalogChkAuth(pCtg, mockPointer, ctgTestUsername, ctgTestDbname, AUTH_TYPE_READ, &pass);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(pass, true);

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

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

D
dapan1121 已提交
2828 2829 2830 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
  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 已提交
2905 2906 2907 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
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 已提交
3032 3033 3034 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
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 已提交
3095

D
dapan1121 已提交
3096 3097 3098 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
#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 已提交
3148

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

D
dapan1121 已提交
3154
#pragma GCC diagnostic pop