ctgAsync.c 74.9 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
int32_t ctgInitGetTbMetaTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
24 25
  SCtgTbMetaParam* pParam = (SCtgTbMetaParam*)param;
  SName*   name = pParam->pName;
D
dapan1121 已提交
26
  SCtgTask task = {0};
D
dapan1121 已提交
27

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

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

  taosArrayPush(pJob->pTasks, &task);

74 75
  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 已提交
76 77 78 79

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
130 131
int32_t ctgInitGetDbInfoTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
  char*    dbFName = (char*)param;
D
dapan1121 已提交
132 133 134 135 136 137 138 139 140 141 142 143
  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;
144

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

  taosArrayPush(pJob->pTasks, &task);

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

  taosArrayPush(pJob->pTasks, &task);

205 206
  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);
207 208 209 210

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

  taosArrayPush(pJob->pTasks, &task);

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

  taosArrayPush(pJob->pTasks, &task);

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

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
330 331
int32_t ctgInitGetTbIndexTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
  SName*   name = (SName*)param;
D
dapan1121 已提交
332 333
  SCtgTask task = {0};

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

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

  taosArrayPush(pJob->pTasks, &task);

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

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
360 361
int32_t ctgInitGetTbCfgTask(SCtgJob* pJob, int32_t taskIdx, void* param) {
  SName*   name = (SName*)param;
D
dapan1121 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
  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);
  }
379

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

  taosArrayPush(pJob->pTasks, &task);

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

  return TSDB_CODE_SUCCESS;
}

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 420
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 已提交
421
int32_t ctgHandleForceUpdate(SCatalog* pCtg, int32_t taskNum, SCtgJob* pJob, const SCatalogReq* pReq) {
D
dapan1121 已提交
422
  SHashObj* pDb = taosHashInit(taskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
D
dapan1121 已提交
423 424 425 426
  SHashObj* pTb = taosHashInit(taskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  if (NULL == pDb || NULL == pTb) {
    taosHashCleanup(pDb);
    taosHashCleanup(pTb);
D
dapan1121 已提交
427 428
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }
429

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

D
dapan1121 已提交
714 715
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pTableMeta, &res);
D
dapan1121 已提交
716 717 718 719

  return TSDB_CODE_SUCCESS;
}

720
int32_t ctgDumpTbMetasRes(SCtgTask* pTask) {
D
dapan1121 已提交
721 722 723 724
  if (pTask->subTask) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
725 726 727 728 729 730 731
  SCtgJob* pJob = pTask->pJob;

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

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
732
int32_t ctgDumpDbVgRes(SCtgTask* pTask) {
D
dapan1121 已提交
733 734 735 736
  if (pTask->subTask) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
737 738
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pDbVgroup) {
D
dapan1121 已提交
739
    pJob->jobRes.pDbVgroup = taosArrayInit(pJob->dbVgNum, sizeof(SMetaRes));
D
dapan1121 已提交
740 741 742 743 744
    if (NULL == pJob->jobRes.pDbVgroup) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

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

  return TSDB_CODE_SUCCESS;
}

int32_t ctgDumpTbHashRes(SCtgTask* pTask) {
D
dapan1121 已提交
752 753 754 755
  if (pTask->subTask) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
756 757
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pTableHash) {
D
dapan1121 已提交
758
    pJob->jobRes.pTableHash = taosArrayInit(pJob->tbHashNum, sizeof(SMetaRes));
D
dapan1121 已提交
759 760 761 762 763
    if (NULL == pJob->jobRes.pTableHash) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
764 765
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pTableHash, &res);
D
dapan1121 已提交
766 767 768 769

  return TSDB_CODE_SUCCESS;
}

770
int32_t ctgDumpTbHashsRes(SCtgTask* pTask) {
D
dapan1121 已提交
771 772 773 774
  if (pTask->subTask) {
    return TSDB_CODE_SUCCESS;
  }

775 776 777 778 779 780 781
  SCtgJob* pJob = pTask->pJob;

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

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
782
int32_t ctgDumpTbIndexRes(SCtgTask* pTask) {
D
dapan1121 已提交
783 784 785 786
  if (pTask->subTask) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
787 788
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pTableIndex) {
789 790 791 792 793
    SArray* pRes = taosArrayInit(pJob->tbIndexNum, sizeof(SMetaRes));
    if (atomic_val_compare_exchange_ptr(&pJob->jobRes.pTableIndex, NULL, pRes)) {
      taosArrayDestroy(pRes);
    }

D
dapan1121 已提交
794 795 796 797 798 799
    if (NULL == pJob->jobRes.pTableIndex) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

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

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
805
int32_t ctgDumpTbCfgRes(SCtgTask* pTask) {
D
dapan1121 已提交
806 807 808 809
  if (pTask->subTask) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
810 811
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pTableCfg) {
812 813 814 815 816
    SArray* pRes = taosArrayInit(pJob->tbCfgNum, sizeof(SMetaRes));
    if (atomic_val_compare_exchange_ptr(&pJob->jobRes.pTableCfg, NULL, pRes)) {
      taosArrayDestroy(pRes);
    }

D
dapan1121 已提交
817 818 819 820 821 822 823 824 825 826 827
    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;
}

828
int32_t ctgDumpTbTagRes(SCtgTask* pTask) {
D
dapan1121 已提交
829 830 831 832
  if (pTask->subTask) {
    return TSDB_CODE_SUCCESS;
  }

833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pTableTag) {
    SArray* pRes = taosArrayInit(pJob->tbTagNum, sizeof(SMetaRes));
    if (atomic_val_compare_exchange_ptr(&pJob->jobRes.pTableTag, NULL, pRes)) {
      taosArrayDestroy(pRes);
    }
    
    if (NULL == pJob->jobRes.pTableTag) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

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

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
852
int32_t ctgDumpIndexRes(SCtgTask* pTask) {
D
dapan1121 已提交
853 854 855 856
  if (pTask->subTask) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
857 858
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pIndex) {
D
dapan1121 已提交
859
    pJob->jobRes.pIndex = taosArrayInit(pJob->indexNum, sizeof(SMetaRes));
D
dapan1121 已提交
860 861 862 863 864
    if (NULL == pJob->jobRes.pIndex) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
865 866
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pIndex, &res);
D
dapan1121 已提交
867 868 869 870 871

  return TSDB_CODE_SUCCESS;
}

int32_t ctgDumpQnodeRes(SCtgTask* pTask) {
D
dapan1121 已提交
872 873 874 875
  if (pTask->subTask) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
876
  SCtgJob* pJob = pTask->pJob;
D
dapan1121 已提交
877 878 879 880 881 882
  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 已提交
883

D
dapan1121 已提交
884 885
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pQnodeList, &res);
D
dapan1121 已提交
886 887 888 889

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
890
int32_t ctgDumpDnodeRes(SCtgTask* pTask) {
D
dapan1121 已提交
891 892 893 894
  if (pTask->subTask) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
895 896 897 898 899 900 901 902 903 904 905 906 907 908
  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 已提交
909
int32_t ctgDumpDbCfgRes(SCtgTask* pTask) {
D
dapan1121 已提交
910 911 912 913
  if (pTask->subTask) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
914 915
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pDbCfg) {
D
dapan1121 已提交
916
    pJob->jobRes.pDbCfg = taosArrayInit(pJob->dbCfgNum, sizeof(SMetaRes));
D
dapan1121 已提交
917 918 919 920 921
    if (NULL == pJob->jobRes.pDbCfg) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
922 923
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pDbCfg, &res);
D
dapan1121 已提交
924 925 926 927

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
928
int32_t ctgDumpDbInfoRes(SCtgTask* pTask) {
D
dapan1121 已提交
929 930 931 932
  if (pTask->subTask) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
933 934
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pDbInfo) {
D
dapan1121 已提交
935
    pJob->jobRes.pDbInfo = taosArrayInit(pJob->dbInfoNum, sizeof(SMetaRes));
D
dapan1121 已提交
936 937 938 939 940
    if (NULL == pJob->jobRes.pDbInfo) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
941 942
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pDbInfo, &res);
D
dapan1121 已提交
943 944 945 946

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
947
int32_t ctgDumpUdfRes(SCtgTask* pTask) {
D
dapan1121 已提交
948 949 950 951
  if (pTask->subTask) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
952 953
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pUdfList) {
D
dapan1121 已提交
954
    pJob->jobRes.pUdfList = taosArrayInit(pJob->udfNum, sizeof(SMetaRes));
D
dapan1121 已提交
955 956 957 958 959
    if (NULL == pJob->jobRes.pUdfList) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
960 961
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pUdfList, &res);
D
dapan1121 已提交
962 963 964 965 966

  return TSDB_CODE_SUCCESS;
}

int32_t ctgDumpUserRes(SCtgTask* pTask) {
D
dapan1121 已提交
967 968 969 970
  if (pTask->subTask) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
971 972
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pUser) {
D
dapan1121 已提交
973
    pJob->jobRes.pUser = taosArrayInit(pJob->userNum, sizeof(SMetaRes));
D
dapan1121 已提交
974 975 976 977 978
    if (NULL == pJob->jobRes.pUser) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
979 980
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pUser, &res);
D
dapan1121 已提交
981 982 983 984

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
985
int32_t ctgDumpSvrVer(SCtgTask* pTask) {
D
dapan1121 已提交
986 987 988 989
  if (pTask->subTask) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
990 991 992 993 994 995 996 997 998 999
  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;
1000

D
dapan1121 已提交
1001 1002 1003
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1004
int32_t ctgCallSubCb(SCtgTask* pTask) {
D
dapan1121 已提交
1005
  int32_t code = 0;
1006

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

D
dapan1121 已提交
1009 1010
  int32_t parentNum = taosArrayGetSize(pTask->pParents);
  for (int32_t i = 0; i < parentNum; ++i) {
dengyihao's avatar
dengyihao 已提交
1011 1012
    SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
    SCtgTask*   pParent = taosArrayGetP(pTask->pParents, i);
1013

D
dapan1121 已提交
1014 1015 1016 1017 1018 1019 1020
    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;
      }
    }
1021

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

1024
    pParMsgCtx->pBatchs = pMsgCtx->pBatchs;
D
dapan1121 已提交
1025 1026
    CTG_ERR_JRET(pParent->subRes.fp(pParent));
  }
1027

D
dapan1121 已提交
1028 1029 1030 1031
_return:

  CTG_UNLOCK(CTG_WRITE, &pTask->lock);

1032
  CTG_RET(code);
D
dapan1121 已提交
1033 1034
}

D
dapan1121 已提交
1035 1036
int32_t ctgCallUserCb(void* param) {
  SCtgJob* pJob = (SCtgJob*)param;
D
dapan1121 已提交
1037 1038

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

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

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

D
dapan1121 已提交
1044 1045 1046 1047
  taosRemoveRef(gCtgMgmt.jobPool, pJob->refId);

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
1048

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

dengyihao's avatar
dengyihao 已提交
1052
  atomic_store_32(&pJob->jobResCode, errCode);
dengyihao's avatar
dengyihao 已提交
1053
  qDebug("QID:0x%" PRIx64 " ctg job errCode updated to %s", pJob->queryId, tstrerror(errCode));
dengyihao's avatar
dengyihao 已提交
1054
  return;
dengyihao's avatar
dengyihao 已提交
1055 1056
}

D
dapan1121 已提交
1057 1058
int32_t ctgHandleTaskEnd(SCtgTask* pTask, int32_t rspCode) {
  SCtgJob* pJob = pTask->pJob;
dengyihao's avatar
dengyihao 已提交
1059
  int32_t  code = 0;
D
dapan1121 已提交
1060

D
dapan1121 已提交
1061 1062 1063 1064
  if (CTG_TASK_DONE == pTask->status) {
    return TSDB_CODE_SUCCESS;
  }

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

D
dapan1121 已提交
1067
  pTask->code = rspCode;
D
dapan1121 已提交
1068 1069
  pTask->status = CTG_TASK_DONE;

D
dapan1121 已提交
1070
  ctgCallSubCb(pTask);
D
dapan1121 已提交
1071 1072 1073

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

    ctgUpdateJobErrCode(pJob, rspCode);
D
dapan1121 已提交
1078 1079 1080 1081 1082 1083 1084
    return TSDB_CODE_SUCCESS;
  }

  CTG_ERR_JRET(ctgMakeAsyncRes(pJob));

_return:

dengyihao's avatar
dengyihao 已提交
1085 1086
  ctgUpdateJobErrCode(pJob, rspCode);
  // pJob->jobResCode = code;
D
dapan1121 已提交
1087

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

D
dapan1121 已提交
1091
  taosAsyncExec(ctgCallUserCb, pJob, NULL);
1092

D
dapan1121 已提交
1093 1094 1095
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1096 1097 1098 1099 1100
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 已提交
1101
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1102 1103 1104 1105 1106
  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 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119

  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 已提交
1120
      CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, pName, &vgInfo, NULL, tReq));
D
dapan1121 已提交
1121 1122 1123 1124 1125

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

D
dapan1121 已提交
1127 1128 1129 1130
      if (CTG_IS_META_NULL(pOut->metaType)) {
        if (CTG_FLAG_IS_STB(flag)) {
          char dbFName[TSDB_DB_FNAME_LEN] = {0};
          tNameGetFullDbName(pName, dbFName);
1131

D
dapan1121 已提交
1132 1133 1134 1135
          CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache));
          if (NULL != dbCache) {
            SVgroupInfo vgInfo = {0};
            CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, dbCache->vgCache.vgInfo, pName, &vgInfo));
1136

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

1139
            *vgId = vgInfo.vgId;
D
dapan1121 已提交
1140
            CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, pName, &vgInfo, NULL, tReq));
D
dapan1121 已提交
1141 1142

            ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1143
            dbCache = NULL;
D
dapan1121 已提交
1144 1145
          } else {
            SBuildUseDBInput input = {0};
1146

D
dapan1121 已提交
1147 1148
            tstrncpy(input.db, dbFName, tListLen(input.db));
            input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
1149

D
dapan1121 已提交
1150
            CTG_ERR_JRET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, tReq));
D
dapan1121 已提交
1151 1152 1153 1154
          }

          return TSDB_CODE_SUCCESS;
        }
1155

D
dapan1121 已提交
1156 1157
        ctgError("no tbmeta got, tbName:%s", tNameGetTableName(pName));
        ctgRemoveTbMetaFromCache(pCtg, pName, false);
1158

D
dapan1121 已提交
1159 1160 1161 1162 1163 1164 1165 1166
        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);
      }
1167

D
dapan1121 已提交
1168 1169 1170 1171
      break;
    }
    case TDMT_VND_TABLE_META: {
      STableMetaOutput* pOut = (STableMetaOutput*)pMsgCtx->out;
1172

D
dapan1121 已提交
1173 1174 1175 1176 1177 1178 1179 1180 1181
      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;
      }
1182

D
dapan1121 已提交
1183 1184
      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));
1185

D
dapan1121 已提交
1186
        taosMemoryFreeClear(pOut->tbMeta);
1187

D
dapan1121 已提交
1188
        CTG_RET(ctgGetTbMetaFromMnode(pCtg, pConn, pName, NULL, tReq));
D
dapan1121 已提交
1189 1190 1191 1192 1193 1194 1195 1196 1197
      } 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;

1198
          taosMemoryFreeClear(pOut->tbMeta);
D
dapan1121 已提交
1199 1200 1201 1202 1203
          CTG_ERR_JRET(ctgReadTbMetaFromCache(pCtg, &stbCtx, &pOut->tbMeta));
          if (pOut->tbMeta) {
            exist = 1;
          }
        }
1204

D
dapan1121 已提交
1205 1206
        if (0 == exist) {
          TSWAP(pMsgCtx->lastOut, pMsgCtx->out);
D
dapan1121 已提交
1207
          CTG_RET(ctgGetTbMetaFromMnodeImpl(pCtg, pConn, pOut->dbFName, pOut->tbName, NULL, tReq));
D
dapan1121 已提交
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
        }
      }
      break;
    }
    default:
      ctgError("invalid reqType %d", reqType);
      CTG_ERR_JRET(TSDB_CODE_INVALID_MSG);
  }

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

1219
  ctgUpdateTbMetaToCache(pCtg, pOut, flag & CTG_FLAG_SYNC_OP);
D
dapan1121 已提交
1220 1221 1222 1223

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

dengyihao's avatar
dengyihao 已提交
1225 1226 1227 1228 1229 1230 1231
  /*
    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 已提交
1232

dengyihao's avatar
dengyihao 已提交
1233 1234 1235 1236
      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 已提交
1237

dengyihao's avatar
dengyihao 已提交
1238 1239 1240 1241 1242 1243
        return TSDB_CODE_SUCCESS;
      }

      memcpy(pOut->tbMeta, &pOut->ctbMeta, sizeof(pOut->ctbMeta));
    }
  */
D
dapan1121 已提交
1244 1245 1246 1247 1248 1249 1250 1251 1252

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

_return:

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

1253
  if (code) {
dengyihao's avatar
dengyihao 已提交
1254 1255
    ctgTaskError("Get table %d.%s.%s meta failed with error %s", pName->acctId, pName->dbname, pName->tname,
                 tstrerror(code));
1256
  }
1257
  if (pTask->res || code) {
D
dapan1121 已提交
1258 1259
    ctgHandleTaskEnd(pTask, code);
  }
1260

D
dapan1121 已提交
1261 1262 1263
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1264 1265 1266 1267 1268
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 已提交
1269
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1270 1271 1272 1273 1274 1275 1276
  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 已提交
1277

D
dapan1121 已提交
1278
  CTG_ERR_JRET(ctgProcessRspMsg(pMsgCtx->out, reqType, pMsg->pData, pMsg->len, rspCode, pMsgCtx->target));
D
dapan1121 已提交
1279 1280 1281

  switch (reqType) {
    case TDMT_MND_USE_DB: {
D
dapan1121 已提交
1282
      SUseDbOutput* pOut = (SUseDbOutput*)pMsgCtx->out;
D
dapan1121 已提交
1283 1284

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

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

D
dapan1121 已提交
1289
      *vgId = vgInfo.vgId;
D
dapan1121 已提交
1290
      CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, pName, &vgInfo, NULL, tReq));
D
dapan1121 已提交
1291 1292 1293 1294

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

D
dapan1121 已提交
1297
      if (CTG_IS_META_NULL(pOut->metaType)) {
D
dapan1121 已提交
1298
        if (CTG_FLAG_IS_STB(flag)) {
D
dapan1121 已提交
1299
          char dbFName[TSDB_DB_FNAME_LEN] = {0};
D
dapan1121 已提交
1300
          tNameGetFullDbName(pName, dbFName);
1301

D
dapan1121 已提交
1302 1303 1304
          CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache));
          if (NULL != dbCache) {
            SVgroupInfo vgInfo = {0};
D
dapan1121 已提交
1305
            CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, dbCache->vgCache.vgInfo, pName, &vgInfo));
1306

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

1309
            *vgId = vgInfo.vgId;
D
dapan1121 已提交
1310
            CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, pName, &vgInfo, NULL, tReq));
D
dapan1121 已提交
1311

D
dapan1121 已提交
1312
            ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1313
            dbCache = NULL;
D
dapan1121 已提交
1314 1315
          } else {
            SBuildUseDBInput input = {0};
1316

D
dapan1121 已提交
1317 1318
            tstrncpy(input.db, dbFName, tListLen(input.db));
            input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
1319

D
dapan1121 已提交
1320
            CTG_ERR_JRET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, tReq));
D
dapan1121 已提交
1321 1322 1323 1324
          }

          return TSDB_CODE_SUCCESS;
        }
1325

1326
        ctgTaskError("no tbmeta got, tbName:%s", tNameGetTableName(pName));
D
dapan1121 已提交
1327
        ctgRemoveTbMetaFromCache(pCtg, pName, false);
1328

D
dapan1121 已提交
1329 1330 1331
        CTG_ERR_JRET(CTG_ERR_CODE_TABLE_NOT_EXIST);
      }

D
dapan1121 已提交
1332 1333 1334
      if (pMsgCtx->lastOut) {
        TSWAP(pMsgCtx->out, pMsgCtx->lastOut);
        STableMetaOutput* pLastOut = (STableMetaOutput*)pMsgCtx->out;
D
dapan1121 已提交
1335 1336
        TSWAP(pLastOut->tbMeta, pOut->tbMeta);
      }
1337

D
dapan1121 已提交
1338 1339 1340
      break;
    }
    case TDMT_VND_TABLE_META: {
D
dapan1121 已提交
1341
      STableMetaOutput* pOut = (STableMetaOutput*)pMsgCtx->out;
1342

D
dapan1121 已提交
1343
      if (CTG_IS_META_NULL(pOut->metaType)) {
1344
        ctgTaskError("no tbmeta got, tbNmae:%s", tNameGetTableName(pName));
D
dapan1121 已提交
1345
        ctgRemoveTbMetaFromCache(pCtg, pName, false);
D
dapan1121 已提交
1346 1347 1348
        CTG_ERR_JRET(CTG_ERR_CODE_TABLE_NOT_EXIST);
      }

D
dapan1121 已提交
1349
      if (CTG_FLAG_IS_STB(flag)) {
D
dapan1121 已提交
1350 1351
        break;
      }
1352

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

D
dapan1121 已提交
1356
        taosMemoryFreeClear(pOut->tbMeta);
1357

D
dapan1121 已提交
1358
        CTG_RET(ctgGetTbMetaFromMnode(pCtg, pConn, pName, NULL, tReq));
D
dapan1121 已提交
1359 1360
      } else if (CTG_IS_META_BOTH(pOut->metaType)) {
        int32_t exist = 0;
D
dapan1121 已提交
1361 1362 1363 1364 1365 1366 1367
        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 已提交
1368
          STableMeta* stbMeta = NULL;
1369
          (void)ctgReadTbMetaFromCache(pCtg, &stbCtx, &stbMeta);
D
dapan1121 已提交
1370
          if (stbMeta && stbMeta->sversion >= pOut->tbMeta->sversion) {
1371
            ctgTaskDebug("use cached stb meta, tbName:%s", tNameGetTableName(pName));
D
dapan1121 已提交
1372
            exist = 1;
dengyihao's avatar
dengyihao 已提交
1373
            taosMemoryFreeClear(stbMeta);
D
dapan1121 已提交
1374
          } else {
1375
            ctgTaskDebug("need to get/update stb meta, tbName:%s", tNameGetTableName(pName));
D
dapan1121 已提交
1376 1377
            taosMemoryFreeClear(pOut->tbMeta);
            taosMemoryFreeClear(stbMeta);
D
dapan1121 已提交
1378
          }
D
dapan1121 已提交
1379
        }
1380

D
dapan1121 已提交
1381
        if (0 == exist) {
D
dapan1121 已提交
1382
          TSWAP(pMsgCtx->lastOut, pMsgCtx->out);
D
dapan1121 已提交
1383
          CTG_RET(ctgGetTbMetaFromMnodeImpl(pCtg, pConn, pOut->dbFName, pOut->tbName, NULL, tReq));
D
dapan1121 已提交
1384 1385 1386 1387 1388
        }
      }
      break;
    }
    default:
1389
      ctgTaskError("invalid reqType %d", reqType);
D
dapan1121 已提交
1390 1391 1392
      CTG_ERR_JRET(TSDB_CODE_INVALID_MSG);
  }

D
dapan1121 已提交
1393
  STableMetaOutput* pOut = (STableMetaOutput*)pMsgCtx->out;
D
dapan1121 已提交
1394 1395 1396 1397 1398

  ctgUpdateTbMetaToCache(pCtg, pOut, false);

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

dengyihao's avatar
dengyihao 已提交
1401 1402 1403 1404 1405 1406 1407
  /*
    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 已提交
1408

dengyihao's avatar
dengyihao 已提交
1409 1410 1411 1412
      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 已提交
1413

dengyihao's avatar
dengyihao 已提交
1414 1415 1416 1417 1418 1419
        return TSDB_CODE_SUCCESS;
      }

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

1421
  SMetaRes* pRes = taosArrayGet(ctx->pResList, pFetch->resIdx);
D
dapan1121 已提交
1422 1423 1424 1425
  pRes->code = 0;
  pRes->pRes = pOut->tbMeta;
  pOut->tbMeta = NULL;
  if (0 == atomic_sub_fetch_32(&ctx->fetchNum, 1)) {
1426
    TSWAP(pTask->res, ctx->pResList);
1427
    taskDone = true;
D
dapan1121 已提交
1428
  }
D
dapan1121 已提交
1429 1430 1431 1432

_return:

  if (dbCache) {
D
dapan1121 已提交
1433
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1434 1435
  }

D
dapan1121 已提交
1436
  if (code) {
1437
    SMetaRes* pRes = taosArrayGet(ctx->pResList, pFetch->resIdx);
D
dapan1121 已提交
1438 1439
    pRes->code = code;
    pRes->pRes = NULL;
1440 1441
    ctgTaskError("Get table %d.%s.%s meta failed with error %s", pName->acctId, pName->dbname, pName->tname,
                 tstrerror(code));
D
dapan1121 已提交
1442
    if (0 == atomic_sub_fetch_32(&ctx->fetchNum, 1)) {
1443
      TSWAP(pTask->res, ctx->pResList);
1444
      taskDone = true;
D
dapan1121 已提交
1445 1446 1447
    }
  }

1448
  if (pTask->res && taskDone) {
D
dapan1121 已提交
1449 1450
    ctgHandleTaskEnd(pTask, code);
  }
1451

D
dapan1121 已提交
1452 1453 1454
  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1455 1456 1457
int32_t ctgHandleGetDbVgRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t      code = 0;
  SCtgTask*    pTask = tReq->pTask;
D
dapan1121 已提交
1458
  SCtgDbVgCtx* ctx = (SCtgDbVgCtx*)pTask->taskCtx;
dengyihao's avatar
dengyihao 已提交
1459
  SCatalog*    pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1460 1461

  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));
D
dapan1121 已提交
1462 1463 1464 1465

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

D
dapan1121 已提交
1468 1469 1470 1471
      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 已提交
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486

      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 已提交
1487 1488 1489
int32_t ctgHandleGetTbHashRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t        code = 0;
  SCtgTask*      pTask = tReq->pTask;
D
dapan1121 已提交
1490
  SCtgTbHashCtx* ctx = (SCtgTbHashCtx*)pTask->taskCtx;
dengyihao's avatar
dengyihao 已提交
1491
  SCatalog*      pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1492 1493

  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));
D
dapan1121 已提交
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504

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

D
dapan1121 已提交
1506
      CTG_ERR_JRET(ctgUpdateVgroupEnqueue(pCtg, ctx->dbFName, pOut->dbId, pOut->dbVgroup, false));
D
dapan1121 已提交
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
      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 已提交
1523 1524 1525
int32_t ctgHandleGetTbHashsRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t         code = 0;
  SCtgTask*       pTask = tReq->pTask;
1526
  SCtgTbHashsCtx* ctx = (SCtgTbHashsCtx*)pTask->taskCtx;
dengyihao's avatar
dengyihao 已提交
1527 1528 1529 1530
  SCatalog*       pCtg = pTask->pJob->pCtg;
  SCtgMsgCtx*     pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, tReq->msgIdx);
  SCtgFetch*      pFetch = taosArrayGet(ctx->pFetchs, tReq->msgIdx);
  bool            taskDone = false;
1531 1532 1533 1534 1535 1536 1537

  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;

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

1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
      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);
1553
    taskDone = true;
1554 1555 1556 1557 1558
  }

_return:

  if (code) {
1559
    STablesReq* pReq = taosArrayGet(ctx->pNames, pFetch->dbIdx);
dengyihao's avatar
dengyihao 已提交
1560
    int32_t     num = taosArrayGetSize(pReq->pTables);
1561
    for (int32_t i = 0; i < num; ++i) {
dengyihao's avatar
dengyihao 已提交
1562
      SMetaRes* pRes = taosArrayGet(ctx->pResList, pFetch->resIdx + i);
1563 1564 1565
      pRes->code = code;
      pRes->pRes = NULL;
    }
1566

1567 1568
    if (0 == atomic_sub_fetch_32(&ctx->fetchNum, 1)) {
      TSWAP(pTask->res, ctx->pResList);
1569
      taskDone = true;
1570 1571 1572
    }
  }

1573
  if (pTask->res && taskDone) {
1574 1575 1576 1577 1578 1579
    ctgHandleTaskEnd(pTask, code);
  }

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1580 1581
int32_t ctgHandleGetTbIndexRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1582
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1583
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));
D
dapan1121 已提交
1584

D
dapan1121 已提交
1585
  STableIndex* pOut = (STableIndex*)pTask->msgCtx.out;
dengyihao's avatar
dengyihao 已提交
1586
  SArray*      pInfo = NULL;
D
dapan1121 已提交
1587 1588 1589
  CTG_ERR_JRET(ctgCloneTableIndex(pOut->pIndex, &pInfo));
  pTask->res = pInfo;

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

D
dapan1121 已提交
1593
_return:
D
dapan1121 已提交
1594

X
Xiaoyu Wang 已提交
1595 1596 1597
  if (TSDB_CODE_MND_DB_INDEX_NOT_EXIST == code) {
    code = TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
1598 1599 1600 1601 1602
  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

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

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

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

  ctgHandleTaskEnd(pTask, code);

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

1617 1618 1619 1620

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

  STableCfgRsp* pRsp = (STableCfgRsp*)pTask->msgCtx.out;
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
  if (NULL == pRsp->pTags || pRsp->tagsLen <= 0) {
    ctgError("invalid tag in tbCfg rsp, pTags:%p, len:%d", pRsp->pTags, pRsp->tagsLen);
    CTG_ERR_JRET(TSDB_CODE_INVALID_MSG);
  }

  SArray* pTagVals = NULL;
  STag*   pTag = (STag*)pRsp->pTags;

  if (tTagIsJson(pTag)) {
    pTagVals = taosArrayInit(1, sizeof(STagVal));
    if (NULL == pTagVals) {
      CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
    }

    char* pJson = parseTagDatatoJson(pTag);
    STagVal tagVal;
    tagVal.cid = 0;
    tagVal.type = TSDB_DATA_TYPE_JSON;
    tagVal.pData = pJson;
    tagVal.nData = strlen(pJson);
    taosArrayPush(pTagVals, &tagVal);
  } else {
    CTG_ERR_JRET(tTagToValArray((const STag*)pRsp->pTags, &pTagVals));
  }
1649
  
1650
  pTask->res = pTagVals;
1651 1652 1653 1654 1655 1656 1657 1658 1659

_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}


dengyihao's avatar
dengyihao 已提交
1660 1661
int32_t ctgHandleGetDbCfgRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1662
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1663 1664 1665
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

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

D
dapan1121 已提交
1667 1668 1669 1670 1671 1672 1673
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1674
int32_t ctgHandleGetDbInfoRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
D
dapan1121 已提交
1675 1676 1677
  CTG_RET(TSDB_CODE_APP_ERROR);
}

dengyihao's avatar
dengyihao 已提交
1678 1679
int32_t ctgHandleGetQnodeRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1680
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1681 1682 1683
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

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

D
dapan1121 已提交
1685 1686 1687 1688 1689 1690 1691
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1692 1693
int32_t ctgHandleGetDnodeRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1694
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1695 1696 1697
  CTG_ERR_JRET(ctgProcessRspMsg(&pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

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

D
dapan1121 已提交
1699 1700 1701 1702 1703 1704 1705
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1706 1707
int32_t ctgHandleGetIndexRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1708
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1709 1710 1711
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

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

D
dapan1121 已提交
1713 1714 1715 1716 1717 1718 1719
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1720 1721
int32_t ctgHandleGetUdfRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1722
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1723 1724 1725
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

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

D
dapan1121 已提交
1727 1728 1729 1730 1731 1732 1733
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1734 1735 1736 1737
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 已提交
1738
  SGetUserAuthRsp* pOut = (SGetUserAuthRsp*)pTask->msgCtx.out;
D
dapan1121 已提交
1739 1740 1741

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

D
dapan1121 已提交
1742 1743
  ctgUpdateUserEnqueue(pCtg, pOut, true);
  taosMemoryFreeClear(pTask->msgCtx.out);
D
dapan1121 已提交
1744

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

D
dapan1121 已提交
1747
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1748 1749 1750 1751 1752 1753 1754 1755

_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1756 1757
int32_t ctgHandleGetSvrVerRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* pMsg, int32_t rspCode) {
  int32_t   code = 0;
D
dapan1121 已提交
1758
  SCtgTask* pTask = tReq->pTask;
D
dapan1121 已提交
1759 1760 1761 1762

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

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

D
dapan1121 已提交
1764 1765 1766 1767 1768 1769 1770
_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1771 1772 1773
int32_t ctgAsyncRefreshTbMeta(SCtgTaskReq* tReq, int32_t flag, SName* pName, int32_t* vgId) {
  SCtgTask*         pTask = tReq->pTask;
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1774
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1775
  int32_t           code = 0;
D
dapan1121 已提交
1776

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

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

D
dapan1121 已提交
1783 1784
  if (CTG_FLAG_IS_STB(flag)) {
    ctgDebug("will refresh tbmeta, supposed to be stb, tbName:%s", tNameGetTableName(pName));
D
dapan1121 已提交
1785 1786

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

dengyihao's avatar
dengyihao 已提交
1790 1791
  SCtgDBCache* dbCache = NULL;
  char         dbFName[TSDB_DB_FNAME_LEN] = {0};
D
dapan1121 已提交
1792
  tNameGetFullDbName(pName, dbFName);
1793

D
dapan1121 已提交
1794
  CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache));
D
dapan1121 已提交
1795
  if (dbCache) {
D
dapan1121 已提交
1796
    SVgroupInfo vgInfo = {0};
D
dapan1121 已提交
1797
    CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, dbCache->vgCache.vgInfo, pName, &vgInfo));
D
dapan1121 已提交
1798

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

D
dapan1121 已提交
1801
    *vgId = vgInfo.vgId;
D
dapan1121 已提交
1802
    CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, pName, &vgInfo, NULL, tReq));
D
dapan1121 已提交
1803 1804 1805 1806 1807 1808
  } else {
    SBuildUseDBInput input = {0};

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

D
dapan1121 已提交
1809
    CTG_ERR_JRET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, tReq));
D
dapan1121 已提交
1810 1811 1812 1813 1814
  }

_return:

  if (dbCache) {
D
dapan1121 已提交
1815
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1816 1817 1818 1819 1820
  }

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1821 1822
int32_t ctgLaunchGetTbMetaTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1823
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1824 1825
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
1826 1827 1828
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
1829

D
dapan1121 已提交
1830
  CTG_ERR_RET(ctgGetTbMetaFromCache(pCtg, (SCtgTbMetaCtx*)pTask->taskCtx, (STableMeta**)&pTask->res));
D
dapan1121 已提交
1831 1832 1833 1834 1835
  if (pTask->res) {
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
1836
  SCtgTbMetaCtx* pCtx = (SCtgTbMetaCtx*)pTask->taskCtx;
dengyihao's avatar
dengyihao 已提交
1837
  SCtgTaskReq    tReq;
D
dapan1121 已提交
1838
  tReq.pTask = pTask;
1839
  tReq.msgIdx = -1;
D
dapan1121 已提交
1840
  CTG_ERR_RET(ctgAsyncRefreshTbMeta(&tReq, pCtx->flag, pCtx->pName, &pCtx->vgId));
D
dapan1121 已提交
1841 1842 1843 1844

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1845 1846
int32_t ctgLaunchGetTbMetasTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1847
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1848 1849
  SCtgTbMetasCtx*   pCtx = (SCtgTbMetasCtx*)pTask->taskCtx;
  SCtgJob*          pJob = pTask->pJob;
D
dapan1121 已提交
1850

1851 1852 1853 1854 1855
  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);
1856
    ctgDebug("start to check tb metas in db %s, tbNum %ld", pReq->dbFName, taosArrayGetSize(pReq->pTables));
1857 1858 1859
    CTG_ERR_RET(ctgGetTbMetasFromCache(pCtg, pConn, pCtx, i, &fetchIdx, baseResIdx, pReq->pTables));
    baseResIdx += taosArrayGetSize(pReq->pTables);
  }
1860

1861 1862 1863
  pCtx->fetchNum = taosArrayGetSize(pCtx->pFetchs);
  if (pCtx->fetchNum <= 0) {
    TSWAP(pTask->res, pCtx->pResList);
1864

D
dapan1121 已提交
1865 1866 1867
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }
1868

1869
  pTask->msgCtxs = taosArrayInit_s(sizeof(SCtgMsgCtx), pCtx->fetchNum);
D
dapan1121 已提交
1870
  for (int32_t i = 0; i < pCtx->fetchNum; ++i) {
dengyihao's avatar
dengyihao 已提交
1871 1872 1873
    SCtgFetch*  pFetch = taosArrayGet(pCtx->pFetchs, i);
    SName*      pName = ctgGetFetchName(pCtx->pNames, pFetch);
    SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, i);
D
dapan1121 已提交
1874 1875 1876
    if (NULL == pMsgCtx->pBatchs) {
      pMsgCtx->pBatchs = pJob->pBatchs;
    }
1877

D
dapan1121 已提交
1878 1879
    SCtgTaskReq tReq;
    tReq.pTask = pTask;
1880
    tReq.msgIdx = pFetch->fetchIdx;
D
dapan1121 已提交
1881
    CTG_ERR_RET(ctgAsyncRefreshTbMeta(&tReq, pFetch->flag, pName, &pFetch->vgId));
D
dapan1121 已提交
1882
  }
1883

D
dapan1121 已提交
1884 1885 1886
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1887 1888 1889
int32_t ctgLaunchGetDbVgTask(SCtgTask* pTask) {
  int32_t           code = 0;
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1890
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1891 1892 1893 1894
  SCtgDBCache*      dbCache = NULL;
  SCtgDbVgCtx*      pCtx = (SCtgDbVgCtx*)pTask->taskCtx;
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
1895 1896 1897
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
1898

D
dapan1121 已提交
1899 1900
  CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, pCtx->dbFName, &dbCache));
  if (NULL != dbCache) {
D
dapan1121 已提交
1901
    CTG_ERR_JRET(ctgGenerateVgList(pCtg, dbCache->vgCache.vgInfo->vgHash, (SArray**)&pTask->res));
D
dapan1121 已提交
1902 1903 1904

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

D
dapan1121 已提交
1906 1907 1908
    CTG_ERR_JRET(ctgHandleTaskEnd(pTask, 0));
  } else {
    SBuildUseDBInput input = {0};
1909

D
dapan1121 已提交
1910 1911
    tstrncpy(input.db, pCtx->dbFName, tListLen(input.db));
    input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
D
dapan1121 已提交
1912 1913 1914 1915 1916

    SCtgTaskReq tReq;
    tReq.pTask = pTask;
    tReq.msgIdx = -1;
    CTG_ERR_RET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, &tReq));
D
dapan1121 已提交
1917 1918 1919 1920 1921
  }

_return:

  if (dbCache) {
D
dapan1121 已提交
1922
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1923 1924 1925 1926 1927
  }

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1928 1929 1930
int32_t ctgLaunchGetTbHashTask(SCtgTask* pTask) {
  int32_t           code = 0;
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
1931
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1932 1933 1934 1935
  SCtgDBCache*      dbCache = NULL;
  SCtgTbHashCtx*    pCtx = (SCtgTbHashCtx*)pTask->taskCtx;
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
1936 1937 1938
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
1939

D
dapan1121 已提交
1940 1941 1942 1943 1944 1945
  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 已提交
1946
    CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, dbCache->vgCache.vgInfo, pCtx->pName, (SVgroupInfo*)pTask->res));
D
dapan1121 已提交
1947 1948 1949

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

D
dapan1121 已提交
1951 1952 1953
    CTG_ERR_JRET(ctgHandleTaskEnd(pTask, 0));
  } else {
    SBuildUseDBInput input = {0};
1954

D
dapan1121 已提交
1955 1956
    tstrncpy(input.db, pCtx->dbFName, tListLen(input.db));
    input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
D
dapan1121 已提交
1957 1958 1959

    SCtgTaskReq tReq;
    tReq.pTask = pTask;
1960
    tReq.msgIdx = -1;
D
dapan1121 已提交
1961
    CTG_ERR_RET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, &tReq));
D
dapan1121 已提交
1962 1963 1964 1965 1966
  }

_return:

  if (dbCache) {
D
dapan1121 已提交
1967
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1968 1969 1970 1971 1972
  }

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
1973 1974
int32_t ctgLaunchGetTbHashsTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
1975
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
1976 1977 1978 1979 1980 1981 1982
  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;
1983

1984 1985
  for (int32_t i = 0; i < dbNum; ++i) {
    STablesReq* pReq = taosArrayGet(pCtx->pNames, i);
1986

1987
    CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, pReq->dbFName, &dbCache));
1988 1989

    if (NULL != dbCache) {
D
dapan1121 已提交
1990 1991
      SCtgTaskReq tReq;
      tReq.pTask = pTask;
1992
      tReq.msgIdx = -1;
dengyihao's avatar
dengyihao 已提交
1993 1994
      CTG_ERR_JRET(
          ctgGetVgInfosFromHashValue(pCtg, &tReq, dbCache->vgCache.vgInfo, pCtx, pReq->dbFName, pReq->pTables, false));
1995 1996 1997

      ctgReleaseVgInfoToCache(pCtg, dbCache);
      dbCache = NULL;
1998 1999

      baseResIdx += taosArrayGetSize(pReq->pTables);
2000
    } else {
2001
      ctgAddFetch(&pCtx->pFetchs, i, -1, &fetchIdx, baseResIdx, 0);
2002

2003
      baseResIdx += taosArrayGetSize(pReq->pTables);
H
Haojun Liao 已提交
2004 2005 2006 2007
      int32_t inc = baseResIdx - taosArrayGetSize(pCtx->pResList);
      for(int32_t j = 0; j < inc; ++j) {
        taosArrayPush(pCtx->pResList, &(SMetaRes){0});
      }
2008 2009 2010 2011 2012 2013
    }
  }

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

2015 2016 2017
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }
2018

2019
  pTask->msgCtxs = taosArrayInit_s(sizeof(SCtgMsgCtx), pCtx->fetchNum);
2020

2021
  for (int32_t i = 0; i < pCtx->fetchNum; ++i) {
dengyihao's avatar
dengyihao 已提交
2022
    SCtgFetch*  pFetch = taosArrayGet(pCtx->pFetchs, i);
D
dapan1121 已提交
2023
    STablesReq* pReq = taosArrayGet(pCtx->pNames, pFetch->dbIdx);
dengyihao's avatar
dengyihao 已提交
2024
    SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, i);
D
dapan1121 已提交
2025 2026 2027
    if (NULL == pMsgCtx->pBatchs) {
      pMsgCtx->pBatchs = pJob->pBatchs;
    }
2028

2029
    SBuildUseDBInput input = {0};
D
dapan1121 已提交
2030
    strcpy(input.db, pReq->dbFName);
2031

2032 2033
    input.vgVersion = CTG_DEFAULT_INVALID_VERSION;

D
dapan1121 已提交
2034 2035 2036 2037
    SCtgTaskReq tReq;
    tReq.pTask = pTask;
    tReq.msgIdx = pFetch->fetchIdx;
    CTG_ERR_RET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, &tReq));
2038 2039 2040 2041 2042 2043 2044
  }

_return:

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

2046 2047 2048
  return code;
}

dengyihao's avatar
dengyihao 已提交
2049 2050 2051
int32_t ctgLaunchGetTbIndexTask(SCtgTask* pTask) {
  int32_t           code = 0;
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2052
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2053 2054 2055 2056
  SCtgTbIndexCtx*   pCtx = (SCtgTbIndexCtx*)pTask->taskCtx;
  SArray*           pRes = NULL;
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2057 2058 2059
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2060 2061 2062 2063

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

D
dapan1121 已提交
2065 2066 2067
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }
2068

D
dapan1121 已提交
2069
  CTG_ERR_RET(ctgGetTbIndexFromMnode(pCtg, pConn, pCtx->pName, NULL, pTask));
D
dapan1121 已提交
2070 2071 2072
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2073 2074 2075
int32_t ctgLaunchGetTbCfgTask(SCtgTask* pTask) {
  int32_t           code = 0;
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2076
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2077 2078 2079
  SCtgTbCfgCtx*     pCtx = (SCtgTbCfgCtx*)pTask->taskCtx;
  SArray*           pRes = NULL;
  char              dbFName[TSDB_DB_FNAME_LEN];
D
dapan1121 已提交
2080
  tNameGetFullDbName(pCtx->pName, dbFName);
dengyihao's avatar
dengyihao 已提交
2081 2082
  SCtgJob*    pJob = pTask->pJob;
  SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2083 2084 2085
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2086

D
dapan1121 已提交
2087 2088
  CTG_CACHE_NHIT_INC(CTG_CI_TBL_CFG, 1);
  
D
dapan1121 已提交
2089 2090 2091
  if (pCtx->tbType <= 0) {
    CTG_ERR_JRET(ctgReadTbTypeFromCache(pCtg, dbFName, pCtx->pName->tname, &pCtx->tbType));
    if (pCtx->tbType <= 0) {
2092 2093 2094 2095
      SCtgTbMetaParam param;
      param.pName = pCtx->pName;
      param.flag = 0;
      CTG_ERR_JRET(ctgLaunchSubTask(pTask, CTG_TASK_GET_TB_META, ctgGetTbCfgCb, &param));
D
dapan1121 已提交
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
      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;
      }
    }
2110

D
dapan1121 已提交
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
    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);
  }
2121

D
dapan1121 已提交
2122 2123 2124
  CTG_RET(code);
}

2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142

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) {
2143
      CTG_ERR_JRET(ctgLaunchSubTask(pTask, CTG_TASK_GET_DB_VGROUP, ctgGetTbTagCb, dbFName));
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
      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 已提交
2164 2165
int32_t ctgLaunchGetQnodeTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2166
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2167 2168
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2169 2170 2171
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2172

D
dapan1121 已提交
2173
  CTG_CACHE_NHIT_INC(CTG_CI_QNODE, 1);
D
dapan1121 已提交
2174
  CTG_ERR_RET(ctgGetQnodeListFromMnode(pCtg, pConn, NULL, pTask));
D
dapan1121 已提交
2175 2176 2177
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2178 2179
int32_t ctgLaunchGetDnodeTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2180
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2181 2182
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2183 2184 2185
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2186

D
dapan1121 已提交
2187
  CTG_CACHE_NHIT_INC(CTG_CI_DNODE, 1);
D
dapan1121 已提交
2188 2189 2190 2191
  CTG_ERR_RET(ctgGetDnodeListFromMnode(pCtg, pConn, NULL, pTask));
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2192 2193
int32_t ctgLaunchGetDbCfgTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2194
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2195 2196 2197
  SCtgDbCfgCtx*     pCtx = (SCtgDbCfgCtx*)pTask->taskCtx;
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2198 2199 2200
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2201

D
dapan1121 已提交
2202 2203
  CTG_CACHE_NHIT_INC(CTG_CI_DB_CFG, 1);

D
dapan1121 已提交
2204
  CTG_ERR_RET(ctgGetDBCfgFromMnode(pCtg, pConn, pCtx->dbFName, NULL, pTask));
D
dapan1121 已提交
2205 2206 2207 2208

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2209 2210 2211 2212
int32_t ctgLaunchGetDbInfoTask(SCtgTask* pTask) {
  int32_t        code = 0;
  SCatalog*      pCtg = pTask->pJob->pCtg;
  SCtgDBCache*   dbCache = NULL;
D
dapan1121 已提交
2213
  SCtgDbInfoCtx* pCtx = (SCtgDbInfoCtx*)pTask->taskCtx;
dengyihao's avatar
dengyihao 已提交
2214 2215
  SCtgJob*       pJob = pTask->pJob;
  SCtgMsgCtx*    pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2216 2217 2218
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2219 2220 2221 2222 2223 2224 2225 2226 2227

  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 已提交
2228
    pInfo->vgVer = dbCache->vgCache.vgInfo->vgVersion;
D
dapan1121 已提交
2229
    pInfo->dbId = dbCache->dbId;
D
dapan1121 已提交
2230
    pInfo->tbNum = dbCache->vgCache.vgInfo->numOfTable;
D
dapan1121 已提交
2231
    pInfo->stateTs = dbCache->vgCache.vgInfo->stateTs;
D
dapan1121 已提交
2232

D
dapan1121 已提交
2233 2234
    CTG_CACHE_HIT_INC(CTG_CI_DB_INFO, 1);

D
dapan1121 已提交
2235 2236
    ctgReleaseVgInfoToCache(pCtg, dbCache);
    dbCache = NULL;
D
dapan1121 已提交
2237 2238
  } else {
    pInfo->vgVer = CTG_DEFAULT_INVALID_VERSION;
D
dapan1121 已提交
2239 2240

    CTG_CACHE_NHIT_INC(CTG_CI_DB_INFO, 1);
D
dapan1121 已提交
2241 2242 2243 2244 2245 2246 2247 2248 2249
  }

  CTG_ERR_JRET(ctgHandleTaskEnd(pTask, 0));

_return:

  CTG_RET(code);
}

dengyihao's avatar
dengyihao 已提交
2250 2251
int32_t ctgLaunchGetIndexTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2252
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2253 2254 2255
  SCtgIndexCtx*     pCtx = (SCtgIndexCtx*)pTask->taskCtx;
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2256 2257 2258
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2259

D
dapan1121 已提交
2260 2261
  CTG_CACHE_NHIT_INC(CTG_CI_INDEX_INFO, 1);

D
dapan1121 已提交
2262
  CTG_ERR_RET(ctgGetIndexInfoFromMnode(pCtg, pConn, pCtx->indexFName, NULL, pTask));
D
dapan1121 已提交
2263 2264 2265 2266

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2267 2268
int32_t ctgLaunchGetUdfTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2269
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2270 2271 2272
  SCtgUdfCtx*       pCtx = (SCtgUdfCtx*)pTask->taskCtx;
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2273 2274 2275
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2276

D
dapan1121 已提交
2277 2278
  CTG_CACHE_NHIT_INC(CTG_CI_UDF, 1);

D
dapan1121 已提交
2279
  CTG_ERR_RET(ctgGetUdfInfoFromMnode(pCtg, pConn, pCtx->udfName, NULL, pTask));
D
dapan1121 已提交
2280 2281 2282 2283

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2284
int32_t ctgLaunchGetUserTask(SCtgTask* pTask) {
D
dapan1121 已提交
2285
  int32_t           code = 0;
dengyihao's avatar
dengyihao 已提交
2286
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2287
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2288 2289
  SCtgUserCtx*      pCtx = (SCtgUserCtx*)pTask->taskCtx;
  bool              inCache = false;
D
dapan1121 已提交
2290
  SCtgAuthRsp       rsp = {0};
dengyihao's avatar
dengyihao 已提交
2291 2292
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2293 2294 2295
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
2296

D
dapan1121 已提交
2297 2298 2299 2300 2301 2302
  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 已提交
2303
  if (inCache) {
D
dapan1121 已提交
2304
    pTask->res = rsp.pRawRes;
2305

D
dapan1121 已提交
2306 2307
    ctgTaskDebug("Final res got, pass:%d, pCond:%p", rsp.pRawRes->pass, rsp.pRawRes->pCond);

D
dapan1121 已提交
2308 2309 2310 2311
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
2312
  taosMemoryFreeClear(rsp.pRawRes);
D
dapan1121 已提交
2313

D
dapan1121 已提交
2314
  if (rsp.metaNotExists) {
2315 2316 2317 2318
    SCtgTbMetaParam param;
    param.pName = &pCtx->user.tbName;
    param.flag = CTG_FLAG_SYNC_OP;
    CTG_ERR_RET(ctgLaunchSubTask(pTask, CTG_TASK_GET_TB_META, ctgGetUserCb, &param));
D
dapan1121 已提交
2319 2320 2321 2322
  } else {
    CTG_ERR_RET(ctgGetUserDbAuthFromMnode(pCtg, pConn, pCtx->user.user, NULL, pTask));
  }
  
D
dapan1121 已提交
2323 2324 2325
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2326 2327
int32_t ctgLaunchGetSvrVerTask(SCtgTask* pTask) {
  SCatalog*         pCtg = pTask->pJob->pCtg;
D
dapan1121 已提交
2328
  SRequestConnInfo* pConn = &pTask->pJob->conn;
dengyihao's avatar
dengyihao 已提交
2329 2330
  SCtgJob*          pJob = pTask->pJob;
  SCtgMsgCtx*       pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
D
dapan1121 已提交
2331 2332 2333
  if (NULL == pMsgCtx->pBatchs) {
    pMsgCtx->pBatchs = pJob->pBatchs;
  }
D
dapan1121 已提交
2334

D
dapan1121 已提交
2335 2336
  CTG_CACHE_NHIT_INC(CTG_CI_SVR_VER, 1);

D
dapan1121 已提交
2337 2338 2339 2340 2341
  CTG_ERR_RET(ctgGetSvrVerFromMnode(pCtg, pConn, NULL, pTask));

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2342
int32_t ctgRelaunchGetTbMetaTask(SCtgTask* pTask) {
D
dapan1121 已提交
2343 2344 2345 2346 2347 2348 2349
  ctgResetTbMetaTask(pTask);

  CTG_ERR_RET(ctgLaunchGetTbMetaTask(pTask));

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2350
int32_t ctgGetTbCfgCb(SCtgTask* pTask) {
D
dapan1121 已提交
2351
  int32_t code = 0;
2352

D
dapan1121 已提交
2353 2354 2355 2356 2357 2358 2359
  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;
2360

D
dapan1121 已提交
2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371
    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));
}

2372 2373 2374 2375 2376 2377 2378 2379
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;

2380 2381 2382 2383 2384
  if (NULL == pCtx->pVgInfo) {
    pCtx->pVgInfo = taosMemoryCalloc(1, sizeof(SVgroupInfo));
    CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pTask->pJob->pCtg, pDb, pCtx->pName, pCtx->pVgInfo));
  }
  
2385 2386 2387 2388 2389 2390 2391 2392
  CTG_RET(ctgLaunchGetTbTagTask(pTask));

_return:

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


D
dapan1121 已提交
2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406

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 已提交
2407 2408 2409 2410 2411 2412 2413 2414 2415 2416
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;
2417
  SCtgTbMetaParam* pParam = (SCtgTbMetaParam*)param;
D
dapan1121 已提交
2418

2419 2420 2421 2422
  *equal = tNameTbNameEqual(ctx->pName, (SName*)pParam->pName);
  if (*equal) {
    ctx->flag |= pParam->flag;
  }
D
dapan1121 已提交
2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438

  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 已提交
2439
SCtgAsyncFps gCtgAsyncFps[] = {
dengyihao's avatar
dengyihao 已提交
2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455
    {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},
2456
    {ctgInitGetTbTagTask, ctgLaunchGetTbTagTask, ctgHandleGetTbTagRsp, ctgDumpTbTagRes, NULL, NULL},
D
dapan1121 已提交
2457 2458
};

dengyihao's avatar
dengyihao 已提交
2459
int32_t ctgMakeAsyncRes(SCtgJob* pJob) {
D
dapan1121 已提交
2460 2461
  int32_t code = 0;
  int32_t taskNum = taosArrayGetSize(pJob->pTasks);
2462

D
dapan1121 已提交
2463
  for (int32_t i = 0; i < taskNum; ++i) {
dengyihao's avatar
dengyihao 已提交
2464
    SCtgTask* pTask = taosArrayGet(pJob->pTasks, i);
D
dapan1121 已提交
2465 2466 2467 2468 2469 2470
    CTG_ERR_RET((*gCtgAsyncFps[pTask->type].dumpResFp)(pTask));
  }

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
2471
int32_t ctgSearchExistingTask(SCtgJob* pJob, CTG_TASK_TYPE type, void* param, int32_t* taskId) {
D
dapan1121 已提交
2472 2473 2474
  bool      equal = false;
  SCtgTask* pTask = NULL;
  int32_t   code = 0;
2475

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

D
dapan1121 已提交
2478 2479 2480
  int32_t taskNum = taosArrayGetSize(pJob->pTasks);
  for (int32_t i = 0; i < taskNum; ++i) {
    pTask = taosArrayGet(pJob->pTasks, i);
dengyihao's avatar
dengyihao 已提交
2481
    if (type != pTask->type) {
D
dapan1121 已提交
2482 2483
      continue;
    }
2484

D
dapan1121 已提交
2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500
    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 已提交
2501
int32_t ctgSetSubTaskCb(SCtgTask* pSub, SCtgTask* pTask) {
D
dapan1121 已提交
2502
  int32_t code = 0;
2503

D
dapan1121 已提交
2504 2505 2506 2507
  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 已提交
2508 2509
    SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
    SCtgMsgCtx* pSubMsgCtx = CTG_GET_TASK_MSGCTX(pSub, -1);
D
dapan1121 已提交
2510
    pMsgCtx->pBatchs = pSubMsgCtx->pBatchs;
2511

D
dapan1121 已提交
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524
    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);

2525
  CTG_RET(code);
D
dapan1121 已提交
2526 2527
}

dengyihao's avatar
dengyihao 已提交
2528
int32_t ctgLaunchSubTask(SCtgTask* pTask, CTG_TASK_TYPE type, ctgSubTaskCbFp fp, void* param) {
D
dapan1121 已提交
2529 2530 2531 2532 2533 2534 2535
  SCtgJob* pJob = pTask->pJob;
  int32_t  subTaskId = -1;
  bool     newTask = false;

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

D
dapan1121 已提交
2537 2538 2539 2540 2541
  CTG_ERR_RET(ctgSearchExistingTask(pJob, type, param, &subTaskId));
  if (subTaskId < 0) {
    CTG_ERR_RET(ctgInitTask(pJob, type, param, &subTaskId));
    newTask = true;
  }
2542

D
dapan1121 已提交
2543
  SCtgTask* pSub = taosArrayGet(pJob->pTasks, subTaskId);
D
dapan1121 已提交
2544 2545 2546
  if (newTask) {
    pSub->subTask = true;
  }
D
dapan1121 已提交
2547 2548 2549 2550

  CTG_ERR_RET(ctgSetSubTaskCb(pSub, pTask));

  if (newTask) {
dengyihao's avatar
dengyihao 已提交
2551 2552
    SCtgMsgCtx* pMsgCtx = CTG_GET_TASK_MSGCTX(pTask, -1);
    SCtgMsgCtx* pSubMsgCtx = CTG_GET_TASK_MSGCTX(pSub, -1);
D
dapan1121 已提交
2553 2554
    pSubMsgCtx->pBatchs = pMsgCtx->pBatchs;

D
dapan1121 已提交
2555 2556 2557 2558 2559 2560
    CTG_ERR_RET((*gCtgAsyncFps[pSub->type].launchFp)(pSub));
    pSub->status = CTG_TASK_LAUNCHED;
  }

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
2561

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

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

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

D
dapan1121 已提交
2571
    pTask->status = CTG_TASK_LAUNCHED;
D
dapan1121 已提交
2572 2573
  }

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

D
dapan1121 已提交
2577
    taosAsyncExec(ctgCallUserCb, pJob, NULL);
2578
#if CTG_BATCH_FETCH
D
dapan1121 已提交
2579 2580 2581
  } else {
    ctgLaunchBatchs(pJob->pCtg, pJob, pJob->pBatchs);
#endif
D
dapan1121 已提交
2582 2583
  }

D
dapan1121 已提交
2584 2585
  return TSDB_CODE_SUCCESS;
}