From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 707E1708B7; Mon, 7 Jun 2021 17:35:56 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id C051112F0C; Mon, 7 Jun 2021 17:35:55 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 71FF612D66; Mon, 7 Jun 2021 17:35:50 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 407A442DEA; Mon, 7 Jun 2021 17:35:50 +0200 (CEST) From: Stefan Reiter To: pve-devel@lists.proxmox.com, pbs-devel@lists.proxmox.com Date: Mon, 7 Jun 2021 17:35:26 +0200 Message-Id: <20210607153532.2522267-4-s.reiter@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210607153532.2522267-1-s.reiter@proxmox.com> References: <20210607153532.2522267-1-s.reiter@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.864 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [PATCH v2 proxmox-backup 3/9] backup: add CachedChunkReader utilizing AsyncLruCache X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 07 Jun 2021 15:35:56 -0000 Provides a fast arbitrary read implementation with full async and concurrency support. Signed-off-by: Stefan Reiter --- This is technically all that's needed for proxmox-backup-qemu to build and function as intended, but I decided to also use this IMHO cleaner implementation to replace the AsyncIndexReader with the following patches. v2: * drop ChunkCache type alias, not necessary and looked weird, since it couldn't be constructed directly * add comment to other unwrap in read_at src/backup.rs | 3 ++ src/backup/cached_chunk_reader.rs | 89 +++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/backup/cached_chunk_reader.rs diff --git a/src/backup.rs b/src/backup.rs index ae937be0..5e1147b4 100644 --- a/src/backup.rs +++ b/src/backup.rs @@ -259,3 +259,6 @@ pub use catalog_shell::*; mod async_index_reader; pub use async_index_reader::*; + +mod cached_chunk_reader; +pub use cached_chunk_reader::*; diff --git a/src/backup/cached_chunk_reader.rs b/src/backup/cached_chunk_reader.rs new file mode 100644 index 00000000..ff476e37 --- /dev/null +++ b/src/backup/cached_chunk_reader.rs @@ -0,0 +1,89 @@ +//! An async and concurrency safe data reader backed by a local LRU cache. + +use anyhow::Error; + +use std::future::Future; +use std::sync::Arc; + +use crate::backup::{AsyncReadChunk, IndexFile}; +use crate::tools::async_lru_cache::{AsyncCacher, AsyncLruCache}; + +struct AsyncChunkCacher { + reader: Arc, +} + +impl AsyncCacher<[u8; 32], Arc>> + for AsyncChunkCacher +{ + fn fetch( + &self, + key: [u8; 32], + ) -> Box>>, Error>> + Send> { + let reader = Arc::clone(&self.reader); + Box::new(async move { + AsyncReadChunk::read_chunk(reader.as_ref(), &key) + .await + .map(|x| Some(Arc::new(x))) + }) + } +} + +/// Allows arbitrary data reads from an Index via an AsyncReadChunk implementation, using an LRU +/// cache internally to cache chunks and provide support for multiple concurrent reads (potentially +/// to the same chunk). +pub struct CachedChunkReader { + cache: Arc>>>, + cacher: AsyncChunkCacher, + index: I, +} + +impl CachedChunkReader { + /// Create a new reader with a local LRU cache containing 'capacity' chunks. + pub fn new(reader: R, index: I, capacity: usize) -> Self { + let cache = Arc::new(AsyncLruCache::new(capacity)); + Self::new_with_cache(reader, index, cache) + } + + /// Create a new reader with a custom LRU cache. Use this to share a cache between multiple + /// readers. + pub fn new_with_cache( + reader: R, + index: I, + cache: Arc>>>, + ) -> Self { + Self { + cache, + cacher: AsyncChunkCacher { + reader: Arc::new(reader), + }, + index, + } + } + + /// Read data at a given byte offset into a variable size buffer. Returns the amount of bytes + /// read, which will always be the size of the buffer except when reaching EOF. + pub async fn read_at(&self, buf: &mut [u8], offset: u64) -> Result { + let size = buf.len(); + let mut read: usize = 0; + while read < size { + let cur_offset = offset + read as u64; + if let Some(chunk) = self.index.chunk_from_offset(cur_offset) { + // chunk indices retrieved from chunk_from_offset always resolve to Some(_) + let info = self.index.chunk_info(chunk.0).unwrap(); + + // will never be None, see AsyncChunkCacher + let data = self.cache.access(info.digest, &self.cacher).await?.unwrap(); + + let want_bytes = ((info.range.end - cur_offset) as usize).min(size - read); + let slice = &mut buf[read..(read + want_bytes)]; + let intra_chunk = chunk.1 as usize; + slice.copy_from_slice(&data[intra_chunk..(intra_chunk + want_bytes)]); + read += want_bytes; + } else { + // EOF + break; + } + } + Ok(read) + } +} -- 2.30.2