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 8, 2023
1 parent 503fa49 commit ffae522
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 @@ -645,12 +645,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 = <C::AsyncRuntime as AsyncRuntime>::Instant::now();

Expand All @@ -659,10 +667,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 ffae522

Please sign in to comment.