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

taosdata / TDengine / #3911

24 Apr 2025 11:36PM UTC coverage: 53.735% (-1.6%) from 55.295%
#3911

push

travis-ci

happyguoxy
Sync branches at 2025-04-25 07:35

170049 of 316459 relevant lines covered (53.73%)

1192430.54 hits per line

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

69.41
/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 "syncCommit.h"
19
#include "syncIndexMgr.h"
20
#include "syncInt.h"
21
#include "syncPipeline.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) {
92,005✔
34
  return (type == TDMT_VND_CREATE_TABLE) || (type == TDMT_VND_ALTER_TABLE) || (type == TDMT_VND_DROP_TABLE) ||
91,930✔
35
         (type == TDMT_VND_UPDATE_TAG_VAL) || (type == TDMT_VND_ALTER_CONFIRM);
183,935✔
36
}
37

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

42
int64_t syncLogBufferGetEndIndex(SSyncLogBuffer* pBuf) {
69,527✔
43
  (void)taosThreadMutexLock(&pBuf->mutex);
69,527✔
44
  int64_t index = pBuf->endIndex;
69,528✔
45
  (void)taosThreadMutexUnlock(&pBuf->mutex);
69,528✔
46
  return index;
69,528✔
47
}
48

49
int32_t syncLogBufferAppend(SSyncLogBuffer* pBuf, SSyncNode* pNode, SSyncRaftEntry* pEntry) {
69,527✔
50
  int32_t code = 0;
69,527✔
51
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
69,527✔
52
  (void)taosThreadMutexLock(&pBuf->mutex);
69,524✔
53
  SyncIndex index = pEntry->index;
69,527✔
54

55
  if (index - pBuf->startIndex >= pBuf->size) {
69,527✔
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) {
69,527✔
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);
69,527✔
69
  if (pNode->restoreFinish && pBuf->commitIndex - appliedIndex >= TSDB_SYNC_APPLYQ_SIZE_LIMIT) {
69,526✔
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) {
69,526✔
77
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
78
    goto _err;
×
79
  };
80

81
  SSyncRaftEntry* pExist = pBuf->entries[index % pBuf->size].pItem;
69,526✔
82
  if (pExist != NULL) {
69,526✔
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;
69,526✔
89
  if (pMatch == NULL) {
69,526✔
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) {
69,526✔
95
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
96
    goto _err;
×
97
  }
98
  if (!(pMatch->term <= pEntry->term)) {
69,526✔
99
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
100
    goto _err;
×
101
  }
102

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

111
  (void)taosThreadMutexUnlock(&pBuf->mutex);
69,526✔
112
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
69,527✔
113
  return 0;
69,526✔
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) {
60,141✔
123
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
60,141✔
124
  SSyncRaftEntry* pEntry = NULL;
60,141✔
125
  SyncIndex       prevIndex = index - 1;
60,141✔
126
  SyncTerm        prevLogTerm = -1;
60,141✔
127
  int32_t         code = 0;
60,141✔
128

129
  if (prevIndex == -1 && pNode->pLogStore->syncLogBeginIndex(pNode->pLogStore) == 0) {
60,141✔
130
    *pSyncTerm = 0;
66✔
131
    return 0;
66✔
132
  }
133

134
  if (prevIndex > pBuf->matchIndex) {
60,075✔
135
    *pSyncTerm = -1;
×
136
    TAOS_RETURN(TSDB_CODE_WAL_LOG_NOT_EXIST);
×
137
  }
138

139
  if (index - 1 != prevIndex) return TSDB_CODE_SYN_INTERNAL_ERROR;
60,075✔
140

141
  if (prevIndex >= pBuf->startIndex) {
60,075✔
142
    pEntry = pBuf->entries[(prevIndex + pBuf->size) % pBuf->size].pItem;
60,007✔
143
    if (pEntry == NULL) {
60,007✔
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;
60,007✔
149
    *pSyncTerm = prevLogTerm;
60,007✔
150
    return 0;
60,007✔
151
  }
152

153
  if (pMgr && pMgr->startIndex <= prevIndex && prevIndex < pMgr->endIndex) {
68✔
154
    int64_t timeMs = pMgr->states[(prevIndex + pMgr->size) % pMgr->size].timeMs;
61✔
155
    if (timeMs == 0) {
61✔
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;
61✔
161
    if (!(prevIndex == 0 || prevLogTerm != 0)) {
61✔
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;
61✔
168
    return 0;
61✔
169
  }
170

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

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

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

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

195
int32_t syncLogValidateAlignmentOfCommit(SSyncNode* pNode, SyncIndex commitIndex) {
299✔
196
  SyncIndex firstVer = pNode->pLogStore->syncLogBeginIndex(pNode->pLogStore);
299✔
197
  if (firstVer > commitIndex + 1) {
299✔
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);
299✔
205
  if (lastVer < commitIndex) {
299✔
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;
299✔
213
}
214

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

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

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

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

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

253
  while (true) {
2,552✔
254
    if (index <= pBuf->commitIndex) {
2,851✔
255
      takeDummy = true;
299✔
256
      break;
299✔
257
    }
258

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

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

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

280
    if (!taken) {
2,552✔
281
      syncEntryDestroy(pEntry);
×
282
      pEntry = NULL;
×
283
      break;
×
284
    }
285

286
    index--;
2,552✔
287
  }
288

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

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

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

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

313
  pBuf->isCatchup = false;
299✔
314

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

318
  // validate
319
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
299✔
320
  return 0;
299✔
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) {
299✔
330
  (void)taosThreadMutexLock(&pBuf->mutex);
299✔
331
  int32_t ret = syncLogBufferInitWithoutLock(pBuf, pNode);
299✔
332
  (void)taosThreadMutexUnlock(&pBuf->mutex);
299✔
333
  return ret;
299✔
334
}
335

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

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

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

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

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

392
  if (lastMatchTerm < 0) {
60,096✔
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) {
60,096✔
399
    sTrace("vgId:%d, already committed, index:%" PRId64 ", term:%" PRId64 ", log buffer: [%" PRId64 " %" PRId64
3✔
400
           " %" PRId64 ", %" PRId64 ")",
401
           pNode->vgId, pEntry->index, pEntry->term, pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex,
402
           pBuf->endIndex);
403
    SyncTerm term = -1;
3✔
404
    code = syncLogReplGetPrevLogTerm(NULL, pNode, index + 1, &term);
3✔
405
    if (pEntry->term < 0) {
3✔
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) {
3✔
411
      code = 0;
3✔
412
    }
413
    goto _out;
3✔
414
  }
415

416
  if (pNode->raftCfg.cfg.nodeInfo[pNode->raftCfg.cfg.myIndex].nodeRole == TAOS_SYNC_ROLE_LEARNER && index > 0 &&
60,093✔
417
      index > pBuf->totalIndex) {
180✔
418
    pBuf->totalIndex = index;
28✔
419
    sTrace("vgId:%d, update learner progress, index:%" PRId64 ", term:%" PRId64 ": prevterm:%" PRId64
28✔
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) {
60,093✔
426
    sWarn("vgId:%d, out of buffer range, index:%" PRId64 ", term:%" PRId64 ", log buffer: [%" PRId64 " %" PRId64
×
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;
×
431
    goto _out;
×
432
  }
433

434
  if (index > pBuf->matchIndex && lastMatchTerm != prevTerm) {
60,093✔
435
    sWarn("vgId:%d, not ready to accept, index:%" PRId64 ", term:%" PRId64 ": prevterm:%" PRId64
15✔
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;
15✔
440
    goto _out;
15✔
441
  }
442

443
  // check current in buffer
444
  code = syncLogBufferGetOneEntry(pBuf, pNode, index, &inBuf, &pExist);
60,078✔
445
  if (pExist != NULL) {
60,078✔
446
    if (pEntry->index != pExist->index) {
25✔
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) {
25✔
453
      TAOS_CHECK_GOTO(syncLogBufferRollback(pBuf, pNode, index), NULL, _out);
×
454
    } else {
455
      sTrace("vgId:%d, duplicate log entry received, index:%" PRId64 ", term:%" PRId64 ", log buffer: [%" PRId64
25✔
456
             " %" PRId64 " %" PRId64 ", %" PRId64 ")",
457
             pNode->vgId, pEntry->index, pEntry->term, pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex,
458
             pBuf->endIndex);
459
      SyncTerm existPrevTerm = -1;
25✔
460
      TAOS_CHECK_GOTO(syncLogReplGetPrevLogTerm(NULL, pNode, index, &existPrevTerm), NULL, _out);
25✔
461
      if (!(pEntry->term == pExist->term && (pEntry->index > pBuf->matchIndex || prevTerm == existPrevTerm))) {
25✔
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;
25✔
470
      goto _out;
25✔
471
    }
472
  }
473

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

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

498
  // success
499
  code = 0;
60,052✔
500

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

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

516
int32_t syncLogStorePersist(SSyncLogStore* pLogStore, SSyncNode* pNode, SSyncRaftEntry* pEntry) {
129,580✔
517
  int32_t code = 0;
129,580✔
518
  if (pEntry->index < 0) return TSDB_CODE_SYN_INTERNAL_ERROR;
129,580✔
519
  SyncIndex lastVer = pLogStore->syncLogLastIndex(pLogStore);
129,580✔
520
  if (lastVer >= pEntry->index && (code = pLogStore->syncLogTruncate(pLogStore, pEntry->index)) < 0) {
129,581✔
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);
129,581✔
525
  if (pEntry->index != lastVer + 1) return TSDB_CODE_SYN_INTERNAL_ERROR;
129,579✔
526

527
  bool doFsync = syncLogStoreNeedFlush(pEntry, pNode->replicaNum);
129,579✔
528
  if ((code = pLogStore->syncLogAppendEntry(pLogStore, pEntry, doFsync)) < 0) {
129,579✔
529
    sError("failed to persist raft 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);
129,577✔
535
  if (pEntry->index != lastVer) return TSDB_CODE_SYN_INTERNAL_ERROR;
129,579✔
536
  return 0;
129,579✔
537
}
538

539
int64_t syncLogBufferProceed(SSyncLogBuffer* pBuf, SSyncNode* pNode, SyncTerm* pMatchTerm, char* str, const SRpcMsg *pMsg) {
129,619✔
540
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
129,619✔
541
  (void)taosThreadMutexLock(&pBuf->mutex);
129,615✔
542

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

547
  while (pBuf->matchIndex + 1 < pBuf->endIndex) {
259,196✔
548
    int64_t index = pBuf->matchIndex + 1;
129,782✔
549
    if (index < 0) {
129,782✔
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];
129,782✔
557
    SyncIndex         prevLogIndex = pBufEntry->prevLogIndex;
129,782✔
558
    SyncTerm          prevLogTerm = pBufEntry->prevLogTerm;
129,782✔
559
    SSyncRaftEntry*   pEntry = pBufEntry->pItem;
129,782✔
560
    if (pEntry == NULL) {
129,782✔
561
      sTrace("vgId:%d, msg:%p, cannot proceed match index in log buffer, no raft entry at next pos of matchIndex:%" PRId64,
206✔
562
             pNode->vgId, pMsg, pBuf->matchIndex);
563
      goto _out;
206✔
564
    }
565

566
    if (index != pEntry->index) {
129,576✔
567
      sError("vgId:%d, msg:%p, failed to proceed index:%" PRId64 ", pEntry->index:%" PRId64, pNode->vgId, pMsg, 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;
129,576✔
574
    if (pMatch == NULL) {
129,576✔
575
      sError("vgId:%d, msg:%p, failed to proceed since pMatch is null", pNode->vgId, pMsg);
×
576
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
577
      goto _out;
×
578
    }
579
    if (pMatch->index != pBuf->matchIndex) {
129,576✔
580
      sError("vgId:%d, msg:%p, failed to proceed, pMatch->index:%" PRId64 ", pBuf->matchIndex:%" PRId64, pNode->vgId,
×
581
             pMsg, pMatch->index, pBuf->matchIndex);
582
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
583
      goto _out;
×
584
    }
585
    if (pMatch->index + 1 != pEntry->index) {
129,576✔
586
      sError("vgId:%d, msg:%p, failed to proceed, pMatch->index:%" PRId64 ", pEntry->index:%" PRId64, pNode->vgId, pMsg,
×
587
             pMatch->index, pEntry->index);
588
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
589
      goto _out;
×
590
    }
591
    if (prevLogIndex != pMatch->index) {
129,576✔
592
      sError("vgId:%d, msg:%p, failed to proceed, prevLogIndex:%" PRId64 ", pMatch->index:%" PRId64, pNode->vgId, pMsg,
×
593
             prevLogIndex, pMatch->index);
594
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
595
      goto _out;
×
596
    }
597

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

608
    // increase match index
609
    pBuf->matchIndex = index;
129,576✔
610

611
    sGDebug(pMsg ? &pMsg->info.traceId : NULL,
129,576✔
612
            "vgId:%d, msg:%p, log buffer proceed, start index:%" PRId64 ", match index:%" PRId64 ", end index:%" PRId64,
613
            pNode->vgId, pMsg, pBuf->startIndex, pBuf->matchIndex, pBuf->endIndex);
614

615
    // persist
616
    if ((code = syncLogStorePersist(pLogStore, pNode, pEntry)) < 0) {
129,586✔
617
      sError("vgId:%d, msg:%p, failed to persist sync log entry from buffer since %s, index:%" PRId64, pNode->vgId,
×
618
             pMsg, tstrerror(code), pEntry->index);
619
      taosMsleep(1);
×
620
      goto _out;
×
621
    }
622

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

653
    // replicate on demand
654
    if ((code = syncNodeReplicateWithoutLock(pNode)) != 0) {
129,576✔
655
      sError("vgId:%d, msg:%p, failed to replicate since %s, index:%" PRId64, pNode->vgId, pMsg, tstrerror(code),
×
656
             pEntry->index);
657
      goto _out;
×
658
    }
659

660
    if (pEntry->index != pBuf->matchIndex) {
129,580✔
661
      sError("vgId:%d, msg:%p, failed to proceed, pEntry->index:%" PRId64 ", pBuf->matchIndex:%" PRId64, pNode->vgId,
×
662
             pMsg, pEntry->index, pBuf->matchIndex);
663
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
664
      goto _out;
×
665
    }
666

667
    // update my match index
668
    matchIndex = pBuf->matchIndex;
129,580✔
669
    syncIndexMgrSetIndex(pNode->pMatchIndex, &pNode->myRaftId, pBuf->matchIndex);
129,580✔
670
  }  // end of while
671

672
_out:
129,414✔
673
  pBuf->matchIndex = matchIndex;
129,620✔
674
  if (pMatchTerm) {
129,620✔
675
    *pMatchTerm = pBuf->entries[(matchIndex + pBuf->size) % pBuf->size].pItem->term;
60,092✔
676
  }
677
  (void)taosThreadMutexUnlock(&pBuf->mutex);
129,620✔
678
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
129,622✔
679
  return matchIndex;
129,619✔
680
}
681

682
int32_t syncFsmExecute(SSyncNode* pNode, SSyncFSM* pFsm, ESyncState role, SyncTerm term, SSyncRaftEntry* pEntry,
132,103✔
683
                       int32_t applyCode, bool force) {
684
  // learner need to execute fsm when it catch up entry log
685
  // if force is true, keep all contition check to execute fsm
686
  if (pNode->replicaNum == 1 && pNode->restoreFinish && pNode->vgId != 1 &&
132,103✔
687
      pNode->raftCfg.cfg.nodeInfo[pNode->raftCfg.cfg.myIndex].nodeRole != TAOS_SYNC_ROLE_LEARNER && force == false) {
37,459✔
688
    sGDebug(&pEntry->originRpcTraceId,
37,459✔
689
            "vgId:%d, index:%" PRId64 ", raft fsm no need to execute, term:%" PRId64
690
            ", type:%s code:0x%x, replica:%d, role:%d, restoreFinish:%d",
691
            pNode->vgId, pEntry->index, pEntry->term, TMSG_INFO(pEntry->originalRpcType), applyCode, pNode->replicaNum,
692
            pNode->raftCfg.cfg.nodeInfo[pNode->raftCfg.cfg.myIndex].nodeRole, pNode->restoreFinish);
693
    return 0;
37,459✔
694
  }
695

696
  if (pNode->vgId != 1 && syncIsMsgBlock(pEntry->originalRpcType)) {
94,644✔
697
    sTrace("vgId:%d, index:%" PRId64 ", blocking msg ready to execute, term:%" PRId64 ", type:%s code:0x%x",
127✔
698
           pNode->vgId, pEntry->index, pEntry->term, TMSG_INFO(pEntry->originalRpcType), applyCode);
699
  }
700

701
  if (pEntry->originalRpcType == TDMT_VND_COMMIT) {
94,644✔
702
    sInfo("vgId:%d, index:%" PRId64 ", fsm execute vnode commit, term:%" PRId64, pNode->vgId, pEntry->index,
567✔
703
          pEntry->term);
704
  }
705

706
  int32_t code = 0, lino = 0;
94,645✔
707
  bool    retry = false;
94,645✔
708
  do {
709
    SFsmCbMeta cbMeta = {0};
94,645✔
710
    cbMeta.lastConfigIndex = syncNodeGetSnapshotConfigIndex(pNode, pEntry->index);
94,645✔
711
    if (cbMeta.lastConfigIndex < -1) {
94,646✔
712
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
713
      if (terrno != 0) code = terrno;
×
714
      return code;
×
715
    }
716

717
    SRpcMsg rpcMsg = {.code = applyCode};
94,646✔
718
    TAOS_CHECK_EXIT(syncEntry2OriginalRpc(pEntry, &rpcMsg));
94,646✔
719

720
    cbMeta.index = pEntry->index;
94,646✔
721
    cbMeta.isWeak = pEntry->isWeak;
94,646✔
722
    cbMeta.code = applyCode;
94,646✔
723
    cbMeta.state = role;
94,646✔
724
    cbMeta.seqNum = pEntry->seqNum;
94,646✔
725
    cbMeta.term = pEntry->term;
94,646✔
726
    cbMeta.currentTerm = term;
94,646✔
727
    cbMeta.flag = -1;
94,646✔
728
    rpcMsg.info.traceId = pEntry->originRpcTraceId;
94,646✔
729

730
    int32_t num = syncRespMgrGetAndDel(pNode->pSyncRespMgr, cbMeta.seqNum, &rpcMsg.info);
94,646✔
731
    sGDebug(&rpcMsg.info.traceId, "vgId:%d, index:%" PRId64 ", get response info, handle:%p seq:%" PRId64 " num:%d",
94,647✔
732
            pNode->vgId, pEntry->index, &rpcMsg.info, cbMeta.seqNum, num);
733

734
    code = pFsm->FpCommitCb(pFsm, &rpcMsg, &cbMeta);
94,647✔
735
    retry = (code != 0) && (terrno == TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE);
94,646✔
736

737
    sGTrace(&rpcMsg.info.traceId,
94,645✔
738
            "vgId:%d, index:%" PRId64 ", fsm execute, term:%" PRId64 ", type:%s, code:%d, retry:%d", pNode->vgId,
739
            pEntry->index, pEntry->term, TMSG_INFO(pEntry->originalRpcType), code, retry);
740

741
    if (retry) {
94,645✔
742
      taosMsleep(10);
×
743
      if (code == TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE) {
×
744
        pNode->applyQueueErrorCount++;
×
745
        if (pNode->applyQueueErrorCount == APPLY_QUEUE_ERROR_THRESHOLD) {
×
746
          pNode->applyQueueErrorCount = 0;
×
747
          sGWarn(&rpcMsg.info.traceId,
×
748
                 "vgId:%d, index:%" PRId64 ", will retry to execute fsm after 10ms, last error is %s", pNode->vgId,
749
                 pEntry->index, tstrerror(code));
750
        } else {
751
          sGTrace(&rpcMsg.info.traceId,
×
752
                  "vgId:%d, index:%" PRId64 ", will retry to execute fsm after 10ms, last error is %s", pNode->vgId,
753
                  pEntry->index, tstrerror(code));
754
        }
755
      }
756
    }
757
  } while (retry);
94,645✔
758

759
_exit:
94,645✔
760
  if (code < 0) {
94,645✔
761
    sError("vgId:%d, index:%" PRId64 ", failed to execute fsm at line %d since %s, term:%" PRId64 ", type:%s",
×
762
           pNode->vgId, pEntry->index, lino, tstrerror(code), pEntry->term, TMSG_INFO(pEntry->originalRpcType));
763
  }
764
  return code;
94,645✔
765
}
766

767
int32_t syncLogBufferValidate(SSyncLogBuffer* pBuf) {
847,163✔
768
  if (pBuf->startIndex > pBuf->matchIndex) {
847,163✔
769
    sError("failed to validate, pBuf->startIndex:%" PRId64 ", pBuf->matchIndex:%" PRId64, pBuf->startIndex,
×
770
           pBuf->matchIndex);
771
    return TSDB_CODE_SYN_INTERNAL_ERROR;
×
772
  }
773
  if (pBuf->commitIndex > pBuf->matchIndex) {
847,163✔
774
    sError("failed to validate, pBuf->commitIndex:%" PRId64 ", pBuf->matchIndex:%" PRId64, pBuf->commitIndex,
×
775
           pBuf->matchIndex);
776
    return TSDB_CODE_SYN_INTERNAL_ERROR;
×
777
  }
778
  if (pBuf->matchIndex >= pBuf->endIndex) {
847,163✔
779
    sError("failed to validate, pBuf->matchIndex:%" PRId64 ", pBuf->endIndex:%" PRId64, pBuf->matchIndex,
×
780
           pBuf->endIndex);
781
    return TSDB_CODE_SYN_INTERNAL_ERROR;
×
782
  }
783
  if (pBuf->endIndex - pBuf->startIndex > pBuf->size) {
847,163✔
784
    sError("failed to validate, pBuf->endIndex:%" PRId64 ", pBuf->startIndex:%" PRId64 ", pBuf->size:%" PRId64,
×
785
           pBuf->endIndex, pBuf->startIndex, pBuf->size);
786
    return TSDB_CODE_SYN_INTERNAL_ERROR;
×
787
  }
788
  if (pBuf->entries[(pBuf->matchIndex + pBuf->size) % pBuf->size].pItem == NULL) {
847,163✔
789
    sError("failed to validate since pItem is null");
×
790
    return TSDB_CODE_SYN_INTERNAL_ERROR;
×
791
  }
792
  return 0;
847,163✔
793
}
794

795
int32_t syncLogBufferCommit(SSyncLogBuffer* pBuf, SSyncNode* pNode, int64_t commitIndex, const STraceId* trace,
163,830✔
796
                            const char* src) {
797
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
163,830✔
798
  (void)taosThreadMutexLock(&pBuf->mutex);
163,833✔
799

800
  SSyncLogStore*  pLogStore = pNode->pLogStore;
163,835✔
801
  SSyncFSM*       pFsm = pNode->pFsm;
163,835✔
802
  ESyncState      role = pNode->state;
163,835✔
803
  SyncTerm        currentTerm = raftStoreGetTerm(pNode);
163,835✔
804
  SyncGroupId     vgId = pNode->vgId;
163,836✔
805
  int32_t         code = 0;
163,836✔
806
  int64_t         upperIndex = TMIN(commitIndex, pBuf->matchIndex);
163,836✔
807
  SSyncRaftEntry* pEntry = NULL;
163,836✔
808
  bool            inBuf = false;
163,836✔
809
  SSyncRaftEntry* pNextEntry = NULL;
163,836✔
810
  bool            nextInBuf = false;
163,836✔
811
  bool            restoreFinishAtThisCommit = false;
163,836✔
812

813
  if (commitIndex <= pBuf->commitIndex) {
163,836✔
814
    sGDebug(trace, "vgId:%d, stale commit index:%" PRId64 ", notified:%" PRId64, vgId, commitIndex, pBuf->commitIndex);
34,446✔
815
    if (!pNode->restoreFinish && commitIndex > 0 && commitIndex == pBuf->commitIndex) {
34,446✔
816
      int32_t ret = syncLogBufferGetOneEntry(pBuf, pNode, commitIndex, &inBuf, &pEntry);
89✔
817
      if (ret != 0) {
89✔
818
        sError("vgId:%d, failed to get entry at index:%" PRId64, vgId, commitIndex);
3✔
819
      }
820
    }
821
    goto _out;
34,446✔
822
  }
823

824
  sGDebug(trace,
129,390✔
825
          "vgId:%d, log commit since %s, buffer:[%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64
826
          "), role:%d, term:%" PRId64,
827
          pNode->vgId, src, pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex, role, currentTerm);
828

829
  // execute in fsm
830
  for (int64_t index = pBuf->commitIndex + 1; index <= upperIndex; index++) {
261,490✔
831
    // get a log entry
832
    code = syncLogBufferGetOneEntry(pBuf, pNode, index, &inBuf, &pEntry);
132,103✔
833
    if (pEntry == NULL) {
132,103✔
834
      goto _out;
×
835
    }
836

837
    // execute it
838
    if (!syncUtilUserCommit(pEntry->originalRpcType)) {
132,103✔
839
      sGInfo(&pEntry->originRpcTraceId,
443✔
840
             "vgId:%d, index:%" PRId64 ", log commit sync barrier, term:%" PRId64 ", type:%s", vgId, pEntry->index,
841
             pEntry->term, TMSG_INFO(pEntry->originalRpcType));
842
    }
843

844
    if ((code = syncFsmExecute(pNode, pFsm, role, currentTerm, pEntry, 0, false)) != 0) {
132,105✔
845
      sGError(&pEntry->originRpcTraceId,
×
846
              "vgId:%d, index:%" PRId64 ", failed to execute sync log entry, term:%" PRId64
847
              ", role:%d, current term:%" PRId64,
848
              vgId, pEntry->index, pEntry->term, role, currentTerm);
849
      goto _out;
×
850
    }
851
    pBuf->commitIndex = index;
132,104✔
852

853
    sGDebug(&pEntry->originRpcTraceId,
132,104✔
854
            "vgId:%d, index:%" PRId64 ", raft entry committed, term:%" PRId64 ", role:%d, current term:%" PRId64,
855
            pNode->vgId, pEntry->index, pEntry->term, role, currentTerm);
856

857
    code = syncLogBufferGetOneEntry(pBuf, pNode, index + 1, &nextInBuf, &pNextEntry);
132,104✔
858
    if (pNextEntry != NULL) {
132,102✔
859
      if (pNextEntry->originalRpcType == TDMT_SYNC_CONFIG_CHANGE) {
60,469✔
860
        sInfo(
×
861
            "vgId:%d, to change config at commit, "
862
            "current entry, index:%" PRId64 ", term:%" PRId64
863
            ", "
864
            "node, role:%d, current term:%" PRId64
865
            ", restore:%d, "
866
            "cond, next entry index:%" PRId64 ", msgType:%s",
867
            vgId, pEntry->index, pEntry->term, role, currentTerm, pNode->restoreFinish, pNextEntry->index,
868
            TMSG_INFO(pNextEntry->originalRpcType));
869

870
        if ((code = syncNodeChangeConfig(pNode, pNextEntry, "Commit")) != 0) {
×
871
          sError("vgId:%d, failed to change config from Commit, index:%" PRId64 ", term:%" PRId64
×
872
                 ", role:%d, current term:%" PRId64,
873
                 vgId, pNextEntry->index, pNextEntry->term, role, currentTerm);
874
          goto _out;
×
875
        }
876

877
        // for 2->1, need to apply config change entry in sync thread,
878
        if (pNode->replicaNum == 1) {
×
879
          if ((code = syncFsmExecute(pNode, pFsm, role, currentTerm, pNextEntry, 0, true)) != 0) {
×
880
            sError("vgId:%d, failed to execute sync log entry, index:%" PRId64 ", term:%" PRId64
×
881
                   ", role:%d, current term:%" PRId64,
882
                   vgId, pNextEntry->index, pNextEntry->term, role, currentTerm);
883
            goto _out;
×
884
          }
885

886
          index++;
×
887
          pBuf->commitIndex = index;
×
888

889
          sGDebug(&pNextEntry->originRpcTraceId,
×
890
                  "vgId:%d, index:%" PRId64 ", raft entry committed, term:%" PRId64 ", role:%d, current term:%" PRId64,
891
                  pNode->vgId, pNextEntry->index, pNextEntry->term, role, currentTerm);
892
        }
893
      }
894
      if (!nextInBuf) {
60,468✔
895
        syncEntryDestroy(pNextEntry);
×
896
        pNextEntry = NULL;
×
897
      }
898
    }
899

900
    if (!inBuf) {
132,101✔
901
      syncEntryDestroy(pEntry);
×
902
      pEntry = NULL;
×
903
    }
904
  }
905

906
  // recycle
907
  bool      isVnode = pNode->vgId > 1;
129,387✔
908
  SyncIndex until = pBuf->commitIndex - TSDB_SYNC_LOG_BUFFER_RETENTION;
129,387✔
909
  do {
113,015✔
910
    if ((pBuf->startIndex >= pBuf->commitIndex) ||
242,402✔
911
        !((pBuf->startIndex < until) || (isVnode && pBuf->bytes >= TSDB_SYNC_LOG_BUFFER_THRESHOLD &&
242,400✔
912
                                         atomic_load_64(&tsLogBufferMemoryUsed) >= tsLogBufferMemoryAllowed))) {
506✔
913
      break;
914
    }
915
    SSyncRaftEntry* pEntry = pBuf->entries[(pBuf->startIndex + pBuf->size) % pBuf->size].pItem;
113,015✔
916
    if (pEntry == NULL) {
113,015✔
917
      sError("vgId:%d, invalid log entry to recycle, index:%" PRId64 ", startIndex:%" PRId64 ", until:%" PRId64
×
918
             ", commitIndex:%" PRId64 ", endIndex:%" PRId64 ", term:%" PRId64,
919
             pNode->vgId, pEntry->index, pBuf->startIndex, until, pBuf->commitIndex, pBuf->endIndex, pEntry->term);
920
      return TSDB_CODE_SYN_INTERNAL_ERROR;
×
921
    }
922
    if (isVnode) {
113,015✔
923
      pBuf->bytes -= pEntry->bytes;
112,935✔
924
      (void)atomic_sub_fetch_64(&tsLogBufferMemoryUsed, (int64_t)pEntry->bytes);
112,935✔
925
    }
926
    sDebug("vgId:%d, recycle log entry, index:%" PRId64 ", startIndex:%" PRId64 ", until:%" PRId64
113,016✔
927
           ", commitIndex:%" PRId64 ", endIndex:%" PRId64 ", term:%" PRId64 ", entry bytes:%u, buf bytes:%" PRId64
928
           ", used:%" PRId64 ", allowed:%" PRId64,
929
           pNode->vgId, pEntry->index, pBuf->startIndex, until, pBuf->commitIndex, pBuf->endIndex, pEntry->term,
930
           pEntry->bytes, pBuf->bytes, atomic_load_64(&tsLogBufferMemoryUsed), tsLogBufferMemoryAllowed);
931
    syncEntryDestroy(pEntry);
113,016✔
932
    (void)memset(&pBuf->entries[(pBuf->startIndex + pBuf->size) % pBuf->size], 0, sizeof(pBuf->entries[0]));
113,015✔
933
    ++pBuf->startIndex;
113,015✔
934
  } while (true);
935

936
  code = 0;
129,387✔
937
_out:
163,833✔
938
  // mark as restored if needed
939
  if (!pNode->restoreFinish && pBuf->commitIndex >= pNode->commitIndex && pEntry != NULL &&
163,833✔
940
      currentTerm <= pEntry->term) {
451✔
941
    pNode->pFsm->FpRestoreFinishCb(pNode->pFsm, pBuf->commitIndex);
312✔
942
    pNode->restoreFinish = true;
312✔
943
    restoreFinishAtThisCommit = true;
312✔
944
    sInfo("vgId:%d, restore finished, term:%" PRId64 ", log buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 ")",
312✔
945
          pNode->vgId, currentTerm, pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
946
  }
947

948
  if (!inBuf) {
163,833✔
949
    syncEntryDestroy(pEntry);
34,424✔
950
    pEntry = NULL;
34,423✔
951
  }
952
  if (!nextInBuf) {
163,832✔
953
    syncEntryDestroy(pNextEntry);
105,995✔
954
    pNextEntry = NULL;
105,998✔
955
  }
956
  (void)taosThreadMutexUnlock(&pBuf->mutex);
163,835✔
957

958
  if (restoreFinishAtThisCommit && pNode->pFsm->FpAfterRestoredCb != NULL) {
163,836✔
959
    pNode->pFsm->FpAfterRestoredCb(pNode->pFsm, pBuf->commitIndex);
57✔
960
    sInfo("vgId:%d, after restore finished callback executed)", pNode->vgId);
57✔
961
  }
962

963
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
163,836✔
964
  TAOS_RETURN(code);
163,834✔
965
}
966

967
void syncLogReplReset(SSyncLogReplMgr* pMgr) {
841✔
968
  if (pMgr == NULL) return;
841✔
969

970
  if (pMgr->startIndex < 0) {
841✔
971
    sError("failed to reset, pMgr->startIndex:%" PRId64, pMgr->startIndex);
×
972
    return;
×
973
  }
974
  for (SyncIndex index = pMgr->startIndex; index < pMgr->endIndex; index++) {
895✔
975
    (void)memset(&pMgr->states[index % pMgr->size], 0, sizeof(pMgr->states[0]));
54✔
976
  }
977
  pMgr->startIndex = 0;
841✔
978
  pMgr->matchIndex = 0;
841✔
979
  pMgr->endIndex = 0;
841✔
980
  pMgr->restored = false;
841✔
981
  pMgr->retryBackoff = 0;
841✔
982
}
983

984
int32_t syncLogReplRetryOnNeed(SSyncLogReplMgr* pMgr, SSyncNode* pNode) {
120,519✔
985
  if (pMgr->endIndex <= pMgr->startIndex) {
120,519✔
986
    return 0;
×
987
  }
988

989
  SRaftId* pDestId = &pNode->replicasId[pMgr->peerId];
120,519✔
990
  if (pMgr->retryBackoff == SYNC_MAX_RETRY_BACKOFF) {
120,519✔
991
    syncLogReplReset(pMgr);
×
992
    sWarn("vgId:%d, reset sync log repl since retry backoff exceeding limit, peer addr:0x%" PRIx64, pNode->vgId,
×
993
          pDestId->addr);
994
    return TSDB_CODE_OUT_OF_RANGE;
×
995
  }
996

997
  int32_t  code = 0;
120,519✔
998
  bool     retried = false;
120,519✔
999
  int64_t  retryWaitMs = syncLogReplGetRetryBackoffTimeMs(pMgr);
120,519✔
1000
  int64_t  nowMs = taosGetMonoTimestampMs();
120,521✔
1001
  int      count = 0;
120,521✔
1002
  int64_t  firstIndex = -1;
120,521✔
1003
  SyncTerm term = -1;
120,521✔
1004
  int64_t  batchSize = TMAX(1, pMgr->size >> (4 + pMgr->retryBackoff));
120,521✔
1005

1006
  for (SyncIndex index = pMgr->startIndex; index < pMgr->endIndex; index++) {
122,630✔
1007
    int64_t pos = index % pMgr->size;
122,118✔
1008
    if (!(!pMgr->states[pos].barrier || (index == pMgr->startIndex || index + 1 == pMgr->endIndex))) {
122,118✔
1009
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
1010
      goto _out;
×
1011
    }
1012

1013
    if (nowMs < pMgr->states[pos].timeMs + retryWaitMs) {
122,118✔
1014
      break;
120,009✔
1015
    }
1016

1017
    if (pMgr->states[pos].acked) {
2,109✔
1018
      if (pMgr->matchIndex < index && pMgr->states[pos].timeMs + (syncGetRetryMaxWaitMs() << 3) < nowMs) {
2,107✔
1019
        syncLogReplReset(pMgr);
×
1020
        sWarn("vgId:%d, reset sync log repl since stagnation, index:%" PRId64 ", peer addr:0x%" PRIx64, pNode->vgId, index,
×
1021
              pDestId->addr);
1022
        code = TSDB_CODE_ACTION_IN_PROGRESS;
×
1023
        goto _out;
×
1024
      }
1025
      continue;
2,107✔
1026
    }
1027

1028
    bool barrier = false;
2✔
1029
    if ((code = syncLogReplSendTo(pMgr, pNode, index, &term, pDestId, &barrier)) < 0) {
2✔
1030
      sError("vgId:%d, failed to replicate sync log entry since %s, index:%" PRId64 ", dest addr:0x%" PRIx64, pNode->vgId,
×
1031
             tstrerror(code), index, pDestId->addr);
1032
      goto _out;
×
1033
    }
1034
    if (barrier != pMgr->states[pos].barrier) {
2✔
1035
      code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
1036
      goto _out;
×
1037
    }
1038
    pMgr->states[pos].timeMs = nowMs;
2✔
1039
    pMgr->states[pos].term = term;
2✔
1040
    pMgr->states[pos].acked = false;
2✔
1041

1042
    retried = true;
2✔
1043
    if (firstIndex == -1) firstIndex = index;
2✔
1044

1045
    if (batchSize <= count++) {
2✔
1046
      break;
×
1047
    }
1048
  }
1049

1050
_out:
512✔
1051
  if (retried) {
120,521✔
1052
    pMgr->retryBackoff = syncLogReplGetNextRetryBackoff(pMgr);
1✔
1053
    SSyncLogBuffer* pBuf = pNode->pLogBuf;
1✔
1054
    sInfo("vgId:%d, resend %d sync log entries, dest addr:0x%" PRIx64 ", indexes:%" PRId64 " ..., terms: .., %" PRId64
1✔
1055
          ", retryWaitMs:%" PRId64 ", repl-mgr:[%" PRId64 " %" PRId64 ", %" PRId64 "), buffer: [%" PRId64 " %" PRId64
1056
          " %" PRId64 ", %" PRId64 ")",
1057
          pNode->vgId, count, pDestId->addr, firstIndex, term, retryWaitMs, pMgr->startIndex, pMgr->matchIndex,
1058
          pMgr->endIndex, pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
1059
  }
1060
  TAOS_RETURN(code);
120,521✔
1061
}
1062

1063
int32_t syncLogReplRecover(SSyncLogReplMgr* pMgr, SSyncNode* pNode, SyncAppendEntriesReply* pMsg) {
90✔
1064
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
90✔
1065
  SRaftId         destId = pMsg->srcId;
90✔
1066
  int32_t         code = 0;
90✔
1067
  if (pMgr->restored != false) return TSDB_CODE_SYN_INTERNAL_ERROR;
90✔
1068

1069
  sTrace("vgId:%d, begin to recover sync log repl, peer dnode:%d (0x%" PRIx64 "), repl-mgr:[%" PRId64 ", %" PRId64
90✔
1070
         ", %" PRId64 ") restore:%d, buffer: [%" PRId64 ", %" PRId64 ", %" PRId64 ", %" PRId64
1071
         "), msg: {lastSendIndex:%" PRId64 ", matchIndex:%" PRId64 ", fsmState:%d, success:%d, lastMatchTerm:%" PRId64
1072
         "}",
1073
         pNode->vgId, DID(&destId), destId.addr, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex, pMgr->restored,
1074
         pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex, pMsg->lastSendIndex, pMsg->matchIndex,
1075
         pMsg->fsmState, pMsg->success, pMsg->lastMatchTerm);
1076

1077
  if (pMgr->endIndex == 0) {
90✔
1078
    if (pMgr->startIndex != 0) return TSDB_CODE_SYN_INTERNAL_ERROR;
36✔
1079
    if (pMgr->matchIndex != 0) return TSDB_CODE_SYN_INTERNAL_ERROR;
36✔
1080
    if (pMsg->matchIndex < 0) {
36✔
1081
      pMgr->restored = true;
8✔
1082
      sInfo("vgId:%d, sync log repl restored, peer dnode:%d (0x%" PRIx64 "), repl-mgr:[%" PRId64 " %" PRId64 ", %" PRId64
8✔
1083
            "), buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 ")",
1084
            pNode->vgId, DID(&destId), destId.addr, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex,
1085
            pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
1086
      return 0;
8✔
1087
    }
1088
  } else {
1089
    if (pMsg->lastSendIndex < pMgr->startIndex || pMsg->lastSendIndex >= pMgr->endIndex) {
54✔
1090
      TAOS_CHECK_RETURN(syncLogReplRetryOnNeed(pMgr, pNode));
1✔
1091
      return 0;
1✔
1092
    }
1093

1094
    pMgr->states[pMsg->lastSendIndex % pMgr->size].acked = true;
53✔
1095

1096
    if (pMsg->success && pMsg->matchIndex == pMsg->lastSendIndex) {
53✔
1097
      pMgr->matchIndex = pMsg->matchIndex;
49✔
1098
      pMgr->restored = true;
49✔
1099
      sInfo("vgId:%d, sync log repl restored, peer dnode:%d (0x%" PRIx64 "), repl-mgr:[%" PRId64 " %" PRId64 ", %" PRId64
49✔
1100
            "), buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 ")",
1101
            pNode->vgId, DID(&destId), destId.addr, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex,
1102
            pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
1103
      return 0;
49✔
1104
    }
1105

1106
    if (pMsg->fsmState == SYNC_FSM_STATE_INCOMPLETE || (!pMsg->success && pMsg->matchIndex >= pMsg->lastSendIndex)) {
4✔
1107
      char* msg1 = " rollback match index failure";
×
1108
      char* msg2 = " incomplete fsm state";
×
1109
      sInfo("vgId:%d, snapshot replication to dnode:%d, reason:%s, match index:%" PRId64 ", last sent:%" PRId64,
×
1110
            pNode->vgId, DID(&destId), (pMsg->fsmState == SYNC_FSM_STATE_INCOMPLETE ? msg2 : msg1), pMsg->matchIndex,
1111
            pMsg->lastSendIndex);
1112
      if ((code = syncNodeStartSnapshot(pNode, &destId)) < 0) {
×
1113
        sError("vgId:%d, failed to start snapshot for peer dnode:%d", pNode->vgId, DID(&destId));
×
1114
        TAOS_RETURN(code);
×
1115
      }
1116
      return 0;
×
1117
    }
1118
  }
1119

1120
  // check last match term
1121
  SyncTerm  term = -1;
32✔
1122
  SyncIndex firstVer = pNode->pLogStore->syncLogBeginIndex(pNode->pLogStore);
32✔
1123
  SyncIndex index = TMIN(pMsg->matchIndex, pNode->pLogBuf->matchIndex);
32✔
1124
  SET_ERRNO(0);
32✔
1125

1126
  if (pMsg->matchIndex < pNode->pLogBuf->matchIndex) {
32✔
1127
    code = syncLogReplGetPrevLogTerm(pMgr, pNode, index + 1, &term);
9✔
1128
    if (term < 0 && (ERRNO == ENFILE || ERRNO == EMFILE || ERRNO == ENOENT)) {
9✔
1129
      sError("vgId:%d, failed to get prev log term since %s, index:%" PRId64, pNode->vgId, tstrerror(code), index + 1);
×
1130
      TAOS_RETURN(code);
×
1131
    }
1132

1133
    if (pMsg->matchIndex == -1) {
9✔
1134
      // first time to restore
1135
      sInfo("vgId:%d, first time to restore sync log repl, peer dnode:%d (0x%" PRIx64 "), repl-mgr:[%" PRId64 " %" PRId64
4✔
1136
            ", %" PRId64 "), buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 "), index:%" PRId64
1137
            ", firstVer:%" PRId64 ", term:%" PRId64 ", lastMatchTerm:%" PRId64,
1138
            pNode->vgId, DID(&destId), destId.addr, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex,
1139
            pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex, index, firstVer, term,
1140
            pMsg->lastMatchTerm);
1141
    }
1142

1143
    if ((index + 1 < firstVer) || (term < 0) ||
9✔
1144
        (term != pMsg->lastMatchTerm && (index + 1 == firstVer || index == firstVer))) {
9✔
1145
      if (!(term >= 0 || terrno == TSDB_CODE_WAL_LOG_NOT_EXIST)) return TSDB_CODE_SYN_INTERNAL_ERROR;
×
1146
      if ((code = syncNodeStartSnapshot(pNode, &destId)) < 0) {
×
1147
        sError("vgId:%d, failed to start snapshot for peer dnode:%d", pNode->vgId, DID(&destId));
×
1148
        TAOS_RETURN(code);
×
1149
      }
1150
      sInfo("vgId:%d, snapshot replication to peer dnode:%d", pNode->vgId, DID(&destId));
×
1151
      return 0;
×
1152
    }
1153

1154
    if (!(index + 1 >= firstVer)) return TSDB_CODE_SYN_INTERNAL_ERROR;
9✔
1155

1156
    if (term == pMsg->lastMatchTerm) {
9✔
1157
      index = index + 1;
9✔
1158
      if (!(index <= pNode->pLogBuf->matchIndex)) return TSDB_CODE_SYN_INTERNAL_ERROR;
9✔
1159
    } else {
1160
      if (!(index > firstVer)) return TSDB_CODE_SYN_INTERNAL_ERROR;
×
1161
    }
1162
  }
1163

1164
  // attempt to replicate the raft log at index
1165
  syncLogReplReset(pMgr);
32✔
1166
  return syncLogReplProbe(pMgr, pNode, index);
32✔
1167
}
1168

1169
int32_t syncLogReplProcessHeartbeatReply(SSyncLogReplMgr* pMgr, SSyncNode* pNode, SyncHeartbeatReply* pMsg) {
3,931✔
1170
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
3,931✔
1171
  (void)taosThreadMutexLock(&pMgr->mutex);
3,931✔
1172
  if (pMsg->startTime != 0 && pMsg->startTime != pMgr->peerStartTime) {
3,931✔
1173
    sInfo("vgId:%d, reset sync log repl in heartbeat, peer addr:0x%" PRIx64 ", start time:%" PRId64 ", old:%" PRId64,
47✔
1174
          pNode->vgId, pMsg->srcId.addr, pMsg->startTime, pMgr->peerStartTime);
1175
    syncLogReplReset(pMgr);
47✔
1176
    pMgr->peerStartTime = pMsg->startTime;
47✔
1177
  }
1178
  (void)taosThreadMutexUnlock(&pMgr->mutex);
3,931✔
1179
  return 0;
3,931✔
1180
}
1181

1182
int32_t syncLogReplProcessReply(SSyncLogReplMgr* pMgr, SSyncNode* pNode, SyncAppendEntriesReply* pMsg) {
60,092✔
1183
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
60,092✔
1184
  (void)taosThreadMutexLock(&pMgr->mutex);
60,092✔
1185
  if (pMsg->startTime != pMgr->peerStartTime) {
60,094✔
1186
    sInfo("vgId:%d, reset sync log repl in appendlog reply, peer addr:0x%" PRIx64 ", start time:%" PRId64 ", old:%" PRId64,
10✔
1187
          pNode->vgId, pMsg->srcId.addr, pMsg->startTime, pMgr->peerStartTime);
1188
    syncLogReplReset(pMgr);
10✔
1189
    pMgr->peerStartTime = pMsg->startTime;
10✔
1190
  }
1191
  (void)taosThreadMutexUnlock(&pMgr->mutex);
60,094✔
1192

1193
  (void)taosThreadMutexLock(&pBuf->mutex);
60,093✔
1194

1195
  int32_t code = 0;
60,094✔
1196
  if (pMgr->restored) {
60,094✔
1197
    if ((code = syncLogReplContinue(pMgr, pNode, pMsg)) != 0) {
60,004✔
1198
      sWarn("vgId:%d, failed to continue sync log repl since %s", pNode->vgId, tstrerror(code));
×
1199
    }
1200
  } else {
1201
    if ((code = syncLogReplRecover(pMgr, pNode, pMsg)) != 0) {
90✔
1202
      sWarn("vgId:%d, failed to recover sync log repl since %s", pNode->vgId, tstrerror(code));
×
1203
    }
1204
  }
1205
  (void)taosThreadMutexUnlock(&pBuf->mutex);
60,094✔
1206
  return 0;
60,093✔
1207
}
1208

1209
int32_t syncLogReplStart(SSyncLogReplMgr* pMgr, SSyncNode* pNode) {
60,579✔
1210
  if (pMgr->restored) {
60,579✔
1211
    TAOS_CHECK_RETURN(syncLogReplAttempt(pMgr, pNode));
60,515✔
1212
  } else {
1213
    TAOS_CHECK_RETURN(syncLogReplProbe(pMgr, pNode, pNode->pLogBuf->matchIndex));
64✔
1214
  }
1215
  return 0;
60,580✔
1216
}
1217

1218
int32_t syncLogReplProbe(SSyncLogReplMgr* pMgr, SSyncNode* pNode, SyncIndex index) {
96✔
1219
  if (pMgr->restored) return TSDB_CODE_SYN_INTERNAL_ERROR;
96✔
1220
  if (!(pMgr->startIndex >= 0)) return TSDB_CODE_SYN_INTERNAL_ERROR;
96✔
1221
  int64_t retryMaxWaitMs = syncGetRetryMaxWaitMs();
96✔
1222
  int64_t nowMs = taosGetMonoTimestampMs();
96✔
1223
  int32_t code = 0;
96✔
1224

1225
  sTrace("vgId:%d, begin to probe peer addr:0x%" PRIx64 " with msg of index:%" PRId64 ", repl-mgr:[%" PRId64 ", %" PRId64
96✔
1226
         ", %" PRId64 "), restored:%d",
1227
         pNode->vgId, pNode->replicasId[pMgr->peerId].addr, index, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex,
1228
         pMgr->restored);
1229

1230
  if (pMgr->endIndex > pMgr->startIndex &&
96✔
1231
      nowMs < pMgr->states[pMgr->startIndex % pMgr->size].timeMs + retryMaxWaitMs) {
6✔
1232
    return 0;
5✔
1233
  }
1234
  syncLogReplReset(pMgr);
91✔
1235

1236
  SRaftId* pDestId = &pNode->replicasId[pMgr->peerId];
91✔
1237
  bool     barrier = false;
91✔
1238
  SyncTerm term = -1;
91✔
1239
  if ((code = syncLogReplSendTo(pMgr, pNode, index, &term, pDestId, &barrier)) < 0) {
91✔
1240
    sError("vgId:%d, failed to replicate log entry since %s, index:%" PRId64 ", dest addr:0x%016" PRIx64, pNode->vgId,
×
1241
           tstrerror(code), index, pDestId->addr);
1242
    TAOS_RETURN(code);
×
1243
  }
1244

1245
  if (!(index >= 0)) return TSDB_CODE_SYN_INTERNAL_ERROR;
91✔
1246
  pMgr->states[index % pMgr->size].barrier = barrier;
91✔
1247
  pMgr->states[index % pMgr->size].timeMs = nowMs;
91✔
1248
  pMgr->states[index % pMgr->size].term = term;
91✔
1249
  pMgr->states[index % pMgr->size].acked = false;
91✔
1250

1251
  pMgr->startIndex = index;
91✔
1252
  pMgr->endIndex = index + 1;
91✔
1253

1254
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
91✔
1255
  sTrace("vgId:%d, probe peer addr:0x%" PRIx64 " with msg of index:%" PRId64 " term:%" PRId64 ", repl-mgr:[%" PRId64
91✔
1256
         " %" PRId64 ", %" PRId64 "), buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 ")",
1257
         pNode->vgId, pDestId->addr, index, term, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex, pBuf->startIndex,
1258
         pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
1259
  return 0;
91✔
1260
}
1261

1262
int32_t syncLogReplAttempt(SSyncLogReplMgr* pMgr, SSyncNode* pNode) {
120,518✔
1263
  if (!pMgr->restored) return TSDB_CODE_SYN_INTERNAL_ERROR;
120,518✔
1264

1265
  sTrace("vgId:%d, replicate raft entries from end to match, repl-mgr:[%" PRId64 ", %" PRId64 ", %" PRId64
120,518✔
1266
         "), restore:%d",
1267
         pNode->vgId, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex, pMgr->restored);
1268

1269
  SRaftId*  pDestId = &pNode->replicasId[pMgr->peerId];
120,518✔
1270
  int32_t   batchSize = TMAX(1, pMgr->size >> (4 + pMgr->retryBackoff));
120,518✔
1271
  int32_t   code = 0;
120,518✔
1272
  int32_t   count = 0;
120,518✔
1273
  int64_t   nowMs = taosGetMonoTimestampMs();
120,516✔
1274
  int64_t   limit = pMgr->size >> 1;
120,516✔
1275
  SyncTerm  term = -1;
120,516✔
1276
  SyncIndex firstIndex = -1;
120,516✔
1277

1278
  for (SyncIndex index = pMgr->endIndex; index <= pNode->pLogBuf->matchIndex; index++) {
180,489✔
1279
    if (batchSize < count || limit <= index - pMgr->startIndex) {
60,058✔
1280
      break;
1281
    }
1282
    if (pMgr->startIndex + 1 < index && pMgr->states[(index - 1) % pMgr->size].barrier) {
60,058✔
1283
      break;
44✔
1284
    }
1285
    int64_t  pos = index % pMgr->size;
60,014✔
1286
    SRaftId* pDestId = &pNode->replicasId[pMgr->peerId];
60,014✔
1287
    bool     barrier = false;
60,014✔
1288
    SyncTerm term = -1;
60,014✔
1289

1290
    code = syncLogReplSendTo(pMgr, pNode, index, &term, pDestId, &barrier);
60,014✔
1291
    if (code < 0) {
60,016✔
1292
      sError("vgId:%d, failed to replicate log entry since %s, index:%" PRId64 ", dest addr:0x%016" PRIx64, pNode->vgId,
×
1293
             tstrerror(code), index, pDestId->addr);
1294
      TAOS_RETURN(code);
×
1295
    }
1296
    pMgr->states[pos].barrier = barrier;
60,016✔
1297
    pMgr->states[pos].timeMs = nowMs;
60,016✔
1298
    pMgr->states[pos].term = term;
60,016✔
1299
    pMgr->states[pos].acked = false;
60,016✔
1300

1301
    if (firstIndex == -1) {
60,016✔
1302
      firstIndex = index;
59,820✔
1303
    }
1304

1305
    count++;
60,016✔
1306

1307
    pMgr->endIndex = index + 1;
60,016✔
1308
    if (barrier) {
60,016✔
1309
      sInfo("vgId:%d, replicated sync barrier to dnode:%d, index:%" PRId64 ", term:%" PRId64 ", repl-mgr:[%" PRId64
43✔
1310
            " %" PRId64 ", %" PRId64 ")",
1311
            pNode->vgId, DID(pDestId), index, term, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex);
1312
      break;
43✔
1313
    }
1314
  }
1315

1316
  TAOS_CHECK_RETURN(syncLogReplRetryOnNeed(pMgr, pNode));
120,518✔
1317

1318
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
120,518✔
1319
  sTrace("vgId:%d, replicated %d msgs to peer addr:0x%" PRIx64 ", indexes:%" PRId64 "..., terms: ...%" PRId64
120,518✔
1320
         ", repl-mgr:[%" PRId64 " %" PRId64 ", %" PRId64 "), buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64
1321
         ")",
1322
         pNode->vgId, count, pDestId->addr, firstIndex, term, pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex,
1323
         pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
1324
  return 0;
120,519✔
1325
}
1326

1327
int32_t syncLogReplContinue(SSyncLogReplMgr* pMgr, SSyncNode* pNode, SyncAppendEntriesReply* pMsg) {
60,002✔
1328
  if (pMgr->restored != true) return TSDB_CODE_SYN_INTERNAL_ERROR;
60,002✔
1329
  if (pMgr->startIndex <= pMsg->lastSendIndex && pMsg->lastSendIndex < pMgr->endIndex) {
60,002✔
1330
    if (pMgr->startIndex < pMgr->matchIndex && pMgr->retryBackoff > 0) {
59,905✔
1331
      int64_t firstMs = pMgr->states[pMgr->startIndex % pMgr->size].timeMs;
×
1332
      int64_t lastMs = pMgr->states[(pMgr->endIndex - 1) % pMgr->size].timeMs;
×
1333
      int64_t diffMs = lastMs - firstMs;
×
1334
      if (diffMs > 0 && diffMs < ((int64_t)SYNC_LOG_REPL_RETRY_WAIT_MS << (pMgr->retryBackoff - 1))) {
×
1335
        pMgr->retryBackoff -= 1;
×
1336
      }
1337
    }
1338
    pMgr->states[pMsg->lastSendIndex % pMgr->size].acked = true;
59,905✔
1339
    pMgr->matchIndex = TMAX(pMgr->matchIndex, pMsg->matchIndex);
59,905✔
1340
    for (SyncIndex index = pMgr->startIndex; index < pMgr->matchIndex; index++) {
119,899✔
1341
      (void)memset(&pMgr->states[index % pMgr->size], 0, sizeof(pMgr->states[0]));
59,994✔
1342
    }
1343
    pMgr->startIndex = pMgr->matchIndex;
59,905✔
1344
  }
1345

1346
  return syncLogReplAttempt(pMgr, pNode);
60,002✔
1347
}
1348

1349
SSyncLogReplMgr* syncLogReplCreate() {
4,484✔
1350
  SSyncLogReplMgr* pMgr = taosMemoryCalloc(1, sizeof(SSyncLogReplMgr));
4,484✔
1351
  if (pMgr == NULL) {
4,485✔
1352
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1353
    return NULL;
×
1354
  }
1355

1356
  pMgr->size = sizeof(pMgr->states) / sizeof(pMgr->states[0]);
4,485✔
1357

1358
  if (pMgr->size != TSDB_SYNC_LOG_BUFFER_SIZE) {
4,485✔
1359
    terrno = TSDB_CODE_SYN_INTERNAL_ERROR;
×
1360
    return NULL;
×
1361
  }
1362

1363
  int32_t code = taosThreadMutexInit(&pMgr->mutex, NULL);
4,485✔
1364
  if (code) {
4,484✔
1365
    terrno = code;
×
1366
    return NULL;
×
1367
  }
1368

1369
  return pMgr;
4,484✔
1370
}
1371

1372
void syncLogReplDestroy(SSyncLogReplMgr* pMgr) {
4,439✔
1373
  if (pMgr == NULL) {
4,439✔
1374
    return;
×
1375
  }
1376
  (void)taosThreadMutexDestroy(&pMgr->mutex);
4,439✔
1377
  taosMemoryFree(pMgr);
4,437✔
1378
  return;
4,440✔
1379
}
1380

1381
int32_t syncNodeLogReplInit(SSyncNode* pNode) {
299✔
1382
  for (int i = 0; i < TSDB_MAX_REPLICA + TSDB_MAX_LEARNER_REPLICA; i++) {
4,783✔
1383
    if (pNode->logReplMgrs[i] != NULL) return TSDB_CODE_SYN_INTERNAL_ERROR;
4,484✔
1384
    pNode->logReplMgrs[i] = syncLogReplCreate();
4,484✔
1385
    if (pNode->logReplMgrs[i] == NULL) {
4,484✔
1386
      TAOS_RETURN(terrno);
×
1387
    }
1388
    pNode->logReplMgrs[i]->peerId = i;
4,484✔
1389
  }
1390
  return 0;
299✔
1391
}
1392

1393
void syncNodeLogReplDestroy(SSyncNode* pNode) {
296✔
1394
  for (int i = 0; i < TSDB_MAX_REPLICA + TSDB_MAX_LEARNER_REPLICA; i++) {
4,736✔
1395
    syncLogReplDestroy(pNode->logReplMgrs[i]);
4,439✔
1396
    pNode->logReplMgrs[i] = NULL;
4,440✔
1397
  }
1398
}
297✔
1399

1400
int32_t syncLogBufferCreate(SSyncLogBuffer** ppBuf) {
299✔
1401
  int32_t         code = 0;
299✔
1402
  SSyncLogBuffer* pBuf = taosMemoryCalloc(1, sizeof(SSyncLogBuffer));
299✔
1403
  if (pBuf == NULL) {
299✔
1404
    TAOS_CHECK_GOTO(terrno, NULL, _exit);
×
1405
  }
1406

1407
  pBuf->size = sizeof(pBuf->entries) / sizeof(pBuf->entries[0]);
299✔
1408

1409
  if (pBuf->size != TSDB_SYNC_LOG_BUFFER_SIZE) {
299✔
1410
    code = TSDB_CODE_SYN_INTERNAL_ERROR;
×
1411
    goto _exit;
×
1412
  }
1413

1414
  if (taosThreadMutexAttrInit(&pBuf->attr) < 0) {
299✔
1415
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
1416
    sError("failed to init log buffer mutexattr due to %s", tstrerror(code));
×
1417
    goto _exit;
×
1418
  }
1419

1420
  if (taosThreadMutexAttrSetType(&pBuf->attr, PTHREAD_MUTEX_RECURSIVE) < 0) {
299✔
1421
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
1422
    sError("failed to set log buffer mutexattr type due to %s", tstrerror(code));
×
1423
    goto _exit;
×
1424
  }
1425

1426
  if (taosThreadMutexInit(&pBuf->mutex, &pBuf->attr) < 0) {
299✔
1427
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
1428
    sError("failed to init log buffer mutex due to %s", tstrerror(code));
×
1429
    goto _exit;
×
1430
  }
1431
_exit:
299✔
1432
  if (code != 0) {
299✔
1433
    taosMemoryFreeClear(pBuf);
×
1434
  }
1435
  *ppBuf = pBuf;
299✔
1436
  TAOS_RETURN(code);
299✔
1437
}
1438

1439
void syncLogBufferClear(SSyncLogBuffer* pBuf) {
296✔
1440
  (void)taosThreadMutexLock(&pBuf->mutex);
296✔
1441
  for (SyncIndex index = pBuf->startIndex; index < pBuf->endIndex; index++) {
19,696✔
1442
    SSyncRaftEntry* pEntry = pBuf->entries[(index + pBuf->size) % pBuf->size].pItem;
19,400✔
1443
    if (pEntry == NULL) continue;
19,400✔
1444
    syncEntryDestroy(pEntry);
19,400✔
1445
    pEntry = NULL;
19,400✔
1446
    (void)memset(&pBuf->entries[(index + pBuf->size) % pBuf->size], 0, sizeof(pBuf->entries[0]));
19,400✔
1447
  }
1448
  pBuf->startIndex = pBuf->commitIndex = pBuf->matchIndex = pBuf->endIndex = 0;
296✔
1449
  pBuf->bytes = 0;
296✔
1450
  (void)taosThreadMutexUnlock(&pBuf->mutex);
296✔
1451
}
296✔
1452

1453
void syncLogBufferDestroy(SSyncLogBuffer* pBuf) {
296✔
1454
  if (pBuf == NULL) {
296✔
1455
    return;
×
1456
  }
1457
  syncLogBufferClear(pBuf);
296✔
1458
  (void)taosThreadMutexDestroy(&pBuf->mutex);
296✔
1459
  (void)taosThreadMutexAttrDestroy(&pBuf->attr);
296✔
1460
  taosMemoryFree(pBuf);
296✔
1461
  return;
296✔
1462
}
1463

1464
int32_t syncLogBufferRollback(SSyncLogBuffer* pBuf, SSyncNode* pNode, SyncIndex toIndex) {
377✔
1465
  int32_t code = 0;
377✔
1466
  if (!(pBuf->commitIndex < toIndex && toIndex <= pBuf->endIndex)) return TSDB_CODE_SYN_INTERNAL_ERROR;
377✔
1467

1468
  if (toIndex == pBuf->endIndex) {
377✔
1469
    return 0;
377✔
1470
  }
1471

1472
  sInfo("vgId:%d, rollback sync log buffer, toindex:%" PRId64 ", buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64
×
1473
        ")",
1474
        pNode->vgId, toIndex, pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
1475

1476
  // trunc buffer
1477
  SyncIndex index = pBuf->endIndex - 1;
×
1478
  while (index >= toIndex) {
×
1479
    SSyncRaftEntry* pEntry = pBuf->entries[index % pBuf->size].pItem;
×
1480
    if (pEntry != NULL) {
×
1481
      syncEntryDestroy(pEntry);
×
1482
      pEntry = NULL;
×
1483
      (void)memset(&pBuf->entries[index % pBuf->size], 0, sizeof(pBuf->entries[0]));
×
1484
    }
1485
    index--;
×
1486
  }
1487
  pBuf->endIndex = toIndex;
×
1488
  pBuf->matchIndex = TMIN(pBuf->matchIndex, index);
×
1489
  if (index + 1 != toIndex) return TSDB_CODE_SYN_INTERNAL_ERROR;
×
1490

1491
  // trunc wal
1492
  SyncIndex lastVer = pNode->pLogStore->syncLogLastIndex(pNode->pLogStore);
×
1493
  if (lastVer >= toIndex && (code = pNode->pLogStore->syncLogTruncate(pNode->pLogStore, toIndex)) < 0) {
×
1494
    sError("vgId:%d, failed to truncate log store since %s, from index:%" PRId64, pNode->vgId, tstrerror(code),
×
1495
           toIndex);
1496
    TAOS_RETURN(code);
×
1497
  }
1498
  lastVer = pNode->pLogStore->syncLogLastIndex(pNode->pLogStore);
×
1499
  if (toIndex != lastVer + 1) return TSDB_CODE_SYN_INTERNAL_ERROR;
×
1500

1501
  // refill buffer on need
1502
  if (toIndex <= pBuf->startIndex) {
×
1503
    if ((code = syncLogBufferInitWithoutLock(pBuf, pNode)) < 0) {
×
1504
      sError("vgId:%d, failed to refill sync log buffer since %s", pNode->vgId, tstrerror(code));
×
1505
      TAOS_RETURN(code);
×
1506
    }
1507
  }
1508

1509
  if (pBuf->endIndex != toIndex) return TSDB_CODE_SYN_INTERNAL_ERROR;
×
1510
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
×
1511
  return 0;
×
1512
}
1513

1514
int32_t syncLogBufferReset(SSyncLogBuffer* pBuf, SSyncNode* pNode) {
377✔
1515
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
377✔
1516
  (void)taosThreadMutexLock(&pBuf->mutex);
377✔
1517
  SyncIndex lastVer = pNode->pLogStore->syncLogLastIndex(pNode->pLogStore);
377✔
1518
  if (lastVer != pBuf->matchIndex) return TSDB_CODE_SYN_INTERNAL_ERROR;
377✔
1519
  SyncIndex index = pBuf->endIndex - 1;
377✔
1520

1521
  int32_t code = 0;
377✔
1522
  if ((code = syncLogBufferRollback(pBuf, pNode, pBuf->matchIndex + 1)) != 0) {
377✔
1523
    sError("vgId:%d, failed to rollback sync log buffer since %s", pNode->vgId, tstrerror(code));
×
1524
  }
1525

1526
  sInfo("vgId:%d, reset sync log buffer, buffer: [%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 ")", pNode->vgId,
377✔
1527
        pBuf->startIndex, pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
1528

1529
  pBuf->endIndex = pBuf->matchIndex + 1;
377✔
1530

1531
  // reset repl mgr
1532
  for (int i = 0; i < pNode->totalReplicaNum; i++) {
1,038✔
1533
    SSyncLogReplMgr* pMgr = pNode->logReplMgrs[i];
661✔
1534
    syncLogReplReset(pMgr);
661✔
1535
  }
1536
  (void)taosThreadMutexUnlock(&pBuf->mutex);
377✔
1537
  TAOS_CHECK_RETURN(syncLogBufferValidate(pBuf));
377✔
1538
  return 0;
377✔
1539
}
1540

1541
int32_t syncLogBufferGetOneEntry(SSyncLogBuffer* pBuf, SSyncNode* pNode, SyncIndex index, bool* pInBuf,
384,472✔
1542
                                 SSyncRaftEntry** ppEntry) {
1543
  int32_t code = 0;
384,472✔
1544

1545
  *ppEntry = NULL;
384,472✔
1546

1547
  if (index >= pBuf->endIndex) {
384,472✔
1548
    return TSDB_CODE_OUT_OF_RANGE;
131,513✔
1549
  }
1550

1551
  if (index > pBuf->startIndex) {  // startIndex might be dummy
252,959✔
1552
    *pInBuf = true;
252,827✔
1553
    *ppEntry = pBuf->entries[index % pBuf->size].pItem;
252,827✔
1554
  } else {
1555
    *pInBuf = false;
132✔
1556

1557
    if ((code = pNode->pLogStore->syncLogGetEntry(pNode->pLogStore, index, ppEntry)) < 0) {
132✔
1558
      sWarn("vgId:%d, failed to get log entry since %s, index:%" PRId64, pNode->vgId, tstrerror(code), index);
3✔
1559
    }
1560
  }
1561
  TAOS_RETURN(code);
252,966✔
1562
}
1563

1564
int32_t syncLogReplSendTo(SSyncLogReplMgr* pMgr, SSyncNode* pNode, SyncIndex index, SyncTerm* pTerm, SRaftId* pDestId,
60,105✔
1565
                          bool* pBarrier) {
1566
  SSyncRaftEntry* pEntry = NULL;
60,105✔
1567
  SRpcMsg         msgOut = {0};
60,105✔
1568
  bool            inBuf = false;
60,105✔
1569
  SyncTerm        prevLogTerm = -1;
60,105✔
1570
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
60,105✔
1571
  int32_t         code = 0;
60,105✔
1572
  int32_t         lino = 0;
60,105✔
1573

1574
  code = syncLogBufferGetOneEntry(pBuf, pNode, index, &inBuf, &pEntry);
60,105✔
1575
  if (pEntry == NULL) {
60,105✔
1576
    sWarn("vgId:%d, failed to get raft entry for index:%" PRId64, pNode->vgId, index);
×
1577
    if (code == TSDB_CODE_WAL_LOG_NOT_EXIST) {
×
1578
      SSyncLogReplMgr* pMgr = syncNodeGetLogReplMgr(pNode, pDestId);
×
1579
      if (pMgr) {
×
1580
        sInfo("vgId:%d, reset sync log repl of peer addr:0x%" PRIx64 " since %s, index:%" PRId64, pNode->vgId, pDestId->addr,
×
1581
              tstrerror(code), index);
1582
        syncLogReplReset(pMgr);
×
1583
      }
1584
    }
1585
    goto _err;
×
1586
  }
1587
  *pBarrier = syncLogReplBarrier(pEntry);
60,105✔
1588

1589
  code = syncLogReplGetPrevLogTerm(pMgr, pNode, index, &prevLogTerm);
60,105✔
1590
  if (prevLogTerm < 0) {
60,105✔
1591
    sError("vgId:%d, failed to get prev log term since %s, index:%" PRId64, pNode->vgId, tstrerror(code), index);
×
1592
    goto _err;
×
1593
  }
1594
  if (pTerm) *pTerm = pEntry->term;
60,105✔
1595

1596
  code = syncBuildAppendEntriesFromRaftEntry(pNode, pEntry, prevLogTerm, &msgOut);
60,105✔
1597
  if (code < 0) {
60,108✔
1598
    sError("vgId:%d, failed to get append entries for index:%" PRId64, pNode->vgId, index);
×
1599
    goto _err;
×
1600
  }
1601

1602
  TRACE_SET_MSGID(&(msgOut.info.traceId), tGenIdPI64());
60,108✔
1603
  sGDebug(&msgOut.info.traceId,
60,109✔
1604
          "vgId:%d, index:%" PRId64 ", replicate one msg to dest addr:0x%" PRIx64 ", term:%" PRId64 " prevterm:%" PRId64,
1605
          pNode->vgId, pEntry->index, pDestId->addr, pEntry->term, prevLogTerm);
1606
  TAOS_CHECK_GOTO(syncNodeSendAppendEntries(pNode, pDestId, &msgOut), &lino, _err);
60,109✔
1607

1608
  if (!inBuf) {
60,109✔
1609
    syncEntryDestroy(pEntry);
72✔
1610
    pEntry = NULL;
72✔
1611
  }
1612
  return 0;
60,109✔
1613

1614
_err:
×
1615
  rpcFreeCont(msgOut.pCont);
×
1616
  msgOut.pCont = NULL;
×
1617
  if (!inBuf) {
×
1618
    syncEntryDestroy(pEntry);
×
1619
    pEntry = NULL;
×
1620
  }
1621
  TAOS_RETURN(code);
×
1622
}
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