qworker.c 37.9 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"
H
Haojun Liao 已提交
11
#include "tmsg.h"
12
#include "tname.h"
D
dapan1121 已提交
13
#include "tglobal.h"
D
dapan1121 已提交
14

D
dapan1121 已提交
15
SQWorkerMgmt gQwMgmt = {
16 17 18
    .lock = 0,
    .qwRef = -1,
    .qwNum = 0,
D
dapan1121 已提交
19
};
20

H
Hongze Cheng 已提交
21 22
static void freeBlock(void *param) {
  SSDataBlock *pBlock = *(SSDataBlock **)param;
H
Haojun Liao 已提交
23 24 25
  blockDataDestroy(pBlock);
}

D
dapan1121 已提交
26 27 28 29
int32_t qwProcessHbLinkBroken(SQWorker *mgmt, SQWMsg *qwMsg, SSchedulerHbReq *req) {
  int32_t         code = 0;
  SSchedulerHbRsp rsp = {0};
  SQWSchStatus   *sch = NULL;
D
dapan1121 已提交
30

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

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

D
dapan1121 已提交
35
  sch->hbBrokenTs = taosGetTimestampMs();
36

D
dapan1121 已提交
37 38 39 40
  if (qwMsg->connInfo.handle == sch->hbConnInfo.handle) {
    tmsgReleaseHandle(&sch->hbConnInfo, TAOS_CONN_SERVER);
    sch->hbConnInfo.handle = NULL;
    sch->hbConnInfo.ahandle = NULL;
D
dapan1121 已提交
41

D
dapan1121 已提交
42 43 44
    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 已提交
45
  }
D
dapan1121 已提交
46

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

D
dapan1121 已提交
49
  qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
50

D
dapan1121 已提交
51
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
52 53
}

54 55
static void freeItem(void *param) {
  SExplainExecInfo *pInfo = param;
H
Haojun Liao 已提交
56 57 58
  taosMemoryFree(pInfo->verboseInfo);
}

D
dapan1121 已提交
59
int32_t qwHandleTaskComplete(QW_FPARAMS_DEF, SQWTaskCtx *ctx) {
D
dapan1121 已提交
60
  qTaskInfo_t taskHandle = ctx->taskHandle;
D
dapan1121 已提交
61

D
dapan1121 已提交
62
  if (TASK_TYPE_TEMP == ctx->taskType && taskHandle) {
D
dapan1121 已提交
63
    if (ctx->explain) {
64
      SArray *execInfoList = taosArrayInit(4, sizeof(SExplainExecInfo));
H
Haojun Liao 已提交
65
      QW_ERR_RET(qGetExplainExecInfo(taskHandle, execInfoList));
66

D
dapan1121 已提交
67
      if (ctx->localExec) {
68 69 70 71 72 73 74 75 76 77 78
        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 已提交
79 80 81 82 83 84 85
      } else {
        SRpcHandleInfo connInfo = ctx->ctrlConnInfo;
        connInfo.ahandle = NULL;
        int32_t code = qwBuildAndSendExplainRsp(&connInfo, execInfoList);
        taosArrayDestroyEx(execInfoList, freeItem);
        QW_ERR_RET(code);
      }
D
dapan1121 已提交
86
    }
D
dapan1121 已提交
87 88 89 90

    if (!ctx->needFetch) {
      dsGetDataLength(ctx->sinkHandle, &ctx->affectedRows, NULL);
    }
D
dapan1121 已提交
91 92 93 94 95
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
96 97
int32_t qwSendQueryRsp(QW_FPARAMS_DEF, int32_t msgType, SQWTaskCtx *ctx, int32_t rspCode, bool quickRsp) {
  if ((!quickRsp) || QUERY_RSP_POLICY_QUICK == tsQueryRspPolicy) {
98 99 100 101 102
    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 已提交
103 104 105 106 107 108
    ctx->queryRsped = true;
  }

  return TSDB_CODE_SUCCESS;
}

109
int32_t qwExecTask(QW_FPARAMS_DEF, SQWTaskCtx *ctx, bool *queryStop) {
dengyihao's avatar
dengyihao 已提交
110 111 112 113 114
  int32_t        code = 0;
  bool           qcontinue = true;
  uint64_t       useconds = 0;
  int32_t        i = 0;
  int32_t        execNum = 0;
D
dapan1121 已提交
115
  qTaskInfo_t    taskHandle = ctx->taskHandle;
D
dapan1121 已提交
116
  DataSinkHandle sinkHandle = ctx->sinkHandle;
H
Hongze Cheng 已提交
117
  SLocalFetch    localFetch = {(void *)mgmt, ctx->localExec, qWorkerProcessLocalFetch, ctx->explainRes};
dengyihao's avatar
dengyihao 已提交
118

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

D
dapan1121 已提交
123
    // if *taskHandle is NULL, it's killed right now
124 125
    bool hasMore = false;

D
dapan1121 已提交
126
    if (taskHandle) {
D
dapan1121 已提交
127
      qwDbgSimulateSleep();
128 129

      code = qExecTaskOpt(taskHandle, pResList, &useconds, &hasMore, &localFetch);
D
dapan1121 已提交
130
      if (code) {
131 132 133 134 135
        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));
        }
136
        QW_ERR_JRET(code);
D
dapan1121 已提交
137
      }
D
dapan1121 已提交
138 139
    }

D
dapan1121 已提交
140 141
    ++execNum;

142 143
    size_t numOfResBlock = taosArrayGetSize(pResList);
    for (int32_t j = 0; j < numOfResBlock; ++j) {
H
Haojun Liao 已提交
144 145
      SSDataBlock *pRes = taosArrayGetP(pResList, j);
      ASSERT(pRes->info.rows > 0);
H
Haojun Liao 已提交
146

H
Haojun Liao 已提交
147 148 149 150
      SInputData inputData = {.pData = pRes};
      code = dsPutDataBlock(sinkHandle, &inputData, &qcontinue);
      if (code) {
        QW_TASK_ELOG("dsPutDataBlock failed, code:%x - %s", code, tstrerror(code));
151
        QW_ERR_JRET(code);
H
Haojun Liao 已提交
152
      }
153

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

157 158 159
    if (numOfResBlock == 0 || (hasMore == false)) {
      if (numOfResBlock == 0) {
        QW_TASK_DLOG("qExecTask end with empty res, useconds:%" PRIu64, useconds);
H
Haojun Liao 已提交
160 161
      } else {
        QW_TASK_DLOG("qExecTask done, useconds:%" PRIu64, useconds);
162 163 164 165 166 167 168 169 170 171 172 173
      }

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

      if (queryStop) {
        *queryStop = true;
      }

      break;
    }

D
dapan1121 已提交
174
    if (!qcontinue) {
175 176 177
      if (queryStop) {
        *queryStop = true;
      }
178

D
dapan1121 已提交
179 180 181
      break;
    }

D
dapan1121 已提交
182
    if (ctx->needFetch && (!ctx->queryRsped) && execNum >= QW_DEFAULT_SHORT_RUN_TIMES) {
D
dapan1121 已提交
183 184 185
      break;
    }

D
dapan1121 已提交
186
    if (QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
D
dapan1121 已提交
187 188
      break;
    }
D
dapan1121 已提交
189

D
dapan1121 已提交
190 191 192
    if (atomic_load_32(&ctx->rspCode)) {
      break;
    }
D
dapan1121 已提交
193 194
  }

195
_return:
H
Haojun Liao 已提交
196
  taosArrayDestroyEx(pResList, freeBlock);
D
dapan1121 已提交
197
  QW_RET(code);
D
dapan1121 已提交
198
}
D
dapan1121 已提交
199

D
dapan1121 已提交
200
int32_t qwGenerateSchHbRsp(SQWorker *mgmt, SQWSchStatus *sch, SQWHbInfo *hbInfo) {
D
dapan1121 已提交
201 202
  int32_t taskNum = 0;

D
dapan1121 已提交
203
  hbInfo->connInfo = sch->hbConnInfo;
D
dapan1121 已提交
204
  hbInfo->rsp.epId = sch->hbEpId;
D
dapan1121 已提交
205

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

D
dapan1121 已提交
208
  taskNum = taosHashGetSize(sch->tasksHash);
D
dapan1121 已提交
209 210 211

  hbInfo->rsp.taskStatus = taosArrayInit(taskNum, sizeof(STaskStatus));
  if (NULL == hbInfo->rsp.taskStatus) {
D
dapan1121 已提交
212
    QW_UNLOCK(QW_READ, &sch->tasksLock);
D
dapan1121 已提交
213
    QW_ELOG("taosArrayInit taskStatus failed, num:%d", taskNum);
D
dapan1121 已提交
214 215 216
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }

217
  void       *key = NULL;
dengyihao's avatar
dengyihao 已提交
218 219
  size_t      keyLen = 0;
  int32_t     i = 0;
D
dapan1121 已提交
220
  STaskStatus status = {0};
D
dapan1121 已提交
221 222 223 224

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

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

D
dapan1121 已提交
229
    QW_GET_QTID(key, status.queryId, status.taskId, status.execId);
D
dapan1121 已提交
230 231
    status.status = taskStatus->status;
    status.refId = taskStatus->refId;
dengyihao's avatar
dengyihao 已提交
232

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

D
dapan1121 已提交
235 236
    ++i;
    pIter = taosHashIterate(sch->tasksHash, pIter);
dengyihao's avatar
dengyihao 已提交
237
  }
D
dapan1121 已提交
238 239 240 241 242 243

  QW_UNLOCK(QW_READ, &sch->tasksLock);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
244
int32_t qwGetQueryResFromSink(QW_FPARAMS_DEF, SQWTaskCtx *ctx, int32_t *dataLen, void **rspMsg, SOutputData *pOutput) {
D
dapan1121 已提交
245
  int64_t            len = 0;
D
dapan1121 已提交
246
  SRetrieveTableRsp *rsp = NULL;
dengyihao's avatar
dengyihao 已提交
247 248
  bool               queryEnd = false;
  int32_t            code = 0;
249
  SOutputData        output = {0};
D
dapan1121 已提交
250

251 252 253 254
  if (NULL == ctx->sinkHandle) {
    return TSDB_CODE_SUCCESS;
  }

255
  *dataLen = 0;
D
dapan1121 已提交
256

257 258
  while (true) {
    dsGetDataLength(ctx->sinkHandle, &len, &queryEnd);
D
dapan1121 已提交
259

260
    if (len < 0) {
D
dapan1121 已提交
261
      QW_TASK_ELOG("invalid length from dsGetDataLength, length:%" PRId64, len);
262 263
      QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
    }
dengyihao's avatar
dengyihao 已提交
264

265 266 267 268 269 270 271
    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 已提交
272

273 274
        QW_TASK_DLOG("no more data in sink and query end, fetched blocks %d rows %d", pOutput->numOfBlocks,
                     pOutput->numOfRows);
275

276 277
        qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_SUCC);
        if (NULL == rsp) {
D
dapan1121 已提交
278
          QW_ERR_RET(qwMallocFetchRsp(!ctx->localExec, len, &rsp));
279 280 281 282 283 284 285 286 287 288 289 290 291
          *pOutput = output;
        } else {
          pOutput->queryEnd = output.queryEnd;
          pOutput->bufStatus = output.bufStatus;
          pOutput->useconds = output.useconds;
        }

        break;
      }

      pOutput->bufStatus = DS_BUF_EMPTY;

      break;
D
dapan1121 已提交
292
    }
D
dapan1121 已提交
293

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

297
    *dataLen += len;
D
dapan1121 已提交
298

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

301 302 303 304 305 306
    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 已提交
307

308 309 310 311 312 313 314 315 316 317
    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) {
318 319
      QW_TASK_DLOG("task all data fetched and done, fetched blocks %d rows %d", pOutput->numOfBlocks,
                   pOutput->numOfRows);
320 321 322
      qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_SUCC);
      break;
    }
dengyihao's avatar
dengyihao 已提交
323

324 325 326 327
    if (0 == ctx->level) {
      QW_TASK_DLOG("task fetched blocks %d rows %d, level %d", pOutput->numOfBlocks, pOutput->numOfRows, ctx->level);
      break;
    }
D
dapan1121 已提交
328

329 330 331 332
    if (pOutput->numOfRows >= QW_MIN_RES_ROWS) {
      QW_TASK_DLOG("task fetched blocks %d rows %d reaches the min rows", pOutput->numOfBlocks, pOutput->numOfRows);
      break;
    }
D
dapan1121 已提交
333 334
  }

335 336
  *rspMsg = rsp;

D
dapan1121 已提交
337
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
338 339
}

340
int32_t qwGetDeleteResFromSink(QW_FPARAMS_DEF, SQWTaskCtx *ctx, SDeleteRes *pRes) {
341 342 343 344
  int64_t     len = 0;
  bool        queryEnd = false;
  int32_t     code = 0;
  SOutputData output = {0};
D
dapan1121 已提交
345 346 347

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

D
dapan1121 已提交
348
  if (len <= 0 || len != sizeof(SDeleterRes)) {
D
dapan1121 已提交
349
    QW_TASK_ELOG("invalid length from dsGetDataLength, length:%" PRId64, len);
D
dapan1121 已提交
350 351 352 353 354 355 356
    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);
  }
357

D
dapan1121 已提交
358 359 360 361 362 363 364
  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);
  }

365 366
  SDeleterRes *pDelRes = (SDeleterRes *)output.pData;

D
dapan1121 已提交
367
  pRes->suid = pDelRes->suid;
D
dapan1121 已提交
368 369 370
  pRes->uidList = pDelRes->uidList;
  pRes->skey = pDelRes->skey;
  pRes->ekey = pDelRes->ekey;
371
  pRes->affectedRows = pDelRes->affectedRows;
372 373
  strcpy(pRes->tableFName, pDelRes->tableName);
  strcpy(pRes->tsColName, pDelRes->tsColName);
D
dapan1121 已提交
374
  taosMemoryFree(output.pData);
375

D
dapan1121 已提交
376 377 378
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
379
int32_t qwHandlePrePhaseEvents(QW_FPARAMS_DEF, int8_t phase, SQWPhaseInput *input, SQWPhaseOutput *output) {
380 381
  int32_t     code = 0;
  SQWTaskCtx *ctx = NULL;
D
dapan1121 已提交
382

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

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

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

D
dapan1121 已提交
389
  QW_SET_PHASE(ctx, phase);
D
dapan1121 已提交
390

dengyihao's avatar
dengyihao 已提交
391
  if (atomic_load_8((int8_t *)&ctx->queryEnd)) {
D
dapan1121 已提交
392 393 394
    QW_TASK_ELOG_E("query already end");
    QW_ERR_JRET(TSDB_CODE_QW_MSG_ERROR);
  }
D
dapan1121 已提交
395

D
dapan1121 已提交
396 397
  switch (phase) {
    case QW_PHASE_PRE_QUERY: {
D
dapan1121 已提交
398
      if (QW_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
H
Haojun Liao 已提交
399
        QW_TASK_ELOG("task already dropped at wrong phase %s", qwPhaseStr(phase));
D
dapan1121 已提交
400
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_STATUS_ERROR);
D
dapan1121 已提交
401
      }
D
dapan1121 已提交
402

D
dapan1121 已提交
403
      if (QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
404
        QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
405

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

D
dapan1121 已提交
409
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
D
dapan1121 已提交
410
      }
D
dapan1121 已提交
411

D
dapan1121 已提交
412
      QW_ERR_JRET(qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_EXEC));
D
dapan1121 已提交
413 414
      break;
    }
D
dapan1121 已提交
415
    case QW_PHASE_PRE_FETCH: {
D
dapan1121 已提交
416
      if (QW_EVENT_PROCESSED(ctx, QW_EVENT_DROP) || QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
417
        QW_TASK_WLOG("task dropping or already dropped, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
418 419
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
      }
D
dapan1121 已提交
420

D
dapan1121 已提交
421
      if (QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
D
dapan1121 已提交
422
        QW_TASK_WLOG("last fetch still not processed, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
423 424 425
        QW_ERR_JRET(TSDB_CODE_QRY_DUPLICATTED_OPERATION);
      }

D
dapan1121 已提交
426 427 428 429 430
      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 已提交
431
      if (!ctx->queryRsped) {
D
dapan1121 已提交
432
        QW_TASK_ELOG("ready msg has not been processed, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
433
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_MSG_ERROR);
D
dapan1121 已提交
434 435
      }
      break;
dengyihao's avatar
dengyihao 已提交
436
    }
D
dapan1121 已提交
437
    case QW_PHASE_PRE_CQUERY: {
D
dapan1121 已提交
438
      if (QW_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
439
        QW_TASK_WLOG("task already dropped, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
440
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
D
dapan1121 已提交
441
      }
D
dapan1121 已提交
442

D
dapan1121 已提交
443 444 445 446 447
      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 已提交
448
      if (QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
449
        QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
H
Haojun Liao 已提交
450

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

D
dapan1121 已提交
454
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
D
dapan1121 已提交
455
      }
D
dapan1121 已提交
456

D
dapan1121 已提交
457
      break;
D
dapan1121 已提交
458 459 460 461 462 463 464
    }
    default:
      QW_TASK_ELOG("invalid phase %s", qwPhaseStr(phase));
      QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  }

  if (ctx->rspCode) {
465
    QW_TASK_ELOG("task already failed at phase %s, code:%s", qwPhaseStr(phase), tstrerror(ctx->rspCode));
D
dapan1121 已提交
466
    QW_ERR_JRET(ctx->rspCode);
D
dapan1121 已提交
467
  }
D
dapan1121 已提交
468

D
dapan1121 已提交
469
_return:
D
dapan1121 已提交
470

D
dapan1121 已提交
471
  if (ctx) {
D
dapan1121 已提交
472
    QW_UPDATE_RSP_CODE(ctx, code);
dengyihao's avatar
dengyihao 已提交
473

D
dapan1121 已提交
474
    QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
475 476
    qwReleaseTaskCtx(mgmt, ctx);
  }
D
dapan1121 已提交
477

478 479 480 481 482
  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 已提交
483 484 485 486 487

  QW_RET(code);
}

int32_t qwHandlePostPhaseEvents(QW_FPARAMS_DEF, int8_t phase, SQWPhaseInput *input, SQWPhaseOutput *output) {
488 489 490
  int32_t        code = 0;
  SQWTaskCtx    *ctx = NULL;
  SRpcHandleInfo connInfo = {0};
D
dapan1121 已提交
491

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

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

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

D
dapan1121 已提交
498
  if (QW_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
499
    QW_TASK_WLOG("task already dropped, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
500 501 502
    QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
  }

D
dapan1121 已提交
503
  if (QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
504 505 506 507
    if (QW_PHASE_POST_FETCH == phase) {
      QW_TASK_WLOG("drop received at wrong phase %s", qwPhaseStr(phase));
      QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
    }
D
dapan1121 已提交
508

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

D
dapan1121 已提交
512 513
    QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
    QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
D
dapan1121 已提交
514 515 516
  }

  if (ctx->rspCode) {
dengyihao's avatar
dengyihao 已提交
517 518
    QW_TASK_ELOG("task already failed, phase %s, error:%x - %s", qwPhaseStr(phase), ctx->rspCode,
                 tstrerror(ctx->rspCode));
D
dapan1121 已提交
519
    QW_ERR_JRET(ctx->rspCode);
dengyihao's avatar
dengyihao 已提交
520
  }
D
dapan1121 已提交
521

D
dapan1121 已提交
522
  QW_ERR_JRET(input->code);
D
dapan1121 已提交
523 524 525

_return:

D
dapan1121 已提交
526
  if (TSDB_CODE_SUCCESS == code && QW_PHASE_POST_QUERY == phase) {
D
dapan1121 已提交
527
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_PART_SUCC);
528
    ctx->queryGotData = true;
D
dapan1121 已提交
529 530
  }

531
  if (QW_PHASE_POST_QUERY == phase && ctx && !ctx->queryRsped) {
D
dapan1121 已提交
532 533 534 535 536 537
    bool   rsped = false;
    SQWMsg qwMsg = {.msgType = ctx->msgType, .connInfo = ctx->ctrlConnInfo};
    qwDbgSimulateRedirect(&qwMsg, ctx, &rsped);
    qwDbgSimulateDead(QW_FPARAMS(), ctx, &rsped);
    if (!rsped) {      
      qwSendQueryRsp(QW_FPARAMS(), input->msgType + 1, ctx, code, false);
538
    }
539 540
  }

D
dapan1121 已提交
541
  if (ctx) {
D
dapan1121 已提交
542
    QW_UPDATE_RSP_CODE(ctx, code);
D
dapan1121 已提交
543

D
dapan1121 已提交
544
    QW_SET_PHASE(ctx, phase);
dengyihao's avatar
dengyihao 已提交
545

D
dapan1121 已提交
546
    QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
547
    qwReleaseTaskCtx(mgmt, ctx);
D
dapan1121 已提交
548 549
  }

D
dapan1121 已提交
550
  if (code) {
D
dapan1121 已提交
551
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_FAIL);
D
dapan1121 已提交
552 553
  }

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

D
dapan1121 已提交
556 557 558
  QW_RET(code);
}

D
dapan1121 已提交
559 560 561 562 563 564
int32_t qwAbortPrerocessQuery(QW_FPARAMS_DEF) {
  QW_ERR_RET(qwDropTask(QW_FPARAMS()));

  QW_RET(TSDB_CODE_SUCCESS);
}

D
dapan1121 已提交
565
int32_t qwPreprocessQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
H
Hongze Cheng 已提交
566 567
  int32_t     code = 0;
  SQWTaskCtx *ctx = NULL;
D
dapan1121 已提交
568

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

D
dapan1121 已提交
571 572 573
  QW_ERR_JRET(qwAddTaskCtx(QW_FPARAMS()));

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

D
dapan1121 已提交
575
  ctx->ctrlConnInfo = qwMsg->connInfo;
D
dapan1121 已提交
576
  ctx->phase = -1;
D
dapan1121 已提交
577

D
dapan1121 已提交
578
  QW_ERR_JRET(qwAddTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_INIT));
D
dapan1121 已提交
579 580 581 582 583 584 585 586 587 588 589

_return:

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

  QW_RET(TSDB_CODE_SUCCESS);
}

590
int32_t qwProcessQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg, char *sql) {
D
dapan1121 已提交
591 592 593 594 595 596 597 598
  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 已提交
599
  QW_ERR_JRET(qwHandlePrePhaseEvents(QW_FPARAMS(), QW_PHASE_PRE_QUERY, &input, NULL));
D
dapan1121 已提交
600 601

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

D
dapan1121 已提交
603 604 605
  ctx->taskType = qwMsg->msgInfo.taskType;
  ctx->explain = qwMsg->msgInfo.explain;
  ctx->needFetch = qwMsg->msgInfo.needFetch;
D
dapan1121 已提交
606
  ctx->msgType = qwMsg->msgType;
D
dapan1121 已提交
607
  ctx->localExec = false;
X
Xiaoyu Wang 已提交
608

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

611
  code = qMsgToSubplan(qwMsg->msg, qwMsg->msgLen, &plan);
D
dapan1121 已提交
612
  if (TSDB_CODE_SUCCESS != code) {
613 614
    code = TSDB_CODE_INVALID_MSG;
    QW_TASK_ELOG("task physical plan to subplan failed, code:%x - %s", code, tstrerror(code));
D
dapan1121 已提交
615
    QW_ERR_JRET(code);
D
dapan1121 已提交
616
  }
dengyihao's avatar
dengyihao 已提交
617

618
  code = qCreateExecTask(qwMsg->node, mgmt->nodeId, tId, plan, &pTaskInfo, &sinkHandle, sql, OPTR_EXEC_MODEL_BATCH);
D
dapan1121 已提交
619
  sql = NULL;
D
dapan1121 已提交
620
  if (code) {
D
dapan1121 已提交
621
    QW_TASK_ELOG("qCreateExecTask failed, code:%x - %s", code, tstrerror(code));
D
dapan1121 已提交
622
    QW_ERR_JRET(code);
D
dapan1121 已提交
623
  }
D
dapan1121 已提交
624

H
Haojun Liao 已提交
625
  if (NULL == sinkHandle || NULL == pTaskInfo) {
D
dapan1121 已提交
626 627 628 629
    QW_TASK_ELOG("create task result error, taskHandle:%p, sinkHandle:%p", pTaskInfo, sinkHandle);
    QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  }

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

632
  ctx->level = plan->level;
D
dapan1121 已提交
633 634 635
  atomic_store_ptr(&ctx->taskHandle, pTaskInfo);
  atomic_store_ptr(&ctx->sinkHandle, sinkHandle);

D
dapan1121 已提交
636 637
  qwSaveTbVersionInfo(pTaskInfo, ctx);
  QW_ERR_JRET(qwExecTask(QW_FPARAMS(), ctx, NULL));
dengyihao's avatar
dengyihao 已提交
638

D
dapan1121 已提交
639 640
_return:

D
dapan1121 已提交
641
  taosMemoryFree(sql);
642

D
dapan1121 已提交
643
  input.code = code;
D
dapan1121 已提交
644
  input.msgType = qwMsg->msgType;
D
dapan1121 已提交
645
  code = qwHandlePostPhaseEvents(QW_FPARAMS(), QW_PHASE_POST_QUERY, &input, NULL);
646

H
Haojun Liao 已提交
647
  if (ctx != NULL && QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
648 649 650 651
    void         *rsp = NULL;
    int32_t       dataLen = 0;
    SOutputData sOutput = {0};
    QW_ERR_JRET(qwGetQueryResFromSink(QW_FPARAMS(), ctx, &dataLen, &rsp, &sOutput));
H
Haojun Liao 已提交
652

653 654
    if (rsp) {
      bool qComplete = (DS_BUF_EMPTY == sOutput.bufStatus && sOutput.queryEnd);
H
Haojun Liao 已提交
655

656 657 658 659
      qwBuildFetchRsp(rsp, &sOutput, dataLen, qComplete);
      if (qComplete) {
        atomic_store_8((int8_t *)&ctx->queryEnd, true);
      }
H
Haojun Liao 已提交
660

661 662
      qwMsg->connInfo = ctx->dataConnInfo;
      QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_FETCH);
H
Haojun Liao 已提交
663

664 665
      qwBuildAndSendFetchRsp(ctx->fetchType, &qwMsg->connInfo, rsp, dataLen, code);
      rsp = NULL;
H
Haojun Liao 已提交
666

667 668 669 670 671
      QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code,
                   tstrerror(code), dataLen);
    }
  }

D
dapan1121 已提交
672
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
673 674
}

D
dapan1121 已提交
675
int32_t qwProcessCQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
676
  SQWTaskCtx   *ctx = NULL;
dengyihao's avatar
dengyihao 已提交
677
  int32_t       code = 0;
678
  SQWPhaseInput input = {0};
679
  void         *rsp = NULL;
dengyihao's avatar
dengyihao 已提交
680
  int32_t       dataLen = 0;
681
  bool          queryStop = false;
dengyihao's avatar
dengyihao 已提交
682

D
dapan1121 已提交
683
  do {
D
dapan1121 已提交
684
    QW_ERR_JRET(qwHandlePrePhaseEvents(QW_FPARAMS(), QW_PHASE_PRE_CQUERY, &input, NULL));
D
dapan1121 已提交
685

D
dapan1121 已提交
686
    QW_ERR_JRET(qwGetTaskCtx(QW_FPARAMS(), &ctx));
D
dapan1121 已提交
687

dengyihao's avatar
dengyihao 已提交
688 689
    atomic_store_8((int8_t *)&ctx->queryInQueue, 0);
    atomic_store_8((int8_t *)&ctx->queryContinue, 0);
D
dapan1121 已提交
690

691
    QW_ERR_JRET(qwExecTask(QW_FPARAMS(), ctx, &queryStop));
D
dapan1121 已提交
692

D
dapan1121 已提交
693
    if (QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
D
dapan1121 已提交
694
      SOutputData sOutput = {0};
D
dapan1121 已提交
695
      QW_ERR_JRET(qwGetQueryResFromSink(QW_FPARAMS(), ctx, &dataLen, &rsp, &sOutput));
dengyihao's avatar
dengyihao 已提交
696 697

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

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

D
dapan1121 已提交
703
      if (rsp) {
D
dapan1121 已提交
704
        bool qComplete = (DS_BUF_EMPTY == sOutput.bufStatus && sOutput.queryEnd);
dengyihao's avatar
dengyihao 已提交
705

D
dapan1121 已提交
706
        qwBuildFetchRsp(rsp, &sOutput, dataLen, qComplete);
D
dapan1121 已提交
707
        if (qComplete) {
dengyihao's avatar
dengyihao 已提交
708
          atomic_store_8((int8_t *)&ctx->queryEnd, true);
D
dapan1121 已提交
709
        }
H
Haojun Liao 已提交
710

D
dapan1121 已提交
711
        qwMsg->connInfo = ctx->dataConnInfo;
dengyihao's avatar
dengyihao 已提交
712 713
        QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_FETCH);

D
dapan1121 已提交
714
        qwBuildAndSendFetchRsp(ctx->fetchType, &qwMsg->connInfo, rsp, dataLen, code);
D
dapan1121 已提交
715
        rsp = NULL;
716

dengyihao's avatar
dengyihao 已提交
717 718
        QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code,
                     tstrerror(code), dataLen);
D
dapan1121 已提交
719
      } else {
dengyihao's avatar
dengyihao 已提交
720
        atomic_store_8((int8_t *)&ctx->queryContinue, 1);
721 722 723
      }
    }

dengyihao's avatar
dengyihao 已提交
724
  _return:
725

D
dapan1121 已提交
726 727 728 729
    if (NULL == ctx) {
      break;
    }

D
dapan1121 已提交
730
    if (code && QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
dengyihao's avatar
dengyihao 已提交
731
      QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_FETCH);
732 733
      qwFreeFetchRsp(rsp);
      rsp = NULL;
dengyihao's avatar
dengyihao 已提交
734

D
dapan1121 已提交
735
      qwMsg->connInfo = ctx->dataConnInfo;
D
dapan1121 已提交
736
      qwBuildAndSendFetchRsp(ctx->fetchType, &qwMsg->connInfo, NULL, 0, code);
dengyihao's avatar
dengyihao 已提交
737 738
      QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code, tstrerror(code),
                   0);
739
    }
D
dapan1121 已提交
740

D
dapan1121 已提交
741
    QW_LOCK(QW_WRITE, &ctx->lock);
742
    if (queryStop || code || 0 == atomic_load_8((int8_t *)&ctx->queryContinue)) {
D
dapan1121 已提交
743 744
      // Note: query is not running anymore
      QW_SET_PHASE(ctx, 0);
dengyihao's avatar
dengyihao 已提交
745
      QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
746 747
      break;
    }
dengyihao's avatar
dengyihao 已提交
748
    QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
749
  } while (true);
D
dapan1121 已提交
750

D
dapan1121 已提交
751
  input.code = code;
dengyihao's avatar
dengyihao 已提交
752
  qwHandlePostPhaseEvents(QW_FPARAMS(), QW_PHASE_POST_CQUERY, &input, NULL);
D
dapan1121 已提交
753

dengyihao's avatar
dengyihao 已提交
754
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
755
}
D
dapan1121 已提交
756

D
dapan1121 已提交
757
int32_t qwProcessFetch(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
dengyihao's avatar
dengyihao 已提交
758 759 760
  int32_t       code = 0;
  int32_t       dataLen = 0;
  bool          locked = false;
761 762
  SQWTaskCtx   *ctx = NULL;
  void         *rsp = NULL;
D
dapan1121 已提交
763
  SQWPhaseInput input = {0};
D
dapan1121 已提交
764

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

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

D
dapan1121 已提交
769
  ctx->msgType = qwMsg->msgType;
D
dapan1121 已提交
770
  ctx->dataConnInfo = qwMsg->connInfo;
D
dapan1121 已提交
771

D
dapan 已提交
772
  SOutputData sOutput = {0};
D
dapan1121 已提交
773
  QW_ERR_JRET(qwGetQueryResFromSink(QW_FPARAMS(), ctx, &dataLen, &rsp, &sOutput));
D
dapan1121 已提交
774

775 776
  if (NULL == rsp) {
    QW_SET_EVENT_RECEIVED(ctx, QW_EVENT_FETCH);
D
dapan1121 已提交
777
  } else {
D
dapan1121 已提交
778
    bool qComplete = (DS_BUF_EMPTY == sOutput.bufStatus && sOutput.queryEnd);
dengyihao's avatar
dengyihao 已提交
779

D
dapan1121 已提交
780
    qwBuildFetchRsp(rsp, &sOutput, dataLen, qComplete);
D
dapan1121 已提交
781
    if (qComplete) {
dengyihao's avatar
dengyihao 已提交
782
      atomic_store_8((int8_t *)&ctx->queryEnd, true);
D
dapan1121 已提交
783
    }
D
dapan1121 已提交
784 785
  }

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

D
dapan1121 已提交
789 790
    QW_LOCK(QW_WRITE, &ctx->lock);
    locked = true;
791

D
dapan1121 已提交
792
    // RC WARNING
793 794 795
    if (-1 == ctx->phase || false == ctx->queryGotData) {
      QW_TASK_DLOG_E("task query unfinished");
    } else if (QW_QUERY_RUNNING(ctx)) {
dengyihao's avatar
dengyihao 已提交
796 797
      atomic_store_8((int8_t *)&ctx->queryContinue, 1);
    } else if (0 == atomic_load_8((int8_t *)&ctx->queryInQueue)) {
D
dapan1121 已提交
798
      qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_EXEC);
D
dapan1121 已提交
799

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

D
dapan1121 已提交
802
      QW_ERR_JRET(qwBuildAndSendCQueryMsg(QW_FPARAMS(), &qwMsg->connInfo));
803
    }
D
dapan 已提交
804
  }
dengyihao's avatar
dengyihao 已提交
805

D
dapan1121 已提交
806
_return:
D
dapan1121 已提交
807

D
dapan1121 已提交
808 809 810 811 812
  if (locked) {
    QW_UNLOCK(QW_WRITE, &ctx->lock);
  }

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

D
dapan 已提交
815 816 817
  if (code) {
    qwFreeFetchRsp(rsp);
    rsp = NULL;
D
dapan1121 已提交
818
    dataLen = 0;
D
dapan1121 已提交
819 820 821
  }

  if (code || rsp) {
D
dapan1121 已提交
822
    bool rsped = false;
D
dapan1121 已提交
823
    if (ctx) {
824
      qwDbgSimulateRedirect(qwMsg, ctx, &rsped);
D
dapan1121 已提交
825 826
      qwDbgSimulateDead(QW_FPARAMS(), ctx, &rsped);
    }
D
dapan1121 已提交
827 828
    if (!rsped) {
      qwBuildAndSendFetchRsp(qwMsg->msgType + 1, &qwMsg->connInfo, rsp, dataLen, code);
829 830
      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 已提交
831
    }
D
dapan1121 已提交
832 833
  }

D
dapan1121 已提交
834
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
835
}
D
dapan1121 已提交
836

D
dapan1121 已提交
837
int32_t qwProcessDrop(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
dengyihao's avatar
dengyihao 已提交
838
  int32_t     code = 0;
D
dapan1121 已提交
839
  bool        dropped = false;
D
dapan1121 已提交
840
  SQWTaskCtx *ctx = NULL;
dengyihao's avatar
dengyihao 已提交
841
  bool        locked = false;
D
dapan1121 已提交
842

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

D
dapan1121 已提交
845 846 847 848
  QW_LOCK(QW_WRITE, &ctx->lock);

  locked = true;

D
dapan1121 已提交
849
  if (QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
850
    QW_TASK_WLOG_E("task already dropping");
D
dapan1121 已提交
851 852 853
    QW_ERR_JRET(TSDB_CODE_QRY_DUPLICATTED_OPERATION);
  }

D
dapan1121 已提交
854
  if (QW_QUERY_RUNNING(ctx)) {
D
dapan1121 已提交
855
    QW_ERR_JRET(qwKillTaskHandle(ctx));
D
dapan1121 已提交
856
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_DROP);
D
dapan1121 已提交
857
  } else {
D
dapan1121 已提交
858
    QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
D
dapan1121 已提交
859
    dropped = true;
D
dapan1121 已提交
860
  }
D
dapan1121 已提交
861

D
dapan1121 已提交
862
  if (!dropped) {
D
dapan1121 已提交
863 864
    QW_SET_EVENT_RECEIVED(ctx, QW_EVENT_DROP);
  }
865

D
dapan1121 已提交
866
_return:
D
dapan1121 已提交
867

D
dapan1121 已提交
868
  if (code) {
D
dapan1121 已提交
869 870 871
    if (ctx) {
      QW_UPDATE_RSP_CODE(ctx, code);
    }
H
Haojun Liao 已提交
872

D
dapan1121 已提交
873
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_FAIL);
D
dapan1121 已提交
874 875
  }

D
dapan 已提交
876 877 878 879
  if (locked) {
    QW_UNLOCK(QW_WRITE, &ctx->lock);
  }

D
dapan1121 已提交
880
  if (ctx) {
D
dapan1121 已提交
881
    qwReleaseTaskCtx(mgmt, ctx);
D
dapan1121 已提交
882 883
  }

D
dapan1121 已提交
884 885 886
  QW_RET(TSDB_CODE_SUCCESS);
}

D
dapan1121 已提交
887
int32_t qwProcessHb(SQWorker *mgmt, SQWMsg *qwMsg, SSchedulerHbReq *req) {
dengyihao's avatar
dengyihao 已提交
888
  int32_t         code = 0;
D
dapan1121 已提交
889
  SSchedulerHbRsp rsp = {0};
890
  SQWSchStatus   *sch = NULL;
D
dapan1121 已提交
891

D
dapan1121 已提交
892 893 894
  if (qwMsg->code) {
    QW_RET(qwProcessHbLinkBroken(mgmt, qwMsg, req));
  }
D
dapan1121 已提交
895 896

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

D
dapan1121 已提交
899 900
  sch->hbBrokenTs = 0;

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

D
dapan1121 已提交
903
  if (sch->hbConnInfo.handle) {
904
    tmsgReleaseHandle(&sch->hbConnInfo, TAOS_CONN_SERVER);
S
Shengliang Guan 已提交
905
    sch->hbConnInfo.handle = NULL;
D
dapan1121 已提交
906
  }
D
dapan1121 已提交
907

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

D
dapan1121 已提交
911
  QW_UNLOCK(QW_WRITE, &sch->hbConnLock);
dengyihao's avatar
dengyihao 已提交
912 913 914

  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 已提交
915

D
dapan1121 已提交
916 917 918 919
  qwReleaseScheduler(QW_READ, mgmt);

_return:

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

S
shm  
Shengliang Guan 已提交
922
  qwBuildAndSendHbRsp(&qwMsg->connInfo, &rsp, code);
D
dapan1121 已提交
923 924

  if (code) {
925
    tmsgReleaseHandle(&qwMsg->connInfo, TAOS_CONN_SERVER);
S
Shengliang Guan 已提交
926
    qwMsg->connInfo.handle = NULL;
D
dapan1121 已提交
927
  }
dengyihao's avatar
dengyihao 已提交
928

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

D
dapan1121 已提交
931
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
932 933 934
}

void qwProcessHbTimerEvent(void *param, void *tmrId) {
935
  SQWHbParam *hbParam = (SQWHbParam *)param;
D
dapan1121 已提交
936 937 938 939
  if (hbParam->qwrId != atomic_load_32(&gQwMgmt.qwRef)) {
    return;
  }

940
  int64_t   refId = hbParam->refId;
D
dapan1121 已提交
941 942 943 944 945
  SQWorker *mgmt = qwAcquire(refId);
  if (NULL == mgmt) {
    QW_DLOG("qwAcquire %" PRIx64 "failed", refId);
    return;
  }
946

D
dapan1121 已提交
947
  SQWSchStatus *sch = NULL;
dengyihao's avatar
dengyihao 已提交
948
  int32_t       taskNum = 0;
949
  SQWHbInfo    *rspList = NULL;
D
dapan1121 已提交
950
  SArray       *pExpiredSch = NULL;
dengyihao's avatar
dengyihao 已提交
951
  int32_t       code = 0;
D
dapan1121 已提交
952

D
dapan1121 已提交
953 954
  qwDbgDumpMgmtInfo(mgmt);

D
dapan1121 已提交
955 956 957 958 959
  QW_LOCK(QW_READ, &mgmt->schLock);

  int32_t schNum = taosHashGetSize(mgmt->schHash);
  if (schNum <= 0) {
    QW_UNLOCK(QW_READ, &mgmt->schLock);
D
dapan1121 已提交
960
    taosTmrReset(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, param, mgmt->timer, &mgmt->hbTimer);
D
dapan1121 已提交
961
    qwRelease(refId);
D
dapan1121 已提交
962
    return;
D
dapan1121 已提交
963 964
  }

wafwerar's avatar
wafwerar 已提交
965
  rspList = taosMemoryCalloc(schNum, sizeof(SQWHbInfo));
D
dapan1121 已提交
966 967
  pExpiredSch = taosArrayInit(schNum, sizeof(uint64_t));
  if (NULL == rspList || NULL == pExpiredSch) {
D
dapan1121 已提交
968
    QW_UNLOCK(QW_READ, &mgmt->schLock);
D
dapan1121 已提交
969 970
    taosMemoryFree(rspList);
    taosArrayDestroy(pExpiredSch);
D
dapan1121 已提交
971 972
    QW_ELOG("calloc %d SQWHbInfo failed", schNum);
    taosTmrReset(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, param, mgmt->timer, &mgmt->hbTimer);
D
dapan1121 已提交
973
    qwRelease(refId);
D
dapan1121 已提交
974
    return;
D
dapan1121 已提交
975 976
  }

977
  void   *key = NULL;
dengyihao's avatar
dengyihao 已提交
978
  size_t  keyLen = 0;
D
dapan1121 已提交
979
  int32_t i = 0;
D
dapan1121 已提交
980
  int64_t currentMs = taosGetTimestampMs();
D
dapan1121 已提交
981 982 983

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

H
Haojun Liao 已提交
989 990
      if (sch1->hbBrokenTs > 0 && ((currentMs - sch1->hbBrokenTs) > QW_SCH_TIMEOUT_MSEC) &&
          taosHashGetSize(sch1->tasksHash) <= 0) {
D
dapan1121 已提交
991 992
        taosArrayPush(pExpiredSch, sId);
      }
993

D
dapan1121 已提交
994 995 996
      pIter = taosHashIterate(mgmt->schHash, pIter);
      continue;
    }
dengyihao's avatar
dengyihao 已提交
997

D
dapan1121 已提交
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
    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 已提交
1013
    qwBuildAndSendHbRsp(&rspList[j].connInfo, &rspList[j].rsp, code);
1014 1015
    /*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 已提交
1016
    tFreeSSchedulerHbRsp(&rspList[j].rsp);
D
dapan1121 已提交
1017 1018
  }

D
dapan1121 已提交
1019
  if (taosArrayGetSize(pExpiredSch) > 0) {
D
dapan1121 已提交
1020
    qwClearExpiredSch(mgmt, pExpiredSch);
D
dapan1121 已提交
1021 1022
  }

wafwerar's avatar
wafwerar 已提交
1023
  taosMemoryFreeClear(rspList);
D
dapan1121 已提交
1024
  taosArrayDestroy(pExpiredSch);
D
dapan1121 已提交
1025

dengyihao's avatar
dengyihao 已提交
1026
  taosTmrReset(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, param, mgmt->timer, &mgmt->hbTimer);
1027
  qwRelease(refId);
D
dapan1121 已提交
1028 1029
}

1030
int32_t qwProcessDelete(QW_FPARAMS_DEF, SQWMsg *qwMsg, SDeleteRes *pRes) {
D
dapan1121 已提交
1031 1032 1033 1034 1035 1036
  int32_t        code = 0;
  SSubplan      *plan = NULL;
  qTaskInfo_t    pTaskInfo = NULL;
  DataSinkHandle sinkHandle = NULL;
  SQWTaskCtx     ctx = {0};

1037
  code = qMsgToSubplan(qwMsg->msg, qwMsg->msgLen, &plan);
D
dapan1121 已提交
1038 1039 1040 1041 1042 1043
  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);
  }

1044
  code = qCreateExecTask(qwMsg->node, mgmt->nodeId, tId, plan, &pTaskInfo, &sinkHandle, NULL, OPTR_EXEC_MODEL_BATCH);
D
dapan1121 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
  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);
    QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  }

D
dapan1121 已提交
1055 1056
  ctx.taskHandle = pTaskInfo;
  ctx.sinkHandle = sinkHandle;
D
dapan1121 已提交
1057

D
dapan1121 已提交
1058
  QW_ERR_JRET(qwExecTask(QW_FPARAMS(), &ctx, NULL));
D
dapan1121 已提交
1059

1060
  QW_ERR_JRET(qwGetDeleteResFromSink(QW_FPARAMS(), &ctx, pRes));
D
dapan1121 已提交
1061 1062 1063

_return:

D
dapan1121 已提交
1064
  qwFreeTaskCtx(&ctx);
D
dapan1121 已提交
1065 1066 1067 1068

  QW_RET(TSDB_CODE_SUCCESS);
}

D
dapan1121 已提交
1069 1070
int32_t qWorkerInit(int8_t nodeType, int32_t nodeId, void **qWorkerMgmt, const SMsgCb *pMsgCb) {
  if (NULL == qWorkerMgmt || (pMsgCb && pMsgCb->mgmt == NULL)) {
D
dapan1121 已提交
1071 1072 1073
    qError("invalid param to init qworker");
    QW_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }
S
Shengliang 已提交
1074

D
dapan1121 已提交
1075 1076 1077 1078
  int32_t qwNum = atomic_add_fetch_32(&gQwMgmt.qwNum, 1);
  if (1 == qwNum) {
    memset(gQwMgmt.param, 0, sizeof(gQwMgmt.param));
  }
D
dapan1121 已提交
1079 1080 1081 1082 1083 1084 1085 1086

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

  SQWorker *mgmt = taosMemoryCalloc(1, sizeof(SQWorker));
D
dapan1121 已提交
1087
  if (NULL == mgmt) {
D
dapan1121 已提交
1088 1089
    qError("calloc %d failed", (int32_t)sizeof(SQWorker));
    atomic_sub_fetch_32(&gQwMgmt.qwNum, 1);
D
dapan1121 已提交
1090
    QW_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
1091 1092
  }

D
dapan1121 已提交
1093 1094 1095
  mgmt->cfg.maxSchedulerNum = QW_DEFAULT_SCHEDULER_NUMBER;
  mgmt->cfg.maxTaskNum = QW_DEFAULT_TASK_NUMBER;
  mgmt->cfg.maxSchTaskNum = QW_DEFAULT_SCH_TASK_NUMBER;
D
dapan1121 已提交
1096

dengyihao's avatar
dengyihao 已提交
1097 1098
  mgmt->schHash = taosHashInit(mgmt->cfg.maxSchedulerNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false,
                               HASH_ENTRY_LOCK);
D
dapan1121 已提交
1099
  if (NULL == mgmt->schHash) {
wafwerar's avatar
wafwerar 已提交
1100
    taosMemoryFreeClear(mgmt);
D
dapan1121 已提交
1101
    qError("init %d scheduler hash failed", mgmt->cfg.maxSchedulerNum);
D
dapan1121 已提交
1102
    QW_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
1103 1104
  }

dengyihao's avatar
dengyihao 已提交
1105 1106
  mgmt->ctxHash =
      taosHashInit(mgmt->cfg.maxTaskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
D
dapan1121 已提交
1107
  if (NULL == mgmt->ctxHash) {
D
dapan1121 已提交
1108
    qError("init %d task ctx hash failed", mgmt->cfg.maxTaskNum);
D
dapan1121 已提交
1109 1110 1111 1112 1113 1114 1115 1116 1117
    QW_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  mgmt->timer = taosTmrInit(0, 0, 0, "qworker");
  if (NULL == mgmt->timer) {
    qError("init timer failed, error:%s", tstrerror(terrno));
    QW_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
1118 1119
  mgmt->nodeType = nodeType;
  mgmt->nodeId = nodeId;
D
dapan1121 已提交
1120 1121 1122 1123 1124
  if (pMsgCb) {
    mgmt->msgCb = *pMsgCb;
  } else {
    memset(&mgmt->msgCb, 0, sizeof(mgmt->msgCb));
  }
D
dapan1121 已提交
1125

D
dapan1121 已提交
1126 1127 1128 1129 1130 1131
  mgmt->refId = taosAddRef(gQwMgmt.qwRef, mgmt);
  if (mgmt->refId < 0) {
    qError("taosAddRef qw failed, error:%s", tstrerror(terrno));
    QW_ERR_JRET(terrno);
  }

D
dapan1121 已提交
1132 1133 1134
  SQWHbParam *param = NULL;
  qwSetHbParam(mgmt->refId, &param);

1135
  mgmt->hbTimer = taosTmrStart(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, (void *)param, mgmt->timer);
D
dapan1121 已提交
1136 1137 1138 1139
  if (NULL == mgmt->hbTimer) {
    qError("start hb timer failed");
    QW_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
1140

D
dapan1121 已提交
1141 1142
  *qWorkerMgmt = mgmt;

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

D
dapan1121 已提交
1145
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1146 1147 1148

_return:

D
dapan1121 已提交
1149 1150 1151 1152 1153 1154 1155
  if (mgmt->refId >= 0) {
    qwRelease(mgmt->refId);
  } else {
    taosHashCleanup(mgmt->schHash);
    taosHashCleanup(mgmt->ctxHash);
    taosTmrCleanUp(mgmt->timer);
    taosMemoryFreeClear(mgmt);
D
dapan1121 已提交
1156

1157
    atomic_sub_fetch_32(&gQwMgmt.qwNum, 1);
D
dapan1121 已提交
1158
  }
1159

D
dapan1121 已提交
1160
  QW_RET(code);
D
dapan1121 已提交
1161
}
D
dapan1121 已提交
1162 1163 1164 1165

void qWorkerDestroy(void **qWorkerMgmt) {
  if (NULL == qWorkerMgmt || NULL == *qWorkerMgmt) {
    return;
D
dapan1121 已提交
1166
  }
D
dapan 已提交
1167

H
Hongze Cheng 已提交
1168
  int32_t   destroyed = 0;
D
dapan1121 已提交
1169
  SQWorker *mgmt = *qWorkerMgmt;
D
dapan1121 已提交
1170
  mgmt->destroyed = &destroyed;
D
dapan1121 已提交
1171

D
dapan1121 已提交
1172 1173
  if (taosRemoveRef(gQwMgmt.qwRef, mgmt->refId)) {
    qError("remove qw from ref list failed, refId:%" PRIx64, mgmt->refId);
D
dapan1121 已提交
1174 1175 1176 1177 1178
    return;
  }

  while (0 == destroyed) {
    taosMsleep(2);
D
dapan1121 已提交
1179
  }
D
dapan1121 已提交
1180
}
D
dapan1121 已提交
1181

D
dapan1121 已提交
1182 1183 1184 1185 1186
int32_t qWorkerGetStat(SReadHandle *handle, void *qWorkerMgmt, SQWorkerStat *pStat) {
  if (NULL == handle || NULL == qWorkerMgmt || NULL == pStat) {
    QW_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

1187
  SQWorker     *mgmt = (SQWorker *)qWorkerMgmt;
D
dapan1121 已提交
1188
  SDataSinkStat sinkStat = {0};
1189

D
dapan1121 已提交
1190 1191
  dsDataSinkGetCacheSize(&sinkStat);
  pStat->cacheDataSize = sinkStat.cachedSize;
1192

D
dapan1121 已提交
1193 1194 1195 1196 1197
  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 已提交
1198
  pStat->deleteProcessed = QW_STAT_GET(mgmt->stat.msgStat.deleteProcessed);
D
dapan1121 已提交
1199 1200 1201 1202 1203 1204 1205

  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 已提交
1206
}
D
dapan1121 已提交
1207

H
Hongze Cheng 已提交
1208 1209 1210
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 已提交
1211 1212 1213 1214 1215 1216
  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 已提交
1217
  SReadHandle    rHandle = {0};
D
dapan1121 已提交
1218 1219

  QW_ERR_JRET(qwAddTaskCtx(QW_FPARAMS()));
D
dapan1121 已提交
1220
  QW_ERR_JRET(qwAddTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_INIT));
D
dapan1121 已提交
1221

D
dapan1121 已提交
1222
  QW_ERR_JRET(qwHandlePrePhaseEvents(QW_FPARAMS(), QW_PHASE_PRE_QUERY, &input, NULL));
D
dapan1121 已提交
1223 1224 1225 1226 1227 1228
  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 已提交
1229 1230
  ctx->localExec = true;
  ctx->explainRes = explainRes;
D
dapan1121 已提交
1231

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

D
dapan1121 已提交
1235
  code = qCreateExecTask(&rHandle, mgmt->nodeId, tId, plan, &pTaskInfo, &sinkHandle, NULL, OPTR_EXEC_MODEL_BATCH);
D
dapan1121 已提交
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
  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);
    QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  }

  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 已提交
1254
  taosMemoryFree(rHandle.pMsgCb);
H
Haojun Liao 已提交
1255

D
dapan1121 已提交
1256 1257 1258 1259
  input.code = code;
  input.msgType = qwMsg->msgType;
  code = qwHandlePostPhaseEvents(QW_FPARAMS(), QW_PHASE_POST_QUERY, &input, NULL);

D
dapan1121 已提交
1260 1261 1262 1263 1264 1265 1266 1267
  if (ctx) {
    QW_UPDATE_RSP_CODE(ctx, code);
    qwReleaseTaskCtx(mgmt, ctx);
  }

  QW_RET(code);
}

H
Hongze Cheng 已提交
1268 1269 1270 1271 1272 1273 1274 1275
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 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284

  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 已提交
1285

D
dapan1121 已提交
1286
  SOutputData sOutput = {0};
H
Haojun Liao 已提交
1287

D
dapan1121 已提交
1288 1289 1290 1291 1292
  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 已提交
1293

D
dapan1121 已提交
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
      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);
}