qwUtil.c 15.5 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 216 217 218 219 220 221 222 223 224

  *ctx = taosHashAcquire(mgmt->ctxHash, id, sizeof(id));
  if (NULL == (*ctx)) {
    QW_TASK_DLOG_E("task ctx not exist, may be dropped");
    QW_ERR_RET(TSDB_CODE_QRY_TASK_CTX_NOT_EXIST);
  }

  return TSDB_CODE_SUCCESS;
}

int32_t qwGetTaskCtx(QW_FPARAMS_DEF, SQWTaskCtx **ctx) {
D
dapan1121 已提交
225 226
  char id[sizeof(qId) + sizeof(tId) + sizeof(eId)] = {0};
  QW_SET_QTID(id, qId, tId, eId);
D
dapan1121 已提交
227 228 229 230 231 232 233 234 235 236 237

  *ctx = taosHashGet(mgmt->ctxHash, id, sizeof(id));
  if (NULL == (*ctx)) {
    QW_TASK_DLOG_E("task ctx not exist, may be dropped");
    QW_ERR_RET(TSDB_CODE_QRY_TASK_CTX_NOT_EXIST);
  }

  return TSDB_CODE_SUCCESS;
}

int32_t qwAddTaskCtxImpl(QW_FPARAMS_DEF, bool acquire, SQWTaskCtx **ctx) {
D
dapan1121 已提交
238 239
  char id[sizeof(qId) + sizeof(tId) + sizeof(eId)] = {0};
  QW_SET_QTID(id, qId, tId, eId);
D
dapan1121 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

  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 已提交
256
      QW_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    }
  }

  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 已提交
275
void qwFreeTaskHandle(qTaskInfo_t *taskHandle) {
D
dapan1121 已提交
276 277 278 279
  // 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 已提交
280
    qDebug("task handle destroyed");
D
dapan1121 已提交
281 282 283
  }
}

D
dapan1121 已提交
284
int32_t qwKillTaskHandle(SQWTaskCtx *ctx, int32_t rspCode) {
D
dapan1121 已提交
285
  int32_t code = 0;
dengyihao's avatar
dengyihao 已提交
286

D
dapan1121 已提交
287 288 289
  // 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 已提交
290
    qDebug("start to kill task");
D
dapan1121 已提交
291
    code = qAsyncKillTask(taskHandle, rspCode);
D
dapan1121 已提交
292 293 294 295 296 297
    atomic_store_ptr(&ctx->taskHandle, taskHandle);
  }

  QW_RET(code);
}

D
dapan1121 已提交
298
void qwFreeTaskCtx(SQWTaskCtx *ctx) {
D
dapan1121 已提交
299 300 301
  if (ctx->ctrlConnInfo.handle) {
    tmsgReleaseHandle(&ctx->ctrlConnInfo, TAOS_CONN_SERVER);
  }
H
Hongze Cheng 已提交
302

D
dapan1121 已提交
303 304 305 306 307
  ctx->ctrlConnInfo.handle = NULL;
  ctx->ctrlConnInfo.refId = -1;

  // NO need to release dataConnInfo

D
dapan1121 已提交
308
  qwFreeTaskHandle(&ctx->taskHandle);
D
dapan1121 已提交
309 310 311 312

  if (ctx->sinkHandle) {
    dsDestroyDataSinker(ctx->sinkHandle);
    ctx->sinkHandle = NULL;
H
Haojun Liao 已提交
313
    qDebug("sink handle destroyed");
D
dapan1121 已提交
314 315 316 317
  }
}

int32_t qwDropTaskCtx(QW_FPARAMS_DEF) {
D
dapan1121 已提交
318 319
  char id[sizeof(qId) + sizeof(tId) + sizeof(eId)] = {0};
  QW_SET_QTID(id, qId, tId, eId);
D
dapan1121 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
  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 已提交
339
  qwFreeTaskCtx(&octx);
D
dapan1121 已提交
340 341 342 343 344 345 346 347 348 349 350

  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 已提交
351 352
  char id[sizeof(qId) + sizeof(tId) + sizeof(eId)] = {0};
  QW_SET_QTID(id, qId, tId, eId);
D
dapan1121 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367

  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 已提交
368
    QW_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 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
  }

  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 已提交
419
      newParamIdx = 1;
D
dapan1121 已提交
420 421 422 423 424 425 426 427 428
    } else {
      newParamIdx = paramIdx + 1;
    }

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

D
dapan1121 已提交
429 430 431 432
  if (paramIdx == tListLen(gQwMgmt.param)) {
    paramIdx = 0;
  }

D
dapan1121 已提交
433 434 435 436 437 438
  gQwMgmt.param[paramIdx].qwrId = gQwMgmt.qwRef;
  gQwMgmt.param[paramIdx].refId = refId;

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

L
Liu Jicong 已提交
439
void qwSaveTbVersionInfo(qTaskInfo_t pTaskInfo, SQWTaskCtx *ctx) {
H
Haojun Liao 已提交
440 441
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
  char tbName[TSDB_TABLE_NAME_LEN] = {0};
L
Liu Jicong 已提交
442

443
  qGetQueryTableSchemaVersion(pTaskInfo, dbFName, tbName, &ctx->tbInfo.sversion, &ctx->tbInfo.tversion);
D
dapan1121 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

  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 已提交
465 466 467
  int8_t    nodeType = mgmt->nodeType;
  int32_t   nodeId = mgmt->nodeId;

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

L
Liu Jicong 已提交
472 473
  taosTmrStop(mgmt->hbTimer);
  mgmt->hbTimer = NULL;
D
dapan1121 已提交
474 475
  taosTmrCleanUp(mgmt->timer);

D
dapan1121 已提交
476
  uint64_t qId, tId;
H
Hongze Cheng 已提交
477 478
  int32_t  eId;
  void    *pIter = taosHashIterate(mgmt->ctxHash, NULL);
dengyihao's avatar
dengyihao 已提交
479

D
dapan1121 已提交
480 481
  while (pIter) {
    SQWTaskCtx *ctx = (SQWTaskCtx *)pIter;
H
Hongze Cheng 已提交
482
    void       *key = taosHashGetKey(pIter, NULL);
D
dapan1121 已提交
483
    QW_GET_QTID(key, qId, tId, eId);
D
dapan1121 已提交
484

D
dapan1121 已提交
485
    qwFreeTaskCtx(ctx);
D
dapan1121 已提交
486
    QW_TASK_DLOG_E("task ctx freed");
D
dapan1121 已提交
487
    pIter = taosHashIterate(mgmt->ctxHash, pIter);
dengyihao's avatar
dengyihao 已提交
488
    taskCount++;
D
dapan1121 已提交
489
  }
D
dapan1121 已提交
490 491
  taosHashCleanup(mgmt->ctxHash);

D
dapan1121 已提交
492
  pIter = taosHashIterate(mgmt->schHash, NULL);
D
dapan1121 已提交
493 494 495
  while (pIter) {
    SQWSchStatus *sch = (SQWSchStatus *)pIter;
    qwDestroySchStatus(sch);
dengyihao's avatar
dengyihao 已提交
496

D
dapan1121 已提交
497
    pIter = taosHashIterate(mgmt->schHash, pIter);
dengyihao's avatar
dengyihao 已提交
498
    schStatusCount++;
D
dapan1121 已提交
499 500 501
  }
  taosHashCleanup(mgmt->schHash);

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

D
dapan1121 已提交
504 505 506 507 508
  taosMemoryFree(mgmt);

  atomic_sub_fetch_32(&gQwMgmt.qwNum, 1);

  qwCloseRef();
D
dapan1121 已提交
509

dengyihao's avatar
dengyihao 已提交
510 511
  qDebug("qworker destroyed, type:%d, id:%d, handle:%p, taskCount:%d, schStatusCount: %d", nodeType, nodeId, mgmt,
         taskCount, schStatusCount);
D
dapan1121 已提交
512 513 514 515 516 517 518 519 520
}

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 已提交
521
      QW_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
522 523 524 525 526 527 528
    }
  }
  taosWUnLockLatch(&gQwMgmt.lock);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
529
int32_t qwUpdateTimeInQueue(SQWorker *mgmt, int64_t ts, EQueueType type) {
D
dapan1121 已提交
530 531 532 533 534 535 536
  if (ts <= 0) {
    return TSDB_CODE_SUCCESS;
  }

  int64_t duration = taosGetTimestampUs() - ts;
  switch (type) {
    case QUERY_QUEUE:
D
dapan1121 已提交
537 538
      ++mgmt->stat.msgStat.waitTime[0].num;
      mgmt->stat.msgStat.waitTime[0].total += duration;
D
dapan1121 已提交
539 540
      break;
    case FETCH_QUEUE:
D
dapan1121 已提交
541 542
      ++mgmt->stat.msgStat.waitTime[1].num;
      mgmt->stat.msgStat.waitTime[1].total += duration;
D
dapan1121 已提交
543 544 545 546 547 548 549 550 551
      break;
    default:
      qError("unsupported queue type %d", type);
      return TSDB_CODE_APP_ERROR;
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
552 553
int64_t qwGetTimeInQueue(SQWorker *mgmt, EQueueType type) {
  SQWTimeInQ *pStat = NULL;
D
dapan1121 已提交
554 555
  switch (type) {
    case QUERY_QUEUE:
D
dapan1121 已提交
556
      pStat = &mgmt->stat.msgStat.waitTime[0];
L
Liu Jicong 已提交
557
      return pStat->num ? (pStat->total / pStat->num) : 0;
D
dapan1121 已提交
558
    case FETCH_QUEUE:
D
dapan1121 已提交
559
      pStat = &mgmt->stat.msgStat.waitTime[1];
L
Liu Jicong 已提交
560
      return pStat->num ? (pStat->total / pStat->num) : 0;
D
dapan1121 已提交
561 562 563
    default:
      qError("unsupported queue type %d", type);
  }
D
dapan1121 已提交
564 565

  return -1;
D
dapan1121 已提交
566 567
}

H
Hongze Cheng 已提交
568
void qwClearExpiredSch(SQWorker *mgmt, SArray *pExpiredSch) {
D
dapan1121 已提交
569 570
  int32_t num = taosArrayGetSize(pExpiredSch);
  for (int32_t i = 0; i < num; ++i) {
H
Hongze Cheng 已提交
571
    uint64_t     *sId = taosArrayGet(pExpiredSch, i);
D
dapan1121 已提交
572 573 574 575 576 577 578 579
    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 已提交
580
      qDebug("sch %" PRIx64 " destroyed", *sId);
D
dapan1121 已提交
581
    }
D
dapan1121 已提交
582

D
dapan1121 已提交
583 584
    qwReleaseScheduler(QW_WRITE, mgmt);
  }
D
dapan1121 已提交
585
}