use std::collections::{HashMap, VecDeque};
use std::io::{self, Write};
use std::sync::{Arc, Condvar, Mutex, PoisonError};
use std::time::{Duration, Instant};
use super::Link;
use super::codec::Message;
use super::protocol::{BackupPiece, BackupWrite, BackupWritten, Failure};
use crate::Store;
use crate::sync::lock;
const HELD: usize = 4;
const PIECE: usize = 256 << 10;
const PIECE_FIELDS: usize = 64;
const PATIENCE: Duration = if cfg!(test) { Duration::from_secs(1) } else { Duration::from_secs(60) };
#[derive(Default)]
pub(crate) struct Backups {
going: Arc<Mutex<HashMap<u32, Arc<Sending>>>>,
}
impl Backups {
pub(crate) fn grant(&self, stream: u32, credit: u32) {
let sending = lock(&self.going).get(&stream).cloned();
if let Some(sending) = sending {
sending.grant(credit);
forget(&self.going, &sending);
}
}
pub(crate) fn cancel(&self, stream: u32) -> bool {
let sending = lock(&self.going).remove(&stream);
sending.is_some_and(|sending| sending.end(Err(Failure::cancelled("the client cancelled its backup"))))
}
pub(crate) fn end_all(&self, why: &str) {
let going: Vec<Arc<Sending>> = lock(&self.going).drain().map(|(_, sending)| sending).collect();
for sending in going {
sending.end(Err(Failure::unavailable(why)));
}
}
}
pub(crate) fn asked(backups: &Backups, stream: u32, body: &[u8], link: Link) -> Result<Arc<Sending>, Failure> {
let asked = BackupWrite::decode(body)?;
let piece = PIECE.min(link.max_body.saturating_sub(PIECE_FIELDS)).max(1);
let state = Mutex::new(SendState { credit: link.credit, ..SendState::default() });
let sending = Arc::new(Sending { stream, asked, piece, state, room: Condvar::new(), link });
lock(&backups.going).insert(stream, Arc::clone(&sending));
Ok(sending)
}
pub(crate) struct Sending {
stream: u32,
asked: BackupWrite,
piece: usize,
state: Mutex<SendState>,
room: Condvar,
link: Link,
}
#[derive(Default)]
struct SendState {
credit: u64,
granted: Option<Instant>,
held: VecDeque<Vec<u8>>,
last: Option<Result<Vec<u8>, Failure>>,
ended: bool,
}
impl Sending {
pub(crate) fn begin(self: &Arc<Self>, store: &Store, backups: &Backups) {
let (store, sending, going) = (store.clone(), Arc::clone(self), Arc::clone(&backups.going));
(self.link.spawn)(Box::new(move || {
let mut backup = store.backup();
for name in &sending.asked.include {
backup = backup.include(name);
}
if sending.asked.include_encryption_key {
backup = backup.include_encryption_key();
}
let written = backup.write_to(Pieces { sending: &sending, piece: Vec::new() });
let last = written.map(|backup| {
let (files, bytes, key_left_out) = (backup.files as u64, backup.bytes, backup.key_left_out);
BackupWritten { files, bytes, key_left_out, excluded: backup.excluded }.encode()
});
sending.finish(last.map_err(Failure::from));
forget(&going, &sending);
}));
}
fn grant(&self, credit: u32) {
let mut state = lock(&self.state);
state.credit += u64::from(credit);
state.granted = Some(Instant::now());
self.flush(&mut state);
}
fn push(&self, body: Vec<u8>) -> io::Result<()> {
let mut state = lock(&self.state);
let began = Instant::now();
while state.held.len() >= HELD && !state.ended {
let since = state.granted.map_or(began, |granted| granted.max(began));
let left = (since + PATIENCE).saturating_duration_since(Instant::now());
if left.is_zero() {
let why = format!("the backup's reader took nothing for {PATIENCE:?}, and the backup stopped");
self.end_in(&mut state, Err(Failure::cancelled(why)));
break;
}
state = self.room.wait_timeout(state, left).unwrap_or_else(PoisonError::into_inner).0;
}
if state.ended {
return Err(io::Error::other("the stream of the backup ended"));
}
state.held.push_back(body);
self.flush(&mut state);
Ok(())
}
fn flush(&self, state: &mut SendState) {
while state.held.front().is_some_and(|body| body.len() as u64 <= state.credit) {
let body = state.held.pop_front().unwrap_or_default();
state.credit -= body.len() as u64;
(self.link.send)(body);
self.room.notify_all();
}
if state.held.is_empty()
&& !state.ended
&& let Some(last) = state.last.take()
{
state.ended = true;
(self.link.finish)(last);
}
}
fn finish(&self, last: Result<Vec<u8>, Failure>) {
let mut state = lock(&self.state);
if last.is_err() {
state.held.clear();
}
state.last = Some(last);
self.flush(&mut state);
}
fn end(&self, last: Result<Vec<u8>, Failure>) -> bool {
self.end_in(&mut lock(&self.state), last)
}
fn end_in(&self, state: &mut SendState, last: Result<Vec<u8>, Failure>) -> bool {
if std::mem::replace(&mut state.ended, true) {
return false;
}
state.held.clear();
self.room.notify_all();
(self.link.finish)(last);
true
}
}
struct Pieces<'a> {
sending: &'a Sending,
piece: Vec<u8>,
}
impl Write for Pieces<'_> {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
let taken = bytes.len().min(self.sending.piece - self.piece.len());
self.piece.extend_from_slice(&bytes[..taken]);
if self.piece.len() == self.sending.piece {
self.flush()?;
}
Ok(taken)
}
fn flush(&mut self) -> io::Result<()> {
if self.piece.is_empty() {
return Ok(());
}
let bytes = std::mem::take(&mut self.piece);
self.sending.push(BackupPiece { bytes }.encode())
}
}
fn forget(going: &Mutex<HashMap<u32, Arc<Sending>>>, sending: &Arc<Sending>) {
if !lock(&sending.state).ended {
return;
}
let mut going = lock(going);
if going.get(&sending.stream).is_some_and(|held| Arc::ptr_eq(held, sending)) {
going.remove(&sending.stream);
}
}
#[cfg(test)]
#[path = "backup_tests.rs"]
mod tests;