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

taosdata / TDengine / #4720

08 Sep 2025 08:43AM UTC coverage: 58.139% (-0.6%) from 58.762%
#4720

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

133181 of 292179 branches covered (45.58%)

Branch coverage included in aggregate %.

201691 of 283811 relevant lines covered (71.07%)

5442780.71 hits per line

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

61.31
/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) {
14,352✔
29
  SMgmtInputOpt input = dmBuildMgmtInputOpt(pWrapper);
14,352✔
30
  input.dnodeId = pDnode->data.dnodeId;
14,352✔
31

32
  bool    required = false;
14,352✔
33
  int32_t code = (*pWrapper->func.requiredFp)(&input, &required);
14,352✔
34
  if (!required) {
14,352✔
35
    dDebug("node:%s, does not require startup", pWrapper->name);
7,776✔
36
  } else {
37
    dDebug("node:%s, required to startup", pWrapper->name);
6,576✔
38
  }
39

40
  return required;
14,352✔
41
}
42

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

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

52
  // compress module init
53
  tsCompressInit(tsLossyColumns, tsFPrecision, tsDPrecision, tsMaxRange, tsCurRange, (int)tsIfAdtFse, tsCompressor);
2,392✔
54

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

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

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

76
    pWrapper->required = dmRequireNode(pDnode, pWrapper);
14,352✔
77
  }
78

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

84
  if ((code = dmInitModule(pDnode)) != 0) {
2,391!
85
    goto _OVER;
×
86
  }
87

88
  indexInit(tsNumOfCommitThreads);
2,391✔
89

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

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

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

108
  return code;
2,392✔
109
}
110

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

116
  dmCleanupClient(pDnode);
2,391✔
117
  dmCleanupStatusClient(pDnode);
2,391✔
118
  dmCleanupSyncClient(pDnode);
2,391✔
119
  dmCleanupServer(pDnode);
2,391✔
120

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

127
  // compress destroy
128
  tsCompressExit();
2,391✔
129

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

133
int32_t dmInitVarsWrapper(SDnode *pDnode) {
2,392✔
134
  int32_t code = dmInitVars(pDnode);
2,392✔
135
  if (code == -1) {
2,392!
136
    return terrno;
×
137
  }
138
  return 0;
2,392✔
139
}
140
int32_t dmInitVars(SDnode *pDnode) {
2,392✔
141
  int32_t     code = 0;
2,392✔
142
  SDnodeData *pData = &pDnode->data;
2,392✔
143
  pData->dnodeId = 0;
2,392✔
144
  pData->clusterId = 0;
2,392✔
145
  pData->dnodeVer = 0;
2,392✔
146
  pData->engineVer = 0;
2,392✔
147
  pData->updateTime = 0;
2,392✔
148
  pData->rebootTime = taosGetTimestampMs();
2,392✔
149
  pData->dropped = 0;
2,392✔
150
  pData->stopped = 0;
2,392✔
151
  char *machineId = NULL;
2,392✔
152
  code = tGetMachineId(&machineId);
2,392✔
153
  if (machineId) {
2,392!
154
    tstrncpy(pData->machineId, machineId, TSDB_MACHINE_ID_LEN + 1);
2,392✔
155
    taosMemoryFreeClear(machineId);
2,392!
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);
2,392✔
164
  if (pData->dnodeHash == NULL) {
2,392!
165
    dError("failed to init dnode hash");
×
166
    return terrno;
×
167
  }
168

169
  if ((code = dmReadEps(pData)) != 0) {
2,392!
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;
2,392✔
176
  tsiEncryptScope = pData->encryptScope;
2,392✔
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) {
2,392!
215
    dError("dnode will not start since its already dropped");
×
216
    return -1;
×
217
  }
218

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

225
extern SMonVloadInfo tsVinfo;
226

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

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

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

261
  (void)taosThreadRwlockDestroy(&pData->lock);
2,392✔
262

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

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

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

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

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

305
  return pRetWrapper;
2,752✔
306
}
307

308
int32_t dmMarkWrapper(SMgmtWrapper *pWrapper) {
9,633,533✔
309
  int32_t code = 0;
9,633,533✔
310

311
  (void)taosThreadRwlockRdlock(&pWrapper->lock);
9,633,533✔
312
  if (pWrapper->deployed) {
9,634,542✔
313
    int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1);
9,520,128✔
314
    // dTrace("node:%s, is marked, ref:%d", pWrapper->name, refCount);
315
  } else {
316
    switch (pWrapper->ntype) {
114,414!
317
      case MNODE:
35,195✔
318
        code = TSDB_CODE_MNODE_NOT_FOUND;
35,195✔
319
        break;
35,195✔
320
      case QNODE:
78,656✔
321
        code = TSDB_CODE_QNODE_NOT_FOUND;
78,656✔
322
        break;
78,656✔
323
      case SNODE:
1✔
324
        code = TSDB_CODE_SNODE_NOT_FOUND;
1✔
325
        break;
1✔
326
      case BNODE:
×
327
        code = TSDB_CODE_BNODE_NOT_FOUND;
×
328
        break;
×
329
      case VNODE:
562✔
330
        code = TSDB_CODE_VND_STOPPED;
562✔
331
        break;
562✔
332
      default:
×
333
        code = TSDB_CODE_APP_IS_STOPPING;
×
334
        break;
×
335
    }
336
  }
337
  (void)taosThreadRwlockUnlock(&pWrapper->lock);
9,634,719✔
338

339
  return code;
9,634,120✔
340
}
341

342
void dmReleaseWrapper(SMgmtWrapper *pWrapper) {
9,547,612✔
343
  if (pWrapper == NULL) return;
9,547,612✔
344

345
  (void)taosThreadRwlockRdlock(&pWrapper->lock);
9,521,695✔
346
  int32_t refCount = atomic_sub_fetch_32(&pWrapper->refCount, 1);
9,522,223✔
347
  (void)taosThreadRwlockUnlock(&pWrapper->lock);
9,522,225✔
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

© 2025 Coveralls, Inc