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

taosdata / TDengine / #4907

30 Dec 2025 10:52AM UTC coverage: 65.541% (+0.03%) from 65.514%
#4907

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

808 existing lines in 106 files now uncovered.

193920 of 295877 relevant lines covered (65.54%)

118520209.34 hits per line

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

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

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

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

39
  SVnodeObj *pVnode = *ppVnode;
6,910,467✔
40

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

48
  taosMemoryFree(pVnode->path);
6,910,629✔
49
  taosMemoryFree(pVnode);
6,910,629✔
50
  ppVnode[0] = NULL;
6,910,629✔
51
}
52

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

62
  pCreatingVnode->vgId = vgId;
2,574,446✔
63
  pCreatingVnode->diskPrimary = diskId;
2,574,446✔
64

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

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

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

83
  return code;
2,574,446✔
84
}
85

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

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

101
  if (pOld) {
2,575,401✔
102
    dTrace("vgId:%d, free vnode pOld:%p", vgId, &pOld);
2,574,446✔
103
    vmFreeVnodeObj(&pOld);
2,574,446✔
104
  }
105

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

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

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

128
  diskId = tfsSearch(pTfs, 0, fname);
2,569,508✔
129
  if (diskId >= 0) {
2,573,596✔
130
    return diskId;
11✔
131
  }
132
  diskId = tfsSearch(pTfs, 0, fnameTmp);
2,573,585✔
133
  if (diskId >= 0) {
2,573,746✔
134
    return diskId;
×
135
  }
136

137
  // alloc
138
  int32_t     disks[TFS_MAX_DISKS_PER_TIER] = {0};
2,573,746✔
139
  int32_t     numOfVnodes = 0;
2,573,712✔
140
  SVnodeObj **ppVnodes = NULL;
2,574,412✔
141

142
  code = taosThreadMutexLock(&pMgmt->mutex);
2,573,493✔
143
  if (code != 0) {
2,574,446✔
144
    return code;
×
145
  }
146

147
  code = vmGetAllVnodeListFromHashWithCreating(pMgmt, &numOfVnodes, &ppVnodes);
2,574,446✔
148
  if (code != 0) {
2,574,446✔
149
    int32_t r = taosThreadMutexUnlock(&pMgmt->mutex);
×
150
    if (r != 0) {
×
151
      dError("vgId:%d, failed to unlock mutex since %s", vgId, tstrerror(r));
×
152
    }
153
    return code;
×
154
  }
155

156
  for (int32_t v = 0; v < numOfVnodes; v++) {
11,334,444✔
157
    SVnodeObj *pVnode = ppVnodes[v];
8,759,998✔
158
    disks[pVnode->diskPrimary] += 1;
8,759,998✔
159
  }
160

161
  int32_t minVal = INT_MAX;
2,574,446✔
162
  int32_t ndisk = tfsGetDisksAtLevel(pTfs, 0);
2,574,446✔
163
  diskId = 0;
2,574,446✔
164
  for (int32_t id = 0; id < ndisk; id++) {
5,373,178✔
165
    if (minVal > disks[id]) {
2,798,732✔
166
      minVal = disks[id];
2,622,653✔
167
      diskId = id;
2,622,653✔
168
    }
169
  }
170
  code = vmRegisterCreatingState(pMgmt, vgId, diskId);
2,574,446✔
171
  if (code != 0) {
2,574,446✔
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,574,446✔
180
  if (code != 0) {
2,574,446✔
181
    goto _OVER;
×
182
  }
183

184
_OVER:
2,574,446✔
185

186
  for (int32_t i = 0; i < numOfVnodes; ++i) {
11,334,444✔
187
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
8,759,998✔
188
    vmReleaseVnode(pMgmt, ppVnodes[i]);
8,759,998✔
189
  }
190
  if (ppVnodes != NULL) {
2,574,446✔
191
    taosMemoryFree(ppVnodes);
2,574,446✔
192
  }
193

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

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

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

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

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

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

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

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

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

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

247
  return code;
3,612,202✔
248
}
249

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

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

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

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

289
  return code;
723,981✔
290
}
291

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

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

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

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

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

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

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

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

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

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

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

451
  TAOS_RETURN(code);
3,612,202✔
452
}
453

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

459
  if (pVnode->pImpl && vnodeIsLeader(pVnode->pImpl)) {
3,612,202✔
460
    vnodeProposeCommitOnNeed(pVnode->pImpl, atExit);
2,774,536✔
461
  }
462

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

473
  vmReleaseVnode(pMgmt, pVnode);
3,612,202✔
474

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

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

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

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

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

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

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

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

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

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

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

515
  vmFreeQueue(pMgmt, pVnode);
3,612,202✔
516

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

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

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

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

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

557
  vmFreeVnodeObj(&pVnode);
3,612,202✔
558
}
559

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

769
  pMgmt->state.totalVnodes = numOfVnodes;
537,564✔
770

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

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

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

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

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

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

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

810
    (void)taosThreadAttrDestroy(&thAttr);
287,040✔
811
  }
812

813
  bool updateVnodesList = false;
537,564✔
814

815
  for (int32_t t = 0; t < threadNum; ++t) {
11,288,844✔
816
    SVnodeThread *pThread = &threads[t];
10,751,280✔
817
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
10,751,280✔
818
      (void)taosThreadJoin(pThread->thread, NULL);
287,040✔
819
      taosThreadClear(&pThread->thread);
287,040✔
820
    }
821
    taosMemoryFree(pThread->pCfgs);
10,751,280✔
822
    if (pThread->updateVnodesList) updateVnodesList = true;
10,751,280✔
823
  }
824
  taosMemoryFree(threads);
537,564✔
825
  taosMemoryFree(pCfgs);
537,564✔
826

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

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

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

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

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

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

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

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

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

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

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

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

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

912
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
537,564✔
913
  for (int32_t t = 0; t < threadNum; ++t) {
11,288,844✔
914
    threads[t].threadIndex = t;
10,751,280✔
915
    threads[t].pMgmt = pMgmt;
10,751,280✔
916
    threads[t].ppVnodes = taosMemoryCalloc(vnodesPerThread, sizeof(SVnode *));
10,751,280✔
917
  }
918

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

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

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

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

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

947
  for (int32_t t = 0; t < threadNum; ++t) {
11,288,844✔
948
    SVnodeThread *pThread = &threads[t];
10,751,280✔
949
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
10,751,280✔
950
      (void)taosThreadJoin(pThread->thread, NULL);
1,442,766✔
951
      taosThreadClear(&pThread->thread);
1,442,766✔
952
    }
953
    taosMemoryFree(pThread->ppVnodes);
10,751,280✔
954
  }
955
  taosMemoryFree(threads);
537,564✔
956

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

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

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

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

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

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

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

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

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

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

1027
  if (ppVnodes != NULL) {
1,587,895✔
1028
    for (int32_t i = 0; i < numOfVnodes; ++i) {
6,299,869✔
1029
      SVnodeObj *pVnode = ppVnodes[i];
4,711,974✔
1030
      if (!pVnode->failed) {
4,711,974✔
1031
        vnodeSyncCheckTimeout(pVnode->pImpl);
4,711,974✔
1032
      }
1033
      vmReleaseVnode(pMgmt, pVnode);
4,711,974✔
1034
    }
1035
    taosMemoryFree(ppVnodes);
1,587,895✔
1036
  }
1037
}
1038

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

1044
  while (1) {
543,876,703✔
1045
    lastTime++;
544,414,267✔
1046
    taosMsleep(100);
544,414,267✔
1047
    if (pMgmt->stop) break;
544,414,267✔
1048
    if (lastTime % 10 != 0) continue;
543,876,703✔
1049

1050
    int64_t sec = lastTime / 10;
54,156,716✔
1051
    if (sec % (VNODE_TIMEOUT_SEC / 2) == 0) {
54,156,716✔
1052
      vmCheckSyncTimeout(pMgmt);
1,587,895✔
1053
    }
1054
  }
1055

1056
  return NULL;
537,564✔
1057
}
1058

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

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

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

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

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

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

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

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

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

1135
  tmsgReportStartup("vnode-wal", "initialized");
537,564✔
1136

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

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

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

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

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

1166
  code = 0;
537,564✔
1167

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

1176
  return code;
537,564✔
1177
}
1178

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1271
    (void)taosThreadAttrDestroy(&thAttr);
287,040✔
1272
  }
1273

1274
  for (int32_t t = 0; t < threadNum; ++t) {
11,288,844✔
1275
    SVnodeThread *pThread = &threads[t];
10,751,280✔
1276
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
10,751,280✔
1277
      (void)taosThreadJoin(pThread->thread, NULL);
287,040✔
1278
      taosThreadClear(&pThread->thread);
287,040✔
1279
    }
1280
    taosMemoryFree(pThread->ppVnodes);
10,751,280✔
1281
  }
1282
  taosMemoryFree(threads);
537,564✔
1283

1284
  for (int32_t i = 0; i < numOfVnodes; ++i) {
826,224✔
1285
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
288,660✔
1286
    vmReleaseVnode(pMgmt, ppVnodes[i]);
288,660✔
1287
  }
1288

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

1293
  return vmInitTimer(pMgmt);
537,564✔
1294

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

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

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

1315
  return mgmtFunc;
540,689✔
1316
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc