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

taosdata / TDengine / #3629

04 Mar 2025 01:45PM UTC coverage: 63.692% (-0.1%) from 63.79%
#3629

push

travis-ci

web-flow
Merge pull request #30007 from taosdata/revert-29951-docs/update-exception-handling-strategy

Revert "docs: update exception handling strategy"

149369 of 300378 branches covered (49.73%)

Branch coverage included in aggregate %.

233614 of 300930 relevant lines covered (77.63%)

18792670.99 hits per line

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

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

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

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

43
  if (IsReq(pMsg)) {
10,074!
44
    if (code != 0) {
10,173!
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);
10,173✔
49
  }
50

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

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

61
  dGTrace("msg:%p, get from vnode-mgmt queue", pMsg);
9,547!
62
  switch (pMsg->msgType) {
9,547!
63
    case TDMT_DND_CREATE_VNODE:
×
64
      code = vmProcessCreateVnodeReq(pMgmt, pMsg);
×
65
      break;
×
66
    case TDMT_DND_DROP_VNODE:
4,552✔
67
      code = vmProcessDropVnodeReq(pMgmt, pMsg);
4,552✔
68
      break;
4,552✔
69
    case TDMT_VND_ALTER_REPLICA:
1,078✔
70
      code = vmProcessAlterVnodeReplicaReq(pMgmt, pMsg);
1,078✔
71
      break;
1,078✔
72
    case TDMT_VND_DISABLE_WRITE:
32✔
73
      code = vmProcessDisableVnodeWriteReq(pMgmt, pMsg);
32✔
74
      break;
32✔
75
    case TDMT_VND_ALTER_HASHRANGE:
32✔
76
      code = vmProcessAlterHashRangeReq(pMgmt, pMsg);
32✔
77
      break;
32✔
78
    case TDMT_DND_ALTER_VNODE_TYPE:
3,821✔
79
      code = vmProcessAlterVnodeTypeReq(pMgmt, pMsg);
3,821✔
80
      break;
3,821✔
81
    case TDMT_DND_CHECK_VNODE_LEARNER_CATCHUP:
×
82
      code = vmProcessCheckLearnCatchupReq(pMgmt, pMsg);
×
83
      break;
×
84
    case TDMT_VND_ARB_HEARTBEAT:
32✔
85
      code = vmProcessArbHeartBeatReq(pMgmt, pMsg);
32✔
86
      break;
32✔
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)) {
9,547!
93
    if (code != 0) {
9,547✔
94
      if (terrno != 0) code = terrno;
3,661!
95
      dGError("msg:%p, failed to process since %s, type:%s", pMsg, tstrerror(code), TMSG_INFO(pMsg->msgType));
3,661!
96
    }
97
    vmSendRsp(pMsg, code);
9,547✔
98
  }
99

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

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

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

117
  dGTrace("vgId:%d, msg:%p is freed, code:0x%x", pVnode->vgId, pMsg, code);
8,286,606!
118
  rpcFreeCont(pMsg->pCont);
8,286,607✔
119
  taosFreeQitem(pMsg);
8,291,314✔
120
}
8,291,456✔
121

122
static void vmProcessStreamQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
371,625✔
123
  SVnodeObj      *pVnode = pInfo->ahandle;
371,625✔
124
  const STraceId *trace = &pMsg->info.traceId;
371,625✔
125

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

135
  dGTrace("vgId:%d, msg:%p is freed, code:0x%x", pVnode->vgId, pMsg, code);
370,784!
136
  rpcFreeCont(pMsg->pCont);
370,784✔
137
  taosFreeQitem(pMsg);
371,207✔
138
}
371,647✔
139

140
static void vmProcessStreamCtrlQueue(SQueueInfo *pInfo, STaosQall* pQall, int32_t numOfItems) {
172,222✔
141
  SVnodeObj *pVnode = pInfo->ahandle;
172,222✔
142
  void      *pItem = NULL;
172,222✔
143
  int32_t    code = 0;
172,222✔
144

145
  while (1) {
214,422✔
146
    if (taosGetQitem(pQall, &pItem) == 0) {
386,644✔
147
      break;
172,222✔
148
    }
149

150
    SRpcMsg        *pMsg = pItem;
214,422✔
151
    const STraceId *trace = &pMsg->info.traceId;
214,422✔
152

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

162
    dGTrace("vgId:%d, msg:%p is freed, code:0x%x", pVnode->vgId, pMsg, code);
214,422!
163
    rpcFreeCont(pMsg->pCont);
214,422✔
164
    taosFreeQitem(pMsg);
214,422✔
165
  }
166
}
172,222✔
167

168
static void vmProcessStreamLongExecQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
2,537✔
169
  SVnodeObj      *pVnode = pInfo->ahandle;
2,537✔
170
  const STraceId *trace = &pMsg->info.traceId;
2,537✔
171
  int32_t         code = 0;
2,537✔
172

173
  dGTrace("vgId:%d, msg:%p get from vnode-stream long-exec queue", pVnode->vgId, pMsg);
2,537!
174

175
  code = vnodeProcessStreamLongExecMsg(pVnode->pImpl, pMsg, pInfo);
2,537✔
176
  if (code != 0) {
2,537✔
177
    terrno = code;
1✔
178
    dGError("vgId:%d, msg:%p failed to process stream msg %s since %s", pVnode->vgId, pMsg, TMSG_INFO(pMsg->msgType),
1!
179
            tstrerror(code));
180
    vmSendRsp(pMsg, code);
1✔
181
  }
182

183
  dGTrace("vgId:%d, msg:%p is freed, code:0x%x", pVnode->vgId, pMsg, code);
2,537!
184
  rpcFreeCont(pMsg->pCont);
2,537✔
185
  taosFreeQitem(pMsg);
2,537✔
186
}
2,537✔
187

188
static void vmProcessFetchQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
20,410,854✔
189
  SVnodeObj *pVnode = pInfo->ahandle;
20,410,854✔
190
  SRpcMsg   *pMsg = NULL;
20,410,854✔
191

192
  for (int32_t i = 0; i < numOfMsgs; ++i) {
41,290,835✔
193
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
20,872,243!
194
    const STraceId *trace = &pMsg->info.traceId;
20,873,908✔
195
    dGTrace("vgId:%d, msg:%p get from vnode-fetch queue", pVnode->vgId, pMsg);
20,873,908!
196

197
    terrno = 0;
20,873,908✔
198
    int32_t code = vnodeProcessFetchMsg(pVnode->pImpl, pMsg, pInfo);
20,868,573✔
199
    if (code != 0) {
20,869,872✔
200
      if (code == -1 && terrno != 0) {
1,822!
201
        code = terrno;
×
202
      }
203

204
      if (code == TSDB_CODE_WAL_LOG_NOT_EXIST) {
1,822!
205
        dGDebug("vnodeProcessFetchMsg vgId:%d, msg:%p failed to fetch since %s", pVnode->vgId, pMsg, terrstr());
×
206
      } else {
207
        dGError("vnodeProcessFetchMsg vgId:%d, msg:%p failed to fetch since %s", pVnode->vgId, pMsg, terrstr());
1,822!
208
      }
209

210
      vmSendRsp(pMsg, code);
1,822✔
211
    }
212

213
    dGTrace("vnodeProcessFetchMsg vgId:%d, msg:%p is freed, code:0x%x", pVnode->vgId, pMsg, code);
20,869,872!
214
    rpcFreeCont(pMsg->pCont);
20,869,873✔
215
    taosFreeQitem(pMsg);
20,878,485✔
216
  }
217
}
20,418,592✔
218

219
static void vmProcessSyncQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
1,403,952✔
220
  SVnodeObj *pVnode = pInfo->ahandle;
1,403,952✔
221
  SRpcMsg   *pMsg = NULL;
1,403,952✔
222

223
  for (int32_t i = 0; i < numOfMsgs; ++i) {
3,951,605✔
224
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
2,547,413!
225
    const STraceId *trace = &pMsg->info.traceId;
2,547,504✔
226
    dGTrace("vgId:%d, msg:%p get from vnode-sync queue", pVnode->vgId, pMsg);
2,547,504!
227

228
    int32_t code = vnodeProcessSyncMsg(pVnode->pImpl, pMsg, NULL);  // no response here
2,547,504✔
229
    dGTrace("vgId:%d, msg:%p is freed, code:0x%x", pVnode->vgId, pMsg, code);
2,547,172!
230
    rpcFreeCont(pMsg->pCont);
2,547,172✔
231
    taosFreeQitem(pMsg);
2,547,571✔
232
  }
233
}
1,404,192✔
234

235
static void vmSendResponse(SRpcMsg *pMsg) {
×
236
  if (pMsg->info.handle) {
×
237
    SRpcMsg rsp = {.info = pMsg->info, .code = terrno};
×
238
    if (rpcSendResponse(&rsp) != 0) {
×
239
      dError("failed to send response since %s", terrstr());
×
240
    }
241
  }
242
}
×
243

244
static bool vmDataSpaceSufficient(SVnodeObj *pVnode) {
10,361,964✔
245
  STfs *pTfs = pVnode->pImpl->pTfs;
10,361,964✔
246
  if (pTfs) {
10,361,964!
247
    return tfsDiskSpaceSufficient(pTfs, 0, pVnode->diskPrimary);
10,370,224✔
248
  } else {
249
    return osDataSpaceSufficient();
×
250
  }
251
}
252

253
static int32_t vmAcquireVnodeWrapper(SVnodeMgmt *pMgt, int32_t vgId, SVnodeObj **pNode) {
44,163,126✔
254
  *pNode = vmAcquireVnode(pMgt, vgId);
44,163,126✔
255
  if (*pNode == NULL) {
44,238,457✔
256
    return terrno;
17,485✔
257
  }
258
  return 0;
44,220,972✔
259
}
260
static int32_t vmPutMsgToQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg, EQueueType qtype) {
44,166,127✔
261
  int32_t         code = 0;
44,166,127✔
262
  const STraceId *trace = &pMsg->info.traceId;
44,166,127✔
263
  if (pMsg->contLen < sizeof(SMsgHead)) {
44,166,127!
264
    dGError("invalid rpc msg with no msg head at pCont. pMsg:%p, type:%s, contLen:%d", pMsg, TMSG_INFO(pMsg->msgType),
×
265
            pMsg->contLen);
266
    return TSDB_CODE_INVALID_MSG;
×
267
  }
268

269
  SMsgHead *pHead = pMsg->pCont;
44,166,127✔
270

271
  pHead->contLen = ntohl(pHead->contLen);
44,166,127✔
272
  pHead->vgId = ntohl(pHead->vgId);
44,166,127✔
273

274
  SVnodeObj *pVnode = NULL;
44,166,127✔
275
  code = vmAcquireVnodeWrapper(pMgmt, pHead->vgId, &pVnode);
44,166,127✔
276
  if (code != 0) {
44,220,232✔
277
    dGDebug("vgId:%d, msg:%p failed to put into vnode queue since %s, type:%s qtype:%d contLen:%d", pHead->vgId, pMsg,
17,650!
278
            tstrerror(code), TMSG_INFO(pMsg->msgType), qtype, pHead->contLen);
279
    return code;
17,650✔
280
  }
281

282
  switch (qtype) {
44,202,582✔
283
    case QUERY_QUEUE:
8,286,823✔
284
      code = vnodePreprocessQueryMsg(pVnode->pImpl, pMsg);
8,286,823✔
285
      if (code) {
8,286,274✔
286
        dError("vgId:%d, msg:%p preprocess query msg failed since %s", pVnode->vgId, pMsg, tstrerror(code));
11!
287
      } else {
288
        dGTrace("vgId:%d, msg:%p put into vnode-query queue", pVnode->vgId, pMsg);
8,286,263!
289
        code = taosWriteQitem(pVnode->pQueryQ, pMsg);
8,286,263✔
290
      }
291
      break;
8,288,414✔
292
    case STREAM_QUEUE:
371,666✔
293
      dGTrace("vgId:%d, msg:%p put into vnode-stream queue", pVnode->vgId, pMsg);
371,666!
294
      code = taosWriteQitem(pVnode->pStreamQ, pMsg);
371,666✔
295
      break;
371,667✔
296
    case STREAM_CTRL_QUEUE:
214,022✔
297
      dGTrace("vgId:%d, msg:%p put into vnode-stream-ctrl queue", pVnode->vgId, pMsg);
214,022!
298
      code = taosWriteQitem(pVnode->pStreamCtrlQ, pMsg);
214,021✔
299
      break;
214,314✔
300
    case STREAM_LONG_EXEC_QUEUE:
2,537✔
301
      dGTrace("vgId:%d, msg:%p put into vnode-stream-long-exec queue", pVnode->vgId, pMsg);
2,537!
302
      code = taosWriteQitem(pVnode->pStreamLongExecQ, pMsg);
2,537✔
303
      break;
2,537✔
304
    case FETCH_QUEUE:
20,855,915✔
305
      dGTrace("vgId:%d, msg:%p put into vnode-fetch queue", pVnode->vgId, pMsg);
20,855,915!
306
      code = taosWriteQitem(pVnode->pFetchQ, pMsg);
20,855,919✔
307
      break;
20,872,580✔
308
    case WRITE_QUEUE:
10,362,783✔
309
      if (!vmDataSpaceSufficient(pVnode)) {
10,362,783!
310
        code = TSDB_CODE_NO_ENOUGH_DISKSPACE;
×
311
        dError("vgId:%d, msg:%p put into vnode-write queue failed since %s", pVnode->vgId, pMsg, tstrerror(code));
×
312
        break;
×
313
      }
314
      if (pMsg->msgType == TDMT_VND_SUBMIT && (grantCheck(TSDB_GRANT_STORAGE) != TSDB_CODE_SUCCESS)) {
10,374,793!
315
        code = TSDB_CODE_VND_NO_WRITE_AUTH;
×
316
        dDebug("vgId:%d, msg:%p put into vnode-write queue failed since %s", pVnode->vgId, pMsg, tstrerror(code));
×
317
        break;
×
318
      }
319
      if (pMsg->msgType != TDMT_VND_ALTER_CONFIRM && pVnode->disable) {
10,366,044✔
320
        dDebug("vgId:%d, msg:%p put into vnode-write queue failed since its disable", pVnode->vgId, pMsg);
258!
321
        code = TSDB_CODE_VND_STOPPED;
258✔
322
        break;
258✔
323
      }
324
      dGTrace("vgId:%d, msg:%p put into vnode-write queue", pVnode->vgId, pMsg);
10,365,786!
325
      code = taosWriteQitem(pVnode->pWriteW.queue, pMsg);
10,365,786✔
326
      break;
10,390,110✔
327
    case SYNC_QUEUE:
2,479,655✔
328
      dGTrace("vgId:%d, msg:%p put into vnode-sync queue", pVnode->vgId, pMsg);
2,479,655!
329
      code = taosWriteQitem(pVnode->pSyncW.queue, pMsg);
2,479,655✔
330
      break;
2,480,818✔
331
    case SYNC_RD_QUEUE:
66,785✔
332
      dGTrace("vgId:%d, msg:%p put into vnode-sync-rd queue", pVnode->vgId, pMsg);
66,785!
333
      code = taosWriteQitem(pVnode->pSyncRdW.queue, pMsg);
66,785✔
334
      break;
66,795✔
335
    case APPLY_QUEUE:
1,525,228✔
336
      dGTrace("vgId:%d, msg:%p put into vnode-apply queue", pVnode->vgId, pMsg);
1,525,228!
337
      code = taosWriteQitem(pVnode->pApplyW.queue, pMsg);
1,525,228✔
338
      break;
1,525,220✔
339
    default:
37,168✔
340
      code = TSDB_CODE_INVALID_MSG;
37,168✔
341
      break;
37,168✔
342
  }
343

344
  vmReleaseVnode(pMgmt, pVnode);
44,249,881✔
345
  return code;
44,219,890✔
346
}
347

348
int32_t vmPutMsgToSyncRdQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, SYNC_RD_QUEUE); }
67,243✔
349

350
int32_t vmPutMsgToSyncQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, SYNC_QUEUE); }
2,081,600✔
351

352
int32_t vmPutMsgToWriteQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, WRITE_QUEUE); }
10,335,939✔
353

354
int32_t vmPutMsgToQueryQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, QUERY_QUEUE); }
7,701,541✔
355

356
int32_t vmPutMsgToFetchQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, FETCH_QUEUE); }
20,861,667✔
357

358
int32_t vmPutMsgToStreamQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, STREAM_QUEUE); }
907✔
359

360
int32_t vmPutMsgToStreamCtrlQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return vmPutMsgToQueue(pMgmt, pMsg, STREAM_CTRL_QUEUE); }
214,052✔
361

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

364
int32_t vmPutMsgToMultiMgmtQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
10,168✔
365
  const STraceId *trace = &pMsg->info.traceId;
10,168✔
366
  dGTrace("msg:%p, put into vnode-multi-mgmt queue", pMsg);
10,168!
367
  return taosWriteQitem(pMgmt->mgmtMultiWorker.queue, pMsg);
10,168✔
368
}
369

370
int32_t vmPutMsgToMgmtQueue(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
9,540✔
371
  const STraceId *trace = &pMsg->info.traceId;
9,540✔
372
  dGTrace("msg:%p, put into vnode-mgmt queue", pMsg);
9,540!
373
  return taosWriteQitem(pMgmt->mgmtWorker.queue, pMsg);
9,540✔
374
}
375

376
int32_t vmPutRpcMsgToQueue(SVnodeMgmt *pMgmt, EQueueType qtype, SRpcMsg *pRpc) {
2,922,527✔
377
  int32_t code;
378
  if (pRpc->contLen < sizeof(SMsgHead)) {
2,922,527!
379
    dError("invalid rpc msg with no msg head at pCont. pRpc:%p, type:%s, len:%d", pRpc, TMSG_INFO(pRpc->msgType),
×
380
           pRpc->contLen);
381
    rpcFreeCont(pRpc->pCont);
×
382
    pRpc->pCont = NULL;
×
383
    return TSDB_CODE_INVALID_MSG;
×
384
  }
385

386
  EQItype  itype = APPLY_QUEUE == qtype ? APPLY_QITEM : RPC_QITEM;
2,922,527✔
387
  SRpcMsg *pMsg;
388
  code = taosAllocateQitem(sizeof(SRpcMsg), itype, pRpc->contLen, (void **)&pMsg);
2,922,527✔
389
  if (code) {
2,922,486!
390
    rpcFreeCont(pRpc->pCont);
×
391
    pRpc->pCont = NULL;
×
392
    return code;
×
393
  }
394

395
  SMsgHead *pHead = pRpc->pCont;
2,922,486✔
396
  dTrace("vgId:%d, msg:%p is created, type:%s len:%d", pHead->vgId, pMsg, TMSG_INFO(pRpc->msgType), pRpc->contLen);
2,922,486!
397

398
  pHead->contLen = htonl(pHead->contLen);
2,922,486✔
399
  pHead->vgId = htonl(pHead->vgId);
2,922,486✔
400
  memcpy(pMsg, pRpc, sizeof(SRpcMsg));
2,922,486✔
401
  pRpc->pCont = NULL;
2,922,486✔
402

403
  code = vmPutMsgToQueue(pMgmt, pMsg, qtype);
2,922,486✔
404
  if (code != 0) {
2,922,539✔
405
    dTrace("msg:%p, is freed", pMsg);
5,505✔
406
    rpcFreeCont(pMsg->pCont);
5,505✔
407
    taosFreeQitem(pMsg);
5,507✔
408
  }
409

410
  return code;
2,922,535✔
411
}
412

413
int32_t vmGetQueueSize(SVnodeMgmt *pMgmt, int32_t vgId, EQueueType qtype) {
156,746✔
414
  int32_t    size = -1;
156,746✔
415
  SVnodeObj *pVnode = vmAcquireVnode(pMgmt, vgId);
156,746✔
416
  if (pVnode != NULL) {
156,759✔
417
    switch (qtype) {
131,856!
418
      case WRITE_QUEUE:
×
419
        size = taosQueueItemSize(pVnode->pWriteW.queue);
×
420
        break;
×
421
      case SYNC_QUEUE:
×
422
        size = taosQueueItemSize(pVnode->pSyncW.queue);
×
423
        break;
×
424
      case APPLY_QUEUE:
131,856✔
425
        size = taosQueueItemSize(pVnode->pApplyW.queue);
131,856✔
426
        break;
131,858✔
427
      case QUERY_QUEUE:
×
428
        size = taosQueueItemSize(pVnode->pQueryQ);
×
429
        break;
×
430
      case FETCH_QUEUE:
×
431
        size = taosQueueItemSize(pVnode->pFetchQ);
×
432
        break;
×
433
      case STREAM_QUEUE:
×
434
        size = taosQueueItemSize(pVnode->pStreamQ);
×
435
        break;
×
436
      case STREAM_CTRL_QUEUE:
×
437
        size = taosQueueItemSize(pVnode->pStreamCtrlQ);
×
438
        break;
×
439
      case STREAM_LONG_EXEC_QUEUE:
×
440
        size = taosQueueItemSize(pVnode->pStreamLongExecQ);
×
441
        break;
×
442
      default:
×
443
        break;
×
444
    }
445
  }
446
  if (pVnode) vmReleaseVnode(pMgmt, pVnode);
156,761✔
447
  if (size < 0) {
156,753✔
448
    dTrace("vgId:%d, can't get size from queue since %s, qtype:%d", vgId, terrstr(), qtype);
24,891✔
449
    size = 0;
24,883✔
450
  }
451
  return size;
156,745✔
452
}
453

454
int32_t vmAllocQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
12,291✔
455
  int32_t         code = 0;
12,291✔
456
  SMultiWorkerCfg wcfg = {.max = 1, .name = "vnode-write", .fp = (FItems)vnodeProposeWriteMsg, .param = pVnode->pImpl};
12,291✔
457
  SMultiWorkerCfg scfg = {.max = 1, .name = "vnode-sync", .fp = (FItems)vmProcessSyncQueue, .param = pVnode};
12,291✔
458
  SMultiWorkerCfg sccfg = {.max = 1, .name = "vnode-sync-rd", .fp = (FItems)vmProcessSyncQueue, .param = pVnode};
12,291✔
459
  SMultiWorkerCfg acfg = {.max = 1, .name = "vnode-apply", .fp = (FItems)vnodeApplyWriteMsg, .param = pVnode->pImpl};
12,291✔
460
  code = tMultiWorkerInit(&pVnode->pWriteW, &wcfg);
12,291✔
461
  if (code) {
12,291!
462
    return code;
×
463
  }
464
  code = tMultiWorkerInit(&pVnode->pSyncW, &scfg);
12,291✔
465
  if (code) {
12,291!
466
    tMultiWorkerCleanup(&pVnode->pWriteW);
×
467
    return code;
×
468
  }
469
  code = tMultiWorkerInit(&pVnode->pSyncRdW, &sccfg);
12,291✔
470
  if (code) {
12,291!
471
    tMultiWorkerCleanup(&pVnode->pWriteW);
×
472
    tMultiWorkerCleanup(&pVnode->pSyncW);
×
473
    return code;
×
474
  }
475
  code = tMultiWorkerInit(&pVnode->pApplyW, &acfg);
12,291✔
476
  if (code) {
12,291!
477
    tMultiWorkerCleanup(&pVnode->pWriteW);
×
478
    tMultiWorkerCleanup(&pVnode->pSyncW);
×
479
    tMultiWorkerCleanup(&pVnode->pSyncRdW);
×
480
    return code;
×
481
  }
482

483
  pVnode->pQueryQ = tQueryAutoQWorkerAllocQueue(&pMgmt->queryPool, pVnode, (FItem)vmProcessQueryQueue);
12,291✔
484
  pVnode->pFetchQ = tWWorkerAllocQueue(&pMgmt->fetchPool, pVnode, (FItems)vmProcessFetchQueue);
12,291✔
485

486
  // init stream msg processing queue family
487
  pVnode->pStreamQ = tAutoQWorkerAllocQueue(&pMgmt->streamPool, pVnode, (FItem)vmProcessStreamQueue, 2);
12,291✔
488
  pVnode->pStreamCtrlQ = tWWorkerAllocQueue(&pMgmt->streamCtrlPool, pVnode, (FItems)vmProcessStreamCtrlQueue);
12,291✔
489
  pVnode->pStreamLongExecQ = tAutoQWorkerAllocQueue(&pMgmt->streamLongExecPool, pVnode, (FItem)vmProcessStreamLongExecQueue, 1);
12,291✔
490

491
  if (pVnode->pWriteW.queue == NULL || pVnode->pSyncW.queue == NULL || pVnode->pSyncRdW.queue == NULL ||
12,291!
492
      pVnode->pApplyW.queue == NULL || pVnode->pQueryQ == NULL || pVnode->pStreamQ == NULL || pVnode->pFetchQ == NULL
12,291!
493
      || pVnode->pStreamCtrlQ == NULL || pVnode->pStreamLongExecQ == NULL) {
12,291!
494
    return TSDB_CODE_OUT_OF_MEMORY;
×
495
  }
496

497
  dInfo("vgId:%d, write-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pWriteW.queue,
12,291!
498
        taosQueueGetThreadId(pVnode->pWriteW.queue));
499
  dInfo("vgId:%d, sync-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pSyncW.queue,
12,291!
500
        taosQueueGetThreadId(pVnode->pSyncW.queue));
501
  dInfo("vgId:%d, sync-rd-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pSyncRdW.queue,
12,291!
502
        taosQueueGetThreadId(pVnode->pSyncRdW.queue));
503
  dInfo("vgId:%d, apply-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pApplyW.queue,
12,291!
504
        taosQueueGetThreadId(pVnode->pApplyW.queue));
505
  dInfo("vgId:%d, query-queue:%p is alloced", pVnode->vgId, pVnode->pQueryQ);
12,291!
506
  dInfo("vgId:%d, fetch-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pFetchQ,
12,291!
507
        taosQueueGetThreadId(pVnode->pFetchQ));
508
  dInfo("vgId:%d, stream-queue:%p is alloced", pVnode->vgId, pVnode->pStreamQ);
12,291!
509
  dInfo("vgId:%d, stream-long-exec-queue:%p is alloced", pVnode->vgId, pVnode->pStreamLongExecQ);
12,291!
510
  dInfo("vgId:%d, stream-ctrl-queue:%p is alloced, thread:%08" PRId64, pVnode->vgId, pVnode->pStreamCtrlQ,
12,291!
511
        taosQueueGetThreadId(pVnode->pStreamCtrlQ));
512
  return 0;
12,291✔
513
}
514

515
void vmFreeQueue(SVnodeMgmt *pMgmt, SVnodeObj *pVnode) {
12,261✔
516
  tQueryAutoQWorkerFreeQueue(&pMgmt->queryPool, pVnode->pQueryQ);
12,261✔
517
  tAutoQWorkerFreeQueue(&pMgmt->streamPool, pVnode->pStreamQ);
12,262✔
518
  tAutoQWorkerFreeQueue(&pMgmt->streamLongExecPool, pVnode->pStreamLongExecQ);
12,262✔
519
  tWWorkerFreeQueue(&pMgmt->streamCtrlPool, pVnode->pStreamCtrlQ);
12,262✔
520
  tWWorkerFreeQueue(&pMgmt->fetchPool, pVnode->pFetchQ);
12,262✔
521
  pVnode->pQueryQ = NULL;
12,262✔
522
  pVnode->pFetchQ = NULL;
12,262✔
523

524
  pVnode->pStreamQ = NULL;
12,262✔
525
  pVnode->pStreamCtrlQ = NULL;
12,262✔
526
  pVnode->pStreamLongExecQ = NULL;
12,262✔
527

528
  dDebug("vgId:%d, queue is freed", pVnode->vgId);
12,262✔
529
}
12,262✔
530

531
int32_t vmStartWorker(SVnodeMgmt *pMgmt) {
2,230✔
532
  int32_t code = 0;
2,230✔
533

534
  SQueryAutoQWorkerPool *pQPool = &pMgmt->queryPool;
2,230✔
535
  pQPool->name = "vnode-query";
2,230✔
536
  pQPool->min = tsNumOfVnodeQueryThreads;
2,230✔
537
  pQPool->max = tsNumOfVnodeQueryThreads;
2,230✔
538
  if ((code = tQueryAutoQWorkerInit(pQPool)) != 0) return code;
2,230!
539

540
  tsNumOfQueryThreads += tsNumOfVnodeQueryThreads;
2,230✔
541

542
  SAutoQWorkerPool *pStreamPool = &pMgmt->streamPool;
2,230✔
543
  pStreamPool->name = "vnode-stream";
2,230✔
544
  pStreamPool->ratio = tsRatioOfVnodeStreamThreads;
2,230✔
545
  if ((code = tAutoQWorkerInit(pStreamPool)) != 0) return code;
2,230!
546

547
  SAutoQWorkerPool *pLongExecPool = &pMgmt->streamLongExecPool;
2,230✔
548
  pLongExecPool->name = "vnode-stream-long-exec";
2,230✔
549
  pLongExecPool->ratio = tsRatioOfVnodeStreamThreads/3;
2,230✔
550
  if ((code = tAutoQWorkerInit(pLongExecPool)) != 0) return code;
2,230!
551

552
  SWWorkerPool *pStreamCtrlPool = &pMgmt->streamCtrlPool;
2,230✔
553
  pStreamCtrlPool->name = "vnode-stream-ctrl";
2,230✔
554
  pStreamCtrlPool->max = 1;
2,230✔
555
  if ((code = tWWorkerInit(pStreamCtrlPool)) != 0) return code;
2,230!
556

557
  SWWorkerPool *pFPool = &pMgmt->fetchPool;
2,230✔
558
  pFPool->name = "vnode-fetch";
2,230✔
559
  pFPool->max = tsNumOfVnodeFetchThreads;
2,230✔
560
  if ((code = tWWorkerInit(pFPool)) != 0) return code;
2,230!
561

562
  SSingleWorkerCfg mgmtCfg = {
2,230✔
563
      .min = 1, .max = 1, .name = "vnode-mgmt", .fp = (FItem)vmProcessMgmtQueue, .param = pMgmt};
564

565
  if ((code = tSingleWorkerInit(&pMgmt->mgmtWorker, &mgmtCfg)) != 0) return code;
2,230!
566

567
  int32_t threadNum = 0;
2,230✔
568
  if (tsNumOfCores == 1) {
2,230!
569
    threadNum = 2;
×
570
  } else {
571
    threadNum = tsNumOfCores;
2,230✔
572
  }
573
  SSingleWorkerCfg multiMgmtCfg = {.min = threadNum,
2,230✔
574
                                   .max = threadNum,
575
                                   .name = "vnode-multi-mgmt",
576
                                   .fp = (FItem)vmProcessMultiMgmtQueue,
577
                                   .param = pMgmt};
578

579
  if ((code = tSingleWorkerInit(&pMgmt->mgmtMultiWorker, &multiMgmtCfg)) != 0) return code;
2,230!
580

581
  dDebug("vnode workers are initialized");
2,230✔
582
  return 0;
2,230✔
583
}
584

585
void vmStopWorker(SVnodeMgmt *pMgmt) {
2,229✔
586
  tQueryAutoQWorkerCleanup(&pMgmt->queryPool);
2,229✔
587
  tAutoQWorkerCleanup(&pMgmt->streamPool);
2,229✔
588
  tAutoQWorkerCleanup(&pMgmt->streamLongExecPool);
2,229✔
589
  tWWorkerCleanup(&pMgmt->streamCtrlPool);
2,229✔
590
  tWWorkerCleanup(&pMgmt->fetchPool);
2,229✔
591
  dDebug("vnode workers are closed");
2,229✔
592
}
2,229✔
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