Skip to content

Commit

Permalink
Fix: AsyncReadExt::read_buf() only reads at most 2MB per call
Browse files Browse the repository at this point in the history
When streaming a snapshot chunk, it should repeatly `read_buf()` until
`snapshot_max_chunk_size` is full or read EOF.
  • Loading branch information
drmingdrmer committed Nov 28, 2023
1 parent c7725c7 commit f469878
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions openraft/src/replication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,20 @@ where

let mut offset = 0;
let end = snapshot.snapshot.seek(SeekFrom::End(0)).await.sto_res(err_x)?;
let mut buf = Vec::with_capacity(self.config.snapshot_max_chunk_size as usize);

loop {
// Build the RPC.
snapshot.snapshot.seek(SeekFrom::Start(offset)).await.sto_res(err_x)?;
let n_read = snapshot.snapshot.read_buf(&mut buf).await.sto_res(err_x)?;

let mut buf = Vec::with_capacity(self.config.snapshot_max_chunk_size as usize);
while buf.capacity() > buf.len() {
let n = snapshot.snapshot.read_buf(&mut buf).await.sto_res(err_x)?;
if n == 0 {
break;
}
}

let n_read = buf.len();

let leader_time = Instant::now();

Expand All @@ -650,10 +658,9 @@ where
vote: self.session_id.vote,
meta: snapshot.meta.clone(),
offset,
data: Vec::from(&buf[..n_read]),
data: buf,
done,
};
buf.clear();

// Send the RPC over to the target.
tracing::debug!(
Expand Down

0 comments on commit f469878

Please sign in to comment.