schRemote.c 36.7 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "catalog.h"
#include "command.h"
#include "query.h"
D
dapan1121 已提交
19
#include "schInt.h"
D
dapan1121 已提交
20 21 22
#include "tmsg.h"
#include "tref.h"
#include "trpc.h"
dengyihao's avatar
dengyihao 已提交
23
// clang-format off
24
int32_t schValidateRspMsgType(SSchJob *pJob, SSchTask *pTask, int32_t msgType) {
D
dapan1121 已提交
25
  int32_t lastMsgType = pTask->lastMsgType;
D
dapan1121 已提交
26
  int32_t taskStatus = SCH_GET_TASK_STATUS(pTask);
27
  int32_t reqMsgType = (msgType & 1U) ? msgType : (msgType - 1);
D
dapan1121 已提交
28 29
  switch (msgType) {
    case TDMT_SCH_LINK_BROKEN:
D
dapan1121 已提交
30
    case TDMT_SCH_EXPLAIN_RSP:
D
dapan1121 已提交
31
      return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
32
    case TDMT_SCH_FETCH_RSP:
D
dapan1121 已提交
33
    case TDMT_SCH_MERGE_FETCH_RSP:
34
      if (lastMsgType != reqMsgType) {
D
dapan1121 已提交
35 36
        SCH_TASK_ELOG("rsp msg type mis-match, last sent msgType:%s, rspType:%s", TMSG_INFO(lastMsgType),
                      TMSG_INFO(msgType));
37
        SCH_ERR_RET(TSDB_CODE_QW_MSG_ERROR);
38
      }
39
      if (taskStatus != JOB_TASK_STATUS_PART_SUCC) {
D
dapan1121 已提交
40 41
        SCH_TASK_ELOG("rsp msg conflicted with task status, status:%s, rspType:%s", jobTaskStatusStr(taskStatus),
                      TMSG_INFO(msgType));
42
        SCH_ERR_RET(TSDB_CODE_QW_MSG_ERROR);
D
dapan1121 已提交
43 44 45
      }

      return TSDB_CODE_SUCCESS;
46 47
    case TDMT_SCH_MERGE_QUERY_RSP:
    case TDMT_SCH_QUERY_RSP:
D
dapan1121 已提交
48 49 50 51
    case TDMT_VND_CREATE_TABLE_RSP:
    case TDMT_VND_DROP_TABLE_RSP:
    case TDMT_VND_ALTER_TABLE_RSP:
    case TDMT_VND_SUBMIT_RSP:
D
dapan1121 已提交
52
    case TDMT_VND_DELETE_RSP:
H
Hongze Cheng 已提交
53
    case TDMT_VND_COMMIT_RSP:
D
dapan1121 已提交
54 55 56
      break;
    default:
      SCH_TASK_ELOG("unknown rsp msg, type:%s, status:%s", TMSG_INFO(msgType), jobTaskStatusStr(taskStatus));
D
dapan1121 已提交
57
      SCH_ERR_RET(TSDB_CODE_INVALID_MSG);
D
dapan1121 已提交
58 59 60 61 62
  }

  if (lastMsgType != reqMsgType) {
    SCH_TASK_ELOG("rsp msg type mis-match, last sent msgType:%s, rspType:%s", TMSG_INFO(lastMsgType),
                  TMSG_INFO(msgType));
63
    SCH_ERR_RET(TSDB_CODE_QW_MSG_ERROR);
D
dapan1121 已提交
64 65
  }

66
  if (taskStatus != JOB_TASK_STATUS_EXEC) {
D
dapan1121 已提交
67 68
    SCH_TASK_ELOG("rsp msg conflicted with task status, status:%s, rspType:%s", jobTaskStatusStr(taskStatus),
                  TMSG_INFO(msgType));
69
    SCH_ERR_RET(TSDB_CODE_QW_MSG_ERROR);
D
dapan1121 已提交
70 71 72 73 74
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
int32_t schProcessFetchRsp(SSchJob *pJob, SSchTask *pTask, char *msg, int32_t rspCode) {
  SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)msg;
  int32_t code = 0;
  
  SCH_ERR_JRET(rspCode);
  
  if (NULL == msg) {
    SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }
  
  if (SCH_IS_EXPLAIN_JOB(pJob)) {
    if (rsp->completed) {
      SRetrieveTableRsp *pRsp = NULL;
      SCH_ERR_JRET(qExecExplainEnd(pJob->explainCtx, &pRsp));
      if (pRsp) {
        SCH_ERR_JRET(schProcessOnExplainDone(pJob, pTask, pRsp));
      }
  
      taosMemoryFreeClear(msg);
  
      return TSDB_CODE_SUCCESS;
    }
  
    SCH_ERR_JRET(schLaunchFetchTask(pJob));
  
    taosMemoryFreeClear(msg);
  
    return TSDB_CODE_SUCCESS;
  }
  
  if (pJob->fetchRes) {
    SCH_TASK_ELOG("got fetch rsp while res already exists, res:%p", pJob->fetchRes);
    SCH_ERR_JRET(TSDB_CODE_SCH_STATUS_ERROR);
  }
  
  atomic_store_ptr(&pJob->fetchRes, rsp);
111
  atomic_add_fetch_64(&pJob->resNumOfRows, htobe64(rsp->numOfRows));
D
dapan1121 已提交
112 113 114 115 116
  
  if (rsp->completed) {
    SCH_SET_TASK_STATUS(pTask, JOB_TASK_STATUS_SUCC);
  }
  
117
  SCH_TASK_DLOG("got fetch rsp, rows:%" PRId64 ", complete:%d", htobe64(rsp->numOfRows), rsp->completed);
D
dapan1121 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130

  msg = NULL;
  schProcessOnDataFetched(pJob);

_return:

  taosMemoryFreeClear(msg);

  SCH_RET(code);
}

int32_t schProcessExplainRsp(SSchJob *pJob, SSchTask *pTask, SExplainRsp *rsp) {
  SRetrieveTableRsp *pRsp = NULL;
131
  SCH_ERR_RET(qExplainUpdateExecInfo(pJob->explainCtx, rsp, pTask->plan->id.groupId, &pRsp));
D
dapan1121 已提交
132 133
  
  if (pRsp) {
134
    SCH_ERR_RET(schProcessOnExplainDone(pJob, pTask, pRsp));
D
dapan1121 已提交
135 136 137 138 139
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
140
// Note: no more task error processing, handled in function internal
D
dapan1121 已提交
141
int32_t schHandleResponseMsg(SSchJob *pJob, SSchTask *pTask, int32_t execId, SDataBuf *pMsg, int32_t rspCode) {
D
dapan1121 已提交
142
  int32_t code = 0;
143
  char   *msg = pMsg->pData;
D
dapan1121 已提交
144 145 146 147
  int32_t msgSize = pMsg->len;
  int32_t msgType = pMsg->msgType;

  bool dropExecNode = (msgType == TDMT_SCH_LINK_BROKEN || SCH_NETWORK_ERR(rspCode));
D
dapan1121 已提交
148 149 150 151
  if (SCH_IS_QUERY_JOB(pJob)) {
    SCH_ERR_JRET(schUpdateTaskHandle(pJob, pTask, dropExecNode, pMsg->handle, execId));
  }
  
152
  SCH_ERR_JRET(schValidateRspMsgType(pJob, pTask, msgType));
D
dapan1121 已提交
153 154

  int32_t reqType = IsReq(pMsg) ? pMsg->msgType : (pMsg->msgType - 1);
D
dapan1121 已提交
155
  if (SCH_TASK_NEED_REDIRECT(pTask, reqType, rspCode, pMsg->len)) {
D
dapan1121 已提交
156 157
    SCH_RET(schHandleRedirect(pJob, pTask, (SDataBuf *)pMsg, rspCode));
  }
D
dapan1121 已提交
158

D
dapan1121 已提交
159 160
  pTask->redirectCtx.inRedirect = false;

D
dapan1121 已提交
161
  switch (msgType) {
H
Hongze Cheng 已提交
162
    case TDMT_VND_COMMIT_RSP: {
163
      SCH_ERR_JRET(rspCode);
D
dapan1121 已提交
164
      SCH_ERR_JRET(schProcessOnTaskSuccess(pJob, pTask));
165 166
      break;
    }
D
dapan1121 已提交
167 168 169 170 171 172 173
    case TDMT_VND_CREATE_TABLE_RSP: {
      SVCreateTbBatchRsp batchRsp = {0};
      if (msg) {
        SDecoder coder = {0};
        tDecoderInit(&coder, msg, msgSize);
        code = tDecodeSVCreateTbBatchRsp(&coder, &batchRsp);
        if (TSDB_CODE_SUCCESS == code && batchRsp.nRsps > 0) {
174 175
          SCH_LOCK(SCH_WRITE, &pJob->resLock);
          if (NULL == pJob->execRes.res) {
D
dapan1121 已提交
176
            pJob->execRes.res = (void*)taosArrayInit(batchRsp.nRsps, POINTER_BYTES);
177 178
            pJob->execRes.msgType = TDMT_VND_CREATE_TABLE;
          }
179

D
dapan1121 已提交
180 181
          for (int32_t i = 0; i < batchRsp.nRsps; ++i) {
            SVCreateTbRsp *rsp = batchRsp.pRsps + i;
182 183 184 185
            if (rsp->pMeta) {
              taosArrayPush((SArray*)pJob->execRes.res, &rsp->pMeta);
            }
            
D
dapan1121 已提交
186 187 188 189
            if (TSDB_CODE_SUCCESS != rsp->code) {
              code = rsp->code;
            }
          }
190 191 192 193 194

          if (taosArrayGetSize((SArray*)pJob->execRes.res) <= 0) {        
            taosArrayDestroy((SArray*)pJob->execRes.res);
            pJob->execRes.res = NULL;
          }
D
dapan1121 已提交
195
          SCH_UNLOCK(SCH_WRITE, &pJob->resLock);
D
dapan1121 已提交
196
        }
197
        
D
dapan1121 已提交
198 199 200 201 202
        tDecoderClear(&coder);
        SCH_ERR_JRET(code);
      }

      SCH_ERR_JRET(rspCode);
203 204
      taosMemoryFreeClear(msg);

D
dapan1121 已提交
205
      SCH_ERR_JRET(schProcessOnTaskSuccess(pJob, pTask));
D
dapan1121 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
      break;
    }
    case TDMT_VND_DROP_TABLE_RSP: {
      SVDropTbBatchRsp batchRsp = {0};
      if (msg) {
        SDecoder coder = {0};
        tDecoderInit(&coder, msg, msgSize);
        code = tDecodeSVDropTbBatchRsp(&coder, &batchRsp);
        if (TSDB_CODE_SUCCESS == code && batchRsp.nRsps > 0) {
          for (int32_t i = 0; i < batchRsp.nRsps; ++i) {
            SVDropTbRsp *rsp = batchRsp.pRsps + i;
            if (TSDB_CODE_SUCCESS != rsp->code) {
              code = rsp->code;
              tDecoderClear(&coder);
              SCH_ERR_JRET(code);
            }
          }
        }
        tDecoderClear(&coder);
        SCH_ERR_JRET(code);
      }

      SCH_ERR_JRET(rspCode);
229 230
      taosMemoryFreeClear(msg);

D
dapan1121 已提交
231
      SCH_ERR_JRET(schProcessOnTaskSuccess(pJob, pTask));
D
dapan1121 已提交
232 233 234 235 236 237 238 239 240 241 242
      break;
    }
    case TDMT_VND_ALTER_TABLE_RSP: {
      SVAlterTbRsp rsp = {0};
      if (msg) {
        SDecoder coder = {0};
        tDecoderInit(&coder, msg, msgSize);
        code = tDecodeSVAlterTbRsp(&coder, &rsp);
        tDecoderClear(&coder);
        SCH_ERR_JRET(code);
        SCH_ERR_JRET(rsp.code);
243

D
dapan1121 已提交
244 245
        pJob->execRes.res = rsp.pMeta;
        pJob->execRes.msgType = TDMT_VND_ALTER_TABLE;
D
dapan1121 已提交
246 247 248 249 250 251 252
      }

      SCH_ERR_JRET(rspCode);

      if (NULL == msg) {
        SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
      }
D
dapan1121 已提交
253

254 255
      taosMemoryFreeClear(msg);

D
dapan1121 已提交
256
      SCH_ERR_JRET(schProcessOnTaskSuccess(pJob, pTask));
D
dapan1121 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
      break;
    }
    case TDMT_VND_SUBMIT_RSP: {
      SCH_ERR_JRET(rspCode);

      if (msg) {
        SDecoder    coder = {0};
        SSubmitRsp *rsp = taosMemoryMalloc(sizeof(*rsp));
        tDecoderInit(&coder, msg, msgSize);
        code = tDecodeSSubmitRsp(&coder, rsp);
        if (code) {
          SCH_TASK_ELOG("decode submitRsp failed, code:%d", code);
          tFreeSSubmitRsp(rsp);
          SCH_ERR_JRET(code);
        }

        if (rsp->nBlocks > 0) {
          for (int32_t i = 0; i < rsp->nBlocks; ++i) {
            SSubmitBlkRsp *blk = rsp->pBlocks + i;
            if (TSDB_CODE_SUCCESS != blk->code) {
              code = blk->code;
              tFreeSSubmitRsp(rsp);
              SCH_ERR_JRET(code);
            }
          }
        }

284
        atomic_add_fetch_64(&pJob->resNumOfRows, rsp->affectedRows);
D
dapan1121 已提交
285
        SCH_TASK_DLOG("submit succeed, affectedRows:%d, blocks:%d", rsp->affectedRows, rsp->nBlocks);
D
dapan1121 已提交
286 287

        SCH_LOCK(SCH_WRITE, &pJob->resLock);
D
dapan1121 已提交
288 289
        if (pJob->execRes.res) {
          SSubmitRsp *sum = pJob->execRes.res;
D
dapan1121 已提交
290
          sum->affectedRows += rsp->affectedRows;
D
dapan1121 已提交
291 292 293 294 295
          sum->nBlocks += rsp->nBlocks;          
          if (rsp->nBlocks > 0 && rsp->pBlocks) {
            sum->pBlocks = taosMemoryRealloc(sum->pBlocks, sum->nBlocks * sizeof(*sum->pBlocks));
            memcpy(sum->pBlocks + sum->nBlocks - rsp->nBlocks, rsp->pBlocks, rsp->nBlocks * sizeof(*sum->pBlocks));
          }
D
dapan1121 已提交
296 297 298
          taosMemoryFree(rsp->pBlocks);
          taosMemoryFree(rsp);
        } else {
D
dapan1121 已提交
299 300
          pJob->execRes.res = rsp;
          pJob->execRes.msgType = TDMT_VND_SUBMIT;
D
dapan1121 已提交
301
        }
D
dapan1121 已提交
302
        pJob->execRes.numOfBytes += pTask->msgLen;
D
dapan1121 已提交
303 304 305
        SCH_UNLOCK(SCH_WRITE, &pJob->resLock);
      }

306
      taosMemoryFreeClear(msg);
D
dapan1121 已提交
307

D
dapan1121 已提交
308
      SCH_ERR_JRET(schProcessOnTaskSuccess(pJob, pTask));
D
dapan1121 已提交
309 310 311

      break;
    }
D
dapan1121 已提交
312 313 314 315
    case TDMT_VND_DELETE_RSP: {
      SCH_ERR_JRET(rspCode);

      if (msg) {
316
        SDecoder    coder = {0};
D
dapan1121 已提交
317 318 319
        SVDeleteRsp rsp = {0};
        tDecoderInit(&coder, msg, msgSize);
        tDecodeSVDeleteRsp(&coder, &rsp);
wmmhello's avatar
wmmhello 已提交
320
        tDecoderClear(&coder);
D
dapan1121 已提交
321

322
        atomic_add_fetch_64(&pJob->resNumOfRows, rsp.affectedRows);
D
dapan1121 已提交
323
        SCH_TASK_DLOG("delete succeed, affectedRows:%" PRId64, rsp.affectedRows);
D
dapan1121 已提交
324 325 326 327
      }

      taosMemoryFreeClear(msg);

D
dapan1121 已提交
328
      SCH_ERR_JRET(schProcessOnTaskSuccess(pJob, pTask));
D
dapan1121 已提交
329 330

      break;
331
    }
D
dapan1121 已提交
332 333
    case TDMT_SCH_QUERY_RSP:
    case TDMT_SCH_MERGE_QUERY_RSP: {
D
dapan1121 已提交
334 335 336 337
      SCH_ERR_JRET(rspCode);
      if (NULL == msg) {
        SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
      }
D
dapan1121 已提交
338

339 340 341 342 343 344 345
      SQueryTableRsp rsp = {0};
      if (tDeserializeSQueryTableRsp(msg, msgSize, &rsp) < 0) {
        SCH_TASK_ELOG("tDeserializeSQueryTableRsp failed, msgSize:%d", msgSize);
        SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_MSG);
      }
      
      SCH_ERR_JRET(rsp.code);
D
dapan1121 已提交
346

347
      SCH_ERR_JRET(schSaveJobExecRes(pJob, &rsp));
D
dapan1121 已提交
348

349
      atomic_add_fetch_64(&pJob->resNumOfRows, rsp.affectedRows);
D
dapan1121 已提交
350

351 352
      taosMemoryFreeClear(msg);

D
dapan1121 已提交
353
      SCH_ERR_JRET(schProcessOnTaskSuccess(pJob, pTask));
D
dapan1121 已提交
354 355 356

      break;
    }
D
dapan1121 已提交
357
    case TDMT_SCH_EXPLAIN_RSP: {
D
dapan1121 已提交
358 359 360 361 362 363 364 365 366 367
      SCH_ERR_JRET(rspCode);
      if (NULL == msg) {
        SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
      }

      if (!SCH_IS_EXPLAIN_JOB(pJob)) {
        SCH_TASK_ELOG("invalid msg received for none explain query, msg type:%s", TMSG_INFO(msgType));
        SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
      }

D
dapan1121 已提交
368 369
      if (pJob->fetchRes) {
        SCH_TASK_ELOG("explain result is already generated, res:%p", pJob->fetchRes);
D
dapan1121 已提交
370 371 372 373 374
        SCH_ERR_JRET(TSDB_CODE_SCH_STATUS_ERROR);
      }

      SExplainRsp rsp = {0};
      if (tDeserializeSExplainRsp(msg, msgSize, &rsp)) {
375
        tFreeSExplainRsp(&rsp);
S
Shengliang Guan 已提交
376
        SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
377 378
      }

D
dapan1121 已提交
379
      SCH_ERR_JRET(schProcessExplainRsp(pJob, pTask, &rsp));
D
dapan1121 已提交
380 381

      taosMemoryFreeClear(msg);
D
dapan1121 已提交
382 383
      break;
    }
D
dapan1121 已提交
384 385
    case TDMT_SCH_FETCH_RSP:
    case TDMT_SCH_MERGE_FETCH_RSP: {
D
dapan1121 已提交
386
      code = schProcessFetchRsp(pJob, pTask, msg, rspCode);
387
      msg = NULL;
D
dapan1121 已提交
388
      SCH_ERR_JRET(code);
D
dapan1121 已提交
389 390
      break;
    }
D
dapan1121 已提交
391 392
    case TDMT_SCH_DROP_TASK_RSP: {
      // NEVER REACH HERE
D
dapan1121 已提交
393
      SCH_TASK_ELOG("invalid status to handle drop task rsp, refId:0x%" PRIx64, pJob->refId);
D
dapan1121 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
      SCH_ERR_JRET(TSDB_CODE_SCH_INTERNAL_ERROR);
    }
    case TDMT_SCH_LINK_BROKEN:
      SCH_TASK_ELOG("link broken received, error:%x - %s", rspCode, tstrerror(rspCode));
      SCH_ERR_JRET(rspCode);
      break;
    default:
      SCH_TASK_ELOG("unknown rsp msg, type:%d, status:%s", msgType, SCH_GET_TASK_STATUS_STR(pTask));
      SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  return TSDB_CODE_SUCCESS;

_return:

D
dapan1121 已提交
409
  taosMemoryFreeClear(msg);
D
dapan1121 已提交
410

D
dapan1121 已提交
411
  SCH_RET(schProcessOnTaskFailure(pJob, pTask, code));
dengyihao's avatar
dengyihao 已提交
412
} 
D
dapan1121 已提交
413
int32_t schHandleCallback(void *param, SDataBuf *pMsg, int32_t rspCode) {
D
dapan1121 已提交
414 415 416
  int32_t                code = 0;
  SSchTaskCallbackParam *pParam = (SSchTaskCallbackParam *)param;
  SSchTask              *pTask = NULL;
D
dapan1121 已提交
417
  SSchJob               *pJob = NULL;
D
dapan1121 已提交
418

419 420
  qDebug("begin to handle rsp msg, type:%s, handle:%p, code:%s", TMSG_INFO(pMsg->msgType), pMsg->handle,
         tstrerror(rspCode));
D
dapan1121 已提交
421

D
dapan1121 已提交
422
  SCH_ERR_JRET(schProcessOnCbBegin(&pJob, &pTask, pParam->queryId, pParam->refId, pParam->taskId));
423
  
D
dapan1121 已提交
424
  code = schHandleResponseMsg(pJob, pTask, pParam->execId, pMsg, rspCode);
D
dapan1121 已提交
425
  pMsg->pData = NULL;
D
dapan1121 已提交
426

D
dapan1121 已提交
427
  schProcessOnCbEnd(pJob, pTask, code);
D
dapan1121 已提交
428

D
dapan1121 已提交
429 430
_return:

D
dapan1121 已提交
431
  taosMemoryFreeClear(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
432
  taosMemoryFreeClear(pMsg->pEpSet);
D
dapan1121 已提交
433

434 435
  qDebug("end to handle rsp msg, type:%s, handle:%p, code:%s", TMSG_INFO(pMsg->msgType), pMsg->handle,
         tstrerror(rspCode));
D
dapan1121 已提交
436

D
dapan1121 已提交
437 438 439
  SCH_RET(code);
}

D
dapan1121 已提交
440
int32_t schHandleDropCallback(void *param, SDataBuf *pMsg, int32_t code) {
D
dapan1121 已提交
441
  SSchTaskCallbackParam *pParam = (SSchTaskCallbackParam *)param;
442 443
  qDebug("QID:0x%" PRIx64 ",TID:0x%" PRIx64 " drop task rsp received, code:0x%x", pParam->queryId, pParam->taskId,
         code);
D
dapan1121 已提交
444
  if (pMsg) {
dengyihao's avatar
dengyihao 已提交
445
    taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
446
    taosMemoryFree(pMsg->pEpSet);
D
dapan1121 已提交
447
  }
D
dapan1121 已提交
448 449 450
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
451
int32_t schHandleLinkBrokenCallback(void *param, SDataBuf *pMsg, int32_t code) {
D
dapan1121 已提交
452 453 454 455 456 457
  SSchCallbackParamHeader *head = (SSchCallbackParamHeader *)param;
  rpcReleaseHandle(pMsg->handle, TAOS_CONN_CLIENT);

  qDebug("handle %p is broken", pMsg->handle);

  if (head->isHbParam) {
D
dapan1121 已提交
458
    taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
459
    taosMemoryFree(pMsg->pEpSet);
dengyihao's avatar
dengyihao 已提交
460

D
dapan1121 已提交
461
    SSchHbCallbackParam *hbParam = (SSchHbCallbackParam *)param;
D
dapan1121 已提交
462
    SSchTrans            trans = {.pTrans = hbParam->pTrans, .pHandle = NULL};
D
dapan1121 已提交
463 464
    SCH_ERR_RET(schUpdateHbConnection(&hbParam->nodeEpId, &trans));

D
dapan1121 已提交
465
    SCH_ERR_RET(schBuildAndSendHbMsg(&hbParam->nodeEpId, NULL));
D
dapan1121 已提交
466
  } else {
D
dapan1121 已提交
467
    SCH_ERR_RET(schHandleCallback(param, pMsg, code));
D
dapan1121 已提交
468 469 470 471 472
  }

  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
473 474 475
int32_t schHandleCommitCallback(void *param, SDataBuf *pMsg, int32_t code) {
  return schHandleCallback(param, pMsg, code);
}
D
dapan1121 已提交
476

D
dapan1121 已提交
477
int32_t schHandleHbCallback(void *param, SDataBuf *pMsg, int32_t code) {
478
  SSchedulerHbRsp        rsp = {0};
D
dapan1121 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
  SSchTaskCallbackParam *pParam = (SSchTaskCallbackParam *)param;

  if (code) {
    qError("hb rsp error:%s", tstrerror(code));
    SCH_ERR_JRET(code);
  }

  if (tDeserializeSSchedulerHbRsp(pMsg->pData, pMsg->len, &rsp)) {
    qError("invalid hb rsp msg, size:%d", pMsg->len);
    SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  SSchTrans trans = {0};
  trans.pTrans = pParam->pTrans;
  trans.pHandle = pMsg->handle;

  SCH_ERR_JRET(schUpdateHbConnection(&rsp.epId, &trans));
  SCH_ERR_JRET(schProcessOnTaskStatusRsp(&rsp.epId, rsp.taskStatus));

_return:
D
dapan1121 已提交
499

D
dapan1121 已提交
500
  tFreeSSchedulerHbRsp(&rsp);
H
Haojun Liao 已提交
501
  taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
502
  taosMemoryFree(pMsg->pEpSet);
D
dapan1121 已提交
503 504 505
  SCH_RET(code);
}

506 507
int32_t schMakeCallbackParam(SSchJob *pJob, SSchTask *pTask, int32_t msgType, bool isHb, SSchTrans *trans,
                             void **pParam) {
D
dapan1121 已提交
508 509 510 511
  if (!isHb) {
    SSchTaskCallbackParam *param = taosMemoryCalloc(1, sizeof(SSchTaskCallbackParam));
    if (NULL == param) {
      SCH_TASK_ELOG("calloc %d failed", (int32_t)sizeof(SSchTaskCallbackParam));
S
Shengliang Guan 已提交
512
      SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
513
    }
514

D
dapan1121 已提交
515 516 517 518
    param->queryId = pJob->queryId;
    param->refId = pJob->refId;
    param->taskId = SCH_TASK_ID(pTask);
    param->pTrans = pJob->conn.pTrans;
D
dapan1121 已提交
519
    param->execId = pTask->execId;
D
dapan1121 已提交
520 521 522
    *pParam = param;

    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
523 524
  }

D
dapan1121 已提交
525 526 527 528
  if (TDMT_SCH_LINK_BROKEN == msgType) {
    SSchHbCallbackParam *param = taosMemoryCalloc(1, sizeof(SSchHbCallbackParam));
    if (NULL == param) {
      SCH_TASK_ELOG("calloc %d failed", (int32_t)sizeof(SSchHbCallbackParam));
S
Shengliang Guan 已提交
529
      SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
530
    }
531

D
dapan1121 已提交
532
    param->head.isHbParam = true;
533

D
dapan1121 已提交
534 535
    SQueryNodeAddr *addr = taosArrayGet(pTask->candidateAddrs, pTask->candidateIdx);
    param->nodeEpId.nodeId = addr->nodeId;
536
    SEp *pEp = SCH_GET_CUR_EP(addr);
D
dapan1121 已提交
537 538 539 540 541 542
    strcpy(param->nodeEpId.ep.fqdn, pEp->fqdn);
    param->nodeEpId.ep.port = pEp->port;
    param->pTrans = trans->pTrans;
    *pParam = param;

    return TSDB_CODE_SUCCESS;
543
  }
D
dapan1121 已提交
544 545

  // hb msg
D
dapan1121 已提交
546 547
  SSchTaskCallbackParam *param = taosMemoryCalloc(1, sizeof(SSchTaskCallbackParam));
  if (NULL == param) {
D
dapan1121 已提交
548
    qError("calloc SSchTaskCallbackParam failed");
S
Shengliang Guan 已提交
549
    SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
550
  }
551

D
dapan1121 已提交
552 553
  param->pTrans = trans->pTrans;
  *pParam = param;
D
dapan1121 已提交
554

D
dapan1121 已提交
555 556
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
557

558 559
int32_t schGenerateCallBackInfo(SSchJob *pJob, SSchTask *pTask, void *msg, uint32_t msgSize, int32_t msgType,
                                SSchTrans *trans, bool isHb, SMsgSendInfo **pMsgSendInfo) {
D
dapan1121 已提交
560 561 562 563
  int32_t       code = 0;
  SMsgSendInfo *msgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
  if (NULL == msgSendInfo) {
    SCH_TASK_ELOG("calloc %d failed", (int32_t)sizeof(SMsgSendInfo));
S
Shengliang Guan 已提交
564
    SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
565 566
  }

H
more  
Hongze Cheng 已提交
567
  msgSendInfo->paramFreeFp = taosMemoryFree;
D
dapan1121 已提交
568
  SCH_ERR_JRET(schMakeCallbackParam(pJob, pTask, msgType, isHb, trans, &msgSendInfo->param));
D
dapan1121 已提交
569

D
dapan1121 已提交
570
  SCH_ERR_JRET(schGetCallbackFp(msgType, &msgSendInfo->fp));
D
dapan1121 已提交
571

D
dapan1121 已提交
572 573 574 575
  if (pJob) {
    msgSendInfo->requestId = pJob->conn.requestId;
    msgSendInfo->requestObjRefId = pJob->conn.requestObjRefId;
  }
576

D
dapan1121 已提交
577 578 579 580 581 582
  if (TDMT_SCH_LINK_BROKEN != msgType) {
    msgSendInfo->msgInfo.pData = msg;
    msgSendInfo->msgInfo.len = msgSize;
    msgSendInfo->msgInfo.handle = trans->pHandle;
    msgSendInfo->msgType = msgType;
  }
D
dapan1121 已提交
583 584 585 586 587 588 589

  *pMsgSendInfo = msgSendInfo;

  return TSDB_CODE_SUCCESS;

_return:

D
dapan1121 已提交
590 591 592
  if (msgSendInfo) {
    destroySendMsgInfo(msgSendInfo);
  }
D
dapan1121 已提交
593

D
dapan1121 已提交
594
  taosMemoryFree(msg);
H
more  
Hongze Cheng 已提交
595

D
dapan1121 已提交
596 597 598 599 600 601 602 603 604
  SCH_RET(code);
}

int32_t schGetCallbackFp(int32_t msgType, __async_send_cb_fn_t *fp) {
  switch (msgType) {
    case TDMT_VND_CREATE_TABLE:
    case TDMT_VND_DROP_TABLE:
    case TDMT_VND_ALTER_TABLE:
    case TDMT_VND_SUBMIT:
D
dapan1121 已提交
605
    case TDMT_SCH_QUERY:
D
dapan1121 已提交
606
    case TDMT_SCH_MERGE_QUERY:
D
dapan1121 已提交
607
    case TDMT_VND_DELETE:
D
dapan1121 已提交
608 609
    case TDMT_SCH_EXPLAIN:
    case TDMT_SCH_FETCH:
D
dapan1121 已提交
610
    case TDMT_SCH_MERGE_FETCH:
D
dapan1121 已提交
611
      *fp = schHandleCallback;
D
dapan1121 已提交
612
      break;
D
dapan1121 已提交
613
    case TDMT_SCH_DROP_TASK:
D
dapan1121 已提交
614
      *fp = schHandleDropCallback;
D
dapan1121 已提交
615
      break;
D
dapan1121 已提交
616
    case TDMT_SCH_QUERY_HEARTBEAT:
D
dapan1121 已提交
617 618
      *fp = schHandleHbCallback;
      break;
X
Xiaoyu Wang 已提交
619 620 621
    case TDMT_VND_COMMIT:
      *fp = schHandleCommitCallback;
      break;
D
dapan1121 已提交
622 623 624 625 626
    case TDMT_SCH_LINK_BROKEN:
      *fp = schHandleLinkBrokenCallback;
      break;
    default:
      qError("unknown msg type for callback, msgType:%d", msgType);
S
Shengliang Guan 已提交
627
      SCH_ERR_RET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
628 629 630 631 632
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
633
/*
D
dapan1121 已提交
634 635 636 637
int32_t schMakeHbCallbackParam(SSchJob *pJob, SSchTask *pTask, void **pParam) {
  SSchHbCallbackParam *param = taosMemoryCalloc(1, sizeof(SSchHbCallbackParam));
  if (NULL == param) {
    SCH_TASK_ELOG("calloc %d failed", (int32_t)sizeof(SSchHbCallbackParam));
S
Shengliang Guan 已提交
638
    SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
639 640 641 642 643 644 645
  }

  param->head.isHbParam = true;

  SQueryNodeAddr *addr = taosArrayGet(pTask->candidateAddrs, pTask->candidateIdx);

  param->nodeEpId.nodeId = addr->nodeId;
D
dapan1121 已提交
646 647 648
  SEp* pEp = SCH_GET_CUR_EP(addr);
  strcpy(param->nodeEpId.ep.fqdn, pEp->fqdn);
  param->nodeEpId.ep.port = pEp->port;
D
dapan1121 已提交
649
  param->pTrans = pJob->pTrans;
D
dapan1121 已提交
650 651 652 653 654

  *pParam = param;

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
655
*/
D
dapan1121 已提交
656 657 658

int32_t schCloneHbRpcCtx(SRpcCtx *pSrc, SRpcCtx *pDst) {
  int32_t code = 0;
D
dapan1121 已提交
659
  memcpy(pDst, pSrc, sizeof(SRpcCtx));
D
dapan1121 已提交
660
  pDst->brokenVal.val = NULL;
D
dapan1121 已提交
661
  pDst->args = NULL;
D
dapan1121 已提交
662 663 664 665 666 667

  SCH_ERR_RET(schCloneSMsgSendInfo(pSrc->brokenVal.val, &pDst->brokenVal.val));

  pDst->args = taosHashInit(1, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
  if (NULL == pDst->args) {
    qError("taosHashInit %d RpcCtx failed", 1);
S
Shengliang Guan 已提交
668
    SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
  }

  SRpcCtxVal dst = {0};
  void      *pIter = taosHashIterate(pSrc->args, NULL);
  while (pIter) {
    SRpcCtxVal *pVal = (SRpcCtxVal *)pIter;
    int32_t    *msgType = taosHashGetKey(pIter, NULL);

    dst = *pVal;
    dst.val = NULL;

    SCH_ERR_JRET(schCloneSMsgSendInfo(pVal->val, &dst.val));

    if (taosHashPut(pDst->args, msgType, sizeof(*msgType), &dst, sizeof(dst))) {
      qError("taosHashPut msg %d to rpcCtx failed", *msgType);
D
dapan1121 已提交
684
      (*pSrc->freeFunc)(dst.val);
S
Shengliang Guan 已提交
685
      SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
    }

    pIter = taosHashIterate(pSrc->args, pIter);
  }

  return TSDB_CODE_SUCCESS;

_return:

  schFreeRpcCtx(pDst);
  SCH_RET(code);
}

int32_t schMakeHbRpcCtx(SSchJob *pJob, SSchTask *pTask, SRpcCtx *pCtx) {
  int32_t              code = 0;
  SSchHbCallbackParam *param = NULL;
  SMsgSendInfo        *pMsgSendInfo = NULL;
  SQueryNodeAddr      *addr = taosArrayGet(pTask->candidateAddrs, pTask->candidateIdx);
  SQueryNodeEpId       epId = {0};

  epId.nodeId = addr->nodeId;
  memcpy(&epId.ep, SCH_GET_CUR_EP(addr), sizeof(SEp));

  pCtx->args = taosHashInit(1, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
  if (NULL == pCtx->args) {
    SCH_TASK_ELOG("taosHashInit %d RpcCtx failed", 1);
S
Shengliang Guan 已提交
712
    SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
713 714 715 716 717
  }

  pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
  if (NULL == pMsgSendInfo) {
    SCH_TASK_ELOG("calloc %d failed", (int32_t)sizeof(SMsgSendInfo));
S
Shengliang Guan 已提交
718
    SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
719 720 721 722 723
  }

  param = taosMemoryCalloc(1, sizeof(SSchHbCallbackParam));
  if (NULL == param) {
    SCH_TASK_ELOG("calloc %d failed", (int32_t)sizeof(SSchHbCallbackParam));
S
Shengliang Guan 已提交
724
    SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
725 726
  }

D
dapan1121 已提交
727
  int32_t              msgType = TDMT_SCH_QUERY_HEARTBEAT_RSP;
D
dapan1121 已提交
728
  __async_send_cb_fn_t fp = NULL;
D
dapan1121 已提交
729
  SCH_ERR_JRET(schGetCallbackFp(TDMT_SCH_QUERY_HEARTBEAT, &fp));
D
dapan1121 已提交
730 731

  param->nodeEpId = epId;
D
dapan1121 已提交
732
  param->pTrans = pJob->conn.pTrans;
D
dapan1121 已提交
733 734

  pMsgSendInfo->param = param;
H
more  
Hongze Cheng 已提交
735
  pMsgSendInfo->paramFreeFp = taosMemoryFree;
D
dapan1121 已提交
736 737
  pMsgSendInfo->fp = fp;

D
dapan1121 已提交
738
  SRpcCtxVal ctxVal = {.val = pMsgSendInfo, .clone = schCloneSMsgSendInfo};
D
dapan1121 已提交
739 740
  if (taosHashPut(pCtx->args, &msgType, sizeof(msgType), &ctxVal, sizeof(ctxVal))) {
    SCH_TASK_ELOG("taosHashPut msg %d to rpcCtx failed", msgType);
S
Shengliang Guan 已提交
741
    SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
742 743 744
  }

  SCH_ERR_JRET(schMakeBrokenLinkVal(pJob, pTask, &pCtx->brokenVal, true));
D
dapan1121 已提交
745
  pCtx->freeFunc = schFreeRpcCtxVal;
D
dapan1121 已提交
746 747 748 749 750 751 752 753 754 755 756 757 758

  return TSDB_CODE_SUCCESS;

_return:

  taosHashCleanup(pCtx->args);
  taosMemoryFreeClear(param);
  taosMemoryFreeClear(pMsgSendInfo);

  SCH_RET(code);
}

int32_t schMakeBrokenLinkVal(SSchJob *pJob, SSchTask *pTask, SRpcBrokenlinkVal *brokenVal, bool isHb) {
759 760 761
  int32_t       code = 0;
  int32_t       msgType = TDMT_SCH_LINK_BROKEN;
  SSchTrans     trans = {.pTrans = pJob->conn.pTrans};
D
dapan1121 已提交
762
  SMsgSendInfo *pMsgSendInfo = NULL;
D
dapan1121 已提交
763
  SCH_ERR_JRET(schGenerateCallBackInfo(pJob, pTask, NULL, 0, msgType, &trans, isHb, &pMsgSendInfo));
D
dapan1121 已提交
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785

  brokenVal->msgType = msgType;
  brokenVal->val = pMsgSendInfo;
  brokenVal->clone = schCloneSMsgSendInfo;

  return TSDB_CODE_SUCCESS;

_return:

  taosMemoryFreeClear(pMsgSendInfo->param);
  taosMemoryFreeClear(pMsgSendInfo);

  SCH_RET(code);
}

int32_t schMakeQueryRpcCtx(SSchJob *pJob, SSchTask *pTask, SRpcCtx *pCtx) {
  int32_t       code = 0;
  SMsgSendInfo *pExplainMsgSendInfo = NULL;

  pCtx->args = taosHashInit(1, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
  if (NULL == pCtx->args) {
    SCH_TASK_ELOG("taosHashInit %d RpcCtx failed", 1);
S
Shengliang Guan 已提交
786
    SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
787 788
  }

D
dapan1121 已提交
789
  SSchTrans trans = {.pTrans = pJob->conn.pTrans, .pHandle = SCH_GET_TASK_HANDLE(pTask)};
D
dapan1121 已提交
790
  SCH_ERR_JRET(schGenerateCallBackInfo(pJob, pTask, NULL, 0, TDMT_SCH_EXPLAIN, &trans, false, &pExplainMsgSendInfo));
D
dapan1121 已提交
791

D
dapan1121 已提交
792
  int32_t    msgType = TDMT_SCH_EXPLAIN_RSP;
D
dapan1121 已提交
793
  SRpcCtxVal ctxVal = {.val = pExplainMsgSendInfo, .clone = schCloneSMsgSendInfo};
D
dapan1121 已提交
794 795
  if (taosHashPut(pCtx->args, &msgType, sizeof(msgType), &ctxVal, sizeof(ctxVal))) {
    SCH_TASK_ELOG("taosHashPut msg %d to rpcCtx failed", msgType);
S
Shengliang Guan 已提交
796
    SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
797 798 799
  }

  SCH_ERR_JRET(schMakeBrokenLinkVal(pJob, pTask, &pCtx->brokenVal, false));
D
dapan1121 已提交
800
  pCtx->freeFunc = schFreeRpcCtxVal;
D
dapan1121 已提交
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820

  return TSDB_CODE_SUCCESS;

_return:

  taosHashCleanup(pCtx->args);

  if (pExplainMsgSendInfo) {
    taosMemoryFreeClear(pExplainMsgSendInfo->param);
    taosMemoryFreeClear(pExplainMsgSendInfo);
  }

  SCH_RET(code);
}

int32_t schCloneCallbackParam(SSchCallbackParamHeader *pSrc, SSchCallbackParamHeader **pDst) {
  if (pSrc->isHbParam) {
    SSchHbCallbackParam *dst = taosMemoryMalloc(sizeof(SSchHbCallbackParam));
    if (NULL == dst) {
      qError("malloc SSchHbCallbackParam failed");
S
Shengliang Guan 已提交
821
      SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
822 823 824 825 826 827 828 829 830 831 832
    }

    memcpy(dst, pSrc, sizeof(*dst));
    *pDst = (SSchCallbackParamHeader *)dst;

    return TSDB_CODE_SUCCESS;
  }

  SSchTaskCallbackParam *dst = taosMemoryMalloc(sizeof(SSchTaskCallbackParam));
  if (NULL == dst) {
    qError("malloc SSchTaskCallbackParam failed");
S
Shengliang Guan 已提交
833
    SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846 847
  }

  memcpy(dst, pSrc, sizeof(*dst));
  *pDst = (SSchCallbackParamHeader *)dst;

  return TSDB_CODE_SUCCESS;
}

int32_t schCloneSMsgSendInfo(void *src, void **dst) {
  SMsgSendInfo *pSrc = src;
  int32_t       code = 0;
  SMsgSendInfo *pDst = taosMemoryMalloc(sizeof(*pSrc));
  if (NULL == pDst) {
    qError("malloc SMsgSendInfo for rpcCtx failed, len:%d", (int32_t)sizeof(*pSrc));
S
Shengliang Guan 已提交
848
    SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
849 850 851 852 853 854
  }

  memcpy(pDst, pSrc, sizeof(*pSrc));
  pDst->param = NULL;

  SCH_ERR_JRET(schCloneCallbackParam(pSrc->param, (SSchCallbackParamHeader **)&pDst->param));
H
more  
Hongze Cheng 已提交
855
  pDst->paramFreeFp = taosMemoryFree;
D
dapan1121 已提交
856 857 858 859 860 861 862 863 864 865 866

  *dst = pDst;

  return TSDB_CODE_SUCCESS;

_return:

  taosMemoryFreeClear(pDst);
  SCH_RET(code);
}

D
dapan1121 已提交
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
int32_t schUpdateSendTargetInfo(SMsgSendInfo *pMsgSendInfo, SQueryNodeAddr *addr, SSchTask *pTask) {
  if (NULL == pTask || addr->nodeId < MNODE_HANDLE) {
    return TSDB_CODE_SUCCESS;
  }

  if (addr->nodeId == MNODE_HANDLE) {
    pMsgSendInfo->target.type = TARGET_TYPE_MNODE;
  } else {
    pMsgSendInfo->target.type = TARGET_TYPE_VNODE;
    pMsgSendInfo->target.vgId = addr->nodeId;
    pMsgSendInfo->target.dbFName = strdup(pTask->plan->dbFName);
  }

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
882

883 884
int32_t schAsyncSendMsg(SSchJob *pJob, SSchTask *pTask, SSchTrans *trans, SQueryNodeAddr *addr, int32_t msgType,
                        void *msg, uint32_t msgSize, bool persistHandle, SRpcCtx *ctx) {
D
dapan1121 已提交
885
  int32_t code = 0;
D
dapan1121 已提交
886
  SEpSet *epSet = &addr->epSet;
D
dapan1121 已提交
887 888

  SMsgSendInfo *pMsgSendInfo = NULL;
889
  bool          isHb = (TDMT_SCH_QUERY_HEARTBEAT == msgType);
D
dapan1121 已提交
890
  SCH_ERR_JRET(schGenerateCallBackInfo(pJob, pTask, msg, msgSize, msgType, trans, isHb, &pMsgSendInfo));
891
  SCH_ERR_JRET(schUpdateSendTargetInfo(pMsgSendInfo, addr, pTask));
D
dapan1121 已提交
892

893 894 895 896 897 898 899 900
  if (pJob && pTask) {
    SCH_TASK_DLOG("start to send %s msg to node[%d,%s,%d], pTrans:%p, pHandle:%p", TMSG_INFO(msgType), addr->nodeId,
           epSet->eps[epSet->inUse].fqdn, epSet->eps[epSet->inUse].port, trans->pTrans, trans->pHandle);
  } else {
    qDebug("start to send %s msg to node[%d,%s,%d], pTrans:%p, pHandle:%p", TMSG_INFO(msgType), addr->nodeId,
           epSet->eps[epSet->inUse].fqdn, epSet->eps[epSet->inUse].port, trans->pTrans, trans->pHandle);
  }
  
D
dapan1121 已提交
901 902 903
  if (pTask) {
    pTask->lastMsgType = msgType;
  }
D
dapan1121 已提交
904

D
dapan1121 已提交
905
  int64_t transporterId = 0;
D
dapan1121 已提交
906
  code = asyncSendMsgToServerExt(trans->pTrans, epSet, &transporterId, pMsgSendInfo, persistHandle, ctx);
D
dapan1121 已提交
907
  pMsgSendInfo = NULL;
D
dapan1121 已提交
908 909 910 911
  if (code) {
    SCH_ERR_JRET(code);
  }

D
dapan1121 已提交
912 913 914 915 916
  if (pJob) {
    SCH_TASK_DLOG("req msg sent, type:%d, %s", msgType, TMSG_INFO(msgType));
  } else {
    qDebug("req msg sent, type:%d, %s", msgType, TMSG_INFO(msgType));
  }
D
dapan1121 已提交
917 918 919 920
  return TSDB_CODE_SUCCESS;

_return:

D
dapan1121 已提交
921 922 923 924 925 926
  if (pJob) {
    SCH_TASK_ELOG("fail to send msg, type:%d, %s, error:%s", msgType, TMSG_INFO(msgType), tstrerror(code));
  } else {
    qError("fail to send msg, type:%d, %s, error:%s", msgType, TMSG_INFO(msgType), tstrerror(code));
  }

D
dapan1121 已提交
927
  if (pMsgSendInfo) {
D
dapan1121 已提交
928
    destroySendMsgInfo(pMsgSendInfo);
D
dapan1121 已提交
929 930 931 932 933
  }

  SCH_RET(code);
}

934
int32_t schBuildAndSendHbMsg(SQueryNodeEpId *nodeEpId, SArray *taskAction) {
D
dapan1121 已提交
935 936 937 938
  SSchedulerHbReq req = {0};
  int32_t         code = 0;
  SRpcCtx         rpcCtx = {0};
  SSchTrans       trans = {0};
D
dapan1121 已提交
939
  int32_t         msgType = TDMT_SCH_QUERY_HEARTBEAT;
D
dapan1121 已提交
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964

  req.header.vgId = nodeEpId->nodeId;
  req.sId = schMgmt.sId;
  memcpy(&req.epId, nodeEpId, sizeof(SQueryNodeEpId));

  SCH_LOCK(SCH_READ, &schMgmt.hbLock);
  SSchHbTrans *hb = taosHashGet(schMgmt.hbConnections, nodeEpId, sizeof(SQueryNodeEpId));
  if (NULL == hb) {
    SCH_UNLOCK(SCH_READ, &schMgmt.hbLock);
    qError("hb connection no longer exist, nodeId:%d, fqdn:%s, port:%d", nodeEpId->nodeId, nodeEpId->ep.fqdn,
           nodeEpId->ep.port);
    return TSDB_CODE_SUCCESS;
  }

  SCH_LOCK(SCH_WRITE, &hb->lock);
  code = schCloneHbRpcCtx(&hb->rpcCtx, &rpcCtx);
  memcpy(&trans, &hb->trans, sizeof(trans));
  SCH_UNLOCK(SCH_WRITE, &hb->lock);
  SCH_UNLOCK(SCH_READ, &schMgmt.hbLock);

  SCH_ERR_RET(code);

  int32_t msgSize = tSerializeSSchedulerHbReq(NULL, 0, &req);
  if (msgSize < 0) {
    qError("tSerializeSSchedulerHbReq hbReq failed, size:%d", msgSize);
S
Shengliang Guan 已提交
965
    SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
966 967 968 969
  }
  void *msg = taosMemoryCalloc(1, msgSize);
  if (NULL == msg) {
    qError("calloc hb req %d failed", msgSize);
S
Shengliang Guan 已提交
970
    SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
971 972 973 974
  }

  if (tSerializeSSchedulerHbReq(msg, msgSize, &req) < 0) {
    qError("tSerializeSSchedulerHbReq hbReq failed, size:%d", msgSize);
S
Shengliang Guan 已提交
975
    SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
976 977
  }

978
  int64_t        transporterId = 0;
D
dapan1121 已提交
979 980 981 982 983
  SQueryNodeAddr addr = {.nodeId = nodeEpId->nodeId};
  addr.epSet.inUse = 0;
  addr.epSet.numOfEps = 1;
  memcpy(&addr.epSet.eps[0], &nodeEpId->ep, sizeof(nodeEpId->ep));

D
dapan1121 已提交
984 985 986
  code = schAsyncSendMsg(NULL, NULL, &trans, &addr, msgType, msg, msgSize, true, &rpcCtx);
  msg = NULL;
  SCH_ERR_JRET(code);
D
dapan1121 已提交
987 988 989 990 991 992 993 994 995 996

  return TSDB_CODE_SUCCESS;

_return:

  taosMemoryFreeClear(msg);
  schFreeRpcCtx(&rpcCtx);
  SCH_RET(code);
}

D
dapan1121 已提交
997
int32_t schBuildAndSendMsg(SSchJob *pJob, SSchTask *pTask, SQueryNodeAddr *addr, int32_t msgType) {
D
dapan1121 已提交
998
  int32_t msgSize = 0;
D
dapan1121 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007
  void    *msg = NULL;
  int32_t  code = 0;
  bool     isCandidateAddr = false;
  bool     persistHandle = false;
  SRpcCtx  rpcCtx = {0};

  if (NULL == addr) {
    addr = taosArrayGet(pTask->candidateAddrs, pTask->candidateIdx);
    isCandidateAddr = true;
1008 1009
    SCH_TASK_DLOG("target candidateIdx %d, epInUse %d/%d", pTask->candidateIdx, addr->epSet.inUse,
                  addr->epSet.numOfEps);
D
dapan1121 已提交
1010 1011 1012 1013 1014 1015
  }

  switch (msgType) {
    case TDMT_VND_CREATE_TABLE:
    case TDMT_VND_DROP_TABLE:
    case TDMT_VND_ALTER_TABLE:
X
Xiaoyu Wang 已提交
1016 1017
    case TDMT_VND_SUBMIT:
    case TDMT_VND_COMMIT: {
D
dapan1121 已提交
1018 1019 1020 1021
      msgSize = pTask->msgLen;
      msg = taosMemoryCalloc(1, msgSize);
      if (NULL == msg) {
        SCH_TASK_ELOG("calloc %d failed", msgSize);
S
Shengliang Guan 已提交
1022
        SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1023 1024 1025 1026 1027 1028
      }

      memcpy(msg, pTask->msg, msgSize);
      break;
    }

D
dapan1121 已提交
1029 1030
    case TDMT_VND_DELETE: {
      SVDeleteReq req = {0};
D
dapan1121 已提交
1031
      req.header.vgId = addr->nodeId;
D
dapan1121 已提交
1032 1033 1034 1035 1036
      req.sId = schMgmt.sId;
      req.queryId = pJob->queryId;
      req.taskId = pTask->taskId;
      req.phyLen = pTask->msgLen;
      req.sqlLen = strlen(pJob->sql);
1037
      req.sql = (char *)pJob->sql;
D
dapan1121 已提交
1038
      req.msg = pTask->msg;
D
dapan1121 已提交
1039 1040
      msgSize = tSerializeSVDeleteReq(NULL, 0, &req);
      msg = taosMemoryCalloc(1, msgSize);
D
dapan1121 已提交
1041
      if (NULL == msg) {
D
dapan1121 已提交
1042
        SCH_TASK_ELOG("calloc %d failed", msgSize);
S
Shengliang Guan 已提交
1043
        SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1044 1045
      }

D
dapan1121 已提交
1046
      tSerializeSVDeleteReq(msg, msgSize, &req);
D
dapan1121 已提交
1047 1048
      break;
    }
1049
    case TDMT_SCH_QUERY:
D
dapan1121 已提交
1050
    case TDMT_SCH_MERGE_QUERY: {
D
dapan1121 已提交
1051 1052
      SCH_ERR_RET(schMakeQueryRpcCtx(pJob, pTask, &rpcCtx));

D
dapan1121 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
      SSubQueryMsg qMsg;
      qMsg.header.vgId = addr->nodeId;
      qMsg.header.contLen = 0;
      qMsg.sId = schMgmt.sId;
      qMsg.queryId = pJob->queryId;
      qMsg.taskId = pTask->taskId;
      qMsg.refId = pJob->refId;
      qMsg.execId = pTask->execId;
      qMsg.msgMask = (pTask->plan->showRewrite) ? QUERY_MSG_MASK_SHOW_REWRITE() : 0;
      qMsg.taskType = TASK_TYPE_TEMP;
      qMsg.explain = SCH_IS_EXPLAIN_JOB(pJob);
      qMsg.needFetch = SCH_TASK_NEED_FETCH(pTask);
      qMsg.sqlLen = strlen(pJob->sql);
      qMsg.sql = pJob->sql;
      qMsg.msgLen = pTask->msgLen;
      qMsg.msg = pTask->msg;

      msgSize = tSerializeSSubQueryMsg(NULL, 0, &qMsg);
      if (msgSize < 0) {
        SCH_TASK_ELOG("tSerializeSSubQueryMsg get size, msgSize:%d", msgSize);
S
Shengliang Guan 已提交
1073
        SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1074 1075
      }
      
D
dapan1121 已提交
1076 1077 1078
      msg = taosMemoryCalloc(1, msgSize);
      if (NULL == msg) {
        SCH_TASK_ELOG("calloc %d failed", msgSize);
S
Shengliang Guan 已提交
1079
        SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1080 1081
      }

D
dapan1121 已提交
1082 1083 1084
      if (tSerializeSSubQueryMsg(msg, msgSize, &qMsg) < 0) {
        SCH_TASK_ELOG("tSerializeSSubQueryMsg failed, msgSize:%d", msgSize);
        taosMemoryFree(msg);
S
Shengliang Guan 已提交
1085
        SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1086
      }
D
dapan1121 已提交
1087 1088

      persistHandle = true;
D
dapan1121 已提交
1089
      SCH_SET_TASK_HANDLE(pTask, rpcAllocHandle());
D
dapan1121 已提交
1090 1091
      break;
    }
D
dapan1121 已提交
1092 1093
    case TDMT_SCH_FETCH:
    case TDMT_SCH_MERGE_FETCH: {
D
dapan1121 已提交
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
      SResFetchReq req = {0};
      req.header.vgId = addr->nodeId;
      req.sId = schMgmt.sId;
      req.queryId = pJob->queryId;
      req.taskId = pTask->taskId;
      req.execId = pTask->execId;

      msgSize = tSerializeSResFetchReq(NULL, 0, &req);
      if (msgSize < 0) {
        SCH_TASK_ELOG("tSerializeSResFetchReq get size, msgSize:%d", msgSize);
S
Shengliang Guan 已提交
1104
        SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1105 1106
      }
      
D
dapan1121 已提交
1107 1108 1109
      msg = taosMemoryCalloc(1, msgSize);
      if (NULL == msg) {
        SCH_TASK_ELOG("calloc %d failed", msgSize);
S
Shengliang Guan 已提交
1110
        SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1111 1112
      }

D
dapan1121 已提交
1113 1114
      if (tSerializeSResFetchReq(msg, msgSize, &req) < 0) {
        SCH_TASK_ELOG("tSerializeSResFetchReq %d failed", msgSize);
S
Shengliang Guan 已提交
1115
        SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1116
      }
D
dapan1121 已提交
1117 1118
      break;
    }
D
dapan1121 已提交
1119
    case TDMT_SCH_DROP_TASK: {
D
dapan1121 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
      STaskDropReq qMsg;
      qMsg.header.vgId = addr->nodeId;
      qMsg.header.contLen = 0;
      qMsg.sId = schMgmt.sId;
      qMsg.queryId = pJob->queryId;
      qMsg.taskId = pTask->taskId;
      qMsg.refId = pJob->refId;
      qMsg.execId = pTask->execId;

      msgSize = tSerializeSTaskDropReq(NULL, 0, &qMsg);
      if (msgSize < 0) {
        SCH_TASK_ELOG("tSerializeSTaskDropReq get size, msgSize:%d", msgSize);
S
Shengliang Guan 已提交
1132
        SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1133 1134
      }
      
D
dapan1121 已提交
1135 1136 1137
      msg = taosMemoryCalloc(1, msgSize);
      if (NULL == msg) {
        SCH_TASK_ELOG("calloc %d failed", msgSize);
S
Shengliang Guan 已提交
1138
        SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1139 1140
      }

D
dapan1121 已提交
1141 1142 1143
      if (tSerializeSTaskDropReq(msg, msgSize, &qMsg) < 0) {
        SCH_TASK_ELOG("tSerializeSTaskDropReq failed, msgSize:%d", msgSize);
        taosMemoryFree(msg);
S
Shengliang Guan 已提交
1144
        SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1145
      }
D
dapan1121 已提交
1146 1147
      break;
    }
D
dapan1121 已提交
1148
    case TDMT_SCH_QUERY_HEARTBEAT: {
D
dapan1121 已提交
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
      SCH_ERR_RET(schMakeHbRpcCtx(pJob, pTask, &rpcCtx));

      SSchedulerHbReq req = {0};
      req.sId = schMgmt.sId;
      req.header.vgId = addr->nodeId;
      req.epId.nodeId = addr->nodeId;
      memcpy(&req.epId.ep, SCH_GET_CUR_EP(addr), sizeof(SEp));

      msgSize = tSerializeSSchedulerHbReq(NULL, 0, &req);
      if (msgSize < 0) {
        SCH_JOB_ELOG("tSerializeSSchedulerHbReq hbReq failed, size:%d", msgSize);
S
Shengliang Guan 已提交
1160
        SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1161 1162 1163 1164
      }
      msg = taosMemoryCalloc(1, msgSize);
      if (NULL == msg) {
        SCH_JOB_ELOG("calloc %d failed", msgSize);
S
Shengliang Guan 已提交
1165
        SCH_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1166 1167 1168
      }
      if (tSerializeSSchedulerHbReq(msg, msgSize, &req) < 0) {
        SCH_JOB_ELOG("tSerializeSSchedulerHbReq hbReq failed, size:%d", msgSize);
S
Shengliang Guan 已提交
1169
        SCH_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
      }

      persistHandle = true;
      break;
    }
    default:
      SCH_TASK_ELOG("unknown msg type to send, msgType:%d", msgType);
      SCH_ERR_RET(TSDB_CODE_SCH_INTERNAL_ERROR);
  }

D
dapan1121 已提交
1180
#if 1
D
dapan1121 已提交
1181
  SSchTrans trans = {.pTrans = pJob->conn.pTrans, .pHandle = SCH_GET_TASK_HANDLE(pTask)};
D
dapan1121 已提交
1182
  code = schAsyncSendMsg(pJob, pTask, &trans, addr, msgType, msg, (uint32_t)msgSize, persistHandle, (rpcCtx.args ? &rpcCtx : NULL));
D
dapan1121 已提交
1183 1184
  msg = NULL;
  SCH_ERR_JRET(code);
H
more  
Hongze Cheng 已提交
1185

D
dapan1121 已提交
1186
  if (msgType == TDMT_SCH_QUERY || msgType == TDMT_SCH_MERGE_QUERY) {
D
dapan1121 已提交
1187
    SCH_ERR_RET(schAppendTaskExecNode(pJob, pTask, addr, pTask->execId));
D
dapan1121 已提交
1188
  }
H
more  
Hongze Cheng 已提交
1189 1190 1191
#else
  if (TDMT_VND_SUBMIT != msgType) {
    SSchTrans trans = {.pTrans = pJob->conn.pTrans, .pHandle = SCH_GET_TASK_HANDLE(pTask)};
dengyihao's avatar
dengyihao 已提交
1192
    code = schAsyncSendMsg(pJob, pTask, &trans, addr, msgType, msg, msgSize, persistHandle, (rpcCtx.args ? &rpcCtx : NULL));
H
more  
Hongze Cheng 已提交
1193 1194 1195 1196 1197 1198 1199
    msg = NULL;
    SCH_ERR_JRET(code);

    if (msgType == TDMT_SCH_QUERY || msgType == TDMT_SCH_MERGE_QUERY) {
      SCH_ERR_RET(schAppendTaskExecNode(pJob, pTask, addr, pTask->execId));
    }
  } else {
X
Xiaoyu Wang 已提交
1200
    taosMemoryFree(msg);
H
more  
Hongze Cheng 已提交
1201 1202 1203
    SCH_ERR_RET(schProcessOnTaskSuccess(pJob, pTask));
  }
#endif
D
dapan1121 已提交
1204 1205 1206 1207 1208

  return TSDB_CODE_SUCCESS;

_return:

D
dapan1121 已提交
1209
  pTask->lastMsgType = -1;
D
dapan1121 已提交
1210 1211 1212 1213 1214
  schFreeRpcCtx(&rpcCtx);

  taosMemoryFreeClear(msg);
  SCH_RET(code);
}
dengyihao's avatar
dengyihao 已提交
1215
// clang-format on