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

taosdata / TDengine / #3842

07 Apr 2025 11:21AM UTC coverage: 62.696% (-0.3%) from 63.027%
#3842

push

travis-ci

web-flow
merge: from main to 3.0 branch (#30679)

154855 of 315075 branches covered (49.15%)

Branch coverage included in aggregate %.

6 of 8 new or added lines in 5 files covered. (75.0%)

2309 existing lines in 130 files now uncovered.

240176 of 314995 relevant lines covered (76.25%)

19119980.29 hits per line

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

65.92
/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,532,001✔
26

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

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

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

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

64
  if (code == 0) code = TSDB_CODE_SYN_NOT_LEADER;
35,949!
65

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

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

79
  tmsgSendRsp(&rsp);
35,949✔
80
}
35,947✔
81

82
static void inline vnodeHandleWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,261,018✔
83
  SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
10,261,018✔
84
  if (vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp) < 0) {
10,261,018!
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,261,221✔
90
    tmsgSendRsp(&rsp);
10,225,641✔
91
  } else {
92
    if (rsp.pCont) {
35,580✔
93
      rpcFreeCont(rsp.pCont);
24,987✔
94
    }
95
  }
96
}
10,261,359✔
97

98
static void vnodeHandleProposeError(SVnode *pVnode, SRpcMsg *pMsg, int32_t code) {
132,898✔
99
  if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING) {
132,898✔
100
    vnodeRedirectRpcMsg(pVnode, pMsg, code);
27,854✔
101
  } else if (code == TSDB_CODE_MSG_PREPROCESSED) {
105,044✔
102
    SRpcMsg rsp = {.code = TSDB_CODE_SUCCESS, .info = pMsg->info};
104,026✔
103
    if (rsp.info.handle != NULL) {
104,026!
104
      tmsgSendRsp(&rsp);
104,124✔
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,018!
108
            tstrerror(code), code);
109
    SRpcMsg rsp = {.code = code, .info = pMsg->info};
1,039✔
110
    if (rsp.info.handle != NULL) {
1,039✔
111
      tmsgSendRsp(&rsp);
638✔
112
    }
113
  }
114
}
133,164✔
115

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

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

133
  if (code > 0) {
10,408,771✔
134
    vnodeHandleWriteMsg(pVnode, pMsg);
10,261,442✔
135
  } else if (code < 0) {
147,329✔
136
    if (terrno != 0) code = terrno;
8,366!
137
    vnodeHandleProposeError(pVnode, pMsg, code);
8,366✔
138
  }
139

140
  if (wait) vnodeWaitBlockMsg(pVnode, pMsg);
10,408,632✔
141
  return code;
10,408,710✔
142
}
143

144
void vnodeProposeCommitOnNeed(SVnode *pVnode, bool atExit) {
10,522,587✔
145
  if (!vnodeShouldCommit(pVnode, atExit)) {
10,522,587✔
146
    return;
10,514,612✔
147
  }
148

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

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

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

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

289
  for (int32_t msg = 0; msg < numOfMsgs; msg++) {
19,330,883✔
290
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
10,532,314!
291
    bool isWeak = vnodeIsMsgWeak(pMsg->msgType);
10,532,595✔
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,532,111!
294
            vgId, pMsg, isWeak, vnodeIsMsgBlock(pMsg->msgType), msg, numOfMsgs, pMsg->info.handle);
295

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

305
    bool atExit = false;
10,512,233✔
306
    vnodeProposeCommitOnNeed(pVnode, atExit);
10,512,233✔
307

308
    code = vnodePreProcessWriteMsg(pVnode, pMsg);
10,512,635✔
309
    if (code != 0) {
10,511,932✔
310
      if (code != TSDB_CODE_MSG_PREPROCESSED) {
104,413✔
311
        vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to pre-process since %s", vgId, pMsg, tstrerror(code));
522!
312
      }
313
      vnodeHandleProposeError(pVnode, pMsg, code);
104,413✔
314
      rpcFreeCont(pMsg->pCont);
104,594✔
315
      taosFreeQitem(pMsg);
104,759✔
316
      continue;
104,804✔
317
    }
318

319
    code = vnodeProposeMsg(pVnode, pMsg, isWeak);
10,407,519✔
320

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

327
#endif
328

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

335
  for (int32_t i = 0; i < numOfMsgs; ++i) {
2,598,022✔
336
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
1,599,039!
337

338
    if (vnodeIsMsgBlock(pMsg->msgType)) {
1,599,035✔
339
      vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-apply queue, type:%s handle:%p index:%" PRId64
21,497!
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,577,534!
345
              TMSG_INFO(pMsg->msgType), pMsg->info.handle, pMsg->info.conn.applyIndex);
346
    }
347

348
    SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
1,599,041✔
349
    if (rsp.code == 0) {
1,599,041!
350
      if (vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp) < 0) {
1,599,049!
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,599,032✔
358
    if (rsp.info.handle != NULL) {
1,599,036✔
359
      tmsgSendRsp(&rsp);
138,899✔
360
    } else {
361
      if (rsp.pCont) {
1,460,137✔
362
        rpcFreeCont(rsp.pCont);
1,410,517✔
363
      }
364
    }
365

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

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

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

382
  return code;
2,733,191✔
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) {
385,269✔
405
  if (pMsg == NULL || pMsg->pCont == NULL) {
385,269!
406
    return TSDB_CODE_INVALID_PARA;
×
407
  }
408

409
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
385,270!
UNCOV
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);
385,271✔
416
  if (code != 0) {
385,266✔
417
    rpcFreeCont(pMsg->pCont);
1,280✔
418
    pMsg->pCont = NULL;
1,279✔
419
  }
420
  return code;
385,265✔
421
}
422

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

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

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

441
  vGDebug(&pMsg->info.traceId,
1,600,635!
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,600,642✔
448
  if (code < 0) vError("vgId:%d, failed to put into apply_queue since %s", pVnode->config.vgId, tstrerror(code));
1,600,642!
449
  return code;
1,600,640✔
450
}
451

452
static int32_t vnodeSyncCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
1,600,635✔
453
  if (pMsg->code == 0) {
1,600,635!
454
    return vnodeSyncApplyMsg(pFsm, pMsg, pMeta);
1,600,636✔
455
  }
456

457
  SVnode *pVnode = pFsm->data;
×
458
  vnodePostBlockMsg(pVnode, pMsg);
×
459

460
  SRpcMsg rsp = {
×
461
      .code = pMsg->code,
×
462
      .info = pMsg->info,
463
  };
464

465
  if (rsp.info.handle != NULL) {
×
466
    tmsgSendRsp(&rsp);
×
467
  }
468

469
  vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x index:%" PRId64, TD_VID(pVnode), pMsg, rsp.code,
×
470
          pMeta->index);
471
  rpcFreeCont(pMsg->pCont);
×
472
  pMsg->pCont = NULL;
×
473
  return 0;
×
474
}
475

476
static int32_t vnodeSyncPreCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
×
477
  if (pMeta->isWeak == 1) {
×
478
    return vnodeSyncApplyMsg(pFsm, pMsg, pMeta);
×
479
  }
480
  return 0;
×
481
}
482

483
static SyncIndex vnodeSyncAppliedIndex(const SSyncFSM *pFSM) {
10,524,605✔
484
  SVnode *pVnode = pFSM->data;
10,524,605✔
485
  return atomic_load_64(&pVnode->state.applied);
10,524,605✔
486
}
487

488
static void vnodeSyncRollBackMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
×
489
  SVnode *pVnode = pFsm->data;
×
490
  vGDebug(&pMsg->info.traceId,
×
491
          "vgId:%d, rollback-cb is excuted, fsm:%p, index:%" PRId64 ", weak:%d, code:%d, state:%d %s, type:%s",
492
          pVnode->config.vgId, pFsm, pMeta->index, pMeta->isWeak, pMeta->code, pMeta->state, syncStr(pMeta->state),
493
          TMSG_INFO(pMsg->msgType));
494
}
×
495

496
static int32_t vnodeSnapshotStartRead(const SSyncFSM *pFsm, void *pParam, void **ppReader) {
60✔
497
  SVnode *pVnode = pFsm->data;
60✔
498
  return vnodeSnapReaderOpen(pVnode, (SSnapshotParam *)pParam, (SVSnapReader **)ppReader);
60✔
499
}
500

501
static void vnodeSnapshotStopRead(const SSyncFSM *pFsm, void *pReader) {
60✔
502
  SVnode *pVnode = pFsm->data;
60✔
503
  vnodeSnapReaderClose(pReader);
60✔
504
}
60✔
505

506
static int32_t vnodeSnapshotDoRead(const SSyncFSM *pFsm, void *pReader, void **ppBuf, int32_t *len) {
7,096✔
507
  SVnode *pVnode = pFsm->data;
7,096✔
508
  return vnodeSnapRead(pReader, (uint8_t **)ppBuf, len);
7,096✔
509
}
510

511
static int32_t vnodeSnapshotStartWrite(const SSyncFSM *pFsm, void *pParam, void **ppWriter) {
59✔
512
  SVnode *pVnode = pFsm->data;
59✔
513

514
  do {
×
515
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
59✔
516
    if (itemSize == 0) {
59!
517
      vInfo("vgId:%d, start write vnode snapshot since apply queue is empty", pVnode->config.vgId);
59!
518
      break;
59✔
519
    } else {
520
      vInfo("vgId:%d, write vnode snapshot later since %d items in apply queue", pVnode->config.vgId, itemSize);
×
521
      taosMsleep(10);
×
522
    }
523
  } while (true);
524

525
  return vnodeSnapWriterOpen(pVnode, (SSnapshotParam *)pParam, (SVSnapWriter **)ppWriter);
59✔
526
}
527

528
static int32_t vnodeSnapshotStopWrite(const SSyncFSM *pFsm, void *pWriter, bool isApply, SSnapshot *pSnapshot) {
59✔
529
  SVnode *pVnode = pFsm->data;
59✔
530
  vInfo("vgId:%d, stop write vnode snapshot, apply:%d, index:%" PRId64 " term:%" PRIu64 " config:%" PRId64,
59!
531
        pVnode->config.vgId, isApply, pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm, pSnapshot->lastConfigIndex);
532

533
  int32_t code = vnodeSnapWriterClose(pWriter, !isApply, pSnapshot);
59✔
534
  if (code != 0) {
59!
535
    vError("vgId:%d, failed to finish applying vnode snapshot since %s, code:0x%x", pVnode->config.vgId, terrstr(),
×
536
           code);
537
  }
538
  return code;
59✔
539
}
540

541
static int32_t vnodeSnapshotDoWrite(const SSyncFSM *pFsm, void *pWriter, void *pBuf, int32_t len) {
6,979✔
542
  SVnode *pVnode = pFsm->data;
6,979✔
543
  vDebug("vgId:%d, continue write vnode snapshot, blockLen:%d", pVnode->config.vgId, len);
6,979!
544
  int32_t code = vnodeSnapWrite(pWriter, pBuf, len);
6,979✔
545
  vDebug("vgId:%d, continue write vnode snapshot finished, blockLen:%d", pVnode->config.vgId, len);
6,979!
546
  return code;
6,979✔
547
}
548

549
static void vnodeRestoreFinish(const SSyncFSM *pFsm, const SyncIndex commitIdx) {
12,900✔
550
  SVnode   *pVnode = pFsm->data;
12,900✔
551
  int32_t   vgId = pVnode->config.vgId;
12,900✔
552
  SyncIndex appliedIdx = -1;
12,900✔
553

554
  do {
555
    appliedIdx = vnodeSyncAppliedIndex(pFsm);
32,152✔
556
    if (appliedIdx > commitIdx) {
32,149!
557
      vError("vgId:%d, restore failed since applied-index:%" PRId64 " is larger than commit-index:%" PRId64, vgId,
×
558
             appliedIdx, commitIdx);
559
      break;
×
560
    }
561
    if (appliedIdx == commitIdx) {
32,149✔
562
      vInfo("vgId:%d, no items to be applied, restore finish", pVnode->config.vgId);
12,899✔
563
      break;
12,900✔
564
    } else {
565
      vInfo("vgId:%d, restore not finish since %" PRId64 " items to be applied. commit-index:%" PRId64
19,250✔
566
            ", applied-index:%" PRId64,
567
            vgId, commitIdx - appliedIdx, commitIdx, appliedIdx);
568
      taosMsleep(10);
19,252✔
569
    }
570
  } while (true);
571

572
  walApplyVer(pVnode->pWal, commitIdx);
12,900✔
573
  pVnode->restored = true;
12,900✔
574

575
#ifdef USE_STREAM
576
  SStreamMeta *pMeta = pVnode->pTq->pStreamMeta;
12,900✔
577
  streamMetaWLock(pMeta);
12,900✔
578

579
  if (pMeta->startInfo.tasksWillRestart) {
12,900✔
580
    vInfo("vgId:%d, sync restore finished, stream tasks will be launched by other thread", vgId);
914!
581
    streamMetaWUnLock(pMeta);
914✔
582
    return;
914✔
583
  }
584

585
  if (vnodeIsRoleLeader(pVnode)) {
11,986✔
586
    // start to restore all stream tasks
587
    if (tsDisableStream) {
9,596!
588
      vInfo("vgId:%d, sync restore finished, not launch stream tasks, since stream tasks are disabled", vgId);
×
589
    } else {
590
      vInfo("vgId:%d sync restore finished, start to launch stream task(s)", vgId);
9,596✔
591
      if (pMeta->startInfo.startAllTasks == 1) {
9,596!
592
        pMeta->startInfo.restartCount += 1;
×
593
        vDebug("vgId:%d in start tasks procedure, inc restartCounter by 1, remaining restart:%d", vgId,
×
594
               pMeta->startInfo.restartCount);
595
      } else {
596
        pMeta->startInfo.startAllTasks = 1;
9,596✔
597
        streamMetaWUnLock(pMeta);
9,596✔
598

599
        tqInfo("vgId:%d stream task already loaded, start them", vgId);
9,596!
600
        int32_t code = streamTaskSchedTask(&pVnode->msgCb, TD_VID(pVnode), 0, 0, STREAM_EXEC_T_START_ALL_TASKS, false);
9,596✔
601
        if (code != 0) {
9,596!
602
          tqError("vgId:%d failed to sched stream task, code:%s", vgId, tstrerror(code));
×
603
        }
604
        return;
9,596✔
605
      }
606
    }
607
  } else {
608
    vInfo("vgId:%d, sync restore finished, not launch stream tasks since not leader", vgId);
2,390!
609
  }
610

611
  streamMetaWUnLock(pMeta);
2,390✔
612
#endif
613
}
614

615
static void vnodeBecomeFollower(const SSyncFSM *pFsm) {
4,672✔
616
  SVnode *pVnode = pFsm->data;
4,672✔
617
  vInfo("vgId:%d, become follower", pVnode->config.vgId);
4,672!
618

619
  (void)taosThreadMutexLock(&pVnode->lock);
4,672✔
620
  if (pVnode->blocked) {
4,672!
621
    pVnode->blocked = false;
×
622
    vDebug("vgId:%d, become follower and post block", pVnode->config.vgId);
×
623
    if (tsem_post(&pVnode->syncSem) != 0) {
×
624
      vError("vgId:%d, failed to post sync semaphore", pVnode->config.vgId);
×
625
    }
626
  }
627
  (void)taosThreadMutexUnlock(&pVnode->lock);
4,672✔
628

629
#ifdef USE_TQ
630
  if (pVnode->pTq) {
4,672!
631
    tqUpdateNodeStage(pVnode->pTq, false);
4,672✔
632
    if (tqStopStreamAllTasksAsync(pVnode->pTq->pStreamMeta, &pVnode->msgCb) != 0) {
4,672!
633
      vError("vgId:%d, failed to stop stream tasks", pVnode->config.vgId);
×
634
    }
635
  }
636
#endif
637
}
4,672✔
638

639
static void vnodeBecomeLearner(const SSyncFSM *pFsm) {
164✔
640
  SVnode *pVnode = pFsm->data;
164✔
641
  vInfo("vgId:%d, become learner", pVnode->config.vgId);
164!
642

643
  (void)taosThreadMutexLock(&pVnode->lock);
164✔
644
  if (pVnode->blocked) {
164!
645
    pVnode->blocked = false;
×
646
    vDebug("vgId:%d, become learner and post block", pVnode->config.vgId);
×
647
    if (tsem_post(&pVnode->syncSem) != 0) {
×
648
      vError("vgId:%d, failed to post sync semaphore", pVnode->config.vgId);
×
649
    }
650
  }
651
  (void)taosThreadMutexUnlock(&pVnode->lock);
164✔
652
}
164✔
653

654
static void vnodeBecomeLeader(const SSyncFSM *pFsm) {
10,509✔
655
  SVnode *pVnode = pFsm->data;
10,509✔
656
  vDebug("vgId:%d, become leader", pVnode->config.vgId);
10,509✔
657
#ifdef USE_TQ
658
  if (pVnode->pTq) {
10,509!
659
    tqUpdateNodeStage(pVnode->pTq, true);
10,509✔
660
  }
661
#endif
662
}
10,510✔
663

664
static void vnodeBecomeAssignedLeader(const SSyncFSM *pFsm) {
×
665
  SVnode *pVnode = pFsm->data;
×
666
  vDebug("vgId:%d, become assigned leader", pVnode->config.vgId);
×
667
#ifdef USE_TQ
668
  if (pVnode->pTq) {
×
669
    tqUpdateNodeStage(pVnode->pTq, true);
×
670
  }
671
#endif
672
}
×
673

674
static bool vnodeApplyQueueEmpty(const SSyncFSM *pFsm) {
×
675
  SVnode *pVnode = pFsm->data;
×
676

677
  if (pVnode != NULL && pVnode->msgCb.qsizeFp != NULL) {
×
678
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
×
679
    return (itemSize == 0);
×
680
  } else {
681
    return true;
×
682
  }
683
}
684

685
static int32_t vnodeApplyQueueItems(const SSyncFSM *pFsm) {
81,792✔
686
  SVnode *pVnode = pFsm->data;
81,792✔
687

688
  if (pVnode != NULL && pVnode->msgCb.qsizeFp != NULL) {
81,792!
689
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
81,795✔
690
    return itemSize;
81,790✔
691
  } else {
692
    return TSDB_CODE_INVALID_PARA;
×
693
  }
694
}
695

696
static SSyncFSM *vnodeSyncMakeFsm(SVnode *pVnode) {
12,450✔
697
  SSyncFSM *pFsm = taosMemoryCalloc(1, sizeof(SSyncFSM));
12,450!
698
  if (pFsm == NULL) {
12,450!
699
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
700
    return NULL;
×
701
  }
702
  pFsm->data = pVnode;
12,450✔
703
  pFsm->FpCommitCb = vnodeSyncCommitMsg;
12,450✔
704
  pFsm->FpAppliedIndexCb = vnodeSyncAppliedIndex;
12,450✔
705
  pFsm->FpPreCommitCb = vnodeSyncPreCommitMsg;
12,450✔
706
  pFsm->FpRollBackCb = vnodeSyncRollBackMsg;
12,450✔
707
  pFsm->FpGetSnapshot = NULL;
12,450✔
708
  pFsm->FpGetSnapshotInfo = vnodeSyncGetSnapshotInfo;
12,450✔
709
  pFsm->FpRestoreFinishCb = vnodeRestoreFinish;
12,450✔
710
  pFsm->FpAfterRestoredCb = NULL;
12,450✔
711
  pFsm->FpLeaderTransferCb = NULL;
12,450✔
712
  pFsm->FpApplyQueueEmptyCb = vnodeApplyQueueEmpty;
12,450✔
713
  pFsm->FpApplyQueueItems = vnodeApplyQueueItems;
12,450✔
714
  pFsm->FpBecomeLeaderCb = vnodeBecomeLeader;
12,450✔
715
  pFsm->FpBecomeAssignedLeaderCb = vnodeBecomeAssignedLeader;
12,450✔
716
  pFsm->FpBecomeFollowerCb = vnodeBecomeFollower;
12,450✔
717
  pFsm->FpBecomeLearnerCb = vnodeBecomeLearner;
12,450✔
718
  pFsm->FpReConfigCb = NULL;
12,450✔
719
  pFsm->FpSnapshotStartRead = vnodeSnapshotStartRead;
12,450✔
720
  pFsm->FpSnapshotStopRead = vnodeSnapshotStopRead;
12,450✔
721
  pFsm->FpSnapshotDoRead = vnodeSnapshotDoRead;
12,450✔
722
  pFsm->FpSnapshotStartWrite = vnodeSnapshotStartWrite;
12,450✔
723
  pFsm->FpSnapshotStopWrite = vnodeSnapshotStopWrite;
12,450✔
724
  pFsm->FpSnapshotDoWrite = vnodeSnapshotDoWrite;
12,450✔
725

726
  return pFsm;
12,450✔
727
}
728

729
int32_t vnodeSyncOpen(SVnode *pVnode, char *path, int32_t vnodeVersion) {
12,450✔
730
  SSyncInfo syncInfo = {
12,450✔
731
      .snapshotStrategy = SYNC_STRATEGY_WAL_FIRST,
732
      .batchSize = 1,
733
      .vgId = pVnode->config.vgId,
12,450✔
734
      .syncCfg = pVnode->config.syncCfg,
735
      .pWal = pVnode->pWal,
12,450✔
736
      .msgcb = &pVnode->msgCb,
12,450✔
737
      .syncSendMSg = vnodeSyncSendMsg,
738
      .syncEqMsg = vnodeSyncEqMsg,
739
      .syncEqCtrlMsg = vnodeSyncEqCtrlMsg,
740
      .pingMs = 5000,
741
      .electMs = 4000,
742
      .heartbeatMs = 700,
743
  };
744

745
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", path, TD_DIRSEP);
12,450✔
746
  syncInfo.pFsm = vnodeSyncMakeFsm(pVnode);
12,450✔
747

748
  SSyncCfg *pCfg = &syncInfo.syncCfg;
12,450✔
749
  vInfo("vgId:%d, start to open sync, replica:%d selfIndex:%d", pVnode->config.vgId, pCfg->replicaNum, pCfg->myIndex);
12,450✔
750
  for (int32_t i = 0; i < pCfg->totalReplicaNum; ++i) {
31,007✔
751
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
18,557✔
752
    vInfo("vgId:%d, index:%d ep:%s:%u dnode:%d cluster:%" PRId64, pVnode->config.vgId, i, pNode->nodeFqdn,
18,557✔
753
          pNode->nodePort, pNode->nodeId, pNode->clusterId);
754
  }
755

756
  pVnode->sync = syncOpen(&syncInfo, vnodeVersion);
12,450✔
757
  if (pVnode->sync <= 0) {
12,450!
758
    vError("vgId:%d, failed to open sync since %s", pVnode->config.vgId, terrstr());
×
759
    return terrno;
×
760
  }
761

762
  return 0;
12,450✔
763
}
764

765
int32_t vnodeSyncStart(SVnode *pVnode) {
12,450✔
766
  vInfo("vgId:%d, start sync", pVnode->config.vgId);
12,450✔
767
  int32_t code = syncStart(pVnode->sync);
12,450✔
768
  if (code) {
12,450!
769
    vError("vgId:%d, failed to start sync subsystem since %s", pVnode->config.vgId, tstrerror(code));
×
770
    return code;
×
771
  }
772
  return 0;
12,450✔
773
}
774

775
void vnodeSyncPreClose(SVnode *pVnode) {
12,448✔
776
  vInfo("vgId:%d, sync pre close", pVnode->config.vgId);
12,448✔
777
  int32_t code = syncLeaderTransfer(pVnode->sync);
12,450✔
778
  if (code) {
12,450✔
779
    vError("vgId:%d, failed to transfer leader since %s", pVnode->config.vgId, tstrerror(code));
882!
780
  }
781
  syncPreStop(pVnode->sync);
12,450✔
782

783
  (void)taosThreadMutexLock(&pVnode->lock);
12,450✔
784
  if (pVnode->blocked) {
12,449✔
785
    vInfo("vgId:%d, post block after close sync", pVnode->config.vgId);
19!
786
    pVnode->blocked = false;
19✔
787
    if (tsem_post(&pVnode->syncSem) != 0) {
19!
788
      vError("vgId:%d, failed to post block", pVnode->config.vgId);
×
789
    }
790
  }
791
  (void)taosThreadMutexUnlock(&pVnode->lock);
12,449✔
792
}
12,449✔
793

794
void vnodeSyncPostClose(SVnode *pVnode) {
12,450✔
795
  vInfo("vgId:%d, sync post close", pVnode->config.vgId);
12,450✔
796
  syncPostStop(pVnode->sync);
12,450✔
797
}
12,449✔
798

799
void vnodeSyncClose(SVnode *pVnode) {
12,450✔
800
  vInfo("vgId:%d, close sync", pVnode->config.vgId);
12,450✔
801
  syncStop(pVnode->sync);
12,450✔
802
}
12,448✔
803

804
void vnodeSyncCheckTimeout(SVnode *pVnode) {
33,931✔
805
  vTrace("vgId:%d, check sync timeout msg", pVnode->config.vgId);
33,931✔
806
  (void)taosThreadMutexLock(&pVnode->lock);
33,931✔
807
  if (pVnode->blocked) {
33,931✔
808
    int32_t curSec = taosGetTimestampSec();
6✔
809
    int32_t delta = curSec - pVnode->blockSec;
6✔
810
    if (delta > VNODE_TIMEOUT_SEC) {
6!
811
      vError("vgId:%d, failed to propose since timeout and post block, start:%d cur:%d delta:%d seq:%" PRId64,
×
812
             pVnode->config.vgId, pVnode->blockSec, curSec, delta, pVnode->blockSeq);
813
      if (syncSendTimeoutRsp(pVnode->sync, pVnode->blockSeq) != 0) {
×
814
#if 0
815
        SRpcMsg rpcMsg = {.code = TSDB_CODE_SYN_TIMEOUT, .info = pVnode->blockInfo};
816
        vError("send timeout response since its applyed, seq:%" PRId64 " handle:%p ahandle:%p", pVnode->blockSeq,
817
              rpcMsg.info.handle, rpcMsg.info.ahandle);
818
        rpcSendResponse(&rpcMsg);
819
#endif
820
      }
821
      pVnode->blocked = false;
×
822
      pVnode->blockSec = 0;
×
823
      pVnode->blockSeq = 0;
×
824
      if (tsem_post(&pVnode->syncSem) != 0) {
×
825
        vError("vgId:%d, failed to post block", pVnode->config.vgId);
×
826
      }
827
    }
828
  }
829
  (void)taosThreadMutexUnlock(&pVnode->lock);
33,931✔
830
}
33,931✔
831

832
bool vnodeIsRoleLeader(SVnode *pVnode) {
150,341✔
833
  SSyncState state = syncGetState(pVnode->sync);
150,341✔
834
  return state.state == TAOS_SYNC_STATE_LEADER;
150,405✔
835
}
836

837
bool vnodeIsLeader(SVnode *pVnode) {
18,676✔
838
  terrno = 0;
18,676✔
839
  SSyncState state = syncGetState(pVnode->sync);
18,703✔
840

841
  if (terrno != 0) {
18,723✔
842
    vInfo("vgId:%d, vnode is stopping", pVnode->config.vgId);
2,103!
843
    return false;
2,103✔
844
  }
845

846
  if (state.state != TAOS_SYNC_STATE_LEADER) {
16,609!
847
    terrno = TSDB_CODE_SYN_NOT_LEADER;
×
848
    vInfo("vgId:%d, vnode not leader, state:%s", pVnode->config.vgId, syncStr(state.state));
×
849
    return false;
×
850
  }
851

852
  if (!state.restored || !pVnode->restored) {
16,609✔
853
    terrno = TSDB_CODE_SYN_RESTORING;
15✔
854
    vInfo("vgId:%d, vnode not restored:%d:%d", pVnode->config.vgId, state.restored, pVnode->restored);
×
855
    return false;
×
856
  }
857

858
  return true;
16,594✔
859
}
860

861
int64_t vnodeClusterId(SVnode *pVnode) {
×
862
  SSyncCfg *syncCfg = &pVnode->config.syncCfg;
×
863
  return syncCfg->nodeInfo[syncCfg->myIndex].clusterId;
×
864
}
865

866
int32_t vnodeNodeId(SVnode *pVnode) {
846,778✔
867
  SSyncCfg *syncCfg = &pVnode->config.syncCfg;
846,778✔
868
  return syncCfg->nodeInfo[syncCfg->myIndex].nodeId;
846,778✔
869
}
870

871
int32_t vnodeGetSnapshot(SVnode *pVnode, SSnapshot *pSnap) {
430,602✔
872
  int code = 0;
430,602✔
873
  pSnap->lastApplyIndex = pVnode->state.committed;
430,602✔
874
  pSnap->lastApplyTerm = pVnode->state.commitTerm;
430,602✔
875
  pSnap->lastConfigIndex = -1;
430,602✔
876
  pSnap->state = SYNC_FSM_STATE_COMPLETE;
430,602✔
877

878
  if (tsdbSnapGetFsState(pVnode) != TSDB_FS_STATE_NORMAL) {
430,602!
879
    pSnap->state = SYNC_FSM_STATE_INCOMPLETE;
×
880
  }
881

882
  if (pSnap->type == TDMT_SYNC_PREP_SNAPSHOT || pSnap->type == TDMT_SYNC_PREP_SNAPSHOT_REPLY) {
430,621✔
883
    code = tsdbSnapPrepDescription(pVnode, pSnap);
118✔
884
  }
885
  return code;
430,622✔
886
}
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