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 641B074B2B; Wed, 2 Jun 2021 16:39:19 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 40CB0D165; Wed, 2 Jun 2021 16:38:49 +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 0FEF9C9D0; Wed, 2 Jun 2021 16:38:45 +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 DDF68466D8; Wed, 2 Jun 2021 16:38:44 +0200 (CEST) From: Stefan Reiter To: pve-devel@lists.proxmox.com, pbs-devel@lists.proxmox.com Date: Wed, 2 Jun 2021 16:38:33 +0200 Message-Id: <20210602143833.4423-10-s.reiter@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210602143833.4423-1-s.reiter@proxmox.com> References: <20210602143833.4423-1-s.reiter@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.046 Adjusted score from AWL reputation of From: address 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 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [restore.rs] Subject: [pve-devel] [PATCH proxmox-backup-qemu 9/9] access: use CachedChunkReader 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: Wed, 02 Jun 2021 14:39:19 -0000 Use the new CachedChunkReader with the shared_cache implementation to provide a concurrency-safe async way of accessing data. This provides two benefits: * uses a shared LRU cache, which is very helpful for random-access like during a live-restore * does away with the global Mutex in read_image_at, providing real concurrency without lock contention Signed-off-by: Stefan Reiter --- src/restore.rs | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/src/restore.rs b/src/restore.rs index 0790d7f..33959d9 100644 --- a/src/restore.rs +++ b/src/restore.rs @@ -1,10 +1,8 @@ use std::sync::{Arc, Mutex}; -use std::io::SeekFrom; use std::convert::TryInto; use anyhow::{format_err, bail, Error}; use once_cell::sync::OnceCell; -use tokio::io::{AsyncReadExt, AsyncSeekExt}; use tokio::runtime::Runtime; use proxmox_backup::tools::runtime::get_runtime_with_builder; @@ -14,9 +12,10 @@ use proxmox_backup::client::{HttpClient, HttpClientOptions, BackupReader, Remote use super::BackupSetup; use crate::registry::Registry; use crate::capi_types::DataPointer; +use crate::shared_cache::get_shared_chunk_cache; struct ImageAccessInfo { - reader: Arc>>, + reader: Arc>, _archive_name: String, archive_size: u64, } @@ -229,12 +228,13 @@ impl RestoreTask { most_used, ); - let reader = AsyncIndexReader::new(index, chunk_reader); + let cache = get_shared_chunk_cache(); + let reader = Arc::new(CachedChunkReader::new_with_cache(chunk_reader, index, cache)); let info = ImageAccessInfo { archive_size, _archive_name: archive_name, /// useful to debug - reader: Arc::new(tokio::sync::Mutex::new(reader)), + reader, }; (*self.image_registry.lock().unwrap()).register(info) @@ -258,23 +258,9 @@ impl RestoreTask { bail!("read index {} out of bounds {}", offset, image_size); } - let mut reader = reader.lock().await; - - let buf: &mut [u8] = unsafe { std::slice::from_raw_parts_mut(data.0 as *mut u8, size as usize)}; - let mut read = 0; - - while read < size { - reader.seek(SeekFrom::Start(offset + read)).await?; - let bytes = reader.read(&mut buf[read as usize..]).await?; - - if bytes == 0 { - // EOF - break; - } - - read += bytes as u64; - } - + let buf: &mut [u8] = + unsafe { std::slice::from_raw_parts_mut(data.0 as *mut u8, size as usize) }; + let read = reader.read_at(buf, offset).await?; Ok(read.try_into()?) } } -- 2.30.2