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

taosdata / TDengine / #3796

31 Mar 2025 10:39AM UTC coverage: 30.372% (-7.1%) from 37.443%
#3796

push

travis-ci

happyguoxy
test:add test cases

69287 of 309062 branches covered (22.42%)

Branch coverage included in aggregate %.

118044 of 307720 relevant lines covered (38.36%)

278592.15 hits per line

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

38.83
/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 "audit.h"
18
#include "mndDb.h"
19
#include "mndDnode.h"
20
#include "mndMnode.h"
21
#include "mndPrivilege.h"
22
#include "mndProfile.h"
23
#include "mndQnode.h"
24
#include "mndShow.h"
25
#include "mndSma.h"
26
#include "mndStb.h"
27
#include "mndUser.h"
28
#include "mndView.h"
29
#include "tglobal.h"
30
#include "tversion.h"
31

32
typedef struct {
33
  uint32_t id;
34
  int8_t   connType;
35
  char     user[TSDB_USER_LEN];
36
  char     app[TSDB_APP_NAME_LEN];  // app name that invokes taosc
37
  int64_t  appStartTimeMs;          // app start time
38
  int32_t  pid;                     // pid of app that invokes taosc
39
  uint32_t ip;
40
  uint16_t port;
41
  int8_t   killed;
42
  int64_t  loginTimeMs;
43
  int64_t  lastAccessTimeMs;
44
  uint64_t killId;
45
  int32_t  numOfQueries;
46
  SRWLatch queryLock;
47
  SArray  *pQueries;  // SArray<SQueryDesc>
48
  char     userApp[TSDB_APP_NAME_LEN];
49
  uint32_t userIp;
50
} SConnObj;
51

52
typedef struct {
53
  int64_t            appId;
54
  uint32_t           ip;
55
  int32_t            pid;
56
  char               name[TSDB_APP_NAME_LEN];
57
  int64_t            startTime;
58
  SAppClusterSummary summary;
59
  int64_t            lastAccessTimeMs;
60
} SAppObj;
61

62
typedef struct {
63
  int32_t totalDnodes;
64
  int32_t onlineDnodes;
65
  SEpSet  epSet;
66
  SArray *pQnodeList;
67
  int64_t ipWhiteListVer;
68
} SConnPreparedObj;
69

70
#define CACHE_OBJ_KEEP_TIME 3  // s
71

72
static SConnObj *mndCreateConn(SMnode *pMnode, const char *user, int8_t connType, uint32_t ip, uint16_t port,
73
                               int32_t pid, const char *app, int64_t startTime);
74
static void      mndFreeConn(SConnObj *pConn);
75
static SConnObj *mndAcquireConn(SMnode *pMnode, uint32_t connId);
76
static void      mndReleaseConn(SMnode *pMnode, SConnObj *pConn, bool extendLifespan);
77
static void     *mndGetNextConn(SMnode *pMnode, SCacheIter *pIter);
78
static void      mndCancelGetNextConn(SMnode *pMnode, void *pIter);
79
static int32_t   mndProcessHeartBeatReq(SRpcMsg *pReq);
80
static int32_t   mndProcessConnectReq(SRpcMsg *pReq);
81
static int32_t   mndProcessKillQueryReq(SRpcMsg *pReq);
82
static int32_t   mndProcessKillConnReq(SRpcMsg *pReq);
83
static int32_t   mndRetrieveConns(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
84
static int32_t   mndRetrieveQueries(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
85
static void      mndCancelGetNextQuery(SMnode *pMnode, void *pIter);
86
static void      mndFreeApp(SAppObj *pApp);
87
static int32_t   mndRetrieveApps(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
88
static void      mndCancelGetNextApp(SMnode *pMnode, void *pIter);
89
static int32_t   mndProcessSvrVerReq(SRpcMsg *pReq);
90

91
int32_t mndInitProfile(SMnode *pMnode) {
9✔
92
  int32_t       code = 0;
9✔
93
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
9✔
94

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

104
  pMgmt->appCache = taosCacheInit(TSDB_DATA_TYPE_BIGINT, checkTime, true, (__cache_free_fn_t)mndFreeApp, "app");
9✔
105
  if (pMgmt->appCache == NULL) {
9!
106
    code = TSDB_CODE_OUT_OF_MEMORY;
×
107
    mError("failed to alloc profile cache since %s", terrstr());
×
108
    TAOS_RETURN(code);
×
109
  }
110

111
  mndSetMsgHandle(pMnode, TDMT_MND_HEARTBEAT, mndProcessHeartBeatReq);
9✔
112
  mndSetMsgHandle(pMnode, TDMT_MND_CONNECT, mndProcessConnectReq);
9✔
113
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_QUERY, mndProcessKillQueryReq);
9✔
114
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_CONN, mndProcessKillConnReq);
9✔
115
  mndSetMsgHandle(pMnode, TDMT_MND_SERVER_VERSION, mndProcessSvrVerReq);
9✔
116

117
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONNS, mndRetrieveConns);
9✔
118
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONNS, mndCancelGetNextConn);
9✔
119
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_QUERIES, mndRetrieveQueries);
9✔
120
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_QUERIES, mndCancelGetNextQuery);
9✔
121
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_APPS, mndRetrieveApps);
9✔
122
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_APPS, mndCancelGetNextApp);
9✔
123

124
  TAOS_RETURN(code);
9✔
125
}
126

127
void mndCleanupProfile(SMnode *pMnode) {
9✔
128
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
9✔
129
  if (pMgmt->connCache != NULL) {
9!
130
    taosCacheCleanup(pMgmt->connCache);
9✔
131
    pMgmt->connCache = NULL;
9✔
132
  }
133

134
  if (pMgmt->appCache != NULL) {
9!
135
    taosCacheCleanup(pMgmt->appCache);
9✔
136
    pMgmt->appCache = NULL;
9✔
137
  }
138
}
9✔
139

140
static void setUserInfo2Conn(SConnObj *connObj, char *userApp, uint32_t userIp) {
2✔
141
  if (connObj == NULL) {
2!
142
    return;
×
143
  }
144
  tstrncpy(connObj->userApp, userApp, sizeof(connObj->userApp));
2✔
145
  connObj->userIp = userIp;
2✔
146
}
147
static SConnObj *mndCreateConn(SMnode *pMnode, const char *user, int8_t connType, uint32_t ip, uint16_t port,
3✔
148
                               int32_t pid, const char *app, int64_t startTime) {
149
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
3✔
150

151
  char     connStr[255] = {0};
3✔
152
  int32_t  len = tsnprintf(connStr, sizeof(connStr), "%s%d%d%d%s", user, ip, port, pid, app);
3✔
153
  uint32_t connId = mndGenerateUid(connStr, len);
3✔
154
  if (startTime == 0) startTime = taosGetTimestampMs();
5✔
155

156
  SConnObj connObj = {
3✔
157
      .id = connId,
158
      .connType = connType,
159
      .appStartTimeMs = startTime,
160
      .pid = pid,
161
      .ip = ip,
162
      .port = port,
163
      .killed = 0,
164
      .loginTimeMs = taosGetTimestampMs(),
3✔
165
      .lastAccessTimeMs = 0,
166
      .killId = 0,
167
      .numOfQueries = 0,
168
      .pQueries = NULL,
169
  };
170

171
  connObj.lastAccessTimeMs = connObj.loginTimeMs;
3✔
172
  tstrncpy(connObj.user, user, TSDB_USER_LEN);
3✔
173
  tstrncpy(connObj.app, app, TSDB_APP_NAME_LEN);
3✔
174

175
  SConnObj *pConn =
176
      taosCachePut(pMgmt->connCache, &connId, sizeof(uint32_t), &connObj, sizeof(connObj), CACHE_OBJ_KEEP_TIME * 1000);
3✔
177
  if (pConn == NULL) {
3!
178
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
179
    mError("conn:%d, failed to put into cache since %s, user:%s", connId, user, terrstr());
×
180
    return NULL;
×
181
  } else {
182
    mTrace("conn:%u, is created, data:%p user:%s", pConn->id, pConn, user);
3✔
183
    return pConn;
3✔
184
  }
185
}
186

187
static void mndFreeConn(SConnObj *pConn) {
3✔
188
  taosWLockLatch(&pConn->queryLock);
3✔
189
  taosArrayDestroyEx(pConn->pQueries, tFreeClientHbQueryDesc);
3✔
190
  taosWUnLockLatch(&pConn->queryLock);
3✔
191

192
  mTrace("conn:%u, is destroyed, data:%p", pConn->id, pConn);
3✔
193
}
3✔
194

195
static SConnObj *mndAcquireConn(SMnode *pMnode, uint32_t connId) {
2✔
196
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
2✔
197

198
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &connId, sizeof(connId));
2✔
199
  if (pConn == NULL) {
2!
200
    mDebug("conn:%u, already destroyed", connId);
×
201
    return NULL;
×
202
  }
203

204
  pConn->lastAccessTimeMs = taosGetTimestampMs();
2✔
205
  mTrace("conn:%u, acquired from cache, data:%p", pConn->id, pConn);
2!
206
  return pConn;
2✔
207
}
208

209
static void mndReleaseConn(SMnode *pMnode, SConnObj *pConn, bool extendLifespan) {
6✔
210
  if (pConn == NULL) return;
6✔
211
  mTrace("conn:%u, released from cache, data:%p", pConn->id, pConn);
5✔
212

213
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
5✔
214
  if (extendLifespan) taosCacheTryExtendLifeSpan(pMgmt->connCache, (void **)&pConn);
5!
215
  taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
5✔
216
}
217

218
void *mndGetNextConn(SMnode *pMnode, SCacheIter *pIter) {
6✔
219
  SConnObj *pConn = NULL;
6✔
220
  bool      hasNext = taosCacheIterNext(pIter);
6✔
221
  if (hasNext) {
6✔
222
    size_t dataLen = 0;
3✔
223
    pConn = taosCacheIterGetData(pIter, &dataLen);
3✔
224
  } else {
225
    taosCacheDestroyIter(pIter);
3✔
226
  }
227

228
  return pConn;
6✔
229
}
230

231
static void mndCancelGetNextConn(SMnode *pMnode, void *pIter) {
×
232
  if (pIter != NULL) {
×
233
    taosCacheDestroyIter(pIter);
×
234
  }
235
}
×
236

237
static int32_t mndProcessConnectReq(SRpcMsg *pReq) {
4✔
238
  SMnode         *pMnode = pReq->info.node;
4✔
239
  SUserObj       *pUser = NULL;
4✔
240
  SDbObj         *pDb = NULL;
4✔
241
  SConnObj       *pConn = NULL;
4✔
242
  int32_t         code = 0;
4✔
243
  SConnectReq     connReq = {0};
4✔
244
  char            ip[TD_IP_LEN] = {0};
4✔
245
  const STraceId *trace = &pReq->info.traceId;
4✔
246

247
  if ((code = tDeserializeSConnectReq(pReq->pCont, pReq->contLen, &connReq)) != 0) {
4!
248
    goto _OVER;
×
249
  }
250

251
  if ((code = taosCheckVersionCompatibleFromStr(connReq.sVer, td_version, 3)) != 0) {
4!
252
    mGError("version not compatible. client version: %s, server version: %s", connReq.sVer, td_version);
×
253
    goto _OVER;
×
254
  }
255

256
  taosInetNtoa(ip, pReq->info.conn.clientIp);
4✔
257
  if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CONNECT)) != 0) {
4!
258
    mGError("user:%s, failed to login from %s since %s", pReq->info.conn.user, ip, tstrerror(code));
×
259
    goto _OVER;
×
260
  }
261

262
  code = mndAcquireUser(pMnode, pReq->info.conn.user, &pUser);
4✔
263
  if (pUser == NULL) {
4!
264
    mGError("user:%s, failed to login from %s while acquire user since %s", pReq->info.conn.user, ip, tstrerror(code));
×
265
    goto _OVER;
×
266
  }
267

268
  if (strncmp(connReq.passwd, pUser->pass, TSDB_PASSWORD_LEN - 1) != 0 && !tsMndSkipGrant) {
4!
269
    mGError("user:%s, failed to login from %s since invalid pass, input:%s", pReq->info.conn.user, ip, connReq.passwd);
×
270
    code = TSDB_CODE_MND_AUTH_FAILURE;
×
271
    goto _OVER;
×
272
  }
273

274
  if (connReq.db[0]) {
4✔
275
    char db[TSDB_DB_FNAME_LEN] = {0};
1✔
276
    (void)snprintf(db, TSDB_DB_FNAME_LEN, "%d%s%s", pUser->acctId, TS_PATH_DELIMITER, connReq.db);
1✔
277
    pDb = mndAcquireDb(pMnode, db);
1✔
278
    if (pDb == NULL) {
1!
279
      if (0 != strcmp(connReq.db, TSDB_INFORMATION_SCHEMA_DB) &&
1!
280
          (0 != strcmp(connReq.db, TSDB_PERFORMANCE_SCHEMA_DB))) {
1!
281
        code = TSDB_CODE_MND_DB_NOT_EXIST;
1✔
282
        mGError("user:%s, failed to login from %s while use db:%s since %s", pReq->info.conn.user, ip, connReq.db,
1!
283
                tstrerror(code));
284
        goto _OVER;
1✔
285
      }
286
    }
287

288
    TAOS_CHECK_GOTO(mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_READ_OR_WRITE_DB, pDb), NULL, _OVER);
×
289
  }
290

291
  pConn = mndCreateConn(pMnode, pReq->info.conn.user, connReq.connType, pReq->info.conn.clientIp,
3✔
292
                        pReq->info.conn.clientPort, connReq.pid, connReq.app, connReq.startTime);
3✔
293
  if (pConn == NULL) {
3!
294
    code = terrno;
×
295
    mGError("user:%s, failed to login from %s while create connection since %s", pReq->info.conn.user, ip,
×
296
            tstrerror(code));
297
    goto _OVER;
×
298
  }
299

300
  SConnectRsp connectRsp = {0};
3✔
301
  connectRsp.acctId = pUser->acctId;
3✔
302
  connectRsp.superUser = pUser->superUser;
3✔
303
  connectRsp.sysInfo = pUser->sysInfo;
3✔
304
  connectRsp.clusterId = pMnode->clusterId;
3✔
305
  connectRsp.connId = pConn->id;
3✔
306
  connectRsp.connType = connReq.connType;
3✔
307
  connectRsp.dnodeNum = mndGetDnodeSize(pMnode);
3✔
308
  connectRsp.svrTimestamp = taosGetTimestampSec();
3✔
309
  connectRsp.passVer = pUser->passVersion;
3✔
310
  connectRsp.authVer = pUser->authVersion;
3✔
311
  connectRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
3✔
312
  connectRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
3✔
313
  connectRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
3✔
314
  connectRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
3✔
315
  connectRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
3✔
316
  connectRsp.enableAuditDelete = tsEnableAuditDelete;
3✔
317
  tstrncpy(connectRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
3✔
318
  connectRsp.whiteListVer = pUser->ipWhiteListVer;
3✔
319

320
  tstrncpy(connectRsp.sVer, td_version, sizeof(connectRsp.sVer));
3✔
321
  (void)snprintf(connectRsp.sDetailVer, sizeof(connectRsp.sDetailVer), "ver:%s\nbuild:%s\ngitinfo:%s", td_version,
3✔
322
                 td_buildinfo, td_gitinfo);
323
  mndGetMnodeEpSet(pMnode, &connectRsp.epSet);
3✔
324

325
  int32_t contLen = tSerializeSConnectRsp(NULL, 0, &connectRsp);
3✔
326
  if (contLen < 0) {
3!
327
    TAOS_CHECK_GOTO(contLen, NULL, _OVER);
×
328
  }
329
  void *pRsp = rpcMallocCont(contLen);
3✔
330
  if (pRsp == NULL) {
3!
331
    TAOS_CHECK_GOTO(terrno, NULL, _OVER);
×
332
  }
333

334
  contLen = tSerializeSConnectRsp(pRsp, contLen, &connectRsp);
3✔
335
  if (contLen < 0) {
3!
336
    rpcFreeCont(pRsp);
×
337
    TAOS_CHECK_GOTO(contLen, NULL, _OVER);
×
338
  }
339

340
  pReq->info.rspLen = contLen;
3✔
341
  pReq->info.rsp = pRsp;
3✔
342

343
  mGDebug("user:%s, login from %s:%d, conn:%u, app:%s", pReq->info.conn.user, ip, pConn->port, pConn->id, connReq.app);
3!
344

345
  code = 0;
3✔
346

347
  char    detail[1000] = {0};
3✔
348
  int32_t nBytes = snprintf(detail, sizeof(detail), "app:%s", connReq.app);
3✔
349
  if ((uint32_t)nBytes < sizeof(detail)) {
3!
350
    auditRecord(pReq, pMnode->clusterId, "login", "", "", detail, strlen(detail));
3✔
351
  } else {
352
    mError("failed to audit logic since %s", tstrerror(TSDB_CODE_OUT_OF_RANGE));
×
353
  }
354

355
_OVER:
×
356

357
  mndReleaseUser(pMnode, pUser);
4✔
358
  mndReleaseDb(pMnode, pDb);
4✔
359
  mndReleaseConn(pMnode, pConn, true);
4✔
360

361
  TAOS_RETURN(code);
4✔
362
}
363

364
static int32_t mndSaveQueryList(SConnObj *pConn, SQueryHbReqBasic *pBasic) {
2✔
365
  taosWLockLatch(&pConn->queryLock);
2✔
366

367
  taosArrayDestroyEx(pConn->pQueries, tFreeClientHbQueryDesc);
2✔
368

369
  pConn->pQueries = pBasic->queryDesc;
2✔
370
  pConn->numOfQueries = pBasic->queryDesc ? taosArrayGetSize(pBasic->queryDesc) : 0;
2!
371
  pBasic->queryDesc = NULL;
2✔
372

373
  mDebug("queries updated in conn %u, num:%d", pConn->id, pConn->numOfQueries);
2!
374

375
  taosWUnLockLatch(&pConn->queryLock);
2✔
376

377
  return TSDB_CODE_SUCCESS;
2✔
378
}
379

380
static SAppObj *mndCreateApp(SMnode *pMnode, uint32_t clientIp, SAppHbReq *pReq) {
1✔
381
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
1✔
382

383
  SAppObj app;
384
  app.appId = pReq->appId;
1✔
385
  app.ip = clientIp;
1✔
386
  app.pid = pReq->pid;
1✔
387
  tstrncpy(app.name, pReq->name, sizeof(app.name));
1✔
388
  app.startTime = pReq->startTime;
1✔
389
  (void)memcpy(&app.summary, &pReq->summary, sizeof(pReq->summary));
1✔
390
  app.lastAccessTimeMs = taosGetTimestampMs();
1✔
391

392
  SAppObj *pApp =
393
      taosCachePut(pMgmt->appCache, &pReq->appId, sizeof(pReq->appId), &app, sizeof(app), CACHE_OBJ_KEEP_TIME * 1000);
1✔
394
  if (pApp == NULL) {
1!
395
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
396
    mError("failed to app %" PRIx64 " into cache since %s", pReq->appId, terrstr());
×
397
    return NULL;
×
398
  }
399

400
  mTrace("app %" PRIx64 " is put into cache", pReq->appId);
1!
401
  return pApp;
1✔
402
}
403

404
static void mndFreeApp(SAppObj *pApp) { mTrace("app %" PRIx64 " is destroyed", pApp->appId); }
1!
405

406
static SAppObj *mndAcquireApp(SMnode *pMnode, int64_t appId) {
2✔
407
  terrno = 0;
2✔
408
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
2✔
409

410
  SAppObj *pApp = taosCacheAcquireByKey(pMgmt->appCache, &appId, sizeof(appId));
2✔
411
  if (pApp == NULL) {
2✔
412
    mDebug("app %" PRIx64 " not in cache", appId);
1!
413
    return NULL;
1✔
414
  }
415

416
  pApp->lastAccessTimeMs = (uint64_t)taosGetTimestampMs();
1✔
417

418
  mTrace("app %" PRIx64 " acquired from cache", appId);
1!
419
  return pApp;
1✔
420
}
421

422
static void mndReleaseApp(SMnode *pMnode, SAppObj *pApp) {
2✔
423
  if (pApp == NULL) return;
2!
424
  mTrace("release app %" PRIx64 " to cache", pApp->appId);
2!
425

426
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
2✔
427
  taosCacheRelease(pMgmt->appCache, (void **)&pApp, false);
2✔
428
}
429

430
SAppObj *mndGetNextApp(SMnode *pMnode, SCacheIter *pIter) {
×
431
  SAppObj *pApp = NULL;
×
432
  bool     hasNext = taosCacheIterNext(pIter);
×
433
  if (hasNext) {
×
434
    size_t dataLen = 0;
×
435
    pApp = taosCacheIterGetData(pIter, &dataLen);
×
436
  } else {
437
    taosCacheDestroyIter(pIter);
×
438
  }
439

440
  return pApp;
×
441
}
442

443
static void mndCancelGetNextApp(SMnode *pMnode, void *pIter) {
×
444
  if (pIter != NULL) {
×
445
    taosCacheDestroyIter(pIter);
×
446
  }
447
}
×
448

449
static SClientHbRsp *mndMqHbBuildRsp(SMnode *pMnode, SClientHbReq *pReq) {
1✔
450
  //
451
  return NULL;
1✔
452
}
453

454
static int32_t mndUpdateAppInfo(SMnode *pMnode, SClientHbReq *pHbReq, SRpcConnInfo *connInfo) {
2✔
455
  int32_t    code = 0;
2✔
456
  SAppHbReq *pReq = &pHbReq->app;
2✔
457
  SAppObj   *pApp = mndAcquireApp(pMnode, pReq->appId);
2✔
458
  if (pApp == NULL) {
2✔
459
    pApp = mndCreateApp(pMnode, connInfo->clientIp, pReq);
1✔
460
    if (pApp == NULL) {
1!
461
      mError("failed to create new app %" PRIx64 " since %s", pReq->appId, terrstr());
×
462
      code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
463
      if (terrno != 0) code = terrno;
×
464
      TAOS_RETURN(code);
×
465
    } else {
466
      mDebug("a new app %" PRIx64 " is created", pReq->appId);
1!
467
      mndReleaseApp(pMnode, pApp);
1✔
468
      return TSDB_CODE_SUCCESS;
1✔
469
    }
470
  }
471

472
  (void)memcpy(&pApp->summary, &pReq->summary, sizeof(pReq->summary));
1✔
473

474
  mndReleaseApp(pMnode, pApp);
1✔
475

476
  return TSDB_CODE_SUCCESS;
1✔
477
}
478

479
static int32_t mndGetOnlineDnodeNum(SMnode *pMnode, int32_t *num) {
3✔
480
  SSdb      *pSdb = pMnode->pSdb;
3✔
481
  SDnodeObj *pDnode = NULL;
3✔
482
  int64_t    curMs = taosGetTimestampMs();
3✔
483
  void      *pIter = NULL;
3✔
484

485
  while (true) {
3✔
486
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
6✔
487
    if (pIter == NULL) break;
6✔
488

489
    bool online = mndIsDnodeOnline(pDnode, curMs);
3✔
490
    if (online) {
3!
491
      (*num)++;
3✔
492
    }
493

494
    sdbRelease(pSdb, pDnode);
3✔
495
  }
496

497
  return TSDB_CODE_SUCCESS;
3✔
498
}
499

500
static int32_t mndProcessQueryHeartBeat(SMnode *pMnode, SRpcMsg *pMsg, SClientHbReq *pHbReq,
2✔
501
                                        SClientHbBatchRsp *pBatchRsp, SConnPreparedObj *pObj) {
502
  int32_t       code = 0;
2✔
503
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
2✔
504
  SClientHbRsp  hbRsp = {.connKey = pHbReq->connKey, .status = 0, .info = NULL, .query = NULL};
2✔
505
  SRpcConnInfo  connInfo = pMsg->info.conn;
2✔
506

507
  if (0 != pHbReq->app.appId) {
2!
508
    TAOS_CHECK_RETURN(mndUpdateAppInfo(pMnode, pHbReq, &connInfo));
2!
509
  }
510

511
  if (pHbReq->query) {
2!
512
    SQueryHbReqBasic *pBasic = pHbReq->query;
2✔
513

514
    SConnObj *pConn = mndAcquireConn(pMnode, pBasic->connId);
2✔
515
    if (pConn == NULL) {
2!
516
      pConn = mndCreateConn(pMnode, connInfo.user, CONN_TYPE__QUERY, connInfo.clientIp, connInfo.clientPort,
×
517
                            pHbReq->app.pid, pHbReq->app.name, 0);
×
518
      if (pConn == NULL) {
×
519
        mError("user:%s, conn:%u is freed and failed to create new since %s", connInfo.user, pBasic->connId, terrstr());
×
520
        code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
521
        if (terrno != 0) code = terrno;
×
522
        TAOS_RETURN(code);
×
523
      } else {
524
        mDebug("user:%s, conn:%u is freed, will create a new conn:%u", connInfo.user, pBasic->connId, pConn->id);
×
525
      }
526
    }
527

528
    setUserInfo2Conn(pConn, pHbReq->userApp, pHbReq->userIp);
2✔
529
    SQueryHbRspBasic *rspBasic = taosMemoryCalloc(1, sizeof(SQueryHbRspBasic));
2!
530
    if (rspBasic == NULL) {
2!
531
      mndReleaseConn(pMnode, pConn, true);
×
532
      code = terrno;
×
533
      mError("user:%s, conn:%u failed to process hb while since %s", pConn->user, pBasic->connId, terrstr());
×
534
      TAOS_RETURN(code);
×
535
    }
536

537
    TAOS_CHECK_RETURN(mndSaveQueryList(pConn, pBasic));
2!
538
    if (pConn->killed != 0) {
2!
539
      rspBasic->killConnection = 1;
×
540
    }
541

542
    if (pConn->killId != 0) {
2!
543
      rspBasic->killRid = pConn->killId;
×
544
      pConn->killId = 0;
×
545
    }
546

547
    rspBasic->connId = pConn->id;
2✔
548
    rspBasic->connId = pConn->id;
2✔
549
    rspBasic->totalDnodes = pObj->totalDnodes;
2✔
550
    rspBasic->onlineDnodes = pObj->onlineDnodes;
2✔
551
    rspBasic->epSet = pObj->epSet;
2✔
552
    rspBasic->pQnodeList = taosArrayDup(pObj->pQnodeList, NULL);
2✔
553

554
    mndReleaseConn(pMnode, pConn, true);
2✔
555

556
    hbRsp.query = rspBasic;
2✔
557
  } else {
558
    mDebug("no query info in hb msg");
×
559
  }
560

561
  int32_t kvNum = taosHashGetSize(pHbReq->info);
2✔
562
  if (NULL == pHbReq->info || kvNum <= 0) {
2!
563
    if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
×
564
      mError("failed to put rsp into array, but continue at this heartbeat");
×
565
    }
566
    return TSDB_CODE_SUCCESS;
×
567
  }
568

569
  hbRsp.info = taosArrayInit(kvNum, sizeof(SKv));
2✔
570
  if (NULL == hbRsp.info) {
2!
571
    mError("taosArrayInit %d rsp kv failed", kvNum);
×
572
    code = terrno;
×
573
    tFreeClientHbRsp(&hbRsp);
574
    TAOS_RETURN(code);
×
575
  }
576

577
#ifdef TD_ENTERPRISE
578
  bool             needCheck = true;
2✔
579
  int32_t          key = HEARTBEAT_KEY_DYN_VIEW;
2✔
580
  SDynViewVersion *pDynViewVer = NULL;
2✔
581
  SKv             *pKv = taosHashGet(pHbReq->info, &key, sizeof(key));
2✔
582
  if (NULL != pKv) {
2!
583
    pDynViewVer = pKv->value;
×
584
    mTrace("recv view dyn ver, bootTs:%" PRId64 ", ver:%" PRIu64, pDynViewVer->svrBootTs, pDynViewVer->dynViewVer);
×
585

586
    SDynViewVersion *pRspVer = NULL;
×
587
    if (0 != (code = mndValidateDynViewVersion(pMnode, pDynViewVer, &needCheck, &pRspVer))) {
×
588
      TAOS_RETURN(code);
×
589
    }
590

591
    if (needCheck) {
×
592
      SKv kv1 = {.key = HEARTBEAT_KEY_DYN_VIEW, .valueLen = sizeof(*pDynViewVer), .value = pRspVer};
×
593
      if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
×
594
        if (terrno != 0) code = terrno;
×
595
        TAOS_RETURN(code);
×
596
      };
597
      mTrace("need to check view ver, lastest bootTs:%" PRId64 ", ver:%" PRIu64, pRspVer->svrBootTs,
×
598
             pRspVer->dynViewVer);
599
    }
600
  }
601
#endif
602

603
  void *pIter = taosHashIterate(pHbReq->info, NULL);
2✔
604
  while (pIter != NULL) {
5✔
605
    SKv *kv = pIter;
3✔
606

607
    switch (kv->key) {
3!
608
      case HEARTBEAT_KEY_USER_AUTHINFO: {
2✔
609
        void   *rspMsg = NULL;
2✔
610
        int32_t rspLen = 0;
2✔
611
        (void)mndValidateUserAuthInfo(pMnode, kv->value, kv->valueLen / sizeof(SUserAuthVersion), &rspMsg, &rspLen,
2✔
612
                                      pObj->ipWhiteListVer);
613
        if (rspMsg && rspLen > 0) {
2!
614
          SKv kv1 = {.key = HEARTBEAT_KEY_USER_AUTHINFO, .valueLen = rspLen, .value = rspMsg};
1✔
615
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
2!
616
            mError("failed to put kv into array, but continue at this heartbeat");
×
617
          }
618
        }
619
        break;
2✔
620
      }
621
      case HEARTBEAT_KEY_DBINFO: {
×
622
        void   *rspMsg = NULL;
×
623
        int32_t rspLen = 0;
×
624
        (void)mndValidateDbInfo(pMnode, kv->value, kv->valueLen / sizeof(SDbCacheInfo), &rspMsg, &rspLen);
×
625
        if (rspMsg && rspLen > 0) {
×
626
          SKv kv1 = {.key = HEARTBEAT_KEY_DBINFO, .valueLen = rspLen, .value = rspMsg};
×
627
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
×
628
            mError("failed to put kv into array, but continue at this heartbeat");
×
629
          }
630
        }
631
        break;
×
632
      }
633
      case HEARTBEAT_KEY_STBINFO: {
1✔
634
        void   *rspMsg = NULL;
1✔
635
        int32_t rspLen = 0;
1✔
636
        (void)mndValidateStbInfo(pMnode, kv->value, kv->valueLen / sizeof(SSTableVersion), &rspMsg, &rspLen);
1✔
637
        if (rspMsg && rspLen > 0) {
1!
638
          SKv kv1 = {.key = HEARTBEAT_KEY_STBINFO, .valueLen = rspLen, .value = rspMsg};
1✔
639
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
2!
640
            mError("failed to put kv into array, but continue at this heartbeat");
×
641
          }
642
        }
643
        break;
1✔
644
      }
645
#ifdef TD_ENTERPRISE
646
      case HEARTBEAT_KEY_DYN_VIEW: {
×
647
        break;
×
648
      }
649
      case HEARTBEAT_KEY_VIEWINFO: {
×
650
        if (!needCheck) {
×
651
          break;
×
652
        }
653

654
        void   *rspMsg = NULL;
×
655
        int32_t rspLen = 0;
×
656
        (void)mndValidateViewInfo(pMnode, kv->value, kv->valueLen / sizeof(SViewVersion), &rspMsg, &rspLen);
×
657
        if (rspMsg && rspLen > 0) {
×
658
          SKv kv1 = {.key = HEARTBEAT_KEY_VIEWINFO, .valueLen = rspLen, .value = rspMsg};
×
659
          if (taosArrayPush(hbRsp.info, &kv1) == NULL) {
×
660
            mError("failed to put kv into array, but continue at this heartbeat");
×
661
          }
662
        }
663
        break;
×
664
      }
665
#endif
666
      case HEARTBEAT_KEY_TSMA: {
×
667
        void   *rspMsg = NULL;
×
668
        int32_t rspLen = 0;
×
669
        (void)mndValidateTSMAInfo(pMnode, kv->value, kv->valueLen / sizeof(STSMAVersion), &rspMsg, &rspLen);
×
670
        if (rspMsg && rspLen > 0) {
×
671
          SKv kv = {.key = HEARTBEAT_KEY_TSMA, .valueLen = rspLen, .value = rspMsg};
×
672
          if (taosArrayPush(hbRsp.info, &kv) == NULL) {
×
673
            mError("failed to put kv into array, but continue at this heartbeat");
×
674
          }
675
        }
676
        break;
×
677
      }
678
      default:
×
679
        mError("invalid kv key:%d", kv->key);
×
680
        hbRsp.status = TSDB_CODE_APP_ERROR;
×
681
        break;
×
682
    }
683

684
    pIter = taosHashIterate(pHbReq->info, pIter);
3✔
685
  }
686

687
  if (taosArrayPush(pBatchRsp->rsps, &hbRsp) == NULL) {
4!
688
    if (terrno != 0) code = terrno;
×
689
  }
690
  TAOS_RETURN(code);
2✔
691
}
692

693
static int32_t mndProcessHeartBeatReq(SRpcMsg *pReq) {
3✔
694
  int32_t code = 0;
3✔
695
  int32_t lino = 0;
3✔
696
  SMnode *pMnode = pReq->info.node;
3✔
697

698
  SClientHbBatchReq batchReq = {0};
3✔
699
  if (tDeserializeSClientHbBatchReq(pReq->pCont, pReq->contLen, &batchReq) != 0) {
3!
700
    taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
×
701
    code = TSDB_CODE_INVALID_MSG;
×
702
    TAOS_RETURN(code);
×
703
  }
704

705
  SConnPreparedObj obj = {0};
3✔
706
  obj.totalDnodes = mndGetDnodeSize(pMnode);
3✔
707
  obj.ipWhiteListVer = batchReq.ipWhiteList;
3✔
708
  TAOS_CHECK_RETURN(mndGetOnlineDnodeNum(pMnode, &obj.onlineDnodes));
3!
709
  mndGetMnodeEpSet(pMnode, &obj.epSet);
3✔
710
  TAOS_CHECK_RETURN(mndCreateQnodeList(pMnode, &obj.pQnodeList, -1));
3!
711

712
  SClientHbBatchRsp batchRsp = {0};
3✔
713
  batchRsp.svrTimestamp = taosGetTimestampSec();
3✔
714
  batchRsp.rsps = taosArrayInit(0, sizeof(SClientHbRsp));
3✔
715
  if (batchRsp.rsps == NULL) {
3!
716
    TAOS_CHECK_EXIT(terrno);
×
717
  }
718
  batchRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
3✔
719
  batchRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
3✔
720
  batchRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
3✔
721
  tstrncpy(batchRsp.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
3✔
722
  batchRsp.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
3✔
723
  batchRsp.monitorParas.tsSlowLogScope = tsSlowLogScope;
3✔
724
  batchRsp.enableAuditDelete = tsEnableAuditDelete;
3✔
725
  batchRsp.enableStrongPass = tsEnableStrongPassword;
3✔
726

727
  int32_t sz = taosArrayGetSize(batchReq.reqs);
3✔
728
  for (int i = 0; i < sz; i++) {
6✔
729
    SClientHbReq *pHbReq = taosArrayGet(batchReq.reqs, i);
3✔
730
    if (pHbReq->connKey.connType == CONN_TYPE__QUERY) {
3✔
731
      TAOS_CHECK_EXIT(mndProcessQueryHeartBeat(pMnode, pReq, pHbReq, &batchRsp, &obj));
2!
732
    } else if (pHbReq->connKey.connType == CONN_TYPE__TMQ) {
1!
733
      SClientHbRsp *pRsp = mndMqHbBuildRsp(pMnode, pHbReq);
1✔
734
      if (pRsp != NULL) {
1!
735
        if (taosArrayPush(batchRsp.rsps, pRsp) == NULL) {
×
736
          mError("failed to put kv into array, but continue at this heartbeat");
×
737
        }
738
        taosMemoryFree(pRsp);
×
739
      }
740
    }
741
  }
742
  taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
3✔
743

744
  int32_t tlen = tSerializeSClientHbBatchRsp(NULL, 0, &batchRsp);
3✔
745
  if (tlen < 0) {
3!
746
    TAOS_CHECK_EXIT(tlen);
×
747
  }
748
  void *buf = rpcMallocCont(tlen);
3✔
749
  if (!buf) {
3!
750
    TAOS_CHECK_EXIT(terrno);
×
751
  }
752
  tlen = tSerializeSClientHbBatchRsp(buf, tlen, &batchRsp);
3✔
753
  if (tlen < 0) {
3!
754
    rpcFreeCont(buf);
×
755
    TAOS_CHECK_EXIT(tlen);
×
756
  }
757
  pReq->info.rspLen = tlen;
3✔
758
  pReq->info.rsp = buf;
3✔
759
_exit:
3✔
760
  tFreeClientHbBatchRsp(&batchRsp);
761

762
  taosArrayDestroy(obj.pQnodeList);
3✔
763

764
  TAOS_RETURN(code);
3✔
765
}
766

767
static int32_t mndProcessKillQueryReq(SRpcMsg *pReq) {
1✔
768
  int32_t       code = 0;
1✔
769
  SMnode       *pMnode = pReq->info.node;
1✔
770
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
1✔
771

772
  SKillQueryReq killReq = {0};
1✔
773
  TAOS_CHECK_RETURN(tDeserializeSKillQueryReq(pReq->pCont, pReq->contLen, &killReq));
1!
774

775
  mInfo("kill query msg is received, queryId:%s", killReq.queryStrId);
1!
776
  TAOS_CHECK_RETURN(mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_KILL_QUERY));
1!
777

778
  int32_t  connId = 0;
1✔
779
  uint64_t queryId = 0;
1✔
780
  char    *p = strchr(killReq.queryStrId, ':');
1✔
781
  if (NULL == p) {
1!
782
    mError("invalid QID:%s", killReq.queryStrId);
×
783
    code = TSDB_CODE_MND_INVALID_QUERY_ID;
×
784
    TAOS_RETURN(code);
×
785
  }
786
  *p = 0;
1✔
787
  connId = taosStr2Int32(killReq.queryStrId, NULL, 16);
1✔
788
  queryId = taosStr2UInt64(p + 1, NULL, 16);
1✔
789

790
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &connId, sizeof(int32_t));
1✔
791
  if (pConn == NULL) {
1!
792
    mError("connId:%x, failed to kill queryId:%" PRIx64 ", conn not exist", connId, queryId);
1!
793
    code = TSDB_CODE_MND_INVALID_CONN_ID;
1✔
794
    TAOS_RETURN(code);
1✔
795
  } else {
796
    mInfo("connId:%x, queryId:%" PRIx64 " is killed by user:%s", connId, queryId, pReq->info.conn.user);
×
797
    pConn->killId = queryId;
×
798
    taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
×
799
    TAOS_RETURN(code);
×
800
  }
801
}
802

803
static int32_t mndProcessKillConnReq(SRpcMsg *pReq) {
1✔
804
  int32_t       code = 0;
1✔
805
  SMnode       *pMnode = pReq->info.node;
1✔
806
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
1✔
807

808
  SKillConnReq killReq = {0};
1✔
809
  TAOS_CHECK_RETURN(tDeserializeSKillConnReq(pReq->pCont, pReq->contLen, &killReq));
1!
810

811
  TAOS_CHECK_RETURN(mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_KILL_CONN));
1!
812

813
  SConnObj *pConn = taosCacheAcquireByKey(pMgmt->connCache, &killReq.connId, sizeof(uint32_t));
1✔
814
  if (pConn == NULL) {
1!
815
    mError("connId:%u, failed to kill connection, conn not exist", killReq.connId);
1!
816
    code = TSDB_CODE_MND_INVALID_CONN_ID;
1✔
817
    TAOS_RETURN(code);
1✔
818
  } else {
819
    mInfo("connId:%u, is killed by user:%s", killReq.connId, pReq->info.conn.user);
×
820
    pConn->killed = 1;
×
821
    taosCacheRelease(pMgmt->connCache, (void **)&pConn, false);
×
822
    TAOS_RETURN(code);
×
823
  }
824
}
825

826
static int32_t mndProcessSvrVerReq(SRpcMsg *pReq) {
×
827
  int32_t       code = 0;
×
828
  int32_t       lino = 0;
×
829
  SServerVerRsp rsp = {0};
×
830
  tstrncpy(rsp.ver, td_version, sizeof(rsp.ver));
×
831

832
  int32_t contLen = tSerializeSServerVerRsp(NULL, 0, &rsp);
×
833
  if (contLen < 0) {
×
834
    TAOS_CHECK_EXIT(contLen);
×
835
  }
836
  void *pRsp = rpcMallocCont(contLen);
×
837
  if (pRsp == NULL) {
×
838
    TAOS_CHECK_EXIT(terrno);
×
839
  }
840
  contLen = tSerializeSServerVerRsp(pRsp, contLen, &rsp);
×
841
  if (contLen < 0) {
×
842
    rpcFreeCont(pRsp);
×
843
    TAOS_CHECK_EXIT(contLen);
×
844
  }
845

846
  pReq->info.rspLen = contLen;
×
847
  pReq->info.rsp = pRsp;
×
848

849
_exit:
×
850

851
  TAOS_RETURN(code);
×
852
}
853

854
static int32_t mndRetrieveConns(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
2✔
855
  SMnode   *pMnode = pReq->info.node;
2✔
856
  SSdb     *pSdb = pMnode->pSdb;
2✔
857
  int32_t   numOfRows = 0;
2✔
858
  int32_t   cols = 0;
2✔
859
  int32_t   code = 0;
2✔
860
  SConnObj *pConn = NULL;
2✔
861

862
  if (pShow->pIter == NULL) {
2!
863
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
2✔
864
    pShow->pIter = taosCacheCreateIter(pMgmt->connCache);
2✔
865
    if (!pShow->pIter) return terrno;
2!
866
  }
867

868
  while (numOfRows < rows) {
4!
869
    pConn = mndGetNextConn(pMnode, pShow->pIter);
4✔
870
    if (pConn == NULL) {
4✔
871
      pShow->pIter = NULL;
2✔
872
      break;
2✔
873
    }
874

875
    if ((taosGetTimestampMs() - pConn->lastAccessTimeMs) > ((int64_t)CACHE_OBJ_KEEP_TIME * 1000)) {
2!
876
      continue;
×
877
    }
878

879
    cols = 0;
2✔
880

881
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2✔
882
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->id, false);
2✔
883
    if (code != 0) {
2!
884
      mError("failed to set conn id:%u since %s", pConn->id, tstrerror(code));
×
885
      return code;
×
886
    }
887

888
    char user[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
2✔
889
    STR_TO_VARSTR(user, pConn->user);
2✔
890
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2✔
891
    code = colDataSetVal(pColInfo, numOfRows, (const char *)user, false);
2✔
892
    if (code != 0) {
2!
893
      mError("failed to set user since %s", tstrerror(code));
×
894
      return code;
×
895
    }
896

897
    char app[TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE];
898
    STR_TO_VARSTR(app, pConn->app);
2✔
899
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2✔
900
    code = colDataSetVal(pColInfo, numOfRows, (const char *)app, false);
2✔
901
    if (code != 0) {
2!
902
      mError("failed to set app since %s", tstrerror(code));
×
903
      return code;
×
904
    }
905

906
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2✔
907
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->pid, false);
2✔
908
    if (code != 0) {
2!
909
      mError("failed to set conn id:%u since %s", pConn->id, tstrerror(code));
×
910
      return code;
×
911
    }
912

913
    char endpoint[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
2✔
914
    taosInetNtoa(varDataVal(endpoint), pConn->ip);
2✔
915
    (void)tsnprintf(varDataVal(endpoint) + strlen(varDataVal(endpoint)),
2✔
916
              sizeof(endpoint) - VARSTR_HEADER_SIZE - strlen(varDataVal(endpoint)), ":%d", pConn->port);
2✔
917
    varDataLen(endpoint) = strlen(varDataVal(endpoint));
2✔
918
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2✔
919
    code = colDataSetVal(pColInfo, numOfRows, (const char *)endpoint, false);
2✔
920
    if (code != 0) {
2!
921
      mError("failed to set endpoint since %s", tstrerror(code));
×
922
      return code;
×
923
    }
924

925
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2✔
926
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->loginTimeMs, false);
2✔
927
    if (code != 0) {
2!
928
      mError("failed to set login time since %s", tstrerror(code));
×
929
      return code;
×
930
    }
931

932
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2✔
933
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pConn->lastAccessTimeMs, false);
2✔
934
    if (code != 0) {
2!
935
      mError("failed to set last access time since %s", tstrerror(code));
×
936
      return code;
×
937
    }
938

939
    char userApp[TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE];
940
    STR_TO_VARSTR(userApp, pConn->userApp);
2✔
941
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2✔
942
    code = colDataSetVal(pColInfo, numOfRows, (const char *)userApp, false);
2✔
943
    if (code != 0) {
2!
944
      mError("failed to set user app since %s", tstrerror(code));
×
945
      return code;
×
946
    }
947

948
    char userIp[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
2✔
949
    if (pConn->userIp != 0 && pConn->userIp != INADDR_NONE) {
2!
950
      taosInetNtoa(varDataVal(userIp), pConn->userIp);
×
951
      varDataLen(userIp) = strlen(varDataVal(userIp));
×
952
    }
953
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2✔
954
    code = colDataSetVal(pColInfo, numOfRows, (const char *)userIp, false);
2✔
955
    if (code != 0) {
2!
956
      mError("failed to set user ip since %s", tstrerror(code));
×
957
      return code;
×
958
    }
959

960
    numOfRows++;
2✔
961
  }
962

963
  pShow->numOfRows += numOfRows;
2✔
964
  return numOfRows;
2✔
965
}
966

967
/**
968
 * @param pConn the conn queries pack from
969
 * @param[out] pBlock the block data packed into
970
 * @param offset skip [offset] queries in pConn
971
 * @param rowsToPack at most rows to pack
972
 * @return rows packed
973
 */
974
static int32_t packQueriesIntoBlock(SShowObj *pShow, SConnObj *pConn, SSDataBlock *pBlock, uint32_t offset,
1✔
975
                                    uint32_t rowsToPack) {
976
  int32_t cols = 0;
1✔
977
  int32_t code = 0;
1✔
978
  taosRLockLatch(&pConn->queryLock);
1✔
979
  int32_t numOfQueries = taosArrayGetSize(pConn->pQueries);
1✔
980
  if (NULL == pConn->pQueries || numOfQueries <= offset) {
1!
981
    taosRUnLockLatch(&pConn->queryLock);
1✔
982
    return 0;
1✔
983
  }
984

985
  int32_t i = offset;
×
986
  for (; i < numOfQueries && (i - offset) < rowsToPack; ++i) {
×
987
    int32_t     curRowIndex = pBlock->info.rows;
×
988
    SQueryDesc *pQuery = taosArrayGet(pConn->pQueries, i);
×
989
    cols = 0;
×
990

991
    char queryId[26 + VARSTR_HEADER_SIZE] = {0};
×
992
    (void)tsnprintf(&queryId[VARSTR_HEADER_SIZE], sizeof(queryId) - VARSTR_HEADER_SIZE, "%x:%" PRIx64, pConn->id,
×
993
              pQuery->reqRid);
994
    varDataLen(queryId) = strlen(&queryId[VARSTR_HEADER_SIZE]);
×
995
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
996
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)queryId, false);
×
997
    if (code != 0) {
×
998
      mError("failed to set query id:%s since %s", queryId, tstrerror(code));
×
999
      taosRUnLockLatch(&pConn->queryLock);
×
1000
      return code;
×
1001
    }
1002

1003
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1004
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->queryId, false);
×
1005
    if (code != 0) {
×
1006
      mError("failed to set query id:%" PRIx64 " since %s", pQuery->queryId, tstrerror(code));
×
1007
      taosRUnLockLatch(&pConn->queryLock);
×
1008
      return code;
×
1009
    }
1010

1011
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1012
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pConn->id, false);
×
1013
    if (code != 0) {
×
1014
      mError("failed to set conn id:%u since %s", pConn->id, tstrerror(code));
×
1015
      taosRUnLockLatch(&pConn->queryLock);
×
1016
      return code;
×
1017
    }
1018

1019
    char app[TSDB_APP_NAME_LEN + VARSTR_HEADER_SIZE];
1020
    STR_TO_VARSTR(app, pConn->app);
×
1021
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1022
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)app, false);
×
1023
    if (code != 0) {
×
1024
      mError("failed to set app since %s", tstrerror(code));
×
1025
      taosRUnLockLatch(&pConn->queryLock);
×
1026
      return code;
×
1027
    }
1028

1029
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1030
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pConn->pid, false);
×
1031
    if (code != 0) {
×
1032
      mError("failed to set conn id:%u since %s", pConn->id, tstrerror(code));
×
1033
      taosRUnLockLatch(&pConn->queryLock);
×
1034
      return code;
×
1035
    }
1036

1037
    char user[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
1038
    STR_TO_VARSTR(user, pConn->user);
×
1039
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1040
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)user, false);
×
1041
    if (code != 0) {
×
1042
      mError("failed to set user since %s", tstrerror(code));
×
1043
      taosRUnLockLatch(&pConn->queryLock);
×
1044
      return code;
×
1045
    }
1046

1047
    char endpoint[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
×
1048
    taosInetNtoa(varDataVal(endpoint), pConn->ip);
×
1049
    (void)tsnprintf(varDataVal(endpoint) + strlen(varDataVal(endpoint)),
×
1050
              sizeof(endpoint) - VARSTR_HEADER_SIZE - strlen(varDataVal(endpoint)), ":%d", pConn->port);
×
1051
    varDataLen(endpoint) = strlen(&endpoint[VARSTR_HEADER_SIZE]);
×
1052
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1053
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)endpoint, false);
×
1054
    if (code != 0) {
×
1055
      mError("failed to set endpoint since %s", tstrerror(code));
×
1056
      taosRUnLockLatch(&pConn->queryLock);
×
1057
      return code;
×
1058
    }
1059

1060
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1061
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->stime, false);
×
1062
    if (code != 0) {
×
1063
      mError("failed to set start time since %s", tstrerror(code));
×
1064
      taosRUnLockLatch(&pConn->queryLock);
×
1065
      return code;
×
1066
    }
1067

1068
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1069
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->useconds, false);
×
1070
    if (code != 0) {
×
1071
      mError("failed to set useconds since %s", tstrerror(code));
×
1072
      taosRUnLockLatch(&pConn->queryLock);
×
1073
      return code;
×
1074
    }
1075

1076
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1077
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->stableQuery, false);
×
1078
    if (code != 0) {
×
1079
      mError("failed to set stable query since %s", tstrerror(code));
×
1080
      taosRUnLockLatch(&pConn->queryLock);
×
1081
      return code;
×
1082
    }
1083

1084
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1085
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->isSubQuery, false);
×
1086
    if (code != 0) {
×
1087
      mError("failed to set sub query since %s", tstrerror(code));
×
1088
      taosRUnLockLatch(&pConn->queryLock);
×
1089
      return code;
×
1090
    }
1091

1092
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1093
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)&pQuery->subPlanNum, false);
×
1094
    if (code != 0) {
×
1095
      mError("failed to set sub plan num since %s", tstrerror(code));
×
1096
      taosRUnLockLatch(&pConn->queryLock);
×
1097
      return code;
×
1098
    }
1099

1100
    char    subStatus[TSDB_SHOW_SUBQUERY_LEN + VARSTR_HEADER_SIZE] = {0};
×
1101
    int64_t reserve = 64;
×
1102
    int32_t strSize = sizeof(subStatus);
×
1103
    int32_t offset = VARSTR_HEADER_SIZE;
×
1104
    for (int32_t i = 0; i < pQuery->subPlanNum && offset + reserve < strSize; ++i) {
×
1105
      if (i) {
×
1106
        offset += tsnprintf(subStatus + offset, sizeof(subStatus) - offset, ",");
×
1107
      }
1108
      if (offset + reserve < strSize) {
×
1109
        SQuerySubDesc *pDesc = taosArrayGet(pQuery->subDesc, i);
×
1110
        offset +=
×
1111
            tsnprintf(subStatus + offset, sizeof(subStatus) - offset, "%" PRIu64 ":%s", pDesc->tid, pDesc->status);
×
1112
      } else {
1113
        break;
×
1114
      }
1115
    }
1116
    varDataLen(subStatus) = strlen(&subStatus[VARSTR_HEADER_SIZE]);
×
1117
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1118
    code = colDataSetVal(pColInfo, curRowIndex, subStatus, (varDataLen(subStatus) == 0) ? true : false);
×
1119
    if (code != 0) {
×
1120
      mError("failed to set sub status since %s", tstrerror(code));
×
1121
      taosRUnLockLatch(&pConn->queryLock);
×
1122
      return code;
×
1123
    }
1124

1125
    char sql[TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE] = {0};
×
1126
    STR_TO_VARSTR(sql, pQuery->sql);
×
1127
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1128
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)sql, false);
×
1129
    if (code != 0) {
×
1130
      mError("failed to set sql since %s", tstrerror(code));
×
1131
      taosRUnLockLatch(&pConn->queryLock);
×
1132
      return code;
×
1133
    }
1134

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

1145
    char userIp[TD_IP_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
×
1146
    if (pConn->userIp != 0 && pConn->userIp != INADDR_NONE) {
×
1147
      taosInetNtoa(varDataVal(userIp), pConn->userIp);
×
1148
      varDataLen(userIp) = strlen(varDataVal(userIp));
×
1149
    }
1150
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1151
    code = colDataSetVal(pColInfo, curRowIndex, (const char *)userIp, false);
×
1152
    if (code != 0) {
×
1153
      mError("failed to set user ip since %s", tstrerror(code));
×
1154
      taosRUnLockLatch(&pConn->queryLock);
×
1155
      return code;
×
1156
    }
1157

1158
    pBlock->info.rows++;
×
1159
  }
1160

1161
  taosRUnLockLatch(&pConn->queryLock);
×
1162
  return i - offset;
×
1163
}
1164

1165
static int32_t mndRetrieveQueries(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
1✔
1166
  SMnode   *pMnode = pReq->info.node;
1✔
1167
  SSdb     *pSdb = pMnode->pSdb;
1✔
1168
  int32_t   numOfRows = 0;
1✔
1169
  SConnObj *pConn = NULL;
1✔
1170

1171
  if (pShow->pIter == NULL) {
1!
1172
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
1✔
1173
    pShow->pIter = taosCacheCreateIter(pMgmt->connCache);
1✔
1174
    if (!pShow->pIter) return terrno;
1!
1175
  }
1176

1177
  // means fetched some data last time for this conn
1178
  if (pShow->curIterPackedRows > 0) {
1!
1179
    size_t len = 0;
×
1180
    pConn = taosCacheIterGetData(pShow->pIter, &len);
×
1181
    if (pConn && (taosArrayGetSize(pConn->pQueries) > pShow->curIterPackedRows)) {
×
1182
      numOfRows = packQueriesIntoBlock(pShow, pConn, pBlock, pShow->curIterPackedRows, rows);
×
1183
      pShow->curIterPackedRows += numOfRows;
×
1184
    }
1185
  }
1186

1187
  while (numOfRows < rows) {
2!
1188
    pConn = mndGetNextConn(pMnode, pShow->pIter);
2✔
1189
    if (pConn == NULL) {
2✔
1190
      pShow->pIter = NULL;
1✔
1191
      break;
1✔
1192
    }
1193

1194
    int32_t packedRows = packQueriesIntoBlock(pShow, pConn, pBlock, 0, rows - numOfRows);
1✔
1195
    pShow->curIterPackedRows = packedRows;
1✔
1196
    numOfRows += packedRows;
1✔
1197
  }
1198
  pShow->numOfRows += numOfRows;
1✔
1199
  return numOfRows;
1✔
1200
}
1201

1202
static int32_t mndRetrieveApps(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
1203
  SMnode  *pMnode = pReq->info.node;
×
1204
  SSdb    *pSdb = pMnode->pSdb;
×
1205
  int32_t  numOfRows = 0;
×
1206
  int32_t  cols = 0;
×
1207
  SAppObj *pApp = NULL;
×
1208
  int32_t  code = 0;
×
1209

1210
  if (pShow->pIter == NULL) {
×
1211
    SProfileMgmt *pMgmt = &pMnode->profileMgmt;
×
1212
    pShow->pIter = taosCacheCreateIter(pMgmt->appCache);
×
1213
    if (!pShow->pIter) return terrno;
×
1214
  }
1215

1216
  while (numOfRows < rows) {
×
1217
    pApp = mndGetNextApp(pMnode, pShow->pIter);
×
1218
    if (pApp == NULL) {
×
1219
      pShow->pIter = NULL;
×
1220
      break;
×
1221
    }
1222

1223
    cols = 0;
×
1224

1225
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1226
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->appId, false);
×
1227
    if (code != 0) {
×
1228
      mError("failed to set app id since %s", tstrerror(code));
×
1229
      return code;
×
1230
    }
1231

1232
    char ip[TD_IP_LEN + VARSTR_HEADER_SIZE] = {0};
×
1233
    taosInetNtoa(varDataVal(ip), pApp->ip);
×
1234
    varDataLen(ip) = strlen(varDataVal(ip));
×
1235
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1236
    code = colDataSetVal(pColInfo, numOfRows, (const char *)ip, false);
×
1237
    if (code != 0) {
×
1238
      mError("failed to set ip since %s", tstrerror(code));
×
1239
      return code;
×
1240
    }
1241

1242
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1243
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->pid, false);
×
1244
    if (code != 0) {
×
1245
      mError("failed to set pid since %s", tstrerror(code));
×
1246
      return code;
×
1247
    }
1248

1249
    char name[TSDB_APP_NAME_LEN + 6 + VARSTR_HEADER_SIZE] = {0};
×
1250
    (void)tsnprintf(&name[VARSTR_HEADER_SIZE], sizeof(name) - VARSTR_HEADER_SIZE, "%s", pApp->name);
×
1251
    varDataLen(name) = strlen(&name[VARSTR_HEADER_SIZE]);
×
1252
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1253
    code = colDataSetVal(pColInfo, numOfRows, (const char *)name, false);
×
1254
    if (code != 0) {
×
1255
      mError("failed to set app name since %s", tstrerror(code));
×
1256
      return code;
×
1257
    }
1258

1259
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1260
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->startTime, false);
×
1261
    if (code != 0) {
×
1262
      mError("failed to set start time since %s", tstrerror(code));
×
1263
      return code;
×
1264
    }
1265

1266
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1267
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.numOfInsertsReq, false);
×
1268
    if (code != 0) {
×
1269
      mError("failed to set insert req since %s", tstrerror(code));
×
1270
      return code;
×
1271
    }
1272

1273
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1274
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.numOfInsertRows, false);
×
1275
    if (code != 0) {
×
1276
      mError("failed to set insert rows since %s", tstrerror(code));
×
1277
      return code;
×
1278
    }
1279

1280
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1281
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.insertElapsedTime, false);
×
1282
    if (code != 0) {
×
1283
      mError("failed to set insert elapsed time since %s", tstrerror(code));
×
1284
      return code;
×
1285
    }
1286

1287
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1288
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.insertBytes, false);
×
1289
    if (code != 0) {
×
1290
      mError("failed to set insert bytes since %s", tstrerror(code));
×
1291
      return code;
×
1292
    }
1293

1294
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1295
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.fetchBytes, false);
×
1296
    if (code != 0) {
×
1297
      mError("failed to set fetch bytes since %s", tstrerror(code));
×
1298
      return code;
×
1299
    }
1300

1301
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1302
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.queryElapsedTime, false);
×
1303
    if (code != 0) {
×
1304
      mError("failed to set query elapsed time since %s", tstrerror(code));
×
1305
      return code;
×
1306
    }
1307

1308
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1309
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.numOfSlowQueries, false);
×
1310
    if (code != 0) {
×
1311
      mError("failed to set slow queries since %s", tstrerror(code));
×
1312
      return code;
×
1313
    }
1314

1315
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1316
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.totalRequests, false);
×
1317
    if (code != 0) {
×
1318
      mError("failed to set total requests since %s", tstrerror(code));
×
1319
      return code;
×
1320
    }
1321

1322
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1323
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->summary.currentRequests, false);
×
1324
    if (code != 0) {
×
1325
      mError("failed to set current requests since %s", tstrerror(code));
×
1326
      return code;
×
1327
    }
1328

1329
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1330
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pApp->lastAccessTimeMs, false);
×
1331
    if (code != 0) {
×
1332
      mError("failed to set last access time since %s", tstrerror(code));
×
1333
      return code;
×
1334
    }
1335

1336
    numOfRows++;
×
1337
  }
1338

1339
  pShow->numOfRows += numOfRows;
×
1340
  return numOfRows;
×
1341
}
1342

1343
static void mndCancelGetNextQuery(SMnode *pMnode, void *pIter) {
×
1344
  if (pIter != NULL) {
×
1345
    taosCacheDestroyIter(pIter);
×
1346
  }
1347
}
×
1348

1349
int32_t mndGetNumOfConnections(SMnode *pMnode) {
×
1350
  SProfileMgmt *pMgmt = &pMnode->profileMgmt;
×
1351
  return taosCacheGetNumOfObj(pMgmt->connCache);
×
1352
}
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