qworker.c 39.0 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

D
dapan1121 已提交
21 22 23 24
int32_t qwProcessHbLinkBroken(SQWorker *mgmt, SQWMsg *qwMsg, SSchedulerHbReq *req) {
  int32_t         code = 0;
  SSchedulerHbRsp rsp = {0};
  SQWSchStatus   *sch = NULL;
D
dapan1121 已提交
25

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

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

D
dapan1121 已提交
30
  sch->hbBrokenTs = taosGetTimestampMs();
31

D
dapan1121 已提交
32 33 34 35
  if (qwMsg->connInfo.handle == sch->hbConnInfo.handle) {
    tmsgReleaseHandle(&sch->hbConnInfo, TAOS_CONN_SERVER);
    sch->hbConnInfo.handle = NULL;
    sch->hbConnInfo.ahandle = NULL;
D
dapan1121 已提交
36

D
dapan1121 已提交
37 38 39
    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 已提交
40
  }
D
dapan1121 已提交
41

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

D
dapan1121 已提交
44
  qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
45

D
dapan1121 已提交
46
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
47 48
}

49 50
static void freeItem(void *param) {
  SExplainExecInfo *pInfo = param;
H
Haojun Liao 已提交
51 52 53
  taosMemoryFree(pInfo->verboseInfo);
}

D
dapan1121 已提交
54
int32_t qwHandleTaskComplete(QW_FPARAMS_DEF, SQWTaskCtx *ctx) {
D
dapan1121 已提交
55
  qTaskInfo_t taskHandle = ctx->taskHandle;
D
dapan1121 已提交
56

D
dapan1121 已提交
57 58
  ctx->queryExecDone = true;

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

  return TSDB_CODE_SUCCESS;
}

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

D
dapan1121 已提交
116 117 118 119
  if (ctx->queryExecDone) {
    if (queryStop) {
      *queryStop = true;
    }
dengyihao's avatar
dengyihao 已提交
120

D
dapan1121 已提交
121 122 123
    return TSDB_CODE_SUCCESS;
  }

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

D
dapan1121 已提交
128
    // if *taskHandle is NULL, it's killed right now
129 130
    bool hasMore = false;

D
dapan1121 已提交
131
    if (taskHandle) {
D
dapan1121 已提交
132
      qwDbgSimulateSleep();
133 134

      code = qExecTaskOpt(taskHandle, pResList, &useconds, &hasMore, &localFetch);
D
dapan1121 已提交
135
      if (code) {
136 137 138 139 140
        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));
        }
141
        QW_ERR_JRET(code);
D
dapan1121 已提交
142
      }
D
dapan1121 已提交
143 144
    }

D
dapan1121 已提交
145 146
    ++execNum;

147 148
    size_t numOfResBlock = taosArrayGetSize(pResList);
    for (int32_t j = 0; j < numOfResBlock; ++j) {
H
Haojun Liao 已提交
149
      SSDataBlock *pRes = taosArrayGetP(pResList, j);
150
      ASSERT(pRes->info.rows > 0);
H
Haojun Liao 已提交
151

H
Haojun Liao 已提交
152 153 154 155
      SInputData inputData = {.pData = pRes};
      code = dsPutDataBlock(sinkHandle, &inputData, &qcontinue);
      if (code) {
        QW_TASK_ELOG("dsPutDataBlock failed, code:%x - %s", code, tstrerror(code));
156
        QW_ERR_JRET(code);
H
Haojun Liao 已提交
157
      }
158

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

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

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

      if (queryStop) {
        *queryStop = true;
      }

      break;
    }

D
dapan1121 已提交
179
    if (!qcontinue) {
180 181 182
      if (queryStop) {
        *queryStop = true;
      }
183

D
dapan1121 已提交
184 185 186
      break;
    }

D
dapan1121 已提交
187
    if (ctx->needFetch && (!ctx->queryRsped) && execNum >= QW_DEFAULT_SHORT_RUN_TIMES) {
D
dapan1121 已提交
188 189 190
      break;
    }

D
dapan1121 已提交
191
    if (QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
D
dapan1121 已提交
192 193
      break;
    }
D
dapan1121 已提交
194

D
dapan1121 已提交
195 196 197
    if (atomic_load_32(&ctx->rspCode)) {
      break;
    }
D
dapan1121 已提交
198 199
  }

200
_return:
H
Haojun Liao 已提交
201
  taosArrayDestroy(pResList);
D
dapan1121 已提交
202
  QW_RET(code);
D
dapan1121 已提交
203
}
D
dapan1121 已提交
204

D
dapan1121 已提交
205
int32_t qwGenerateSchHbRsp(SQWorker *mgmt, SQWSchStatus *sch, SQWHbInfo *hbInfo) {
D
dapan1121 已提交
206 207
  int32_t taskNum = 0;

D
dapan1121 已提交
208
  hbInfo->connInfo = sch->hbConnInfo;
D
dapan1121 已提交
209
  hbInfo->rsp.epId = sch->hbEpId;
D
dapan1121 已提交
210

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

D
dapan1121 已提交
213
  taskNum = taosHashGetSize(sch->tasksHash);
D
dapan1121 已提交
214 215 216

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

222
  void       *key = NULL;
dengyihao's avatar
dengyihao 已提交
223 224
  size_t      keyLen = 0;
  int32_t     i = 0;
D
dapan1121 已提交
225
  STaskStatus status = {0};
D
dapan1121 已提交
226 227 228 229

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

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

D
dapan1121 已提交
234
    QW_GET_QTID(key, status.queryId, status.taskId, status.execId);
D
dapan1121 已提交
235 236
    status.status = taskStatus->status;
    status.refId = taskStatus->refId;
dengyihao's avatar
dengyihao 已提交
237

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

D
dapan1121 已提交
240 241
    ++i;
    pIter = taosHashIterate(sch->tasksHash, pIter);
dengyihao's avatar
dengyihao 已提交
242
  }
D
dapan1121 已提交
243 244 245 246 247 248

  QW_UNLOCK(QW_READ, &sch->tasksLock);

  return TSDB_CODE_SUCCESS;
}

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

256 257 258 259
  if (NULL == ctx->sinkHandle) {
    return TSDB_CODE_SUCCESS;
  }

260
  *dataLen = 0;
D
dapan1121 已提交
261

262 263
  while (true) {
    dsGetDataLength(ctx->sinkHandle, &len, &queryEnd);
D
dapan1121 已提交
264

265
    if (len < 0) {
266
      QW_TASK_ELOG("invalid length from dsGetDataLength, length:%" PRId64 "", len);
267 268
      QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
    }
dengyihao's avatar
dengyihao 已提交
269

270 271 272 273 274 275 276
    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 已提交
277

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

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

        break;
      }

      pOutput->bufStatus = DS_BUF_EMPTY;

      break;
D
dapan1121 已提交
297
    }
D
dapan1121 已提交
298

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

302
    *dataLen += len;
D
dapan1121 已提交
303

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

306 307 308 309 310 311
    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 已提交
312

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

329
    if (0 == ctx->level) {
dengyihao's avatar
dengyihao 已提交
330 331
      QW_TASK_DLOG("task fetched blocks %d rows %" PRId64 ", level %d", pOutput->numOfBlocks, pOutput->numOfRows,
                   ctx->level);
332 333
      break;
    }
D
dapan1121 已提交
334

335
    if (pOutput->numOfRows >= QW_MIN_RES_ROWS) {
dengyihao's avatar
dengyihao 已提交
336 337
      QW_TASK_DLOG("task fetched blocks %d rows %" PRId64 " reaches the min rows", pOutput->numOfBlocks,
                   pOutput->numOfRows);
338 339
      break;
    }
D
dapan1121 已提交
340 341
  }

342 343
  *rspMsg = rsp;

D
dapan1121 已提交
344
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
345 346
}

347
int32_t qwGetDeleteResFromSink(QW_FPARAMS_DEF, SQWTaskCtx *ctx, SDeleteRes *pRes) {
348 349 350 351
  int64_t     len = 0;
  bool        queryEnd = false;
  int32_t     code = 0;
  SOutputData output = {0};
D
dapan1121 已提交
352 353 354

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

D
dapan1121 已提交
355
  if (len <= 0 || len != sizeof(SDeleterRes)) {
D
dapan1121 已提交
356
    QW_TASK_ELOG("invalid length from dsGetDataLength, length:%" PRId64, len);
D
dapan1121 已提交
357 358 359 360 361 362 363
    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);
  }
364

D
dapan1121 已提交
365 366 367 368 369 370 371
  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);
  }

372 373
  SDeleterRes *pDelRes = (SDeleterRes *)output.pData;

D
dapan1121 已提交
374
  pRes->suid = pDelRes->suid;
D
dapan1121 已提交
375 376 377
  pRes->uidList = pDelRes->uidList;
  pRes->skey = pDelRes->skey;
  pRes->ekey = pDelRes->ekey;
378
  pRes->affectedRows = pDelRes->affectedRows;
379 380
  strcpy(pRes->tableFName, pDelRes->tableName);
  strcpy(pRes->tsColName, pDelRes->tsColName);
D
dapan1121 已提交
381
  taosMemoryFree(output.pData);
382

D
dapan1121 已提交
383 384 385
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
386
int32_t qwHandlePrePhaseEvents(QW_FPARAMS_DEF, int8_t phase, SQWPhaseInput *input, SQWPhaseOutput *output) {
387 388
  int32_t     code = 0;
  SQWTaskCtx *ctx = NULL;
D
dapan1121 已提交
389

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

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

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

D
dapan1121 已提交
396
  QW_SET_PHASE(ctx, phase);
D
dapan1121 已提交
397

dengyihao's avatar
dengyihao 已提交
398
  if (atomic_load_8((int8_t *)&ctx->queryEnd)) {
D
dapan1121 已提交
399 400 401
    QW_TASK_ELOG_E("query already end");
    QW_ERR_JRET(TSDB_CODE_QW_MSG_ERROR);
  }
D
dapan1121 已提交
402

D
dapan1121 已提交
403 404
  switch (phase) {
    case QW_PHASE_PRE_QUERY: {
D
dapan1121 已提交
405
      if (QW_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
H
Haojun Liao 已提交
406
        QW_TASK_ELOG("task already dropped at wrong phase %s", qwPhaseStr(phase));
D
dapan1121 已提交
407
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_STATUS_ERROR);
D
dapan1121 已提交
408
      }
D
dapan1121 已提交
409

D
dapan1121 已提交
410
      if (QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
411
        QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
412

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

D
dapan1121 已提交
416
        QW_ERR_JRET(ctx->rspCode);
D
dapan1121 已提交
417
      }
D
dapan1121 已提交
418

D
dapan1121 已提交
419
      QW_ERR_JRET(qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_EXEC));
D
dapan1121 已提交
420 421
      break;
    }
D
dapan1121 已提交
422
    case QW_PHASE_PRE_FETCH: {
D
dapan1121 已提交
423
      if (QW_EVENT_PROCESSED(ctx, QW_EVENT_DROP) || QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
424
        QW_TASK_WLOG("task dropping or already dropped, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
425
        QW_ERR_JRET(ctx->rspCode);
D
dapan1121 已提交
426
      }
D
dapan1121 已提交
427

D
dapan1121 已提交
428
      if (QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
D
dapan1121 已提交
429
        QW_TASK_WLOG("last fetch still not processed, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
430 431 432
        QW_ERR_JRET(TSDB_CODE_QRY_DUPLICATTED_OPERATION);
      }

D
dapan1121 已提交
433 434 435 436 437
      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 已提交
438
      if (!ctx->queryRsped) {
D
dapan1121 已提交
439
        QW_TASK_ELOG("ready msg has not been processed, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
440
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_MSG_ERROR);
D
dapan1121 已提交
441 442
      }
      break;
dengyihao's avatar
dengyihao 已提交
443
    }
D
dapan1121 已提交
444
    case QW_PHASE_PRE_CQUERY: {
D
dapan1121 已提交
445
      if (QW_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
446
        QW_TASK_WLOG("task already dropped, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
447
        QW_ERR_JRET(ctx->rspCode);
D
dapan1121 已提交
448
      }
D
dapan1121 已提交
449

D
dapan1121 已提交
450 451 452 453 454
      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 已提交
455
      if (QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
456
        QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
H
Haojun Liao 已提交
457

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

D
dapan1121 已提交
461
        QW_ERR_JRET(ctx->rspCode);
D
dapan1121 已提交
462
      }
D
dapan1121 已提交
463

D
dapan1121 已提交
464
      break;
D
dapan1121 已提交
465 466 467
    }
    default:
      QW_TASK_ELOG("invalid phase %s", qwPhaseStr(phase));
S
Shengliang Guan 已提交
468
      QW_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
469 470 471
  }

  if (ctx->rspCode) {
472
    QW_TASK_ELOG("task already failed at phase %s, code:%s", qwPhaseStr(phase), tstrerror(ctx->rspCode));
D
dapan1121 已提交
473
    QW_ERR_JRET(ctx->rspCode);
D
dapan1121 已提交
474
  }
D
dapan1121 已提交
475

D
dapan1121 已提交
476
_return:
D
dapan1121 已提交
477

D
dapan1121 已提交
478
  if (ctx) {
D
dapan1121 已提交
479
    QW_UPDATE_RSP_CODE(ctx, code);
dengyihao's avatar
dengyihao 已提交
480

D
dapan1121 已提交
481
    QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
482 483
    qwReleaseTaskCtx(mgmt, ctx);
  }
D
dapan1121 已提交
484

485 486 487 488 489
  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 已提交
490 491 492 493 494

  QW_RET(code);
}

int32_t qwHandlePostPhaseEvents(QW_FPARAMS_DEF, int8_t phase, SQWPhaseInput *input, SQWPhaseOutput *output) {
495 496 497
  int32_t        code = 0;
  SQWTaskCtx    *ctx = NULL;
  SRpcHandleInfo connInfo = {0};
D
dapan1121 已提交
498

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

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

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

D
dapan1121 已提交
505
  if (QW_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
506
    QW_TASK_WLOG("task already dropped, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
507
    QW_ERR_JRET(ctx->rspCode);
D
dapan1121 已提交
508 509
  }

D
dapan1121 已提交
510
  if (QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
511
    QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
D
dapan1121 已提交
512
    QW_ERR_JRET(ctx->rspCode);
D
dapan1121 已提交
513 514 515
  }

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

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

_return:

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

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

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

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

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

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

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

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

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

  QW_RET(TSDB_CODE_SUCCESS);
}

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

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

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

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

D
dapan1121 已提交
574
  ctx->ctrlConnInfo = qwMsg->connInfo;
575
  ctx->sId = sId;
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
    QW_TASK_ELOG("create task result error, taskHandle:%p, sinkHandle:%p", pTaskInfo, sinkHandle);
S
Shengliang Guan 已提交
627
    QW_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
628 629
  }

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

D
dapan1121 已提交
647
  if (QUERY_RSP_POLICY_QUICK == tsQueryRspPolicy && ctx != NULL && QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
dengyihao's avatar
dengyihao 已提交
648 649
    void       *rsp = NULL;
    int32_t     dataLen = 0;
650
    SOutputData sOutput = {0};
D
dapan1121 已提交
651 652 653
    if (qwGetQueryResFromSink(QW_FPARAMS(), ctx, &dataLen, &rsp, &sOutput)) {
      return TSDB_CODE_SUCCESS;
    }
H
Haojun Liao 已提交
654

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

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

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

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

dengyihao's avatar
dengyihao 已提交
669 670
      QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code, tstrerror(code),
                   dataLen);
671 672 673
    }
  }

D
dapan1121 已提交
674
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
675 676
}

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

D
dapan1121 已提交
686
  do {
D
dapan1121 已提交
687
    ctx = NULL;
dengyihao's avatar
dengyihao 已提交
688

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

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

dengyihao's avatar
dengyihao 已提交
693 694
    atomic_store_8((int8_t *)&ctx->queryInQueue, 0);
    atomic_store_8((int8_t *)&ctx->queryContinue, 0);
D
dapan1121 已提交
695

696
    QW_ERR_JRET(qwExecTask(QW_FPARAMS(), ctx, &queryStop));
D
dapan1121 已提交
697

D
dapan1121 已提交
698
    if (QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
D
dapan1121 已提交
699
      SOutputData sOutput = {0};
D
dapan1121 已提交
700
      QW_ERR_JRET(qwGetQueryResFromSink(QW_FPARAMS(), ctx, &dataLen, &rsp, &sOutput));
dengyihao's avatar
dengyihao 已提交
701 702

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

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

D
dapan1121 已提交
708
      if (rsp) {
D
dapan1121 已提交
709
        qComplete = (DS_BUF_EMPTY == sOutput.bufStatus && sOutput.queryEnd);
dengyihao's avatar
dengyihao 已提交
710

D
dapan1121 已提交
711
        qwBuildFetchRsp(rsp, &sOutput, dataLen, qComplete);
D
dapan1121 已提交
712
        if (qComplete) {
dengyihao's avatar
dengyihao 已提交
713
          atomic_store_8((int8_t *)&ctx->queryEnd, true);
D
dapan1121 已提交
714
          atomic_store_8((int8_t *)&ctx->queryContinue, 0);
D
dapan1121 已提交
715
        }
H
Haojun Liao 已提交
716

D
dapan1121 已提交
717
        qwMsg->connInfo = ctx->dataConnInfo;
dengyihao's avatar
dengyihao 已提交
718 719
        QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_FETCH);

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

dengyihao's avatar
dengyihao 已提交
723 724
        QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code,
                     tstrerror(code), dataLen);
D
dapan1121 已提交
725
      } else {
dengyihao's avatar
dengyihao 已提交
726
        atomic_store_8((int8_t *)&ctx->queryContinue, 1);
727 728 729
      }
    }

dengyihao's avatar
dengyihao 已提交
730
  _return:
731

D
dapan1121 已提交
732 733 734 735
    if (NULL == ctx) {
      break;
    }

D
dapan1121 已提交
736
    if (code && QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
dengyihao's avatar
dengyihao 已提交
737
      QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_FETCH);
738 739
      qwFreeFetchRsp(rsp);
      rsp = NULL;
dengyihao's avatar
dengyihao 已提交
740

D
dapan1121 已提交
741
      qwMsg->connInfo = ctx->dataConnInfo;
742
      qwBuildAndSendFetchRsp(ctx->msgType + 1, &qwMsg->connInfo, NULL, 0, code);
dengyihao's avatar
dengyihao 已提交
743 744
      QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code, tstrerror(code),
                   0);
745
    }
D
dapan1121 已提交
746

D
dapan1121 已提交
747
    QW_LOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
748
    if (qComplete || (queryStop && (0 == atomic_load_8((int8_t *)&ctx->queryContinue))) || code) {
D
dapan1121 已提交
749 750
      // Note: query is not running anymore
      QW_SET_PHASE(ctx, 0);
dengyihao's avatar
dengyihao 已提交
751
      QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
752 753
      break;
    }
dengyihao's avatar
dengyihao 已提交
754
    QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
755
  } while (true);
D
dapan1121 已提交
756

D
dapan1121 已提交
757
  input.code = code;
dengyihao's avatar
dengyihao 已提交
758
  qwHandlePostPhaseEvents(QW_FPARAMS(), QW_PHASE_POST_CQUERY, &input, NULL);
D
dapan1121 已提交
759

dengyihao's avatar
dengyihao 已提交
760
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
761
}
D
dapan1121 已提交
762

D
dapan1121 已提交
763
int32_t qwProcessFetch(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
dengyihao's avatar
dengyihao 已提交
764 765 766
  int32_t       code = 0;
  int32_t       dataLen = 0;
  bool          locked = false;
767 768
  SQWTaskCtx   *ctx = NULL;
  void         *rsp = NULL;
D
dapan1121 已提交
769
  SQWPhaseInput input = {0};
D
dapan1121 已提交
770

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

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

D
dapan1121 已提交
775
  ctx->msgType = qwMsg->msgType;
D
dapan1121 已提交
776
  ctx->dataConnInfo = qwMsg->connInfo;
D
dapan1121 已提交
777

D
dapan 已提交
778
  SOutputData sOutput = {0};
D
dapan1121 已提交
779
  QW_ERR_JRET(qwGetQueryResFromSink(QW_FPARAMS(), ctx, &dataLen, &rsp, &sOutput));
D
dapan1121 已提交
780

781 782
  if (NULL == rsp) {
    QW_SET_EVENT_RECEIVED(ctx, QW_EVENT_FETCH);
D
dapan1121 已提交
783
  } else {
D
dapan1121 已提交
784
    bool qComplete = (DS_BUF_EMPTY == sOutput.bufStatus && sOutput.queryEnd);
dengyihao's avatar
dengyihao 已提交
785

D
dapan1121 已提交
786
    qwBuildFetchRsp(rsp, &sOutput, dataLen, qComplete);
D
dapan1121 已提交
787
    if (qComplete) {
dengyihao's avatar
dengyihao 已提交
788
      atomic_store_8((int8_t *)&ctx->queryEnd, true);
D
dapan1121 已提交
789
    }
D
dapan1121 已提交
790 791
  }

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

D
dapan1121 已提交
795 796
    QW_LOCK(QW_WRITE, &ctx->lock);
    locked = true;
797

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

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

D
dapan1121 已提交
808
      QW_ERR_JRET(qwBuildAndSendCQueryMsg(QW_FPARAMS(), &qwMsg->connInfo));
809
    }
D
dapan 已提交
810
  }
dengyihao's avatar
dengyihao 已提交
811

D
dapan1121 已提交
812
_return:
D
dapan1121 已提交
813

D
dapan1121 已提交
814 815 816 817 818
  if (locked) {
    QW_UNLOCK(QW_WRITE, &ctx->lock);
  }

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

D
dapan 已提交
821 822 823
  if (code) {
    qwFreeFetchRsp(rsp);
    rsp = NULL;
D
dapan1121 已提交
824
    dataLen = 0;
D
dapan1121 已提交
825 826 827
  }

  if (code || rsp) {
D
dapan1121 已提交
828
    bool rsped = false;
D
dapan1121 已提交
829
    if (ctx) {
830
      qwDbgSimulateRedirect(qwMsg, ctx, &rsped);
D
dapan1121 已提交
831 832
      qwDbgSimulateDead(QW_FPARAMS(), ctx, &rsped);
    }
D
dapan1121 已提交
833 834
    if (!rsped) {
      qwBuildAndSendFetchRsp(qwMsg->msgType + 1, &qwMsg->connInfo, rsp, dataLen, code);
835 836
      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 已提交
837
    }
D
dapan1121 已提交
838 839
  }

D
dapan1121 已提交
840
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
841
}
D
dapan1121 已提交
842

D
dapan1121 已提交
843
int32_t qwProcessDrop(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
dengyihao's avatar
dengyihao 已提交
844
  int32_t     code = 0;
D
dapan1121 已提交
845
  bool        dropped = false;
D
dapan1121 已提交
846
  SQWTaskCtx *ctx = NULL;
dengyihao's avatar
dengyihao 已提交
847
  bool        locked = false;
D
dapan1121 已提交
848

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

D
dapan1121 已提交
851 852 853 854
  QW_LOCK(QW_WRITE, &ctx->lock);

  locked = true;

D
dapan1121 已提交
855
  if (QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
856
    QW_TASK_WLOG_E("task already dropping");
D
dapan1121 已提交
857 858 859
    QW_ERR_JRET(TSDB_CODE_QRY_DUPLICATTED_OPERATION);
  }

D
dapan1121 已提交
860
  if (QW_QUERY_RUNNING(ctx)) {
D
dapan1121 已提交
861
    QW_ERR_JRET(qwKillTaskHandle(ctx, TSDB_CODE_TSC_QUERY_CANCELLED));
D
dapan1121 已提交
862
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_DROP);
D
dapan1121 已提交
863
  } else {
D
dapan1121 已提交
864
    QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
D
dapan1121 已提交
865
    dropped = true;
D
dapan1121 已提交
866
  }
D
dapan1121 已提交
867

D
dapan1121 已提交
868
  if (!dropped) {
D
dapan1121 已提交
869
    QW_UPDATE_RSP_CODE(ctx, TSDB_CODE_TSC_QUERY_CANCELLED);
D
dapan1121 已提交
870 871
    QW_SET_EVENT_RECEIVED(ctx, QW_EVENT_DROP);
  }
872

D
dapan1121 已提交
873
_return:
D
dapan1121 已提交
874

D
dapan1121 已提交
875
  if (code) {
D
dapan1121 已提交
876 877 878
    if (ctx) {
      QW_UPDATE_RSP_CODE(ctx, code);
    }
H
Haojun Liao 已提交
879

D
dapan1121 已提交
880
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_FAIL);
D
dapan1121 已提交
881 882
  }

D
dapan 已提交
883 884 885 886
  if (locked) {
    QW_UNLOCK(QW_WRITE, &ctx->lock);
  }

D
dapan1121 已提交
887
  if (ctx) {
D
dapan1121 已提交
888
    qwReleaseTaskCtx(mgmt, ctx);
D
dapan1121 已提交
889 890
  }

D
dapan1121 已提交
891 892 893
  QW_RET(TSDB_CODE_SUCCESS);
}

D
dapan1121 已提交
894
int32_t qwProcessHb(SQWorker *mgmt, SQWMsg *qwMsg, SSchedulerHbReq *req) {
dengyihao's avatar
dengyihao 已提交
895
  int32_t         code = 0;
D
dapan1121 已提交
896
  SSchedulerHbRsp rsp = {0};
897
  SQWSchStatus   *sch = NULL;
D
dapan1121 已提交
898

D
dapan1121 已提交
899 900 901
  if (qwMsg->code) {
    QW_RET(qwProcessHbLinkBroken(mgmt, qwMsg, req));
  }
D
dapan1121 已提交
902 903

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

D
dapan1121 已提交
906 907
  sch->hbBrokenTs = 0;

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

D
dapan1121 已提交
910
  if (sch->hbConnInfo.handle) {
911
    tmsgReleaseHandle(&sch->hbConnInfo, TAOS_CONN_SERVER);
S
Shengliang Guan 已提交
912
    sch->hbConnInfo.handle = NULL;
D
dapan1121 已提交
913
  }
D
dapan1121 已提交
914

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

D
dapan1121 已提交
918
  QW_UNLOCK(QW_WRITE, &sch->hbConnLock);
dengyihao's avatar
dengyihao 已提交
919 920 921

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

D
dapan1121 已提交
923 924 925 926
  qwReleaseScheduler(QW_READ, mgmt);

_return:

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

S
shm  
Shengliang Guan 已提交
929
  qwBuildAndSendHbRsp(&qwMsg->connInfo, &rsp, code);
D
dapan1121 已提交
930 931

  if (code) {
932
    tmsgReleaseHandle(&qwMsg->connInfo, TAOS_CONN_SERVER);
S
Shengliang Guan 已提交
933
    qwMsg->connInfo.handle = NULL;
D
dapan1121 已提交
934
  }
dengyihao's avatar
dengyihao 已提交
935

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

D
dapan1121 已提交
938
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
939 940 941
}

void qwProcessHbTimerEvent(void *param, void *tmrId) {
942
  SQWHbParam *hbParam = (SQWHbParam *)param;
D
dapan1121 已提交
943 944 945 946
  if (hbParam->qwrId != atomic_load_32(&gQwMgmt.qwRef)) {
    return;
  }

947
  int64_t   refId = hbParam->refId;
D
dapan1121 已提交
948 949 950 951 952
  SQWorker *mgmt = qwAcquire(refId);
  if (NULL == mgmt) {
    QW_DLOG("qwAcquire %" PRIx64 "failed", refId);
    return;
  }
953

D
dapan1121 已提交
954
  SQWSchStatus *sch = NULL;
dengyihao's avatar
dengyihao 已提交
955
  int32_t       taskNum = 0;
956
  SQWHbInfo    *rspList = NULL;
D
dapan1121 已提交
957
  SArray       *pExpiredSch = NULL;
dengyihao's avatar
dengyihao 已提交
958
  int32_t       code = 0;
D
dapan1121 已提交
959

D
dapan1121 已提交
960 961
  qwDbgDumpMgmtInfo(mgmt);

D
dapan1121 已提交
962 963 964 965 966
  QW_LOCK(QW_READ, &mgmt->schLock);

  int32_t schNum = taosHashGetSize(mgmt->schHash);
  if (schNum <= 0) {
    QW_UNLOCK(QW_READ, &mgmt->schLock);
D
dapan1121 已提交
967
    taosTmrReset(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, param, mgmt->timer, &mgmt->hbTimer);
D
dapan1121 已提交
968
    qwRelease(refId);
D
dapan1121 已提交
969
    return;
D
dapan1121 已提交
970 971
  }

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

984
  void   *key = NULL;
dengyihao's avatar
dengyihao 已提交
985
  size_t  keyLen = 0;
D
dapan1121 已提交
986
  int32_t i = 0;
D
dapan1121 已提交
987
  int64_t currentMs = taosGetTimestampMs();
D
dapan1121 已提交
988 989 990

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

H
Haojun Liao 已提交
996 997
      if (sch1->hbBrokenTs > 0 && ((currentMs - sch1->hbBrokenTs) > QW_SCH_TIMEOUT_MSEC) &&
          taosHashGetSize(sch1->tasksHash) <= 0) {
D
dapan1121 已提交
998 999
        taosArrayPush(pExpiredSch, sId);
      }
1000

D
dapan1121 已提交
1001 1002 1003
      pIter = taosHashIterate(mgmt->schHash, pIter);
      continue;
    }
dengyihao's avatar
dengyihao 已提交
1004

D
dapan1121 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
    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 已提交
1020
    qwBuildAndSendHbRsp(&rspList[j].connInfo, &rspList[j].rsp, code);
1021 1022
    /*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 已提交
1023
    tFreeSSchedulerHbRsp(&rspList[j].rsp);
D
dapan1121 已提交
1024 1025
  }

D
dapan1121 已提交
1026
  if (taosArrayGetSize(pExpiredSch) > 0) {
D
dapan1121 已提交
1027
    qwClearExpiredSch(mgmt, pExpiredSch);
D
dapan1121 已提交
1028 1029
  }

wafwerar's avatar
wafwerar 已提交
1030
  taosMemoryFreeClear(rspList);
D
dapan1121 已提交
1031
  taosArrayDestroy(pExpiredSch);
D
dapan1121 已提交
1032

dengyihao's avatar
dengyihao 已提交
1033
  taosTmrReset(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, param, mgmt->timer, &mgmt->hbTimer);
1034
  qwRelease(refId);
D
dapan1121 已提交
1035 1036
}

1037
int32_t qwProcessDelete(QW_FPARAMS_DEF, SQWMsg *qwMsg, SDeleteRes *pRes) {
D
dapan1121 已提交
1038 1039 1040 1041 1042 1043
  int32_t        code = 0;
  SSubplan      *plan = NULL;
  qTaskInfo_t    pTaskInfo = NULL;
  DataSinkHandle sinkHandle = NULL;
  SQWTaskCtx     ctx = {0};

1044
  code = qMsgToSubplan(qwMsg->msg, qwMsg->msgLen, &plan);
D
dapan1121 已提交
1045 1046 1047 1048 1049 1050
  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);
  }

1051
  code = qCreateExecTask(qwMsg->node, mgmt->nodeId, tId, plan, &pTaskInfo, &sinkHandle, NULL, OPTR_EXEC_MODEL_BATCH);
D
dapan1121 已提交
1052 1053 1054 1055 1056 1057 1058
  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 已提交
1059
    QW_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
1060 1061
  }

D
dapan1121 已提交
1062 1063
  ctx.taskHandle = pTaskInfo;
  ctx.sinkHandle = sinkHandle;
D
dapan1121 已提交
1064

D
dapan1121 已提交
1065
  QW_ERR_JRET(qwExecTask(QW_FPARAMS(), &ctx, NULL));
D
dapan1121 已提交
1066

1067
  QW_ERR_JRET(qwGetDeleteResFromSink(QW_FPARAMS(), &ctx, pRes));
D
dapan1121 已提交
1068 1069 1070

_return:

D
dapan1121 已提交
1071
  qwFreeTaskCtx(&ctx);
D
dapan1121 已提交
1072 1073 1074 1075

  QW_RET(TSDB_CODE_SUCCESS);
}

D
dapan1121 已提交
1076 1077
int32_t qWorkerInit(int8_t nodeType, int32_t nodeId, void **qWorkerMgmt, const SMsgCb *pMsgCb) {
  if (NULL == qWorkerMgmt || (pMsgCb && pMsgCb->mgmt == NULL)) {
D
dapan1121 已提交
1078 1079 1080
    qError("invalid param to init qworker");
    QW_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }
S
Shengliang 已提交
1081

D
dapan1121 已提交
1082 1083 1084 1085
  int32_t qwNum = atomic_add_fetch_32(&gQwMgmt.qwNum, 1);
  if (1 == qwNum) {
    memset(gQwMgmt.param, 0, sizeof(gQwMgmt.param));
  }
D
dapan1121 已提交
1086 1087 1088 1089 1090 1091 1092 1093

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

  SQWorker *mgmt = taosMemoryCalloc(1, sizeof(SQWorker));
D
dapan1121 已提交
1094
  if (NULL == mgmt) {
D
dapan1121 已提交
1095 1096
    qError("calloc %d failed", (int32_t)sizeof(SQWorker));
    atomic_sub_fetch_32(&gQwMgmt.qwNum, 1);
S
Shengliang Guan 已提交
1097
    QW_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1098 1099
  }

D
dapan1121 已提交
1100 1101 1102
  mgmt->cfg.maxSchedulerNum = QW_DEFAULT_SCHEDULER_NUMBER;
  mgmt->cfg.maxTaskNum = QW_DEFAULT_TASK_NUMBER;
  mgmt->cfg.maxSchTaskNum = QW_DEFAULT_SCH_TASK_NUMBER;
D
dapan1121 已提交
1103

dengyihao's avatar
dengyihao 已提交
1104 1105
  mgmt->schHash = taosHashInit(mgmt->cfg.maxSchedulerNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false,
                               HASH_ENTRY_LOCK);
D
dapan1121 已提交
1106
  if (NULL == mgmt->schHash) {
wafwerar's avatar
wafwerar 已提交
1107
    taosMemoryFreeClear(mgmt);
D
dapan1121 已提交
1108
    qError("init %d scheduler hash failed", mgmt->cfg.maxSchedulerNum);
S
Shengliang Guan 已提交
1109
    QW_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1110 1111
  }

dengyihao's avatar
dengyihao 已提交
1112 1113
  mgmt->ctxHash =
      taosHashInit(mgmt->cfg.maxTaskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
D
dapan1121 已提交
1114
  if (NULL == mgmt->ctxHash) {
D
dapan1121 已提交
1115
    qError("init %d task ctx hash failed", mgmt->cfg.maxTaskNum);
S
Shengliang Guan 已提交
1116
    QW_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1117 1118 1119 1120 1121
  }

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

D
dapan1121 已提交
1125 1126
  mgmt->nodeType = nodeType;
  mgmt->nodeId = nodeId;
D
dapan1121 已提交
1127 1128 1129 1130 1131
  if (pMsgCb) {
    mgmt->msgCb = *pMsgCb;
  } else {
    memset(&mgmt->msgCb, 0, sizeof(mgmt->msgCb));
  }
D
dapan1121 已提交
1132

D
dapan1121 已提交
1133 1134 1135 1136 1137 1138
  mgmt->refId = taosAddRef(gQwMgmt.qwRef, mgmt);
  if (mgmt->refId < 0) {
    qError("taosAddRef qw failed, error:%s", tstrerror(terrno));
    QW_ERR_JRET(terrno);
  }

D
dapan1121 已提交
1139 1140 1141
  SQWHbParam *param = NULL;
  qwSetHbParam(mgmt->refId, &param);

1142
  mgmt->hbTimer = taosTmrStart(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, (void *)param, mgmt->timer);
D
dapan1121 已提交
1143 1144
  if (NULL == mgmt->hbTimer) {
    qError("start hb timer failed");
S
Shengliang Guan 已提交
1145
    QW_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1146
  }
1147

D
dapan1121 已提交
1148 1149
  *qWorkerMgmt = mgmt;

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

D
dapan1121 已提交
1152
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1153 1154 1155

_return:

D
dapan1121 已提交
1156 1157 1158 1159 1160 1161 1162
  if (mgmt->refId >= 0) {
    qwRelease(mgmt->refId);
  } else {
    taosHashCleanup(mgmt->schHash);
    taosHashCleanup(mgmt->ctxHash);
    taosTmrCleanUp(mgmt->timer);
    taosMemoryFreeClear(mgmt);
D
dapan1121 已提交
1163

1164
    atomic_sub_fetch_32(&gQwMgmt.qwNum, 1);
D
dapan1121 已提交
1165
  }
1166

D
dapan1121 已提交
1167
  QW_RET(code);
D
dapan1121 已提交
1168
}
D
dapan1121 已提交
1169

D
dapan1121 已提交
1170
void qWorkerStopAllTasks(void *qWorkerMgmt) {
D
dapan1121 已提交
1171 1172 1173
  SQWorker *mgmt = (SQWorker *)qWorkerMgmt;

  QW_DLOG("start to stop all tasks, taskNum:%d", taosHashGetSize(mgmt->ctxHash));
D
dapan1121 已提交
1174

1175
  uint64_t qId, tId, sId;
dengyihao's avatar
dengyihao 已提交
1176
  int32_t  eId;
1177
  int64_t  rId = 0;
D
dapan1121 已提交
1178 1179 1180 1181 1182 1183 1184 1185
  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);

1186 1187
    sId = ctx->sId;

D
dapan1121 已提交
1188
    QW_TASK_DLOG_E("start to force stop task");
dengyihao's avatar
dengyihao 已提交
1189

D
dapan1121 已提交
1190 1191 1192 1193 1194 1195 1196
    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;
    }
dengyihao's avatar
dengyihao 已提交
1197

D
dapan1121 已提交
1198
    if (QW_QUERY_RUNNING(ctx)) {
D
dapan1121 已提交
1199
      qwKillTaskHandle(ctx, TSDB_CODE_VND_STOPPED);
1200
    } else if (QW_FETCH_RUNNING(ctx)) {
D
dapan1121 已提交
1201
      QW_UPDATE_RSP_CODE(ctx, TSDB_CODE_VND_STOPPED);
1202 1203 1204
      QW_SET_EVENT_RECEIVED(ctx, QW_EVENT_DROP);    
    } else {
      qwDropTask(QW_FPARAMS());
D
dapan1121 已提交
1205 1206 1207 1208 1209 1210 1211 1212
    }

    QW_UNLOCK(QW_WRITE, &ctx->lock);

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

D
dapan1121 已提交
1213 1214 1215
void qWorkerDestroy(void **qWorkerMgmt) {
  if (NULL == qWorkerMgmt || NULL == *qWorkerMgmt) {
    return;
D
dapan1121 已提交
1216
  }
D
dapan 已提交
1217

H
Hongze Cheng 已提交
1218
  int32_t   destroyed = 0;
D
dapan1121 已提交
1219
  SQWorker *mgmt = *qWorkerMgmt;
D
dapan1121 已提交
1220
  mgmt->destroyed = &destroyed;
D
dapan1121 已提交
1221

D
dapan1121 已提交
1222 1223
  if (taosRemoveRef(gQwMgmt.qwRef, mgmt->refId)) {
    qError("remove qw from ref list failed, refId:%" PRIx64, mgmt->refId);
D
dapan1121 已提交
1224 1225 1226 1227 1228
    return;
  }

  while (0 == destroyed) {
    taosMsleep(2);
D
dapan1121 已提交
1229
  }
D
dapan1121 已提交
1230
}
D
dapan1121 已提交
1231

D
dapan1121 已提交
1232 1233 1234 1235 1236
int32_t qWorkerGetStat(SReadHandle *handle, void *qWorkerMgmt, SQWorkerStat *pStat) {
  if (NULL == handle || NULL == qWorkerMgmt || NULL == pStat) {
    QW_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

1237
  SQWorker     *mgmt = (SQWorker *)qWorkerMgmt;
D
dapan1121 已提交
1238
  SDataSinkStat sinkStat = {0};
1239

D
dapan1121 已提交
1240 1241
  dsDataSinkGetCacheSize(&sinkStat);
  pStat->cacheDataSize = sinkStat.cachedSize;
1242

D
dapan1121 已提交
1243 1244 1245 1246 1247
  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 已提交
1248
  pStat->deleteProcessed = QW_STAT_GET(mgmt->stat.msgStat.deleteProcessed);
D
dapan1121 已提交
1249 1250 1251 1252 1253 1254 1255

  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 已提交
1256
}
D
dapan1121 已提交
1257

H
Hongze Cheng 已提交
1258 1259 1260
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 已提交
1261 1262 1263 1264 1265 1266
  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 已提交
1267
  SReadHandle    rHandle = {0};
D
dapan1121 已提交
1268 1269

  QW_ERR_JRET(qwAddTaskCtx(QW_FPARAMS()));
D
dapan1121 已提交
1270
  QW_ERR_JRET(qwAddTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_INIT));
D
dapan1121 已提交
1271

D
dapan1121 已提交
1272
  QW_ERR_JRET(qwHandlePrePhaseEvents(QW_FPARAMS(), QW_PHASE_PRE_QUERY, &input, NULL));
D
dapan1121 已提交
1273 1274 1275 1276 1277 1278
  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 已提交
1279 1280
  ctx->localExec = true;
  ctx->explainRes = explainRes;
D
dapan1121 已提交
1281

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

D
dapan1121 已提交
1285
  code = qCreateExecTask(&rHandle, mgmt->nodeId, tId, plan, &pTaskInfo, &sinkHandle, NULL, OPTR_EXEC_MODEL_BATCH);
D
dapan1121 已提交
1286 1287 1288 1289 1290 1291 1292
  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 已提交
1293
    QW_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
  }

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

D
dapan1121 已提交
1306 1307 1308 1309
  input.code = code;
  input.msgType = qwMsg->msgType;
  code = qwHandlePostPhaseEvents(QW_FPARAMS(), QW_PHASE_POST_QUERY, &input, NULL);

D
dapan1121 已提交
1310 1311 1312 1313 1314 1315 1316 1317
  if (ctx) {
    QW_UPDATE_RSP_CODE(ctx, code);
    qwReleaseTaskCtx(mgmt, ctx);
  }

  QW_RET(code);
}

H
Hongze Cheng 已提交
1318 1319 1320 1321 1322 1323 1324 1325
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 已提交
1326 1327 1328 1329 1330 1331 1332 1333 1334

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

D
dapan1121 已提交
1336
  SOutputData sOutput = {0};
H
Haojun Liao 已提交
1337

D
dapan1121 已提交
1338 1339 1340 1341 1342
  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 已提交
1343

D
dapan1121 已提交
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
      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);
}