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

taosdata / TDengine / #4506

15 Jul 2025 12:33AM UTC coverage: 62.026% (-0.7%) from 62.706%
#4506

push

travis-ci

web-flow
docs: update stream docs (#31874)

155391 of 320094 branches covered (48.55%)

Branch coverage included in aggregate %.

240721 of 318525 relevant lines covered (75.57%)

6529048.03 hits per line

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

68.3
/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) {
25,902✔
21
  if (pMsg->info.handle == NULL) return;
25,902✔
22
  SRpcMsg rsp = {
25,749✔
23
      .code = code,
24
      .pCont = pMsg->info.rsp,
25,749✔
25
      .contLen = pMsg->info.rspLen,
25,749✔
26
      .info = pMsg->info,
27
  };
28
  tmsgSendRsp(&rsp);
25,749✔
29
}
30

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

36
  dGTrace("msg:%p, get from vnode-multi-mgmt queue", pMsg);
11,185!
37
  switch (pMsg->msgType) {
11,185!
38
    case TDMT_DND_CREATE_VNODE:
11,388✔
39
      code = vmProcessCreateVnodeReq(pMgmt, pMsg);
11,388✔
40
      break;
11,391✔
41
#ifdef USE_MOUNT
42
    case TDMT_DND_RETRIEVE_MOUNT_PATH:
×
43
      code = vmProcessRetrieveMountPathReq(pMgmt, pMsg);
×
44
      break;
×
45
    case TDMT_DND_MOUNT_VNODE:
×
46
      code = vmProcessMountVnodeReq(pMgmt, pMsg);
×
47
      break;
×
48
#endif
49
  }
50

51
  if (IsReq(pMsg)) {
11,188!
52
    if (code != 0) {
11,391!
53
      if (terrno != 0) code = terrno;
×
54
      dGError("msg:%p, failed to process since %s, type:%s", pMsg, tstrerror(code), TMSG_INFO(pMsg->msgType));
×
55
    }
56
    vmSendRsp(pMsg, code);
11,391✔
57
  }
58

59
  dGTrace("msg:%p, is freed, code:0x%x", pMsg, code);
11,188!
60
  rpcFreeCont(pMsg->pCont);
11,188✔
61
  taosFreeQitem(pMsg);
11,391✔
62
}
11,391✔
63

64
static void vmProcessMgmtQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
11,513✔
65
  SVnodeMgmt     *pMgmt = pInfo->ahandle;
11,513✔
66
  int32_t         code = -1;
11,513✔
67
  const STraceId *trace = &pMsg->info.traceId;
11,513✔
68

69
  dGTrace("msg:%p, get from vnode-mgmt queue", pMsg);
11,513!
70
  switch (pMsg->msgType) {
11,513!
71
    case TDMT_DND_CREATE_VNODE:
×
72
      code = vmProcessCreateVnodeReq(pMgmt, pMsg);
×
73
      break;
×
74
    case TDMT_DND_DROP_VNODE:
4,613✔
75
      code = vmProcessDropVnodeReq(pMgmt, pMsg);
4,613✔
76
      break;
4,613✔
77
    case TDMT_VND_ALTER_REPLICA:
1,806✔
78
      code = vmProcessAlterVnodeReplicaReq(pMgmt, pMsg);
1,806✔
79
      break;
1,806✔
80
    case TDMT_VND_DISABLE_WRITE:
34✔
81
      code = vmProcessDisableVnodeWriteReq(pMgmt, pMsg);
34✔
82
      break;
34✔
83
    case TDMT_VND_ALTER_HASHRANGE:
32✔
84
      code = vmProcessAlterHashRangeReq(pMgmt, pMsg);
32✔
85
      break;
32✔
86
    case TDMT_DND_ALTER_VNODE_TYPE:
4,944✔
87
      code = vmProcessAlterVnodeTypeReq(pMgmt, pMsg);
4,944✔
88
      break;
4,944✔
89
    case TDMT_DND_CHECK_VNODE_LEARNER_CATCHUP:
×
90
      code = vmProcessCheckLearnCatchupReq(pMgmt, pMsg);
×
91
      break;
×
92
    case TDMT_VND_ARB_HEARTBEAT:
84✔
93
      code = vmProcessArbHeartBeatReq(pMgmt, pMsg);
84✔
94
      break;
84✔
95
    default:
×
96
      terrno = TSDB_CODE_MSG_NOT_PROCESSED;
×
97
      dGError("msg:%p, not processed in vnode-mgmt queue", pMsg);
×
98
  }
99

100
  if (IsReq(pMsg)) {
11,513!
101
    if (code != 0) {
11,513✔
102
      if (terrno != 0) code = terrno;
4,698!
103
      dGError("msg:%p, failed to process since %s, type:%s", pMsg, tstrerror(code), TMSG_INFO(pMsg->msgType));
4,698!
104
    }
105
    vmSendRsp(pMsg, code);
11,513✔
106
  }
107

108
  dGTrace("msg:%p, is freed, code:0x%x", pMsg, code);
11,513!
109
  rpcFreeCont(pMsg->pCont);
11,513✔
110
  taosFreeQitem(pMsg);
11,513✔
111
}
11,513✔
112

113
static void vmProcessQueryQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
1,751,026✔
114
  SVnodeObj      *pVnode = pInfo->ahandle;
1,751,026✔
115
  const STraceId *trace = &pMsg->info.traceId;
1,751,026✔
116

117
  dGTrace("vgId:%d, msg:%p, get from vnode-query queue", pVnode->vgId, pMsg);
1,751,026!
118
  int32_t code = vnodeProcessQueryMsg(pVnode->pImpl, pMsg, pInfo);
1,751,026✔
119
  if (code != 0) {
1,749,478✔
120
    if (terrno != 0) code = terrno;
1,266!
121
    dGError("vgId:%d, msg:%p, failed to query since %s", pVnode->vgId, pMsg, tstrerror(code));
1,265!
122
    vmSendRsp(pMsg, code);
1,266✔
123
  }
124

125
  dGTrace("vgId:%d, msg:%p, is freed, code:0x%x", pVnode->vgId, pMsg, code);
1,749,478!
126
  rpcFreeCont(pMsg->pCont);
1,749,478✔
127
  taosFreeQitem(pMsg);
1,750,856✔
128
}
1,750,966✔
129

130
static void vmProcessStreamQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
104,640✔
131
#ifdef USE_STREAM
132
  SVnodeObj      *pVnode = pInfo->ahandle;
104,640✔
133
  const STraceId *trace = &pMsg->info.traceId;
104,640✔
134

135
  dGTrace("vgId:%d, msg:%p, get from vnode-stream queue", pVnode->vgId, pMsg);
104,640!
136
  int32_t code = vnodeProcessStreamMsg(pVnode->pImpl, pMsg, pInfo);
104,640✔
137
  if (code != 0) {
104,640!
138
    terrno = code;
×
139
    dGError("vgId:%d, msg:%p, failed to process stream msg %s since %s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType),
×
140
            tstrerror(code));
141
    vmSendRsp(pMsg, code);
×
142
  }
143

144
  dGTrace("vgId:%d, msg:%p, is freed, code:0x%x", pVnode->vgId, pMsg, code);
104,640!
145
  rpcFreeCont(pMsg->pCont);
104,640✔
146
  taosFreeQitem(pMsg);
104,640✔
147
#endif
148
}
104,640✔
149

150
static void vmProcessStreamCtrlQueue(SQueueInfo *pInfo, STaosQall* pQall, int32_t numOfItems) {
50,711✔
151
#ifdef USE_STREAM
152
  SVnodeObj *pVnode = pInfo->ahandle;
50,711✔
153
  void      *pItem = NULL;
50,711✔
154
  int32_t    code = 0;
50,711✔
155

156
  while (1) {
57,364✔
157
    if (taosGetQitem(pQall, &pItem) == 0) {
108,075✔
158
      break;
50,716✔
159
    }
160

161
    SRpcMsg        *pMsg = pItem;
57,359✔
162
    const STraceId *trace = &pMsg->info.traceId;
57,359✔
163

164
    dGTrace("vgId:%d, msg:%p, get from vnode-stream-ctrl queue", pVnode->vgId, pMsg);
57,359!
165
    code = vnodeProcessStreamCtrlMsg(pVnode->pImpl, pMsg, pInfo);
57,359✔
166
    if (code != 0) {
57,365✔
167
      terrno = code;
139✔
168
      dGError("vgId:%d, msg:%p, failed to process stream ctrl msg %s since %s", pVnode->vgId, pMsg,
139!
169
              TMSG_INFO(pMsg->msgType), tstrerror(code));
170
      vmSendRsp(pMsg, code);
139✔
171
    }
172

173
    dGTrace("vgId:%d, msg:%p, is freed, code:0x%x", pVnode->vgId, pMsg, code);
57,365!
174
    rpcFreeCont(pMsg->pCont);
57,366✔
175
    taosFreeQitem(pMsg);
57,366✔
176
  }
177
#endif
178
}
50,716✔
179

180
static void vmProcessStreamChkptQueue(SQueueInfo *pInfo, STaosQall* pQall, int32_t numOfItems) {
2,919✔
181
  SVnodeObj *pVnode = pInfo->ahandle;
2,919✔
182
  void      *pItem = NULL;
2,919✔
183
  int32_t    code = 0;
2,919✔
184

185
  while (1) {
3,208✔
186
    if (taosGetQitem(pQall, &pItem) == 0) {
6,127✔
187
      break;
2,919✔
188
    }
189

190
    SRpcMsg        *pMsg = pItem;
3,208✔
191
    const STraceId *trace = &pMsg->info.traceId;
3,208✔
192

193
    dGTrace("vgId:%d, msg:%p, get from vnode-stream-chkpt queue", pVnode->vgId, pMsg);
3,208!
194
    code = vnodeProcessStreamChkptMsg(pVnode->pImpl, pMsg, pInfo);
3,208✔
195
    if (code != 0) {
3,208✔
196
      terrno = code;
13✔
197
      dGError("vgId:%d, msg:%p, failed to process stream chkpt msg %s since %s", pVnode->vgId, pMsg,
13!
198
              TMSG_INFO(pMsg->msgType), tstrerror(code));
199
      vmSendRsp(pMsg, code);
13✔
200
    }
201

202
    dGTrace("vgId:%d, msg:%p, is freed, code:0x%x", pVnode->vgId, pMsg, code);
3,208!
203
    rpcFreeCont(pMsg->pCont);
3,208✔
204
    taosFreeQitem(pMsg);
3,208✔
205
  }
206
}
2,919✔
207

208
static void vmProcessStreamLongExecQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
1,029✔
209
  SVnodeObj      *pVnode = pInfo->ahandle;
1,029✔
210
  const STraceId *trace = &pMsg->info.traceId;
1,029✔
211
  int32_t         code = 0;
1,029✔
212

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

215
  code = vnodeProcessStreamLongExecMsg(pVnode->pImpl, pMsg, pInfo);
1,029✔
216
  if (code != 0) {
1,029✔
217
    terrno = code;
1✔
218
    dGError("vgId:%d, msg:%p, failed to process stream msg %s since %s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType),
1!
219
            tstrerror(code));
220
    vmSendRsp(pMsg, code);
1✔
221
  }
222

223
  dGTrace("vgId:%d, msg:%p, is freed, code:0x%x", pVnode->vgId, pMsg, code);
1,029!
224
  rpcFreeCont(pMsg->pCont);
1,029✔
225
  taosFreeQitem(pMsg);
1,029✔
226
}
1,029✔
227

228
static void vmProcessFetchQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
4,468,427✔
229
  SVnodeObj *pVnode = pInfo->ahandle;
4,468,427✔
230
  SRpcMsg   *pMsg = NULL;
4,468,427✔
231

232
  for (int32_t i = 0; i < numOfMsgs; ++i) {
9,021,966✔
233
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
4,550,595!
234
    const STraceId *trace = &pMsg->info.traceId;
4,553,029✔
235
    dGTrace("vgId:%d, msg:%p, get from vnode-fetch queue", pVnode->vgId, pMsg);
4,553,029!
236

237
    terrno = 0;
4,553,029✔
238
    int32_t code = vnodeProcessFetchMsg(pVnode->pImpl, pMsg, pInfo);
4,550,371✔
239
    if (code != 0) {
4,551,795✔
240
      if (code == -1 && terrno != 0) {
1,579!
241
        code = terrno;
×
242
      }
243

244
      if (code == TSDB_CODE_WAL_LOG_NOT_EXIST) {
1,579!
245
        dGDebug("vgId:%d, msg:%p, failed to fetch since %s [vnodeProcessFetchMsg]", pVnode->vgId, pMsg, terrstr());
×
246
      } else {
247
        dGError("vgId:%d, msg:%p, failed to fetch since %s [vnodeProcessFetchMsg]", pVnode->vgId, pMsg, terrstr());
1,579!
248
      }
249

250
      vmSendRsp(pMsg, code);
1,579✔
251
    }
252

253
    dGTrace("vgId:%d, msg:%p, is freed, code:0x%x [vnodeProcessFetchMsg]", pVnode->vgId, pMsg, code);
4,551,795!
254
    rpcFreeCont(pMsg->pCont);
4,551,795✔
255
    taosFreeQitem(pMsg);
4,553,469✔
256
  }
257
}
4,471,371✔
258

259
static void vmProcessSyncQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
1,069,918✔
260
  SVnodeObj *pVnode = pInfo->ahandle;
1,069,918✔
261
  SRpcMsg   *pMsg = NULL;
1,069,918✔
262

263
  for (int32_t i = 0; i < numOfMsgs; ++i) {
2,342,707✔
264
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
1,272,740!
265
    const STraceId *trace = &pMsg->info.traceId;
1,272,913✔
266
    dGTrace("vgId:%d, msg:%p, get from vnode-sync queue", pVnode->vgId, pMsg);
1,272,913!
267

268
    int32_t code = vnodeProcessSyncMsg(pVnode->pImpl, pMsg, NULL);  // no response here
1,272,913✔
269
    dGTrace("vgId:%d, msg:%p, is freed, code:0x%x", pVnode->vgId, pMsg, code);
1,272,781!
270
    rpcFreeCont(pMsg->pCont);
1,272,781✔
271
    taosFreeQitem(pMsg);
1,272,751✔
272
  }
273
}
1,069,967✔
274

275
static void vmSendResponse(SRpcMsg *pMsg) {
×
276
  if (pMsg->info.handle) {
×
277
    SRpcMsg rsp = {.info = pMsg->info, .code = terrno};
×
278
    if (rpcSendResponse(&rsp) != 0) {
×
279
      dError("failed to send response since %s", terrstr());
×
280
    }
281
  }
282
}
×
283

284
static bool vmDataSpaceSufficient(SVnodeObj *pVnode) {
2,514,375✔
285
  STfs *pTfs = pVnode->pImpl->pTfs;
2,514,375✔
286
  if (pTfs) {
2,514,375!
287
    return tfsDiskSpaceSufficient(pTfs, 0, pVnode->diskPrimary);
2,514,460✔
288
  } else {
289
    return osDataSpaceSufficient();
×
290
  }
291
}
292

293
static int32_t vmAcquireVnodeWrapper(SVnodeMgmt *pMgt, int32_t vgId, SVnodeObj **pNode) {
11,333,834✔
294
  *pNode = vmAcquireVnode(pMgt, vgId);
11,333,834✔
295
  if (*pNode == NULL) {
11,347,762✔
296
    return terrno;
10,594✔
297
  }
298
  return 0;
11,337,168✔
299
}
300
static int32_t vmPutMsgToQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg, EQueueType qtype) {
11,334,245✔
301
  int32_t         code = 0;
11,334,245✔
302
  const STraceId *trace = &pMsg->info.traceId;
11,334,245✔
303
  if (pMsg->contLen < sizeof(SMsgHead)) {
11,334,245!
304
    dGError("invalid rpc msg with no msg head at pCont. pMsg:%p, type:%s, contLen:%d", pMsg, TMSG_INFO(pMsg->msgType),
×
305
            pMsg->contLen);
306
    return TSDB_CODE_INVALID_MSG;
×
307
  }
308

309
  SMsgHead *pHead = pMsg->pCont;
11,334,245✔
310

311
  pHead->contLen = ntohl(pHead->contLen);
11,334,245✔
312
  pHead->vgId = ntohl(pHead->vgId);
11,334,245✔
313

314
  SVnodeObj *pVnode = NULL;
11,334,245✔
315
  code = vmAcquireVnodeWrapper(pMgmt, pHead->vgId, &pVnode);
11,334,245✔
316
  if (code != 0) {
11,347,538✔
317
    dGDebug("vgId:%d, msg:%p, failed to put into vnode queue since %s, type:%s qtype:%d contLen:%d", pHead->vgId, pMsg,
10,523!
318
            tstrerror(code), TMSG_INFO(pMsg->msgType), qtype, pHead->contLen);
319
    return code;
10,523✔
320
  }
321

322
  switch (qtype) {
11,337,015✔
323
    case QUERY_QUEUE:
1,749,165✔
324
      code = vnodePreprocessQueryMsg(pVnode->pImpl, pMsg);
1,749,165✔
325
      if (code) {
1,748,969✔
326
        dError("vgId:%d, msg:%p, preprocess query msg failed since %s", pVnode->vgId, pMsg, tstrerror(code));
17!
327
      } else {
328
        dGTrace("vgId:%d, msg:%p, put into vnode-query queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
1,748,952!
329
        code = taosWriteQitem(pVnode->pQueryQ, pMsg);
1,748,952✔
330
      }
331
      break;
1,750,211✔
332
    case STREAM_QUEUE:
104,640✔
333
      dGTrace("vgId:%d, msg:%p, put into vnode-stream queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
104,640!
334
      code = taosWriteQitem(pVnode->pStreamQ, pMsg);
104,640✔
335
      break;
104,639✔
336
    case STREAM_CTRL_QUEUE:
57,365✔
337
      dGTrace("vgId:%d, msg:%p, put into vnode-stream-ctrl queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
57,365!
338
      code = taosWriteQitem(pVnode->pStreamCtrlQ, pMsg);
57,365✔
339
      break;
57,366✔
340
    case STREAM_LONG_EXEC_QUEUE:
1,029✔
341
      dGTrace("vgId:%d, msg:%p, put into vnode-stream-long-exec queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
1,029!
342
      code = taosWriteQitem(pVnode->pStreamLongExecQ, pMsg);
1,029✔
343
      break;
1,029✔
344
    case STREAM_CHKPT_QUEUE:
3,208✔
345
      dGTrace("vgId:%d, msg:%p, put into vnode-stream-chkpt queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
3,208!
346
      code = taosWriteQitem(pVnode->pStreamChkQ, pMsg);
3,208✔
347
      break;
3,208✔
348
    case FETCH_QUEUE:
4,546,377✔
349
      dGTrace("vgId:%d, msg:%p, put into vnode-fetch queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
4,546,377!
350
      code = taosWriteQitem(pVnode->pFetchQ, pMsg);
4,546,377✔
351
      break;
4,551,291✔
352
    case WRITE_QUEUE:
2,514,392✔
353
      if (!vmDataSpaceSufficient(pVnode)) {
2,514,392!
354
        code = TSDB_CODE_NO_ENOUGH_DISKSPACE;
×
355
        dError("vgId:%d, msg:%p, failed to put into vnode-write queue since %s, type:%s", pVnode->vgId, pMsg,
×
356
               tstrerror(code), TMSG_INFO(pMsg->msgType));
357
        break;
×
358
      }
359
#if 0
360
      if (pMsg->msgType == TDMT_VND_SUBMIT && (grantCheck(TSDB_GRANT_STORAGE) != TSDB_CODE_SUCCESS)) {
361
        code = TSDB_CODE_VND_NO_WRITE_AUTH;
362
        dDebug("vgId:%d, msg:%p, failed to put into vnode-write queue since %s, type:%s", pVnode->vgId, pMsg,
363
               tstrerror(code), TMSG_INFO(pMsg->msgType));
364
        break;
365
      }
366
#endif
367
      if (pMsg->msgType != TDMT_VND_ALTER_CONFIRM && pVnode->disable) {
2,514,411✔
368
        dDebug("vgId:%d, msg:%p, failed to put into vnode-write queue since its disable, type:%s", pVnode->vgId, pMsg,
505!
369
               TMSG_INFO(pMsg->msgType));
370
        code = TSDB_CODE_VND_STOPPED;
505✔
371
        break;
505✔
372
      }
373
      dGDebug("vgId:%d, msg:%p, put into vnode-write queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
2,513,906!
374
      code = taosWriteQitem(pVnode->pWriteW.queue, pMsg);
2,513,915✔
375
      break;
2,513,931✔
376
    case SYNC_QUEUE:
1,183,236✔
377
      dGDebug("vgId:%d, msg:%p, put into vnode-sync queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
1,183,236!
378
      code = taosWriteQitem(pVnode->pSyncW.queue, pMsg);
1,183,249✔
379
      break;
1,183,241✔
380
    case SYNC_RD_QUEUE:
89,701✔
381
      dGDebug("vgId:%d, msg:%p, put into vnode-sync-rd queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
89,701!
382
      code = taosWriteQitem(pVnode->pSyncRdW.queue, pMsg);
89,701✔
383
      break;
89,701✔
384
    case APPLY_QUEUE:
1,079,539✔
385
      dGDebug("vgId:%d, msg:%p, put into vnode-apply queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
1,079,539!
386
      code = taosWriteQitem(pVnode->pApplyW.queue, pMsg);
1,079,550✔
387
      break;
1,079,540✔
388
    default:
8,363✔
389
      code = TSDB_CODE_INVALID_MSG;
8,363✔
390
      break;
8,363✔
391
  }
392

393
  vmReleaseVnode(pMgmt, pVnode);
11,343,025✔
394
  return code;
11,336,506✔
395
}
396

397
int32_t vmPutMsgToSyncRdQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, SYNC_RD_QUEUE); }
90,087✔
398

399
int32_t vmPutMsgToSyncQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, SYNC_QUEUE); }
893,691✔
400

401
int32_t vmPutMsgToWriteQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, WRITE_QUEUE); }
2,499,401✔
402

403
int32_t vmPutMsgToQueryQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, QUERY_QUEUE); }
1,683,533✔
404

405
int32_t vmPutMsgToFetchQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, FETCH_QUEUE); }
4,546,634✔
406

407
int32_t vmPutMsgToStreamQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, STREAM_QUEUE); }
802✔
408

409
int32_t vmPutMsgToStreamCtrlQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, STREAM_CTRL_QUEUE); }
57,371✔
410

411
int32_t vmPutMsgToStreamLongExecQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, STREAM_LONG_EXEC_QUEUE); }
×
412

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

415
int32_t vmPutMsgToMultiMgmtQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
11,384✔
416
  const STraceId *trace = &pMsg->info.traceId;
11,384✔
417
  dGTrace("msg:%p, put into vnode-multi-mgmt queue", pMsg);
11,384!
418
  return taosWriteQitem(pMgmt->mgmtMultiWorker.queue, pMsg);
11,384✔
419
}
420

421
int32_t vmPutMsgToMgmtQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
11,510✔
422
  const STraceId *trace = &pMsg->info.traceId;
11,510✔
423
  dGTrace("msg:%p, put into vnode-mgmt queue", pMsg);
11,510!
424
  return taosWriteQitem(pMgmt->mgmtWorker.queue, pMsg);
11,510✔
425
}
426

427
int32_t vmPutRpcMsgToQueue(SVnodeMgmt *pMgmt, EQueueType qtype, SRpcMsg *pRpc) {
1,565,867✔
428
  int32_t code;
429
  if (pRpc->contLen < sizeof(SMsgHead)) {
1,565,867!
430
    dError("invalid rpc msg with no msg head at pCont. pRpc:%p, type:%s, len:%d", pRpc, TMSG_INFO(pRpc->msgType),
×
431
           pRpc->contLen);
432
    rpcFreeCont(pRpc->pCont);
×
433
    pRpc->pCont = NULL;
×
434
    return TSDB_CODE_INVALID_MSG;
×
435
  }
436

437
  EQItype  itype = APPLY_QUEUE == qtype ? APPLY_QITEM : RPC_QITEM;
1,565,867✔
438
  SRpcMsg *pMsg;
439
  code = taosAllocateQitem(sizeof(SRpcMsg), itype, pRpc->contLen, (void **)&pMsg);
1,565,867✔
440
  if (code) {
1,565,853!
441
    rpcFreeCont(pRpc->pCont);
×
442
    pRpc->pCont = NULL;
×
443
    return code;
×
444
  }
445

446
  SMsgHead *pHead = pRpc->pCont;
1,565,853✔
447
  dTrace("vgId:%d, msg:%p, is created, type:%s len:%d", pHead->vgId, pMsg, TMSG_INFO(pRpc->msgType), pRpc->contLen);
1,565,853!
448

449
  pHead->contLen = htonl(pHead->contLen);
1,565,853✔
450
  pHead->vgId = htonl(pHead->vgId);
1,565,853✔
451
  memcpy(pMsg, pRpc, sizeof(SRpcMsg));
1,565,853✔
452
  pRpc->pCont = NULL;
1,565,853✔
453

454
  code = vmPutMsgToQueue(pMgmt, pMsg, qtype);
1,565,853✔
455
  if (code != 0) {
1,565,872✔
456
    dTrace("msg:%p, is freed", pMsg);
5,071✔
457
    rpcFreeCont(pMsg->pCont);
5,071✔
458
    taosFreeQitem(pMsg);
5,070✔
459
  }
460

461
  return code;
1,565,870✔
462
}
463

464
int32_t vmGetQueueSize(SVnodeMgmt *pMgmt, int32_t vgId, EQueueType qtype) {
959,907✔
465
  int32_t    size = -1;
959,907✔
466
  SVnodeObj *pVnode = vmAcquireVnode(pMgmt, vgId);
959,907✔
467
  if (pVnode != NULL) {
959,910✔
468
    switch (qtype) {
930,510!
469
      case WRITE_QUEUE:
10,401✔
470
        size = taosQueueItemSize(pVnode->pWriteW.queue);
10,401✔
471
        break;
10,401✔
472
      case SYNC_QUEUE:
×
473
        size = taosQueueItemSize(pVnode->pSyncW.queue);
×
474
        break;
×
475
      case APPLY_QUEUE:
62,870✔
476
        size = taosQueueItemSize(pVnode->pApplyW.queue);
62,870✔
477
        break;
62,871✔
478
      case QUERY_QUEUE:
×
479
        size = taosQueueItemSize(pVnode->pQueryQ);
×
480
        break;
×
481
      case FETCH_QUEUE:
×
482
        size = taosQueueItemSize(pVnode->pFetchQ);
×
483
        break;
×
484
      case STREAM_QUEUE:
857,240✔
485
        size = taosQueueItemSize(pVnode->pStreamQ);
857,240✔
486
        break;
857,241✔
487
      case STREAM_CTRL_QUEUE:
×
488
        size = taosQueueItemSize(pVnode->pStreamCtrlQ);
×
489
        break;
×
490
      case STREAM_LONG_EXEC_QUEUE:
×
491
        size = taosQueueItemSize(pVnode->pStreamLongExecQ);
×
492
        break;
×
493
      case STREAM_CHKPT_QUEUE:
×
494
        size = taosQueueItemSize(pVnode->pStreamChkQ);
×
495
      default:
×
496
        break;
×
497
    }
498
  }
499
  if (pVnode) vmReleaseVnode(pMgmt, pVnode);
959,913✔
500
  if (size < 0) {
959,909✔
501
    dTrace("vgId:%d, can't get size from queue since %s, qtype:%d", vgId, terrstr(), qtype);
29,392✔
502
    size = 0;
29,394✔
503
  }
504
  return size;
959,911✔
505
}
506

507
int32_t vmAllocQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
14,563✔
508
  int32_t         code = 0;
14,563✔
509
  SMultiWorkerCfg wcfg = {.max = 1, .name = "vnode-write", .fp = (FItems)vnodeProposeWriteMsg, .param = pVnode->pImpl};
14,563✔
510
  SMultiWorkerCfg scfg = {.max = 1, .name = "vnode-sync", .fp = (FItems)vmProcessSyncQueue, .param = pVnode};
14,563✔
511
  SMultiWorkerCfg sccfg = {.max = 1, .name = "vnode-sync-rd", .fp = (FItems)vmProcessSyncQueue, .param = pVnode};
14,563✔
512
  SMultiWorkerCfg acfg = {.max = 1, .name = "vnode-apply", .fp = (FItems)vnodeApplyWriteMsg, .param = pVnode->pImpl};
14,563✔
513
  code = tMultiWorkerInit(&pVnode->pWriteW, &wcfg);
14,563✔
514
  if (code) {
14,563!
515
    return code;
×
516
  }
517
  code = tMultiWorkerInit(&pVnode->pSyncW, &scfg);
14,563✔
518
  if (code) {
14,563!
519
    tMultiWorkerCleanup(&pVnode->pWriteW);
×
520
    return code;
×
521
  }
522
  code = tMultiWorkerInit(&pVnode->pSyncRdW, &sccfg);
14,563✔
523
  if (code) {
14,563!
524
    tMultiWorkerCleanup(&pVnode->pWriteW);
×
525
    tMultiWorkerCleanup(&pVnode->pSyncW);
×
526
    return code;
×
527
  }
528
  code = tMultiWorkerInit(&pVnode->pApplyW, &acfg);
14,563✔
529
  if (code) {
14,563!
530
    tMultiWorkerCleanup(&pVnode->pWriteW);
×
531
    tMultiWorkerCleanup(&pVnode->pSyncW);
×
532
    tMultiWorkerCleanup(&pVnode->pSyncRdW);
×
533
    return code;
×
534
  }
535

536
  pVnode->pQueryQ = tQueryAutoQWorkerAllocQueue(&pMgmt->queryPool, pVnode, (FItem)vmProcessQueryQueue);
14,563✔
537
  pVnode->pFetchQ = tWWorkerAllocQueue(&pMgmt->fetchPool, pVnode, (FItems)vmProcessFetchQueue);
14,562✔
538

539
  // init stream msg processing queue family
540
  pVnode->pStreamQ = tAutoQWorkerAllocQueue(&pMgmt->streamPool, pVnode, (FItem)vmProcessStreamQueue, 2);
14,563✔
541
  pVnode->pStreamCtrlQ = tWWorkerAllocQueue(&pMgmt->streamCtrlPool, pVnode, (FItems)vmProcessStreamCtrlQueue);
14,563✔
542
  pVnode->pStreamLongExecQ = tAutoQWorkerAllocQueue(&pMgmt->streamLongExecPool, pVnode, (FItem)vmProcessStreamLongExecQueue, 1);
14,563✔
543
  pVnode->pStreamChkQ = tWWorkerAllocQueue(&pMgmt->streamChkPool, pVnode, (FItems)vmProcessStreamChkptQueue);
14,563✔
544

545
  if (pVnode->pWriteW.queue == NULL || pVnode->pSyncW.queue == NULL || pVnode->pSyncRdW.queue == NULL ||
14,563!
546
      pVnode->pApplyW.queue == NULL || pVnode->pQueryQ == NULL || pVnode->pStreamQ == NULL || pVnode->pFetchQ == NULL
14,563!
547
      || pVnode->pStreamCtrlQ == NULL || pVnode->pStreamLongExecQ == NULL || pVnode->pStreamChkQ == NULL) {
14,563!
548
    return TSDB_CODE_OUT_OF_MEMORY;
×
549
  }
550

551
  dInfo("vgId:%d, write-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pWriteW.queue,
14,563!
552
        taosQueueGetThreadId(pVnode->pWriteW.queue));
553
  dInfo("vgId:%d, sync-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pSyncW.queue,
14,563!
554
        taosQueueGetThreadId(pVnode->pSyncW.queue));
555
  dInfo("vgId:%d, sync-rd-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pSyncRdW.queue,
14,563!
556
        taosQueueGetThreadId(pVnode->pSyncRdW.queue));
557
  dInfo("vgId:%d, apply-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pApplyW.queue,
14,563!
558
        taosQueueGetThreadId(pVnode->pApplyW.queue));
559
  dInfo("vgId:%d, query-queue:%p is alloced", pVnode->vgId, pVnode->pQueryQ);
14,563!
560
  dInfo("vgId:%d, fetch-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pFetchQ,
14,563!
561
        taosQueueGetThreadId(pVnode->pFetchQ));
562
  dInfo("vgId:%d, stream-queue:%p is alloced", pVnode->vgId, pVnode->pStreamQ);
14,563!
563
  dInfo("vgId:%d, stream-long-exec-queue:%p is alloced", pVnode->vgId, pVnode->pStreamLongExecQ);
14,563!
564
  dInfo("vgId:%d, stream-ctrl-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pStreamCtrlQ,
14,563!
565
        taosQueueGetThreadId(pVnode->pStreamCtrlQ));
566
  dInfo("vgId:%d, stream-chk-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pStreamChkQ,
14,563!
567
        taosQueueGetThreadId(pVnode->pStreamChkQ));
568
  return 0;
14,563✔
569
}
570

571
void vmFreeQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
14,561✔
572
  tQueryAutoQWorkerFreeQueue(&pMgmt->queryPool, pVnode->pQueryQ);
14,561✔
573
  tAutoQWorkerFreeQueue(&pMgmt->streamPool, pVnode->pStreamQ);
14,563✔
574
  tAutoQWorkerFreeQueue(&pMgmt->streamLongExecPool, pVnode->pStreamLongExecQ);
14,563✔
575
  tWWorkerFreeQueue(&pMgmt->streamCtrlPool, pVnode->pStreamCtrlQ);
14,563✔
576
  tWWorkerFreeQueue(&pMgmt->streamChkPool, pVnode->pStreamChkQ);
14,563✔
577
  tWWorkerFreeQueue(&pMgmt->fetchPool, pVnode->pFetchQ);
14,563✔
578
  pVnode->pQueryQ = NULL;
14,563✔
579
  pVnode->pFetchQ = NULL;
14,563✔
580

581
  pVnode->pStreamQ = NULL;
14,563✔
582
  pVnode->pStreamCtrlQ = NULL;
14,563✔
583
  pVnode->pStreamLongExecQ = NULL;
14,563✔
584

585
  pVnode->pStreamChkQ = NULL;
14,563✔
586
  pVnode->pFetchQ = NULL;
14,563✔
587
  dDebug("vgId:%d, queue is freed", pVnode->vgId);
14,563✔
588
}
14,563✔
589

590
int32_t vmStartWorker(SVnodeMgmt *pMgmt) {
3,033✔
591
  int32_t code = 0;
3,033✔
592

593
  SQueryAutoQWorkerPool *pQPool = &pMgmt->queryPool;
3,033✔
594
  pQPool->name = "vnode-query";
3,033✔
595
  pQPool->min = tsNumOfVnodeQueryThreads;
3,033✔
596
  pQPool->max = tsNumOfVnodeQueryThreads;
3,033✔
597
  if ((code = tQueryAutoQWorkerInit(pQPool)) != 0) return code;
3,033!
598

599
  tsNumOfQueryThreads += tsNumOfVnodeQueryThreads;
3,033✔
600

601
  SAutoQWorkerPool *pStreamPool = &pMgmt->streamPool;
3,033✔
602
  pStreamPool->name = "vnode-stream";
3,033✔
603
  pStreamPool->ratio = tsRatioOfVnodeStreamThreads;
3,033✔
604
  if ((code = tAutoQWorkerInit(pStreamPool)) != 0) return code;
3,033!
605

606
  SAutoQWorkerPool *pLongExecPool = &pMgmt->streamLongExecPool;
3,033✔
607
  pLongExecPool->name = "vnode-stream-long-exec";
3,033✔
608
  pLongExecPool->ratio = tsRatioOfVnodeStreamThreads/3;
3,033✔
609
  if ((code = tAutoQWorkerInit(pLongExecPool)) != 0) return code;
3,033!
610

611
  SWWorkerPool *pStreamCtrlPool = &pMgmt->streamCtrlPool;
3,033✔
612
  pStreamCtrlPool->name = "vnode-stream-ctrl";
3,033✔
613
  pStreamCtrlPool->max = 4;
3,033✔
614
  if ((code = tWWorkerInit(pStreamCtrlPool)) != 0) return code;
3,033!
615

616
  SWWorkerPool *pStreamChkPool = &pMgmt->streamChkPool;
3,033✔
617
  pStreamChkPool->name = "vnode-stream-chkpt";
3,033✔
618
  pStreamChkPool->max = 1;
3,033✔
619
  if ((code = tWWorkerInit(pStreamChkPool)) != 0) return code;
3,033!
620

621
  SWWorkerPool *pFPool = &pMgmt->fetchPool;
3,033✔
622
  pFPool->name = "vnode-fetch";
3,033✔
623
  pFPool->max = tsNumOfVnodeFetchThreads;
3,033✔
624
  if ((code = tWWorkerInit(pFPool)) != 0) return code;
3,033!
625

626
  SSingleWorkerCfg mgmtCfg = {
3,033✔
627
      .min = 1, .max = 1, .name = "vnode-mgmt", .fp = (FItem)vmProcessMgmtQueue, .param = pMgmt};
628

629
  if ((code = tSingleWorkerInit(&pMgmt->mgmtWorker, &mgmtCfg)) != 0) return code;
3,033!
630

631
  int32_t threadNum = 0;
3,033✔
632
  if (tsNumOfCores == 1) {
3,033!
633
    threadNum = 2;
×
634
  } else {
635
    threadNum = tsNumOfCores;
3,033✔
636
  }
637
  SSingleWorkerCfg multiMgmtCfg = {.min = threadNum,
3,033✔
638
                                   .max = threadNum,
639
                                   .name = "vnode-multi-mgmt",
640
                                   .fp = (FItem)vmProcessMultiMgmtQueue,
641
                                   .param = pMgmt};
642

643
  if ((code = tSingleWorkerInit(&pMgmt->mgmtMultiWorker, &multiMgmtCfg)) != 0) return code;
3,033!
644

645
  dDebug("vnode workers are initialized");
3,033✔
646
  return 0;
3,033✔
647
}
648

649
void vmStopWorker(SVnodeMgmt *pMgmt) {
3,033✔
650
  tQueryAutoQWorkerCleanup(&pMgmt->queryPool);
3,033✔
651
  tAutoQWorkerCleanup(&pMgmt->streamPool);
3,033✔
652
  tAutoQWorkerCleanup(&pMgmt->streamLongExecPool);
3,033✔
653
  tWWorkerCleanup(&pMgmt->streamCtrlPool);
3,033✔
654
  tWWorkerCleanup(&pMgmt->streamChkPool);
3,033✔
655
  tWWorkerCleanup(&pMgmt->fetchPool);
3,033✔
656
  dDebug("vnode workers are closed");
3,033✔
657
}
3,033✔
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