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

taosdata / TDengine / #5014

03 Apr 2026 03:59PM UTC coverage: 72.256% (-0.06%) from 72.317%
#5014

push

travis-ci

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

4054 of 5985 new or added lines in 68 files covered. (67.74%)

13285 existing lines in 168 files now uncovered.

257272 of 356056 relevant lines covered (72.26%)

133154720.42 hits per line

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

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

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

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

128
  TAOS_RETURN(code);
476,772✔
129
}
130

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

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

144
static void getUserIpFromConnObj(SConnObj *pConn, char *dst) {
41,340✔
145
  static char *none = "0.0.0.0";
146
  if (pConn->userIp != 0 && pConn->userIp != INADDR_NONE) {
41,340✔
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) {
41,340✔
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;
41,340✔
158
}
159
static void setUserInfo2Conn(SConnObj *connObj, char *userApp, uint32_t userIp, char *cInfo) {
34,069,115✔
160
  if (connObj == NULL) {
34,069,115✔
161
    return;
×
162
  }
163
  tstrncpy(connObj->userApp, userApp, sizeof(connObj->userApp));
34,069,115✔
164
  tstrncpy(connObj->cInfo, cInfo, sizeof(connObj->cInfo));
34,056,658✔
165
  connObj->userIp = userIp;
34,057,636✔
166
}
167
static void setUserInfoIpToConn(SConnObj *connObj, SIpRange *pRange) {
34,065,912✔
168
  int32_t code = 0;
34,065,912✔
169
  if (connObj == NULL) {
34,065,912✔
170
    return;
×
171
  }
172

173
  code = tIpUintToStr(pRange, &connObj->userDualIp);
34,065,912✔
174
  if (code != 0) {
34,067,915✔
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,
3,450,997✔
183
                               int32_t pid, const char *app, int64_t startTime, const char *sVer) {
184
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
3,450,997✔
185

186
  char     connStr[255] = {0};
3,450,997✔
187
  char    *ip = IP_ADDR_STR(pAddr);
3,451,046✔
188
  uint16_t port = pAddr->port;
3,450,999✔
189

190
  int32_t  len = tsnprintf(connStr, sizeof(connStr), "%s%d%d%d%s", user, ip, port, pid, app);
3,450,999✔
191
  uint32_t connId = mndGenerateUid(connStr, len);
3,451,048✔
192
  if (startTime == 0) startTime = taosGetTimestampMs();
4,095,009✔
193

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

208
  connObj.lastAccessTimeMs = connObj.loginTimeMs;
3,451,048✔
209
  tstrncpy(connObj.user, user, sizeof(connObj.user));
3,451,048✔
210
  tstrncpy(connObj.tokenName, tokenName, sizeof(connObj.tokenName));
3,450,999✔
211
  tstrncpy(connObj.app, app, sizeof(connObj.app));
3,451,048✔
212
  tstrncpy(connObj.sVer, sVer, sizeof(connObj.sVer));
3,450,999✔
213

214
  SConnObj *pConn =
215
      taosCachePut(pMgmt->connCache, &connId, sizeof(uint32_t), &connObj, sizeof(connObj), CACHE_OBJ_KEEP_TIME * 1000);
3,450,997✔
216
  if (pConn == NULL) {
3,449,169✔
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);
3,449,169✔
222
    return pConn;
3,449,129✔
223
  }
224
}
225

226

227

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

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

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

239
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &connId, sizeof(connId));
34,068,466✔
240
  if (pConn == NULL) {
34,072,719✔
241
    mDebug("conn:%u, already destroyed", connId);
643,573✔
242
    return NULL;
643,573✔
243
  }
244

245
  pConn->lastAccessTimeMs = taosGetTimestampMs();
33,427,836✔
246
  mTrace("conn:%u, acquired from cache, data:%p", pConn->id, pConn);
33,424,268✔
247
  return pConn;
33,414,521✔
248
}
249

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

254
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
41,125,562✔
255
  if (extendLifespan) taosCacheTryExtendLifeSpan(pMgmt->connCache, (void **)&pConn);
41,125,481✔
256
  taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
41,125,753✔
257
}
258

259
void *mndGetNextConn(SMnode *pMnode, SCacheIter *pIter) {
4,618,230✔
260
  SConnObj *pConn = NULL;
4,618,230✔
261
  bool      hasNext = taosCacheIterNext(pIter);
4,618,230✔
262
  if (hasNext) {
4,617,828✔
263
    size_t dataLen = 0;
4,543,535✔
264
    pConn = taosCacheIterGetData(pIter, &dataLen);
4,543,535✔
265
  } else {
266
    taosCacheDestroyIter(pIter);
74,293✔
267
  }
268

269
  return pConn;
4,618,230✔
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) {
54,667✔
282
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
54,667✔
283
  SCacheIter   *pIter = taosCacheCreateIter(pMgmt->connCache);
54,667✔
284
  if (pIter == NULL) {
54,667✔
285
    mError("failed to create conn cache iterator");
×
286
    return -1;
×
287
  }
288

289
  int32_t    count = 0;
54,667✔
290
  SConnObj  *pConn = NULL;
54,667✔
291
  while ((pConn = mndGetNextConn(pMnode, pIter)) != NULL) {
4,298,993✔
292
    if (strncmp(pConn->user, user, TSDB_USER_LEN) == 0) {
4,244,326✔
293
      count++;
951,040✔
294
    }
295
    mndReleaseConn(pMnode, pConn, true);
4,244,326✔
296
  }
297

298
  return count;
54,667✔
299
}
300

301

302

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

306
  const char* currPass = pUser->passwords[0].pass;
2,814,839✔
307
  char pass[TSDB_PASSWORD_LEN] = {0};
2,813,841✔
308
  (void)memcpy(pass, inputPass, TSDB_PASSWORD_LEN);
2,814,451✔
309
  pass[TSDB_PASSWORD_LEN - 1] = 0;
2,813,877✔
310

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

321
  // constant time comparison to prevent timing attack
322
  volatile uint8_t res = 0;
2,813,998✔
323
  for (size_t i = 0; i < sizeof(pass) - 1; i++) {
90,035,005✔
324
    res |= pass[i] ^ currPass[i];
87,220,392✔
325
  }
326

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

330

331

332
static bool verifyTotp(SUserObj *pUser, int32_t totpCode) {
2,809,198✔
333
  if (!mndIsTotpEnabledUser(pUser)) {
2,809,198✔
334
    return true;
2,804,545✔
335
  }
336
  return taosVerifyTotpCode(pUser->totpsecret, sizeof(pUser->totpsecret), totpCode, 6, 1) != 0;
4,300✔
337
}
338

339

340

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

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

358
  if (token != NULL && mndGetCachedTokenInfo(token, &ti) == NULL) {
2,820,943✔
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,820,943✔
362
  TAOS_CHECK_GOTO(taosCheckVersionCompatibleFromStr(connReq.sVer, td_version, 3), &lino, _OVER);
2,820,862✔
363
  TAOS_CHECK_GOTO(tVerifyConnectReqSignature(&connReq), &lino, _OVER);
2,820,942✔
364
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, user, &pUser), &lino, _OVER);
2,820,943✔
365

366
  SLoginInfo li = {0};
2,819,776✔
367
  mndGetUserLoginInfo(user, &li);
2,819,776✔
368
  TAOS_CHECK_GOTO(mndCheckConnectPrivilege(pMnode, pUser, token, &li), &lino, _OVER);
2,819,651✔
369

370
  if (token != NULL || tsMndSkipGrant) {
2,815,530✔
371
    li.lastLoginTime= now;
1,310✔
372
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
1,310✔
373
      mndSetUserLoginInfo(user, &li);
1,515✔
374
    }
375
  } else if ((code = verifyPassword(pUser, connReq.passwd)) == TSDB_CODE_MND_AUTH_FAILURE) {
2,814,453✔
376
    if (pUser->failedLoginAttempts >= 0) {
5,457✔
377
      if (li.failedLoginCount >= pUser->failedLoginAttempts) {
4,640✔
378
        // if we can get here, it means the lock time has passed, so reset the counter
379
        li.failedLoginCount = 0;
×
380
      }
381
      li.failedLoginCount++;
4,640✔
382
      li.lastFailedLoginTime = now;
4,640✔
383
    }
384
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
5,641✔
385
      mndSetUserLoginInfo(user, &li);
5,641✔
386
    }
387
    TAOS_CHECK_GOTO(code, &lino, _OVER);
5,641✔
388
  } else if (code != TSDB_CODE_SUCCESS) {
2,809,198✔
389
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
390
  } else if (!verifyTotp(pUser, connReq.totpCode)) {
2,809,198✔
391
    TAOS_CHECK_GOTO(TSDB_CODE_MND_WRONG_TOTP_CODE, &lino, _OVER);
2,906✔
392
  } else {
393
    li.failedLoginCount = 0;
2,805,939✔
394
    li.lastLoginTime= now;
2,805,939✔
395
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
2,805,939✔
396
      mndSetUserLoginInfo(user, &li);
2,805,939✔
397
    }
398
  } 
399

400
  if (connReq.db[0] != 0) {
2,807,847✔
401
    char db[TSDB_DB_FNAME_LEN] = {0};
520,315✔
402
    (void)snprintf(db, TSDB_DB_FNAME_LEN, "%d%s%s", pUser->acctId, TS_PATH_DELIMITER, connReq.db);
520,315✔
403
    pDb = mndAcquireDb(pMnode, db);
520,315✔
404
    if (pDb == NULL) {
520,008✔
405
      if (0 != strcmp(connReq.db, TSDB_INFORMATION_SCHEMA_DB) && (0 != strcmp(connReq.db, TSDB_PERFORMANCE_SCHEMA_DB))) {
1,108✔
406
        TAOS_CHECK_GOTO(TSDB_CODE_MND_DB_NOT_EXIST, &lino, _OVER);
372✔
407
      }
408
    }
409

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

413
  if (connReq.connType == CONN_TYPE__AUTH_TEST) {
2,807,475✔
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,
2,807,475✔
419
                        connReq.startTime, connReq.sVer);
420
  if (pConn == NULL) {
2,807,094✔
421
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
422
  }
423

424
  SConnectRsp connectRsp = {0};
2,807,094✔
425
  connectRsp.acctId = pUser->acctId;
2,807,247✔
426
  connectRsp.superUser = pUser->superUser;
2,805,986✔
427
  connectRsp.sysInfo = pUser->sysInfo;
2,806,332✔
428
  connectRsp.clusterId = pMnode->clusterId;
2,806,097✔
429
  connectRsp.connId = pConn->id;
2,806,673✔
430
  connectRsp.connType = connReq.connType;
2,807,209✔
431
  connectRsp.dnodeNum = mndGetDnodeSize(pMnode);
2,807,209✔
432
  connectRsp.svrTimestamp = taosGetTimestampSec();
2,807,475✔
433
  connectRsp.passVer = pUser->passVersion;
2,807,134✔
434
  connectRsp.authVer = pUser->authVersion;
2,807,134✔
435
  connectRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
2,806,901✔
436
  connectRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
2,806,901✔
437
  connectRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
2,806,901✔
438
  connectRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
2,806,901✔
439
  connectRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
2,806,901✔
440
  connectRsp.enableAuditDelete = tsEnableAuditDelete;
2,806,901✔
441
  connectRsp.enableAuditSelect = tsEnableAuditSelect;
2,806,901✔
442
  connectRsp.enableAuditInsert = tsEnableAuditInsert;
2,806,901✔
443
  connectRsp.auditLevel = tsAuditLevel;
2,806,901✔
444
  tstrncpy(connectRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
2,806,901✔
445
  connectRsp.whiteListVer = pUser->ipWhiteListVer;
2,806,861✔
446
  connectRsp.timeWhiteListVer = pUser->timeWhiteListVer;
2,806,861✔
447
  connectRsp.userId = pUser->uid;
2,807,093✔
448

449

450
  tstrncpy(connectRsp.sVer, td_version, sizeof(connectRsp.sVer));
2,805,751✔
451
  tstrncpy(connectRsp.user, user, sizeof(connectRsp.user));
2,806,326✔
452
  tstrncpy(connectRsp.tokenName, ti.name, sizeof(connectRsp.tokenName));
2,806,521✔
453
  (void)snprintf(connectRsp.sDetailVer, sizeof(connectRsp.sDetailVer), "ver:%s\nbuild:%s\ngitinfo:%s", td_version,
2,806,820✔
454
                 td_buildinfo, td_gitinfo);
455
  mndGetMnodeEpSet(pMnode, &connectRsp.epSet);
2,806,820✔
456

457
  int32_t contLen = tSerializeSConnectRsp(NULL, 0, &connectRsp);
2,807,475✔
458
  if (contLen < 0) {
2,806,361✔
459
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
460
  }
461
  void *pRsp = rpcMallocCont(contLen);
2,806,361✔
462
  if (pRsp == NULL) {
2,806,707✔
463
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
464
  }
465

466
  contLen = tSerializeSConnectRsp(pRsp, contLen, &connectRsp);
2,806,707✔
467
  if (contLen < 0) {
2,806,388✔
468
    rpcFreeCont(pRsp);
×
469
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
470
  }
471

472
  pReq->info.rspLen = contLen;
2,806,388✔
473
  pReq->info.rsp = pRsp;
2,806,971✔
474

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

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

489
_OVER:
2,818,608✔
490
  if (code != 0) {
2,820,981✔
491
    mGError("user:%s, failed to login from %s since %s, line:%d, db:%s", user, ip, tstrerror(code), lino, connReq.db);
13,506✔
492
  }
493

494
  mndReleaseUser(pMnode, pUser);
2,820,981✔
495
  mndReleaseDb(pMnode, pDb);
2,820,981✔
496
  mndReleaseConn(pMnode, pConn, true);
2,820,981✔
497

498
  TAOS_RETURN(code);
2,820,981✔
499
}
500

501

502

503
static int32_t mndSaveQueryList(SConnObj *pConn, SQueryHbReqBasic *pBasic) {
34,069,236✔
504
  taosWLockLatch(&pConn->queryLock);
34,069,236✔
505

506
  taosArrayDestroyEx(pConn->pQueries, tFreeClientHbQueryDesc);
34,072,695✔
507

508
  pConn->pQueries = pBasic->queryDesc;
34,065,707✔
509
  pConn->numOfQueries = pBasic->queryDesc ? taosArrayGetSize(pBasic->queryDesc) : 0;
34,068,877✔
510
  pBasic->queryDesc = NULL;
34,068,537✔
511

512
  mDebug("queries updated in conn %u, num:%d", pConn->id, pConn->numOfQueries);
34,069,405✔
513

514
  taosWUnLockLatch(&pConn->queryLock);
34,074,277✔
515

516
  return TSDB_CODE_SUCCESS;
34,072,362✔
517
}
518

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

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

531
  SAppObj *pApp =
532
      taosCachePut(pMgmt->appCache, &pReq->appId, sizeof(pReq->appId), &app, sizeof(app), CACHE_OBJ_KEEP_TIME * 1000);
812,933✔
533
  if (pApp == NULL) {
812,933✔
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);
812,933✔
540
  return pApp;
812,933✔
541
}
542

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

545
static SAppObj *mndAcquireApp(SMnode *pMnode, int64_t appId) {
34,070,020✔
546
  terrno = 0;
34,070,020✔
547
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
34,071,256✔
548

549
  SAppObj *pApp = taosCacheAcquireByKey(pMgmt->appCache, &appId, sizeof(appId));
34,069,331✔
550
  if (pApp == NULL) {
34,064,980✔
551
    mDebug("app %" PRIx64 " not in cache", appId);
812,933✔
552
    return NULL;
812,933✔
553
  }
554

555
  pApp->lastAccessTimeMs = (uint64_t)taosGetTimestampMs();
33,253,704✔
556

557
  mTrace("app %" PRIx64 " acquired from cache", appId);
33,251,000✔
558
  return pApp;
33,246,207✔
559
}
560

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

565
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
34,062,913✔
566
  taosCacheRelease(pMgmt->appCache, (void **)&pApp, false);
34,067,363✔
567
}
568

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

579
  return pApp;
6,578✔
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) {
34,066,041✔
594
  int32_t    code = 0;
34,066,041✔
595
  SAppHbReq *pReq = &pHbReq->app;
34,066,041✔
596
  SAppObj   *pApp = mndAcquireApp(pMnode, pReq->appId);
34,063,301✔
597
  if (pApp == NULL) {
34,056,000✔
598
    pApp = mndCreateApp(pMnode, &connInfo->cliAddr, pReq);
812,933✔
599
    if (pApp == NULL) {
812,933✔
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);
812,933✔
606
      mndReleaseApp(pMnode, pApp);
812,933✔
607
      return TSDB_CODE_SUCCESS;
812,479✔
608
    }
609
  }
610

611
  (void)memcpy(&pApp->summary, &pReq->summary, sizeof(pReq->summary));
33,243,067✔
612

613
  mndReleaseApp(pMnode, pApp);
33,250,372✔
614

615
  return TSDB_CODE_SUCCESS;
33,245,082✔
616
}
617

618
static int32_t mndGetOnlineDnodeNum(SMnode *pMnode, int32_t *num) {
28,634,687✔
619
  SSdb      *pSdb = pMnode->pSdb;
28,634,687✔
620
  SDnodeObj *pDnode = NULL;
28,634,480✔
621
  int64_t    curMs = taosGetTimestampMs();
28,631,923✔
622
  void      *pIter = NULL;
28,631,923✔
623

624
  while (true) {
45,163,959✔
625
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
73,795,882✔
626
    if (pIter == NULL) break;
73,799,362✔
627

628
    bool online = mndIsDnodeOnline(pDnode, curMs);
45,162,997✔
629
    if (online) {
45,157,051✔
630
      (*num)++;
43,856,599✔
631
    }
632

633
    sdbRelease(pSdb, pDnode);
45,156,905✔
634
  }
635

636
  return TSDB_CODE_SUCCESS;
28,636,365✔
637
}
638

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

645
  if (0 != pHbReq->app.appId) {
34,067,442✔
646
    TAOS_CHECK_RETURN(mndUpdateAppInfo(pMnode, pHbReq, &pMsg->info.conn));
34,065,009✔
647
  }
648

649
  if (pHbReq->query) {
34,051,673✔
650
    SQueryHbReqBasic *pBasic = pHbReq->query;
34,065,382✔
651
    SConnObj *pConn = mndAcquireConn(pMnode, pBasic->connId);
34,067,809✔
652
    if (pConn == NULL) {
34,060,394✔
653
      SRpcConnInfo  connInfo = pMsg->info.conn;
643,524✔
654
      const char* user = pHbReq->user;
643,524✔
655
      pConn = mndCreateConn(pMnode, user, pHbReq->tokenName, pHbReq->connKey.connType, &connInfo.cliAddr, pHbReq->app.pid,
643,524✔
656
                            pHbReq->app.name, 0, pHbReq->sVer);
643,524✔
657
      if (pConn == NULL) {
642,604✔
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);
642,604✔
664
      }
665
    }
666

667
    setUserInfo2Conn(pConn, pHbReq->userApp, pHbReq->userIp, pHbReq->cInfo);
34,060,443✔
668
    setUserInfoIpToConn(pConn, &pHbReq->userDualIp);
34,055,220✔
669

670
    SQueryHbRspBasic *rspBasic = taosMemoryCalloc(1, sizeof(SQueryHbRspBasic));
34,062,386✔
671
    if (rspBasic == NULL) {
34,062,108✔
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));
34,062,108✔
679
    if (pConn->killed != 0) {
34,072,362✔
680
      rspBasic->killConnection = 1;
×
681
    }
682

683
    if (pConn->killId != 0) {
34,072,362✔
684
      rspBasic->killRid = pConn->killId;
2✔
685
      pConn->killId = 0;
2✔
686
    }
687

688
    rspBasic->connId = pConn->id;
34,073,252✔
689
    rspBasic->connId = pConn->id;
34,071,967✔
690
    rspBasic->totalDnodes = pObj->totalDnodes;
34,072,857✔
691
    rspBasic->onlineDnodes = pObj->onlineDnodes;
34,072,971✔
692
    rspBasic->epSet = pObj->epSet;
34,072,348✔
693
    rspBasic->pQnodeList = taosArrayDup(pObj->pQnodeList, NULL);
34,072,313✔
694

695
    mndReleaseConn(pMnode, pConn, true);
34,073,380✔
696

697
    hbRsp.query = rspBasic;
34,073,652✔
698
  } else {
699
    mDebug("no query info in hb msg");
194✔
700
  }
701

702
  int32_t kvNum = taosHashGetSize(pHbReq->info);
34,073,846✔
703
  if (NULL == pHbReq->info || kvNum <= 0) {
34,072,532✔
704
    if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
15,634,035✔
705
      mError("failed to put rsp into array, but continue at this heartbeat");
×
706
    }
707
    return TSDB_CODE_SUCCESS;
7,817,729✔
708
  }
709

710
  hbRsp.info = taosArrayInit(kvNum, sizeof(SKv));
26,256,226✔
711
  if (NULL == hbRsp.info) {
26,256,226✔
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;
26,256,226✔
720
  int32_t          key = HEARTBEAT_KEY_DYN_VIEW;
26,256,226✔
721
  SDynViewVersion *pDynViewVer = NULL;
26,256,226✔
722
  SKv             *pKv = taosHashGet(pHbReq->info, &key, sizeof(key));
26,256,226✔
723
  if (NULL != pKv) {
26,256,226✔
724
    pDynViewVer = pKv->value;
7,308✔
725
    mTrace("recv view dyn ver, bootTs:%" PRId64 ", ver:%" PRIu64, pDynViewVer->svrBootTs, pDynViewVer->dynViewVer);
7,308✔
726

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

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

744
  void *pIter = taosHashIterate(pHbReq->info, NULL);
26,256,226✔
745
  while (pIter != NULL) {
68,499,382✔
746
    SKv *kv = pIter;
42,243,273✔
747

748
    switch (kv->key) {
42,243,273✔
749
      case HEARTBEAT_KEY_USER_AUTHINFO: {
26,256,032✔
750
        void   *rspMsg = NULL;
26,256,032✔
751
        int32_t rspLen = 0;
26,256,032✔
752
        (void)mndValidateUserAuthInfo(pMnode, kv->value, kv->valueLen / sizeof(SUserAuthVersion), &rspMsg, &rspLen,
26,256,032✔
753
                                      pObj->ipWhiteListVer);
754
        if (rspMsg && rspLen > 0) {
26,255,216✔
755
          SKv kv1 = {.key = HEARTBEAT_KEY_USER_AUTHINFO, .valueLen = rspLen, .value = rspMsg};
26,254,480✔
756
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
52,508,593✔
757
            mError("failed to put kv into array, but continue at this heartbeat");
×
758
          }
759
        }
760
        break;
26,252,905✔
761
      }
762
      case HEARTBEAT_KEY_DBINFO: {
9,270,424✔
763
        void   *rspMsg = NULL;
9,270,424✔
764
        int32_t rspLen = 0;
9,270,424✔
765
        (void)mndValidateDbInfo(pMnode, kv->value, kv->valueLen / sizeof(SDbCacheInfo), &rspMsg, &rspLen);
9,270,424✔
766
        if (rspMsg && rspLen > 0) {
9,270,424✔
767
          SKv kv1 = {.key = HEARTBEAT_KEY_DBINFO, .valueLen = rspLen, .value = rspMsg};
9,269,806✔
768
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
18,540,230✔
769
            mError("failed to put kv into array, but continue at this heartbeat");
×
770
          }
771
        }
772
        break;
9,270,424✔
773
      }
774
      case HEARTBEAT_KEY_STBINFO: {
6,692,830✔
775
        void   *rspMsg = NULL;
6,692,830✔
776
        int32_t rspLen = 0;
6,692,830✔
777
        (void)mndValidateStbInfo(pMnode, kv->value, kv->valueLen / sizeof(SSTableVersion), &rspMsg, &rspLen);
6,692,830✔
778
        if (rspMsg && rspLen > 0) {
6,692,830✔
779
          SKv kv1 = {.key = HEARTBEAT_KEY_STBINFO, .valueLen = rspLen, .value = rspMsg};
6,692,830✔
780
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
13,385,660✔
781
            mError("failed to put kv into array, but continue at this heartbeat");
×
782
          }
783
        }
784
        break;
6,692,830✔
785
      }
786
#ifdef TD_ENTERPRISE
787
      case HEARTBEAT_KEY_DYN_VIEW: {
7,308✔
788
        break;
7,308✔
789
      }
790
      case HEARTBEAT_KEY_VIEWINFO: {
7,308✔
791
        if (!needCheck) {
7,308✔
792
          break;
386✔
793
        }
794

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

825
    pIter = taosHashIterate(pHbReq->info, pIter);
42,240,443✔
826
  }
827

828
  if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
52,511,719✔
829
    if (terrno != 0) code = terrno;
×
830
  }
831
  TAOS_RETURN(code);
26,255,610✔
832
}
833

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

839
  SClientHbBatchReq batchReq = {0};
28,635,657✔
840
  if (tDeserializeSClientHbBatchReq(pReq->pCont, pReq->contLen, &batchReq) != 0) {
28,635,269✔
841
    taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
×
842
    code = TSDB_CODE_INVALID_MSG;
×
843
    TAOS_RETURN(code);
×
844
  }
845

846
  SConnPreparedObj obj = {0};
28,633,471✔
847
  obj.totalDnodes = mndGetDnodeSize(pMnode);
28,633,471✔
848
  obj.ipWhiteListVer = batchReq.ipWhiteListVer;
28,635,889✔
849
  TAOS_CHECK_RETURN(mndGetOnlineDnodeNum(pMnode, &obj.onlineDnodes));
28,635,889✔
850
  mndGetMnodeEpSet(pMnode, &obj.epSet);
28,634,729✔
851
  TAOS_CHECK_RETURN(mndCreateQnodeList(pMnode, &obj.pQnodeList, -1));
28,636,365✔
852

853
  SClientHbBatchRsp batchRsp = {0};
28,632,595✔
854
  batchRsp.svrTimestamp = taosGetTimestampSec();
28,633,129✔
855
  batchRsp.rsps = taosArrayInit(0, sizeof(SClientHbRsp));
28,631,121✔
856
  if (batchRsp.rsps == NULL) {
28,632,473✔
857
    TAOS_CHECK_EXIT(terrno);
×
858
  }
859
  batchRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
28,632,473✔
860
  batchRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
28,632,473✔
861
  batchRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
28,632,473✔
862
  tstrncpy(batchRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
28,632,473✔
863
  batchRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
28,629,266✔
864
  batchRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
28,629,266✔
865
  batchRsp.enableAuditDelete = tsEnableAuditDelete;
28,629,266✔
866
  batchRsp.enableAuditSelect = tsEnableAuditSelect;
28,629,266✔
867
  batchRsp.enableAuditInsert = tsEnableAuditInsert;
28,629,266✔
868
  batchRsp.auditLevel = tsAuditLevel;
28,629,266✔
869
  batchRsp.enableStrongPass = tsEnableStrongPassword;
28,629,266✔
870

871
  int32_t sz = taosArrayGetSize(batchReq.reqs);
28,629,266✔
872
  for (int i = 0; i < sz; i++) {
62,704,950✔
873
    SClientHbReq *pHbReq = taosArrayGet(batchReq.reqs, i);
34,069,765✔
874
    if (pHbReq->connKey.connType == CONN_TYPE__QUERY || pHbReq->connKey.connType == CONN_TYPE__TMQ) {
34,069,734✔
875
      TAOS_CHECK_EXIT(mndProcessQueryHeartBeat(pMnode, pReq, pHbReq, &batchRsp, &obj));
34,069,962✔
876
    } 
877
  }
878
  taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
28,635,185✔
879

880
  int32_t tlen = tSerializeSClientHbBatchRsp(NULL, 0, &batchRsp);
28,633,756✔
881
  if (tlen < 0) {
28,625,241✔
882
    TAOS_CHECK_EXIT(tlen);
×
883
  }
884
  void *buf = rpcMallocCont(tlen);
28,625,241✔
885
  if (!buf) {
28,633,515✔
886
    TAOS_CHECK_EXIT(terrno);
×
887
  }
888
  tlen = tSerializeSClientHbBatchRsp(buf, tlen, &batchRsp);
28,633,515✔
889
  if (tlen < 0) {
28,633,829✔
890
    rpcFreeCont(buf);
×
891
    TAOS_CHECK_EXIT(tlen);
×
892
  }
893
  pReq->info.rspLen = tlen;
28,633,829✔
894
  pReq->info.rsp = buf;
28,632,130✔
895
_exit:
28,632,096✔
896
  tFreeClientHbBatchRsp(&batchRsp);
897

898
  taosArrayDestroy(obj.pQnodeList);
28,631,716✔
899

900
  TAOS_RETURN(code);
28,629,600✔
901
}
902

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

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

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

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

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

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

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

948
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &killReq.connId, sizeof(uint32_t));
344✔
949
  if (pConn == NULL) {
344✔
950
    mError("connId:%u, failed to kill connection, conn not exist", killReq.connId);
344✔
951
    code = TSDB_CODE_MND_INVALID_CONN_ID;
344✔
952
    TAOS_RETURN(code);
344✔
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) {
14,717✔
990
  SMnode   *pMnode = pReq->info.node;
14,717✔
991
  SSdb     *pSdb = pMnode->pSdb;
14,717✔
992
  int32_t   numOfRows = 0;
14,717✔
993
  int32_t   cols = 0;
14,717✔
994
  int32_t   code = 0;
14,717✔
995
  SConnObj *pConn = NULL;
14,717✔
996

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

1003
  while (numOfRows < rows) {
295,268✔
1004
    pConn = mndGetNextConn(pMnode, pShow->pIter);
295,268✔
1005
    if (pConn == NULL) {
295,268✔
1006
      pShow->pIter = NULL;
14,717✔
1007
      break;
14,717✔
1008
    }
1009

1010
    if ((taosGetTimestampMs() - pConn->lastAccessTimeMs) > ((int64_t)CACHE_OBJ_KEEP_TIME * 1000)) {
280,152✔
1011
      continue;
241,908✔
1012
    }
1013

1014
    cols = 0;
38,643✔
1015

1016
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
38,643✔
1017
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->id, false);
38,643✔
1018
    if (code != 0) {
38,643✔
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};
38,643✔
1024
    STR_TO_VARSTR(user, pConn->user);
38,643✔
1025
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
38,643✔
1026
    code = colDataSetVal(pColInfo, numOfRows, (const char *)user, false);
38,643✔
1027
    if (code != 0) {
38,643✔
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];
38,255✔
1033
    STR_TO_VARSTR(app, pConn->app);
38,643✔
1034
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
38,643✔
1035
    code = colDataSetVal(pColInfo, numOfRows, (const char *)app, false);
38,643✔
1036
    if (code != 0) {
38,643✔
1037
      mError("failed to set app since %s", tstrerror(code));
×
1038
      return code;
×
1039
    }
1040

1041
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
38,643✔
1042
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->pid, false);
38,643✔
1043
    if (code != 0) {
38,643✔
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};
38,643✔
1049
    char endpoint[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
38,643✔
1050
    if (snprintf(addr, sizeof(addr), "%s:%d", IP_ADDR_STR(&pConn->addr), pConn->addr.port) >= sizeof(addr)) {
38,643✔
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);
38,643✔
1057

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

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

1072
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
38,643✔
1073
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->lastAccessTimeMs, false);
38,643✔
1074
    if (code != 0) {
38,643✔
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];
38,255✔
1080
    STR_TO_VARSTR(userApp, pConn->userApp);
38,643✔
1081
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
38,643✔
1082
    code = colDataSetVal(pColInfo, numOfRows, (const char *)userApp, false);
38,643✔
1083
    if (code != 0) {
38,643✔
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};
38,643✔
1089
    getUserIpFromConnObj(pConn, userIp);
38,643✔
1090

1091
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
38,643✔
1092
    code = colDataSetVal(pColInfo, numOfRows, (const char *)userIp, false);
38,643✔
1093
    if (code != 0) {
38,643✔
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];
38,255✔
1099
    STR_TO_VARSTR(ver, pConn->sVer);
38,643✔
1100
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
38,643✔
1101
    code = colDataSetVal(pColInfo, numOfRows, (const char *)ver, false);
38,643✔
1102
    if (code != 0) {
38,643✔
1103
      mError("failed to set ver since %s", tstrerror(code));
×
1104
      return code;
×
1105
    }
1106

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

1116
    char type[16 + VARSTR_HEADER_SIZE];
38,255✔
1117
    STR_TO_VARSTR(type, pConn->connType == CONN_TYPE__QUERY ? "QUERY" : (pConn->connType == CONN_TYPE__TMQ ? "TMQ" : "UNKNOWN"));
38,643✔
1118
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
38,643✔
1119
    code = colDataSetVal(pColInfo, numOfRows, (const char *)type, false);
38,643✔
1120
    if (code != 0) {
38,643✔
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};
38,643✔
1126
    STR_TO_VARSTR(tokenName, pConn->tokenName);
38,643✔
1127
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
38,643✔
1128
    code = colDataSetVal(pColInfo, numOfRows, (const char *)tokenName, false);
38,643✔
1129
    if (code != 0) {
38,643✔
1130
      mError("failed to set token name since %s", tstrerror(code));
×
1131
      return code;
×
1132
    }
1133

1134
    numOfRows++;
38,643✔
1135
  }
1136

1137
  pShow->numOfRows += numOfRows;
14,717✔
1138
  return numOfRows;
14,717✔
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,
19,060✔
1149
                                    uint32_t rowsToPack) {
1150
  int32_t cols = 0;
19,060✔
1151
  int32_t code = 0;
19,060✔
1152
  taosRLockLatch(&pConn->queryLock);
19,060✔
1153
  int32_t numOfQueries = taosArrayGetSize(pConn->pQueries);
19,060✔
1154
  if (NULL == pConn->pQueries || numOfQueries <= offset) {
19,060✔
1155
    taosRUnLockLatch(&pConn->queryLock);
16,363✔
1156
    return 0;
16,363✔
1157
  }
1158

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

1165
    char queryId[26 + VARSTR_HEADER_SIZE] = {0};
2,697✔
1166
    (void)snprintf(&queryId[VARSTR_HEADER_SIZE], sizeof(queryId) - VARSTR_HEADER_SIZE, "%x:%" PRIx64, pConn->id,
2,697✔
1167
                   pQuery->reqRid);
1168
    varDataLen(queryId) = strlen(&queryId[VARSTR_HEADER_SIZE]);
2,697✔
1169
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,697✔
1170
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)queryId, false);
2,697✔
1171
    if (code != 0) {
2,697✔
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,697✔
1178
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->queryId, false);
2,697✔
1179
    if (code != 0) {
2,697✔
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,697✔
1186
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pConn->id, false);
2,697✔
1187
    if (code != 0) {
2,697✔
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,697✔
1194
    STR_TO_VARSTR(app, pConn->app);
2,697✔
1195
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,697✔
1196
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)app, false);
2,697✔
1197
    if (code != 0) {
2,697✔
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,697✔
1204
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pConn->pid, false);
2,697✔
1205
    if (code != 0) {
2,697✔
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,697✔
1212
    STR_TO_VARSTR(user, pConn->user);
2,697✔
1213
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,697✔
1214
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)user, false);
2,697✔
1215
    if (code != 0) {
2,697✔
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,697✔
1222
    char buf[IP_RESERVE_CAP] = {0};
2,697✔
1223
    (void)snprintf(buf, sizeof(buf), "%s:%d", IP_ADDR_STR(&pConn->addr), pConn->addr.port);
2,697✔
1224
    STR_TO_VARSTR(endpoint, buf);
2,697✔
1225
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,697✔
1226
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)endpoint, false);
2,697✔
1227
    if (code != 0) {
2,697✔
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,697✔
1234
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->stime, false);
2,697✔
1235
    if (code != 0) {
2,697✔
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,697✔
1242
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->useconds, false);
2,697✔
1243
    if (code != 0) {
2,697✔
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,697✔
1250
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->stableQuery, false);
2,697✔
1251
    if (code != 0) {
2,697✔
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,697✔
1258
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->isSubQuery, false);
2,697✔
1259
    if (code != 0) {
2,697✔
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,697✔
1266
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->subPlanNum, false);
2,697✔
1267
    if (code != 0) {
2,697✔
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,697✔
1274
    int64_t reserve = 128;
2,697✔
1275
    int32_t strSize = sizeof(subStatus);
2,697✔
1276
    int32_t offset = VARSTR_HEADER_SIZE;
2,697✔
1277
    for (int32_t i = 0; i < pQuery->subPlanNum && offset + reserve < strSize; ++i) {
8,152✔
1278
      if (i) {
5,455✔
1279
        offset += snprintf(subStatus + offset, sizeof(subStatus) - offset, ",");
2,760✔
1280
      }
1281
      if (offset + reserve >= strSize) break;
5,455✔
1282

1283
      SQuerySubDesc *pDesc = taosArrayGet(pQuery->subDesc, i);
5,455✔
1284
      if (NULL == pDesc) break;
5,455✔
1285

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

1303
      offset += tsnprintf(subStatus + offset, sizeof(subStatus) - offset,
5,455✔
1304
                          "%" PRIu64 ":%s:%s", pDesc->tid, pDesc->status, startBuf);
5,455✔
1305
    }
1306
    varDataLen(subStatus) = strlen(&subStatus[VARSTR_HEADER_SIZE]);
2,697✔
1307
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,697✔
1308
    code = colDataSetVal(pColInfo, curRowIndex, subStatus, (varDataLen(subStatus) == 0) ? true : false);
2,697✔
1309
    if (code != 0) {
2,697✔
1310
      mError("failed to set sub status since %s", tstrerror(code));
×
1311
      taosRUnLockLatch(&pConn->queryLock);
×
1312
      return code;
×
1313
    }
1314

1315
    char sql[TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE] = {0};
2,697✔
1316
    STR_TO_VARSTR(sql, pQuery->sql);
2,697✔
1317
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,697✔
1318
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)sql, false);
2,697✔
1319
    if (code != 0) {
2,697✔
1320
      mError("failed to set sql since %s", tstrerror(code));
×
1321
      taosRUnLockLatch(&pConn->queryLock);
×
1322
      return code;
×
1323
    }
1324

1325
    char userApp[TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE];
2,697✔
1326
    STR_TO_VARSTR(userApp, pConn->userApp);
2,697✔
1327
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,697✔
1328
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)userApp, false);
2,697✔
1329
    if (code != 0) {
2,697✔
1330
      mError("failed to set user app since %s", tstrerror(code));
×
1331
      taosRUnLockLatch(&pConn->queryLock);
×
1332
      return code;
×
1333
    }
1334

1335
    char userIp[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
2,697✔
1336
    getUserIpFromConnObj(pConn, userIp);
2,697✔
1337

1338
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,697✔
1339
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)userIp, false);
2,697✔
1340
    if (code != 0) {
2,697✔
1341
      mError("failed to set user ip since %s", tstrerror(code));
×
1342
      taosRUnLockLatch(&pConn->queryLock);
×
1343
      return code;
×
1344
    }
1345

1346
    const char* phaseStr = queryPhaseStr(pQuery->execPhase);
2,697✔
1347
    char        phaseVarStr[64 + VARSTR_HEADER_SIZE];
2,697✔
1348
    STR_TO_VARSTR(phaseVarStr, phaseStr);
2,697✔
1349
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,697✔
1350
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)phaseVarStr, false);
2,697✔
1351
    if (code != 0) {
2,697✔
1352
      mError("failed to set current phase since %s", tstrerror(code));
×
1353
      taosRUnLockLatch(&pConn->queryLock);
×
1354
      return code;
×
1355
    }
1356

1357
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2,697✔
1358
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->phaseStartTime, false);
2,697✔
1359
    if (code != 0) {
2,697✔
1360
      mError("failed to set phase start time since %s", tstrerror(code));
×
1361
      taosRUnLockLatch(&pConn->queryLock);
×
1362
      return code;
×
1363
    }
1364

1365
    pBlock->info.rows++;
2,697✔
1366
  }
1367

1368
  taosRUnLockLatch(&pConn->queryLock);
2,697✔
1369
  return i - offset;
2,697✔
1370
}
1371

1372
static int32_t mndRetrieveQueries(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
4,909✔
1373
  SMnode   *pMnode = pReq->info.node;
4,909✔
1374
  SSdb     *pSdb = pMnode->pSdb;
4,909✔
1375
  int32_t   numOfRows = 0;
4,909✔
1376
  SConnObj *pConn = NULL;
4,909✔
1377

1378
  if (pShow->pIter == NULL) {
4,909✔
1379
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
4,909✔
1380
    pShow->pIter = taosCacheCreateIter(pMgmt->connCache);
4,909✔
1381
    if (!pShow->pIter) return terrno;
4,909✔
1382
  }
1383

1384
  // means fetched some data last time for this conn
1385
  if (pShow->curIterPackedRows > 0) {
4,909✔
1386
    size_t len = 0;
×
1387
    pConn = taosCacheIterGetData(pShow->pIter, &len);
×
1388
    if (pConn && (taosArrayGetSize(pConn->pQueries) > pShow->curIterPackedRows)) {
×
1389
      numOfRows = packQueriesIntoBlock(pShow, pConn, pBlock, pShow->curIterPackedRows, rows);
×
1390
      pShow->curIterPackedRows += numOfRows;
×
1391
    }
1392
  }
1393

1394
  while (numOfRows < rows) {
23,969✔
1395
    pConn = mndGetNextConn(pMnode, pShow->pIter);
23,969✔
1396
    if (pConn == NULL) {
23,969✔
1397
      pShow->pIter = NULL;
4,909✔
1398
      break;
4,909✔
1399
    }
1400

1401
    int32_t packedRows = packQueriesIntoBlock(pShow, pConn, pBlock, 0, rows - numOfRows);
19,060✔
1402
    pShow->curIterPackedRows = packedRows;
19,060✔
1403
    numOfRows += packedRows;
19,060✔
1404
  }
1405
  pShow->numOfRows += numOfRows;
4,909✔
1406
  return numOfRows;
4,909✔
1407
}
1408

1409
static int32_t mndRetrieveApps(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
3,289✔
1410
  SMnode  *pMnode = pReq->info.node;
3,289✔
1411
  SSdb    *pSdb = pMnode->pSdb;
3,289✔
1412
  int32_t  numOfRows = 0;
3,289✔
1413
  int32_t  cols = 0;
3,289✔
1414
  SAppObj *pApp = NULL;
3,289✔
1415
  int32_t  code = 0;
3,289✔
1416

1417
  if (pShow->pIter == NULL) {
3,289✔
1418
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
3,289✔
1419
    pShow->pIter = taosCacheCreateIter(pMgmt->appCache);
3,289✔
1420
    if (!pShow->pIter) return terrno;
3,289✔
1421
  }
1422

1423
  while (numOfRows < rows) {
6,578✔
1424
    pApp = mndGetNextApp(pMnode, pShow->pIter);
6,578✔
1425
    if (pApp == NULL) {
6,578✔
1426
      pShow->pIter = NULL;
3,289✔
1427
      break;
3,289✔
1428
    }
1429

1430
    cols = 0;
3,289✔
1431

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

1439
    char ip[TD_IP_LEN + VARSTR_HEADER_SIZE] = {0};
3,289✔
1440
    char buf[IP_RESERVE_CAP] = {0};
3,289✔
1441
    snprintf(buf, sizeof(buf), "%s", IP_ADDR_STR(&pApp->cliAddr));
3,289✔
1442
    STR_TO_VARSTR(ip, buf);
3,289✔
1443

1444
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,289✔
1445
    code = colDataSetVal(pColInfo, numOfRows, (const char *)ip, false);
3,289✔
1446
    if (code != 0) {
3,289✔
1447
      mError("failed to set ip since %s", tstrerror(code));
×
1448
      return code;
×
1449
    }
1450

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

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

1468
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,289✔
1469
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->startTime, false);
3,289✔
1470
    if (code != 0) {
3,289✔
1471
      mError("failed to set start time since %s", tstrerror(code));
×
1472
      return code;
×
1473
    }
1474

1475
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,289✔
1476
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.numOfInsertsReq, false);
3,289✔
1477
    if (code != 0) {
3,289✔
1478
      mError("failed to set insert req since %s", tstrerror(code));
×
1479
      return code;
×
1480
    }
1481

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

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

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

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

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

1517
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,289✔
1518
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.numOfSlowQueries, false);
3,289✔
1519
    if (code != 0) {
3,289✔
1520
      mError("failed to set slow queries since %s", tstrerror(code));
×
1521
      return code;
×
1522
    }
1523

1524
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,289✔
1525
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.totalRequests, false);
3,289✔
1526
    if (code != 0) {
3,289✔
1527
      mError("failed to set total requests since %s", tstrerror(code));
×
1528
      return code;
×
1529
    }
1530

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

1538
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,289✔
1539
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->lastAccessTimeMs, false);
3,289✔
1540
    if (code != 0) {
3,289✔
1541
      mError("failed to set last access time since %s", tstrerror(code));
×
1542
      return code;
×
1543
    }
1544

1545
    numOfRows++;
3,289✔
1546
  }
1547

1548
  pShow->numOfRows += numOfRows;
3,289✔
1549
  return numOfRows;
3,289✔
1550
}
1551

1552
static void mndCancelGetNextQuery(SMnode *pMnode, void *pIter) {
×
1553
  if (pIter != NULL) {
×
1554
    taosCacheDestroyIter(pIter);
×
1555
  }
1556
}
×
1557

1558
int32_t mndGetNumOfConnections(SMnode *pMnode) {
77✔
1559
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
77✔
1560
  return taosCacheGetNumOfObj(pMgmt->connCache);
77✔
1561
}
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