qworker.c 32.7 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
Haojun Liao 已提交
10
#include "tmsg.h"
11
#include "tname.h"
D
dapan1121 已提交
12

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

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

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

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

D
dapan1121 已提交
28
  sch->hbBrokenTs = taosGetTimestampMs();
29

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

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

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

D
dapan1121 已提交
42
  qwReleaseScheduler(QW_READ, mgmt);
D
dapan1121 已提交
43

D
dapan1121 已提交
44
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
45 46
}

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

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

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

      SRpcHandleInfo connInfo = ctx->ctrlConnInfo;
      connInfo.ahandle = NULL;
H
Haojun Liao 已提交
62 63 64
      int32_t code = qwBuildAndSendExplainRsp(&connInfo, execInfoList);
      taosArrayDestroyEx(execInfoList, freeItem);
      QW_ERR_RET(code);
D
dapan1121 已提交
65
    }
D
dapan1121 已提交
66 67 68 69

    if (!ctx->needFetch) {
      dsGetDataLength(ctx->sinkHandle, &ctx->affectedRows, NULL);
    }
D
dapan1121 已提交
70 71 72 73 74
  }

  return TSDB_CODE_SUCCESS;
}

75
int32_t qwExecTask(QW_FPARAMS_DEF, SQWTaskCtx *ctx, bool *queryStop) {
dengyihao's avatar
dengyihao 已提交
76 77 78 79 80
  int32_t        code = 0;
  bool           qcontinue = true;
  uint64_t       useconds = 0;
  int32_t        i = 0;
  int32_t        execNum = 0;
D
dapan1121 已提交
81
  qTaskInfo_t    taskHandle = ctx->taskHandle;
D
dapan1121 已提交
82
  DataSinkHandle sinkHandle = ctx->sinkHandle;
dengyihao's avatar
dengyihao 已提交
83

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

D
dapan1121 已提交
88 89
    // if *taskHandle is NULL, it's killed right now
    if (taskHandle) {
D
dapan1121 已提交
90
      qwDbgSimulateSleep();
91
      code = qExecTaskOpt(taskHandle, pResList, &useconds);
D
dapan1121 已提交
92
      if (code) {
93 94 95 96 97
        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));
        }
98
        QW_ERR_JRET(code);
D
dapan1121 已提交
99
      }
D
dapan1121 已提交
100 101
    }

D
dapan1121 已提交
102 103
    ++execNum;

H
Haojun Liao 已提交
104
    if (taosArrayGetSize(pResList) == 0) {
dengyihao's avatar
dengyihao 已提交
105
      QW_TASK_DLOG("qExecTask end with empty res, useconds:%" PRIu64, useconds);
D
dapan1121 已提交
106
      dsEndPut(sinkHandle, useconds);
D
dapan1121 已提交
107

108
      QW_ERR_JRET(qwHandleTaskComplete(QW_FPARAMS(), ctx));
D
dapan1121 已提交
109

110 111
      if (queryStop) {
        *queryStop = true;
D
dapan1121 已提交
112
      }
dengyihao's avatar
dengyihao 已提交
113

D
dapan1121 已提交
114 115 116
      break;
    }

117
    for (int32_t j = 0; j < taosArrayGetSize(pResList); ++j) {
H
Haojun Liao 已提交
118 119
      SSDataBlock *pRes = taosArrayGetP(pResList, j);
      ASSERT(pRes->info.rows > 0);
H
Haojun Liao 已提交
120

H
Haojun Liao 已提交
121 122 123 124
      SInputData inputData = {.pData = pRes};
      code = dsPutDataBlock(sinkHandle, &inputData, &qcontinue);
      if (code) {
        QW_TASK_ELOG("dsPutDataBlock failed, code:%x - %s", code, tstrerror(code));
125
        QW_ERR_JRET(code);
H
Haojun Liao 已提交
126
      }
127

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

D
dapan1121 已提交
131
    if (!qcontinue) {
132 133 134
      if (queryStop) {
        *queryStop = true;
      }
135

D
dapan1121 已提交
136 137 138
      break;
    }

D
dapan1121 已提交
139
    if (ctx->needFetch && (!ctx->queryRsped) && execNum >= QW_DEFAULT_SHORT_RUN_TIMES) {
D
dapan1121 已提交
140 141 142
      break;
    }

D
dapan1121 已提交
143
    if (QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
D
dapan1121 已提交
144 145
      break;
    }
D
dapan1121 已提交
146

D
dapan1121 已提交
147 148 149
    if (atomic_load_32(&ctx->rspCode)) {
      break;
    }
D
dapan1121 已提交
150 151
  }

H
Haojun Liao 已提交
152
  taosArrayDestroy(pResList);
D
dapan1121 已提交
153
  QW_RET(code);
154 155 156 157 158

_return:
  taosArrayDestroy(pResList);

  return code;
D
dapan1121 已提交
159
}
D
dapan1121 已提交
160

D
dapan1121 已提交
161
int32_t qwGenerateSchHbRsp(SQWorker *mgmt, SQWSchStatus *sch, SQWHbInfo *hbInfo) {
D
dapan1121 已提交
162 163
  int32_t taskNum = 0;

D
dapan1121 已提交
164
  hbInfo->connInfo = sch->hbConnInfo;
D
dapan1121 已提交
165
  hbInfo->rsp.epId = sch->hbEpId;
D
dapan1121 已提交
166

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

D
dapan1121 已提交
169
  taskNum = taosHashGetSize(sch->tasksHash);
D
dapan1121 已提交
170 171 172

  hbInfo->rsp.taskStatus = taosArrayInit(taskNum, sizeof(STaskStatus));
  if (NULL == hbInfo->rsp.taskStatus) {
D
dapan1121 已提交
173
    QW_UNLOCK(QW_READ, &sch->tasksLock);
D
dapan1121 已提交
174
    QW_ELOG("taosArrayInit taskStatus failed, num:%d", taskNum);
D
dapan1121 已提交
175 176 177
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }

178
  void       *key = NULL;
dengyihao's avatar
dengyihao 已提交
179 180
  size_t      keyLen = 0;
  int32_t     i = 0;
D
dapan1121 已提交
181
  STaskStatus status = {0};
D
dapan1121 已提交
182 183 184 185

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

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

D
dapan1121 已提交
190
    QW_GET_QTID(key, status.queryId, status.taskId, status.execId);
D
dapan1121 已提交
191 192
    status.status = taskStatus->status;
    status.refId = taskStatus->refId;
dengyihao's avatar
dengyihao 已提交
193

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

D
dapan1121 已提交
196 197
    ++i;
    pIter = taosHashIterate(sch->tasksHash, pIter);
dengyihao's avatar
dengyihao 已提交
198
  }
D
dapan1121 已提交
199 200 201 202 203 204

  QW_UNLOCK(QW_READ, &sch->tasksLock);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
205
int32_t qwGetQueryResFromSink(QW_FPARAMS_DEF, SQWTaskCtx *ctx, int32_t *dataLen, void **rspMsg, SOutputData *pOutput) {
D
dapan1121 已提交
206
  int64_t            len = 0;
D
dapan1121 已提交
207
  SRetrieveTableRsp *rsp = NULL;
dengyihao's avatar
dengyihao 已提交
208 209
  bool               queryEnd = false;
  int32_t            code = 0;
210
  SOutputData        output = {0};
D
dapan1121 已提交
211

212
  *dataLen = 0;
D
dapan1121 已提交
213

214 215
  while (true) {
    dsGetDataLength(ctx->sinkHandle, &len, &queryEnd);
D
dapan1121 已提交
216

217 218 219 220
    if (len < 0) {
      QW_TASK_ELOG("invalid length from dsGetDataLength, length:%d", len);
      QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
    }
dengyihao's avatar
dengyihao 已提交
221

222 223 224 225 226 227 228
    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 已提交
229

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

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
        qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_SUCC);
        if (NULL == rsp) {
          QW_ERR_RET(qwMallocFetchRsp(len, &rsp));
          *pOutput = output;
        } else {
          pOutput->queryEnd = output.queryEnd;
          pOutput->bufStatus = output.bufStatus;
          pOutput->useconds = output.useconds;
        }

        break;
      }

      pOutput->bufStatus = DS_BUF_EMPTY;

      break;
D
dapan1121 已提交
249
    }
D
dapan1121 已提交
250

251 252
    // Got data from sink
    QW_TASK_DLOG("there are data in sink, dataLength:%d", len);
D
dapan1121 已提交
253

254
    *dataLen += len;
D
dapan1121 已提交
255

256
    QW_ERR_RET(qwMallocFetchRsp(*dataLen, &rsp));
D
dapan1121 已提交
257

258 259 260 261 262 263
    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 已提交
264

265 266 267 268 269 270 271 272 273 274
    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) {
275 276
      QW_TASK_DLOG("task all data fetched and done, fetched blocks %d rows %d", pOutput->numOfBlocks,
                   pOutput->numOfRows);
277 278 279
      qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_SUCC);
      break;
    }
dengyihao's avatar
dengyihao 已提交
280

281 282 283 284
    if (0 == ctx->level) {
      QW_TASK_DLOG("task fetched blocks %d rows %d, level %d", pOutput->numOfBlocks, pOutput->numOfRows, ctx->level);
      break;
    }
D
dapan1121 已提交
285

286 287 288 289
    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 已提交
290 291
  }

292 293
  *rspMsg = rsp;

D
dapan1121 已提交
294
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
295 296
}

297
int32_t qwGetDeleteResFromSink(QW_FPARAMS_DEF, SQWTaskCtx *ctx, SDeleteRes *pRes) {
298 299 300 301
  int64_t     len = 0;
  bool        queryEnd = false;
  int32_t     code = 0;
  SOutputData output = {0};
D
dapan1121 已提交
302 303 304

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

D
dapan1121 已提交
305
  if (len <= 0 || len != sizeof(SDeleterRes)) {
D
dapan1121 已提交
306
    QW_TASK_ELOG("invalid length from dsGetDataLength, length:%" PRId64, len);
D
dapan1121 已提交
307 308 309 310 311 312 313
    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);
  }
314

D
dapan1121 已提交
315 316 317 318 319 320 321
  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);
  }

322 323
  SDeleterRes *pDelRes = (SDeleterRes *)output.pData;

D
dapan1121 已提交
324
  pRes->suid = pDelRes->suid;
D
dapan1121 已提交
325 326 327
  pRes->uidList = pDelRes->uidList;
  pRes->skey = pDelRes->skey;
  pRes->ekey = pDelRes->ekey;
328
  pRes->affectedRows = pDelRes->affectedRows;
329 330
  strcpy(pRes->tableFName, pDelRes->tableName);
  strcpy(pRes->tsColName, pDelRes->tsColName);
D
dapan1121 已提交
331
  taosMemoryFree(output.pData);
332

D
dapan1121 已提交
333 334 335
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
336
int32_t qwHandlePrePhaseEvents(QW_FPARAMS_DEF, int8_t phase, SQWPhaseInput *input, SQWPhaseOutput *output) {
337 338
  int32_t     code = 0;
  SQWTaskCtx *ctx = NULL;
D
dapan1121 已提交
339

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

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

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

D
dapan1121 已提交
346
  QW_SET_PHASE(ctx, phase);
D
dapan1121 已提交
347

dengyihao's avatar
dengyihao 已提交
348
  if (atomic_load_8((int8_t *)&ctx->queryEnd)) {
D
dapan1121 已提交
349 350 351
    QW_TASK_ELOG_E("query already end");
    QW_ERR_JRET(TSDB_CODE_QW_MSG_ERROR);
  }
D
dapan1121 已提交
352

D
dapan1121 已提交
353 354
  switch (phase) {
    case QW_PHASE_PRE_QUERY: {
D
dapan1121 已提交
355
      if (QW_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
H
Haojun Liao 已提交
356
        QW_TASK_ELOG("task already dropped at wrong phase %s", qwPhaseStr(phase));
D
dapan1121 已提交
357
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_STATUS_ERROR);
D
dapan1121 已提交
358 359
        break;
      }
D
dapan1121 已提交
360

D
dapan1121 已提交
361
      if (QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
362
        QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
363

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

D
dapan1121 已提交
367
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
D
dapan 已提交
368
        break;
D
dapan1121 已提交
369
      }
D
dapan1121 已提交
370

D
dapan1121 已提交
371
      QW_ERR_JRET(qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_EXEC));
D
dapan1121 已提交
372 373
      break;
    }
D
dapan1121 已提交
374
    case QW_PHASE_PRE_FETCH: {
D
dapan1121 已提交
375
      if (QW_EVENT_PROCESSED(ctx, QW_EVENT_DROP) || QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
376
        QW_TASK_WLOG("task dropping or already dropped, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
377 378
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
      }
D
dapan1121 已提交
379

D
dapan1121 已提交
380
      if (QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
D
dapan1121 已提交
381
        QW_TASK_WLOG("last fetch still not processed, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
382 383 384
        QW_ERR_JRET(TSDB_CODE_QRY_DUPLICATTED_OPERATION);
      }

D
dapan1121 已提交
385
      if (!ctx->queryRsped) {
D
dapan1121 已提交
386
        QW_TASK_ELOG("ready msg has not been processed, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
387
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_MSG_ERROR);
D
dapan1121 已提交
388 389
      }
      break;
dengyihao's avatar
dengyihao 已提交
390
    }
D
dapan1121 已提交
391
    case QW_PHASE_PRE_CQUERY: {
D
dapan1121 已提交
392
      if (QW_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
393
        QW_TASK_WLOG("task already dropped, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
394
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
D
dapan1121 已提交
395
      }
D
dapan1121 已提交
396

D
dapan1121 已提交
397
      if (QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
398
        QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
H
Haojun Liao 已提交
399

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

D
dapan1121 已提交
403
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
D
dapan1121 已提交
404
      }
D
dapan1121 已提交
405

D
dapan1121 已提交
406
      break;
D
dapan1121 已提交
407 408 409 410 411 412 413
    }
    default:
      QW_TASK_ELOG("invalid phase %s", qwPhaseStr(phase));
      QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  }

  if (ctx->rspCode) {
414
    QW_TASK_ELOG("task already failed at phase %s, code:%s", qwPhaseStr(phase), tstrerror(ctx->rspCode));
D
dapan1121 已提交
415
    QW_ERR_JRET(ctx->rspCode);
D
dapan1121 已提交
416
  }
D
dapan1121 已提交
417

D
dapan1121 已提交
418
_return:
D
dapan1121 已提交
419

D
dapan1121 已提交
420
  if (ctx) {
D
dapan1121 已提交
421
    QW_UPDATE_RSP_CODE(ctx, code);
dengyihao's avatar
dengyihao 已提交
422

D
dapan1121 已提交
423
    QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
424 425
    qwReleaseTaskCtx(mgmt, ctx);
  }
D
dapan1121 已提交
426

427 428 429 430 431
  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 已提交
432 433 434 435 436

  QW_RET(code);
}

int32_t qwHandlePostPhaseEvents(QW_FPARAMS_DEF, int8_t phase, SQWPhaseInput *input, SQWPhaseOutput *output) {
437 438 439
  int32_t        code = 0;
  SQWTaskCtx    *ctx = NULL;
  SRpcHandleInfo connInfo = {0};
D
dapan1121 已提交
440

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

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

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

D
dapan1121 已提交
447
  if (QW_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
448
    QW_TASK_WLOG("task already dropped, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
449 450 451
    QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
  }

D
dapan1121 已提交
452
  if (QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
453 454 455 456
    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 已提交
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 462
    QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
    QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
D
dapan1121 已提交
463 464 465
  }

  if (ctx->rspCode) {
dengyihao's avatar
dengyihao 已提交
466 467
    QW_TASK_ELOG("task already failed, phase %s, error:%x - %s", qwPhaseStr(phase), ctx->rspCode,
                 tstrerror(ctx->rspCode));
D
dapan1121 已提交
468
    QW_ERR_JRET(ctx->rspCode);
dengyihao's avatar
dengyihao 已提交
469
  }
D
dapan1121 已提交
470

D
dapan1121 已提交
471
  QW_ERR_JRET(input->code);
D
dapan1121 已提交
472 473 474

_return:

D
dapan1121 已提交
475
  if (TSDB_CODE_SUCCESS == code && QW_PHASE_POST_QUERY == phase) {
D
dapan1121 已提交
476
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_PART_SUCC);
D
dapan1121 已提交
477 478
  }

D
dapan1121 已提交
479
  if (QW_PHASE_POST_QUERY == phase && ctx) {
D
dapan1121 已提交
480
    ctx->queryRsped = true;
D
dapan1121 已提交
481

482
    bool   rsped = false;
D
dapan1121 已提交
483
    SQWMsg qwMsg = {.msgType = ctx->msgType, .connInfo = ctx->ctrlConnInfo};
D
dapan1121 已提交
484 485
    qwDbgSimulateRedirect(&qwMsg, ctx, &rsped);
    qwDbgSimulateDead(QW_FPARAMS(), ctx, &rsped);
D
dapan1121 已提交
486 487
    if (!rsped) {
      qwBuildAndSendQueryRsp(input->msgType + 1, &ctx->ctrlConnInfo, code, ctx);
D
dapan1121 已提交
488
      QW_TASK_DLOG("query msg rsped, handle:%p, code:%x - %s", ctx->ctrlConnInfo.handle, code, tstrerror(code));
489
    }
490 491
  }

D
dapan1121 已提交
492
  if (ctx) {
D
dapan1121 已提交
493
    QW_UPDATE_RSP_CODE(ctx, code);
D
dapan1121 已提交
494

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

D
dapan1121 已提交
497
    QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
498
    qwReleaseTaskCtx(mgmt, ctx);
D
dapan1121 已提交
499 500
  }

D
dapan1121 已提交
501
  if (code) {
D
dapan1121 已提交
502
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_FAIL);
D
dapan1121 已提交
503 504
  }

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

D
dapan1121 已提交
507 508 509
  QW_RET(code);
}

D
dapan1121 已提交
510 511 512 513 514 515
int32_t qwAbortPrerocessQuery(QW_FPARAMS_DEF) {
  QW_ERR_RET(qwDropTask(QW_FPARAMS()));

  QW_RET(TSDB_CODE_SUCCESS);
}

D
dapan1121 已提交
516
int32_t qwPreprocessQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
517 518 519 520 521 522 523
  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 已提交
524

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

D
dapan1121 已提交
527 528 529
  QW_ERR_JRET(qwAddTaskCtx(QW_FPARAMS()));

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

D
dapan1121 已提交
531 532
  ctx->ctrlConnInfo = qwMsg->connInfo;

D
dapan1121 已提交
533
  QW_ERR_JRET(qwAddTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_INIT));
D
dapan1121 已提交
534 535 536 537 538 539 540 541 542 543 544

_return:

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

  QW_RET(TSDB_CODE_SUCCESS);
}

545
int32_t qwProcessQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg, char *sql) {
D
dapan1121 已提交
546 547 548 549 550 551 552 553
  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 已提交
554
  QW_ERR_JRET(qwHandlePrePhaseEvents(QW_FPARAMS(), QW_PHASE_PRE_QUERY, &input, NULL));
D
dapan1121 已提交
555 556

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

D
dapan1121 已提交
558 559 560
  ctx->taskType = qwMsg->msgInfo.taskType;
  ctx->explain = qwMsg->msgInfo.explain;
  ctx->needFetch = qwMsg->msgInfo.needFetch;
D
dapan1121 已提交
561
  ctx->msgType = qwMsg->msgType;
X
Xiaoyu Wang 已提交
562

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

D
dapan1121 已提交
565 566
  code = qStringToSubplan(qwMsg->msg, &plan);
  if (TSDB_CODE_SUCCESS != code) {
567 568
    code = TSDB_CODE_INVALID_MSG;
    QW_TASK_ELOG("task physical plan to subplan failed, code:%x - %s", code, tstrerror(code));
D
dapan1121 已提交
569
    QW_ERR_JRET(code);
D
dapan1121 已提交
570
  }
dengyihao's avatar
dengyihao 已提交
571

572
  code = qCreateExecTask(qwMsg->node, mgmt->nodeId, tId, plan, &pTaskInfo, &sinkHandle, sql, OPTR_EXEC_MODEL_BATCH);
D
dapan1121 已提交
573
  sql = NULL;
D
dapan1121 已提交
574
  if (code) {
D
dapan1121 已提交
575
    QW_TASK_ELOG("qCreateExecTask failed, code:%x - %s", code, tstrerror(code));
D
dapan1121 已提交
576
    QW_ERR_JRET(code);
D
dapan1121 已提交
577
  }
D
dapan1121 已提交
578

H
Haojun Liao 已提交
579
  if (NULL == sinkHandle || NULL == pTaskInfo) {
D
dapan1121 已提交
580 581 582 583
    QW_TASK_ELOG("create task result error, taskHandle:%p, sinkHandle:%p", pTaskInfo, sinkHandle);
    QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  }

dengyihao's avatar
dengyihao 已提交
584 585
  // QW_ERR_JRET(qwBuildAndSendQueryRsp(&qwMsg->connInfo, code));
  // QW_TASK_DLOG("query msg rsped, handle:%p, code:%x - %s", qwMsg->connInfo.handle, code, tstrerror(code));
D
dapan1121 已提交
586

dengyihao's avatar
dengyihao 已提交
587
  // queryRsped = true;
D
dapan1121 已提交
588

589
  ctx->level = plan->level;
D
dapan1121 已提交
590 591 592
  atomic_store_ptr(&ctx->taskHandle, pTaskInfo);
  atomic_store_ptr(&ctx->sinkHandle, sinkHandle);

D
dapan1121 已提交
593
  if (pTaskInfo && sinkHandle) {
594
    qwSaveTbVersionInfo(pTaskInfo, ctx);
D
dapan1121 已提交
595
    QW_ERR_JRET(qwExecTask(QW_FPARAMS(), ctx, NULL));
D
dapan1121 已提交
596
  }
dengyihao's avatar
dengyihao 已提交
597

D
dapan1121 已提交
598 599
_return:

D
dapan1121 已提交
600
  taosMemoryFree(sql);
601

D
dapan1121 已提交
602
  input.code = code;
D
dapan1121 已提交
603
  input.msgType = qwMsg->msgType;
D
dapan1121 已提交
604
  code = qwHandlePostPhaseEvents(QW_FPARAMS(), QW_PHASE_POST_QUERY, &input, NULL);
605

dengyihao's avatar
dengyihao 已提交
606
  // if (!queryRsped) {
D
dapan1121 已提交
607 608 609
  //  qwBuildAndSendQueryRsp(&qwMsg->connInfo, code);
  //  QW_TASK_DLOG("query msg rsped, handle:%p, code:%x - %s", qwMsg->connInfo.handle, code, tstrerror(code));
  //}
D
dapan1121 已提交
610

D
dapan1121 已提交
611
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
612 613
}

D
dapan1121 已提交
614
int32_t qwProcessCQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
615
  SQWTaskCtx   *ctx = NULL;
dengyihao's avatar
dengyihao 已提交
616
  int32_t       code = 0;
617
  SQWPhaseInput input = {0};
618
  void         *rsp = NULL;
dengyihao's avatar
dengyihao 已提交
619
  int32_t       dataLen = 0;
620
  bool          queryStop = false;
dengyihao's avatar
dengyihao 已提交
621

D
dapan1121 已提交
622
  do {
D
dapan1121 已提交
623
    QW_ERR_JRET(qwHandlePrePhaseEvents(QW_FPARAMS(), QW_PHASE_PRE_CQUERY, &input, NULL));
D
dapan1121 已提交
624

D
dapan1121 已提交
625
    QW_ERR_JRET(qwGetTaskCtx(QW_FPARAMS(), &ctx));
D
dapan1121 已提交
626

dengyihao's avatar
dengyihao 已提交
627 628
    atomic_store_8((int8_t *)&ctx->queryInQueue, 0);
    atomic_store_8((int8_t *)&ctx->queryContinue, 0);
D
dapan1121 已提交
629

630
    QW_ERR_JRET(qwExecTask(QW_FPARAMS(), ctx, &queryStop));
D
dapan1121 已提交
631

D
dapan1121 已提交
632
    if (QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
D
dapan1121 已提交
633
      SOutputData sOutput = {0};
D
dapan1121 已提交
634
      QW_ERR_JRET(qwGetQueryResFromSink(QW_FPARAMS(), ctx, &dataLen, &rsp, &sOutput));
dengyihao's avatar
dengyihao 已提交
635 636

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

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

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

D
dapan1121 已提交
645
        qwBuildFetchRsp(rsp, &sOutput, dataLen, qComplete);
D
dapan1121 已提交
646
        if (qComplete) {
dengyihao's avatar
dengyihao 已提交
647
          atomic_store_8((int8_t *)&ctx->queryEnd, true);
D
dapan1121 已提交
648
        }
H
Haojun Liao 已提交
649

D
dapan1121 已提交
650
        qwMsg->connInfo = ctx->dataConnInfo;
dengyihao's avatar
dengyihao 已提交
651 652
        QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_FETCH);

D
dapan1121 已提交
653
        qwBuildAndSendFetchRsp(ctx->fetchType, &qwMsg->connInfo, rsp, dataLen, code);
D
dapan1121 已提交
654
        rsp = NULL;
655

dengyihao's avatar
dengyihao 已提交
656 657
        QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code,
                     tstrerror(code), dataLen);
D
dapan1121 已提交
658
      } else {
dengyihao's avatar
dengyihao 已提交
659
        atomic_store_8((int8_t *)&ctx->queryContinue, 1);
660 661 662
      }
    }

dengyihao's avatar
dengyihao 已提交
663
  _return:
664

D
dapan1121 已提交
665 666 667 668
    if (NULL == ctx) {
      break;
    }

D
dapan1121 已提交
669
    if (code && QW_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
dengyihao's avatar
dengyihao 已提交
670
      QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_FETCH);
671 672
      qwFreeFetchRsp(rsp);
      rsp = NULL;
dengyihao's avatar
dengyihao 已提交
673

D
dapan1121 已提交
674
      qwMsg->connInfo = ctx->dataConnInfo;
D
dapan1121 已提交
675
      qwBuildAndSendFetchRsp(ctx->fetchType, &qwMsg->connInfo, NULL, 0, code);
dengyihao's avatar
dengyihao 已提交
676 677
      QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code, tstrerror(code),
                   0);
678
    }
D
dapan1121 已提交
679

D
dapan1121 已提交
680
    QW_LOCK(QW_WRITE, &ctx->lock);
681
    if (queryStop || code || 0 == atomic_load_8((int8_t *)&ctx->queryContinue)) {
D
dapan1121 已提交
682 683
      // Note: query is not running anymore
      QW_SET_PHASE(ctx, 0);
dengyihao's avatar
dengyihao 已提交
684
      QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
685 686
      break;
    }
dengyihao's avatar
dengyihao 已提交
687
    QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
688
  } while (true);
D
dapan1121 已提交
689

D
dapan1121 已提交
690
  input.code = code;
dengyihao's avatar
dengyihao 已提交
691
  qwHandlePostPhaseEvents(QW_FPARAMS(), QW_PHASE_POST_CQUERY, &input, NULL);
D
dapan1121 已提交
692

dengyihao's avatar
dengyihao 已提交
693
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
694
}
D
dapan1121 已提交
695

D
dapan1121 已提交
696
int32_t qwProcessFetch(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
dengyihao's avatar
dengyihao 已提交
697 698 699
  int32_t       code = 0;
  int32_t       dataLen = 0;
  bool          locked = false;
700 701
  SQWTaskCtx   *ctx = NULL;
  void         *rsp = NULL;
D
dapan1121 已提交
702
  SQWPhaseInput input = {0};
D
dapan1121 已提交
703

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

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

D
dapan1121 已提交
708
  ctx->msgType = qwMsg->msgType;
D
dapan1121 已提交
709
  ctx->dataConnInfo = qwMsg->connInfo;
D
dapan1121 已提交
710

D
dapan 已提交
711
  SOutputData sOutput = {0};
D
dapan1121 已提交
712
  QW_ERR_JRET(qwGetQueryResFromSink(QW_FPARAMS(), ctx, &dataLen, &rsp, &sOutput));
D
dapan1121 已提交
713

714 715
  if (NULL == rsp) {
    QW_SET_EVENT_RECEIVED(ctx, QW_EVENT_FETCH);
D
dapan1121 已提交
716
  } else {
D
dapan1121 已提交
717
    bool qComplete = (DS_BUF_EMPTY == sOutput.bufStatus && sOutput.queryEnd);
dengyihao's avatar
dengyihao 已提交
718

D
dapan1121 已提交
719
    qwBuildFetchRsp(rsp, &sOutput, dataLen, qComplete);
D
dapan1121 已提交
720
    if (qComplete) {
dengyihao's avatar
dengyihao 已提交
721
      atomic_store_8((int8_t *)&ctx->queryEnd, true);
D
dapan1121 已提交
722
    }
D
dapan1121 已提交
723 724
  }

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

D
dapan1121 已提交
728 729
    QW_LOCK(QW_WRITE, &ctx->lock);
    locked = true;
730

D
dapan1121 已提交
731
    // RC WARNING
D
dapan1121 已提交
732
    if (QW_QUERY_RUNNING(ctx)) {
dengyihao's avatar
dengyihao 已提交
733 734
      atomic_store_8((int8_t *)&ctx->queryContinue, 1);
    } else if (0 == atomic_load_8((int8_t *)&ctx->queryInQueue)) {
D
dapan1121 已提交
735
      qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_EXEC);
D
dapan1121 已提交
736

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

D
dapan1121 已提交
739
      QW_ERR_JRET(qwBuildAndSendCQueryMsg(QW_FPARAMS(), &qwMsg->connInfo));
740
    }
D
dapan 已提交
741
  }
dengyihao's avatar
dengyihao 已提交
742

D
dapan1121 已提交
743
_return:
D
dapan1121 已提交
744

D
dapan1121 已提交
745 746 747 748 749
  if (locked) {
    QW_UNLOCK(QW_WRITE, &ctx->lock);
  }

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

D
dapan 已提交
752 753 754
  if (code) {
    qwFreeFetchRsp(rsp);
    rsp = NULL;
D
dapan1121 已提交
755
    dataLen = 0;
D
dapan1121 已提交
756 757 758
  }

  if (code || rsp) {
D
dapan1121 已提交
759
    bool rsped = false;
D
dapan1121 已提交
760
    if (ctx) {
761
      qwDbgSimulateRedirect(qwMsg, ctx, &rsped);
D
dapan1121 已提交
762 763
      qwDbgSimulateDead(QW_FPARAMS(), ctx, &rsped);
    }
D
dapan1121 已提交
764 765
    if (!rsped) {
      qwBuildAndSendFetchRsp(qwMsg->msgType + 1, &qwMsg->connInfo, rsp, dataLen, code);
766 767
      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 已提交
768
    }
D
dapan1121 已提交
769 770
  }

D
dapan1121 已提交
771
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
772
}
D
dapan1121 已提交
773

D
dapan1121 已提交
774
int32_t qwProcessDrop(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
dengyihao's avatar
dengyihao 已提交
775
  int32_t     code = 0;
D
dapan1121 已提交
776
  bool        dropped = false;
D
dapan1121 已提交
777
  SQWTaskCtx *ctx = NULL;
dengyihao's avatar
dengyihao 已提交
778
  bool        locked = false;
D
dapan1121 已提交
779

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

D
dapan1121 已提交
782 783 784 785
  QW_LOCK(QW_WRITE, &ctx->lock);

  locked = true;

D
dapan1121 已提交
786
  if (QW_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
787
    QW_TASK_WLOG_E("task already dropping");
D
dapan1121 已提交
788 789 790
    QW_ERR_JRET(TSDB_CODE_QRY_DUPLICATTED_OPERATION);
  }

D
dapan1121 已提交
791
  if (QW_QUERY_RUNNING(ctx)) {
D
dapan1121 已提交
792
    QW_ERR_JRET(qwKillTaskHandle(ctx));
D
dapan1121 已提交
793
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_DROP);
D
dapan1121 已提交
794
  } else {
D
dapan1121 已提交
795
    QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
D
dapan1121 已提交
796
    dropped = true;
D
dapan1121 已提交
797
  }
D
dapan1121 已提交
798

D
dapan1121 已提交
799
  if (!dropped) {
D
dapan1121 已提交
800 801
    QW_SET_EVENT_RECEIVED(ctx, QW_EVENT_DROP);
  }
802

D
dapan1121 已提交
803
_return:
D
dapan1121 已提交
804

D
dapan1121 已提交
805
  if (code) {
D
dapan1121 已提交
806 807 808
    if (ctx) {
      QW_UPDATE_RSP_CODE(ctx, code);
    }
H
Haojun Liao 已提交
809

D
dapan1121 已提交
810
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_FAIL);
D
dapan1121 已提交
811 812
  }

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

D
dapan1121 已提交
817
  if (ctx) {
D
dapan1121 已提交
818
    qwReleaseTaskCtx(mgmt, ctx);
D
dapan1121 已提交
819 820
  }

D
dapan1121 已提交
821 822 823
  QW_RET(TSDB_CODE_SUCCESS);
}

D
dapan1121 已提交
824
int32_t qwProcessHb(SQWorker *mgmt, SQWMsg *qwMsg, SSchedulerHbReq *req) {
dengyihao's avatar
dengyihao 已提交
825
  int32_t         code = 0;
D
dapan1121 已提交
826
  SSchedulerHbRsp rsp = {0};
827
  SQWSchStatus   *sch = NULL;
D
dapan1121 已提交
828

D
dapan1121 已提交
829 830 831
  if (qwMsg->code) {
    QW_RET(qwProcessHbLinkBroken(mgmt, qwMsg, req));
  }
D
dapan1121 已提交
832 833

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

D
dapan1121 已提交
836 837
  sch->hbBrokenTs = 0;

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

D
dapan1121 已提交
840
  if (sch->hbConnInfo.handle) {
841
    tmsgReleaseHandle(&sch->hbConnInfo, TAOS_CONN_SERVER);
S
Shengliang Guan 已提交
842
    sch->hbConnInfo.handle = NULL;
D
dapan1121 已提交
843
  }
D
dapan1121 已提交
844

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

D
dapan1121 已提交
848
  QW_UNLOCK(QW_WRITE, &sch->hbConnLock);
dengyihao's avatar
dengyihao 已提交
849 850 851

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

D
dapan1121 已提交
853 854 855 856
  qwReleaseScheduler(QW_READ, mgmt);

_return:

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

S
shm  
Shengliang Guan 已提交
859
  qwBuildAndSendHbRsp(&qwMsg->connInfo, &rsp, code);
D
dapan1121 已提交
860 861

  if (code) {
862
    tmsgReleaseHandle(&qwMsg->connInfo, TAOS_CONN_SERVER);
S
Shengliang Guan 已提交
863
    qwMsg->connInfo.handle = NULL;
D
dapan1121 已提交
864
  }
dengyihao's avatar
dengyihao 已提交
865

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

D
dapan1121 已提交
868
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
869 870 871
}

void qwProcessHbTimerEvent(void *param, void *tmrId) {
872
  SQWHbParam *hbParam = (SQWHbParam *)param;
D
dapan1121 已提交
873 874 875 876
  if (hbParam->qwrId != atomic_load_32(&gQwMgmt.qwRef)) {
    return;
  }

877
  int64_t   refId = hbParam->refId;
D
dapan1121 已提交
878 879 880 881 882
  SQWorker *mgmt = qwAcquire(refId);
  if (NULL == mgmt) {
    QW_DLOG("qwAcquire %" PRIx64 "failed", refId);
    return;
  }
883

D
dapan1121 已提交
884
  SQWSchStatus *sch = NULL;
dengyihao's avatar
dengyihao 已提交
885
  int32_t       taskNum = 0;
886
  SQWHbInfo    *rspList = NULL;
D
dapan1121 已提交
887
  SArray       *pExpiredSch = NULL;
dengyihao's avatar
dengyihao 已提交
888
  int32_t       code = 0;
D
dapan1121 已提交
889

D
dapan1121 已提交
890 891
  qwDbgDumpMgmtInfo(mgmt);

D
dapan1121 已提交
892 893 894 895 896
  QW_LOCK(QW_READ, &mgmt->schLock);

  int32_t schNum = taosHashGetSize(mgmt->schHash);
  if (schNum <= 0) {
    QW_UNLOCK(QW_READ, &mgmt->schLock);
D
dapan1121 已提交
897
    taosTmrReset(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, param, mgmt->timer, &mgmt->hbTimer);
D
dapan1121 已提交
898
    qwRelease(refId);
D
dapan1121 已提交
899
    return;
D
dapan1121 已提交
900 901
  }

wafwerar's avatar
wafwerar 已提交
902
  rspList = taosMemoryCalloc(schNum, sizeof(SQWHbInfo));
D
dapan1121 已提交
903 904
  pExpiredSch = taosArrayInit(schNum, sizeof(uint64_t));
  if (NULL == rspList || NULL == pExpiredSch) {
D
dapan1121 已提交
905
    QW_UNLOCK(QW_READ, &mgmt->schLock);
D
dapan1121 已提交
906 907
    taosMemoryFree(rspList);
    taosArrayDestroy(pExpiredSch);
D
dapan1121 已提交
908 909
    QW_ELOG("calloc %d SQWHbInfo failed", schNum);
    taosTmrReset(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, param, mgmt->timer, &mgmt->hbTimer);
D
dapan1121 已提交
910
    qwRelease(refId);
D
dapan1121 已提交
911
    return;
D
dapan1121 已提交
912 913
  }

914
  void   *key = NULL;
dengyihao's avatar
dengyihao 已提交
915
  size_t  keyLen = 0;
D
dapan1121 已提交
916
  int32_t i = 0;
D
dapan1121 已提交
917
  int64_t currentMs = taosGetTimestampMs();
D
dapan1121 已提交
918 919 920

  void *pIter = taosHashIterate(mgmt->schHash, NULL);
  while (pIter) {
D
dapan1121 已提交
921 922 923
    SQWSchStatus *sch = (SQWSchStatus *)pIter;
    if (NULL == sch->hbConnInfo.handle) {
      uint64_t *sId = taosHashGetKey(pIter, NULL);
D
dapan1121 已提交
924
      QW_TLOG("cancel send hb to sch %" PRIx64 " cause of no connection handle", *sId);
D
dapan1121 已提交
925

926 927
      if (sch->hbBrokenTs > 0 && ((currentMs - sch->hbBrokenTs) > QW_SCH_TIMEOUT_MSEC) &&
          taosHashGetSize(sch->tasksHash) <= 0) {
D
dapan1121 已提交
928 929
        taosArrayPush(pExpiredSch, sId);
      }
930

D
dapan1121 已提交
931 932 933
      pIter = taosHashIterate(mgmt->schHash, pIter);
      continue;
    }
dengyihao's avatar
dengyihao 已提交
934

D
dapan1121 已提交
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
    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 已提交
950
    qwBuildAndSendHbRsp(&rspList[j].connInfo, &rspList[j].rsp, code);
951 952
    /*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 已提交
953
    tFreeSSchedulerHbRsp(&rspList[j].rsp);
D
dapan1121 已提交
954 955
  }

D
dapan1121 已提交
956
  if (taosArrayGetSize(pExpiredSch) > 0) {
D
dapan1121 已提交
957
    qwClearExpiredSch(mgmt, pExpiredSch);
D
dapan1121 已提交
958 959
  }

wafwerar's avatar
wafwerar 已提交
960
  taosMemoryFreeClear(rspList);
D
dapan1121 已提交
961
  taosArrayDestroy(pExpiredSch);
D
dapan1121 已提交
962

dengyihao's avatar
dengyihao 已提交
963
  taosTmrReset(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, param, mgmt->timer, &mgmt->hbTimer);
964
  qwRelease(refId);
D
dapan1121 已提交
965 966
}

967
int32_t qwProcessDelete(QW_FPARAMS_DEF, SQWMsg *qwMsg, SDeleteRes *pRes) {
D
dapan1121 已提交
968 969 970 971 972 973 974 975 976 977 978 979 980
  int32_t        code = 0;
  SSubplan      *plan = NULL;
  qTaskInfo_t    pTaskInfo = NULL;
  DataSinkHandle sinkHandle = NULL;
  SQWTaskCtx     ctx = {0};

  code = qStringToSubplan(qwMsg->msg, &plan);
  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);
  }

981
  code = qCreateExecTask(qwMsg->node, mgmt->nodeId, tId, plan, &pTaskInfo, &sinkHandle, NULL, OPTR_EXEC_MODEL_BATCH);
D
dapan1121 已提交
982 983 984 985 986 987 988 989 990 991
  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 已提交
992 993
  ctx.taskHandle = pTaskInfo;
  ctx.sinkHandle = sinkHandle;
D
dapan1121 已提交
994

D
dapan1121 已提交
995
  QW_ERR_JRET(qwExecTask(QW_FPARAMS(), &ctx, NULL));
D
dapan1121 已提交
996

997
  QW_ERR_JRET(qwGetDeleteResFromSink(QW_FPARAMS(), &ctx, pRes));
D
dapan1121 已提交
998 999 1000

_return:

D
dapan1121 已提交
1001
  qwFreeTaskCtx(&ctx);
D
dapan1121 已提交
1002 1003 1004 1005

  QW_RET(TSDB_CODE_SUCCESS);
}

S
Shengliang Guan 已提交
1006
int32_t qWorkerInit(int8_t nodeType, int32_t nodeId, SQWorkerCfg *cfg, void **qWorkerMgmt, const SMsgCb *pMsgCb) {
1007
  if (NULL == qWorkerMgmt || pMsgCb->mgmt == NULL) {
D
dapan1121 已提交
1008 1009 1010
    qError("invalid param to init qworker");
    QW_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }
S
Shengliang 已提交
1011

D
dapan1121 已提交
1012 1013 1014 1015
  int32_t qwNum = atomic_add_fetch_32(&gQwMgmt.qwNum, 1);
  if (1 == qwNum) {
    memset(gQwMgmt.param, 0, sizeof(gQwMgmt.param));
  }
D
dapan1121 已提交
1016 1017 1018 1019 1020 1021 1022 1023

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

  SQWorker *mgmt = taosMemoryCalloc(1, sizeof(SQWorker));
D
dapan1121 已提交
1024
  if (NULL == mgmt) {
D
dapan1121 已提交
1025 1026
    qError("calloc %d failed", (int32_t)sizeof(SQWorker));
    atomic_sub_fetch_32(&gQwMgmt.qwNum, 1);
D
dapan1121 已提交
1027
    QW_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
1028 1029 1030 1031
  }

  if (cfg) {
    mgmt->cfg = *cfg;
D
dapan1121 已提交
1032
    if (0 == mgmt->cfg.maxSchedulerNum) {
D
dapan1121 已提交
1033
      mgmt->cfg.maxSchedulerNum = QW_DEFAULT_SCHEDULER_NUMBER;
D
dapan1121 已提交
1034 1035
    }
    if (0 == mgmt->cfg.maxTaskNum) {
D
dapan1121 已提交
1036
      mgmt->cfg.maxTaskNum = QW_DEFAULT_TASK_NUMBER;
D
dapan1121 已提交
1037 1038
    }
    if (0 == mgmt->cfg.maxSchTaskNum) {
D
dapan1121 已提交
1039
      mgmt->cfg.maxSchTaskNum = QW_DEFAULT_SCH_TASK_NUMBER;
D
dapan1121 已提交
1040
    }
D
dapan1121 已提交
1041
  } else {
D
dapan1121 已提交
1042 1043 1044
    mgmt->cfg.maxSchedulerNum = QW_DEFAULT_SCHEDULER_NUMBER;
    mgmt->cfg.maxTaskNum = QW_DEFAULT_TASK_NUMBER;
    mgmt->cfg.maxSchTaskNum = QW_DEFAULT_SCH_TASK_NUMBER;
D
dapan1121 已提交
1045 1046
  }

dengyihao's avatar
dengyihao 已提交
1047 1048
  mgmt->schHash = taosHashInit(mgmt->cfg.maxSchedulerNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false,
                               HASH_ENTRY_LOCK);
D
dapan1121 已提交
1049
  if (NULL == mgmt->schHash) {
wafwerar's avatar
wafwerar 已提交
1050
    taosMemoryFreeClear(mgmt);
D
dapan1121 已提交
1051
    qError("init %d scheduler hash failed", mgmt->cfg.maxSchedulerNum);
D
dapan1121 已提交
1052
    QW_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
1053 1054
  }

dengyihao's avatar
dengyihao 已提交
1055 1056
  mgmt->ctxHash =
      taosHashInit(mgmt->cfg.maxTaskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
D
dapan1121 已提交
1057
  if (NULL == mgmt->ctxHash) {
D
dapan1121 已提交
1058
    qError("init %d task ctx hash failed", mgmt->cfg.maxTaskNum);
D
dapan1121 已提交
1059 1060 1061 1062 1063 1064 1065 1066 1067
    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 已提交
1068 1069
  mgmt->nodeType = nodeType;
  mgmt->nodeId = nodeId;
S
Shengliang Guan 已提交
1070
  mgmt->msgCb = *pMsgCb;
D
dapan1121 已提交
1071

D
dapan1121 已提交
1072 1073 1074 1075 1076 1077
  mgmt->refId = taosAddRef(gQwMgmt.qwRef, mgmt);
  if (mgmt->refId < 0) {
    qError("taosAddRef qw failed, error:%s", tstrerror(terrno));
    QW_ERR_JRET(terrno);
  }

D
dapan1121 已提交
1078 1079 1080
  SQWHbParam *param = NULL;
  qwSetHbParam(mgmt->refId, &param);

1081
  mgmt->hbTimer = taosTmrStart(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, (void *)param, mgmt->timer);
D
dapan1121 已提交
1082 1083 1084 1085
  if (NULL == mgmt->hbTimer) {
    qError("start hb timer failed");
    QW_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
1086

D
dapan1121 已提交
1087 1088
  *qWorkerMgmt = mgmt;

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

D
dapan1121 已提交
1091
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1092 1093 1094

_return:

D
dapan1121 已提交
1095 1096 1097 1098 1099 1100 1101
  if (mgmt->refId >= 0) {
    qwRelease(mgmt->refId);
  } else {
    taosHashCleanup(mgmt->schHash);
    taosHashCleanup(mgmt->ctxHash);
    taosTmrCleanUp(mgmt->timer);
    taosMemoryFreeClear(mgmt);
D
dapan1121 已提交
1102

1103
    atomic_sub_fetch_32(&gQwMgmt.qwNum, 1);
D
dapan1121 已提交
1104
  }
1105

D
dapan1121 已提交
1106
  QW_RET(code);
D
dapan1121 已提交
1107
}
D
dapan1121 已提交
1108 1109 1110 1111

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

D
dapan1121 已提交
1114
  SQWorker *mgmt = *qWorkerMgmt;
D
dapan1121 已提交
1115

D
dapan1121 已提交
1116 1117 1118
  if (taosRemoveRef(gQwMgmt.qwRef, mgmt->refId)) {
    qError("remove qw from ref list failed, refId:%" PRIx64, mgmt->refId);
  }
D
dapan1121 已提交
1119
}
D
dapan1121 已提交
1120

D
dapan1121 已提交
1121 1122 1123 1124 1125
int32_t qWorkerGetStat(SReadHandle *handle, void *qWorkerMgmt, SQWorkerStat *pStat) {
  if (NULL == handle || NULL == qWorkerMgmt || NULL == pStat) {
    QW_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

1126
  SQWorker     *mgmt = (SQWorker *)qWorkerMgmt;
D
dapan1121 已提交
1127
  SDataSinkStat sinkStat = {0};
1128

D
dapan1121 已提交
1129 1130
  dsDataSinkGetCacheSize(&sinkStat);
  pStat->cacheDataSize = sinkStat.cachedSize;
1131

D
dapan1121 已提交
1132 1133 1134 1135 1136
  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 已提交
1137
  pStat->deleteProcessed = QW_STAT_GET(mgmt->stat.msgStat.deleteProcessed);
D
dapan1121 已提交
1138 1139 1140 1141 1142 1143 1144

  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 已提交
1145
}