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 6267B708AD; 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 A79E012F0B; 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 CCB2312D5F; Mon, 7 Jun 2021 17:35:46 +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 9E2E444618; Mon, 7 Jun 2021 17:35:46 +0200 (CEST) From: Stefan Reiter To: pve-devel@lists.proxmox.com, pbs-devel@lists.proxmox.com Date: Mon, 7 Jun 2021 17:35:32 +0200 Message-Id: <20210607153532.2522267-10-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.872 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-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: Mon, 07 Jun 2021 15:35:56 -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