use crate::eraftpb::{Entry, Snapshot};
use slog::Logger;
#[derive(Debug)]
pub struct Unstable {
pub snapshot: Option<Snapshot>,
pub entries: Vec<Entry>,
pub offset: u64,
pub logger: Logger,
}
impl Unstable {
pub fn new(offset: u64, logger: Logger) -> Unstable {
Unstable {
offset,
snapshot: None,
entries: vec![],
logger,
}
}
pub fn maybe_first_index(&self) -> Option<u64> {
self.snapshot
.as_ref()
.map(|snap| snap.get_metadata().index + 1)
}
pub fn maybe_last_index(&self) -> Option<u64> {
match self.entries.len() {
0 => self.snapshot.as_ref().map(|snap| snap.get_metadata().index),
len => Some(self.offset + len as u64 - 1),
}
}
pub fn maybe_term(&self, idx: u64) -> Option<u64> {
if idx < self.offset {
let snapshot = self.snapshot.as_ref()?;
let meta = snapshot.get_metadata();
if idx == meta.index {
Some(meta.term)
} else {
None
}
} else {
self.maybe_last_index().and_then(|last| {
if idx > last {
return None;
}
Some(self.entries[(idx - self.offset) as usize].term)
})
}
}
pub fn stable_entries(&mut self, index: u64, term: u64) {
assert!(self.snapshot.is_none());
if let Some(entry) = self.entries.last() {
if entry.get_index() != index || entry.get_term() != term {
fatal!(
self.logger,
"the last one of unstable.slice has different index {} and term {}, expect {} {}",
entry.get_index(),
entry.get_term(),
index,
term
);
}
self.offset = entry.get_index() + 1;
self.entries.clear();
} else {
fatal!(
self.logger,
"unstable.slice is empty, expect its last one's index and term are {} and {}",
index,
term
);
}
}
pub fn stable_snap(&mut self, index: u64) {
if let Some(snap) = &self.snapshot {
if snap.get_metadata().index != index {
fatal!(
self.logger,
"unstable.snap has different index {}, expect {}",
snap.get_metadata().index,
index
);
}
self.snapshot = None;
} else {
fatal!(
self.logger,
"unstable.snap is none, expect a snapshot with index {}",
index
);
}
}
pub fn restore(&mut self, snap: Snapshot) {
self.entries.clear();
self.offset = snap.get_metadata().index + 1;
self.snapshot = Some(snap);
}
pub fn truncate_and_append(&mut self, ents: &[Entry]) {
let after = ents[0].index;
if after == self.offset + self.entries.len() as u64 {
self.entries.extend_from_slice(ents);
} else if after <= self.offset {
self.offset = after;
self.entries.clear();
self.entries.extend_from_slice(ents);
} else {
let off = self.offset;
self.must_check_outofbounds(off, after);
self.entries.truncate((after - off) as usize);
self.entries.extend_from_slice(ents);
}
}
pub fn slice(&self, lo: u64, hi: u64) -> &[Entry] {
self.must_check_outofbounds(lo, hi);
let l = lo as usize;
let h = hi as usize;
let off = self.offset as usize;
&self.entries[l - off..h - off]
}
pub fn must_check_outofbounds(&self, lo: u64, hi: u64) {
if lo > hi {
fatal!(self.logger, "invalid unstable.slice {} > {}", lo, hi)
}
let upper = self.offset + self.entries.len() as u64;
if lo < self.offset || hi > upper {
fatal!(
self.logger,
"unstable.slice[{}, {}] out of bound[{}, {}]",
lo,
hi,
self.offset,
upper
)
}
}
}
#[cfg(test)]
mod test {
use crate::eraftpb::{Entry, Snapshot, SnapshotMetadata};
use crate::log_unstable::Unstable;
fn new_entry(index: u64, term: u64) -> Entry {
let mut e = Entry::default();
e.term = term;
e.index = index;
e
}
fn new_snapshot(index: u64, term: u64) -> Snapshot {
let mut snap = Snapshot::default();
let mut meta = SnapshotMetadata::default();
meta.index = index;
meta.term = term;
snap.set_metadata(meta);
snap
}
#[test]
fn test_maybe_first_index() {
let tests = vec![
(Some(new_entry(5, 1)), 5, None, false, 0),
(None, 0, None, false, 0),
(Some(new_entry(5, 1)), 5, Some(new_snapshot(4, 1)), true, 5),
(None, 5, Some(new_snapshot(4, 1)), true, 5),
];
for (entries, offset, snapshot, wok, windex) in tests {
let u = Unstable {
entries: entries.map_or(vec![], |entry| vec![entry]),
offset,
snapshot,
logger: crate::default_logger(),
};
let index = u.maybe_first_index();
match index {
None => assert!(!wok),
Some(index) => assert_eq!(index, windex),
}
}
}
#[test]
fn test_maybe_last_index() {
let tests = vec![
(Some(new_entry(5, 1)), 5, None, true, 5),
(Some(new_entry(5, 1)), 5, Some(new_snapshot(4, 1)), true, 5),
(None, 5, Some(new_snapshot(4, 1)), true, 4),
(None, 0, None, false, 0),
];
for (entries, offset, snapshot, wok, windex) in tests {
let u = Unstable {
entries: entries.map_or(vec![], |entry| vec![entry]),
offset,
snapshot,
logger: crate::default_logger(),
};
let index = u.maybe_last_index();
match index {
None => assert!(!wok),
Some(index) => assert_eq!(index, windex),
}
}
}
#[test]
fn test_maybe_term() {
let tests = vec![
(Some(new_entry(5, 1)), 5, None, 5, true, 1),
(Some(new_entry(5, 1)), 5, None, 6, false, 0),
(Some(new_entry(5, 1)), 5, None, 4, false, 0),
(
Some(new_entry(5, 1)),
5,
Some(new_snapshot(4, 1)),
5,
true,
1,
),
(
Some(new_entry(5, 1)),
5,
Some(new_snapshot(4, 1)),
6,
false,
0,
),
(
Some(new_entry(5, 1)),
5,
Some(new_snapshot(4, 1)),
4,
true,
1,
),
(
Some(new_entry(5, 1)),
5,
Some(new_snapshot(4, 1)),
3,
false,
0,
),
(None, 5, Some(new_snapshot(4, 1)), 5, false, 0),
(None, 5, Some(new_snapshot(4, 1)), 4, true, 1),
(None, 0, None, 5, false, 0),
];
for (entries, offset, snapshot, index, wok, wterm) in tests {
let u = Unstable {
entries: entries.map_or(vec![], |entry| vec![entry]),
offset,
snapshot,
logger: crate::default_logger(),
};
let term = u.maybe_term(index);
match term {
None => assert!(!wok),
Some(term) => assert_eq!(term, wterm),
}
}
}
#[test]
fn test_restore() {
let mut u = Unstable {
entries: vec![new_entry(5, 1)],
offset: 5,
snapshot: Some(new_snapshot(4, 1)),
logger: crate::default_logger(),
};
let s = new_snapshot(6, 2);
u.restore(s.clone());
assert_eq!(u.offset, s.get_metadata().index + 1);
assert!(u.entries.is_empty());
assert_eq!(u.snapshot.unwrap(), s);
}
#[test]
fn test_stable_snapshot_and_entries() {
let ents = vec![new_entry(5, 1), new_entry(5, 2), new_entry(6, 3)];
let mut u = Unstable {
entries: ents.clone(),
offset: 5,
snapshot: Some(new_snapshot(4, 1)),
logger: crate::default_logger(),
};
assert_eq!(ents, u.entries);
u.stable_snap(4);
u.stable_entries(6, 3);
assert!(u.entries.is_empty());
assert_eq!(u.offset, 7);
}
#[test]
fn test_truncate_and_append() {
let tests = vec![
(
vec![new_entry(5, 1)],
5,
None,
vec![new_entry(6, 1), new_entry(7, 1)],
5,
vec![new_entry(5, 1), new_entry(6, 1), new_entry(7, 1)],
),
(
vec![new_entry(5, 1)],
5,
None,
vec![new_entry(5, 2), new_entry(6, 2)],
5,
vec![new_entry(5, 2), new_entry(6, 2)],
),
(
vec![new_entry(5, 1)],
5,
None,
vec![new_entry(4, 2), new_entry(5, 2), new_entry(6, 2)],
4,
vec![new_entry(4, 2), new_entry(5, 2), new_entry(6, 2)],
),
(
vec![new_entry(5, 1), new_entry(6, 1), new_entry(7, 1)],
5,
None,
vec![new_entry(6, 2)],
5,
vec![new_entry(5, 1), new_entry(6, 2)],
),
(
vec![new_entry(5, 1), new_entry(6, 1), new_entry(7, 1)],
5,
None,
vec![new_entry(7, 2), new_entry(8, 2)],
5,
vec![
new_entry(5, 1),
new_entry(6, 1),
new_entry(7, 2),
new_entry(8, 2),
],
),
];
for (entries, offset, snapshot, to_append, woffset, wentries) in tests {
let mut u = Unstable {
entries,
offset,
snapshot,
logger: crate::default_logger(),
};
u.truncate_and_append(&to_append);
assert_eq!(u.offset, woffset);
assert_eq!(u.entries, wentries);
}
}
}