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

taosdata / TDengine / #3543

29 Nov 2024 02:58AM UTC coverage: 60.842% (+0.02%) from 60.819%
#3543

push

travis-ci

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

merge: from main to 3.0

120460 of 253224 branches covered (47.57%)

Branch coverage included in aggregate %.

706 of 908 new or added lines in 18 files covered. (77.75%)

2401 existing lines in 137 files now uncovered.

201633 of 276172 relevant lines covered (73.01%)

19045673.23 hits per line

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

68.5
/source/libs/sync/src/syncPipeline.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

18
#include "syncPipeline.h"
19
#include "syncCommit.h"
20
#include "syncIndexMgr.h"
21
#include "syncInt.h"
22
#include "syncRaftCfg.h"
23
#include "syncRaftEntry.h"
24
#include "syncRaftStore.h"
25
#include "syncReplication.h"
26
#include "syncRespMgr.h"
27
#include "syncSnapshot.h"
28
#include "syncUtil.h"
29
#include "syncVoteMgr.h"
30

31
static int64_t tsLogBufferMemoryUsed = 0;  // total bytes of vnode log buffer
32

33
static bool syncIsMsgBlock(tmsg_t type) {
3,138,908✔
34
  return (type == TDMT_VND_CREATE_TABLE) || (type == TDMT_VND_ALTER_TABLE) || (type == TDMT_VND_DROP_TABLE) ||
3,128,798✔
35
         (type == TDMT_VND_UPDATE_TAG_VAL) || (type == TDMT_VND_ALTER_CONFIRM);
6,267,706✔
36
}
37

38
FORCE_INLINE static int64_t syncGetRetryMaxWaitMs() {
39
  return SYNC_LOG_REPL_RETRY_WAIT_MS * (1 << SYNC_MAX_RETRY_BACKOFF);
30,238✔
40
}
41

42
int64_t syncLogBufferGetEndIndex(SSyncLogBuffer* pBuf) {
10,532,262✔
43
  (void)taosThreadMutexLock(&pBuf->mutex);
10,532,262✔
44
  int64_t index = pBuf->endIndex;
10,532,589✔
45
  (void)taosThreadMutexUnlock(&pBuf->mutex);
10,532,589✔
46
  return index;
10,532,588✔
47
}
48

49
int32_t syncLogBufferAppend(SSyncLogBuffer* pBuf, SSyncNode* pNode, SSyncRaftEntry* pEntry) {
10,532,308✔
50
  int32_t code = 0;
10,532,308✔
51
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
10,532,308!
52
  (void)taosThreadMutexLock(&pBuf->mutex);
10,532,557✔
53
  SyncIndex index = pEntry->index;
10,532,587✔
54

55
  if (index - pBuf->startIndex >= pBuf->size) {
10,532,587!
56
    code = TSDB_CODE_SYN_BUFFER_FULL;
×
57
    sError("vgId:%d, failed to append since %s. index:%" PRId64 "", pNode->vgId, tstrerror(code), index);
×
58
    goto _err;
×
59
  }
60

61
  if (pNode->restoreFinish && index - pBuf->commitIndex >= TSDB_SYNC_NEGOTIATION_WIN) {
10,532,587!
62
    code = TSDB_CODE_SYN_NEGOTIATION_WIN_FULL;
×
63
    sError("vgId:%d, failed to append since %s, index:%" PRId64 ", commit-index:%" PRId64, pNode->vgId, tstrerror(code),
×
64
           index, pBuf->commitIndex);
65
    goto _err;
×
66
  }
67

68
  SyncIndex appliedIndex = pNode->pFsm->FpAppliedIndexCb(pNode->pFsm);
10,532,587✔
69
  if (pNode->restoreFinish && pBuf->commitIndex - appliedIndex >= TSDB_SYNC_APPLYQ_SIZE_LIMIT) {
10,532,390!
70
    code = TSDB_CODE_SYN_WRITE_STALL;
×
71
    sError("vgId:%d, failed to append since %s. index:%" PRId64 ", commit-index:%" PRId64 ", applied-index:%" PRId64,
×
72
           pNode->vgId, tstrerror(code), index, pBuf->commitIndex, appliedIndex);
73
    goto _err;
×
74
  }
75

76
  if (index != pBuf->endIndex) {
10,532,390!
77
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
78
    goto _err;
×
79
  };
80

81
  SSyncRaftEntry* pExist = pBuf->entries[index % pBuf->size].pItem;
10,532,390✔
82
  if (pExist != NULL) {
10,532,390!
83
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
84
    goto _err;
×
85
  }
86

87
  // initial log buffer with at least one item, e.g. commitIndex
88
  SSyncRaftEntry* pMatch = pBuf->entries[(index - 1 + pBuf->size) % pBuf->size].pItem;
10,532,390✔
89
  if (pMatch == NULL) {
10,532,390!
90
    sError("vgId:%d, no matched log entry", pNode->vgId);
×
91
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
92
    goto _err;
×
93
  }
94
  if (pMatch->index + 1 != index) {
10,532,390!
95
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
96
    goto _err;
×
97
  }
98
  if (!(pMatch->term <= pEntry->term)) {
10,532,390!
99
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
100
    goto _err;
×
101
  }
102

103
  SSyncLogBufEntry tmp = {.pItem = pEntry, .prevLogIndex = pMatch->index, .prevLogTerm = pMatch->term};
10,532,390✔
104
  pBuf->entries[index % pBuf->size] = tmp;
10,532,390✔
105
  pBuf->endIndex = index + 1;
10,532,390✔
106
  if (pNode->vgId > 1) {
10,532,390✔
107
    pBuf->bytes += pEntry->bytes;
10,431,034✔
108
    (void)atomic_add_fetch_64(&tsLogBufferMemoryUsed, (int64_t)pEntry->bytes);
10,431,034✔
109
  }
110

111
  (void)taosThreadMutexUnlock(&pBuf->mutex);
10,532,520✔
112
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
10,532,625!
113
  return 0;
10,532,466✔
114

115
_err:
×
116
  (void)taosThreadMutexUnlock(&pBuf->mutex);
×
117
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
×
118
  taosMsleep(1);
×
119
  TAOS_RETURN(code);
×
120
}
121

122
int32_t syncLogReplGetPrevLogTerm(SSyncLogReplMgr* pMgr, SSyncNode* pNode, SyncIndex index, SyncTerm* pSyncTerm) {
2,764,209✔
123
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
2,764,209✔
124
  SSyncRaftEntry* pEntry = NULL;
2,764,209✔
125
  SyncIndex       prevIndex = index - 1;
2,764,209✔
126
  SyncTerm        prevLogTerm = -1;
2,764,209✔
127
  int32_t         code = 0;
2,764,209✔
128

129
  if (prevIndex == -1 && pNode->pLogStore->syncLogBeginIndex(pNode->pLogStore) == 0) {
2,764,209✔
130
    *pSyncTerm = 0;
2,811✔
131
    return 0;
2,811✔
132
  }
133

134
  if (prevIndex > pBuf->matchIndex) {
2,761,398✔
135
    *pSyncTerm = -1;
37✔
136
    TAOS_RETURN(TSDB_CODE_WAL_LOG_NOT_EXIST);
37✔
137
  }
138

139
  if (index - 1 != prevIndex) return TSDB_CODE_SYN_INTERNAL_ERROR;
2,761,361!
140

141
  if (prevIndex >= pBuf->startIndex) {
2,761,361✔
142
    pEntry = pBuf->entries[(prevIndex + pBuf->size) % pBuf->size].pItem;
332,210✔
143
    if (pEntry == NULL) {
332,210!
144
      sError("vgId:%d, failed to get pre log term since no log entry found", pNode->vgId);
×
145
      *pSyncTerm = -1;
×
146
      TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
147
    }
148
    prevLogTerm = pEntry->term;
332,210✔
149
    *pSyncTerm = prevLogTerm;
332,210✔
150
    return 0;
332,210✔
151
  }
152

153
  if (pMgr && pMgr->startIndex <= prevIndex && prevIndex < pMgr->endIndex) {
2,429,151✔
154
    int64_t timeMs = pMgr->states[(prevIndex + pMgr->size) % pMgr->size].timeMs;
2,422,757✔
155
    if (timeMs == 0) {
2,422,757!
156
      sError("vgId:%d, failed to get pre log term since timeMs is 0", pNode->vgId);
×
157
      *pSyncTerm = -1;
×
158
      TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
159
    }
160
    prevLogTerm = pMgr->states[(prevIndex + pMgr->size) % pMgr->size].term;
2,422,757✔
161
    if (!(prevIndex == 0 || prevLogTerm != 0)) {
2,422,757!
162
      sError("vgId:%d, failed to get pre log term prevIndex:%" PRId64 ", prevLogTerm:%" PRId64, pNode->vgId, prevIndex,
×
163
             prevLogTerm);
164
      *pSyncTerm = -1;
×
165
      TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
166
    }
167
    *pSyncTerm = prevLogTerm;
2,422,757✔
168
    return 0;
2,422,757✔
169
  }
170

171
  SSnapshot snapshot = {0};
6,394✔
172
  (void)pNode->pFsm->FpGetSnapshotInfo(pNode->pFsm, &snapshot);
6,394✔
173
  if (prevIndex == snapshot.lastApplyIndex) {
6,395✔
174
    *pSyncTerm = snapshot.lastApplyTerm;
16✔
175
    return 0;
16✔
176
  }
177

178
  if ((code = pNode->pLogStore->syncLogGetEntry(pNode->pLogStore, prevIndex, &pEntry)) == 0) {
6,379✔
179
    prevLogTerm = pEntry->term;
6,273✔
180
    syncEntryDestroy(pEntry);
6,273✔
181
    pEntry = NULL;
6,273✔
182
    *pSyncTerm = prevLogTerm;
6,273✔
183
    return 0;
6,273✔
184
  }
185

186
  *pSyncTerm = -1;
106✔
187
  sInfo("vgId:%d, failed to get log term since %s. index:%" PRId64, pNode->vgId, tstrerror(code), prevIndex);
106!
188
  TAOS_RETURN(code);
106✔
189
}
190

191
SSyncRaftEntry* syncEntryBuildDummy(SyncTerm term, SyncIndex index, int32_t vgId) {
15,991✔
192
  return syncEntryBuildNoop(term, index, vgId);
15,991✔
193
}
194

195
int32_t syncLogValidateAlignmentOfCommit(SSyncNode* pNode, SyncIndex commitIndex) {
16,012✔
196
  SyncIndex firstVer = pNode->pLogStore->syncLogBeginIndex(pNode->pLogStore);
16,012✔
197
  if (firstVer > commitIndex + 1) {
16,013!
198
    sError("vgId:%d, firstVer of WAL log greater than tsdb commit version + 1. firstVer:%" PRId64
×
199
           ", tsdb commit version:%" PRId64 "",
200
           pNode->vgId, firstVer, commitIndex);
201
    return TSDB_CODE_WAL_LOG_INCOMPLETE;
×
202
  }
203

204
  SyncIndex lastVer = pNode->pLogStore->syncLogLastIndex(pNode->pLogStore);
16,013✔
205
  if (lastVer < commitIndex) {
16,012!
206
    sError("vgId:%d, lastVer of WAL log less than tsdb commit version. lastVer:%" PRId64
×
207
           ", tsdb commit version:%" PRId64 "",
208
           pNode->vgId, lastVer, commitIndex);
209
    return TSDB_CODE_WAL_LOG_INCOMPLETE;
×
210
  }
211

212
  return 0;
16,012✔
213
}
214

215
int32_t syncLogBufferInitWithoutLock(SSyncLogBuffer* pBuf, SSyncNode* pNode) {
16,013✔
216
  if (pNode->pLogStore == NULL) {
16,013!
217
    sError("log store not created");
×
218
    return TSDB_CODE_SYN_INTERNAL_ERROR;
×
219
  }
220
  if (pNode->pFsm == NULL) {
16,013!
221
    sError("pFsm not registered");
×
222
    return TSDB_CODE_SYN_INTERNAL_ERROR;
×
223
  }
224
  if (pNode->pFsm->FpGetSnapshotInfo == NULL) {
16,013!
225
    sError("FpGetSnapshotInfo not registered");
×
226
    return TSDB_CODE_SYN_INTERNAL_ERROR;
×
227
  }
228

229
  int32_t   code = 0, lino = 0;
16,013✔
230
  SSnapshot snapshot = {0};
16,013✔
231
  TAOS_CHECK_EXIT(pNode->pFsm->FpGetSnapshotInfo(pNode->pFsm, &snapshot));
16,013!
232

233
  SyncIndex commitIndex = snapshot.lastApplyIndex;
16,012✔
234
  SyncTerm  commitTerm = TMAX(snapshot.lastApplyTerm, 0);
16,012✔
235
  TAOS_CHECK_EXIT(syncLogValidateAlignmentOfCommit(pNode, commitIndex));
16,012!
236

237
  SyncIndex lastVer = pNode->pLogStore->syncLogLastIndex(pNode->pLogStore);
16,012✔
238
  if (lastVer < commitIndex) return TSDB_CODE_SYN_INTERNAL_ERROR;
16,012!
239
  ;
240
  SyncIndex toIndex = lastVer;
16,012✔
241
  // update match index
242
  pBuf->commitIndex = commitIndex;
16,012✔
243
  pBuf->matchIndex = toIndex;
16,012✔
244
  pBuf->endIndex = toIndex + 1;
16,012✔
245

246
  // load log entries in reverse order
247
  SSyncLogStore*  pLogStore = pNode->pLogStore;
16,012✔
248
  SyncIndex       index = toIndex;
16,012✔
249
  SSyncRaftEntry* pEntry = NULL;
16,012✔
250
  bool            takeDummy = false;
16,012✔
251
  int             emptySize = (TSDB_SYNC_LOG_BUFFER_SIZE >> 1);
16,012✔
252

253
  while (true) {
213,750✔
254
    if (index <= pBuf->commitIndex) {
229,762✔
255
      takeDummy = true;
15,991✔
256
      break;
15,991✔
257
    }
258

259
    if (pLogStore->syncLogGetEntry(pLogStore, index, &pEntry) < 0) {
213,771✔
260
      sWarn("vgId:%d, failed to get log entry since %s. index:%" PRId64 "", pNode->vgId, tstrerror(code), index);
2!
261
      break;
×
262
    }
263

264
    bool taken = false;
213,750✔
265
    if (toIndex - index + 1 <= pBuf->size - emptySize) {
213,750✔
266
      SSyncLogBufEntry tmp = {.pItem = pEntry, .prevLogIndex = -1, .prevLogTerm = -1};
213,730✔
267
      pBuf->entries[index % pBuf->size] = tmp;
213,730✔
268
      taken = true;
213,730✔
269
      if (pNode->vgId > 1) {
213,730✔
270
        pBuf->bytes += pEntry->bytes;
213,262✔
271
        (void)atomic_add_fetch_64(&tsLogBufferMemoryUsed, (int64_t)pEntry->bytes);
213,262✔
272
      }
273
    }
274

275
    if (index < toIndex) {
213,772✔
276
      pBuf->entries[(index + 1) % pBuf->size].prevLogIndex = pEntry->index;
211,731✔
277
      pBuf->entries[(index + 1) % pBuf->size].prevLogTerm = pEntry->term;
211,731✔
278
    }
279

280
    if (!taken) {
213,772✔
281
      syncEntryDestroy(pEntry);
22✔
282
      pEntry = NULL;
22✔
283
      break;
22✔
284
    }
285

286
    index--;
213,750✔
287
  }
288

289
  // put a dummy record at commitIndex if present in log buffer
290
  if (takeDummy) {
16,013✔
291
    if (index != pBuf->commitIndex) return TSDB_CODE_SYN_INTERNAL_ERROR;
15,991!
292

293
    SSyncRaftEntry* pDummy = syncEntryBuildDummy(commitTerm, commitIndex, pNode->vgId);
15,991✔
294
    if (pDummy == NULL) {
15,991!
295
      TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY);
×
296
    }
297
    SSyncLogBufEntry tmp = {.pItem = pDummy, .prevLogIndex = commitIndex - 1, .prevLogTerm = commitTerm};
15,991✔
298
    pBuf->entries[(commitIndex + pBuf->size) % pBuf->size] = tmp;
15,991✔
299
    if (pNode->vgId > 1) {
15,991✔
300
      pBuf->bytes += pDummy->bytes;
13,984✔
301
      (void)atomic_add_fetch_64(&tsLogBufferMemoryUsed, (int64_t)pDummy->bytes);
13,984✔
302
    }
303

304
    if (index < toIndex) {
15,991✔
305
      pBuf->entries[(index + 1) % pBuf->size].prevLogIndex = commitIndex;
2,021✔
306
      pBuf->entries[(index + 1) % pBuf->size].prevLogTerm = commitTerm;
2,021✔
307
    }
308
  }
309

310
  // update startIndex
311
  pBuf->startIndex = takeDummy ? index : index + 1;
16,013✔
312

313
  pBuf->isCatchup = false;
16,013✔
314

315
  sInfo("vgId:%d, init sync log buffer. buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 ")", pNode->vgId,
16,013!
316
        pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
317

318
  // validate
319
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
16,013!
320
  return 0;
16,013✔
321

322
_exit:
×
323
  if (code != 0) {
×
324
    sError("vgId:%d, failed to initialize sync log buffer at line %d since %s.", pNode->vgId, lino, tstrerror(code));
×
325
  }
326
  TAOS_RETURN(code);
×
327
}
328

329
int32_t syncLogBufferInit(SSyncLogBuffer* pBuf, SSyncNode* pNode) {
15,902✔
330
  (void)taosThreadMutexLock(&pBuf->mutex);
15,902✔
331
  int32_t ret = syncLogBufferInitWithoutLock(pBuf, pNode);
15,908✔
332
  (void)taosThreadMutexUnlock(&pBuf->mutex);
15,908✔
333
  return ret;
15,908✔
334
}
335

336
int32_t syncLogBufferReInit(SSyncLogBuffer* pBuf, SSyncNode* pNode) {
105✔
337
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
105!
338
  (void)taosThreadMutexLock(&pBuf->mutex);
105✔
339
  for (SyncIndex index = pBuf->startIndex; index < pBuf->endIndex; index++) {
210✔
340
    SSyncRaftEntry* pEntry = pBuf->entries[(index + pBuf->size) % pBuf->size].pItem;
105✔
341
    if (pEntry == NULL) continue;
105!
342
    syncEntryDestroy(pEntry);
105✔
343
    pEntry = NULL;
105✔
344
    (void)memset(&pBuf->entries[(index + pBuf->size) % pBuf->size], 0, sizeof(pBuf->entries[0]));
105✔
345
  }
346
  pBuf->startIndex = pBuf->commitIndex = pBuf->matchIndex = pBuf->endIndex = 0;
105✔
347
  pBuf->bytes = 0;
105✔
348
  int32_t code = syncLogBufferInitWithoutLock(pBuf, pNode);
105✔
349
  if (code < 0) {
105!
350
    sError("vgId:%d, failed to re-initialize sync log buffer since %s.", pNode->vgId, tstrerror(code));
×
351
  }
352
  (void)taosThreadMutexUnlock(&pBuf->mutex);
105✔
353
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
105!
354
  return code;
105✔
355
}
356

357
FORCE_INLINE SyncTerm syncLogBufferGetLastMatchTermWithoutLock(SSyncLogBuffer* pBuf) {
358
  SyncIndex       index = pBuf->matchIndex;
2,783,915✔
359
  SSyncRaftEntry* pEntry = pBuf->entries[(index + pBuf->size) % pBuf->size].pItem;
2,783,915✔
360
  if (pEntry == NULL) {
45,047!
UNCOV
361
    sError("failed to get last match term since entry is null");
×
UNCOV
362
    terrno = TSDB_CODE_SYN_INTERNAL_ERROR;
×
363
    return -1;
×
364
  }
365
  return pEntry->term;
2,783,918✔
366
}
367

368
SyncTerm syncLogBufferGetLastMatchTerm(SSyncLogBuffer* pBuf) {
45,047✔
369
  (void)taosThreadMutexLock(&pBuf->mutex);
45,047✔
370
  SyncTerm term = syncLogBufferGetLastMatchTermWithoutLock(pBuf);
45,047✔
371
  (void)taosThreadMutexUnlock(&pBuf->mutex);
45,047✔
372
  return term;
45,047✔
373
}
374

375
bool syncLogBufferIsEmpty(SSyncLogBuffer* pBuf) {
45,047✔
376
  (void)taosThreadMutexLock(&pBuf->mutex);
45,047✔
377
  bool empty = (pBuf->endIndex <= pBuf->startIndex);
45,047✔
378
  (void)taosThreadMutexUnlock(&pBuf->mutex);
45,047✔
379
  return empty;
45,047✔
380
}
381

382
int32_t syncLogBufferAccept(SSyncLogBuffer* pBuf, SSyncNode* pNode, SSyncRaftEntry* pEntry, SyncTerm prevTerm) {
2,738,865✔
383
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
2,738,865!
384
  (void)taosThreadMutexLock(&pBuf->mutex);
2,738,871✔
385
  int32_t         code = 0;
2,738,868✔
386
  SyncIndex       index = pEntry->index;
2,738,868✔
387
  SyncIndex       prevIndex = pEntry->index - 1;
2,738,868!
388
  SyncTerm        lastMatchTerm = syncLogBufferGetLastMatchTermWithoutLock(pBuf);
2,738,871✔
389
  SSyncRaftEntry* pExist = NULL;
2,738,871✔
390
  bool            inBuf = true;
2,738,871✔
391

392
  if (lastMatchTerm < 0) {
2,738,871!
393
    sError("vgId:%d, failed to accept, lastMatchTerm:%" PRId64, pNode->vgId, lastMatchTerm);
×
394
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
395
    goto _out;
×
396
  }
397

398
  if (index <= pBuf->commitIndex) {
2,738,871✔
399
    sTrace("vgId:%d, already committed. index:%" PRId64 ", term:%" PRId64 ". log buffer: [%" PRId64 " %" PRId64
7,083!
400
           " %" PRId64 ", %" PRId64 ")",
401
           pNode->vgId, pEntry->index, pEntry->term, pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex,
402
           pBuf->endIndex);
403
    SyncTerm term = -1;
7,083✔
404
    code = syncLogReplGetPrevLogTerm(NULL, pNode, index + 1, &term);
7,083✔
405
    if (pEntry->term < 0) {
7,083!
406
      sError("vgId:%d, failed to accept, pEntry->term:%" PRId64, pNode->vgId, pEntry->term);
×
407
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
408
      goto _out;
×
409
    }
410
    if (term == pEntry->term) {
7,083!
411
      code = 0;
7,083✔
412
    }
413
    goto _out;
7,083✔
414
  }
415

416
  if (pNode->raftCfg.cfg.nodeInfo[pNode->raftCfg.cfg.myIndex].nodeRole == TAOS_SYNC_ROLE_LEARNER && index > 0 &&
2,731,788✔
417
      index > pBuf->totalIndex) {
2,301,672✔
418
    pBuf->totalIndex = index;
10,663✔
419
    sTrace("vgId:%d, update learner progress. index:%" PRId64 ", term:%" PRId64 ": prevterm:%" PRId64
10,663!
420
           " != lastmatch:%" PRId64 ". log buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 ")",
421
           pNode->vgId, pEntry->index, pEntry->term, prevTerm, lastMatchTerm, pBuf->startIndex, pBuf->commitIndex,
422
           pBuf->matchIndex, pBuf->endIndex);
423
  }
424

425
  if (index - pBuf->startIndex >= pBuf->size) {
2,731,779✔
426
    sWarn("vgId:%d, out of buffer range. index:%" PRId64 ", term:%" PRId64 ". log buffer: [%" PRId64 " %" PRId64
20!
427
          " %" PRId64 ", %" PRId64 ")",
428
          pNode->vgId, pEntry->index, pEntry->term, pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex,
429
          pBuf->endIndex);
430
    code = TSDB_CODE_OUT_OF_RANGE;
20✔
431
    goto _out;
20✔
432
  }
433

434
  if (index > pBuf->matchIndex && lastMatchTerm != prevTerm) {
2,731,759✔
435
    sWarn("vgId:%d, not ready to accept. index:%" PRId64 ", term:%" PRId64 ": prevterm:%" PRId64
653!
436
          " != lastmatch:%" PRId64 ". log buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 ")",
437
          pNode->vgId, pEntry->index, pEntry->term, prevTerm, lastMatchTerm, pBuf->startIndex, pBuf->commitIndex,
438
          pBuf->matchIndex, pBuf->endIndex);
439
    code = TSDB_CODE_ACTION_IN_PROGRESS;
653✔
440
    goto _out;
653✔
441
  }
442

443
  // check current in buffer
444
  code = syncLogBufferGetOneEntry(pBuf, pNode, index, &inBuf, &pExist);
2,731,106✔
445
  if (pExist != NULL) {
2,731,109✔
446
    if (pEntry->index != pExist->index) {
2,875!
447
      sError("vgId:%d, failed to accept, pEntry->index:%" PRId64 ", pExist->index:%" PRId64, pNode->vgId, pEntry->index,
×
448
             pExist->index);
449
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
450
      goto _out;
×
451
    }
452
    if (pEntry->term != pExist->term) {
2,875✔
453
      TAOS_CHECK_GOTO(syncLogBufferRollback(pBuf, pNode, index), NULL, _out);
3!
454
    } else {
455
      sTrace("vgId:%d, duplicate log entry received. index:%" PRId64 ", term:%" PRId64 ". log buffer: [%" PRId64
2,872!
456
             " %" PRId64 " %" PRId64 ", %" PRId64 ")",
457
             pNode->vgId, pEntry->index, pEntry->term, pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex,
458
             pBuf->endIndex);
459
      SyncTerm existPrevTerm = -1;
2,872✔
460
      TAOS_CHECK_GOTO(syncLogReplGetPrevLogTerm(NULL, pNode, index, &existPrevTerm), NULL, _out);
2,872✔
461
      if (!(pEntry->term == pExist->term && (pEntry->index > pBuf->matchIndex || prevTerm == existPrevTerm))) {
2,835!
462
        sError("vgId:%d, failed to accept, pEntry->term:%" PRId64 ", pExist->indexpExist->term:%" PRId64
×
463
               ", pEntry->index:%" PRId64 ", pBuf->matchIndex:%" PRId64 ", prevTerm:%" PRId64
464
               ", existPrevTerm:%" PRId64,
465
               pNode->vgId, pEntry->term, pExist->term, pEntry->index, pBuf->matchIndex, prevTerm, existPrevTerm);
466
        code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
467
        goto _out;
×
468
      }
469
      code = 0;
2,835✔
470
      goto _out;
2,835✔
471
    }
472
  }
473

474
  // update
475
  if (!(pBuf->startIndex < index)) {
2,728,237!
476
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
477
    goto _out;
×
478
  };
479
  if (!(index - pBuf->startIndex < pBuf->size)) {
2,728,237!
480
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
481
    goto _out;
×
482
  }
483
  if (pBuf->entries[index % pBuf->size].pItem != NULL) {
2,728,237!
484
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
485
    goto _out;
×
486
  }
487
  SSyncLogBufEntry tmp = {.pItem = pEntry, .prevLogIndex = prevIndex, .prevLogTerm = prevTerm};
2,728,237✔
488
  pBuf->entries[index % pBuf->size] = tmp;
2,728,237✔
489
  if (pNode->vgId > 1) {
2,728,237✔
490
    pBuf->bytes += pEntry->bytes;
2,719,162✔
491
    (void)atomic_add_fetch_64(&tsLogBufferMemoryUsed, (int64_t)pEntry->bytes);
2,719,162✔
492
  }
493
  pEntry = NULL;
2,728,243✔
494

495
  // update end index
496
  pBuf->endIndex = TMAX(index + 1, pBuf->endIndex);
2,728,243✔
497

498
  // success
499
  code = 0;
2,728,243✔
500

501
_out:
2,738,871✔
502
  syncEntryDestroy(pEntry);
2,738,871✔
503
  if (!inBuf) {
2,738,871!
504
    syncEntryDestroy(pExist);
×
505
    pExist = NULL;
×
506
  }
507
  (void)taosThreadMutexUnlock(&pBuf->mutex);
2,738,871✔
508
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
2,738,871!
509
  TAOS_RETURN(code);
2,738,870✔
510
}
511

512
static inline bool syncLogStoreNeedFlush(SSyncRaftEntry* pEntry, int32_t replicaNum) {
13,260,489✔
513
  return (replicaNum > 1) && (pEntry->originalRpcType == TDMT_VND_COMMIT);
13,260,489✔
514
}
515

516
int32_t syncLogStorePersist(SSyncLogStore* pLogStore, SSyncNode* pNode, SSyncRaftEntry* pEntry) {
13,260,383✔
517
  int32_t code = 0;
13,260,383✔
518
  if (pEntry->index < 0) return TSDB_CODE_SYN_INTERNAL_ERROR;
13,260,383!
519
  SyncIndex lastVer = pLogStore->syncLogLastIndex(pLogStore);
13,260,383✔
520
  if (lastVer >= pEntry->index && (code = pLogStore->syncLogTruncate(pLogStore, pEntry->index)) < 0) {
13,260,723!
521
    sError("failed to truncate log store since %s. from index:%" PRId64 "", tstrerror(code), pEntry->index);
×
522
    TAOS_RETURN(code);
×
523
  }
524
  lastVer = pLogStore->syncLogLastIndex(pLogStore);
13,260,723✔
525
  if (pEntry->index != lastVer + 1) return TSDB_CODE_SYN_INTERNAL_ERROR;
13,260,721!
526

527
  bool doFsync = syncLogStoreNeedFlush(pEntry, pNode->replicaNum);
13,260,721✔
528
  if ((code = pLogStore->syncLogAppendEntry(pLogStore, pEntry, doFsync)) < 0) {
13,260,539!
529
    sError("failed to append sync log entry since %s. index:%" PRId64 ", term:%" PRId64 "", tstrerror(code),
×
530
           pEntry->index, pEntry->term);
531
    TAOS_RETURN(code);
×
532
  }
533

534
  lastVer = pLogStore->syncLogLastIndex(pLogStore);
13,260,517✔
535
  if (pEntry->index != lastVer) return TSDB_CODE_SYN_INTERNAL_ERROR;
13,260,601!
536
  return 0;
13,260,601✔
537
}
538

539
int64_t syncLogBufferProceed(SSyncLogBuffer* pBuf, SSyncNode* pNode, SyncTerm* pMatchTerm, char* str) {
13,271,114✔
540
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
13,271,114!
541
  (void)taosThreadMutexLock(&pBuf->mutex);
13,271,215✔
542

543
  SSyncLogStore* pLogStore = pNode->pLogStore;
13,271,476✔
544
  int64_t        matchIndex = pBuf->matchIndex;
13,271,476✔
545
  int32_t        code = 0;
13,271,476✔
546

547
  while (pBuf->matchIndex + 1 < pBuf->endIndex) {
26,532,056✔
548
    int64_t index = pBuf->matchIndex + 1;
13,467,980✔
549
    if (index < 0) {
13,467,980!
550
      sError("vgId:%d, failed to proceed index:%" PRId64, pNode->vgId, index);
×
551
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
552
      goto _out;
×
553
    }
554

555
    // try to proceed
556
    SSyncLogBufEntry* pBufEntry = &pBuf->entries[index % pBuf->size];
13,467,980✔
557
    SyncIndex         prevLogIndex = pBufEntry->prevLogIndex;
13,467,980✔
558
    SyncTerm          prevLogTerm = pBufEntry->prevLogTerm;
13,467,980✔
559
    SSyncRaftEntry*   pEntry = pBufEntry->pItem;
13,467,980✔
560
    if (pEntry == NULL) {
13,467,980✔
561
      sTrace("vgId:%d, cannot proceed match index in log buffer. no raft entry at next pos of matchIndex:%" PRId64,
207,337!
562
             pNode->vgId, pBuf->matchIndex);
563
      goto _out;
207,337✔
564
    }
565

566
    if (index != pEntry->index) {
13,260,643!
567
      sError("vgId:%d, failed to proceed index:%" PRId64 ", pEntry->index:%" PRId64, pNode->vgId, index, pEntry->index);
×
568
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
569
      goto _out;
×
570
    }
571

572
    // match
573
    SSyncRaftEntry* pMatch = pBuf->entries[(pBuf->matchIndex + pBuf->size) % pBuf->size].pItem;
13,260,643✔
574
    if (pMatch == NULL) {
13,260,643!
575
      sError("vgId:%d, failed to proceed since pMatch is null", pNode->vgId);
×
576
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
577
      goto _out;
×
578
    }
579
    if (pMatch->index != pBuf->matchIndex) {
13,260,643!
580
      sError("vgId:%d, failed to proceed, pMatch->index:%" PRId64 ", pBuf->matchIndex:%" PRId64, pNode->vgId,
×
581
             pMatch->index, pBuf->matchIndex);
582
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
583
      goto _out;
×
584
    }
585
    if (pMatch->index + 1 != pEntry->index) {
13,260,643!
586
      sError("vgId:%d, failed to proceed, pMatch->index:%" PRId64 ", pEntry->index:%" PRId64, pNode->vgId,
×
587
             pMatch->index, pEntry->index);
588
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
589
      goto _out;
×
590
    }
591
    if (prevLogIndex != pMatch->index) {
13,260,643!
592
      sError("vgId:%d, failed to proceed, prevLogIndex:%" PRId64 ", pMatch->index:%" PRId64, pNode->vgId, prevLogIndex,
×
593
             pMatch->index);
594
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
595
      goto _out;
×
596
    }
597

598
    if (pMatch->term != prevLogTerm) {
13,260,643!
599
      sInfo(
×
600
          "vgId:%d, mismatching sync log entries encountered. "
601
          "{ index:%" PRId64 ", term:%" PRId64
602
          " } "
603
          "{ index:%" PRId64 ", term:%" PRId64 ", prevLogIndex:%" PRId64 ", prevLogTerm:%" PRId64 " } ",
604
          pNode->vgId, pMatch->index, pMatch->term, pEntry->index, pEntry->term, prevLogIndex, prevLogTerm);
605
      goto _out;
×
606
    }
607

608
    // increase match index
609
    pBuf->matchIndex = index;
13,260,643✔
610

611
    sTrace("vgId:%d, log buffer proceed. start index:%" PRId64 ", match index:%" PRId64 ", end index:%" PRId64,
13,260,643✔
612
           pNode->vgId, pBuf->startIndex, pBuf->matchIndex, pBuf->endIndex);
613

614
    // persist
615
    if ((code = syncLogStorePersist(pLogStore, pNode, pEntry)) < 0) {
13,260,643!
616
      sError("vgId:%d, failed to persist sync log entry from buffer since %s. index:%" PRId64, pNode->vgId,
×
617
             tstrerror(code), pEntry->index);
618
      taosMsleep(1);
×
619
      goto _out;
×
620
    }
621

622
    if (pEntry->originalRpcType == TDMT_SYNC_CONFIG_CHANGE) {
13,260,505!
623
      if (pNode->pLogBuf->commitIndex == pEntry->index - 1) {
×
624
        sInfo(
×
625
            "vgId:%d, to change config at %s. "
626
            "current entry, index:%" PRId64 ", term:%" PRId64
627
            ", "
628
            "node, restore:%d, commitIndex:%" PRId64
629
            ", "
630
            "cond: (pre entry index:%" PRId64 "== buf commit index:%" PRId64 ")",
631
            pNode->vgId, str, pEntry->index, pEntry->term, pNode->restoreFinish, pNode->commitIndex, pEntry->index - 1,
632
            pNode->pLogBuf->commitIndex);
633
        if ((code = syncNodeChangeConfig(pNode, pEntry, str)) != 0) {
×
634
          sError("vgId:%d, failed to change config from Append since %s. index:%" PRId64, pNode->vgId, tstrerror(code),
×
635
                 pEntry->index);
636
          goto _out;
×
637
        }
638
      } else {
639
        sInfo(
×
640
            "vgId:%d, delay change config from Node %s. "
641
            "curent entry, index:%" PRId64 ", term:%" PRId64
642
            ", "
643
            "node, commitIndex:%" PRId64 ",  pBuf: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64
644
            "), "
645
            "cond:( pre entry index:%" PRId64 " != buf commit index:%" PRId64 ")",
646
            pNode->vgId, str, pEntry->index, pEntry->term, pNode->commitIndex, pNode->pLogBuf->startIndex,
647
            pNode->pLogBuf->commitIndex, pNode->pLogBuf->matchIndex, pNode->pLogBuf->endIndex, pEntry->index - 1,
648
            pNode->pLogBuf->commitIndex);
649
      }
650
    }
651

652
    // replicate on demand
653
    if ((code = syncNodeReplicateWithoutLock(pNode)) != 0) {
13,260,505!
654
      sError("vgId:%d, failed to replicate since %s. index:%" PRId64, pNode->vgId, tstrerror(code), pEntry->index);
×
655
      goto _out;
×
656
    }
657

658
    if (pEntry->index != pBuf->matchIndex) {
13,260,559!
659
      sError("vgId:%d, failed to proceed, pEntry->index:%" PRId64 ", pBuf->matchIndex:%" PRId64, pNode->vgId,
×
660
             pEntry->index, pBuf->matchIndex);
661
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
662
      goto _out;
×
663
    }
664

665
    // update my match index
666
    matchIndex = pBuf->matchIndex;
13,260,559✔
667
    syncIndexMgrSetIndex(pNode->pMatchIndex, &pNode->myRaftId, pBuf->matchIndex);
13,260,559✔
668
  }  // end of while
669

670
_out:
13,064,076✔
671
  pBuf->matchIndex = matchIndex;
13,271,413✔
672
  if (pMatchTerm) {
13,271,413✔
673
    *pMatchTerm = pBuf->entries[(matchIndex + pBuf->size) % pBuf->size].pItem->term;
2,738,873✔
674
  }
675
  (void)taosThreadMutexUnlock(&pBuf->mutex);
13,271,413✔
676
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
13,271,279!
677
  return matchIndex;
13,271,293✔
678
}
679

680
int32_t syncFsmExecute(SSyncNode* pNode, SSyncFSM* pFsm, ESyncState role, SyncTerm term, SSyncRaftEntry* pEntry,
13,519,546✔
681
                       int32_t applyCode, bool force) {
682
  // learner need to execute fsm when it catch up entry log
683
  // if force is true, keep all contition check to execute fsm
684
  if (pNode->replicaNum == 1 && pNode->restoreFinish && pNode->vgId != 1 &&
13,519,546✔
685
      pNode->raftCfg.cfg.nodeInfo[pNode->raftCfg.cfg.myIndex].nodeRole != TAOS_SYNC_ROLE_LEARNER && force == false) {
10,297,941!
686
    sDebug("vgId:%d, not to execute fsm, index:%" PRId64 ", term:%" PRId64
10,270,371!
687
           ", type:%s code:0x%x, replicaNum:%d,"
688
           "role:%d, restoreFinish:%d",
689
           pNode->vgId, pEntry->index, pEntry->term, TMSG_INFO(pEntry->originalRpcType), applyCode, pNode->replicaNum,
690
           pNode->raftCfg.cfg.nodeInfo[pNode->raftCfg.cfg.myIndex].nodeRole, pNode->restoreFinish);
691
    return 0;
10,270,346✔
692
  }
693

694
  if (pNode->vgId != 1 && syncIsMsgBlock(pEntry->originalRpcType)) {
3,249,175✔
695
    sTrace("vgId:%d, blocking msg ready to execute, index:%" PRId64 ", term:%" PRId64 ", type:%s code:0x%x",
18,343!
696
           pNode->vgId, pEntry->index, pEntry->term, TMSG_INFO(pEntry->originalRpcType), applyCode);
697
  }
698

699
  if (pEntry->originalRpcType == TDMT_VND_COMMIT) {
3,249,183✔
700
    sInfo("vgId:%d, fsm execute vnode commit. index:%" PRId64 ", term:%" PRId64 "", pNode->vgId, pEntry->index,
1,422!
701
          pEntry->term);
702
  }
703

704
  int32_t code = 0, lino = 0;
3,249,586✔
705
  bool    retry = false;
3,249,586✔
706
  do {
707
    SFsmCbMeta cbMeta = {0};
3,249,586✔
708
    cbMeta.lastConfigIndex = syncNodeGetSnapshotConfigIndex(pNode, pEntry->index);
3,249,586✔
709
    if (cbMeta.lastConfigIndex < -1) {
3,249,595!
710
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
711
      if (terrno != 0) code = terrno;
×
712
      return code;
×
713
    }
714

715
    SRpcMsg rpcMsg = {.code = applyCode};
3,249,595✔
716
    TAOS_CHECK_EXIT(syncEntry2OriginalRpc(pEntry, &rpcMsg));
3,249,595!
717

718
    cbMeta.index = pEntry->index;
3,249,547✔
719
    cbMeta.isWeak = pEntry->isWeak;
3,249,547✔
720
    cbMeta.code = applyCode;
3,249,547✔
721
    cbMeta.state = role;
3,249,547✔
722
    cbMeta.seqNum = pEntry->seqNum;
3,249,547✔
723
    cbMeta.term = pEntry->term;
3,249,547✔
724
    cbMeta.currentTerm = term;
3,249,547✔
725
    cbMeta.flag = -1;
3,249,547✔
726

727
    int32_t num = syncRespMgrGetAndDel(pNode->pSyncRespMgr, cbMeta.seqNum, &rpcMsg.info);
3,249,547✔
728
    sDebug("vgId:%d, get response info,  seqNum:%" PRId64 ", num:%d", pNode->vgId, cbMeta.seqNum, num);
3,249,624✔
729
    code = pFsm->FpCommitCb(pFsm, &rpcMsg, &cbMeta);
3,249,624✔
730
    retry = (code != 0) && (terrno == TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE);
3,249,613!
731
    sDebug("vgId:%d, fsm execute, index:%" PRId64 ", term:%" PRId64 ", type:%s, code:%d, retry:%d", pNode->vgId,
3,249,613!
732
           pEntry->index, pEntry->term, TMSG_INFO(pEntry->originalRpcType), code, retry);
733
    if (retry) {
3,249,613!
734
      taosMsleep(10);
×
735
      sError("vgId:%d, retry on fsm commit since %s. index:%" PRId64, pNode->vgId, tstrerror(code), pEntry->index);
×
736
    }
737
  } while (retry);
3,249,612!
738

739
_exit:
3,249,612✔
740
  if (code < 0) {
3,249,612✔
741
    sError("vgId:%d, failed to execute fsm at line %d since %s. index:%" PRId64 ", term:%" PRId64 ", type:%s",
885!
742
           pNode->vgId, lino, tstrerror(code), pEntry->index, pEntry->term, TMSG_INFO(pEntry->originalRpcType));
743
  }
744
  return code;
3,249,613✔
745
}
746

747
int32_t syncLogBufferValidate(SSyncLogBuffer* pBuf) {
84,578,349✔
748
  if (pBuf->startIndex > pBuf->matchIndex) {
84,578,349!
749
    sError("failed to validate, pBuf->startIndex:%" PRId64 ", pBuf->matchIndex:%" PRId64, pBuf->startIndex,
×
750
           pBuf->matchIndex);
751
    return TSDB_CODE_SYN_INTERNAL_ERROR;
×
752
  }
753
  if (pBuf->commitIndex > pBuf->matchIndex) {
84,578,349!
754
    sError("failed to validate, pBuf->commitIndex:%" PRId64 ", pBuf->matchIndex:%" PRId64, pBuf->commitIndex,
×
755
           pBuf->matchIndex);
756
    return TSDB_CODE_SYN_INTERNAL_ERROR;
×
757
  }
758
  if (pBuf->matchIndex >= pBuf->endIndex) {
84,578,349!
759
    sError("failed to validate, pBuf->matchIndex:%" PRId64 ", pBuf->endIndex:%" PRId64, pBuf->matchIndex,
×
760
           pBuf->endIndex);
761
    return TSDB_CODE_SYN_INTERNAL_ERROR;
×
762
  }
763
  if (pBuf->endIndex - pBuf->startIndex > pBuf->size) {
84,578,349!
764
    sError("failed to validate, pBuf->endIndex:%" PRId64 ", pBuf->startIndex:%" PRId64 ", pBuf->size:%" PRId64,
×
765
           pBuf->endIndex, pBuf->startIndex, pBuf->size);
766
    return TSDB_CODE_SYN_INTERNAL_ERROR;
×
767
  }
768
  if (pBuf->entries[(pBuf->matchIndex + pBuf->size) % pBuf->size].pItem == NULL) {
84,578,349!
769
    sError("failed to validate since pItem is null");
×
770
    return TSDB_CODE_SYN_INTERNAL_ERROR;
×
771
  }
772
  return 0;
84,578,349✔
773
}
774

775
int32_t syncLogBufferCommit(SSyncLogBuffer* pBuf, SSyncNode* pNode, int64_t commitIndex) {
15,722,063✔
776
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
15,722,063!
777
  (void)taosThreadMutexLock(&pBuf->mutex);
15,722,285✔
778

779
  SSyncLogStore*  pLogStore = pNode->pLogStore;
15,722,229✔
780
  SSyncFSM*       pFsm = pNode->pFsm;
15,722,229✔
781
  ESyncState      role = pNode->state;
15,722,229✔
782
  SyncTerm        currentTerm = raftStoreGetTerm(pNode);
15,722,229✔
783
  SyncGroupId     vgId = pNode->vgId;
15,722,467✔
784
  int32_t         code = 0;
15,722,467✔
785
  int64_t         upperIndex = TMIN(commitIndex, pBuf->matchIndex);
15,722,467✔
786
  SSyncRaftEntry* pEntry = NULL;
15,722,467✔
787
  bool            inBuf = false;
15,722,467✔
788
  SSyncRaftEntry* pNextEntry = NULL;
15,722,467✔
789
  bool            nextInBuf = false;
15,722,467✔
790

791
  if (commitIndex <= pBuf->commitIndex) {
15,722,467✔
792
    sDebug("vgId:%d, stale commit index. current:%" PRId64 ", notified:%" PRId64 "", vgId, pBuf->commitIndex,
2,659,082✔
793
           commitIndex);
794
    goto _out;
2,659,082✔
795
  }
796

797
  sTrace("vgId:%d, commit. log buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 "), role:%d, term:%" PRId64,
13,063,385✔
798
         pNode->vgId, pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex, role, currentTerm);
799

800
  // execute in fsm
801
  for (int64_t index = pBuf->commitIndex + 1; index <= upperIndex; index++) {
26,582,155✔
802
    // get a log entry
803
    code = syncLogBufferGetOneEntry(pBuf, pNode, index, &inBuf, &pEntry);
13,519,563✔
804
    if (pEntry == NULL) {
13,519,862!
805
      goto _out;
×
806
    }
807

808
    // execute it
809
    if (!syncUtilUserCommit(pEntry->originalRpcType)) {
13,519,862✔
810
      sInfo("vgId:%d, commit sync barrier. index:%" PRId64 ", term:%" PRId64 ", type:%s", vgId, pEntry->index,
29,777!
811
            pEntry->term, TMSG_INFO(pEntry->originalRpcType));
812
    }
813

814
    if ((code = syncFsmExecute(pNode, pFsm, role, currentTerm, pEntry, 0, false)) != 0) {
13,519,574✔
815
      sError("vgId:%d, failed to execute sync log entry. index:%" PRId64 ", term:%" PRId64
885!
816
             ", role:%d, current term:%" PRId64,
817
             vgId, pEntry->index, pEntry->term, role, currentTerm);
818
      goto _out;
885✔
819
    }
820
    pBuf->commitIndex = index;
13,519,068✔
821

822
    sTrace("vgId:%d, committed index:%" PRId64 ", term:%" PRId64 ", role:%d, current term:%" PRId64 "", pNode->vgId,
13,519,068✔
823
           pEntry->index, pEntry->term, role, currentTerm);
824

825
    code = syncLogBufferGetOneEntry(pBuf, pNode, index + 1, &nextInBuf, &pNextEntry);
13,519,068✔
826
    if (pNextEntry != NULL) {
13,519,034✔
827
      if (pNextEntry->originalRpcType == TDMT_SYNC_CONFIG_CHANGE) {
881,847!
828
        sInfo(
×
829
            "vgId:%d, to change config at Commit. "
830
            "current entry, index:%" PRId64 ", term:%" PRId64
831
            ", "
832
            "node, role:%d, current term:%" PRId64
833
            ", restore:%d, "
834
            "cond, next entry index:%" PRId64 ", msgType:%s",
835
            vgId, pEntry->index, pEntry->term, role, currentTerm, pNode->restoreFinish, pNextEntry->index,
836
            TMSG_INFO(pNextEntry->originalRpcType));
837

838
        if ((code = syncNodeChangeConfig(pNode, pNextEntry, "Commit")) != 0) {
×
839
          sError("vgId:%d, failed to change config from Commit. index:%" PRId64 ", term:%" PRId64
×
840
                 ", role:%d, current term:%" PRId64,
841
                 vgId, pNextEntry->index, pNextEntry->term, role, currentTerm);
842
          goto _out;
×
843
        }
844

845
        // for 2->1, need to apply config change entry in sync thread,
846
        if (pNode->replicaNum == 1) {
×
847
          if ((code = syncFsmExecute(pNode, pFsm, role, currentTerm, pNextEntry, 0, true)) != 0) {
×
848
            sError("vgId:%d, failed to execute sync log entry. index:%" PRId64 ", term:%" PRId64
×
849
                   ", role:%d, current term:%" PRId64,
850
                   vgId, pNextEntry->index, pNextEntry->term, role, currentTerm);
851
            goto _out;
×
852
          }
853

854
          index++;
×
855
          pBuf->commitIndex = index;
×
856

857
          sTrace("vgId:%d, committed index:%" PRId64 ", term:%" PRId64 ", role:%d, current term:%" PRId64 "",
×
858
                 pNode->vgId, pNextEntry->index, pNextEntry->term, role, currentTerm);
859
        }
860
      }
861
      if (!nextInBuf) {
881,581✔
862
        syncEntryDestroy(pNextEntry);
47,388✔
863
        pNextEntry = NULL;
47,388✔
864
      }
865
    }
866

867
    if (!inBuf) {
13,518,768✔
868
      syncEntryDestroy(pEntry);
47,410✔
869
      pEntry = NULL;
47,410✔
870
    }
871
  }
872

873
  // recycle
874
  bool      isVnode = pNode->vgId > 1;
13,062,592✔
875
  SyncIndex until = pBuf->commitIndex - TSDB_SYNC_LOG_BUFFER_RETENTION;
13,062,592✔
876
  do {
12,611,200✔
877
    if ((pBuf->startIndex >= pBuf->commitIndex) ||
25,673,792✔
878
        !((pBuf->startIndex < until) || (isVnode && pBuf->bytes >= TSDB_SYNC_LOG_BUFFER_THRESHOLD &&
25,673,439✔
879
                                         atomic_load_64(&tsLogBufferMemoryUsed) >= tsLogBufferMemoryAllowed))) {
103,321✔
880
      break;
881
    }
882
    SSyncRaftEntry* pEntry = pBuf->entries[(pBuf->startIndex + pBuf->size) % pBuf->size].pItem;
12,611,157✔
883
    if (pEntry == NULL) {
12,611,157!
884
      sError("vgId:%d, invalid log entry to recycle. index:%" PRId64 ", startIndex:%" PRId64 ", until:%" PRId64
×
885
             ", commitIndex:%" PRId64 ", endIndex:%" PRId64 ", term:%" PRId64,
886
             pNode->vgId, pEntry->index, pBuf->startIndex, until, pBuf->commitIndex, pBuf->endIndex, pEntry->term);
887
      return TSDB_CODE_SYN_INTERNAL_ERROR;
×
888
    }
889
    if (isVnode) {
12,611,157✔
890
      pBuf->bytes -= pEntry->bytes;
12,581,338✔
891
      (void)atomic_sub_fetch_64(&tsLogBufferMemoryUsed, (int64_t)pEntry->bytes);
12,581,338✔
892
    }
893
    sDebug("vgId:%d, recycle log entry. index:%" PRId64 ", startIndex:%" PRId64 ", until:%" PRId64
12,611,152✔
894
           ", commitIndex:%" PRId64 ", endIndex:%" PRId64 ", term:%" PRId64 ", entry bytes:%u, buf bytes:%" PRId64
895
           ", used:%" PRId64 ", allowed:%" PRId64,
896
           pNode->vgId, pEntry->index, pBuf->startIndex, until, pBuf->commitIndex, pBuf->endIndex, pEntry->term,
897
           pEntry->bytes, pBuf->bytes, atomic_load_64(&tsLogBufferMemoryUsed), tsLogBufferMemoryAllowed);
898
    syncEntryDestroy(pEntry);
12,611,152✔
899
    (void)memset(&pBuf->entries[(pBuf->startIndex + pBuf->size) % pBuf->size], 0, sizeof(pBuf->entries[0]));
12,611,200✔
900
    ++pBuf->startIndex;
12,611,200✔
901
  } while (true);
902

903
  code = 0;
13,062,635✔
904
_out:
15,722,602✔
905
  // mark as restored if needed
906
  if (!pNode->restoreFinish && pBuf->commitIndex >= pNode->commitIndex && pEntry != NULL &&
15,722,602✔
907
      currentTerm <= pEntry->term) {
2,229,894✔
908
    pNode->pFsm->FpRestoreFinishCb(pNode->pFsm, pBuf->commitIndex);
15,754✔
909
    pNode->restoreFinish = true;
15,754✔
910
    sInfo("vgId:%d, restore finished. term:%" PRId64 ", log buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 ")",
15,754!
911
          pNode->vgId, currentTerm, pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
912
  }
913

914
  if (!inBuf) {
15,722,602✔
915
    syncEntryDestroy(pEntry);
2,659,080✔
916
    pEntry = NULL;
2,659,082✔
917
  }
918
  if (!nextInBuf) {
15,722,604✔
919
    syncEntryDestroy(pNextEntry);
15,160,347✔
920
    pNextEntry = NULL;
15,160,473✔
921
  }
922
  (void)taosThreadMutexUnlock(&pBuf->mutex);
15,722,730✔
923
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
15,722,498!
924
  TAOS_RETURN(code);
15,722,499✔
925
}
926

927
void syncLogReplReset(SSyncLogReplMgr* pMgr) {
42,598✔
928
  if (pMgr == NULL) return;
42,598!
929

930
  if (pMgr->startIndex < 0) {
42,598!
931
    sError("failed to reset, pMgr->startIndex:%" PRId64, pMgr->startIndex);
×
932
    return;
×
933
  }
934
  for (SyncIndex index = pMgr->startIndex; index < pMgr->endIndex; index++) {
53,188✔
935
    (void)memset(&pMgr->states[index % pMgr->size], 0, sizeof(pMgr->states[0]));
10,590✔
936
  }
937
  pMgr->startIndex = 0;
42,598✔
938
  pMgr->matchIndex = 0;
42,598✔
939
  pMgr->endIndex = 0;
42,598✔
940
  pMgr->restored = false;
42,598✔
941
  pMgr->retryBackoff = 0;
42,598✔
942
}
943

944
int32_t syncLogReplRetryOnNeed(SSyncLogReplMgr* pMgr, SSyncNode* pNode) {
3,157,342✔
945
  if (pMgr->endIndex <= pMgr->startIndex) {
3,157,342!
946
    return 0;
×
947
  }
948

949
  SRaftId* pDestId = &pNode->replicasId[pMgr->peerId];
3,157,342✔
950
  if (pMgr->retryBackoff == SYNC_MAX_RETRY_BACKOFF) {
3,157,342✔
951
    syncLogReplReset(pMgr);
19✔
952
    sWarn("vgId:%d, reset sync log repl since retry backoff exceeding limit. peer:%" PRIx64, pNode->vgId,
19!
953
          pDestId->addr);
954
    return TSDB_CODE_OUT_OF_RANGE;
19✔
955
  }
956

957
  int32_t  code = 0;
3,157,323✔
958
  bool     retried = false;
3,157,323✔
959
  int64_t  retryWaitMs = syncLogReplGetRetryBackoffTimeMs(pMgr);
3,157,323✔
960
  int64_t  nowMs = taosGetMonoTimestampMs();
3,157,323✔
961
  int      count = 0;
3,157,323✔
962
  int64_t  firstIndex = -1;
3,157,323✔
963
  SyncTerm term = -1;
3,157,323✔
964
  int64_t  batchSize = TMAX(1, pMgr->size >> (4 + pMgr->retryBackoff));
3,157,323✔
965

966
  for (SyncIndex index = pMgr->startIndex; index < pMgr->endIndex; index++) {
3,192,990✔
967
    int64_t pos = index % pMgr->size;
3,185,149✔
968
    if (!(!pMgr->states[pos].barrier || (index == pMgr->startIndex || index + 1 == pMgr->endIndex))) {
3,185,149!
969
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
970
      goto _out;
1✔
971
    }
972

973
    if (nowMs < pMgr->states[pos].timeMs + retryWaitMs) {
3,185,149✔
974
      break;
3,149,479✔
975
    }
976

977
    if (pMgr->states[pos].acked) {
35,727✔
978
      if (pMgr->matchIndex < index && pMgr->states[pos].timeMs + (syncGetRetryMaxWaitMs() << 3) < nowMs) {
25,411✔
979
        syncLogReplReset(pMgr);
1✔
980
        sWarn("vgId:%d, reset sync log repl since stagnation. index:%" PRId64 ", peer:%" PRIx64, pNode->vgId, index,
1!
981
              pDestId->addr);
982
        code = TSDB_CODE_ACTION_IN_PROGRESS;
1✔
983
        goto _out;
1✔
984
      }
985
      continue;
24,785✔
986
    }
987

988
    bool barrier = false;
10,941✔
989
    if ((code = syncLogReplSendTo(pMgr, pNode, index, &term, pDestId, &barrier)) < 0) {
10,941!
990
      sError("vgId:%d, failed to replicate sync log entry since %s. index:%" PRId64 ", dest:%" PRIx64 "", pNode->vgId,
×
991
             tstrerror(code), index, pDestId->addr);
992
      goto _out;
×
993
    }
994
    if (barrier != pMgr->states[pos].barrier) {
10,939!
995
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
996
      goto _out;
×
997
    }
998
    pMgr->states[pos].timeMs = nowMs;
10,939✔
999
    pMgr->states[pos].term = term;
10,939✔
1000
    pMgr->states[pos].acked = false;
10,939✔
1001

1002
    retried = true;
10,939✔
1003
    if (firstIndex == -1) firstIndex = index;
10,939✔
1004

1005
    if (batchSize <= count++) {
10,939✔
1006
      break;
57✔
1007
    }
1008
  }
1009

1010
_out:
7,841✔
1011
  if (retried) {
3,157,321✔
1012
    pMgr->retryBackoff = syncLogReplGetNextRetryBackoff(pMgr);
232✔
1013
    SSyncLogBuffer* pBuf = pNode->pLogBuf;
232✔
1014
    sInfo("vgId:%d, resend %d sync log entries. dest:%" PRIx64 ", indexes:%" PRId64 " ..., terms: ... %" PRId64
232!
1015
          ", retryWaitMs:%" PRId64 ", repl-mgr:[%" PRId64 " %" PRId64 ", %" PRId64 "), buffer: [%" PRId64 " %" PRId64
1016
          " %" PRId64 ", %" PRId64 ")",
1017
          pNode->vgId, count, pDestId->addr, firstIndex, term, retryWaitMs, pMgr->startIndex, pMgr->matchIndex,
1018
          pMgr->endIndex, pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
1019
  }
1020
  TAOS_RETURN(code);
3,157,321✔
1021
}
1022

1023
int32_t syncLogReplRecover(SSyncLogReplMgr* pMgr, SSyncNode* pNode, SyncAppendEntriesReply* pMsg) {
4,692✔
1024
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
4,692✔
1025
  SRaftId         destId = pMsg->srcId;
4,692✔
1026
  int32_t         code = 0;
4,692✔
1027
  if (pMgr->restored != false) return TSDB_CODE_SYN_INTERNAL_ERROR;
4,692!
1028

1029
  sTrace("vgId:%d, begin to recover sync log repl. peer: dnode:%d (%" PRIx64 "), repl-mgr:[%" PRId64 ", %" PRId64
4,692!
1030
         ", %" PRId64 ") restore:%d, buffer: [%" PRId64 ", %" PRId64 ", %" PRId64 ", %" PRId64
1031
         "), msg: {lastSendIndex:%" PRId64 ", matchIndex:%" PRId64 ", fsmState:%d, success:%d, lastMatchTerm:%" PRId64
1032
         "}",
1033
         pNode->vgId, DID(&destId), destId.addr, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex, pMgr->restored,
1034
         pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex, pMsg->lastSendIndex, pMsg->matchIndex,
1035
         pMsg->fsmState, pMsg->success, pMsg->lastMatchTerm);
1036

1037
  if (pMgr->endIndex == 0) {
4,692✔
1038
    if (pMgr->startIndex != 0) return TSDB_CODE_SYN_INTERNAL_ERROR;
2,198!
1039
    if (pMgr->matchIndex != 0) return TSDB_CODE_SYN_INTERNAL_ERROR;
2,198!
1040
    if (pMsg->matchIndex < 0) {
2,198✔
1041
      pMgr->restored = true;
260✔
1042
      sInfo("vgId:%d, sync log repl restored. peer: dnode:%d (%" PRIx64 "), repl-mgr:[%" PRId64 " %" PRId64 ", %" PRId64
260!
1043
            "), buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 ")",
1044
            pNode->vgId, DID(&destId), destId.addr, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex,
1045
            pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
1046
      return 0;
260✔
1047
    }
1048
  } else {
1049
    if (pMsg->lastSendIndex < pMgr->startIndex || pMsg->lastSendIndex >= pMgr->endIndex) {
2,494!
1050
      TAOS_CHECK_RETURN(syncLogReplRetryOnNeed(pMgr, pNode));
9!
1051
      return 0;
9✔
1052
    }
1053

1054
    pMgr->states[pMsg->lastSendIndex % pMgr->size].acked = true;
2,485✔
1055

1056
    if (pMsg->success && pMsg->matchIndex == pMsg->lastSendIndex) {
2,485✔
1057
      pMgr->matchIndex = pMsg->matchIndex;
2,189✔
1058
      pMgr->restored = true;
2,189✔
1059
      sInfo("vgId:%d, sync log repl restored. peer: dnode:%d (%" PRIx64 "), repl-mgr:[%" PRId64 " %" PRId64 ", %" PRId64
2,189!
1060
            "), buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 ")",
1061
            pNode->vgId, DID(&destId), destId.addr, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex,
1062
            pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
1063
      return 0;
2,189✔
1064
    }
1065

1066
    if (pMsg->fsmState == SYNC_FSM_STATE_INCOMPLETE || (!pMsg->success && pMsg->matchIndex >= pMsg->lastSendIndex)) {
296!
1067
      char* msg1 = " rollback match index failure";
×
1068
      char* msg2 = " incomplete fsm state";
×
1069
      sInfo("vgId:%d, snapshot replication to dnode:%d. reason:%s, match index:%" PRId64 ", last sent:%" PRId64,
×
1070
            pNode->vgId, DID(&destId), (pMsg->fsmState == SYNC_FSM_STATE_INCOMPLETE ? msg2 : msg1), pMsg->matchIndex,
1071
            pMsg->lastSendIndex);
1072
      if ((code = syncNodeStartSnapshot(pNode, &destId)) < 0) {
×
1073
        sError("vgId:%d, failed to start snapshot for peer dnode:%d", pNode->vgId, DID(&destId));
×
1074
        TAOS_RETURN(code);
×
1075
      }
1076
      return 0;
×
1077
    }
1078
  }
1079

1080
  // check last match term
1081
  SyncTerm  term = -1;
2,234✔
1082
  SyncIndex firstVer = pNode->pLogStore->syncLogBeginIndex(pNode->pLogStore);
2,234✔
1083
  SyncIndex index = TMIN(pMsg->matchIndex, pNode->pLogBuf->matchIndex);
2,234✔
1084
  errno = 0;
2,234✔
1085

1086
  if (pMsg->matchIndex < pNode->pLogBuf->matchIndex) {
2,234✔
1087
    code = syncLogReplGetPrevLogTerm(pMgr, pNode, index + 1, &term);
505✔
1088
    if (term < 0 && (errno == ENFILE || errno == EMFILE || errno == ENOENT)) {
505!
1089
      sError("vgId:%d, failed to get prev log term since %s. index:%" PRId64, pNode->vgId, tstrerror(code), index + 1);
×
1090
      TAOS_RETURN(code);
×
1091
    }
1092

1093
    if (pMsg->matchIndex == -1) {
505✔
1094
      // first time to restore
1095
      sInfo("vgId:%d, first time to restore sync log repl. peer: dnode:%d (%" PRIx64 "), repl-mgr:[%" PRId64 " %" PRId64
185!
1096
            ", %" PRId64 "), buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 "), index:%" PRId64
1097
            ", firstVer:%" PRId64 ", term:%" PRId64 ", lastMatchTerm:%" PRId64,
1098
            pNode->vgId, DID(&destId), destId.addr, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex,
1099
            pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex, index, firstVer, term,
1100
            pMsg->lastMatchTerm);
1101
    }
1102

1103
    if ((index + 1 < firstVer) || (term < 0) ||
505!
1104
        (term != pMsg->lastMatchTerm && (index + 1 == firstVer || index == firstVer))) {
399!
1105
      if (!(term >= 0 || terrno == TSDB_CODE_WAL_LOG_NOT_EXIST)) return TSDB_CODE_SYN_INTERNAL_ERROR;
106!
1106
      if ((code = syncNodeStartSnapshot(pNode, &destId)) < 0) {
106!
1107
        sError("vgId:%d, failed to start snapshot for peer dnode:%d", pNode->vgId, DID(&destId));
×
1108
        TAOS_RETURN(code);
×
1109
      }
1110
      sInfo("vgId:%d, snapshot replication to peer dnode:%d", pNode->vgId, DID(&destId));
106!
1111
      return 0;
106✔
1112
    }
1113

1114
    if (!(index + 1 >= firstVer)) return TSDB_CODE_SYN_INTERNAL_ERROR;
399!
1115

1116
    if (term == pMsg->lastMatchTerm) {
399✔
1117
      index = index + 1;
398✔
1118
      if (!(index <= pNode->pLogBuf->matchIndex)) return TSDB_CODE_SYN_INTERNAL_ERROR;
398!
1119
    } else {
1120
      if (!(index > firstVer)) return TSDB_CODE_SYN_INTERNAL_ERROR;
1!
1121
    }
1122
  }
1123

1124
  // attempt to replicate the raft log at index
1125
  syncLogReplReset(pMgr);
2,128✔
1126
  return syncLogReplProbe(pMgr, pNode, index);
2,128✔
1127
}
1128

1129
int32_t syncLogReplProcessHeartbeatReply(SSyncLogReplMgr* pMgr, SSyncNode* pNode, SyncHeartbeatReply* pMsg) {
44,688✔
1130
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
44,688✔
1131
  (void)taosThreadMutexLock(&pBuf->mutex);
44,688✔
1132
  if (pMsg->startTime != 0 && pMsg->startTime != pMgr->peerStartTime) {
44,688!
1133
    sInfo("vgId:%d, reset sync log repl in heartbeat. peer:%" PRIx64 ", start time:%" PRId64 ", old:%" PRId64 "",
2,217!
1134
          pNode->vgId, pMsg->srcId.addr, pMsg->startTime, pMgr->peerStartTime);
1135
    syncLogReplReset(pMgr);
2,217✔
1136
    pMgr->peerStartTime = pMsg->startTime;
2,217✔
1137
  }
1138
  (void)taosThreadMutexUnlock(&pBuf->mutex);
44,688✔
1139
  return 0;
44,688✔
1140
}
1141

1142
int32_t syncLogReplProcessReply(SSyncLogReplMgr* pMgr, SSyncNode* pNode, SyncAppendEntriesReply* pMsg) {
2,736,582✔
1143
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
2,736,582✔
1144
  (void)taosThreadMutexLock(&pBuf->mutex);
2,736,582✔
1145
  if (pMsg->startTime != pMgr->peerStartTime) {
2,736,582✔
1146
    sInfo("vgId:%d, reset sync log repl in appendlog reply. peer:%" PRIx64 ", start time:%" PRId64 ", old:%" PRId64,
275!
1147
          pNode->vgId, pMsg->srcId.addr, pMsg->startTime, pMgr->peerStartTime);
1148
    syncLogReplReset(pMgr);
275✔
1149
    pMgr->peerStartTime = pMsg->startTime;
277✔
1150
  }
1151

1152
  int32_t code = 0;
2,736,584✔
1153
  if (pMgr->restored) {
2,736,584✔
1154
    if ((code = syncLogReplContinue(pMgr, pNode, pMsg)) != 0) {
2,731,892!
UNCOV
1155
      sError("vgId:%d, failed to continue sync log repl since %s", pNode->vgId, tstrerror(code));
×
1156
    }
1157
  } else {
1158
    if ((code = syncLogReplRecover(pMgr, pNode, pMsg)) != 0) {
4,692!
UNCOV
1159
      sError("vgId:%d, failed to recover sync log repl since %s", pNode->vgId, tstrerror(code));
×
1160
    }
1161
  }
1162
  (void)taosThreadMutexUnlock(&pBuf->mutex);
2,736,585✔
1163
  return 0;
2,736,585✔
1164
}
1165

1166
int32_t syncLogReplStart(SSyncLogReplMgr* pMgr, SSyncNode* pNode) {
453,590✔
1167
  if (pMgr->restored) {
453,590✔
1168
    TAOS_CHECK_RETURN(syncLogReplAttempt(pMgr, pNode));
425,483✔
1169
  } else {
1170
    TAOS_CHECK_RETURN(syncLogReplProbe(pMgr, pNode, pNode->pLogBuf->matchIndex));
28,107!
1171
  }
1172
  return 0;
453,533✔
1173
}
1174

1175
int32_t syncLogReplProbe(SSyncLogReplMgr* pMgr, SSyncNode* pNode, SyncIndex index) {
30,238✔
1176
  if (pMgr->restored) return TSDB_CODE_SYN_INTERNAL_ERROR;
30,238!
1177
  if (!(pMgr->startIndex >= 0)) return TSDB_CODE_SYN_INTERNAL_ERROR;
30,238!
1178
  int64_t retryMaxWaitMs = syncGetRetryMaxWaitMs();
30,238✔
1179
  int64_t nowMs = taosGetMonoTimestampMs();
30,238✔
1180
  int32_t code = 0;
30,238✔
1181

1182
  sTrace("vgId:%d, begin to probe peer:%" PRIx64 " with msg of index:%" PRId64 ". repl-mgr:[%" PRId64 ", %" PRId64
30,238!
1183
         ", %" PRId64 "), restored:%d",
1184
         pNode->vgId, pNode->replicasId[pMgr->peerId].addr, index, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex,
1185
         pMgr->restored);
1186

1187
  if (pMgr->endIndex > pMgr->startIndex &&
30,238✔
1188
      nowMs < pMgr->states[pMgr->startIndex % pMgr->size].timeMs + retryMaxWaitMs) {
25,419✔
1189
    return 0;
25,346✔
1190
  }
1191
  syncLogReplReset(pMgr);
4,892✔
1192

1193
  SRaftId* pDestId = &pNode->replicasId[pMgr->peerId];
4,892✔
1194
  bool     barrier = false;
4,892✔
1195
  SyncTerm term = -1;
4,892✔
1196
  if ((code = syncLogReplSendTo(pMgr, pNode, index, &term, pDestId, &barrier)) < 0) {
4,892!
1197
    sError("vgId:%d, failed to replicate log entry since %s. index:%" PRId64 ", dest: 0x%016" PRIx64 "", pNode->vgId,
×
1198
           tstrerror(code), index, pDestId->addr);
1199
    TAOS_RETURN(code);
×
1200
  }
1201

1202
  if (!(index >= 0)) return TSDB_CODE_SYN_INTERNAL_ERROR;
4,892!
1203
  pMgr->states[index % pMgr->size].barrier = barrier;
4,892✔
1204
  pMgr->states[index % pMgr->size].timeMs = nowMs;
4,892✔
1205
  pMgr->states[index % pMgr->size].term = term;
4,892✔
1206
  pMgr->states[index % pMgr->size].acked = false;
4,892✔
1207

1208
  pMgr->startIndex = index;
4,892✔
1209
  pMgr->endIndex = index + 1;
4,892✔
1210

1211
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
4,892✔
1212
  sTrace("vgId:%d, probe peer:%" PRIx64 " with msg of index:%" PRId64 " term:%" PRId64 ". repl-mgr:[%" PRId64
4,892!
1213
         " %" PRId64 ", %" PRId64 "), buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 ")",
1214
         pNode->vgId, pDestId->addr, index, term, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex, pBuf->startIndex,
1215
         pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
1216
  return 0;
4,892✔
1217
}
1218

1219
int32_t syncLogReplAttempt(SSyncLogReplMgr* pMgr, SSyncNode* pNode) {
3,157,372✔
1220
  if (!pMgr->restored) return TSDB_CODE_SYN_INTERNAL_ERROR;
3,157,372!
1221

1222
  sTrace("vgId:%d, begin to attempt replicate log entries from end to match. repl-mgr:[%" PRId64 ", %" PRId64
3,157,372!
1223
         ", %" PRId64 "), restore:%d",
1224
         pNode->vgId, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex, pMgr->restored);
1225

1226
  SRaftId*  pDestId = &pNode->replicasId[pMgr->peerId];
3,157,372✔
1227
  int32_t   batchSize = TMAX(1, pMgr->size >> (4 + pMgr->retryBackoff));
3,157,372✔
1228
  int32_t   code = 0;
3,157,372✔
1229
  int32_t   count = 0;
3,157,372✔
1230
  int64_t   nowMs = taosGetMonoTimestampMs();
3,157,374✔
1231
  int64_t   limit = pMgr->size >> 1;
3,157,374✔
1232
  SyncTerm  term = -1;
3,157,374✔
1233
  SyncIndex firstIndex = -1;
3,157,374✔
1234

1235
  for (SyncIndex index = pMgr->endIndex; index <= pNode->pLogBuf->matchIndex; index++) {
5,893,521✔
1236
    if (batchSize < count || limit <= index - pMgr->startIndex) {
5,249,720✔
1237
      break;
1238
    }
1239
    if (pMgr->startIndex + 1 < index && pMgr->states[(index - 1) % pMgr->size].barrier) {
2,762,717✔
1240
      break;
24,755✔
1241
    }
1242
    int64_t  pos = index % pMgr->size;
2,737,962✔
1243
    SRaftId* pDestId = &pNode->replicasId[pMgr->peerId];
2,737,962✔
1244
    bool     barrier = false;
2,737,962✔
1245
    SyncTerm term = -1;
2,737,962✔
1246
    if ((code = syncLogReplSendTo(pMgr, pNode, index, &term, pDestId, &barrier)) < 0) {
2,737,962✔
1247
      sError("vgId:%d, failed to replicate log entry since %s. index:%" PRId64 ", dest: 0x%016" PRIx64 "", pNode->vgId,
39!
1248
             tstrerror(code), index, pDestId->addr);
1249
      TAOS_RETURN(code);
39✔
1250
    }
1251
    pMgr->states[pos].barrier = barrier;
2,737,923✔
1252
    pMgr->states[pos].timeMs = nowMs;
2,737,923✔
1253
    pMgr->states[pos].term = term;
2,737,923✔
1254
    pMgr->states[pos].acked = false;
2,737,923✔
1255

1256
    if (firstIndex == -1) firstIndex = index;
2,737,923✔
1257
    count++;
2,737,923✔
1258

1259
    pMgr->endIndex = index + 1;
2,737,923✔
1260
    if (barrier) {
2,737,923✔
1261
      sInfo("vgId:%d, replicated sync barrier to dnode:%d. index:%" PRId64 ", term:%" PRId64 ", repl-mgr:[%" PRId64
1,776!
1262
            " %" PRId64 ", %" PRId64 ")",
1263
            pNode->vgId, DID(pDestId), index, term, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex);
1264
      break;
1,776✔
1265
    }
1266
  }
1267

1268
  TAOS_CHECK_RETURN(syncLogReplRetryOnNeed(pMgr, pNode));
3,157,335✔
1269

1270
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
3,157,317✔
1271
  sTrace("vgId:%d, replicated %d msgs to peer:%" PRIx64 ". indexes:%" PRId64 "..., terms: ...%" PRId64
3,157,317!
1272
         ", repl-mgr:[%" PRId64 " %" PRId64 ", %" PRId64 "), buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64
1273
         ")",
1274
         pNode->vgId, count, pDestId->addr, firstIndex, term, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex,
1275
         pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
1276
  return 0;
3,157,317✔
1277
}
1278

1279
int32_t syncLogReplContinue(SSyncLogReplMgr* pMgr, SSyncNode* pNode, SyncAppendEntriesReply* pMsg) {
2,731,891✔
1280
  if (pMgr->restored != true) return TSDB_CODE_SYN_INTERNAL_ERROR;
2,731,891!
1281
  if (pMgr->startIndex <= pMsg->lastSendIndex && pMsg->lastSendIndex < pMgr->endIndex) {
2,731,891✔
1282
    if (pMgr->startIndex < pMgr->matchIndex && pMgr->retryBackoff > 0) {
2,570,617!
1283
      int64_t firstMs = pMgr->states[pMgr->startIndex % pMgr->size].timeMs;
×
1284
      int64_t lastMs = pMgr->states[(pMgr->endIndex - 1) % pMgr->size].timeMs;
×
1285
      int64_t diffMs = lastMs - firstMs;
×
1286
      if (diffMs > 0 && diffMs < ((int64_t)SYNC_LOG_REPL_RETRY_WAIT_MS << (pMgr->retryBackoff - 1))) {
×
1287
        pMgr->retryBackoff -= 1;
×
1288
      }
1289
    }
1290
    pMgr->states[pMsg->lastSendIndex % pMgr->size].acked = true;
2,570,617✔
1291
    pMgr->matchIndex = TMAX(pMgr->matchIndex, pMsg->matchIndex);
2,570,617✔
1292
    for (SyncIndex index = pMgr->startIndex; index < pMgr->matchIndex; index++) {
5,294,161✔
1293
      (void)memset(&pMgr->states[index % pMgr->size], 0, sizeof(pMgr->states[0]));
2,723,544✔
1294
    }
1295
    pMgr->startIndex = pMgr->matchIndex;
2,570,617✔
1296
  }
1297

1298
  return syncLogReplAttempt(pMgr, pNode);
2,731,891✔
1299
}
1300

1301
SSyncLogReplMgr* syncLogReplCreate() {
238,564✔
1302
  SSyncLogReplMgr* pMgr = taosMemoryCalloc(1, sizeof(SSyncLogReplMgr));
238,564✔
1303
  if (pMgr == NULL) {
238,585!
UNCOV
1304
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1305
    return NULL;
×
1306
  }
1307

1308
  pMgr->size = sizeof(pMgr->states) / sizeof(pMgr->states[0]);
238,585✔
1309

1310
  if (pMgr->size != TSDB_SYNC_LOG_BUFFER_SIZE) {
238,585!
1311
    terrno = TSDB_CODE_SYN_INTERNAL_ERROR;
×
1312
    return NULL;
×
1313
  }
1314

1315
  return pMgr;
238,585✔
1316
}
1317

1318
void syncLogReplDestroy(SSyncLogReplMgr* pMgr) {
238,472✔
1319
  if (pMgr == NULL) {
238,472!
1320
    return;
×
1321
  }
1322
  taosMemoryFree(pMgr);
238,472✔
1323
  return;
238,468✔
1324
}
1325

1326
int32_t syncNodeLogReplInit(SSyncNode* pNode) {
15,907✔
1327
  for (int i = 0; i < TSDB_MAX_REPLICA + TSDB_MAX_LEARNER_REPLICA; i++) {
254,489✔
1328
    if (pNode->logReplMgrs[i] != NULL) return TSDB_CODE_SYN_INTERNAL_ERROR;
238,576!
1329
    pNode->logReplMgrs[i] = syncLogReplCreate();
238,576✔
1330
    if (pNode->logReplMgrs[i] == NULL) {
238,585✔
1331
      TAOS_RETURN(terrno);
3✔
1332
    }
1333
    pNode->logReplMgrs[i]->peerId = i;
238,582✔
1334
  }
1335
  return 0;
15,913✔
1336
}
1337

1338
void syncNodeLogReplDestroy(SSyncNode* pNode) {
15,907✔
1339
  for (int i = 0; i < TSDB_MAX_REPLICA + TSDB_MAX_LEARNER_REPLICA; i++) {
254,384✔
1340
    syncLogReplDestroy(pNode->logReplMgrs[i]);
238,474✔
1341
    pNode->logReplMgrs[i] = NULL;
238,477✔
1342
  }
1343
}
15,910✔
1344

1345
int32_t syncLogBufferCreate(SSyncLogBuffer** ppBuf) {
15,901✔
1346
  int32_t         code = 0;
15,901✔
1347
  SSyncLogBuffer* pBuf = taosMemoryCalloc(1, sizeof(SSyncLogBuffer));
15,901✔
1348
  if (pBuf == NULL) {
15,907!
1349
    TAOS_CHECK_GOTO(terrno, NULL, _exit);
×
1350
  }
1351

1352
  pBuf->size = sizeof(pBuf->entries) / sizeof(pBuf->entries[0]);
15,907✔
1353

1354
  if (pBuf->size != TSDB_SYNC_LOG_BUFFER_SIZE) {
15,907!
1355
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
1356
    goto _exit;
×
1357
  }
1358

1359
  if (taosThreadMutexAttrInit(&pBuf->attr) < 0) {
15,907!
1360
    code = TAOS_SYSTEM_ERROR(errno);
×
1361
    sError("failed to init log buffer mutexattr due to %s", tstrerror(code));
×
1362
    goto _exit;
×
1363
  }
1364

1365
  if (taosThreadMutexAttrSetType(&pBuf->attr, PTHREAD_MUTEX_RECURSIVE) < 0) {
15,907!
1366
    code = TAOS_SYSTEM_ERROR(errno);
×
1367
    sError("failed to set log buffer mutexattr type due to %s", tstrerror(code));
×
1368
    goto _exit;
×
1369
  }
1370

1371
  if (taosThreadMutexInit(&pBuf->mutex, &pBuf->attr) < 0) {
15,907!
1372
    code = TAOS_SYSTEM_ERROR(errno);
×
1373
    sError("failed to init log buffer mutex due to %s", tstrerror(code));
×
1374
    goto _exit;
×
1375
  }
1376
_exit:
15,908✔
1377
  if (code != 0) {
15,908!
1378
    taosMemoryFreeClear(pBuf);
×
1379
  }
1380
  *ppBuf = pBuf;
15,908✔
1381
  TAOS_RETURN(code);
15,908✔
1382
}
1383

1384
void syncLogBufferClear(SSyncLogBuffer* pBuf) {
15,906✔
1385
  (void)taosThreadMutexLock(&pBuf->mutex);
15,906✔
1386
  for (SyncIndex index = pBuf->startIndex; index < pBuf->endIndex; index++) {
893,362✔
1387
    SSyncRaftEntry* pEntry = pBuf->entries[(index + pBuf->size) % pBuf->size].pItem;
877,923✔
1388
    if (pEntry == NULL) continue;
877,923!
1389
    syncEntryDestroy(pEntry);
877,923✔
1390
    pEntry = NULL;
877,458✔
1391
    (void)memset(&pBuf->entries[(index + pBuf->size) % pBuf->size], 0, sizeof(pBuf->entries[0]));
877,458✔
1392
  }
1393
  pBuf->startIndex = pBuf->commitIndex = pBuf->matchIndex = pBuf->endIndex = 0;
15,439✔
1394
  pBuf->bytes = 0;
15,439✔
1395
  (void)taosThreadMutexUnlock(&pBuf->mutex);
15,439✔
1396
}
15,907✔
1397

1398
void syncLogBufferDestroy(SSyncLogBuffer* pBuf) {
15,906✔
1399
  if (pBuf == NULL) {
15,906!
1400
    return;
×
1401
  }
1402
  syncLogBufferClear(pBuf);
15,906✔
1403
  (void)taosThreadMutexDestroy(&pBuf->mutex);
15,907✔
1404
  (void)taosThreadMutexAttrDestroy(&pBuf->attr);
15,907✔
1405
  taosMemoryFree(pBuf);
15,907✔
1406
  return;
15,907✔
1407
}
1408

1409
int32_t syncLogBufferRollback(SSyncLogBuffer* pBuf, SSyncNode* pNode, SyncIndex toIndex) {
19,186✔
1410
  int32_t code = 0;
19,186✔
1411
  if (!(pBuf->commitIndex < toIndex && toIndex <= pBuf->endIndex)) return TSDB_CODE_SYN_INTERNAL_ERROR;
19,186!
1412

1413
  if (toIndex == pBuf->endIndex) {
19,186✔
1414
    return 0;
19,169✔
1415
  }
1416

1417
  sInfo("vgId:%d, rollback sync log buffer. toindex:%" PRId64 ", buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64
17!
1418
        ")",
1419
        pNode->vgId, toIndex, pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
1420

1421
  // trunc buffer
1422
  SyncIndex index = pBuf->endIndex - 1;
17✔
1423
  while (index >= toIndex) {
390✔
1424
    SSyncRaftEntry* pEntry = pBuf->entries[index % pBuf->size].pItem;
373✔
1425
    if (pEntry != NULL) {
373✔
1426
      syncEntryDestroy(pEntry);
18✔
1427
      pEntry = NULL;
18✔
1428
      (void)memset(&pBuf->entries[index % pBuf->size], 0, sizeof(pBuf->entries[0]));
18✔
1429
    }
1430
    index--;
373✔
1431
  }
1432
  pBuf->endIndex = toIndex;
17✔
1433
  pBuf->matchIndex = TMIN(pBuf->matchIndex, index);
17✔
1434
  if (index + 1 != toIndex) return TSDB_CODE_SYN_INTERNAL_ERROR;
17!
1435

1436
  // trunc wal
1437
  SyncIndex lastVer = pNode->pLogStore->syncLogLastIndex(pNode->pLogStore);
17✔
1438
  if (lastVer >= toIndex && (code = pNode->pLogStore->syncLogTruncate(pNode->pLogStore, toIndex)) < 0) {
17!
1439
    sError("vgId:%d, failed to truncate log store since %s. from index:%" PRId64 "", pNode->vgId, tstrerror(code),
×
1440
           toIndex);
1441
    TAOS_RETURN(code);
×
1442
  }
1443
  lastVer = pNode->pLogStore->syncLogLastIndex(pNode->pLogStore);
17✔
1444
  if (toIndex != lastVer + 1) return TSDB_CODE_SYN_INTERNAL_ERROR;
17!
1445

1446
  // refill buffer on need
1447
  if (toIndex <= pBuf->startIndex) {
17!
1448
    if ((code = syncLogBufferInitWithoutLock(pBuf, pNode)) < 0) {
×
1449
      sError("vgId:%d, failed to refill sync log buffer since %s", pNode->vgId, tstrerror(code));
×
1450
      TAOS_RETURN(code);
×
1451
    }
1452
  }
1453

1454
  if (pBuf->endIndex != toIndex) return TSDB_CODE_SYN_INTERNAL_ERROR;
17!
1455
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
17!
1456
  return 0;
17✔
1457
}
1458

1459
int32_t syncLogBufferReset(SSyncLogBuffer* pBuf, SSyncNode* pNode) {
19,183✔
1460
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
19,183!
1461
  (void)taosThreadMutexLock(&pBuf->mutex);
19,183✔
1462
  SyncIndex lastVer = pNode->pLogStore->syncLogLastIndex(pNode->pLogStore);
19,183✔
1463
  if (lastVer != pBuf->matchIndex) return TSDB_CODE_SYN_INTERNAL_ERROR;
19,183!
1464
  SyncIndex index = pBuf->endIndex - 1;
19,183✔
1465

1466
  int32_t code = 0;
19,183✔
1467
  if ((code = syncLogBufferRollback(pBuf, pNode, pBuf->matchIndex + 1)) != 0) {
19,183!
1468
    sError("vgId:%d, failed to rollback sync log buffer since %s", pNode->vgId, tstrerror(code));
×
1469
  }
1470

1471
  sInfo("vgId:%d, reset sync log buffer. buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 ")", pNode->vgId,
19,183!
1472
        pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
1473

1474
  pBuf->endIndex = pBuf->matchIndex + 1;
19,183✔
1475

1476
  // reset repl mgr
1477
  for (int i = 0; i < pNode->totalReplicaNum; i++) {
52,102✔
1478
    SSyncLogReplMgr* pMgr = pNode->logReplMgrs[i];
32,919✔
1479
    syncLogReplReset(pMgr);
32,919✔
1480
  }
1481
  (void)taosThreadMutexUnlock(&pBuf->mutex);
19,183✔
1482
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
19,183!
1483
  return 0;
19,183✔
1484
}
1485

1486
int32_t syncLogBufferGetOneEntry(SSyncLogBuffer* pBuf, SSyncNode* pNode, SyncIndex index, bool* pInBuf,
32,522,527✔
1487
                                 SSyncRaftEntry** ppEntry) {
1488
  int32_t code = 0;
32,522,527✔
1489

1490
  *ppEntry = NULL;
32,522,527✔
1491

1492
  if (index >= pBuf->endIndex) {
32,522,527✔
1493
    return TSDB_CODE_OUT_OF_RANGE;
15,187,637✔
1494
  }
1495

1496
  if (index > pBuf->startIndex) {  // startIndex might be dummy
17,334,890✔
1497
    *pInBuf = true;
14,817,744✔
1498
    *ppEntry = pBuf->entries[index % pBuf->size].pItem;
14,817,744✔
1499
  } else {
1500
    *pInBuf = false;
2,517,146✔
1501

1502
    if ((code = pNode->pLogStore->syncLogGetEntry(pNode->pLogStore, index, ppEntry)) < 0) {
2,517,146✔
1503
      sWarn("vgId:%d, failed to get log entry since %s. index:%" PRId64 "", pNode->vgId, tstrerror(code), index);
39!
1504
    }
1505
  }
1506
  TAOS_RETURN(code);
17,335,506✔
1507
}
1508

1509
int32_t syncLogReplSendTo(SSyncLogReplMgr* pMgr, SSyncNode* pNode, SyncIndex index, SyncTerm* pTerm, SRaftId* pDestId,
2,753,788✔
1510
                          bool* pBarrier) {
1511
  SSyncRaftEntry* pEntry = NULL;
2,753,788✔
1512
  SRpcMsg         msgOut = {0};
2,753,788✔
1513
  bool            inBuf = false;
2,753,788✔
1514
  SyncTerm        prevLogTerm = -1;
2,753,788✔
1515
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
2,753,788✔
1516
  int32_t         code = 0;
2,753,788✔
1517
  int32_t         lino = 0;
2,753,788✔
1518

1519
  code = syncLogBufferGetOneEntry(pBuf, pNode, index, &inBuf, &pEntry);
2,753,788✔
1520
  if (pEntry == NULL) {
2,753,792✔
1521
    sWarn("vgId:%d, failed to get raft entry for index:%" PRId64 "", pNode->vgId, index);
42✔
1522
    if (code == TSDB_CODE_WAL_LOG_NOT_EXIST) {
42✔
1523
      SSyncLogReplMgr* pMgr = syncNodeGetLogReplMgr(pNode, pDestId);
39✔
1524
      if (pMgr) {
39!
1525
        sInfo("vgId:%d, reset sync log repl of peer:%" PRIx64 " since %s. index:%" PRId64, pNode->vgId, pDestId->addr,
39!
1526
              tstrerror(code), index);
1527
        syncLogReplReset(pMgr);
39✔
1528
      }
1529
    }
1530
    goto _err;
39✔
1531
  }
1532
  *pBarrier = syncLogReplBarrier(pEntry);
2,753,750✔
1533

1534
  code = syncLogReplGetPrevLogTerm(pMgr, pNode, index, &prevLogTerm);
2,753,750✔
1535
  if (prevLogTerm < 0) {
2,753,749!
1536
    sError("vgId:%d, failed to get prev log term since %s. index:%" PRId64 "", pNode->vgId, tstrerror(code), index);
×
1537
    goto _err;
×
1538
  }
1539
  if (pTerm) *pTerm = pEntry->term;
2,753,749!
1540

1541
  code = syncBuildAppendEntriesFromRaftEntry(pNode, pEntry, prevLogTerm, &msgOut);
2,753,749✔
1542
  if (code < 0) {
2,753,752!
1543
    sError("vgId:%d, failed to get append entries for index:%" PRId64 "", pNode->vgId, index);
×
1544
    goto _err;
×
1545
  }
1546

1547
  TRACE_SET_MSGID(&(msgOut.info.traceId), tGenIdPI64());
2,753,752✔
1548
  STraceId* trace = &(msgOut.info.traceId);
2,753,749✔
1549
  sGTrace("vgId:%d, replicate one msg index:%" PRId64 " term:%" PRId64 " prevterm:%" PRId64 " to dest: 0x%016" PRIx64,
2,753,749!
1550
          pNode->vgId, pEntry->index, pEntry->term, prevLogTerm, pDestId->addr);
1551
  TAOS_CHECK_GOTO(syncNodeSendAppendEntries(pNode, pDestId, &msgOut), &lino, _err);
2,753,749!
1552

1553
  if (!inBuf) {
2,753,754✔
1554
    syncEntryDestroy(pEntry);
2,422,925✔
1555
    pEntry = NULL;
2,422,925✔
1556
  }
1557
  return 0;
2,753,754✔
1558

1559
_err:
39✔
1560
  rpcFreeCont(msgOut.pCont);
39✔
1561
  msgOut.pCont = NULL;
39✔
1562
  if (!inBuf) {
39!
1563
    syncEntryDestroy(pEntry);
39✔
1564
    pEntry = NULL;
39✔
1565
  }
1566
  TAOS_RETURN(code);
39✔
1567
}
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