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

taosdata / TDengine / #3608

12 Feb 2025 05:57AM UTC coverage: 63.066% (+1.4%) from 61.715%
#3608

push

travis-ci

web-flow
Merge pull request #29746 from taosdata/merge/mainto3.02

merge: from main to 3.0 branch

140199 of 286257 branches covered (48.98%)

Branch coverage included in aggregate %.

89 of 161 new or added lines in 18 files covered. (55.28%)

3211 existing lines in 190 files now uncovered.

218998 of 283298 relevant lines covered (77.3%)

5949310.66 hits per line

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

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

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

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

39
  SVnodeObj *pVnode = *ppVnode;
20,591✔
40

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

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

62
  pCreatingVnode->vgId = vgId;
8,576✔
63
  pCreatingVnode->diskPrimary = diskId;
8,576✔
64

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

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

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

83
  return code;
8,576✔
84
}
85

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

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

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

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

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

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

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

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

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

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

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

184
_OVER:
8,576✔
185

186
  for (int32_t i = 0; i < numOfVnodes; ++i) {
33,005✔
187
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
24,429!
188
    vmReleaseVnode(pMgmt, ppVnodes[i]);
24,429✔
189
  }
190
  if (ppVnodes != NULL) {
8,576!
191
    taosMemoryFree(ppVnodes);
8,576!
192
  }
193

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

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

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

208
  (void)taosThreadRwlockRdlock(&pMgmt->lock);
8,712,696✔
209
  int32_t r = taosHashGetDup(pMgmt->runngingHash, &vgId, sizeof(int32_t), (void *)&pVnode);
8,720,058✔
210
  if (pVnode == NULL || strict && (pVnode->dropped || pVnode->failed)) {
8,718,295!
211
    terrno = TSDB_CODE_VND_INVALID_VGROUP_ID;
39,810✔
212
    pVnode = NULL;
40,124✔
213
  } else {
214
    int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
8,678,485✔
215
    dTrace("vgId:%d, acquire vnode, vnode:%p, ref:%d", pVnode->vgId, pVnode, refCount);
8,679,324✔
216
  }
217
  (void)taosThreadRwlockUnlock(&pMgmt->lock);
8,719,453✔
218

219
  return pVnode;
8,719,961✔
220
}
221

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

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

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

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

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

245
  return code;
10,775✔
246
}
247

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

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

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

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

285
  return code;
1,240✔
286
}
287

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

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

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

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

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

337
  (void)taosThreadRwlockWrlock(&pMgmt->lock);
10,775✔
338
  int32_t code = vmRegisterRunningState(pMgmt, pVnode);
10,775✔
339
  vmUnRegisterClosedState(pMgmt, pVnode);
10,775✔
340
  (void)taosThreadRwlockUnlock(&pMgmt->lock);
10,775✔
341

342
  return code;
10,775✔
343
}
344

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

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

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

363
  vmReleaseVnode(pMgmt, pVnode);
10,775✔
364

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

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

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

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

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

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

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

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

397
  tqNotifyClose(pVnode->pImpl->pTq);
10,775✔
398

399
  dInfo("vgId:%d, wait for vnode stream queue:%p is empty", pVnode->vgId, pVnode->pStreamQ);
10,775!
400
  while (!taosQueueEmpty(pVnode->pStreamQ)) taosMsleep(10);
10,775!
401

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

405
  dInfo("vgId:%d, all vnode queues is empty", pVnode->vgId);
10,775!
406

407
  dInfo("vgId:%d, post close", pVnode->vgId);
10,775!
408
  vnodePostClose(pVnode->pImpl);
10,775✔
409

410
  vmFreeQueue(pMgmt, pVnode);
10,775✔
411

412
  if (commitAndRemoveWal) {
10,774✔
413
    dInfo("vgId:%d, commit data for vnode split", pVnode->vgId);
30!
414
    if (vnodeSyncCommit(pVnode->pImpl) != 0) {
30!
415
      dError("vgId:%d, failed to commit data", pVnode->vgId);
×
416
    }
417
    if (vnodeBegin(pVnode->pImpl) != 0) {
30!
418
      dError("vgId:%d, failed to begin", pVnode->vgId);
×
419
    }
420
    dInfo("vgId:%d, commit data finished", pVnode->vgId);
30!
421
  }
422

423
  int32_t nodeId = vnodeNodeId(pVnode->pImpl);
10,774✔
424
  vnodeClose(pVnode->pImpl);
10,774✔
425
  pVnode->pImpl = NULL;
10,775✔
426

427
_closed:
10,775✔
428
  dInfo("vgId:%d, vnode is closed", pVnode->vgId);
10,775!
429

430
  if (commitAndRemoveWal) {
10,775✔
431
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d%swal", TD_DIRSEP, pVnode->vgId, TD_DIRSEP);
30✔
432
    dInfo("vgId:%d, remove all wals, path:%s", pVnode->vgId, path);
30!
433
    if (tfsRmdir(pMgmt->pTfs, path) != 0) {
30!
434
      dTrace("vgId:%d, failed to remove wals, path:%s", pVnode->vgId, path);
×
435
    }
436
    if (tfsMkdir(pMgmt->pTfs, path) != 0) {
30!
437
      dTrace("vgId:%d, failed to create wals, path:%s", pVnode->vgId, path);
×
438
    }
439
  }
440

441
  if (pVnode->dropped) {
10,775✔
442
    dInfo("vgId:%d, vnode is destroyed, dropped:%d", pVnode->vgId, pVnode->dropped);
3,417!
443
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pVnode->vgId);
3,417✔
444
    vnodeDestroy(pVnode->vgId, path, pMgmt->pTfs, nodeId);
3,417✔
445
  }
446

447
  vmFreeVnodeObj(&pVnode);
10,775✔
448
}
449

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

465
static int32_t vmRestoreVgroupId(SWrapperCfg *pCfg, STfs *pTfs) {
×
466
  int32_t srcVgId = pCfg->vgId;
×
467
  int32_t dstVgId = pCfg->toVgId;
×
468
  if (dstVgId == 0) return 0;
×
469

470
  char srcPath[TSDB_FILENAME_LEN];
471
  char dstPath[TSDB_FILENAME_LEN];
472

473
  snprintf(srcPath, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, srcVgId);
×
474
  snprintf(dstPath, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, dstVgId);
×
475

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

483
  pCfg->vgId = vgId;
×
484
  pCfg->toVgId = 0;
×
485
  return 0;
×
486
}
487

488
static void *vmOpenVnodeInThread(void *param) {
924✔
489
  SVnodeThread *pThread = param;
924✔
490
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
924✔
491
  char          path[TSDB_FILENAME_LEN];
492

493
  dInfo("thread:%d, start to open or destroy %d vnodes", pThread->threadIndex, pThread->vnodeNum);
924!
494
  setThreadName("open-vnodes");
924✔
495

496
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
1,853✔
497
    SWrapperCfg *pCfg = &pThread->pCfgs[v];
929✔
498
    if (pCfg->dropped) {
929!
499
      char stepDesc[TSDB_STEP_DESC_LEN] = {0};
×
500
      snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to destroy, %d of %d have been dropped", pCfg->vgId,
×
501
               pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
502
      tmsgReportStartup("vnode-destroy", stepDesc);
×
503

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

512
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
929✔
513
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been opened", pCfg->vgId,
929✔
514
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
515
    tmsgReportStartup("vnode-open", stepDesc);
929✔
516

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

526
    int32_t diskPrimary = pCfg->diskPrimary;
929✔
527
    snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pCfg->vgId);
929✔
528

529
    SVnode *pImpl = vnodeOpen(path, diskPrimary, pMgmt->pTfs, pMgmt->msgCb, false);
929✔
530

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

539
    if (vmOpenVnode(pMgmt, pCfg, pImpl) != 0) {
929!
540
      dError("vgId:%d, failed to open vnode by thread:%d", pCfg->vgId, pThread->threadIndex);
×
541
      pThread->failed++;
×
542
      continue;
×
543
    }
544

545
    dInfo("vgId:%d, is opened by thread:%d", pCfg->vgId, pThread->threadIndex);
929!
546
    pThread->opened++;
929✔
547
    (void)atomic_add_fetch_32(&pMgmt->state.openVnodes, 1);
929✔
548
  }
549

550
  dInfo("thread:%d, numOfVnodes:%d, opened:%d dropped:%d failed:%d", pThread->threadIndex, pThread->vnodeNum,
924!
551
        pThread->opened, pThread->dropped, pThread->failed);
552
  return NULL;
924✔
553
}
554

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

563
  pMgmt->closedHash =
2,152✔
564
      taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
2,152✔
565
  if (pMgmt->closedHash == NULL) {
2,152!
566
    dError("failed to init vnode closed hash since %s", terrstr());
×
567
    return TSDB_CODE_OUT_OF_MEMORY;
×
568
  }
569

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

577
  SWrapperCfg *pCfgs = NULL;
2,152✔
578
  int32_t      numOfVnodes = 0;
2,152✔
579
  if (vmGetVnodeListFromFile(pMgmt, &pCfgs, &numOfVnodes) != 0) {
2,152!
580
    dInfo("failed to get vnode list from disk since %s", terrstr());
×
581
    return -1;
×
582
  }
583

584
  pMgmt->state.totalVnodes = numOfVnodes;
2,152✔
585

586
  int32_t threadNum = tsNumOfCores / 2;
2,152✔
587
  if (threadNum < 1) threadNum = 1;
2,152!
588
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
2,152✔
589

590
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
2,152!
591
  if (threads == NULL) {
2,152!
592
    dError("failed to allocate memory for threads since %s", terrstr());
×
593
    taosMemoryFree(pCfgs);
×
594
    return terrno;
×
595
  }
596

597
  for (int32_t t = 0; t < threadNum; ++t) {
45,192✔
598
    threads[t].threadIndex = t;
43,040✔
599
    threads[t].pMgmt = pMgmt;
43,040✔
600
    threads[t].pCfgs = taosMemoryCalloc(vnodesPerThread, sizeof(SWrapperCfg));
43,040!
601
  }
602

603
  for (int32_t v = 0; v < numOfVnodes; ++v) {
3,081✔
604
    int32_t       t = v % threadNum;
929✔
605
    SVnodeThread *pThread = &threads[t];
929✔
606
    pThread->pCfgs[pThread->vnodeNum++] = pCfgs[v];
929✔
607
  }
608

609
  dInfo("open %d vnodes with %d threads", numOfVnodes, threadNum);
2,152!
610

611
  for (int32_t t = 0; t < threadNum; ++t) {
45,192✔
612
    SVnodeThread *pThread = &threads[t];
43,040✔
613
    if (pThread->vnodeNum == 0) continue;
43,040✔
614

615
    TdThreadAttr thAttr;
616
    (void)taosThreadAttrInit(&thAttr);
924✔
617
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
924✔
618
    if (taosThreadCreate(&pThread->thread, &thAttr, vmOpenVnodeInThread, pThread) != 0) {
924!
619
      dError("thread:%d, failed to create thread to open vnode, reason:%s", pThread->threadIndex, strerror(errno));
×
620
    }
621

622
    (void)taosThreadAttrDestroy(&thAttr);
924✔
623
  }
624

625
  bool updateVnodesList = false;
2,152✔
626

627
  for (int32_t t = 0; t < threadNum; ++t) {
45,192✔
628
    SVnodeThread *pThread = &threads[t];
43,040✔
629
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
43,040!
630
      (void)taosThreadJoin(pThread->thread, NULL);
924✔
631
      taosThreadClear(&pThread->thread);
924✔
632
    }
633
    taosMemoryFree(pThread->pCfgs);
43,040!
634
    if (pThread->updateVnodesList) updateVnodesList = true;
43,040!
635
  }
636
  taosMemoryFree(threads);
2,152!
637
  taosMemoryFree(pCfgs);
2,152!
638

639
  if ((pMgmt->state.openVnodes + pMgmt->state.dropVnodes) != pMgmt->state.totalVnodes) {
2,152!
640
    dError("there are total vnodes:%d, opened:%d", pMgmt->state.totalVnodes, pMgmt->state.openVnodes);
×
641
    terrno = TSDB_CODE_VND_INIT_FAILED;
×
642
    return -1;
×
643
  }
644

645
  if (updateVnodesList && vmWriteVnodeListToFile(pMgmt) != 0) {
2,152!
646
    dError("failed to write vnode list since %s", terrstr());
×
647
    return -1;
×
648
  }
649

650
  dInfo("successfully opened %d vnodes", pMgmt->state.totalVnodes);
2,152!
651
  return 0;
2,152✔
652
}
653

654
static void *vmCloseVnodeInThread(void *param) {
6,022✔
655
  SVnodeThread *pThread = param;
6,022✔
656
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
6,022✔
657

658
  dInfo("thread:%d, start to close %d vnodes", pThread->threadIndex, pThread->vnodeNum);
6,022✔
659
  setThreadName("close-vnodes");
6,025✔
660

661
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
12,111✔
662
    SVnodeObj *pVnode = pThread->ppVnodes[v];
6,088✔
663

664
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
6,088✔
665
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to close, %d of %d have been closed", pVnode->vgId,
6,088✔
666
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
667
    tmsgReportStartup("vnode-close", stepDesc);
6,088✔
668

669
    vmCloseVnode(pMgmt, pVnode, false, false);
6,088✔
670
  }
671

672
  dInfo("thread:%d, numOfVnodes:%d is closed", pThread->threadIndex, pThread->vnodeNum);
6,023!
673
  return NULL;
6,023✔
674
}
675

676
static void vmCloseVnodes(SVnodeMgmt *pMgmt) {
2,152✔
677
  int32_t code = 0;
2,152✔
678
  dInfo("start to close all vnodes");
2,152!
679
  tSingleWorkerCleanup(&pMgmt->mgmtWorker);
2,152✔
680
  dInfo("vnodes mgmt worker is stopped");
2,152!
681
  tSingleWorkerCleanup(&pMgmt->mgmtMultiWorker);
2,152✔
682
  dInfo("vnodes multiple mgmt worker is stopped");
2,152!
683

684
  int32_t     numOfVnodes = 0;
2,152✔
685
  SVnodeObj **ppVnodes = NULL;
2,152✔
686
  code = vmGetVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes);
2,152✔
687
  if (code != 0) {
2,152!
688
    dError("failed to get vnode list since %s", tstrerror(code));
×
689
    return;
×
690
  }
691

692
  int32_t threadNum = tsNumOfCores / 2;
2,152✔
693
  if (threadNum < 1) threadNum = 1;
2,152!
694
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
2,152✔
695

696
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
2,152!
697
  for (int32_t t = 0; t < threadNum; ++t) {
45,192✔
698
    threads[t].threadIndex = t;
43,040✔
699
    threads[t].pMgmt = pMgmt;
43,040✔
700
    threads[t].ppVnodes = taosMemoryCalloc(vnodesPerThread, sizeof(SVnode *));
43,040!
701
  }
702

703
  for (int32_t v = 0; v < numOfVnodes; ++v) {
8,240✔
704
    int32_t       t = v % threadNum;
6,088✔
705
    SVnodeThread *pThread = &threads[t];
6,088✔
706
    if (pThread->ppVnodes != NULL && ppVnodes != NULL) {
6,088!
707
      pThread->ppVnodes[pThread->vnodeNum++] = ppVnodes[v];
6,088✔
708
    }
709
  }
710

711
  pMgmt->state.openVnodes = 0;
2,152✔
712
  dInfo("close %d vnodes with %d threads", numOfVnodes, threadNum);
2,152!
713

714
  int64_t st = taosGetTimestampMs();
2,152✔
715
  dInfo("notify all streams closed in all %d vnodes, ts:%" PRId64, numOfVnodes, st);
2,152!
716
  if (ppVnodes != NULL) {
2,152!
717
    for (int32_t i = 0; i < numOfVnodes; ++i) {
8,240✔
718
      if (ppVnodes[i] != NULL) {
6,088!
719
        if (ppVnodes[i]->pImpl != NULL) {
6,088!
720
          tqNotifyClose(ppVnodes[i]->pImpl->pTq);
6,088✔
721
        }
722
      }
723
    }
724
  }
725

726
  int64_t et = taosGetTimestampMs();
2,152✔
727
  dInfo("notify close stream completed in %d vnodes, elapsed time: %" PRId64 "ms", numOfVnodes, et - st);
2,152!
728

729
  for (int32_t t = 0; t < threadNum; ++t) {
45,192✔
730
    SVnodeThread *pThread = &threads[t];
43,040✔
731
    if (pThread->vnodeNum == 0) continue;
43,040✔
732

733
    TdThreadAttr thAttr;
734
    (void)taosThreadAttrInit(&thAttr);
6,023✔
735
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
6,023✔
736

737
    if (taosThreadCreate(&pThread->thread, &thAttr, vmCloseVnodeInThread, pThread) != 0) {
6,023!
738
      dError("thread:%d, failed to create thread to close vnode since %s", pThread->threadIndex, strerror(errno));
×
739
    }
740

741
    (void)taosThreadAttrDestroy(&thAttr);
6,023✔
742
  }
743

744
  for (int32_t t = 0; t < threadNum; ++t) {
45,192✔
745
    SVnodeThread *pThread = &threads[t];
43,040✔
746
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
43,040!
747
      (void)taosThreadJoin(pThread->thread, NULL);
6,023✔
748
      taosThreadClear(&pThread->thread);
6,023✔
749
    }
750
    taosMemoryFree(pThread->ppVnodes);
43,040!
751
  }
752
  taosMemoryFree(threads);
2,152!
753

754
  if (ppVnodes != NULL) {
2,152!
755
    taosMemoryFree(ppVnodes);
2,152!
756
  }
757

758
  if (pMgmt->runngingHash != NULL) {
2,152!
759
    taosHashCleanup(pMgmt->runngingHash);
2,152✔
760
    pMgmt->runngingHash = NULL;
2,152✔
761
  }
762

763
  void *pIter = taosHashIterate(pMgmt->closedHash, NULL);
2,152✔
764
  while (pIter) {
2,152!
765
    SVnodeObj **ppVnode = pIter;
×
766
    vmFreeVnodeObj(ppVnode);
×
767
    pIter = taosHashIterate(pMgmt->closedHash, pIter);
×
768
  }
769

770
  if (pMgmt->closedHash != NULL) {
2,152!
771
    taosHashCleanup(pMgmt->closedHash);
2,152✔
772
    pMgmt->closedHash = NULL;
2,152✔
773
  }
774

775
  pIter = taosHashIterate(pMgmt->creatingHash, NULL);
2,152✔
776
  while (pIter) {
2,152!
777
    SVnodeObj **ppVnode = pIter;
×
778
    vmFreeVnodeObj(ppVnode);
×
779
    pIter = taosHashIterate(pMgmt->creatingHash, pIter);
×
780
  }
781

782
  if (pMgmt->creatingHash != NULL) {
2,152!
783
    taosHashCleanup(pMgmt->creatingHash);
2,152✔
784
    pMgmt->creatingHash = NULL;
2,152✔
785
  }
786

787
  dInfo("total vnodes:%d are all closed", numOfVnodes);
2,152!
788
}
789

790
static void vmCleanup(SVnodeMgmt *pMgmt) {
2,152✔
791
  vmCloseVnodes(pMgmt);
2,152✔
792
  vmStopWorker(pMgmt);
2,152✔
793
  vnodeCleanup();
2,152✔
794
  (void)taosThreadRwlockDestroy(&pMgmt->lock);
2,152✔
795
  (void)taosThreadMutexDestroy(&pMgmt->mutex);
2,152✔
796
  (void)taosThreadMutexDestroy(&pMgmt->fileLock);
2,152✔
797
  taosMemoryFree(pMgmt);
2,152!
798
}
2,152✔
799

800
static void vmCheckSyncTimeout(SVnodeMgmt *pMgmt) {
1,658✔
801
  int32_t     code = 0;
1,658✔
802
  int32_t     numOfVnodes = 0;
1,658✔
803
  SVnodeObj **ppVnodes = NULL;
1,658✔
804
  code = vmGetVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes);
1,658✔
805
  if (code != 0) {
1,658!
806
    dError("failed to get vnode list since %s", tstrerror(code));
×
807
    return;
×
808
  }
809

810
  if (ppVnodes != NULL) {
1,658!
811
    for (int32_t i = 0; i < numOfVnodes; ++i) {
6,495✔
812
      SVnodeObj *pVnode = ppVnodes[i];
4,837✔
813
      if (!pVnode->failed) {
4,837!
814
        vnodeSyncCheckTimeout(pVnode->pImpl);
4,837✔
815
      }
816
      vmReleaseVnode(pMgmt, pVnode);
4,837✔
817
    }
818
    taosMemoryFree(ppVnodes);
1,658!
819
  }
820
}
821

822
static void *vmThreadFp(void *param) {
2,152✔
823
  SVnodeMgmt *pMgmt = param;
2,152✔
824
  int64_t     lastTime = 0;
2,152✔
825
  setThreadName("vnode-timer");
2,152✔
826

827
  while (1) {
722,574✔
828
    lastTime++;
724,726✔
829
    taosMsleep(100);
724,726✔
830
    if (pMgmt->stop) break;
724,726✔
831
    if (lastTime % 10 != 0) continue;
722,574✔
832

833
    int64_t sec = lastTime / 10;
71,286✔
834
    if (sec % (VNODE_TIMEOUT_SEC / 2) == 0) {
71,286✔
835
      vmCheckSyncTimeout(pMgmt);
1,658✔
836
    }
837
  }
838

839
  return NULL;
2,152✔
840
}
841

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

853
  (void)taosThreadAttrDestroy(&thAttr);
2,152✔
854
  return 0;
2,152✔
855
}
856

857
static void vmCleanupTimer(SVnodeMgmt *pMgmt) {
2,152✔
858
  pMgmt->stop = true;
2,152✔
859
  if (taosCheckPthreadValid(pMgmt->thread)) {
2,152!
860
    (void)taosThreadJoin(pMgmt->thread, NULL);
2,152✔
861
    taosThreadClear(&pMgmt->thread);
2,152✔
862
  }
863
}
2,152✔
864

865
static int32_t vmInit(SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) {
2,152✔
866
  int32_t code = -1;
2,152✔
867

868
  SVnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SVnodeMgmt));
2,152!
869
  if (pMgmt == NULL) {
2,152!
870
    code = terrno;
×
871
    goto _OVER;
×
872
  }
873

874
  pMgmt->pData = pInput->pData;
2,152✔
875
  pMgmt->path = pInput->path;
2,152✔
876
  pMgmt->name = pInput->name;
2,152✔
877
  pMgmt->msgCb = pInput->msgCb;
2,152✔
878
  pMgmt->msgCb.putToQueueFp = (PutToQueueFp)vmPutRpcMsgToQueue;
2,152✔
879
  pMgmt->msgCb.qsizeFp = (GetQueueSizeFp)vmGetQueueSize;
2,152✔
880
  pMgmt->msgCb.mgmt = pMgmt;
2,152✔
881

882
  code = taosThreadRwlockInit(&pMgmt->lock, NULL);
2,152✔
883
  if (code != 0) {
2,152!
884
    code = TAOS_SYSTEM_ERROR(errno);
×
885
    goto _OVER;
×
886
  }
887

888
  code = taosThreadMutexInit(&pMgmt->mutex, NULL);
2,152✔
889
  if (code != 0) {
2,152!
890
    code = TAOS_SYSTEM_ERROR(errno);
×
891
    goto _OVER;
×
892
  }
893

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

900
  pMgmt->pTfs = pInput->pTfs;
2,152✔
901
  if (pMgmt->pTfs == NULL) {
2,152!
902
    dError("tfs is null.");
×
903
    goto _OVER;
×
904
  }
905
  tmsgReportStartup("vnode-tfs", "initialized");
2,152✔
906
  if ((code = walInit(pInput->stopDnodeFp)) != 0) {
2,152!
907
    dError("failed to init wal since %s", tstrerror(code));
×
908
    goto _OVER;
×
909
  }
910

911
  tmsgReportStartup("vnode-wal", "initialized");
2,152✔
912

913
  if ((code = syncInit()) != 0) {
2,152!
914
    dError("failed to open sync since %s", tstrerror(code));
×
915
    goto _OVER;
×
916
  }
917
  tmsgReportStartup("vnode-sync", "initialized");
2,152✔
918

919
  if ((code = vnodeInit(pInput->stopDnodeFp)) != 0) {
2,152!
920
    dError("failed to init vnode since %s", tstrerror(code));
×
921
    goto _OVER;
×
922
  }
923
  tmsgReportStartup("vnode-commit", "initialized");
2,152✔
924

925
  if ((code = vmStartWorker(pMgmt)) != 0) {
2,152!
926
    dError("failed to init workers since %s", tstrerror(code));
×
927
    goto _OVER;
×
928
  }
929
  tmsgReportStartup("vnode-worker", "initialized");
2,152✔
930

931
  if ((code = vmOpenVnodes(pMgmt)) != 0) {
2,152!
932
    dError("failed to open all vnodes since %s", tstrerror(code));
×
933
    goto _OVER;
×
934
  }
935
  tmsgReportStartup("vnode-vnodes", "initialized");
2,152✔
936

937
  if ((code = udfcOpen()) != 0) {
2,152!
938
    dError("failed to open udfc in vnode since %s", tstrerror(code));
×
939
    goto _OVER;
×
940
  }
941

942
  code = 0;
2,152✔
943

944
_OVER:
2,152✔
945
  if (code == 0) {
2,152!
946
    pOutput->pMgmt = pMgmt;
2,152✔
947
  } else {
948
    dError("failed to init vnodes-mgmt since %s", tstrerror(code));
×
949
    vmCleanup(pMgmt);
×
950
  }
951

952
  return code;
2,152✔
953
}
954

955
static int32_t vmRequire(const SMgmtInputOpt *pInput, bool *required) {
2,190✔
956
  *required = tsNumOfSupportVnodes > 0;
2,190✔
957
  return 0;
2,190✔
958
}
959

960
static void *vmRestoreVnodeInThread(void *param) {
924✔
961
  SVnodeThread *pThread = param;
924✔
962
  SVnodeMgmt   *pMgmt = pThread->pMgmt;
924✔
963

964
  dInfo("thread:%d, start to restore %d vnodes", pThread->threadIndex, pThread->vnodeNum);
924!
965
  setThreadName("restore-vnodes");
924✔
966

967
  for (int32_t v = 0; v < pThread->vnodeNum; ++v) {
1,853✔
968
    SVnodeObj *pVnode = pThread->ppVnodes[v];
929✔
969
    if (pVnode->failed) {
929!
UNCOV
970
      dError("vgId:%d, cannot restore a vnode in failed mode.", pVnode->vgId);
×
UNCOV
971
      continue;
×
972
    }
973

974
    char stepDesc[TSDB_STEP_DESC_LEN] = {0};
929✔
975
    snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been restored", pVnode->vgId,
929✔
976
             pMgmt->state.openVnodes, pMgmt->state.totalVnodes);
977
    tmsgReportStartup("vnode-restore", stepDesc);
929✔
978

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

990
  dInfo("thread:%d, numOfVnodes:%d, restored:%d failed:%d", pThread->threadIndex, pThread->vnodeNum, pThread->opened,
924!
991
        pThread->failed);
992
  return NULL;
924✔
993
}
994

995
static int32_t vmStartVnodes(SVnodeMgmt *pMgmt) {
2,152✔
996
  int32_t     code = 0;
2,152✔
997
  int32_t     numOfVnodes = 0;
2,152✔
998
  SVnodeObj **ppVnodes = NULL;
2,152✔
999
  code = vmGetVnodeListFromHash(pMgmt, &numOfVnodes, &ppVnodes);
2,152✔
1000
  if (code != 0) {
2,152!
1001
    dError("failed to get vnode list since %s", tstrerror(code));
×
1002
    return code;
×
1003
  }
1004

1005
  int32_t threadNum = tsNumOfCores / 2;
2,152✔
1006
  if (threadNum < 1) threadNum = 1;
2,152!
1007
  int32_t vnodesPerThread = numOfVnodes / threadNum + 1;
2,152✔
1008

1009
  SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread));
2,152!
1010
  if (threads == NULL) {
2,152!
1011
    return terrno;
×
1012
  }
1013

1014
  for (int32_t t = 0; t < threadNum; ++t) {
45,192✔
1015
    threads[t].threadIndex = t;
43,040✔
1016
    threads[t].pMgmt = pMgmt;
43,040✔
1017
    threads[t].ppVnodes = taosMemoryCalloc(vnodesPerThread, sizeof(SVnode *));
43,040!
1018
    if (threads[t].ppVnodes == NULL) {
43,040!
1019
      code = terrno;
×
1020
      break;
×
1021
    }
1022
  }
1023

1024
  for (int32_t v = 0; v < numOfVnodes; ++v) {
3,081✔
1025
    int32_t       t = v % threadNum;
929✔
1026
    SVnodeThread *pThread = &threads[t];
929✔
1027
    if (pThread->ppVnodes != NULL && ppVnodes != NULL) {
929!
1028
      pThread->ppVnodes[pThread->vnodeNum++] = ppVnodes[v];
929✔
1029
    }
1030
  }
1031

1032
  pMgmt->state.openVnodes = 0;
2,152✔
1033
  pMgmt->state.dropVnodes = 0;
2,152✔
1034
  dInfo("restore %d vnodes with %d threads", numOfVnodes, threadNum);
2,152!
1035

1036
  for (int32_t t = 0; t < threadNum; ++t) {
45,192✔
1037
    SVnodeThread *pThread = &threads[t];
43,040✔
1038
    if (pThread->vnodeNum == 0) continue;
43,040✔
1039

1040
    TdThreadAttr thAttr;
1041
    (void)taosThreadAttrInit(&thAttr);
924✔
1042
    (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
924✔
1043
    if (taosThreadCreate(&pThread->thread, &thAttr, vmRestoreVnodeInThread, pThread) != 0) {
924!
1044
      dError("thread:%d, failed to create thread to restore vnode since %s", pThread->threadIndex, strerror(errno));
×
1045
    }
1046

1047
    (void)taosThreadAttrDestroy(&thAttr);
924✔
1048
  }
1049

1050
  for (int32_t t = 0; t < threadNum; ++t) {
45,192✔
1051
    SVnodeThread *pThread = &threads[t];
43,040✔
1052
    if (pThread->vnodeNum > 0 && taosCheckPthreadValid(pThread->thread)) {
43,040!
1053
      (void)taosThreadJoin(pThread->thread, NULL);
924✔
1054
      taosThreadClear(&pThread->thread);
924✔
1055
    }
1056
    taosMemoryFree(pThread->ppVnodes);
43,040!
1057
  }
1058
  taosMemoryFree(threads);
2,152!
1059

1060
  for (int32_t i = 0; i < numOfVnodes; ++i) {
3,081✔
1061
    if (ppVnodes == NULL || ppVnodes[i] == NULL) continue;
929!
1062
    vmReleaseVnode(pMgmt, ppVnodes[i]);
929✔
1063
  }
1064

1065
  if (ppVnodes != NULL) {
2,152!
1066
    taosMemoryFree(ppVnodes);
2,152!
1067
  }
1068

1069
  return vmInitTimer(pMgmt);
2,152✔
1070

1071
_exit:
1072
  for (int32_t t = 0; t < threadNum; ++t) {
1073
    SVnodeThread *pThread = &threads[t];
1074
    taosMemoryFree(pThread->ppVnodes);
1075
  }
1076
  taosMemoryFree(threads);
1077
  return code;
1078
}
1079

1080
static void vmStop(SVnodeMgmt *pMgmt) { vmCleanupTimer(pMgmt); }
2,152✔
1081

1082
SMgmtFunc vmGetMgmtFunc() {
2,190✔
1083
  SMgmtFunc mgmtFunc = {0};
2,190✔
1084
  mgmtFunc.openFp = vmInit;
2,190✔
1085
  mgmtFunc.closeFp = (NodeCloseFp)vmCleanup;
2,190✔
1086
  mgmtFunc.startFp = (NodeStartFp)vmStartVnodes;
2,190✔
1087
  mgmtFunc.stopFp = (NodeStopFp)vmStop;
2,190✔
1088
  mgmtFunc.requiredFp = vmRequire;
2,190✔
1089
  mgmtFunc.getHandlesFp = vmGetMsgHandles;
2,190✔
1090

1091
  return mgmtFunc;
2,190✔
1092
}
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