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 9F0A365323 for ; Wed, 22 Jul 2020 15:57:07 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 8121C210B9 for ; Wed, 22 Jul 2020 15:56:37 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (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 029622108C for ; Wed, 22 Jul 2020 15:56:35 +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 C132A4332B for ; Wed, 22 Jul 2020 15:56:34 +0200 (CEST) From: Stefan Reiter To: pbs-devel@lists.proxmox.com Date: Wed, 22 Jul 2020 15:56:24 +0200 Message-Id: <20200722135625.23653-5-s.reiter@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200722135625.23653-1-s.reiter@proxmox.com> References: <20200722135625.23653-1-s.reiter@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.018 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust 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: [pbs-devel] [PATCH v2 backup-qemu 4/5] use AsyncIndexReader for read_image_at X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 22 Jul 2020 13:57:07 -0000 Replaces the removed BufferedFixedReader and makes the API fully async (not block_in_place or block_on which could break with many requests at once). A tokio::sync::Mutex needs to be used for exclusive access, since a regular one would not work with async/await calls. Signed-off-by: Stefan Reiter --- src/restore.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/restore.rs b/src/restore.rs index 3e37066..2be0295 100644 --- a/src/restore.rs +++ b/src/restore.rs @@ -1,13 +1,14 @@ use std::sync::{Arc, Mutex}; use std::collections::HashMap; -use std::io::{Read, Seek, SeekFrom}; +use std::io::SeekFrom; use std::convert::TryInto; use anyhow::{format_err, bail, Error}; use once_cell::sync::OnceCell; use tokio::runtime::Runtime; +use tokio::prelude::*; -use proxmox_backup::tools::runtime::{get_runtime_with_builder, block_in_place}; +use proxmox_backup::tools::runtime::get_runtime_with_builder; use proxmox_backup::backup::*; use proxmox_backup::client::{HttpClient, HttpClientOptions, BackupReader, RemoteChunkReader}; @@ -16,7 +17,7 @@ use crate::registry::Registry; use crate::capi_types::DataPointer; struct ImageAccessInfo { - reader: Arc>>, + reader: Arc>>, _archive_name: String, archive_size: u64, } @@ -231,12 +232,12 @@ impl RestoreTask { let index = client.download_fixed_index(&manifest, &archive_name).await?; let archive_size = index.index_bytes(); - let reader = BufferedFixedReader::new(index, chunk_reader); + let reader = AsyncIndexReader::new(index, chunk_reader); let info = ImageAccessInfo { archive_size, _archive_name: archive_name, /// useful to debug - reader: Arc::new(Mutex::new(reader)), + reader: Arc::new(tokio::sync::Mutex::new(reader)), }; (*self.image_registry.lock().unwrap()).register(info) @@ -260,12 +261,10 @@ impl RestoreTask { bail!("read index {} out of bounds {}", offset, image_size); } - let bytes = block_in_place(|| { - let mut reader = reader.lock().unwrap(); - reader.seek(SeekFrom::Start(offset))?; - let buf: &mut [u8] = unsafe { std::slice::from_raw_parts_mut(data.0 as *mut u8, size as usize)}; - reader.read(buf) - })?; + let mut reader = reader.lock().await; + reader.seek(SeekFrom::Start(offset)).await?; + let buf: &mut [u8] = unsafe { std::slice::from_raw_parts_mut(data.0 as *mut u8, size as usize)}; + let bytes = reader.read(buf).await?; Ok(bytes.try_into()?) } -- 2.20.1