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

taosdata / TDengine / #3664

20 Mar 2025 09:19AM UTC coverage: 35.63%. First build
#3664

push

travis-ci

web-flow
Merge 8112ba125 into 9b7434d0a

72910 of 278358 branches covered (26.19%)

Branch coverage included in aggregate %.

195 of 257 new or added lines in 17 files covered. (75.88%)

125571 of 278710 relevant lines covered (45.05%)

1012723.65 hits per line

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

59.67
/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 "tstream.h"
26

27
static bool dmRequireNode(SDnode *pDnode, SMgmtWrapper *pWrapper) {
365✔
28
  SMgmtInputOpt input = dmBuildMgmtInputOpt(pWrapper);
365✔
29

30
  bool    required = false;
365✔
31
  int32_t code = (*pWrapper->func.requiredFp)(&input, &required);
365✔
32
  if (!required) {
365✔
33
    dDebug("node:%s, does not require startup", pWrapper->name);
151✔
34
  } else {
35
    dDebug("node:%s, required to startup", pWrapper->name);
214✔
36
  }
37

38
  return required;
365✔
39
}
40

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

46
  if ((code = dmInitVarsWrapper(pDnode)) != 0) {
73!
47
    goto _OVER;
×
48
  }
49

50
  // compress module init
51
  tsCompressInit(tsLossyColumns, tsFPrecision, tsDPrecision, tsMaxRange, tsCurRange, (int)tsIfAdtFse, tsCompressor);
73✔
52

53
  pDnode->wrappers[DNODE].func = dmGetMgmtFunc();
73✔
54
  pDnode->wrappers[MNODE].func = mmGetMgmtFunc();
73✔
55
  pDnode->wrappers[VNODE].func = vmGetMgmtFunc();
73✔
56
  pDnode->wrappers[QNODE].func = qmGetMgmtFunc();
73✔
57
  pDnode->wrappers[SNODE].func = smGetMgmtFunc();
73✔
58
  pDnode->wrappers[XNODE].func = xmGetMgmtFunc();
59

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

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

365✔
74
    pWrapper->required = dmRequireNode(pDnode, pWrapper);
75
  }
76

73✔
77
  code = dmCheckRunning(tsDataDir, &pDnode->lockfile);
73!
78
  if (code != 0) {
×
79
    goto _OVER;
80
  }
81

73✔
82
  if ((code = dmInitModule(pDnode)) != 0) {
1✔
83
    goto _OVER;
84
  }
85

72✔
86
  indexInit(tsNumOfCommitThreads);
72✔
87
  streamMetaInit();
88

72!
89
  if ((code = dmInitStatusClient(pDnode)) != 0) {
×
90
    goto _OVER;
91
  }
72!
92
  if ((code = dmInitSyncClient(pDnode)) != 0) {
×
93
    goto _OVER;
94
  }
95

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

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

73✔
107
  return code;
108
}
109

21✔
110
void dmCleanupDnode(SDnode *pDnode) {
21!
111
  if (pDnode == NULL) {
×
112
    return;
113
  }
114

21✔
115
  dmCleanupClient(pDnode);
21✔
116
  dmCleanupStatusClient(pDnode);
21✔
117
  dmCleanupSyncClient(pDnode);
21✔
118
  dmCleanupServer(pDnode);
119

21✔
120
  dmClearVars(pDnode);
21✔
121
  rpcCleanup();
21✔
122
  streamMetaCleanup();
21✔
123
  indexCleanup();
21✔
124
  taosConvDestroy();
125

126
  // compress destroy
21✔
127
  tsCompressExit();
128

21✔
129
  dDebug("dnode is closed, ptr:%p", pDnode);
130
}
131

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

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

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

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

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

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

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

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

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

224
extern SMonVloadInfo tsVinfo;
225

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

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

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

22✔
260
  (void)taosThreadRwlockDestroy(&pData->lock);
261

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

22✔
280
  (void)taosThreadMutexDestroy(&pDnode->mutex);
22✔
281
  memset(&pDnode->mutex, 0, sizeof(pDnode->mutex));
282
}
283

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

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

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

9✔
304
  return pRetWrapper;
305
}
306

41,988✔
307
int32_t dmMarkWrapper(SMgmtWrapper *pWrapper) {
41,988✔
308
  int32_t code = 0;
309

41,988✔
310
  (void)taosThreadRwlockRdlock(&pWrapper->lock);
41,993✔
311
  if (pWrapper->deployed) {
40,849✔
312
    int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1);
313
    // dTrace("node:%s, is marked, ref:%d", pWrapper->name, refCount);
314
  } else {
1,144!
315
    switch (pWrapper->ntype) {
314✔
316
      case MNODE:
314✔
317
        code = TSDB_CODE_MNODE_NOT_FOUND;
314✔
318
        break;
830✔
319
      case QNODE:
830✔
320
        code = TSDB_CODE_QNODE_NOT_FOUND;
830✔
321
        break;
×
322
      case SNODE:
×
323
        code = TSDB_CODE_SNODE_NOT_FOUND;
×
324
        break;
×
NEW
325
      case XNODE:
×
NEW
326
        code = TSDB_CODE_XNODE_NOT_FOUND;
×
NEW
327
        break;
×
328
      case VNODE:
×
329
        code = TSDB_CODE_VND_STOPPED;
×
330
        break;
331
      default:
332
        code = TSDB_CODE_APP_IS_STOPPING;
41,990✔
333
        break;
334
    }
41,987✔
335
  }
336
  (void)taosThreadRwlockUnlock(&pWrapper->lock);
337

41,033✔
338
  return code;
41,033✔
339
}
340

40,847✔
341
void dmReleaseWrapper(SMgmtWrapper *pWrapper) {
40,853✔
342
  if (pWrapper == NULL) return;
40,853✔
343

344
  (void)taosThreadRwlockRdlock(&pWrapper->lock);
345
  int32_t refCount = atomic_sub_fetch_32(&pWrapper->refCount, 1);
346
  (void)taosThreadRwlockUnlock(&pWrapper->lock);
×
347
  // dTrace("node:%s, is released, ref:%d", pWrapper->name, refCount);
×
348
}
×
349

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

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

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

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

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

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

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

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

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