mndQuery.c 6.0 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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 "mndQuery.h"
#include "executor.h"
S
Shengliang Guan 已提交
18
#include "mndMnode.h"
D
dapan1121 已提交
19 20
#include "qworker.h"

21
int32_t mndPreProcessQueryMsg(SRpcMsg *pMsg) {
D
dapan1121 已提交
22
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) return 0;
23
  SMnode *pMnode = pMsg->info.node;
D
dapan1121 已提交
24 25 26
  return qWorkerPreprocessQueryMsg(pMnode->pQuery, pMsg);
}

27
void mndPostProcessQueryMsg(SRpcMsg *pMsg) {
D
dapan1121 已提交
28
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) return;
D
dapan1121 已提交
29 30 31 32
  SMnode *pMnode = pMsg->info.node;
  qWorkerAbortPreprocessQueryMsg(pMnode->pQuery, pMsg);
}

S
Shengliang Guan 已提交
33
int32_t mndProcessQueryMsg(SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
34
  int32_t     code = -1;
S
Shengliang Guan 已提交
35
  SMnode     *pMnode = pMsg->info.node;
36
  SReadHandle handle = {.mnd = pMnode, .pMsgCb = &pMnode->msgCb};
D
dapan1121 已提交
37

S
Shengliang Guan 已提交
38 39
  mTrace("msg:%p, in query queue is processing", pMsg);
  switch (pMsg->msgType) {
D
dapan1121 已提交
40
    case TDMT_SCH_QUERY:
D
dapan1121 已提交
41
    case TDMT_SCH_MERGE_QUERY:
D
dapan1121 已提交
42
      code = qWorkerProcessQueryMsg(&handle, pMnode->pQuery, pMsg, 0);
S
Shengliang Guan 已提交
43
      break;
D
dapan1121 已提交
44
    case TDMT_SCH_QUERY_CONTINUE:
D
dapan1121 已提交
45
      code = qWorkerProcessCQueryMsg(&handle, pMnode->pQuery, pMsg, 0);
S
Shengliang Guan 已提交
46
      break;
D
dapan1121 已提交
47
    case TDMT_SCH_FETCH:
D
dapan1121 已提交
48
    case TDMT_SCH_MERGE_FETCH:
D
dapan1121 已提交
49
      code = qWorkerProcessFetchMsg(pMnode, pMnode->pQuery, pMsg, 0);
S
Shengliang Guan 已提交
50
      break;
D
dapan1121 已提交
51
    case TDMT_SCH_DROP_TASK:
D
dapan1121 已提交
52
      code = qWorkerProcessDropMsg(pMnode, pMnode->pQuery, pMsg, 0);
S
Shengliang Guan 已提交
53
      break;
D
dapan1121 已提交
54
    case TDMT_SCH_QUERY_HEARTBEAT:
D
dapan1121 已提交
55
      code = qWorkerProcessHbMsg(pMnode, pMnode->pQuery, pMsg, 0);
S
Shengliang Guan 已提交
56
      break;
D
dapan1121 已提交
57
    default:
S
Shengliang Guan 已提交
58
      terrno = TSDB_CODE_VND_APP_ERROR;
S
Shengliang Guan 已提交
59
      mError("unknown msg type:%d in query queue", pMsg->msgType);
D
dapan1121 已提交
60
  }
S
Shengliang Guan 已提交
61 62 63

  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
  return code;
D
dapan1121 已提交
64 65
}

66
int32_t mndProcessBatchMetaMsg(SRpcMsg *pMsg) {
H
Hongze Cheng 已提交
67 68 69 70 71
  int32_t    code = 0;
  int32_t    offset = 0;
  int32_t    rspSize = 0;
  SBatchReq *batchReq = (SBatchReq *)pMsg->pCont;
  int32_t    msgNum = ntohl(batchReq->msgNum);
72 73 74
  offset += sizeof(SBatchReq);
  SBatchMsg req = {0};
  SBatchRsp rsp = {0};
H
Hongze Cheng 已提交
75 76 77 78
  SRpcMsg   reqMsg = *pMsg;
  SRpcMsg   rspMsg = {0};
  void     *pRsp = NULL;
  SMnode   *pMnode = pMsg->info.node;
79

D
dapan1121 已提交
80 81 82 83 84 85
  if (msgNum >= MAX_META_MSG_IN_BATCH) {
    code = TSDB_CODE_INVALID_MSG;
    mError("too many msgs %d in mnode batch meta req", msgNum);
    goto _exit;
  }

H
Hongze Cheng 已提交
86
  SArray *batchRsp = taosArrayInit(msgNum, sizeof(SBatchRsp));
87 88 89 90
  if (NULL == batchRsp) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
H
Hongze Cheng 已提交
91

92
  for (int32_t i = 0; i < msgNum; ++i) {
H
Hongze Cheng 已提交
93
    req.msgIdx = ntohl(*(int32_t *)((char *)pMsg->pCont + offset));
D
dapan1121 已提交
94 95
    offset += sizeof(req.msgIdx);

H
Hongze Cheng 已提交
96
    req.msgType = ntohl(*(int32_t *)((char *)pMsg->pCont + offset));
97 98
    offset += sizeof(req.msgType);

H
Hongze Cheng 已提交
99
    req.msgLen = ntohl(*(int32_t *)((char *)pMsg->pCont + offset));
100 101
    offset += sizeof(req.msgLen);

H
Hongze Cheng 已提交
102
    req.msg = (char *)pMsg->pCont + offset;
103 104 105 106 107 108 109 110 111 112 113 114
    offset += req.msgLen;

    reqMsg.msgType = req.msgType;
    reqMsg.pCont = req.msg;
    reqMsg.contLen = req.msgLen;
    reqMsg.info.rsp = NULL;
    reqMsg.info.rspLen = 0;

    MndMsgFp fp = pMnode->msgFp[TMSG_INDEX(req.msgType)];
    if (fp == NULL) {
      mError("msg:%p, failed to get msg handle, app:%p type:%s", pMsg, pMsg->info.ahandle, TMSG_INFO(pMsg->msgType));
      terrno = TSDB_CODE_MSG_NOT_PROCESSED;
D
dapan1121 已提交
115
      taosArrayDestroy(batchRsp);
116 117 118
      return -1;
    }

D
dapan1121 已提交
119 120 121 122 123
    if ((*fp)(&reqMsg)) {
      rsp.rspCode = terrno;
    } else {
      rsp.rspCode = 0;
    }
D
dapan1121 已提交
124
    rsp.msgIdx = req.msgIdx;
125 126 127
    rsp.reqType = reqMsg.msgType;
    rsp.msgLen = reqMsg.info.rspLen;
    rsp.msg = reqMsg.info.rsp;
H
Hongze Cheng 已提交
128

129 130 131 132 133 134 135
    taosArrayPush(batchRsp, &rsp);

    rspSize += sizeof(rsp) + rsp.msgLen - POINTER_BYTES;
  }

  rspSize += sizeof(int32_t);
  offset = 0;
H
Hongze Cheng 已提交
136

137 138 139 140 141 142
  pRsp = rpcMallocCont(rspSize);
  if (pRsp == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
143
  *(int32_t *)((char *)pRsp + offset) = htonl(msgNum);
144 145 146
  offset += sizeof(msgNum);
  for (int32_t i = 0; i < msgNum; ++i) {
    SBatchRsp *p = taosArrayGet(batchRsp, i);
H
Hongze Cheng 已提交
147 148

    *(int32_t *)((char *)pRsp + offset) = htonl(p->reqType);
149
    offset += sizeof(p->reqType);
H
Hongze Cheng 已提交
150
    *(int32_t *)((char *)pRsp + offset) = htonl(p->msgIdx);
D
dapan1121 已提交
151
    offset += sizeof(p->msgIdx);
H
Hongze Cheng 已提交
152
    *(int32_t *)((char *)pRsp + offset) = htonl(p->msgLen);
153
    offset += sizeof(p->msgLen);
H
Hongze Cheng 已提交
154
    *(int32_t *)((char *)pRsp + offset) = htonl(p->rspCode);
155
    offset += sizeof(p->rspCode);
H
Hongze Cheng 已提交
156
    memcpy((char *)pRsp + offset, p->msg, p->msgLen);
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    offset += p->msgLen;

    rpcFreeCont(p->msg);
  }

  taosArrayDestroy(batchRsp);
  batchRsp = NULL;

_exit:

  pMsg->info.rsp = pRsp;
  pMsg->info.rspLen = rspSize;

  if (code) {
    mError("mnd get batch meta failed cause of %s", tstrerror(code));
  }

  taosArrayDestroyEx(batchRsp, tFreeSBatchRsp);

  return code;
}

D
dapan1121 已提交
179
int32_t mndInitQuery(SMnode *pMnode) {
D
dapan1121 已提交
180
  if (qWorkerInit(NODE_TYPE_MNODE, MNODE_HANDLE, (void **)&pMnode->pQuery, &pMnode->msgCb) != 0) {
S
Shengliang Guan 已提交
181 182
    mError("failed to init qworker in mnode since %s", terrstr());
    return -1;
D
dapan1121 已提交
183 184
  }

D
dapan1121 已提交
185
  mndSetMsgHandle(pMnode, TDMT_SCH_QUERY, mndProcessQueryMsg);
D
dapan1121 已提交
186
  mndSetMsgHandle(pMnode, TDMT_SCH_MERGE_QUERY, mndProcessQueryMsg);
D
dapan1121 已提交
187 188
  mndSetMsgHandle(pMnode, TDMT_SCH_QUERY_CONTINUE, mndProcessQueryMsg);
  mndSetMsgHandle(pMnode, TDMT_SCH_FETCH, mndProcessQueryMsg);
D
dapan1121 已提交
189
  mndSetMsgHandle(pMnode, TDMT_SCH_MERGE_FETCH, mndProcessQueryMsg);
D
dapan1121 已提交
190 191
  mndSetMsgHandle(pMnode, TDMT_SCH_DROP_TASK, mndProcessQueryMsg);
  mndSetMsgHandle(pMnode, TDMT_SCH_QUERY_HEARTBEAT, mndProcessQueryMsg);
192
  mndSetMsgHandle(pMnode, TDMT_MND_BATCH_META, mndProcessBatchMetaMsg);
D
dapan1121 已提交
193 194 195 196 197

  return 0;
}

void mndCleanupQuery(SMnode *pMnode) { qWorkerDestroy((void **)&pMnode->pQuery); }