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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use log::warn;
use std::collections::hash_map::DefaultHasher;
use std::fs::{read_dir, DirBuilder, File, OpenOptions};
use std::hash::{Hash, Hasher};
use std::io::{self, Read, Seek, Write};
use std::str;
use std::{
collections::{btree_map::Entry, BTreeMap},
convert::TryInto,
};
const DELETE_FILE_INTERVAL: u64 = 8;
const INDEX_NAME: &str = "index";
#[derive(Debug, Clone, Copy)]
pub enum LogType {
Skip = 0,
Propose = 1,
QuorumVotes = 2,
FinalizeBlock = 3,
}
impl From<u8> for LogType {
fn from(s: u8) -> LogType {
match s {
1 => LogType::Propose,
2 => LogType::QuorumVotes,
3 => LogType::FinalizeBlock,
_ => LogType::Skip,
}
}
}
pub struct Wal {
height_fs: BTreeMap<u64, File>,
dir: String,
current_height: u64,
ifile: File,
}
impl Wal {
fn delete_old_file(dir: &str, current_height: u64) -> io::Result<()> {
for entry in read_dir(dir)? {
let entry = entry?;
let fpath = entry.path();
if let Some(fname) = fpath.file_name() {
let strs: Vec<&str> = fname.to_str().unwrap().split('.').collect();
if !strs.is_empty() {
let num = strs[0].parse::<u64>().unwrap_or(current_height);
if num + DELETE_FILE_INTERVAL < current_height {
::std::fs::remove_file(fpath)?;
}
}
}
}
Ok(())
}
pub fn create(dir: &str) -> io::Result<Wal> {
let fss = read_dir(&dir);
if fss.is_err() {
DirBuilder::new().recursive(true).create(dir)?;
}
let file_path = dir.to_string() + "/" + INDEX_NAME;
let mut ifs = OpenOptions::new()
.read(true)
.create(true)
.write(true)
.open(file_path)?;
ifs.seek(io::SeekFrom::Start(0)).unwrap();
let mut string_buf: String = String::new();
let res_fsize = ifs.read_to_string(&mut string_buf)?;
let num_str = string_buf.trim();
let cur_height: u64;
let last_file_path: String;
if res_fsize == 0 {
last_file_path = dir.to_string() + "/1.log";
cur_height = 1;
} else {
let hi_res = num_str.parse::<u64>();
if let Ok(hi) = hi_res {
cur_height = hi;
last_file_path = dir.to_string() + "/" + cur_height.to_string().as_str() + ".log"
} else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"index file data wrong",
));
}
}
Self::delete_old_file(dir, cur_height)?;
let fs = OpenOptions::new()
.read(true)
.create(true)
.write(true)
.open(last_file_path)?;
let mut tmp = BTreeMap::new();
tmp.insert(cur_height, fs);
Ok(Wal {
height_fs: tmp,
dir: dir.to_string(),
current_height: cur_height,
ifile: ifs,
})
}
fn get_file_path(dir: &str, height: u64) -> String {
let mut name = height.to_string();
name += ".log";
let pathname = dir.to_string() + "/";
pathname + &*name
}
fn set_index_file(&mut self, height: u64) -> io::Result<u64> {
self.current_height = height;
self.ifile.seek(io::SeekFrom::Start(0))?;
let hstr = height.to_string();
let content = hstr.as_bytes();
let len = content.len() as u64;
self.ifile.set_len(len)?;
self.ifile.write_all(content)?;
self.ifile.sync_data()?;
Ok(len)
}
pub fn set_height(&mut self, height: u64) -> io::Result<u64> {
let len = self.set_index_file(height)?;
if let Entry::Vacant(entry) = self.height_fs.entry(height) {
let filename = Wal::get_file_path(&self.dir, height);
let fs = OpenOptions::new()
.create(true)
.read(true)
.append(true)
.open(filename)?;
entry.insert(fs);
}
if height > DELETE_FILE_INTERVAL {
let newer_file = self.height_fs.split_off(&(height - DELETE_FILE_INTERVAL));
for &i in self.height_fs.keys() {
let delfilename = Wal::get_file_path(&self.dir, i);
let _ = ::std::fs::remove_file(delfilename);
}
self.height_fs = newer_file;
}
Ok(len)
}
pub fn save(&mut self, height: u64, log_type: LogType, msg: &[u8]) -> io::Result<u64> {
let mtype = log_type as u8;
let mlen = msg.len() as u32;
if mlen == 0 {
return Ok(0);
}
if height > self.current_height && height < self.current_height + DELETE_FILE_INTERVAL {
if let Entry::Vacant(entry) = self.height_fs.entry(height) {
let filename = Wal::get_file_path(&self.dir, height);
let fs = OpenOptions::new()
.read(true)
.create(true)
.write(true)
.open(filename)?;
entry.insert(fs);
}
}
let mut hlen = 0;
if let Some(fs) = self.height_fs.get_mut(&height) {
let len_bytes: [u8; 4] = mlen.to_le_bytes();
let type_bytes: [u8; 1] = [mtype];
let check_sum = calculate_hash(&msg);
fs.seek(io::SeekFrom::End(0))?;
fs.write_all(&len_bytes[..])?;
fs.write_all(&type_bytes[..])?;
fs.write_all(&check_sum.to_le_bytes())?;
hlen = fs.write(msg)?;
fs.flush()?;
} else {
warn!(
"wal not save height {} current height {} ",
height, self.current_height
);
}
let _ = self.set_height(height);
Ok(hlen as u64)
}
pub fn get_cur_height(&self) -> u64 {
self.current_height
}
pub fn load(&self) -> Vec<(u8, Vec<u8>)> {
let mut vec_buf: Vec<u8> = Vec::new();
let mut vec_out: Vec<(u8, Vec<u8>)> = Vec::new();
let cur_height = self.current_height;
if self.height_fs.is_empty() || cur_height == 0 {
return vec_out;
}
for (height, mut fs) in &self.height_fs {
if *height < self.current_height {
continue;
}
fs.seek(io::SeekFrom::Start(0)).unwrap();
let res_fsize = fs.read_to_end(&mut vec_buf);
if res_fsize.is_err() {
return vec_out;
}
let fsize = res_fsize.unwrap();
let mut index = 0;
loop {
if index + 9 > fsize {
break;
}
let bytes = match vec_buf[index..index + 4].try_into() {
Ok(bytes) => bytes,
Err(e) => {
warn!("wal file may be corrupted: {}", e);
return Vec::new();
}
};
let tmp = u32::from_le_bytes(bytes);
let bodylen = tmp as usize;
let mtype = vec_buf[index + 4];
let bytes = match vec_buf[index + 5..index + 9].try_into() {
Ok(bytes) => bytes,
Err(e) => {
warn!("wal file may be corrupted: {}", e);
return Vec::new();
}
};
let saved_crc = u64::from_le_bytes(bytes);
index += 9;
if index + bodylen > fsize {
break;
}
let msg = &vec_buf[index..index + bodylen];
let check_sum = calculate_hash(&msg);
if check_sum != saved_crc {
warn!(
"wal crc checked error saved {} check {}",
saved_crc, check_sum
);
break;
}
vec_out.push((mtype, msg.to_vec()));
index += bodylen;
}
}
vec_out
}
pub fn clear_file(&mut self) -> io::Result<()> {
self.height_fs.clear();
for entry in read_dir(self.dir.clone())? {
let fpath = entry?.path();
let fname = fpath.file_name().map(|f| f.to_str()).flatten();
if let Some(fname) = fname {
if !fname.contains(INDEX_NAME) {
let _ = ::std::fs::remove_file(fpath);
}
}
}
Ok(())
}
}
fn calculate_hash<T: Hash>(t: &T) -> u64 {
let mut s = DefaultHasher::new();
t.hash(&mut s);
s.finish()
}