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

taosdata / TDengine / #4874

04 Dec 2025 01:55AM UTC coverage: 64.623% (+0.07%) from 64.558%
#4874

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

865 of 2219 new or added lines in 36 files covered. (38.98%)

6317 existing lines in 143 files now uncovered.

159543 of 246882 relevant lines covered (64.62%)

106415537.4 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) {
3,007,673✔
24
  int32_t    diskId = -1;
3,007,673✔
25
  SVnodeObj *pVnode = NULL;
3,007,673✔
26

27
  (void)taosThreadRwlockRdlock(&pMgmt->hashLock);
3,007,679✔
28
  int32_t r = taosHashGetDup(pMgmt->runngingHash, &vgId, sizeof(int32_t), (void *)&pVnode);
3,003,185✔
29
  if (pVnode != NULL) {
3,009,135✔
30
    diskId = pVnode->diskPrimary;
×
31
  }
32
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
3,009,135✔
33
  return diskId;
3,012,429✔
34
}
35

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

39
  SVnodeObj *pVnode = *ppVnode;
7,932,646✔
40

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

48
  taosMemoryFree(pVnode->path);
7,932,646✔
49
  taosMemoryFree(pVnode);
7,932,131✔
50
  ppVnode[0] = NULL;
7,932,131✔
51
}
52

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

62
  pCreatingVnode->vgId = vgId;
3,012,525✔
63
  pCreatingVnode->diskPrimary = diskId;
3,012,525✔
64

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

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

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

83
  return code;
3,012,525✔
84
}
85

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

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

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

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

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

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

128
  diskId = tfsSearch(pTfs, 0, fname);
2,997,461✔
129
  if (diskId >= 0) {
3,005,267✔
UNCOV
130
    return diskId;
×
131
  }
132
  diskId = tfsSearch(pTfs, 0, fnameTmp);
3,005,267✔
133
  if (diskId >= 0) {
3,005,751✔
134
    return diskId;
×
135
  }
136

137
  // alloc
138
  int32_t     disks[TFS_MAX_DISKS_PER_TIER] = {0};
3,005,751✔
139
  int32_t     numOfVnodes = 0;
3,007,760✔
140
  SVnodeObj **ppVnodes = NULL;
3,011,493✔
141

142
  code = taosThreadMutexLock(&pMgmt->mutex);
3,010,496✔
143
  if (code != 0) {
3,012,525✔
144
    return code;
×
145
  }
146

147
  code = vmGetAllVnodeListFromHashWithCreating(pMgmt, &numOfVnodes, &ppVnodes);
3,012,525✔
148
  if (code != 0) {
3,012,525✔
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++) {
13,190,899✔
157
    SVnodeObj *pVnode = ppVnodes[v];
10,178,374✔
158
    disks[pVnode->diskPrimary] += 1;
10,178,374✔
159
  }
160

161
  int32_t minVal = INT_MAX;
3,012,525✔
162
  int32_t ndisk = tfsGetDisksAtLevel(pTfs, 0);
3,012,525✔
163
  diskId = 0;
3,012,525✔
164
  for (int32_t id = 0; id < ndisk; id++) {
6,230,187✔
165
    if (minVal > disks[id]) {
3,217,662✔
166
      minVal = disks[id];
3,058,779✔
167
      diskId = id;
3,058,779✔
168
    }
169
  }
170
  code = vmRegisterCreatingState(pMgmt, vgId, diskId);
3,012,525✔
171
  if (code != 0) {
3,012,525✔
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);
3,012,525✔
180
  if (code != 0) {
3,012,525✔
181
    goto _OVER;
×
182
  }
183

184
_OVER:
3,012,525✔
185

186
  for (int32_t i = 0; i < numOfVnodes; ++i) {
13,190,899✔
187
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
10,178,374✔
188
    vmReleaseVnode(pMgmt, ppVnodes[i]);
10,178,374✔
189
  }
190
  if (ppVnodes != NULL) {
3,012,525✔
191
    taosMemoryFree(ppVnodes);
3,012,525✔
192
  }
193

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

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

205
SVnodeObj *vmAcquireVnodeImpl(SVnodeMgmt *pMgmt, int32_t vgId, bool strict) {
1,554,478,600✔
206
  SVnodeObj *pVnode = NULL;
1,554,478,600✔
207

208
  (void)taosThreadRwlockRdlock(&pMgmt->hashLock);
1,554,498,023✔
209
  int32_t r = taosHashGetDup(pMgmt->runngingHash, &vgId, sizeof(int32_t), (void *)&pVnode);
1,554,787,666✔
210
  if (pVnode == NULL || strict && (pVnode->dropped || pVnode->failed)) {
1,554,671,700✔
211
    terrno = TSDB_CODE_VND_INVALID_VGROUP_ID;
13,592,901✔
212
    dDebug("vgId:%d, acquire vnode failed.", vgId);
13,540,690✔
213
    pVnode = NULL;
13,542,226✔
214
  } else {
215
    int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
1,541,127,198✔
216
    dTrace("vgId:%d, acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
1,541,196,421✔
217
  }
218
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
1,554,739,434✔
219

220
  return pVnode;
1,554,787,452✔
221
}
222

223
SVnodeObj *vmAcquireVnode(SVnodeMgmt *pMgmt, int32_t vgId) { return vmAcquireVnodeImpl(pMgmt, vgId, true); }
1,549,935,422✔
224

225
void vmReleaseVnode(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
1,582,446,682✔
226
  if (pVnode == NULL) return;
1,582,446,682✔
227

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

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

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

247
  return code;
4,196,660✔
248
}
249

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

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

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

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

289
  return code;
723,461✔
290
}
291

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

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

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

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

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

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

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

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

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

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

451
  TAOS_RETURN(code);
4,196,660✔
452
}
453

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

458
  if (pVnode->pImpl && vnodeIsLeader(pVnode->pImpl)) {
4,195,927✔
459
    vnodeProposeCommitOnNeed(pVnode->pImpl, atExit);
3,231,985✔
460
  }
461

462
  (void)taosThreadRwlockWrlock(&pMgmt->hashLock);
4,196,484✔
463
  vmUnRegisterRunningState(pMgmt, pVnode->vgId);
4,196,660✔
464
  if (keepClosed) {
4,196,660✔
465
    if (vmRegisterClosedState(pMgmt, pVnode) != 0) {
723,461✔
466
      (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
×
467
      return;
×
468
    };
469
  }
470
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
4,196,660✔
471

472
  vmReleaseVnode(pMgmt, pVnode);
4,196,660✔
473

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

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

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

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

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

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

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

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

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

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

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

514
  vmFreeQueue(pMgmt, pVnode);
4,196,660✔
515

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

527
  int32_t nodeId = vnodeNodeId(pVnode->pImpl);
4,196,660✔
528
  vnodeClose(pVnode->pImpl);
4,196,660✔
529
  pVnode->pImpl = NULL;
4,195,247✔
530

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

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

545
  if (pVnode->dropped) {
4,196,660✔
546
    dInfo("vgId:%d, vnode is destroyed, dropped:%d", pVnode->vgId, pVnode->dropped);
1,630,839✔
547
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pVnode->vgId);
1,630,839✔
548
    vnodeDestroy(pVnode->vgId, path, pMgmt->pTfs, nodeId);
1,630,839✔
549
  }
550
  if (pVnode->mountId && vmReleaseMountTfs(pMgmt, pVnode->mountId, pVnode->dropped ? 1 : 0)) {
4,196,660✔
551
    if (vmWriteMountListToFile(pMgmt) != 0) {
668✔
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,196,660✔
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) {
431,375✔
598
  SVnodeThread *pThread = param;
431,375✔
599
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
431,375✔
600
  char          path[TSDB_FILENAME_LEN];
430,134✔
601

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

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

626
    if (pCfg->toVgId) {
438,041✔
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;
438,041✔
636
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pCfg->vgId);
438,041✔
637

638
    STfs *pMountTfs = NULL;
438,041✔
639
#ifdef USE_MOUNT
640
    bool releaseTfs = false;
438,041✔
641
    if (pCfg->mountId) {
438,041✔
642
      if (vmAcquireMountTfs(pMgmt, pCfg->mountId, NULL, NULL, &pMountTfs) != 0) {
2,672✔
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,672✔
648
    }
649
#endif
650

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

653
    if (pImpl == NULL) {
438,041✔
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) {
438,041✔
665
      if (vmOpenVnode(pMgmt, pCfg, pImpl) != 0) {
438,041✔
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);
438,041✔
676
    pThread->opened++;
438,041✔
677
    (void)atomic_add_fetch_32(&pMgmt->state.openVnodes, 1);
438,041✔
678
  }
679

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

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

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

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

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

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

768
  pMgmt->state.totalVnodes = numOfVnodes;
694,446✔
769

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

774
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
694,446✔
775
  if (threads == NULL) {
694,446✔
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,583,366✔
782
    threads[t].threadIndex = t;
13,888,920✔
783
    threads[t].pMgmt = pMgmt;
13,888,920✔
784
    threads[t].pCfgs = taosMemoryCalloc(vnodesPerThread, sizeof(SWrapperCfg));
13,888,920✔
785
  }
786

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

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

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

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

809
    (void)taosThreadAttrDestroy(&thAttr);
431,375✔
810
  }
811

812
  bool updateVnodesList = false;
694,446✔
813

814
  for (int32_t t = 0; t < threadNum; ++t) {
14,583,366✔
815
    SVnodeThread *pThread = &threads[t];
13,888,920✔
816
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
13,888,920✔
817
      (void)taosThreadJoin(pThread->thread, NULL);
431,375✔
818
      taosThreadClear(&pThread->thread);
431,375✔
819
    }
820
    taosMemoryFree(pThread->pCfgs);
13,888,920✔
821
    if (pThread->updateVnodesList) updateVnodesList = true;
13,888,920✔
822
  }
823
  taosMemoryFree(threads);
694,446✔
824
  taosMemoryFree(pCfgs);
694,446✔
825

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

836
#ifdef USE_MOUNT
837
  bool  updateMountList = false;
694,446✔
838
  void *pIter = NULL;
694,446✔
839
  while ((pIter = taosHashIterate(pMgmt->mountTfsHash, pIter))) {
695,114✔
840
    SMountTfs *pMountTfs = *(SMountTfs **)pIter;
668✔
841
    if (pMountTfs && atomic_load_32(&pMountTfs->nRef) <= 1) {
668✔
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) {
694,446✔
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);
694,446✔
866
  return 0;
694,446✔
867
}
868

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

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

876
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
3,591,496✔
877
    SVnodeObj *pVnode = pThread->ppVnodes[v];
1,822,399✔
878

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

884
    vmCloseVnode(pMgmt, pVnode, false, false);
1,821,666✔
885
  }
886

887
  dInfo("thread:%d, numOfVnodes:%d is closed", pThread->threadIndex, pThread->vnodeNum);
1,768,400✔
888
  return NULL;
1,769,097✔
889
}
890

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

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

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

911
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
694,446✔
912
  for (int32_t t = 0; t < threadNum; ++t) {
14,583,366✔
913
    threads[t].threadIndex = t;
13,888,920✔
914
    threads[t].pMgmt = pMgmt;
13,888,920✔
915
    threads[t].ppVnodes = taosMemoryCalloc(vnodesPerThread, sizeof(SVnode *));
13,888,920✔
916
  }
917

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

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

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

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

943
    (void)taosThreadAttrDestroy(&thAttr);
1,769,097✔
944
  }
945

946
  for (int32_t t = 0; t < threadNum; ++t) {
14,583,366✔
947
    SVnodeThread *pThread = &threads[t];
13,888,920✔
948
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
13,888,920✔
949
      (void)taosThreadJoin(pThread->thread, NULL);
1,769,097✔
950
      taosThreadClear(&pThread->thread);
1,769,097✔
951
    }
952
    taosMemoryFree(pThread->ppVnodes);
13,888,920✔
953
  }
954
  taosMemoryFree(threads);
694,446✔
955

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

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

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

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

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

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

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

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

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

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

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

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

1043
  while (1) {
435,665,609✔
1044
    lastTime++;
436,360,055✔
1045
    taosMsleep(100);
436,360,055✔
1046
    if (pMgmt->stop) break;
436,360,055✔
1047
    if (lastTime % 10 != 0) continue;
435,665,609✔
1048

1049
    int64_t sec = lastTime / 10;
43,251,881✔
1050
    if (sec % (VNODE_TIMEOUT_SEC / 2) == 0) {
43,251,881✔
1051
      vmCheckSyncTimeout(pMgmt);
1,151,274✔
1052
    }
1053
  }
1054

1055
  return NULL;
694,446✔
1056
}
1057

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

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

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

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

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

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

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

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

1134
  tmsgReportStartup("vnode-wal", "initialized");
694,446✔
1135

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

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

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

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

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

1165
  code = 0;
694,446✔
1166

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

1175
  return code;
694,446✔
1176
}
1177

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1270
    (void)taosThreadAttrDestroy(&thAttr);
431,375✔
1271
  }
1272

1273
  for (int32_t t = 0; t < threadNum; ++t) {
14,583,366✔
1274
    SVnodeThread *pThread = &threads[t];
13,888,920✔
1275
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
13,888,920✔
1276
      (void)taosThreadJoin(pThread->thread, NULL);
431,375✔
1277
      taosThreadClear(&pThread->thread);
431,375✔
1278
    }
1279
    taosMemoryFree(pThread->ppVnodes);
13,888,920✔
1280
  }
1281
  taosMemoryFree(threads);
694,446✔
1282

1283
  for (int32_t i = 0; i < numOfVnodes; ++i) {
1,132,487✔
1284
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
438,041✔
1285
    vmReleaseVnode(pMgmt, ppVnodes[i]);
438,041✔
1286
  }
1287

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

1292
  return vmInitTimer(pMgmt);
694,446✔
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); }
694,446✔
1304

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

1314
  return mgmtFunc;
705,563✔
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