Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: logs might be missed during RegionBased replay in the WAL based on local disk #1570

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions src/analytic_engine/src/instance/wal_replayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! Wal replayer

use std::{
collections::{HashMap, VecDeque},
collections::{BTreeMap, HashMap, VecDeque},
fmt::Display,
ops::Range,
sync::Arc,
Expand Down Expand Up @@ -395,6 +395,11 @@ impl RegionBasedReplay {
if failed_tables.contains_key(&table_batch.table_id) {
continue;
}
let log_entries: Vec<_> = table_batch
.ranges
.iter()
.flat_map(|range| log_batch.range(range.clone()))
.collect();

let serial_exec_ctxs = serial_exec_ctxs.clone();
replay_tasks.push(async move {
Expand All @@ -405,7 +410,7 @@ impl RegionBasedReplay {
context.max_retry_flush_limit,
&mut ctx.serial_exec,
&ctx.table_data,
log_batch.range(table_batch.range),
log_entries.into_iter(),
)
.await;
(table_batch.table_id, Some(result))
Expand Down Expand Up @@ -439,13 +444,14 @@ impl RegionBasedReplay {

// Split log batch by table id, for example:
// input batch:
// |1|1|2|2|2|3|3|3|3|
// |1|1|2|2|2|3|3|3|3|1|1|
//
// output batches:
// |1|1|, |2|2|2|, |3|3|3|3|
// |1|1|1|1|, |2|2|2|, |3|3|3|3|
let mut start_log_idx = 0usize;
let mut curr_log_idx = 0usize;
let mut start_table_id = log_batch.get(start_log_idx).unwrap().table_id;
let mut table_ranges = BTreeMap::new();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need to order by table id?

IMO, there is no need for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yse, fixed.

loop {
let time_to_break = curr_log_idx == log_batch.len();
let found_end_idx = if time_to_break {
Expand All @@ -456,10 +462,10 @@ impl RegionBasedReplay {
};

if found_end_idx {
table_batches.push(TableBatch {
table_id: TableId::new(start_table_id),
range: start_log_idx..curr_log_idx,
});
table_ranges
.entry(TableId::new(start_table_id))
.or_insert(Vec::new())
.push(start_log_idx..curr_log_idx);

// Step to next start idx.
start_log_idx = curr_log_idx;
Expand All @@ -476,13 +482,16 @@ impl RegionBasedReplay {
}
curr_log_idx += 1;
}
for (table_id, ranges) in table_ranges {
table_batches.push(TableBatch { table_id, ranges });
}
}
}

#[derive(Debug, Eq, PartialEq)]
struct TableBatch {
table_id: TableId,
range: Range<usize>,
ranges: Vec<Range<usize>>,
}

struct SerialExecContext<'a> {
Expand Down Expand Up @@ -622,6 +631,7 @@ mod tests {
}
}

#[allow(clippy::single_range_in_vec_init)]
fn test_set() -> Vec<(VecDeque<LogEntry<u32>>, Vec<TableBatch>)> {
let test_log_batch1: VecDeque<LogEntry<u32>> = VecDeque::from([
LogEntry {
Expand Down Expand Up @@ -658,15 +668,15 @@ mod tests {
let expected1 = vec![
TableBatch {
table_id: TableId::new(0),
range: 0..3,
ranges: vec![0..3],
},
TableBatch {
table_id: TableId::new(1),
range: 3..5,
ranges: vec![3..5],
},
TableBatch {
table_id: TableId::new(2),
range: 5..6,
ranges: vec![5..6],
},
];

Expand All @@ -677,7 +687,7 @@ mod tests {
}]);
let expected2 = vec![TableBatch {
table_id: TableId::new(0),
range: 0..1,
ranges: vec![0..1],
}];

let test_log_batch3: VecDeque<LogEntry<u32>> = VecDeque::default();
Expand Down
Loading