ctgAsync.c 49.6 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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 "trpc.h"
#include "query.h"
#include "tname.h"
#include "catalogInt.h"
#include "systable.h"
#include "tref.h"

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

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
53 54
int32_t ctgInitGetDbVgTask(SCtgJob *pJob, int32_t taskIdx, void* param) {
  char *dbFName = (char*)param;
D
dapan1121 已提交
55
  SCtgTask task = {0};
D
dapan1121 已提交
56

D
dapan1121 已提交
57 58 59
  task.type = CTG_TASK_GET_DB_VGROUP;
  task.taskId = taskIdx;
  task.pJob = pJob;
D
dapan1121 已提交
60

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

D
dapan1121 已提交
66
  SCtgDbVgCtx* ctx = task.taskCtx;
D
dapan1121 已提交
67 68 69
  
  memcpy(ctx->dbFName, dbFName, sizeof(ctx->dbFName));

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

D
dapan1121 已提交
72
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, dbFName:%s", pJob->queryId, taskIdx, ctgTaskTypeStr(task.type), dbFName);
D
dapan1121 已提交
73 74 75 76

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
77 78
int32_t ctgInitGetDbCfgTask(SCtgJob *pJob, int32_t taskIdx, void* param) {
  char *dbFName = (char*)param;
D
dapan1121 已提交
79
  SCtgTask task = {0};
D
dapan1121 已提交
80

D
dapan1121 已提交
81 82 83
  task.type = CTG_TASK_GET_DB_CFG;
  task.taskId = taskIdx;
  task.pJob = pJob;
D
dapan1121 已提交
84

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

D
dapan1121 已提交
90
  SCtgDbCfgCtx* ctx = task.taskCtx;
D
dapan1121 已提交
91 92 93
  
  memcpy(ctx->dbFName, dbFName, sizeof(ctx->dbFName));

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

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

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
101 102
int32_t ctgInitGetDbInfoTask(SCtgJob *pJob, int32_t taskIdx, void* param) {
  char *dbFName = (char*)param;
D
dapan1121 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  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;
  
  memcpy(ctx->dbFName, dbFName, sizeof(ctx->dbFName));

  taosArrayPush(pJob->pTasks, &task);

D
dapan1121 已提交
120
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, dbFName:%s", pJob->queryId, taskIdx, ctgTaskTypeStr(task.type), dbFName);
D
dapan1121 已提交
121 122 123 124 125

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
126 127
int32_t ctgInitGetTbHashTask(SCtgJob *pJob, int32_t taskIdx, void* param) {
  SName *name = (SName*)param;
D
dapan1121 已提交
128
  SCtgTask task = {0};
D
dapan1121 已提交
129

D
dapan1121 已提交
130 131 132
  task.type = CTG_TASK_GET_TB_HASH;
  task.taskId = taskIdx;
  task.pJob = pJob;
D
dapan1121 已提交
133

D
dapan1121 已提交
134 135
  task.taskCtx = taosMemoryCalloc(1, sizeof(SCtgTbHashCtx));
  if (NULL == task.taskCtx) {
D
dapan1121 已提交
136 137 138
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
139
  SCtgTbHashCtx* ctx = task.taskCtx;
D
dapan1121 已提交
140 141
  ctx->pName = taosMemoryMalloc(sizeof(*name));
  if (NULL == ctx->pName) {
D
dapan1121 已提交
142
    taosMemoryFree(task.taskCtx);
D
dapan1121 已提交
143 144 145 146 147 148
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }
  
  memcpy(ctx->pName, name, sizeof(*name));
  tNameGetFullDbName(ctx->pName, ctx->dbFName);

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

D
dapan1121 已提交
151
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, tableName:%s", pJob->queryId, taskIdx, ctgTaskTypeStr(task.type), name->tname);
D
dapan1121 已提交
152 153 154 155

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
156
int32_t ctgInitGetQnodeTask(SCtgJob *pJob, int32_t taskIdx, void* param) {
D
dapan1121 已提交
157 158 159 160 161 162
  SCtgTask task = {0};

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

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

D
dapan1121 已提交
166
  qDebug("QID:0x%" PRIx64 " the %d task type %s initialized", pJob->queryId, taskIdx, ctgTaskTypeStr(task.type));
D
dapan1121 已提交
167 168 169 170

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
int32_t ctgInitGetDnodeTask(SCtgJob *pJob, int32_t taskIdx, void* param) {
  SCtgTask task = {0};

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

  taosArrayPush(pJob->pTasks, &task);

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

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
186 187
int32_t ctgInitGetIndexTask(SCtgJob *pJob, int32_t taskIdx, void* param) {
  char *name = (char*)param;
D
dapan1121 已提交
188
  SCtgTask task = {0};
D
dapan1121 已提交
189

D
dapan1121 已提交
190 191 192
  task.type = CTG_TASK_GET_INDEX;
  task.taskId = taskIdx;
  task.pJob = pJob;
D
dapan1121 已提交
193

D
dapan1121 已提交
194 195
  task.taskCtx = taosMemoryCalloc(1, sizeof(SCtgIndexCtx));
  if (NULL == task.taskCtx) {
D
dapan1121 已提交
196 197 198
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
199
  SCtgIndexCtx* ctx = task.taskCtx;
D
dapan1121 已提交
200 201 202
  
  strcpy(ctx->indexFName, name);

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

D
dapan1121 已提交
205
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, indexFName:%s", pJob->queryId, taskIdx, ctgTaskTypeStr(task.type), name);
D
dapan1121 已提交
206 207 208 209

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
210 211
int32_t ctgInitGetUdfTask(SCtgJob *pJob, int32_t taskIdx, void* param) {
  char *name = (char*)param;
D
dapan1121 已提交
212
  SCtgTask task = {0};
D
dapan1121 已提交
213

D
dapan1121 已提交
214 215 216
  task.type = CTG_TASK_GET_UDF;
  task.taskId = taskIdx;
  task.pJob = pJob;
D
dapan1121 已提交
217

D
dapan1121 已提交
218 219
  task.taskCtx = taosMemoryCalloc(1, sizeof(SCtgUdfCtx));
  if (NULL == task.taskCtx) {
D
dapan1121 已提交
220 221 222
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
223
  SCtgUdfCtx* ctx = task.taskCtx;
D
dapan1121 已提交
224 225 226
  
  strcpy(ctx->udfName, name);

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

D
dapan1121 已提交
229
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, udfName:%s", pJob->queryId, taskIdx, ctgTaskTypeStr(task.type), name);
D
dapan1121 已提交
230 231 232 233

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
234 235
int32_t ctgInitGetUserTask(SCtgJob *pJob, int32_t taskIdx, void* param) {
  SUserAuthInfo *user = (SUserAuthInfo*)param;
D
dapan1121 已提交
236
  SCtgTask task = {0};
D
dapan1121 已提交
237

D
dapan1121 已提交
238 239 240
  task.type = CTG_TASK_GET_USER;
  task.taskId = taskIdx;
  task.pJob = pJob;
D
dapan1121 已提交
241

D
dapan1121 已提交
242 243
  task.taskCtx = taosMemoryCalloc(1, sizeof(SCtgUserCtx));
  if (NULL == task.taskCtx) {
D
dapan1121 已提交
244 245 246
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
247
  SCtgUserCtx* ctx = task.taskCtx;
D
dapan1121 已提交
248 249 250
  
  memcpy(&ctx->user, user, sizeof(*user));

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

D
dapan1121 已提交
253
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, user:%s", pJob->queryId, taskIdx, ctgTaskTypeStr(task.type), user->user);
D
dapan1121 已提交
254 255 256 257

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271
int32_t ctgInitGetSvrVerTask(SCtgJob *pJob, int32_t taskIdx, void* param) {
  SCtgTask task = {0};

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

  taosArrayPush(pJob->pTasks, &task);

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

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
272 273
int32_t ctgInitGetTbIndexTask(SCtgJob *pJob, int32_t taskIdx, void* param) {
  SName *name = (SName*)param;
D
dapan1121 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
  SCtgTask task = {0};

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

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

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

  taosArrayPush(pJob->pTasks, &task);

D
dapan1121 已提交
296
  qDebug("QID:0x%" PRIx64 " the %dth task type %s initialized, tbName:%s", pJob->queryId, taskIdx, ctgTaskTypeStr(task.type), name->tname);
D
dapan1121 已提交
297 298 299 300

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
int32_t ctgInitGetTbCfgTask(SCtgJob *pJob, int32_t taskIdx, void* param) {
  SName *name = (SName*)param;
  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);
  }
  
  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;
}


D
dapan1121 已提交
331

D
dapan1121 已提交
332 333 334 335 336 337 338 339 340 341
int32_t ctgHandleForceUpdate(SCatalog* pCtg, int32_t taskNum, SCtgJob *pJob, const SCatalogReq* pReq) {
  SHashObj* pDb = taosHashInit(taskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  if (NULL == pDb) {
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }
  
  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 已提交
342

D
dapan1121 已提交
343 344 345 346
  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 已提交
347

D
dapan1121 已提交
348 349 350 351
  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 已提交
352

D
dapan1121 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365
  for (int32_t i = 0; i < pJob->tbMetaNum; ++i) {
    SName* name = taosArrayGet(pReq->pTableMeta, i);
    char dbFName[TSDB_DB_FNAME_LEN];
    tNameGetFullDbName(name, dbFName);
    taosHashPut(pDb, dbFName, strlen(dbFName), dbFName, TSDB_DB_FNAME_LEN);
  }
  
  for (int32_t i = 0; i < pJob->tbHashNum; ++i) {
    SName* name = taosArrayGet(pReq->pTableHash, i);
    char dbFName[TSDB_DB_FNAME_LEN];
    tNameGetFullDbName(name, dbFName);
    taosHashPut(pDb, dbFName, strlen(dbFName), dbFName, TSDB_DB_FNAME_LEN);
  }
D
dapan1121 已提交
366

D
dapan1121 已提交
367 368 369 370 371 372 373
  for (int32_t i = 0; i < pJob->tbCfgNum; ++i) {
    SName* name = taosArrayGet(pReq->pTableCfg, i);
    char dbFName[TSDB_DB_FNAME_LEN];
    tNameGetFullDbName(name, dbFName);
    taosHashPut(pDb, dbFName, strlen(dbFName), dbFName, TSDB_DB_FNAME_LEN);
  }

D
dapan1121 已提交
374 375 376 377
  char* dbFName = taosHashIterate(pDb, NULL);
  while (dbFName) {
    ctgDropDbVgroupEnqueue(pCtg, dbFName, true);
    dbFName = taosHashIterate(pDb, dbFName);
D
dapan1121 已提交
378 379
  }

D
dapan1121 已提交
380 381
  taosHashCleanup(pDb);

D
dapan1121 已提交
382 383 384 385 386 387 388 389 390 391 392
  // REFRESH TABLE META
  SHashObj* pTb = taosHashInit(taskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  for (int32_t i = 0; i < pJob->tbMetaNum; ++i) {
    SName* name = taosArrayGet(pReq->pTableMeta, i);
    taosHashPut(pTb, name, sizeof(SName), name, sizeof(SName));
  }
  
  for (int32_t i = 0; i < pJob->tbHashNum; ++i) {
    SName* name = taosArrayGet(pReq->pTableHash, i);
    taosHashPut(pTb, name, sizeof(SName), name, sizeof(SName));
  }
D
dapan1121 已提交
393

D
dapan1121 已提交
394 395 396 397
  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 已提交
398

D
dapan1121 已提交
399 400 401 402
  SName* name = taosHashIterate(pTb, NULL);
  while (name) {
    catalogRemoveTableMeta(pCtg, name);
    name = taosHashIterate(pTb, name);
D
dapan1121 已提交
403 404
  }

D
dapan1121 已提交
405 406 407
  taosHashCleanup(pTb);


D
dapan1121 已提交
408 409 410 411 412
  for (int32_t i = 0; i < pJob->tbIndexNum; ++i) {
    SName* name = taosArrayGet(pReq->pTableIndex, i);
    ctgDropTbIndexEnqueue(pCtg, name, true);
  }

D
dapan1121 已提交
413 414
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
415

D
dapan1121 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429
int32_t ctgInitTask(SCtgJob *pJob, CTG_TASK_TYPE type, void* param, int32_t *taskId) {
  int32_t tid = atomic_fetch_add_32(&pJob->taskIdx, 1);

  CTG_LOCK(CTG_WRITE, &pJob->taskLock);
  CTG_ERR_RET((*gCtgAsyncFps[type].initFp)(pJob, tid, param));
  CTG_UNLOCK(CTG_WRITE, &pJob->taskLock);

  if (taskId) {
    *taskId = tid;
  }
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
430
int32_t ctgInitJob(SCatalog* pCtg, SRequestConnInfo *pConn, SCtgJob** job, const SCatalogReq* pReq, catalogCallback fp, void* param) {
D
dapan1121 已提交
431 432 433 434 435 436
  int32_t code = 0;
  int32_t tbMetaNum = (int32_t)taosArrayGetSize(pReq->pTableMeta);
  int32_t dbVgNum = (int32_t)taosArrayGetSize(pReq->pDbVgroup);
  int32_t tbHashNum = (int32_t)taosArrayGetSize(pReq->pTableHash);
  int32_t udfNum = (int32_t)taosArrayGetSize(pReq->pUdf);
  int32_t qnodeNum = pReq->qNodeRequired ? 1 : 0;
D
dapan1121 已提交
437
  int32_t dnodeNum = pReq->dNodeRequired ? 1 : 0;
D
dapan1121 已提交
438
  int32_t svrVerNum = pReq->svrVerRequired ? 1 : 0;
D
dapan1121 已提交
439 440 441
  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 已提交
442
  int32_t dbInfoNum = (int32_t)taosArrayGetSize(pReq->pDbInfo);
D
dapan1121 已提交
443
  int32_t tbIndexNum = (int32_t)taosArrayGetSize(pReq->pTableIndex);
D
dapan1121 已提交
444
  int32_t tbCfgNum = (int32_t)taosArrayGetSize(pReq->pTableCfg);
D
dapan1121 已提交
445

D
dapan1121 已提交
446
  int32_t taskNum = tbMetaNum + dbVgNum + udfNum + tbHashNum + qnodeNum + dnodeNum + svrVerNum + dbCfgNum + indexNum + userNum + dbInfoNum + tbIndexNum + tbCfgNum;
447

D
dapan1121 已提交
448 449
  *job = taosMemoryCalloc(1, sizeof(SCtgJob));
  if (NULL == *job) {
D
dapan1121 已提交
450
    ctgError("failed to calloc, size:%d, reqId:0x%" PRIx64, (int32_t)sizeof(SCtgJob), pConn->requestId);
D
dapan1121 已提交
451 452 453 454 455
    CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }

  SCtgJob *pJob = *job;
  
D
dapan1121 已提交
456
  pJob->queryId = pConn->requestId;
D
dapan1121 已提交
457 458
  pJob->userFp = fp;
  pJob->pCtg    = pCtg;
D
dapan1121 已提交
459
  pJob->conn    = *pConn;
D
dapan1121 已提交
460 461 462 463 464
  pJob->userParam = param;
  
  pJob->tbMetaNum = tbMetaNum;
  pJob->tbHashNum = tbHashNum;
  pJob->qnodeNum = qnodeNum;
D
dapan1121 已提交
465
  pJob->dnodeNum = dnodeNum;
D
dapan1121 已提交
466 467 468 469 470
  pJob->dbVgNum = dbVgNum;
  pJob->udfNum = udfNum;
  pJob->dbCfgNum = dbCfgNum;
  pJob->indexNum = indexNum;
  pJob->userNum = userNum;
D
dapan1121 已提交
471
  pJob->dbInfoNum = dbInfoNum;
D
dapan1121 已提交
472
  pJob->tbIndexNum = tbIndexNum;
D
dapan1121 已提交
473
  pJob->tbCfgNum = tbCfgNum;
D
dapan1121 已提交
474
  pJob->svrVerNum = svrVerNum;
475

D
dapan1121 已提交
476
  pJob->pTasks = taosArrayInit(taskNum, sizeof(SCtgTask));
477

D
dapan1121 已提交
478
  if (NULL == pJob->pTasks) {
D
dapan1121 已提交
479
    ctgError("taosArrayInit %d tasks failed", taskNum);
D
dapan1121 已提交
480 481 482
    CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
483 484
  if (pReq->forceUpdate && taskNum) {
    CTG_ERR_JRET(ctgHandleForceUpdate(pCtg, taskNum, pJob, pReq));
D
dapan1121 已提交
485 486
  }

D
dapan1121 已提交
487
  for (int32_t i = 0; i < dbVgNum; ++i) {
488
    char* dbFName = taosArrayGet(pReq->pDbVgroup, i);
D
dapan1121 已提交
489
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_DB_VGROUP, dbFName, NULL));
D
dapan1121 已提交
490 491 492
  }

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

D
dapan1121 已提交
497
  for (int32_t i = 0; i < dbInfoNum; ++i) {
498
    char* dbFName = taosArrayGet(pReq->pDbInfo, i);
D
dapan1121 已提交
499
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_DB_INFO, dbFName, NULL));
D
dapan1121 已提交
500 501
  }

D
dapan1121 已提交
502
  for (int32_t i = 0; i < tbMetaNum; ++i) {
503
    SName* name = taosArrayGet(pReq->pTableMeta, i);
D
dapan1121 已提交
504
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_TB_META, name, NULL));
D
dapan1121 已提交
505 506 507
  }

  for (int32_t i = 0; i < tbHashNum; ++i) {
508
    SName* name = taosArrayGet(pReq->pTableHash, i);
D
dapan1121 已提交
509
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_TB_HASH, name, NULL));
D
dapan1121 已提交
510 511
  }

D
dapan1121 已提交
512 513
  for (int32_t i = 0; i < tbIndexNum; ++i) {
    SName* name = taosArrayGet(pReq->pTableIndex, i);
D
dapan1121 已提交
514 515 516 517 518 519
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_TB_INDEX, name, NULL));
  }

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

D
dapan1121 已提交
522
  for (int32_t i = 0; i < indexNum; ++i) {
523
    char* indexName = taosArrayGet(pReq->pIndex, i);
D
dapan1121 已提交
524
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_INDEX, indexName, NULL));
D
dapan1121 已提交
525 526 527
  }

  for (int32_t i = 0; i < udfNum; ++i) {
528
    char* udfName = taosArrayGet(pReq->pUdf, i);
D
dapan1121 已提交
529
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_UDF, udfName, NULL));
D
dapan1121 已提交
530 531 532
  }

  for (int32_t i = 0; i < userNum; ++i) {
533
    SUserAuthInfo* user = taosArrayGet(pReq->pUser, i);
D
dapan1121 已提交
534
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_USER, user, NULL));
D
dapan1121 已提交
535 536 537
  }

  if (qnodeNum) {
D
dapan1121 已提交
538
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_QNODE, NULL, NULL));
D
dapan1121 已提交
539 540
  }

D
dapan1121 已提交
541 542 543 544
  if (dnodeNum) {
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_DNODE, NULL, NULL));
  }

D
dapan1121 已提交
545 546 547 548
  if (svrVerNum) {
    CTG_ERR_JRET(ctgInitTask(pJob, CTG_TASK_GET_SVR_VER, NULL, NULL));
  }

H
Haojun Liao 已提交
549 550 551 552 553 554 555 556
  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);

D
dapan1121 已提交
557
  qDebug("QID:0x%" PRIx64 ", jobId: 0x%" PRIx64 " initialized, task num %d, forceUpdate %d", pJob->queryId, pJob->refId, taskNum, pReq->forceUpdate);
H
Haojun Liao 已提交
558 559 560
  return TSDB_CODE_SUCCESS;


D
dapan1121 已提交
561
_return:
D
dapan1121 已提交
562

D
dapan1121 已提交
563 564 565 566 567 568 569
  taosMemoryFreeClear(*job);
  CTG_RET(code);
}

int32_t ctgDumpTbMetaRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pTableMeta) {
D
dapan1121 已提交
570
    pJob->jobRes.pTableMeta = taosArrayInit(pJob->tbMetaNum, sizeof(SMetaRes));
D
dapan1121 已提交
571 572 573 574 575
    if (NULL == pJob->jobRes.pTableMeta) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
576 577
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pTableMeta, &res);
D
dapan1121 已提交
578 579 580 581 582 583 584

  return TSDB_CODE_SUCCESS;
}

int32_t ctgDumpDbVgRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pDbVgroup) {
D
dapan1121 已提交
585
    pJob->jobRes.pDbVgroup = taosArrayInit(pJob->dbVgNum, sizeof(SMetaRes));
D
dapan1121 已提交
586 587 588 589 590
    if (NULL == pJob->jobRes.pDbVgroup) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
591 592
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pDbVgroup, &res);
D
dapan1121 已提交
593 594 595 596 597 598 599

  return TSDB_CODE_SUCCESS;
}

int32_t ctgDumpTbHashRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pTableHash) {
D
dapan1121 已提交
600
    pJob->jobRes.pTableHash = taosArrayInit(pJob->tbHashNum, sizeof(SMetaRes));
D
dapan1121 已提交
601 602 603 604 605
    if (NULL == pJob->jobRes.pTableHash) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
606 607
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pTableHash, &res);
D
dapan1121 已提交
608 609 610 611

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
612 613 614 615 616 617 618 619 620 621
int32_t ctgDumpTbIndexRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pTableIndex) {
    pJob->jobRes.pTableIndex = taosArrayInit(pJob->tbIndexNum, sizeof(SMetaRes));
    if (NULL == pJob->jobRes.pTableIndex) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
D
dapan1121 已提交
622
  taosArrayPush(pJob->jobRes.pTableIndex, &res);
D
dapan1121 已提交
623 624 625 626

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
int32_t ctgDumpTbCfgRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pTableCfg) {
    pJob->jobRes.pTableCfg = taosArrayInit(pJob->tbCfgNum, sizeof(SMetaRes));
    if (NULL == pJob->jobRes.pTableCfg) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

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

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
642 643 644
int32_t ctgDumpIndexRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pIndex) {
D
dapan1121 已提交
645
    pJob->jobRes.pIndex = taosArrayInit(pJob->indexNum, sizeof(SMetaRes));
D
dapan1121 已提交
646 647 648 649 650
    if (NULL == pJob->jobRes.pIndex) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
651 652
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pIndex, &res);
D
dapan1121 已提交
653 654 655 656 657 658

  return TSDB_CODE_SUCCESS;
}

int32_t ctgDumpQnodeRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
D
dapan1121 已提交
659 660 661 662 663 664
  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 已提交
665

D
dapan1121 已提交
666 667
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pQnodeList, &res);
D
dapan1121 已提交
668 669 670 671

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
int32_t ctgDumpDnodeRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pDnodeList) {
    pJob->jobRes.pDnodeList = taosArrayInit(1, sizeof(SMetaRes));
    if (NULL == pJob->jobRes.pDnodeList) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

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

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
688 689 690
int32_t ctgDumpDbCfgRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pDbCfg) {
D
dapan1121 已提交
691
    pJob->jobRes.pDbCfg = taosArrayInit(pJob->dbCfgNum, sizeof(SMetaRes));
D
dapan1121 已提交
692 693 694 695 696
    if (NULL == pJob->jobRes.pDbCfg) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

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

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
703 704 705
int32_t ctgDumpDbInfoRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pDbInfo) {
D
dapan1121 已提交
706
    pJob->jobRes.pDbInfo = taosArrayInit(pJob->dbInfoNum, sizeof(SMetaRes));
D
dapan1121 已提交
707 708 709 710 711
    if (NULL == pJob->jobRes.pDbInfo) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

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

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
718 719 720
int32_t ctgDumpUdfRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pUdfList) {
D
dapan1121 已提交
721
    pJob->jobRes.pUdfList = taosArrayInit(pJob->udfNum, sizeof(SMetaRes));
D
dapan1121 已提交
722 723 724 725 726
    if (NULL == pJob->jobRes.pUdfList) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
727 728
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pUdfList, &res);
D
dapan1121 已提交
729 730 731 732 733 734 735

  return TSDB_CODE_SUCCESS;
}

int32_t ctgDumpUserRes(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pUser) {
D
dapan1121 已提交
736
    pJob->jobRes.pUser = taosArrayInit(pJob->userNum, sizeof(SMetaRes));
D
dapan1121 已提交
737 738 739 740 741
    if (NULL == pJob->jobRes.pUser) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

D
dapan1121 已提交
742 743
  SMetaRes res = {.code = pTask->code, .pRes = pTask->res};
  taosArrayPush(pJob->jobRes.pUser, &res);
D
dapan1121 已提交
744 745 746 747

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
int32_t ctgDumpSvrVer(SCtgTask* pTask) {
  SCtgJob* pJob = pTask->pJob;
  if (NULL == pJob->jobRes.pSvrVer) {
    pJob->jobRes.pSvrVer = taosMemoryCalloc(1, sizeof(SMetaRes));
    if (NULL == pJob->jobRes.pSvrVer) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
  }

  pJob->jobRes.pSvrVer->code = pTask->code;
  pJob->jobRes.pSvrVer->pRes = pTask->res;
  
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
763
int32_t ctgCallSubCb(SCtgTask *pTask) {
D
dapan1121 已提交
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
  int32_t code = 0;
  
  CTG_LOCK(CTG_WRITE, &pTask->lock);
  
  int32_t parentNum = taosArrayGetSize(pTask->pParents);
  for (int32_t i = 0; i < parentNum; ++i) {
    SCtgTask* pParent = taosArrayGetP(pTask->pParents, i);
    
    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;
      }
    }
    
    CTG_ERR_JRET(pParent->subRes.fp(pParent));
  }
  
_return:

  CTG_UNLOCK(CTG_WRITE, &pTask->lock);

  CTG_RET(code);  
}

D
dapan1121 已提交
790 791 792 793 794 795 796 797 798
int32_t ctgCallUserCb(void* param) {
  SCtgJob* pJob = (SCtgJob*)param;
  
  (*pJob->userFp)(&pJob->jobRes, pJob->userParam, pJob->jobResCode);

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

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
799

D
dapan1121 已提交
800 801 802 803
int32_t ctgHandleTaskEnd(SCtgTask* pTask, int32_t rspCode) {
  SCtgJob* pJob = pTask->pJob;
  int32_t code = 0;

D
dapan1121 已提交
804 805 806 807
  if (CTG_TASK_DONE == pTask->status) {
    return TSDB_CODE_SUCCESS;
  }

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

D
dapan1121 已提交
810
  pTask->code = rspCode;
D
dapan1121 已提交
811 812
  pTask->status = CTG_TASK_DONE;

D
dapan1121 已提交
813
  ctgCallSubCb(pTask);
D
dapan1121 已提交
814 815 816

  int32_t taskDone = atomic_add_fetch_32(&pJob->taskDone, 1);
  if (taskDone < taosArrayGetSize(pJob->pTasks)) {
817
    qDebug("QID:0x%" PRIx64 " task done: %d, total: %d", pJob->queryId, taskDone, (int32_t)taosArrayGetSize(pJob->pTasks));
D
dapan1121 已提交
818 819 820 821 822 823 824
    return TSDB_CODE_SUCCESS;
  }

  CTG_ERR_JRET(ctgMakeAsyncRes(pJob));

_return:

D
dapan1121 已提交
825
  qDebug("QID:0x%" PRIx64 " ctg call user callback with rsp %s", pJob->queryId, tstrerror(code));
D
dapan1121 已提交
826

D
dapan1121 已提交
827
  pJob->jobResCode = code;
D
dapan1121 已提交
828 829 830

  taosSsleep(2);
  qDebug("QID:0x%" PRIx64 " ctg after sleep", pJob->queryId);
D
dapan1121 已提交
831 832
  
  taosAsyncExec(ctgCallUserCb, pJob, NULL);
D
dapan1121 已提交
833 834 835 836 837 838 839 840 841
  
  CTG_RET(code);
}

int32_t ctgHandleGetTbMetaRsp(SCtgTask* pTask, int32_t reqType, const SDataBuf *pMsg, int32_t rspCode) {
  int32_t code = 0;
  SCtgDBCache *dbCache = NULL;
  SCtgTbMetaCtx* ctx = (SCtgTbMetaCtx*)pTask->taskCtx;
  SCatalog* pCtg = pTask->pJob->pCtg; 
D
dapan1121 已提交
842 843 844
  SRequestConnInfo* pConn = &pTask->pJob->conn;

  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));
D
dapan1121 已提交
845 846 847 848 849 850 851 852 853 854

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

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

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

D
dapan1121 已提交
855 856
      ctx->vgId = vgInfo.vgId;
      CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, ctx->pName, &vgInfo, NULL, pTask));
D
dapan1121 已提交
857 858 859 860 861 862 863 864 865 866 867 868 869 870

      return TSDB_CODE_SUCCESS;
    }
    case TDMT_MND_TABLE_META: {
      STableMetaOutput* pOut = (STableMetaOutput*)pTask->msgCtx.out;
      
      if (CTG_IS_META_NULL(pOut->metaType)) {
        if (CTG_FLAG_IS_STB(ctx->flag)) {
          char dbFName[TSDB_DB_FNAME_LEN] = {0};
          tNameGetFullDbName(ctx->pName, dbFName);
          
          CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache));
          if (NULL != dbCache) {
            SVgroupInfo vgInfo = {0};
D
dapan1121 已提交
871
            CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, dbCache->vgCache.vgInfo, ctx->pName, &vgInfo));
D
dapan1121 已提交
872 873
          
            ctgDebug("will refresh tbmeta, not supposed to be stb, tbName:%s, flag:%d", tNameGetTableName(ctx->pName), ctx->flag);
D
dapan1121 已提交
874 875 876

            ctx->vgId = vgInfo.vgId;          
            CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, ctx->pName, &vgInfo, NULL, pTask));
D
dapan1121 已提交
877

D
dapan1121 已提交
878
            ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
879 880 881 882 883 884
          } else {
            SBuildUseDBInput input = {0};
          
            tstrncpy(input.db, dbFName, tListLen(input.db));
            input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
          
D
dapan1121 已提交
885
            CTG_ERR_JRET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, pTask));
D
dapan1121 已提交
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
          }

          return TSDB_CODE_SUCCESS;
        }
        
        ctgError("no tbmeta got, tbNmae:%s", tNameGetTableName(ctx->pName));
        ctgRemoveTbMetaFromCache(pCtg, ctx->pName, false);
        
        CTG_ERR_JRET(CTG_ERR_CODE_TABLE_NOT_EXIST);
      }

      if (pTask->msgCtx.lastOut) {
        TSWAP(pTask->msgCtx.out, pTask->msgCtx.lastOut);
        STableMetaOutput* pLastOut = (STableMetaOutput*)pTask->msgCtx.out;
        TSWAP(pLastOut->tbMeta, pOut->tbMeta);
      }
      
      break;
    }
    case TDMT_VND_TABLE_META: {
      STableMetaOutput* pOut = (STableMetaOutput*)pTask->msgCtx.out;
      
      if (CTG_IS_META_NULL(pOut->metaType)) {
        ctgError("no tbmeta got, tbNmae:%s", tNameGetTableName(ctx->pName));
        ctgRemoveTbMetaFromCache(pCtg, ctx->pName, false);
        CTG_ERR_JRET(CTG_ERR_CODE_TABLE_NOT_EXIST);
      }

      if (CTG_FLAG_IS_STB(ctx->flag)) {
        break;
      }
      
      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(ctx->pName));
      
        taosMemoryFreeClear(pOut->tbMeta);
        
D
dapan1121 已提交
923
        CTG_RET(ctgGetTbMetaFromMnode(pCtg, pConn, ctx->pName, NULL, pTask));
D
dapan1121 已提交
924 925 926 927 928 929 930 931
      } else if (CTG_IS_META_BOTH(pOut->metaType)) {
        int32_t exist = 0;
        if (!CTG_FLAG_IS_FORCE_UPDATE(ctx->flag)) {
          CTG_ERR_JRET(ctgTbMetaExistInCache(pCtg, pOut->dbFName, pOut->tbName, &exist));
        }
      
        if (0 == exist) {
          TSWAP(pTask->msgCtx.lastOut, pTask->msgCtx.out);
D
dapan1121 已提交
932
          CTG_RET(ctgGetTbMetaFromMnodeImpl(pCtg, pConn, pOut->dbFName, pOut->tbName, NULL, pTask));
D
dapan1121 已提交
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
        } else {
          taosMemoryFreeClear(pOut->tbMeta);
          
          SET_META_TYPE_CTABLE(pOut->metaType); 
        }
      }
      break;
    }
    default:
      ctgError("invalid reqType %d", reqType);
      CTG_ERR_JRET(TSDB_CODE_INVALID_MSG);
      break;
  }

  STableMetaOutput* pOut = (STableMetaOutput*)pTask->msgCtx.out;

  ctgUpdateTbMetaToCache(pCtg, pOut, false);

  if (CTG_IS_META_BOTH(pOut->metaType)) {
    memcpy(pOut->tbMeta, &pOut->ctbMeta, sizeof(pOut->ctbMeta));
  } else if (CTG_IS_META_CTABLE(pOut->metaType)) {
    SName stbName = *ctx->pName;
    strcpy(stbName.tname, pOut->tbName);
    SCtgTbMetaCtx stbCtx = {0};
    stbCtx.flag = ctx->flag;
    stbCtx.pName = &stbName;
    
    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));

      return TSDB_CODE_SUCCESS;
    }

    memcpy(pOut->tbMeta, &pOut->ctbMeta, sizeof(pOut->ctbMeta));
  }

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

_return:

  if (dbCache) {
D
dapan1121 已提交
976
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
977 978 979 980 981 982 983 984 985 986 987
  }

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

int32_t ctgHandleGetDbVgRsp(SCtgTask* pTask, int32_t reqType, const SDataBuf *pMsg, int32_t rspCode) {
  int32_t code = 0;
  SCtgDbVgCtx* ctx = (SCtgDbVgCtx*)pTask->taskCtx;
  SCatalog* pCtg = pTask->pJob->pCtg; 
D
dapan1121 已提交
988 989

  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));
D
dapan1121 已提交
990 991 992 993

  switch (reqType) {
    case TDMT_MND_USE_DB: {
      SUseDbOutput* pOut = (SUseDbOutput*)pTask->msgCtx.out;
D
dapan1121 已提交
994
      SDBVgInfo* pDb = NULL;
D
dapan1121 已提交
995
      
D
dapan1121 已提交
996 997 998 999
      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 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020

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


_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

int32_t ctgHandleGetTbHashRsp(SCtgTask* pTask, int32_t reqType, const SDataBuf *pMsg, int32_t rspCode) {
  int32_t code = 0;
  SCtgTbHashCtx* ctx = (SCtgTbHashCtx*)pTask->taskCtx;
  SCatalog* pCtg = pTask->pJob->pCtg; 
D
dapan1121 已提交
1021 1022

  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));
D
dapan1121 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034

  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));
      
D
dapan1121 已提交
1035
      CTG_ERR_JRET(ctgUpdateVgroupEnqueue(pCtg, ctx->dbFName, pOut->dbId, pOut->dbVgroup, false));
D
dapan1121 已提交
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
      pOut->dbVgroup = NULL;

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


_return:

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

D
dapan1121 已提交
1054 1055
int32_t ctgHandleGetTbIndexRsp(SCtgTask* pTask, int32_t reqType, const SDataBuf *pMsg, int32_t rspCode) {
  int32_t code = 0;
D
dapan1121 已提交
1056
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));
D
dapan1121 已提交
1057

D
dapan1121 已提交
1058 1059 1060 1061 1062 1063
  STableIndex* pOut = (STableIndex*)pTask->msgCtx.out;
  SArray* pInfo = NULL;
  CTG_ERR_JRET(ctgCloneTableIndex(pOut->pIndex, &pInfo));
  pTask->res = pInfo;

  SCtgTbIndexCtx* ctx = pTask->taskCtx;  
D
dapan1121 已提交
1064
  CTG_ERR_JRET(ctgUpdateTbIndexEnqueue(pTask->pJob->pCtg, (STableIndex**)&pTask->msgCtx.out, false));
D
dapan1121 已提交
1065 1066
  
_return:
D
dapan1121 已提交
1067

X
Xiaoyu Wang 已提交
1068 1069 1070
  if (TSDB_CODE_MND_DB_INDEX_NOT_EXIST == code) {
    code = TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
1071 1072 1073 1074 1075
  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

D
dapan1121 已提交
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
int32_t ctgHandleGetTbCfgRsp(SCtgTask* pTask, int32_t reqType, const SDataBuf *pMsg, int32_t rspCode) {
  int32_t code = 0;
  CTG_ERR_JRET(ctgProcessRspMsg(&pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

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

  ctgHandleTaskEnd(pTask, code);

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

D
dapan1121 已提交
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
int32_t ctgHandleGetDbCfgRsp(SCtgTask* pTask, int32_t reqType, const SDataBuf *pMsg, int32_t rspCode) {
  int32_t code = 0;
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

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

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

D
dapan1121 已提交
1102 1103 1104 1105 1106
int32_t ctgHandleGetDbInfoRsp(SCtgTask* pTask, int32_t reqType, const SDataBuf *pMsg, int32_t rspCode) {
  CTG_RET(TSDB_CODE_APP_ERROR);
}


D
dapan1121 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
int32_t ctgHandleGetQnodeRsp(SCtgTask* pTask, int32_t reqType, const SDataBuf *pMsg, int32_t rspCode) {
  int32_t code = 0;
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

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

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

D
dapan1121 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
int32_t ctgHandleGetDnodeRsp(SCtgTask* pTask, int32_t reqType, const SDataBuf *pMsg, int32_t rspCode) {
  int32_t code = 0;
  CTG_ERR_JRET(ctgProcessRspMsg(&pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

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

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

D
dapan1121 已提交
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
int32_t ctgHandleGetIndexRsp(SCtgTask* pTask, int32_t reqType, const SDataBuf *pMsg, int32_t rspCode) {
  int32_t code = 0;
  CTG_ERR_JRET(ctgProcessRspMsg(pTask->msgCtx.out, reqType, pMsg->pData, pMsg->len, rspCode, pTask->msgCtx.target));

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

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

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

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

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

int32_t ctgHandleGetUserRsp(SCtgTask* pTask, int32_t reqType, const SDataBuf *pMsg, int32_t rspCode) {
  int32_t code = 0;
  SCtgUserCtx* ctx = (SCtgUserCtx*)pTask->taskCtx;
  SCatalog* pCtg = pTask->pJob->pCtg; 
  bool pass = false;
  SGetUserAuthRsp* pOut = (SGetUserAuthRsp*)pTask->msgCtx.out;
D
dapan1121 已提交
1165 1166 1167

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

D
dapan1121 已提交
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
  if (pOut->superAuth) {
    pass = true;
    goto _return;
  }

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

  if (ctx->user.type == AUTH_TYPE_READ && pOut->readDbs && taosHashGet(pOut->readDbs, ctx->user.dbFName, strlen(ctx->user.dbFName))) {
    pass = true;
  } else if (ctx->user.type == AUTH_TYPE_WRITE && pOut->writeDbs && taosHashGet(pOut->writeDbs, ctx->user.dbFName, strlen(ctx->user.dbFName))) {
    pass = true;
  }

_return:

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

D
dapan1121 已提交
1195
  ctgUpdateUserEnqueue(pCtg, pOut, false);
D
dapan1121 已提交
1196
  taosMemoryFreeClear(pTask->msgCtx.out);
D
dapan1121 已提交
1197 1198 1199 1200 1201 1202

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

D
dapan1121 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
int32_t ctgHandleGetSvrVerRsp(SCtgTask* pTask, int32_t reqType, const SDataBuf *pMsg, int32_t rspCode) {
  int32_t code = 0;

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

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

  ctgHandleTaskEnd(pTask, code);

  CTG_RET(code);
}

D
dapan1121 已提交
1217 1218
int32_t ctgAsyncRefreshTbMeta(SCtgTask *pTask) {
  SCatalog* pCtg = pTask->pJob->pCtg; 
D
dapan1121 已提交
1219
  SRequestConnInfo* pConn = &pTask->pJob->conn;
D
dapan1121 已提交
1220 1221 1222 1223 1224 1225
  int32_t code = 0;
  SCtgTbMetaCtx* ctx = (SCtgTbMetaCtx*)pTask->taskCtx;

  if (CTG_FLAG_IS_SYS_DB(ctx->flag)) {
    ctgDebug("will refresh sys db tbmeta, tbName:%s", tNameGetTableName(ctx->pName));

D
dapan1121 已提交
1226
    CTG_RET(ctgGetTbMetaFromMnodeImpl(pCtg, pConn, (char *)ctx->pName->dbname, (char *)ctx->pName->tname, NULL, pTask));
D
dapan1121 已提交
1227 1228 1229 1230 1231 1232
  }

  if (CTG_FLAG_IS_STB(ctx->flag)) {
    ctgDebug("will refresh tbmeta, supposed to be stb, tbName:%s", tNameGetTableName(ctx->pName));

    // if get from mnode failed, will not try vnode
D
dapan1121 已提交
1233
    CTG_RET(ctgGetTbMetaFromMnode(pCtg, pConn, ctx->pName, NULL, pTask));
D
dapan1121 已提交
1234 1235 1236 1237 1238 1239 1240
  }

  SCtgDBCache *dbCache = NULL;
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(ctx->pName, dbFName);
  
  CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, dbFName, &dbCache));
D
dapan1121 已提交
1241
  if (dbCache) {
D
dapan1121 已提交
1242
    SVgroupInfo vgInfo = {0};
D
dapan1121 已提交
1243
    CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, dbCache->vgCache.vgInfo, ctx->pName, &vgInfo));
D
dapan1121 已提交
1244 1245 1246

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

D
dapan1121 已提交
1247 1248
    ctx->vgId = vgInfo.vgId;
    CTG_ERR_JRET(ctgGetTbMetaFromVnode(pCtg, pConn, ctx->pName, &vgInfo, NULL, pTask));
D
dapan1121 已提交
1249 1250 1251 1252 1253 1254
  } else {
    SBuildUseDBInput input = {0};

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

D
dapan1121 已提交
1255
    CTG_ERR_JRET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, pTask));
D
dapan1121 已提交
1256 1257 1258 1259 1260
  }

_return:

  if (dbCache) {
D
dapan1121 已提交
1261
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1262 1263 1264 1265 1266 1267 1268
  }

  CTG_RET(code);
}

int32_t ctgLaunchGetTbMetaTask(SCtgTask *pTask) {
  SCatalog* pCtg = pTask->pJob->pCtg; 
D
dapan1121 已提交
1269
  SRequestConnInfo* pConn = &pTask->pJob->conn;
D
dapan1121 已提交
1270

D
dapan1121 已提交
1271
  CTG_ERR_RET(ctgGetTbMetaFromCache(pCtg, pConn, (SCtgTbMetaCtx*)pTask->taskCtx, (STableMeta**)&pTask->res));
D
dapan1121 已提交
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
  if (pTask->res) {
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }

  CTG_ERR_RET(ctgAsyncRefreshTbMeta(pTask));

  return TSDB_CODE_SUCCESS;
}

int32_t ctgLaunchGetDbVgTask(SCtgTask *pTask) {
  int32_t code = 0;
  SCatalog* pCtg = pTask->pJob->pCtg; 
D
dapan1121 已提交
1285
  SRequestConnInfo* pConn = &pTask->pJob->conn;
D
dapan1121 已提交
1286 1287 1288 1289 1290
  SCtgDBCache *dbCache = NULL;
  SCtgDbVgCtx* pCtx = (SCtgDbVgCtx*)pTask->taskCtx;
  
  CTG_ERR_RET(ctgAcquireVgInfoFromCache(pCtg, pCtx->dbFName, &dbCache));
  if (NULL != dbCache) {
D
dapan1121 已提交
1291
    CTG_ERR_JRET(ctgGenerateVgList(pCtg, dbCache->vgCache.vgInfo->vgHash, (SArray**)&pTask->res));
D
dapan1121 已提交
1292 1293 1294

    ctgReleaseVgInfoToCache(pCtg, dbCache);
    dbCache = NULL;
D
dapan1121 已提交
1295 1296 1297 1298 1299 1300 1301 1302
    
    CTG_ERR_JRET(ctgHandleTaskEnd(pTask, 0));
  } else {
    SBuildUseDBInput input = {0};
    
    tstrncpy(input.db, pCtx->dbFName, tListLen(input.db));
    input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
    
D
dapan1121 已提交
1303
    CTG_ERR_RET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, pTask));
D
dapan1121 已提交
1304 1305 1306 1307 1308
  }

_return:

  if (dbCache) {
D
dapan1121 已提交
1309
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1310 1311 1312 1313 1314 1315 1316 1317
  }

  CTG_RET(code);
}

int32_t ctgLaunchGetTbHashTask(SCtgTask *pTask) {
  int32_t code = 0;
  SCatalog* pCtg = pTask->pJob->pCtg; 
D
dapan1121 已提交
1318
  SRequestConnInfo* pConn = &pTask->pJob->conn;
D
dapan1121 已提交
1319 1320 1321 1322 1323 1324 1325 1326 1327
  SCtgDBCache *dbCache = NULL;
  SCtgTbHashCtx* pCtx = (SCtgTbHashCtx*)pTask->taskCtx;
  
  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 已提交
1328
    CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pCtg, dbCache->vgCache.vgInfo, pCtx->pName, (SVgroupInfo*)pTask->res));
D
dapan1121 已提交
1329 1330 1331

    ctgReleaseVgInfoToCache(pCtg, dbCache);
    dbCache = NULL;
D
dapan1121 已提交
1332 1333 1334 1335 1336 1337 1338 1339
    
    CTG_ERR_JRET(ctgHandleTaskEnd(pTask, 0));
  } else {
    SBuildUseDBInput input = {0};
    
    tstrncpy(input.db, pCtx->dbFName, tListLen(input.db));
    input.vgVersion = CTG_DEFAULT_INVALID_VERSION;
    
D
dapan1121 已提交
1340
    CTG_ERR_RET(ctgGetDBVgInfoFromMnode(pCtg, pConn, &input, NULL, pTask));
D
dapan1121 已提交
1341 1342 1343 1344 1345
  }

_return:

  if (dbCache) {
D
dapan1121 已提交
1346
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1347 1348 1349 1350 1351
  }

  CTG_RET(code);
}

D
dapan1121 已提交
1352 1353 1354
int32_t ctgLaunchGetTbIndexTask(SCtgTask *pTask) {
  int32_t code = 0;
  SCatalog* pCtg = pTask->pJob->pCtg; 
D
dapan1121 已提交
1355
  SRequestConnInfo* pConn = &pTask->pJob->conn;
D
dapan1121 已提交
1356
  SCtgTbIndexCtx* pCtx = (SCtgTbIndexCtx*)pTask->taskCtx;
D
dapan1121 已提交
1357 1358 1359 1360 1361 1362 1363 1364 1365
  SArray* pRes = NULL;

  CTG_ERR_RET(ctgReadTbIndexFromCache(pCtg, pCtx->pName, &pRes));
  if (pRes) {
    pTask->res = pRes;
    
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
1366
  
D
dapan1121 已提交
1367
  CTG_ERR_RET(ctgGetTbIndexFromMnode(pCtg, pConn, pCtx->pName, NULL, pTask));
D
dapan1121 已提交
1368 1369 1370
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
int32_t ctgLaunchGetTbCfgTask(SCtgTask *pTask) {
  int32_t code = 0;
  SCatalog* pCtg = pTask->pJob->pCtg; 
  SRequestConnInfo* pConn = &pTask->pJob->conn;
  SCtgTbCfgCtx* pCtx = (SCtgTbCfgCtx*)pTask->taskCtx;
  SArray* pRes = NULL;
  char dbFName[TSDB_DB_FNAME_LEN];
  tNameGetFullDbName(pCtx->pName, dbFName);

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

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

D
dapan1121 已提交
1413

D
dapan1121 已提交
1414 1415
int32_t ctgLaunchGetQnodeTask(SCtgTask *pTask) {
  SCatalog* pCtg = pTask->pJob->pCtg; 
D
dapan1121 已提交
1416
  SRequestConnInfo* pConn = &pTask->pJob->conn;
D
dapan1121 已提交
1417

D
dapan1121 已提交
1418
  CTG_ERR_RET(ctgGetQnodeListFromMnode(pCtg, pConn, NULL, pTask));
D
dapan1121 已提交
1419 1420 1421
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1422 1423 1424 1425 1426 1427 1428 1429 1430
int32_t ctgLaunchGetDnodeTask(SCtgTask *pTask) {
  SCatalog* pCtg = pTask->pJob->pCtg; 
  SRequestConnInfo* pConn = &pTask->pJob->conn;

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


D
dapan1121 已提交
1431 1432
int32_t ctgLaunchGetDbCfgTask(SCtgTask *pTask) {
  SCatalog* pCtg = pTask->pJob->pCtg; 
D
dapan1121 已提交
1433
  SRequestConnInfo* pConn = &pTask->pJob->conn;
D
dapan1121 已提交
1434 1435
  SCtgDbCfgCtx* pCtx = (SCtgDbCfgCtx*)pTask->taskCtx;

D
dapan1121 已提交
1436
  CTG_ERR_RET(ctgGetDBCfgFromMnode(pCtg, pConn, pCtx->dbFName, NULL, pTask));
D
dapan1121 已提交
1437 1438 1439 1440

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
int32_t ctgLaunchGetDbInfoTask(SCtgTask *pTask) {
  int32_t code = 0;
  SCatalog* pCtg = pTask->pJob->pCtg; 
  SCtgDBCache *dbCache = NULL;
  SCtgDbInfoCtx* pCtx = (SCtgDbInfoCtx*)pTask->taskCtx;

  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 已提交
1455
    pInfo->vgVer = dbCache->vgCache.vgInfo->vgVersion;
D
dapan1121 已提交
1456
    pInfo->dbId = dbCache->dbId;
D
dapan1121 已提交
1457
    pInfo->tbNum = dbCache->vgCache.vgInfo->numOfTable;
D
dapan1121 已提交
1458 1459 1460

    ctgReleaseVgInfoToCache(pCtg, dbCache);
    dbCache = NULL;
D
dapan1121 已提交
1461 1462 1463 1464 1465 1466 1467 1468 1469
  } else {
    pInfo->vgVer = CTG_DEFAULT_INVALID_VERSION;
  }

  CTG_ERR_JRET(ctgHandleTaskEnd(pTask, 0));

_return:

  if (dbCache) {
D
dapan1121 已提交
1470
    ctgReleaseVgInfoToCache(pCtg, dbCache);
D
dapan1121 已提交
1471 1472 1473 1474 1475
  }

  CTG_RET(code);
}

D
dapan1121 已提交
1476 1477
int32_t ctgLaunchGetIndexTask(SCtgTask *pTask) {
  SCatalog* pCtg = pTask->pJob->pCtg; 
D
dapan1121 已提交
1478
  SRequestConnInfo* pConn = &pTask->pJob->conn;
D
dapan1121 已提交
1479 1480
  SCtgIndexCtx* pCtx = (SCtgIndexCtx*)pTask->taskCtx;

D
dapan1121 已提交
1481
  CTG_ERR_RET(ctgGetIndexInfoFromMnode(pCtg, pConn, pCtx->indexFName, NULL, pTask));
D
dapan1121 已提交
1482 1483 1484 1485 1486 1487

  return TSDB_CODE_SUCCESS;
}

int32_t ctgLaunchGetUdfTask(SCtgTask *pTask) {
  SCatalog* pCtg = pTask->pJob->pCtg; 
D
dapan1121 已提交
1488
  SRequestConnInfo* pConn = &pTask->pJob->conn;
D
dapan1121 已提交
1489 1490
  SCtgUdfCtx* pCtx = (SCtgUdfCtx*)pTask->taskCtx;

D
dapan1121 已提交
1491
  CTG_ERR_RET(ctgGetUdfInfoFromMnode(pCtg, pConn, pCtx->udfName, NULL, pTask));
D
dapan1121 已提交
1492 1493 1494 1495 1496 1497

  return TSDB_CODE_SUCCESS;
}

int32_t ctgLaunchGetUserTask(SCtgTask *pTask) {
  SCatalog* pCtg = pTask->pJob->pCtg; 
D
dapan1121 已提交
1498
  SRequestConnInfo* pConn = &pTask->pJob->conn;
D
dapan1121 已提交
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
  SCtgUserCtx* pCtx = (SCtgUserCtx*)pTask->taskCtx;
  bool inCache = false;
  bool pass = false;
  
  CTG_ERR_RET(ctgChkAuthFromCache(pCtg, pCtx->user.user, pCtx->user.dbFName, pCtx->user.type, &inCache, &pass));
  if (inCache) {
    pTask->res = taosMemoryCalloc(1, sizeof(bool));
    if (NULL == pTask->res) {
      CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
    *(bool*)pTask->res = pass;
    
    CTG_ERR_RET(ctgHandleTaskEnd(pTask, 0));
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
1515
  CTG_ERR_RET(ctgGetUserDbAuthFromMnode(pCtg, pConn, pCtx->user.user, NULL, pTask));
D
dapan1121 已提交
1516 1517 1518 1519

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1520 1521 1522 1523 1524 1525 1526 1527 1528
int32_t ctgLaunchGetSvrVerTask(SCtgTask *pTask) {
  SCatalog* pCtg = pTask->pJob->pCtg; 
  SRequestConnInfo* pConn = &pTask->pJob->conn;

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

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1529 1530 1531 1532 1533 1534 1535 1536
int32_t ctgRelaunchGetTbMetaTask(SCtgTask *pTask) {
  ctgResetTbMetaTask(pTask);

  CTG_ERR_RET(ctgLaunchGetTbMetaTask(pTask));

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
int32_t ctgGetTbCfgCb(SCtgTask *pTask) {
  int32_t code = 0;
  
  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;
    
    pCtx->pVgInfo = taosMemoryCalloc(1, sizeof(SVgroupInfo));
    CTG_ERR_JRET(ctgGetVgInfoFromHashValue(pTask->pJob->pCtg, pDb, pCtx->pName, pCtx->pVgInfo));
  }

  CTG_RET(ctgLaunchGetTbCfgTask(pTask));

_return:

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

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

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

  return TSDB_CODE_SUCCESS;
}


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

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

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


D
dapan1121 已提交
1589
SCtgAsyncFps gCtgAsyncFps[] = {
D
dapan1121 已提交
1590
  {ctgInitGetQnodeTask,   ctgLaunchGetQnodeTask,   ctgHandleGetQnodeRsp,   ctgDumpQnodeRes,   NULL,               NULL},
D
dapan1121 已提交
1591
  {ctgInitGetDnodeTask,   ctgLaunchGetDnodeTask,   ctgHandleGetDnodeRsp,   ctgDumpDnodeRes,   NULL,               NULL},
D
dapan1121 已提交
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
  {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},
D
dapan1121 已提交
1602
  {ctgInitGetSvrVerTask,  ctgLaunchGetSvrVerTask,  ctgHandleGetSvrVerRsp,  ctgDumpSvrVer,     NULL,               NULL},
D
dapan1121 已提交
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
};

int32_t ctgMakeAsyncRes(SCtgJob *pJob) {
  int32_t code = 0;
  int32_t taskNum = taosArrayGetSize(pJob->pTasks);
  
  for (int32_t i = 0; i < taskNum; ++i) {
    SCtgTask *pTask = taosArrayGet(pJob->pTasks, i);
    CTG_ERR_RET((*gCtgAsyncFps[pTask->type].dumpResFp)(pTask));
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
int32_t ctgSearchExistingTask(SCtgJob *pJob, CTG_TASK_TYPE type, void* param, int32_t* taskId) {
  bool      equal = false;
  SCtgTask* pTask = NULL;
  int32_t   code = 0;
  
  CTG_LOCK(CTG_READ, &pJob->taskLock);
  
  int32_t taskNum = taosArrayGetSize(pJob->pTasks);
  for (int32_t i = 0; i < taskNum; ++i) {
    pTask = taosArrayGet(pJob->pTasks, i);
    if (type !=  pTask->type) {
      continue;
    }
    
    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);
}

int32_t ctgSetSubTaskCb(SCtgTask *pSub, SCtgTask *pTask) {
  int32_t code = 0;
  
  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));
    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);

  CTG_RET(code);  
}


int32_t ctgLaunchSubTask(SCtgTask *pTask, CTG_TASK_TYPE type, ctgSubTaskCbFp fp, void* param) {
  SCtgJob* pJob = pTask->pJob;
  int32_t  subTaskId = -1;
  bool     newTask = false;

  ctgClearSubTaskRes(&pTask->subRes);
  pTask->subRes.type = type;
  pTask->subRes.fp = fp;
  
  CTG_ERR_RET(ctgSearchExistingTask(pJob, type, param, &subTaskId));
  if (subTaskId < 0) {
    CTG_ERR_RET(ctgInitTask(pJob, type, param, &subTaskId));
    newTask = true;
  }
  
  SCtgTask* pSub = taosArrayGet(pJob->pTasks, subTaskId);

  CTG_ERR_RET(ctgSetSubTaskCb(pSub, pTask));

  if (newTask) {
    CTG_ERR_RET((*gCtgAsyncFps[pSub->type].launchFp)(pSub));
    pSub->status = CTG_TASK_LAUNCHED;
  }

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
1697 1698 1699 1700 1701 1702 1703

int32_t ctgLaunchJob(SCtgJob *pJob) {
  int32_t taskNum = taosArrayGetSize(pJob->pTasks);
  
  for (int32_t i = 0; i < taskNum; ++i) {
    SCtgTask *pTask = taosArrayGet(pJob->pTasks, i);

D
dapan1121 已提交
1704
    qDebug("QID:0x%" PRIx64 " ctg launch [%dth] task", pJob->queryId, pTask->taskId);
D
dapan1121 已提交
1705
    CTG_ERR_RET((*gCtgAsyncFps[pTask->type].launchFp)(pTask));
D
dapan1121 已提交
1706
    pTask->status = CTG_TASK_LAUNCHED;
D
dapan1121 已提交
1707 1708
  }

D
dapan1121 已提交
1709 1710 1711 1712 1713 1714
  if (taskNum <= 0) {
    qDebug("QID:0x%" PRIx64 " ctg call user callback with rsp %s", pJob->queryId, tstrerror(pJob->jobResCode));
    
    taosAsyncExec(ctgCallUserCb, pJob, NULL);
  }

D
dapan1121 已提交
1715 1716 1717 1718 1719
  return TSDB_CODE_SUCCESS;
}