ctgAsync.c 68.8 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#include "catalogInt.h"
dengyihao's avatar
dengyihao 已提交
17
#include "query.h"
D
dapan1121 已提交
18
#include "systable.h"
dengyihao's avatar
dengyihao 已提交
19
#include "tname.h"
D
dapan1121 已提交
20
#include "tref.h"
dengyihao's avatar
dengyihao 已提交
21
#include "trpc.h"
D
dapan1121 已提交
22

dengyihao's avatar
dengyihao 已提交
23 24
int32_t ctgInitGetTbMetaTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
  SName*   name = (SName*)param;
D
dapan1121 已提交
25
  SCtgTask task = {0};
D
dapan1121 已提交
26

D
dapan1121 已提交
27 28 29
  task.type = CTG_TASK_GET_TB_META;
  task.taskId = taskIdx;
  task.pJob = pJob;
D
dapan1121 已提交
30

D
dapan1121 已提交
31 32
  task.taskCtx = taosMemoryCalloc(1, sizeof(SCtgTbMetaCtx));
  if (NULL == task.taskCtx) {
D
dapan1121 已提交
33 34 35
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
36
  SCtgTbMetaCtx* ctx = task.taskCtx;
D
dapan1121 已提交
37 38
  ctx->pName = taosMemoryMalloc(sizeof(*name));
  if (NULL == ctx->pName) {
D
dapan1121 已提交
39
    taosMemoryFree(task.taskCtx);
D
dapan1121 已提交
40 41
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }
42

D
dapan1121 已提交
43 44 45
  memcpy(ctx->pName, name, sizeof(*name));
  ctx->flag = CTG_FLAG_UNKNOWN_STB;

D
dapan1121 已提交
46 47
  taosArrayPush(pJob->pTasks, &task);

dengyihao's avatar
dengyihao 已提交
48 49
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, tbName:%s", pJob->queryId, taskIdx,
         ctgTaskTypeStr(task.type), name->tname);
D
dapan1121 已提交
50 51 52 53

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
54 55
int32_t ctgInitGetTbMetasTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
  SName*   name = (SName*)param;
D
dapan1121 已提交
56 57 58 59 60 61
  SCtgTask task = {0};

  task.type = CTG_TASK_GET_TB_META_BATCH;
  task.taskId = taskIdx;
  task.pJob = pJob;

62
  task.taskCtx = taosMemoryCalloc(1, sizeof(SCtgTbMetasCtx));
D
dapan1121 已提交
63 64 65 66
  if (NULL == task.taskCtx) {
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

67
  SCtgTbMetasCtx* ctx = task.taskCtx;
D
dapan1121 已提交
68
  ctx->pNames = param;
69
  ctx->pResList = taosArrayInit(pJob->tbMetaNum, sizeof(SMetaRes));
D
dapan1121 已提交
70 71 72

  taosArrayPush(pJob->pTasks, &task);

73 74
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, dbNum:%lu, tbNum:%d", pJob->queryId, taskIdx,
         ctgTaskTypeStr(task.type), taosArrayGetSize(ctx->pNames), pJob->tbMetaNum);
D
dapan1121 已提交
75 76 77 78

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
79 80
int32_t ctgInitGetDbVgTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
  char*    dbFName = (char*)param;
D
dapan1121 已提交
81
  SCtgTask task = {0};
D
dapan1121 已提交
82

D
dapan1121 已提交
83 84 85
  task.type = CTG_TASK_GET_DB_VGROUP;
  task.taskId = taskIdx;
  task.pJob = pJob;
D
dapan1121 已提交
86

D
dapan1121 已提交
87 88
  task.taskCtx = taosMemoryCalloc(1, sizeof(SCtgDbVgCtx));
  if (NULL == task.taskCtx) {
D
dapan1121 已提交
89 90 91
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
92
  SCtgDbVgCtx* ctx = task.taskCtx;
93

D
dapan1121 已提交
94 95
  memcpy(ctx->dbFName, dbFName, sizeof(ctx->dbFName));

D
dapan1121 已提交
96 97
  taosArrayPush(pJob->pTasks, &task);

dengyihao's avatar
dengyihao 已提交
98 99
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, dbFName:%s", pJob->queryId, taskIdx,
         ctgTaskTypeStr(task.type), dbFName);
D
dapan1121 已提交
100 101 102 103

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
104 105
int32_t ctgInitGetDbCfgTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
  char*    dbFName = (char*)param;
D
dapan1121 已提交
106
  SCtgTask task = {0};
D
dapan1121 已提交
107

D
dapan1121 已提交
108 109 110
  task.type = CTG_TASK_GET_DB_CFG;
  task.taskId = taskIdx;
  task.pJob = pJob;
D
dapan1121 已提交
111

D
dapan1121 已提交
112 113
  task.taskCtx = taosMemoryCalloc(1, sizeof(SCtgDbCfgCtx));
  if (NULL == task.taskCtx) {
D
dapan1121 已提交
114 115 116
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
117
  SCtgDbCfgCtx* ctx = task.taskCtx;
118

D
dapan1121 已提交
119 120
  memcpy(ctx->dbFName, dbFName, sizeof(ctx->dbFName));

D
dapan1121 已提交
121 122
  taosArrayPush(pJob->pTasks, &task);

dengyihao's avatar
dengyihao 已提交
123 124
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, dbFName:%s", pJob->queryId, taskIdx,
         ctgTaskTypeStr(task.type), dbFName);
D
dapan1121 已提交
125 126 127 128

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
129 130
int32_t ctgInitGetDbInfoTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
  char*    dbFName = (char*)param;
D
dapan1121 已提交
131 132 133 134 135 136 137 138 139 140 141 142
  SCtgTask task = {0};

  task.type = CTG_TASK_GET_DB_INFO;
  task.taskId = taskIdx;
  task.pJob = pJob;

  task.taskCtx = taosMemoryCalloc(1, sizeof(SCtgDbInfoCtx));
  if (NULL == task.taskCtx) {
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

  SCtgDbInfoCtx* ctx = task.taskCtx;
143

D
dapan1121 已提交
144 145 146 147
  memcpy(ctx->dbFName, dbFName, sizeof(ctx->dbFName));

  taosArrayPush(pJob->pTasks, &task);

dengyihao's avatar
dengyihao 已提交
148 149
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, dbFName:%s", pJob->queryId, taskIdx,
         ctgTaskTypeStr(task.type), dbFName);
D
dapan1121 已提交
150 151 152 153

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
154 155
int32_t ctgInitGetTbHashTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
  SName*   name = (SName*)param;
D
dapan1121 已提交
156
  SCtgTask task = {0};
D
dapan1121 已提交
157

D
dapan1121 已提交
158 159 160
  task.type = CTG_TASK_GET_TB_HASH;
  task.taskId = taskIdx;
  task.pJob = pJob;
D
dapan1121 已提交
161

D
dapan1121 已提交
162 163
  task.taskCtx = taosMemoryCalloc(1, sizeof(SCtgTbHashCtx));
  if (NULL == task.taskCtx) {
D
dapan1121 已提交
164 165 166
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
167
  SCtgTbHashCtx* ctx = task.taskCtx;
D
dapan1121 已提交
168 169
  ctx->pName = taosMemoryMalloc(sizeof(*name));
  if (NULL == ctx->pName) {
D
dapan1121 已提交
170
    taosMemoryFree(task.taskCtx);
D
dapan1121 已提交
171 172
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }
173

D
dapan1121 已提交
174 175 176
  memcpy(ctx->pName, name, sizeof(*name));
  tNameGetFullDbName(ctx->pName, ctx->dbFName);

D
dapan1121 已提交
177 178
  taosArrayPush(pJob->pTasks, &task);

dengyihao's avatar
dengyihao 已提交
179 180
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, tableName:%s", pJob->queryId, taskIdx,
         ctgTaskTypeStr(task.type), name->tname);
D
dapan1121 已提交
181 182 183 184

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
185 186
int32_t ctgInitGetTbHashsTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
  SName*   name = (SName*)param;
187 188 189 190 191 192
  SCtgTask task = {0};

  task.type = CTG_TASK_GET_TB_HASH_BATCH;
  task.taskId = taskIdx;
  task.pJob = pJob;

193
  task.taskCtx = taosMemoryCalloc(1, sizeof(SCtgTbHashsCtx));
194 195 196 197
  if (NULL == task.taskCtx) {
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

198
  SCtgTbHashsCtx* ctx = task.taskCtx;
199
  ctx->pNames = param;
200
  ctx->pResList = taosArrayInit(pJob->tbHashNum, sizeof(SMetaRes));
201 202 203

  taosArrayPush(pJob->pTasks, &task);

204 205
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, dbNum:%lu, tbNum:%d", pJob->queryId, taskIdx,
         ctgTaskTypeStr(task.type), taosArrayGetSize(ctx->pNames), pJob->tbHashNum);
206 207 208 209

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
210
int32_t ctgInitGetQnodeTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
D
dapan1121 已提交
211 212 213 214 215 216
  SCtgTask task = {0};

  task.type = CTG_TASK_GET_QNODE;
  task.taskId = taskIdx;
  task.pJob = pJob;
  task.taskCtx = NULL;
D
dapan1121 已提交
217

D
dapan1121 已提交
218
  taosArrayPush(pJob->pTasks, &task);
D
dapan1121 已提交
219

D
dapan1121 已提交
220
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized", pJob->queryId, taskIdx, ctgTaskTypeStr(task.type));
D
dapan1121 已提交
221 222 223 224

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
225
int32_t ctgInitGetDnodeTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
D
dapan1121 已提交
226 227 228 229 230 231 232 233 234
  SCtgTask task = {0};

  task.type = CTG_TASK_GET_DNODE;
  task.taskId = taskIdx;
  task.pJob = pJob;
  task.taskCtx = NULL;

  taosArrayPush(pJob->pTasks, &task);

D
dapan1121 已提交
235
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized", pJob->queryId, taskIdx, ctgTaskTypeStr(task.type));
D
dapan1121 已提交
236 237 238 239

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
240 241
int32_t ctgInitGetIndexTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
  char*    name = (char*)param;
D
dapan1121 已提交
242
  SCtgTask task = {0};
D
dapan1121 已提交
243

D
dapan1121 已提交
244 245 246
  task.type = CTG_TASK_GET_INDEX;
  task.taskId = taskIdx;
  task.pJob = pJob;
D
dapan1121 已提交
247

D
dapan1121 已提交
248 249
  task.taskCtx = taosMemoryCalloc(1, sizeof(SCtgIndexCtx));
  if (NULL == task.taskCtx) {
D
dapan1121 已提交
250 251 252
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
253
  SCtgIndexCtx* ctx = task.taskCtx;
254

D
dapan1121 已提交
255
  tstrncpy(ctx->indexFName, name, sizeof(ctx->indexFName));
D
dapan1121 已提交
256

D
dapan1121 已提交
257 258
  taosArrayPush(pJob->pTasks, &task);

dengyihao's avatar
dengyihao 已提交
259 260
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, indexFName:%s", pJob->queryId, taskIdx,
         ctgTaskTypeStr(task.type), name);
D
dapan1121 已提交
261 262 263 264

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
265 266
int32_t ctgInitGetUdfTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
  char*    name = (char*)param;
D
dapan1121 已提交
267
  SCtgTask task = {0};
D
dapan1121 已提交
268

D
dapan1121 已提交
269 270 271
  task.type = CTG_TASK_GET_UDF;
  task.taskId = taskIdx;
  task.pJob = pJob;
D
dapan1121 已提交
272

D
dapan1121 已提交
273 274
  task.taskCtx = taosMemoryCalloc(1, sizeof(SCtgUdfCtx));
  if (NULL == task.taskCtx) {
D
dapan1121 已提交
275 276 277
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
278
  SCtgUdfCtx* ctx = task.taskCtx;
279

D
dapan1121 已提交
280
  tstrncpy(ctx->udfName, name, sizeof(ctx->udfName));
D
dapan1121 已提交
281

D
dapan1121 已提交
282 283
  taosArrayPush(pJob->pTasks, &task);

dengyihao's avatar
dengyihao 已提交
284 285
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, udfName:%s", pJob->queryId, taskIdx,
         ctgTaskTypeStr(task.type), name);
D
dapan1121 已提交
286 287 288 289

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
290 291 292
int32_t ctgInitGetUserTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
  SUserAuthInfo* user = (SUserAuthInfo*)param;
  SCtgTask       task = {0};
D
dapan1121 已提交
293

D
dapan1121 已提交
294 295 296
  task.type = CTG_TASK_GET_USER;
  task.taskId = taskIdx;
  task.pJob = pJob;
D
dapan1121 已提交
297

D
dapan1121 已提交
298 299
  task.taskCtx = taosMemoryCalloc(1, sizeof(SCtgUserCtx));
  if (NULL == task.taskCtx) {
D
dapan1121 已提交
300 301 302
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
303
  SCtgUserCtx* ctx = task.taskCtx;
304

D
dapan1121 已提交
305 306
  memcpy(&ctx->user, user, sizeof(*user));

D
dapan1121 已提交
307 308
  taosArrayPush(pJob->pTasks, &task);

dengyihao's avatar
dengyihao 已提交
309 310
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, user:%s", pJob->queryId, taskIdx,
         ctgTaskTypeStr(task.type), user->user);
D
dapan1121 已提交
311 312 313 314

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
315
int32_t ctgInitGetSvrVerTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
D
dapan1121 已提交
316 317 318 319 320 321 322 323
  SCtgTask task = {0};

  task.type = CTG_TASK_GET_SVR_VER;
  task.taskId = taskIdx;
  task.pJob = pJob;

  taosArrayPush(pJob->pTasks, &task);

D
dapan1121 已提交
324
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized", pJob->queryId, taskIdx, ctgTaskTypeStr(task.type));
D
dapan1121 已提交
325 326 327 328

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
329 330
int32_t ctgInitGetTbIndexTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
  SName*   name = (SName*)param;
D
dapan1121 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
  SCtgTask task = {0};

  task.type = CTG_TASK_GET_TB_INDEX;
  task.taskId = taskIdx;
  task.pJob = pJob;

  task.taskCtx = taosMemoryCalloc(1, sizeof(SCtgTbIndexCtx));
  if (NULL == task.taskCtx) {
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

  SCtgTbIndexCtx* ctx = task.taskCtx;
  ctx->pName = taosMemoryMalloc(sizeof(*name));
  if (NULL == ctx->pName) {
    taosMemoryFree(task.taskCtx);
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }
348

D
dapan1121 已提交
349 350 351 352
  memcpy(ctx->pName, name, sizeof(*name));

  taosArrayPush(pJob->pTasks, &task);

dengyihao's avatar
dengyihao 已提交
353 354
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, tbName:%s", pJob->queryId, taskIdx,
         ctgTaskTypeStr(task.type), name->tname);
D
dapan1121 已提交
355 356 357 358

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
359 360
int32_t ctgInitGetTbCfgTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
  SName*   name = (SName*)param;
D
dapan1121 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
  SCtgTask task = {0};

  task.type = CTG_TASK_GET_TB_CFG;
  task.taskId = taskIdx;
  task.pJob = pJob;

  task.taskCtx = taosMemoryCalloc(1, sizeof(SCtgTbCfgCtx));
  if (NULL == task.taskCtx) {
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

  SCtgTbCfgCtx* ctx = task.taskCtx;
  ctx->pName = taosMemoryMalloc(sizeof(*name));
  if (NULL == ctx->pName) {
    taosMemoryFree(task.taskCtx);
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }
378

D
dapan1121 已提交
379 380 381 382
  memcpy(ctx->pName, name, sizeof(*name));

  taosArrayPush(pJob->pTasks, &task);

dengyihao's avatar
dengyihao 已提交
383 384
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, tbName:%s", pJob->queryId, taskIdx,
         ctgTaskTypeStr(task.type), name->tname);
D
dapan1121 已提交
385 386 387 388

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
389
int32_t ctgHandleForceUpdate(SCatalog* pCtg, int32_t taskNum, SCtgJob* pJob, const SCatalogReq* pReq) {
D
dapan1121 已提交
390
  SHashObj* pDb = taosHashInit(taskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
D
dapan1121 已提交
391 392 393 394
  SHashObj* pTb = taosHashInit(taskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  if (NULL == pDb || NULL == pTb) {
    taosHashCleanup(pDb);
    taosHashCleanup(pTb);
D
dapan1121 已提交
395 396
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }
397

D
dapan1121 已提交
398 399 400 401
  for (int32_t i = 0; i < pJob->dbVgNum; ++i) {
    char* dbFName = taosArrayGet(pReq->pDbVgroup, i);
    taosHashPut(pDb, dbFName, strlen(dbFName), dbFName, TSDB_DB_FNAME_LEN);
  }
D
dapan1121 已提交
402

D
dapan1121 已提交
403 404 405 406
  for (int32_t i = 0; i < pJob->dbCfgNum; ++i) {
    char* dbFName = taosArrayGet(pReq->pDbCfg, i);
    taosHashPut(pDb, dbFName, strlen(dbFName), dbFName, TSDB_DB_FNAME_LEN);
  }
D
dapan1121 已提交
407

D
dapan1121 已提交
408 409 410 411
  for (int32_t i = 0; i < pJob->dbInfoNum; ++i) {
    char* dbFName = taosArrayGet(pReq->pDbInfo, i);
    taosHashPut(pDb, dbFName, strlen(dbFName), dbFName, TSDB_DB_FNAME_LEN);
  }
D
dapan1121 已提交
412

D
dapan1121 已提交
413 414 415 416 417 418 419 420 421
  int32_t dbNum = taosArrayGetSize(pReq->pTableMeta);
  for (int32_t i = 0; i < dbNum; ++i) {
    STablesReq* p = taosArrayGet(pReq->pTableMeta, i);
    taosHashPut(pDb, p->dbFName, strlen(p->dbFName), p->dbFName, TSDB_DB_FNAME_LEN);
    int32_t tbNum = taosArrayGetSize(p->pTables);
    for (int32_t m = 0; m < tbNum; ++m) {
      SName* name = taosArrayGet(p->pTables, m);
      taosHashPut(pTb, name, sizeof(SName), name, sizeof(SName));
    }
D
dapan1121 已提交
422
  }
D
dapan1121 已提交
423 424 425 426 427 428 429 430 431 432

  dbNum = taosArrayGetSize(pReq->pTableHash);
  for (int32_t i = 0; i < dbNum; ++i) {
    STablesReq* p = taosArrayGet(pReq->pTableHash, i);
    taosHashPut(pDb, p->dbFName, strlen(p->dbFName), p->dbFName, TSDB_DB_FNAME_LEN);
    int32_t tbNum = taosArrayGetSize(p->pTables);
    for (int32_t m = 0; m < tbNum; ++m) {
      SName* name = taosArrayGet(p->pTables, m);
      taosHashPut(pTb, name, sizeof(SName), name, sizeof(SName));
    }
D
dapan1121 已提交
433
  }
D
dapan1121 已提交
434

D
dapan1121 已提交
435 436
  for (int32_t i = 0; i < pJob->tbCfgNum; ++i) {
    SName* name = taosArrayGet(pReq->pTableCfg, i);
dengyihao's avatar
dengyihao 已提交
437
    char   dbFName[TSDB_DB_FNAME_LEN];
D
dapan1121 已提交
438 439 440 441
    tNameGetFullDbName(name, dbFName);
    taosHashPut(pDb, dbFName, strlen(dbFName), dbFName, TSDB_DB_FNAME_LEN);
  }

D
dapan1121 已提交
442 443 444 445
  char* dbFName = taosHashIterate(pDb, NULL);
  while (dbFName) {
    ctgDropDbVgroupEnqueue(pCtg, dbFName, true);
    dbFName = taosHashIterate(pDb, dbFName);
D
dapan1121 已提交
446 447
  }

D
dapan1121 已提交
448 449
  taosHashCleanup(pDb);

D
dapan1121 已提交
450
  // REFRESH TABLE META
D
dapan1121 已提交
451

D
dapan1121 已提交
452 453 454 455
  for (int32_t i = 0; i < pJob->tbCfgNum; ++i) {
    SName* name = taosArrayGet(pReq->pTableCfg, i);
    taosHashPut(pTb, name, sizeof(SName), name, sizeof(SName));
  }
D
dapan1121 已提交
456

D
dapan1121 已提交
457 458
  SName* name = taosHashIterate(pTb, NULL);
  while (name) {
D
dapan1121 已提交
459
    ctgRemoveTbMeta(pCtg, name);
D
dapan1121 已提交
460
    name = taosHashIterate(pTb, name);
D
dapan1121 已提交
461 462
  }

D
dapan1121 已提交
463 464
  taosHashCleanup(pTb);

D
dapan1121 已提交
465 466 467 468 469
  for (int32_t i = 0; i < pJob->tbIndexNum; ++i) {
    SName* name = taosArrayGet(pReq->pTableIndex, i);
    ctgDropTbIndexEnqueue(pCtg, name, true);
  }

D
dapan1121 已提交
470 471
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
472

dengyihao's avatar
dengyihao 已提交
473
int32_t ctgInitTask(SCtgJob* pJob, CTG_TASK_TYPE type, void* param, int32_t* taskId) {
K
kailixu 已提交
474
  int32_t code = 0;
D
dapan1121 已提交
475 476 477
  int32_t tid = atomic_fetch_add_32(&pJob->taskIdx, 1);

  CTG_LOCK(CTG_WRITE, &pJob->taskLock);
K
kailixu 已提交
478
  CTG_ERR_JRET((*gCtgAsyncFps[type].initFp)(pJob, tid, param));
D
dapan1121 已提交
479 480 481 482

  if (taskId) {
    *taskId = tid;
  }
483

K
kailixu 已提交
484 485
_return:
  CTG_UNLOCK(CTG_WRITE, &pJob->taskLock);
dengyihao's avatar
dengyihao 已提交
486

K
kailixu 已提交
487
  return code;
D
dapan1121 已提交
488 489
}

dengyihao's avatar
dengyihao 已提交
490 491
int32_t ctgInitJob(SCatalog* pCtg, SRequestConnInfo* pConn, SCtgJob** job, const SCatalogReq* pReq, catalogCallback fp,
                   void* param) {
D
dapan1121 已提交
492
  int32_t code = 0;
493 494
  int64_t st = taosGetTimestampUs();

495
  int32_t tbMetaNum = (int32_t)ctgGetTablesReqNum(pReq->pTableMeta);
D
dapan1121 已提交
496
  int32_t dbVgNum = (int32_t)taosArrayGetSize(pReq->pDbVgroup);
497
  int32_t tbHashNum = (int32_t)ctgGetTablesReqNum(pReq->pTableHash);
D
dapan1121 已提交
498 499
  int32_t udfNum = (int32_t)taosArrayGetSize(pReq->pUdf);
  int32_t qnodeNum = pReq->qNodeRequired ? 1 : 0;
D
dapan1121 已提交
500
  int32_t dnodeNum = pReq->dNodeRequired ? 1 : 0;
D
dapan1121 已提交
501
  int32_t svrVerNum = pReq->svrVerRequired ? 1 : 0;
D
dapan1121 已提交
502 503 504
  int32_t dbCfgNum = (int32_t)taosArrayGetSize(pReq->pDbCfg);
  int32_t indexNum = (int32_t)taosArrayGetSize(pReq->pIndex);
  int32_t userNum = (int32_t)taosArrayGetSize(pReq->pUser);
D
dapan1121 已提交
505
  int32_t dbInfoNum = (int32_t)taosArrayGetSize(pReq->pDbInfo);
D
dapan1121 已提交
506
  int32_t tbIndexNum = (int32_t)taosArrayGetSize(pReq->pTableIndex);
D
dapan1121 已提交
507
  int32_t tbCfgNum = (int32_t)taosArrayGetSize(pReq->pTableCfg);
D
dapan1121 已提交
508

dengyihao's avatar
dengyihao 已提交
509 510
  int32_t taskNum = tbMetaNum + dbVgNum + udfNum + tbHashNum + qnodeNum + dnodeNum + svrVerNum + dbCfgNum + indexNum +
                    userNum + dbInfoNum + tbIndexNum + tbCfgNum;
511

D
dapan1121 已提交
512 513
  *job = taosMemoryCalloc(1, sizeof(SCtgJob));
  if (NULL == *job) {
D
dapan1121 已提交
514
    ctgError("failed to calloc, size:%d, reqId:0x%" PRIx64, (int32_t)sizeof(SCtgJob), pConn->requestId);
D
dapan1121 已提交
515 516 517
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

dengyihao's avatar
dengyihao 已提交
518
  SCtgJob* pJob = *job;
D
dapan1121 已提交
519 520

  pJob->subTaskNum = taskNum;
D
dapan1121 已提交
521
  pJob->queryId = pConn->requestId;
D
dapan1121 已提交
522
  pJob->userFp = fp;
dengyihao's avatar
dengyihao 已提交
523 524
  pJob->pCtg = pCtg;
  pJob->conn = *pConn;
D
dapan1121 已提交
525
  pJob->userParam = param;
526

D
dapan1121 已提交
527 528 529
  pJob->tbMetaNum = tbMetaNum;
  pJob->tbHashNum = tbHashNum;
  pJob->qnodeNum = qnodeNum;
D
dapan1121 已提交
530
  pJob->dnodeNum = dnodeNum;
D
dapan1121 已提交
531 532 533 534 535
  pJob->dbVgNum = dbVgNum;
  pJob->udfNum = udfNum;
  pJob->dbCfgNum = dbCfgNum;
  pJob->indexNum = indexNum;
  pJob->userNum = userNum;
D
dapan1121 已提交
536
  pJob->dbInfoNum = dbInfoNum;
D
dapan1121 已提交
537
  pJob->tbIndexNum = tbIndexNum;
D
dapan1121 已提交
538
  pJob->tbCfgNum = tbCfgNum;
D
dapan1121 已提交
539
  pJob->svrVerNum = svrVerNum;
540

D
dapan1121 已提交
541
#if CTG_BATCH_FETCH
dengyihao's avatar
dengyihao 已提交
542 543
  pJob->pBatchs =
      taosHashInit(CTG_DEFAULT_BATCH_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
D
dapan1121 已提交
544 545 546 547 548
  if (NULL == pJob->pBatchs) {
    ctgError("taosHashInit %d batch failed", CTG_DEFAULT_BATCH_NUM);
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
  }
#endif
549

D
dapan1121 已提交
550
  pJob->pTasks = taosArrayInit(taskNum, sizeof(SCtgTask));
D
dapan1121 已提交
551
  if (NULL == pJob->pTasks) {
D
dapan1121 已提交
552
    ctgError("taosArrayInit %d tasks failed", taskNum);
D
dapan1121 已提交
553 554 555
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
556 557
  if (pReq->forceUpdate && taskNum) {
    CTG_ERR_JRET(ctgHandleForceUpdate(pCtg, taskNum, pJob, pReq));
D
dapan1121 已提交
558 559
  }

D
dapan1121 已提交
560
  for (int32_t i = 0; i < dbVgNum; ++i) {
561
    char* dbFName = taosArrayGet(pReq->pDbVgroup, i);
D
dapan1121 已提交
562
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_DB_VGROUP, dbFName, NULL));
D
dapan1121 已提交
563 564 565
  }

  for (int32_t i = 0; i < dbCfgNum; ++i) {
566
    char* dbFName = taosArrayGet(pReq->pDbCfg, i);
D
dapan1121 已提交
567
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_DB_CFG, dbFName, NULL));
D
dapan1121 已提交
568 569
  }

D
dapan1121 已提交
570
  for (int32_t i = 0; i < dbInfoNum; ++i) {
571
    char* dbFName = taosArrayGet(pReq->pDbInfo, i);
D
dapan1121 已提交
572
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_DB_INFO, dbFName, NULL));
D
dapan1121 已提交
573 574
  }

D
dapan1121 已提交
575
#if 0
D
dapan1121 已提交
576
  for (int32_t i = 0; i < tbMetaNum; ++i) {
577
    SName* name = taosArrayGet(pReq->pTableMeta, i);
D
dapan1121 已提交
578
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_TB_META, name, NULL));
D
dapan1121 已提交
579
  }
D
dapan1121 已提交
580 581 582 583 584
#else
  if (tbMetaNum > 0) {
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_TB_META_BATCH, pReq->pTableMeta, NULL));
  }
#endif
D
dapan1121 已提交
585

586
#if 0
D
dapan1121 已提交
587
  for (int32_t i = 0; i < tbHashNum; ++i) {
588
    SName* name = taosArrayGet(pReq->pTableHash, i);
D
dapan1121 已提交
589
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_TB_HASH, name, NULL));
D
dapan1121 已提交
590
  }
591 592 593 594 595
#else
  if (tbHashNum > 0) {
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_TB_HASH_BATCH, pReq->pTableHash, NULL));
  }
#endif
D
dapan1121 已提交
596

D
dapan1121 已提交
597 598
  for (int32_t i = 0; i < tbIndexNum; ++i) {
    SName* name = taosArrayGet(pReq->pTableIndex, i);
D
dapan1121 已提交
599 600 601 602 603 604
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_TB_INDEX, name, NULL));
  }

  for (int32_t i = 0; i < tbCfgNum; ++i) {
    SName* name = taosArrayGet(pReq->pTableCfg, i);
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_TB_CFG, name, NULL));
D
dapan1121 已提交
605 606
  }

D
dapan1121 已提交
607
  for (int32_t i = 0; i < indexNum; ++i) {
608
    char* indexName = taosArrayGet(pReq->pIndex, i);
D
dapan1121 已提交
609
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_INDEX, indexName, NULL));
D
dapan1121 已提交
610 611 612
  }

  for (int32_t i = 0; i < udfNum; ++i) {
613
    char* udfName = taosArrayGet(pReq->pUdf, i);
D
dapan1121 已提交
614
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_UDF, udfName, NULL));
D
dapan1121 已提交
615 616 617
  }

  for (int32_t i = 0; i < userNum; ++i) {
618
    SUserAuthInfo* user = taosArrayGet(pReq->pUser, i);
D
dapan1121 已提交
619
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_USER, user, NULL));
D
dapan1121 已提交
620 621 622
  }

  if (qnodeNum) {
D
dapan1121 已提交
623
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_QNODE, NULL, NULL));
D
dapan1121 已提交
624 625
  }

D
dapan1121 已提交
626 627 628 629
  if (dnodeNum) {
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_DNODE, NULL, NULL));
  }

D
dapan1121 已提交
630 631 632 633
  if (svrVerNum) {
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_SVR_VER, NULL, NULL));
  }

H
Haojun Liao 已提交
634 635 636 637 638 639 640 641
  pJob->refId = taosAddRef(gCtgMgmt.jobPool, pJob);
  if (pJob->refId < 0) {
    ctgError("add job to ref failed, error: %s", tstrerror(terrno));
    CTG_ERR_JRET(terrno);
  }

  taosAcquireRef(gCtgMgmt.jobPool, pJob->refId);

H
Hongze Cheng 已提交
642
  double el = (taosGetTimestampUs() - st) / 1000.0;
643 644
  qDebug("QID:0x%" PRIx64 ", jobId: 0x%" PRIx64 " initialized, task num %d, forceUpdate %d, elapsed time:%.2f ms",
         pJob->queryId, pJob->refId, taskNum, pReq->forceUpdate, el);
H
Haojun Liao 已提交
645 646
  return TSDB_CODE_SUCCESS;

D
dapan1121 已提交
647
_return:
D
dapan1121 已提交
648
  ctgFreeJob(*job);
D
dapan1121 已提交
649 650 651 652 653 654
  CTG_RET(code);
}

int32_t ctgDumpTbMetaRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pTableMeta) {
D
dapan1121 已提交
655
    pJob->jobRes.pTableMeta = taosArrayInit(pJob->tbMetaNum, sizeof(SMetaRes));
D
dapan1121 已提交
656 657 658 659 660
    if (NULL == pJob->jobRes.pTableMeta) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
661 662
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pTableMeta, &res);
D
dapan1121 已提交
663 664 665 666

  return TSDB_CODE_SUCCESS;
}

667
int32_t ctgDumpTbMetasRes(SCtgTask* pTask) {
D
dapan1121 已提交
668 669 670 671 672 673 674
  SCtgJob* pJob = pTask->pJob;

  pJob->jobRes.pTableMeta = pTask->res;

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
675 676 677
int32_t ctgDumpDbVgRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pDbVgroup) {
D
dapan1121 已提交
678
    pJob->jobRes.pDbVgroup = taosArrayInit(pJob->dbVgNum, sizeof(SMetaRes));
D
dapan1121 已提交
679 680 681 682 683
    if (NULL == pJob->jobRes.pDbVgroup) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
684 685
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pDbVgroup, &res);
D
dapan1121 已提交
686 687 688 689 690 691 692

  return TSDB_CODE_SUCCESS;
}

int32_t ctgDumpTbHashRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pTableHash) {
D
dapan1121 已提交
693
    pJob->jobRes.pTableHash = taosArrayInit(pJob->tbHashNum, sizeof(SMetaRes));
D
dapan1121 已提交
694 695 696 697 698
    if (NULL == pJob->jobRes.pTableHash) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
699 700
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pTableHash, &res);
D
dapan1121 已提交
701 702 703 704

  return TSDB_CODE_SUCCESS;
}

705
int32_t ctgDumpTbHashsRes(SCtgTask* pTask) {
706 707 708 709 710 711 712
  SCtgJob* pJob = pTask->pJob;

  pJob->jobRes.pTableHash = pTask->res;

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
713 714 715 716 717 718 719 720 721 722
int32_t ctgDumpTbIndexRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pTableIndex) {
    pJob->jobRes.pTableIndex = taosArrayInit(pJob->tbIndexNum, sizeof(SMetaRes));
    if (NULL == pJob->jobRes.pTableIndex) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
D
dapan1121 已提交
723
  taosArrayPush(pJob->jobRes.pTableIndex, &res);
D
dapan1121 已提交
724 725 726 727

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
int32_t ctgDumpTbCfgRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pTableCfg) {
    pJob->jobRes.pTableCfg = taosArrayInit(pJob->tbCfgNum, sizeof(SMetaRes));
    if (NULL == pJob->jobRes.pTableCfg) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pTableCfg, &res);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
743 744 745
int32_t ctgDumpIndexRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pIndex) {
D
dapan1121 已提交
746
    pJob->jobRes.pIndex = taosArrayInit(pJob->indexNum, sizeof(SMetaRes));
D
dapan1121 已提交
747 748 749 750 751
    if (NULL == pJob->jobRes.pIndex) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
752 753
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pIndex, &res);
D
dapan1121 已提交
754 755 756 757 758 759

  return TSDB_CODE_SUCCESS;
}

int32_t ctgDumpQnodeRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
D
dapan1121 已提交
760 761 762 763 764 765
  if (NULL == pJob->jobRes.pQnodeList) {
    pJob->jobRes.pQnodeList = taosArrayInit(1, sizeof(SMetaRes));
    if (NULL == pJob->jobRes.pQnodeList) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }
D
dapan1121 已提交
766

D
dapan1121 已提交
767 768
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pQnodeList, &res);
D
dapan1121 已提交
769 770 771 772

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
int32_t ctgDumpDnodeRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pDnodeList) {
    pJob->jobRes.pDnodeList = taosArrayInit(1, sizeof(SMetaRes));
    if (NULL == pJob->jobRes.pDnodeList) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pDnodeList, &res);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
788 789 790
int32_t ctgDumpDbCfgRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pDbCfg) {
D
dapan1121 已提交
791
    pJob->jobRes.pDbCfg = taosArrayInit(pJob->dbCfgNum, sizeof(SMetaRes));
D
dapan1121 已提交
792 793 794 795 796
    if (NULL == pJob->jobRes.pDbCfg) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
797 798
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pDbCfg, &res);
D
dapan1121 已提交
799 800 801 802

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
803 804 805
int32_t ctgDumpDbInfoRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pDbInfo) {
D
dapan1121 已提交
806
    pJob->jobRes.pDbInfo = taosArrayInit(pJob->dbInfoNum, sizeof(SMetaRes));
D
dapan1121 已提交
807 808 809 810 811
    if (NULL == pJob->jobRes.pDbInfo) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
812 813
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pDbInfo, &res);
D
dapan1121 已提交
814 815 816 817

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
818 819 820
int32_t ctgDumpUdfRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pUdfList) {
D
dapan1121 已提交
821
    pJob->jobRes.pUdfList = taosArrayInit(pJob->udfNum, sizeof(SMetaRes));
D
dapan1121 已提交
822 823 824 825 826
    if (NULL == pJob->jobRes.pUdfList) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
827 828
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pUdfList, &res);
D
dapan1121 已提交
829 830 831 832 833 834 835

  return TSDB_CODE_SUCCESS;
}

int32_t ctgDumpUserRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pUser) {
D
dapan1121 已提交
836
    pJob->jobRes.pUser = taosArrayInit(pJob->userNum, sizeof(SMetaRes));
D
dapan1121 已提交
837 838 839 840 841
    if (NULL == pJob->jobRes.pUser) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
842 843
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pUser, &res);
D
dapan1121 已提交
844 845 846 847

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
848 849 850 851 852 853 854 855 856 857 858
int32_t ctgDumpSvrVer(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pSvrVer) {
    pJob->jobRes.pSvrVer = taosMemoryCalloc(1, sizeof(SMetaRes));
    if (NULL == pJob->jobRes.pSvrVer) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

  pJob->jobRes.pSvrVer->code = pTask->code;
  pJob->jobRes.pSvrVer->pRes = pTask->res;
859

D
dapan1121 已提交
860 861 862
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
863
int32_t ctgCallSubCb(SCtgTask* pTask) {
D
dapan1121 已提交
864
  int32_t code = 0;
865

D
dapan1121 已提交
866
  CTG_LOCK(CTG_WRITE, &pTask->lock);
867

D
dapan1121 已提交
868 869
  int32_t parentNum = taosArrayGetSize(pTask->pParents);
  for (int32_t i = 0; i < parentNum; ++i) {
dengyihao's avatar
dengyihao 已提交
870 871
    SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
    SCtgTask*   pParent = taosArrayGetP(pTask->pParents, i);
872

D
dapan1121 已提交
873 874 875 876 877 878 879
    pParent->subRes.code = pTask->code;
    if (TSDB_CODE_SUCCESS == pTask->code) {
      code = (*gCtgAsyncFps[pTask->type].cloneFp)(pTask, &pParent->subRes.res);
      if (code) {
        pParent->subRes.code = code;
      }
    }
880

dengyihao's avatar
dengyihao 已提交
881
    SCtgMsgCtx* pParMsgCtx = CTG_GET_TASK_MSGCTX(pParent, -1);
D
dapan1121 已提交
882

883
    pParMsgCtx->pBatchs = pMsgCtx->pBatchs;
D
dapan1121 已提交
884 885
    CTG_ERR_JRET(pParent->subRes.fp(pParent));
  }
886

D
dapan1121 已提交
887 888 889 890
_return:

  CTG_UNLOCK(CTG_WRITE, &pTask->lock);

891
  CTG_RET(code);
D
dapan1121 已提交
892 893
}

D
dapan1121 已提交
894 895
int32_t ctgCallUserCb(void* param) {
  SCtgJob* pJob = (SCtgJob*)param;
D
dapan1121 已提交
896 897

  qDebug("QID:0x%" PRIx64 " ctg start to call user cb with rsp %s", pJob->queryId, tstrerror(pJob->jobResCode));
898

D
dapan1121 已提交
899 900
  (*pJob->userFp)(&pJob->jobRes, pJob->userParam, pJob->jobResCode);

D
dapan1121 已提交
901 902
  qDebug("QID:0x%" PRIx64 " ctg end to call user cb", pJob->queryId);

D
dapan1121 已提交
903 904 905 906
  taosRemoveRef(gCtgMgmt.jobPool, pJob->refId);

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
907

dengyihao's avatar
dengyihao 已提交
908
void ctgUpdateJobErrCode(SCtgJob* pJob, int32_t errCode) {
dengyihao's avatar
dengyihao 已提交
909
  if (!NEED_CLIENT_HANDLE_ERROR(errCode) || errCode == TSDB_CODE_SUCCESS) return;
dengyihao's avatar
dengyihao 已提交
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932

  int32_t origCode = atomic_load_32(&pJob->jobResCode);
  if (TSDB_CODE_SUCCESS == origCode) {
    if (origCode == atomic_val_compare_exchange_32(&pJob->jobResCode, origCode, errCode)) {
      goto _return;
    }
    origCode = atomic_load_32(&pJob->jobResCode);
  }

  if (NEED_CLIENT_HANDLE_ERROR(origCode)) {
    return;
  }

  if (NEED_CLIENT_HANDLE_ERROR(errCode)) {
    atomic_store_32(&pJob->jobResCode, errCode);
    goto _return;
  }
  return;

_return:
  qDebug("QID:0x%" PRIx64 " ctg job errCode updated to %s", pJob->queryId, tstrerror(errCode));
}

D
dapan1121 已提交
933 934
int32_t ctgHandleTaskEnd(SCtgTask* pTask, int32_t rspCode) {
  SCtgJob* pJob = pTask->pJob;
dengyihao's avatar
dengyihao 已提交
935
  int32_t  code = 0;
D
dapan1121 已提交
936

D
dapan1121 已提交
937 938 939 940
  if (CTG_TASK_DONE == pTask->status) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
941
  qDebug("QID:0x%" PRIx64 " task %d end with res %s", pJob->queryId, pTask->taskId, tstrerror(rspCode));
D
dapan1121 已提交
942

D
dapan1121 已提交
943
  pTask->code = rspCode;
D
dapan1121 已提交
944 945
  pTask->status = CTG_TASK_DONE;

D
dapan1121 已提交
946
  ctgCallSubCb(pTask);
D
dapan1121 已提交
947 948 949

  int32_t taskDone = atomic_add_fetch_32(&pJob->taskDone, 1);
  if (taskDone < taosArrayGetSize(pJob->pTasks)) {
dengyihao's avatar
dengyihao 已提交
950 951
    qDebug("QID:0x%" PRIx64 " task done: %d, total: %d", pJob->queryId, taskDone,
           (int32_t)taosArrayGetSize(pJob->pTasks));
dengyihao's avatar
dengyihao 已提交
952 953

    ctgUpdateJobErrCode(pJob, rspCode);
D
dapan1121 已提交
954 955 956 957 958 959 960
    return TSDB_CODE_SUCCESS;
  }

  CTG_ERR_JRET(ctgMakeAsyncRes(pJob));

_return:

dengyihao's avatar
dengyihao 已提交
961 962
  ctgUpdateJobErrCode(pJob, rspCode);
  // pJob->jobResCode = code;
D
dapan1121 已提交
963

dengyihao's avatar
dengyihao 已提交
964 965
  // taosSsleep(2);
  // qDebug("QID:0x%" PRIx64 " ctg after sleep", pJob->queryId);
966

D
dapan1121 已提交
967
  taosAsyncExec(ctgCallUserCb, pJob, NULL);
968

D
dapan1121 已提交
969 970 971
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
972 973 974 975 976
int32_t ctgHandleGetTbMetaRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t           code = 0;
  SCtgDBCache*      dbCache = NULL;
  SCtgTask*         pTask = tReq->pTask;
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
977
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
978 979 980 981 982
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, tReq->msgIdx);
  SCtgTbMetaCtx*    ctx = (SCtgTbMetaCtx*)pTask->taskCtx;
  SName*            pName = ctx->pName;
  int32_t           flag = ctx->flag;
  int32_t*          vgId = &ctx->vgId;
D
dapan1121 已提交
983 984 985 986 987 988 989 990 991 992 993 994 995

  CTG_ERR_JRET(ctgProcessRspMsg(pMsgCtx->out, reqType, pMsg->pData, pMsg->len, rspCode, pMsgCtx->target));

  switch (reqType) {
    case TDMT_MND_USE_DB: {
      SUseDbOutput* pOut = (SUseDbOutput*)pMsgCtx->out;

      SVgroupInfo vgInfo = {0};
      CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, pOut->dbVgroup, pName, &vgInfo));

      ctgDebug("will refresh tbmeta, not supposed to be stb, tbName:%s, flag:%d", tNameGetTableName(pName), flag);

      *vgId = vgInfo.vgId;
D
dapan1121 已提交
996
      CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, pName, &vgInfo, NULL, tReq));
D
dapan1121 已提交
997 998 999 1000 1001

      return TSDB_CODE_SUCCESS;
    }
    case TDMT_MND_TABLE_META: {
      STableMetaOutput* pOut = (STableMetaOutput*)pMsgCtx->out;
1002

D
dapan1121 已提交
1003 1004 1005 1006
      if (CTG_IS_META_NULL(pOut->metaType)) {
        if (CTG_FLAG_IS_STB(flag)) {
          char dbFName[TSDB_DB_FNAME_LEN] = {0};
          tNameGetFullDbName(pName, dbFName);
1007

D
dapan1121 已提交
1008 1009 1010 1011
          CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache));
          if (NULL != dbCache) {
            SVgroupInfo vgInfo = {0};
            CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, dbCache->vgCache.vgInfo, pName, &vgInfo));
1012

D
dapan1121 已提交
1013 1014
            ctgDebug("will refresh tbmeta, supposed to be stb, tbName:%s, flag:%d", tNameGetTableName(pName), flag);

1015
            *vgId = vgInfo.vgId;
D
dapan1121 已提交
1016
            CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, pName, &vgInfo, NULL, tReq));
D
dapan1121 已提交
1017 1018 1019 1020

            ctgReleaseVgInfoToCache(pCtg, dbCache);
          } else {
            SBuildUseDBInput input = {0};
1021

D
dapan1121 已提交
1022 1023
            tstrncpy(input.db, dbFName, tListLen(input.db));
            input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
1024

D
dapan1121 已提交
1025
            CTG_ERR_JRET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, tReq));
D
dapan1121 已提交
1026 1027 1028 1029
          }

          return TSDB_CODE_SUCCESS;
        }
1030

D
dapan1121 已提交
1031 1032
        ctgError("no tbmeta got, tbName:%s", tNameGetTableName(pName));
        ctgRemoveTbMetaFromCache(pCtg, pName, false);
1033

D
dapan1121 已提交
1034 1035 1036 1037 1038 1039 1040 1041
        CTG_ERR_JRET(CTG_ERR_CODE_TABLE_NOT_EXIST);
      }

      if (pMsgCtx->lastOut) {
        TSWAP(pMsgCtx->out, pMsgCtx->lastOut);
        STableMetaOutput* pLastOut = (STableMetaOutput*)pMsgCtx->out;
        TSWAP(pLastOut->tbMeta, pOut->tbMeta);
      }
1042

D
dapan1121 已提交
1043 1044 1045 1046
      break;
    }
    case TDMT_VND_TABLE_META: {
      STableMetaOutput* pOut = (STableMetaOutput*)pMsgCtx->out;
1047

D
dapan1121 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056
      if (CTG_IS_META_NULL(pOut->metaType)) {
        ctgError("no tbmeta got, tbNmae:%s", tNameGetTableName(pName));
        ctgRemoveTbMetaFromCache(pCtg, pName, false);
        CTG_ERR_JRET(CTG_ERR_CODE_TABLE_NOT_EXIST);
      }

      if (CTG_FLAG_IS_STB(flag)) {
        break;
      }
1057

D
dapan1121 已提交
1058 1059
      if (CTG_IS_META_TABLE(pOut->metaType) && TSDB_SUPER_TABLE == pOut->tbMeta->tableType) {
        ctgDebug("will continue to refresh tbmeta since got stb, tbName:%s", tNameGetTableName(pName));
1060

D
dapan1121 已提交
1061
        taosMemoryFreeClear(pOut->tbMeta);
1062

D
dapan1121 已提交
1063
        CTG_RET(ctgGetTbMetaFromMnode(pCtg, pConn, pName, NULL, tReq));
D
dapan1121 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072
      } else if (CTG_IS_META_BOTH(pOut->metaType)) {
        int32_t exist = 0;
        if (!CTG_FLAG_IS_FORCE_UPDATE(flag)) {
          SName stbName = *pName;
          strcpy(stbName.tname, pOut->tbName);
          SCtgTbMetaCtx stbCtx = {0};
          stbCtx.flag = flag;
          stbCtx.pName = &stbName;

1073
          taosMemoryFreeClear(pOut->tbMeta);
D
dapan1121 已提交
1074 1075 1076 1077 1078
          CTG_ERR_JRET(ctgReadTbMetaFromCache(pCtg, &stbCtx, &pOut->tbMeta));
          if (pOut->tbMeta) {
            exist = 1;
          }
        }
1079

D
dapan1121 已提交
1080 1081
        if (0 == exist) {
          TSWAP(pMsgCtx->lastOut, pMsgCtx->out);
D
dapan1121 已提交
1082
          CTG_RET(ctgGetTbMetaFromMnodeImpl(pCtg, pConn, pOut->dbFName, pOut->tbName, NULL, tReq));
D
dapan1121 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
        }
      }
      break;
    }
    default:
      ctgError("invalid reqType %d", reqType);
      CTG_ERR_JRET(TSDB_CODE_INVALID_MSG);
  }

  STableMetaOutput* pOut = (STableMetaOutput*)pMsgCtx->out;

  ctgUpdateTbMetaToCache(pCtg, pOut, false);

  if (CTG_IS_META_BOTH(pOut->metaType)) {
    memcpy(pOut->tbMeta, &pOut->ctbMeta, sizeof(pOut->ctbMeta));
  }
1099

dengyihao's avatar
dengyihao 已提交
1100 1101 1102 1103 1104 1105 1106
  /*
    else if (CTG_IS_META_CTABLE(pOut->metaType)) {
      SName stbName = *pName;
      strcpy(stbName.tname, pOut->tbName);
      SCtgTbMetaCtx stbCtx = {0};
      stbCtx.flag = flag;
      stbCtx.pName = &stbName;
D
dapan1121 已提交
1107

dengyihao's avatar
dengyihao 已提交
1108 1109 1110 1111
      CTG_ERR_JRET(ctgReadTbMetaFromCache(pCtg, &stbCtx, &pOut->tbMeta));
      if (NULL == pOut->tbMeta) {
        ctgDebug("stb no longer exist, stbName:%s", stbName.tname);
        CTG_ERR_JRET(ctgRelaunchGetTbMetaTask(pTask));
D
dapan1121 已提交
1112

dengyihao's avatar
dengyihao 已提交
1113 1114 1115 1116 1117 1118
        return TSDB_CODE_SUCCESS;
      }

      memcpy(pOut->tbMeta, &pOut->ctbMeta, sizeof(pOut->ctbMeta));
    }
  */
D
dapan1121 已提交
1119 1120 1121 1122 1123 1124 1125 1126 1127

  TSWAP(pTask->res, pOut->tbMeta);

_return:

  if (dbCache) {
    ctgReleaseVgInfoToCache(pCtg, dbCache);
  }

1128
  if (code) {
dengyihao's avatar
dengyihao 已提交
1129 1130
    ctgTaskError("Get table %d.%s.%s meta failed with error %s", pName->acctId, pName->dbname, pName->tname,
                 tstrerror(code));
1131
  }
1132
  if (pTask->res || code) {
D
dapan1121 已提交
1133 1134
    ctgHandleTaskEnd(pTask, code);
  }
1135

D
dapan1121 已提交
1136 1137 1138
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1139 1140 1141 1142 1143
int32_t ctgHandleGetTbMetasRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t           code = 0;
  SCtgDBCache*      dbCache = NULL;
  SCtgTask*         pTask = tReq->pTask;
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1144
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1145 1146 1147 1148 1149 1150 1151
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, tReq->msgIdx);
  SCtgTbMetasCtx*   ctx = (SCtgTbMetasCtx*)pTask->taskCtx;
  SCtgFetch*        pFetch = taosArrayGet(ctx->pFetchs, tReq->msgIdx);
  SName*            pName = ctgGetFetchName(ctx->pNames, pFetch);
  int32_t           flag = pFetch->flag;
  int32_t*          vgId = &pFetch->vgId;
  bool              taskDone = false;
D
dapan1121 已提交
1152

D
dapan1121 已提交
1153
  CTG_ERR_JRET(ctgProcessRspMsg(pMsgCtx->out, reqType, pMsg->pData, pMsg->len, rspCode, pMsgCtx->target));
D
dapan1121 已提交
1154 1155 1156

  switch (reqType) {
    case TDMT_MND_USE_DB: {
D
dapan1121 已提交
1157
      SUseDbOutput* pOut = (SUseDbOutput*)pMsgCtx->out;
D
dapan1121 已提交
1158 1159

      SVgroupInfo vgInfo = {0};
D
dapan1121 已提交
1160
      CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, pOut->dbVgroup, pName, &vgInfo));
D
dapan1121 已提交
1161

1162
      ctgTaskDebug("will refresh tbmeta, not supposed to be stb, tbName:%s, flag:%d", tNameGetTableName(pName), flag);
D
dapan1121 已提交
1163

D
dapan1121 已提交
1164
      *vgId = vgInfo.vgId;
D
dapan1121 已提交
1165
      CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, pName, &vgInfo, NULL, tReq));
D
dapan1121 已提交
1166 1167 1168 1169

      return TSDB_CODE_SUCCESS;
    }
    case TDMT_MND_TABLE_META: {
D
dapan1121 已提交
1170
      STableMetaOutput* pOut = (STableMetaOutput*)pMsgCtx->out;
1171

D
dapan1121 已提交
1172
      if (CTG_IS_META_NULL(pOut->metaType)) {
D
dapan1121 已提交
1173
        if (CTG_FLAG_IS_STB(flag)) {
D
dapan1121 已提交
1174
          char dbFName[TSDB_DB_FNAME_LEN] = {0};
D
dapan1121 已提交
1175
          tNameGetFullDbName(pName, dbFName);
1176

D
dapan1121 已提交
1177 1178 1179
          CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache));
          if (NULL != dbCache) {
            SVgroupInfo vgInfo = {0};
D
dapan1121 已提交
1180
            CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, dbCache->vgCache.vgInfo, pName, &vgInfo));
1181

1182
            ctgTaskDebug("will refresh tbmeta, supposed to be stb, tbName:%s, flag:%d", tNameGetTableName(pName), flag);
D
dapan1121 已提交
1183

1184
            *vgId = vgInfo.vgId;
D
dapan1121 已提交
1185
            CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, pName, &vgInfo, NULL, tReq));
D
dapan1121 已提交
1186

D
dapan1121 已提交
1187
            ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1188 1189
          } else {
            SBuildUseDBInput input = {0};
1190

D
dapan1121 已提交
1191 1192
            tstrncpy(input.db, dbFName, tListLen(input.db));
            input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
1193

D
dapan1121 已提交
1194
            CTG_ERR_JRET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, tReq));
D
dapan1121 已提交
1195 1196 1197 1198
          }

          return TSDB_CODE_SUCCESS;
        }
1199

1200
        ctgTaskError("no tbmeta got, tbName:%s", tNameGetTableName(pName));
D
dapan1121 已提交
1201
        ctgRemoveTbMetaFromCache(pCtg, pName, false);
1202

D
dapan1121 已提交
1203 1204 1205
        CTG_ERR_JRET(CTG_ERR_CODE_TABLE_NOT_EXIST);
      }

D
dapan1121 已提交
1206 1207 1208
      if (pMsgCtx->lastOut) {
        TSWAP(pMsgCtx->out, pMsgCtx->lastOut);
        STableMetaOutput* pLastOut = (STableMetaOutput*)pMsgCtx->out;
D
dapan1121 已提交
1209 1210
        TSWAP(pLastOut->tbMeta, pOut->tbMeta);
      }
1211

D
dapan1121 已提交
1212 1213 1214
      break;
    }
    case TDMT_VND_TABLE_META: {
D
dapan1121 已提交
1215
      STableMetaOutput* pOut = (STableMetaOutput*)pMsgCtx->out;
1216

D
dapan1121 已提交
1217
      if (CTG_IS_META_NULL(pOut->metaType)) {
1218
        ctgTaskError("no tbmeta got, tbNmae:%s", tNameGetTableName(pName));
D
dapan1121 已提交
1219
        ctgRemoveTbMetaFromCache(pCtg, pName, false);
D
dapan1121 已提交
1220 1221 1222
        CTG_ERR_JRET(CTG_ERR_CODE_TABLE_NOT_EXIST);
      }

D
dapan1121 已提交
1223
      if (CTG_FLAG_IS_STB(flag)) {
D
dapan1121 已提交
1224 1225
        break;
      }
1226

D
dapan1121 已提交
1227
      if (CTG_IS_META_TABLE(pOut->metaType) && TSDB_SUPER_TABLE == pOut->tbMeta->tableType) {
1228
        ctgTaskDebug("will continue to refresh tbmeta since got stb, tbName:%s", tNameGetTableName(pName));
1229

D
dapan1121 已提交
1230
        taosMemoryFreeClear(pOut->tbMeta);
1231

D
dapan1121 已提交
1232
        CTG_RET(ctgGetTbMetaFromMnode(pCtg, pConn, pName, NULL, tReq));
D
dapan1121 已提交
1233 1234
      } else if (CTG_IS_META_BOTH(pOut->metaType)) {
        int32_t exist = 0;
D
dapan1121 已提交
1235 1236 1237 1238 1239 1240 1241
        if (!CTG_FLAG_IS_FORCE_UPDATE(flag)) {
          SName stbName = *pName;
          strcpy(stbName.tname, pOut->tbName);
          SCtgTbMetaCtx stbCtx = {0};
          stbCtx.flag = flag;
          stbCtx.pName = &stbName;

dengyihao's avatar
dengyihao 已提交
1242
          STableMeta* stbMeta = NULL;
1243
          (void)ctgReadTbMetaFromCache(pCtg, &stbCtx, &stbMeta);
D
dapan1121 已提交
1244
          if (stbMeta && stbMeta->sversion >= pOut->tbMeta->sversion) {
1245
            ctgTaskDebug("use cached stb meta, tbName:%s", tNameGetTableName(pName));
D
dapan1121 已提交
1246
            exist = 1;
dengyihao's avatar
dengyihao 已提交
1247
            taosMemoryFreeClear(stbMeta);
D
dapan1121 已提交
1248
          } else {
1249
            ctgTaskDebug("need to get/update stb meta, tbName:%s", tNameGetTableName(pName));
D
dapan1121 已提交
1250 1251
            taosMemoryFreeClear(pOut->tbMeta);
            taosMemoryFreeClear(stbMeta);
D
dapan1121 已提交
1252
          }
D
dapan1121 已提交
1253
        }
1254

D
dapan1121 已提交
1255
        if (0 == exist) {
D
dapan1121 已提交
1256
          TSWAP(pMsgCtx->lastOut, pMsgCtx->out);
D
dapan1121 已提交
1257
          CTG_RET(ctgGetTbMetaFromMnodeImpl(pCtg, pConn, pOut->dbFName, pOut->tbName, NULL, tReq));
D
dapan1121 已提交
1258 1259 1260 1261 1262
        }
      }
      break;
    }
    default:
1263
      ctgTaskError("invalid reqType %d", reqType);
D
dapan1121 已提交
1264 1265 1266
      CTG_ERR_JRET(TSDB_CODE_INVALID_MSG);
  }

D
dapan1121 已提交
1267
  STableMetaOutput* pOut = (STableMetaOutput*)pMsgCtx->out;
D
dapan1121 已提交
1268 1269 1270 1271 1272

  ctgUpdateTbMetaToCache(pCtg, pOut, false);

  if (CTG_IS_META_BOTH(pOut->metaType)) {
    memcpy(pOut->tbMeta, &pOut->ctbMeta, sizeof(pOut->ctbMeta));
D
dapan1121 已提交
1273
  }
1274

dengyihao's avatar
dengyihao 已提交
1275 1276 1277 1278 1279 1280 1281
  /*
    else if (CTG_IS_META_CTABLE(pOut->metaType)) {
      SName stbName = *pName;
      strcpy(stbName.tname, pOut->tbName);
      SCtgTbMetaCtx stbCtx = {0};
      stbCtx.flag = flag;
      stbCtx.pName = &stbName;
D
dapan1121 已提交
1282

dengyihao's avatar
dengyihao 已提交
1283 1284 1285 1286
      CTG_ERR_JRET(ctgReadTbMetaFromCache(pCtg, &stbCtx, &pOut->tbMeta));
      if (NULL == pOut->tbMeta) {
        ctgDebug("stb no longer exist, stbName:%s", stbName.tname);
        CTG_ERR_JRET(ctgRelaunchGetTbMetaTask(pTask));
D
dapan1121 已提交
1287

dengyihao's avatar
dengyihao 已提交
1288 1289 1290 1291 1292 1293
        return TSDB_CODE_SUCCESS;
      }

      memcpy(pOut->tbMeta, &pOut->ctbMeta, sizeof(pOut->ctbMeta));
    }
  */
D
dapan1121 已提交
1294

1295
  SMetaRes* pRes = taosArrayGet(ctx->pResList, pFetch->resIdx);
D
dapan1121 已提交
1296 1297 1298 1299
  pRes->code = 0;
  pRes->pRes = pOut->tbMeta;
  pOut->tbMeta = NULL;
  if (0 == atomic_sub_fetch_32(&ctx->fetchNum, 1)) {
1300
    TSWAP(pTask->res, ctx->pResList);
1301
    taskDone = true;
D
dapan1121 已提交
1302
  }
D
dapan1121 已提交
1303 1304 1305 1306

_return:

  if (dbCache) {
D
dapan1121 已提交
1307
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1308 1309
  }

D
dapan1121 已提交
1310
  if (code) {
1311
    SMetaRes* pRes = taosArrayGet(ctx->pResList, pFetch->resIdx);
D
dapan1121 已提交
1312 1313 1314
    pRes->code = code;
    pRes->pRes = NULL;
    if (0 == atomic_sub_fetch_32(&ctx->fetchNum, 1)) {
1315
      TSWAP(pTask->res, ctx->pResList);
1316
      taskDone = true;
D
dapan1121 已提交
1317
    }
dengyihao's avatar
dengyihao 已提交
1318 1319
    ctgTaskError("Get table %d.%s.%s meta failed with error %s", pName->acctId, pName->dbname, pName->tname,
                 tstrerror(code));
D
dapan1121 已提交
1320 1321
  }

1322
  if (pTask->res && taskDone) {
D
dapan1121 已提交
1323 1324
    ctgHandleTaskEnd(pTask, code);
  }
1325

D
dapan1121 已提交
1326 1327 1328
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1329 1330 1331
int32_t ctgHandleGetDbVgRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t      code = 0;
  SCtgTask*    pTask = tReq->pTask;
D
dapan1121 已提交
1332
  SCtgDbVgCtx* ctx = (SCtgDbVgCtx*)pTask->taskCtx;
dengyihao's avatar
dengyihao 已提交
1333
  SCatalog*    pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1334 1335

  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));
D
dapan1121 已提交
1336 1337 1338 1339

  switch (reqType) {
    case TDMT_MND_USE_DB: {
      SUseDbOutput* pOut = (SUseDbOutput*)pTask->msgCtx.out;
dengyihao's avatar
dengyihao 已提交
1340
      SDBVgInfo*    pDb = NULL;
1341

D
dapan1121 已提交
1342 1343 1344 1345
      CTG_ERR_JRET(ctgGenerateVgList(pCtg, pOut->dbVgroup->vgHash, (SArray**)&pTask->res));

      CTG_ERR_JRET(cloneDbVgInfo(pOut->dbVgroup, &pDb));
      CTG_ERR_JRET(ctgUpdateVgroupEnqueue(pCtg, ctx->dbFName, pOut->dbId, pDb, false));
D
dapan1121 已提交
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360

      break;
    }
    default:
      ctgError("invalid reqType %d", reqType);
      CTG_ERR_JRET(TSDB_CODE_INVALID_MSG);
  }

_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1361 1362 1363
int32_t ctgHandleGetTbHashRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t        code = 0;
  SCtgTask*      pTask = tReq->pTask;
D
dapan1121 已提交
1364
  SCtgTbHashCtx* ctx = (SCtgTbHashCtx*)pTask->taskCtx;
dengyihao's avatar
dengyihao 已提交
1365
  SCatalog*      pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1366 1367

  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));
D
dapan1121 已提交
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378

  switch (reqType) {
    case TDMT_MND_USE_DB: {
      SUseDbOutput* pOut = (SUseDbOutput*)pTask->msgCtx.out;

      pTask->res = taosMemoryMalloc(sizeof(SVgroupInfo));
      if (NULL == pTask->res) {
        CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
      }

      CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, pOut->dbVgroup, ctx->pName, (SVgroupInfo*)pTask->res));
1379

D
dapan1121 已提交
1380
      CTG_ERR_JRET(ctgUpdateVgroupEnqueue(pCtg, ctx->dbFName, pOut->dbId, pOut->dbVgroup, false));
D
dapan1121 已提交
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
      pOut->dbVgroup = NULL;

      break;
    }
    default:
      ctgError("invalid reqType %d", reqType);
      CTG_ERR_JRET(TSDB_CODE_INVALID_MSG);
  }

_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1397 1398 1399
int32_t ctgHandleGetTbHashsRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t         code = 0;
  SCtgTask*       pTask = tReq->pTask;
1400
  SCtgTbHashsCtx* ctx = (SCtgTbHashsCtx*)pTask->taskCtx;
dengyihao's avatar
dengyihao 已提交
1401 1402 1403 1404
  SCatalog*       pCtg = pTask->pJob->pCtg;
  SCtgMsgCtx*     pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, tReq->msgIdx);
  SCtgFetch*      pFetch = taosArrayGet(ctx->pFetchs, tReq->msgIdx);
  bool            taskDone = false;
1405 1406 1407 1408 1409 1410 1411

  CTG_ERR_JRET(ctgProcessRspMsg(pMsgCtx->out, reqType, pMsg->pData, pMsg->len, rspCode, pMsgCtx->target));

  switch (reqType) {
    case TDMT_MND_USE_DB: {
      SUseDbOutput* pOut = (SUseDbOutput*)pMsgCtx->out;

1412
      STablesReq* pReq = taosArrayGet(ctx->pNames, pFetch->dbIdx);
D
dapan1121 已提交
1413
      CTG_ERR_JRET(ctgGetVgInfosFromHashValue(pCtg, tReq, pOut->dbVgroup, ctx, pMsgCtx->target, pReq->pTables, true));
1414

1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
      CTG_ERR_JRET(ctgUpdateVgroupEnqueue(pCtg, pMsgCtx->target, pOut->dbId, pOut->dbVgroup, false));
      pOut->dbVgroup = NULL;

      break;
    }
    default:
      ctgError("invalid reqType %d", reqType);
      CTG_ERR_JRET(TSDB_CODE_INVALID_MSG);
  }

  if (0 == atomic_sub_fetch_32(&ctx->fetchNum, 1)) {
    TSWAP(pTask->res, ctx->pResList);
1427
    taskDone = true;
1428 1429 1430 1431 1432
  }

_return:

  if (code) {
1433
    STablesReq* pReq = taosArrayGet(ctx->pNames, pFetch->dbIdx);
dengyihao's avatar
dengyihao 已提交
1434
    int32_t     num = taosArrayGetSize(pReq->pTables);
1435
    for (int32_t i = 0; i < num; ++i) {
dengyihao's avatar
dengyihao 已提交
1436
      SMetaRes* pRes = taosArrayGet(ctx->pResList, pFetch->resIdx + i);
1437 1438 1439
      pRes->code = code;
      pRes->pRes = NULL;
    }
1440

1441 1442
    if (0 == atomic_sub_fetch_32(&ctx->fetchNum, 1)) {
      TSWAP(pTask->res, ctx->pResList);
1443
      taskDone = true;
1444 1445 1446
    }
  }

1447
  if (pTask->res && taskDone) {
1448 1449 1450 1451 1452 1453
    ctgHandleTaskEnd(pTask, code);
  }

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1454 1455
int32_t ctgHandleGetTbIndexRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1456
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1457
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));
D
dapan1121 已提交
1458

D
dapan1121 已提交
1459
  STableIndex* pOut = (STableIndex*)pTask->msgCtx.out;
dengyihao's avatar
dengyihao 已提交
1460
  SArray*      pInfo = NULL;
D
dapan1121 已提交
1461 1462 1463
  CTG_ERR_JRET(ctgCloneTableIndex(pOut->pIndex, &pInfo));
  pTask->res = pInfo;

1464
  SCtgTbIndexCtx* ctx = pTask->taskCtx;
D
dapan1121 已提交
1465
  CTG_ERR_JRET(ctgUpdateTbIndexEnqueue(pTask->pJob->pCtg, (STableIndex**)&pTask->msgCtx.out, false));
1466

D
dapan1121 已提交
1467
_return:
D
dapan1121 已提交
1468

X
Xiaoyu Wang 已提交
1469 1470 1471
  if (TSDB_CODE_MND_DB_INDEX_NOT_EXIST == code) {
    code = TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
1472 1473 1474 1475 1476
  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1477 1478
int32_t ctgHandleGetTbCfgRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1479
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1480 1481 1482
  CTG_ERR_JRET(ctgProcessRspMsg(&pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

  TSWAP(pTask->res, pTask->msgCtx.out);
1483

D
dapan1121 已提交
1484 1485 1486 1487 1488 1489
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}
D
dapan1121 已提交
1490

dengyihao's avatar
dengyihao 已提交
1491 1492
int32_t ctgHandleGetDbCfgRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1493
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1494 1495 1496
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

  TSWAP(pTask->res, pTask->msgCtx.out);
1497

D
dapan1121 已提交
1498 1499 1500 1501 1502 1503 1504
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1505
int32_t ctgHandleGetDbInfoRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
D
dapan1121 已提交
1506 1507 1508
  CTG_RET(TSDB_CODE_APP_ERROR);
}

dengyihao's avatar
dengyihao 已提交
1509 1510
int32_t ctgHandleGetQnodeRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1511
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1512 1513 1514
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

  TSWAP(pTask->res, pTask->msgCtx.out);
1515

D
dapan1121 已提交
1516 1517 1518 1519 1520 1521 1522
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1523 1524
int32_t ctgHandleGetDnodeRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1525
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1526 1527 1528
  CTG_ERR_JRET(ctgProcessRspMsg(&pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

  TSWAP(pTask->res, pTask->msgCtx.out);
1529

D
dapan1121 已提交
1530 1531 1532 1533 1534 1535 1536
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1537 1538
int32_t ctgHandleGetIndexRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1539
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1540 1541 1542
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

  TSWAP(pTask->res, pTask->msgCtx.out);
1543

D
dapan1121 已提交
1544 1545 1546 1547 1548 1549 1550
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1551 1552
int32_t ctgHandleGetUdfRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1553
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1554 1555 1556
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

  TSWAP(pTask->res, pTask->msgCtx.out);
1557

D
dapan1121 已提交
1558 1559 1560 1561 1562 1563 1564
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1565 1566 1567 1568 1569 1570
int32_t ctgHandleGetUserRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t          code = 0;
  SCtgTask*        pTask = tReq->pTask;
  SCtgUserCtx*     ctx = (SCtgUserCtx*)pTask->taskCtx;
  SCatalog*        pCtg = pTask->pJob->pCtg;
  bool             pass = false;
D
dapan1121 已提交
1571
  SGetUserAuthRsp* pOut = (SGetUserAuthRsp*)pTask->msgCtx.out;
D
dapan1121 已提交
1572 1573 1574

  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

D
dapan1121 已提交
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
  if (pOut->superAuth) {
    pass = true;
    goto _return;
  }

  if (pOut->createdDbs && taosHashGet(pOut->createdDbs, ctx->user.dbFName, strlen(ctx->user.dbFName))) {
    pass = true;
    goto _return;
  }

D
dapan1121 已提交
1585
  if (CTG_AUTH_READ(ctx->user.type) && pOut->readDbs &&
dengyihao's avatar
dengyihao 已提交
1586
      taosHashGet(pOut->readDbs, ctx->user.dbFName, strlen(ctx->user.dbFName))) {
D
dapan1121 已提交
1587
    pass = true;
D
dapan1121 已提交
1588
  } else if (CTG_AUTH_WRITE(ctx->user.type) && pOut->writeDbs &&
dengyihao's avatar
dengyihao 已提交
1589
             taosHashGet(pOut->writeDbs, ctx->user.dbFName, strlen(ctx->user.dbFName))) {
D
dapan1121 已提交
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
    pass = true;
  }

_return:

  if (TSDB_CODE_SUCCESS == code) {
    pTask->res = taosMemoryCalloc(1, sizeof(bool));
    if (NULL == pTask->res) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    } else {
      *(bool*)pTask->res = pass;
    }
  }

D
dapan1121 已提交
1604
  ctgUpdateUserEnqueue(pCtg, pOut, false);
D
dapan1121 已提交
1605
  taosMemoryFreeClear(pTask->msgCtx.out);
D
dapan1121 已提交
1606 1607 1608 1609 1610 1611

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1612 1613
int32_t ctgHandleGetSvrVerRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1614
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1615 1616 1617 1618

  CTG_ERR_JRET(ctgProcessRspMsg(&pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

  TSWAP(pTask->res, pTask->msgCtx.out);
1619

D
dapan1121 已提交
1620 1621 1622 1623 1624 1625 1626
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1627 1628 1629
int32_t ctgAsyncRefreshTbMeta(SCtgTaskReq* tReq, int32_t flag, SName* pName, int32_t* vgId) {
  SCtgTask*         pTask = tReq->pTask;
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1630
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1631
  int32_t           code = 0;
D
dapan1121 已提交
1632

D
dapan1121 已提交
1633 1634
  if (CTG_FLAG_IS_SYS_DB(flag)) {
    ctgDebug("will refresh sys db tbmeta, tbName:%s", tNameGetTableName(pName));
D
dapan1121 已提交
1635

dengyihao's avatar
dengyihao 已提交
1636
    CTG_RET(ctgGetTbMetaFromMnodeImpl(pCtg, pConn, (char*)pName->dbname, (char*)pName->tname, NULL, tReq));
D
dapan1121 已提交
1637 1638
  }

D
dapan1121 已提交
1639 1640
  if (CTG_FLAG_IS_STB(flag)) {
    ctgDebug("will refresh tbmeta, supposed to be stb, tbName:%s", tNameGetTableName(pName));
D
dapan1121 已提交
1641 1642

    // if get from mnode failed, will not try vnode
D
dapan1121 已提交
1643
    CTG_RET(ctgGetTbMetaFromMnode(pCtg, pConn, pName, NULL, tReq));
D
dapan1121 已提交
1644 1645
  }

dengyihao's avatar
dengyihao 已提交
1646 1647
  SCtgDBCache* dbCache = NULL;
  char         dbFName[TSDB_DB_FNAME_LEN] = {0};
D
dapan1121 已提交
1648
  tNameGetFullDbName(pName, dbFName);
1649

D
dapan1121 已提交
1650
  CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache));
D
dapan1121 已提交
1651
  if (dbCache) {
D
dapan1121 已提交
1652
    SVgroupInfo vgInfo = {0};
D
dapan1121 已提交
1653
    CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, dbCache->vgCache.vgInfo, pName, &vgInfo));
D
dapan1121 已提交
1654

D
dapan1121 已提交
1655
    ctgDebug("will refresh tbmeta, not supposed to be stb, tbName:%s, flag:%d", tNameGetTableName(pName), flag);
D
dapan1121 已提交
1656

D
dapan1121 已提交
1657
    *vgId = vgInfo.vgId;
D
dapan1121 已提交
1658
    CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, pName, &vgInfo, NULL, tReq));
D
dapan1121 已提交
1659 1660 1661 1662 1663 1664
  } else {
    SBuildUseDBInput input = {0};

    tstrncpy(input.db, dbFName, tListLen(input.db));
    input.vgVersion = CTG_DEFAULT_INVALID_VERSION;

D
dapan1121 已提交
1665
    CTG_ERR_JRET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, tReq));
D
dapan1121 已提交
1666 1667 1668 1669 1670
  }

_return:

  if (dbCache) {
D
dapan1121 已提交
1671
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1672 1673 1674 1675 1676
  }

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1677 1678
int32_t ctgLaunchGetTbMetaTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1679
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1680 1681
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
1682 1683 1684
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
1685

D
dapan1121 已提交
1686
  CTG_ERR_RET(ctgGetTbMetaFromCache(pCtg, (SCtgTbMetaCtx*)pTask->taskCtx, (STableMeta**)&pTask->res));
D
dapan1121 已提交
1687 1688 1689 1690 1691
  if (pTask->res) {
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
1692
  SCtgTbMetaCtx* pCtx = (SCtgTbMetaCtx*)pTask->taskCtx;
dengyihao's avatar
dengyihao 已提交
1693
  SCtgTaskReq    tReq;
D
dapan1121 已提交
1694
  tReq.pTask = pTask;
1695
  tReq.msgIdx = -1;
D
dapan1121 已提交
1696
  CTG_ERR_RET(ctgAsyncRefreshTbMeta(&tReq, pCtx->flag, pCtx->pName, &pCtx->vgId));
D
dapan1121 已提交
1697 1698 1699 1700

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1701 1702
int32_t ctgLaunchGetTbMetasTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1703
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1704 1705
  SCtgTbMetasCtx*   pCtx = (SCtgTbMetasCtx*)pTask->taskCtx;
  SCtgJob*          pJob = pTask->pJob;
D
dapan1121 已提交
1706

1707 1708 1709 1710 1711
  int32_t dbNum = taosArrayGetSize(pCtx->pNames);
  int32_t fetchIdx = 0;
  int32_t baseResIdx = 0;
  for (int32_t i = 0; i < dbNum; ++i) {
    STablesReq* pReq = taosArrayGet(pCtx->pNames, i);
1712
    ctgDebug("start to check tb metas in db %s, tbNum %ld", pReq->dbFName, taosArrayGetSize(pReq->pTables));
1713 1714 1715
    CTG_ERR_RET(ctgGetTbMetasFromCache(pCtg, pConn, pCtx, i, &fetchIdx, baseResIdx, pReq->pTables));
    baseResIdx += taosArrayGetSize(pReq->pTables);
  }
1716

1717 1718 1719
  pCtx->fetchNum = taosArrayGetSize(pCtx->pFetchs);
  if (pCtx->fetchNum <= 0) {
    TSWAP(pTask->res, pCtx->pResList);
1720

D
dapan1121 已提交
1721 1722 1723
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }
1724

D
dapan1121 已提交
1725
  pTask->msgCtxs = taosArrayInit(pCtx->fetchNum, sizeof(SCtgMsgCtx));
1726
  taosArraySetSize(pTask->msgCtxs, pCtx->fetchNum);
1727

D
dapan1121 已提交
1728
  for (int32_t i = 0; i < pCtx->fetchNum; ++i) {
dengyihao's avatar
dengyihao 已提交
1729 1730 1731
    SCtgFetch*  pFetch = taosArrayGet(pCtx->pFetchs, i);
    SName*      pName = ctgGetFetchName(pCtx->pNames, pFetch);
    SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, i);
D
dapan1121 已提交
1732 1733 1734
    if (NULL == pMsgCtx->pBatchs) {
      pMsgCtx->pBatchs = pJob->pBatchs;
    }
1735

D
dapan1121 已提交
1736 1737
    SCtgTaskReq tReq;
    tReq.pTask = pTask;
1738
    tReq.msgIdx = pFetch->fetchIdx;
D
dapan1121 已提交
1739
    CTG_ERR_RET(ctgAsyncRefreshTbMeta(&tReq, pFetch->flag, pName, &pFetch->vgId));
D
dapan1121 已提交
1740
  }
1741

D
dapan1121 已提交
1742 1743 1744
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1745 1746 1747
int32_t ctgLaunchGetDbVgTask(SCtgTask* pTask) {
  int32_t           code = 0;
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1748
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1749 1750 1751 1752
  SCtgDBCache*      dbCache = NULL;
  SCtgDbVgCtx*      pCtx = (SCtgDbVgCtx*)pTask->taskCtx;
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
1753 1754 1755
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
1756

D
dapan1121 已提交
1757 1758
  CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, pCtx->dbFName, &dbCache));
  if (NULL != dbCache) {
D
dapan1121 已提交
1759
    CTG_ERR_JRET(ctgGenerateVgList(pCtg, dbCache->vgCache.vgInfo->vgHash, (SArray**)&pTask->res));
D
dapan1121 已提交
1760 1761 1762

    ctgReleaseVgInfoToCache(pCtg, dbCache);
    dbCache = NULL;
1763

D
dapan1121 已提交
1764 1765 1766
    CTG_ERR_JRET(ctgHandleTaskEnd(pTask, 0));
  } else {
    SBuildUseDBInput input = {0};
1767

D
dapan1121 已提交
1768 1769
    tstrncpy(input.db, pCtx->dbFName, tListLen(input.db));
    input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
D
dapan1121 已提交
1770 1771 1772 1773 1774

    SCtgTaskReq tReq;
    tReq.pTask = pTask;
    tReq.msgIdx = -1;
    CTG_ERR_RET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, &tReq));
D
dapan1121 已提交
1775 1776 1777 1778 1779
  }

_return:

  if (dbCache) {
D
dapan1121 已提交
1780
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1781 1782 1783 1784 1785
  }

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1786 1787 1788
int32_t ctgLaunchGetTbHashTask(SCtgTask* pTask) {
  int32_t           code = 0;
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1789
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1790 1791 1792 1793
  SCtgDBCache*      dbCache = NULL;
  SCtgTbHashCtx*    pCtx = (SCtgTbHashCtx*)pTask->taskCtx;
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
1794 1795 1796
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
1797

D
dapan1121 已提交
1798 1799 1800 1801 1802 1803
  CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, pCtx->dbFName, &dbCache));
  if (NULL != dbCache) {
    pTask->res = taosMemoryMalloc(sizeof(SVgroupInfo));
    if (NULL == pTask->res) {
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
    }
D
dapan1121 已提交
1804
    CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, dbCache->vgCache.vgInfo, pCtx->pName, (SVgroupInfo*)pTask->res));
D
dapan1121 已提交
1805 1806 1807

    ctgReleaseVgInfoToCache(pCtg, dbCache);
    dbCache = NULL;
1808

D
dapan1121 已提交
1809 1810 1811
    CTG_ERR_JRET(ctgHandleTaskEnd(pTask, 0));
  } else {
    SBuildUseDBInput input = {0};
1812

D
dapan1121 已提交
1813 1814
    tstrncpy(input.db, pCtx->dbFName, tListLen(input.db));
    input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
D
dapan1121 已提交
1815 1816 1817

    SCtgTaskReq tReq;
    tReq.pTask = pTask;
1818
    tReq.msgIdx = -1;
D
dapan1121 已提交
1819
    CTG_ERR_RET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, &tReq));
D
dapan1121 已提交
1820 1821 1822 1823 1824
  }

_return:

  if (dbCache) {
D
dapan1121 已提交
1825
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1826 1827 1828 1829 1830
  }

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1831 1832
int32_t ctgLaunchGetTbHashsTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
1833
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1834 1835 1836 1837 1838 1839 1840
  SCtgTbHashsCtx*   pCtx = (SCtgTbHashsCtx*)pTask->taskCtx;
  SCtgDBCache*      dbCache = NULL;
  SCtgJob*          pJob = pTask->pJob;
  int32_t           dbNum = taosArrayGetSize(pCtx->pNames);
  int32_t           fetchIdx = 0;
  int32_t           baseResIdx = 0;
  int32_t           code = 0;
1841

1842 1843
  for (int32_t i = 0; i < dbNum; ++i) {
    STablesReq* pReq = taosArrayGet(pCtx->pNames, i);
1844

1845
    CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, pReq->dbFName, &dbCache));
1846 1847

    if (NULL != dbCache) {
D
dapan1121 已提交
1848 1849
      SCtgTaskReq tReq;
      tReq.pTask = pTask;
1850
      tReq.msgIdx = -1;
dengyihao's avatar
dengyihao 已提交
1851 1852
      CTG_ERR_JRET(
          ctgGetVgInfosFromHashValue(pCtg, &tReq, dbCache->vgCache.vgInfo, pCtx, pReq->dbFName, pReq->pTables, false));
1853 1854 1855

      ctgReleaseVgInfoToCache(pCtg, dbCache);
      dbCache = NULL;
1856 1857

      baseResIdx += taosArrayGetSize(pReq->pTables);
1858
    } else {
1859
      ctgAddFetch(&pCtx->pFetchs, i, -1, &fetchIdx, baseResIdx, 0);
1860

1861
      baseResIdx += taosArrayGetSize(pReq->pTables);
1862
      taosArraySetSize(pCtx->pResList, baseResIdx);
1863 1864 1865 1866 1867 1868
    }
  }

  pCtx->fetchNum = taosArrayGetSize(pCtx->pFetchs);
  if (pCtx->fetchNum <= 0) {
    TSWAP(pTask->res, pCtx->pResList);
1869

1870 1871 1872
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }
1873

1874 1875
  pTask->msgCtxs = taosArrayInit(pCtx->fetchNum, sizeof(SCtgMsgCtx));
  taosArraySetSize(pTask->msgCtxs, pCtx->fetchNum);
1876

1877
  for (int32_t i = 0; i < pCtx->fetchNum; ++i) {
dengyihao's avatar
dengyihao 已提交
1878
    SCtgFetch*  pFetch = taosArrayGet(pCtx->pFetchs, i);
D
dapan1121 已提交
1879
    STablesReq* pReq = taosArrayGet(pCtx->pNames, pFetch->dbIdx);
dengyihao's avatar
dengyihao 已提交
1880
    SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, i);
D
dapan1121 已提交
1881 1882 1883
    if (NULL == pMsgCtx->pBatchs) {
      pMsgCtx->pBatchs = pJob->pBatchs;
    }
1884

1885
    SBuildUseDBInput input = {0};
D
dapan1121 已提交
1886
    strcpy(input.db, pReq->dbFName);
1887

1888 1889
    input.vgVersion = CTG_DEFAULT_INVALID_VERSION;

D
dapan1121 已提交
1890 1891 1892 1893
    SCtgTaskReq tReq;
    tReq.pTask = pTask;
    tReq.msgIdx = pFetch->fetchIdx;
    CTG_ERR_RET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, &tReq));
1894 1895 1896 1897 1898 1899 1900
  }

_return:

  if (dbCache) {
    ctgReleaseVgInfoToCache(pCtg, dbCache);
  }
1901

1902 1903 1904
  return code;
}

dengyihao's avatar
dengyihao 已提交
1905 1906 1907
int32_t ctgLaunchGetTbIndexTask(SCtgTask* pTask) {
  int32_t           code = 0;
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1908
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1909 1910 1911 1912
  SCtgTbIndexCtx*   pCtx = (SCtgTbIndexCtx*)pTask->taskCtx;
  SArray*           pRes = NULL;
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
1913 1914 1915
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
1916 1917 1918 1919

  CTG_ERR_RET(ctgReadTbIndexFromCache(pCtg, pCtx->pName, &pRes));
  if (pRes) {
    pTask->res = pRes;
1920

D
dapan1121 已提交
1921 1922 1923
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }
1924

D
dapan1121 已提交
1925
  CTG_ERR_RET(ctgGetTbIndexFromMnode(pCtg, pConn, pCtx->pName, NULL, pTask));
D
dapan1121 已提交
1926 1927 1928
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1929 1930 1931
int32_t ctgLaunchGetTbCfgTask(SCtgTask* pTask) {
  int32_t           code = 0;
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1932
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1933 1934 1935
  SCtgTbCfgCtx*     pCtx = (SCtgTbCfgCtx*)pTask->taskCtx;
  SArray*           pRes = NULL;
  char              dbFName[TSDB_DB_FNAME_LEN];
D
dapan1121 已提交
1936
  tNameGetFullDbName(pCtx->pName, dbFName);
dengyihao's avatar
dengyihao 已提交
1937 1938
  SCtgJob*    pJob = pTask->pJob;
  SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
1939 1940 1941
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960

  if (pCtx->tbType <= 0) {
    CTG_ERR_JRET(ctgReadTbTypeFromCache(pCtg, dbFName, pCtx->pName->tname, &pCtx->tbType));
    if (pCtx->tbType <= 0) {
      CTG_ERR_JRET(ctgLaunchSubTask(pTask, CTG_TASK_GET_TB_META, ctgGetTbCfgCb, pCtx->pName));
      return TSDB_CODE_SUCCESS;
    }
  }

  if (TSDB_SUPER_TABLE == pCtx->tbType) {
    CTG_ERR_JRET(ctgGetTableCfgFromMnode(pCtg, pConn, pCtx->pName, NULL, pTask));
  } else {
    if (NULL == pCtx->pVgInfo) {
      CTG_ERR_JRET(ctgGetTbHashVgroupFromCache(pCtg, pCtx->pName, &pCtx->pVgInfo));
      if (NULL == pCtx->pVgInfo) {
        CTG_ERR_JRET(ctgLaunchSubTask(pTask, CTG_TASK_GET_DB_VGROUP, ctgGetTbCfgCb, dbFName));
        return TSDB_CODE_SUCCESS;
      }
    }
1961

D
dapan1121 已提交
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
    CTG_ERR_JRET(ctgGetTableCfgFromVnode(pCtg, pConn, pCtx->pName, pCtx->pVgInfo, NULL, pTask));
  }

  return TSDB_CODE_SUCCESS;

_return:

  if (CTG_TASK_LAUNCHED == pTask->status) {
    ctgHandleTaskEnd(pTask, code);
  }
1972

D
dapan1121 已提交
1973 1974 1975
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1976 1977
int32_t ctgLaunchGetQnodeTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1978
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1979 1980
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
1981 1982 1983
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
1984

D
dapan1121 已提交
1985
  CTG_ERR_RET(ctgGetQnodeListFromMnode(pCtg, pConn, NULL, pTask));
D
dapan1121 已提交
1986 1987 1988
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1989 1990
int32_t ctgLaunchGetDnodeTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1991
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1992 1993
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
1994 1995 1996
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
1997 1998 1999 2000 2001

  CTG_ERR_RET(ctgGetDnodeListFromMnode(pCtg, pConn, NULL, pTask));
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2002 2003
int32_t ctgLaunchGetDbCfgTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2004
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2005 2006 2007
  SCtgDbCfgCtx*     pCtx = (SCtgDbCfgCtx*)pTask->taskCtx;
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2008 2009 2010
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2011

D
dapan1121 已提交
2012
  CTG_ERR_RET(ctgGetDBCfgFromMnode(pCtg, pConn, pCtx->dbFName, NULL, pTask));
D
dapan1121 已提交
2013 2014 2015 2016

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2017 2018 2019 2020
int32_t ctgLaunchGetDbInfoTask(SCtgTask* pTask) {
  int32_t        code = 0;
  SCatalog*      pCtg = pTask->pJob->pCtg;
  SCtgDBCache*   dbCache = NULL;
D
dapan1121 已提交
2021
  SCtgDbInfoCtx* pCtx = (SCtgDbInfoCtx*)pTask->taskCtx;
dengyihao's avatar
dengyihao 已提交
2022 2023
  SCtgJob*       pJob = pTask->pJob;
  SCtgMsgCtx*    pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2024 2025 2026
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2027 2028 2029 2030 2031 2032 2033 2034 2035

  pTask->res = taosMemoryCalloc(1, sizeof(SDbInfo));
  if (NULL == pTask->res) {
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

  SDbInfo* pInfo = (SDbInfo*)pTask->res;
  CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, pCtx->dbFName, &dbCache));
  if (NULL != dbCache) {
D
dapan1121 已提交
2036
    pInfo->vgVer = dbCache->vgCache.vgInfo->vgVersion;
D
dapan1121 已提交
2037
    pInfo->dbId = dbCache->dbId;
D
dapan1121 已提交
2038
    pInfo->tbNum = dbCache->vgCache.vgInfo->numOfTable;
D
dapan1121 已提交
2039
    pInfo->stateTs = dbCache->vgCache.vgInfo->stateTs;
D
dapan1121 已提交
2040 2041 2042

    ctgReleaseVgInfoToCache(pCtg, dbCache);
    dbCache = NULL;
D
dapan1121 已提交
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
  } else {
    pInfo->vgVer = CTG_DEFAULT_INVALID_VERSION;
  }

  CTG_ERR_JRET(ctgHandleTaskEnd(pTask, 0));

_return:

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
2054 2055
int32_t ctgLaunchGetIndexTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2056
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2057 2058 2059
  SCtgIndexCtx*     pCtx = (SCtgIndexCtx*)pTask->taskCtx;
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2060 2061 2062
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2063

D
dapan1121 已提交
2064
  CTG_ERR_RET(ctgGetIndexInfoFromMnode(pCtg, pConn, pCtx->indexFName, NULL, pTask));
D
dapan1121 已提交
2065 2066 2067 2068

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2069 2070
int32_t ctgLaunchGetUdfTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2071
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2072 2073 2074
  SCtgUdfCtx*       pCtx = (SCtgUdfCtx*)pTask->taskCtx;
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2075 2076 2077
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2078

D
dapan1121 已提交
2079
  CTG_ERR_RET(ctgGetUdfInfoFromMnode(pCtg, pConn, pCtx->udfName, NULL, pTask));
D
dapan1121 已提交
2080 2081 2082 2083

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2084 2085
int32_t ctgLaunchGetUserTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2086
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2087 2088 2089 2090 2091
  SCtgUserCtx*      pCtx = (SCtgUserCtx*)pTask->taskCtx;
  bool              inCache = false;
  bool              pass = false;
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2092 2093 2094
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
2095

D
dapan1121 已提交
2096 2097 2098 2099 2100 2101 2102
  CTG_ERR_RET(ctgChkAuthFromCache(pCtg, pCtx->user.user, pCtx->user.dbFName, pCtx->user.type, &inCache, &pass));
  if (inCache) {
    pTask->res = taosMemoryCalloc(1, sizeof(bool));
    if (NULL == pTask->res) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
    *(bool*)pTask->res = pass;
2103

D
dapan1121 已提交
2104 2105 2106 2107
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
2108
  CTG_ERR_RET(ctgGetUserDbAuthFromMnode(pCtg, pConn, pCtx->user.user, NULL, pTask));
D
dapan1121 已提交
2109 2110 2111 2112

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2113 2114
int32_t ctgLaunchGetSvrVerTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2115
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2116 2117
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2118 2119 2120
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2121 2122 2123 2124 2125 2126

  CTG_ERR_RET(ctgGetSvrVerFromMnode(pCtg, pConn, NULL, pTask));

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2127
int32_t ctgRelaunchGetTbMetaTask(SCtgTask* pTask) {
D
dapan1121 已提交
2128 2129 2130 2131 2132 2133 2134
  ctgResetTbMetaTask(pTask);

  CTG_ERR_RET(ctgLaunchGetTbMetaTask(pTask));

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2135
int32_t ctgGetTbCfgCb(SCtgTask* pTask) {
D
dapan1121 已提交
2136
  int32_t code = 0;
2137

D
dapan1121 已提交
2138 2139 2140 2141 2142 2143 2144
  CTG_ERR_JRET(pTask->subRes.code);

  SCtgTbCfgCtx* pCtx = (SCtgTbCfgCtx*)pTask->taskCtx;
  if (CTG_TASK_GET_TB_META == pTask->subRes.type) {
    pCtx->tbType = ((STableMeta*)pTask->subRes.res)->tableType;
  } else if (CTG_TASK_GET_DB_VGROUP == pTask->subRes.type) {
    SDBVgInfo* pDb = (SDBVgInfo*)pTask->subRes.res;
2145

D
dapan1121 已提交
2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184
    pCtx->pVgInfo = taosMemoryCalloc(1, sizeof(SVgroupInfo));
    CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pTask->pJob->pCtg, pDb, pCtx->pName, pCtx->pVgInfo));
  }

  CTG_RET(ctgLaunchGetTbCfgTask(pTask));

_return:

  CTG_RET(ctgHandleTaskEnd(pTask, pTask->subRes.code));
}

int32_t ctgCompDbVgTasks(SCtgTask* pTask, void* param, bool* equal) {
  SCtgDbVgCtx* ctx = pTask->taskCtx;

  *equal = (0 == strcmp(ctx->dbFName, param));

  return TSDB_CODE_SUCCESS;
}

int32_t ctgCompTbMetaTasks(SCtgTask* pTask, void* param, bool* equal) {
  SCtgTbMetaCtx* ctx = pTask->taskCtx;

  *equal = tNameTbNameEqual(ctx->pName, (SName*)param);

  return TSDB_CODE_SUCCESS;
}

int32_t ctgCloneTbMeta(SCtgTask* pTask, void** pRes) {
  STableMeta* pMeta = (STableMeta*)pTask->res;

  CTG_RET(cloneTableMeta(pMeta, (STableMeta**)pRes));
}

int32_t ctgCloneDbVg(SCtgTask* pTask, void** pRes) {
  SUseDbOutput* pOut = (SUseDbOutput*)pTask->msgCtx.out;

  CTG_RET(cloneDbVgInfo(pOut->dbVgroup, (SDBVgInfo**)pRes));
}

D
dapan1121 已提交
2185
SCtgAsyncFps gCtgAsyncFps[] = {
dengyihao's avatar
dengyihao 已提交
2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201
    {ctgInitGetQnodeTask, ctgLaunchGetQnodeTask, ctgHandleGetQnodeRsp, ctgDumpQnodeRes, NULL, NULL},
    {ctgInitGetDnodeTask, ctgLaunchGetDnodeTask, ctgHandleGetDnodeRsp, ctgDumpDnodeRes, NULL, NULL},
    {ctgInitGetDbVgTask, ctgLaunchGetDbVgTask, ctgHandleGetDbVgRsp, ctgDumpDbVgRes, ctgCompDbVgTasks, ctgCloneDbVg},
    {ctgInitGetDbCfgTask, ctgLaunchGetDbCfgTask, ctgHandleGetDbCfgRsp, ctgDumpDbCfgRes, NULL, NULL},
    {ctgInitGetDbInfoTask, ctgLaunchGetDbInfoTask, ctgHandleGetDbInfoRsp, ctgDumpDbInfoRes, NULL, NULL},
    {ctgInitGetTbMetaTask, ctgLaunchGetTbMetaTask, ctgHandleGetTbMetaRsp, ctgDumpTbMetaRes, ctgCompTbMetaTasks,
     ctgCloneTbMeta},
    {ctgInitGetTbHashTask, ctgLaunchGetTbHashTask, ctgHandleGetTbHashRsp, ctgDumpTbHashRes, NULL, NULL},
    {ctgInitGetTbIndexTask, ctgLaunchGetTbIndexTask, ctgHandleGetTbIndexRsp, ctgDumpTbIndexRes, NULL, NULL},
    {ctgInitGetTbCfgTask, ctgLaunchGetTbCfgTask, ctgHandleGetTbCfgRsp, ctgDumpTbCfgRes, NULL, NULL},
    {ctgInitGetIndexTask, ctgLaunchGetIndexTask, ctgHandleGetIndexRsp, ctgDumpIndexRes, NULL, NULL},
    {ctgInitGetUdfTask, ctgLaunchGetUdfTask, ctgHandleGetUdfRsp, ctgDumpUdfRes, NULL, NULL},
    {ctgInitGetUserTask, ctgLaunchGetUserTask, ctgHandleGetUserRsp, ctgDumpUserRes, NULL, NULL},
    {ctgInitGetSvrVerTask, ctgLaunchGetSvrVerTask, ctgHandleGetSvrVerRsp, ctgDumpSvrVer, NULL, NULL},
    {ctgInitGetTbMetasTask, ctgLaunchGetTbMetasTask, ctgHandleGetTbMetasRsp, ctgDumpTbMetasRes, NULL, NULL},
    {ctgInitGetTbHashsTask, ctgLaunchGetTbHashsTask, ctgHandleGetTbHashsRsp, ctgDumpTbHashsRes, NULL, NULL},
D
dapan1121 已提交
2202 2203
};

dengyihao's avatar
dengyihao 已提交
2204
int32_t ctgMakeAsyncRes(SCtgJob* pJob) {
D
dapan1121 已提交
2205 2206
  int32_t code = 0;
  int32_t taskNum = taosArrayGetSize(pJob->pTasks);
2207

D
dapan1121 已提交
2208
  for (int32_t i = 0; i < taskNum; ++i) {
dengyihao's avatar
dengyihao 已提交
2209
    SCtgTask* pTask = taosArrayGet(pJob->pTasks, i);
D
dapan1121 已提交
2210 2211 2212 2213 2214 2215
    CTG_ERR_RET((*gCtgAsyncFps[pTask->type].dumpResFp)(pTask));
  }

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2216
int32_t ctgSearchExistingTask(SCtgJob* pJob, CTG_TASK_TYPE type, void* param, int32_t* taskId) {
D
dapan1121 已提交
2217 2218 2219
  bool      equal = false;
  SCtgTask* pTask = NULL;
  int32_t   code = 0;
2220

D
dapan1121 已提交
2221
  CTG_LOCK(CTG_READ, &pJob->taskLock);
2222

D
dapan1121 已提交
2223 2224 2225
  int32_t taskNum = taosArrayGetSize(pJob->pTasks);
  for (int32_t i = 0; i < taskNum; ++i) {
    pTask = taosArrayGet(pJob->pTasks, i);
dengyihao's avatar
dengyihao 已提交
2226
    if (type != pTask->type) {
D
dapan1121 已提交
2227 2228
      continue;
    }
2229

D
dapan1121 已提交
2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245
    CTG_ERR_JRET((*gCtgAsyncFps[type].compFp)(pTask, param, &equal));
    if (equal) {
      break;
    }
  }

_return:

  CTG_UNLOCK(CTG_READ, &pJob->taskLock);
  if (equal) {
    *taskId = pTask->taskId;
  }

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
2246
int32_t ctgSetSubTaskCb(SCtgTask* pSub, SCtgTask* pTask) {
D
dapan1121 已提交
2247
  int32_t code = 0;
2248

D
dapan1121 已提交
2249 2250 2251 2252
  CTG_LOCK(CTG_WRITE, &pSub->lock);
  if (CTG_TASK_DONE == pSub->status) {
    pTask->subRes.code = pSub->code;
    CTG_ERR_JRET((*gCtgAsyncFps[pTask->type].cloneFp)(pSub, &pTask->subRes.res));
dengyihao's avatar
dengyihao 已提交
2253 2254
    SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
    SCtgMsgCtx* pSubMsgCtx = CTG_GET_TASK_MSGCTX(pSub, -1);
D
dapan1121 已提交
2255
    pMsgCtx->pBatchs = pSubMsgCtx->pBatchs;
2256

D
dapan1121 已提交
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269
    CTG_ERR_JRET(pTask->subRes.fp(pTask));
  } else {
    if (NULL == pSub->pParents) {
      pSub->pParents = taosArrayInit(4, POINTER_BYTES);
    }

    taosArrayPush(pSub->pParents, &pTask);
  }

_return:

  CTG_UNLOCK(CTG_WRITE, &pSub->lock);

2270
  CTG_RET(code);
D
dapan1121 已提交
2271 2272
}

dengyihao's avatar
dengyihao 已提交
2273
int32_t ctgLaunchSubTask(SCtgTask* pTask, CTG_TASK_TYPE type, ctgSubTaskCbFp fp, void* param) {
D
dapan1121 已提交
2274 2275 2276 2277 2278 2279 2280
  SCtgJob* pJob = pTask->pJob;
  int32_t  subTaskId = -1;
  bool     newTask = false;

  ctgClearSubTaskRes(&pTask->subRes);
  pTask->subRes.type = type;
  pTask->subRes.fp = fp;
2281

D
dapan1121 已提交
2282 2283 2284 2285 2286
  CTG_ERR_RET(ctgSearchExistingTask(pJob, type, param, &subTaskId));
  if (subTaskId < 0) {
    CTG_ERR_RET(ctgInitTask(pJob, type, param, &subTaskId));
    newTask = true;
  }
2287

D
dapan1121 已提交
2288 2289 2290 2291 2292
  SCtgTask* pSub = taosArrayGet(pJob->pTasks, subTaskId);

  CTG_ERR_RET(ctgSetSubTaskCb(pSub, pTask));

  if (newTask) {
dengyihao's avatar
dengyihao 已提交
2293 2294
    SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
    SCtgMsgCtx* pSubMsgCtx = CTG_GET_TASK_MSGCTX(pSub, -1);
D
dapan1121 已提交
2295 2296
    pSubMsgCtx->pBatchs = pMsgCtx->pBatchs;

D
dapan1121 已提交
2297 2298 2299 2300 2301 2302
    CTG_ERR_RET((*gCtgAsyncFps[pSub->type].launchFp)(pSub));
    pSub->status = CTG_TASK_LAUNCHED;
  }

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
2303

dengyihao's avatar
dengyihao 已提交
2304
int32_t ctgLaunchJob(SCtgJob* pJob) {
D
dapan1121 已提交
2305
  int32_t taskNum = taosArrayGetSize(pJob->pTasks);
2306

D
dapan1121 已提交
2307
  for (int32_t i = 0; i < taskNum; ++i) {
dengyihao's avatar
dengyihao 已提交
2308
    SCtgTask* pTask = taosArrayGet(pJob->pTasks, i);
D
dapan1121 已提交
2309

D
dapan1121 已提交
2310
    qDebug("QID:0x%" PRIx64 " ctg launch [%dth] task", pJob->queryId, pTask->taskId);
D
dapan1121 已提交
2311
    CTG_ERR_RET((*gCtgAsyncFps[pTask->type].launchFp)(pTask));
2312

D
dapan1121 已提交
2313
    pTask->status = CTG_TASK_LAUNCHED;
D
dapan1121 已提交
2314 2315
  }

D
dapan1121 已提交
2316 2317
  if (taskNum <= 0) {
    qDebug("QID:0x%" PRIx64 " ctg call user callback with rsp %s", pJob->queryId, tstrerror(pJob->jobResCode));
2318

D
dapan1121 已提交
2319
    taosAsyncExec(ctgCallUserCb, pJob, NULL);
2320
#if CTG_BATCH_FETCH
D
dapan1121 已提交
2321 2322 2323
  } else {
    ctgLaunchBatchs(pJob->pCtg, pJob, pJob->pBatchs);
#endif
D
dapan1121 已提交
2324 2325
  }

D
dapan1121 已提交
2326 2327
  return TSDB_CODE_SUCCESS;
}