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

taosdata / TDengine / #5044

06 May 2026 02:35AM UTC coverage: 73.169% (+0.06%) from 73.107%
#5044

push

travis-ci

web-flow
feat: [6659794715] cpu limit (#35153)

244 of 275 new or added lines in 23 files covered. (88.73%)

526 existing lines in 141 files now uncovered.

277745 of 379596 relevant lines covered (73.17%)

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

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

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

130
  TAOS_RETURN(code);
490,090✔
131
}
132

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

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

146
static void getUserIpFromConnObj(SConnObj *pConn, char *dst) {
53,812✔
147
  static char *none = "0.0.0.0";
148
  if (pConn->userIp != 0 && pConn->userIp != INADDR_NONE) {
53,812✔
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) {
53,812✔
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;
53,812✔
160
}
161
static void setUserInfo2Conn(SConnObj *connObj, char *userApp, uint32_t userIp, char *cInfo) {
36,134,841✔
162
  if (connObj == NULL) {
36,134,841✔
163
    return;
×
164
  }
165
  tstrncpy(connObj->userApp, userApp, sizeof(connObj->userApp));
36,134,841✔
166
  tstrncpy(connObj->cInfo, cInfo, sizeof(connObj->cInfo));
36,128,203✔
167
  connObj->userIp = userIp;
36,134,043✔
168
}
169
static void setUserInfoIpToConn(SConnObj *connObj, SIpRange *pRange) {
36,130,852✔
170
  int32_t code = 0;
36,130,852✔
171
  if (connObj == NULL) {
36,130,852✔
172
    return;
×
173
  }
174

175
  code = tIpUintToStr(pRange, &connObj->userDualIp);
36,130,852✔
176
  if (code != 0) {
36,134,213✔
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,640,353✔
185
                               int32_t pid, const char *app, int64_t startTime, const char *sVer) {
186
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
3,640,353✔
187

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

192
  int32_t  len = tsnprintf(connStr, sizeof(connStr), "%s%d%d%d%s", user, ip, port, pid, app);
3,640,308✔
193
  uint32_t connId = mndGenerateUid(connStr, len);
3,639,950✔
194
  if (startTime == 0) startTime = taosGetTimestampMs();
4,305,545✔
195

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

210
  connObj.lastAccessTimeMs = connObj.loginTimeMs;
3,639,983✔
211
  tstrncpy(connObj.user, user, sizeof(connObj.user));
3,639,983✔
212
  tstrncpy(connObj.tokenName, tokenName, sizeof(connObj.tokenName));
3,640,041✔
213
  tstrncpy(connObj.app, app, sizeof(connObj.app));
3,639,668✔
214
  tstrncpy(connObj.sVer, sVer, sizeof(connObj.sVer));
3,640,350✔
215

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

228

229

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

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

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

241
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &connId, sizeof(connId));
36,135,896✔
242
  if (pConn == NULL) {
36,138,410✔
243
    mDebug("conn:%u, already destroyed", connId);
664,989✔
244
    return NULL;
664,989✔
245
  }
246

247
  pConn->lastAccessTimeMs = taosGetTimestampMs();
35,472,995✔
248
  mTrace("conn:%u, acquired from cache, data:%p", pConn->id, pConn);
35,472,328✔
249
  return pConn;
35,459,143✔
250
}
251

252
static void mndReleaseConn(SMnode *pMnode, SConnObj *pConn, bool extendLifespan) {
42,888,352✔
253
  if (pConn == NULL) return;
42,888,352✔
254
  mTrace("conn:%u, released from cache, data:%p", pConn->id, pConn);
42,873,576✔
255

256
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
42,873,576✔
257
  if (extendLifespan) taosCacheTryExtendLifeSpan(pMgmt->connCache, (void **)&pConn);
42,871,920✔
258
  taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
42,871,391✔
259
}
260

261
void *mndGetNextConn(SMnode *pMnode, SCacheIter *pIter) {
4,053,219✔
262
  SConnObj *pConn = NULL;
4,053,219✔
263
  bool      hasNext = taosCacheIterNext(pIter);
4,053,219✔
264
  if (hasNext) {
4,053,219✔
265
    size_t dataLen = 0;
3,964,401✔
266
    pConn = taosCacheIterGetData(pIter, &dataLen);
3,964,401✔
267
  } else {
268
    taosCacheDestroyIter(pIter);
88,818✔
269
  }
270

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

291
  int32_t    count = 0;
54,728✔
292
  SConnObj  *pConn = NULL;
54,728✔
293
  while ((pConn = mndGetNextConn(pMnode, pIter)) != NULL) {
3,811,781✔
294
    if (strncmp(pConn->user, user, TSDB_USER_LEN) == 0) {
3,757,053✔
295
      count++;
930,719✔
296
    }
297
    mndReleaseConn(pMnode, pConn, true);
3,757,053✔
298
  }
299

300
  return count;
54,728✔
301
}
302

303

304

305
static int32_t verifyPassword(SUserObj* pUser, const char* inputPass) {
2,982,586✔
306
  int32_t code = 0;
2,982,586✔
307

308
  const char* currPass = pUser->passwords[0].pass;
2,982,586✔
309
  char pass[TSDB_PASSWORD_LEN] = {0};
2,981,578✔
310
  (void)memcpy(pass, inputPass, TSDB_PASSWORD_LEN);
2,982,728✔
311
  pass[TSDB_PASSWORD_LEN - 1] = 0;
2,982,225✔
312

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

320
  // constant time comparison to prevent timing attack
321
  volatile uint8_t res = 0;
2,981,963✔
322
  for (size_t i = 0; i < sizeof(pass) - 1; i++) {
95,365,430✔
323
    res |= pass[i] ^ currPass[i];
92,382,304✔
324
  }
325

326
 return (res == 0) ? TSDB_CODE_SUCCESS: TSDB_CODE_MND_AUTH_FAILURE;
2,983,126✔
327
}
328

329

330

331
static bool verifyTotp(SUserObj *pUser, int32_t totpCode) {
2,975,594✔
332
  if (!mndIsTotpEnabledUser(pUser)) {
2,975,594✔
333
    return true;
2,972,067✔
334
  }
335
  return taosVerifyTotpCode(pUser->totpsecret, sizeof(pUser->totpsecret), totpCode, 6, 1) != 0;
4,351✔
336
}
337

338

339

340
static int32_t mndProcessConnectReq(SRpcMsg *pReq) {
2,990,143✔
341
  int32_t          code = 0, lino = 0;
2,990,143✔
342

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

357
  if (token != NULL && mndGetCachedTokenInfo(token, &ti) == NULL) {
2,989,538✔
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);
2,989,538✔
361
  TAOS_CHECK_GOTO(taosCheckVersionCompatibleFromStr(connReq.sVer, td_version, 3), &lino, _OVER);
2,989,684✔
362
  TAOS_CHECK_GOTO(tVerifyConnectReqSignature(&connReq), &lino, _OVER);
2,989,770✔
363
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, user, &pUser), &lino, _OVER);
2,990,140✔
364

365
  SLoginInfo li = {0};
2,988,025✔
366
  mndGetUserLoginInfo(user, &li);
2,988,025✔
367
  TAOS_CHECK_GOTO(mndCheckConnectPrivilege(pMnode, pUser, token, &li), &lino, _OVER);
2,988,606✔
368

369
  if (token != NULL || tsMndSkipGrant) {
2,983,613✔
370
    li.lastLoginTime= now;
3,519✔
371
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
3,519✔
372
      mndSetUserLoginInfo(user, &li);
1,495✔
373
    }
374
  } else if ((code = verifyPassword(pUser, connReq.passwd)) == TSDB_CODE_MND_AUTH_FAILURE) {
2,980,094✔
375
    if (pUser->failedLoginAttempts >= 0) {
6,551✔
376
      if (li.failedLoginCount >= pUser->failedLoginAttempts) {
5,620✔
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,620✔
381
      li.lastFailedLoginTime = now;
5,620✔
382
    }
383
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
6,561✔
384
      mndSetUserLoginInfo(user, &li);
6,561✔
385
    }
386
    TAOS_CHECK_GOTO(code, &lino, _OVER);
6,561✔
387
  } else if (code != TSDB_CODE_SUCCESS) {
2,976,176✔
388
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
389
  } else if (!verifyTotp(pUser, connReq.totpCode)) {
2,976,176✔
390
    TAOS_CHECK_GOTO(TSDB_CODE_MND_WRONG_TOTP_CODE, &lino, _OVER);
2,945✔
391
  } else {
392
    li.failedLoginCount = 0;
2,973,473✔
393
    li.lastLoginTime= now;
2,973,473✔
394
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
2,973,473✔
395
      mndSetUserLoginInfo(user, &li);
2,973,880✔
396
    }
397
  } 
398

399
  if (connReq.db[0] != 0) {
2,975,357✔
400
    char db[TSDB_DB_FNAME_LEN] = {0};
496,850✔
401
    (void)snprintf(db, TSDB_DB_FNAME_LEN, "%d%s%s", pUser->acctId, TS_PATH_DELIMITER, connReq.db);
496,850✔
402
    pDb = mndAcquireDb(pMnode, db);
496,850✔
403
    if (pDb == NULL) {
496,834✔
404
      if (0 != strcmp(connReq.db, TSDB_INFORMATION_SCHEMA_DB) && (0 != strcmp(connReq.db, TSDB_PERFORMANCE_SCHEMA_DB))) {
962✔
405
        TAOS_CHECK_GOTO(TSDB_CODE_MND_DB_NOT_EXIST, &lino, _OVER);
234✔
406
      }
407
    }
408

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

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

423
  SConnectRsp connectRsp = {0};
2,974,665✔
424
  connectRsp.acctId = pUser->acctId;
2,974,531✔
425
  connectRsp.superUser = pUser->superUser;
2,974,118✔
426
  connectRsp.sysInfo = pUser->sysInfo;
2,974,325✔
427
  connectRsp.minSecLevel = pUser->minSecLevel;
2,974,810✔
428
  connectRsp.maxSecLevel = pUser->maxSecLevel;
2,974,141✔
429
  connectRsp.sodInitial = (pMnode->sodPhase == TSDB_SOD_PHASE_INITIAL ? 1 : 0);
2,973,504✔
430
  connectRsp.macActive = (pMnode->macActive == MAC_MODE_MANDATORY ? 1 : 0);
2,974,574✔
431
  connectRsp.clusterId = pMnode->clusterId;
2,975,367✔
432
  connectRsp.connId = pConn->id;
2,973,904✔
433
  connectRsp.connType = connReq.connType;
2,972,708✔
434
  connectRsp.dnodeNum = mndGetDnodeSize(pMnode);
2,972,708✔
435
  connectRsp.svrTimestamp = taosGetTimestampSec();
2,975,364✔
436
  connectRsp.passVer = pUser->passVersion;
2,974,590✔
437
  connectRsp.authVer = pUser->authVersion;
2,974,590✔
438
  connectRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
2,974,278✔
439
  connectRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
2,974,278✔
440
  connectRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
2,974,278✔
441
  connectRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
2,974,278✔
442
  connectRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
2,974,278✔
443
  connectRsp.enableAuditDelete = tsEnableAuditDelete;
2,974,278✔
444
  connectRsp.enableAuditSelect = tsEnableAuditSelect;
2,974,278✔
445
  connectRsp.enableAuditInsert = tsEnableAuditInsert;
2,974,278✔
446
  connectRsp.auditLevel = tsAuditLevel;
2,974,278✔
447
  tstrncpy(connectRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
2,974,278✔
448
  connectRsp.whiteListVer = pUser->ipWhiteListVer;
2,975,347✔
449
  connectRsp.timeWhiteListVer = pUser->timeWhiteListVer;
2,974,422✔
450
  connectRsp.userId = pUser->uid;
2,975,344✔
451

452

453
  tstrncpy(connectRsp.sVer, td_version, sizeof(connectRsp.sVer));
2,975,367✔
454
  tstrncpy(connectRsp.user, user, sizeof(connectRsp.user));
2,974,522✔
455
  tstrncpy(connectRsp.tokenName, ti.name, sizeof(connectRsp.tokenName));
2,974,029✔
456
  (void)snprintf(connectRsp.sDetailVer, sizeof(connectRsp.sDetailVer), "ver:%s\nbuild:%s\ngitinfo:%s", td_version,
2,973,898✔
457
                 td_buildinfo, td_gitinfo);
458
  mndGetMnodeEpSet(pMnode, &connectRsp.epSet);
2,973,898✔
459

460
  int32_t contLen = tSerializeSConnectRsp(NULL, 0, &connectRsp);
2,975,367✔
461
  if (contLen < 0) {
2,973,375✔
462
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
463
  }
464
  void *pRsp = rpcMallocCont(contLen);
2,973,375✔
465
  if (pRsp == NULL) {
2,974,521✔
466
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
467
  }
468

469
  contLen = tSerializeSConnectRsp(pRsp, contLen, &connectRsp);
2,974,521✔
470
  if (contLen < 0) {
2,974,326✔
471
    rpcFreeCont(pRsp);
×
472
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
473
  }
474

475
  pReq->info.rspLen = contLen;
2,974,326✔
476
  pReq->info.rsp = pRsp;
2,975,348✔
477

478
  mGDebug("user:%s, login from %s:%d, conn:%u, app:%s, db:%s", user, ip, port, pConn->id, connReq.app, connReq.db);
2,974,922✔
479
  code = 0;
2,975,367✔
480

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

492
_OVER:
2,987,673✔
493
  if (code != 0) {
2,990,143✔
494
    mGError("user:%s, failed to login from %s since %s, line:%d, db:%s", user, ip, tstrerror(code), lino, connReq.db);
14,776✔
495
  }
496

497
  mndReleaseUser(pMnode, pUser);
2,990,143✔
498
  mndReleaseDb(pMnode, pDb);
2,990,143✔
499
  mndReleaseConn(pMnode, pConn, true);
2,990,143✔
500

501
  TAOS_RETURN(code);
2,990,143✔
502
}
503

504

505

506
static int32_t mndSaveQueryList(SConnObj *pConn, SQueryHbReqBasic *pBasic) {
36,132,437✔
507
  taosWLockLatch(&pConn->queryLock);
36,132,437✔
508

509
  taosArrayDestroyEx(pConn->pQueries, tFreeClientHbQueryDesc);
36,136,872✔
510

511
  pConn->pQueries = pBasic->queryDesc;
36,137,059✔
512
  pConn->numOfQueries = pBasic->queryDesc ? taosArrayGetSize(pBasic->queryDesc) : 0;
36,137,542✔
513
  pBasic->queryDesc = NULL;
36,136,559✔
514

515
  mDebug("queries updated in conn %u, num:%d", pConn->id, pConn->numOfQueries);
36,137,573✔
516

517
  taosWUnLockLatch(&pConn->queryLock);
36,143,629✔
518

519
  return TSDB_CODE_SUCCESS;
36,139,570✔
520
}
521

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

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

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

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

548
static SAppObj *mndAcquireApp(SMnode *pMnode, int64_t appId) {
36,137,248✔
549
  terrno = 0;
36,137,248✔
550
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
36,138,093✔
551

552
  SAppObj *pApp = taosCacheAcquireByKey(pMgmt->appCache, &appId, sizeof(appId));
36,139,828✔
553
  if (pApp == NULL) {
36,129,428✔
554
    mDebug("app %" PRIx64 " not in cache", appId);
824,862✔
555
    return NULL;
824,862✔
556
  }
557

558
  pApp->lastAccessTimeMs = (uint64_t)taosGetTimestampMs();
35,309,710✔
559

560
  mTrace("app %" PRIx64 " acquired from cache", appId);
35,309,105✔
561
  return pApp;
35,309,550✔
562
}
563

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

568
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
36,130,220✔
569
  taosCacheRelease(pMgmt->appCache, (void **)&pApp, false);
36,130,046✔
570
}
571

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

582
  return pApp;
6,570✔
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) {
36,137,794✔
597
  int32_t    code = 0;
36,137,794✔
598
  SAppHbReq *pReq = &pHbReq->app;
36,137,794✔
599
  SAppObj   *pApp = mndAcquireApp(pMnode, pReq->appId);
36,138,689✔
600
  if (pApp == NULL) {
36,129,440✔
601
    pApp = mndCreateApp(pMnode, &connInfo->cliAddr, pReq);
824,862✔
602
    if (pApp == NULL) {
824,862✔
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);
824,862✔
609
      mndReleaseApp(pMnode, pApp);
824,862✔
610
      return TSDB_CODE_SUCCESS;
824,862✔
611
    }
612
  }
613

614
  (void)memcpy(&pApp->summary, &pReq->summary, sizeof(pReq->summary));
35,304,578✔
615

616
  mndReleaseApp(pMnode, pApp);
35,312,612✔
617

618
  return TSDB_CODE_SUCCESS;
35,301,855✔
619
}
620

621
static int32_t mndGetOnlineDnodeNum(SMnode *pMnode, int32_t *num) {
29,435,693✔
622
  SSdb      *pSdb = pMnode->pSdb;
29,435,693✔
623
  SDnodeObj *pDnode = NULL;
29,437,113✔
624
  int64_t    curMs = taosGetTimestampMs();
29,432,357✔
625
  void      *pIter = NULL;
29,432,357✔
626

627
  while (true) {
47,291,701✔
628
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
76,724,058✔
629
    if (pIter == NULL) break;
76,728,958✔
630

631
    bool online = mndIsDnodeOnline(pDnode, curMs);
47,290,502✔
632
    if (online) {
47,289,395✔
633
      (*num)++;
45,918,832✔
634
    }
635

636
    sdbRelease(pSdb, pDnode);
47,286,020✔
637
  }
638

639
  return TSDB_CODE_SUCCESS;
29,438,456✔
640
}
641

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

648
  if (0 != pHbReq->app.appId) {
36,136,865✔
649
    TAOS_CHECK_RETURN(mndUpdateAppInfo(pMnode, pHbReq, &pMsg->info.conn));
36,128,816✔
650
  }
651

652
  if (pHbReq->query) {
36,123,713✔
653
    SQueryHbReqBasic *pBasic = pHbReq->query;
36,122,819✔
654
    SConnObj *pConn = mndAcquireConn(pMnode, pBasic->connId);
36,131,830✔
655
    if (pConn == NULL) {
36,125,394✔
656
      SRpcConnInfo  connInfo = pMsg->info.conn;
664,941✔
657
      const char* user = pHbReq->user;
664,941✔
658
      pConn = mndCreateConn(pMnode, user, pHbReq->tokenName, pHbReq->connKey.connType, &connInfo.cliAddr, pHbReq->app.pid,
664,941✔
659
                            pHbReq->app.name, 0, pHbReq->sVer);
664,941✔
660
      if (pConn == NULL) {
664,989✔
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);
664,989✔
667
      }
668
    }
669

670
    setUserInfo2Conn(pConn, pHbReq->userApp, pHbReq->userIp, pHbReq->cInfo);
36,125,442✔
671
    setUserInfoIpToConn(pConn, &pHbReq->userDualIp);
36,121,371✔
672

673
    SQueryHbRspBasic *rspBasic = taosMemoryCalloc(1, sizeof(SQueryHbRspBasic));
36,125,895✔
674
    if (rspBasic == NULL) {
36,119,942✔
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));
36,119,942✔
682
    if (pConn->killed != 0) {
36,139,792✔
683
      rspBasic->killConnection = 1;
×
684
    }
685

686
    if (pConn->killId != 0) {
36,140,896✔
687
      rspBasic->killRid = pConn->killId;
1,341✔
688
      pConn->killId = 0;
1,341✔
689
    }
690

691
    rspBasic->connId = pConn->id;
36,140,498✔
692
    rspBasic->connId = pConn->id;
36,140,742✔
693
    rspBasic->totalDnodes = pObj->totalDnodes;
36,140,896✔
694
    rspBasic->onlineDnodes = pObj->onlineDnodes;
36,140,896✔
695
    rspBasic->epSet = pObj->epSet;
36,139,946✔
696
    rspBasic->pQnodeList = taosArrayDup(pObj->pQnodeList, NULL);
36,141,294✔
697

698
    mndReleaseConn(pMnode, pConn, true);
36,139,654✔
699

700
    hbRsp.query = rspBasic;
36,140,655✔
701
  } else {
702
    mDebug("no query info in hb msg");
100✔
703
  }
704

705
  int32_t kvNum = taosHashGetSize(pHbReq->info);
36,140,755✔
706
  if (NULL == pHbReq->info || kvNum <= 0) {
36,140,605✔
707
    if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
18,960,746✔
708
      mError("failed to put rsp into array, but continue at this heartbeat");
×
709
    }
710
    return TSDB_CODE_SUCCESS;
9,480,743✔
711
  }
712

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

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

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

747
  void *pIter = taosHashIterate(pHbReq->info, NULL);
26,660,513✔
748
  while (pIter != NULL) {
69,029,958✔
749
    SKv *kv = pIter;
42,370,570✔
750

751
    switch (kv->key) {
42,370,570✔
752
      case HEARTBEAT_KEY_USER_AUTHINFO: {
26,659,992✔
753
        void   *rspMsg = NULL;
26,659,992✔
754
        int32_t rspLen = 0;
26,660,130✔
755
        (void)mndValidateUserAuthInfo(pMnode, kv->value, kv->valueLen / sizeof(SUserAuthVersion), &rspMsg, &rspLen,
26,659,992✔
756
                                      pObj->ipWhiteListVer);
757
        if (rspMsg && rspLen > 0) {
26,659,079✔
758
          SKv kv1 = {.key = HEARTBEAT_KEY_USER_AUTHINFO, .valueLen = rspLen, .value = rspMsg};
26,659,503✔
759
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
53,317,818✔
760
            mError("failed to put kv into array, but continue at this heartbeat");
×
761
          }
762
        }
763
        break;
26,658,315✔
764
      }
765
      case HEARTBEAT_KEY_DBINFO: {
9,209,141✔
766
        void   *rspMsg = NULL;
9,209,141✔
767
        int32_t rspLen = 0;
9,209,141✔
768
        (void)mndValidateDbInfo(pMnode, kv->value, kv->valueLen / sizeof(SDbCacheInfo), &rspMsg, &rspLen);
9,209,141✔
769
        if (rspMsg && rspLen > 0) {
9,209,141✔
770
          SKv kv1 = {.key = HEARTBEAT_KEY_DBINFO, .valueLen = rspLen, .value = rspMsg};
9,209,141✔
771
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
18,418,282✔
772
            mError("failed to put kv into array, but continue at this heartbeat");
×
773
          }
774
        }
775
        break;
9,209,141✔
776
      }
777
      case HEARTBEAT_KEY_STBINFO: {
6,478,286✔
778
        void   *rspMsg = NULL;
6,478,286✔
779
        int32_t rspLen = 0;
6,478,286✔
780
        (void)mndValidateStbInfo(pMnode, kv->value, kv->valueLen / sizeof(SSTableVersion), &rspMsg, &rspLen);
6,478,286✔
781
        if (rspMsg && rspLen > 0) {
6,478,286✔
782
          SKv kv1 = {.key = HEARTBEAT_KEY_STBINFO, .valueLen = rspLen, .value = rspMsg};
6,478,286✔
783
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
12,956,572✔
784
            mError("failed to put kv into array, but continue at this heartbeat");
×
785
          }
786
        }
787
        break;
6,478,286✔
788
      }
789
#ifdef TD_ENTERPRISE
790
      case HEARTBEAT_KEY_DYN_VIEW: {
6,890✔
791
        break;
6,890✔
792
      }
793
      case HEARTBEAT_KEY_VIEWINFO: {
6,890✔
794
        if (!needCheck) {
6,890✔
795
          break;
1,559✔
796
        }
797

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

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

831
  if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
53,320,039✔
832
    if (terrno != 0) code = terrno;
×
833
  }
834
  TAOS_RETURN(code);
26,660,651✔
835
}
836

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

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

849
  SConnPreparedObj obj = {0};
29,438,293✔
850
  obj.totalDnodes = mndGetDnodeSize(pMnode);
29,438,293✔
851
  obj.ipWhiteListVer = batchReq.ipWhiteListVer;
29,436,885✔
852
  TAOS_CHECK_RETURN(mndGetOnlineDnodeNum(pMnode, &obj.onlineDnodes));
29,436,885✔
853
  mndGetMnodeEpSet(pMnode, &obj.epSet);
29,436,181✔
854
  TAOS_CHECK_RETURN(mndCreateQnodeList(pMnode, &obj.pQnodeList, -1));
29,437,910✔
855

856
  SClientHbBatchRsp batchRsp = {0};
29,431,753✔
857
  batchRsp.svrTimestamp = taosGetTimestampSec();
29,432,471✔
858
  batchRsp.rsps = taosArrayInit(0, sizeof(SClientHbRsp));
29,431,063✔
859
  if (batchRsp.rsps == NULL) {
29,428,285✔
860
    TAOS_CHECK_EXIT(terrno);
×
861
  }
862
  batchRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
29,428,285✔
863
  batchRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
29,428,285✔
864
  batchRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
29,428,285✔
865
  tstrncpy(batchRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
29,428,285✔
866
  batchRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
29,432,163✔
867
  batchRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
29,432,163✔
868
  batchRsp.enableAuditDelete = tsEnableAuditDelete;
29,432,163✔
869
  batchRsp.enableAuditSelect = tsEnableAuditSelect;
29,432,163✔
870
  batchRsp.enableAuditInsert = tsEnableAuditInsert;
29,432,163✔
871
  batchRsp.auditLevel = tsAuditLevel;
29,432,163✔
872
  batchRsp.enableStrongPass = tsEnableStrongPassword;
29,432,163✔
873
  batchRsp.sodInitial = (pMnode->sodPhase == TSDB_SOD_PHASE_INITIAL ? 1 : 0);
29,432,163✔
874
  batchRsp.macActive = (pMnode->macActive == MAC_MODE_MANDATORY ? 1 : 0);
29,436,891✔
875

876
  int32_t sz = taosArrayGetSize(batchReq.reqs);
29,424,863✔
877
  for (int i = 0; i < sz; i++) {
65,574,886✔
878
    SClientHbReq *pHbReq = taosArrayGet(batchReq.reqs, i);
36,137,495✔
879
    if (pHbReq->connKey.connType == CONN_TYPE__QUERY || pHbReq->connKey.connType == CONN_TYPE__TMQ) {
36,136,745✔
880
      TAOS_CHECK_EXIT(mndProcessQueryHeartBeat(pMnode, pReq, pHbReq, &batchRsp, &obj));
36,137,069✔
881
    } 
882
  }
883
  taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
29,437,391✔
884

885
  int32_t tlen = tSerializeSClientHbBatchRsp(NULL, 0, &batchRsp);
29,435,985✔
886
  if (tlen < 0) {
29,427,074✔
887
    TAOS_CHECK_EXIT(tlen);
×
888
  }
889
  void *buf = rpcMallocCont(tlen);
29,427,074✔
890
  if (!buf) {
29,431,604✔
891
    TAOS_CHECK_EXIT(terrno);
×
892
  }
893
  tlen = tSerializeSClientHbBatchRsp(buf, tlen, &batchRsp);
29,431,604✔
894
  if (tlen < 0) {
29,433,023✔
895
    rpcFreeCont(buf);
×
896
    TAOS_CHECK_EXIT(tlen);
×
897
  }
898
  pReq->info.rspLen = tlen;
29,433,023✔
899
  pReq->info.rsp = buf;
29,434,123✔
900
_exit:
29,435,066✔
901
  tFreeClientHbBatchRsp(&batchRsp);
902

903
  taosArrayDestroy(obj.pQnodeList);
29,434,543✔
904

905
  TAOS_RETURN(code);
29,434,773✔
906
}
907

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

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

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

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

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

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

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

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

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

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

1015
    if ((taosGetTimestampMs() - pConn->lastAccessTimeMs) > ((int64_t)CACHE_OBJ_KEEP_TIME * 1000)) {
151,744✔
1016
      continue;
119,478✔
1017
    }
1018

1019
    cols = 0;
32,266✔
1020

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

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

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

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

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

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

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

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

1139
    numOfRows++;
32,266✔
1140
  }
1141

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

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

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

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

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

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

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

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

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

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

1406
    int32_t packedRows = packQueriesIntoBlock(pShow, pConn, pBlock, 0, rows - numOfRows);
55,604✔
1407
    pShow->curIterPackedRows = packedRows;
55,604✔
1408
    numOfRows += packedRows;
55,604✔
1409
  }
1410
  pShow->numOfRows += numOfRows;
19,516✔
1411
  return numOfRows;
19,516✔
1412
}
1413

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

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

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

1435
    cols = 0;
3,285✔
1436

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1553
  pShow->numOfRows += numOfRows;
3,285✔
1554
  return numOfRows;
3,285✔
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