qwUtil.c 15.8 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6
#include "dataSinkMgt.h"
#include "executor.h"
#include "planner.h"
#include "query.h"
#include "qwInt.h"
#include "qwMsg.h"
L
Liu Jicong 已提交
7
#include "qworker.h"
D
dapan1121 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#include "tcommon.h"
#include "tmsg.h"
#include "tname.h"

char *qwPhaseStr(int32_t phase) {
  switch (phase) {
    case QW_PHASE_PRE_QUERY:
      return "PRE_QUERY";
    case QW_PHASE_POST_QUERY:
      return "POST_QUERY";
    case QW_PHASE_PRE_FETCH:
      return "PRE_FETCH";
    case QW_PHASE_POST_FETCH:
      return "POST_FETCH";
    case QW_PHASE_PRE_CQUERY:
      return "PRE_CQUERY";
    case QW_PHASE_POST_CQUERY:
      return "POST_CQUERY";
    default:
      break;
  }

  return "UNKNOWN";
}

char *qwBufStatusStr(int32_t bufStatus) {
  switch (bufStatus) {
    case DS_BUF_LOW:
      return "LOW";
    case DS_BUF_FULL:
      return "FULL";
    case DS_BUF_EMPTY:
      return "EMPTY";
    default:
      break;
  }

  return "UNKNOWN";
}

int32_t qwSetTaskStatus(QW_FPARAMS_DEF, SQWTaskStatus *task, int8_t status) {
  int32_t code = 0;
  int8_t  origStatus = 0;
  bool    ignore = false;

  while (true) {
    origStatus = atomic_load_8(&task->status);

    QW_ERR_RET(qwDbgValidateStatus(QW_FPARAMS(), origStatus, status, &ignore));
    if (ignore) {
      break;
    }

    if (origStatus != atomic_val_compare_exchange_8(&task->status, origStatus, status)) {
      continue;
    }

    QW_TASK_DLOG("task status updated from %s to %s", jobTaskStatusStr(origStatus), jobTaskStatusStr(status));

    break;
  }

  return TSDB_CODE_SUCCESS;
}

int32_t qwAddSchedulerImpl(SQWorker *mgmt, uint64_t sId, int32_t rwType) {
  SQWSchStatus newSch = {0};
  newSch.tasksHash =
      taosHashInit(mgmt->cfg.maxSchTaskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
dengyihao's avatar
dengyihao 已提交
77 78
  newSch.hbBrokenTs = taosGetTimestampMs();

D
dapan1121 已提交
79 80
  if (NULL == newSch.tasksHash) {
    QW_SCH_ELOG("taosHashInit %d failed", mgmt->cfg.maxSchTaskNum);
S
Shengliang Guan 已提交
81
    QW_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
82 83 84 85 86 87 88 89 90 91
  }

  QW_LOCK(QW_WRITE, &mgmt->schLock);
  int32_t code = taosHashPut(mgmt->schHash, &sId, sizeof(sId), &newSch, sizeof(newSch));
  if (0 != code) {
    if (!HASH_NODE_EXIST(code)) {
      QW_UNLOCK(QW_WRITE, &mgmt->schLock);

      QW_SCH_ELOG("taosHashPut new sch to scheduleHash failed, errno:%d", errno);
      taosHashCleanup(newSch.tasksHash);
S
Shengliang Guan 已提交
92
      QW_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    }

    taosHashCleanup(newSch.tasksHash);
  }
  QW_UNLOCK(QW_WRITE, &mgmt->schLock);

  return TSDB_CODE_SUCCESS;
}

int32_t qwAcquireSchedulerImpl(SQWorker *mgmt, uint64_t sId, int32_t rwType, SQWSchStatus **sch, int32_t nOpt) {
  while (true) {
    QW_LOCK(rwType, &mgmt->schLock);
    *sch = taosHashGet(mgmt->schHash, &sId, sizeof(sId));
    if (NULL == (*sch)) {
      QW_UNLOCK(rwType, &mgmt->schLock);

      if (QW_NOT_EXIST_ADD == nOpt) {
        QW_ERR_RET(qwAddSchedulerImpl(mgmt, sId, rwType));

        nOpt = QW_NOT_EXIST_RET_ERR;

        continue;
      } else if (QW_NOT_EXIST_RET_ERR == nOpt) {
        QW_RET(TSDB_CODE_QRY_SCH_NOT_EXIST);
      } else {
        QW_SCH_ELOG("unknown notExistOpt:%d", nOpt);
S
Shengliang Guan 已提交
119
        QW_ERR_RET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
      }
    }

    break;
  }

  return TSDB_CODE_SUCCESS;
}

int32_t qwAcquireAddScheduler(SQWorker *mgmt, uint64_t sId, int32_t rwType, SQWSchStatus **sch) {
  return qwAcquireSchedulerImpl(mgmt, sId, rwType, sch, QW_NOT_EXIST_ADD);
}

int32_t qwAcquireScheduler(SQWorker *mgmt, uint64_t sId, int32_t rwType, SQWSchStatus **sch) {
  return qwAcquireSchedulerImpl(mgmt, sId, rwType, sch, QW_NOT_EXIST_RET_ERR);
}

void qwReleaseScheduler(int32_t rwType, SQWorker *mgmt) { QW_UNLOCK(rwType, &mgmt->schLock); }

int32_t qwAcquireTaskStatus(QW_FPARAMS_DEF, int32_t rwType, SQWSchStatus *sch, SQWTaskStatus **task) {
D
dapan1121 已提交
140 141
  char id[sizeof(qId) + sizeof(tId) + sizeof(eId)] = {0};
  QW_SET_QTID(id, qId, tId, eId);
D
dapan1121 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155

  QW_LOCK(rwType, &sch->tasksLock);
  *task = taosHashGet(sch->tasksHash, id, sizeof(id));
  if (NULL == (*task)) {
    QW_UNLOCK(rwType, &sch->tasksLock);
    QW_ERR_RET(TSDB_CODE_QRY_TASK_NOT_EXIST);
  }

  return TSDB_CODE_SUCCESS;
}

int32_t qwAddTaskStatusImpl(QW_FPARAMS_DEF, SQWSchStatus *sch, int32_t rwType, int32_t status, SQWTaskStatus **task) {
  int32_t code = 0;

D
dapan1121 已提交
156 157
  char id[sizeof(qId) + sizeof(tId) + sizeof(eId)] = {0};
  QW_SET_QTID(id, qId, tId, eId);
D
dapan1121 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

  SQWTaskStatus ntask = {0};
  ntask.status = status;
  ntask.refId = rId;

  QW_LOCK(QW_WRITE, &sch->tasksLock);
  code = taosHashPut(sch->tasksHash, id, sizeof(id), &ntask, sizeof(ntask));
  if (0 != code) {
    QW_UNLOCK(QW_WRITE, &sch->tasksLock);
    if (HASH_NODE_EXIST(code)) {
      if (rwType && task) {
        QW_RET(qwAcquireTaskStatus(QW_FPARAMS(), rwType, sch, task));
      } else {
        QW_TASK_ELOG("task status already exist, newStatus:%s", jobTaskStatusStr(status));
        QW_ERR_RET(TSDB_CODE_QRY_TASK_ALREADY_EXIST);
      }
    } else {
      QW_TASK_ELOG("taosHashPut to tasksHash failed, error:%x - %s", code, tstrerror(code));
S
Shengliang Guan 已提交
176
      QW_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    }
  }
  QW_UNLOCK(QW_WRITE, &sch->tasksLock);

  QW_TASK_DLOG("task status added, newStatus:%s", jobTaskStatusStr(status));

  if (rwType && task) {
    QW_ERR_RET(qwAcquireTaskStatus(QW_FPARAMS(), rwType, sch, task));
  }

  return TSDB_CODE_SUCCESS;
}

int32_t qwAddTaskStatus(QW_FPARAMS_DEF, int32_t status) {
  SQWSchStatus *tsch = NULL;
  int32_t       code = 0;
  QW_ERR_RET(qwAcquireAddScheduler(mgmt, sId, QW_READ, &tsch));

  QW_ERR_JRET(qwAddTaskStatusImpl(QW_FPARAMS(), tsch, 0, status, NULL));

_return:

  qwReleaseScheduler(QW_READ, mgmt);

  QW_RET(code);
}

int32_t qwAddAcquireTaskStatus(QW_FPARAMS_DEF, int32_t rwType, SQWSchStatus *sch, int32_t status,
                               SQWTaskStatus **task) {
  return qwAddTaskStatusImpl(QW_FPARAMS(), sch, rwType, status, task);
}

void qwReleaseTaskStatus(int32_t rwType, SQWSchStatus *sch) { QW_UNLOCK(rwType, &sch->tasksLock); }

int32_t qwAcquireTaskCtx(QW_FPARAMS_DEF, SQWTaskCtx **ctx) {
D
dapan1121 已提交
212 213
  char id[sizeof(qId) + sizeof(tId) + sizeof(eId)] = {0};
  QW_SET_QTID(id, qId, tId, eId);
D
dapan1121 已提交
214 215

  *ctx = taosHashAcquire(mgmt->ctxHash, id, sizeof(id));
216
  int8_t nodeStopped = atomic_load_8(&mgmt->nodeStopped);
D
dapan1121 已提交
217
  if (NULL == (*ctx)) {
218 219 220 221 222 223 224
    if (!nodeStopped) {
      QW_TASK_DLOG_E("task ctx not exist, may be dropped");
      QW_ERR_RET(TSDB_CODE_QRY_TASK_CTX_NOT_EXIST);
    } else {
      QW_TASK_DLOG_E("node stopped");
      QW_ERR_RET(TSDB_CODE_VND_STOPPED);
    }
D
dapan1121 已提交
225 226 227 228 229 230
  }

  return TSDB_CODE_SUCCESS;
}

int32_t qwGetTaskCtx(QW_FPARAMS_DEF, SQWTaskCtx **ctx) {
D
dapan1121 已提交
231 232
  char id[sizeof(qId) + sizeof(tId) + sizeof(eId)] = {0};
  QW_SET_QTID(id, qId, tId, eId);
D
dapan1121 已提交
233 234

  *ctx = taosHashGet(mgmt->ctxHash, id, sizeof(id));
235 236
  int8_t nodeStopped = atomic_load_8(&mgmt->nodeStopped);

D
dapan1121 已提交
237
  if (NULL == (*ctx)) {
238 239 240 241 242 243 244
    if (!nodeStopped) {
      QW_TASK_DLOG_E("task ctx not exist, may be dropped");
      QW_ERR_RET(TSDB_CODE_QRY_TASK_CTX_NOT_EXIST);
    } else {
      QW_TASK_DLOG_E("node stopped");
      QW_ERR_RET(TSDB_CODE_VND_STOPPED);
    }
D
dapan1121 已提交
245 246 247 248 249 250
  }

  return TSDB_CODE_SUCCESS;
}

int32_t qwAddTaskCtxImpl(QW_FPARAMS_DEF, bool acquire, SQWTaskCtx **ctx) {
D
dapan1121 已提交
251 252
  char id[sizeof(qId) + sizeof(tId) + sizeof(eId)] = {0};
  QW_SET_QTID(id, qId, tId, eId);
D
dapan1121 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

  SQWTaskCtx nctx = {0};

  int32_t code = taosHashPut(mgmt->ctxHash, id, sizeof(id), &nctx, sizeof(SQWTaskCtx));
  if (0 != code) {
    if (HASH_NODE_EXIST(code)) {
      if (acquire && ctx) {
        QW_RET(qwAcquireTaskCtx(QW_FPARAMS(), ctx));
      } else if (ctx) {
        QW_RET(qwGetTaskCtx(QW_FPARAMS(), ctx));
      } else {
        QW_TASK_ELOG_E("task ctx already exist");
        QW_ERR_RET(TSDB_CODE_QRY_TASK_ALREADY_EXIST);
      }
    } else {
      QW_TASK_ELOG("taosHashPut to ctxHash failed, error:%x", code);
S
Shengliang Guan 已提交
269
      QW_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    }
  }

  if (acquire && ctx) {
    QW_RET(qwAcquireTaskCtx(QW_FPARAMS(), ctx));
  } else if (ctx) {
    QW_RET(qwGetTaskCtx(QW_FPARAMS(), ctx));
  }

  return TSDB_CODE_SUCCESS;
}

int32_t qwAddTaskCtx(QW_FPARAMS_DEF) { QW_RET(qwAddTaskCtxImpl(QW_FPARAMS(), false, NULL)); }

int32_t qwAddAcquireTaskCtx(QW_FPARAMS_DEF, SQWTaskCtx **ctx) { return qwAddTaskCtxImpl(QW_FPARAMS(), true, ctx); }

void qwReleaseTaskCtx(SQWorker *mgmt, void *ctx) { taosHashRelease(mgmt->ctxHash, ctx); }

D
dapan1121 已提交
288
void qwFreeTaskHandle(qTaskInfo_t *taskHandle) {
D
dapan1121 已提交
289 290 291 292
  // Note: free/kill may in RC
  qTaskInfo_t otaskHandle = atomic_load_ptr(taskHandle);
  if (otaskHandle && atomic_val_compare_exchange_ptr(taskHandle, otaskHandle, NULL)) {
    qDestroyTask(otaskHandle);
H
Haojun Liao 已提交
293
    qDebug("task handle destroyed");
D
dapan1121 已提交
294 295 296
  }
}

D
dapan1121 已提交
297
int32_t qwKillTaskHandle(SQWTaskCtx *ctx, int32_t rspCode) {
D
dapan1121 已提交
298
  int32_t code = 0;
dengyihao's avatar
dengyihao 已提交
299

D
dapan1121 已提交
300 301 302
  // Note: free/kill may in RC
  qTaskInfo_t taskHandle = atomic_load_ptr(&ctx->taskHandle);
  if (taskHandle && atomic_val_compare_exchange_ptr(&ctx->taskHandle, taskHandle, NULL)) {
D
dapan1121 已提交
303
    qDebug("start to kill task");
D
dapan1121 已提交
304
    code = qAsyncKillTask(taskHandle, rspCode);
D
dapan1121 已提交
305 306 307 308 309 310
    atomic_store_ptr(&ctx->taskHandle, taskHandle);
  }

  QW_RET(code);
}

D
dapan1121 已提交
311
void qwFreeTaskCtx(SQWTaskCtx *ctx) {
D
dapan1121 已提交
312 313 314
  if (ctx->ctrlConnInfo.handle) {
    tmsgReleaseHandle(&ctx->ctrlConnInfo, TAOS_CONN_SERVER);
  }
H
Hongze Cheng 已提交
315

D
dapan1121 已提交
316 317 318 319 320
  ctx->ctrlConnInfo.handle = NULL;
  ctx->ctrlConnInfo.refId = -1;

  // NO need to release dataConnInfo

D
dapan1121 已提交
321
  qwFreeTaskHandle(&ctx->taskHandle);
D
dapan1121 已提交
322 323 324 325

  if (ctx->sinkHandle) {
    dsDestroyDataSinker(ctx->sinkHandle);
    ctx->sinkHandle = NULL;
H
Haojun Liao 已提交
326
    qDebug("sink handle destroyed");
D
dapan1121 已提交
327 328 329 330
  }
}

int32_t qwDropTaskCtx(QW_FPARAMS_DEF) {
D
dapan1121 已提交
331 332
  char id[sizeof(qId) + sizeof(tId) + sizeof(eId)] = {0};
  QW_SET_QTID(id, qId, tId, eId);
D
dapan1121 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
  SQWTaskCtx octx;

  SQWTaskCtx *ctx = taosHashGet(mgmt->ctxHash, id, sizeof(id));
  if (NULL == ctx) {
    QW_ERR_RET(TSDB_CODE_QRY_TASK_CTX_NOT_EXIST);
  }

  octx = *ctx;

  atomic_store_ptr(&ctx->taskHandle, NULL);
  atomic_store_ptr(&ctx->sinkHandle, NULL);

  QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_DROP);

  if (taosHashRemove(mgmt->ctxHash, id, sizeof(id))) {
    QW_TASK_ELOG_E("taosHashRemove from ctx hash failed");
    QW_ERR_RET(TSDB_CODE_QRY_TASK_CTX_NOT_EXIST);
  }

D
dapan1121 已提交
352
  qwFreeTaskCtx(&octx);
D
dapan1121 已提交
353 354 355 356 357 358 359 360 361 362 363

  QW_TASK_DLOG_E("task ctx dropped");

  return TSDB_CODE_SUCCESS;
}

int32_t qwDropTaskStatus(QW_FPARAMS_DEF) {
  SQWSchStatus  *sch = NULL;
  SQWTaskStatus *task = NULL;
  int32_t        code = 0;

D
dapan1121 已提交
364 365
  char id[sizeof(qId) + sizeof(tId) + sizeof(eId)] = {0};
  QW_SET_QTID(id, qId, tId, eId);
D
dapan1121 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380

  if (qwAcquireScheduler(mgmt, sId, QW_WRITE, &sch)) {
    QW_TASK_WLOG_E("scheduler does not exist");
    return TSDB_CODE_SUCCESS;
  }

  if (qwAcquireTaskStatus(QW_FPARAMS(), QW_WRITE, sch, &task)) {
    qwReleaseScheduler(QW_WRITE, mgmt);

    QW_TASK_WLOG_E("task does not exist");
    return TSDB_CODE_SUCCESS;
  }

  if (taosHashRemove(sch->tasksHash, id, sizeof(id))) {
    QW_TASK_ELOG_E("taosHashRemove task from hash failed");
S
Shengliang Guan 已提交
381
    QW_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
  }

  QW_TASK_DLOG_E("task status dropped");

_return:

  if (task) {
    qwReleaseTaskStatus(QW_WRITE, sch);
  }
  qwReleaseScheduler(QW_WRITE, mgmt);

  QW_RET(code);
}

int32_t qwUpdateTaskStatus(QW_FPARAMS_DEF, int8_t status) {
  SQWSchStatus  *sch = NULL;
  SQWTaskStatus *task = NULL;
  int32_t        code = 0;

  QW_ERR_RET(qwAcquireScheduler(mgmt, sId, QW_READ, &sch));
  QW_ERR_JRET(qwAcquireTaskStatus(QW_FPARAMS(), QW_READ, sch, &task));

  QW_ERR_JRET(qwSetTaskStatus(QW_FPARAMS(), task, status));

_return:

  if (task) {
    qwReleaseTaskStatus(QW_READ, sch);
  }
  qwReleaseScheduler(QW_READ, mgmt);

  QW_RET(code);
}

int32_t qwDropTask(QW_FPARAMS_DEF) {
  QW_ERR_RET(qwDropTaskStatus(QW_FPARAMS()));
  QW_ERR_RET(qwDropTaskCtx(QW_FPARAMS()));

  QW_TASK_DLOG_E("task is dropped");

  return TSDB_CODE_SUCCESS;
}

void qwSetHbParam(int64_t refId, SQWHbParam **pParam) {
  int32_t paramIdx = 0;
  int32_t newParamIdx = 0;

  while (true) {
    paramIdx = atomic_load_32(&gQwMgmt.paramIdx);
    if (paramIdx == tListLen(gQwMgmt.param)) {
D
dapan1121 已提交
432
      newParamIdx = 1;
D
dapan1121 已提交
433 434 435 436 437 438 439 440 441
    } else {
      newParamIdx = paramIdx + 1;
    }

    if (paramIdx == atomic_val_compare_exchange_32(&gQwMgmt.paramIdx, paramIdx, newParamIdx)) {
      break;
    }
  }

D
dapan1121 已提交
442 443 444 445
  if (paramIdx == tListLen(gQwMgmt.param)) {
    paramIdx = 0;
  }

D
dapan1121 已提交
446 447 448 449 450 451
  gQwMgmt.param[paramIdx].qwrId = gQwMgmt.qwRef;
  gQwMgmt.param[paramIdx].refId = refId;

  *pParam = &gQwMgmt.param[paramIdx];
}

L
Liu Jicong 已提交
452
void qwSaveTbVersionInfo(qTaskInfo_t pTaskInfo, SQWTaskCtx *ctx) {
H
Haojun Liao 已提交
453 454
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
  char tbName[TSDB_TABLE_NAME_LEN] = {0};
L
Liu Jicong 已提交
455

456
  qGetQueryTableSchemaVersion(pTaskInfo, dbFName, tbName, &ctx->tbInfo.sversion, &ctx->tbInfo.tversion);
D
dapan1121 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477

  if (dbFName[0] && tbName[0]) {
    sprintf(ctx->tbInfo.tbFName, "%s.%s", dbFName, tbName);
  } else {
    ctx->tbInfo.tbFName[0] = 0;
  }
}

void qwCloseRef(void) {
  taosWLockLatch(&gQwMgmt.lock);
  if (atomic_load_32(&gQwMgmt.qwNum) <= 0 && gQwMgmt.qwRef >= 0) {
    taosCloseRef(gQwMgmt.qwRef);
    gQwMgmt.qwRef = -1;
  }
  taosWUnLockLatch(&gQwMgmt.lock);
}

void qwDestroySchStatus(SQWSchStatus *pStatus) { taosHashCleanup(pStatus->tasksHash); }

void qwDestroyImpl(void *pMgmt) {
  SQWorker *mgmt = (SQWorker *)pMgmt;
H
Hongze Cheng 已提交
478 479 480
  int8_t    nodeType = mgmt->nodeType;
  int32_t   nodeId = mgmt->nodeId;

dengyihao's avatar
dengyihao 已提交
481 482
  int32_t taskCount = 0;
  int32_t schStatusCount = 0;
H
Hongze Cheng 已提交
483
  qDebug("start to destroy qworker, type:%d, id:%d, handle:%p", nodeType, nodeId, mgmt);
D
dapan1121 已提交
484

L
Liu Jicong 已提交
485 486
  taosTmrStop(mgmt->hbTimer);
  mgmt->hbTimer = NULL;
D
dapan1121 已提交
487 488
  taosTmrCleanUp(mgmt->timer);

D
dapan1121 已提交
489
  uint64_t qId, tId;
H
Hongze Cheng 已提交
490 491
  int32_t  eId;
  void    *pIter = taosHashIterate(mgmt->ctxHash, NULL);
dengyihao's avatar
dengyihao 已提交
492

D
dapan1121 已提交
493 494
  while (pIter) {
    SQWTaskCtx *ctx = (SQWTaskCtx *)pIter;
H
Hongze Cheng 已提交
495
    void       *key = taosHashGetKey(pIter, NULL);
D
dapan1121 已提交
496
    QW_GET_QTID(key, qId, tId, eId);
D
dapan1121 已提交
497

D
dapan1121 已提交
498
    qwFreeTaskCtx(ctx);
D
dapan1121 已提交
499
    QW_TASK_DLOG_E("task ctx freed");
D
dapan1121 已提交
500
    pIter = taosHashIterate(mgmt->ctxHash, pIter);
dengyihao's avatar
dengyihao 已提交
501
    taskCount++;
D
dapan1121 已提交
502
  }
D
dapan1121 已提交
503 504
  taosHashCleanup(mgmt->ctxHash);

D
dapan1121 已提交
505
  pIter = taosHashIterate(mgmt->schHash, NULL);
D
dapan1121 已提交
506 507 508
  while (pIter) {
    SQWSchStatus *sch = (SQWSchStatus *)pIter;
    qwDestroySchStatus(sch);
dengyihao's avatar
dengyihao 已提交
509

D
dapan1121 已提交
510
    pIter = taosHashIterate(mgmt->schHash, pIter);
dengyihao's avatar
dengyihao 已提交
511
    schStatusCount++;
D
dapan1121 已提交
512 513 514
  }
  taosHashCleanup(mgmt->schHash);

D
dapan1121 已提交
515
  *mgmt->destroyed = 1;
H
Hongze Cheng 已提交
516

D
dapan1121 已提交
517 518 519 520 521
  taosMemoryFree(mgmt);

  atomic_sub_fetch_32(&gQwMgmt.qwNum, 1);

  qwCloseRef();
D
dapan1121 已提交
522

dengyihao's avatar
dengyihao 已提交
523 524
  qDebug("qworker destroyed, type:%d, id:%d, handle:%p, taskCount:%d, schStatusCount: %d", nodeType, nodeId, mgmt,
         taskCount, schStatusCount);
D
dapan1121 已提交
525 526 527 528 529 530 531 532 533
}

int32_t qwOpenRef(void) {
  taosWLockLatch(&gQwMgmt.lock);
  if (gQwMgmt.qwRef < 0) {
    gQwMgmt.qwRef = taosOpenRef(100, qwDestroyImpl);
    if (gQwMgmt.qwRef < 0) {
      taosWUnLockLatch(&gQwMgmt.lock);
      qError("init qworker ref failed");
S
Shengliang Guan 已提交
534
      QW_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
535 536 537 538 539 540 541
    }
  }
  taosWUnLockLatch(&gQwMgmt.lock);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
542
int32_t qwUpdateTimeInQueue(SQWorker *mgmt, int64_t ts, EQueueType type) {
D
dapan1121 已提交
543 544 545 546 547 548 549
  if (ts <= 0) {
    return TSDB_CODE_SUCCESS;
  }

  int64_t duration = taosGetTimestampUs() - ts;
  switch (type) {
    case QUERY_QUEUE:
D
dapan1121 已提交
550 551
      ++mgmt->stat.msgStat.waitTime[0].num;
      mgmt->stat.msgStat.waitTime[0].total += duration;
D
dapan1121 已提交
552 553
      break;
    case FETCH_QUEUE:
D
dapan1121 已提交
554 555
      ++mgmt->stat.msgStat.waitTime[1].num;
      mgmt->stat.msgStat.waitTime[1].total += duration;
D
dapan1121 已提交
556 557 558 559 560 561 562 563 564
      break;
    default:
      qError("unsupported queue type %d", type);
      return TSDB_CODE_APP_ERROR;
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
565 566
int64_t qwGetTimeInQueue(SQWorker *mgmt, EQueueType type) {
  SQWTimeInQ *pStat = NULL;
D
dapan1121 已提交
567 568
  switch (type) {
    case QUERY_QUEUE:
D
dapan1121 已提交
569
      pStat = &mgmt->stat.msgStat.waitTime[0];
L
Liu Jicong 已提交
570
      return pStat->num ? (pStat->total / pStat->num) : 0;
D
dapan1121 已提交
571
    case FETCH_QUEUE:
D
dapan1121 已提交
572
      pStat = &mgmt->stat.msgStat.waitTime[1];
L
Liu Jicong 已提交
573
      return pStat->num ? (pStat->total / pStat->num) : 0;
D
dapan1121 已提交
574 575 576
    default:
      qError("unsupported queue type %d", type);
  }
D
dapan1121 已提交
577 578

  return -1;
D
dapan1121 已提交
579 580
}

H
Hongze Cheng 已提交
581
void qwClearExpiredSch(SQWorker *mgmt, SArray *pExpiredSch) {
D
dapan1121 已提交
582 583
  int32_t num = taosArrayGetSize(pExpiredSch);
  for (int32_t i = 0; i < num; ++i) {
H
Hongze Cheng 已提交
584
    uint64_t     *sId = taosArrayGet(pExpiredSch, i);
D
dapan1121 已提交
585 586 587 588 589 590 591 592
    SQWSchStatus *pSch = NULL;
    if (qwAcquireScheduler(mgmt, *sId, QW_WRITE, &pSch)) {
      continue;
    }

    if (taosHashGetSize(pSch->tasksHash) <= 0) {
      qwDestroySchStatus(pSch);
      taosHashRemove(mgmt->schHash, sId, sizeof(*sId));
D
dapan1121 已提交
593
      qDebug("sch %" PRIx64 " destroyed", *sId);
D
dapan1121 已提交
594
    }
D
dapan1121 已提交
595

D
dapan1121 已提交
596 597
    qwReleaseScheduler(QW_WRITE, mgmt);
  }
D
dapan1121 已提交
598
}