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

taosdata / TDengine / #4911

04 Jan 2026 09:05AM UTC coverage: 65.028% (-0.8%) from 65.864%
#4911

push

travis-ci

web-flow
merge: from main to 3.0 branch #34156

1206 of 4524 new or added lines in 22 files covered. (26.66%)

1517 existing lines in 134 files now uncovered.

195276 of 300296 relevant lines covered (65.03%)

116931714.52 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,550,563✔
24
  int32_t    diskId = -1;
2,550,563✔
25
  SVnodeObj *pVnode = NULL;
2,550,563✔
26

27
  (void)taosThreadRwlockRdlock(&pMgmt->hashLock);
2,567,091✔
28
  int32_t r = taosHashGetDup(pMgmt->runngingHash, &vgId, sizeof(int32_t), (void *)&pVnode);
2,564,981✔
29
  if (pVnode != NULL) {
2,566,088✔
30
    diskId = pVnode->diskPrimary;
×
31
  }
32
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
2,566,088✔
33
  return diskId;
2,566,467✔
34
}
35

36
static void vmFreeVnodeObj(SVnodeObj **ppVnode) {
6,872,932✔
37
  if (!ppVnode || !(*ppVnode)) return;
6,872,932✔
38

39
  SVnodeObj *pVnode = *ppVnode;
6,873,761✔
40

41
  int32_t refCount = atomic_load_32(&pVnode->refCount);
6,872,932✔
42
  while (refCount > 0) {
6,872,812✔
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);
6,872,812✔
49
  taosMemoryFree(pVnode);
6,873,597✔
50
  ppVnode[0] = NULL;
6,873,344✔
51
}
52

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

62
  pCreatingVnode->vgId = vgId;
2,567,095✔
63
  pCreatingVnode->diskPrimary = diskId;
2,567,095✔
64

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

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

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

83
  return code;
2,567,095✔
84
}
85

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

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

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

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

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

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

128
  diskId = tfsSearch(pTfs, 0, fname);
2,562,515✔
129
  if (diskId >= 0) {
2,562,810✔
130
    return diskId;
×
131
  }
132
  diskId = tfsSearch(pTfs, 0, fnameTmp);
2,562,810✔
133
  if (diskId >= 0) {
2,567,095✔
134
    return diskId;
×
135
  }
136

137
  // alloc
138
  int32_t     disks[TFS_MAX_DISKS_PER_TIER] = {0};
2,567,095✔
139
  int32_t     numOfVnodes = 0;
2,567,095✔
140
  SVnodeObj **ppVnodes = NULL;
2,563,361✔
141

142
  code = taosThreadMutexLock(&pMgmt->mutex);
2,563,331✔
143
  if (code != 0) {
2,567,095✔
144
    return code;
×
145
  }
146

147
  code = vmGetAllVnodeListFromHashWithCreating(pMgmt, &numOfVnodes, &ppVnodes);
2,567,095✔
148
  if (code != 0) {
2,567,095✔
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,370,947✔
157
    SVnodeObj *pVnode = ppVnodes[v];
8,803,852✔
158
    disks[pVnode->diskPrimary] += 1;
8,803,852✔
159
  }
160

161
  int32_t minVal = INT_MAX;
2,567,095✔
162
  int32_t ndisk = tfsGetDisksAtLevel(pTfs, 0);
2,567,095✔
163
  diskId = 0;
2,567,095✔
164
  for (int32_t id = 0; id < ndisk; id++) {
5,358,139✔
165
    if (minVal > disks[id]) {
2,791,044✔
166
      minVal = disks[id];
2,615,542✔
167
      diskId = id;
2,615,542✔
168
    }
169
  }
170
  code = vmRegisterCreatingState(pMgmt, vgId, diskId);
2,567,095✔
171
  if (code != 0) {
2,567,095✔
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,567,095✔
180
  if (code != 0) {
2,567,095✔
181
    goto _OVER;
×
182
  }
183

184
_OVER:
2,567,095✔
185

186
  for (int32_t i = 0; i < numOfVnodes; ++i) {
11,370,947✔
187
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
8,803,852✔
188
    vmReleaseVnode(pMgmt, ppVnodes[i]);
8,803,852✔
189
  }
190
  if (ppVnodes != NULL) {
2,567,095✔
191
    taosMemoryFree(ppVnodes);
2,567,095✔
192
  }
193

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

203
void vmCleanPrimaryDisk(SVnodeMgmt *pMgmt, int32_t vgId) { vmUnRegisterCreatingState(pMgmt, vgId); }
2,567,987✔
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;
12,441,447✔
212
    dDebug("vgId:%d, acquire vnode failed.", vgId);
12,379,227✔
213
    pVnode = NULL;
12,377,155✔
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,591,923✔
235
  SVnodeObj *pOld = NULL;
3,591,923✔
236
  dInfo("vgId:%d, put vnode into running hash", pVnode->vgId);
3,591,923✔
237

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

247
  return code;
3,591,923✔
248
}
249

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

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

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

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

289
  return code;
714,996✔
290
}
291

292
static void vmUnRegisterClosedState(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
3,591,923✔
293
  SVnodeObj *pOld = NULL;
3,591,923✔
294
  dInfo("vgId:%d, remove from closed hash", pVnode->vgId);
3,591,923✔
295
  int32_t r = taosHashGetDup(pMgmt->closedHash, &pVnode->vgId, sizeof(int32_t), (void *)&pOld);
3,591,923✔
296
  if (r != 0) {
3,591,923✔
297
    dError("vgId:%d, failed to get vnode from closedHash", pVnode->vgId);
×
298
  }
299
  if (pOld != NULL) {
3,591,923✔
300
    vmFreeVnodeObj(&pOld);
714,996✔
301
    dInfo("vgId:%d, remove from closedHash", pVnode->vgId);
714,996✔
302
    r = taosHashRemove(pMgmt->closedHash, &pVnode->vgId, sizeof(int32_t));
714,996✔
303
    if (r != 0) {
714,996✔
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,591,923✔
312
#ifdef USE_MOUNT
313
int32_t vmAcquireMountTfs(SVnodeMgmt *pMgmt, int64_t mountId, const char *mountName, const char *mountPath,
1,776✔
314
                          STfs **ppTfs) {
315
  int32_t    code = 0, lino = 0;
1,776✔
316
  TdFilePtr  pFile = NULL;
1,776✔
317
  SArray    *pDisks = NULL;
1,776✔
318
  SMountTfs *pMountTfs = NULL;
1,776✔
319
  bool       unlock = false;
1,776✔
320

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

344
  TAOS_CHECK_EXIT(vmMountCheckRunning(mountName, mountPath, &pFile, 3));
223✔
345
  TAOS_CHECK_EXIT(vmGetMountDisks(pMgmt, mountPath, &pDisks));
223✔
346
  int32_t numOfDisks = taosArrayGetSize(pDisks);
223✔
347
  if (numOfDisks <= 0) {
223✔
348
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_JSON_FORMAT);
×
349
  }
350
  TSDB_CHECK_NULL((pMountTfs = taosMemoryCalloc(1, sizeof(SMountTfs))), code, lino, _exit, terrno);
223✔
351
  if (mountName) (void)snprintf(pMountTfs->name, sizeof(pMountTfs->name), "%s", mountName);
223✔
352
  if (mountPath) (void)snprintf(pMountTfs->path, sizeof(pMountTfs->path), "%s", mountPath);
223✔
353
  pMountTfs->pFile = pFile;
223✔
354
  atomic_store_32(&pMountTfs->nRef, 2);  // init and acquire
223✔
355
  TAOS_CHECK_EXIT(tfsOpen(TARRAY_GET_ELEM(pDisks, 0), numOfDisks, &pMountTfs->pTfs));
223✔
356
  TAOS_CHECK_EXIT(taosHashPut(pMgmt->mountTfsHash, &mountId, sizeof(mountId), &pMountTfs, POINTER_BYTES));
223✔
357
_exit:
223✔
358
  if (unlock) {
223✔
359
    (void)taosThreadMutexUnlock(&pMgmt->mutex);
223✔
360
  }
361
  taosArrayDestroy(pDisks);
223✔
362
  if (code != 0) {
223✔
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;
223✔
376
  }
377

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

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

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

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

419
  pVnode->vgId = pCfg->vgId;
3,591,923✔
420
  pVnode->vgVersion = pCfg->vgVersion;
3,591,923✔
421
  pVnode->diskPrimary = pCfg->diskPrimary;
3,591,923✔
422
  pVnode->mountId = pCfg->mountId;
3,591,923✔
423
  pVnode->refCount = 0;
3,591,923✔
424
  pVnode->dropped = 0;
3,591,923✔
425
  pVnode->failed = 0;
3,591,923✔
426
  pVnode->path = taosStrdup(pCfg->path);
3,591,923✔
427
  pVnode->pImpl = pImpl;
3,591,923✔
428

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

435
  if (pImpl) {
3,591,923✔
436
    if (vmAllocQueue(pMgmt, pVnode) != 0) {
3,591,923✔
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,591,923✔
447
  int32_t code = vmRegisterRunningState(pMgmt, pVnode);
3,591,923✔
448
  vmUnRegisterClosedState(pMgmt, pVnode);
3,591,923✔
449
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
3,591,923✔
450

451
  TAOS_RETURN(code);
3,591,923✔
452
}
453

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

459
  if (pVnode->pImpl && vnodeIsLeader(pVnode->pImpl)) {
3,591,923✔
460
    vnodeProposeCommitOnNeed(pVnode->pImpl, atExit);
2,767,777✔
461
  }
462

463
  (void)taosThreadRwlockWrlock(&pMgmt->hashLock);
3,591,848✔
464
  vmUnRegisterRunningState(pMgmt, pVnode->vgId);
3,591,923✔
465
  if (keepClosed) {
3,591,923✔
466
    if (vmRegisterClosedState(pMgmt, pVnode) != 0) {
714,996✔
467
      (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
×
468
      return;
×
469
    };
470
  }
471
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
3,591,923✔
472

473
  vmReleaseVnode(pMgmt, pVnode);
3,591,923✔
474

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

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

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

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

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

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

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

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

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

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

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

515
  vmFreeQueue(pMgmt, pVnode);
3,591,923✔
516

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

528
  int32_t nodeId = vnodeNodeId(pVnode->pImpl);
3,591,923✔
529
  vnodeClose(pVnode->pImpl);
3,591,923✔
530
  pVnode->pImpl = NULL;
3,591,253✔
531

532
_closed:
3,590,495✔
533
  dInfo("vgId:%d, vnode is closed", pVnode->vgId);
3,590,495✔
534

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

546
  if (pVnode->dropped) {
3,591,923✔
547
    dInfo("vgId:%d, vnode is destroyed, dropped:%d", pVnode->vgId, pVnode->dropped);
1,387,190✔
548
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pVnode->vgId);
1,387,190✔
549
    vnodeDestroy(pVnode->vgId, path, pMgmt->pTfs, nodeId);
1,387,190✔
550
  }
551
  if (pVnode->mountId && vmReleaseMountTfs(pMgmt, pVnode->mountId, pVnode->dropped ? 1 : 0)) {
3,591,923✔
552
    if (vmWriteMountListToFile(pMgmt) != 0) {
221✔
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,591,670✔
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) {
282,869✔
599
  SVnodeThread *pThread = param;
282,869✔
600
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
282,869✔
601
  char          path[TSDB_FILENAME_LEN];
282,808✔
602

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

606
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
567,922✔
607
    SWrapperCfg *pCfg = &pThread->pCfgs[v];
285,053✔
608
    if (pCfg->dropped) {
285,053✔
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};
285,053✔
623
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been opened", pCfg->vgId,
285,053✔
624
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
625
    tmsgReportStartup("vnode-open", stepDesc);
285,053✔
626

627
    if (pCfg->toVgId) {
285,053✔
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;
285,053✔
637
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pCfg->vgId);
284,995✔
638

639
    STfs *pMountTfs = NULL;
285,053✔
640
#ifdef USE_MOUNT
641
    bool releaseTfs = false;
285,053✔
642
    if (pCfg->mountId) {
285,053✔
643
      if (vmAcquireMountTfs(pMgmt, pCfg->mountId, NULL, NULL, &pMountTfs) != 0) {
884✔
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;
884✔
649
    }
650
#endif
651

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

654
    if (pImpl == NULL) {
285,053✔
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) {
285,053✔
666
      if (vmOpenVnode(pMgmt, pCfg, pImpl) != 0) {
285,053✔
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);
285,053✔
677
    pThread->opened++;
285,053✔
678
    (void)atomic_add_fetch_32(&pMgmt->state.openVnodes, 1);
285,053✔
679
  }
680

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

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

695
  TAOS_CHECK_EXIT(vmGetMountListFromFile(pMgmt, &pMountCfgs, &numOfMounts));
530,861✔
696
  for (int32_t i = 0; i < numOfMounts; ++i) {
531,082✔
697
    SMountCfg *pCfg = &pMountCfgs[i];
221✔
698
    if (taosHashGet(pMgmt->mountTfsHash, &pCfg->mountId, sizeof(pCfg->mountId))) {
221✔
699
      TAOS_CHECK_EXIT(TSDB_CODE_INTERNAL_ERROR);
×
700
    }
701
    TAOS_CHECK_EXIT(vmMountCheckRunning(pCfg->name, pCfg->path, &pFile, 3));
221✔
702
    TAOS_CHECK_EXIT(vmGetMountDisks(pMgmt, pCfg->path, &pDisks));
221✔
703
    int32_t nDisks = taosArrayGetSize(pDisks);
221✔
704
    if (nDisks < 1 || nDisks > TFS_MAX_DISKS) {
221✔
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);
221✔
711
    TAOS_CHECK_EXIT(tfsOpen(TARRAY_GET_ELEM(pDisks, 0), TARRAY_SIZE(pDisks), &pMountTfs->pTfs));
221✔
712
    (void)snprintf(pMountTfs->name, sizeof(pMountTfs->name), "%s", pCfg->name);
221✔
713
    (void)snprintf(pMountTfs->path, sizeof(pMountTfs->path), "%s", pCfg->path);
221✔
714
    pMountTfs->pFile = pFile;
221✔
715
    pMountTfs->nRef = 1;
221✔
716
    TAOS_CHECK_EXIT(taosHashPut(pMgmt->mountTfsHash, &pCfg->mountId, sizeof(pCfg->mountId), &pMountTfs, POINTER_BYTES));
221✔
717
    taosArrayDestroy(pDisks);
221✔
718
    pDisks = NULL;
221✔
719
    pMountTfs = NULL;
221✔
720
    pFile = NULL;
221✔
721
  }
722
_exit:
530,861✔
723
  if (code != 0) {
530,861✔
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);
530,861✔
736
  TAOS_RETURN(code);
530,861✔
737
}
738
#endif
739
static int32_t vmOpenVnodes(SVnodeMgmt *pMgmt) {
530,861✔
740
  pMgmt->runngingHash =
530,861✔
741
      taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
530,861✔
742
  if (pMgmt->runngingHash == NULL) {
530,861✔
743
    dError("failed to init vnode hash since %s", terrstr());
×
744
    return TSDB_CODE_OUT_OF_MEMORY;
×
745
  }
746

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

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

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

769
  pMgmt->state.totalVnodes = numOfVnodes;
530,861✔
770

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

775
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
530,861✔
776
  if (threads == NULL) {
530,861✔
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,148,081✔
783
    threads[t].threadIndex = t;
10,617,220✔
784
    threads[t].pMgmt = pMgmt;
10,617,220✔
785
    threads[t].pCfgs = taosMemoryCalloc(vnodesPerThread, sizeof(SWrapperCfg));
10,617,220✔
786
  }
787

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

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

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

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

810
    (void)taosThreadAttrDestroy(&thAttr);
282,869✔
811
  }
812

813
  bool updateVnodesList = false;
530,861✔
814

815
  for (int32_t t = 0; t < threadNum; ++t) {
11,148,081✔
816
    SVnodeThread *pThread = &threads[t];
10,617,220✔
817
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
10,617,220✔
818
      (void)taosThreadJoin(pThread->thread, NULL);
282,869✔
819
      taosThreadClear(&pThread->thread);
282,869✔
820
    }
821
    taosMemoryFree(pThread->pCfgs);
10,617,220✔
822
    if (pThread->updateVnodesList) updateVnodesList = true;
10,617,220✔
823
  }
824
  taosMemoryFree(threads);
530,861✔
825
  taosMemoryFree(pCfgs);
530,861✔
826

827
  if ((pMgmt->state.openVnodes + pMgmt->state.dropVnodes) != pMgmt->state.totalVnodes) {
530,861✔
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) {
530,861✔
833
    dError("failed to write vnode list since %s", tstrerror(code));
×
834
    return code;
×
835
  }
836

837
#ifdef USE_MOUNT
838
  bool  updateMountList = false;
530,861✔
839
  void *pIter = NULL;
530,861✔
840
  while ((pIter = taosHashIterate(pMgmt->mountTfsHash, pIter))) {
531,082✔
841
    SMountTfs *pMountTfs = *(SMountTfs **)pIter;
221✔
842
    if (pMountTfs && atomic_load_32(&pMountTfs->nRef) <= 1) {
221✔
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) {
530,861✔
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);
530,861✔
867
  return 0;
530,861✔
868
}
869

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

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

877
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
2,908,147✔
878
    SVnodeObj *pVnode = pThread->ppVnodes[v];
1,465,469✔
879

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

885
    vmCloseVnode(pMgmt, pVnode, false, false);
1,465,469✔
886
  }
887

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

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

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

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

912
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
530,861✔
913
  for (int32_t t = 0; t < threadNum; ++t) {
11,148,081✔
914
    threads[t].threadIndex = t;
10,617,220✔
915
    threads[t].pMgmt = pMgmt;
10,617,220✔
916
    threads[t].ppVnodes = taosMemoryCalloc(vnodesPerThread, sizeof(SVnode *));
10,617,220✔
917
  }
918

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

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

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

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

944
    (void)taosThreadAttrDestroy(&thAttr);
1,442,297✔
945
  }
946

947
  for (int32_t t = 0; t < threadNum; ++t) {
11,148,081✔
948
    SVnodeThread *pThread = &threads[t];
10,617,220✔
949
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
10,617,220✔
950
      (void)taosThreadJoin(pThread->thread, NULL);
1,442,297✔
951
      taosThreadClear(&pThread->thread);
1,442,297✔
952
    }
953
    taosMemoryFree(pThread->ppVnodes);
10,617,220✔
954
  }
955
  taosMemoryFree(threads);
530,861✔
956

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

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

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

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

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

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

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

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

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

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

1027
  if (ppVnodes != NULL) {
1,088,057✔
1028
    for (int32_t i = 0; i < numOfVnodes; ++i) {
4,413,612✔
1029
      SVnodeObj *pVnode = ppVnodes[i];
3,325,555✔
1030
      if (!pVnode->failed) {
3,325,555✔
1031
        vnodeSyncCheckTimeout(pVnode->pImpl);
3,325,555✔
1032
      }
1033
      vmReleaseVnode(pMgmt, pVnode);
3,325,555✔
1034
    }
1035
    taosMemoryFree(ppVnodes);
1,088,057✔
1036
  }
1037
}
1038

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

1044
  while (1) {
395,755,565✔
1045
    lastTime++;
396,286,426✔
1046
    taosMsleep(100);
396,286,426✔
1047
    if (pMgmt->stop) break;
396,286,426✔
1048
    if (lastTime % 10 != 0) continue;
395,755,565✔
1049

1050
    int64_t sec = lastTime / 10;
39,339,983✔
1051
    if (sec % (VNODE_TIMEOUT_SEC / 2) == 0) {
39,339,983✔
1052
      vmCheckSyncTimeout(pMgmt);
1,088,057✔
1053
    }
1054
  }
1055

1056
  return NULL;
530,861✔
1057
}
1058

1059
static int32_t vmInitTimer(SVnodeMgmt *pMgmt) {
530,861✔
1060
  int32_t      code = 0;
530,861✔
1061
  TdThreadAttr thAttr;
530,652✔
1062
  (void)taosThreadAttrInit(&thAttr);
530,861✔
1063
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
530,861✔
1064
#ifdef TD_COMPACT_OS
1065
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
1066
#endif
1067
  if (taosThreadCreate(&pMgmt->thread, &thAttr, vmThreadFp, pMgmt) != 0) {
530,861✔
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);
530,861✔
1074
  return 0;
530,861✔
1075
}
1076

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

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

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

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

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

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

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

1135
  tmsgReportStartup("vnode-wal", "initialized");
530,861✔
1136

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

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

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

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

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

1166
  code = 0;
530,861✔
1167

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

1176
  return code;
530,861✔
1177
}
1178

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

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

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

1191
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
567,922✔
1192
    SVnodeObj *pVnode = pThread->ppVnodes[v];
285,053✔
1193
    if (pVnode->failed) {
285,053✔
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};
285,053✔
1199
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been restored", pVnode->vgId,
285,053✔
1200
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
1201
    tmsgReportStartup("vnode-restore", stepDesc);
285,053✔
1202

1203
    int32_t code = vnodeStart(pVnode->pImpl);
285,053✔
1204
    if (code != 0) {
285,053✔
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);
285,053✔
1209
      pThread->opened++;
285,053✔
1210
      (void)atomic_add_fetch_32(&pMgmt->state.openVnodes, 1);
285,053✔
1211
    }
1212
  }
1213

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

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

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

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

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

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

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

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

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

1271
    (void)taosThreadAttrDestroy(&thAttr);
282,869✔
1272
  }
1273

1274
  for (int32_t t = 0; t < threadNum; ++t) {
11,148,081✔
1275
    SVnodeThread *pThread = &threads[t];
10,617,220✔
1276
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
10,617,220✔
1277
      (void)taosThreadJoin(pThread->thread, NULL);
282,869✔
1278
      taosThreadClear(&pThread->thread);
282,869✔
1279
    }
1280
    taosMemoryFree(pThread->ppVnodes);
10,617,220✔
1281
  }
1282
  taosMemoryFree(threads);
530,861✔
1283

1284
  for (int32_t i = 0; i < numOfVnodes; ++i) {
815,914✔
1285
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
285,053✔
1286
    vmReleaseVnode(pMgmt, ppVnodes[i]);
285,053✔
1287
  }
1288

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

1293
  return vmInitTimer(pMgmt);
530,861✔
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); }
530,861✔
1305

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

1315
  return mgmtFunc;
538,984✔
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