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

taosdata / TDengine / #3629

04 Mar 2025 01:45PM UTC coverage: 63.692% (-0.1%) from 63.79%
#3629

push

travis-ci

web-flow
Merge pull request #30007 from taosdata/revert-29951-docs/update-exception-handling-strategy

Revert "docs: update exception handling strategy"

149369 of 300378 branches covered (49.73%)

Branch coverage included in aggregate %.

233614 of 300930 relevant lines covered (77.63%)

18792670.99 hits per line

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

61.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) {
10,056✔
24
  int32_t    diskId = -1;
10,056✔
25
  SVnodeObj *pVnode = NULL;
10,056✔
26

27
  (void)taosThreadRwlockRdlock(&pMgmt->hashLock);
10,056✔
28
  int32_t r = taosHashGetDup(pMgmt->runngingHash, &vgId, sizeof(int32_t), (void *)&pVnode);
10,058✔
29
  if (pVnode != NULL) {
10,054!
30
    diskId = pVnode->diskPrimary;
×
31
  }
32
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
10,054✔
33
  return diskId;
10,060✔
34
}
35

36
static void vmFreeVnodeObj(SVnodeObj **ppVnode) {
23,560✔
37
  if (!ppVnode || !(*ppVnode)) return;
23,560!
38

39
  SVnodeObj *pVnode = *ppVnode;
23,560✔
40

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

48
  taosMemoryFree(pVnode->path);
23,560!
49
  taosMemoryFree(pVnode);
23,560!
50
  ppVnode[0] = NULL;
23,560✔
51
}
52

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

62
  pCreatingVnode->vgId = vgId;
10,060✔
63
  pCreatingVnode->diskPrimary = diskId;
10,060✔
64

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

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

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

83
  return code;
10,060✔
84
}
85

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

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

101
  if (pOld) {
10,060!
102
    dTrace("vgId:%d, free vnode pOld:%p", vgId, &pOld);
10,060✔
103
    vmFreeVnodeObj(&pOld);
10,060✔
104
  }
105

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

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

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

128
  diskId = tfsSearch(pTfs, 0, fname);
10,055✔
129
  if (diskId >= 0) {
10,033!
130
    return diskId;
×
131
  }
132
  diskId = tfsSearch(pTfs, 0, fnameTmp);
10,033✔
133
  if (diskId >= 0) {
10,029!
134
    return diskId;
×
135
  }
136

137
  // alloc
138
  int32_t     disks[TFS_MAX_DISKS_PER_TIER] = {0};
10,029✔
139
  int32_t     numOfVnodes = 0;
10,029✔
140
  SVnodeObj **ppVnodes = NULL;
10,029✔
141

142
  code = taosThreadMutexLock(&pMgmt->mutex);
10,029✔
143
  if (code != 0) {
10,060!
144
    return code;
×
145
  }
146

147
  code = vmGetAllVnodeListFromHashWithCreating(pMgmt, &numOfVnodes, &ppVnodes);
10,060✔
148
  if (code != 0) {
10,060!
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++) {
52,422✔
157
    SVnodeObj *pVnode = ppVnodes[v];
42,362✔
158
    disks[pVnode->diskPrimary] += 1;
42,362✔
159
  }
160

161
  int32_t minVal = INT_MAX;
10,060✔
162
  int32_t ndisk = tfsGetDisksAtLevel(pTfs, 0);
10,060✔
163
  diskId = 0;
10,060✔
164
  for (int32_t id = 0; id < ndisk; id++) {
20,127✔
165
    if (minVal > disks[id]) {
10,067✔
166
      minVal = disks[id];
10,062✔
167
      diskId = id;
10,062✔
168
    }
169
  }
170
  code = vmRegisterCreatingState(pMgmt, vgId, diskId);
10,060✔
171
  if (code != 0) {
10,060!
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);
10,060✔
180
  if (code != 0) {
10,060!
181
    goto _OVER;
×
182
  }
183

184
_OVER:
10,060✔
185

186
  for (int32_t i = 0; i < numOfVnodes; ++i) {
52,422✔
187
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
42,362!
188
    vmReleaseVnode(pMgmt, ppVnodes[i]);
42,362✔
189
  }
190
  if (ppVnodes != NULL) {
10,060!
191
    taosMemoryFree(ppVnodes);
10,060!
192
  }
193

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

203
void vmCleanPrimaryDisk(SVnodeMgmt *pMgmt, int32_t vgId) { vmUnRegisterCreatingState(pMgmt, vgId); }
10,060✔
204

205
SVnodeObj *vmAcquireVnodeImpl(SVnodeMgmt *pMgmt, int32_t vgId, bool strict) {
44,334,162✔
206
  SVnodeObj *pVnode = NULL;
44,334,162✔
207

208
  (void)taosThreadRwlockRdlock(&pMgmt->hashLock);
44,334,162✔
209
  int32_t r = taosHashGetDup(pMgmt->runngingHash, &vgId, sizeof(int32_t), (void *)&pVnode);
44,416,522✔
210
  if (pVnode == NULL || strict && (pVnode->dropped || pVnode->failed)) {
44,408,337!
211
    terrno = TSDB_CODE_VND_INVALID_VGROUP_ID;
46,244✔
212
    pVnode = NULL;
52,587✔
213
  } else {
214
    int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
44,362,093✔
215
    dTrace("vgId:%d, acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
44,358,617✔
216
  }
217
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
44,411,205✔
218

219
  return pVnode;
44,415,643✔
220
}
221

222
SVnodeObj *vmAcquireVnode(SVnodeMgmt *pMgmt, int32_t vgId) { return vmAcquireVnodeImpl(pMgmt, vgId, true); }
44,326,278✔
223

224
void vmReleaseVnode(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
44,543,782✔
225
  if (pVnode == NULL) return;
44,543,782!
226

227
  //(void)taosThreadRwlockRdlock(&pMgmt->lock);
228
  int32_t refCount = atomic_sub_fetch_32(&pVnode->refCount, 1);
44,543,782✔
229
  dTrace("vgId:%d, release vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
44,560,278✔
230
  //(void)taosThreadRwlockUnlock(&pMgmt->lock);
231
}
232

233
static int32_t vmRegisterRunningState(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
12,291✔
234
  SVnodeObj *pOld = NULL;
12,291✔
235

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

245
  return code;
12,291✔
246
}
247

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

256
static int32_t vmRegisterClosedState(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
1,238✔
257
  int32_t    code = 0;
1,238✔
258
  SVnodeObj *pClosedVnode = taosMemoryCalloc(1, sizeof(SVnodeObj));
1,238!
259
  if (pClosedVnode == NULL) {
1,238!
260
    dError("failed to alloc vnode since %s", terrstr());
×
261
    return terrno;
×
262
  }
263
  (void)memset(pClosedVnode, 0, sizeof(SVnodeObj));
1,238✔
264

265
  pClosedVnode->vgId = pVnode->vgId;
1,238✔
266
  pClosedVnode->dropped = pVnode->dropped;
1,238✔
267
  pClosedVnode->vgVersion = pVnode->vgVersion;
1,238✔
268
  pClosedVnode->diskPrimary = pVnode->diskPrimary;
1,238✔
269
  pClosedVnode->toVgId = pVnode->toVgId;
1,238✔
270

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

285
  return code;
1,238✔
286
}
287

288
static void vmUnRegisterClosedState(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
12,291✔
289
  SVnodeObj *pOld = NULL;
12,291✔
290
  int32_t    r = taosHashGetDup(pMgmt->closedHash, &pVnode->vgId, sizeof(int32_t), (void *)&pOld);
12,291✔
291
  if (r != 0) {
12,291!
292
    dError("vgId:%d, failed to get vnode from closedHash", pVnode->vgId);
×
293
  }
294
  if (pOld != NULL) {
12,291✔
295
    vmFreeVnodeObj(&pOld);
1,238✔
296
    dInfo("vgId:%d, remove from closedHash", pVnode->vgId);
1,238!
297
    r = taosHashRemove(pMgmt->closedHash, &pVnode->vgId, sizeof(int32_t));
1,238✔
298
    if (r != 0) {
1,238!
299
      dError("vgId:%d, failed to remove vnode from hash", pVnode->vgId);
×
300
    }
301
  }
302
}
12,291✔
303

304
int32_t vmOpenVnode(SVnodeMgmt *pMgmt, SWrapperCfg *pCfg, SVnode *pImpl) {
12,291✔
305
  SVnodeObj *pVnode = taosMemoryCalloc(1, sizeof(SVnodeObj));
12,291!
306
  if (pVnode == NULL) {
12,291!
307
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
308
    return -1;
×
309
  }
310

311
  pVnode->vgId = pCfg->vgId;
12,291✔
312
  pVnode->vgVersion = pCfg->vgVersion;
12,291✔
313
  pVnode->diskPrimary = pCfg->diskPrimary;
12,291✔
314
  pVnode->refCount = 0;
12,291✔
315
  pVnode->dropped = 0;
12,291✔
316
  pVnode->failed = 0;
12,291✔
317
  pVnode->path = taosStrdup(pCfg->path);
12,291!
318
  pVnode->pImpl = pImpl;
12,291✔
319

320
  if (pVnode->path == NULL) {
12,291!
321
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
322
    taosMemoryFree(pVnode);
×
323
    return -1;
×
324
  }
325

326
  if (pImpl) {
12,291!
327
    if (vmAllocQueue(pMgmt, pVnode) != 0) {
12,291!
328
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
329
      taosMemoryFree(pVnode->path);
×
330
      taosMemoryFree(pVnode);
×
331
      return -1;
×
332
    }
333
  } else {
334
    pVnode->failed = 1;
×
335
  }
336

337
  (void)taosThreadRwlockWrlock(&pMgmt->hashLock);
12,291✔
338
  int32_t code = vmRegisterRunningState(pMgmt, pVnode);
12,291✔
339
  vmUnRegisterClosedState(pMgmt, pVnode);
12,291✔
340
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
12,291✔
341

342
  return code;
12,291✔
343
}
344

345
void vmCloseVnode(SVnodeMgmt *pMgmt, SVnodeObj *pVnode, bool commitAndRemoveWal, bool keepClosed) {
12,282✔
346
  char path[TSDB_FILENAME_LEN] = {0};
12,282✔
347
  bool atExit = true;
12,282✔
348

349
  if (pVnode->pImpl && vnodeIsLeader(pVnode->pImpl)) {
12,282✔
350
    vnodeProposeCommitOnNeed(pVnode->pImpl, atExit);
10,329✔
351
  }
352

353
  (void)taosThreadRwlockWrlock(&pMgmt->hashLock);
12,279✔
354
  vmUnRegisterRunningState(pMgmt, pVnode->vgId);
12,282✔
355
  if (keepClosed) {
12,282✔
356
    if (vmRegisterClosedState(pMgmt, pVnode) != 0) {
1,238!
357
      (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
×
358
      return;
×
359
    };
360
  }
361
  (void)taosThreadRwlockUnlock(&pMgmt->hashLock);
12,282✔
362

363
  vmReleaseVnode(pMgmt, pVnode);
12,282✔
364

365
  if (pVnode->failed) {
12,282!
366
    goto _closed;
×
367
  }
368
  dInfo("vgId:%d, pre close", pVnode->vgId);
12,282!
369
  vnodePreClose(pVnode->pImpl);
12,282✔
370

371
  dInfo("vgId:%d, wait for vnode ref become 0", pVnode->vgId);
12,276!
372
  while (pVnode->refCount > 0) taosMsleep(10);
12,282!
373

374
  dInfo("vgId:%d, wait for vnode write queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pWriteW.queue,
12,282!
375
        taosQueueGetThreadId(pVnode->pWriteW.queue));
376
  tMultiWorkerCleanup(&pVnode->pWriteW);
12,282✔
377

378
  dInfo("vgId:%d, wait for vnode sync queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pSyncW.queue,
12,282!
379
        taosQueueGetThreadId(pVnode->pSyncW.queue));
380
  tMultiWorkerCleanup(&pVnode->pSyncW);
12,282✔
381

382
  dInfo("vgId:%d, wait for vnode sync rd queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pSyncRdW.queue,
12,282!
383
        taosQueueGetThreadId(pVnode->pSyncRdW.queue));
384
  tMultiWorkerCleanup(&pVnode->pSyncRdW);
12,282✔
385

386
  dInfo("vgId:%d, wait for vnode apply queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pApplyW.queue,
12,282!
387
        taosQueueGetThreadId(pVnode->pApplyW.queue));
388
  tMultiWorkerCleanup(&pVnode->pApplyW);
12,282✔
389

390
  dInfo("vgId:%d, wait for vnode fetch queue:%p is empty, thread:%08" PRId64, pVnode->vgId, pVnode->pFetchQ,
12,281!
391
        taosQueueGetThreadId(pVnode->pFetchQ));
392
  while (!taosQueueEmpty(pVnode->pFetchQ)) taosMsleep(10);
12,282!
393

394
  dInfo("vgId:%d, wait for vnode query queue:%p is empty", pVnode->vgId, pVnode->pQueryQ);
12,281!
395
  while (!taosQueueEmpty(pVnode->pQueryQ)) taosMsleep(10);
12,284✔
396

397
  tqNotifyClose(pVnode->pImpl->pTq);
12,281✔
398

399
  dInfo("vgId:%d, wait for vnode stream queue:%p is empty, %d remains", pVnode->vgId,
12,261!
400
        pVnode->pStreamQ, taosQueueItemSize(pVnode->pStreamQ));
401
  while (!taosQueueEmpty(pVnode->pStreamQ)) taosMsleep(50);
12,262!
402

403
  dInfo("vgId:%d, wait for vnode stream ctrl queue:%p is empty", pVnode->vgId, pVnode->pStreamCtrlQ);
12,262!
404
  while (!taosQueueEmpty(pVnode->pStreamCtrlQ)) taosMsleep(50);
12,262!
405

406
  dInfo("vgId:%d, wait for vnode stream long-exec queue:%p is empty, %d remains", pVnode->vgId,
12,262!
407
        pVnode->pStreamLongExecQ, taosQueueItemSize(pVnode->pStreamLongExecQ));
408
  while (!taosQueueEmpty(pVnode->pStreamLongExecQ)) taosMsleep(50);
12,262!
409

410
  dInfo("vgId:%d, all vnode queues is empty", pVnode->vgId);
12,262!
411

412
  dInfo("vgId:%d, post close", pVnode->vgId);
12,262!
413
  vnodePostClose(pVnode->pImpl);
12,262✔
414

415
  vmFreeQueue(pMgmt, pVnode);
12,255✔
416

417
  if (commitAndRemoveWal) {
12,261✔
418
    dInfo("vgId:%d, commit data for vnode split", pVnode->vgId);
32!
419
    if (vnodeSyncCommit(pVnode->pImpl) != 0) {
32!
420
      dError("vgId:%d, failed to commit data", pVnode->vgId);
×
421
    }
422
    if (vnodeBegin(pVnode->pImpl) != 0) {
32!
423
      dError("vgId:%d, failed to begin", pVnode->vgId);
×
424
    }
425
    dInfo("vgId:%d, commit data finished", pVnode->vgId);
32!
426
  }
427

428
  int32_t nodeId = vnodeNodeId(pVnode->pImpl);
12,261✔
429
  vnodeClose(pVnode->pImpl);
12,262✔
430
  pVnode->pImpl = NULL;
12,262✔
431

432
_closed:
12,262✔
433
  dInfo("vgId:%d, vnode is closed", pVnode->vgId);
12,262!
434

435
  if (commitAndRemoveWal) {
12,262✔
436
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d%swal", TD_DIRSEP, pVnode->vgId, TD_DIRSEP);
32✔
437
    dInfo("vgId:%d, remove all wals, path:%s", pVnode->vgId, path);
32!
438
    if (tfsRmdir(pMgmt->pTfs, path) != 0) {
32!
439
      dTrace("vgId:%d, failed to remove wals, path:%s", pVnode->vgId, path);
×
440
    }
441
    if (tfsMkdir(pMgmt->pTfs, path) != 0) {
32!
442
      dTrace("vgId:%d, failed to create wals, path:%s", pVnode->vgId, path);
×
443
    }
444
  }
445

446
  if (pVnode->dropped) {
12,262✔
447
    dInfo("vgId:%d, vnode is destroyed, dropped:%d", pVnode->vgId, pVnode->dropped);
4,552!
448
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pVnode->vgId);
4,552✔
449
    vnodeDestroy(pVnode->vgId, path, pMgmt->pTfs, nodeId);
4,552✔
450
  }
451

452
  vmFreeVnodeObj(&pVnode);
12,262✔
453
}
454

455
void vmCloseFailedVnode(SVnodeMgmt *pMgmt, int32_t vgId) {
×
456
  int32_t r = 0;
×
457
  r = taosThreadRwlockWrlock(&pMgmt->hashLock);
×
458
  if (r != 0) {
×
459
    dError("vgId:%d, failed to lock since %s", vgId, tstrerror(r));
×
460
  }
461
  if (r == 0) {
×
462
    vmUnRegisterRunningState(pMgmt, vgId);
×
463
  }
464
  r = taosThreadRwlockUnlock(&pMgmt->hashLock);
×
465
  if (r != 0) {
×
466
    dError("vgId:%d, failed to unlock since %s", vgId, tstrerror(r));
×
467
  }
468
}
×
469

470
static int32_t vmRestoreVgroupId(SWrapperCfg *pCfg, STfs *pTfs) {
×
471
  int32_t srcVgId = pCfg->vgId;
×
472
  int32_t dstVgId = pCfg->toVgId;
×
473
  if (dstVgId == 0) return 0;
×
474

475
  char srcPath[TSDB_FILENAME_LEN];
476
  char dstPath[TSDB_FILENAME_LEN];
477

478
  snprintf(srcPath, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, srcVgId);
×
479
  snprintf(dstPath, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, dstVgId);
×
480

481
  int32_t diskPrimary = pCfg->diskPrimary;
×
482
  int32_t vgId = vnodeRestoreVgroupId(srcPath, dstPath, srcVgId, dstVgId, diskPrimary, pTfs);
×
483
  if (vgId <= 0) {
×
484
    dError("vgId:%d, failed to restore vgroup id. srcPath: %s", pCfg->vgId, srcPath);
×
485
    return -1;
×
486
  }
487

488
  pCfg->vgId = vgId;
×
489
  pCfg->toVgId = 0;
×
490
  return 0;
×
491
}
492

493
static void *vmOpenVnodeInThread(void *param) {
946✔
494
  SVnodeThread *pThread = param;
946✔
495
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
946✔
496
  char          path[TSDB_FILENAME_LEN];
497

498
  dInfo("thread:%d, start to open or destroy %d vnodes", pThread->threadIndex, pThread->vnodeNum);
946!
499
  setThreadName("open-vnodes");
946✔
500

501
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
1,907✔
502
    SWrapperCfg *pCfg = &pThread->pCfgs[v];
961✔
503
    if (pCfg->dropped) {
961!
504
      char stepDesc[TSDB_STEP_DESC_LEN] = {0};
×
505
      snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to destroy, %d of %d have been dropped", pCfg->vgId,
×
506
               pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
507
      tmsgReportStartup("vnode-destroy", stepDesc);
×
508

509
      snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pCfg->vgId);
×
510
      vnodeDestroy(pCfg->vgId, path, pMgmt->pTfs, 0);
×
511
      pThread->updateVnodesList = true;
×
512
      pThread->dropped++;
×
513
      (void)atomic_add_fetch_32(&pMgmt->state.dropVnodes, 1);
×
514
      continue;
×
515
    }
516

517
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
961✔
518
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been opened", pCfg->vgId,
961✔
519
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
520
    tmsgReportStartup("vnode-open", stepDesc);
961✔
521

522
    if (pCfg->toVgId) {
961!
523
      if (vmRestoreVgroupId(pCfg, pMgmt->pTfs) != 0) {
×
524
        dError("vgId:%d, failed to restore vgroup id by thread:%d", pCfg->vgId, pThread->threadIndex);
×
525
        pThread->failed++;
×
526
        continue;
×
527
      }
528
      pThread->updateVnodesList = true;
×
529
    }
530

531
    int32_t diskPrimary = pCfg->diskPrimary;
961✔
532
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pCfg->vgId);
961✔
533

534
    SVnode *pImpl = vnodeOpen(path, diskPrimary, pMgmt->pTfs, pMgmt->msgCb, false);
961✔
535

536
    if (pImpl == NULL) {
961!
537
      dError("vgId:%d, failed to open vnode by thread:%d since %s", pCfg->vgId, pThread->threadIndex, terrstr());
×
538
      if (terrno != TSDB_CODE_NEED_RETRY) {
×
539
        pThread->failed++;
×
540
        continue;
×
541
      }
542
    }
543

544
    if (vmOpenVnode(pMgmt, pCfg, pImpl) != 0) {
961!
545
      dError("vgId:%d, failed to open vnode by thread:%d", pCfg->vgId, pThread->threadIndex);
×
546
      pThread->failed++;
×
547
      continue;
×
548
    }
549

550
    dInfo("vgId:%d, is opened by thread:%d", pCfg->vgId, pThread->threadIndex);
961!
551
    pThread->opened++;
961✔
552
    (void)atomic_add_fetch_32(&pMgmt->state.openVnodes, 1);
961✔
553
  }
554

555
  dInfo("thread:%d, numOfVnodes:%d, opened:%d dropped:%d failed:%d", pThread->threadIndex, pThread->vnodeNum,
946!
556
        pThread->opened, pThread->dropped, pThread->failed);
557
  return NULL;
946✔
558
}
559

560
static int32_t vmOpenVnodes(SVnodeMgmt *pMgmt) {
2,230✔
561
  pMgmt->runngingHash =
2,230✔
562
      taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
2,230✔
563
  if (pMgmt->runngingHash == NULL) {
2,230!
564
    dError("failed to init vnode hash since %s", terrstr());
×
565
    return TSDB_CODE_OUT_OF_MEMORY;
×
566
  }
567

568
  pMgmt->closedHash =
2,230✔
569
      taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
2,230✔
570
  if (pMgmt->closedHash == NULL) {
2,230!
571
    dError("failed to init vnode closed hash since %s", terrstr());
×
572
    return TSDB_CODE_OUT_OF_MEMORY;
×
573
  }
574

575
  pMgmt->creatingHash =
2,230✔
576
      taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
2,230✔
577
  if (pMgmt->creatingHash == NULL) {
2,230!
578
    dError("failed to init vnode creatingHash hash since %s", terrstr());
×
579
    return TSDB_CODE_OUT_OF_MEMORY;
×
580
  }
581

582
  SWrapperCfg *pCfgs = NULL;
2,230✔
583
  int32_t      numOfVnodes = 0;
2,230✔
584
  if (vmGetVnodeListFromFile(pMgmt, &pCfgs, &numOfVnodes) != 0) {
2,230!
585
    dInfo("failed to get vnode list from disk since %s", terrstr());
×
586
    return -1;
×
587
  }
588

589
  pMgmt->state.totalVnodes = numOfVnodes;
2,230✔
590

591
  int32_t threadNum = tsNumOfCores / 2;
2,230✔
592
  if (threadNum < 1) threadNum = 1;
2,230!
593
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
2,230✔
594

595
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
2,230!
596
  if (threads == NULL) {
2,230!
597
    dError("failed to allocate memory for threads since %s", terrstr());
×
598
    taosMemoryFree(pCfgs);
×
599
    return terrno;
×
600
  }
601

602
  for (int32_t t = 0; t < threadNum; ++t) {
46,830✔
603
    threads[t].threadIndex = t;
44,600✔
604
    threads[t].pMgmt = pMgmt;
44,600✔
605
    threads[t].pCfgs = taosMemoryCalloc(vnodesPerThread, sizeof(SWrapperCfg));
44,600!
606
  }
607

608
  for (int32_t v = 0; v < numOfVnodes; ++v) {
3,191✔
609
    int32_t       t = v % threadNum;
961✔
610
    SVnodeThread *pThread = &threads[t];
961✔
611
    pThread->pCfgs[pThread->vnodeNum++] = pCfgs[v];
961✔
612
  }
613

614
  dInfo("open %d vnodes with %d threads", numOfVnodes, threadNum);
2,230!
615

616
  for (int32_t t = 0; t < threadNum; ++t) {
46,830✔
617
    SVnodeThread *pThread = &threads[t];
44,600✔
618
    if (pThread->vnodeNum == 0) continue;
44,600✔
619

620
    TdThreadAttr thAttr;
621
    (void)taosThreadAttrInit(&thAttr);
946✔
622
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
946✔
623
    if (taosThreadCreate(&pThread->thread, &thAttr, vmOpenVnodeInThread, pThread) != 0) {
946!
624
      dError("thread:%d, failed to create thread to open vnode, reason:%s", pThread->threadIndex, strerror(errno));
×
625
    }
626

627
    (void)taosThreadAttrDestroy(&thAttr);
946✔
628
  }
629

630
  bool updateVnodesList = false;
2,230✔
631

632
  for (int32_t t = 0; t < threadNum; ++t) {
46,830✔
633
    SVnodeThread *pThread = &threads[t];
44,600✔
634
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
44,600!
635
      (void)taosThreadJoin(pThread->thread, NULL);
946✔
636
      taosThreadClear(&pThread->thread);
946✔
637
    }
638
    taosMemoryFree(pThread->pCfgs);
44,600!
639
    if (pThread->updateVnodesList) updateVnodesList = true;
44,600!
640
  }
641
  taosMemoryFree(threads);
2,230!
642
  taosMemoryFree(pCfgs);
2,230!
643

644
  if ((pMgmt->state.openVnodes + pMgmt->state.dropVnodes) != pMgmt->state.totalVnodes) {
2,230!
645
    dError("there are total vnodes:%d, opened:%d", pMgmt->state.totalVnodes, pMgmt->state.openVnodes);
×
646
    terrno = TSDB_CODE_VND_INIT_FAILED;
×
647
    return -1;
×
648
  }
649

650
  if (updateVnodesList && vmWriteVnodeListToFile(pMgmt) != 0) {
2,230!
651
    dError("failed to write vnode list since %s", terrstr());
×
652
    return -1;
×
653
  }
654

655
  dInfo("successfully opened %d vnodes", pMgmt->state.totalVnodes);
2,230!
656
  return 0;
2,230✔
657
}
658

659
static void *vmCloseVnodeInThread(void *param) {
6,384✔
660
  SVnodeThread *pThread = param;
6,384✔
661
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
6,384✔
662

663
  dInfo("thread:%d, start to close %d vnodes", pThread->threadIndex, pThread->vnodeNum);
6,384!
664
  setThreadName("close-vnodes");
6,390✔
665

666
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
12,832✔
667
    SVnodeObj *pVnode = pThread->ppVnodes[v];
6,459✔
668

669
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
6,459✔
670
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to close, %d of %d have been closed", pVnode->vgId,
6,459✔
671
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
672
    tmsgReportStartup("vnode-close", stepDesc);
6,459✔
673

674
    vmCloseVnode(pMgmt, pVnode, false, false);
6,460✔
675
  }
676

677
  dInfo("thread:%d, numOfVnodes:%d is closed", pThread->threadIndex, pThread->vnodeNum);
6,373!
678
  return NULL;
6,373✔
679
}
680

681
static void vmCloseVnodes(SVnodeMgmt *pMgmt) {
2,230✔
682
  int32_t code = 0;
2,230✔
683
  dInfo("start to close all vnodes");
2,230!
684
  tSingleWorkerCleanup(&pMgmt->mgmtWorker);
2,230✔
685
  dInfo("vnodes mgmt worker is stopped");
2,230!
686
  tSingleWorkerCleanup(&pMgmt->mgmtMultiWorker);
2,230✔
687
  dInfo("vnodes multiple mgmt worker is stopped");
2,230!
688

689
  int32_t     numOfVnodes = 0;
2,230✔
690
  SVnodeObj **ppVnodes = NULL;
2,230✔
691
  code = vmGetVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes);
2,230✔
692
  if (code != 0) {
2,230!
693
    dError("failed to get vnode list since %s", tstrerror(code));
×
694
    return;
×
695
  }
696

697
  int32_t threadNum = tsNumOfCores / 2;
2,230✔
698
  if (threadNum < 1) threadNum = 1;
2,230!
699
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
2,230✔
700

701
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
2,230!
702
  for (int32_t t = 0; t < threadNum; ++t) {
46,830✔
703
    threads[t].threadIndex = t;
44,600✔
704
    threads[t].pMgmt = pMgmt;
44,600✔
705
    threads[t].ppVnodes = taosMemoryCalloc(vnodesPerThread, sizeof(SVnode *));
44,600!
706
  }
707

708
  for (int32_t v = 0; v < numOfVnodes; ++v) {
8,699✔
709
    int32_t       t = v % threadNum;
6,469✔
710
    SVnodeThread *pThread = &threads[t];
6,469✔
711
    if (pThread->ppVnodes != NULL && ppVnodes != NULL) {
6,469!
712
      pThread->ppVnodes[pThread->vnodeNum++] = ppVnodes[v];
6,469✔
713
    }
714
  }
715

716
  pMgmt->state.openVnodes = 0;
2,230✔
717
  dInfo("close %d vnodes with %d threads", numOfVnodes, threadNum);
2,230!
718

719
  int64_t st = taosGetTimestampMs();
2,230✔
720
  dInfo("notify all streams closed in all %d vnodes, ts:%" PRId64, numOfVnodes, st);
2,230!
721
  if (ppVnodes != NULL) {
2,230!
722
    for (int32_t i = 0; i < numOfVnodes; ++i) {
8,699✔
723
      if (ppVnodes[i] != NULL) {
6,469!
724
        if (ppVnodes[i]->pImpl != NULL) {
6,469!
725
          tqNotifyClose(ppVnodes[i]->pImpl->pTq);
6,469✔
726
        }
727
      }
728
    }
729
  }
730

731
  int64_t et = taosGetTimestampMs();
2,230✔
732
  dInfo("notify close stream completed in %d vnodes, elapsed time: %" PRId64 "ms", numOfVnodes, et - st);
2,230!
733

734
  for (int32_t t = 0; t < threadNum; ++t) {
46,830✔
735
    SVnodeThread *pThread = &threads[t];
44,600✔
736
    if (pThread->vnodeNum == 0) continue;
44,600✔
737

738
    TdThreadAttr thAttr;
739
    (void)taosThreadAttrInit(&thAttr);
6,393✔
740
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
6,393✔
741

742
    if (taosThreadCreate(&pThread->thread, &thAttr, vmCloseVnodeInThread, pThread) != 0) {
6,393!
743
      dError("thread:%d, failed to create thread to close vnode since %s", pThread->threadIndex, strerror(errno));
×
744
    }
745

746
    (void)taosThreadAttrDestroy(&thAttr);
6,393✔
747
  }
748

749
  for (int32_t t = 0; t < threadNum; ++t) {
46,810✔
750
    SVnodeThread *pThread = &threads[t];
44,581✔
751
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
44,581!
752
      (void)taosThreadJoin(pThread->thread, NULL);
6,374✔
753
      taosThreadClear(&pThread->thread);
6,373✔
754
    }
755
    taosMemoryFree(pThread->ppVnodes);
44,580!
756
  }
757
  taosMemoryFree(threads);
2,229!
758

759
  if (ppVnodes != NULL) {
2,229!
760
    taosMemoryFree(ppVnodes);
2,229!
761
  }
762

763
  if (pMgmt->runngingHash != NULL) {
2,229!
764
    taosHashCleanup(pMgmt->runngingHash);
2,229✔
765
    pMgmt->runngingHash = NULL;
2,229✔
766
  }
767

768
  void *pIter = taosHashIterate(pMgmt->closedHash, NULL);
2,229✔
769
  while (pIter) {
2,229!
770
    SVnodeObj **ppVnode = pIter;
×
771
    vmFreeVnodeObj(ppVnode);
×
772
    pIter = taosHashIterate(pMgmt->closedHash, pIter);
×
773
  }
774

775
  if (pMgmt->closedHash != NULL) {
2,229!
776
    taosHashCleanup(pMgmt->closedHash);
2,229✔
777
    pMgmt->closedHash = NULL;
2,229✔
778
  }
779

780
  pIter = taosHashIterate(pMgmt->creatingHash, NULL);
2,229✔
781
  while (pIter) {
2,229!
782
    SVnodeObj **ppVnode = pIter;
×
783
    vmFreeVnodeObj(ppVnode);
×
784
    pIter = taosHashIterate(pMgmt->creatingHash, pIter);
×
785
  }
786

787
  if (pMgmt->creatingHash != NULL) {
2,229!
788
    taosHashCleanup(pMgmt->creatingHash);
2,229✔
789
    pMgmt->creatingHash = NULL;
2,229✔
790
  }
791

792
  dInfo("total vnodes:%d are all closed", numOfVnodes);
2,229!
793
}
794

795
static void vmCleanup(SVnodeMgmt *pMgmt) {
2,230✔
796
  vmCloseVnodes(pMgmt);
2,230✔
797
  vmStopWorker(pMgmt);
2,229✔
798
  vnodeCleanup();
2,229✔
799
  (void)taosThreadRwlockDestroy(&pMgmt->hashLock);
2,229✔
800
  (void)taosThreadMutexDestroy(&pMgmt->mutex);
2,229✔
801
  (void)taosThreadMutexDestroy(&pMgmt->fileLock);
2,229✔
802
  taosMemoryFree(pMgmt);
2,229!
803
}
2,229✔
804

805
static void vmCheckSyncTimeout(SVnodeMgmt *pMgmt) {
2,915✔
806
  int32_t     code = 0;
2,915✔
807
  int32_t     numOfVnodes = 0;
2,915✔
808
  SVnodeObj **ppVnodes = NULL;
2,915✔
809
  code = vmGetVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes);
2,915✔
810
  if (code != 0) {
2,915!
811
    dError("failed to get vnode list since %s", tstrerror(code));
×
812
    return;
×
813
  }
814

815
  if (ppVnodes != NULL) {
2,915!
816
    for (int32_t i = 0; i < numOfVnodes; ++i) {
37,633✔
817
      SVnodeObj *pVnode = ppVnodes[i];
34,718✔
818
      if (!pVnode->failed) {
34,718!
819
        vnodeSyncCheckTimeout(pVnode->pImpl);
34,718✔
820
      }
821
      vmReleaseVnode(pMgmt, pVnode);
34,718✔
822
    }
823
    taosMemoryFree(ppVnodes);
2,915!
824
  }
825
}
826

827
static void *vmThreadFp(void *param) {
2,230✔
828
  SVnodeMgmt *pMgmt = param;
2,230✔
829
  int64_t     lastTime = 0;
2,230✔
830
  setThreadName("vnode-timer");
2,230✔
831

832
  while (1) {
1,108,442✔
833
    lastTime++;
1,110,672✔
834
    taosMsleep(100);
1,110,672✔
835
    if (pMgmt->stop) break;
1,110,672✔
836
    if (lastTime % 10 != 0) continue;
1,108,442✔
837

838
    int64_t sec = lastTime / 10;
109,844✔
839
    if (sec % (VNODE_TIMEOUT_SEC / 2) == 0) {
109,844✔
840
      vmCheckSyncTimeout(pMgmt);
2,915✔
841
    }
842
  }
843

844
  return NULL;
2,230✔
845
}
846

847
static int32_t vmInitTimer(SVnodeMgmt *pMgmt) {
2,230✔
848
  int32_t      code = 0;
2,230✔
849
  TdThreadAttr thAttr;
850
  (void)taosThreadAttrInit(&thAttr);
2,230✔
851
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
2,230✔
852
  if (taosThreadCreate(&pMgmt->thread, &thAttr, vmThreadFp, pMgmt) != 0) {
2,230!
853
    code = TAOS_SYSTEM_ERROR(errno);
×
854
    dError("failed to create vnode timer thread since %s", tstrerror(code));
×
855
    return code;
×
856
  }
857

858
  (void)taosThreadAttrDestroy(&thAttr);
2,230✔
859
  return 0;
2,230✔
860
}
861

862
static void vmCleanupTimer(SVnodeMgmt *pMgmt) {
2,230✔
863
  pMgmt->stop = true;
2,230✔
864
  if (taosCheckPthreadValid(pMgmt->thread)) {
2,230!
865
    (void)taosThreadJoin(pMgmt->thread, NULL);
2,230✔
866
    taosThreadClear(&pMgmt->thread);
2,230✔
867
  }
868
}
2,230✔
869

870
static int32_t vmInit(SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) {
2,230✔
871
  int32_t code = -1;
2,230✔
872

873
  SVnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SVnodeMgmt));
2,230!
874
  if (pMgmt == NULL) {
2,230!
875
    code = terrno;
×
876
    goto _OVER;
×
877
  }
878

879
  pMgmt->pData = pInput->pData;
2,230✔
880
  pMgmt->path = pInput->path;
2,230✔
881
  pMgmt->name = pInput->name;
2,230✔
882
  pMgmt->msgCb = pInput->msgCb;
2,230✔
883
  pMgmt->msgCb.putToQueueFp = (PutToQueueFp)vmPutRpcMsgToQueue;
2,230✔
884
  pMgmt->msgCb.qsizeFp = (GetQueueSizeFp)vmGetQueueSize;
2,230✔
885
  pMgmt->msgCb.mgmt = pMgmt;
2,230✔
886

887
  code = taosThreadRwlockInit(&pMgmt->hashLock, NULL);
2,230✔
888
  if (code != 0) {
2,230!
889
    code = TAOS_SYSTEM_ERROR(errno);
×
890
    goto _OVER;
×
891
  }
892

893
  code = taosThreadMutexInit(&pMgmt->mutex, NULL);
2,230✔
894
  if (code != 0) {
2,230!
895
    code = TAOS_SYSTEM_ERROR(errno);
×
896
    goto _OVER;
×
897
  }
898

899
  code = taosThreadMutexInit(&pMgmt->fileLock, NULL);
2,230✔
900
  if (code != 0) {
2,230!
901
    code = TAOS_SYSTEM_ERROR(errno);
×
902
    goto _OVER;
×
903
  }
904

905
  pMgmt->pTfs = pInput->pTfs;
2,230✔
906
  if (pMgmt->pTfs == NULL) {
2,230!
907
    dError("tfs is null.");
×
908
    goto _OVER;
×
909
  }
910
  tmsgReportStartup("vnode-tfs", "initialized");
2,230✔
911
  if ((code = walInit(pInput->stopDnodeFp)) != 0) {
2,230!
912
    dError("failed to init wal since %s", tstrerror(code));
×
913
    goto _OVER;
×
914
  }
915

916
  tmsgReportStartup("vnode-wal", "initialized");
2,230✔
917

918
  if ((code = syncInit()) != 0) {
2,230!
919
    dError("failed to open sync since %s", tstrerror(code));
×
920
    goto _OVER;
×
921
  }
922
  tmsgReportStartup("vnode-sync", "initialized");
2,230✔
923

924
  if ((code = vnodeInit(pInput->stopDnodeFp)) != 0) {
2,230!
925
    dError("failed to init vnode since %s", tstrerror(code));
×
926
    goto _OVER;
×
927
  }
928
  tmsgReportStartup("vnode-commit", "initialized");
2,230✔
929

930
  if ((code = vmStartWorker(pMgmt)) != 0) {
2,230!
931
    dError("failed to init workers since %s", tstrerror(code));
×
932
    goto _OVER;
×
933
  }
934
  tmsgReportStartup("vnode-worker", "initialized");
2,230✔
935

936
  if ((code = vmOpenVnodes(pMgmt)) != 0) {
2,230!
937
    dError("failed to open all vnodes since %s", tstrerror(code));
×
938
    goto _OVER;
×
939
  }
940
  tmsgReportStartup("vnode-vnodes", "initialized");
2,230✔
941

942
  if ((code = udfcOpen()) != 0) {
2,230!
943
    dError("failed to open udfc in vnode since %s", tstrerror(code));
×
944
    goto _OVER;
×
945
  }
946

947
  code = 0;
2,230✔
948

949
_OVER:
2,230✔
950
  if (code == 0) {
2,230!
951
    pOutput->pMgmt = pMgmt;
2,230✔
952
  } else {
953
    dError("failed to init vnodes-mgmt since %s", tstrerror(code));
×
954
    vmCleanup(pMgmt);
×
955
  }
956

957
  return code;
2,230✔
958
}
959

960
static int32_t vmRequire(const SMgmtInputOpt *pInput, bool *required) {
2,266✔
961
  *required = tsNumOfSupportVnodes > 0;
2,266✔
962
  return 0;
2,266✔
963
}
964

965
static void *vmRestoreVnodeInThread(void *param) {
946✔
966
  SVnodeThread *pThread = param;
946✔
967
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
946✔
968

969
  dInfo("thread:%d, start to restore %d vnodes", pThread->threadIndex, pThread->vnodeNum);
946!
970
  setThreadName("restore-vnodes");
946✔
971

972
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
1,907✔
973
    SVnodeObj *pVnode = pThread->ppVnodes[v];
961✔
974
    if (pVnode->failed) {
961!
975
      dError("vgId:%d, cannot restore a vnode in failed mode.", pVnode->vgId);
×
976
      continue;
×
977
    }
978

979
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
961✔
980
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been restored", pVnode->vgId,
961✔
981
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
982
    tmsgReportStartup("vnode-restore", stepDesc);
961✔
983

984
    int32_t code = vnodeStart(pVnode->pImpl);
961✔
985
    if (code != 0) {
961!
986
      dError("vgId:%d, failed to restore vnode by thread:%d", pVnode->vgId, pThread->threadIndex);
×
987
      pThread->failed++;
×
988
    } else {
989
      dInfo("vgId:%d, is restored by thread:%d", pVnode->vgId, pThread->threadIndex);
961!
990
      pThread->opened++;
961✔
991
      (void)atomic_add_fetch_32(&pMgmt->state.openVnodes, 1);
961✔
992
    }
993
  }
994

995
  dInfo("thread:%d, numOfVnodes:%d, restored:%d failed:%d", pThread->threadIndex, pThread->vnodeNum, pThread->opened,
946!
996
        pThread->failed);
997
  return NULL;
946✔
998
}
999

1000
static int32_t vmStartVnodes(SVnodeMgmt *pMgmt) {
2,230✔
1001
  int32_t     code = 0;
2,230✔
1002
  int32_t     numOfVnodes = 0;
2,230✔
1003
  SVnodeObj **ppVnodes = NULL;
2,230✔
1004
  code = vmGetVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes);
2,230✔
1005
  if (code != 0) {
2,230!
1006
    dError("failed to get vnode list since %s", tstrerror(code));
×
1007
    return code;
×
1008
  }
1009

1010
  int32_t threadNum = tsNumOfCores / 2;
2,230✔
1011
  if (threadNum < 1) threadNum = 1;
2,230!
1012
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
2,230✔
1013

1014
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
2,230!
1015
  if (threads == NULL) {
2,230!
1016
    return terrno;
×
1017
  }
1018

1019
  for (int32_t t = 0; t < threadNum; ++t) {
46,830✔
1020
    threads[t].threadIndex = t;
44,600✔
1021
    threads[t].pMgmt = pMgmt;
44,600✔
1022
    threads[t].ppVnodes = taosMemoryCalloc(vnodesPerThread, sizeof(SVnode *));
44,600!
1023
    if (threads[t].ppVnodes == NULL) {
44,600!
1024
      code = terrno;
×
1025
      break;
×
1026
    }
1027
  }
1028

1029
  for (int32_t v = 0; v < numOfVnodes; ++v) {
3,191✔
1030
    int32_t       t = v % threadNum;
961✔
1031
    SVnodeThread *pThread = &threads[t];
961✔
1032
    if (pThread->ppVnodes != NULL && ppVnodes != NULL) {
961!
1033
      pThread->ppVnodes[pThread->vnodeNum++] = ppVnodes[v];
961✔
1034
    }
1035
  }
1036

1037
  pMgmt->state.openVnodes = 0;
2,230✔
1038
  pMgmt->state.dropVnodes = 0;
2,230✔
1039
  dInfo("restore %d vnodes with %d threads", numOfVnodes, threadNum);
2,230!
1040

1041
  for (int32_t t = 0; t < threadNum; ++t) {
46,830✔
1042
    SVnodeThread *pThread = &threads[t];
44,600✔
1043
    if (pThread->vnodeNum == 0) continue;
44,600✔
1044

1045
    TdThreadAttr thAttr;
1046
    (void)taosThreadAttrInit(&thAttr);
946✔
1047
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
946✔
1048
    if (taosThreadCreate(&pThread->thread, &thAttr, vmRestoreVnodeInThread, pThread) != 0) {
946!
1049
      dError("thread:%d, failed to create thread to restore vnode since %s", pThread->threadIndex, strerror(errno));
×
1050
    }
1051

1052
    (void)taosThreadAttrDestroy(&thAttr);
946✔
1053
  }
1054

1055
  for (int32_t t = 0; t < threadNum; ++t) {
46,830✔
1056
    SVnodeThread *pThread = &threads[t];
44,600✔
1057
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
44,600!
1058
      (void)taosThreadJoin(pThread->thread, NULL);
946✔
1059
      taosThreadClear(&pThread->thread);
946✔
1060
    }
1061
    taosMemoryFree(pThread->ppVnodes);
44,600!
1062
  }
1063
  taosMemoryFree(threads);
2,230!
1064

1065
  for (int32_t i = 0; i < numOfVnodes; ++i) {
3,191✔
1066
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
961!
1067
    vmReleaseVnode(pMgmt, ppVnodes[i]);
961✔
1068
  }
1069

1070
  if (ppVnodes != NULL) {
2,230!
1071
    taosMemoryFree(ppVnodes);
2,230!
1072
  }
1073

1074
  return vmInitTimer(pMgmt);
2,230✔
1075

1076
_exit:
1077
  for (int32_t t = 0; t < threadNum; ++t) {
1078
    SVnodeThread *pThread = &threads[t];
1079
    taosMemoryFree(pThread->ppVnodes);
1080
  }
1081
  taosMemoryFree(threads);
1082
  return code;
1083
}
1084

1085
static void vmStop(SVnodeMgmt *pMgmt) { vmCleanupTimer(pMgmt); }
2,230✔
1086

1087
SMgmtFunc vmGetMgmtFunc() {
2,266✔
1088
  SMgmtFunc mgmtFunc = {0};
2,266✔
1089
  mgmtFunc.openFp = vmInit;
2,266✔
1090
  mgmtFunc.closeFp = (NodeCloseFp)vmCleanup;
2,266✔
1091
  mgmtFunc.startFp = (NodeStartFp)vmStartVnodes;
2,266✔
1092
  mgmtFunc.stopFp = (NodeStopFp)vmStop;
2,266✔
1093
  mgmtFunc.requiredFp = vmRequire;
2,266✔
1094
  mgmtFunc.getHandlesFp = vmGetMsgHandles;
2,266✔
1095

1096
  return mgmtFunc;
2,266✔
1097
}
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