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

taosdata / TDengine / #5036

28 Apr 2026 02:11PM UTC coverage: 73.055% (-0.005%) from 73.06%
#5036

push

travis-ci

web-flow
func/regexp_extract: new scalar func and test cases (#35191)

46 of 59 new or added lines in 1 file covered. (77.97%)

5784 existing lines in 151 files now uncovered.

276105 of 377940 relevant lines covered (73.06%)

133708549.2 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) {
485,631✔
98
  int32_t       code = 0;
485,631✔
99
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
485,631✔
100

101
  // in ms
102
  int32_t checkTime = CACHE_OBJ_KEEP_TIME * 1000;
485,631✔
103
  pMgmt->connCache = taosCacheInit(TSDB_DATA_TYPE_UINT, checkTime, false, (__cache_free_fn_t)mndFreeConn, "conn");
485,631✔
104
  if (pMgmt->connCache == NULL) {
485,631✔
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");
485,631✔
111
  if (pMgmt->appCache == NULL) {
485,631✔
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);
485,631✔
118
  mndSetMsgHandle(pMnode, TDMT_MND_CONNECT, mndProcessConnectReq);
485,631✔
119
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_QUERY, mndProcessKillQueryReq);
485,631✔
120
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_CONN, mndProcessKillConnReq);
485,631✔
121
  mndSetMsgHandle(pMnode, TDMT_MND_SERVER_VERSION, mndProcessSvrVerReq);
485,631✔
122

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

130
  TAOS_RETURN(code);
485,631✔
131
}
132

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

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

146
static void getUserIpFromConnObj(SConnObj *pConn, char *dst) {
56,535✔
147
  static char *none = "0.0.0.0";
148
  if (pConn->userIp != 0 && pConn->userIp != INADDR_NONE) {
56,535✔
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) {
56,535✔
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;
56,535✔
160
}
161
static void setUserInfo2Conn(SConnObj *connObj, char *userApp, uint32_t userIp, char *cInfo) {
38,413,757✔
162
  if (connObj == NULL) {
38,413,757✔
163
    return;
×
164
  }
165
  tstrncpy(connObj->userApp, userApp, sizeof(connObj->userApp));
38,413,757✔
166
  tstrncpy(connObj->cInfo, cInfo, sizeof(connObj->cInfo));
38,411,872✔
167
  connObj->userIp = userIp;
38,399,978✔
168
}
169
static void setUserInfoIpToConn(SConnObj *connObj, SIpRange *pRange) {
38,414,130✔
170
  int32_t code = 0;
38,414,130✔
171
  if (connObj == NULL) {
38,414,130✔
172
    return;
×
173
  }
174

175
  code = tIpUintToStr(pRange, &connObj->userDualIp);
38,414,130✔
176
  if (code != 0) {
38,404,081✔
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,648,841✔
185
                               int32_t pid, const char *app, int64_t startTime, const char *sVer) {
186
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
3,648,841✔
187

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

192
  int32_t  len = tsnprintf(connStr, sizeof(connStr), "%s%d%d%d%s", user, ip, port, pid, app);
3,648,841✔
193
  uint32_t connId = mndGenerateUid(connStr, len);
3,648,534✔
194
  if (startTime == 0) startTime = taosGetTimestampMs();
4,294,713✔
195

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

210
  connObj.lastAccessTimeMs = connObj.loginTimeMs;
3,649,061✔
211
  tstrncpy(connObj.user, user, sizeof(connObj.user));
3,649,061✔
212
  tstrncpy(connObj.tokenName, tokenName, sizeof(connObj.tokenName));
3,648,837✔
213
  tstrncpy(connObj.app, app, sizeof(connObj.app));
3,649,061✔
214
  tstrncpy(connObj.sVer, sVer, sizeof(connObj.sVer));
3,648,841✔
215

216
  SConnObj *pConn =
217
      taosCachePut(pMgmt->connCache, &connId, sizeof(uint32_t), &connObj, sizeof(connObj), CACHE_OBJ_KEEP_TIME * 1000);
3,648,837✔
218
  if (pConn == NULL) {
3,648,856✔
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,648,856✔
224
    return pConn;
3,648,971✔
225
  }
226
}
227

228

229

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

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

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

241
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &connId, sizeof(connId));
38,412,130✔
242
  if (pConn == NULL) {
38,413,339✔
243
    mDebug("conn:%u, already destroyed", connId);
645,260✔
244
    return NULL;
645,260✔
245
  }
246

247
  pConn->lastAccessTimeMs = taosGetTimestampMs();
37,768,557✔
248
  mTrace("conn:%u, acquired from cache, data:%p", pConn->id, pConn);
37,766,605✔
249
  return pConn;
37,754,026✔
250
}
251

252
static void mndReleaseConn(SMnode *pMnode, SConnObj *pConn, bool extendLifespan) {
45,256,533✔
253
  if (pConn == NULL) return;
45,256,533✔
254
  mTrace("conn:%u, released from cache, data:%p", pConn->id, pConn);
45,241,706✔
255

256
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
45,241,706✔
257
  if (extendLifespan) taosCacheTryExtendLifeSpan(pMgmt->connCache, (void **)&pConn);
45,240,781✔
258
  taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
45,240,781✔
259
}
260

261
void *mndGetNextConn(SMnode *pMnode, SCacheIter *pIter) {
4,019,539✔
262
  SConnObj *pConn = NULL;
4,019,539✔
263
  bool      hasNext = taosCacheIterNext(pIter);
4,019,539✔
264
  if (hasNext) {
4,019,539✔
265
    size_t dataLen = 0;
3,928,013✔
266
    pConn = taosCacheIterGetData(pIter, &dataLen);
3,928,013✔
267
  } else {
268
    taosCacheDestroyIter(pIter);
91,526✔
269
  }
270

271
  return pConn;
4,019,539✔
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) {
55,054✔
284
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
55,054✔
285
  SCacheIter   *pIter = taosCacheCreateIter(pMgmt->connCache);
55,054✔
286
  if (pIter == NULL) {
55,054✔
287
    mError("failed to create conn cache iterator");
×
288
    return -1;
×
289
  }
290

291
  int32_t    count = 0;
55,054✔
292
  SConnObj  *pConn = NULL;
55,054✔
293
  while ((pConn = mndGetNextConn(pMnode, pIter)) != NULL) {
3,874,723✔
294
    if (strncmp(pConn->user, user, TSDB_USER_LEN) == 0) {
3,819,669✔
295
      count++;
946,906✔
296
    }
297
    mndReleaseConn(pMnode, pConn, true);
3,819,669✔
298
  }
299

300
  return count;
55,054✔
301
}
302

303

304

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

308
  const char* currPass = pUser->passwords[0].pass;
3,011,693✔
309
  char pass[TSDB_PASSWORD_LEN] = {0};
3,011,300✔
310
  (void)memcpy(pass, inputPass, TSDB_PASSWORD_LEN);
3,011,482✔
311
  pass[TSDB_PASSWORD_LEN - 1] = 0;
3,011,698✔
312

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

320
  // constant time comparison to prevent timing attack
321
  volatile uint8_t res = 0;
3,011,693✔
322
  for (size_t i = 0; i < sizeof(pass) - 1; i++) {
96,345,663✔
323
    res |= pass[i] ^ currPass[i];
93,333,937✔
324
  }
325

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

329

330

331
static bool verifyTotp(SUserObj *pUser, int32_t totpCode) {
3,005,146✔
332
  if (!mndIsTotpEnabledUser(pUser)) {
3,005,146✔
333
    return true;
3,001,442✔
334
  }
335
  return taosVerifyTotpCode(pUser->totpsecret, sizeof(pUser->totpsecret), totpCode, 6, 1) != 0;
4,228✔
336
}
337

338

339

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

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

357
  if (token != NULL && mndGetCachedTokenInfo(token, &ti) == NULL) {
3,018,371✔
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,018,371✔
361
  TAOS_CHECK_GOTO(taosCheckVersionCompatibleFromStr(connReq.sVer, td_version, 3), &lino, _OVER);
3,018,408✔
362
  TAOS_CHECK_GOTO(tVerifyConnectReqSignature(&connReq), &lino, _OVER);
3,018,408✔
363
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, user, &pUser), &lino, _OVER);
3,018,101✔
364

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

369
  if (token != NULL || tsMndSkipGrant) {
3,012,890✔
370
    li.lastLoginTime= now;
1,401✔
371
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
1,401✔
372
      mndSetUserLoginInfo(user, &li);
1,484✔
373
    }
374
  } else if ((code = verifyPassword(pUser, connReq.passwd)) == TSDB_CODE_MND_AUTH_FAILURE) {
3,011,512✔
375
    if (pUser->failedLoginAttempts >= 0) {
6,929✔
376
      if (li.failedLoginCount >= pUser->failedLoginAttempts) {
5,651✔
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++;
5,651✔
381
      li.lastFailedLoginTime = now;
5,651✔
382
    }
383
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
6,580✔
384
      mndSetUserLoginInfo(user, &li);
6,580✔
385
    }
386
    TAOS_CHECK_GOTO(code, &lino, _OVER);
6,580✔
387
  } else if (code != TSDB_CODE_SUCCESS) {
3,005,146✔
388
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
389
  } else if (!verifyTotp(pUser, connReq.totpCode)) {
3,005,146✔
390
    TAOS_CHECK_GOTO(TSDB_CODE_MND_WRONG_TOTP_CODE, &lino, _OVER);
2,861✔
391
  } else {
392
    li.failedLoginCount = 0;
3,002,505✔
393
    li.lastLoginTime= now;
3,002,505✔
394
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
3,002,505✔
395
      mndSetUserLoginInfo(user, &li);
3,002,058✔
396
    }
397
  } 
398

399
  if (connReq.db[0] != 0) {
3,004,743✔
400
    char db[TSDB_DB_FNAME_LEN] = {0};
517,774✔
401
    (void)snprintf(db, TSDB_DB_FNAME_LEN, "%d%s%s", pUser->acctId, TS_PATH_DELIMITER, connReq.db);
517,774✔
402
    pDb = mndAcquireDb(pMnode, db);
517,774✔
403
    if (pDb == NULL) {
517,554✔
404
      if (0 != strcmp(connReq.db, TSDB_INFORMATION_SCHEMA_DB) && (0 != strcmp(connReq.db, TSDB_PERFORMANCE_SCHEMA_DB))) {
1,044✔
405
        TAOS_CHECK_GOTO(TSDB_CODE_MND_DB_NOT_EXIST, &lino, _OVER);
332✔
406
      }
407
    }
408

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

412
  if (connReq.connType == CONN_TYPE__AUTH_TEST) {
3,003,801✔
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,003,801✔
418
                        connReq.startTime, connReq.sVer);
419
  if (pConn == NULL) {
3,003,033✔
420
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
421
  }
422

423
  SConnectRsp connectRsp = {0};
3,003,033✔
424
  connectRsp.acctId = pUser->acctId;
3,003,801✔
425
  connectRsp.superUser = pUser->superUser;
3,002,581✔
426
  connectRsp.sysInfo = pUser->sysInfo;
3,003,118✔
427
  connectRsp.minSecLevel = pUser->minSecLevel;
3,003,596✔
428
  connectRsp.maxSecLevel = pUser->maxSecLevel;
3,003,356✔
429
  connectRsp.sodInitial = (pMnode->sodPhase == TSDB_SOD_PHASE_INITIAL ? 1 : 0);
3,003,352✔
430
  connectRsp.macActive = (pMnode->macActive == MAC_MODE_MANDATORY ? 1 : 0);
3,003,211✔
431
  connectRsp.clusterId = pMnode->clusterId;
3,003,797✔
432
  connectRsp.connId = pConn->id;
3,003,356✔
433
  connectRsp.connType = connReq.connType;
3,003,356✔
434
  connectRsp.dnodeNum = mndGetDnodeSize(pMnode);
3,003,356✔
435
  connectRsp.svrTimestamp = taosGetTimestampSec();
3,003,032✔
436
  connectRsp.passVer = pUser->passVersion;
3,003,065✔
437
  connectRsp.authVer = pUser->authVersion;
3,002,508✔
438
  connectRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
3,002,887✔
439
  connectRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
3,002,887✔
440
  connectRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
3,002,887✔
441
  connectRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
3,002,887✔
442
  connectRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
3,002,887✔
443
  connectRsp.enableAuditDelete = tsEnableAuditDelete;
3,002,887✔
444
  connectRsp.enableAuditSelect = tsEnableAuditSelect;
3,002,887✔
445
  connectRsp.enableAuditInsert = tsEnableAuditInsert;
3,002,887✔
446
  connectRsp.auditLevel = tsAuditLevel;
3,002,887✔
447
  tstrncpy(connectRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
3,002,887✔
448
  connectRsp.whiteListVer = pUser->ipWhiteListVer;
3,003,175✔
449
  connectRsp.timeWhiteListVer = pUser->timeWhiteListVer;
3,003,801✔
450
  connectRsp.userId = pUser->uid;
3,003,711✔
451

452

453
  tstrncpy(connectRsp.sVer, td_version, sizeof(connectRsp.sVer));
3,003,715✔
454
  tstrncpy(connectRsp.user, user, sizeof(connectRsp.user));
3,003,742✔
455
  tstrncpy(connectRsp.tokenName, ti.name, sizeof(connectRsp.tokenName));
3,003,007✔
456
  (void)snprintf(connectRsp.sDetailVer, sizeof(connectRsp.sDetailVer), "ver:%s\nbuild:%s\ngitinfo:%s", td_version,
3,003,070✔
457
                 td_buildinfo, td_gitinfo);
458
  mndGetMnodeEpSet(pMnode, &connectRsp.epSet);
3,003,070✔
459

460
  int32_t contLen = tSerializeSConnectRsp(NULL, 0, &connectRsp);
3,003,801✔
461
  if (contLen < 0) {
3,002,402✔
462
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
463
  }
464
  void *pRsp = rpcMallocCont(contLen);
3,002,402✔
465
  if (pRsp == NULL) {
3,003,429✔
466
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
467
  }
468

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

475
  pReq->info.rspLen = contLen;
3,003,344✔
476
  pReq->info.rsp = pRsp;
3,003,302✔
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,003,742✔
479
  code = 0;
3,003,801✔
480

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

492
_OVER:
3,016,839✔
493
  if (code != 0) {
3,018,628✔
494
    mGError("user:%s, failed to login from %s since %s, line:%d, db:%s", user, ip, tstrerror(code), lino, connReq.db);
14,827✔
495
  }
496

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

501
  TAOS_RETURN(code);
3,018,628✔
502
}
503

504

505

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

509
  taosArrayDestroyEx(pConn->pQueries, tFreeClientHbQueryDesc);
38,410,301✔
510

511
  pConn->pQueries = pBasic->queryDesc;
38,415,641✔
512
  pConn->numOfQueries = pBasic->queryDesc ? taosArrayGetSize(pBasic->queryDesc) : 0;
38,415,228✔
513
  pBasic->queryDesc = NULL;
38,414,738✔
514

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

517
  taosWUnLockLatch(&pConn->queryLock);
38,418,706✔
518

519
  return TSDB_CODE_SUCCESS;
38,417,708✔
520
}
521

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

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

534
  SAppObj *pApp =
535
      taosCachePut(pMgmt->appCache, &pReq->appId, sizeof(pReq->appId), &app, sizeof(app), CACHE_OBJ_KEEP_TIME * 1000);
889,592✔
536
  if (pApp == NULL) {
889,592✔
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);
889,592✔
543
  return pApp;
889,592✔
544
}
545

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

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

552
  SAppObj *pApp = taosCacheAcquireByKey(pMgmt->appCache, &appId, sizeof(appId));
38,413,939✔
553
  if (pApp == NULL) {
38,409,045✔
554
    mDebug("app %" PRIx64 " not in cache", appId);
889,592✔
555
    return NULL;
889,592✔
556
  }
557

558
  pApp->lastAccessTimeMs = (uint64_t)taosGetTimestampMs();
37,518,238✔
559

560
  mTrace("app %" PRIx64 " acquired from cache", appId);
37,522,209✔
561
  return pApp;
37,513,190✔
562
}
563

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

568
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
38,407,807✔
569
  taosCacheRelease(pMgmt->appCache, (void **)&pApp, false);
38,414,172✔
570
}
571

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

582
  return pApp;
6,440✔
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,414,323✔
597
  int32_t    code = 0;
38,414,323✔
598
  SAppHbReq *pReq = &pHbReq->app;
38,414,323✔
599
  SAppObj   *pApp = mndAcquireApp(pMnode, pReq->appId);
38,416,372✔
600
  if (pApp == NULL) {
38,409,636✔
601
    pApp = mndCreateApp(pMnode, &connInfo->cliAddr, pReq);
889,592✔
602
    if (pApp == NULL) {
889,592✔
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);
889,592✔
609
      mndReleaseApp(pMnode, pApp);
889,592✔
610
      return TSDB_CODE_SUCCESS;
889,592✔
611
    }
612
  }
613

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

616
  mndReleaseApp(pMnode, pApp);
37,523,758✔
617

618
  return TSDB_CODE_SUCCESS;
37,512,034✔
619
}
620

621
static int32_t mndGetOnlineDnodeNum(SMnode *pMnode, int32_t *num) {
29,828,119✔
622
  SSdb      *pSdb = pMnode->pSdb;
29,828,119✔
623
  SDnodeObj *pDnode = NULL;
29,826,129✔
624
  int64_t    curMs = taosGetTimestampMs();
29,821,841✔
625
  void      *pIter = NULL;
29,821,841✔
626

627
  while (true) {
47,820,995✔
628
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
77,642,836✔
629
    if (pIter == NULL) break;
77,649,919✔
630

631
    bool online = mndIsDnodeOnline(pDnode, curMs);
47,820,635✔
632
    if (online) {
47,817,678✔
633
      (*num)++;
46,503,479✔
634
    }
635

636
    sdbRelease(pSdb, pDnode);
47,808,527✔
637
  }
638

639
  return TSDB_CODE_SUCCESS;
29,829,284✔
640
}
641

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

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

652
  if (pHbReq->query) {
38,386,099✔
653
    SQueryHbReqBasic *pBasic = pHbReq->query;
38,412,575✔
654
    SConnObj *pConn = mndAcquireConn(pMnode, pBasic->connId);
38,413,476✔
655
    if (pConn == NULL) {
38,403,280✔
656
      SRpcConnInfo  connInfo = pMsg->info.conn;
645,260✔
657
      const char* user = pHbReq->user;
645,260✔
658
      pConn = mndCreateConn(pMnode, user, pHbReq->tokenName, pHbReq->connKey.connType, &connInfo.cliAddr, pHbReq->app.pid,
645,260✔
659
                            pHbReq->app.name, 0, pHbReq->sVer);
645,260✔
660
      if (pConn == NULL) {
645,260✔
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);
645,260✔
667
      }
668
    }
669

670
    setUserInfo2Conn(pConn, pHbReq->userApp, pHbReq->userIp, pHbReq->cInfo);
38,403,280✔
671
    setUserInfoIpToConn(pConn, &pHbReq->userDualIp);
38,412,984✔
672

673
    SQueryHbRspBasic *rspBasic = taosMemoryCalloc(1, sizeof(SQueryHbRspBasic));
38,400,973✔
674
    if (rspBasic == NULL) {
38,406,557✔
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,406,557✔
682
    if (pConn->killed != 0) {
38,417,317✔
683
      rspBasic->killConnection = 1;
×
684
    }
685

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

691
    rspBasic->connId = pConn->id;
38,417,294✔
692
    rspBasic->connId = pConn->id;
38,417,839✔
693
    rspBasic->totalDnodes = pObj->totalDnodes;
38,415,182✔
694
    rspBasic->onlineDnodes = pObj->onlineDnodes;
38,416,261✔
695
    rspBasic->epSet = pObj->epSet;
38,416,215✔
696
    rspBasic->pQnodeList = taosArrayDup(pObj->pQnodeList, NULL);
38,416,215✔
697

698
    mndReleaseConn(pMnode, pConn, true);
38,418,236✔
699

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

705
  int32_t kvNum = taosHashGetSize(pHbReq->info);
38,418,035✔
706
  if (NULL == pHbReq->info || kvNum <= 0) {
38,418,035✔
707
    if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
22,899,779✔
708
      mError("failed to put rsp into array, but continue at this heartbeat");
×
709
    }
710
    return TSDB_CODE_SUCCESS;
11,450,088✔
711
  }
712

713
  hbRsp.info = taosArrayInit(kvNum, sizeof(SKv));
26,968,344✔
714
  if (NULL == hbRsp.info) {
26,967,601✔
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;
26,967,601✔
723
  int32_t          key = HEARTBEAT_KEY_DYN_VIEW;
26,968,344✔
724
  SDynViewVersion *pDynViewVer = NULL;
26,967,601✔
725
  SKv             *pKv = taosHashGet(pHbReq->info, &key, sizeof(key));
26,967,601✔
726
  if (NULL != pKv) {
26,968,344✔
727
    pDynViewVer = pKv->value;
6,510✔
728
    mTrace("recv view dyn ver, bootTs:%" PRId64 ", ver:%" PRIu64, pDynViewVer->svrBootTs, pDynViewVer->dynViewVer);
6,510✔
729

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

735
    if (needCheck) {
6,510✔
736
      SKv kv1 = {.key = HEARTBEAT_KEY_DYN_VIEW, .valueLen = sizeof(*pDynViewVer), .value = pRspVer};
5,374✔
737
      if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
10,748✔
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,
5,374✔
742
             pRspVer->dynViewVer);
743
    }
744
  }
745
#endif
746

747
  void *pIter = taosHashIterate(pHbReq->info, NULL);
26,968,344✔
748
  while (pIter != NULL) {
69,534,965✔
749
    SKv *kv = pIter;
42,566,621✔
750

751
    switch (kv->key) {
42,566,621✔
752
      case HEARTBEAT_KEY_USER_AUTHINFO: {
26,968,148✔
753
        void   *rspMsg = NULL;
26,968,148✔
754
        int32_t rspLen = 0;
26,968,148✔
755
        (void)mndValidateUserAuthInfo(pMnode, kv->value, kv->valueLen / sizeof(SUserAuthVersion), &rspMsg, &rspLen,
26,968,148✔
756
                                      pObj->ipWhiteListVer);
757
        if (rspMsg && rspLen > 0) {
26,968,072✔
758
          SKv kv1 = {.key = HEARTBEAT_KEY_USER_AUTHINFO, .valueLen = rspLen, .value = rspMsg};
26,968,064✔
759
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
53,931,353✔
760
            mError("failed to put kv into array, but continue at this heartbeat");
×
761
          }
762
        }
763
        break;
26,964,049✔
764
      }
765
      case HEARTBEAT_KEY_DBINFO: {
9,043,413✔
766
        void   *rspMsg = NULL;
9,043,413✔
767
        int32_t rspLen = 0;
9,043,413✔
768
        (void)mndValidateDbInfo(pMnode, kv->value, kv->valueLen / sizeof(SDbCacheInfo), &rspMsg, &rspLen);
9,043,413✔
769
        if (rspMsg && rspLen > 0) {
9,043,413✔
770
          SKv kv1 = {.key = HEARTBEAT_KEY_DBINFO, .valueLen = rspLen, .value = rspMsg};
9,043,413✔
771
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
18,086,826✔
772
            mError("failed to put kv into array, but continue at this heartbeat");
×
773
          }
774
        }
775
        break;
9,043,413✔
776
      }
777
      case HEARTBEAT_KEY_STBINFO: {
6,531,259✔
778
        void   *rspMsg = NULL;
6,531,259✔
779
        int32_t rspLen = 0;
6,531,259✔
780
        (void)mndValidateStbInfo(pMnode, kv->value, kv->valueLen / sizeof(SSTableVersion), &rspMsg, &rspLen);
6,531,259✔
781
        if (rspMsg && rspLen > 0) {
6,531,259✔
782
          SKv kv1 = {.key = HEARTBEAT_KEY_STBINFO, .valueLen = rspLen, .value = rspMsg};
6,531,259✔
783
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
13,062,518✔
784
            mError("failed to put kv into array, but continue at this heartbeat");
×
785
          }
786
        }
787
        break;
6,531,259✔
788
      }
789
#ifdef TD_ENTERPRISE
790
      case HEARTBEAT_KEY_DYN_VIEW: {
6,510✔
791
        break;
6,510✔
792
      }
793
      case HEARTBEAT_KEY_VIEWINFO: {
6,510✔
794
        if (!needCheck) {
6,510✔
795
          break;
1,136✔
796
        }
797

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

828
    pIter = taosHashIterate(pHbReq->info, pIter);
42,562,693✔
829
  }
830

831
  if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
53,935,768✔
832
    if (terrno != 0) code = terrno;
×
833
  }
834
  TAOS_RETURN(code);
26,967,424✔
835
}
836

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

842
  SClientHbBatchReq batchReq = {0};
29,827,368✔
843
  if (tDeserializeSClientHbBatchReq(pReq->pCont, pReq->contLen, &batchReq) != 0) {
29,826,840✔
844
    taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
×
845
    code = TSDB_CODE_INVALID_MSG;
×
846
    TAOS_RETURN(code);
×
847
  }
848

849
  SConnPreparedObj obj = {0};
29,828,181✔
850
  obj.totalDnodes = mndGetDnodeSize(pMnode);
29,828,717✔
851
  obj.ipWhiteListVer = batchReq.ipWhiteListVer;
29,827,902✔
852
  TAOS_CHECK_RETURN(mndGetOnlineDnodeNum(pMnode, &obj.onlineDnodes));
29,827,902✔
853
  mndGetMnodeEpSet(pMnode, &obj.epSet);
29,828,429✔
854
  TAOS_CHECK_RETURN(mndCreateQnodeList(pMnode, &obj.pQnodeList, -1));
29,829,284✔
855

856
  SClientHbBatchRsp batchRsp = {0};
29,821,424✔
857
  batchRsp.svrTimestamp = taosGetTimestampSec();
29,822,618✔
858
  batchRsp.rsps = taosArrayInit(0, sizeof(SClientHbRsp));
29,823,877✔
859
  if (batchRsp.rsps == NULL) {
29,820,577✔
860
    TAOS_CHECK_EXIT(terrno);
×
861
  }
862
  batchRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
29,820,577✔
863
  batchRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
29,820,577✔
864
  batchRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
29,820,577✔
865
  tstrncpy(batchRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
29,820,577✔
866
  batchRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
29,825,217✔
867
  batchRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
29,825,217✔
868
  batchRsp.enableAuditDelete = tsEnableAuditDelete;
29,825,217✔
869
  batchRsp.enableAuditSelect = tsEnableAuditSelect;
29,825,217✔
870
  batchRsp.enableAuditInsert = tsEnableAuditInsert;
29,825,217✔
871
  batchRsp.auditLevel = tsAuditLevel;
29,825,217✔
872
  batchRsp.enableStrongPass = tsEnableStrongPassword;
29,825,217✔
873
  batchRsp.sodInitial = (pMnode->sodPhase == TSDB_SOD_PHASE_INITIAL ? 1 : 0);
29,825,217✔
874
  batchRsp.macActive = (pMnode->macActive == MAC_MODE_MANDATORY ? 1 : 0);
29,826,380✔
875

876
  int32_t sz = taosArrayGetSize(batchReq.reqs);
29,828,464✔
877
  for (int i = 0; i < sz; i++) {
68,245,224✔
878
    SClientHbReq *pHbReq = taosArrayGet(batchReq.reqs, i);
38,416,322✔
879
    if (pHbReq->connKey.connType == CONN_TYPE__QUERY || pHbReq->connKey.connType == CONN_TYPE__TMQ) {
38,415,558✔
880
      TAOS_CHECK_EXIT(mndProcessQueryHeartBeat(pMnode, pReq, pHbReq, &batchRsp, &obj));
38,416,013✔
881
    } 
882
  }
883
  taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
29,828,902✔
884

885
  int32_t tlen = tSerializeSClientHbBatchRsp(NULL, 0, &batchRsp);
29,825,819✔
886
  if (tlen < 0) {
29,820,120✔
887
    TAOS_CHECK_EXIT(tlen);
×
888
  }
889
  void *buf = rpcMallocCont(tlen);
29,820,120✔
890
  if (!buf) {
29,825,365✔
891
    TAOS_CHECK_EXIT(terrno);
×
892
  }
893
  tlen = tSerializeSClientHbBatchRsp(buf, tlen, &batchRsp);
29,825,365✔
894
  if (tlen < 0) {
29,823,248✔
895
    rpcFreeCont(buf);
×
896
    TAOS_CHECK_EXIT(tlen);
×
897
  }
898
  pReq->info.rspLen = tlen;
29,823,248✔
899
  pReq->info.rsp = buf;
29,825,192✔
900
_exit:
29,824,482✔
901
  tFreeClientHbBatchRsp(&batchRsp);
902

903
  taosArrayDestroy(obj.pQnodeList);
29,821,619✔
904

905
  TAOS_RETURN(code);
29,820,383✔
906
}
907

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

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

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

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

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

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

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

953
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &killReq.connId, sizeof(uint32_t));
341✔
954
  if (pConn == NULL) {
341✔
955
    mError("connId:%u, failed to kill connection, conn not exist", killReq.connId);
341✔
956
    code = TSDB_CODE_MND_INVALID_CONN_ID;
341✔
957
    TAOS_RETURN(code);
341✔
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) {
14,582✔
995
  SMnode   *pMnode = pReq->info.node;
14,582✔
996
  SSdb     *pSdb = pMnode->pSdb;
14,582✔
997
  int32_t   numOfRows = 0;
14,582✔
998
  int32_t   cols = 0;
14,582✔
999
  int32_t   code = 0;
14,582✔
1000
  SConnObj *pConn = NULL;
14,582✔
1001

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

1008
  while (numOfRows < rows) {
58,559✔
1009
    pConn = mndGetNextConn(pMnode, pShow->pIter);
58,559✔
1010
    if (pConn == NULL) {
58,559✔
1011
      pShow->pIter = NULL;
14,582✔
1012
      break;
14,582✔
1013
    }
1014

1015
    if ((taosGetTimestampMs() - pConn->lastAccessTimeMs) > ((int64_t)CACHE_OBJ_KEEP_TIME * 1000)) {
43,977✔
1016
      continue;
9,549✔
1017
    }
1018

1019
    cols = 0;
34,428✔
1020

1021
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
34,428✔
1022
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->id, false);
34,428✔
1023
    if (code != 0) {
34,428✔
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};
34,428✔
1029
    STR_TO_VARSTR(user, pConn->user);
34,428✔
1030
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
34,428✔
1031
    code = colDataSetVal(pColInfo, numOfRows, (const char *)user, false);
34,428✔
1032
    if (code != 0) {
34,428✔
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];
34,036✔
1038
    STR_TO_VARSTR(app, pConn->app);
34,428✔
1039
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
34,428✔
1040
    code = colDataSetVal(pColInfo, numOfRows, (const char *)app, false);
34,428✔
1041
    if (code != 0) {
34,428✔
1042
      mError("failed to set app since %s", tstrerror(code));
×
1043
      return code;
×
1044
    }
1045

1046
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
34,428✔
1047
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->pid, false);
34,428✔
1048
    if (code != 0) {
34,428✔
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};
34,428✔
1054
    char endpoint[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
34,428✔
1055
    if (snprintf(addr, sizeof(addr), "%s:%d", IP_ADDR_STR(&pConn->addr), pConn->addr.port) >= sizeof(addr)) {
34,428✔
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);
34,428✔
1062

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

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

1077
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
34,428✔
1078
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->lastAccessTimeMs, false);
34,428✔
1079
    if (code != 0) {
34,428✔
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];
34,036✔
1085
    STR_TO_VARSTR(userApp, pConn->userApp);
34,428✔
1086
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
34,428✔
1087
    code = colDataSetVal(pColInfo, numOfRows, (const char *)userApp, false);
34,428✔
1088
    if (code != 0) {
34,428✔
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};
34,428✔
1094
    getUserIpFromConnObj(pConn, userIp);
34,428✔
1095

1096
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
34,428✔
1097
    code = colDataSetVal(pColInfo, numOfRows, (const char *)userIp, false);
34,428✔
1098
    if (code != 0) {
34,428✔
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];
34,036✔
1104
    STR_TO_VARSTR(ver, pConn->sVer);
34,428✔
1105
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
34,428✔
1106
    code = colDataSetVal(pColInfo, numOfRows, (const char *)ver, false);
34,428✔
1107
    if (code != 0) {
34,428✔
1108
      mError("failed to set ver since %s", tstrerror(code));
×
1109
      return code;
×
1110
    }
1111

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

1121
    char type[16 + VARSTR_HEADER_SIZE];
34,036✔
1122
    STR_TO_VARSTR(type, pConn->connType == CONN_TYPE__QUERY ? "QUERY" : (pConn->connType == CONN_TYPE__TMQ ? "TMQ" : "UNKNOWN"));
34,428✔
1123
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
34,428✔
1124
    code = colDataSetVal(pColInfo, numOfRows, (const char *)type, false);
34,428✔
1125
    if (code != 0) {
34,428✔
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};
34,428✔
1131
    STR_TO_VARSTR(tokenName, pConn->tokenName);
34,428✔
1132
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
34,428✔
1133
    code = colDataSetVal(pColInfo, numOfRows, (const char *)tokenName, false);
34,428✔
1134
    if (code != 0) {
34,428✔
1135
      mError("failed to set token name since %s", tstrerror(code));
×
1136
      return code;
×
1137
    }
1138

1139
    numOfRows++;
34,428✔
1140
  }
1141

1142
  pShow->numOfRows += numOfRows;
14,582✔
1143
  return numOfRows;
14,582✔
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,367✔
1154
                                    uint32_t rowsToPack) {
1155
  int32_t cols = 0;
64,367✔
1156
  int32_t code = 0;
64,367✔
1157
  taosRLockLatch(&pConn->queryLock);
64,367✔
1158
  int32_t numOfQueries = taosArrayGetSize(pConn->pQueries);
64,367✔
1159
  if (NULL == pConn->pQueries || numOfQueries <= offset) {
64,367✔
1160
    taosRUnLockLatch(&pConn->queryLock);
42,260✔
1161
    return 0;
42,260✔
1162
  }
1163

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

1170
    char queryId[26 + VARSTR_HEADER_SIZE] = {0};
22,107✔
1171
    (void)snprintf(&queryId[VARSTR_HEADER_SIZE], sizeof(queryId) - VARSTR_HEADER_SIZE, "%x:%" PRIx64, pConn->id,
22,107✔
1172
                   pQuery->reqRid);
1173
    varDataLen(queryId) = strlen(&queryId[VARSTR_HEADER_SIZE]);
22,107✔
1174
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,107✔
1175
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)queryId, false);
22,107✔
1176
    if (code != 0) {
22,107✔
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,107✔
1183
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->queryId, false);
22,107✔
1184
    if (code != 0) {
22,107✔
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,107✔
1191
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pConn->id, false);
22,107✔
1192
    if (code != 0) {
22,107✔
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,107✔
1199
    STR_TO_VARSTR(app, pConn->app);
22,107✔
1200
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,107✔
1201
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)app, false);
22,107✔
1202
    if (code != 0) {
22,107✔
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,107✔
1209
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pConn->pid, false);
22,107✔
1210
    if (code != 0) {
22,107✔
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,107✔
1217
    STR_TO_VARSTR(user, pConn->user);
22,107✔
1218
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,107✔
1219
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)user, false);
22,107✔
1220
    if (code != 0) {
22,107✔
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,107✔
1227
    char buf[IP_RESERVE_CAP] = {0};
22,107✔
1228
    (void)snprintf(buf, sizeof(buf), "%s:%d", IP_ADDR_STR(&pConn->addr), pConn->addr.port);
22,107✔
1229
    STR_TO_VARSTR(endpoint, buf);
22,107✔
1230
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,107✔
1231
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)endpoint, false);
22,107✔
1232
    if (code != 0) {
22,107✔
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,107✔
1239
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->stime, false);
22,107✔
1240
    if (code != 0) {
22,107✔
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,107✔
1247
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->useconds, false);
22,107✔
1248
    if (code != 0) {
22,107✔
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,107✔
1255
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->stableQuery, false);
22,107✔
1256
    if (code != 0) {
22,107✔
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,107✔
1263
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->isSubQuery, false);
22,107✔
1264
    if (code != 0) {
22,107✔
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,107✔
1271
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->subPlanNum, false);
22,107✔
1272
    if (code != 0) {
22,107✔
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,107✔
1279
    int64_t reserve = 128;
22,107✔
1280
    int32_t strSize = sizeof(subStatus);
22,107✔
1281
    int32_t offset = VARSTR_HEADER_SIZE;
22,107✔
1282
    for (int32_t i = 0; i < pQuery->subPlanNum && offset + reserve < strSize; ++i) {
44,211✔
1283
      if (i) {
22,104✔
UNCOV
1284
        offset += snprintf(subStatus + offset, sizeof(subStatus) - offset, ",");
×
1285
      }
1286
      if (offset + reserve >= strSize) break;
22,104✔
1287

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

1291
      char startBuf[32] = {0};
22,104✔
1292
      (void)snprintf(startBuf, sizeof(startBuf), "-");
22,104✔
1293
      if (pDesc->startTs > 0) {
22,104✔
1294
        time_t    startSec = (time_t)(pDesc->startTs / 1000000);
22,104✔
1295
        int32_t   startFrac = (int32_t)(pDesc->startTs % 1000000) / 1000;
22,104✔
1296
        struct tm startTm;
22,104✔
1297
        if (taosLocalTime(&startSec, &startTm, NULL, 0, NULL) != NULL) {
22,104✔
1298
          size_t n = taosStrfTime(startBuf, sizeof(startBuf), "%Y-%m-%d %H:%M:%S", &startTm);
22,104✔
1299
          if (tsnprintf(startBuf + n, sizeof(startBuf) - n, ".%03d", startFrac) < 0) {
22,104✔
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,104✔
1309
                          "%" PRIu64 ":%s:%s", pDesc->tid, pDesc->status, startBuf);
22,104✔
1310
    }
1311
    varDataLen(subStatus) = strlen(&subStatus[VARSTR_HEADER_SIZE]);
22,107✔
1312
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,107✔
1313
    code = colDataSetVal(pColInfo, curRowIndex, subStatus, (varDataLen(subStatus) == 0) ? true : false);
22,107✔
1314
    if (code != 0) {
22,107✔
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,107✔
1321
    STR_TO_VARSTR(sql, pQuery->sql);
22,107✔
1322
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,107✔
1323
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)sql, false);
22,107✔
1324
    if (code != 0) {
22,107✔
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,107✔
1331
    STR_TO_VARSTR(userApp, pConn->userApp);
22,107✔
1332
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,107✔
1333
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)userApp, false);
22,107✔
1334
    if (code != 0) {
22,107✔
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,107✔
1341
    getUserIpFromConnObj(pConn, userIp);
22,107✔
1342

1343
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,107✔
1344
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)userIp, false);
22,107✔
1345
    if (code != 0) {
22,107✔
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,107✔
1352
    char        phaseVarStr[64 + VARSTR_HEADER_SIZE];
22,107✔
1353
    STR_TO_VARSTR(phaseVarStr, phaseStr);
22,107✔
1354
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
22,107✔
1355
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)phaseVarStr, false);
22,107✔
1356
    if (code != 0) {
22,107✔
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,107✔
1363
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->phaseStartTime, false);
22,107✔
1364
    if (code != 0) {
22,107✔
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,107✔
1371
  }
1372

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

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

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

1389
  // means fetched some data last time for this conn
1390
  if (pShow->curIterPackedRows > 0) {
21,890✔
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) {
86,257✔
1400
    pConn = mndGetNextConn(pMnode, pShow->pIter);
86,257✔
1401
    if (pConn == NULL) {
86,257✔
1402
      pShow->pIter = NULL;
21,890✔
1403
      break;
21,890✔
1404
    }
1405

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

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

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

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

1435
    cols = 0;
3,220✔
1436

1437
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,220✔
1438
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->appId, false);
3,220✔
1439
    if (code != 0) {
3,220✔
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,220✔
1445
    char buf[IP_RESERVE_CAP] = {0};
3,220✔
1446
    snprintf(buf, sizeof(buf), "%s", IP_ADDR_STR(&pApp->cliAddr));
3,220✔
1447
    STR_TO_VARSTR(ip, buf);
3,220✔
1448

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

1456
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,220✔
1457
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->pid, false);
3,220✔
1458
    if (code != 0) {
3,220✔
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,220✔
1464
    (void)snprintf(&name[VARSTR_HEADER_SIZE], sizeof(name) - VARSTR_HEADER_SIZE, "%s", pApp->name);
3,220✔
1465
    varDataLen(name) = strlen(&name[VARSTR_HEADER_SIZE]);
3,220✔
1466
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,220✔
1467
    code = colDataSetVal(pColInfo, numOfRows, (const char *)name, false);
3,220✔
1468
    if (code != 0) {
3,220✔
1469
      mError("failed to set app name since %s", tstrerror(code));
×
1470
      return code;
×
1471
    }
1472

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

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

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

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

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

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

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

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

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

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

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

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

1553
  pShow->numOfRows += numOfRows;
3,220✔
1554
  return numOfRows;
3,220✔
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) {
59✔
1564
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
59✔
1565
  return taosCacheGetNumOfObj(pMgmt->connCache);
59✔
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