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

taosdata / TDengine / #3565

25 Dec 2024 05:34AM UTC coverage: 51.098% (-11.1%) from 62.21%
#3565

push

travis-ci

web-flow
Merge pull request #29316 from taosdata/enh/3.0/TD-33266

enh(ut):Add wal & config UT.

111558 of 284773 branches covered (39.17%)

Branch coverage included in aggregate %.

1 of 2 new or added lines in 2 files covered. (50.0%)

39015 existing lines in 102 files now uncovered.

177882 of 281666 relevant lines covered (63.15%)

15090998.35 hits per line

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

48.01
/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) {
18✔
24
  int32_t    diskId = -1;
18✔
25
  SVnodeObj *pVnode = NULL;
18✔
26

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

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

39
  SVnodeObj *pVnode = *ppVnode;
36✔
40

41
  int32_t refCount = atomic_load_32(&pVnode->refCount);
36✔
42
  while (refCount > 0) {
36!
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);
36!
49
  taosMemoryFree(pVnode);
36!
50
  ppVnode[0] = NULL;
36✔
51
}
52

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

62
  pCreatingVnode->vgId = vgId;
18✔
63
  pCreatingVnode->diskPrimary = diskId;
18✔
64

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

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

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

83
  return code;
18✔
84
}
85

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

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

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

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

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

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

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

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

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

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

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

184
_OVER:
18✔
185

186
  for (int32_t i = 0; i < numOfVnodes; ++i) {
27✔
187
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
9!
188
    vmReleaseVnode(pMgmt, ppVnodes[i]);
9✔
189
  }
190
  if (ppVnodes != NULL) {
18!
191
    taosMemoryFree(ppVnodes);
18!
192
  }
193

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

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

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

208
  (void)taosThreadRwlockRdlock(&pMgmt->lock);
126✔
209
  int32_t r = taosHashGetDup(pMgmt->runngingHash, &vgId, sizeof(int32_t), (void *)&pVnode);
126✔
210
  if (pVnode == NULL || strict && (pVnode->dropped || pVnode->failed)) {
126!
211
    terrno = TSDB_CODE_VND_INVALID_VGROUP_ID;
34✔
212
    pVnode = NULL;
34✔
213
  } else {
214
    int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
92✔
215
    dTrace("vgId:%d, acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
92!
216
  }
217
  (void)taosThreadRwlockUnlock(&pMgmt->lock);
126✔
218

219
  return pVnode;
126✔
220
}
221

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

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

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

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

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

245
  return code;
18✔
246
}
247

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

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

UNCOV
265
  pClosedVnode->vgId = pVnode->vgId;
×
UNCOV
266
  pClosedVnode->dropped = pVnode->dropped;
×
UNCOV
267
  pClosedVnode->vgVersion = pVnode->vgVersion;
×
UNCOV
268
  pClosedVnode->diskPrimary = pVnode->diskPrimary;
×
UNCOV
269
  pClosedVnode->toVgId = pVnode->toVgId;
×
270

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

UNCOV
285
  return code;
×
286
}
287

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

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

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

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

326
  if (pImpl) {
18!
327
    if (vmAllocQueue(pMgmt, pVnode) != 0) {
18!
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->lock);
18✔
338
  int32_t code = vmRegisterRunningState(pMgmt, pVnode);
18✔
339
  vmUnRegisterClosedState(pMgmt, pVnode);
18✔
340
  (void)taosThreadRwlockUnlock(&pMgmt->lock);
18✔
341

342
  return code;
18✔
343
}
344

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

349
  if (pVnode->pImpl && vnodeIsLeader(pVnode->pImpl)) {
18!
350
    vnodeProposeCommitOnNeed(pVnode->pImpl, atExit);
18✔
351
  }
352

353
  (void)taosThreadRwlockWrlock(&pMgmt->lock);
18✔
354
  vmUnRegisterRunningState(pMgmt, pVnode->vgId);
18✔
355
  if (keepClosed) {
18!
UNCOV
356
    if (vmRegisterClosedState(pMgmt, pVnode) != 0) {
×
357
      (void)taosThreadRwlockUnlock(&pMgmt->lock);
×
358
      return;
×
359
    };
360
  }
361
  (void)taosThreadRwlockUnlock(&pMgmt->lock);
18✔
362

363
  vmReleaseVnode(pMgmt, pVnode);
18✔
364

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

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

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

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

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

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

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

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

397
  tqNotifyClose(pVnode->pImpl->pTq);
18✔
398
  dInfo("vgId:%d, wait for vnode stream queue:%p is empty", pVnode->vgId, pVnode->pStreamQ);
18!
399
  while (!taosQueueEmpty(pVnode->pStreamQ)) taosMsleep(10);
18!
400

401
  dInfo("vgId:%d, all vnode queues is empty", pVnode->vgId);
18!
402

403
  dInfo("vgId:%d, post close", pVnode->vgId);
18!
404
  vnodePostClose(pVnode->pImpl);
18✔
405

406
  vmFreeQueue(pMgmt, pVnode);
18✔
407

408
  if (commitAndRemoveWal) {
18!
UNCOV
409
    dInfo("vgId:%d, commit data for vnode split", pVnode->vgId);
×
UNCOV
410
    if (vnodeSyncCommit(pVnode->pImpl) != 0) {
×
411
      dError("vgId:%d, failed to commit data", pVnode->vgId);
×
412
    }
UNCOV
413
    if (vnodeBegin(pVnode->pImpl) != 0) {
×
414
      dError("vgId:%d, failed to begin", pVnode->vgId);
×
415
    }
UNCOV
416
    dInfo("vgId:%d, commit data finished", pVnode->vgId);
×
417
  }
418

419
  int32_t nodeId = vnodeNodeId(pVnode->pImpl);
18✔
420
  vnodeClose(pVnode->pImpl);
18✔
421
  pVnode->pImpl = NULL;
18✔
422

423
_closed:
18✔
424
  dInfo("vgId:%d, vnode is closed", pVnode->vgId);
18!
425

426
  if (commitAndRemoveWal) {
18!
UNCOV
427
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d%swal", TD_DIRSEP, pVnode->vgId, TD_DIRSEP);
×
UNCOV
428
    dInfo("vgId:%d, remove all wals, path:%s", pVnode->vgId, path);
×
UNCOV
429
    if (tfsRmdir(pMgmt->pTfs, path) != 0) {
×
430
      dTrace("vgId:%d, failed to remove wals, path:%s", pVnode->vgId, path);
×
431
    }
UNCOV
432
    if (tfsMkdir(pMgmt->pTfs, path) != 0) {
×
433
      dTrace("vgId:%d, failed to create wals, path:%s", pVnode->vgId, path);
×
434
    }
435
  }
436

437
  if (pVnode->dropped) {
18✔
438
    dInfo("vgId:%d, vnode is destroyed, dropped:%d", pVnode->vgId, pVnode->dropped);
16!
439
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pVnode->vgId);
16✔
440
    vnodeDestroy(pVnode->vgId, path, pMgmt->pTfs, nodeId);
16✔
441
  }
442

443
  vmFreeVnodeObj(&pVnode);
18✔
444
}
445

446
void vmCloseFailedVnode(SVnodeMgmt *pMgmt, int32_t vgId) {
×
447
  int32_t r = 0;
×
448
  r = taosThreadRwlockWrlock(&pMgmt->lock);
×
449
  if (r != 0) {
×
450
    dError("vgId:%d, failed to lock since %s", vgId, tstrerror(r));
×
451
  }
452
  if (r == 0) {
×
453
    vmUnRegisterRunningState(pMgmt, vgId);
×
454
  }
455
  r = taosThreadRwlockUnlock(&pMgmt->lock);
×
456
  if (r != 0) {
×
457
    dError("vgId:%d, failed to unlock since %s", vgId, tstrerror(r));
×
458
  }
459
}
×
460

461
static int32_t vmRestoreVgroupId(SWrapperCfg *pCfg, STfs *pTfs) {
×
462
  int32_t srcVgId = pCfg->vgId;
×
463
  int32_t dstVgId = pCfg->toVgId;
×
464
  if (dstVgId == 0) return 0;
×
465

466
  char srcPath[TSDB_FILENAME_LEN];
467
  char dstPath[TSDB_FILENAME_LEN];
468

469
  snprintf(srcPath, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, srcVgId);
×
470
  snprintf(dstPath, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, dstVgId);
×
471

472
  int32_t diskPrimary = pCfg->diskPrimary;
×
473
  int32_t vgId = vnodeRestoreVgroupId(srcPath, dstPath, srcVgId, dstVgId, diskPrimary, pTfs);
×
474
  if (vgId <= 0) {
×
475
    dError("vgId:%d, failed to restore vgroup id. srcPath: %s", pCfg->vgId, srcPath);
×
476
    return -1;
×
477
  }
478

479
  pCfg->vgId = vgId;
×
480
  pCfg->toVgId = 0;
×
481
  return 0;
×
482
}
483

UNCOV
484
static void *vmOpenVnodeInThread(void *param) {
×
UNCOV
485
  SVnodeThread *pThread = param;
×
UNCOV
486
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
×
487
  char          path[TSDB_FILENAME_LEN];
488

UNCOV
489
  dInfo("thread:%d, start to open or destroy %d vnodes", pThread->threadIndex, pThread->vnodeNum);
×
UNCOV
490
  setThreadName("open-vnodes");
×
491

UNCOV
492
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
×
UNCOV
493
    SWrapperCfg *pCfg = &pThread->pCfgs[v];
×
UNCOV
494
    if (pCfg->dropped) {
×
495
      char stepDesc[TSDB_STEP_DESC_LEN] = {0};
×
496
      snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to destroy, %d of %d have been dropped", pCfg->vgId,
×
497
               pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
498
      tmsgReportStartup("vnode-destroy", stepDesc);
×
499

500
      snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pCfg->vgId);
×
501
      vnodeDestroy(pCfg->vgId, path, pMgmt->pTfs, 0);
×
502
      pThread->updateVnodesList = true;
×
503
      pThread->dropped++;
×
504
      (void)atomic_add_fetch_32(&pMgmt->state.dropVnodes, 1);
×
505
      continue;
×
506
    }
507

UNCOV
508
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
×
UNCOV
509
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been opened", pCfg->vgId,
×
510
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
UNCOV
511
    tmsgReportStartup("vnode-open", stepDesc);
×
512

UNCOV
513
    if (pCfg->toVgId) {
×
514
      if (vmRestoreVgroupId(pCfg, pMgmt->pTfs) != 0) {
×
515
        dError("vgId:%d, failed to restore vgroup id by thread:%d", pCfg->vgId, pThread->threadIndex);
×
516
        pThread->failed++;
×
517
        continue;
×
518
      }
519
      pThread->updateVnodesList = true;
×
520
    }
521

UNCOV
522
    int32_t diskPrimary = pCfg->diskPrimary;
×
UNCOV
523
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pCfg->vgId);
×
524

UNCOV
525
    SVnode *pImpl = vnodeOpen(path, diskPrimary, pMgmt->pTfs, pMgmt->msgCb, false);
×
526

UNCOV
527
    if (pImpl == NULL) {
×
528
      dError("vgId:%d, failed to open vnode by thread:%d since %s", pCfg->vgId, pThread->threadIndex, terrstr());
×
529
      if (terrno != TSDB_CODE_NEED_RETRY) {
×
530
        pThread->failed++;
×
531
        continue;
×
532
      }
533
    }
534

UNCOV
535
    if (vmOpenVnode(pMgmt, pCfg, pImpl) != 0) {
×
536
      dError("vgId:%d, failed to open vnode by thread:%d", pCfg->vgId, pThread->threadIndex);
×
537
      pThread->failed++;
×
538
      continue;
×
539
    }
540

UNCOV
541
    dInfo("vgId:%d, is opened by thread:%d", pCfg->vgId, pThread->threadIndex);
×
UNCOV
542
    pThread->opened++;
×
UNCOV
543
    (void)atomic_add_fetch_32(&pMgmt->state.openVnodes, 1);
×
544
  }
545

UNCOV
546
  dInfo("thread:%d, numOfVnodes:%d, opened:%d dropped:%d failed:%d", pThread->threadIndex, pThread->vnodeNum,
×
547
        pThread->opened, pThread->dropped, pThread->failed);
UNCOV
548
  return NULL;
×
549
}
550

551
static int32_t vmOpenVnodes(SVnodeMgmt *pMgmt) {
8✔
552
  pMgmt->runngingHash =
8✔
553
      taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
8✔
554
  if (pMgmt->runngingHash == NULL) {
8!
555
    dError("failed to init vnode hash since %s", terrstr());
×
556
    return TSDB_CODE_OUT_OF_MEMORY;
×
557
  }
558

559
  pMgmt->closedHash =
8✔
560
      taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
8✔
561
  if (pMgmt->closedHash == NULL) {
8!
562
    dError("failed to init vnode closed hash since %s", terrstr());
×
563
    return TSDB_CODE_OUT_OF_MEMORY;
×
564
  }
565

566
  pMgmt->creatingHash =
8✔
567
      taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
8✔
568
  if (pMgmt->creatingHash == NULL) {
8!
569
    dError("failed to init vnode creatingHash hash since %s", terrstr());
×
570
    return TSDB_CODE_OUT_OF_MEMORY;
×
571
  }
572

573
  SWrapperCfg *pCfgs = NULL;
8✔
574
  int32_t      numOfVnodes = 0;
8✔
575
  if (vmGetVnodeListFromFile(pMgmt, &pCfgs, &numOfVnodes) != 0) {
8!
576
    dInfo("failed to get vnode list from disk since %s", terrstr());
×
577
    return -1;
×
578
  }
579

580
  pMgmt->state.totalVnodes = numOfVnodes;
8✔
581

582
  int32_t threadNum = tsNumOfCores / 2;
8✔
583
  if (threadNum < 1) threadNum = 1;
8!
584
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
8✔
585

586
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
8!
587
  if (threads == NULL) {
8!
588
    dError("failed to allocate memory for threads since %s", terrstr());
×
589
    taosMemoryFree(pCfgs);
×
590
    return terrno;
×
591
  }
592

593
  for (int32_t t = 0; t < threadNum; ++t) {
168✔
594
    threads[t].threadIndex = t;
160✔
595
    threads[t].pMgmt = pMgmt;
160✔
596
    threads[t].pCfgs = taosMemoryCalloc(vnodesPerThread, sizeof(SWrapperCfg));
160!
597
  }
598

599
  for (int32_t v = 0; v < numOfVnodes; ++v) {
8!
UNCOV
600
    int32_t       t = v % threadNum;
×
UNCOV
601
    SVnodeThread *pThread = &threads[t];
×
UNCOV
602
    pThread->pCfgs[pThread->vnodeNum++] = pCfgs[v];
×
603
  }
604

605
  dInfo("open %d vnodes with %d threads", numOfVnodes, threadNum);
8!
606

607
  for (int32_t t = 0; t < threadNum; ++t) {
168✔
608
    SVnodeThread *pThread = &threads[t];
160✔
609
    if (pThread->vnodeNum == 0) continue;
160!
610

611
    TdThreadAttr thAttr;
UNCOV
612
    (void)taosThreadAttrInit(&thAttr);
×
UNCOV
613
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
×
UNCOV
614
    if (taosThreadCreate(&pThread->thread, &thAttr, vmOpenVnodeInThread, pThread) != 0) {
×
615
      dError("thread:%d, failed to create thread to open vnode, reason:%s", pThread->threadIndex, strerror(errno));
×
616
    }
617

UNCOV
618
    (void)taosThreadAttrDestroy(&thAttr);
×
619
  }
620

621
  bool updateVnodesList = false;
8✔
622

623
  for (int32_t t = 0; t < threadNum; ++t) {
168✔
624
    SVnodeThread *pThread = &threads[t];
160✔
625
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
160!
UNCOV
626
      (void)taosThreadJoin(pThread->thread, NULL);
×
UNCOV
627
      taosThreadClear(&pThread->thread);
×
628
    }
629
    taosMemoryFree(pThread->pCfgs);
160!
630
    if (pThread->updateVnodesList) updateVnodesList = true;
160!
631
  }
632
  taosMemoryFree(threads);
8!
633
  taosMemoryFree(pCfgs);
8!
634

635
  if ((pMgmt->state.openVnodes + pMgmt->state.dropVnodes) != pMgmt->state.totalVnodes) {
8!
636
    dError("there are total vnodes:%d, opened:%d", pMgmt->state.totalVnodes, pMgmt->state.openVnodes);
×
637
    terrno = TSDB_CODE_VND_INIT_FAILED;
×
638
    return -1;
×
639
  }
640

641
  if (updateVnodesList && vmWriteVnodeListToFile(pMgmt) != 0) {
8!
642
    dError("failed to write vnode list since %s", terrstr());
×
643
    return -1;
×
644
  }
645

646
  dInfo("successfully opened %d vnodes", pMgmt->state.totalVnodes);
8!
647
  return 0;
8✔
648
}
649

650
static void *vmCloseVnodeInThread(void *param) {
2✔
651
  SVnodeThread *pThread = param;
2✔
652
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
2✔
653

654
  dInfo("thread:%d, start to close %d vnodes", pThread->threadIndex, pThread->vnodeNum);
2!
655
  setThreadName("close-vnodes");
2✔
656

657
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
4✔
658
    SVnodeObj *pVnode = pThread->ppVnodes[v];
2✔
659

660
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
2✔
661
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to close, %d of %d have been closed", pVnode->vgId,
2✔
662
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
663
    tmsgReportStartup("vnode-close", stepDesc);
2✔
664

665
    vmCloseVnode(pMgmt, pVnode, false, false);
2✔
666
  }
667

668
  dInfo("thread:%d, numOfVnodes:%d is closed", pThread->threadIndex, pThread->vnodeNum);
2!
669
  return NULL;
2✔
670
}
671

672
static void vmCloseVnodes(SVnodeMgmt *pMgmt) {
8✔
673
  int32_t code = 0;
8✔
674
  dInfo("start to close all vnodes");
8!
675
  tSingleWorkerCleanup(&pMgmt->mgmtWorker);
8✔
676
  dInfo("vnodes mgmt worker is stopped");
8!
677
  tSingleWorkerCleanup(&pMgmt->mgmtMultiWorker);
8✔
678
  dInfo("vnodes multiple mgmt worker is stopped");
8!
679

680
  int32_t     numOfVnodes = 0;
8✔
681
  SVnodeObj **ppVnodes = NULL;
8✔
682
  code = vmGetVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes);
8✔
683
  if (code != 0) {
8!
684
    dError("failed to get vnode list since %s", tstrerror(code));
×
685
    return;
×
686
  }
687

688
  int32_t threadNum = tsNumOfCores / 2;
8✔
689
  if (threadNum < 1) threadNum = 1;
8!
690
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
8✔
691

692
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
8!
693
  for (int32_t t = 0; t < threadNum; ++t) {
168✔
694
    threads[t].threadIndex = t;
160✔
695
    threads[t].pMgmt = pMgmt;
160✔
696
    threads[t].ppVnodes = taosMemoryCalloc(vnodesPerThread, sizeof(SVnode *));
160!
697
  }
698

699
  for (int32_t v = 0; v < numOfVnodes; ++v) {
10✔
700
    int32_t       t = v % threadNum;
2✔
701
    SVnodeThread *pThread = &threads[t];
2✔
702
    if (pThread->ppVnodes != NULL && ppVnodes != NULL) {
2!
703
      pThread->ppVnodes[pThread->vnodeNum++] = ppVnodes[v];
2✔
704
    }
705
  }
706

707
  pMgmt->state.openVnodes = 0;
8✔
708
  dInfo("close %d vnodes with %d threads", numOfVnodes, threadNum);
8!
709

710
  for (int32_t t = 0; t < threadNum; ++t) {
168✔
711
    SVnodeThread *pThread = &threads[t];
160✔
712
    if (pThread->vnodeNum == 0) continue;
160✔
713

714
    TdThreadAttr thAttr;
715
    (void)taosThreadAttrInit(&thAttr);
2✔
716
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
2✔
717
    if (taosThreadCreate(&pThread->thread, &thAttr, vmCloseVnodeInThread, pThread) != 0) {
2!
718
      dError("thread:%d, failed to create thread to close vnode since %s", pThread->threadIndex, strerror(errno));
×
719
    }
720

721
    (void)taosThreadAttrDestroy(&thAttr);
2✔
722
  }
723

724
  for (int32_t t = 0; t < threadNum; ++t) {
168✔
725
    SVnodeThread *pThread = &threads[t];
160✔
726
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
160!
727
      (void)taosThreadJoin(pThread->thread, NULL);
2✔
728
      taosThreadClear(&pThread->thread);
2✔
729
    }
730
    taosMemoryFree(pThread->ppVnodes);
160!
731
  }
732
  taosMemoryFree(threads);
8!
733

734
  if (ppVnodes != NULL) {
8!
735
    taosMemoryFree(ppVnodes);
8!
736
  }
737

738
  if (pMgmt->runngingHash != NULL) {
8!
739
    taosHashCleanup(pMgmt->runngingHash);
8✔
740
    pMgmt->runngingHash = NULL;
8✔
741
  }
742

743
  void *pIter = taosHashIterate(pMgmt->closedHash, NULL);
8✔
744
  while (pIter) {
8!
745
    SVnodeObj **ppVnode = pIter;
×
746
    vmFreeVnodeObj(ppVnode);
×
747
    pIter = taosHashIterate(pMgmt->closedHash, pIter);
×
748
  }
749

750
  if (pMgmt->closedHash != NULL) {
8!
751
    taosHashCleanup(pMgmt->closedHash);
8✔
752
    pMgmt->closedHash = NULL;
8✔
753
  }
754

755
  pIter = taosHashIterate(pMgmt->creatingHash, NULL);
8✔
756
  while (pIter) {
8!
757
    SVnodeObj **ppVnode = pIter;
×
758
    vmFreeVnodeObj(ppVnode);
×
759
    pIter = taosHashIterate(pMgmt->creatingHash, pIter);
×
760
  }
761

762
  if (pMgmt->creatingHash != NULL) {
8!
763
    taosHashCleanup(pMgmt->creatingHash);
8✔
764
    pMgmt->creatingHash = NULL;
8✔
765
  }
766

767
  dInfo("total vnodes:%d are all closed", numOfVnodes);
8!
768
}
769

770
static void vmCleanup(SVnodeMgmt *pMgmt) {
8✔
771
  vmCloseVnodes(pMgmt);
8✔
772
  vmStopWorker(pMgmt);
8✔
773
  vnodeCleanup();
8✔
774
  (void)taosThreadRwlockDestroy(&pMgmt->lock);
8✔
775
  (void)taosThreadMutexDestroy(&pMgmt->mutex);
8✔
776
  (void)taosThreadMutexDestroy(&pMgmt->fileLock);
8✔
777
  taosMemoryFree(pMgmt);
8!
778
}
8✔
779

UNCOV
780
static void vmCheckSyncTimeout(SVnodeMgmt *pMgmt) {
×
UNCOV
781
  int32_t     code = 0;
×
UNCOV
782
  int32_t     numOfVnodes = 0;
×
UNCOV
783
  SVnodeObj **ppVnodes = NULL;
×
UNCOV
784
  code = vmGetVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes);
×
UNCOV
785
  if (code != 0) {
×
786
    dError("failed to get vnode list since %s", tstrerror(code));
×
787
    return;
×
788
  }
789

UNCOV
790
  if (ppVnodes != NULL) {
×
UNCOV
791
    for (int32_t i = 0; i < numOfVnodes; ++i) {
×
UNCOV
792
      SVnodeObj *pVnode = ppVnodes[i];
×
UNCOV
793
      if (!pVnode->failed) {
×
UNCOV
794
        vnodeSyncCheckTimeout(pVnode->pImpl);
×
795
      }
UNCOV
796
      vmReleaseVnode(pMgmt, pVnode);
×
797
    }
UNCOV
798
    taosMemoryFree(ppVnodes);
×
799
  }
800
}
801

802
static void *vmThreadFp(void *param) {
8✔
803
  SVnodeMgmt *pMgmt = param;
8✔
804
  int64_t     lastTime = 0;
8✔
805
  setThreadName("vnode-timer");
8✔
806

807
  while (1) {
204✔
808
    lastTime++;
212✔
809
    taosMsleep(100);
212✔
810
    if (pMgmt->stop) break;
212✔
811
    if (lastTime % 10 != 0) continue;
204✔
812

813
    int64_t sec = lastTime / 10;
16✔
814
    if (sec % (VNODE_TIMEOUT_SEC / 2) == 0) {
16!
UNCOV
815
      vmCheckSyncTimeout(pMgmt);
×
816
    }
817
  }
818

819
  return NULL;
8✔
820
}
821

822
static int32_t vmInitTimer(SVnodeMgmt *pMgmt) {
8✔
823
  int32_t      code = 0;
8✔
824
  TdThreadAttr thAttr;
825
  (void)taosThreadAttrInit(&thAttr);
8✔
826
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
8✔
827
  if (taosThreadCreate(&pMgmt->thread, &thAttr, vmThreadFp, pMgmt) != 0) {
8!
828
    code = TAOS_SYSTEM_ERROR(errno);
×
829
    dError("failed to create vnode timer thread since %s", tstrerror(code));
×
830
    return code;
×
831
  }
832

833
  (void)taosThreadAttrDestroy(&thAttr);
8✔
834
  return 0;
8✔
835
}
836

837
static void vmCleanupTimer(SVnodeMgmt *pMgmt) {
8✔
838
  pMgmt->stop = true;
8✔
839
  if (taosCheckPthreadValid(pMgmt->thread)) {
8!
840
    (void)taosThreadJoin(pMgmt->thread, NULL);
8✔
841
    taosThreadClear(&pMgmt->thread);
8✔
842
  }
843
}
8✔
844

845
static int32_t vmInit(SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) {
8✔
846
  int32_t code = -1;
8✔
847

848
  SVnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SVnodeMgmt));
8!
849
  if (pMgmt == NULL) {
8!
850
    code = terrno;
×
851
    goto _OVER;
×
852
  }
853

854
  pMgmt->pData = pInput->pData;
8✔
855
  pMgmt->path = pInput->path;
8✔
856
  pMgmt->name = pInput->name;
8✔
857
  pMgmt->msgCb = pInput->msgCb;
8✔
858
  pMgmt->msgCb.putToQueueFp = (PutToQueueFp)vmPutRpcMsgToQueue;
8✔
859
  pMgmt->msgCb.qsizeFp = (GetQueueSizeFp)vmGetQueueSize;
8✔
860
  pMgmt->msgCb.mgmt = pMgmt;
8✔
861

862
  code = taosThreadRwlockInit(&pMgmt->lock, NULL);
8✔
863
  if (code != 0) {
8!
864
    code = TAOS_SYSTEM_ERROR(errno);
×
865
    goto _OVER;
×
866
  }
867

868
  code = taosThreadMutexInit(&pMgmt->mutex, NULL);
8✔
869
  if (code != 0) {
8!
870
    code = TAOS_SYSTEM_ERROR(errno);
×
871
    goto _OVER;
×
872
  }
873

874
  code = taosThreadMutexInit(&pMgmt->fileLock, NULL);
8✔
875
  if (code != 0) {
8!
876
    code = TAOS_SYSTEM_ERROR(errno);
×
877
    goto _OVER;
×
878
  }
879

880
  pMgmt->pTfs = pInput->pTfs;
8✔
881
  if (pMgmt->pTfs == NULL) {
8!
882
    dError("tfs is null.");
×
883
    goto _OVER;
×
884
  }
885
  tmsgReportStartup("vnode-tfs", "initialized");
8✔
886
  if ((code = walInit(pInput->stopDnodeFp)) != 0) {
8!
887
    dError("failed to init wal since %s", tstrerror(code));
×
888
    goto _OVER;
×
889
  }
890

891
  tmsgReportStartup("vnode-wal", "initialized");
8✔
892

893
  if ((code = syncInit()) != 0) {
8!
894
    dError("failed to open sync since %s", tstrerror(code));
×
895
    goto _OVER;
×
896
  }
897
  tmsgReportStartup("vnode-sync", "initialized");
8✔
898

899
  if ((code = vnodeInit(pInput->stopDnodeFp)) != 0) {
8!
900
    dError("failed to init vnode since %s", tstrerror(code));
×
901
    goto _OVER;
×
902
  }
903
  tmsgReportStartup("vnode-commit", "initialized");
8✔
904

905
  if ((code = vmStartWorker(pMgmt)) != 0) {
8!
906
    dError("failed to init workers since %s", tstrerror(code));
×
907
    goto _OVER;
×
908
  }
909
  tmsgReportStartup("vnode-worker", "initialized");
8✔
910

911
  if ((code = vmOpenVnodes(pMgmt)) != 0) {
8!
912
    dError("failed to open all vnodes since %s", tstrerror(code));
×
913
    goto _OVER;
×
914
  }
915
  tmsgReportStartup("vnode-vnodes", "initialized");
8✔
916

917
  if ((code = udfcOpen()) != 0) {
8!
918
    dError("failed to open udfc in vnode since %s", tstrerror(code));
×
919
    goto _OVER;
×
920
  }
921

922
  code = 0;
8✔
923

924
_OVER:
8✔
925
  if (code == 0) {
8!
926
    pOutput->pMgmt = pMgmt;
8✔
927
  } else {
928
    dError("failed to init vnodes-mgmt since %s", tstrerror(code));
×
929
    vmCleanup(pMgmt);
×
930
  }
931

932
  return code;
8✔
933
}
934

935
static int32_t vmRequire(const SMgmtInputOpt *pInput, bool *required) {
8✔
936
  *required = tsNumOfSupportVnodes > 0;
8✔
937
  return 0;
8✔
938
}
939

UNCOV
940
static void *vmRestoreVnodeInThread(void *param) {
×
UNCOV
941
  SVnodeThread *pThread = param;
×
UNCOV
942
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
×
943

UNCOV
944
  dInfo("thread:%d, start to restore %d vnodes", pThread->threadIndex, pThread->vnodeNum);
×
UNCOV
945
  setThreadName("restore-vnodes");
×
946

UNCOV
947
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
×
UNCOV
948
    SVnodeObj *pVnode = pThread->ppVnodes[v];
×
UNCOV
949
    if (pVnode->failed) {
×
950
      dError("vgId:%d, cannot restore a vnode in failed mode.", pVnode->vgId);
×
951
      continue;
×
952
    }
953

UNCOV
954
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
×
UNCOV
955
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been restored", pVnode->vgId,
×
956
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
UNCOV
957
    tmsgReportStartup("vnode-restore", stepDesc);
×
958

UNCOV
959
    int32_t code = vnodeStart(pVnode->pImpl);
×
UNCOV
960
    if (code != 0) {
×
961
      dError("vgId:%d, failed to restore vnode by thread:%d", pVnode->vgId, pThread->threadIndex);
×
962
      pThread->failed++;
×
963
    } else {
UNCOV
964
      dInfo("vgId:%d, is restored by thread:%d", pVnode->vgId, pThread->threadIndex);
×
UNCOV
965
      pThread->opened++;
×
UNCOV
966
      (void)atomic_add_fetch_32(&pMgmt->state.openVnodes, 1);
×
967
    }
968
  }
969

UNCOV
970
  dInfo("thread:%d, numOfVnodes:%d, restored:%d failed:%d", pThread->threadIndex, pThread->vnodeNum, pThread->opened,
×
971
        pThread->failed);
UNCOV
972
  return NULL;
×
973
}
974

975
static int32_t vmStartVnodes(SVnodeMgmt *pMgmt) {
8✔
976
  int32_t     code = 0;
8✔
977
  int32_t     numOfVnodes = 0;
8✔
978
  SVnodeObj **ppVnodes = NULL;
8✔
979
  code = vmGetVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes);
8✔
980
  if (code != 0) {
8!
981
    dError("failed to get vnode list since %s", tstrerror(code));
×
982
    return code;
×
983
  }
984

985
  int32_t threadNum = tsNumOfCores / 2;
8✔
986
  if (threadNum < 1) threadNum = 1;
8!
987
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
8✔
988

989
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
8!
990
  if (threads == NULL) {
8!
991
    return terrno;
×
992
  }
993

994
  for (int32_t t = 0; t < threadNum; ++t) {
168✔
995
    threads[t].threadIndex = t;
160✔
996
    threads[t].pMgmt = pMgmt;
160✔
997
    threads[t].ppVnodes = taosMemoryCalloc(vnodesPerThread, sizeof(SVnode *));
160!
998
    if (threads[t].ppVnodes == NULL) {
160!
999
      code = terrno;
×
1000
      break;
×
1001
    }
1002
  }
1003

1004
  for (int32_t v = 0; v < numOfVnodes; ++v) {
8!
UNCOV
1005
    int32_t       t = v % threadNum;
×
UNCOV
1006
    SVnodeThread *pThread = &threads[t];
×
UNCOV
1007
    if (pThread->ppVnodes != NULL && ppVnodes != NULL) {
×
UNCOV
1008
      pThread->ppVnodes[pThread->vnodeNum++] = ppVnodes[v];
×
1009
    }
1010
  }
1011

1012
  pMgmt->state.openVnodes = 0;
8✔
1013
  pMgmt->state.dropVnodes = 0;
8✔
1014
  dInfo("restore %d vnodes with %d threads", numOfVnodes, threadNum);
8!
1015

1016
  for (int32_t t = 0; t < threadNum; ++t) {
168✔
1017
    SVnodeThread *pThread = &threads[t];
160✔
1018
    if (pThread->vnodeNum == 0) continue;
160!
1019

1020
    TdThreadAttr thAttr;
UNCOV
1021
    (void)taosThreadAttrInit(&thAttr);
×
UNCOV
1022
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
×
UNCOV
1023
    if (taosThreadCreate(&pThread->thread, &thAttr, vmRestoreVnodeInThread, pThread) != 0) {
×
1024
      dError("thread:%d, failed to create thread to restore vnode since %s", pThread->threadIndex, strerror(errno));
×
1025
    }
1026

UNCOV
1027
    (void)taosThreadAttrDestroy(&thAttr);
×
1028
  }
1029

1030
  for (int32_t t = 0; t < threadNum; ++t) {
168✔
1031
    SVnodeThread *pThread = &threads[t];
160✔
1032
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
160!
UNCOV
1033
      (void)taosThreadJoin(pThread->thread, NULL);
×
UNCOV
1034
      taosThreadClear(&pThread->thread);
×
1035
    }
1036
    taosMemoryFree(pThread->ppVnodes);
160!
1037
  }
1038
  taosMemoryFree(threads);
8!
1039

1040
  for (int32_t i = 0; i < numOfVnodes; ++i) {
8!
UNCOV
1041
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
×
UNCOV
1042
    vmReleaseVnode(pMgmt, ppVnodes[i]);
×
1043
  }
1044

1045
  if (ppVnodes != NULL) {
8!
1046
    taosMemoryFree(ppVnodes);
8!
1047
  }
1048

1049
  return vmInitTimer(pMgmt);
8✔
1050

1051
_exit:
1052
  for (int32_t t = 0; t < threadNum; ++t) {
1053
    SVnodeThread *pThread = &threads[t];
1054
    taosMemoryFree(pThread->ppVnodes);
1055
  }
1056
  taosMemoryFree(threads);
1057
  return code;
1058
}
1059

1060
static void vmStop(SVnodeMgmt *pMgmt) { vmCleanupTimer(pMgmt); }
8✔
1061

1062
SMgmtFunc vmGetMgmtFunc() {
8✔
1063
  SMgmtFunc mgmtFunc = {0};
8✔
1064
  mgmtFunc.openFp = vmInit;
8✔
1065
  mgmtFunc.closeFp = (NodeCloseFp)vmCleanup;
8✔
1066
  mgmtFunc.startFp = (NodeStartFp)vmStartVnodes;
8✔
1067
  mgmtFunc.stopFp = (NodeStopFp)vmStop;
8✔
1068
  mgmtFunc.requiredFp = vmRequire;
8✔
1069
  mgmtFunc.getHandlesFp = vmGetMsgHandles;
8✔
1070

1071
  return mgmtFunc;
8✔
1072
}
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