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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use std::collections::Bound;
use std::fmt::Debug;
use std::ops::RangeBounds;
use async_trait::async_trait;
use crate::raft::Entry;
use crate::storage::HardState;
use crate::AppData;
use crate::AppDataResponse;
use crate::DefensiveError;
use crate::ErrorSubject;
use crate::LogId;
use crate::RaftStorage;
use crate::StorageError;
use crate::Violation;
use crate::Wrapper;
#[async_trait]
pub trait DefensiveCheck<D, R, T>
where
D: AppData,
R: AppDataResponse,
T: RaftStorage<D, R>,
Self: Wrapper<T>,
{
fn set_defensive(&self, v: bool);
fn is_defensive(&self) -> bool;
async fn defensive_no_dirty_log(&self) -> Result<(), StorageError> {
if !self.is_defensive() {
return Ok(());
}
let (last_applied, _) = self.inner().last_applied_state().await?;
let last_log_id = self.inner().last_id_in_log().await?;
if last_log_id.index > last_applied.index && last_log_id < last_applied {
return Err(
DefensiveError::new(ErrorSubject::Log(last_log_id), Violation::DirtyLog {
higher_index_log_id: last_log_id,
lower_index_log_id: last_applied,
})
.into(),
);
}
Ok(())
}
async fn defensive_incremental_hard_state(&self, hs: &HardState) -> Result<(), StorageError> {
if !self.is_defensive() {
return Ok(());
}
let h = self.inner().read_hard_state().await?;
let curr = h.unwrap_or_default();
if hs.current_term < curr.current_term {
return Err(
DefensiveError::new(ErrorSubject::HardState, Violation::TermNotAscending {
curr: curr.current_term,
to: hs.current_term,
})
.into(),
);
}
if hs.current_term == curr.current_term && curr.voted_for.is_some() && hs.voted_for != curr.voted_for {
return Err(
DefensiveError::new(ErrorSubject::HardState, Violation::VotedForChanged {
curr,
to: hs.clone(),
})
.into(),
);
}
Ok(())
}
async fn defensive_consecutive_input(&self, entries: &[&Entry<D>]) -> Result<(), StorageError> {
if !self.is_defensive() {
return Ok(());
}
if entries.is_empty() {
return Ok(());
}
let mut prev_log_id = entries[0].log_id;
for e in entries.iter().skip(1) {
if e.log_id.index != prev_log_id.index + 1 {
return Err(DefensiveError::new(ErrorSubject::Logs, Violation::LogsNonConsecutive {
prev: prev_log_id,
next: e.log_id,
})
.into());
}
prev_log_id = e.log_id;
}
Ok(())
}
async fn defensive_nonempty_input(&self, entries: &[&Entry<D>]) -> Result<(), StorageError> {
if !self.is_defensive() {
return Ok(());
}
if entries.is_empty() {
return Err(DefensiveError::new(ErrorSubject::Logs, Violation::LogsEmpty).into());
}
Ok(())
}
async fn defensive_append_log_index_is_last_plus_one(&self, entries: &[&Entry<D>]) -> Result<(), StorageError> {
if !self.is_defensive() {
return Ok(());
}
let last_id = self.last_log_id().await?;
let first_id = entries[0].log_id;
if last_id.index + 1 != first_id.index {
return Err(
DefensiveError::new(ErrorSubject::Log(first_id), Violation::LogsNonConsecutive {
prev: last_id,
next: first_id,
})
.into(),
);
}
Ok(())
}
async fn defensive_append_log_id_gt_last(&self, entries: &[&Entry<D>]) -> Result<(), StorageError> {
if !self.is_defensive() {
return Ok(());
}
let last_id = self.last_log_id().await?;
let first_id = entries[0].log_id;
if first_id < last_id {
return Err(
DefensiveError::new(ErrorSubject::Log(first_id), Violation::LogsNonConsecutive {
prev: last_id,
next: first_id,
})
.into(),
);
}
Ok(())
}
async fn last_log_id(&self) -> Result<LogId, StorageError> {
let log_last_id = self.inner().last_id_in_log().await?;
let (sm_last_id, _) = self.inner().last_applied_state().await?;
Ok(std::cmp::max(log_last_id, sm_last_id))
}
async fn defensive_apply_index_is_last_applied_plus_one(&self, entries: &[&Entry<D>]) -> Result<(), StorageError> {
if !self.is_defensive() {
return Ok(());
}
let (last_id, _) = self.inner().last_applied_state().await?;
let first_id = entries[0].log_id;
if last_id.index + 1 != first_id.index {
return Err(
DefensiveError::new(ErrorSubject::Apply(first_id), Violation::ApplyNonConsecutive {
prev: last_id,
next: first_id,
})
.into(),
);
}
Ok(())
}
async fn defensive_nonempty_range<RNG: RangeBounds<u64> + Clone + Debug + Send>(
&self,
range: RNG,
) -> Result<(), StorageError> {
if !self.is_defensive() {
return Ok(());
}
let start = match range.start_bound() {
Bound::Included(i) => Some(*i),
Bound::Excluded(i) => Some(*i + 1),
Bound::Unbounded => None,
};
let end = match range.end_bound() {
Bound::Included(i) => Some(*i),
Bound::Excluded(i) => Some(*i - 1),
Bound::Unbounded => None,
};
if start.is_none() || end.is_none() {
return Ok(());
}
if start > end {
return Err(DefensiveError::new(ErrorSubject::Logs, Violation::RangeEmpty { start, end }).into());
}
Ok(())
}
async fn defensive_half_open_range<RNG: RangeBounds<u64> + Clone + Debug + Send>(
&self,
range: RNG,
) -> Result<(), StorageError> {
if !self.is_defensive() {
return Ok(());
}
if let Bound::Unbounded = range.start_bound() {
return Ok(());
};
if let Bound::Unbounded = range.end_bound() {
return Ok(());
};
Err(DefensiveError::new(ErrorSubject::Logs, Violation::RangeNotHalfOpen {
start: range.start_bound().cloned(),
end: range.end_bound().cloned(),
})
.into())
}
async fn defensive_range_hits_logs<RNG: RangeBounds<u64> + Debug + Send>(
&self,
range: RNG,
logs: &[Entry<D>],
) -> Result<(), StorageError> {
if !self.is_defensive() {
return Ok(());
}
{
let want_first = match range.start_bound() {
Bound::Included(i) => Some(*i),
Bound::Excluded(i) => Some(*i + 1),
Bound::Unbounded => None,
};
let first = logs.first().map(|x| x.log_id.index);
if let Some(want) = want_first {
if first != want_first {
return Err(
DefensiveError::new(ErrorSubject::LogIndex(want), Violation::LogIndexNotFound {
want,
got: first,
})
.into(),
);
}
}
}
{
let want_last = match range.end_bound() {
Bound::Included(i) => Some(*i),
Bound::Excluded(i) => Some(*i - 1),
Bound::Unbounded => None,
};
let last = logs.last().map(|x| x.log_id.index);
if let Some(want) = want_last {
if last != want_last {
return Err(
DefensiveError::new(ErrorSubject::LogIndex(want), Violation::LogIndexNotFound {
want,
got: last,
})
.into(),
);
}
}
}
Ok(())
}
async fn defensive_apply_log_id_gt_last(&self, entries: &[&Entry<D>]) -> Result<(), StorageError> {
if !self.is_defensive() {
return Ok(());
}
let (last_id, _) = self.inner().last_applied_state().await?;
let first_id = entries[0].log_id;
if first_id < last_id {
return Err(
DefensiveError::new(ErrorSubject::Apply(first_id), Violation::ApplyNonConsecutive {
prev: last_id,
next: first_id,
})
.into(),
);
}
Ok(())
}
}