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
use std::fs;
use std::path::Path;
use engine_traits::{CacheStats, RaftEngine, RaftLogBatch as RaftLogBatchTrait, Result};
use kvproto::raft_serverpb::RaftLocalState;
use raft::eraftpb::Entry;
use raft_engine::{EntryExt, Error as RaftEngineError, LogBatch, RaftLogEngine as RawRaftEngine};
pub use raft_engine::{Config as RaftEngineConfig, RecoveryMode};
#[derive(Clone)]
pub struct EntryExtTyped;
impl EntryExt<Entry> for EntryExtTyped {
fn index(e: &Entry) -> u64 {
e.index
}
}
#[derive(Clone)]
pub struct RaftLogEngine(RawRaftEngine<Entry, EntryExtTyped>);
impl RaftLogEngine {
pub fn new(config: RaftEngineConfig) -> Self {
RaftLogEngine(RawRaftEngine::new(config))
}
pub fn exists(path: &str) -> bool {
let path = Path::new(path);
if !path.exists() || !path.is_dir() {
return false;
}
fs::read_dir(&path).unwrap().next().is_some()
}
pub fn raft_groups(&self) -> Vec<u64> {
self.0.raft_groups()
}
pub fn first_index(&self, raft_id: u64) -> Option<u64> {
self.0.first_index(raft_id)
}
pub fn last_index(&self, raft_id: u64) -> Option<u64> {
self.0.last_index(raft_id)
}
}
#[derive(Default)]
pub struct RaftLogBatch(LogBatch<Entry, EntryExtTyped>);
const RAFT_LOG_STATE_KEY: &[u8] = b"R";
impl RaftLogBatchTrait for RaftLogBatch {
fn append(&mut self, raft_group_id: u64, entries: Vec<Entry>) -> Result<()> {
self.0.add_entries(raft_group_id, entries);
Ok(())
}
fn cut_logs(&mut self, _: u64, _: u64, _: u64) {
}
fn put_raft_state(&mut self, raft_group_id: u64, state: &RaftLocalState) -> Result<()> {
box_try!(
self.0
.put_msg(raft_group_id, RAFT_LOG_STATE_KEY.to_vec(), state)
);
Ok(())
}
fn is_empty(&self) -> bool {
self.0.items.is_empty()
}
}
impl RaftEngine for RaftLogEngine {
type LogBatch = RaftLogBatch;
fn log_batch(&self, _capacity: usize) -> Self::LogBatch {
RaftLogBatch::default()
}
fn sync(&self) -> Result<()> {
box_try!(self.0.sync());
Ok(())
}
fn get_raft_state(&self, raft_group_id: u64) -> Result<Option<RaftLocalState>> {
let state = box_try!(self.0.get_msg(raft_group_id, RAFT_LOG_STATE_KEY));
Ok(state)
}
fn get_entry(&self, raft_group_id: u64, index: u64) -> Result<Option<Entry>> {
self.0
.get_entry(raft_group_id, index)
.map_err(transfer_error)
}
fn fetch_entries_to(
&self,
raft_group_id: u64,
begin: u64,
end: u64,
max_size: Option<usize>,
to: &mut Vec<Entry>,
) -> Result<usize> {
self.0
.fetch_entries_to(raft_group_id, begin, end, max_size, to)
.map_err(transfer_error)
}
fn consume(&self, batch: &mut Self::LogBatch, sync: bool) -> Result<usize> {
let ret = box_try!(self.0.write(&mut batch.0, sync));
Ok(ret)
}
fn consume_and_shrink(
&self,
batch: &mut Self::LogBatch,
sync: bool,
_: usize,
_: usize,
) -> Result<usize> {
let ret = box_try!(self.0.write(&mut batch.0, sync));
Ok(ret)
}
fn clean(
&self,
raft_group_id: u64,
_: &RaftLocalState,
batch: &mut RaftLogBatch,
) -> Result<()> {
batch.0.clean_region(raft_group_id);
Ok(())
}
fn append(&self, raft_group_id: u64, entries: Vec<Entry>) -> Result<usize> {
let mut batch = Self::LogBatch::default();
batch.0.add_entries(raft_group_id, entries);
let ret = box_try!(self.0.write(&mut batch.0, false));
Ok(ret)
}
fn put_raft_state(&self, raft_group_id: u64, state: &RaftLocalState) -> Result<()> {
box_try!(self.0.put_msg(raft_group_id, RAFT_LOG_STATE_KEY, state));
Ok(())
}
fn gc(&self, raft_group_id: u64, _from: u64, to: u64) -> Result<usize> {
Ok(self.0.compact_to(raft_group_id, to) as usize)
}
fn purge_expired_files(&self) -> Result<Vec<u64>> {
let ret = box_try!(self.0.purge_expired_files());
Ok(ret)
}
fn has_builtin_entry_cache(&self) -> bool {
true
}
fn gc_entry_cache(&self, raft_group_id: u64, to: u64) {
self.0.compact_cache_to(raft_group_id, to)
}
fn flush_stats(&self) -> Option<CacheStats> {
let stat = self.0.flush_cache_stats();
Some(engine_traits::CacheStats {
hit: stat.hit,
miss: stat.miss,
cache_size: stat.cache_size,
})
}
fn stop(&self) {
self.0.stop();
}
fn dump_stats(&self) -> Result<String> {
Ok("".to_owned())
}
}
fn transfer_error(e: RaftEngineError) -> engine_traits::Error {
match e {
RaftEngineError::StorageCompacted => engine_traits::Error::EntriesCompacted,
RaftEngineError::StorageUnavailable => engine_traits::Error::EntriesUnavailable,
e => {
let e = box_err!(e);
engine_traits::Error::Other(e)
}
}
}