• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3526

10 Nov 2024 03:50AM UTC coverage: 60.225% (-0.6%) from 60.818%
#3526

push

travis-ci

web-flow
Merge pull request #28709 from taosdata/main

merge: from main to 3.0 branch

117031 of 249004 branches covered (47.0%)

Branch coverage included in aggregate %.

130 of 169 new or added lines in 23 files covered. (76.92%)

4149 existing lines in 176 files now uncovered.

197577 of 273386 relevant lines covered (72.27%)

5840219.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

65.71
/source/client/src/clientMsgHandler.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "catalog.h"
17
#include "clientInt.h"
18
#include "clientMonitor.h"
19
#include "clientLog.h"
20
#include "cmdnodes.h"
21
#include "os.h"
22
#include "query.h"
23
#include "systable.h"
24
#include "tdatablock.h"
25
#include "tdef.h"
26
#include "tglobal.h"
27
#include "tname.h"
28
#include "tversion.h"
29
#include "command.h"
30

31
extern SClientHbMgr clientHbMgr;
32

33
static void setErrno(SRequestObj* pRequest, int32_t code) {
11,740✔
34
  pRequest->code = code;
11,740✔
35
  terrno = code;
11,740✔
36
}
11,740✔
37

38
int32_t genericRspCallback(void* param, SDataBuf* pMsg, int32_t code) {
9,200✔
39
  SRequestObj* pRequest = param;
9,200✔
40
  setErrno(pRequest, code);
9,200✔
41

42
  if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type)) {
9,200!
43
    if (removeMeta(pRequest->pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type)) != 0){
2,416!
44
      tscError("failed to remove meta data for table");
×
45
    }
46
  }
47

48
  taosMemoryFree(pMsg->pEpSet);
9,200✔
49
  taosMemoryFree(pMsg->pData);
9,200✔
50
  if (pRequest->body.queryFp != NULL) {
9,200✔
51
    doRequestCallback(pRequest, code);
9,194✔
52
  } else {
53
    if (tsem_post(&pRequest->body.rspSem) != 0){
6!
54
      tscError("failed to post semaphore");
×
55
    }
56
  }
57
  return code;
9,200✔
58
}
59

60
int32_t processConnectRsp(void* param, SDataBuf* pMsg, int32_t code) {
9,045✔
61
  SRequestObj* pRequest = acquireRequest(*(int64_t*)param);
9,045✔
62
  if (NULL == pRequest) {
9,045!
63
    goto EXIT;
×
64
  }
65

66
  if (code != TSDB_CODE_SUCCESS) {
9,045✔
67
    goto End;
101✔
68
  }
69

70
  STscObj* pTscObj = pRequest->pTscObj;
8,944✔
71

72
  if (NULL == pTscObj->pAppInfo) {
8,944!
73
    code = TSDB_CODE_TSC_DISCONNECTED;
×
74
    goto End;
×
75
  }
76

77
  SConnectRsp connectRsp = {0};
8,944✔
78
  if (tDeserializeSConnectRsp(pMsg->pData, pMsg->len, &connectRsp) != 0) {
8,944!
79
    code = TSDB_CODE_TSC_INVALID_VERSION;
×
80
    goto End;
×
81
  }
82

83
  if ((code = taosCheckVersionCompatibleFromStr(td_version, connectRsp.sVer, 3)) != 0) {
8,944!
84
    tscError("version not compatible. client version: %s, server version: %s", td_version, connectRsp.sVer);
×
85
    goto End;
×
86
  }
87

88
  int32_t now = taosGetTimestampSec();
8,944✔
89
  int32_t delta = abs(now - connectRsp.svrTimestamp);
8,944✔
90
  if (delta > timestampDeltaLimit) {
8,944!
91
    code = TSDB_CODE_TIME_UNSYNCED;
×
92
    tscError("time diff:%ds is too big", delta);
×
93
    goto End;
×
94
  }
95

96
  if (connectRsp.epSet.numOfEps == 0) {
8,944!
97
    code = TSDB_CODE_APP_ERROR;
×
98
    goto End;
×
99
  }
100

101
  int updateEpSet = 1;
8,944✔
102
  if (connectRsp.dnodeNum == 1) {
8,944✔
103
    SEpSet srcEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
8,248✔
104
    SEpSet dstEpSet = connectRsp.epSet;
8,248✔
105
    if (srcEpSet.numOfEps == 1) {
8,248✔
106
      if (rpcSetDefaultAddr(pTscObj->pAppInfo->pTransporter, srcEpSet.eps[srcEpSet.inUse].fqdn,
5,999!
107
                        dstEpSet.eps[dstEpSet.inUse].fqdn) != 0){
5,999✔
108
        tscError("failed to set default addr for rpc");
×
109
      }
110
      updateEpSet = 0;
5,999✔
111
    }
112
  }
113
  if (updateEpSet == 1 && !isEpsetEqual(&pTscObj->pAppInfo->mgmtEp.epSet, &connectRsp.epSet)) {
8,944✔
114
    SEpSet corEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
2,419✔
115

116
    SEpSet* pOrig = &corEpSet;
2,419✔
117
    SEp*    pOrigEp = &pOrig->eps[pOrig->inUse];
2,419✔
118
    SEp*    pNewEp = &connectRsp.epSet.eps[connectRsp.epSet.inUse];
2,419✔
119
    tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in connRsp", pOrig->inUse, pOrig->numOfEps,
2,419✔
120
             pOrigEp->fqdn, pOrigEp->port, connectRsp.epSet.inUse, connectRsp.epSet.numOfEps, pNewEp->fqdn,
121
             pNewEp->port);
122
    updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &connectRsp.epSet);
2,419✔
123
  }
124

125
  for (int32_t i = 0; i < connectRsp.epSet.numOfEps; ++i) {
18,250✔
126
    tscDebug("0x%" PRIx64 " epSet.fqdn[%d]:%s port:%d, connObj:0x%" PRIx64, pRequest->requestId, i,
9,306✔
127
             connectRsp.epSet.eps[i].fqdn, connectRsp.epSet.eps[i].port, pTscObj->id);
128
  }
129

130
  pTscObj->sysInfo = connectRsp.sysInfo;
8,944✔
131
  pTscObj->connId = connectRsp.connId;
8,944✔
132
  pTscObj->acctId = connectRsp.acctId;
8,944✔
133
  tstrncpy(pTscObj->sVer, connectRsp.sVer, tListLen(pTscObj->sVer));
8,944✔
134
  tstrncpy(pTscObj->sDetailVer, connectRsp.sDetailVer, tListLen(pTscObj->sDetailVer));
8,944✔
135

136
  // update the appInstInfo
137
  pTscObj->pAppInfo->clusterId = connectRsp.clusterId;
8,944✔
138
  pTscObj->pAppInfo->serverCfg.monitorParas = connectRsp.monitorParas;
8,944✔
139
  pTscObj->pAppInfo->serverCfg.enableAuditDelete = connectRsp.enableAuditDelete;
8,944✔
140
  tscDebug("[monitor] paras from connect rsp, clusterId:%" PRIx64 " monitorParas threshold:%d scope:%d",
8,944✔
141
           connectRsp.clusterId, connectRsp.monitorParas.tsSlowLogThreshold, connectRsp.monitorParas.tsSlowLogScope);
142
  lastClusterId = connectRsp.clusterId;
8,944✔
143

144
  pTscObj->connType = connectRsp.connType;
8,944✔
145
  pTscObj->passInfo.ver = connectRsp.passVer;
8,944✔
146
  pTscObj->authVer = connectRsp.authVer;
8,944✔
147
  pTscObj->whiteListInfo.ver = connectRsp.whiteListVer;
8,944✔
148

149
  if(taosHashGet(appInfo.pInstMapByClusterId, &connectRsp.clusterId, LONG_BYTES) == NULL){
8,944✔
150
    if(taosHashPut(appInfo.pInstMapByClusterId, &connectRsp.clusterId, LONG_BYTES, &pTscObj->pAppInfo, POINTER_BYTES) != 0){
3,250!
151
      tscError("failed to put appInfo into appInfo.pInstMapByClusterId");
×
152
    }else{
153
      MonitorSlowLogData data = {0};
3,250✔
154
      data.clusterId = pTscObj->pAppInfo->clusterId;
3,250✔
155
      data.type = SLOW_LOG_READ_BEGINNIG;
3,250✔
156
      (void)monitorPutData2MonitorQueue(data); // ignore
3,250✔
157
      monitorClientSlowQueryInit(connectRsp.clusterId);
3,250✔
158
      monitorClientSQLReqInit(connectRsp.clusterId);
3,250✔
159
    }
160
  }
161

162
  (void)taosThreadMutexLock(&clientHbMgr.lock);
8,944✔
163
  SAppHbMgr* pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, pTscObj->appHbMgrIdx);
8,944✔
164
  if (pAppHbMgr) {
8,944!
165
    if (hbRegisterConn(pAppHbMgr, pTscObj->id, connectRsp.clusterId, connectRsp.connType) != 0){
8,944!
166
      tscError("0x%" PRIx64 " failed to register conn to hbMgr", pRequest->requestId);
×
167
    }
168
  } else {
169
    (void)taosThreadMutexUnlock(&clientHbMgr.lock);
×
170
    code = TSDB_CODE_TSC_DISCONNECTED;
×
171
    goto End;
×
172
  }
173
  (void)taosThreadMutexUnlock(&clientHbMgr.lock);
8,944✔
174

175
  tscDebug("0x%" PRIx64 " clusterId:%" PRId64 ", totalConn:%" PRId64, pRequest->requestId, connectRsp.clusterId,
8,944✔
176
           pTscObj->pAppInfo->numOfConns);
177

178
End:
7,629✔
179
  if (code != 0){
9,045✔
180
    setErrno(pRequest, code);
101✔
181
  }
182
  if (tsem_post(&pRequest->body.rspSem) != 0){
9,045!
183
    tscError("failed to post semaphore");
×
184
  }
185

186
  if (pRequest) {
9,045!
187
    (void)releaseRequest(pRequest->self);
9,045✔
188
  }
189

190
EXIT:
×
191
  taosMemoryFree(param);
9,045✔
192
  taosMemoryFree(pMsg->pEpSet);
9,045✔
193
  taosMemoryFree(pMsg->pData);
9,045✔
194
  return code;
9,045✔
195
}
196

197
SMsgSendInfo* buildMsgInfoImpl(SRequestObj* pRequest) {
37,366✔
198
  SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
37,366✔
199
  if(pMsgSendInfo == NULL) return pMsgSendInfo;
37,367!
200
  pMsgSendInfo->requestObjRefId = pRequest->self;
37,367✔
201
  pMsgSendInfo->requestId = pRequest->requestId;
37,367✔
202
  pMsgSendInfo->param = pRequest;
37,367✔
203
  pMsgSendInfo->msgType = pRequest->type;
37,367✔
204
  pMsgSendInfo->target.type = TARGET_TYPE_MNODE;
37,367✔
205

206
  pMsgSendInfo->msgInfo = pRequest->body.requestMsg;
37,367✔
207
  pMsgSendInfo->fp = getMsgRspHandle(pRequest->type);
37,367✔
208
  return pMsgSendInfo;
37,367✔
209
}
210

211
int32_t processCreateDbRsp(void* param, SDataBuf* pMsg, int32_t code) {
4,947✔
212
  // todo rsp with the vnode id list
213
  SRequestObj* pRequest = param;
4,947✔
214
  taosMemoryFree(pMsg->pData);
4,947✔
215
  taosMemoryFree(pMsg->pEpSet);
4,947✔
216
  if (code != TSDB_CODE_SUCCESS) {
4,947✔
217
    setErrno(pRequest, code);
691✔
218
  } else {
219
    struct SCatalog* pCatalog = NULL;
4,256✔
220
    code = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
4,256✔
221
    if (TSDB_CODE_SUCCESS == code) {
4,256!
222
      STscObj* pTscObj = pRequest->pTscObj;
4,256✔
223

224
      SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
4,256✔
225
                               .requestId = pRequest->requestId,
4,256✔
226
                               .requestObjRefId = pRequest->self,
4,256✔
227
                               .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
4,256✔
228
      char             dbFName[TSDB_DB_FNAME_LEN];
229
      (void)snprintf(dbFName, sizeof(dbFName) - 1, "%d.%s", pTscObj->acctId, TSDB_INFORMATION_SCHEMA_DB);
4,256✔
230
      if (catalogRefreshDBVgInfo(pCatalog, &conn, dbFName) != 0){
4,256!
231
        tscError("0x%" PRIx64 " failed to refresh db vg info", pRequest->requestId);
×
232
      }
233
      (void)snprintf(dbFName, sizeof(dbFName) - 1, "%d.%s", pTscObj->acctId, TSDB_PERFORMANCE_SCHEMA_DB);
4,256✔
234
      if (catalogRefreshDBVgInfo(pCatalog, &conn, dbFName) != 0){
4,256!
235
        tscError("0x%" PRIx64 " failed to refresh db vg info", pRequest->requestId);
×
236
      }
237
    }
238
  }
239

240
  if (pRequest->body.queryFp) {
4,947!
241
    doRequestCallback(pRequest, code);
4,947✔
242
  } else {
243
    if (tsem_post(&pRequest->body.rspSem) != 0){
×
244
      tscError("failed to post semaphore");
×
245
    }
246
  }
247
  return code;
4,947✔
248
}
249

250
int32_t processUseDbRsp(void* param, SDataBuf* pMsg, int32_t code) {
8,569✔
251
  SRequestObj* pRequest = param;
8,569✔
252
  if (TSDB_CODE_MND_DB_NOT_EXIST == code || TSDB_CODE_MND_DB_IN_CREATING == code ||
8,569!
253
      TSDB_CODE_MND_DB_IN_DROPPING == code) {
UNCOV
254
    SUseDbRsp usedbRsp = {0};
×
UNCOV
255
    if (tDeserializeSUseDbRsp(pMsg->pData, pMsg->len, &usedbRsp) != 0){
×
256
      tscError("0x%" PRIx64 " deserialize SUseDbRsp failed", pRequest->requestId);
1!
257
    }
258
    struct SCatalog* pCatalog = NULL;
1✔
259

260
    if (usedbRsp.vgVersion >= 0) {  // cached in local
1!
261
      int64_t clusterId = pRequest->pTscObj->pAppInfo->clusterId;
1✔
262
      int32_t  code1 = catalogGetHandle(clusterId, &pCatalog);
1✔
263
      if (code1 != TSDB_CODE_SUCCESS) {
1!
264
        tscWarn("0x%" PRIx64 "catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", pRequest->requestId, clusterId,
×
265
                tstrerror(code1));
266
      } else {
267
        if (catalogRemoveDB(pCatalog, usedbRsp.db, usedbRsp.uid) != 0){
1!
268
          tscError("0x%" PRIx64 "catalogRemoveDB failed, db:%s, uid:%" PRId64, pRequest->requestId, usedbRsp.db,
×
269
                   usedbRsp.uid);
270
        }
271
      }
272
    }
273
    tFreeSUsedbRsp(&usedbRsp);
1✔
274
  }
275

276
  if (code != TSDB_CODE_SUCCESS) {
8,570✔
277
    taosMemoryFree(pMsg->pData);
8✔
278
    taosMemoryFree(pMsg->pEpSet);
8✔
279
    setErrno(pRequest, code);
8✔
280

281
    if (pRequest->body.queryFp != NULL) {
8!
282
      doRequestCallback(pRequest, pRequest->code);
8✔
283

284
    } else {
285
      if (tsem_post(&pRequest->body.rspSem) != 0){
×
286
        tscError("failed to post semaphore");
×
287
      }
288
    }
289

290
    return code;
8✔
291
  }
292

293
  SUseDbRsp usedbRsp = {0};
8,562✔
294
  if (tDeserializeSUseDbRsp(pMsg->pData, pMsg->len, &usedbRsp) != 0){
8,562!
295
    tscError("0x%" PRIx64 " deserialize SUseDbRsp failed", pRequest->requestId);
×
296
  }
297

298
  if (strlen(usedbRsp.db) == 0) {
8,561!
299
    taosMemoryFree(pMsg->pData);
×
300
    taosMemoryFree(pMsg->pEpSet);
×
301

302
    if (usedbRsp.errCode != 0) {
×
303
      return usedbRsp.errCode;
×
304
    } else {
305
      return TSDB_CODE_APP_ERROR;
×
306
    }
307
  }
308

309
  tscTrace("db:%s, usedbRsp received, numOfVgroups:%d", usedbRsp.db, usedbRsp.vgNum);
8,561✔
310
  for (int32_t i = 0; i < usedbRsp.vgNum; ++i) {
15,535✔
311
    SVgroupInfo* pInfo = taosArrayGet(usedbRsp.pVgroupInfos, i);
6,974✔
312
    if (pInfo == NULL){
6,974!
313
      continue;
×
314
    }
315
    tscTrace("vgId:%d, numOfEps:%d inUse:%d ", pInfo->vgId, pInfo->epSet.numOfEps, pInfo->epSet.inUse);
6,974✔
316
    for (int32_t j = 0; j < pInfo->epSet.numOfEps; ++j) {
14,372✔
317
      tscTrace("vgId:%d, index:%d epset:%s:%u", pInfo->vgId, j, pInfo->epSet.eps[j].fqdn, pInfo->epSet.eps[j].port);
7,398✔
318
    }
319
  }
320

321
  SName name = {0};
8,561✔
322
  if(tNameFromString(&name, usedbRsp.db, T_NAME_ACCT | T_NAME_DB) != TSDB_CODE_SUCCESS) {
8,561!
323
    tscError("0x%" PRIx64 " failed to parse db name:%s", pRequest->requestId, usedbRsp.db);
×
324
  }
325

326
  SUseDbOutput output = {0};
8,562✔
327
  code = queryBuildUseDbOutput(&output, &usedbRsp);
8,562✔
328
  if (code != 0) {
8,561!
329
    terrno = code;
×
330
    if (output.dbVgroup) taosHashCleanup(output.dbVgroup->vgHash);
×
331

332
    tscError("0x%" PRIx64 " failed to build use db output since %s", pRequest->requestId, terrstr());
×
333
  } else if (output.dbVgroup && output.dbVgroup->vgHash) {
8,561✔
334
    struct SCatalog* pCatalog = NULL;
3,159✔
335

336
    int32_t code1 = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
3,159✔
337
    if (code1 != TSDB_CODE_SUCCESS) {
3,159!
338
      tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", pRequest->pTscObj->pAppInfo->clusterId,
×
339
              tstrerror(code1));
340
    } else {
341
      if (catalogUpdateDBVgInfo(pCatalog, output.db, output.dbId, output.dbVgroup) != 0){
3,159!
342
        tscError("0x%" PRIx64 " failed to update db vg info, db:%s, dbId:%" PRId64, pRequest->requestId, output.db,
×
343
                 output.dbId);
344
      }
345
      output.dbVgroup = NULL;
3,159✔
346
    }
347
  }
348

349
  taosMemoryFreeClear(output.dbVgroup);
8,560✔
350
  tFreeSUsedbRsp(&usedbRsp);
8,563✔
351

352
  char db[TSDB_DB_NAME_LEN] = {0};
8,562✔
353
  if(tNameGetDbName(&name, db) != TSDB_CODE_SUCCESS) {
8,562!
354
    tscError("0x%" PRIx64 " failed to get db name since %s", pRequest->requestId, tstrerror(code));
×
355
  }
356

357
  setConnectionDB(pRequest->pTscObj, db);
8,560✔
358

359
  taosMemoryFree(pMsg->pData);
8,564✔
360
  taosMemoryFree(pMsg->pEpSet);
8,564✔
361

362
  if (pRequest->body.queryFp != NULL) {
8,564!
363
    doRequestCallback(pRequest, pRequest->code);
8,564✔
364
  } else {
365
    if (tsem_post(&pRequest->body.rspSem) != 0){
×
366
      tscError("failed to post semaphore");
×
367
    }
368
  }
369
  return 0;
8,563✔
370
}
371

372
int32_t processCreateSTableRsp(void* param, SDataBuf* pMsg, int32_t code) {
7,184✔
373
  if (pMsg == NULL) {
7,184!
374
    return TSDB_CODE_TSC_INVALID_INPUT;
×
375
  }
376
  if (param == NULL) {
7,184!
377
    taosMemoryFree(pMsg->pEpSet);
×
378
    taosMemoryFree(pMsg->pData);
×
379
    return TSDB_CODE_TSC_INVALID_INPUT;
×
380
  }
381

382
  SRequestObj* pRequest = param;
7,184✔
383

384
  if (code != TSDB_CODE_SUCCESS) {
7,184✔
385
    setErrno(pRequest, code);
58✔
386
  } else {
387
    SMCreateStbRsp createRsp = {0};
7,126✔
388
    SDecoder       coder = {0};
7,126✔
389
    tDecoderInit(&coder, pMsg->pData, pMsg->len);
7,126✔
390
    if (pMsg->len > 0){
7,126✔
391
      code = tDecodeSMCreateStbRsp(&coder, &createRsp);  // pMsg->len == 0
7,064✔
392
      if (code != TSDB_CODE_SUCCESS) {
7,064!
393
        setErrno(pRequest, code);
×
394
      }
395
    }
396
    tDecoderClear(&coder);
7,126✔
397

398
    pRequest->body.resInfo.execRes.msgType = TDMT_MND_CREATE_STB;
7,126✔
399
    pRequest->body.resInfo.execRes.res = createRsp.pMeta;
7,126✔
400
  }
401

402
  taosMemoryFree(pMsg->pEpSet);
7,184✔
403
  taosMemoryFree(pMsg->pData);
7,184✔
404

405
  if (pRequest->body.queryFp != NULL) {
7,184✔
406
    SExecResult* pRes = &pRequest->body.resInfo.execRes;
6,253✔
407

408
    if (code == TSDB_CODE_SUCCESS) {
6,253✔
409
      SCatalog* pCatalog = NULL;
6,231✔
410
      int32_t   ret = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
6,231✔
411
      if (pRes->res != NULL) {
6,231✔
412
        ret = handleCreateTbExecRes(pRes->res, pCatalog);
6,222✔
413
      }
414

415
      if (ret != TSDB_CODE_SUCCESS) {
6,231!
416
        code = ret;
×
417
      }
418
    }
419

420
    doRequestCallback(pRequest, code);
6,253✔
421
  } else {
422
    if (tsem_post(&pRequest->body.rspSem) != 0){
931!
423
      tscError("failed to post semaphore");
×
424
    }
425
  }
426
  return code;
7,184✔
427
}
428

429
int32_t processDropDbRsp(void* param, SDataBuf* pMsg, int32_t code) {
3,433✔
430
  SRequestObj* pRequest = param;
3,433✔
431
  if (code != TSDB_CODE_SUCCESS) {
3,433✔
432
    setErrno(pRequest, code);
85✔
433
  } else {
434
    SDropDbRsp dropdbRsp = {0};
3,348✔
435
    if (tDeserializeSDropDbRsp(pMsg->pData, pMsg->len, &dropdbRsp) != 0){
3,348!
436
      tscError("0x%" PRIx64 " deserialize SDropDbRsp failed", pRequest->requestId);
×
437
    }
438
    struct SCatalog* pCatalog = NULL;
3,348✔
439
    code = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
3,348✔
440
    if (TSDB_CODE_SUCCESS == code) {
3,348!
441
      if (catalogRemoveDB(pCatalog, dropdbRsp.db, dropdbRsp.uid) != 0){
3,348!
442
        tscError("0x%" PRIx64 " failed to remove db:%s", pRequest->requestId, dropdbRsp.db);
×
443
      }
444
      STscObj* pTscObj = pRequest->pTscObj;
3,348✔
445

446
      SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
3,348✔
447
                               .requestId = pRequest->requestId,
3,348✔
448
                               .requestObjRefId = pRequest->self,
3,348✔
449
                               .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
3,348✔
450
      char             dbFName[TSDB_DB_FNAME_LEN] = {0};
3,348✔
451
      (void)snprintf(dbFName, sizeof(dbFName) - 1, "%d.%s", pTscObj->acctId, TSDB_INFORMATION_SCHEMA_DB);
3,348✔
452
      if (catalogRefreshDBVgInfo(pCatalog, &conn, dbFName) != TSDB_CODE_SUCCESS) {
3,348!
453
        tscError("0x%" PRIx64 " failed to refresh db vg info, db:%s", pRequest->requestId, dbFName);
×
454
       }
455
      (void)snprintf(dbFName, sizeof(dbFName) - 1, "%d.%s", pTscObj->acctId, TSDB_PERFORMANCE_SCHEMA_DB);
3,348✔
456
      if (catalogRefreshDBVgInfo(pCatalog, &conn, dbFName) != 0) {
3,348!
457
        tscError("0x%" PRIx64 " failed to refresh db vg info, db:%s", pRequest->requestId, dbFName);
×
458
      }
459
    }
460
  }
461

462
  taosMemoryFree(pMsg->pData);
3,433✔
463
  taosMemoryFree(pMsg->pEpSet);
3,433✔
464

465
  if (pRequest->body.queryFp != NULL) {
3,433!
466
    doRequestCallback(pRequest, code);
3,433✔
467
  } else {
468
    if (tsem_post(&pRequest->body.rspSem) != 0){
×
469
      tscError("failed to post semaphore");
×
470
    }
471
  }
472
  return code;
3,433✔
473
}
474

475
int32_t processAlterStbRsp(void* param, SDataBuf* pMsg, int32_t code) {
4,011✔
476
  SRequestObj* pRequest = param;
4,011✔
477
  if (code != TSDB_CODE_SUCCESS) {
4,011✔
478
    setErrno(pRequest, code);
1,597✔
479
  } else {
480
    SMAlterStbRsp alterRsp = {0};
2,414✔
481
    SDecoder      coder = {0};
2,414✔
482
    tDecoderInit(&coder, pMsg->pData, pMsg->len);
2,414✔
483
    if (pMsg->len > 0){
2,414✔
484
      code = tDecodeSMAlterStbRsp(&coder, &alterRsp);  // pMsg->len == 0
2,385✔
485
      if (code != TSDB_CODE_SUCCESS) {
2,385!
486
        setErrno(pRequest, code);
×
487
      }
488
    }
489
    tDecoderClear(&coder);
2,414✔
490

491
    pRequest->body.resInfo.execRes.msgType = TDMT_MND_ALTER_STB;
2,414✔
492
    pRequest->body.resInfo.execRes.res = alterRsp.pMeta;
2,414✔
493
  }
494

495
  taosMemoryFree(pMsg->pData);
4,011✔
496
  taosMemoryFree(pMsg->pEpSet);
4,011✔
497

498
  if (pRequest->body.queryFp != NULL) {
4,011!
499
    SExecResult* pRes = &pRequest->body.resInfo.execRes;
4,011✔
500

501
    if (code == TSDB_CODE_SUCCESS) {
4,011✔
502
      SCatalog* pCatalog = NULL;
2,414✔
503
      int32_t   ret = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
2,414✔
504
      if (pRes->res != NULL) {
2,414✔
505
        ret = handleAlterTbExecRes(pRes->res, pCatalog);
2,385✔
506
      }
507

508
      if (ret != TSDB_CODE_SUCCESS) {
2,414!
509
        code = ret;
×
510
      }
511
    }
512

513
    doRequestCallback(pRequest, code);
4,011✔
514
  } else {
515
    if (tsem_post(&pRequest->body.rspSem) != 0){
×
516
      tscError("failed to post semaphore");
×
517
    }
518
  }
519
  return code;
4,011✔
520
}
521

522
static int32_t buildShowVariablesBlock(SArray* pVars, SSDataBlock** block) {
11✔
523
  int32_t code = 0;
11✔
524
  int32_t line = 0;
11✔
525
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
11✔
526
  TSDB_CHECK_NULL(pBlock, code, line, END, terrno);
11!
527
  pBlock->info.hasVarCol = true;
11✔
528

529
  pBlock->pDataBlock = taosArrayInit(SHOW_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));
11✔
530
  TSDB_CHECK_NULL(pBlock->pDataBlock, code, line, END, terrno);
11!
531
  SColumnInfoData infoData = {0};
11✔
532
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
11✔
533
  infoData.info.bytes = SHOW_VARIABLES_RESULT_FIELD1_LEN;
11✔
534
  TSDB_CHECK_NULL(taosArrayPush(pBlock->pDataBlock, &infoData), code, line, END, terrno);
22!
535

536
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
11✔
537
  infoData.info.bytes = SHOW_VARIABLES_RESULT_FIELD2_LEN;
11✔
538
  TSDB_CHECK_NULL(taosArrayPush(pBlock->pDataBlock, &infoData), code, line, END, terrno);
22!
539

540
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
11✔
541
  infoData.info.bytes = SHOW_VARIABLES_RESULT_FIELD3_LEN;
11✔
542
  TSDB_CHECK_NULL(taosArrayPush(pBlock->pDataBlock, &infoData), code, line, END, terrno);
22!
543

544
  int32_t numOfCfg = taosArrayGetSize(pVars);
11✔
545
  code = blockDataEnsureCapacity(pBlock, numOfCfg);
11✔
546
  TSDB_CHECK_CODE(code, line, END);
11!
547

548
  for (int32_t i = 0, c = 0; i < numOfCfg; ++i, c = 0) {
110✔
549
    SVariablesInfo* pInfo = taosArrayGet(pVars, i);
99✔
550
    TSDB_CHECK_NULL(pInfo, code, line, END, terrno);
99!
551

552
    char name[TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE] = {0};
99✔
553
    STR_WITH_MAXSIZE_TO_VARSTR(name, pInfo->name, TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE);
99✔
554
    SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, c++);
99✔
555
    TSDB_CHECK_NULL(pColInfo, code, line, END, terrno);
99!
556
    code = colDataSetVal(pColInfo, i, name, false);
99✔
557
    TSDB_CHECK_CODE(code, line, END);
99!
558

559
    char value[TSDB_CONFIG_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
99✔
560
    STR_WITH_MAXSIZE_TO_VARSTR(value, pInfo->value, TSDB_CONFIG_VALUE_LEN + VARSTR_HEADER_SIZE);
99✔
561
    pColInfo = taosArrayGet(pBlock->pDataBlock, c++);
99✔
562
    TSDB_CHECK_NULL(pColInfo, code, line, END, terrno);
99!
563
    code = colDataSetVal(pColInfo, i, value, false);
99✔
564
    TSDB_CHECK_CODE(code, line, END);
99!
565

566
    char scope[TSDB_CONFIG_SCOPE_LEN + VARSTR_HEADER_SIZE] = {0};
99✔
567
    STR_WITH_MAXSIZE_TO_VARSTR(scope, pInfo->scope, TSDB_CONFIG_SCOPE_LEN + VARSTR_HEADER_SIZE);
99✔
568
    pColInfo = taosArrayGet(pBlock->pDataBlock, c++);
99✔
569
    TSDB_CHECK_NULL(pColInfo, code, line, END, terrno);
99!
570
    code = colDataSetVal(pColInfo, i, scope, false);
99✔
571
    TSDB_CHECK_CODE(code, line, END);
99!
572
  }
573

574
  pBlock->info.rows = numOfCfg;
11✔
575

576
  *block = pBlock;
11✔
577
  return code;
11✔
578

579
END:
×
580
  taosArrayDestroy(pBlock->pDataBlock);
×
581
  taosMemoryFree(pBlock);
×
582
  return code;
×
583
}
584

585
static int32_t buildShowVariablesRsp(SArray* pVars, SRetrieveTableRsp** pRsp) {
11✔
586
  SSDataBlock* pBlock = NULL;
11✔
587
  int32_t      code = buildShowVariablesBlock(pVars, &pBlock);
11✔
588
  if (code) {
11!
589
    return code;
×
590
  }
591

592
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
11✔
593
  size_t rspSize = sizeof(SRetrieveTableRsp) + dataEncodeBufSize + PAYLOAD_PREFIX_LEN;
11✔
594
  *pRsp = taosMemoryCalloc(1, rspSize);
11✔
595
  if (NULL == *pRsp) {
11!
596
    code = terrno;
×
597
    goto  _exit;
×
598
  }
599

600
  (*pRsp)->useconds = 0;
11✔
601
  (*pRsp)->completed = 1;
11✔
602
  (*pRsp)->precision = 0;
11✔
603
  (*pRsp)->compressed = 0;
11✔
604

605
  (*pRsp)->numOfRows = htobe64((int64_t)pBlock->info.rows);
11✔
606
  (*pRsp)->numOfCols = htonl(SHOW_VARIABLES_RESULT_COLS);
11✔
607

608
  int32_t len = blockEncode(pBlock, (*pRsp)->data + PAYLOAD_PREFIX_LEN, dataEncodeBufSize, SHOW_VARIABLES_RESULT_COLS);
11✔
609
  if(len < 0) {
11!
610
    uError("buildShowVariablesRsp error, len:%d", len);
×
611
    code = terrno;
×
612
    goto _exit;
×
613
  }
614
  blockDataDestroy(pBlock);
11✔
615

616
  SET_PAYLOAD_LEN((*pRsp)->data, len, len);
11✔
617

618
  int32_t payloadLen = len + PAYLOAD_PREFIX_LEN;
11✔
619
  (*pRsp)->payloadLen = htonl(payloadLen);
11✔
620
  (*pRsp)->compLen = htonl(payloadLen);
11✔
621

622
  if (payloadLen != rspSize - sizeof(SRetrieveTableRsp)) {
11!
623
    uError("buildShowVariablesRsp error, len:%d != rspSize - sizeof(SRetrieveTableRsp):%" PRIu64, len,
×
624
           (uint64_t)(rspSize - sizeof(SRetrieveTableRsp)));
625
    code = TSDB_CODE_TSC_INVALID_INPUT;
×
626
    goto _exit;
×
627
  }
628

629
  return TSDB_CODE_SUCCESS;
11✔
630
_exit:
×
631
  if(*pRsp)  {
×
632
    taosMemoryFree(*pRsp);
×
633
    *pRsp = NULL;
×
634
  }
635
  if(pBlock) {
×
636
    blockDataDestroy(pBlock);
×
637
    pBlock = NULL;
×
638
  }
639
  return code;
×
640
}
641

642
int32_t processShowVariablesRsp(void* param, SDataBuf* pMsg, int32_t code) {
11✔
643
  SRequestObj* pRequest = param;
11✔
644
  if (code != TSDB_CODE_SUCCESS) {
11!
645
    setErrno(pRequest, code);
×
646
  } else {
647
    SShowVariablesRsp  rsp = {0};
11✔
648
    SRetrieveTableRsp* pRes = NULL;
11✔
649
    code = tDeserializeSShowVariablesRsp(pMsg->pData, pMsg->len, &rsp);
11✔
650
    if (TSDB_CODE_SUCCESS == code) {
11!
651
      code = buildShowVariablesRsp(rsp.variables, &pRes);
11✔
652
    }
653
    if (TSDB_CODE_SUCCESS == code) {
11!
654
      code = setQueryResultFromRsp(&pRequest->body.resInfo, pRes, false);
11✔
655
    }
656

657
    if (code != 0) {
11!
658
      taosMemoryFree(pRes);
×
659
    }
660
    tFreeSShowVariablesRsp(&rsp);
11✔
661
  }
662

663
  taosMemoryFree(pMsg->pData);
11✔
664
  taosMemoryFree(pMsg->pEpSet);
11✔
665

666
  if (pRequest->body.queryFp != NULL) {
11!
667
    doRequestCallback(pRequest, code);
11✔
668
  } else {
669
    if (tsem_post(&pRequest->body.rspSem) != 0){
×
670
      tscError("failed to post semaphore");
×
671
    }
672
  }
673
  return code;
11✔
674
}
675

676
static int32_t buildCompactDbBlock(SCompactDbRsp* pRsp, SSDataBlock** block) {
10✔
677
  int32_t code = 0;
10✔
678
  int32_t line = 0;
10✔
679
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
10✔
680
  TSDB_CHECK_NULL(pBlock, code, line, END, terrno);
10!
681
  pBlock->info.hasVarCol = true;
10✔
682

683
  pBlock->pDataBlock = taosArrayInit(COMPACT_DB_RESULT_COLS, sizeof(SColumnInfoData));
10✔
684
  TSDB_CHECK_NULL(pBlock->pDataBlock, code, line, END, terrno);
10!
685
  SColumnInfoData infoData = {0};
10✔
686
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
10✔
687
  infoData.info.bytes = COMPACT_DB_RESULT_FIELD1_LEN;
10✔
688
  TSDB_CHECK_NULL(taosArrayPush(pBlock->pDataBlock, &infoData), code, line, END, terrno);
20!
689

690
  infoData.info.type = TSDB_DATA_TYPE_INT;
10✔
691
  infoData.info.bytes = tDataTypes[TSDB_DATA_TYPE_INT].bytes;
10✔
692
  TSDB_CHECK_NULL(taosArrayPush(pBlock->pDataBlock, &infoData), code, line, END, terrno);
20!
693

694
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
10✔
695
  infoData.info.bytes = COMPACT_DB_RESULT_FIELD3_LEN;
10✔
696
  TSDB_CHECK_NULL(taosArrayPush(pBlock->pDataBlock, &infoData), code, line, END, terrno);
20!
697

698
  code = blockDataEnsureCapacity(pBlock, 1);
10✔
699
  TSDB_CHECK_CODE(code, line, END);
10!
700

701
  SColumnInfoData* pResultCol = taosArrayGet(pBlock->pDataBlock, 0);
10✔
702
  TSDB_CHECK_NULL(pResultCol, code, line, END, terrno);
10!
703
  SColumnInfoData* pIdCol = taosArrayGet(pBlock->pDataBlock, 1);
10✔
704
  TSDB_CHECK_NULL(pIdCol, code, line, END, terrno);
10!
705
  SColumnInfoData* pReasonCol = taosArrayGet(pBlock->pDataBlock, 2);
10✔
706
  TSDB_CHECK_NULL(pReasonCol, code, line, END, terrno);
10!
707

708
  char result[COMPACT_DB_RESULT_FIELD1_LEN] = {0};
10✔
709
  char reason[COMPACT_DB_RESULT_FIELD3_LEN] = {0};
10✔
710
  if (pRsp->bAccepted) {
10!
711
    STR_TO_VARSTR(result, "accepted");
10✔
712
    code = colDataSetVal(pResultCol, 0, result, false);
10✔
713
    TSDB_CHECK_CODE(code, line, END);
10!
714
    code = colDataSetVal(pIdCol, 0, (void*)&pRsp->compactId, false);
10✔
715
    TSDB_CHECK_CODE(code, line, END);
10!
716
    STR_TO_VARSTR(reason, "success");
10✔
717
    code = colDataSetVal(pReasonCol, 0, reason, false);
10✔
718
    TSDB_CHECK_CODE(code, line, END);
10!
719
  } else {
720
    STR_TO_VARSTR(result, "rejected");
×
721
    code = colDataSetVal(pResultCol, 0, result, false);
×
722
    TSDB_CHECK_CODE(code, line, END);
×
723
    colDataSetNULL(pIdCol, 0);
724
    STR_TO_VARSTR(reason, "compaction is ongoing");
×
725
    code = colDataSetVal(pReasonCol, 0, reason, false);
×
726
    TSDB_CHECK_CODE(code, line, END);
×
727
  }
728
  pBlock->info.rows = 1;
10✔
729

730
  *block = pBlock;
10✔
731

732
  return TSDB_CODE_SUCCESS;
10✔
733
END:
×
734
  taosMemoryFree(pBlock);
×
735
  taosArrayDestroy(pBlock->pDataBlock);
×
736
  return code;
×
737
}
738

739
static int32_t buildRetriveTableRspForCompactDb(SCompactDbRsp* pCompactDb, SRetrieveTableRsp** pRsp) {
10✔
740
  SSDataBlock* pBlock = NULL;
10✔
741
  int32_t      code = buildCompactDbBlock(pCompactDb, &pBlock);
10✔
742
  if (code) {
10!
743
    return code;
×
744
  }
745

746
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
10✔
747
  size_t rspSize = sizeof(SRetrieveTableRsp) + dataEncodeBufSize + PAYLOAD_PREFIX_LEN;
10✔
748
  *pRsp = taosMemoryCalloc(1, rspSize);
10✔
749
  if (NULL == *pRsp) {
10!
750
    code = terrno;
×
751
    goto _exit;
×
752
  }
753

754
  (*pRsp)->useconds = 0;
10✔
755
  (*pRsp)->completed = 1;
10✔
756
  (*pRsp)->precision = 0;
10✔
757
  (*pRsp)->compressed = 0;
10✔
758
  (*pRsp)->compLen = 0;
10✔
759
  (*pRsp)->payloadLen = 0;
10✔
760
  (*pRsp)->numOfRows = htobe64((int64_t)pBlock->info.rows);
10✔
761
  (*pRsp)->numOfCols = htonl(COMPACT_DB_RESULT_COLS);
10✔
762

763
  int32_t len = blockEncode(pBlock, (*pRsp)->data + PAYLOAD_PREFIX_LEN, dataEncodeBufSize, COMPACT_DB_RESULT_COLS);
10✔
764
  if(len < 0) {
10!
765
    uError("buildRetriveTableRspForCompactDb error, len:%d", len);
×
766
    code = terrno;
×
767
    goto _exit;
×
768
  }
769
  blockDataDestroy(pBlock);
10✔
770

771
  SET_PAYLOAD_LEN((*pRsp)->data, len, len);
10✔
772

773
  int32_t payloadLen = len + PAYLOAD_PREFIX_LEN;
10✔
774
  (*pRsp)->payloadLen = htonl(payloadLen);
10✔
775
  (*pRsp)->compLen = htonl(payloadLen);
10✔
776

777
  if (payloadLen != rspSize - sizeof(SRetrieveTableRsp)) {
10!
778
    uError("buildRetriveTableRspForCompactDb error, len:%d != rspSize - sizeof(SRetrieveTableRsp):%" PRIu64, len,
×
779
           (uint64_t)(rspSize - sizeof(SRetrieveTableRsp)));
780
    code = TSDB_CODE_TSC_INVALID_INPUT;
×
781
    goto _exit;
×
782
  }
783

784
  return TSDB_CODE_SUCCESS;
10✔
785
_exit:
×
786
  if(*pRsp)  {
×
787
    taosMemoryFree(*pRsp);
×
788
    *pRsp = NULL;
×
789
  }
790
  if(pBlock) {
×
791
    blockDataDestroy(pBlock);
×
792
    pBlock = NULL;
×
793
  }
794
  return code;
×
795
}
796

797

798
int32_t processCompactDbRsp(void* param, SDataBuf* pMsg, int32_t code) {
10✔
799
  SRequestObj* pRequest = param;
10✔
800
  if (code != TSDB_CODE_SUCCESS) {
10!
801
    setErrno(pRequest, code);
×
802
  } else {
803
    SCompactDbRsp  rsp = {0};
10✔
804
    SRetrieveTableRsp* pRes = NULL;
10✔
805
    code = tDeserializeSCompactDbRsp(pMsg->pData, pMsg->len, &rsp);
10✔
806
    if (TSDB_CODE_SUCCESS == code) {
10!
807
      code = buildRetriveTableRspForCompactDb(&rsp, &pRes);
10✔
808
    }
809
    if (TSDB_CODE_SUCCESS == code) {
10!
810
      code = setQueryResultFromRsp(&pRequest->body.resInfo, pRes, false);
10✔
811
    }
812

813
    if (code != 0) {
10!
814
      taosMemoryFree(pRes);
×
815
    }
816
  }
817

818
  taosMemoryFree(pMsg->pData);
10✔
819
  taosMemoryFree(pMsg->pEpSet);
10✔
820

821
  if (pRequest->body.queryFp != NULL) {
10!
822
    pRequest->body.queryFp(((SSyncQueryParam *)pRequest->body.interParam)->userParam, pRequest, code);
10✔
823
  } else {
824
    if (tsem_post(&pRequest->body.rspSem) != 0){
×
825
      tscError("failed to post semaphore");
×
826
    }
827
  }
828
  return code;  
10✔
829
}
830

831
__async_send_cb_fn_t getMsgRspHandle(int32_t msgType) {
46,412✔
832
  switch (msgType) {
46,412✔
833
    case TDMT_MND_CONNECT:
9,045✔
834
      return processConnectRsp;
9,045✔
835
    case TDMT_MND_CREATE_DB:
4,947✔
836
      return processCreateDbRsp;
4,947✔
837
    case TDMT_MND_USE_DB:
8,571✔
838
      return processUseDbRsp;
8,571✔
839
    case TDMT_MND_CREATE_STB:
7,184✔
840
      return processCreateSTableRsp;
7,184✔
841
    case TDMT_MND_DROP_DB:
3,433✔
842
      return processDropDbRsp;
3,433✔
843
    case TDMT_MND_ALTER_STB:
4,011✔
844
      return processAlterStbRsp;
4,011✔
845
    case TDMT_MND_SHOW_VARIABLES:
11✔
846
      return processShowVariablesRsp;
11✔
847
    case TDMT_MND_COMPACT_DB:
10✔
848
      return processCompactDbRsp;  
10✔
849
    default:
9,200✔
850
      return genericRspCallback;
9,200✔
851
  }
852
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc