querymsg.c 10.0 KB
Newer Older
H
hzcheng 已提交
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 "tmsg.h"
D
dapan1121 已提交
17
#include "queryInt.h"
D
dapan1121 已提交
18
#include "query.h"
D
catalog  
dapan1121 已提交
19
#include "trpc.h"
D
dapan 已提交
20

S
Shengliang Guan 已提交
21 22 23
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-truncation"

S
Shengliang Guan 已提交
24 25
int32_t (*queryBuildMsg[TDMT_MAX])(void *input, char **msg, int32_t msgSize, int32_t *msgLen) = {0};
int32_t (*queryProcessMsgRsp[TDMT_MAX])(void *output, char *msg, int32_t msgSize) = {0};
D
dapan 已提交
26

X
Xiaoyu Wang 已提交
27 28 29
int32_t queryBuildUseDbOutput(SUseDbOutput *pOut, SUseDbRsp *usedbRsp) {
  memcpy(pOut->db, usedbRsp->db, TSDB_DB_FNAME_LEN);
  pOut->dbId = usedbRsp->uid;
D
dapan1121 已提交
30

X
Xiaoyu Wang 已提交
31 32 33 34 35 36 37
  pOut->dbVgroup = calloc(1, sizeof(SDBVgInfo));
  if (NULL == pOut->dbVgroup) {
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }

  pOut->dbVgroup->vgVersion = usedbRsp->vgVersion;
  pOut->dbVgroup->hashMethod = usedbRsp->hashMethod;
D
dapan1121 已提交
38 39 40 41 42

  if (usedbRsp->vgNum <= 0) {
    return TSDB_CODE_SUCCESS;
  }
  
X
Xiaoyu Wang 已提交
43 44 45 46 47 48 49 50
  pOut->dbVgroup->vgHash =
      taosHashInit(usedbRsp->vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
  if (NULL == pOut->dbVgroup->vgHash) {
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }

  for (int32_t i = 0; i < usedbRsp->vgNum; ++i) {
    SVgroupInfo *pVgInfo = taosArrayGet(usedbRsp->pVgroupInfos, i);
D
dapan 已提交
51
    pOut->dbVgroup->numOfTable += pVgInfo->numOfTable;
X
Xiaoyu Wang 已提交
52 53 54 55 56 57 58 59
    if (0 != taosHashPut(pOut->dbVgroup->vgHash, &pVgInfo->vgId, sizeof(int32_t), pVgInfo, sizeof(SVgroupInfo))) {
      return TSDB_CODE_TSC_OUT_OF_MEMORY;
    }
  }

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
60 61
int32_t queryBuildTableMetaReqMsg(void *input, char **msg, int32_t msgSize, int32_t *msgLen) {
  SBuildTableMetaInput *pInput = input;
D
dapan1121 已提交
62 63 64 65
  if (NULL == input || NULL == msg || NULL == msgLen) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

S
Shengliang Guan 已提交
66 67
  STableInfoReq infoReq = {0};
  infoReq.header.vgId = pInput->vgId;
S
Shengliang Guan 已提交
68
  if (pInput->dbFName) {
S
Shengliang Guan 已提交
69
    tstrncpy(infoReq.dbFName, pInput->dbFName, TSDB_DB_FNAME_LEN);
D
dapan1121 已提交
70
  }
S
Shengliang Guan 已提交
71
  tstrncpy(infoReq.tbName, pInput->tbName, TSDB_TABLE_NAME_LEN);
D
dapan1121 已提交
72

S
Shengliang Guan 已提交
73 74 75 76 77 78
  int32_t bufLen = tSerializeSTableInfoReq(NULL, 0, &infoReq);
  void   *pBuf = rpcMallocCont(bufLen);
  tSerializeSTableInfoReq(pBuf, bufLen, &infoReq);

  *msg = pBuf;
  *msgLen = bufLen;
D
dapan1121 已提交
79 80 81 82

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
83
int32_t queryBuildUseDbMsg(void *input, char **msg, int32_t msgSize, int32_t *msgLen) {
S
Shengliang Guan 已提交
84 85
  SBuildUseDBInput *pInput = input;
  if (NULL == pInput || NULL == msg || NULL == msgLen) {
D
dapan1121 已提交
86 87 88
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

S
Shengliang Guan 已提交
89
  SUseDbReq usedbReq = {0};
S
Shengliang Guan 已提交
90
  strncpy(usedbReq.db, pInput->db, sizeof(usedbReq.db));
S
Shengliang Guan 已提交
91
  usedbReq.db[sizeof(usedbReq.db) - 1] = 0;
S
Shengliang Guan 已提交
92
  usedbReq.vgVersion = pInput->vgVersion;
X
Xiaoyu Wang 已提交
93
  usedbReq.dbId = pInput->dbId;
D
dapan 已提交
94
  usedbReq.numOfTable = pInput->numOfTable;
D
dapan1121 已提交
95

S
Shengliang Guan 已提交
96 97 98
  int32_t bufLen = tSerializeSUseDbReq(NULL, 0, &usedbReq);
  void   *pBuf = rpcMallocCont(bufLen);
  tSerializeSUseDbReq(pBuf, bufLen, &usedbReq);
D
dapan1121 已提交
99

S
Shengliang Guan 已提交
100 101
  *msg = pBuf;
  *msgLen = bufLen;
D
dapan1121 已提交
102

S
Shengliang Guan 已提交
103
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
104 105
}

D
dapan1121 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
int32_t queryBuildQnodeListMsg(void *input, char **msg, int32_t msgSize, int32_t *msgLen) {
  if (NULL == msg || NULL == msgLen) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

  SQnodeListReq qnodeListReq = {0};
  qnodeListReq.rowNum = -1;

  int32_t bufLen = tSerializeSQnodeListReq(NULL, 0, &qnodeListReq);
  void   *pBuf = rpcMallocCont(bufLen);
  tSerializeSQnodeListReq(pBuf, bufLen, &qnodeListReq);

  *msg = pBuf;
  *msgLen = bufLen;

  return TSDB_CODE_SUCCESS;
}


S
Shengliang Guan 已提交
125
int32_t queryProcessUseDBRsp(void *output, char *msg, int32_t msgSize) {
S
Shengliang Guan 已提交
126 127 128 129
  SUseDbOutput *pOut = output;
  SUseDbRsp     usedbRsp = {0};
  int32_t       code = -1;

D
dapan1121 已提交
130
  if (NULL == output || NULL == msg || msgSize <= 0) {
S
Shengliang Guan 已提交
131 132
    code = TSDB_CODE_TSC_INVALID_INPUT;
    goto PROCESS_USEDB_OVER;
D
dapan1121 已提交
133 134
  }

S
Shengliang Guan 已提交
135 136
  if (tDeserializeSUseDbRsp(msg, msgSize, &usedbRsp) != 0) {
    qError("invalid use db rsp msg, msgSize:%d", msgSize);
S
Shengliang Guan 已提交
137 138
    code = TSDB_CODE_INVALID_MSG;
    goto PROCESS_USEDB_OVER;
D
dapan1121 已提交
139 140
  }

S
Shengliang Guan 已提交
141 142
  if (usedbRsp.vgNum < 0) {
    qError("invalid db[%s] vgroup number[%d]", usedbRsp.db, usedbRsp.vgNum);
S
Shengliang Guan 已提交
143 144
    code = TSDB_CODE_TSC_INVALID_VALUE;
    goto PROCESS_USEDB_OVER;
D
dapan1121 已提交
145 146
  }

X
Xiaoyu Wang 已提交
147
  code = queryBuildUseDbOutput(pOut, &usedbRsp);
148

S
Shengliang Guan 已提交
149
PROCESS_USEDB_OVER:
X
Xiaoyu Wang 已提交
150

S
Shengliang Guan 已提交
151 152 153 154 155 156
  if (code != 0) {
    if (pOut) {
      if (pOut->dbVgroup) taosHashCleanup(pOut->dbVgroup->vgHash);
      tfree(pOut->dbVgroup);
    }
    qError("failed to process usedb rsp since %s", terrstr());
D
dapan1121 已提交
157
  }
S
Shengliang Guan 已提交
158

S
Shengliang Guan 已提交
159
  tFreeSUsedbRsp(&usedbRsp);
D
dapan1121 已提交
160 161 162
  return code;
}

S
Shengliang Guan 已提交
163
static int32_t queryConvertTableMetaMsg(STableMetaRsp *pMetaMsg) {
D
dapan1121 已提交
164 165 166 167 168 169 170 171 172 173
  if (pMetaMsg->numOfTags < 0 || pMetaMsg->numOfTags > TSDB_MAX_TAGS) {
    qError("invalid numOfTags[%d] in table meta rsp msg", pMetaMsg->numOfTags);
    return TSDB_CODE_TSC_INVALID_VALUE;
  }

  if (pMetaMsg->numOfColumns > TSDB_MAX_COLUMNS || pMetaMsg->numOfColumns <= 0) {
    qError("invalid numOfColumns[%d] in table meta rsp msg", pMetaMsg->numOfColumns);
    return TSDB_CODE_TSC_INVALID_VALUE;
  }

S
Shengliang Guan 已提交
174
  if (pMetaMsg->tableType != TSDB_SUPER_TABLE && pMetaMsg->tableType != TSDB_CHILD_TABLE &&
X
Xiaoyu Wang 已提交
175
      pMetaMsg->tableType != TSDB_NORMAL_TABLE && pMetaMsg->tableType != TSDB_SYSTEM_TABLE) {
D
dapan1121 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189
    qError("invalid tableType[%d] in table meta rsp msg", pMetaMsg->tableType);
    return TSDB_CODE_TSC_INVALID_VALUE;
  }

  if (pMetaMsg->sversion < 0) {
    qError("invalid sversion[%d] in table meta rsp msg", pMetaMsg->sversion);
    return TSDB_CODE_TSC_INVALID_VALUE;
  }

  if (pMetaMsg->tversion < 0) {
    qError("invalid tversion[%d] in table meta rsp msg", pMetaMsg->tversion);
    return TSDB_CODE_TSC_INVALID_VALUE;
  }

S
Shengliang Guan 已提交
190 191
  if (pMetaMsg->pSchemas[0].colId != PRIMARYKEY_TIMESTAMP_COL_ID) {
    qError("invalid colId[%d] for the first column in table meta rsp msg", pMetaMsg->pSchemas[0].colId);
D
dapan1121 已提交
192 193 194 195 196 197
    return TSDB_CODE_TSC_INVALID_VALUE;
  }

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
198
int32_t queryCreateTableMetaFromMsg(STableMetaRsp *msg, bool isSuperTable, STableMeta **pMeta) {
D
dapan1121 已提交
199 200
  int32_t total = msg->numOfColumns + msg->numOfTags;
  int32_t metaSize = sizeof(STableMeta) + sizeof(SSchema) * total;
S
Shengliang Guan 已提交
201 202

  STableMeta *pTableMeta = calloc(1, metaSize);
D
dapan1121 已提交
203 204 205 206
  if (NULL == pTableMeta) {
    qError("calloc size[%d] failed", metaSize);
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }
D
dapan1121 已提交
207 208

  pTableMeta->vgId = isSuperTable ? 0 : msg->vgId;
D
dapan1121 已提交
209
  pTableMeta->tableType = isSuperTable ? TSDB_SUPER_TABLE : msg->tableType;
S
Shengliang Guan 已提交
210
  pTableMeta->uid = isSuperTable ? msg->suid : msg->tuid;
D
dapan1121 已提交
211 212 213 214 215 216 217 218
  pTableMeta->suid = msg->suid;
  pTableMeta->sversion = msg->sversion;
  pTableMeta->tversion = msg->tversion;

  pTableMeta->tableInfo.numOfTags = msg->numOfTags;
  pTableMeta->tableInfo.precision = msg->precision;
  pTableMeta->tableInfo.numOfColumns = msg->numOfColumns;

S
Shengliang Guan 已提交
219
  memcpy(pTableMeta->schema, msg->pSchemas, sizeof(SSchema) * total);
D
dapan1121 已提交
220

S
Shengliang Guan 已提交
221
  for (int32_t i = 0; i < msg->numOfColumns; ++i) {
D
dapan1121 已提交
222 223 224 225 226 227 228
    pTableMeta->tableInfo.rowSize += pTableMeta->schema[i].bytes;
  }

  *pMeta = pTableMeta;
  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
229
int32_t queryProcessTableMetaRsp(void *output, char *msg, int32_t msgSize) {
D
dapan1121 已提交
230
  int32_t       code = 0;
S
Shengliang Guan 已提交
231 232 233 234 235 236
  STableMetaRsp metaRsp = {0};

  if (NULL == output || NULL == msg || msgSize <= 0) {
    code = TSDB_CODE_TSC_INVALID_INPUT;
    goto PROCESS_META_OVER;
  }
D
dapan1121 已提交
237

S
Shengliang Guan 已提交
238 239 240 241 242 243
  if (tDeserializeSTableMetaRsp(msg, msgSize, &metaRsp) != 0) {
    code = TSDB_CODE_INVALID_MSG;
    goto PROCESS_META_OVER;
  }

  code = queryConvertTableMetaMsg(&metaRsp);
D
dapan1121 已提交
244
  if (code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
245
    goto PROCESS_META_OVER;
D
dapan1121 已提交
246 247
  }

D
dapan1121 已提交
248
  if (0 != strcmp(metaRsp.dbFName, TSDB_INFORMATION_SCHEMA_DB) && !tIsValidSchema(metaRsp.pSchemas, metaRsp.numOfColumns, metaRsp.numOfTags)) {
S
Shengliang Guan 已提交
249 250
    code = TSDB_CODE_TSC_INVALID_VALUE;
    goto PROCESS_META_OVER;
D
dapan1121 已提交
251 252
  }

S
Shengliang Guan 已提交
253 254 255
  STableMetaOutput *pOut = output;
  strcpy(pOut->dbFName, metaRsp.dbFName);
  pOut->dbId = metaRsp.dbId;
D
dapan1121 已提交
256

S
Shengliang Guan 已提交
257
  if (metaRsp.tableType == TSDB_CHILD_TABLE) {
D
dapan1121 已提交
258
    SET_META_TYPE_BOTH_TABLE(pOut->metaType);
D
dapan1121 已提交
259

S
Shengliang Guan 已提交
260 261
    strcpy(pOut->ctbName, metaRsp.tbName);
    strcpy(pOut->tbName, metaRsp.stbName);
D
dapan1121 已提交
262

S
Shengliang Guan 已提交
263 264 265 266 267 268
    pOut->ctbMeta.vgId = metaRsp.vgId;
    pOut->ctbMeta.tableType = metaRsp.tableType;
    pOut->ctbMeta.uid = metaRsp.tuid;
    pOut->ctbMeta.suid = metaRsp.suid;

    code = queryCreateTableMetaFromMsg(&metaRsp, true, &pOut->tbMeta);
D
dapan1121 已提交
269
  } else {
D
dapan1121 已提交
270
    SET_META_TYPE_TABLE(pOut->metaType);
S
Shengliang Guan 已提交
271 272 273 274 275 276
    strcpy(pOut->tbName, metaRsp.tbName);
    code = queryCreateTableMetaFromMsg(&metaRsp, (metaRsp.tableType == TSDB_SUPER_TABLE), &pOut->tbMeta);
  }

PROCESS_META_OVER:
  if (code != 0) {
D
dapan1121 已提交
277
    qError("failed to process table meta rsp since %s", tstrerror(code));
D
dapan1121 已提交
278
  }
S
Shengliang Guan 已提交
279 280

  tFreeSTableMetaRsp(&metaRsp);
D
dapan1121 已提交
281 282 283
  return code;
}

D
dapan1121 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

int32_t queryProcessQnodeListRsp(void *output, char *msg, int32_t msgSize) {
  SQnodeListRsp out = {0};
  int32_t       code = -1;

  if (NULL == output || NULL == msg || msgSize <= 0) {
    code = TSDB_CODE_TSC_INVALID_INPUT;
    goto PROCESS_QLIST_OVER;
  }

  if (tDeserializeSQnodeListRsp(msg, msgSize, &out) != 0) {
    qError("invalid qnode list rsp msg, msgSize:%d", msgSize);
    code = TSDB_CODE_INVALID_MSG;
    goto PROCESS_QLIST_OVER;
  }

PROCESS_QLIST_OVER:

  if (code != 0) {
    tFreeSQnodeListRsp(&out);
    out.epSetList = NULL;
  }

  *(SArray **)output = out.epSetList;

  return code;
}


313
void initQueryModuleMsgHandle() {
D
catalog  
dapan1121 已提交
314
  queryBuildMsg[TMSG_INDEX(TDMT_VND_TABLE_META)] = queryBuildTableMetaReqMsg;
D
dapan1121 已提交
315
  queryBuildMsg[TMSG_INDEX(TDMT_MND_TABLE_META)] = queryBuildTableMetaReqMsg;
D
catalog  
dapan1121 已提交
316
  queryBuildMsg[TMSG_INDEX(TDMT_MND_USE_DB)] = queryBuildUseDbMsg;
D
dapan1121 已提交
317
  queryBuildMsg[TMSG_INDEX(TDMT_MND_QNODE_LIST)] = queryBuildQnodeListMsg;
D
dapan1121 已提交
318

D
catalog  
dapan1121 已提交
319
  queryProcessMsgRsp[TMSG_INDEX(TDMT_VND_TABLE_META)] = queryProcessTableMetaRsp;
D
dapan1121 已提交
320
  queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_TABLE_META)] = queryProcessTableMetaRsp;
D
catalog  
dapan1121 已提交
321
  queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_USE_DB)] = queryProcessUseDBRsp;
D
dapan1121 已提交
322
  queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_QNODE_LIST)] = queryProcessQnodeListRsp;
D
dapan 已提交
323 324
}

D
dapan1121 已提交
325
#pragma GCC diagnostic pop