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

taosdata / TDengine / #5071

17 May 2026 01:15AM UTC coverage: 63.054% (-10.3%) from 73.326%
#5071

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)

238317 of 377957 relevant lines covered (63.05%)

130539817.12 hits per line

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

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

101
  // in ms
102
  int32_t checkTime = CACHE_OBJ_KEEP_TIME * 1000;
404,647✔
103
  pMgmt->connCache = taosCacheInit(TSDB_DATA_TYPE_UINT, checkTime, false, (__cache_free_fn_t)mndFreeConn, "conn");
404,647✔
104
  if (pMgmt->connCache == NULL) {
404,647✔
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");
404,647✔
111
  if (pMgmt->appCache == NULL) {
404,647✔
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);
404,647✔
118
  mndSetMsgHandle(pMnode, TDMT_MND_CONNECT, mndProcessConnectReq);
404,647✔
119
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_QUERY, mndProcessKillQueryReq);
404,647✔
120
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_CONN, mndProcessKillConnReq);
404,647✔
121
  mndSetMsgHandle(pMnode, TDMT_MND_SERVER_VERSION, mndProcessSvrVerReq);
404,647✔
122

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

130
  TAOS_RETURN(code);
404,647✔
131
}
132

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

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

146
static void getUserIpFromConnObj(SConnObj *pConn, char *dst) {
113,691✔
147
  static char *none = "0.0.0.0";
148
  if (pConn->userIp != 0 && pConn->userIp != INADDR_NONE) {
113,691✔
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) {
113,691✔
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;
113,691✔
160
}
161
static void setUserInfo2Conn(SConnObj *connObj, char *userApp, uint32_t userIp, char *cInfo) {
22,406,957✔
162
  if (connObj == NULL) {
22,406,957✔
163
    return;
×
164
  }
165
  tstrncpy(connObj->userApp, userApp, sizeof(connObj->userApp));
22,406,957✔
166
  tstrncpy(connObj->cInfo, cInfo, sizeof(connObj->cInfo));
22,405,567✔
167
  connObj->userIp = userIp;
22,405,813✔
168
}
169
static void setUserInfoIpToConn(SConnObj *connObj, SIpRange *pRange) {
22,406,957✔
170
  int32_t code = 0;
22,406,957✔
171
  if (connObj == NULL) {
22,406,957✔
172
    return;
×
173
  }
174

175
  code = tIpUintToStr(pRange, &connObj->userDualIp);
22,406,957✔
176
  if (code != 0) {
22,405,733✔
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,
2,630,695✔
185
                               int32_t pid, const char *app, int64_t startTime, const char *sVer) {
186
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
2,630,695✔
187

188
  char     connStr[255] = {0};
2,630,695✔
189
  char    *ip = IP_ADDR_STR(pAddr);
2,630,695✔
190
  uint16_t port = pAddr->port;
2,630,672✔
191

192
  int32_t  len = tsnprintf(connStr, sizeof(connStr), "%s%d%d%d%s", user, ip, port, pid, app);
2,630,672✔
193
  uint32_t connId = mndGenerateUid(connStr, len);
2,631,330✔
194
  if (startTime == 0) startTime = taosGetTimestampMs();
3,063,556✔
195

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

210
  connObj.lastAccessTimeMs = connObj.loginTimeMs;
2,631,330✔
211
  tstrncpy(connObj.user, user, sizeof(connObj.user));
2,631,330✔
212
  tstrncpy(connObj.tokenName, tokenName, sizeof(connObj.tokenName));
2,631,330✔
213
  tstrncpy(connObj.app, app, sizeof(connObj.app));
2,631,330✔
214
  tstrncpy(connObj.sVer, sVer, sizeof(connObj.sVer));
2,630,672✔
215

216
  SConnObj *pConn =
217
      taosCachePut(pMgmt->connCache, &connId, sizeof(uint32_t), &connObj, sizeof(connObj), CACHE_OBJ_KEEP_TIME * 1000);
2,630,672✔
218
  if (pConn == NULL) {
2,629,825✔
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);
2,629,825✔
224
    return pConn;
2,628,509✔
225
  }
226
}
227

228

229

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

235
  mTrace("conn:%u, is destroyed, data:%p", pConn->id, pConn);
2,631,330✔
236
}
2,631,330✔
237

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

241
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &connId, sizeof(connId));
22,406,957✔
242
  if (pConn == NULL) {
22,406,807✔
243
    mDebug("conn:%u, already destroyed", connId);
432,024✔
244
    return NULL;
432,024✔
245
  }
246

247
  pConn->lastAccessTimeMs = taosGetTimestampMs();
21,974,783✔
248
  mTrace("conn:%u, acquired from cache, data:%p", pConn->id, pConn);
21,975,374✔
249
  return pConn;
21,973,348✔
250
}
251

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

256
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
25,049,837✔
257
  if (extendLifespan) taosCacheTryExtendLifeSpan(pMgmt->connCache, (void **)&pConn);
25,049,837✔
258
  taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
25,049,837✔
259
}
260

261
void *mndGetNextConn(SMnode *pMnode, SCacheIter *pIter) {
950,526✔
262
  SConnObj *pConn = NULL;
950,526✔
263
  bool      hasNext = taosCacheIterNext(pIter);
950,526✔
264
  if (hasNext) {
950,526✔
265
    size_t dataLen = 0;
901,437✔
266
    pConn = taosCacheIterGetData(pIter, &dataLen);
901,437✔
267
  } else {
268
    taosCacheDestroyIter(pIter);
49,089✔
269
  }
270

271
  return pConn;
950,101✔
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) {
21,692✔
284
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
21,692✔
285
  SCacheIter   *pIter = taosCacheCreateIter(pMgmt->connCache);
21,692✔
286
  if (pIter == NULL) {
21,692✔
287
    mError("failed to create conn cache iterator");
×
288
    return -1;
×
289
  }
290

291
  int32_t    count = 0;
21,692✔
292
  SConnObj  *pConn = NULL;
21,692✔
293
  while ((pConn = mndGetNextConn(pMnode, pIter)) != NULL) {
464,825✔
294
    if (strncmp(pConn->user, user, TSDB_USER_LEN) == 0) {
443,133✔
295
      count++;
51,040✔
296
    }
297
    mndReleaseConn(pMnode, pConn, true);
443,133✔
298
  }
299

300
  return count;
21,692✔
301
}
302

303

304

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

308
  const char* currPass = pUser->passwords[0].pass;
2,198,448✔
309
  char pass[TSDB_PASSWORD_LEN] = {0};
2,198,365✔
310
  (void)memcpy(pass, inputPass, TSDB_PASSWORD_LEN);
2,197,707✔
311
  pass[TSDB_PASSWORD_LEN - 1] = 0;
2,196,860✔
312

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

320
  // constant time comparison to prevent timing attack
321
  volatile uint8_t res = 0;
2,196,754✔
322
  for (size_t i = 0; i < sizeof(pass) - 1; i++) {
70,233,947✔
323
    res |= pass[i] ^ currPass[i];
68,034,924✔
324
  }
325

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

329

330

331
static bool verifyTotp(SUserObj *pUser, int32_t totpCode) {
2,198,448✔
332
  if (!mndIsTotpEnabledUser(pUser)) {
2,198,448✔
333
    return true;
2,196,062✔
334
  }
335
  return taosVerifyTotpCode(pUser->totpsecret, sizeof(pUser->totpsecret), totpCode, 6, 1) != 0;
×
336
}
337

338

339

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

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

357
  if (token != NULL && mndGetCachedTokenInfo(token, &ti) == NULL) {
2,199,407✔
358
    TAOS_CHECK_GOTO(TSDB_CODE_MND_TOKEN_NOT_EXIST, &lino, _OVER);
×
359
  }
360
  TAOS_CHECK_GOTO(tDeserializeSConnectReq(pReq->pCont, pReq->contLen, &connReq), &lino, _OVER);
2,199,407✔
361
  TAOS_CHECK_GOTO(taosCheckVersionCompatibleFromStr(connReq.sVer, td_version, 3), &lino, _OVER);
2,198,560✔
362
  TAOS_CHECK_GOTO(tVerifyConnectReqSignature(&connReq), &lino, _OVER);
2,197,713✔
363
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, user, &pUser), &lino, _OVER);
2,198,832✔
364

365
  SLoginInfo li = {0};
2,196,511✔
366
  mndGetUserLoginInfo(user, &li);
2,197,358✔
367
  TAOS_CHECK_GOTO(mndCheckConnectPrivilege(pMnode, pUser, token, &li), &lino, _OVER);
2,199,407✔
368

369
  if (token != NULL || tsMndSkipGrant) {
2,198,749✔
370
    li.lastLoginTime= now;
3,306✔
371
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
3,306✔
372
      mndSetUserLoginInfo(user, &li);
384✔
373
    }
374
  } else if ((code = verifyPassword(pUser, connReq.passwd)) == TSDB_CODE_MND_AUTH_FAILURE) {
2,195,443✔
375
    if (pUser->failedLoginAttempts >= 0) {
575✔
376
      if (li.failedLoginCount >= pUser->failedLoginAttempts) {
×
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++;
×
381
      li.lastFailedLoginTime = now;
×
382
    }
383
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
×
384
      mndSetUserLoginInfo(user, &li);
×
385
    }
386
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
387
  } else if (code != TSDB_CODE_SUCCESS) {
2,198,448✔
388
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
389
  } else if (!verifyTotp(pUser, connReq.totpCode)) {
2,198,448✔
390
    TAOS_CHECK_GOTO(TSDB_CODE_MND_WRONG_TOTP_CODE, &lino, _OVER);
×
391
  } else {
392
    li.failedLoginCount = 0;
2,195,487✔
393
    li.lastLoginTime= now;
2,195,487✔
394
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
2,195,487✔
395
      mndSetUserLoginInfo(user, &li);
2,195,487✔
396
    }
397
  } 
398

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

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

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

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

423
  SConnectRsp connectRsp = {0};
2,197,990✔
424
  connectRsp.acctId = pUser->acctId;
2,198,648✔
425
  connectRsp.superUser = pUser->superUser;
2,196,740✔
426
  connectRsp.sysInfo = pUser->sysInfo;
2,197,801✔
427
  connectRsp.minSecLevel = pUser->minSecLevel;
2,197,990✔
428
  connectRsp.maxSecLevel = pUser->maxSecLevel;
2,197,990✔
429
  connectRsp.sodInitial = (pMnode->sodPhase == TSDB_SOD_PHASE_INITIAL ? 1 : 0);
2,196,954✔
430
  connectRsp.macActive = (pMnode->macActive == MAC_MODE_MANDATORY ? 1 : 0);
2,197,143✔
431
  connectRsp.clusterId = pMnode->clusterId;
2,197,801✔
432
  connectRsp.connId = pConn->id;
2,197,143✔
433
  connectRsp.connType = connReq.connType;
2,197,801✔
434
  connectRsp.dnodeNum = mndGetDnodeSize(pMnode);
2,197,801✔
435
  connectRsp.svrTimestamp = taosGetTimestampSec();
2,198,459✔
436
  connectRsp.passVer = pUser->passVersion;
2,198,459✔
437
  connectRsp.authVer = pUser->authVersion;
2,197,801✔
438
  connectRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
2,197,801✔
439
  connectRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
2,197,801✔
440
  connectRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
2,197,801✔
441
  connectRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
2,197,801✔
442
  connectRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
2,197,801✔
443
  connectRsp.enableAuditDelete = tsEnableAuditDelete;
2,197,801✔
444
  connectRsp.enableAuditSelect = tsEnableAuditSelect;
2,197,801✔
445
  connectRsp.enableAuditInsert = tsEnableAuditInsert;
2,197,801✔
446
  connectRsp.auditLevel = tsAuditLevel;
2,197,801✔
447
  tstrncpy(connectRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
2,197,801✔
448
  connectRsp.whiteListVer = pUser->ipWhiteListVer;
2,198,648✔
449
  connectRsp.timeWhiteListVer = pUser->timeWhiteListVer;
2,199,306✔
450
  connectRsp.userId = pUser->uid;
2,199,306✔
451

452

453
  tstrncpy(connectRsp.sVer, td_version, sizeof(connectRsp.sVer));
2,196,568✔
454
  tstrncpy(connectRsp.user, user, sizeof(connectRsp.user));
2,195,722✔
455
  tstrncpy(connectRsp.tokenName, ti.name, sizeof(connectRsp.tokenName));
2,197,415✔
456
  (void)snprintf(connectRsp.sDetailVer, sizeof(connectRsp.sDetailVer), "ver:%s\nbuild:%s\ngitinfo:%s", td_version,
2,198,073✔
457
                 td_buildinfo, td_gitinfo);
458
  mndGetMnodeEpSet(pMnode, &connectRsp.epSet);
2,198,073✔
459

460
  int32_t contLen = tSerializeSConnectRsp(NULL, 0, &connectRsp);
2,199,306✔
461
  if (contLen < 0) {
2,193,642✔
462
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
463
  }
464
  void *pRsp = rpcMallocCont(contLen);
2,193,642✔
465
  if (pRsp == NULL) {
2,197,884✔
466
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
467
  }
468

469
  contLen = tSerializeSConnectRsp(pRsp, contLen, &connectRsp);
2,197,884✔
470
  if (contLen < 0) {
2,198,648✔
471
    rpcFreeCont(pRsp);
×
472
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
473
  }
474

475
  pReq->info.rspLen = contLen;
2,198,648✔
476
  pReq->info.rsp = pRsp;
2,198,731✔
477

478
  mGDebug("user:%s, login from %s:%d, conn:%u, app:%s, db:%s", user, ip, port, pConn->id, connReq.app, connReq.db);
2,197,615✔
479
  code = 0;
2,199,306✔
480

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

492
_OVER:
2,199,104✔
493
  if (code != 0) {
2,199,407✔
494
    mGError("user:%s, failed to login from %s since %s, line:%d, db:%s", user, ip, tstrerror(code), lino, connReq.db);
101✔
495
  }
496

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

501
  TAOS_RETURN(code);
2,199,407✔
502
}
503

504

505

506
static int32_t mndSaveQueryList(SConnObj *pConn, SQueryHbReqBasic *pBasic) {
22,405,233✔
507
  taosWLockLatch(&pConn->queryLock);
22,405,233✔
508

509
  taosArrayDestroyEx(pConn->pQueries, tFreeClientHbQueryDesc);
22,406,995✔
510

511
  pConn->pQueries = pBasic->queryDesc;
22,406,621✔
512
  pConn->numOfQueries = pBasic->queryDesc ? taosArrayGetSize(pBasic->queryDesc) : 0;
22,407,398✔
513
  pBasic->queryDesc = NULL;
22,407,398✔
514

515
  mDebug("queries updated in conn %u, num:%d", pConn->id, pConn->numOfQueries);
22,406,915✔
516

517
  taosWUnLockLatch(&pConn->queryLock);
22,408,305✔
518

519
  return TSDB_CODE_SUCCESS;
22,407,398✔
520
}
521

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

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

534
  SAppObj *pApp =
535
      taosCachePut(pMgmt->appCache, &pReq->appId, sizeof(pReq->appId), &app, sizeof(app), CACHE_OBJ_KEEP_TIME * 1000);
706,729✔
536
  if (pApp == NULL) {
706,729✔
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);
706,729✔
543
  return pApp;
706,729✔
544
}
545

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

548
static SAppObj *mndAcquireApp(SMnode *pMnode, int64_t appId) {
22,405,905✔
549
  terrno = 0;
22,405,905✔
550
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
22,406,601✔
551

552
  SAppObj *pApp = taosCacheAcquireByKey(pMgmt->appCache, &appId, sizeof(appId));
22,405,465✔
553
  if (pApp == NULL) {
22,406,413✔
554
    mDebug("app %" PRIx64 " not in cache", appId);
706,729✔
555
    return NULL;
706,729✔
556
  }
557

558
  pApp->lastAccessTimeMs = (uint64_t)taosGetTimestampMs();
21,700,669✔
559

560
  mTrace("app %" PRIx64 " acquired from cache", appId);
21,698,979✔
561
  return pApp;
21,697,063✔
562
}
563

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

568
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
22,405,594✔
569
  taosCacheRelease(pMgmt->appCache, (void **)&pApp, false);
22,405,840✔
570
}
571

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

582
  return pApp;
×
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) {
22,405,560✔
597
  int32_t    code = 0;
22,405,560✔
598
  SAppHbReq *pReq = &pHbReq->app;
22,405,560✔
599
  SAppObj   *pApp = mndAcquireApp(pMnode, pReq->appId);
22,405,932✔
600
  if (pApp == NULL) {
22,406,167✔
601
    pApp = mndCreateApp(pMnode, &connInfo->cliAddr, pReq);
706,729✔
602
    if (pApp == NULL) {
706,729✔
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);
706,729✔
609
      mndReleaseApp(pMnode, pApp);
706,729✔
610
      return TSDB_CODE_SUCCESS;
706,729✔
611
    }
612
  }
613

614
  (void)memcpy(&pApp->summary, &pReq->summary, sizeof(pReq->summary));
21,699,438✔
615

616
  mndReleaseApp(pMnode, pApp);
21,699,834✔
617

618
  return TSDB_CODE_SUCCESS;
21,697,591✔
619
}
620

621
static int32_t mndGetOnlineDnodeNum(SMnode *pMnode, int32_t *num) {
18,991,303✔
622
  SSdb      *pSdb = pMnode->pSdb;
18,991,303✔
623
  SDnodeObj *pDnode = NULL;
18,993,144✔
624
  int64_t    curMs = taosGetTimestampMs();
18,991,248✔
625
  void      *pIter = NULL;
18,991,248✔
626

627
  while (true) {
28,508,678✔
628
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
47,499,926✔
629
    if (pIter == NULL) break;
47,502,454✔
630

631
    bool online = mndIsDnodeOnline(pDnode, curMs);
28,508,678✔
632
    if (online) {
28,507,650✔
633
      (*num)++;
27,745,761✔
634
    }
635

636
    sdbRelease(pSdb, pDnode);
28,507,650✔
637
  }
638

639
  return TSDB_CODE_SUCCESS;
18,993,776✔
640
}
641

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

648
  if (0 != pHbReq->app.appId) {
22,407,087✔
649
    TAOS_CHECK_RETURN(mndUpdateAppInfo(pMnode, pHbReq, &pMsg->info.conn));
22,405,014✔
650
  }
651

652
  if (pHbReq->query) {
22,404,750✔
653
    SQueryHbReqBasic *pBasic = pHbReq->query;
22,405,905✔
654
    SConnObj *pConn = mndAcquireConn(pMnode, pBasic->connId);
22,406,366✔
655
    if (pConn == NULL) {
22,405,963✔
656
      SRpcConnInfo  connInfo = pMsg->info.conn;
432,024✔
657
      const char* user = pHbReq->user;
432,024✔
658
      pConn = mndCreateConn(pMnode, user, pHbReq->tokenName, pHbReq->connKey.connType, &connInfo.cliAddr, pHbReq->app.pid,
432,024✔
659
                            pHbReq->app.name, 0, pHbReq->sVer);
432,024✔
660
      if (pConn == NULL) {
432,024✔
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);
432,024✔
667
      }
668
    }
669

670
    setUserInfo2Conn(pConn, pHbReq->userApp, pHbReq->userIp, pHbReq->cInfo);
22,405,963✔
671
    setUserInfoIpToConn(pConn, &pHbReq->userDualIp);
22,406,554✔
672

673
    SQueryHbRspBasic *rspBasic = taosMemoryCalloc(1, sizeof(SQueryHbRspBasic));
22,404,739✔
674
    if (rspBasic == NULL) {
22,404,434✔
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));
22,404,434✔
682
    if (pConn->killed != 0) {
22,407,398✔
683
      rspBasic->killConnection = 1;
×
684
    }
685

686
    if (pConn->killId != 0) {
22,407,398✔
687
      rspBasic->killRid = pConn->killId;
1,407✔
688
      pConn->killId = 0;
1,407✔
689
    }
690

691
    rspBasic->connId = pConn->id;
22,407,398✔
692
    rspBasic->connId = pConn->id;
22,407,398✔
693
    rspBasic->totalDnodes = pObj->totalDnodes;
22,407,398✔
694
    rspBasic->onlineDnodes = pObj->onlineDnodes;
22,407,398✔
695
    rspBasic->epSet = pObj->epSet;
22,407,398✔
696
    rspBasic->pQnodeList = taosArrayDup(pObj->pQnodeList, NULL);
22,407,398✔
697

698
    mndReleaseConn(pMnode, pConn, true);
22,407,398✔
699

700
    hbRsp.query = rspBasic;
22,407,398✔
701
  } else {
702
    mDebug("no query info in hb msg");
101✔
703
  }
704

705
  int32_t kvNum = taosHashGetSize(pHbReq->info);
22,407,499✔
706
  if (NULL == pHbReq->info || kvNum <= 0) {
22,407,499✔
707
    if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
9,662,234✔
708
      mError("failed to put rsp into array, but continue at this heartbeat");
×
709
    }
710
    return TSDB_CODE_SUCCESS;
4,831,117✔
711
  }
712

713
  hbRsp.info = taosArrayInit(kvNum, sizeof(SKv));
17,576,382✔
714
  if (NULL == hbRsp.info) {
17,576,382✔
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;
17,576,382✔
723
  int32_t          key = HEARTBEAT_KEY_DYN_VIEW;
17,576,382✔
724
  SDynViewVersion *pDynViewVer = NULL;
17,576,382✔
725
  SKv             *pKv = taosHashGet(pHbReq->info, &key, sizeof(key));
17,576,382✔
726
  if (NULL != pKv) {
17,576,382✔
727
    pDynViewVer = pKv->value;
5,250✔
728
    mTrace("recv view dyn ver, bootTs:%" PRId64 ", ver:%" PRIu64, pDynViewVer->svrBootTs, pDynViewVer->dynViewVer);
5,250✔
729

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

735
    if (needCheck) {
5,250✔
736
      SKv kv1 = {.key = HEARTBEAT_KEY_DYN_VIEW, .valueLen = sizeof(*pDynViewVer), .value = pRspVer};
4,828✔
737
      if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
9,656✔
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,
4,828✔
742
             pRspVer->dynViewVer);
743
    }
744
  }
745
#endif
746

747
  void *pIter = taosHashIterate(pHbReq->info, NULL);
17,576,382✔
748
  while (pIter != NULL) {
45,711,123✔
749
    SKv *kv = pIter;
28,136,096✔
750

751
    switch (kv->key) {
28,136,096✔
752
      case HEARTBEAT_KEY_USER_AUTHINFO: {
17,576,281✔
753
        void   *rspMsg = NULL;
17,576,281✔
754
        int32_t rspLen = 0;
17,576,281✔
755
        (void)mndValidateUserAuthInfo(pMnode, kv->value, kv->valueLen / sizeof(SUserAuthVersion), &rspMsg, &rspLen,
17,576,281✔
756
                                      pObj->ipWhiteListVer);
757
        if (rspMsg && rspLen > 0) {
17,575,134✔
758
          SKv kv1 = {.key = HEARTBEAT_KEY_USER_AUTHINFO, .valueLen = rspLen, .value = rspMsg};
17,575,559✔
759
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
35,149,059✔
760
            mError("failed to put kv into array, but continue at this heartbeat");
×
761
          }
762
        }
763
        break;
17,574,222✔
764
      }
765
      case HEARTBEAT_KEY_DBINFO: {
6,149,874✔
766
        void   *rspMsg = NULL;
6,149,874✔
767
        int32_t rspLen = 0;
6,149,874✔
768
        (void)mndValidateDbInfo(pMnode, kv->value, kv->valueLen / sizeof(SDbCacheInfo), &rspMsg, &rspLen);
6,149,874✔
769
        if (rspMsg && rspLen > 0) {
6,149,874✔
770
          SKv kv1 = {.key = HEARTBEAT_KEY_DBINFO, .valueLen = rspLen, .value = rspMsg};
6,149,874✔
771
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
12,299,748✔
772
            mError("failed to put kv into array, but continue at this heartbeat");
×
773
          }
774
        }
775
        break;
6,149,874✔
776
      }
777
      case HEARTBEAT_KEY_STBINFO: {
4,399,340✔
778
        void   *rspMsg = NULL;
4,399,340✔
779
        int32_t rspLen = 0;
4,399,340✔
780
        (void)mndValidateStbInfo(pMnode, kv->value, kv->valueLen / sizeof(SSTableVersion), &rspMsg, &rspLen);
4,399,340✔
781
        if (rspMsg && rspLen > 0) {
4,399,340✔
782
          SKv kv1 = {.key = HEARTBEAT_KEY_STBINFO, .valueLen = rspLen, .value = rspMsg};
4,399,340✔
783
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
8,798,680✔
784
            mError("failed to put kv into array, but continue at this heartbeat");
×
785
          }
786
        }
787
        break;
4,399,340✔
788
      }
789
#ifdef TD_ENTERPRISE
790
      case HEARTBEAT_KEY_DYN_VIEW: {
5,250✔
791
        break;
5,250✔
792
      }
793
      case HEARTBEAT_KEY_VIEWINFO: {
5,250✔
794
        if (!needCheck) {
5,250✔
795
          break;
422✔
796
        }
797

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

828
    pIter = taosHashIterate(pHbReq->info, pIter);
28,134,462✔
829
  }
830

831
  if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
35,149,241✔
832
    if (terrno != 0) code = terrno;
×
833
  }
834
  TAOS_RETURN(code);
17,574,214✔
835
}
836

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

842
  SClientHbBatchReq batchReq = {0};
18,993,776✔
843
  if (tDeserializeSClientHbBatchReq(pReq->pCont, pReq->contLen, &batchReq) != 0) {
18,992,512✔
844
    taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
×
845
    code = TSDB_CODE_INVALID_MSG;
×
846
    TAOS_RETURN(code);
×
847
  }
848

849
  SConnPreparedObj obj = {0};
18,993,776✔
850
  obj.totalDnodes = mndGetDnodeSize(pMnode);
18,993,776✔
851
  obj.ipWhiteListVer = batchReq.ipWhiteListVer;
18,993,776✔
852
  TAOS_CHECK_RETURN(mndGetOnlineDnodeNum(pMnode, &obj.onlineDnodes));
18,993,776✔
853
  mndGetMnodeEpSet(pMnode, &obj.epSet);
18,993,776✔
854
  TAOS_CHECK_RETURN(mndCreateQnodeList(pMnode, &obj.pQnodeList, -1));
18,993,776✔
855

856
  SClientHbBatchRsp batchRsp = {0};
18,992,617✔
857
  batchRsp.svrTimestamp = taosGetTimestampSec();
18,992,026✔
858
  batchRsp.rsps = taosArrayInit(0, sizeof(SClientHbRsp));
18,991,985✔
859
  if (batchRsp.rsps == NULL) {
18,991,289✔
860
    TAOS_CHECK_EXIT(terrno);
×
861
  }
862
  batchRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
18,991,289✔
863
  batchRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
18,991,289✔
864
  batchRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
18,991,289✔
865
  tstrncpy(batchRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
18,991,289✔
866
  batchRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
18,993,776✔
867
  batchRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
18,993,776✔
868
  batchRsp.enableAuditDelete = tsEnableAuditDelete;
18,993,776✔
869
  batchRsp.enableAuditSelect = tsEnableAuditSelect;
18,993,776✔
870
  batchRsp.enableAuditInsert = tsEnableAuditInsert;
18,993,776✔
871
  batchRsp.auditLevel = tsAuditLevel;
18,993,776✔
872
  batchRsp.enableStrongPass = tsEnableStrongPassword;
18,993,776✔
873
  batchRsp.sodInitial = (pMnode->sodPhase == TSDB_SOD_PHASE_INITIAL ? 1 : 0);
18,993,776✔
874
  batchRsp.macActive = (pMnode->macActive == MAC_MODE_MANDATORY ? 1 : 0);
18,992,724✔
875

876
  int32_t sz = taosArrayGetSize(batchReq.reqs);
18,989,999✔
877
  for (int i = 0; i < sz; i++) {
41,398,645✔
878
    SClientHbReq *pHbReq = taosArrayGet(batchReq.reqs, i);
22,407,105✔
879
    if (pHbReq->connKey.connType == CONN_TYPE__QUERY || pHbReq->connKey.connType == CONN_TYPE__TMQ) {
22,406,429✔
880
      TAOS_CHECK_EXIT(mndProcessQueryHeartBeat(pMnode, pReq, pHbReq, &batchRsp, &obj));
22,406,004✔
881
    } 
882
  }
883
  taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
18,991,540✔
884

885
  int32_t tlen = tSerializeSClientHbBatchRsp(NULL, 0, &batchRsp);
18,991,808✔
886
  if (tlen < 0) {
18,985,933✔
887
    TAOS_CHECK_EXIT(tlen);
×
888
  }
889
  void *buf = rpcMallocCont(tlen);
18,985,933✔
890
  if (!buf) {
18,989,493✔
891
    TAOS_CHECK_EXIT(terrno);
×
892
  }
893
  tlen = tSerializeSClientHbBatchRsp(buf, tlen, &batchRsp);
18,989,493✔
894
  if (tlen < 0) {
18,988,879✔
895
    rpcFreeCont(buf);
×
896
    TAOS_CHECK_EXIT(tlen);
×
897
  }
898
  pReq->info.rspLen = tlen;
18,988,879✔
899
  pReq->info.rsp = buf;
18,991,916✔
900
_exit:
18,989,928✔
901
  tFreeClientHbBatchRsp(&batchRsp);
902

903
  taosArrayDestroy(obj.pQnodeList);
18,992,373✔
904

905
  TAOS_RETURN(code);
18,990,986✔
906
}
907

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

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

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

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

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

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

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

953
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &killReq.connId, sizeof(uint32_t));
101✔
954
  if (pConn == NULL) {
101✔
955
    mError("connId:%u, failed to kill connection, conn not exist", killReq.connId);
101✔
956
    code = TSDB_CODE_MND_INVALID_CONN_ID;
101✔
957
    TAOS_RETURN(code);
101✔
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) {
11,819✔
995
  SMnode   *pMnode = pReq->info.node;
11,819✔
996
  SSdb     *pSdb = pMnode->pSdb;
11,819✔
997
  int32_t   numOfRows = 0;
11,819✔
998
  int32_t   cols = 0;
11,819✔
999
  int32_t   code = 0;
11,819✔
1000
  SConnObj *pConn = NULL;
11,819✔
1001

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

1008
  while (numOfRows < rows) {
425,936✔
1009
    pConn = mndGetNextConn(pMnode, pShow->pIter);
425,936✔
1010
    if (pConn == NULL) {
425,936✔
1011
      pShow->pIter = NULL;
11,819✔
1012
      break;
11,819✔
1013
    }
1014

1015
    if ((taosGetTimestampMs() - pConn->lastAccessTimeMs) > ((int64_t)CACHE_OBJ_KEEP_TIME * 1000)) {
413,265✔
1016
      continue;
316,415✔
1017
    }
1018

1019
    cols = 0;
97,276✔
1020

1021
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
97,276✔
1022
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->id, false);
97,276✔
1023
    if (code != 0) {
97,276✔
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};
97,276✔
1029
    STR_TO_VARSTR(user, pConn->user);
97,276✔
1030
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
97,276✔
1031
    code = colDataSetVal(pColInfo, numOfRows, (const char *)user, false);
97,276✔
1032
    if (code != 0) {
97,276✔
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];
97,074✔
1038
    STR_TO_VARSTR(app, pConn->app);
97,276✔
1039
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
97,276✔
1040
    code = colDataSetVal(pColInfo, numOfRows, (const char *)app, false);
97,276✔
1041
    if (code != 0) {
97,276✔
1042
      mError("failed to set app since %s", tstrerror(code));
×
1043
      return code;
×
1044
    }
1045

1046
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
97,276✔
1047
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->pid, false);
97,276✔
1048
    if (code != 0) {
97,276✔
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};
97,276✔
1054
    char endpoint[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
97,276✔
1055
    if (snprintf(addr, sizeof(addr), "%s:%d", IP_ADDR_STR(&pConn->addr), pConn->addr.port) >= sizeof(addr)) {
97,276✔
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);
97,276✔
1062

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

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

1077
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
97,276✔
1078
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->lastAccessTimeMs, false);
97,276✔
1079
    if (code != 0) {
97,276✔
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];
97,074✔
1085
    STR_TO_VARSTR(userApp, pConn->userApp);
97,276✔
1086
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
97,276✔
1087
    code = colDataSetVal(pColInfo, numOfRows, (const char *)userApp, false);
97,276✔
1088
    if (code != 0) {
97,276✔
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};
97,276✔
1094
    getUserIpFromConnObj(pConn, userIp);
97,276✔
1095

1096
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
97,276✔
1097
    code = colDataSetVal(pColInfo, numOfRows, (const char *)userIp, false);
97,276✔
1098
    if (code != 0) {
97,276✔
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];
97,074✔
1104
    STR_TO_VARSTR(ver, pConn->sVer);
97,276✔
1105
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
97,276✔
1106
    code = colDataSetVal(pColInfo, numOfRows, (const char *)ver, false);
97,276✔
1107
    if (code != 0) {
97,276✔
1108
      mError("failed to set ver since %s", tstrerror(code));
×
1109
      return code;
×
1110
    }
1111

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

1121
    char type[16 + VARSTR_HEADER_SIZE];
97,074✔
1122
    STR_TO_VARSTR(type, pConn->connType == CONN_TYPE__QUERY ? "QUERY" : (pConn->connType == CONN_TYPE__TMQ ? "TMQ" : "UNKNOWN"));
97,276✔
1123
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
97,276✔
1124
    code = colDataSetVal(pColInfo, numOfRows, (const char *)type, false);
97,276✔
1125
    if (code != 0) {
97,276✔
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};
97,276✔
1131
    STR_TO_VARSTR(tokenName, pConn->tokenName);
97,276✔
1132
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
97,276✔
1133
    code = colDataSetVal(pColInfo, numOfRows, (const char *)tokenName, false);
97,276✔
1134
    if (code != 0) {
97,276✔
1135
      mError("failed to set token name since %s", tstrerror(code));
×
1136
      return code;
×
1137
    }
1138

1139
    numOfRows++;
97,276✔
1140
  }
1141

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

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

1170
    char queryId[26 + VARSTR_HEADER_SIZE] = {0};
16,415✔
1171
    (void)snprintf(&queryId[VARSTR_HEADER_SIZE], sizeof(queryId) - VARSTR_HEADER_SIZE, "%x:%" PRIx64, pConn->id,
16,415✔
1172
                   pQuery->reqRid);
1173
    varDataLen(queryId) = strlen(&queryId[VARSTR_HEADER_SIZE]);
16,415✔
1174
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,415✔
1175
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)queryId, false);
16,415✔
1176
    if (code != 0) {
16,415✔
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++);
16,415✔
1183
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->queryId, false);
16,415✔
1184
    if (code != 0) {
16,415✔
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++);
16,415✔
1191
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pConn->id, false);
16,415✔
1192
    if (code != 0) {
16,415✔
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];
16,415✔
1199
    STR_TO_VARSTR(app, pConn->app);
16,415✔
1200
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,415✔
1201
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)app, false);
16,415✔
1202
    if (code != 0) {
16,415✔
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++);
16,415✔
1209
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pConn->pid, false);
16,415✔
1210
    if (code != 0) {
16,415✔
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};
16,415✔
1217
    STR_TO_VARSTR(user, pConn->user);
16,415✔
1218
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,415✔
1219
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)user, false);
16,415✔
1220
    if (code != 0) {
16,415✔
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};
16,415✔
1227
    char buf[IP_RESERVE_CAP] = {0};
16,415✔
1228
    (void)snprintf(buf, sizeof(buf), "%s:%d", IP_ADDR_STR(&pConn->addr), pConn->addr.port);
16,415✔
1229
    STR_TO_VARSTR(endpoint, buf);
16,415✔
1230
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,415✔
1231
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)endpoint, false);
16,415✔
1232
    if (code != 0) {
16,415✔
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++);
16,415✔
1239
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->stime, false);
16,415✔
1240
    if (code != 0) {
16,415✔
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++);
16,415✔
1247
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->useconds, false);
16,415✔
1248
    if (code != 0) {
16,415✔
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++);
16,415✔
1255
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->stableQuery, false);
16,415✔
1256
    if (code != 0) {
16,415✔
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++);
16,415✔
1263
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->isSubQuery, false);
16,415✔
1264
    if (code != 0) {
16,415✔
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++);
16,415✔
1271
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->subPlanNum, false);
16,415✔
1272
    if (code != 0) {
16,415✔
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};
16,415✔
1279
    int64_t reserve = 128;
16,415✔
1280
    int32_t strSize = sizeof(subStatus);
16,415✔
1281
    int32_t offset = VARSTR_HEADER_SIZE;
16,415✔
1282
    for (int32_t i = 0; i < pQuery->subPlanNum && offset + reserve < strSize; ++i) {
32,830✔
1283
      if (i) {
16,415✔
1284
        offset += snprintf(subStatus + offset, sizeof(subStatus) - offset, ",");
×
1285
      }
1286
      if (offset + reserve >= strSize) break;
16,415✔
1287

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

1291
      char startBuf[32] = {0};
16,415✔
1292
      (void)snprintf(startBuf, sizeof(startBuf), "-");
16,415✔
1293
      if (pDesc->startTs > 0) {
16,415✔
1294
        time_t    startSec = (time_t)(pDesc->startTs / 1000000);
16,415✔
1295
        int32_t   startFrac = (int32_t)(pDesc->startTs % 1000000) / 1000;
16,415✔
1296
        struct tm startTm;
16,415✔
1297
        if (taosLocalTime(&startSec, &startTm, NULL, 0, NULL) != NULL) {
16,415✔
1298
          size_t n = taosStrfTime(startBuf, sizeof(startBuf), "%Y-%m-%d %H:%M:%S", &startTm);
16,415✔
1299
          if (tsnprintf(startBuf + n, sizeof(startBuf) - n, ".%03d", startFrac) < 0) {
16,415✔
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,
16,415✔
1309
                          "%" PRIu64 ":%s:%s", pDesc->tid, pDesc->status, startBuf);
16,415✔
1310
    }
1311
    varDataLen(subStatus) = strlen(&subStatus[VARSTR_HEADER_SIZE]);
16,415✔
1312
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,415✔
1313
    code = colDataSetVal(pColInfo, curRowIndex, subStatus, (varDataLen(subStatus) == 0) ? true : false);
16,415✔
1314
    if (code != 0) {
16,415✔
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};
16,415✔
1321
    STR_TO_VARSTR(sql, pQuery->sql);
16,415✔
1322
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,415✔
1323
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)sql, false);
16,415✔
1324
    if (code != 0) {
16,415✔
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];
16,415✔
1331
    STR_TO_VARSTR(userApp, pConn->userApp);
16,415✔
1332
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,415✔
1333
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)userApp, false);
16,415✔
1334
    if (code != 0) {
16,415✔
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};
16,415✔
1341
    getUserIpFromConnObj(pConn, userIp);
16,415✔
1342

1343
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,415✔
1344
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)userIp, false);
16,415✔
1345
    if (code != 0) {
16,415✔
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);
16,415✔
1352
    char        phaseVarStr[64 + VARSTR_HEADER_SIZE];
16,415✔
1353
    STR_TO_VARSTR(phaseVarStr, phaseStr);
16,415✔
1354
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,415✔
1355
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)phaseVarStr, false);
16,415✔
1356
    if (code != 0) {
16,415✔
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++);
16,415✔
1363
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->phaseStartTime, false);
16,415✔
1364
    if (code != 0) {
16,415✔
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++;
16,415✔
1371
  }
1372

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

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

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

1389
  // means fetched some data last time for this conn
1390
  if (pShow->curIterPackedRows > 0) {
15,578✔
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) {
59,765✔
1400
    pConn = mndGetNextConn(pMnode, pShow->pIter);
59,765✔
1401
    if (pConn == NULL) {
59,765✔
1402
      pShow->pIter = NULL;
15,578✔
1403
      break;
15,578✔
1404
    }
1405

1406
    int32_t packedRows = packQueriesIntoBlock(pShow, pConn, pBlock, 0, rows - numOfRows);
44,187✔
1407
    pShow->curIterPackedRows = packedRows;
44,187✔
1408
    numOfRows += packedRows;
44,187✔
1409
  }
1410
  pShow->numOfRows += numOfRows;
15,578✔
1411
  return numOfRows;
15,578✔
1412
}
1413

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

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

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

1435
    cols = 0;
×
1436

1437
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1438
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->appId, false);
×
1439
    if (code != 0) {
×
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};
×
1445
    char buf[IP_RESERVE_CAP] = {0};
×
1446
    snprintf(buf, sizeof(buf), "%s", IP_ADDR_STR(&pApp->cliAddr));
×
1447
    STR_TO_VARSTR(ip, buf);
×
1448

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

1456
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1457
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->pid, false);
×
1458
    if (code != 0) {
×
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};
×
1464
    (void)snprintf(&name[VARSTR_HEADER_SIZE], sizeof(name) - VARSTR_HEADER_SIZE, "%s", pApp->name);
×
1465
    varDataLen(name) = strlen(&name[VARSTR_HEADER_SIZE]);
×
1466
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1467
    code = colDataSetVal(pColInfo, numOfRows, (const char *)name, false);
×
1468
    if (code != 0) {
×
1469
      mError("failed to set app name since %s", tstrerror(code));
×
1470
      return code;
×
1471
    }
1472

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

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

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

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

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

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

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

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

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

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

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

1550
    numOfRows++;
×
1551
  }
1552

1553
  pShow->numOfRows += numOfRows;
×
1554
  return numOfRows;
×
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) {
×
1564
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
×
1565
  return taosCacheGetNumOfObj(pMgmt->connCache);
×
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