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

taosdata / TDengine / #5055

17 May 2026 01:15AM UTC coverage: 73.355% (-0.003%) from 73.358%
#5055

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

281532 of 383795 relevant lines covered (73.35%)

135557734.7 hits per line

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

76.95
/source/dnode/mnode/impl/src/mndProfile.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "mndProfile.h"
18
#include "audit.h"
19
#include "crypt.h"
20
#include "mndCluster.h"
21
#include "mndDb.h"
22
#include "mndDnode.h"
23
#include "mndMnode.h"
24
#include "mndPrivilege.h"
25
#include "mndQnode.h"
26
#include "mndSecurityPolicy.h"
27
#include "mndShow.h"
28
#include "mndSma.h"
29
#include "mndStb.h"
30
#include "mndToken.h"
31
#include "mndUser.h"
32
#include "mndView.h"
33
#include "tglobal.h"
34
#include "totp.h"
35
#include "tversion.h"
36

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

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

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

78
#define CACHE_OBJ_KEEP_TIME 3  // s
79

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

97
int32_t mndInitProfile(SMnode *pMnode) {
531,365✔
98
  int32_t       code = 0;
531,365✔
99
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
531,365✔
100

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

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

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

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

130
  TAOS_RETURN(code);
531,365✔
131
}
132

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

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

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

153
  if (pConn->userDualIp.ipv4[0] != 0 && strncmp(pConn->userDualIp.ipv4, none, strlen(none)) != 0) {
60,718✔
154
    char   *ipstr = IP_ADDR_STR(&pConn->userDualIp);
×
155
    int32_t len = strlen(ipstr);
×
156
    memcpy(varDataVal(dst), ipstr, len);
×
157
    varDataLen(dst) = len;
×
158
  }
159
  return;
60,718✔
160
}
161
static void setUserInfo2Conn(SConnObj *connObj, char *userApp, uint32_t userIp, char *cInfo) {
39,659,178✔
162
  if (connObj == NULL) {
39,659,178✔
163
    return;
×
164
  }
165
  tstrncpy(connObj->userApp, userApp, sizeof(connObj->userApp));
39,659,178✔
166
  tstrncpy(connObj->cInfo, cInfo, sizeof(connObj->cInfo));
39,653,819✔
167
  connObj->userIp = userIp;
39,645,886✔
168
}
169
static void setUserInfoIpToConn(SConnObj *connObj, SIpRange *pRange) {
39,658,141✔
170
  int32_t code = 0;
39,658,141✔
171
  if (connObj == NULL) {
39,658,141✔
172
    return;
×
173
  }
174

175
  code = tIpUintToStr(pRange, &connObj->userDualIp);
39,658,141✔
176
  if (code != 0) {
39,645,739✔
177
    mError("conn:%u, failed to set user ip to conn since %s", connObj->id, tstrerror(code));
×
178
    return;
×
179
  }
180
}
181

182

183

184
static SConnObj *mndCreateConn(SMnode *pMnode, const char *user, const char* tokenName, int8_t connType, SIpAddr *pAddr,
3,877,391✔
185
                               int32_t pid, const char *app, int64_t startTime, const char *sVer) {
186
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
3,877,391✔
187

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

192
  int32_t  len = tsnprintf(connStr, sizeof(connStr), "%s%d%d%d%s", user, ip, port, pid, app);
3,877,319✔
193
  uint32_t connId = mndGenerateUid(connStr, len);
3,877,251✔
194
  if (startTime == 0) startTime = taosGetTimestampMs();
4,609,862✔
195

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

210
  connObj.lastAccessTimeMs = connObj.loginTimeMs;
3,878,043✔
211
  tstrncpy(connObj.user, user, sizeof(connObj.user));
3,878,043✔
212
  tstrncpy(connObj.tokenName, tokenName, sizeof(connObj.tokenName));
3,877,986✔
213
  tstrncpy(connObj.app, app, sizeof(connObj.app));
3,878,043✔
214
  tstrncpy(connObj.sVer, sVer, sizeof(connObj.sVer));
3,877,296✔
215

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

228

229

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

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

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

241
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &connId, sizeof(connId));
39,655,733✔
242
  if (pConn == NULL) {
39,662,161✔
243
    mDebug("conn:%u, already destroyed", connId);
731,619✔
244
    return NULL;
731,619✔
245
  }
246

247
  pConn->lastAccessTimeMs = taosGetTimestampMs();
38,924,160✔
248
  mTrace("conn:%u, acquired from cache, data:%p", pConn->id, pConn);
38,925,159✔
249
  return pConn;
38,912,789✔
250
}
251

252
static void mndReleaseConn(SMnode *pMnode, SConnObj *pConn, bool extendLifespan) {
47,174,644✔
253
  if (pConn == NULL) return;
47,174,644✔
254
  mTrace("conn:%u, released from cache, data:%p", pConn->id, pConn);
47,157,399✔
255

256
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
47,157,399✔
257
  if (extendLifespan) taosCacheTryExtendLifeSpan(pMgmt->connCache, (void **)&pConn);
47,155,034✔
258
  taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
47,156,206✔
259
}
260

261
void *mndGetNextConn(SMnode *pMnode, SCacheIter *pIter) {
4,810,066✔
262
  SConnObj *pConn = NULL;
4,810,066✔
263
  bool      hasNext = taosCacheIterNext(pIter);
4,810,066✔
264
  if (hasNext) {
4,810,066✔
265
    size_t dataLen = 0;
4,708,467✔
266
    pConn = taosCacheIterGetData(pIter, &dataLen);
4,708,467✔
267
  } else {
268
    taosCacheDestroyIter(pIter);
101,599✔
269
  }
270

271
  return pConn;
4,810,066✔
272
}
273

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

280

281

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

291
  int32_t    count = 0;
61,627✔
292
  SConnObj  *pConn = NULL;
61,627✔
293
  while ((pConn = mndGetNextConn(pMnode, pIter)) != NULL) {
4,410,357✔
294
    if (strncmp(pConn->user, user, TSDB_USER_LEN) == 0) {
4,348,912✔
295
      count++;
1,081,235✔
296
    }
297
    mndReleaseConn(pMnode, pConn, true);
4,348,912✔
298
  }
299

300
  return count;
61,809✔
301
}
302

303

304

305
static int32_t verifyPassword(SUserObj* pUser, const char* inputPass) {
3,155,433✔
306
  int32_t code = 0;
3,155,433✔
307

308
  const char* currPass = pUser->passwords[0].pass;
3,155,433✔
309
  char pass[TSDB_PASSWORD_LEN] = {0};
3,156,020✔
310
  (void)memcpy(pass, inputPass, TSDB_PASSWORD_LEN);
3,155,321✔
311
  pass[TSDB_PASSWORD_LEN - 1] = 0;
3,155,909✔
312

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

320
  // constant time comparison to prevent timing attack
321
  volatile uint8_t res = 0;
3,155,493✔
322
  for (size_t i = 0; i < sizeof(pass) - 1; i++) {
100,979,676✔
323
    res |= pass[i] ^ currPass[i];
97,824,183✔
324
  }
325

326
 return (res == 0) ? TSDB_CODE_SUCCESS: TSDB_CODE_MND_AUTH_FAILURE;
3,155,493✔
327
}
328

329

330

331
static bool verifyTotp(SUserObj *pUser, int32_t totpCode) {
3,147,759✔
332
  if (!mndIsTotpEnabledUser(pUser)) {
3,147,759✔
333
    return true;
3,143,429✔
334
  }
335
  return taosVerifyTotpCode(pUser->totpsecret, sizeof(pUser->totpsecret), totpCode, 6, 1) != 0;
4,934✔
336
}
337

338

339

340
static int32_t mndProcessConnectReq(SRpcMsg *pReq) {
3,163,631✔
341
  int32_t          code = 0, lino = 0;
3,163,631✔
342

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

357
  if (token != NULL && mndGetCachedTokenInfo(token, &ti) == NULL) {
3,163,454✔
358
    TAOS_CHECK_GOTO(TSDB_CODE_MND_TOKEN_NOT_EXIST, &lino, _OVER);
×
359
  }
360
  TAOS_CHECK_GOTO(tDeserializeSConnectReq(pReq->pCont, pReq->contLen, &connReq), &lino, _OVER);
3,163,454✔
361
  TAOS_CHECK_GOTO(taosCheckVersionCompatibleFromStr(connReq.sVer, td_version, 3), &lino, _OVER);
3,163,604✔
362
  TAOS_CHECK_GOTO(tVerifyConnectReqSignature(&connReq), &lino, _OVER);
3,163,573✔
363
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, user, &pUser), &lino, _OVER);
3,162,979✔
364

365
  SLoginInfo li = {0};
3,161,557✔
366
  mndGetUserLoginInfo(user, &li);
3,161,557✔
367
  TAOS_CHECK_GOTO(mndCheckConnectPrivilege(pMnode, pUser, token, &li), &lino, _OVER);
3,161,298✔
368

369
  if (token != NULL || tsMndSkipGrant) {
3,157,144✔
370
    li.lastLoginTime= now;
2,017✔
371
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
2,017✔
372
      mndSetUserLoginInfo(user, &li);
1,708✔
373
    }
374
  } else if ((code = verifyPassword(pUser, connReq.passwd)) == TSDB_CODE_MND_AUTH_FAILURE) {
3,155,127✔
375
    if (pUser->failedLoginAttempts >= 0) {
7,829✔
376
      if (li.failedLoginCount >= pUser->failedLoginAttempts) {
6,582✔
377
        // if we can get here, it means the lock time has passed, so reset the counter
378
        li.failedLoginCount = 0;
×
379
      }
380
      li.failedLoginCount++;
6,582✔
381
      li.lastFailedLoginTime = now;
6,582✔
382
    }
383
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
7,734✔
384
      mndSetUserLoginInfo(user, &li);
7,734✔
385
    }
386
    TAOS_CHECK_GOTO(code, &lino, _OVER);
7,734✔
387
  } else if (code != TSDB_CODE_SUCCESS) {
3,147,721✔
388
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
389
  } else if (!verifyTotp(pUser, connReq.totpCode)) {
3,147,721✔
390
    TAOS_CHECK_GOTO(TSDB_CODE_MND_WRONG_TOTP_CODE, &lino, _OVER);
3,324✔
391
  } else {
392
    li.failedLoginCount = 0;
3,144,368✔
393
    li.lastLoginTime= now;
3,144,368✔
394
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
3,144,368✔
395
      mndSetUserLoginInfo(user, &li);
3,144,387✔
396
    }
397
  } 
398

399
  if (connReq.db[0] != 0) {
3,146,893✔
400
    char db[TSDB_DB_FNAME_LEN] = {0};
433,490✔
401
    (void)snprintf(db, TSDB_DB_FNAME_LEN, "%d%s%s", pUser->acctId, TS_PATH_DELIMITER, connReq.db);
433,490✔
402
    pDb = mndAcquireDb(pMnode, db);
433,490✔
403
    if (pDb == NULL) {
433,490✔
404
      if (0 != strcmp(connReq.db, TSDB_INFORMATION_SCHEMA_DB) && (0 != strcmp(connReq.db, TSDB_PERFORMANCE_SCHEMA_DB))) {
1,119✔
405
        TAOS_CHECK_GOTO(TSDB_CODE_MND_DB_NOT_EXIST, &lino, _OVER);
303✔
406
      }
407
    }
408

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

412
  if (connReq.connType == CONN_TYPE__AUTH_TEST) {
3,146,424✔
413
    code = 0;
×
414
    goto _OVER;
×
415
  }
416

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

423
  SConnectRsp connectRsp = {0};
3,145,563✔
424
  connectRsp.acctId = pUser->acctId;
3,145,715✔
425
  connectRsp.superUser = pUser->superUser;
3,145,511✔
426
  connectRsp.sysInfo = pUser->sysInfo;
3,145,655✔
427
  connectRsp.minSecLevel = pUser->minSecLevel;
3,145,687✔
428
  connectRsp.maxSecLevel = pUser->maxSecLevel;
3,145,707✔
429
  connectRsp.sodInitial = (pMnode->sodPhase == TSDB_SOD_PHASE_INITIAL ? 1 : 0);
3,146,163✔
430
  connectRsp.macActive = (pMnode->macActive == MAC_MODE_MANDATORY ? 1 : 0);
3,145,627✔
431
  connectRsp.clusterId = pMnode->clusterId;
3,145,699✔
432
  connectRsp.connId = pConn->id;
3,145,707✔
433
  connectRsp.connType = connReq.connType;
3,146,336✔
434
  connectRsp.dnodeNum = mndGetDnodeSize(pMnode);
3,146,336✔
435
  connectRsp.svrTimestamp = taosGetTimestampSec();
3,145,772✔
436
  connectRsp.passVer = pUser->passVersion;
3,145,712✔
437
  connectRsp.authVer = pUser->authVersion;
3,145,607✔
438
  connectRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
3,145,715✔
439
  connectRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
3,145,715✔
440
  connectRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
3,145,715✔
441
  connectRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
3,145,715✔
442
  connectRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
3,145,715✔
443
  connectRsp.enableAuditDelete = tsEnableAuditDelete;
3,145,715✔
444
  connectRsp.enableAuditSelect = tsEnableAuditSelect;
3,145,715✔
445
  connectRsp.enableAuditInsert = tsEnableAuditInsert;
3,145,715✔
446
  connectRsp.auditLevel = tsAuditLevel;
3,145,715✔
447
  tstrncpy(connectRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
3,145,715✔
448
  connectRsp.whiteListVer = pUser->ipWhiteListVer;
3,146,416✔
449
  connectRsp.timeWhiteListVer = pUser->timeWhiteListVer;
3,146,303✔
450
  connectRsp.userId = pUser->uid;
3,145,701✔
451

452

453
  tstrncpy(connectRsp.sVer, td_version, sizeof(connectRsp.sVer));
3,145,527✔
454
  tstrncpy(connectRsp.user, user, sizeof(connectRsp.user));
3,146,408✔
455
  tstrncpy(connectRsp.tokenName, ti.name, sizeof(connectRsp.tokenName));
3,146,359✔
456
  (void)snprintf(connectRsp.sDetailVer, sizeof(connectRsp.sDetailVer), "ver:%s\nbuild:%s\ngitinfo:%s", td_version,
3,145,677✔
457
                 td_buildinfo, td_gitinfo);
458
  mndGetMnodeEpSet(pMnode, &connectRsp.epSet);
3,145,677✔
459

460
  int32_t contLen = tSerializeSConnectRsp(NULL, 0, &connectRsp);
3,146,424✔
461
  if (contLen < 0) {
3,145,279✔
462
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
463
  }
464
  void *pRsp = rpcMallocCont(contLen);
3,145,279✔
465
  if (pRsp == NULL) {
3,146,342✔
466
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
467
  }
468

469
  contLen = tSerializeSConnectRsp(pRsp, contLen, &connectRsp);
3,146,342✔
470
  if (contLen < 0) {
3,145,655✔
471
    rpcFreeCont(pRsp);
×
472
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
473
  }
474

475
  pReq->info.rspLen = contLen;
3,145,655✔
476
  pReq->info.rsp = pRsp;
3,146,424✔
477

478
  mGDebug("user:%s, login from %s:%d, conn:%u, app:%s, db:%s", user, ip, port, pConn->id, connReq.app, connReq.db);
3,145,655✔
479
  code = 0;
3,146,424✔
480

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

492
_OVER:
3,161,692✔
493
  if (code != 0) {
3,163,669✔
494
    mGError("user:%s, failed to login from %s since %s, line:%d, db:%s", user, ip, tstrerror(code), lino, connReq.db);
17,245✔
495
  }
496

497
  mndReleaseUser(pMnode, pUser);
3,163,669✔
498
  mndReleaseDb(pMnode, pDb);
3,163,669✔
499
  mndReleaseConn(pMnode, pConn, true);
3,163,669✔
500

501
  TAOS_RETURN(code);
3,163,669✔
502
}
503

504

505

506
static int32_t mndSaveQueryList(SConnObj *pConn, SQueryHbReqBasic *pBasic) {
39,652,511✔
507
  taosWLockLatch(&pConn->queryLock);
39,652,511✔
508

509
  taosArrayDestroyEx(pConn->pQueries, tFreeClientHbQueryDesc);
39,662,487✔
510

511
  pConn->pQueries = pBasic->queryDesc;
39,659,035✔
512
  pConn->numOfQueries = pBasic->queryDesc ? taosArrayGetSize(pBasic->queryDesc) : 0;
39,658,836✔
513
  pBasic->queryDesc = NULL;
39,660,693✔
514

515
  mDebug("queries updated in conn %u, num:%d", pConn->id, pConn->numOfQueries);
39,659,224✔
516

517
  taosWUnLockLatch(&pConn->queryLock);
39,665,653✔
518

519
  return TSDB_CODE_SUCCESS;
39,662,487✔
520
}
521

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

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

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

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

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

548
static SAppObj *mndAcquireApp(SMnode *pMnode, int64_t appId) {
39,659,498✔
549
  terrno = 0;
39,659,498✔
550
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
39,660,272✔
551

552
  SAppObj *pApp = taosCacheAcquireByKey(pMgmt->appCache, &appId, sizeof(appId));
39,658,043✔
553
  if (pApp == NULL) {
39,653,946✔
554
    mDebug("app %" PRIx64 " not in cache", appId);
913,863✔
555
    return NULL;
913,863✔
556
  }
557

558
  pApp->lastAccessTimeMs = (uint64_t)taosGetTimestampMs();
38,744,299✔
559

560
  mTrace("app %" PRIx64 " acquired from cache", appId);
38,741,512✔
561
  return pApp;
38,733,539✔
562
}
563

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

568
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
39,655,242✔
569
  taosCacheRelease(pMgmt->appCache, (void **)&pApp, false);
39,657,335✔
570
}
571

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

582
  return pApp;
7,372✔
583
}
584

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

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

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

614
  (void)memcpy(&pApp->summary, &pReq->summary, sizeof(pReq->summary));
38,741,551✔
615

616
  mndReleaseApp(pMnode, pApp);
38,743,950✔
617

618
  return TSDB_CODE_SUCCESS;
38,731,562✔
619
}
620

621
static int32_t mndGetOnlineDnodeNum(SMnode *pMnode, int32_t *num) {
31,576,264✔
622
  SSdb      *pSdb = pMnode->pSdb;
31,576,264✔
623
  SDnodeObj *pDnode = NULL;
31,574,080✔
624
  int64_t    curMs = taosGetTimestampMs();
31,573,394✔
625
  void      *pIter = NULL;
31,573,394✔
626

627
  while (true) {
51,309,923✔
628
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
82,883,317✔
629
    if (pIter == NULL) break;
82,888,030✔
630

631
    bool online = mndIsDnodeOnline(pDnode, curMs);
51,308,520✔
632
    if (online) {
51,305,240✔
633
      (*num)++;
49,826,215✔
634
    }
635

636
    sdbRelease(pSdb, pDnode);
51,296,609✔
637
  }
638

639
  return TSDB_CODE_SUCCESS;
31,579,510✔
640
}
641

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

648
  if (0 != pHbReq->app.appId) {
39,657,878✔
649
    TAOS_CHECK_RETURN(mndUpdateAppInfo(pMnode, pHbReq, &pMsg->info.conn));
39,651,654✔
650
  }
651

652
  if (pHbReq->query) {
39,643,690✔
653
    SQueryHbReqBasic *pBasic = pHbReq->query;
39,658,238✔
654
    SConnObj *pConn = mndAcquireConn(pMnode, pBasic->connId);
39,657,102✔
655
    if (pConn == NULL) {
39,644,957✔
656
      SRpcConnInfo  connInfo = pMsg->info.conn;
731,547✔
657
      const char* user = pHbReq->user;
731,619✔
658
      pConn = mndCreateConn(pMnode, user, pHbReq->tokenName, pHbReq->connKey.connType, &connInfo.cliAddr, pHbReq->app.pid,
731,619✔
659
                            pHbReq->app.name, 0, pHbReq->sVer);
731,536✔
660
      if (pConn == NULL) {
731,464✔
661
        mError("user:%s, conn:%u is freed and failed to create new since %s", user, pBasic->connId, terrstr());
×
662
        code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
663
        if (terrno != 0) code = terrno;
×
664
        TAOS_RETURN(code);
×
665
      } else {
666
        mDebug("user:%s, conn:%u is freed, will create a new conn:%u", user, pBasic->connId, pConn->id);
731,464✔
667
      }
668
    }
669

670
    setUserInfo2Conn(pConn, pHbReq->userApp, pHbReq->userIp, pHbReq->cInfo);
39,645,029✔
671
    setUserInfoIpToConn(pConn, &pHbReq->userDualIp);
39,659,153✔
672

673
    SQueryHbRspBasic *rspBasic = taosMemoryCalloc(1, sizeof(SQueryHbRspBasic));
39,643,578✔
674
    if (rspBasic == NULL) {
39,647,893✔
675
      mndReleaseConn(pMnode, pConn, true);
×
676
      code = terrno;
×
677
      mError("user:%s, conn:%u failed to process hb while since %s", pConn->user, pBasic->connId, terrstr());
×
678
      TAOS_RETURN(code);
×
679
    }
680

681
    TAOS_CHECK_RETURN(mndSaveQueryList(pConn, pBasic));
39,647,893✔
682
    if (pConn->killed != 0) {
39,662,487✔
683
      rspBasic->killConnection = 1;
×
684
    }
685

686
    if (pConn->killId != 0) {
39,662,487✔
687
      rspBasic->killRid = pConn->killId;
1,434✔
688
      pConn->killId = 0;
1,434✔
689
    }
690

691
    rspBasic->connId = pConn->id;
39,662,060✔
692
    rspBasic->connId = pConn->id;
39,661,475✔
693
    rspBasic->totalDnodes = pObj->totalDnodes;
39,660,551✔
694
    rspBasic->onlineDnodes = pObj->onlineDnodes;
39,660,140✔
695
    rspBasic->epSet = pObj->epSet;
39,661,649✔
696
    rspBasic->pQnodeList = taosArrayDup(pObj->pQnodeList, NULL);
39,660,479✔
697

698
    mndReleaseConn(pMnode, pConn, true);
39,661,343✔
699

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

705
  int32_t kvNum = taosHashGetSize(pHbReq->info);
39,662,587✔
706
  if (NULL == pHbReq->info || kvNum <= 0) {
39,660,707✔
707
    if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
22,062,063✔
708
      mError("failed to put rsp into array, but continue at this heartbeat");
×
709
    }
710
    return TSDB_CODE_SUCCESS;
11,031,851✔
711
  }
712

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

721
#ifdef TD_ENTERPRISE
722
  bool             needCheck = true;
28,630,402✔
723
  int32_t          key = HEARTBEAT_KEY_DYN_VIEW;
28,630,495✔
724
  SDynViewVersion *pDynViewVer = NULL;
28,630,310✔
725
  SKv             *pKv = taosHashGet(pHbReq->info, &key, sizeof(key));
28,630,310✔
726
  if (NULL != pKv) {
28,630,134✔
727
    pDynViewVer = pKv->value;
2,485✔
728
    mTrace("recv view dyn ver, bootTs:%" PRId64 ", ver:%" PRIu64, pDynViewVer->svrBootTs, pDynViewVer->dynViewVer);
2,485✔
729

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

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

747
  void *pIter = taosHashIterate(pHbReq->info, NULL);
28,630,134✔
748
  while (pIter != NULL) {
73,830,663✔
749
    SKv *kv = pIter;
45,200,957✔
750

751
    switch (kv->key) {
45,200,957✔
752
      case HEARTBEAT_KEY_USER_AUTHINFO: {
28,629,731✔
753
        void   *rspMsg = NULL;
28,629,731✔
754
        int32_t rspLen = 0;
28,629,731✔
755
        (void)mndValidateUserAuthInfo(pMnode, kv->value, kv->valueLen / sizeof(SUserAuthVersion), &rspMsg, &rspLen,
28,629,731✔
756
                                      pObj->ipWhiteListVer);
757
        if (rspMsg && rspLen > 0) {
28,628,906✔
758
          SKv kv1 = {.key = HEARTBEAT_KEY_USER_AUTHINFO, .valueLen = rspLen, .value = rspMsg};
28,628,258✔
759
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
57,254,602✔
760
            mError("failed to put kv into array, but continue at this heartbeat");
×
761
          }
762
        }
763
        break;
28,628,833✔
764
      }
765
      case HEARTBEAT_KEY_DBINFO: {
9,745,834✔
766
        void   *rspMsg = NULL;
9,745,834✔
767
        int32_t rspLen = 0;
9,746,477✔
768
        (void)mndValidateDbInfo(pMnode, kv->value, kv->valueLen / sizeof(SDbCacheInfo), &rspMsg, &rspLen);
9,745,741✔
769
        if (rspMsg && rspLen > 0) {
9,746,477✔
770
          SKv kv1 = {.key = HEARTBEAT_KEY_DBINFO, .valueLen = rspLen, .value = rspMsg};
9,746,477✔
771
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
19,492,954✔
772
            mError("failed to put kv into array, but continue at this heartbeat");
×
773
          }
774
        }
775
        break;
9,746,477✔
776
      }
777
      case HEARTBEAT_KEY_STBINFO: {
6,808,892✔
778
        void   *rspMsg = NULL;
6,808,892✔
779
        int32_t rspLen = 0;
6,808,892✔
780
        (void)mndValidateStbInfo(pMnode, kv->value, kv->valueLen / sizeof(SSTableVersion), &rspMsg, &rspLen);
6,808,892✔
781
        if (rspMsg && rspLen > 0) {
6,808,892✔
782
          SKv kv1 = {.key = HEARTBEAT_KEY_STBINFO, .valueLen = rspLen, .value = rspMsg};
6,808,892✔
783
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
13,617,784✔
784
            mError("failed to put kv into array, but continue at this heartbeat");
×
785
          }
786
        }
787
        break;
6,808,892✔
788
      }
789
#ifdef TD_ENTERPRISE
790
      case HEARTBEAT_KEY_DYN_VIEW: {
2,485✔
791
        break;
2,485✔
792
      }
793
      case HEARTBEAT_KEY_VIEWINFO: {
2,485✔
794
        if (!needCheck) {
2,485✔
795
          break;
438✔
796
        }
797

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

828
    pIter = taosHashIterate(pHbReq->info, pIter);
45,200,041✔
829
  }
830

831
  if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
57,260,141✔
832
    if (terrno != 0) code = terrno;
×
833
  }
834
  TAOS_RETURN(code);
28,630,435✔
835
}
836

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

842
  SClientHbBatchReq batchReq = {0};
31,575,902✔
843
  if (tDeserializeSClientHbBatchReq(pReq->pCont, pReq->contLen, &batchReq) != 0) {
31,574,290✔
844
    taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
×
845
    code = TSDB_CODE_INVALID_MSG;
×
846
    TAOS_RETURN(code);
×
847
  }
848

849
  SConnPreparedObj obj = {0};
31,576,692✔
850
  obj.totalDnodes = mndGetDnodeSize(pMnode);
31,577,787✔
851
  obj.ipWhiteListVer = batchReq.ipWhiteListVer;
31,578,692✔
852
  TAOS_CHECK_RETURN(mndGetOnlineDnodeNum(pMnode, &obj.onlineDnodes));
31,578,692✔
853
  mndGetMnodeEpSet(pMnode, &obj.epSet);
31,578,012✔
854
  TAOS_CHECK_RETURN(mndCreateQnodeList(pMnode, &obj.pQnodeList, -1));
31,579,510✔
855

856
  SClientHbBatchRsp batchRsp = {0};
31,574,290✔
857
  batchRsp.svrTimestamp = taosGetTimestampSec();
31,572,762✔
858
  batchRsp.rsps = taosArrayInit(0, sizeof(SClientHbRsp));
31,574,878✔
859
  if (batchRsp.rsps == NULL) {
31,571,099✔
860
    TAOS_CHECK_EXIT(terrno);
×
861
  }
862
  batchRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
31,571,099✔
863
  batchRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
31,571,099✔
864
  batchRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
31,571,099✔
865
  tstrncpy(batchRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
31,571,099✔
866
  batchRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
31,575,094✔
867
  batchRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
31,575,094✔
868
  batchRsp.enableAuditDelete = tsEnableAuditDelete;
31,575,094✔
869
  batchRsp.enableAuditSelect = tsEnableAuditSelect;
31,575,094✔
870
  batchRsp.enableAuditInsert = tsEnableAuditInsert;
31,575,094✔
871
  batchRsp.auditLevel = tsAuditLevel;
31,575,094✔
872
  batchRsp.enableStrongPass = tsEnableStrongPassword;
31,575,094✔
873
  batchRsp.sodInitial = (pMnode->sodPhase == TSDB_SOD_PHASE_INITIAL ? 1 : 0);
31,575,094✔
874
  batchRsp.macActive = (pMnode->macActive == MAC_MODE_MANDATORY ? 1 : 0);
31,578,512✔
875

876
  int32_t sz = taosArrayGetSize(batchReq.reqs);
31,574,902✔
877
  for (int i = 0; i < sz; i++) {
71,238,732✔
878
    SClientHbReq *pHbReq = taosArrayGet(batchReq.reqs, i);
39,659,848✔
879
    if (pHbReq->connKey.connType == CONN_TYPE__QUERY || pHbReq->connKey.connType == CONN_TYPE__TMQ) {
39,657,631✔
880
      TAOS_CHECK_EXIT(mndProcessQueryHeartBeat(pMnode, pReq, pHbReq, &batchRsp, &obj));
39,658,644✔
881
    } 
882
  }
883
  taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
31,578,884✔
884

885
  int32_t tlen = tSerializeSClientHbBatchRsp(NULL, 0, &batchRsp);
31,576,822✔
886
  if (tlen < 0) {
31,565,276✔
887
    TAOS_CHECK_EXIT(tlen);
×
888
  }
889
  void *buf = rpcMallocCont(tlen);
31,565,276✔
890
  if (!buf) {
31,571,910✔
891
    TAOS_CHECK_EXIT(terrno);
×
892
  }
893
  tlen = tSerializeSClientHbBatchRsp(buf, tlen, &batchRsp);
31,571,910✔
894
  if (tlen < 0) {
31,570,702✔
895
    rpcFreeCont(buf);
×
896
    TAOS_CHECK_EXIT(tlen);
×
897
  }
898
  pReq->info.rspLen = tlen;
31,570,702✔
899
  pReq->info.rsp = buf;
31,569,180✔
900
_exit:
31,567,709✔
901
  tFreeClientHbBatchRsp(&batchRsp);
902

903
  taosArrayDestroy(obj.pQnodeList);
31,569,310✔
904

905
  TAOS_RETURN(code);
31,569,051✔
906
}
907

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

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

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

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

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

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

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

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

966
static int32_t mndProcessSvrVerReq(SRpcMsg *pReq) {
×
967
  int32_t       code = 0;
×
968
  int32_t       lino = 0;
×
969
  SServerVerRsp rsp = {0};
×
970
  tstrncpy(rsp.ver, td_version, sizeof(rsp.ver));
×
971

972
  int32_t contLen = tSerializeSServerVerRsp(NULL, 0, &rsp);
×
973
  if (contLen < 0) {
×
974
    TAOS_CHECK_EXIT(contLen);
×
975
  }
976
  void *pRsp = rpcMallocCont(contLen);
×
977
  if (pRsp == NULL) {
×
978
    TAOS_CHECK_EXIT(terrno);
×
979
  }
980
  contLen = tSerializeSServerVerRsp(pRsp, contLen, &rsp);
×
981
  if (contLen < 0) {
×
982
    rpcFreeCont(pRsp);
×
983
    TAOS_CHECK_EXIT(contLen);
×
984
  }
985

986
  pReq->info.rspLen = contLen;
×
987
  pReq->info.rsp = pRsp;
×
988

989
_exit:
×
990

991
  TAOS_RETURN(code);
×
992
}
993

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

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

1008
  while (numOfRows < rows) {
308,960✔
1009
    pConn = mndGetNextConn(pMnode, pShow->pIter);
308,960✔
1010
    if (pConn == NULL) {
308,960✔
1011
      pShow->pIter = NULL;
15,744✔
1012
      break;
15,744✔
1013
    }
1014

1015
    if ((taosGetTimestampMs() - pConn->lastAccessTimeMs) > ((int64_t)CACHE_OBJ_KEEP_TIME * 1000)) {
293,216✔
1016
      continue;
257,608✔
1017
    }
1018

1019
    cols = 0;
35,608✔
1020

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

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

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

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

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

1061
    STR_TO_VARSTR(endpoint, addr);
35,608✔
1062

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

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

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

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

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

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

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

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

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

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

1139
    numOfRows++;
35,608✔
1140
  }
1141

1142
  pShow->numOfRows += numOfRows;
15,744✔
1143
  return numOfRows;
15,744✔
1144
}
1145

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1278
    char    subStatus[TSDB_SHOW_SUBQUERY_LEN + VARSTR_HEADER_SIZE] = {0};
25,110✔
1279
    int64_t reserve = 128;
25,110✔
1280
    int32_t strSize = sizeof(subStatus);
25,110✔
1281
    int32_t offset = VARSTR_HEADER_SIZE;
25,110✔
1282
    for (int32_t i = 0; i < pQuery->subPlanNum && offset + reserve < strSize; ++i) {
44,946✔
1283
      if (i) {
19,836✔
1284
        offset += snprintf(subStatus + offset, sizeof(subStatus) - offset, ",");
×
1285
      }
1286
      if (offset + reserve >= strSize) break;
19,836✔
1287

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

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

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

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

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

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

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

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

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

1370
    pBlock->info.rows++;
25,110✔
1371
  }
1372

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

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

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

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

1399
  while (numOfRows < rows) {
90,567✔
1400
    pConn = mndGetNextConn(pMnode, pShow->pIter);
90,567✔
1401
    if (pConn == NULL) {
90,567✔
1402
      pShow->pIter = NULL;
24,046✔
1403
      break;
24,046✔
1404
    }
1405

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

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

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

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

1435
    cols = 0;
3,686✔
1436

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1563
int32_t mndGetNumOfConnections(SMnode *pMnode) {
86✔
1564
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
86✔
1565
  return taosCacheGetNumOfObj(pMgmt->connCache);
86✔
1566
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc