Skip to content

Commit ffae522

Browse files
committed
Fix: AsyncReadExt::read_buf() only reads at most 2MB per call
When streaming a snapshot chunk, it should repeatly `read_buf()` until `snapshot_max_chunk_size` is full or read EOF.
1 parent 503fa49 commit ffae522

File tree

1 file changed

+11
-4
lines changed
  • openraft/src/replication

1 file changed

+11
-4
lines changed

openraft/src/replication/mod.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -645,12 +645,20 @@ where
645645

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

650649
loop {
651650
// Build the RPC.
652651
snapshot.snapshot.seek(SeekFrom::Start(offset)).await.sto_res(err_x)?;
653-
let n_read = snapshot.snapshot.read_buf(&mut buf).await.sto_res(err_x)?;
652+
653+
let mut buf = Vec::with_capacity(self.config.snapshot_max_chunk_size as usize);
654+
while buf.capacity() > buf.len() {
655+
let n = snapshot.snapshot.read_buf(&mut buf).await.sto_res(err_x)?;
656+
if n == 0 {
657+
break;
658+
}
659+
}
660+
661+
let n_read = buf.len();
654662

655663
let leader_time = <C::AsyncRuntime as AsyncRuntime>::Instant::now();
656664

@@ -659,10 +667,9 @@ where
659667
vote: self.session_id.vote,
660668
meta: snapshot.meta.clone(),
661669
offset,
662-
data: Vec::from(&buf[..n_read]),
670+
data: buf,
663671
done,
664672
};
665-
buf.clear();
666673

667674
// Send the RPC over to the target.
668675
tracing::debug!(

0 commit comments

Comments
 (0)