qworker.c 39.8 KB
Newer Older
1 2
#include "qworker.h"

dengyihao's avatar
dengyihao 已提交
3
#include "dataSinkMgt.h"
4
#include "executor.h"
D
dapan1121 已提交
5
#include "planner.h"
H
Haojun Liao 已提交
6
#include "query.h"
D
dapan1121 已提交
7 8
#include "qwInt.h"
#include "qwMsg.h"
dengyihao's avatar
dengyihao 已提交
9
#include "tcommon.h"
H
Hongze Cheng 已提交
10
#include "tdatablock.h"
dengyihao's avatar
dengyihao 已提交
11
#include "tglobal.h"
H
Haojun Liao 已提交
12
#include "tmsg.h"
13
#include "tname.h"
D
dapan1121 已提交
14

D
dapan1121 已提交
15
SQWorkerMgmt gQwMgmt = {
16 17 18
    .lock = 0,
    .qwRef = -1,
    .qwNum = 0,
D
dapan1121 已提交
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

int32_t qwStopAllTasks(SQWorker *mgmt) {
  uint64_t qId, tId, sId;
  int32_t  eId;
  int64_t  rId = 0;

  void    *pIter = taosHashIterate(mgmt->ctxHash, NULL);
  while (pIter) {
    SQWTaskCtx *ctx = (SQWTaskCtx *)pIter;
    void       *key = taosHashGetKey(pIter, NULL);
    QW_GET_QTID(key, qId, tId, eId);

    QW_LOCK(QW_WRITE, &ctx->lock);

    sId = ctx->sId;

    QW_TASK_DLOG_E("start to force stop task");

    if (QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP) || QW_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
      QW_TASK_WLOG_E("task already dropping");
      QW_UNLOCK(QW_WRITE, &ctx->lock);

      pIter = taosHashIterate(mgmt->ctxHash, pIter);
      continue;
    }

    if (QW_QUERY_RUNNING(ctx)) {
      qwKillTaskHandle(ctx, TSDB_CODE_VND_STOPPED);
      QW_TASK_DLOG_E("task running, async killed");
    } else if (QW_FETCH_RUNNING(ctx)) {
      QW_UPDATE_RSP_CODE(ctx, TSDB_CODE_VND_STOPPED);
      QW_SET_EVENT_RECEIVED(ctx, QW_EVENT_DROP);    
      QW_TASK_DLOG_E("task fetching, update drop received");
    } else {
      qwDropTask(QW_FPARAMS());
    }

    QW_UNLOCK(QW_WRITE, &ctx->lock);

    pIter = taosHashIterate(mgmt->ctxHash, pIter);
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
66 67 68 69
int32_t qwProcessHbLinkBroken(SQWorker *mgmt, SQWMsg *qwMsg, SSchedulerHbReq *req) {
  int32_t         code = 0;
  SSchedulerHbRsp rsp = {0};
  SQWSchStatus   *sch = NULL;
D
dapan1121 已提交
70

D
dapan1121 已提交
71
  QW_ERR_RET(qwAcquireScheduler(mgmt, req->sId, QW_READ, &sch));
D
dapan1121 已提交
72

D
dapan1121 已提交
73
  QW_LOCK(QW_WRITE, &sch->hbConnLock);
D
dapan1121 已提交
74

D
dapan1121 已提交
75
  sch->hbBrokenTs = taosGetTimestampMs();
76

D
dapan1121 已提交
77 78 79 80
  if (qwMsg->connInfo.handle == sch->hbConnInfo.handle) {
    tmsgReleaseHandle(&sch->hbConnInfo, TAOS_CONN_SERVER);
    sch->hbConnInfo.handle = NULL;
    sch->hbConnInfo.ahandle = NULL;
D
dapan1121 已提交
81

D
dapan1121 已提交
82 83 84
    QW_DLOG("release hb handle due to connection broken, handle:%p", qwMsg->connInfo.handle);
  } else {
    QW_DLOG("ignore hb connection broken, handle:%p, currentHandle:%p", qwMsg->connInfo.handle, sch->hbConnInfo.handle);
D
dapan1121 已提交
85
  }
D
dapan1121 已提交
86

D
dapan1121 已提交
87
  QW_UNLOCK(QW_WRITE, &sch->hbConnLock);
D
dapan1121 已提交
88

D
dapan1121 已提交
89
  qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
90

D
dapan1121 已提交
91
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
92 93
}

94 95
static void freeItem(void *param) {
  SExplainExecInfo *pInfo = param;
H
Haojun Liao 已提交
96 97 98
  taosMemoryFree(pInfo->verboseInfo);
}

D
dapan1121 已提交
99
int32_t qwHandleTaskComplete(QW_FPARAMS_DEF, SQWTaskCtx *ctx) {
D
dapan1121 已提交
100
  qTaskInfo_t taskHandle = ctx->taskHandle;
D
dapan1121 已提交
101

D
dapan1121 已提交
102 103
  ctx->queryExecDone = true;

D
dapan1121 已提交
104
  if (TASK_TYPE_TEMP == ctx->taskType && taskHandle) {
D
dapan1121 已提交
105
    if (ctx->explain) {
106
      SArray *execInfoList = taosArrayInit(4, sizeof(SExplainExecInfo));
H
Haojun Liao 已提交
107
      QW_ERR_RET(qGetExplainExecInfo(taskHandle, execInfoList));
108

D
dapan1121 已提交
109
      if (ctx->localExec) {
110 111 112 113 114 115 116 117 118 119 120
        SExplainLocalRsp localRsp = {0};
        localRsp.rsp.numOfPlans = taosArrayGetSize(execInfoList);
        SExplainExecInfo *pExec = taosMemoryCalloc(localRsp.rsp.numOfPlans, sizeof(SExplainExecInfo));
        memcpy(pExec, taosArrayGet(execInfoList, 0), localRsp.rsp.numOfPlans * sizeof(SExplainExecInfo));
        localRsp.rsp.subplanInfo = pExec;
        localRsp.qId = qId;
        localRsp.tId = tId;
        localRsp.rId = rId;
        localRsp.eId = eId;
        taosArrayPush(ctx->explainRes, &localRsp);
        taosArrayDestroy(execInfoList);
D
dapan1121 已提交
121 122 123 124 125 126 127
      } else {
        SRpcHandleInfo connInfo = ctx->ctrlConnInfo;
        connInfo.ahandle = NULL;
        int32_t code = qwBuildAndSendExplainRsp(&connInfo, execInfoList);
        taosArrayDestroyEx(execInfoList, freeItem);
        QW_ERR_RET(code);
      }
D
dapan1121 已提交
128
    }
D
dapan1121 已提交
129 130 131 132

    if (!ctx->needFetch) {
      dsGetDataLength(ctx->sinkHandle, &ctx->affectedRows, NULL);
    }
D
dapan1121 已提交
133 134 135 136 137
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
138 139
int32_t qwSendQueryRsp(QW_FPARAMS_DEF, int32_t msgType, SQWTaskCtx *ctx, int32_t rspCode, bool quickRsp) {
  if ((!quickRsp) || QUERY_RSP_POLICY_QUICK == tsQueryRspPolicy) {
140 141 142 143 144
    if (!ctx->localExec) {
      qwBuildAndSendQueryRsp(msgType, &ctx->ctrlConnInfo, rspCode, ctx);
      QW_TASK_DLOG("query msg rsped, handle:%p, code:%x - %s", ctx->ctrlConnInfo.handle, rspCode, tstrerror(rspCode));
    }

D
dapan1121 已提交
145 146 147 148 149 150
    ctx->queryRsped = true;
  }

  return TSDB_CODE_SUCCESS;
}

151
int32_t qwExecTask(QW_FPARAMS_DEF, SQWTaskCtx *ctx, bool *queryStop) {
dengyihao's avatar
dengyihao 已提交
152 153 154 155 156
  int32_t        code = 0;
  bool           qcontinue = true;
  uint64_t       useconds = 0;
  int32_t        i = 0;
  int32_t        execNum = 0;
D
dapan1121 已提交
157
  qTaskInfo_t    taskHandle = ctx->taskHandle;
D
dapan1121 已提交
158
  DataSinkHandle sinkHandle = ctx->sinkHandle;
H
Hongze Cheng 已提交
159
  SLocalFetch    localFetch = {(void *)mgmt, ctx->localExec, qWorkerProcessLocalFetch, ctx->explainRes};
dengyihao's avatar
dengyihao 已提交
160

D
dapan1121 已提交
161 162 163 164
  if (ctx->queryExecDone) {
    if (queryStop) {
      *queryStop = true;
    }
dengyihao's avatar
dengyihao 已提交
165

D
dapan1121 已提交
166 167 168
    return TSDB_CODE_SUCCESS;
  }

169
  SArray *pResList = taosArrayInit(4, POINTER_BYTES);
D
dapan1121 已提交
170
  while (true) {
H
Haojun Liao 已提交
171
    QW_TASK_DLOG("start to execTask, loopIdx:%d", i++);
D
dapan1121 已提交
172

D
dapan1121 已提交
173
    // if *taskHandle is NULL, it's killed right now
174 175
    bool hasMore = false;

D
dapan1121 已提交
176
    if (taskHandle) {
D
dapan1121 已提交
177
      qwDbgSimulateSleep();
178 179

      code = qExecTaskOpt(taskHandle, pResList, &useconds, &hasMore, &localFetch);
D
dapan1121 已提交
180
      if (code) {
181 182 183 184 185
        if (code != TSDB_CODE_OPS_NOT_SUPPORT) {
          QW_TASK_ELOG("qExecTask failed, code:%x - %s", code, tstrerror(code));
        } else {
          QW_TASK_DLOG("qExecTask failed, code:%x - %s", code, tstrerror(code));
        }
186
        QW_ERR_JRET(code);
D
dapan1121 已提交
187
      }
D
dapan1121 已提交
188 189
    }

D
dapan1121 已提交
190 191
    ++execNum;

192 193
    size_t numOfResBlock = taosArrayGetSize(pResList);
    for (int32_t j = 0; j < numOfResBlock; ++j) {
H
Haojun Liao 已提交
194
      SSDataBlock *pRes = taosArrayGetP(pResList, j);
195
      ASSERT(pRes->info.rows > 0);
H
Haojun Liao 已提交
196

H
Haojun Liao 已提交
197 198 199 200
      SInputData inputData = {.pData = pRes};
      code = dsPutDataBlock(sinkHandle, &inputData, &qcontinue);
      if (code) {
        QW_TASK_ELOG("dsPutDataBlock failed, code:%x - %s", code, tstrerror(code));
201
        QW_ERR_JRET(code);
H
Haojun Liao 已提交
202
      }
203

H
Haojun Liao 已提交
204
      QW_TASK_DLOG("data put into sink, rows:%d, continueExecTask:%d", pRes->info.rows, qcontinue);
D
dapan1121 已提交
205
    }
D
dapan1121 已提交
206

207 208 209
    if (numOfResBlock == 0 || (hasMore == false)) {
      if (numOfResBlock == 0) {
        QW_TASK_DLOG("qExecTask end with empty res, useconds:%" PRIu64, useconds);
H
Haojun Liao 已提交
210 211
      } else {
        QW_TASK_DLOG("qExecTask done, useconds:%" PRIu64, useconds);
212 213 214 215 216 217 218 219 220 221 222 223
      }

      dsEndPut(sinkHandle, useconds);
      QW_ERR_JRET(qwHandleTaskComplete(QW_FPARAMS(), ctx));

      if (queryStop) {
        *queryStop = true;
      }

      break;
    }

D
dapan1121 已提交
224
    if (!qcontinue) {
225 226 227
      if (queryStop) {
        *queryStop = true;
      }
228

D
dapan1121 已提交
229 230 231
      break;
    }

D
dapan1121 已提交
232
    if (ctx->needFetch && (!ctx->queryRsped) && execNum >= QW_DEFAULT_SHORT_RUN_TIMES) {
D
dapan1121 已提交
233 234 235
      break;
    }

D
dapan1121 已提交
236
    if (QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
D
dapan1121 已提交
237 238
      break;
    }
D
dapan1121 已提交
239

D
dapan1121 已提交
240 241 242
    if (atomic_load_32(&ctx->rspCode)) {
      break;
    }
D
dapan1121 已提交
243 244
  }

245
_return:
H
Haojun Liao 已提交
246
  taosArrayDestroy(pResList);
D
dapan1121 已提交
247
  QW_RET(code);
D
dapan1121 已提交
248
}
D
dapan1121 已提交
249

250 251 252 253 254 255 256 257 258
bool qwTaskNotInExec(SQWTaskCtx *ctx) {
  qTaskInfo_t    taskHandle = ctx->taskHandle;
  if (NULL == taskHandle || !qTaskIsExecuting(taskHandle)) {
    return true;
  }

  return false;
}

D
dapan1121 已提交
259
int32_t qwGenerateSchHbRsp(SQWorker *mgmt, SQWSchStatus *sch, SQWHbInfo *hbInfo) {
D
dapan1121 已提交
260 261
  int32_t taskNum = 0;

D
dapan1121 已提交
262
  hbInfo->connInfo = sch->hbConnInfo;
D
dapan1121 已提交
263
  hbInfo->rsp.epId = sch->hbEpId;
D
dapan1121 已提交
264

D
dapan1121 已提交
265
  QW_LOCK(QW_READ, &sch->tasksLock);
dengyihao's avatar
dengyihao 已提交
266

D
dapan1121 已提交
267
  taskNum = taosHashGetSize(sch->tasksHash);
D
dapan1121 已提交
268 269 270

  hbInfo->rsp.taskStatus = taosArrayInit(taskNum, sizeof(STaskStatus));
  if (NULL == hbInfo->rsp.taskStatus) {
D
dapan1121 已提交
271
    QW_UNLOCK(QW_READ, &sch->tasksLock);
D
dapan1121 已提交
272
    QW_ELOG("taosArrayInit taskStatus failed, num:%d", taskNum);
S
Shengliang Guan 已提交
273
    return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
274 275
  }

276
  void       *key = NULL;
dengyihao's avatar
dengyihao 已提交
277 278
  size_t      keyLen = 0;
  int32_t     i = 0;
D
dapan1121 已提交
279
  STaskStatus status = {0};
D
dapan1121 已提交
280 281 282 283

  void *pIter = taosHashIterate(sch->tasksHash, NULL);
  while (pIter) {
    SQWTaskStatus *taskStatus = (SQWTaskStatus *)pIter;
D
dapan1121 已提交
284
    key = taosHashGetKey(pIter, &keyLen);
D
dapan1121 已提交
285

dengyihao's avatar
dengyihao 已提交
286
    // TODO GET EXECUTOR API TO GET MORE INFO
D
dapan1121 已提交
287

D
dapan1121 已提交
288
    QW_GET_QTID(key, status.queryId, status.taskId, status.execId);
D
dapan1121 已提交
289 290
    status.status = taskStatus->status;
    status.refId = taskStatus->refId;
dengyihao's avatar
dengyihao 已提交
291

D
dapan1121 已提交
292
    taosArrayPush(hbInfo->rsp.taskStatus, &status);
dengyihao's avatar
dengyihao 已提交
293

D
dapan1121 已提交
294 295
    ++i;
    pIter = taosHashIterate(sch->tasksHash, pIter);
dengyihao's avatar
dengyihao 已提交
296
  }
D
dapan1121 已提交
297 298 299 300 301 302

  QW_UNLOCK(QW_READ, &sch->tasksLock);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
303
int32_t qwGetQueryResFromSink(QW_FPARAMS_DEF, SQWTaskCtx *ctx, int32_t *dataLen, void **rspMsg, SOutputData *pOutput) {
D
dapan1121 已提交
304
  int64_t            len = 0;
D
dapan1121 已提交
305
  SRetrieveTableRsp *rsp = NULL;
dengyihao's avatar
dengyihao 已提交
306 307
  bool               queryEnd = false;
  int32_t            code = 0;
308
  SOutputData        output = {0};
D
dapan1121 已提交
309

310
  if (NULL == ctx->sinkHandle) {
D
dapan1121 已提交
311
    pOutput->queryEnd = true;
312 313 314
    return TSDB_CODE_SUCCESS;
  }

315
  *dataLen = 0;
D
dapan1121 已提交
316

317 318
  while (true) {
    dsGetDataLength(ctx->sinkHandle, &len, &queryEnd);
D
dapan1121 已提交
319

320
    if (len < 0) {
321
      QW_TASK_ELOG("invalid length from dsGetDataLength, length:%" PRId64 "", len);
322 323
      QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
    }
dengyihao's avatar
dengyihao 已提交
324

325 326 327 328 329 330 331
    if (len == 0) {
      if (queryEnd) {
        code = dsGetDataBlock(ctx->sinkHandle, &output);
        if (code) {
          QW_TASK_ELOG("dsGetDataBlock failed, code:%x - %s", code, tstrerror(code));
          QW_ERR_RET(code);
        }
H
Haojun Liao 已提交
332

dengyihao's avatar
dengyihao 已提交
333
        QW_TASK_DLOG("no more data in sink and query end, fetched blocks %d rows %" PRId64, pOutput->numOfBlocks,
334
                     pOutput->numOfRows);
335

336 337
        qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_SUCC);
        if (NULL == rsp) {
D
dapan1121 已提交
338
          QW_ERR_RET(qwMallocFetchRsp(!ctx->localExec, len, &rsp));
339 340 341 342 343 344 345 346 347 348 349 350 351
          *pOutput = output;
        } else {
          pOutput->queryEnd = output.queryEnd;
          pOutput->bufStatus = output.bufStatus;
          pOutput->useconds = output.useconds;
        }

        break;
      }

      pOutput->bufStatus = DS_BUF_EMPTY;

      break;
D
dapan1121 已提交
352
    }
D
dapan1121 已提交
353

354
    // Got data from sink
355
    QW_TASK_DLOG("there are data in sink, dataLength:%" PRId64 "", len);
D
dapan1121 已提交
356

357
    *dataLen += len;
D
dapan1121 已提交
358

D
dapan1121 已提交
359
    QW_ERR_RET(qwMallocFetchRsp(!ctx->localExec, *dataLen, &rsp));
D
dapan1121 已提交
360

361 362 363 364 365 366
    output.pData = rsp->data + *dataLen - len;
    code = dsGetDataBlock(ctx->sinkHandle, &output);
    if (code) {
      QW_TASK_ELOG("dsGetDataBlock failed, code:%x - %s", code, tstrerror(code));
      QW_ERR_RET(code);
    }
dengyihao's avatar
dengyihao 已提交
367

368 369 370 371 372 373 374 375 376 377
    pOutput->queryEnd = output.queryEnd;
    pOutput->precision = output.precision;
    pOutput->bufStatus = output.bufStatus;
    pOutput->useconds = output.useconds;
    pOutput->compressed = output.compressed;
    pOutput->numOfCols = output.numOfCols;
    pOutput->numOfRows += output.numOfRows;
    pOutput->numOfBlocks++;

    if (DS_BUF_EMPTY == pOutput->bufStatus && pOutput->queryEnd) {
378
      QW_TASK_DLOG("task all data fetched and done, fetched blocks %d rows %" PRId64, pOutput->numOfBlocks,
379
                   pOutput->numOfRows);
380 381 382
      qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_SUCC);
      break;
    }
dengyihao's avatar
dengyihao 已提交
383

384
    if (0 == ctx->level) {
dengyihao's avatar
dengyihao 已提交
385 386
      QW_TASK_DLOG("task fetched blocks %d rows %" PRId64 ", level %d", pOutput->numOfBlocks, pOutput->numOfRows,
                   ctx->level);
387 388
      break;
    }
D
dapan1121 已提交
389

390
    if (pOutput->numOfRows >= QW_MIN_RES_ROWS) {
dengyihao's avatar
dengyihao 已提交
391 392
      QW_TASK_DLOG("task fetched blocks %d rows %" PRId64 " reaches the min rows", pOutput->numOfBlocks,
                   pOutput->numOfRows);
393 394
      break;
    }
D
dapan1121 已提交
395 396
  }

397 398
  *rspMsg = rsp;

D
dapan1121 已提交
399
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
400 401
}

402
int32_t qwGetDeleteResFromSink(QW_FPARAMS_DEF, SQWTaskCtx *ctx, SDeleteRes *pRes) {
403 404 405 406
  int64_t     len = 0;
  bool        queryEnd = false;
  int32_t     code = 0;
  SOutputData output = {0};
D
dapan1121 已提交
407 408 409

  dsGetDataLength(ctx->sinkHandle, &len, &queryEnd);

D
dapan1121 已提交
410
  if (len <= 0 || len != sizeof(SDeleterRes)) {
D
dapan1121 已提交
411
    QW_TASK_ELOG("invalid length from dsGetDataLength, length:%" PRId64, len);
D
dapan1121 已提交
412 413 414 415 416 417 418
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  output.pData = taosMemoryCalloc(1, len);
  if (NULL == output.pData) {
    QW_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }
419

D
dapan1121 已提交
420 421 422 423 424 425 426
  code = dsGetDataBlock(ctx->sinkHandle, &output);
  if (code) {
    QW_TASK_ELOG("dsGetDataBlock failed, code:%x - %s", code, tstrerror(code));
    taosMemoryFree(output.pData);
    QW_ERR_RET(code);
  }

427 428
  SDeleterRes *pDelRes = (SDeleterRes *)output.pData;

D
dapan1121 已提交
429
  pRes->suid = pDelRes->suid;
D
dapan1121 已提交
430 431 432
  pRes->uidList = pDelRes->uidList;
  pRes->skey = pDelRes->skey;
  pRes->ekey = pDelRes->ekey;
433
  pRes->affectedRows = pDelRes->affectedRows;
434 435
  strcpy(pRes->tableFName, pDelRes->tableName);
  strcpy(pRes->tsColName, pDelRes->tsColName);
D
dapan1121 已提交
436
  taosMemoryFree(output.pData);
437

D
dapan1121 已提交
438 439 440
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
441
int32_t qwHandlePrePhaseEvents(QW_FPARAMS_DEF, int8_t phase, SQWPhaseInput *input, SQWPhaseOutput *output) {
442 443
  int32_t     code = 0;
  SQWTaskCtx *ctx = NULL;
D
dapan1121 已提交
444

D
dapan1121 已提交
445
  QW_TASK_DLOG("start to handle event at phase %s", qwPhaseStr(phase));
D
dapan1121 已提交
446

D
dapan1121 已提交
447
  QW_ERR_JRET(qwAcquireTaskCtx(QW_FPARAMS(), &ctx));
dengyihao's avatar
dengyihao 已提交
448

D
dapan1121 已提交
449
  QW_LOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
450

D
dapan1121 已提交
451
  QW_SET_PHASE(ctx, phase);
D
dapan1121 已提交
452

dengyihao's avatar
dengyihao 已提交
453
  if (atomic_load_8((int8_t *)&ctx->queryEnd)) {
D
dapan1121 已提交
454 455 456
    QW_TASK_ELOG_E("query already end");
    QW_ERR_JRET(TSDB_CODE_QW_MSG_ERROR);
  }
D
dapan1121 已提交
457

D
dapan1121 已提交
458 459
  switch (phase) {
    case QW_PHASE_PRE_QUERY: {
D
dapan1121 已提交
460
      if (QW_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
H
Haojun Liao 已提交
461
        QW_TASK_ELOG("task already dropped at wrong phase %s", qwPhaseStr(phase));
D
dapan1121 已提交
462
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_STATUS_ERROR);
D
dapan1121 已提交
463
      }
D
dapan1121 已提交
464

D
dapan1121 已提交
465
      if (QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
466
        QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
467

468 469
        // qwBuildAndSendDropRsp(&ctx->ctrlConnInfo, code);
        // QW_TASK_DLOG("drop rsp send, handle:%p, code:%x - %s", ctx->ctrlConnInfo.handle, code, tstrerror(code));
D
dapan1121 已提交
470

D
dapan1121 已提交
471
        QW_ERR_JRET(ctx->rspCode);
D
dapan1121 已提交
472
      }
D
dapan1121 已提交
473

D
dapan1121 已提交
474
      QW_ERR_JRET(qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_EXEC));
D
dapan1121 已提交
475 476
      break;
    }
D
dapan1121 已提交
477
    case QW_PHASE_PRE_FETCH: {
D
dapan1121 已提交
478
      if (QW_EVENT_PROCESSED(ctx, QW_EVENT_DROP) || QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
479
        QW_TASK_WLOG("task dropping or already dropped, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
480
        QW_ERR_JRET(ctx->rspCode);
D
dapan1121 已提交
481
      }
D
dapan1121 已提交
482

D
dapan1121 已提交
483
      if (QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
D
dapan1121 已提交
484
        QW_TASK_WLOG("last fetch still not processed, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
485 486 487
        QW_ERR_JRET(TSDB_CODE_QRY_DUPLICATTED_OPERATION);
      }

D
dapan1121 已提交
488 489 490 491 492
      if (ctx->rspCode) {
        QW_TASK_ELOG("task already failed cause of %s, phase:%s", tstrerror(ctx->rspCode), qwPhaseStr(phase));
        QW_ERR_JRET(ctx->rspCode);
      }

D
dapan1121 已提交
493
      if (!ctx->queryRsped) {
D
dapan1121 已提交
494
        QW_TASK_ELOG("ready msg has not been processed, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
495
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_MSG_ERROR);
D
dapan1121 已提交
496 497
      }
      break;
dengyihao's avatar
dengyihao 已提交
498
    }
D
dapan1121 已提交
499
    case QW_PHASE_PRE_CQUERY: {
D
dapan1121 已提交
500
      if (QW_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
501
        QW_TASK_WLOG("task already dropped, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
502
        QW_ERR_JRET(ctx->rspCode);
D
dapan1121 已提交
503
      }
D
dapan1121 已提交
504

D
dapan1121 已提交
505 506 507 508 509
      if (ctx->rspCode) {
        QW_TASK_ELOG("task already failed cause of %s, phase:%s", tstrerror(ctx->rspCode), qwPhaseStr(phase));
        QW_ERR_JRET(ctx->rspCode);
      }

D
dapan1121 已提交
510
      if (QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
511
        QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
H
Haojun Liao 已提交
512

513 514
        // qwBuildAndSendDropRsp(&ctx->ctrlConnInfo, code);
        // QW_TASK_DLOG("drop rsp send, handle:%p, code:%x - %s", ctx->ctrlConnInfo.handle, code, tstrerror(code));
515

D
dapan1121 已提交
516
        QW_ERR_JRET(ctx->rspCode);
D
dapan1121 已提交
517
      }
D
dapan1121 已提交
518

D
dapan1121 已提交
519
      break;
D
dapan1121 已提交
520 521 522
    }
    default:
      QW_TASK_ELOG("invalid phase %s", qwPhaseStr(phase));
S
Shengliang Guan 已提交
523
      QW_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
524 525 526
  }

  if (ctx->rspCode) {
527
    QW_TASK_ELOG("task already failed at phase %s, code:%s", qwPhaseStr(phase), tstrerror(ctx->rspCode));
D
dapan1121 已提交
528
    QW_ERR_JRET(ctx->rspCode);
D
dapan1121 已提交
529
  }
D
dapan1121 已提交
530

D
dapan1121 已提交
531
_return:
D
dapan1121 已提交
532

D
dapan1121 已提交
533
  if (ctx) {
D
dapan1121 已提交
534
    QW_UPDATE_RSP_CODE(ctx, code);
dengyihao's avatar
dengyihao 已提交
535

D
dapan1121 已提交
536
    QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
537 538
    qwReleaseTaskCtx(mgmt, ctx);
  }
D
dapan1121 已提交
539

540 541 542 543 544
  if (code != TSDB_CODE_SUCCESS) {
    QW_TASK_ELOG("end to handle event at phase %s, code:%s", qwPhaseStr(phase), tstrerror(code));
  } else {
    QW_TASK_DLOG("end to handle event at phase %s, code:%s", qwPhaseStr(phase), tstrerror(code));
  }
D
dapan1121 已提交
545 546 547 548 549

  QW_RET(code);
}

int32_t qwHandlePostPhaseEvents(QW_FPARAMS_DEF, int8_t phase, SQWPhaseInput *input, SQWPhaseOutput *output) {
550 551 552
  int32_t        code = 0;
  SQWTaskCtx    *ctx = NULL;
  SRpcHandleInfo connInfo = {0};
D
dapan1121 已提交
553

D
dapan1121 已提交
554
  QW_TASK_DLOG("start to handle event at phase %s", qwPhaseStr(phase));
dengyihao's avatar
dengyihao 已提交
555

D
dapan1121 已提交
556
  QW_ERR_JRET(qwAcquireTaskCtx(QW_FPARAMS(), &ctx));
dengyihao's avatar
dengyihao 已提交
557

D
dapan1121 已提交
558 559
  QW_LOCK(QW_WRITE, &ctx->lock);

D
dapan1121 已提交
560
  if (QW_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
561
    QW_TASK_WLOG("task already dropped, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
562
    QW_ERR_JRET(ctx->rspCode);
D
dapan1121 已提交
563 564
  }

D
dapan1121 已提交
565
  if (QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
566
    if (QW_PHASE_POST_FETCH != phase || ((!QW_QUERY_RUNNING(ctx)) && qwTaskNotInExec(ctx))) {
567 568 569
      QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
      QW_ERR_JRET(ctx->rspCode);
    }
D
dapan1121 已提交
570 571 572
  }

  if (ctx->rspCode) {
dengyihao's avatar
dengyihao 已提交
573 574
    QW_TASK_ELOG("task already failed, phase %s, error:%x - %s", qwPhaseStr(phase), ctx->rspCode,
                 tstrerror(ctx->rspCode));
D
dapan1121 已提交
575
    QW_ERR_JRET(ctx->rspCode);
dengyihao's avatar
dengyihao 已提交
576
  }
D
dapan1121 已提交
577

D
dapan1121 已提交
578
  QW_ERR_JRET(input->code);
D
dapan1121 已提交
579 580 581

_return:

D
dapan1121 已提交
582
  if (TSDB_CODE_SUCCESS == code && QW_PHASE_POST_QUERY == phase) {
D
dapan1121 已提交
583
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_PART_SUCC);
584
    ctx->queryGotData = true;
D
dapan1121 已提交
585 586
  }

587
  if (QW_PHASE_POST_QUERY == phase && ctx && !ctx->queryRsped) {
D
dapan1121 已提交
588 589 590 591
    bool   rsped = false;
    SQWMsg qwMsg = {.msgType = ctx->msgType, .connInfo = ctx->ctrlConnInfo};
    qwDbgSimulateRedirect(&qwMsg, ctx, &rsped);
    qwDbgSimulateDead(QW_FPARAMS(), ctx, &rsped);
dengyihao's avatar
dengyihao 已提交
592
    if (!rsped) {
D
dapan1121 已提交
593
      qwSendQueryRsp(QW_FPARAMS(), input->msgType + 1, ctx, code, false);
594
    }
595 596
  }

D
dapan1121 已提交
597
  if (ctx) {
D
dapan1121 已提交
598
    QW_UPDATE_RSP_CODE(ctx, code);
D
dapan1121 已提交
599

600 601 602
    if (QW_PHASE_POST_CQUERY != phase) {
      QW_SET_PHASE(ctx, phase);
    }
dengyihao's avatar
dengyihao 已提交
603

D
dapan1121 已提交
604
    QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
605
    qwReleaseTaskCtx(mgmt, ctx);
D
dapan1121 已提交
606 607
  }

D
dapan1121 已提交
608
  if (code) {
D
dapan1121 已提交
609
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_FAIL);
D
dapan1121 已提交
610 611
  }

D
dapan1121 已提交
612
  QW_TASK_DLOG("end to handle event at phase %s, code:%x - %s", qwPhaseStr(phase), code, tstrerror(code));
D
dapan1121 已提交
613

D
dapan1121 已提交
614 615 616
  QW_RET(code);
}

D
dapan1121 已提交
617 618 619 620 621 622
int32_t qwAbortPrerocessQuery(QW_FPARAMS_DEF) {
  QW_ERR_RET(qwDropTask(QW_FPARAMS()));

  QW_RET(TSDB_CODE_SUCCESS);
}

D
dapan1121 已提交
623
int32_t qwPreprocessQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
H
Hongze Cheng 已提交
624 625
  int32_t     code = 0;
  SQWTaskCtx *ctx = NULL;
D
dapan1121 已提交
626

D
dapan1121 已提交
627
  QW_ERR_JRET(qwRegisterQueryBrokenLinkArg(QW_FPARAMS(), &qwMsg->connInfo));
D
dapan1121 已提交
628

D
dapan1121 已提交
629 630 631
  QW_ERR_JRET(qwAddTaskCtx(QW_FPARAMS()));

  QW_ERR_JRET(qwAcquireTaskCtx(QW_FPARAMS(), &ctx));
D
dapan1121 已提交
632

D
dapan1121 已提交
633
  ctx->ctrlConnInfo = qwMsg->connInfo;
634
  ctx->sId = sId;
D
dapan1121 已提交
635
  ctx->phase = -1;
D
dapan1121 已提交
636

D
dapan1121 已提交
637
  QW_ERR_JRET(qwAddTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_INIT));
D
dapan1121 已提交
638 639 640 641 642 643 644 645 646 647 648

_return:

  if (ctx) {
    QW_UPDATE_RSP_CODE(ctx, code);
    qwReleaseTaskCtx(mgmt, ctx);
  }

  QW_RET(TSDB_CODE_SUCCESS);
}

649
int32_t qwProcessQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg, char *sql) {
D
dapan1121 已提交
650 651 652 653 654 655 656 657
  int32_t        code = 0;
  bool           queryRsped = false;
  SSubplan      *plan = NULL;
  SQWPhaseInput  input = {0};
  qTaskInfo_t    pTaskInfo = NULL;
  DataSinkHandle sinkHandle = NULL;
  SQWTaskCtx    *ctx = NULL;

D
dapan1121 已提交
658
  QW_ERR_JRET(qwHandlePrePhaseEvents(QW_FPARAMS(), QW_PHASE_PRE_QUERY, &input, NULL));
D
dapan1121 已提交
659 660

  QW_ERR_JRET(qwGetTaskCtx(QW_FPARAMS(), &ctx));
dengyihao's avatar
dengyihao 已提交
661

D
dapan1121 已提交
662 663 664
  ctx->taskType = qwMsg->msgInfo.taskType;
  ctx->explain = qwMsg->msgInfo.explain;
  ctx->needFetch = qwMsg->msgInfo.needFetch;
D
dapan1121 已提交
665
  ctx->msgType = qwMsg->msgType;
D
dapan1121 已提交
666
  ctx->localExec = false;
X
Xiaoyu Wang 已提交
667

668
  // QW_TASK_DLOGL("subplan json string, len:%d, %s", qwMsg->msgLen, qwMsg->msg);
X
Xiaoyu Wang 已提交
669

670
  code = qMsgToSubplan(qwMsg->msg, qwMsg->msgLen, &plan);
D
dapan1121 已提交
671
  if (TSDB_CODE_SUCCESS != code) {
672 673
    code = TSDB_CODE_INVALID_MSG;
    QW_TASK_ELOG("task physical plan to subplan failed, code:%x - %s", code, tstrerror(code));
D
dapan1121 已提交
674
    QW_ERR_JRET(code);
D
dapan1121 已提交
675
  }
dengyihao's avatar
dengyihao 已提交
676

677
  code = qCreateExecTask(qwMsg->node, mgmt->nodeId, tId, plan, &pTaskInfo, &sinkHandle, sql, OPTR_EXEC_MODEL_BATCH);
D
dapan1121 已提交
678
  sql = NULL;
D
dapan1121 已提交
679
  if (code) {
D
dapan1121 已提交
680
    QW_TASK_ELOG("qCreateExecTask failed, code:%x - %s", code, tstrerror(code));
D
dapan1121 已提交
681
    QW_ERR_JRET(code);
D
dapan1121 已提交
682
  }
D
dapan1121 已提交
683

H
Haojun Liao 已提交
684
  if (NULL == sinkHandle || NULL == pTaskInfo) {
D
dapan1121 已提交
685
    QW_TASK_ELOG("create task result error, taskHandle:%p, sinkHandle:%p", pTaskInfo, sinkHandle);
S
Shengliang Guan 已提交
686
    QW_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
687 688
  }

D
dapan1121 已提交
689 690
  qwSendQueryRsp(QW_FPARAMS(), qwMsg->msgType + 1, ctx, code, true);

691
  ctx->level = plan->level;
D
dapan1121 已提交
692 693 694
  atomic_store_ptr(&ctx->taskHandle, pTaskInfo);
  atomic_store_ptr(&ctx->sinkHandle, sinkHandle);

D
dapan1121 已提交
695 696
  qwSaveTbVersionInfo(pTaskInfo, ctx);
  QW_ERR_JRET(qwExecTask(QW_FPARAMS(), ctx, NULL));
dengyihao's avatar
dengyihao 已提交
697

D
dapan1121 已提交
698 699
_return:

D
dapan1121 已提交
700
  taosMemoryFree(sql);
701

D
dapan1121 已提交
702
  input.code = code;
D
dapan1121 已提交
703
  input.msgType = qwMsg->msgType;
D
dapan1121 已提交
704
  code = qwHandlePostPhaseEvents(QW_FPARAMS(), QW_PHASE_POST_QUERY, &input, NULL);
705

D
dapan1121 已提交
706
  if (QUERY_RSP_POLICY_QUICK == tsQueryRspPolicy && ctx != NULL && QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
dengyihao's avatar
dengyihao 已提交
707 708
    void       *rsp = NULL;
    int32_t     dataLen = 0;
709
    SOutputData sOutput = {0};
D
dapan1121 已提交
710 711 712
    if (qwGetQueryResFromSink(QW_FPARAMS(), ctx, &dataLen, &rsp, &sOutput)) {
      return TSDB_CODE_SUCCESS;
    }
H
Haojun Liao 已提交
713

714 715
    if (rsp) {
      bool qComplete = (DS_BUF_EMPTY == sOutput.bufStatus && sOutput.queryEnd);
H
Haojun Liao 已提交
716

717 718 719 720
      qwBuildFetchRsp(rsp, &sOutput, dataLen, qComplete);
      if (qComplete) {
        atomic_store_8((int8_t *)&ctx->queryEnd, true);
      }
H
Haojun Liao 已提交
721

722 723
      qwMsg->connInfo = ctx->dataConnInfo;
      QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_FETCH);
H
Haojun Liao 已提交
724

725
      qwBuildAndSendFetchRsp(ctx->msgType + 1, &qwMsg->connInfo, rsp, dataLen, code);
726
      rsp = NULL;
H
Haojun Liao 已提交
727

dengyihao's avatar
dengyihao 已提交
728 729
      QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code, tstrerror(code),
                   dataLen);
730 731 732
    }
  }

D
dapan1121 已提交
733
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
734 735
}

D
dapan1121 已提交
736
int32_t qwProcessCQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
737
  SQWTaskCtx   *ctx = NULL;
dengyihao's avatar
dengyihao 已提交
738
  int32_t       code = 0;
739
  SQWPhaseInput input = {0};
740
  void         *rsp = NULL;
dengyihao's avatar
dengyihao 已提交
741
  int32_t       dataLen = 0;
742
  bool          queryStop = false;
D
dapan1121 已提交
743
  bool          qComplete = false;
dengyihao's avatar
dengyihao 已提交
744

D
dapan1121 已提交
745
  do {
D
dapan1121 已提交
746
    ctx = NULL;
dengyihao's avatar
dengyihao 已提交
747

D
dapan1121 已提交
748
    QW_ERR_JRET(qwHandlePrePhaseEvents(QW_FPARAMS(), QW_PHASE_PRE_CQUERY, &input, NULL));
D
dapan1121 已提交
749

D
dapan1121 已提交
750
    QW_ERR_JRET(qwGetTaskCtx(QW_FPARAMS(), &ctx));
D
dapan1121 已提交
751

dengyihao's avatar
dengyihao 已提交
752 753
    atomic_store_8((int8_t *)&ctx->queryInQueue, 0);
    atomic_store_8((int8_t *)&ctx->queryContinue, 0);
D
dapan1121 已提交
754

755
    QW_ERR_JRET(qwExecTask(QW_FPARAMS(), ctx, &queryStop));
D
dapan1121 已提交
756

D
dapan1121 已提交
757
    if (QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
D
dapan1121 已提交
758
      SOutputData sOutput = {0};
D
dapan1121 已提交
759
      QW_ERR_JRET(qwGetQueryResFromSink(QW_FPARAMS(), ctx, &dataLen, &rsp, &sOutput));
dengyihao's avatar
dengyihao 已提交
760 761

      if ((!sOutput.queryEnd) && (DS_BUF_LOW == sOutput.bufStatus || DS_BUF_EMPTY == sOutput.bufStatus)) {
D
dapan1121 已提交
762
        QW_TASK_DLOG("task not end and buf is %s, need to continue query", qwBufStatusStr(sOutput.bufStatus));
dengyihao's avatar
dengyihao 已提交
763 764

        atomic_store_8((int8_t *)&ctx->queryContinue, 1);
765
      }
dengyihao's avatar
dengyihao 已提交
766

D
dapan1121 已提交
767
      if (rsp) {
D
dapan1121 已提交
768
        qComplete = (DS_BUF_EMPTY == sOutput.bufStatus && sOutput.queryEnd);
dengyihao's avatar
dengyihao 已提交
769

D
dapan1121 已提交
770
        qwBuildFetchRsp(rsp, &sOutput, dataLen, qComplete);
D
dapan1121 已提交
771
        if (qComplete) {
dengyihao's avatar
dengyihao 已提交
772
          atomic_store_8((int8_t *)&ctx->queryEnd, true);
D
dapan1121 已提交
773
          atomic_store_8((int8_t *)&ctx->queryContinue, 0);
D
dapan1121 已提交
774
        }
H
Haojun Liao 已提交
775

D
dapan1121 已提交
776
        qwMsg->connInfo = ctx->dataConnInfo;
dengyihao's avatar
dengyihao 已提交
777 778
        QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_FETCH);

779
        qwBuildAndSendFetchRsp(ctx->msgType + 1, &qwMsg->connInfo, rsp, dataLen, code);
D
dapan1121 已提交
780
        rsp = NULL;
781

dengyihao's avatar
dengyihao 已提交
782 783
        QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code,
                     tstrerror(code), dataLen);
D
dapan1121 已提交
784
      } else {
dengyihao's avatar
dengyihao 已提交
785
        atomic_store_8((int8_t *)&ctx->queryContinue, 1);
786 787 788
      }
    }

dengyihao's avatar
dengyihao 已提交
789
  _return:
790

D
dapan1121 已提交
791 792 793 794
    if (NULL == ctx) {
      break;
    }

D
dapan1121 已提交
795
    if (code && QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
dengyihao's avatar
dengyihao 已提交
796
      QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_FETCH);
797 798
      qwFreeFetchRsp(rsp);
      rsp = NULL;
dengyihao's avatar
dengyihao 已提交
799

D
dapan1121 已提交
800
      qwMsg->connInfo = ctx->dataConnInfo;
801
      qwBuildAndSendFetchRsp(ctx->msgType + 1, &qwMsg->connInfo, NULL, 0, code);
dengyihao's avatar
dengyihao 已提交
802 803
      QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code, tstrerror(code),
                   0);
804
    }
D
dapan1121 已提交
805

D
dapan1121 已提交
806
    QW_LOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
807
    if (atomic_load_8((int8_t*)&ctx->queryEnd) || (queryStop && (0 == atomic_load_8((int8_t *)&ctx->queryContinue))) || code) {
D
dapan1121 已提交
808
      // Note: query is not running anymore
809
      QW_SET_PHASE(ctx, QW_PHASE_POST_CQUERY);
dengyihao's avatar
dengyihao 已提交
810
      QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
811 812
      break;
    }
dengyihao's avatar
dengyihao 已提交
813
    QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
814
  } while (true);
D
dapan1121 已提交
815

D
dapan1121 已提交
816
  input.code = code;
dengyihao's avatar
dengyihao 已提交
817
  qwHandlePostPhaseEvents(QW_FPARAMS(), QW_PHASE_POST_CQUERY, &input, NULL);
D
dapan1121 已提交
818

dengyihao's avatar
dengyihao 已提交
819
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
820
}
D
dapan1121 已提交
821

D
dapan1121 已提交
822
int32_t qwProcessFetch(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
dengyihao's avatar
dengyihao 已提交
823 824 825
  int32_t       code = 0;
  int32_t       dataLen = 0;
  bool          locked = false;
826 827
  SQWTaskCtx   *ctx = NULL;
  void         *rsp = NULL;
D
dapan1121 已提交
828
  SQWPhaseInput input = {0};
D
dapan1121 已提交
829

D
dapan1121 已提交
830
  QW_ERR_JRET(qwHandlePrePhaseEvents(QW_FPARAMS(), QW_PHASE_PRE_FETCH, &input, NULL));
831

832
  QW_ERR_JRET(qwGetTaskCtx(QW_FPARAMS(), &ctx));
dengyihao's avatar
dengyihao 已提交
833

D
dapan1121 已提交
834
  ctx->msgType = qwMsg->msgType;
D
dapan1121 已提交
835
  ctx->dataConnInfo = qwMsg->connInfo;
D
dapan1121 已提交
836

D
dapan 已提交
837
  SOutputData sOutput = {0};
D
dapan1121 已提交
838
  QW_ERR_JRET(qwGetQueryResFromSink(QW_FPARAMS(), ctx, &dataLen, &rsp, &sOutput));
D
dapan1121 已提交
839

840 841
  if (NULL == rsp) {
    QW_SET_EVENT_RECEIVED(ctx, QW_EVENT_FETCH);
D
dapan1121 已提交
842
  } else {
D
dapan1121 已提交
843
    bool qComplete = (DS_BUF_EMPTY == sOutput.bufStatus && sOutput.queryEnd);
dengyihao's avatar
dengyihao 已提交
844

D
dapan1121 已提交
845
    qwBuildFetchRsp(rsp, &sOutput, dataLen, qComplete);
D
dapan1121 已提交
846
    if (qComplete) {
dengyihao's avatar
dengyihao 已提交
847
      atomic_store_8((int8_t *)&ctx->queryEnd, true);
D
dapan1121 已提交
848
    }
D
dapan1121 已提交
849 850
  }

dengyihao's avatar
dengyihao 已提交
851
  if ((!sOutput.queryEnd) && (DS_BUF_LOW == sOutput.bufStatus || DS_BUF_EMPTY == sOutput.bufStatus)) {
D
dapan1121 已提交
852
    QW_TASK_DLOG("task not end and buf is %s, need to continue query", qwBufStatusStr(sOutput.bufStatus));
D
dapan1121 已提交
853

D
dapan1121 已提交
854 855
    QW_LOCK(QW_WRITE, &ctx->lock);
    locked = true;
856

D
dapan1121 已提交
857
    // RC WARNING
858 859 860
    if (-1 == ctx->phase || false == ctx->queryGotData) {
      QW_TASK_DLOG_E("task query unfinished");
    } else if (QW_QUERY_RUNNING(ctx)) {
dengyihao's avatar
dengyihao 已提交
861 862
      atomic_store_8((int8_t *)&ctx->queryContinue, 1);
    } else if (0 == atomic_load_8((int8_t *)&ctx->queryInQueue)) {
D
dapan1121 已提交
863
      qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_EXEC);
D
dapan1121 已提交
864

dengyihao's avatar
dengyihao 已提交
865 866
      atomic_store_8((int8_t *)&ctx->queryInQueue, 1);

D
dapan1121 已提交
867
      QW_ERR_JRET(qwBuildAndSendCQueryMsg(QW_FPARAMS(), &qwMsg->connInfo));
868
    }
D
dapan 已提交
869
  }
dengyihao's avatar
dengyihao 已提交
870

D
dapan1121 已提交
871
_return:
D
dapan1121 已提交
872

D
dapan1121 已提交
873 874 875 876 877
  if (locked) {
    QW_UNLOCK(QW_WRITE, &ctx->lock);
  }

  input.code = code;
D
dapan1121 已提交
878
  code = qwHandlePostPhaseEvents(QW_FPARAMS(), QW_PHASE_POST_FETCH, &input, NULL);
D
dapan1121 已提交
879

D
dapan 已提交
880 881 882
  if (code) {
    qwFreeFetchRsp(rsp);
    rsp = NULL;
D
dapan1121 已提交
883
    dataLen = 0;
D
dapan1121 已提交
884 885 886
  }

  if (code || rsp) {
D
dapan1121 已提交
887
    bool rsped = false;
D
dapan1121 已提交
888
    if (ctx) {
889
      qwDbgSimulateRedirect(qwMsg, ctx, &rsped);
D
dapan1121 已提交
890 891
      qwDbgSimulateDead(QW_FPARAMS(), ctx, &rsped);
    }
D
dapan1121 已提交
892 893
    if (!rsped) {
      qwBuildAndSendFetchRsp(qwMsg->msgType + 1, &qwMsg->connInfo, rsp, dataLen, code);
894 895
      QW_TASK_DLOG("%s send, handle:%p, code:%x - %s, dataLen:%d", TMSG_INFO(qwMsg->msgType + 1),
                   qwMsg->connInfo.handle, code, tstrerror(code), dataLen);
D
dapan1121 已提交
896
    }
D
dapan1121 已提交
897 898
  }

D
dapan1121 已提交
899
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
900
}
D
dapan1121 已提交
901

D
dapan1121 已提交
902
int32_t qwProcessDrop(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
dengyihao's avatar
dengyihao 已提交
903
  int32_t     code = 0;
D
dapan1121 已提交
904
  bool        dropped = false;
D
dapan1121 已提交
905
  SQWTaskCtx *ctx = NULL;
dengyihao's avatar
dengyihao 已提交
906
  bool        locked = false;
D
dapan1121 已提交
907

D
dapan1121 已提交
908
  QW_ERR_JRET(qwAcquireTaskCtx(QW_FPARAMS(), &ctx));
dengyihao's avatar
dengyihao 已提交
909

D
dapan1121 已提交
910 911 912 913
  QW_LOCK(QW_WRITE, &ctx->lock);

  locked = true;

D
dapan1121 已提交
914
  if (QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
915
    QW_TASK_WLOG_E("task already dropping");
D
dapan1121 已提交
916 917 918
    QW_ERR_JRET(TSDB_CODE_QRY_DUPLICATTED_OPERATION);
  }

D
dapan1121 已提交
919
  if (QW_QUERY_RUNNING(ctx)) {
D
dapan1121 已提交
920
    QW_ERR_JRET(qwKillTaskHandle(ctx, TSDB_CODE_TSC_QUERY_CANCELLED));
D
dapan1121 已提交
921
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_DROP);
D
dapan1121 已提交
922
  } else {
D
dapan1121 已提交
923
    QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
D
dapan1121 已提交
924
    dropped = true;
D
dapan1121 已提交
925
  }
D
dapan1121 已提交
926

D
dapan1121 已提交
927
  if (!dropped) {
D
dapan1121 已提交
928
    QW_UPDATE_RSP_CODE(ctx, TSDB_CODE_TSC_QUERY_CANCELLED);
D
dapan1121 已提交
929 930
    QW_SET_EVENT_RECEIVED(ctx, QW_EVENT_DROP);
  }
931

D
dapan1121 已提交
932
_return:
D
dapan1121 已提交
933

D
dapan1121 已提交
934
  if (code) {
D
dapan1121 已提交
935 936 937
    if (ctx) {
      QW_UPDATE_RSP_CODE(ctx, code);
    }
H
Haojun Liao 已提交
938

D
dapan1121 已提交
939
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_FAIL);
D
dapan1121 已提交
940 941
  }

D
dapan 已提交
942 943 944 945
  if (locked) {
    QW_UNLOCK(QW_WRITE, &ctx->lock);
  }

D
dapan1121 已提交
946
  if (ctx) {
D
dapan1121 已提交
947
    qwReleaseTaskCtx(mgmt, ctx);
D
dapan1121 已提交
948 949
  }

D
dapan1121 已提交
950 951 952
  QW_RET(TSDB_CODE_SUCCESS);
}

D
dapan1121 已提交
953
int32_t qwProcessHb(SQWorker *mgmt, SQWMsg *qwMsg, SSchedulerHbReq *req) {
dengyihao's avatar
dengyihao 已提交
954
  int32_t         code = 0;
D
dapan1121 已提交
955
  SSchedulerHbRsp rsp = {0};
956
  SQWSchStatus   *sch = NULL;
D
dapan1121 已提交
957

D
dapan1121 已提交
958 959 960
  if (qwMsg->code) {
    QW_RET(qwProcessHbLinkBroken(mgmt, qwMsg, req));
  }
D
dapan1121 已提交
961 962

  QW_ERR_JRET(qwAcquireAddScheduler(mgmt, req->sId, QW_READ, &sch));
D
dapan1121 已提交
963 964
  QW_ERR_JRET(qwRegisterHbBrokenLinkArg(mgmt, req->sId, &qwMsg->connInfo));

D
dapan1121 已提交
965 966
  sch->hbBrokenTs = 0;

D
dapan1121 已提交
967
  QW_LOCK(QW_WRITE, &sch->hbConnLock);
D
dapan1121 已提交
968

D
dapan1121 已提交
969
  if (sch->hbConnInfo.handle) {
970
    tmsgReleaseHandle(&sch->hbConnInfo, TAOS_CONN_SERVER);
S
Shengliang Guan 已提交
971
    sch->hbConnInfo.handle = NULL;
D
dapan1121 已提交
972
  }
D
dapan1121 已提交
973

D
dapan1121 已提交
974
  memcpy(&sch->hbConnInfo, &qwMsg->connInfo, sizeof(qwMsg->connInfo));
D
dapan1121 已提交
975
  memcpy(&sch->hbEpId, &req->epId, sizeof(req->epId));
dengyihao's avatar
dengyihao 已提交
976

D
dapan1121 已提交
977
  QW_UNLOCK(QW_WRITE, &sch->hbConnLock);
dengyihao's avatar
dengyihao 已提交
978 979 980

  QW_DLOG("hb connection updated, sId:%" PRIx64 ", nodeId:%d, fqdn:%s, port:%d, handle:%p, ahandle:%p", req->sId,
          req->epId.nodeId, req->epId.ep.fqdn, req->epId.ep.port, qwMsg->connInfo.handle, qwMsg->connInfo.ahandle);
D
dapan1121 已提交
981

D
dapan1121 已提交
982 983 984 985
  qwReleaseScheduler(QW_READ, mgmt);

_return:

D
dapan1121 已提交
986 987
  memcpy(&rsp.epId, &req->epId, sizeof(req->epId));

S
shm  
Shengliang Guan 已提交
988
  qwBuildAndSendHbRsp(&qwMsg->connInfo, &rsp, code);
D
dapan1121 已提交
989 990

  if (code) {
991
    tmsgReleaseHandle(&qwMsg->connInfo, TAOS_CONN_SERVER);
S
Shengliang Guan 已提交
992
    qwMsg->connInfo.handle = NULL;
D
dapan1121 已提交
993
  }
dengyihao's avatar
dengyihao 已提交
994

995
  /*QW_DLOG("hb rsp send, handle:%p, code:%x - %s", qwMsg->connInfo.handle, code, tstrerror(code));*/
dengyihao's avatar
dengyihao 已提交
996

D
dapan1121 已提交
997
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
998 999 1000
}

void qwProcessHbTimerEvent(void *param, void *tmrId) {
1001
  SQWHbParam *hbParam = (SQWHbParam *)param;
D
dapan1121 已提交
1002 1003 1004 1005
  if (hbParam->qwrId != atomic_load_32(&gQwMgmt.qwRef)) {
    return;
  }

1006
  int64_t   refId = hbParam->refId;
D
dapan1121 已提交
1007 1008 1009 1010 1011
  SQWorker *mgmt = qwAcquire(refId);
  if (NULL == mgmt) {
    QW_DLOG("qwAcquire %" PRIx64 "failed", refId);
    return;
  }
1012

D
dapan1121 已提交
1013
  SQWSchStatus *sch = NULL;
dengyihao's avatar
dengyihao 已提交
1014
  int32_t       taskNum = 0;
1015
  SQWHbInfo    *rspList = NULL;
D
dapan1121 已提交
1016
  SArray       *pExpiredSch = NULL;
dengyihao's avatar
dengyihao 已提交
1017
  int32_t       code = 0;
D
dapan1121 已提交
1018

D
dapan1121 已提交
1019 1020
  qwDbgDumpMgmtInfo(mgmt);

1021 1022 1023 1024
  if (gQWDebug.forceStop) {
    (void)qwStopAllTasks(mgmt);
  }

D
dapan1121 已提交
1025 1026 1027 1028 1029
  QW_LOCK(QW_READ, &mgmt->schLock);

  int32_t schNum = taosHashGetSize(mgmt->schHash);
  if (schNum <= 0) {
    QW_UNLOCK(QW_READ, &mgmt->schLock);
D
dapan1121 已提交
1030
    taosTmrReset(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, param, mgmt->timer, &mgmt->hbTimer);
D
dapan1121 已提交
1031
    qwRelease(refId);
D
dapan1121 已提交
1032
    return;
D
dapan1121 已提交
1033 1034
  }

wafwerar's avatar
wafwerar 已提交
1035
  rspList = taosMemoryCalloc(schNum, sizeof(SQWHbInfo));
D
dapan1121 已提交
1036 1037
  pExpiredSch = taosArrayInit(schNum, sizeof(uint64_t));
  if (NULL == rspList || NULL == pExpiredSch) {
D
dapan1121 已提交
1038
    QW_UNLOCK(QW_READ, &mgmt->schLock);
D
dapan1121 已提交
1039 1040
    taosMemoryFree(rspList);
    taosArrayDestroy(pExpiredSch);
D
dapan1121 已提交
1041 1042
    QW_ELOG("calloc %d SQWHbInfo failed", schNum);
    taosTmrReset(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, param, mgmt->timer, &mgmt->hbTimer);
D
dapan1121 已提交
1043
    qwRelease(refId);
D
dapan1121 已提交
1044
    return;
D
dapan1121 已提交
1045 1046
  }

1047
  void   *key = NULL;
dengyihao's avatar
dengyihao 已提交
1048
  size_t  keyLen = 0;
D
dapan1121 已提交
1049
  int32_t i = 0;
D
dapan1121 已提交
1050
  int64_t currentMs = taosGetTimestampMs();
D
dapan1121 已提交
1051 1052 1053

  void *pIter = taosHashIterate(mgmt->schHash, NULL);
  while (pIter) {
H
Haojun Liao 已提交
1054 1055
    SQWSchStatus *sch1 = (SQWSchStatus *)pIter;
    if (NULL == sch1->hbConnInfo.handle) {
D
dapan1121 已提交
1056
      uint64_t *sId = taosHashGetKey(pIter, NULL);
D
dapan1121 已提交
1057
      QW_TLOG("cancel send hb to sch %" PRIx64 " cause of no connection handle", *sId);
D
dapan1121 已提交
1058

H
Haojun Liao 已提交
1059 1060
      if (sch1->hbBrokenTs > 0 && ((currentMs - sch1->hbBrokenTs) > QW_SCH_TIMEOUT_MSEC) &&
          taosHashGetSize(sch1->tasksHash) <= 0) {
D
dapan1121 已提交
1061 1062
        taosArrayPush(pExpiredSch, sId);
      }
1063

D
dapan1121 已提交
1064 1065 1066
      pIter = taosHashIterate(mgmt->schHash, pIter);
      continue;
    }
dengyihao's avatar
dengyihao 已提交
1067

D
dapan1121 已提交
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
    code = qwGenerateSchHbRsp(mgmt, (SQWSchStatus *)pIter, &rspList[i]);
    if (code) {
      taosHashCancelIterate(mgmt->schHash, pIter);
      QW_ERR_JRET(code);
    }

    ++i;
    pIter = taosHashIterate(mgmt->schHash, pIter);
  }

_return:

  QW_UNLOCK(QW_READ, &mgmt->schLock);

  for (int32_t j = 0; j < i; ++j) {
S
shm  
Shengliang Guan 已提交
1083
    qwBuildAndSendHbRsp(&rspList[j].connInfo, &rspList[j].rsp, code);
1084 1085
    /*QW_DLOG("hb rsp send, handle:%p, code:%x - %s, taskNum:%d", rspList[j].connInfo.handle, code, tstrerror(code),*/
    /*(rspList[j].rsp.taskStatus ? (int32_t)taosArrayGetSize(rspList[j].rsp.taskStatus) : 0));*/
D
dapan1121 已提交
1086
    tFreeSSchedulerHbRsp(&rspList[j].rsp);
D
dapan1121 已提交
1087 1088
  }

D
dapan1121 已提交
1089
  if (taosArrayGetSize(pExpiredSch) > 0) {
D
dapan1121 已提交
1090
    qwClearExpiredSch(mgmt, pExpiredSch);
D
dapan1121 已提交
1091 1092
  }

wafwerar's avatar
wafwerar 已提交
1093
  taosMemoryFreeClear(rspList);
D
dapan1121 已提交
1094
  taosArrayDestroy(pExpiredSch);
D
dapan1121 已提交
1095

dengyihao's avatar
dengyihao 已提交
1096
  taosTmrReset(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, param, mgmt->timer, &mgmt->hbTimer);
1097
  qwRelease(refId);
D
dapan1121 已提交
1098 1099
}

1100
int32_t qwProcessDelete(QW_FPARAMS_DEF, SQWMsg *qwMsg, SDeleteRes *pRes) {
D
dapan1121 已提交
1101 1102 1103 1104 1105 1106
  int32_t        code = 0;
  SSubplan      *plan = NULL;
  qTaskInfo_t    pTaskInfo = NULL;
  DataSinkHandle sinkHandle = NULL;
  SQWTaskCtx     ctx = {0};

1107
  code = qMsgToSubplan(qwMsg->msg, qwMsg->msgLen, &plan);
D
dapan1121 已提交
1108 1109 1110 1111 1112 1113
  if (TSDB_CODE_SUCCESS != code) {
    code = TSDB_CODE_INVALID_MSG;
    QW_TASK_ELOG("task physical plan to subplan failed, code:%x - %s", code, tstrerror(code));
    QW_ERR_JRET(code);
  }

1114
  code = qCreateExecTask(qwMsg->node, mgmt->nodeId, tId, plan, &pTaskInfo, &sinkHandle, NULL, OPTR_EXEC_MODEL_BATCH);
D
dapan1121 已提交
1115 1116 1117 1118 1119 1120 1121
  if (code) {
    QW_TASK_ELOG("qCreateExecTask failed, code:%x - %s", code, tstrerror(code));
    QW_ERR_JRET(code);
  }

  if (NULL == sinkHandle || NULL == pTaskInfo) {
    QW_TASK_ELOG("create task result error, taskHandle:%p, sinkHandle:%p", pTaskInfo, sinkHandle);
S
Shengliang Guan 已提交
1122
    QW_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
1123 1124
  }

D
dapan1121 已提交
1125 1126
  ctx.taskHandle = pTaskInfo;
  ctx.sinkHandle = sinkHandle;
D
dapan1121 已提交
1127

D
dapan1121 已提交
1128
  QW_ERR_JRET(qwExecTask(QW_FPARAMS(), &ctx, NULL));
D
dapan1121 已提交
1129

1130
  QW_ERR_JRET(qwGetDeleteResFromSink(QW_FPARAMS(), &ctx, pRes));
D
dapan1121 已提交
1131 1132 1133

_return:

D
dapan1121 已提交
1134
  qwFreeTaskCtx(&ctx);
D
dapan1121 已提交
1135 1136 1137 1138

  QW_RET(TSDB_CODE_SUCCESS);
}

1139

D
dapan1121 已提交
1140 1141
int32_t qWorkerInit(int8_t nodeType, int32_t nodeId, void **qWorkerMgmt, const SMsgCb *pMsgCb) {
  if (NULL == qWorkerMgmt || (pMsgCb && pMsgCb->mgmt == NULL)) {
D
dapan1121 已提交
1142 1143 1144
    qError("invalid param to init qworker");
    QW_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }
S
Shengliang 已提交
1145

D
dapan1121 已提交
1146 1147 1148 1149
  int32_t qwNum = atomic_add_fetch_32(&gQwMgmt.qwNum, 1);
  if (1 == qwNum) {
    memset(gQwMgmt.param, 0, sizeof(gQwMgmt.param));
  }
D
dapan1121 已提交
1150 1151 1152 1153 1154 1155 1156 1157

  int32_t code = qwOpenRef();
  if (code) {
    atomic_sub_fetch_32(&gQwMgmt.qwNum, 1);
    QW_RET(code);
  }

  SQWorker *mgmt = taosMemoryCalloc(1, sizeof(SQWorker));
D
dapan1121 已提交
1158
  if (NULL == mgmt) {
D
dapan1121 已提交
1159 1160
    qError("calloc %d failed", (int32_t)sizeof(SQWorker));
    atomic_sub_fetch_32(&gQwMgmt.qwNum, 1);
S
Shengliang Guan 已提交
1161
    QW_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1162 1163
  }

D
dapan1121 已提交
1164 1165 1166
  mgmt->cfg.maxSchedulerNum = QW_DEFAULT_SCHEDULER_NUMBER;
  mgmt->cfg.maxTaskNum = QW_DEFAULT_TASK_NUMBER;
  mgmt->cfg.maxSchTaskNum = QW_DEFAULT_SCH_TASK_NUMBER;
D
dapan1121 已提交
1167

dengyihao's avatar
dengyihao 已提交
1168 1169
  mgmt->schHash = taosHashInit(mgmt->cfg.maxSchedulerNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false,
                               HASH_ENTRY_LOCK);
D
dapan1121 已提交
1170
  if (NULL == mgmt->schHash) {
wafwerar's avatar
wafwerar 已提交
1171
    taosMemoryFreeClear(mgmt);
D
dapan1121 已提交
1172
    qError("init %d scheduler hash failed", mgmt->cfg.maxSchedulerNum);
S
Shengliang Guan 已提交
1173
    QW_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1174 1175
  }

dengyihao's avatar
dengyihao 已提交
1176 1177
  mgmt->ctxHash =
      taosHashInit(mgmt->cfg.maxTaskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
D
dapan1121 已提交
1178
  if (NULL == mgmt->ctxHash) {
D
dapan1121 已提交
1179
    qError("init %d task ctx hash failed", mgmt->cfg.maxTaskNum);
S
Shengliang Guan 已提交
1180
    QW_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1181 1182 1183 1184 1185
  }

  mgmt->timer = taosTmrInit(0, 0, 0, "qworker");
  if (NULL == mgmt->timer) {
    qError("init timer failed, error:%s", tstrerror(terrno));
S
Shengliang Guan 已提交
1186
    QW_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1187 1188
  }

D
dapan1121 已提交
1189 1190
  mgmt->nodeType = nodeType;
  mgmt->nodeId = nodeId;
D
dapan1121 已提交
1191 1192 1193 1194 1195
  if (pMsgCb) {
    mgmt->msgCb = *pMsgCb;
  } else {
    memset(&mgmt->msgCb, 0, sizeof(mgmt->msgCb));
  }
D
dapan1121 已提交
1196

D
dapan1121 已提交
1197 1198 1199 1200 1201 1202
  mgmt->refId = taosAddRef(gQwMgmt.qwRef, mgmt);
  if (mgmt->refId < 0) {
    qError("taosAddRef qw failed, error:%s", tstrerror(terrno));
    QW_ERR_JRET(terrno);
  }

D
dapan1121 已提交
1203 1204 1205
  SQWHbParam *param = NULL;
  qwSetHbParam(mgmt->refId, &param);

1206
  mgmt->hbTimer = taosTmrStart(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, (void *)param, mgmt->timer);
D
dapan1121 已提交
1207 1208
  if (NULL == mgmt->hbTimer) {
    qError("start hb timer failed");
S
Shengliang Guan 已提交
1209
    QW_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1210
  }
1211

D
dapan1121 已提交
1212 1213
  *qWorkerMgmt = mgmt;

S
Shengliang Guan 已提交
1214
  qDebug("qworker initialized, type:%d, id:%d, handle:%p", mgmt->nodeType, mgmt->nodeId, mgmt);
D
dapan1121 已提交
1215

D
dapan1121 已提交
1216
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1217 1218 1219

_return:

D
dapan1121 已提交
1220 1221 1222 1223 1224 1225 1226
  if (mgmt->refId >= 0) {
    qwRelease(mgmt->refId);
  } else {
    taosHashCleanup(mgmt->schHash);
    taosHashCleanup(mgmt->ctxHash);
    taosTmrCleanUp(mgmt->timer);
    taosMemoryFreeClear(mgmt);
D
dapan1121 已提交
1227

1228
    atomic_sub_fetch_32(&gQwMgmt.qwNum, 1);
D
dapan1121 已提交
1229
  }
1230

D
dapan1121 已提交
1231
  QW_RET(code);
D
dapan1121 已提交
1232
}
D
dapan1121 已提交
1233

D
dapan1121 已提交
1234
void qWorkerStopAllTasks(void *qWorkerMgmt) {
D
dapan1121 已提交
1235 1236 1237
  SQWorker *mgmt = (SQWorker *)qWorkerMgmt;

  QW_DLOG("start to stop all tasks, taskNum:%d", taosHashGetSize(mgmt->ctxHash));
1238
 
1239 1240
  atomic_store_8(&mgmt->nodeStopped, 1);

1241
  (void)qwStopAllTasks(mgmt);
D
dapan1121 已提交
1242 1243
}

D
dapan1121 已提交
1244 1245 1246
void qWorkerDestroy(void **qWorkerMgmt) {
  if (NULL == qWorkerMgmt || NULL == *qWorkerMgmt) {
    return;
D
dapan1121 已提交
1247
  }
D
dapan 已提交
1248

H
Hongze Cheng 已提交
1249
  int32_t   destroyed = 0;
D
dapan1121 已提交
1250
  SQWorker *mgmt = *qWorkerMgmt;
D
dapan1121 已提交
1251
  mgmt->destroyed = &destroyed;
D
dapan1121 已提交
1252

D
dapan1121 已提交
1253 1254
  if (taosRemoveRef(gQwMgmt.qwRef, mgmt->refId)) {
    qError("remove qw from ref list failed, refId:%" PRIx64, mgmt->refId);
D
dapan1121 已提交
1255 1256 1257 1258 1259
    return;
  }

  while (0 == destroyed) {
    taosMsleep(2);
D
dapan1121 已提交
1260
  }
D
dapan1121 已提交
1261
}
D
dapan1121 已提交
1262

D
dapan1121 已提交
1263 1264 1265 1266 1267
int32_t qWorkerGetStat(SReadHandle *handle, void *qWorkerMgmt, SQWorkerStat *pStat) {
  if (NULL == handle || NULL == qWorkerMgmt || NULL == pStat) {
    QW_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

1268
  SQWorker     *mgmt = (SQWorker *)qWorkerMgmt;
D
dapan1121 已提交
1269
  SDataSinkStat sinkStat = {0};
1270

D
dapan1121 已提交
1271 1272
  dsDataSinkGetCacheSize(&sinkStat);
  pStat->cacheDataSize = sinkStat.cachedSize;
1273

D
dapan1121 已提交
1274 1275 1276 1277 1278
  pStat->queryProcessed = QW_STAT_GET(mgmt->stat.msgStat.queryProcessed);
  pStat->cqueryProcessed = QW_STAT_GET(mgmt->stat.msgStat.cqueryProcessed);
  pStat->fetchProcessed = QW_STAT_GET(mgmt->stat.msgStat.fetchProcessed);
  pStat->dropProcessed = QW_STAT_GET(mgmt->stat.msgStat.dropProcessed);
  pStat->hbProcessed = QW_STAT_GET(mgmt->stat.msgStat.hbProcessed);
D
dapan1121 已提交
1279
  pStat->deleteProcessed = QW_STAT_GET(mgmt->stat.msgStat.deleteProcessed);
D
dapan1121 已提交
1280 1281 1282 1283 1284 1285 1286

  pStat->numOfQueryInQueue = handle->pMsgCb->qsizeFp(handle->pMsgCb->mgmt, mgmt->nodeId, QUERY_QUEUE);
  pStat->numOfFetchInQueue = handle->pMsgCb->qsizeFp(handle->pMsgCb->mgmt, mgmt->nodeId, FETCH_QUEUE);
  pStat->timeInQueryQueue = qwGetTimeInQueue((SQWorker *)qWorkerMgmt, QUERY_QUEUE);
  pStat->timeInFetchQueue = qwGetTimeInQueue((SQWorker *)qWorkerMgmt, FETCH_QUEUE);

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1287
}
D
dapan1121 已提交
1288

H
Hongze Cheng 已提交
1289 1290 1291
int32_t qWorkerProcessLocalQuery(void *pMgmt, uint64_t sId, uint64_t qId, uint64_t tId, int64_t rId, int32_t eId,
                                 SQWMsg *qwMsg, SArray *explainRes) {
  SQWorker      *mgmt = (SQWorker *)pMgmt;
D
dapan1121 已提交
1292 1293 1294 1295 1296 1297
  int32_t        code = 0;
  SQWTaskCtx    *ctx = NULL;
  SSubplan      *plan = (SSubplan *)qwMsg->msg;
  SQWPhaseInput  input = {0};
  qTaskInfo_t    pTaskInfo = NULL;
  DataSinkHandle sinkHandle = NULL;
H
Hongze Cheng 已提交
1298
  SReadHandle    rHandle = {0};
D
dapan1121 已提交
1299 1300

  QW_ERR_JRET(qwAddTaskCtx(QW_FPARAMS()));
D
dapan1121 已提交
1301
  QW_ERR_JRET(qwAddTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_INIT));
D
dapan1121 已提交
1302

D
dapan1121 已提交
1303
  QW_ERR_JRET(qwHandlePrePhaseEvents(QW_FPARAMS(), QW_PHASE_PRE_QUERY, &input, NULL));
D
dapan1121 已提交
1304 1305 1306 1307 1308 1309
  QW_ERR_JRET(qwAcquireTaskCtx(QW_FPARAMS(), &ctx));

  ctx->taskType = qwMsg->msgInfo.taskType;
  ctx->explain = qwMsg->msgInfo.explain;
  ctx->needFetch = qwMsg->msgInfo.needFetch;
  ctx->msgType = qwMsg->msgType;
D
dapan1121 已提交
1310 1311
  ctx->localExec = true;
  ctx->explainRes = explainRes;
D
dapan1121 已提交
1312

D
dapan1121 已提交
1313 1314
  rHandle.pMsgCb = taosMemoryCalloc(1, sizeof(SMsgCb));
  rHandle.pMsgCb->clientRpc = qwMsg->connInfo.handle;
H
Haojun Liao 已提交
1315

D
dapan1121 已提交
1316
  code = qCreateExecTask(&rHandle, mgmt->nodeId, tId, plan, &pTaskInfo, &sinkHandle, NULL, OPTR_EXEC_MODEL_BATCH);
D
dapan1121 已提交
1317 1318 1319 1320 1321 1322 1323
  if (code) {
    QW_TASK_ELOG("qCreateExecTask failed, code:%x - %s", code, tstrerror(code));
    QW_ERR_JRET(code);
  }

  if (NULL == sinkHandle || NULL == pTaskInfo) {
    QW_TASK_ELOG("create task result error, taskHandle:%p, sinkHandle:%p", pTaskInfo, sinkHandle);
S
Shengliang Guan 已提交
1324
    QW_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
  }

  ctx->level = plan->level;
  atomic_store_ptr(&ctx->taskHandle, pTaskInfo);
  atomic_store_ptr(&ctx->sinkHandle, sinkHandle);

  QW_ERR_JRET(qwExecTask(QW_FPARAMS(), ctx, NULL));

_return:

D
dapan1121 已提交
1335
  taosMemoryFree(rHandle.pMsgCb);
H
Haojun Liao 已提交
1336

D
dapan1121 已提交
1337 1338 1339 1340
  input.code = code;
  input.msgType = qwMsg->msgType;
  code = qwHandlePostPhaseEvents(QW_FPARAMS(), QW_PHASE_POST_QUERY, &input, NULL);

D
dapan1121 已提交
1341 1342 1343 1344 1345 1346 1347 1348
  if (ctx) {
    QW_UPDATE_RSP_CODE(ctx, code);
    qwReleaseTaskCtx(mgmt, ctx);
  }

  QW_RET(code);
}

H
Hongze Cheng 已提交
1349 1350 1351 1352 1353 1354 1355 1356
int32_t qWorkerProcessLocalFetch(void *pMgmt, uint64_t sId, uint64_t qId, uint64_t tId, int64_t rId, int32_t eId,
                                 void **pRsp, SArray *explainRes) {
  SQWorker   *mgmt = (SQWorker *)pMgmt;
  int32_t     code = 0;
  int32_t     dataLen = 0;
  SQWTaskCtx *ctx = NULL;
  void       *rsp = NULL;
  bool        queryStop = false;
D
dapan1121 已提交
1357 1358 1359 1360 1361 1362 1363 1364 1365

  SQWPhaseInput input = {0};

  QW_ERR_JRET(qwHandlePrePhaseEvents(QW_FPARAMS(), QW_PHASE_PRE_FETCH, &input, NULL));

  QW_ERR_JRET(qwGetTaskCtx(QW_FPARAMS(), &ctx));

  ctx->msgType = TDMT_SCH_MERGE_FETCH;
  ctx->explainRes = explainRes;
H
Haojun Liao 已提交
1366

D
dapan1121 已提交
1367
  SOutputData sOutput = {0};
H
Haojun Liao 已提交
1368

D
dapan1121 已提交
1369 1370 1371 1372 1373
  while (true) {
    QW_ERR_JRET(qwGetQueryResFromSink(QW_FPARAMS(), ctx, &dataLen, &rsp, &sOutput));

    if (NULL == rsp) {
      QW_ERR_JRET(qwExecTask(QW_FPARAMS(), ctx, &queryStop));
H
Haojun Liao 已提交
1374

D
dapan1121 已提交
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
      continue;
    } else {
      bool qComplete = (DS_BUF_EMPTY == sOutput.bufStatus && sOutput.queryEnd);

      qwBuildFetchRsp(rsp, &sOutput, dataLen, qComplete);
      if (qComplete) {
        atomic_store_8((int8_t *)&ctx->queryEnd, true);
      }

      break;
    }
  }

_return:

  *pRsp = rsp;

  input.code = code;
  code = qwHandlePostPhaseEvents(QW_FPARAMS(), QW_PHASE_POST_FETCH, &input, NULL);

  QW_RET(code);
}