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

taosdata / TDengine / #5047

07 May 2026 07:27AM UTC coverage: 73.152% (+0.008%) from 73.144%
#5047

push

travis-ci

web-flow
feat(parser): add support for QUARTER duration alias and related tests (#35268)

6 of 6 new or added lines in 2 files covered. (100.0%)

597 existing lines in 127 files now uncovered.

277691 of 379607 relevant lines covered (73.15%)

132499712.47 hits per line

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

63.41
/source/libs/sync/src/syncAppendEntriesReply.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "syncAppendEntriesReply.h"
18
#include "syncCommit.h"
19
#include "syncIndexMgr.h"
20
#include "syncPipeline.h"
21
#include "syncMessage.h"
22
#include "syncRaftEntry.h"
23
#include "syncRaftStore.h"
24
#include "syncReplication.h"
25
#include "syncSnapshot.h"
26
#include "syncUtil.h"
27

28
// TLA+ Spec
29
// HandleAppendEntriesResponse(i, j, m) ==
30
//    /\ m.mterm = currentTerm[i]
31
//    /\ \/ /\ m.msuccess \* successful
32
//          /\ nextIndex'  = [nextIndex  EXCEPT ![i][j] = m.mmatchIndex + 1]
33
//          /\ matchIndex' = [matchIndex EXCEPT ![i][j] = m.mmatchIndex]
34
//       \/ /\ \lnot m.msuccess \* not successful
35
//          /\ nextIndex' = [nextIndex EXCEPT ![i][j] =
36
//                               Max({nextIndex[i][j] - 1, 1})]
37
//          /\ UNCHANGED <<matchIndex>>
38
//    /\ Discard(m)
39
//    /\ UNCHANGED <<serverVars, candidateVars, logVars, elections>>
40
//
41

42
int32_t syncNodeOnAppendEntriesReply(SSyncNode* ths, const SRpcMsg* pRpcMsg) {
112,490,312✔
43
  int32_t                 code = 0;
112,490,312✔
44
  SyncAppendEntriesReply* pMsg = (SyncAppendEntriesReply*)pRpcMsg->pCont;
112,490,312✔
45
  int32_t                 ret = 0;
112,490,312✔
46

47
  // if already drop replica, do not process
48
  if (!syncNodeInRaftGroup(ths, &(pMsg->srcId))) {
112,490,312✔
49
    syncLogRecvAppendEntriesReply(ths, pMsg, "not in my config", &pRpcMsg->info.traceId);
×
50
    return 0;
×
51
  }
52

53
  // drop stale response
54
  if (pMsg->term < raftStoreGetTerm(ths)) {
112,489,994✔
UNCOV
55
    syncLogRecvAppendEntriesReply(ths, pMsg, "drop stale response", &pRpcMsg->info.traceId);
×
UNCOV
56
    return 0;
×
57
  }
58

59
  if (ths->state == TAOS_SYNC_STATE_LEADER || ths->state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
112,490,312✔
60
    if (pMsg->term != raftStoreGetTerm(ths)) {
112,489,522✔
61
      syncLogRecvAppendEntriesReply(ths, pMsg, "error term", &pRpcMsg->info.traceId);
×
62
      syncNodeStepDown(ths, pMsg->term, pMsg->srcId, "appendEntryReply");
×
63
      return TSDB_CODE_SYN_WRONG_TERM;
×
64
    }
65

66
    sGDebug(&pRpcMsg->info.traceId, "vgId:%d, received append entries reply, src addr:0x%" PRIx64 ", term:%" PRId64 ", matchIndex:%" PRId64,
112,489,522✔
67
            pMsg->vgId, pMsg->srcId.addr, pMsg->term, pMsg->matchIndex);
68

69
    if (pMsg->success) {
112,489,522✔
70
      SyncIndex oldMatchIndex = syncIndexMgrGetIndex(ths->pMatchIndex, &(pMsg->srcId));
107,320,804✔
71
      if (pMsg->matchIndex > oldMatchIndex) {
107,320,804✔
72
        syncIndexMgrSetIndex(ths->pMatchIndex, &(pMsg->srcId), pMsg->matchIndex);
101,361,301✔
73
      }
74

75
      // commit if needed
76
      SyncIndex indexLikely = TMIN(pMsg->matchIndex, ths->pLogBuf->matchIndex);
107,320,804✔
77
      SyncIndex commitIndex = syncNodeCheckCommitIndex(ths, indexLikely, &pRpcMsg->info.traceId);
107,320,804✔
78
      if (ths->state == TAOS_SYNC_STATE_ASSIGNED_LEADER) {
107,320,486✔
79
        if (commitIndex >= ths->assignedCommitIndex) {
19✔
80
          // Backward compatibility: old version messages don't include appliedIndex field.
81
          // If message is from old version, default to commitIndex so gap=0, preserving old step-down behavior.
82
          SyncIndex peerAppliedIndex = commitIndex;
×
83
          if (pRpcMsg->contLen >= (int32_t)sizeof(SyncAppendEntriesReply)) {
×
84
            peerAppliedIndex = pMsg->appliedIndex;
×
85
          }
86
          SyncIndex appliedGap = commitIndex - peerAppliedIndex;
×
87
          if (tsSyncAssignedCheckAppliedGap == 0 || appliedGap <= tsSyncAssignedCheckAppliedGap) {
×
88
            sInfo("vgId:%d, going to step down from assigned leader by append entries reply, commitIndex:%" PRId64
×
89
                  ", assignedCommitIndex:%" PRId64 ", peerAppliedIndex:%" PRId64 ", appliedGap:%" PRId64,
90
                  ths->vgId, commitIndex, ths->assignedCommitIndex, peerAppliedIndex, appliedGap);
91
            syncNodeStepDown(ths, pMsg->term, pMsg->destId, "appendEntryReply");
×
92
          } else {
93
            sDebug("vgId:%d, delay step down from assigned leader, commitIndex:%" PRId64
×
94
                   ", assignedCommitIndex:%" PRId64 ", peerAppliedIndex:%" PRId64 ", appliedGap:%" PRId64
95
                   ", threshold:%" PRId64,
96
                   ths->vgId, commitIndex, ths->assignedCommitIndex, peerAppliedIndex, appliedGap,
97
                   tsSyncAssignedCheckAppliedGap);
98
          }
99
        }
100
      } else {
101
        TAOS_CHECK_RETURN(syncLogBufferCommit(ths->pLogBuf, ths, commitIndex, &pRpcMsg->info.traceId, "sync-append-entries-reply"));
107,320,467✔
102
      }
103
    }
104

105
    // replicate log
106
    SSyncLogReplMgr* pMgr = syncNodeGetLogReplMgr(ths, &pMsg->srcId);
112,489,097✔
107
    if (pMgr == NULL) {
112,489,522✔
108
      code = TSDB_CODE_MND_RETURN_VALUE_NULL;
508✔
109
      if (terrno != 0) code = terrno;
508✔
110
      sError("vgId:%d, failed to get log repl mgr for src addr:0x%" PRIx64, ths->vgId, pMsg->srcId.addr);
508✔
111
      TAOS_RETURN(code);
508✔
112
    }
113
    TAOS_CHECK_RETURN(syncLogReplProcessReply(pMgr, ths, pMsg));
112,489,014✔
114
  }
115
  TAOS_RETURN(code);
112,489,696✔
116
}
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