2020-06-12 09:38:21 +00:00
|
|
|
use std::future::Future;
|
2019-07-05 08:42:46 +00:00
|
|
|
use std::collections::HashMap;
|
2020-06-12 09:38:21 +00:00
|
|
|
use std::pin::Pin;
|
2020-07-03 05:36:23 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2019-08-26 11:40:24 +00:00
|
|
|
|
2020-08-10 11:25:07 +00:00
|
|
|
use anyhow::{bail, Error};
|
2019-07-03 12:39:13 +00:00
|
|
|
|
2021-07-09 13:29:42 +00:00
|
|
|
use pbs_datastore::{CryptConfig, CryptMode};
|
|
|
|
use pbs_datastore::data_blob::DataBlob;
|
|
|
|
use pbs_datastore::read_chunk::ReadChunk;
|
2021-07-19 08:44:40 +00:00
|
|
|
use pbs_datastore::read_chunk::AsyncReadChunk;
|
2021-07-06 10:08:44 +00:00
|
|
|
use pbs_runtime::block_on;
|
2019-07-03 12:39:13 +00:00
|
|
|
|
2021-07-09 13:29:42 +00:00
|
|
|
use super::BackupReader;
|
|
|
|
|
2019-07-03 12:39:13 +00:00
|
|
|
/// Read chunks from remote host using ``BackupReader``
|
2020-07-07 05:34:58 +00:00
|
|
|
#[derive(Clone)]
|
2019-07-03 12:39:13 +00:00
|
|
|
pub struct RemoteChunkReader {
|
|
|
|
client: Arc<BackupReader>,
|
|
|
|
crypt_config: Option<Arc<CryptConfig>>,
|
2020-08-10 11:25:07 +00:00
|
|
|
crypt_mode: CryptMode,
|
2020-09-22 07:51:19 +00:00
|
|
|
cache_hint: Arc<HashMap<[u8; 32], usize>>,
|
2020-07-07 05:34:58 +00:00
|
|
|
cache: Arc<Mutex<HashMap<[u8; 32], Vec<u8>>>>,
|
2019-07-03 12:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RemoteChunkReader {
|
2019-07-05 08:42:46 +00:00
|
|
|
/// Create a new instance.
|
|
|
|
///
|
|
|
|
/// Chunks listed in ``cache_hint`` are cached and kept in RAM.
|
|
|
|
pub fn new(
|
|
|
|
client: Arc<BackupReader>,
|
|
|
|
crypt_config: Option<Arc<CryptConfig>>,
|
2020-08-10 11:25:07 +00:00
|
|
|
crypt_mode: CryptMode,
|
2019-07-05 08:42:46 +00:00
|
|
|
cache_hint: HashMap<[u8; 32], usize>,
|
|
|
|
) -> Self {
|
2020-06-12 09:01:04 +00:00
|
|
|
Self {
|
|
|
|
client,
|
|
|
|
crypt_config,
|
2020-08-10 11:25:07 +00:00
|
|
|
crypt_mode,
|
2020-09-22 07:51:19 +00:00
|
|
|
cache_hint: Arc::new(cache_hint),
|
2020-07-07 05:34:58 +00:00
|
|
|
cache: Arc::new(Mutex::new(HashMap::new())),
|
2020-06-12 09:01:04 +00:00
|
|
|
}
|
2019-07-03 12:39:13 +00:00
|
|
|
}
|
2020-06-12 09:38:21 +00:00
|
|
|
|
2020-08-03 12:10:44 +00:00
|
|
|
/// Downloads raw chunk. This only verifies the (untrusted) CRC32, use
|
|
|
|
/// DataBlob::verify_unencrypted or DataBlob::decode before storing/processing further.
|
2020-07-03 05:36:23 +00:00
|
|
|
pub async fn read_raw_chunk(&self, digest: &[u8; 32]) -> Result<DataBlob, Error> {
|
2020-06-12 09:38:21 +00:00
|
|
|
let mut chunk_data = Vec::with_capacity(4 * 1024 * 1024);
|
|
|
|
|
|
|
|
self.client
|
|
|
|
.download_chunk(&digest, &mut chunk_data)
|
|
|
|
.await?;
|
|
|
|
|
2020-07-28 08:23:16 +00:00
|
|
|
let chunk = DataBlob::load_from_reader(&mut &chunk_data[..])?;
|
2020-06-12 09:38:21 +00:00
|
|
|
|
2020-08-10 11:25:07 +00:00
|
|
|
match self.crypt_mode {
|
|
|
|
CryptMode::Encrypt => {
|
|
|
|
match chunk.crypt_mode()? {
|
|
|
|
CryptMode::Encrypt => Ok(chunk),
|
|
|
|
CryptMode::SignOnly | CryptMode::None => bail!("Index and chunk CryptMode don't match."),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
CryptMode::SignOnly | CryptMode::None => {
|
|
|
|
match chunk.crypt_mode()? {
|
|
|
|
CryptMode::Encrypt => bail!("Index and chunk CryptMode don't match."),
|
|
|
|
CryptMode::SignOnly | CryptMode::None => Ok(chunk),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2020-06-12 09:38:21 +00:00
|
|
|
}
|
2019-07-03 12:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ReadChunk for RemoteChunkReader {
|
2020-07-03 05:36:23 +00:00
|
|
|
fn read_raw_chunk(&self, digest: &[u8; 32]) -> Result<DataBlob, Error> {
|
2020-06-18 11:55:22 +00:00
|
|
|
block_on(Self::read_raw_chunk(self, digest))
|
2020-01-02 12:29:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 05:36:23 +00:00
|
|
|
fn read_chunk(&self, digest: &[u8; 32]) -> Result<Vec<u8>, Error> {
|
|
|
|
if let Some(raw_data) = (*self.cache.lock().unwrap()).get(digest) {
|
2020-01-02 12:29:10 +00:00
|
|
|
return Ok(raw_data.to_vec());
|
|
|
|
}
|
2019-10-06 08:31:06 +00:00
|
|
|
|
2020-06-12 09:38:21 +00:00
|
|
|
let chunk = ReadChunk::read_raw_chunk(self, digest)?;
|
2019-07-03 12:39:13 +00:00
|
|
|
|
2020-08-03 12:10:43 +00:00
|
|
|
let raw_data = chunk.decode(self.crypt_config.as_ref().map(Arc::as_ref), Some(digest))?;
|
2020-01-02 12:29:10 +00:00
|
|
|
|
|
|
|
let use_cache = self.cache_hint.contains_key(digest);
|
2019-07-05 08:42:46 +00:00
|
|
|
if use_cache {
|
2020-07-03 05:36:23 +00:00
|
|
|
(*self.cache.lock().unwrap()).insert(*digest, raw_data.to_vec());
|
2019-07-05 08:42:46 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 12:39:13 +00:00
|
|
|
Ok(raw_data)
|
|
|
|
}
|
|
|
|
}
|
2020-06-12 09:38:21 +00:00
|
|
|
|
|
|
|
impl AsyncReadChunk for RemoteChunkReader {
|
|
|
|
fn read_raw_chunk<'a>(
|
2020-07-03 05:36:23 +00:00
|
|
|
&'a self,
|
2020-06-12 09:38:21 +00:00
|
|
|
digest: &'a [u8; 32],
|
|
|
|
) -> Pin<Box<dyn Future<Output = Result<DataBlob, Error>> + Send + 'a>> {
|
|
|
|
Box::pin(Self::read_raw_chunk(self, digest))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_chunk<'a>(
|
2020-07-03 05:36:23 +00:00
|
|
|
&'a self,
|
2020-06-12 09:38:21 +00:00
|
|
|
digest: &'a [u8; 32],
|
|
|
|
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, Error>> + Send + 'a>> {
|
|
|
|
Box::pin(async move {
|
2020-07-03 05:36:23 +00:00
|
|
|
if let Some(raw_data) = (*self.cache.lock().unwrap()).get(digest) {
|
2020-06-12 09:38:21 +00:00
|
|
|
return Ok(raw_data.to_vec());
|
|
|
|
}
|
|
|
|
|
|
|
|
let chunk = Self::read_raw_chunk(self, digest).await?;
|
|
|
|
|
2020-08-03 12:10:43 +00:00
|
|
|
let raw_data = chunk.decode(self.crypt_config.as_ref().map(Arc::as_ref), Some(digest))?;
|
2020-06-12 09:38:21 +00:00
|
|
|
|
|
|
|
let use_cache = self.cache_hint.contains_key(digest);
|
|
|
|
if use_cache {
|
2020-07-03 05:36:23 +00:00
|
|
|
(*self.cache.lock().unwrap()).insert(*digest, raw_data.to_vec());
|
2020-06-12 09:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(raw_data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|