ctgAsync.c 72.0 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
  task.type = CTG_TASK_GET_INDEX_INFO;
D
dapan1121 已提交
245 246
  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
  SCtgTask task = {0};

D
dapan1121 已提交
333
  task.type = CTG_TASK_GET_TB_SMA_INDEX;
D
dapan1121 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347
  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;
}

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
int32_t ctgInitGetTbTagTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
  SName*   name = (SName*)param;
  SCtgTask task = {0};

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

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

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

  memcpy(ctx->pName, name, sizeof(*name));

  taosArrayPush(pJob->pTasks, &task);

  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, tbName:%s", pJob->queryId, taskIdx,
         ctgTaskTypeStr(task.type), name->tname);

  return TSDB_CODE_SUCCESS;
}


dengyihao's avatar
dengyihao 已提交
420
int32_t ctgHandleForceUpdate(SCatalog* pCtg, int32_t taskNum, SCtgJob* pJob, const SCatalogReq* pReq) {
D
dapan1121 已提交
421
  SHashObj* pDb = taosHashInit(taskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
D
dapan1121 已提交
422 423 424 425
  SHashObj* pTb = taosHashInit(taskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  if (NULL == pDb || NULL == pTb) {
    taosHashCleanup(pDb);
    taosHashCleanup(pTb);
D
dapan1121 已提交
426 427
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }
428

D
dapan1121 已提交
429 430 431 432
  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 已提交
433

D
dapan1121 已提交
434 435 436 437
  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 已提交
438

D
dapan1121 已提交
439 440 441 442
  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 已提交
443

D
dapan1121 已提交
444 445 446 447 448 449 450 451 452
  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 已提交
453
  }
D
dapan1121 已提交
454 455 456 457 458 459 460 461 462 463

  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 已提交
464
  }
D
dapan1121 已提交
465

D
dapan1121 已提交
466 467
  for (int32_t i = 0; i < pJob->tbCfgNum; ++i) {
    SName* name = taosArrayGet(pReq->pTableCfg, i);
dengyihao's avatar
dengyihao 已提交
468
    char   dbFName[TSDB_DB_FNAME_LEN];
D
dapan1121 已提交
469 470
    tNameGetFullDbName(name, dbFName);
    taosHashPut(pDb, dbFName, strlen(dbFName), dbFName, TSDB_DB_FNAME_LEN);
471 472 473 474 475 476 477 478 479
    taosHashPut(pTb, name, sizeof(SName), name, sizeof(SName));
  }

  for (int32_t i = 0; i < pJob->tbTagNum; ++i) {
    SName* name = taosArrayGet(pReq->pTableTag, i);
    char   dbFName[TSDB_DB_FNAME_LEN];
    tNameGetFullDbName(name, dbFName);
    taosHashPut(pDb, dbFName, strlen(dbFName), dbFName, TSDB_DB_FNAME_LEN);
    taosHashPut(pTb, name, sizeof(SName), name, sizeof(SName));
D
dapan1121 已提交
480 481
  }

D
dapan1121 已提交
482 483 484 485
  char* dbFName = taosHashIterate(pDb, NULL);
  while (dbFName) {
    ctgDropDbVgroupEnqueue(pCtg, dbFName, true);
    dbFName = taosHashIterate(pDb, dbFName);
D
dapan1121 已提交
486 487
  }

D
dapan1121 已提交
488 489
  taosHashCleanup(pDb);

D
dapan1121 已提交
490
  // REFRESH TABLE META
D
dapan1121 已提交
491

D
dapan1121 已提交
492 493 494 495
  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 已提交
496

D
dapan1121 已提交
497 498
  SName* name = taosHashIterate(pTb, NULL);
  while (name) {
D
dapan1121 已提交
499
    ctgRemoveTbMeta(pCtg, name);
D
dapan1121 已提交
500
    name = taosHashIterate(pTb, name);
D
dapan1121 已提交
501 502
  }

D
dapan1121 已提交
503 504
  taosHashCleanup(pTb);

D
dapan1121 已提交
505 506 507 508 509
  for (int32_t i = 0; i < pJob->tbIndexNum; ++i) {
    SName* name = taosArrayGet(pReq->pTableIndex, i);
    ctgDropTbIndexEnqueue(pCtg, name, true);
  }

D
dapan1121 已提交
510 511
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
512

dengyihao's avatar
dengyihao 已提交
513
int32_t ctgInitTask(SCtgJob* pJob, CTG_TASK_TYPE type, void* param, int32_t* taskId) {
K
kailixu 已提交
514
  int32_t code = 0;
D
dapan1121 已提交
515 516 517
  int32_t tid = atomic_fetch_add_32(&pJob->taskIdx, 1);

  CTG_LOCK(CTG_WRITE, &pJob->taskLock);
K
kailixu 已提交
518
  CTG_ERR_JRET((*gCtgAsyncFps[type].initFp)(pJob, tid, param));
D
dapan1121 已提交
519 520 521 522

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

K
kailixu 已提交
524 525
_return:
  CTG_UNLOCK(CTG_WRITE, &pJob->taskLock);
dengyihao's avatar
dengyihao 已提交
526

K
kailixu 已提交
527
  return code;
D
dapan1121 已提交
528 529
}

dengyihao's avatar
dengyihao 已提交
530 531
int32_t ctgInitJob(SCatalog* pCtg, SRequestConnInfo* pConn, SCtgJob** job, const SCatalogReq* pReq, catalogCallback fp,
                   void* param) {
D
dapan1121 已提交
532
  int32_t code = 0;
533 534
  int64_t st = taosGetTimestampUs();

535
  int32_t tbMetaNum = (int32_t)ctgGetTablesReqNum(pReq->pTableMeta);
D
dapan1121 已提交
536
  int32_t dbVgNum = (int32_t)taosArrayGetSize(pReq->pDbVgroup);
537
  int32_t tbHashNum = (int32_t)ctgGetTablesReqNum(pReq->pTableHash);
D
dapan1121 已提交
538 539
  int32_t udfNum = (int32_t)taosArrayGetSize(pReq->pUdf);
  int32_t qnodeNum = pReq->qNodeRequired ? 1 : 0;
D
dapan1121 已提交
540
  int32_t dnodeNum = pReq->dNodeRequired ? 1 : 0;
D
dapan1121 已提交
541
  int32_t svrVerNum = pReq->svrVerRequired ? 1 : 0;
D
dapan1121 已提交
542 543 544
  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 已提交
545
  int32_t dbInfoNum = (int32_t)taosArrayGetSize(pReq->pDbInfo);
D
dapan1121 已提交
546
  int32_t tbIndexNum = (int32_t)taosArrayGetSize(pReq->pTableIndex);
D
dapan1121 已提交
547
  int32_t tbCfgNum = (int32_t)taosArrayGetSize(pReq->pTableCfg);
548
  int32_t tbTagNum = (int32_t)ctgGetTablesReqNum(pReq->pTableTag);
D
dapan1121 已提交
549

dengyihao's avatar
dengyihao 已提交
550
  int32_t taskNum = tbMetaNum + dbVgNum + udfNum + tbHashNum + qnodeNum + dnodeNum + svrVerNum + dbCfgNum + indexNum +
551
                    userNum + dbInfoNum + tbIndexNum + tbCfgNum + tbTagNum;
552

D
dapan1121 已提交
553 554
  *job = taosMemoryCalloc(1, sizeof(SCtgJob));
  if (NULL == *job) {
D
dapan1121 已提交
555
    ctgError("failed to calloc, size:%d, reqId:0x%" PRIx64, (int32_t)sizeof(SCtgJob), pConn->requestId);
D
dapan1121 已提交
556 557 558
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

dengyihao's avatar
dengyihao 已提交
559
  SCtgJob* pJob = *job;
D
dapan1121 已提交
560 561

  pJob->subTaskNum = taskNum;
D
dapan1121 已提交
562
  pJob->queryId = pConn->requestId;
D
dapan1121 已提交
563
  pJob->userFp = fp;
dengyihao's avatar
dengyihao 已提交
564 565
  pJob->pCtg = pCtg;
  pJob->conn = *pConn;
D
dapan1121 已提交
566
  pJob->userParam = param;
567

D
dapan1121 已提交
568 569 570
  pJob->tbMetaNum = tbMetaNum;
  pJob->tbHashNum = tbHashNum;
  pJob->qnodeNum = qnodeNum;
D
dapan1121 已提交
571
  pJob->dnodeNum = dnodeNum;
D
dapan1121 已提交
572 573 574 575 576
  pJob->dbVgNum = dbVgNum;
  pJob->udfNum = udfNum;
  pJob->dbCfgNum = dbCfgNum;
  pJob->indexNum = indexNum;
  pJob->userNum = userNum;
D
dapan1121 已提交
577
  pJob->dbInfoNum = dbInfoNum;
D
dapan1121 已提交
578
  pJob->tbIndexNum = tbIndexNum;
D
dapan1121 已提交
579
  pJob->tbCfgNum = tbCfgNum;
D
dapan1121 已提交
580
  pJob->svrVerNum = svrVerNum;
581
  pJob->tbTagNum = tbTagNum;
582

D
dapan1121 已提交
583
#if CTG_BATCH_FETCH
dengyihao's avatar
dengyihao 已提交
584 585
  pJob->pBatchs =
      taosHashInit(CTG_DEFAULT_BATCH_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
D
dapan1121 已提交
586 587 588 589 590
  if (NULL == pJob->pBatchs) {
    ctgError("taosHashInit %d batch failed", CTG_DEFAULT_BATCH_NUM);
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
  }
#endif
591

D
dapan1121 已提交
592
  pJob->pTasks = taosArrayInit(taskNum, sizeof(SCtgTask));
D
dapan1121 已提交
593
  if (NULL == pJob->pTasks) {
D
dapan1121 已提交
594
    ctgError("taosArrayInit %d tasks failed", taskNum);
D
dapan1121 已提交
595 596 597
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
598 599
  if (pReq->forceUpdate && taskNum) {
    CTG_ERR_JRET(ctgHandleForceUpdate(pCtg, taskNum, pJob, pReq));
D
dapan1121 已提交
600 601
  }

D
dapan1121 已提交
602
  for (int32_t i = 0; i < dbVgNum; ++i) {
603
    char* dbFName = taosArrayGet(pReq->pDbVgroup, i);
D
dapan1121 已提交
604
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_DB_VGROUP, dbFName, NULL));
D
dapan1121 已提交
605 606 607
  }

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

D
dapan1121 已提交
612
  for (int32_t i = 0; i < dbInfoNum; ++i) {
613
    char* dbFName = taosArrayGet(pReq->pDbInfo, i);
D
dapan1121 已提交
614
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_DB_INFO, dbFName, NULL));
D
dapan1121 已提交
615 616
  }

D
dapan1121 已提交
617
#if 0
D
dapan1121 已提交
618
  for (int32_t i = 0; i < tbMetaNum; ++i) {
619
    SName* name = taosArrayGet(pReq->pTableMeta, i);
D
dapan1121 已提交
620
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_TB_META, name, NULL));
D
dapan1121 已提交
621
  }
D
dapan1121 已提交
622 623 624 625 626
#else
  if (tbMetaNum > 0) {
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_TB_META_BATCH, pReq->pTableMeta, NULL));
  }
#endif
D
dapan1121 已提交
627

628
#if 0
D
dapan1121 已提交
629
  for (int32_t i = 0; i < tbHashNum; ++i) {
630
    SName* name = taosArrayGet(pReq->pTableHash, i);
D
dapan1121 已提交
631
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_TB_HASH, name, NULL));
D
dapan1121 已提交
632
  }
633 634 635 636 637
#else
  if (tbHashNum > 0) {
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_TB_HASH_BATCH, pReq->pTableHash, NULL));
  }
#endif
D
dapan1121 已提交
638

D
dapan1121 已提交
639 640
  for (int32_t i = 0; i < tbIndexNum; ++i) {
    SName* name = taosArrayGet(pReq->pTableIndex, i);
D
dapan1121 已提交
641
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_TB_SMA_INDEX, name, NULL));
D
dapan1121 已提交
642 643 644 645 646
  }

  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 已提交
647 648
  }

649 650 651 652 653 654
  for (int32_t i = 0; i < tbCfgNum; ++i) {
    SName* name = taosArrayGet(pReq->pTableTag, i);
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_TB_TAG, name, NULL));
  }


D
dapan1121 已提交
655
  for (int32_t i = 0; i < indexNum; ++i) {
656
    char* indexName = taosArrayGet(pReq->pIndex, i);
D
dapan1121 已提交
657
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_INDEX_INFO, indexName, NULL));
D
dapan1121 已提交
658 659 660
  }

  for (int32_t i = 0; i < udfNum; ++i) {
661
    char* udfName = taosArrayGet(pReq->pUdf, i);
D
dapan1121 已提交
662
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_UDF, udfName, NULL));
D
dapan1121 已提交
663 664 665
  }

  for (int32_t i = 0; i < userNum; ++i) {
666
    SUserAuthInfo* user = taosArrayGet(pReq->pUser, i);
D
dapan1121 已提交
667
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_USER, user, NULL));
D
dapan1121 已提交
668 669 670
  }

  if (qnodeNum) {
D
dapan1121 已提交
671
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_QNODE, NULL, NULL));
D
dapan1121 已提交
672 673
  }

D
dapan1121 已提交
674 675 676 677
  if (dnodeNum) {
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_DNODE, NULL, NULL));
  }

D
dapan1121 已提交
678 679 680 681
  if (svrVerNum) {
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_SVR_VER, NULL, NULL));
  }

H
Haojun Liao 已提交
682 683 684 685 686 687 688 689
  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 已提交
690
  double el = (taosGetTimestampUs() - st) / 1000.0;
691 692
  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 已提交
693 694
  return TSDB_CODE_SUCCESS;

D
dapan1121 已提交
695
_return:
D
dapan1121 已提交
696
  ctgFreeJob(*job);
D
dapan1121 已提交
697 698 699 700 701 702
  CTG_RET(code);
}

int32_t ctgDumpTbMetaRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pTableMeta) {
D
dapan1121 已提交
703
    pJob->jobRes.pTableMeta = taosArrayInit(pJob->tbMetaNum, sizeof(SMetaRes));
D
dapan1121 已提交
704 705 706 707 708
    if (NULL == pJob->jobRes.pTableMeta) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
709 710
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pTableMeta, &res);
D
dapan1121 已提交
711 712 713 714

  return TSDB_CODE_SUCCESS;
}

715
int32_t ctgDumpTbMetasRes(SCtgTask* pTask) {
D
dapan1121 已提交
716 717 718 719 720 721 722
  SCtgJob* pJob = pTask->pJob;

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

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
723 724 725
int32_t ctgDumpDbVgRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pDbVgroup) {
D
dapan1121 已提交
726
    pJob->jobRes.pDbVgroup = taosArrayInit(pJob->dbVgNum, sizeof(SMetaRes));
D
dapan1121 已提交
727 728 729 730 731
    if (NULL == pJob->jobRes.pDbVgroup) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
732 733
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pDbVgroup, &res);
D
dapan1121 已提交
734 735 736 737 738 739 740

  return TSDB_CODE_SUCCESS;
}

int32_t ctgDumpTbHashRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pTableHash) {
D
dapan1121 已提交
741
    pJob->jobRes.pTableHash = taosArrayInit(pJob->tbHashNum, sizeof(SMetaRes));
D
dapan1121 已提交
742 743 744 745 746
    if (NULL == pJob->jobRes.pTableHash) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
747 748
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pTableHash, &res);
D
dapan1121 已提交
749 750 751 752

  return TSDB_CODE_SUCCESS;
}

753
int32_t ctgDumpTbHashsRes(SCtgTask* pTask) {
754 755 756 757 758 759 760
  SCtgJob* pJob = pTask->pJob;

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

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
761 762 763 764 765 766 767 768 769 770
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 已提交
771
  taosArrayPush(pJob->jobRes.pTableIndex, &res);
D
dapan1121 已提交
772 773 774 775

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
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 已提交
791 792 793
int32_t ctgDumpIndexRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pIndex) {
D
dapan1121 已提交
794
    pJob->jobRes.pIndex = taosArrayInit(pJob->indexNum, sizeof(SMetaRes));
D
dapan1121 已提交
795 796 797 798 799
    if (NULL == pJob->jobRes.pIndex) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
800 801
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pIndex, &res);
D
dapan1121 已提交
802 803 804 805 806 807

  return TSDB_CODE_SUCCESS;
}

int32_t ctgDumpQnodeRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
D
dapan1121 已提交
808 809 810 811 812 813
  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 已提交
814

D
dapan1121 已提交
815 816
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pQnodeList, &res);
D
dapan1121 已提交
817 818 819 820

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
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 已提交
836 837 838
int32_t ctgDumpDbCfgRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pDbCfg) {
D
dapan1121 已提交
839
    pJob->jobRes.pDbCfg = taosArrayInit(pJob->dbCfgNum, sizeof(SMetaRes));
D
dapan1121 已提交
840 841 842 843 844
    if (NULL == pJob->jobRes.pDbCfg) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
845 846
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pDbCfg, &res);
D
dapan1121 已提交
847 848 849 850

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
851 852 853
int32_t ctgDumpDbInfoRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pDbInfo) {
D
dapan1121 已提交
854
    pJob->jobRes.pDbInfo = taosArrayInit(pJob->dbInfoNum, sizeof(SMetaRes));
D
dapan1121 已提交
855 856 857 858 859
    if (NULL == pJob->jobRes.pDbInfo) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
860 861
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pDbInfo, &res);
D
dapan1121 已提交
862 863 864 865

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
866 867 868
int32_t ctgDumpUdfRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pUdfList) {
D
dapan1121 已提交
869
    pJob->jobRes.pUdfList = taosArrayInit(pJob->udfNum, sizeof(SMetaRes));
D
dapan1121 已提交
870 871 872 873 874
    if (NULL == pJob->jobRes.pUdfList) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
875 876
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pUdfList, &res);
D
dapan1121 已提交
877 878 879 880 881 882 883

  return TSDB_CODE_SUCCESS;
}

int32_t ctgDumpUserRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pUser) {
D
dapan1121 已提交
884
    pJob->jobRes.pUser = taosArrayInit(pJob->userNum, sizeof(SMetaRes));
D
dapan1121 已提交
885 886 887 888 889
    if (NULL == pJob->jobRes.pUser) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
890 891
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pUser, &res);
D
dapan1121 已提交
892 893 894 895

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
896 897 898 899 900 901 902 903 904 905 906
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;
907

D
dapan1121 已提交
908 909 910
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
911
int32_t ctgCallSubCb(SCtgTask* pTask) {
D
dapan1121 已提交
912
  int32_t code = 0;
913

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

D
dapan1121 已提交
916 917
  int32_t parentNum = taosArrayGetSize(pTask->pParents);
  for (int32_t i = 0; i < parentNum; ++i) {
dengyihao's avatar
dengyihao 已提交
918 919
    SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
    SCtgTask*   pParent = taosArrayGetP(pTask->pParents, i);
920

D
dapan1121 已提交
921 922 923 924 925 926 927
    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;
      }
    }
928

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

931
    pParMsgCtx->pBatchs = pMsgCtx->pBatchs;
D
dapan1121 已提交
932 933
    CTG_ERR_JRET(pParent->subRes.fp(pParent));
  }
934

D
dapan1121 已提交
935 936 937 938
_return:

  CTG_UNLOCK(CTG_WRITE, &pTask->lock);

939
  CTG_RET(code);
D
dapan1121 已提交
940 941
}

D
dapan1121 已提交
942 943
int32_t ctgCallUserCb(void* param) {
  SCtgJob* pJob = (SCtgJob*)param;
D
dapan1121 已提交
944 945

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

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

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

D
dapan1121 已提交
951 952 953 954
  taosRemoveRef(gCtgMgmt.jobPool, pJob->refId);

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
955

dengyihao's avatar
dengyihao 已提交
956
void ctgUpdateJobErrCode(SCtgJob* pJob, int32_t errCode) {
dengyihao's avatar
dengyihao 已提交
957
  if (!NEED_CLIENT_REFRESH_VG_ERROR(errCode) || errCode == TSDB_CODE_SUCCESS) return;
dengyihao's avatar
dengyihao 已提交
958

dengyihao's avatar
dengyihao 已提交
959
  atomic_store_32(&pJob->jobResCode, errCode);
dengyihao's avatar
dengyihao 已提交
960
  qDebug("QID:0x%" PRIx64 " ctg job errCode updated to %s", pJob->queryId, tstrerror(errCode));
dengyihao's avatar
dengyihao 已提交
961
  return;
dengyihao's avatar
dengyihao 已提交
962 963
}

D
dapan1121 已提交
964 965
int32_t ctgHandleTaskEnd(SCtgTask* pTask, int32_t rspCode) {
  SCtgJob* pJob = pTask->pJob;
dengyihao's avatar
dengyihao 已提交
966
  int32_t  code = 0;
D
dapan1121 已提交
967

D
dapan1121 已提交
968 969 970 971
  if (CTG_TASK_DONE == pTask->status) {
    return TSDB_CODE_SUCCESS;
  }

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

D
dapan1121 已提交
974
  pTask->code = rspCode;
D
dapan1121 已提交
975 976
  pTask->status = CTG_TASK_DONE;

D
dapan1121 已提交
977
  ctgCallSubCb(pTask);
D
dapan1121 已提交
978 979 980

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

    ctgUpdateJobErrCode(pJob, rspCode);
D
dapan1121 已提交
985 986 987 988 989 990 991
    return TSDB_CODE_SUCCESS;
  }

  CTG_ERR_JRET(ctgMakeAsyncRes(pJob));

_return:

dengyihao's avatar
dengyihao 已提交
992 993
  ctgUpdateJobErrCode(pJob, rspCode);
  // pJob->jobResCode = code;
D
dapan1121 已提交
994

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

D
dapan1121 已提交
998
  taosAsyncExec(ctgCallUserCb, pJob, NULL);
999

D
dapan1121 已提交
1000 1001 1002
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1003 1004 1005 1006 1007
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 已提交
1008
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1009 1010 1011 1012 1013
  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 已提交
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026

  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 已提交
1027
      CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, pName, &vgInfo, NULL, tReq));
D
dapan1121 已提交
1028 1029 1030 1031 1032

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

D
dapan1121 已提交
1034 1035 1036 1037
      if (CTG_IS_META_NULL(pOut->metaType)) {
        if (CTG_FLAG_IS_STB(flag)) {
          char dbFName[TSDB_DB_FNAME_LEN] = {0};
          tNameGetFullDbName(pName, dbFName);
1038

D
dapan1121 已提交
1039 1040 1041 1042
          CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache));
          if (NULL != dbCache) {
            SVgroupInfo vgInfo = {0};
            CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, dbCache->vgCache.vgInfo, pName, &vgInfo));
1043

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

1046
            *vgId = vgInfo.vgId;
D
dapan1121 已提交
1047
            CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, pName, &vgInfo, NULL, tReq));
D
dapan1121 已提交
1048 1049

            ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1050
            dbCache = NULL;
D
dapan1121 已提交
1051 1052
          } else {
            SBuildUseDBInput input = {0};
1053

D
dapan1121 已提交
1054 1055
            tstrncpy(input.db, dbFName, tListLen(input.db));
            input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
1056

D
dapan1121 已提交
1057
            CTG_ERR_JRET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, tReq));
D
dapan1121 已提交
1058 1059 1060 1061
          }

          return TSDB_CODE_SUCCESS;
        }
1062

D
dapan1121 已提交
1063 1064
        ctgError("no tbmeta got, tbName:%s", tNameGetTableName(pName));
        ctgRemoveTbMetaFromCache(pCtg, pName, false);
1065

D
dapan1121 已提交
1066 1067 1068 1069 1070 1071 1072 1073
        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);
      }
1074

D
dapan1121 已提交
1075 1076 1077 1078
      break;
    }
    case TDMT_VND_TABLE_META: {
      STableMetaOutput* pOut = (STableMetaOutput*)pMsgCtx->out;
1079

D
dapan1121 已提交
1080 1081 1082 1083 1084 1085 1086 1087 1088
      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;
      }
1089

D
dapan1121 已提交
1090 1091
      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));
1092

D
dapan1121 已提交
1093
        taosMemoryFreeClear(pOut->tbMeta);
1094

D
dapan1121 已提交
1095
        CTG_RET(ctgGetTbMetaFromMnode(pCtg, pConn, pName, NULL, tReq));
D
dapan1121 已提交
1096 1097 1098 1099 1100 1101 1102 1103 1104
      } 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;

1105
          taosMemoryFreeClear(pOut->tbMeta);
D
dapan1121 已提交
1106 1107 1108 1109 1110
          CTG_ERR_JRET(ctgReadTbMetaFromCache(pCtg, &stbCtx, &pOut->tbMeta));
          if (pOut->tbMeta) {
            exist = 1;
          }
        }
1111

D
dapan1121 已提交
1112 1113
        if (0 == exist) {
          TSWAP(pMsgCtx->lastOut, pMsgCtx->out);
D
dapan1121 已提交
1114
          CTG_RET(ctgGetTbMetaFromMnodeImpl(pCtg, pConn, pOut->dbFName, pOut->tbName, NULL, tReq));
D
dapan1121 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
        }
      }
      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));
  }
1131

dengyihao's avatar
dengyihao 已提交
1132 1133 1134 1135 1136 1137 1138
  /*
    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 已提交
1139

dengyihao's avatar
dengyihao 已提交
1140 1141 1142 1143
      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 已提交
1144

dengyihao's avatar
dengyihao 已提交
1145 1146 1147 1148 1149 1150
        return TSDB_CODE_SUCCESS;
      }

      memcpy(pOut->tbMeta, &pOut->ctbMeta, sizeof(pOut->ctbMeta));
    }
  */
D
dapan1121 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159

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

_return:

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

1160
  if (code) {
dengyihao's avatar
dengyihao 已提交
1161 1162
    ctgTaskError("Get table %d.%s.%s meta failed with error %s", pName->acctId, pName->dbname, pName->tname,
                 tstrerror(code));
1163
  }
1164
  if (pTask->res || code) {
D
dapan1121 已提交
1165 1166
    ctgHandleTaskEnd(pTask, code);
  }
1167

D
dapan1121 已提交
1168 1169 1170
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1171 1172 1173 1174 1175
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 已提交
1176
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1177 1178 1179 1180 1181 1182 1183
  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 已提交
1184

D
dapan1121 已提交
1185
  CTG_ERR_JRET(ctgProcessRspMsg(pMsgCtx->out, reqType, pMsg->pData, pMsg->len, rspCode, pMsgCtx->target));
D
dapan1121 已提交
1186 1187 1188

  switch (reqType) {
    case TDMT_MND_USE_DB: {
D
dapan1121 已提交
1189
      SUseDbOutput* pOut = (SUseDbOutput*)pMsgCtx->out;
D
dapan1121 已提交
1190 1191

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

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

D
dapan1121 已提交
1196
      *vgId = vgInfo.vgId;
D
dapan1121 已提交
1197
      CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, pName, &vgInfo, NULL, tReq));
D
dapan1121 已提交
1198 1199 1200 1201

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

D
dapan1121 已提交
1204
      if (CTG_IS_META_NULL(pOut->metaType)) {
D
dapan1121 已提交
1205
        if (CTG_FLAG_IS_STB(flag)) {
D
dapan1121 已提交
1206
          char dbFName[TSDB_DB_FNAME_LEN] = {0};
D
dapan1121 已提交
1207
          tNameGetFullDbName(pName, dbFName);
1208

D
dapan1121 已提交
1209 1210 1211
          CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache));
          if (NULL != dbCache) {
            SVgroupInfo vgInfo = {0};
D
dapan1121 已提交
1212
            CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, dbCache->vgCache.vgInfo, pName, &vgInfo));
1213

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

1216
            *vgId = vgInfo.vgId;
D
dapan1121 已提交
1217
            CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, pName, &vgInfo, NULL, tReq));
D
dapan1121 已提交
1218

D
dapan1121 已提交
1219
            ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1220
            dbCache = NULL;
D
dapan1121 已提交
1221 1222
          } else {
            SBuildUseDBInput input = {0};
1223

D
dapan1121 已提交
1224 1225
            tstrncpy(input.db, dbFName, tListLen(input.db));
            input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
1226

D
dapan1121 已提交
1227
            CTG_ERR_JRET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, tReq));
D
dapan1121 已提交
1228 1229 1230 1231
          }

          return TSDB_CODE_SUCCESS;
        }
1232

1233
        ctgTaskError("no tbmeta got, tbName:%s", tNameGetTableName(pName));
D
dapan1121 已提交
1234
        ctgRemoveTbMetaFromCache(pCtg, pName, false);
1235

D
dapan1121 已提交
1236 1237 1238
        CTG_ERR_JRET(CTG_ERR_CODE_TABLE_NOT_EXIST);
      }

D
dapan1121 已提交
1239 1240 1241
      if (pMsgCtx->lastOut) {
        TSWAP(pMsgCtx->out, pMsgCtx->lastOut);
        STableMetaOutput* pLastOut = (STableMetaOutput*)pMsgCtx->out;
D
dapan1121 已提交
1242 1243
        TSWAP(pLastOut->tbMeta, pOut->tbMeta);
      }
1244

D
dapan1121 已提交
1245 1246 1247
      break;
    }
    case TDMT_VND_TABLE_META: {
D
dapan1121 已提交
1248
      STableMetaOutput* pOut = (STableMetaOutput*)pMsgCtx->out;
1249

D
dapan1121 已提交
1250
      if (CTG_IS_META_NULL(pOut->metaType)) {
1251
        ctgTaskError("no tbmeta got, tbNmae:%s", tNameGetTableName(pName));
D
dapan1121 已提交
1252
        ctgRemoveTbMetaFromCache(pCtg, pName, false);
D
dapan1121 已提交
1253 1254 1255
        CTG_ERR_JRET(CTG_ERR_CODE_TABLE_NOT_EXIST);
      }

D
dapan1121 已提交
1256
      if (CTG_FLAG_IS_STB(flag)) {
D
dapan1121 已提交
1257 1258
        break;
      }
1259

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

D
dapan1121 已提交
1263
        taosMemoryFreeClear(pOut->tbMeta);
1264

D
dapan1121 已提交
1265
        CTG_RET(ctgGetTbMetaFromMnode(pCtg, pConn, pName, NULL, tReq));
D
dapan1121 已提交
1266 1267
      } else if (CTG_IS_META_BOTH(pOut->metaType)) {
        int32_t exist = 0;
D
dapan1121 已提交
1268 1269 1270 1271 1272 1273 1274
        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 已提交
1275
          STableMeta* stbMeta = NULL;
1276
          (void)ctgReadTbMetaFromCache(pCtg, &stbCtx, &stbMeta);
D
dapan1121 已提交
1277
          if (stbMeta && stbMeta->sversion >= pOut->tbMeta->sversion) {
1278
            ctgTaskDebug("use cached stb meta, tbName:%s", tNameGetTableName(pName));
D
dapan1121 已提交
1279
            exist = 1;
dengyihao's avatar
dengyihao 已提交
1280
            taosMemoryFreeClear(stbMeta);
D
dapan1121 已提交
1281
          } else {
1282
            ctgTaskDebug("need to get/update stb meta, tbName:%s", tNameGetTableName(pName));
D
dapan1121 已提交
1283 1284
            taosMemoryFreeClear(pOut->tbMeta);
            taosMemoryFreeClear(stbMeta);
D
dapan1121 已提交
1285
          }
D
dapan1121 已提交
1286
        }
1287

D
dapan1121 已提交
1288
        if (0 == exist) {
D
dapan1121 已提交
1289
          TSWAP(pMsgCtx->lastOut, pMsgCtx->out);
D
dapan1121 已提交
1290
          CTG_RET(ctgGetTbMetaFromMnodeImpl(pCtg, pConn, pOut->dbFName, pOut->tbName, NULL, tReq));
D
dapan1121 已提交
1291 1292 1293 1294 1295
        }
      }
      break;
    }
    default:
1296
      ctgTaskError("invalid reqType %d", reqType);
D
dapan1121 已提交
1297 1298 1299
      CTG_ERR_JRET(TSDB_CODE_INVALID_MSG);
  }

D
dapan1121 已提交
1300
  STableMetaOutput* pOut = (STableMetaOutput*)pMsgCtx->out;
D
dapan1121 已提交
1301 1302 1303 1304 1305

  ctgUpdateTbMetaToCache(pCtg, pOut, false);

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

dengyihao's avatar
dengyihao 已提交
1308 1309 1310 1311 1312 1313 1314
  /*
    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 已提交
1315

dengyihao's avatar
dengyihao 已提交
1316 1317 1318 1319
      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 已提交
1320

dengyihao's avatar
dengyihao 已提交
1321 1322 1323 1324 1325 1326
        return TSDB_CODE_SUCCESS;
      }

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

1328
  SMetaRes* pRes = taosArrayGet(ctx->pResList, pFetch->resIdx);
D
dapan1121 已提交
1329 1330 1331 1332
  pRes->code = 0;
  pRes->pRes = pOut->tbMeta;
  pOut->tbMeta = NULL;
  if (0 == atomic_sub_fetch_32(&ctx->fetchNum, 1)) {
1333
    TSWAP(pTask->res, ctx->pResList);
1334
    taskDone = true;
D
dapan1121 已提交
1335
  }
D
dapan1121 已提交
1336 1337 1338 1339

_return:

  if (dbCache) {
D
dapan1121 已提交
1340
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1341 1342
  }

D
dapan1121 已提交
1343
  if (code) {
1344
    SMetaRes* pRes = taosArrayGet(ctx->pResList, pFetch->resIdx);
D
dapan1121 已提交
1345 1346 1347
    pRes->code = code;
    pRes->pRes = NULL;
    if (0 == atomic_sub_fetch_32(&ctx->fetchNum, 1)) {
1348
      TSWAP(pTask->res, ctx->pResList);
1349
      taskDone = true;
D
dapan1121 已提交
1350
    }
dengyihao's avatar
dengyihao 已提交
1351 1352
    ctgTaskError("Get table %d.%s.%s meta failed with error %s", pName->acctId, pName->dbname, pName->tname,
                 tstrerror(code));
D
dapan1121 已提交
1353 1354
  }

1355
  if (pTask->res && taskDone) {
D
dapan1121 已提交
1356 1357
    ctgHandleTaskEnd(pTask, code);
  }
1358

D
dapan1121 已提交
1359 1360 1361
  CTG_RET(code);
}

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

  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));
D
dapan1121 已提交
1369 1370 1371 1372

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

D
dapan1121 已提交
1375 1376 1377 1378
      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 已提交
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393

      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 已提交
1394 1395 1396
int32_t ctgHandleGetTbHashRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t        code = 0;
  SCtgTask*      pTask = tReq->pTask;
D
dapan1121 已提交
1397
  SCtgTbHashCtx* ctx = (SCtgTbHashCtx*)pTask->taskCtx;
dengyihao's avatar
dengyihao 已提交
1398
  SCatalog*      pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1399 1400

  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));
D
dapan1121 已提交
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411

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

D
dapan1121 已提交
1413
      CTG_ERR_JRET(ctgUpdateVgroupEnqueue(pCtg, ctx->dbFName, pOut->dbId, pOut->dbVgroup, false));
D
dapan1121 已提交
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
      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 已提交
1430 1431 1432
int32_t ctgHandleGetTbHashsRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t         code = 0;
  SCtgTask*       pTask = tReq->pTask;
1433
  SCtgTbHashsCtx* ctx = (SCtgTbHashsCtx*)pTask->taskCtx;
dengyihao's avatar
dengyihao 已提交
1434 1435 1436 1437
  SCatalog*       pCtg = pTask->pJob->pCtg;
  SCtgMsgCtx*     pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, tReq->msgIdx);
  SCtgFetch*      pFetch = taosArrayGet(ctx->pFetchs, tReq->msgIdx);
  bool            taskDone = false;
1438 1439 1440 1441 1442 1443 1444

  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;

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

1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
      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);
1460
    taskDone = true;
1461 1462 1463 1464 1465
  }

_return:

  if (code) {
1466
    STablesReq* pReq = taosArrayGet(ctx->pNames, pFetch->dbIdx);
dengyihao's avatar
dengyihao 已提交
1467
    int32_t     num = taosArrayGetSize(pReq->pTables);
1468
    for (int32_t i = 0; i < num; ++i) {
dengyihao's avatar
dengyihao 已提交
1469
      SMetaRes* pRes = taosArrayGet(ctx->pResList, pFetch->resIdx + i);
1470 1471 1472
      pRes->code = code;
      pRes->pRes = NULL;
    }
1473

1474 1475
    if (0 == atomic_sub_fetch_32(&ctx->fetchNum, 1)) {
      TSWAP(pTask->res, ctx->pResList);
1476
      taskDone = true;
1477 1478 1479
    }
  }

1480
  if (pTask->res && taskDone) {
1481 1482 1483 1484 1485 1486
    ctgHandleTaskEnd(pTask, code);
  }

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1487 1488
int32_t ctgHandleGetTbIndexRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1489
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1490
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));
D
dapan1121 已提交
1491

D
dapan1121 已提交
1492
  STableIndex* pOut = (STableIndex*)pTask->msgCtx.out;
dengyihao's avatar
dengyihao 已提交
1493
  SArray*      pInfo = NULL;
D
dapan1121 已提交
1494 1495 1496
  CTG_ERR_JRET(ctgCloneTableIndex(pOut->pIndex, &pInfo));
  pTask->res = pInfo;

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

D
dapan1121 已提交
1500
_return:
D
dapan1121 已提交
1501

X
Xiaoyu Wang 已提交
1502 1503 1504
  if (TSDB_CODE_MND_DB_INDEX_NOT_EXIST == code) {
    code = TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
1505 1506 1507 1508 1509
  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

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

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

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

  ctgHandleTaskEnd(pTask, code);

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

1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541

int32_t ctgHandleGetTbTagRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
  SCtgTask* pTask = tReq->pTask;
  CTG_ERR_JRET(ctgProcessRspMsg(&pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

  STableCfgRsp* pRsp = (STableCfgRsp*)pTask->msgCtx.out;
  
  TSWAP(pTask->res, pTask->msgCtx.out);

_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}


dengyihao's avatar
dengyihao 已提交
1542 1543
int32_t ctgHandleGetDbCfgRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1544
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1545 1546 1547
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

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

D
dapan1121 已提交
1549 1550 1551 1552 1553 1554 1555
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1556
int32_t ctgHandleGetDbInfoRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
D
dapan1121 已提交
1557 1558 1559
  CTG_RET(TSDB_CODE_APP_ERROR);
}

dengyihao's avatar
dengyihao 已提交
1560 1561
int32_t ctgHandleGetQnodeRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1562
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1563 1564 1565
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

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

D
dapan1121 已提交
1567 1568 1569 1570 1571 1572 1573
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1574 1575
int32_t ctgHandleGetDnodeRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1576
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1577 1578 1579
  CTG_ERR_JRET(ctgProcessRspMsg(&pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

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

D
dapan1121 已提交
1581 1582 1583 1584 1585 1586 1587
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1588 1589
int32_t ctgHandleGetIndexRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1590
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1591 1592 1593
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

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

D
dapan1121 已提交
1595 1596 1597 1598 1599 1600 1601
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1602 1603
int32_t ctgHandleGetUdfRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1604
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1605 1606 1607
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

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

D
dapan1121 已提交
1609 1610 1611 1612 1613 1614 1615
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1616 1617 1618 1619
int32_t ctgHandleGetUserRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t          code = 0;
  SCtgTask*        pTask = tReq->pTask;
  SCatalog*        pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1620
  SGetUserAuthRsp* pOut = (SGetUserAuthRsp*)pTask->msgCtx.out;
D
dapan1121 已提交
1621 1622 1623

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

D
dapan1121 已提交
1624 1625
  ctgUpdateUserEnqueue(pCtg, pOut, true);
  taosMemoryFreeClear(pTask->msgCtx.out);
D
dapan1121 已提交
1626

D
dapan1121 已提交
1627
  CTG_ERR_JRET((*gCtgAsyncFps[pTask->type].launchFp)(pTask));
D
dapan1121 已提交
1628

D
dapan1121 已提交
1629
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1630 1631 1632 1633 1634 1635 1636 1637

_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1638 1639
int32_t ctgHandleGetSvrVerRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1640
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1641 1642 1643 1644

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

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

D
dapan1121 已提交
1646 1647 1648 1649 1650 1651 1652
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1653 1654 1655
int32_t ctgAsyncRefreshTbMeta(SCtgTaskReq* tReq, int32_t flag, SName* pName, int32_t* vgId) {
  SCtgTask*         pTask = tReq->pTask;
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1656
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1657
  int32_t           code = 0;
D
dapan1121 已提交
1658

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

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

D
dapan1121 已提交
1665 1666
  if (CTG_FLAG_IS_STB(flag)) {
    ctgDebug("will refresh tbmeta, supposed to be stb, tbName:%s", tNameGetTableName(pName));
D
dapan1121 已提交
1667 1668

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

dengyihao's avatar
dengyihao 已提交
1672 1673
  SCtgDBCache* dbCache = NULL;
  char         dbFName[TSDB_DB_FNAME_LEN] = {0};
D
dapan1121 已提交
1674
  tNameGetFullDbName(pName, dbFName);
1675

D
dapan1121 已提交
1676
  CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache));
D
dapan1121 已提交
1677
  if (dbCache) {
D
dapan1121 已提交
1678
    SVgroupInfo vgInfo = {0};
D
dapan1121 已提交
1679
    CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, dbCache->vgCache.vgInfo, pName, &vgInfo));
D
dapan1121 已提交
1680

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

D
dapan1121 已提交
1683
    *vgId = vgInfo.vgId;
D
dapan1121 已提交
1684
    CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, pName, &vgInfo, NULL, tReq));
D
dapan1121 已提交
1685 1686 1687 1688 1689 1690
  } else {
    SBuildUseDBInput input = {0};

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

D
dapan1121 已提交
1691
    CTG_ERR_JRET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, tReq));
D
dapan1121 已提交
1692 1693 1694 1695 1696
  }

_return:

  if (dbCache) {
D
dapan1121 已提交
1697
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1698 1699 1700 1701 1702
  }

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1703 1704
int32_t ctgLaunchGetTbMetaTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1705
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1706 1707
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
1708 1709 1710
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
1711

D
dapan1121 已提交
1712
  CTG_ERR_RET(ctgGetTbMetaFromCache(pCtg, (SCtgTbMetaCtx*)pTask->taskCtx, (STableMeta**)&pTask->res));
D
dapan1121 已提交
1713 1714 1715 1716 1717
  if (pTask->res) {
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
1718
  SCtgTbMetaCtx* pCtx = (SCtgTbMetaCtx*)pTask->taskCtx;
dengyihao's avatar
dengyihao 已提交
1719
  SCtgTaskReq    tReq;
D
dapan1121 已提交
1720
  tReq.pTask = pTask;
1721
  tReq.msgIdx = -1;
D
dapan1121 已提交
1722
  CTG_ERR_RET(ctgAsyncRefreshTbMeta(&tReq, pCtx->flag, pCtx->pName, &pCtx->vgId));
D
dapan1121 已提交
1723 1724 1725 1726

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1727 1728
int32_t ctgLaunchGetTbMetasTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1729
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1730 1731
  SCtgTbMetasCtx*   pCtx = (SCtgTbMetasCtx*)pTask->taskCtx;
  SCtgJob*          pJob = pTask->pJob;
D
dapan1121 已提交
1732

1733 1734 1735 1736 1737
  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);
1738
    ctgDebug("start to check tb metas in db %s, tbNum %ld", pReq->dbFName, taosArrayGetSize(pReq->pTables));
1739 1740 1741
    CTG_ERR_RET(ctgGetTbMetasFromCache(pCtg, pConn, pCtx, i, &fetchIdx, baseResIdx, pReq->pTables));
    baseResIdx += taosArrayGetSize(pReq->pTables);
  }
1742

1743 1744 1745
  pCtx->fetchNum = taosArrayGetSize(pCtx->pFetchs);
  if (pCtx->fetchNum <= 0) {
    TSWAP(pTask->res, pCtx->pResList);
1746

D
dapan1121 已提交
1747 1748 1749
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }
1750

1751
  pTask->msgCtxs = taosArrayInit_s(sizeof(SCtgMsgCtx), pCtx->fetchNum);
D
dapan1121 已提交
1752
  for (int32_t i = 0; i < pCtx->fetchNum; ++i) {
dengyihao's avatar
dengyihao 已提交
1753 1754 1755
    SCtgFetch*  pFetch = taosArrayGet(pCtx->pFetchs, i);
    SName*      pName = ctgGetFetchName(pCtx->pNames, pFetch);
    SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, i);
D
dapan1121 已提交
1756 1757 1758
    if (NULL == pMsgCtx->pBatchs) {
      pMsgCtx->pBatchs = pJob->pBatchs;
    }
1759

D
dapan1121 已提交
1760 1761
    SCtgTaskReq tReq;
    tReq.pTask = pTask;
1762
    tReq.msgIdx = pFetch->fetchIdx;
D
dapan1121 已提交
1763
    CTG_ERR_RET(ctgAsyncRefreshTbMeta(&tReq, pFetch->flag, pName, &pFetch->vgId));
D
dapan1121 已提交
1764
  }
1765

D
dapan1121 已提交
1766 1767 1768
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1769 1770 1771
int32_t ctgLaunchGetDbVgTask(SCtgTask* pTask) {
  int32_t           code = 0;
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1772
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1773 1774 1775 1776
  SCtgDBCache*      dbCache = NULL;
  SCtgDbVgCtx*      pCtx = (SCtgDbVgCtx*)pTask->taskCtx;
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
1777 1778 1779
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
1780

D
dapan1121 已提交
1781 1782
  CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, pCtx->dbFName, &dbCache));
  if (NULL != dbCache) {
D
dapan1121 已提交
1783
    CTG_ERR_JRET(ctgGenerateVgList(pCtg, dbCache->vgCache.vgInfo->vgHash, (SArray**)&pTask->res));
D
dapan1121 已提交
1784 1785 1786

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

D
dapan1121 已提交
1788 1789 1790
    CTG_ERR_JRET(ctgHandleTaskEnd(pTask, 0));
  } else {
    SBuildUseDBInput input = {0};
1791

D
dapan1121 已提交
1792 1793
    tstrncpy(input.db, pCtx->dbFName, tListLen(input.db));
    input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
D
dapan1121 已提交
1794 1795 1796 1797 1798

    SCtgTaskReq tReq;
    tReq.pTask = pTask;
    tReq.msgIdx = -1;
    CTG_ERR_RET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, &tReq));
D
dapan1121 已提交
1799 1800 1801 1802 1803
  }

_return:

  if (dbCache) {
D
dapan1121 已提交
1804
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1805 1806 1807 1808 1809
  }

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1810 1811 1812
int32_t ctgLaunchGetTbHashTask(SCtgTask* pTask) {
  int32_t           code = 0;
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1813
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1814 1815 1816 1817
  SCtgDBCache*      dbCache = NULL;
  SCtgTbHashCtx*    pCtx = (SCtgTbHashCtx*)pTask->taskCtx;
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
1818 1819 1820
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
1821

D
dapan1121 已提交
1822 1823 1824 1825 1826 1827
  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 已提交
1828
    CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, dbCache->vgCache.vgInfo, pCtx->pName, (SVgroupInfo*)pTask->res));
D
dapan1121 已提交
1829 1830 1831

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

D
dapan1121 已提交
1833 1834 1835
    CTG_ERR_JRET(ctgHandleTaskEnd(pTask, 0));
  } else {
    SBuildUseDBInput input = {0};
1836

D
dapan1121 已提交
1837 1838
    tstrncpy(input.db, pCtx->dbFName, tListLen(input.db));
    input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
D
dapan1121 已提交
1839 1840 1841

    SCtgTaskReq tReq;
    tReq.pTask = pTask;
1842
    tReq.msgIdx = -1;
D
dapan1121 已提交
1843
    CTG_ERR_RET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, &tReq));
D
dapan1121 已提交
1844 1845 1846 1847 1848
  }

_return:

  if (dbCache) {
D
dapan1121 已提交
1849
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1850 1851 1852 1853 1854
  }

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1855 1856
int32_t ctgLaunchGetTbHashsTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
1857
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1858 1859 1860 1861 1862 1863 1864
  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;
1865

1866 1867
  for (int32_t i = 0; i < dbNum; ++i) {
    STablesReq* pReq = taosArrayGet(pCtx->pNames, i);
1868

1869
    CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, pReq->dbFName, &dbCache));
1870 1871

    if (NULL != dbCache) {
D
dapan1121 已提交
1872 1873
      SCtgTaskReq tReq;
      tReq.pTask = pTask;
1874
      tReq.msgIdx = -1;
dengyihao's avatar
dengyihao 已提交
1875 1876
      CTG_ERR_JRET(
          ctgGetVgInfosFromHashValue(pCtg, &tReq, dbCache->vgCache.vgInfo, pCtx, pReq->dbFName, pReq->pTables, false));
1877 1878 1879

      ctgReleaseVgInfoToCache(pCtg, dbCache);
      dbCache = NULL;
1880 1881

      baseResIdx += taosArrayGetSize(pReq->pTables);
1882
    } else {
1883
      ctgAddFetch(&pCtx->pFetchs, i, -1, &fetchIdx, baseResIdx, 0);
1884

1885
      baseResIdx += taosArrayGetSize(pReq->pTables);
H
Haojun Liao 已提交
1886 1887 1888 1889
      int32_t inc = baseResIdx - taosArrayGetSize(pCtx->pResList);
      for(int32_t j = 0; j < inc; ++j) {
        taosArrayPush(pCtx->pResList, &(SMetaRes){0});
      }
1890 1891 1892 1893 1894 1895
    }
  }

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

1897 1898 1899
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }
1900

1901
  pTask->msgCtxs = taosArrayInit_s(sizeof(SCtgMsgCtx), pCtx->fetchNum);
1902

1903
  for (int32_t i = 0; i < pCtx->fetchNum; ++i) {
dengyihao's avatar
dengyihao 已提交
1904
    SCtgFetch*  pFetch = taosArrayGet(pCtx->pFetchs, i);
D
dapan1121 已提交
1905
    STablesReq* pReq = taosArrayGet(pCtx->pNames, pFetch->dbIdx);
dengyihao's avatar
dengyihao 已提交
1906
    SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, i);
D
dapan1121 已提交
1907 1908 1909
    if (NULL == pMsgCtx->pBatchs) {
      pMsgCtx->pBatchs = pJob->pBatchs;
    }
1910

1911
    SBuildUseDBInput input = {0};
D
dapan1121 已提交
1912
    strcpy(input.db, pReq->dbFName);
1913

1914 1915
    input.vgVersion = CTG_DEFAULT_INVALID_VERSION;

D
dapan1121 已提交
1916 1917 1918 1919
    SCtgTaskReq tReq;
    tReq.pTask = pTask;
    tReq.msgIdx = pFetch->fetchIdx;
    CTG_ERR_RET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, &tReq));
1920 1921 1922 1923 1924 1925 1926
  }

_return:

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

1928 1929 1930
  return code;
}

dengyihao's avatar
dengyihao 已提交
1931 1932 1933
int32_t ctgLaunchGetTbIndexTask(SCtgTask* pTask) {
  int32_t           code = 0;
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1934
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1935 1936 1937 1938
  SCtgTbIndexCtx*   pCtx = (SCtgTbIndexCtx*)pTask->taskCtx;
  SArray*           pRes = NULL;
  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

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

D
dapan1121 已提交
1947 1948 1949
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }
1950

D
dapan1121 已提交
1951
  CTG_ERR_RET(ctgGetTbIndexFromMnode(pCtg, pConn, pCtx->pName, NULL, pTask));
D
dapan1121 已提交
1952 1953 1954
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1955 1956 1957
int32_t ctgLaunchGetTbCfgTask(SCtgTask* pTask) {
  int32_t           code = 0;
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1958
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1959 1960 1961
  SCtgTbCfgCtx*     pCtx = (SCtgTbCfgCtx*)pTask->taskCtx;
  SArray*           pRes = NULL;
  char              dbFName[TSDB_DB_FNAME_LEN];
D
dapan1121 已提交
1962
  tNameGetFullDbName(pCtx->pName, dbFName);
dengyihao's avatar
dengyihao 已提交
1963 1964
  SCtgJob*    pJob = pTask->pJob;
  SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
1965 1966 1967
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
1968

D
dapan1121 已提交
1969 1970
  CTG_CACHE_NHIT_INC(CTG_CI_TBL_CFG, 1);
  
D
dapan1121 已提交
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
  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;
      }
    }
1989

D
dapan1121 已提交
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
    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);
  }
2000

D
dapan1121 已提交
2001 2002 2003
  CTG_RET(code);
}

2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042

int32_t ctgLaunchGetTbTagTask(SCtgTask* pTask) {
  int32_t           code = 0;
  SCatalog*         pCtg = pTask->pJob->pCtg;
  SRequestConnInfo* pConn = &pTask->pJob->conn;
  SCtgTbTagCtx*     pCtx = (SCtgTbTagCtx*)pTask->taskCtx;
  SArray*           pRes = NULL;
  char              dbFName[TSDB_DB_FNAME_LEN];
  tNameGetFullDbName(pCtx->pName, dbFName);
  SCtgJob*    pJob = pTask->pJob;
  SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }

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

  CTG_CACHE_NHIT_INC(CTG_CI_TBL_TAG, 1);

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

  CTG_RET(code);
}


dengyihao's avatar
dengyihao 已提交
2043 2044
int32_t ctgLaunchGetQnodeTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2045
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2046 2047
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2048 2049 2050
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2051

D
dapan1121 已提交
2052
  CTG_CACHE_NHIT_INC(CTG_CI_QNODE, 1);
D
dapan1121 已提交
2053
  CTG_ERR_RET(ctgGetQnodeListFromMnode(pCtg, pConn, NULL, pTask));
D
dapan1121 已提交
2054 2055 2056
  return TSDB_CODE_SUCCESS;
}

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

D
dapan1121 已提交
2066
  CTG_CACHE_NHIT_INC(CTG_CI_DNODE, 1);
D
dapan1121 已提交
2067 2068 2069 2070
  CTG_ERR_RET(ctgGetDnodeListFromMnode(pCtg, pConn, NULL, pTask));
  return TSDB_CODE_SUCCESS;
}

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

D
dapan1121 已提交
2081 2082
  CTG_CACHE_NHIT_INC(CTG_CI_DB_CFG, 1);

D
dapan1121 已提交
2083
  CTG_ERR_RET(ctgGetDBCfgFromMnode(pCtg, pConn, pCtx->dbFName, NULL, pTask));
D
dapan1121 已提交
2084 2085 2086 2087

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2088 2089 2090 2091
int32_t ctgLaunchGetDbInfoTask(SCtgTask* pTask) {
  int32_t        code = 0;
  SCatalog*      pCtg = pTask->pJob->pCtg;
  SCtgDBCache*   dbCache = NULL;
D
dapan1121 已提交
2092
  SCtgDbInfoCtx* pCtx = (SCtgDbInfoCtx*)pTask->taskCtx;
dengyihao's avatar
dengyihao 已提交
2093 2094
  SCtgJob*       pJob = pTask->pJob;
  SCtgMsgCtx*    pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2095 2096 2097
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2098 2099 2100 2101 2102 2103 2104 2105 2106

  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 已提交
2107
    pInfo->vgVer = dbCache->vgCache.vgInfo->vgVersion;
D
dapan1121 已提交
2108
    pInfo->dbId = dbCache->dbId;
D
dapan1121 已提交
2109
    pInfo->tbNum = dbCache->vgCache.vgInfo->numOfTable;
D
dapan1121 已提交
2110
    pInfo->stateTs = dbCache->vgCache.vgInfo->stateTs;
D
dapan1121 已提交
2111

D
dapan1121 已提交
2112 2113
    CTG_CACHE_HIT_INC(CTG_CI_DB_INFO, 1);

D
dapan1121 已提交
2114 2115
    ctgReleaseVgInfoToCache(pCtg, dbCache);
    dbCache = NULL;
D
dapan1121 已提交
2116 2117
  } else {
    pInfo->vgVer = CTG_DEFAULT_INVALID_VERSION;
D
dapan1121 已提交
2118 2119

    CTG_CACHE_NHIT_INC(CTG_CI_DB_INFO, 1);
D
dapan1121 已提交
2120 2121 2122 2123 2124 2125 2126 2127 2128
  }

  CTG_ERR_JRET(ctgHandleTaskEnd(pTask, 0));

_return:

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
2129 2130
int32_t ctgLaunchGetIndexTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2131
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2132 2133 2134
  SCtgIndexCtx*     pCtx = (SCtgIndexCtx*)pTask->taskCtx;
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2135 2136 2137
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2138

D
dapan1121 已提交
2139 2140
  CTG_CACHE_NHIT_INC(CTG_CI_INDEX_INFO, 1);

D
dapan1121 已提交
2141
  CTG_ERR_RET(ctgGetIndexInfoFromMnode(pCtg, pConn, pCtx->indexFName, NULL, pTask));
D
dapan1121 已提交
2142 2143 2144 2145

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2146 2147
int32_t ctgLaunchGetUdfTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2148
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2149 2150 2151
  SCtgUdfCtx*       pCtx = (SCtgUdfCtx*)pTask->taskCtx;
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2152 2153 2154
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2155

D
dapan1121 已提交
2156 2157
  CTG_CACHE_NHIT_INC(CTG_CI_UDF, 1);

D
dapan1121 已提交
2158
  CTG_ERR_RET(ctgGetUdfInfoFromMnode(pCtg, pConn, pCtx->udfName, NULL, pTask));
D
dapan1121 已提交
2159 2160 2161 2162

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2163
int32_t ctgLaunchGetUserTask(SCtgTask* pTask) {
D
dapan1121 已提交
2164
  int32_t           code = 0;
dengyihao's avatar
dengyihao 已提交
2165
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2166
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2167 2168
  SCtgUserCtx*      pCtx = (SCtgUserCtx*)pTask->taskCtx;
  bool              inCache = false;
D
dapan1121 已提交
2169
  SCtgAuthRsp       rsp = {0};
dengyihao's avatar
dengyihao 已提交
2170 2171
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2172 2173 2174
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
2175

D
dapan1121 已提交
2176 2177 2178 2179 2180 2181
  rsp.pRawRes = taosMemoryCalloc(1, sizeof(SUserAuthRes));
  if (NULL == rsp.pRawRes) {
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }
  
  CTG_ERR_RET(ctgChkAuthFromCache(pCtg, &pCtx->user, &inCache, &rsp));
D
dapan1121 已提交
2182
  if (inCache) {
D
dapan1121 已提交
2183
    pTask->res = rsp.pRawRes;
2184

D
dapan1121 已提交
2185 2186 2187 2188
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
2189
  taosMemoryFreeClear(rsp.pRawRes);
D
dapan1121 已提交
2190

D
dapan1121 已提交
2191 2192 2193 2194 2195 2196
  if (rsp.metaNotExists) {
    CTG_ERR_RET(ctgLaunchSubTask(pTask, CTG_TASK_GET_TB_META, ctgGetTbCfgCb, &pCtx->user.tbName));
  } else {
    CTG_ERR_RET(ctgGetUserDbAuthFromMnode(pCtg, pConn, pCtx->user.user, NULL, pTask));
  }
  
D
dapan1121 已提交
2197 2198 2199
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2200 2201
int32_t ctgLaunchGetSvrVerTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2202
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2203 2204
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2205 2206 2207
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2208

D
dapan1121 已提交
2209 2210
  CTG_CACHE_NHIT_INC(CTG_CI_SVR_VER, 1);

D
dapan1121 已提交
2211 2212 2213 2214 2215
  CTG_ERR_RET(ctgGetSvrVerFromMnode(pCtg, pConn, NULL, pTask));

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2216
int32_t ctgRelaunchGetTbMetaTask(SCtgTask* pTask) {
D
dapan1121 已提交
2217 2218 2219 2220 2221 2222 2223
  ctgResetTbMetaTask(pTask);

  CTG_ERR_RET(ctgLaunchGetTbMetaTask(pTask));

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2224
int32_t ctgGetTbCfgCb(SCtgTask* pTask) {
D
dapan1121 已提交
2225
  int32_t code = 0;
2226

D
dapan1121 已提交
2227 2228 2229 2230 2231 2232 2233
  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;
2234

D
dapan1121 已提交
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245
    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));
}

2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264
int32_t ctgGetTbTagCb(SCtgTask* pTask) {
  int32_t code = 0;

  CTG_ERR_JRET(pTask->subRes.code);

  SCtgTbTagCtx* pCtx = (SCtgTbTagCtx*)pTask->taskCtx;
  SDBVgInfo* pDb = (SDBVgInfo*)pTask->subRes.res;

  pCtx->pVgInfo = taosMemoryCalloc(1, sizeof(SVgroupInfo));
  CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pTask->pJob->pCtg, pDb, pCtx->pName, pCtx->pVgInfo));

  CTG_RET(ctgLaunchGetTbTagTask(pTask));

_return:

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


D
dapan1121 已提交
2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278

int32_t ctgGetUserCb(SCtgTask* pTask) {
  int32_t code = 0;

  CTG_ERR_JRET(pTask->subRes.code);

  CTG_RET(ctgLaunchGetUserTask(pTask));

_return:

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


D
dapan1121 已提交
2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
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 已提交
2307
SCtgAsyncFps gCtgAsyncFps[] = {
dengyihao's avatar
dengyihao 已提交
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323
    {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},
2324
    {ctgInitGetTbTagTask, ctgLaunchGetTbTagTask, ctgHandleGetTbTagRsp, ctgDumpTbTagRes, NULL, NULL},
D
dapan1121 已提交
2325 2326
};

dengyihao's avatar
dengyihao 已提交
2327
int32_t ctgMakeAsyncRes(SCtgJob* pJob) {
D
dapan1121 已提交
2328 2329
  int32_t code = 0;
  int32_t taskNum = taosArrayGetSize(pJob->pTasks);
2330

D
dapan1121 已提交
2331
  for (int32_t i = 0; i < taskNum; ++i) {
dengyihao's avatar
dengyihao 已提交
2332
    SCtgTask* pTask = taosArrayGet(pJob->pTasks, i);
D
dapan1121 已提交
2333 2334 2335 2336 2337 2338
    CTG_ERR_RET((*gCtgAsyncFps[pTask->type].dumpResFp)(pTask));
  }

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2339
int32_t ctgSearchExistingTask(SCtgJob* pJob, CTG_TASK_TYPE type, void* param, int32_t* taskId) {
D
dapan1121 已提交
2340 2341 2342
  bool      equal = false;
  SCtgTask* pTask = NULL;
  int32_t   code = 0;
2343

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

D
dapan1121 已提交
2346 2347 2348
  int32_t taskNum = taosArrayGetSize(pJob->pTasks);
  for (int32_t i = 0; i < taskNum; ++i) {
    pTask = taosArrayGet(pJob->pTasks, i);
dengyihao's avatar
dengyihao 已提交
2349
    if (type != pTask->type) {
D
dapan1121 已提交
2350 2351
      continue;
    }
2352

D
dapan1121 已提交
2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368
    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 已提交
2369
int32_t ctgSetSubTaskCb(SCtgTask* pSub, SCtgTask* pTask) {
D
dapan1121 已提交
2370
  int32_t code = 0;
2371

D
dapan1121 已提交
2372 2373 2374 2375
  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 已提交
2376 2377
    SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
    SCtgMsgCtx* pSubMsgCtx = CTG_GET_TASK_MSGCTX(pSub, -1);
D
dapan1121 已提交
2378
    pMsgCtx->pBatchs = pSubMsgCtx->pBatchs;
2379

D
dapan1121 已提交
2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
    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);

2393
  CTG_RET(code);
D
dapan1121 已提交
2394 2395
}

dengyihao's avatar
dengyihao 已提交
2396
int32_t ctgLaunchSubTask(SCtgTask* pTask, CTG_TASK_TYPE type, ctgSubTaskCbFp fp, void* param) {
D
dapan1121 已提交
2397 2398 2399 2400 2401 2402 2403
  SCtgJob* pJob = pTask->pJob;
  int32_t  subTaskId = -1;
  bool     newTask = false;

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

D
dapan1121 已提交
2405 2406 2407 2408 2409
  CTG_ERR_RET(ctgSearchExistingTask(pJob, type, param, &subTaskId));
  if (subTaskId < 0) {
    CTG_ERR_RET(ctgInitTask(pJob, type, param, &subTaskId));
    newTask = true;
  }
2410

D
dapan1121 已提交
2411 2412 2413 2414 2415
  SCtgTask* pSub = taosArrayGet(pJob->pTasks, subTaskId);

  CTG_ERR_RET(ctgSetSubTaskCb(pSub, pTask));

  if (newTask) {
dengyihao's avatar
dengyihao 已提交
2416 2417
    SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
    SCtgMsgCtx* pSubMsgCtx = CTG_GET_TASK_MSGCTX(pSub, -1);
D
dapan1121 已提交
2418 2419
    pSubMsgCtx->pBatchs = pMsgCtx->pBatchs;

D
dapan1121 已提交
2420 2421 2422 2423 2424 2425
    CTG_ERR_RET((*gCtgAsyncFps[pSub->type].launchFp)(pSub));
    pSub->status = CTG_TASK_LAUNCHED;
  }

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
2426

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

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

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

D
dapan1121 已提交
2436
    pTask->status = CTG_TASK_LAUNCHED;
D
dapan1121 已提交
2437 2438
  }

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

D
dapan1121 已提交
2442
    taosAsyncExec(ctgCallUserCb, pJob, NULL);
2443
#if CTG_BATCH_FETCH
D
dapan1121 已提交
2444 2445 2446
  } else {
    ctgLaunchBatchs(pJob->pCtg, pJob, pJob->pBatchs);
#endif
D
dapan1121 已提交
2447 2448
  }

D
dapan1121 已提交
2449 2450
  return TSDB_CODE_SUCCESS;
}