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

taosdata / TDengine / #4878

11 Dec 2025 02:43AM UTC coverage: 64.569% (-0.02%) from 64.586%
#4878

push

travis-ci

guanshengliang
feat(TS-7270): internal dependence

307 of 617 new or added lines in 24 files covered. (49.76%)

3821 existing lines in 123 files now uncovered.

163630 of 253417 relevant lines covered (64.57%)

107598827.89 hits per line

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

75.61
/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,942,928✔
24
  int32_t    diskId = -1;
2,942,928✔
25
  SVnodeObj *pVnode = NULL;
2,942,928✔
26

27
  (void)taosThreadRwlockRdlock(&pMgmt->hashLock);
2,944,669✔
28
  int32_t r = taosHashGetDup(pMgmt->runngingHash, &vgId, sizeof(int32_t), (void *)&pVnode);
2,943,535✔
29
  if (pVnode != NULL) {
2,946,407✔
30
    diskId = pVnode->diskPrimary;
×
31
  }
32
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
2,946,407✔
33
  return diskId;
2,946,933✔
34
}
35

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

39
  SVnodeObj *pVnode = *ppVnode;
7,767,844✔
40

41
  int32_t refCount = atomic_load_32(&pVnode->refCount);
7,767,546✔
42
  while (refCount > 0) {
7,770,493✔
43
    dWarn("vgId:%d, vnode is refenced, retry to free in 200ms, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
2,947✔
44
    taosMsleep(200);
2,947✔
45
    refCount = atomic_load_32(&pVnode->refCount);
2,947✔
46
  }
47

48
  taosMemoryFree(pVnode->path);
7,767,546✔
49
  taosMemoryFree(pVnode);
7,767,546✔
50
  ppVnode[0] = NULL;
7,767,346✔
51
}
52

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

62
  pCreatingVnode->vgId = vgId;
2,948,142✔
63
  pCreatingVnode->diskPrimary = diskId;
2,948,142✔
64

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

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

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

83
  return code;
2,948,142✔
84
}
85

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

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

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

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

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

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

128
  diskId = tfsSearch(pTfs, 0, fname);
2,936,647✔
129
  if (diskId >= 0) {
2,943,110✔
UNCOV
130
    return diskId;
×
131
  }
132
  diskId = tfsSearch(pTfs, 0, fnameTmp);
2,943,110✔
133
  if (diskId >= 0) {
2,943,339✔
134
    return diskId;
×
135
  }
136

137
  // alloc
138
  int32_t     disks[TFS_MAX_DISKS_PER_TIER] = {0};
2,943,339✔
139
  int32_t     numOfVnodes = 0;
2,946,764✔
140
  SVnodeObj **ppVnodes = NULL;
2,946,681✔
141

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

147
  code = vmGetAllVnodeListFromHashWithCreating(pMgmt, &numOfVnodes, &ppVnodes);
2,948,142✔
148
  if (code != 0) {
2,948,142✔
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++) {
12,855,951✔
157
    SVnodeObj *pVnode = ppVnodes[v];
9,907,809✔
158
    disks[pVnode->diskPrimary] += 1;
9,907,809✔
159
  }
160

161
  int32_t minVal = INT_MAX;
2,948,142✔
162
  int32_t ndisk = tfsGetDisksAtLevel(pTfs, 0);
2,948,142✔
163
  diskId = 0;
2,948,142✔
164
  for (int32_t id = 0; id < ndisk; id++) {
6,095,534✔
165
    if (minVal > disks[id]) {
3,147,392✔
166
      minVal = disks[id];
2,993,129✔
167
      diskId = id;
2,993,129✔
168
    }
169
  }
170
  code = vmRegisterCreatingState(pMgmt, vgId, diskId);
2,948,142✔
171
  if (code != 0) {
2,948,142✔
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,948,142✔
180
  if (code != 0) {
2,948,142✔
181
    goto _OVER;
×
182
  }
183

184
_OVER:
2,948,142✔
185

186
  for (int32_t i = 0; i < numOfVnodes; ++i) {
12,855,951✔
187
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
9,907,809✔
188
    vmReleaseVnode(pMgmt, ppVnodes[i]);
9,907,809✔
189
  }
190
  if (ppVnodes != NULL) {
2,948,142✔
191
    taosMemoryFree(ppVnodes);
2,948,142✔
192
  }
193

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

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

205
SVnodeObj *vmAcquireVnodeImpl(SVnodeMgmt *pMgmt, int32_t vgId, bool strict) {
1,540,098,981✔
206
  SVnodeObj *pVnode = NULL;
1,540,098,981✔
207

208
  (void)taosThreadRwlockRdlock(&pMgmt->hashLock);
1,540,132,981✔
209
  int32_t r = taosHashGetDup(pMgmt->runngingHash, &vgId, sizeof(int32_t), (void *)&pVnode);
1,540,386,158✔
210
  if (pVnode == NULL || strict && (pVnode->dropped || pVnode->failed)) {
1,540,300,407✔
211
    terrno = TSDB_CODE_VND_INVALID_VGROUP_ID;
13,461,487✔
212
    dDebug("vgId:%d, acquire vnode failed.", vgId);
13,363,632✔
213
    pVnode = NULL;
13,365,653✔
214
  } else {
215
    int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
1,526,918,811✔
216
    dTrace("vgId:%d, acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
1,526,970,110✔
217
  }
218
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
1,540,336,956✔
219

220
  return pVnode;
1,540,373,199✔
221
}
222

223
SVnodeObj *vmAcquireVnode(SVnodeMgmt *pMgmt, int32_t vgId) { return vmAcquireVnodeImpl(pMgmt, vgId, true); }
1,535,669,760✔
224

225
void vmReleaseVnode(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
1,567,184,249✔
226
  if (pVnode == NULL) return;
1,567,184,249✔
227

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

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

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

247
  return code;
4,103,897✔
248
}
249

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

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

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

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

289
  return code;
715,805✔
290
}
291

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

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

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

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

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

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

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

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

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

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

451
  TAOS_RETURN(code);
4,103,897✔
452
}
453

454
void vmCloseVnode(SVnodeMgmt *pMgmt, SVnodeObj *pVnode, bool commitAndRemoveWal, bool keepClosed) {
4,103,007✔
455
  char path[TSDB_FILENAME_LEN] = {0};
4,103,007✔
456
  bool atExit = true;
4,103,007✔
457

458
  if (pVnode->pImpl && vnodeIsLeader(pVnode->pImpl)) {
4,103,007✔
459
    vnodeProposeCommitOnNeed(pVnode->pImpl, atExit);
3,148,281✔
460
  }
461

462
  (void)taosThreadRwlockWrlock(&pMgmt->hashLock);
4,102,592✔
463
  vmUnRegisterRunningState(pMgmt, pVnode->vgId);
4,103,897✔
464
  if (keepClosed) {
4,103,897✔
465
    if (vmRegisterClosedState(pMgmt, pVnode) != 0) {
715,805✔
466
      (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
×
467
      return;
×
468
    };
469
  }
470
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
4,103,897✔
471

472
  vmReleaseVnode(pMgmt, pVnode);
4,103,897✔
473

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

480
  dInfo("vgId:%d, wait for vnode ref become 0", pVnode->vgId);
4,103,897✔
481
  while (pVnode->refCount > 0) taosMsleep(10);
4,106,021✔
482

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

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

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

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

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

503
  dInfo("vgId:%d, wait for vnode query queue:%p is empty", pVnode->vgId, pVnode->pQueryQ);
4,103,897✔
504
  while (!taosQueueEmpty(pVnode->pQueryQ)) taosMsleep(10);
4,104,588✔
505

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

509
  dInfo("vgId:%d, all vnode queues is empty", pVnode->vgId);
4,103,897✔
510

511
  dInfo("vgId:%d, post close", pVnode->vgId);
4,103,897✔
512
  vnodePostClose(pVnode->pImpl);
4,103,897✔
513

514
  vmFreeQueue(pMgmt, pVnode);
4,103,295✔
515

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

527
  int32_t nodeId = vnodeNodeId(pVnode->pImpl);
4,103,897✔
528
  vnodeClose(pVnode->pImpl);
4,103,897✔
529
  pVnode->pImpl = NULL;
4,103,897✔
530

531
_closed:
4,100,797✔
532
  dInfo("vgId:%d, vnode is closed", pVnode->vgId);
4,100,797✔
533

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

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

556
  vmFreeVnodeObj(&pVnode);
4,103,897✔
557
}
558

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

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

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

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

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

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

597
static void *vmOpenVnodeInThread(void *param) {
412,699✔
598
  SVnodeThread *pThread = param;
412,699✔
599
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
412,699✔
600
  char          path[TSDB_FILENAME_LEN];
411,556✔
601

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

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

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

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

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

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

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

651
    SVnode *pImpl = vnodeOpen(path, diskPrimary, pMgmt->pTfs, pMountTfs, pMgmt->msgCb, false);
417,291✔
652

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

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

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

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

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

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

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

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

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

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

768
  pMgmt->state.totalVnodes = numOfVnodes;
678,058✔
769

770
  int32_t threadNum = tsNumOfCores / 2;
678,058✔
771
  if (threadNum < 1) threadNum = 1;
678,058✔
772
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
678,058✔
773

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

781
  for (int32_t t = 0; t < threadNum; ++t) {
14,239,218✔
782
    threads[t].threadIndex = t;
13,561,160✔
783
    threads[t].pMgmt = pMgmt;
13,561,160✔
784
    threads[t].pCfgs = taosMemoryCalloc(vnodesPerThread, sizeof(SWrapperCfg));
13,561,160✔
785
  }
786

787
  for (int32_t v = 0; v < numOfVnodes; ++v) {
1,095,349✔
788
    int32_t       t = v % threadNum;
417,291✔
789
    SVnodeThread *pThread = &threads[t];
417,291✔
790
    pThread->pCfgs[pThread->vnodeNum++] = pCfgs[v];
417,291✔
791
  }
792

793
  dInfo("open %d vnodes with %d threads", numOfVnodes, threadNum);
678,058✔
794

795
  for (int32_t t = 0; t < threadNum; ++t) {
14,239,218✔
796
    SVnodeThread *pThread = &threads[t];
13,561,160✔
797
    if (pThread->vnodeNum == 0) continue;
13,561,160✔
798

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

809
    (void)taosThreadAttrDestroy(&thAttr);
412,699✔
810
  }
811

812
  bool updateVnodesList = false;
678,058✔
813

814
  for (int32_t t = 0; t < threadNum; ++t) {
14,239,218✔
815
    SVnodeThread *pThread = &threads[t];
13,561,160✔
816
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
13,561,160✔
817
      (void)taosThreadJoin(pThread->thread, NULL);
412,699✔
818
      taosThreadClear(&pThread->thread);
412,699✔
819
    }
820
    taosMemoryFree(pThread->pCfgs);
13,561,160✔
821
    if (pThread->updateVnodesList) updateVnodesList = true;
13,561,160✔
822
  }
823
  taosMemoryFree(threads);
678,058✔
824
  taosMemoryFree(pCfgs);
678,058✔
825

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

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

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

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

869
static void *vmCloseVnodeInThread(void *param) {
1,720,665✔
870
  SVnodeThread *pThread = param;
1,720,665✔
871
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
1,720,665✔
872

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

876
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
3,491,217✔
877
    SVnodeObj *pVnode = pThread->ppVnodes[v];
1,770,265✔
878

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

884
    vmCloseVnode(pMgmt, pVnode, false, false);
1,768,637✔
885
  }
886

887
  dInfo("thread:%d, numOfVnodes:%d is closed", pThread->threadIndex, pThread->vnodeNum);
1,720,666✔
888
  return NULL;
1,720,952✔
889
}
890

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

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

907
  int32_t threadNum = tsNumOfCores / 2;
678,058✔
908
  if (threadNum < 1) threadNum = 1;
678,058✔
909
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
678,058✔
910

911
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
678,058✔
912
  for (int32_t t = 0; t < threadNum; ++t) {
14,239,218✔
913
    threads[t].threadIndex = t;
13,561,160✔
914
    threads[t].pMgmt = pMgmt;
13,561,160✔
915
    threads[t].ppVnodes = taosMemoryCalloc(vnodesPerThread, sizeof(SVnode *));
13,561,160✔
916
  }
917

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

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

929
  for (int32_t t = 0; t < threadNum; ++t) {
14,239,218✔
930
    SVnodeThread *pThread = &threads[t];
13,561,160✔
931
    if (pThread->vnodeNum == 0) continue;
13,561,160✔
932

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

943
    (void)taosThreadAttrDestroy(&thAttr);
1,720,952✔
944
  }
945

946
  for (int32_t t = 0; t < threadNum; ++t) {
14,239,218✔
947
    SVnodeThread *pThread = &threads[t];
13,561,160✔
948
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
13,561,160✔
949
      (void)taosThreadJoin(pThread->thread, NULL);
1,720,952✔
950
      taosThreadClear(&pThread->thread);
1,720,952✔
951
    }
952
    taosMemoryFree(pThread->ppVnodes);
13,561,160✔
953
  }
954
  taosMemoryFree(threads);
678,058✔
955

956
  if (ppVnodes != NULL) {
678,058✔
957
    taosMemoryFree(ppVnodes);
678,058✔
958
  }
959

960
  if (pMgmt->runngingHash != NULL) {
678,058✔
961
    taosHashCleanup(pMgmt->runngingHash);
678,058✔
962
    pMgmt->runngingHash = NULL;
678,058✔
963
  }
964

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

972
  if (pMgmt->closedHash != NULL) {
678,058✔
973
    taosHashCleanup(pMgmt->closedHash);
678,058✔
974
    pMgmt->closedHash = NULL;
678,058✔
975
  }
976

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

984
  if (pMgmt->creatingHash != NULL) {
678,058✔
985
    taosHashCleanup(pMgmt->creatingHash);
678,058✔
986
    pMgmt->creatingHash = NULL;
678,058✔
987
  }
988

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

1004
  dInfo("total vnodes:%d are all closed", numOfVnodes);
678,058✔
1005
}
1006

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

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

1026
  if (ppVnodes != NULL) {
1,138,299✔
1027
    for (int32_t i = 0; i < numOfVnodes; ++i) {
4,259,450✔
1028
      SVnodeObj *pVnode = ppVnodes[i];
3,121,151✔
1029
      if (!pVnode->failed) {
3,121,151✔
1030
        vnodeSyncCheckTimeout(pVnode->pImpl);
3,121,151✔
1031
      }
1032
      vmReleaseVnode(pMgmt, pVnode);
3,121,151✔
1033
    }
1034
    taosMemoryFree(ppVnodes);
1,138,299✔
1035
  }
1036
}
1037

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

1043
  while (1) {
431,402,166✔
1044
    lastTime++;
432,080,224✔
1045
    taosMsleep(100);
432,080,224✔
1046
    if (pMgmt->stop) break;
432,080,224✔
1047
    if (lastTime % 10 != 0) continue;
431,402,166✔
1048

1049
    int64_t sec = lastTime / 10;
42,834,069✔
1050
    if (sec % (VNODE_TIMEOUT_SEC / 2) == 0) {
42,834,069✔
1051
      vmCheckSyncTimeout(pMgmt);
1,138,299✔
1052
    }
1053
  }
1054

1055
  return NULL;
678,058✔
1056
}
1057

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

1072
  (void)taosThreadAttrDestroy(&thAttr);
678,058✔
1073
  return 0;
678,058✔
1074
}
1075

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

1084
static int32_t vmInit(SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) {
678,058✔
1085
  int32_t code = -1;
678,058✔
1086

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

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

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

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

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

1134
  tmsgReportStartup("vnode-wal", "initialized");
678,058✔
1135

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

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

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

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

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

1165
  code = 0;
678,058✔
1166

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

1175
  return code;
678,058✔
1176
}
1177

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

1183
static void *vmRestoreVnodeInThread(void *param) {
412,699✔
1184
  SVnodeThread *pThread = param;
412,699✔
1185
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
412,699✔
1186

1187
  dInfo("thread:%d, start to restore %d vnodes", pThread->threadIndex, pThread->vnodeNum);
412,699✔
1188
  setThreadName("restore-vnodes");
412,699✔
1189

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

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

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

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

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

1228
  int32_t threadNum = tsNumOfCores / 2;
678,058✔
1229
  if (threadNum < 1) threadNum = 1;
678,058✔
1230
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
678,058✔
1231

1232
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
678,058✔
1233
  if (threads == NULL) {
678,058✔
1234
    return terrno;
×
1235
  }
1236

1237
  for (int32_t t = 0; t < threadNum; ++t) {
14,239,218✔
1238
    threads[t].threadIndex = t;
13,561,160✔
1239
    threads[t].pMgmt = pMgmt;
13,561,160✔
1240
    threads[t].ppVnodes = taosMemoryCalloc(vnodesPerThread, sizeof(SVnode *));
13,561,160✔
1241
    if (threads[t].ppVnodes == NULL) {
13,561,160✔
1242
      code = terrno;
×
1243
      break;
×
1244
    }
1245
  }
1246

1247
  for (int32_t v = 0; v < numOfVnodes; ++v) {
1,095,349✔
1248
    int32_t       t = v % threadNum;
417,291✔
1249
    SVnodeThread *pThread = &threads[t];
417,291✔
1250
    if (pThread->ppVnodes != NULL && ppVnodes != NULL) {
417,291✔
1251
      pThread->ppVnodes[pThread->vnodeNum++] = ppVnodes[v];
417,291✔
1252
    }
1253
  }
1254

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

1259
  for (int32_t t = 0; t < threadNum; ++t) {
14,239,218✔
1260
    SVnodeThread *pThread = &threads[t];
13,561,160✔
1261
    if (pThread->vnodeNum == 0) continue;
13,561,160✔
1262

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

1270
    (void)taosThreadAttrDestroy(&thAttr);
412,699✔
1271
  }
1272

1273
  for (int32_t t = 0; t < threadNum; ++t) {
14,239,218✔
1274
    SVnodeThread *pThread = &threads[t];
13,561,160✔
1275
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
13,561,160✔
1276
      (void)taosThreadJoin(pThread->thread, NULL);
412,699✔
1277
      taosThreadClear(&pThread->thread);
412,699✔
1278
    }
1279
    taosMemoryFree(pThread->ppVnodes);
13,561,160✔
1280
  }
1281
  taosMemoryFree(threads);
678,058✔
1282

1283
  for (int32_t i = 0; i < numOfVnodes; ++i) {
1,095,349✔
1284
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
417,291✔
1285
    vmReleaseVnode(pMgmt, ppVnodes[i]);
417,291✔
1286
  }
1287

1288
  if (ppVnodes != NULL) {
678,058✔
1289
    taosMemoryFree(ppVnodes);
678,058✔
1290
  }
1291

1292
  return vmInitTimer(pMgmt);
678,058✔
1293

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

1303
static void vmStop(SVnodeMgmt *pMgmt) { vmCleanupTimer(pMgmt); }
678,058✔
1304

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

1314
  return mgmtFunc;
685,114✔
1315
}
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