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

taosdata / TDengine / #4886

16 Dec 2025 01:13AM UTC coverage: 65.292% (+0.03%) from 65.258%
#4886

push

travis-ci

web-flow
fix: compile error (#33938)

178718 of 273721 relevant lines covered (65.29%)

103311111.65 hits per line

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

67.45
/source/dnode/mgmt/mgmt_dnode/src/dmWorker.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 "dmInt.h"
18
#include "tgrant.h"
19
#include "thttp.h"
20
#include "streamMsg.h"
21

22
static void *dmStatusThreadFp(void *param) {
689,069✔
23
  SDnodeMgmt *pMgmt = param;
689,069✔
24
  int64_t     lastTime = taosGetTimestampMs();
689,069✔
25
  setThreadName("dnode-status");
689,069✔
26

27
  while (1) {
814,498,748✔
28
    taosMsleep(50);
815,187,817✔
29
    if (pMgmt->pData->dropped || pMgmt->pData->stopped) break;
815,187,817✔
30

31
    int64_t curTime = taosGetTimestampMs();
814,498,748✔
32
    if (curTime < lastTime) lastTime = curTime;
814,498,748✔
33
    float interval = curTime - lastTime;
814,498,748✔
34
    if (interval >= tsStatusIntervalMs) {
814,498,748✔
35
      dmSendStatusReq(pMgmt);
40,753,441✔
36
      lastTime = curTime;
40,753,441✔
37
    }
38
  }
39

40
  return NULL;
689,069✔
41
}
42

43
static void *dmConfigThreadFp(void *param) {
689,069✔
44
  SDnodeMgmt *pMgmt = param;
689,069✔
45
  int64_t     lastTime = taosGetTimestampMs();
689,069✔
46
  setThreadName("dnode-config");
689,069✔
47
  while (1) {
13,738,682✔
48
    taosMsleep(50);
14,427,751✔
49
    if (pMgmt->pData->dropped || pMgmt->pData->stopped || tsConfigInited) break;
14,427,751✔
50

51
    int64_t curTime = taosGetTimestampMs();
13,738,682✔
52
    if (curTime < lastTime) lastTime = curTime;
13,738,682✔
53
    float interval = curTime - lastTime;
13,738,682✔
54
    if (interval >= tsStatusIntervalMs) {
13,738,682✔
55
      dmSendConfigReq(pMgmt);
689,592✔
56
      lastTime = curTime;
689,592✔
57
    }
58
  }
59
  return NULL;
689,069✔
60
}
61

62
static void *dmStatusInfoThreadFp(void *param) {
689,069✔
63
  SDnodeMgmt *pMgmt = param;
689,069✔
64
  int64_t     lastTime = taosGetTimestampMs();
689,069✔
65
  setThreadName("dnode-status-info");
689,069✔
66

67
  int32_t upTimeCount = 0;
689,069✔
68
  int64_t upTime = 0;
689,069✔
69

70
  while (1) {
835,701,362✔
71
    taosMsleep(50);
836,390,431✔
72
    if (pMgmt->pData->dropped || pMgmt->pData->stopped) break;
836,390,431✔
73

74
    int64_t curTime = taosGetTimestampMs();
835,701,362✔
75
    if (curTime < lastTime) lastTime = curTime;
835,701,362✔
76
    float interval = curTime - lastTime;
835,701,362✔
77
    if (interval >= tsStatusIntervalMs) {
835,701,362✔
78
      dmUpdateStatusInfo(pMgmt);
41,537,167✔
79
      lastTime = curTime;
41,537,167✔
80

81
      if ((upTimeCount = ((upTimeCount + 1) & 63)) == 0) {
41,537,167✔
82
        upTime = taosGetOsUptime() - tsDndStartOsUptime;
420,898✔
83
        if (upTime > 0) tsDndUpTime = upTime;
420,898✔
84
      }
85
    }
86
  }
87

88
  return NULL;
689,069✔
89
}
90

91
#if defined(TD_ENTERPRISE)
92
SDmNotifyHandle dmNotifyHdl = {.state = 0};
93
#define TIMESERIES_STASH_NUM 5
94
static void *dmNotifyThreadFp(void *param) {
689,069✔
95
  SDnodeMgmt *pMgmt = param;
689,069✔
96
  int64_t     lastTime = taosGetTimestampMs();
689,069✔
97
  setThreadName("dnode-notify");
689,069✔
98

99
  if (tsem_init(&dmNotifyHdl.sem, 0, 0) != 0) {
689,069✔
100
    return NULL;
×
101
  }
102

103
  // calculate approximate timeSeries per second
104
  int64_t  notifyTimeStamp[TIMESERIES_STASH_NUM];
688,740✔
105
  int64_t  notifyTimeSeries[TIMESERIES_STASH_NUM];
688,740✔
106
  int64_t  approximateTimeSeries = 0;
689,069✔
107
  uint64_t nTotalNotify = 0;
689,069✔
108
  int32_t  head, tail = 0;
689,069✔
109

110
  bool       wait = true;
689,069✔
111
  int32_t    nDnode = 0;
689,069✔
112
  int64_t    lastNotify = 0;
689,069✔
113
  int64_t    lastFetchDnode = 0;
689,069✔
114
  SNotifyReq req = {0};
689,069✔
115
  while (1) {
47,715,664✔
116
    if (pMgmt->pData->dropped || pMgmt->pData->stopped) break;
48,404,733✔
117
    if (wait) tsem_wait(&dmNotifyHdl.sem);
47,715,664✔
118
    atomic_store_8(&dmNotifyHdl.state, 1);
47,715,664✔
119

120
    int64_t remainTimeSeries = grantRemain(TSDB_GRANT_TIMESERIES);
47,715,664✔
121
    if (remainTimeSeries == INT64_MAX || remainTimeSeries <= 0) {
47,715,664✔
122
      goto _skip;
47,715,664✔
123
    }
124
    int64_t current = taosGetTimestampMs();
×
125
    if (current - lastFetchDnode > 1000) {
×
126
      nDnode = dmGetDnodeSize(pMgmt->pData);
×
127
      if (nDnode < 1) nDnode = 1;
×
128
      lastFetchDnode = current;
×
129
    }
130
    if (req.dnodeId == 0 || req.clusterId == 0) {
×
131
      req.dnodeId = pMgmt->pData->dnodeId;
×
132
      req.clusterId = pMgmt->pData->clusterId;
×
133
    }
134

135
    if (current - lastNotify < 10) {
×
136
      int64_t nCmprTimeSeries = approximateTimeSeries / 100;
×
137
      if (nCmprTimeSeries < 1e5) nCmprTimeSeries = 1e5;
×
138
      if (remainTimeSeries > nCmprTimeSeries * 10) {
×
139
        taosMsleep(10);
×
140
      } else if (remainTimeSeries > nCmprTimeSeries * 5) {
×
141
        taosMsleep(5);
×
142
      } else {
143
        taosMsleep(2);
×
144
      }
145
    }
146

147
    SMonVloadInfo vinfo = {0};
×
148
    (*pMgmt->getVnodeLoadsLiteFp)(&vinfo);
×
149
    req.pVloads = vinfo.pVloads;
×
150
    int32_t nVgroup = taosArrayGetSize(req.pVloads);
×
151
    int64_t nTimeSeries = 0;
×
152
    for (int32_t i = 0; i < nVgroup; ++i) {
×
153
      SVnodeLoadLite *vload = TARRAY_GET_ELEM(req.pVloads, i);
×
154
      nTimeSeries += vload->nTimeSeries;
×
155
    }
156
    notifyTimeSeries[tail] = nTimeSeries;
×
157
    notifyTimeStamp[tail] = taosGetTimestampNs();
×
158
    ++nTotalNotify;
×
159

160
    approximateTimeSeries = 0;
×
161
    if (nTotalNotify >= TIMESERIES_STASH_NUM) {
×
162
      head = tail - TIMESERIES_STASH_NUM + 1;
×
163
      if (head < 0) head += TIMESERIES_STASH_NUM;
×
164
      int64_t timeDiff = notifyTimeStamp[tail] - notifyTimeStamp[head];
×
165
      int64_t tsDiff = notifyTimeSeries[tail] - notifyTimeSeries[head];
×
166
      if (tsDiff > 0) {
×
167
        if (timeDiff > 0 && timeDiff < 1e9) {
×
168
          approximateTimeSeries = (double)tsDiff * 1e9 / timeDiff;
×
169
          if ((approximateTimeSeries * nDnode) > remainTimeSeries) {
×
170
            dmSendNotifyReq(pMgmt, &req);
×
171
          }
172
        } else {
173
          dmSendNotifyReq(pMgmt, &req);
×
174
        }
175
      }
176
    } else {
177
      dmSendNotifyReq(pMgmt, &req);
×
178
    }
179
    if (++tail == TIMESERIES_STASH_NUM) tail = 0;
×
180

181
    tFreeSNotifyReq(&req);
×
182
    lastNotify = taosGetTimestampMs();
×
183
  _skip:
47,715,664✔
184
    if (1 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 0)) {
47,715,664✔
185
      wait = true;
47,710,814✔
186
      continue;
47,710,814✔
187
    }
188
    wait = false;
4,850✔
189
  }
190

191
  return NULL;
689,069✔
192
}
193
#endif
194

195
#ifdef USE_MONITOR
196
static void *dmMonitorThreadFp(void *param) {
689,069✔
197
  SDnodeMgmt *pMgmt = param;
689,069✔
198
  int64_t     lastTime = taosGetTimestampMs();
689,069✔
199
  int64_t     lastTimeForBasic = taosGetTimestampMs();
689,069✔
200
  setThreadName("dnode-monitor");
689,069✔
201

202
  static int32_t TRIM_FREQ = 20;
203
  int32_t        trimCount = 0;
689,069✔
204

205
  while (1) {
210,317,751✔
206
    taosMsleep(200);
211,006,820✔
207
    if (pMgmt->pData->dropped || pMgmt->pData->stopped) break;
211,006,820✔
208

209
    int64_t curTime = taosGetTimestampMs();
210,317,751✔
210

211
    if (curTime < lastTime) lastTime = curTime;
210,317,751✔
212
    float interval = (curTime - lastTime) / 1000.0f;
210,317,751✔
213
    if (interval >= tsMonitorInterval) {
210,317,751✔
214
      (*pMgmt->sendMonitorReportFp)();
1,113,749✔
215
      (*pMgmt->monitorCleanExpiredSamplesFp)();
1,113,749✔
216
      lastTime = curTime;
1,113,749✔
217

218
      trimCount = (trimCount + 1) % TRIM_FREQ;
1,113,749✔
219
      if (trimCount == 0) {
1,113,749✔
220
        taosMemoryTrim(0, NULL);
1,347✔
221
      }
222
    }
223
    if (atomic_val_compare_exchange_8(&tsNeedTrim, 1, 0)) {
210,317,751✔
224
      taosMemoryTrim(0, NULL);
7,727,734✔
225
    }
226
  }
227

228
  return NULL;
689,069✔
229
}
230
#endif
231
#ifdef USE_AUDIT
232
static void *dmAuditThreadFp(void *param) {
689,069✔
233
  SDnodeMgmt *pMgmt = param;
689,069✔
234
  int64_t     lastTime = taosGetTimestampMs();
689,069✔
235
  setThreadName("dnode-audit");
689,069✔
236

237
  while (1) {
419,934,182✔
238
    taosMsleep(100);
420,623,251✔
239
    if (pMgmt->pData->dropped || pMgmt->pData->stopped) break;
420,623,251✔
240

241
    int64_t curTime = taosGetTimestampMs();
419,934,182✔
242
    if (curTime < lastTime) lastTime = curTime;
419,934,182✔
243
    float interval = curTime - lastTime;
419,934,182✔
244
    if (interval >= tsAuditInterval) {
419,934,182✔
245
      (*pMgmt->sendAuditRecordsFp)();
8,075,563✔
246
      lastTime = curTime;
8,075,563✔
247
    }
248
  }
249

250
  return NULL;
689,069✔
251
}
252
#endif
253
#ifdef USE_REPORT
254
static void *dmCrashReportThreadFp(void *param) {
×
255
  int32_t     code = 0;
×
256
  SDnodeMgmt *pMgmt = param;
×
257
  int64_t     lastTime = taosGetTimestampMs();
×
258
  setThreadName("dnode-crashReport");
×
259
  char filepath[PATH_MAX] = {0};
×
260
  snprintf(filepath, sizeof(filepath), "%s%s.taosdCrashLog", tsLogDir, TD_DIRSEP);
×
261
  char     *pMsg = NULL;
×
262
  int64_t   msgLen = 0;
×
263
  TdFilePtr pFile = NULL;
×
264
  bool      truncateFile = false;
×
265
  int32_t   sleepTime = 200;
×
266
  int32_t   reportPeriodNum = 3600 * 1000 / sleepTime;
×
267
  int32_t   loopTimes = reportPeriodNum;
×
268

269
  STelemAddrMgmt mgt = {0};
×
270
  code = taosTelemetryMgtInit(&mgt, tsTelemServer);
×
271
  if (code != 0) {
×
272
    dError("failed to init telemetry since %s", tstrerror(code));
×
273
    return NULL;
×
274
  }
275
  code = initCrashLogWriter();
×
276
  if (code != 0) {
×
277
    dError("failed to init crash log writer since %s", tstrerror(code));
×
278
    return NULL;
×
279
  }
280

281
  while (1) {
282
    checkAndPrepareCrashInfo();
×
283
    if ((pMgmt->pData->dropped || pMgmt->pData->stopped) && reportThreadSetQuit()) {
×
284
      break;
×
285
    }
286
    if (loopTimes++ < reportPeriodNum) {
×
287
      taosMsleep(sleepTime);
×
288
      if (loopTimes < 0) loopTimes = reportPeriodNum;
×
289
      continue;
×
290
    }
291
    taosReadCrashInfo(filepath, &pMsg, &msgLen, &pFile);
×
292
    if (pMsg && msgLen > 0) {
×
293
      if (taosSendTelemReport(&mgt, tsSvrCrashReportUri, tsTelemPort, pMsg, msgLen, HTTP_FLAT) != 0) {
×
294
        dError("failed to send crash report");
×
295
        if (pFile) {
×
296
          taosReleaseCrashLogFile(pFile, false);
×
297
          pFile = NULL;
×
298

299
          taosMsleep(sleepTime);
×
300
          loopTimes = 0;
×
301
          continue;
×
302
        }
303
      } else {
304
        dInfo("succeed to send crash report");
×
305
        truncateFile = true;
×
306
      }
307
    } else {
308
      dInfo("no crash info was found");
×
309
    }
310

311
    taosMemoryFree(pMsg);
×
312

313
    if (pMsg && msgLen > 0) {
×
314
      pMsg = NULL;
×
315
      continue;
×
316
    }
317

318
    if (pFile) {
×
319
      taosReleaseCrashLogFile(pFile, truncateFile);
×
320
      pFile = NULL;
×
321
      truncateFile = false;
×
322
    }
323

324
    taosMsleep(sleepTime);
×
325
    loopTimes = 0;
×
326
  }
327
  taosTelemetryDestroy(&mgt);
×
328

329
  return NULL;
×
330
}
331
#endif
332

333
static void *dmMetricsThreadFp(void *param) {
689,069✔
334
  SDnodeMgmt *pMgmt = param;
689,069✔
335
  int64_t     lastTime = taosGetTimestampMs();
689,069✔
336
  setThreadName("dnode-metrics");
689,069✔
337
  while (1) {
210,327,075✔
338
    taosMsleep(200);
211,016,144✔
339
    if (pMgmt->pData->dropped || pMgmt->pData->stopped) break;
211,016,144✔
340

341
    int64_t curTime = taosGetTimestampMs();
210,327,075✔
342
    if (curTime < lastTime) lastTime = curTime;
210,327,075✔
343
    float interval = (curTime - lastTime) / 1000.0f;
210,327,075✔
344
    if (interval >= tsMetricsInterval) {
210,327,075✔
345
      (*pMgmt->sendMetricsReportFp)();
1,109,732✔
346
      (*pMgmt->metricsCleanExpiredSamplesFp)();
1,109,732✔
347
      lastTime = curTime;
1,109,732✔
348
    }
349
  }
350
  return NULL;
689,069✔
351
}
352

353
int32_t dmStartStatusThread(SDnodeMgmt *pMgmt) {
689,069✔
354
  int32_t      code = 0;
689,069✔
355
  TdThreadAttr thAttr;
688,740✔
356
  (void)taosThreadAttrInit(&thAttr);
689,069✔
357
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
689,069✔
358
#ifdef TD_COMPACT_OS
359
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
360
#endif
361
  if (taosThreadCreate(&pMgmt->statusThread, &thAttr, dmStatusThreadFp, pMgmt) != 0) {
689,069✔
362
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
363
    dError("failed to create status thread since %s", tstrerror(code));
×
364
    return code;
×
365
  }
366

367
  (void)taosThreadAttrDestroy(&thAttr);
689,069✔
368
  tmsgReportStartup("dnode-status", "initialized");
689,069✔
369
  return 0;
689,069✔
370
}
371

372
int32_t dmStartConfigThread(SDnodeMgmt *pMgmt) {
689,069✔
373
  int32_t      code = 0;
689,069✔
374
  TdThreadAttr thAttr;
688,740✔
375
  (void)taosThreadAttrInit(&thAttr);
689,069✔
376
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
689,069✔
377
#ifdef TD_COMPACT_OS
378
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
379
#endif
380
  if (taosThreadCreate(&pMgmt->configThread, &thAttr, dmConfigThreadFp, pMgmt) != 0) {
689,069✔
381
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
382
    dError("failed to create config thread since %s", tstrerror(code));
×
383
    return code;
×
384
  }
385

386
  (void)taosThreadAttrDestroy(&thAttr);
689,069✔
387
  tmsgReportStartup("config-status", "initialized");
689,069✔
388
  return 0;
689,069✔
389
}
390

391
int32_t dmStartStatusInfoThread(SDnodeMgmt *pMgmt) {
689,069✔
392
  int32_t      code = 0;
689,069✔
393
  TdThreadAttr thAttr;
688,740✔
394
  (void)taosThreadAttrInit(&thAttr);
689,069✔
395
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
689,069✔
396
#ifdef TD_COMPACT_OS
397
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
398
#endif
399
  if (taosThreadCreate(&pMgmt->statusInfoThread, &thAttr, dmStatusInfoThreadFp, pMgmt) != 0) {
689,069✔
400
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
401
    dError("failed to create status Info thread since %s", tstrerror(code));
×
402
    return code;
×
403
  }
404

405
  (void)taosThreadAttrDestroy(&thAttr);
689,069✔
406
  tmsgReportStartup("dnode-status-info", "initialized");
689,069✔
407
  return 0;
689,069✔
408
}
409

410
void dmStopStatusThread(SDnodeMgmt *pMgmt) {
689,069✔
411
  if (taosCheckPthreadValid(pMgmt->statusThread)) {
689,069✔
412
    (void)taosThreadJoin(pMgmt->statusThread, NULL);
689,069✔
413
    taosThreadClear(&pMgmt->statusThread);
689,069✔
414
  }
415
}
689,069✔
416

417
void dmStopConfigThread(SDnodeMgmt *pMgmt) {
689,069✔
418
  if (taosCheckPthreadValid(pMgmt->configThread)) {
689,069✔
419
    (void)taosThreadJoin(pMgmt->configThread, NULL);
689,069✔
420
    taosThreadClear(&pMgmt->configThread);
689,069✔
421
  }
422
}
689,069✔
423

424
void dmStopStatusInfoThread(SDnodeMgmt *pMgmt) {
689,069✔
425
  if (taosCheckPthreadValid(pMgmt->statusInfoThread)) {
689,069✔
426
    (void)taosThreadJoin(pMgmt->statusInfoThread, NULL);
689,069✔
427
    taosThreadClear(&pMgmt->statusInfoThread);
689,069✔
428
  }
429
}
689,069✔
430
#ifdef TD_ENTERPRISE
431
int32_t dmStartNotifyThread(SDnodeMgmt *pMgmt) {
689,069✔
432
  int32_t      code = 0;
689,069✔
433
  TdThreadAttr thAttr;
688,740✔
434
  (void)taosThreadAttrInit(&thAttr);
689,069✔
435
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
689,069✔
436
  if (taosThreadCreate(&pMgmt->notifyThread, &thAttr, dmNotifyThreadFp, pMgmt) != 0) {
689,069✔
437
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
438
    dError("failed to create notify thread since %s", tstrerror(code));
×
439
    return code;
×
440
  }
441

442
  (void)taosThreadAttrDestroy(&thAttr);
689,069✔
443
  tmsgReportStartup("dnode-notify", "initialized");
689,069✔
444
  return 0;
689,069✔
445
}
446

447
void dmStopNotifyThread(SDnodeMgmt *pMgmt) {
689,069✔
448
  if (taosCheckPthreadValid(pMgmt->notifyThread)) {
689,069✔
449
    if (tsem_post(&dmNotifyHdl.sem) != 0) {
689,069✔
450
      dError("failed to post notify sem");
×
451
    }
452

453
    (void)taosThreadJoin(pMgmt->notifyThread, NULL);
689,069✔
454
    taosThreadClear(&pMgmt->notifyThread);
689,069✔
455
  }
456
  if (tsem_destroy(&dmNotifyHdl.sem) != 0) {
689,069✔
457
    dError("failed to destroy notify sem");
×
458
  }
459
}
689,069✔
460
#endif
461
int32_t dmStartMonitorThread(SDnodeMgmt *pMgmt) {
689,069✔
462
  int32_t      code = 0;
689,069✔
463
#ifdef USE_MONITOR
464
  TdThreadAttr thAttr;
688,740✔
465
  (void)taosThreadAttrInit(&thAttr);
689,069✔
466
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
689,069✔
467
  if (taosThreadCreate(&pMgmt->monitorThread, &thAttr, dmMonitorThreadFp, pMgmt) != 0) {
689,069✔
468
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
469
    dError("failed to create monitor thread since %s", tstrerror(code));
×
470
    return code;
×
471
  }
472

473
  (void)taosThreadAttrDestroy(&thAttr);
689,069✔
474
  tmsgReportStartup("dnode-monitor", "initialized");
689,069✔
475
#endif
476
  return 0;
689,069✔
477
}
478

479
int32_t dmStartAuditThread(SDnodeMgmt *pMgmt) {
689,069✔
480
  int32_t      code = 0;
689,069✔
481
#ifdef USE_AUDIT  
482
  TdThreadAttr thAttr;
688,740✔
483
  (void)taosThreadAttrInit(&thAttr);
689,069✔
484
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
689,069✔
485
  if (taosThreadCreate(&pMgmt->auditThread, &thAttr, dmAuditThreadFp, pMgmt) != 0) {
689,069✔
486
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
487
    dError("failed to create audit thread since %s", tstrerror(code));
×
488
    return code;
×
489
  }
490

491
  (void)taosThreadAttrDestroy(&thAttr);
689,069✔
492
  tmsgReportStartup("dnode-audit", "initialized");
689,069✔
493
#endif  
494
  return 0;
689,069✔
495
}
496

497
int32_t dmStartMetricsThread(SDnodeMgmt *pMgmt) {
689,069✔
498
  int32_t code = 0;
689,069✔
499
#ifdef USE_MONITOR
500
  TdThreadAttr thAttr;
688,740✔
501
  (void)taosThreadAttrInit(&thAttr);
689,069✔
502
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
689,069✔
503
  if (taosThreadCreate(&pMgmt->metricsThread, &thAttr, dmMetricsThreadFp, pMgmt) != 0) {
689,069✔
504
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
505
    dError("failed to create metrics thread since %s", tstrerror(code));
×
506
    return code;
×
507
  }
508

509
  (void)taosThreadAttrDestroy(&thAttr);
689,069✔
510
  tmsgReportStartup("dnode-metrics", "initialized");
689,069✔
511
#endif
512
  return 0;
689,069✔
513
}
514

515
void dmStopMonitorThread(SDnodeMgmt *pMgmt) {
689,069✔
516
#ifdef USE_MONITOR
517
  if (taosCheckPthreadValid(pMgmt->monitorThread)) {
689,069✔
518
    (void)taosThreadJoin(pMgmt->monitorThread, NULL);
689,069✔
519
    taosThreadClear(&pMgmt->monitorThread);
689,069✔
520
  }
521
#endif
522
}
689,069✔
523

524
void dmStopAuditThread(SDnodeMgmt *pMgmt) {
689,069✔
525
#ifdef USE_AUDIT
526
  if (taosCheckPthreadValid(pMgmt->auditThread)) {
689,069✔
527
    (void)taosThreadJoin(pMgmt->auditThread, NULL);
689,069✔
528
    taosThreadClear(&pMgmt->auditThread);
689,069✔
529
  }
530
#endif
531
}
689,069✔
532

533
int32_t dmStartCrashReportThread(SDnodeMgmt *pMgmt) {
689,069✔
534
  int32_t code = 0;
689,069✔
535
#ifdef USE_REPORT
536
  if (!tsEnableCrashReport) {
689,069✔
537
    return 0;
689,069✔
538
  }
539

540
  TdThreadAttr thAttr;
×
541
  (void)taosThreadAttrInit(&thAttr);
×
542
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
×
543
  if (taosThreadCreate(&pMgmt->crashReportThread, &thAttr, dmCrashReportThreadFp, pMgmt) != 0) {
×
544
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
545
    dError("failed to create crashReport thread since %s", tstrerror(code));
×
546
    return code;
×
547
  }
548

549
  (void)taosThreadAttrDestroy(&thAttr);
×
550
  tmsgReportStartup("dnode-crashReport", "initialized");
×
551
#endif
552
  return 0;
×
553
}
554

555
void dmStopCrashReportThread(SDnodeMgmt *pMgmt) {
689,069✔
556
#ifdef USE_REPORT
557
  if (!tsEnableCrashReport) {
689,069✔
558
    return;
689,069✔
559
  }
560

561
  if (taosCheckPthreadValid(pMgmt->crashReportThread)) {
×
562
    (void)taosThreadJoin(pMgmt->crashReportThread, NULL);
×
563
    taosThreadClear(&pMgmt->crashReportThread);
×
564
  }
565
#endif
566
}
567

568
void dmStopMetricsThread(SDnodeMgmt *pMgmt) {
689,069✔
569
  if (taosCheckPthreadValid(pMgmt->metricsThread)) {
689,069✔
570
    (void)taosThreadJoin(pMgmt->metricsThread, NULL);
689,069✔
571
    taosThreadClear(&pMgmt->metricsThread);
689,069✔
572
  }
573
}
689,069✔
574

575
static void dmProcessMgmtQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
89,330,418✔
576
  SDnodeMgmt *pMgmt = pInfo->ahandle;
89,330,418✔
577
  int32_t     code = -1;
89,330,418✔
578
  STraceId   *trace = &pMsg->info.traceId;
89,330,418✔
579
  dGTrace("msg:%p, will be processed in dnode queue, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
89,330,418✔
580

581
  switch (pMsg->msgType) {
89,330,418✔
582
    case TDMT_DND_CONFIG_DNODE:
84,630✔
583
      code = dmProcessConfigReq(pMgmt, pMsg);
84,630✔
584
      break;
84,630✔
585
    case TDMT_MND_AUTH_RSP:
×
586
      code = dmProcessAuthRsp(pMgmt, pMsg);
×
587
      break;
×
588
    case TDMT_MND_GRANT_RSP:
×
589
      code = dmProcessGrantRsp(pMgmt, pMsg);
×
590
      break;
×
591
    case TDMT_DND_CREATE_MNODE:
31,347✔
592
      code = (*pMgmt->processCreateNodeFp)(MNODE, pMsg);
31,347✔
593
      break;
31,347✔
594
    case TDMT_DND_DROP_MNODE:
1,602✔
595
      code = (*pMgmt->processDropNodeFp)(MNODE, pMsg);
1,602✔
596
      break;
1,602✔
597
    case TDMT_DND_CREATE_QNODE:
10,608✔
598
      code = (*pMgmt->processCreateNodeFp)(QNODE, pMsg);
10,608✔
599
      break;
10,608✔
600
    case TDMT_DND_DROP_QNODE:
996✔
601
      code = (*pMgmt->processDropNodeFp)(QNODE, pMsg);
996✔
602
      break;
996✔
603
    case TDMT_DND_CREATE_SNODE:
93,053✔
604
      code = (*pMgmt->processCreateNodeFp)(SNODE, pMsg);
93,053✔
605
      break;
93,053✔
606
    case TDMT_DND_ALTER_SNODE:
166,179✔
607
      code = (*pMgmt->processAlterNodeFp)(SNODE, pMsg);
166,179✔
608
      break;
166,179✔
609
    case TDMT_DND_DROP_SNODE:
58,975✔
610
      code = (*pMgmt->processDropNodeFp)(SNODE, pMsg);
58,975✔
611
      break;
58,975✔
612
    case TDMT_DND_CREATE_BNODE:
36,022✔
613
      code = (*pMgmt->processCreateNodeFp)(BNODE, pMsg);
36,022✔
614
      break;
36,022✔
615
    case TDMT_DND_DROP_BNODE:
36,015✔
616
      code = (*pMgmt->processDropNodeFp)(BNODE, pMsg);
36,015✔
617
      break;
36,015✔
618
    case TDMT_DND_ALTER_MNODE_TYPE:
444,246✔
619
      code = (*pMgmt->processAlterNodeTypeFp)(MNODE, pMsg);
444,246✔
620
      break;
444,246✔
621
    case TDMT_DND_SERVER_STATUS:
808✔
622
      code = dmProcessServerRunStatus(pMgmt, pMsg);
808✔
623
      break;
808✔
624
    case TDMT_DND_SYSTABLE_RETRIEVE:
70,772✔
625
      code = dmProcessRetrieve(pMgmt, pMsg);
70,772✔
626
      break;
70,772✔
627
    case TDMT_MND_GRANT:
1,003,537✔
628
      code = dmProcessGrantReq(&pMgmt->pData->clusterId, pMsg);
1,003,537✔
629
      break;
1,003,537✔
630
    case TDMT_MND_GRANT_NOTIFY:
87,288,781✔
631
      code = dmProcessGrantNotify(NULL, pMsg);
87,288,781✔
632
      break;
87,288,781✔
633
    case TDMT_DND_CREATE_ENCRYPT_KEY:
2,847✔
634
      code = dmProcessCreateEncryptKeyReq(pMgmt, pMsg);
2,847✔
635
      break;
2,847✔
636
    case TDMT_DND_RELOAD_DNODE_TLS:
×
637
      code = dmProcessReloadTlsConfig(pMgmt, pMsg);
×
638
      // code = dmProcessReloadEncryptKeyReq(pMgmt, pMsg);
639
      break;
×
640
    default:
×
641

642
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
643
      dGError("msg:%p, not processed in mgmt queue, reason:%s", pMsg, tstrerror(code));
×
644
      break;
×
645
  }
646

647
  if (IsReq(pMsg)) {
89,330,418✔
648
    if (code != 0 && terrno != 0) code = terrno;
89,330,418✔
649
    SRpcMsg rsp = {
178,650,557✔
650
        .code = code,
651
        .pCont = pMsg->info.rsp,
89,330,418✔
652
        .contLen = pMsg->info.rspLen,
89,330,418✔
653
        .info = pMsg->info,
654
    };
655

656
    code = rpcSendResponse(&rsp);
89,330,418✔
657
    if (code != 0) {
89,330,418✔
658
      dError("failed to send response since %s", tstrerror(code));
×
659
    }
660
  }
661

662
  dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
89,330,418✔
663
  rpcFreeCont(pMsg->pCont);
89,330,418✔
664
  taosFreeQitem(pMsg);
89,330,418✔
665
}
89,330,418✔
666

667
int32_t dmDispatchStreamHbMsg(struct SDispatchWorkerPool* pPool, void* pParam, int32_t *pWorkerIdx) {
14,589,168✔
668
  SRpcMsg* pMsg = (SRpcMsg*)pParam;
14,589,168✔
669
  if (pMsg->code) {
14,589,168✔
670
    *pWorkerIdx = 0;
217,764✔
671
    return TSDB_CODE_SUCCESS;
217,764✔
672
  }
673
  SStreamMsgGrpHeader* pHeader = (SStreamMsgGrpHeader*)pMsg->pCont;
14,371,404✔
674
  *pWorkerIdx = pHeader->streamGid % tsNumOfStreamMgmtThreads;
14,371,404✔
675
  return TSDB_CODE_SUCCESS;
14,371,404✔
676
}
677

678

679
static void dmProcessStreamMgmtQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
14,589,168✔
680
  SDnodeMgmt *pMgmt = pInfo->ahandle;
14,589,168✔
681
  int32_t     code = -1;
14,589,168✔
682
  STraceId   *trace = &pMsg->info.traceId;
14,589,168✔
683
  dGTrace("msg:%p, will be processed in dnode stream mgmt queue, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
14,589,168✔
684

685
  switch (pMsg->msgType) {
14,589,168✔
686
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
14,589,168✔
687
      code = dmProcessStreamHbRsp(pMgmt, pMsg);
14,589,168✔
688
      break;
14,589,168✔
689
    default:
×
690
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
691
      dGError("msg:%p, not processed in mgmt queue, reason:%s", pMsg, tstrerror(code));
×
692
      break;
×
693
  }
694

695
  if (IsReq(pMsg)) {
14,589,168✔
696
    if (code != 0 && terrno != 0) code = terrno;
×
697
    SRpcMsg rsp = {
×
698
        .code = code,
699
        .pCont = pMsg->info.rsp,
×
700
        .contLen = pMsg->info.rspLen,
×
701
        .info = pMsg->info,
702
    };
703

704
    code = rpcSendResponse(&rsp);
×
705
    if (code != 0) {
×
706
      dError("failed to send response since %s", tstrerror(code));
×
707
    }
708
  }
709

710
  dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
14,589,168✔
711
  rpcFreeCont(pMsg->pCont);
14,589,168✔
712
  taosFreeQitem(pMsg);
14,589,168✔
713
}
14,589,168✔
714

715

716
int32_t dmStartWorker(SDnodeMgmt *pMgmt) {
689,069✔
717
  int32_t          code = 0;
689,069✔
718
  SSingleWorkerCfg cfg = {
689,069✔
719
      .min = 1,
720
      .max = 1,
721
      .name = "dnode-mgmt",
722
      .fp = (FItem)dmProcessMgmtQueue,
723
      .param = pMgmt,
724
  };
725
  if ((code = tSingleWorkerInit(&pMgmt->mgmtWorker, &cfg)) != 0) {
689,069✔
726
    dError("failed to start dnode-mgmt worker since %s", tstrerror(code));
×
727
    return code;
×
728
  }
729

730
  SDispatchWorkerPool* pStMgmtpool = &pMgmt->streamMgmtWorker;
689,069✔
731
  pStMgmtpool->max = tsNumOfStreamMgmtThreads;
689,069✔
732
  pStMgmtpool->name = "dnode-stream-mgmt";
689,069✔
733
  code = tDispatchWorkerInit(pStMgmtpool);
689,069✔
734
  if (code != 0) {
689,069✔
735
    dError("failed to start dnode-stream-mgmt worker since %s", tstrerror(code));
×
736
    return code;
×
737
  }
738
  code = tDispatchWorkerAllocQueue(pStMgmtpool, pMgmt, (FItem)dmProcessStreamMgmtQueue, dmDispatchStreamHbMsg);
689,069✔
739
  if (code != 0) {
689,069✔
740
    dError("failed to allocate dnode-stream-mgmt worker queue since %s", tstrerror(code));
×
741
    return code;
×
742
  }
743

744
  dDebug("dnode workers are initialized");
689,069✔
745
  return 0;
689,069✔
746
}
747

748
void dmStopWorker(SDnodeMgmt *pMgmt) {
689,069✔
749
  tSingleWorkerCleanup(&pMgmt->mgmtWorker);
689,069✔
750
  tDispatchWorkerCleanup(&pMgmt->streamMgmtWorker);
689,069✔
751
  dDebug("dnode workers are closed");
689,069✔
752
}
689,069✔
753

754
int32_t dmPutNodeMsgToMgmtQueue(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
89,330,418✔
755
  SSingleWorker *pWorker = &pMgmt->mgmtWorker;
89,330,418✔
756
  dTrace("msg:%p, put into worker %s", pMsg, pWorker->name);
89,330,418✔
757
  return taosWriteQitem(pWorker->queue, pMsg);
89,330,418✔
758
}
759

760
int32_t dmPutMsgToStreamMgmtQueue(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
14,589,168✔
761
  return tAddTaskIntoDispatchWorkerPool(&pMgmt->streamMgmtWorker, pMsg);
14,589,168✔
762
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc