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

taosdata / TDengine / #5049

11 May 2026 06:30AM UTC coverage: 73.313% (+0.09%) from 73.222%
#5049

push

travis-ci

web-flow
feat: refactor taosdump code to improve backup speed and compression ratio (#35292)

6625 of 8435 new or added lines in 28 files covered. (78.54%)

2491 existing lines in 142 files now uncovered.

281233 of 383605 relevant lines covered (73.31%)

132489999.79 hits per line

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

76.95
/source/dnode/mnode/impl/src/mndProfile.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
#define _DEFAULT_SOURCE
17
#include "mndProfile.h"
18
#include "audit.h"
19
#include "crypt.h"
20
#include "mndCluster.h"
21
#include "mndDb.h"
22
#include "mndDnode.h"
23
#include "mndMnode.h"
24
#include "mndPrivilege.h"
25
#include "mndQnode.h"
26
#include "mndSecurityPolicy.h"
27
#include "mndShow.h"
28
#include "mndSma.h"
29
#include "mndStb.h"
30
#include "mndToken.h"
31
#include "mndUser.h"
32
#include "mndView.h"
33
#include "tglobal.h"
34
#include "totp.h"
35
#include "tversion.h"
36

37
typedef struct {
38
  uint32_t id;
39
  int8_t   connType;
40
  int8_t   killed;
41
  char     user[TSDB_USER_LEN];
42
  char     tokenName[TSDB_TOKEN_LEN];
43
  char     app[TSDB_APP_NAME_LEN];  // app name that invokes taosc
44
  int64_t  appStartTimeMs;          // app start time
45
  int32_t  pid;                     // pid of app that invokes taosc
46
  int32_t  numOfQueries;
47
  int64_t  loginTimeMs;
48
  int64_t  lastAccessTimeMs;
49
  uint64_t killId;
50
  SArray  *pQueries;  // SArray<SQueryDesc>
51
  char     userApp[TSDB_APP_NAME_LEN];
52
  SRWLatch queryLock;
53
  uint32_t userIp;
54
  SIpAddr  userDualIp;
55
  SIpAddr  addr;
56
  char     sVer[TSDB_VERSION_LEN];
57
  char     cInfo[CONNECTOR_INFO_LEN];
58
} SConnObj;
59

60
typedef struct {
61
  int64_t            appId;
62
  SIpAddr            cliAddr;
63
  int32_t            pid;
64
  char               name[TSDB_APP_NAME_LEN];
65
  int64_t            startTime;
66
  SAppClusterSummary summary;
67
  int64_t            lastAccessTimeMs;
68
} SAppObj;
69

70
typedef struct {
71
  int32_t totalDnodes;
72
  int32_t onlineDnodes;
73
  SEpSet  epSet;
74
  SArray *pQnodeList;
75
  int64_t ipWhiteListVer;
76
} SConnPreparedObj;
77

78
#define CACHE_OBJ_KEEP_TIME 3  // s
79

80
static void      mndFreeConn(SConnObj *pConn);
81
static SConnObj *mndAcquireConn(SMnode *pMnode, uint32_t connId);
82
static void      mndReleaseConn(SMnode *pMnode, SConnObj *pConn, bool extendLifespan);
83
static void     *mndGetNextConn(SMnode *pMnode, SCacheIter *pIter);
84
static void      mndCancelGetNextConn(SMnode *pMnode, void *pIter);
85
static int32_t   mndProcessHeartBeatReq(SRpcMsg *pReq);
86
static int32_t   mndProcessConnectReq(SRpcMsg *pReq);
87
static int32_t   mndProcessKillQueryReq(SRpcMsg *pReq);
88
static int32_t   mndProcessKillConnReq(SRpcMsg *pReq);
89
static int32_t   mndRetrieveConns(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
90
static int32_t   mndRetrieveQueries(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
91
static void      mndCancelGetNextQuery(SMnode *pMnode, void *pIter);
92
static void      mndFreeApp(SAppObj *pApp);
93
static int32_t   mndRetrieveApps(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
94
static void      mndCancelGetNextApp(SMnode *pMnode, void *pIter);
95
static int32_t   mndProcessSvrVerReq(SRpcMsg *pReq);
96

97
int32_t mndInitProfile(SMnode *pMnode) {
543,781✔
98
  int32_t       code = 0;
543,781✔
99
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
543,781✔
100

101
  // in ms
102
  int32_t checkTime = CACHE_OBJ_KEEP_TIME * 1000;
543,781✔
103
  pMgmt->connCache = taosCacheInit(TSDB_DATA_TYPE_UINT, checkTime, false, (__cache_free_fn_t)mndFreeConn, "conn");
543,781✔
104
  if (pMgmt->connCache == NULL) {
543,781✔
105
    code = TSDB_CODE_OUT_OF_MEMORY;
×
106
    mError("failed to alloc profile cache since %s", terrstr());
×
107
    TAOS_RETURN(code);
×
108
  }
109

110
  pMgmt->appCache = taosCacheInit(TSDB_DATA_TYPE_BIGINT, checkTime, true, (__cache_free_fn_t)mndFreeApp, "app");
543,781✔
111
  if (pMgmt->appCache == NULL) {
543,781✔
112
    code = TSDB_CODE_OUT_OF_MEMORY;
×
113
    mError("failed to alloc profile cache since %s", terrstr());
×
114
    TAOS_RETURN(code);
×
115
  }
116

117
  mndSetMsgHandle(pMnode, TDMT_MND_HEARTBEAT, mndProcessHeartBeatReq);
543,781✔
118
  mndSetMsgHandle(pMnode, TDMT_MND_CONNECT, mndProcessConnectReq);
543,781✔
119
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_QUERY, mndProcessKillQueryReq);
543,781✔
120
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_CONN, mndProcessKillConnReq);
543,781✔
121
  mndSetMsgHandle(pMnode, TDMT_MND_SERVER_VERSION, mndProcessSvrVerReq);
543,781✔
122

123
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONNS, mndRetrieveConns);
543,781✔
124
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONNS, mndCancelGetNextConn);
543,781✔
125
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_QUERIES, mndRetrieveQueries);
543,781✔
126
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_QUERIES, mndCancelGetNextQuery);
543,781✔
127
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_APPS, mndRetrieveApps);
543,781✔
128
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_APPS, mndCancelGetNextApp);
543,781✔
129

130
  TAOS_RETURN(code);
543,781✔
131
}
132

133
void mndCleanupProfile(SMnode *pMnode) {
543,711✔
134
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
543,711✔
135
  if (pMgmt->connCache != NULL) {
543,711✔
136
    taosCacheCleanup(pMgmt->connCache);
543,711✔
137
    pMgmt->connCache = NULL;
543,711✔
138
  }
139

140
  if (pMgmt->appCache != NULL) {
543,711✔
141
    taosCacheCleanup(pMgmt->appCache);
543,711✔
142
    pMgmt->appCache = NULL;
543,711✔
143
  }
144
}
543,711✔
145

146
static void getUserIpFromConnObj(SConnObj *pConn, char *dst) {
59,284✔
147
  static char *none = "0.0.0.0";
148
  if (pConn->userIp != 0 && pConn->userIp != INADDR_NONE) {
59,284✔
149
    taosInetNtoa(varDataVal(dst), pConn->userIp);
×
150
    varDataLen(dst) = strlen(varDataVal(dst));
×
151
  }
152

153
  if (pConn->userDualIp.ipv4[0] != 0 && strncmp(pConn->userDualIp.ipv4, none, strlen(none)) != 0) {
59,284✔
154
    char   *ipstr = IP_ADDR_STR(&pConn->userDualIp);
×
155
    int32_t len = strlen(ipstr);
×
156
    memcpy(varDataVal(dst), ipstr, len);
×
157
    varDataLen(dst) = len;
×
158
  }
159
  return;
59,284✔
160
}
161
static void setUserInfo2Conn(SConnObj *connObj, char *userApp, uint32_t userIp, char *cInfo) {
38,731,565✔
162
  if (connObj == NULL) {
38,731,565✔
163
    return;
×
164
  }
165
  tstrncpy(connObj->userApp, userApp, sizeof(connObj->userApp));
38,731,565✔
166
  tstrncpy(connObj->cInfo, cInfo, sizeof(connObj->cInfo));
38,717,349✔
167
  connObj->userIp = userIp;
38,718,320✔
168
}
169
static void setUserInfoIpToConn(SConnObj *connObj, SIpRange *pRange) {
38,731,897✔
170
  int32_t code = 0;
38,731,897✔
171
  if (connObj == NULL) {
38,731,897✔
172
    return;
×
173
  }
174

175
  code = tIpUintToStr(pRange, &connObj->userDualIp);
38,731,897✔
176
  if (code != 0) {
38,723,616✔
177
    mError("conn:%u, failed to set user ip to conn since %s", connObj->id, tstrerror(code));
×
178
    return;
×
179
  }
180
}
181

182

183

184
static SConnObj *mndCreateConn(SMnode *pMnode, const char *user, const char* tokenName, int8_t connType, SIpAddr *pAddr,
3,954,345✔
185
                               int32_t pid, const char *app, int64_t startTime, const char *sVer) {
186
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
3,954,345✔
187

188
  char     connStr[255] = {0};
3,954,345✔
189
  char    *ip = IP_ADDR_STR(pAddr);
3,954,345✔
190
  uint16_t port = pAddr->port;
3,954,144✔
191

192
  int32_t  len = tsnprintf(connStr, sizeof(connStr), "%s%d%d%d%s", user, ip, port, pid, app);
3,954,345✔
193
  uint32_t connId = mndGenerateUid(connStr, len);
3,954,296✔
194
  if (startTime == 0) startTime = taosGetTimestampMs();
4,682,501✔
195

196
  SConnObj connObj = {
3,954,345✔
197
      .id = connId,
198
      .connType = connType,
199
      .appStartTimeMs = startTime,
200
      .pid = pid,
201
      .addr = *pAddr,
202
      .killed = 0,
203
      .loginTimeMs = taosGetTimestampMs(),
3,953,482✔
204
      .lastAccessTimeMs = 0,
205
      .killId = 0,
206
      .numOfQueries = 0,
207
      .pQueries = NULL,
208
  };
209

210
  connObj.lastAccessTimeMs = connObj.loginTimeMs;
3,954,254✔
211
  tstrncpy(connObj.user, user, sizeof(connObj.user));
3,954,254✔
212
  tstrncpy(connObj.tokenName, tokenName, sizeof(connObj.tokenName));
3,952,607✔
213
  tstrncpy(connObj.app, app, sizeof(connObj.app));
3,954,345✔
214
  tstrncpy(connObj.sVer, sVer, sizeof(connObj.sVer));
3,954,296✔
215

216
  SConnObj *pConn =
217
      taosCachePut(pMgmt->connCache, &connId, sizeof(uint32_t), &connObj, sizeof(connObj), CACHE_OBJ_KEEP_TIME * 1000);
3,953,318✔
218
  if (pConn == NULL) {
3,953,248✔
219
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
220
    mError("conn:%d, failed to put into cache since %s, user:%s", connId, user, terrstr());
×
221
    return NULL;
×
222
  } else {
223
    mTrace("conn:%u, is created, data:%p user:%s", pConn->id, pConn, user);
3,953,248✔
224
    return pConn;
3,953,945✔
225
  }
226
}
227

228

229

230
static void mndFreeConn(SConnObj *pConn) {
3,954,345✔
231
  taosWLockLatch(&pConn->queryLock);
3,954,345✔
232
  taosArrayDestroyEx(pConn->pQueries, tFreeClientHbQueryDesc);
3,954,345✔
233
  taosWUnLockLatch(&pConn->queryLock);
3,954,345✔
234

235
  mTrace("conn:%u, is destroyed, data:%p", pConn->id, pConn);
3,954,345✔
236
}
3,954,345✔
237

238
static SConnObj *mndAcquireConn(SMnode *pMnode, uint32_t connId) {
38,732,169✔
239
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
38,732,169✔
240

241
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &connId, sizeof(connId));
38,731,784✔
242
  if (pConn == NULL) {
38,737,830✔
243
    mDebug("conn:%u, already destroyed", connId);
727,629✔
244
    return NULL;
727,760✔
245
  }
246

247
  pConn->lastAccessTimeMs = taosGetTimestampMs();
38,005,076✔
248
  mTrace("conn:%u, acquired from cache, data:%p", pConn->id, pConn);
38,004,696✔
249
  return pConn;
37,992,219✔
250
}
251

252
static void mndReleaseConn(SMnode *pMnode, SConnObj *pConn, bool extendLifespan) {
46,877,130✔
253
  if (pConn == NULL) return;
46,877,130✔
254
  mTrace("conn:%u, released from cache, data:%p", pConn->id, pConn);
46,858,551✔
255

256
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
46,858,551✔
257
  if (extendLifespan) taosCacheTryExtendLifeSpan(pMgmt->connCache, (void **)&pConn);
46,856,813✔
258
  taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
46,855,769✔
259
}
260

261
void *mndGetNextConn(SMnode *pMnode, SCacheIter *pIter) {
5,541,726✔
262
  SConnObj *pConn = NULL;
5,541,726✔
263
  bool      hasNext = taosCacheIterNext(pIter);
5,541,726✔
264
  if (hasNext) {
5,541,726✔
265
    size_t dataLen = 0;
5,437,800✔
266
    pConn = taosCacheIterGetData(pIter, &dataLen);
5,437,800✔
267
  } else {
268
    taosCacheDestroyIter(pIter);
103,926✔
269
  }
270

271
  return pConn;
5,541,726✔
272
}
273

274
static void mndCancelGetNextConn(SMnode *pMnode, void *pIter) {
×
275
  if (pIter != NULL) {
×
276
    taosCacheDestroyIter(pIter);
×
277
  }
278
}
×
279

280

281

282
// TODO: if there are many connections, this function may be slow
283
int32_t mndCountUserConns(SMnode *pMnode, const char *user) {
64,939✔
284
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
64,939✔
285
  SCacheIter   *pIter = taosCacheCreateIter(pMgmt->connCache);
64,939✔
286
  if (pIter == NULL) {
64,939✔
287
    mError("failed to create conn cache iterator");
×
288
    return -1;
×
289
  }
290

291
  int32_t    count = 0;
64,939✔
292
  SConnObj  *pConn = NULL;
64,939✔
293
  while ((pConn = mndGetNextConn(pMnode, pIter)) != NULL) {
4,958,747✔
294
    if (strncmp(pConn->user, user, TSDB_USER_LEN) == 0) {
4,893,808✔
295
      count++;
1,187,962✔
296
    }
297
    mndReleaseConn(pMnode, pConn, true);
4,893,808✔
298
  }
299

300
  return count;
64,939✔
301
}
302

303

304

305
static int32_t verifyPassword(SUserObj* pUser, const char* inputPass) {
3,237,168✔
306
  int32_t code = 0;
3,237,168✔
307

308
  const char* currPass = pUser->passwords[0].pass;
3,237,168✔
309
  char pass[TSDB_PASSWORD_LEN] = {0};
3,237,005✔
310
  (void)memcpy(pass, inputPass, TSDB_PASSWORD_LEN);
3,237,054✔
311
  pass[TSDB_PASSWORD_LEN - 1] = 0;
3,235,987✔
312

313
  if (pUser->passEncryptAlgorithm != 0 && strlen(tsDataKey) > 0) {
3,235,987✔
314
    code = mndEncryptPass(pass, pUser->salt, NULL);
2,945✔
315
    if (code != TSDB_CODE_SUCCESS) {
2,680✔
316
      return code;
×
317
    }
318
  }
319

320
  // constant time comparison to prevent timing attack
321
  volatile uint8_t res = 0;
3,234,776✔
322
  for (size_t i = 0; i < sizeof(pass) - 1; i++) {
103,476,076✔
323
    res |= pass[i] ^ currPass[i];
100,239,619✔
324
  }
325

326
 return (res == 0) ? TSDB_CODE_SUCCESS: TSDB_CODE_MND_AUTH_FAILURE;
3,236,457✔
327
}
328

329

330

331
static bool verifyTotp(SUserObj *pUser, int32_t totpCode) {
3,228,614✔
332
  if (!mndIsTotpEnabledUser(pUser)) {
3,228,614✔
333
    return true;
3,222,979✔
334
  }
335
  return taosVerifyTotpCode(pUser->totpsecret, sizeof(pUser->totpsecret), totpCode, 6, 1) != 0;
5,311✔
336
}
337

338

339

340
static int32_t mndProcessConnectReq(SRpcMsg *pReq) {
3,245,115✔
341
  int32_t          code = 0, lino = 0;
3,245,115✔
342

343
  SMnode          *pMnode = pReq->info.node;
3,245,164✔
344
  SConnectReq      connReq = {0};
3,245,164✔
345
  SUserObj        *pUser = NULL;
3,245,098✔
346
  SDbObj          *pDb = NULL;
3,245,164✔
347
  SConnObj        *pConn = NULL;
3,245,164✔
348
  const STraceId  *trace = &pReq->info.traceId;
3,245,164✔
349
  char            *ip = IP_ADDR_STR(&pReq->info.conn.cliAddr);
3,245,032✔
350
  uint16_t         port = pReq->info.conn.cliAddr.port;
3,245,098✔
351
  SCachedTokenInfo ti = {0};
3,245,164✔
352
  const char      *user = RPC_MSG_USER(pReq);
3,245,164✔
353
  const char      *token = RPC_MSG_TOKEN(pReq);
3,244,983✔
354
  int64_t          tss = taosGetTimestampMs();
3,244,983✔
355
  int64_t          now = tss / 1000;
3,244,983✔
356

357
  if (token != NULL && mndGetCachedTokenInfo(token, &ti) == NULL) {
3,244,983✔
358
    TAOS_CHECK_GOTO(TSDB_CODE_MND_TOKEN_NOT_EXIST, &lino, _OVER);
×
359
  }
360
  TAOS_CHECK_GOTO(tDeserializeSConnectReq(pReq->pCont, pReq->contLen, &connReq), &lino, _OVER);
3,244,983✔
361
  TAOS_CHECK_GOTO(taosCheckVersionCompatibleFromStr(connReq.sVer, td_version, 3), &lino, _OVER);
3,244,816✔
362
  TAOS_CHECK_GOTO(tVerifyConnectReqSignature(&connReq), &lino, _OVER);
3,244,969✔
363
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, user, &pUser), &lino, _OVER);
3,245,000✔
364

365
  SLoginInfo li = {0};
3,243,596✔
366
  mndGetUserLoginInfo(user, &li);
3,243,596✔
367
  TAOS_CHECK_GOTO(mndCheckConnectPrivilege(pMnode, pUser, token, &li), &lino, _OVER);
3,241,878✔
368

369
  if (token != NULL || tsMndSkipGrant) {
3,235,785✔
370
    li.lastLoginTime= now;
687✔
371
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
687✔
372
      mndSetUserLoginInfo(user, &li);
1,800✔
373
    }
374
  } else if ((code = verifyPassword(pUser, connReq.passwd)) == TSDB_CODE_MND_AUTH_FAILURE) {
3,236,065✔
375
    if (pUser->failedLoginAttempts >= 0) {
8,441✔
376
      if (li.failedLoginCount >= pUser->failedLoginAttempts) {
7,037✔
377
        // if we can get here, it means the lock time has passed, so reset the counter
378
        li.failedLoginCount = 0;
×
379
      }
380
      li.failedLoginCount++;
7,037✔
381
      li.lastFailedLoginTime = now;
7,037✔
382
    }
383
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
8,277✔
384
      mndSetUserLoginInfo(user, &li);
8,277✔
385
    }
386
    TAOS_CHECK_GOTO(code, &lino, _OVER);
8,277✔
387
  } else if (code != TSDB_CODE_SUCCESS) {
3,227,967✔
388
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
389
  } else if (!verifyTotp(pUser, connReq.totpCode)) {
3,227,967✔
390
    TAOS_CHECK_GOTO(TSDB_CODE_MND_WRONG_TOTP_CODE, &lino, _OVER);
3,587✔
391
  } else {
392
    li.failedLoginCount = 0;
3,224,703✔
393
    li.lastLoginTime= now;
3,224,703✔
394
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
3,224,703✔
395
      mndSetUserLoginInfo(user, &li);
3,224,766✔
396
    }
397
  } 
398

399
  if (connReq.db[0] != 0) {
3,227,156✔
400
    char db[TSDB_DB_FNAME_LEN] = {0};
448,194✔
401
    (void)snprintf(db, TSDB_DB_FNAME_LEN, "%d%s%s", pUser->acctId, TS_PATH_DELIMITER, connReq.db);
448,194✔
402
    pDb = mndAcquireDb(pMnode, db);
448,194✔
403
    if (pDb == NULL) {
448,145✔
404
      if (0 != strcmp(connReq.db, TSDB_INFORMATION_SCHEMA_DB) && (0 != strcmp(connReq.db, TSDB_PERFORMANCE_SCHEMA_DB))) {
1,313✔
405
        TAOS_CHECK_GOTO(TSDB_CODE_MND_DB_NOT_EXIST, &lino, _OVER);
433✔
406
      }
407
    }
408

409
    TAOS_CHECK_GOTO(mndCheckDbPrivilege(pMnode, user,RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB, pDb), NULL, _OVER);
447,712✔
410
  }
411

412
  if (connReq.connType == CONN_TYPE__AUTH_TEST) {
3,226,585✔
413
    code = 0;
×
414
    goto _OVER;
×
415
  }
416

417
  pConn = mndCreateConn(pMnode, user, ti.name, connReq.connType, &pReq->info.conn.cliAddr, connReq.pid, connReq.app,
3,226,585✔
418
                        connReq.startTime, connReq.sVer);
419
  if (pConn == NULL) {
3,225,941✔
420
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
421
  }
422

423
  SConnectRsp connectRsp = {0};
3,225,941✔
424
  connectRsp.acctId = pUser->acctId;
3,226,054✔
425
  connectRsp.superUser = pUser->superUser;
3,224,573✔
426
  connectRsp.sysInfo = pUser->sysInfo;
3,225,491✔
427
  connectRsp.minSecLevel = pUser->minSecLevel;
3,226,062✔
428
  connectRsp.maxSecLevel = pUser->maxSecLevel;
3,225,898✔
429
  connectRsp.sodInitial = (pMnode->sodPhase == TSDB_SOD_PHASE_INITIAL ? 1 : 0);
3,224,608✔
430
  connectRsp.macActive = (pMnode->macActive == MAC_MODE_MANDATORY ? 1 : 0);
3,226,103✔
431
  connectRsp.clusterId = pMnode->clusterId;
3,225,933✔
432
  connectRsp.connId = pConn->id;
3,225,208✔
433
  connectRsp.connType = connReq.connType;
3,226,010✔
434
  connectRsp.dnodeNum = mndGetDnodeSize(pMnode);
3,226,010✔
435
  connectRsp.svrTimestamp = taosGetTimestampSec();
3,226,585✔
436
  connectRsp.passVer = pUser->passVersion;
3,226,585✔
437
  connectRsp.authVer = pUser->authVersion;
3,226,585✔
438
  connectRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
3,225,646✔
439
  connectRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
3,225,646✔
440
  connectRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
3,225,646✔
441
  connectRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
3,225,646✔
442
  connectRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
3,225,646✔
443
  connectRsp.enableAuditDelete = tsEnableAuditDelete;
3,225,646✔
444
  connectRsp.enableAuditSelect = tsEnableAuditSelect;
3,225,646✔
445
  connectRsp.enableAuditInsert = tsEnableAuditInsert;
3,225,646✔
446
  connectRsp.auditLevel = tsAuditLevel;
3,225,646✔
447
  tstrncpy(connectRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
3,225,646✔
448
  connectRsp.whiteListVer = pUser->ipWhiteListVer;
3,226,428✔
449
  connectRsp.timeWhiteListVer = pUser->timeWhiteListVer;
3,226,448✔
450
  connectRsp.userId = pUser->uid;
3,224,707✔
451

452

453
  tstrncpy(connectRsp.sVer, td_version, sizeof(connectRsp.sVer));
3,226,120✔
454
  tstrncpy(connectRsp.user, user, sizeof(connectRsp.user));
3,225,881✔
455
  tstrncpy(connectRsp.tokenName, ti.name, sizeof(connectRsp.tokenName));
3,226,462✔
456
  (void)snprintf(connectRsp.sDetailVer, sizeof(connectRsp.sDetailVer), "ver:%s\nbuild:%s\ngitinfo:%s", td_version,
3,226,267✔
457
                 td_buildinfo, td_gitinfo);
458
  mndGetMnodeEpSet(pMnode, &connectRsp.epSet);
3,226,267✔
459

460
  int32_t contLen = tSerializeSConnectRsp(NULL, 0, &connectRsp);
3,226,585✔
461
  if (contLen < 0) {
3,223,143✔
462
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
463
  }
464
  void *pRsp = rpcMallocCont(contLen);
3,223,143✔
465
  if (pRsp == NULL) {
3,224,083✔
466
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
467
  }
468

469
  contLen = tSerializeSConnectRsp(pRsp, contLen, &connectRsp);
3,224,083✔
470
  if (contLen < 0) {
3,225,419✔
471
    rpcFreeCont(pRsp);
×
472
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
473
  }
474

475
  pReq->info.rspLen = contLen;
3,225,419✔
476
  pReq->info.rsp = pRsp;
3,223,415✔
477

478
  mGDebug("user:%s, login from %s:%d, conn:%u, app:%s, db:%s", user, ip, port, pConn->id, connReq.app, connReq.db);
3,224,003✔
479
  code = 0;
3,226,585✔
480

481
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
3,226,585✔
482
    char    detail[1000] = {0};
3,226,585✔
483
    int32_t nBytes = snprintf(detail, sizeof(detail), "app:%s", connReq.app);
3,226,585✔
484
    if ((uint32_t)nBytes < sizeof(detail)) {
3,226,585✔
485
      double duration = (taosGetTimestampMs() - tss) / 1000.0;
3,226,585✔
486
      auditRecord(pReq, pMnode->clusterId, "login", "", "", detail, strlen(detail), duration, 0);
3,226,585✔
487
    } else {
488
      mError("failed to audit logic since %s", tstrerror(TSDB_CODE_OUT_OF_RANGE));
×
489
    }
490
  }
491

492
_OVER:
3,242,744✔
493
  if (code != 0) {
3,245,164✔
494
    mGError("user:%s, failed to login from %s since %s, line:%d, db:%s", user, ip, tstrerror(code), lino, connReq.db);
18,579✔
495
  }
496

497
  mndReleaseUser(pMnode, pUser);
3,245,164✔
498
  mndReleaseDb(pMnode, pDb);
3,245,164✔
499
  mndReleaseConn(pMnode, pConn, true);
3,245,164✔
500

501
  TAOS_RETURN(code);
3,245,164✔
502
}
503

504

505

506
static int32_t mndSaveQueryList(SConnObj *pConn, SQueryHbReqBasic *pBasic) {
38,726,196✔
507
  taosWLockLatch(&pConn->queryLock);
38,726,196✔
508

509
  taosArrayDestroyEx(pConn->pQueries, tFreeClientHbQueryDesc);
38,733,761✔
510

511
  pConn->pQueries = pBasic->queryDesc;
38,734,765✔
512
  pConn->numOfQueries = pBasic->queryDesc ? taosArrayGetSize(pBasic->queryDesc) : 0;
38,734,235✔
513
  pBasic->queryDesc = NULL;
38,733,640✔
514

515
  mDebug("queries updated in conn %u, num:%d", pConn->id, pConn->numOfQueries);
38,735,275✔
516

517
  taosWUnLockLatch(&pConn->queryLock);
38,741,940✔
518

519
  return TSDB_CODE_SUCCESS;
38,736,067✔
520
}
521

522
static SAppObj *mndCreateApp(SMnode *pMnode, const SIpAddr *pAddr, const SAppHbReq *pReq) {
873,657✔
523
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
873,657✔
524

525
  SAppObj app;
873,469✔
526
  app.appId = pReq->appId;
873,657✔
527
  app.cliAddr = *pAddr;
873,657✔
528
  app.pid = pReq->pid;
873,657✔
529
  tstrncpy(app.name, pReq->name, sizeof(app.name));
873,657✔
530
  app.startTime = pReq->startTime;
873,657✔
531
  (void)memcpy(&app.summary, &pReq->summary, sizeof(pReq->summary));
873,657✔
532
  app.lastAccessTimeMs = taosGetTimestampMs();
873,657✔
533

534
  SAppObj *pApp =
535
      taosCachePut(pMgmt->appCache, &pReq->appId, sizeof(pReq->appId), &app, sizeof(app), CACHE_OBJ_KEEP_TIME * 1000);
873,657✔
536
  if (pApp == NULL) {
873,657✔
537
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
538
    mError("failed to app %" PRIx64 " into cache since %s", pReq->appId, terrstr());
×
539
    return NULL;
×
540
  }
541

542
  mTrace("app %" PRIx64 " is put into cache", pReq->appId);
873,657✔
543
  return pApp;
873,657✔
544
}
545

546
static void mndFreeApp(SAppObj *pApp) { mTrace("app %" PRIx64 " is destroyed", pApp->appId); }
873,657✔
547

548
static SAppObj *mndAcquireApp(SMnode *pMnode, int64_t appId) {
38,732,168✔
549
  terrno = 0;
38,732,168✔
550
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
38,732,593✔
551

552
  SAppObj *pApp = taosCacheAcquireByKey(pMgmt->appCache, &appId, sizeof(appId));
38,730,803✔
553
  if (pApp == NULL) {
38,730,593✔
554
    mDebug("app %" PRIx64 " not in cache", appId);
873,657✔
555
    return NULL;
873,657✔
556
  }
557

558
  pApp->lastAccessTimeMs = (uint64_t)taosGetTimestampMs();
37,858,095✔
559

560
  mTrace("app %" PRIx64 " acquired from cache", appId);
37,853,421✔
561
  return pApp;
37,842,333✔
562
}
563

564
static void mndReleaseApp(SMnode *pMnode, SAppObj *pApp) {
38,724,080✔
565
  if (pApp == NULL) return;
38,724,080✔
566
  mTrace("release app %" PRIx64 " to cache", pApp->appId);
38,724,080✔
567

568
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
38,724,080✔
569
  taosCacheRelease(pMgmt->appCache, (void **)&pApp, false);
38,728,502✔
570
}
571

572
SAppObj *mndGetNextApp(SMnode *pMnode, SCacheIter *pIter) {
7,884✔
573
  SAppObj *pApp = NULL;
7,884✔
574
  bool     hasNext = taosCacheIterNext(pIter);
7,884✔
575
  if (hasNext) {
7,884✔
576
    size_t dataLen = 0;
3,942✔
577
    pApp = taosCacheIterGetData(pIter, &dataLen);
3,942✔
578
  } else {
579
    taosCacheDestroyIter(pIter);
3,942✔
580
  }
581

582
  return pApp;
7,884✔
583
}
584

585
static void mndCancelGetNextApp(SMnode *pMnode, void *pIter) {
×
586
  if (pIter != NULL) {
×
587
    taosCacheDestroyIter(pIter);
×
588
  }
589
}
×
590

591
static SClientHbRsp *mndMqHbBuildRsp(SMnode *pMnode, SClientHbReq *pReq) {
×
592
  //
593
  return NULL;
×
594
}
595

596
static int32_t mndUpdateAppInfo(SMnode *pMnode, SClientHbReq *pHbReq, const SRpcConnInfo *connInfo) {
38,729,995✔
597
  int32_t    code = 0;
38,729,995✔
598
  SAppHbReq *pReq = &pHbReq->app;
38,729,995✔
599
  SAppObj   *pApp = mndAcquireApp(pMnode, pReq->appId);
38,731,500✔
600
  if (pApp == NULL) {
38,721,296✔
601
    pApp = mndCreateApp(pMnode, &connInfo->cliAddr, pReq);
873,657✔
602
    if (pApp == NULL) {
873,657✔
603
      mError("failed to create new app %" PRIx64 " since %s", pReq->appId, terrstr());
×
604
      code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
605
      if (terrno != 0) code = terrno;
×
606
      TAOS_RETURN(code);
×
607
    } else {
608
      mDebug("a new app %" PRIx64 " is created", pReq->appId);
873,657✔
609
      mndReleaseApp(pMnode, pApp);
873,657✔
610
      return TSDB_CODE_SUCCESS;
873,657✔
611
    }
612
  }
613

614
  (void)memcpy(&pApp->summary, &pReq->summary, sizeof(pReq->summary));
37,847,639✔
615

616
  mndReleaseApp(pMnode, pApp);
37,855,436✔
617

618
  return TSDB_CODE_SUCCESS;
37,843,732✔
619
}
620

621
static int32_t mndGetOnlineDnodeNum(SMnode *pMnode, int32_t *num) {
31,907,583✔
622
  SSdb      *pSdb = pMnode->pSdb;
31,907,583✔
623
  SDnodeObj *pDnode = NULL;
31,909,787✔
624
  int64_t    curMs = taosGetTimestampMs();
31,910,037✔
625
  void      *pIter = NULL;
31,910,037✔
626

627
  while (true) {
51,437,451✔
628
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
83,347,488✔
629
    if (pIter == NULL) break;
83,347,645✔
630

631
    bool online = mndIsDnodeOnline(pDnode, curMs);
51,434,959✔
632
    if (online) {
51,428,062✔
633
      (*num)++;
49,933,117✔
634
    }
635

636
    sdbRelease(pSdb, pDnode);
51,423,442✔
637
  }
638

639
  return TSDB_CODE_SUCCESS;
31,912,686✔
640
}
641

642
static int32_t mndProcessQueryHeartBeat(SMnode *pMnode, SRpcMsg *pMsg, SClientHbReq *pHbReq,
38,734,584✔
643
                                        SClientHbBatchRsp *pBatchRsp, SConnPreparedObj *pObj) {
644
  int32_t       code = 0;
38,734,584✔
645
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
38,734,584✔
646
  SClientHbRsp  hbRsp = {.connKey = pHbReq->connKey, .status = 0, .info = NULL, .query = NULL};
38,734,257✔
647

648
  if (0 != pHbReq->app.appId) {
38,732,624✔
649
    TAOS_CHECK_RETURN(mndUpdateAppInfo(pMnode, pHbReq, &pMsg->info.conn));
38,721,325✔
650
  }
651

652
  if (pHbReq->query) {
38,720,840✔
653
    SQueryHbReqBasic *pBasic = pHbReq->query;
38,727,645✔
654
    SConnObj *pConn = mndAcquireConn(pMnode, pBasic->connId);
38,733,637✔
655
    if (pConn == NULL) {
38,719,954✔
656
      SRpcConnInfo  connInfo = pMsg->info.conn;
727,760✔
657
      const char* user = pHbReq->user;
727,760✔
658
      pConn = mndCreateConn(pMnode, user, pHbReq->tokenName, pHbReq->connKey.connType, &connInfo.cliAddr, pHbReq->app.pid,
727,669✔
659
                            pHbReq->app.name, 0, pHbReq->sVer);
727,760✔
660
      if (pConn == NULL) {
727,559✔
661
        mError("user:%s, conn:%u is freed and failed to create new since %s", user, pBasic->connId, terrstr());
×
662
        code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
663
        if (terrno != 0) code = terrno;
×
664
        TAOS_RETURN(code);
×
665
      } else {
666
        mDebug("user:%s, conn:%u is freed, will create a new conn:%u", user, pBasic->connId, pConn->id);
727,559✔
667
      }
668
    }
669

670
    setUserInfo2Conn(pConn, pHbReq->userApp, pHbReq->userIp, pHbReq->cInfo);
38,719,954✔
671
    setUserInfoIpToConn(pConn, &pHbReq->userDualIp);
38,731,002✔
672

673
    SQueryHbRspBasic *rspBasic = taosMemoryCalloc(1, sizeof(SQueryHbRspBasic));
38,725,164✔
674
    if (rspBasic == NULL) {
38,727,007✔
675
      mndReleaseConn(pMnode, pConn, true);
×
676
      code = terrno;
×
677
      mError("user:%s, conn:%u failed to process hb while since %s", pConn->user, pBasic->connId, terrstr());
×
678
      TAOS_RETURN(code);
×
679
    }
680

681
    TAOS_CHECK_RETURN(mndSaveQueryList(pConn, pBasic));
38,727,007✔
682
    if (pConn->killed != 0) {
38,735,481✔
683
      rspBasic->killConnection = 1;
×
684
    }
685

686
    if (pConn->killId != 0) {
38,736,899✔
687
      rspBasic->killRid = pConn->killId;
1,476✔
688
      pConn->killId = 0;
1,476✔
689
    }
690

691
    rspBasic->connId = pConn->id;
38,736,899✔
692
    rspBasic->connId = pConn->id;
38,735,461✔
693
    rspBasic->totalDnodes = pObj->totalDnodes;
38,735,321✔
694
    rspBasic->onlineDnodes = pObj->onlineDnodes;
38,734,218✔
695
    rspBasic->epSet = pObj->epSet;
38,735,175✔
696
    rspBasic->pQnodeList = taosArrayDup(pObj->pQnodeList, NULL);
38,734,729✔
697

698
    mndReleaseConn(pMnode, pConn, true);
38,736,327✔
699

700
    hbRsp.query = rspBasic;
38,736,786✔
701
  } else {
702
    mDebug("no query info in hb msg");
198✔
703
  }
704

705
  int32_t kvNum = taosHashGetSize(pHbReq->info);
38,736,984✔
706
  if (NULL == pHbReq->info || kvNum <= 0) {
38,736,784✔
707
    if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
19,406,077✔
708
      mError("failed to put rsp into array, but continue at this heartbeat");
×
709
    }
710
    return TSDB_CODE_SUCCESS;
9,703,600✔
711
  }
712

713
  hbRsp.info = taosArrayInit(kvNum, sizeof(SKv));
29,034,307✔
714
  if (NULL == hbRsp.info) {
29,034,978✔
715
    mError("taosArrayInit %d rsp kv failed", kvNum);
×
716
    code = terrno;
×
717
    tFreeClientHbRsp(&hbRsp);
718
    TAOS_RETURN(code);
×
719
  }
720

721
#ifdef TD_ENTERPRISE
722
  bool             needCheck = true;
29,034,978✔
723
  int32_t          key = HEARTBEAT_KEY_DYN_VIEW;
29,034,978✔
724
  SDynViewVersion *pDynViewVer = NULL;
29,034,978✔
725
  SKv             *pKv = taosHashGet(pHbReq->info, &key, sizeof(key));
29,034,978✔
726
  if (NULL != pKv) {
29,034,664✔
727
    pDynViewVer = pKv->value;
9,072✔
728
    mTrace("recv view dyn ver, bootTs:%" PRId64 ", ver:%" PRIu64, pDynViewVer->svrBootTs, pDynViewVer->dynViewVer);
9,072✔
729

730
    SDynViewVersion *pRspVer = NULL;
9,072✔
731
    if (0 != (code = mndValidateDynViewVersion(pMnode, pDynViewVer, &needCheck, &pRspVer))) {
9,072✔
732
      TAOS_RETURN(code);
×
733
    }
734

735
    if (needCheck) {
9,072✔
736
      SKv kv1 = {.key = HEARTBEAT_KEY_DYN_VIEW, .valueLen = sizeof(*pDynViewVer), .value = pRspVer};
6,879✔
737
      if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
13,758✔
738
        if (terrno != 0) code = terrno;
×
739
        TAOS_RETURN(code);
×
740
      };
741
      mTrace("need to check view ver, lastest bootTs:%" PRId64 ", ver:%" PRIu64, pRspVer->svrBootTs,
6,879✔
742
             pRspVer->dynViewVer);
743
    }
744
  }
745
#endif
746

747
  void *pIter = taosHashIterate(pHbReq->info, NULL);
29,034,664✔
748
  while (pIter != NULL) {
75,111,543✔
749
    SKv *kv = pIter;
46,076,223✔
750

751
    switch (kv->key) {
46,076,223✔
752
      case HEARTBEAT_KEY_USER_AUTHINFO: {
29,034,497✔
753
        void   *rspMsg = NULL;
29,034,497✔
754
        int32_t rspLen = 0;
29,034,497✔
755
        (void)mndValidateUserAuthInfo(pMnode, kv->value, kv->valueLen / sizeof(SUserAuthVersion), &rspMsg, &rspLen,
29,034,497✔
756
                                      pObj->ipWhiteListVer);
757
        if (rspMsg && rspLen > 0) {
29,033,105✔
758
          SKv kv1 = {.key = HEARTBEAT_KEY_USER_AUTHINFO, .valueLen = rspLen, .value = rspMsg};
29,033,511✔
759
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
58,063,390✔
760
            mError("failed to put kv into array, but continue at this heartbeat");
×
761
          }
762
        }
763
        break;
29,031,916✔
764
      }
765
      case HEARTBEAT_KEY_DBINFO: {
10,046,943✔
766
        void   *rspMsg = NULL;
10,046,943✔
767
        int32_t rspLen = 0;
10,046,943✔
768
        (void)mndValidateDbInfo(pMnode, kv->value, kv->valueLen / sizeof(SDbCacheInfo), &rspMsg, &rspLen);
10,046,943✔
769
        if (rspMsg && rspLen > 0) {
10,046,943✔
770
          SKv kv1 = {.key = HEARTBEAT_KEY_DBINFO, .valueLen = rspLen, .value = rspMsg};
10,046,943✔
771
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
20,093,886✔
772
            mError("failed to put kv into array, but continue at this heartbeat");
×
773
          }
774
        }
775
        break;
10,046,943✔
776
      }
777
      case HEARTBEAT_KEY_STBINFO: {
6,963,885✔
778
        void   *rspMsg = NULL;
6,963,885✔
779
        int32_t rspLen = 0;
6,963,885✔
780
        (void)mndValidateStbInfo(pMnode, kv->value, kv->valueLen / sizeof(SSTableVersion), &rspMsg, &rspLen);
6,963,885✔
781
        if (rspMsg && rspLen > 0) {
6,963,885✔
782
          SKv kv1 = {.key = HEARTBEAT_KEY_STBINFO, .valueLen = rspLen, .value = rspMsg};
6,963,885✔
783
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
13,927,770✔
784
            mError("failed to put kv into array, but continue at this heartbeat");
×
785
          }
786
        }
787
        break;
6,963,885✔
788
      }
789
#ifdef TD_ENTERPRISE
790
      case HEARTBEAT_KEY_DYN_VIEW: {
9,072✔
791
        break;
9,072✔
792
      }
793
      case HEARTBEAT_KEY_VIEWINFO: {
9,072✔
794
        if (!needCheck) {
9,072✔
795
          break;
2,193✔
796
        }
797

798
        void   *rspMsg = NULL;
6,879✔
799
        int32_t rspLen = 0;
6,879✔
800
        (void)mndValidateViewInfo(pMnode, kv->value, kv->valueLen / sizeof(SViewVersion), &rspMsg, &rspLen);
6,879✔
801
        if (rspMsg && rspLen > 0) {
6,879✔
802
          SKv kv1 = {.key = HEARTBEAT_KEY_VIEWINFO, .valueLen = rspLen, .value = rspMsg};
6,879✔
803
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
13,758✔
804
            mError("failed to put kv into array, but continue at this heartbeat");
×
805
          }
806
        }
807
        break;
6,879✔
808
      }
809
#endif
810
      case HEARTBEAT_KEY_TSMA: {
13,552✔
811
        void   *rspMsg = NULL;
13,552✔
812
        int32_t rspLen = 0;
13,552✔
813
        (void)mndValidateTSMAInfo(pMnode, kv->value, kv->valueLen / sizeof(STSMAVersion), &rspMsg, &rspLen);
13,552✔
814
        if (rspMsg && rspLen > 0) {
13,552✔
815
          SKv kv = {.key = HEARTBEAT_KEY_TSMA, .valueLen = rspLen, .value = rspMsg};
13,552✔
816
          if (taosArrayPush(hbRsp.info, &kv) == NULL) {
27,104✔
817
            mError("failed to put kv into array, but continue at this heartbeat");
×
818
          }
819
        }
820
        break;
13,552✔
821
      }
822
      default:
198✔
823
        mError("invalid kv key:%d", kv->key);
198✔
824
        hbRsp.status = TSDB_CODE_APP_ERROR;
198✔
825
        break;
198✔
826
    }
827

828
    pIter = taosHashIterate(pHbReq->info, pIter);
46,075,532✔
829
  }
830

831
  if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
58,070,655✔
832
    if (terrno != 0) code = terrno;
×
833
  }
834
  TAOS_RETURN(code);
29,035,335✔
835
}
836

837
static int32_t mndProcessHeartBeatReq(SRpcMsg *pReq) {
31,906,202✔
838
  int32_t code = 0;
31,906,202✔
839
  int32_t lino = 0;
31,906,202✔
840
  SMnode *pMnode = pReq->info.node;
31,906,202✔
841

842
  SClientHbBatchReq batchReq = {0};
31,910,906✔
843
  if (tDeserializeSClientHbBatchReq(pReq->pCont, pReq->contLen, &batchReq) != 0) {
31,909,683✔
844
    taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
×
845
    code = TSDB_CODE_INVALID_MSG;
×
846
    TAOS_RETURN(code);
×
847
  }
848

849
  SConnPreparedObj obj = {0};
31,910,937✔
850
  obj.totalDnodes = mndGetDnodeSize(pMnode);
31,909,737✔
851
  obj.ipWhiteListVer = batchReq.ipWhiteListVer;
31,912,686✔
852
  TAOS_CHECK_RETURN(mndGetOnlineDnodeNum(pMnode, &obj.onlineDnodes));
31,912,686✔
853
  mndGetMnodeEpSet(pMnode, &obj.epSet);
31,908,356✔
854
  TAOS_CHECK_RETURN(mndCreateQnodeList(pMnode, &obj.pQnodeList, -1));
31,912,686✔
855

856
  SClientHbBatchRsp batchRsp = {0};
31,907,967✔
857
  batchRsp.svrTimestamp = taosGetTimestampSec();
31,905,221✔
858
  batchRsp.rsps = taosArrayInit(0, sizeof(SClientHbRsp));
31,904,026✔
859
  if (batchRsp.rsps == NULL) {
31,900,249✔
860
    TAOS_CHECK_EXIT(terrno);
×
861
  }
862
  batchRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
31,900,249✔
863
  batchRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
31,900,249✔
864
  batchRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
31,900,249✔
865
  tstrncpy(batchRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
31,900,249✔
866
  batchRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
31,903,400✔
867
  batchRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
31,903,400✔
868
  batchRsp.enableAuditDelete = tsEnableAuditDelete;
31,903,400✔
869
  batchRsp.enableAuditSelect = tsEnableAuditSelect;
31,903,400✔
870
  batchRsp.enableAuditInsert = tsEnableAuditInsert;
31,903,400✔
871
  batchRsp.auditLevel = tsAuditLevel;
31,903,400✔
872
  batchRsp.enableStrongPass = tsEnableStrongPassword;
31,903,400✔
873
  batchRsp.sodInitial = (pMnode->sodPhase == TSDB_SOD_PHASE_INITIAL ? 1 : 0);
31,903,400✔
874
  batchRsp.macActive = (pMnode->macActive == MAC_MODE_MANDATORY ? 1 : 0);
31,908,665✔
875

876
  int32_t sz = taosArrayGetSize(batchReq.reqs);
31,900,567✔
877
  for (int i = 0; i < sz; i++) {
70,645,120✔
878
    SClientHbReq *pHbReq = taosArrayGet(batchReq.reqs, i);
38,733,063✔
879
    if (pHbReq->connKey.connType == CONN_TYPE__QUERY || pHbReq->connKey.connType == CONN_TYPE__TMQ) {
38,734,201✔
880
      TAOS_CHECK_EXIT(mndProcessQueryHeartBeat(pMnode, pReq, pHbReq, &batchRsp, &obj));
38,733,907✔
881
    } 
882
  }
883
  taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
31,912,057✔
884

885
  int32_t tlen = tSerializeSClientHbBatchRsp(NULL, 0, &batchRsp);
31,909,815✔
886
  if (tlen < 0) {
31,902,133✔
887
    TAOS_CHECK_EXIT(tlen);
×
888
  }
889
  void *buf = rpcMallocCont(tlen);
31,902,133✔
890
  if (!buf) {
31,906,778✔
891
    TAOS_CHECK_EXIT(terrno);
×
892
  }
893
  tlen = tSerializeSClientHbBatchRsp(buf, tlen, &batchRsp);
31,906,778✔
894
  if (tlen < 0) {
31,908,819✔
895
    rpcFreeCont(buf);
×
896
    TAOS_CHECK_EXIT(tlen);
×
897
  }
898
  pReq->info.rspLen = tlen;
31,908,819✔
899
  pReq->info.rsp = buf;
31,906,552✔
900
_exit:
31,907,392✔
901
  tFreeClientHbBatchRsp(&batchRsp);
902

903
  taosArrayDestroy(obj.pQnodeList);
31,907,472✔
904

905
  TAOS_RETURN(code);
31,909,453✔
906
}
907

908
static int32_t mndProcessKillQueryReq(SRpcMsg *pReq) {
1,860✔
909
  int32_t       code = 0;
1,860✔
910
  SMnode       *pMnode = pReq->info.node;
1,860✔
911
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
1,860✔
912

913
  SKillQueryReq killReq = {0};
1,860✔
914
  TAOS_CHECK_RETURN(tDeserializeSKillQueryReq(pReq->pCont, pReq->contLen, &killReq));
1,860✔
915

916
  mInfo("kill query msg is received, queryId:%s", killReq.queryStrId);
1,860✔
917
  TAOS_CHECK_RETURN(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_KILL_QUERY));
1,860✔
918
  int32_t  connId = 0;
1,860✔
919
  uint64_t queryId = 0;
1,860✔
920
  char    *p = strchr(killReq.queryStrId, ':');
1,860✔
921
  if (NULL == p) {
1,860✔
922
    mError("invalid QID:%s", killReq.queryStrId);
×
923
    code = TSDB_CODE_MND_INVALID_QUERY_ID;
×
924
    TAOS_RETURN(code);
×
925
  }
926
  *p = 0;
1,860✔
927
  connId = taosStr2Int32(killReq.queryStrId, NULL, 16);
1,860✔
928
  queryId = taosStr2UInt64(p + 1, NULL, 16);
1,860✔
929

930
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &connId, sizeof(int32_t));
1,860✔
931
  if (pConn == NULL) {
1,860✔
932
    mError("connId:%x, failed to kill queryId:%" PRIx64 ", conn not exist", connId, queryId);
384✔
933
    code = TSDB_CODE_MND_INVALID_CONN_ID;
384✔
934
    TAOS_RETURN(code);
384✔
935
  } else {
936
    mInfo("connId:%x, queryId:%" PRIx64 " is killed by user:%s", connId, queryId, RPC_MSG_USER(pReq));
1,476✔
937
    pConn->killId = queryId;
1,476✔
938
    taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
1,476✔
939
    TAOS_RETURN(code);
1,476✔
940
  }
941
}
942

943
static int32_t mndProcessKillConnReq(SRpcMsg *pReq) {
384✔
944
  int32_t       code = 0;
384✔
945
  SMnode       *pMnode = pReq->info.node;
384✔
946
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
384✔
947

948
  SKillConnReq killReq = {0};
384✔
949
  TAOS_CHECK_RETURN(tDeserializeSKillConnReq(pReq->pCont, pReq->contLen, &killReq));
384✔
950

951
  TAOS_CHECK_RETURN(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_KILL_CONN));
384✔
952

953
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &killReq.connId, sizeof(uint32_t));
384✔
954
  if (pConn == NULL) {
384✔
955
    mError("connId:%u, failed to kill connection, conn not exist", killReq.connId);
384✔
956
    code = TSDB_CODE_MND_INVALID_CONN_ID;
384✔
957
    TAOS_RETURN(code);
384✔
958
  } else {
959
    mInfo("connId:%u, is killed by user:%s", killReq.connId, RPC_MSG_USER(pReq));
×
960
    pConn->killed = 1;
×
961
    taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
×
962
    TAOS_RETURN(code);
×
963
  }
964
}
965

966
static int32_t mndProcessSvrVerReq(SRpcMsg *pReq) {
×
967
  int32_t       code = 0;
×
968
  int32_t       lino = 0;
×
969
  SServerVerRsp rsp = {0};
×
970
  tstrncpy(rsp.ver, td_version, sizeof(rsp.ver));
×
971

972
  int32_t contLen = tSerializeSServerVerRsp(NULL, 0, &rsp);
×
973
  if (contLen < 0) {
×
974
    TAOS_CHECK_EXIT(contLen);
×
975
  }
976
  void *pRsp = rpcMallocCont(contLen);
×
977
  if (pRsp == NULL) {
×
978
    TAOS_CHECK_EXIT(terrno);
×
979
  }
980
  contLen = tSerializeSServerVerRsp(pRsp, contLen, &rsp);
×
981
  if (contLen < 0) {
×
982
    rpcFreeCont(pRsp);
×
983
    TAOS_CHECK_EXIT(contLen);
×
984
  }
985

986
  pReq->info.rspLen = contLen;
×
987
  pReq->info.rsp = pRsp;
×
988

989
_exit:
×
990

991
  TAOS_RETURN(code);
×
992
}
993

994
static int32_t mndRetrieveConns(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
16,550✔
995
  SMnode   *pMnode = pReq->info.node;
16,550✔
996
  SSdb     *pSdb = pMnode->pSdb;
16,550✔
997
  int32_t   numOfRows = 0;
16,550✔
998
  int32_t   cols = 0;
16,550✔
999
  int32_t   code = 0;
16,550✔
1000
  SConnObj *pConn = NULL;
16,550✔
1001

1002
  if (pShow->pIter == NULL) {
16,550✔
1003
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
16,550✔
1004
    pShow->pIter = taosCacheCreateIter(pMgmt->connCache);
16,550✔
1005
    if (!pShow->pIter) return terrno;
16,550✔
1006
  }
1007

1008
  while (numOfRows < rows) {
495,562✔
1009
    pConn = mndGetNextConn(pMnode, pShow->pIter);
495,562✔
1010
    if (pConn == NULL) {
495,562✔
1011
      pShow->pIter = NULL;
16,550✔
1012
      break;
16,550✔
1013
    }
1014

1015
    if ((taosGetTimestampMs() - pConn->lastAccessTimeMs) > ((int64_t)CACHE_OBJ_KEEP_TIME * 1000)) {
479,012✔
1016
      continue;
442,192✔
1017
    }
1018

1019
    cols = 0;
36,820✔
1020

1021
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
36,820✔
1022
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->id, false);
36,820✔
1023
    if (code != 0) {
36,820✔
1024
      mError("failed to set conn id:%u since %s", pConn->id, tstrerror(code));
×
1025
      return code;
×
1026
    }
1027

1028
    char user[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
36,820✔
1029
    STR_TO_VARSTR(user, pConn->user);
36,820✔
1030
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
36,820✔
1031
    code = colDataSetVal(pColInfo, numOfRows, (const char *)user, false);
36,820✔
1032
    if (code != 0) {
36,820✔
1033
      mError("failed to set user since %s", tstrerror(code));
×
1034
      return code;
×
1035
    }
1036

1037
    char app[TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE];
36,424✔
1038
    STR_TO_VARSTR(app, pConn->app);
36,820✔
1039
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
36,820✔
1040
    code = colDataSetVal(pColInfo, numOfRows, (const char *)app, false);
36,820✔
1041
    if (code != 0) {
36,820✔
1042
      mError("failed to set app since %s", tstrerror(code));
×
1043
      return code;
×
1044
    }
1045

1046
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
36,820✔
1047
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->pid, false);
36,820✔
1048
    if (code != 0) {
36,820✔
1049
      mError("failed to set conn id:%u since %s", pConn->id, tstrerror(code));
×
1050
      return code;
×
1051
    }
1052

1053
    char addr[IP_RESERVE_CAP] = {0};
36,820✔
1054
    char endpoint[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
36,820✔
1055
    if (snprintf(addr, sizeof(addr), "%s:%d", IP_ADDR_STR(&pConn->addr), pConn->addr.port) >= sizeof(addr)) {
36,820✔
1056
      code = TSDB_CODE_OUT_OF_RANGE;
×
1057
      mError("failed to set endpoint since %s", tstrerror(code));
×
1058
      return code;
×
1059
    }
1060

1061
    STR_TO_VARSTR(endpoint, addr);
36,820✔
1062

1063
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
36,820✔
1064
    code = colDataSetVal(pColInfo, numOfRows, (const char *)endpoint, false);
36,820✔
1065
    if (code != 0) {
36,820✔
1066
      mError("failed to set endpoint since %s", tstrerror(code));
×
1067
      return code;
×
1068
    }
1069

1070
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
36,820✔
1071
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->loginTimeMs, false);
36,820✔
1072
    if (code != 0) {
36,820✔
1073
      mError("failed to set login time since %s", tstrerror(code));
×
1074
      return code;
×
1075
    }
1076

1077
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
36,820✔
1078
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->lastAccessTimeMs, false);
36,820✔
1079
    if (code != 0) {
36,820✔
1080
      mError("failed to set last access time since %s", tstrerror(code));
×
1081
      return code;
×
1082
    }
1083

1084
    char userApp[TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE];
36,424✔
1085
    STR_TO_VARSTR(userApp, pConn->userApp);
36,820✔
1086
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
36,820✔
1087
    code = colDataSetVal(pColInfo, numOfRows, (const char *)userApp, false);
36,820✔
1088
    if (code != 0) {
36,820✔
1089
      mError("failed to set user app since %s", tstrerror(code));
×
1090
      return code;
×
1091
    }
1092

1093
    char userIp[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
36,820✔
1094
    getUserIpFromConnObj(pConn, userIp);
36,820✔
1095

1096
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
36,820✔
1097
    code = colDataSetVal(pColInfo, numOfRows, (const char *)userIp, false);
36,820✔
1098
    if (code != 0) {
36,820✔
1099
      mError("failed to set user ip since %s", tstrerror(code));
×
1100
      return code;
×
1101
    }
1102

1103
    char ver[TSDB_VERSION_LEN + VARSTR_HEADER_SIZE];
36,424✔
1104
    STR_TO_VARSTR(ver, pConn->sVer);
36,820✔
1105
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
36,820✔
1106
    code = colDataSetVal(pColInfo, numOfRows, (const char *)ver, false);
36,820✔
1107
    if (code != 0) {
36,820✔
1108
      mError("failed to set ver since %s", tstrerror(code));
×
1109
      return code;
×
1110
    }
1111

1112
    char cInfo[CONNECTOR_INFO_LEN + VARSTR_HEADER_SIZE];
36,424✔
1113
    STR_TO_VARSTR(cInfo, pConn->cInfo);
36,820✔
1114
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
36,820✔
1115
    code = colDataSetVal(pColInfo, numOfRows, (const char *)cInfo, false);
36,820✔
1116
    if (code != 0) {
36,820✔
1117
      mError("failed to set connector info since %s", tstrerror(code));
×
1118
      return code;
×
1119
    }
1120

1121
    char type[16 + VARSTR_HEADER_SIZE];
36,424✔
1122
    STR_TO_VARSTR(type, pConn->connType == CONN_TYPE__QUERY ? "QUERY" : (pConn->connType == CONN_TYPE__TMQ ? "TMQ" : "UNKNOWN"));
36,820✔
1123
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
36,820✔
1124
    code = colDataSetVal(pColInfo, numOfRows, (const char *)type, false);
36,820✔
1125
    if (code != 0) {
36,820✔
1126
      mError("failed to set type info since %s", tstrerror(code));
×
1127
      return code;
×
1128
    }
1129

1130
    char tokenName[TSDB_TOKEN_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
36,820✔
1131
    STR_TO_VARSTR(tokenName, pConn->tokenName);
36,820✔
1132
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
36,820✔
1133
    code = colDataSetVal(pColInfo, numOfRows, (const char *)tokenName, false);
36,820✔
1134
    if (code != 0) {
36,820✔
1135
      mError("failed to set token name since %s", tstrerror(code));
×
1136
      return code;
×
1137
    }
1138

1139
    numOfRows++;
36,820✔
1140
  }
1141

1142
  pShow->numOfRows += numOfRows;
16,550✔
1143
  return numOfRows;
16,550✔
1144
}
1145

1146
/**
1147
 * @param pConn the conn queries pack from
1148
 * @param[out] pBlock the block data packed into
1149
 * @param offset skip [offset] queries in pConn
1150
 * @param rowsToPack at most rows to pack
1151
 * @return rows packed
1152
 */
1153
static int32_t packQueriesIntoBlock(SShowObj *pShow, SConnObj *pConn, SSDataBlock *pBlock, uint32_t offset,
64,980✔
1154
                                    uint32_t rowsToPack) {
1155
  int32_t cols = 0;
64,980✔
1156
  int32_t code = 0;
64,980✔
1157
  taosRLockLatch(&pConn->queryLock);
64,980✔
1158
  int32_t numOfQueries = taosArrayGetSize(pConn->pQueries);
64,980✔
1159
  if (NULL == pConn->pQueries || numOfQueries <= offset) {
64,980✔
1160
    taosRUnLockLatch(&pConn->queryLock);
42,516✔
1161
    return 0;
42,516✔
1162
  }
1163

1164
  int32_t i = offset;
22,464✔
1165
  for (; i < numOfQueries && (i - offset) < rowsToPack; ++i) {
44,928✔
1166
    int32_t     curRowIndex = pBlock->info.rows;
22,464✔
1167
    SQueryDesc *pQuery = taosArrayGet(pConn->pQueries, i);
22,464✔
1168
    cols = 0;
22,464✔
1169

1170
    char queryId[26 + VARSTR_HEADER_SIZE] = {0};
22,464✔
1171
    (void)snprintf(&queryId[VARSTR_HEADER_SIZE], sizeof(queryId) - VARSTR_HEADER_SIZE, "%x:%" PRIx64, pConn->id,
22,464✔
1172
                   pQuery->reqRid);
1173
    varDataLen(queryId) = strlen(&queryId[VARSTR_HEADER_SIZE]);
22,464✔
1174
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1175
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)queryId, false);
22,464✔
1176
    if (code != 0) {
22,464✔
1177
      mError("failed to set query id:%s since %s", queryId, tstrerror(code));
×
1178
      taosRUnLockLatch(&pConn->queryLock);
×
1179
      return code;
×
1180
    }
1181

1182
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1183
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->queryId, false);
22,464✔
1184
    if (code != 0) {
22,464✔
1185
      mError("failed to set query id:%" PRIx64 " since %s", pQuery->queryId, tstrerror(code));
×
1186
      taosRUnLockLatch(&pConn->queryLock);
×
1187
      return code;
×
1188
    }
1189

1190
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1191
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pConn->id, false);
22,464✔
1192
    if (code != 0) {
22,464✔
1193
      mError("failed to set conn id:%u since %s", pConn->id, tstrerror(code));
×
1194
      taosRUnLockLatch(&pConn->queryLock);
×
1195
      return code;
×
1196
    }
1197

1198
    char app[TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE];
22,464✔
1199
    STR_TO_VARSTR(app, pConn->app);
22,464✔
1200
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1201
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)app, false);
22,464✔
1202
    if (code != 0) {
22,464✔
1203
      mError("failed to set app since %s", tstrerror(code));
×
1204
      taosRUnLockLatch(&pConn->queryLock);
×
1205
      return code;
×
1206
    }
1207

1208
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1209
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pConn->pid, false);
22,464✔
1210
    if (code != 0) {
22,464✔
1211
      mError("failed to set conn id:%u since %s", pConn->id, tstrerror(code));
×
1212
      taosRUnLockLatch(&pConn->queryLock);
×
1213
      return code;
×
1214
    }
1215

1216
    char user[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
22,464✔
1217
    STR_TO_VARSTR(user, pConn->user);
22,464✔
1218
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1219
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)user, false);
22,464✔
1220
    if (code != 0) {
22,464✔
1221
      mError("failed to set user since %s", tstrerror(code));
×
1222
      taosRUnLockLatch(&pConn->queryLock);
×
1223
      return code;
×
1224
    }
1225

1226
    char endpoint[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
22,464✔
1227
    char buf[IP_RESERVE_CAP] = {0};
22,464✔
1228
    (void)snprintf(buf, sizeof(buf), "%s:%d", IP_ADDR_STR(&pConn->addr), pConn->addr.port);
22,464✔
1229
    STR_TO_VARSTR(endpoint, buf);
22,464✔
1230
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1231
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)endpoint, false);
22,464✔
1232
    if (code != 0) {
22,464✔
1233
      mError("failed to set endpoint since %s", tstrerror(code));
×
1234
      taosRUnLockLatch(&pConn->queryLock);
×
1235
      return code;
×
1236
    }
1237

1238
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1239
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->stime, false);
22,464✔
1240
    if (code != 0) {
22,464✔
1241
      mError("failed to set start time since %s", tstrerror(code));
×
1242
      taosRUnLockLatch(&pConn->queryLock);
×
1243
      return code;
×
1244
    }
1245

1246
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1247
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->useconds, false);
22,464✔
1248
    if (code != 0) {
22,464✔
1249
      mError("failed to set useconds since %s", tstrerror(code));
×
1250
      taosRUnLockLatch(&pConn->queryLock);
×
1251
      return code;
×
1252
    }
1253

1254
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1255
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->stableQuery, false);
22,464✔
1256
    if (code != 0) {
22,464✔
1257
      mError("failed to set stable query since %s", tstrerror(code));
×
1258
      taosRUnLockLatch(&pConn->queryLock);
×
1259
      return code;
×
1260
    }
1261

1262
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1263
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->isSubQuery, false);
22,464✔
1264
    if (code != 0) {
22,464✔
1265
      mError("failed to set sub query since %s", tstrerror(code));
×
1266
      taosRUnLockLatch(&pConn->queryLock);
×
1267
      return code;
×
1268
    }
1269

1270
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1271
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->subPlanNum, false);
22,464✔
1272
    if (code != 0) {
22,464✔
1273
      mError("failed to set sub plan num since %s", tstrerror(code));
×
1274
      taosRUnLockLatch(&pConn->queryLock);
×
1275
      return code;
×
1276
    }
1277

1278
    char    subStatus[TSDB_SHOW_SUBQUERY_LEN + VARSTR_HEADER_SIZE] = {0};
22,464✔
1279
    int64_t reserve = 128;
22,464✔
1280
    int32_t strSize = sizeof(subStatus);
22,464✔
1281
    int32_t offset = VARSTR_HEADER_SIZE;
22,464✔
1282
    for (int32_t i = 0; i < pQuery->subPlanNum && offset + reserve < strSize; ++i) {
44,913✔
1283
      if (i) {
22,449✔
UNCOV
1284
        offset += snprintf(subStatus + offset, sizeof(subStatus) - offset, ",");
×
1285
      }
1286
      if (offset + reserve >= strSize) break;
22,449✔
1287

1288
      SQuerySubDesc *pDesc = taosArrayGet(pQuery->subDesc, i);
22,449✔
1289
      if (NULL == pDesc) break;
22,449✔
1290

1291
      char startBuf[32] = {0};
22,449✔
1292
      (void)snprintf(startBuf, sizeof(startBuf), "-");
22,449✔
1293
      if (pDesc->startTs > 0) {
22,449✔
1294
        time_t    startSec = (time_t)(pDesc->startTs / 1000000);
22,449✔
1295
        int32_t   startFrac = (int32_t)(pDesc->startTs % 1000000) / 1000;
22,449✔
1296
        struct tm startTm;
22,449✔
1297
        if (taosLocalTime(&startSec, &startTm, NULL, 0, NULL) != NULL) {
22,449✔
1298
          size_t n = taosStrfTime(startBuf, sizeof(startBuf), "%Y-%m-%d %H:%M:%S", &startTm);
22,449✔
1299
          if (tsnprintf(startBuf + n, sizeof(startBuf) - n, ".%03d", startFrac) < 0) {
22,449✔
1300
            mError("failed to format start time for sub query since %s", tstrerror(terrno));
×
1301
            code = terrno;
×
1302
            taosRUnLockLatch(&pConn->queryLock);
×
1303
            return code;
×
1304
          }
1305
        }
1306
      }
1307

1308
      offset += tsnprintf(subStatus + offset, sizeof(subStatus) - offset,
22,449✔
1309
                          "%" PRIu64 ":%s:%s", pDesc->tid, pDesc->status, startBuf);
22,449✔
1310
    }
1311
    varDataLen(subStatus) = strlen(&subStatus[VARSTR_HEADER_SIZE]);
22,464✔
1312
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1313
    code = colDataSetVal(pColInfo, curRowIndex, subStatus, (varDataLen(subStatus) == 0) ? true : false);
22,464✔
1314
    if (code != 0) {
22,464✔
1315
      mError("failed to set sub status since %s", tstrerror(code));
×
1316
      taosRUnLockLatch(&pConn->queryLock);
×
1317
      return code;
×
1318
    }
1319

1320
    char sql[TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE] = {0};
22,464✔
1321
    STR_TO_VARSTR(sql, pQuery->sql);
22,464✔
1322
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1323
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)sql, false);
22,464✔
1324
    if (code != 0) {
22,464✔
1325
      mError("failed to set sql since %s", tstrerror(code));
×
1326
      taosRUnLockLatch(&pConn->queryLock);
×
1327
      return code;
×
1328
    }
1329

1330
    char userApp[TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE];
22,464✔
1331
    STR_TO_VARSTR(userApp, pConn->userApp);
22,464✔
1332
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1333
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)userApp, false);
22,464✔
1334
    if (code != 0) {
22,464✔
1335
      mError("failed to set user app since %s", tstrerror(code));
×
1336
      taosRUnLockLatch(&pConn->queryLock);
×
1337
      return code;
×
1338
    }
1339

1340
    char userIp[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
22,464✔
1341
    getUserIpFromConnObj(pConn, userIp);
22,464✔
1342

1343
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1344
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)userIp, false);
22,464✔
1345
    if (code != 0) {
22,464✔
1346
      mError("failed to set user ip since %s", tstrerror(code));
×
1347
      taosRUnLockLatch(&pConn->queryLock);
×
1348
      return code;
×
1349
    }
1350

1351
    const char* phaseStr = queryPhaseStr(pQuery->execPhase);
22,464✔
1352
    char        phaseVarStr[64 + VARSTR_HEADER_SIZE];
22,464✔
1353
    STR_TO_VARSTR(phaseVarStr, phaseStr);
22,464✔
1354
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1355
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)phaseVarStr, false);
22,464✔
1356
    if (code != 0) {
22,464✔
1357
      mError("failed to set current phase since %s", tstrerror(code));
×
1358
      taosRUnLockLatch(&pConn->queryLock);
×
1359
      return code;
×
1360
    }
1361

1362
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,464✔
1363
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->phaseStartTime, false);
22,464✔
1364
    if (code != 0) {
22,464✔
1365
      mError("failed to set phase start time since %s", tstrerror(code));
×
1366
      taosRUnLockLatch(&pConn->queryLock);
×
1367
      return code;
×
1368
    }
1369

1370
    pBlock->info.rows++;
22,464✔
1371
  }
1372

1373
  taosRUnLockLatch(&pConn->queryLock);
22,464✔
1374
  return i - offset;
22,464✔
1375
}
1376

1377
static int32_t mndRetrieveQueries(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
22,437✔
1378
  SMnode   *pMnode = pReq->info.node;
22,437✔
1379
  SSdb     *pSdb = pMnode->pSdb;
22,437✔
1380
  int32_t   numOfRows = 0;
22,437✔
1381
  SConnObj *pConn = NULL;
22,437✔
1382

1383
  if (pShow->pIter == NULL) {
22,437✔
1384
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
22,437✔
1385
    pShow->pIter = taosCacheCreateIter(pMgmt->connCache);
22,437✔
1386
    if (!pShow->pIter) return terrno;
22,437✔
1387
  }
1388

1389
  // means fetched some data last time for this conn
1390
  if (pShow->curIterPackedRows > 0) {
22,437✔
1391
    size_t len = 0;
×
1392
    pConn = taosCacheIterGetData(pShow->pIter, &len);
×
1393
    if (pConn && (taosArrayGetSize(pConn->pQueries) > pShow->curIterPackedRows)) {
×
1394
      numOfRows = packQueriesIntoBlock(pShow, pConn, pBlock, pShow->curIterPackedRows, rows);
×
1395
      pShow->curIterPackedRows += numOfRows;
×
1396
    }
1397
  }
1398

1399
  while (numOfRows < rows) {
87,417✔
1400
    pConn = mndGetNextConn(pMnode, pShow->pIter);
87,417✔
1401
    if (pConn == NULL) {
87,417✔
1402
      pShow->pIter = NULL;
22,437✔
1403
      break;
22,437✔
1404
    }
1405

1406
    int32_t packedRows = packQueriesIntoBlock(pShow, pConn, pBlock, 0, rows - numOfRows);
64,980✔
1407
    pShow->curIterPackedRows = packedRows;
64,980✔
1408
    numOfRows += packedRows;
64,980✔
1409
  }
1410
  pShow->numOfRows += numOfRows;
22,437✔
1411
  return numOfRows;
22,437✔
1412
}
1413

1414
static int32_t mndRetrieveApps(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
3,942✔
1415
  SMnode  *pMnode = pReq->info.node;
3,942✔
1416
  SSdb    *pSdb = pMnode->pSdb;
3,942✔
1417
  int32_t  numOfRows = 0;
3,942✔
1418
  int32_t  cols = 0;
3,942✔
1419
  SAppObj *pApp = NULL;
3,942✔
1420
  int32_t  code = 0;
3,942✔
1421

1422
  if (pShow->pIter == NULL) {
3,942✔
1423
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
3,942✔
1424
    pShow->pIter = taosCacheCreateIter(pMgmt->appCache);
3,942✔
1425
    if (!pShow->pIter) return terrno;
3,942✔
1426
  }
1427

1428
  while (numOfRows < rows) {
7,884✔
1429
    pApp = mndGetNextApp(pMnode, pShow->pIter);
7,884✔
1430
    if (pApp == NULL) {
7,884✔
1431
      pShow->pIter = NULL;
3,942✔
1432
      break;
3,942✔
1433
    }
1434

1435
    cols = 0;
3,942✔
1436

1437
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,942✔
1438
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->appId, false);
3,942✔
1439
    if (code != 0) {
3,942✔
1440
      mError("failed to set app id since %s", tstrerror(code));
×
1441
      return code;
×
1442
    }
1443

1444
    char ip[TD_IP_LEN + VARSTR_HEADER_SIZE] = {0};
3,942✔
1445
    char buf[IP_RESERVE_CAP] = {0};
3,942✔
1446
    snprintf(buf, sizeof(buf), "%s", IP_ADDR_STR(&pApp->cliAddr));
3,942✔
1447
    STR_TO_VARSTR(ip, buf);
3,942✔
1448

1449
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,942✔
1450
    code = colDataSetVal(pColInfo, numOfRows, (const char *)ip, false);
3,942✔
1451
    if (code != 0) {
3,942✔
1452
      mError("failed to set ip since %s", tstrerror(code));
×
1453
      return code;
×
1454
    }
1455

1456
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,942✔
1457
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->pid, false);
3,942✔
1458
    if (code != 0) {
3,942✔
1459
      mError("failed to set pid since %s", tstrerror(code));
×
1460
      return code;
×
1461
    }
1462

1463
    char name[TSDB_APP_NAME_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
3,942✔
1464
    (void)snprintf(&name[VARSTR_HEADER_SIZE], sizeof(name) - VARSTR_HEADER_SIZE, "%s", pApp->name);
3,942✔
1465
    varDataLen(name) = strlen(&name[VARSTR_HEADER_SIZE]);
3,942✔
1466
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,942✔
1467
    code = colDataSetVal(pColInfo, numOfRows, (const char *)name, false);
3,942✔
1468
    if (code != 0) {
3,942✔
1469
      mError("failed to set app name since %s", tstrerror(code));
×
1470
      return code;
×
1471
    }
1472

1473
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,942✔
1474
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->startTime, false);
3,942✔
1475
    if (code != 0) {
3,942✔
1476
      mError("failed to set start time since %s", tstrerror(code));
×
1477
      return code;
×
1478
    }
1479

1480
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,942✔
1481
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.numOfInsertsReq, false);
3,942✔
1482
    if (code != 0) {
3,942✔
1483
      mError("failed to set insert req since %s", tstrerror(code));
×
1484
      return code;
×
1485
    }
1486

1487
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,942✔
1488
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.numOfInsertRows, false);
3,942✔
1489
    if (code != 0) {
3,942✔
1490
      mError("failed to set insert rows since %s", tstrerror(code));
×
1491
      return code;
×
1492
    }
1493

1494
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,942✔
1495
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.insertElapsedTime, false);
3,942✔
1496
    if (code != 0) {
3,942✔
1497
      mError("failed to set insert elapsed time since %s", tstrerror(code));
×
1498
      return code;
×
1499
    }
1500

1501
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,942✔
1502
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.insertBytes, false);
3,942✔
1503
    if (code != 0) {
3,942✔
1504
      mError("failed to set insert bytes since %s", tstrerror(code));
×
1505
      return code;
×
1506
    }
1507

1508
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,942✔
1509
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.fetchBytes, false);
3,942✔
1510
    if (code != 0) {
3,942✔
1511
      mError("failed to set fetch bytes since %s", tstrerror(code));
×
1512
      return code;
×
1513
    }
1514

1515
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,942✔
1516
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.queryElapsedTime, false);
3,942✔
1517
    if (code != 0) {
3,942✔
1518
      mError("failed to set query elapsed time since %s", tstrerror(code));
×
1519
      return code;
×
1520
    }
1521

1522
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,942✔
1523
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.numOfSlowQueries, false);
3,942✔
1524
    if (code != 0) {
3,942✔
1525
      mError("failed to set slow queries since %s", tstrerror(code));
×
1526
      return code;
×
1527
    }
1528

1529
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,942✔
1530
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.totalRequests, false);
3,942✔
1531
    if (code != 0) {
3,942✔
1532
      mError("failed to set total requests since %s", tstrerror(code));
×
1533
      return code;
×
1534
    }
1535

1536
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,942✔
1537
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.currentRequests, false);
3,942✔
1538
    if (code != 0) {
3,942✔
1539
      mError("failed to set current requests since %s", tstrerror(code));
×
1540
      return code;
×
1541
    }
1542

1543
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,942✔
1544
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->lastAccessTimeMs, false);
3,942✔
1545
    if (code != 0) {
3,942✔
1546
      mError("failed to set last access time since %s", tstrerror(code));
×
1547
      return code;
×
1548
    }
1549

1550
    numOfRows++;
3,942✔
1551
  }
1552

1553
  pShow->numOfRows += numOfRows;
3,942✔
1554
  return numOfRows;
3,942✔
1555
}
1556

1557
static void mndCancelGetNextQuery(SMnode *pMnode, void *pIter) {
×
1558
  if (pIter != NULL) {
×
1559
    taosCacheDestroyIter(pIter);
×
1560
  }
1561
}
×
1562

1563
int32_t mndGetNumOfConnections(SMnode *pMnode) {
100✔
1564
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
100✔
1565
  return taosCacheGetNumOfObj(pMgmt->connCache);
100✔
1566
}
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