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

taosdata / TDengine / #3798

31 Mar 2025 10:39AM UTC coverage: 9.424% (-20.9%) from 30.372%
#3798

push

travis-ci

happyguoxy
test:add test cases

21549 of 307601 branches covered (7.01%)

Branch coverage included in aggregate %.

36084 of 303967 relevant lines covered (11.87%)

58620.7 hits per line

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

0.0
/source/dnode/mnode/impl/src/mndTelem.c
1
/*
2
 * Copyright (c) 2020 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 "mndTelem.h"
18
#include "mndCluster.h"
19
#include "mndSync.h"
20
#include "thttp.h"
21
#include "tjson.h"
22
#include "mndAnode.h"
23

24
typedef struct {
25
  int64_t numOfDnode;
26
  int64_t numOfMnode;
27
  int64_t numOfVgroup;
28
  int64_t numOfDatabase;
29
  int64_t numOfSuperTable;
30
  int64_t numOfChildTable;
31
  int64_t numOfNormalTable;
32
  int64_t numOfColumn;
33
  int64_t totalPoints;
34
  int64_t totalStorage;
35
  int64_t compStorage;
36
  int32_t numOfAnalysisAlgos;
37
} SMnodeStat;
38

39
static void mndGetStat(SMnode* pMnode, SMnodeStat* pStat) {
×
40
  memset(pStat, 0, sizeof(SMnodeStat));
×
41

42
  SSdb* pSdb = pMnode->pSdb;
×
43
  pStat->numOfDnode = sdbGetSize(pSdb, SDB_DNODE);
×
44
  pStat->numOfMnode = sdbGetSize(pSdb, SDB_MNODE);
×
45
  pStat->numOfVgroup = sdbGetSize(pSdb, SDB_VGROUP);
×
46
  pStat->numOfDatabase = sdbGetSize(pSdb, SDB_DB);
×
47
  pStat->numOfSuperTable = sdbGetSize(pSdb, SDB_STB);
×
48

49
  void* pIter = NULL;
×
50
  while (1) {
×
51
    SVgObj* pVgroup = NULL;
×
52
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
×
53
    if (pIter == NULL) break;
×
54

55
    pStat->numOfChildTable += pVgroup->numOfTables;
×
56
    pStat->numOfColumn += pVgroup->numOfTimeSeries;
×
57
    pStat->totalPoints += pVgroup->pointsWritten;
×
58
    pStat->totalStorage += pVgroup->totalStorage;
×
59
    pStat->compStorage += pVgroup->compStorage;
×
60

61
    sdbRelease(pSdb, pVgroup);
×
62
  }
63
}
×
64

65
static int32_t algoToJson(const void* pObj, SJson* pJson) {
×
66
  const SAnodeAlgo* pNode = (const SAnodeAlgo*)pObj;
×
67
  int32_t code = tjsonAddStringToObject(pJson, "name", pNode->name);
×
68
  return code;
×
69
}
70

71
static void mndBuildRuntimeInfo(SMnode* pMnode, SJson* pJson) {
×
72
  SMnodeStat mstat = {0};
×
73
  int32_t    code = 0;
×
74
  int32_t    lino = 0;
×
75
  SArray*    pFcList = NULL;
×
76
  SArray*    pAdList = NULL;
×
77

78
  mndGetStat(pMnode, &mstat);
×
79

80
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfDnode", mstat.numOfDnode), &lino, _OVER);
×
81
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfMnode", mstat.numOfMnode), &lino, _OVER);
×
82
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfVgroup", mstat.numOfVgroup), &lino, _OVER);
×
83
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfDatabase", mstat.numOfDatabase), &lino, _OVER);
×
84
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfSuperTable", mstat.numOfSuperTable), &lino, _OVER);
×
85
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfChildTable", mstat.numOfChildTable), &lino, _OVER);
×
86
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfColumn", mstat.numOfColumn), &lino, _OVER);
×
87
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfPoint", mstat.totalPoints), &lino, _OVER);
×
88
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "totalStorage", mstat.totalStorage), &lino, _OVER);
×
89
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "compStorage", mstat.compStorage), &lino, _OVER);
×
90

91
  pFcList = taosArrayInit(4, sizeof(SAnodeAlgo));
×
92
  pAdList = taosArrayInit(4, sizeof(SAnodeAlgo));
×
93
  if (pFcList == NULL || pAdList == NULL) {
×
94
    lino = __LINE__;
×
95
    goto _OVER;
×
96
  }
97

98
  mndRetrieveAlgoList(pMnode, pFcList, pAdList);
×
99

100
  if (taosArrayGetSize(pFcList) > 0) {
×
101
    SJson* items = tjsonAddArrayToObject(pJson, "forecast");
×
102
    TSDB_CHECK_NULL(items, code, lino, _OVER, terrno);
×
103

104
    for (int32_t i = 0; i < taosArrayGetSize(pFcList); ++i) {
×
105
      SJson* item = tjsonCreateObject();
×
106

107
      TSDB_CHECK_NULL(item, code, lino, _OVER, terrno);
×
108
      TAOS_CHECK_GOTO(tjsonAddItemToArray(items, item), &lino, _OVER);
×
109

110
      SAnodeAlgo* p = taosArrayGet(pFcList, i);
×
111
      TSDB_CHECK_NULL(p, code, lino, _OVER, terrno);
×
112
      TAOS_CHECK_GOTO(tjsonAddStringToObject(item, "name", p->name), &lino, _OVER);
×
113
    }
114
  }
115

116
  if (taosArrayGetSize(pAdList) > 0) {
×
117
    SJson* items1 = tjsonAddArrayToObject(pJson, "anomaly_detection");
×
118
    TSDB_CHECK_NULL(items1, code, lino, _OVER, terrno);
×
119

120
    for (int32_t i = 0; i < taosArrayGetSize(pAdList); ++i) {
×
121
      SJson* item = tjsonCreateObject();
×
122

123
      TSDB_CHECK_NULL(item, code, lino, _OVER, terrno);
×
124
      TAOS_CHECK_GOTO(tjsonAddItemToArray(items1, item), &lino, _OVER);
×
125

126
      SAnodeAlgo* p = taosArrayGet(pAdList, i);
×
127
      TSDB_CHECK_NULL(p, code, lino, _OVER, terrno);
×
128
      TAOS_CHECK_GOTO(tjsonAddStringToObject(item, "name", p->name), &lino, _OVER);
×
129
    }
130
  }
131

132
_OVER:
×
133
  taosArrayDestroy(pFcList);
×
134
  taosArrayDestroy(pAdList);
×
135

136
  if (code != 0) {
×
137
    mError("failed to mndBuildRuntimeInfo at line:%d since %s", lino, tstrerror(code));
×
138
  }
139
}
×
140

141
static char* mndBuildTelemetryReport(SMnode* pMnode) {
×
142
  char        tmp[4096] = {0};
×
143
  STelemMgmt* pMgmt = &pMnode->telemMgmt;
×
144
  int32_t     code = 0;
×
145
  int32_t     lino = 0;
×
146

147
  SJson* pJson = tjsonCreateObject();
×
148
  if (pJson == NULL) return NULL;
×
149

150
  char clusterName[64] = {0};
×
151
  if ((terrno = mndGetClusterName(pMnode, clusterName, sizeof(clusterName))) != 0) return NULL;
×
152
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "instanceId", clusterName), &lino, _OVER);
×
153
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "reportVersion", 1), &lino, _OVER);
×
154

155
  if (taosGetOsReleaseName(tmp, NULL, NULL, sizeof(tmp)) == 0) {
×
156
    TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "os", tmp), &lino, _OVER);
×
157
  }
158

159
  float numOfCores = 0;
×
160
  if (taosGetCpuInfo(tmp, sizeof(tmp), &numOfCores) == 0) {
×
161
    TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "cpuModel", tmp), &lino, _OVER);
×
162
    TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfCpu", numOfCores), &lino, _OVER);
×
163
  } else {
164
    TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfCpu", tsNumOfCores), &lino, _OVER);
×
165
  }
166

167
  snprintf(tmp, sizeof(tmp), "%" PRId64 " kB", tsTotalMemoryKB);
×
168
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "memory", tmp), &lino, _OVER);
×
169

170
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "version", td_version), &lino, _OVER);
×
171
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "buildInfo", td_buildinfo), &lino, _OVER);
×
172
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "gitInfo", td_gitinfo), &lino, _OVER);
×
173
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "email", pMgmt->email), &lino, _OVER);
×
174

175
  mndBuildRuntimeInfo(pMnode, pJson);
×
176

177
_OVER:
×
178
  if (code != 0) {
×
179
    mError("failed to build telemetry report at lino:%d, since %s", lino, tstrerror(code));
×
180
  }
181
  char* pCont = tjsonToString(pJson);
×
182
  tjsonDelete(pJson);
×
183
  return pCont;
×
184
}
185

186
static int32_t mndProcessTelemTimer(SRpcMsg* pReq) {
×
187
  int32_t     code = 0;
×
188
  int32_t     line = 0;
×
189
  SMnode*     pMnode = pReq->info.node;
×
190
  STelemMgmt* pMgmt = &pMnode->telemMgmt;
×
191

192
  if (!tsEnableTelem) {
×
193
    return 0;
×
194
  }
195

196
  (void)taosThreadMutexLock(&pMgmt->lock);
×
197
  char* pCont = mndBuildTelemetryReport(pMnode);
×
198
  (void)taosThreadMutexUnlock(&pMgmt->lock);
×
199

200
  TSDB_CHECK_NULL(pCont, code, line, _end, terrno);
×
201

202
  code = taosSendTelemReport(&pMgmt->addrMgt, tsTelemUri, tsTelemPort, pCont, strlen(pCont), HTTP_FLAT);
×
203
  taosMemoryFree(pCont);
×
204
  return code;
×
205

206
_end:
×
207
  if (code != 0) {
×
208
    mError("%s failed to send telemetry report, line %d since %s", __func__, line, tstrerror(code));
×
209
  }
210
  taosMemoryFree(pCont);
×
211
  return code;
×
212
}
213

214
int32_t mndInitTelem(SMnode* pMnode) {
×
215
  int32_t     code = 0;
×
216
  STelemMgmt* pMgmt = &pMnode->telemMgmt;
×
217

218
  (void)taosThreadMutexInit(&pMgmt->lock, NULL);
×
219
  if ((code = taosGetEmail(pMgmt->email, sizeof(pMgmt->email))) != 0) {
×
220
    mWarn("failed to get email since %s", tstrerror(code));
×
221
  }
222

223
  code = taosTelemetryMgtInit(&pMgmt->addrMgt, tsTelemServer);
×
224
  if (code != 0) {
×
225
    mError("failed to init telemetry management since %s", tstrerror(code));
×
226
    return code;
×
227
  }
228

229
  mndSetMsgHandle(pMnode, TDMT_MND_TELEM_TIMER, mndProcessTelemTimer);
×
230
  return 0;
×
231
}
232

233
void mndCleanupTelem(SMnode* pMnode) {
×
234
  STelemMgmt* pMgmt = &pMnode->telemMgmt;
×
235
  taosTelemetryDestroy(&pMgmt->addrMgt);
×
236
  (void)taosThreadMutexDestroy(&pMgmt->lock);
×
237
}
×
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