public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox-backup] fix #3359: fix blocking writes in async code during pxar create
@ 2021-03-23 10:12 Dominik Csapak
  0 siblings, 0 replies; 4+ messages in thread
From: Dominik Csapak @ 2021-03-23 10:12 UTC (permalink / raw)
  To: pbs-devel

in commit `asyncify pxar create_archive`, we changed from a
separate thread for creating a pxar to using async code, but the
StdChannelWriter used for both pxar and catalog can block, which
may block the tokio runtime for single (and probably dual) core
environments

this patch adds a wrapper struct for any writer that implements
'std::io::Write' and wraps the write calls with 'block_in_place'
so that if called in a tokio runtime, it knows that this code
potentially blocks

Fixes: 6afb60abf557 ("asyncify pxar create_archive")

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
alternatives would be:
* asyncify complete CatalogWriter and PxarBackupStream
* add 'block_in_place' in 'create_archive'
* add a threaded non-async variant of create_archive

 src/bin/proxmox-backup-client.rs  | 12 ++++++++----
 src/client/pxar_backup_stream.rs  | 10 +++++++---
 src/tools.rs                      |  3 +++
 src/tools/tokio_writer_adapter.rs | 26 ++++++++++++++++++++++++++
 4 files changed, 44 insertions(+), 7 deletions(-)
 create mode 100644 src/tools/tokio_writer_adapter.rs

diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs
index 5aae0873..45b26c7a 100644
--- a/src/bin/proxmox-backup-client.rs
+++ b/src/bin/proxmox-backup-client.rs
@@ -32,7 +32,11 @@ use proxmox::{
 };
 use pxar::accessor::{MaybeReady, ReadAt, ReadAtOperation};
 
-use proxmox_backup::tools;
+use proxmox_backup::tools::{
+    self,
+    StdChannelWriter,
+    TokioWriterAdapter,
+};
 use proxmox_backup::api2::types::*;
 use proxmox_backup::api2::version;
 use proxmox_backup::client::*;
@@ -162,7 +166,7 @@ async fn backup_directory<P: AsRef<Path>>(
     dir_path: P,
     archive_name: &str,
     chunk_size: Option<usize>,
-    catalog: Arc<Mutex<CatalogWriter<crate::tools::StdChannelWriter>>>,
+    catalog: Arc<Mutex<CatalogWriter<TokioWriterAdapter<StdChannelWriter>>>>,
     pxar_create_options: proxmox_backup::pxar::PxarCreateOptions,
     upload_options: UploadOptions,
 ) -> Result<BackupStats, Error> {
@@ -460,7 +464,7 @@ async fn start_garbage_collection(param: Value) -> Result<Value, Error> {
 }
 
 struct CatalogUploadResult {
-    catalog_writer: Arc<Mutex<CatalogWriter<crate::tools::StdChannelWriter>>>,
+    catalog_writer: Arc<Mutex<CatalogWriter<TokioWriterAdapter<StdChannelWriter>>>>,
     result: tokio::sync::oneshot::Receiver<Result<BackupStats, Error>>,
 }
 
@@ -473,7 +477,7 @@ fn spawn_catalog_upload(
     let catalog_chunk_size = 512*1024;
     let catalog_chunk_stream = ChunkStream::new(catalog_stream, Some(catalog_chunk_size));
 
-    let catalog_writer = Arc::new(Mutex::new(CatalogWriter::new(crate::tools::StdChannelWriter::new(catalog_tx))?));
+    let catalog_writer = Arc::new(Mutex::new(CatalogWriter::new(TokioWriterAdapter::new(StdChannelWriter::new(catalog_tx)))?));
 
     let (catalog_result_tx, catalog_result_rx) = tokio::sync::oneshot::channel();
 
diff --git a/src/client/pxar_backup_stream.rs b/src/client/pxar_backup_stream.rs
index b57061a3..035f735c 100644
--- a/src/client/pxar_backup_stream.rs
+++ b/src/client/pxar_backup_stream.rs
@@ -13,6 +13,10 @@ use nix::fcntl::OFlag;
 use nix::sys::stat::Mode;
 
 use crate::backup::CatalogWriter;
+use crate::tools::{
+    StdChannelWriter,
+    TokioWriterAdapter,
+};
 
 /// Stream implementation to encode and upload .pxar archives.
 ///
@@ -45,10 +49,10 @@ impl PxarBackupStream {
         let error = Arc::new(Mutex::new(None));
         let error2 = Arc::clone(&error);
         let handler = async move {
-            let writer = std::io::BufWriter::with_capacity(
+            let writer = TokioWriterAdapter::new(std::io::BufWriter::with_capacity(
                 buffer_size,
-                crate::tools::StdChannelWriter::new(tx),
-            );
+                StdChannelWriter::new(tx),
+            ));
 
             let verbose = options.verbose;
 
diff --git a/src/tools.rs b/src/tools.rs
index cc782da2..7e3bff7b 100644
--- a/src/tools.rs
+++ b/src/tools.rs
@@ -57,6 +57,9 @@ pub use async_channel_writer::AsyncChannelWriter;
 mod std_channel_writer;
 pub use std_channel_writer::StdChannelWriter;
 
+mod tokio_writer_adapter;
+pub use tokio_writer_adapter::TokioWriterAdapter;
+
 mod process_locker;
 pub use process_locker::{ProcessLocker, ProcessLockExclusiveGuard, ProcessLockSharedGuard};
 
diff --git a/src/tools/tokio_writer_adapter.rs b/src/tools/tokio_writer_adapter.rs
new file mode 100644
index 00000000..7b7f5dcf
--- /dev/null
+++ b/src/tools/tokio_writer_adapter.rs
@@ -0,0 +1,26 @@
+use std::io::Write;
+
+use tokio::task::block_in_place;
+
+/// Wrapper around a writer which implements Write
+///
+/// wraps each write with a 'block_in_place' so that
+/// any (blocking) writer can be safely used in async context in a
+/// tokio runtime
+pub struct TokioWriterAdapter<W: Write>(W);
+
+impl<W: Write> TokioWriterAdapter<W> {
+    pub fn new(writer: W) -> Self {
+        Self(writer)
+    }
+}
+
+impl<W: Write> Write for TokioWriterAdapter<W> {
+    fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
+        block_in_place(|| self.0.write(buf))
+    }
+
+    fn flush(&mut self) -> Result<(), std::io::Error> {
+        block_in_place(|| self.0.flush())
+    }
+}
-- 
2.20.1





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [pbs-devel] [PATCH proxmox-backup] fix #3359: fix blocking writes in async code during pxar create
@ 2021-03-24  7:47 Dietmar Maurer
  0 siblings, 0 replies; 4+ messages in thread
From: Dietmar Maurer @ 2021-03-24  7:47 UTC (permalink / raw)
  To: Dominik Csapak, Proxmox Backup Server development discussion,
	Wolfgang Bumiller, Stefan Reiter


> On 03/24/2021 8:43 AM Dominik Csapak <d.csapak@proxmox.com> wrote:
> 
>  
> On 3/24/21 08:17, Dietmar Maurer wrote:
> >> Fixes: 6afb60abf557 ("asyncify pxar create_archive")
> > 
> > I am not sure, but maybe 6afb60abf557 is also the cause for
> > the performance regression?
> > 
> > (reported at https://bugzilla.proxmox.com/show_bug.cgi?id=3174)
> > 
> 
> i mean not the perfect benchmark, but here on a relatively
> empty vm (~2GB root disk) with 4 cores + 4GiB Memory
> 
> there is no measurable difference between before
> that commit and after

Maybe you test on SDD?

Please can you also test on a slow disk with large seek times?

Also, please drop caches before the test ...




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [pbs-devel] [PATCH proxmox-backup] fix #3359: fix blocking writes in async code during pxar create
  2021-03-24  7:17 Dietmar Maurer
@ 2021-03-24  7:43 ` Dominik Csapak
  0 siblings, 0 replies; 4+ messages in thread
From: Dominik Csapak @ 2021-03-24  7:43 UTC (permalink / raw)
  To: Dietmar Maurer, Proxmox Backup Server development discussion,
	Wolfgang Bumiller, Stefan Reiter

On 3/24/21 08:17, Dietmar Maurer wrote:
>> Fixes: 6afb60abf557 ("asyncify pxar create_archive")
> 
> I am not sure, but maybe 6afb60abf557 is also the cause for
> the performance regression?
> 
> (reported at https://bugzilla.proxmox.com/show_bug.cgi?id=3174)
> 

i mean not the perfect benchmark, but here on a relatively
empty vm (~2GB root disk) with 4 cores + 4GiB Memory

there is no measurable difference between before
that commit and after

(a backup of / takes ~15s in both cases)




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [pbs-devel] [PATCH proxmox-backup] fix #3359: fix blocking writes in async code during pxar create
@ 2021-03-24  7:17 Dietmar Maurer
  2021-03-24  7:43 ` Dominik Csapak
  0 siblings, 1 reply; 4+ messages in thread
From: Dietmar Maurer @ 2021-03-24  7:17 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Dominik Csapak,
	Wolfgang Bumiller, Stefan Reiter

> Fixes: 6afb60abf557 ("asyncify pxar create_archive")

I am not sure, but maybe 6afb60abf557 is also the cause for 
the performance regression?

(reported at https://bugzilla.proxmox.com/show_bug.cgi?id=3174)




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-03-24  7:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-23 10:12 [pbs-devel] [PATCH proxmox-backup] fix #3359: fix blocking writes in async code during pxar create Dominik Csapak
2021-03-24  7:17 Dietmar Maurer
2021-03-24  7:43 ` Dominik Csapak
2021-03-24  7:47 Dietmar Maurer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal