querymsg.c 8.5 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

S
Shengliang Guan 已提交
27 28
int32_t queryBuildTableMetaReqMsg(void *input, char **msg, int32_t msgSize, int32_t *msgLen) {
  SBuildTableMetaInput *pInput = input;
D
dapan1121 已提交
29 30 31 32
  if (NULL == input || NULL == msg || NULL == msgLen) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

S
Shengliang Guan 已提交
33
  int32_t estimateSize = sizeof(STableInfoReq);
D
dapan1121 已提交
34 35
  if (NULL == *msg || msgSize < estimateSize) {
    tfree(*msg);
D
catalog  
dapan1121 已提交
36
    *msg = rpcMallocCont(estimateSize);
D
dapan1121 已提交
37 38 39 40 41
    if (NULL == *msg) {
      return TSDB_CODE_TSC_OUT_OF_MEMORY;
    }
  }

S
Shengliang Guan 已提交
42
  STableInfoReq *bMsg = (STableInfoReq *)*msg;
D
dapan1121 已提交
43

S
Shengliang Guan 已提交
44
  bMsg->header.vgId = htonl(pInput->vgId);
D
dapan1121 已提交
45

S
Shengliang Guan 已提交
46 47
  if (pInput->dbFName) {
    tstrncpy(bMsg->dbFName, pInput->dbFName, tListLen(bMsg->dbFName));
D
dapan1121 已提交
48 49
  }

S
Shengliang Guan 已提交
50
  tstrncpy(bMsg->tbName, pInput->tbName, tListLen(bMsg->tbName));
D
dapan1121 已提交
51 52 53 54 55

  *msgLen = (int32_t)sizeof(*bMsg);
  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
56
int32_t queryBuildUseDbMsg(void *input, char **msg, int32_t msgSize, int32_t *msgLen) {
D
dapan1121 已提交
57 58 59 60
  if (NULL == input || NULL == msg || NULL == msgLen) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

S
Shengliang Guan 已提交
61
  SBuildUseDBInput *bInput = input;
D
dapan1121 已提交
62

S
Shengliang Guan 已提交
63 64 65 66
  SUseDbReq usedbReq = {0};
  strncpy(usedbReq.db, bInput->db, sizeof(usedbReq.db));
  usedbReq.db[sizeof(usedbReq.db) - 1] = 0;
  usedbReq.vgVersion = bInput->vgVersion;
D
dapan1121 已提交
67

S
Shengliang Guan 已提交
68 69 70
  int32_t bufLen = tSerializeSUseDbReq(NULL, 0, &usedbReq);
  void   *pBuf = rpcMallocCont(bufLen);
  tSerializeSUseDbReq(pBuf, bufLen, &usedbReq);
D
dapan1121 已提交
71

S
Shengliang Guan 已提交
72 73
  *msg = pBuf;
  *msgLen = bufLen;
D
dapan1121 已提交
74

S
Shengliang Guan 已提交
75
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
76 77
}

S
Shengliang Guan 已提交
78
int32_t queryProcessUseDBRsp(void *output, char *msg, int32_t msgSize) {
S
Shengliang Guan 已提交
79 80 81 82
  SUseDbOutput *pOut = output;
  SUseDbRsp     usedbRsp = {0};
  int32_t       code = -1;

D
dapan1121 已提交
83
  if (NULL == output || NULL == msg || msgSize <= 0) {
S
Shengliang Guan 已提交
84 85
    code = TSDB_CODE_TSC_INVALID_INPUT;
    goto PROCESS_USEDB_OVER;
D
dapan1121 已提交
86 87
  }

S
Shengliang Guan 已提交
88 89
  if (tDeserializeSUseDbRsp(msg, msgSize, &usedbRsp) != 0) {
    qError("invalid use db rsp msg, msgSize:%d", msgSize);
S
Shengliang Guan 已提交
90 91
    code = TSDB_CODE_INVALID_MSG;
    goto PROCESS_USEDB_OVER;
D
dapan1121 已提交
92 93
  }

S
Shengliang Guan 已提交
94 95
  if (usedbRsp.vgNum < 0) {
    qError("invalid db[%s] vgroup number[%d]", usedbRsp.db, usedbRsp.vgNum);
S
Shengliang Guan 已提交
96 97
    code = TSDB_CODE_TSC_INVALID_VALUE;
    goto PROCESS_USEDB_OVER;
D
dapan1121 已提交
98 99
  }

S
Shengliang Guan 已提交
100 101 102 103 104
  memcpy(pOut->db, usedbRsp.db, TSDB_DB_FNAME_LEN);
  pOut->dbId = usedbRsp.uid;
  pOut->dbVgroup->vgVersion = usedbRsp.vgVersion;
  pOut->dbVgroup->hashMethod = usedbRsp.hashMethod;

D
dapan1121 已提交
105
  pOut->dbVgroup = calloc(1, sizeof(SDBVgInfo));
D
dapan1121 已提交
106
  if (NULL == pOut->dbVgroup) {
S
Shengliang Guan 已提交
107 108
    code = TSDB_CODE_TSC_OUT_OF_MEMORY;
    goto PROCESS_USEDB_OVER;
D
dapan1121 已提交
109 110
  }

S
Shengliang Guan 已提交
111 112
  pOut->dbVgroup->vgHash =
      taosHashInit(usedbRsp.vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
D
dapan1121 已提交
113 114
  if (NULL == pOut->dbVgroup->vgHash) {
    tfree(pOut->dbVgroup);
S
Shengliang Guan 已提交
115 116
    code = TSDB_CODE_TSC_OUT_OF_MEMORY;
    goto PROCESS_USEDB_OVER;
D
dapan1121 已提交
117 118
  }

S
Shengliang Guan 已提交
119 120 121
  for (int32_t i = 0; i < usedbRsp.vgNum; ++i) {
    SVgroupInfo *pVgInfo = taosArrayGet(usedbRsp.pVgroupInfos, i);
    if (0 != taosHashPut(pOut->dbVgroup->vgHash, &pVgInfo->vgId, sizeof(int32_t), pVgInfo, sizeof(SVgroupInfo))) {
S
Shengliang Guan 已提交
122 123
      code = TSDB_CODE_TSC_OUT_OF_MEMORY;
      goto PROCESS_USEDB_OVER;
D
dapan1121 已提交
124 125 126
    }
  }

S
Shengliang Guan 已提交
127
  code = 0;
128

S
Shengliang Guan 已提交
129 130 131 132 133 134 135
PROCESS_USEDB_OVER:
  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 已提交
136
  }
S
Shengliang Guan 已提交
137

S
Shengliang Guan 已提交
138
  tFreeSUsedbRsp(&usedbRsp);
D
dapan1121 已提交
139 140 141
  return code;
}

S
Shengliang Guan 已提交
142
static int32_t queryConvertTableMetaMsg(STableMetaRsp *pMetaMsg) {
D
dapan1121 已提交
143 144 145 146 147 148 149 150 151 152
  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 已提交
153 154
  if (pMetaMsg->tableType != TSDB_SUPER_TABLE && pMetaMsg->tableType != TSDB_CHILD_TABLE &&
      pMetaMsg->tableType != TSDB_NORMAL_TABLE) {
D
dapan1121 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168
    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 已提交
169 170
  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 已提交
171 172 173 174 175 176
    return TSDB_CODE_TSC_INVALID_VALUE;
  }

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
177
int32_t queryCreateTableMetaFromMsg(STableMetaRsp *msg, bool isSuperTable, STableMeta **pMeta) {
D
dapan1121 已提交
178 179
  int32_t total = msg->numOfColumns + msg->numOfTags;
  int32_t metaSize = sizeof(STableMeta) + sizeof(SSchema) * total;
S
Shengliang Guan 已提交
180 181

  STableMeta *pTableMeta = calloc(1, metaSize);
D
dapan1121 已提交
182 183 184 185
  if (NULL == pTableMeta) {
    qError("calloc size[%d] failed", metaSize);
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }
D
dapan1121 已提交
186 187

  pTableMeta->vgId = isSuperTable ? 0 : msg->vgId;
D
dapan1121 已提交
188
  pTableMeta->tableType = isSuperTable ? TSDB_SUPER_TABLE : msg->tableType;
S
Shengliang Guan 已提交
189
  pTableMeta->uid = isSuperTable ? msg->suid : msg->tuid;
D
dapan1121 已提交
190 191 192 193 194 195 196 197
  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 已提交
198
  memcpy(pTableMeta->schema, msg->pSchemas, sizeof(SSchema) * total);
D
dapan1121 已提交
199

S
Shengliang Guan 已提交
200
  for (int32_t i = 0; i < msg->numOfColumns; ++i) {
D
dapan1121 已提交
201 202 203 204 205 206 207
    pTableMeta->tableInfo.rowSize += pTableMeta->schema[i].bytes;
  }

  *pMeta = pTableMeta;
  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
208 209 210 211 212 213 214 215
int32_t queryProcessTableMetaRsp(void *output, char *msg, int32_t msgSize) {
  int32_t       code = -1;
  STableMetaRsp metaRsp = {0};

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

S
Shengliang Guan 已提交
217 218 219 220 221 222
  if (tDeserializeSTableMetaRsp(msg, msgSize, &metaRsp) != 0) {
    code = TSDB_CODE_INVALID_MSG;
    goto PROCESS_META_OVER;
  }

  code = queryConvertTableMetaMsg(&metaRsp);
D
dapan1121 已提交
223
  if (code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
224
    goto PROCESS_META_OVER;
D
dapan1121 已提交
225 226
  }

S
Shengliang Guan 已提交
227 228 229
  if (!tIsValidSchema(metaRsp.pSchemas, metaRsp.numOfColumns, metaRsp.numOfTags)) {
    code = TSDB_CODE_TSC_INVALID_VALUE;
    goto PROCESS_META_OVER;
D
dapan1121 已提交
230 231
  }

S
Shengliang Guan 已提交
232 233 234
  STableMetaOutput *pOut = output;
  strcpy(pOut->dbFName, metaRsp.dbFName);
  pOut->dbId = metaRsp.dbId;
D
dapan1121 已提交
235

S
Shengliang Guan 已提交
236
  if (metaRsp.tableType == TSDB_CHILD_TABLE) {
D
dapan1121 已提交
237
    SET_META_TYPE_BOTH_TABLE(pOut->metaType);
D
dapan1121 已提交
238

S
Shengliang Guan 已提交
239 240
    strcpy(pOut->ctbName, metaRsp.tbName);
    strcpy(pOut->tbName, metaRsp.stbName);
D
dapan1121 已提交
241

S
Shengliang Guan 已提交
242 243 244 245 246 247
    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 已提交
248
  } else {
D
dapan1121 已提交
249
    SET_META_TYPE_TABLE(pOut->metaType);
S
Shengliang Guan 已提交
250 251 252 253 254 255 256
    strcpy(pOut->tbName, metaRsp.tbName);
    code = queryCreateTableMetaFromMsg(&metaRsp, (metaRsp.tableType == TSDB_SUPER_TABLE), &pOut->tbMeta);
  }

PROCESS_META_OVER:
  if (code != 0) {
    qError("failed to process table meta rsp since %s", terrstr());
D
dapan1121 已提交
257
  }
S
Shengliang Guan 已提交
258 259

  tFreeSTableMetaRsp(&metaRsp);
D
dapan1121 已提交
260 261 262
  return code;
}

263
void initQueryModuleMsgHandle() {
D
catalog  
dapan1121 已提交
264 265 266
  queryBuildMsg[TMSG_INDEX(TDMT_VND_TABLE_META)] = queryBuildTableMetaReqMsg;
  queryBuildMsg[TMSG_INDEX(TDMT_MND_STB_META)] = queryBuildTableMetaReqMsg;
  queryBuildMsg[TMSG_INDEX(TDMT_MND_USE_DB)] = queryBuildUseDbMsg;
D
dapan1121 已提交
267

D
catalog  
dapan1121 已提交
268 269 270
  queryProcessMsgRsp[TMSG_INDEX(TDMT_VND_TABLE_META)] = queryProcessTableMetaRsp;
  queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_STB_META)] = queryProcessTableMetaRsp;
  queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_USE_DB)] = queryProcessUseDBRsp;
D
dapan 已提交
271 272
}

D
dapan1121 已提交
273
#pragma GCC diagnostic pop