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

taosdata / TDengine / #3819

01 Apr 2025 09:27AM UTC coverage: 34.076% (+0.01%) from 34.065%
#3819

push

travis-ci

happyguoxy
test:alter gcda dir

148544 of 599532 branches covered (24.78%)

Branch coverage included in aggregate %.

222541 of 489451 relevant lines covered (45.47%)

763329.1 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 "tglobal.h"
23
#include "tgrant.h"
24
#include "tstream.h"
25
#include "tconv.h"
26

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

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

38
  return required;
695✔
39
}
40

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

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

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

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

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

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

73
    pWrapper->required = dmRequireNode(pDnode, pWrapper);
695✔
74
  }
75

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

81
  if ((code = dmInitModule(pDnode)) != 0) {
139✔
82
    goto _OVER;
3✔
83
  }
84

85
  indexInit(tsNumOfCommitThreads);
136✔
86
  streamMetaInit();
136✔
87

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

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

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

106
  return code;
139✔
107
}
108

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

114
  dmCleanupClient(pDnode);
70✔
115
  dmCleanupStatusClient(pDnode);
70✔
116
  dmCleanupSyncClient(pDnode);
70✔
117
  dmCleanupServer(pDnode);
70✔
118

119
  dmClearVars(pDnode);
70✔
120
  rpcCleanup();
70✔
121
  streamMetaCleanup();
70✔
122
  indexCleanup();
70✔
123
  taosConvDestroy();
70✔
124

125
  // compress destroy
126
  tsCompressExit();
70✔
127

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

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

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

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

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

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

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

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

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

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

223
extern SMonVloadInfo tsVinfo;
224

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

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

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

259
  (void)taosThreadRwlockDestroy(&pData->lock);
73✔
260

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

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

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

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

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

303
  return pRetWrapper;
43✔
304
}
305

306
int32_t dmMarkWrapper(SMgmtWrapper *pWrapper) {
258,027✔
307
  int32_t code = 0;
258,027✔
308

309
  (void)taosThreadRwlockRdlock(&pWrapper->lock);
258,027✔
310
  if (pWrapper->deployed) {
258,030✔
311
    int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1);
252,689✔
312
    // dTrace("node:%s, is marked, ref:%d", pWrapper->name, refCount);
313
  } else {
314
    switch (pWrapper->ntype) {
5,341!
315
      case MNODE:
1,612✔
316
        code = TSDB_CODE_MNODE_NOT_FOUND;
1,612✔
317
        break;
1,612✔
318
      case QNODE:
3,729✔
319
        code = TSDB_CODE_QNODE_NOT_FOUND;
3,729✔
320
        break;
3,729✔
321
      case SNODE:
×
322
        code = TSDB_CODE_SNODE_NOT_FOUND;
×
323
        break;
×
324
      case VNODE:
×
325
        code = TSDB_CODE_VND_STOPPED;
×
326
        break;
×
327
      default:
×
328
        code = TSDB_CODE_APP_IS_STOPPING;
×
329
        break;
×
330
    }
331
  }
332
  (void)taosThreadRwlockUnlock(&pWrapper->lock);
258,028✔
333

334
  return code;
258,026✔
335
}
336

337
void dmReleaseWrapper(SMgmtWrapper *pWrapper) {
256,877✔
338
  if (pWrapper == NULL) return;
256,877✔
339

340
  (void)taosThreadRwlockRdlock(&pWrapper->lock);
252,704✔
341
  int32_t refCount = atomic_sub_fetch_32(&pWrapper->refCount, 1);
252,712✔
342
  (void)taosThreadRwlockUnlock(&pWrapper->lock);
252,717✔
343
  // dTrace("node:%s, is released, ref:%d", pWrapper->name, refCount);
344
}
345

346
static void dmGetServerStartupStatus(SDnode *pDnode, SServerStatusRsp *pStatus) {
×
347
  SDnodeMgmt *pMgmt = pDnode->wrappers[DNODE].pMgmt;
×
348
  pStatus->details[0] = 0;
×
349

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

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

363
  SRpcMsg rsp = {.info = pMsg->info};
×
364
  rsp.pCont = rpcMallocCont(pMsg->contLen);
×
365
  if (rsp.pCont == NULL) {
×
366
    rsp.code = TSDB_CODE_OUT_OF_MEMORY;
×
367
  } else {
368
    rsp.contLen = pMsg->contLen;
×
369
  }
370

371
  if (rpcSendResponse(&rsp) != 0) {
×
372
    dError("failed to send response, msg:%p", &rsp);
×
373
  }
374
  rpcFreeCont(pMsg->pCont);
×
375
}
×
376

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

380
  SServerStatusRsp statusRsp = {0};
×
381
  dmGetServerStartupStatus(pDnode, &statusRsp);
×
382

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

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