qworker.c 31.6 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1
#include "dataSinkMgt.h"
2
#include "executor.h"
D
dapan1121 已提交
3
#include "planner.h"
H
Haojun Liao 已提交
4
#include "query.h"
D
dapan1121 已提交
5 6
#include "qwInt.h"
#include "qwMsg.h"
dengyihao's avatar
dengyihao 已提交
7
#include "tcommon.h"
H
Haojun Liao 已提交
8
#include "tmsg.h"
9
#include "tname.h"
D
dapan1121 已提交
10
#include "qworker.h"
D
dapan1121 已提交
11

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

dengyihao's avatar
dengyihao 已提交
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 29
  sch->hbBrokenTs = taosGetTimestampMs();
  
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
}

D
dapan1121 已提交
47
int32_t qwHandleTaskComplete(QW_FPARAMS_DEF, SQWTaskCtx *ctx) {
D
dapan1121 已提交
48
  qTaskInfo_t taskHandle = ctx->taskHandle;
D
dapan1121 已提交
49

D
dapan1121 已提交
50
  if (TASK_TYPE_TEMP == ctx->taskType && taskHandle) {
D
dapan1121 已提交
51 52
    if (ctx->explain) {
      SExplainExecInfo *execInfo = NULL;
dengyihao's avatar
dengyihao 已提交
53
      int32_t           resNum = 0;
D
dapan1121 已提交
54
      QW_ERR_RET(qGetExplainExecInfo(taskHandle, &resNum, &execInfo));
55 56 57 58

      SRpcHandleInfo connInfo = ctx->ctrlConnInfo;
      connInfo.ahandle = NULL;
      QW_ERR_RET(qwBuildAndSendExplainRsp(&connInfo, execInfo, resNum));
D
dapan1121 已提交
59 60 61 62 63 64
    }
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
65
int32_t qwExecTask(QW_FPARAMS_DEF, SQWTaskCtx *ctx, bool *queryEnd) {
dengyihao's avatar
dengyihao 已提交
66 67
  int32_t        code = 0;
  bool           qcontinue = true;
68
  SSDataBlock   *pRes = NULL;
dengyihao's avatar
dengyihao 已提交
69 70 71
  uint64_t       useconds = 0;
  int32_t        i = 0;
  int32_t        execNum = 0;
D
dapan1121 已提交
72
  qTaskInfo_t    taskHandle = ctx->taskHandle;
D
dapan1121 已提交
73
  DataSinkHandle sinkHandle = ctx->sinkHandle;
dengyihao's avatar
dengyihao 已提交
74

D
dapan1121 已提交
75
  while (true) {
H
Haojun Liao 已提交
76
    QW_TASK_DLOG("start to execTask, loopIdx:%d", i++);
D
dapan1121 已提交
77

D
dapan1121 已提交
78 79 80 81 82 83
    pRes = NULL;

    // if *taskHandle is NULL, it's killed right now
    if (taskHandle) {
      code = qExecTask(taskHandle, &pRes, &useconds);
      if (code) {
84 85 86 87 88
        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));
        }
D
dapan1121 已提交
89 90
        QW_ERR_RET(code);
      }
D
dapan1121 已提交
91 92
    }

D
dapan1121 已提交
93 94
    ++execNum;

D
dapan1121 已提交
95
    if (NULL == pRes) {
dengyihao's avatar
dengyihao 已提交
96
      QW_TASK_DLOG("qExecTask end with empty res, useconds:%" PRIu64, useconds);
H
Haojun Liao 已提交
97

D
dapan1121 已提交
98
      dsEndPut(sinkHandle, useconds);
D
dapan1121 已提交
99 100

      QW_ERR_RET(qwHandleTaskComplete(QW_FPARAMS(), ctx));
D
dapan1121 已提交
101 102 103 104

      if (queryEnd) {
        *queryEnd = true;
      }
dengyihao's avatar
dengyihao 已提交
105

D
dapan1121 已提交
106 107 108
      break;
    }

D
dapan1121 已提交
109
    int32_t rows = pRes->info.rows;
H
Haojun Liao 已提交
110

111 112
    ASSERT(pRes->info.rows > 0);

H
Haojun Liao 已提交
113
    SInputData inputData = {.pData = pRes};
D
dapan1121 已提交
114 115
    code = dsPutDataBlock(sinkHandle, &inputData, &qcontinue);
    if (code) {
D
dapan1121 已提交
116 117
      QW_TASK_ELOG("dsPutDataBlock failed, code:%x - %s", code, tstrerror(code));
      QW_ERR_RET(code);
D
dapan1121 已提交
118
    }
D
dapan1121 已提交
119

D
dapan1121 已提交
120
    QW_TASK_DLOG("data put into sink, rows:%d, continueExecTask:%d", rows, qcontinue);
dengyihao's avatar
dengyihao 已提交
121

D
dapan1121 已提交
122 123 124 125
    if (!qcontinue) {
      break;
    }

D
dapan1121 已提交
126 127 128 129 130
    if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_READY) && execNum >= QW_DEFAULT_SHORT_RUN_TIMES) {
      break;
    }

    if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
D
dapan1121 已提交
131 132
      break;
    }
D
dapan1121 已提交
133

D
dapan1121 已提交
134 135 136
    if (atomic_load_32(&ctx->rspCode)) {
      break;
    }
D
dapan1121 已提交
137 138 139 140
  }

  QW_RET(code);
}
D
dapan1121 已提交
141

D
dapan1121 已提交
142
int32_t qwGenerateSchHbRsp(SQWorker *mgmt, SQWSchStatus *sch, SQWHbInfo *hbInfo) {
D
dapan1121 已提交
143 144
  int32_t taskNum = 0;

D
dapan1121 已提交
145
  hbInfo->connInfo = sch->hbConnInfo;
D
dapan1121 已提交
146
  hbInfo->rsp.epId = sch->hbEpId;
D
dapan1121 已提交
147

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

D
dapan1121 已提交
150
  taskNum = taosHashGetSize(sch->tasksHash);
D
dapan1121 已提交
151 152 153

  hbInfo->rsp.taskStatus = taosArrayInit(taskNum, sizeof(STaskStatus));
  if (NULL == hbInfo->rsp.taskStatus) {
D
dapan1121 已提交
154
    QW_UNLOCK(QW_READ, &sch->tasksLock);
D
dapan1121 已提交
155
    QW_ELOG("taosArrayInit taskStatus failed, num:%d", taskNum);
D
dapan1121 已提交
156 157 158
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }

159
  void       *key = NULL;
dengyihao's avatar
dengyihao 已提交
160 161
  size_t      keyLen = 0;
  int32_t     i = 0;
D
dapan1121 已提交
162
  STaskStatus status = {0};
D
dapan1121 已提交
163 164 165 166

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

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

D
dapan1121 已提交
171 172 173
    QW_GET_QTID(key, status.queryId, status.taskId);
    status.status = taskStatus->status;
    status.refId = taskStatus->refId;
dengyihao's avatar
dengyihao 已提交
174

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

D
dapan1121 已提交
177 178
    ++i;
    pIter = taosHashIterate(sch->tasksHash, pIter);
dengyihao's avatar
dengyihao 已提交
179
  }
D
dapan1121 已提交
180 181 182 183 184 185

  QW_UNLOCK(QW_READ, &sch->tasksLock);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
186
int32_t qwGetQueryResFromSink(QW_FPARAMS_DEF, SQWTaskCtx *ctx, int32_t *dataLen, void **rspMsg, SOutputData *pOutput) {
dengyihao's avatar
dengyihao 已提交
187
  int32_t            len = 0;
D
dapan1121 已提交
188
  SRetrieveTableRsp *rsp = NULL;
dengyihao's avatar
dengyihao 已提交
189 190
  bool               queryEnd = false;
  int32_t            code = 0;
D
dapan1121 已提交
191

D
dapan1121 已提交
192
  dsGetDataLength(ctx->sinkHandle, &len, &queryEnd);
D
dapan1121 已提交
193

D
dapan1121 已提交
194 195 196 197
  if (len < 0) {
    QW_TASK_ELOG("invalid length from dsGetDataLength, length:%d", len);
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }
D
dapan1121 已提交
198

D
dapan1121 已提交
199 200
  if (len == 0) {
    if (queryEnd) {
D
dapan 已提交
201
      code = dsGetDataBlock(ctx->sinkHandle, pOutput);
D
dapan1121 已提交
202
      if (code) {
D
dapan1121 已提交
203
        QW_TASK_ELOG("dsGetDataBlock failed, code:%x - %s", code, tstrerror(code));
D
dapan1121 已提交
204 205
        QW_ERR_RET(code);
      }
dengyihao's avatar
dengyihao 已提交
206

D
dapan1121 已提交
207
      QW_TASK_DLOG_E("no data in sink and query end");
H
Haojun Liao 已提交
208

D
dapan1121 已提交
209
      qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_SUCCEED);
L
Liu Jicong 已提交
210
      QW_ERR_RET(qwMallocFetchRsp(len, &rsp));
211

D
dapan1121 已提交
212
      *rspMsg = rsp;
D
dapan 已提交
213
      *dataLen = 0;
D
dapan1121 已提交
214 215
      return TSDB_CODE_SUCCESS;
    }
D
dapan1121 已提交
216 217

    pOutput->bufStatus = DS_BUF_EMPTY;
D
dapan1121 已提交
218

D
dapan1121 已提交
219
    return TSDB_CODE_SUCCESS;
L
Liu Jicong 已提交
220
  }
D
dapan1121 已提交
221

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

D
dapan 已提交
225
  *dataLen = len;
dengyihao's avatar
dengyihao 已提交
226

D
dapan1121 已提交
227
  QW_ERR_RET(qwMallocFetchRsp(len, &rsp));
D
dapan 已提交
228
  *rspMsg = rsp;
dengyihao's avatar
dengyihao 已提交
229

D
dapan 已提交
230 231
  pOutput->pData = rsp->data;
  code = dsGetDataBlock(ctx->sinkHandle, pOutput);
D
dapan1121 已提交
232
  if (code) {
D
dapan1121 已提交
233
    QW_TASK_ELOG("dsGetDataBlock failed, code:%x - %s", code, tstrerror(code));
D
dapan1121 已提交
234 235
    QW_ERR_RET(code);
  }
D
dapan1121 已提交
236

D
dapan1121 已提交
237
  if (DS_BUF_EMPTY == pOutput->bufStatus && pOutput->queryEnd) {
D
dapan1121 已提交
238
    QW_TASK_DLOG_E("task all data fetched, done");
D
dapan1121 已提交
239
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_SUCCEED);
D
dapan1121 已提交
240 241
  }

D
dapan1121 已提交
242
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
243 244
}

D
dapan1121 已提交
245
int32_t qwGetDeleteResFromSink(QW_FPARAMS_DEF, SQWTaskCtx *ctx, int32_t *dataLen, void **rspMsg, SDeleteRes *pRes) {
D
dapan1121 已提交
246 247 248 249 250 251 252 253
  int32_t            len = 0;
  SVDeleteRsp        rsp = {0};
  bool               queryEnd = false;
  int32_t            code = 0;
  SOutputData        output = {0};

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

D
dapan1121 已提交
254
  if (len <= 0 || len != sizeof(SDeleterRes)) {
D
dapan1121 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    QW_TASK_ELOG("invalid length from dsGetDataLength, length:%d", len);
    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);
  }
  
  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);
  }

D
dapan1121 已提交
271 272 273
  SDeleterRes* pDelRes = (SDeleterRes*)output.pData;
  
  rsp.affectedRows = pDelRes->affectedRows;
D
dapan1121 已提交
274
  pRes->suid = pDelRes->suid;
D
dapan1121 已提交
275 276 277
  pRes->uidList = pDelRes->uidList;
  pRes->skey = pDelRes->skey;
  pRes->ekey = pDelRes->ekey;
D
dapan1121 已提交
278 279

  SEncoder coder = {0};  
D
dapan1121 已提交
280 281
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, len, code);
  void *msg = rpcMallocCont(len);
D
dapan1121 已提交
282 283 284 285 286 287 288 289 290 291
  tEncoderInit(&coder, msg, len);
  tEncodeSVDeleteRsp(&coder, &rsp);
  tEncoderClear(&coder);

  *rspMsg = msg;
  *dataLen = len;
  
  return TSDB_CODE_SUCCESS;
}

292

D
dapan1121 已提交
293
int32_t qwHandlePrePhaseEvents(QW_FPARAMS_DEF, int8_t phase, SQWPhaseInput *input, SQWPhaseOutput *output) {
294 295
  int32_t         code = 0;
  SQWTaskCtx     *ctx = NULL;
S
Shengliang Guan 已提交
296
  SRpcHandleInfo *cancelConnection = NULL;
D
dapan1121 已提交
297

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

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

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

D
dapan1121 已提交
304
  if (QW_PHASE_PRE_FETCH == phase) {
dengyihao's avatar
dengyihao 已提交
305
    atomic_store_8((int8_t *)&ctx->queryFetched, true);
D
dapan1121 已提交
306
  } else {
D
dapan1121 已提交
307 308
    atomic_store_8(&ctx->phase, phase);
  }
D
dapan1121 已提交
309

dengyihao's avatar
dengyihao 已提交
310
  if (atomic_load_8((int8_t *)&ctx->queryEnd)) {
D
dapan1121 已提交
311 312 313
    QW_TASK_ELOG_E("query already end");
    QW_ERR_JRET(TSDB_CODE_QW_MSG_ERROR);
  }
D
dapan1121 已提交
314

D
dapan1121 已提交
315 316
  switch (phase) {
    case QW_PHASE_PRE_QUERY: {
D
dapan1121 已提交
317
      if (QW_IS_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
H
Haojun Liao 已提交
318
        QW_TASK_ELOG("task already dropped at wrong phase %s", qwPhaseStr(phase));
D
dapan1121 已提交
319
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_STATUS_ERROR);
D
dapan1121 已提交
320 321
        break;
      }
D
dapan1121 已提交
322

D
dapan1121 已提交
323
      if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
324
        QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
325

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

D
dapan1121 已提交
329
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
D
dapan 已提交
330
        break;
D
dapan1121 已提交
331
      }
D
dapan1121 已提交
332

D
dapan1121 已提交
333
      QW_ERR_JRET(qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_EXECUTING));
D
dapan1121 已提交
334 335
      break;
    }
D
dapan1121 已提交
336
    case QW_PHASE_PRE_FETCH: {
D
dapan1121 已提交
337 338
      if (QW_IS_EVENT_PROCESSED(ctx, QW_EVENT_DROP) || QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
        QW_TASK_WLOG("task dropping or already dropped, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
339 340
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
      }
D
dapan1121 已提交
341

D
dapan1121 已提交
342
      if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
D
dapan1121 已提交
343
        QW_TASK_WLOG("last fetch still not processed, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
344 345 346 347
        QW_ERR_JRET(TSDB_CODE_QRY_DUPLICATTED_OPERATION);
      }

      if (!QW_IS_EVENT_PROCESSED(ctx, QW_EVENT_READY)) {
D
dapan1121 已提交
348
        QW_TASK_ELOG("ready msg has not been processed, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
349
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_MSG_ERROR);
D
dapan1121 已提交
350 351
      }
      break;
dengyihao's avatar
dengyihao 已提交
352
    }
D
dapan1121 已提交
353 354
    case QW_PHASE_PRE_CQUERY: {
      if (QW_IS_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
355
        QW_TASK_WLOG("task already dropped, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
356
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
D
dapan1121 已提交
357
      }
D
dapan1121 已提交
358

D
dapan1121 已提交
359
      if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
360
        QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
H
Haojun Liao 已提交
361

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

D
dapan1121 已提交
365
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
D
dapan1121 已提交
366
      }
D
dapan1121 已提交
367

D
dapan1121 已提交
368
      break;
D
dapan1121 已提交
369 370 371 372 373 374 375
    }
    default:
      QW_TASK_ELOG("invalid phase %s", qwPhaseStr(phase));
      QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  }

  if (ctx->rspCode) {
376
    QW_TASK_ELOG("task already failed at phase %s, code:%s", qwPhaseStr(phase), tstrerror(ctx->rspCode));
D
dapan1121 已提交
377
    QW_ERR_JRET(ctx->rspCode);
D
dapan1121 已提交
378
  }
D
dapan1121 已提交
379

D
dapan1121 已提交
380 381
_return:
  if (ctx) {
D
dapan1121 已提交
382
    QW_UPDATE_RSP_CODE(ctx, code);
dengyihao's avatar
dengyihao 已提交
383

D
dapan1121 已提交
384
    QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
385 386
    qwReleaseTaskCtx(mgmt, ctx);
  }
D
dapan1121 已提交
387

D
dapan1121 已提交
388
  if (cancelConnection) {
S
shm  
Shengliang Guan 已提交
389
    qwBuildAndSendCancelRsp(cancelConnection, code);
D
dapan1121 已提交
390
    QW_TASK_DLOG("cancel rsp send, handle:%p, code:%x - %s", cancelConnection->handle, code, tstrerror(code));
D
dapan1121 已提交
391 392
  }

393 394 395 396 397
  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 已提交
398 399 400 401 402

  QW_RET(code);
}

int32_t qwHandlePostPhaseEvents(QW_FPARAMS_DEF, int8_t phase, SQWPhaseInput *input, SQWPhaseOutput *output) {
S
Shengliang Guan 已提交
403 404 405
  int32_t         code = 0;
  SQWTaskCtx     *ctx = NULL;
  SRpcHandleInfo  connInfo = {0};
D
dapan1121 已提交
406
  SRpcHandleInfo *rspConnection = NULL;
D
dapan1121 已提交
407

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

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

D
dapan1121 已提交
412 413 414
  QW_LOCK(QW_WRITE, &ctx->lock);

  if (QW_IS_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
415
    QW_TASK_WLOG("task already dropped, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
416 417 418 419
    QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
  }

  if (QW_PHASE_POST_QUERY == phase) {
D
dapan1121 已提交
420
#if 0    
D
dapan1121 已提交
421
    if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_READY)) {
D
dapan1121 已提交
422
      readyConnection = &ctx->connInfo;
D
dapan1121 已提交
423 424
      QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_READY);
    }
D
dapan1121 已提交
425
#else
426
    connInfo = ctx->ctrlConnInfo;
D
dapan1121 已提交
427
    rspConnection = &connInfo;
dengyihao's avatar
dengyihao 已提交
428

D
dapan1121 已提交
429 430
    QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_READY);
#endif
D
dapan1121 已提交
431 432
  }

D
dapan1121 已提交
433
  if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
434 435 436 437
    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 已提交
438

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

D
dapan1121 已提交
442 443
    QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
    QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
D
dapan1121 已提交
444 445 446
  }

  if (ctx->rspCode) {
dengyihao's avatar
dengyihao 已提交
447 448
    QW_TASK_ELOG("task already failed, phase %s, error:%x - %s", qwPhaseStr(phase), ctx->rspCode,
                 tstrerror(ctx->rspCode));
D
dapan1121 已提交
449
    QW_ERR_JRET(ctx->rspCode);
dengyihao's avatar
dengyihao 已提交
450
  }
D
dapan1121 已提交
451

D
dapan1121 已提交
452
  QW_ERR_JRET(input->code);
D
dapan1121 已提交
453 454 455

_return:

D
dapan1121 已提交
456 457 458 459
  if (TSDB_CODE_SUCCESS == code && QW_PHASE_POST_QUERY == phase) {
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_PARTIAL_SUCCEED);
  }

D
dapan1121 已提交
460 461 462
  if (rspConnection) {
    qwBuildAndSendQueryRsp(rspConnection, code, ctx ? &ctx->tbInfo : NULL);
    QW_TASK_DLOG("ready msg rsped, handle:%p, code:%x - %s", rspConnection->handle, code, tstrerror(code));
463 464
  }

D
dapan1121 已提交
465
  if (ctx) {
D
dapan1121 已提交
466
    QW_UPDATE_RSP_CODE(ctx, code);
D
dapan1121 已提交
467

D
dapan1121 已提交
468 469 470
    if (QW_PHASE_POST_FETCH != phase) {
      atomic_store_8(&ctx->phase, phase);
    }
dengyihao's avatar
dengyihao 已提交
471

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

D
dapan1121 已提交
476 477
  if (code) {
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_FAILED);
D
dapan1121 已提交
478 479
  }

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

D
dapan1121 已提交
482 483 484
  QW_RET(code);
}

D
dapan1121 已提交
485 486 487 488 489 490 491
int32_t qwAbortPrerocessQuery(QW_FPARAMS_DEF) {
  QW_ERR_RET(qwDropTask(QW_FPARAMS()));

  QW_RET(TSDB_CODE_SUCCESS);
}


D
dapan1121 已提交
492
int32_t qwPrerocessQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
493 494 495 496 497 498 499
  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 已提交
500

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

D
dapan1121 已提交
503 504
  QW_ERR_JRET(qwAddAcquireTaskCtx(QW_FPARAMS(), &ctx));

D
dapan1121 已提交
505 506
  ctx->ctrlConnInfo = qwMsg->connInfo;

D
dapan1121 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519
  QW_ERR_JRET(qwAddTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_NOT_START));

_return:

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

  QW_RET(TSDB_CODE_SUCCESS);
}


520
int32_t qwProcessQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg, int8_t taskType, int8_t explain, const char* sql) {
D
dapan1121 已提交
521 522 523 524 525 526 527 528
  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 已提交
529
  QW_ERR_JRET(qwHandlePrePhaseEvents(QW_FPARAMS(), QW_PHASE_PRE_QUERY, &input, NULL));
D
dapan1121 已提交
530 531

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

D
dapan1121 已提交
533
  atomic_store_8(&ctx->taskType, taskType);
D
dapan1121 已提交
534
  atomic_store_8(&ctx->explain, explain);
X
Xiaoyu Wang 已提交
535

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

D
dapan1121 已提交
538 539
  code = qStringToSubplan(qwMsg->msg, &plan);
  if (TSDB_CODE_SUCCESS != code) {
540 541
    code = TSDB_CODE_INVALID_MSG;
    QW_TASK_ELOG("task physical plan to subplan failed, code:%x - %s", code, tstrerror(code));
D
dapan1121 已提交
542
    QW_ERR_JRET(code);
D
dapan1121 已提交
543
  }
dengyihao's avatar
dengyihao 已提交
544

D
dapan1121 已提交
545 546
  ctx->plan = plan;

547
  code = qCreateExecTask(qwMsg->node, mgmt->nodeId, tId, plan, &pTaskInfo, &sinkHandle, sql, OPTR_EXEC_MODEL_BATCH);
D
dapan1121 已提交
548
  if (code) {
D
dapan1121 已提交
549
    QW_TASK_ELOG("qCreateExecTask failed, code:%x - %s", code, tstrerror(code));
D
dapan1121 已提交
550
    QW_ERR_JRET(code);
D
dapan1121 已提交
551
  }
D
dapan1121 已提交
552

H
Haojun Liao 已提交
553
  if (NULL == sinkHandle || NULL == pTaskInfo) {
D
dapan1121 已提交
554 555 556 557
    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 已提交
558 559
  // 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 已提交
560

dengyihao's avatar
dengyihao 已提交
561
  // queryRsped = true;
D
dapan1121 已提交
562

D
dapan1121 已提交
563 564 565
  atomic_store_ptr(&ctx->taskHandle, pTaskInfo);
  atomic_store_ptr(&ctx->sinkHandle, sinkHandle);

D
dapan1121 已提交
566
  if (pTaskInfo && sinkHandle) {
567
    qwSaveTbVersionInfo(pTaskInfo, ctx);
D
dapan1121 已提交
568
    QW_ERR_JRET(qwExecTask(QW_FPARAMS(), ctx, NULL));
D
dapan1121 已提交
569
  }
dengyihao's avatar
dengyihao 已提交
570

D
dapan1121 已提交
571 572
_return:

D
dapan1121 已提交
573 574
  input.code = code;
  code = qwHandlePostPhaseEvents(QW_FPARAMS(), QW_PHASE_POST_QUERY, &input, NULL);
575

dengyihao's avatar
dengyihao 已提交
576
  // if (!queryRsped) {
D
dapan1121 已提交
577 578 579
  //  qwBuildAndSendQueryRsp(&qwMsg->connInfo, code);
  //  QW_TASK_DLOG("query msg rsped, handle:%p, code:%x - %s", qwMsg->connInfo.handle, code, tstrerror(code));
  //}
D
dapan1121 已提交
580

D
dapan1121 已提交
581
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
582 583
}

D
dapan1121 已提交
584
int32_t qwProcessCQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
585
  SQWTaskCtx   *ctx = NULL;
dengyihao's avatar
dengyihao 已提交
586
  int32_t       code = 0;
587
  SQWPhaseInput input = {0};
588
  void         *rsp = NULL;
dengyihao's avatar
dengyihao 已提交
589 590 591
  int32_t       dataLen = 0;
  bool          queryEnd = false;

D
dapan1121 已提交
592
  do {
D
dapan1121 已提交
593
    QW_ERR_JRET(qwHandlePrePhaseEvents(QW_FPARAMS(), QW_PHASE_PRE_CQUERY, &input, NULL));
D
dapan1121 已提交
594

D
dapan1121 已提交
595
    QW_ERR_JRET(qwGetTaskCtx(QW_FPARAMS(), &ctx));
D
dapan1121 已提交
596

dengyihao's avatar
dengyihao 已提交
597 598
    atomic_store_8((int8_t *)&ctx->queryInQueue, 0);
    atomic_store_8((int8_t *)&ctx->queryContinue, 0);
D
dapan1121 已提交
599

D
dapan1121 已提交
600
    QW_ERR_JRET(qwExecTask(QW_FPARAMS(), ctx, &queryEnd));
D
dapan1121 已提交
601

D
dapan1121 已提交
602 603
    if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
      SOutputData sOutput = {0};
D
dapan1121 已提交
604
      QW_ERR_JRET(qwGetQueryResFromSink(QW_FPARAMS(), ctx, &dataLen, &rsp, &sOutput));
dengyihao's avatar
dengyihao 已提交
605 606

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

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

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

D
dapan1121 已提交
615
        qwBuildFetchRsp(rsp, &sOutput, dataLen, qComplete);
D
dapan1121 已提交
616
        if (qComplete) {
dengyihao's avatar
dengyihao 已提交
617
          atomic_store_8((int8_t *)&ctx->queryEnd, true);
D
dapan1121 已提交
618
        }
H
Haojun Liao 已提交
619

D
dapan1121 已提交
620
        qwMsg->connInfo = ctx->dataConnInfo;
dengyihao's avatar
dengyihao 已提交
621 622 623
        QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_FETCH);

        qwBuildAndSendFetchRsp(&qwMsg->connInfo, rsp, dataLen, code);
D
dapan1121 已提交
624 625
        rsp = NULL;
        
dengyihao's avatar
dengyihao 已提交
626 627
        QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code,
                     tstrerror(code), dataLen);
D
dapan1121 已提交
628
      } else {
dengyihao's avatar
dengyihao 已提交
629
        atomic_store_8((int8_t *)&ctx->queryContinue, 1);
630 631 632
      }
    }

dengyihao's avatar
dengyihao 已提交
633
  _return:
634

D
dapan1121 已提交
635 636 637 638
    if (NULL == ctx) {
      break;
    }

D
dapan1121 已提交
639
    if (code && QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
dengyihao's avatar
dengyihao 已提交
640
      QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_FETCH);
641 642
      qwFreeFetchRsp(rsp);
      rsp = NULL;
dengyihao's avatar
dengyihao 已提交
643

D
dapan1121 已提交
644
      qwMsg->connInfo = ctx->dataConnInfo;
D
dapan1121 已提交
645
      qwBuildAndSendFetchRsp(&qwMsg->connInfo, NULL, 0, code);
dengyihao's avatar
dengyihao 已提交
646 647
      QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code, tstrerror(code),
                   0);
648
    }
D
dapan1121 已提交
649

D
dapan1121 已提交
650
    QW_LOCK(QW_WRITE, &ctx->lock);
dengyihao's avatar
dengyihao 已提交
651
    if (queryEnd || code || 0 == atomic_load_8((int8_t *)&ctx->queryContinue)) {
D
dapan1121 已提交
652
      // Note: if necessary, fetch need to put cquery to queue again
D
dapan1121 已提交
653
      atomic_store_8(&ctx->phase, 0);
dengyihao's avatar
dengyihao 已提交
654
      QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
655 656
      break;
    }
dengyihao's avatar
dengyihao 已提交
657
    QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
658
  } while (true);
D
dapan1121 已提交
659

D
dapan1121 已提交
660
  input.code = code;
dengyihao's avatar
dengyihao 已提交
661
  qwHandlePostPhaseEvents(QW_FPARAMS(), QW_PHASE_POST_CQUERY, &input, NULL);
D
dapan1121 已提交
662

dengyihao's avatar
dengyihao 已提交
663
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
664
}
D
dapan1121 已提交
665

D
dapan1121 已提交
666
int32_t qwProcessFetch(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
dengyihao's avatar
dengyihao 已提交
667 668 669
  int32_t       code = 0;
  int32_t       dataLen = 0;
  bool          locked = false;
670 671
  SQWTaskCtx   *ctx = NULL;
  void         *rsp = NULL;
D
dapan1121 已提交
672
  SQWPhaseInput input = {0};
D
dapan1121 已提交
673

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

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

D
dapan 已提交
678
  SOutputData sOutput = {0};
D
dapan1121 已提交
679
  QW_ERR_JRET(qwGetQueryResFromSink(QW_FPARAMS(), ctx, &dataLen, &rsp, &sOutput));
D
dapan1121 已提交
680

681
  if (NULL == rsp) {
D
dapan1121 已提交
682
    ctx->dataConnInfo = qwMsg->connInfo;
dengyihao's avatar
dengyihao 已提交
683

684
    QW_SET_EVENT_RECEIVED(ctx, QW_EVENT_FETCH);
D
dapan1121 已提交
685
  } else {
D
dapan1121 已提交
686
    bool qComplete = (DS_BUF_EMPTY == sOutput.bufStatus && sOutput.queryEnd);
dengyihao's avatar
dengyihao 已提交
687

D
dapan1121 已提交
688
    qwBuildFetchRsp(rsp, &sOutput, dataLen, qComplete);
D
dapan1121 已提交
689
    if (qComplete) {
dengyihao's avatar
dengyihao 已提交
690
      atomic_store_8((int8_t *)&ctx->queryEnd, true);
D
dapan1121 已提交
691
    }
D
dapan1121 已提交
692 693
  }

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

D
dapan1121 已提交
697 698
    QW_LOCK(QW_WRITE, &ctx->lock);
    locked = true;
699

D
dapan1121 已提交
700
    // RC WARNING
D
dapan1121 已提交
701
    if (QW_IS_QUERY_RUNNING(ctx)) {
dengyihao's avatar
dengyihao 已提交
702 703
      atomic_store_8((int8_t *)&ctx->queryContinue, 1);
    } else if (0 == atomic_load_8((int8_t *)&ctx->queryInQueue)) {
H
Haojun Liao 已提交
704
      qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_EXECUTING);
D
dapan1121 已提交
705

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

D
dapan1121 已提交
708
      QW_ERR_JRET(qwBuildAndSendCQueryMsg(QW_FPARAMS(), &qwMsg->connInfo));
709
    }
D
dapan 已提交
710
  }
dengyihao's avatar
dengyihao 已提交
711

D
dapan1121 已提交
712
_return:
D
dapan1121 已提交
713

D
dapan1121 已提交
714 715 716 717 718
  if (locked) {
    QW_UNLOCK(QW_WRITE, &ctx->lock);
  }

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

D
dapan 已提交
721 722 723
  if (code) {
    qwFreeFetchRsp(rsp);
    rsp = NULL;
D
dapan1121 已提交
724
    dataLen = 0;
D
dapan1121 已提交
725 726 727
  }

  if (code || rsp) {
S
shm  
Shengliang Guan 已提交
728
    qwBuildAndSendFetchRsp(&qwMsg->connInfo, rsp, dataLen, code);
dengyihao's avatar
dengyihao 已提交
729 730
    QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code, tstrerror(code),
                 dataLen);
D
dapan1121 已提交
731 732
  }

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

D
dapan1121 已提交
736
int32_t qwProcessDrop(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
dengyihao's avatar
dengyihao 已提交
737 738
  int32_t     code = 0;
  bool        rsped = false;
D
dapan1121 已提交
739
  SQWTaskCtx *ctx = NULL;
dengyihao's avatar
dengyihao 已提交
740
  bool        locked = false;
D
dapan1121 已提交
741

D
dapan1121 已提交
742 743
  // TODO : TASK ALREADY REMOVED AND A NEW DROP MSG RECEIVED

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

D
dapan1121 已提交
746 747 748 749 750
  QW_LOCK(QW_WRITE, &ctx->lock);

  locked = true;

  if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
751
    QW_TASK_WLOG_E("task already dropping");
D
dapan1121 已提交
752 753 754
    QW_ERR_JRET(TSDB_CODE_QRY_DUPLICATTED_OPERATION);
  }

D
dapan1121 已提交
755
  if (QW_IS_QUERY_RUNNING(ctx)) {
D
dapan1121 已提交
756
    QW_ERR_JRET(qwKillTaskHandle(QW_FPARAMS(), ctx));
D
dapan1121 已提交
757
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_DROPPING);
D
dapan1121 已提交
758
  } else if (ctx->phase > 0) {
D
dapan1121 已提交
759
    QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
D
dapan1121 已提交
760
    rsped = true;
D
dapan1121 已提交
761 762
  } else {
    // task not started
D
dapan1121 已提交
763
  }
D
dapan1121 已提交
764

D
dapan1121 已提交
765
  if (!rsped) {
766
    ctx->ctrlConnInfo = qwMsg->connInfo;
dengyihao's avatar
dengyihao 已提交
767

D
dapan1121 已提交
768 769
    QW_SET_EVENT_RECEIVED(ctx, QW_EVENT_DROP);
  }
770

D
dapan1121 已提交
771
_return:
D
dapan1121 已提交
772

D
dapan1121 已提交
773
  if (code) {
D
dapan1121 已提交
774 775 776
    if (ctx) {
      QW_UPDATE_RSP_CODE(ctx, code);
    }
H
Haojun Liao 已提交
777

D
dapan1121 已提交
778
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_FAILED);
D
dapan1121 已提交
779 780
  }

D
dapan 已提交
781 782 783 784
  if (locked) {
    QW_UNLOCK(QW_WRITE, &ctx->lock);
  }

D
dapan1121 已提交
785
  if (ctx) {
D
dapan1121 已提交
786
    qwReleaseTaskCtx(mgmt, ctx);
D
dapan1121 已提交
787 788
  }

D
dapan1121 已提交
789 790 791
  QW_RET(TSDB_CODE_SUCCESS);
}

D
dapan1121 已提交
792
int32_t qwProcessHb(SQWorker *mgmt, SQWMsg *qwMsg, SSchedulerHbReq *req) {
dengyihao's avatar
dengyihao 已提交
793
  int32_t         code = 0;
D
dapan1121 已提交
794
  SSchedulerHbRsp rsp = {0};
795
  SQWSchStatus   *sch = NULL;
D
dapan1121 已提交
796

D
dapan1121 已提交
797 798 799
  if (qwMsg->code) {
    QW_RET(qwProcessHbLinkBroken(mgmt, qwMsg, req));
  }
D
dapan1121 已提交
800 801

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

D
dapan1121 已提交
804 805
  sch->hbBrokenTs = 0;

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

D
dapan1121 已提交
808
  if (sch->hbConnInfo.handle) {
809
    tmsgReleaseHandle(&sch->hbConnInfo, TAOS_CONN_SERVER);
S
Shengliang Guan 已提交
810
    sch->hbConnInfo.handle = NULL;
D
dapan1121 已提交
811
  }
D
dapan1121 已提交
812

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

D
dapan1121 已提交
816
  QW_UNLOCK(QW_WRITE, &sch->hbConnLock);
dengyihao's avatar
dengyihao 已提交
817 818 819

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

D
dapan1121 已提交
821 822 823 824
  qwReleaseScheduler(QW_READ, mgmt);

_return:

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

S
shm  
Shengliang Guan 已提交
827
  qwBuildAndSendHbRsp(&qwMsg->connInfo, &rsp, code);
D
dapan1121 已提交
828 829

  if (code) {
830
    tmsgReleaseHandle(&qwMsg->connInfo, TAOS_CONN_SERVER);
S
Shengliang Guan 已提交
831
    qwMsg->connInfo.handle = NULL;
D
dapan1121 已提交
832
  }
dengyihao's avatar
dengyihao 已提交
833

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

D
dapan1121 已提交
836
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
837 838 839
}

void qwProcessHbTimerEvent(void *param, void *tmrId) {
840
  SQWHbParam *hbParam = (SQWHbParam *)param;
D
dapan1121 已提交
841 842 843 844
  if (hbParam->qwrId != atomic_load_32(&gQwMgmt.qwRef)) {
    return;
  }

845
  int64_t   refId = hbParam->refId;
D
dapan1121 已提交
846 847 848 849 850
  SQWorker *mgmt = qwAcquire(refId);
  if (NULL == mgmt) {
    QW_DLOG("qwAcquire %" PRIx64 "failed", refId);
    return;
  }
851

D
dapan1121 已提交
852
  SQWSchStatus *sch = NULL;
dengyihao's avatar
dengyihao 已提交
853
  int32_t       taskNum = 0;
854
  SQWHbInfo    *rspList = NULL;
D
dapan1121 已提交
855
  SArray       *pExpiredSch = NULL;
dengyihao's avatar
dengyihao 已提交
856
  int32_t       code = 0;
D
dapan1121 已提交
857

D
dapan1121 已提交
858 859
  qwDbgDumpMgmtInfo(mgmt);

D
dapan1121 已提交
860 861 862 863 864
  QW_LOCK(QW_READ, &mgmt->schLock);

  int32_t schNum = taosHashGetSize(mgmt->schHash);
  if (schNum <= 0) {
    QW_UNLOCK(QW_READ, &mgmt->schLock);
D
dapan1121 已提交
865
    taosTmrReset(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, param, mgmt->timer, &mgmt->hbTimer);
D
dapan1121 已提交
866
    qwRelease(refId);
D
dapan1121 已提交
867
    return;
D
dapan1121 已提交
868 869
  }

wafwerar's avatar
wafwerar 已提交
870
  rspList = taosMemoryCalloc(schNum, sizeof(SQWHbInfo));
D
dapan1121 已提交
871 872
  pExpiredSch = taosArrayInit(schNum, sizeof(uint64_t));
  if (NULL == rspList || NULL == pExpiredSch) {
D
dapan1121 已提交
873
    QW_UNLOCK(QW_READ, &mgmt->schLock);
D
dapan1121 已提交
874 875
    taosMemoryFree(rspList);
    taosArrayDestroy(pExpiredSch);
D
dapan1121 已提交
876 877
    QW_ELOG("calloc %d SQWHbInfo failed", schNum);
    taosTmrReset(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, param, mgmt->timer, &mgmt->hbTimer);
D
dapan1121 已提交
878
    qwRelease(refId);
D
dapan1121 已提交
879
    return;
D
dapan1121 已提交
880 881
  }

882
  void   *key = NULL;
dengyihao's avatar
dengyihao 已提交
883
  size_t  keyLen = 0;
D
dapan1121 已提交
884
  int32_t i = 0;
D
dapan1121 已提交
885
  int64_t currentMs = taosGetTimestampMs();
D
dapan1121 已提交
886 887 888

  void *pIter = taosHashIterate(mgmt->schHash, NULL);
  while (pIter) {
D
dapan1121 已提交
889 890 891
    SQWSchStatus *sch = (SQWSchStatus *)pIter;
    if (NULL == sch->hbConnInfo.handle) {
      uint64_t *sId = taosHashGetKey(pIter, NULL);
D
dapan1121 已提交
892
      QW_TLOG("cancel send hb to sch %" PRIx64 " cause of no connection handle", *sId);
D
dapan1121 已提交
893 894 895 896 897

      if (sch->hbBrokenTs > 0 && ((currentMs - sch->hbBrokenTs) > QW_SCH_TIMEOUT_MSEC) && taosHashGetSize(sch->tasksHash) <= 0) {
        taosArrayPush(pExpiredSch, sId);
      }
      
D
dapan1121 已提交
898 899 900
      pIter = taosHashIterate(mgmt->schHash, pIter);
      continue;
    }
dengyihao's avatar
dengyihao 已提交
901

D
dapan1121 已提交
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
    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 已提交
917
    qwBuildAndSendHbRsp(&rspList[j].connInfo, &rspList[j].rsp, code);
918 919
    /*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 已提交
920
    tFreeSSchedulerHbRsp(&rspList[j].rsp);
D
dapan1121 已提交
921 922
  }

D
dapan1121 已提交
923
  if (taosArrayGetSize(pExpiredSch) > 0) {
D
dapan1121 已提交
924
    qwClearExpiredSch(mgmt, pExpiredSch);
D
dapan1121 已提交
925 926
  }

wafwerar's avatar
wafwerar 已提交
927
  taosMemoryFreeClear(rspList);
D
dapan1121 已提交
928
  taosArrayDestroy(pExpiredSch);
D
dapan1121 已提交
929

dengyihao's avatar
dengyihao 已提交
930
  taosTmrReset(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, param, mgmt->timer, &mgmt->hbTimer);
931
  qwRelease(refId);
D
dapan1121 已提交
932 933
}

D
dapan1121 已提交
934
int32_t qwProcessDelete(QW_FPARAMS_DEF, SQWMsg *qwMsg, SRpcMsg *pRsp, SDeleteRes *pRes) {
D
dapan1121 已提交
935 936 937 938 939 940 941 942 943 944 945 946 947
  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);
  }

D
dapan1121 已提交
948
  ctx.plan = plan;
D
dapan1121 已提交
949

950
  code = qCreateExecTask(qwMsg->node, mgmt->nodeId, tId, plan, &pTaskInfo, &sinkHandle, NULL, OPTR_EXEC_MODEL_BATCH);
D
dapan1121 已提交
951 952 953 954 955 956 957 958 959 960
  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 已提交
961 962
  ctx.taskHandle = pTaskInfo;
  ctx.sinkHandle = sinkHandle;
D
dapan1121 已提交
963

D
dapan1121 已提交
964
  QW_ERR_JRET(qwExecTask(QW_FPARAMS(), &ctx, NULL));
D
dapan1121 已提交
965

D
dapan1121 已提交
966
  QW_ERR_JRET(qwGetDeleteResFromSink(QW_FPARAMS(), &ctx, &pRsp->contLen, &pRsp->pCont, pRes));
D
dapan1121 已提交
967 968 969 970 971 972 973 974

_return:

  qwFreeTaskCtx(QW_FPARAMS(), &ctx);

  QW_RET(TSDB_CODE_SUCCESS);
}

D
dapan1121 已提交
975

S
Shengliang Guan 已提交
976
int32_t qWorkerInit(int8_t nodeType, int32_t nodeId, SQWorkerCfg *cfg, void **qWorkerMgmt, const SMsgCb *pMsgCb) {
977
  if (NULL == qWorkerMgmt || pMsgCb->mgmt == NULL) {
D
dapan1121 已提交
978 979 980
    qError("invalid param to init qworker");
    QW_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }
S
Shengliang 已提交
981

D
dapan1121 已提交
982 983 984 985
  int32_t qwNum = atomic_add_fetch_32(&gQwMgmt.qwNum, 1);
  if (1 == qwNum) {
    memset(gQwMgmt.param, 0, sizeof(gQwMgmt.param));
  }
D
dapan1121 已提交
986 987 988 989 990 991 992 993

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

  SQWorker *mgmt = taosMemoryCalloc(1, sizeof(SQWorker));
D
dapan1121 已提交
994
  if (NULL == mgmt) {
D
dapan1121 已提交
995 996
    qError("calloc %d failed", (int32_t)sizeof(SQWorker));
    atomic_sub_fetch_32(&gQwMgmt.qwNum, 1);
D
dapan1121 已提交
997
    QW_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
998 999 1000 1001
  }

  if (cfg) {
    mgmt->cfg = *cfg;
D
dapan1121 已提交
1002
    if (0 == mgmt->cfg.maxSchedulerNum) {
D
dapan1121 已提交
1003
      mgmt->cfg.maxSchedulerNum = QW_DEFAULT_SCHEDULER_NUMBER;
D
dapan1121 已提交
1004 1005
    }
    if (0 == mgmt->cfg.maxTaskNum) {
D
dapan1121 已提交
1006
      mgmt->cfg.maxTaskNum = QW_DEFAULT_TASK_NUMBER;
D
dapan1121 已提交
1007 1008
    }
    if (0 == mgmt->cfg.maxSchTaskNum) {
D
dapan1121 已提交
1009
      mgmt->cfg.maxSchTaskNum = QW_DEFAULT_SCH_TASK_NUMBER;
D
dapan1121 已提交
1010
    }
D
dapan1121 已提交
1011
  } else {
D
dapan1121 已提交
1012 1013 1014
    mgmt->cfg.maxSchedulerNum = QW_DEFAULT_SCHEDULER_NUMBER;
    mgmt->cfg.maxTaskNum = QW_DEFAULT_TASK_NUMBER;
    mgmt->cfg.maxSchTaskNum = QW_DEFAULT_SCH_TASK_NUMBER;
D
dapan1121 已提交
1015 1016
  }

dengyihao's avatar
dengyihao 已提交
1017 1018
  mgmt->schHash = taosHashInit(mgmt->cfg.maxSchedulerNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false,
                               HASH_ENTRY_LOCK);
D
dapan1121 已提交
1019
  if (NULL == mgmt->schHash) {
wafwerar's avatar
wafwerar 已提交
1020
    taosMemoryFreeClear(mgmt);
D
dapan1121 已提交
1021
    qError("init %d scheduler hash failed", mgmt->cfg.maxSchedulerNum);
D
dapan1121 已提交
1022
    QW_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
1023 1024
  }

dengyihao's avatar
dengyihao 已提交
1025 1026
  mgmt->ctxHash =
      taosHashInit(mgmt->cfg.maxTaskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
D
dapan1121 已提交
1027
  if (NULL == mgmt->ctxHash) {
D
dapan1121 已提交
1028
    qError("init %d task ctx hash failed", mgmt->cfg.maxTaskNum);
D
dapan1121 已提交
1029 1030 1031 1032 1033 1034 1035 1036 1037
    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 已提交
1038 1039
  mgmt->nodeType = nodeType;
  mgmt->nodeId = nodeId;
S
Shengliang Guan 已提交
1040
  mgmt->msgCb = *pMsgCb;
D
dapan1121 已提交
1041

D
dapan1121 已提交
1042 1043 1044 1045 1046 1047
  mgmt->refId = taosAddRef(gQwMgmt.qwRef, mgmt);
  if (mgmt->refId < 0) {
    qError("taosAddRef qw failed, error:%s", tstrerror(terrno));
    QW_ERR_JRET(terrno);
  }

D
dapan1121 已提交
1048 1049 1050
  SQWHbParam *param = NULL;
  qwSetHbParam(mgmt->refId, &param);

1051
  mgmt->hbTimer = taosTmrStart(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, (void *)param, mgmt->timer);
D
dapan1121 已提交
1052 1053 1054 1055
  if (NULL == mgmt->hbTimer) {
    qError("start hb timer failed");
    QW_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
1056

D
dapan1121 已提交
1057 1058
  *qWorkerMgmt = mgmt;

D
dapan1121 已提交
1059 1060
  qDebug("qworker initialized for node, type:%d, id:%d, handle:%p", mgmt->nodeType, mgmt->nodeId, mgmt);

D
dapan1121 已提交
1061
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1062 1063 1064

_return:

D
dapan1121 已提交
1065 1066 1067 1068 1069 1070 1071
  if (mgmt->refId >= 0) {
    qwRelease(mgmt->refId);
  } else {
    taosHashCleanup(mgmt->schHash);
    taosHashCleanup(mgmt->ctxHash);
    taosTmrCleanUp(mgmt->timer);
    taosMemoryFreeClear(mgmt);
D
dapan1121 已提交
1072

1073
    atomic_sub_fetch_32(&gQwMgmt.qwNum, 1);
D
dapan1121 已提交
1074
  }
1075

D
dapan1121 已提交
1076
  QW_RET(code);
D
dapan1121 已提交
1077
}
D
dapan1121 已提交
1078 1079 1080 1081

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

D
dapan1121 已提交
1084
  SQWorker *mgmt = *qWorkerMgmt;
D
dapan1121 已提交
1085

D
dapan1121 已提交
1086 1087 1088
  if (taosRemoveRef(gQwMgmt.qwRef, mgmt->refId)) {
    qError("remove qw from ref list failed, refId:%" PRIx64, mgmt->refId);
  }
D
dapan1121 已提交
1089
}
D
dapan1121 已提交
1090

D
dapan1121 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
int32_t qWorkerGetStat(SReadHandle *handle, void *qWorkerMgmt, SQWorkerStat *pStat) {
  if (NULL == handle || NULL == qWorkerMgmt || NULL == pStat) {
    QW_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  SQWorker *mgmt = (SQWorker *)qWorkerMgmt;
  SDataSinkStat sinkStat = {0};
  
  dsDataSinkGetCacheSize(&sinkStat);
  pStat->cacheDataSize = sinkStat.cachedSize;
  
  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 已提交
1107
  pStat->deleteProcessed = QW_STAT_GET(mgmt->stat.msgStat.deleteProcessed);
D
dapan1121 已提交
1108 1109 1110 1111 1112 1113 1114

  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 已提交
1115 1116 1117
}


dengyihao's avatar
dengyihao 已提交
1118