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

taosdata / TDengine / #4696

29 Aug 2025 06:36AM UTC coverage: 58.25% (+0.2%) from 58.041%
#4696

push

travis-ci

web-flow
fix(gpt): fix race-condition in preparing tmp files (#32800)

133424 of 291873 branches covered (45.71%)

Branch coverage included in aggregate %.

5 of 34 new or added lines in 6 files covered. (14.71%)

444 existing lines in 69 files now uncovered.

201767 of 283561 relevant lines covered (71.15%)

17907122.76 hits per line

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

50.63
/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) {
29,912✔
21
  SSyncIndexMgr *pIndexMgr = taosMemoryCalloc(1, sizeof(SSyncIndexMgr));
29,912!
22
  if (pIndexMgr == NULL) {
29,914!
23
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
24
    return NULL;
×
25
  }
26

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

33
  return pIndexMgr;
29,914✔
34
}
35

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

44
void syncIndexMgrDestroy(SSyncIndexMgr *pIndexMgr) {
29,912✔
45
  if (pIndexMgr != NULL) {
29,912✔
46
    taosMemoryFree(pIndexMgr);
29,911!
47
  }
48
}
29,909✔
49

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

54
  int64_t timeNow = taosGetTimestampMs();
30,477✔
55
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
77,811✔
56
    pIndexMgr->startTimeArr[i] = 0;
47,334✔
57
    pIndexMgr->recvHBTimeArr[i] = timeNow;
47,334✔
58
    pIndexMgr->recvHBCountArr[i] = 0;
47,334✔
59
  }
60
}
30,477✔
61

62
void syncIndexMgrSetIndex(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId, SyncIndex index) {
15,514,831✔
63
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
20,836,154!
64
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
20,836,357✔
65
      (pIndexMgr->index)[i] = index;
15,515,096✔
66
      return;
15,515,096✔
67
    }
68
  }
69

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) {
5,588,135✔
97
  for (int i = 0; i < pNode->totalReplicaNum; i++) {
11,291,344✔
98
    if (syncUtilSameId(&pNode->replicasId[i], pRaftId)) {
11,291,343✔
99
      return pNode->logReplMgrs[i];
5,588,128✔
100
    }
101
  }
102

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

108
SyncIndex syncIndexMgrGetIndex(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) {
2,726,646✔
109
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
5,511,947✔
110
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
5,511,946✔
111
      SyncIndex idx = (pIndexMgr->index)[i];
2,726,644✔
112
      return idx;
2,726,644✔
113
    }
114
  }
115

116
  terrno = TSDB_CODE_SYN_INVALID_ID;
1✔
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) {
122,753✔
149
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
236,920!
150
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
236,920✔
151
      (pIndexMgr->recvHBTimeArr)[i] = recvTime;
122,753✔
152
      return;
122,753✔
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) {
122,753✔
161
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
236,920!
162
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
236,920✔
163
      (pIndexMgr->recvHBCountArr)[i] = (pIndexMgr->recvHBCountArr)[i] + 1;
122,753✔
164
      return;
122,753✔
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) {
65,460✔
172
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
139,383!
173
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
139,383✔
174
      (pIndexMgr->sentHBTimeArr)[i] = sentTime;
65,460✔
175
      return;
65,460✔
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) {
584,704✔
184
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
1,108,481!
185
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
1,108,496✔
186
      int64_t recvTime = (pIndexMgr->recvHBTimeArr)[i];
584,709✔
187
      return recvTime;
584,709✔
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) {
373,185✔
197
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
642,229✔
198
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
642,217✔
199
      int64_t count = (pIndexMgr->recvHBCountArr)[i];
373,179✔
200
      return count;
373,179✔
201
    }
202
  }
203

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

209
int64_t syncIndexMgrGetSentTime(SSyncIndexMgr *pIndexMgr, const SRaftId *pRaftId) {
124,398✔
210
  for (int i = 0; i < pIndexMgr->totalReplicaNum; ++i) {
214,089!
211
    if (syncUtilSameId(&((*(pIndexMgr->replicas))[i]), pRaftId)) {
214,093✔
212
      int64_t recvTime = (pIndexMgr->sentHBTimeArr)[i];
124,405✔
213
      return recvTime;
124,405✔
214
    }
215
  }
216

UNCOV
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