Skip to content

Commit

Permalink
Feature: LogIdList: add method purge() to delete log ids
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer committed May 8, 2022
1 parent deeb67b commit 2262c79
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 15 deletions.
28 changes: 28 additions & 0 deletions openraft/src/engine/log_id_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,34 @@ impl<NID: NodeId> LogIdList<NID> {
self.key_log_ids.push(new_log_id);
}

/// Purge log ids upto the log with index `upto_index`, inclusive.
#[allow(dead_code)]
pub(crate) fn purge(&mut self, upto: &LogId<NID>) {
// When installing snapshot it may need to purge across the `last_log_id`.
if upto.index > self.key_log_ids[self.key_log_ids.len() - 1].index {
assert!(upto > &self.key_log_ids[self.key_log_ids.len() - 1]);
self.key_log_ids = vec![*upto];
}

if upto.index < self.key_log_ids[0].index {
return;
}

let res = self.key_log_ids.binary_search_by(|log_id| log_id.index.cmp(&upto.index));

match res {
Ok(i) => {
if i > 0 {
self.key_log_ids = self.key_log_ids.split_off(i)
}
}
Err(i) => {
self.key_log_ids = self.key_log_ids.split_off(i - 1);
self.key_log_ids[0].index = upto.index;
}
}
}

/// Get the log id at the specified index.
///
/// It will return `last_purged_log_id` if index is at the last purged index.
Expand Down
85 changes: 70 additions & 15 deletions openraft/src/engine/log_id_list_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ use crate::LogId;

#[test]
fn test_log_id_list_extend_from_same_leader() -> anyhow::Result<()> {
let log_id = |t, i| LogId::<u64> {
leader_id: LeaderId { term: t, node_id: 1 },
index: i,
};

let mut ids = LogIdList::<u64>::default();

// Extend one log id to an empty LogIdList: Just store it directly
Expand Down Expand Up @@ -55,11 +50,6 @@ fn test_log_id_list_extend_from_same_leader() -> anyhow::Result<()> {

#[test]
fn test_log_id_list_append() -> anyhow::Result<()> {
let log_id = |t, i| LogId::<u64> {
leader_id: LeaderId { term: t, node_id: 1 },
index: i,
};

let mut ids = LogIdList::<u64>::default();

// Append log id one by one, check the internally constructed `key_log_id` as expected.
Expand All @@ -82,12 +72,71 @@ fn test_log_id_list_append() -> anyhow::Result<()> {
}

#[test]
fn test_log_id_list_get_log_id() -> anyhow::Result<()> {
let log_id = |t, i| LogId::<u64> {
leader_id: LeaderId { term: t, node_id: 1 },
index: i,
};
fn test_log_id_list_purge() -> anyhow::Result<()> {
// Append log id one by one, check the internally constructed `key_log_id` as expected.

let cases = vec![
//
(log_id(2, 1), vec![
log_id(2, 2),
log_id(3, 3),
log_id(6, 6),
log_id(9, 9),
log_id(9, 11),
]),
(log_id(2, 2), vec![
log_id(2, 2),
log_id(3, 3),
log_id(6, 6),
log_id(9, 9),
log_id(9, 11),
]),
(log_id(3, 3), vec![
log_id(3, 3),
log_id(6, 6),
log_id(9, 9),
log_id(9, 11),
]),
(log_id(3, 4), vec![
log_id(3, 4),
log_id(6, 6),
log_id(9, 9),
log_id(9, 11),
]),
(log_id(3, 5), vec![
log_id(3, 5),
log_id(6, 6),
log_id(9, 9),
log_id(9, 11),
]),
(log_id(6, 6), vec![log_id(6, 6), log_id(9, 9), log_id(9, 11)]),
(log_id(6, 7), vec![log_id(6, 7), log_id(9, 9), log_id(9, 11)]),
(log_id(6, 8), vec![log_id(6, 8), log_id(9, 9), log_id(9, 11)]),
(log_id(9, 9), vec![log_id(9, 9), log_id(9, 11)]),
(log_id(9, 10), vec![log_id(9, 10), log_id(9, 11)]),
(log_id(9, 11), vec![log_id(9, 11)]),
(log_id(9, 12), vec![log_id(9, 12)]),
(log_id(10, 12), vec![log_id(10, 12)]),
];

for (upto, want) in cases {
let mut ids = LogIdList::<u64>::new(vec![
log_id(2, 2), // force multi line
log_id(3, 3),
log_id(6, 6),
log_id(9, 9),
log_id(9, 11),
]);

ids.purge(&upto);
assert_eq!(want, ids.key_log_ids(), "purge upto: {}", upto);
}

Ok(())
}

#[test]
fn test_log_id_list_get_log_id() -> anyhow::Result<()> {
// Get log id from empty list always returns `None`.

let ids = LogIdList::<u64>::default();
Expand Down Expand Up @@ -122,3 +171,9 @@ fn test_log_id_list_get_log_id() -> anyhow::Result<()> {

Ok(())
}
fn log_id(term: u64, index: u64) -> LogId<u64> {
LogId::<u64> {
leader_id: LeaderId { term, node_id: 1 },
index,
}
}

0 comments on commit 2262c79

Please sign in to comment.