querymsg.c 21.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/>.
 */

D
dapan1121 已提交
16
#include "query.h"
H
Hongze Cheng 已提交
17
#include "queryInt.h"
18
#include "systable.h"
H
Hongze Cheng 已提交
19 20
#include "tmsg.h"
#include "trpc.h"
D
dapan 已提交
21

S
Shengliang Guan 已提交
22
#pragma GCC diagnostic push
wafwerar's avatar
wafwerar 已提交
23
#ifdef COMPILER_SUPPORTS_CXX13
S
Shengliang Guan 已提交
24
#pragma GCC diagnostic ignored "-Wformat-truncation"
wafwerar's avatar
wafwerar 已提交
25
#endif
S
Shengliang Guan 已提交
26

wafwerar's avatar
wafwerar 已提交
27 28
int32_t (*queryBuildMsg[TDMT_MAX])(void *input, char **msg, int32_t msgSize, int32_t *msgLen, 
                                   void *(*mallocFp)(int64_t)) = {0};
S
Shengliang Guan 已提交
29
int32_t (*queryProcessMsgRsp[TDMT_MAX])(void *output, char *msg, int32_t msgSize) = {0};
D
dapan 已提交
30

X
Xiaoyu Wang 已提交
31 32 33
int32_t queryBuildUseDbOutput(SUseDbOutput *pOut, SUseDbRsp *usedbRsp) {
  memcpy(pOut->db, usedbRsp->db, TSDB_DB_FNAME_LEN);
  pOut->dbId = usedbRsp->uid;
D
dapan1121 已提交
34

wafwerar's avatar
wafwerar 已提交
35
  pOut->dbVgroup = taosMemoryCalloc(1, sizeof(SDBVgInfo));
X
Xiaoyu Wang 已提交
36
  if (NULL == pOut->dbVgroup) {
S
Shengliang Guan 已提交
37
    return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
38 39 40 41
  }

  pOut->dbVgroup->vgVersion = usedbRsp->vgVersion;
  pOut->dbVgroup->hashMethod = usedbRsp->hashMethod;
42 43
  pOut->dbVgroup->hashPrefix = usedbRsp->hashPrefix;
  pOut->dbVgroup->hashSuffix = usedbRsp->hashSuffix;
D
dapan1121 已提交
44
  pOut->dbVgroup->stateTs = usedbRsp->stateTs;
D
dapan1121 已提交
45

D
dapan1121 已提交
46
  qDebug("Got %d vgroup for db %s, vgVersion:%d, stateTs:%" PRId64, usedbRsp->vgNum, usedbRsp->db, usedbRsp->vgVersion, usedbRsp->stateTs);
D
dapan1121 已提交
47

D
dapan1121 已提交
48 49 50
  if (usedbRsp->vgNum <= 0) {
    return TSDB_CODE_SUCCESS;
  }
H
Hongze Cheng 已提交
51

X
Xiaoyu Wang 已提交
52 53 54
  pOut->dbVgroup->vgHash =
      taosHashInit(usedbRsp->vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
  if (NULL == pOut->dbVgroup->vgHash) {
S
Shengliang Guan 已提交
55
    return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
56 57 58 59
  }

  for (int32_t i = 0; i < usedbRsp->vgNum; ++i) {
    SVgroupInfo *pVgInfo = taosArrayGet(usedbRsp->pVgroupInfos, i);
D
dapan 已提交
60
    pOut->dbVgroup->numOfTable += pVgInfo->numOfTable;
D
dapan1121 已提交
61
    qDebug("the %dth vgroup, id %d, epNum %d, current %s port %d", i, pVgInfo->vgId, pVgInfo->epSet.numOfEps,
H
Hongze Cheng 已提交
62
           pVgInfo->epSet.eps[pVgInfo->epSet.inUse].fqdn, pVgInfo->epSet.eps[pVgInfo->epSet.inUse].port);
X
Xiaoyu Wang 已提交
63
    if (0 != taosHashPut(pOut->dbVgroup->vgHash, &pVgInfo->vgId, sizeof(int32_t), pVgInfo, sizeof(SVgroupInfo))) {
S
Shengliang Guan 已提交
64
      return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
65 66 67 68 69 70
    }
  }

  return TSDB_CODE_SUCCESS;
}

wafwerar's avatar
wafwerar 已提交
71 72
int32_t queryBuildTableMetaReqMsg(void *input, char **msg, int32_t msgSize, int32_t *msgLen, 
                                  void *(*mallcFp)(int64_t)) {
D
dapan1121 已提交
73
  SBuildTableInput *pInput = input;
D
dapan1121 已提交
74 75 76 77
  if (NULL == input || NULL == msg || NULL == msgLen) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

S
Shengliang Guan 已提交
78 79
  STableInfoReq infoReq = {0};
  infoReq.header.vgId = pInput->vgId;
S
Shengliang Guan 已提交
80
  if (pInput->dbFName) {
S
Shengliang Guan 已提交
81
    tstrncpy(infoReq.dbFName, pInput->dbFName, TSDB_DB_FNAME_LEN);
D
dapan1121 已提交
82
  }
S
Shengliang Guan 已提交
83
  tstrncpy(infoReq.tbName, pInput->tbName, TSDB_TABLE_NAME_LEN);
D
dapan1121 已提交
84

S
Shengliang Guan 已提交
85
  int32_t bufLen = tSerializeSTableInfoReq(NULL, 0, &infoReq);
D
dapan1121 已提交
86
  void   *pBuf = (*mallcFp)(bufLen);
S
Shengliang Guan 已提交
87 88 89 90
  tSerializeSTableInfoReq(pBuf, bufLen, &infoReq);

  *msg = pBuf;
  *msgLen = bufLen;
D
dapan1121 已提交
91 92 93 94

  return TSDB_CODE_SUCCESS;
}

wafwerar's avatar
wafwerar 已提交
95
int32_t queryBuildUseDbMsg(void *input, char **msg, int32_t msgSize, int32_t *msgLen, void *(*mallcFp)(int64_t)) {
S
Shengliang Guan 已提交
96 97
  SBuildUseDBInput *pInput = input;
  if (NULL == pInput || NULL == msg || NULL == msgLen) {
D
dapan1121 已提交
98 99 100
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

S
Shengliang Guan 已提交
101
  SUseDbReq usedbReq = {0};
S
Shengliang Guan 已提交
102
  strncpy(usedbReq.db, pInput->db, sizeof(usedbReq.db));
S
Shengliang Guan 已提交
103
  usedbReq.db[sizeof(usedbReq.db) - 1] = 0;
S
Shengliang Guan 已提交
104
  usedbReq.vgVersion = pInput->vgVersion;
X
Xiaoyu Wang 已提交
105
  usedbReq.dbId = pInput->dbId;
D
dapan 已提交
106
  usedbReq.numOfTable = pInput->numOfTable;
D
dapan1121 已提交
107
  usedbReq.stateTs = pInput->stateTs;
D
dapan1121 已提交
108

S
Shengliang Guan 已提交
109
  int32_t bufLen = tSerializeSUseDbReq(NULL, 0, &usedbReq);
D
dapan1121 已提交
110
  void   *pBuf = (*mallcFp)(bufLen);
S
Shengliang Guan 已提交
111
  tSerializeSUseDbReq(pBuf, bufLen, &usedbReq);
D
dapan1121 已提交
112

S
Shengliang Guan 已提交
113 114
  *msg = pBuf;
  *msgLen = bufLen;
D
dapan1121 已提交
115

S
Shengliang Guan 已提交
116
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
117 118
}

wafwerar's avatar
wafwerar 已提交
119
int32_t queryBuildQnodeListMsg(void *input, char **msg, int32_t msgSize, int32_t *msgLen, void *(*mallcFp)(int64_t)) {
D
dapan1121 已提交
120 121 122 123 124 125 126 127
  if (NULL == msg || NULL == msgLen) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

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

  int32_t bufLen = tSerializeSQnodeListReq(NULL, 0, &qnodeListReq);
D
dapan1121 已提交
128
  void   *pBuf = (*mallcFp)(bufLen);
D
dapan1121 已提交
129 130 131 132 133 134 135 136
  tSerializeSQnodeListReq(pBuf, bufLen, &qnodeListReq);

  *msg = pBuf;
  *msgLen = bufLen;

  return TSDB_CODE_SUCCESS;
}

wafwerar's avatar
wafwerar 已提交
137
int32_t queryBuildDnodeListMsg(void *input, char **msg, int32_t msgSize, int32_t *msgLen, void *(*mallcFp)(int64_t)) {
D
dapan1121 已提交
138 139 140 141
  if (NULL == msg || NULL == msgLen) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

D
dapan1121 已提交
142 143 144 145 146 147 148 149 150
  SDnodeListReq dnodeListReq = {0};
  dnodeListReq.rowNum = -1;

  int32_t bufLen = tSerializeSDnodeListReq(NULL, 0, &dnodeListReq);
  void   *pBuf = (*mallcFp)(bufLen);
  tSerializeSDnodeListReq(pBuf, bufLen, &dnodeListReq);

  *msg = pBuf;
  *msgLen = bufLen;
D
dapan1121 已提交
151 152 153 154

  return TSDB_CODE_SUCCESS;
}

wafwerar's avatar
wafwerar 已提交
155
int32_t queryBuildGetSerVerMsg(void *input, char **msg, int32_t msgSize, int32_t *msgLen, void *(*mallcFp)(int64_t)) {
D
dapan1121 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  if (NULL == msg || NULL == msgLen) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

  SServerVerReq req = {0};

  int32_t bufLen = tSerializeSServerVerReq(NULL, 0, &req);
  void   *pBuf = (*mallcFp)(bufLen);
  tSerializeSServerVerReq(pBuf, bufLen, &req);

  *msg = pBuf;
  *msgLen = bufLen;

  return TSDB_CODE_SUCCESS;
}

wafwerar's avatar
wafwerar 已提交
172
int32_t queryBuildGetDBCfgMsg(void *input, char **msg, int32_t msgSize, int32_t *msgLen, void *(*mallcFp)(int64_t)) {
D
dapan1121 已提交
173 174 175 176 177
  if (NULL == msg || NULL == msgLen) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

  SDbCfgReq dbCfgReq = {0};
D
dapan1121 已提交
178
  strncpy(dbCfgReq.db, input, sizeof(dbCfgReq.db) - 1);
D
dapan1121 已提交
179 180

  int32_t bufLen = tSerializeSDbCfgReq(NULL, 0, &dbCfgReq);
D
dapan1121 已提交
181
  void   *pBuf = (*mallcFp)(bufLen);
D
dapan1121 已提交
182 183 184 185 186 187 188
  tSerializeSDbCfgReq(pBuf, bufLen, &dbCfgReq);

  *msg = pBuf;
  *msgLen = bufLen;

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
189

wafwerar's avatar
wafwerar 已提交
190
int32_t queryBuildGetIndexMsg(void *input, char **msg, int32_t msgSize, int32_t *msgLen, void *(*mallcFp)(int64_t)) {
D
dapan1121 已提交
191 192 193 194 195
  if (NULL == msg || NULL == msgLen) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

  SUserIndexReq indexReq = {0};
D
dapan1121 已提交
196
  strncpy(indexReq.indexFName, input, sizeof(indexReq.indexFName) - 1);
D
dapan1121 已提交
197 198

  int32_t bufLen = tSerializeSUserIndexReq(NULL, 0, &indexReq);
D
dapan1121 已提交
199
  void   *pBuf = (*mallcFp)(bufLen);
D
dapan1121 已提交
200 201 202 203 204 205 206 207
  tSerializeSUserIndexReq(pBuf, bufLen, &indexReq);

  *msg = pBuf;
  *msgLen = bufLen;

  return TSDB_CODE_SUCCESS;
}

wafwerar's avatar
wafwerar 已提交
208 209
int32_t queryBuildRetrieveFuncMsg(void *input, char **msg, int32_t msgSize, int32_t *msgLen, 
                                  void *(*mallcFp)(int64_t)) {
D
dapan1121 已提交
210 211 212 213 214 215
  if (NULL == msg || NULL == msgLen) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

  SRetrieveFuncReq funcReq = {0};
  funcReq.numOfFuncs = 1;
D
dapan1121 已提交
216
  funcReq.ignoreCodeComment = true;
D
dapan1121 已提交
217 218 219 220
  funcReq.pFuncNames = taosArrayInit(1, strlen(input) + 1);
  taosArrayPush(funcReq.pFuncNames, input);

  int32_t bufLen = tSerializeSRetrieveFuncReq(NULL, 0, &funcReq);
D
dapan1121 已提交
221
  void   *pBuf = (*mallcFp)(bufLen);
D
dapan1121 已提交
222 223 224 225 226 227 228 229 230
  tSerializeSRetrieveFuncReq(pBuf, bufLen, &funcReq);

  taosArrayDestroy(funcReq.pFuncNames);

  *msg = pBuf;
  *msgLen = bufLen;

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
231

wafwerar's avatar
wafwerar 已提交
232
int32_t queryBuildGetUserAuthMsg(void *input, char **msg, int32_t msgSize, int32_t *msgLen, void *(*mallcFp)(int64_t)) {
D
dapan 已提交
233 234 235 236 237
  if (NULL == msg || NULL == msgLen) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

  SGetUserAuthReq req = {0};
D
dapan1121 已提交
238
  strncpy(req.user, input, sizeof(req.user) - 1);
D
dapan 已提交
239 240

  int32_t bufLen = tSerializeSGetUserAuthReq(NULL, 0, &req);
D
dapan1121 已提交
241
  void   *pBuf = (*mallcFp)(bufLen);
D
dapan 已提交
242 243 244 245 246 247 248 249
  tSerializeSGetUserAuthReq(pBuf, bufLen, &req);

  *msg = pBuf;
  *msgLen = bufLen;

  return TSDB_CODE_SUCCESS;
}

wafwerar's avatar
wafwerar 已提交
250
int32_t queryBuildGetTbIndexMsg(void *input, char **msg, int32_t msgSize, int32_t *msgLen, void *(*mallcFp)(int64_t)) {
D
dapan1121 已提交
251 252 253 254 255
  if (NULL == msg || NULL == msgLen) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

  STableIndexReq indexReq = {0};
D
dapan1121 已提交
256
  strncpy(indexReq.tbFName, input, sizeof(indexReq.tbFName) - 1);
D
dapan1121 已提交
257 258 259 260 261 262 263 264 265 266 267

  int32_t bufLen = tSerializeSTableIndexReq(NULL, 0, &indexReq);
  void   *pBuf = (*mallcFp)(bufLen);
  tSerializeSTableIndexReq(pBuf, bufLen, &indexReq);

  *msg = pBuf;
  *msgLen = bufLen;

  return TSDB_CODE_SUCCESS;
}

wafwerar's avatar
wafwerar 已提交
268
int32_t queryBuildGetTbCfgMsg(void *input, char **msg, int32_t msgSize, int32_t *msgLen, void *(*mallcFp)(int64_t)) {
D
dapan1121 已提交
269 270 271 272 273
  if (NULL == msg || NULL == msgLen) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

  SBuildTableInput *pInput = input;
H
Hongze Cheng 已提交
274
  STableCfgReq      cfgReq = {0};
D
dapan1121 已提交
275
  cfgReq.header.vgId = pInput->vgId;
D
dapan1121 已提交
276 277
  strncpy(cfgReq.dbFName, pInput->dbFName, sizeof(cfgReq.dbFName) - 1);
  strncpy(cfgReq.tbName, pInput->tbName, sizeof(cfgReq.tbName) - 1);
D
dapan1121 已提交
278 279 280 281 282 283 284 285 286 287 288

  int32_t bufLen = tSerializeSTableCfgReq(NULL, 0, &cfgReq);
  void   *pBuf = (*mallcFp)(bufLen);
  tSerializeSTableCfgReq(pBuf, bufLen, &cfgReq);

  *msg = pBuf;
  *msgLen = bufLen;

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
289
int32_t queryProcessUseDBRsp(void *output, char *msg, int32_t msgSize) {
S
Shengliang Guan 已提交
290 291 292 293
  SUseDbOutput *pOut = output;
  SUseDbRsp     usedbRsp = {0};
  int32_t       code = -1;

D
dapan1121 已提交
294
  if (NULL == output || NULL == msg || msgSize <= 0) {
S
Shengliang Guan 已提交
295 296
    code = TSDB_CODE_TSC_INVALID_INPUT;
    goto PROCESS_USEDB_OVER;
D
dapan1121 已提交
297 298
  }

S
Shengliang Guan 已提交
299 300
  if (tDeserializeSUseDbRsp(msg, msgSize, &usedbRsp) != 0) {
    qError("invalid use db rsp msg, msgSize:%d", msgSize);
S
Shengliang Guan 已提交
301 302
    code = TSDB_CODE_INVALID_MSG;
    goto PROCESS_USEDB_OVER;
D
dapan1121 已提交
303 304
  }

S
Shengliang Guan 已提交
305 306
  if (usedbRsp.vgNum < 0) {
    qError("invalid db[%s] vgroup number[%d]", usedbRsp.db, usedbRsp.vgNum);
S
Shengliang Guan 已提交
307 308
    code = TSDB_CODE_TSC_INVALID_VALUE;
    goto PROCESS_USEDB_OVER;
D
dapan1121 已提交
309 310
  }

S
Shengliang Guan 已提交
311 312 313 314 315 316 317 318 319
  qTrace("db:%s, usedbRsp received, numOfVgroups:%d", usedbRsp.db, usedbRsp.vgNum);
  for (int32_t i = 0; i < usedbRsp.vgNum; ++i) {
    SVgroupInfo *pInfo = taosArrayGet(usedbRsp.pVgroupInfos, i);
    qTrace("vgId:%d, numOfEps:%d inUse:%d ", pInfo->vgId, pInfo->epSet.numOfEps, pInfo->epSet.inUse);
    for (int32_t j = 0; j < pInfo->epSet.numOfEps; ++j) {
      qTrace("vgId:%d, index:%d epset:%s:%u", pInfo->vgId, j, pInfo->epSet.eps[j].fqdn, pInfo->epSet.eps[j].port);
    }
  }

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

S
Shengliang Guan 已提交
322
PROCESS_USEDB_OVER:
X
Xiaoyu Wang 已提交
323

S
Shengliang Guan 已提交
324 325 326
  if (code != 0) {
    if (pOut) {
      if (pOut->dbVgroup) taosHashCleanup(pOut->dbVgroup->vgHash);
wafwerar's avatar
wafwerar 已提交
327
      taosMemoryFreeClear(pOut->dbVgroup);
S
Shengliang Guan 已提交
328 329
    }
    qError("failed to process usedb rsp since %s", terrstr());
D
dapan1121 已提交
330
  }
S
Shengliang Guan 已提交
331

S
Shengliang Guan 已提交
332
  tFreeSUsedbRsp(&usedbRsp);
D
dapan1121 已提交
333 334 335
  return code;
}

S
Shengliang Guan 已提交
336
static int32_t queryConvertTableMetaMsg(STableMetaRsp *pMetaMsg) {
D
dapan1121 已提交
337 338 339 340 341 342 343 344 345 346
  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 已提交
347
  if (pMetaMsg->tableType != TSDB_SUPER_TABLE && pMetaMsg->tableType != TSDB_CHILD_TABLE &&
X
Xiaoyu Wang 已提交
348
      pMetaMsg->tableType != TSDB_NORMAL_TABLE && pMetaMsg->tableType != TSDB_SYSTEM_TABLE) {
D
dapan1121 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362
    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 已提交
363
  if (pMetaMsg->pSchemas[0].colId != PRIMARYKEY_TIMESTAMP_COL_ID) {
364
    qError("invalid colId[%" PRIi16 "] for the first column in table meta rsp msg", pMetaMsg->pSchemas[0].colId);
D
dapan1121 已提交
365 366 367 368 369 370
    return TSDB_CODE_TSC_INVALID_VALUE;
  }

  return TSDB_CODE_SUCCESS;
}

371 372 373 374 375 376
int32_t queryCreateCTableMetaFromMsg(STableMetaRsp *msg, SCTableMeta *pMeta) {
  pMeta->vgId = msg->vgId;
  pMeta->tableType = msg->tableType;
  pMeta->uid = msg->tuid;
  pMeta->suid = msg->suid;

H
Hongze Cheng 已提交
377 378
  qDebug("ctable %s uid %" PRIx64 " meta returned, type %d vgId:%d db %s suid %" PRIx64, msg->tbName, pMeta->uid,
         pMeta->tableType, pMeta->vgId, msg->dbFName, pMeta->suid);
379 380 381 382

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
383
int32_t queryCreateTableMetaFromMsg(STableMetaRsp *msg, bool isStb, STableMeta **pMeta) {
D
dapan1121 已提交
384 385
  int32_t total = msg->numOfColumns + msg->numOfTags;
  int32_t metaSize = sizeof(STableMeta) + sizeof(SSchema) * total;
S
Shengliang Guan 已提交
386

wafwerar's avatar
wafwerar 已提交
387
  STableMeta *pTableMeta = taosMemoryCalloc(1, metaSize);
D
dapan1121 已提交
388 389
  if (NULL == pTableMeta) {
    qError("calloc size[%d] failed", metaSize);
S
Shengliang Guan 已提交
390
    return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
391
  }
D
dapan1121 已提交
392

D
dapan1121 已提交
393 394 395
  pTableMeta->vgId = isStb ? 0 : msg->vgId;
  pTableMeta->tableType = isStb ? TSDB_SUPER_TABLE : msg->tableType;
  pTableMeta->uid = isStb ? msg->suid : msg->tuid;
D
dapan1121 已提交
396 397 398 399 400 401 402 403
  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 已提交
404
  memcpy(pTableMeta->schema, msg->pSchemas, sizeof(SSchema) * total);
D
dapan1121 已提交
405

S
Shengliang Guan 已提交
406
  for (int32_t i = 0; i < msg->numOfColumns; ++i) {
D
dapan1121 已提交
407 408 409
    pTableMeta->tableInfo.rowSize += pTableMeta->schema[i].bytes;
  }

S
Shengliang Guan 已提交
410 411 412 413 414
  qDebug("table %s uid %" PRIx64 " meta returned, type %d vgId:%d db %s stb %s suid %" PRIx64 " sver %d tver %d" PRIx64
         " tagNum %d colNum %d precision %d rowSize %d",
         msg->tbName, pTableMeta->uid, pTableMeta->tableType, pTableMeta->vgId, msg->dbFName, msg->stbName,
         pTableMeta->suid, pTableMeta->sversion, pTableMeta->tversion, pTableMeta->tableInfo.numOfTags,
         pTableMeta->tableInfo.numOfColumns, pTableMeta->tableInfo.precision, pTableMeta->tableInfo.rowSize);
D
dapan1121 已提交
415

D
dapan1121 已提交
416 417 418 419
  *pMeta = pTableMeta;
  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
420
int32_t queryProcessTableMetaRsp(void *output, char *msg, int32_t msgSize) {
D
dapan1121 已提交
421
  int32_t       code = 0;
S
Shengliang Guan 已提交
422 423 424 425 426 427
  STableMetaRsp metaRsp = {0};

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

S
Shengliang Guan 已提交
429 430 431 432 433 434
  if (tDeserializeSTableMetaRsp(msg, msgSize, &metaRsp) != 0) {
    code = TSDB_CODE_INVALID_MSG;
    goto PROCESS_META_OVER;
  }

  code = queryConvertTableMetaMsg(&metaRsp);
D
dapan1121 已提交
435
  if (code != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
436
    goto PROCESS_META_OVER;
D
dapan1121 已提交
437 438
  }

H
Hongze Cheng 已提交
439 440
  if (0 != strcmp(metaRsp.dbFName, TSDB_INFORMATION_SCHEMA_DB) &&
      !tIsValidSchema(metaRsp.pSchemas, metaRsp.numOfColumns, metaRsp.numOfTags)) {
S
Shengliang Guan 已提交
441 442
    code = TSDB_CODE_TSC_INVALID_VALUE;
    goto PROCESS_META_OVER;
D
dapan1121 已提交
443 444
  }

S
Shengliang Guan 已提交
445 446 447
  STableMetaOutput *pOut = output;
  strcpy(pOut->dbFName, metaRsp.dbFName);
  pOut->dbId = metaRsp.dbId;
D
dapan1121 已提交
448

S
Shengliang Guan 已提交
449
  if (metaRsp.tableType == TSDB_CHILD_TABLE) {
D
dapan1121 已提交
450
    SET_META_TYPE_BOTH_TABLE(pOut->metaType);
D
dapan1121 已提交
451

S
Shengliang Guan 已提交
452 453
    strcpy(pOut->ctbName, metaRsp.tbName);
    strcpy(pOut->tbName, metaRsp.stbName);
D
dapan1121 已提交
454

S
Shengliang Guan 已提交
455 456 457 458 459 460
    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 已提交
461
  } else {
D
dapan1121 已提交
462
    SET_META_TYPE_TABLE(pOut->metaType);
S
Shengliang Guan 已提交
463 464 465 466 467 468
    strcpy(pOut->tbName, metaRsp.tbName);
    code = queryCreateTableMetaFromMsg(&metaRsp, (metaRsp.tableType == TSDB_SUPER_TABLE), &pOut->tbMeta);
  }

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

  tFreeSTableMetaRsp(&metaRsp);
D
dapan1121 已提交
473 474 475
  return code;
}

D
dapan1121 已提交
476 477
int32_t queryProcessQnodeListRsp(void *output, char *msg, int32_t msgSize) {
  SQnodeListRsp out = {0};
D
dapan 已提交
478
  int32_t       code = 0;
D
dapan1121 已提交
479 480 481

  if (NULL == output || NULL == msg || msgSize <= 0) {
    code = TSDB_CODE_TSC_INVALID_INPUT;
D
dapan 已提交
482
    return code;
D
dapan1121 已提交
483 484
  }

D
dapan1121 已提交
485
  out.qnodeList = (SArray *)output;
D
dapan1121 已提交
486 487 488
  if (tDeserializeSQnodeListRsp(msg, msgSize, &out) != 0) {
    qError("invalid qnode list rsp msg, msgSize:%d", msgSize);
    code = TSDB_CODE_INVALID_MSG;
D
dapan 已提交
489
    return code;
D
dapan1121 已提交
490 491 492 493 494
  }

  return code;
}

D
dapan1121 已提交
495
int32_t queryProcessDnodeListRsp(void *output, char *msg, int32_t msgSize) {
D
dapan1121 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509
  SDnodeListRsp out = {0};
  int32_t       code = 0;

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

  if (tDeserializeSDnodeListRsp(msg, msgSize, &out) != 0) {
    qError("invalid dnode list rsp msg, msgSize:%d", msgSize);
    code = TSDB_CODE_INVALID_MSG;
    return code;
  }

H
Hongze Cheng 已提交
510
  *(SArray **)output = out.dnodeList;
D
dapan1121 已提交
511 512 513 514

  return code;
}

D
dapan1121 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
int32_t queryProcessGetSerVerRsp(void *output, char *msg, int32_t msgSize) {
  SServerVerRsp out = {0};
  int32_t       code = 0;

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

  if (tDeserializeSServerVerRsp(msg, msgSize, &out) != 0) {
    qError("invalid svr ver rsp msg, msgSize:%d", msgSize);
    code = TSDB_CODE_INVALID_MSG;
    return code;
  }

530
  *(char **)output = taosStrdup(out.ver);
D
dapan1121 已提交
531 532 533 534

  return code;
}

D
dapan1121 已提交
535 536 537 538 539 540 541 542
int32_t queryProcessGetDbCfgRsp(void *output, char *msg, int32_t msgSize) {
  SDbCfgRsp out = {0};

  if (NULL == output || NULL == msg || msgSize <= 0) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

  if (tDeserializeSDbCfgRsp(msg, msgSize, &out) != 0) {
haoranc's avatar
haoranc 已提交
543
    qError("tDeserializeSDbCfgRsp failed, msgSize:%d,dbCfgRsp:%lu", msgSize, sizeof(out));
D
dapan1121 已提交
544 545 546 547 548 549 550 551
    return TSDB_CODE_INVALID_MSG;
  }

  memcpy(output, &out, sizeof(out));

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
int32_t queryProcessGetIndexRsp(void *output, char *msg, int32_t msgSize) {
  SUserIndexRsp out = {0};

  if (NULL == output || NULL == msg || msgSize <= 0) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

  if (tDeserializeSUserIndexRsp(msg, msgSize, &out) != 0) {
    qError("tDeserializeSUserIndexRsp failed, msgSize:%d", msgSize);
    return TSDB_CODE_INVALID_MSG;
  }

  memcpy(output, &out, sizeof(out));

  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
568

D
dapan1121 已提交
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
int32_t queryProcessRetrieveFuncRsp(void *output, char *msg, int32_t msgSize) {
  SRetrieveFuncRsp out = {0};

  if (NULL == output || NULL == msg || msgSize <= 0) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

  if (tDeserializeSRetrieveFuncRsp(msg, msgSize, &out) != 0) {
    qError("tDeserializeSRetrieveFuncRsp failed, msgSize:%d", msgSize);
    return TSDB_CODE_INVALID_MSG;
  }

  if (1 != out.numOfFuncs) {
    qError("invalid func num returned, numOfFuncs:%d", out.numOfFuncs);
    return TSDB_CODE_INVALID_MSG;
  }

H
Hongze Cheng 已提交
586
  SFuncInfo *funcInfo = taosArrayGet(out.pFuncInfos, 0);
D
dapan1121 已提交
587 588 589

  memcpy(output, funcInfo, sizeof(*funcInfo));
  taosArrayDestroy(out.pFuncInfos);
590 591
  taosArrayDestroy(out.pFuncVersions);
  
D
dapan1121 已提交
592 593 594
  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607
int32_t queryProcessGetUserAuthRsp(void *output, char *msg, int32_t msgSize) {
  if (NULL == output || NULL == msg || msgSize <= 0) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

  if (tDeserializeSGetUserAuthRsp(msg, msgSize, (SGetUserAuthRsp *)output) != 0) {
    qError("tDeserializeSGetUserAuthRsp failed, msgSize:%d", msgSize);
    return TSDB_CODE_INVALID_MSG;
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
608 609 610 611
int32_t queryProcessGetTbIndexRsp(void *output, char *msg, int32_t msgSize) {
  if (NULL == output || NULL == msg || msgSize <= 0) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }
D
dapan 已提交
612

H
Hongze Cheng 已提交
613
  STableIndexRsp *out = (STableIndexRsp *)output;
D
dapan1121 已提交
614
  if (tDeserializeSTableIndexRsp(msg, msgSize, out) != 0) {
D
dapan1121 已提交
615 616 617
    qError("tDeserializeSTableIndexRsp failed, msgSize:%d", msgSize);
    return TSDB_CODE_INVALID_MSG;
  }
H
Hongze Cheng 已提交
618

D
dapan1121 已提交
619 620 621
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
622 623 624 625 626 627 628 629
int32_t queryProcessGetTbCfgRsp(void *output, char *msg, int32_t msgSize) {
  if (NULL == output || NULL == msg || msgSize <= 0) {
    return TSDB_CODE_TSC_INVALID_INPUT;
  }

  STableCfgRsp *out = taosMemoryCalloc(1, sizeof(STableCfgRsp));
  if (tDeserializeSTableCfgRsp(msg, msgSize, out) != 0) {
    qError("tDeserializeSTableCfgRsp failed, msgSize:%d", msgSize);
D
dapan1121 已提交
630 631
    tFreeSTableCfgRsp(out);
    taosMemoryFree(out);
D
dapan1121 已提交
632 633 634
    return TSDB_CODE_INVALID_MSG;
  }

H
Hongze Cheng 已提交
635 636
  *(STableCfgRsp **)output = out;

D
dapan1121 已提交
637 638
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
639

D
dapan1121 已提交
640
void initQueryModuleMsgHandle() {
H
Hongze Cheng 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
  queryBuildMsg[TMSG_INDEX(TDMT_VND_TABLE_META)] = queryBuildTableMetaReqMsg;
  queryBuildMsg[TMSG_INDEX(TDMT_MND_TABLE_META)] = queryBuildTableMetaReqMsg;
  queryBuildMsg[TMSG_INDEX(TDMT_MND_USE_DB)] = queryBuildUseDbMsg;
  queryBuildMsg[TMSG_INDEX(TDMT_MND_QNODE_LIST)] = queryBuildQnodeListMsg;
  queryBuildMsg[TMSG_INDEX(TDMT_MND_DNODE_LIST)] = queryBuildDnodeListMsg;
  queryBuildMsg[TMSG_INDEX(TDMT_MND_GET_DB_CFG)] = queryBuildGetDBCfgMsg;
  queryBuildMsg[TMSG_INDEX(TDMT_MND_GET_INDEX)] = queryBuildGetIndexMsg;
  queryBuildMsg[TMSG_INDEX(TDMT_MND_RETRIEVE_FUNC)] = queryBuildRetrieveFuncMsg;
  queryBuildMsg[TMSG_INDEX(TDMT_MND_GET_USER_AUTH)] = queryBuildGetUserAuthMsg;
  queryBuildMsg[TMSG_INDEX(TDMT_MND_GET_TABLE_INDEX)] = queryBuildGetTbIndexMsg;
  queryBuildMsg[TMSG_INDEX(TDMT_VND_TABLE_CFG)] = queryBuildGetTbCfgMsg;
  queryBuildMsg[TMSG_INDEX(TDMT_MND_TABLE_CFG)] = queryBuildGetTbCfgMsg;
  queryBuildMsg[TMSG_INDEX(TDMT_MND_SERVER_VERSION)] = queryBuildGetSerVerMsg;

  queryProcessMsgRsp[TMSG_INDEX(TDMT_VND_TABLE_META)] = queryProcessTableMetaRsp;
  queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_TABLE_META)] = queryProcessTableMetaRsp;
  queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_USE_DB)] = queryProcessUseDBRsp;
  queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_QNODE_LIST)] = queryProcessQnodeListRsp;
  queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_DNODE_LIST)] = queryProcessDnodeListRsp;
  queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_GET_DB_CFG)] = queryProcessGetDbCfgRsp;
  queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_GET_INDEX)] = queryProcessGetIndexRsp;
  queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_RETRIEVE_FUNC)] = queryProcessRetrieveFuncRsp;
  queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_GET_USER_AUTH)] = queryProcessGetUserAuthRsp;
D
dapan1121 已提交
664
  queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_GET_TABLE_INDEX)] = queryProcessGetTbIndexRsp;
H
Hongze Cheng 已提交
665 666 667
  queryProcessMsgRsp[TMSG_INDEX(TDMT_VND_TABLE_CFG)] = queryProcessGetTbCfgRsp;
  queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_TABLE_CFG)] = queryProcessGetTbCfgRsp;
  queryProcessMsgRsp[TMSG_INDEX(TDMT_MND_SERVER_VERSION)] = queryProcessGetSerVerRsp;
D
dapan 已提交
668 669
}

D
dapan1121 已提交
670
#pragma GCC diagnostic pop