vnodeQuery.c 6.4 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

H
Hongze Cheng 已提交
16
#include "vnodeInt.h"
D
dapan1121 已提交
17

S
Shengliang 已提交
18
static int     vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg);
H
Haojun Liao 已提交
19

S
Shengliang Guan 已提交
20
int vnodeQueryOpen(SVnode *pVnode) {
S
Shengliang Guan 已提交
21
  return qWorkerInit(NODE_TYPE_VNODE, pVnode->vgId, NULL, (void **)&pVnode->pQuery, &pVnode->msgCb);
S
Shengliang Guan 已提交
22
}
D
dapan1121 已提交
23

L
Liu Jicong 已提交
24
void vnodeQueryClose(SVnode *pVnode) { qWorkerDestroy((void **)&pVnode->pQuery); }
D
dapan1121 已提交
25

S
Shengliang 已提交
26
int vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
D
dapan1121 已提交
27
  vTrace("message in query queue is processing");
D
dapan1121 已提交
28
  SReadHandle handle = {.reader = pVnode->pTsdb, .meta = pVnode->pMeta, .config = &pVnode->config};
D
dapan1121 已提交
29 30

  switch (pMsg->msgType) {
D
dapan1121 已提交
31
    case TDMT_VND_QUERY:
H
Haojun Liao 已提交
32
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg);
D
dapan1121 已提交
33
    case TDMT_VND_QUERY_CONTINUE:
H
Haojun Liao 已提交
34
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg);
D
dapan1121 已提交
35 36 37 38
    default:
      vError("unknown msg type:%d in query queue", pMsg->msgType);
      return TSDB_CODE_VND_APP_ERROR;
  }
D
dapan1121 已提交
39 40
}

L
Liu Jicong 已提交
41
int vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
D
dapan1121 已提交
42
  vTrace("message in fetch queue is processing");
L
Liu Jicong 已提交
43 44
  char   *msgstr = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
  int32_t msgLen = pMsg->contLen - sizeof(SMsgHead);
D
dapan1121 已提交
45 46 47
  switch (pMsg->msgType) {
    case TDMT_VND_FETCH:
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg);
S
Shengliang 已提交
48 49
    case TDMT_VND_FETCH_RSP:
      return qWorkerProcessFetchRsp(pVnode, pVnode->pQuery, pMsg);
D
dapan1121 已提交
50 51 52 53 54 55 56 57
    case TDMT_VND_RES_READY:
      return qWorkerProcessReadyMsg(pVnode, pVnode->pQuery, pMsg);
    case TDMT_VND_TASKS_STATUS:
      return qWorkerProcessStatusMsg(pVnode, pVnode->pQuery, pMsg);
    case TDMT_VND_CANCEL_TASK:
      return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg);
    case TDMT_VND_DROP_TASK:
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg);
D
dapan1121 已提交
58
    case TDMT_VND_TABLE_META:
S
Shengliang 已提交
59
      return vnodeGetTableMeta(pVnode, pMsg);
L
Liu Jicong 已提交
60
    case TDMT_VND_CONSUME:
L
fix  
Liu Jicong 已提交
61
      return tqProcessPollReq(pVnode->pTq, pMsg, pInfo->workerId);
L
Liu Jicong 已提交
62 63
    case TDMT_VND_TASK_PIPE_EXEC:
    case TDMT_VND_TASK_MERGE_EXEC:
64
      return tqProcessTaskExec(pVnode->pTq, msgstr, msgLen, 0);
L
Liu Jicong 已提交
65
    case TDMT_VND_STREAM_TRIGGER:
66
      return tqProcessStreamTrigger(pVnode->pTq, pMsg->pCont, pMsg->contLen, 0);
D
dapan1121 已提交
67 68
    case TDMT_VND_QUERY_HEARTBEAT:
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg);
D
dapan1121 已提交
69 70 71 72
    default:
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
      return TSDB_CODE_VND_APP_ERROR;
  }
D
dapan1121 已提交
73 74
}

S
Shengliang 已提交
75
static int vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg) {
L
Liu Jicong 已提交
76 77
  STbCfg         *pTbCfg = NULL;
  STbCfg         *pStbCfg = NULL;
H
Hongze Cheng 已提交
78 79 80
  tb_uid_t        uid;
  int32_t         nCols;
  int32_t         nTagCols;
H
Haojun Liao 已提交
81 82
  SSchemaWrapper *pSW = NULL;
  STableMetaRsp  *pTbMetaMsg = NULL;
S
Shengliang Guan 已提交
83 84
  STableMetaRsp   metaRsp = {0};
  SSchema        *pTagSchema;
H
more  
Hongze Cheng 已提交
85
  SRpcMsg         rpcMsg;
D
dapan1121 已提交
86
  int             msgLen = 0;
D
dapan1121 已提交
87
  int32_t         code = 0;
D
dapan1121 已提交
88
  char            tableFName[TSDB_TABLE_FNAME_LEN];
D
dapan1121 已提交
89 90
  int32_t         rspLen = 0;
  void           *pRsp = NULL;
H
more  
Hongze Cheng 已提交
91

S
Shengliang Guan 已提交
92 93
  STableInfoReq infoReq = {0};
  if (tDeserializeSTableInfoReq(pMsg->pCont, pMsg->contLen, &infoReq) != 0) {
D
dapan1121 已提交
94
    code = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
95 96 97
    goto _exit;
  }

D
dapan1121 已提交
98 99 100 101 102 103 104 105 106 107
  metaRsp.dbId = pVnode->config.dbId;
  memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
  strcpy(metaRsp.tbName, infoReq.tbName);

  sprintf(tableFName, "%s.%s", infoReq.dbFName, infoReq.tbName);
  code = vnodeValidateTableHash(&pVnode->config, tableFName);
  if (code) {
    goto _exit;
  }

S
Shengliang Guan 已提交
108
  pTbCfg = metaGetTbInfoByName(pVnode->pMeta, infoReq.tbName, &uid);
H
Hongze Cheng 已提交
109
  if (pTbCfg == NULL) {
H
Haojun Liao 已提交
110
    code = TSDB_CODE_VND_TB_NOT_EXIST;
H
more  
Hongze Cheng 已提交
111
    goto _exit;
H
Hongze Cheng 已提交
112 113 114 115 116
  }

  if (pTbCfg->type == META_CHILD_TABLE) {
    pStbCfg = metaGetTbInfoByUid(pVnode->pMeta, pTbCfg->ctbCfg.suid);
    if (pStbCfg == NULL) {
D
dapan1121 已提交
117
      code = TSDB_CODE_VND_TB_NOT_EXIST;
H
more  
Hongze Cheng 已提交
118
      goto _exit;
H
Hongze Cheng 已提交
119 120 121 122 123 124 125 126 127 128 129
    }

    pSW = metaGetTableSchema(pVnode->pMeta, pTbCfg->ctbCfg.suid, 0, true);
  } else {
    pSW = metaGetTableSchema(pVnode->pMeta, uid, 0, true);
  }

  nCols = pSW->nCols;
  if (pTbCfg->type == META_SUPER_TABLE) {
    nTagCols = pTbCfg->stbCfg.nTagCols;
    pTagSchema = pTbCfg->stbCfg.pTagSchema;
H
Hongze Cheng 已提交
130
  } else if (pTbCfg->type == META_CHILD_TABLE) {
H
Hongze Cheng 已提交
131 132 133 134 135
    nTagCols = pStbCfg->stbCfg.nTagCols;
    pTagSchema = pStbCfg->stbCfg.pTagSchema;
  } else {
    nTagCols = 0;
    pTagSchema = NULL;
H
more  
Hongze Cheng 已提交
136 137
  }

wafwerar's avatar
wafwerar 已提交
138
  metaRsp.pSchemas = taosMemoryCalloc(nCols + nTagCols, sizeof(SSchema));
S
Shengliang Guan 已提交
139
  if (metaRsp.pSchemas == NULL) {
D
dapan1121 已提交
140
    code = TSDB_CODE_VND_OUT_OF_MEMORY;
H
more  
Hongze Cheng 已提交
141
    goto _exit;
H
more  
Hongze Cheng 已提交
142 143
  }

H
Hongze Cheng 已提交
144
  if (pTbCfg->type == META_CHILD_TABLE) {
S
Shengliang Guan 已提交
145 146
    strcpy(metaRsp.stbName, pStbCfg->name);
    metaRsp.suid = pTbCfg->ctbCfg.suid;
D
dapan1121 已提交
147
  } else if (pTbCfg->type == META_SUPER_TABLE) {
S
Shengliang Guan 已提交
148 149
    strcpy(metaRsp.stbName, pTbCfg->name);
    metaRsp.suid = uid;
H
Hongze Cheng 已提交
150
  }
S
Shengliang Guan 已提交
151 152 153 154 155
  metaRsp.numOfTags = nTagCols;
  metaRsp.numOfColumns = nCols;
  metaRsp.tableType = pTbCfg->type;
  metaRsp.tuid = uid;
  metaRsp.vgId = pVnode->vgId;
H
Hongze Cheng 已提交
156

S
Shengliang Guan 已提交
157
  memcpy(metaRsp.pSchemas, pSW->pSchema, sizeof(SSchema) * pSW->nCols);
H
Hongze Cheng 已提交
158
  if (nTagCols) {
S
Shengliang Guan 已提交
159
    memcpy(POINTER_SHIFT(metaRsp.pSchemas, sizeof(SSchema) * pSW->nCols), pTagSchema, sizeof(SSchema) * nTagCols);
H
Hongze Cheng 已提交
160 161
  }

D
dapan1121 已提交
162 163 164
_exit:

  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
S
Shengliang Guan 已提交
165 166 167 168 169
  if (rspLen < 0) {
    code = TSDB_CODE_INVALID_MSG;
    goto _exit;
  }

D
dapan1121 已提交
170
  pRsp = rpcMallocCont(rspLen);
S
Shengliang Guan 已提交
171 172 173
  if (pRsp == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
H
Hongze Cheng 已提交
174
  }
S
Shengliang Guan 已提交
175
  tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
H
more  
Hongze Cheng 已提交
176

S
Shengliang Guan 已提交
177
  tFreeSTableMetaRsp(&metaRsp);
H
Haojun Liao 已提交
178
  if (pSW != NULL) {
wafwerar's avatar
wafwerar 已提交
179 180
    taosMemoryFreeClear(pSW->pSchema);
    taosMemoryFreeClear(pSW);
H
Hongze Cheng 已提交
181
  }
H
Haojun Liao 已提交
182 183

  if (pTbCfg) {
wafwerar's avatar
wafwerar 已提交
184
    taosMemoryFreeClear(pTbCfg->name);
H
Haojun Liao 已提交
185
    if (pTbCfg->type == META_SUPER_TABLE) {
wafwerar's avatar
wafwerar 已提交
186
      taosMemoryFree(pTbCfg->stbCfg.pTagSchema);
H
Haojun Liao 已提交
187 188 189 190
    } else if (pTbCfg->type == META_SUPER_TABLE) {
      kvRowFree(pTbCfg->ctbCfg.pTag);
    }

wafwerar's avatar
wafwerar 已提交
191
    taosMemoryFreeClear(pTbCfg);
H
Haojun Liao 已提交
192 193
  }

D
dapan1121 已提交
194 195
  rpcMsg.handle = pMsg->handle;
  rpcMsg.ahandle = pMsg->ahandle;
S
Shengliang Guan 已提交
196 197
  rpcMsg.pCont = pRsp;
  rpcMsg.contLen = rspLen;
D
dapan1121 已提交
198
  rpcMsg.code = code;
D
dapan1121 已提交
199

S
shm  
Shengliang Guan 已提交
200
  tmsgSendRsp(&rpcMsg);
D
dapan1121 已提交
201
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
202
}