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

taosdata / TDengine / #4800

16 Oct 2025 09:19AM UTC coverage: 53.935% (-7.1%) from 61.083%
#4800

push

travis-ci

web-flow
Merge b32e3a393 into a190048d5

134724 of 323629 branches covered (41.63%)

Branch coverage included in aggregate %.

184803 of 268802 relevant lines covered (68.75%)

69058627.2 hits per line

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

61.71
/source/dnode/mgmt/node_mgmt/src/dmMgmt.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 "dmMgmt.h"
18
#include "dmNodes.h"
19
#include "index.h"
20
#include "qworker.h"
21
#include "tcompression.h"
22
#include "tconv.h"
23
#include "tglobal.h"
24
#include "tgrant.h"
25
#include "tconv.h"
26
#include "stream.h"
27

28
static bool dmRequireNode(SDnode *pDnode, SMgmtWrapper *pWrapper) {
1,423,170✔
29
  SMgmtInputOpt input = dmBuildMgmtInputOpt(pWrapper);
1,423,170✔
30
  input.dnodeId = pDnode->data.dnodeId;
1,423,170✔
31

32
  bool    required = false;
1,423,170✔
33
  int32_t code = (*pWrapper->func.requiredFp)(&input, &required);
1,423,170✔
34
  if (!required) {
1,423,170✔
35
    dDebug("node:%s, does not require startup", pWrapper->name);
785,544✔
36
  } else {
37
    dDebug("node:%s, required to startup", pWrapper->name);
637,626✔
38
  }
39

40
  return required;
1,423,170!
41
}
42

43
int32_t dmInitDnode(SDnode *pDnode) {
237,195✔
44
  dDebug("start to create dnode");
237,195✔
45
  int32_t code = -1;
237,195✔
46
  char    path[PATH_MAX + 100] = {0};
237,195✔
47

48
  if ((code = dmInitVarsWrapper(pDnode)) != 0) {
237,195!
49
    goto _OVER;
×
50
  }
51

52
  // compress module init
53
  tsCompressInit(tsLossyColumns, tsFPrecision, tsDPrecision, tsMaxRange, tsCurRange, (int)tsIfAdtFse, tsCompressor);
237,195!
54

55
  pDnode->wrappers[DNODE].func = dmGetMgmtFunc();
237,195✔
56
  pDnode->wrappers[MNODE].func = mmGetMgmtFunc();
237,195✔
57
  pDnode->wrappers[VNODE].func = vmGetMgmtFunc();
237,195✔
58
  pDnode->wrappers[QNODE].func = qmGetMgmtFunc();
237,195✔
59
  pDnode->wrappers[SNODE].func = smGetMgmtFunc();
237,195✔
60
  pDnode->wrappers[BNODE].func = bmGetMgmtFunc();
237,195✔
61

62
  for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
1,660,365✔
63
    SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
1,423,170✔
64
    pWrapper->pDnode = pDnode;
1,423,170✔
65
    pWrapper->name = dmNodeName(ntype);
1,423,170✔
66
    pWrapper->ntype = ntype;
1,423,170✔
67
    (void)taosThreadRwlockInit(&pWrapper->lock, NULL);
1,423,170✔
68

69
    snprintf(path, sizeof(path), "%s%s%s", tsDataDir, TD_DIRSEP, pWrapper->name);
1,423,170!
70
    pWrapper->path = taosStrdup(path);
1,423,170!
71
    if (pWrapper->path == NULL) {
1,423,170!
72
      code = terrno;
×
73
      goto _OVER;
×
74
    }
75

76
    pWrapper->required = dmRequireNode(pDnode, pWrapper);
1,423,170✔
77
  }
78

79
  code = dmCheckRunning(tsDataDir, &pDnode->lockfile);
237,195✔
80
  if (code != 0) {
237,195✔
81
    goto _OVER;
101✔
82
  }
83

84
  if ((code = dmInitModule(pDnode)) != 0) {
237,094✔
85
    goto _OVER;
42✔
86
  }
87

88
  indexInit(tsNumOfCommitThreads);
237,052✔
89

90
  if ((code = dmInitStatusClient(pDnode)) != 0) {
237,052!
91
    goto _OVER;
×
92
  }
93
  if ((code = dmInitSyncClient(pDnode)) != 0) {
237,052!
94
    goto _OVER;
×
95
  }
96

97
  dmReportStartup("dnode-transport", "initialized");
237,052✔
98
  dDebug("dnode is created, ptr:%p", pDnode);
237,052✔
99
  code = 0;
237,052✔
100

101
_OVER:
237,195✔
102
  if (code != 0 && pDnode != NULL) {
237,195!
103
    dmClearVars(pDnode);
143✔
104
    pDnode = NULL;
143✔
105
    dError("failed to create dnode since %s", tstrerror(code));
143!
106
  }
107

108
  return code;
237,195✔
109
}
110

111
void dmCleanupDnode(SDnode *pDnode) {
237,052✔
112
  if (pDnode == NULL) {
237,052!
113
    return;
×
114
  }
115

116
  dmCleanupClient(pDnode);
237,052✔
117
  dmCleanupStatusClient(pDnode);
237,052✔
118
  dmCleanupSyncClient(pDnode);
237,052✔
119
  dmCleanupServer(pDnode);
237,052✔
120

121
  dmClearVars(pDnode);
237,052✔
122
  rpcCleanup();
237,052✔
123
  streamCleanup();
237,052✔
124
  indexCleanup();
237,052✔
125
  taosConvDestroy();
237,052✔
126

127
  // compress destroy
128
  tsCompressExit();
237,052✔
129

130
  dDebug("dnode is closed, ptr:%p", pDnode);
237,052✔
131
}
132

133
int32_t dmInitVarsWrapper(SDnode *pDnode) {
237,195✔
134
  int32_t code = dmInitVars(pDnode);
237,195✔
135
  if (code == -1) {
237,195!
136
    return terrno;
×
137
  }
138
  return 0;
237,195✔
139
}
140
int32_t dmInitVars(SDnode *pDnode) {
237,195✔
141
  int32_t     code = 0;
237,195✔
142
  SDnodeData *pData = &pDnode->data;
237,195✔
143
  pData->dnodeId = 0;
237,195✔
144
  pData->clusterId = 0;
237,195✔
145
  pData->dnodeVer = 0;
237,195✔
146
  pData->engineVer = 0;
237,195✔
147
  pData->updateTime = 0;
237,195✔
148
  pData->rebootTime = taosGetTimestampMs();
237,195✔
149
  pData->dropped = 0;
237,195✔
150
  pData->stopped = 0;
237,195✔
151
  char *machineId = NULL;
237,195✔
152
  code = tGetMachineId(&machineId);
237,195✔
153
  if (machineId) {
237,195!
154
    tstrncpy(pData->machineId, machineId, TSDB_MACHINE_ID_LEN + 1);
237,195!
155
    taosMemoryFreeClear(machineId);
237,195!
156
  } else {
157
#if defined(TD_ENTERPRISE) && !defined(GRANTS_CFG)
158
    code = TSDB_CODE_DNODE_NO_MACHINE_CODE;
×
159
    return terrno = code;
×
160
#endif
161
  }
162

163
  pData->dnodeHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
237,195✔
164
  if (pData->dnodeHash == NULL) {
237,195!
165
    dError("failed to init dnode hash");
×
166
    return terrno;
×
167
  }
168

169
  if ((code = dmReadEps(pData)) != 0) {
237,195!
170
    dError("failed to read file since %s", tstrerror(code));
×
171
    return code;
×
172
  }
173

174
#if defined(TD_ENTERPRISE) || defined(TD_ASTRA_TODO)
175
  tsiEncryptAlgorithm = pData->encryptAlgorigthm;
237,195✔
176
  tsiEncryptScope = pData->encryptScope;
237,195✔
177
  /*
178
  if(tsiEncryptAlgorithm != 0) {
179
    if(pData->machineId != NULL && strlen(pData->machineId) > 0){
180
      dInfo("get crypt key at startup, machineId:%s", pData->machineId);
181
      int32_t code = 0;
182

183
      //code = taosGetCryptKey(tsAuthCode, pData->machineId, tsCryptKey);
184
      code = 0;
185
      tstrncpy(tsEncryptKey, tsAuthCode, 16);
186

187
      if (code != 0) {
188
        if(code == -1){
189
          terrno = TSDB_CODE_DNODE_NO_ENCRYPT_KEY;
190
          dError("machine code changed, can't get crypt key");
191
        }
192
        if(code == -2){
193
          terrno = TSDB_CODE_DNODE_NO_ENCRYPT_KEY;
194
          dError("failed to get crypt key");
195
        }
196
        return -1;
197
      }
198

199
      if(strlen(tsEncryptKey) == 0){
200
        terrno = TSDB_CODE_DNODE_NO_ENCRYPT_KEY;
201
        dError("failed to get crypt key at startup since key is null, machineId:%s", pData->machineId);
202
        return -1;
203
      }
204
    }
205
    else{
206
      terrno = TSDB_CODE_DNODE_NO_MACHINE_CODE;
207
      dError("failed to get crypt key at startup, machineId:%s", pData->machineId);
208
      return -1;
209
    }
210
  }
211
  */
212
#endif
213

214
  if (pData->dropped) {
237,195!
215
    dError("dnode will not start since its already dropped");
×
216
    return -1;
×
217
  }
218

219
  (void)taosThreadRwlockInit(&pData->lock, NULL);
237,195✔
220
  (void)taosThreadMutexInit(&pData->statusInfolock, NULL);
237,195✔
221
  (void)taosThreadMutexInit(&pDnode->mutex, NULL);
237,195✔
222
  return 0;
237,195✔
223
}
224

225
extern SMonVloadInfo tsVinfo;
226

227
void dmClearVars(SDnode *pDnode) {
237,195✔
228
  for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
1,660,365✔
229
    SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
1,423,170✔
230
    taosMemoryFreeClear(pWrapper->path);
1,423,170!
231
    (void)taosThreadRwlockDestroy(&pWrapper->lock);
1,423,170✔
232
  }
233
  if (pDnode->lockfile != NULL) {
237,195!
234
    if (taosUnLockFile(pDnode->lockfile) != 0) {
237,195!
235
      dError("failed to unlock file");
×
236
    }
237

238
    (void)taosCloseFile(&pDnode->lockfile);
237,195✔
239
    pDnode->lockfile = NULL;
237,195✔
240
  }
241

242
  SDnodeData *pData = &pDnode->data;
237,195✔
243
  (void)taosThreadRwlockWrlock(&pData->lock);
237,195✔
244
  if (pData->oldDnodeEps != NULL) {
237,195!
245
    if (dmWriteEps(pData) == 0) {
×
246
      dmRemoveDnodePairs(pData);
×
247
    }
248
    taosArrayDestroy(pData->oldDnodeEps);
×
249
    pData->oldDnodeEps = NULL;
×
250
  }
251
  if (pData->dnodeEps != NULL) {
237,195!
252
    taosArrayDestroy(pData->dnodeEps);
237,195✔
253
    pData->dnodeEps = NULL;
237,195✔
254
  }
255
  if (pData->dnodeHash != NULL) {
237,195!
256
    taosHashCleanup(pData->dnodeHash);
237,195✔
257
    pData->dnodeHash = NULL;
237,195✔
258
  }
259
  (void)taosThreadRwlockUnlock(&pData->lock);
237,195✔
260

261
  (void)taosThreadRwlockDestroy(&pData->lock);
237,195✔
262

263
  dDebug("begin to lock status info when thread exit");
237,195✔
264
  if (taosThreadMutexLock(&pData->statusInfolock) != 0) {
237,195!
265
    dError("failed to lock status info lock");
×
266
    return;
×
267
  }
268
  if (tsVinfo.pVloads != NULL) {
237,195✔
269
    taosArrayDestroy(tsVinfo.pVloads);
49,432✔
270
    tsVinfo.pVloads = NULL;
49,432✔
271
  }
272
  if (taosThreadMutexUnlock(&pData->statusInfolock) != 0) {
237,195!
273
    dError("failed to unlock status info lock");
×
274
    return;
×
275
  }
276
  if (taosThreadMutexDestroy(&pData->statusInfolock) != 0) {
237,195!
277
    dError("failed to destroy status info lock");
×
278
  }
279
  memset(&pData->statusInfolock, 0, sizeof(pData->statusInfolock));
237,195!
280

281
  (void)taosThreadMutexDestroy(&pDnode->mutex);
237,195✔
282
  memset(&pDnode->mutex, 0, sizeof(pDnode->mutex));
237,195!
283
}
284

285
void dmSetStatus(SDnode *pDnode, EDndRunStatus status) {
474,104✔
286
  if (pDnode->status != status) {
474,104!
287
    dDebug("dnode status set from %s to %s", dmStatStr(pDnode->status), dmStatStr(status));
474,104✔
288
    pDnode->status = status;
474,104✔
289
  }
290
}
474,104✔
291

292
SMgmtWrapper *dmAcquireWrapper(SDnode *pDnode, EDndNodeType ntype) {
189,703✔
293
  SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
189,703✔
294
  SMgmtWrapper *pRetWrapper = pWrapper;
189,703✔
295

296
  (void)taosThreadRwlockRdlock(&pWrapper->lock);
189,703✔
297
  if (pWrapper->deployed) {
189,703!
298
    int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1);
132,918✔
299
    // dTrace("node:%s, is acquired, ref:%d", pWrapper->name, refCount);
300
  } else {
301
    pRetWrapper = NULL;
56,785✔
302
  }
303
  (void)taosThreadRwlockUnlock(&pWrapper->lock);
189,703✔
304

305
  return pRetWrapper;
189,703✔
306
}
307

308
int32_t dmMarkWrapper(SMgmtWrapper *pWrapper) {
745,865,790✔
309
  int32_t code = 0;
745,865,790✔
310

311
  (void)taosThreadRwlockRdlock(&pWrapper->lock);
745,865,790✔
312
  if (pWrapper->deployed) {
745,882,196✔
313
    int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1);
727,578,675✔
314
    // dTrace("node:%s, is marked, ref:%d", pWrapper->name, refCount);
315
  } else {
316
    switch (pWrapper->ntype) {
18,299,922!
317
      case MNODE:
5,710,089✔
318
        code = TSDB_CODE_MNODE_NOT_FOUND;
5,710,089✔
319
        break;
5,710,089✔
320
      case QNODE:
12,583,907✔
321
        code = TSDB_CODE_QNODE_NOT_FOUND;
12,583,907✔
322
        break;
12,583,907✔
323
      case SNODE:
×
324
        code = TSDB_CODE_SNODE_NOT_FOUND;
×
325
        break;
×
326
      case BNODE:
×
327
        code = TSDB_CODE_BNODE_NOT_FOUND;
×
328
        break;
×
329
      case VNODE:
5,936✔
330
        code = TSDB_CODE_VND_STOPPED;
5,936✔
331
        break;
5,936✔
332
      default:
×
333
        code = TSDB_CODE_APP_IS_STOPPING;
×
334
        break;
×
335
    }
336
  }
337
  (void)taosThreadRwlockUnlock(&pWrapper->lock);
745,882,925✔
338

339
  return code;
745,885,710✔
340
}
341

342
void dmReleaseWrapper(SMgmtWrapper *pWrapper) {
737,249,815✔
343
  if (pWrapper == NULL) return;
737,249,815✔
344

345
  (void)taosThreadRwlockRdlock(&pWrapper->lock);
727,691,531✔
346
  int32_t refCount = atomic_sub_fetch_32(&pWrapper->refCount, 1);
727,719,062✔
347
  (void)taosThreadRwlockUnlock(&pWrapper->lock);
727,712,928✔
348
  // dTrace("node:%s, is released, ref:%d", pWrapper->name, refCount);
349
}
350

351
static void dmGetServerStartupStatus(SDnode *pDnode, SServerStatusRsp *pStatus) {
×
352
  SDnodeMgmt *pMgmt = pDnode->wrappers[DNODE].pMgmt;
×
353
  pStatus->details[0] = 0;
×
354

355
  if (pDnode->status == DND_STAT_INIT) {
×
356
    pStatus->statusCode = TSDB_SRV_STATUS_NETWORK_OK;
×
357
    snprintf(pStatus->details, sizeof(pStatus->details), "%s: %s", pDnode->startup.name, pDnode->startup.desc);
×
358
  } else if (pDnode->status == DND_STAT_STOPPED) {
×
359
    pStatus->statusCode = TSDB_SRV_STATUS_EXTING;
×
360
  } else {
361
    pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_OK;
×
362
  }
363
}
×
364

365
void dmProcessNetTestReq(SDnode *pDnode, SRpcMsg *pMsg) {
×
366
  dDebug("msg:%p, net test req will be processed", pMsg);
×
367

368
  SRpcMsg rsp = {.info = pMsg->info};
×
369
  rsp.pCont = rpcMallocCont(pMsg->contLen);
×
370
  if (rsp.pCont == NULL) {
×
371
    rsp.code = TSDB_CODE_OUT_OF_MEMORY;
×
372
  } else {
373
    rsp.contLen = pMsg->contLen;
×
374
  }
375

376
  if (rpcSendResponse(&rsp) != 0) {
×
377
    dError("failed to send response, msg:%p", &rsp);
×
378
  }
379
  rpcFreeCont(pMsg->pCont);
×
380
}
×
381

382
void dmProcessServerStartupStatus(SDnode *pDnode, SRpcMsg *pMsg) {
×
383
  dDebug("msg:%p, server startup status req will be processed", pMsg);
×
384

385
  SServerStatusRsp statusRsp = {0};
×
386
  dmGetServerStartupStatus(pDnode, &statusRsp);
×
387

388
  SRpcMsg rsp = {.info = pMsg->info};
×
389
  int32_t contLen = tSerializeSServerStatusRsp(NULL, 0, &statusRsp);
×
390
  if (contLen < 0) {
×
391
    rsp.code = TSDB_CODE_OUT_OF_MEMORY;
×
392
  } else {
393
    rsp.pCont = rpcMallocCont(contLen);
×
394
    if (rsp.pCont != NULL) {
×
395
      if (tSerializeSServerStatusRsp(rsp.pCont, contLen, &statusRsp) < 0) {
×
396
        rsp.code = TSDB_CODE_APP_ERROR;
×
397
      } else {
398
        rsp.contLen = contLen;
×
399
      }
400
    }
401
  }
402

403
  if (rpcSendResponse(&rsp) != 0) {
×
404
    dError("failed to send response, msg:%p", &rsp);
×
405
  }
406
  rpcFreeCont(pMsg->pCont);
×
407
}
×
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