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

taosdata / TDengine / #4308

14 Jun 2025 02:06PM UTC coverage: 62.454% (-0.3%) from 62.777%
#4308

push

travis-ci

web-flow
fix: taosdump windows pthread_mutex_unlock crash(3.0) (#31357)

* fix: windows pthread_mutex_unlock crash

* enh: sync from main fix taosdump crash windows

* fix: restore .github action branch to main

153985 of 315105 branches covered (48.87%)

Branch coverage included in aggregate %.

238120 of 312727 relevant lines covered (76.14%)

6462519.65 hits per line

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

61.9
/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) {
10,816✔
24
  int32_t    diskId = -1;
10,816✔
25
  SVnodeObj *pVnode = NULL;
10,816✔
26

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

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

39
  SVnodeObj *pVnode = *ppVnode;
26,479✔
40

41
  int32_t refCount = atomic_load_32(&pVnode->refCount);
26,479✔
42
  while (refCount > 0) {
26,479!
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);
26,479!
49
  taosMemoryFree(pVnode);
26,479!
50
  ppVnode[0] = NULL;
26,478✔
51
}
52

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

62
  pCreatingVnode->vgId = vgId;
10,817✔
63
  pCreatingVnode->diskPrimary = diskId;
10,817✔
64

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

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

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

83
  return code;
10,817✔
84
}
85

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

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

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

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

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

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

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

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

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

147
  code = vmGetAllVnodeListFromHashWithCreating(pMgmt, &numOfVnodes, &ppVnodes);
10,817✔
148
  if (code != 0) {
10,817!
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++) {
40,982✔
157
    SVnodeObj *pVnode = ppVnodes[v];
30,165✔
158
    disks[pVnode->diskPrimary] += 1;
30,165✔
159
  }
160

161
  int32_t minVal = INT_MAX;
10,817✔
162
  int32_t ndisk = tfsGetDisksAtLevel(pTfs, 0);
10,817✔
163
  diskId = 0;
10,817✔
164
  for (int32_t id = 0; id < ndisk; id++) {
21,909✔
165
    if (minVal > disks[id]) {
11,092✔
166
      minVal = disks[id];
10,873✔
167
      diskId = id;
10,873✔
168
    }
169
  }
170
  code = vmRegisterCreatingState(pMgmt, vgId, diskId);
10,817✔
171
  if (code != 0) {
10,817!
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);
10,817✔
180
  if (code != 0) {
10,817!
181
    goto _OVER;
×
182
  }
183

184
_OVER:
10,817✔
185

186
  for (int32_t i = 0; i < numOfVnodes; ++i) {
40,982✔
187
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
30,165!
188
    vmReleaseVnode(pMgmt, ppVnodes[i]);
30,165✔
189
  }
190
  if (ppVnodes != NULL) {
10,817!
191
    taosMemoryFree(ppVnodes);
10,817!
192
  }
193

194
  if (code != 0) {
10,817!
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);
10,817!
199
    return diskId;
10,817✔
200
  }
201
}
202

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

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

208
  (void)taosThreadRwlockRdlock(&pMgmt->hashLock);
11,812,047✔
209
  int32_t r = taosHashGetDup(pMgmt->runngingHash, &vgId, sizeof(int32_t), (void *)&pVnode);
11,826,264✔
210
  if (pVnode == NULL || strict && (pVnode->dropped || pVnode->failed)) {
11,824,868!
211
    terrno = TSDB_CODE_VND_INVALID_VGROUP_ID;
49,284✔
212
    pVnode = NULL;
48,980✔
213
  } else {
214
    int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
11,775,584✔
215
    dTrace("vgId:%d, acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
11,775,638✔
216
  }
217
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
11,824,619✔
218

219
  return pVnode;
11,826,197✔
220
}
221

222
SVnodeObj *vmAcquireVnode(SVnodeMgmt *pMgmt, int32_t vgId) { return vmAcquireVnodeImpl(pMgmt, vgId, true); }
11,796,986✔
223

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

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

233
static int32_t vmRegisterRunningState(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
13,893✔
234
  SVnodeObj *pOld = NULL;
13,893✔
235
  dInfo("vgId:%d, put vnode into running hash", pVnode->vgId);
13,893!
236

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

246
  return code;
13,893✔
247
}
248

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

257
static int32_t vmRegisterClosedState(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
1,769✔
258
  int32_t    code = 0;
1,769✔
259
  dInfo("vgId:%d, put into closed hash", pVnode->vgId);
1,769!
260
  SVnodeObj *pClosedVnode = taosMemoryCalloc(1, sizeof(SVnodeObj));
1,769!
261
  if (pClosedVnode == NULL) {
1,769!
262
    dError("failed to alloc vnode since %s", terrstr());
×
263
    return terrno;
×
264
  }
265
  (void)memset(pClosedVnode, 0, sizeof(SVnodeObj));
1,769✔
266

267
  pClosedVnode->vgId = pVnode->vgId;
1,769✔
268
  pClosedVnode->dropped = pVnode->dropped;
1,769✔
269
  pClosedVnode->vgVersion = pVnode->vgVersion;
1,769✔
270
  pClosedVnode->diskPrimary = pVnode->diskPrimary;
1,769✔
271
  pClosedVnode->toVgId = pVnode->toVgId;
1,769✔
272

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

287
  return code;
1,769✔
288
}
289

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

307
int32_t vmOpenVnode(SVnodeMgmt *pMgmt, SWrapperCfg *pCfg, SVnode *pImpl) {
13,893✔
308
  SVnodeObj *pVnode = taosMemoryCalloc(1, sizeof(SVnodeObj));
13,893!
309
  if (pVnode == NULL) {
13,893!
310
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
311
    return -1;
×
312
  }
313

314
  pVnode->vgId = pCfg->vgId;
13,893✔
315
  pVnode->vgVersion = pCfg->vgVersion;
13,893✔
316
  pVnode->diskPrimary = pCfg->diskPrimary;
13,893✔
317
  pVnode->refCount = 0;
13,893✔
318
  pVnode->dropped = 0;
13,893✔
319
  pVnode->failed = 0;
13,893✔
320
  pVnode->path = taosStrdup(pCfg->path);
13,893!
321
  pVnode->pImpl = pImpl;
13,893✔
322

323
  if (pVnode->path == NULL) {
13,893!
324
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
325
    taosMemoryFree(pVnode);
×
326
    return -1;
×
327
  }
328

329
  if (pImpl) {
13,893!
330
    if (vmAllocQueue(pMgmt, pVnode) != 0) {
13,893!
331
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
332
      taosMemoryFree(pVnode->path);
×
333
      taosMemoryFree(pVnode);
×
334
      return -1;
×
335
    }
336
  } else {
337
    pVnode->failed = 1;
×
338
  }
339

340
  (void)taosThreadRwlockWrlock(&pMgmt->hashLock);
13,893✔
341
  int32_t code = vmRegisterRunningState(pMgmt, pVnode);
13,893✔
342
  vmUnRegisterClosedState(pMgmt, pVnode);
13,893✔
343
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
13,893✔
344

345
  return code;
13,893✔
346
}
347

348
void vmCloseVnode(SVnodeMgmt *pMgmt, SVnodeObj *pVnode, bool commitAndRemoveWal, bool keepClosed) {
13,893✔
349
  char path[TSDB_FILENAME_LEN] = {0};
13,893✔
350
  bool atExit = true;
13,893✔
351

352
  if (pVnode->pImpl && vnodeIsLeader(pVnode->pImpl)) {
13,893✔
353
    vnodeProposeCommitOnNeed(pVnode->pImpl, atExit);
11,358✔
354
  }
355

356
  (void)taosThreadRwlockWrlock(&pMgmt->hashLock);
13,891✔
357
  vmUnRegisterRunningState(pMgmt, pVnode->vgId);
13,893✔
358
  if (keepClosed) {
13,893✔
359
    if (vmRegisterClosedState(pMgmt, pVnode) != 0) {
1,769!
360
      (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
×
361
      return;
×
362
    };
363
  }
364
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
13,893✔
365

366
  vmReleaseVnode(pMgmt, pVnode);
13,893✔
367

368
  if (pVnode->failed) {
13,893!
369
    goto _closed;
×
370
  }
371
  dInfo("vgId:%d, pre close", pVnode->vgId);
13,893!
372
  vnodePreClose(pVnode->pImpl);
13,893✔
373

374
  dInfo("vgId:%d, wait for vnode ref become 0", pVnode->vgId);
13,892!
375
  while (pVnode->refCount > 0) taosMsleep(10);
13,893!
376

377
  dInfo("vgId:%d, wait for vnode write queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pWriteW.queue,
13,893!
378
        taosQueueGetThreadId(pVnode->pWriteW.queue));
379
  tMultiWorkerCleanup(&pVnode->pWriteW);
13,892✔
380

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

385
  dInfo("vgId:%d, wait for vnode sync rd queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pSyncRdW.queue,
13,893!
386
        taosQueueGetThreadId(pVnode->pSyncRdW.queue));
387
  tMultiWorkerCleanup(&pVnode->pSyncRdW);
13,892✔
388

389
  dInfo("vgId:%d, wait for vnode apply queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pApplyW.queue,
13,893!
390
        taosQueueGetThreadId(pVnode->pApplyW.queue));
391
  tMultiWorkerCleanup(&pVnode->pApplyW);
13,893✔
392

393
  dInfo("vgId:%d, wait for vnode fetch queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pFetchQ,
13,892!
394
        taosQueueGetThreadId(pVnode->pFetchQ));
395
  while (!taosQueueEmpty(pVnode->pFetchQ)) taosMsleep(10);
13,893!
396

397
  dInfo("vgId:%d, wait for vnode query queue:%p is empty", pVnode->vgId, pVnode->pQueryQ);
13,893!
398
  while (!taosQueueEmpty(pVnode->pQueryQ)) taosMsleep(10);
13,893!
399

400
  tqNotifyClose(pVnode->pImpl->pTq);
13,893✔
401

402
  dInfo("vgId:%d, wait for vnode stream queue:%p is empty, %d remains", pVnode->vgId,
13,893✔
403
        pVnode->pStreamQ, taosQueueItemSize(pVnode->pStreamQ));
404
  while (!taosQueueEmpty(pVnode->pStreamQ)) taosMsleep(50);
13,893!
405

406
  dInfo("vgId:%d, wait for vnode stream ctrl queue:%p is empty", pVnode->vgId, pVnode->pStreamCtrlQ);
13,893!
407
  while (!taosQueueEmpty(pVnode->pStreamCtrlQ)) taosMsleep(50);
13,893!
408

409
  dInfo("vgId:%d, wait for vnode stream long-exec queue:%p is empty, %d remains", pVnode->vgId,
13,893!
410
        pVnode->pStreamLongExecQ, taosQueueItemSize(pVnode->pStreamLongExecQ));
411
  while (!taosQueueEmpty(pVnode->pStreamLongExecQ)) taosMsleep(50);
13,893!
412

413
  dInfo("vgId:%d, wait for vnode stream chkpt queue:%p is empty", pVnode->vgId, pVnode->pStreamChkQ);
13,893!
414
  while (!taosQueueEmpty(pVnode->pStreamChkQ)) taosMsleep(10);
13,893!
415

416
  dInfo("vgId:%d, all vnode queues is empty", pVnode->vgId);
13,893!
417

418
  dInfo("vgId:%d, post close", pVnode->vgId);
13,893!
419
  vnodePostClose(pVnode->pImpl);
13,893✔
420

421
  vmFreeQueue(pMgmt, pVnode);
13,892✔
422

423
  if (commitAndRemoveWal) {
13,892✔
424
    dInfo("vgId:%d, commit data for vnode split", pVnode->vgId);
78!
425
    if (vnodeSyncCommit(pVnode->pImpl) != 0) {
78!
426
      dError("vgId:%d, failed to commit data", pVnode->vgId);
×
427
    }
428
    if (vnodeBegin(pVnode->pImpl) != 0) {
78!
429
      dError("vgId:%d, failed to begin", pVnode->vgId);
×
430
    }
431
    dInfo("vgId:%d, commit data finished", pVnode->vgId);
78!
432
  }
433

434
  int32_t nodeId = vnodeNodeId(pVnode->pImpl);
13,892✔
435
  vnodeClose(pVnode->pImpl);
13,891✔
436
  pVnode->pImpl = NULL;
13,893✔
437

438
_closed:
13,893✔
439
  dInfo("vgId:%d, vnode is closed", pVnode->vgId);
13,893!
440

441
  if (commitAndRemoveWal) {
13,893✔
442
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d%swal", TD_DIRSEP, pVnode->vgId, TD_DIRSEP);
78✔
443
    dInfo("vgId:%d, remove all wals, path:%s", pVnode->vgId, path);
78!
444
    if (tfsRmdir(pMgmt->pTfs, path) != 0) {
78!
445
      dTrace("vgId:%d, failed to remove wals, path:%s", pVnode->vgId, path);
×
446
    }
447
    if (tfsMkdir(pMgmt->pTfs, path) != 0) {
78!
448
      dTrace("vgId:%d, failed to create wals, path:%s", pVnode->vgId, path);
×
449
    }
450
  }
451

452
  if (pVnode->dropped) {
13,893✔
453
    dInfo("vgId:%d, vnode is destroyed, dropped:%d", pVnode->vgId, pVnode->dropped);
4,192!
454
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pVnode->vgId);
4,192✔
455
    vnodeDestroy(pVnode->vgId, path, pMgmt->pTfs, nodeId);
4,192✔
456
  }
457

458
  vmFreeVnodeObj(&pVnode);
13,893✔
459
}
460

461
void vmCloseFailedVnode(SVnodeMgmt *pMgmt, int32_t vgId) {
×
462
  int32_t r = 0;
×
463
  r = taosThreadRwlockWrlock(&pMgmt->hashLock);
×
464
  if (r != 0) {
×
465
    dError("vgId:%d, failed to lock since %s", vgId, tstrerror(r));
×
466
  }
467
  if (r == 0) {
×
468
    vmUnRegisterRunningState(pMgmt, vgId);
×
469
  }
470
  r = taosThreadRwlockUnlock(&pMgmt->hashLock);
×
471
  if (r != 0) {
×
472
    dError("vgId:%d, failed to unlock since %s", vgId, tstrerror(r));
×
473
  }
474
}
×
475

476
static int32_t vmRestoreVgroupId(SWrapperCfg *pCfg, STfs *pTfs) {
×
477
  int32_t srcVgId = pCfg->vgId;
×
478
  int32_t dstVgId = pCfg->toVgId;
×
479
  if (dstVgId == 0) return 0;
×
480

481
  char srcPath[TSDB_FILENAME_LEN];
482
  char dstPath[TSDB_FILENAME_LEN];
483

484
  snprintf(srcPath, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, srcVgId);
×
485
  snprintf(dstPath, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, dstVgId);
×
486

487
  int32_t diskPrimary = pCfg->diskPrimary;
×
488
  int32_t vgId = vnodeRestoreVgroupId(srcPath, dstPath, srcVgId, dstVgId, diskPrimary, pTfs);
×
489
  if (vgId <= 0) {
×
490
    dError("vgId:%d, failed to restore vgroup id. srcPath: %s", pCfg->vgId, srcPath);
×
491
    return -1;
×
492
  }
493

494
  pCfg->vgId = vgId;
×
495
  pCfg->toVgId = 0;
×
496
  return 0;
×
497
}
498

499
static void *vmOpenVnodeInThread(void *param) {
1,215✔
500
  SVnodeThread *pThread = param;
1,215✔
501
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
1,215✔
502
  char          path[TSDB_FILENAME_LEN];
503

504
  dInfo("thread:%d, start to open or destroy %d vnodes", pThread->threadIndex, pThread->vnodeNum);
1,215!
505
  setThreadName("open-vnodes");
1,215✔
506

507
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
2,444✔
508
    SWrapperCfg *pCfg = &pThread->pCfgs[v];
1,229✔
509
    if (pCfg->dropped) {
1,229!
510
      char stepDesc[TSDB_STEP_DESC_LEN] = {0};
×
511
      snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to destroy, %d of %d have been dropped", pCfg->vgId,
×
512
               pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
513
      tmsgReportStartup("vnode-destroy", stepDesc);
×
514

515
      snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pCfg->vgId);
×
516
      vnodeDestroy(pCfg->vgId, path, pMgmt->pTfs, 0);
×
517
      pThread->updateVnodesList = true;
×
518
      pThread->dropped++;
×
519
      (void)atomic_add_fetch_32(&pMgmt->state.dropVnodes, 1);
×
520
      continue;
×
521
    }
522

523
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
1,229✔
524
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been opened", pCfg->vgId,
1,229✔
525
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
526
    tmsgReportStartup("vnode-open", stepDesc);
1,229✔
527

528
    if (pCfg->toVgId) {
1,229!
529
      if (vmRestoreVgroupId(pCfg, pMgmt->pTfs) != 0) {
×
530
        dError("vgId:%d, failed to restore vgroup id by thread:%d", pCfg->vgId, pThread->threadIndex);
×
531
        pThread->failed++;
×
532
        continue;
×
533
      }
534
      pThread->updateVnodesList = true;
×
535
    }
536

537
    int32_t diskPrimary = pCfg->diskPrimary;
1,229✔
538
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pCfg->vgId);
1,229✔
539

540
    SVnode *pImpl = vnodeOpen(path, diskPrimary, pMgmt->pTfs, pMgmt->msgCb, false);
1,229✔
541

542
    if (pImpl == NULL) {
1,229!
543
      dError("vgId:%d, failed to open vnode by thread:%d since %s", pCfg->vgId, pThread->threadIndex, terrstr());
×
544
      if (terrno != TSDB_CODE_NEED_RETRY) {
×
545
        pThread->failed++;
×
546
        continue;
×
547
      }
548
    }
549

550
    if (pImpl != NULL) {
1,229!
551
      if (vmOpenVnode(pMgmt, pCfg, pImpl) != 0) {
1,229!
552
        dError("vgId:%d, failed to open vnode by thread:%d", pCfg->vgId, pThread->threadIndex);
×
553
        pThread->failed++;
×
554
        continue;
×
555
      }
556
    }
557

558
    dInfo("vgId:%d, is opened by thread:%d", pCfg->vgId, pThread->threadIndex);
1,229!
559
    pThread->opened++;
1,229✔
560
    (void)atomic_add_fetch_32(&pMgmt->state.openVnodes, 1);
1,229✔
561
  }
562

563
  dInfo("thread:%d, numOfVnodes:%d, opened:%d dropped:%d failed:%d", pThread->threadIndex, pThread->vnodeNum,
1,215!
564
        pThread->opened, pThread->dropped, pThread->failed);
565
  return NULL;
1,215✔
566
}
567

568
static int32_t vmOpenVnodes(SVnodeMgmt *pMgmt) {
2,741✔
569
  pMgmt->runngingHash =
2,741✔
570
      taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
2,741✔
571
  if (pMgmt->runngingHash == NULL) {
2,741!
572
    dError("failed to init vnode hash since %s", terrstr());
×
573
    return TSDB_CODE_OUT_OF_MEMORY;
×
574
  }
575

576
  pMgmt->closedHash =
2,741✔
577
      taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
2,741✔
578
  if (pMgmt->closedHash == NULL) {
2,741!
579
    dError("failed to init vnode closed hash since %s", terrstr());
×
580
    return TSDB_CODE_OUT_OF_MEMORY;
×
581
  }
582

583
  pMgmt->creatingHash =
2,741✔
584
      taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
2,741✔
585
  if (pMgmt->creatingHash == NULL) {
2,741!
586
    dError("failed to init vnode creatingHash hash since %s", terrstr());
×
587
    return TSDB_CODE_OUT_OF_MEMORY;
×
588
  }
589

590
  SWrapperCfg *pCfgs = NULL;
2,741✔
591
  int32_t      numOfVnodes = 0;
2,741✔
592
  int32_t      code = 0;
2,741✔
593
  if ((code = vmGetVnodeListFromFile(pMgmt, &pCfgs, &numOfVnodes)) != 0) {
2,741!
594
    dInfo("failed to get vnode list from disk since %s", tstrerror(code));
×
595
    return code;
×
596
  }
597

598
  pMgmt->state.totalVnodes = numOfVnodes;
2,741✔
599

600
  int32_t threadNum = tsNumOfCores / 2;
2,741✔
601
  if (threadNum < 1) threadNum = 1;
2,741!
602
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
2,741✔
603

604
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
2,741!
605
  if (threads == NULL) {
2,741!
606
    dError("failed to allocate memory for threads since %s", terrstr());
×
607
    taosMemoryFree(pCfgs);
×
608
    return terrno;
×
609
  }
610

611
  for (int32_t t = 0; t < threadNum; ++t) {
57,561✔
612
    threads[t].threadIndex = t;
54,820✔
613
    threads[t].pMgmt = pMgmt;
54,820✔
614
    threads[t].pCfgs = taosMemoryCalloc(vnodesPerThread, sizeof(SWrapperCfg));
54,820!
615
  }
616

617
  for (int32_t v = 0; v < numOfVnodes; ++v) {
3,970✔
618
    int32_t       t = v % threadNum;
1,229✔
619
    SVnodeThread *pThread = &threads[t];
1,229✔
620
    pThread->pCfgs[pThread->vnodeNum++] = pCfgs[v];
1,229✔
621
  }
622

623
  dInfo("open %d vnodes with %d threads", numOfVnodes, threadNum);
2,741!
624

625
  for (int32_t t = 0; t < threadNum; ++t) {
57,561✔
626
    SVnodeThread *pThread = &threads[t];
54,820✔
627
    if (pThread->vnodeNum == 0) continue;
54,820✔
628

629
    TdThreadAttr thAttr;
630
    (void)taosThreadAttrInit(&thAttr);
1,215✔
631
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
1,215✔
632
#ifdef TD_COMPACT_OS
633
    (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
634
#endif
635
    if (taosThreadCreate(&pThread->thread, &thAttr, vmOpenVnodeInThread, pThread) != 0) {
1,215!
636
      dError("thread:%d, failed to create thread to open vnode, reason:%s", pThread->threadIndex, strerror(ERRNO));
×
637
    }
638

639
    (void)taosThreadAttrDestroy(&thAttr);
1,215✔
640
  }
641

642
  bool updateVnodesList = false;
2,741✔
643

644
  for (int32_t t = 0; t < threadNum; ++t) {
57,561✔
645
    SVnodeThread *pThread = &threads[t];
54,820✔
646
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
54,820!
647
      (void)taosThreadJoin(pThread->thread, NULL);
1,215✔
648
      taosThreadClear(&pThread->thread);
1,215✔
649
    }
650
    taosMemoryFree(pThread->pCfgs);
54,820!
651
    if (pThread->updateVnodesList) updateVnodesList = true;
54,820!
652
  }
653
  taosMemoryFree(threads);
2,741!
654
  taosMemoryFree(pCfgs);
2,741!
655

656
  if ((pMgmt->state.openVnodes + pMgmt->state.dropVnodes) != pMgmt->state.totalVnodes) {
2,741!
657
    dError("there are total vnodes:%d, opened:%d", pMgmt->state.totalVnodes, pMgmt->state.openVnodes);
×
658
    return terrno = TSDB_CODE_VND_INIT_FAILED;
×
659
  }
660

661
  if (updateVnodesList && (code = vmWriteVnodeListToFile(pMgmt)) != 0) {
2,741!
662
    dError("failed to write vnode list since %s", tstrerror(code));
×
663
    return code;
×
664
  }
665

666
  dInfo("successfully opened %d vnodes", pMgmt->state.totalVnodes);
2,741!
667
  return 0;
2,741✔
668
}
669

670
static void *vmCloseVnodeInThread(void *param) {
7,768✔
671
  SVnodeThread *pThread = param;
7,768✔
672
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
7,768✔
673

674
  dInfo("thread:%d, start to close %d vnodes", pThread->threadIndex, pThread->vnodeNum);
7,768✔
675
  setThreadName("close-vnodes");
7,771✔
676

677
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
15,622✔
678
    SVnodeObj *pVnode = pThread->ppVnodes[v];
7,854✔
679

680
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
7,854✔
681
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to close, %d of %d have been closed", pVnode->vgId,
7,854✔
682
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
683
    tmsgReportStartup("vnode-close", stepDesc);
7,854✔
684

685
    vmCloseVnode(pMgmt, pVnode, false, false);
7,854✔
686
  }
687

688
  dInfo("thread:%d, numOfVnodes:%d is closed", pThread->threadIndex, pThread->vnodeNum);
7,768!
689
  return NULL;
7,768✔
690
}
691

692
static void vmCloseVnodes(SVnodeMgmt *pMgmt) {
2,741✔
693
  int32_t code = 0;
2,741✔
694
  dInfo("start to close all vnodes");
2,741!
695
  tSingleWorkerCleanup(&pMgmt->mgmtWorker);
2,741✔
696
  dInfo("vnodes mgmt worker is stopped");
2,741!
697
  tSingleWorkerCleanup(&pMgmt->mgmtMultiWorker);
2,741✔
698
  dInfo("vnodes multiple mgmt worker is stopped");
2,741!
699

700
  int32_t     numOfVnodes = 0;
2,741✔
701
  SVnodeObj **ppVnodes = NULL;
2,741✔
702
  code = vmGetVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes);
2,741✔
703
  if (code != 0) {
2,741!
704
    dError("failed to get vnode list since %s", tstrerror(code));
×
705
    return;
×
706
  }
707

708
  int32_t threadNum = tsNumOfCores / 2;
2,741✔
709
  if (threadNum < 1) threadNum = 1;
2,741!
710
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
2,741✔
711

712
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
2,741!
713
  for (int32_t t = 0; t < threadNum; ++t) {
57,561✔
714
    threads[t].threadIndex = t;
54,820✔
715
    threads[t].pMgmt = pMgmt;
54,820✔
716
    threads[t].ppVnodes = taosMemoryCalloc(vnodesPerThread, sizeof(SVnode *));
54,820!
717
  }
718

719
  for (int32_t v = 0; v < numOfVnodes; ++v) {
10,595✔
720
    int32_t       t = v % threadNum;
7,854✔
721
    SVnodeThread *pThread = &threads[t];
7,854✔
722
    if (pThread->ppVnodes != NULL && ppVnodes != NULL) {
7,854!
723
      pThread->ppVnodes[pThread->vnodeNum++] = ppVnodes[v];
7,854✔
724
    }
725
  }
726

727
  pMgmt->state.openVnodes = 0;
2,741✔
728
  dInfo("close %d vnodes with %d threads", numOfVnodes, threadNum);
2,741!
729

730
  int64_t st = taosGetTimestampMs();
2,741✔
731
  dInfo("notify all streams closed in all %d vnodes, ts:%" PRId64, numOfVnodes, st);
2,741!
732
  if (ppVnodes != NULL) {
2,741!
733
    for (int32_t i = 0; i < numOfVnodes; ++i) {
10,595✔
734
      if (ppVnodes[i] != NULL) {
7,854!
735
        if (ppVnodes[i]->pImpl != NULL) {
7,854!
736
          tqNotifyClose(ppVnodes[i]->pImpl->pTq);
7,854✔
737
        }
738
      }
739
    }
740
  }
741

742
  int64_t et = taosGetTimestampMs();
2,741✔
743
  dInfo("notify close stream completed in %d vnodes, elapsed time: %" PRId64 "ms", numOfVnodes, et - st);
2,741!
744

745
  for (int32_t t = 0; t < threadNum; ++t) {
57,561✔
746
    SVnodeThread *pThread = &threads[t];
54,820✔
747
    if (pThread->vnodeNum == 0) continue;
54,820✔
748

749
    TdThreadAttr thAttr;
750
    (void)taosThreadAttrInit(&thAttr);
7,768✔
751
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
7,768✔
752
#ifdef TD_COMPACT_OS
753
    (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
754
#endif
755
    if (taosThreadCreate(&pThread->thread, &thAttr, vmCloseVnodeInThread, pThread) != 0) {
7,768!
756
      dError("thread:%d, failed to create thread to close vnode since %s", pThread->threadIndex, strerror(ERRNO));
×
757
    }
758

759
    (void)taosThreadAttrDestroy(&thAttr);
7,768✔
760
  }
761

762
  for (int32_t t = 0; t < threadNum; ++t) {
57,561✔
763
    SVnodeThread *pThread = &threads[t];
54,820✔
764
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
54,820!
765
      (void)taosThreadJoin(pThread->thread, NULL);
7,768✔
766
      taosThreadClear(&pThread->thread);
7,768✔
767
    }
768
    taosMemoryFree(pThread->ppVnodes);
54,820!
769
  }
770
  taosMemoryFree(threads);
2,741!
771

772
  if (ppVnodes != NULL) {
2,741!
773
    taosMemoryFree(ppVnodes);
2,741!
774
  }
775

776
  if (pMgmt->runngingHash != NULL) {
2,741!
777
    taosHashCleanup(pMgmt->runngingHash);
2,741✔
778
    pMgmt->runngingHash = NULL;
2,741✔
779
  }
780

781
  void *pIter = taosHashIterate(pMgmt->closedHash, NULL);
2,741✔
782
  while (pIter) {
2,741!
783
    SVnodeObj **ppVnode = pIter;
×
784
    vmFreeVnodeObj(ppVnode);
×
785
    pIter = taosHashIterate(pMgmt->closedHash, pIter);
×
786
  }
787

788
  if (pMgmt->closedHash != NULL) {
2,741!
789
    taosHashCleanup(pMgmt->closedHash);
2,741✔
790
    pMgmt->closedHash = NULL;
2,741✔
791
  }
792

793
  pIter = taosHashIterate(pMgmt->creatingHash, NULL);
2,741✔
794
  while (pIter) {
2,741!
795
    SVnodeObj **ppVnode = pIter;
×
796
    vmFreeVnodeObj(ppVnode);
×
797
    pIter = taosHashIterate(pMgmt->creatingHash, pIter);
×
798
  }
799

800
  if (pMgmt->creatingHash != NULL) {
2,741!
801
    taosHashCleanup(pMgmt->creatingHash);
2,741✔
802
    pMgmt->creatingHash = NULL;
2,741✔
803
  }
804

805
  dInfo("total vnodes:%d are all closed", numOfVnodes);
2,741!
806
}
807

808
static void vmCleanup(SVnodeMgmt *pMgmt) {
2,741✔
809
  vmCloseVnodes(pMgmt);
2,741✔
810
  vmStopWorker(pMgmt);
2,741✔
811
  vnodeCleanup();
2,741✔
812
  (void)taosThreadRwlockDestroy(&pMgmt->hashLock);
2,741✔
813
  (void)taosThreadMutexDestroy(&pMgmt->mutex);
2,741✔
814
  (void)taosThreadMutexDestroy(&pMgmt->fileLock);
2,741✔
815
  taosMemoryFree(pMgmt);
2,741!
816
}
2,741✔
817

818
static void vmCheckSyncTimeout(SVnodeMgmt *pMgmt) {
2,323✔
819
  int32_t     code = 0;
2,323✔
820
  int32_t     numOfVnodes = 0;
2,323✔
821
  SVnodeObj **ppVnodes = NULL;
2,323✔
822
  code = vmGetVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes);
2,323✔
823
  if (code != 0) {
2,323!
824
    dError("failed to get vnode list since %s", tstrerror(code));
×
825
    return;
×
826
  }
827

828
  if (ppVnodes != NULL) {
2,323!
829
    for (int32_t i = 0; i < numOfVnodes; ++i) {
8,946✔
830
      SVnodeObj *pVnode = ppVnodes[i];
6,623✔
831
      if (!pVnode->failed) {
6,623!
832
        vnodeSyncCheckTimeout(pVnode->pImpl);
6,623✔
833
      }
834
      vmReleaseVnode(pMgmt, pVnode);
6,623✔
835
    }
836
    taosMemoryFree(ppVnodes);
2,323!
837
  }
838
}
839

840
static void *vmThreadFp(void *param) {
2,741✔
841
  SVnodeMgmt *pMgmt = param;
2,741✔
842
  int64_t     lastTime = 0;
2,741✔
843
  setThreadName("vnode-timer");
2,741✔
844

845
  while (1) {
994,311✔
846
    lastTime++;
997,052✔
847
    taosMsleep(100);
997,052✔
848
    if (pMgmt->stop) break;
997,052✔
849
    if (lastTime % 10 != 0) continue;
994,311✔
850

851
    int64_t sec = lastTime / 10;
98,214✔
852
    if (sec % (VNODE_TIMEOUT_SEC / 2) == 0) {
98,214✔
853
      vmCheckSyncTimeout(pMgmt);
2,323✔
854
    }
855
  }
856

857
  return NULL;
2,741✔
858
}
859

860
static int32_t vmInitTimer(SVnodeMgmt *pMgmt) {
2,741✔
861
  int32_t      code = 0;
2,741✔
862
  TdThreadAttr thAttr;
863
  (void)taosThreadAttrInit(&thAttr);
2,741✔
864
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
2,741✔
865
#ifdef TD_COMPACT_OS
866
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
867
#endif
868
  if (taosThreadCreate(&pMgmt->thread, &thAttr, vmThreadFp, pMgmt) != 0) {
2,741!
869
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
870
    dError("failed to create vnode timer thread since %s", tstrerror(code));
×
871
    return code;
×
872
  }
873

874
  (void)taosThreadAttrDestroy(&thAttr);
2,741✔
875
  return 0;
2,741✔
876
}
877

878
static void vmCleanupTimer(SVnodeMgmt *pMgmt) {
2,741✔
879
  pMgmt->stop = true;
2,741✔
880
  if (taosCheckPthreadValid(pMgmt->thread)) {
2,741!
881
    (void)taosThreadJoin(pMgmt->thread, NULL);
2,741✔
882
    taosThreadClear(&pMgmt->thread);
2,741✔
883
  }
884
}
2,741✔
885

886
static int32_t vmInit(SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) {
2,741✔
887
  int32_t code = -1;
2,741✔
888

889
  SVnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SVnodeMgmt));
2,741!
890
  if (pMgmt == NULL) {
2,741!
891
    code = terrno;
×
892
    goto _OVER;
×
893
  }
894

895
  pMgmt->pData = pInput->pData;
2,741✔
896
  pMgmt->path = pInput->path;
2,741✔
897
  pMgmt->name = pInput->name;
2,741✔
898
  pMgmt->msgCb = pInput->msgCb;
2,741✔
899
  pMgmt->msgCb.putToQueueFp = (PutToQueueFp)vmPutRpcMsgToQueue;
2,741✔
900
  pMgmt->msgCb.qsizeFp = (GetQueueSizeFp)vmGetQueueSize;
2,741✔
901
  pMgmt->msgCb.mgmt = pMgmt;
2,741✔
902

903
  code = taosThreadRwlockInit(&pMgmt->hashLock, NULL);
2,741✔
904
  if (code != 0) {
2,741!
905
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
906
    goto _OVER;
×
907
  }
908

909
  code = taosThreadMutexInit(&pMgmt->mutex, NULL);
2,741✔
910
  if (code != 0) {
2,741!
911
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
912
    goto _OVER;
×
913
  }
914

915
  code = taosThreadMutexInit(&pMgmt->fileLock, NULL);
2,741✔
916
  if (code != 0) {
2,741!
917
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
918
    goto _OVER;
×
919
  }
920

921
  pMgmt->pTfs = pInput->pTfs;
2,741✔
922
  if (pMgmt->pTfs == NULL) {
2,741!
923
    dError("tfs is null.");
×
924
    goto _OVER;
×
925
  }
926
  tmsgReportStartup("vnode-tfs", "initialized");
2,741✔
927
  if ((code = walInit(pInput->stopDnodeFp)) != 0) {
2,741!
928
    dError("failed to init wal since %s", tstrerror(code));
×
929
    goto _OVER;
×
930
  }
931

932
  tmsgReportStartup("vnode-wal", "initialized");
2,741✔
933

934
  if ((code = syncInit()) != 0) {
2,741!
935
    dError("failed to open sync since %s", tstrerror(code));
×
936
    goto _OVER;
×
937
  }
938
  tmsgReportStartup("vnode-sync", "initialized");
2,741✔
939

940
  if ((code = vnodeInit(pInput->stopDnodeFp)) != 0) {
2,741!
941
    dError("failed to init vnode since %s", tstrerror(code));
×
942
    goto _OVER;
×
943
  }
944
  tmsgReportStartup("vnode-commit", "initialized");
2,741✔
945

946
  if ((code = vmStartWorker(pMgmt)) != 0) {
2,741!
947
    dError("failed to init workers since %s", tstrerror(code));
×
948
    goto _OVER;
×
949
  }
950
  tmsgReportStartup("vnode-worker", "initialized");
2,741✔
951

952
  if ((code = vmOpenVnodes(pMgmt)) != 0) {
2,741!
953
    dError("failed to open all vnodes since %s", tstrerror(code));
×
954
    goto _OVER;
×
955
  }
956
  tmsgReportStartup("vnode-vnodes", "initialized");
2,741✔
957

958
  if ((code = udfcOpen()) != 0) {
2,741!
959
    dError("failed to open udfc in vnode since %s", tstrerror(code));
×
960
    goto _OVER;
×
961
  }
962

963
  code = 0;
2,741✔
964

965
_OVER:
2,741✔
966
  if (code == 0) {
2,741!
967
    pOutput->pMgmt = pMgmt;
2,741✔
968
  } else {
969
    dError("failed to init vnodes-mgmt since %s", tstrerror(code));
×
970
    vmCleanup(pMgmt);
×
971
  }
972

973
  return code;
2,741✔
974
}
975

976
static int32_t vmRequire(const SMgmtInputOpt *pInput, bool *required) {
2,784✔
977
  *required = tsNumOfSupportVnodes > 0;
2,784✔
978
  return 0;
2,784✔
979
}
980

981
static void *vmRestoreVnodeInThread(void *param) {
1,214✔
982
  SVnodeThread *pThread = param;
1,214✔
983
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
1,214✔
984

985
  dInfo("thread:%d, start to restore %d vnodes", pThread->threadIndex, pThread->vnodeNum);
1,214!
986
  setThreadName("restore-vnodes");
1,214✔
987

988
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
2,444✔
989
    SVnodeObj *pVnode = pThread->ppVnodes[v];
1,229✔
990
    if (pVnode->failed) {
1,229!
991
      dError("vgId:%d, cannot restore a vnode in failed mode.", pVnode->vgId);
×
992
      continue;
×
993
    }
994

995
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
1,229✔
996
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been restored", pVnode->vgId,
1,229✔
997
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
998
    tmsgReportStartup("vnode-restore", stepDesc);
1,229✔
999

1000
    int32_t code = vnodeStart(pVnode->pImpl);
1,229✔
1001
    if (code != 0) {
1,229!
1002
      dError("vgId:%d, failed to restore vnode by thread:%d", pVnode->vgId, pThread->threadIndex);
×
1003
      pThread->failed++;
×
1004
    } else {
1005
      dInfo("vgId:%d, is restored by thread:%d", pVnode->vgId, pThread->threadIndex);
1,229!
1006
      pThread->opened++;
1,229✔
1007
      (void)atomic_add_fetch_32(&pMgmt->state.openVnodes, 1);
1,229✔
1008
    }
1009
  }
1010

1011
  dInfo("thread:%d, numOfVnodes:%d, restored:%d failed:%d", pThread->threadIndex, pThread->vnodeNum, pThread->opened,
1,215!
1012
        pThread->failed);
1013
  return NULL;
1,215✔
1014
}
1015

1016
static int32_t vmStartVnodes(SVnodeMgmt *pMgmt) {
2,741✔
1017
  int32_t     code = 0;
2,741✔
1018
  int32_t     numOfVnodes = 0;
2,741✔
1019
  SVnodeObj **ppVnodes = NULL;
2,741✔
1020
  code = vmGetVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes);
2,741✔
1021
  if (code != 0) {
2,741!
1022
    dError("failed to get vnode list since %s", tstrerror(code));
×
1023
    return code;
×
1024
  }
1025

1026
  int32_t threadNum = tsNumOfCores / 2;
2,741✔
1027
  if (threadNum < 1) threadNum = 1;
2,741!
1028
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
2,741✔
1029

1030
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
2,741!
1031
  if (threads == NULL) {
2,741!
1032
    return terrno;
×
1033
  }
1034

1035
  for (int32_t t = 0; t < threadNum; ++t) {
57,561✔
1036
    threads[t].threadIndex = t;
54,820✔
1037
    threads[t].pMgmt = pMgmt;
54,820✔
1038
    threads[t].ppVnodes = taosMemoryCalloc(vnodesPerThread, sizeof(SVnode *));
54,820!
1039
    if (threads[t].ppVnodes == NULL) {
54,820!
1040
      code = terrno;
×
1041
      break;
×
1042
    }
1043
  }
1044

1045
  for (int32_t v = 0; v < numOfVnodes; ++v) {
3,970✔
1046
    int32_t       t = v % threadNum;
1,229✔
1047
    SVnodeThread *pThread = &threads[t];
1,229✔
1048
    if (pThread->ppVnodes != NULL && ppVnodes != NULL) {
1,229!
1049
      pThread->ppVnodes[pThread->vnodeNum++] = ppVnodes[v];
1,229✔
1050
    }
1051
  }
1052

1053
  pMgmt->state.openVnodes = 0;
2,741✔
1054
  pMgmt->state.dropVnodes = 0;
2,741✔
1055
  dInfo("restore %d vnodes with %d threads", numOfVnodes, threadNum);
2,741!
1056

1057
  for (int32_t t = 0; t < threadNum; ++t) {
57,561✔
1058
    SVnodeThread *pThread = &threads[t];
54,820✔
1059
    if (pThread->vnodeNum == 0) continue;
54,820✔
1060

1061
    TdThreadAttr thAttr;
1062
    (void)taosThreadAttrInit(&thAttr);
1,215✔
1063
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
1,215✔
1064
    if (taosThreadCreate(&pThread->thread, &thAttr, vmRestoreVnodeInThread, pThread) != 0) {
1,215!
1065
      dError("thread:%d, failed to create thread to restore vnode since %s", pThread->threadIndex, strerror(ERRNO));
×
1066
    }
1067

1068
    (void)taosThreadAttrDestroy(&thAttr);
1,215✔
1069
  }
1070

1071
  for (int32_t t = 0; t < threadNum; ++t) {
57,561✔
1072
    SVnodeThread *pThread = &threads[t];
54,820✔
1073
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
54,820!
1074
      (void)taosThreadJoin(pThread->thread, NULL);
1,215✔
1075
      taosThreadClear(&pThread->thread);
1,215✔
1076
    }
1077
    taosMemoryFree(pThread->ppVnodes);
54,820!
1078
  }
1079
  taosMemoryFree(threads);
2,741!
1080

1081
  for (int32_t i = 0; i < numOfVnodes; ++i) {
3,970✔
1082
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
1,229!
1083
    vmReleaseVnode(pMgmt, ppVnodes[i]);
1,229✔
1084
  }
1085

1086
  if (ppVnodes != NULL) {
2,741!
1087
    taosMemoryFree(ppVnodes);
2,741!
1088
  }
1089

1090
  return vmInitTimer(pMgmt);
2,741✔
1091

1092
_exit:
1093
  for (int32_t t = 0; t < threadNum; ++t) {
1094
    SVnodeThread *pThread = &threads[t];
1095
    taosMemoryFree(pThread->ppVnodes);
1096
  }
1097
  taosMemoryFree(threads);
1098
  return code;
1099
}
1100

1101
static void vmStop(SVnodeMgmt *pMgmt) { vmCleanupTimer(pMgmt); }
2,741✔
1102

1103
SMgmtFunc vmGetMgmtFunc() {
2,784✔
1104
  SMgmtFunc mgmtFunc = {0};
2,784✔
1105
  mgmtFunc.openFp = vmInit;
2,784✔
1106
  mgmtFunc.closeFp = (NodeCloseFp)vmCleanup;
2,784✔
1107
  mgmtFunc.startFp = (NodeStartFp)vmStartVnodes;
2,784✔
1108
  mgmtFunc.stopFp = (NodeStopFp)vmStop;
2,784✔
1109
  mgmtFunc.requiredFp = vmRequire;
2,784✔
1110
  mgmtFunc.getHandlesFp = vmGetMsgHandles;
2,784✔
1111

1112
  return mgmtFunc;
2,784✔
1113
}
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