qwMsg.c 18.1 KB
Newer Older
D
dapan1121 已提交
1
#include "qwMsg.h"
L
Liu Jicong 已提交
2
#include "dataSinkMgt.h"
D
dapan1121 已提交
3 4 5
#include "executor.h"
#include "planner.h"
#include "query.h"
L
Liu Jicong 已提交
6
#include "qworker.h"
D
dapan1121 已提交
7
#include "qwInt.h"
L
Liu Jicong 已提交
8
#include "tcommon.h"
D
dapan1121 已提交
9 10 11 12 13
#include "tmsg.h"
#include "tname.h"

int32_t qwMallocFetchRsp(int32_t length, SRetrieveTableRsp **rsp) {
  int32_t msgSize = sizeof(SRetrieveTableRsp) + length;
L
Liu Jicong 已提交
14

D
dapan1121 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27
  SRetrieveTableRsp *pRsp = (SRetrieveTableRsp *)rpcMallocCont(msgSize);
  if (NULL == pRsp) {
    qError("rpcMallocCont %d failed", msgSize);
    QW_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  memset(pRsp, 0, sizeof(SRetrieveTableRsp));

  *rsp = pRsp;

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
28
void qwBuildFetchRsp(void *msg, SOutputData *input, int32_t len, bool qComplete) {
D
dapan1121 已提交
29
  SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)msg;
L
Liu Jicong 已提交
30

D
dapan1121 已提交
31
  rsp->useconds = htobe64(input->useconds);
D
dapan1121 已提交
32
  rsp->completed = qComplete;
D
dapan1121 已提交
33 34 35 36
  rsp->precision = input->precision;
  rsp->compressed = input->compressed;
  rsp->compLen = htonl(len);
  rsp->numOfRows = htonl(input->numOfRows);
37
  rsp->numOfCols = htonl(input->numOfCols);
D
dapan1121 已提交
38 39 40
}

void qwFreeFetchRsp(void *msg) {
D
dapan 已提交
41 42 43
  if (msg) {
    rpcFreeCont(msg);
  }
D
dapan1121 已提交
44 45
}

D
dapan1121 已提交
46 47 48
int32_t qwBuildAndSendQueryRsp(int32_t rspType, SRpcHandleInfo *pConn, int32_t code, SQWTaskCtx *ctx) {
  STbVerInfo* tbInfo = ctx ? &ctx->tbInfo : NULL;
  int64_t affectedRows = ctx ? ctx->affectedRows : 0;
D
dapan1121 已提交
49
  SQueryTableRsp *pRsp = (SQueryTableRsp *)rpcMallocCont(sizeof(SQueryTableRsp));
D
dapan1121 已提交
50 51
  pRsp->code = htonl(code);
  pRsp->affectedRows = htobe64(affectedRows);
52 53
  if (tbInfo) {
    strcpy(pRsp->tbFName, tbInfo->tbFName);
D
dapan1121 已提交
54 55
    pRsp->sversion = htonl(tbInfo->sversion);
    pRsp->tversion = htonl(tbInfo->tversion);
56
  }
D
dapan1121 已提交
57 58

  SRpcMsg rpcRsp = {
D
dapan1121 已提交
59
      .msgType = rspType,
L
Liu Jicong 已提交
60 61 62
      .pCont = pRsp,
      .contLen = sizeof(*pRsp),
      .code = code,
63
      .info = *pConn,
D
dapan1121 已提交
64 65
  };

S
shm  
Shengliang Guan 已提交
66
  tmsgSendRsp(&rpcRsp);
D
dapan1121 已提交
67 68 69 70

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
71
int32_t qwBuildAndSendExplainRsp(SRpcHandleInfo *pConn, SExplainExecInfo *execInfo, int32_t num) {
D
dapan1121 已提交
72 73 74
  SExplainRsp rsp = {.numOfPlans = num, .subplanInfo = execInfo};

  int32_t contLen = tSerializeSExplainRsp(NULL, 0, &rsp);
dengyihao's avatar
dengyihao 已提交
75
  void *  pRsp = rpcMallocCont(contLen);
D
dapan1121 已提交
76 77 78
  tSerializeSExplainRsp(pRsp, contLen, &rsp);

  SRpcMsg rpcRsp = {
D
dapan1121 已提交
79
      .msgType = TDMT_SCH_EXPLAIN_RSP,
L
Liu Jicong 已提交
80 81 82
      .pCont = pRsp,
      .contLen = contLen,
      .code = 0,
83
      .info = *pConn,
D
dapan1121 已提交
84
  };
85
  rpcRsp.info.ahandle = NULL;
D
dapan1121 已提交
86 87 88 89 90 91

  tmsgSendRsp(&rpcRsp);

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
92
int32_t qwBuildAndSendHbRsp(SRpcHandleInfo *pConn, SSchedulerHbRsp *pStatus, int32_t code) {
D
dapan1121 已提交
93
  int32_t contLen = tSerializeSSchedulerHbRsp(NULL, 0, pStatus);
dengyihao's avatar
dengyihao 已提交
94
  void *  pRsp = rpcMallocCont(contLen);
D
dapan1121 已提交
95
  tSerializeSSchedulerHbRsp(pRsp, contLen, pStatus);
D
dapan1121 已提交
96 97

  SRpcMsg rpcRsp = {
D
dapan1121 已提交
98
      .msgType = TDMT_SCH_QUERY_HEARTBEAT_RSP,
L
Liu Jicong 已提交
99
      .contLen = contLen,
100
      .pCont = pRsp,
L
Liu Jicong 已提交
101
      .code = code,
102
      .info = *pConn,
D
dapan1121 已提交
103 104
  };

S
shm  
Shengliang Guan 已提交
105
  tmsgSendRsp(&rpcRsp);
D
dapan1121 已提交
106 107 108 109

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
110
int32_t qwBuildAndSendFetchRsp(int32_t rspType, SRpcHandleInfo *pConn, SRetrieveTableRsp *pRsp, int32_t dataLength, int32_t code) {
D
dapan1121 已提交
111 112 113 114 115 116 117
  if (NULL == pRsp) {
    pRsp = (SRetrieveTableRsp *)rpcMallocCont(sizeof(SRetrieveTableRsp));
    memset(pRsp, 0, sizeof(SRetrieveTableRsp));
    dataLength = 0;
  }

  SRpcMsg rpcRsp = {
D
dapan1121 已提交
118
      .msgType = rspType,
L
Liu Jicong 已提交
119 120 121
      .pCont = pRsp,
      .contLen = sizeof(*pRsp) + dataLength,
      .code = code,
122
      .info = *pConn,
D
dapan1121 已提交
123 124
  };

S
shm  
Shengliang Guan 已提交
125
  tmsgSendRsp(&rpcRsp);
D
dapan1121 已提交
126 127 128 129

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
130
int32_t qwBuildAndSendCancelRsp(SRpcHandleInfo *pConn, int32_t code) {
D
dapan1121 已提交
131 132 133 134
  STaskCancelRsp *pRsp = (STaskCancelRsp *)rpcMallocCont(sizeof(STaskCancelRsp));
  pRsp->code = code;

  SRpcMsg rpcRsp = {
D
dapan1121 已提交
135
      .msgType = TDMT_SCH_CANCEL_TASK_RSP,
L
Liu Jicong 已提交
136 137 138
      .pCont = pRsp,
      .contLen = sizeof(*pRsp),
      .code = code,
139
      .info = *pConn,
D
dapan1121 已提交
140 141
  };

S
shm  
Shengliang Guan 已提交
142
  tmsgSendRsp(&rpcRsp);
D
dapan1121 已提交
143 144 145
  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
146
int32_t qwBuildAndSendDropRsp(SRpcHandleInfo *pConn, int32_t code) {
D
dapan1121 已提交
147 148 149 150
  STaskDropRsp *pRsp = (STaskDropRsp *)rpcMallocCont(sizeof(STaskDropRsp));
  pRsp->code = code;

  SRpcMsg rpcRsp = {
D
dapan1121 已提交
151
      .msgType = TDMT_SCH_DROP_TASK_RSP,
L
Liu Jicong 已提交
152 153 154
      .pCont = pRsp,
      .contLen = sizeof(*pRsp),
      .code = code,
155
      .info = *pConn,
D
dapan1121 已提交
156 157
  };

S
shm  
Shengliang Guan 已提交
158
  tmsgSendRsp(&rpcRsp);
D
dapan1121 已提交
159 160 161
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
int32_t qwBuildAndSendDropMsg(QW_FPARAMS_DEF, SRpcHandleInfo *pConn) {
  STaskDropReq *req = (STaskDropReq *)rpcMallocCont(sizeof(STaskDropReq));
  if (NULL == req) {
    QW_SCH_TASK_ELOG("rpcMallocCont %d failed", (int32_t)sizeof(STaskDropReq));
    QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  req->header.vgId = mgmt->nodeId;
  req->sId = sId;
  req->queryId = qId;
  req->taskId = tId;
  req->refId = rId;
  req->execId = eId;

  SRpcMsg pNewMsg = {
      .msgType = TDMT_SCH_DROP_TASK,
      .pCont = req,
      .contLen = sizeof(STaskDropReq),
      .code = 0,
      .info = *pConn,
  };

  int32_t code = tmsgPutToQueue(&mgmt->msgCb, FETCH_QUEUE, &pNewMsg);
  if (TSDB_CODE_SUCCESS != code) {
    QW_SCH_TASK_ELOG("put drop task msg to queue failed, vgId:%d, code:%s", mgmt->nodeId, tstrerror(code));
    rpcFreeCont(req);
    QW_ERR_RET(code);
  }

  QW_SCH_TASK_DLOG("drop task msg put to queue, vgId:%d", mgmt->nodeId);

  return TSDB_CODE_SUCCESS;
}


S
Shengliang Guan 已提交
197
int32_t qwBuildAndSendCQueryMsg(QW_FPARAMS_DEF, SRpcHandleInfo *pConn) {
L
Liu Jicong 已提交
198
  SQueryContinueReq *req = (SQueryContinueReq *)rpcMallocCont(sizeof(SQueryContinueReq));
D
dapan1121 已提交
199 200 201 202 203 204 205 206 207
  if (NULL == req) {
    QW_SCH_TASK_ELOG("rpcMallocCont %d failed", (int32_t)sizeof(SQueryContinueReq));
    QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  req->header.vgId = mgmt->nodeId;
  req->sId = sId;
  req->queryId = qId;
  req->taskId = tId;
D
dapan1121 已提交
208
  req->execId = eId;
D
dapan1121 已提交
209 210

  SRpcMsg pNewMsg = {
D
dapan1121 已提交
211
      .msgType = TDMT_SCH_QUERY_CONTINUE,
L
Liu Jicong 已提交
212 213 214
      .pCont = req,
      .contLen = sizeof(SQueryContinueReq),
      .code = 0,
215
      .info = *pConn,
D
dapan1121 已提交
216 217
  };

S
Shengliang Guan 已提交
218
  int32_t code = tmsgPutToQueue(&mgmt->msgCb, QUERY_QUEUE, &pNewMsg);
D
dapan1121 已提交
219
  if (TSDB_CODE_SUCCESS != code) {
H
Haojun Liao 已提交
220
    QW_SCH_TASK_ELOG("put query continue msg to queue failed, vgId:%d, code:%s", mgmt->nodeId, tstrerror(code));
D
dapan1121 已提交
221 222 223 224
    rpcFreeCont(req);
    QW_ERR_RET(code);
  }

D
dapan1121 已提交
225
  QW_SCH_TASK_DLOG("query continue msg put to queue, vgId:%d", mgmt->nodeId);
D
dapan1121 已提交
226 227 228 229

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
230
int32_t qwRegisterQueryBrokenLinkArg(QW_FPARAMS_DEF, SRpcHandleInfo *pConn) {
L
Liu Jicong 已提交
231
  STaskDropReq *req = (STaskDropReq *)rpcMallocCont(sizeof(STaskDropReq));
D
dapan1121 已提交
232 233 234
  if (NULL == req) {
    QW_SCH_TASK_ELOG("rpcMallocCont %d failed", (int32_t)sizeof(STaskDropReq));
    QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
L
Liu Jicong 已提交
235
  }
D
dapan1121 已提交
236

D
dapan1121 已提交
237 238 239 240 241
  req->header.vgId = htonl(mgmt->nodeId);
  req->sId = htobe64(sId);
  req->queryId = htobe64(qId);
  req->taskId = htobe64(tId);
  req->refId = htobe64(rId);
L
Liu Jicong 已提交
242

S
Shengliang Guan 已提交
243
  SRpcMsg brokenMsg = {
D
dapan1121 已提交
244
      .msgType = TDMT_SCH_DROP_TASK,
L
Liu Jicong 已提交
245 246
      .pCont = req,
      .contLen = sizeof(STaskDropReq),
D
dapan1121 已提交
247
      .code = TSDB_CODE_RPC_BROKEN_LINK,
248
      .info = *pConn,
D
dapan1121 已提交
249
  };
L
Liu Jicong 已提交
250

S
Shengliang Guan 已提交
251
  tmsgRegisterBrokenLinkArg(&brokenMsg);
D
dapan1121 已提交
252 253 254 255

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
256
int32_t qwRegisterHbBrokenLinkArg(SQWorker *mgmt, uint64_t sId, SRpcHandleInfo *pConn) {
D
dapan1121 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
  SSchedulerHbReq req = {0};
  req.header.vgId = mgmt->nodeId;
  req.sId = sId;

  int32_t msgSize = tSerializeSSchedulerHbReq(NULL, 0, &req);
  if (msgSize < 0) {
    QW_SCH_ELOG("tSerializeSSchedulerHbReq hbReq failed, size:%d", msgSize);
    QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
  void *msg = rpcMallocCont(msgSize);
  if (NULL == msg) {
    QW_SCH_ELOG("calloc %d failed", msgSize);
    QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
  if (tSerializeSSchedulerHbReq(msg, msgSize, &req) < 0) {
    QW_SCH_ELOG("tSerializeSSchedulerHbReq hbReq failed, size:%d", msgSize);
    taosMemoryFree(msg);
    QW_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
L
Liu Jicong 已提交
276

S
Shengliang Guan 已提交
277
  SRpcMsg brokenMsg = {
D
dapan1121 已提交
278
      .msgType = TDMT_SCH_QUERY_HEARTBEAT,
L
Liu Jicong 已提交
279 280
      .pCont = msg,
      .contLen = msgSize,
D
dapan1121 已提交
281
      .code = TSDB_CODE_RPC_BROKEN_LINK,
282
      .info = *pConn,
D
dapan1121 已提交
283
  };
L
Liu Jicong 已提交
284

S
Shengliang Guan 已提交
285
  tmsgRegisterBrokenLinkArg(&brokenMsg);
D
dapan1121 已提交
286 287 288 289

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
int32_t qWorkerPreprocessQueryMsg(void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == qWorkerMgmt || NULL == pMsg) {
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  int32_t       code = 0;
  SSubQueryMsg *msg = pMsg->pCont;
  SQWorker *    mgmt = (SQWorker *)qWorkerMgmt;

  if (NULL == msg || pMsg->contLen <= sizeof(*msg)) {
    QW_ELOG("invalid query msg, msg:%p, msgLen:%d", msg, pMsg->contLen);
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  msg->sId = be64toh(msg->sId);
  msg->queryId = be64toh(msg->queryId);
  msg->taskId = be64toh(msg->taskId);
  msg->refId = be64toh(msg->refId);
D
dapan1121 已提交
308
  msg->execId = ntohl(msg->execId);
D
dapan1121 已提交
309 310 311 312 313 314 315
  msg->phyLen = ntohl(msg->phyLen);
  msg->sqlLen = ntohl(msg->sqlLen);

  uint64_t sId = msg->sId;
  uint64_t qId = msg->queryId;
  uint64_t tId = msg->taskId;
  int64_t  rId = msg->refId;
D
dapan1121 已提交
316
  int32_t  eId = msg->execId;
D
dapan1121 已提交
317

D
dapan1121 已提交
318
  SQWMsg qwMsg = {.msgType = pMsg->msgType, .msg = msg->msg + msg->sqlLen, .msgLen = msg->phyLen, .connInfo = pMsg->info};
D
dapan1121 已提交
319 320

  QW_SCH_TASK_DLOG("prerocessQuery start, handle:%p", pMsg->info.handle);
D
dapan1121 已提交
321
  QW_ERR_RET(qwPreprocessQuery(QW_FPARAMS(), &qwMsg));
D
dapan1121 已提交
322 323 324 325 326
  QW_SCH_TASK_DLOG("prerocessQuery end, handle:%p", pMsg->info.handle);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
327 328 329 330 331 332 333 334 335 336 337 338
int32_t qWorkerAbortPreprocessQueryMsg(void *qWorkerMgmt, SRpcMsg *pMsg) {
  if (NULL == qWorkerMgmt || NULL == pMsg) {
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  SSubQueryMsg *msg = pMsg->pCont;
  SQWorker *    mgmt = (SQWorker *)qWorkerMgmt;

  uint64_t sId = msg->sId;
  uint64_t qId = msg->queryId;
  uint64_t tId = msg->taskId;
  int64_t  rId = msg->refId;
D
dapan1121 已提交
339
  int32_t  eId = msg->execId;
D
dapan1121 已提交
340 341 342 343 344 345 346 347

  QW_SCH_TASK_DLOG("Abort prerocessQuery start, handle:%p", pMsg->info.handle);
  qwAbortPrerocessQuery(QW_FPARAMS());
  QW_SCH_TASK_DLOG("Abort prerocessQuery end, handle:%p", pMsg->info.handle);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
348
int32_t qWorkerProcessQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg, int64_t ts) {
D
dapan1121 已提交
349 350 351 352
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

L
Liu Jicong 已提交
353
  int32_t       code = 0;
D
dapan1121 已提交
354
  SSubQueryMsg *msg = pMsg->pCont;
dengyihao's avatar
dengyihao 已提交
355
  SQWorker *    mgmt = (SQWorker *)qWorkerMgmt;
L
Liu Jicong 已提交
356

D
dapan1121 已提交
357 358
  qwUpdateTimeInQueue(mgmt, ts, QUERY_QUEUE);
  QW_STAT_INC(mgmt->stat.msgStat.queryProcessed, 1);
D
dapan1121 已提交
359

D
dapan1121 已提交
360
  if (NULL == msg || pMsg->contLen <= sizeof(*msg)) {
D
dapan1121 已提交
361
    QW_ELOG("invalid query msg, msg:%p, msgLen:%d", msg, pMsg->contLen);
D
dapan1121 已提交
362
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
363 364 365 366 367
  }

  uint64_t sId = msg->sId;
  uint64_t qId = msg->queryId;
  uint64_t tId = msg->taskId;
D
dapan1121 已提交
368
  int64_t  rId = msg->refId;
D
dapan1121 已提交
369
  int32_t  eId = msg->execId;
D
dapan1121 已提交
370

D
dapan1121 已提交
371
  SQWMsg qwMsg = {.node = node, .msg = msg->msg + msg->sqlLen, .msgLen = msg->phyLen, .connInfo = pMsg->info, .msgType = pMsg->msgType};
D
dapan1121 已提交
372 373 374 375
  qwMsg.msgInfo.explain = msg->explain;
  qwMsg.msgInfo.taskType = msg->taskType;
  qwMsg.msgInfo.needFetch = msg->needFetch;
  
dengyihao's avatar
dengyihao 已提交
376
  char * sql = strndup(msg->msg, msg->sqlLen);
D
dapan1121 已提交
377
  QW_SCH_TASK_DLOG("processQuery start, node:%p, type:%s, handle:%p, SQL:%s", node, TMSG_INFO(pMsg->msgType), pMsg->info.handle, sql);
D
dapan1121 已提交
378
  QW_ERR_RET(qwProcessQuery(QW_FPARAMS(), &qwMsg, sql));
D
dapan1121 已提交
379
  QW_SCH_TASK_DLOG("processQuery end, node:%p", node);
380

L
Liu Jicong 已提交
381
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
382 383
}

D
dapan1121 已提交
384
int32_t qWorkerProcessCQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg, int64_t ts) {
L
Liu Jicong 已提交
385 386 387
  int32_t            code = 0;
  int8_t             status = 0;
  bool               queryDone = false;
388
  SQueryContinueReq *msg = (SQueryContinueReq *)pMsg->pCont;
L
Liu Jicong 已提交
389
  bool               needStop = false;
dengyihao's avatar
dengyihao 已提交
390 391
  SQWTaskCtx *       handles = NULL;
  SQWorker *         mgmt = (SQWorker *)qWorkerMgmt;
D
dapan1121 已提交
392

D
dapan1121 已提交
393 394
  qwUpdateTimeInQueue(mgmt, ts, QUERY_QUEUE);
  QW_STAT_INC(mgmt->stat.msgStat.cqueryProcessed, 1);
D
dapan1121 已提交
395

D
dapan1121 已提交
396
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
D
dapan1121 已提交
397
    QW_ELOG("invalid cquery msg, msg:%p, msgLen:%d", msg, pMsg->contLen);
D
dapan1121 已提交
398
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
399 400
  }

401 402 403
  uint64_t sId = msg->sId;
  uint64_t qId = msg->queryId;
  uint64_t tId = msg->taskId;
L
Liu Jicong 已提交
404
  int64_t  rId = 0;
D
dapan1121 已提交
405
  int32_t  eId = msg->execId;
D
dapan1121 已提交
406

407
  SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0, .connInfo = pMsg->info};
D
dapan1121 已提交
408

S
Shengliang Guan 已提交
409
  QW_SCH_TASK_DLOG("processCQuery start, node:%p, handle:%p", node, pMsg->info.handle);
410 411 412

  QW_ERR_RET(qwProcessCQuery(QW_FPARAMS(), &qwMsg));

D
dapan1121 已提交
413
  QW_SCH_TASK_DLOG("processCQuery end, node:%p", node);
414

L
Liu Jicong 已提交
415
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
416 417
}

D
dapan1121 已提交
418
int32_t qWorkerProcessFetchMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg, int64_t ts) {
D
dapan1121 已提交
419 420 421 422 423
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

  SResFetchReq *msg = pMsg->pCont;
dengyihao's avatar
dengyihao 已提交
424
  SQWorker *    mgmt = (SQWorker *)qWorkerMgmt;
L
Liu Jicong 已提交
425

D
dapan1121 已提交
426 427
  qwUpdateTimeInQueue(mgmt, ts, FETCH_QUEUE);
  QW_STAT_INC(mgmt->stat.msgStat.fetchProcessed, 1);
D
dapan1121 已提交
428

D
dapan1121 已提交
429
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
L
Liu Jicong 已提交
430
    QW_ELOG("invalid fetch msg, msg:%p, msgLen:%d", msg, pMsg->contLen);
D
dapan1121 已提交
431
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
L
Liu Jicong 已提交
432
  }
D
dapan1121 已提交
433

D
dapan1121 已提交
434 435 436
  msg->sId = be64toh(msg->sId);
  msg->queryId = be64toh(msg->queryId);
  msg->taskId = be64toh(msg->taskId);
D
dapan1121 已提交
437
  msg->execId = ntohl(msg->execId);
D
dapan1121 已提交
438 439 440 441

  uint64_t sId = msg->sId;
  uint64_t qId = msg->queryId;
  uint64_t tId = msg->taskId;
L
Liu Jicong 已提交
442
  int64_t  rId = 0;
D
dapan1121 已提交
443
  int32_t  eId = msg->execId;
D
dapan1121 已提交
444

D
dapan1121 已提交
445
  SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0, .connInfo = pMsg->info, .msgType = pMsg->msgType};
D
dapan1121 已提交
446

S
Shengliang Guan 已提交
447
  QW_SCH_TASK_DLOG("processFetch start, node:%p, handle:%p", node, pMsg->info.handle);
448 449 450

  QW_ERR_RET(qwProcessFetch(QW_FPARAMS(), &qwMsg));

D
dapan1121 已提交
451
  QW_SCH_TASK_DLOG("processFetch end, node:%p", node);
452

L
Liu Jicong 已提交
453
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
454 455
}

D
dapan1121 已提交
456
int32_t qWorkerProcessRspMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg, int64_t ts) {
D
dapan1121 已提交
457
  SQWorker *      mgmt = (SQWorker *)qWorkerMgmt;
D
dapan1121 已提交
458 459
  if (mgmt) {
    qwUpdateTimeInQueue(mgmt, ts, FETCH_QUEUE);
D
dapan1121 已提交
460
    QW_STAT_INC(mgmt->stat.msgStat.rspProcessed, 1);
D
dapan1121 已提交
461
  }
D
dapan1121 已提交
462

D
dapan1121 已提交
463
  qProcessRspMsg(NULL, pMsg, NULL);
464
  pMsg->pCont = NULL;
S
Shengliang 已提交
465 466 467
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
468
int32_t qWorkerProcessCancelMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg, int64_t ts) {
D
dapan1121 已提交
469 470 471 472
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

dengyihao's avatar
dengyihao 已提交
473
  SQWorker *      mgmt = (SQWorker *)qWorkerMgmt;
L
Liu Jicong 已提交
474
  int32_t         code = 0;
D
dapan1121 已提交
475
  STaskCancelReq *msg = pMsg->pCont;
D
dapan1121 已提交
476

D
dapan1121 已提交
477 478
  qwUpdateTimeInQueue(mgmt, ts, FETCH_QUEUE);
  QW_STAT_INC(mgmt->stat.msgStat.cancelProcessed, 1);
D
dapan1121 已提交
479

D
dapan1121 已提交
480
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
L
Liu Jicong 已提交
481
    qError("invalid task cancel msg");
D
dapan1121 已提交
482
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
L
Liu Jicong 已提交
483
  }
D
dapan1121 已提交
484

D
dapan1121 已提交
485 486 487 488
  msg->sId = be64toh(msg->sId);
  msg->queryId = be64toh(msg->queryId);
  msg->taskId = be64toh(msg->taskId);
  msg->refId = be64toh(msg->refId);
D
dapan1121 已提交
489
  msg->execId = ntohl(msg->execId);
D
dapan1121 已提交
490

D
dapan1121 已提交
491 492 493 494
  uint64_t sId = msg->sId;
  uint64_t qId = msg->queryId;
  uint64_t tId = msg->taskId;
  int64_t  rId = msg->refId;
D
dapan1121 已提交
495
  int32_t  eId = msg->execId;
D
dapan1121 已提交
496

497
  SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0, .connInfo = pMsg->info};
D
dapan1121 已提交
498

L
Liu Jicong 已提交
499
  // QW_ERR_JRET(qwCancelTask(qWorkerMgmt, msg->sId, msg->queryId, msg->taskId));
D
dapan1121 已提交
500 501 502

_return:

S
shm  
Shengliang Guan 已提交
503
  QW_ERR_RET(qwBuildAndSendCancelRsp(&qwMsg.connInfo, code));
D
dapan1121 已提交
504
  QW_SCH_TASK_DLOG("cancel rsp send, handle:%p, code:%x - %s", qwMsg.connInfo.handle, code, tstrerror(code));
D
dapan1121 已提交
505 506 507 508

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
509
int32_t qWorkerProcessDropMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg, int64_t ts) {
D
dapan1121 已提交
510 511 512 513
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

L
Liu Jicong 已提交
514
  int32_t       code = 0;
D
dapan1121 已提交
515
  STaskDropReq *msg = pMsg->pCont;
dengyihao's avatar
dengyihao 已提交
516
  SQWorker *    mgmt = (SQWorker *)qWorkerMgmt;
L
Liu Jicong 已提交
517

D
dapan1121 已提交
518 519
  qwUpdateTimeInQueue(mgmt, ts, FETCH_QUEUE);
  QW_STAT_INC(mgmt->stat.msgStat.dropProcessed, 1);
D
dapan1121 已提交
520

D
dapan1121 已提交
521
  if (NULL == msg || pMsg->contLen < sizeof(*msg)) {
D
dapan1121 已提交
522
    QW_ELOG("invalid task drop msg, msg:%p, msgLen:%d", msg, pMsg->contLen);
D
dapan1121 已提交
523
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
L
Liu Jicong 已提交
524
  }
D
dapan1121 已提交
525

D
dapan1121 已提交
526 527 528
  msg->sId = be64toh(msg->sId);
  msg->queryId = be64toh(msg->queryId);
  msg->taskId = be64toh(msg->taskId);
D
dapan1121 已提交
529
  msg->refId = be64toh(msg->refId);
D
dapan1121 已提交
530
  msg->execId = ntohl(msg->execId);
D
dapan1121 已提交
531 532 533 534

  uint64_t sId = msg->sId;
  uint64_t qId = msg->queryId;
  uint64_t tId = msg->taskId;
D
dapan1121 已提交
535
  int64_t  rId = msg->refId;
D
dapan1121 已提交
536
  int32_t  eId = msg->execId;
D
dapan1121 已提交
537

538
  SQWMsg qwMsg = {.node = node, .msg = NULL, .msgLen = 0, .code = pMsg->code, .connInfo = pMsg->info};
D
dapan1121 已提交
539

D
dapan1121 已提交
540
  if (TSDB_CODE_RPC_BROKEN_LINK == pMsg->code) {
L
Liu Jicong 已提交
541
    QW_SCH_TASK_DLOG("receive drop task due to network broken, error:%s", tstrerror(pMsg->code));
D
dapan1121 已提交
542
  }
D
dapan1121 已提交
543

S
Shengliang Guan 已提交
544
  QW_SCH_TASK_DLOG("processDrop start, node:%p, handle:%p", node, pMsg->info.handle);
545 546 547

  QW_ERR_RET(qwProcessDrop(QW_FPARAMS(), &qwMsg));

D
dapan1121 已提交
548
  QW_SCH_TASK_DLOG("processDrop end, node:%p", node);
549 550

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
551 552
}

D
dapan1121 已提交
553
int32_t qWorkerProcessHbMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg, int64_t ts) {
D
dapan1121 已提交
554 555 556 557
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
    return TSDB_CODE_QRY_INVALID_INPUT;
  }

L
Liu Jicong 已提交
558
  int32_t         code = 0;
D
dapan1121 已提交
559
  SSchedulerHbReq req = {0};
dengyihao's avatar
dengyihao 已提交
560
  SQWorker *      mgmt = (SQWorker *)qWorkerMgmt;
L
Liu Jicong 已提交
561

D
dapan1121 已提交
562 563
  qwUpdateTimeInQueue(mgmt, ts, FETCH_QUEUE);
  QW_STAT_INC(mgmt->stat.msgStat.hbProcessed, 1);
D
dapan1121 已提交
564

D
dapan1121 已提交
565 566 567
  if (NULL == pMsg->pCont) {
    QW_ELOG("invalid hb msg, msg:%p, msgLen:%d", pMsg->pCont, pMsg->contLen);
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
L
Liu Jicong 已提交
568
  }
D
dapan1121 已提交
569 570 571 572 573 574 575 576

  if (tDeserializeSSchedulerHbReq(pMsg->pCont, pMsg->contLen, &req)) {
    QW_ELOG("invalid hb msg, msg:%p, msgLen:%d", pMsg->pCont, pMsg->contLen);
    tFreeSSchedulerHbReq(&req);
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  uint64_t sId = req.sId;
577
  SQWMsg   qwMsg = {.node = node, .msg = NULL, .msgLen = 0, .code = pMsg->code, .connInfo = pMsg->info};
D
dapan1121 已提交
578
  if (TSDB_CODE_RPC_BROKEN_LINK == pMsg->code) {
L
Liu Jicong 已提交
579
    QW_SCH_DLOG("receive Hb msg due to network broken, error:%s", tstrerror(pMsg->code));
D
dapan1121 已提交
580 581
  }

S
Shengliang Guan 已提交
582
  QW_SCH_DLOG("processHb start, node:%p, handle:%p", node, pMsg->info.handle);
D
dapan1121 已提交
583 584 585 586 587 588 589

  QW_ERR_RET(qwProcessHb(mgmt, &qwMsg, &req));

  QW_SCH_DLOG("processHb end, node:%p", node);

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
590 591


592 593
int32_t qWorkerProcessDeleteMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg, SDeleteRes *pRes) {
  if (NULL == node || NULL == qWorkerMgmt || NULL == pMsg) {
D
dapan1121 已提交
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
    QW_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  int32_t       code = 0;
  SVDeleteReq req = {0};
  SQWorker *    mgmt = (SQWorker *)qWorkerMgmt;

  QW_STAT_INC(mgmt->stat.msgStat.deleteProcessed, 1);

  tDeserializeSVDeleteReq(pMsg->pCont, pMsg->contLen, &req);
  
  uint64_t sId = req.sId;
  uint64_t qId = req.queryId;
  uint64_t tId = req.taskId;
  int64_t  rId = 0;
D
dapan1121 已提交
609
  int32_t  eId = -1;
D
dapan1121 已提交
610 611 612 613 614

  SQWMsg qwMsg = {.node = node, .msg = req.msg, .msgLen = req.phyLen, .connInfo = pMsg->info};
  QW_SCH_TASK_DLOG("processDelete start, node:%p, handle:%p, sql:%s", node, pMsg->info.handle, req.sql);
  taosMemoryFreeClear(req.sql);

615
  QW_ERR_JRET(qwProcessDelete(QW_FPARAMS(), &qwMsg, pRes));
D
dapan1121 已提交
616 617 618 619 620 621 622 623 624

  QW_SCH_TASK_DLOG("processDelete end, node:%p", node);

_return:

  QW_RET(code);
}