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

taosdata / TDengine / #3647

13 Mar 2025 05:26AM UTC coverage: 25.9% (-2.5%) from 28.375%
#3647

push

travis-ci

web-flow
Merge pull request #30158 from taosdata/docs/anchor-caps-30

docs: lowercase anchors for 3.0

53974 of 285572 branches covered (18.9%)

Branch coverage included in aggregate %.

92870 of 281392 relevant lines covered (33.0%)

617448.64 hits per line

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

30.88
/source/libs/sync/src/syncRaftStore.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 "syncRaftStore.h"
18
#include "syncUtil.h"
19
#include "tjson.h"
20

21
int32_t raftStoreReadFile(SSyncNode *pNode);
22
int32_t raftStoreWriteFile(SSyncNode *pNode);
23

24
static int32_t raftStoreDecode(const SJson *pJson, SRaftStore *pStore) {
×
25
  int32_t code = 0;
×
26

27
  tjsonGetNumberValue(pJson, "current_term", pStore->currentTerm, code);
×
28
  if (code < 0) TAOS_RETURN(TSDB_CODE_FAILED);
×
29
  tjsonGetNumberValue(pJson, "vote_for_addr", pStore->voteFor.addr, code);
×
30
  if (code < 0) TAOS_RETURN(TSDB_CODE_FAILED);
×
31
  tjsonGetInt32ValueFromDouble(pJson, "vote_for_vgid", pStore->voteFor.vgId, code);
×
32
  if (code < 0) TAOS_RETURN(TSDB_CODE_FAILED);
×
33

34
  TAOS_RETURN(TSDB_CODE_SUCCESS);
×
35
}
36

37
int32_t raftStoreReadFile(SSyncNode *pNode) {
28✔
38
  int32_t     code = -1, lino = 0;
28✔
39
  TdFilePtr   pFile = NULL;
28✔
40
  char       *pData = NULL;
28✔
41
  SJson      *pJson = NULL;
28✔
42
  const char *file = pNode->raftStorePath;
28✔
43
  SRaftStore *pStore = &pNode->raftStore;
28✔
44

45
  if (taosStatFile(file, NULL, NULL, NULL) < 0) {
28!
46
    sInfo("vgId:%d, raft store file:%s not exist, use default value", pNode->vgId, file);
28!
47
    pStore->currentTerm = 0;
28✔
48
    pStore->voteFor.addr = 0;
28✔
49
    pStore->voteFor.vgId = 0;
28✔
50
    return raftStoreWriteFile(pNode);
28✔
51
  }
52

53
  pFile = taosOpenFile(file, TD_FILE_READ);
×
54
  if (pFile == NULL) {
×
55
    sError("vgId:%d, failed to open raft store file:%s since %s", pNode->vgId, file, terrstr());
×
56

57
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
58
  }
59

60
  int64_t size = 0;
×
61
  code = taosFStatFile(pFile, &size, NULL);
×
62
  if (code != 0) {
×
63
    sError("vgId:%d, failed to fstat raft store file:%s since %s", pNode->vgId, file, terrstr());
×
64
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
65
  }
66

67
  pData = taosMemoryMalloc(size + 1);
×
68
  if (pData == NULL) {
×
69
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
70
  }
71

72
  if (taosReadFile(pFile, pData, size) != size) {
×
73
    sError("vgId:%d, failed to read raft store file:%s since %s", pNode->vgId, file, terrstr());
×
74

75
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
76
  }
77

78
  pData[size] = '\0';
×
79

80
  pJson = tjsonParse(pData);
×
81
  if (pJson == NULL) {
×
82
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_JSON_FORMAT, &lino, _OVER);
×
83
  }
84

85
  if (raftStoreDecode(pJson, pStore) < 0) {
×
86
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_JSON_FORMAT, &lino, _OVER);
×
87
  }
88

89
  code = 0;
×
90
  sInfo("vgId:%d, succceed to read raft store file %s", pNode->vgId, file);
×
91

92
_OVER:
×
93
  if (pData != NULL) taosMemoryFree(pData);
×
94
  if (pJson != NULL) cJSON_Delete(pJson);
×
95
  if (pFile != NULL) taosCloseFile(&pFile);
×
96

97
  if (code != 0) {
×
98
    sError("vgId:%d, failed to read raft store file:%s since %s", pNode->vgId, file, terrstr());
×
99
  }
100

101
  TAOS_RETURN(code);
×
102
}
103

104
static int32_t raftStoreEncode(SJson *pJson, SRaftStore *pStore) {
56✔
105
  if (tjsonAddIntegerToObject(pJson, "current_term", pStore->currentTerm) < 0) TAOS_RETURN(TSDB_CODE_FAILED);
56!
106
  if (tjsonAddIntegerToObject(pJson, "vote_for_addr", pStore->voteFor.addr) < 0) TAOS_RETURN(TSDB_CODE_FAILED);
56!
107
  if (tjsonAddDoubleToObject(pJson, "vote_for_vgid", pStore->voteFor.vgId) < 0) TAOS_RETURN(TSDB_CODE_FAILED);
56!
108

109
  TAOS_RETURN(TSDB_CODE_SUCCESS);
56✔
110
}
111

112
int32_t raftStoreWriteFile(SSyncNode *pNode) {
56✔
113
  int32_t     code = -1, lino = 0;
56✔
114
  char       *buffer = NULL;
56✔
115
  SJson      *pJson = NULL;
56✔
116
  TdFilePtr   pFile = NULL;
56✔
117
  const char *realfile = pNode->raftStorePath;
56✔
118
  SRaftStore *pStore = &pNode->raftStore;
56✔
119
  char        file[PATH_MAX] = {0};
56✔
120
  snprintf(file, sizeof(file), "%s.bak", realfile);
56✔
121

122
  pJson = tjsonCreateObject();
56✔
123
  if (pJson == NULL) TAOS_CHECK_GOTO(terrno, &lino, _OVER);
56!
124
  if (raftStoreEncode(pJson, pStore) != 0) TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
56!
125

126
  buffer = tjsonToString(pJson);
56✔
127
  if (buffer == NULL) TAOS_CHECK_GOTO(terrno, &lino, _OVER);
56!
128

129
  pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH);
56✔
130
  if (pFile == NULL) TAOS_CHECK_GOTO(terrno, &lino, _OVER);
56!
131

132
  int32_t len = strlen(buffer);
56✔
133
  if (taosWriteFile(pFile, buffer, len) <= 0) TAOS_CHECK_GOTO(terrno, &lino, _OVER);
56!
134

135
  if (taosFsyncFile(pFile) < 0) TAOS_CHECK_GOTO(terrno, &lino, _OVER);
56!
136

137
  TAOS_CHECK_GOTO(taosCloseFile(&pFile), &lino, _OVER);
56!
138
  if (taosRenameFile(file, realfile) != 0) TAOS_CHECK_GOTO(terrno, &lino, _OVER);
56!
139

140
  code = 0;
56✔
141
  sInfo("vgId:%d, succeed to write raft store file:%s, term:%" PRId64, pNode->vgId, realfile, pStore->currentTerm);
56!
142

143
_OVER:
56✔
144
  if (pJson != NULL) tjsonDelete(pJson);
56!
145
  if (buffer != NULL) taosMemoryFree(buffer);
56!
146
  if (pFile != NULL) taosCloseFile(&pFile);
56!
147

148
  if (code != 0) {
56!
149
    sError("vgId:%d, failed to write raft store file:%s since %s", pNode->vgId, realfile, terrstr());
×
150
  }
151
  return code;
56✔
152
}
153

154
int32_t raftStoreOpen(SSyncNode *pNode) {
28✔
155
  (void)taosThreadMutexInit(&pNode->raftStore.mutex, NULL);
28✔
156
  return raftStoreReadFile(pNode);
28✔
157
}
158

159
void raftStoreClose(SSyncNode *pNode) { (void)taosThreadMutexDestroy(&pNode->raftStore.mutex); }
28✔
160

161
bool raftStoreHasVoted(SSyncNode *pNode) {
×
162
  (void)taosThreadMutexLock(&pNode->raftStore.mutex);
×
163
  bool b = syncUtilEmptyId(&pNode->raftStore.voteFor);
×
164
  (void)taosThreadMutexUnlock(&pNode->raftStore.mutex);
×
165
  return (!b);
×
166
}
167

168
void raftStoreVote(SSyncNode *pNode, SRaftId *pRaftId) {
×
169
  (void)taosThreadMutexLock(&pNode->raftStore.mutex);
×
170
  pNode->raftStore.voteFor = *pRaftId;
×
171
  int32_t code = 0;
×
172
  if ((code = raftStoreWriteFile(pNode)) != 0) {
×
173
    sError("vgId:%d, failed to write raft store file since %s", pNode->vgId, tstrerror(code));
×
174
  }
175
  (void)taosThreadMutexUnlock(&pNode->raftStore.mutex);
×
176
}
×
177

178
void raftStoreClearVote(SSyncNode *pNode) {
×
179
  (void)taosThreadMutexLock(&pNode->raftStore.mutex);
×
180
  pNode->raftStore.voteFor = EMPTY_RAFT_ID;
×
181
  int32_t code = 0;
×
182
  if ((code = raftStoreWriteFile(pNode)) != 0) {
×
183
    sError("vgId:%d, failed to write raft store file since %s", pNode->vgId, tstrerror(code));
×
184
  }
185
  (void)taosThreadMutexUnlock(&pNode->raftStore.mutex);
×
186
}
×
187

188
void raftStoreNextTerm(SSyncNode *pNode) {
28✔
189
  (void)taosThreadMutexLock(&pNode->raftStore.mutex);
28✔
190
  pNode->raftStore.currentTerm++;
28✔
191
  int32_t code = 0;
28✔
192
  if ((code = raftStoreWriteFile(pNode)) != 0) {
28!
193
    sError("vgId:%d, failed to write raft store file since %s", pNode->vgId, tstrerror(code));
×
194
  }
195
  (void)taosThreadMutexUnlock(&pNode->raftStore.mutex);
28✔
196
}
28✔
197

198
void raftStoreSetTerm(SSyncNode *pNode, SyncTerm term) {
×
199
  (void)taosThreadMutexLock(&pNode->raftStore.mutex);
×
200
  if (pNode->raftStore.currentTerm < term) {
×
201
    pNode->raftStore.currentTerm = term;
×
202
    int32_t code = 0;
×
203
    if ((code = raftStoreWriteFile(pNode)) != 0) {
×
204
      sError("vgId:%d, failed to write raft store file since %s", pNode->vgId, tstrerror(code));
×
205
    }
206
  }
207
  (void)taosThreadMutexUnlock(&pNode->raftStore.mutex);
×
208
}
×
209

210
SyncTerm raftStoreGetTerm(SSyncNode *pNode) {
1,998✔
211
  (void)taosThreadMutexLock(&pNode->raftStore.mutex);
1,998✔
212
  SyncTerm term = pNode->raftStore.currentTerm;
1,998✔
213
  (void)taosThreadMutexUnlock(&pNode->raftStore.mutex);
1,998✔
214
  return term;
1,998✔
215
}
216

217
SyncTerm raftStoreTryGetTerm(SSyncNode *pNode) {
×
218
  SyncTerm term = 0;
×
219
  if (taosThreadMutexTryLock(&pNode->raftStore.mutex) == 0) {
×
220
    term = pNode->raftStore.currentTerm;
×
221
    (void)taosThreadMutexUnlock(&pNode->raftStore.mutex);
×
222
  }
223

224
  return term;
×
225
}
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