-
Notifications
You must be signed in to change notification settings - Fork 0
/
raft_storage.h
253 lines (194 loc) · 6.66 KB
/
raft_storage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#ifndef raft_storage_h
#define raft_storage_h
#include "raft_protocol.h"
#include <fcntl.h>
#include <fstream>
#include <iostream>
#include <mutex>
#include <sstream>
#include <string>
#include <vector>
template <typename command> class raft_storage {
public:
raft_storage(const std::string &file_dir);
~raft_storage();
bool updateMetadata(int term, int vote);
bool updateSnapshot(const std::vector<char> &snapshot);
bool updateLog(const std::vector<log_entry<command>> &log);
bool appendLog(const log_entry<command> &log, int new_size);
bool appendLog(const std::vector<log_entry<command>> &log, int new_size);
bool updateTotal(int term, int vote, const std::vector<log_entry<command>> &log, const std::vector<char> &snapshot);
bool restore(int &term, int &vote, std::vector<log_entry<command>> &log, std::vector<char> &snapshot);
private:
std::mutex mtx;
std::string m_metadata;
std::string m_log;
std::string m_snapshot;
char *buf;
int buf_size;
};
template <typename command> raft_storage<command>::raft_storage(const std::string &dir) {
m_metadata = dir + "/metadata";
m_log = dir + "/log";
m_snapshot = dir + "/snapshot";
buf_size = 16;
buf = new char[buf_size];
}
template <typename command> raft_storage<command>::~raft_storage() { delete[] buf; }
template <typename command> bool raft_storage<command>::updateMetadata(int term, int vote) {
std::unique_lock<std::mutex> lock(mtx);
std::fstream fs(m_metadata, std::ios::out | std::ios::trunc | std::ios::binary);
if (fs.fail()) {
return false;
}
fs.write((const char *)&term, sizeof(int));
fs.write((const char *)&vote, sizeof(int));
fs.close();
return true;
}
template <typename command> bool raft_storage<command>::updateLog(const std::vector<log_entry<command>> &log) {
std::unique_lock<std::mutex> lock(mtx);
std::fstream fs(m_log, std::ios::out | std::ios::trunc | std::ios::binary);
if (fs.fail()) {
return false;
}
int size = log.size();
fs.write((const char *)&size, sizeof(int));
for (const log_entry<command> &entry : log) {
fs.write((const char *)&entry.index, sizeof(int));
fs.write((const char *)&entry.term, sizeof(int));
size = entry.cmd.size();
fs.write((const char *)&size, sizeof(int));
if (size > buf_size) {
delete[] buf;
buf_size = std::max(size, 2 * buf_size);
buf = new char[buf_size];
}
entry.cmd.serialize(buf, size);
fs.write(buf, size);
}
fs.close();
return true;
}
template <typename command> bool raft_storage<command>::appendLog(const log_entry<command> &entry, int new_size) {
std::unique_lock<std::mutex> lock(mtx);
std::fstream fs(m_log, std::ios::out | std::ios::in | std::ios::binary);
if (fs.fail()) {
return false;
}
int size = 0;
fs.seekp(0, std::ios::end);
fs.write((const char *)&entry.index, sizeof(int));
fs.write((const char *)&entry.term, sizeof(int));
size = entry.cmd.size();
fs.write((const char *)&size, sizeof(int));
if (size > buf_size) {
delete[] buf;
buf_size = std::max(size, 2 * buf_size);
buf = new char[buf_size];
}
entry.cmd.serialize(buf, size);
fs.write(buf, size);
fs.seekp(0, std::ios::beg);
fs.write((const char *)&new_size, sizeof(int));
fs.close();
return true;
}
template <typename command>
bool raft_storage<command>::appendLog(const std::vector<log_entry<command>> &log, int new_size) {
std::unique_lock<std::mutex> lock(mtx);
std::fstream fs(m_log, std::ios::out | std::ios::in | std::ios::binary);
if (fs.fail()) {
return false;
}
int size = 0;
fs.seekp(0, std::ios::end);
for (const log_entry<command> &entry : log) {
fs.write((const char *)&entry.index, sizeof(int));
fs.write((const char *)&entry.term, sizeof(int));
size = entry.cmd.size();
fs.write((const char *)&size, sizeof(int));
if (size > buf_size) {
delete[] buf;
buf_size = std::max(size, 2 * buf_size);
buf = new char[buf_size];
}
entry.cmd.serialize(buf, size);
fs.write(buf, size);
}
fs.seekp(0, std::ios::beg);
fs.write((const char *)&new_size, sizeof(int));
fs.close();
return true;
}
template <typename command> bool raft_storage<command>::updateSnapshot(const std::vector<char> &snapshot) {
std::unique_lock<std::mutex> lock(mtx);
std::fstream fs(m_snapshot, std::ios::out | std::ios::trunc | std::ios::binary);
if (fs.fail()) {
return false;
}
int size = snapshot.size();
fs.write((const char *)&size, sizeof(int));
fs.write(snapshot.data(), size);
fs.close();
return true;
}
template <typename command>
bool raft_storage<command>::updateTotal(int term, int vote, const std::vector<log_entry<command>> &log,
const std::vector<char> &snapshot) {
if (!updateMetadata(term, vote)) {
return false;
}
if (!updateLog(log)) {
return false;
}
if (!updateSnapshot(snapshot)) {
return false;
}
return true;
}
template <typename command>
bool raft_storage<command>::restore(int &term, int &vote, std::vector<log_entry<command>> &log,
std::vector<char> &snapshot) {
std::unique_lock<std::mutex> lock(mtx);
std::fstream fs;
fs.open(m_metadata, std::ios::in | std::ios::binary);
if (fs.fail() || fs.eof()) { // no file or empty file
return false;
}
fs.read((char *)&term, sizeof(int));
fs.read((char *)&vote, sizeof(int));
fs.close();
fs.open(m_log, std::ios::in | std::ios::binary);
if (fs.fail() || fs.eof()) { // no file or empty file
return false;
}
int size = 0;
fs.read((char *)&size, sizeof(int));
log.resize(size);
for (log_entry<command> &entry : log) {
fs.read((char *)&entry.index, sizeof(int));
fs.read((char *)&entry.term, sizeof(int));
fs.read((char *)&size, sizeof(int));
if (size > buf_size) {
delete[] buf;
buf_size = std::max(size, 2 * buf_size);
buf = new char[buf_size];
}
fs.read(buf, size);
entry.cmd.deserialize(buf, size);
}
fs.close();
fs.open(m_snapshot, std::ios::in | std::ios::binary);
if (fs.fail() || fs.eof()) { // no file or empty file
return false;
}
fs.read((char *)&size, sizeof(int));
snapshot.resize(size);
for (char &c : snapshot) {
fs.read(&c, sizeof(char));
}
fs.close();
return true;
}
#endif // raft_storage_h