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

taosdata / TDengine / #4127

20 May 2025 07:22AM UTC coverage: 62.384% (-0.6%) from 63.022%
#4127

push

travis-ci

web-flow
docs(datain): add topic meta options docs in tmq (#31147)

155581 of 318088 branches covered (48.91%)

Branch coverage included in aggregate %.

240701 of 317147 relevant lines covered (75.9%)

16334195.26 hits per line

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

65.93
/source/dnode/vnode/src/vnd/vnodeSync.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 "sync.h"
18
#include "tq.h"
19
#include "tqCommon.h"
20
#include "tsdb.h"
21
#include "vnd.h"
22

23
#define BATCH_ENABLE 0
24

25
static inline bool vnodeIsMsgWeak(tmsg_t type) { return false; }
10,694,283✔
26

27
static inline void vnodeWaitBlockMsg(SVnode *pVnode, const SRpcMsg *pMsg) {
2,653✔
28
  vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, wait block, type:%s sec:%d seq:%" PRId64, pVnode->config.vgId, pMsg,
2,653!
29
          TMSG_INFO(pMsg->msgType), pVnode->blockSec, pVnode->blockSeq);
30
  if (tsem_wait(&pVnode->syncSem) != 0) {
2,653!
31
    vError("vgId:%d, failed to wait sem", pVnode->config.vgId);
×
32
  }
33
}
2,653✔
34

35
static inline void vnodePostBlockMsg(SVnode *pVnode, const SRpcMsg *pMsg) {
1,805,832✔
36
  if (vnodeIsMsgBlock(pMsg->msgType)) {
1,805,832✔
37
    (void)taosThreadMutexLock(&pVnode->lock);
23,897✔
38
    if (pVnode->blocked) {
23,897✔
39
      vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, post block, type:%s sec:%d seq:%" PRId64, pVnode->config.vgId, pMsg,
2,628!
40
              TMSG_INFO(pMsg->msgType), pVnode->blockSec, pVnode->blockSeq);
41
      pVnode->blocked = false;
2,628✔
42
      pVnode->blockSec = 0;
2,628✔
43
      pVnode->blockSeq = 0;
2,628✔
44
      if (tsem_post(&pVnode->syncSem) != 0) {
2,628!
45
        vError("vgId:%d, failed to post sem", pVnode->config.vgId);
×
46
      }
47
    }
48
    (void)taosThreadMutexUnlock(&pVnode->lock);
23,897✔
49
  }
50
}
1,805,831✔
51

52
void vnodeRedirectRpcMsg(SVnode *pVnode, SRpcMsg *pMsg, int32_t code) {
42,955✔
53
  SEpSet newEpSet = {0};
42,955✔
54
  syncGetRetryEpSet(pVnode->sync, &newEpSet);
42,955✔
55

56
  vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is redirect since not leader, numOfEps:%d inUse:%d",
42,955!
57
          pVnode->config.vgId, pMsg, newEpSet.numOfEps, newEpSet.inUse);
58
  for (int32_t i = 0; i < newEpSet.numOfEps; ++i) {
170,885✔
59
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, redirect:%d ep:%s:%u", pVnode->config.vgId, pMsg, i,
127,930!
60
            newEpSet.eps[i].fqdn, newEpSet.eps[i].port);
61
  }
62
  pMsg->info.hasEpSet = 1;
42,955✔
63

64
  if (code == 0) code = TSDB_CODE_SYN_NOT_LEADER;
42,955!
65

66
  SRpcMsg rsp = {.code = code, .info = pMsg->info, .msgType = pMsg->msgType + 1};
42,955✔
67
  int32_t contLen = tSerializeSEpSet(NULL, 0, &newEpSet);
42,955✔
68

69
  rsp.pCont = rpcMallocCont(contLen);
42,955✔
70
  if (rsp.pCont == NULL) {
42,955!
71
    pMsg->code = TSDB_CODE_OUT_OF_MEMORY;
×
72
  } else {
73
    if (tSerializeSEpSet(rsp.pCont, contLen, &newEpSet) < 0) {
42,955!
74
      vError("vgId:%d, failed to serialize ep set", pVnode->config.vgId);
×
75
    }
76
    rsp.contLen = contLen;
42,955✔
77
  }
78

79
  tmsgSendRsp(&rsp);
42,955✔
80
}
42,955✔
81

82
static void inline vnodeHandleWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,445,632✔
83
  SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
10,445,632✔
84
  if (vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp) < 0) {
10,445,632!
85
    rsp.code = terrno;
×
86
    vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to apply right now since %s", pVnode->config.vgId, pMsg,
×
87
            terrstr());
88
  }
89
  if (rsp.info.handle != NULL) {
10,445,772✔
90
    tmsgSendRsp(&rsp);
10,417,610✔
91
  } else {
92
    if (rsp.pCont) {
28,162✔
93
      rpcFreeCont(rsp.pCont);
19,278✔
94
    }
95
  }
96
}
10,445,932✔
97

98
static void vnodeHandleProposeError(SVnode *pVnode, SRpcMsg *pMsg, int32_t code) {
104,677✔
99
  if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING) {
104,677✔
100
    vnodeRedirectRpcMsg(pVnode, pMsg, code);
36,366✔
101
  } else if (code == TSDB_CODE_MSG_PREPROCESSED) {
68,311✔
102
    SRpcMsg rsp = {.code = TSDB_CODE_SUCCESS, .info = pMsg->info};
67,089✔
103
    if (rsp.info.handle != NULL) {
67,089!
104
      tmsgSendRsp(&rsp);
67,143✔
105
    }
106
  } else {
107
    vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to propose since %s, code:0x%x", pVnode->config.vgId, pMsg,
1,222!
108
            tstrerror(code), code);
109
    SRpcMsg rsp = {.code = code, .info = pMsg->info};
1,243✔
110
    if (rsp.info.handle != NULL) {
1,243✔
111
      tmsgSendRsp(&rsp);
718✔
112
    }
113
  }
114
}
104,926✔
115

116
static int32_t inline vnodeProposeMsg(SVnode *pVnode, SRpcMsg *pMsg, bool isWeak) {
10,603,860✔
117
  int64_t seq = 0;
10,603,860✔
118

119
  (void)taosThreadMutexLock(&pVnode->lock);
10,603,860✔
120
  int32_t code = syncPropose(pVnode->sync, pMsg, isWeak, &seq);
10,604,177✔
121
  bool    wait = (code == 0 && vnodeIsMsgBlock(pMsg->msgType));
10,603,811✔
122
  if (wait) {
10,603,813✔
123
    if (pVnode->blocked) {
2,653!
124
      (void)taosThreadMutexUnlock(&pVnode->lock);
×
125
      return TSDB_CODE_INTERNAL_ERROR;
×
126
    }
127
    pVnode->blocked = true;
2,653✔
128
    pVnode->blockSec = taosGetTimestampSec();
2,653✔
129
    pVnode->blockSeq = seq;
2,653✔
130
  }
131
  (void)taosThreadMutexUnlock(&pVnode->lock);
10,603,813✔
132

133
  if (code > 0) {
10,604,169✔
134
    vnodeHandleWriteMsg(pVnode, pMsg);
10,446,023✔
135
  } else if (code < 0) {
158,146✔
136
    if (terrno != 0) code = terrno;
13,909!
137
    vnodeHandleProposeError(pVnode, pMsg, code);
13,909✔
138
  }
139

140
  if (wait) vnodeWaitBlockMsg(pVnode, pMsg);
10,604,039✔
141
  return code;
10,604,057✔
142
}
143

144
void vnodeProposeCommitOnNeed(SVnode *pVnode, bool atExit) {
10,682,429✔
145
  if (!vnodeShouldCommit(pVnode, atExit)) {
10,682,429✔
146
    return;
10,674,120✔
147
  }
148

149
  int32_t   contLen = sizeof(SMsgHead);
8,835✔
150
  SMsgHead *pHead = rpcMallocCont(contLen);
8,835✔
151
  pHead->contLen = contLen;
8,839✔
152
  pHead->vgId = pVnode->config.vgId;
8,839✔
153

154
  SRpcMsg rpcMsg = {0};
8,839✔
155
  rpcMsg.msgType = TDMT_VND_COMMIT;
8,839✔
156
  rpcMsg.contLen = contLen;
8,839✔
157
  rpcMsg.pCont = pHead;
8,839✔
158
  rpcMsg.info.noResp = 1;
8,839✔
159

160
  vInfo("vgId:%d, propose vnode commit", pVnode->config.vgId);
8,839✔
161
  bool isWeak = false;
8,840✔
162

163
  if (!atExit) {
8,840✔
164
    if (vnodeProposeMsg(pVnode, &rpcMsg, isWeak) < 0) {
494!
165
      vTrace("vgId:%d, failed to propose vnode commit since %s", pVnode->config.vgId, terrstr());
×
166
    }
167
    rpcFreeCont(rpcMsg.pCont);
494✔
168
    rpcMsg.pCont = NULL;
494✔
169
  } else {
170
    int32_t code = 0;
8,346✔
171
    if ((code = tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &rpcMsg)) < 0) {
8,346✔
172
      vError("vgId:%d, failed to put vnode commit to write_queue since %s", pVnode->config.vgId, tstrerror(code));
3,499✔
173
    }
174
  }
175
}
176

177
#if BATCH_ENABLE
178

179
static void inline vnodeProposeBatchMsg(SVnode *pVnode, SRpcMsg **pMsgArr, bool *pIsWeakArr, int32_t *arrSize) {
180
  if (*arrSize <= 0) return;
181
  SRpcMsg *pLastMsg = pMsgArr[*arrSize - 1];
182

183
  (void)taosThreadMutexLock(&pVnode->lock);
184
  int32_t code = syncProposeBatch(pVnode->sync, pMsgArr, pIsWeakArr, *arrSize);
185
  bool    wait = (code == 0 && vnodeIsBlockMsg(pLastMsg->msgType));
186
  if (wait) {
187
    pVnode->blocked = true;
188
  }
189
  (void)taosThreadMutexUnlock(&pVnode->lock);
190

191
  if (code > 0) {
192
    for (int32_t i = 0; i < *arrSize; ++i) {
193
      vnodeHandleWriteMsg(pVnode, pMsgArr[i]);
194
    }
195
  } else if (code < 0) {
196
    if (terrno != 0) code = terrno;
197
    for (int32_t i = 0; i < *arrSize; ++i) {
198
      vnodeHandleProposeError(pVnode, pMsgArr[i], code);
199
    }
200
  }
201

202
  if (wait) vnodeWaitBlockMsg(pVnode, pLastMsg);
203
  pLastMsg = NULL;
204

205
  for (int32_t i = 0; i < *arrSize; ++i) {
206
    SRpcMsg        *pMsg = pMsgArr[i];
207
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x", pVnode->config.vgId, pMsg, code);
208
    rpcFreeCont(pMsg->pCont);
209
    taosFreeQitem(pMsg);
210
  }
211

212
  *arrSize = 0;
213
}
214

215
void vnodeProposeWriteMsg(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
216
  SVnode   *pVnode = pInfo->ahandle;
217
  int32_t   vgId = pVnode->config.vgId;
218
  int32_t   code = 0;
219
  SRpcMsg  *pMsg = NULL;
220
  int32_t   arrayPos = 0;
221
  SRpcMsg **pMsgArr = taosMemoryCalloc(numOfMsgs, sizeof(SRpcMsg *));
222
  bool     *pIsWeakArr = taosMemoryCalloc(numOfMsgs, sizeof(bool));
223
  vTrace("vgId:%d, get %d msgs from vnode-write queue", vgId, numOfMsgs);
224

225
  for (int32_t msg = 0; msg < numOfMsgs; msg++) {
226
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
227
    bool isWeak = vnodeIsMsgWeak(pMsg->msgType);
228
    bool isBlock = vnodeIsMsgBlock(pMsg->msgType);
229

230
    vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-write queue, weak:%d block:%d msg:%d:%d pos:%d, handle:%p", vgId, pMsg,
231
            isWeak, isBlock, msg, numOfMsgs, arrayPos, pMsg->info.handle);
232

233
    if (!pVnode->restored) {
234
      vGWarn(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to process since restore not finished, type:%s", vgId, pMsg,
235
             TMSG_INFO(pMsg->msgType));
236
      terrno = TSDB_CODE_SYN_RESTORING;
237
      vnodeHandleProposeError(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
238
      rpcFreeCont(pMsg->pCont);
239
      taosFreeQitem(pMsg);
240
      continue;
241
    }
242

243
    if (pMsgArr == NULL || pIsWeakArr == NULL) {
244
      vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to process since out of memory, type:%s", vgId, pMsg, TMSG_INFO(pMsg->msgType));
245
      terrno = TSDB_CODE_OUT_OF_MEMORY;
246
      vnodeHandleProposeError(pVnode, pMsg, terrno);
247
      rpcFreeCont(pMsg->pCont);
248
      taosFreeQitem(pMsg);
249
      continue;
250
    }
251

252
    bool atExit = false;
253
    vnodeProposeCommitOnNeed(pVnode, atExit);
254

255
    code = vnodePreProcessWriteMsg(pVnode, pMsg);
256
    if (code != 0) {
257
      vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to pre-process since %s", vgId, pMsg, terrstr());
258
      rpcFreeCont(pMsg->pCont);
259
      taosFreeQitem(pMsg);
260
      continue;
261
    }
262

263
    if (isBlock) {
264
      vnodeProposeBatchMsg(pVnode, pMsgArr, pIsWeakArr, &arrayPos);
265
    }
266

267
    pMsgArr[arrayPos] = pMsg;
268
    pIsWeakArr[arrayPos] = isWeak;
269
    arrayPos++;
270

271
    if (isBlock || msg == numOfMsgs - 1) {
272
      vnodeProposeBatchMsg(pVnode, pMsgArr, pIsWeakArr, &arrayPos);
273
    }
274
  }
275

276
  taosMemoryFree(pMsgArr);
277
  taosMemoryFree(pIsWeakArr);
278
}
279

280
#else
281

282
void vnodeProposeWriteMsg(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
8,907,245✔
283
  SVnode  *pVnode = pInfo->ahandle;
8,907,245✔
284
  int32_t  vgId = pVnode->config.vgId;
8,907,245✔
285
  int32_t  code = 0;
8,907,245✔
286
  SRpcMsg *pMsg = NULL;
8,907,245✔
287
  vTrace("vgId:%d, get %d msgs from vnode-write queue", vgId, numOfMsgs);
8,907,245✔
288

289
  for (int32_t msg = 0; msg < numOfMsgs; msg++) {
19,602,345✔
290
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
10,694,434!
291
    bool isWeak = vnodeIsMsgWeak(pMsg->msgType);
10,694,610✔
292

293
    vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-write queue, weak:%d block:%d msg:%d:%d, handle:%p",
10,694,350!
294
            vgId, pMsg, isWeak, vnodeIsMsgBlock(pMsg->msgType), msg, numOfMsgs, pMsg->info.handle);
295

296
    if (!pVnode->restored) {
10,694,419✔
297
      vGWarn(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to process since restore not finished, type:%s", vgId, pMsg,
23,293!
298
             TMSG_INFO(pMsg->msgType));
299
      vnodeHandleProposeError(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
23,293✔
300
      rpcFreeCont(pMsg->pCont);
23,293✔
301
      taosFreeQitem(pMsg);
23,293✔
302
      continue;
23,293✔
303
    }
304

305
    bool atExit = false;
10,671,126✔
306
    vnodeProposeCommitOnNeed(pVnode, atExit);
10,671,126✔
307

308
    code = vnodePreProcessWriteMsg(pVnode, pMsg);
10,671,493✔
309
    if (code != 0) {
10,670,921✔
310
      if (code != TSDB_CODE_MSG_PREPROCESSED) {
67,518✔
311
        vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to pre-process since %s", vgId, pMsg, tstrerror(code));
517!
312
      }
313
      vnodeHandleProposeError(pVnode, pMsg, code);
67,518✔
314
      rpcFreeCont(pMsg->pCont);
67,686✔
315
      taosFreeQitem(pMsg);
67,778✔
316
      continue;
67,805✔
317
    }
318

319
    code = vnodeProposeMsg(pVnode, pMsg, isWeak);
10,603,403✔
320

321
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x", vgId, pMsg, code);
10,603,511!
322
    rpcFreeCont(pMsg->pCont);
10,603,511✔
323
    taosFreeQitem(pMsg);
10,603,653✔
324
  }
325
}
8,907,911✔
326

327
#endif
328

329
void vnodeApplyWriteMsg(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
1,297,882✔
330
  SVnode  *pVnode = pInfo->ahandle;
1,297,882✔
331
  int32_t  vgId = pVnode->config.vgId;
1,297,882✔
332
  int32_t  code = 0;
1,297,882✔
333
  SRpcMsg *pMsg = NULL;
1,297,882✔
334

335
  for (int32_t i = 0; i < numOfMsgs; ++i) {
3,103,714✔
336
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
1,805,829!
337

338
    if (vnodeIsMsgBlock(pMsg->msgType)) {
1,805,825✔
339
      vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-apply queue, type:%s handle:%p index:%" PRId64
23,894!
340
              ", blocking msg obtained sec:%d seq:%" PRId64,
341
              vgId, pMsg, TMSG_INFO(pMsg->msgType), pMsg->info.handle, pMsg->info.conn.applyIndex, pVnode->blockSec,
342
              pVnode->blockSeq);
343
    } else {
344
      vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-apply queue, type:%s handle:%p index:%" PRId64, vgId, pMsg,
1,781,931!
345
              TMSG_INFO(pMsg->msgType), pMsg->info.handle, pMsg->info.conn.applyIndex);
346
    }
347

348
    SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
1,805,837✔
349
    if (rsp.code == 0) {
1,805,837✔
350
      if (vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp) < 0) {
1,805,833!
351
        rsp.code = terrno;
×
352
        vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to apply since %s, index:%" PRId64, vgId, pMsg, terrstr(),
×
353
                pMsg->info.conn.applyIndex);
354
      }
355
    }
356

357
    vnodePostBlockMsg(pVnode, pMsg);
1,805,836✔
358
    if (rsp.info.handle != NULL) {
1,805,831✔
359
      tmsgSendRsp(&rsp);
144,176✔
360
    } else {
361
      if (rsp.pCont) {
1,661,655✔
362
        rpcFreeCont(rsp.pCont);
1,603,133✔
363
      }
364
    }
365

366
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x index:%" PRId64, vgId, pMsg, rsp.code,
1,805,835!
367
            pMsg->info.conn.applyIndex);
368
    rpcFreeCont(pMsg->pCont);
1,805,835✔
369
    taosFreeQitem(pMsg);
1,805,832✔
370
  }
371
}
1,297,885✔
372

373
int32_t vnodeProcessSyncMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) {
3,032,008✔
374
  vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-sync queue, type:%s", pVnode->config.vgId, pMsg, TMSG_INFO(pMsg->msgType));
3,032,008!
375

376
  int32_t code = syncProcessMsg(pVnode->sync, pMsg);
3,032,016✔
377
  if (code != 0) {
3,032,162✔
378
    vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to process since %s, type:%s", pVnode->config.vgId, pMsg, tstrerror(code),
79!
379
            TMSG_INFO(pMsg->msgType));
380
  }
381

382
  return code;
3,032,132✔
383
}
384

385
static int32_t vnodeSyncEqCtrlMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) {
×
386
  if (pMsg == NULL || pMsg->pCont == NULL) {
×
387
    return TSDB_CODE_INVALID_PARA;
×
388
  }
389

390
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
×
391
    rpcFreeCont(pMsg->pCont);
×
392
    pMsg->pCont = NULL;
×
393
    return TSDB_CODE_INVALID_PARA;
×
394
  }
395

396
  int32_t code = tmsgPutToQueue(msgcb, SYNC_RD_QUEUE, pMsg);
×
397
  if (code != 0) {
×
398
    rpcFreeCont(pMsg->pCont);
×
399
    pMsg->pCont = NULL;
×
400
  }
401
  return code;
×
402
}
403

404
static int32_t vnodeSyncEqMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) {
327,436✔
405
  if (pMsg == NULL || pMsg->pCont == NULL) {
327,436!
406
    return TSDB_CODE_INVALID_PARA;
×
407
  }
408

409
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
327,437!
410
    rpcFreeCont(pMsg->pCont);
×
411
    pMsg->pCont = NULL;
×
412
    return TSDB_CODE_INVALID_PARA;
×
413
  }
414

415
  int32_t code = tmsgPutToQueue(msgcb, SYNC_QUEUE, pMsg);
327,439✔
416
  if (code != 0) {
327,437✔
417
    rpcFreeCont(pMsg->pCont);
1,619✔
418
    pMsg->pCont = NULL;
1,619✔
419
  }
420
  return code;
327,437✔
421
}
422

423
static int32_t vnodeSyncSendMsg(const SEpSet *pEpSet, SRpcMsg *pMsg) {
2,731,687✔
424
  int32_t code = tmsgSendSyncReq(pEpSet, pMsg);
2,731,687✔
425
  if (code != 0) {
2,731,706!
426
    rpcFreeCont(pMsg->pCont);
×
427
    pMsg->pCont = NULL;
×
428
  }
429
  return code;
2,731,706✔
430
}
431

432
static int32_t vnodeSyncGetSnapshotInfo(const SSyncFSM *pFsm, SSnapshot *pSnapshot) {
472,782✔
433
  return vnodeGetSnapshot(pFsm->data, pSnapshot);
472,782✔
434
}
435

436
static int32_t vnodeSyncApplyMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, const SFsmCbMeta *pMeta) {
1,805,851✔
437
  SVnode *pVnode = pFsm->data;
1,805,851✔
438
  pMsg->info.conn.applyIndex = pMeta->index;
1,805,851✔
439
  pMsg->info.conn.applyTerm = pMeta->term;
1,805,851✔
440

441
  vGDebug(&pMsg->info.traceId,
1,805,851!
442
          "vgId:%d, index:%" PRId64 ", execute commit cb, fsm:%p, term:%" PRIu64 ", msg-index:%" PRId64
443
          ", weak:%d, code:%d, state:%d %s, type:%s code:0x%x",
444
          pVnode->config.vgId, pMeta->index, pFsm, pMeta->term, pMsg->info.conn.applyIndex, pMeta->isWeak, pMeta->code,
445
          pMeta->state, syncStr(pMeta->state), TMSG_INFO(pMsg->msgType), pMsg->code);
446

447
  int32_t code = tmsgPutToQueue(&pVnode->msgCb, APPLY_QUEUE, pMsg);
1,805,853✔
448
  if (code < 0) {
1,805,855✔
449
    if (code == TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE) {
18!
450
      pVnode->applyQueueErrorCount++;
×
451
      if (pVnode->applyQueueErrorCount == APPLY_QUEUE_ERROR_THRESHOLD) {
×
452
        pVnode->applyQueueErrorCount = 0;
×
453
        vWarn("vgId:%d, failed to put into apply_queue since %s", pVnode->config.vgId, tstrerror(code));
×
454
      }
455
    } else {
456
      vError("vgId:%d, failed to put into apply_queue since %s", pVnode->config.vgId, tstrerror(code));
18!
457
    }
458
  }
459
  return code;
1,805,855✔
460
}
461

462
static int32_t vnodeSyncCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
1,805,852✔
463
  if (pMsg->code == 0) {
1,805,852!
464
    return vnodeSyncApplyMsg(pFsm, pMsg, pMeta);
1,805,852✔
465
  }
466

467
  SVnode *pVnode = pFsm->data;
×
468
  vnodePostBlockMsg(pVnode, pMsg);
×
469

470
  SRpcMsg rsp = {
×
471
      .code = pMsg->code,
×
472
      .info = pMsg->info,
473
  };
474

475
  if (rsp.info.handle != NULL) {
×
476
    tmsgSendRsp(&rsp);
×
477
  }
478

479
  vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x index:%" PRId64, TD_VID(pVnode), pMsg, rsp.code,
×
480
          pMeta->index);
481
  rpcFreeCont(pMsg->pCont);
×
482
  pMsg->pCont = NULL;
×
483
  return 0;
×
484
}
485

486
static int32_t vnodeSyncPreCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
×
487
  if (pMeta->isWeak == 1) {
×
488
    return vnodeSyncApplyMsg(pFsm, pMsg, pMeta);
×
489
  }
490
  return 0;
×
491
}
492

493
static SyncIndex vnodeSyncAppliedIndex(const SSyncFSM *pFSM) {
10,728,133✔
494
  SVnode *pVnode = pFSM->data;
10,728,133✔
495
  return atomic_load_64(&pVnode->state.applied);
10,728,133✔
496
}
497

498
static void vnodeSyncRollBackMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
×
499
  SVnode *pVnode = pFsm->data;
×
500
  vGDebug(&pMsg->info.traceId,
×
501
          "vgId:%d, rollback-cb is excuted, fsm:%p, index:%" PRId64 ", weak:%d, code:%d, state:%d %s, type:%s",
502
          pVnode->config.vgId, pFsm, pMeta->index, pMeta->isWeak, pMeta->code, pMeta->state, syncStr(pMeta->state),
503
          TMSG_INFO(pMsg->msgType));
504
}
×
505

506
static int32_t vnodeSnapshotStartRead(const SSyncFSM *pFsm, void *pParam, void **ppReader) {
58✔
507
  SVnode *pVnode = pFsm->data;
58✔
508
  return vnodeSnapReaderOpen(pVnode, (SSnapshotParam *)pParam, (SVSnapReader **)ppReader);
58✔
509
}
510

511
static void vnodeSnapshotStopRead(const SSyncFSM *pFsm, void *pReader) {
58✔
512
  SVnode *pVnode = pFsm->data;
58✔
513
  vnodeSnapReaderClose(pReader);
58✔
514
}
58✔
515

516
static int32_t vnodeSnapshotDoRead(const SSyncFSM *pFsm, void *pReader, void **ppBuf, int32_t *len) {
5,804✔
517
  SVnode *pVnode = pFsm->data;
5,804✔
518
  return vnodeSnapRead(pReader, (uint8_t **)ppBuf, len);
5,804✔
519
}
520

521
static int32_t vnodeSnapshotStartWrite(const SSyncFSM *pFsm, void *pParam, void **ppWriter) {
56✔
522
  SVnode *pVnode = pFsm->data;
56✔
523

524
  do {
×
525
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
56✔
526
    if (itemSize == 0) {
56!
527
      vInfo("vgId:%d, start write vnode snapshot since apply queue is empty", pVnode->config.vgId);
56!
528
      break;
56✔
529
    } else {
530
      vInfo("vgId:%d, write vnode snapshot later since %d items in apply queue", pVnode->config.vgId, itemSize);
×
531
      taosMsleep(10);
×
532
    }
533
  } while (true);
534

535
  return vnodeSnapWriterOpen(pVnode, (SSnapshotParam *)pParam, (SVSnapWriter **)ppWriter);
56✔
536
}
537

538
static int32_t vnodeSnapshotStopWrite(const SSyncFSM *pFsm, void *pWriter, bool isApply, SSnapshot *pSnapshot) {
56✔
539
  SVnode *pVnode = pFsm->data;
56✔
540
  vInfo("vgId:%d, stop write vnode snapshot, apply:%d, index:%" PRId64 " term:%" PRIu64 " config:%" PRId64,
56!
541
        pVnode->config.vgId, isApply, pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm, pSnapshot->lastConfigIndex);
542

543
  int32_t code = vnodeSnapWriterClose(pWriter, !isApply, pSnapshot);
56✔
544
  if (code != 0) {
56!
545
    vError("vgId:%d, failed to finish applying vnode snapshot since %s, code:0x%x", pVnode->config.vgId, terrstr(),
×
546
           code);
547
  }
548
  return code;
56✔
549
}
550

551
static int32_t vnodeSnapshotDoWrite(const SSyncFSM *pFsm, void *pWriter, void *pBuf, int32_t len) {
5,690✔
552
  SVnode *pVnode = pFsm->data;
5,690✔
553
  vDebug("vgId:%d, continue write vnode snapshot, blockLen:%d", pVnode->config.vgId, len);
5,690!
554
  int32_t code = vnodeSnapWrite(pWriter, pBuf, len);
5,690✔
555
  vDebug("vgId:%d, continue write vnode snapshot finished, blockLen:%d", pVnode->config.vgId, len);
5,690!
556
  return code;
5,690✔
557
}
558

559
static void vnodeRestoreFinish(const SSyncFSM *pFsm, const SyncIndex commitIdx) {
14,414✔
560
  SVnode   *pVnode = pFsm->data;
14,414✔
561
  int32_t   vgId = pVnode->config.vgId;
14,414✔
562
  SyncIndex appliedIdx = -1;
14,414✔
563

564
  do {
565
    appliedIdx = vnodeSyncAppliedIndex(pFsm);
34,054✔
566
    if (appliedIdx > commitIdx) {
34,246✔
567
      vError("vgId:%d, restore failed since applied-index:%" PRId64 " is larger than commit-index:%" PRId64, vgId,
27!
568
             appliedIdx, commitIdx);
569
      break;
×
570
    }
571
    if (appliedIdx == commitIdx) {
34,219✔
572
      vInfo("vgId:%d, no items to be applied, restore finish", pVnode->config.vgId);
14,415✔
573
      break;
14,417✔
574
    } else {
575
      if (appliedIdx % 10 == 0) {
19,804✔
576
        vInfo("vgId:%d, restore not finish since %" PRId64 " items to be applied. commit-index:%" PRId64
1,012!
577
              ", applied-index:%" PRId64,
578
              vgId, commitIdx - appliedIdx, commitIdx, appliedIdx);
579
      } else {
580
        vDebug("vgId:%d, restore not finish since %" PRId64 " items to be applied. commit-index:%" PRId64
18,792✔
581
               ", applied-index:%" PRId64,
582
               vgId, commitIdx - appliedIdx, commitIdx, appliedIdx);
583
      }
584
      taosMsleep(10);
19,816✔
585
    }
586
  } while (true);
587

588
  walApplyVer(pVnode->pWal, commitIdx);
14,417✔
589
  pVnode->restored = true;
14,417✔
590

591
#ifdef USE_STREAM
592
  SStreamMeta *pMeta = pVnode->pTq->pStreamMeta;
14,417✔
593
  streamMetaWLock(pMeta);
14,417✔
594

595
  if (pMeta->startInfo.tasksWillRestart) {
14,417✔
596
    vInfo("vgId:%d, sync restore finished, stream tasks will be launched by other thread", vgId);
1,120!
597
    streamMetaWUnLock(pMeta);
1,120✔
598
    return;
1,120✔
599
  }
600

601
  if (vnodeIsRoleLeader(pVnode)) {
13,297✔
602
    // start to restore all stream tasks
603
    if (tsDisableStream) {
10,364!
604
      vInfo("vgId:%d, sync restore finished, not launch stream tasks, since stream tasks are disabled", vgId);
×
605
    } else {
606
      vInfo("vgId:%d sync restore finished, start to launch stream task(s)", vgId);
10,364✔
607
      if (pMeta->startInfo.startAllTasks == 1) {
10,364!
608
        pMeta->startInfo.restartCount += 1;
×
609
        vDebug("vgId:%d in start tasks procedure, inc restartCounter by 1, remaining restart:%d", vgId,
×
610
               pMeta->startInfo.restartCount);
611
      } else {
612
        pMeta->startInfo.startAllTasks = 1;
10,364✔
613
        streamMetaWUnLock(pMeta);
10,364✔
614

615
        tqInfo("vgId:%d stream task already loaded, start them", vgId);
10,364!
616
        int32_t code = streamTaskSchedTask(&pVnode->msgCb, TD_VID(pVnode), 0, 0, STREAM_EXEC_T_START_ALL_TASKS, false);
10,364✔
617
        if (code != 0) {
10,364!
618
          tqError("vgId:%d failed to sched stream task, code:%s", vgId, tstrerror(code));
×
619
        }
620
        return;
10,364✔
621
      }
622
    }
623
  } else {
624
    vInfo("vgId:%d, sync restore finished, not launch stream tasks since not leader", vgId);
2,933!
625
  }
626

627
  streamMetaWUnLock(pMeta);
2,933✔
628
#endif
629
}
630

631
static void vnodeBecomeFollower(const SSyncFSM *pFsm) {
5,820✔
632
  SVnode *pVnode = pFsm->data;
5,820✔
633
  vInfo("vgId:%d, become follower", pVnode->config.vgId);
5,820!
634

635
  (void)taosThreadMutexLock(&pVnode->lock);
5,820✔
636
  if (pVnode->blocked) {
5,820!
637
    pVnode->blocked = false;
×
638
    vDebug("vgId:%d, become follower and post block", pVnode->config.vgId);
×
639
    if (tsem_post(&pVnode->syncSem) != 0) {
×
640
      vError("vgId:%d, failed to post sync semaphore", pVnode->config.vgId);
×
641
    }
642
  }
643
  (void)taosThreadMutexUnlock(&pVnode->lock);
5,820✔
644

645
#ifdef USE_TQ
646
  if (pVnode->pTq) {
5,820!
647
    tqUpdateNodeStage(pVnode->pTq, false);
5,820✔
648
    if (tqStopStreamAllTasksAsync(pVnode->pTq->pStreamMeta, &pVnode->msgCb) != 0) {
5,820!
649
      vError("vgId:%d, failed to stop stream tasks", pVnode->config.vgId);
×
650
    }
651
  }
652
#endif
653
}
5,820✔
654

655
static void vnodeBecomeLearner(const SSyncFSM *pFsm) {
203✔
656
  SVnode *pVnode = pFsm->data;
203✔
657
  vInfo("vgId:%d, become learner", pVnode->config.vgId);
203!
658

659
  (void)taosThreadMutexLock(&pVnode->lock);
203✔
660
  if (pVnode->blocked) {
203!
661
    pVnode->blocked = false;
×
662
    vDebug("vgId:%d, become learner and post block", pVnode->config.vgId);
×
663
    if (tsem_post(&pVnode->syncSem) != 0) {
×
664
      vError("vgId:%d, failed to post sync semaphore", pVnode->config.vgId);
×
665
    }
666
  }
667
  (void)taosThreadMutexUnlock(&pVnode->lock);
203✔
668
}
203✔
669

670
static void vnodeBecomeLeader(const SSyncFSM *pFsm) {
11,486✔
671
  SVnode *pVnode = pFsm->data;
11,486✔
672
  vDebug("vgId:%d, become leader", pVnode->config.vgId);
11,486✔
673
#ifdef USE_TQ
674
  if (pVnode->pTq) {
11,486!
675
    tqUpdateNodeStage(pVnode->pTq, true);
11,486✔
676
  }
677
#endif
678
}
11,486✔
679

680
static void vnodeBecomeAssignedLeader(const SSyncFSM *pFsm) {
×
681
  SVnode *pVnode = pFsm->data;
×
682
  vDebug("vgId:%d, become assigned leader", pVnode->config.vgId);
×
683
#ifdef USE_TQ
684
  if (pVnode->pTq) {
×
685
    tqUpdateNodeStage(pVnode->pTq, true);
×
686
  }
687
#endif
688
}
×
689

690
static bool vnodeApplyQueueEmpty(const SSyncFSM *pFsm) {
×
691
  SVnode *pVnode = pFsm->data;
×
692

693
  if (pVnode != NULL && pVnode->msgCb.qsizeFp != NULL) {
×
694
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
×
695
    return (itemSize == 0);
×
696
  } else {
697
    return true;
×
698
  }
699
}
700

701
static int32_t vnodeApplyQueueItems(const SSyncFSM *pFsm) {
92,562✔
702
  SVnode *pVnode = pFsm->data;
92,562✔
703

704
  if (pVnode != NULL && pVnode->msgCb.qsizeFp != NULL) {
92,562!
705
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
92,571✔
706
    return itemSize;
92,566✔
707
  } else {
708
    return TSDB_CODE_INVALID_PARA;
×
709
  }
710
}
711

712
static SSyncFSM *vnodeSyncMakeFsm(SVnode *pVnode) {
13,933✔
713
  SSyncFSM *pFsm = taosMemoryCalloc(1, sizeof(SSyncFSM));
13,933!
714
  if (pFsm == NULL) {
13,933!
715
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
716
    return NULL;
×
717
  }
718
  pFsm->data = pVnode;
13,933✔
719
  pFsm->FpCommitCb = vnodeSyncCommitMsg;
13,933✔
720
  pFsm->FpAppliedIndexCb = vnodeSyncAppliedIndex;
13,933✔
721
  pFsm->FpPreCommitCb = vnodeSyncPreCommitMsg;
13,933✔
722
  pFsm->FpRollBackCb = vnodeSyncRollBackMsg;
13,933✔
723
  pFsm->FpGetSnapshot = NULL;
13,933✔
724
  pFsm->FpGetSnapshotInfo = vnodeSyncGetSnapshotInfo;
13,933✔
725
  pFsm->FpRestoreFinishCb = vnodeRestoreFinish;
13,933✔
726
  pFsm->FpAfterRestoredCb = NULL;
13,933✔
727
  pFsm->FpLeaderTransferCb = NULL;
13,933✔
728
  pFsm->FpApplyQueueEmptyCb = vnodeApplyQueueEmpty;
13,933✔
729
  pFsm->FpApplyQueueItems = vnodeApplyQueueItems;
13,933✔
730
  pFsm->FpBecomeLeaderCb = vnodeBecomeLeader;
13,933✔
731
  pFsm->FpBecomeAssignedLeaderCb = vnodeBecomeAssignedLeader;
13,933✔
732
  pFsm->FpBecomeFollowerCb = vnodeBecomeFollower;
13,933✔
733
  pFsm->FpBecomeLearnerCb = vnodeBecomeLearner;
13,933✔
734
  pFsm->FpReConfigCb = NULL;
13,933✔
735
  pFsm->FpSnapshotStartRead = vnodeSnapshotStartRead;
13,933✔
736
  pFsm->FpSnapshotStopRead = vnodeSnapshotStopRead;
13,933✔
737
  pFsm->FpSnapshotDoRead = vnodeSnapshotDoRead;
13,933✔
738
  pFsm->FpSnapshotStartWrite = vnodeSnapshotStartWrite;
13,933✔
739
  pFsm->FpSnapshotStopWrite = vnodeSnapshotStopWrite;
13,933✔
740
  pFsm->FpSnapshotDoWrite = vnodeSnapshotDoWrite;
13,933✔
741

742
  return pFsm;
13,933✔
743
}
744

745
int32_t vnodeSyncOpen(SVnode *pVnode, char *path, int32_t vnodeVersion) {
13,933✔
746
  SSyncInfo syncInfo = {
13,933✔
747
      .snapshotStrategy = SYNC_STRATEGY_WAL_FIRST,
748
      .batchSize = 1,
749
      .vgId = pVnode->config.vgId,
13,933✔
750
      .syncCfg = pVnode->config.syncCfg,
751
      .pWal = pVnode->pWal,
13,933✔
752
      .msgcb = &pVnode->msgCb,
13,933✔
753
      .syncSendMSg = vnodeSyncSendMsg,
754
      .syncEqMsg = vnodeSyncEqMsg,
755
      .syncEqCtrlMsg = vnodeSyncEqCtrlMsg,
756
      .pingMs = 5000,
757
      .electMs = 4000,
758
      .heartbeatMs = 700,
759
  };
760

761
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", path, TD_DIRSEP);
13,933✔
762
  syncInfo.pFsm = vnodeSyncMakeFsm(pVnode);
13,933✔
763

764
  SSyncCfg *pCfg = &syncInfo.syncCfg;
13,933✔
765
  vInfo("vgId:%d, start to open sync, replica:%d selfIndex:%d", pVnode->config.vgId, pCfg->replicaNum, pCfg->myIndex);
13,933✔
766
  for (int32_t i = 0; i < pCfg->totalReplicaNum; ++i) {
35,663✔
767
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
21,730✔
768
    vInfo("vgId:%d, index:%d ep:%s:%u dnode:%d cluster:%" PRId64, pVnode->config.vgId, i, pNode->nodeFqdn,
21,730✔
769
          pNode->nodePort, pNode->nodeId, pNode->clusterId);
770
  }
771

772
  pVnode->sync = syncOpen(&syncInfo, vnodeVersion);
13,933✔
773
  if (pVnode->sync <= 0) {
13,933!
774
    vError("vgId:%d, failed to open sync since %s", pVnode->config.vgId, terrstr());
×
775
    return terrno;
×
776
  }
777

778
  return 0;
13,933✔
779
}
780

781
int32_t vnodeSyncStart(SVnode *pVnode) {
13,933✔
782
  vInfo("vgId:%d, start sync", pVnode->config.vgId);
13,933✔
783
  int32_t code = syncStart(pVnode->sync);
13,933✔
784
  if (code) {
13,933!
785
    vError("vgId:%d, failed to start sync subsystem since %s", pVnode->config.vgId, tstrerror(code));
×
786
    return code;
×
787
  }
788
  return 0;
13,933✔
789
}
790

791
void vnodeSyncPreClose(SVnode *pVnode) {
13,932✔
792
  vInfo("vgId:%d, sync pre close", pVnode->config.vgId);
13,932✔
793
  int32_t code = syncLeaderTransfer(pVnode->sync);
13,933✔
794
  if (code) {
13,933✔
795
    vError("vgId:%d, failed to transfer leader since %s", pVnode->config.vgId, tstrerror(code));
1,093!
796
  }
797
  syncPreStop(pVnode->sync);
13,933✔
798

799
  (void)taosThreadMutexLock(&pVnode->lock);
13,933✔
800
  if (pVnode->blocked) {
13,933✔
801
    vInfo("vgId:%d, post block after close sync", pVnode->config.vgId);
25!
802
    pVnode->blocked = false;
25✔
803
    if (tsem_post(&pVnode->syncSem) != 0) {
25!
804
      vError("vgId:%d, failed to post block", pVnode->config.vgId);
×
805
    }
806
  }
807
  (void)taosThreadMutexUnlock(&pVnode->lock);
13,933✔
808
}
13,933✔
809

810
void vnodeSyncPostClose(SVnode *pVnode) {
13,933✔
811
  vInfo("vgId:%d, sync post close", pVnode->config.vgId);
13,933✔
812
  syncPostStop(pVnode->sync);
13,933✔
813
}
13,933✔
814

815
void vnodeSyncClose(SVnode *pVnode) {
13,932✔
816
  vInfo("vgId:%d, close sync", pVnode->config.vgId);
13,932✔
817
  syncStop(pVnode->sync);
13,933✔
818
}
13,932✔
819

820
void vnodeSyncCheckTimeout(SVnode *pVnode) {
22,212✔
821
  vTrace("vgId:%d, check sync timeout msg", pVnode->config.vgId);
22,212✔
822
  (void)taosThreadMutexLock(&pVnode->lock);
22,212✔
823
  if (pVnode->blocked) {
22,212✔
824
    int32_t curSec = taosGetTimestampSec();
5✔
825
    int32_t delta = curSec - pVnode->blockSec;
5✔
826
    if (delta > VNODE_TIMEOUT_SEC) {
5!
827
      vError("vgId:%d, failed to propose since timeout and post block, start:%d cur:%d delta:%d seq:%" PRId64,
×
828
             pVnode->config.vgId, pVnode->blockSec, curSec, delta, pVnode->blockSeq);
829
      if (syncSendTimeoutRsp(pVnode->sync, pVnode->blockSeq) != 0) {
×
830
#if 0
831
        SRpcMsg rpcMsg = {.code = TSDB_CODE_SYN_TIMEOUT, .info = pVnode->blockInfo};
832
        vError("send timeout response since its applyed, seq:%" PRId64 " handle:%p ahandle:%p", pVnode->blockSeq,
833
              rpcMsg.info.handle, rpcMsg.info.ahandle);
834
        rpcSendResponse(&rpcMsg);
835
#endif
836
      }
837
      pVnode->blocked = false;
×
838
      pVnode->blockSec = 0;
×
839
      pVnode->blockSeq = 0;
×
840
      if (tsem_post(&pVnode->syncSem) != 0) {
×
841
        vError("vgId:%d, failed to post block", pVnode->config.vgId);
×
842
      }
843
    }
844
  }
845
  (void)taosThreadMutexUnlock(&pVnode->lock);
22,212✔
846
}
22,212✔
847

848
bool vnodeIsRoleLeader(SVnode *pVnode) {
135,435✔
849
  SSyncState state = syncGetState(pVnode->sync);
135,435✔
850
  return state.state == TAOS_SYNC_STATE_LEADER;
135,487✔
851
}
852

853
bool vnodeIsLeader(SVnode *pVnode) {
20,303✔
854
  terrno = 0;
20,303✔
855
  SSyncState state = syncGetState(pVnode->sync);
20,303✔
856

857
  if (terrno != 0) {
20,339✔
858
    vInfo("vgId:%d, vnode is stopping", pVnode->config.vgId);
2,610!
859
    return false;
2,610✔
860
  }
861

862
  if (state.state != TAOS_SYNC_STATE_LEADER) {
17,706!
863
    terrno = TSDB_CODE_SYN_NOT_LEADER;
×
864
    vInfo("vgId:%d, vnode not leader, state:%s", pVnode->config.vgId, syncStr(state.state));
×
865
    return false;
×
866
  }
867

868
  if (!state.restored || !pVnode->restored) {
17,706!
869
    terrno = TSDB_CODE_SYN_RESTORING;
×
870
    vInfo("vgId:%d, vnode not restored:%d:%d", pVnode->config.vgId, state.restored, pVnode->restored);
×
871
    return false;
×
872
  }
873

874
  return true;
17,709✔
875
}
876

877
int64_t vnodeClusterId(SVnode *pVnode) {
×
878
  SSyncCfg *syncCfg = &pVnode->config.syncCfg;
×
879
  return syncCfg->nodeInfo[syncCfg->myIndex].clusterId;
×
880
}
881

882
int32_t vnodeNodeId(SVnode *pVnode) {
841,376✔
883
  SSyncCfg *syncCfg = &pVnode->config.syncCfg;
841,376✔
884
  return syncCfg->nodeInfo[syncCfg->myIndex].nodeId;
841,376✔
885
}
886

887
int32_t vnodeGetSnapshot(SVnode *pVnode, SSnapshot *pSnap) {
472,841✔
888
  int code = 0;
472,841✔
889
  pSnap->lastApplyIndex = pVnode->state.committed;
472,841✔
890
  pSnap->lastApplyTerm = pVnode->state.commitTerm;
472,841✔
891
  pSnap->lastConfigIndex = -1;
472,841✔
892
  pSnap->state = SYNC_FSM_STATE_COMPLETE;
472,841✔
893

894
  if (tsdbSnapGetFsState(pVnode) != TSDB_FS_STATE_NORMAL) {
472,841!
895
    pSnap->state = SYNC_FSM_STATE_INCOMPLETE;
×
896
  }
897

898
  if (pSnap->type == TDMT_SYNC_PREP_SNAPSHOT || pSnap->type == TDMT_SYNC_PREP_SNAPSHOT_REPLY) {
472,857✔
899
    code = tsdbSnapPrepDescription(pVnode, pSnap);
109✔
900
  }
901
  return code;
472,888✔
902
}
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