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

taosdata / TDengine / #4908

30 Dec 2025 10:52AM UTC coverage: 65.386% (-0.2%) from 65.541%
#4908

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

1330 existing lines in 113 files now uncovered.

193461 of 295877 relevant lines covered (65.39%)

115765274.47 hits per line

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

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

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

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

128
  TAOS_RETURN(code);
397,374✔
129
}
130

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

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

144
static void getUserIpFromConnObj(SConnObj *pConn, char *dst) {
14,309✔
145
  static char *none = "0.0.0.0";
146
  if (pConn->userIp != 0 && pConn->userIp != INADDR_NONE) {
14,309✔
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) {
14,309✔
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;
14,309✔
158
}
159
static void setUserInfo2Conn(SConnObj *connObj, char *userApp, uint32_t userIp, char *cInfo) {
24,057,747✔
160
  if (connObj == NULL) {
24,057,747✔
161
    return;
×
162
  }
163
  tstrncpy(connObj->userApp, userApp, sizeof(connObj->userApp));
24,057,747✔
164
  tstrncpy(connObj->cInfo, cInfo, sizeof(connObj->cInfo));
24,046,841✔
165
  connObj->userIp = userIp;
24,050,324✔
166
}
167
static void setUserInfoIpToConn(SConnObj *connObj, SIpRange *pRange) {
24,056,376✔
168
  int32_t code = 0;
24,056,376✔
169
  if (connObj == NULL) {
24,056,376✔
170
    return;
×
171
  }
172

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

180

181

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

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

190
  int32_t  len = tsnprintf(connStr, sizeof(connStr), "%s%d%d%d%s", user, ip, port, pid, app);
2,490,921✔
191
  uint32_t connId = mndGenerateUid(connStr, len);
2,490,161✔
192
  if (startTime == 0) startTime = taosGetTimestampMs();
2,983,850✔
193

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

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

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

226

227

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

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

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

239
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &connId, sizeof(connId));
24,056,564✔
240
  if (pConn == NULL) {
24,059,941✔
241
    mDebug("conn:%u, already destroyed", connId);
492,929✔
242
    return NULL;
492,929✔
243
  }
244

245
  pConn->lastAccessTimeMs = taosGetTimestampMs();
23,566,463✔
246
  mTrace("conn:%u, acquired from cache, data:%p", pConn->id, pConn);
23,565,056✔
247
  return pConn;
23,563,026✔
248
}
249

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

254
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
26,152,070✔
255
  if (extendLifespan) taosCacheTryExtendLifeSpan(pMgmt->connCache, (void **)&pConn);
26,151,331✔
256
  taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
26,152,071✔
257
}
258

259
void *mndGetNextConn(SMnode *pMnode, SCacheIter *pIter) {
155,448✔
260
  SConnObj *pConn = NULL;
155,448✔
261
  bool      hasNext = taosCacheIterNext(pIter);
155,448✔
262
  if (hasNext) {
155,448✔
263
    size_t dataLen = 0;
132,928✔
264
    pConn = taosCacheIterGetData(pIter, &dataLen);
132,928✔
265
  } else {
266
    taosCacheDestroyIter(pIter);
22,520✔
267
  }
268

269
  return pConn;
155,448✔
270
}
271

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

278

279

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

289
  int32_t    count = 0;
16,454✔
290
  SConnObj  *pConn = NULL;
16,454✔
291
  while ((pConn = mndGetNextConn(pMnode, pIter)) != NULL) {
110,825✔
292
    if (strncmp(pConn->user, user, TSDB_USER_LEN) == 0) {
94,371✔
293
      count++;
7,085✔
294
    }
295
    mndReleaseConn(pMnode, pConn, true);
94,371✔
296
  }
297

298
  return count;
16,454✔
299
}
300

301

302

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

306
  const char* currPass = pUser->passwords[0].pass;
1,997,811✔
307
  char pass[TSDB_PASSWORD_LEN] = {0};
1,997,580✔
308
  (void)memcpy(pass, inputPass, TSDB_PASSWORD_LEN);
1,997,414✔
309
  pass[TSDB_PASSWORD_LEN - 1] = 0;
1,997,272✔
310

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

321
  // constant time comparison to prevent timing attack
322
  volatile uint8_t res = 0;
1,997,614✔
323
  for (size_t i = 0; i < sizeof(pass) - 1; i++) {
63,892,290✔
324
    res |= pass[i] ^ currPass[i];
61,894,093✔
325
  }
326

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

330

331

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

339

340

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

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

358
  if (token != NULL && mndGetCachedTokenInfo(token, &ti) == NULL) {
1,997,675✔
359
    TAOS_CHECK_GOTO(TSDB_CODE_MND_TOKEN_NOT_EXIST, &lino, _OVER);
×
360
  }
361
  TAOS_CHECK_GOTO(tDeserializeSConnectReq(pReq->pCont, pReq->contLen, &connReq), &lino, _OVER);
1,997,675✔
362
  TAOS_CHECK_GOTO(taosCheckVersionCompatibleFromStr(connReq.sVer, td_version, 3), &lino, _OVER);
1,997,946✔
363
  TAOS_CHECK_GOTO(tVerifyConnectReqSignature(&connReq), &lino, _OVER);
1,998,229✔
364
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, user, &pUser), &lino, _OVER);
1,998,229✔
365

366
  SLoginInfo li = {0};
1,998,197✔
367
  mndGetUserLoginInfo(user, &li);
1,997,650✔
368
  TAOS_CHECK_GOTO(mndCheckConnectPrivilege(pMnode, pUser, token, &li), &lino, _OVER);
1,997,596✔
369

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

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

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

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

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

424
  SConnectRsp connectRsp = {0};
1,997,232✔
425
  connectRsp.acctId = pUser->acctId;
1,996,508✔
426
  connectRsp.superUser = pUser->superUser;
1,995,606✔
427
  connectRsp.sysInfo = pUser->sysInfo;
1,996,402✔
428
  connectRsp.clusterId = pMnode->clusterId;
1,996,466✔
429
  connectRsp.connId = pConn->id;
1,995,936✔
430
  connectRsp.connType = connReq.connType;
1,997,397✔
431
  connectRsp.dnodeNum = mndGetDnodeSize(pMnode);
1,997,397✔
432
  connectRsp.svrTimestamp = taosGetTimestampSec();
1,997,992✔
433
  connectRsp.passVer = pUser->passVersion;
1,996,848✔
434
  connectRsp.authVer = pUser->authVersion;
1,997,402✔
435
  connectRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
1,997,749✔
436
  connectRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
1,997,749✔
437
  connectRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
1,997,749✔
438
  connectRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
1,997,749✔
439
  connectRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
1,997,749✔
440
  connectRsp.enableAuditDelete = tsEnableAuditDelete;
1,997,749✔
441
  connectRsp.enableAuditSelect = tsEnableAuditSelect;
1,997,749✔
442
  connectRsp.enableAuditInsert = tsEnableAuditInsert;
1,997,749✔
443
  connectRsp.auditLevel = tsAuditLevel;
1,997,749✔
444
  tstrncpy(connectRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
1,997,749✔
445
  connectRsp.whiteListVer = pUser->ipWhiteListVer;
1,997,749✔
446
  connectRsp.timeWhiteListVer = pUser->timeWhiteListVer;
1,997,237✔
447
  connectRsp.userId = pUser->uid;
1,997,439✔
448

449

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

457
  int32_t contLen = tSerializeSConnectRsp(NULL, 0, &connectRsp);
1,997,992✔
458
  if (contLen < 0) {
1,995,173✔
459
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
460
  }
461
  void *pRsp = rpcMallocCont(contLen);
1,995,173✔
462
  if (pRsp == NULL) {
1,996,533✔
463
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
464
  }
465

466
  contLen = tSerializeSConnectRsp(pRsp, contLen, &connectRsp);
1,996,533✔
467
  if (contLen < 0) {
1,997,721✔
468
    rpcFreeCont(pRsp);
×
469
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
470
  }
471

472
  pReq->info.rspLen = contLen;
1,997,721✔
473
  pReq->info.rsp = pRsp;
1,997,327✔
474

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

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

489
_OVER:
1,996,499✔
490
  if (code != 0) {
1,998,229✔
491
    mGError("user:%s, failed to login from %s since %s, line:%d, db:%s", user, ip, tstrerror(code), lino, connReq.db);
237✔
492
  }
493

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

498
  TAOS_RETURN(code);
1,998,229✔
499
}
500

501

502

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

506
  taosArrayDestroyEx(pConn->pQueries, tFreeClientHbQueryDesc);
24,057,396✔
507

508
  pConn->pQueries = pBasic->queryDesc;
24,056,408✔
509
  pConn->numOfQueries = pBasic->queryDesc ? taosArrayGetSize(pBasic->queryDesc) : 0;
24,057,520✔
510
  pBasic->queryDesc = NULL;
24,058,050✔
511

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

514
  taosWUnLockLatch(&pConn->queryLock);
24,060,639✔
515

516
  return TSDB_CODE_SUCCESS;
24,059,699✔
517
}
518

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

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

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

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

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

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

549
  SAppObj *pApp = taosCacheAcquireByKey(pMgmt->appCache, &appId, sizeof(appId));
24,058,176✔
550
  if (pApp == NULL) {
24,055,358✔
551
    mDebug("app %" PRIx64 " not in cache", appId);
632,746✔
552
    return NULL;
632,746✔
553
  }
554

555
  pApp->lastAccessTimeMs = (uint64_t)taosGetTimestampMs();
23,424,767✔
556

557
  mTrace("app %" PRIx64 " acquired from cache", appId);
23,424,326✔
558
  return pApp;
23,419,036✔
559
}
560

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

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

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

579
  return pApp;
5,388✔
580
}
581

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

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

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

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

613
  mndReleaseApp(pMnode, pApp);
23,424,803✔
614

615
  return TSDB_CODE_SUCCESS;
23,422,631✔
616
}
617

618
static int32_t mndGetOnlineDnodeNum(SMnode *pMnode, int32_t *num) {
19,814,260✔
619
  SSdb      *pSdb = pMnode->pSdb;
19,814,260✔
620
  SDnodeObj *pDnode = NULL;
19,813,929✔
621
  int64_t    curMs = taosGetTimestampMs();
19,811,729✔
622
  void      *pIter = NULL;
19,811,729✔
623

624
  while (true) {
34,519,025✔
625
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
54,330,754✔
626
    if (pIter == NULL) break;
54,333,495✔
627

628
    bool online = mndIsDnodeOnline(pDnode, curMs);
34,518,753✔
629
    if (online) {
34,516,577✔
630
      (*num)++;
33,405,390✔
631
    }
632

633
    sdbRelease(pSdb, pDnode);
34,516,701✔
634
  }
635

636
  return TSDB_CODE_SUCCESS;
19,814,742✔
637
}
638

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

645
  if (0 != pHbReq->app.appId) {
24,057,642✔
646
    TAOS_CHECK_RETURN(mndUpdateAppInfo(pMnode, pHbReq, &pMsg->info.conn));
24,053,255✔
647
  }
648

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

667
    setUserInfo2Conn(pConn, pHbReq->userApp, pHbReq->userIp, pHbReq->cInfo);
24,055,477✔
668
    setUserInfoIpToConn(pConn, &pHbReq->userDualIp);
24,050,548✔
669

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

678
    TAOS_CHECK_RETURN(mndSaveQueryList(pConn, pBasic));
24,053,882✔
679
    if (pConn->killed != 0) {
24,059,699✔
680
      rspBasic->killConnection = 1;
×
681
    }
682

683
    if (pConn->killId != 0) {
24,059,699✔
684
      rspBasic->killRid = pConn->killId;
5✔
685
      pConn->killId = 0;
5✔
686
    }
687

688
    rspBasic->connId = pConn->id;
24,060,041✔
689
    rspBasic->connId = pConn->id;
24,058,755✔
690
    rspBasic->totalDnodes = pObj->totalDnodes;
24,058,897✔
691
    rspBasic->onlineDnodes = pObj->onlineDnodes;
24,059,231✔
692
    rspBasic->epSet = pObj->epSet;
24,059,373✔
693
    rspBasic->pQnodeList = taosArrayDup(pObj->pQnodeList, NULL);
24,059,089✔
694

695
    mndReleaseConn(pMnode, pConn, true);
24,060,041✔
696

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

702
  int32_t kvNum = taosHashGetSize(pHbReq->info);
24,059,566✔
703
  if (NULL == pHbReq->info || kvNum <= 0) {
24,059,707✔
704
    if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
12,625,930✔
705
      mError("failed to put rsp into array, but continue at this heartbeat");
×
706
    }
707
    return TSDB_CODE_SUCCESS;
6,313,132✔
708
  }
709

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

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

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

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

744
  void *pIter = taosHashIterate(pHbReq->info, NULL);
17,746,909✔
745
  while (pIter != NULL) {
44,852,333✔
746
    SKv *kv = pIter;
27,105,424✔
747

748
    switch (kv->key) {
27,105,424✔
749
      case HEARTBEAT_KEY_USER_AUTHINFO: {
17,746,909✔
750
        void   *rspMsg = NULL;
17,746,909✔
751
        int32_t rspLen = 0;
17,746,909✔
752
        (void)mndValidateUserAuthInfo(pMnode, kv->value, kv->valueLen / sizeof(SUserAuthVersion), &rspMsg, &rspLen,
17,746,909✔
753
                                      pObj->ipWhiteListVer);
754
        if (rspMsg && rspLen > 0) {
17,746,909✔
755
          SKv kv1 = {.key = HEARTBEAT_KEY_USER_AUTHINFO, .valueLen = rspLen, .value = rspMsg};
750,967✔
756
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
1,501,934✔
757
            mError("failed to put kv into array, but continue at this heartbeat");
×
758
          }
759
        }
760
        break;
17,746,909✔
761
      }
762
      case HEARTBEAT_KEY_DBINFO: {
5,520,350✔
763
        void   *rspMsg = NULL;
5,520,350✔
764
        int32_t rspLen = 0;
5,520,350✔
765
        (void)mndValidateDbInfo(pMnode, kv->value, kv->valueLen / sizeof(SDbCacheInfo), &rspMsg, &rspLen);
5,520,350✔
766
        if (rspMsg && rspLen > 0) {
5,520,350✔
767
          SKv kv1 = {.key = HEARTBEAT_KEY_DBINFO, .valueLen = rspLen, .value = rspMsg};
5,520,350✔
768
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
11,040,700✔
769
            mError("failed to put kv into array, but continue at this heartbeat");
×
770
          }
771
        }
772
        break;
5,520,350✔
773
      }
774
      case HEARTBEAT_KEY_STBINFO: {
3,837,115✔
775
        void   *rspMsg = NULL;
3,837,115✔
776
        int32_t rspLen = 0;
3,837,115✔
777
        (void)mndValidateStbInfo(pMnode, kv->value, kv->valueLen / sizeof(SSTableVersion), &rspMsg, &rspLen);
3,837,115✔
778
        if (rspMsg && rspLen > 0) {
3,837,115✔
779
          SKv kv1 = {.key = HEARTBEAT_KEY_STBINFO, .valueLen = rspLen, .value = rspMsg};
3,837,115✔
780
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
7,674,230✔
781
            mError("failed to put kv into array, but continue at this heartbeat");
×
782
          }
783
        }
784
        break;
3,837,115✔
785
      }
786
#ifdef TD_ENTERPRISE
787
      case HEARTBEAT_KEY_DYN_VIEW: {
525✔
788
        break;
525✔
789
      }
790
      case HEARTBEAT_KEY_VIEWINFO: {
525✔
791
        if (!needCheck) {
525✔
792
          break;
×
793
        }
794

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

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

828
  if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
35,493,561✔
829
    if (terrno != 0) code = terrno;
×
830
  }
831
  TAOS_RETURN(code);
17,746,652✔
832
}
833

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

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

846
  SConnPreparedObj obj = {0};
19,813,577✔
847
  obj.totalDnodes = mndGetDnodeSize(pMnode);
19,814,189✔
848
  obj.ipWhiteListVer = batchReq.ipWhiteListVer;
19,814,742✔
849
  TAOS_CHECK_RETURN(mndGetOnlineDnodeNum(pMnode, &obj.onlineDnodes));
19,814,742✔
850
  mndGetMnodeEpSet(pMnode, &obj.epSet);
19,814,742✔
851
  TAOS_CHECK_RETURN(mndCreateQnodeList(pMnode, &obj.pQnodeList, -1));
19,814,742✔
852

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

871
  int32_t sz = taosArrayGetSize(batchReq.reqs);
19,810,337✔
872
  for (int i = 0; i < sz; i++) {
43,871,295✔
873
    SClientHbReq *pHbReq = taosArrayGet(batchReq.reqs, i);
24,059,376✔
874
    if (pHbReq->connKey.connType == CONN_TYPE__QUERY || pHbReq->connKey.connType == CONN_TYPE__TMQ) {
24,060,041✔
875
      TAOS_CHECK_EXIT(mndProcessQueryHeartBeat(pMnode, pReq, pHbReq, &batchRsp, &obj));
24,060,041✔
876
    } 
877
  }
878
  taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
19,811,919✔
879

880
  int32_t tlen = tSerializeSClientHbBatchRsp(NULL, 0, &batchRsp);
19,812,841✔
881
  if (tlen < 0) {
19,807,094✔
882
    TAOS_CHECK_EXIT(tlen);
×
883
  }
884
  void *buf = rpcMallocCont(tlen);
19,807,094✔
885
  if (!buf) {
19,809,544✔
886
    TAOS_CHECK_EXIT(terrno);
×
887
  }
888
  tlen = tSerializeSClientHbBatchRsp(buf, tlen, &batchRsp);
19,809,544✔
889
  if (tlen < 0) {
19,812,475✔
890
    rpcFreeCont(buf);
×
891
    TAOS_CHECK_EXIT(tlen);
×
892
  }
893
  pReq->info.rspLen = tlen;
19,812,475✔
894
  pReq->info.rsp = buf;
19,810,042✔
895
_exit:
19,810,389✔
896
  tFreeClientHbBatchRsp(&batchRsp);
897

898
  taosArrayDestroy(obj.pQnodeList);
19,814,650✔
899

900
  TAOS_RETURN(code);
19,813,363✔
901
}
902

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

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

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

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

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

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

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

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

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

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

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

984
_exit:
×
985

986
  TAOS_RETURN(code);
×
987
}
988

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

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

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

1010
    if ((taosGetTimestampMs() - pConn->lastAccessTimeMs) > ((int64_t)CACHE_OBJ_KEEP_TIME * 1000)) {
20,182✔
1011
      continue;
10,622✔
1012
    }
1013

1014
    cols = 0;
9,560✔
1015

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

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

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

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

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

1056
    STR_TO_VARSTR(endpoint, addr);
9,560✔
1057

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

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

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

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

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

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

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

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

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

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

1134
    numOfRows++;
9,560✔
1135
  }
1136

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1329
    pBlock->info.rows++;
4,749✔
1330
  }
1331

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

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

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

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

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

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

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

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

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

1394
    cols = 0;
2,694✔
1395

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

© 2026 Coveralls, Inc