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

taosdata / TDengine / #4066

12 May 2025 05:35AM UTC coverage: 62.547% (+0.04%) from 62.508%
#4066

push

travis-ci

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

merge: from main to 3.0 branch

155777 of 317858 branches covered (49.01%)

Branch coverage included in aggregate %.

382 of 573 new or added lines in 31 files covered. (66.67%)

648 existing lines in 129 files now uncovered.

241270 of 316936 relevant lines covered (76.13%)

6462449.7 hits per line

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

65.51
/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; }
2,263,582✔
26

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

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

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

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

64
  if (code == 0) code = TSDB_CODE_SYN_NOT_LEADER;
47,927!
65

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

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

79
  tmsgSendRsp(&rsp);
47,927✔
80
}
47,926✔
81

82
static void inline vnodeHandleWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,063,960✔
83
  SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
2,063,960✔
84
  if (vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp) < 0) {
2,063,960!
UNCOV
85
    rsp.code = terrno;
×
UNCOV
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) {
2,064,081✔
90
    tmsgSendRsp(&rsp);
2,048,124✔
91
  } else {
92
    if (rsp.pCont) {
15,957✔
93
      rpcFreeCont(rsp.pCont);
9,608✔
94
    }
95
  }
96
}
2,064,137✔
97

98
static void vnodeHandleProposeError(SVnode *pVnode, SRpcMsg *pMsg, int32_t code) {
55,610✔
99
  if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING) {
55,610✔
100
    vnodeRedirectRpcMsg(pVnode, pMsg, code);
37,252✔
101
  } else if (code == TSDB_CODE_MSG_PREPROCESSED) {
18,358✔
102
    SRpcMsg rsp = {.code = TSDB_CODE_SUCCESS, .info = pMsg->info};
17,545✔
103
    if (rsp.info.handle != NULL) {
17,545✔
104
      tmsgSendRsp(&rsp);
17,542✔
105
    }
106
  } else {
107
    vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to propose since %s, code:0x%x", pVnode->config.vgId, pMsg,
813!
108
            tstrerror(code), code);
109
    SRpcMsg rsp = {.code = code, .info = pMsg->info};
813✔
110
    if (rsp.info.handle != NULL) {
813✔
111
      tmsgSendRsp(&rsp);
249✔
112
    }
113
  }
114
}
55,630✔
115

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

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

133
  if (code > 0) {
2,222,532✔
134
    vnodeHandleWriteMsg(pVnode, pMsg);
2,064,173✔
135
  } else if (code < 0) {
158,359✔
136
    if (terrno != 0) code = terrno;
14,115!
137
    vnodeHandleProposeError(pVnode, pMsg, code);
14,115✔
138
  }
139

140
  if (wait) vnodeWaitBlockMsg(pVnode, pMsg);
2,222,460✔
141
  return code;
2,222,500✔
142
}
143

144
void vnodeProposeCommitOnNeed(SVnode *pVnode, bool atExit) {
2,250,666✔
145
  if (!vnodeShouldCommit(pVnode, atExit)) {
2,250,666✔
146
    return;
2,243,043✔
147
  }
148

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

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

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

163
  if (!atExit) {
7,832✔
164
    if (vnodeProposeMsg(pVnode, &rpcMsg, isWeak) < 0) {
159!
165
      vTrace("vgId:%d, failed to propose vnode commit since %s", pVnode->config.vgId, terrstr());
×
166
    }
167
    rpcFreeCont(rpcMsg.pCont);
159✔
168
    rpcMsg.pCont = NULL;
159✔
169
  } else {
170
    int32_t code = 0;
7,673✔
171
    if ((code = tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &rpcMsg)) < 0) {
7,673✔
172
      vError("vgId:%d, failed to put vnode commit to write_queue since %s", pVnode->config.vgId, tstrerror(code));
2,752✔
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) {
2,255,244✔
283
  SVnode  *pVnode = pInfo->ahandle;
2,255,244✔
284
  int32_t  vgId = pVnode->config.vgId;
2,255,244✔
285
  int32_t  code = 0;
2,255,244✔
286
  SRpcMsg *pMsg = NULL;
2,255,244✔
287
  vTrace("vgId:%d, get %d msgs from vnode-write queue", vgId, numOfMsgs);
2,255,244✔
288

289
  for (int32_t msg = 0; msg < numOfMsgs; msg++) {
4,519,613✔
290
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
2,263,906!
291
    bool isWeak = vnodeIsMsgWeak(pMsg->msgType);
2,263,813✔
292

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

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

305
    bool atExit = false;
2,239,932✔
306
    vnodeProposeCommitOnNeed(pVnode, atExit);
2,239,932✔
307

308
    code = vnodePreProcessWriteMsg(pVnode, pMsg);
2,240,083✔
309
    if (code != 0) {
2,239,901✔
310
      if (code != TSDB_CODE_MSG_PREPROCESSED) {
17,647✔
311
        vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to pre-process since %s", vgId, pMsg, tstrerror(code));
121!
312
      }
313
      vnodeHandleProposeError(pVnode, pMsg, code);
17,647✔
314
      rpcFreeCont(pMsg->pCont);
17,655✔
315
      taosFreeQitem(pMsg);
17,670✔
316
      continue;
17,672✔
317
    }
318

319
    code = vnodeProposeMsg(pVnode, pMsg, isWeak);
2,222,254✔
320

321
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x", vgId, pMsg, code);
2,222,290!
322
    rpcFreeCont(pMsg->pCont);
2,222,290✔
323
    taosFreeQitem(pMsg);
2,222,388✔
324
  }
325
}
2,255,707✔
326

327
#endif
328

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

335
  for (int32_t i = 0; i < numOfMsgs; ++i) {
1,389,001✔
336
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
789,070!
337

338
    if (vnodeIsMsgBlock(pMsg->msgType)) {
789,069✔
339
      vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-apply queue, type:%s handle:%p index:%" PRId64
26,204!
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,
762,867!
345
              TMSG_INFO(pMsg->msgType), pMsg->info.handle, pMsg->info.conn.applyIndex);
346
    }
347

348
    SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
789,092✔
349
    if (rsp.code == 0) {
789,092✔
350
      if (vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp) < 0) {
789,086!
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);
789,084✔
358
    if (rsp.info.handle != NULL) {
789,076✔
359
      tmsgSendRsp(&rsp);
144,182✔
360
    } else {
361
      if (rsp.pCont) {
644,894✔
362
        rpcFreeCont(rsp.pCont);
582,643✔
363
      }
364
    }
365

366
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x index:%" PRId64, vgId, pMsg, rsp.code,
789,082!
367
            pMsg->info.conn.applyIndex);
368
    rpcFreeCont(pMsg->pCont);
789,082✔
369
    taosFreeQitem(pMsg);
789,081✔
370
  }
371
}
599,931✔
372

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

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

382
  return code;
988,611✔
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) {
232,278✔
405
  if (pMsg == NULL || pMsg->pCont == NULL) {
232,278!
406
    return TSDB_CODE_INVALID_PARA;
×
407
  }
408

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

415
  int32_t code = tmsgPutToQueue(msgcb, SYNC_QUEUE, pMsg);
232,278✔
416
  if (code != 0) {
232,279✔
417
    rpcFreeCont(pMsg->pCont);
1,691✔
418
    pMsg->pCont = NULL;
1,690✔
419
  }
420
  return code;
232,278✔
421
}
422

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

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

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

441
  vGDebug(&pMsg->info.traceId,
789,084!
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);
789,087✔
448
  if (code < 0) {
789,092✔
449
    if (code == TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE) {
13!
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));
13!
457
    }
458
  }
459
  return code;
789,094✔
460
}
461

462
static int32_t vnodeSyncCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
789,085✔
463
  if (pMsg->code == 0) {
789,085!
464
    return vnodeSyncApplyMsg(pFsm, pMsg, pMeta);
789,087✔
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) {
2,341,761✔
494
  SVnode *pVnode = pFSM->data;
2,341,761✔
495
  return atomic_load_64(&pVnode->state.applied);
2,341,761✔
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) {
55✔
507
  SVnode *pVnode = pFsm->data;
55✔
508
  return vnodeSnapReaderOpen(pVnode, (SSnapshotParam *)pParam, (SVSnapReader **)ppReader);
55✔
509
}
510

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

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

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

524
  do {
×
525
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
53✔
526
    if (itemSize == 0) {
53!
527
      vInfo("vgId:%d, start write vnode snapshot since apply queue is empty", pVnode->config.vgId);
53!
528
      break;
53✔
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);
53✔
536
}
537

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

543
  int32_t code = vnodeSnapWriterClose(pWriter, !isApply, pSnapshot);
53✔
544
  if (code != 0) {
53!
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;
53✔
549
}
550

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

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

564
  do {
565
    appliedIdx = vnodeSyncAppliedIndex(pFsm);
33,162✔
566
    if (appliedIdx > commitIdx) {
33,165!
567
      vError("vgId:%d, restore failed since applied-index:%" PRId64 " is larger than commit-index:%" PRId64, vgId,
×
568
             appliedIdx, commitIdx);
569
      break;
×
570
    }
571
    if (appliedIdx == commitIdx) {
33,165✔
572
      vInfo("vgId:%d, no items to be applied, restore finish", pVnode->config.vgId);
13,805✔
573
      break;
13,805✔
574
    } else {
575
      if (appliedIdx % 10 == 0) {
19,360✔
576
        vInfo("vgId:%d, restore not finish since %" PRId64 " items to be applied. commit-index:%" PRId64
1,008!
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,352✔
581
               ", applied-index:%" PRId64,
582
               vgId, commitIdx - appliedIdx, commitIdx, appliedIdx);
583
      }
584
      taosMsleep(10);
19,360✔
585
    }
586
  } while (true);
587

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

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

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

601
  if (vnodeIsRoleLeader(pVnode)) {
12,655✔
602
    // start to restore all stream tasks
603
    if (tsDisableStream) {
9,658!
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);
9,658✔
607
      if (pMeta->startInfo.startAllTasks == 1) {
9,658!
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;
9,658✔
613
        streamMetaWUnLock(pMeta);
9,658✔
614

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

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

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

635
  (void)taosThreadMutexLock(&pVnode->lock);
5,900✔
636
  if (pVnode->blocked) {
5,901!
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,901✔
644

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

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

659
  (void)taosThreadMutexLock(&pVnode->lock);
217✔
660
  if (pVnode->blocked) {
217!
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);
217✔
668
}
217✔
669

670
static void vnodeBecomeLeader(const SSyncFSM *pFsm) {
10,808✔
671
  SVnode *pVnode = pFsm->data;
10,808✔
672
  vDebug("vgId:%d, become leader", pVnode->config.vgId);
10,808✔
673
#ifdef USE_TQ
674
  if (pVnode->pTq) {
10,808!
675
    tqUpdateNodeStage(pVnode->pTq, true);
10,809✔
676
  }
677
#endif
678
}
10,808✔
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) {
89,489✔
702
  SVnode *pVnode = pFsm->data;
89,489✔
703

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

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

742
  return pFsm;
13,319✔
743
}
744

745
int32_t vnodeSyncOpen(SVnode *pVnode, char *path, int32_t vnodeVersion) {
13,318✔
746
  SSyncInfo syncInfo = {
13,318✔
747
      .snapshotStrategy = SYNC_STRATEGY_WAL_FIRST,
748
      .batchSize = 1,
749
      .vgId = pVnode->config.vgId,
13,318✔
750
      .syncCfg = pVnode->config.syncCfg,
751
      .pWal = pVnode->pWal,
13,318✔
752
      .msgcb = &pVnode->msgCb,
13,318✔
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,318✔
762
  syncInfo.pFsm = vnodeSyncMakeFsm(pVnode);
13,318✔
763

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

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

778
  return 0;
13,319✔
779
}
780

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

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

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

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

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

820
void vnodeSyncCheckTimeout(SVnode *pVnode) {
5,779✔
821
  vTrace("vgId:%d, check sync timeout msg", pVnode->config.vgId);
5,779✔
822
  (void)taosThreadMutexLock(&pVnode->lock);
5,779✔
823
  if (pVnode->blocked) {
5,779!
824
    int32_t curSec = taosGetTimestampSec();
×
825
    int32_t delta = curSec - pVnode->blockSec;
×
826
    if (delta > VNODE_TIMEOUT_SEC) {
×
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);
5,779✔
846
}
5,779✔
847

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

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

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

862
  if (state.state != TAOS_SYNC_STATE_LEADER) {
14,628!
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) {
14,628!
869
    terrno = TSDB_CODE_SYN_RESTORING;
1✔
870
    vInfo("vgId:%d, vnode not restored:%d:%d", pVnode->config.vgId, state.restored, pVnode->restored);
×
871
    return false;
×
872
  }
873

874
  return true;
14,627✔
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) {
762,534✔
883
  SSyncCfg *syncCfg = &pVnode->config.syncCfg;
762,534✔
884
  return syncCfg->nodeInfo[syncCfg->myIndex].nodeId;
762,534✔
885
}
886

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

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

898
  if (pSnap->type == TDMT_SYNC_PREP_SNAPSHOT || pSnap->type == TDMT_SYNC_PREP_SNAPSHOT_REPLY) {
453,187✔
899
    code = tsdbSnapPrepDescription(pVnode, pSnap);
107✔
900
  }
901
  return code;
453,190✔
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