qworker.c 31.2 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
    QW_GET_QTID(key, status.queryId, status.taskId, status.execId);
D
dapan1121 已提交
172 173
    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_SUCC);
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_SUCC);
D
dapan1121 已提交
240 241
  }

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

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

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

D
dapan1121 已提交
253
  if (len <= 0 || len != sizeof(SDeleterRes)) {
D
dapan1121 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    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 已提交
270 271
  SDeleterRes* pDelRes = (SDeleterRes*)output.pData;
  
D
dapan1121 已提交
272
  pRes->suid = pDelRes->suid;
D
dapan1121 已提交
273 274 275
  pRes->uidList = pDelRes->uidList;
  pRes->skey = pDelRes->skey;
  pRes->ekey = pDelRes->ekey;
276
  pRes->affectedRows = pDelRes->affectedRows;
D
dapan1121 已提交
277 278 279 280
  
  return TSDB_CODE_SUCCESS;
}

281

D
dapan1121 已提交
282
int32_t qwHandlePrePhaseEvents(QW_FPARAMS_DEF, int8_t phase, SQWPhaseInput *input, SQWPhaseOutput *output) {
283 284
  int32_t         code = 0;
  SQWTaskCtx     *ctx = NULL;
S
Shengliang Guan 已提交
285
  SRpcHandleInfo *cancelConnection = NULL;
D
dapan1121 已提交
286

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

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

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

D
dapan1121 已提交
293
  if (QW_PHASE_PRE_FETCH == phase) {
dengyihao's avatar
dengyihao 已提交
294
    atomic_store_8((int8_t *)&ctx->queryFetched, true);
D
dapan1121 已提交
295
  } else {
D
dapan1121 已提交
296 297
    atomic_store_8(&ctx->phase, phase);
  }
D
dapan1121 已提交
298

dengyihao's avatar
dengyihao 已提交
299
  if (atomic_load_8((int8_t *)&ctx->queryEnd)) {
D
dapan1121 已提交
300 301 302
    QW_TASK_ELOG_E("query already end");
    QW_ERR_JRET(TSDB_CODE_QW_MSG_ERROR);
  }
D
dapan1121 已提交
303

D
dapan1121 已提交
304 305
  switch (phase) {
    case QW_PHASE_PRE_QUERY: {
D
dapan1121 已提交
306
      if (QW_IS_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
H
Haojun Liao 已提交
307
        QW_TASK_ELOG("task already dropped at wrong phase %s", qwPhaseStr(phase));
D
dapan1121 已提交
308
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_STATUS_ERROR);
D
dapan1121 已提交
309 310
        break;
      }
D
dapan1121 已提交
311

D
dapan1121 已提交
312
      if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
313
        QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
314

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

D
dapan1121 已提交
318
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
D
dapan 已提交
319
        break;
D
dapan1121 已提交
320
      }
D
dapan1121 已提交
321

D
dapan1121 已提交
322
      QW_ERR_JRET(qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_EXEC));
D
dapan1121 已提交
323 324
      break;
    }
D
dapan1121 已提交
325
    case QW_PHASE_PRE_FETCH: {
D
dapan1121 已提交
326 327
      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 已提交
328 329
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
      }
D
dapan1121 已提交
330

D
dapan1121 已提交
331
      if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
D
dapan1121 已提交
332
        QW_TASK_WLOG("last fetch still not processed, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
333 334 335 336
        QW_ERR_JRET(TSDB_CODE_QRY_DUPLICATTED_OPERATION);
      }

      if (!QW_IS_EVENT_PROCESSED(ctx, QW_EVENT_READY)) {
D
dapan1121 已提交
337
        QW_TASK_ELOG("ready msg has not been processed, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
338
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_MSG_ERROR);
D
dapan1121 已提交
339 340
      }
      break;
dengyihao's avatar
dengyihao 已提交
341
    }
D
dapan1121 已提交
342 343
    case QW_PHASE_PRE_CQUERY: {
      if (QW_IS_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
344
        QW_TASK_WLOG("task already dropped, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
345
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
D
dapan1121 已提交
346
      }
D
dapan1121 已提交
347

D
dapan1121 已提交
348
      if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
349
        QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
H
Haojun Liao 已提交
350

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

D
dapan1121 已提交
354
        QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
D
dapan1121 已提交
355
      }
D
dapan1121 已提交
356

D
dapan1121 已提交
357
      break;
D
dapan1121 已提交
358 359 360 361 362 363 364
    }
    default:
      QW_TASK_ELOG("invalid phase %s", qwPhaseStr(phase));
      QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
  }

  if (ctx->rspCode) {
365
    QW_TASK_ELOG("task already failed at phase %s, code:%s", qwPhaseStr(phase), tstrerror(ctx->rspCode));
D
dapan1121 已提交
366
    QW_ERR_JRET(ctx->rspCode);
D
dapan1121 已提交
367
  }
D
dapan1121 已提交
368

D
dapan1121 已提交
369 370
_return:
  if (ctx) {
D
dapan1121 已提交
371
    QW_UPDATE_RSP_CODE(ctx, code);
dengyihao's avatar
dengyihao 已提交
372

D
dapan1121 已提交
373
    QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
374 375
    qwReleaseTaskCtx(mgmt, ctx);
  }
D
dapan1121 已提交
376

D
dapan1121 已提交
377
  if (cancelConnection) {
S
shm  
Shengliang Guan 已提交
378
    qwBuildAndSendCancelRsp(cancelConnection, code);
D
dapan1121 已提交
379
    QW_TASK_DLOG("cancel rsp send, handle:%p, code:%x - %s", cancelConnection->handle, code, tstrerror(code));
D
dapan1121 已提交
380 381
  }

382 383 384 385 386
  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 已提交
387 388 389 390 391

  QW_RET(code);
}

int32_t qwHandlePostPhaseEvents(QW_FPARAMS_DEF, int8_t phase, SQWPhaseInput *input, SQWPhaseOutput *output) {
S
Shengliang Guan 已提交
392 393 394
  int32_t         code = 0;
  SQWTaskCtx     *ctx = NULL;
  SRpcHandleInfo  connInfo = {0};
D
dapan1121 已提交
395
  SRpcHandleInfo *rspConnection = NULL;
D
dapan1121 已提交
396

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

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

D
dapan1121 已提交
401 402 403
  QW_LOCK(QW_WRITE, &ctx->lock);

  if (QW_IS_EVENT_PROCESSED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
404
    QW_TASK_WLOG("task already dropped, phase:%s", qwPhaseStr(phase));
D
dapan1121 已提交
405 406 407 408
    QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
  }

  if (QW_PHASE_POST_QUERY == phase) {
409
    connInfo = ctx->ctrlConnInfo;
D
dapan1121 已提交
410
    rspConnection = &connInfo;
dengyihao's avatar
dengyihao 已提交
411

D
dapan1121 已提交
412
    QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_READY);
D
dapan1121 已提交
413 414
  }

D
dapan1121 已提交
415
  if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
416 417 418 419
    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 已提交
420

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

D
dapan1121 已提交
424 425
    QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
    QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED);
D
dapan1121 已提交
426 427 428
  }

  if (ctx->rspCode) {
dengyihao's avatar
dengyihao 已提交
429 430
    QW_TASK_ELOG("task already failed, phase %s, error:%x - %s", qwPhaseStr(phase), ctx->rspCode,
                 tstrerror(ctx->rspCode));
D
dapan1121 已提交
431
    QW_ERR_JRET(ctx->rspCode);
dengyihao's avatar
dengyihao 已提交
432
  }
D
dapan1121 已提交
433

D
dapan1121 已提交
434
  QW_ERR_JRET(input->code);
D
dapan1121 已提交
435 436 437

_return:

D
dapan1121 已提交
438
  if (TSDB_CODE_SUCCESS == code && QW_PHASE_POST_QUERY == phase) {
D
dapan1121 已提交
439
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_PART_SUCC);
D
dapan1121 已提交
440 441
  }

D
dapan1121 已提交
442
  if (rspConnection) {
D
dapan1121 已提交
443 444
    qwBuildAndSendQueryRsp(input->msgType + 1, rspConnection, code, ctx ? &ctx->tbInfo : NULL);
    QW_TASK_DLOG("query msg rsped, handle:%p, code:%x - %s", rspConnection->handle, code, tstrerror(code));
445 446
  }

D
dapan1121 已提交
447
  if (ctx) {
D
dapan1121 已提交
448
    QW_UPDATE_RSP_CODE(ctx, code);
D
dapan1121 已提交
449

D
dapan1121 已提交
450 451 452
    if (QW_PHASE_POST_FETCH != phase) {
      atomic_store_8(&ctx->phase, phase);
    }
dengyihao's avatar
dengyihao 已提交
453

D
dapan1121 已提交
454
    QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
455
    qwReleaseTaskCtx(mgmt, ctx);
D
dapan1121 已提交
456 457
  }

D
dapan1121 已提交
458
  if (code) {
D
dapan1121 已提交
459
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_FAIL);
D
dapan1121 已提交
460 461
  }

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

D
dapan1121 已提交
464 465 466
  QW_RET(code);
}

D
dapan1121 已提交
467 468 469 470 471 472 473
int32_t qwAbortPrerocessQuery(QW_FPARAMS_DEF) {
  QW_ERR_RET(qwDropTask(QW_FPARAMS()));

  QW_RET(TSDB_CODE_SUCCESS);
}


D
dapan1121 已提交
474
int32_t qwPrerocessQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
475 476 477 478 479 480 481
  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 已提交
482

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

D
dapan1121 已提交
485 486 487
  QW_ERR_JRET(qwAddTaskCtx(QW_FPARAMS()));

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

D
dapan1121 已提交
489 490
  ctx->ctrlConnInfo = qwMsg->connInfo;

D
dapan1121 已提交
491
  QW_ERR_JRET(qwAddTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_INIT));
D
dapan1121 已提交
492 493 494 495 496 497 498 499 500 501 502 503

_return:

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

  QW_RET(TSDB_CODE_SUCCESS);
}


504
int32_t qwProcessQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg, int8_t taskType, int8_t explain, const char* sql) {
D
dapan1121 已提交
505 506 507 508 509 510 511 512
  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 已提交
513
  QW_ERR_JRET(qwHandlePrePhaseEvents(QW_FPARAMS(), QW_PHASE_PRE_QUERY, &input, NULL));
D
dapan1121 已提交
514 515

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

D
dapan1121 已提交
517 518 519
  ctx->taskType = taskType;
  ctx->explain = explain;
  ctx->queryType = qwMsg->msgType;
X
Xiaoyu Wang 已提交
520

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

D
dapan1121 已提交
523 524
  code = qStringToSubplan(qwMsg->msg, &plan);
  if (TSDB_CODE_SUCCESS != code) {
525 526
    code = TSDB_CODE_INVALID_MSG;
    QW_TASK_ELOG("task physical plan to subplan failed, code:%x - %s", code, tstrerror(code));
D
dapan1121 已提交
527
    QW_ERR_JRET(code);
D
dapan1121 已提交
528
  }
dengyihao's avatar
dengyihao 已提交
529

D
dapan1121 已提交
530 531
  ctx->plan = plan;

532
  code = qCreateExecTask(qwMsg->node, mgmt->nodeId, tId, plan, &pTaskInfo, &sinkHandle, sql, OPTR_EXEC_MODEL_BATCH);
D
dapan1121 已提交
533
  if (code) {
D
dapan1121 已提交
534
    QW_TASK_ELOG("qCreateExecTask failed, code:%x - %s", code, tstrerror(code));
D
dapan1121 已提交
535
    QW_ERR_JRET(code);
D
dapan1121 已提交
536
  }
D
dapan1121 已提交
537

H
Haojun Liao 已提交
538
  if (NULL == sinkHandle || NULL == pTaskInfo) {
D
dapan1121 已提交
539 540 541 542
    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 已提交
543 544
  // 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 已提交
545

dengyihao's avatar
dengyihao 已提交
546
  // queryRsped = true;
D
dapan1121 已提交
547

D
dapan1121 已提交
548 549 550
  atomic_store_ptr(&ctx->taskHandle, pTaskInfo);
  atomic_store_ptr(&ctx->sinkHandle, sinkHandle);

D
dapan1121 已提交
551
  if (pTaskInfo && sinkHandle) {
552
    qwSaveTbVersionInfo(pTaskInfo, ctx);
D
dapan1121 已提交
553
    QW_ERR_JRET(qwExecTask(QW_FPARAMS(), ctx, NULL));
D
dapan1121 已提交
554
  }
dengyihao's avatar
dengyihao 已提交
555

D
dapan1121 已提交
556 557
_return:

D
dapan1121 已提交
558
  input.code = code;
D
dapan1121 已提交
559
  input.msgType = qwMsg->msgType;
D
dapan1121 已提交
560
  code = qwHandlePostPhaseEvents(QW_FPARAMS(), QW_PHASE_POST_QUERY, &input, NULL);
561

dengyihao's avatar
dengyihao 已提交
562
  // if (!queryRsped) {
D
dapan1121 已提交
563 564 565
  //  qwBuildAndSendQueryRsp(&qwMsg->connInfo, code);
  //  QW_TASK_DLOG("query msg rsped, handle:%p, code:%x - %s", qwMsg->connInfo.handle, code, tstrerror(code));
  //}
D
dapan1121 已提交
566

D
dapan1121 已提交
567
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
568 569
}

D
dapan1121 已提交
570
int32_t qwProcessCQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
571
  SQWTaskCtx   *ctx = NULL;
dengyihao's avatar
dengyihao 已提交
572
  int32_t       code = 0;
573
  SQWPhaseInput input = {0};
574
  void         *rsp = NULL;
dengyihao's avatar
dengyihao 已提交
575 576 577
  int32_t       dataLen = 0;
  bool          queryEnd = false;

D
dapan1121 已提交
578
  do {
D
dapan1121 已提交
579
    QW_ERR_JRET(qwHandlePrePhaseEvents(QW_FPARAMS(), QW_PHASE_PRE_CQUERY, &input, NULL));
D
dapan1121 已提交
580

D
dapan1121 已提交
581
    QW_ERR_JRET(qwGetTaskCtx(QW_FPARAMS(), &ctx));
D
dapan1121 已提交
582

dengyihao's avatar
dengyihao 已提交
583 584
    atomic_store_8((int8_t *)&ctx->queryInQueue, 0);
    atomic_store_8((int8_t *)&ctx->queryContinue, 0);
D
dapan1121 已提交
585

D
dapan1121 已提交
586
    QW_ERR_JRET(qwExecTask(QW_FPARAMS(), ctx, &queryEnd));
D
dapan1121 已提交
587

D
dapan1121 已提交
588 589
    if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
      SOutputData sOutput = {0};
D
dapan1121 已提交
590
      QW_ERR_JRET(qwGetQueryResFromSink(QW_FPARAMS(), ctx, &dataLen, &rsp, &sOutput));
dengyihao's avatar
dengyihao 已提交
591 592

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

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

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

D
dapan1121 已提交
601
        qwBuildFetchRsp(rsp, &sOutput, dataLen, qComplete);
D
dapan1121 已提交
602
        if (qComplete) {
dengyihao's avatar
dengyihao 已提交
603
          atomic_store_8((int8_t *)&ctx->queryEnd, true);
D
dapan1121 已提交
604
        }
H
Haojun Liao 已提交
605

D
dapan1121 已提交
606
        qwMsg->connInfo = ctx->dataConnInfo;
dengyihao's avatar
dengyihao 已提交
607 608
        QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_FETCH);

D
dapan1121 已提交
609
        qwBuildAndSendFetchRsp(ctx->fetchType, &qwMsg->connInfo, rsp, dataLen, code);
D
dapan1121 已提交
610 611
        rsp = NULL;
        
dengyihao's avatar
dengyihao 已提交
612 613
        QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code,
                     tstrerror(code), dataLen);
D
dapan1121 已提交
614
      } else {
dengyihao's avatar
dengyihao 已提交
615
        atomic_store_8((int8_t *)&ctx->queryContinue, 1);
616 617 618
      }
    }

dengyihao's avatar
dengyihao 已提交
619
  _return:
620

D
dapan1121 已提交
621 622 623 624
    if (NULL == ctx) {
      break;
    }

D
dapan1121 已提交
625
    if (code && QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_FETCH)) {
dengyihao's avatar
dengyihao 已提交
626
      QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_FETCH);
627 628
      qwFreeFetchRsp(rsp);
      rsp = NULL;
dengyihao's avatar
dengyihao 已提交
629

D
dapan1121 已提交
630
      qwMsg->connInfo = ctx->dataConnInfo;
D
dapan1121 已提交
631
      qwBuildAndSendFetchRsp(ctx->fetchType, &qwMsg->connInfo, NULL, 0, code);
dengyihao's avatar
dengyihao 已提交
632 633
      QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code, tstrerror(code),
                   0);
634
    }
D
dapan1121 已提交
635

D
dapan1121 已提交
636
    QW_LOCK(QW_WRITE, &ctx->lock);
dengyihao's avatar
dengyihao 已提交
637
    if (queryEnd || code || 0 == atomic_load_8((int8_t *)&ctx->queryContinue)) {
D
dapan1121 已提交
638
      // Note: if necessary, fetch need to put cquery to queue again
D
dapan1121 已提交
639
      atomic_store_8(&ctx->phase, 0);
dengyihao's avatar
dengyihao 已提交
640
      QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
641 642
      break;
    }
dengyihao's avatar
dengyihao 已提交
643
    QW_UNLOCK(QW_WRITE, &ctx->lock);
D
dapan1121 已提交
644
  } while (true);
D
dapan1121 已提交
645

D
dapan1121 已提交
646
  input.code = code;
dengyihao's avatar
dengyihao 已提交
647
  qwHandlePostPhaseEvents(QW_FPARAMS(), QW_PHASE_POST_CQUERY, &input, NULL);
D
dapan1121 已提交
648

dengyihao's avatar
dengyihao 已提交
649
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
650
}
D
dapan1121 已提交
651

D
dapan1121 已提交
652
int32_t qwProcessFetch(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
dengyihao's avatar
dengyihao 已提交
653 654 655
  int32_t       code = 0;
  int32_t       dataLen = 0;
  bool          locked = false;
656 657
  SQWTaskCtx   *ctx = NULL;
  void         *rsp = NULL;
D
dapan1121 已提交
658
  SQWPhaseInput input = {0};
D
dapan1121 已提交
659

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

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

D
dapan1121 已提交
664 665
  ctx->queryType = qwMsg->msgType;

D
dapan 已提交
666
  SOutputData sOutput = {0};
D
dapan1121 已提交
667
  QW_ERR_JRET(qwGetQueryResFromSink(QW_FPARAMS(), ctx, &dataLen, &rsp, &sOutput));
D
dapan1121 已提交
668

669
  if (NULL == rsp) {
D
dapan1121 已提交
670
    ctx->dataConnInfo = qwMsg->connInfo;
dengyihao's avatar
dengyihao 已提交
671

672
    QW_SET_EVENT_RECEIVED(ctx, QW_EVENT_FETCH);
D
dapan1121 已提交
673
  } else {
D
dapan1121 已提交
674
    bool qComplete = (DS_BUF_EMPTY == sOutput.bufStatus && sOutput.queryEnd);
dengyihao's avatar
dengyihao 已提交
675

D
dapan1121 已提交
676
    qwBuildFetchRsp(rsp, &sOutput, dataLen, qComplete);
D
dapan1121 已提交
677
    if (qComplete) {
dengyihao's avatar
dengyihao 已提交
678
      atomic_store_8((int8_t *)&ctx->queryEnd, true);
D
dapan1121 已提交
679
    }
D
dapan1121 已提交
680 681
  }

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

D
dapan1121 已提交
685 686
    QW_LOCK(QW_WRITE, &ctx->lock);
    locked = true;
687

D
dapan1121 已提交
688
    // RC WARNING
D
dapan1121 已提交
689
    if (QW_IS_QUERY_RUNNING(ctx)) {
dengyihao's avatar
dengyihao 已提交
690 691
      atomic_store_8((int8_t *)&ctx->queryContinue, 1);
    } else if (0 == atomic_load_8((int8_t *)&ctx->queryInQueue)) {
D
dapan1121 已提交
692
      qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_EXEC);
D
dapan1121 已提交
693

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

D
dapan1121 已提交
696
      QW_ERR_JRET(qwBuildAndSendCQueryMsg(QW_FPARAMS(), &qwMsg->connInfo));
697
    }
D
dapan 已提交
698
  }
dengyihao's avatar
dengyihao 已提交
699

D
dapan1121 已提交
700
_return:
D
dapan1121 已提交
701

D
dapan1121 已提交
702 703 704 705 706
  if (locked) {
    QW_UNLOCK(QW_WRITE, &ctx->lock);
  }

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

D
dapan 已提交
709 710 711
  if (code) {
    qwFreeFetchRsp(rsp);
    rsp = NULL;
D
dapan1121 已提交
712
    dataLen = 0;
D
dapan1121 已提交
713 714 715
  }

  if (code || rsp) {
D
dapan1121 已提交
716
    qwBuildAndSendFetchRsp(qwMsg->msgType + 1, &qwMsg->connInfo, rsp, dataLen, code);
dengyihao's avatar
dengyihao 已提交
717 718
    QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code, tstrerror(code),
                 dataLen);
D
dapan1121 已提交
719 720
  }

D
dapan1121 已提交
721
  QW_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
722
}
D
dapan1121 已提交
723

D
dapan1121 已提交
724
int32_t qwProcessDrop(QW_FPARAMS_DEF, SQWMsg *qwMsg) {
dengyihao's avatar
dengyihao 已提交
725 726
  int32_t     code = 0;
  bool        rsped = false;
D
dapan1121 已提交
727
  SQWTaskCtx *ctx = NULL;
dengyihao's avatar
dengyihao 已提交
728
  bool        locked = false;
D
dapan1121 已提交
729

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

D
dapan1121 已提交
732 733 734 735 736
  QW_LOCK(QW_WRITE, &ctx->lock);

  locked = true;

  if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) {
D
dapan1121 已提交
737
    QW_TASK_WLOG_E("task already dropping");
D
dapan1121 已提交
738 739 740
    QW_ERR_JRET(TSDB_CODE_QRY_DUPLICATTED_OPERATION);
  }

D
dapan1121 已提交
741
  if (QW_IS_QUERY_RUNNING(ctx)) {
D
dapan1121 已提交
742
    QW_ERR_JRET(qwKillTaskHandle(QW_FPARAMS(), ctx));
D
dapan1121 已提交
743
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_DROP);
D
dapan1121 已提交
744
  } else if (ctx->phase > 0) {
D
dapan1121 已提交
745
    QW_ERR_JRET(qwDropTask(QW_FPARAMS()));
D
dapan1121 已提交
746
    rsped = true;
D
dapan1121 已提交
747 748
  } else {
    // task not started
D
dapan1121 已提交
749
  }
D
dapan1121 已提交
750

D
dapan1121 已提交
751
  if (!rsped) {
752
    ctx->ctrlConnInfo = qwMsg->connInfo;
dengyihao's avatar
dengyihao 已提交
753

D
dapan1121 已提交
754 755
    QW_SET_EVENT_RECEIVED(ctx, QW_EVENT_DROP);
  }
756

D
dapan1121 已提交
757
_return:
D
dapan1121 已提交
758

D
dapan1121 已提交
759
  if (code) {
D
dapan1121 已提交
760 761 762
    if (ctx) {
      QW_UPDATE_RSP_CODE(ctx, code);
    }
H
Haojun Liao 已提交
763

D
dapan1121 已提交
764
    qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_FAIL);
D
dapan1121 已提交
765 766
  }

D
dapan 已提交
767 768 769 770
  if (locked) {
    QW_UNLOCK(QW_WRITE, &ctx->lock);
  }

D
dapan1121 已提交
771
  if (ctx) {
D
dapan1121 已提交
772
    qwReleaseTaskCtx(mgmt, ctx);
D
dapan1121 已提交
773 774
  }

D
dapan1121 已提交
775 776 777
  QW_RET(TSDB_CODE_SUCCESS);
}

D
dapan1121 已提交
778
int32_t qwProcessHb(SQWorker *mgmt, SQWMsg *qwMsg, SSchedulerHbReq *req) {
dengyihao's avatar
dengyihao 已提交
779
  int32_t         code = 0;
D
dapan1121 已提交
780
  SSchedulerHbRsp rsp = {0};
781
  SQWSchStatus   *sch = NULL;
D
dapan1121 已提交
782

D
dapan1121 已提交
783 784 785
  if (qwMsg->code) {
    QW_RET(qwProcessHbLinkBroken(mgmt, qwMsg, req));
  }
D
dapan1121 已提交
786 787

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

D
dapan1121 已提交
790 791
  sch->hbBrokenTs = 0;

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

D
dapan1121 已提交
794
  if (sch->hbConnInfo.handle) {
795
    tmsgReleaseHandle(&sch->hbConnInfo, TAOS_CONN_SERVER);
S
Shengliang Guan 已提交
796
    sch->hbConnInfo.handle = NULL;
D
dapan1121 已提交
797
  }
D
dapan1121 已提交
798

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

D
dapan1121 已提交
802
  QW_UNLOCK(QW_WRITE, &sch->hbConnLock);
dengyihao's avatar
dengyihao 已提交
803 804 805

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

D
dapan1121 已提交
807 808 809 810
  qwReleaseScheduler(QW_READ, mgmt);

_return:

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

S
shm  
Shengliang Guan 已提交
813
  qwBuildAndSendHbRsp(&qwMsg->connInfo, &rsp, code);
D
dapan1121 已提交
814 815

  if (code) {
816
    tmsgReleaseHandle(&qwMsg->connInfo, TAOS_CONN_SERVER);
S
Shengliang Guan 已提交
817
    qwMsg->connInfo.handle = NULL;
D
dapan1121 已提交
818
  }
dengyihao's avatar
dengyihao 已提交
819

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

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

void qwProcessHbTimerEvent(void *param, void *tmrId) {
826
  SQWHbParam *hbParam = (SQWHbParam *)param;
D
dapan1121 已提交
827 828 829 830
  if (hbParam->qwrId != atomic_load_32(&gQwMgmt.qwRef)) {
    return;
  }

831
  int64_t   refId = hbParam->refId;
D
dapan1121 已提交
832 833 834 835 836
  SQWorker *mgmt = qwAcquire(refId);
  if (NULL == mgmt) {
    QW_DLOG("qwAcquire %" PRIx64 "failed", refId);
    return;
  }
837

D
dapan1121 已提交
838
  SQWSchStatus *sch = NULL;
dengyihao's avatar
dengyihao 已提交
839
  int32_t       taskNum = 0;
840
  SQWHbInfo    *rspList = NULL;
D
dapan1121 已提交
841
  SArray       *pExpiredSch = NULL;
dengyihao's avatar
dengyihao 已提交
842
  int32_t       code = 0;
D
dapan1121 已提交
843

D
dapan1121 已提交
844 845
  qwDbgDumpMgmtInfo(mgmt);

D
dapan1121 已提交
846 847 848 849 850
  QW_LOCK(QW_READ, &mgmt->schLock);

  int32_t schNum = taosHashGetSize(mgmt->schHash);
  if (schNum <= 0) {
    QW_UNLOCK(QW_READ, &mgmt->schLock);
D
dapan1121 已提交
851
    taosTmrReset(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, param, mgmt->timer, &mgmt->hbTimer);
D
dapan1121 已提交
852
    qwRelease(refId);
D
dapan1121 已提交
853
    return;
D
dapan1121 已提交
854 855
  }

wafwerar's avatar
wafwerar 已提交
856
  rspList = taosMemoryCalloc(schNum, sizeof(SQWHbInfo));
D
dapan1121 已提交
857 858
  pExpiredSch = taosArrayInit(schNum, sizeof(uint64_t));
  if (NULL == rspList || NULL == pExpiredSch) {
D
dapan1121 已提交
859
    QW_UNLOCK(QW_READ, &mgmt->schLock);
D
dapan1121 已提交
860 861
    taosMemoryFree(rspList);
    taosArrayDestroy(pExpiredSch);
D
dapan1121 已提交
862 863
    QW_ELOG("calloc %d SQWHbInfo failed", schNum);
    taosTmrReset(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, param, mgmt->timer, &mgmt->hbTimer);
D
dapan1121 已提交
864
    qwRelease(refId);
D
dapan1121 已提交
865
    return;
D
dapan1121 已提交
866 867
  }

868
  void   *key = NULL;
dengyihao's avatar
dengyihao 已提交
869
  size_t  keyLen = 0;
D
dapan1121 已提交
870
  int32_t i = 0;
D
dapan1121 已提交
871
  int64_t currentMs = taosGetTimestampMs();
D
dapan1121 已提交
872 873 874

  void *pIter = taosHashIterate(mgmt->schHash, NULL);
  while (pIter) {
D
dapan1121 已提交
875 876 877
    SQWSchStatus *sch = (SQWSchStatus *)pIter;
    if (NULL == sch->hbConnInfo.handle) {
      uint64_t *sId = taosHashGetKey(pIter, NULL);
D
dapan1121 已提交
878
      QW_TLOG("cancel send hb to sch %" PRIx64 " cause of no connection handle", *sId);
D
dapan1121 已提交
879 880 881 882 883

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

D
dapan1121 已提交
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
    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 已提交
903
    qwBuildAndSendHbRsp(&rspList[j].connInfo, &rspList[j].rsp, code);
904 905
    /*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 已提交
906
    tFreeSSchedulerHbRsp(&rspList[j].rsp);
D
dapan1121 已提交
907 908
  }

D
dapan1121 已提交
909
  if (taosArrayGetSize(pExpiredSch) > 0) {
D
dapan1121 已提交
910
    qwClearExpiredSch(mgmt, pExpiredSch);
D
dapan1121 已提交
911 912
  }

wafwerar's avatar
wafwerar 已提交
913
  taosMemoryFreeClear(rspList);
D
dapan1121 已提交
914
  taosArrayDestroy(pExpiredSch);
D
dapan1121 已提交
915

dengyihao's avatar
dengyihao 已提交
916
  taosTmrReset(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, param, mgmt->timer, &mgmt->hbTimer);
917
  qwRelease(refId);
D
dapan1121 已提交
918 919
}

920
int32_t qwProcessDelete(QW_FPARAMS_DEF, SQWMsg *qwMsg, SDeleteRes *pRes) {
D
dapan1121 已提交
921 922 923 924 925 926 927 928 929 930 931 932 933
  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 已提交
934
  ctx.plan = plan;
D
dapan1121 已提交
935
  
936
  code = qCreateExecTask(qwMsg->node, mgmt->nodeId, tId, plan, &pTaskInfo, &sinkHandle, NULL, OPTR_EXEC_MODEL_BATCH);
D
dapan1121 已提交
937 938 939 940 941 942 943 944 945 946
  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 已提交
947 948
  ctx.taskHandle = pTaskInfo;
  ctx.sinkHandle = sinkHandle;
D
dapan1121 已提交
949

D
dapan1121 已提交
950
  QW_ERR_JRET(qwExecTask(QW_FPARAMS(), &ctx, NULL));
D
dapan1121 已提交
951

952
  QW_ERR_JRET(qwGetDeleteResFromSink(QW_FPARAMS(), &ctx, pRes));
D
dapan1121 已提交
953 954 955 956 957 958 959 960

_return:

  qwFreeTaskCtx(QW_FPARAMS(), &ctx);

  QW_RET(TSDB_CODE_SUCCESS);
}

D
dapan1121 已提交
961

S
Shengliang Guan 已提交
962
int32_t qWorkerInit(int8_t nodeType, int32_t nodeId, SQWorkerCfg *cfg, void **qWorkerMgmt, const SMsgCb *pMsgCb) {
963
  if (NULL == qWorkerMgmt || pMsgCb->mgmt == NULL) {
D
dapan1121 已提交
964 965 966
    qError("invalid param to init qworker");
    QW_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }
S
Shengliang 已提交
967

D
dapan1121 已提交
968 969 970 971
  int32_t qwNum = atomic_add_fetch_32(&gQwMgmt.qwNum, 1);
  if (1 == qwNum) {
    memset(gQwMgmt.param, 0, sizeof(gQwMgmt.param));
  }
D
dapan1121 已提交
972 973 974 975 976 977 978 979

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

  SQWorker *mgmt = taosMemoryCalloc(1, sizeof(SQWorker));
D
dapan1121 已提交
980
  if (NULL == mgmt) {
D
dapan1121 已提交
981 982
    qError("calloc %d failed", (int32_t)sizeof(SQWorker));
    atomic_sub_fetch_32(&gQwMgmt.qwNum, 1);
D
dapan1121 已提交
983
    QW_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
984 985 986 987
  }

  if (cfg) {
    mgmt->cfg = *cfg;
D
dapan1121 已提交
988
    if (0 == mgmt->cfg.maxSchedulerNum) {
D
dapan1121 已提交
989
      mgmt->cfg.maxSchedulerNum = QW_DEFAULT_SCHEDULER_NUMBER;
D
dapan1121 已提交
990 991
    }
    if (0 == mgmt->cfg.maxTaskNum) {
D
dapan1121 已提交
992
      mgmt->cfg.maxTaskNum = QW_DEFAULT_TASK_NUMBER;
D
dapan1121 已提交
993 994
    }
    if (0 == mgmt->cfg.maxSchTaskNum) {
D
dapan1121 已提交
995
      mgmt->cfg.maxSchTaskNum = QW_DEFAULT_SCH_TASK_NUMBER;
D
dapan1121 已提交
996
    }
D
dapan1121 已提交
997
  } else {
D
dapan1121 已提交
998 999 1000
    mgmt->cfg.maxSchedulerNum = QW_DEFAULT_SCHEDULER_NUMBER;
    mgmt->cfg.maxTaskNum = QW_DEFAULT_TASK_NUMBER;
    mgmt->cfg.maxSchTaskNum = QW_DEFAULT_SCH_TASK_NUMBER;
D
dapan1121 已提交
1001 1002
  }

dengyihao's avatar
dengyihao 已提交
1003 1004
  mgmt->schHash = taosHashInit(mgmt->cfg.maxSchedulerNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false,
                               HASH_ENTRY_LOCK);
D
dapan1121 已提交
1005
  if (NULL == mgmt->schHash) {
wafwerar's avatar
wafwerar 已提交
1006
    taosMemoryFreeClear(mgmt);
D
dapan1121 已提交
1007
    qError("init %d scheduler hash failed", mgmt->cfg.maxSchedulerNum);
D
dapan1121 已提交
1008
    QW_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
1009 1010
  }

dengyihao's avatar
dengyihao 已提交
1011 1012
  mgmt->ctxHash =
      taosHashInit(mgmt->cfg.maxTaskNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
D
dapan1121 已提交
1013
  if (NULL == mgmt->ctxHash) {
D
dapan1121 已提交
1014
    qError("init %d task ctx hash failed", mgmt->cfg.maxTaskNum);
D
dapan1121 已提交
1015 1016 1017 1018 1019 1020 1021 1022 1023
    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 已提交
1024 1025
  mgmt->nodeType = nodeType;
  mgmt->nodeId = nodeId;
S
Shengliang Guan 已提交
1026
  mgmt->msgCb = *pMsgCb;
D
dapan1121 已提交
1027

D
dapan1121 已提交
1028 1029 1030 1031 1032 1033
  mgmt->refId = taosAddRef(gQwMgmt.qwRef, mgmt);
  if (mgmt->refId < 0) {
    qError("taosAddRef qw failed, error:%s", tstrerror(terrno));
    QW_ERR_JRET(terrno);
  }

D
dapan1121 已提交
1034 1035 1036
  SQWHbParam *param = NULL;
  qwSetHbParam(mgmt->refId, &param);

1037
  mgmt->hbTimer = taosTmrStart(qwProcessHbTimerEvent, QW_DEFAULT_HEARTBEAT_MSEC, (void *)param, mgmt->timer);
D
dapan1121 已提交
1038 1039 1040 1041
  if (NULL == mgmt->hbTimer) {
    qError("start hb timer failed");
    QW_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
1042

D
dapan1121 已提交
1043 1044
  *qWorkerMgmt = mgmt;

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

D
dapan1121 已提交
1047
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
1048 1049 1050

_return:

D
dapan1121 已提交
1051 1052 1053 1054 1055 1056 1057
  if (mgmt->refId >= 0) {
    qwRelease(mgmt->refId);
  } else {
    taosHashCleanup(mgmt->schHash);
    taosHashCleanup(mgmt->ctxHash);
    taosTmrCleanUp(mgmt->timer);
    taosMemoryFreeClear(mgmt);
D
dapan1121 已提交
1058

1059
    atomic_sub_fetch_32(&gQwMgmt.qwNum, 1);
D
dapan1121 已提交
1060
  }
1061

D
dapan1121 已提交
1062
  QW_RET(code);
D
dapan1121 已提交
1063
}
D
dapan1121 已提交
1064 1065 1066 1067

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

D
dapan1121 已提交
1070
  SQWorker *mgmt = *qWorkerMgmt;
D
dapan1121 已提交
1071

D
dapan1121 已提交
1072 1073 1074
  if (taosRemoveRef(gQwMgmt.qwRef, mgmt->refId)) {
    qError("remove qw from ref list failed, refId:%" PRIx64, mgmt->refId);
  }
D
dapan1121 已提交
1075
}
D
dapan1121 已提交
1076

D
dapan1121 已提交
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
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 已提交
1093
  pStat->deleteProcessed = QW_STAT_GET(mgmt->stat.msgStat.deleteProcessed);
D
dapan1121 已提交
1094 1095 1096 1097 1098 1099 1100

  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 已提交
1101 1102 1103
}


dengyihao's avatar
dengyihao 已提交
1104