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

taosdata / TDengine / #4473

08 Jul 2025 09:38AM UTC coverage: 62.922% (+0.7%) from 62.22%
#4473

push

travis-ci

web-flow
Merge pull request #31712 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

158525 of 321496 branches covered (49.31%)

Branch coverage included in aggregate %.

56 of 60 new or added lines in 13 files covered. (93.33%)

1333 existing lines in 67 files now uncovered.

245526 of 320647 relevant lines covered (76.57%)

17689640.25 hits per line

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

69.71
/source/dnode/mgmt/mgmt_vnode/src/vmWorker.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 "vnodeInt.h"
19

20
static inline void vmSendRsp(SRpcMsg *pMsg, int32_t code) {
30,227✔
21
  if (pMsg->info.handle == NULL) return;
30,227✔
22
  SRpcMsg rsp = {
30,046✔
23
      .code = code,
24
      .pCont = pMsg->info.rsp,
30,046✔
25
      .contLen = pMsg->info.rspLen,
30,046✔
26
      .info = pMsg->info,
27
  };
28
  tmsgSendRsp(&rsp);
30,046✔
29
}
30

31
static void vmProcessMultiMgmtQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
12,403✔
32
  SVnodeMgmt     *pMgmt = pInfo->ahandle;
12,403✔
33
  int32_t         code = -1;
12,403✔
34
  const STraceId *trace = &pMsg->info.traceId;
12,403✔
35

36
  dGTrace("msg:%p, get from vnode-multi-mgmt queue", pMsg);
12,403!
37
  switch (pMsg->msgType) {
12,403!
38
    case TDMT_DND_CREATE_VNODE:
12,403✔
39
      code = vmProcessCreateVnodeReq(pMgmt, pMsg);
12,403✔
40
      break;
12,403✔
41
  }
42

43
  if (IsReq(pMsg)) {
12,403!
44
    if (code != 0) {
12,403!
45
      if (terrno != 0) code = terrno;
×
46
      dGError("msg:%p, failed to process since %s, type:%s", pMsg, tstrerror(code), TMSG_INFO(pMsg->msgType));
×
47
    }
48
    vmSendRsp(pMsg, code);
12,403✔
49
  }
50

51
  dGTrace("msg:%p, is freed, code:0x%x", pMsg, code);
12,403!
52
  rpcFreeCont(pMsg->pCont);
12,403✔
53
  taosFreeQitem(pMsg);
12,403✔
54
}
12,403✔
55

56
static void vmProcessMgmtQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
13,513✔
57
  SVnodeMgmt     *pMgmt = pInfo->ahandle;
13,513✔
58
  int32_t         code = -1;
13,513✔
59
  const STraceId *trace = &pMsg->info.traceId;
13,513✔
60

61
  dGTrace("msg:%p, get from vnode-mgmt queue", pMsg);
13,513!
62
  switch (pMsg->msgType) {
13,513!
63
    case TDMT_DND_CREATE_VNODE:
×
64
      code = vmProcessCreateVnodeReq(pMgmt, pMsg);
×
65
      break;
×
66
    case TDMT_DND_DROP_VNODE:
5,554✔
67
      code = vmProcessDropVnodeReq(pMgmt, pMsg);
5,554✔
68
      break;
5,554✔
69
    case TDMT_VND_ALTER_REPLICA:
1,841✔
70
      code = vmProcessAlterVnodeReplicaReq(pMgmt, pMsg);
1,841✔
71
      break;
1,841✔
72
    case TDMT_VND_DISABLE_WRITE:
46✔
73
      code = vmProcessDisableVnodeWriteReq(pMgmt, pMsg);
46✔
74
      break;
46✔
75
    case TDMT_VND_ALTER_HASHRANGE:
44✔
76
      code = vmProcessAlterHashRangeReq(pMgmt, pMsg);
44✔
77
      break;
44✔
78
    case TDMT_DND_ALTER_VNODE_TYPE:
5,933✔
79
      code = vmProcessAlterVnodeTypeReq(pMgmt, pMsg);
5,933✔
80
      break;
5,933✔
81
    case TDMT_DND_CHECK_VNODE_LEARNER_CATCHUP:
×
82
      code = vmProcessCheckLearnCatchupReq(pMgmt, pMsg);
×
83
      break;
×
84
    case TDMT_VND_ARB_HEARTBEAT:
95✔
85
      code = vmProcessArbHeartBeatReq(pMgmt, pMsg);
95✔
86
      break;
95✔
87
    default:
×
88
      terrno = TSDB_CODE_MSG_NOT_PROCESSED;
×
89
      dGError("msg:%p, not processed in vnode-mgmt queue", pMsg);
×
90
  }
91

92
  if (IsReq(pMsg)) {
13,513!
93
    if (code != 0) {
13,513✔
94
      if (terrno != 0) code = terrno;
5,665!
95
      dGError("msg:%p, failed to process since %s, type:%s", pMsg, tstrerror(code), TMSG_INFO(pMsg->msgType));
5,665!
96
    }
97
    vmSendRsp(pMsg, code);
13,513✔
98
  }
99

100
  dGTrace("msg:%p, is freed, code:0x%x", pMsg, code);
13,513!
101
  rpcFreeCont(pMsg->pCont);
13,513✔
102
  taosFreeQitem(pMsg);
13,513✔
103
}
13,513✔
104

105
static void vmProcessQueryQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
8,298,532✔
106
  SVnodeObj      *pVnode = pInfo->ahandle;
8,298,532✔
107
  const STraceId *trace = &pMsg->info.traceId;
8,298,532✔
108

109
  dGTrace("vgId:%d, msg:%p, get from vnode-query queue", pVnode->vgId, pMsg);
8,298,532!
110
  int32_t code = vnodeProcessQueryMsg(pVnode->pImpl, pMsg, pInfo);
8,298,532✔
111
  if (code != 0) {
8,294,256✔
112
    if (terrno != 0) code = terrno;
1,319!
113
    dGError("vgId:%d, msg:%p, failed to query since %s", pVnode->vgId, pMsg, tstrerror(code));
1,319!
114
    vmSendRsp(pMsg, code);
1,319✔
115
  }
116

117
  dGTrace("vgId:%d, msg:%p, is freed, code:0x%x", pVnode->vgId, pMsg, code);
8,294,255!
118
  rpcFreeCont(pMsg->pCont);
8,294,255✔
119
  taosFreeQitem(pMsg);
8,298,502✔
120
}
8,298,685✔
121

122
static void vmProcessStreamQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
263,770✔
123
#ifdef USE_STREAM
124
  SVnodeObj      *pVnode = pInfo->ahandle;
263,770✔
125
  const STraceId *trace = &pMsg->info.traceId;
263,770✔
126

127
  dGTrace("vgId:%d, msg:%p, get from vnode-stream queue", pVnode->vgId, pMsg);
263,770!
128
  int32_t code = vnodeProcessStreamMsg(pVnode->pImpl, pMsg, pInfo);
263,770✔
129
  if (code != 0) {
263,455✔
130
    terrno = code;
5✔
131
    dGError("vgId:%d, msg:%p, failed to process stream msg %s since %s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType),
5!
132
            tstrerror(code));
133
    vmSendRsp(pMsg, code);
5✔
134
  }
135

136
  dGTrace("vgId:%d, msg:%p, is freed, code:0x%x", pVnode->vgId, pMsg, code);
263,455!
137
  rpcFreeCont(pMsg->pCont);
263,455✔
138
  taosFreeQitem(pMsg);
263,793✔
139
#endif
140
}
263,824✔
141

142
static void vmProcessStreamCtrlQueue(SQueueInfo *pInfo, STaosQall* pQall, int32_t numOfItems) {
104,912✔
143
#ifdef USE_STREAM
144
  SVnodeObj *pVnode = pInfo->ahandle;
104,912✔
145
  void      *pItem = NULL;
104,912✔
146
  int32_t    code = 0;
104,912✔
147

148
  while (1) {
117,493✔
149
    if (taosGetQitem(pQall, &pItem) == 0) {
222,405✔
150
      break;
104,966✔
151
    }
152

153
    SRpcMsg        *pMsg = pItem;
117,415✔
154
    const STraceId *trace = &pMsg->info.traceId;
117,415✔
155

156
    dGTrace("vgId:%d, msg:%p, get from vnode-stream-ctrl queue", pVnode->vgId, pMsg);
117,415!
157
    code = vnodeProcessStreamCtrlMsg(pVnode->pImpl, pMsg, pInfo);
117,415✔
158
    if (code != 0) {
117,439✔
159
      terrno = code;
161✔
160
      dGError("vgId:%d, msg:%p, failed to process stream ctrl msg %s since %s", pVnode->vgId, pMsg,
161!
161
              TMSG_INFO(pMsg->msgType), tstrerror(code));
162
      vmSendRsp(pMsg, code);
161✔
163
    }
164

165
    dGTrace("vgId:%d, msg:%p, is freed, code:0x%x", pVnode->vgId, pMsg, code);
117,439!
166
    rpcFreeCont(pMsg->pCont);
117,439✔
167
    taosFreeQitem(pMsg);
117,489✔
168
  }
169
#endif
170
}
104,966✔
171

172
static void vmProcessStreamChkptQueue(SQueueInfo *pInfo, STaosQall* pQall, int32_t numOfItems) {
7,533✔
173
  SVnodeObj *pVnode = pInfo->ahandle;
7,533✔
174
  void      *pItem = NULL;
7,533✔
175
  int32_t    code = 0;
7,533✔
176

177
  while (1) {
7,985✔
178
    if (taosGetQitem(pQall, &pItem) == 0) {
15,518✔
179
      break;
7,533✔
180
    }
181

182
    SRpcMsg        *pMsg = pItem;
7,985✔
183
    const STraceId *trace = &pMsg->info.traceId;
7,985✔
184

185
    dGTrace("vgId:%d, msg:%p, get from vnode-stream-chkpt queue", pVnode->vgId, pMsg);
7,985!
186
    code = vnodeProcessStreamChkptMsg(pVnode->pImpl, pMsg, pInfo);
7,985✔
187
    if (code != 0) {
7,985✔
188
      terrno = code;
14✔
189
      dGError("vgId:%d, msg:%p, failed to process stream chkpt msg %s since %s", pVnode->vgId, pMsg,
14!
190
              TMSG_INFO(pMsg->msgType), tstrerror(code));
191
      vmSendRsp(pMsg, code);
14✔
192
    }
193

194
    dGTrace("vgId:%d, msg:%p, is freed, code:0x%x", pVnode->vgId, pMsg, code);
7,985!
195
    rpcFreeCont(pMsg->pCont);
7,985✔
196
    taosFreeQitem(pMsg);
7,985✔
197
  }
198
}
7,533✔
199

200
static void vmProcessStreamLongExecQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
1,761✔
201
  SVnodeObj      *pVnode = pInfo->ahandle;
1,761✔
202
  const STraceId *trace = &pMsg->info.traceId;
1,761✔
203
  int32_t         code = 0;
1,761✔
204

205
  dGTrace("vgId:%d, msg:%p, get from vnode-stream long-exec queue", pVnode->vgId, pMsg);
1,761!
206

207
  code = vnodeProcessStreamLongExecMsg(pVnode->pImpl, pMsg, pInfo);
1,761✔
208
  if (code != 0) {
1,761✔
209
    terrno = code;
1✔
210
    dGError("vgId:%d, msg:%p, failed to process stream msg %s since %s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType),
1!
211
            tstrerror(code));
212
    vmSendRsp(pMsg, code);
1✔
213
  }
214

215
  dGTrace("vgId:%d, msg:%p, is freed, code:0x%x", pVnode->vgId, pMsg, code);
1,761!
216
  rpcFreeCont(pMsg->pCont);
1,761✔
217
  taosFreeQitem(pMsg);
1,761✔
218
}
1,761✔
219

220
static void vmProcessFetchQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
21,008,936✔
221
  SVnodeObj *pVnode = pInfo->ahandle;
21,008,936✔
222
  SRpcMsg   *pMsg = NULL;
21,008,936✔
223

224
  for (int32_t i = 0; i < numOfMsgs; ++i) {
42,643,303✔
225
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
21,618,328!
226
    const STraceId *trace = &pMsg->info.traceId;
21,623,227✔
227
    dGTrace("vgId:%d, msg:%p, get from vnode-fetch queue", pVnode->vgId, pMsg);
21,623,227!
228

229
    terrno = 0;
21,623,228✔
230
    int32_t code = vnodeProcessFetchMsg(pVnode->pImpl, pMsg, pInfo);
21,612,899✔
231
    if (code != 0) {
21,619,610✔
232
      if (code == -1 && terrno != 0) {
2,811!
233
        code = terrno;
×
234
      }
235

236
      if (code == TSDB_CODE_WAL_LOG_NOT_EXIST) {
2,811!
237
        dGDebug("vgId:%d, msg:%p, failed to fetch since %s [vnodeProcessFetchMsg]", pVnode->vgId, pMsg, terrstr());
×
238
      } else {
239
        dGError("vgId:%d, msg:%p, failed to fetch since %s [vnodeProcessFetchMsg]", pVnode->vgId, pMsg, terrstr());
2,811!
240
      }
241

242
      vmSendRsp(pMsg, code);
2,811✔
243
    }
244

245
    dGTrace("vgId:%d, msg:%p, is freed, code:0x%x [vnodeProcessFetchMsg]", pVnode->vgId, pMsg, code);
21,619,610!
246
    rpcFreeCont(pMsg->pCont);
21,619,610✔
247
    taosFreeQitem(pMsg);
21,630,118✔
248
  }
249
}
21,024,975✔
250

251
static void vmProcessSyncQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
2,594,588✔
252
  SVnodeObj *pVnode = pInfo->ahandle;
2,594,588✔
253
  SRpcMsg   *pMsg = NULL;
2,594,588✔
254

255
  for (int32_t i = 0; i < numOfMsgs; ++i) {
9,067,164✔
256
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
6,472,308!
257
    const STraceId *trace = &pMsg->info.traceId;
6,472,615✔
258
    dGTrace("vgId:%d, msg:%p, get from vnode-sync queue", pVnode->vgId, pMsg);
6,472,615!
259

260
    int32_t code = vnodeProcessSyncMsg(pVnode->pImpl, pMsg, NULL);  // no response here
6,472,615✔
261
    dGTrace("vgId:%d, msg:%p, is freed, code:0x%x", pVnode->vgId, pMsg, code);
6,472,330!
262
    rpcFreeCont(pMsg->pCont);
6,472,329✔
263
    taosFreeQitem(pMsg);
6,472,530✔
264
  }
265
}
2,594,856✔
266

267
static void vmSendResponse(SRpcMsg *pMsg) {
×
268
  if (pMsg->info.handle) {
×
269
    SRpcMsg rsp = {.info = pMsg->info, .code = terrno};
×
270
    if (rpcSendResponse(&rsp) != 0) {
×
271
      dError("failed to send response since %s", terrstr());
×
272
    }
273
  }
274
}
×
275

276
static bool vmDataSpaceSufficient(SVnodeObj *pVnode) {
10,949,120✔
277
  STfs *pTfs = pVnode->pImpl->pTfs;
10,949,120✔
278
  if (pTfs) {
10,949,120!
279
    return tfsDiskSpaceSufficient(pTfs, 0, pVnode->diskPrimary);
10,953,604✔
280
  } else {
281
    return osDataSpaceSufficient();
×
282
  }
283
}
284

285
static int32_t vmAcquireVnodeWrapper(SVnodeMgmt *pMgt, int32_t vgId, SVnodeObj **pNode) {
51,261,440✔
286
  *pNode = vmAcquireVnode(pMgt, vgId);
51,261,440✔
287
  if (*pNode == NULL) {
51,352,452✔
288
    return terrno;
23,050✔
289
  }
290
  return 0;
51,329,402✔
291
}
292
static int32_t vmPutMsgToQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg, EQueueType qtype) {
51,268,966✔
293
  int32_t         code = 0;
51,268,966✔
294
  const STraceId *trace = &pMsg->info.traceId;
51,268,966✔
295
  if (pMsg->contLen < sizeof(SMsgHead)) {
51,268,966!
296
    dGError("invalid rpc msg with no msg head at pCont. pMsg:%p, type:%s, contLen:%d", pMsg, TMSG_INFO(pMsg->msgType),
×
297
            pMsg->contLen);
298
    return TSDB_CODE_INVALID_MSG;
×
299
  }
300

301
  SMsgHead *pHead = pMsg->pCont;
51,268,966✔
302

303
  pHead->contLen = ntohl(pHead->contLen);
51,268,966✔
304
  pHead->vgId = ntohl(pHead->vgId);
51,268,966✔
305

306
  SVnodeObj *pVnode = NULL;
51,268,966✔
307
  code = vmAcquireVnodeWrapper(pMgmt, pHead->vgId, &pVnode);
51,268,966✔
308
  if (code != 0) {
51,320,257✔
309
    dGDebug("vgId:%d, msg:%p, failed to put into vnode queue since %s, type:%s qtype:%d contLen:%d", pHead->vgId, pMsg,
23,844!
310
            tstrerror(code), TMSG_INFO(pMsg->msgType), qtype, pHead->contLen);
311
    return code;
23,844✔
312
  }
313

314
  switch (qtype) {
51,296,413✔
315
    case QUERY_QUEUE:
8,291,650✔
316
      code = vnodePreprocessQueryMsg(pVnode->pImpl, pMsg);
8,291,650✔
317
      if (code) {
8,291,032✔
318
        dError("vgId:%d, msg:%p, preprocess query msg failed since %s", pVnode->vgId, pMsg, tstrerror(code));
21!
319
      } else {
320
        dGTrace("vgId:%d, msg:%p, put into vnode-query queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
8,291,011!
321
        code = taosWriteQitem(pVnode->pQueryQ, pMsg);
8,291,011✔
322
      }
323
      break;
8,295,002✔
324
    case STREAM_QUEUE:
263,810✔
325
      dGTrace("vgId:%d, msg:%p, put into vnode-stream queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
263,810!
326
      code = taosWriteQitem(pVnode->pStreamQ, pMsg);
263,810✔
327
      break;
263,826✔
328
    case STREAM_CTRL_QUEUE:
117,052✔
329
      dGTrace("vgId:%d, msg:%p, put into vnode-stream-ctrl queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
117,052!
330
      code = taosWriteQitem(pVnode->pStreamCtrlQ, pMsg);
117,052✔
331
      break;
117,406✔
332
    case STREAM_LONG_EXEC_QUEUE:
1,760✔
333
      dGTrace("vgId:%d, msg:%p, put into vnode-stream-long-exec queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
1,760!
334
      code = taosWriteQitem(pVnode->pStreamLongExecQ, pMsg);
1,760✔
335
      break;
1,760✔
336
    case STREAM_CHKPT_QUEUE:
7,966✔
337
      dGTrace("vgId:%d, msg:%p, put into vnode-stream-chkpt queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
7,966!
338
      code = taosWriteQitem(pVnode->pStreamChkQ, pMsg);
7,966✔
339
      break;
7,973✔
340
    case FETCH_QUEUE:
21,599,870✔
341
      dGTrace("vgId:%d, msg:%p, put into vnode-fetch queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
21,599,870!
342
      code = taosWriteQitem(pVnode->pFetchQ, pMsg);
21,599,870✔
343
      break;
21,625,210✔
344
    case WRITE_QUEUE:
10,951,450✔
345
      if (!vmDataSpaceSufficient(pVnode)) {
10,951,450!
346
        code = TSDB_CODE_NO_ENOUGH_DISKSPACE;
×
347
        dError("vgId:%d, msg:%p, failed to put into vnode-write queue since %s, type:%s", pVnode->vgId, pMsg,
×
348
               tstrerror(code), TMSG_INFO(pMsg->msgType));
349
        break;
×
350
      }
351
#if 0
352
      if (pMsg->msgType == TDMT_VND_SUBMIT && (grantCheck(TSDB_GRANT_STORAGE) != TSDB_CODE_SUCCESS)) {
353
        code = TSDB_CODE_VND_NO_WRITE_AUTH;
354
        dDebug("vgId:%d, msg:%p, failed to put into vnode-write queue since %s, type:%s", pVnode->vgId, pMsg,
355
               tstrerror(code), TMSG_INFO(pMsg->msgType));
356
        break;
357
      }
358
#endif
359
      if (pMsg->msgType != TDMT_VND_ALTER_CONFIRM && pVnode->disable) {
10,958,724✔
360
        dDebug("vgId:%d, msg:%p, failed to put into vnode-write queue since its disable, type:%s", pVnode->vgId, pMsg,
661!
361
               TMSG_INFO(pMsg->msgType));
362
        code = TSDB_CODE_VND_STOPPED;
661✔
363
        break;
661✔
364
      }
365
      dGDebug("vgId:%d, msg:%p, put into vnode-write queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
10,958,063!
366
      code = taosWriteQitem(pVnode->pWriteW.queue, pMsg);
10,958,071✔
367
      break;
10,969,491✔
368
    case SYNC_QUEUE:
6,307,844✔
369
      dGDebug("vgId:%d, msg:%p, put into vnode-sync queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
6,307,844!
370
      code = taosWriteQitem(pVnode->pSyncW.queue, pMsg);
6,307,862✔
371
      break;
6,314,511✔
372
    case SYNC_RD_QUEUE:
157,123✔
373
      dGDebug("vgId:%d, msg:%p, put into vnode-sync-rd queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
157,123!
374
      code = taosWriteQitem(pVnode->pSyncRdW.queue, pMsg);
157,122✔
375
      break;
157,251✔
376
    case APPLY_QUEUE:
3,575,247✔
377
      dGDebug("vgId:%d, msg:%p, put into vnode-apply queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
3,575,247!
378
      code = taosWriteQitem(pVnode->pApplyW.queue, pMsg);
3,575,250✔
379
      break;
3,575,245✔
380
    default:
22,641✔
381
      code = TSDB_CODE_INVALID_MSG;
22,641✔
382
      break;
22,641✔
383
  }
384

385
  vmReleaseVnode(pMgmt, pVnode);
51,350,977✔
386
  return code;
51,340,625✔
387
}
388

389
int32_t vmPutMsgToSyncRdQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, SYNC_RD_QUEUE); }
157,564✔
390

391
int32_t vmPutMsgToSyncQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, SYNC_QUEUE); }
5,933,557✔
392

393
int32_t vmPutMsgToWriteQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, WRITE_QUEUE); }
10,928,320✔
394

395
int32_t vmPutMsgToQueryQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, QUERY_QUEUE); }
7,897,684✔
396

397
int32_t vmPutMsgToFetchQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, FETCH_QUEUE); }
21,606,337✔
398

399
int32_t vmPutMsgToStreamQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, STREAM_QUEUE); }
959✔
400

401
int32_t vmPutMsgToStreamCtrlQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, STREAM_CTRL_QUEUE); }
117,108✔
402

UNCOV
403
int32_t vmPutMsgToStreamLongExecQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, STREAM_LONG_EXEC_QUEUE); }
×
404

405
int32_t vmPutMsgToStreamChkQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, STREAM_CHKPT_QUEUE); }
×
406

407
int32_t vmPutMsgToMultiMgmtQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
12,383✔
408
  const STraceId *trace = &pMsg->info.traceId;
12,383✔
409
  dGTrace("msg:%p, put into vnode-multi-mgmt queue", pMsg);
12,383!
410
  return taosWriteQitem(pMgmt->mgmtMultiWorker.queue, pMsg);
12,383✔
411
}
412

413
int32_t vmPutMsgToMgmtQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
13,504✔
414
  const STraceId *trace = &pMsg->info.traceId;
13,504✔
415
  dGTrace("msg:%p, put into vnode-mgmt queue", pMsg);
13,504!
416
  return taosWriteQitem(pMgmt->mgmtWorker.queue, pMsg);
13,504✔
417
}
418

419
int32_t vmPutRpcMsgToQueue(SVnodeMgmt *pMgmt, EQueueType qtype, SRpcMsg *pRpc) {
4,652,552✔
420
  int32_t code;
421
  if (pRpc->contLen < sizeof(SMsgHead)) {
4,652,552!
UNCOV
422
    dError("invalid rpc msg with no msg head at pCont. pRpc:%p, type:%s, len:%d", pRpc, TMSG_INFO(pRpc->msgType),
×
423
           pRpc->contLen);
424
    rpcFreeCont(pRpc->pCont);
×
UNCOV
425
    pRpc->pCont = NULL;
×
426
    return TSDB_CODE_INVALID_MSG;
×
427
  }
428

429
  EQItype  itype = APPLY_QUEUE == qtype ? APPLY_QITEM : RPC_QITEM;
4,652,552✔
430
  SRpcMsg *pMsg;
431
  code = taosAllocateQitem(sizeof(SRpcMsg), itype, pRpc->contLen, (void **)&pMsg);
4,652,552✔
432
  if (code) {
4,652,568!
UNCOV
433
    rpcFreeCont(pRpc->pCont);
×
UNCOV
434
    pRpc->pCont = NULL;
×
435
    return code;
×
436
  }
437

438
  SMsgHead *pHead = pRpc->pCont;
4,652,568✔
439
  dTrace("vgId:%d, msg:%p, is created, type:%s len:%d", pHead->vgId, pMsg, TMSG_INFO(pRpc->msgType), pRpc->contLen);
4,652,568!
440

441
  pHead->contLen = htonl(pHead->contLen);
4,652,568✔
442
  pHead->vgId = htonl(pHead->vgId);
4,652,568✔
443
  memcpy(pMsg, pRpc, sizeof(SRpcMsg));
4,652,568✔
444
  pRpc->pCont = NULL;
4,652,568✔
445

446
  code = vmPutMsgToQueue(pMgmt, pMsg, qtype);
4,652,568✔
447
  if (code != 0) {
4,652,600✔
448
    dTrace("msg:%p, is freed", pMsg);
5,822✔
449
    rpcFreeCont(pMsg->pCont);
5,822✔
450
    taosFreeQitem(pMsg);
5,820✔
451
  }
452

453
  return code;
4,652,593✔
454
}
455

456
int32_t vmGetQueueSize(SVnodeMgmt *pMgmt, int32_t vgId, EQueueType qtype) {
2,867,981✔
457
  int32_t    size = -1;
2,867,981✔
458
  SVnodeObj *pVnode = vmAcquireVnode(pMgmt, vgId);
2,867,981✔
459
  if (pVnode != NULL) {
2,868,042✔
460
    switch (qtype) {
2,836,488!
461
      case WRITE_QUEUE:
20,237✔
462
        size = taosQueueItemSize(pVnode->pWriteW.queue);
20,237✔
463
        break;
20,220✔
UNCOV
464
      case SYNC_QUEUE:
×
UNCOV
465
        size = taosQueueItemSize(pVnode->pSyncW.queue);
×
466
        break;
×
467
      case APPLY_QUEUE:
64,705✔
468
        size = taosQueueItemSize(pVnode->pApplyW.queue);
64,705✔
469
        break;
64,710✔
UNCOV
470
      case QUERY_QUEUE:
×
UNCOV
471
        size = taosQueueItemSize(pVnode->pQueryQ);
×
472
        break;
×
473
      case FETCH_QUEUE:
×
474
        size = taosQueueItemSize(pVnode->pFetchQ);
×
475
        break;
×
476
      case STREAM_QUEUE:
2,751,529✔
477
        size = taosQueueItemSize(pVnode->pStreamQ);
2,751,529✔
478
        break;
2,751,543✔
UNCOV
479
      case STREAM_CTRL_QUEUE:
×
UNCOV
480
        size = taosQueueItemSize(pVnode->pStreamCtrlQ);
×
481
        break;
×
482
      case STREAM_LONG_EXEC_QUEUE:
×
483
        size = taosQueueItemSize(pVnode->pStreamLongExecQ);
×
484
        break;
×
485
      case STREAM_CHKPT_QUEUE:
×
486
        size = taosQueueItemSize(pVnode->pStreamChkQ);
×
487
      default:
×
488
        break;
×
489
    }
490
  }
491
  if (pVnode) vmReleaseVnode(pMgmt, pVnode);
2,868,027✔
492
  if (size < 0) {
2,868,035✔
493
    dTrace("vgId:%d, can't get size from queue since %s, qtype:%d", vgId, terrstr(), qtype);
31,548✔
494
    size = 0;
31,546✔
495
  }
496
  return size;
2,868,033✔
497
}
498

499
int32_t vmAllocQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
15,641✔
500
  int32_t         code = 0;
15,641✔
501
  SMultiWorkerCfg wcfg = {.max = 1, .name = "vnode-write", .fp = (FItems)vnodeProposeWriteMsg, .param = pVnode->pImpl};
15,641✔
502
  SMultiWorkerCfg scfg = {.max = 1, .name = "vnode-sync", .fp = (FItems)vmProcessSyncQueue, .param = pVnode};
15,641✔
503
  SMultiWorkerCfg sccfg = {.max = 1, .name = "vnode-sync-rd", .fp = (FItems)vmProcessSyncQueue, .param = pVnode};
15,641✔
504
  SMultiWorkerCfg acfg = {.max = 1, .name = "vnode-apply", .fp = (FItems)vnodeApplyWriteMsg, .param = pVnode->pImpl};
15,641✔
505
  code = tMultiWorkerInit(&pVnode->pWriteW, &wcfg);
15,641✔
506
  if (code) {
15,641!
UNCOV
507
    return code;
×
508
  }
509
  code = tMultiWorkerInit(&pVnode->pSyncW, &scfg);
15,641✔
510
  if (code) {
15,641!
UNCOV
511
    tMultiWorkerCleanup(&pVnode->pWriteW);
×
UNCOV
512
    return code;
×
513
  }
514
  code = tMultiWorkerInit(&pVnode->pSyncRdW, &sccfg);
15,641✔
515
  if (code) {
15,641!
UNCOV
516
    tMultiWorkerCleanup(&pVnode->pWriteW);
×
UNCOV
517
    tMultiWorkerCleanup(&pVnode->pSyncW);
×
518
    return code;
×
519
  }
520
  code = tMultiWorkerInit(&pVnode->pApplyW, &acfg);
15,641✔
521
  if (code) {
15,641!
UNCOV
522
    tMultiWorkerCleanup(&pVnode->pWriteW);
×
UNCOV
523
    tMultiWorkerCleanup(&pVnode->pSyncW);
×
524
    tMultiWorkerCleanup(&pVnode->pSyncRdW);
×
525
    return code;
×
526
  }
527

528
  pVnode->pQueryQ = tQueryAutoQWorkerAllocQueue(&pMgmt->queryPool, pVnode, (FItem)vmProcessQueryQueue);
15,641✔
529
  pVnode->pFetchQ = tWWorkerAllocQueue(&pMgmt->fetchPool, pVnode, (FItems)vmProcessFetchQueue);
15,641✔
530

531
  // init stream msg processing queue family
532
  pVnode->pStreamQ = tAutoQWorkerAllocQueue(&pMgmt->streamPool, pVnode, (FItem)vmProcessStreamQueue, 2);
15,641✔
533
  pVnode->pStreamCtrlQ = tWWorkerAllocQueue(&pMgmt->streamCtrlPool, pVnode, (FItems)vmProcessStreamCtrlQueue);
15,641✔
534
  pVnode->pStreamLongExecQ = tAutoQWorkerAllocQueue(&pMgmt->streamLongExecPool, pVnode, (FItem)vmProcessStreamLongExecQueue, 1);
15,641✔
535
  pVnode->pStreamChkQ = tWWorkerAllocQueue(&pMgmt->streamChkPool, pVnode, (FItems)vmProcessStreamChkptQueue);
15,641✔
536

537
  if (pVnode->pWriteW.queue == NULL || pVnode->pSyncW.queue == NULL || pVnode->pSyncRdW.queue == NULL ||
15,641!
538
      pVnode->pApplyW.queue == NULL || pVnode->pQueryQ == NULL || pVnode->pStreamQ == NULL || pVnode->pFetchQ == NULL
15,641!
539
      || pVnode->pStreamCtrlQ == NULL || pVnode->pStreamLongExecQ == NULL || pVnode->pStreamChkQ == NULL) {
15,641!
UNCOV
540
    return TSDB_CODE_OUT_OF_MEMORY;
×
541
  }
542

543
  dInfo("vgId:%d, write-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pWriteW.queue,
15,641!
544
        taosQueueGetThreadId(pVnode->pWriteW.queue));
545
  dInfo("vgId:%d, sync-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pSyncW.queue,
15,641!
546
        taosQueueGetThreadId(pVnode->pSyncW.queue));
547
  dInfo("vgId:%d, sync-rd-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pSyncRdW.queue,
15,641!
548
        taosQueueGetThreadId(pVnode->pSyncRdW.queue));
549
  dInfo("vgId:%d, apply-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pApplyW.queue,
15,641!
550
        taosQueueGetThreadId(pVnode->pApplyW.queue));
551
  dInfo("vgId:%d, query-queue:%p is alloced", pVnode->vgId, pVnode->pQueryQ);
15,641!
552
  dInfo("vgId:%d, fetch-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pFetchQ,
15,641!
553
        taosQueueGetThreadId(pVnode->pFetchQ));
554
  dInfo("vgId:%d, stream-queue:%p is alloced", pVnode->vgId, pVnode->pStreamQ);
15,641!
555
  dInfo("vgId:%d, stream-long-exec-queue:%p is alloced", pVnode->vgId, pVnode->pStreamLongExecQ);
15,641!
556
  dInfo("vgId:%d, stream-ctrl-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pStreamCtrlQ,
15,641!
557
        taosQueueGetThreadId(pVnode->pStreamCtrlQ));
558
  dInfo("vgId:%d, stream-chk-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pStreamChkQ,
15,641!
559
        taosQueueGetThreadId(pVnode->pStreamChkQ));
560
  return 0;
15,641✔
561
}
562

563
void vmFreeQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
15,640✔
564
  tQueryAutoQWorkerFreeQueue(&pMgmt->queryPool, pVnode->pQueryQ);
15,640✔
565
  tAutoQWorkerFreeQueue(&pMgmt->streamPool, pVnode->pStreamQ);
15,641✔
566
  tAutoQWorkerFreeQueue(&pMgmt->streamLongExecPool, pVnode->pStreamLongExecQ);
15,641✔
567
  tWWorkerFreeQueue(&pMgmt->streamCtrlPool, pVnode->pStreamCtrlQ);
15,640✔
568
  tWWorkerFreeQueue(&pMgmt->streamChkPool, pVnode->pStreamChkQ);
15,640✔
569
  tWWorkerFreeQueue(&pMgmt->fetchPool, pVnode->pFetchQ);
15,641✔
570
  pVnode->pQueryQ = NULL;
15,640✔
571
  pVnode->pFetchQ = NULL;
15,640✔
572

573
  pVnode->pStreamQ = NULL;
15,640✔
574
  pVnode->pStreamCtrlQ = NULL;
15,640✔
575
  pVnode->pStreamLongExecQ = NULL;
15,640✔
576

577
  pVnode->pStreamChkQ = NULL;
15,640✔
578
  pVnode->pFetchQ = NULL;
15,640✔
579
  dDebug("vgId:%d, queue is freed", pVnode->vgId);
15,640✔
580
}
15,640✔
581

582
int32_t vmStartWorker(SVnodeMgmt *pMgmt) {
3,064✔
583
  int32_t code = 0;
3,064✔
584

585
  SQueryAutoQWorkerPool *pQPool = &pMgmt->queryPool;
3,064✔
586
  pQPool->name = "vnode-query";
3,064✔
587
  pQPool->min = tsNumOfVnodeQueryThreads;
3,064✔
588
  pQPool->max = tsNumOfVnodeQueryThreads;
3,064✔
589
  if ((code = tQueryAutoQWorkerInit(pQPool)) != 0) return code;
3,064!
590

591
  tsNumOfQueryThreads += tsNumOfVnodeQueryThreads;
3,064✔
592

593
  SAutoQWorkerPool *pStreamPool = &pMgmt->streamPool;
3,064✔
594
  pStreamPool->name = "vnode-stream";
3,064✔
595
  pStreamPool->ratio = tsRatioOfVnodeStreamThreads;
3,064✔
596
  if ((code = tAutoQWorkerInit(pStreamPool)) != 0) return code;
3,064!
597

598
  SAutoQWorkerPool *pLongExecPool = &pMgmt->streamLongExecPool;
3,064✔
599
  pLongExecPool->name = "vnode-stream-long-exec";
3,064✔
600
  pLongExecPool->ratio = tsRatioOfVnodeStreamThreads/3;
3,064✔
601
  if ((code = tAutoQWorkerInit(pLongExecPool)) != 0) return code;
3,064!
602

603
  SWWorkerPool *pStreamCtrlPool = &pMgmt->streamCtrlPool;
3,064✔
604
  pStreamCtrlPool->name = "vnode-stream-ctrl";
3,064✔
605
  pStreamCtrlPool->max = 4;
3,064✔
606
  if ((code = tWWorkerInit(pStreamCtrlPool)) != 0) return code;
3,064!
607

608
  SWWorkerPool *pStreamChkPool = &pMgmt->streamChkPool;
3,064✔
609
  pStreamChkPool->name = "vnode-stream-chkpt";
3,064✔
610
  pStreamChkPool->max = 1;
3,064✔
611
  if ((code = tWWorkerInit(pStreamChkPool)) != 0) return code;
3,064!
612

613
  SWWorkerPool *pFPool = &pMgmt->fetchPool;
3,064✔
614
  pFPool->name = "vnode-fetch";
3,064✔
615
  pFPool->max = tsNumOfVnodeFetchThreads;
3,064✔
616
  if ((code = tWWorkerInit(pFPool)) != 0) return code;
3,064!
617

618
  SSingleWorkerCfg mgmtCfg = {
3,064✔
619
      .min = 1, .max = 1, .name = "vnode-mgmt", .fp = (FItem)vmProcessMgmtQueue, .param = pMgmt};
620

621
  if ((code = tSingleWorkerInit(&pMgmt->mgmtWorker, &mgmtCfg)) != 0) return code;
3,064!
622

623
  int32_t threadNum = 0;
3,064✔
624
  if (tsNumOfCores == 1) {
3,064!
UNCOV
625
    threadNum = 2;
×
626
  } else {
627
    threadNum = tsNumOfCores;
3,064✔
628
  }
629
  SSingleWorkerCfg multiMgmtCfg = {.min = threadNum,
3,064✔
630
                                   .max = threadNum,
631
                                   .name = "vnode-multi-mgmt",
632
                                   .fp = (FItem)vmProcessMultiMgmtQueue,
633
                                   .param = pMgmt};
634

635
  if ((code = tSingleWorkerInit(&pMgmt->mgmtMultiWorker, &multiMgmtCfg)) != 0) return code;
3,064!
636

637
  dDebug("vnode workers are initialized");
3,064✔
638
  return 0;
3,064✔
639
}
640

641
void vmStopWorker(SVnodeMgmt *pMgmt) {
3,064✔
642
  tQueryAutoQWorkerCleanup(&pMgmt->queryPool);
3,064✔
643
  tAutoQWorkerCleanup(&pMgmt->streamPool);
3,064✔
644
  tAutoQWorkerCleanup(&pMgmt->streamLongExecPool);
3,064✔
645
  tWWorkerCleanup(&pMgmt->streamCtrlPool);
3,064✔
646
  tWWorkerCleanup(&pMgmt->streamChkPool);
3,064✔
647
  tWWorkerCleanup(&pMgmt->fetchPool);
3,064✔
648
  dDebug("vnode workers are closed");
3,064✔
649
}
3,064✔
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