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

taosdata / TDengine / #4911

04 Jan 2026 09:05AM UTC coverage: 65.028% (-0.8%) from 65.864%
#4911

push

travis-ci

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

1206 of 4524 new or added lines in 22 files covered. (26.66%)

1517 existing lines in 134 files now uncovered.

195276 of 300296 relevant lines covered (65.03%)

116931714.52 hits per line

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

71.3
/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) {
385,454✔
96
  int32_t       code = 0;
385,454✔
97
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
385,454✔
98

99
  // in ms
100
  int32_t checkTime = CACHE_OBJ_KEEP_TIME * 1000;
385,454✔
101
  pMgmt->connCache = taosCacheInit(TSDB_DATA_TYPE_UINT, checkTime, false, (__cache_free_fn_t)mndFreeConn, "conn");
385,454✔
102
  if (pMgmt->connCache == NULL) {
385,454✔
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");
385,454✔
109
  if (pMgmt->appCache == NULL) {
385,454✔
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);
385,454✔
116
  mndSetMsgHandle(pMnode, TDMT_MND_CONNECT, mndProcessConnectReq);
385,454✔
117
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_QUERY, mndProcessKillQueryReq);
385,454✔
118
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_CONN, mndProcessKillConnReq);
385,454✔
119
  mndSetMsgHandle(pMnode, TDMT_MND_SERVER_VERSION, mndProcessSvrVerReq);
385,454✔
120

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

128
  TAOS_RETURN(code);
385,454✔
129
}
130

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

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

144
static void getUserIpFromConnObj(SConnObj *pConn, char *dst) {
10,783✔
145
  static char *none = "0.0.0.0";
146
  if (pConn->userIp != 0 && pConn->userIp != INADDR_NONE) {
10,783✔
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) {
10,783✔
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;
10,783✔
158
}
159
static void setUserInfo2Conn(SConnObj *connObj, char *userApp, uint32_t userIp, char *cInfo) {
24,062,123✔
160
  if (connObj == NULL) {
24,062,123✔
161
    return;
×
162
  }
163
  tstrncpy(connObj->userApp, userApp, sizeof(connObj->userApp));
24,062,123✔
164
  tstrncpy(connObj->cInfo, cInfo, sizeof(connObj->cInfo));
24,044,846✔
165
  connObj->userIp = userIp;
24,046,998✔
166
}
167
static void setUserInfoIpToConn(SConnObj *connObj, SIpRange *pRange) {
24,060,100✔
168
  int32_t code = 0;
24,060,100✔
169
  if (connObj == NULL) {
24,060,100✔
170
    return;
×
171
  }
172

173
  code = tIpUintToStr(pRange, &connObj->userDualIp);
24,060,100✔
174
  if (code != 0) {
24,058,353✔
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,438,234✔
183
                               int32_t pid, const char *app, int64_t startTime, const char *sVer) {
184
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
2,438,234✔
185

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

190
  int32_t  len = tsnprintf(connStr, sizeof(connStr), "%s%d%d%d%s", user, ip, port, pid, app);
2,438,234✔
191
  uint32_t connId = mndGenerateUid(connStr, len);
2,438,234✔
192
  if (startTime == 0) startTime = taosGetTimestampMs();
2,932,070✔
193

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

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

214
  SConnObj *pConn =
215
      taosCachePut(pMgmt->connCache, &connId, sizeof(uint32_t), &connObj, sizeof(connObj), CACHE_OBJ_KEEP_TIME * 1000);
2,438,234✔
216
  if (pConn == NULL) {
2,438,230✔
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,438,230✔
222
    return pConn;
2,438,230✔
223
  }
224
}
225

226

227

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

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

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

239
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &connId, sizeof(connId));
24,061,418✔
240
  if (pConn == NULL) {
24,064,285✔
241
    mDebug("conn:%u, already destroyed", connId);
493,237✔
242
    return NULL;
493,836✔
243
  }
244

245
  pConn->lastAccessTimeMs = taosGetTimestampMs();
23,569,247✔
246
  mTrace("conn:%u, acquired from cache, data:%p", pConn->id, pConn);
23,569,047✔
247
  return pConn;
23,559,995✔
248
}
249

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

254
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
26,009,282✔
255
  if (extendLifespan) taosCacheTryExtendLifeSpan(pMgmt->connCache, (void **)&pConn);
26,009,282✔
256
  taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
26,007,829✔
257
}
258

259
void *mndGetNextConn(SMnode *pMnode, SCacheIter *pIter) {
24,346✔
260
  SConnObj *pConn = NULL;
24,346✔
261
  bool      hasNext = taosCacheIterNext(pIter);
24,346✔
262
  if (hasNext) {
24,346✔
263
    size_t dataLen = 0;
18,676✔
264
    pConn = taosCacheIterGetData(pIter, &dataLen);
18,676✔
265
  } else {
266
    taosCacheDestroyIter(pIter);
5,670✔
267
  }
268

269
  return pConn;
24,346✔
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
UNCOV
281
int32_t mndCountUserConns(SMnode *pMnode, const char *user) {
×
UNCOV
282
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
×
UNCOV
283
  SCacheIter   *pIter = taosCacheCreateIter(pMgmt->connCache);
×
UNCOV
284
  if (pIter == NULL) {
×
285
    mError("failed to create conn cache iterator");
×
286
    return -1;
×
287
  }
288

UNCOV
289
  int32_t    count = 0;
×
UNCOV
290
  SConnObj  *pConn = NULL;
×
UNCOV
291
  while ((pConn = mndGetNextConn(pMnode, pIter)) != NULL) {
×
UNCOV
292
    if (strncmp(pConn->user, user, TSDB_USER_LEN) == 0) {
×
UNCOV
293
      count++;
×
294
    }
UNCOV
295
    mndReleaseConn(pMnode, pConn, true);
×
296
  }
297

UNCOV
298
  return count;
×
299
}
300

301

302

303
static int32_t verifyPassword(SUserObj* pUser, const char* inputPass) {
1,943,809✔
304
  int32_t code = 0;
1,943,809✔
305

306
  const char* currPass = pUser->passwords[0].pass;
1,943,809✔
307
  char pass[TSDB_PASSWORD_LEN] = {0};
1,943,874✔
308
  (void)memcpy(pass, inputPass, TSDB_PASSWORD_LEN);
1,943,874✔
309
  pass[TSDB_PASSWORD_LEN - 1] = 0;
1,943,837✔
310

311
  if (pUser->passEncryptAlgorithm != 0) {
1,943,837✔
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) {
252✔
317
      return code;
×
318
    }
319
  }
320

321
  // constant time comparison to prevent timing attack
322
  volatile uint8_t res = 0;
1,944,507✔
323
  for (size_t i = 0; i < sizeof(pass) - 1; i++) {
62,198,107✔
324
    res |= pass[i] ^ currPass[i];
60,253,531✔
325
  }
326

327
 return (res == 0) ? TSDB_CODE_SUCCESS: TSDB_CODE_MND_AUTH_FAILURE;
1,944,576✔
328
}
329

330

331

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

339

340

341
static int32_t mndProcessConnectReq(SRpcMsg *pReq) {
1,944,569✔
342
  int32_t          code = 0, lino = 0;
1,944,569✔
343

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

358
  if (token != NULL && mndGetCachedTokenInfo(token, &ti) == NULL) {
1,944,309✔
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);
1,944,309✔
362
  TAOS_CHECK_GOTO(taosCheckVersionCompatibleFromStr(connReq.sVer, td_version, 3), &lino, _OVER);
1,944,569✔
363
  TAOS_CHECK_GOTO(tVerifyConnectReqSignature(&connReq), &lino, _OVER);
1,944,602✔
364
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, user, &pUser), &lino, _OVER);
1,944,537✔
365

366
  SLoginInfo li = {0};
1,944,511✔
367
  mndGetUserLoginInfo(user, &li);
1,944,544✔
368
  TAOS_CHECK_GOTO(mndCheckConnectPrivilege(pMnode, pUser, token, &li), &lino, _OVER);
1,944,523✔
369

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

400
  if (connReq.db[0] != 0) {
1,944,515✔
401
    char db[TSDB_DB_FNAME_LEN] = {0};
411,460✔
402
    (void)snprintf(db, TSDB_DB_FNAME_LEN, "%d%s%s", pUser->acctId, TS_PATH_DELIMITER, connReq.db);
411,460✔
403
    pDb = mndAcquireDb(pMnode, db);
411,460✔
404
    if (pDb == NULL) {
411,460✔
405
      if (0 != strcmp(connReq.db, TSDB_INFORMATION_SCHEMA_DB) && (0 != strcmp(connReq.db, TSDB_PERFORMANCE_SCHEMA_DB))) {
636✔
406
        TAOS_CHECK_GOTO(TSDB_CODE_MND_DB_NOT_EXIST, &lino, _OVER);
56✔
407
      }
408
    }
409

410
    TAOS_CHECK_GOTO(mndCheckDbPrivilege(pMnode, user,RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB, pDb), NULL, _OVER);
411,404✔
411
  }
412

413
  if (connReq.connType == CONN_TYPE__AUTH_TEST) {
1,944,398✔
414
    code = 0;
×
415
    goto _OVER;
×
416
  }
417

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

424
  SConnectRsp connectRsp = {0};
1,944,233✔
425
  connectRsp.acctId = pUser->acctId;
1,944,398✔
426
  connectRsp.superUser = pUser->superUser;
1,944,229✔
427
  connectRsp.sysInfo = pUser->sysInfo;
1,944,394✔
428
  connectRsp.clusterId = pMnode->clusterId;
1,944,229✔
429
  connectRsp.connId = pConn->id;
1,944,233✔
430
  connectRsp.connType = connReq.connType;
1,943,527✔
431
  connectRsp.dnodeNum = mndGetDnodeSize(pMnode);
1,943,527✔
432
  connectRsp.svrTimestamp = taosGetTimestampSec();
1,944,398✔
433
  connectRsp.passVer = pUser->passVersion;
1,944,353✔
434
  connectRsp.authVer = pUser->authVersion;
1,944,353✔
435
  connectRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
1,943,973✔
436
  connectRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
1,943,973✔
437
  connectRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
1,943,973✔
438
  connectRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
1,943,973✔
439
  connectRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
1,943,973✔
440
  connectRsp.enableAuditDelete = tsEnableAuditDelete;
1,943,973✔
441
  connectRsp.enableAuditSelect = tsEnableAuditSelect;
1,943,973✔
442
  connectRsp.enableAuditInsert = tsEnableAuditInsert;
1,943,973✔
443
  connectRsp.auditLevel = tsAuditLevel;
1,943,973✔
444
  tstrncpy(connectRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
1,943,973✔
445
  connectRsp.whiteListVer = pUser->ipWhiteListVer;
1,943,973✔
446
  connectRsp.timeWhiteListVer = pUser->timeWhiteListVer;
1,944,398✔
447
  connectRsp.userId = pUser->uid;
1,944,398✔
448

449

450
  tstrncpy(connectRsp.sVer, td_version, sizeof(connectRsp.sVer));
1,943,696✔
451
  tstrncpy(connectRsp.user, user, sizeof(connectRsp.user));
1,943,696✔
452
  tstrncpy(connectRsp.tokenName, ti.name, sizeof(connectRsp.tokenName));
1,943,696✔
453
  (void)snprintf(connectRsp.sDetailVer, sizeof(connectRsp.sDetailVer), "ver:%s\nbuild:%s\ngitinfo:%s", td_version,
1,943,696✔
454
                 td_buildinfo, td_gitinfo);
455
  mndGetMnodeEpSet(pMnode, &connectRsp.epSet);
1,943,696✔
456

457
  int32_t contLen = tSerializeSConnectRsp(NULL, 0, &connectRsp);
1,944,398✔
458
  if (contLen < 0) {
1,942,953✔
459
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
460
  }
461
  void *pRsp = rpcMallocCont(contLen);
1,942,953✔
462
  if (pRsp == NULL) {
1,943,613✔
463
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
464
  }
465

466
  contLen = tSerializeSConnectRsp(pRsp, contLen, &connectRsp);
1,943,613✔
467
  if (contLen < 0) {
1,944,354✔
468
    rpcFreeCont(pRsp);
×
469
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
470
  }
471

472
  pReq->info.rspLen = contLen;
1,944,354✔
473
  pReq->info.rsp = pRsp;
1,944,386✔
474

475
  mGDebug("user:%s, login from %s:%d, conn:%u, app:%s, db:%s", user, ip, port, pConn->id, connReq.app, connReq.db);
1,944,061✔
476
  code = 0;
1,944,398✔
477

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

489
_OVER:
1,943,103✔
490
  if (code != 0) {
1,944,602✔
491
    mGError("user:%s, failed to login from %s since %s, line:%d, db:%s", user, ip, tstrerror(code), lino, connReq.db);
204✔
492
  }
493

494
  mndReleaseUser(pMnode, pUser);
1,944,602✔
495
  mndReleaseDb(pMnode, pDb);
1,944,602✔
496
  mndReleaseConn(pMnode, pConn, true);
1,944,602✔
497

498
  TAOS_RETURN(code);
1,944,602✔
499
}
500

501

502

503
static int32_t mndSaveQueryList(SConnObj *pConn, SQueryHbReqBasic *pBasic) {
24,058,779✔
504
  taosWLockLatch(&pConn->queryLock);
24,058,779✔
505

506
  taosArrayDestroyEx(pConn->pQueries, tFreeClientHbQueryDesc);
24,064,809✔
507

508
  pConn->pQueries = pBasic->queryDesc;
24,061,078✔
509
  pConn->numOfQueries = pBasic->queryDesc ? taosArrayGetSize(pBasic->queryDesc) : 0;
24,062,330✔
510
  pBasic->queryDesc = NULL;
24,062,809✔
511

512
  mDebug("queries updated in conn %u, num:%d", pConn->id, pConn->numOfQueries);
24,062,497✔
513

514
  taosWUnLockLatch(&pConn->queryLock);
24,068,462✔
515

516
  return TSDB_CODE_SUCCESS;
24,064,087✔
517
}
518

519
static SAppObj *mndCreateApp(SMnode *pMnode, const SIpAddr *pAddr, const SAppHbReq *pReq) {
649,247✔
520
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
649,247✔
521

522
  SAppObj app;
649,085✔
523
  app.appId = pReq->appId;
649,247✔
524
  app.cliAddr = *pAddr;
649,247✔
525
  app.pid = pReq->pid;
649,247✔
526
  tstrncpy(app.name, pReq->name, sizeof(app.name));
649,247✔
527
  app.startTime = pReq->startTime;
649,247✔
528
  (void)memcpy(&app.summary, &pReq->summary, sizeof(pReq->summary));
649,247✔
529
  app.lastAccessTimeMs = taosGetTimestampMs();
649,247✔
530

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

539
  mTrace("app %" PRIx64 " is put into cache", pReq->appId);
649,247✔
540
  return pApp;
649,247✔
541
}
542

543
static void mndFreeApp(SAppObj *pApp) { mTrace("app %" PRIx64 " is destroyed", pApp->appId); }
649,247✔
544

545
static SAppObj *mndAcquireApp(SMnode *pMnode, int64_t appId) {
24,063,817✔
546
  terrno = 0;
24,063,817✔
547
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
24,062,947✔
548

549
  SAppObj *pApp = taosCacheAcquireByKey(pMgmt->appCache, &appId, sizeof(appId));
24,061,782✔
550
  if (pApp == NULL) {
24,058,686✔
551
    mDebug("app %" PRIx64 " not in cache", appId);
649,247✔
552
    return NULL;
649,247✔
553
  }
554

555
  pApp->lastAccessTimeMs = (uint64_t)taosGetTimestampMs();
23,407,425✔
556

557
  mTrace("app %" PRIx64 " acquired from cache", appId);
23,410,399✔
558
  return pApp;
23,413,382✔
559
}
560

561
static void mndReleaseApp(SMnode *pMnode, SAppObj *pApp) {
24,055,915✔
562
  if (pApp == NULL) return;
24,055,915✔
563
  mTrace("release app %" PRIx64 " to cache", pApp->appId);
24,055,915✔
564

565
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
24,055,915✔
566
  taosCacheRelease(pMgmt->appCache, (void **)&pApp, false);
24,056,590✔
567
}
568

569
SAppObj *mndGetNextApp(SMnode *pMnode, SCacheIter *pIter) {
5,016✔
570
  SAppObj *pApp = NULL;
5,016✔
571
  bool     hasNext = taosCacheIterNext(pIter);
5,016✔
572
  if (hasNext) {
5,016✔
573
    size_t dataLen = 0;
2,508✔
574
    pApp = taosCacheIterGetData(pIter, &dataLen);
2,508✔
575
  } else {
576
    taosCacheDestroyIter(pIter);
2,508✔
577
  }
578

579
  return pApp;
5,016✔
580
}
581

582
static void mndCancelGetNextApp(SMnode *pMnode, void *pIter) {
×
583
  if (pIter != NULL) {
×
584
    taosCacheDestroyIter(pIter);
×
585
  }
586
}
×
587

588
static SClientHbRsp *mndMqHbBuildRsp(SMnode *pMnode, SClientHbReq *pReq) {
×
589
  //
590
  return NULL;
×
591
}
592

593
static int32_t mndUpdateAppInfo(SMnode *pMnode, SClientHbReq *pHbReq, const SRpcConnInfo *connInfo) {
24,063,817✔
594
  int32_t    code = 0;
24,063,817✔
595
  SAppHbReq *pReq = &pHbReq->app;
24,063,817✔
596
  SAppObj   *pApp = mndAcquireApp(pMnode, pReq->appId);
24,063,279✔
597
  if (pApp == NULL) {
24,051,969✔
598
    pApp = mndCreateApp(pMnode, &connInfo->cliAddr, pReq);
649,247✔
599
    if (pApp == NULL) {
649,247✔
600
      mError("failed to create new app %" PRIx64 " since %s", pReq->appId, terrstr());
×
601
      code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
602
      if (terrno != 0) code = terrno;
×
603
      TAOS_RETURN(code);
×
604
    } else {
605
      mDebug("a new app %" PRIx64 " is created", pReq->appId);
649,247✔
606
      mndReleaseApp(pMnode, pApp);
649,247✔
607
      return TSDB_CODE_SUCCESS;
649,247✔
608
    }
609
  }
610

611
  (void)memcpy(&pApp->summary, &pReq->summary, sizeof(pReq->summary));
23,402,722✔
612

613
  mndReleaseApp(pMnode, pApp);
23,411,781✔
614

615
  return TSDB_CODE_SUCCESS;
23,403,768✔
616
}
617

618
static int32_t mndGetOnlineDnodeNum(SMnode *pMnode, int32_t *num) {
19,938,339✔
619
  SSdb      *pSdb = pMnode->pSdb;
19,938,339✔
620
  SDnodeObj *pDnode = NULL;
19,938,187✔
621
  int64_t    curMs = taosGetTimestampMs();
19,937,432✔
622
  void      *pIter = NULL;
19,937,432✔
623

624
  while (true) {
34,121,138✔
625
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
54,058,570✔
626
    if (pIter == NULL) break;
54,059,452✔
627

628
    bool online = mndIsDnodeOnline(pDnode, curMs);
34,120,636✔
629
    if (online) {
34,118,073✔
630
      (*num)++;
33,026,553✔
631
    }
632

633
    sdbRelease(pSdb, pDnode);
34,117,816✔
634
  }
635

636
  return TSDB_CODE_SUCCESS;
19,938,816✔
637
}
638

639
static int32_t mndProcessQueryHeartBeat(SMnode *pMnode, SRpcMsg *pMsg, SClientHbReq *pHbReq,
24,063,817✔
640
                                        SClientHbBatchRsp *pBatchRsp, SConnPreparedObj *pObj) {
641
  int32_t       code = 0;
24,063,817✔
642
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
24,063,817✔
643
  SClientHbRsp  hbRsp = {.connKey = pHbReq->connKey, .status = 0, .info = NULL, .query = NULL};
24,062,686✔
644

645
  if (0 != pHbReq->app.appId) {
24,063,817✔
646
    TAOS_CHECK_RETURN(mndUpdateAppInfo(pMnode, pHbReq, &pMsg->info.conn));
24,062,310✔
647
  }
648

649
  if (pHbReq->query) {
24,047,083✔
650
    SQueryHbReqBasic *pBasic = pHbReq->query;
24,057,259✔
651
    SConnObj *pConn = mndAcquireConn(pMnode, pBasic->connId);
24,055,869✔
652
    if (pConn == NULL) {
24,052,879✔
653
      SRpcConnInfo  connInfo = pMsg->info.conn;
493,836✔
654
      const char* user = pHbReq->user;
493,836✔
655
      pConn = mndCreateConn(pMnode, user, pHbReq->tokenName, pHbReq->connKey.connType, &connInfo.cliAddr, pHbReq->app.pid,
493,836✔
656
                            pHbReq->app.name, 0, pHbReq->sVer);
493,836✔
657
      if (pConn == NULL) {
493,836✔
658
        mError("user:%s, conn:%u is freed and failed to create new since %s", user, pBasic->connId, terrstr());
×
659
        code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
660
        if (terrno != 0) code = terrno;
×
661
        TAOS_RETURN(code);
×
662
      } else {
663
        mDebug("user:%s, conn:%u is freed, will create a new conn:%u", user, pBasic->connId, pConn->id);
493,836✔
664
      }
665
    }
666

667
    setUserInfo2Conn(pConn, pHbReq->userApp, pHbReq->userIp, pHbReq->cInfo);
24,052,879✔
668
    setUserInfoIpToConn(pConn, &pHbReq->userDualIp);
24,053,773✔
669

670
    SQueryHbRspBasic *rspBasic = taosMemoryCalloc(1, sizeof(SQueryHbRspBasic));
24,049,598✔
671
    if (rspBasic == NULL) {
24,046,714✔
672
      mndReleaseConn(pMnode, pConn, true);
×
673
      code = terrno;
×
674
      mError("user:%s, conn:%u failed to process hb while since %s", pConn->user, pBasic->connId, terrstr());
×
675
      TAOS_RETURN(code);
×
676
    }
677

678
    TAOS_CHECK_RETURN(mndSaveQueryList(pConn, pBasic));
24,046,714✔
679
    if (pConn->killed != 0) {
24,064,087✔
680
      rspBasic->killConnection = 1;
×
681
    }
682

683
    if (pConn->killId != 0) {
24,064,549✔
684
      rspBasic->killRid = pConn->killId;
4✔
685
      pConn->killId = 0;
4✔
686
    }
687

688
    rspBasic->connId = pConn->id;
24,064,556✔
689
    rspBasic->connId = pConn->id;
24,064,556✔
690
    rspBasic->totalDnodes = pObj->totalDnodes;
24,064,422✔
691
    rspBasic->onlineDnodes = pObj->onlineDnodes;
24,064,069✔
692
    rspBasic->epSet = pObj->epSet;
24,063,741✔
693
    rspBasic->pQnodeList = taosArrayDup(pObj->pQnodeList, NULL);
24,064,556✔
694

695
    mndReleaseConn(pMnode, pConn, true);
24,064,884✔
696

697
    hbRsp.query = rspBasic;
24,064,556✔
698
  } else {
699
    mDebug("no query info in hb msg");
×
700
  }
701

702
  int32_t kvNum = taosHashGetSize(pHbReq->info);
24,064,556✔
703
  if (NULL == pHbReq->info || kvNum <= 0) {
24,063,175✔
704
    if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
12,342,139✔
705
      mError("failed to put rsp into array, but continue at this heartbeat");
×
706
    }
707
    return TSDB_CODE_SUCCESS;
6,171,924✔
708
  }
709

710
  hbRsp.info = taosArrayInit(kvNum, sizeof(SKv));
17,892,960✔
711
  if (NULL == hbRsp.info) {
17,892,960✔
712
    mError("taosArrayInit %d rsp kv failed", kvNum);
×
713
    code = terrno;
×
714
    tFreeClientHbRsp(&hbRsp);
715
    TAOS_RETURN(code);
×
716
  }
717

718
#ifdef TD_ENTERPRISE
719
  bool             needCheck = true;
17,892,960✔
720
  int32_t          key = HEARTBEAT_KEY_DYN_VIEW;
17,892,960✔
721
  SDynViewVersion *pDynViewVer = NULL;
17,892,911✔
722
  SKv             *pKv = taosHashGet(pHbReq->info, &key, sizeof(key));
17,892,911✔
723
  if (NULL != pKv) {
17,892,911✔
UNCOV
724
    pDynViewVer = pKv->value;
×
UNCOV
725
    mTrace("recv view dyn ver, bootTs:%" PRId64 ", ver:%" PRIu64, pDynViewVer->svrBootTs, pDynViewVer->dynViewVer);
×
726

UNCOV
727
    SDynViewVersion *pRspVer = NULL;
×
UNCOV
728
    if (0 != (code = mndValidateDynViewVersion(pMnode, pDynViewVer, &needCheck, &pRspVer))) {
×
729
      TAOS_RETURN(code);
×
730
    }
731

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

744
  void *pIter = taosHashIterate(pHbReq->info, NULL);
17,892,911✔
745
  while (pIter != NULL) {
45,085,080✔
746
    SKv *kv = pIter;
27,192,120✔
747

748
    switch (kv->key) {
27,192,120✔
749
      case HEARTBEAT_KEY_USER_AUTHINFO: {
17,892,960✔
750
        void   *rspMsg = NULL;
17,892,960✔
751
        int32_t rspLen = 0;
17,892,960✔
752
        (void)mndValidateUserAuthInfo(pMnode, kv->value, kv->valueLen / sizeof(SUserAuthVersion), &rspMsg, &rspLen,
17,892,960✔
753
                                      pObj->ipWhiteListVer);
754
        if (rspMsg && rspLen > 0) {
17,892,960✔
755
          SKv kv1 = {.key = HEARTBEAT_KEY_USER_AUTHINFO, .valueLen = rspLen, .value = rspMsg};
763,672✔
756
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
1,527,344✔
757
            mError("failed to put kv into array, but continue at this heartbeat");
×
758
          }
759
        }
760
        break;
17,892,960✔
761
      }
762
      case HEARTBEAT_KEY_DBINFO: {
5,523,079✔
763
        void   *rspMsg = NULL;
5,523,079✔
764
        int32_t rspLen = 0;
5,523,079✔
765
        (void)mndValidateDbInfo(pMnode, kv->value, kv->valueLen / sizeof(SDbCacheInfo), &rspMsg, &rspLen);
5,523,079✔
766
        if (rspMsg && rspLen > 0) {
5,523,079✔
767
          SKv kv1 = {.key = HEARTBEAT_KEY_DBINFO, .valueLen = rspLen, .value = rspMsg};
5,523,079✔
768
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
11,046,158✔
769
            mError("failed to put kv into array, but continue at this heartbeat");
×
770
          }
771
        }
772
        break;
5,523,079✔
773
      }
774
      case HEARTBEAT_KEY_STBINFO: {
3,776,081✔
775
        void   *rspMsg = NULL;
3,776,081✔
776
        int32_t rspLen = 0;
3,776,081✔
777
        (void)mndValidateStbInfo(pMnode, kv->value, kv->valueLen / sizeof(SSTableVersion), &rspMsg, &rspLen);
3,776,081✔
778
        if (rspMsg && rspLen > 0) {
3,776,081✔
779
          SKv kv1 = {.key = HEARTBEAT_KEY_STBINFO, .valueLen = rspLen, .value = rspMsg};
3,776,081✔
780
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
7,552,162✔
781
            mError("failed to put kv into array, but continue at this heartbeat");
×
782
          }
783
        }
784
        break;
3,776,081✔
785
      }
786
#ifdef TD_ENTERPRISE
UNCOV
787
      case HEARTBEAT_KEY_DYN_VIEW: {
×
UNCOV
788
        break;
×
789
      }
UNCOV
790
      case HEARTBEAT_KEY_VIEWINFO: {
×
UNCOV
791
        if (!needCheck) {
×
792
          break;
×
793
        }
794

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

825
    pIter = taosHashIterate(pHbReq->info, pIter);
27,192,120✔
826
  }
827

828
  if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
35,785,920✔
829
    if (terrno != 0) code = terrno;
×
830
  }
831
  TAOS_RETURN(code);
17,892,960✔
832
}
833

834
static int32_t mndProcessHeartBeatReq(SRpcMsg *pReq) {
19,936,727✔
835
  int32_t code = 0;
19,936,727✔
836
  int32_t lino = 0;
19,936,727✔
837
  SMnode *pMnode = pReq->info.node;
19,936,727✔
838

839
  SClientHbBatchReq batchReq = {0};
19,938,511✔
840
  if (tDeserializeSClientHbBatchReq(pReq->pCont, pReq->contLen, &batchReq) != 0) {
19,935,760✔
841
    taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
×
842
    code = TSDB_CODE_INVALID_MSG;
×
843
    TAOS_RETURN(code);
×
844
  }
845

846
  SConnPreparedObj obj = {0};
19,938,507✔
847
  obj.totalDnodes = mndGetDnodeSize(pMnode);
19,938,507✔
848
  obj.ipWhiteListVer = batchReq.ipWhiteListVer;
19,938,289✔
849
  TAOS_CHECK_RETURN(mndGetOnlineDnodeNum(pMnode, &obj.onlineDnodes));
19,938,289✔
850
  mndGetMnodeEpSet(pMnode, &obj.epSet);
19,937,132✔
851
  TAOS_CHECK_RETURN(mndCreateQnodeList(pMnode, &obj.pQnodeList, -1));
19,939,118✔
852

853
  SClientHbBatchRsp batchRsp = {0};
19,936,367✔
854
  batchRsp.svrTimestamp = taosGetTimestampSec();
19,937,366✔
855
  batchRsp.rsps = taosArrayInit(0, sizeof(SClientHbRsp));
19,934,693✔
856
  if (batchRsp.rsps == NULL) {
19,935,670✔
857
    TAOS_CHECK_EXIT(terrno);
×
858
  }
859
  batchRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
19,935,670✔
860
  batchRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
19,935,670✔
861
  batchRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
19,935,670✔
862
  tstrncpy(batchRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
19,935,670✔
863
  batchRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
19,935,670✔
864
  batchRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
19,935,670✔
865
  batchRsp.enableAuditDelete = tsEnableAuditDelete;
19,935,670✔
866
  batchRsp.enableAuditSelect = tsEnableAuditSelect;
19,935,670✔
867
  batchRsp.enableAuditInsert = tsEnableAuditInsert;
19,935,670✔
868
  batchRsp.auditLevel = tsAuditLevel;
19,935,670✔
869
  batchRsp.enableStrongPass = tsEnableStrongPassword;
19,935,670✔
870

871
  int32_t sz = taosArrayGetSize(batchReq.reqs);
19,935,670✔
872
  for (int i = 0; i < sz; i++) {
44,001,523✔
873
    SClientHbReq *pHbReq = taosArrayGet(batchReq.reqs, i);
24,063,817✔
874
    if (pHbReq->connKey.connType == CONN_TYPE__QUERY || pHbReq->connKey.connType == CONN_TYPE__TMQ) {
24,063,228✔
875
      TAOS_CHECK_EXIT(mndProcessQueryHeartBeat(pMnode, pReq, pHbReq, &batchRsp, &obj));
24,063,513✔
876
    } 
877
  }
878
  taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
19,937,706✔
879

880
  int32_t tlen = tSerializeSClientHbBatchRsp(NULL, 0, &batchRsp);
19,938,257✔
881
  if (tlen < 0) {
19,932,077✔
882
    TAOS_CHECK_EXIT(tlen);
×
883
  }
884
  void *buf = rpcMallocCont(tlen);
19,932,077✔
885
  if (!buf) {
19,929,126✔
886
    TAOS_CHECK_EXIT(terrno);
×
887
  }
888
  tlen = tSerializeSClientHbBatchRsp(buf, tlen, &batchRsp);
19,929,126✔
889
  if (tlen < 0) {
19,936,111✔
890
    rpcFreeCont(buf);
×
891
    TAOS_CHECK_EXIT(tlen);
×
892
  }
893
  pReq->info.rspLen = tlen;
19,936,111✔
894
  pReq->info.rsp = buf;
19,935,972✔
895
_exit:
19,931,443✔
896
  tFreeClientHbBatchRsp(&batchRsp);
897

898
  taosArrayDestroy(obj.pQnodeList);
19,938,556✔
899

900
  TAOS_RETURN(code);
19,936,092✔
901
}
902

903
static int32_t mndProcessKillQueryReq(SRpcMsg *pReq) {
4✔
904
  int32_t       code = 0;
4✔
905
  SMnode       *pMnode = pReq->info.node;
4✔
906
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
4✔
907

908
  SKillQueryReq killReq = {0};
4✔
909
  TAOS_CHECK_RETURN(tDeserializeSKillQueryReq(pReq->pCont, pReq->contLen, &killReq));
4✔
910

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

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

938
static int32_t mndProcessKillConnReq(SRpcMsg *pReq) {
145✔
939
  int32_t       code = 0;
145✔
940
  SMnode       *pMnode = pReq->info.node;
145✔
941
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
145✔
942

943
  SKillConnReq killReq = {0};
145✔
944
  TAOS_CHECK_RETURN(tDeserializeSKillConnReq(pReq->pCont, pReq->contLen, &killReq));
145✔
945

946
  TAOS_CHECK_RETURN(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_KILL_CONN));
145✔
947

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

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

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

981
  pReq->info.rspLen = contLen;
×
982
  pReq->info.rsp = pRsp;
×
983

984
_exit:
×
985

986
  TAOS_RETURN(code);
×
987
}
988

989
static int32_t mndRetrieveConns(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
3,154✔
990
  SMnode   *pMnode = pReq->info.node;
3,154✔
991
  SSdb     *pSdb = pMnode->pSdb;
3,154✔
992
  int32_t   numOfRows = 0;
3,154✔
993
  int32_t   cols = 0;
3,154✔
994
  int32_t   code = 0;
3,154✔
995
  SConnObj *pConn = NULL;
3,154✔
996

997
  if (pShow->pIter == NULL) {
3,154✔
998
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
3,154✔
999
    pShow->pIter = taosCacheCreateIter(pMgmt->connCache);
3,154✔
1000
    if (!pShow->pIter) return terrno;
3,154✔
1001
  }
1002

1003
  while (numOfRows < rows) {
13,044✔
1004
    pConn = mndGetNextConn(pMnode, pShow->pIter);
13,044✔
1005
    if (pConn == NULL) {
13,044✔
1006
      pShow->pIter = NULL;
3,154✔
1007
      break;
3,154✔
1008
    }
1009

1010
    if ((taosGetTimestampMs() - pConn->lastAccessTimeMs) > ((int64_t)CACHE_OBJ_KEEP_TIME * 1000)) {
9,890✔
1011
      continue;
1,740✔
1012
    }
1013

1014
    cols = 0;
8,150✔
1015

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

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

1032
    char app[TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE];
8,150✔
1033
    STR_TO_VARSTR(app, pConn->app);
8,150✔
1034
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8,150✔
1035
    code = colDataSetVal(pColInfo, numOfRows, (const char *)app, false);
8,150✔
1036
    if (code != 0) {
8,150✔
1037
      mError("failed to set app since %s", tstrerror(code));
×
1038
      return code;
×
1039
    }
1040

1041
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8,150✔
1042
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->pid, false);
8,150✔
1043
    if (code != 0) {
8,150✔
1044
      mError("failed to set conn id:%u since %s", pConn->id, tstrerror(code));
×
1045
      return code;
×
1046
    }
1047

1048
    char addr[IP_RESERVE_CAP] = {0};
8,150✔
1049
    char endpoint[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
8,150✔
1050
    if (tsnprintf(addr, sizeof(addr), "%s:%d", IP_ADDR_STR(&pConn->addr), pConn->addr.port) >= sizeof(addr)) {
8,150✔
1051
      code = TSDB_CODE_OUT_OF_RANGE;
×
1052
      mError("failed to set endpoint since %s", tstrerror(code));
×
1053
      return code;
×
1054
    }
1055

1056
    STR_TO_VARSTR(endpoint, addr);
8,150✔
1057

1058
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8,150✔
1059
    code = colDataSetVal(pColInfo, numOfRows, (const char *)endpoint, false);
8,150✔
1060
    if (code != 0) {
8,150✔
1061
      mError("failed to set endpoint since %s", tstrerror(code));
×
1062
      return code;
×
1063
    }
1064

1065
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8,150✔
1066
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->loginTimeMs, false);
8,150✔
1067
    if (code != 0) {
8,150✔
1068
      mError("failed to set login time since %s", tstrerror(code));
×
1069
      return code;
×
1070
    }
1071

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

1079
    char userApp[TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE];
8,150✔
1080
    STR_TO_VARSTR(userApp, pConn->userApp);
8,150✔
1081
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8,150✔
1082
    code = colDataSetVal(pColInfo, numOfRows, (const char *)userApp, false);
8,150✔
1083
    if (code != 0) {
8,150✔
1084
      mError("failed to set user app since %s", tstrerror(code));
×
1085
      return code;
×
1086
    }
1087

1088
    char userIp[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
8,150✔
1089
    getUserIpFromConnObj(pConn, userIp);
8,150✔
1090

1091
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8,150✔
1092
    code = colDataSetVal(pColInfo, numOfRows, (const char *)userIp, false);
8,150✔
1093
    if (code != 0) {
8,150✔
1094
      mError("failed to set user ip since %s", tstrerror(code));
×
1095
      return code;
×
1096
    }
1097

1098
    char ver[TSDB_VERSION_LEN + VARSTR_HEADER_SIZE];
8,150✔
1099
    STR_TO_VARSTR(ver, pConn->sVer);
8,150✔
1100
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8,150✔
1101
    code = colDataSetVal(pColInfo, numOfRows, (const char *)ver, false);
8,150✔
1102
    if (code != 0) {
8,150✔
1103
      mError("failed to set ver since %s", tstrerror(code));
×
1104
      return code;
×
1105
    }
1106

1107
    char cInfo[CONNECTOR_INFO_LEN + VARSTR_HEADER_SIZE];
8,150✔
1108
    STR_TO_VARSTR(cInfo, pConn->cInfo);
8,150✔
1109
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8,150✔
1110
    code = colDataSetVal(pColInfo, numOfRows, (const char *)cInfo, false);
8,150✔
1111
    if (code != 0) {
8,150✔
1112
      mError("failed to set connector info since %s", tstrerror(code));
×
1113
      return code;
×
1114
    }
1115

1116
    char type[16 + VARSTR_HEADER_SIZE];
8,150✔
1117
    STR_TO_VARSTR(type, pConn->connType == CONN_TYPE__QUERY ? "QUERY" : (pConn->connType == CONN_TYPE__TMQ ? "TMQ" : "UNKNOWN"));
8,150✔
1118
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8,150✔
1119
    code = colDataSetVal(pColInfo, numOfRows, (const char *)type, false);
8,150✔
1120
    if (code != 0) {
8,150✔
1121
      mError("failed to set type info since %s", tstrerror(code));
×
1122
      return code;
×
1123
    }
1124

1125
    char tokenName[TSDB_TOKEN_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
8,150✔
1126
    STR_TO_VARSTR(tokenName, pConn->tokenName);
8,150✔
1127
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8,150✔
1128
    code = colDataSetVal(pColInfo, numOfRows, (const char *)tokenName, false);
8,150✔
1129
    if (code != 0) {
8,150✔
1130
      mError("failed to set token name since %s", tstrerror(code));
×
1131
      return code;
×
1132
    }
1133

1134
    numOfRows++;
8,150✔
1135
  }
1136

1137
  pShow->numOfRows += numOfRows;
3,154✔
1138
  return numOfRows;
3,154✔
1139
}
1140

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

1159
  int32_t i = offset;
2,633✔
1160
  for (; i < numOfQueries && (i - offset) < rowsToPack; ++i) {
5,266✔
1161
    int32_t     curRowIndex = pBlock->info.rows;
2,633✔
1162
    SQueryDesc *pQuery = taosArrayGet(pConn->pQueries, i);
2,633✔
1163
    cols = 0;
2,633✔
1164

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

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

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

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

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

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

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

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

1241
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,633✔
1242
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->useconds, false);
2,633✔
1243
    if (code != 0) {
2,633✔
1244
      mError("failed to set useconds since %s", tstrerror(code));
×
1245
      taosRUnLockLatch(&pConn->queryLock);
×
1246
      return code;
×
1247
    }
1248

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

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

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

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

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

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

1318
    char userIp[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
2,633✔
1319
    getUserIpFromConnObj(pConn, userIp);
2,633✔
1320

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

1329
    pBlock->info.rows++;
2,633✔
1330
  }
1331

1332
  taosRUnLockLatch(&pConn->queryLock);
2,633✔
1333
  return i - offset;
2,633✔
1334
}
1335

1336
static int32_t mndRetrieveQueries(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
2,516✔
1337
  SMnode   *pMnode = pReq->info.node;
2,516✔
1338
  SSdb     *pSdb = pMnode->pSdb;
2,516✔
1339
  int32_t   numOfRows = 0;
2,516✔
1340
  SConnObj *pConn = NULL;
2,516✔
1341

1342
  if (pShow->pIter == NULL) {
2,516✔
1343
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
2,516✔
1344
    pShow->pIter = taosCacheCreateIter(pMgmt->connCache);
2,516✔
1345
    if (!pShow->pIter) return terrno;
2,516✔
1346
  }
1347

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

1358
  while (numOfRows < rows) {
11,302✔
1359
    pConn = mndGetNextConn(pMnode, pShow->pIter);
11,302✔
1360
    if (pConn == NULL) {
11,302✔
1361
      pShow->pIter = NULL;
2,516✔
1362
      break;
2,516✔
1363
    }
1364

1365
    int32_t packedRows = packQueriesIntoBlock(pShow, pConn, pBlock, 0, rows - numOfRows);
8,786✔
1366
    pShow->curIterPackedRows = packedRows;
8,786✔
1367
    numOfRows += packedRows;
8,786✔
1368
  }
1369
  pShow->numOfRows += numOfRows;
2,516✔
1370
  return numOfRows;
2,516✔
1371
}
1372

1373
static int32_t mndRetrieveApps(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
2,508✔
1374
  SMnode  *pMnode = pReq->info.node;
2,508✔
1375
  SSdb    *pSdb = pMnode->pSdb;
2,508✔
1376
  int32_t  numOfRows = 0;
2,508✔
1377
  int32_t  cols = 0;
2,508✔
1378
  SAppObj *pApp = NULL;
2,508✔
1379
  int32_t  code = 0;
2,508✔
1380

1381
  if (pShow->pIter == NULL) {
2,508✔
1382
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
2,508✔
1383
    pShow->pIter = taosCacheCreateIter(pMgmt->appCache);
2,508✔
1384
    if (!pShow->pIter) return terrno;
2,508✔
1385
  }
1386

1387
  while (numOfRows < rows) {
5,016✔
1388
    pApp = mndGetNextApp(pMnode, pShow->pIter);
5,016✔
1389
    if (pApp == NULL) {
5,016✔
1390
      pShow->pIter = NULL;
2,508✔
1391
      break;
2,508✔
1392
    }
1393

1394
    cols = 0;
2,508✔
1395

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

1403
    char ip[TD_IP_LEN + VARSTR_HEADER_SIZE] = {0};
2,508✔
1404
    char buf[IP_RESERVE_CAP] = {0};
2,508✔
1405
    snprintf(buf, sizeof(buf), "%s", IP_ADDR_STR(&pApp->cliAddr));
2,508✔
1406
    STR_TO_VARSTR(ip, buf);
2,508✔
1407

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1509
    numOfRows++;
2,508✔
1510
  }
1511

1512
  pShow->numOfRows += numOfRows;
2,508✔
1513
  return numOfRows;
2,508✔
1514
}
1515

1516
static void mndCancelGetNextQuery(SMnode *pMnode, void *pIter) {
×
1517
  if (pIter != NULL) {
×
1518
    taosCacheDestroyIter(pIter);
×
1519
  }
1520
}
×
1521

1522
int32_t mndGetNumOfConnections(SMnode *pMnode) {
60✔
1523
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
60✔
1524
  return taosCacheGetNumOfObj(pMgmt->connCache);
60✔
1525
}
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