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

taosdata / TDengine / #4899

27 Dec 2025 07:32AM UTC coverage: 65.534% (+0.5%) from 65.061%
#4899

push

travis-ci

web-flow
test: remove semaphore test (#34071)

189567 of 289265 relevant lines covered (65.53%)

114701701.06 hits per line

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

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

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

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

128
  TAOS_RETURN(code);
385,493✔
129
}
130

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

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

144
static void getUserIpFromConnObj(SConnObj *pConn, char *dst) {
34,636✔
145
  static char *none = "0.0.0.0";
146
  if (pConn->userIp != 0 && pConn->userIp != INADDR_NONE) {
34,636✔
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) {
34,636✔
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;
34,636✔
158
}
159
static void setUserInfo2Conn(SConnObj *connObj, char *userApp, uint32_t userIp, char *cInfo) {
23,675,755✔
160
  if (connObj == NULL) {
23,675,755✔
161
    return;
×
162
  }
163
  tstrncpy(connObj->userApp, userApp, sizeof(connObj->userApp));
23,675,755✔
164
  tstrncpy(connObj->cInfo, cInfo, sizeof(connObj->cInfo));
23,661,280✔
165
  connObj->userIp = userIp;
23,664,086✔
166
}
167
static void setUserInfoIpToConn(SConnObj *connObj, SIpRange *pRange) {
23,674,699✔
168
  int32_t code = 0;
23,674,699✔
169
  if (connObj == NULL) {
23,674,699✔
170
    return;
×
171
  }
172

173
  code = tIpUintToStr(pRange, &connObj->userDualIp);
23,674,699✔
174
  if (code != 0) {
23,667,336✔
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,484,923✔
183
                               int32_t pid, const char *app, int64_t startTime, const char *sVer) {
184
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
2,484,923✔
185

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

190
  int32_t  len = tsnprintf(connStr, sizeof(connStr), "%s%d%d%d%s", user, ip, port, pid, app);
2,484,925✔
191
  uint32_t connId = mndGenerateUid(connStr, len);
2,484,885✔
192
  if (startTime == 0) startTime = taosGetTimestampMs();
3,003,975✔
193

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

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

214
  SConnObj *pConn =
215
      taosCachePut(pMgmt->connCache, &connId, sizeof(uint32_t), &connObj, sizeof(connObj), CACHE_OBJ_KEEP_TIME * 1000);
2,484,925✔
216
  if (pConn == NULL) {
2,484,894✔
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,484,894✔
222
    return pConn;
2,484,690✔
223
  }
224
}
225

226

227

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

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

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

239
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &connId, sizeof(connId));
23,675,650✔
240
  if (pConn == NULL) {
23,679,280✔
241
    mDebug("conn:%u, already destroyed", connId);
519,050✔
242
    return NULL;
519,050✔
243
  }
244

245
  pConn->lastAccessTimeMs = taosGetTimestampMs();
23,159,280✔
246
  mTrace("conn:%u, acquired from cache, data:%p", pConn->id, pConn);
23,158,869✔
247
  return pConn;
23,146,782✔
248
}
249

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

254
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
26,189,655✔
255
  if (extendLifespan) taosCacheTryExtendLifeSpan(pMgmt->connCache, (void **)&pConn);
26,189,655✔
256
  taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
26,189,058✔
257
}
258

259
void *mndGetNextConn(SMnode *pMnode, SCacheIter *pIter) {
767,813✔
260
  SConnObj *pConn = NULL;
767,813✔
261
  bool      hasNext = taosCacheIterNext(pIter);
767,813✔
262
  if (hasNext) {
768,497✔
263
    size_t dataLen = 0;
708,322✔
264
    pConn = taosCacheIterGetData(pIter, &dataLen);
708,322✔
265
  } else {
266
    taosCacheDestroyIter(pIter);
60,175✔
267
  }
268

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

289
  int32_t    count = 0;
46,077✔
290
  SConnObj  *pConn = NULL;
46,077✔
291
  while ((pConn = mndGetNextConn(pMnode, pIter)) != NULL) {
589,079✔
292
    if (strncmp(pConn->user, user, TSDB_USER_LEN) == 0) {
543,002✔
293
      count++;
77,231✔
294
    }
295
    mndReleaseConn(pMnode, pConn, true);
543,002✔
296
  }
297

298
  return count;
46,077✔
299
}
300

301

302

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

306
  const char* currPass = pUser->passwords[0].pass;
1,966,006✔
307
  char pass[TSDB_PASSWORD_LEN] = {0};
1,965,983✔
308
  (void)memcpy(pass, inputPass, TSDB_PASSWORD_LEN);
1,966,288✔
309
  pass[TSDB_PASSWORD_LEN - 1] = 0;
1,965,905✔
310

311
  if (pUser->passEncryptAlgorithm != 0) {
1,965,905✔
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) {
×
317
      return code;
×
318
    }
319
  }
320

321
  // constant time comparison to prevent timing attack
322
  volatile uint8_t res = 0;
1,965,868✔
323
  for (size_t i = 0; i < sizeof(pass) - 1; i++) {
62,909,388✔
324
    res |= pass[i] ^ currPass[i];
60,943,071✔
325
  }
326

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

330

331

332
static bool verifyTotp(SUserObj *pUser, int32_t totpCode) {
1,966,006✔
333
  if (!mndIsTotpEnabledUser(pUser)) {
1,966,006✔
334
    return true;
1,966,317✔
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,967,027✔
342
  int32_t          code = 0, lino = 0;
1,967,027✔
343

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

358
  if (token != NULL && mndGetCachedTokenInfo(token, &ti) == NULL) {
1,966,926✔
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,966,926✔
362
  TAOS_CHECK_GOTO(taosCheckVersionCompatibleFromStr(connReq.sVer, td_version, 3), &lino, _OVER);
1,967,031✔
363
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, user, &pUser), &lino, _OVER);
1,966,573✔
364

365
  SLoginInfo li = {0};
1,966,244✔
366
  mndGetUserLoginInfo(user, &li);
1,966,278✔
367
  TAOS_CHECK_GOTO(mndCheckConnectPrivilege(pMnode, pUser, token, &li), &lino, _OVER);
1,965,932✔
368

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

399
  if (connReq.db[0] != 0) {
1,965,931✔
400
    char db[TSDB_DB_FNAME_LEN] = {0};
396,758✔
401
    (void)snprintf(db, TSDB_DB_FNAME_LEN, "%d%s%s", pUser->acctId, TS_PATH_DELIMITER, connReq.db);
396,758✔
402
    pDb = mndAcquireDb(pMnode, db);
396,758✔
403
    if (pDb == NULL) {
396,581✔
404
      if (0 != strcmp(connReq.db, TSDB_INFORMATION_SCHEMA_DB) && (0 != strcmp(connReq.db, TSDB_PERFORMANCE_SCHEMA_DB))) {
668✔
405
        TAOS_CHECK_GOTO(TSDB_CODE_MND_DB_NOT_EXIST, &lino, _OVER);
56✔
406
      }
407
    }
408

409
    TAOS_CHECK_GOTO(mndCheckDbPrivilege(pMnode, user, RPC_MSG_TOKEN(pReq), MND_OPER_READ_OR_WRITE_DB, pDb), &lino, _OVER);
396,525✔
410
  }
411

412
  if (connReq.connType == CONN_TYPE__AUTH_TEST) {
1,965,875✔
413
    code = 0;
×
414
    goto _OVER;
×
415
  }
416

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

423
  SConnectRsp connectRsp = {0};
1,965,676✔
424
  connectRsp.acctId = pUser->acctId;
1,965,665✔
425
  connectRsp.superUser = pUser->superUser;
1,964,882✔
426
  connectRsp.sysInfo = pUser->sysInfo;
1,965,441✔
427
  connectRsp.clusterId = pMnode->clusterId;
1,964,980✔
428
  connectRsp.connId = pConn->id;
1,965,665✔
429
  connectRsp.connType = connReq.connType;
1,965,696✔
430
  connectRsp.dnodeNum = mndGetDnodeSize(pMnode);
1,965,696✔
431
  connectRsp.svrTimestamp = taosGetTimestampSec();
1,965,875✔
432
  connectRsp.passVer = pUser->passVersion;
1,965,674✔
433
  connectRsp.authVer = pUser->authVersion;
1,965,674✔
434
  connectRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
1,965,853✔
435
  connectRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
1,965,853✔
436
  connectRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
1,965,853✔
437
  connectRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
1,965,853✔
438
  connectRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
1,965,853✔
439
  connectRsp.enableAuditDelete = tsEnableAuditDelete;
1,965,853✔
440
  connectRsp.enableAuditSelect = tsEnableAuditSelect;
1,965,853✔
441
  connectRsp.enableAuditInsert = tsEnableAuditInsert;
1,965,853✔
442
  connectRsp.auditLevel = tsAuditLevel;
1,965,853✔
443
  tstrncpy(connectRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
1,965,853✔
444
  connectRsp.whiteListVer = pUser->ipWhiteListVer;
1,965,853✔
445
  connectRsp.timeWhiteListVer = pUser->timeWhiteListVer;
1,965,842✔
446

447
  tstrncpy(connectRsp.sVer, td_version, sizeof(connectRsp.sVer));
1,965,665✔
448
  tstrncpy(connectRsp.user, user, sizeof(connectRsp.user));
1,965,665✔
449
  tstrncpy(connectRsp.tokenName, ti.name, sizeof(connectRsp.tokenName));
1,965,665✔
450
  (void)snprintf(connectRsp.sDetailVer, sizeof(connectRsp.sDetailVer), "ver:%s\nbuild:%s\ngitinfo:%s", td_version,
1,965,665✔
451
                 td_buildinfo, td_gitinfo);
452
  mndGetMnodeEpSet(pMnode, &connectRsp.epSet);
1,965,665✔
453

454
  int32_t contLen = tSerializeSConnectRsp(NULL, 0, &connectRsp);
1,965,875✔
455
  if (contLen < 0) {
1,965,562✔
456
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
457
  }
458
  void *pRsp = rpcMallocCont(contLen);
1,965,562✔
459
  if (pRsp == NULL) {
1,965,748✔
460
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
461
  }
462

463
  contLen = tSerializeSConnectRsp(pRsp, contLen, &connectRsp);
1,965,748✔
464
  if (contLen < 0) {
1,965,719✔
465
    rpcFreeCont(pRsp);
×
466
    TAOS_CHECK_GOTO(contLen, &lino, _OVER);
×
467
  }
468

469
  pReq->info.rspLen = contLen;
1,965,719✔
470
  pReq->info.rsp = pRsp;
1,965,780✔
471

472
  mGDebug("user:%s, login from %s:%d, conn:%u, app:%s, db:%s", user, ip, port, pConn->id, connReq.app, connReq.db);
1,965,127✔
473
  code = 0;
1,965,875✔
474

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

486
_OVER:
1,965,472✔
487
  if (code != 0) {
1,967,031✔
488
    mGError("user:%s, failed to login from %s since %s, line:%d, db:%s", user, ip, tstrerror(code), lino, connReq.db);
1,156✔
489
  }
490

491
  mndReleaseUser(pMnode, pUser);
1,967,031✔
492
  mndReleaseDb(pMnode, pDb);
1,967,031✔
493
  mndReleaseConn(pMnode, pConn, true);
1,967,031✔
494

495
  TAOS_RETURN(code);
1,967,031✔
496
}
497

498

499

500
static int32_t mndSaveQueryList(SConnObj *pConn, SQueryHbReqBasic *pBasic) {
23,672,329✔
501
  taosWLockLatch(&pConn->queryLock);
23,672,329✔
502

503
  taosArrayDestroyEx(pConn->pQueries, tFreeClientHbQueryDesc);
23,678,024✔
504

505
  pConn->pQueries = pBasic->queryDesc;
23,677,834✔
506
  pConn->numOfQueries = pBasic->queryDesc ? taosArrayGetSize(pBasic->queryDesc) : 0;
23,678,107✔
507
  pBasic->queryDesc = NULL;
23,679,015✔
508

509
  mDebug("queries updated in conn %u, num:%d", pConn->id, pConn->numOfQueries);
23,679,075✔
510

511
  taosWUnLockLatch(&pConn->queryLock);
23,682,186✔
512

513
  return TSDB_CODE_SUCCESS;
23,680,404✔
514
}
515

516
static SAppObj *mndCreateApp(SMnode *pMnode, const SIpAddr *pAddr, const SAppHbReq *pReq) {
562,561✔
517
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
562,561✔
518

519
  SAppObj app;
562,393✔
520
  app.appId = pReq->appId;
562,561✔
521
  app.cliAddr = *pAddr;
562,561✔
522
  app.pid = pReq->pid;
562,561✔
523
  tstrncpy(app.name, pReq->name, sizeof(app.name));
562,561✔
524
  app.startTime = pReq->startTime;
562,561✔
525
  (void)memcpy(&app.summary, &pReq->summary, sizeof(pReq->summary));
562,561✔
526
  app.lastAccessTimeMs = taosGetTimestampMs();
562,561✔
527

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

536
  mTrace("app %" PRIx64 " is put into cache", pReq->appId);
562,561✔
537
  return pApp;
562,561✔
538
}
539

540
static void mndFreeApp(SAppObj *pApp) { mTrace("app %" PRIx64 " is destroyed", pApp->appId); }
562,561✔
541

542
static SAppObj *mndAcquireApp(SMnode *pMnode, int64_t appId) {
23,678,838✔
543
  terrno = 0;
23,678,838✔
544
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
23,679,294✔
545

546
  SAppObj *pApp = taosCacheAcquireByKey(pMgmt->appCache, &appId, sizeof(appId));
23,677,652✔
547
  if (pApp == NULL) {
23,673,713✔
548
    mDebug("app %" PRIx64 " not in cache", appId);
562,561✔
549
    return NULL;
562,561✔
550
  }
551

552
  pApp->lastAccessTimeMs = (uint64_t)taosGetTimestampMs();
23,114,884✔
553

554
  mTrace("app %" PRIx64 " acquired from cache", appId);
23,113,628✔
555
  return pApp;
23,106,247✔
556
}
557

558
static void mndReleaseApp(SMnode *pMnode, SAppObj *pApp) {
23,672,540✔
559
  if (pApp == NULL) return;
23,672,540✔
560
  mTrace("release app %" PRIx64 " to cache", pApp->appId);
23,672,540✔
561

562
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
23,672,540✔
563
  taosCacheRelease(pMgmt->appCache, (void **)&pApp, false);
23,674,325✔
564
}
565

566
SAppObj *mndGetNextApp(SMnode *pMnode, SCacheIter *pIter) {
5,240✔
567
  SAppObj *pApp = NULL;
5,240✔
568
  bool     hasNext = taosCacheIterNext(pIter);
5,240✔
569
  if (hasNext) {
5,240✔
570
    size_t dataLen = 0;
2,620✔
571
    pApp = taosCacheIterGetData(pIter, &dataLen);
2,620✔
572
  } else {
573
    taosCacheDestroyIter(pIter);
2,620✔
574
  }
575

576
  return pApp;
5,240✔
577
}
578

579
static void mndCancelGetNextApp(SMnode *pMnode, void *pIter) {
×
580
  if (pIter != NULL) {
×
581
    taosCacheDestroyIter(pIter);
×
582
  }
583
}
×
584

585
static SClientHbRsp *mndMqHbBuildRsp(SMnode *pMnode, SClientHbReq *pReq) {
×
586
  //
587
  return NULL;
×
588
}
589

590
static int32_t mndUpdateAppInfo(SMnode *pMnode, SClientHbReq *pHbReq, const SRpcConnInfo *connInfo) {
23,678,838✔
591
  int32_t    code = 0;
23,678,838✔
592
  SAppHbReq *pReq = &pHbReq->app;
23,678,838✔
593
  SAppObj   *pApp = mndAcquireApp(pMnode, pReq->appId);
23,679,303✔
594
  if (pApp == NULL) {
23,675,748✔
595
    pApp = mndCreateApp(pMnode, &connInfo->cliAddr, pReq);
562,561✔
596
    if (pApp == NULL) {
562,561✔
597
      mError("failed to create new app %" PRIx64 " since %s", pReq->appId, terrstr());
×
598
      code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
599
      if (terrno != 0) code = terrno;
×
600
      TAOS_RETURN(code);
×
601
    } else {
602
      mDebug("a new app %" PRIx64 " is created", pReq->appId);
562,561✔
603
      mndReleaseApp(pMnode, pApp);
562,561✔
604
      return TSDB_CODE_SUCCESS;
562,561✔
605
    }
606
  }
607

608
  (void)memcpy(&pApp->summary, &pReq->summary, sizeof(pReq->summary));
23,113,187✔
609

610
  mndReleaseApp(pMnode, pApp);
23,115,107✔
611

612
  return TSDB_CODE_SUCCESS;
23,106,182✔
613
}
614

615
static int32_t mndGetOnlineDnodeNum(SMnode *pMnode, int32_t *num) {
19,634,090✔
616
  SSdb      *pSdb = pMnode->pSdb;
19,634,090✔
617
  SDnodeObj *pDnode = NULL;
19,633,622✔
618
  int64_t    curMs = taosGetTimestampMs();
19,633,515✔
619
  void      *pIter = NULL;
19,633,515✔
620

621
  while (true) {
33,446,654✔
622
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
53,080,169✔
623
    if (pIter == NULL) break;
53,081,199✔
624

625
    bool online = mndIsDnodeOnline(pDnode, curMs);
33,445,286✔
626
    if (online) {
33,443,802✔
627
      (*num)++;
32,362,110✔
628
    }
629

630
    sdbRelease(pSdb, pDnode);
33,443,741✔
631
  }
632

633
  return TSDB_CODE_SUCCESS;
19,635,913✔
634
}
635

636
static int32_t mndProcessQueryHeartBeat(SMnode *pMnode, SRpcMsg *pMsg, SClientHbReq *pHbReq,
23,678,838✔
637
                                        SClientHbBatchRsp *pBatchRsp, SConnPreparedObj *pObj) {
638
  int32_t       code = 0;
23,678,838✔
639
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
23,678,838✔
640
  SClientHbRsp  hbRsp = {.connKey = pHbReq->connKey, .status = 0, .info = NULL, .query = NULL};
23,677,228✔
641

642
  if (0 != pHbReq->app.appId) {
23,673,789✔
643
    TAOS_CHECK_RETURN(mndUpdateAppInfo(pMnode, pHbReq, &pMsg->info.conn));
23,676,665✔
644
  }
645

646
  if (pHbReq->query) {
23,663,987✔
647
    SQueryHbReqBasic *pBasic = pHbReq->query;
23,676,097✔
648
    SConnObj *pConn = mndAcquireConn(pMnode, pBasic->connId);
23,675,272✔
649
    if (pConn == NULL) {
23,668,808✔
650
      SRpcConnInfo  connInfo = pMsg->info.conn;
519,050✔
651
      const char* user = pHbReq->user;
519,050✔
652
      pConn = mndCreateConn(pMnode, user, pHbReq->tokenName, pHbReq->connKey.connType, &connInfo.cliAddr, pHbReq->app.pid,
519,050✔
653
                            pHbReq->app.name, 0, pHbReq->sVer);
519,050✔
654
      if (pConn == NULL) {
519,050✔
655
        mError("user:%s, conn:%u is freed and failed to create new since %s", user, pBasic->connId, terrstr());
×
656
        code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
657
        if (terrno != 0) code = terrno;
×
658
        TAOS_RETURN(code);
×
659
      } else {
660
        mDebug("user:%s, conn:%u is freed, will create a new conn:%u", user, pBasic->connId, pConn->id);
519,050✔
661
      }
662
    }
663

664
    setUserInfo2Conn(pConn, pHbReq->userApp, pHbReq->userIp, pHbReq->cInfo);
23,668,808✔
665
    setUserInfoIpToConn(pConn, &pHbReq->userDualIp);
23,674,136✔
666

667
    SQueryHbRspBasic *rspBasic = taosMemoryCalloc(1, sizeof(SQueryHbRspBasic));
23,664,706✔
668
    if (rspBasic == NULL) {
23,667,005✔
669
      mndReleaseConn(pMnode, pConn, true);
×
670
      code = terrno;
×
671
      mError("user:%s, conn:%u failed to process hb while since %s", pConn->user, pBasic->connId, terrstr());
×
672
      TAOS_RETURN(code);
×
673
    }
674

675
    TAOS_CHECK_RETURN(mndSaveQueryList(pConn, pBasic));
23,667,005✔
676
    if (pConn->killed != 0) {
23,680,404✔
677
      rspBasic->killConnection = 1;
×
678
    }
679

680
    if (pConn->killId != 0) {
23,680,729✔
681
      rspBasic->killRid = pConn->killId;
2✔
682
      pConn->killId = 0;
2✔
683
    }
684

685
    rspBasic->connId = pConn->id;
23,680,706✔
686
    rspBasic->connId = pConn->id;
23,681,180✔
687
    rspBasic->totalDnodes = pObj->totalDnodes;
23,681,180✔
688
    rspBasic->onlineDnodes = pObj->onlineDnodes;
23,680,056✔
689
    rspBasic->epSet = pObj->epSet;
23,680,056✔
690
    rspBasic->pQnodeList = taosArrayDup(pObj->pQnodeList, NULL);
23,680,056✔
691

692
    mndReleaseConn(pMnode, pConn, true);
23,680,390✔
693

694
    hbRsp.query = rspBasic;
23,681,103✔
695
  } else {
696
    mDebug("no query info in hb msg");
×
697
  }
698

699
  int32_t kvNum = taosHashGetSize(pHbReq->info);
23,681,103✔
700
  if (NULL == pHbReq->info || kvNum <= 0) {
23,681,180✔
701
    if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
12,665,218✔
702
      mError("failed to put rsp into array, but continue at this heartbeat");
×
703
    }
704
    return TSDB_CODE_SUCCESS;
6,332,609✔
705
  }
706

707
  hbRsp.info = taosArrayInit(kvNum, sizeof(SKv));
17,348,571✔
708
  if (NULL == hbRsp.info) {
17,348,571✔
709
    mError("taosArrayInit %d rsp kv failed", kvNum);
×
710
    code = terrno;
×
711
    tFreeClientHbRsp(&hbRsp);
712
    TAOS_RETURN(code);
×
713
  }
714

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

724
    SDynViewVersion *pRspVer = NULL;
3,219✔
725
    if (0 != (code = mndValidateDynViewVersion(pMnode, pDynViewVer, &needCheck, &pRspVer))) {
3,219✔
726
      TAOS_RETURN(code);
×
727
    }
728

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

741
  void *pIter = taosHashIterate(pHbReq->info, NULL);
17,348,571✔
742
  while (pIter != NULL) {
43,686,142✔
743
    SKv *kv = pIter;
26,338,275✔
744

745
    switch (kv->key) {
26,338,275✔
746
      case HEARTBEAT_KEY_USER_AUTHINFO: {
17,328,634✔
747
        void   *rspMsg = NULL;
17,328,634✔
748
        int32_t rspLen = 0;
17,328,634✔
749
        (void)mndValidateUserAuthInfo(pMnode, kv->value, kv->valueLen / sizeof(SUserAuthVersion), &rspMsg, &rspLen,
17,328,634✔
750
                                      pObj->ipWhiteListVer);
751
        if (rspMsg && rspLen > 0) {
17,327,275✔
752
          SKv kv1 = {.key = HEARTBEAT_KEY_USER_AUTHINFO, .valueLen = rspLen, .value = rspMsg};
616,335✔
753
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
1,232,670✔
754
            mError("failed to put kv into array, but continue at this heartbeat");
×
755
          }
756
        }
757
        break;
17,328,634✔
758
      }
759
      case HEARTBEAT_KEY_DBINFO: {
5,274,279✔
760
        void   *rspMsg = NULL;
5,274,279✔
761
        int32_t rspLen = 0;
5,274,279✔
762
        (void)mndValidateDbInfo(pMnode, kv->value, kv->valueLen / sizeof(SDbCacheInfo), &rspMsg, &rspLen);
5,274,279✔
763
        if (rspMsg && rspLen > 0) {
5,274,279✔
764
          SKv kv1 = {.key = HEARTBEAT_KEY_DBINFO, .valueLen = rspLen, .value = rspMsg};
5,274,279✔
765
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
10,548,558✔
766
            mError("failed to put kv into array, but continue at this heartbeat");
×
767
          }
768
        }
769
        break;
5,274,279✔
770
      }
771
      case HEARTBEAT_KEY_STBINFO: {
3,728,924✔
772
        void   *rspMsg = NULL;
3,728,924✔
773
        int32_t rspLen = 0;
3,728,924✔
774
        (void)mndValidateStbInfo(pMnode, kv->value, kv->valueLen / sizeof(SSTableVersion), &rspMsg, &rspLen);
3,728,924✔
775
        if (rspMsg && rspLen > 0) {
3,728,924✔
776
          SKv kv1 = {.key = HEARTBEAT_KEY_STBINFO, .valueLen = rspLen, .value = rspMsg};
3,728,924✔
777
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
7,457,848✔
778
            mError("failed to put kv into array, but continue at this heartbeat");
×
779
          }
780
        }
781
        break;
3,728,924✔
782
      }
783
#ifdef TD_ENTERPRISE
784
      case HEARTBEAT_KEY_DYN_VIEW: {
3,219✔
785
        break;
3,219✔
786
      }
787
      case HEARTBEAT_KEY_VIEWINFO: {
3,219✔
788
        if (!needCheck) {
3,219✔
789
          break;
×
790
        }
791

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

822
    pIter = taosHashIterate(pHbReq->info, pIter);
26,338,275✔
823
  }
824

825
  if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
34,696,438✔
826
    if (terrno != 0) code = terrno;
×
827
  }
828
  TAOS_RETURN(code);
17,348,571✔
829
}
830

831
static int32_t mndProcessHeartBeatReq(SRpcMsg *pReq) {
19,632,907✔
832
  int32_t code = 0;
19,632,907✔
833
  int32_t lino = 0;
19,632,907✔
834
  SMnode *pMnode = pReq->info.node;
19,632,907✔
835

836
  SClientHbBatchReq batchReq = {0};
19,633,658✔
837
  if (tDeserializeSClientHbBatchReq(pReq->pCont, pReq->contLen, &batchReq) != 0) {
19,633,344✔
838
    taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
×
839
    code = TSDB_CODE_INVALID_MSG;
×
840
    TAOS_RETURN(code);
×
841
  }
842

843
  SConnPreparedObj obj = {0};
19,633,267✔
844
  obj.totalDnodes = mndGetDnodeSize(pMnode);
19,634,754✔
845
  obj.ipWhiteListVer = batchReq.ipWhiteListVer;
19,632,898✔
846
  TAOS_CHECK_RETURN(mndGetOnlineDnodeNum(pMnode, &obj.onlineDnodes));
19,632,898✔
847
  mndGetMnodeEpSet(pMnode, &obj.epSet);
19,635,913✔
848
  TAOS_CHECK_RETURN(mndCreateQnodeList(pMnode, &obj.pQnodeList, -1));
19,635,119✔
849

850
  SClientHbBatchRsp batchRsp = {0};
19,632,746✔
851
  batchRsp.svrTimestamp = taosGetTimestampSec();
19,633,284✔
852
  batchRsp.rsps = taosArrayInit(0, sizeof(SClientHbRsp));
19,633,496✔
853
  if (batchRsp.rsps == NULL) {
19,630,857✔
854
    TAOS_CHECK_EXIT(terrno);
×
855
  }
856
  batchRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
19,630,857✔
857
  batchRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
19,630,857✔
858
  batchRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
19,630,857✔
859
  tstrncpy(batchRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
19,630,857✔
860
  batchRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
19,630,857✔
861
  batchRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
19,630,857✔
862
  batchRsp.enableAuditDelete = tsEnableAuditDelete;
19,630,857✔
863
  batchRsp.enableAuditSelect = tsEnableAuditSelect;
19,630,857✔
864
  batchRsp.enableAuditInsert = tsEnableAuditInsert;
19,630,857✔
865
  batchRsp.auditLevel = tsAuditLevel;
19,630,857✔
866
  batchRsp.enableStrongPass = tsEnableStrongPassword;
19,630,857✔
867

868
  int32_t sz = taosArrayGetSize(batchReq.reqs);
19,630,857✔
869
  for (int i = 0; i < sz; i++) {
43,315,633✔
870
    SClientHbReq *pHbReq = taosArrayGet(batchReq.reqs, i);
23,678,550✔
871
    if (pHbReq->connKey.connType == CONN_TYPE__QUERY || pHbReq->connKey.connType == CONN_TYPE__TMQ) {
23,679,267✔
872
      TAOS_CHECK_EXIT(mndProcessQueryHeartBeat(pMnode, pReq, pHbReq, &batchRsp, &obj));
23,679,123✔
873
    } 
874
  }
875
  taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
19,637,083✔
876

877
  int32_t tlen = tSerializeSClientHbBatchRsp(NULL, 0, &batchRsp);
19,632,139✔
878
  if (tlen < 0) {
19,624,098✔
879
    TAOS_CHECK_EXIT(tlen);
×
880
  }
881
  void *buf = rpcMallocCont(tlen);
19,624,098✔
882
  if (!buf) {
19,623,497✔
883
    TAOS_CHECK_EXIT(terrno);
×
884
  }
885
  tlen = tSerializeSClientHbBatchRsp(buf, tlen, &batchRsp);
19,623,497✔
886
  if (tlen < 0) {
19,630,842✔
887
    rpcFreeCont(buf);
×
888
    TAOS_CHECK_EXIT(tlen);
×
889
  }
890
  pReq->info.rspLen = tlen;
19,630,842✔
891
  pReq->info.rsp = buf;
19,628,236✔
892
_exit:
19,631,824✔
893
  tFreeClientHbBatchRsp(&batchRsp);
894

895
  taosArrayDestroy(obj.pQnodeList);
19,634,972✔
896

897
  TAOS_RETURN(code);
19,631,023✔
898
}
899

900
static int32_t mndProcessKillQueryReq(SRpcMsg *pReq) {
2✔
901
  int32_t       code = 0;
2✔
902
  SMnode       *pMnode = pReq->info.node;
2✔
903
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
2✔
904

905
  SKillQueryReq killReq = {0};
2✔
906
  TAOS_CHECK_RETURN(tDeserializeSKillQueryReq(pReq->pCont, pReq->contLen, &killReq));
2✔
907

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

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

935
static int32_t mndProcessKillConnReq(SRpcMsg *pReq) {
153✔
936
  int32_t       code = 0;
153✔
937
  SMnode       *pMnode = pReq->info.node;
153✔
938
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
153✔
939

940
  SKillConnReq killReq = {0};
153✔
941
  TAOS_CHECK_RETURN(tDeserializeSKillConnReq(pReq->pCont, pReq->contLen, &killReq));
153✔
942

943
  TAOS_CHECK_RETURN(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_KILL_CONN));
153✔
944

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

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

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

978
  pReq->info.rspLen = contLen;
×
979
  pReq->info.rsp = pRsp;
×
980

981
_exit:
×
982

983
  TAOS_RETURN(code);
×
984
}
985

986
static int32_t mndRetrieveConns(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
11,508✔
987
  SMnode   *pMnode = pReq->info.node;
11,508✔
988
  SSdb     *pSdb = pMnode->pSdb;
11,508✔
989
  int32_t   numOfRows = 0;
11,508✔
990
  int32_t   cols = 0;
11,508✔
991
  int32_t   code = 0;
11,508✔
992
  SConnObj *pConn = NULL;
11,508✔
993

994
  if (pShow->pIter == NULL) {
11,508✔
995
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
11,508✔
996
    pShow->pIter = taosCacheCreateIter(pMgmt->connCache);
11,166✔
997
    if (!pShow->pIter) return terrno;
11,508✔
998
  }
999

1000
  while (numOfRows < rows) {
158,390✔
1001
    pConn = mndGetNextConn(pMnode, pShow->pIter);
158,390✔
1002
    if (pConn == NULL) {
159,074✔
1003
      pShow->pIter = NULL;
11,508✔
1004
      break;
11,508✔
1005
    }
1006

1007
    if ((taosGetTimestampMs() - pConn->lastAccessTimeMs) > ((int64_t)CACHE_OBJ_KEEP_TIME * 1000)) {
147,566✔
1008
      continue;
119,979✔
1009
    }
1010

1011
    cols = 0;
27,587✔
1012

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

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

1029
    char tokenName[TSDB_TOKEN_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
27,587✔
1030
    STR_TO_VARSTR(tokenName, pConn->tokenName);
27,587✔
1031
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
27,245✔
1032
    code = colDataSetVal(pColInfo, numOfRows, (const char *)tokenName, false);
27,245✔
1033
    if (code != 0) {
27,587✔
1034
      mError("failed to set token name since %s", tstrerror(code));
×
1035
      return code;
×
1036
    }
1037

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

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

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

1062
    STR_TO_VARSTR(endpoint, addr);
27,245✔
1063

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

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

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

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

1094
    char userIp[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
27,587✔
1095
    getUserIpFromConnObj(pConn, userIp);
27,587✔
1096

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

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

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

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

1133
  pShow->numOfRows += numOfRows;
11,508✔
1134
  return numOfRows;
11,508✔
1135
}
1136

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

1155
  int32_t i = offset;
7,049✔
1156
  for (; i < numOfQueries && (i - offset) < rowsToPack; ++i) {
14,098✔
1157
    int32_t     curRowIndex = pBlock->info.rows;
7,049✔
1158
    SQueryDesc *pQuery = taosArrayGet(pConn->pQueries, i);
7,049✔
1159
    cols = 0;
7,049✔
1160

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1314
    char userIp[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
7,049✔
1315
    getUserIpFromConnObj(pConn, userIp);
7,049✔
1316

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

1325
    pBlock->info.rows++;
7,049✔
1326
  }
1327

1328
  taosRUnLockLatch(&pConn->queryLock);
7,049✔
1329
  return i - offset;
7,049✔
1330
}
1331

1332
static int32_t mndRetrieveQueries(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
2,590✔
1333
  SMnode   *pMnode = pReq->info.node;
2,590✔
1334
  SSdb     *pSdb = pMnode->pSdb;
2,590✔
1335
  int32_t   numOfRows = 0;
2,590✔
1336
  SConnObj *pConn = NULL;
2,590✔
1337

1338
  if (pShow->pIter == NULL) {
2,590✔
1339
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
2,590✔
1340
    pShow->pIter = taosCacheCreateIter(pMgmt->connCache);
2,590✔
1341
    if (!pShow->pIter) return terrno;
2,590✔
1342
  }
1343

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

1354
  while (numOfRows < rows) {
20,344✔
1355
    pConn = mndGetNextConn(pMnode, pShow->pIter);
20,344✔
1356
    if (pConn == NULL) {
20,344✔
1357
      pShow->pIter = NULL;
2,590✔
1358
      break;
2,590✔
1359
    }
1360

1361
    int32_t packedRows = packQueriesIntoBlock(pShow, pConn, pBlock, 0, rows - numOfRows);
17,754✔
1362
    pShow->curIterPackedRows = packedRows;
17,754✔
1363
    numOfRows += packedRows;
17,754✔
1364
  }
1365
  pShow->numOfRows += numOfRows;
2,590✔
1366
  return numOfRows;
2,590✔
1367
}
1368

1369
static int32_t mndRetrieveApps(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
2,620✔
1370
  SMnode  *pMnode = pReq->info.node;
2,620✔
1371
  SSdb    *pSdb = pMnode->pSdb;
2,620✔
1372
  int32_t  numOfRows = 0;
2,620✔
1373
  int32_t  cols = 0;
2,620✔
1374
  SAppObj *pApp = NULL;
2,620✔
1375
  int32_t  code = 0;
2,620✔
1376

1377
  if (pShow->pIter == NULL) {
2,620✔
1378
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
2,620✔
1379
    pShow->pIter = taosCacheCreateIter(pMgmt->appCache);
2,620✔
1380
    if (!pShow->pIter) return terrno;
2,620✔
1381
  }
1382

1383
  while (numOfRows < rows) {
5,240✔
1384
    pApp = mndGetNextApp(pMnode, pShow->pIter);
5,240✔
1385
    if (pApp == NULL) {
5,240✔
1386
      pShow->pIter = NULL;
2,620✔
1387
      break;
2,620✔
1388
    }
1389

1390
    cols = 0;
2,620✔
1391

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

1399
    char ip[TD_IP_LEN + VARSTR_HEADER_SIZE] = {0};
2,620✔
1400
    char buf[IP_RESERVE_CAP] = {0};
2,620✔
1401
    snprintf(buf, sizeof(buf), "%s", IP_ADDR_STR(&pApp->cliAddr));
2,620✔
1402
    STR_TO_VARSTR(ip, buf);
2,620✔
1403

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1505
    numOfRows++;
2,620✔
1506
  }
1507

1508
  pShow->numOfRows += numOfRows;
2,620✔
1509
  return numOfRows;
2,620✔
1510
}
1511

1512
static void mndCancelGetNextQuery(SMnode *pMnode, void *pIter) {
×
1513
  if (pIter != NULL) {
×
1514
    taosCacheDestroyIter(pIter);
×
1515
  }
1516
}
×
1517

1518
int32_t mndGetNumOfConnections(SMnode *pMnode) {
65✔
1519
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
65✔
1520
  return taosCacheGetNumOfObj(pMgmt->connCache);
65✔
1521
}
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