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

taosdata / TDengine / #4113

17 May 2025 06:43AM UTC coverage: 62.054% (-0.8%) from 62.857%
#4113

push

travis-ci

web-flow
Merge pull request #31115 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

154737 of 318088 branches covered (48.65%)

Branch coverage included in aggregate %.

175 of 225 new or added lines in 20 files covered. (77.78%)

5853 existing lines in 216 files now uncovered.

239453 of 317147 relevant lines covered (75.5%)

15121865.73 hits per line

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

55.14
/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

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

26
  int32_t upTimeCount = 0;
2,618✔
27
  int64_t upTime = 0;
2,618✔
28

29
  while (1) {
670,899✔
30
    taosMsleep(200);
673,517✔
31
    if (pMgmt->pData->dropped || pMgmt->pData->stopped) break;
673,517!
32

33
    int64_t curTime = taosGetTimestampMs();
670,899✔
34
    if (curTime < lastTime) lastTime = curTime;
670,899!
35
    float interval = (curTime - lastTime) / 1000.0f;
670,899✔
36
    if (interval >= tsStatusInterval) {
670,899✔
37
      dmSendStatusReq(pMgmt);
133,708✔
38
      lastTime = curTime;
133,708✔
39

40
      if ((upTimeCount = ((upTimeCount + 1) & 63)) == 0) {
133,708✔
41
        upTime = taosGetOsUptime() - tsDndStartOsUptime;
1,439✔
42
        tsDndUpTime = TMAX(tsDndUpTime, upTime);
1,439✔
43
      }
44
    }
45
  }
46

47
  return NULL;
2,618✔
48
}
49

50
static void *dmConfigThreadFp(void *param) {
2,618✔
51
  SDnodeMgmt *pMgmt = param;
2,618✔
52
  int64_t     lastTime = taosGetTimestampMs();
2,618✔
53
  setThreadName("dnode-config");
2,618✔
54
  while (1) {
13,076✔
55
    taosMsleep(200);
15,694✔
56
    if (pMgmt->pData->dropped || pMgmt->pData->stopped || tsConfigInited) break;
15,694!
57

58
    int64_t curTime = taosGetTimestampMs();
13,076✔
59
    if (curTime < lastTime) lastTime = curTime;
13,076!
60
    float interval = (curTime - lastTime) / 1000.0f;
13,076✔
61
    if (interval >= tsStatusInterval) {
13,076✔
62
      dmSendConfigReq(pMgmt);
2,612✔
63
      lastTime = curTime;
2,612✔
64
    }
65
  }
66
  return NULL;
2,618✔
67
}
68

69
static void *dmStatusInfoThreadFp(void *param) {
2,618✔
70
  SDnodeMgmt *pMgmt = param;
5,236✔
71
  int64_t     lastTime = taosGetTimestampMs();
2,618✔
72
  setThreadName("dnode-status-info");
2,618✔
73

74
  int32_t upTimeCount = 0;
2,618✔
75
  int64_t upTime = 0;
2,618✔
76

77
  while (1) {
686,604✔
78
    taosMsleep(200);
689,222✔
79
    if (pMgmt->pData->dropped || pMgmt->pData->stopped) break;
689,222!
80

81
    int64_t curTime = taosGetTimestampMs();
686,604✔
82
    if (curTime < lastTime) lastTime = curTime;
686,604!
83
    float interval = (curTime - lastTime) / 1000.0f;
686,604✔
84
    if (interval >= tsStatusInterval) {
686,604✔
85
      dmUpdateStatusInfo(pMgmt);
136,351✔
86
      lastTime = curTime;
136,351✔
87

88
      if ((upTimeCount = ((upTimeCount + 1) & 63)) == 0) {
136,351✔
89
        upTime = taosGetOsUptime() - tsDndStartOsUptime;
1,464✔
90
        tsDndUpTime = TMAX(tsDndUpTime, upTime);
1,464✔
91
      }
92
    }
93
  }
94

95
  return NULL;
2,618✔
96
}
97

98
#if defined(TD_ENTERPRISE)
99
SDmNotifyHandle dmNotifyHdl = {.state = 0};
100
#define TIMESERIES_STASH_NUM 5
101
static void *dmNotifyThreadFp(void *param) {
2,618✔
102
  SDnodeMgmt *pMgmt = param;
2,618✔
103
  int64_t     lastTime = taosGetTimestampMs();
2,618✔
104
  setThreadName("dnode-notify");
2,618✔
105

106
  if (tsem_init(&dmNotifyHdl.sem, 0, 0) != 0) {
2,618!
107
    return NULL;
×
108
  }
109

110
  // calculate approximate timeSeries per second
111
  int64_t  notifyTimeStamp[TIMESERIES_STASH_NUM];
112
  int64_t  notifyTimeSeries[TIMESERIES_STASH_NUM];
113
  int64_t  approximateTimeSeries = 0;
2,618✔
114
  uint64_t nTotalNotify = 0;
2,618✔
115
  int32_t  head, tail = 0;
2,618✔
116

117
  bool       wait = true;
2,618✔
118
  int32_t    nDnode = 0;
2,618✔
119
  int64_t    lastNotify = 0;
2,618✔
120
  int64_t    lastFetchDnode = 0;
2,618✔
121
  SNotifyReq req = {0};
2,618✔
122
  while (1) {
119,957✔
123
    if (pMgmt->pData->dropped || pMgmt->pData->stopped) break;
122,575!
124
    if (wait) tsem_wait(&dmNotifyHdl.sem);
119,957✔
125
    atomic_store_8(&dmNotifyHdl.state, 1);
119,957✔
126

127
    int64_t remainTimeSeries = grantRemain(TSDB_GRANT_TIMESERIES);
119,957✔
128
    if (remainTimeSeries == INT64_MAX || remainTimeSeries <= 0) {
119,957!
129
      goto _skip;
119,957✔
130
    }
UNCOV
131
    int64_t current = taosGetTimestampMs();
×
UNCOV
132
    if (current - lastFetchDnode > 1000) {
×
UNCOV
133
      nDnode = dmGetDnodeSize(pMgmt->pData);
×
UNCOV
134
      if (nDnode < 1) nDnode = 1;
×
UNCOV
135
      lastFetchDnode = current;
×
136
    }
UNCOV
137
    if (req.dnodeId == 0 || req.clusterId == 0) {
×
UNCOV
138
      req.dnodeId = pMgmt->pData->dnodeId;
×
UNCOV
139
      req.clusterId = pMgmt->pData->clusterId;
×
140
    }
141

UNCOV
142
    if (current - lastNotify < 10) {
×
143
      int64_t nCmprTimeSeries = approximateTimeSeries / 100;
×
144
      if (nCmprTimeSeries < 1e5) nCmprTimeSeries = 1e5;
×
145
      if (remainTimeSeries > nCmprTimeSeries * 10) {
×
146
        taosMsleep(10);
×
147
      } else if (remainTimeSeries > nCmprTimeSeries * 5) {
×
148
        taosMsleep(5);
×
149
      } else {
150
        taosMsleep(2);
×
151
      }
152
    }
153

UNCOV
154
    SMonVloadInfo vinfo = {0};
×
UNCOV
155
    (*pMgmt->getVnodeLoadsLiteFp)(&vinfo);
×
UNCOV
156
    req.pVloads = vinfo.pVloads;
×
UNCOV
157
    int32_t nVgroup = taosArrayGetSize(req.pVloads);
×
UNCOV
158
    int64_t nTimeSeries = 0;
×
UNCOV
159
    for (int32_t i = 0; i < nVgroup; ++i) {
×
160
      SVnodeLoadLite *vload = TARRAY_GET_ELEM(req.pVloads, i);
×
161
      nTimeSeries += vload->nTimeSeries;
×
162
    }
UNCOV
163
    notifyTimeSeries[tail] = nTimeSeries;
×
UNCOV
164
    notifyTimeStamp[tail] = taosGetTimestampNs();
×
UNCOV
165
    ++nTotalNotify;
×
166

UNCOV
167
    approximateTimeSeries = 0;
×
UNCOV
168
    if (nTotalNotify >= TIMESERIES_STASH_NUM) {
×
169
      head = tail - TIMESERIES_STASH_NUM + 1;
×
170
      if (head < 0) head += TIMESERIES_STASH_NUM;
×
171
      int64_t timeDiff = notifyTimeStamp[tail] - notifyTimeStamp[head];
×
172
      int64_t tsDiff = notifyTimeSeries[tail] - notifyTimeSeries[head];
×
173
      if (tsDiff > 0) {
×
174
        if (timeDiff > 0 && timeDiff < 1e9) {
×
175
          approximateTimeSeries = (double)tsDiff * 1e9 / timeDiff;
×
176
          if ((approximateTimeSeries * nDnode) > remainTimeSeries) {
×
177
            dmSendNotifyReq(pMgmt, &req);
×
178
          }
179
        } else {
180
          dmSendNotifyReq(pMgmt, &req);
×
181
        }
182
      }
183
    } else {
UNCOV
184
      dmSendNotifyReq(pMgmt, &req);
×
185
    }
UNCOV
186
    if (++tail == TIMESERIES_STASH_NUM) tail = 0;
×
187

UNCOV
188
    tFreeSNotifyReq(&req);
×
UNCOV
189
    lastNotify = taosGetTimestampMs();
×
190
  _skip:
119,957✔
191
    if (1 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 0)) {
119,957✔
192
      wait = true;
119,925✔
193
      continue;
119,925✔
194
    }
195
    wait = false;
32✔
196
  }
197

198
  return NULL;
2,618✔
199
}
200
#endif
201

202
#ifdef USE_MONITOR
203
static void *dmMonitorThreadFp(void *param) {
2,618✔
204
  SDnodeMgmt *pMgmt = param;
2,618✔
205
  int64_t     lastTime = taosGetTimestampMs();
2,618✔
206
  int64_t     lastTimeForBasic = taosGetTimestampMs();
2,618✔
207
  setThreadName("dnode-monitor");
2,618✔
208

209
  static int32_t TRIM_FREQ = 20;
210
  int32_t        trimCount = 0;
2,618✔
211

212
  while (1) {
686,121✔
213
    taosMsleep(200);
688,739✔
214
    if (pMgmt->pData->dropped || pMgmt->pData->stopped) break;
688,739!
215

216
    int64_t curTime = taosGetTimestampMs();
686,121✔
217

218
    if (curTime < lastTime) lastTime = curTime;
686,121!
219
    float interval = (curTime - lastTime) / 1000.0f;
686,121✔
220
    if (interval >= tsMonitorInterval) {
686,121✔
221
      (*pMgmt->sendMonitorReportFp)();
3,692✔
222
      (*pMgmt->monitorCleanExpiredSamplesFp)();
3,692✔
223
      lastTime = curTime;
3,692✔
224

225
      trimCount = (trimCount + 1) % TRIM_FREQ;
3,692✔
226
      if (trimCount == 0) {
3,692✔
227
        taosMemoryTrim(0, NULL);
56!
228
      }
229
    }
230
    if (atomic_val_compare_exchange_8(&tsNeedTrim, 1, 0)) {
686,121✔
231
      taosMemoryTrim(0, NULL);
25,722!
232
    }
233
  }
234

235
  return NULL;
2,618✔
236
}
237
#endif
238
#ifdef USE_AUDIT
239
static void *dmAuditThreadFp(void *param) {
2,618✔
240
  SDnodeMgmt *pMgmt = param;
2,618✔
241
  int64_t     lastTime = taosGetTimestampMs();
2,618✔
242
  setThreadName("dnode-audit");
2,618✔
243

244
  while (1) {
1,374,543✔
245
    taosMsleep(100);
1,377,161✔
246
    if (pMgmt->pData->dropped || pMgmt->pData->stopped) break;
1,377,161!
247

248
    int64_t curTime = taosGetTimestampMs();
1,374,543✔
249
    if (curTime < lastTime) lastTime = curTime;
1,374,543!
250
    float interval = curTime - lastTime;
1,374,543✔
251
    if (interval >= tsAuditInterval) {
1,374,543✔
252
      (*pMgmt->sendAuditRecordsFp)();
26,213✔
253
      lastTime = curTime;
26,213✔
254
    }
255
  }
256

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

276
  STelemAddrMgmt mgt = {0};
×
277
  code = taosTelemetryMgtInit(&mgt, tsTelemServer);
×
278
  if (code != 0) {
×
279
    dError("failed to init telemetry since %s", tstrerror(code));
×
280
    return NULL;
×
281
  }
282
  code = initCrashLogWriter();
×
283
  if (code != 0) {
×
284
    dError("failed to init crash log writer since %s", tstrerror(code));
×
285
    return NULL;
×
286
  }
287

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

306
          taosMsleep(sleepTime);
×
307
          loopTimes = 0;
×
308
          continue;
×
309
        }
310
      } else {
311
        dInfo("succeed to send crash report");
×
312
        truncateFile = true;
×
313
      }
314
    } else {
315
      dInfo("no crash info was found");
×
316
    }
317

318
    taosMemoryFree(pMsg);
×
319

320
    if (pMsg && msgLen > 0) {
×
321
      pMsg = NULL;
×
322
      continue;
×
323
    }
324

325
    if (pFile) {
×
326
      taosReleaseCrashLogFile(pFile, truncateFile);
×
327
      pFile = NULL;
×
328
      truncateFile = false;
×
329
    }
330

331
    taosMsleep(sleepTime);
×
332
    loopTimes = 0;
×
333
  }
334
  taosTelemetryDestroy(&mgt);
×
335

336
  return NULL;
×
337
}
338
#endif
339

340
int32_t dmStartStatusThread(SDnodeMgmt *pMgmt) {
2,618✔
341
  int32_t      code = 0;
2,618✔
342
  TdThreadAttr thAttr;
343
  (void)taosThreadAttrInit(&thAttr);
2,618✔
344
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
2,618✔
345
#ifdef TD_COMPACT_OS
346
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
347
#endif
348
  if (taosThreadCreate(&pMgmt->statusThread, &thAttr, dmStatusThreadFp, pMgmt) != 0) {
2,618!
349
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
350
    dError("failed to create status thread since %s", tstrerror(code));
×
351
    return code;
×
352
  }
353

354
  (void)taosThreadAttrDestroy(&thAttr);
2,618✔
355
  tmsgReportStartup("dnode-status", "initialized");
2,618✔
356
  return 0;
2,618✔
357
}
358

359
int32_t dmStartConfigThread(SDnodeMgmt *pMgmt) {
2,618✔
360
  int32_t      code = 0;
2,618✔
361
  TdThreadAttr thAttr;
362
  (void)taosThreadAttrInit(&thAttr);
2,618✔
363
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
2,618✔
364
#ifdef TD_COMPACT_OS
365
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
366
#endif
367
  if (taosThreadCreate(&pMgmt->configThread, &thAttr, dmConfigThreadFp, pMgmt) != 0) {
2,618!
368
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
369
    dError("failed to create config thread since %s", tstrerror(code));
×
370
    return code;
×
371
  }
372

373
  (void)taosThreadAttrDestroy(&thAttr);
2,618✔
374
  tmsgReportStartup("config-status", "initialized");
2,618✔
375
  return 0;
2,618✔
376
}
377

378
int32_t dmStartStatusInfoThread(SDnodeMgmt *pMgmt) {
2,618✔
379
  int32_t      code = 0;
2,618✔
380
  TdThreadAttr thAttr;
381
  (void)taosThreadAttrInit(&thAttr);
2,618✔
382
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
2,618✔
383
#ifdef TD_COMPACT_OS
384
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
385
#endif
386
  if (taosThreadCreate(&pMgmt->statusInfoThread, &thAttr, dmStatusInfoThreadFp, pMgmt) != 0) {
2,618!
387
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
388
    dError("failed to create status Info thread since %s", tstrerror(code));
×
389
    return code;
×
390
  }
391

392
  (void)taosThreadAttrDestroy(&thAttr);
2,618✔
393
  tmsgReportStartup("dnode-status-info", "initialized");
2,618✔
394
  return 0;
2,618✔
395
}
396

397
void dmStopStatusThread(SDnodeMgmt *pMgmt) {
2,618✔
398
  if (taosCheckPthreadValid(pMgmt->statusThread)) {
2,618!
399
    (void)taosThreadJoin(pMgmt->statusThread, NULL);
2,618✔
400
    taosThreadClear(&pMgmt->statusThread);
2,618✔
401
  }
402
}
2,618✔
403

404
void dmStopConfigThread(SDnodeMgmt *pMgmt) {
2,618✔
405
  if (taosCheckPthreadValid(pMgmt->configThread)) {
2,618!
406
    (void)taosThreadJoin(pMgmt->configThread, NULL);
2,618✔
407
    taosThreadClear(&pMgmt->configThread);
2,618✔
408
  }
409
}
2,618✔
410

411
void dmStopStatusInfoThread(SDnodeMgmt *pMgmt) {
2,618✔
412
  if (taosCheckPthreadValid(pMgmt->statusInfoThread)) {
2,618!
413
    (void)taosThreadJoin(pMgmt->statusInfoThread, NULL);
2,618✔
414
    taosThreadClear(&pMgmt->statusInfoThread);
2,618✔
415
  }
416
}
2,618✔
417
#ifdef TD_ENTERPRISE
418
int32_t dmStartNotifyThread(SDnodeMgmt *pMgmt) {
2,618✔
419
  int32_t      code = 0;
2,618✔
420
  TdThreadAttr thAttr;
421
  (void)taosThreadAttrInit(&thAttr);
2,618✔
422
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
2,618✔
423
  if (taosThreadCreate(&pMgmt->notifyThread, &thAttr, dmNotifyThreadFp, pMgmt) != 0) {
2,618!
424
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
425
    dError("failed to create notify thread since %s", tstrerror(code));
×
426
    return code;
×
427
  }
428

429
  (void)taosThreadAttrDestroy(&thAttr);
2,618✔
430
  tmsgReportStartup("dnode-notify", "initialized");
2,618✔
431
  return 0;
2,618✔
432
}
433

434
void dmStopNotifyThread(SDnodeMgmt *pMgmt) {
2,618✔
435
  if (taosCheckPthreadValid(pMgmt->notifyThread)) {
2,618!
436
    if (tsem_post(&dmNotifyHdl.sem) != 0) {
2,618!
437
      dError("failed to post notify sem");
×
438
    }
439

440
    (void)taosThreadJoin(pMgmt->notifyThread, NULL);
2,618✔
441
    taosThreadClear(&pMgmt->notifyThread);
2,618✔
442
  }
443
  if (tsem_destroy(&dmNotifyHdl.sem) != 0) {
2,618!
444
    dError("failed to destroy notify sem");
×
445
  }
446
}
2,618✔
447
#endif
448
int32_t dmStartMonitorThread(SDnodeMgmt *pMgmt) {
2,618✔
449
  int32_t      code = 0;
2,618✔
450
#ifdef USE_MONITOR
451
  TdThreadAttr thAttr;
452
  (void)taosThreadAttrInit(&thAttr);
2,618✔
453
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
2,618✔
454
  if (taosThreadCreate(&pMgmt->monitorThread, &thAttr, dmMonitorThreadFp, pMgmt) != 0) {
2,618!
455
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
456
    dError("failed to create monitor thread since %s", tstrerror(code));
×
457
    return code;
×
458
  }
459

460
  (void)taosThreadAttrDestroy(&thAttr);
2,618✔
461
  tmsgReportStartup("dnode-monitor", "initialized");
2,618✔
462
#endif
463
  return 0;
2,618✔
464
}
465

466
int32_t dmStartAuditThread(SDnodeMgmt *pMgmt) {
2,618✔
467
  int32_t      code = 0;
2,618✔
468
#ifdef USE_AUDIT  
469
  TdThreadAttr thAttr;
470
  (void)taosThreadAttrInit(&thAttr);
2,618✔
471
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
2,618✔
472
  if (taosThreadCreate(&pMgmt->auditThread, &thAttr, dmAuditThreadFp, pMgmt) != 0) {
2,618!
473
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
474
    dError("failed to create audit thread since %s", tstrerror(code));
×
475
    return code;
×
476
  }
477

478
  (void)taosThreadAttrDestroy(&thAttr);
2,618✔
479
  tmsgReportStartup("dnode-audit", "initialized");
2,618✔
480
#endif  
481
  return 0;
2,618✔
482
}
483

484
void dmStopMonitorThread(SDnodeMgmt *pMgmt) {
2,618✔
485
#ifdef USE_MONITOR
486
  if (taosCheckPthreadValid(pMgmt->monitorThread)) {
2,618!
487
    (void)taosThreadJoin(pMgmt->monitorThread, NULL);
2,618✔
488
    taosThreadClear(&pMgmt->monitorThread);
2,618✔
489
  }
490
#endif
491
}
2,618✔
492

493
void dmStopAuditThread(SDnodeMgmt *pMgmt) {
2,618✔
494
#ifdef USE_AUDIT
495
  if (taosCheckPthreadValid(pMgmt->auditThread)) {
2,618!
496
    (void)taosThreadJoin(pMgmt->auditThread, NULL);
2,618✔
497
    taosThreadClear(&pMgmt->auditThread);
2,618✔
498
  }
499
#endif
500
}
2,618✔
501

502
int32_t dmStartCrashReportThread(SDnodeMgmt *pMgmt) {
2,618✔
503
  int32_t code = 0;
2,618✔
504
#ifdef USE_REPORT
505
  if (!tsEnableCrashReport) {
2,618!
506
    return 0;
2,618✔
507
  }
508

509
  TdThreadAttr thAttr;
510
  (void)taosThreadAttrInit(&thAttr);
×
511
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
×
512
  if (taosThreadCreate(&pMgmt->crashReportThread, &thAttr, dmCrashReportThreadFp, pMgmt) != 0) {
×
513
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
514
    dError("failed to create crashReport thread since %s", tstrerror(code));
×
515
    return code;
×
516
  }
517

518
  (void)taosThreadAttrDestroy(&thAttr);
×
519
  tmsgReportStartup("dnode-crashReport", "initialized");
×
520
#endif
521
  return 0;
×
522
}
523

524
void dmStopCrashReportThread(SDnodeMgmt *pMgmt) {
2,618✔
525
#ifdef USE_REPORT
526
  if (!tsEnableCrashReport) {
2,618!
527
    return;
2,618✔
528
  }
529

530
  if (taosCheckPthreadValid(pMgmt->crashReportThread)) {
×
531
    (void)taosThreadJoin(pMgmt->crashReportThread, NULL);
×
532
    taosThreadClear(&pMgmt->crashReportThread);
×
533
  }
534
#endif
535
}
536

537
static void dmProcessMgmtQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
279,941✔
538
  SDnodeMgmt *pMgmt = pInfo->ahandle;
279,941✔
539
  int32_t     code = -1;
279,941✔
540
  STraceId   *trace = &pMsg->info.traceId;
279,941✔
541
  dGTrace("msg:%p, will be processed in dnode queue, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
279,941!
542

543
  switch (pMsg->msgType) {
279,941!
544
    case TDMT_DND_CONFIG_DNODE:
1,368✔
545
      code = dmProcessConfigReq(pMgmt, pMsg);
1,368✔
546
      break;
1,368✔
547
    case TDMT_MND_AUTH_RSP:
×
548
      code = dmProcessAuthRsp(pMgmt, pMsg);
×
549
      break;
×
550
    case TDMT_MND_GRANT_RSP:
×
551
      code = dmProcessGrantRsp(pMgmt, pMsg);
×
552
      break;
×
553
    case TDMT_DND_CREATE_MNODE:
107✔
554
      code = (*pMgmt->processCreateNodeFp)(MNODE, pMsg);
107✔
555
      break;
107✔
556
    case TDMT_DND_DROP_MNODE:
7✔
557
      code = (*pMgmt->processDropNodeFp)(MNODE, pMsg);
7✔
558
      break;
7✔
559
    case TDMT_DND_CREATE_QNODE:
491✔
560
      code = (*pMgmt->processCreateNodeFp)(QNODE, pMsg);
491✔
561
      break;
491✔
562
    case TDMT_DND_DROP_QNODE:
13✔
563
      code = (*pMgmt->processDropNodeFp)(QNODE, pMsg);
13✔
564
      break;
13✔
565
    case TDMT_DND_CREATE_SNODE:
23✔
566
      code = (*pMgmt->processCreateNodeFp)(SNODE, pMsg);
23✔
567
      break;
23✔
568
    case TDMT_DND_DROP_SNODE:
7✔
569
      code = (*pMgmt->processDropNodeFp)(SNODE, pMsg);
7✔
570
      break;
7✔
571
    case TDMT_DND_ALTER_MNODE_TYPE:
881✔
572
      code = (*pMgmt->processAlterNodeTypeFp)(MNODE, pMsg);
881✔
573
      break;
881✔
574
    case TDMT_DND_SERVER_STATUS:
3✔
575
      code = dmProcessServerRunStatus(pMgmt, pMsg);
3✔
576
      break;
3✔
577
    case TDMT_DND_SYSTABLE_RETRIEVE:
4,516✔
578
      code = dmProcessRetrieve(pMgmt, pMsg);
4,516✔
579
      break;
4,516✔
580
    case TDMT_MND_GRANT:
3,083✔
581
      code = dmProcessGrantReq(&pMgmt->pData->clusterId, pMsg);
3,083✔
582
      break;
3,083✔
583
    case TDMT_MND_GRANT_NOTIFY:
269,442✔
584
      code = dmProcessGrantNotify(NULL, pMsg);
269,442✔
585
      break;
269,442✔
UNCOV
586
    case TDMT_DND_CREATE_ENCRYPT_KEY:
×
UNCOV
587
      code = dmProcessCreateEncryptKeyReq(pMgmt, pMsg);
×
UNCOV
588
      break;
×
589
    default:
×
590
      code = TSDB_CODE_MSG_NOT_PROCESSED;
×
591
      dGError("msg:%p, not processed in mgmt queue, reason:%s", pMsg, tstrerror(code));
×
592
      break;
×
593
  }
594

595
  if (IsReq(pMsg)) {
279,941!
596
    if (code != 0 && terrno != 0) code = terrno;
279,941✔
597
    SRpcMsg rsp = {
279,941✔
598
        .code = code,
599
        .pCont = pMsg->info.rsp,
279,941✔
600
        .contLen = pMsg->info.rspLen,
279,941✔
601
        .info = pMsg->info,
602
    };
603

604
    code = rpcSendResponse(&rsp);
279,941✔
605
    if (code != 0) {
279,941!
606
      dError("failed to send response since %s", tstrerror(code));
×
607
    }
608
  }
609

610
  dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
279,941✔
611
  rpcFreeCont(pMsg->pCont);
279,941✔
612
  taosFreeQitem(pMsg);
279,941✔
613
}
279,941✔
614

615
int32_t dmStartWorker(SDnodeMgmt *pMgmt) {
2,618✔
616
  int32_t          code = 0;
2,618✔
617
  SSingleWorkerCfg cfg = {
2,618✔
618
      .min = 1,
619
      .max = 1,
620
      .name = "dnode-mgmt",
621
      .fp = (FItem)dmProcessMgmtQueue,
622
      .param = pMgmt,
623
  };
624
  if ((code = tSingleWorkerInit(&pMgmt->mgmtWorker, &cfg)) != 0) {
2,618!
625
    dError("failed to start dnode-mgmt worker since %s", tstrerror(code));
×
626
    return code;
×
627
  }
628

629
  dDebug("dnode workers are initialized");
2,618✔
630
  return 0;
2,618✔
631
}
632

633
void dmStopWorker(SDnodeMgmt *pMgmt) {
2,618✔
634
  tSingleWorkerCleanup(&pMgmt->mgmtWorker);
2,618✔
635
  dDebug("dnode workers are closed");
2,618✔
636
}
2,618✔
637

638
int32_t dmPutNodeMsgToMgmtQueue(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
279,925✔
639
  SSingleWorker *pWorker = &pMgmt->mgmtWorker;
279,925✔
640
  dTrace("msg:%p, put into worker %s", pMsg, pWorker->name);
279,925✔
641
  return taosWriteQitem(pWorker->queue, pMsg);
279,925✔
642
}
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