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

taosdata / TDengine / #4897

25 Dec 2025 10:17AM UTC coverage: 65.717% (-0.2%) from 65.929%
#4897

push

travis-ci

web-flow
fix: [6622889291] Fix invalid rowSize. (#34043)

186011 of 283047 relevant lines covered (65.72%)

113853896.64 hits per line

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

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

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

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

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

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

76
#define CACHE_OBJ_KEEP_TIME 3  // s
77

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

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

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

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

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

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

128
  TAOS_RETURN(code);
513,503✔
129
}
130

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

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

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

151
  if (pConn->userDualIp.ipv4[0] != 0 && strncmp(pConn->userDualIp.ipv4, none, strlen(none)) != 0) {
57,030✔
152
    char   *ipstr = IP_ADDR_STR(&pConn->userDualIp);
×
153
    int32_t len = strlen(ipstr);
×
154
    memcpy(varDataVal(dst), ipstr, len);
×
155
    varDataLen(dst) = len;
×
156
  }
157
  return;
57,030✔
158
}
159
static void setUserInfo2Conn(SConnObj *connObj, char *userApp, uint32_t userIp, char *cInfo) {
23,401,619✔
160
  if (connObj == NULL) {
23,401,619✔
161
    return;
×
162
  }
163
  tstrncpy(connObj->userApp, userApp, sizeof(connObj->userApp));
23,401,619✔
164
  tstrncpy(connObj->cInfo, cInfo, sizeof(connObj->cInfo));
23,400,528✔
165
  connObj->userIp = userIp;
23,400,815✔
166
}
167
static void setUserInfoIpToConn(SConnObj *connObj, SIpRange *pRange) {
23,400,639✔
168
  int32_t code = 0;
23,400,639✔
169
  if (connObj == NULL) {
23,400,639✔
170
    return;
×
171
  }
172

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

180

181

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

186
  char     connStr[255] = {0};
3,093,247✔
187
  char    *ip = IP_ADDR_STR(pAddr);
3,093,486✔
188
  uint16_t port = pAddr->port;
3,093,486✔
189

190
  int32_t  len = tsnprintf(connStr, sizeof(connStr), "%s%d%d%d%s", user, ip, port, pid, app);
3,093,486✔
191
  uint32_t connId = mndGenerateUid(connStr, len);
3,092,864✔
192
  if (startTime == 0) startTime = taosGetTimestampMs();
3,620,168✔
193

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

208
  connObj.lastAccessTimeMs = connObj.loginTimeMs;
3,093,247✔
209
  tstrncpy(connObj.user, user, sizeof(connObj.user));
3,093,247✔
210
  tstrncpy(connObj.tokenName, tokenName, sizeof(connObj.tokenName));
3,093,247✔
211
  tstrncpy(connObj.app, app, sizeof(connObj.app));
3,093,247✔
212
  tstrncpy(connObj.sVer, sVer, sizeof(connObj.sVer));
3,093,247✔
213

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

226

227

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

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

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

239
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &connId, sizeof(connId));
23,400,321✔
240
  if (pConn == NULL) {
23,402,258✔
241
    mDebug("conn:%u, already destroyed", connId);
526,682✔
242
    return NULL;
526,682✔
243
  }
244

245
  pConn->lastAccessTimeMs = taosGetTimestampMs();
22,874,935✔
246
  mTrace("conn:%u, acquired from cache, data:%p", pConn->id, pConn);
22,874,927✔
247
  return pConn;
22,871,437✔
248
}
249

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

254
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
26,827,337✔
255
  if (extendLifespan) taosCacheTryExtendLifeSpan(pMgmt->connCache, (void **)&pConn);
26,826,854✔
256
  taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
26,825,416✔
257
}
258

259
void *mndGetNextConn(SMnode *pMnode, SCacheIter *pIter) {
1,188,667✔
260
  SConnObj *pConn = NULL;
1,188,667✔
261
  bool      hasNext = taosCacheIterNext(pIter);
1,188,667✔
262
  if (hasNext) {
1,188,667✔
263
    size_t dataLen = 0;
1,088,787✔
264
    pConn = taosCacheIterGetData(pIter, &dataLen);
1,088,787✔
265
  } else {
266
    taosCacheDestroyIter(pIter);
99,880✔
267
  }
268

269
  return pConn;
1,188,667✔
270
}
271

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

278

279

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

289
  int32_t    count = 0;
76,312✔
290
  SConnObj  *pConn = NULL;
76,312✔
291
  while ((pConn = mndGetNextConn(pMnode, pIter)) != NULL) {
934,477✔
292
    if (strncmp(pConn->user, user, TSDB_USER_LEN) == 0) {
858,165✔
293
      count++;
160,776✔
294
    }
295
    mndReleaseConn(pMnode, pConn, true);
858,165✔
296
  }
297

298
  return count;
76,312✔
299
}
300

301

302

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

306
  const char* currPass = pUser->passwords[0].pass;
2,567,566✔
307
  char pass[TSDB_PASSWORD_LEN] = {0};
2,567,960✔
308
  (void)memcpy(pass, inputPass, TSDB_PASSWORD_LEN);
2,567,554✔
309
  pass[TSDB_PASSWORD_LEN - 1] = 0;
2,567,460✔
310

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

321
  // constant time comparison to prevent timing attack
322
  volatile uint8_t res = 0;
2,567,804✔
323
  for (size_t i = 0; i < sizeof(pass) - 1; i++) {
82,124,969✔
324
    res |= pass[i] ^ currPass[i];
79,556,751✔
325
  }
326

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

330

331

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

339

340

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

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

358
  TAOS_CHECK_GOTO(tDeserializeSConnectReq(pReq->pCont, pReq->contLen, &connReq), &lino, _OVER);
2,569,718✔
359
  TAOS_CHECK_GOTO(taosCheckVersionCompatibleFromStr(connReq.sVer, td_version, 3), &lino, _OVER);
2,569,600✔
360

361
  if (connReq.token[0] != 0) {
2,569,968✔
362
    if (mndGetCachedTokenInfo(connReq.token, &ti) == NULL) {
×
363
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOKEN_NOT_EXIST, &lino, _OVER);
×
364
    }
365

366
    if (ti.enabled == 0) {
×
367
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOKEN_DISABLED, &lino, _OVER);
×
368
    }
369

370
    if (ti.expireTime > 0 && now > (ti.expireTime + TSDB_TOKEN_EXPIRY_LEEWAY)) {
×
371
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOKEN_EXPIRED, &lino, _OVER);
×
372
    }
373

374
    user = ti.user;
×
375
    viaToken = true;
×
376
  }
377

378
  TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, user, RPC_MSG_TOKEN(pReq), MND_OPER_CONNECT), &lino, _OVER);
2,569,968✔
379
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, user, &pUser), &lino, _OVER);
2,568,218✔
380

381
  if ((!viaToken) && pUser->passwordLifeTime > 0 && pUser->passwordGraceTime >= 0) {
2,568,218✔
382
    int32_t age = now - pUser->passwords[0].setTime;
76,312✔
383
    int32_t maxLifeTime = pUser->passwordLifeTime + pUser->passwordGraceTime;
76,312✔
384
    if (age >= maxLifeTime) {
76,312✔
385
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_PASSWORD_EXPIRED, &lino, _OVER);
×
386
    }
387
  }
388

389
  if (!isTimeInDateTimeWhiteList(pUser->pTimeWhiteList, now)) {
2,568,218✔
390
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_DISABLED, &lino, _OVER);
×
391
  }
392

393
  SLoginInfo li = {0};
2,567,554✔
394
  mndGetUserLoginInfo(user, &li);
2,567,812✔
395
  if (pUser->inactiveAccountTime >= 0 && (now - li.lastLoginTime >= pUser->inactiveAccountTime)) {
2,567,329✔
396
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_DISABLED, &lino, _OVER);
×
397
  }
398

399
  if ((!viaToken) && pUser->failedLoginAttempts >= 0 & li.failedLoginCount >= pUser->failedLoginAttempts) {
2,567,812✔
400
    if(pUser->passwordLockTime < 0 || now - li.lastFailedLoginTime < pUser->passwordLockTime) {
×
401
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_DISABLED, &lino, _OVER);
×
402
    }
403
  }
404

405
  if (pUser->sessionPerUser >= 0) {
2,567,343✔
406
    int32_t currentSessions = mndCountUserConns(pMnode, user);
76,312✔
407
    if (currentSessions >= pUser->sessionPerUser) {
76,312✔
408
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_CONNECTIONS, &lino, _OVER);
×
409
    }
410
  }
411

412
  if (viaToken || tsMndSkipGrant) {
2,567,577✔
413
    li.lastLoginTime= now;
631✔
414
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
631✔
415
      mndSetUserLoginInfo(user, &li);
×
416
    }
417
  } else if (!verifyTotp(pUser, connReq.totpCode)) {
2,566,961✔
418
    TAOS_CHECK_GOTO(TSDB_CODE_MND_WRONG_TOTP_CODE, &lino, _OVER);
×
419
  } else if ((code = verifyPassword(pUser, connReq.passwd)) == TSDB_CODE_SUCCESS) {
2,568,218✔
420
    li.failedLoginCount = 0;
2,566,968✔
421
    li.lastLoginTime= now;
2,566,968✔
422
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
2,566,968✔
423
      mndSetUserLoginInfo(user, &li);
2,566,812✔
424
    }
425
  } else if (code == TSDB_CODE_MND_AUTH_FAILURE) {
1,250✔
426
    if (pUser->failedLoginAttempts >= 0) {
1,250✔
427
      if (li.failedLoginCount >= pUser->failedLoginAttempts) {
934✔
428
        // if we can get here, it means the lock time has passed, so reset the counter
429
        li.failedLoginCount = 0;
×
430
      }
431
      li.failedLoginCount++;
934✔
432
      li.lastFailedLoginTime = now;
934✔
433
    }
434
    if (connReq.connType != CONN_TYPE__AUTH_TEST) {
1,250✔
435
      mndSetUserLoginInfo(user, &li);
1,250✔
436
    }
437
    goto _OVER;
1,250✔
438
  } else {
439
    goto _OVER;
×
440
  } 
441

442
  if (connReq.db[0] != 0) {
2,567,124✔
443
    char db[TSDB_DB_FNAME_LEN] = {0};
1,105,305✔
444
    (void)snprintf(db, TSDB_DB_FNAME_LEN, "%d%s%s", pUser->acctId, TS_PATH_DELIMITER, connReq.db);
1,105,305✔
445
    pDb = mndAcquireDb(pMnode, db);
1,105,305✔
446
    if (pDb == NULL) {
1,105,066✔
447
      if (0 != strcmp(connReq.db, TSDB_INFORMATION_SCHEMA_DB) && (0 != strcmp(connReq.db, TSDB_PERFORMANCE_SCHEMA_DB))) {
1,596✔
448
        TAOS_CHECK_GOTO(TSDB_CODE_MND_DB_NOT_EXIST, &lino, _OVER);
164✔
449
      }
450
    }
451

452
    TAOS_CHECK_GOTO(mndCheckDbPrivilege(pMnode, user, RPC_MSG_TOKEN(pReq), MND_OPER_READ_OR_WRITE_DB, pDb), &lino, _OVER);
1,104,902✔
453
  }
454

455
  if (connReq.connType == CONN_TYPE__AUTH_TEST) {
2,566,804✔
456
    code = 0;
×
457
    goto _OVER;
×
458
  }
459

460
  pConn = mndCreateConn(pMnode, user, ti.name, connReq.connType, &pReq->info.conn.cliAddr, connReq.pid, connReq.app,
2,566,804✔
461
                        connReq.startTime, connReq.sVer);
462
  if (pConn == NULL) {
2,566,308✔
463
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
464
  }
465

466
  SConnectRsp connectRsp = {0};
2,566,308✔
467
  connectRsp.acctId = pUser->acctId;
2,566,565✔
468
  connectRsp.superUser = pUser->superUser;
2,566,083✔
469
  connectRsp.sysInfo = pUser->sysInfo;
2,566,083✔
470
  connectRsp.clusterId = pMnode->clusterId;
2,566,083✔
471
  connectRsp.connId = pConn->id;
2,566,308✔
472
  connectRsp.connType = connReq.connType;
2,566,565✔
473
  connectRsp.dnodeNum = mndGetDnodeSize(pMnode);
2,566,565✔
474
  connectRsp.svrTimestamp = taosGetTimestampSec();
2,566,804✔
475
  connectRsp.passVer = pUser->passVersion;
2,566,340✔
476
  connectRsp.authVer = pUser->authVersion;
2,566,565✔
477
  connectRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
2,566,804✔
478
  connectRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
2,566,804✔
479
  connectRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
2,566,804✔
480
  connectRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
2,566,804✔
481
  connectRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
2,566,804✔
482
  connectRsp.enableAuditDelete = tsEnableAuditDelete;
2,566,804✔
483
  connectRsp.enableAuditSelect = tsEnableAuditSelect;
2,566,804✔
484
  connectRsp.enableAuditInsert = tsEnableAuditInsert;
2,566,804✔
485
  connectRsp.auditLevel = tsAuditLevel;
2,566,804✔
486
  tstrncpy(connectRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
2,566,804✔
487
  connectRsp.whiteListVer = pUser->ipWhiteListVer;
2,566,804✔
488
  connectRsp.timeWhiteListVer = pUser->timeWhiteListVer;
2,566,565✔
489

490
  tstrncpy(connectRsp.sVer, td_version, sizeof(connectRsp.sVer));
2,566,371✔
491
  tstrncpy(connectRsp.user, user, sizeof(connectRsp.user));
2,566,371✔
492
  tstrncpy(connectRsp.tokenName, ti.name, sizeof(connectRsp.tokenName));
2,566,371✔
493
  (void)snprintf(connectRsp.sDetailVer, sizeof(connectRsp.sDetailVer), "ver:%s\nbuild:%s\ngitinfo:%s", td_version,
2,566,371✔
494
                 td_buildinfo, td_gitinfo);
495
  mndGetMnodeEpSet(pMnode, &connectRsp.epSet);
2,566,371✔
496

497
  int32_t contLen = tSerializeSConnectRsp(NULL, 0, &connectRsp);
2,566,804✔
498
  if (contLen < 0) {
2,565,976✔
499
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
500
  }
501
  void *pRsp = rpcMallocCont(contLen);
2,565,976✔
502
  if (pRsp == NULL) {
2,565,695✔
503
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
504
  }
505

506
  contLen = tSerializeSConnectRsp(pRsp, contLen, &connectRsp);
2,565,695✔
507
  if (contLen < 0) {
2,566,308✔
508
    rpcFreeCont(pRsp);
×
509
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
510
  }
511

512
  pReq->info.rspLen = contLen;
2,566,308✔
513
  pReq->info.rsp = pRsp;
2,566,554✔
514

515
  mGDebug("user:%s, login from %s:%d, conn:%u, app:%s, db:%s", user, ip, port, pConn->id, connReq.app, connReq.db);
2,565,675✔
516
  code = 0;
2,566,804✔
517

518
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
2,566,804✔
519
    char    detail[1000] = {0};
2,566,804✔
520
    int32_t nBytes = snprintf(detail, sizeof(detail), "app:%s", connReq.app);
2,566,804✔
521
    if ((uint32_t)nBytes < sizeof(detail)) {
2,566,804✔
522
      double duration = (taosGetTimestampMs() - tss) / 1000.0;
2,566,804✔
523
      auditRecord(pReq, pMnode->clusterId, "login", "", "", detail, strlen(detail), duration, 0);
2,566,804✔
524
    } else {
525
      mError("failed to audit logic since %s", tstrerror(TSDB_CODE_OUT_OF_RANGE));
×
526
    }
527
  }
528

529
_OVER:
2,566,836✔
530
  if (code != 0) {
2,570,226✔
531
    mGError("user:%s, failed to login from %s since %s, line:%d, db:%s", user, ip, tstrerror(code), lino, connReq.db);
3,422✔
532
  }
533

534
  mndReleaseUser(pMnode, pUser);
2,570,226✔
535
  mndReleaseDb(pMnode, pDb);
2,570,226✔
536
  mndReleaseConn(pMnode, pConn, true);
2,570,226✔
537

538
  TAOS_RETURN(code);
2,570,226✔
539
}
540

541

542

543
static int32_t mndSaveQueryList(SConnObj *pConn, SQueryHbReqBasic *pBasic) {
23,399,168✔
544
  taosWLockLatch(&pConn->queryLock);
23,399,168✔
545

546
  taosArrayDestroyEx(pConn->pQueries, tFreeClientHbQueryDesc);
23,401,453✔
547

548
  pConn->pQueries = pBasic->queryDesc;
23,400,272✔
549
  pConn->numOfQueries = pBasic->queryDesc ? taosArrayGetSize(pBasic->queryDesc) : 0;
23,400,953✔
550
  pBasic->queryDesc = NULL;
23,401,275✔
551

552
  mDebug("queries updated in conn %u, num:%d", pConn->id, pConn->numOfQueries);
23,401,451✔
553

554
  taosWUnLockLatch(&pConn->queryLock);
23,403,063✔
555

556
  return TSDB_CODE_SUCCESS;
23,401,994✔
557
}
558

559
static SAppObj *mndCreateApp(SMnode *pMnode, const SIpAddr *pAddr, const SAppHbReq *pReq) {
652,646✔
560
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
652,646✔
561

562
  SAppObj app;
652,339✔
563
  app.appId = pReq->appId;
652,646✔
564
  app.cliAddr = *pAddr;
652,646✔
565
  app.pid = pReq->pid;
652,646✔
566
  tstrncpy(app.name, pReq->name, sizeof(app.name));
652,646✔
567
  app.startTime = pReq->startTime;
652,646✔
568
  (void)memcpy(&app.summary, &pReq->summary, sizeof(pReq->summary));
652,646✔
569
  app.lastAccessTimeMs = taosGetTimestampMs();
652,646✔
570

571
  SAppObj *pApp =
572
      taosCachePut(pMgmt->appCache, &pReq->appId, sizeof(pReq->appId), &app, sizeof(app), CACHE_OBJ_KEEP_TIME * 1000);
652,646✔
573
  if (pApp == NULL) {
652,646✔
574
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
575
    mError("failed to app %" PRIx64 " into cache since %s", pReq->appId, terrstr());
×
576
    return NULL;
×
577
  }
578

579
  mTrace("app %" PRIx64 " is put into cache", pReq->appId);
652,646✔
580
  return pApp;
652,646✔
581
}
582

583
static void mndFreeApp(SAppObj *pApp) { mTrace("app %" PRIx64 " is destroyed", pApp->appId); }
652,646✔
584

585
static SAppObj *mndAcquireApp(SMnode *pMnode, int64_t appId) {
23,402,169✔
586
  terrno = 0;
23,402,169✔
587
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
23,401,716✔
588

589
  SAppObj *pApp = taosCacheAcquireByKey(pMgmt->appCache, &appId, sizeof(appId));
23,401,893✔
590
  if (pApp == NULL) {
23,400,100✔
591
    mDebug("app %" PRIx64 " not in cache", appId);
652,646✔
592
    return NULL;
652,646✔
593
  }
594

595
  pApp->lastAccessTimeMs = (uint64_t)taosGetTimestampMs();
22,747,718✔
596

597
  mTrace("app %" PRIx64 " acquired from cache", appId);
22,747,730✔
598
  return pApp;
22,748,972✔
599
}
600

601
static void mndReleaseApp(SMnode *pMnode, SAppObj *pApp) {
23,400,251✔
602
  if (pApp == NULL) return;
23,400,251✔
603
  mTrace("release app %" PRIx64 " to cache", pApp->appId);
23,400,251✔
604

605
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
23,400,251✔
606
  taosCacheRelease(pMgmt->appCache, (void **)&pApp, false);
23,399,625✔
607
}
608

609
SAppObj *mndGetNextApp(SMnode *pMnode, SCacheIter *pIter) {
12,204✔
610
  SAppObj *pApp = NULL;
12,204✔
611
  bool     hasNext = taosCacheIterNext(pIter);
12,204✔
612
  if (hasNext) {
12,204✔
613
    size_t dataLen = 0;
6,102✔
614
    pApp = taosCacheIterGetData(pIter, &dataLen);
6,102✔
615
  } else {
616
    taosCacheDestroyIter(pIter);
6,102✔
617
  }
618

619
  return pApp;
12,204✔
620
}
621

622
static void mndCancelGetNextApp(SMnode *pMnode, void *pIter) {
×
623
  if (pIter != NULL) {
×
624
    taosCacheDestroyIter(pIter);
×
625
  }
626
}
×
627

628
static SClientHbRsp *mndMqHbBuildRsp(SMnode *pMnode, SClientHbReq *pReq) {
×
629
  //
630
  return NULL;
×
631
}
632

633
static int32_t mndUpdateAppInfo(SMnode *pMnode, SClientHbReq *pHbReq, const SRpcConnInfo *connInfo) {
23,402,179✔
634
  int32_t    code = 0;
23,402,179✔
635
  SAppHbReq *pReq = &pHbReq->app;
23,402,179✔
636
  SAppObj   *pApp = mndAcquireApp(pMnode, pReq->appId);
23,401,755✔
637
  if (pApp == NULL) {
23,398,360✔
638
    pApp = mndCreateApp(pMnode, &connInfo->cliAddr, pReq);
652,646✔
639
    if (pApp == NULL) {
652,646✔
640
      mError("failed to create new app %" PRIx64 " since %s", pReq->appId, terrstr());
×
641
      code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
642
      if (terrno != 0) code = terrno;
×
643
      TAOS_RETURN(code);
×
644
    } else {
645
      mDebug("a new app %" PRIx64 " is created", pReq->appId);
652,646✔
646
      mndReleaseApp(pMnode, pApp);
652,646✔
647
      return TSDB_CODE_SUCCESS;
652,646✔
648
    }
649
  }
650

651
  (void)memcpy(&pApp->summary, &pReq->summary, sizeof(pReq->summary));
22,745,714✔
652

653
  mndReleaseApp(pMnode, pApp);
22,746,796✔
654

655
  return TSDB_CODE_SUCCESS;
22,744,676✔
656
}
657

658
static int32_t mndGetOnlineDnodeNum(SMnode *pMnode, int32_t *num) {
18,929,009✔
659
  SSdb      *pSdb = pMnode->pSdb;
18,929,009✔
660
  SDnodeObj *pDnode = NULL;
18,929,324✔
661
  int64_t    curMs = taosGetTimestampMs();
18,928,537✔
662
  void      *pIter = NULL;
18,928,537✔
663

664
  while (true) {
35,411,308✔
665
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
54,339,845✔
666
    if (pIter == NULL) break;
54,340,073✔
667

668
    bool online = mndIsDnodeOnline(pDnode, curMs);
35,410,383✔
669
    if (online) {
35,405,353✔
670
      (*num)++;
33,785,748✔
671
    }
672

673
    sdbRelease(pSdb, pDnode);
35,408,515✔
674
  }
675

676
  return TSDB_CODE_SUCCESS;
18,929,690✔
677
}
678

679
static int32_t mndProcessQueryHeartBeat(SMnode *pMnode, SRpcMsg *pMsg, SClientHbReq *pHbReq,
23,402,197✔
680
                                        SClientHbBatchRsp *pBatchRsp, SConnPreparedObj *pObj) {
681
  int32_t       code = 0;
23,402,197✔
682
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
23,402,197✔
683
  SClientHbRsp  hbRsp = {.connKey = pHbReq->connKey, .status = 0, .info = NULL, .query = NULL};
23,401,521✔
684

685
  if (0 != pHbReq->app.appId) {
23,402,017✔
686
    TAOS_CHECK_RETURN(mndUpdateAppInfo(pMnode, pHbReq, &pMsg->info.conn));
23,401,168✔
687
  }
688

689
  if (pHbReq->query) {
23,396,531✔
690
    SQueryHbReqBasic *pBasic = pHbReq->query;
23,400,208✔
691
    SConnObj *pConn = mndAcquireConn(pMnode, pBasic->connId);
23,400,264✔
692
    if (pConn == NULL) {
23,398,910✔
693
      SRpcConnInfo  connInfo = pMsg->info.conn;
526,682✔
694
      const char* user = pHbReq->user;
526,682✔
695
      pConn = mndCreateConn(pMnode, user, pHbReq->tokenName, CONN_TYPE__QUERY, &connInfo.cliAddr, pHbReq->app.pid,
526,682✔
696
                            pHbReq->app.name, 0, pHbReq->sVer);
526,682✔
697
      if (pConn == NULL) {
526,682✔
698
        mError("user:%s, conn:%u is freed and failed to create new since %s", user, pBasic->connId, terrstr());
×
699
        code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
700
        if (terrno != 0) code = terrno;
×
701
        TAOS_RETURN(code);
×
702
      } else {
703
        mDebug("user:%s, conn:%u is freed, will create a new conn:%u", user, pBasic->connId, pConn->id);
526,682✔
704
      }
705
    }
706

707
    setUserInfo2Conn(pConn, pHbReq->userApp, pHbReq->userIp, pHbReq->cInfo);
23,398,910✔
708
    setUserInfoIpToConn(pConn, &pHbReq->userDualIp);
23,398,663✔
709

710
    SQueryHbRspBasic *rspBasic = taosMemoryCalloc(1, sizeof(SQueryHbRspBasic));
23,398,219✔
711
    if (rspBasic == NULL) {
23,396,253✔
712
      mndReleaseConn(pMnode, pConn, true);
×
713
      code = terrno;
×
714
      mError("user:%s, conn:%u failed to process hb while since %s", pConn->user, pBasic->connId, terrstr());
×
715
      TAOS_RETURN(code);
×
716
    }
717

718
    TAOS_CHECK_RETURN(mndSaveQueryList(pConn, pBasic));
23,396,253✔
719
    if (pConn->killed != 0) {
23,401,994✔
720
      rspBasic->killConnection = 1;
×
721
    }
722

723
    if (pConn->killId != 0) {
23,401,994✔
724
      rspBasic->killRid = pConn->killId;
194✔
725
      pConn->killId = 0;
194✔
726
    }
727

728
    rspBasic->connId = pConn->id;
23,402,182✔
729
    rspBasic->connId = pConn->id;
23,402,368✔
730
    rspBasic->totalDnodes = pObj->totalDnodes;
23,402,182✔
731
    rspBasic->onlineDnodes = pObj->onlineDnodes;
23,402,182✔
732
    rspBasic->epSet = pObj->epSet;
23,402,181✔
733
    rspBasic->pQnodeList = taosArrayDup(pObj->pQnodeList, NULL);
23,402,368✔
734

735
    mndReleaseConn(pMnode, pConn, true);
23,401,995✔
736

737
    hbRsp.query = rspBasic;
23,402,002✔
738
  } else {
739
    mDebug("no query info in hb msg");
×
740
  }
741

742
  int32_t kvNum = taosHashGetSize(pHbReq->info);
23,402,002✔
743
  if (NULL == pHbReq->info || kvNum <= 0) {
23,400,957✔
744
    if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
12,453,431✔
745
      mError("failed to put rsp into array, but continue at this heartbeat");
×
746
    }
747
    return TSDB_CODE_SUCCESS;
6,227,421✔
748
  }
749

750
  hbRsp.info = taosArrayInit(kvNum, sizeof(SKv));
17,174,947✔
751
  if (NULL == hbRsp.info) {
17,174,947✔
752
    mError("taosArrayInit %d rsp kv failed", kvNum);
×
753
    code = terrno;
×
754
    tFreeClientHbRsp(&hbRsp);
755
    TAOS_RETURN(code);
×
756
  }
757

758
#ifdef TD_ENTERPRISE
759
  bool             needCheck = true;
17,174,947✔
760
  int32_t          key = HEARTBEAT_KEY_DYN_VIEW;
17,174,947✔
761
  SDynViewVersion *pDynViewVer = NULL;
17,174,947✔
762
  SKv             *pKv = taosHashGet(pHbReq->info, &key, sizeof(key));
17,174,947✔
763
  if (NULL != pKv) {
17,174,947✔
764
    pDynViewVer = pKv->value;
5,914✔
765
    mTrace("recv view dyn ver, bootTs:%" PRId64 ", ver:%" PRIu64, pDynViewVer->svrBootTs, pDynViewVer->dynViewVer);
5,914✔
766

767
    SDynViewVersion *pRspVer = NULL;
5,914✔
768
    if (0 != (code = mndValidateDynViewVersion(pMnode, pDynViewVer, &needCheck, &pRspVer))) {
5,914✔
769
      TAOS_RETURN(code);
×
770
    }
771

772
    if (needCheck) {
5,914✔
773
      SKv kv1 = {.key = HEARTBEAT_KEY_DYN_VIEW, .valueLen = sizeof(*pDynViewVer), .value = pRspVer};
5,464✔
774
      if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
10,928✔
775
        if (terrno != 0) code = terrno;
×
776
        TAOS_RETURN(code);
×
777
      };
778
      mTrace("need to check view ver, lastest bootTs:%" PRId64 ", ver:%" PRIu64, pRspVer->svrBootTs,
5,464✔
779
             pRspVer->dynViewVer);
780
    }
781
  }
782
#endif
783

784
  void *pIter = taosHashIterate(pHbReq->info, NULL);
17,174,947✔
785
  while (pIter != NULL) {
42,972,092✔
786
    SKv *kv = pIter;
25,797,145✔
787

788
    switch (kv->key) {
25,797,145✔
789
      case HEARTBEAT_KEY_USER_AUTHINFO: {
17,145,199✔
790
        void   *rspMsg = NULL;
17,145,199✔
791
        int32_t rspLen = 0;
17,145,199✔
792
        (void)mndValidateUserAuthInfo(pMnode, kv->value, kv->valueLen / sizeof(SUserAuthVersion), &rspMsg, &rspLen,
17,145,199✔
793
                                      pObj->ipWhiteListVer);
794
        if (rspMsg && rspLen > 0) {
17,145,199✔
795
          SKv kv1 = {.key = HEARTBEAT_KEY_USER_AUTHINFO, .valueLen = rspLen, .value = rspMsg};
681,615✔
796
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
1,363,195✔
797
            mError("failed to put kv into array, but continue at this heartbeat");
×
798
          }
799
        }
800
        break;
17,145,164✔
801
      }
802
      case HEARTBEAT_KEY_DBINFO: {
5,101,154✔
803
        void   *rspMsg = NULL;
5,101,154✔
804
        int32_t rspLen = 0;
5,101,154✔
805
        (void)mndValidateDbInfo(pMnode, kv->value, kv->valueLen / sizeof(SDbCacheInfo), &rspMsg, &rspLen);
5,101,154✔
806
        if (rspMsg && rspLen > 0) {
5,101,154✔
807
          SKv kv1 = {.key = HEARTBEAT_KEY_DBINFO, .valueLen = rspLen, .value = rspMsg};
5,101,154✔
808
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
10,202,308✔
809
            mError("failed to put kv into array, but continue at this heartbeat");
×
810
          }
811
        }
812
        break;
5,101,154✔
813
      }
814
      case HEARTBEAT_KEY_STBINFO: {
3,538,964✔
815
        void   *rspMsg = NULL;
3,538,964✔
816
        int32_t rspLen = 0;
3,538,964✔
817
        (void)mndValidateStbInfo(pMnode, kv->value, kv->valueLen / sizeof(SSTableVersion), &rspMsg, &rspLen);
3,538,964✔
818
        if (rspMsg && rspLen > 0) {
3,538,964✔
819
          SKv kv1 = {.key = HEARTBEAT_KEY_STBINFO, .valueLen = rspLen, .value = rspMsg};
3,538,964✔
820
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
7,077,928✔
821
            mError("failed to put kv into array, but continue at this heartbeat");
×
822
          }
823
        }
824
        break;
3,538,964✔
825
      }
826
#ifdef TD_ENTERPRISE
827
      case HEARTBEAT_KEY_DYN_VIEW: {
5,914✔
828
        break;
5,914✔
829
      }
830
      case HEARTBEAT_KEY_VIEWINFO: {
5,914✔
831
        if (!needCheck) {
5,914✔
832
          break;
450✔
833
        }
834

835
        void   *rspMsg = NULL;
5,464✔
836
        int32_t rspLen = 0;
5,464✔
837
        (void)mndValidateViewInfo(pMnode, kv->value, kv->valueLen / sizeof(SViewVersion), &rspMsg, &rspLen);
5,464✔
838
        if (rspMsg && rspLen > 0) {
5,464✔
839
          SKv kv1 = {.key = HEARTBEAT_KEY_VIEWINFO, .valueLen = rspLen, .value = rspMsg};
5,464✔
840
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
10,928✔
841
            mError("failed to put kv into array, but continue at this heartbeat");
×
842
          }
843
        }
844
        break;
5,464✔
845
      }
846
#endif
847
      case HEARTBEAT_KEY_TSMA: {
×
848
        void   *rspMsg = NULL;
×
849
        int32_t rspLen = 0;
×
850
        (void)mndValidateTSMAInfo(pMnode, kv->value, kv->valueLen / sizeof(STSMAVersion), &rspMsg, &rspLen);
×
851
        if (rspMsg && rspLen > 0) {
×
852
          SKv kv = {.key = HEARTBEAT_KEY_TSMA, .valueLen = rspLen, .value = rspMsg};
×
853
          if (taosArrayPush(hbRsp.info, &kv) == NULL) {
×
854
            mError("failed to put kv into array, but continue at this heartbeat");
×
855
          }
856
        }
857
        break;
×
858
      }
859
      default:
×
860
        mError("invalid kv key:%d", kv->key);
×
861
        hbRsp.status = TSDB_CODE_APP_ERROR;
×
862
        break;
×
863
    }
864

865
    pIter = taosHashIterate(pHbReq->info, pIter);
25,797,145✔
866
  }
867

868
  if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
34,349,894✔
869
    if (terrno != 0) code = terrno;
×
870
  }
871
  TAOS_RETURN(code);
17,174,947✔
872
}
873

874
static int32_t mndProcessHeartBeatReq(SRpcMsg *pReq) {
18,928,472✔
875
  int32_t code = 0;
18,928,472✔
876
  int32_t lino = 0;
18,928,472✔
877
  SMnode *pMnode = pReq->info.node;
18,928,472✔
878

879
  SClientHbBatchReq batchReq = {0};
18,929,009✔
880
  if (tDeserializeSClientHbBatchReq(pReq->pCont, pReq->contLen, &batchReq) != 0) {
18,929,076✔
881
    taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
×
882
    code = TSDB_CODE_INVALID_MSG;
×
883
    TAOS_RETURN(code);
×
884
  }
885

886
  SConnPreparedObj obj = {0};
18,929,617✔
887
  obj.totalDnodes = mndGetDnodeSize(pMnode);
18,929,690✔
888
  obj.ipWhiteListVer = batchReq.ipWhiteListVer;
18,928,431✔
889
  TAOS_CHECK_RETURN(mndGetOnlineDnodeNum(pMnode, &obj.onlineDnodes));
18,928,431✔
890
  mndGetMnodeEpSet(pMnode, &obj.epSet);
18,929,690✔
891
  TAOS_CHECK_RETURN(mndCreateQnodeList(pMnode, &obj.pQnodeList, -1));
18,929,690✔
892

893
  SClientHbBatchRsp batchRsp = {0};
18,928,498✔
894
  batchRsp.svrTimestamp = taosGetTimestampSec();
18,928,523✔
895
  batchRsp.rsps = taosArrayInit(0, sizeof(SClientHbRsp));
18,927,116✔
896
  if (batchRsp.rsps == NULL) {
18,927,408✔
897
    TAOS_CHECK_EXIT(terrno);
×
898
  }
899
  batchRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
18,927,408✔
900
  batchRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
18,927,408✔
901
  batchRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
18,927,408✔
902
  tstrncpy(batchRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
18,927,408✔
903
  batchRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
18,927,408✔
904
  batchRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
18,927,408✔
905
  batchRsp.enableAuditDelete = tsEnableAuditDelete;
18,927,408✔
906
  batchRsp.enableAuditSelect = tsEnableAuditSelect;
18,927,408✔
907
  batchRsp.enableAuditInsert = tsEnableAuditInsert;
18,927,408✔
908
  batchRsp.auditLevel = tsAuditLevel;
18,927,408✔
909
  batchRsp.enableStrongPass = tsEnableStrongPassword;
18,927,408✔
910

911
  int32_t sz = taosArrayGetSize(batchReq.reqs);
18,927,408✔
912
  for (int i = 0; i < sz; i++) {
42,331,063✔
913
    SClientHbReq *pHbReq = taosArrayGet(batchReq.reqs, i);
23,402,197✔
914
    if (pHbReq->connKey.connType == CONN_TYPE__QUERY) {
23,401,589✔
915
      TAOS_CHECK_EXIT(mndProcessQueryHeartBeat(pMnode, pReq, pHbReq, &batchRsp, &obj));
23,402,368✔
916
    } else if (pHbReq->connKey.connType == CONN_TYPE__TMQ) {
×
917
      SClientHbRsp *pRsp = mndMqHbBuildRsp(pMnode, pHbReq);
×
918
      if (pRsp != NULL) {
×
919
        if (taosArrayPush(batchRsp.rsps, pRsp) == NULL) {
×
920
          mError("failed to put kv into array, but continue at this heartbeat");
×
921
        }
922
        taosMemoryFree(pRsp);
×
923
      }
924
    }
925
  }
926
  taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
18,928,866✔
927

928
  int32_t tlen = tSerializeSClientHbBatchRsp(NULL, 0, &batchRsp);
18,928,537✔
929
  if (tlen < 0) {
18,924,884✔
930
    TAOS_CHECK_EXIT(tlen);
×
931
  }
932
  void *buf = rpcMallocCont(tlen);
18,924,884✔
933
  if (!buf) {
18,924,565✔
934
    TAOS_CHECK_EXIT(terrno);
×
935
  }
936
  tlen = tSerializeSClientHbBatchRsp(buf, tlen, &batchRsp);
18,924,565✔
937
  if (tlen < 0) {
18,928,310✔
938
    rpcFreeCont(buf);
×
939
    TAOS_CHECK_EXIT(tlen);
×
940
  }
941
  pReq->info.rspLen = tlen;
18,928,310✔
942
  pReq->info.rsp = buf;
18,928,112✔
943
_exit:
18,927,096✔
944
  tFreeClientHbBatchRsp(&batchRsp);
945

946
  taosArrayDestroy(obj.pQnodeList);
18,927,969✔
947

948
  TAOS_RETURN(code);
18,928,013✔
949
}
950

951
static int32_t mndProcessKillQueryReq(SRpcMsg *pReq) {
194✔
952
  int32_t       code = 0;
194✔
953
  SMnode       *pMnode = pReq->info.node;
194✔
954
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
194✔
955

956
  SKillQueryReq killReq = {0};
194✔
957
  TAOS_CHECK_RETURN(tDeserializeSKillQueryReq(pReq->pCont, pReq->contLen, &killReq));
194✔
958

959
  mInfo("kill query msg is received, queryId:%s", killReq.queryStrId);
194✔
960
  TAOS_CHECK_RETURN(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_KILL_QUERY));
194✔
961
  int32_t  connId = 0;
194✔
962
  uint64_t queryId = 0;
194✔
963
  char    *p = strchr(killReq.queryStrId, ':');
194✔
964
  if (NULL == p) {
194✔
965
    mError("invalid QID:%s", killReq.queryStrId);
×
966
    code = TSDB_CODE_MND_INVALID_QUERY_ID;
×
967
    TAOS_RETURN(code);
×
968
  }
969
  *p = 0;
194✔
970
  connId = taosStr2Int32(killReq.queryStrId, NULL, 16);
194✔
971
  queryId = taosStr2UInt64(p + 1, NULL, 16);
194✔
972

973
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &connId, sizeof(int32_t));
194✔
974
  if (pConn == NULL) {
194✔
975
    mError("connId:%x, failed to kill queryId:%" PRIx64 ", conn not exist", connId, queryId);
×
976
    code = TSDB_CODE_MND_INVALID_CONN_ID;
×
977
    TAOS_RETURN(code);
×
978
  } else {
979
    mInfo("connId:%x, queryId:%" PRIx64 " is killed by user:%s", connId, queryId, RPC_MSG_USER(pReq));
194✔
980
    pConn->killId = queryId;
194✔
981
    taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
194✔
982
    TAOS_RETURN(code);
194✔
983
  }
984
}
985

986
static int32_t mndProcessKillConnReq(SRpcMsg *pReq) {
358✔
987
  int32_t       code = 0;
358✔
988
  SMnode       *pMnode = pReq->info.node;
358✔
989
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
358✔
990

991
  SKillConnReq killReq = {0};
358✔
992
  TAOS_CHECK_RETURN(tDeserializeSKillConnReq(pReq->pCont, pReq->contLen, &killReq));
358✔
993

994
  TAOS_CHECK_RETURN(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_KILL_CONN));
358✔
995

996
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &killReq.connId, sizeof(uint32_t));
×
997
  if (pConn == NULL) {
×
998
    mError("connId:%u, failed to kill connection, conn not exist", killReq.connId);
×
999
    code = TSDB_CODE_MND_INVALID_CONN_ID;
×
1000
    TAOS_RETURN(code);
×
1001
  } else {
1002
    mInfo("connId:%u, is killed by user:%s", killReq.connId, RPC_MSG_USER(pReq));
×
1003
    pConn->killed = 1;
×
1004
    taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
×
1005
    TAOS_RETURN(code);
×
1006
  }
1007
}
1008

1009
static int32_t mndProcessSvrVerReq(SRpcMsg *pReq) {
×
1010
  int32_t       code = 0;
×
1011
  int32_t       lino = 0;
×
1012
  SServerVerRsp rsp = {0};
×
1013
  tstrncpy(rsp.ver, td_version, sizeof(rsp.ver));
×
1014

1015
  int32_t contLen = tSerializeSServerVerRsp(NULL, 0, &rsp);
×
1016
  if (contLen < 0) {
×
1017
    TAOS_CHECK_EXIT(contLen);
×
1018
  }
1019
  void *pRsp = rpcMallocCont(contLen);
×
1020
  if (pRsp == NULL) {
×
1021
    TAOS_CHECK_EXIT(terrno);
×
1022
  }
1023
  contLen = tSerializeSServerVerRsp(pRsp, contLen, &rsp);
×
1024
  if (contLen < 0) {
×
1025
    rpcFreeCont(pRsp);
×
1026
    TAOS_CHECK_EXIT(contLen);
×
1027
  }
1028

1029
  pReq->info.rspLen = contLen;
×
1030
  pReq->info.rsp = pRsp;
×
1031

1032
_exit:
×
1033

1034
  TAOS_RETURN(code);
×
1035
}
1036

1037
static int32_t mndRetrieveConns(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
17,078✔
1038
  SMnode   *pMnode = pReq->info.node;
17,078✔
1039
  SSdb     *pSdb = pMnode->pSdb;
17,078✔
1040
  int32_t   numOfRows = 0;
17,078✔
1041
  int32_t   cols = 0;
17,078✔
1042
  int32_t   code = 0;
17,078✔
1043
  SConnObj *pConn = NULL;
17,078✔
1044

1045
  if (pShow->pIter == NULL) {
17,078✔
1046
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
17,078✔
1047
    pShow->pIter = taosCacheCreateIter(pMgmt->connCache);
17,078✔
1048
    if (!pShow->pIter) return terrno;
17,078✔
1049
  }
1050

1051
  while (numOfRows < rows) {
204,792✔
1052
    pConn = mndGetNextConn(pMnode, pShow->pIter);
204,792✔
1053
    if (pConn == NULL) {
204,792✔
1054
      pShow->pIter = NULL;
17,078✔
1055
      break;
17,078✔
1056
    }
1057

1058
    if ((taosGetTimestampMs() - pConn->lastAccessTimeMs) > ((int64_t)CACHE_OBJ_KEEP_TIME * 1000)) {
187,714✔
1059
      continue;
142,892✔
1060
    }
1061

1062
    cols = 0;
44,822✔
1063

1064
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
44,822✔
1065
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->id, false);
44,822✔
1066
    if (code != 0) {
44,822✔
1067
      mError("failed to set conn id:%u since %s", pConn->id, tstrerror(code));
×
1068
      return code;
×
1069
    }
1070

1071
    char user[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
44,822✔
1072
    STR_TO_VARSTR(user, pConn->user);
44,822✔
1073
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
44,822✔
1074
    code = colDataSetVal(pColInfo, numOfRows, (const char *)user, false);
44,822✔
1075
    if (code != 0) {
44,822✔
1076
      mError("failed to set user since %s", tstrerror(code));
×
1077
      return code;
×
1078
    }
1079

1080
    char tokenName[TSDB_TOKEN_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
44,822✔
1081
    STR_TO_VARSTR(tokenName, pConn->tokenName);
44,822✔
1082
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
44,822✔
1083
    code = colDataSetVal(pColInfo, numOfRows, (const char *)tokenName, false);
44,822✔
1084
    if (code != 0) {
44,822✔
1085
      mError("failed to set token name since %s", tstrerror(code));
×
1086
      return code;
×
1087
    }
1088

1089
    char app[TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE];
44,822✔
1090
    STR_TO_VARSTR(app, pConn->app);
44,822✔
1091
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
44,822✔
1092
    code = colDataSetVal(pColInfo, numOfRows, (const char *)app, false);
44,822✔
1093
    if (code != 0) {
44,822✔
1094
      mError("failed to set app since %s", tstrerror(code));
×
1095
      return code;
×
1096
    }
1097

1098
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
44,822✔
1099
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->pid, false);
44,822✔
1100
    if (code != 0) {
44,822✔
1101
      mError("failed to set conn id:%u since %s", pConn->id, tstrerror(code));
×
1102
      return code;
×
1103
    }
1104

1105
    char addr[IP_RESERVE_CAP] = {0};
44,822✔
1106
    char endpoint[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
44,822✔
1107
    if (tsnprintf(addr, sizeof(addr), "%s:%d", IP_ADDR_STR(&pConn->addr), pConn->addr.port) >= sizeof(addr)) {
44,822✔
1108
      code = TSDB_CODE_OUT_OF_RANGE;
×
1109
      mError("failed to set endpoint since %s", tstrerror(code));
×
1110
      return code;
×
1111
    }
1112

1113
    STR_TO_VARSTR(endpoint, addr);
44,822✔
1114

1115
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
44,822✔
1116
    code = colDataSetVal(pColInfo, numOfRows, (const char *)endpoint, false);
44,822✔
1117
    if (code != 0) {
44,822✔
1118
      mError("failed to set endpoint since %s", tstrerror(code));
×
1119
      return code;
×
1120
    }
1121

1122
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
44,822✔
1123
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->loginTimeMs, false);
44,822✔
1124
    if (code != 0) {
44,822✔
1125
      mError("failed to set login time since %s", tstrerror(code));
×
1126
      return code;
×
1127
    }
1128

1129
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
44,822✔
1130
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->lastAccessTimeMs, false);
44,822✔
1131
    if (code != 0) {
44,822✔
1132
      mError("failed to set last access time since %s", tstrerror(code));
×
1133
      return code;
×
1134
    }
1135

1136
    char userApp[TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE];
44,822✔
1137
    STR_TO_VARSTR(userApp, pConn->userApp);
44,822✔
1138
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
44,822✔
1139
    code = colDataSetVal(pColInfo, numOfRows, (const char *)userApp, false);
44,822✔
1140
    if (code != 0) {
44,822✔
1141
      mError("failed to set user app since %s", tstrerror(code));
×
1142
      return code;
×
1143
    }
1144

1145
    char userIp[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
44,822✔
1146
    getUserIpFromConnObj(pConn, userIp);
44,822✔
1147

1148
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
44,822✔
1149
    code = colDataSetVal(pColInfo, numOfRows, (const char *)userIp, false);
44,822✔
1150
    if (code != 0) {
44,822✔
1151
      mError("failed to set user ip since %s", tstrerror(code));
×
1152
      return code;
×
1153
    }
1154

1155
    char ver[TSDB_VERSION_LEN + VARSTR_HEADER_SIZE];
44,822✔
1156
    STR_TO_VARSTR(ver, pConn->sVer);
44,822✔
1157
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
44,822✔
1158
    code = colDataSetVal(pColInfo, numOfRows, (const char *)ver, false);
44,822✔
1159
    if (code != 0) {
44,822✔
1160
      mError("failed to set ver since %s", tstrerror(code));
×
1161
      return code;
×
1162
    }
1163

1164
    char cInfo[CONNECTOR_INFO_LEN + VARSTR_HEADER_SIZE];
44,822✔
1165
    STR_TO_VARSTR(cInfo, pConn->cInfo);
44,822✔
1166
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
44,822✔
1167
    code = colDataSetVal(pColInfo, numOfRows, (const char *)cInfo, false);
44,822✔
1168
    if (code != 0) {
44,822✔
1169
      mError("failed to set connector info since %s", tstrerror(code));
×
1170
      return code;
×
1171
    }
1172
    numOfRows++;
44,822✔
1173
  }
1174

1175
  pShow->numOfRows += numOfRows;
17,078✔
1176
  return numOfRows;
17,078✔
1177
}
1178

1179
/**
1180
 * @param pConn the conn queries pack from
1181
 * @param[out] pBlock the block data packed into
1182
 * @param offset skip [offset] queries in pConn
1183
 * @param rowsToPack at most rows to pack
1184
 * @return rows packed
1185
 */
1186
static int32_t packQueriesIntoBlock(SShowObj *pShow, SConnObj *pConn, SSDataBlock *pBlock, uint32_t offset,
42,908✔
1187
                                    uint32_t rowsToPack) {
1188
  int32_t cols = 0;
42,908✔
1189
  int32_t code = 0;
42,908✔
1190
  taosRLockLatch(&pConn->queryLock);
42,908✔
1191
  int32_t numOfQueries = taosArrayGetSize(pConn->pQueries);
42,908✔
1192
  if (NULL == pConn->pQueries || numOfQueries <= offset) {
42,908✔
1193
    taosRUnLockLatch(&pConn->queryLock);
30,700✔
1194
    return 0;
30,700✔
1195
  }
1196

1197
  int32_t i = offset;
12,208✔
1198
  for (; i < numOfQueries && (i - offset) < rowsToPack; ++i) {
24,416✔
1199
    int32_t     curRowIndex = pBlock->info.rows;
12,208✔
1200
    SQueryDesc *pQuery = taosArrayGet(pConn->pQueries, i);
12,208✔
1201
    cols = 0;
12,208✔
1202

1203
    char queryId[26 + VARSTR_HEADER_SIZE] = {0};
12,208✔
1204
    (void)tsnprintf(&queryId[VARSTR_HEADER_SIZE], sizeof(queryId) - VARSTR_HEADER_SIZE, "%x:%" PRIx64, pConn->id,
12,208✔
1205
                    pQuery->reqRid);
1206
    varDataLen(queryId) = strlen(&queryId[VARSTR_HEADER_SIZE]);
12,208✔
1207
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,208✔
1208
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)queryId, false);
12,208✔
1209
    if (code != 0) {
12,208✔
1210
      mError("failed to set query id:%s since %s", queryId, tstrerror(code));
×
1211
      taosRUnLockLatch(&pConn->queryLock);
×
1212
      return code;
×
1213
    }
1214

1215
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,208✔
1216
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->queryId, false);
12,208✔
1217
    if (code != 0) {
12,208✔
1218
      mError("failed to set query id:%" PRIx64 " since %s", pQuery->queryId, tstrerror(code));
×
1219
      taosRUnLockLatch(&pConn->queryLock);
×
1220
      return code;
×
1221
    }
1222

1223
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,208✔
1224
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pConn->id, false);
12,208✔
1225
    if (code != 0) {
12,208✔
1226
      mError("failed to set conn id:%u since %s", pConn->id, tstrerror(code));
×
1227
      taosRUnLockLatch(&pConn->queryLock);
×
1228
      return code;
×
1229
    }
1230

1231
    char app[TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE];
12,208✔
1232
    STR_TO_VARSTR(app, pConn->app);
12,208✔
1233
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,208✔
1234
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)app, false);
12,208✔
1235
    if (code != 0) {
12,208✔
1236
      mError("failed to set app since %s", tstrerror(code));
×
1237
      taosRUnLockLatch(&pConn->queryLock);
×
1238
      return code;
×
1239
    }
1240

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

1249
    char user[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
12,208✔
1250
    STR_TO_VARSTR(user, pConn->user);
12,208✔
1251
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,208✔
1252
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)user, false);
12,208✔
1253
    if (code != 0) {
12,208✔
1254
      mError("failed to set user since %s", tstrerror(code));
×
1255
      taosRUnLockLatch(&pConn->queryLock);
×
1256
      return code;
×
1257
    }
1258

1259
    char endpoint[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
12,208✔
1260
    char buf[IP_RESERVE_CAP] = {0};
12,208✔
1261
    (void)tsnprintf(buf, sizeof(buf), "%s:%d", IP_ADDR_STR(&pConn->addr), pConn->addr.port);
12,208✔
1262
    STR_TO_VARSTR(endpoint, buf);
12,208✔
1263
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,208✔
1264
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)endpoint, false);
12,208✔
1265
    if (code != 0) {
12,208✔
1266
      mError("failed to set endpoint since %s", tstrerror(code));
×
1267
      taosRUnLockLatch(&pConn->queryLock);
×
1268
      return code;
×
1269
    }
1270

1271
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,208✔
1272
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->stime, false);
12,208✔
1273
    if (code != 0) {
12,208✔
1274
      mError("failed to set start time since %s", tstrerror(code));
×
1275
      taosRUnLockLatch(&pConn->queryLock);
×
1276
      return code;
×
1277
    }
1278

1279
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,208✔
1280
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->useconds, false);
12,208✔
1281
    if (code != 0) {
12,208✔
1282
      mError("failed to set useconds since %s", tstrerror(code));
×
1283
      taosRUnLockLatch(&pConn->queryLock);
×
1284
      return code;
×
1285
    }
1286

1287
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,208✔
1288
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->stableQuery, false);
12,208✔
1289
    if (code != 0) {
12,208✔
1290
      mError("failed to set stable query since %s", tstrerror(code));
×
1291
      taosRUnLockLatch(&pConn->queryLock);
×
1292
      return code;
×
1293
    }
1294

1295
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,208✔
1296
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->isSubQuery, false);
12,208✔
1297
    if (code != 0) {
12,208✔
1298
      mError("failed to set sub query since %s", tstrerror(code));
×
1299
      taosRUnLockLatch(&pConn->queryLock);
×
1300
      return code;
×
1301
    }
1302

1303
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,208✔
1304
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->subPlanNum, false);
12,208✔
1305
    if (code != 0) {
12,208✔
1306
      mError("failed to set sub plan num since %s", tstrerror(code));
×
1307
      taosRUnLockLatch(&pConn->queryLock);
×
1308
      return code;
×
1309
    }
1310

1311
    char    subStatus[TSDB_SHOW_SUBQUERY_LEN + VARSTR_HEADER_SIZE] = {0};
12,208✔
1312
    int64_t reserve = 64;
12,208✔
1313
    int32_t strSize = sizeof(subStatus);
12,208✔
1314
    int32_t offset = VARSTR_HEADER_SIZE;
12,208✔
1315
    for (int32_t i = 0; i < pQuery->subPlanNum && offset + reserve < strSize; ++i) {
39,422✔
1316
      if (i) {
27,214✔
1317
        offset += tsnprintf(subStatus + offset, sizeof(subStatus) - offset, ",");
15,394✔
1318
      }
1319
      if (offset + reserve < strSize) {
27,214✔
1320
        SQuerySubDesc *pDesc = taosArrayGet(pQuery->subDesc, i);
27,214✔
1321
        offset +=
27,214✔
1322
            tsnprintf(subStatus + offset, sizeof(subStatus) - offset, "%" PRIu64 ":%s", pDesc->tid, pDesc->status);
27,214✔
1323
      } else {
1324
        break;
×
1325
      }
1326
    }
1327
    varDataLen(subStatus) = strlen(&subStatus[VARSTR_HEADER_SIZE]);
12,208✔
1328
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,208✔
1329
    code = colDataSetVal(pColInfo, curRowIndex, subStatus, (varDataLen(subStatus) == 0) ? true : false);
12,208✔
1330
    if (code != 0) {
12,208✔
1331
      mError("failed to set sub status since %s", tstrerror(code));
×
1332
      taosRUnLockLatch(&pConn->queryLock);
×
1333
      return code;
×
1334
    }
1335

1336
    char sql[TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE] = {0};
12,208✔
1337
    STR_TO_VARSTR(sql, pQuery->sql);
12,208✔
1338
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,208✔
1339
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)sql, false);
12,208✔
1340
    if (code != 0) {
12,208✔
1341
      mError("failed to set sql since %s", tstrerror(code));
×
1342
      taosRUnLockLatch(&pConn->queryLock);
×
1343
      return code;
×
1344
    }
1345

1346
    char userApp[TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE];
12,208✔
1347
    STR_TO_VARSTR(userApp, pConn->userApp);
12,208✔
1348
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,208✔
1349
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)userApp, false);
12,208✔
1350
    if (code != 0) {
12,208✔
1351
      mError("failed to set user app since %s", tstrerror(code));
×
1352
      taosRUnLockLatch(&pConn->queryLock);
×
1353
      return code;
×
1354
    }
1355

1356
    char userIp[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
12,208✔
1357
    getUserIpFromConnObj(pConn, userIp);
12,208✔
1358

1359
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,208✔
1360
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)userIp, false);
12,208✔
1361
    if (code != 0) {
12,208✔
1362
      mError("failed to set user ip since %s", tstrerror(code));
×
1363
      taosRUnLockLatch(&pConn->queryLock);
×
1364
      return code;
×
1365
    }
1366

1367
    pBlock->info.rows++;
12,208✔
1368
  }
1369

1370
  taosRUnLockLatch(&pConn->queryLock);
12,208✔
1371
  return i - offset;
12,208✔
1372
}
1373

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

1380
  if (pShow->pIter == NULL) {
6,490✔
1381
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
6,490✔
1382
    pShow->pIter = taosCacheCreateIter(pMgmt->connCache);
6,490✔
1383
    if (!pShow->pIter) return terrno;
6,490✔
1384
  }
1385

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

1396
  while (numOfRows < rows) {
49,398✔
1397
    pConn = mndGetNextConn(pMnode, pShow->pIter);
49,398✔
1398
    if (pConn == NULL) {
49,398✔
1399
      pShow->pIter = NULL;
6,490✔
1400
      break;
6,490✔
1401
    }
1402

1403
    int32_t packedRows = packQueriesIntoBlock(pShow, pConn, pBlock, 0, rows - numOfRows);
42,908✔
1404
    pShow->curIterPackedRows = packedRows;
42,908✔
1405
    numOfRows += packedRows;
42,908✔
1406
  }
1407
  pShow->numOfRows += numOfRows;
6,490✔
1408
  return numOfRows;
6,490✔
1409
}
1410

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

1419
  if (pShow->pIter == NULL) {
6,102✔
1420
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
6,102✔
1421
    pShow->pIter = taosCacheCreateIter(pMgmt->appCache);
6,102✔
1422
    if (!pShow->pIter) return terrno;
6,102✔
1423
  }
1424

1425
  while (numOfRows < rows) {
12,204✔
1426
    pApp = mndGetNextApp(pMnode, pShow->pIter);
12,204✔
1427
    if (pApp == NULL) {
12,204✔
1428
      pShow->pIter = NULL;
6,102✔
1429
      break;
6,102✔
1430
    }
1431

1432
    cols = 0;
6,102✔
1433

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1547
    numOfRows++;
6,102✔
1548
  }
1549

1550
  pShow->numOfRows += numOfRows;
6,102✔
1551
  return numOfRows;
6,102✔
1552
}
1553

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

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