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

taosdata / TDengine / #5052

13 May 2026 12:00PM UTC coverage: 73.338% (-0.02%) from 73.358%
#5052

push

travis-ci

web-flow
feat: taosdump support stream backup/restore (#35326)

139 of 170 new or added lines in 3 files covered. (81.76%)

761 existing lines in 163 files now uncovered.

281469 of 383795 relevant lines covered (73.34%)

134502812.98 hits per line

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

58.02
/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) {
11,106,520✔
21
  SSyncIndexMgr *pIndexMgr = taosMemoryCalloc(1, sizeof(SSyncIndexMgr));
11,106,520✔
22
  if (pIndexMgr == NULL) {
11,106,994✔
23
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
24
    return NULL;
×
25
  }
26

27
  pIndexMgr->replicas = &pNode->replicasId;
11,106,994✔
28
  pIndexMgr->replicaNum = pNode->replicaNum;
11,106,994✔
29
  pIndexMgr->totalReplicaNum = pNode->totalReplicaNum;
11,106,994✔
30
  pIndexMgr->pNode = pNode;
11,106,994✔
31
  syncIndexMgrClear(pIndexMgr);
11,106,994✔
32

33
  return pIndexMgr;
11,107,252✔
34
}
35

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

44
void syncIndexMgrDestroy(SSyncIndexMgr *pIndexMgr) {
11,080,964✔
45
  if (pIndexMgr != NULL) {
11,080,964✔
46
    taosMemoryFree(pIndexMgr);
11,080,964✔
47
  }
48
}
11,081,366✔
49

50
void syncIndexMgrClear(SSyncIndexMgr *pIndexMgr) {
11,226,518✔
51
  memset(pIndexMgr->index, 0, sizeof(pIndexMgr->index));
11,226,518✔
52
  memset(pIndexMgr->privateTerm, 0, sizeof(pIndexMgr->privateTerm));
11,226,518✔
53

54
  int64_t timeNow = taosGetTimestampMs();
11,227,266✔
55
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
29,529,268✔
56
    pIndexMgr->startTimeArr[i] = 0;
18,302,796✔
57
    pIndexMgr->recvHBTimeArr[i] = timeNow;
18,302,796✔
58
    pIndexMgr->recvHBCountArr[i] = 0;
18,303,076✔
59
  }
60
}
11,226,776✔
61

62
void syncIndexMgrSetIndex(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId, SyncIndex index) {
1,717,023,519✔
63
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
2,147,483,647✔
64
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
2,147,483,647✔
65
      (pIndexMgr->index)[i] = index;
1,716,964,526✔
66
      return;
1,716,959,829✔
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) {
1,059,576,712✔
97
  for (int i = 0; i < pNode->totalReplicaNum; i++) {
2,147,483,647✔
98
    if (syncUtilSameId(&pNode->replicasId[i], pRaftId)) {
2,147,483,647✔
99
      return pNode->logReplMgrs[i];
1,059,576,987✔
100
    }
101
  }
102

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

108
SyncIndex syncIndexMgrGetIndex(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) {
432,057,357✔
109
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
1,010,427,541✔
110
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
1,010,427,541✔
111
      SyncIndex idx = (pIndexMgr->index)[i];
432,057,357✔
112
      return idx;
432,057,357✔
113
    }
114
  }
115

UNCOV
116
  terrno = TSDB_CODE_SYN_INVALID_ID;
×
UNCOV
117
  sError("vgId:%d, indexmgr get index from dnode:%d cluster:%d failed", pIndexMgr->pNode->vgId, DID(pRaftId),
×
118
         CID(pRaftId));
UNCOV
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) {
59,187,773✔
149
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
115,210,729✔
150
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
115,210,347✔
151
      (pIndexMgr->recvHBTimeArr)[i] = recvTime;
59,187,773✔
152
      return;
59,187,773✔
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) {
59,187,773✔
161
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
115,210,729✔
162
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
115,210,729✔
163
      (pIndexMgr->recvHBCountArr)[i] = (pIndexMgr->recvHBCountArr)[i] + 1;
59,187,773✔
164
      return;
59,187,773✔
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) {
31,707,882✔
172
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
66,936,746✔
173
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
66,936,746✔
174
      (pIndexMgr->sentHBTimeArr)[i] = sentTime;
31,707,882✔
175
      return;
31,707,882✔
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) {
483,679,223✔
184
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
1,056,771,337✔
185
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
1,056,768,073✔
186
      int64_t recvTime = (pIndexMgr->recvHBTimeArr)[i];
483,677,870✔
187
      return recvTime;
483,679,632✔
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) {
158,799,389✔
197
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
288,423,698✔
198
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
288,415,948✔
199
      int64_t count = (pIndexMgr->recvHBCountArr)[i];
158,796,998✔
200
      return count;
158,798,746✔
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) {
52,935,216✔
210
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
96,145,850✔
211
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
96,146,230✔
212
      int64_t recvTime = (pIndexMgr->sentHBTimeArr)[i];
52,932,824✔
213
      return recvTime;
52,932,744✔
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