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

taosdata / TDengine / #4940

27 Jan 2026 10:23AM UTC coverage: 66.832% (-0.1%) from 66.931%
#4940

push

travis-ci

web-flow
fix: asan invalid write issue (#34400)

7 of 8 new or added lines in 2 files covered. (87.5%)

822 existing lines in 141 files now uncovered.

204293 of 305680 relevant lines covered (66.83%)

124534723.83 hits per line

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

75.29
/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) {
2,612,580✔
24
  int32_t    diskId = -1;
2,612,580✔
25
  SVnodeObj *pVnode = NULL;
2,612,580✔
26

27
  (void)taosThreadRwlockRdlock(&pMgmt->hashLock);
2,664,233✔
28
  int32_t r = taosHashGetDup(pMgmt->runngingHash, &vgId, sizeof(int32_t), (void *)&pVnode);
2,663,645✔
29
  if (pVnode != NULL) {
2,664,734✔
30
    diskId = pVnode->diskPrimary;
×
31
  }
32
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
2,664,734✔
33
  return diskId;
2,666,939✔
34
}
35

36
static void vmFreeVnodeObj(SVnodeObj **ppVnode) {
7,129,459✔
37
  if (!ppVnode || !(*ppVnode)) return;
7,129,459✔
38

39
  SVnodeObj *pVnode = *ppVnode;
7,129,459✔
40

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

48
  taosMemoryFree(pVnode->path);
7,129,508✔
49
  taosMemoryFree(pVnode);
7,128,014✔
50
  ppVnode[0] = NULL;
7,129,508✔
51
}
52

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

62
  pCreatingVnode->vgId = vgId;
2,666,939✔
63
  pCreatingVnode->diskPrimary = diskId;
2,666,939✔
64

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

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

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

83
  return code;
2,666,939✔
84
}
85

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

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

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

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

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

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

128
  diskId = tfsSearch(pTfs, 0, fname);
2,654,752✔
129
  if (diskId >= 0) {
2,663,152✔
130
    return diskId;
×
131
  }
132
  diskId = tfsSearch(pTfs, 0, fnameTmp);
2,663,152✔
133
  if (diskId >= 0) {
2,663,844✔
134
    return diskId;
×
135
  }
136

137
  // alloc
138
  int32_t     disks[TFS_MAX_DISKS_PER_TIER] = {0};
2,663,844✔
139
  int32_t     numOfVnodes = 0;
2,663,153✔
140
  SVnodeObj **ppVnodes = NULL;
2,663,348✔
141

142
  code = taosThreadMutexLock(&pMgmt->mutex);
2,662,333✔
143
  if (code != 0) {
2,666,939✔
144
    return code;
×
145
  }
146

147
  code = vmGetAllVnodeListFromHashWithCreating(pMgmt, &numOfVnodes, &ppVnodes);
2,666,939✔
148
  if (code != 0) {
2,666,939✔
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++) {
11,730,325✔
157
    SVnodeObj *pVnode = ppVnodes[v];
9,063,386✔
158
    disks[pVnode->diskPrimary] += 1;
9,063,386✔
159
  }
160

161
  int32_t minVal = INT_MAX;
2,666,939✔
162
  int32_t ndisk = tfsGetDisksAtLevel(pTfs, 0);
2,666,939✔
163
  diskId = 0;
2,666,939✔
164
  for (int32_t id = 0; id < ndisk; id++) {
5,559,872✔
165
    if (minVal > disks[id]) {
2,892,933✔
166
      minVal = disks[id];
2,715,678✔
167
      diskId = id;
2,715,678✔
168
    }
169
  }
170
  code = vmRegisterCreatingState(pMgmt, vgId, diskId);
2,666,939✔
171
  if (code != 0) {
2,666,939✔
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);
2,666,939✔
180
  if (code != 0) {
2,666,939✔
181
    goto _OVER;
×
182
  }
183

184
_OVER:
2,666,939✔
185

186
  for (int32_t i = 0; i < numOfVnodes; ++i) {
11,730,325✔
187
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
9,063,386✔
188
    vmReleaseVnode(pMgmt, ppVnodes[i]);
9,063,386✔
189
  }
190
  if (ppVnodes != NULL) {
2,666,939✔
191
    taosMemoryFree(ppVnodes);
2,666,939✔
192
  }
193

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

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

205
SVnodeObj *vmAcquireVnodeImpl(SVnodeMgmt *pMgmt, int32_t vgId, bool strict) {
2,147,483,647✔
206
  SVnodeObj *pVnode = NULL;
2,147,483,647✔
207

208
  (void)taosThreadRwlockRdlock(&pMgmt->hashLock);
2,147,483,647✔
209
  int32_t r = taosHashGetDup(pMgmt->runngingHash, &vgId, sizeof(int32_t), (void *)&pVnode);
2,147,483,647✔
210
  if (pVnode == NULL || strict && (pVnode->dropped || pVnode->failed)) {
2,147,483,647✔
211
    terrno = TSDB_CODE_VND_INVALID_VGROUP_ID;
13,902,716✔
212
    dDebug("vgId:%d, acquire vnode failed.", vgId);
13,887,856✔
213
    pVnode = NULL;
13,889,407✔
214
  } else {
215
    int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
2,147,483,647✔
216
    dTrace("vgId:%d, acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
2,147,483,647✔
217
  }
218
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
2,147,483,647✔
219

220
  return pVnode;
2,147,483,647✔
221
}
222

223
SVnodeObj *vmAcquireVnode(SVnodeMgmt *pMgmt, int32_t vgId) { return vmAcquireVnodeImpl(pMgmt, vgId, true); }
2,147,483,647✔
224

225
void vmReleaseVnode(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
2,147,483,647✔
226
  if (pVnode == NULL) return;
2,147,483,647✔
227

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

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

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

247
  return code;
3,725,897✔
248
}
249

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

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

268
  pClosedVnode->vgId = pVnode->vgId;
736,672✔
269
  pClosedVnode->dropped = pVnode->dropped;
736,672✔
270
  pClosedVnode->vgVersion = pVnode->vgVersion;
736,672✔
271
  pClosedVnode->diskPrimary = pVnode->diskPrimary;
736,672✔
272
  pClosedVnode->toVgId = pVnode->toVgId;
736,672✔
273
  pClosedVnode->mountId = pVnode->mountId;
736,672✔
274

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

289
  return code;
736,672✔
290
}
291

292
static void vmUnRegisterClosedState(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
3,725,897✔
293
  SVnodeObj *pOld = NULL;
3,725,897✔
294
  dInfo("vgId:%d, remove from closed hash", pVnode->vgId);
3,725,897✔
295
  int32_t r = taosHashGetDup(pMgmt->closedHash, &pVnode->vgId, sizeof(int32_t), (void *)&pOld);
3,725,897✔
296
  if (r != 0) {
3,725,897✔
297
    dError("vgId:%d, failed to get vnode from closedHash", pVnode->vgId);
×
298
  }
299
  if (pOld != NULL) {
3,725,897✔
300
    vmFreeVnodeObj(&pOld);
736,672✔
301
    dInfo("vgId:%d, remove from closedHash", pVnode->vgId);
736,672✔
302
    r = taosHashRemove(pMgmt->closedHash, &pVnode->vgId, sizeof(int32_t));
736,672✔
303
    if (r != 0) {
736,672✔
304
      if (r == TSDB_CODE_NOT_FOUND) {
×
305
        dWarn("vgId:%d, vnode not found in closedHash when unregistering", pVnode->vgId);
×
306
      } else {
307
        dError("vgId:%d, failed to remove vnode from hash when unregistering since %s", pVnode->vgId, tstrerror(r));
×
308
      }
309
    }
310
  }
311
}
3,725,897✔
312
#ifdef USE_MOUNT
313
int32_t vmAcquireMountTfs(SVnodeMgmt *pMgmt, int64_t mountId, const char *mountName, const char *mountPath,
1,940✔
314
                          STfs **ppTfs) {
315
  int32_t    code = 0, lino = 0;
1,940✔
316
  TdFilePtr  pFile = NULL;
1,940✔
317
  SArray    *pDisks = NULL;
1,940✔
318
  SMountTfs *pMountTfs = NULL;
1,940✔
319
  bool       unlock = false;
1,940✔
320

321
  pMountTfs = taosHashGet(pMgmt->mountTfsHash, &mountId, sizeof(mountId));
1,940✔
322
  if (pMountTfs && *(SMountTfs **)pMountTfs) {
1,940✔
323
    if (!(*ppTfs = (*(SMountTfs **)pMountTfs)->pTfs)) {
968✔
324
      TAOS_CHECK_EXIT(TSDB_CODE_INTERNAL_ERROR);
×
325
    }
326
    (void)atomic_add_fetch_32(&(*(SMountTfs **)pMountTfs)->nRef, 1);
968✔
327
    TAOS_RETURN(code);
968✔
328
  }
329
  if (!mountPath || mountPath[0] == 0 || mountId == 0) {
972✔
330
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
331
  }
332
  (void)(taosThreadMutexLock(&pMgmt->mutex));
972✔
333
  unlock = true;
972✔
334
  pMountTfs = taosHashGet(pMgmt->mountTfsHash, &mountId, sizeof(mountId));
972✔
335
  if (pMountTfs && *(SMountTfs **)pMountTfs) {
972✔
336
    if (!(*ppTfs = (*(SMountTfs **)pMountTfs)->pTfs)) {
729✔
337
      TAOS_CHECK_EXIT(TSDB_CODE_INTERNAL_ERROR);
×
338
    }
339
    (void)taosThreadMutexUnlock(&pMgmt->mutex);
729✔
340
    (void)atomic_add_fetch_32(&(*(SMountTfs **)pMountTfs)->nRef, 1);
729✔
341
    TAOS_RETURN(code);
729✔
342
  }
343

344
  TAOS_CHECK_EXIT(vmMountCheckRunning(mountName, mountPath, &pFile, 3));
243✔
345
  TAOS_CHECK_EXIT(vmGetMountDisks(pMgmt, mountPath, &pDisks));
243✔
346
  int32_t numOfDisks = taosArrayGetSize(pDisks);
243✔
347
  if (numOfDisks <= 0) {
243✔
348
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_JSON_FORMAT);
×
349
  }
350
  TSDB_CHECK_NULL((pMountTfs = taosMemoryCalloc(1, sizeof(SMountTfs))), code, lino, _exit, terrno);
243✔
351
  if (mountName) (void)snprintf(pMountTfs->name, sizeof(pMountTfs->name), "%s", mountName);
243✔
352
  if (mountPath) (void)snprintf(pMountTfs->path, sizeof(pMountTfs->path), "%s", mountPath);
243✔
353
  pMountTfs->pFile = pFile;
243✔
354
  atomic_store_32(&pMountTfs->nRef, 2);  // init and acquire
243✔
355
  TAOS_CHECK_EXIT(tfsOpen(TARRAY_GET_ELEM(pDisks, 0), numOfDisks, &pMountTfs->pTfs));
243✔
356
  TAOS_CHECK_EXIT(taosHashPut(pMgmt->mountTfsHash, &mountId, sizeof(mountId), &pMountTfs, POINTER_BYTES));
243✔
357
_exit:
243✔
358
  if (unlock) {
243✔
359
    (void)taosThreadMutexUnlock(&pMgmt->mutex);
243✔
360
  }
361
  taosArrayDestroy(pDisks);
243✔
362
  if (code != 0) {
243✔
363
    dError("mount:%" PRIi64 ",%s, failed at line %d to get mount tfs since %s", mountId, mountPath ? mountPath : "NULL",
×
364
           lino, tstrerror(code));
365
    if (pFile) {
×
366
      (void)taosUnLockFile(pFile);
×
367
      (void)taosCloseFile(&pFile);
×
368
    }
369
    if (pMountTfs) {
×
370
      tfsClose(pMountTfs->pTfs);
×
371
      taosMemoryFree(pMountTfs);
×
372
    }
373
    *ppTfs = NULL;
×
374
  } else {
375
    *ppTfs = pMountTfs->pTfs;
243✔
376
  }
377

378
  TAOS_RETURN(code);
243✔
379
}
380
#endif
381

382
bool vmReleaseMountTfs(SVnodeMgmt *pMgmt, int64_t mountId, int32_t minRef) {
1,940✔
383
#ifdef USE_MOUNT
384
  SMountTfs *pMountTfs = NULL;
1,940✔
385
  int32_t    nRef = INT32_MAX, code = 0;
1,940✔
386

387
  pMountTfs = taosHashGet(pMgmt->mountTfsHash, &mountId, sizeof(mountId));
1,940✔
388
  if (pMountTfs && *(SMountTfs **)pMountTfs) {
1,940✔
389
    if ((nRef = atomic_sub_fetch_32(&(*(SMountTfs **)pMountTfs)->nRef, 1)) <= minRef) {
1,940✔
390
      (void)(taosThreadMutexLock(&pMgmt->mutex));
242✔
391
      SMountTfs *pTmp = taosHashGet(pMgmt->mountTfsHash, &mountId, sizeof(mountId));
242✔
392
      if (pTmp && *(SMountTfs **)pTmp) {
242✔
393
        dInfo("mount:%" PRIi64 ", ref:%d, release mount tfs", mountId, nRef);
242✔
394
        tfsClose((*(SMountTfs **)pTmp)->pTfs);
242✔
395
        if ((*(SMountTfs **)pTmp)->pFile) {
242✔
396
          (void)taosUnLockFile((*(SMountTfs **)pTmp)->pFile);
242✔
397
          (void)taosCloseFile(&(*(SMountTfs **)pTmp)->pFile);
242✔
398
        }
399
        taosMemoryFree(*(SMountTfs **)pTmp);
242✔
400
        if ((code = taosHashRemove(pMgmt->mountTfsHash, &mountId, sizeof(mountId))) < 0) {
242✔
401
          dError("failed at line %d to remove mountId:%" PRIi64 " from mount tfs hash", __LINE__, mountId);
×
402
        }
403
      }
404
      (void)taosThreadMutexUnlock(&pMgmt->mutex);
242✔
405
      return true;
242✔
406
    }
407
  }
408
#endif
409
  return false;
1,698✔
410
}
411

412
int32_t vmOpenVnode(SVnodeMgmt *pMgmt, SWrapperCfg *pCfg, SVnode *pImpl) {
3,724,165✔
413
  SVnodeObj *pVnode = taosMemoryCalloc(1, sizeof(SVnodeObj));
3,724,165✔
414
  if (pVnode == NULL) {
3,724,855✔
415
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
416
    return -1;
×
417
  }
418

419
  pVnode->vgId = pCfg->vgId;
3,724,855✔
420
  pVnode->vgVersion = pCfg->vgVersion;
3,725,897✔
421
  pVnode->diskPrimary = pCfg->diskPrimary;
3,725,224✔
422
  pVnode->mountId = pCfg->mountId;
3,724,855✔
423
  pVnode->refCount = 0;
3,724,855✔
424
  pVnode->dropped = 0;
3,724,146✔
425
  pVnode->failed = 0;
3,724,143✔
426
  pVnode->path = taosStrdup(pCfg->path);
3,724,143✔
427
  pVnode->pImpl = pImpl;
3,725,897✔
428

429
  if (pVnode->path == NULL) {
3,725,528✔
430
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
431
    taosMemoryFree(pVnode);
×
432
    return -1;
×
433
  }
434

435
  if (pImpl) {
3,723,832✔
436
    if (vmAllocQueue(pMgmt, pVnode) != 0) {
3,723,832✔
437
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
438
      taosMemoryFree(pVnode->path);
×
439
      taosMemoryFree(pVnode);
×
440
      return -1;
×
441
    }
442
  } else {
443
    pVnode->failed = 1;
×
444
  }
445

446
  (void)taosThreadRwlockWrlock(&pMgmt->hashLock);
3,725,897✔
447
  int32_t code = vmRegisterRunningState(pMgmt, pVnode);
3,725,897✔
448
  vmUnRegisterClosedState(pMgmt, pVnode);
3,725,897✔
449
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
3,725,897✔
450

451
  TAOS_RETURN(code);
3,725,897✔
452
}
453

454
void vmCloseVnode(SVnodeMgmt *pMgmt, SVnodeObj *pVnode, bool commitAndRemoveWal, bool keepClosed) {
3,724,979✔
455
  vDebug("vgId:%d, start to close vnode", pVnode->vgId);
3,724,979✔
456
  char path[TSDB_FILENAME_LEN] = {0};
3,725,149✔
457
  bool atExit = true;
3,725,897✔
458

459
  if (pVnode->pImpl && vnodeIsLeader(pVnode->pImpl)) {
3,725,897✔
460
    vnodeProposeCommitOnNeed(pVnode->pImpl, atExit);
2,877,305✔
461
  }
462

463
  (void)taosThreadRwlockWrlock(&pMgmt->hashLock);
3,724,103✔
464
  vmUnRegisterRunningState(pMgmt, pVnode->vgId);
3,725,897✔
465
  if (keepClosed) {
3,725,897✔
466
    if (vmRegisterClosedState(pMgmt, pVnode) != 0) {
736,672✔
467
      (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
×
468
      return;
×
469
    };
470
  }
471
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
3,725,897✔
472

473
  vmReleaseVnode(pMgmt, pVnode);
3,725,897✔
474

475
  if (pVnode->failed) {
3,725,897✔
476
    goto _closed;
×
477
  }
478
  dInfo("vgId:%d, pre close", pVnode->vgId);
3,725,897✔
479
  vnodePreClose(pVnode->pImpl);
3,725,897✔
480

481
  dInfo("vgId:%d, wait for vnode ref become 0", pVnode->vgId);
3,725,897✔
482
  while (pVnode->refCount > 0) taosMsleep(10);
3,725,897✔
483

484
  dInfo("vgId:%d, wait for vnode write queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pWriteW.queue,
3,725,897✔
485
        taosQueueGetThreadId(pVnode->pWriteW.queue));
486
  tMultiWorkerCleanup(&pVnode->pWriteW);
3,725,897✔
487

488
  dInfo("vgId:%d, wait for vnode sync queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pSyncW.queue,
3,725,897✔
489
        taosQueueGetThreadId(pVnode->pSyncW.queue));
490
  tMultiWorkerCleanup(&pVnode->pSyncW);
3,725,897✔
491

492
  dInfo("vgId:%d, wait for vnode sync rd queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pSyncRdW.queue,
3,725,897✔
493
        taosQueueGetThreadId(pVnode->pSyncRdW.queue));
494
  tMultiWorkerCleanup(&pVnode->pSyncRdW);
3,725,897✔
495

496
  dInfo("vgId:%d, wait for vnode apply queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pApplyW.queue,
3,725,897✔
497
        taosQueueGetThreadId(pVnode->pApplyW.queue));
498
  tMultiWorkerCleanup(&pVnode->pApplyW);
3,725,897✔
499

500
  dInfo("vgId:%d, wait for vnode fetch queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pFetchQ,
3,725,897✔
501
        taosQueueGetThreadId(pVnode->pFetchQ));
502
  while (!taosQueueEmpty(pVnode->pFetchQ)) taosMsleep(10);
3,725,897✔
503

504
  dInfo("vgId:%d, wait for vnode query queue:%p is empty", pVnode->vgId, pVnode->pQueryQ);
3,725,897✔
505
  while (!taosQueueEmpty(pVnode->pQueryQ)) taosMsleep(10);
3,727,499✔
506

507
  dInfo("vgId:%d, wait for vnode stream reader queue:%p is empty", pVnode->vgId, pVnode->pStreamReaderQ);
3,725,897✔
508
  while (!taosQueueEmpty(pVnode->pStreamReaderQ)) taosMsleep(10);
3,725,897✔
509

510
  dInfo("vgId:%d, all vnode queues is empty", pVnode->vgId);
3,725,897✔
511

512
  dInfo("vgId:%d, post close", pVnode->vgId);
3,725,897✔
513
  vnodePostClose(pVnode->pImpl);
3,725,897✔
514

515
  vmFreeQueue(pMgmt, pVnode);
3,725,565✔
516

517
  if (commitAndRemoveWal) {
3,725,897✔
518
    dInfo("vgId:%d, commit data for vnode split", pVnode->vgId);
24,694✔
519
    if (vnodeSyncCommit(pVnode->pImpl) != 0) {
24,694✔
520
      dError("vgId:%d, failed to commit data", pVnode->vgId);
×
521
    }
522
    if (vnodeBegin(pVnode->pImpl) != 0) {
24,694✔
523
      dError("vgId:%d, failed to begin", pVnode->vgId);
×
524
    }
525
    dInfo("vgId:%d, commit data finished", pVnode->vgId);
24,694✔
526
  }
527

528
  int32_t nodeId = vnodeNodeId(pVnode->pImpl);
3,725,897✔
529
  vnodeClose(pVnode->pImpl);
3,725,897✔
530
  pVnode->pImpl = NULL;
3,724,922✔
531

532
_closed:
3,723,577✔
533
  dInfo("vgId:%d, vnode is closed", pVnode->vgId);
3,724,156✔
534

535
  if (commitAndRemoveWal) {
3,725,897✔
536
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d%swal", TD_DIRSEP, pVnode->vgId, TD_DIRSEP);
24,694✔
537
    dInfo("vgId:%d, remove all wals, path:%s", pVnode->vgId, path);
24,694✔
538
    if (tfsRmdir(pMgmt->pTfs, path) != 0) {
24,694✔
539
      dTrace("vgId:%d, failed to remove wals, path:%s", pVnode->vgId, path);
×
540
    }
541
    if (tfsMkdir(pMgmt->pTfs, path) != 0) {
24,694✔
542
      dTrace("vgId:%d, failed to create wals, path:%s", pVnode->vgId, path);
×
543
    }
544
  }
545

546
  if (pVnode->dropped) {
3,725,897✔
547
    dInfo("vgId:%d, vnode is destroyed, dropped:%d", pVnode->vgId, pVnode->dropped);
1,426,103✔
548
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pVnode->vgId);
1,426,103✔
549
    vnodeDestroy(pVnode->vgId, path, pMgmt->pTfs, nodeId);
1,426,103✔
550
  }
551
  if (pVnode->mountId && vmReleaseMountTfs(pMgmt, pVnode->mountId, pVnode->dropped ? 1 : 0)) {
3,725,897✔
552
    if (vmWriteMountListToFile(pMgmt) != 0) {
242✔
553
      dError("vgId:%d, failed at line %d to write mount list since %s", pVnode->vgId, __LINE__, terrstr());
×
554
    }
555
  }
556

557
  vmFreeVnodeObj(&pVnode);
3,725,897✔
558
}
559

560
void vmCloseFailedVnode(SVnodeMgmt *pMgmt, int32_t vgId) {
×
561
  int32_t r = 0;
×
562
  r = taosThreadRwlockWrlock(&pMgmt->hashLock);
×
563
  if (r != 0) {
×
564
    dError("vgId:%d, failed to lock since %s", vgId, tstrerror(r));
×
565
  }
566
  if (r == 0) {
×
567
    vmUnRegisterRunningState(pMgmt, vgId);
×
568
  }
569
  r = taosThreadRwlockUnlock(&pMgmt->hashLock);
×
570
  if (r != 0) {
×
571
    dError("vgId:%d, failed to unlock since %s", vgId, tstrerror(r));
×
572
  }
573
}
×
574

575
static int32_t vmRestoreVgroupId(SWrapperCfg *pCfg, STfs *pTfs) {
×
576
  int32_t srcVgId = pCfg->vgId;
×
577
  int32_t dstVgId = pCfg->toVgId;
×
578
  if (dstVgId == 0) return 0;
×
579

580
  char srcPath[TSDB_FILENAME_LEN];
×
581
  char dstPath[TSDB_FILENAME_LEN];
×
582

583
  snprintf(srcPath, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, srcVgId);
×
584
  snprintf(dstPath, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, dstVgId);
×
585

586
  int32_t diskPrimary = pCfg->diskPrimary;
×
587
  int32_t vgId = vnodeRestoreVgroupId(srcPath, dstPath, srcVgId, dstVgId, diskPrimary, pTfs);
×
588
  if (vgId <= 0) {
×
589
    dError("vgId:%d, failed to restore vgroup id. srcPath: %s", pCfg->vgId, srcPath);
×
590
    return -1;
×
591
  }
592

593
  pCfg->vgId = vgId;
×
594
  pCfg->toVgId = 0;
×
595
  return 0;
×
596
}
597

598
static void *vmOpenVnodeInThread(void *param) {
294,816✔
599
  SVnodeThread *pThread = param;
294,816✔
600
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
294,816✔
601
  char          path[TSDB_FILENAME_LEN];
294,750✔
602

603
  dInfo("thread:%d, start to open or destroy %d vnodes", pThread->threadIndex, pThread->vnodeNum);
294,816✔
604
  setThreadName("open-vnodes");
294,816✔
605

606
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
591,436✔
607
    SWrapperCfg *pCfg = &pThread->pCfgs[v];
296,620✔
608
    if (pCfg->dropped) {
296,620✔
609
      char stepDesc[TSDB_STEP_DESC_LEN] = {0};
×
610
      snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to destroy, %d of %d have been dropped", pCfg->vgId,
×
611
               pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
612
      tmsgReportStartup("vnode-destroy", stepDesc);
×
613

614
      snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pCfg->vgId);
×
615
      vnodeDestroy(pCfg->vgId, path, pMgmt->pTfs, 0);
×
616
      pThread->updateVnodesList = true;
×
617
      pThread->dropped++;
×
618
      (void)atomic_add_fetch_32(&pMgmt->state.dropVnodes, 1);
×
619
      continue;
×
620
    }
621

622
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
296,620✔
623
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been opened", pCfg->vgId,
296,620✔
624
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
625
    tmsgReportStartup("vnode-open", stepDesc);
296,620✔
626

627
    if (pCfg->toVgId) {
296,620✔
628
      if (vmRestoreVgroupId(pCfg, pMgmt->pTfs) != 0) {
×
629
        dError("vgId:%d, failed to restore vgroup id by thread:%d", pCfg->vgId, pThread->threadIndex);
×
630
        pThread->failed++;
×
631
        continue;
×
632
      }
633
      pThread->updateVnodesList = true;
×
634
    }
635

636
    int32_t diskPrimary = pCfg->mountId == 0 ? pCfg->diskPrimary : 0;
296,620✔
637
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pCfg->vgId);
296,620✔
638

639
    STfs *pMountTfs = NULL;
296,620✔
640
#ifdef USE_MOUNT
641
    bool releaseTfs = false;
296,620✔
642
    if (pCfg->mountId) {
296,620✔
643
      if (vmAcquireMountTfs(pMgmt, pCfg->mountId, NULL, NULL, &pMountTfs) != 0) {
968✔
644
        dError("vgId:%d, failed to get mount tfs by thread:%d", pCfg->vgId, pThread->threadIndex);
×
645
        pThread->failed++;
×
646
        continue;
×
647
      }
648
      releaseTfs = true;
968✔
649
    }
650
#endif
651

652
    SVnode *pImpl = vnodeOpen(path, diskPrimary, pMgmt->pTfs, pMountTfs, pMgmt->msgCb, false);
296,620✔
653

654
    if (pImpl == NULL) {
296,620✔
655
      dError("vgId:%d, failed to open vnode by thread:%d since %s", pCfg->vgId, pThread->threadIndex, terrstr());
×
656
      if (terrno != TSDB_CODE_NEED_RETRY) {
×
657
        pThread->failed++;
×
658
#ifdef USE_MOUNT
659
        if (releaseTfs) vmReleaseMountTfs(pMgmt, pCfg->mountId, 0);
×
660
#endif
661
        continue;
×
662
      }
663
    }
664

665
    if (pImpl != NULL) {
296,620✔
666
      if (vmOpenVnode(pMgmt, pCfg, pImpl) != 0) {
296,620✔
667
        dError("vgId:%d, failed to open vnode by thread:%d", pCfg->vgId, pThread->threadIndex);
×
668
        pThread->failed++;
×
669
#ifdef USE_MOUNT
670
        if (releaseTfs) vmReleaseMountTfs(pMgmt, pCfg->mountId, 0);
×
671
#endif
672
        continue;
×
673
      }
674
    }
675

676
    dInfo("vgId:%d, is opened by thread:%d", pCfg->vgId, pThread->threadIndex);
296,620✔
677
    pThread->opened++;
296,620✔
678
    (void)atomic_add_fetch_32(&pMgmt->state.openVnodes, 1);
296,620✔
679
  }
680

681
  dInfo("thread:%d, numOfVnodes:%d, opened:%d dropped:%d failed:%d", pThread->threadIndex, pThread->vnodeNum,
294,816✔
682
        pThread->opened, pThread->dropped, pThread->failed);
683
  return NULL;
294,816✔
684
}
685

686
#ifdef USE_MOUNT
687
static int32_t vmOpenMountTfs(SVnodeMgmt *pMgmt) {
547,663✔
688
  int32_t    code = 0, lino = 0;
547,663✔
689
  int32_t    numOfMounts = 0;
547,663✔
690
  SMountCfg *pMountCfgs = NULL;
547,663✔
691
  SArray    *pDisks = NULL;
547,663✔
692
  TdFilePtr  pFile = NULL;
547,663✔
693
  SMountTfs *pMountTfs = NULL;
547,663✔
694

695
  TAOS_CHECK_EXIT(vmGetMountListFromFile(pMgmt, &pMountCfgs, &numOfMounts));
547,663✔
696
  for (int32_t i = 0; i < numOfMounts; ++i) {
547,905✔
697
    SMountCfg *pCfg = &pMountCfgs[i];
242✔
698
    if (taosHashGet(pMgmt->mountTfsHash, &pCfg->mountId, sizeof(pCfg->mountId))) {
242✔
699
      TAOS_CHECK_EXIT(TSDB_CODE_INTERNAL_ERROR);
×
700
    }
701
    TAOS_CHECK_EXIT(vmMountCheckRunning(pCfg->name, pCfg->path, &pFile, 3));
242✔
702
    TAOS_CHECK_EXIT(vmGetMountDisks(pMgmt, pCfg->path, &pDisks));
242✔
703
    int32_t nDisks = taosArrayGetSize(pDisks);
242✔
704
    if (nDisks < 1 || nDisks > TFS_MAX_DISKS) {
242✔
705
      dError("mount:%s, %" PRIi64 ", %s, invalid number of disks:%d, expect 1 to %d", pCfg->name, pCfg->mountId,
×
706
             pCfg->path, nDisks, TFS_MAX_DISKS);
707
      TAOS_CHECK_EXIT(TSDB_CODE_INVALID_JSON_FORMAT);
×
708
    }
709

710
    TSDB_CHECK_NULL((pMountTfs = taosMemoryCalloc(1, sizeof(SMountTfs))), code, lino, _exit, terrno);
242✔
711
    TAOS_CHECK_EXIT(tfsOpen(TARRAY_GET_ELEM(pDisks, 0), TARRAY_SIZE(pDisks), &pMountTfs->pTfs));
242✔
712
    (void)snprintf(pMountTfs->name, sizeof(pMountTfs->name), "%s", pCfg->name);
242✔
713
    (void)snprintf(pMountTfs->path, sizeof(pMountTfs->path), "%s", pCfg->path);
242✔
714
    pMountTfs->pFile = pFile;
242✔
715
    pMountTfs->nRef = 1;
242✔
716
    TAOS_CHECK_EXIT(taosHashPut(pMgmt->mountTfsHash, &pCfg->mountId, sizeof(pCfg->mountId), &pMountTfs, POINTER_BYTES));
242✔
717
    taosArrayDestroy(pDisks);
242✔
718
    pDisks = NULL;
242✔
719
    pMountTfs = NULL;
242✔
720
    pFile = NULL;
242✔
721
  }
722
_exit:
547,663✔
723
  if (code != 0) {
547,663✔
724
    dError("failed to open mount tfs at line %d since %s", lino, tstrerror(code));
×
725
    if (pFile) {
×
726
      (void)taosUnLockFile(pFile);
×
727
      (void)taosCloseFile(&pFile);
×
728
    }
729
    if (pMountTfs) {
×
730
      tfsClose(pMountTfs->pTfs);
×
731
      taosMemoryFree(pMountTfs);
×
732
    }
733
    taosArrayDestroy(pDisks);
×
734
  }
735
  taosMemoryFree(pMountCfgs);
547,663✔
736
  TAOS_RETURN(code);
547,663✔
737
}
738
#endif
739
static int32_t vmOpenVnodes(SVnodeMgmt *pMgmt) {
547,663✔
740
  pMgmt->runngingHash =
547,663✔
741
      taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
547,663✔
742
  if (pMgmt->runngingHash == NULL) {
547,663✔
743
    dError("failed to init vnode hash since %s", terrstr());
×
744
    return TSDB_CODE_OUT_OF_MEMORY;
×
745
  }
746

747
  pMgmt->closedHash =
547,663✔
748
      taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
547,663✔
749
  if (pMgmt->closedHash == NULL) {
547,663✔
750
    dError("failed to init vnode closed hash since %s", terrstr());
×
751
    return TSDB_CODE_OUT_OF_MEMORY;
×
752
  }
753

754
  pMgmt->creatingHash =
547,663✔
755
      taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
547,663✔
756
  if (pMgmt->creatingHash == NULL) {
547,663✔
757
    dError("failed to init vnode creatingHash hash since %s", terrstr());
×
758
    return TSDB_CODE_OUT_OF_MEMORY;
×
759
  }
760

761
  SWrapperCfg *pCfgs = NULL;
547,663✔
762
  int32_t      numOfVnodes = 0;
547,663✔
763
  int32_t      code = 0;
547,663✔
764
  if ((code = vmGetVnodeListFromFile(pMgmt, &pCfgs, &numOfVnodes)) != 0) {
547,663✔
765
    dInfo("failed to get vnode list from disk since %s", tstrerror(code));
×
766
    return code;
×
767
  }
768

769
  pMgmt->state.totalVnodes = numOfVnodes;
547,663✔
770

771
  int32_t threadNum = tsNumOfCores / 2;
547,663✔
772
  if (threadNum < 1) threadNum = 1;
547,663✔
773
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
547,663✔
774

775
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
547,663✔
776
  if (threads == NULL) {
547,663✔
777
    dError("failed to allocate memory for threads since %s", terrstr());
×
778
    taosMemoryFree(pCfgs);
×
779
    return terrno;
×
780
  }
781

782
  for (int32_t t = 0; t < threadNum; ++t) {
11,500,923✔
783
    threads[t].threadIndex = t;
10,953,260✔
784
    threads[t].pMgmt = pMgmt;
10,953,260✔
785
    threads[t].pCfgs = taosMemoryCalloc(vnodesPerThread, sizeof(SWrapperCfg));
10,953,260✔
786
  }
787

788
  for (int32_t v = 0; v < numOfVnodes; ++v) {
844,283✔
789
    int32_t       t = v % threadNum;
296,620✔
790
    SVnodeThread *pThread = &threads[t];
296,620✔
791
    pThread->pCfgs[pThread->vnodeNum++] = pCfgs[v];
296,620✔
792
  }
793

794
  dInfo("open %d vnodes with %d threads", numOfVnodes, threadNum);
547,663✔
795

796
  for (int32_t t = 0; t < threadNum; ++t) {
11,500,923✔
797
    SVnodeThread *pThread = &threads[t];
10,953,260✔
798
    if (pThread->vnodeNum == 0) continue;
10,953,260✔
799

800
    TdThreadAttr thAttr;
294,750✔
801
    (void)taosThreadAttrInit(&thAttr);
294,816✔
802
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
294,816✔
803
#ifdef TD_COMPACT_OS
804
    (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
805
#endif
806
    if (taosThreadCreate(&pThread->thread, &thAttr, vmOpenVnodeInThread, pThread) != 0) {
294,816✔
807
      dError("thread:%d, failed to create thread to open vnode, reason:%s", pThread->threadIndex, strerror(ERRNO));
×
808
    }
809

810
    (void)taosThreadAttrDestroy(&thAttr);
294,816✔
811
  }
812

813
  bool updateVnodesList = false;
547,663✔
814

815
  for (int32_t t = 0; t < threadNum; ++t) {
11,500,923✔
816
    SVnodeThread *pThread = &threads[t];
10,953,260✔
817
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
10,953,260✔
818
      (void)taosThreadJoin(pThread->thread, NULL);
294,816✔
819
      taosThreadClear(&pThread->thread);
294,816✔
820
    }
821
    taosMemoryFree(pThread->pCfgs);
10,953,260✔
822
    if (pThread->updateVnodesList) updateVnodesList = true;
10,953,260✔
823
  }
824
  taosMemoryFree(threads);
547,663✔
825
  taosMemoryFree(pCfgs);
547,663✔
826

827
  if ((pMgmt->state.openVnodes + pMgmt->state.dropVnodes) != pMgmt->state.totalVnodes) {
547,663✔
828
    dError("there are total vnodes:%d, opened:%d", pMgmt->state.totalVnodes, pMgmt->state.openVnodes);
×
829
    return terrno = TSDB_CODE_VND_INIT_FAILED;
×
830
  }
831

832
  if (updateVnodesList && (code = vmWriteVnodeListToFile(pMgmt)) != 0) {
547,663✔
833
    dError("failed to write vnode list since %s", tstrerror(code));
×
834
    return code;
×
835
  }
836

837
#ifdef USE_MOUNT
838
  bool  updateMountList = false;
547,663✔
839
  void *pIter = NULL;
547,663✔
840
  while ((pIter = taosHashIterate(pMgmt->mountTfsHash, pIter))) {
547,905✔
841
    SMountTfs *pMountTfs = *(SMountTfs **)pIter;
242✔
842
    if (pMountTfs && atomic_load_32(&pMountTfs->nRef) <= 1) {
242✔
843
      size_t  keyLen = 0;
×
844
      int64_t mountId = *(int64_t *)taosHashGetKey(pIter, &keyLen);
×
845
      dInfo("mount:%s, %s, %" PRIi64 ", ref:%d, remove unused mount tfs", pMountTfs->name, pMountTfs->path, mountId,
×
846
            atomic_load_32(&pMountTfs->nRef));
847
      if (pMountTfs->pFile) {
×
848
        (void)taosUnLockFile(pMountTfs->pFile);
×
849
        (void)taosCloseFile(&pMountTfs->pFile);
×
850
      }
851
      tfsClose(pMountTfs->pTfs);
×
852
      taosMemoryFree(pMountTfs);
×
853
      if ((code = taosHashRemove(pMgmt->mountTfsHash, &mountId, keyLen)) != 0) {
×
854
        dWarn("failed at line %d to remove mount:%s, %s, %" PRIi64 " from mount tfs hash since %s", __LINE__,
×
855
              pMountTfs->name, pMountTfs->path, mountId, tstrerror(code));
856
      }
857
      updateMountList = true;
×
858
    }
859
  }
860
  if (updateMountList && (code = vmWriteMountListToFile(pMgmt)) != 0) {
547,663✔
861
    dError("failed to write mount list at line %d since %s", __LINE__, tstrerror(code));
×
862
    return code;
×
863
  }
864
#endif
865

866
  dInfo("successfully opened %d vnodes", pMgmt->state.totalVnodes);
547,663✔
867
  return 0;
547,663✔
868
}
869

870
static void *vmCloseVnodeInThread(void *param) {
1,515,092✔
871
  SVnodeThread *pThread = param;
1,515,092✔
872
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
1,515,092✔
873

874
  dInfo("thread:%d, start to close %d vnodes", pThread->threadIndex, pThread->vnodeNum);
1,515,092✔
875
  setThreadName("close-vnodes");
1,515,092✔
876

877
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
3,053,720✔
878
    SVnodeObj *pVnode = pThread->ppVnodes[v];
1,538,428✔
879

880
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
1,538,428✔
881
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to close, %d of %d have been closed", pVnode->vgId,
1,538,428✔
882
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
883
    tmsgReportStartup("vnode-close", stepDesc);
1,538,428✔
884

885
    vmCloseVnode(pMgmt, pVnode, false, false);
1,538,258✔
886
  }
887

888
  dInfo("thread:%d, numOfVnodes:%d is closed", pThread->threadIndex, pThread->vnodeNum);
1,515,292✔
889
  return NULL;
1,515,292✔
890
}
891

892
static void vmCloseVnodes(SVnodeMgmt *pMgmt) {
547,663✔
893
  int32_t code = 0;
547,663✔
894
  dInfo("start to close all vnodes");
547,663✔
895
  tSingleWorkerCleanup(&pMgmt->mgmtWorker);
547,663✔
896
  dInfo("vnodes mgmt worker is stopped");
547,663✔
897
  tSingleWorkerCleanup(&pMgmt->mgmtMultiWorker);
547,663✔
898
  dInfo("vnodes multiple mgmt worker is stopped");
547,663✔
899

900
  int32_t     numOfVnodes = 0;
547,663✔
901
  SVnodeObj **ppVnodes = NULL;
547,663✔
902
  code = vmGetVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes);
547,663✔
903
  if (code != 0) {
547,663✔
904
    dError("failed to get vnode list since %s", tstrerror(code));
×
905
    return;
×
906
  }
907

908
  int32_t threadNum = tsNumOfCores / 2;
547,663✔
909
  if (threadNum < 1) threadNum = 1;
547,663✔
910
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
547,663✔
911

912
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
547,663✔
913
  for (int32_t t = 0; t < threadNum; ++t) {
11,500,923✔
914
    threads[t].threadIndex = t;
10,953,260✔
915
    threads[t].pMgmt = pMgmt;
10,953,260✔
916
    threads[t].ppVnodes = taosMemoryCalloc(vnodesPerThread, sizeof(SVnode *));
10,953,260✔
917
  }
918

919
  for (int32_t v = 0; v < numOfVnodes; ++v) {
2,086,091✔
920
    int32_t       t = v % threadNum;
1,538,428✔
921
    SVnodeThread *pThread = &threads[t];
1,538,428✔
922
    if (pThread->ppVnodes != NULL && ppVnodes != NULL) {
1,538,428✔
923
      pThread->ppVnodes[pThread->vnodeNum++] = ppVnodes[v];
1,538,428✔
924
    }
925
  }
926

927
  pMgmt->state.openVnodes = 0;
547,663✔
928
  dInfo("close %d vnodes with %d threads", numOfVnodes, threadNum);
547,663✔
929

930
  for (int32_t t = 0; t < threadNum; ++t) {
11,500,923✔
931
    SVnodeThread *pThread = &threads[t];
10,953,260✔
932
    if (pThread->vnodeNum == 0) continue;
10,953,260✔
933

934
    TdThreadAttr thAttr;
1,511,831✔
935
    (void)taosThreadAttrInit(&thAttr);
1,515,292✔
936
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
1,515,292✔
937
#ifdef TD_COMPACT_OS
938
    (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
939
#endif
940
    if (taosThreadCreate(&pThread->thread, &thAttr, vmCloseVnodeInThread, pThread) != 0) {
1,515,292✔
941
      dError("thread:%d, failed to create thread to close vnode since %s", pThread->threadIndex, strerror(ERRNO));
×
942
    }
943

944
    (void)taosThreadAttrDestroy(&thAttr);
1,515,292✔
945
  }
946

947
  for (int32_t t = 0; t < threadNum; ++t) {
11,500,923✔
948
    SVnodeThread *pThread = &threads[t];
10,953,260✔
949
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
10,953,260✔
950
      (void)taosThreadJoin(pThread->thread, NULL);
1,515,292✔
951
      taosThreadClear(&pThread->thread);
1,515,292✔
952
    }
953
    taosMemoryFree(pThread->ppVnodes);
10,953,260✔
954
  }
955
  taosMemoryFree(threads);
547,663✔
956

957
  if (ppVnodes != NULL) {
547,663✔
958
    taosMemoryFree(ppVnodes);
547,663✔
959
  }
960

961
  if (pMgmt->runngingHash != NULL) {
547,663✔
962
    taosHashCleanup(pMgmt->runngingHash);
547,663✔
963
    pMgmt->runngingHash = NULL;
547,663✔
964
  }
965

966
  void *pIter = taosHashIterate(pMgmt->closedHash, NULL);
547,663✔
967
  while (pIter) {
547,663✔
968
    SVnodeObj **ppVnode = pIter;
×
969
    vmFreeVnodeObj(ppVnode);
×
970
    pIter = taosHashIterate(pMgmt->closedHash, pIter);
×
971
  }
972

973
  if (pMgmt->closedHash != NULL) {
547,663✔
974
    taosHashCleanup(pMgmt->closedHash);
547,663✔
975
    pMgmt->closedHash = NULL;
547,663✔
976
  }
977

978
  pIter = taosHashIterate(pMgmt->creatingHash, NULL);
547,663✔
979
  while (pIter) {
547,663✔
980
    SVnodeObj **ppVnode = pIter;
×
981
    vmFreeVnodeObj(ppVnode);
×
982
    pIter = taosHashIterate(pMgmt->creatingHash, pIter);
×
983
  }
984

985
  if (pMgmt->creatingHash != NULL) {
547,663✔
986
    taosHashCleanup(pMgmt->creatingHash);
547,663✔
987
    pMgmt->creatingHash = NULL;
547,663✔
988
  }
989

990
#ifdef USE_MOUNT
991
  pIter = NULL;
547,663✔
992
  while ((pIter = taosHashIterate(pMgmt->mountTfsHash, pIter))) {
547,906✔
993
    SMountTfs *mountTfs = *(SMountTfs **)pIter;
243✔
994
    if (mountTfs->pFile) {
243✔
995
      (void)taosUnLockFile(mountTfs->pFile);
243✔
996
      (void)taosCloseFile(&mountTfs->pFile);
243✔
997
    }
998
    tfsClose(mountTfs->pTfs);
243✔
999
    taosMemoryFree(mountTfs);
243✔
1000
  }
1001
  taosHashCleanup(pMgmt->mountTfsHash);
547,663✔
1002
  pMgmt->mountTfsHash = NULL;
547,663✔
1003
#endif
1004

1005
  dInfo("total vnodes:%d are all closed", numOfVnodes);
547,663✔
1006
}
1007

1008
static void vmCleanup(SVnodeMgmt *pMgmt) {
547,663✔
1009
  vmCloseVnodes(pMgmt);
547,663✔
1010
  vmStopWorker(pMgmt);
547,663✔
1011
  vnodeCleanup();
547,663✔
1012
  (void)taosThreadRwlockDestroy(&pMgmt->hashLock);
547,663✔
1013
  (void)taosThreadMutexDestroy(&pMgmt->mutex);
547,663✔
1014
  taosMemoryFree(pMgmt);
547,663✔
1015
}
547,663✔
1016

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

1027
  if (ppVnodes != NULL) {
1,497,732✔
1028
    for (int32_t i = 0; i < numOfVnodes; ++i) {
6,048,296✔
1029
      SVnodeObj *pVnode = ppVnodes[i];
4,550,564✔
1030
      if (!pVnode->failed) {
4,550,564✔
1031
        vnodeSyncCheckTimeout(pVnode->pImpl);
4,550,564✔
1032
      }
1033
      vmReleaseVnode(pMgmt, pVnode);
4,550,564✔
1034
    }
1035
    taosMemoryFree(ppVnodes);
1,497,732✔
1036
  }
1037
}
1038

1039
static void *vmThreadFp(void *param) {
547,663✔
1040
  SVnodeMgmt *pMgmt = param;
547,663✔
1041
  int64_t     lastTime = 0;
547,663✔
1042
  setThreadName("vnode-timer");
547,663✔
1043

1044
  while (1) {
521,613,028✔
1045
    lastTime++;
522,160,691✔
1046
    taosMsleep(100);
522,160,691✔
1047
    if (pMgmt->stop) break;
522,160,691✔
1048
    if (lastTime % 10 != 0) continue;
521,613,028✔
1049

1050
    int64_t sec = lastTime / 10;
51,925,876✔
1051
    if (sec % (VNODE_TIMEOUT_SEC / 2) == 0) {
51,925,876✔
1052
      vmCheckSyncTimeout(pMgmt);
1,497,732✔
1053
    }
1054
  }
1055

1056
  return NULL;
547,663✔
1057
}
1058

1059
static int32_t vmInitTimer(SVnodeMgmt *pMgmt) {
547,663✔
1060
  int32_t      code = 0;
547,663✔
1061
  TdThreadAttr thAttr;
545,949✔
1062
  (void)taosThreadAttrInit(&thAttr);
547,663✔
1063
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
547,663✔
1064
#ifdef TD_COMPACT_OS
1065
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
1066
#endif
1067
  if (taosThreadCreate(&pMgmt->thread, &thAttr, vmThreadFp, pMgmt) != 0) {
547,663✔
1068
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
1069
    dError("failed to create vnode timer thread since %s", tstrerror(code));
×
1070
    return code;
×
1071
  }
1072

1073
  (void)taosThreadAttrDestroy(&thAttr);
547,663✔
1074
  return 0;
547,663✔
1075
}
1076

1077
static void vmCleanupTimer(SVnodeMgmt *pMgmt) {
547,663✔
1078
  pMgmt->stop = true;
547,663✔
1079
  if (taosCheckPthreadValid(pMgmt->thread)) {
547,663✔
1080
    (void)taosThreadJoin(pMgmt->thread, NULL);
547,663✔
1081
    taosThreadClear(&pMgmt->thread);
547,663✔
1082
  }
1083
}
547,663✔
1084

1085
static int32_t vmInit(SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) {
547,663✔
1086
  int32_t code = -1;
547,663✔
1087

1088
  SVnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SVnodeMgmt));
547,663✔
1089
  if (pMgmt == NULL) {
547,663✔
1090
    code = terrno;
×
1091
    goto _OVER;
×
1092
  }
1093

1094
  pMgmt->pData = pInput->pData;
547,663✔
1095
  pMgmt->path = pInput->path;
547,663✔
1096
  pMgmt->name = pInput->name;
547,663✔
1097
  pMgmt->msgCb = pInput->msgCb;
547,663✔
1098
  pMgmt->msgCb.putToQueueFp = (PutToQueueFp)vmPutRpcMsgToQueue;
547,663✔
1099
  pMgmt->msgCb.qsizeFp = (GetQueueSizeFp)vmGetQueueSize;
547,663✔
1100
  pMgmt->msgCb.mgmt = pMgmt;
547,663✔
1101

1102
  code = taosThreadRwlockInit(&pMgmt->hashLock, NULL);
547,663✔
1103
  if (code != 0) {
547,663✔
1104
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
1105
    goto _OVER;
×
1106
  }
1107

1108
  code = taosThreadMutexInit(&pMgmt->mutex, NULL);
547,663✔
1109
  if (code != 0) {
547,663✔
1110
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
1111
    goto _OVER;
×
1112
  }
1113

1114
  pMgmt->pTfs = pInput->pTfs;
547,663✔
1115
  if (pMgmt->pTfs == NULL) {
547,663✔
1116
    dError("tfs is null.");
×
1117
    goto _OVER;
×
1118
  }
1119
#ifdef USE_MOUNT
1120
  if (!(pMgmt->mountTfsHash =
547,663✔
1121
            taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK))) {
547,663✔
1122
    dError("failed to init mountTfsHash since %s", terrstr());
×
1123
    return TSDB_CODE_OUT_OF_MEMORY;
×
1124
  }
1125
  if ((code = vmOpenMountTfs(pMgmt)) != 0) {
547,663✔
1126
    goto _OVER;
×
1127
  }
1128
#endif
1129
  tmsgReportStartup("vnode-tfs", "initialized");
547,663✔
1130
  if ((code = walInit(pInput->stopDnodeFp)) != 0) {
547,663✔
1131
    dError("failed to init wal since %s", tstrerror(code));
×
1132
    goto _OVER;
×
1133
  }
1134

1135
  tmsgReportStartup("vnode-wal", "initialized");
547,663✔
1136

1137
  if ((code = syncInit()) != 0) {
547,663✔
1138
    dError("failed to open sync since %s", tstrerror(code));
×
1139
    goto _OVER;
×
1140
  }
1141
  tmsgReportStartup("vnode-sync", "initialized");
547,663✔
1142

1143
  if ((code = vnodeInit(pInput->stopDnodeFp)) != 0) {
547,663✔
1144
    dError("failed to init vnode since %s", tstrerror(code));
×
1145
    goto _OVER;
×
1146
  }
1147
  tmsgReportStartup("vnode-commit", "initialized");
547,663✔
1148

1149
  if ((code = vmStartWorker(pMgmt)) != 0) {
547,663✔
1150
    dError("failed to init workers since %s", tstrerror(code));
×
1151
    goto _OVER;
×
1152
  }
1153
  tmsgReportStartup("vnode-worker", "initialized");
547,663✔
1154

1155
  if ((code = vmOpenVnodes(pMgmt)) != 0) {
547,663✔
1156
    dError("failed to open all vnodes since %s", tstrerror(code));
×
1157
    goto _OVER;
×
1158
  }
1159
  tmsgReportStartup("vnode-vnodes", "initialized");
547,663✔
1160

1161
  if ((code = udfcOpen()) != 0) {
547,663✔
1162
    dError("failed to open udfc in vnode since %s", tstrerror(code));
×
1163
    goto _OVER;
×
1164
  }
1165

1166
  code = 0;
547,663✔
1167

1168
_OVER:
547,663✔
1169
  if (code == 0) {
547,663✔
1170
    pOutput->pMgmt = pMgmt;
547,663✔
1171
  } else {
1172
    dError("failed to init vnodes-mgmt since %s", tstrerror(code));
×
1173
    vmCleanup(pMgmt);
×
1174
  }
1175

1176
  return code;
547,663✔
1177
}
1178

1179
static int32_t vmRequire(const SMgmtInputOpt *pInput, bool *required) {
550,848✔
1180
  *required = tsNumOfSupportVnodes > 0;
550,848✔
1181
  return 0;
550,848✔
1182
}
1183

1184
static void *vmRestoreVnodeInThread(void *param) {
294,816✔
1185
  SVnodeThread *pThread = param;
294,816✔
1186
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
294,816✔
1187

1188
  dInfo("thread:%d, start to restore %d vnodes", pThread->threadIndex, pThread->vnodeNum);
294,816✔
1189
  setThreadName("restore-vnodes");
294,816✔
1190

1191
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
591,436✔
1192
    SVnodeObj *pVnode = pThread->ppVnodes[v];
296,620✔
1193
    if (pVnode->failed) {
296,620✔
1194
      dError("vgId:%d, cannot restore a vnode in failed mode.", pVnode->vgId);
×
1195
      continue;
×
1196
    }
1197

1198
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
296,620✔
1199
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been restored", pVnode->vgId,
296,620✔
1200
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
1201
    tmsgReportStartup("vnode-restore", stepDesc);
296,620✔
1202

1203
    int32_t code = vnodeStart(pVnode->pImpl);
296,620✔
1204
    if (code != 0) {
296,620✔
1205
      dError("vgId:%d, failed to restore vnode by thread:%d", pVnode->vgId, pThread->threadIndex);
×
1206
      pThread->failed++;
×
1207
    } else {
1208
      dInfo("vgId:%d, is restored by thread:%d", pVnode->vgId, pThread->threadIndex);
296,620✔
1209
      pThread->opened++;
296,620✔
1210
      (void)atomic_add_fetch_32(&pMgmt->state.openVnodes, 1);
296,620✔
1211
    }
1212
  }
1213

1214
  dInfo("thread:%d, numOfVnodes:%d, restored:%d failed:%d", pThread->threadIndex, pThread->vnodeNum, pThread->opened,
294,816✔
1215
        pThread->failed);
1216
  return NULL;
294,816✔
1217
}
1218

1219
static int32_t vmStartVnodes(SVnodeMgmt *pMgmt) {
547,663✔
1220
  int32_t     code = 0;
547,663✔
1221
  int32_t     numOfVnodes = 0;
547,663✔
1222
  SVnodeObj **ppVnodes = NULL;
547,663✔
1223
  code = vmGetVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes);
547,663✔
1224
  if (code != 0) {
547,663✔
1225
    dError("failed to get vnode list since %s", tstrerror(code));
×
1226
    return code;
×
1227
  }
1228

1229
  int32_t threadNum = tsNumOfCores / 2;
547,663✔
1230
  if (threadNum < 1) threadNum = 1;
547,663✔
1231
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
547,663✔
1232

1233
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
547,663✔
1234
  if (threads == NULL) {
547,663✔
1235
    return terrno;
×
1236
  }
1237

1238
  for (int32_t t = 0; t < threadNum; ++t) {
11,500,923✔
1239
    threads[t].threadIndex = t;
10,953,260✔
1240
    threads[t].pMgmt = pMgmt;
10,953,260✔
1241
    threads[t].ppVnodes = taosMemoryCalloc(vnodesPerThread, sizeof(SVnode *));
10,953,260✔
1242
    if (threads[t].ppVnodes == NULL) {
10,953,260✔
1243
      code = terrno;
×
1244
      break;
×
1245
    }
1246
  }
1247

1248
  for (int32_t v = 0; v < numOfVnodes; ++v) {
844,283✔
1249
    int32_t       t = v % threadNum;
296,620✔
1250
    SVnodeThread *pThread = &threads[t];
296,620✔
1251
    if (pThread->ppVnodes != NULL && ppVnodes != NULL) {
296,620✔
1252
      pThread->ppVnodes[pThread->vnodeNum++] = ppVnodes[v];
296,620✔
1253
    }
1254
  }
1255

1256
  pMgmt->state.openVnodes = 0;
547,663✔
1257
  pMgmt->state.dropVnodes = 0;
547,663✔
1258
  dInfo("restore %d vnodes with %d threads", numOfVnodes, threadNum);
547,663✔
1259

1260
  for (int32_t t = 0; t < threadNum; ++t) {
11,500,923✔
1261
    SVnodeThread *pThread = &threads[t];
10,953,260✔
1262
    if (pThread->vnodeNum == 0) continue;
10,953,260✔
1263

1264
    TdThreadAttr thAttr;
294,750✔
1265
    (void)taosThreadAttrInit(&thAttr);
294,816✔
1266
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
294,816✔
1267
    if (taosThreadCreate(&pThread->thread, &thAttr, vmRestoreVnodeInThread, pThread) != 0) {
294,816✔
1268
      dError("thread:%d, failed to create thread to restore vnode since %s", pThread->threadIndex, strerror(ERRNO));
×
1269
    }
1270

1271
    (void)taosThreadAttrDestroy(&thAttr);
294,816✔
1272
  }
1273

1274
  for (int32_t t = 0; t < threadNum; ++t) {
11,500,923✔
1275
    SVnodeThread *pThread = &threads[t];
10,953,260✔
1276
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
10,953,260✔
1277
      (void)taosThreadJoin(pThread->thread, NULL);
294,816✔
1278
      taosThreadClear(&pThread->thread);
294,816✔
1279
    }
1280
    taosMemoryFree(pThread->ppVnodes);
10,953,260✔
1281
  }
1282
  taosMemoryFree(threads);
547,663✔
1283

1284
  for (int32_t i = 0; i < numOfVnodes; ++i) {
844,283✔
1285
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
296,620✔
1286
    vmReleaseVnode(pMgmt, ppVnodes[i]);
296,620✔
1287
  }
1288

1289
  if (ppVnodes != NULL) {
547,663✔
1290
    taosMemoryFree(ppVnodes);
547,663✔
1291
  }
1292

1293
  return vmInitTimer(pMgmt);
547,663✔
1294

1295
_exit:
1296
  for (int32_t t = 0; t < threadNum; ++t) {
1297
    SVnodeThread *pThread = &threads[t];
1298
    taosMemoryFree(pThread->ppVnodes);
1299
  }
1300
  taosMemoryFree(threads);
1301
  return code;
1302
}
1303

1304
static void vmStop(SVnodeMgmt *pMgmt) { vmCleanupTimer(pMgmt); }
547,663✔
1305

1306
SMgmtFunc vmGetMgmtFunc() {
550,848✔
1307
  SMgmtFunc mgmtFunc = {0};
550,848✔
1308
  mgmtFunc.openFp = vmInit;
550,848✔
1309
  mgmtFunc.closeFp = (NodeCloseFp)vmCleanup;
550,848✔
1310
  mgmtFunc.startFp = (NodeStartFp)vmStartVnodes;
550,848✔
1311
  mgmtFunc.stopFp = (NodeStopFp)vmStop;
550,848✔
1312
  mgmtFunc.requiredFp = vmRequire;
550,848✔
1313
  mgmtFunc.getHandlesFp = vmGetMsgHandles;
550,848✔
1314

1315
  return mgmtFunc;
550,848✔
1316
}
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