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

taosdata / TDengine / #4794

15 Oct 2025 11:06AM UTC coverage: 60.905% (-0.001%) from 60.906%
#4794

push

travis-ci

web-flow
Merge c54016657 into 7e74ade39

154617 of 324341 branches covered (47.67%)

Branch coverage included in aggregate %.

54 of 65 new or added lines in 13 files covered. (83.08%)

2506 existing lines in 114 files now uncovered.

207077 of 269525 relevant lines covered (76.83%)

125143530.79 hits per line

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

49.39
/source/libs/sync/src/syncIndexMgr.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 "syncIndexMgr.h"
18
#include "syncUtil.h"
19

20
SSyncIndexMgr *syncIndexMgrCreate(SSyncNode *pNode) {
9,327,820✔
21
  SSyncIndexMgr *pIndexMgr = taosMemoryCalloc(1, sizeof(SSyncIndexMgr));
9,327,820!
22
  if (pIndexMgr == NULL) {
9,329,899!
23
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
24
    return NULL;
×
25
  }
26

27
  pIndexMgr->replicas = &pNode->replicasId;
9,329,899✔
28
  pIndexMgr->replicaNum = pNode->replicaNum;
9,330,408✔
29
  pIndexMgr->totalReplicaNum = pNode->totalReplicaNum;
9,331,031✔
30
  pIndexMgr->pNode = pNode;
9,329,548✔
31
  syncIndexMgrClear(pIndexMgr);
9,329,071✔
32

33
  return pIndexMgr;
9,330,455✔
34
}
35

36
void syncIndexMgrUpdate(SSyncIndexMgr *pIndexMgr, SSyncNode *pNode) {
144,032✔
37
  pIndexMgr->replicas = &pNode->replicasId;
144,032✔
38
  pIndexMgr->replicaNum = pNode->replicaNum;
144,032✔
39
  pIndexMgr->totalReplicaNum = pNode->totalReplicaNum;
144,032✔
40
  pIndexMgr->pNode = pNode;
144,032✔
41
  syncIndexMgrClear(pIndexMgr);
144,032✔
42
}
144,032✔
43

44
void syncIndexMgrDestroy(SSyncIndexMgr *pIndexMgr) {
9,293,913✔
45
  if (pIndexMgr != NULL) {
9,293,913!
46
    taosMemoryFree(pIndexMgr);
9,293,913!
47
  }
48
}
9,294,274✔
49

50
void syncIndexMgrClear(SSyncIndexMgr *pIndexMgr) {
9,474,925✔
51
  memset(pIndexMgr->index, 0, sizeof(pIndexMgr->index));
9,474,925!
52
  memset(pIndexMgr->privateTerm, 0, sizeof(pIndexMgr->privateTerm));
9,475,254!
53

54
  int64_t timeNow = taosGetTimestampMs();
9,476,376✔
55
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
25,226,341✔
56
    pIndexMgr->startTimeArr[i] = 0;
15,750,241✔
57
    pIndexMgr->recvHBTimeArr[i] = timeNow;
15,749,495✔
58
    pIndexMgr->recvHBCountArr[i] = 0;
15,748,363✔
59
  }
60
}
9,474,487✔
61

62
void syncIndexMgrSetIndex(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId, SyncIndex index) {
695,753,227✔
63
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
775,293,750!
64
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
775,333,664✔
65
      (pIndexMgr->index)[i] = index;
695,747,769✔
66
      return;
695,772,934✔
67
    }
68
  }
69

UNCOV
70
  sError("vgId:%d, indexmgr set index:%" PRId64 " for dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, index,
×
71
         DID(pRaftId), CID(pRaftId));
72
}
73

74
void syncIndexMgrCopyIfExist(SSyncIndexMgr * pNewIndex, SSyncIndexMgr * pOldIndex, SRaftId *oldReplicasId){
×
75
  for(int j = 0; j < pOldIndex->totalReplicaNum; ++j){
×
76
    sDebug("old Index j:%d, index:%"PRId64, j, pOldIndex->index[j]);
×
77
  }
78
  
79
  for (int i = 0; i < pNewIndex->totalReplicaNum; ++i) {
×
80
    for(int j = 0; j < pOldIndex->totalReplicaNum; ++j){
×
81
      if (syncUtilSameId(/*(const SRaftId*)*/&((oldReplicasId[j])), &((*(pNewIndex->replicas))[i]))) {
×
82
        pNewIndex->index[i] = pOldIndex->index[j];
×
83
        pNewIndex->privateTerm[i] = pOldIndex->privateTerm[j];
×
84
        pNewIndex->startTimeArr[i] = pOldIndex->startTimeArr[j];
×
85
        pNewIndex->recvHBTimeArr[i] = pOldIndex->recvHBTimeArr[j];
×
86
        pNewIndex->recvHBCountArr[i] = pOldIndex->recvHBCountArr[j];   
×
87
      }
88
    }
89
  }
90

91
  for (int i = 0; i < pNewIndex->totalReplicaNum; ++i){
×
92
    sDebug("new index i:%d, index:%"PRId64, i, pNewIndex->index[i]);
×
93
  }
94
}
×
95

96
SSyncLogReplMgr *syncNodeGetLogReplMgr(SSyncNode *pNode, SRaftId *pRaftId) {
83,728,753✔
97
  for (int i = 0; i < pNode->totalReplicaNum; i++) {
185,723,682✔
98
    if (syncUtilSameId(&pNode->replicasId[i], pRaftId)) {
185,724,106✔
99
      return pNode->logReplMgrs[i];
83,728,964✔
100
    }
101
  }
102

103
  terrno = TSDB_CODE_SYN_INVALID_ID;
1,004✔
104
  sError("vgId:%d, indexmgr get replmgr from dnode:%d cluster:%d failed", pNode->vgId, DID(pRaftId), CID(pRaftId));
1,004!
105
  return NULL;
1,004✔
106
}
107

108
SyncIndex syncIndexMgrGetIndex(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) {
74,141,121✔
109
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
160,771,089!
110
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
160,771,089✔
111
      SyncIndex idx = (pIndexMgr->index)[i];
74,141,121✔
112
      return idx;
74,141,121✔
113
    }
114
  }
115

116
  terrno = TSDB_CODE_SYN_INVALID_ID;
×
117
  sError("vgId:%d, indexmgr get index from dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, DID(pRaftId),
×
118
         CID(pRaftId));
119
  return SYNC_INDEX_INVALID;
×
120
}
121

122
void syncIndexMgrSetStartTime(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId, int64_t startTime) {
×
123
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
×
124
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
×
125
      (pIndexMgr->startTimeArr)[i] = startTime;
×
126
      return;
×
127
    }
128
  }
129

130
  sError("vgId:%d, indexmgr set start-time:%" PRId64 " for dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId,
×
131
         startTime, DID(pRaftId), CID(pRaftId));
132
}
133

134
int64_t syncIndexMgrGetStartTime(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) {
×
135
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
×
136
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
×
137
      int64_t startTime = (pIndexMgr->startTimeArr)[i];
×
138
      return startTime;
×
139
    }
140
  }
141

142
  sError("vgId:%d, indexmgr get start-time from dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, DID(pRaftId),
×
143
         CID(pRaftId));
144
  return TSDB_CODE_SYN_INVALID_ID;
×
145
  ;
146
}
147

148
void syncIndexMgrSetRecvTime(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId, int64_t recvTime) {
38,743,547✔
149
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
75,375,697!
150
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
75,375,902✔
151
      (pIndexMgr->recvHBTimeArr)[i] = recvTime;
38,742,522✔
152
      return;
38,742,727✔
153
    }
154
  }
155

156
  sError("vgId:%d, indexmgr set recv-time:%" PRId64 " for dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, recvTime,
×
157
         DID(pRaftId), CID(pRaftId));
158
}
159

160
void syncIndexMgrIncRecvCount(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) {
38,743,957✔
161
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
75,376,517!
162
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
75,374,901✔
163
      (pIndexMgr->recvHBCountArr)[i] = (pIndexMgr->recvHBCountArr)[i] + 1;
38,743,547✔
164
      return;
38,743,547✔
165
    }
166
  }
167

168
  sError("vgId:%d, indexmgr inc recv for dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, DID(pRaftId), CID(pRaftId));
×
169
}
170

171
void syncIndexMgrSetSentTime(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId, int64_t sentTime) {
21,664,553✔
172
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
45,810,571!
173
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
45,810,571✔
174
      (pIndexMgr->sentHBTimeArr)[i] = sentTime;
21,664,553✔
175
      return;
21,664,553✔
176
    }
177
  }
178

179
  sError("vgId:%d, indexmgr set sent-time:%" PRId64 " for dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, sentTime,
×
180
         DID(pRaftId), CID(pRaftId));
181
}
182

183
int64_t syncIndexMgrGetRecvTime(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) {
146,489,103✔
184
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
278,874,982!
185
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
278,874,228✔
186
      int64_t recvTime = (pIndexMgr->recvHBTimeArr)[i];
146,486,867✔
187
      return recvTime;
146,487,137✔
188
    }
189
  }
190

191
  sError("vgId:%d, indexmgr get recv-time from dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, DID(pRaftId),
×
192
         CID(pRaftId));
193
  return TSDB_CODE_SYN_INVALID_ID;
×
194
}
195

196
int64_t syncIndexMgrGetRecvCount(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) {
123,672,363✔
197
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
224,852,205!
198
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
224,853,115✔
199
      int64_t count = (pIndexMgr->recvHBCountArr)[i];
123,669,714✔
200
      return count;
123,672,109✔
201
    }
202
  }
203

204
  sError("vgId:%d, indexmgr get recv-time from dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, DID(pRaftId),
×
205
         CID(pRaftId));
206
  return TSDB_CODE_SYN_INVALID_ID;
×
207
}
208

209
int64_t syncIndexMgrGetSentTime(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) {
41,226,526✔
210
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
74,953,140!
211
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
74,953,045✔
212
      int64_t recvTime = (pIndexMgr->sentHBTimeArr)[i];
41,226,253✔
213
      return recvTime;
41,226,766✔
214
    }
215
  }
216

217
  sError("vgId:%d, indexmgr get sent-time from dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, DID(pRaftId),
×
218
         CID(pRaftId));
219
  return TSDB_CODE_SYN_INVALID_ID;
×
220
}
221

222
void syncIndexMgrSetTerm(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId, SyncTerm term) {
×
223
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
×
224
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
×
225
      (pIndexMgr->privateTerm)[i] = term;
×
226
      return;
×
227
    }
228
  }
229

230
  sError("vgId:%d, indexmgr set term:%" PRId64 " for dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, term,
×
231
         DID(pRaftId), CID(pRaftId));
232
}
233

234
SyncTerm syncIndexMgrGetTerm(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) {
×
235
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
×
236
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
×
237
      SyncTerm term = (pIndexMgr->privateTerm)[i];
×
238
      return term;
×
239
    }
240
  }
241

242
  sError("vgId:%d, indexmgr get term from dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, DID(pRaftId),
×
243
         CID(pRaftId));
244
  return TSDB_CODE_SYN_INVALID_ID;
×
245
}
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