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

taosdata / TDengine / #5034

24 Apr 2026 11:25AM UTC coverage: 73.058%. Remained the same
#5034

push

travis-ci

web-flow
merge: from main to 3.0 branch #35224

merge: from main to 3.0 branch[manual-only]

1336 of 1975 new or added lines in 48 files covered. (67.65%)

14149 existing lines in 164 files now uncovered.

275896 of 377640 relevant lines covered (73.06%)

132944440.29 hits per line

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

77.05
/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) {
480,119✔
98
  int32_t       code = 0;
480,119✔
99
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
480,119✔
100

101
  // in ms
102
  int32_t checkTime = CACHE_OBJ_KEEP_TIME * 1000;
480,119✔
103
  pMgmt->connCache = taosCacheInit(TSDB_DATA_TYPE_UINT, checkTime, false, (__cache_free_fn_t)mndFreeConn, "conn");
480,119✔
104
  if (pMgmt->connCache == NULL) {
480,119✔
UNCOV
105
    code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
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");
480,119✔
111
  if (pMgmt->appCache == NULL) {
480,119✔
UNCOV
112
    code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
113
    mError("failed to alloc profile cache since %s", terrstr());
×
114
    TAOS_RETURN(code);
×
115
  }
116

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

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

130
  TAOS_RETURN(code);
480,119✔
131
}
132

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

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

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

153
  if (pConn->userDualIp.ipv4[0] != 0 && strncmp(pConn->userDualIp.ipv4, none, strlen(none)) != 0) {
59,939✔
UNCOV
154
    char   *ipstr = IP_ADDR_STR(&pConn->userDualIp);
×
UNCOV
155
    int32_t len = strlen(ipstr);
×
156
    memcpy(varDataVal(dst), ipstr, len);
×
157
    varDataLen(dst) = len;
×
158
  }
159
  return;
59,939✔
160
}
161
static void setUserInfo2Conn(SConnObj *connObj, char *userApp, uint32_t userIp, char *cInfo) {
36,679,853✔
162
  if (connObj == NULL) {
36,679,853✔
UNCOV
163
    return;
×
164
  }
165
  tstrncpy(connObj->userApp, userApp, sizeof(connObj->userApp));
36,679,853✔
166
  tstrncpy(connObj->cInfo, cInfo, sizeof(connObj->cInfo));
36,673,588✔
167
  connObj->userIp = userIp;
36,674,003✔
168
}
169
static void setUserInfoIpToConn(SConnObj *connObj, SIpRange *pRange) {
36,676,037✔
170
  int32_t code = 0;
36,676,037✔
171
  if (connObj == NULL) {
36,676,037✔
UNCOV
172
    return;
×
173
  }
174

175
  code = tIpUintToStr(pRange, &connObj->userDualIp);
36,676,037✔
176
  if (code != 0) {
36,676,272✔
UNCOV
177
    mError("conn:%u, failed to set user ip to conn since %s", connObj->id, tstrerror(code));
×
UNCOV
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,606,338✔
185
                               int32_t pid, const char *app, int64_t startTime, const char *sVer) {
186
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
3,606,338✔
187

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

192
  int32_t  len = tsnprintf(connStr, sizeof(connStr), "%s%d%d%d%s", user, ip, port, pid, app);
3,606,338✔
193
  uint32_t connId = mndGenerateUid(connStr, len);
3,606,301✔
194
  if (startTime == 0) startTime = taosGetTimestampMs();
4,242,022✔
195

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

210
  connObj.lastAccessTimeMs = connObj.loginTimeMs;
3,606,078✔
211
  tstrncpy(connObj.user, user, sizeof(connObj.user));
3,606,078✔
212
  tstrncpy(connObj.tokenName, tokenName, sizeof(connObj.tokenName));
3,606,116✔
213
  tstrncpy(connObj.app, app, sizeof(connObj.app));
3,606,311✔
214
  tstrncpy(connObj.sVer, sVer, sizeof(connObj.sVer));
3,606,300✔
215

216
  SConnObj *pConn =
217
      taosCachePut(pMgmt->connCache, &connId, sizeof(uint32_t), &connObj, sizeof(connObj), CACHE_OBJ_KEEP_TIME * 1000);
3,606,274✔
218
  if (pConn == NULL) {
3,606,311✔
UNCOV
219
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
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,606,311✔
224
    return pConn;
3,606,311✔
225
  }
226
}
227

228

229

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

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

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

241
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &connId, sizeof(connId));
36,677,398✔
242
  if (pConn == NULL) {
36,681,646✔
243
    mDebug("conn:%u, already destroyed", connId);
635,292✔
244
    return NULL;
635,292✔
245
  }
246

247
  pConn->lastAccessTimeMs = taosGetTimestampMs();
36,045,252✔
248
  mTrace("conn:%u, acquired from cache, data:%p", pConn->id, pConn);
36,044,341✔
249
  return pConn;
36,032,666✔
250
}
251

252
static void mndReleaseConn(SMnode *pMnode, SConnObj *pConn, bool extendLifespan) {
43,364,455✔
253
  if (pConn == NULL) return;
43,364,455✔
254
  mTrace("conn:%u, released from cache, data:%p", pConn->id, pConn);
43,349,908✔
255

256
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
43,349,908✔
257
  if (extendLifespan) taosCacheTryExtendLifeSpan(pMgmt->connCache, (void **)&pConn);
43,349,908✔
258
  taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
43,347,483✔
259
}
260

261
void *mndGetNextConn(SMnode *pMnode, SCacheIter *pIter) {
4,130,107✔
262
  SConnObj *pConn = NULL;
4,130,107✔
263
  bool      hasNext = taosCacheIterNext(pIter);
4,130,107✔
264
  if (hasNext) {
4,130,107✔
265
    size_t dataLen = 0;
4,039,130✔
266
    pConn = taosCacheIterGetData(pIter, &dataLen);
4,039,130✔
267
  } else {
268
    taosCacheDestroyIter(pIter);
90,977✔
269
  }
270

271
  return pConn;
4,130,107✔
272
}
273

UNCOV
274
static void mndCancelGetNextConn(SMnode *pMnode, void *pIter) {
×
UNCOV
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) {
53,721✔
284
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
53,721✔
285
  SCacheIter   *pIter = taosCacheCreateIter(pMgmt->connCache);
53,721✔
286
  if (pIter == NULL) {
53,721✔
UNCOV
287
    mError("failed to create conn cache iterator");
×
UNCOV
288
    return -1;
×
289
  }
290

291
  int32_t    count = 0;
53,721✔
292
  SConnObj  *pConn = NULL;
53,721✔
293
  while ((pConn = mndGetNextConn(pMnode, pIter)) != NULL) {
3,748,803✔
294
    if (strncmp(pConn->user, user, TSDB_USER_LEN) == 0) {
3,695,082✔
295
      count++;
913,381✔
296
    }
297
    mndReleaseConn(pMnode, pConn, true);
3,695,082✔
298
  }
299

300
  return count;
53,721✔
301
}
302

303

304

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

308
  const char* currPass = pUser->passwords[0].pass;
2,979,074✔
309
  char pass[TSDB_PASSWORD_LEN] = {0};
2,978,427✔
310
  (void)memcpy(pass, inputPass, TSDB_PASSWORD_LEN);
2,978,714✔
311
  pass[TSDB_PASSWORD_LEN - 1] = 0;
2,978,786✔
312

313
  if (pUser->passEncryptAlgorithm != 0 && strlen(tsDataKey) > 0) {
2,978,786✔
314
    code = mndEncryptPass(pass, pUser->salt, NULL);
2,291✔
315
    if (code != TSDB_CODE_SUCCESS) {
2,474✔
UNCOV
316
      return code;
×
317
    }
318
  }
319

320
  // constant time comparison to prevent timing attack
321
  volatile uint8_t res = 0;
2,978,546✔
322
  for (size_t i = 0; i < sizeof(pass) - 1; i++) {
95,303,309✔
323
    res |= pass[i] ^ currPass[i];
92,324,589✔
324
  }
325

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

329

330

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

338

339

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

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

357
  if (token != NULL && mndGetCachedTokenInfo(token, &ti) == NULL) {
2,985,344✔
UNCOV
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,985,344✔
361
  TAOS_CHECK_GOTO(taosCheckVersionCompatibleFromStr(connReq.sVer, td_version, 3), &lino, _OVER);
2,985,529✔
362
  TAOS_CHECK_GOTO(tVerifyConnectReqSignature(&connReq), &lino, _OVER);
2,985,566✔
363
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, user, &pUser), &lino, _OVER);
2,985,112✔
364

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

369
  if (token != NULL || tsMndSkipGrant) {
2,980,780✔
370
    li.lastLoginTime= now;
1,835✔
371
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
1,835✔
372
      mndSetUserLoginInfo(user, &li);
1,484✔
373
    }
374
  } else if ((code = verifyPassword(pUser, connReq.passwd)) == TSDB_CODE_MND_AUTH_FAILURE) {
2,978,945✔
375
    if (pUser->failedLoginAttempts >= 0) {
6,195✔
376
      if (li.failedLoginCount >= pUser->failedLoginAttempts) {
5,477✔
377
        // if we can get here, it means the lock time has passed, so reset the counter
UNCOV
378
        li.failedLoginCount = 0;
×
379
      }
380
      li.failedLoginCount++;
5,477✔
381
      li.lastFailedLoginTime = now;
5,477✔
382
    }
383
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
6,402✔
384
      mndSetUserLoginInfo(user, &li);
6,402✔
385
    }
386
    TAOS_CHECK_GOTO(code, &lino, _OVER);
6,402✔
387
  } else if (code != TSDB_CODE_SUCCESS) {
2,972,635✔
UNCOV
388
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
389
  } else if (!verifyTotp(pUser, connReq.totpCode)) {
2,972,635✔
390
    TAOS_CHECK_GOTO(TSDB_CODE_MND_WRONG_TOTP_CODE, &lino, _OVER);
2,862✔
391
  } else {
392
    li.failedLoginCount = 0;
2,970,059✔
393
    li.lastLoginTime= now;
2,970,059✔
394
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
2,970,059✔
395
      mndSetUserLoginInfo(user, &li);
2,969,810✔
396
    }
397
  } 
398

399
  if (connReq.db[0] != 0) {
2,971,792✔
400
    char db[TSDB_DB_FNAME_LEN] = {0};
500,131✔
401
    (void)snprintf(db, TSDB_DB_FNAME_LEN, "%d%s%s", pUser->acctId, TS_PATH_DELIMITER, connReq.db);
500,131✔
402
    pDb = mndAcquireDb(pMnode, db);
500,131✔
403
    if (pDb == NULL) {
500,104✔
404
      if (0 != strcmp(connReq.db, TSDB_INFORMATION_SCHEMA_DB) && (0 != strcmp(connReq.db, TSDB_PERFORMANCE_SCHEMA_DB))) {
1,041✔
405
        TAOS_CHECK_GOTO(TSDB_CODE_MND_DB_NOT_EXIST, &lino, _OVER);
333✔
406
      }
407
    }
408

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

412
  if (connReq.connType == CONN_TYPE__AUTH_TEST) {
2,971,046✔
413
    code = 0;
×
UNCOV
414
    goto _OVER;
×
415
  }
416

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

423
  SConnectRsp connectRsp = {0};
2,971,046✔
424
  connectRsp.acctId = pUser->acctId;
2,971,046✔
425
  connectRsp.superUser = pUser->superUser;
2,970,695✔
426
  connectRsp.sysInfo = pUser->sysInfo;
2,971,035✔
427
  connectRsp.minSecLevel = pUser->minSecLevel;
2,971,019✔
428
  connectRsp.maxSecLevel = pUser->maxSecLevel;
2,970,955✔
429
  connectRsp.sodInitial = (pMnode->sodPhase == TSDB_SOD_PHASE_INITIAL ? 1 : 0);
2,970,797✔
430
  connectRsp.macActive = (pMnode->macActive == MAC_MODE_MANDATORY ? 1 : 0);
2,970,992✔
431
  connectRsp.clusterId = pMnode->clusterId;
2,970,965✔
432
  connectRsp.connId = pConn->id;
2,970,955✔
433
  connectRsp.connType = connReq.connType;
2,970,917✔
434
  connectRsp.dnodeNum = mndGetDnodeSize(pMnode);
2,970,917✔
435
  connectRsp.svrTimestamp = taosGetTimestampSec();
2,971,046✔
436
  connectRsp.passVer = pUser->passVersion;
2,971,046✔
437
  connectRsp.authVer = pUser->authVersion;
2,971,046✔
438
  connectRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
2,971,007✔
439
  connectRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
2,971,007✔
440
  connectRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
2,971,007✔
441
  connectRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
2,971,007✔
442
  connectRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
2,971,007✔
443
  connectRsp.enableAuditDelete = tsEnableAuditDelete;
2,971,007✔
444
  connectRsp.enableAuditSelect = tsEnableAuditSelect;
2,971,007✔
445
  connectRsp.enableAuditInsert = tsEnableAuditInsert;
2,971,007✔
446
  connectRsp.auditLevel = tsAuditLevel;
2,971,007✔
447
  tstrncpy(connectRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
2,971,007✔
448
  connectRsp.whiteListVer = pUser->ipWhiteListVer;
2,970,768✔
449
  connectRsp.timeWhiteListVer = pUser->timeWhiteListVer;
2,970,797✔
450
  connectRsp.userId = pUser->uid;
2,970,238✔
451

452

453
  tstrncpy(connectRsp.sVer, td_version, sizeof(connectRsp.sVer));
2,971,019✔
454
  tstrncpy(connectRsp.user, user, sizeof(connectRsp.user));
2,970,795✔
455
  tstrncpy(connectRsp.tokenName, ti.name, sizeof(connectRsp.tokenName));
2,970,988✔
456
  (void)snprintf(connectRsp.sDetailVer, sizeof(connectRsp.sDetailVer), "ver:%s\nbuild:%s\ngitinfo:%s", td_version,
2,970,360✔
457
                 td_buildinfo, td_gitinfo);
458
  mndGetMnodeEpSet(pMnode, &connectRsp.epSet);
2,970,360✔
459

460
  int32_t contLen = tSerializeSConnectRsp(NULL, 0, &connectRsp);
2,971,046✔
461
  if (contLen < 0) {
2,970,650✔
UNCOV
462
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
463
  }
464
  void *pRsp = rpcMallocCont(contLen);
2,970,650✔
465
  if (pRsp == NULL) {
2,970,524✔
UNCOV
466
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
467
  }
468

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

475
  pReq->info.rspLen = contLen;
2,970,541✔
476
  pReq->info.rsp = pRsp;
2,970,963✔
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,970,706✔
479
  code = 0;
2,971,046✔
480

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

492
_OVER:
2,983,606✔
493
  if (code != 0) {
2,985,593✔
494
    mGError("user:%s, failed to login from %s since %s, line:%d, db:%s", user, ip, tstrerror(code), lino, connReq.db);
14,547✔
495
  }
496

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

501
  TAOS_RETURN(code);
2,985,566✔
502
}
503

504

505

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

509
  taosArrayDestroyEx(pConn->pQueries, tFreeClientHbQueryDesc);
36,683,380✔
510

511
  pConn->pQueries = pBasic->queryDesc;
36,677,281✔
512
  pConn->numOfQueries = pBasic->queryDesc ? taosArrayGetSize(pBasic->queryDesc) : 0;
36,679,018✔
513
  pBasic->queryDesc = NULL;
36,679,852✔
514

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

517
  taosWUnLockLatch(&pConn->queryLock);
36,685,869✔
518

519
  return TSDB_CODE_SUCCESS;
36,681,894✔
520
}
521

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

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

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

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

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

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

552
  SAppObj *pApp = taosCacheAcquireByKey(pMgmt->appCache, &appId, sizeof(appId));
36,679,808✔
553
  if (pApp == NULL) {
36,677,443✔
554
    mDebug("app %" PRIx64 " not in cache", appId);
867,121✔
555
    return NULL;
867,121✔
556
  }
557

558
  pApp->lastAccessTimeMs = (uint64_t)taosGetTimestampMs();
35,809,955✔
559

560
  mTrace("app %" PRIx64 " acquired from cache", appId);
35,808,362✔
561
  return pApp;
35,813,207✔
562
}
563

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

568
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
36,675,030✔
569
  taosCacheRelease(pMgmt->appCache, (void **)&pApp, false);
36,674,187✔
570
}
571

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

582
  return pApp;
6,414✔
583
}
584

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

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

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

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

616
  mndReleaseApp(pMnode, pApp);
35,813,519✔
617

618
  return TSDB_CODE_SUCCESS;
35,805,685✔
619
}
620

621
static int32_t mndGetOnlineDnodeNum(SMnode *pMnode, int32_t *num) {
29,302,261✔
622
  SSdb      *pSdb = pMnode->pSdb;
29,302,261✔
623
  SDnodeObj *pDnode = NULL;
29,301,857✔
624
  int64_t    curMs = taosGetTimestampMs();
29,300,422✔
625
  void      *pIter = NULL;
29,300,422✔
626

627
  while (true) {
47,015,466✔
628
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
76,315,888✔
629
    if (pIter == NULL) break;
76,316,978✔
630

631
    bool online = mndIsDnodeOnline(pDnode, curMs);
47,013,024✔
632
    if (online) {
47,010,178✔
633
      (*num)++;
45,711,234✔
634
    }
635

636
    sdbRelease(pSdb, pDnode);
47,010,913✔
637
  }
638

639
  return TSDB_CODE_SUCCESS;
29,303,954✔
640
}
641

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

648
  if (0 != pHbReq->app.appId) {
36,681,281✔
649
    TAOS_CHECK_RETURN(mndUpdateAppInfo(pMnode, pHbReq, &pMsg->info.conn));
36,678,374✔
650
  }
651

652
  if (pHbReq->query) {
36,669,570✔
653
    SQueryHbReqBasic *pBasic = pHbReq->query;
36,676,266✔
654
    SConnObj *pConn = mndAcquireConn(pMnode, pBasic->connId);
36,674,085✔
655
    if (pConn == NULL) {
36,671,724✔
656
      SRpcConnInfo  connInfo = pMsg->info.conn;
635,292✔
657
      const char* user = pHbReq->user;
635,292✔
658
      pConn = mndCreateConn(pMnode, user, pHbReq->tokenName, pHbReq->connKey.connType, &connInfo.cliAddr, pHbReq->app.pid,
635,292✔
659
                            pHbReq->app.name, 0, pHbReq->sVer);
635,292✔
660
      if (pConn == NULL) {
635,292✔
UNCOV
661
        mError("user:%s, conn:%u is freed and failed to create new since %s", user, pBasic->connId, terrstr());
×
UNCOV
662
        code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
UNCOV
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);
635,292✔
667
      }
668
    }
669

670
    setUserInfo2Conn(pConn, pHbReq->userApp, pHbReq->userIp, pHbReq->cInfo);
36,671,724✔
671
    setUserInfoIpToConn(pConn, &pHbReq->userDualIp);
36,667,690✔
672

673
    SQueryHbRspBasic *rspBasic = taosMemoryCalloc(1, sizeof(SQueryHbRspBasic));
36,665,251✔
674
    if (rspBasic == NULL) {
36,666,350✔
UNCOV
675
      mndReleaseConn(pMnode, pConn, true);
×
UNCOV
676
      code = terrno;
×
UNCOV
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,666,350✔
682
    if (pConn->killed != 0) {
36,681,894✔
UNCOV
683
      rspBasic->killConnection = 1;
×
684
    }
685

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

691
    rspBasic->connId = pConn->id;
36,682,597✔
692
    rspBasic->connId = pConn->id;
36,683,541✔
693
    rspBasic->totalDnodes = pObj->totalDnodes;
36,683,380✔
694
    rspBasic->onlineDnodes = pObj->onlineDnodes;
36,682,831✔
695
    rspBasic->epSet = pObj->epSet;
36,682,460✔
696
    rspBasic->pQnodeList = taosArrayDup(pObj->pQnodeList, NULL);
36,682,992✔
697

698
    mndReleaseConn(pMnode, pConn, true);
36,683,231✔
699

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

705
  int32_t kvNum = taosHashGetSize(pHbReq->info);
36,683,980✔
706
  if (NULL == pHbReq->info || kvNum <= 0) {
36,683,737✔
707
    if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
20,282,995✔
UNCOV
708
      mError("failed to put rsp into array, but continue at this heartbeat");
×
709
    }
710
    return TSDB_CODE_SUCCESS;
10,141,660✔
711
  }
712

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

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

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

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

747
  void *pIter = taosHashIterate(pHbReq->info, NULL);
26,542,316✔
748
  while (pIter != NULL) {
68,604,885✔
749
    SKv *kv = pIter;
42,063,217✔
750

751
    switch (kv->key) {
42,063,217✔
752
      case HEARTBEAT_KEY_USER_AUTHINFO: {
26,542,269✔
753
        void   *rspMsg = NULL;
26,542,269✔
754
        int32_t rspLen = 0;
26,542,269✔
755
        (void)mndValidateUserAuthInfo(pMnode, kv->value, kv->valueLen / sizeof(SUserAuthVersion), &rspMsg, &rspLen,
26,542,269✔
756
                                      pObj->ipWhiteListVer);
757
        if (rspMsg && rspLen > 0) {
26,541,798✔
758
          SKv kv1 = {.key = HEARTBEAT_KEY_USER_AUTHINFO, .valueLen = rspLen, .value = rspMsg};
26,542,269✔
759
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
53,083,789✔
UNCOV
760
            mError("failed to put kv into array, but continue at this heartbeat");
×
761
          }
762
        }
763
        break;
26,541,149✔
764
      }
765
      case HEARTBEAT_KEY_DBINFO: {
9,244,522✔
766
        void   *rspMsg = NULL;
9,244,522✔
767
        int32_t rspLen = 0;
9,244,522✔
768
        (void)mndValidateDbInfo(pMnode, kv->value, kv->valueLen / sizeof(SDbCacheInfo), &rspMsg, &rspLen);
9,244,522✔
769
        if (rspMsg && rspLen > 0) {
9,244,522✔
770
          SKv kv1 = {.key = HEARTBEAT_KEY_DBINFO, .valueLen = rspLen, .value = rspMsg};
9,244,522✔
771
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
18,489,044✔
UNCOV
772
            mError("failed to put kv into array, but continue at this heartbeat");
×
773
          }
774
        }
775
        break;
9,244,522✔
776
      }
777
      case HEARTBEAT_KEY_STBINFO: {
6,255,659✔
778
        void   *rspMsg = NULL;
6,255,659✔
779
        int32_t rspLen = 0;
6,255,659✔
780
        (void)mndValidateStbInfo(pMnode, kv->value, kv->valueLen / sizeof(SSTableVersion), &rspMsg, &rspLen);
6,255,659✔
781
        if (rspMsg && rspLen > 0) {
6,255,659✔
782
          SKv kv1 = {.key = HEARTBEAT_KEY_STBINFO, .valueLen = rspLen, .value = rspMsg};
6,255,659✔
783
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
12,511,318✔
UNCOV
784
            mError("failed to put kv into array, but continue at this heartbeat");
×
785
          }
786
        }
787
        break;
6,255,659✔
788
      }
789
#ifdef TD_ENTERPRISE
790
      case HEARTBEAT_KEY_DYN_VIEW: {
8,243✔
791
        break;
8,243✔
792
      }
793
      case HEARTBEAT_KEY_VIEWINFO: {
8,243✔
794
        if (!needCheck) {
8,243✔
795
          break;
1,343✔
796
        }
797

798
        void   *rspMsg = NULL;
6,900✔
799
        int32_t rspLen = 0;
6,900✔
800
        (void)mndValidateViewInfo(pMnode, kv->value, kv->valueLen / sizeof(SViewVersion), &rspMsg, &rspLen);
6,900✔
801
        if (rspMsg && rspLen > 0) {
6,900✔
802
          SKv kv1 = {.key = HEARTBEAT_KEY_VIEWINFO, .valueLen = rspLen, .value = rspMsg};
6,900✔
803
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
13,800✔
UNCOV
804
            mError("failed to put kv into array, but continue at this heartbeat");
×
805
          }
806
        }
807
        break;
6,900✔
808
      }
809
#endif
810
      case HEARTBEAT_KEY_TSMA: {
4,234✔
811
        void   *rspMsg = NULL;
4,234✔
812
        int32_t rspLen = 0;
4,234✔
813
        (void)mndValidateTSMAInfo(pMnode, kv->value, kv->valueLen / sizeof(STSMAVersion), &rspMsg, &rspLen);
4,234✔
814
        if (rspMsg && rspLen > 0) {
4,234✔
815
          SKv kv = {.key = HEARTBEAT_KEY_TSMA, .valueLen = rspLen, .value = rspMsg};
4,234✔
816
          if (taosArrayPush(hbRsp.info, &kv) == NULL) {
8,468✔
UNCOV
817
            mError("failed to put kv into array, but continue at this heartbeat");
×
818
          }
819
        }
820
        break;
4,234✔
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,062,089✔
829
  }
830

831
  if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
53,084,133✔
UNCOV
832
    if (terrno != 0) code = terrno;
×
833
  }
834
  TAOS_RETURN(code);
26,542,465✔
835
}
836

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

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

849
  SConnPreparedObj obj = {0};
29,303,262✔
850
  obj.totalDnodes = mndGetDnodeSize(pMnode);
29,303,262✔
851
  obj.ipWhiteListVer = batchReq.ipWhiteListVer;
29,301,557✔
852
  TAOS_CHECK_RETURN(mndGetOnlineDnodeNum(pMnode, &obj.onlineDnodes));
29,301,557✔
853
  mndGetMnodeEpSet(pMnode, &obj.epSet);
29,302,222✔
854
  TAOS_CHECK_RETURN(mndCreateQnodeList(pMnode, &obj.pQnodeList, -1));
29,303,954✔
855

856
  SClientHbBatchRsp batchRsp = {0};
29,301,446✔
857
  batchRsp.svrTimestamp = taosGetTimestampSec();
29,300,845✔
858
  batchRsp.rsps = taosArrayInit(0, sizeof(SClientHbRsp));
29,299,636✔
859
  if (batchRsp.rsps == NULL) {
29,298,655✔
UNCOV
860
    TAOS_CHECK_EXIT(terrno);
×
861
  }
862
  batchRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
29,298,655✔
863
  batchRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
29,298,655✔
864
  batchRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
29,298,655✔
865
  tstrncpy(batchRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
29,298,655✔
866
  batchRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
29,300,491✔
867
  batchRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
29,300,491✔
868
  batchRsp.enableAuditDelete = tsEnableAuditDelete;
29,300,491✔
869
  batchRsp.enableAuditSelect = tsEnableAuditSelect;
29,300,491✔
870
  batchRsp.enableAuditInsert = tsEnableAuditInsert;
29,300,491✔
871
  batchRsp.auditLevel = tsAuditLevel;
29,300,491✔
872
  batchRsp.enableStrongPass = tsEnableStrongPassword;
29,300,491✔
873
  batchRsp.sodInitial = (pMnode->sodPhase == TSDB_SOD_PHASE_INITIAL ? 1 : 0);
29,300,491✔
874
  batchRsp.macActive = (pMnode->macActive == MAC_MODE_MANDATORY ? 1 : 0);
29,301,265✔
875

876
  int32_t sz = taosArrayGetSize(batchReq.reqs);
29,298,234✔
877
  for (int i = 0; i < sz; i++) {
65,983,926✔
878
    SClientHbReq *pHbReq = taosArrayGet(batchReq.reqs, i);
36,681,388✔
879
    if (pHbReq->connKey.connType == CONN_TYPE__QUERY || pHbReq->connKey.connType == CONN_TYPE__TMQ) {
36,681,948✔
880
      TAOS_CHECK_EXIT(mndProcessQueryHeartBeat(pMnode, pReq, pHbReq, &batchRsp, &obj));
36,681,477✔
881
    } 
882
  }
883
  taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
29,302,538✔
884

885
  int32_t tlen = tSerializeSClientHbBatchRsp(NULL, 0, &batchRsp);
29,302,550✔
886
  if (tlen < 0) {
29,294,564✔
UNCOV
887
    TAOS_CHECK_EXIT(tlen);
×
888
  }
889
  void *buf = rpcMallocCont(tlen);
29,294,564✔
890
  if (!buf) {
29,300,395✔
UNCOV
891
    TAOS_CHECK_EXIT(terrno);
×
892
  }
893
  tlen = tSerializeSClientHbBatchRsp(buf, tlen, &batchRsp);
29,300,395✔
894
  if (tlen < 0) {
29,298,937✔
UNCOV
895
    rpcFreeCont(buf);
×
896
    TAOS_CHECK_EXIT(tlen);
×
897
  }
898
  pReq->info.rspLen = tlen;
29,298,937✔
899
  pReq->info.rsp = buf;
29,297,633✔
900
_exit:
29,300,955✔
901
  tFreeClientHbBatchRsp(&batchRsp);
902

903
  taosArrayDestroy(obj.pQnodeList);
29,298,161✔
904

905
  TAOS_RETURN(code);
29,300,379✔
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✔
UNCOV
922
    mError("invalid QID:%s", killReq.queryStrId);
×
UNCOV
923
    code = TSDB_CODE_MND_INVALID_QUERY_ID;
×
UNCOV
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);
345✔
933
    code = TSDB_CODE_MND_INVALID_CONN_ID;
345✔
934
    TAOS_RETURN(code);
345✔
935
  } else {
936
    mInfo("connId:%x, queryId:%" PRIx64 " is killed by user:%s", connId, queryId, RPC_MSG_USER(pReq));
1,313✔
937
    pConn->killId = queryId;
1,313✔
938
    taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
1,313✔
939
    TAOS_RETURN(code);
1,313✔
940
  }
941
}
942

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

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

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

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

966
static int32_t mndProcessSvrVerReq(SRpcMsg *pReq) {
×
967
  int32_t       code = 0;
×
UNCOV
968
  int32_t       lino = 0;
×
UNCOV
969
  SServerVerRsp rsp = {0};
×
UNCOV
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
  }
UNCOV
976
  void *pRsp = rpcMallocCont(contLen);
×
977
  if (pRsp == NULL) {
×
978
    TAOS_CHECK_EXIT(terrno);
×
979
  }
UNCOV
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

UNCOV
989
_exit:
×
990

991
  TAOS_RETURN(code);
×
992
}
993

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

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

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

1015
    if ((taosGetTimestampMs() - pConn->lastAccessTimeMs) > ((int64_t)CACHE_OBJ_KEEP_TIME * 1000)) {
277,246✔
1016
      continue;
238,816✔
1017
    }
1018

1019
    cols = 0;
38,430✔
1020

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

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

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

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

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

1061
    STR_TO_VARSTR(endpoint, addr);
38,430✔
1062

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

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

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

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

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

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

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

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

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

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

1139
    numOfRows++;
38,430✔
1140
  }
1141

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1278
    char    subStatus[TSDB_SHOW_SUBQUERY_LEN + VARSTR_HEADER_SIZE] = {0};
21,509✔
1279
    int64_t reserve = 128;
21,509✔
1280
    int32_t strSize = sizeof(subStatus);
21,509✔
1281
    int32_t offset = VARSTR_HEADER_SIZE;
21,509✔
1282
    for (int32_t i = 0; i < pQuery->subPlanNum && offset + reserve < strSize; ++i) {
47,264✔
1283
      if (i) {
25,755✔
1284
        offset += snprintf(subStatus + offset, sizeof(subStatus) - offset, ",");
4,248✔
1285
      }
1286
      if (offset + reserve >= strSize) break;
25,755✔
1287

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

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

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

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

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

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

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

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

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

1370
    pBlock->info.rows++;
21,509✔
1371
  }
1372

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

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

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

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

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

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

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

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

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

1435
    cols = 0;
3,207✔
1436

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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