• 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

59.88
/source/dnode/mgmt/mgmt_vnode/src/vmInt.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 "vmInt.h"
18
#include "libs/function/tudf.h"
19
#include "osMemory.h"
20
#include "tfs.h"
21
#include "vnd.h"
22

23
int32_t vmGetPrimaryDisk(SVnodeMgmt *pMgmt, int32_t vgId) {
101✔
24
  int32_t    diskId = -1;
101✔
25
  SVnodeObj *pVnode = NULL;
101✔
26

27
  (void)taosThreadRwlockRdlock(&pMgmt->lock);
101✔
28
  int32_t r = taosHashGetDup(pMgmt->runngingHash, &vgId, sizeof(int32_t), (void *)&pVnode);
101✔
29
  if (pVnode != NULL) {
101!
30
    diskId = pVnode->diskPrimary;
×
31
  }
32
  (void)taosThreadRwlockUnlock(&pMgmt->lock);
101✔
33
  return diskId;
101✔
34
}
35

36
static void vmFreeVnodeObj(SVnodeObj **ppVnode) {
1,103✔
37
  if (!ppVnode || !(*ppVnode)) return;
1,103!
38

39
  SVnodeObj *pVnode = *ppVnode;
1,103✔
40

41
  int32_t refCount = atomic_load_32(&pVnode->refCount);
1,103✔
42
  while (refCount > 0) {
1,103!
43
    dWarn("vgId:%d, vnode is refenced, retry to free in 200ms, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
×
44
    taosMsleep(200);
×
45
    refCount = atomic_load_32(&pVnode->refCount);
×
46
  }
47

48
  taosMemoryFree(pVnode->path);
1,103!
49
  taosMemoryFree(pVnode);
1,103!
50
  ppVnode[0] = NULL;
1,103✔
51
}
52

53
static int32_t vmRegisterCreatingState(SVnodeMgmt *pMgmt, int32_t vgId, int32_t diskId) {
101✔
54
  int32_t    code = 0;
101✔
55
  SVnodeObj *pCreatingVnode = taosMemoryCalloc(1, sizeof(SVnodeObj));
101!
56
  if (pCreatingVnode == NULL) {
101!
57
    dError("failed to alloc vnode since %s", terrstr());
×
58
    return terrno;
×
59
  }
60
  (void)memset(pCreatingVnode, 0, sizeof(SVnodeObj));
101✔
61

62
  pCreatingVnode->vgId = vgId;
101✔
63
  pCreatingVnode->diskPrimary = diskId;
101✔
64

65
  code = taosThreadRwlockWrlock(&pMgmt->lock);
101✔
66
  if (code != 0) {
101!
67
    taosMemoryFree(pCreatingVnode);
×
68
    return code;
×
69
  }
70

71
  dTrace("vgId:%d, put vnode into creating hash, pCreatingVnode:%p", vgId, pCreatingVnode);
101✔
72
  code = taosHashPut(pMgmt->creatingHash, &vgId, sizeof(int32_t), &pCreatingVnode, sizeof(SVnodeObj *));
101✔
73
  if (code != 0) {
101!
74
    dError("vgId:%d, failed to put vnode to creatingHash", vgId);
×
75
    taosMemoryFree(pCreatingVnode);
×
76
  }
77

78
  int32_t r = taosThreadRwlockUnlock(&pMgmt->lock);
101✔
79
  if (r != 0) {
101!
80
    dError("vgId:%d, failed to unlock since %s", vgId, tstrerror(r));
×
81
  }
82

83
  return code;
101✔
84
}
85

86
static void vmUnRegisterCreatingState(SVnodeMgmt *pMgmt, int32_t vgId) {
101✔
87
  SVnodeObj *pOld = NULL;
101✔
88

89
  (void)taosThreadRwlockWrlock(&pMgmt->lock);
101✔
90
  int32_t r = taosHashGetDup(pMgmt->creatingHash, &vgId, sizeof(int32_t), (void *)&pOld);
101✔
91
  if (r != 0) {
101!
92
    dError("vgId:%d, failed to get vnode from creating Hash", vgId);
×
93
  }
94
  dTrace("vgId:%d, remove from creating Hash", vgId);
101✔
95
  r = taosHashRemove(pMgmt->creatingHash, &vgId, sizeof(int32_t));
101✔
96
  if (r != 0) {
101!
97
    dError("vgId:%d, failed to remove vnode from creatingHash", vgId);
×
98
  }
99
  (void)taosThreadRwlockUnlock(&pMgmt->lock);
101✔
100

101
  if (pOld) {
101!
102
    dTrace("vgId:%d, free vnode pOld:%p", vgId, &pOld);
101✔
103
    vmFreeVnodeObj(&pOld);
101✔
104
  }
105

106
_OVER:
×
107
  if (r != 0) {
101!
108
    dError("vgId:%d, failed to remove vnode from creatingHash since %s", vgId, tstrerror(r));
×
109
  }
110
}
101✔
111

112
int32_t vmAllocPrimaryDisk(SVnodeMgmt *pMgmt, int32_t vgId) {
101✔
113
  int32_t code = 0;
101✔
114
  STfs   *pTfs = pMgmt->pTfs;
101✔
115
  int32_t diskId = 0;
101✔
116
  if (!pTfs) {
101!
117
    return diskId;
×
118
  }
119

120
  // search fs
121
  char vnodePath[TSDB_FILENAME_LEN] = {0};
101✔
122
  snprintf(vnodePath, TSDB_FILENAME_LEN - 1, "vnode%svnode%d", TD_DIRSEP, vgId);
101✔
123
  char fname[TSDB_FILENAME_LEN] = {0};
101✔
124
  char fnameTmp[TSDB_FILENAME_LEN] = {0};
101✔
125
  snprintf(fname, TSDB_FILENAME_LEN - 1, "%s%s%s", vnodePath, TD_DIRSEP, VND_INFO_FNAME);
101✔
126
  snprintf(fnameTmp, TSDB_FILENAME_LEN - 1, "%s%s%s", vnodePath, TD_DIRSEP, VND_INFO_FNAME_TMP);
101✔
127

128
  diskId = tfsSearch(pTfs, 0, fname);
101✔
129
  if (diskId >= 0) {
101!
130
    return diskId;
×
131
  }
132
  diskId = tfsSearch(pTfs, 0, fnameTmp);
101✔
133
  if (diskId >= 0) {
101!
134
    return diskId;
×
135
  }
136

137
  // alloc
138
  int32_t     disks[TFS_MAX_DISKS_PER_TIER] = {0};
101✔
139
  int32_t     numOfVnodes = 0;
101✔
140
  SVnodeObj **ppVnodes = NULL;
101✔
141

142
  code = taosThreadMutexLock(&pMgmt->mutex);
101✔
143
  if (code != 0) {
101!
144
    return code;
×
145
  }
146

147
  code = vmGetAllVnodeListFromHashWithCreating(pMgmt, &numOfVnodes, &ppVnodes);
101✔
148
  if (code != 0) {
101!
149
    int32_t r = taosThreadMutexUnlock(&pMgmt->mutex);
×
150
    if (r != 0) {
×
151
      dError("vgId:%d, failed to unlock mutex since %s", vgId, tstrerror(r));
×
152
    }
153
    return code;
×
154
  }
155

156
  for (int32_t v = 0; v < numOfVnodes; v++) {
163✔
157
    SVnodeObj *pVnode = ppVnodes[v];
62✔
158
    disks[pVnode->diskPrimary] += 1;
62✔
159
  }
160

161
  int32_t minVal = INT_MAX;
101✔
162
  int32_t ndisk = tfsGetDisksAtLevel(pTfs, 0);
101✔
163
  diskId = 0;
101✔
164
  for (int32_t id = 0; id < ndisk; id++) {
202✔
165
    if (minVal > disks[id]) {
101!
166
      minVal = disks[id];
101✔
167
      diskId = id;
101✔
168
    }
169
  }
170
  code = vmRegisterCreatingState(pMgmt, vgId, diskId);
101✔
171
  if (code != 0) {
101!
172
    int32_t r = taosThreadMutexUnlock(&pMgmt->mutex);
×
173
    if (r != 0) {
×
174
      dError("vgId:%d, failed to unlock mutex since %s", vgId, tstrerror(r));
×
175
    }
176
    goto _OVER;
×
177
  }
178

179
  code = taosThreadMutexUnlock(&pMgmt->mutex);
101✔
180
  if (code != 0) {
101!
181
    goto _OVER;
×
182
  }
183

184
_OVER:
101✔
185

186
  for (int32_t i = 0; i < numOfVnodes; ++i) {
163✔
187
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
62!
188
    vmReleaseVnode(pMgmt, ppVnodes[i]);
62✔
189
  }
190
  if (ppVnodes != NULL) {
101!
191
    taosMemoryFree(ppVnodes);
101!
192
  }
193

194
  if (code != 0) {
101!
195
    dError("vgId:%d, failed to alloc disk since %s", vgId, tstrerror(code));
×
196
    return code;
×
197
  } else {
198
    dInfo("vgId:%d, alloc disk:%d of level 0. ndisk:%d, vnodes: %d", vgId, diskId, ndisk, numOfVnodes);
101!
199
    return diskId;
101✔
200
  }
201
}
202

203
void vmCleanPrimaryDisk(SVnodeMgmt *pMgmt, int32_t vgId) { vmUnRegisterCreatingState(pMgmt, vgId); }
101✔
204

205
SVnodeObj *vmAcquireVnodeImpl(SVnodeMgmt *pMgmt, int32_t vgId, bool strict) {
345,533✔
206
  SVnodeObj *pVnode = NULL;
345,533✔
207

208
  (void)taosThreadRwlockRdlock(&pMgmt->lock);
345,533✔
209
  int32_t r = taosHashGetDup(pMgmt->runngingHash, &vgId, sizeof(int32_t), (void *)&pVnode);
345,883✔
210
  if (pVnode == NULL || strict && (pVnode->dropped || pVnode->failed)) {
345,826!
211
    terrno = TSDB_CODE_VND_INVALID_VGROUP_ID;
1,543✔
212
    pVnode = NULL;
1,552✔
213
  } else {
214
    int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
344,283✔
215
    dTrace("vgId:%d, acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
344,278✔
216
  }
217
  (void)taosThreadRwlockUnlock(&pMgmt->lock);
345,830✔
218

219
  return pVnode;
345,854✔
220
}
221

222
SVnodeObj *vmAcquireVnode(SVnodeMgmt *pMgmt, int32_t vgId) { return vmAcquireVnodeImpl(pMgmt, vgId, true); }
345,009✔
223

224
void vmReleaseVnode(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
344,760✔
225
  if (pVnode == NULL) return;
344,760!
226

227
  //(void)taosThreadRwlockRdlock(&pMgmt->lock);
228
  int32_t refCount = atomic_sub_fetch_32(&pVnode->refCount, 1);
344,760✔
229
  dTrace("vgId:%d, release vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
344,956✔
230
  //(void)taosThreadRwlockUnlock(&pMgmt->lock);
231
}
232

233
static int32_t vmRegisterRunningState(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
556✔
234
  SVnodeObj *pOld = NULL;
556✔
235

236
  int32_t r = taosHashGetDup(pMgmt->runngingHash, &pVnode->vgId, sizeof(int32_t), (void *)&pOld);
556✔
237
  if (r != 0) {
556!
238
    dError("vgId:%d, failed to get vnode from hash", pVnode->vgId);
×
239
  }
240
  if (pOld) {
556!
241
    vmFreeVnodeObj(&pOld);
×
242
  }
243
  int32_t code = taosHashPut(pMgmt->runngingHash, &pVnode->vgId, sizeof(int32_t), &pVnode, sizeof(SVnodeObj *));
556✔
244

245
  return code;
556✔
246
}
247

248
static void vmUnRegisterRunningState(SVnodeMgmt *pMgmt, int32_t vgId) {
556✔
249
  dInfo("vgId:%d, remove from hash", vgId);
556!
250
  int32_t r = taosHashRemove(pMgmt->runngingHash, &vgId, sizeof(int32_t));
556✔
251
  if (r != 0) {
556!
252
    dError("vgId:%d, failed to remove vnode since %s", vgId, tstrerror(r));
×
253
  }
254
}
556✔
255

256
static int32_t vmRegisterClosedState(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
446✔
257
  int32_t    code = 0;
446✔
258
  SVnodeObj *pClosedVnode = taosMemoryCalloc(1, sizeof(SVnodeObj));
446!
259
  if (pClosedVnode == NULL) {
446!
260
    dError("failed to alloc vnode since %s", terrstr());
×
261
    return terrno;
×
262
  }
263
  (void)memset(pClosedVnode, 0, sizeof(SVnodeObj));
446✔
264

265
  pClosedVnode->vgId = pVnode->vgId;
446✔
266
  pClosedVnode->dropped = pVnode->dropped;
446✔
267
  pClosedVnode->vgVersion = pVnode->vgVersion;
446✔
268
  pClosedVnode->diskPrimary = pVnode->diskPrimary;
446✔
269
  pClosedVnode->toVgId = pVnode->toVgId;
446✔
270

271
  SVnodeObj *pOld = NULL;
446✔
272
  int32_t    r = taosHashGetDup(pMgmt->closedHash, &pVnode->vgId, sizeof(int32_t), (void *)&pOld);
446✔
273
  if (r != 0) {
446!
274
    dError("vgId:%d, failed to get vnode from closedHash", pVnode->vgId);
×
275
  }
276
  if (pOld) {
446!
277
    vmFreeVnodeObj(&pOld);
×
278
  }
279
  dInfo("vgId:%d, put vnode to closedHash", pVnode->vgId);
446!
280
  r = taosHashPut(pMgmt->closedHash, &pVnode->vgId, sizeof(int32_t), &pClosedVnode, sizeof(SVnodeObj *));
446✔
281
  if (r != 0) {
446!
282
    dError("vgId:%d, failed to put vnode to closedHash", pVnode->vgId);
×
283
  }
284

285
  return code;
446✔
286
}
287

288
static void vmUnRegisterClosedState(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
556✔
289
  SVnodeObj *pOld = NULL;
556✔
290
  int32_t    r = taosHashGetDup(pMgmt->closedHash, &pVnode->vgId, sizeof(int32_t), (void *)&pOld);
556✔
291
  if (r != 0) {
556!
292
    dError("vgId:%d, failed to get vnode from closedHash", pVnode->vgId);
×
293
  }
294
  if (pOld != NULL) {
556✔
295
    vmFreeVnodeObj(&pOld);
446✔
296
    dInfo("vgId:%d, remove from closedHash", pVnode->vgId);
446!
297
    r = taosHashRemove(pMgmt->closedHash, &pVnode->vgId, sizeof(int32_t));
446✔
298
    if (r != 0) {
446!
299
      dError("vgId:%d, failed to remove vnode from hash", pVnode->vgId);
×
300
    }
301
  }
302
}
556✔
303

304
int32_t vmOpenVnode(SVnodeMgmt *pMgmt, SWrapperCfg *pCfg, SVnode *pImpl) {
556✔
305
  SVnodeObj *pVnode = taosMemoryCalloc(1, sizeof(SVnodeObj));
556!
306
  if (pVnode == NULL) {
556!
307
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
308
    return -1;
×
309
  }
310

311
  pVnode->vgId = pCfg->vgId;
556✔
312
  pVnode->vgVersion = pCfg->vgVersion;
556✔
313
  pVnode->diskPrimary = pCfg->diskPrimary;
556✔
314
  pVnode->refCount = 0;
556✔
315
  pVnode->dropped = 0;
556✔
316
  pVnode->failed = 0;
556✔
317
  pVnode->path = taosStrdup(pCfg->path);
556!
318
  pVnode->pImpl = pImpl;
556✔
319

320
  if (pVnode->path == NULL) {
556!
321
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
322
    taosMemoryFree(pVnode);
×
323
    return -1;
×
324
  }
325

326
  if (pImpl) {
556!
327
    if (vmAllocQueue(pMgmt, pVnode) != 0) {
556!
328
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
329
      taosMemoryFree(pVnode->path);
×
330
      taosMemoryFree(pVnode);
×
331
      return -1;
×
332
    }
333
  } else {
334
    pVnode->failed = 1;
×
335
  }
336

337
  (void)taosThreadRwlockWrlock(&pMgmt->lock);
556✔
338
  int32_t code = vmRegisterRunningState(pMgmt, pVnode);
556✔
339
  vmUnRegisterClosedState(pMgmt, pVnode);
556✔
340
  (void)taosThreadRwlockUnlock(&pMgmt->lock);
556✔
341

342
  return code;
556✔
343
}
344

345
void vmCloseVnode(SVnodeMgmt *pMgmt, SVnodeObj *pVnode, bool commitAndRemoveWal, bool keepClosed) {
556✔
346
  char path[TSDB_FILENAME_LEN] = {0};
556✔
347
  bool atExit = true;
556✔
348

349
  if (pVnode->pImpl && vnodeIsLeader(pVnode->pImpl)) {
556!
350
    vnodeProposeCommitOnNeed(pVnode->pImpl, atExit);
180✔
351
  }
352

353
  (void)taosThreadRwlockWrlock(&pMgmt->lock);
556✔
354
  vmUnRegisterRunningState(pMgmt, pVnode->vgId);
556✔
355
  if (keepClosed) {
556✔
356
    if (vmRegisterClosedState(pMgmt, pVnode) != 0) {
446!
357
      (void)taosThreadRwlockUnlock(&pMgmt->lock);
×
358
      return;
×
359
    };
360
  }
361
  (void)taosThreadRwlockUnlock(&pMgmt->lock);
556✔
362

363
  vmReleaseVnode(pMgmt, pVnode);
556✔
364

365
  if (pVnode->failed) {
556!
366
    goto _closed;
×
367
  }
368
  dInfo("vgId:%d, pre close", pVnode->vgId);
556!
369
  vnodePreClose(pVnode->pImpl);
556✔
370

371
  dInfo("vgId:%d, wait for vnode ref become 0", pVnode->vgId);
556!
372
  while (pVnode->refCount > 0) taosMsleep(10);
556!
373

374
  dInfo("vgId:%d, wait for vnode write queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pWriteW.queue,
556!
375
        taosQueueGetThreadId(pVnode->pWriteW.queue));
376
  tMultiWorkerCleanup(&pVnode->pWriteW);
556✔
377

378
  dInfo("vgId:%d, wait for vnode sync queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pSyncW.queue,
556!
379
        taosQueueGetThreadId(pVnode->pSyncW.queue));
380
  tMultiWorkerCleanup(&pVnode->pSyncW);
556✔
381

382
  dInfo("vgId:%d, wait for vnode sync rd queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pSyncRdW.queue,
556!
383
        taosQueueGetThreadId(pVnode->pSyncRdW.queue));
384
  tMultiWorkerCleanup(&pVnode->pSyncRdW);
556✔
385

386
  dInfo("vgId:%d, wait for vnode apply queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pApplyW.queue,
556!
387
        taosQueueGetThreadId(pVnode->pApplyW.queue));
388
  tMultiWorkerCleanup(&pVnode->pApplyW);
556✔
389

390
  dInfo("vgId:%d, wait for vnode fetch queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pFetchQ,
556!
391
        taosQueueGetThreadId(pVnode->pFetchQ));
392
  while (!taosQueueEmpty(pVnode->pFetchQ)) taosMsleep(10);
556!
393

394
  dInfo("vgId:%d, wait for vnode query queue:%p is empty", pVnode->vgId, pVnode->pQueryQ);
556!
395
  while (!taosQueueEmpty(pVnode->pQueryQ)) taosMsleep(10);
556!
396

397
  tqNotifyClose(pVnode->pImpl->pTq);
556✔
398
  dInfo("vgId:%d, wait for vnode stream queue:%p is empty", pVnode->vgId, pVnode->pStreamQ);
556!
399
  while (!taosQueueEmpty(pVnode->pStreamQ)) taosMsleep(10);
556!
400

401
  dInfo("vgId:%d, all vnode queues is empty", pVnode->vgId);
556!
402

403
  dInfo("vgId:%d, post close", pVnode->vgId);
556!
404
  vnodePostClose(pVnode->pImpl);
556✔
405

406
  vmFreeQueue(pMgmt, pVnode);
556✔
407

408
  if (commitAndRemoveWal) {
556!
UNCOV
409
    dInfo("vgId:%d, commit data for vnode split", pVnode->vgId);
×
UNCOV
410
    if (vnodeSyncCommit(pVnode->pImpl) != 0) {
×
411
      dError("vgId:%d, failed to commit data", pVnode->vgId);
×
412
    }
UNCOV
413
    if (vnodeBegin(pVnode->pImpl) != 0) {
×
414
      dError("vgId:%d, failed to begin", pVnode->vgId);
×
415
    }
UNCOV
416
    dInfo("vgId:%d, commit data finished", pVnode->vgId);
×
417
  }
418

419
  int32_t nodeId = vnodeNodeId(pVnode->pImpl);
556✔
420
  vnodeClose(pVnode->pImpl);
556✔
421
  pVnode->pImpl = NULL;
556✔
422

423
_closed:
556✔
424
  dInfo("vgId:%d, vnode is closed", pVnode->vgId);
556!
425

426
  if (commitAndRemoveWal) {
556!
UNCOV
427
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d%swal", TD_DIRSEP, pVnode->vgId, TD_DIRSEP);
×
UNCOV
428
    dInfo("vgId:%d, remove all wals, path:%s", pVnode->vgId, path);
×
UNCOV
429
    if (tfsRmdir(pMgmt->pTfs, path) != 0) {
×
430
      dTrace("vgId:%d, failed to remove wals, path:%s", pVnode->vgId, path);
×
431
    }
UNCOV
432
    if (tfsMkdir(pMgmt->pTfs, path) != 0) {
×
433
      dTrace("vgId:%d, failed to create wals, path:%s", pVnode->vgId, path);
×
434
    }
435
  }
436

437
  if (pVnode->dropped) {
556✔
438
    dInfo("vgId:%d, vnode is destroyed, dropped:%d", pVnode->vgId, pVnode->dropped);
63!
439
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pVnode->vgId);
63✔
440
    vnodeDestroy(pVnode->vgId, path, pMgmt->pTfs, nodeId);
63✔
441
  }
442

443
  vmFreeVnodeObj(&pVnode);
556✔
444
}
445

446
void vmCloseFailedVnode(SVnodeMgmt *pMgmt, int32_t vgId) {
×
447
  int32_t r = 0;
×
448
  r = taosThreadRwlockWrlock(&pMgmt->lock);
×
449
  if (r != 0) {
×
450
    dError("vgId:%d, failed to lock since %s", vgId, tstrerror(r));
×
451
  }
452
  if (r == 0) {
×
453
    vmUnRegisterRunningState(pMgmt, vgId);
×
454
  }
455
  r = taosThreadRwlockUnlock(&pMgmt->lock);
×
456
  if (r != 0) {
×
457
    dError("vgId:%d, failed to unlock since %s", vgId, tstrerror(r));
×
458
  }
459
}
×
460

461
static int32_t vmRestoreVgroupId(SWrapperCfg *pCfg, STfs *pTfs) {
×
462
  int32_t srcVgId = pCfg->vgId;
×
463
  int32_t dstVgId = pCfg->toVgId;
×
464
  if (dstVgId == 0) return 0;
×
465

466
  char srcPath[TSDB_FILENAME_LEN];
467
  char dstPath[TSDB_FILENAME_LEN];
468

469
  snprintf(srcPath, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, srcVgId);
×
470
  snprintf(dstPath, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, dstVgId);
×
471

472
  int32_t diskPrimary = pCfg->diskPrimary;
×
473
  int32_t vgId = vnodeRestoreVgroupId(srcPath, dstPath, srcVgId, dstVgId, diskPrimary, pTfs);
×
474
  if (vgId <= 0) {
×
475
    dError("vgId:%d, failed to restore vgroup id. srcPath: %s", pCfg->vgId, srcPath);
×
476
    return -1;
×
477
  }
478

479
  pCfg->vgId = vgId;
×
480
  pCfg->toVgId = 0;
×
481
  return 0;
×
482
}
483

484
static void *vmOpenVnodeInThread(void *param) {
9✔
485
  SVnodeThread *pThread = param;
9✔
486
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
9✔
487
  char          path[TSDB_FILENAME_LEN];
488

489
  dInfo("thread:%d, start to open or destroy %d vnodes", pThread->threadIndex, pThread->vnodeNum);
9!
490
  setThreadName("open-vnodes");
9✔
491

492
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
18✔
493
    SWrapperCfg *pCfg = &pThread->pCfgs[v];
9✔
494
    if (pCfg->dropped) {
9!
495
      char stepDesc[TSDB_STEP_DESC_LEN] = {0};
×
496
      snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to destroy, %d of %d have been dropped", pCfg->vgId,
×
497
               pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
498
      tmsgReportStartup("vnode-destroy", stepDesc);
×
499

500
      snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pCfg->vgId);
×
501
      vnodeDestroy(pCfg->vgId, path, pMgmt->pTfs, 0);
×
502
      pThread->updateVnodesList = true;
×
503
      pThread->dropped++;
×
504
      (void)atomic_add_fetch_32(&pMgmt->state.dropVnodes, 1);
×
505
      continue;
×
506
    }
507

508
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
9✔
509
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been opened", pCfg->vgId,
9✔
510
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
511
    tmsgReportStartup("vnode-open", stepDesc);
9✔
512

513
    if (pCfg->toVgId) {
9!
514
      if (vmRestoreVgroupId(pCfg, pMgmt->pTfs) != 0) {
×
515
        dError("vgId:%d, failed to restore vgroup id by thread:%d", pCfg->vgId, pThread->threadIndex);
×
516
        pThread->failed++;
×
517
        continue;
×
518
      }
519
      pThread->updateVnodesList = true;
×
520
    }
521

522
    int32_t diskPrimary = pCfg->diskPrimary;
9✔
523
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pCfg->vgId);
9✔
524

525
    SVnode *pImpl = vnodeOpen(path, diskPrimary, pMgmt->pTfs, pMgmt->msgCb, false);
9✔
526

527
    if (pImpl == NULL) {
9!
528
      dError("vgId:%d, failed to open vnode by thread:%d since %s", pCfg->vgId, pThread->threadIndex, terrstr());
×
529
      if (terrno != TSDB_CODE_NEED_RETRY) {
×
530
        pThread->failed++;
×
531
        continue;
×
532
      }
533
    }
534

535
    if (vmOpenVnode(pMgmt, pCfg, pImpl) != 0) {
9!
536
      dError("vgId:%d, failed to open vnode by thread:%d", pCfg->vgId, pThread->threadIndex);
×
537
      pThread->failed++;
×
538
      continue;
×
539
    }
540

541
    dInfo("vgId:%d, is opened by thread:%d", pCfg->vgId, pThread->threadIndex);
9!
542
    pThread->opened++;
9✔
543
    (void)atomic_add_fetch_32(&pMgmt->state.openVnodes, 1);
9✔
544
  }
545

546
  dInfo("thread:%d, numOfVnodes:%d, opened:%d dropped:%d failed:%d", pThread->threadIndex, pThread->vnodeNum,
9!
547
        pThread->opened, pThread->dropped, pThread->failed);
548
  return NULL;
9✔
549
}
550

551
static int32_t vmOpenVnodes(SVnodeMgmt *pMgmt) {
40✔
552
  pMgmt->runngingHash =
40✔
553
      taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
40✔
554
  if (pMgmt->runngingHash == NULL) {
40!
555
    dError("failed to init vnode hash since %s", terrstr());
×
556
    return TSDB_CODE_OUT_OF_MEMORY;
×
557
  }
558

559
  pMgmt->closedHash =
40✔
560
      taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
40✔
561
  if (pMgmt->closedHash == NULL) {
40!
562
    dError("failed to init vnode closed hash since %s", terrstr());
×
563
    return TSDB_CODE_OUT_OF_MEMORY;
×
564
  }
565

566
  pMgmt->creatingHash =
40✔
567
      taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
40✔
568
  if (pMgmt->creatingHash == NULL) {
40!
569
    dError("failed to init vnode creatingHash hash since %s", terrstr());
×
570
    return TSDB_CODE_OUT_OF_MEMORY;
×
571
  }
572

573
  SWrapperCfg *pCfgs = NULL;
40✔
574
  int32_t      numOfVnodes = 0;
40✔
575
  if (vmGetVnodeListFromFile(pMgmt, &pCfgs, &numOfVnodes) != 0) {
40!
576
    dInfo("failed to get vnode list from disk since %s", terrstr());
×
577
    return -1;
×
578
  }
579

580
  pMgmt->state.totalVnodes = numOfVnodes;
40✔
581

582
  int32_t threadNum = tsNumOfCores / 2;
40✔
583
  if (threadNum < 1) threadNum = 1;
40!
584
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
40✔
585

586
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
40!
587
  if (threads == NULL) {
40!
588
    dError("failed to allocate memory for threads since %s", terrstr());
×
589
    taosMemoryFree(pCfgs);
×
590
    return terrno;
×
591
  }
592

593
  for (int32_t t = 0; t < threadNum; ++t) {
840✔
594
    threads[t].threadIndex = t;
800✔
595
    threads[t].pMgmt = pMgmt;
800✔
596
    threads[t].pCfgs = taosMemoryCalloc(vnodesPerThread, sizeof(SWrapperCfg));
800!
597
  }
598

599
  for (int32_t v = 0; v < numOfVnodes; ++v) {
49✔
600
    int32_t       t = v % threadNum;
9✔
601
    SVnodeThread *pThread = &threads[t];
9✔
602
    pThread->pCfgs[pThread->vnodeNum++] = pCfgs[v];
9✔
603
  }
604

605
  dInfo("open %d vnodes with %d threads", numOfVnodes, threadNum);
40!
606

607
  for (int32_t t = 0; t < threadNum; ++t) {
840✔
608
    SVnodeThread *pThread = &threads[t];
800✔
609
    if (pThread->vnodeNum == 0) continue;
800✔
610

611
    TdThreadAttr thAttr;
612
    (void)taosThreadAttrInit(&thAttr);
9✔
613
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
9✔
614
    if (taosThreadCreate(&pThread->thread, &thAttr, vmOpenVnodeInThread, pThread) != 0) {
9!
615
      dError("thread:%d, failed to create thread to open vnode, reason:%s", pThread->threadIndex, strerror(errno));
×
616
    }
617

618
    (void)taosThreadAttrDestroy(&thAttr);
9✔
619
  }
620

621
  bool updateVnodesList = false;
40✔
622

623
  for (int32_t t = 0; t < threadNum; ++t) {
840✔
624
    SVnodeThread *pThread = &threads[t];
800✔
625
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
800!
626
      (void)taosThreadJoin(pThread->thread, NULL);
9✔
627
      taosThreadClear(&pThread->thread);
9✔
628
    }
629
    taosMemoryFree(pThread->pCfgs);
800!
630
    if (pThread->updateVnodesList) updateVnodesList = true;
800!
631
  }
632
  taosMemoryFree(threads);
40!
633
  taosMemoryFree(pCfgs);
40!
634

635
  if ((pMgmt->state.openVnodes + pMgmt->state.dropVnodes) != pMgmt->state.totalVnodes) {
40!
636
    dError("there are total vnodes:%d, opened:%d", pMgmt->state.totalVnodes, pMgmt->state.openVnodes);
×
637
    terrno = TSDB_CODE_VND_INIT_FAILED;
×
638
    return -1;
×
639
  }
640

641
  if (updateVnodesList && vmWriteVnodeListToFile(pMgmt) != 0) {
40!
642
    dError("failed to write vnode list since %s", terrstr());
×
643
    return -1;
×
644
  }
645

646
  dInfo("successfully opened %d vnodes", pMgmt->state.totalVnodes);
40!
647
  return 0;
40✔
648
}
649

650
static void *vmCloseVnodeInThread(void *param) {
47✔
651
  SVnodeThread *pThread = param;
47✔
652
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
47✔
653

654
  dInfo("thread:%d, start to close %d vnodes", pThread->threadIndex, pThread->vnodeNum);
47!
655
  setThreadName("close-vnodes");
47✔
656

657
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
94✔
658
    SVnodeObj *pVnode = pThread->ppVnodes[v];
47✔
659

660
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
47✔
661
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to close, %d of %d have been closed", pVnode->vgId,
47✔
662
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
663
    tmsgReportStartup("vnode-close", stepDesc);
47✔
664

665
    vmCloseVnode(pMgmt, pVnode, false, false);
47✔
666
  }
667

668
  dInfo("thread:%d, numOfVnodes:%d is closed", pThread->threadIndex, pThread->vnodeNum);
47!
669
  return NULL;
47✔
670
}
671

672
static void vmCloseVnodes(SVnodeMgmt *pMgmt) {
40✔
673
  int32_t code = 0;
40✔
674
  dInfo("start to close all vnodes");
40!
675
  tSingleWorkerCleanup(&pMgmt->mgmtWorker);
40✔
676
  dInfo("vnodes mgmt worker is stopped");
40!
677
  tSingleWorkerCleanup(&pMgmt->mgmtMultiWorker);
40✔
678
  dInfo("vnodes multiple mgmt worker is stopped");
40!
679

680
  int32_t     numOfVnodes = 0;
40✔
681
  SVnodeObj **ppVnodes = NULL;
40✔
682
  code = vmGetVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes);
40✔
683
  if (code != 0) {
40!
684
    dError("failed to get vnode list since %s", tstrerror(code));
×
685
    return;
×
686
  }
687

688
  int32_t threadNum = tsNumOfCores / 2;
40✔
689
  if (threadNum < 1) threadNum = 1;
40!
690
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
40✔
691

692
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
40!
693
  for (int32_t t = 0; t < threadNum; ++t) {
840✔
694
    threads[t].threadIndex = t;
800✔
695
    threads[t].pMgmt = pMgmt;
800✔
696
    threads[t].ppVnodes = taosMemoryCalloc(vnodesPerThread, sizeof(SVnode *));
800!
697
  }
698

699
  for (int32_t v = 0; v < numOfVnodes; ++v) {
87✔
700
    int32_t       t = v % threadNum;
47✔
701
    SVnodeThread *pThread = &threads[t];
47✔
702
    if (pThread->ppVnodes != NULL && ppVnodes != NULL) {
47!
703
      pThread->ppVnodes[pThread->vnodeNum++] = ppVnodes[v];
47✔
704
    }
705
  }
706

707
  pMgmt->state.openVnodes = 0;
40✔
708
  dInfo("close %d vnodes with %d threads", numOfVnodes, threadNum);
40!
709

710
  for (int32_t t = 0; t < threadNum; ++t) {
840✔
711
    SVnodeThread *pThread = &threads[t];
800✔
712
    if (pThread->vnodeNum == 0) continue;
800✔
713

714
    TdThreadAttr thAttr;
715
    (void)taosThreadAttrInit(&thAttr);
47✔
716
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
47✔
717
    if (taosThreadCreate(&pThread->thread, &thAttr, vmCloseVnodeInThread, pThread) != 0) {
47!
718
      dError("thread:%d, failed to create thread to close vnode since %s", pThread->threadIndex, strerror(errno));
×
719
    }
720

721
    (void)taosThreadAttrDestroy(&thAttr);
47✔
722
  }
723

724
  for (int32_t t = 0; t < threadNum; ++t) {
840✔
725
    SVnodeThread *pThread = &threads[t];
800✔
726
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
800!
727
      (void)taosThreadJoin(pThread->thread, NULL);
47✔
728
      taosThreadClear(&pThread->thread);
47✔
729
    }
730
    taosMemoryFree(pThread->ppVnodes);
800!
731
  }
732
  taosMemoryFree(threads);
40!
733

734
  if (ppVnodes != NULL) {
40!
735
    taosMemoryFree(ppVnodes);
40!
736
  }
737

738
  if (pMgmt->runngingHash != NULL) {
40!
739
    taosHashCleanup(pMgmt->runngingHash);
40✔
740
    pMgmt->runngingHash = NULL;
40✔
741
  }
742

743
  void *pIter = taosHashIterate(pMgmt->closedHash, NULL);
40✔
744
  while (pIter) {
40!
745
    SVnodeObj **ppVnode = pIter;
×
746
    vmFreeVnodeObj(ppVnode);
×
747
    pIter = taosHashIterate(pMgmt->closedHash, pIter);
×
748
  }
749

750
  if (pMgmt->closedHash != NULL) {
40!
751
    taosHashCleanup(pMgmt->closedHash);
40✔
752
    pMgmt->closedHash = NULL;
40✔
753
  }
754

755
  pIter = taosHashIterate(pMgmt->creatingHash, NULL);
40✔
756
  while (pIter) {
40!
757
    SVnodeObj **ppVnode = pIter;
×
758
    vmFreeVnodeObj(ppVnode);
×
759
    pIter = taosHashIterate(pMgmt->creatingHash, pIter);
×
760
  }
761

762
  if (pMgmt->creatingHash != NULL) {
40!
763
    taosHashCleanup(pMgmt->creatingHash);
40✔
764
    pMgmt->creatingHash = NULL;
40✔
765
  }
766

767
  dInfo("total vnodes:%d are all closed", numOfVnodes);
40!
768
}
769

770
static void vmCleanup(SVnodeMgmt *pMgmt) {
40✔
771
  vmCloseVnodes(pMgmt);
40✔
772
  vmStopWorker(pMgmt);
40✔
773
  vnodeCleanup();
40✔
774
  (void)taosThreadRwlockDestroy(&pMgmt->lock);
40✔
775
  (void)taosThreadMutexDestroy(&pMgmt->mutex);
40✔
776
  (void)taosThreadMutexDestroy(&pMgmt->fileLock);
40✔
777
  taosMemoryFree(pMgmt);
40!
778
}
40✔
779

780
static void vmCheckSyncTimeout(SVnodeMgmt *pMgmt) {
198✔
781
  int32_t     code = 0;
198✔
782
  int32_t     numOfVnodes = 0;
198✔
783
  SVnodeObj **ppVnodes = NULL;
198✔
784
  code = vmGetVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes);
198✔
785
  if (code != 0) {
198!
786
    dError("failed to get vnode list since %s", tstrerror(code));
×
787
    return;
×
788
  }
789

790
  if (ppVnodes != NULL) {
198!
791
    for (int32_t i = 0; i < numOfVnodes; ++i) {
411✔
792
      SVnodeObj *pVnode = ppVnodes[i];
213✔
793
      if (!pVnode->failed) {
213!
794
        vnodeSyncCheckTimeout(pVnode->pImpl);
213✔
795
      }
796
      vmReleaseVnode(pMgmt, pVnode);
213✔
797
    }
798
    taosMemoryFree(ppVnodes);
198!
799
  }
800
}
801

802
static void *vmThreadFp(void *param) {
40✔
803
  SVnodeMgmt *pMgmt = param;
40✔
804
  int64_t     lastTime = 0;
40✔
805
  setThreadName("vnode-timer");
40✔
806

807
  while (1) {
64,004✔
808
    lastTime++;
64,044✔
809
    taosMsleep(100);
64,044✔
810
    if (pMgmt->stop) break;
64,044✔
811
    if (lastTime % 10 != 0) continue;
64,004✔
812

813
    int64_t sec = lastTime / 10;
6,383✔
814
    if (sec % (VNODE_TIMEOUT_SEC / 2) == 0) {
6,383✔
815
      vmCheckSyncTimeout(pMgmt);
198✔
816
    }
817
  }
818

819
  return NULL;
40✔
820
}
821

822
static int32_t vmInitTimer(SVnodeMgmt *pMgmt) {
40✔
823
  int32_t      code = 0;
40✔
824
  TdThreadAttr thAttr;
825
  (void)taosThreadAttrInit(&thAttr);
40✔
826
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
40✔
827
  if (taosThreadCreate(&pMgmt->thread, &thAttr, vmThreadFp, pMgmt) != 0) {
40!
828
    code = TAOS_SYSTEM_ERROR(errno);
×
829
    dError("failed to create vnode timer thread since %s", tstrerror(code));
×
830
    return code;
×
831
  }
832

833
  (void)taosThreadAttrDestroy(&thAttr);
40✔
834
  return 0;
40✔
835
}
836

837
static void vmCleanupTimer(SVnodeMgmt *pMgmt) {
40✔
838
  pMgmt->stop = true;
40✔
839
  if (taosCheckPthreadValid(pMgmt->thread)) {
40!
840
    (void)taosThreadJoin(pMgmt->thread, NULL);
40✔
841
    taosThreadClear(&pMgmt->thread);
40✔
842
  }
843
}
40✔
844

845
static int32_t vmInit(SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) {
40✔
846
  int32_t code = -1;
40✔
847

848
  SVnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SVnodeMgmt));
40!
849
  if (pMgmt == NULL) {
40!
850
    code = terrno;
×
851
    goto _OVER;
×
852
  }
853

854
  pMgmt->pData = pInput->pData;
40✔
855
  pMgmt->path = pInput->path;
40✔
856
  pMgmt->name = pInput->name;
40✔
857
  pMgmt->msgCb = pInput->msgCb;
40✔
858
  pMgmt->msgCb.putToQueueFp = (PutToQueueFp)vmPutRpcMsgToQueue;
40✔
859
  pMgmt->msgCb.qsizeFp = (GetQueueSizeFp)vmGetQueueSize;
40✔
860
  pMgmt->msgCb.mgmt = pMgmt;
40✔
861

862
  code = taosThreadRwlockInit(&pMgmt->lock, NULL);
40✔
863
  if (code != 0) {
40!
864
    code = TAOS_SYSTEM_ERROR(errno);
×
865
    goto _OVER;
×
866
  }
867

868
  code = taosThreadMutexInit(&pMgmt->mutex, NULL);
40✔
869
  if (code != 0) {
40!
870
    code = TAOS_SYSTEM_ERROR(errno);
×
871
    goto _OVER;
×
872
  }
873

874
  code = taosThreadMutexInit(&pMgmt->fileLock, NULL);
40✔
875
  if (code != 0) {
40!
876
    code = TAOS_SYSTEM_ERROR(errno);
×
877
    goto _OVER;
×
878
  }
879

880
  pMgmt->pTfs = pInput->pTfs;
40✔
881
  if (pMgmt->pTfs == NULL) {
40!
882
    dError("tfs is null.");
×
883
    goto _OVER;
×
884
  }
885
  tmsgReportStartup("vnode-tfs", "initialized");
40✔
886
  if ((code = walInit(pInput->stopDnodeFp)) != 0) {
40!
887
    dError("failed to init wal since %s", tstrerror(code));
×
888
    goto _OVER;
×
889
  }
890

891
  tmsgReportStartup("vnode-wal", "initialized");
40✔
892

893
  if ((code = syncInit()) != 0) {
40!
894
    dError("failed to open sync since %s", tstrerror(code));
×
895
    goto _OVER;
×
896
  }
897
  tmsgReportStartup("vnode-sync", "initialized");
40✔
898

899
  if ((code = vnodeInit(pInput->stopDnodeFp)) != 0) {
40!
900
    dError("failed to init vnode since %s", tstrerror(code));
×
901
    goto _OVER;
×
902
  }
903
  tmsgReportStartup("vnode-commit", "initialized");
40✔
904

905
  if ((code = vmStartWorker(pMgmt)) != 0) {
40!
906
    dError("failed to init workers since %s", tstrerror(code));
×
907
    goto _OVER;
×
908
  }
909
  tmsgReportStartup("vnode-worker", "initialized");
40✔
910

911
  if ((code = vmOpenVnodes(pMgmt)) != 0) {
40!
912
    dError("failed to open all vnodes since %s", tstrerror(code));
×
913
    goto _OVER;
×
914
  }
915
  tmsgReportStartup("vnode-vnodes", "initialized");
40✔
916

917
  if ((code = udfcOpen()) != 0) {
40!
918
    dError("failed to open udfc in vnode since %s", tstrerror(code));
×
919
    goto _OVER;
×
920
  }
921

922
  code = 0;
40✔
923

924
_OVER:
40✔
925
  if (code == 0) {
40!
926
    pOutput->pMgmt = pMgmt;
40✔
927
  } else {
928
    dError("failed to init vnodes-mgmt since %s", tstrerror(code));
×
929
    vmCleanup(pMgmt);
×
930
  }
931

932
  return code;
40✔
933
}
934

935
static int32_t vmRequire(const SMgmtInputOpt *pInput, bool *required) {
43✔
936
  *required = tsNumOfSupportVnodes > 0;
43✔
937
  return 0;
43✔
938
}
939

940
static void *vmRestoreVnodeInThread(void *param) {
9✔
941
  SVnodeThread *pThread = param;
9✔
942
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
9✔
943

944
  dInfo("thread:%d, start to restore %d vnodes", pThread->threadIndex, pThread->vnodeNum);
9!
945
  setThreadName("restore-vnodes");
9✔
946

947
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
18✔
948
    SVnodeObj *pVnode = pThread->ppVnodes[v];
9✔
949
    if (pVnode->failed) {
9!
950
      dError("vgId:%d, cannot restore a vnode in failed mode.", pVnode->vgId);
×
951
      continue;
×
952
    }
953

954
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
9✔
955
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been restored", pVnode->vgId,
9✔
956
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
957
    tmsgReportStartup("vnode-restore", stepDesc);
9✔
958

959
    int32_t code = vnodeStart(pVnode->pImpl);
9✔
960
    if (code != 0) {
9!
961
      dError("vgId:%d, failed to restore vnode by thread:%d", pVnode->vgId, pThread->threadIndex);
×
962
      pThread->failed++;
×
963
    } else {
964
      dInfo("vgId:%d, is restored by thread:%d", pVnode->vgId, pThread->threadIndex);
9!
965
      pThread->opened++;
9✔
966
      (void)atomic_add_fetch_32(&pMgmt->state.openVnodes, 1);
9✔
967
    }
968
  }
969

970
  dInfo("thread:%d, numOfVnodes:%d, restored:%d failed:%d", pThread->threadIndex, pThread->vnodeNum, pThread->opened,
9!
971
        pThread->failed);
972
  return NULL;
9✔
973
}
974

975
static int32_t vmStartVnodes(SVnodeMgmt *pMgmt) {
40✔
976
  int32_t     code = 0;
40✔
977
  int32_t     numOfVnodes = 0;
40✔
978
  SVnodeObj **ppVnodes = NULL;
40✔
979
  code = vmGetVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes);
40✔
980
  if (code != 0) {
40!
981
    dError("failed to get vnode list since %s", tstrerror(code));
×
982
    return code;
×
983
  }
984

985
  int32_t threadNum = tsNumOfCores / 2;
40✔
986
  if (threadNum < 1) threadNum = 1;
40!
987
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
40✔
988

989
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
40!
990
  if (threads == NULL) {
40!
991
    return terrno;
×
992
  }
993

994
  for (int32_t t = 0; t < threadNum; ++t) {
840✔
995
    threads[t].threadIndex = t;
800✔
996
    threads[t].pMgmt = pMgmt;
800✔
997
    threads[t].ppVnodes = taosMemoryCalloc(vnodesPerThread, sizeof(SVnode *));
800!
998
    if (threads[t].ppVnodes == NULL) {
800!
999
      code = terrno;
×
1000
      break;
×
1001
    }
1002
  }
1003

1004
  for (int32_t v = 0; v < numOfVnodes; ++v) {
49✔
1005
    int32_t       t = v % threadNum;
9✔
1006
    SVnodeThread *pThread = &threads[t];
9✔
1007
    if (pThread->ppVnodes != NULL && ppVnodes != NULL) {
9!
1008
      pThread->ppVnodes[pThread->vnodeNum++] = ppVnodes[v];
9✔
1009
    }
1010
  }
1011

1012
  pMgmt->state.openVnodes = 0;
40✔
1013
  pMgmt->state.dropVnodes = 0;
40✔
1014
  dInfo("restore %d vnodes with %d threads", numOfVnodes, threadNum);
40!
1015

1016
  for (int32_t t = 0; t < threadNum; ++t) {
840✔
1017
    SVnodeThread *pThread = &threads[t];
800✔
1018
    if (pThread->vnodeNum == 0) continue;
800✔
1019

1020
    TdThreadAttr thAttr;
1021
    (void)taosThreadAttrInit(&thAttr);
9✔
1022
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
9✔
1023
    if (taosThreadCreate(&pThread->thread, &thAttr, vmRestoreVnodeInThread, pThread) != 0) {
9!
1024
      dError("thread:%d, failed to create thread to restore vnode since %s", pThread->threadIndex, strerror(errno));
×
1025
    }
1026

1027
    (void)taosThreadAttrDestroy(&thAttr);
9✔
1028
  }
1029

1030
  for (int32_t t = 0; t < threadNum; ++t) {
840✔
1031
    SVnodeThread *pThread = &threads[t];
800✔
1032
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
800!
1033
      (void)taosThreadJoin(pThread->thread, NULL);
9✔
1034
      taosThreadClear(&pThread->thread);
9✔
1035
    }
1036
    taosMemoryFree(pThread->ppVnodes);
800!
1037
  }
1038
  taosMemoryFree(threads);
40!
1039

1040
  for (int32_t i = 0; i < numOfVnodes; ++i) {
49✔
1041
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
9!
1042
    vmReleaseVnode(pMgmt, ppVnodes[i]);
9✔
1043
  }
1044

1045
  if (ppVnodes != NULL) {
40!
1046
    taosMemoryFree(ppVnodes);
40!
1047
  }
1048

1049
  return vmInitTimer(pMgmt);
40✔
1050

1051
_exit:
1052
  for (int32_t t = 0; t < threadNum; ++t) {
1053
    SVnodeThread *pThread = &threads[t];
1054
    taosMemoryFree(pThread->ppVnodes);
1055
  }
1056
  taosMemoryFree(threads);
1057
  return code;
1058
}
1059

1060
static void vmStop(SVnodeMgmt *pMgmt) { vmCleanupTimer(pMgmt); }
40✔
1061

1062
SMgmtFunc vmGetMgmtFunc() {
43✔
1063
  SMgmtFunc mgmtFunc = {0};
43✔
1064
  mgmtFunc.openFp = vmInit;
43✔
1065
  mgmtFunc.closeFp = (NodeCloseFp)vmCleanup;
43✔
1066
  mgmtFunc.startFp = (NodeStartFp)vmStartVnodes;
43✔
1067
  mgmtFunc.stopFp = (NodeStopFp)vmStop;
43✔
1068
  mgmtFunc.requiredFp = vmRequire;
43✔
1069
  mgmtFunc.getHandlesFp = vmGetMsgHandles;
43✔
1070

1071
  return mgmtFunc;
43✔
1072
}
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