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
use std::io::ErrorKind::UnexpectedEof;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
#[cfg(feature = "zstd")]
use zstd::block::decompress;
use crossbeam::sync::SegQueue;
use super::*;
pub struct LockFreeLog {
pub(super) iobufs: Arc<IoBufs>,
flusher_shutdown: Arc<AtomicBool>,
flusher_handle: Option<std::thread::JoinHandle<()>>,
}
unsafe impl Send for LockFreeLog {}
unsafe impl Sync for LockFreeLog {}
impl Drop for LockFreeLog {
fn drop(&mut self) {
self.flusher_shutdown.store(
true,
std::sync::atomic::Ordering::SeqCst,
);
if let Some(join_handle) = self.flusher_handle.take() {
join_handle.join().unwrap();
}
}
}
impl LockFreeLog {
pub fn start_system(config: Config) -> LockFreeLog {
#[cfg(feature = "log")]
let _r = env_logger::init();
let deferred_hole_punches = Arc::new(SegQueue::new());
let iobufs = Arc::new(IoBufs::new(config.clone(), deferred_hole_punches.clone()));
let flusher_shutdown = Arc::new(AtomicBool::new(false));
let flusher_handle = config.get_flush_every_ms().map(|flush_every_ms| {
periodic_flusher::flusher(
"log flusher".to_owned(),
config.clone(),
iobufs.clone(),
flusher_shutdown.clone(),
flush_every_ms,
deferred_hole_punches,
).unwrap()
});
LockFreeLog {
iobufs: iobufs,
flusher_shutdown: flusher_shutdown,
flusher_handle: flusher_handle,
}
}
pub fn flush(&self) {
self.iobufs.flush();
}
pub fn defer_hole_punch(&self, lids: Vec<LogID>) {
self.iobufs.defer_hole_punch(lids);
}
}
impl Log for LockFreeLog {
fn reserve(&self, buf: Vec<u8>) -> Reservation {
self.iobufs.reserve(buf)
}
fn config(&self) -> &Config {
self.iobufs.config()
}
fn write(&self, buf: Vec<u8>) -> LogID {
self.iobufs.reserve(buf).complete()
}
fn read(&self, id: LogID) -> io::Result<LogRead> {
let start = clock();
let cached_f = self.config().cached_file();
let mut f = cached_f.borrow_mut();
f.seek(SeekFrom::Start(id))?;
let mut valid_buf = [0u8; 1];
f.read_exact(&mut valid_buf)?;
let valid = valid_buf[0] == 1;
let mut len_buf = [0u8; 4];
f.read_exact(&mut len_buf)?;
let len32: u32 = unsafe { std::mem::transmute(len_buf) };
let mut len = len32 as usize;
let max = self.config().get_io_buf_size() - HEADER_LEN;
if len > max {
#[cfg(feature = "log")]
error!("log read invalid message length, {} should be <= {}", len, max);
M.read.measure(clock() - start);
return Ok(LogRead::Corrupted(len));
} else if len == 0 && !valid {
loop {
let mut byte = [0u8; 1];
if let Err(e) = f.read_exact(&mut byte) {
if e.kind() == UnexpectedEof {
break;
}
panic!("{:?}", e);
}
if byte[0] != 1 {
debug_assert_eq!(byte[0], 0);
len += 1;
} else {
break;
}
}
}
if !valid {
M.read.measure(clock() - start);
return Ok(LogRead::Zeroed(len + 5));
}
let mut crc16_buf = [0u8; 2];
f.read_exact(&mut crc16_buf)?;
let mut buf = Vec::with_capacity(len);
unsafe {
buf.set_len(len);
}
f.read_exact(&mut buf)?;
let checksum = crc16_arr(&buf);
if checksum != crc16_buf {
M.read.measure(clock() - start);
return Ok(LogRead::Corrupted(len));
}
#[cfg(feature = "zstd")]
let res = {
if self.config().get_use_compression() {
let start = clock();
let res = Ok(LogRead::Flush(decompress(&*buf, max).unwrap(), len));
M.decompress.measure(clock() - start);
res
} else {
Ok(LogRead::Flush(buf, len))
}
};
#[cfg(not(feature = "zstd"))]
let res = Ok(LogRead::Flush(buf, len));
M.read.measure(clock() - start);
res
}
fn stable_offset(&self) -> LogID {
self.iobufs.stable()
}
fn make_stable(&self, id: LogID) {
let start = clock();
let mut spins = 0;
while self.iobufs.stable() <= id {
self.iobufs.flush();
spins += 1;
if spins > 2_000_000 {
#[cfg(feature = "log")]
debug!("have spun >2000000x in make_stable");
spins = 0;
}
}
M.make_stable.measure(clock() - start);
}
fn punch_hole(&self, id: LogID) -> std::io::Result<()> {
let cached_f = self.config().cached_file();
let mut f = cached_f.borrow_mut();
punch_hole(&mut f, id)
}
}