clientMsgHandler.c 17.3 KB
Newer Older
H
Haojun Liao 已提交
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/>.
 */

L
Liu Jicong 已提交
16
#include "catalog.h"
H
Haojun Liao 已提交
17
#include "clientInt.h"
18
#include "clientLog.h"
L
Liu Jicong 已提交
19
#include "os.h"
D
dapan1121 已提交
20
#include "query.h"
21 22
#include "systable.h"
#include "tdatablock.h"
L
Liu Jicong 已提交
23
#include "tdef.h"
24
#include "tglobal.h"
L
Liu Jicong 已提交
25
#include "tname.h"
26
#include "tversion.h"
H
Haojun Liao 已提交
27

H
Haojun Liao 已提交
28 29 30 31 32
static void setErrno(SRequestObj* pRequest, int32_t code) {
  pRequest->code = code;
  terrno = code;
}

D
dapan1121 已提交
33
int32_t genericRspCallback(void* param, SDataBuf* pMsg, int32_t code) {
34
  SRequestObj* pRequest = param;
H
Haojun Liao 已提交
35 36
  setErrno(pRequest, code);

37 38 39 40
  if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type)) {
    removeMeta(pRequest->pTscObj, pRequest->targetTableList);
  }

dengyihao's avatar
dengyihao 已提交
41
  taosMemoryFree(pMsg->pEpSet);
wafwerar's avatar
wafwerar 已提交
42
  taosMemoryFree(pMsg->pData);
43 44 45 46 47
  if (pRequest->body.queryFp != NULL) {
    pRequest->body.queryFp(pRequest->body.param, pRequest, code);
  } else {
    tsem_post(&pRequest->body.rspSem);
  }
H
Haojun Liao 已提交
48
  return code;
49 50
}

D
dapan1121 已提交
51
int32_t processConnectRsp(void* param, SDataBuf* pMsg, int32_t code) {
52
  SRequestObj* pRequest = acquireRequest(*(int64_t*)param);
D
dapan1121 已提交
53 54 55
  if (NULL == pRequest) {
    goto End;
  }
56

H
Haojun Liao 已提交
57
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
58
    setErrno(pRequest, code);
59
    tsem_post(&pRequest->body.rspSem);
dengyihao's avatar
dengyihao 已提交
60
    goto End;
H
Haojun Liao 已提交
61
  }
62

S
Shengliang Guan 已提交
63
  STscObj* pTscObj = pRequest->pTscObj;
H
Haojun Liao 已提交
64

D
dapan1121 已提交
65 66 67 68 69
  if (NULL == pTscObj->pAppInfo || NULL == pTscObj->pAppInfo->pAppHbMgr) {
    setErrno(pRequest, TSDB_CODE_TSC_DISCONNECTED);
    tsem_post(&pRequest->body.rspSem);
    goto End;
  }
70

S
Shengliang Guan 已提交
71
  SConnectRsp connectRsp = {0};
dengyihao's avatar
dengyihao 已提交
72 73 74 75
  if (tDeserializeSConnectRsp(pMsg->pData, pMsg->len, &connectRsp) != 0) {
    code = TSDB_CODE_TSC_INVALID_VERSION;
    setErrno(pRequest, code);
    tsem_post(&pRequest->body.rspSem);
dengyihao's avatar
dengyihao 已提交
76
    goto End;
dengyihao's avatar
dengyihao 已提交
77
  }
dengyihao's avatar
dengyihao 已提交
78

79 80 81 82 83 84
  if ((code = taosCheckVersionCompatibleFromStr(version, connectRsp.sVer, 2)) != 0) {
    setErrno(pRequest, code);
    tsem_post(&pRequest->body.rspSem);
    goto End;
  }

dengyihao's avatar
dengyihao 已提交
85 86 87 88
  int32_t now = taosGetTimestampSec();
  int32_t delta = abs(now - connectRsp.svrTimestamp);
  if (delta > timestampDeltaLimit) {
    code = TSDB_CODE_TIME_UNSYNCED;
dengyihao's avatar
dengyihao 已提交
89
    tscError("time diff:%ds is too big", delta);
dengyihao's avatar
dengyihao 已提交
90 91
    setErrno(pRequest, code);
    tsem_post(&pRequest->body.rspSem);
dengyihao's avatar
dengyihao 已提交
92
    goto End;
dengyihao's avatar
dengyihao 已提交
93 94
  }

L
Liu Jicong 已提交
95 96
  /*assert(connectRsp.epSet.numOfEps > 0);*/
  if (connectRsp.epSet.numOfEps == 0) {
S
Shengliang Guan 已提交
97
    setErrno(pRequest, TSDB_CODE_APP_ERROR);
L
Liu Jicong 已提交
98
    tsem_post(&pRequest->body.rspSem);
dengyihao's avatar
dengyihao 已提交
99
    goto End;
L
Liu Jicong 已提交
100
  }
H
Haojun Liao 已提交
101

D
dapan1121 已提交
102
  if (connectRsp.dnodeNum == 1) {
dengyihao's avatar
dengyihao 已提交
103 104 105 106
    SEpSet srcEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
    SEpSet dstEpSet = connectRsp.epSet;
    rpcSetDefaultAddr(pTscObj->pAppInfo->pTransporter, srcEpSet.eps[srcEpSet.inUse].fqdn,
                      dstEpSet.eps[dstEpSet.inUse].fqdn);
D
dapan1121 已提交
107
  } else if (connectRsp.dnodeNum > 1 && !isEpsetEqual(&pTscObj->pAppInfo->mgmtEp.epSet, &connectRsp.epSet)) {
D
dapan1121 已提交
108
    SEpSet* pOrig = &pTscObj->pAppInfo->mgmtEp.epSet;
109 110 111 112 113
    SEp*    pOrigEp = &pOrig->eps[pOrig->inUse];
    SEp*    pNewEp = &connectRsp.epSet.eps[connectRsp.epSet.inUse];
    tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in connRsp", pOrig->inUse, pOrig->numOfEps,
             pOrigEp->fqdn, pOrigEp->port, connectRsp.epSet.inUse, connectRsp.epSet.numOfEps, pNewEp->fqdn,
             pNewEp->port);
S
Shengliang Guan 已提交
114
    updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &connectRsp.epSet);
115 116
  }

S
Shengliang Guan 已提交
117
  for (int32_t i = 0; i < connectRsp.epSet.numOfEps; ++i) {
S
Shengliang Guan 已提交
118
    tscDebug("0x%" PRIx64 " epSet.fqdn[%d]:%s port:%d, connObj:0x%" PRIx64, pRequest->requestId, i,
D
dapan1121 已提交
119
             connectRsp.epSet.eps[i].fqdn, connectRsp.epSet.eps[i].port, pTscObj->id);
H
Haojun Liao 已提交
120 121
  }

122
  pTscObj->sysInfo = connectRsp.sysInfo;
S
Shengliang Guan 已提交
123 124
  pTscObj->connId = connectRsp.connId;
  pTscObj->acctId = connectRsp.acctId;
D
dapan1121 已提交
125 126
  tstrncpy(pTscObj->sVer, connectRsp.sVer, tListLen(pTscObj->sVer));
  tstrncpy(pTscObj->sDetailVer, connectRsp.sDetailVer, tListLen(pTscObj->sDetailVer));
H
Haojun Liao 已提交
127 128

  // update the appInstInfo
S
Shengliang Guan 已提交
129
  pTscObj->pAppInfo->clusterId = connectRsp.clusterId;
D
dapan1121 已提交
130
  lastClusterId = connectRsp.clusterId;
H
Haojun Liao 已提交
131

L
Liu Jicong 已提交
132
  pTscObj->connType = connectRsp.connType;
133

D
dapan1121 已提交
134
  hbRegisterConn(pTscObj->pAppInfo->pAppHbMgr, pTscObj->id, connectRsp.clusterId, connectRsp.connType);
L
Liu Jicong 已提交
135

S
Shengliang Guan 已提交
136
  tscDebug("0x%" PRIx64 " clusterId:%" PRId64 ", totalConn:%" PRId64, pRequest->requestId, connectRsp.clusterId,
137
           pTscObj->pAppInfo->numOfConns);
138

139
  tsem_post(&pRequest->body.rspSem);
dengyihao's avatar
dengyihao 已提交
140 141
End:

D
dapan1121 已提交
142 143 144
  if (pRequest) {
    releaseRequest(pRequest->self);
  }
145

D
dapan1121 已提交
146
  taosMemoryFree(param);
dengyihao's avatar
dengyihao 已提交
147 148 149
  taosMemoryFree(pMsg->pEpSet);
  taosMemoryFree(pMsg->pData);
  return code;
150
}
H
Haojun Liao 已提交
151

L
Liu Jicong 已提交
152
SMsgSendInfo* buildMsgInfoImpl(SRequestObj* pRequest) {
wafwerar's avatar
wafwerar 已提交
153
  SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
154

H
Haojun Liao 已提交
155
  pMsgSendInfo->requestObjRefId = pRequest->self;
L
Liu Jicong 已提交
156 157 158
  pMsgSendInfo->requestId = pRequest->requestId;
  pMsgSendInfo->param = pRequest;
  pMsgSendInfo->msgType = pRequest->type;
D
dapan1121 已提交
159
  pMsgSendInfo->target.type = TARGET_TYPE_MNODE;
160

H
Haojun Liao 已提交
161
  pMsgSendInfo->msgInfo = pRequest->body.requestMsg;
H
Haojun Liao 已提交
162
  pMsgSendInfo->fp = getMsgRspHandle(pRequest->type);
163
  return pMsgSendInfo;
164 165
}

D
dapan1121 已提交
166
int32_t processCreateDbRsp(void* param, SDataBuf* pMsg, int32_t code) {
H
Haojun Liao 已提交
167
  // todo rsp with the vnode id list
168
  SRequestObj* pRequest = param;
wafwerar's avatar
wafwerar 已提交
169
  taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
170
  taosMemoryFree(pMsg->pEpSet);
X
Xiaoyu Wang 已提交
171 172
  if (code != TSDB_CODE_SUCCESS) {
    setErrno(pRequest, code);
173 174 175 176
  } else {
    struct SCatalog* pCatalog = NULL;
    int32_t          code = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
    if (TSDB_CODE_SUCCESS == code) {
177
      STscObj* pTscObj = pRequest->pTscObj;
178 179 180 181 182

      SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
                               .requestId = pRequest->requestId,
                               .requestObjRefId = pRequest->self,
                               .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
183
      char             dbFName[TSDB_DB_FNAME_LEN];
184 185 186 187
      snprintf(dbFName, sizeof(dbFName) - 1, "%d.%s", pTscObj->acctId, TSDB_INFORMATION_SCHEMA_DB);
      catalogRefreshDBVgInfo(pCatalog, &conn, dbFName);
      snprintf(dbFName, sizeof(dbFName) - 1, "%d.%s", pTscObj->acctId, TSDB_PERFORMANCE_SCHEMA_DB);
      catalogRefreshDBVgInfo(pCatalog, &conn, dbFName);
188
    }
X
Xiaoyu Wang 已提交
189
  }
190 191 192 193 194 195

  if (pRequest->body.queryFp) {
    pRequest->body.queryFp(pRequest->body.param, pRequest, code);
  } else {
    tsem_post(&pRequest->body.rspSem);
  }
X
Xiaoyu Wang 已提交
196
  return code;
H
Haojun Liao 已提交
197
}
H
Haojun Liao 已提交
198

D
dapan1121 已提交
199
int32_t processUseDbRsp(void* param, SDataBuf* pMsg, int32_t code) {
H
Haojun Liao 已提交
200 201
  SRequestObj* pRequest = param;

202 203
  if (TSDB_CODE_MND_DB_NOT_EXIST == code || TSDB_CODE_MND_DB_IN_CREATING == code ||
      TSDB_CODE_MND_DB_IN_DROPPING == code) {
D
dapan1121 已提交
204 205
    SUseDbRsp usedbRsp = {0};
    tDeserializeSUseDbRsp(pMsg->pData, pMsg->len, &usedbRsp);
L
Liu Jicong 已提交
206
    struct SCatalog* pCatalog = NULL;
D
dapan1121 已提交
207

208
    if (usedbRsp.vgVersion >= 0) {  // cached in local
209
      uint64_t clusterId = pRequest->pTscObj->pAppInfo->clusterId;
dengyihao's avatar
dengyihao 已提交
210
      int32_t  code1 = catalogGetHandle(clusterId, &pCatalog);
211
      if (code1 != TSDB_CODE_SUCCESS) {
dengyihao's avatar
dengyihao 已提交
212 213
        tscWarn("0x%" PRIx64 "catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", pRequest->requestId, clusterId,
                tstrerror(code1));
D
dapan1121 已提交
214 215 216 217 218
      } else {
        catalogRemoveDB(pCatalog, usedbRsp.db, usedbRsp.uid);
      }
    }

L
Liu Jicong 已提交
219
    tFreeSUsedbRsp(&usedbRsp);
D
dapan1121 已提交
220 221
  }

H
Haojun Liao 已提交
222
  if (code != TSDB_CODE_SUCCESS) {
wafwerar's avatar
wafwerar 已提交
223
    taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
224
    taosMemoryFree(pMsg->pEpSet);
H
Haojun Liao 已提交
225
    setErrno(pRequest, code);
226 227 228 229 230 231 232

    if (pRequest->body.queryFp != NULL) {
      pRequest->body.queryFp(pRequest->body.param, pRequest, pRequest->code);
    } else {
      tsem_post(&pRequest->body.rspSem);
    }

H
Haojun Liao 已提交
233 234 235
    return code;
  }

S
Shengliang Guan 已提交
236 237 238
  SUseDbRsp usedbRsp = {0};
  tDeserializeSUseDbRsp(pMsg->pData, pMsg->len, &usedbRsp);

dengyihao's avatar
dengyihao 已提交
239
  if (strlen(usedbRsp.db) == 0) {
240 241 242 243 244
    if (usedbRsp.errCode != 0) {
      return usedbRsp.errCode;
    } else {
      return TSDB_CODE_APP_ERROR;
    }
wmmhello's avatar
wmmhello 已提交
245 246
  }

S
Shengliang Guan 已提交
247 248 249 250 251 252 253 254 255
  tscTrace("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);
    tscTrace("vgId:%d, numOfEps:%d inUse:%d ", pInfo->vgId, pInfo->epSet.numOfEps, pInfo->epSet.inUse);
    for (int32_t j = 0; j < pInfo->epSet.numOfEps; ++j) {
      tscTrace("vgId:%d, index:%d epset:%s:%u", pInfo->vgId, j, pInfo->epSet.eps[j].fqdn, pInfo->epSet.eps[j].port);
    }
  }

256
  SName name = {0};
L
Liu Jicong 已提交
257
  tNameFromString(&name, usedbRsp.db, T_NAME_ACCT | T_NAME_DB);
S
Shengliang Guan 已提交
258

D
dapan1121 已提交
259 260 261 262 263 264 265
  SUseDbOutput output = {0};
  code = queryBuildUseDbOutput(&output, &usedbRsp);

  if (code != 0) {
    terrno = code;
    if (output.dbVgroup) taosHashCleanup(output.dbVgroup->vgHash);

dengyihao's avatar
dengyihao 已提交
266
    tscError("0x%" PRIx64 " failed to build use db output since %s", pRequest->requestId, terrstr());
D
dapan1121 已提交
267
  } else if (output.dbVgroup && output.dbVgroup->vgHash) {
L
Liu Jicong 已提交
268 269
    struct SCatalog* pCatalog = NULL;

270 271
    int32_t code1 = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
    if (code1 != TSDB_CODE_SUCCESS) {
L
Liu Jicong 已提交
272
      tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", pRequest->pTscObj->pAppInfo->clusterId,
273
              tstrerror(code1));
D
dapan1121 已提交
274 275
    } else {
      catalogUpdateDBVgInfo(pCatalog, output.db, output.dbId, output.dbVgroup);
D
dapan1121 已提交
276
      output.dbVgroup = NULL;
D
dapan1121 已提交
277 278 279
    }
  }

D
dapan1121 已提交
280 281
  taosMemoryFreeClear(output.dbVgroup);

S
Shengliang Guan 已提交
282
  tFreeSUsedbRsp(&usedbRsp);
283 284 285

  char db[TSDB_DB_NAME_LEN] = {0};
  tNameGetDbName(&name, db);
286

287
  setConnectionDB(pRequest->pTscObj, db);
wafwerar's avatar
wafwerar 已提交
288
  taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
289
  taosMemoryFree(pMsg->pEpSet);
290 291 292 293 294 295

  if (pRequest->body.queryFp != NULL) {
    pRequest->body.queryFp(pRequest->body.param, pRequest, pRequest->code);
  } else {
    tsem_post(&pRequest->body.rspSem);
  }
296
  return 0;
297 298
}

D
dapan1121 已提交
299
int32_t processCreateSTableRsp(void* param, SDataBuf* pMsg, int32_t code) {
300
  if (pMsg == NULL || param == NULL) {
301 302
    return TSDB_CODE_TSC_INVALID_INPUT;
  }
303
  SRequestObj* pRequest = param;
H
Haojun Liao 已提交
304 305 306

  if (code != TSDB_CODE_SUCCESS) {
    setErrno(pRequest, code);
307 308
  } else {
    SMCreateStbRsp createRsp = {0};
dengyihao's avatar
dengyihao 已提交
309
    SDecoder       coder = {0};
310 311 312 313 314 315
    tDecoderInit(&coder, pMsg->pData, pMsg->len);
    tDecodeSMCreateStbRsp(&coder, &createRsp);
    tDecoderClear(&coder);

    pRequest->body.resInfo.execRes.msgType = TDMT_MND_CREATE_STB;
    pRequest->body.resInfo.execRes.res = createRsp.pMeta;
H
Haojun Liao 已提交
316 317
  }

dengyihao's avatar
dengyihao 已提交
318
  taosMemoryFree(pMsg->pEpSet);
319 320
  taosMemoryFree(pMsg->pData);

321
  if (pRequest->body.queryFp != NULL) {
322 323 324 325 326 327 328 329 330 331 332 333 334
    SExecResult* pRes = &pRequest->body.resInfo.execRes;

    if (code == TSDB_CODE_SUCCESS) {
      SCatalog* pCatalog = NULL;
      int32_t   ret = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
      if (pRes->res != NULL) {
        ret = handleCreateTbExecRes(pRes->res, pCatalog);
      }

      if (ret != TSDB_CODE_SUCCESS) {
        code = ret;
      }
    }
dengyihao's avatar
dengyihao 已提交
335

336 337 338 339
    pRequest->body.queryFp(pRequest->body.param, pRequest, code);
  } else {
    tsem_post(&pRequest->body.rspSem);
  }
H
Haojun Liao 已提交
340
  return code;
341 342
}

D
dapan1121 已提交
343
int32_t processDropDbRsp(void* param, SDataBuf* pMsg, int32_t code) {
344
  SRequestObj* pRequest = param;
H
Haojun Liao 已提交
345 346
  if (code != TSDB_CODE_SUCCESS) {
    setErrno(pRequest, code);
347 348 349
  } else {
    SDropDbRsp dropdbRsp = {0};
    tDeserializeSDropDbRsp(pMsg->pData, pMsg->len, &dropdbRsp);
D
dapan1121 已提交
350

351
    struct SCatalog* pCatalog = NULL;
dengyihao's avatar
dengyihao 已提交
352
    int32_t          code = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
D
dapan1121 已提交
353 354
    if (TSDB_CODE_SUCCESS == code) {
      catalogRemoveDB(pCatalog, dropdbRsp.db, dropdbRsp.uid);
355
      STscObj* pTscObj = pRequest->pTscObj;
356 357 358 359 360

      SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
                               .requestId = pRequest->requestId,
                               .requestObjRefId = pRequest->self,
                               .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
361
      char             dbFName[TSDB_DB_FNAME_LEN];
D
dapan1121 已提交
362 363 364 365
      snprintf(dbFName, sizeof(dbFName) - 1, "%d.%s", pTscObj->acctId, TSDB_INFORMATION_SCHEMA_DB);
      catalogRefreshDBVgInfo(pCatalog, &conn, dbFName);
      snprintf(dbFName, sizeof(dbFName) - 1, "%d.%s", pTscObj->acctId, TSDB_PERFORMANCE_SCHEMA_DB);
      catalogRefreshDBVgInfo(pCatalog, &conn, dbFName);
D
dapan1121 已提交
366
    }
367
  }
D
dapan1121 已提交
368

D
dapan1121 已提交
369
  taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
370
  taosMemoryFree(pMsg->pEpSet);
D
dapan1121 已提交
371

372 373 374 375 376
  if (pRequest->body.queryFp != NULL) {
    pRequest->body.queryFp(pRequest->body.param, pRequest, code);
  } else {
    tsem_post(&pRequest->body.rspSem);
  }
H
Haojun Liao 已提交
377
  return code;
378 379
}

D
dapan1121 已提交
380
int32_t processAlterStbRsp(void* param, SDataBuf* pMsg, int32_t code) {
D
dapan1121 已提交
381 382 383
  SRequestObj* pRequest = param;
  if (code != TSDB_CODE_SUCCESS) {
    setErrno(pRequest, code);
384 385 386 387 388 389 390 391 392
  } else {
    SMAlterStbRsp alterRsp = {0};
    SDecoder      coder = {0};
    tDecoderInit(&coder, pMsg->pData, pMsg->len);
    tDecodeSMAlterStbRsp(&coder, &alterRsp);
    tDecoderClear(&coder);

    pRequest->body.resInfo.execRes.msgType = TDMT_MND_ALTER_STB;
    pRequest->body.resInfo.execRes.res = alterRsp.pMeta;
D
dapan1121 已提交
393 394
  }

D
dapan1121 已提交
395
  taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
396
  taosMemoryFree(pMsg->pEpSet);
D
dapan1121 已提交
397

398
  if (pRequest->body.queryFp != NULL) {
D
dapan1121 已提交
399
    SExecResult* pRes = &pRequest->body.resInfo.execRes;
400 401 402 403 404 405 406 407 408 409 410 411 412

    if (code == TSDB_CODE_SUCCESS) {
      SCatalog* pCatalog = NULL;
      int32_t   ret = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
      if (pRes->res != NULL) {
        ret = handleAlterTbExecRes(pRes->res, pCatalog);
      }

      if (ret != TSDB_CODE_SUCCESS) {
        code = ret;
      }
    }

413 414 415 416
    pRequest->body.queryFp(pRequest->body.param, pRequest, code);
  } else {
    tsem_post(&pRequest->body.rspSem);
  }
D
dapan1121 已提交
417 418 419
  return code;
}

D
dapan1121 已提交
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
static int32_t buildShowVariablesBlock(SArray* pVars, SSDataBlock** block) {
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
  pBlock->info.hasVarCol = true;

  pBlock->pDataBlock = taosArrayInit(SHOW_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));

  SColumnInfoData infoData = {0};
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
  infoData.info.bytes = SHOW_VARIABLES_RESULT_FIELD1_LEN;

  taosArrayPush(pBlock->pDataBlock, &infoData);

  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
  infoData.info.bytes = SHOW_VARIABLES_RESULT_FIELD2_LEN;
  taosArrayPush(pBlock->pDataBlock, &infoData);

  int32_t numOfCfg = taosArrayGetSize(pVars);
  blockDataEnsureCapacity(pBlock, numOfCfg);

  for (int32_t i = 0, c = 0; i < numOfCfg; ++i, c = 0) {
440
    SVariablesInfo* pInfo = taosArrayGet(pVars, i);
D
dapan1121 已提交
441 442 443

    char name[TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE] = {0};
    STR_WITH_MAXSIZE_TO_VARSTR(name, pInfo->name, TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE);
444
    SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, c++);
445
    colDataSetVal(pColInfo, i, name, false);
446

D
dapan1121 已提交
447 448 449
    char value[TSDB_CONFIG_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
    STR_WITH_MAXSIZE_TO_VARSTR(value, pInfo->value, TSDB_CONFIG_VALUE_LEN + VARSTR_HEADER_SIZE);
    pColInfo = taosArrayGet(pBlock->pDataBlock, c++);
450
    colDataSetVal(pColInfo, i, value, false);
D
dapan1121 已提交
451 452 453 454 455
  }

  pBlock->info.rows = numOfCfg;

  *block = pBlock;
456

D
dapan1121 已提交
457 458 459 460 461
  return TSDB_CODE_SUCCESS;
}

static int32_t buildShowVariablesRsp(SArray* pVars, SRetrieveTableRsp** pRsp) {
  SSDataBlock* pBlock = NULL;
462
  int32_t      code = buildShowVariablesBlock(pVars, &pBlock);
D
dapan1121 已提交
463 464 465 466 467 468 469
  if (code) {
    return code;
  }

  size_t rspSize = sizeof(SRetrieveTableRsp) + blockGetEncodeSize(pBlock);
  *pRsp = taosMemoryCalloc(1, rspSize);
  if (NULL == *pRsp) {
D
dapan1121 已提交
470
    blockDataDestroy(pBlock);
D
dapan1121 已提交
471 472 473 474 475 476 477 478
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  (*pRsp)->useconds = 0;
  (*pRsp)->completed = 1;
  (*pRsp)->precision = 0;
  (*pRsp)->compressed = 0;
  (*pRsp)->compLen = 0;
479
  (*pRsp)->numOfRows = htobe64((int64_t)pBlock->info.rows);
D
dapan1121 已提交
480 481
  (*pRsp)->numOfCols = htonl(SHOW_VARIABLES_RESULT_COLS);

H
Haojun Liao 已提交
482
  int32_t len = blockEncode(pBlock, (*pRsp)->data, SHOW_VARIABLES_RESULT_COLS);
483 484
  blockDataDestroy(pBlock);

485 486 487
  if (len != rspSize - sizeof(SRetrieveTableRsp)) {
    uError("buildShowVariablesRsp error, len:%d != rspSize - sizeof(SRetrieveTableRsp):%" PRIu64, len,
           (uint64_t)(rspSize - sizeof(SRetrieveTableRsp)));
488 489
    return TSDB_CODE_TSC_INVALID_INPUT;
  }
D
dapan1121 已提交
490 491 492 493

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
494
int32_t processShowVariablesRsp(void* param, SDataBuf* pMsg, int32_t code) {
D
dapan1121 已提交
495 496 497 498
  SRequestObj* pRequest = param;
  if (code != TSDB_CODE_SUCCESS) {
    setErrno(pRequest, code);
  } else {
499
    SShowVariablesRsp  rsp = {0};
D
dapan1121 已提交
500 501 502 503 504 505
    SRetrieveTableRsp* pRes = NULL;
    code = tDeserializeSShowVariablesRsp(pMsg->pData, pMsg->len, &rsp);
    if (TSDB_CODE_SUCCESS == code) {
      code = buildShowVariablesRsp(rsp.variables, &pRes);
    }
    if (TSDB_CODE_SUCCESS == code) {
D
dapan1121 已提交
506
      code = setQueryResultFromRsp(&pRequest->body.resInfo, pRes, false, true);
D
dapan1121 已提交
507
    }
508

D
dapan1121 已提交
509 510 511
    tFreeSShowVariablesRsp(&rsp);
  }

D
dapan1121 已提交
512
  taosMemoryFree(pMsg->pData);
dengyihao's avatar
dengyihao 已提交
513
  taosMemoryFree(pMsg->pEpSet);
D
dapan1121 已提交
514

D
dapan1121 已提交
515 516 517 518 519 520 521 522
  if (pRequest->body.queryFp != NULL) {
    pRequest->body.queryFp(pRequest->body.param, pRequest, code);
  } else {
    tsem_post(&pRequest->body.rspSem);
  }
  return code;
}

H
Haojun Liao 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536
__async_send_cb_fn_t getMsgRspHandle(int32_t msgType) {
  switch (msgType) {
    case TDMT_MND_CONNECT:
      return processConnectRsp;
    case TDMT_MND_CREATE_DB:
      return processCreateDbRsp;
    case TDMT_MND_USE_DB:
      return processUseDbRsp;
    case TDMT_MND_CREATE_STB:
      return processCreateSTableRsp;
    case TDMT_MND_DROP_DB:
      return processDropDbRsp;
    case TDMT_MND_ALTER_STB:
      return processAlterStbRsp;
D
dapan1121 已提交
537 538
    case TDMT_MND_SHOW_VARIABLES:
      return processShowVariablesRsp;
H
Haojun Liao 已提交
539 540 541
    default:
      return genericRspCallback;
  }
L
Liu Jicong 已提交
542
}