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

taosdata / TDengine / #4488

12 Jul 2025 07:47AM UTC coverage: 62.207% (-0.7%) from 62.948%
#4488

push

travis-ci

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

157961 of 324087 branches covered (48.74%)

Branch coverage included in aggregate %.

244465 of 322830 relevant lines covered (75.73%)

6561668.76 hits per line

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

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

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

36
  dGTrace("msg:%p, get from vnode-multi-mgmt queue", pMsg);
11,546!
37
  switch (pMsg->msgType) {
11,546!
38
    case TDMT_DND_CREATE_VNODE:
11,547✔
39
      code = vmProcessCreateVnodeReq(pMgmt, pMsg);
11,547✔
40
      break;
11,547✔
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,546!
52
    if (code != 0) {
11,547!
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,547✔
57
  }
58

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

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

69
  dGTrace("msg:%p, get from vnode-mgmt queue", pMsg);
11,641!
70
  switch (pMsg->msgType) {
11,641!
71
    case TDMT_DND_CREATE_VNODE:
×
72
      code = vmProcessCreateVnodeReq(pMgmt, pMsg);
×
73
      break;
×
74
    case TDMT_DND_DROP_VNODE:
4,616✔
75
      code = vmProcessDropVnodeReq(pMgmt, pMsg);
4,616✔
76
      break;
4,616✔
77
    case TDMT_VND_ALTER_REPLICA:
1,769✔
78
      code = vmProcessAlterVnodeReplicaReq(pMgmt, pMsg);
1,769✔
79
      break;
1,769✔
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:
5,084✔
87
      code = vmProcessAlterVnodeTypeReq(pMgmt, pMsg);
5,084✔
88
      break;
5,084✔
89
    case TDMT_DND_CHECK_VNODE_LEARNER_CATCHUP:
×
90
      code = vmProcessCheckLearnCatchupReq(pMgmt, pMsg);
×
91
      break;
×
92
    case TDMT_VND_ARB_HEARTBEAT:
106✔
93
      code = vmProcessArbHeartBeatReq(pMgmt, pMsg);
106✔
94
      break;
106✔
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,641!
101
    if (code != 0) {
11,641✔
102
      if (terrno != 0) code = terrno;
4,838!
103
      dGError("msg:%p, failed to process since %s, type:%s", pMsg, tstrerror(code), TMSG_INFO(pMsg->msgType));
4,838!
104
    }
105
    vmSendRsp(pMsg, code);
11,641✔
106
  }
107

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

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

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

125
  dGTrace("vgId:%d, msg:%p, is freed, code:0x%x", pVnode->vgId, pMsg, code);
1,690,469!
126
  rpcFreeCont(pMsg->pCont);
1,690,468✔
127
  taosFreeQitem(pMsg);
1,691,847✔
128
}
1,691,931✔
129

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

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

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

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

156
  while (1) {
58,910✔
157
    if (taosGetQitem(pQall, &pItem) == 0) {
111,059✔
158
      break;
52,161✔
159
    }
160

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

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

173
    dGTrace("vgId:%d, msg:%p, is freed, code:0x%x", pVnode->vgId, pMsg, code);
58,910!
174
    rpcFreeCont(pMsg->pCont);
58,910✔
175
    taosFreeQitem(pMsg);
58,909✔
176
  }
177
#endif
178
}
52,161✔
179

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

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

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

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

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

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

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

215
  code = vnodeProcessStreamLongExecMsg(pVnode->pImpl, pMsg, pInfo);
1,027✔
216
  if (code != 0) {
1,027✔
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,027!
224
  rpcFreeCont(pMsg->pCont);
1,027✔
225
  taosFreeQitem(pMsg);
1,027✔
226
}
1,027✔
227

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

232
  for (int32_t i = 0; i < numOfMsgs; ++i) {
8,759,111✔
233
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
4,418,300!
234
    const STraceId *trace = &pMsg->info.traceId;
4,420,724✔
235
    dGTrace("vgId:%d, msg:%p, get from vnode-fetch queue", pVnode->vgId, pMsg);
4,420,724!
236

237
    terrno = 0;
4,420,724✔
238
    int32_t code = vnodeProcessFetchMsg(pVnode->pImpl, pMsg, pInfo);
4,418,047✔
239
    if (code != 0) {
4,419,230✔
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,419,230!
254
    rpcFreeCont(pMsg->pCont);
4,419,230✔
255
    taosFreeQitem(pMsg);
4,421,288✔
256
  }
257
}
4,340,811✔
258

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

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

268
    int32_t code = vnodeProcessSyncMsg(pVnode->pImpl, pMsg, NULL);  // no response here
1,220,977✔
269
    dGTrace("vgId:%d, msg:%p, is freed, code:0x%x", pVnode->vgId, pMsg, code);
1,220,783!
270
    rpcFreeCont(pMsg->pCont);
1,220,783✔
271
    taosFreeQitem(pMsg);
1,220,804✔
272
  }
273
}
1,026,091✔
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,590,023✔
285
  STfs *pTfs = pVnode->pImpl->pTfs;
2,590,023✔
286
  if (pTfs) {
2,590,023!
287
    return tfsDiskSpaceSufficient(pTfs, 0, pVnode->diskPrimary);
2,590,123✔
288
  } else {
289
    return osDataSpaceSufficient();
×
290
  }
291
}
292

293
static int32_t vmAcquireVnodeWrapper(SVnodeMgmt *pMgt, int32_t vgId, SVnodeObj **pNode) {
11,122,962✔
294
  *pNode = vmAcquireVnode(pMgt, vgId);
11,122,962✔
295
  if (*pNode == NULL) {
11,137,250✔
296
    return terrno;
10,702✔
297
  }
298
  return 0;
11,126,548✔
299
}
300
static int32_t vmPutMsgToQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg, EQueueType qtype) {
11,123,743✔
301
  int32_t         code = 0;
11,123,743✔
302
  const STraceId *trace = &pMsg->info.traceId;
11,123,743✔
303
  if (pMsg->contLen < sizeof(SMsgHead)) {
11,123,743!
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,123,743✔
310

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

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

322
  switch (qtype) {
11,126,075✔
323
    case QUERY_QUEUE:
1,689,721✔
324
      code = vnodePreprocessQueryMsg(pVnode->pImpl, pMsg);
1,689,721✔
325
      if (code) {
1,690,169✔
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,690,152!
329
        code = taosWriteQitem(pVnode->pQueryQ, pMsg);
1,690,152✔
330
      }
331
      break;
1,690,925✔
332
    case STREAM_QUEUE:
104,493✔
333
      dGTrace("vgId:%d, msg:%p, put into vnode-stream queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
104,493!
334
      code = taosWriteQitem(pVnode->pStreamQ, pMsg);
104,493✔
335
      break;
104,495✔
336
    case STREAM_CTRL_QUEUE:
58,906✔
337
      dGTrace("vgId:%d, msg:%p, put into vnode-stream-ctrl queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
58,906!
338
      code = taosWriteQitem(pVnode->pStreamCtrlQ, pMsg);
58,906✔
339
      break;
58,908✔
340
    case STREAM_LONG_EXEC_QUEUE:
1,027✔
341
      dGTrace("vgId:%d, msg:%p, put into vnode-stream-long-exec queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
1,027!
342
      code = taosWriteQitem(pVnode->pStreamLongExecQ, pMsg);
1,027✔
343
      break;
1,027✔
344
    case STREAM_CHKPT_QUEUE:
3,236✔
345
      dGTrace("vgId:%d, msg:%p, put into vnode-stream-chkpt queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
3,236!
346
      code = taosWriteQitem(pVnode->pStreamChkQ, pMsg);
3,236✔
347
      break;
3,236✔
348
    case FETCH_QUEUE:
4,414,882✔
349
      dGTrace("vgId:%d, msg:%p, put into vnode-fetch queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
4,414,882!
350
      code = taosWriteQitem(pVnode->pFetchQ, pMsg);
4,414,885✔
351
      break;
4,418,947✔
352
    case WRITE_QUEUE:
2,590,038✔
353
      if (!vmDataSpaceSufficient(pVnode)) {
2,590,038!
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,590,070✔
368
        dDebug("vgId:%d, msg:%p, failed to put into vnode-write queue since its disable, type:%s", pVnode->vgId, pMsg,
592!
369
               TMSG_INFO(pMsg->msgType));
370
        code = TSDB_CODE_VND_STOPPED;
592✔
371
        break;
592✔
372
      }
373
      dGDebug("vgId:%d, msg:%p, put into vnode-write queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
2,589,478!
374
      code = taosWriteQitem(pVnode->pWriteW.queue, pMsg);
2,589,483✔
375
      break;
2,589,503✔
376
    case SYNC_QUEUE:
1,131,323✔
377
      dGDebug("vgId:%d, msg:%p, put into vnode-sync queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
1,131,323!
378
      code = taosWriteQitem(pVnode->pSyncW.queue, pMsg);
1,131,336✔
379
      break;
1,131,325✔
380
    case SYNC_RD_QUEUE:
89,684✔
381
      dGDebug("vgId:%d, msg:%p, put into vnode-sync-rd queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
89,684!
382
      code = taosWriteQitem(pVnode->pSyncRdW.queue, pMsg);
89,684✔
383
      break;
89,681✔
384
    case APPLY_QUEUE:
1,035,264✔
385
      dGDebug("vgId:%d, msg:%p, put into vnode-apply queue, type:%s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType));
1,035,264!
386
      code = taosWriteQitem(pVnode->pApplyW.queue, pMsg);
1,035,267✔
387
      break;
1,035,255✔
388
    default:
7,501✔
389
      code = TSDB_CODE_INVALID_MSG;
7,501✔
390
      break;
7,501✔
391
  }
392

393
  vmReleaseVnode(pMgmt, pVnode);
11,131,395✔
394
  return code;
11,127,147✔
395
}
396

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

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

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

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

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

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

409
int32_t vmPutMsgToStreamCtrlQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, STREAM_CTRL_QUEUE); }
58,915✔
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,418✔
416
  const STraceId *trace = &pMsg->info.traceId;
11,418✔
417
  dGTrace("msg:%p, put into vnode-multi-mgmt queue", pMsg);
11,418!
418
  return taosWriteQitem(pMgmt->mgmtMultiWorker.queue, pMsg);
11,418✔
419
}
420

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

427
int32_t vmPutRpcMsgToQueue(SVnodeMgmt *pMgmt, EQueueType qtype, SRpcMsg *pRpc) {
1,483,169✔
428
  int32_t code;
429
  if (pRpc->contLen < sizeof(SMsgHead)) {
1,483,169!
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,483,169✔
438
  SRpcMsg *pMsg;
439
  code = taosAllocateQitem(sizeof(SRpcMsg), itype, pRpc->contLen, (void **)&pMsg);
1,483,169✔
440
  if (code) {
1,483,163!
441
    rpcFreeCont(pRpc->pCont);
×
442
    pRpc->pCont = NULL;
×
443
    return code;
×
444
  }
445

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

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

454
  code = vmPutMsgToQueue(pMgmt, pMsg, qtype);
1,483,163✔
455
  if (code != 0) {
1,483,169✔
456
    dTrace("msg:%p, is freed", pMsg);
5,095✔
457
    rpcFreeCont(pMsg->pCont);
5,095✔
458
    taosFreeQitem(pMsg);
5,097✔
459
  }
460

461
  return code;
1,483,173✔
462
}
463

464
int32_t vmGetQueueSize(SVnodeMgmt *pMgmt, int32_t vgId, EQueueType qtype) {
954,001✔
465
  int32_t    size = -1;
954,001✔
466
  SVnodeObj *pVnode = vmAcquireVnode(pMgmt, vgId);
954,001✔
467
  if (pVnode != NULL) {
954,005✔
468
    switch (qtype) {
924,677!
469
      case WRITE_QUEUE:
10,440✔
470
        size = taosQueueItemSize(pVnode->pWriteW.queue);
10,440✔
471
        break;
10,441✔
472
      case SYNC_QUEUE:
×
473
        size = taosQueueItemSize(pVnode->pSyncW.queue);
×
474
        break;
×
475
      case APPLY_QUEUE:
61,845✔
476
        size = taosQueueItemSize(pVnode->pApplyW.queue);
61,845✔
477
        break;
61,845✔
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:
852,392✔
485
        size = taosQueueItemSize(pVnode->pStreamQ);
852,392✔
486
        break;
852,392✔
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);
954,006✔
500
  if (size < 0) {
953,992✔
501
    dTrace("vgId:%d, can't get size from queue since %s, qtype:%d", vgId, terrstr(), qtype);
29,311✔
502
    size = 0;
29,319✔
503
  }
504
  return size;
954,000✔
505
}
506

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

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

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

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

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

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

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

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

599
  tsNumOfQueryThreads += tsNumOfVnodeQueryThreads;
3,021✔
600

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

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

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

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

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

626
  SSingleWorkerCfg mgmtCfg = {
3,021✔
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,021!
630

631
  int32_t threadNum = 0;
3,021✔
632
  if (tsNumOfCores == 1) {
3,021!
633
    threadNum = 2;
×
634
  } else {
635
    threadNum = tsNumOfCores;
3,021✔
636
  }
637
  SSingleWorkerCfg multiMgmtCfg = {.min = threadNum,
3,021✔
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,021!
644

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

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