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

taosdata / TDengine / #3562

20 Dec 2024 09:57AM UTC coverage: 26.655% (-32.2%) from 58.812%
#3562

push

travis-ci

web-flow
Merge pull request #29229 from taosdata/enh/TS-5749-3.0

enh: seperate tsdb async tasks to different thread pools

21498 of 109421 branches covered (19.65%)

Branch coverage included in aggregate %.

66 of 96 new or added lines in 7 files covered. (68.75%)

39441 existing lines in 157 files now uncovered.

35007 of 102566 relevant lines covered (34.13%)

53922.97 hits per line

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

58.01
/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) {
215✔
28
  SMgmtInputOpt input = dmBuildMgmtInputOpt(pWrapper);
215✔
29

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

38
  return required;
215✔
39
}
40

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

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

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

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

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

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

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

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

81
  if ((code = dmInitModule(pDnode)) != 0) {
43!
UNCOV
82
    goto _OVER;
×
83
  }
84

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

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

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

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

106
  return code;
43✔
107
}
108

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

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

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

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

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

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

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

172
#if defined(TD_ENTERPRISE)
173
  tsiEncryptAlgorithm = pData->encryptAlgorigthm;
43✔
174
  tsiEncryptScope = pData->encryptScope;
43✔
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) {
43!
213
    dError("dnode will not start since its already dropped");
×
214
    return -1;
×
215
  }
216

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

223
extern SMonVloadInfo tsVinfo;
224

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

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

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

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

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

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

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

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

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

303
  return pRetWrapper;
4✔
304
}
305

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

309
  (void)taosThreadRwlockRdlock(&pWrapper->lock);
296,208✔
310
  if (pWrapper->deployed) {
296,854✔
311
    int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1);
283,897✔
312
    // dTrace("node:%s, is marked, ref:%d", pWrapper->name, refCount);
313
  } else {
314
    switch (pWrapper->ntype) {
12,957!
315
      case MNODE:
5,492✔
316
        code = TSDB_CODE_MNODE_NOT_FOUND;
5,492✔
317
        break;
5,492✔
318
      case QNODE:
6,593✔
319
        code = TSDB_CODE_QNODE_NOT_FOUND;
6,593✔
320
        break;
6,593✔
UNCOV
321
      case SNODE:
×
UNCOV
322
        code = TSDB_CODE_SNODE_NOT_FOUND;
×
UNCOV
323
        break;
×
324
      case VNODE:
872✔
325
        code = TSDB_CODE_VND_STOPPED;
872✔
326
        break;
872✔
327
      default:
×
328
        code = TSDB_CODE_APP_IS_STOPPING;
×
329
        break;
×
330
    }
331
  }
332
  (void)taosThreadRwlockUnlock(&pWrapper->lock);
296,903✔
333

334
  return code;
296,486✔
335
}
336

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

340
  (void)taosThreadRwlockRdlock(&pWrapper->lock);
283,753✔
341
  int32_t refCount = atomic_sub_fetch_32(&pWrapper->refCount, 1);
283,951✔
342
  (void)taosThreadRwlockUnlock(&pWrapper->lock);
283,956✔
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