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

taosdata / TDengine / #4903

27 Dec 2025 02:36PM UTC coverage: 65.73% (+0.09%) from 65.642%
#4903

push

travis-ci

web-flow
fix: ci errors (#34079)

192966 of 293572 relevant lines covered (65.73%)

118504973.46 hits per line

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

74.41
/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 "mndDb.h"
21
#include "mndDnode.h"
22
#include "mndMnode.h"
23
#include "mndPrivilege.h"
24
#include "mndQnode.h"
25
#include "mndShow.h"
26
#include "mndSma.h"
27
#include "mndStb.h"
28
#include "mndUser.h"
29
#include "mndView.h"
30
#include "mndToken.h"
31
#include "tglobal.h"
32
#include "tversion.h"
33
#include "totp.h"
34

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

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

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

76
#define CACHE_OBJ_KEEP_TIME 3  // s
77

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

95
int32_t mndInitProfile(SMnode *pMnode) {
394,488✔
96
  int32_t       code = 0;
394,488✔
97
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
394,488✔
98

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

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

115
  mndSetMsgHandle(pMnode, TDMT_MND_HEARTBEAT, mndProcessHeartBeatReq);
394,488✔
116
  mndSetMsgHandle(pMnode, TDMT_MND_CONNECT, mndProcessConnectReq);
394,488✔
117
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_QUERY, mndProcessKillQueryReq);
394,488✔
118
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_CONN, mndProcessKillConnReq);
394,488✔
119
  mndSetMsgHandle(pMnode, TDMT_MND_SERVER_VERSION, mndProcessSvrVerReq);
394,488✔
120

121
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONNS, mndRetrieveConns);
394,488✔
122
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONNS, mndCancelGetNextConn);
394,488✔
123
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_QUERIES, mndRetrieveQueries);
394,488✔
124
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_QUERIES, mndCancelGetNextQuery);
394,488✔
125
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_APPS, mndRetrieveApps);
394,488✔
126
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_APPS, mndCancelGetNextApp);
394,488✔
127

128
  TAOS_RETURN(code);
394,488✔
129
}
130

131
void mndCleanupProfile(SMnode *pMnode) {
394,425✔
132
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
394,425✔
133
  if (pMgmt->connCache != NULL) {
394,425✔
134
    taosCacheCleanup(pMgmt->connCache);
394,425✔
135
    pMgmt->connCache = NULL;
394,425✔
136
  }
137

138
  if (pMgmt->appCache != NULL) {
394,425✔
139
    taosCacheCleanup(pMgmt->appCache);
394,425✔
140
    pMgmt->appCache = NULL;
394,425✔
141
  }
142
}
394,425✔
143

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

151
  if (pConn->userDualIp.ipv4[0] != 0 && strncmp(pConn->userDualIp.ipv4, none, strlen(none)) != 0) {
35,179✔
152
    char   *ipstr = IP_ADDR_STR(&pConn->userDualIp);
×
153
    int32_t len = strlen(ipstr);
×
154
    memcpy(varDataVal(dst), ipstr, len);
×
155
    varDataLen(dst) = len;
×
156
  }
157
  return;
35,179✔
158
}
159
static void setUserInfo2Conn(SConnObj *connObj, char *userApp, uint32_t userIp, char *cInfo) {
26,840,567✔
160
  if (connObj == NULL) {
26,840,567✔
161
    return;
×
162
  }
163
  tstrncpy(connObj->userApp, userApp, sizeof(connObj->userApp));
26,840,567✔
164
  tstrncpy(connObj->cInfo, cInfo, sizeof(connObj->cInfo));
26,830,801✔
165
  connObj->userIp = userIp;
26,841,286✔
166
}
167
static void setUserInfoIpToConn(SConnObj *connObj, SIpRange *pRange) {
26,838,913✔
168
  int32_t code = 0;
26,838,913✔
169
  if (connObj == NULL) {
26,838,913✔
170
    return;
×
171
  }
172

173
  code = tIpUintToStr(pRange, &connObj->userDualIp);
26,838,913✔
174
  if (code != 0) {
26,834,087✔
175
    mError("conn:%u, failed to set user ip to conn since %s", connObj->id, tstrerror(code));
×
176
    return;
×
177
  }
178
}
179

180

181

182
static SConnObj *mndCreateConn(SMnode *pMnode, const char *user, const char* tokenName, int8_t connType, SIpAddr *pAddr,
2,599,941✔
183
                               int32_t pid, const char *app, int64_t startTime, const char *sVer) {
184
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
2,599,941✔
185

186
  char     connStr[255] = {0};
2,599,941✔
187
  char    *ip = IP_ADDR_STR(pAddr);
2,599,941✔
188
  uint16_t port = pAddr->port;
2,599,941✔
189

190
  int32_t  len = tsnprintf(connStr, sizeof(connStr), "%s%d%d%d%s", user, ip, port, pid, app);
2,599,941✔
191
  uint32_t connId = mndGenerateUid(connStr, len);
2,599,941✔
192
  if (startTime == 0) startTime = taosGetTimestampMs();
3,174,221✔
193

194
  SConnObj connObj = {
2,599,941✔
195
      .id = connId,
196
      .connType = connType,
197
      .appStartTimeMs = startTime,
198
      .pid = pid,
199
      .addr = *pAddr,
200
      .killed = 0,
201
      .loginTimeMs = taosGetTimestampMs(),
2,599,911✔
202
      .lastAccessTimeMs = 0,
203
      .killId = 0,
204
      .numOfQueries = 0,
205
      .pQueries = NULL,
206
  };
207

208
  connObj.lastAccessTimeMs = connObj.loginTimeMs;
2,599,911✔
209
  tstrncpy(connObj.user, user, sizeof(connObj.user));
2,599,911✔
210
  tstrncpy(connObj.tokenName, tokenName, sizeof(connObj.tokenName));
2,599,911✔
211
  tstrncpy(connObj.app, app, sizeof(connObj.app));
2,599,911✔
212
  tstrncpy(connObj.sVer, sVer, sizeof(connObj.sVer));
2,599,911✔
213

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

226

227

228
static void mndFreeConn(SConnObj *pConn) {
2,599,941✔
229
  taosWLockLatch(&pConn->queryLock);
2,599,941✔
230
  taosArrayDestroyEx(pConn->pQueries, tFreeClientHbQueryDesc);
2,599,941✔
231
  taosWUnLockLatch(&pConn->queryLock);
2,599,941✔
232

233
  mTrace("conn:%u, is destroyed, data:%p", pConn->id, pConn);
2,599,941✔
234
}
2,599,941✔
235

236
static SConnObj *mndAcquireConn(SMnode *pMnode, uint32_t connId) {
26,840,658✔
237
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
26,840,658✔
238

239
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &connId, sizeof(connId));
26,842,548✔
240
  if (pConn == NULL) {
26,842,670✔
241
    mDebug("conn:%u, already destroyed", connId);
573,631✔
242
    return NULL;
574,280✔
243
  }
244

245
  pConn->lastAccessTimeMs = taosGetTimestampMs();
26,268,071✔
246
  mTrace("conn:%u, acquired from cache, data:%p", pConn->id, pConn);
26,265,908✔
247
  return pConn;
26,257,727✔
248
}
249

250
static void mndReleaseConn(SMnode *pMnode, SConnObj *pConn, bool extendLifespan) {
29,430,519✔
251
  if (pConn == NULL) return;
29,430,519✔
252
  mTrace("conn:%u, released from cache, data:%p", pConn->id, pConn);
29,429,306✔
253

254
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
29,429,306✔
255
  if (extendLifespan) taosCacheTryExtendLifeSpan(pMgmt->connCache, (void **)&pConn);
29,428,537✔
256
  taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
29,428,468✔
257
}
258

259
void *mndGetNextConn(SMnode *pMnode, SCacheIter *pIter) {
868,113✔
260
  SConnObj *pConn = NULL;
868,113✔
261
  bool      hasNext = taosCacheIterNext(pIter);
868,113✔
262
  if (hasNext) {
868,113✔
263
    size_t dataLen = 0;
806,591✔
264
    pConn = taosCacheIterGetData(pIter, &dataLen);
806,591✔
265
  } else {
266
    taosCacheDestroyIter(pIter);
61,522✔
267
  }
268

269
  return pConn;
868,113✔
270
}
271

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

278

279

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

289
  int32_t    count = 0;
47,123✔
290
  SConnObj  *pConn = NULL;
47,123✔
291
  while ((pConn = mndGetNextConn(pMnode, pIter)) != NULL) {
606,947✔
292
    if (strncmp(pConn->user, user, TSDB_USER_LEN) == 0) {
559,824✔
293
      count++;
77,799✔
294
    }
295
    mndReleaseConn(pMnode, pConn, true);
559,824✔
296
  }
297

298
  return count;
47,123✔
299
}
300

301

302

303
static int32_t verifyPassword(SUserObj* pUser, const char* inputPass) {
2,026,135✔
304
  int32_t code = 0;
2,026,135✔
305

306
  const char* currPass = pUser->passwords[0].pass;
2,026,135✔
307
  char pass[TSDB_PASSWORD_LEN] = {0};
2,025,386✔
308
  (void)memcpy(pass, inputPass, TSDB_PASSWORD_LEN);
2,026,135✔
309
  pass[TSDB_PASSWORD_LEN - 1] = 0;
2,026,105✔
310

311
  if (pUser->passEncryptAlgorithm != 0) {
2,026,105✔
312
    if (pUser->passEncryptAlgorithm != tsiEncryptPassAlgorithm) {
×
313
      return TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
314
    }
315
    code = mndEncryptPass(pass, pUser->salt, NULL);
×
316
    if (code != TSDB_CODE_SUCCESS) {
42✔
317
      return code;
×
318
    }
319
  }
320

321
  // constant time comparison to prevent timing attack
322
  volatile uint8_t res = 0;
2,026,138✔
323
  for (size_t i = 0; i < sizeof(pass) - 1; i++) {
64,800,992✔
324
    res |= pass[i] ^ currPass[i];
62,775,624✔
325
  }
326

327
 return (res == 0) ? TSDB_CODE_SUCCESS: TSDB_CODE_MND_AUTH_FAILURE;
2,025,368✔
328
}
329

330

331

332
static bool verifyTotp(SUserObj *pUser, int32_t totpCode) {
2,026,138✔
333
  if (!mndIsTotpEnabledUser(pUser)) {
2,026,138✔
334
    return true;
2,025,386✔
335
  }
336
  return taosVerifyTotpCode(pUser->totpsecret, sizeof(pUser->totpsecret), totpCode, 6, 1) != 0;
×
337
}
338

339

340

341
static int32_t mndProcessConnectReq(SRpcMsg *pReq) {
2,026,871✔
342
  int32_t          code = 0, lino = 0;
2,026,871✔
343

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

358
  if (token != NULL && mndGetCachedTokenInfo(token, &ti) == NULL) {
2,026,871✔
359
    TAOS_CHECK_GOTO(TSDB_CODE_MND_TOKEN_NOT_EXIST, &lino, _OVER);
×
360
  }
361
  TAOS_CHECK_GOTO(tDeserializeSConnectReq(pReq->pCont, pReq->contLen, &connReq), &lino, _OVER);
2,026,871✔
362
  TAOS_CHECK_GOTO(taosCheckVersionCompatibleFromStr(connReq.sVer, td_version, 3), &lino, _OVER);
2,026,844✔
363
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, user, &pUser), &lino, _OVER);
2,026,874✔
364

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

369
  if (token != NULL || tsMndSkipGrant) {
2,026,108✔
370
    li.lastLoginTime= now;
9✔
371
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
9✔
372
      mndSetUserLoginInfo(user, &li);
×
373
    }
374
  } else if (!verifyTotp(pUser, connReq.totpCode)) {
2,026,099✔
375
    TAOS_CHECK_GOTO(TSDB_CODE_MND_WRONG_TOTP_CODE, &lino, _OVER);
×
376
  } else if ((code = verifyPassword(pUser, connReq.passwd)) == TSDB_CODE_SUCCESS) {
2,025,386✔
377
    li.failedLoginCount = 0;
2,025,661✔
378
    li.lastLoginTime= now;
2,025,661✔
379
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
2,025,661✔
380
      mndSetUserLoginInfo(user, &li);
2,025,691✔
381
    }
382
  } else if (code == TSDB_CODE_MND_AUTH_FAILURE) {
417✔
383
    if (pUser->failedLoginAttempts >= 0) {
417✔
384
      if (li.failedLoginCount >= pUser->failedLoginAttempts) {
384✔
385
        // if we can get here, it means the lock time has passed, so reset the counter
386
        li.failedLoginCount = 0;
×
387
      }
388
      li.failedLoginCount++;
384✔
389
      li.lastFailedLoginTime = now;
384✔
390
    }
391
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
417✔
392
      mndSetUserLoginInfo(user, &li);
417✔
393
    }
394
    goto _OVER;
417✔
395
  } else {
396
    goto _OVER;
×
397
  } 
398

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

409
    TAOS_CHECK_GOTO(mndCheckDbPrivilege(pMnode, user, RPC_MSG_TOKEN(pReq), MND_OPER_READ_OR_WRITE_DB, pDb), &lino, _OVER);
440,596✔
410
  }
411

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

423
  SConnectRsp connectRsp = {0};
2,025,628✔
424
  connectRsp.acctId = pUser->acctId;
2,025,661✔
425
  connectRsp.superUser = pUser->superUser;
2,025,628✔
426
  connectRsp.sysInfo = pUser->sysInfo;
2,024,954✔
427
  connectRsp.clusterId = pMnode->clusterId;
2,025,628✔
428
  connectRsp.connId = pConn->id;
2,025,661✔
429
  connectRsp.connType = connReq.connType;
2,024,924✔
430
  connectRsp.dnodeNum = mndGetDnodeSize(pMnode);
2,024,924✔
431
  connectRsp.svrTimestamp = taosGetTimestampSec();
2,025,661✔
432
  connectRsp.passVer = pUser->passVersion;
2,025,658✔
433
  connectRsp.authVer = pUser->authVersion;
2,025,661✔
434
  connectRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
2,025,661✔
435
  connectRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
2,025,661✔
436
  connectRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
2,025,661✔
437
  connectRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
2,025,661✔
438
  connectRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
2,025,661✔
439
  connectRsp.enableAuditDelete = tsEnableAuditDelete;
2,025,661✔
440
  connectRsp.enableAuditSelect = tsEnableAuditSelect;
2,025,661✔
441
  connectRsp.enableAuditInsert = tsEnableAuditInsert;
2,025,661✔
442
  connectRsp.auditLevel = tsAuditLevel;
2,025,661✔
443
  tstrncpy(connectRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
2,025,661✔
444
  connectRsp.whiteListVer = pUser->ipWhiteListVer;
2,025,661✔
445
  connectRsp.timeWhiteListVer = pUser->timeWhiteListVer;
2,025,658✔
446

447
  tstrncpy(connectRsp.sVer, td_version, sizeof(connectRsp.sVer));
2,025,631✔
448
  tstrncpy(connectRsp.user, user, sizeof(connectRsp.user));
2,025,631✔
449
  tstrncpy(connectRsp.tokenName, ti.name, sizeof(connectRsp.tokenName));
2,025,631✔
450
  (void)snprintf(connectRsp.sDetailVer, sizeof(connectRsp.sDetailVer), "ver:%s\nbuild:%s\ngitinfo:%s", td_version,
2,025,631✔
451
                 td_buildinfo, td_gitinfo);
452
  mndGetMnodeEpSet(pMnode, &connectRsp.epSet);
2,025,631✔
453

454
  int32_t contLen = tSerializeSConnectRsp(NULL, 0, &connectRsp);
2,025,661✔
455
  if (contLen < 0) {
2,025,019✔
456
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
457
  }
458
  void *pRsp = rpcMallocCont(contLen);
2,025,019✔
459
  if (pRsp == NULL) {
2,024,331✔
460
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
461
  }
462

463
  contLen = tSerializeSConnectRsp(pRsp, contLen, &connectRsp);
2,024,331✔
464
  if (contLen < 0) {
2,025,565✔
465
    rpcFreeCont(pRsp);
×
466
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
467
  }
468

469
  pReq->info.rspLen = contLen;
2,025,565✔
470
  pReq->info.rsp = pRsp;
2,024,301✔
471

472
  mGDebug("user:%s, login from %s:%d, conn:%u, app:%s, db:%s", user, ip, port, pConn->id, connReq.app, connReq.db);
2,025,068✔
473
  code = 0;
2,025,661✔
474

475
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
2,025,661✔
476
    char    detail[1000] = {0};
2,025,661✔
477
    int32_t nBytes = snprintf(detail, sizeof(detail), "app:%s", connReq.app);
2,025,661✔
478
    if ((uint32_t)nBytes < sizeof(detail)) {
2,025,661✔
479
      double duration = (taosGetTimestampMs() - tss) / 1000.0;
2,025,661✔
480
      auditRecord(pReq, pMnode->clusterId, "login", "", "", detail, strlen(detail), duration, 0);
2,025,661✔
481
    } else {
482
      mError("failed to audit logic since %s", tstrerror(TSDB_CODE_OUT_OF_RANGE));
×
483
    }
484
  }
485

486
_OVER:
2,025,285✔
487
  if (code != 0) {
2,026,874✔
488
    mGError("user:%s, failed to login from %s since %s, line:%d, db:%s", user, ip, tstrerror(code), lino, connReq.db);
1,213✔
489
  }
490

491
  mndReleaseUser(pMnode, pUser);
2,026,874✔
492
  mndReleaseDb(pMnode, pDb);
2,026,874✔
493
  mndReleaseConn(pMnode, pConn, true);
2,026,874✔
494

495
  TAOS_RETURN(code);
2,026,874✔
496
}
497

498

499

500
static int32_t mndSaveQueryList(SConnObj *pConn, SQueryHbReqBasic *pBasic) {
26,837,762✔
501
  taosWLockLatch(&pConn->queryLock);
26,837,762✔
502

503
  taosArrayDestroyEx(pConn->pQueries, tFreeClientHbQueryDesc);
26,843,351✔
504

505
  pConn->pQueries = pBasic->queryDesc;
26,842,839✔
506
  pConn->numOfQueries = pBasic->queryDesc ? taosArrayGetSize(pBasic->queryDesc) : 0;
26,842,992✔
507
  pBasic->queryDesc = NULL;
26,844,156✔
508

509
  mDebug("queries updated in conn %u, num:%d", pConn->id, pConn->numOfQueries);
26,842,390✔
510

511
  taosWUnLockLatch(&pConn->queryLock);
26,845,921✔
512

513
  return TSDB_CODE_SUCCESS;
26,844,471✔
514
}
515

516
static SAppObj *mndCreateApp(SMnode *pMnode, const SIpAddr *pAddr, const SAppHbReq *pReq) {
563,492✔
517
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
563,492✔
518

519
  SAppObj app;
563,306✔
520
  app.appId = pReq->appId;
563,492✔
521
  app.cliAddr = *pAddr;
563,492✔
522
  app.pid = pReq->pid;
563,492✔
523
  tstrncpy(app.name, pReq->name, sizeof(app.name));
563,492✔
524
  app.startTime = pReq->startTime;
563,492✔
525
  (void)memcpy(&app.summary, &pReq->summary, sizeof(pReq->summary));
563,492✔
526
  app.lastAccessTimeMs = taosGetTimestampMs();
563,492✔
527

528
  SAppObj *pApp =
529
      taosCachePut(pMgmt->appCache, &pReq->appId, sizeof(pReq->appId), &app, sizeof(app), CACHE_OBJ_KEEP_TIME * 1000);
563,492✔
530
  if (pApp == NULL) {
563,492✔
531
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
532
    mError("failed to app %" PRIx64 " into cache since %s", pReq->appId, terrstr());
×
533
    return NULL;
×
534
  }
535

536
  mTrace("app %" PRIx64 " is put into cache", pReq->appId);
563,492✔
537
  return pApp;
563,492✔
538
}
539

540
static void mndFreeApp(SAppObj *pApp) { mTrace("app %" PRIx64 " is destroyed", pApp->appId); }
563,492✔
541

542
static SAppObj *mndAcquireApp(SMnode *pMnode, int64_t appId) {
26,842,033✔
543
  terrno = 0;
26,842,033✔
544
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
26,840,937✔
545

546
  SAppObj *pApp = taosCacheAcquireByKey(pMgmt->appCache, &appId, sizeof(appId));
26,841,496✔
547
  if (pApp == NULL) {
26,836,956✔
548
    mDebug("app %" PRIx64 " not in cache", appId);
562,962✔
549
    return NULL;
563,492✔
550
  }
551

552
  pApp->lastAccessTimeMs = (uint64_t)taosGetTimestampMs();
26,275,619✔
553

554
  mTrace("app %" PRIx64 " acquired from cache", appId);
26,273,409✔
555
  return pApp;
26,277,584✔
556
}
557

558
static void mndReleaseApp(SMnode *pMnode, SAppObj *pApp) {
26,836,837✔
559
  if (pApp == NULL) return;
26,836,837✔
560
  mTrace("release app %" PRIx64 " to cache", pApp->appId);
26,836,837✔
561

562
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
26,836,837✔
563
  taosCacheRelease(pMgmt->appCache, (void **)&pApp, false);
26,837,701✔
564
}
565

566
SAppObj *mndGetNextApp(SMnode *pMnode, SCacheIter *pIter) {
5,386✔
567
  SAppObj *pApp = NULL;
5,386✔
568
  bool     hasNext = taosCacheIterNext(pIter);
5,386✔
569
  if (hasNext) {
5,386✔
570
    size_t dataLen = 0;
2,693✔
571
    pApp = taosCacheIterGetData(pIter, &dataLen);
2,693✔
572
  } else {
573
    taosCacheDestroyIter(pIter);
2,693✔
574
  }
575

576
  return pApp;
5,386✔
577
}
578

579
static void mndCancelGetNextApp(SMnode *pMnode, void *pIter) {
×
580
  if (pIter != NULL) {
×
581
    taosCacheDestroyIter(pIter);
×
582
  }
583
}
×
584

585
static SClientHbRsp *mndMqHbBuildRsp(SMnode *pMnode, SClientHbReq *pReq) {
×
586
  //
587
  return NULL;
×
588
}
589

590
static int32_t mndUpdateAppInfo(SMnode *pMnode, SClientHbReq *pHbReq, const SRpcConnInfo *connInfo) {
26,843,035✔
591
  int32_t    code = 0;
26,843,035✔
592
  SAppHbReq *pReq = &pHbReq->app;
26,843,035✔
593
  SAppObj   *pApp = mndAcquireApp(pMnode, pReq->appId);
26,843,095✔
594
  if (pApp == NULL) {
26,834,275✔
595
    pApp = mndCreateApp(pMnode, &connInfo->cliAddr, pReq);
563,492✔
596
    if (pApp == NULL) {
563,492✔
597
      mError("failed to create new app %" PRIx64 " since %s", pReq->appId, terrstr());
×
598
      code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
599
      if (terrno != 0) code = terrno;
×
600
      TAOS_RETURN(code);
×
601
    } else {
602
      mDebug("a new app %" PRIx64 " is created", pReq->appId);
563,492✔
603
      mndReleaseApp(pMnode, pApp);
563,492✔
604
      return TSDB_CODE_SUCCESS;
563,492✔
605
    }
606
  }
607

608
  (void)memcpy(&pApp->summary, &pReq->summary, sizeof(pReq->summary));
26,270,783✔
609

610
  mndReleaseApp(pMnode, pApp);
26,277,696✔
611

612
  return TSDB_CODE_SUCCESS;
26,272,852✔
613
}
614

615
static int32_t mndGetOnlineDnodeNum(SMnode *pMnode, int32_t *num) {
22,047,407✔
616
  SSdb      *pSdb = pMnode->pSdb;
22,047,407✔
617
  SDnodeObj *pDnode = NULL;
22,046,700✔
618
  int64_t    curMs = taosGetTimestampMs();
22,046,396✔
619
  void      *pIter = NULL;
22,046,396✔
620

621
  while (true) {
37,972,009✔
622
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
60,018,405✔
623
    if (pIter == NULL) break;
60,019,617✔
624

625
    bool online = mndIsDnodeOnline(pDnode, curMs);
37,971,073✔
626
    if (online) {
37,970,598✔
627
      (*num)++;
36,767,443✔
628
    }
629

630
    sdbRelease(pSdb, pDnode);
37,967,885✔
631
  }
632

633
  return TSDB_CODE_SUCCESS;
22,048,544✔
634
}
635

636
static int32_t mndProcessQueryHeartBeat(SMnode *pMnode, SRpcMsg *pMsg, SClientHbReq *pHbReq,
26,843,308✔
637
                                        SClientHbBatchRsp *pBatchRsp, SConnPreparedObj *pObj) {
638
  int32_t       code = 0;
26,843,308✔
639
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
26,843,308✔
640
  SClientHbRsp  hbRsp = {.connKey = pHbReq->connKey, .status = 0, .info = NULL, .query = NULL};
26,842,429✔
641

642
  if (0 != pHbReq->app.appId) {
26,843,437✔
643
    TAOS_CHECK_RETURN(mndUpdateAppInfo(pMnode, pHbReq, &pMsg->info.conn));
26,839,180✔
644
  }
645

646
  if (pHbReq->query) {
26,834,929✔
647
    SQueryHbReqBasic *pBasic = pHbReq->query;
26,833,763✔
648
    SConnObj *pConn = mndAcquireConn(pMnode, pBasic->connId);
26,837,750✔
649
    if (pConn == NULL) {
26,834,077✔
650
      SRpcConnInfo  connInfo = pMsg->info.conn;
574,280✔
651
      const char* user = pHbReq->user;
574,280✔
652
      pConn = mndCreateConn(pMnode, user, pHbReq->tokenName, pHbReq->connKey.connType, &connInfo.cliAddr, pHbReq->app.pid,
574,280✔
653
                            pHbReq->app.name, 0, pHbReq->sVer);
574,280✔
654
      if (pConn == NULL) {
574,280✔
655
        mError("user:%s, conn:%u is freed and failed to create new since %s", user, pBasic->connId, terrstr());
×
656
        code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
657
        if (terrno != 0) code = terrno;
×
658
        TAOS_RETURN(code);
×
659
      } else {
660
        mDebug("user:%s, conn:%u is freed, will create a new conn:%u", user, pBasic->connId, pConn->id);
574,280✔
661
      }
662
    }
663

664
    setUserInfo2Conn(pConn, pHbReq->userApp, pHbReq->userIp, pHbReq->cInfo);
26,834,077✔
665
    setUserInfoIpToConn(pConn, &pHbReq->userDualIp);
26,834,431✔
666

667
    SQueryHbRspBasic *rspBasic = taosMemoryCalloc(1, sizeof(SQueryHbRspBasic));
26,828,687✔
668
    if (rspBasic == NULL) {
26,831,425✔
669
      mndReleaseConn(pMnode, pConn, true);
×
670
      code = terrno;
×
671
      mError("user:%s, conn:%u failed to process hb while since %s", pConn->user, pBasic->connId, terrstr());
×
672
      TAOS_RETURN(code);
×
673
    }
674

675
    TAOS_CHECK_RETURN(mndSaveQueryList(pConn, pBasic));
26,831,425✔
676
    if (pConn->killed != 0) {
26,843,541✔
677
      rspBasic->killConnection = 1;
×
678
    }
679

680
    if (pConn->killId != 0) {
26,843,212✔
681
      rspBasic->killRid = pConn->killId;
3✔
682
      pConn->killId = 0;
3✔
683
    }
684

685
    rspBasic->connId = pConn->id;
26,843,541✔
686
    rspBasic->connId = pConn->id;
26,844,925✔
687
    rspBasic->totalDnodes = pObj->totalDnodes;
26,843,519✔
688
    rspBasic->onlineDnodes = pObj->onlineDnodes;
26,843,328✔
689
    rspBasic->epSet = pObj->epSet;
26,843,190✔
690
    rspBasic->pQnodeList = taosArrayDup(pObj->pQnodeList, NULL);
26,843,804✔
691

692
    mndReleaseConn(pMnode, pConn, true);
26,843,367✔
693

694
    hbRsp.query = rspBasic;
26,844,453✔
695
  } else {
696
    mDebug("no query info in hb msg");
30✔
697
  }
698

699
  int32_t kvNum = taosHashGetSize(pHbReq->info);
26,844,483✔
700
  if (NULL == pHbReq->info || kvNum <= 0) {
26,844,955✔
701
    if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
14,493,066✔
702
      mError("failed to put rsp into array, but continue at this heartbeat");
×
703
    }
704
    return TSDB_CODE_SUCCESS;
7,247,009✔
705
  }
706

707
  hbRsp.info = taosArrayInit(kvNum, sizeof(SKv));
19,597,946✔
708
  if (NULL == hbRsp.info) {
19,597,946✔
709
    mError("taosArrayInit %d rsp kv failed", kvNum);
×
710
    code = terrno;
×
711
    tFreeClientHbRsp(&hbRsp);
712
    TAOS_RETURN(code);
×
713
  }
714

715
#ifdef TD_ENTERPRISE
716
  bool             needCheck = true;
19,597,946✔
717
  int32_t          key = HEARTBEAT_KEY_DYN_VIEW;
19,597,946✔
718
  SDynViewVersion *pDynViewVer = NULL;
19,597,946✔
719
  SKv             *pKv = taosHashGet(pHbReq->info, &key, sizeof(key));
19,597,946✔
720
  if (NULL != pKv) {
19,597,946✔
721
    pDynViewVer = pKv->value;
2,931✔
722
    mTrace("recv view dyn ver, bootTs:%" PRId64 ", ver:%" PRIu64, pDynViewVer->svrBootTs, pDynViewVer->dynViewVer);
2,931✔
723

724
    SDynViewVersion *pRspVer = NULL;
2,931✔
725
    if (0 != (code = mndValidateDynViewVersion(pMnode, pDynViewVer, &needCheck, &pRspVer))) {
2,931✔
726
      TAOS_RETURN(code);
×
727
    }
728

729
    if (needCheck) {
2,931✔
730
      SKv kv1 = {.key = HEARTBEAT_KEY_DYN_VIEW, .valueLen = sizeof(*pDynViewVer), .value = pRspVer};
2,591✔
731
      if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
5,182✔
732
        if (terrno != 0) code = terrno;
×
733
        TAOS_RETURN(code);
×
734
      };
735
      mTrace("need to check view ver, lastest bootTs:%" PRId64 ", ver:%" PRIu64, pRspVer->svrBootTs,
2,591✔
736
             pRspVer->dynViewVer);
737
    }
738
  }
739
#endif
740

741
  void *pIter = taosHashIterate(pHbReq->info, NULL);
19,597,946✔
742
  while (pIter != NULL) {
49,386,510✔
743
    SKv *kv = pIter;
29,788,799✔
744

745
    switch (kv->key) {
29,788,799✔
746
      case HEARTBEAT_KEY_USER_AUTHINFO: {
19,562,153✔
747
        void   *rspMsg = NULL;
19,562,153✔
748
        int32_t rspLen = 0;
19,562,153✔
749
        (void)mndValidateUserAuthInfo(pMnode, kv->value, kv->valueLen / sizeof(SUserAuthVersion), &rspMsg, &rspLen,
19,561,844✔
750
                                      pObj->ipWhiteListVer);
751
        if (rspMsg && rspLen > 0) {
19,562,153✔
752
          SKv kv1 = {.key = HEARTBEAT_KEY_USER_AUTHINFO, .valueLen = rspLen, .value = rspMsg};
609,184✔
753
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
1,218,368✔
754
            mError("failed to put kv into array, but continue at this heartbeat");
×
755
          }
756
        }
757
        break;
19,562,153✔
758
      }
759
      case HEARTBEAT_KEY_DBINFO: {
6,127,187✔
760
        void   *rspMsg = NULL;
6,127,187✔
761
        int32_t rspLen = 0;
6,127,187✔
762
        (void)mndValidateDbInfo(pMnode, kv->value, kv->valueLen / sizeof(SDbCacheInfo), &rspMsg, &rspLen);
6,127,187✔
763
        if (rspMsg && rspLen > 0) {
6,127,187✔
764
          SKv kv1 = {.key = HEARTBEAT_KEY_DBINFO, .valueLen = rspLen, .value = rspMsg};
6,127,187✔
765
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
12,254,374✔
766
            mError("failed to put kv into array, but continue at this heartbeat");
×
767
          }
768
        }
769
        break;
6,127,187✔
770
      }
771
      case HEARTBEAT_KEY_STBINFO: {
4,093,597✔
772
        void   *rspMsg = NULL;
4,093,597✔
773
        int32_t rspLen = 0;
4,093,597✔
774
        (void)mndValidateStbInfo(pMnode, kv->value, kv->valueLen / sizeof(SSTableVersion), &rspMsg, &rspLen);
4,093,597✔
775
        if (rspMsg && rspLen > 0) {
4,093,597✔
776
          SKv kv1 = {.key = HEARTBEAT_KEY_STBINFO, .valueLen = rspLen, .value = rspMsg};
4,093,597✔
777
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
8,187,194✔
778
            mError("failed to put kv into array, but continue at this heartbeat");
×
779
          }
780
        }
781
        break;
4,093,597✔
782
      }
783
#ifdef TD_ENTERPRISE
784
      case HEARTBEAT_KEY_DYN_VIEW: {
2,931✔
785
        break;
2,931✔
786
      }
787
      case HEARTBEAT_KEY_VIEWINFO: {
2,931✔
788
        if (!needCheck) {
2,931✔
789
          break;
340✔
790
        }
791

792
        void   *rspMsg = NULL;
2,591✔
793
        int32_t rspLen = 0;
2,591✔
794
        (void)mndValidateViewInfo(pMnode, kv->value, kv->valueLen / sizeof(SViewVersion), &rspMsg, &rspLen);
2,591✔
795
        if (rspMsg && rspLen > 0) {
2,591✔
796
          SKv kv1 = {.key = HEARTBEAT_KEY_VIEWINFO, .valueLen = rspLen, .value = rspMsg};
2,591✔
797
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
5,182✔
798
            mError("failed to put kv into array, but continue at this heartbeat");
×
799
          }
800
        }
801
        break;
2,591✔
802
      }
803
#endif
804
      case HEARTBEAT_KEY_TSMA: {
×
805
        void   *rspMsg = NULL;
×
806
        int32_t rspLen = 0;
×
807
        (void)mndValidateTSMAInfo(pMnode, kv->value, kv->valueLen / sizeof(STSMAVersion), &rspMsg, &rspLen);
×
808
        if (rspMsg && rspLen > 0) {
×
809
          SKv kv = {.key = HEARTBEAT_KEY_TSMA, .valueLen = rspLen, .value = rspMsg};
×
810
          if (taosArrayPush(hbRsp.info, &kv) == NULL) {
×
811
            mError("failed to put kv into array, but continue at this heartbeat");
×
812
          }
813
        }
814
        break;
×
815
      }
816
      default:
×
817
        mError("invalid kv key:%d", kv->key);
×
818
        hbRsp.status = TSDB_CODE_APP_ERROR;
×
819
        break;
×
820
    }
821

822
    pIter = taosHashIterate(pHbReq->info, pIter);
29,788,799✔
823
  }
824

825
  if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
39,195,657✔
826
    if (terrno != 0) code = terrno;
×
827
  }
828
  TAOS_RETURN(code);
19,597,946✔
829
}
830

831
static int32_t mndProcessHeartBeatReq(SRpcMsg *pReq) {
22,046,476✔
832
  int32_t code = 0;
22,046,476✔
833
  int32_t lino = 0;
22,046,476✔
834
  SMnode *pMnode = pReq->info.node;
22,046,476✔
835

836
  SClientHbBatchReq batchReq = {0};
22,047,593✔
837
  if (tDeserializeSClientHbBatchReq(pReq->pCont, pReq->contLen, &batchReq) != 0) {
22,046,959✔
838
    taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
×
839
    code = TSDB_CODE_INVALID_MSG;
×
840
    TAOS_RETURN(code);
×
841
  }
842

843
  SConnPreparedObj obj = {0};
22,048,241✔
844
  obj.totalDnodes = mndGetDnodeSize(pMnode);
22,048,241✔
845
  obj.ipWhiteListVer = batchReq.ipWhiteListVer;
22,048,241✔
846
  TAOS_CHECK_RETURN(mndGetOnlineDnodeNum(pMnode, &obj.onlineDnodes));
22,048,241✔
847
  mndGetMnodeEpSet(pMnode, &obj.epSet);
22,046,403✔
848
  TAOS_CHECK_RETURN(mndCreateQnodeList(pMnode, &obj.pQnodeList, -1));
22,048,544✔
849

850
  SClientHbBatchRsp batchRsp = {0};
22,044,370✔
851
  batchRsp.svrTimestamp = taosGetTimestampSec();
22,045,652✔
852
  batchRsp.rsps = taosArrayInit(0, sizeof(SClientHbRsp));
22,042,239✔
853
  if (batchRsp.rsps == NULL) {
22,047,726✔
854
    TAOS_CHECK_EXIT(terrno);
×
855
  }
856
  batchRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
22,047,726✔
857
  batchRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
22,047,726✔
858
  batchRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
22,047,726✔
859
  tstrncpy(batchRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
22,047,726✔
860
  batchRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
22,047,726✔
861
  batchRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
22,047,726✔
862
  batchRsp.enableAuditDelete = tsEnableAuditDelete;
22,047,726✔
863
  batchRsp.enableAuditSelect = tsEnableAuditSelect;
22,047,726✔
864
  batchRsp.enableAuditInsert = tsEnableAuditInsert;
22,047,726✔
865
  batchRsp.auditLevel = tsAuditLevel;
22,047,726✔
866
  batchRsp.enableStrongPass = tsEnableStrongPassword;
22,047,726✔
867

868
  int32_t sz = taosArrayGetSize(batchReq.reqs);
22,047,726✔
869
  for (int i = 0; i < sz; i++) {
48,892,467✔
870
    SClientHbReq *pHbReq = taosArrayGet(batchReq.reqs, i);
26,843,435✔
871
    if (pHbReq->connKey.connType == CONN_TYPE__QUERY || pHbReq->connKey.connType == CONN_TYPE__TMQ) {
26,842,751✔
872
      TAOS_CHECK_EXIT(mndProcessQueryHeartBeat(pMnode, pReq, pHbReq, &batchRsp, &obj));
26,844,479✔
873
    } 
874
  }
875
  taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
22,049,032✔
876

877
  int32_t tlen = tSerializeSClientHbBatchRsp(NULL, 0, &batchRsp);
22,047,015✔
878
  if (tlen < 0) {
22,038,482✔
879
    TAOS_CHECK_EXIT(tlen);
×
880
  }
881
  void *buf = rpcMallocCont(tlen);
22,038,482✔
882
  if (!buf) {
22,035,907✔
883
    TAOS_CHECK_EXIT(terrno);
×
884
  }
885
  tlen = tSerializeSClientHbBatchRsp(buf, tlen, &batchRsp);
22,035,907✔
886
  if (tlen < 0) {
22,043,762✔
887
    rpcFreeCont(buf);
×
888
    TAOS_CHECK_EXIT(tlen);
×
889
  }
890
  pReq->info.rspLen = tlen;
22,043,762✔
891
  pReq->info.rsp = buf;
22,043,948✔
892
_exit:
22,039,006✔
893
  tFreeClientHbBatchRsp(&batchRsp);
894

895
  taosArrayDestroy(obj.pQnodeList);
22,047,414✔
896

897
  TAOS_RETURN(code);
22,042,707✔
898
}
899

900
static int32_t mndProcessKillQueryReq(SRpcMsg *pReq) {
3✔
901
  int32_t       code = 0;
3✔
902
  SMnode       *pMnode = pReq->info.node;
3✔
903
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
3✔
904

905
  SKillQueryReq killReq = {0};
3✔
906
  TAOS_CHECK_RETURN(tDeserializeSKillQueryReq(pReq->pCont, pReq->contLen, &killReq));
3✔
907

908
  mInfo("kill query msg is received, queryId:%s", killReq.queryStrId);
3✔
909
  TAOS_CHECK_RETURN(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_KILL_QUERY));
3✔
910
  int32_t  connId = 0;
3✔
911
  uint64_t queryId = 0;
3✔
912
  char    *p = strchr(killReq.queryStrId, ':');
3✔
913
  if (NULL == p) {
3✔
914
    mError("invalid QID:%s", killReq.queryStrId);
×
915
    code = TSDB_CODE_MND_INVALID_QUERY_ID;
×
916
    TAOS_RETURN(code);
×
917
  }
918
  *p = 0;
3✔
919
  connId = taosStr2Int32(killReq.queryStrId, NULL, 16);
3✔
920
  queryId = taosStr2UInt64(p + 1, NULL, 16);
3✔
921

922
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &connId, sizeof(int32_t));
3✔
923
  if (pConn == NULL) {
3✔
924
    mError("connId:%x, failed to kill queryId:%" PRIx64 ", conn not exist", connId, queryId);
×
925
    code = TSDB_CODE_MND_INVALID_CONN_ID;
×
926
    TAOS_RETURN(code);
×
927
  } else {
928
    mInfo("connId:%x, queryId:%" PRIx64 " is killed by user:%s", connId, queryId, RPC_MSG_USER(pReq));
3✔
929
    pConn->killId = queryId;
3✔
930
    taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
3✔
931
    TAOS_RETURN(code);
3✔
932
  }
933
}
934

935
static int32_t mndProcessKillConnReq(SRpcMsg *pReq) {
158✔
936
  int32_t       code = 0;
158✔
937
  SMnode       *pMnode = pReq->info.node;
158✔
938
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
158✔
939

940
  SKillConnReq killReq = {0};
158✔
941
  TAOS_CHECK_RETURN(tDeserializeSKillConnReq(pReq->pCont, pReq->contLen, &killReq));
158✔
942

943
  TAOS_CHECK_RETURN(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_KILL_CONN));
158✔
944

945
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &killReq.connId, sizeof(uint32_t));
×
946
  if (pConn == NULL) {
×
947
    mError("connId:%u, failed to kill connection, conn not exist", killReq.connId);
×
948
    code = TSDB_CODE_MND_INVALID_CONN_ID;
×
949
    TAOS_RETURN(code);
×
950
  } else {
951
    mInfo("connId:%u, is killed by user:%s", killReq.connId, RPC_MSG_USER(pReq));
×
952
    pConn->killed = 1;
×
953
    taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
×
954
    TAOS_RETURN(code);
×
955
  }
956
}
957

958
static int32_t mndProcessSvrVerReq(SRpcMsg *pReq) {
×
959
  int32_t       code = 0;
×
960
  int32_t       lino = 0;
×
961
  SServerVerRsp rsp = {0};
×
962
  tstrncpy(rsp.ver, td_version, sizeof(rsp.ver));
×
963

964
  int32_t contLen = tSerializeSServerVerRsp(NULL, 0, &rsp);
×
965
  if (contLen < 0) {
×
966
    TAOS_CHECK_EXIT(contLen);
×
967
  }
968
  void *pRsp = rpcMallocCont(contLen);
×
969
  if (pRsp == NULL) {
×
970
    TAOS_CHECK_EXIT(terrno);
×
971
  }
972
  contLen = tSerializeSServerVerRsp(pRsp, contLen, &rsp);
×
973
  if (contLen < 0) {
×
974
    rpcFreeCont(pRsp);
×
975
    TAOS_CHECK_EXIT(contLen);
×
976
  }
977

978
  pReq->info.rspLen = contLen;
×
979
  pReq->info.rsp = pRsp;
×
980

981
_exit:
×
982

983
  TAOS_RETURN(code);
×
984
}
985

986
static int32_t mndRetrieveConns(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
11,699✔
987
  SMnode   *pMnode = pReq->info.node;
11,699✔
988
  SSdb     *pSdb = pMnode->pSdb;
11,699✔
989
  int32_t   numOfRows = 0;
11,699✔
990
  int32_t   cols = 0;
11,699✔
991
  int32_t   code = 0;
11,699✔
992
  SConnObj *pConn = NULL;
11,699✔
993

994
  if (pShow->pIter == NULL) {
11,699✔
995
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
11,699✔
996
    pShow->pIter = taosCacheCreateIter(pMgmt->connCache);
11,699✔
997
    if (!pShow->pIter) return terrno;
11,699✔
998
  }
999

1000
  while (numOfRows < rows) {
240,106✔
1001
    pConn = mndGetNextConn(pMnode, pShow->pIter);
240,106✔
1002
    if (pConn == NULL) {
240,106✔
1003
      pShow->pIter = NULL;
11,699✔
1004
      break;
11,699✔
1005
    }
1006

1007
    if ((taosGetTimestampMs() - pConn->lastAccessTimeMs) > ((int64_t)CACHE_OBJ_KEEP_TIME * 1000)) {
228,407✔
1008
      continue;
197,658✔
1009
    }
1010

1011
    cols = 0;
30,749✔
1012

1013
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
30,749✔
1014
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->id, false);
30,749✔
1015
    if (code != 0) {
30,749✔
1016
      mError("failed to set conn id:%u since %s", pConn->id, tstrerror(code));
×
1017
      return code;
×
1018
    }
1019

1020
    char user[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
30,749✔
1021
    STR_TO_VARSTR(user, pConn->user);
30,749✔
1022
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
30,749✔
1023
    code = colDataSetVal(pColInfo, numOfRows, (const char *)user, false);
30,749✔
1024
    if (code != 0) {
30,749✔
1025
      mError("failed to set user since %s", tstrerror(code));
×
1026
      return code;
×
1027
    }
1028

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

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

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

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

1062
    STR_TO_VARSTR(endpoint, addr);
30,749✔
1063

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

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

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

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

1094
    char userIp[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
30,749✔
1095
    getUserIpFromConnObj(pConn, userIp);
30,749✔
1096

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

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

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

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

1133
  pShow->numOfRows += numOfRows;
11,699✔
1134
  return numOfRows;
11,699✔
1135
}
1136

1137
/**
1138
 * @param pConn the conn queries pack from
1139
 * @param[out] pBlock the block data packed into
1140
 * @param offset skip [offset] queries in pConn
1141
 * @param rowsToPack at most rows to pack
1142
 * @return rows packed
1143
 */
1144
static int32_t packQueriesIntoBlock(SShowObj *pShow, SConnObj *pConn, SSDataBlock *pBlock, uint32_t offset,
18,360✔
1145
                                    uint32_t rowsToPack) {
1146
  int32_t cols = 0;
18,360✔
1147
  int32_t code = 0;
18,360✔
1148
  taosRLockLatch(&pConn->queryLock);
18,360✔
1149
  int32_t numOfQueries = taosArrayGetSize(pConn->pQueries);
18,360✔
1150
  if (NULL == pConn->pQueries || numOfQueries <= offset) {
18,360✔
1151
    taosRUnLockLatch(&pConn->queryLock);
13,930✔
1152
    return 0;
13,930✔
1153
  }
1154

1155
  int32_t i = offset;
4,430✔
1156
  for (; i < numOfQueries && (i - offset) < rowsToPack; ++i) {
8,860✔
1157
    int32_t     curRowIndex = pBlock->info.rows;
4,430✔
1158
    SQueryDesc *pQuery = taosArrayGet(pConn->pQueries, i);
4,430✔
1159
    cols = 0;
4,430✔
1160

1161
    char queryId[26 + VARSTR_HEADER_SIZE] = {0};
4,430✔
1162
    (void)tsnprintf(&queryId[VARSTR_HEADER_SIZE], sizeof(queryId) - VARSTR_HEADER_SIZE, "%x:%" PRIx64, pConn->id,
4,430✔
1163
                    pQuery->reqRid);
1164
    varDataLen(queryId) = strlen(&queryId[VARSTR_HEADER_SIZE]);
4,430✔
1165
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,430✔
1166
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)queryId, false);
4,430✔
1167
    if (code != 0) {
4,430✔
1168
      mError("failed to set query id:%s since %s", queryId, tstrerror(code));
×
1169
      taosRUnLockLatch(&pConn->queryLock);
×
1170
      return code;
×
1171
    }
1172

1173
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,430✔
1174
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->queryId, false);
4,430✔
1175
    if (code != 0) {
4,430✔
1176
      mError("failed to set query id:%" PRIx64 " since %s", pQuery->queryId, tstrerror(code));
×
1177
      taosRUnLockLatch(&pConn->queryLock);
×
1178
      return code;
×
1179
    }
1180

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

1189
    char app[TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE];
4,430✔
1190
    STR_TO_VARSTR(app, pConn->app);
4,430✔
1191
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,430✔
1192
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)app, false);
4,430✔
1193
    if (code != 0) {
4,430✔
1194
      mError("failed to set app since %s", tstrerror(code));
×
1195
      taosRUnLockLatch(&pConn->queryLock);
×
1196
      return code;
×
1197
    }
1198

1199
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,430✔
1200
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pConn->pid, false);
4,430✔
1201
    if (code != 0) {
4,430✔
1202
      mError("failed to set conn id:%u since %s", pConn->id, tstrerror(code));
×
1203
      taosRUnLockLatch(&pConn->queryLock);
×
1204
      return code;
×
1205
    }
1206

1207
    char user[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
4,430✔
1208
    STR_TO_VARSTR(user, pConn->user);
4,430✔
1209
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,430✔
1210
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)user, false);
4,430✔
1211
    if (code != 0) {
4,430✔
1212
      mError("failed to set user since %s", tstrerror(code));
×
1213
      taosRUnLockLatch(&pConn->queryLock);
×
1214
      return code;
×
1215
    }
1216

1217
    char endpoint[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
4,430✔
1218
    char buf[IP_RESERVE_CAP] = {0};
4,430✔
1219
    (void)tsnprintf(buf, sizeof(buf), "%s:%d", IP_ADDR_STR(&pConn->addr), pConn->addr.port);
4,430✔
1220
    STR_TO_VARSTR(endpoint, buf);
4,430✔
1221
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,430✔
1222
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)endpoint, false);
4,430✔
1223
    if (code != 0) {
4,430✔
1224
      mError("failed to set endpoint since %s", tstrerror(code));
×
1225
      taosRUnLockLatch(&pConn->queryLock);
×
1226
      return code;
×
1227
    }
1228

1229
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,430✔
1230
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->stime, false);
4,430✔
1231
    if (code != 0) {
4,430✔
1232
      mError("failed to set start time since %s", tstrerror(code));
×
1233
      taosRUnLockLatch(&pConn->queryLock);
×
1234
      return code;
×
1235
    }
1236

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

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

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

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

1269
    char    subStatus[TSDB_SHOW_SUBQUERY_LEN + VARSTR_HEADER_SIZE] = {0};
4,430✔
1270
    int64_t reserve = 64;
4,430✔
1271
    int32_t strSize = sizeof(subStatus);
4,430✔
1272
    int32_t offset = VARSTR_HEADER_SIZE;
4,430✔
1273
    for (int32_t i = 0; i < pQuery->subPlanNum && offset + reserve < strSize; ++i) {
8,857✔
1274
      if (i) {
4,427✔
1275
        offset += tsnprintf(subStatus + offset, sizeof(subStatus) - offset, ",");
×
1276
      }
1277
      if (offset + reserve < strSize) {
4,427✔
1278
        SQuerySubDesc *pDesc = taosArrayGet(pQuery->subDesc, i);
4,427✔
1279
        offset +=
4,427✔
1280
            tsnprintf(subStatus + offset, sizeof(subStatus) - offset, "%" PRIu64 ":%s", pDesc->tid, pDesc->status);
4,427✔
1281
      } else {
1282
        break;
×
1283
      }
1284
    }
1285
    varDataLen(subStatus) = strlen(&subStatus[VARSTR_HEADER_SIZE]);
4,430✔
1286
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,430✔
1287
    code = colDataSetVal(pColInfo, curRowIndex, subStatus, (varDataLen(subStatus) == 0) ? true : false);
4,430✔
1288
    if (code != 0) {
4,430✔
1289
      mError("failed to set sub status since %s", tstrerror(code));
×
1290
      taosRUnLockLatch(&pConn->queryLock);
×
1291
      return code;
×
1292
    }
1293

1294
    char sql[TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE] = {0};
4,430✔
1295
    STR_TO_VARSTR(sql, pQuery->sql);
4,430✔
1296
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,430✔
1297
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)sql, false);
4,430✔
1298
    if (code != 0) {
4,430✔
1299
      mError("failed to set sql since %s", tstrerror(code));
×
1300
      taosRUnLockLatch(&pConn->queryLock);
×
1301
      return code;
×
1302
    }
1303

1304
    char userApp[TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE];
4,430✔
1305
    STR_TO_VARSTR(userApp, pConn->userApp);
4,430✔
1306
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,430✔
1307
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)userApp, false);
4,430✔
1308
    if (code != 0) {
4,430✔
1309
      mError("failed to set user app since %s", tstrerror(code));
×
1310
      taosRUnLockLatch(&pConn->queryLock);
×
1311
      return code;
×
1312
    }
1313

1314
    char userIp[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
4,430✔
1315
    getUserIpFromConnObj(pConn, userIp);
4,430✔
1316

1317
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,430✔
1318
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)userIp, false);
4,430✔
1319
    if (code != 0) {
4,430✔
1320
      mError("failed to set user ip since %s", tstrerror(code));
×
1321
      taosRUnLockLatch(&pConn->queryLock);
×
1322
      return code;
×
1323
    }
1324

1325
    pBlock->info.rows++;
4,430✔
1326
  }
1327

1328
  taosRUnLockLatch(&pConn->queryLock);
4,430✔
1329
  return i - offset;
4,430✔
1330
}
1331

1332
static int32_t mndRetrieveQueries(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
2,700✔
1333
  SMnode   *pMnode = pReq->info.node;
2,700✔
1334
  SSdb     *pSdb = pMnode->pSdb;
2,700✔
1335
  int32_t   numOfRows = 0;
2,700✔
1336
  SConnObj *pConn = NULL;
2,700✔
1337

1338
  if (pShow->pIter == NULL) {
2,700✔
1339
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
2,700✔
1340
    pShow->pIter = taosCacheCreateIter(pMgmt->connCache);
2,700✔
1341
    if (!pShow->pIter) return terrno;
2,700✔
1342
  }
1343

1344
  // means fetched some data last time for this conn
1345
  if (pShow->curIterPackedRows > 0) {
2,700✔
1346
    size_t len = 0;
×
1347
    pConn = taosCacheIterGetData(pShow->pIter, &len);
×
1348
    if (pConn && (taosArrayGetSize(pConn->pQueries) > pShow->curIterPackedRows)) {
×
1349
      numOfRows = packQueriesIntoBlock(pShow, pConn, pBlock, pShow->curIterPackedRows, rows);
×
1350
      pShow->curIterPackedRows += numOfRows;
×
1351
    }
1352
  }
1353

1354
  while (numOfRows < rows) {
21,060✔
1355
    pConn = mndGetNextConn(pMnode, pShow->pIter);
21,060✔
1356
    if (pConn == NULL) {
21,060✔
1357
      pShow->pIter = NULL;
2,700✔
1358
      break;
2,700✔
1359
    }
1360

1361
    int32_t packedRows = packQueriesIntoBlock(pShow, pConn, pBlock, 0, rows - numOfRows);
18,360✔
1362
    pShow->curIterPackedRows = packedRows;
18,360✔
1363
    numOfRows += packedRows;
18,360✔
1364
  }
1365
  pShow->numOfRows += numOfRows;
2,700✔
1366
  return numOfRows;
2,700✔
1367
}
1368

1369
static int32_t mndRetrieveApps(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
2,693✔
1370
  SMnode  *pMnode = pReq->info.node;
2,693✔
1371
  SSdb    *pSdb = pMnode->pSdb;
2,693✔
1372
  int32_t  numOfRows = 0;
2,693✔
1373
  int32_t  cols = 0;
2,693✔
1374
  SAppObj *pApp = NULL;
2,693✔
1375
  int32_t  code = 0;
2,693✔
1376

1377
  if (pShow->pIter == NULL) {
2,693✔
1378
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
2,693✔
1379
    pShow->pIter = taosCacheCreateIter(pMgmt->appCache);
2,693✔
1380
    if (!pShow->pIter) return terrno;
2,693✔
1381
  }
1382

1383
  while (numOfRows < rows) {
5,386✔
1384
    pApp = mndGetNextApp(pMnode, pShow->pIter);
5,386✔
1385
    if (pApp == NULL) {
5,386✔
1386
      pShow->pIter = NULL;
2,693✔
1387
      break;
2,693✔
1388
    }
1389

1390
    cols = 0;
2,693✔
1391

1392
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,693✔
1393
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->appId, false);
2,693✔
1394
    if (code != 0) {
2,693✔
1395
      mError("failed to set app id since %s", tstrerror(code));
×
1396
      return code;
×
1397
    }
1398

1399
    char ip[TD_IP_LEN + VARSTR_HEADER_SIZE] = {0};
2,693✔
1400
    char buf[IP_RESERVE_CAP] = {0};
2,693✔
1401
    snprintf(buf, sizeof(buf), "%s", IP_ADDR_STR(&pApp->cliAddr));
2,693✔
1402
    STR_TO_VARSTR(ip, buf);
2,693✔
1403

1404
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,693✔
1405
    code = colDataSetVal(pColInfo, numOfRows, (const char *)ip, false);
2,693✔
1406
    if (code != 0) {
2,693✔
1407
      mError("failed to set ip since %s", tstrerror(code));
×
1408
      return code;
×
1409
    }
1410

1411
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,693✔
1412
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->pid, false);
2,693✔
1413
    if (code != 0) {
2,693✔
1414
      mError("failed to set pid since %s", tstrerror(code));
×
1415
      return code;
×
1416
    }
1417

1418
    char name[TSDB_APP_NAME_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
2,693✔
1419
    (void)tsnprintf(&name[VARSTR_HEADER_SIZE], sizeof(name) - VARSTR_HEADER_SIZE, "%s", pApp->name);
2,693✔
1420
    varDataLen(name) = strlen(&name[VARSTR_HEADER_SIZE]);
2,693✔
1421
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,693✔
1422
    code = colDataSetVal(pColInfo, numOfRows, (const char *)name, false);
2,693✔
1423
    if (code != 0) {
2,693✔
1424
      mError("failed to set app name since %s", tstrerror(code));
×
1425
      return code;
×
1426
    }
1427

1428
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,693✔
1429
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->startTime, false);
2,693✔
1430
    if (code != 0) {
2,693✔
1431
      mError("failed to set start time since %s", tstrerror(code));
×
1432
      return code;
×
1433
    }
1434

1435
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,693✔
1436
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.numOfInsertsReq, false);
2,693✔
1437
    if (code != 0) {
2,693✔
1438
      mError("failed to set insert req since %s", tstrerror(code));
×
1439
      return code;
×
1440
    }
1441

1442
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,693✔
1443
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.numOfInsertRows, false);
2,693✔
1444
    if (code != 0) {
2,693✔
1445
      mError("failed to set insert rows since %s", tstrerror(code));
×
1446
      return code;
×
1447
    }
1448

1449
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,693✔
1450
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.insertElapsedTime, false);
2,693✔
1451
    if (code != 0) {
2,693✔
1452
      mError("failed to set insert elapsed time since %s", tstrerror(code));
×
1453
      return code;
×
1454
    }
1455

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

1463
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,693✔
1464
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.fetchBytes, false);
2,693✔
1465
    if (code != 0) {
2,693✔
1466
      mError("failed to set fetch bytes since %s", tstrerror(code));
×
1467
      return code;
×
1468
    }
1469

1470
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,693✔
1471
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.queryElapsedTime, false);
2,693✔
1472
    if (code != 0) {
2,693✔
1473
      mError("failed to set query elapsed time since %s", tstrerror(code));
×
1474
      return code;
×
1475
    }
1476

1477
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,693✔
1478
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.numOfSlowQueries, false);
2,693✔
1479
    if (code != 0) {
2,693✔
1480
      mError("failed to set slow queries since %s", tstrerror(code));
×
1481
      return code;
×
1482
    }
1483

1484
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,693✔
1485
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.totalRequests, false);
2,693✔
1486
    if (code != 0) {
2,693✔
1487
      mError("failed to set total requests since %s", tstrerror(code));
×
1488
      return code;
×
1489
    }
1490

1491
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,693✔
1492
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.currentRequests, false);
2,693✔
1493
    if (code != 0) {
2,693✔
1494
      mError("failed to set current requests since %s", tstrerror(code));
×
1495
      return code;
×
1496
    }
1497

1498
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,693✔
1499
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->lastAccessTimeMs, false);
2,693✔
1500
    if (code != 0) {
2,693✔
1501
      mError("failed to set last access time since %s", tstrerror(code));
×
1502
      return code;
×
1503
    }
1504

1505
    numOfRows++;
2,693✔
1506
  }
1507

1508
  pShow->numOfRows += numOfRows;
2,693✔
1509
  return numOfRows;
2,693✔
1510
}
1511

1512
static void mndCancelGetNextQuery(SMnode *pMnode, void *pIter) {
×
1513
  if (pIter != NULL) {
×
1514
    taosCacheDestroyIter(pIter);
×
1515
  }
1516
}
×
1517

1518
int32_t mndGetNumOfConnections(SMnode *pMnode) {
69✔
1519
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
69✔
1520
  return taosCacheGetNumOfObj(pMgmt->connCache);
69✔
1521
}
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