public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores
@ 2025-07-19 12:49 Christian Ebner
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox v9 1/3] pbs-api-types: extend datastore config by backend config enum Christian Ebner
                   ` (51 more replies)
  0 siblings, 52 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:49 UTC (permalink / raw)
  To: pbs-devel

Disclaimer: These patches are still in an experimental state and not
intended for production use.

This patch series aims to add S3 compatible object stores as storage
backend for PBS datastores. A PBS local cache store using the regular
datastore layout is used for faster operation, bypassing requests to
the S3 api when possible. Further, the local cache store allows to
keep frequently used chunks and is used to avoid expensive metadata
updates on the object store, e.g. by using local marker file during
garbage collection.

Backups are created by upload chunks to the corresponding S3 bucket,
while keeping the index files in the local cache store, on backup
finish, the snapshot metadata are persisted to the S3 storage backend.

Snapshot restores read chunks preferably from the local cache store,
downloading and insterting them if not present from the S3 object
store. Listing and snapsoht metadata operation currently rely soly on
the local cache store.

Currently chunks use a 1:1 mapping to S3 objects. An advanced packing
mechanism for chunks to significantly reduce the number of api
requests and therefore be more cost effective will be implemented as
followup patches.

Most notably changes since version 8 of the patches (thanks @Lukas for
code review):
- Moved s3 refresh button to the datastore content, and made it less
  visible by placing it into a 'More' dropdown.
- Added basic unit tests for s3 object key helpers
- Extend missing public function and type documentation
- Use pbs_buildcfg::configdir macro to build config paths
- Mostly refactoring based on feedback, please refer to the per-patch
  changelog for details.
- Fixes typos in docs (thanks @Maximiliano)

Most notably changes since version 7 of the patches (thanks @Thomas
and @Lukas for feedback, testing and debugging):
- Improve self-signed certificate fingerprint check, verify valid
  expected fingerprint is passed to client on instantiation.
- Rename previously missed host to endpoint is s3 client selector
- Use more specific `S3 Client ID` over ambiguous `Unique Identifier`
- Implement missing cli commands for s3 client manipulation
- Add in-use marker to s3 object stores to avoid accitental reuse of
  object stores which are already used as datastore by another
  instance, adding also flags to overwrite.
- Automatically perform an s3-refresh when recreating a datastore,
  pre-populating the contents without further user interaction.
- Add documentation
- Fix formatting issue in proxmox-s3-client

Most notably changes since version 6 of the patches (thanks @Thomas
for feedback):
- Reworked uri encoding logic, instead of doing this in the
  S3ObjectKey, perform this in the build_uri helper used by all
  client api requests.
- Add cache-size optional parameter to datastore backend config,
  allows to define the local datastore LRU cache capacity.
- Increase s3 client timeout, as otherwise delete objects operations
  on Cloudflare R2 would run into a timeout error.
- Also upload client log, previously not uploaded to s3 backend.
- Add missing documentation to some pub types in the response reader
- Use s3 object key generation helper for index file upload, which
  fixes the missing key prefix.
- Add basic regression tests for uri encoder and decoder helper
  functions.
- Include some baseline performance tests for garbage collection as
  well as chunk up-/download when caching.

Most notably changes since version 5 of the patches (thanks @Thomas
for feedback):
- Move s3 client into its own, dedicated crate in the proxmox repo
- Factor out any directly PBS related code from the client
- Guard implementation behind feature cfg, so api types can be used
  independently
- Add basic example and extend on crate documentation

Most notably changes since version 4 of the patches:
- Fix race between S3 backend upload and local cache store insert,
  avoiding possibly chunk loss for concurrent backups.
- Use the local datastore cache also for local chunk reader instances
- Fallback to fetching chunks from S3 backend if they should be cached
  but the local chunk file is missing or empty, instead of failing
- Rename chunks detected as corrupt also on the S3 object store
- Retry chunk uploads via put objects in case of errors.
- Add possibility to add rate limits for the s3 client put requests, as
  otherwise object stores can be overloaded.
- Allow for Cloudflare R2 compatible `auto` region, as otherwise AWS
  sign v4 request authentication will fail
- Use `Async` instead of `Sync` variant for the api handler of the
  s3-refresh command, as otherwise this fails.
- Take into account that some type folders might not be present when
  performing an s3-refresh.
- Use `Local` instead of `Regular` to refer to normal datastores in the
  creation window.

Most notably changes since version 3 of the patches:
- Rebased onto current master, fixed incompatibilities with upgraded
  dependencies
- Added method to uri decode s3 object keys, as they are required in
  order to download contents to a local store
- Added api endpoint to allow resyncing of the datastore contents to
  the local cache store, introducing a new maintenance mode s3-refresh
  to guarantee consistency.

Most notably changes since RFC version 2 of the patches (thanks
@Lukas for feedback):
- Extend S3 client implementation to also support path style bucket
  addressing.
- Keep bucket name as config option for the datastore, allowing more
  flexible reuse of a configured S3 client.
- Use the datastore name as additional object key prefix to allow for
  multiple datastores on the same bucket.
- Allow bucket and region templating in S3 endpoint, making this more
  flexible with respect to possible DNS records.
- Rework datastore create window to be less overloaded.
- Drop dead code in the S3 client implementation, since tagging and
  object copying is currently not required.
- Fix missing locking when deleting chunks from s3 store during
  garbage collection, avoiding possible chunk loss for concurrent
  backups.
- Remove chunks from LRU cache when deleting chunks during garbage
  collection, avoiding possible chunk loss for concurrent backups.
- Add dedicated types for object prefix and relative s3 key paths to
  avoid misuse.
- Use more fitting icon for S3 client.

Link to the bugtracker issue:
https://bugzilla.proxmox.com/show_bug.cgi?id=2943

Steps to setup a local S3 object store using RADOS gateway or MinIO
can be found at (internal only, external users might use the steps
outlined in the cover letter and comments of RFC version 2):
https://wiki.intra.proxmox.com/PBS_Setup_S3_Object_Store

proxmox:

Christian Ebner (3):
  pbs-api-types: extend datastore config by backend config enum
  pbs-api-types: maintenance: add new maintenance mode S3 refresh
  s3 client: wrap upload with retry into dedicated methods

 Cargo.toml                       |   1 +
 pbs-api-types/Cargo.toml         |   1 +
 pbs-api-types/debian/control     |   2 +
 pbs-api-types/src/datastore.rs   | 114 ++++++++++++++++++++++++++++++-
 pbs-api-types/src/maintenance.rs |   4 ++
 proxmox-s3-client/src/client.rs  |  32 ++++++++-
 6 files changed, 151 insertions(+), 3 deletions(-)


proxmox-backup:

Christian Ebner (46):
  datastore: add helpers for path/digest to s3 object key conversion
  config: introduce s3 object store client configuration
  api: config: implement endpoints to manipulate and list s3 configs
  api: datastore: check s3 backend bucket access on datastore create
  api/cli: add endpoint and command to check s3 client connection
  datastore: allow to get the backend for a datastore
  api: backup: store datastore backend in runtime environment
  api: backup: conditionally upload chunks to s3 object store backend
  api: backup: conditionally upload blobs to s3 object store backend
  api: backup: conditionally upload indices to s3 object store backend
  api: backup: conditionally upload manifest to s3 object store backend
  api: datastore: conditionally upload client log to s3 backend
  sync: pull: conditionally upload content to s3 backend
  api: reader: fetch chunks based on datastore backend
  datastore: local chunk reader: read chunks based on backend
  verify worker: add datastore backed to verify worker
  verify: implement chunk verification for stores with s3 backend
  datastore: create namespace marker in s3 backend
  datastore: create/delete protected marker file on s3 storage backend
  datastore: prune groups/snapshots from s3 object store backend
  datastore: get and set owner for s3 store backend
  datastore: implement garbage collection for s3 backend
  ui: add datastore type selector and reorganize component layout
  ui: add s3 client edit window for configuration create/edit
  ui: add s3 client view for configuration
  ui: expose the s3 client view in the navigation tree
  ui: add s3 client selector and bucket field for s3 backend setup
  tools: lru cache: add removed callback for evicted cache nodes
  tools: async lru cache: implement insert, remove and contains methods
  datastore: add local datastore cache for network attached storages
  api: backup: use local datastore cache on s3 backend chunk upload
  api: reader: use local datastore cache on s3 backend chunk fetching
  datastore: local chunk reader: get cached chunk from local cache store
  backup writer: refactor parameters into backup writer options struct
  api: backup: add no-cache flag to bypass local datastore cache
  api/datastore: implement refresh endpoint for stores with s3 backend
  cli: add dedicated subcommand for datastore s3 refresh
  ui: render s3 refresh as valid maintenance type and task description
  ui: expose s3 refresh button for datastores backed by object store
  datastore: conditionally upload atime marker chunk to s3 backend
  bin: implement client subcommands for s3 configuration manipulation
  bin: expose reuse-datastore flag for proxmox-backup-manager
  datastore: mark store as in-use by setting marker on s3 backend
  datastore: run s3-refresh when reusing a datastore with s3 backend
  api/ui: add flag to allow overwriting in-use marker for s3 backend
  docs: Add section describing how to setup s3 backed datastore

 Cargo.toml                                    |   2 +
 docs/storage.rst                              |  68 ++
 examples/upload-speed.rs                      |  17 +-
 pbs-client/src/backup_writer.rs               |  47 +-
 pbs-config/Cargo.toml                         |   1 +
 pbs-config/src/lib.rs                         |   1 +
 pbs-config/src/s3.rs                          |  89 +++
 pbs-datastore/Cargo.toml                      |   5 +
 pbs-datastore/src/backup_info.rs              |  63 +-
 pbs-datastore/src/cached_chunk_reader.rs      |   6 +-
 pbs-datastore/src/chunk_store.rs              |  29 +-
 pbs-datastore/src/datastore.rs                | 688 ++++++++++++++++--
 pbs-datastore/src/dynamic_index.rs            |   1 +
 pbs-datastore/src/lib.rs                      |   5 +
 pbs-datastore/src/local_chunk_reader.rs       |  61 +-
 .../src/local_datastore_lru_cache.rs          | 180 +++++
 pbs-datastore/src/s3.rs                       | 114 +++
 pbs-tools/src/async_lru_cache.rs              |  46 +-
 pbs-tools/src/lru_cache.rs                    |  42 +-
 proxmox-backup-client/src/benchmark.rs        |  17 +-
 proxmox-backup-client/src/main.rs             |  26 +-
 src/api2/admin/datastore.rs                   |  85 ++-
 src/api2/admin/mod.rs                         |   2 +
 src/api2/admin/s3.rs                          |  82 +++
 src/api2/backup/environment.rs                |  82 ++-
 src/api2/backup/mod.rs                        | 131 ++--
 src/api2/backup/upload_chunk.rs               | 114 ++-
 src/api2/config/datastore.rs                  | 147 +++-
 src/api2/config/mod.rs                        |   2 +
 src/api2/config/s3.rs                         | 310 ++++++++
 src/api2/node/disks/directory.rs              |   2 +-
 src/api2/node/disks/zfs.rs                    |   2 +-
 src/api2/reader/environment.rs                |  12 +-
 src/api2/reader/mod.rs                        |  62 +-
 src/backup/verify.rs                          | 134 +++-
 src/bin/proxmox-backup-manager.rs             |   1 +
 src/bin/proxmox_backup_manager/datastore.rs   |  42 ++
 src/bin/proxmox_backup_manager/mod.rs         |   2 +
 src/bin/proxmox_backup_manager/s3.rs          | 102 +++
 src/server/pull.rs                            |  68 +-
 src/server/push.rs                            |  19 +-
 src/server/verify_job.rs                      |   2 +-
 www/Makefile                                  |   3 +
 www/NavigationTree.js                         |   6 +
 www/Utils.js                                  |   4 +
 www/config/S3ClientView.js                    | 141 ++++
 www/datastore/Content.js                      |  48 ++
 www/form/S3ClientSelector.js                  |  33 +
 www/window/DataStoreEdit.js                   | 132 +++-
 www/window/MaintenanceOptions.js              |   6 +-
 www/window/S3ClientEdit.js                    | 148 ++++
 51 files changed, 3104 insertions(+), 328 deletions(-)
 create mode 100644 pbs-config/src/s3.rs
 create mode 100644 pbs-datastore/src/local_datastore_lru_cache.rs
 create mode 100644 pbs-datastore/src/s3.rs
 create mode 100644 src/api2/admin/s3.rs
 create mode 100644 src/api2/config/s3.rs
 create mode 100644 src/bin/proxmox_backup_manager/s3.rs
 create mode 100644 www/config/S3ClientView.js
 create mode 100644 www/form/S3ClientSelector.js
 create mode 100644 www/window/S3ClientEdit.js


Summary over all repositories:
  57 files changed, 3255 insertions(+), 331 deletions(-)

-- 
Generated by git-murpp 0.8.1


_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox v9 1/3] pbs-api-types: extend datastore config by backend config enum
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
@ 2025-07-19 12:49 ` Christian Ebner
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox v9 2/3] pbs-api-types: maintenance: add new maintenance mode S3 refresh Christian Ebner
                   ` (50 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:49 UTC (permalink / raw)
  To: pbs-devel

Allows to configure a backend config variant for a datastore on
creation. The current default `Filesystem` backend variant is
introduced to be compatible with existing storages. A new S3 backend
variant allows to create datastores backed by an S3 compatible object
store instead.

For S3 backends, the type, id of the corresponding S3 client
configuration as well as the bucket name are stored as property
string. A valid datastore backend configuration for S3 therefore
contains:
```
    ...
    backend bucket=<BUCKET_NAME>,client=<S3_CONFIG_ID>,type=s3
    ...
```

Further, a maximum cache size for the local store cache can be
assigned, for example to limit to max 1G:
```
    ...
    backend bucket=<BUCKET_NAME>,client=<S3_CONFIG_ID>,type=s3,max-cache-size=1G
    ...
```

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- no changes

 Cargo.toml                     |   1 +
 pbs-api-types/Cargo.toml       |   1 +
 pbs-api-types/debian/control   |   2 +
 pbs-api-types/src/datastore.rs | 113 ++++++++++++++++++++++++++++++++-
 4 files changed, 116 insertions(+), 1 deletion(-)

diff --git a/Cargo.toml b/Cargo.toml
index dbc389b9..fd7eba63 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -152,6 +152,7 @@ proxmox-product-config = { version = "1.0.0", path = "proxmox-product-config" }
 proxmox-config-digest = { version = "1.0.0", path = "proxmox-config-digest" }
 proxmox-rest-server = { version = "1.0.0", path = "proxmox-rest-server" }
 proxmox-router = { version = "3.2.2", path = "proxmox-router" }
+proxmox-s3-client = { version = "1.0.0", path = "proxmox-s3-client" }
 proxmox-schema = { version = "4.1.0", path = "proxmox-schema" }
 proxmox-section-config = { version = "3.1.0", path = "proxmox-section-config" }
 proxmox-sendmail = { version = "1.0.0", path = "proxmox-sendmail" }
diff --git a/pbs-api-types/Cargo.toml b/pbs-api-types/Cargo.toml
index e23bd137..4e8c72d9 100644
--- a/pbs-api-types/Cargo.toml
+++ b/pbs-api-types/Cargo.toml
@@ -20,6 +20,7 @@ proxmox-auth-api = { workspace = true, features = [ "api-types" ] }
 proxmox-apt-api-types.workspace = true
 proxmox-human-byte.workspace = true
 proxmox-lang.workspace=true
+proxmox-s3-client.workspace = true
 proxmox-schema = { workspace = true, features = [ "api-macro" ] }
 proxmox-serde.workspace = true
 proxmox-time.workspace = true
diff --git a/pbs-api-types/debian/control b/pbs-api-types/debian/control
index 8cf785dd..f4bca53f 100644
--- a/pbs-api-types/debian/control
+++ b/pbs-api-types/debian/control
@@ -15,6 +15,7 @@ Build-Depends-Arch: cargo:native <!nocheck>,
  librust-proxmox-auth-api-1+default-dev <!nocheck>,
  librust-proxmox-human-byte-1+default-dev <!nocheck>,
  librust-proxmox-lang-1+default-dev (>= 1.5-~~) <!nocheck>,
+ librust-proxmox-s3-client-1+default-dev <!nocheck>,
  librust-proxmox-schema-4+api-macro-dev (>= 4.1.0-~~) <!nocheck>,
  librust-proxmox-schema-4+default-dev (>= 4.1.0-~~) <!nocheck>,
  librust-proxmox-serde-1+default-dev <!nocheck>,
@@ -46,6 +47,7 @@ Depends:
  librust-proxmox-auth-api-1+default-dev,
  librust-proxmox-human-byte-1+default-dev,
  librust-proxmox-lang-1+default-dev (>= 1.5-~~),
+ librust-proxmox-s3-client-1+default-dev,
  librust-proxmox-schema-4+api-macro-dev (>= 4.1.0-~~),
  librust-proxmox-schema-4+default-dev (>= 4.1.0-~~),
  librust-proxmox-serde-1+default-dev,
diff --git a/pbs-api-types/src/datastore.rs b/pbs-api-types/src/datastore.rs
index 62b20460..7110a329 100644
--- a/pbs-api-types/src/datastore.rs
+++ b/pbs-api-types/src/datastore.rs
@@ -8,6 +8,7 @@ use anyhow::{bail, format_err, Error};
 use const_format::concatcp;
 use serde::{Deserialize, Serialize};
 
+use proxmox_human_byte::HumanByte;
 use proxmox_schema::{
     api, const_regex, ApiStringFormat, ApiType, ArraySchema, EnumEntry, IntegerSchema, ReturnType,
     Schema, StringSchema, Updater, UpdaterType,
@@ -286,6 +287,106 @@ pub const DATASTORE_TUNING_STRING_SCHEMA: Schema = StringSchema::new("Datastore
     ))
     .schema();
 
+#[api]
+#[derive(Copy, Clone, Default, Deserialize, Serialize, Updater, PartialEq)]
+#[serde(rename_all = "kebab-case")]
+/// Datastore backend type
+pub enum DatastoreBackendType {
+    /// Local filesystem
+    #[default]
+    Filesystem,
+    /// S3 object store
+    S3,
+}
+serde_plain::derive_display_from_serialize!(DatastoreBackendType);
+serde_plain::derive_fromstr_from_deserialize!(DatastoreBackendType);
+
+#[api(
+    properties: {
+        type: {
+            type: DatastoreBackendType,
+            optional: true,
+        },
+        client: {
+            schema: proxmox_s3_client::S3_CLIENT_ID_SCHEMA,
+            optional: true,
+        },
+        bucket: {
+            schema: proxmox_s3_client::S3_BUCKET_NAME_SCHEMA,
+            optional: true,
+        },
+        "max-cache-size": {
+            type: HumanByte,
+            optional: true,
+        }
+    },
+    default_key: "type",
+)]
+#[derive(Default, Deserialize, Serialize)]
+#[serde(rename_all = "kebab-case")]
+/// Datastore backend config
+pub struct DatastoreBackendConfig {
+    /// backend type
+    #[serde(rename = "type")]
+    pub ty: Option<DatastoreBackendType>,
+    /// s3 client id
+    #[serde(skip_serializing_if = "Option::is_none")]
+    pub client: Option<String>,
+    /// s3 bucket name
+    #[serde(skip_serializing_if = "Option::is_none")]
+    pub bucket: Option<String>,
+    /// maximum cache size for local datastore LRU cache
+    #[serde(skip_serializing_if = "Option::is_none")]
+    pub max_cache_size: Option<HumanByte>,
+}
+
+pub const DATASTORE_BACKEND_CONFIG_STRING_SCHEMA: Schema =
+    StringSchema::new("Datastore backend config")
+        .format(&ApiStringFormat::VerifyFn(verify_datastore_backend_config))
+        .type_text("<backend-config>")
+        .schema();
+
+fn verify_datastore_backend_config(input: &str) -> Result<(), Error> {
+    DatastoreBackendConfig::from_str(input).map(|_| ())
+}
+
+impl FromStr for DatastoreBackendConfig {
+    type Err = Error;
+
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        let backend_config: DatastoreBackendConfig =
+            proxmox_schema::property_string::parse_with_schema(
+                s,
+                &DatastoreBackendConfig::API_SCHEMA,
+            )?;
+        let backend_type = backend_config.ty.unwrap_or_default();
+        match backend_type {
+            DatastoreBackendType::Filesystem => {
+                if backend_config.client.is_some() {
+                    bail!("additional option client, not allowed for backend type filesystem");
+                }
+                if backend_config.bucket.is_some() {
+                    bail!("additional option bucket, not allowed for backend type filesystem");
+                }
+                if backend_config.max_cache_size.is_some() {
+                    bail!(
+                        "additional option max-cache-size, not allowed for backend type filesystem"
+                    );
+                }
+            }
+            DatastoreBackendType::S3 => {
+                if backend_config.client.is_none() {
+                    bail!("missing option client, required for backend type s3");
+                }
+                if backend_config.bucket.is_none() {
+                    bail!("missing option bucket, required for backend type s3");
+                }
+            }
+        }
+        Ok(backend_config)
+    }
+}
+
 #[api(
     properties: {
         name: {
@@ -336,7 +437,11 @@ pub const DATASTORE_TUNING_STRING_SCHEMA: Schema = StringSchema::new("Datastore
             optional: true,
             format: &proxmox_schema::api_types::UUID_FORMAT,
             type: String,
-        }
+        },
+        backend: {
+            schema: DATASTORE_BACKEND_CONFIG_STRING_SCHEMA,
+            optional: true,
+        },
     }
 )]
 #[derive(Serialize, Deserialize, Updater, Clone, PartialEq)]
@@ -389,6 +494,11 @@ pub struct DataStoreConfig {
     #[updater(skip)]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub backing_device: Option<String>,
+
+    /// Backend configuration for datastore
+    #[updater(skip)]
+    #[serde(skip_serializing_if = "Option::is_none")]
+    pub backend: Option<String>,
 }
 
 #[api]
@@ -424,6 +534,7 @@ impl DataStoreConfig {
             tuning: None,
             maintenance_mode: None,
             backing_device: None,
+            backend: None,
         }
     }
 
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox v9 2/3] pbs-api-types: maintenance: add new maintenance mode S3 refresh
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox v9 1/3] pbs-api-types: extend datastore config by backend config enum Christian Ebner
@ 2025-07-19 12:49 ` Christian Ebner
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox v9 3/3] s3 client: wrap upload with retry into dedicated methods Christian Ebner
                   ` (49 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:49 UTC (permalink / raw)
  To: pbs-devel

The new maintenance mode S3 refresh disallows any read and write
operations on the underlying datastore, but without expecting it to
be unmountable or clearing it from the internal datastore cache.

This mode is intended to be used when refreshing a datastore backed
by and S3 object store, downloading, clearing and recreating the
contents.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- no changes

 pbs-api-types/src/datastore.rs   | 1 +
 pbs-api-types/src/maintenance.rs | 4 ++++
 2 files changed, 5 insertions(+)

diff --git a/pbs-api-types/src/datastore.rs b/pbs-api-types/src/datastore.rs
index 7110a329..ee94ccad 100644
--- a/pbs-api-types/src/datastore.rs
+++ b/pbs-api-types/src/datastore.rs
@@ -567,6 +567,7 @@ impl DataStoreConfig {
             Some(MaintenanceType::Unmount) => {
                 /* used to reset it after failed unmount, or alternative for aborting unmount task */
             }
+            Some(MaintenanceType::S3Refresh) => { /* used to reset state after refresh finished */ }
             Some(MaintenanceType::Delete) => {
                 match new_type {
                     Some(MaintenanceType::Delete) => { /* allow to delete a deleted storage */ }
diff --git a/pbs-api-types/src/maintenance.rs b/pbs-api-types/src/maintenance.rs
index 3c9aa819..a516a1d9 100644
--- a/pbs-api-types/src/maintenance.rs
+++ b/pbs-api-types/src/maintenance.rs
@@ -49,6 +49,8 @@ pub enum MaintenanceType {
     Delete,
     /// The (removable) datastore is being unmounted.
     Unmount,
+    /// The S3 cache store is being refreshed.
+    S3Refresh,
 }
 serde_plain::derive_display_from_serialize!(MaintenanceType);
 serde_plain::derive_fromstr_from_deserialize!(MaintenanceType);
@@ -100,6 +102,8 @@ impl MaintenanceMode {
             bail!("datastore is being unmounted");
         } else if self.ty == MaintenanceType::Offline {
             bail!("offline maintenance mode: {}", message);
+        } else if self.ty == MaintenanceType::S3Refresh {
+            bail!("S3 refresh maintenance mode: {}", message);
         } else if self.ty == MaintenanceType::ReadOnly {
             if let Some(Operation::Write) = operation {
                 bail!("read-only maintenance mode: {}", message);
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox v9 3/3] s3 client: wrap upload with retry into dedicated methods
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox v9 1/3] pbs-api-types: extend datastore config by backend config enum Christian Ebner
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox v9 2/3] pbs-api-types: maintenance: add new maintenance mode S3 refresh Christian Ebner
@ 2025-07-19 12:49 ` Christian Ebner
  2025-07-21 15:37   ` [pbs-devel] applied: " Thomas Lamprecht
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 01/46] datastore: add helpers for path/digest to s3 object key conversion Christian Ebner
                   ` (48 subsequent siblings)
  51 siblings, 1 reply; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:49 UTC (permalink / raw)
  To: pbs-devel

The `replace` boolean flag in the upload_with_retry() method
signature unfortunately is not very ergonomic and hinders code
readability.

Therefore, wrap the method into helpers covering both variants,
encoding their behaviour in the method name instead.

As the client is currently only used by the Proxmox Backup Server's
s3 backend, the code not being applied just yet, it is fine to drop
the previous public method althogehter without much further adaptions.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- not present in previous version

 proxmox-s3-client/src/client.rs | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)

diff --git a/proxmox-s3-client/src/client.rs b/proxmox-s3-client/src/client.rs
index 08e45ebf..5e9d7cb2 100644
--- a/proxmox-s3-client/src/client.rs
+++ b/proxmox-s3-client/src/client.rs
@@ -585,8 +585,36 @@ impl S3Client {
         Ok(delete_errors)
     }
 
-    /// Upload the given object via the S3 api, retrying up to 3 times in case of error.
-    pub async fn upload_with_retry(
+    /// Upload the given object via the S3 api, not replacing it if already present in the object
+    /// store.
+    /// Retrying up to 3 times in case of error.
+    #[inline(always)]
+    pub async fn upload_no_replace_with_retry(
+        &self,
+        object_key: S3ObjectKey,
+        object_data: Bytes,
+    ) -> Result<bool, Error> {
+        let replace = false;
+        self.do_upload_with_retry(object_key, object_data, replace)
+            .await
+    }
+
+    /// Upload the given object via the S3 api, replacing it if already present in the object store.
+    /// Retrying up to 3 times in case of error.
+    #[inline(always)]
+    pub async fn upload_replace_with_retry(
+        &self,
+        object_key: S3ObjectKey,
+        object_data: Bytes,
+    ) -> Result<bool, Error> {
+        let replace = true;
+        self.do_upload_with_retry(object_key, object_data, replace)
+            .await
+    }
+
+    /// Helper to perform the object upload and retry, wrapped by the corresponding methods
+    /// to mask the `replace` flag.
+    async fn do_upload_with_retry(
         &self,
         object_key: S3ObjectKey,
         object_data: Bytes,
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 01/46] datastore: add helpers for path/digest to s3 object key conversion
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (2 preceding siblings ...)
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox v9 3/3] s3 client: wrap upload with retry into dedicated methods Christian Ebner
@ 2025-07-19 12:49 ` Christian Ebner
  2025-07-21 12:29   ` Hannes Laimer
  2025-07-21 13:58   ` Hannes Laimer
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 02/46] config: introduce s3 object store client configuration Christian Ebner
                   ` (47 subsequent siblings)
  51 siblings, 2 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:49 UTC (permalink / raw)
  To: pbs-devel

Adds helper methods to generate the s3 object keys given a relative
path and filename for datastore contents or digest in case of chunk
files.

Regular datastore contents are stored by grouping them with a content
prefix in the object key. In order to keep the object key length
small, given the max limit of 1024 bytes {0], `.cnt` is used as
content prefix. Chunks on the other hand are prefixed by `.chunks`,
same as on regular datastores.

The prefix allows for selective listing of either contents or chunks
by providing the prefix to the respective api calls.

[0] https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- added unit tests for helper functions

 Cargo.toml               |   1 +
 pbs-datastore/Cargo.toml |   1 +
 pbs-datastore/src/lib.rs |   1 +
 pbs-datastore/src/s3.rs  | 114 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 117 insertions(+)
 create mode 100644 pbs-datastore/src/s3.rs

diff --git a/Cargo.toml b/Cargo.toml
index adfa427d1..97783ddd5 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -77,6 +77,7 @@ proxmox-rest-server = { version = "1", features = [ "templates" ] }
 proxmox-router = { version = "3.2.2", default-features = false }
 proxmox-rrd = "1"
 proxmox-rrd-api-types = "1.0.2"
+proxmox-s3-client = "1.0.0"
 # everything but pbs-config and pbs-client use "api-macro"
 proxmox-schema = "4"
 proxmox-section-config = "3"
diff --git a/pbs-datastore/Cargo.toml b/pbs-datastore/Cargo.toml
index 56f6e9094..c42eff165 100644
--- a/pbs-datastore/Cargo.toml
+++ b/pbs-datastore/Cargo.toml
@@ -34,6 +34,7 @@ proxmox-borrow.workspace = true
 proxmox-human-byte.workspace = true
 proxmox-io.workspace = true
 proxmox-lang.workspace=true
+proxmox-s3-client = { workspace = true, features = [ "impl" ] }
 proxmox-schema = { workspace = true, features = [ "api-macro" ] }
 proxmox-serde = { workspace = true, features = [ "serde_json" ] }
 proxmox-sys.workspace = true
diff --git a/pbs-datastore/src/lib.rs b/pbs-datastore/src/lib.rs
index 5014b6c09..ffd0d91b2 100644
--- a/pbs-datastore/src/lib.rs
+++ b/pbs-datastore/src/lib.rs
@@ -182,6 +182,7 @@ pub mod manifest;
 pub mod paperkey;
 pub mod prune;
 pub mod read_chunk;
+pub mod s3;
 pub mod store_progress;
 pub mod task_tracking;
 
diff --git a/pbs-datastore/src/s3.rs b/pbs-datastore/src/s3.rs
new file mode 100644
index 000000000..79e7548fb
--- /dev/null
+++ b/pbs-datastore/src/s3.rs
@@ -0,0 +1,114 @@
+use std::path::{Path, PathBuf};
+
+use anyhow::{bail, format_err, Error};
+
+use proxmox_s3_client::S3ObjectKey;
+
+/// Object key prefix to group regular datastore contents (not chunks)
+pub const S3_CONTENT_PREFIX: &str = ".cnt";
+
+/// Generate a relative object key with content prefix from given path and filename
+pub fn object_key_from_path(path: &Path, filename: &str) -> Result<S3ObjectKey, Error> {
+    // Force the use of relative paths, otherwise this would loose the content prefix
+    if path.is_absolute() {
+        bail!("cannot generate object key from absolute path");
+    }
+    if filename.contains('/') {
+        bail!("invalid filename containing slashes");
+    }
+    let mut object_path = PathBuf::from(S3_CONTENT_PREFIX);
+    object_path.push(path);
+    object_path.push(filename);
+
+    let object_key_str = object_path
+        .to_str()
+        .ok_or_else(|| format_err!("unexpected object key path"))?;
+    Ok(S3ObjectKey::from(object_key_str))
+}
+
+/// Generate a relative object key with chunk prefix from given digest
+pub fn object_key_from_digest(digest: &[u8; 32]) -> Result<S3ObjectKey, Error> {
+    let object_key = hex::encode(digest);
+    let digest_prefix = &object_key[..4];
+    let object_key_string = format!(".chunks/{digest_prefix}/{object_key}");
+    Ok(S3ObjectKey::from(object_key_string.as_str()))
+}
+
+/// Generate a relative object key with chunk prefix from given digest, extended by suffix
+pub fn object_key_from_digest_with_suffix(
+    digest: &[u8; 32],
+    suffix: &str,
+) -> Result<S3ObjectKey, Error> {
+    if suffix.contains('/') {
+        bail!("invalid suffix containing slashes");
+    }
+    let object_key = hex::encode(digest);
+    let digest_prefix = &object_key[..4];
+    let object_key_string = format!(".chunks/{digest_prefix}/{object_key}{suffix}");
+    Ok(S3ObjectKey::from(object_key_string.as_str()))
+}
+
+#[test]
+fn test_object_key_from_path() {
+    let path = Path::new("vm/100/2025-07-14T14:20:02Z");
+    let filename = "drive-scsci0.img.fidx";
+    assert_eq!(
+        object_key_from_path(path, filename).unwrap().to_string(),
+        ".cnt/vm/100/2025-07-14T14:20:02Z/drive-scsci0.img.fidx",
+    );
+}
+
+#[test]
+fn test_object_key_from_empty_path() {
+    let path = Path::new("");
+    let filename = ".marker";
+    assert_eq!(
+        object_key_from_path(path, filename).unwrap().to_string(),
+        ".cnt/.marker",
+    );
+}
+
+#[test]
+fn test_object_key_from_absolute_path() {
+    assert!(object_key_from_path(Path::new("/"), ".marker").is_err());
+}
+
+#[test]
+fn test_object_key_from_path_incorrect_filename() {
+    assert!(object_key_from_path(Path::new(""), "/.marker").is_err());
+}
+
+#[test]
+fn test_object_key_from_digest() {
+    use hex::FromHex;
+    let digest =
+        <[u8; 32]>::from_hex("bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8")
+            .unwrap();
+    assert_eq!(
+        object_key_from_digest(&digest).unwrap().to_string(),
+        ".chunks/bb9f/bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8",
+    );
+}
+
+#[test]
+fn test_object_key_from_digest_with_suffix() {
+    use hex::FromHex;
+    let digest =
+        <[u8; 32]>::from_hex("bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8")
+            .unwrap();
+    assert_eq!(
+        object_key_from_digest_with_suffix(&digest, ".0.bad")
+            .unwrap()
+            .to_string(),
+        ".chunks/bb9f/bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8.0.bad",
+    );
+}
+
+#[test]
+fn test_object_key_from_digest_with_invalid_suffix() {
+    use hex::FromHex;
+    let digest =
+        <[u8; 32]>::from_hex("bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8")
+            .unwrap();
+    assert!(object_key_from_digest_with_suffix(&digest, "/.0.bad").is_err());
+}
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 02/46] config: introduce s3 object store client configuration
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (3 preceding siblings ...)
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 01/46] datastore: add helpers for path/digest to s3 object key conversion Christian Ebner
@ 2025-07-19 12:49 ` Christian Ebner
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 03/46] api: config: implement endpoints to manipulate and list s3 configs Christian Ebner
                   ` (46 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:49 UTC (permalink / raw)
  To: pbs-devel

Adds the client configuration for s3 object store as dedicated
configuration files, with secrets being stored separately from the
regular configuration and excluded from api responses for security
reasons.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use pbs_buildcfg::configdir as config base path
- define and expose `s3client` and `s3secrets` as config type constants
- use object_schema_unwrap()
- add missing docstrings

 pbs-config/Cargo.toml |  1 +
 pbs-config/src/lib.rs |  1 +
 pbs-config/src/s3.rs  | 89 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 91 insertions(+)
 create mode 100644 pbs-config/src/s3.rs

diff --git a/pbs-config/Cargo.toml b/pbs-config/Cargo.toml
index 284149658..74afb3c64 100644
--- a/pbs-config/Cargo.toml
+++ b/pbs-config/Cargo.toml
@@ -19,6 +19,7 @@ serde_json.workspace = true
 
 proxmox-notify.workspace = true
 proxmox-router = { workspace = true, default-features = false }
+proxmox-s3-client.workspace = true
 proxmox-schema.workspace = true
 proxmox-section-config.workspace = true
 proxmox-shared-memory.workspace = true
diff --git a/pbs-config/src/lib.rs b/pbs-config/src/lib.rs
index 9c4d77c24..d03c079ab 100644
--- a/pbs-config/src/lib.rs
+++ b/pbs-config/src/lib.rs
@@ -10,6 +10,7 @@ pub mod network;
 pub mod notifications;
 pub mod prune;
 pub mod remote;
+pub mod s3;
 pub mod sync;
 pub mod tape_job;
 pub mod token_shadow;
diff --git a/pbs-config/src/s3.rs b/pbs-config/src/s3.rs
new file mode 100644
index 000000000..7351cd184
--- /dev/null
+++ b/pbs-config/src/s3.rs
@@ -0,0 +1,89 @@
+use std::collections::HashMap;
+use std::sync::LazyLock;
+
+use anyhow::Error;
+
+use proxmox_s3_client::{S3ClientConfig, S3ClientSecretsConfig};
+use proxmox_schema::*;
+use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
+
+use pbs_api_types::JOB_ID_SCHEMA;
+
+use pbs_buildcfg::configdir;
+
+use crate::{open_backup_lockfile, replace_backup_config, BackupLockGuard};
+
+pub static CONFIG: LazyLock<SectionConfig> = LazyLock::new(init);
+
+fn init() -> SectionConfig {
+    let obj_schema = S3ClientConfig::API_SCHEMA.unwrap_object_schema();
+    let secrets_obj_schema = S3ClientSecretsConfig::API_SCHEMA.unwrap_object_schema();
+    let plugin =
+        SectionConfigPlugin::new("s3client".to_string(), Some(String::from("id")), obj_schema);
+    let secrets_plugin = SectionConfigPlugin::new(
+        "s3secrets".to_string(),
+        Some(String::from("secrets-id")),
+        secrets_obj_schema,
+    );
+    let mut config = SectionConfig::new(&JOB_ID_SCHEMA);
+    config.register_plugin(plugin);
+    config.register_plugin(secrets_plugin);
+
+    config
+}
+
+/// Configuration file location for S3 client.
+pub const S3_CFG_FILENAME: &str = configdir!("/s3.cfg");
+/// Configuration file location for S3 client secret with limited access, only readable by `root`.
+pub const S3_SECRETS_CFG_FILENAME: &str = configdir!("/s3-secrets.cfg");
+/// Configuration lock file used to prevent concurrent configuration update operations.
+pub const S3_CFG_LOCKFILE: &str = configdir!("/.s3.lck");
+
+/// Config type for s3 client config entries
+pub const S3_CFG_TYPE_ID: &str = "s3client";
+/// Config type for s3 client secrets config entries
+pub const S3_SECRETS_CFG_TYPE_ID: &str = "s3secrets";
+
+/// Get exclusive lock for S3 client configuration update.
+pub fn lock_config() -> Result<BackupLockGuard, Error> {
+    open_backup_lockfile(S3_CFG_LOCKFILE, None, true)
+}
+
+/// Load s3 client configuration from file.
+pub fn config() -> Result<(SectionConfigData, [u8; 32]), Error> {
+    parse_config(S3_CFG_FILENAME)
+}
+
+/// Load s3 client secrets configuration from file.
+pub fn secrets_config() -> Result<(SectionConfigData, [u8; 32]), Error> {
+    parse_config(S3_SECRETS_CFG_FILENAME)
+}
+
+/// Save given s3 client and secrets configuration to file.
+pub fn save_config(config: &SectionConfigData, secrets: &SectionConfigData) -> Result<(), Error> {
+    let raw = CONFIG.write(S3_CFG_FILENAME, config)?;
+    replace_backup_config(S3_CFG_FILENAME, raw.as_bytes())?;
+
+    let secrets_raw = CONFIG.write(S3_SECRETS_CFG_FILENAME, secrets)?;
+    // Secrets are stored with `backup` permissions to allow reading from
+    // not protected api endpoints as well.
+    replace_backup_config(S3_SECRETS_CFG_FILENAME, secrets_raw.as_bytes())?;
+
+    Ok(())
+}
+
+/// Shell completion helper to complete s3 client id's as found in the config.
+pub fn complete_s3_client_id(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
+    match config() {
+        Ok((data, _digest)) => data.sections.keys().map(|id| id.to_string()).collect(),
+        Err(_) => Vec::new(),
+    }
+}
+
+fn parse_config(path: &str) -> Result<(SectionConfigData, [u8; 32]), Error> {
+    let content = proxmox_sys::fs::file_read_optional_string(path)?;
+    let content = content.unwrap_or_default();
+    let digest = openssl::sha::sha256(content.as_bytes());
+    let data = CONFIG.parse(path, &content)?;
+    Ok((data, digest))
+}
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 03/46] api: config: implement endpoints to manipulate and list s3 configs
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (4 preceding siblings ...)
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 02/46] config: introduce s3 object store client configuration Christian Ebner
@ 2025-07-19 12:49 ` Christian Ebner
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 04/46] api: datastore: check s3 backend bucket access on datastore create Christian Ebner
                   ` (45 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:49 UTC (permalink / raw)
  To: pbs-devel

Allows to create, list, modify and delete configurations for s3
clients via the api.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- no changes

 Cargo.toml             |   1 +
 src/api2/config/mod.rs |   2 +
 src/api2/config/s3.rs  | 310 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 313 insertions(+)
 create mode 100644 src/api2/config/s3.rs

diff --git a/Cargo.toml b/Cargo.toml
index 97783ddd5..2c2db0123 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -225,6 +225,7 @@ proxmox-notify = { workspace = true, features = [ "pbs-context" ] }
 proxmox-openid.workspace = true
 proxmox-rest-server = { workspace = true, features = [ "rate-limited-stream" ] }
 proxmox-router = { workspace = true, features = [ "cli", "server"] }
+proxmox-s3-client.workspace = true
 proxmox-schema = { workspace = true, features = [ "api-macro" ] }
 proxmox-section-config.workspace = true
 proxmox-serde = { workspace = true, features = [ "serde_json" ] }
diff --git a/src/api2/config/mod.rs b/src/api2/config/mod.rs
index 15dc5db92..1cd9ead76 100644
--- a/src/api2/config/mod.rs
+++ b/src/api2/config/mod.rs
@@ -14,6 +14,7 @@ pub mod metrics;
 pub mod notifications;
 pub mod prune;
 pub mod remote;
+pub mod s3;
 pub mod sync;
 pub mod tape_backup_job;
 pub mod tape_encryption_keys;
@@ -32,6 +33,7 @@ const SUBDIRS: SubdirMap = &sorted!([
     ("notifications", &notifications::ROUTER),
     ("prune", &prune::ROUTER),
     ("remote", &remote::ROUTER),
+    ("s3", &s3::ROUTER),
     ("sync", &sync::ROUTER),
     ("tape-backup-job", &tape_backup_job::ROUTER),
     ("tape-encryption-keys", &tape_encryption_keys::ROUTER),
diff --git a/src/api2/config/s3.rs b/src/api2/config/s3.rs
new file mode 100644
index 000000000..c76704f5a
--- /dev/null
+++ b/src/api2/config/s3.rs
@@ -0,0 +1,310 @@
+use ::serde::{Deserialize, Serialize};
+use anyhow::Error;
+use hex::FromHex;
+use serde_json::Value;
+
+use proxmox_router::{http_bail, Permission, Router, RpcEnvironment};
+use proxmox_s3_client::{
+    S3ClientConfig, S3ClientConfigUpdater, S3ClientSecretsConfig, S3ClientSecretsConfigUpdater,
+};
+use proxmox_schema::{api, param_bail};
+
+use pbs_api_types::{JOB_ID_SCHEMA, PRIV_SYS_AUDIT, PRIV_SYS_MODIFY, PROXMOX_CONFIG_DIGEST_SCHEMA};
+use pbs_config::s3;
+
+#[api(
+    input: {
+        properties: {},
+    },
+    returns: {
+        description: "List configured s3 clients.",
+        type: Array,
+        items: { type: S3ClientConfig },
+    },
+    access: {
+        permission: &Permission::Privilege(&[], PRIV_SYS_AUDIT, false),
+    },
+)]
+/// List all s3 client configurations.
+pub fn list_s3_client_config(
+    _param: Value,
+    rpcenv: &mut dyn RpcEnvironment,
+) -> Result<Vec<S3ClientConfig>, Error> {
+    let (config, digest) = s3::config()?;
+    let list = config.convert_to_typed_array("s3client")?;
+
+    let (_secrets, secrets_digest) = s3::secrets_config()?;
+    let digest = digest_with_secrets(&digest, &secrets_digest);
+    rpcenv["digest"] = hex::encode(digest).into();
+
+    Ok(list)
+}
+
+#[api(
+    protected: true,
+    input: {
+        properties: {
+            config: {
+                type: S3ClientConfig,
+                flatten: true,
+            },
+            secrets: {
+                type: S3ClientSecretsConfig,
+                flatten: true,
+            },
+        },
+    },
+    access: {
+        permission: &Permission::Privilege(&[], PRIV_SYS_MODIFY, false),
+    },
+)]
+/// Create a new s3 client configuration.
+pub fn create_s3_client_config(
+    config: S3ClientConfig,
+    secrets: S3ClientSecretsConfig,
+    _rpcenv: &mut dyn RpcEnvironment,
+) -> Result<(), Error> {
+    // Asssure both, config and secrets are referenced by the same `id`
+    if config.id != secrets.secrets_id {
+        param_bail!(
+            "id",
+            "config and secrets must use the same id ({} != {})",
+            config.id,
+            secrets.secrets_id
+        );
+    }
+
+    let _lock = s3::lock_config()?;
+    let (mut section_config, _digest) = s3::config()?;
+    if section_config.sections.contains_key(&config.id) {
+        param_bail!("id", "s3 client config '{}' already exists.", config.id);
+    }
+
+    let (mut section_secrets, _secrets_digest) = s3::secrets_config()?;
+    if section_secrets.sections.contains_key(&config.id) {
+        param_bail!("id", "s3 secrets config '{}' already exists.", config.id);
+    }
+
+    section_config.set_data(&config.id, "s3client", &config)?;
+    section_secrets.set_data(&config.id, "s3secrets", &secrets)?;
+    s3::save_config(&section_config, &section_secrets)?;
+
+    Ok(())
+}
+
+#[api(
+    input: {
+        properties: {
+            id: {
+                schema: JOB_ID_SCHEMA,
+            },
+        },
+    },
+    returns: { type: S3ClientConfig },
+    access: {
+        permission: &Permission::Privilege(&[], PRIV_SYS_AUDIT, false),
+    },
+)]
+/// Read an s3 client configuration.
+pub fn read_s3_client_config(
+    id: String,
+    rpcenv: &mut dyn RpcEnvironment,
+) -> Result<S3ClientConfig, Error> {
+    let (config, digest) = s3::config()?;
+    let s3_client_config: S3ClientConfig = config.lookup("s3client", &id)?;
+
+    let (_secrets, secrets_digest) = s3::secrets_config()?;
+    let digest = digest_with_secrets(&digest, &secrets_digest);
+    rpcenv["digest"] = hex::encode(digest).into();
+
+    Ok(s3_client_config)
+}
+
+#[api()]
+#[derive(Serialize, Deserialize)]
+#[serde(rename_all = "kebab-case")]
+/// Deletable property name
+pub enum DeletableProperty {
+    /// Delete the port property.
+    Port,
+    /// Delete the region property.
+    Region,
+    /// Delete the fingerprint property.
+    Fingerprint,
+    /// Delete the path-style property.
+    PathStyle,
+}
+
+#[api(
+    protected: true,
+    input: {
+        properties: {
+            id: {
+                schema: JOB_ID_SCHEMA,
+            },
+            update: {
+                type: S3ClientConfigUpdater,
+                flatten: true,
+            },
+            "update-secrets": {
+                type: S3ClientSecretsConfigUpdater,
+                flatten: true,
+            },
+            delete: {
+                description: "List of properties to delete.",
+                type: Array,
+                optional: true,
+                items: {
+                    type: DeletableProperty,
+                }
+            },
+            digest: {
+                optional: true,
+                schema: PROXMOX_CONFIG_DIGEST_SCHEMA,
+            },
+        },
+    },
+    access: {
+        permission: &Permission::Privilege(&[], PRIV_SYS_MODIFY, false),
+    },
+)]
+/// Update an s3 client configuration.
+#[allow(clippy::too_many_arguments)]
+pub fn update_s3_client_config(
+    id: String,
+    update: S3ClientConfigUpdater,
+    update_secrets: S3ClientSecretsConfigUpdater,
+    delete: Option<Vec<DeletableProperty>>,
+    digest: Option<String>,
+    _rpcenv: &mut dyn RpcEnvironment,
+) -> Result<(), Error> {
+    let _lock = s3::lock_config()?;
+    let (mut config, expected_digest) = s3::config()?;
+    let (mut secrets, secrets_digest) = s3::secrets_config()?;
+    let expected_digest = digest_with_secrets(&expected_digest, &secrets_digest);
+
+    // Secrets are not included in digest concurrent changes therefore not detected.
+    if let Some(ref digest) = digest {
+        let digest = <[u8; 32]>::from_hex(digest)?;
+        crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?;
+    }
+
+    let mut data: S3ClientConfig = config.lookup("s3client", &id)?;
+
+    if let Some(delete) = delete {
+        for delete_prop in delete {
+            match delete_prop {
+                DeletableProperty::Port => {
+                    data.port = None;
+                }
+                DeletableProperty::Region => {
+                    data.region = None;
+                }
+                DeletableProperty::Fingerprint => {
+                    data.fingerprint = None;
+                }
+                DeletableProperty::PathStyle => {
+                    data.path_style = None;
+                }
+            }
+        }
+    }
+
+    if let Some(endpoint) = update.endpoint {
+        data.endpoint = endpoint;
+    }
+    if let Some(port) = update.port {
+        data.port = Some(port);
+    }
+    if let Some(region) = update.region {
+        data.region = Some(region);
+    }
+    if let Some(access_key) = update.access_key {
+        data.access_key = access_key;
+    }
+    if let Some(fingerprint) = update.fingerprint {
+        data.fingerprint = Some(fingerprint);
+    }
+    if let Some(path_style) = update.path_style {
+        data.path_style = Some(path_style);
+    }
+
+    let mut secrets_data: S3ClientSecretsConfig = secrets.lookup("s3secrets", &id)?;
+    if let Some(secret_key) = update_secrets.secret_key {
+        secrets_data.secret_key = secret_key;
+    }
+
+    config.set_data(&id, "s3client", &data)?;
+    secrets.set_data(&id, "s3secrets", &secrets_data)?;
+    s3::save_config(&config, &secrets)?;
+
+    Ok(())
+}
+
+#[api(
+    protected: true,
+    input: {
+        properties: {
+            id: {
+                schema: JOB_ID_SCHEMA,
+            },
+            digest: {
+                optional: true,
+                schema: PROXMOX_CONFIG_DIGEST_SCHEMA,
+            },
+        },
+    },
+    access: {
+        permission: &Permission::Privilege(&[], PRIV_SYS_MODIFY, false),
+    },
+)]
+/// Remove an s3 client configuration.
+pub fn delete_s3_client_config(
+    id: String,
+    digest: Option<String>,
+    _rpcenv: &mut dyn RpcEnvironment,
+) -> Result<(), Error> {
+    let _lock = s3::lock_config()?;
+    let (mut config, expected_digest) = s3::config()?;
+    let (mut secrets, secrets_digest) = s3::secrets_config()?;
+    let expected_digest = digest_with_secrets(&expected_digest, &secrets_digest);
+
+    if let Some(ref digest) = digest {
+        let digest = <[u8; 32]>::from_hex(digest)?;
+        crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?;
+    }
+
+    match (config.sections.remove(&id), secrets.sections.remove(&id)) {
+        (Some(_), Some(_)) => {}
+        (None, None) => http_bail!(
+            NOT_FOUND,
+            "s3 client config and secrets '{id}' do not exist."
+        ),
+        (Some(_), None) => http_bail!(
+            NOT_FOUND,
+            "removed s3 client config, but no secrets for '{id}' found."
+        ),
+        (None, Some(_)) => http_bail!(
+            NOT_FOUND,
+            "removed s3 client secrets, but no config for '{id}' found."
+        ),
+    }
+    s3::save_config(&config, &secrets)
+}
+
+// Calculate the digest based on the digest of config and secrets to detect changes for both
+fn digest_with_secrets(digest: &[u8; 32], secrets_digest: &[u8; 32]) -> [u8; 32] {
+    let mut digest = digest.to_vec();
+    digest.append(&mut secrets_digest.to_vec());
+    openssl::sha::sha256(&digest)
+}
+
+const ITEM_ROUTER: Router = Router::new()
+    .get(&API_METHOD_READ_S3_CLIENT_CONFIG)
+    .put(&API_METHOD_UPDATE_S3_CLIENT_CONFIG)
+    .delete(&API_METHOD_DELETE_S3_CLIENT_CONFIG);
+
+pub const ROUTER: Router = Router::new()
+    .get(&API_METHOD_LIST_S3_CLIENT_CONFIG)
+    .post(&API_METHOD_CREATE_S3_CLIENT_CONFIG)
+    .match_all("id", &ITEM_ROUTER);
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 04/46] api: datastore: check s3 backend bucket access on datastore create
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (5 preceding siblings ...)
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 03/46] api: config: implement endpoints to manipulate and list s3 configs Christian Ebner
@ 2025-07-19 12:49 ` Christian Ebner
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 05/46] api/cli: add endpoint and command to check s3 client connection Christian Ebner
                   ` (44 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:49 UTC (permalink / raw)
  To: pbs-devel

Check if the configured S3 object store backend can be reached and
the provided secrets have the permissions to access the bucket.

Perform the check before creating the chunk store, so it is not left
behind if the bucket cannot be reached.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use s3 config type constants over plain str for config lookups

 Cargo.toml                   |  2 +-
 src/api2/config/datastore.rs | 49 ++++++++++++++++++++++++++++++++----
 2 files changed, 45 insertions(+), 6 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 2c2db0123..afc782041 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -77,7 +77,7 @@ proxmox-rest-server = { version = "1", features = [ "templates" ] }
 proxmox-router = { version = "3.2.2", default-features = false }
 proxmox-rrd = "1"
 proxmox-rrd-api-types = "1.0.2"
-proxmox-s3-client = "1.0.0"
+proxmox-s3-client = { version = "1.0.0", features = [ "impl" ] }
 # everything but pbs-config and pbs-client use "api-macro"
 proxmox-schema = "4"
 proxmox-section-config = "3"
diff --git a/src/api2/config/datastore.rs b/src/api2/config/datastore.rs
index b133be707..7a4a39074 100644
--- a/src/api2/config/datastore.rs
+++ b/src/api2/config/datastore.rs
@@ -1,22 +1,24 @@
 use std::path::{Path, PathBuf};
 
 use ::serde::{Deserialize, Serialize};
-use anyhow::{bail, Context, Error};
+use anyhow::{bail, format_err, Context, Error};
 use hex::FromHex;
 use serde_json::Value;
 use tracing::{info, warn};
 
 use proxmox_router::{http_bail, Permission, Router, RpcEnvironment, RpcEnvironmentType};
+use proxmox_s3_client::{S3Client, S3ClientConfig, S3ClientOptions, S3ClientSecretsConfig};
 use proxmox_schema::{api, param_bail, ApiType};
 use proxmox_section_config::SectionConfigData;
 use proxmox_uuid::Uuid;
 
 use pbs_api_types::{
-    Authid, DataStoreConfig, DataStoreConfigUpdater, DatastoreNotify, DatastoreTuning, KeepOptions,
-    MaintenanceMode, PruneJobConfig, PruneJobOptions, DATASTORE_SCHEMA, PRIV_DATASTORE_ALLOCATE,
-    PRIV_DATASTORE_AUDIT, PRIV_DATASTORE_MODIFY, PRIV_SYS_MODIFY, PROXMOX_CONFIG_DIGEST_SCHEMA,
-    UPID_SCHEMA,
+    Authid, DataStoreConfig, DataStoreConfigUpdater, DatastoreBackendConfig, DatastoreBackendType,
+    DatastoreNotify, DatastoreTuning, KeepOptions, MaintenanceMode, PruneJobConfig,
+    PruneJobOptions, DATASTORE_SCHEMA, PRIV_DATASTORE_ALLOCATE, PRIV_DATASTORE_AUDIT,
+    PRIV_DATASTORE_MODIFY, PRIV_SYS_MODIFY, PROXMOX_CONFIG_DIGEST_SCHEMA, UPID_SCHEMA,
 };
+use pbs_config::s3::{S3_CFG_TYPE_ID, S3_SECRETS_CFG_TYPE_ID};
 use pbs_config::BackupLockGuard;
 use pbs_datastore::chunk_store::ChunkStore;
 
@@ -116,6 +118,43 @@ pub(crate) fn do_create_datastore(
             .parse_property_string(datastore.tuning.as_deref().unwrap_or(""))?,
     )?;
 
+    if let Some(ref backend_config) = datastore.backend {
+        let backend_config: DatastoreBackendConfig = backend_config.parse()?;
+        match backend_config.ty.unwrap_or_default() {
+            DatastoreBackendType::Filesystem => (),
+            DatastoreBackendType::S3 => {
+                let s3_client_id = backend_config
+                    .client
+                    .as_ref()
+                    .ok_or_else(|| format_err!("missing required client"))?;
+                let bucket = backend_config
+                    .bucket
+                    .clone()
+                    .ok_or_else(|| format_err!("missing required bucket"))?;
+                let (config, _config_digest) =
+                    pbs_config::s3::config().context("failed to get s3 config")?;
+                let (secrets, _secrets_digest) =
+                    pbs_config::s3::secrets_config().context("failed to get s3 secrets")?;
+                let config: S3ClientConfig = config
+                    .lookup(S3_CFG_TYPE_ID, s3_client_id)
+                    .with_context(|| format!("no '{s3_client_id}' in config"))?;
+                let secrets: S3ClientSecretsConfig = secrets
+                    .lookup(S3_SECRETS_CFG_TYPE_ID, s3_client_id)
+                    .with_context(|| format!("no '{s3_client_id}' in secrets"))?;
+                let options = S3ClientOptions::from_config(
+                    config,
+                    secrets,
+                    bucket,
+                    datastore.name.to_owned(),
+                );
+                let s3_client = S3Client::new(options).context("failed to create s3 client")?;
+                // Fine to block since this runs in worker task
+                proxmox_async::runtime::block_on(s3_client.head_bucket())
+                    .context("failed to access bucket")?;
+            }
+        }
+    }
+
     let unmount_guard = if datastore.backing_device.is_some() {
         do_mount_device(datastore.clone())?;
         UnmountGuard::new(Some(path.clone()))
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 05/46] api/cli: add endpoint and command to check s3 client connection
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (6 preceding siblings ...)
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 04/46] api: datastore: check s3 backend bucket access on datastore create Christian Ebner
@ 2025-07-19 12:49 ` Christian Ebner
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 06/46] datastore: allow to get the backend for a datastore Christian Ebner
                   ` (43 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:49 UTC (permalink / raw)
  To: pbs-devel

Adds a dedicated api endpoint and a proxmox-backup-manager command to
check if the configured S3 client can reach the bucket.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use s3 config type constants over plain str for config lookups

 src/api2/admin/mod.rs                 |  2 +
 src/api2/admin/s3.rs                  | 82 +++++++++++++++++++++++++++
 src/bin/proxmox-backup-manager.rs     |  1 +
 src/bin/proxmox_backup_manager/mod.rs |  2 +
 src/bin/proxmox_backup_manager/s3.rs  | 46 +++++++++++++++
 5 files changed, 133 insertions(+)
 create mode 100644 src/api2/admin/s3.rs
 create mode 100644 src/bin/proxmox_backup_manager/s3.rs

diff --git a/src/api2/admin/mod.rs b/src/api2/admin/mod.rs
index a1c49f8e2..7694de4b9 100644
--- a/src/api2/admin/mod.rs
+++ b/src/api2/admin/mod.rs
@@ -9,6 +9,7 @@ pub mod gc;
 pub mod metrics;
 pub mod namespace;
 pub mod prune;
+pub mod s3;
 pub mod sync;
 pub mod traffic_control;
 pub mod verify;
@@ -19,6 +20,7 @@ const SUBDIRS: SubdirMap = &sorted!([
     ("metrics", &metrics::ROUTER),
     ("prune", &prune::ROUTER),
     ("gc", &gc::ROUTER),
+    ("s3", &s3::ROUTER),
     ("sync", &sync::ROUTER),
     ("traffic-control", &traffic_control::ROUTER),
     ("verify", &verify::ROUTER),
diff --git a/src/api2/admin/s3.rs b/src/api2/admin/s3.rs
new file mode 100644
index 000000000..cb415dc14
--- /dev/null
+++ b/src/api2/admin/s3.rs
@@ -0,0 +1,82 @@
+//! S3 bucket operations
+
+use anyhow::{Context, Error};
+use serde_json::Value;
+
+use proxmox_http::Body;
+use proxmox_router::{list_subdirs_api_method, Permission, Router, RpcEnvironment, SubdirMap};
+use proxmox_s3_client::{
+    S3Client, S3ClientConfig, S3ClientOptions, S3ClientSecretsConfig, S3_BUCKET_NAME_SCHEMA,
+    S3_CLIENT_ID_SCHEMA,
+};
+use proxmox_schema::*;
+use proxmox_sortable_macro::sortable;
+
+use pbs_api_types::PRIV_SYS_MODIFY;
+
+use pbs_config::s3::{S3_CFG_TYPE_ID, S3_SECRETS_CFG_TYPE_ID};
+
+#[api(
+    input: {
+        properties: {
+            "s3-client-id": {
+                schema: S3_CLIENT_ID_SCHEMA,
+            },
+            bucket: {
+                schema: S3_BUCKET_NAME_SCHEMA,
+            },
+            "store-prefix": {
+                type: String,
+                description: "Store prefix within bucket for S3 object keys (commonly datastore name)",
+            },
+        },
+    },
+    access: {
+        permission: &Permission::Privilege(&[], PRIV_SYS_MODIFY, false),
+    },
+)]
+/// Perform basic sanity check for given s3 client configuration
+pub async fn check(
+    s3_client_id: String,
+    bucket: String,
+    store_prefix: String,
+    _rpcenv: &mut dyn RpcEnvironment,
+) -> Result<Value, Error> {
+    let (config, _digest) = pbs_config::s3::config()?;
+    let config: S3ClientConfig = config
+        .lookup(S3_CFG_TYPE_ID, &s3_client_id)
+        .context("config lookup failed")?;
+    let (secrets, _secrets_digest) = pbs_config::s3::secrets_config()?;
+    let secrets: S3ClientSecretsConfig = secrets
+        .lookup(S3_SECRETS_CFG_TYPE_ID, &s3_client_id)
+        .context("secrets lookup failed")?;
+
+    let options = S3ClientOptions::from_config(config, secrets, bucket, store_prefix);
+
+    let test_object_key = ".s3-client-test";
+    let client = S3Client::new(options).context("client creation failed")?;
+    client.head_bucket().await.context("head object failed")?;
+    client
+        .put_object(test_object_key.into(), Body::empty(), true)
+        .await
+        .context("put object failed")?;
+    client
+        .get_object(test_object_key.into())
+        .await
+        .context("get object failed")?;
+    client
+        .delete_object(test_object_key.into())
+        .await
+        .context("delete object failed")?;
+
+    Ok(Value::Null)
+}
+
+#[sortable]
+const S3_OPERATION_SUBDIRS: SubdirMap = &[("check", &Router::new().get(&API_METHOD_CHECK))];
+
+const S3_OPERATION_ROUTER: Router = Router::new()
+    .get(&list_subdirs_api_method!(S3_OPERATION_SUBDIRS))
+    .subdirs(S3_OPERATION_SUBDIRS);
+
+pub const ROUTER: Router = Router::new().match_all("s3-client-id", &S3_OPERATION_ROUTER);
diff --git a/src/bin/proxmox-backup-manager.rs b/src/bin/proxmox-backup-manager.rs
index 378c5c7f1..0a04ce0bf 100644
--- a/src/bin/proxmox-backup-manager.rs
+++ b/src/bin/proxmox-backup-manager.rs
@@ -677,6 +677,7 @@ async fn run() -> Result<(), Error> {
         .insert("garbage-collection", garbage_collection_commands())
         .insert("acme", acme_mgmt_cli())
         .insert("cert", cert_mgmt_cli())
+        .insert("s3", s3_commands())
         .insert("subscription", subscription_commands())
         .insert("sync-job", sync_job_commands())
         .insert("verify-job", verify_job_commands())
diff --git a/src/bin/proxmox_backup_manager/mod.rs b/src/bin/proxmox_backup_manager/mod.rs
index 52cfe9b29..a9b02604e 100644
--- a/src/bin/proxmox_backup_manager/mod.rs
+++ b/src/bin/proxmox_backup_manager/mod.rs
@@ -27,6 +27,8 @@ mod prune;
 pub use prune::*;
 mod remote;
 pub use remote::*;
+mod s3;
+pub use s3::*;
 mod subscription;
 pub use subscription::*;
 mod sync;
diff --git a/src/bin/proxmox_backup_manager/s3.rs b/src/bin/proxmox_backup_manager/s3.rs
new file mode 100644
index 000000000..9bb89ff55
--- /dev/null
+++ b/src/bin/proxmox_backup_manager/s3.rs
@@ -0,0 +1,46 @@
+use proxmox_router::{cli::*, RpcEnvironment};
+use proxmox_s3_client::{S3_BUCKET_NAME_SCHEMA, S3_CLIENT_ID_SCHEMA};
+use proxmox_schema::api;
+
+use proxmox_backup::api2;
+
+use anyhow::Error;
+use serde_json::Value;
+
+#[api(
+    input: {
+        properties: {
+            "s3-client-id": {
+                schema: S3_CLIENT_ID_SCHEMA,
+            },
+            bucket: {
+                schema: S3_BUCKET_NAME_SCHEMA,
+            },
+            "store-prefix": {
+                type: String,
+                description: "Store prefix within bucket for S3 object keys (commonly datastore name)",
+            },
+        },
+    },
+)]
+/// Perform basic sanity checks for given S3 client configuration
+async fn check(
+    s3_client_id: String,
+    bucket: String,
+    store_prefix: String,
+    rpcenv: &mut dyn RpcEnvironment,
+) -> Result<Value, Error> {
+    api2::admin::s3::check(s3_client_id, bucket, store_prefix, rpcenv).await?;
+    Ok(Value::Null)
+}
+
+pub fn s3_commands() -> CommandLineInterface {
+    let cmd_def = CliCommandMap::new().insert(
+        "check",
+        CliCommand::new(&API_METHOD_CHECK)
+            .arg_param(&["s3-client-id", "bucket"])
+            .completion_cb("s3-client-id", pbs_config::s3::complete_s3_client_id),
+    );
+
+    cmd_def.into()
+}
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 06/46] datastore: allow to get the backend for a datastore
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (7 preceding siblings ...)
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 05/46] api/cli: add endpoint and command to check s3 client connection Christian Ebner
@ 2025-07-19 12:49 ` Christian Ebner
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 07/46] api: backup: store datastore backend in runtime environment Christian Ebner
                   ` (42 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:49 UTC (permalink / raw)
  To: pbs-devel

Implements an enum with variants Filesystem and S3 to distinguish
between available backends. Filesystem will be used as default, if no
backend is configured in the datastores configuration. If the
datastore has an s3 backend configured, the backend method will
instantiate and s3 client and return it with the S3 variant.

This allows to instantiate the client once, keeping and reusing the
same open connection to the api for the lifetime of task or job, e.g.
in the backup writer/readers runtime environment.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use s3 config type constants over plain str for config lookups
- added missing docstrings

 pbs-datastore/src/datastore.rs | 58 ++++++++++++++++++++++++++++++++--
 pbs-datastore/src/lib.rs       |  1 +
 2 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
index d663465e2..bc829c5b8 100644
--- a/pbs-datastore/src/datastore.rs
+++ b/pbs-datastore/src/datastore.rs
@@ -12,6 +12,7 @@ use pbs_tools::lru_cache::LruCache;
 use tracing::{info, warn};
 
 use proxmox_human_byte::HumanByte;
+use proxmox_s3_client::{S3Client, S3ClientConfig, S3ClientOptions, S3ClientSecretsConfig};
 use proxmox_schema::ApiType;
 
 use proxmox_sys::error::SysError;
@@ -23,9 +24,11 @@ use proxmox_worker_task::WorkerTaskContext;
 
 use pbs_api_types::{
     ArchiveType, Authid, BackupGroupDeleteStats, BackupNamespace, BackupType, ChunkOrder,
-    DataStoreConfig, DatastoreFSyncLevel, DatastoreTuning, GarbageCollectionCacheStats,
-    GarbageCollectionStatus, MaintenanceMode, MaintenanceType, Operation, UPID,
+    DataStoreConfig, DatastoreBackendConfig, DatastoreBackendType, DatastoreFSyncLevel,
+    DatastoreTuning, GarbageCollectionCacheStats, GarbageCollectionStatus, MaintenanceMode,
+    MaintenanceType, Operation, UPID,
 };
+use pbs_config::s3::{S3_CFG_TYPE_ID, S3_SECRETS_CFG_TYPE_ID};
 use pbs_config::BackupLockGuard;
 
 use crate::backup_info::{BackupDir, BackupGroup, BackupInfo, OLD_LOCKING};
@@ -127,6 +130,7 @@ pub struct DataStoreImpl {
     chunk_order: ChunkOrder,
     last_digest: Option<[u8; 32]>,
     sync_level: DatastoreFSyncLevel,
+    backend_config: DatastoreBackendConfig,
 }
 
 impl DataStoreImpl {
@@ -141,6 +145,7 @@ impl DataStoreImpl {
             chunk_order: Default::default(),
             last_digest: None,
             sync_level: Default::default(),
+            backend_config: Default::default(),
         })
     }
 }
@@ -196,6 +201,15 @@ impl Drop for DataStore {
     }
 }
 
+#[derive(Clone)]
+/// Storage backend type for a datastore.
+pub enum DatastoreBackend {
+    /// Storage is located on local filesystem.
+    Filesystem,
+    /// Storage is located on S3 compatible object store.
+    S3(Arc<S3Client>),
+}
+
 impl DataStore {
     // This one just panics on everything
     #[doc(hidden)]
@@ -206,6 +220,40 @@ impl DataStore {
         })
     }
 
+    /// Get the backend for this datastore based on it's configuration
+    pub fn backend(&self) -> Result<DatastoreBackend, Error> {
+        let backend_type = match self.inner.backend_config.ty.unwrap_or_default() {
+            DatastoreBackendType::Filesystem => DatastoreBackend::Filesystem,
+            DatastoreBackendType::S3 => {
+                let s3_client_id = self
+                    .inner
+                    .backend_config
+                    .client
+                    .as_ref()
+                    .ok_or_else(|| format_err!("missing client for s3 backend"))?;
+                let bucket = self
+                    .inner
+                    .backend_config
+                    .bucket
+                    .clone()
+                    .ok_or_else(|| format_err!("missing bucket for s3 backend"))?;
+
+                let (config, _config_digest) = pbs_config::s3::config()?;
+                let (secrets, _secrets_digest) = pbs_config::s3::secrets_config()?;
+                let config: S3ClientConfig = config.lookup(S3_CFG_TYPE_ID, s3_client_id)?;
+                let secrets: S3ClientSecretsConfig =
+                    secrets.lookup(S3_SECRETS_CFG_TYPE_ID, s3_client_id)?;
+
+                let options =
+                    S3ClientOptions::from_config(config, secrets, bucket, self.name().to_owned());
+                let s3_client = S3Client::new(options)?;
+                DatastoreBackend::S3(Arc::new(s3_client))
+            }
+        };
+
+        Ok(backend_type)
+    }
+
     pub fn lookup_datastore(
         name: &str,
         operation: Option<Operation>,
@@ -383,6 +431,11 @@ impl DataStore {
                 .parse_property_string(config.tuning.as_deref().unwrap_or(""))?,
         )?;
 
+        let backend_config: DatastoreBackendConfig = serde_json::from_value(
+            DatastoreBackendConfig::API_SCHEMA
+                .parse_property_string(config.backend.as_deref().unwrap_or(""))?,
+        )?;
+
         Ok(DataStoreImpl {
             chunk_store,
             gc_mutex: Mutex::new(()),
@@ -391,6 +444,7 @@ impl DataStore {
             chunk_order: tuning.chunk_order.unwrap_or_default(),
             last_digest,
             sync_level: tuning.sync_level.unwrap_or_default(),
+            backend_config,
         })
     }
 
diff --git a/pbs-datastore/src/lib.rs b/pbs-datastore/src/lib.rs
index ffd0d91b2..ca6fdb7d8 100644
--- a/pbs-datastore/src/lib.rs
+++ b/pbs-datastore/src/lib.rs
@@ -204,6 +204,7 @@ pub use store_progress::StoreProgress;
 mod datastore;
 pub use datastore::{
     check_backup_owner, ensure_datastore_is_mounted, get_datastore_mount_status, DataStore,
+    DatastoreBackend,
 };
 
 mod hierarchy;
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 07/46] api: backup: store datastore backend in runtime environment
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (8 preceding siblings ...)
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 06/46] datastore: allow to get the backend for a datastore Christian Ebner
@ 2025-07-19 12:49 ` Christian Ebner
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 08/46] api: backup: conditionally upload chunks to s3 object store backend Christian Ebner
                   ` (41 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:49 UTC (permalink / raw)
  To: pbs-devel

Get and store the datastore's backend during creation of the backup
runtime environment and upload the chunks to the local filesystem or
s3 object store based on the backend variant.

By storing the backend variant in the environment the s3 client is
instantiated only once and reused for all api calls in the same
backup http/2 connection.

Refactor the upgrade method by moving all logic into the async block,
such that the now possible error on backup environment creation gets
propagated to the thread spawn call side.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- no changes

 src/api2/backup/environment.rs |  11 +--
 src/api2/backup/mod.rs         | 128 ++++++++++++++++-----------------
 2 files changed, 71 insertions(+), 68 deletions(-)

diff --git a/src/api2/backup/environment.rs b/src/api2/backup/environment.rs
index 1d8f64aa0..7bd86f39c 100644
--- a/src/api2/backup/environment.rs
+++ b/src/api2/backup/environment.rs
@@ -16,7 +16,7 @@ use pbs_api_types::Authid;
 use pbs_datastore::backup_info::{BackupDir, BackupInfo};
 use pbs_datastore::dynamic_index::DynamicIndexWriter;
 use pbs_datastore::fixed_index::FixedIndexWriter;
-use pbs_datastore::{DataBlob, DataStore};
+use pbs_datastore::{DataBlob, DataStore, DatastoreBackend};
 use proxmox_rest_server::{formatter::*, WorkerTask};
 
 use crate::backup::VerifyWorker;
@@ -116,6 +116,7 @@ pub struct BackupEnvironment {
     pub datastore: Arc<DataStore>,
     pub backup_dir: BackupDir,
     pub last_backup: Option<BackupInfo>,
+    pub backend: DatastoreBackend,
     state: Arc<Mutex<SharedBackupState>>,
 }
 
@@ -126,7 +127,7 @@ impl BackupEnvironment {
         worker: Arc<WorkerTask>,
         datastore: Arc<DataStore>,
         backup_dir: BackupDir,
-    ) -> Self {
+    ) -> Result<Self, Error> {
         let state = SharedBackupState {
             finished: false,
             uid_counter: 0,
@@ -138,7 +139,8 @@ impl BackupEnvironment {
             backup_stat: UploadStatistic::new(),
         };
 
-        Self {
+        let backend = datastore.backend()?;
+        Ok(Self {
             result_attributes: json!({}),
             env_type,
             auth_id,
@@ -148,8 +150,9 @@ impl BackupEnvironment {
             formatter: JSON_FORMATTER,
             backup_dir,
             last_backup: None,
+            backend,
             state: Arc::new(Mutex::new(state)),
-        }
+        })
     }
 
     /// Register a Chunk with associated length.
diff --git a/src/api2/backup/mod.rs b/src/api2/backup/mod.rs
index a723e7cb0..026f1f106 100644
--- a/src/api2/backup/mod.rs
+++ b/src/api2/backup/mod.rs
@@ -187,7 +187,8 @@ fn upgrade_to_backup_protocol(
             }
 
             // lock last snapshot to prevent forgetting/pruning it during backup
-            let guard = last.backup_dir
+            let guard = last
+                .backup_dir
                 .lock_shared()
                 .with_context(|| format!("while locking last snapshot during backup '{last:?}'"))?;
             Some(guard)
@@ -206,14 +207,14 @@ fn upgrade_to_backup_protocol(
             Some(worker_id),
             auth_id.to_string(),
             true,
-            move |worker| {
+            move |worker| async move {
                 let mut env = BackupEnvironment::new(
                     env_type,
                     auth_id,
                     worker.clone(),
                     datastore,
                     backup_dir,
-                );
+                )?;
 
                 env.debug = debug;
                 env.last_backup = last_backup;
@@ -247,74 +248,73 @@ fn upgrade_to_backup_protocol(
                         http.max_frame_size(4 * 1024 * 1024);
 
                         let env3 = env2.clone();
-                        http.serve_connection(conn, TowerToHyperService::new(service)).map(move |result| {
-                            match result {
-                                Err(err) => {
-                                    // Avoid  Transport endpoint is not connected (os error 107)
-                                    // fixme: find a better way to test for that error
-                                    if err.to_string().starts_with("connection error")
-                                        && env3.finished()
-                                    {
-                                        Ok(())
-                                    } else {
-                                        Err(Error::from(err))
+                        http.serve_connection(conn, TowerToHyperService::new(service))
+                            .map(move |result| {
+                                match result {
+                                    Err(err) => {
+                                        // Avoid  Transport endpoint is not connected (os error 107)
+                                        // fixme: find a better way to test for that error
+                                        if err.to_string().starts_with("connection error")
+                                            && env3.finished()
+                                        {
+                                            Ok(())
+                                        } else {
+                                            Err(Error::from(err))
+                                        }
                                     }
+                                    Ok(()) => Ok(()),
                                 }
-                                Ok(()) => Ok(()),
-                            }
-                        })
+                            })
                     });
                 let mut abort_future = abort_future.map(|_| Err(format_err!("task aborted")));
 
-                async move {
-                    // keep flock until task ends
-                    let _group_guard = _group_guard;
-                    let snap_guard = snap_guard;
-                    let _last_guard = _last_guard;
-
-                    let res = select! {
-                        req = req_fut => req,
-                        abrt = abort_future => abrt,
-                    };
-                    if benchmark {
-                        env.log("benchmark finished successfully");
-                        proxmox_async::runtime::block_in_place(|| env.remove_backup())?;
-                        return Ok(());
+                // keep flock until task ends
+                let _group_guard = _group_guard;
+                let snap_guard = snap_guard;
+                let _last_guard = _last_guard;
+
+                let res = select! {
+                    req = req_fut => req,
+                    abrt = abort_future => abrt,
+                };
+                if benchmark {
+                    env.log("benchmark finished successfully");
+                    proxmox_async::runtime::block_in_place(|| env.remove_backup())?;
+                    return Ok(());
+                }
+
+                let verify = |env: BackupEnvironment| {
+                    if let Err(err) = env.verify_after_complete(snap_guard) {
+                        env.log(format!(
+                            "backup finished, but starting the requested verify task failed: {}",
+                            err
+                        ));
                     }
+                };
 
-                    let verify = |env: BackupEnvironment| {
-                        if let Err(err) = env.verify_after_complete(snap_guard) {
-                            env.log(format!(
-                                "backup finished, but starting the requested verify task failed: {}",
-                                err
-                            ));
-                        }
-                    };
-
-                    match (res, env.ensure_finished()) {
-                        (Ok(_), Ok(())) => {
-                            env.log("backup finished successfully");
-                            verify(env);
-                            Ok(())
-                        }
-                        (Err(err), Ok(())) => {
-                            // ignore errors after finish
-                            env.log(format!("backup had errors but finished: {}", err));
-                            verify(env);
-                            Ok(())
-                        }
-                        (Ok(_), Err(err)) => {
-                            env.log(format!("backup ended and finish failed: {}", err));
-                            env.log("removing unfinished backup");
-                            proxmox_async::runtime::block_in_place(|| env.remove_backup())?;
-                            Err(err)
-                        }
-                        (Err(err), Err(_)) => {
-                            env.log(format!("backup failed: {}", err));
-                            env.log("removing failed backup");
-                            proxmox_async::runtime::block_in_place(|| env.remove_backup())?;
-                            Err(err)
-                        }
+                match (res, env.ensure_finished()) {
+                    (Ok(_), Ok(())) => {
+                        env.log("backup finished successfully");
+                        verify(env);
+                        Ok(())
+                    }
+                    (Err(err), Ok(())) => {
+                        // ignore errors after finish
+                        env.log(format!("backup had errors but finished: {}", err));
+                        verify(env);
+                        Ok(())
+                    }
+                    (Ok(_), Err(err)) => {
+                        env.log(format!("backup ended and finish failed: {}", err));
+                        env.log("removing unfinished backup");
+                        proxmox_async::runtime::block_in_place(|| env.remove_backup())?;
+                        Err(err)
+                    }
+                    (Err(err), Err(_)) => {
+                        env.log(format!("backup failed: {}", err));
+                        env.log("removing failed backup");
+                        proxmox_async::runtime::block_in_place(|| env.remove_backup())?;
+                        Err(err)
                     }
                 }
             },
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 08/46] api: backup: conditionally upload chunks to s3 object store backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (9 preceding siblings ...)
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 07/46] api: backup: store datastore backend in runtime environment Christian Ebner
@ 2025-07-19 12:49 ` Christian Ebner
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 09/46] api: backup: conditionally upload blobs " Christian Ebner
                   ` (40 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:49 UTC (permalink / raw)
  To: pbs-devel

Upload fixed and dynamic sized chunks to either the filesystem or
the S3 object store, depending on the configured backend.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use refactored upload_no_replace_with_retry

 src/api2/backup/upload_chunk.rs | 73 ++++++++++++++++++++-------------
 1 file changed, 44 insertions(+), 29 deletions(-)

diff --git a/src/api2/backup/upload_chunk.rs b/src/api2/backup/upload_chunk.rs
index 2c66c2855..f1b07a123 100644
--- a/src/api2/backup/upload_chunk.rs
+++ b/src/api2/backup/upload_chunk.rs
@@ -16,7 +16,7 @@ use proxmox_sortable_macro::sortable;
 
 use pbs_api_types::{BACKUP_ARCHIVE_NAME_SCHEMA, CHUNK_DIGEST_SCHEMA};
 use pbs_datastore::file_formats::{DataBlobHeader, EncryptedDataBlobHeader};
-use pbs_datastore::{DataBlob, DataStore};
+use pbs_datastore::{DataBlob, DataStore, DatastoreBackend};
 use pbs_tools::json::{required_integer_param, required_string_param};
 
 use super::environment::*;
@@ -154,22 +154,10 @@ fn upload_fixed_chunk(
 ) -> ApiResponseFuture {
     async move {
         let wid = required_integer_param(&param, "wid")? as usize;
-        let size = required_integer_param(&param, "size")? as u32;
-        let encoded_size = required_integer_param(&param, "encoded-size")? as u32;
-
-        let digest_str = required_string_param(&param, "digest")?;
-        let digest = <[u8; 32]>::from_hex(digest_str)?;
-
         let env: &BackupEnvironment = rpcenv.as_ref();
 
-        let (digest, size, compressed_size, is_duplicate) = UploadChunk::new(
-            BodyDataStream::new(req_body),
-            env.datastore.clone(),
-            digest,
-            size,
-            encoded_size,
-        )
-        .await?;
+        let (digest, size, compressed_size, is_duplicate) =
+            upload_to_backend(req_body, param, env).await?;
 
         env.register_fixed_chunk(wid, digest, size, compressed_size, is_duplicate)?;
         let digest_str = hex::encode(digest);
@@ -229,22 +217,10 @@ fn upload_dynamic_chunk(
 ) -> ApiResponseFuture {
     async move {
         let wid = required_integer_param(&param, "wid")? as usize;
-        let size = required_integer_param(&param, "size")? as u32;
-        let encoded_size = required_integer_param(&param, "encoded-size")? as u32;
-
-        let digest_str = required_string_param(&param, "digest")?;
-        let digest = <[u8; 32]>::from_hex(digest_str)?;
-
         let env: &BackupEnvironment = rpcenv.as_ref();
 
-        let (digest, size, compressed_size, is_duplicate) = UploadChunk::new(
-            BodyDataStream::new(req_body),
-            env.datastore.clone(),
-            digest,
-            size,
-            encoded_size,
-        )
-        .await?;
+        let (digest, size, compressed_size, is_duplicate) =
+            upload_to_backend(req_body, param, env).await?;
 
         env.register_dynamic_chunk(wid, digest, size, compressed_size, is_duplicate)?;
         let digest_str = hex::encode(digest);
@@ -256,6 +232,45 @@ fn upload_dynamic_chunk(
     .boxed()
 }
 
+async fn upload_to_backend(
+    req_body: Incoming,
+    param: Value,
+    env: &BackupEnvironment,
+) -> Result<([u8; 32], u32, u32, bool), Error> {
+    let size = required_integer_param(&param, "size")? as u32;
+    let encoded_size = required_integer_param(&param, "encoded-size")? as u32;
+    let digest_str = required_string_param(&param, "digest")?;
+    let digest = <[u8; 32]>::from_hex(digest_str)?;
+
+    match &env.backend {
+        DatastoreBackend::Filesystem => {
+            UploadChunk::new(
+                BodyDataStream::new(req_body),
+                env.datastore.clone(),
+                digest,
+                size,
+                encoded_size,
+            )
+            .await
+        }
+        DatastoreBackend::S3(s3_client) => {
+            let data = req_body.collect().await?.to_bytes();
+            if encoded_size != data.len() as u32 {
+                bail!(
+                    "got blob with unexpected length ({encoded_size} != {})",
+                    data.len()
+                );
+            }
+
+            let object_key = pbs_datastore::s3::object_key_from_digest(&digest)?;
+            let is_duplicate = s3_client
+                .upload_no_replace_with_retry(object_key, data)
+                .await?;
+            Ok((digest, size, encoded_size, is_duplicate))
+        }
+    }
+}
+
 pub const API_METHOD_UPLOAD_SPEEDTEST: ApiMethod = ApiMethod::new(
     &ApiHandler::AsyncHttp(&upload_speedtest),
     &ObjectSchema::new("Test upload speed.", &[]),
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 09/46] api: backup: conditionally upload blobs to s3 object store backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (10 preceding siblings ...)
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 08/46] api: backup: conditionally upload chunks to s3 object store backend Christian Ebner
@ 2025-07-19 12:49 ` Christian Ebner
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 10/46] api: backup: conditionally upload indices " Christian Ebner
                   ` (39 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:49 UTC (permalink / raw)
  To: pbs-devel

Upload blobs to both, the local datastore cache and the S3 object
store if s3 is configured as backend.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use refactored upload_replace_with_retry

 src/api2/backup/environment.rs | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/src/api2/backup/environment.rs b/src/api2/backup/environment.rs
index 7bd86f39c..a5683b845 100644
--- a/src/api2/backup/environment.rs
+++ b/src/api2/backup/environment.rs
@@ -581,6 +581,20 @@ impl BackupEnvironment {
         let blob = DataBlob::load_from_reader(&mut &data[..])?;
 
         let raw_data = blob.raw_data();
+        if let DatastoreBackend::S3(s3_client) = &self.backend {
+            let object_key = pbs_datastore::s3::object_key_from_path(
+                &self.backup_dir.relative_path(),
+                file_name,
+            )
+            .context("invalid blob object key")?;
+            let data = hyper::body::Bytes::copy_from_slice(raw_data);
+            proxmox_async::runtime::block_on(
+                s3_client.upload_replace_with_retry(object_key.clone(), data),
+            )
+            .context("failed to upload blob to s3 backend")?;
+            self.log(format!("Uploaded blob to object store: {object_key}"))
+        }
+
         replace_file(&path, raw_data, CreateOptions::new(), false)?;
 
         self.log(format!(
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 10/46] api: backup: conditionally upload indices to s3 object store backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (11 preceding siblings ...)
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 09/46] api: backup: conditionally upload blobs " Christian Ebner
@ 2025-07-19 12:49 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 11/46] api: backup: conditionally upload manifest " Christian Ebner
                   ` (38 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:49 UTC (permalink / raw)
  To: pbs-devel

If the datastore is backed by an S3 compatible object store, upload
the dynamic or fixed index files to the object store after closing
them. The local index files are kept in the local caching datastore
to allow for fast and efficient content lookups, avoiding expensive
(as in monetary cost and IO latency) requests.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- read file contents via std::fs::read
- adapt log output to be analogous for fixed and dynamic indexes
- use refactored upload_replace_with_retry

 src/api2/backup/environment.rs | 36 ++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/src/api2/backup/environment.rs b/src/api2/backup/environment.rs
index a5683b845..b67145fa6 100644
--- a/src/api2/backup/environment.rs
+++ b/src/api2/backup/environment.rs
@@ -18,6 +18,7 @@ use pbs_datastore::dynamic_index::DynamicIndexWriter;
 use pbs_datastore::fixed_index::FixedIndexWriter;
 use pbs_datastore::{DataBlob, DataStore, DatastoreBackend};
 use proxmox_rest_server::{formatter::*, WorkerTask};
+use proxmox_s3_client::S3Client;
 
 use crate::backup::VerifyWorker;
 
@@ -479,6 +480,16 @@ impl BackupEnvironment {
             );
         }
 
+        // For S3 backends, upload the index file to the object store after closing
+        if let DatastoreBackend::S3(s3_client) = &self.backend {
+            self.s3_upload_index(s3_client, &data.name)
+                .context("failed to upload dynamic index to s3 backend")?;
+            self.log(format!(
+                "Uploaded dynamic index file to s3 backend: {}",
+                data.name
+            ))
+        }
+
         self.log_upload_stat(
             &data.name,
             &csum,
@@ -553,6 +564,16 @@ impl BackupEnvironment {
             );
         }
 
+        // For S3 backends, upload the index file to the object store after closing
+        if let DatastoreBackend::S3(s3_client) = &self.backend {
+            self.s3_upload_index(s3_client, &data.name)
+                .context("failed to upload fixed index to s3 backend")?;
+            self.log(format!(
+                "Uploaded fixed index file to object store: {}",
+                data.name
+            ))
+        }
+
         self.log_upload_stat(
             &data.name,
             &expected_csum,
@@ -751,6 +772,21 @@ impl BackupEnvironment {
 
         Ok(())
     }
+
+    fn s3_upload_index(&self, s3_client: &S3Client, name: &str) -> Result<(), Error> {
+        let object_key =
+            pbs_datastore::s3::object_key_from_path(&self.backup_dir.relative_path(), name)
+                .context("invalid index file object key")?;
+
+        let mut full_path = self.backup_dir.full_path();
+        full_path.push(name);
+        let data = std::fs::read(&full_path).context("failed to read index contents")?;
+        let contents = hyper::body::Bytes::from(data);
+        proxmox_async::runtime::block_on(
+            s3_client.upload_replace_with_retry(object_key, contents),
+        )?;
+        Ok(())
+    }
 }
 
 impl RpcEnvironment for BackupEnvironment {
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 11/46] api: backup: conditionally upload manifest to s3 object store backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (12 preceding siblings ...)
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 10/46] api: backup: conditionally upload indices " Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 12/46] api: datastore: conditionally upload client log to s3 backend Christian Ebner
                   ` (37 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Reupload the manifest to the S3 object store backend on manifest
updates, if s3 is configured as backend.
This also triggers the initial manifest upload when finishing backup
snapshot in the backup api call handler.
Updates also the locally cached version for fast and efficient
listing of contents without the need to perform expensive (as in
monetary cost and IO latency) requests.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use refactored upload_replace_with_retry
- inline err in format_err
- use to_string for group name formatting

 pbs-datastore/Cargo.toml         |  3 +++
 pbs-datastore/src/backup_info.rs | 12 +++++++++++-
 src/api2/admin/datastore.rs      | 14 ++++++++++++--
 src/api2/backup/environment.rs   | 16 ++++++++--------
 src/backup/verify.rs             |  2 +-
 5 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/pbs-datastore/Cargo.toml b/pbs-datastore/Cargo.toml
index c42eff165..7e56dbd31 100644
--- a/pbs-datastore/Cargo.toml
+++ b/pbs-datastore/Cargo.toml
@@ -13,6 +13,7 @@ crc32fast.workspace = true
 endian_trait.workspace = true
 futures.workspace = true
 hex = { workspace = true, features = [ "serde" ] }
+hyper.workspace = true
 libc.workspace = true
 log.workspace = true
 nix.workspace = true
@@ -29,8 +30,10 @@ zstd-safe.workspace = true
 pathpatterns.workspace = true
 pxar.workspace = true
 
+proxmox-async.workspace = true
 proxmox-base64.workspace = true
 proxmox-borrow.workspace = true
+proxmox-http.workspace = true
 proxmox-human-byte.workspace = true
 proxmox-io.workspace = true
 proxmox-lang.workspace=true
diff --git a/pbs-datastore/src/backup_info.rs b/pbs-datastore/src/backup_info.rs
index e3ecd437f..a4bf958cf 100644
--- a/pbs-datastore/src/backup_info.rs
+++ b/pbs-datastore/src/backup_info.rs
@@ -19,7 +19,7 @@ use pbs_api_types::{
 use pbs_config::{open_backup_lockfile, BackupLockGuard};
 
 use crate::manifest::{BackupManifest, MANIFEST_LOCK_NAME};
-use crate::{DataBlob, DataStore};
+use crate::{DataBlob, DataStore, DatastoreBackend};
 
 pub const DATASTORE_LOCKS_DIR: &str = "/run/proxmox-backup/locks";
 const PROTECTED_MARKER_FILENAME: &str = ".protected";
@@ -666,6 +666,7 @@ impl BackupDir {
     /// only use this method - anything else may break locking guarantees.
     pub fn update_manifest(
         &self,
+        backend: &DatastoreBackend,
         update_fn: impl FnOnce(&mut BackupManifest),
     ) -> Result<(), Error> {
         let _guard = self.lock_manifest()?;
@@ -678,6 +679,15 @@ impl BackupDir {
         let blob = DataBlob::encode(manifest.as_bytes(), None, true)?;
         let raw_data = blob.raw_data();
 
+        if let DatastoreBackend::S3(s3_client) = backend {
+            let object_key =
+                super::s3::object_key_from_path(&self.relative_path(), MANIFEST_BLOB_NAME.as_ref())
+                    .context("invalid manifest object key")?;
+            let data = hyper::body::Bytes::copy_from_slice(raw_data);
+            proxmox_async::runtime::block_on(s3_client.upload_replace_with_retry(object_key, data))
+                .context("failed to update manifest on s3 backend")?;
+        }
+
         let mut path = self.full_path();
         path.push(MANIFEST_BLOB_NAME.as_ref());
 
diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs
index e24bc1c1b..1fabda09a 100644
--- a/src/api2/admin/datastore.rs
+++ b/src/api2/admin/datastore.rs
@@ -65,7 +65,7 @@ use pbs_datastore::manifest::BackupManifest;
 use pbs_datastore::prune::compute_prune_info;
 use pbs_datastore::{
     check_backup_owner, ensure_datastore_is_mounted, task_tracking, BackupDir, BackupGroup,
-    DataStore, LocalChunkReader, StoreProgress,
+    DataStore, DatastoreBackend, LocalChunkReader, StoreProgress,
 };
 use pbs_tools::json::required_string_param;
 use proxmox_rest_server::{formatter, WorkerTask};
@@ -2086,6 +2086,16 @@ pub fn set_group_notes(
         &backup_group,
     )?;
 
+    if let DatastoreBackend::S3(s3_client) = datastore.backend()? {
+        let mut path = ns.path();
+        path.push(backup_group.to_string());
+        let object_key = pbs_datastore::s3::object_key_from_path(&path, "notes")
+            .context("invalid owner file object key")?;
+        let data = hyper::body::Bytes::copy_from_slice(notes.as_bytes());
+        let _is_duplicate =
+            proxmox_async::runtime::block_on(s3_client.upload_replace_with_retry(object_key, data))
+                .context("failed to set notes on s3 backend")?;
+    }
     let notes_path = datastore.group_notes_path(&ns, &backup_group);
     replace_file(notes_path, notes.as_bytes(), CreateOptions::new(), false)?;
 
@@ -2188,7 +2198,7 @@ pub fn set_notes(
     let backup_dir = datastore.backup_dir(ns, backup_dir)?;
 
     backup_dir
-        .update_manifest(|manifest| {
+        .update_manifest(&datastore.backend()?, |manifest| {
             manifest.unprotected["notes"] = notes.into();
         })
         .map_err(|err| format_err!("unable to update manifest blob - {}", err))?;
diff --git a/src/api2/backup/environment.rs b/src/api2/backup/environment.rs
index b67145fa6..4a70d7c13 100644
--- a/src/api2/backup/environment.rs
+++ b/src/api2/backup/environment.rs
@@ -646,14 +646,6 @@ impl BackupEnvironment {
             bail!("backup does not contain valid files (file count == 0)");
         }
 
-        // check for valid manifest and store stats
-        let stats = serde_json::to_value(state.backup_stat)?;
-        self.backup_dir
-            .update_manifest(|manifest| {
-                manifest.unprotected["chunk_upload_stats"] = stats;
-            })
-            .map_err(|err| format_err!("unable to update manifest blob - {}", err))?;
-
         if let Some(base) = &self.last_backup {
             let path = base.backup_dir.full_path();
             if !path.exists() {
@@ -664,6 +656,14 @@ impl BackupEnvironment {
             }
         }
 
+        // check for valid manifest and store stats
+        let stats = serde_json::to_value(state.backup_stat)?;
+        self.backup_dir
+            .update_manifest(&self.backend, |manifest| {
+                manifest.unprotected["chunk_upload_stats"] = stats;
+            })
+            .map_err(|err| format_err!("unable to update manifest blob - {err}"))?;
+
         self.datastore.try_ensure_sync_level()?;
 
         // marks the backup as successful
diff --git a/src/backup/verify.rs b/src/backup/verify.rs
index 0b954ae23..9344033d8 100644
--- a/src/backup/verify.rs
+++ b/src/backup/verify.rs
@@ -359,7 +359,7 @@ impl VerifyWorker {
 
         if let Err(err) = {
             let verify_state = serde_json::to_value(verify_state)?;
-            backup_dir.update_manifest(|manifest| {
+            backup_dir.update_manifest(&self.datastore.backend()?, |manifest| {
                 manifest.unprotected["verify_state"] = verify_state;
             })
         } {
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 12/46] api: datastore: conditionally upload client log to s3 backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (13 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 11/46] api: backup: conditionally upload manifest " Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 13/46] sync: pull: conditionally upload content " Christian Ebner
                   ` (36 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

If the datastore is backed by an s3 compatible object store, upload
the client log content to the s3 backend before persisting it to the
local cache store.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use refactored upload_replace_with_retry

 src/api2/admin/datastore.rs | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs
index 1fabda09a..e6b8449d2 100644
--- a/src/api2/admin/datastore.rs
+++ b/src/api2/admin/datastore.rs
@@ -1637,6 +1637,17 @@ pub fn upload_backup_log(
         // always verify blob/CRC at server side
         let blob = DataBlob::load_from_reader(&mut &data[..])?;
 
+        if let DatastoreBackend::S3(s3_client) = datastore.backend()? {
+            let object_key = pbs_datastore::s3::object_key_from_path(
+                &backup_dir.relative_path(),
+                file_name.as_ref(),
+            )
+            .context("invalid client log object key")?;
+            let data = hyper::body::Bytes::copy_from_slice(blob.raw_data());
+            proxmox_async::runtime::block_on(s3_client.upload_replace_with_retry(object_key, data))
+                .context("failed to upload client log to s3 backend")?;
+        };
+
         replace_file(&path, blob.raw_data(), CreateOptions::new(), false)?;
 
         // fixme: use correct formatter
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 13/46] sync: pull: conditionally upload content to s3 backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (14 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 12/46] api: datastore: conditionally upload client log to s3 backend Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 14/46] api: reader: fetch chunks based on datastore backend Christian Ebner
                   ` (35 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

If the datastore is backed by an S3 object store, not only insert the
pulled contents to the local cache store, but also upload it to the
S3 backend.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use refactored upload_replace_with_retry and
  upload_no_replace_with_retry
- read file contents via tokio::fs::read

 src/server/pull.rs | 64 +++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 61 insertions(+), 3 deletions(-)

diff --git a/src/server/pull.rs b/src/server/pull.rs
index b1724c142..775ed0c59 100644
--- a/src/server/pull.rs
+++ b/src/server/pull.rs
@@ -6,7 +6,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
 use std::sync::{Arc, Mutex};
 use std::time::SystemTime;
 
-use anyhow::{bail, format_err, Error};
+use anyhow::{bail, format_err, Context, Error};
 use proxmox_human_byte::HumanByte;
 use tracing::info;
 
@@ -24,7 +24,7 @@ use pbs_datastore::fixed_index::FixedIndexReader;
 use pbs_datastore::index::IndexFile;
 use pbs_datastore::manifest::{BackupManifest, FileInfo};
 use pbs_datastore::read_chunk::AsyncReadChunk;
-use pbs_datastore::{check_backup_owner, DataStore, StoreProgress};
+use pbs_datastore::{check_backup_owner, DataStore, DatastoreBackend, StoreProgress};
 use pbs_tools::sha::sha256;
 
 use super::sync::{
@@ -167,7 +167,20 @@ async fn pull_index_chunks<I: IndexFile>(
         move |(chunk, digest, size): (DataBlob, [u8; 32], u64)| {
             // println!("verify and write {}", hex::encode(&digest));
             chunk.verify_unencrypted(size as usize, &digest)?;
-            target2.insert_chunk(&chunk, &digest)?;
+            match target2.backend()? {
+                DatastoreBackend::Filesystem => {
+                    target2.insert_chunk(&chunk, &digest)?;
+                }
+                DatastoreBackend::S3(s3_client) => {
+                    let data = chunk.raw_data().to_vec();
+                    let upload_data = hyper::body::Bytes::from(data);
+                    let object_key = pbs_datastore::s3::object_key_from_digest(&digest)?;
+                    let _is_duplicate = proxmox_async::runtime::block_on(
+                        s3_client.upload_no_replace_with_retry(object_key, upload_data),
+                    )
+                    .context("failed to upload chunk to s3 backend")?;
+                }
+            }
             Ok(())
         },
     );
@@ -331,6 +344,19 @@ async fn pull_single_archive<'a>(
     if let Err(err) = std::fs::rename(&tmp_path, &path) {
         bail!("Atomic rename file {:?} failed - {}", path, err);
     }
+    if let DatastoreBackend::S3(s3_client) = snapshot.datastore().backend()? {
+        let object_key =
+            pbs_datastore::s3::object_key_from_path(&snapshot.relative_path(), archive_name)
+                .context("invalid archive object key")?;
+
+        let data = tokio::fs::read(&path)
+            .await
+            .context("failed to read archive contents")?;
+        let contents = hyper::body::Bytes::from(data);
+        let _is_duplicate = s3_client
+            .upload_replace_with_retry(object_key, contents)
+            .await?;
+    }
     Ok(sync_stats)
 }
 
@@ -401,6 +427,7 @@ async fn pull_snapshot<'a>(
         }
     }
 
+    let manifest_data = tmp_manifest_blob.raw_data().to_vec();
     let manifest = BackupManifest::try_from(tmp_manifest_blob)?;
 
     if ignore_not_verified_or_encrypted(
@@ -467,9 +494,40 @@ async fn pull_snapshot<'a>(
     if let Err(err) = std::fs::rename(&tmp_manifest_name, &manifest_name) {
         bail!("Atomic rename file {:?} failed - {}", manifest_name, err);
     }
+    if let DatastoreBackend::S3(s3_client) = snapshot.datastore().backend()? {
+        let object_key = pbs_datastore::s3::object_key_from_path(
+            &snapshot.relative_path(),
+            MANIFEST_BLOB_NAME.as_ref(),
+        )
+        .context("invalid manifest object key")?;
+
+        let data = hyper::body::Bytes::from(manifest_data);
+        let _is_duplicate = s3_client
+            .upload_replace_with_retry(object_key, data)
+            .await
+            .context("failed to upload manifest to s3 backend")?;
+    }
 
     if !client_log_name.exists() {
         reader.try_download_client_log(&client_log_name).await?;
+        if client_log_name.exists() {
+            if let DatastoreBackend::S3(s3_client) = snapshot.datastore().backend()? {
+                let object_key = pbs_datastore::s3::object_key_from_path(
+                    &snapshot.relative_path(),
+                    CLIENT_LOG_BLOB_NAME.as_ref(),
+                )
+                .context("invalid archive object key")?;
+
+                let data = tokio::fs::read(&client_log_name)
+                    .await
+                    .context("failed to read log file contents")?;
+                let contents = hyper::body::Bytes::from(data);
+                let _is_duplicate = s3_client
+                    .upload_replace_with_retry(object_key, contents)
+                    .await
+                    .context("failed to upload client log to s3 backend")?;
+            }
+        }
     };
     snapshot
         .cleanup_unreferenced_files(&manifest)
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 14/46] api: reader: fetch chunks based on datastore backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (15 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 13/46] sync: pull: conditionally upload content " Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 15/46] datastore: local chunk reader: read chunks based on backend Christian Ebner
                   ` (34 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Read the chunk based on the datastores backend, reading from local
filesystem or fetching from S3 object store.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- no changes

 src/api2/reader/environment.rs | 12 ++++++----
 src/api2/reader/mod.rs         | 41 +++++++++++++++++++++++-----------
 2 files changed, 36 insertions(+), 17 deletions(-)

diff --git a/src/api2/reader/environment.rs b/src/api2/reader/environment.rs
index 3b2f06f43..8924352b0 100644
--- a/src/api2/reader/environment.rs
+++ b/src/api2/reader/environment.rs
@@ -1,13 +1,14 @@
 use std::collections::HashSet;
 use std::sync::{Arc, RwLock};
 
+use anyhow::Error;
 use serde_json::{json, Value};
 
 use proxmox_router::{RpcEnvironment, RpcEnvironmentType};
 
 use pbs_api_types::Authid;
 use pbs_datastore::backup_info::BackupDir;
-use pbs_datastore::DataStore;
+use pbs_datastore::{DataStore, DatastoreBackend};
 use proxmox_rest_server::formatter::*;
 use proxmox_rest_server::WorkerTask;
 use tracing::info;
@@ -23,6 +24,7 @@ pub struct ReaderEnvironment {
     pub worker: Arc<WorkerTask>,
     pub datastore: Arc<DataStore>,
     pub backup_dir: BackupDir,
+    pub backend: DatastoreBackend,
     allowed_chunks: Arc<RwLock<HashSet<[u8; 32]>>>,
 }
 
@@ -33,8 +35,9 @@ impl ReaderEnvironment {
         worker: Arc<WorkerTask>,
         datastore: Arc<DataStore>,
         backup_dir: BackupDir,
-    ) -> Self {
-        Self {
+    ) -> Result<Self, Error> {
+        let backend = datastore.backend()?;
+        Ok(Self {
             result_attributes: json!({}),
             env_type,
             auth_id,
@@ -43,8 +46,9 @@ impl ReaderEnvironment {
             debug: tracing::enabled!(tracing::Level::DEBUG),
             formatter: JSON_FORMATTER,
             backup_dir,
+            backend,
             allowed_chunks: Arc::new(RwLock::new(HashSet::new())),
-        }
+        })
     }
 
     pub fn log<S: AsRef<str>>(&self, msg: S) {
diff --git a/src/api2/reader/mod.rs b/src/api2/reader/mod.rs
index a77216043..997d9ca77 100644
--- a/src/api2/reader/mod.rs
+++ b/src/api2/reader/mod.rs
@@ -3,6 +3,7 @@
 use anyhow::{bail, format_err, Context, Error};
 use futures::*;
 use hex::FromHex;
+use http_body_util::BodyExt;
 use hyper::body::Incoming;
 use hyper::header::{self, HeaderValue, CONNECTION, UPGRADE};
 use hyper::http::request::Parts;
@@ -27,8 +28,9 @@ use pbs_api_types::{
 };
 use pbs_config::CachedUserInfo;
 use pbs_datastore::index::IndexFile;
-use pbs_datastore::{DataStore, PROXMOX_BACKUP_READER_PROTOCOL_ID_V1};
+use pbs_datastore::{DataStore, DatastoreBackend, PROXMOX_BACKUP_READER_PROTOCOL_ID_V1};
 use pbs_tools::json::required_string_param;
+use proxmox_s3_client::S3Client;
 
 use crate::api2::backup::optional_ns_param;
 use crate::api2::helpers;
@@ -162,7 +164,7 @@ fn upgrade_to_backup_reader_protocol(
                     worker.clone(),
                     datastore,
                     backup_dir,
-                );
+                )?;
 
                 env.debug = debug;
 
@@ -323,17 +325,10 @@ fn download_chunk(
             ));
         }
 
-        let (path, _) = env.datastore.chunk_path(&digest);
-        let path2 = path.clone();
-
-        env.debug(format!("download chunk {:?}", path));
-
-        let data =
-            proxmox_async::runtime::block_in_place(|| std::fs::read(path)).map_err(move |err| {
-                http_err!(BAD_REQUEST, "reading file {:?} failed: {}", path2, err)
-            })?;
-
-        let body = Body::from(data);
+        let body = match &env.backend {
+            DatastoreBackend::Filesystem => load_from_filesystem(env, &digest)?,
+            DatastoreBackend::S3(s3_client) => fetch_from_object_store(s3_client, &digest).await?,
+        };
 
         // fixme: set other headers ?
         Ok(Response::builder()
@@ -345,6 +340,26 @@ fn download_chunk(
     .boxed()
 }
 
+async fn fetch_from_object_store(s3_client: &S3Client, digest: &[u8; 32]) -> Result<Body, Error> {
+    let object_key = pbs_datastore::s3::object_key_from_digest(digest)?;
+    if let Some(response) = s3_client.get_object(object_key).await? {
+        let data = response.content.collect().await?.to_bytes();
+        return Ok(Body::from(data));
+    }
+    bail!("cannot find chunk with digest {}", hex::encode(digest));
+}
+
+fn load_from_filesystem(env: &ReaderEnvironment, digest: &[u8; 32]) -> Result<Body, Error> {
+    let (path, _) = env.datastore.chunk_path(digest);
+    let path2 = path.clone();
+
+    env.debug(format!("download chunk {path:?}"));
+
+    let data = proxmox_async::runtime::block_in_place(|| std::fs::read(path))
+        .map_err(move |err| http_err!(BAD_REQUEST, "reading file {path2:?} failed: {err}"))?;
+    Ok(Body::from(data))
+}
+
 /* this is too slow
 fn download_chunk_old(
     _parts: Parts,
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 15/46] datastore: local chunk reader: read chunks based on backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (16 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 14/46] api: reader: fetch chunks based on datastore backend Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-21 13:12   ` Hannes Laimer
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 16/46] verify worker: add datastore backed to verify worker Christian Ebner
                   ` (33 subsequent siblings)
  51 siblings, 1 reply; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Get and store the datastore's backend on local chunk reader
instantiantion and fetch chunks based on the variant from either the
filesystem or the s3 object store.

By storing the backend variant, the s3 client is instantiated only
once and reused until the local chunk reader instance is dropped.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use Arc::clone() over .clone()

 pbs-datastore/Cargo.toml                |  1 +
 pbs-datastore/src/local_chunk_reader.rs | 38 +++++++++++++++++++++----
 2 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/pbs-datastore/Cargo.toml b/pbs-datastore/Cargo.toml
index 7e56dbd31..8ce930a94 100644
--- a/pbs-datastore/Cargo.toml
+++ b/pbs-datastore/Cargo.toml
@@ -13,6 +13,7 @@ crc32fast.workspace = true
 endian_trait.workspace = true
 futures.workspace = true
 hex = { workspace = true, features = [ "serde" ] }
+http-body-util.workspace = true
 hyper.workspace = true
 libc.workspace = true
 log.workspace = true
diff --git a/pbs-datastore/src/local_chunk_reader.rs b/pbs-datastore/src/local_chunk_reader.rs
index 05a70c068..667c97206 100644
--- a/pbs-datastore/src/local_chunk_reader.rs
+++ b/pbs-datastore/src/local_chunk_reader.rs
@@ -3,17 +3,21 @@ use std::pin::Pin;
 use std::sync::Arc;
 
 use anyhow::{bail, Error};
+use http_body_util::BodyExt;
 
 use pbs_api_types::CryptMode;
 use pbs_tools::crypt_config::CryptConfig;
+use proxmox_s3_client::S3Client;
 
 use crate::data_blob::DataBlob;
+use crate::datastore::DatastoreBackend;
 use crate::read_chunk::{AsyncReadChunk, ReadChunk};
 use crate::DataStore;
 
 #[derive(Clone)]
 pub struct LocalChunkReader {
     store: Arc<DataStore>,
+    backend: DatastoreBackend,
     crypt_config: Option<Arc<CryptConfig>>,
     crypt_mode: CryptMode,
 }
@@ -24,8 +28,11 @@ impl LocalChunkReader {
         crypt_config: Option<Arc<CryptConfig>>,
         crypt_mode: CryptMode,
     ) -> Self {
+        // TODO: Error handling!
+        let backend = store.backend().unwrap();
         Self {
             store,
+            backend,
             crypt_config,
             crypt_mode,
         }
@@ -47,10 +54,26 @@ impl LocalChunkReader {
     }
 }
 
+async fn fetch(s3_client: Arc<S3Client>, digest: &[u8; 32]) -> Result<DataBlob, Error> {
+    let object_key = crate::s3::object_key_from_digest(digest)?;
+    if let Some(response) = s3_client.get_object(object_key).await? {
+        let bytes = response.content.collect().await?.to_bytes();
+        DataBlob::from_raw(bytes.to_vec())
+    } else {
+        bail!("no object with digest {}", hex::encode(digest));
+    }
+}
+
 impl ReadChunk for LocalChunkReader {
     fn read_raw_chunk(&self, digest: &[u8; 32]) -> Result<DataBlob, Error> {
-        let chunk = self.store.load_chunk(digest)?;
+        let chunk = match &self.backend {
+            DatastoreBackend::Filesystem => self.store.load_chunk(digest)?,
+            DatastoreBackend::S3(s3_client) => {
+                proxmox_async::runtime::block_on(fetch(Arc::clone(s3_client), digest))?
+            }
+        };
         self.ensure_crypt_mode(chunk.crypt_mode()?)?;
+
         Ok(chunk)
     }
 
@@ -69,11 +92,14 @@ impl AsyncReadChunk for LocalChunkReader {
         digest: &'a [u8; 32],
     ) -> Pin<Box<dyn Future<Output = Result<DataBlob, Error>> + Send + 'a>> {
         Box::pin(async move {
-            let (path, _) = self.store.chunk_path(digest);
-
-            let raw_data = tokio::fs::read(&path).await?;
-
-            let chunk = DataBlob::load_from_reader(&mut &raw_data[..])?;
+            let chunk = match &self.backend {
+                DatastoreBackend::Filesystem => {
+                    let (path, _) = self.store.chunk_path(digest);
+                    let raw_data = tokio::fs::read(&path).await?;
+                    DataBlob::load_from_reader(&mut &raw_data[..])?
+                }
+                DatastoreBackend::S3(s3_client) => fetch(Arc::clone(s3_client), digest).await?,
+            };
             self.ensure_crypt_mode(chunk.crypt_mode()?)?;
 
             Ok(chunk)
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 16/46] verify worker: add datastore backed to verify worker
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (17 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 15/46] datastore: local chunk reader: read chunks based on backend Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 17/46] verify: implement chunk verification for stores with s3 backend Christian Ebner
                   ` (32 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

In order to fetch chunks from an S3 compatible object store,
instantiate and store the s3 client in the verify worker by storing
the datastore's backend. This allows to reuse the same instance for
the whole verification task.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- no changes

 src/api2/admin/datastore.rs    |  2 +-
 src/api2/backup/environment.rs |  2 +-
 src/backup/verify.rs           | 14 ++++++++++----
 src/server/verify_job.rs       |  2 +-
 4 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs
index e6b8449d2..c72d2dbc3 100644
--- a/src/api2/admin/datastore.rs
+++ b/src/api2/admin/datastore.rs
@@ -883,7 +883,7 @@ pub fn verify(
         auth_id.to_string(),
         to_stdout,
         move |worker| {
-            let verify_worker = VerifyWorker::new(worker.clone(), datastore);
+            let verify_worker = VerifyWorker::new(worker.clone(), datastore)?;
             let failed_dirs = if let Some(backup_dir) = backup_dir {
                 let mut res = Vec::new();
                 if !verify_worker.verify_backup_dir(
diff --git a/src/api2/backup/environment.rs b/src/api2/backup/environment.rs
index 4a70d7c13..6a265bcc5 100644
--- a/src/api2/backup/environment.rs
+++ b/src/api2/backup/environment.rs
@@ -710,7 +710,7 @@ impl BackupEnvironment {
             move |worker| {
                 worker.log_message("Automatically verifying newly added snapshot");
 
-                let verify_worker = VerifyWorker::new(worker.clone(), datastore);
+                let verify_worker = VerifyWorker::new(worker.clone(), datastore)?;
                 if !verify_worker.verify_backup_dir_with_lock(
                     &backup_dir,
                     worker.upid().clone(),
diff --git a/src/backup/verify.rs b/src/backup/verify.rs
index 9344033d8..dea10f618 100644
--- a/src/backup/verify.rs
+++ b/src/backup/verify.rs
@@ -17,7 +17,7 @@ use pbs_api_types::{
 use pbs_datastore::backup_info::{BackupDir, BackupGroup, BackupInfo};
 use pbs_datastore::index::IndexFile;
 use pbs_datastore::manifest::{BackupManifest, FileInfo};
-use pbs_datastore::{DataBlob, DataStore, StoreProgress};
+use pbs_datastore::{DataBlob, DataStore, DatastoreBackend, StoreProgress};
 
 use crate::tools::parallel_handler::ParallelHandler;
 
@@ -30,19 +30,25 @@ pub struct VerifyWorker {
     datastore: Arc<DataStore>,
     verified_chunks: Arc<Mutex<HashSet<[u8; 32]>>>,
     corrupt_chunks: Arc<Mutex<HashSet<[u8; 32]>>>,
+    backend: DatastoreBackend,
 }
 
 impl VerifyWorker {
     /// Creates a new VerifyWorker for a given task worker and datastore.
-    pub fn new(worker: Arc<dyn WorkerTaskContext>, datastore: Arc<DataStore>) -> Self {
-        Self {
+    pub fn new(
+        worker: Arc<dyn WorkerTaskContext>,
+        datastore: Arc<DataStore>,
+    ) -> Result<Self, Error> {
+        let backend = datastore.backend()?;
+        Ok(Self {
             worker,
             datastore,
             // start with 16k chunks == up to 64G data
             verified_chunks: Arc::new(Mutex::new(HashSet::with_capacity(16 * 1024))),
             // start with 64 chunks since we assume there are few corrupt ones
             corrupt_chunks: Arc::new(Mutex::new(HashSet::with_capacity(64))),
-        }
+            backend,
+        })
     }
 
     fn verify_blob(backup_dir: &BackupDir, info: &FileInfo) -> Result<(), Error> {
diff --git a/src/server/verify_job.rs b/src/server/verify_job.rs
index 95a7b2a9b..c8792174b 100644
--- a/src/server/verify_job.rs
+++ b/src/server/verify_job.rs
@@ -41,7 +41,7 @@ pub fn do_verification_job(
                 None => Default::default(),
             };
 
-            let verify_worker = VerifyWorker::new(worker.clone(), datastore);
+            let verify_worker = VerifyWorker::new(worker.clone(), datastore)?;
             let result = verify_worker.verify_all_backups(
                 worker.upid(),
                 ns,
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 17/46] verify: implement chunk verification for stores with s3 backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (18 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 16/46] verify worker: add datastore backed to verify worker Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-21 13:35   ` Hannes Laimer
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 18/46] datastore: create namespace marker in " Christian Ebner
                   ` (31 subsequent siblings)
  51 siblings, 1 reply; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

For datastores backed by an S3 compatible object store, rather than
reading the chunks to be verified from the local filesystem, fetch
them via the s3 client from the configured bucket.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- refactored corrupt chunk marking into more compact methods

 src/backup/verify.rs | 118 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 103 insertions(+), 15 deletions(-)

diff --git a/src/backup/verify.rs b/src/backup/verify.rs
index dea10f618..9a2148b56 100644
--- a/src/backup/verify.rs
+++ b/src/backup/verify.rs
@@ -5,6 +5,7 @@ use std::sync::{Arc, Mutex};
 use std::time::Instant;
 
 use anyhow::{bail, Error};
+use http_body_util::BodyExt;
 use tracing::{error, info, warn};
 
 use proxmox_worker_task::WorkerTaskContext;
@@ -15,7 +16,7 @@ use pbs_api_types::{
     UPID,
 };
 use pbs_datastore::backup_info::{BackupDir, BackupGroup, BackupInfo};
-use pbs_datastore::index::IndexFile;
+use pbs_datastore::index::{ChunkReadInfo, IndexFile};
 use pbs_datastore::manifest::{BackupManifest, FileInfo};
 use pbs_datastore::{DataBlob, DataStore, DatastoreBackend, StoreProgress};
 
@@ -89,6 +90,38 @@ impl VerifyWorker {
             }
         }
 
+        if let Ok(DatastoreBackend::S3(s3_client)) = datastore.backend() {
+            let suffix = format!(".{}.bad", counter);
+            let target_key =
+                match pbs_datastore::s3::object_key_from_digest_with_suffix(digest, &suffix) {
+                    Ok(target_key) => target_key,
+                    Err(err) => {
+                        info!("could not generate target key for corrupted chunk {path:?} - {err}");
+                        return;
+                    }
+                };
+            let object_key = match pbs_datastore::s3::object_key_from_digest(digest) {
+                Ok(object_key) => object_key,
+                Err(err) => {
+                    info!("could not generate object key for corrupted chunk {path:?} - {err}");
+                    return;
+                }
+            };
+            if proxmox_async::runtime::block_on(
+                s3_client.copy_object(object_key.clone(), target_key),
+            )
+            .is_ok()
+            {
+                if proxmox_async::runtime::block_on(s3_client.delete_object(object_key)).is_err() {
+                    info!("failed to delete corrupt chunk on s3 backend: {digest_str}");
+                }
+            } else {
+                info!("failed to copy corrupt chunk on s3 backend: {digest_str}");
+            }
+        } else {
+            info!("failed to get s3 backend while trying to rename bad chunk: {digest_str}");
+        }
+
         match std::fs::rename(&path, &new_path) {
             Ok(_) => {
                 info!("corrupted chunk renamed to {:?}", &new_path);
@@ -189,20 +222,13 @@ impl VerifyWorker {
                 continue; // already verified or marked corrupt
             }
 
-            match self.datastore.load_chunk(&info.digest) {
-                Err(err) => {
-                    self.corrupt_chunks.lock().unwrap().insert(info.digest);
-                    error!("can't verify chunk, load failed - {err}");
-                    errors.fetch_add(1, Ordering::SeqCst);
-                    Self::rename_corrupted_chunk(self.datastore.clone(), &info.digest);
-                }
-                Ok(chunk) => {
-                    let size = info.size();
-                    read_bytes += chunk.raw_size();
-                    decoder_pool.send((chunk, info.digest, size))?;
-                    decoded_bytes += size;
-                }
-            }
+            self.verify_chunk_by_backend(
+                &info,
+                &mut read_bytes,
+                &mut decoded_bytes,
+                Arc::clone(&errors),
+                &decoder_pool,
+            )?;
         }
 
         decoder_pool.complete()?;
@@ -228,6 +254,68 @@ impl VerifyWorker {
         Ok(())
     }
 
+    fn verify_chunk_by_backend(
+        &self,
+        info: &ChunkReadInfo,
+        read_bytes: &mut u64,
+        decoded_bytes: &mut u64,
+        errors: Arc<AtomicUsize>,
+        decoder_pool: &ParallelHandler<(DataBlob, [u8; 32], u64)>,
+    ) -> Result<(), Error> {
+        match &self.backend {
+            DatastoreBackend::Filesystem => match self.datastore.load_chunk(&info.digest) {
+                Err(err) => self.add_corrupt_chunk(
+                    info.digest,
+                    errors,
+                    &format!("can't verify chunk, load failed - {err}"),
+                ),
+                Ok(chunk) => {
+                    let size = info.size();
+                    *read_bytes += chunk.raw_size();
+                    decoder_pool.send((chunk, info.digest, size))?;
+                    *decoded_bytes += size;
+                }
+            },
+            DatastoreBackend::S3(s3_client) => {
+                let object_key = pbs_datastore::s3::object_key_from_digest(&info.digest)?;
+                match proxmox_async::runtime::block_on(s3_client.get_object(object_key)) {
+                    Ok(Some(response)) => {
+                        let bytes = proxmox_async::runtime::block_on(response.content.collect())?
+                            .to_bytes();
+                        let chunk = DataBlob::from_raw(bytes.to_vec())?;
+                        let size = info.size();
+                        *read_bytes += chunk.raw_size();
+                        decoder_pool.send((chunk, info.digest, size))?;
+                        *decoded_bytes += size;
+                    }
+                    Ok(None) => self.add_corrupt_chunk(
+                        info.digest,
+                        errors,
+                        &format!(
+                            "can't verify missing chunk with digest {}",
+                            hex::encode(info.digest)
+                        ),
+                    ),
+                    Err(err) => self.add_corrupt_chunk(
+                        info.digest,
+                        errors,
+                        &format!("can't verify chunk, load failed - {err}"),
+                    ),
+                }
+            }
+        }
+        Ok(())
+    }
+
+    fn add_corrupt_chunk(&self, digest: [u8; 32], errors: Arc<AtomicUsize>, message: &str) {
+        // Panic on poisoned mutex
+        let mut corrupt_chunks = self.corrupt_chunks.lock().unwrap();
+        corrupt_chunks.insert(digest);
+        error!(message);
+        errors.fetch_add(1, Ordering::SeqCst);
+        Self::rename_corrupted_chunk(self.datastore.clone(), &digest);
+    }
+
     fn verify_fixed_index(&self, backup_dir: &BackupDir, info: &FileInfo) -> Result<(), Error> {
         let mut path = backup_dir.relative_path();
         path.push(&info.filename);
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 18/46] datastore: create namespace marker in s3 backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (19 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 17/46] verify: implement chunk verification for stores with s3 backend Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-21 13:52   ` Hannes Laimer
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 19/46] datastore: create/delete protected marker file on s3 storage backend Christian Ebner
                   ` (30 subsequent siblings)
  51 siblings, 1 reply; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

The S3 object store only allows to store objects, referenced by their
key. For backup namespaces datastores however use directories, so
they cannot be represented as one to one mapping.

Instead, create an empty marker file for each namespace and operate
based on that.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use upload_on_replace_with_retry

 pbs-datastore/src/datastore.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
index bc829c5b8..5bb4e1777 100644
--- a/pbs-datastore/src/datastore.rs
+++ b/pbs-datastore/src/datastore.rs
@@ -44,6 +44,7 @@ static DATASTORE_MAP: LazyLock<Mutex<HashMap<String, Arc<DataStoreImpl>>>> =
     LazyLock::new(|| Mutex::new(HashMap::new()));
 
 const GROUP_NOTES_FILE_NAME: &str = "notes";
+const NAMESPACE_MARKER_FILENAME: &str = ".namespace";
 
 /// checks if auth_id is owner, or, if owner is a token, if
 /// auth_id is the user of the token
@@ -613,6 +614,15 @@ impl DataStore {
         // construct ns before mkdir to enforce max-depth and name validity
         let ns = BackupNamespace::from_parent_ns(parent, name)?;
 
+        if let DatastoreBackend::S3(s3_client) = self.backend()? {
+            let object_key = crate::s3::object_key_from_path(&ns.path(), NAMESPACE_MARKER_FILENAME)
+                .context("invalid namespace marker object key")?;
+            let _is_duplicate = proxmox_async::runtime::block_on(
+                s3_client.upload_no_replace_with_retry(object_key, hyper::body::Bytes::from("")),
+            )
+            .context("failed to create namespace on s3 backend")?;
+        }
+
         let mut ns_full_path = self.base_path();
         ns_full_path.push(ns.path());
 
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 19/46] datastore: create/delete protected marker file on s3 storage backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (20 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 18/46] datastore: create namespace marker in " Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 20/46] datastore: prune groups/snapshots from s3 object store backend Christian Ebner
                   ` (29 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Commit 8292d3d2 ("api2/admin/datastore: add get/set_protection")
introduced the protected flag for backup snapshots, considering
snapshots as protected based on the presence/absence of the
`.protected` marker file in the corresponding snapshot directory.

To allow independent recovery of a datastore backed by an S3 bucket,
also create/delete the marker file on the object store backend. For
actual checks, still rely on the marker as encountered in the local
cache store.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use upload_no_replace_with_retry

 pbs-datastore/src/backup_info.rs |  2 +-
 pbs-datastore/src/datastore.rs   | 42 +++++++++++++++++++++++++++-----
 2 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/pbs-datastore/src/backup_info.rs b/pbs-datastore/src/backup_info.rs
index a4bf958cf..26f03a0ae 100644
--- a/pbs-datastore/src/backup_info.rs
+++ b/pbs-datastore/src/backup_info.rs
@@ -22,7 +22,7 @@ use crate::manifest::{BackupManifest, MANIFEST_LOCK_NAME};
 use crate::{DataBlob, DataStore, DatastoreBackend};
 
 pub const DATASTORE_LOCKS_DIR: &str = "/run/proxmox-backup/locks";
-const PROTECTED_MARKER_FILENAME: &str = ".protected";
+pub const PROTECTED_MARKER_FILENAME: &str = ".protected";
 
 proxmox_schema::const_regex! {
     pub BACKUP_FILES_AND_PROTECTED_REGEX = concatcp!(r"^(.*\.([fd]idx|blob)|\", PROTECTED_MARKER_FILENAME, ")$");
diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
index 5bb4e1777..fb6e14a35 100644
--- a/pbs-datastore/src/datastore.rs
+++ b/pbs-datastore/src/datastore.rs
@@ -31,7 +31,9 @@ use pbs_api_types::{
 use pbs_config::s3::{S3_CFG_TYPE_ID, S3_SECRETS_CFG_TYPE_ID};
 use pbs_config::BackupLockGuard;
 
-use crate::backup_info::{BackupDir, BackupGroup, BackupInfo, OLD_LOCKING};
+use crate::backup_info::{
+    BackupDir, BackupGroup, BackupInfo, OLD_LOCKING, PROTECTED_MARKER_FILENAME,
+};
 use crate::chunk_store::ChunkStore;
 use crate::dynamic_index::{DynamicIndexReader, DynamicIndexWriter};
 use crate::fixed_index::{FixedIndexReader, FixedIndexWriter};
@@ -1576,12 +1578,40 @@ impl DataStore {
 
         let protected_path = backup_dir.protected_file();
         if protection {
-            std::fs::File::create(protected_path)
+            std::fs::File::create(&protected_path)
                 .map_err(|err| format_err!("could not create protection file: {}", err))?;
-        } else if let Err(err) = std::fs::remove_file(protected_path) {
-            // ignore error for non-existing file
-            if err.kind() != std::io::ErrorKind::NotFound {
-                bail!("could not remove protection file: {}", err);
+            if let DatastoreBackend::S3(s3_client) = self.backend()? {
+                let object_key = crate::s3::object_key_from_path(
+                    &backup_dir.relative_path(),
+                    PROTECTED_MARKER_FILENAME,
+                )
+                .context("invalid protected marker object key")?;
+                let _is_duplicate = proxmox_async::runtime::block_on(
+                    s3_client
+                        .upload_no_replace_with_retry(object_key, hyper::body::Bytes::from("")),
+                )
+                .context("failed to mark snapshot as protected on s3 backend")?;
+            }
+        } else {
+            if let Err(err) = std::fs::remove_file(&protected_path) {
+                // ignore error for non-existing file
+                if err.kind() != std::io::ErrorKind::NotFound {
+                    bail!("could not remove protection file: {err}");
+                }
+            }
+            if let DatastoreBackend::S3(s3_client) = self.backend()? {
+                let object_key = crate::s3::object_key_from_path(
+                    &backup_dir.relative_path(),
+                    PROTECTED_MARKER_FILENAME,
+                )
+                .context("invalid protected marker object key")?;
+                if let Err(err) =
+                    proxmox_async::runtime::block_on(s3_client.delete_object(object_key))
+                {
+                    std::fs::File::create(&protected_path)
+                        .map_err(|err| format_err!("could not re-create protection file: {err}"))?;
+                    return Err(err);
+                }
             }
         }
 
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 20/46] datastore: prune groups/snapshots from s3 object store backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (21 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 19/46] datastore: create/delete protected marker file on s3 storage backend Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 21/46] datastore: get and set owner for s3 " Christian Ebner
                   ` (28 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

When pruning a backup group or a backup snapshot for a datastore with
S3 object store backend, remove the associated objects by removing
them based on the prefix.

In order to exclude protected contents, add a filtering based on the
presence of the protected marker.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- define and use dedicated constant for owner filename

 pbs-datastore/src/backup_info.rs | 49 ++++++++++++++++++++++++++++++--
 pbs-datastore/src/datastore.rs   | 48 +++++++++++++++++++++++++++----
 src/api2/admin/datastore.rs      | 24 ++++++++++------
 3 files changed, 104 insertions(+), 17 deletions(-)

diff --git a/pbs-datastore/src/backup_info.rs b/pbs-datastore/src/backup_info.rs
index 26f03a0ae..4b10b6435 100644
--- a/pbs-datastore/src/backup_info.rs
+++ b/pbs-datastore/src/backup_info.rs
@@ -9,6 +9,7 @@ use std::time::Duration;
 use anyhow::{bail, format_err, Context, Error};
 use const_format::concatcp;
 
+use proxmox_s3_client::S3PathPrefix;
 use proxmox_sys::fs::{lock_dir_noblock, lock_dir_noblock_shared, replace_file, CreateOptions};
 use proxmox_systemd::escape_unit;
 
@@ -18,7 +19,9 @@ use pbs_api_types::{
 };
 use pbs_config::{open_backup_lockfile, BackupLockGuard};
 
+use crate::datastore::{GROUP_NOTES_FILE_NAME, GROUP_OWNER_FILE_NAME};
 use crate::manifest::{BackupManifest, MANIFEST_LOCK_NAME};
+use crate::s3::S3_CONTENT_PREFIX;
 use crate::{DataBlob, DataStore, DatastoreBackend};
 
 pub const DATASTORE_LOCKS_DIR: &str = "/run/proxmox-backup/locks";
@@ -218,7 +221,7 @@ impl BackupGroup {
     ///
     /// Returns `BackupGroupDeleteStats`, containing the number of deleted snapshots
     /// and number of protected snaphsots, which therefore were not removed.
-    pub fn destroy(&self) -> Result<BackupGroupDeleteStats, Error> {
+    pub fn destroy(&self, backend: &DatastoreBackend) -> Result<BackupGroupDeleteStats, Error> {
         let _guard = self
             .lock()
             .with_context(|| format!("while destroying group '{self:?}'"))?;
@@ -232,10 +235,30 @@ impl BackupGroup {
                 delete_stats.increment_protected_snapshots();
                 continue;
             }
-            snap.destroy(false)?;
+            // also for S3 cleanup local only, the actual S3 objects will be removed below,
+            // reducing the number of required API calls.
+            snap.destroy(false, &DatastoreBackend::Filesystem)?;
             delete_stats.increment_removed_snapshots();
         }
 
+        if let DatastoreBackend::S3(s3_client) = backend {
+            let path = self.relative_group_path();
+            let group_prefix = path
+                .to_str()
+                .ok_or_else(|| format_err!("invalid group path prefix"))?;
+            let prefix = format!("{S3_CONTENT_PREFIX}/{group_prefix}");
+            let delete_objects_error = proxmox_async::runtime::block_on(
+                s3_client.delete_objects_by_prefix_with_suffix_filter(
+                    &S3PathPrefix::Some(prefix),
+                    PROTECTED_MARKER_FILENAME,
+                    &[GROUP_OWNER_FILE_NAME, GROUP_NOTES_FILE_NAME],
+                ),
+            )?;
+            if delete_objects_error {
+                bail!("deleting objects failed");
+            }
+        }
+
         // Note: make sure the old locking mechanism isn't used as `remove_dir_all` is not safe in
         // that case
         if delete_stats.all_removed() && !*OLD_LOCKING {
@@ -588,7 +611,7 @@ impl BackupDir {
     /// Destroy the whole snapshot, bails if it's protected
     ///
     /// Setting `force` to true skips locking and thus ignores if the backup is currently in use.
-    pub fn destroy(&self, force: bool) -> Result<(), Error> {
+    pub fn destroy(&self, force: bool, backend: &DatastoreBackend) -> Result<(), Error> {
         let (_guard, _manifest_guard);
         if !force {
             _guard = self
@@ -601,6 +624,20 @@ impl BackupDir {
             bail!("cannot remove protected snapshot"); // use special error type?
         }
 
+        if let DatastoreBackend::S3(s3_client) = backend {
+            let path = self.relative_path();
+            let snapshot_prefix = path
+                .to_str()
+                .ok_or_else(|| format_err!("invalid snapshot path"))?;
+            let prefix = format!("{S3_CONTENT_PREFIX}/{snapshot_prefix}");
+            let delete_objects_error = proxmox_async::runtime::block_on(
+                s3_client.delete_objects_by_prefix(&S3PathPrefix::Some(prefix)),
+            )?;
+            if delete_objects_error {
+                bail!("deleting objects failed");
+            }
+        }
+
         let full_path = self.full_path();
         log::info!("removing backup snapshot {:?}", full_path);
         std::fs::remove_dir_all(&full_path).map_err(|err| {
@@ -630,6 +667,12 @@ impl BackupDir {
         // do to rectify the situation.
         if guard.is_ok() && group.list_backups()?.is_empty() && !*OLD_LOCKING {
             group.remove_group_dir()?;
+            if let DatastoreBackend::S3(s3_client) = backend {
+                let object_key =
+                    super::s3::object_key_from_path(&group.relative_group_path(), "owner")
+                        .context("invalid owner file object key")?;
+                proxmox_async::runtime::block_on(s3_client.delete_object(object_key))?;
+            }
         } else if let Err(err) = guard {
             log::debug!("{err:#}");
         }
diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
index fb6e14a35..d5eb1ca8f 100644
--- a/pbs-datastore/src/datastore.rs
+++ b/pbs-datastore/src/datastore.rs
@@ -12,7 +12,9 @@ use pbs_tools::lru_cache::LruCache;
 use tracing::{info, warn};
 
 use proxmox_human_byte::HumanByte;
-use proxmox_s3_client::{S3Client, S3ClientConfig, S3ClientOptions, S3ClientSecretsConfig};
+use proxmox_s3_client::{
+    S3Client, S3ClientConfig, S3ClientOptions, S3ClientSecretsConfig, S3PathPrefix,
+};
 use proxmox_schema::ApiType;
 
 use proxmox_sys::error::SysError;
@@ -39,13 +41,17 @@ use crate::dynamic_index::{DynamicIndexReader, DynamicIndexWriter};
 use crate::fixed_index::{FixedIndexReader, FixedIndexWriter};
 use crate::hierarchy::{ListGroups, ListGroupsType, ListNamespaces, ListNamespacesRecursive};
 use crate::index::IndexFile;
+use crate::s3::S3_CONTENT_PREFIX;
 use crate::task_tracking::{self, update_active_operations};
 use crate::DataBlob;
 
 static DATASTORE_MAP: LazyLock<Mutex<HashMap<String, Arc<DataStoreImpl>>>> =
     LazyLock::new(|| Mutex::new(HashMap::new()));
 
-const GROUP_NOTES_FILE_NAME: &str = "notes";
+/// Filename to store backup group notes
+pub const GROUP_NOTES_FILE_NAME: &str = "notes";
+/// Filename to store backup group owner
+pub const GROUP_OWNER_FILE_NAME: &str = "owner";
 const NAMESPACE_MARKER_FILENAME: &str = ".namespace";
 
 /// checks if auth_id is owner, or, if owner is a token, if
@@ -658,7 +664,9 @@ impl DataStore {
         let mut stats = BackupGroupDeleteStats::default();
 
         for group in self.iter_backup_groups(ns.to_owned())? {
-            let delete_stats = group?.destroy()?;
+            let group = group?;
+            let backend = self.backend()?;
+            let delete_stats = group.destroy(&backend)?;
             stats.add(&delete_stats);
             removed_all_groups = removed_all_groups && delete_stats.all_removed();
         }
@@ -692,6 +700,8 @@ impl DataStore {
         let store = self.name();
         let mut removed_all_requested = true;
         let mut stats = BackupGroupDeleteStats::default();
+        let backend = self.backend()?;
+
         if delete_groups {
             log::info!("removing whole namespace recursively below {store}:/{ns}",);
             for ns in self.recursive_iter_backup_ns(ns.to_owned())? {
@@ -699,6 +709,24 @@ impl DataStore {
                 stats.add(&delete_stats);
                 removed_all_requested = removed_all_requested && removed_ns_groups;
             }
+
+            if let DatastoreBackend::S3(s3_client) = &backend {
+                let ns_dir = ns.path();
+                let ns_prefix = ns_dir
+                    .to_str()
+                    .ok_or_else(|| format_err!("invalid namespace path prefix"))?;
+                let prefix = format!("{S3_CONTENT_PREFIX}/{ns_prefix}");
+                let delete_objects_error = proxmox_async::runtime::block_on(
+                    s3_client.delete_objects_by_prefix_with_suffix_filter(
+                        &S3PathPrefix::Some(prefix),
+                        PROTECTED_MARKER_FILENAME,
+                        &[GROUP_OWNER_FILE_NAME, GROUP_NOTES_FILE_NAME],
+                    ),
+                )?;
+                if delete_objects_error {
+                    bail!("deleting objects failed");
+                }
+            }
         } else {
             log::info!("pruning empty namespace recursively below {store}:/{ns}");
         }
@@ -734,6 +762,14 @@ impl DataStore {
                         log::warn!("failed to remove namespace {ns} - {err}")
                     }
                 }
+                if let DatastoreBackend::S3(s3_client) = &backend {
+                    // Only remove the namespace marker, if it was empty,
+                    // than this is the same as the namespace being removed.
+                    let object_key =
+                        crate::s3::object_key_from_path(&ns.path(), NAMESPACE_MARKER_FILENAME)
+                            .context("invalid namespace marker object key")?;
+                    proxmox_async::runtime::block_on(s3_client.delete_object(object_key))?;
+                }
             }
         }
 
@@ -751,7 +787,7 @@ impl DataStore {
     ) -> Result<BackupGroupDeleteStats, Error> {
         let backup_group = self.backup_group(ns.clone(), backup_group.clone());
 
-        backup_group.destroy()
+        backup_group.destroy(&self.backend()?)
     }
 
     /// Remove a backup directory including all content
@@ -763,7 +799,7 @@ impl DataStore {
     ) -> Result<(), Error> {
         let backup_dir = self.backup_dir(ns.clone(), backup_dir.clone())?;
 
-        backup_dir.destroy(force)
+        backup_dir.destroy(force, &self.backend()?)
     }
 
     /// Returns the time of the last successful backup
@@ -791,7 +827,7 @@ impl DataStore {
         ns: &BackupNamespace,
         group: &pbs_api_types::BackupGroup,
     ) -> PathBuf {
-        self.group_path(ns, group).join("owner")
+        self.group_path(ns, group).join(GROUP_OWNER_FILE_NAME)
     }
 
     /// Returns the backup owner.
diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs
index c72d2dbc3..87a8641bd 100644
--- a/src/api2/admin/datastore.rs
+++ b/src/api2/admin/datastore.rs
@@ -422,7 +422,7 @@ pub async fn delete_snapshot(
 
         let snapshot = datastore.backup_dir(ns, backup_dir)?;
 
-        snapshot.destroy(false)?;
+        snapshot.destroy(false, &datastore.backend()?)?;
 
         Ok(Value::Null)
     })
@@ -1088,13 +1088,21 @@ pub fn prune(
             });
 
             if !keep {
-                if let Err(err) = backup_dir.destroy(false) {
-                    warn!(
-                        "failed to remove dir {:?}: {}",
-                        backup_dir.relative_path(),
-                        err,
-                    );
-                }
+                match datastore.backend() {
+                    Ok(backend) => {
+                        if let Err(err) = backup_dir.destroy(false, &backend) {
+                            warn!(
+                                "failed to remove dir {:?}: {}",
+                                backup_dir.relative_path(),
+                                err,
+                            );
+                        }
+                    }
+                    Err(err) => warn!(
+                        "failed to remove dir {:?}: {err}",
+                        backup_dir.relative_path()
+                    ),
+                };
             }
         }
         prune_result
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 21/46] datastore: get and set owner for s3 store backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (22 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 20/46] datastore: prune groups/snapshots from s3 object store backend Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 22/46] datastore: implement garbage collection for s3 backend Christian Ebner
                   ` (27 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Read or write the ownership information from/to the corresponding
object in the S3 object store. Keep that information available if
the bucket is reused as datastore.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use owner filename constant
- use .to_string() for group formatting
- use upload_replace_with_retry

 pbs-datastore/src/datastore.rs | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
index d5eb1ca8f..8bddafd5d 100644
--- a/pbs-datastore/src/datastore.rs
+++ b/pbs-datastore/src/datastore.rs
@@ -7,6 +7,7 @@ use std::sync::{Arc, LazyLock, Mutex};
 use std::time::Duration;
 
 use anyhow::{bail, format_err, Context, Error};
+use http_body_util::BodyExt;
 use nix::unistd::{unlinkat, UnlinkatFlags};
 use pbs_tools::lru_cache::LruCache;
 use tracing::{info, warn};
@@ -839,6 +840,21 @@ impl DataStore {
         backup_group: &pbs_api_types::BackupGroup,
     ) -> Result<Authid, Error> {
         let full_path = self.owner_path(ns, backup_group);
+
+        if let DatastoreBackend::S3(s3_client) = self.backend()? {
+            let mut path = ns.path();
+            path.push(backup_group.to_string());
+            let object_key = crate::s3::object_key_from_path(&path, GROUP_OWNER_FILE_NAME)
+                .context("invalid owner file object key")?;
+            let response = proxmox_async::runtime::block_on(s3_client.get_object(object_key))?
+                .ok_or_else(|| format_err!("fetching owner failed"))?;
+            let content = proxmox_async::runtime::block_on(response.content.collect())?;
+            let owner = String::from_utf8(content.to_bytes().trim_ascii_end().to_vec())?;
+            return owner
+                .parse()
+                .map_err(|err| format_err!("parsing owner for {backup_group} failed: {err}"));
+        }
+
         let owner = proxmox_sys::fs::file_read_firstline(full_path)?;
         owner
             .trim_end() // remove trailing newline
@@ -867,6 +883,18 @@ impl DataStore {
     ) -> Result<(), Error> {
         let path = self.owner_path(ns, backup_group);
 
+        if let DatastoreBackend::S3(s3_client) = self.backend()? {
+            let mut path = ns.path();
+            path.push(backup_group.to_string());
+            let object_key = crate::s3::object_key_from_path(&path, GROUP_OWNER_FILE_NAME)
+                .context("invalid owner file object key")?;
+            let data = hyper::body::Bytes::from(format!("{auth_id}\n"));
+            let _is_duplicate = proxmox_async::runtime::block_on(
+                s3_client.upload_replace_with_retry(object_key, data),
+            )
+            .context("failed to set owner on s3 backend")?;
+        }
+
         let mut open_options = std::fs::OpenOptions::new();
         open_options.write(true);
 
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 22/46] datastore: implement garbage collection for s3 backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (23 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 21/46] datastore: get and set owner for s3 " Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 23/46] ui: add datastore type selector and reorganize component layout Christian Ebner
                   ` (26 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Implements the garbage collection for datastores backed by an s3
object store.
Take advantage of the local datastore by placing marker files in the
chunk store during phase 1 of the garbage collection, updating their
atime if already present.
This allows us to avoid making expensive API calls to update object
metadata, which would only be possible via a copy object operation.

The phase 2 is implemented by fetching a list of all the chunks via
the ListObjectsV2 API call, filtered by the chunk folder prefix.
This operation has to be performed in batches of 1000 objects, given
by the APIs response limits.
For each object key, lookup the marker file and decide based on the
marker existence and it's atime if the chunk object needs to be
removed. Deletion happens via the delete objects operation, allowing
to delete multiple chunks by a single request.

This allows to efficiently lookup chunks which are not in use
anymore while being performant and cost effective.

Baseline runtime performance tests:
-----------------------------------

3 garbage collection runs were performed with hot filesystem caches
(by additional GC run before the test runs). The PBS instance was
virtualized, the same virtualized disk using ZFS for all the local
cache stores:

All datastores contained the same encrypted data, with the following
content statistics:
Original data usage: 269.685 GiB
On-Disk usage: 9.018 GiB (3.34%)
On-Disk chunks: 6477
Deduplication factor: 29.90
Average chunk size: 1.426 MiB

The resutlts demonstrate the overhead caused by the additional
ListObjectV2 API calls and their processing, but depending on the
object store backend.

Average garbage collection runtime:
Local datastore:             (2.04 ± 0.01) s
Local RADOS gateway (Squid): (3.05 ± 0.01) s
AWS S3:                      (3.05 ± 0.01) s
Cloudflare R2:               (6.71 ± 0.58) s

After pruning of all datastore contents (therefore including
DeleteObjects requests):
Local datastore:              3.04 s
Local RADOS gateway (Squid): 14.08 s
AWS S3:                      13.06 s
Cloudflare R2:               78.21 s

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- refactor s3 backend gc logic into more compact helper functions

 pbs-datastore/src/chunk_store.rs |   4 +
 pbs-datastore/src/datastore.rs   | 252 +++++++++++++++++++++++++++----
 2 files changed, 230 insertions(+), 26 deletions(-)

diff --git a/pbs-datastore/src/chunk_store.rs b/pbs-datastore/src/chunk_store.rs
index 8c195df54..95f00e8d5 100644
--- a/pbs-datastore/src/chunk_store.rs
+++ b/pbs-datastore/src/chunk_store.rs
@@ -353,6 +353,10 @@ impl ChunkStore {
         ProcessLocker::oldest_shared_lock(self.locker.clone().unwrap())
     }
 
+    pub fn mutex(&self) -> &std::sync::Mutex<()> {
+        &self.mutex
+    }
+
     pub fn sweep_unused_chunks(
         &self,
         oldest_writer: i64,
diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
index 8bddafd5d..c8c08010d 100644
--- a/pbs-datastore/src/datastore.rs
+++ b/pbs-datastore/src/datastore.rs
@@ -4,7 +4,7 @@ use std::os::unix::ffi::OsStrExt;
 use std::os::unix::io::AsRawFd;
 use std::path::{Path, PathBuf};
 use std::sync::{Arc, LazyLock, Mutex};
-use std::time::Duration;
+use std::time::{Duration, SystemTime};
 
 use anyhow::{bail, format_err, Context, Error};
 use http_body_util::BodyExt;
@@ -14,7 +14,7 @@ use tracing::{info, warn};
 
 use proxmox_human_byte::HumanByte;
 use proxmox_s3_client::{
-    S3Client, S3ClientConfig, S3ClientOptions, S3ClientSecretsConfig, S3PathPrefix,
+    S3Client, S3ClientConfig, S3ClientOptions, S3ClientSecretsConfig, S3ObjectKey, S3PathPrefix,
 };
 use proxmox_schema::ApiType;
 
@@ -1216,6 +1216,7 @@ impl DataStore {
         chunk_lru_cache: &mut Option<LruCache<[u8; 32], ()>>,
         status: &mut GarbageCollectionStatus,
         worker: &dyn WorkerTaskContext,
+        s3_client: Option<Arc<S3Client>>,
     ) -> Result<(), Error> {
         status.index_file_count += 1;
         status.index_data_bytes += index.index_bytes();
@@ -1238,21 +1239,41 @@ impl DataStore {
                 }
             }
 
-            if !self.inner.chunk_store.cond_touch_chunk(digest, false)? {
-                let hex = hex::encode(digest);
-                warn!(
-                    "warning: unable to access non-existent chunk {hex}, required by {file_name:?}"
-                );
-
-                // touch any corresponding .bad files to keep them around, meaning if a chunk is
-                // rewritten correctly they will be removed automatically, as well as if no index
-                // file requires the chunk anymore (won't get to this loop then)
-                for i in 0..=9 {
-                    let bad_ext = format!("{}.bad", i);
-                    let mut bad_path = PathBuf::new();
-                    bad_path.push(self.chunk_path(digest).0);
-                    bad_path.set_extension(bad_ext);
-                    self.inner.chunk_store.cond_touch_path(&bad_path, false)?;
+            match s3_client {
+                None => {
+                    // Filesystem backend
+                    if !self.inner.chunk_store.cond_touch_chunk(digest, false)? {
+                        let hex = hex::encode(digest);
+                        warn!(
+                            "warning: unable to access non-existent chunk {hex}, required by {file_name:?}"
+                        );
+
+                        // touch any corresponding .bad files to keep them around, meaning if a chunk is
+                        // rewritten correctly they will be removed automatically, as well as if no index
+                        // file requires the chunk anymore (won't get to this loop then)
+                        for i in 0..=9 {
+                            let bad_ext = format!("{}.bad", i);
+                            let mut bad_path = PathBuf::new();
+                            bad_path.push(self.chunk_path(digest).0);
+                            bad_path.set_extension(bad_ext);
+                            self.inner.chunk_store.cond_touch_path(&bad_path, false)?;
+                        }
+                    }
+                }
+                Some(ref _s3_client) => {
+                    // Update atime on local cache marker files.
+                    if !self.inner.chunk_store.cond_touch_chunk(digest, false)? {
+                        let (chunk_path, _digest) = self.chunk_path(digest);
+                        // Insert empty file as marker to tell GC phase2 that this is
+                        // a chunk still in-use, so to keep in the S3 object store.
+                        std::fs::File::options()
+                            .write(true)
+                            .create_new(true)
+                            .open(&chunk_path)
+                            .with_context(|| {
+                                format!("failed to create marker for chunk {}", hex::encode(digest))
+                            })?;
+                    }
                 }
             }
         }
@@ -1264,6 +1285,7 @@ impl DataStore {
         status: &mut GarbageCollectionStatus,
         worker: &dyn WorkerTaskContext,
         cache_capacity: usize,
+        s3_client: Option<Arc<S3Client>>,
     ) -> Result<(), Error> {
         // Iterate twice over the datastore to fetch index files, even if this comes with an
         // additional runtime cost:
@@ -1357,6 +1379,7 @@ impl DataStore {
                                 &mut chunk_lru_cache,
                                 status,
                                 worker,
+                                s3_client.as_ref().cloned(),
                             )?;
 
                             if !unprocessed_index_list.remove(&path) {
@@ -1391,7 +1414,14 @@ impl DataStore {
                     continue;
                 }
             };
-            self.index_mark_used_chunks(index, &path, &mut chunk_lru_cache, status, worker)?;
+            self.index_mark_used_chunks(
+                index,
+                &path,
+                &mut chunk_lru_cache,
+                status,
+                worker,
+                s3_client.as_ref().cloned(),
+            )?;
             warn!("Marked chunks for unexpected index file at '{path:?}'");
         }
         if strange_paths_count > 0 {
@@ -1490,18 +1520,104 @@ impl DataStore {
                 1024 * 1024
             };
 
-            info!("Start GC phase1 (mark used chunks)");
+            let s3_client = match self.backend()? {
+                DatastoreBackend::Filesystem => None,
+                DatastoreBackend::S3(s3_client) => {
+                    proxmox_async::runtime::block_on(s3_client.head_bucket())
+                        .context("failed to reach bucket")?;
+                    Some(s3_client)
+                }
+            };
 
-            self.mark_used_chunks(&mut gc_status, worker, gc_cache_capacity)
-                .context("marking used chunks failed")?;
+            info!("Start GC phase1 (mark used chunks)");
 
-            info!("Start GC phase2 (sweep unused chunks)");
-            self.inner.chunk_store.sweep_unused_chunks(
-                oldest_writer,
-                min_atime,
+            self.mark_used_chunks(
                 &mut gc_status,
                 worker,
-            )?;
+                gc_cache_capacity,
+                s3_client.as_ref().cloned(),
+            )
+            .context("marking used chunks failed")?;
+
+            info!("Start GC phase2 (sweep unused chunks)");
+
+            if let Some(ref s3_client) = s3_client {
+                let mut chunk_count = 0;
+                let prefix = S3PathPrefix::Some(".chunks/".to_string());
+                // Operates in batches of 1000 objects max per request
+                let mut list_bucket_result =
+                    proxmox_async::runtime::block_on(s3_client.list_objects_v2(&prefix, None))
+                        .context("failed to list chunk in s3 object store")?;
+
+                let mut delete_list = Vec::with_capacity(1000);
+                loop {
+                    let lock = self.inner.chunk_store.mutex().lock().unwrap();
+
+                    for content in list_bucket_result.contents {
+                        if self
+                            .mark_chunk_for_object_key(
+                                &content.key,
+                                content.size,
+                                min_atime,
+                                oldest_writer,
+                                &mut delete_list,
+                                &mut gc_status,
+                            )
+                            .with_context(|| {
+                                format!("failed to mark chunk for object key {}", content.key)
+                            })?
+                        {
+                            chunk_count += 1;
+                        }
+                    }
+
+                    if !delete_list.is_empty() {
+                        let delete_objects_result = proxmox_async::runtime::block_on(
+                            s3_client.delete_objects(&delete_list),
+                        )?;
+                        if let Some(_err) = delete_objects_result.error {
+                            bail!("failed to delete some objects");
+                        }
+                        delete_list.clear();
+                    }
+
+                    drop(lock);
+
+                    // Process next batch of chunks if there is more
+                    if list_bucket_result.is_truncated {
+                        list_bucket_result =
+                            proxmox_async::runtime::block_on(s3_client.list_objects_v2(
+                                &prefix,
+                                list_bucket_result.next_continuation_token.as_deref(),
+                            ))?;
+                        continue;
+                    }
+
+                    break;
+                }
+                info!("processed {chunk_count} total chunks");
+
+                // Phase 2 GC of Filesystem backed storage is phase 3 for S3 backed GC
+                info!("Start GC phase3 (sweep unused chunk markers)");
+
+                let mut tmp_gc_status = GarbageCollectionStatus {
+                    upid: Some(upid.to_string()),
+                    ..Default::default()
+                };
+                self.inner.chunk_store.sweep_unused_chunks(
+                    oldest_writer,
+                    min_atime,
+                    &mut tmp_gc_status,
+                    worker,
+                )?;
+            } else {
+                self.inner.chunk_store.sweep_unused_chunks(
+                    oldest_writer,
+                    min_atime,
+                    &mut gc_status,
+                    worker,
+                )?;
+            }
 
             if let Some(cache_stats) = &gc_status.cache_stats {
                 let total_cache_counts = cache_stats.hits + cache_stats.misses;
@@ -1588,6 +1704,90 @@ impl DataStore {
         Ok(())
     }
 
+    // Mark the chunk marker in the local cache store for the given object key as in use
+    // by updating it's atime.
+    // Returns Ok(true) if the chunk was updated and Ok(false) if the object was not a chunk.
+    fn mark_chunk_for_object_key(
+        &self,
+        object_key: &S3ObjectKey,
+        size: u64,
+        min_atime: i64,
+        oldest_writer: i64,
+        delete_list: &mut Vec<S3ObjectKey>,
+        gc_status: &mut GarbageCollectionStatus,
+    ) -> Result<bool, Error> {
+        let chunk_path = match self.chunk_path_from_object_key(&object_key) {
+            Some(path) => path,
+            None => return Ok(false),
+        };
+
+        // Check local markers (created or atime updated during phase1) and
+        // keep or delete chunk based on that.
+        let atime = match std::fs::metadata(&chunk_path) {
+            Ok(stat) => stat.accessed()?,
+            Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
+                // File not found, delete by setting atime to unix epoch
+                info!("Not found, mark for deletion: {object_key}");
+                SystemTime::UNIX_EPOCH
+            }
+            Err(err) => return Err(err.into()),
+        };
+        let atime = atime.duration_since(SystemTime::UNIX_EPOCH)?.as_secs() as i64;
+
+        let bad = chunk_path.as_path().ends_with(".bad");
+
+        if atime < min_atime {
+            delete_list.push(object_key.clone());
+            if bad {
+                gc_status.removed_bad += 1;
+            } else {
+                gc_status.removed_chunks += 1;
+            }
+            gc_status.removed_bytes += size;
+        } else if atime < oldest_writer {
+            if bad {
+                gc_status.still_bad += 1;
+            } else {
+                gc_status.pending_chunks += 1;
+            }
+            gc_status.pending_bytes += size;
+        } else {
+            if !bad {
+                gc_status.disk_chunks += 1;
+            }
+            gc_status.disk_bytes += size;
+        }
+
+        Ok(true)
+    }
+
+    // Check and generate a chunk path from given object key
+    fn chunk_path_from_object_key(&self, object_key: &S3ObjectKey) -> Option<PathBuf> {
+        // Check object is actually a chunk
+        let digest = match Path::new::<str>(object_key).file_name() {
+            Some(file_name) => file_name,
+            // should never be the case as objects will have a filename
+            None => return None,
+        };
+        let bytes = digest.as_bytes();
+        if bytes.len() != 64 && bytes.len() != 64 + ".0.bad".len() {
+            return None;
+        }
+        if !bytes.iter().take(64).all(u8::is_ascii_hexdigit) {
+            return None;
+        }
+
+        // Safe since contains valid ascii hexdigits only as checked above.
+        let digest_str = digest.to_string_lossy();
+        let hexdigit_prefix = unsafe { digest_str.get_unchecked(0..4) };
+        let mut chunk_path = self.base_path();
+        chunk_path.push(".chunks");
+        chunk_path.push(hexdigit_prefix);
+        chunk_path.push(digest);
+
+        Some(chunk_path)
+    }
+
     pub fn try_shared_chunk_store_lock(&self) -> Result<ProcessLockSharedGuard, Error> {
         self.inner.chunk_store.try_shared_lock()
     }
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel

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

* [pbs-devel] [PATCH proxmox-backup v9 23/46] ui: add datastore type selector and reorganize component layout
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (24 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 22/46] datastore: implement garbage collection for s3 backend Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 24/46] ui: add s3 client edit window for configuration create/edit Christian Ebner
                   ` (25 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

In preparation for adding the S3 backed datastore variant to the edit
window. Introduce a datastore type selector in order to distinguish
between creation of regular and removable datastores, instead of
using the checkbox as is currently the case.

This allows to more easily expand for further datastore type variants
while keeping the datastore edit window compact.

Since selecting the type is one of the first steps during datastore
creation, position the component right below the datastore name field
and re-organize the components related to the removable datastore
creation, while keeping additional required components for the S3
backed datastore creation in mind.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use gettext for datastore type combo items

 www/window/DataStoreEdit.js | 78 +++++++++++++++++++++----------------
 1 file changed, 45 insertions(+), 33 deletions(-)

diff --git a/www/window/DataStoreEdit.js b/www/window/DataStoreEdit.js
index 372984e37..c91b93cd9 100644
--- a/www/window/DataStoreEdit.js
+++ b/www/window/DataStoreEdit.js
@@ -52,6 +52,41 @@ Ext.define('PBS.DataStoreEdit', {
                         allowBlank: false,
                         fieldLabel: gettext('Name'),
                     },
+                    {
+                        xtype: 'proxmoxKVComboBox',
+                        name: 'datastore-type',
+                        fieldLabel: gettext('Datastore Type'),
+                        value: '__default__',
+                        submitValue: false,
+                        comboItems: [
+                            ['__default__', gettext('Local')],
+                            ['removable', gettext('Removable')],
+                        ],
+                        cbind: {
+                            disabled: '{!isCreate}',
+                        },
+                        listeners: {
+                            change: function (checkbox, selected) {
+                                let isRemovable = selected === 'removable';
+
+                                let inputPanel = checkbox.up('inputpanel');
+                                let pathField = inputPanel.down('[name=path]');
+                                let uuidEditField = inputPanel.down('[name=backing-device]');
+
+                                uuidEditField.setDisabled(!isRemovable);
+                                uuidEditField.allowBlank = !isRemovable;
+                                uuidEditField.setValue('');
+
+                                if (isRemovable) {
+                                    pathField.setFieldLabel(gettext('Path on Device'));
+                                    pathField.setEmptyText(gettext('A relative path'));
+                                } else {
+                                    pathField.setFieldLabel(gettext('Backing Path'));
+                                    pathField.setEmptyText(gettext('An absolute path'));
+                                }
+                            },
+                        },
+                    },
                     {
                         xtype: 'pmxDisplayEditField',
                         cbind: {
@@ -63,17 +98,6 @@ Ext.define('PBS.DataStoreEdit', {
                         emptyText: gettext('An absolute path'),
                         validator: (val) => val?.trim() !== '/',
                     },
-                    {
-                        xtype: 'pbsPartitionSelector',
-                        fieldLabel: gettext('Device'),
-                        name: 'backing-device',
-                        disabled: true,
-                        allowBlank: true,
-                        cbind: {
-                            editable: '{isCreate}',
-                        },
-                        emptyText: gettext('Device path'),
-                    },
                 ],
                 column2: [
                     {
@@ -97,31 +121,19 @@ Ext.define('PBS.DataStoreEdit', {
                             value: '{scheduleValue}',
                         },
                     },
-                ],
-                columnB: [
                     {
-                        xtype: 'checkbox',
-                        boxLabel: gettext('Removable datastore'),
-                        submitValue: false,
-                        listeners: {
-                            change: function (checkbox, isRemovable) {
-                                let inputPanel = checkbox.up('inputpanel');
-                                let pathField = inputPanel.down('[name=path]');
-                                let uuidEditField = inputPanel.down('[name=backing-device]');
-
-                                uuidEditField.setDisabled(!isRemovable);
-                                uuidEditField.allowBlank = !isRemovable;
-                                uuidEditField.setValue('');
-                                if (isRemovable) {
-                                    pathField.setFieldLabel(gettext('Path on Device'));
-                                    pathField.setEmptyText(gettext('A relative path'));
-                                } else {
-                                    pathField.setFieldLabel(gettext('Backing Path'));
-                                    pathField.setEmptyText(gettext('An absolute path'));
-                                }
-                            },
+                        xtype: 'pbsPartitionSelector',
+                        fieldLabel: gettext('Device'),
+                        name: 'backing-device',
+                        disabled: true,
+                        allowBlank: true,
+                        cbind: {
+                            editable: '{isCreate}',
                         },
+                        emptyText: gettext('Device path'),
                     },
+                ],
+                columnB: [
                     {
                         xtype: 'textfield',
                         name: 'comment',
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 24/46] ui: add s3 client edit window for configuration create/edit
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (25 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 23/46] ui: add datastore type selector and reorganize component layout Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 25/46] ui: add s3 client view for configuration Christian Ebner
                   ` (24 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Adds an edit window for creating or editing S3 client configurations.
Loosely based on the same edit window for the remote configuration.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- no changes

 www/window/S3ClientEdit.js | 148 +++++++++++++++++++++++++++++++++++++
 1 file changed, 148 insertions(+)
 create mode 100644 www/window/S3ClientEdit.js

diff --git a/www/window/S3ClientEdit.js b/www/window/S3ClientEdit.js
new file mode 100644
index 000000000..b22e920f8
--- /dev/null
+++ b/www/window/S3ClientEdit.js
@@ -0,0 +1,148 @@
+Ext.define('PBS.window.S3ClientEdit', {
+    extend: 'Proxmox.window.Edit',
+    alias: 'widget.pbsS3ClientEdit',
+    mixins: ['Proxmox.Mixin.CBind'],
+
+    onlineHelp: 'backup_s3client',
+
+    isAdd: true,
+
+    subject: gettext('S3 Client'),
+
+    fieldDefaults: { labelWidth: 120 },
+
+    cbindData: function (initialConfig) {
+        let me = this;
+
+        let baseurl = '/api2/extjs/config/s3';
+        let id = initialConfig.id;
+
+        me.isCreate = !id;
+        me.url = id ? `${baseurl}/${id}` : baseurl;
+        me.method = id ? 'PUT' : 'POST';
+        me.autoLoad = !!id;
+        return {
+            passwordEmptyText: me.isCreate ? '' : gettext('Unchanged'),
+        };
+    },
+
+    items: {
+        xtype: 'inputpanel',
+        column1: [
+            {
+                xtype: 'pmxDisplayEditField',
+                name: 'id',
+                fieldLabel: gettext('S3 Client ID'),
+                renderer: Ext.htmlEncode,
+                allowBlank: false,
+                minLength: 4,
+                cbind: {
+                    editable: '{isCreate}',
+                },
+            },
+            {
+                xtype: 'proxmoxtextfield',
+                name: 'endpoint',
+                fieldLabel: gettext('Endpoint'),
+                allowBlank: false,
+                emptyText: gettext('e.g. {{bucket}}.s3.{{region}}.amazonaws.com'),
+                autoEl: {
+                    tag: 'div',
+                    'data-qtip': gettext(
+                        'IP or FQDN S3 endpoint (allows {{bucket}} or {{region}} templating)',
+                    ),
+                },
+            },
+            {
+                xtype: 'proxmoxtextfield',
+                name: 'port',
+                fieldLabel: gettext('Port'),
+                emptyText: gettext('default (443)'),
+                cbind: {
+                    deleteEmpty: '{!isCreate}',
+                },
+            },
+            {
+                xtype: 'proxmoxcheckbox',
+                name: 'path-style',
+                fieldLabel: gettext('Path Style'),
+                autoEl: {
+                    tag: 'div',
+                    'data-qtip': gettext('Use path style over vhost style bucket addressing.'),
+                },
+                uncheckedValue: false,
+                value: false,
+            },
+        ],
+
+        column2: [
+            {
+                xtype: 'proxmoxtextfield',
+                name: 'region',
+                fieldLabel: gettext('Region'),
+                emptyText: gettext('default (us-west-1)'),
+                cbind: {
+                    deleteEmpty: '{!isCreate}',
+                },
+            },
+            {
+                xtype: 'proxmoxtextfield',
+                name: 'access-key',
+                fieldLabel: gettext('Access Key'),
+                cbind: {
+                    emptyText: '{passwordEmptyText}',
+                    allowBlank: '{!isCreate}',
+                },
+            },
+            {
+                xtype: 'textfield',
+                name: 'secret-key',
+                inputType: 'password',
+                fieldLabel: gettext('Secret Key'),
+                cbind: {
+                    emptyText: '{passwordEmptyText}',
+                    allowBlank: '{!isCreate}',
+                },
+            },
+        ],
+
+        columnB: [
+            {
+                xtype: 'proxmoxtextfield',
+                name: 'fingerprint',
+                fieldLabel: gettext('Fingerprint'),
+                emptyText: gettext(
+                    "Server certificate's SHA-256 fingerprint, required for self-signed certificates",
+                ),
+                cbind: {
+                    deleteEmpty: '{!isCreate}',
+                },
+            },
+        ],
+    },
+
+    getValues: function () {
+        let me = this;
+        let values = me.callParent(arguments);
+
+        if (me.isCreate) {
+            /// Secrets are stored into separate config, but set the same id for both configs
+            values['secrets-id'] = values.id;
+        }
+
+        if (values.delete && !Ext.isArray(values.delete)) {
+            values.delete = values.delete.split(',');
+        }
+        PBS.Utils.delete_if_default(values, 'path-style', false, me.isCreate);
+
+        if (values['access-key'] === '') {
+            delete values['access-key'];
+        }
+
+        if (values['secret-key'] === '') {
+            delete values['secret-key'];
+        }
+
+        return values;
+    },
+});
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 25/46] ui: add s3 client view for configuration
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (26 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 24/46] ui: add s3 client edit window for configuration create/edit Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 26/46] ui: expose the s3 client view in the navigation tree Christian Ebner
                   ` (23 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Adds the view to configure S3 clients in the Configuration section of
the UI.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- no changes

 www/config/S3ClientView.js | 141 +++++++++++++++++++++++++++++++++++++
 1 file changed, 141 insertions(+)
 create mode 100644 www/config/S3ClientView.js

diff --git a/www/config/S3ClientView.js b/www/config/S3ClientView.js
new file mode 100644
index 000000000..15236960b
--- /dev/null
+++ b/www/config/S3ClientView.js
@@ -0,0 +1,141 @@
+Ext.define('pmx-s3client', {
+    extend: 'Ext.data.Model',
+    fields: ['id', 'endpoint', 'port', 'access-key', 'secret-key', 'region', 'fingerprint'],
+    idProperty: 'id',
+    proxy: {
+        type: 'proxmox',
+        url: '/api2/json/config/s3',
+    },
+});
+
+Ext.define('PBS.config.S3ClientView', {
+    extend: 'Ext.grid.GridPanel',
+    alias: 'widget.pbsS3ClientView',
+
+    title: gettext('S3 Clients'),
+
+    stateful: true,
+    stateId: 'grid-s3clients',
+    tools: [PBS.Utils.get_help_tool('backup-s3-client')],
+
+    controller: {
+        xclass: 'Ext.app.ViewController',
+
+        addS3Client: function () {
+            let me = this;
+            Ext.create('PBS.window.S3ClientEdit', {
+                listeners: {
+                    destroy: function () {
+                        me.reload();
+                    },
+                },
+            }).show();
+        },
+
+        editS3Client: function () {
+            let me = this;
+            let view = me.getView();
+            let selection = view.getSelection();
+            if (selection.length < 1) {
+                return;
+            }
+
+            Ext.create('PBS.window.S3ClientEdit', {
+                id: selection[0].data.id,
+                listeners: {
+                    destroy: function () {
+                        me.reload();
+                    },
+                },
+            }).show();
+        },
+
+        reload: function () {
+            this.getView().getStore().rstore.load();
+        },
+
+        init: function (view) {
+            Proxmox.Utils.monStoreErrors(view, view.getStore().rstore);
+        },
+    },
+
+    listeners: {
+        activate: 'reload',
+        itemdblclick: 'editS3Client',
+    },
+
+    store: {
+        type: 'diff',
+        autoDestroy: true,
+        autoDestroyRstore: true,
+        sorters: 'id',
+        rstore: {
+            type: 'update',
+            storeid: 'pmx-s3client',
+            model: 'pmx-s3client',
+            autoStart: true,
+            interval: 5000,
+        },
+    },
+
+    tbar: [
+        {
+            xtype: 'proxmoxButton',
+            text: gettext('Add'),
+            handler: 'addS3Client',
+            selModel: false,
+        },
+        {
+            xtype: 'proxmoxButton',
+            text: gettext('Edit'),
+            handler: 'editS3Client',
+            disabled: true,
+        },
+        {
+            xtype: 'proxmoxStdRemoveButton',
+            baseurl: '/config/s3',
+            callback: 'reload',
+        },
+    ],
+
+    viewConfig: {
+        trackOver: false,
+    },
+
+    columns: [
+        {
+            dataIndex: 'id',
+            header: gettext('S3 Client ID'),
+            renderer: Ext.String.htmlEncode,
+            sortable: true,
+            width: 200,
+        },
+        {
+            dataIndex: 'endpoint',
+            header: gettext('Endpoint'),
+            sortable: true,
+            width: 200,
+        },
+        {
+            dataIndex: 'port',
+            header: gettext('Port'),
+            renderer: Ext.String.htmlEncode,
+            sortable: true,
+            width: 100,
+        },
+        {
+            dataIndex: 'region',
+            header: gettext('Region'),
+            renderer: Ext.String.htmlEncode,
+            sortable: true,
+            width: 100,
+        },
+        {
+            dataIndex: 'fingerprint',
+            header: gettext('Fingerprint'),
+            renderer: Ext.String.htmlEncode,
+            sortable: false,
+            flex: 1,
+        },
+    ],
+});
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 26/46] ui: expose the s3 client view in the navigation tree
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (27 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 25/46] ui: add s3 client view for configuration Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 27/46] ui: add s3 client selector and bucket field for s3 backend setup Christian Ebner
                   ` (22 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Add a `S3 Clients` item to the navigation tree to allow accessing the
S3 client configuration view and edit windows.

Adds the required source files to the Makefile.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- no changes

 www/Makefile          | 2 ++
 www/NavigationTree.js | 6 ++++++
 2 files changed, 8 insertions(+)

diff --git a/www/Makefile b/www/Makefile
index ab9946be0..767713c75 100644
--- a/www/Makefile
+++ b/www/Makefile
@@ -61,6 +61,7 @@ JSSRC=							\
 	config/RemoteView.js				\
 	config/TrafficControlView.js			\
 	config/ACLView.js				\
+	config/S3ClientView.js				\
 	config/SyncView.js				\
 	config/VerifyView.js				\
 	config/PruneView.js				\
@@ -85,6 +86,7 @@ JSSRC=							\
 	window/PruneJobEdit.js				\
 	window/GCJobEdit.js				\
 	window/UserEdit.js				\
+	window/S3ClientEdit.js				\
 	window/Settings.js				\
 	window/TokenEdit.js				\
 	window/VerifyJobEdit.js				\
diff --git a/www/NavigationTree.js b/www/NavigationTree.js
index aac9bd1b2..f445da49d 100644
--- a/www/NavigationTree.js
+++ b/www/NavigationTree.js
@@ -80,6 +80,12 @@ Ext.define('PBS.store.NavigationStore', {
                         path: 'pbsSubscription',
                         leaf: true,
                     },
+                    {
+                        text: gettext('S3 Clients'),
+                        iconCls: 'fa fa-cloud-upload',
+                        path: 'pbsS3ClientView',
+                        leaf: true,
+                    },
                 ],
             },
             {
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 27/46] ui: add s3 client selector and bucket field for s3 backend setup
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (28 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 26/46] ui: expose the s3 client view in the navigation tree Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 28/46] tools: lru cache: add removed callback for evicted cache nodes Christian Ebner
                   ` (21 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

In order to be able to create datastore with an s3 object store
backend. Implements a s3 client selector and exposes it in the
datastore edit window, together with the additional bucket name field
to associate with the datastore's s3 backend.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use gettext for datastore type combo items

 www/Makefile                 |  1 +
 www/form/S3ClientSelector.js | 33 +++++++++++++++++++++++++++
 www/window/DataStoreEdit.js  | 44 ++++++++++++++++++++++++++++++++++++
 3 files changed, 78 insertions(+)
 create mode 100644 www/form/S3ClientSelector.js

diff --git a/www/Makefile b/www/Makefile
index 767713c75..410e9f3e0 100644
--- a/www/Makefile
+++ b/www/Makefile
@@ -42,6 +42,7 @@ JSSRC=							\
 	Schema.js					\
 	form/TokenSelector.js				\
 	form/AuthidSelector.js				\
+	form/S3ClientSelector.js			\
 	form/RemoteSelector.js				\
 	form/RemoteTargetSelector.js   			\
 	form/DataStoreSelector.js			\
diff --git a/www/form/S3ClientSelector.js b/www/form/S3ClientSelector.js
new file mode 100644
index 000000000..243484909
--- /dev/null
+++ b/www/form/S3ClientSelector.js
@@ -0,0 +1,33 @@
+Ext.define('PBS.form.S3ClientSelector', {
+    extend: 'Proxmox.form.ComboGrid',
+    alias: 'widget.pbsS3ClientSelector',
+
+    allowBlank: false,
+    autoSelect: false,
+    valueField: 'id',
+    displayField: 'id',
+
+    store: {
+        model: 'pmx-s3client',
+        autoLoad: true,
+        sorters: 'id',
+    },
+
+    listConfig: {
+        columns: [
+            {
+                header: gettext('S3 Client ID'),
+                sortable: true,
+                dataIndex: 'id',
+                renderer: Ext.String.htmlEncode,
+                flex: 1,
+            },
+            {
+                header: gettext('Endpoint'),
+                sortable: true,
+                dataIndex: 'endpoint',
+                flex: 1,
+            },
+        ],
+    },
+});
diff --git a/www/window/DataStoreEdit.js b/www/window/DataStoreEdit.js
index c91b93cd9..9826313b2 100644
--- a/www/window/DataStoreEdit.js
+++ b/www/window/DataStoreEdit.js
@@ -61,6 +61,7 @@ Ext.define('PBS.DataStoreEdit', {
                         comboItems: [
                             ['__default__', gettext('Local')],
                             ['removable', gettext('Removable')],
+                            ['s3', gettext('S3 (experimental)')],
                         ],
                         cbind: {
                             disabled: '{!isCreate}',
@@ -68,18 +69,32 @@ Ext.define('PBS.DataStoreEdit', {
                         listeners: {
                             change: function (checkbox, selected) {
                                 let isRemovable = selected === 'removable';
+                                let isS3 = selected === 's3';
 
                                 let inputPanel = checkbox.up('inputpanel');
                                 let pathField = inputPanel.down('[name=path]');
                                 let uuidEditField = inputPanel.down('[name=backing-device]');
+                                let bucketField = inputPanel.down('[name=bucket]');
+                                let s3ClientSelector = inputPanel.down('[name=s3client]');
 
                                 uuidEditField.setDisabled(!isRemovable);
                                 uuidEditField.allowBlank = !isRemovable;
                                 uuidEditField.setValue('');
 
+                                bucketField.setDisabled(!isS3);
+                                bucketField.allowBlank = !isS3;
+                                bucketField.setValue('');
+
+                                s3ClientSelector.setDisabled(!isS3);
+                                s3ClientSelector.allowBlank = !isS3;
+                                s3ClientSelector.setValue('');
+
                                 if (isRemovable) {
                                     pathField.setFieldLabel(gettext('Path on Device'));
                                     pathField.setEmptyText(gettext('A relative path'));
+                                } else if (isS3) {
+                                    pathField.setFieldLabel(gettext('Store Cache'));
+                                    pathField.setEmptyText(gettext('An absolute path'));
                                 } else {
                                     pathField.setFieldLabel(gettext('Backing Path'));
                                     pathField.setEmptyText(gettext('An absolute path'));
@@ -98,6 +113,15 @@ Ext.define('PBS.DataStoreEdit', {
                         emptyText: gettext('An absolute path'),
                         validator: (val) => val?.trim() !== '/',
                     },
+                    {
+                        xtype: 'pbsS3ClientSelector',
+                        name: 's3client',
+                        fieldLabel: gettext('S3 Client ID'),
+                        disabled: true,
+                        cbind: {
+                            editable: '{isCreate}',
+                        },
+                    },
                 ],
                 column2: [
                     {
@@ -132,6 +156,13 @@ Ext.define('PBS.DataStoreEdit', {
                         },
                         emptyText: gettext('Device path'),
                     },
+                    {
+                        xtype: 'proxmoxtextfield',
+                        name: 'bucket',
+                        fieldLabel: gettext('Bucket'),
+                        allowBlank: false,
+                        disabled: true,
+                    },
                 ],
                 columnB: [
                     {
@@ -154,7 +185,20 @@ Ext.define('PBS.DataStoreEdit', {
                     if (me.isCreate) {
                         // New datastores default to using the notification system
                         values['notification-mode'] = 'notification-system';
+
+                        if (values.s3client) {
+                            let s3BackendConf = {
+                                type: 's3',
+                                client: values.s3client,
+                                bucket: values.bucket,
+                            };
+                            values.backend = PBS.Utils.printPropertyString(s3BackendConf);
+                        }
                     }
+
+                    delete values.s3client;
+                    delete values.bucket;
+
                     return values;
                 },
             },
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 28/46] tools: lru cache: add removed callback for evicted cache nodes
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (29 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 27/46] ui: add s3 client selector and bucket field for s3 backend setup Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 29/46] tools: async lru cache: implement insert, remove and contains methods Christian Ebner
                   ` (20 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Add a callback function to be executed on evicted cache nodes. The
callback gets the key of the removed node, allowing to externally act
based on that value.

Since the callback might fail, extend the current LRU cache api to
return an error on insert, covering the error for the `removed`
callback.

Async lru cache, callsites and tests are adapted to include the
additional callback parameter accordingly.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- change due to rebase onto current master (gc cache stats)

 pbs-datastore/src/cached_chunk_reader.rs |  6 +++-
 pbs-datastore/src/datastore.rs           |  2 +-
 pbs-datastore/src/dynamic_index.rs       |  1 +
 pbs-tools/src/async_lru_cache.rs         | 23 +++++++++----
 pbs-tools/src/lru_cache.rs               | 42 +++++++++++++++---------
 5 files changed, 50 insertions(+), 24 deletions(-)

diff --git a/pbs-datastore/src/cached_chunk_reader.rs b/pbs-datastore/src/cached_chunk_reader.rs
index be7f2a1e2..95ac23a54 100644
--- a/pbs-datastore/src/cached_chunk_reader.rs
+++ b/pbs-datastore/src/cached_chunk_reader.rs
@@ -81,7 +81,11 @@ impl<I: IndexFile, R: AsyncReadChunk + Send + Sync + 'static> CachedChunkReader<
                 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 data = self
+                    .cache
+                    .access(info.digest, &self.cacher, |_| Ok(()))
+                    .await?
+                    .unwrap();
 
                 let want_bytes = ((info.range.end - cur_offset) as usize).min(size - read);
                 let slice = &mut buf[read..(read + want_bytes)];
diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
index c8c08010d..fdb30b4d6 100644
--- a/pbs-datastore/src/datastore.rs
+++ b/pbs-datastore/src/datastore.rs
@@ -1228,7 +1228,7 @@ impl DataStore {
 
             // Avoid multiple expensive atime updates by utimensat
             if let Some(chunk_lru_cache) = chunk_lru_cache {
-                if chunk_lru_cache.insert(*digest, ()) {
+                if chunk_lru_cache.insert(*digest, (), |_| Ok(()))? {
                     if let Some(cache_stats) = status.cache_stats.as_mut() {
                         cache_stats.hits += 1;
                     }
diff --git a/pbs-datastore/src/dynamic_index.rs b/pbs-datastore/src/dynamic_index.rs
index b1d85a049..83e13b311 100644
--- a/pbs-datastore/src/dynamic_index.rs
+++ b/pbs-datastore/src/dynamic_index.rs
@@ -599,6 +599,7 @@ impl<S: ReadChunk> BufferedDynamicReader<S> {
                     store: &mut self.store,
                     index: &self.index,
                 },
+                |_| Ok(()),
             )?
             .ok_or_else(|| format_err!("chunk not found by cacher"))?;
 
diff --git a/pbs-tools/src/async_lru_cache.rs b/pbs-tools/src/async_lru_cache.rs
index c43b87717..141114933 100644
--- a/pbs-tools/src/async_lru_cache.rs
+++ b/pbs-tools/src/async_lru_cache.rs
@@ -42,7 +42,16 @@ impl<K: std::cmp::Eq + std::hash::Hash + Copy, V: Clone + Send + 'static> AsyncL
     /// Access an item either via the cache or by calling cacher.fetch. A return value of Ok(None)
     /// means the item requested has no representation, Err(_) means a call to fetch() failed,
     /// regardless of whether it was initiated by this call or a previous one.
-    pub async fn access(&self, key: K, cacher: &dyn AsyncCacher<K, V>) -> Result<Option<V>, Error> {
+    /// Calls the removed callback on  the evicted item, if any.
+    pub async fn access<F>(
+        &self,
+        key: K,
+        cacher: &dyn AsyncCacher<K, V>,
+        removed: F,
+    ) -> Result<Option<V>, Error>
+    where
+        F: Fn(K) -> Result<(), Error>,
+    {
         let (owner, result_fut) = {
             // check if already requested
             let mut maps = self.maps.lock().unwrap();
@@ -71,7 +80,7 @@ impl<K: std::cmp::Eq + std::hash::Hash + Copy, V: Clone + Send + 'static> AsyncL
             // this call was the one initiating the request, put into LRU and remove from map
             let mut maps = self.maps.lock().unwrap();
             if let Ok(Some(ref value)) = result {
-                maps.0.insert(key, value.clone());
+                maps.0.insert(key, value.clone(), removed)?;
             }
             maps.1.remove(&key);
         }
@@ -106,15 +115,15 @@ mod test {
             let cache: AsyncLruCache<i32, String> = AsyncLruCache::new(2);
 
             assert_eq!(
-                cache.access(10, &cacher).await.unwrap(),
+                cache.access(10, &cacher, |_| Ok(())).await.unwrap(),
                 Some("x10".to_string())
             );
             assert_eq!(
-                cache.access(20, &cacher).await.unwrap(),
+                cache.access(20, &cacher, |_| Ok(())).await.unwrap(),
                 Some("x20".to_string())
             );
             assert_eq!(
-                cache.access(30, &cacher).await.unwrap(),
+                cache.access(30, &cacher, |_| Ok(())).await.unwrap(),
                 Some("x30".to_string())
             );
 
@@ -123,14 +132,14 @@ mod test {
                 tokio::spawn(async move {
                     let cacher = TestAsyncCacher { prefix: "y" };
                     assert_eq!(
-                        c.access(40, &cacher).await.unwrap(),
+                        c.access(40, &cacher, |_| Ok(())).await.unwrap(),
                         Some("y40".to_string())
                     );
                 });
             }
 
             assert_eq!(
-                cache.access(20, &cacher).await.unwrap(),
+                cache.access(20, &cacher, |_| Ok(())).await.unwrap(),
                 Some("x20".to_string())
             );
         });
diff --git a/pbs-tools/src/lru_cache.rs b/pbs-tools/src/lru_cache.rs
index 94757bbf7..a7aea6528 100644
--- a/pbs-tools/src/lru_cache.rs
+++ b/pbs-tools/src/lru_cache.rs
@@ -60,10 +60,10 @@ impl<K, V> CacheNode<K, V> {
 /// assert_eq!(cache.get_mut(1), None);
 /// assert_eq!(cache.len(), 0);
 ///
-/// cache.insert(1, 1);
-/// cache.insert(2, 2);
-/// cache.insert(3, 3);
-/// cache.insert(4, 4);
+/// cache.insert(1, 1, |_| Ok(()));
+/// cache.insert(2, 2, |_| Ok(()));
+/// cache.insert(3, 3, |_| Ok(()));
+/// cache.insert(4, 4, |_| Ok(()));
 /// assert_eq!(cache.len(), 3);
 ///
 /// assert_eq!(cache.get_mut(1), None);
@@ -77,9 +77,9 @@ impl<K, V> CacheNode<K, V> {
 /// assert_eq!(cache.len(), 0);
 /// assert_eq!(cache.get_mut(2), None);
 /// // access will fill in missing cache entry by fetching from LruCacher
-/// assert_eq!(cache.access(2, &mut LruCacher {}).unwrap(), Some(&mut 2));
+/// assert_eq!(cache.access(2, &mut LruCacher {}, |_| Ok(())).unwrap(), Some(&mut 2));
 ///
-/// cache.insert(1, 1);
+/// cache.insert(1, 1, |_| Ok(()));
 /// assert_eq!(cache.get_mut(1), Some(&mut 1));
 ///
 /// cache.clear();
@@ -135,7 +135,10 @@ impl<K: std::cmp::Eq + std::hash::Hash + Copy, V> LruCache<K, V> {
 
     /// Insert or update an entry identified by `key` with the given `value`.
     /// This entry is placed as the most recently used node at the head.
-    pub fn insert(&mut self, key: K, value: V) -> bool {
+    pub fn insert<F>(&mut self, key: K, value: V, removed: F) -> Result<bool, anyhow::Error>
+    where
+        F: Fn(K) -> Result<(), anyhow::Error>,
+    {
         match self.map.entry(key) {
             Entry::Occupied(mut o) => {
                 // Node present, update value
@@ -144,7 +147,7 @@ impl<K: std::cmp::Eq + std::hash::Hash + Copy, V> LruCache<K, V> {
                 let mut node = unsafe { Box::from_raw(node_ptr) };
                 node.value = value;
                 let _node_ptr = Box::into_raw(node);
-                true
+                Ok(true)
             }
             Entry::Vacant(v) => {
                 // Node not present, insert a new one
@@ -160,9 +163,11 @@ impl<K: std::cmp::Eq + std::hash::Hash + Copy, V> LruCache<K, V> {
                 // avoid borrow conflict. This means there are temporarily
                 // self.capacity + 1 cache nodes.
                 if self.map.len() > self.capacity {
-                    self.pop_tail();
+                    if let Some(removed_node) = self.pop_tail() {
+                        removed(removed_node)?;
+                    }
                 }
-                false
+                Ok(false)
             }
         }
     }
@@ -176,11 +181,12 @@ impl<K: std::cmp::Eq + std::hash::Hash + Copy, V> LruCache<K, V> {
     }
 
     /// Remove the least recently used node from the cache.
-    fn pop_tail(&mut self) {
+    fn pop_tail(&mut self) -> Option<K> {
         if let Some(old_tail) = self.list.pop_tail() {
             // Remove HashMap entry for old tail
-            self.map.remove(&old_tail.key);
+            return self.map.remove(&old_tail.key).map(|_| old_tail.key);
         }
+        None
     }
 
     /// Get a mutable reference to the value identified by `key`.
@@ -208,11 +214,15 @@ impl<K: std::cmp::Eq + std::hash::Hash + Copy, V> LruCache<K, V> {
     /// value.
     /// If fetch returns a value, it is inserted as the most recently used entry
     /// in the cache.
-    pub fn access<'a>(
+    pub fn access<'a, F>(
         &'a mut self,
         key: K,
         cacher: &mut dyn Cacher<K, V>,
-    ) -> Result<Option<&'a mut V>, anyhow::Error> {
+        removed: F,
+    ) -> Result<Option<&'a mut V>, anyhow::Error>
+    where
+        F: Fn(K) -> Result<(), anyhow::Error>,
+    {
         match self.map.entry(key) {
             Entry::Occupied(mut o) => {
                 // Cache hit, birng node to front of list
@@ -236,7 +246,9 @@ impl<K: std::cmp::Eq + std::hash::Hash + Copy, V> LruCache<K, V> {
                         // avoid borrow conflict. This means there are temporarily
                         // self.capacity + 1 cache nodes.
                         if self.map.len() > self.capacity {
-                            self.pop_tail();
+                            if let Some(removed_node) = self.pop_tail() {
+                                removed(removed_node)?;
+                            }
                         }
                     }
                 }
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 29/46] tools: async lru cache: implement insert, remove and contains methods
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (30 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 28/46] tools: lru cache: add removed callback for evicted cache nodes Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 30/46] datastore: add local datastore cache for network attached storages Christian Ebner
                   ` (19 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Add methods to insert new cache entries without using the cacher,
remove cache entries given their key and check if the cache contains
a key, marking it the most recently used one if it does.

These methods will be used to implement the local datastore cache
which stores the values (chunks) on the filesystem rather than
keeping track of them by storing them in-memory in the cache. The lru
cache will only be used to allow for fast lookup and keep track of
the lookup order.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- no changes

 pbs-tools/src/async_lru_cache.rs | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/pbs-tools/src/async_lru_cache.rs b/pbs-tools/src/async_lru_cache.rs
index 141114933..3a975de32 100644
--- a/pbs-tools/src/async_lru_cache.rs
+++ b/pbs-tools/src/async_lru_cache.rs
@@ -87,6 +87,29 @@ impl<K: std::cmp::Eq + std::hash::Hash + Copy, V: Clone + Send + 'static> AsyncL
 
         result
     }
+
+    /// Insert an item as the most recently used one into the cache, calling the removed callback
+    /// on the evicted cache item, if any.
+    pub fn insert<F>(&self, key: K, value: V, removed: F) -> Result<(), Error>
+    where
+        F: Fn(K) -> Result<(), Error>,
+    {
+        let mut maps = self.maps.lock().unwrap();
+        maps.0.insert(key, value.clone(), removed)?;
+        Ok(())
+    }
+
+    /// Check if the item exists and if so, mark it as the most recently uses one.
+    pub fn contains(&self, key: K) -> bool {
+        let mut maps = self.maps.lock().unwrap();
+        maps.0.get_mut(key).is_some()
+    }
+
+    /// Remove the item from the cache.
+    pub fn remove(&self, key: K) {
+        let mut maps = self.maps.lock().unwrap();
+        maps.0.remove(key);
+    }
 }
 
 #[cfg(test)]
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 30/46] datastore: add local datastore cache for network attached storages
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (31 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 29/46] tools: async lru cache: implement insert, remove and contains methods Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 31/46] api: backup: use local datastore cache on s3 backend chunk upload Christian Ebner
                   ` (18 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Use a local datastore as cache using LRU cache replacement policy for
operations on a datastore backed by a network, e.g. by an S3 object
store backend. The goal is to reduce number of requests to the
backend and thereby save costs (monetary as well as time).

Cached chunks are stored on the local datastore cache, already
containing the datastore's contents metadata (namespace, group,
snapshot, owner, index files, ecc..), used to perform fast lookups.
The cache itself only stores chunk digests, not the raw data itself.
When payload data is required, contents are looked up and read from
the local datastore cache filesystem, including fallback to fetch from
the backend if the presumably cached entry is not found.

The cacher allows to fetch cache items on cache misses via the access
method.

The capacity of the cache is derived from the local datastore cache
filesystem, or by the user configured value, whichever is smalller.
The capacity is only set on instantiation of the store, and the current
value kept as long as the datastore remains cached in the datastore
cache. To change the value, the store has to be either be set to offline
mode and back, or the services restarted.

Basic performance tests:

Backup and upload of contents of linux git repository to AWS S3,
snapshots removed in-between each backup run to avoid other chunk reuse
optimization of PBS.

no-cache:
    had to backup 5.069 GiB of 5.069 GiB (compressed 3.718 GiB) in 50.76 s (average 102.258 MiB/s)
empty-cache:
    had to backup 5.069 GiB of 5.069 GiB (compressed 3.718 GiB) in 50.42 s (average 102.945 MiB/s)
all-cached:
    had to backup 5.069 GiB of 5.069 GiB (compressed 3.718 GiB) in 43.78 s (average 118.554 MiB/s)

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- added missing docstrings
- use Arc::clone() over .clone()

 pbs-datastore/src/datastore.rs                |  81 +++++++-
 pbs-datastore/src/lib.rs                      |   3 +
 .../src/local_datastore_lru_cache.rs          | 180 ++++++++++++++++++
 3 files changed, 260 insertions(+), 4 deletions(-)
 create mode 100644 pbs-datastore/src/local_datastore_lru_cache.rs

diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
index fdb30b4d6..a524d7b32 100644
--- a/pbs-datastore/src/datastore.rs
+++ b/pbs-datastore/src/datastore.rs
@@ -42,9 +42,10 @@ use crate::dynamic_index::{DynamicIndexReader, DynamicIndexWriter};
 use crate::fixed_index::{FixedIndexReader, FixedIndexWriter};
 use crate::hierarchy::{ListGroups, ListGroupsType, ListNamespaces, ListNamespacesRecursive};
 use crate::index::IndexFile;
+use crate::local_datastore_lru_cache::S3Cacher;
 use crate::s3::S3_CONTENT_PREFIX;
 use crate::task_tracking::{self, update_active_operations};
-use crate::DataBlob;
+use crate::{DataBlob, LocalDatastoreLruCache};
 
 static DATASTORE_MAP: LazyLock<Mutex<HashMap<String, Arc<DataStoreImpl>>>> =
     LazyLock::new(|| Mutex::new(HashMap::new()));
@@ -141,6 +142,7 @@ pub struct DataStoreImpl {
     last_digest: Option<[u8; 32]>,
     sync_level: DatastoreFSyncLevel,
     backend_config: DatastoreBackendConfig,
+    lru_store_caching: Option<LocalDatastoreLruCache>,
 }
 
 impl DataStoreImpl {
@@ -156,6 +158,7 @@ impl DataStoreImpl {
             last_digest: None,
             sync_level: Default::default(),
             backend_config: Default::default(),
+            lru_store_caching: None,
         })
     }
 }
@@ -264,6 +267,39 @@ impl DataStore {
         Ok(backend_type)
     }
 
+    pub fn cache(&self) -> Option<&LocalDatastoreLruCache> {
+        self.inner.lru_store_caching.as_ref()
+    }
+
+    /// Check if the digest is present in the local datastore cache.
+    /// Always returns false if there is no cache configured for this datastore.
+    pub fn cache_contains(&self, digest: &[u8; 32]) -> bool {
+        if let Some(cache) = self.inner.lru_store_caching.as_ref() {
+            return cache.contains(digest);
+        }
+        false
+    }
+
+    /// Insert digest as most recently used on in the cache.
+    /// Returns with success if there is no cache configured for this datastore.
+    pub fn cache_insert(&self, digest: &[u8; 32], chunk: &DataBlob) -> Result<(), Error> {
+        if let Some(cache) = self.inner.lru_store_caching.as_ref() {
+            return cache.insert(digest, chunk);
+        }
+        Ok(())
+    }
+
+    /// Returns the cacher for datastores backed by S3 object stores.
+    /// This allows to fetch chunks to the local cache store on-demand.
+    pub fn cacher(&self) -> Result<Option<S3Cacher>, Error> {
+        self.backend().map(|backend| match backend {
+            DatastoreBackend::S3(s3_client) => {
+                Some(S3Cacher::new(s3_client, self.inner.chunk_store.clone()))
+            }
+            DatastoreBackend::Filesystem => None,
+        })
+    }
+
     pub fn lookup_datastore(
         name: &str,
         operation: Option<Operation>,
@@ -446,6 +482,33 @@ impl DataStore {
                 .parse_property_string(config.backend.as_deref().unwrap_or(""))?,
         )?;
 
+        let lru_store_caching = if DatastoreBackendType::S3 == backend_config.ty.unwrap_or_default()
+        {
+            let mut cache_capacity = 0;
+            if let Ok(fs_info) = proxmox_sys::fs::fs_info(&chunk_store.base_path()) {
+                cache_capacity = fs_info.available / (16 * 1024 * 1024);
+            }
+            if let Some(max_cache_size) = backend_config.max_cache_size {
+                info!(
+                    "Got requested max cache size {max_cache_size} for store {}",
+                    config.name
+                );
+                let max_cache_capacity = max_cache_size.as_u64() / (16 * 1024 * 1024);
+                cache_capacity = cache_capacity.min(max_cache_capacity);
+            }
+            let cache_capacity = usize::try_from(cache_capacity).unwrap_or_default();
+
+            info!(
+                "Using datastore cache with capacity {cache_capacity} for store {}",
+                config.name
+            );
+
+            let cache = LocalDatastoreLruCache::new(cache_capacity, chunk_store.clone());
+            Some(cache)
+        } else {
+            None
+        };
+
         Ok(DataStoreImpl {
             chunk_store,
             gc_mutex: Mutex::new(()),
@@ -455,6 +518,7 @@ impl DataStore {
             last_digest,
             sync_level: tuning.sync_level.unwrap_or_default(),
             backend_config,
+            lru_store_caching,
         })
     }
 
@@ -1716,7 +1780,7 @@ impl DataStore {
         delete_list: &mut Vec<S3ObjectKey>,
         gc_status: &mut GarbageCollectionStatus,
     ) -> Result<bool, Error> {
-        let chunk_path = match self.chunk_path_from_object_key(&object_key) {
+        let (chunk_path, digest) = match self.chunk_path_from_object_key(&object_key) {
             Some(path) => path,
             None => return Ok(false),
         };
@@ -1737,6 +1801,10 @@ impl DataStore {
         let bad = chunk_path.as_path().ends_with(".bad");
 
         if atime < min_atime {
+            if let Some(cache) = self.cache() {
+                // ignore errors, phase 3 will retry cleanup anyways
+                let _ = cache.remove(&digest);
+            }
             delete_list.push(object_key.clone());
             if bad {
                 gc_status.removed_bad += 1;
@@ -1762,7 +1830,7 @@ impl DataStore {
     }
 
     // Check and generate a chunk path from given object key
-    fn chunk_path_from_object_key(&self, object_key: &S3ObjectKey) -> Option<PathBuf> {
+    fn chunk_path_from_object_key(&self, object_key: &S3ObjectKey) -> Option<(PathBuf, [u8; 32])> {
         // Check object is actually a chunk
         let digest = match Path::new::<str>(object_key).file_name() {
             Some(file_name) => file_name,
@@ -1785,7 +1853,12 @@ impl DataStore {
         chunk_path.push(hexdigit_prefix);
         chunk_path.push(digest);
 
-        Some(chunk_path)
+        let mut digest_bytes = [0u8; 32];
+        let digest = digest.as_bytes();
+        // safe to unwrap as already checked above
+        hex::decode_to_slice(&digest[..64], &mut digest_bytes).unwrap();
+
+        Some((chunk_path, digest_bytes))
     }
 
     pub fn try_shared_chunk_store_lock(&self) -> Result<ProcessLockSharedGuard, Error> {
diff --git a/pbs-datastore/src/lib.rs b/pbs-datastore/src/lib.rs
index ca6fdb7d8..b9eb035c2 100644
--- a/pbs-datastore/src/lib.rs
+++ b/pbs-datastore/src/lib.rs
@@ -217,3 +217,6 @@ pub use snapshot_reader::SnapshotReader;
 
 mod local_chunk_reader;
 pub use local_chunk_reader::LocalChunkReader;
+
+mod local_datastore_lru_cache;
+pub use local_datastore_lru_cache::LocalDatastoreLruCache;
diff --git a/pbs-datastore/src/local_datastore_lru_cache.rs b/pbs-datastore/src/local_datastore_lru_cache.rs
new file mode 100644
index 000000000..c0edd3619
--- /dev/null
+++ b/pbs-datastore/src/local_datastore_lru_cache.rs
@@ -0,0 +1,180 @@
+//! Use a local datastore as cache for operations on a datastore attached via
+//! a network layer (e.g. via the S3 backend).
+
+use std::future::Future;
+use std::sync::Arc;
+
+use anyhow::{bail, Error};
+use http_body_util::BodyExt;
+
+use pbs_tools::async_lru_cache::{AsyncCacher, AsyncLruCache};
+use proxmox_s3_client::S3Client;
+
+use crate::ChunkStore;
+use crate::DataBlob;
+
+#[derive(Clone)]
+/// Cacher to fetch chunks from the S3 object store and insert them in the local cache store.
+pub struct S3Cacher {
+    client: Arc<S3Client>,
+    store: Arc<ChunkStore>,
+}
+
+impl AsyncCacher<[u8; 32], ()> for S3Cacher {
+    fn fetch(
+        &self,
+        key: [u8; 32],
+    ) -> Box<dyn Future<Output = Result<Option<()>, Error>> + Send + 'static> {
+        let client = Arc::clone(&self.client);
+        let store = Arc::clone(&self.store);
+        Box::new(async move {
+            let object_key = crate::s3::object_key_from_digest(&key)?;
+            match client.get_object(object_key).await? {
+                None => bail!("could not fetch object with key {}", hex::encode(key)),
+                Some(response) => {
+                    let bytes = response.content.collect().await?.to_bytes();
+                    let chunk = DataBlob::from_raw(bytes.to_vec())?;
+                    store.insert_chunk(&chunk, &key)?;
+                    Ok(Some(()))
+                }
+            }
+        })
+    }
+}
+
+impl S3Cacher {
+    pub fn new(client: Arc<S3Client>, store: Arc<ChunkStore>) -> Self {
+        Self { client, store }
+    }
+}
+
+/// LRU cache using local datastore for caching chunks
+///
+/// Uses a LRU cache, but without storing the values in-memory but rather
+/// on the filesystem
+pub struct LocalDatastoreLruCache {
+    cache: AsyncLruCache<[u8; 32], ()>,
+    store: Arc<ChunkStore>,
+}
+
+impl LocalDatastoreLruCache {
+    /// Create a new cache instance storing up to given capacity chunks in the local cache store.
+    pub fn new(capacity: usize, store: Arc<ChunkStore>) -> Self {
+        Self {
+            cache: AsyncLruCache::new(capacity),
+            store,
+        }
+    }
+
+    /// Insert a new chunk into the local datastore cache.
+    ///
+    /// Fails if the chunk cannot be inserted successfully.
+    pub fn insert(&self, digest: &[u8; 32], chunk: &DataBlob) -> Result<(), Error> {
+        self.store.insert_chunk(chunk, digest)?;
+        self.cache.insert(*digest, (), |digest| {
+            let (path, _digest_str) = self.store.chunk_path(&digest);
+            // Truncate to free up space but keep the inode around, since that
+            // is used as marker for chunks in use by garbage collection.
+            if let Err(err) = nix::unistd::truncate(&path, 0) {
+                if err != nix::errno::Errno::ENOENT {
+                    return Err(Error::from(err));
+                }
+            }
+            Ok(())
+        })
+    }
+
+    /// Remove a chunk from the local datastore cache.
+    ///
+    /// Fails if the chunk cannot be deleted successfully.
+    pub fn remove(&self, digest: &[u8; 32]) -> Result<(), Error> {
+        self.cache.remove(*digest);
+        let (path, _digest_str) = self.store.chunk_path(digest);
+        std::fs::remove_file(path).map_err(Error::from)
+    }
+
+    /// Access the locally cached chunk or fetch it from the S3 object store via the provided
+    /// cacher instance.
+    ///
+    /// For evicted cache nodes, clear the chunk file contents but leave the empty marker file
+    /// behind so garbage collection doesn't clean it while in use.
+    pub async fn access(
+        &self,
+        digest: &[u8; 32],
+        cacher: &mut S3Cacher,
+    ) -> Result<Option<DataBlob>, Error> {
+        if self
+            .cache
+            .access(*digest, cacher, |digest| {
+                let (path, _digest_str) = self.store.chunk_path(&digest);
+                // Truncate to free up space but keep the inode around, since that
+                // is used as marker for chunks in use by garbage collection.
+                if let Err(err) = nix::unistd::truncate(&path, 0) {
+                    if err != nix::errno::Errno::ENOENT {
+                        return Err(Error::from(err));
+                    }
+                }
+                Ok(())
+            })
+            .await?
+            .is_some()
+        {
+            let (path, _digest_str) = self.store.chunk_path(digest);
+            let mut file = match std::fs::File::open(&path) {
+                Ok(file) => file,
+                Err(err) => {
+                    // Expected chunk to be present since LRU cache has it, but it is missing
+                    // locally, try to fetch again
+                    if err.kind() == std::io::ErrorKind::NotFound {
+                        let object_key = crate::s3::object_key_from_digest(digest)?;
+                        match cacher.client.get_object(object_key).await? {
+                            None => {
+                                bail!("could not fetch object with key {}", hex::encode(digest))
+                            }
+                            Some(response) => {
+                                let bytes = response.content.collect().await?.to_bytes();
+                                let chunk = DataBlob::from_raw(bytes.to_vec())?;
+                                self.store.insert_chunk(&chunk, digest)?;
+                                std::fs::File::open(&path)?
+                            }
+                        }
+                    } else {
+                        return Err(Error::from(err));
+                    }
+                }
+            };
+            let chunk = match DataBlob::load_from_reader(&mut file) {
+                Ok(chunk) => chunk,
+                Err(err) => {
+                    use std::io::Seek;
+                    // Check if file is empty marker file, try fetching content if so
+                    if file.seek(std::io::SeekFrom::End(0))? == 0 {
+                        let object_key = crate::s3::object_key_from_digest(digest)?;
+                        match cacher.client.get_object(object_key).await? {
+                            None => {
+                                bail!("could not fetch object with key {}", hex::encode(digest))
+                            }
+                            Some(response) => {
+                                let bytes = response.content.collect().await?.to_bytes();
+                                let chunk = DataBlob::from_raw(bytes.to_vec())?;
+                                self.store.insert_chunk(&chunk, digest)?;
+                                let mut file = std::fs::File::open(&path)?;
+                                DataBlob::load_from_reader(&mut file)?
+                            }
+                        }
+                    } else {
+                        return Err(err);
+                    }
+                }
+            };
+            Ok(Some(chunk))
+        } else {
+            Ok(None)
+        }
+    }
+
+    /// Checks if the given digest is stored in the datastores LRU cache
+    pub fn contains(&self, digest: &[u8; 32]) -> bool {
+        self.cache.contains(*digest)
+    }
+}
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 31/46] api: backup: use local datastore cache on s3 backend chunk upload
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (32 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 30/46] datastore: add local datastore cache for network attached storages Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 32/46] api: reader: use local datastore cache on s3 backend chunk fetching Christian Ebner
                   ` (17 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Take advantage of the local datastore cache to avoid re-uploading of
already known chunks. This not only helps improve the backup/upload
speeds, but also avoids additionally costs by reducing the number of
requests and transferred payload data to the S3 object store api.

If the cache is present, lookup if it contains the chunk, skipping
upload altogether if it is. Otherwise, upload the chunk into memory,
upload it to the S3 object store api and insert it into the local
datastore cache.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use upload_no_replace_with_retry

 src/api2/backup/upload_chunk.rs | 36 ++++++++++++++++++++++++++++++---
 src/server/pull.rs              |  4 ++++
 2 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/src/api2/backup/upload_chunk.rs b/src/api2/backup/upload_chunk.rs
index f1b07a123..4514e3b9b 100644
--- a/src/api2/backup/upload_chunk.rs
+++ b/src/api2/backup/upload_chunk.rs
@@ -2,7 +2,7 @@ use std::pin::Pin;
 use std::sync::Arc;
 use std::task::{Context, Poll};
 
-use anyhow::{bail, format_err, Error};
+use anyhow::{bail, format_err, Context as AnyhowContext, Error};
 use futures::*;
 use hex::FromHex;
 use http_body_util::{BodyDataStream, BodyExt};
@@ -262,10 +262,40 @@ async fn upload_to_backend(
                 );
             }
 
+            // Avoid re-upload to S3 if the chunk is either present in the LRU cache or the chunk
+            // file exists on filesystem. The latter means that the chunk has been present in the
+            // past an was not cleaned up by garbage collection, so contained in the S3 object store.
+            if env.datastore.cache_contains(&digest) {
+                tracing::info!("Skip upload of cached chunk {}", hex::encode(digest));
+                return Ok((digest, size, encoded_size, true));
+            }
+            if let Ok(true) = env.datastore.cond_touch_chunk(&digest, false) {
+                tracing::info!(
+                    "Skip upload of already encountered chunk {}",
+                    hex::encode(digest)
+                );
+                return Ok((digest, size, encoded_size, true));
+            }
+
+            tracing::info!("Upload of new chunk {}", hex::encode(digest));
             let object_key = pbs_datastore::s3::object_key_from_digest(&digest)?;
             let is_duplicate = s3_client
-                .upload_no_replace_with_retry(object_key, data)
-                .await?;
+                .upload_no_replace_with_retry(object_key, data.clone())
+                .await
+                .context("failed to upload chunk to s3 backend")?;
+
+            // Only insert the chunk into the cache after it has been successufuly uploaded.
+            // Although less performant than doing this in parallel, it is required for consisency
+            // since chunks are considered as present on the backend if the file exists in the local
+            // cache store.
+            let datastore = env.datastore.clone();
+            tracing::info!("Caching of chunk {}", hex::encode(digest));
+            let _ = tokio::task::spawn_blocking(move || {
+                let chunk = DataBlob::from_raw(data.to_vec())?;
+                datastore.cache_insert(&digest, &chunk)
+            })
+            .await?;
+
             Ok((digest, size, encoded_size, is_duplicate))
         }
     }
diff --git a/src/server/pull.rs b/src/server/pull.rs
index 775ed0c59..f979aba1e 100644
--- a/src/server/pull.rs
+++ b/src/server/pull.rs
@@ -172,6 +172,10 @@ async fn pull_index_chunks<I: IndexFile>(
                     target2.insert_chunk(&chunk, &digest)?;
                 }
                 DatastoreBackend::S3(s3_client) => {
+                    if target2.cache_contains(&digest) {
+                        return Ok(());
+                    }
+                    target2.cache_insert(&digest, &chunk)?;
                     let data = chunk.raw_data().to_vec();
                     let upload_data = hyper::body::Bytes::from(data);
                     let object_key = pbs_datastore::s3::object_key_from_digest(&digest)?;
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 32/46] api: reader: use local datastore cache on s3 backend chunk fetching
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (33 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 31/46] api: backup: use local datastore cache on s3 backend chunk upload Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 33/46] datastore: local chunk reader: get cached chunk from local cache store Christian Ebner
                   ` (16 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Take advantage of the local datastore filesystem cache for datastores
backed by an s3 object store in order to reduce number of requests
and latency, and increase throughput.

Also, reducing the number of requests is cost beneficial for S3 object
stores charging for fetching of objects.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- no changes

 src/api2/reader/mod.rs | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/src/api2/reader/mod.rs b/src/api2/reader/mod.rs
index 997d9ca77..846493c61 100644
--- a/src/api2/reader/mod.rs
+++ b/src/api2/reader/mod.rs
@@ -327,7 +327,28 @@ fn download_chunk(
 
         let body = match &env.backend {
             DatastoreBackend::Filesystem => load_from_filesystem(env, &digest)?,
-            DatastoreBackend::S3(s3_client) => fetch_from_object_store(s3_client, &digest).await?,
+            DatastoreBackend::S3(s3_client) => {
+                match env.datastore.cache() {
+                    None => fetch_from_object_store(s3_client, &digest).await?,
+                    Some(cache) => {
+                        let mut cacher = env
+                            .datastore
+                            .cacher()?
+                            .ok_or(format_err!("no cacher for datastore"))?;
+                        // Download from object store, insert to local cache store and read from
+                        // file. Can this be optimized?
+                        let chunk =
+                            cache
+                                .access(&digest, &mut cacher)
+                                .await?
+                                .ok_or(format_err!(
+                                    "unable to access chunk with digest {}",
+                                    hex::encode(digest)
+                                ))?;
+                        Body::from(chunk.raw_data().to_owned())
+                    }
+                }
+            }
         };
 
         // fixme: set other headers ?
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 33/46] datastore: local chunk reader: get cached chunk from local cache store
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (34 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 32/46] api: reader: use local datastore cache on s3 backend chunk fetching Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 34/46] backup writer: refactor parameters into backup writer options struct Christian Ebner
                   ` (15 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Check if a chunk is contained in the local cache and if so prefer
fetching it from the cache instead of pulling it via the S3 api. This
improves performance and reduces number of requests to the backend.

Basic restore performance tests:

Restored a snapshot containing the linux git repository (on-disk size
5.069 GiB, compressed 3.718 GiB) from an AWS S3 backed datastore, with
and without cached contents:
non cached: 691.95 s
all cached:  74.89 s

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use Arc::clone() over .clone()

 pbs-datastore/src/local_chunk_reader.rs | 31 +++++++++++++++++++++----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/pbs-datastore/src/local_chunk_reader.rs b/pbs-datastore/src/local_chunk_reader.rs
index 667c97206..73e103085 100644
--- a/pbs-datastore/src/local_chunk_reader.rs
+++ b/pbs-datastore/src/local_chunk_reader.rs
@@ -2,7 +2,7 @@ use std::future::Future;
 use std::pin::Pin;
 use std::sync::Arc;
 
-use anyhow::{bail, Error};
+use anyhow::{bail, format_err, Error};
 use http_body_util::BodyExt;
 
 use pbs_api_types::CryptMode;
@@ -68,9 +68,18 @@ impl ReadChunk for LocalChunkReader {
     fn read_raw_chunk(&self, digest: &[u8; 32]) -> Result<DataBlob, Error> {
         let chunk = match &self.backend {
             DatastoreBackend::Filesystem => self.store.load_chunk(digest)?,
-            DatastoreBackend::S3(s3_client) => {
-                proxmox_async::runtime::block_on(fetch(Arc::clone(s3_client), digest))?
-            }
+            DatastoreBackend::S3(s3_client) => match self.store.cache() {
+                None => proxmox_async::runtime::block_on(fetch(Arc::clone(s3_client), digest))?,
+                Some(cache) => {
+                    let mut cacher = self
+                        .store
+                        .cacher()?
+                        .ok_or(format_err!("no cacher for datastore"))?;
+                    proxmox_async::runtime::block_on(cache.access(digest, &mut cacher))?.ok_or(
+                        format_err!("unable to access chunk with digest {}", hex::encode(digest)),
+                    )?
+                }
+            },
         };
         self.ensure_crypt_mode(chunk.crypt_mode()?)?;
 
@@ -98,7 +107,19 @@ impl AsyncReadChunk for LocalChunkReader {
                     let raw_data = tokio::fs::read(&path).await?;
                     DataBlob::load_from_reader(&mut &raw_data[..])?
                 }
-                DatastoreBackend::S3(s3_client) => fetch(Arc::clone(s3_client), digest).await?,
+                DatastoreBackend::S3(s3_client) => match self.store.cache() {
+                    None => fetch(Arc::clone(s3_client), digest).await?,
+                    Some(cache) => {
+                        let mut cacher = self
+                            .store
+                            .cacher()?
+                            .ok_or(format_err!("no cacher for datastore"))?;
+                        cache.access(digest, &mut cacher).await?.ok_or(format_err!(
+                            "unable to access chunk with digest {}",
+                            hex::encode(digest)
+                        ))?
+                    }
+                },
             };
             self.ensure_crypt_mode(chunk.crypt_mode()?)?;
 
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 34/46] backup writer: refactor parameters into backup writer options struct
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (35 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 33/46] datastore: local chunk reader: get cached chunk from local cache store Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 35/46] api: backup: add no-cache flag to bypass local datastore cache Christian Ebner
                   ` (14 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Introduce a BackupWriterOptions struct, bundling the currently
present writer start parameters n order to limit their number
and make this easier extensible.

No functional changes intended.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- not present in previous version

 examples/upload-speed.rs               | 16 ++++++----
 pbs-client/src/backup_writer.rs        | 44 ++++++++++++++++----------
 proxmox-backup-client/src/benchmark.rs | 16 ++++++----
 proxmox-backup-client/src/main.rs      | 18 ++++++-----
 src/server/push.rs                     | 18 +++++++----
 5 files changed, 66 insertions(+), 46 deletions(-)

diff --git a/examples/upload-speed.rs b/examples/upload-speed.rs
index e4b570ec5..bbabb37df 100644
--- a/examples/upload-speed.rs
+++ b/examples/upload-speed.rs
@@ -1,7 +1,7 @@
 use anyhow::Error;
 
 use pbs_api_types::{Authid, BackupNamespace, BackupType};
-use pbs_client::{BackupWriter, HttpClient, HttpClientOptions};
+use pbs_client::{BackupWriter, BackupWriterOptions, HttpClient, HttpClientOptions};
 
 async fn upload_speed() -> Result<f64, Error> {
     let host = "localhost";
@@ -19,12 +19,14 @@ async fn upload_speed() -> Result<f64, Error> {
 
     let client = BackupWriter::start(
         &client,
-        None,
-        datastore,
-        &BackupNamespace::root(),
-        &(BackupType::Host, "speedtest".to_string(), backup_time).into(),
-        false,
-        true,
+        BackupWriterOptions {
+            datastore,
+            ns: &BackupNamespace::root(),
+            backup: &(BackupType::Host, "speedtest".to_string(), backup_time).into(),
+            crypt_config: None,
+            debug: false,
+            benchmark: true,
+        },
     )
     .await?;
 
diff --git a/pbs-client/src/backup_writer.rs b/pbs-client/src/backup_writer.rs
index 1253ef561..853b1cb4f 100644
--- a/pbs-client/src/backup_writer.rs
+++ b/pbs-client/src/backup_writer.rs
@@ -63,6 +63,22 @@ struct ChunkUploadResponse {
 type UploadQueueSender = mpsc::Sender<(MergedChunkInfo, Option<ChunkUploadResponse>)>;
 type UploadResultReceiver = oneshot::Receiver<Result<(), Error>>;
 
+/// Additional configuration options for BackupWriter instance
+pub struct BackupWriterOptions<'a> {
+    /// Target datastore
+    pub datastore: &'a str,
+    /// Target namespace
+    pub ns: &'a BackupNamespace,
+    /// Target snapshot
+    pub backup: &'a BackupDir,
+    /// Crypto configuration
+    pub crypt_config: Option<Arc<CryptConfig>>,
+    /// Run in debug mode
+    pub debug: bool,
+    /// Start benchmark
+    pub benchmark: bool,
+}
+
 impl BackupWriter {
     fn new(h2: H2Client, abort: AbortHandle, crypt_config: Option<Arc<CryptConfig>>) -> Arc<Self> {
         Arc::new(Self {
@@ -72,28 +88,22 @@ impl BackupWriter {
         })
     }
 
-    // FIXME: extract into (flattened) parameter struct?
     #[allow(clippy::too_many_arguments)]
-    pub async fn start(
+    pub async fn start<'a>(
         client: &HttpClient,
-        crypt_config: Option<Arc<CryptConfig>>,
-        datastore: &str,
-        ns: &BackupNamespace,
-        backup: &BackupDir,
-        debug: bool,
-        benchmark: bool,
+        writer_options: BackupWriterOptions<'a>,
     ) -> Result<Arc<BackupWriter>, Error> {
         let mut param = json!({
-            "backup-type": backup.ty(),
-            "backup-id": backup.id(),
-            "backup-time": backup.time,
-            "store": datastore,
-            "debug": debug,
-            "benchmark": benchmark
+            "backup-type": writer_options.backup.ty(),
+            "backup-id": writer_options.backup.id(),
+            "backup-time": writer_options.backup.time,
+            "store": writer_options.datastore,
+            "debug": writer_options.debug,
+            "benchmark": writer_options.benchmark,
         });
 
-        if !ns.is_root() {
-            param["ns"] = serde_json::to_value(ns)?;
+        if !writer_options.ns.is_root() {
+            param["ns"] = serde_json::to_value(writer_options.ns)?;
         }
 
         let req = HttpClient::request_builder(
@@ -109,7 +119,7 @@ impl BackupWriter {
             .start_h2_connection(req, String::from(PROXMOX_BACKUP_PROTOCOL_ID_V1!()))
             .await?;
 
-        Ok(BackupWriter::new(h2, abort, crypt_config))
+        Ok(BackupWriter::new(h2, abort, writer_options.crypt_config))
     }
 
     pub async fn get(&self, path: &str, param: Option<Value>) -> Result<Value, Error> {
diff --git a/proxmox-backup-client/src/benchmark.rs b/proxmox-backup-client/src/benchmark.rs
index a6f24d745..6b11e216d 100644
--- a/proxmox-backup-client/src/benchmark.rs
+++ b/proxmox-backup-client/src/benchmark.rs
@@ -16,7 +16,7 @@ use proxmox_schema::{api, ApiType, ReturnType};
 
 use pbs_api_types::{BackupNamespace, BackupType};
 use pbs_client::tools::key_source::get_encryption_key_password;
-use pbs_client::{BackupRepository, BackupWriter};
+use pbs_client::{BackupRepository, BackupWriter, BackupWriterOptions};
 use pbs_datastore::data_blob::{DataBlob, DataChunkBuilder};
 use pbs_key_config::{load_and_decrypt_key, KeyDerivationConfig};
 use pbs_tools::crypt_config::CryptConfig;
@@ -230,12 +230,14 @@ async fn test_upload_speed(
     log::debug!("Connecting to backup server");
     let client = BackupWriter::start(
         &client,
-        crypt_config.clone(),
-        repo.store(),
-        &BackupNamespace::root(),
-        &(BackupType::Host, "benchmark".to_string(), backup_time).into(),
-        false,
-        true,
+        BackupWriterOptions {
+            datastore: repo.store(),
+            ns: &BackupNamespace::root(),
+            backup: &(BackupType::Host, "benchmark".to_string(), backup_time).into(),
+            crypt_config: crypt_config.clone(),
+            debug: false,
+            benchmark: true,
+        },
     )
     .await?;
 
diff --git a/proxmox-backup-client/src/main.rs b/proxmox-backup-client/src/main.rs
index 44f4f5db5..44c076627 100644
--- a/proxmox-backup-client/src/main.rs
+++ b/proxmox-backup-client/src/main.rs
@@ -46,8 +46,8 @@ use pbs_client::tools::{
 use pbs_client::{
     delete_ticket_info, parse_backup_specification, view_task_result, BackupDetectionMode,
     BackupReader, BackupRepository, BackupSpecificationType, BackupStats, BackupWriter,
-    ChunkStream, FixedChunkStream, HttpClient, InjectionData, PxarBackupStream, RemoteChunkReader,
-    UploadOptions, BACKUP_SOURCE_SCHEMA,
+    BackupWriterOptions, ChunkStream, FixedChunkStream, HttpClient, InjectionData,
+    PxarBackupStream, RemoteChunkReader, UploadOptions, BACKUP_SOURCE_SCHEMA,
 };
 use pbs_datastore::catalog::{BackupCatalogWriter, CatalogReader, CatalogWriter};
 use pbs_datastore::chunk_store::verify_chunk_size;
@@ -954,12 +954,14 @@ async fn create_backup(
 
     let client = BackupWriter::start(
         &http_client,
-        crypt_config.clone(),
-        repo.store(),
-        &backup_ns,
-        &snapshot,
-        true,
-        false,
+        BackupWriterOptions {
+            datastore: repo.store(),
+            ns: &backup_ns,
+            backup: &snapshot,
+            crypt_config: crypt_config.clone(),
+            debug: true,
+            benchmark: false,
+        },
     )
     .await?;
 
diff --git a/src/server/push.rs b/src/server/push.rs
index e71012ed8..3bb0a087d 100644
--- a/src/server/push.rs
+++ b/src/server/push.rs
@@ -16,7 +16,9 @@ use pbs_api_types::{
     MANIFEST_BLOB_NAME, PRIV_DATASTORE_BACKUP, PRIV_DATASTORE_READ, PRIV_REMOTE_DATASTORE_BACKUP,
     PRIV_REMOTE_DATASTORE_MODIFY, PRIV_REMOTE_DATASTORE_PRUNE,
 };
-use pbs_client::{BackupRepository, BackupWriter, HttpClient, MergedChunkInfo, UploadOptions};
+use pbs_client::{
+    BackupRepository, BackupWriter, BackupWriterOptions, HttpClient, MergedChunkInfo, UploadOptions,
+};
 use pbs_config::CachedUserInfo;
 use pbs_datastore::data_blob::ChunkInfo;
 use pbs_datastore::dynamic_index::DynamicIndexReader;
@@ -822,12 +824,14 @@ pub(crate) async fn push_snapshot(
     // Writer instance locks the snapshot on the remote side
     let backup_writer = BackupWriter::start(
         &params.target.client,
-        None,
-        params.target.repo.store(),
-        &target_ns,
-        snapshot,
-        false,
-        false,
+        BackupWriterOptions {
+            datastore: params.target.repo.store(),
+            ns: &target_ns,
+            backup: snapshot,
+            crypt_config: None,
+            debug: false,
+            benchmark: false,
+        },
     )
     .await?;
 
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 35/46] api: backup: add no-cache flag to bypass local datastore cache
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (36 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 34/46] backup writer: refactor parameters into backup writer options struct Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 36/46] api/datastore: implement refresh endpoint for stores with s3 backend Christian Ebner
                   ` (13 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Adds the `no-cache` flag so the client can request to bypass the
local datastore cache for chunk uploads. This is mainly intended for
debugging and benchmarking, but can be used in cases the caching is
known to be ineffective (no possible deduplication).

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- adapt to newly introduced BackupWriterOptions

 examples/upload-speed.rs               | 1 +
 pbs-client/src/backup_writer.rs        | 3 +++
 proxmox-backup-client/src/benchmark.rs | 1 +
 proxmox-backup-client/src/main.rs      | 8 ++++++++
 src/api2/backup/environment.rs         | 3 +++
 src/api2/backup/mod.rs                 | 3 +++
 src/api2/backup/upload_chunk.rs        | 9 +++++++++
 src/server/push.rs                     | 1 +
 8 files changed, 29 insertions(+)

diff --git a/examples/upload-speed.rs b/examples/upload-speed.rs
index bbabb37df..ed181330d 100644
--- a/examples/upload-speed.rs
+++ b/examples/upload-speed.rs
@@ -26,6 +26,7 @@ async fn upload_speed() -> Result<f64, Error> {
             crypt_config: None,
             debug: false,
             benchmark: true,
+            no_cache: false,
         },
     )
     .await?;
diff --git a/pbs-client/src/backup_writer.rs b/pbs-client/src/backup_writer.rs
index 853b1cb4f..abe7c79e2 100644
--- a/pbs-client/src/backup_writer.rs
+++ b/pbs-client/src/backup_writer.rs
@@ -77,6 +77,8 @@ pub struct BackupWriterOptions<'a> {
     pub debug: bool,
     /// Start benchmark
     pub benchmark: bool,
+    /// Skip datastore cache
+    pub no_cache: bool,
 }
 
 impl BackupWriter {
@@ -100,6 +102,7 @@ impl BackupWriter {
             "store": writer_options.datastore,
             "debug": writer_options.debug,
             "benchmark": writer_options.benchmark,
+            "no-cache": writer_options.no_cache,
         });
 
         if !writer_options.ns.is_root() {
diff --git a/proxmox-backup-client/src/benchmark.rs b/proxmox-backup-client/src/benchmark.rs
index 6b11e216d..463c2e61e 100644
--- a/proxmox-backup-client/src/benchmark.rs
+++ b/proxmox-backup-client/src/benchmark.rs
@@ -237,6 +237,7 @@ async fn test_upload_speed(
             crypt_config: crypt_config.clone(),
             debug: false,
             benchmark: true,
+            no_cache: true,
         },
     )
     .await?;
diff --git a/proxmox-backup-client/src/main.rs b/proxmox-backup-client/src/main.rs
index 44c076627..3f6c5adb9 100644
--- a/proxmox-backup-client/src/main.rs
+++ b/proxmox-backup-client/src/main.rs
@@ -742,6 +742,12 @@ fn spawn_catalog_upload(
                 optional: true,
                 default: false,
             },
+            "no-cache": {
+                type: Boolean,
+                description: "Bypass local datastore cache for network storages.",
+                optional: true,
+                default: false,
+            },
         }
     }
  )]
@@ -754,6 +760,7 @@ async fn create_backup(
     change_detection_mode: Option<BackupDetectionMode>,
     dry_run: bool,
     skip_e2big_xattr: bool,
+    no_cache: bool,
     limit: ClientRateLimitConfig,
     _info: &ApiMethod,
     _rpcenv: &mut dyn RpcEnvironment,
@@ -961,6 +968,7 @@ async fn create_backup(
             crypt_config: crypt_config.clone(),
             debug: true,
             benchmark: false,
+            no_cache,
         },
     )
     .await?;
diff --git a/src/api2/backup/environment.rs b/src/api2/backup/environment.rs
index 6a265bcc5..d5e6869cd 100644
--- a/src/api2/backup/environment.rs
+++ b/src/api2/backup/environment.rs
@@ -112,6 +112,7 @@ pub struct BackupEnvironment {
     result_attributes: Value,
     auth_id: Authid,
     pub debug: bool,
+    pub no_cache: bool,
     pub formatter: &'static dyn OutputFormatter,
     pub worker: Arc<WorkerTask>,
     pub datastore: Arc<DataStore>,
@@ -128,6 +129,7 @@ impl BackupEnvironment {
         worker: Arc<WorkerTask>,
         datastore: Arc<DataStore>,
         backup_dir: BackupDir,
+        no_cache: bool,
     ) -> Result<Self, Error> {
         let state = SharedBackupState {
             finished: false,
@@ -148,6 +150,7 @@ impl BackupEnvironment {
             worker,
             datastore,
             debug: tracing::enabled!(tracing::Level::DEBUG),
+            no_cache,
             formatter: JSON_FORMATTER,
             backup_dir,
             last_backup: None,
diff --git a/src/api2/backup/mod.rs b/src/api2/backup/mod.rs
index 026f1f106..ae61ff697 100644
--- a/src/api2/backup/mod.rs
+++ b/src/api2/backup/mod.rs
@@ -53,6 +53,7 @@ pub const API_METHOD_UPGRADE_BACKUP: ApiMethod = ApiMethod::new(
             ("backup-time", false, &BACKUP_TIME_SCHEMA),
             ("debug", true, &BooleanSchema::new("Enable verbose debug logging.").schema()),
             ("benchmark", true, &BooleanSchema::new("Job is a benchmark (do not keep data).").schema()),
+            ("no-cache", true, &BooleanSchema::new("Disable local datastore cache for network storages").schema()),
         ]),
     )
 ).access(
@@ -79,6 +80,7 @@ fn upgrade_to_backup_protocol(
     async move {
         let debug = param["debug"].as_bool().unwrap_or(false);
         let benchmark = param["benchmark"].as_bool().unwrap_or(false);
+        let no_cache = param["no-cache"].as_bool().unwrap_or(false);
 
         let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
 
@@ -214,6 +216,7 @@ fn upgrade_to_backup_protocol(
                     worker.clone(),
                     datastore,
                     backup_dir,
+                    no_cache,
                 )?;
 
                 env.debug = debug;
diff --git a/src/api2/backup/upload_chunk.rs b/src/api2/backup/upload_chunk.rs
index 4514e3b9b..35378377f 100644
--- a/src/api2/backup/upload_chunk.rs
+++ b/src/api2/backup/upload_chunk.rs
@@ -262,6 +262,15 @@ async fn upload_to_backend(
                 );
             }
 
+            if env.no_cache {
+                let object_key = pbs_datastore::s3::object_key_from_digest(&digest)?;
+                let is_duplicate = s3_client
+                    .upload_no_replace_with_retry(object_key, data)
+                    .await
+                    .context("failed to upload chunk to s3 backend")?;
+                return Ok((digest, size, encoded_size, is_duplicate));
+            }
+
             // Avoid re-upload to S3 if the chunk is either present in the LRU cache or the chunk
             // file exists on filesystem. The latter means that the chunk has been present in the
             // past an was not cleaned up by garbage collection, so contained in the S3 object store.
diff --git a/src/server/push.rs b/src/server/push.rs
index 3bb0a087d..1634f92d1 100644
--- a/src/server/push.rs
+++ b/src/server/push.rs
@@ -831,6 +831,7 @@ pub(crate) async fn push_snapshot(
             crypt_config: None,
             debug: false,
             benchmark: false,
+            no_cache: false,
         },
     )
     .await?;
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 36/46] api/datastore: implement refresh endpoint for stores with s3 backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (37 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 35/46] api: backup: add no-cache flag to bypass local datastore cache Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-21 14:16   ` Hannes Laimer
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 37/46] cli: add dedicated subcommand for datastore s3 refresh Christian Ebner
                   ` (12 subsequent siblings)
  51 siblings, 1 reply; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Allows to easily refresh the contents on the local cache store for
datastores backed by an S3 object store.

In order to guarantee that no read or write operations are ongoing,
the store is first set into the maintenance mode `S3Refresh`. Objects
are then fetched into a temporary directory to avoid loosing contents
and consistency in case of an error. Once all objects have been
fetched, clears out existing contents and moves the newly fetched
contents in place.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- refactor s3 refresh into more compact methods
- drop un-necessary drop(_lock)
- use missing tokio::task::spawn_blocking context for blocking
  maintenance mode setting

 pbs-datastore/src/datastore.rs | 175 +++++++++++++++++++++++++++++++++
 src/api2/admin/datastore.rs    |  34 +++++++
 2 files changed, 209 insertions(+)

diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
index a524d7b32..b2af05eac 100644
--- a/pbs-datastore/src/datastore.rs
+++ b/pbs-datastore/src/datastore.rs
@@ -10,6 +10,7 @@ use anyhow::{bail, format_err, Context, Error};
 use http_body_util::BodyExt;
 use nix::unistd::{unlinkat, UnlinkatFlags};
 use pbs_tools::lru_cache::LruCache;
+use tokio::io::AsyncWriteExt;
 use tracing::{info, warn};
 
 use proxmox_human_byte::HumanByte;
@@ -2200,4 +2201,178 @@ impl DataStore {
     pub fn old_locking(&self) -> bool {
         *OLD_LOCKING
     }
+
+    /// Set the datastore's maintenance mode to `S3Refresh`, fetch from S3 object store, clear and
+    /// replace the local cache store contents. Once finished disable the maintenance mode again.
+    /// Returns with error for other datastore backends without setting the maintenance mode.
+    pub async fn s3_refresh(self: &Arc<Self>) -> Result<(), Error> {
+        match self.backend()? {
+            DatastoreBackend::Filesystem => bail!("store '{}' not backed by S3", self.name()),
+            DatastoreBackend::S3(s3_client) => {
+                let self_clone = Arc::clone(self);
+                tokio::task::spawn_blocking(move || {
+                    self_clone.maintenance_mode(Some(MaintenanceMode {
+                        ty: MaintenanceType::S3Refresh,
+                        message: None,
+                    }))
+                })
+                .await?
+                .context("failed to set maintenance mode")?;
+
+                let tmp_base = proxmox_sys::fs::make_tmp_dir(&self.base_path(), None)
+                    .context("failed to create temporary content folder in {store_base}")?;
+
+                self.fetch_tmp_contents(&tmp_base, &s3_client).await?;
+                self.move_tmp_contents_in_place(&tmp_base).await?;
+
+                let self_clone = Arc::clone(self);
+                tokio::task::spawn_blocking(move || self_clone.maintenance_mode(None))
+                    .await?
+                    .context("failed to clear maintenance mode")?;
+            }
+        }
+        Ok(())
+    }
+
+    // Set or clear the datastores maintenance mode by locking and updating the datastore config
+    fn maintenance_mode(&self, maintenance_mode: Option<MaintenanceMode>) -> Result<(), Error> {
+        let _lock = pbs_config::datastore::lock_config()?;
+        let (mut section_config, _digest) = pbs_config::datastore::config()?;
+        let mut datastore: DataStoreConfig = section_config.lookup("datastore", self.name())?;
+        datastore.set_maintenance_mode(maintenance_mode)?;
+        section_config.set_data(self.name(), "datastore", &datastore)?;
+        pbs_config::datastore::save_config(&section_config)?;
+        Ok(())
+    }
+
+    // Fetch the contents (metadata, no chunks) of the datastore from the S3 object store to the
+    // provided temporaray directory
+    async fn fetch_tmp_contents(&self, tmp_base: &Path, s3_client: &S3Client) -> Result<(), Error> {
+        let backup_user = pbs_config::backup_user().context("failed to get backup user")?;
+        let mode = nix::sys::stat::Mode::from_bits_truncate(0o0644);
+        let file_create_options = CreateOptions::new()
+            .perm(mode)
+            .owner(backup_user.uid)
+            .group(backup_user.gid);
+        let mode = nix::sys::stat::Mode::from_bits_truncate(0o0755);
+        let dir_create_options = CreateOptions::new()
+            .perm(mode)
+            .owner(backup_user.uid)
+            .group(backup_user.gid);
+
+        let list_prefix = S3PathPrefix::Some(S3_CONTENT_PREFIX.to_string());
+        let store_prefix = format!("{}/{S3_CONTENT_PREFIX}/", self.name());
+        let mut next_continuation_token: Option<String> = None;
+        loop {
+            let list_objects_result = s3_client
+                .list_objects_v2(&list_prefix, next_continuation_token.as_deref())
+                .await
+                .context("failed to list object")?;
+
+            let objects_to_fetch: Vec<S3ObjectKey> = list_objects_result
+                .contents
+                .into_iter()
+                .map(|item| item.key)
+                .collect();
+
+            for object_key in objects_to_fetch {
+                let object_path = format!("{object_key}");
+                let object_path = object_path.strip_prefix(&store_prefix).with_context(|| {
+                    format!("failed to strip store context prefix {store_prefix} for {object_key}")
+                })?;
+                if object_path.ends_with(NAMESPACE_MARKER_FILENAME) {
+                    continue;
+                }
+
+                info!("Fetching object {object_path}");
+
+                let file_path = tmp_base.join(object_path);
+                if let Some(parent) = file_path.parent() {
+                    proxmox_sys::fs::create_path(
+                        parent,
+                        Some(dir_create_options),
+                        Some(dir_create_options),
+                    )?;
+                }
+
+                let mut target_file = tokio::fs::OpenOptions::new()
+                    .write(true)
+                    .create(true)
+                    .truncate(true)
+                    .read(true)
+                    .open(&file_path)
+                    .await
+                    .with_context(|| format!("failed to create target file {file_path:?}"))?;
+
+                if let Some(response) = s3_client
+                    .get_object(object_key)
+                    .await
+                    .with_context(|| format!("failed to fetch object {object_path}"))?
+                {
+                    let data = response
+                        .content
+                        .collect()
+                        .await
+                        .context("failed to collect object contents")?;
+                    target_file
+                        .write_all(&data.to_bytes())
+                        .await
+                        .context("failed to write to target file")?;
+                    file_create_options
+                        .apply_to(&mut target_file, &file_path)
+                        .context("failed to set target file create options")?;
+                    target_file
+                        .flush()
+                        .await
+                        .context("failed to flush target file")?;
+                } else {
+                    bail!("failed to download {object_path}, not found");
+                }
+            }
+
+            if list_objects_result.is_truncated {
+                next_continuation_token = list_objects_result
+                    .next_continuation_token
+                    .as_ref()
+                    .cloned();
+                continue;
+            }
+            break;
+        }
+        Ok(())
+    }
+
+    // Fetch the contents (metadata, no chunks) of the datastore from the S3 object store to the
+    // provided temporaray directory
+    async fn move_tmp_contents_in_place(&self, tmp_base: &PathBuf) -> Result<(), Error> {
+        for ty in ["vm", "ct", "host", "ns"] {
+            let store_base_clone = self.base_path().clone();
+            let tmp_base_clone = tmp_base.clone();
+            tokio::task::spawn_blocking(move || {
+                let type_dir = store_base_clone.join(ty);
+                if let Err(err) = std::fs::remove_dir_all(&type_dir) {
+                    if err.kind() != io::ErrorKind::NotFound {
+                        return Err(err).with_context(|| {
+                            format!("failed to remove old contents in {type_dir:?}")
+                        });
+                    }
+                }
+                let tmp_type_dir = tmp_base_clone.join(ty);
+                if let Err(err) = std::fs::rename(&tmp_type_dir, &type_dir) {
+                    if err.kind() != io::ErrorKind::NotFound {
+                        return Err(err)
+                            .with_context(|| format!("failed to rename {tmp_type_dir:?}"));
+                    }
+                }
+                Ok::<(), Error>(())
+            })
+            .await?
+            .with_context(|| format!("failed to refresh {:?}", self.base_path()))?;
+        }
+
+        std::fs::remove_dir_all(&tmp_base)
+            .with_context(|| format!("failed to cleanup temporary content in {tmp_base:?}"))?;
+
+        Ok(())
+    }
 }
diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs
index 87a8641bd..23b216bef 100644
--- a/src/api2/admin/datastore.rs
+++ b/src/api2/admin/datastore.rs
@@ -2707,6 +2707,39 @@ pub async fn unmount(store: String, rpcenv: &mut dyn RpcEnvironment) -> Result<V
     Ok(json!(upid))
 }
 
+#[api(
+    protected: true,
+    input: {
+        properties: {
+            store: {
+                schema: DATASTORE_SCHEMA,
+            },
+        }
+    },
+    returns: {
+        schema: UPID_SCHEMA,
+    },
+    access: {
+        permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_MODIFY, false),
+    },
+)]
+/// Refresh datastore contents from S3 to local cache store.
+pub async fn s3_refresh(store: String, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
+    let datastore = DataStore::lookup_datastore(&store, Some(Operation::Lookup))?;
+    let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
+    let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
+
+    let upid = WorkerTask::spawn(
+        "s3-refresh",
+        Some(store),
+        auth_id.to_string(),
+        to_stdout,
+        move |_worker| async move { datastore.s3_refresh().await },
+    )?;
+
+    Ok(json!(upid))
+}
+
 #[sortable]
 const DATASTORE_INFO_SUBDIRS: SubdirMap = &[
     (
@@ -2773,6 +2806,7 @@ const DATASTORE_INFO_SUBDIRS: SubdirMap = &[
         &Router::new().download(&API_METHOD_PXAR_FILE_DOWNLOAD),
     ),
     ("rrd", &Router::new().get(&API_METHOD_GET_RRD_STATS)),
+    ("s3-refresh", &Router::new().put(&API_METHOD_S3_REFRESH)),
     (
         "snapshots",
         &Router::new()
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 37/46] cli: add dedicated subcommand for datastore s3 refresh
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (38 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 36/46] api/datastore: implement refresh endpoint for stores with s3 backend Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 38/46] ui: render s3 refresh as valid maintenance type and task description Christian Ebner
                   ` (11 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Allows to manually trigger an s3 refresh via proxmox-backup-manager
by calling the corresponding api endpoint handler.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- no changes

 src/bin/proxmox_backup_manager/datastore.rs | 30 +++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/src/bin/proxmox_backup_manager/datastore.rs b/src/bin/proxmox_backup_manager/datastore.rs
index 1922a55a2..4d8b8bf3a 100644
--- a/src/bin/proxmox_backup_manager/datastore.rs
+++ b/src/bin/proxmox_backup_manager/datastore.rs
@@ -290,6 +290,30 @@ async fn uuid_mount(param: Value, _rpcenv: &mut dyn RpcEnvironment) -> Result<Va
     Ok(Value::Null)
 }
 
+#[api(
+    protected: true,
+    input: {
+        properties: {
+            store: {
+                schema: DATASTORE_SCHEMA,
+            },
+        },
+    },
+)]
+/// Refresh datastore contents from S3 to local cache store.
+async fn s3_refresh(mut param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
+    param["node"] = "localhost".into();
+
+    let info = &api2::admin::datastore::API_METHOD_S3_REFRESH;
+    let result = match info.handler {
+        ApiHandler::Async(handler) => (handler)(param, info, rpcenv).await?,
+        _ => unreachable!(),
+    };
+
+    crate::wait_for_local_worker(result.as_str().unwrap()).await?;
+    Ok(())
+}
+
 pub fn datastore_commands() -> CommandLineInterface {
     let cmd_def = CliCommandMap::new()
         .insert("list", CliCommand::new(&API_METHOD_LIST_DATASTORES))
@@ -302,6 +326,12 @@ pub fn datastore_commands() -> CommandLineInterface {
                     pbs_config::datastore::complete_removable_datastore_name,
                 ),
         )
+        .insert(
+            "s3-refresh",
+            CliCommand::new(&API_METHOD_S3_REFRESH)
+                .arg_param(&["store"])
+                .completion_cb("store", pbs_config::datastore::complete_datastore_name),
+        )
         .insert(
             "show",
             CliCommand::new(&API_METHOD_SHOW_DATASTORE)
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 38/46] ui: render s3 refresh as valid maintenance type and task description
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (39 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 37/46] cli: add dedicated subcommand for datastore s3 refresh Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 39/46] ui: expose s3 refresh button for datastores backed by object store Christian Ebner
                   ` (10 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Analogous to the maintenance type `unmount`, show the `s3-refresh` as
translated string in the maintenance mode options and task
description.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- no changes

 www/Utils.js                     | 4 ++++
 www/window/MaintenanceOptions.js | 6 +++++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/www/Utils.js b/www/Utils.js
index 30b4a6e79..61f504c3e 100644
--- a/www/Utils.js
+++ b/www/Utils.js
@@ -441,6 +441,7 @@ Ext.define('PBS.Utils', {
             prunejob: (type, id) => PBS.Utils.render_prune_job_worker_id(id, gettext('Prune Job')),
             reader: (type, id) => PBS.Utils.render_datastore_worker_id(id, gettext('Read Objects')),
             'rewind-media': [gettext('Drive'), gettext('Rewind Media')],
+            's3-refresh': [gettext('Datastore'), gettext('S3 Refresh')],
             sync: ['Datastore', gettext('Remote Sync')],
             syncjob: [gettext('Sync Job'), gettext('Remote Sync')],
             'tape-backup': (type, id) =>
@@ -838,6 +839,9 @@ Ext.define('PBS.Utils', {
             case 'unmount':
                 modeText = gettext('Unmounting');
                 break;
+            case 's3-refresh':
+                modeText = gettext('S3 refresh');
+                break;
         }
         return `${modeText} ${extra}`;
     },
diff --git a/www/window/MaintenanceOptions.js b/www/window/MaintenanceOptions.js
index 292353556..9a735e5e8 100644
--- a/www/window/MaintenanceOptions.js
+++ b/www/window/MaintenanceOptions.js
@@ -90,13 +90,17 @@ Ext.define('PBS.window.MaintenanceOptions', {
         }
 
         let unmounting = options['maintenance-type'] === 'unmount';
+        let s3Refresh = options['maintenance-type'] === 's3-refresh';
         let defaultType = options['maintenance-type'] === '__default__';
         if (unmounting) {
             options['maintenance-type'] = gettext('Unmounting');
         }
+        if (s3Refresh) {
+            options['maintenance-type'] = gettext('S3 Refresh');
+        }
 
         me.callParent([options]);
 
-        me.lookupReference('message-field').setDisabled(unmounting || defaultType);
+        me.lookupReference('message-field').setDisabled(unmounting || s3Refresh || defaultType);
     },
 });
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 39/46] ui: expose s3 refresh button for datastores backed by object store
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (40 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 38/46] ui: render s3 refresh as valid maintenance type and task description Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 40/46] datastore: conditionally upload atime marker chunk to s3 backend Christian Ebner
                   ` (9 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Allows to trigger a refresh of the local datastore contents from
the WebUI.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- expose s3 refresh button in contents tab inside of 'More' dropdown
  instead of in summary tab.

 www/datastore/Content.js | 48 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/www/datastore/Content.js b/www/datastore/Content.js
index e7fa3a8f7..37156c1cb 100644
--- a/www/datastore/Content.js
+++ b/www/datastore/Content.js
@@ -123,6 +123,22 @@ Ext.define('PBS.DataStoreContent', {
             this.store.load();
         },
 
+        s3Refresh: function () {
+            let me = this;
+            let view = me.getView();
+            Proxmox.Utils.API2Request({
+                url: `/admin/datastore/${view.datastore}/s3-refresh`,
+                method: 'PUT',
+                failure: (response) => Ext.Msg.alert(gettext('Error'), response.htmlStatus),
+                success: function (response, options) {
+                    Ext.create('Proxmox.window.TaskViewer', {
+                        upid: response.result.data,
+                        taskDone: () => me.reload(),
+                    }).show();
+                },
+            });
+        },
+
         getRecordGroups: function (records) {
             let groups = {};
 
@@ -1273,12 +1289,44 @@ Ext.define('PBS.DataStoreContent', {
         },
     ],
 
+    initComponent: function () {
+        let me = this;
+
+        me.callParent();
+
+        Proxmox.Utils.API2Request({
+            url: `/config/datastore/${me.datastore}`,
+            failure: (response) => Ext.Msg.alert(gettext('Error'), response.htmlStatus),
+            success: function (response, options) {
+                let data = response.result.data;
+                if (data.backend) {
+                    let backendConfig = PBS.Utils.parsePropertyString(data.backend);
+                    let hasS3Backend = backendConfig.type === 's3';
+                    me.down('#moreDropdown').setDisabled(!hasS3Backend);
+                }
+            },
+        });
+    },
+
     tbar: [
         {
             text: gettext('Reload'),
             iconCls: 'fa fa-refresh',
             handler: 'reload',
         },
+        {
+            text: gettext('More'),
+            itemId: 'moreDropdown',
+            disabled: true,
+            menu: [
+                {
+                    text: gettext('Refresh contents from S3 bucket'),
+                    iconCls: 'fa fa-cloud-download',
+                    handler: 's3Refresh',
+                    selModel: false,
+                },
+            ],
+        },
         '-',
         {
             xtype: 'proxmoxButton',
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 40/46] datastore: conditionally upload atime marker chunk to s3 backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (41 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 39/46] ui: expose s3 refresh button for datastores backed by object store Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 41/46] bin: implement client subcommands for s3 configuration manipulation Christian Ebner
                   ` (8 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Since commit b18eab64 ("fix #5982: garbage collection: check atime
updates are honored"), the 4 MiB fixed sized, unencypted and
compressed chunk containing all zeros is inserted at datastore
creation if the atime safety check is enabled.

If the datastore is backed by an S3 object store, chunk uploads are
avoided by checking the presence of the chunks in the local cache
store. Therefore, the all zero chunk will however not be uploaded
since already inserted locally.

Fix this by conditionally uploading the chunk before performing the
atime update check for datastores backed by S3.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- use upload_no_replace_with_retry

 pbs-datastore/src/chunk_store.rs | 25 ++++++++++++++++++++++---
 pbs-datastore/src/datastore.rs   | 20 ++++++++++----------
 src/api2/config/datastore.rs     |  5 ++++-
 3 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/pbs-datastore/src/chunk_store.rs b/pbs-datastore/src/chunk_store.rs
index 95f00e8d5..3c59612bb 100644
--- a/pbs-datastore/src/chunk_store.rs
+++ b/pbs-datastore/src/chunk_store.rs
@@ -9,6 +9,7 @@ use tracing::{info, warn};
 
 use pbs_api_types::{DatastoreFSyncLevel, GarbageCollectionStatus};
 use proxmox_io::ReadExt;
+use proxmox_s3_client::S3Client;
 use proxmox_sys::fs::{create_dir, create_path, file_type_from_file_stat, CreateOptions};
 use proxmox_sys::process_locker::{
     ProcessLockExclusiveGuard, ProcessLockSharedGuard, ProcessLocker,
@@ -454,11 +455,29 @@ impl ChunkStore {
     /// Uses a 4 MiB fixed size, compressed but unencrypted chunk to test. The chunk is inserted in
     /// the chunk store if not yet present.
     /// Returns with error if the check could not be performed.
-    pub fn check_fs_atime_updates(&self, retry_on_file_changed: bool) -> Result<(), Error> {
+    pub fn check_fs_atime_updates(
+        &self,
+        retry_on_file_changed: bool,
+        s3_client: Option<Arc<S3Client>>,
+    ) -> Result<(), Error> {
         let (zero_chunk, digest) = DataChunkBuilder::build_zero_chunk(None, 4096 * 1024, true)?;
-        let (pre_existing, _) = self.insert_chunk(&zero_chunk, &digest)?;
         let (path, _digest) = self.chunk_path(&digest);
 
+        if let Some(ref s3_client) = s3_client {
+            if let Err(err) = std::fs::metadata(&path) {
+                if err.kind() == std::io::ErrorKind::NotFound {
+                    let object_key = crate::s3::object_key_from_digest(&digest)?;
+                    proxmox_async::runtime::block_on(s3_client.upload_no_replace_with_retry(
+                        object_key,
+                        zero_chunk.raw_data().to_vec().into(),
+                    ))
+                    .context("failed to upload chunk to s3 backend")?;
+                }
+            }
+        }
+
+        let (pre_existing, _) = self.insert_chunk(&zero_chunk, &digest)?;
+
         // Take into account timestamp update granularity in the kernel
         // Blocking the thread is fine here since this runs in a worker.
         std::thread::sleep(Duration::from_secs(1));
@@ -478,7 +497,7 @@ impl ChunkStore {
         // two metadata calls, try to check once again on changed file
         if metadata_before.ino() != metadata_now.ino() {
             if retry_on_file_changed {
-                return self.check_fs_atime_updates(false);
+                return self.check_fs_atime_updates(false, s3_client);
             }
             bail!("chunk {path:?} changed twice during access time safety check, cannot proceed.");
         }
diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
index b2af05eac..17cc9042d 100644
--- a/pbs-datastore/src/datastore.rs
+++ b/pbs-datastore/src/datastore.rs
@@ -1543,10 +1543,19 @@ impl DataStore {
                     .parse_property_string(gc_store_config.tuning.as_deref().unwrap_or(""))?,
             )?;
 
+            let s3_client = match self.backend()? {
+                DatastoreBackend::Filesystem => None,
+                DatastoreBackend::S3(s3_client) => {
+                    proxmox_async::runtime::block_on(s3_client.head_bucket())
+                        .context("failed to reach bucket")?;
+                    Some(s3_client)
+                }
+            };
+
             if tuning.gc_atime_safety_check.unwrap_or(true) {
                 self.inner
                     .chunk_store
-                    .check_fs_atime_updates(true)
+                    .check_fs_atime_updates(true, s3_client.clone())
                     .context("atime safety check failed")?;
                 info!("Access time update check successful, proceeding with GC.");
             } else {
@@ -1585,15 +1594,6 @@ impl DataStore {
                 1024 * 1024
             };
 
-            let s3_client = match self.backend()? {
-                DatastoreBackend::Filesystem => None,
-                DatastoreBackend::S3(s3_client) => {
-                    proxmox_async::runtime::block_on(s3_client.head_bucket())
-                        .context("failed to reach bucket")?;
-                    Some(s3_client)
-                }
-            };
-
             info!("Start GC phase1 (mark used chunks)");
 
             self.mark_used_chunks(
diff --git a/src/api2/config/datastore.rs b/src/api2/config/datastore.rs
index 7a4a39074..9b87e01f2 100644
--- a/src/api2/config/datastore.rs
+++ b/src/api2/config/datastore.rs
@@ -1,4 +1,5 @@
 use std::path::{Path, PathBuf};
+use std::sync::Arc;
 
 use ::serde::{Deserialize, Serialize};
 use anyhow::{bail, format_err, Context, Error};
@@ -118,6 +119,7 @@ pub(crate) fn do_create_datastore(
             .parse_property_string(datastore.tuning.as_deref().unwrap_or(""))?,
     )?;
 
+    let mut backend_s3_client = None;
     if let Some(ref backend_config) = datastore.backend {
         let backend_config: DatastoreBackendConfig = backend_config.parse()?;
         match backend_config.ty.unwrap_or_default() {
@@ -151,6 +153,7 @@ pub(crate) fn do_create_datastore(
                 // Fine to block since this runs in worker task
                 proxmox_async::runtime::block_on(s3_client.head_bucket())
                     .context("failed to access bucket")?;
+                backend_s3_client = Some(Arc::new(s3_client));
             }
         }
     }
@@ -194,7 +197,7 @@ pub(crate) fn do_create_datastore(
 
     if tuning.gc_atime_safety_check.unwrap_or(true) {
         chunk_store
-            .check_fs_atime_updates(true)
+            .check_fs_atime_updates(true, backend_s3_client)
             .context("access time safety check failed")?;
         info!("Access time update check successful.");
     } else {
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 41/46] bin: implement client subcommands for s3 configuration manipulation
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (42 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 40/46] datastore: conditionally upload atime marker chunk to s3 backend Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 42/46] bin: expose reuse-datastore flag for proxmox-backup-manager Christian Ebner
                   ` (7 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Implement and expose the proxmox-backup-manager commands to interact
with the s3 client configuration.

This mostly requires to insert the commands into the cli command map and
bind them to the corresponding api methods. The list method is the only
exception, as it requires rendering of the output given the provided
output format.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- no changes

 src/bin/proxmox_backup_manager/s3.rs | 70 +++++++++++++++++++++++++---
 1 file changed, 63 insertions(+), 7 deletions(-)

diff --git a/src/bin/proxmox_backup_manager/s3.rs b/src/bin/proxmox_backup_manager/s3.rs
index 9bb89ff55..82bc9413a 100644
--- a/src/bin/proxmox_backup_manager/s3.rs
+++ b/src/bin/proxmox_backup_manager/s3.rs
@@ -1,4 +1,4 @@
-use proxmox_router::{cli::*, RpcEnvironment};
+use proxmox_router::{cli::*, ApiHandler, RpcEnvironment};
 use proxmox_s3_client::{S3_BUCKET_NAME_SCHEMA, S3_CLIENT_ID_SCHEMA};
 use proxmox_schema::api;
 
@@ -34,13 +34,69 @@ async fn check(
     Ok(Value::Null)
 }
 
+#[api(
+    input: {
+        properties: {
+            "output-format": {
+                schema: OUTPUT_FORMAT,
+                optional: true,
+            },
+        }
+    }
+)]
+/// List configured s3 clients.
+fn list_s3_clients(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
+    let output_format = get_output_format(&param);
+
+    let info = &api2::config::s3::API_METHOD_LIST_S3_CLIENT_CONFIG;
+    let mut data = match info.handler {
+        ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
+        _ => unreachable!(),
+    };
+
+    let options = default_table_format_options()
+        .column(ColumnConfig::new("id"))
+        .column(ColumnConfig::new("endpoint"))
+        .column(ColumnConfig::new("port"))
+        .column(ColumnConfig::new("region"))
+        .column(ColumnConfig::new("access-key"))
+        .column(ColumnConfig::new("fingerprint"))
+        .column(ColumnConfig::new("path-style"));
+
+    format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
+
+    Ok(Value::Null)
+}
+
 pub fn s3_commands() -> CommandLineInterface {
-    let cmd_def = CliCommandMap::new().insert(
-        "check",
-        CliCommand::new(&API_METHOD_CHECK)
-            .arg_param(&["s3-client-id", "bucket"])
-            .completion_cb("s3-client-id", pbs_config::s3::complete_s3_client_id),
-    );
+    let client_cmd_def = CliCommandMap::new()
+        .insert("list", CliCommand::new(&API_METHOD_LIST_S3_CLIENTS))
+        .insert(
+            "create",
+            CliCommand::new(&api2::config::s3::API_METHOD_CREATE_S3_CLIENT_CONFIG)
+                .arg_param(&["id"]),
+        )
+        .insert(
+            "update",
+            CliCommand::new(&api2::config::s3::API_METHOD_UPDATE_S3_CLIENT_CONFIG)
+                .arg_param(&["id"])
+                .completion_cb("id", pbs_config::s3::complete_s3_client_id),
+        )
+        .insert(
+            "remove",
+            CliCommand::new(&api2::config::s3::API_METHOD_DELETE_S3_CLIENT_CONFIG)
+                .arg_param(&["id"])
+                .completion_cb("id", pbs_config::s3::complete_s3_client_id),
+        );
+
+    let cmd_def = CliCommandMap::new()
+        .insert(
+            "check",
+            CliCommand::new(&API_METHOD_CHECK)
+                .arg_param(&["s3-client-id", "bucket"])
+                .completion_cb("s3-client-id", pbs_config::s3::complete_s3_client_id),
+        )
+        .insert("client", client_cmd_def);
 
     cmd_def.into()
 }
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 42/46] bin: expose reuse-datastore flag for proxmox-backup-manager
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (43 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 41/46] bin: implement client subcommands for s3 configuration manipulation Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 43/46] datastore: mark store as in-use by setting marker on s3 backend Christian Ebner
                   ` (6 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

It is currently not possible to create a new datastore config and reuse
an existing datastore. Expose the `reuse-datastore` flag also for the
proxmox-backup-manager command, equivalent to what is already exposed in
the WebUI.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- no changes

 src/bin/proxmox_backup_manager/datastore.rs | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/bin/proxmox_backup_manager/datastore.rs b/src/bin/proxmox_backup_manager/datastore.rs
index 4d8b8bf3a..703974882 100644
--- a/src/bin/proxmox_backup_manager/datastore.rs
+++ b/src/bin/proxmox_backup_manager/datastore.rs
@@ -107,6 +107,12 @@ fn show_datastore(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value
                 type: DataStoreConfig,
                 flatten: true,
             },
+            "reuse-datastore": {
+                type: Boolean,
+                optional: true,
+                default: false,
+                description: "Re-use existing datastore directory."
+            },
             "output-format": {
                 schema: OUTPUT_FORMAT,
                 optional: true,
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 43/46] datastore: mark store as in-use by setting marker on s3 backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (44 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 42/46] bin: expose reuse-datastore flag for proxmox-backup-manager Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 44/46] datastore: run s3-refresh when reusing a datastore with " Christian Ebner
                   ` (5 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Adds an in-use marker on the S3 store to protect from accidental reuse
of the same datastore by multiple Proxmox Backup Server instances. Set
the marker file on store creation.

The local cache folder is however always assumed to be empty and needs
creation on datastore creation to guarantee consistency.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- no changes

 src/api2/config/datastore.rs | 44 +++++++++++++++++++++++++++++++++++-
 1 file changed, 43 insertions(+), 1 deletion(-)

diff --git a/src/api2/config/datastore.rs b/src/api2/config/datastore.rs
index 9b87e01f2..67ce6505b 100644
--- a/src/api2/config/datastore.rs
+++ b/src/api2/config/datastore.rs
@@ -4,6 +4,7 @@ use std::sync::Arc;
 use ::serde::{Deserialize, Serialize};
 use anyhow::{bail, format_err, Context, Error};
 use hex::FromHex;
+use http_body_util::BodyExt;
 use serde_json::Value;
 use tracing::{info, warn};
 
@@ -35,10 +36,20 @@ use pbs_config::CachedUserInfo;
 
 use pbs_datastore::get_datastore_mount_status;
 use proxmox_rest_server::WorkerTask;
+use proxmox_s3_client::S3ObjectKey;
 
 use crate::server::jobstate;
 use crate::tools::disks::unmount_by_mountpoint;
 
+const S3_DATASTORE_IN_USE_MARKER: &str = ".in-use";
+
+#[derive(Default, serde::Deserialize, serde::Serialize)]
+#[serde(rename_all = "kebab-case")]
+struct InUseContent {
+    #[serde(skip_serializing_if = "Option::is_none")]
+    hostname: Option<String>,
+}
+
 #[api(
     input: {
         properties: {},
@@ -153,6 +164,23 @@ pub(crate) fn do_create_datastore(
                 // Fine to block since this runs in worker task
                 proxmox_async::runtime::block_on(s3_client.head_bucket())
                     .context("failed to access bucket")?;
+
+                let object_key = S3ObjectKey::from(S3_DATASTORE_IN_USE_MARKER);
+                if let Some(response) =
+                    proxmox_async::runtime::block_on(s3_client.get_object(object_key.clone()))
+                        .context("failed to get in-use marker from bucket")?
+                {
+                    let content = proxmox_async::runtime::block_on(response.content.collect())
+                        .unwrap_or_default();
+                    let content =
+                        String::from_utf8(content.to_bytes().to_vec()).unwrap_or_default();
+                    let in_use: InUseContent = serde_json::from_str(&content).unwrap_or_default();
+                    if let Some(hostname) = in_use.hostname {
+                        bail!("Bucket already contains datastore in use by host {hostname}");
+                    } else {
+                        bail!("Bucket already contains datastore in use");
+                    }
+                }
                 backend_s3_client = Some(Arc::new(s3_client));
             }
         }
@@ -165,7 +193,7 @@ pub(crate) fn do_create_datastore(
         UnmountGuard::new(None)
     };
 
-    let chunk_store = if reuse_datastore {
+    let chunk_store = if reuse_datastore && backend_s3_client.is_none() {
         ChunkStore::verify_chunkstore(&path).and_then(|_| {
             // Must be the only instance accessing and locking the chunk store,
             // dropping will close all other locks from this process on the lockfile as well.
@@ -195,6 +223,20 @@ pub(crate) fn do_create_datastore(
         )?
     };
 
+    if let Some(ref s3_client) = backend_s3_client {
+        let object_key = S3ObjectKey::from(S3_DATASTORE_IN_USE_MARKER);
+        let content = serde_json::to_string(&InUseContent {
+            hostname: Some(proxmox_sys::nodename().to_string()),
+        })
+        .context("failed to encode hostname")?;
+        proxmox_async::runtime::block_on(s3_client.put_object(
+            object_key,
+            hyper::body::Bytes::from(content).into(),
+            true,
+        ))
+        .context("failed to upload in-use marker for datastore")?;
+    }
+
     if tuning.gc_atime_safety_check.unwrap_or(true) {
         chunk_store
             .check_fs_atime_updates(true, backend_s3_client)
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 44/46] datastore: run s3-refresh when reusing a datastore with s3 backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (45 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 43/46] datastore: mark store as in-use by setting marker on s3 backend Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 45/46] api/ui: add flag to allow overwriting in-use marker for " Christian Ebner
                   ` (4 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Instead of relying on the user to manually trigger the refresh after
datastore creation, do it already automatically in the datastore
creation task, thereby improving ergonomics.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- no changes

 src/api2/config/datastore.rs | 30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)

diff --git a/src/api2/config/datastore.rs b/src/api2/config/datastore.rs
index 67ce6505b..9f1cbe237 100644
--- a/src/api2/config/datastore.rs
+++ b/src/api2/config/datastore.rs
@@ -16,7 +16,7 @@ use proxmox_uuid::Uuid;
 
 use pbs_api_types::{
     Authid, DataStoreConfig, DataStoreConfigUpdater, DatastoreBackendConfig, DatastoreBackendType,
-    DatastoreNotify, DatastoreTuning, KeepOptions, MaintenanceMode, PruneJobConfig,
+    DatastoreNotify, DatastoreTuning, KeepOptions, MaintenanceMode, Operation, PruneJobConfig,
     PruneJobOptions, DATASTORE_SCHEMA, PRIV_DATASTORE_ALLOCATE, PRIV_DATASTORE_AUDIT,
     PRIV_DATASTORE_MODIFY, PRIV_SYS_MODIFY, PROXMOX_CONFIG_DIGEST_SCHEMA, UPID_SCHEMA,
 };
@@ -34,7 +34,7 @@ use crate::api2::config::tape_backup_job::{delete_tape_backup_job, list_tape_bac
 use crate::api2::config::verify::delete_verification_job;
 use pbs_config::CachedUserInfo;
 
-use pbs_datastore::get_datastore_mount_status;
+use pbs_datastore::{get_datastore_mount_status, DatastoreBackend};
 use proxmox_rest_server::WorkerTask;
 use proxmox_s3_client::S3ObjectKey;
 
@@ -343,19 +343,37 @@ pub fn create_datastore(
         ..config
     };
 
+    let store_name = config.name.to_string();
     WorkerTask::new_thread(
         "create-datastore",
-        Some(config.name.to_string()),
+        Some(store_name.clone()),
         auth_id.to_string(),
         to_stdout,
         move |_worker| {
             do_create_datastore(lock, section_config, config, reuse_datastore)?;
 
             if let Some(prune_job_config) = prune_job_config {
-                do_create_prune_job(prune_job_config)
-            } else {
-                Ok(())
+                do_create_prune_job(prune_job_config)?;
             }
+
+            if reuse_datastore {
+                let datastore = pbs_datastore::DataStore::lookup_datastore(
+                    &store_name,
+                    Some(Operation::Lookup),
+                )
+                .context("failed to lookup datastore")?;
+                match datastore
+                    .backend()
+                    .context("failed to get datastore backend")?
+                {
+                    DatastoreBackend::Filesystem => (),
+                    DatastoreBackend::S3(_s3_client) => {
+                        proxmox_async::runtime::block_on(datastore.s3_refresh())
+                            .context("S3 refresh failed")?;
+                    }
+                }
+            }
+            Ok(())
         },
     )
 }
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 45/46] api/ui: add flag to allow overwriting in-use marker for s3 backend
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (46 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 44/46] datastore: run s3-refresh when reusing a datastore with " Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 46/46] docs: Add section describing how to setup s3 backed datastore Christian Ebner
                   ` (3 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Datastores backed by an s3 object store mark the corresponding bucket
prefix given by the datastore name as in-use to protect from
accidental reuse of the same datastore from other instances.

If the datastore has to be re-created because the Proxmox Backup
Server instance is no longer available, skipping the check and
overwriting the marker with the current hostname is necessary.

Expose this flag to the datastore create api endpoint and expose
it to the web ui and cli command.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- fix formatting using proxmox-biome

 src/api2/config/datastore.rs                | 49 ++++++++++++++-------
 src/api2/node/disks/directory.rs            |  2 +-
 src/api2/node/disks/zfs.rs                  |  2 +-
 src/bin/proxmox_backup_manager/datastore.rs |  6 +++
 www/window/DataStoreEdit.js                 | 22 +++++++++
 5 files changed, 63 insertions(+), 18 deletions(-)

diff --git a/src/api2/config/datastore.rs b/src/api2/config/datastore.rs
index 9f1cbe237..c91969f47 100644
--- a/src/api2/config/datastore.rs
+++ b/src/api2/config/datastore.rs
@@ -113,6 +113,7 @@ pub(crate) fn do_create_datastore(
     mut config: SectionConfigData,
     datastore: DataStoreConfig,
     reuse_datastore: bool,
+    overwrite_in_use: bool,
 ) -> Result<(), Error> {
     let path: PathBuf = datastore.absolute_path().into();
 
@@ -165,20 +166,23 @@ pub(crate) fn do_create_datastore(
                 proxmox_async::runtime::block_on(s3_client.head_bucket())
                     .context("failed to access bucket")?;
 
-                let object_key = S3ObjectKey::from(S3_DATASTORE_IN_USE_MARKER);
-                if let Some(response) =
-                    proxmox_async::runtime::block_on(s3_client.get_object(object_key.clone()))
-                        .context("failed to get in-use marker from bucket")?
-                {
-                    let content = proxmox_async::runtime::block_on(response.content.collect())
-                        .unwrap_or_default();
-                    let content =
-                        String::from_utf8(content.to_bytes().to_vec()).unwrap_or_default();
-                    let in_use: InUseContent = serde_json::from_str(&content).unwrap_or_default();
-                    if let Some(hostname) = in_use.hostname {
-                        bail!("Bucket already contains datastore in use by host {hostname}");
-                    } else {
-                        bail!("Bucket already contains datastore in use");
+                if !overwrite_in_use {
+                    let object_key = S3ObjectKey::from(S3_DATASTORE_IN_USE_MARKER);
+                    if let Some(response) =
+                        proxmox_async::runtime::block_on(s3_client.get_object(object_key.clone()))
+                            .context("failed to get in-use marker from bucket")?
+                    {
+                        let content = proxmox_async::runtime::block_on(response.content.collect())
+                            .unwrap_or_default();
+                        let content =
+                            String::from_utf8(content.to_bytes().to_vec()).unwrap_or_default();
+                        let in_use: InUseContent =
+                            serde_json::from_str(&content).unwrap_or_default();
+                        if let Some(hostname) = in_use.hostname {
+                            bail!("Bucket already contains datastore in use by host {hostname}");
+                        } else {
+                            bail!("Bucket already contains datastore in use");
+                        }
                     }
                 }
                 backend_s3_client = Some(Arc::new(s3_client));
@@ -270,7 +274,13 @@ pub(crate) fn do_create_datastore(
                 optional: true,
                 default: false,
                 description: "Re-use existing datastore directory."
-            }
+            },
+            "overwrite-in-use": {
+                type: Boolean,
+                optional: true,
+                default: false,
+                description: "Overwrite in use marker (S3 backed datastores only)."
+            },
         },
     },
     access: {
@@ -282,6 +292,7 @@ pub(crate) fn do_create_datastore(
 pub fn create_datastore(
     config: DataStoreConfig,
     reuse_datastore: bool,
+    overwrite_in_use: bool,
     rpcenv: &mut dyn RpcEnvironment,
 ) -> Result<String, Error> {
     let lock = pbs_config::datastore::lock_config()?;
@@ -350,7 +361,13 @@ pub fn create_datastore(
         auth_id.to_string(),
         to_stdout,
         move |_worker| {
-            do_create_datastore(lock, section_config, config, reuse_datastore)?;
+            do_create_datastore(
+                lock,
+                section_config,
+                config,
+                reuse_datastore,
+                overwrite_in_use,
+            )?;
 
             if let Some(prune_job_config) = prune_job_config {
                 do_create_prune_job(prune_job_config)?;
diff --git a/src/api2/node/disks/directory.rs b/src/api2/node/disks/directory.rs
index 62f463437..74819079c 100644
--- a/src/api2/node/disks/directory.rs
+++ b/src/api2/node/disks/directory.rs
@@ -254,7 +254,7 @@ pub fn create_datastore_disk(
                 }
 
                 crate::api2::config::datastore::do_create_datastore(
-                    lock, config, datastore, false,
+                    lock, config, datastore, false, false,
                 )?;
             }
 
diff --git a/src/api2/node/disks/zfs.rs b/src/api2/node/disks/zfs.rs
index b6cf18265..cdb7cc6a1 100644
--- a/src/api2/node/disks/zfs.rs
+++ b/src/api2/node/disks/zfs.rs
@@ -314,7 +314,7 @@ pub fn create_zpool(
                 }
 
                 crate::api2::config::datastore::do_create_datastore(
-                    lock, config, datastore, false,
+                    lock, config, datastore, false, false,
                 )?;
             }
 
diff --git a/src/bin/proxmox_backup_manager/datastore.rs b/src/bin/proxmox_backup_manager/datastore.rs
index 703974882..45ad27049 100644
--- a/src/bin/proxmox_backup_manager/datastore.rs
+++ b/src/bin/proxmox_backup_manager/datastore.rs
@@ -113,6 +113,12 @@ fn show_datastore(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value
                 default: false,
                 description: "Re-use existing datastore directory."
             },
+            "overwrite-in-use": {
+                type: Boolean,
+                optional: true,
+                default: false,
+                description: "Overwrite in use marker (S3 backed datastores only)."
+            },
             "output-format": {
                 schema: OUTPUT_FORMAT,
                 optional: true,
diff --git a/www/window/DataStoreEdit.js b/www/window/DataStoreEdit.js
index 9826313b2..2e5f07598 100644
--- a/www/window/DataStoreEdit.js
+++ b/www/window/DataStoreEdit.js
@@ -76,6 +76,8 @@ Ext.define('PBS.DataStoreEdit', {
                                 let uuidEditField = inputPanel.down('[name=backing-device]');
                                 let bucketField = inputPanel.down('[name=bucket]');
                                 let s3ClientSelector = inputPanel.down('[name=s3client]');
+                                let overwriteInUseField =
+                                    inputPanel.down('[name=overwrite-in-use]');
 
                                 uuidEditField.setDisabled(!isRemovable);
                                 uuidEditField.allowBlank = !isRemovable;
@@ -89,6 +91,10 @@ Ext.define('PBS.DataStoreEdit', {
                                 s3ClientSelector.allowBlank = !isS3;
                                 s3ClientSelector.setValue('');
 
+                                overwriteInUseField.setHidden(!isS3);
+                                overwriteInUseField.setDisabled(!isS3);
+                                overwriteInUseField.setValue(false);
+
                                 if (isRemovable) {
                                     pathField.setFieldLabel(gettext('Path on Device'));
                                     pathField.setEmptyText(gettext('A relative path'));
@@ -176,6 +182,22 @@ Ext.define('PBS.DataStoreEdit', {
                         xtype: 'checkbox',
                         name: 'reuse-datastore',
                         fieldLabel: gettext('Reuse existing datastore'),
+                        listeners: {
+                            change: function (checkbox, selected) {
+                                let inputPanel = checkbox.up('inputpanel');
+                                let overwriteInUseField =
+                                    inputPanel.down('[name=overwrite-in-use]');
+                                overwriteInUseField.setDisabled(!selected);
+                                overwriteInUseField.setValue(false);
+                            },
+                        },
+                    },
+                ],
+                advancedColumn2: [
+                    {
+                        xtype: 'checkbox',
+                        name: 'overwrite-in-use',
+                        fieldLabel: gettext('Overwrite in-use marker'),
                     },
                 ],
 
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] [PATCH proxmox-backup v9 46/46] docs: Add section describing how to setup s3 backed datastore
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (47 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 45/46] api/ui: add flag to allow overwriting in-use marker for " Christian Ebner
@ 2025-07-19 12:50 ` Christian Ebner
  2025-07-21 14:24 ` [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Hannes Laimer
                   ` (2 subsequent siblings)
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-19 12:50 UTC (permalink / raw)
  To: pbs-devel

Describe required basic S3 client setup and possible configuration
options as well as the actual setup of a datastore using the client and
a bucket as backend.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 8:
- fix typos

 docs/storage.rst | 68 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)

diff --git a/docs/storage.rst b/docs/storage.rst
index 4a8d8255e..312c3c479 100644
--- a/docs/storage.rst
+++ b/docs/storage.rst
@@ -233,6 +233,74 @@ datastore is not mounted when they are scheduled. Sync jobs start, but fail
 with an error saying the datastore was not mounted. The reason is that syncs
 not happening as scheduled should at least be noticeable.
 
+Datastores with S3 Backend (experimental)
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Proxmox Backup Server supports S3 compatible object stores as storage backend for datastores. For
+this, an S3 client needs to be set-up under "Configuration" > "S3 Clients".
+
+In the client configuration, provide the REST API endpoint for the object store. The endpoint
+is provider dependent and allows for the bucket and region templating. For example, configuring
+the endpoint as e.g. ``{{bucket}}.s3.{{region}}.amazonaws.com`` will be expanded to
+``my-pbs-bucket.s3.eu-central-1.amazonaws.com`` with a configured bucket of name ``my-pbs-bucket``
+located in region ``eu-central-1``.
+
+The bucket name is part of the datastore backend configuration rather than the client configuration,
+as the same client might be reused for multiple bucket. Objects placed in the bucket are prefixed by
+the datastore name, therefore it is possible to create multiple datastores using the same bucket.
+
+.. note:: Proxmox Backup Server does not handle bucket creation and access control. The bucket used
+   to store the datastore's objects as well as the access key have to be setup beforehand in your S3
+   provider interface. The Proxmox Backup Server acts as client and requires permissions to get, put
+   list and delete objects in the bucket.
+
+Most providers allow to access buckets either using a vhost style addressing, the bucket name being
+part of the endpoint address, or via path style addressing, the bucket name being the prefix to
+the path components of requests. Proxmox Backup Server supports both styles, favoring the vhost
+style urls over the path style. To use path style addresses, set the corresponding configuration
+flag.
+
+Proxmox Backup Server does not support plain text communication with the S3 API, all communication
+is encrypted using HTTPS in transit. Therefore, for self-hosted S3 object stores using a self-signed
+certificate, the matching fingerprint has to be provided to the client configuration. Otherwise the
+client refuses connections to the S3 object store.
+
+The following example shows the setup of a new s3 client configuration:
+
+.. code-block:: console
+
+   # proxmox-backup-manager s3 client create my-s3-client --secrets-id my-s3-client --access-key 'my-access-key' --secret-key 'my-secret-key' --endpoint '{{bucket}}.s3.{{region}}.amazonaws.com' --region eu-central-1
+
+To list your s3 client configuration, run:
+
+.. code-block:: console
+
+   # proxmox-backup-manager s3 client list
+
+A new datastore with S3 backend can be created using one of the configures S3 clients. Although
+storing all contents on the S3 object store, the datastore requires nevertheless a local cache store,
+used to increase performance and reduce the number of requests to the backend. For this, a local
+filesystem path has to be provided during datastore creation, just like for regular datastore setup.
+A minimum size of a few GiB of storage is recommended, given that cache datastore contents include
+also data chunks.
+
+To setup a new datastore called ``my-s3-store`` placed in a bucket called ``pbs-s3-bucket``, run:
+
+.. code-block:: console
+
+   # proxmox-backup-manager datastore create my-s3-store /mnt/datastore/my-s3-store-cache --backend type=s3,client=my-s3-client,bucket=pbs-s3-bucket
+
+A datastore cannot be shared between multiple Proxmox Backup Server instances, only one instance can
+operate on the datastore at a time. However, datastore contents used on an instance which is no
+longer available can be reused on a fresh installation. To recreate the datastore, you must pass the
+``reuse-datastore`` and ``overwrite-in-use`` flags. Since the datastore name is used as prefix, the
+same datastore name must be used.
+
+.. code-block:: console
+
+   # proxmox-backup-manager datastore create my-s3-store /mnt/datastore/my-new-s3-store-cache --backend type=s3,client=my-s3-client,bucket=pbs-s3-bucket --reuse-datastore true --overwrite-in-use true
+
+
 Managing Datastores
 ^^^^^^^^^^^^^^^^^^^
 
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 01/46] datastore: add helpers for path/digest to s3 object key conversion
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 01/46] datastore: add helpers for path/digest to s3 object key conversion Christian Ebner
@ 2025-07-21 12:29   ` Hannes Laimer
  2025-07-21 12:51     ` Christian Ebner
  2025-07-21 13:58   ` Hannes Laimer
  1 sibling, 1 reply; 74+ messages in thread
From: Hannes Laimer @ 2025-07-21 12:29 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion; +Cc: pbs-devel

On Sat Jul 19, 2025 at 2:49 PM CEST, Christian Ebner wrote:
> Adds helper methods to generate the s3 object keys given a relative
> path and filename for datastore contents or digest in case of chunk
> files.
>
> Regular datastore contents are stored by grouping them with a content
> prefix in the object key. In order to keep the object key length
> small, given the max limit of 1024 bytes {0], `.cnt` is used as
> content prefix. Chunks on the other hand are prefixed by `.chunks`,
> same as on regular datastores.
>
> The prefix allows for selective listing of either contents or chunks
> by providing the prefix to the respective api calls.
>
> [0] https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
>
> Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
> ---
> changes since version 8:
> - added unit tests for helper functions
>
>  Cargo.toml               |   1 +
>  pbs-datastore/Cargo.toml |   1 +
>  pbs-datastore/src/lib.rs |   1 +
>  pbs-datastore/src/s3.rs  | 114 +++++++++++++++++++++++++++++++++++++++
>  4 files changed, 117 insertions(+)
>  create mode 100644 pbs-datastore/src/s3.rs
>
> diff --git a/Cargo.toml b/Cargo.toml
> index adfa427d1..97783ddd5 100644
> --- a/Cargo.toml
> +++ b/Cargo.toml
> @@ -77,6 +77,7 @@ proxmox-rest-server = { version = "1", features = [ "templates" ] }
>  proxmox-router = { version = "3.2.2", default-features = false }
>  proxmox-rrd = "1"
>  proxmox-rrd-api-types = "1.0.2"
> +proxmox-s3-client = "1.0.0"
>  # everything but pbs-config and pbs-client use "api-macro"
>  proxmox-schema = "4"
>  proxmox-section-config = "3"
> diff --git a/pbs-datastore/Cargo.toml b/pbs-datastore/Cargo.toml
> index 56f6e9094..c42eff165 100644
> --- a/pbs-datastore/Cargo.toml
> +++ b/pbs-datastore/Cargo.toml
> @@ -34,6 +34,7 @@ proxmox-borrow.workspace = true
>  proxmox-human-byte.workspace = true
>  proxmox-io.workspace = true
>  proxmox-lang.workspace=true
> +proxmox-s3-client = { workspace = true, features = [ "impl" ] }
>  proxmox-schema = { workspace = true, features = [ "api-macro" ] }
>  proxmox-serde = { workspace = true, features = [ "serde_json" ] }
>  proxmox-sys.workspace = true
> diff --git a/pbs-datastore/src/lib.rs b/pbs-datastore/src/lib.rs
> index 5014b6c09..ffd0d91b2 100644
> --- a/pbs-datastore/src/lib.rs
> +++ b/pbs-datastore/src/lib.rs
> @@ -182,6 +182,7 @@ pub mod manifest;
>  pub mod paperkey;
>  pub mod prune;
>  pub mod read_chunk;
> +pub mod s3;
>  pub mod store_progress;
>  pub mod task_tracking;
>  
> diff --git a/pbs-datastore/src/s3.rs b/pbs-datastore/src/s3.rs
> new file mode 100644
> index 000000000..79e7548fb
> --- /dev/null
> +++ b/pbs-datastore/src/s3.rs
> @@ -0,0 +1,114 @@
> +use std::path::{Path, PathBuf};
> +
> +use anyhow::{bail, format_err, Error};
> +
> +use proxmox_s3_client::S3ObjectKey;
> +
> +/// Object key prefix to group regular datastore contents (not chunks)
> +pub const S3_CONTENT_PREFIX: &str = ".cnt";
> +
> +/// Generate a relative object key with content prefix from given path and filename
> +pub fn object_key_from_path(path: &Path, filename: &str) -> Result<S3ObjectKey, Error> {
> +    // Force the use of relative paths, otherwise this would loose the content prefix
> +    if path.is_absolute() {
> +        bail!("cannot generate object key from absolute path");
> +    }
> +    if filename.contains('/') {
> +        bail!("invalid filename containing slashes");
> +    }
> +    let mut object_path = PathBuf::from(S3_CONTENT_PREFIX);
> +    object_path.push(path);
> +    object_path.push(filename);
> +
> +    let object_key_str = object_path
> +        .to_str()
> +        .ok_or_else(|| format_err!("unexpected object key path"))?;
> +    Ok(S3ObjectKey::from(object_key_str))
> +}
> +
> +/// Generate a relative object key with chunk prefix from given digest
> +pub fn object_key_from_digest(digest: &[u8; 32]) -> Result<S3ObjectKey, Error> {
> +    let object_key = hex::encode(digest);
> +    let digest_prefix = &object_key[..4];
> +    let object_key_string = format!(".chunks/{digest_prefix}/{object_key}");

I just skimmed of the S3 key specs, but I was wondering if having the
`digest_prefix` in the key actually adds anything. For FSs sure, but S3?
They say this is just chars for them, they don't infer hierarchy on `/`s,
so whatever optimisation they do with the prefix present, they should
also do without it, no?

> +    Ok(S3ObjectKey::from(object_key_string.as_str()))
> +}
> +
> +/// Generate a relative object key with chunk prefix from given digest, extended by suffix
> +pub fn object_key_from_digest_with_suffix(
> +    digest: &[u8; 32],
> +    suffix: &str,
> +) -> Result<S3ObjectKey, Error> {
> +    if suffix.contains('/') {
> +        bail!("invalid suffix containing slashes");
> +    }
> +    let object_key = hex::encode(digest);
> +    let digest_prefix = &object_key[..4];
> +    let object_key_string = format!(".chunks/{digest_prefix}/{object_key}{suffix}");
> +    Ok(S3ObjectKey::from(object_key_string.as_str()))
> +}
> +
> +#[test]
> +fn test_object_key_from_path() {
> +    let path = Path::new("vm/100/2025-07-14T14:20:02Z");
> +    let filename = "drive-scsci0.img.fidx";
> +    assert_eq!(
> +        object_key_from_path(path, filename).unwrap().to_string(),
> +        ".cnt/vm/100/2025-07-14T14:20:02Z/drive-scsci0.img.fidx",
> +    );
> +}
> +
> +#[test]
> +fn test_object_key_from_empty_path() {
> +    let path = Path::new("");
> +    let filename = ".marker";
> +    assert_eq!(
> +        object_key_from_path(path, filename).unwrap().to_string(),
> +        ".cnt/.marker",
> +    );
> +}
> +
> +#[test]
> +fn test_object_key_from_absolute_path() {
> +    assert!(object_key_from_path(Path::new("/"), ".marker").is_err());
> +}
> +
> +#[test]
> +fn test_object_key_from_path_incorrect_filename() {
> +    assert!(object_key_from_path(Path::new(""), "/.marker").is_err());
> +}
> +
> +#[test]
> +fn test_object_key_from_digest() {
> +    use hex::FromHex;
> +    let digest =
> +        <[u8; 32]>::from_hex("bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8")
> +            .unwrap();
> +    assert_eq!(
> +        object_key_from_digest(&digest).unwrap().to_string(),
> +        ".chunks/bb9f/bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8",
> +    );
> +}
> +
> +#[test]
> +fn test_object_key_from_digest_with_suffix() {
> +    use hex::FromHex;
> +    let digest =
> +        <[u8; 32]>::from_hex("bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8")
> +            .unwrap();
> +    assert_eq!(
> +        object_key_from_digest_with_suffix(&digest, ".0.bad")
> +            .unwrap()
> +            .to_string(),
> +        ".chunks/bb9f/bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8.0.bad",
> +    );
> +}
> +
> +#[test]
> +fn test_object_key_from_digest_with_invalid_suffix() {
> +    use hex::FromHex;
> +    let digest =
> +        <[u8; 32]>::from_hex("bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8")
> +            .unwrap();
> +    assert!(object_key_from_digest_with_suffix(&digest, "/.0.bad").is_err());
> +}



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 01/46] datastore: add helpers for path/digest to s3 object key conversion
  2025-07-21 12:29   ` Hannes Laimer
@ 2025-07-21 12:51     ` Christian Ebner
  2025-07-21 12:55       ` Hannes Laimer
  0 siblings, 1 reply; 74+ messages in thread
From: Christian Ebner @ 2025-07-21 12:51 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Hannes Laimer; +Cc: pbs-devel

On 7/21/25 2:29 PM, Hannes Laimer wrote:
> On Sat Jul 19, 2025 at 2:49 PM CEST, Christian Ebner wrote:
>> Adds helper methods to generate the s3 object keys given a relative
>> path and filename for datastore contents or digest in case of chunk
>> files.
>>
>> Regular datastore contents are stored by grouping them with a content
>> prefix in the object key. In order to keep the object key length
>> small, given the max limit of 1024 bytes {0], `.cnt` is used as
>> content prefix. Chunks on the other hand are prefixed by `.chunks`,
>> same as on regular datastores.
>>
>> The prefix allows for selective listing of either contents or chunks
>> by providing the prefix to the respective api calls.
>>
>> [0] https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
>>
>> Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
>> ---
>> changes since version 8:
>> - added unit tests for helper functions
>>
>>   Cargo.toml               |   1 +
>>   pbs-datastore/Cargo.toml |   1 +
>>   pbs-datastore/src/lib.rs |   1 +
>>   pbs-datastore/src/s3.rs  | 114 +++++++++++++++++++++++++++++++++++++++
>>   4 files changed, 117 insertions(+)
>>   create mode 100644 pbs-datastore/src/s3.rs
>>
>> diff --git a/Cargo.toml b/Cargo.toml
>> index adfa427d1..97783ddd5 100644
>> --- a/Cargo.toml
>> +++ b/Cargo.toml
>> @@ -77,6 +77,7 @@ proxmox-rest-server = { version = "1", features = [ "templates" ] }
>>   proxmox-router = { version = "3.2.2", default-features = false }
>>   proxmox-rrd = "1"
>>   proxmox-rrd-api-types = "1.0.2"
>> +proxmox-s3-client = "1.0.0"
>>   # everything but pbs-config and pbs-client use "api-macro"
>>   proxmox-schema = "4"
>>   proxmox-section-config = "3"
>> diff --git a/pbs-datastore/Cargo.toml b/pbs-datastore/Cargo.toml
>> index 56f6e9094..c42eff165 100644
>> --- a/pbs-datastore/Cargo.toml
>> +++ b/pbs-datastore/Cargo.toml
>> @@ -34,6 +34,7 @@ proxmox-borrow.workspace = true
>>   proxmox-human-byte.workspace = true
>>   proxmox-io.workspace = true
>>   proxmox-lang.workspace=true
>> +proxmox-s3-client = { workspace = true, features = [ "impl" ] }
>>   proxmox-schema = { workspace = true, features = [ "api-macro" ] }
>>   proxmox-serde = { workspace = true, features = [ "serde_json" ] }
>>   proxmox-sys.workspace = true
>> diff --git a/pbs-datastore/src/lib.rs b/pbs-datastore/src/lib.rs
>> index 5014b6c09..ffd0d91b2 100644
>> --- a/pbs-datastore/src/lib.rs
>> +++ b/pbs-datastore/src/lib.rs
>> @@ -182,6 +182,7 @@ pub mod manifest;
>>   pub mod paperkey;
>>   pub mod prune;
>>   pub mod read_chunk;
>> +pub mod s3;
>>   pub mod store_progress;
>>   pub mod task_tracking;
>>   
>> diff --git a/pbs-datastore/src/s3.rs b/pbs-datastore/src/s3.rs
>> new file mode 100644
>> index 000000000..79e7548fb
>> --- /dev/null
>> +++ b/pbs-datastore/src/s3.rs
>> @@ -0,0 +1,114 @@
>> +use std::path::{Path, PathBuf};
>> +
>> +use anyhow::{bail, format_err, Error};
>> +
>> +use proxmox_s3_client::S3ObjectKey;
>> +
>> +/// Object key prefix to group regular datastore contents (not chunks)
>> +pub const S3_CONTENT_PREFIX: &str = ".cnt";
>> +
>> +/// Generate a relative object key with content prefix from given path and filename
>> +pub fn object_key_from_path(path: &Path, filename: &str) -> Result<S3ObjectKey, Error> {
>> +    // Force the use of relative paths, otherwise this would loose the content prefix
>> +    if path.is_absolute() {
>> +        bail!("cannot generate object key from absolute path");
>> +    }
>> +    if filename.contains('/') {
>> +        bail!("invalid filename containing slashes");
>> +    }
>> +    let mut object_path = PathBuf::from(S3_CONTENT_PREFIX);
>> +    object_path.push(path);
>> +    object_path.push(filename);
>> +
>> +    let object_key_str = object_path
>> +        .to_str()
>> +        .ok_or_else(|| format_err!("unexpected object key path"))?;
>> +    Ok(S3ObjectKey::from(object_key_str))
>> +}
>> +
>> +/// Generate a relative object key with chunk prefix from given digest
>> +pub fn object_key_from_digest(digest: &[u8; 32]) -> Result<S3ObjectKey, Error> {
>> +    let object_key = hex::encode(digest);
>> +    let digest_prefix = &object_key[..4];
>> +    let object_key_string = format!(".chunks/{digest_prefix}/{object_key}");
> 
> I just skimmed of the S3 key specs, but I was wondering if having the
> `digest_prefix` in the key actually adds anything. For FSs sure, but S3?
> They say this is just chars for them, they don't infer hierarchy on `/`s,
> so whatever optimisation they do with the prefix present, they should
> also do without it, no?

Yes, however the intention was to keep this analogous to the filesystem 
based datastore's in order to be able to fetch the contents by external 
tooling without the need to have a running PBS instance. So you could 
recreate a datastore locally if needed.

> 
>> +    Ok(S3ObjectKey::from(object_key_string.as_str()))
>> +}
>> +
>> +/// Generate a relative object key with chunk prefix from given digest, extended by suffix
>> +pub fn object_key_from_digest_with_suffix(
>> +    digest: &[u8; 32],
>> +    suffix: &str,
>> +) -> Result<S3ObjectKey, Error> {
>> +    if suffix.contains('/') {
>> +        bail!("invalid suffix containing slashes");
>> +    }
>> +    let object_key = hex::encode(digest);
>> +    let digest_prefix = &object_key[..4];
>> +    let object_key_string = format!(".chunks/{digest_prefix}/{object_key}{suffix}");
>> +    Ok(S3ObjectKey::from(object_key_string.as_str()))
>> +}
>> +
>> +#[test]
>> +fn test_object_key_from_path() {
>> +    let path = Path::new("vm/100/2025-07-14T14:20:02Z");
>> +    let filename = "drive-scsci0.img.fidx";
>> +    assert_eq!(
>> +        object_key_from_path(path, filename).unwrap().to_string(),
>> +        ".cnt/vm/100/2025-07-14T14:20:02Z/drive-scsci0.img.fidx",
>> +    );
>> +}
>> +
>> +#[test]
>> +fn test_object_key_from_empty_path() {
>> +    let path = Path::new("");
>> +    let filename = ".marker";
>> +    assert_eq!(
>> +        object_key_from_path(path, filename).unwrap().to_string(),
>> +        ".cnt/.marker",
>> +    );
>> +}
>> +
>> +#[test]
>> +fn test_object_key_from_absolute_path() {
>> +    assert!(object_key_from_path(Path::new("/"), ".marker").is_err());
>> +}
>> +
>> +#[test]
>> +fn test_object_key_from_path_incorrect_filename() {
>> +    assert!(object_key_from_path(Path::new(""), "/.marker").is_err());
>> +}
>> +
>> +#[test]
>> +fn test_object_key_from_digest() {
>> +    use hex::FromHex;
>> +    let digest =
>> +        <[u8; 32]>::from_hex("bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8")
>> +            .unwrap();
>> +    assert_eq!(
>> +        object_key_from_digest(&digest).unwrap().to_string(),
>> +        ".chunks/bb9f/bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8",
>> +    );
>> +}
>> +
>> +#[test]
>> +fn test_object_key_from_digest_with_suffix() {
>> +    use hex::FromHex;
>> +    let digest =
>> +        <[u8; 32]>::from_hex("bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8")
>> +            .unwrap();
>> +    assert_eq!(
>> +        object_key_from_digest_with_suffix(&digest, ".0.bad")
>> +            .unwrap()
>> +            .to_string(),
>> +        ".chunks/bb9f/bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8.0.bad",
>> +    );
>> +}
>> +
>> +#[test]
>> +fn test_object_key_from_digest_with_invalid_suffix() {
>> +    use hex::FromHex;
>> +    let digest =
>> +        <[u8; 32]>::from_hex("bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8")
>> +            .unwrap();
>> +    assert!(object_key_from_digest_with_suffix(&digest, "/.0.bad").is_err());
>> +}
> 
> 
> 
> _______________________________________________
> pbs-devel mailing list
> pbs-devel@lists.proxmox.com
> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
> 
> 



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 01/46] datastore: add helpers for path/digest to s3 object key conversion
  2025-07-21 12:51     ` Christian Ebner
@ 2025-07-21 12:55       ` Hannes Laimer
  0 siblings, 0 replies; 74+ messages in thread
From: Hannes Laimer @ 2025-07-21 12:55 UTC (permalink / raw)
  To: Christian Ebner, Proxmox Backup Server development discussion,
	Hannes Laimer
  Cc: pbs-devel

On Mon Jul 21, 2025 at 2:51 PM CEST, Christian Ebner wrote:
> On 7/21/25 2:29 PM, Hannes Laimer wrote:
>> On Sat Jul 19, 2025 at 2:49 PM CEST, Christian Ebner wrote:
>>> Adds helper methods to generate the s3 object keys given a relative
>>> path and filename for datastore contents or digest in case of chunk
>>> files.
>>>
>>> Regular datastore contents are stored by grouping them with a content
>>> prefix in the object key. In order to keep the object key length
>>> small, given the max limit of 1024 bytes {0], `.cnt` is used as
>>> content prefix. Chunks on the other hand are prefixed by `.chunks`,
>>> same as on regular datastores.
>>>
>>> The prefix allows for selective listing of either contents or chunks
>>> by providing the prefix to the respective api calls.
>>>
>>> [0] https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
>>>
>>> Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
>>> ---
>>> changes since version 8:
>>> - added unit tests for helper functions
>>>
>>>   Cargo.toml               |   1 +
>>>   pbs-datastore/Cargo.toml |   1 +
>>>   pbs-datastore/src/lib.rs |   1 +
>>>   pbs-datastore/src/s3.rs  | 114 +++++++++++++++++++++++++++++++++++++++
>>>   4 files changed, 117 insertions(+)
>>>   create mode 100644 pbs-datastore/src/s3.rs
>>>
>>> diff --git a/Cargo.toml b/Cargo.toml
>>> index adfa427d1..97783ddd5 100644
>>> --- a/Cargo.toml
>>> +++ b/Cargo.toml
>>> @@ -77,6 +77,7 @@ proxmox-rest-server = { version = "1", features = [ "templates" ] }
>>>   proxmox-router = { version = "3.2.2", default-features = false }
>>>   proxmox-rrd = "1"
>>>   proxmox-rrd-api-types = "1.0.2"
>>> +proxmox-s3-client = "1.0.0"
>>>   # everything but pbs-config and pbs-client use "api-macro"
>>>   proxmox-schema = "4"
>>>   proxmox-section-config = "3"
>>> diff --git a/pbs-datastore/Cargo.toml b/pbs-datastore/Cargo.toml
>>> index 56f6e9094..c42eff165 100644
>>> --- a/pbs-datastore/Cargo.toml
>>> +++ b/pbs-datastore/Cargo.toml
>>> @@ -34,6 +34,7 @@ proxmox-borrow.workspace = true
>>>   proxmox-human-byte.workspace = true
>>>   proxmox-io.workspace = true
>>>   proxmox-lang.workspace=true
>>> +proxmox-s3-client = { workspace = true, features = [ "impl" ] }
>>>   proxmox-schema = { workspace = true, features = [ "api-macro" ] }
>>>   proxmox-serde = { workspace = true, features = [ "serde_json" ] }
>>>   proxmox-sys.workspace = true
>>> diff --git a/pbs-datastore/src/lib.rs b/pbs-datastore/src/lib.rs
>>> index 5014b6c09..ffd0d91b2 100644
>>> --- a/pbs-datastore/src/lib.rs
>>> +++ b/pbs-datastore/src/lib.rs
>>> @@ -182,6 +182,7 @@ pub mod manifest;
>>>   pub mod paperkey;
>>>   pub mod prune;
>>>   pub mod read_chunk;
>>> +pub mod s3;
>>>   pub mod store_progress;
>>>   pub mod task_tracking;
>>>   
>>> diff --git a/pbs-datastore/src/s3.rs b/pbs-datastore/src/s3.rs
>>> new file mode 100644
>>> index 000000000..79e7548fb
>>> --- /dev/null
>>> +++ b/pbs-datastore/src/s3.rs
>>> @@ -0,0 +1,114 @@
>>> +use std::path::{Path, PathBuf};
>>> +
>>> +use anyhow::{bail, format_err, Error};
>>> +
>>> +use proxmox_s3_client::S3ObjectKey;
>>> +
>>> +/// Object key prefix to group regular datastore contents (not chunks)
>>> +pub const S3_CONTENT_PREFIX: &str = ".cnt";
>>> +
>>> +/// Generate a relative object key with content prefix from given path and filename
>>> +pub fn object_key_from_path(path: &Path, filename: &str) -> Result<S3ObjectKey, Error> {
>>> +    // Force the use of relative paths, otherwise this would loose the content prefix
>>> +    if path.is_absolute() {
>>> +        bail!("cannot generate object key from absolute path");
>>> +    }
>>> +    if filename.contains('/') {
>>> +        bail!("invalid filename containing slashes");
>>> +    }
>>> +    let mut object_path = PathBuf::from(S3_CONTENT_PREFIX);
>>> +    object_path.push(path);
>>> +    object_path.push(filename);
>>> +
>>> +    let object_key_str = object_path
>>> +        .to_str()
>>> +        .ok_or_else(|| format_err!("unexpected object key path"))?;
>>> +    Ok(S3ObjectKey::from(object_key_str))
>>> +}
>>> +
>>> +/// Generate a relative object key with chunk prefix from given digest
>>> +pub fn object_key_from_digest(digest: &[u8; 32]) -> Result<S3ObjectKey, Error> {
>>> +    let object_key = hex::encode(digest);
>>> +    let digest_prefix = &object_key[..4];
>>> +    let object_key_string = format!(".chunks/{digest_prefix}/{object_key}");
>> 
>> I just skimmed of the S3 key specs, but I was wondering if having the
>> `digest_prefix` in the key actually adds anything. For FSs sure, but S3?
>> They say this is just chars for them, they don't infer hierarchy on `/`s,
>> so whatever optimisation they do with the prefix present, they should
>> also do without it, no?
>
> Yes, however the intention was to keep this analogous to the filesystem 
> based datastore's in order to be able to fetch the contents by external 
> tooling without the need to have a running PBS instance. So you could 
> recreate a datastore locally if needed.
>

makes sense :) just didn't know if longer/shorter keys have any
performance implications(probably not I assume)

>> 
>>> +    Ok(S3ObjectKey::from(object_key_string.as_str()))
>>> +}
>>> +
>>> +/// Generate a relative object key with chunk prefix from given digest, extended by suffix
>>> +pub fn object_key_from_digest_with_suffix(
>>> +    digest: &[u8; 32],
>>> +    suffix: &str,
>>> +) -> Result<S3ObjectKey, Error> {
>>> +    if suffix.contains('/') {
>>> +        bail!("invalid suffix containing slashes");
>>> +    }
>>> +    let object_key = hex::encode(digest);
>>> +    let digest_prefix = &object_key[..4];
>>> +    let object_key_string = format!(".chunks/{digest_prefix}/{object_key}{suffix}");
>>> +    Ok(S3ObjectKey::from(object_key_string.as_str()))
>>> +}
>>> +
>>> +#[test]
>>> +fn test_object_key_from_path() {
>>> +    let path = Path::new("vm/100/2025-07-14T14:20:02Z");
>>> +    let filename = "drive-scsci0.img.fidx";
>>> +    assert_eq!(
>>> +        object_key_from_path(path, filename).unwrap().to_string(),
>>> +        ".cnt/vm/100/2025-07-14T14:20:02Z/drive-scsci0.img.fidx",
>>> +    );
>>> +}
>>> +
>>> +#[test]
>>> +fn test_object_key_from_empty_path() {
>>> +    let path = Path::new("");
>>> +    let filename = ".marker";
>>> +    assert_eq!(
>>> +        object_key_from_path(path, filename).unwrap().to_string(),
>>> +        ".cnt/.marker",
>>> +    );
>>> +}
>>> +
>>> +#[test]
>>> +fn test_object_key_from_absolute_path() {
>>> +    assert!(object_key_from_path(Path::new("/"), ".marker").is_err());
>>> +}
>>> +
>>> +#[test]
>>> +fn test_object_key_from_path_incorrect_filename() {
>>> +    assert!(object_key_from_path(Path::new(""), "/.marker").is_err());
>>> +}
>>> +
>>> +#[test]
>>> +fn test_object_key_from_digest() {
>>> +    use hex::FromHex;
>>> +    let digest =
>>> +        <[u8; 32]>::from_hex("bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8")
>>> +            .unwrap();
>>> +    assert_eq!(
>>> +        object_key_from_digest(&digest).unwrap().to_string(),
>>> +        ".chunks/bb9f/bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8",
>>> +    );
>>> +}
>>> +
>>> +#[test]
>>> +fn test_object_key_from_digest_with_suffix() {
>>> +    use hex::FromHex;
>>> +    let digest =
>>> +        <[u8; 32]>::from_hex("bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8")
>>> +            .unwrap();
>>> +    assert_eq!(
>>> +        object_key_from_digest_with_suffix(&digest, ".0.bad")
>>> +            .unwrap()
>>> +            .to_string(),
>>> +        ".chunks/bb9f/bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8.0.bad",
>>> +    );
>>> +}
>>> +
>>> +#[test]
>>> +fn test_object_key_from_digest_with_invalid_suffix() {
>>> +    use hex::FromHex;
>>> +    let digest =
>>> +        <[u8; 32]>::from_hex("bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8")
>>> +            .unwrap();
>>> +    assert!(object_key_from_digest_with_suffix(&digest, "/.0.bad").is_err());
>>> +}
>> 
>> 
>> 
>> _______________________________________________
>> pbs-devel mailing list
>> pbs-devel@lists.proxmox.com
>> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
>> 
>> 



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 15/46] datastore: local chunk reader: read chunks based on backend
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 15/46] datastore: local chunk reader: read chunks based on backend Christian Ebner
@ 2025-07-21 13:12   ` Hannes Laimer
  2025-07-21 13:24     ` Christian Ebner
  2025-07-21 13:36     ` Lukas Wagner
  0 siblings, 2 replies; 74+ messages in thread
From: Hannes Laimer @ 2025-07-21 13:12 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion; +Cc: pbs-devel

On Sat Jul 19, 2025 at 2:50 PM CEST, Christian Ebner wrote:
> Get and store the datastore's backend on local chunk reader
> instantiantion and fetch chunks based on the variant from either the
> filesystem or the s3 object store.
>
> By storing the backend variant, the s3 client is instantiated only
> once and reused until the local chunk reader instance is dropped.
>
> Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
> ---
> changes since version 8:
> - use Arc::clone() over .clone()
>
>  pbs-datastore/Cargo.toml                |  1 +
>  pbs-datastore/src/local_chunk_reader.rs | 38 +++++++++++++++++++++----
>  2 files changed, 33 insertions(+), 6 deletions(-)
>
> diff --git a/pbs-datastore/Cargo.toml b/pbs-datastore/Cargo.toml
> index 7e56dbd31..8ce930a94 100644
> --- a/pbs-datastore/Cargo.toml
> +++ b/pbs-datastore/Cargo.toml
> @@ -13,6 +13,7 @@ crc32fast.workspace = true
>  endian_trait.workspace = true
>  futures.workspace = true
>  hex = { workspace = true, features = [ "serde" ] }
> +http-body-util.workspace = true
>  hyper.workspace = true
>  libc.workspace = true
>  log.workspace = true
> diff --git a/pbs-datastore/src/local_chunk_reader.rs b/pbs-datastore/src/local_chunk_reader.rs
> index 05a70c068..667c97206 100644
> --- a/pbs-datastore/src/local_chunk_reader.rs
> +++ b/pbs-datastore/src/local_chunk_reader.rs
> @@ -3,17 +3,21 @@ use std::pin::Pin;
>  use std::sync::Arc;
>  
>  use anyhow::{bail, Error};
> +use http_body_util::BodyExt;
>  
>  use pbs_api_types::CryptMode;
>  use pbs_tools::crypt_config::CryptConfig;
> +use proxmox_s3_client::S3Client;
>  
>  use crate::data_blob::DataBlob;
> +use crate::datastore::DatastoreBackend;
>  use crate::read_chunk::{AsyncReadChunk, ReadChunk};
>  use crate::DataStore;
>  
>  #[derive(Clone)]
>  pub struct LocalChunkReader {
>      store: Arc<DataStore>,
> +    backend: DatastoreBackend,
>      crypt_config: Option<Arc<CryptConfig>>,
>      crypt_mode: CryptMode,
>  }
> @@ -24,8 +28,11 @@ impl LocalChunkReader {
>          crypt_config: Option<Arc<CryptConfig>>,
>          crypt_mode: CryptMode,
>      ) -> Self {
> +        // TODO: Error handling!
> +        let backend = store.backend().unwrap();

Was this missed, or was this intentionally left in?
I feel like we don't want to panic here :P
(correct me if I'm wrong, but I think we would whenever anything goes
wrong when connecting to s3?)

>          Self {
>              store,
> +            backend,
>              crypt_config,
>              crypt_mode,
>          }
> @@ -47,10 +54,26 @@ impl LocalChunkReader {
>      }
>  }
>  
> +async fn fetch(s3_client: Arc<S3Client>, digest: &[u8; 32]) -> Result<DataBlob, Error> {
> +    let object_key = crate::s3::object_key_from_digest(digest)?;
> +    if let Some(response) = s3_client.get_object(object_key).await? {
> +        let bytes = response.content.collect().await?.to_bytes();
> +        DataBlob::from_raw(bytes.to_vec())
> +    } else {
> +        bail!("no object with digest {}", hex::encode(digest));
> +    }
> +}
> +
>  impl ReadChunk for LocalChunkReader {
>      fn read_raw_chunk(&self, digest: &[u8; 32]) -> Result<DataBlob, Error> {
> -        let chunk = self.store.load_chunk(digest)?;
> +        let chunk = match &self.backend {
> +            DatastoreBackend::Filesystem => self.store.load_chunk(digest)?,
> +            DatastoreBackend::S3(s3_client) => {
> +                proxmox_async::runtime::block_on(fetch(Arc::clone(s3_client), digest))?
> +            }
> +        };
>          self.ensure_crypt_mode(chunk.crypt_mode()?)?;
> +
>          Ok(chunk)
>      }
>  
> @@ -69,11 +92,14 @@ impl AsyncReadChunk for LocalChunkReader {
>          digest: &'a [u8; 32],
>      ) -> Pin<Box<dyn Future<Output = Result<DataBlob, Error>> + Send + 'a>> {
>          Box::pin(async move {
> -            let (path, _) = self.store.chunk_path(digest);
> -
> -            let raw_data = tokio::fs::read(&path).await?;
> -
> -            let chunk = DataBlob::load_from_reader(&mut &raw_data[..])?;
> +            let chunk = match &self.backend {
> +                DatastoreBackend::Filesystem => {
> +                    let (path, _) = self.store.chunk_path(digest);
> +                    let raw_data = tokio::fs::read(&path).await?;
> +                    DataBlob::load_from_reader(&mut &raw_data[..])?
> +                }
> +                DatastoreBackend::S3(s3_client) => fetch(Arc::clone(s3_client), digest).await?,
> +            };
>              self.ensure_crypt_mode(chunk.crypt_mode()?)?;
>  
>              Ok(chunk)



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 15/46] datastore: local chunk reader: read chunks based on backend
  2025-07-21 13:12   ` Hannes Laimer
@ 2025-07-21 13:24     ` Christian Ebner
  2025-07-21 13:36     ` Lukas Wagner
  1 sibling, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-21 13:24 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Hannes Laimer; +Cc: pbs-devel

On 7/21/25 3:13 PM, Hannes Laimer wrote:
> On Sat Jul 19, 2025 at 2:50 PM CEST, Christian Ebner wrote:
>> Get and store the datastore's backend on local chunk reader
>> instantiantion and fetch chunks based on the variant from either the
>> filesystem or the s3 object store.
>>
>> By storing the backend variant, the s3 client is instantiated only
>> once and reused until the local chunk reader instance is dropped.
>>
>> Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
>> ---
>> changes since version 8:
>> - use Arc::clone() over .clone()
>>
>>   pbs-datastore/Cargo.toml                |  1 +
>>   pbs-datastore/src/local_chunk_reader.rs | 38 +++++++++++++++++++++----
>>   2 files changed, 33 insertions(+), 6 deletions(-)
>>
>> diff --git a/pbs-datastore/Cargo.toml b/pbs-datastore/Cargo.toml
>> index 7e56dbd31..8ce930a94 100644
>> --- a/pbs-datastore/Cargo.toml
>> +++ b/pbs-datastore/Cargo.toml
>> @@ -13,6 +13,7 @@ crc32fast.workspace = true
>>   endian_trait.workspace = true
>>   futures.workspace = true
>>   hex = { workspace = true, features = [ "serde" ] }
>> +http-body-util.workspace = true
>>   hyper.workspace = true
>>   libc.workspace = true
>>   log.workspace = true
>> diff --git a/pbs-datastore/src/local_chunk_reader.rs b/pbs-datastore/src/local_chunk_reader.rs
>> index 05a70c068..667c97206 100644
>> --- a/pbs-datastore/src/local_chunk_reader.rs
>> +++ b/pbs-datastore/src/local_chunk_reader.rs
>> @@ -3,17 +3,21 @@ use std::pin::Pin;
>>   use std::sync::Arc;
>>   
>>   use anyhow::{bail, Error};
>> +use http_body_util::BodyExt;
>>   
>>   use pbs_api_types::CryptMode;
>>   use pbs_tools::crypt_config::CryptConfig;
>> +use proxmox_s3_client::S3Client;
>>   
>>   use crate::data_blob::DataBlob;
>> +use crate::datastore::DatastoreBackend;
>>   use crate::read_chunk::{AsyncReadChunk, ReadChunk};
>>   use crate::DataStore;
>>   
>>   #[derive(Clone)]
>>   pub struct LocalChunkReader {
>>       store: Arc<DataStore>,
>> +    backend: DatastoreBackend,
>>       crypt_config: Option<Arc<CryptConfig>>,
>>       crypt_mode: CryptMode,
>>   }
>> @@ -24,8 +28,11 @@ impl LocalChunkReader {
>>           crypt_config: Option<Arc<CryptConfig>>,
>>           crypt_mode: CryptMode,
>>       ) -> Self {
>> +        // TODO: Error handling!
>> +        let backend = store.backend().unwrap();
> 
> Was this missed, or was this intentionally left in?
> I feel like we don't want to panic here :P
> (correct me if I'm wrong, but I think we would whenever anything goes
> wrong when connecting to s3?)

Ah, good catch, this was overlooked indeed, will fix this, thanks!


_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 17/46] verify: implement chunk verification for stores with s3 backend
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 17/46] verify: implement chunk verification for stores with s3 backend Christian Ebner
@ 2025-07-21 13:35   ` Hannes Laimer
  2025-07-21 13:38     ` Christian Ebner
  0 siblings, 1 reply; 74+ messages in thread
From: Hannes Laimer @ 2025-07-21 13:35 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion; +Cc: pbs-devel

On Sat Jul 19, 2025 at 2:50 PM CEST, Christian Ebner wrote:
> For datastores backed by an S3 compatible object store, rather than
> reading the chunks to be verified from the local filesystem, fetch
> them via the s3 client from the configured bucket.
>

Could we somehow take advantage of the hash S3 provides for objects[1]?
We can't use our hashes tough, so not if how we would...

[1] https://docs.aws.amazon.com/AmazonS3/latest/userguide/checking-object-integrity.html

> Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
> ---
> changes since version 8:
> - refactored corrupt chunk marking into more compact methods
>
>  src/backup/verify.rs | 118 +++++++++++++++++++++++++++++++++++++------
>  1 file changed, 103 insertions(+), 15 deletions(-)
>
> diff --git a/src/backup/verify.rs b/src/backup/verify.rs
> index dea10f618..9a2148b56 100644
> --- a/src/backup/verify.rs
> +++ b/src/backup/verify.rs
> @@ -5,6 +5,7 @@ use std::sync::{Arc, Mutex};
>  use std::time::Instant;
>  
>  use anyhow::{bail, Error};
> +use http_body_util::BodyExt;
>  use tracing::{error, info, warn};
>  
>  use proxmox_worker_task::WorkerTaskContext;
> @@ -15,7 +16,7 @@ use pbs_api_types::{
>      UPID,
>  };
>  use pbs_datastore::backup_info::{BackupDir, BackupGroup, BackupInfo};
> -use pbs_datastore::index::IndexFile;
> +use pbs_datastore::index::{ChunkReadInfo, IndexFile};
>  use pbs_datastore::manifest::{BackupManifest, FileInfo};
>  use pbs_datastore::{DataBlob, DataStore, DatastoreBackend, StoreProgress};
>  
> @@ -89,6 +90,38 @@ impl VerifyWorker {
>              }
>          }
>  
> +        if let Ok(DatastoreBackend::S3(s3_client)) = datastore.backend() {
> +            let suffix = format!(".{}.bad", counter);
> +            let target_key =
> +                match pbs_datastore::s3::object_key_from_digest_with_suffix(digest, &suffix) {
> +                    Ok(target_key) => target_key,
> +                    Err(err) => {
> +                        info!("could not generate target key for corrupted chunk {path:?} - {err}");
> +                        return;
> +                    }
> +                };
> +            let object_key = match pbs_datastore::s3::object_key_from_digest(digest) {
> +                Ok(object_key) => object_key,
> +                Err(err) => {
> +                    info!("could not generate object key for corrupted chunk {path:?} - {err}");
> +                    return;
> +                }
> +            };
> +            if proxmox_async::runtime::block_on(
> +                s3_client.copy_object(object_key.clone(), target_key),
> +            )
> +            .is_ok()
> +            {
> +                if proxmox_async::runtime::block_on(s3_client.delete_object(object_key)).is_err() {
> +                    info!("failed to delete corrupt chunk on s3 backend: {digest_str}");
> +                }
> +            } else {
> +                info!("failed to copy corrupt chunk on s3 backend: {digest_str}");
> +            }
> +        } else {
> +            info!("failed to get s3 backend while trying to rename bad chunk: {digest_str}");
> +        }
> +
>          match std::fs::rename(&path, &new_path) {
>              Ok(_) => {
>                  info!("corrupted chunk renamed to {:?}", &new_path);
> @@ -189,20 +222,13 @@ impl VerifyWorker {
>                  continue; // already verified or marked corrupt
>              }
>  
> -            match self.datastore.load_chunk(&info.digest) {
> -                Err(err) => {
> -                    self.corrupt_chunks.lock().unwrap().insert(info.digest);
> -                    error!("can't verify chunk, load failed - {err}");
> -                    errors.fetch_add(1, Ordering::SeqCst);
> -                    Self::rename_corrupted_chunk(self.datastore.clone(), &info.digest);
> -                }
> -                Ok(chunk) => {
> -                    let size = info.size();
> -                    read_bytes += chunk.raw_size();
> -                    decoder_pool.send((chunk, info.digest, size))?;
> -                    decoded_bytes += size;
> -                }
> -            }
> +            self.verify_chunk_by_backend(
> +                &info,
> +                &mut read_bytes,
> +                &mut decoded_bytes,
> +                Arc::clone(&errors),
> +                &decoder_pool,
> +            )?;
>          }
>  
>          decoder_pool.complete()?;
> @@ -228,6 +254,68 @@ impl VerifyWorker {
>          Ok(())
>      }
>  
> +    fn verify_chunk_by_backend(
> +        &self,
> +        info: &ChunkReadInfo,
> +        read_bytes: &mut u64,
> +        decoded_bytes: &mut u64,
> +        errors: Arc<AtomicUsize>,
> +        decoder_pool: &ParallelHandler<(DataBlob, [u8; 32], u64)>,
> +    ) -> Result<(), Error> {
> +        match &self.backend {
> +            DatastoreBackend::Filesystem => match self.datastore.load_chunk(&info.digest) {
> +                Err(err) => self.add_corrupt_chunk(
> +                    info.digest,
> +                    errors,
> +                    &format!("can't verify chunk, load failed - {err}"),
> +                ),
> +                Ok(chunk) => {
> +                    let size = info.size();
> +                    *read_bytes += chunk.raw_size();
> +                    decoder_pool.send((chunk, info.digest, size))?;
> +                    *decoded_bytes += size;
> +                }
> +            },
> +            DatastoreBackend::S3(s3_client) => {
> +                let object_key = pbs_datastore::s3::object_key_from_digest(&info.digest)?;
> +                match proxmox_async::runtime::block_on(s3_client.get_object(object_key)) {
> +                    Ok(Some(response)) => {
> +                        let bytes = proxmox_async::runtime::block_on(response.content.collect())?
> +                            .to_bytes();
> +                        let chunk = DataBlob::from_raw(bytes.to_vec())?;
> +                        let size = info.size();
> +                        *read_bytes += chunk.raw_size();
> +                        decoder_pool.send((chunk, info.digest, size))?;
> +                        *decoded_bytes += size;
> +                    }
> +                    Ok(None) => self.add_corrupt_chunk(
> +                        info.digest,
> +                        errors,
> +                        &format!(
> +                            "can't verify missing chunk with digest {}",
> +                            hex::encode(info.digest)
> +                        ),
> +                    ),
> +                    Err(err) => self.add_corrupt_chunk(
> +                        info.digest,
> +                        errors,
> +                        &format!("can't verify chunk, load failed - {err}"),
> +                    ),
> +                }
> +            }
> +        }
> +        Ok(())
> +    }
> +
> +    fn add_corrupt_chunk(&self, digest: [u8; 32], errors: Arc<AtomicUsize>, message: &str) {
> +        // Panic on poisoned mutex
> +        let mut corrupt_chunks = self.corrupt_chunks.lock().unwrap();
> +        corrupt_chunks.insert(digest);
> +        error!(message);
> +        errors.fetch_add(1, Ordering::SeqCst);
> +        Self::rename_corrupted_chunk(self.datastore.clone(), &digest);
> +    }
> +
>      fn verify_fixed_index(&self, backup_dir: &BackupDir, info: &FileInfo) -> Result<(), Error> {
>          let mut path = backup_dir.relative_path();
>          path.push(&info.filename);



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 15/46] datastore: local chunk reader: read chunks based on backend
  2025-07-21 13:12   ` Hannes Laimer
  2025-07-21 13:24     ` Christian Ebner
@ 2025-07-21 13:36     ` Lukas Wagner
  1 sibling, 0 replies; 74+ messages in thread
From: Lukas Wagner @ 2025-07-21 13:36 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Hannes Laimer

On Mon Jul 21, 2025 at 3:12 PM CEST, Hannes Laimer wrote:
>>  #[derive(Clone)]
>>  pub struct LocalChunkReader {
>>      store: Arc<DataStore>,
>> +    backend: DatastoreBackend,
>>      crypt_config: Option<Arc<CryptConfig>>,
>>      crypt_mode: CryptMode,
>>  }
>> @@ -24,8 +28,11 @@ impl LocalChunkReader {
>>          crypt_config: Option<Arc<CryptConfig>>,
>>          crypt_mode: CryptMode,
>>      ) -> Self {
>> +        // TODO: Error handling!
>> +        let backend = store.backend().unwrap();
>
> Was this missed, or was this intentionally left in?
> I feel like we don't want to panic here :P
> (correct me if I'm wrong, but I think we would whenever anything goes
> wrong when connecting to s3?)
>

Good catch! I remember spotting this during my review, but somehow
forgot to note it down in my email, my bad.

>>          Self {
>>              store,
>> +            backend,
>>              crypt_config,
>>              crypt_mode,
>>          }
>> @@ -47,10 +54,26 @@ impl LocalChunkReader {
>>      }
>>  }
>>  
>> +async fn fetch(s3_client: Arc<S3Client>, digest: &[u8; 32]) -> Result<DataBlob, Error> {
>> +    let object_key = crate::s3::object_key_from_digest(digest)?;
>> +    if let Some(response) = s3_client.get_object(object_key).await? {
>> +        let bytes = response.content.collect().await?.to_bytes();
>> +        DataBlob::from_raw(bytes.to_vec())
>> +    } else {
>> +        bail!("no object with digest {}", hex::encode(digest));
>> +    }
>> +}
>> +
>>  impl ReadChunk for LocalChunkReader {
>>      fn read_raw_chunk(&self, digest: &[u8; 32]) -> Result<DataBlob, Error> {
>> -        let chunk = self.store.load_chunk(digest)?;
>> +        let chunk = match &self.backend {
>> +            DatastoreBackend::Filesystem => self.store.load_chunk(digest)?,
>> +            DatastoreBackend::S3(s3_client) => {
>> +                proxmox_async::runtime::block_on(fetch(Arc::clone(s3_client), digest))?
>> +            }
>> +        };
>>          self.ensure_crypt_mode(chunk.crypt_mode()?)?;
>> +
>>          Ok(chunk)
>>      }
>>  
>> @@ -69,11 +92,14 @@ impl AsyncReadChunk for LocalChunkReader {
>>          digest: &'a [u8; 32],
>>      ) -> Pin<Box<dyn Future<Output = Result<DataBlob, Error>> + Send + 'a>> {
>>          Box::pin(async move {
>> -            let (path, _) = self.store.chunk_path(digest);
>> -
>> -            let raw_data = tokio::fs::read(&path).await?;
>> -
>> -            let chunk = DataBlob::load_from_reader(&mut &raw_data[..])?;
>> +            let chunk = match &self.backend {
>> +                DatastoreBackend::Filesystem => {
>> +                    let (path, _) = self.store.chunk_path(digest);
>> +                    let raw_data = tokio::fs::read(&path).await?;
>> +                    DataBlob::load_from_reader(&mut &raw_data[..])?
>> +                }
>> +                DatastoreBackend::S3(s3_client) => fetch(Arc::clone(s3_client), digest).await?,
>> +            };
>>              self.ensure_crypt_mode(chunk.crypt_mode()?)?;
>>  
>>              Ok(chunk)
>
>
>
> _______________________________________________
> pbs-devel mailing list
> pbs-devel@lists.proxmox.com
> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 17/46] verify: implement chunk verification for stores with s3 backend
  2025-07-21 13:35   ` Hannes Laimer
@ 2025-07-21 13:38     ` Christian Ebner
  2025-07-21 13:55       ` Christian Ebner
  0 siblings, 1 reply; 74+ messages in thread
From: Christian Ebner @ 2025-07-21 13:38 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Hannes Laimer; +Cc: pbs-devel

On 7/21/25 3:36 PM, Hannes Laimer wrote:
> On Sat Jul 19, 2025 at 2:50 PM CEST, Christian Ebner wrote:
>> For datastores backed by an S3 compatible object store, rather than
>> reading the chunks to be verified from the local filesystem, fetch
>> them via the s3 client from the configured bucket.
>>
> 
> Could we somehow take advantage of the hash S3 provides for objects[1]?
> We can't use our hashes tough, so not if how we would...
> 
> [1] https://docs.aws.amazon.com/AmazonS3/latest/userguide/checking-object-integrity.html
> This is actually on my todo  list, the issue here is that not all s3 
backends implement the checksuming as required. and for encrypted we 
would need the crc checksums.

That is why for now this does not use these values, but rather the 
regular download and check logic.


_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 18/46] datastore: create namespace marker in s3 backend
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 18/46] datastore: create namespace marker in " Christian Ebner
@ 2025-07-21 13:52   ` Hannes Laimer
  2025-07-21 14:01     ` Christian Ebner
  0 siblings, 1 reply; 74+ messages in thread
From: Hannes Laimer @ 2025-07-21 13:52 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion; +Cc: pbs-devel

On Sat Jul 19, 2025 at 2:50 PM CEST, Christian Ebner wrote:
> The S3 object store only allows to store objects, referenced by their
> key. For backup namespaces datastores however use directories, so
> they cannot be represented as one to one mapping.
>
> Instead, create an empty marker file for each namespace and operate
> based on that.
>
> Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
> ---
> changes since version 8:
> - use upload_on_replace_with_retry
>
>  pbs-datastore/src/datastore.rs | 10 ++++++++++
>  1 file changed, 10 insertions(+)
>
> diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
> index bc829c5b8..5bb4e1777 100644
> --- a/pbs-datastore/src/datastore.rs
> +++ b/pbs-datastore/src/datastore.rs
> @@ -44,6 +44,7 @@ static DATASTORE_MAP: LazyLock<Mutex<HashMap<String, Arc<DataStoreImpl>>>> =
>      LazyLock::new(|| Mutex::new(HashMap::new()));
>  
>  const GROUP_NOTES_FILE_NAME: &str = "notes";
> +const NAMESPACE_MARKER_FILENAME: &str = ".namespace";
>  
>  /// checks if auth_id is owner, or, if owner is a token, if
>  /// auth_id is the user of the token
> @@ -613,6 +614,15 @@ impl DataStore {
>          // construct ns before mkdir to enforce max-depth and name validity
>          let ns = BackupNamespace::from_parent_ns(parent, name)?;
>  
> +        if let DatastoreBackend::S3(s3_client) = self.backend()? {
> +            let object_key = crate::s3::object_key_from_path(&ns.path(), NAMESPACE_MARKER_FILENAME)
> +                .context("invalid namespace marker object key")?;


hmmm, I didn't test this, but with our NS max depth of 7 (IIRC) and the
linux max filename length of ~250 this "could" be larger than the 1024
bytes an object key can be. Maybe we should check for that, even though
I doubt this would ever be a problem...

> +            let _is_duplicate = proxmox_async::runtime::block_on(
> +                s3_client.upload_no_replace_with_retry(object_key, hyper::body::Bytes::from("")),
> +            )
> +            .context("failed to create namespace on s3 backend")?;
> +        }
> +
>          let mut ns_full_path = self.base_path();
>          ns_full_path.push(ns.path());
>  



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 17/46] verify: implement chunk verification for stores with s3 backend
  2025-07-21 13:38     ` Christian Ebner
@ 2025-07-21 13:55       ` Christian Ebner
  0 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-21 13:55 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Hannes Laimer; +Cc: pbs-devel

On 7/21/25 3:38 PM, Christian Ebner wrote:
> On 7/21/25 3:36 PM, Hannes Laimer wrote:
>> On Sat Jul 19, 2025 at 2:50 PM CEST, Christian Ebner wrote:
>>> For datastores backed by an S3 compatible object store, rather than
>>> reading the chunks to be verified from the local filesystem, fetch
>>> them via the s3 client from the configured bucket.
>>>
>>
>> Could we somehow take advantage of the hash S3 provides for objects[1]?
>> We can't use our hashes tough, so not if how we would...
>>
>> [1] https://docs.aws.amazon.com/AmazonS3/latest/userguide/checking- 
>> object-integrity.html
>> This is actually on my todo  list, the issue here is that not all s3 
> backends implement the checksuming as required. and for encrypted we 
> would need the crc checksums.
> 
> That is why for now this does not use these values, but rather the 
> regular download and check logic.

Further details and bugtracker issues where referenced in the
fn parse_x_amz_checksum_crc32_header(headers: &HeaderMap) method of 
https://lore.proxmox.com/pbs-devel/20250529143207.694497-13-c.ebner@proxmox.com/ 
which has been dropped since, as dead code.

Including them here for reference again as well:

+    // TODO: Integrity checks via CRC32 or SHA265 currently cannot be 
performed, since not
+    // supported by all S3 object store providers.
+    // See also:
+    // https://tracker.ceph.com/issues/63951
+    // https://tracker.ceph.com/issues/69105
+    // https://www.backblaze.com/docs/cloud-storage-s3-compatible-api


_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel

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

* Re: [pbs-devel] [PATCH proxmox-backup v9 01/46] datastore: add helpers for path/digest to s3 object key conversion
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 01/46] datastore: add helpers for path/digest to s3 object key conversion Christian Ebner
  2025-07-21 12:29   ` Hannes Laimer
@ 2025-07-21 13:58   ` Hannes Laimer
  2025-07-21 14:15     ` Christian Ebner
  1 sibling, 1 reply; 74+ messages in thread
From: Hannes Laimer @ 2025-07-21 13:58 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion; +Cc: pbs-devel

On Sat Jul 19, 2025 at 2:49 PM CEST, Christian Ebner wrote:
> Adds helper methods to generate the s3 object keys given a relative
> path and filename for datastore contents or digest in case of chunk
> files.
>
> Regular datastore contents are stored by grouping them with a content
> prefix in the object key. In order to keep the object key length
> small, given the max limit of 1024 bytes {0], `.cnt` is used as
> content prefix. Chunks on the other hand are prefixed by `.chunks`,
> same as on regular datastores.
>
> The prefix allows for selective listing of either contents or chunks
> by providing the prefix to the respective api calls.
>
> [0] https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
>
> Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
> ---
> changes since version 8:
> - added unit tests for helper functions
>
>  Cargo.toml               |   1 +
>  pbs-datastore/Cargo.toml |   1 +
>  pbs-datastore/src/lib.rs |   1 +
>  pbs-datastore/src/s3.rs  | 114 +++++++++++++++++++++++++++++++++++++++
>  4 files changed, 117 insertions(+)
>  create mode 100644 pbs-datastore/src/s3.rs
>
> diff --git a/Cargo.toml b/Cargo.toml
> index adfa427d1..97783ddd5 100644
> --- a/Cargo.toml
> +++ b/Cargo.toml
> @@ -77,6 +77,7 @@ proxmox-rest-server = { version = "1", features = [ "templates" ] }
>  proxmox-router = { version = "3.2.2", default-features = false }
>  proxmox-rrd = "1"
>  proxmox-rrd-api-types = "1.0.2"
> +proxmox-s3-client = "1.0.0"
>  # everything but pbs-config and pbs-client use "api-macro"
>  proxmox-schema = "4"
>  proxmox-section-config = "3"
> diff --git a/pbs-datastore/Cargo.toml b/pbs-datastore/Cargo.toml
> index 56f6e9094..c42eff165 100644
> --- a/pbs-datastore/Cargo.toml
> +++ b/pbs-datastore/Cargo.toml
> @@ -34,6 +34,7 @@ proxmox-borrow.workspace = true
>  proxmox-human-byte.workspace = true
>  proxmox-io.workspace = true
>  proxmox-lang.workspace=true
> +proxmox-s3-client = { workspace = true, features = [ "impl" ] }
>  proxmox-schema = { workspace = true, features = [ "api-macro" ] }
>  proxmox-serde = { workspace = true, features = [ "serde_json" ] }
>  proxmox-sys.workspace = true
> diff --git a/pbs-datastore/src/lib.rs b/pbs-datastore/src/lib.rs
> index 5014b6c09..ffd0d91b2 100644
> --- a/pbs-datastore/src/lib.rs
> +++ b/pbs-datastore/src/lib.rs
> @@ -182,6 +182,7 @@ pub mod manifest;
>  pub mod paperkey;
>  pub mod prune;
>  pub mod read_chunk;
> +pub mod s3;
>  pub mod store_progress;
>  pub mod task_tracking;
>  
> diff --git a/pbs-datastore/src/s3.rs b/pbs-datastore/src/s3.rs
> new file mode 100644
> index 000000000..79e7548fb
> --- /dev/null
> +++ b/pbs-datastore/src/s3.rs
> @@ -0,0 +1,114 @@
> +use std::path::{Path, PathBuf};
> +
> +use anyhow::{bail, format_err, Error};
> +
> +use proxmox_s3_client::S3ObjectKey;
> +
> +/// Object key prefix to group regular datastore contents (not chunks)
> +pub const S3_CONTENT_PREFIX: &str = ".cnt";
> +
> +/// Generate a relative object key with content prefix from given path and filename
> +pub fn object_key_from_path(path: &Path, filename: &str) -> Result<S3ObjectKey, Error> {
> +    // Force the use of relative paths, otherwise this would loose the content prefix
> +    if path.is_absolute() {
> +        bail!("cannot generate object key from absolute path");
> +    }
> +    if filename.contains('/') {
> +        bail!("invalid filename containing slashes");
> +    }
> +    let mut object_path = PathBuf::from(S3_CONTENT_PREFIX);
> +    object_path.push(path);
> +    object_path.push(filename);

similarly to what I wrote on [18/46] with NS names maxed out this could
end up being longer than the max len for a key... not sure how to best
deal with that though. Limiting NS name len won't really work cause we
just read them from the FS...

> +
> +    let object_key_str = object_path
> +        .to_str()
> +        .ok_or_else(|| format_err!("unexpected object key path"))?;
> +    Ok(S3ObjectKey::from(object_key_str))
> +}
> +
> +/// Generate a relative object key with chunk prefix from given digest
> +pub fn object_key_from_digest(digest: &[u8; 32]) -> Result<S3ObjectKey, Error> {
> +    let object_key = hex::encode(digest);
> +    let digest_prefix = &object_key[..4];
> +    let object_key_string = format!(".chunks/{digest_prefix}/{object_key}");
> +    Ok(S3ObjectKey::from(object_key_string.as_str()))
> +}
> +
> +/// Generate a relative object key with chunk prefix from given digest, extended by suffix
> +pub fn object_key_from_digest_with_suffix(
> +    digest: &[u8; 32],
> +    suffix: &str,
> +) -> Result<S3ObjectKey, Error> {
> +    if suffix.contains('/') {
> +        bail!("invalid suffix containing slashes");
> +    }
> +    let object_key = hex::encode(digest);
> +    let digest_prefix = &object_key[..4];
> +    let object_key_string = format!(".chunks/{digest_prefix}/{object_key}{suffix}");
> +    Ok(S3ObjectKey::from(object_key_string.as_str()))
> +}
> +
> +#[test]
> +fn test_object_key_from_path() {
> +    let path = Path::new("vm/100/2025-07-14T14:20:02Z");
> +    let filename = "drive-scsci0.img.fidx";
> +    assert_eq!(
> +        object_key_from_path(path, filename).unwrap().to_string(),
> +        ".cnt/vm/100/2025-07-14T14:20:02Z/drive-scsci0.img.fidx",
> +    );
> +}
> +
> +#[test]
> +fn test_object_key_from_empty_path() {
> +    let path = Path::new("");
> +    let filename = ".marker";
> +    assert_eq!(
> +        object_key_from_path(path, filename).unwrap().to_string(),
> +        ".cnt/.marker",
> +    );
> +}
> +
> +#[test]
> +fn test_object_key_from_absolute_path() {
> +    assert!(object_key_from_path(Path::new("/"), ".marker").is_err());
> +}
> +
> +#[test]
> +fn test_object_key_from_path_incorrect_filename() {
> +    assert!(object_key_from_path(Path::new(""), "/.marker").is_err());
> +}
> +
> +#[test]
> +fn test_object_key_from_digest() {
> +    use hex::FromHex;
> +    let digest =
> +        <[u8; 32]>::from_hex("bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8")
> +            .unwrap();
> +    assert_eq!(
> +        object_key_from_digest(&digest).unwrap().to_string(),
> +        ".chunks/bb9f/bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8",
> +    );
> +}
> +
> +#[test]
> +fn test_object_key_from_digest_with_suffix() {
> +    use hex::FromHex;
> +    let digest =
> +        <[u8; 32]>::from_hex("bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8")
> +            .unwrap();
> +    assert_eq!(
> +        object_key_from_digest_with_suffix(&digest, ".0.bad")
> +            .unwrap()
> +            .to_string(),
> +        ".chunks/bb9f/bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8.0.bad",
> +    );
> +}
> +
> +#[test]
> +fn test_object_key_from_digest_with_invalid_suffix() {
> +    use hex::FromHex;
> +    let digest =
> +        <[u8; 32]>::from_hex("bb9f8df61474d25e71fa00722318cd387396ca1736605e1248821cc0de3d3af8")
> +            .unwrap();
> +    assert!(object_key_from_digest_with_suffix(&digest, "/.0.bad").is_err());
> +}



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 18/46] datastore: create namespace marker in s3 backend
  2025-07-21 13:52   ` Hannes Laimer
@ 2025-07-21 14:01     ` Christian Ebner
  0 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-21 14:01 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Hannes Laimer; +Cc: pbs-devel

On 7/21/25 3:52 PM, Hannes Laimer wrote:
> On Sat Jul 19, 2025 at 2:50 PM CEST, Christian Ebner wrote:
>> The S3 object store only allows to store objects, referenced by their
>> key. For backup namespaces datastores however use directories, so
>> they cannot be represented as one to one mapping.
>>
>> Instead, create an empty marker file for each namespace and operate
>> based on that.
>>
>> Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
>> ---
>> changes since version 8:
>> - use upload_on_replace_with_retry
>>
>>   pbs-datastore/src/datastore.rs | 10 ++++++++++
>>   1 file changed, 10 insertions(+)
>>
>> diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
>> index bc829c5b8..5bb4e1777 100644
>> --- a/pbs-datastore/src/datastore.rs
>> +++ b/pbs-datastore/src/datastore.rs
>> @@ -44,6 +44,7 @@ static DATASTORE_MAP: LazyLock<Mutex<HashMap<String, Arc<DataStoreImpl>>>> =
>>       LazyLock::new(|| Mutex::new(HashMap::new()));
>>   
>>   const GROUP_NOTES_FILE_NAME: &str = "notes";
>> +const NAMESPACE_MARKER_FILENAME: &str = ".namespace";
>>   
>>   /// checks if auth_id is owner, or, if owner is a token, if
>>   /// auth_id is the user of the token
>> @@ -613,6 +614,15 @@ impl DataStore {
>>           // construct ns before mkdir to enforce max-depth and name validity
>>           let ns = BackupNamespace::from_parent_ns(parent, name)?;
>>   
>> +        if let DatastoreBackend::S3(s3_client) = self.backend()? {
>> +            let object_key = crate::s3::object_key_from_path(&ns.path(), NAMESPACE_MARKER_FILENAME)
>> +                .context("invalid namespace marker object key")?;
> 
> 
> hmmm, I didn't test this, but with our NS max depth of 7 (IIRC) and the
> linux max filename length of ~250 this "could" be larger than the 1024
> bytes an object key can be. Maybe we should check for that, even though
> I doubt this would ever be a problem..

Okay, can add a check to cover that as well.


_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 01/46] datastore: add helpers for path/digest to s3 object key conversion
  2025-07-21 13:58   ` Hannes Laimer
@ 2025-07-21 14:15     ` Christian Ebner
  2025-07-21 14:20       ` Hannes Laimer
  0 siblings, 1 reply; 74+ messages in thread
From: Christian Ebner @ 2025-07-21 14:15 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Hannes Laimer; +Cc: pbs-devel

On 7/21/25 3:58 PM, Hannes Laimer wrote:
> On Sat Jul 19, 2025 at 2:49 PM CEST, Christian Ebner wrote:
>> Adds helper methods to generate the s3 object keys given a relative
>> path and filename for datastore contents or digest in case of chunk
>> files.
>>
>> Regular datastore contents are stored by grouping them with a content
>> prefix in the object key. In order to keep the object key length
>> small, given the max limit of 1024 bytes {0], `.cnt` is used as
>> content prefix. Chunks on the other hand are prefixed by `.chunks`,
>> same as on regular datastores.
>>
>> The prefix allows for selective listing of either contents or chunks
>> by providing the prefix to the respective api calls.
>>
>> [0] https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
>>
>> Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
>> ---
>> changes since version 8:
>> - added unit tests for helper functions
>>
>>   Cargo.toml               |   1 +
>>   pbs-datastore/Cargo.toml |   1 +
>>   pbs-datastore/src/lib.rs |   1 +
>>   pbs-datastore/src/s3.rs  | 114 +++++++++++++++++++++++++++++++++++++++
>>   4 files changed, 117 insertions(+)
>>   create mode 100644 pbs-datastore/src/s3.rs
>>
>> diff --git a/Cargo.toml b/Cargo.toml
>> index adfa427d1..97783ddd5 100644
>> --- a/Cargo.toml
>> +++ b/Cargo.toml
>> @@ -77,6 +77,7 @@ proxmox-rest-server = { version = "1", features = [ "templates" ] }
>>   proxmox-router = { version = "3.2.2", default-features = false }
>>   proxmox-rrd = "1"
>>   proxmox-rrd-api-types = "1.0.2"
>> +proxmox-s3-client = "1.0.0"
>>   # everything but pbs-config and pbs-client use "api-macro"
>>   proxmox-schema = "4"
>>   proxmox-section-config = "3"
>> diff --git a/pbs-datastore/Cargo.toml b/pbs-datastore/Cargo.toml
>> index 56f6e9094..c42eff165 100644
>> --- a/pbs-datastore/Cargo.toml
>> +++ b/pbs-datastore/Cargo.toml
>> @@ -34,6 +34,7 @@ proxmox-borrow.workspace = true
>>   proxmox-human-byte.workspace = true
>>   proxmox-io.workspace = true
>>   proxmox-lang.workspace=true
>> +proxmox-s3-client = { workspace = true, features = [ "impl" ] }
>>   proxmox-schema = { workspace = true, features = [ "api-macro" ] }
>>   proxmox-serde = { workspace = true, features = [ "serde_json" ] }
>>   proxmox-sys.workspace = true
>> diff --git a/pbs-datastore/src/lib.rs b/pbs-datastore/src/lib.rs
>> index 5014b6c09..ffd0d91b2 100644
>> --- a/pbs-datastore/src/lib.rs
>> +++ b/pbs-datastore/src/lib.rs
>> @@ -182,6 +182,7 @@ pub mod manifest;
>>   pub mod paperkey;
>>   pub mod prune;
>>   pub mod read_chunk;
>> +pub mod s3;
>>   pub mod store_progress;
>>   pub mod task_tracking;
>>   
>> diff --git a/pbs-datastore/src/s3.rs b/pbs-datastore/src/s3.rs
>> new file mode 100644
>> index 000000000..79e7548fb
>> --- /dev/null
>> +++ b/pbs-datastore/src/s3.rs
>> @@ -0,0 +1,114 @@
>> +use std::path::{Path, PathBuf};
>> +
>> +use anyhow::{bail, format_err, Error};
>> +
>> +use proxmox_s3_client::S3ObjectKey;
>> +
>> +/// Object key prefix to group regular datastore contents (not chunks)
>> +pub const S3_CONTENT_PREFIX: &str = ".cnt";
>> +
>> +/// Generate a relative object key with content prefix from given path and filename
>> +pub fn object_key_from_path(path: &Path, filename: &str) -> Result<S3ObjectKey, Error> {
>> +    // Force the use of relative paths, otherwise this would loose the content prefix
>> +    if path.is_absolute() {
>> +        bail!("cannot generate object key from absolute path");
>> +    }
>> +    if filename.contains('/') {
>> +        bail!("invalid filename containing slashes");
>> +    }
>> +    let mut object_path = PathBuf::from(S3_CONTENT_PREFIX);
>> +    object_path.push(path);
>> +    object_path.push(filename);
> 
> similarly to what I wrote on [18/46] with NS names maxed out this could
> end up being longer than the max len for a key... not sure how to best
> deal with that though. Limiting NS name len won't really work cause we
> just read them from the FS...

yes, but we cannot support object keys longer than this, and there is 
not really another viable option. So the user will be forced to limit 
namespace length anyways (also on sync sources for example).



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 36/46] api/datastore: implement refresh endpoint for stores with s3 backend
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 36/46] api/datastore: implement refresh endpoint for stores with s3 backend Christian Ebner
@ 2025-07-21 14:16   ` Hannes Laimer
  2025-07-21 14:26     ` Christian Ebner
  0 siblings, 1 reply; 74+ messages in thread
From: Hannes Laimer @ 2025-07-21 14:16 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion; +Cc: pbs-devel

On Sat Jul 19, 2025 at 2:50 PM CEST, Christian Ebner wrote:
> Allows to easily refresh the contents on the local cache store for
> datastores backed by an S3 object store.
>
> In order to guarantee that no read or write operations are ongoing,
> the store is first set into the maintenance mode `S3Refresh`. Objects
> are then fetched into a temporary directory to avoid loosing contents
> and consistency in case of an error. Once all objects have been
> fetched, clears out existing contents and moves the newly fetched
> contents in place.
>
> Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
> ---
> changes since version 8:
> - refactor s3 refresh into more compact methods
> - drop un-necessary drop(_lock)
> - use missing tokio::task::spawn_blocking context for blocking
>   maintenance mode setting
>
>  pbs-datastore/src/datastore.rs | 175 +++++++++++++++++++++++++++++++++
>  src/api2/admin/datastore.rs    |  34 +++++++
>  2 files changed, 209 insertions(+)
>
> diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
> index a524d7b32..b2af05eac 100644
> --- a/pbs-datastore/src/datastore.rs
> +++ b/pbs-datastore/src/datastore.rs
> @@ -10,6 +10,7 @@ use anyhow::{bail, format_err, Context, Error};
>  use http_body_util::BodyExt;
>  use nix::unistd::{unlinkat, UnlinkatFlags};
>  use pbs_tools::lru_cache::LruCache;
> +use tokio::io::AsyncWriteExt;
>  use tracing::{info, warn};
>  
>  use proxmox_human_byte::HumanByte;
> @@ -2200,4 +2201,178 @@ impl DataStore {
>      pub fn old_locking(&self) -> bool {
>          *OLD_LOCKING
>      }
> +
> +    /// Set the datastore's maintenance mode to `S3Refresh`, fetch from S3 object store, clear and
> +    /// replace the local cache store contents. Once finished disable the maintenance mode again.
> +    /// Returns with error for other datastore backends without setting the maintenance mode.
> +    pub async fn s3_refresh(self: &Arc<Self>) -> Result<(), Error> {
> +        match self.backend()? {
> +            DatastoreBackend::Filesystem => bail!("store '{}' not backed by S3", self.name()),
> +            DatastoreBackend::S3(s3_client) => {
> +                let self_clone = Arc::clone(self);
> +                tokio::task::spawn_blocking(move || {
> +                    self_clone.maintenance_mode(Some(MaintenanceMode {
> +                        ty: MaintenanceType::S3Refresh,
> +                        message: None,
> +                    }))
> +                })
> +                .await?
> +                .context("failed to set maintenance mode")?;

I think we should hold the config lock, so it can't be changed while we
refresh, no?

> +
> +                let tmp_base = proxmox_sys::fs::make_tmp_dir(&self.base_path(), None)
> +                    .context("failed to create temporary content folder in {store_base}")?;
> +
> +                self.fetch_tmp_contents(&tmp_base, &s3_client).await?;
> +                self.move_tmp_contents_in_place(&tmp_base).await?;
> +
> +                let self_clone = Arc::clone(self);
> +                tokio::task::spawn_blocking(move || self_clone.maintenance_mode(None))
> +                    .await?
> +                    .context("failed to clear maintenance mode")?;
> +            }
> +        }
> +        Ok(())
> +    }
> +
> +    // Set or clear the datastores maintenance mode by locking and updating the datastore config
> +    fn maintenance_mode(&self, maintenance_mode: Option<MaintenanceMode>) -> Result<(), Error> {
> +        let _lock = pbs_config::datastore::lock_config()?;
> +        let (mut section_config, _digest) = pbs_config::datastore::config()?;
> +        let mut datastore: DataStoreConfig = section_config.lookup("datastore", self.name())?;
> +        datastore.set_maintenance_mode(maintenance_mode)?;
> +        section_config.set_data(self.name(), "datastore", &datastore)?;
> +        pbs_config::datastore::save_config(&section_config)?;
> +        Ok(())
> +    }
> +
> +    // Fetch the contents (metadata, no chunks) of the datastore from the S3 object store to the
> +    // provided temporaray directory
> +    async fn fetch_tmp_contents(&self, tmp_base: &Path, s3_client: &S3Client) -> Result<(), Error> {
> +        let backup_user = pbs_config::backup_user().context("failed to get backup user")?;
> +        let mode = nix::sys::stat::Mode::from_bits_truncate(0o0644);
> +        let file_create_options = CreateOptions::new()
> +            .perm(mode)
> +            .owner(backup_user.uid)
> +            .group(backup_user.gid);
> +        let mode = nix::sys::stat::Mode::from_bits_truncate(0o0755);
> +        let dir_create_options = CreateOptions::new()
> +            .perm(mode)
> +            .owner(backup_user.uid)
> +            .group(backup_user.gid);
> +
> +        let list_prefix = S3PathPrefix::Some(S3_CONTENT_PREFIX.to_string());
> +        let store_prefix = format!("{}/{S3_CONTENT_PREFIX}/", self.name());
> +        let mut next_continuation_token: Option<String> = None;
> +        loop {
> +            let list_objects_result = s3_client
> +                .list_objects_v2(&list_prefix, next_continuation_token.as_deref())
> +                .await
> +                .context("failed to list object")?;
> +
> +            let objects_to_fetch: Vec<S3ObjectKey> = list_objects_result
> +                .contents
> +                .into_iter()
> +                .map(|item| item.key)
> +                .collect();
> +
> +            for object_key in objects_to_fetch {
> +                let object_path = format!("{object_key}");
> +                let object_path = object_path.strip_prefix(&store_prefix).with_context(|| {
> +                    format!("failed to strip store context prefix {store_prefix} for {object_key}")
> +                })?;
> +                if object_path.ends_with(NAMESPACE_MARKER_FILENAME) {
> +                    continue;
> +                }
> +
> +                info!("Fetching object {object_path}");
> +
> +                let file_path = tmp_base.join(object_path);
> +                if let Some(parent) = file_path.parent() {
> +                    proxmox_sys::fs::create_path(
> +                        parent,
> +                        Some(dir_create_options),
> +                        Some(dir_create_options),
> +                    )?;
> +                }
> +
> +                let mut target_file = tokio::fs::OpenOptions::new()
> +                    .write(true)
> +                    .create(true)
> +                    .truncate(true)
> +                    .read(true)
> +                    .open(&file_path)
> +                    .await
> +                    .with_context(|| format!("failed to create target file {file_path:?}"))?;
> +
> +                if let Some(response) = s3_client
> +                    .get_object(object_key)
> +                    .await
> +                    .with_context(|| format!("failed to fetch object {object_path}"))?
> +                {
> +                    let data = response
> +                        .content
> +                        .collect()
> +                        .await
> +                        .context("failed to collect object contents")?;
> +                    target_file
> +                        .write_all(&data.to_bytes())
> +                        .await
> +                        .context("failed to write to target file")?;
> +                    file_create_options
> +                        .apply_to(&mut target_file, &file_path)
> +                        .context("failed to set target file create options")?;
> +                    target_file
> +                        .flush()
> +                        .await
> +                        .context("failed to flush target file")?;
> +                } else {
> +                    bail!("failed to download {object_path}, not found");
> +                }
> +            }
> +
> +            if list_objects_result.is_truncated {
> +                next_continuation_token = list_objects_result
> +                    .next_continuation_token
> +                    .as_ref()
> +                    .cloned();
> +                continue;
> +            }
> +            break;
> +        }
> +        Ok(())
> +    }
> +
> +    // Fetch the contents (metadata, no chunks) of the datastore from the S3 object store to the
> +    // provided temporaray directory
> +    async fn move_tmp_contents_in_place(&self, tmp_base: &PathBuf) -> Result<(), Error> {
> +        for ty in ["vm", "ct", "host", "ns"] {
> +            let store_base_clone = self.base_path().clone();
> +            let tmp_base_clone = tmp_base.clone();
> +            tokio::task::spawn_blocking(move || {
> +                let type_dir = store_base_clone.join(ty);
> +                if let Err(err) = std::fs::remove_dir_all(&type_dir) {
> +                    if err.kind() != io::ErrorKind::NotFound {
> +                        return Err(err).with_context(|| {
> +                            format!("failed to remove old contents in {type_dir:?}")
> +                        });
> +                    }
> +                }
> +                let tmp_type_dir = tmp_base_clone.join(ty);
> +                if let Err(err) = std::fs::rename(&tmp_type_dir, &type_dir) {
> +                    if err.kind() != io::ErrorKind::NotFound {
> +                        return Err(err)
> +                            .with_context(|| format!("failed to rename {tmp_type_dir:?}"));
> +                    }
> +                }
> +                Ok::<(), Error>(())
> +            })
> +            .await?
> +            .with_context(|| format!("failed to refresh {:?}", self.base_path()))?;
> +        }
> +
> +        std::fs::remove_dir_all(&tmp_base)
> +            .with_context(|| format!("failed to cleanup temporary content in {tmp_base:?}"))?;
> +
> +        Ok(())
> +    }
>  }
> diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs
> index 87a8641bd..23b216bef 100644
> --- a/src/api2/admin/datastore.rs
> +++ b/src/api2/admin/datastore.rs
> @@ -2707,6 +2707,39 @@ pub async fn unmount(store: String, rpcenv: &mut dyn RpcEnvironment) -> Result<V
>      Ok(json!(upid))
>  }
>  
> +#[api(
> +    protected: true,
> +    input: {
> +        properties: {
> +            store: {
> +                schema: DATASTORE_SCHEMA,
> +            },
> +        }
> +    },
> +    returns: {
> +        schema: UPID_SCHEMA,
> +    },
> +    access: {
> +        permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_MODIFY, false),
> +    },
> +)]
> +/// Refresh datastore contents from S3 to local cache store.
> +pub async fn s3_refresh(store: String, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
> +    let datastore = DataStore::lookup_datastore(&store, Some(Operation::Lookup))?;
> +    let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
> +    let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
> +
> +    let upid = WorkerTask::spawn(
> +        "s3-refresh",
> +        Some(store),
> +        auth_id.to_string(),
> +        to_stdout,
> +        move |_worker| async move { datastore.s3_refresh().await },
> +    )?;
> +
> +    Ok(json!(upid))
> +}
> +
>  #[sortable]
>  const DATASTORE_INFO_SUBDIRS: SubdirMap = &[
>      (
> @@ -2773,6 +2806,7 @@ const DATASTORE_INFO_SUBDIRS: SubdirMap = &[
>          &Router::new().download(&API_METHOD_PXAR_FILE_DOWNLOAD),
>      ),
>      ("rrd", &Router::new().get(&API_METHOD_GET_RRD_STATS)),
> +    ("s3-refresh", &Router::new().put(&API_METHOD_S3_REFRESH)),
>      (
>          "snapshots",
>          &Router::new()



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 01/46] datastore: add helpers for path/digest to s3 object key conversion
  2025-07-21 14:15     ` Christian Ebner
@ 2025-07-21 14:20       ` Hannes Laimer
  0 siblings, 0 replies; 74+ messages in thread
From: Hannes Laimer @ 2025-07-21 14:20 UTC (permalink / raw)
  To: Christian Ebner, Proxmox Backup Server development discussion,
	Hannes Laimer
  Cc: pbs-devel

On Mon Jul 21, 2025 at 4:15 PM CEST, Christian Ebner wrote:
> On 7/21/25 3:58 PM, Hannes Laimer wrote:
>> On Sat Jul 19, 2025 at 2:49 PM CEST, Christian Ebner wrote:
>>> Adds helper methods to generate the s3 object keys given a relative
>>> path and filename for datastore contents or digest in case of chunk
>>> files.
>>>
>>> Regular datastore contents are stored by grouping them with a content
>>> prefix in the object key. In order to keep the object key length
>>> small, given the max limit of 1024 bytes {0], `.cnt` is used as
>>> content prefix. Chunks on the other hand are prefixed by `.chunks`,
>>> same as on regular datastores.
>>>
>>> The prefix allows for selective listing of either contents or chunks
>>> by providing the prefix to the respective api calls.
>>>
>>> [0] https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
>>>
>>> Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
>>> ---
>>> changes since version 8:
>>> - added unit tests for helper functions
>>>
>>>   Cargo.toml               |   1 +
>>>   pbs-datastore/Cargo.toml |   1 +
>>>   pbs-datastore/src/lib.rs |   1 +
>>>   pbs-datastore/src/s3.rs  | 114 +++++++++++++++++++++++++++++++++++++++
>>>   4 files changed, 117 insertions(+)
>>>   create mode 100644 pbs-datastore/src/s3.rs
>>>
>>> diff --git a/Cargo.toml b/Cargo.toml
>>> index adfa427d1..97783ddd5 100644
>>> --- a/Cargo.toml
>>> +++ b/Cargo.toml
>>> @@ -77,6 +77,7 @@ proxmox-rest-server = { version = "1", features = [ "templates" ] }
>>>   proxmox-router = { version = "3.2.2", default-features = false }
>>>   proxmox-rrd = "1"
>>>   proxmox-rrd-api-types = "1.0.2"
>>> +proxmox-s3-client = "1.0.0"
>>>   # everything but pbs-config and pbs-client use "api-macro"
>>>   proxmox-schema = "4"
>>>   proxmox-section-config = "3"
>>> diff --git a/pbs-datastore/Cargo.toml b/pbs-datastore/Cargo.toml
>>> index 56f6e9094..c42eff165 100644
>>> --- a/pbs-datastore/Cargo.toml
>>> +++ b/pbs-datastore/Cargo.toml
>>> @@ -34,6 +34,7 @@ proxmox-borrow.workspace = true
>>>   proxmox-human-byte.workspace = true
>>>   proxmox-io.workspace = true
>>>   proxmox-lang.workspace=true
>>> +proxmox-s3-client = { workspace = true, features = [ "impl" ] }
>>>   proxmox-schema = { workspace = true, features = [ "api-macro" ] }
>>>   proxmox-serde = { workspace = true, features = [ "serde_json" ] }
>>>   proxmox-sys.workspace = true
>>> diff --git a/pbs-datastore/src/lib.rs b/pbs-datastore/src/lib.rs
>>> index 5014b6c09..ffd0d91b2 100644
>>> --- a/pbs-datastore/src/lib.rs
>>> +++ b/pbs-datastore/src/lib.rs
>>> @@ -182,6 +182,7 @@ pub mod manifest;
>>>   pub mod paperkey;
>>>   pub mod prune;
>>>   pub mod read_chunk;
>>> +pub mod s3;
>>>   pub mod store_progress;
>>>   pub mod task_tracking;
>>>   
>>> diff --git a/pbs-datastore/src/s3.rs b/pbs-datastore/src/s3.rs
>>> new file mode 100644
>>> index 000000000..79e7548fb
>>> --- /dev/null
>>> +++ b/pbs-datastore/src/s3.rs
>>> @@ -0,0 +1,114 @@
>>> +use std::path::{Path, PathBuf};
>>> +
>>> +use anyhow::{bail, format_err, Error};
>>> +
>>> +use proxmox_s3_client::S3ObjectKey;
>>> +
>>> +/// Object key prefix to group regular datastore contents (not chunks)
>>> +pub const S3_CONTENT_PREFIX: &str = ".cnt";
>>> +
>>> +/// Generate a relative object key with content prefix from given path and filename
>>> +pub fn object_key_from_path(path: &Path, filename: &str) -> Result<S3ObjectKey, Error> {
>>> +    // Force the use of relative paths, otherwise this would loose the content prefix
>>> +    if path.is_absolute() {
>>> +        bail!("cannot generate object key from absolute path");
>>> +    }
>>> +    if filename.contains('/') {
>>> +        bail!("invalid filename containing slashes");
>>> +    }
>>> +    let mut object_path = PathBuf::from(S3_CONTENT_PREFIX);
>>> +    object_path.push(path);
>>> +    object_path.push(filename);
>> 
>> similarly to what I wrote on [18/46] with NS names maxed out this could
>> end up being longer than the max len for a key... not sure how to best
>> deal with that though. Limiting NS name len won't really work cause we
>> just read them from the FS...
>
> yes, but we cannot support object keys longer than this, and there is 
> not really another viable option. So the user will be forced to limit 
> namespace length anyways (also on sync sources for example).

yup
well, we could hash the path when making the key :S but I don't think we want that


_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (48 preceding siblings ...)
  2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 46/46] docs: Add section describing how to setup s3 backed datastore Christian Ebner
@ 2025-07-21 14:24 ` Hannes Laimer
  2025-07-21 15:05 ` Lukas Wagner
  2025-07-21 16:46 ` Christian Ebner
  51 siblings, 0 replies; 74+ messages in thread
From: Hannes Laimer @ 2025-07-21 14:24 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion; +Cc: pbs-devel

This is a really cool feature, and a very well put together series!
So LGTM, other than the missed `.unwrap()`, consider this

Reviewed-by: Hannes Laimer <h.laimer@proxmox.com>

On Sat Jul 19, 2025 at 2:49 PM CEST, Christian Ebner wrote:
> Disclaimer: These patches are still in an experimental state and not
> intended for production use.
>
> This patch series aims to add S3 compatible object stores as storage
> backend for PBS datastores. A PBS local cache store using the regular
> datastore layout is used for faster operation, bypassing requests to
> the S3 api when possible. Further, the local cache store allows to
> keep frequently used chunks and is used to avoid expensive metadata
> updates on the object store, e.g. by using local marker file during
> garbage collection.
>
> Backups are created by upload chunks to the corresponding S3 bucket,
> while keeping the index files in the local cache store, on backup
> finish, the snapshot metadata are persisted to the S3 storage backend.
>
> Snapshot restores read chunks preferably from the local cache store,
> downloading and insterting them if not present from the S3 object
> store. Listing and snapsoht metadata operation currently rely soly on
> the local cache store.
>
> Currently chunks use a 1:1 mapping to S3 objects. An advanced packing
> mechanism for chunks to significantly reduce the number of api
> requests and therefore be more cost effective will be implemented as
> followup patches.
>
> Most notably changes since version 8 of the patches (thanks @Lukas for
> code review):
> - Moved s3 refresh button to the datastore content, and made it less
>   visible by placing it into a 'More' dropdown.
> - Added basic unit tests for s3 object key helpers
> - Extend missing public function and type documentation
> - Use pbs_buildcfg::configdir macro to build config paths
> - Mostly refactoring based on feedback, please refer to the per-patch
>   changelog for details.
> - Fixes typos in docs (thanks @Maximiliano)
>
> Most notably changes since version 7 of the patches (thanks @Thomas
> and @Lukas for feedback, testing and debugging):
> - Improve self-signed certificate fingerprint check, verify valid
>   expected fingerprint is passed to client on instantiation.
> - Rename previously missed host to endpoint is s3 client selector
> - Use more specific `S3 Client ID` over ambiguous `Unique Identifier`
> - Implement missing cli commands for s3 client manipulation
> - Add in-use marker to s3 object stores to avoid accitental reuse of
>   object stores which are already used as datastore by another
>   instance, adding also flags to overwrite.
> - Automatically perform an s3-refresh when recreating a datastore,
>   pre-populating the contents without further user interaction.
> - Add documentation
> - Fix formatting issue in proxmox-s3-client
>
> Most notably changes since version 6 of the patches (thanks @Thomas
> for feedback):
> - Reworked uri encoding logic, instead of doing this in the
>   S3ObjectKey, perform this in the build_uri helper used by all
>   client api requests.
> - Add cache-size optional parameter to datastore backend config,
>   allows to define the local datastore LRU cache capacity.
> - Increase s3 client timeout, as otherwise delete objects operations
>   on Cloudflare R2 would run into a timeout error.
> - Also upload client log, previously not uploaded to s3 backend.
> - Add missing documentation to some pub types in the response reader
> - Use s3 object key generation helper for index file upload, which
>   fixes the missing key prefix.
> - Add basic regression tests for uri encoder and decoder helper
>   functions.
> - Include some baseline performance tests for garbage collection as
>   well as chunk up-/download when caching.
>
> Most notably changes since version 5 of the patches (thanks @Thomas
> for feedback):
> - Move s3 client into its own, dedicated crate in the proxmox repo
> - Factor out any directly PBS related code from the client
> - Guard implementation behind feature cfg, so api types can be used
>   independently
> - Add basic example and extend on crate documentation
>
> Most notably changes since version 4 of the patches:
> - Fix race between S3 backend upload and local cache store insert,
>   avoiding possibly chunk loss for concurrent backups.
> - Use the local datastore cache also for local chunk reader instances
> - Fallback to fetching chunks from S3 backend if they should be cached
>   but the local chunk file is missing or empty, instead of failing
> - Rename chunks detected as corrupt also on the S3 object store
> - Retry chunk uploads via put objects in case of errors.
> - Add possibility to add rate limits for the s3 client put requests, as
>   otherwise object stores can be overloaded.
> - Allow for Cloudflare R2 compatible `auto` region, as otherwise AWS
>   sign v4 request authentication will fail
> - Use `Async` instead of `Sync` variant for the api handler of the
>   s3-refresh command, as otherwise this fails.
> - Take into account that some type folders might not be present when
>   performing an s3-refresh.
> - Use `Local` instead of `Regular` to refer to normal datastores in the
>   creation window.
>
> Most notably changes since version 3 of the patches:
> - Rebased onto current master, fixed incompatibilities with upgraded
>   dependencies
> - Added method to uri decode s3 object keys, as they are required in
>   order to download contents to a local store
> - Added api endpoint to allow resyncing of the datastore contents to
>   the local cache store, introducing a new maintenance mode s3-refresh
>   to guarantee consistency.
>
> Most notably changes since RFC version 2 of the patches (thanks
> @Lukas for feedback):
> - Extend S3 client implementation to also support path style bucket
>   addressing.
> - Keep bucket name as config option for the datastore, allowing more
>   flexible reuse of a configured S3 client.
> - Use the datastore name as additional object key prefix to allow for
>   multiple datastores on the same bucket.
> - Allow bucket and region templating in S3 endpoint, making this more
>   flexible with respect to possible DNS records.
> - Rework datastore create window to be less overloaded.
> - Drop dead code in the S3 client implementation, since tagging and
>   object copying is currently not required.
> - Fix missing locking when deleting chunks from s3 store during
>   garbage collection, avoiding possible chunk loss for concurrent
>   backups.
> - Remove chunks from LRU cache when deleting chunks during garbage
>   collection, avoiding possible chunk loss for concurrent backups.
> - Add dedicated types for object prefix and relative s3 key paths to
>   avoid misuse.
> - Use more fitting icon for S3 client.
>
> Link to the bugtracker issue:
> https://bugzilla.proxmox.com/show_bug.cgi?id=2943
>
> Steps to setup a local S3 object store using RADOS gateway or MinIO
> can be found at (internal only, external users might use the steps
> outlined in the cover letter and comments of RFC version 2):
> https://wiki.intra.proxmox.com/PBS_Setup_S3_Object_Store
>
> proxmox:
>
> Christian Ebner (3):
>   pbs-api-types: extend datastore config by backend config enum
>   pbs-api-types: maintenance: add new maintenance mode S3 refresh
>   s3 client: wrap upload with retry into dedicated methods
>
>  Cargo.toml                       |   1 +
>  pbs-api-types/Cargo.toml         |   1 +
>  pbs-api-types/debian/control     |   2 +
>  pbs-api-types/src/datastore.rs   | 114 ++++++++++++++++++++++++++++++-
>  pbs-api-types/src/maintenance.rs |   4 ++
>  proxmox-s3-client/src/client.rs  |  32 ++++++++-
>  6 files changed, 151 insertions(+), 3 deletions(-)
>
>
> proxmox-backup:
>
> Christian Ebner (46):
>   datastore: add helpers for path/digest to s3 object key conversion
>   config: introduce s3 object store client configuration
>   api: config: implement endpoints to manipulate and list s3 configs
>   api: datastore: check s3 backend bucket access on datastore create
>   api/cli: add endpoint and command to check s3 client connection
>   datastore: allow to get the backend for a datastore
>   api: backup: store datastore backend in runtime environment
>   api: backup: conditionally upload chunks to s3 object store backend
>   api: backup: conditionally upload blobs to s3 object store backend
>   api: backup: conditionally upload indices to s3 object store backend
>   api: backup: conditionally upload manifest to s3 object store backend
>   api: datastore: conditionally upload client log to s3 backend
>   sync: pull: conditionally upload content to s3 backend
>   api: reader: fetch chunks based on datastore backend
>   datastore: local chunk reader: read chunks based on backend
>   verify worker: add datastore backed to verify worker
>   verify: implement chunk verification for stores with s3 backend
>   datastore: create namespace marker in s3 backend
>   datastore: create/delete protected marker file on s3 storage backend
>   datastore: prune groups/snapshots from s3 object store backend
>   datastore: get and set owner for s3 store backend
>   datastore: implement garbage collection for s3 backend
>   ui: add datastore type selector and reorganize component layout
>   ui: add s3 client edit window for configuration create/edit
>   ui: add s3 client view for configuration
>   ui: expose the s3 client view in the navigation tree
>   ui: add s3 client selector and bucket field for s3 backend setup
>   tools: lru cache: add removed callback for evicted cache nodes
>   tools: async lru cache: implement insert, remove and contains methods
>   datastore: add local datastore cache for network attached storages
>   api: backup: use local datastore cache on s3 backend chunk upload
>   api: reader: use local datastore cache on s3 backend chunk fetching
>   datastore: local chunk reader: get cached chunk from local cache store
>   backup writer: refactor parameters into backup writer options struct
>   api: backup: add no-cache flag to bypass local datastore cache
>   api/datastore: implement refresh endpoint for stores with s3 backend
>   cli: add dedicated subcommand for datastore s3 refresh
>   ui: render s3 refresh as valid maintenance type and task description
>   ui: expose s3 refresh button for datastores backed by object store
>   datastore: conditionally upload atime marker chunk to s3 backend
>   bin: implement client subcommands for s3 configuration manipulation
>   bin: expose reuse-datastore flag for proxmox-backup-manager
>   datastore: mark store as in-use by setting marker on s3 backend
>   datastore: run s3-refresh when reusing a datastore with s3 backend
>   api/ui: add flag to allow overwriting in-use marker for s3 backend
>   docs: Add section describing how to setup s3 backed datastore
>
>  Cargo.toml                                    |   2 +
>  docs/storage.rst                              |  68 ++
>  examples/upload-speed.rs                      |  17 +-
>  pbs-client/src/backup_writer.rs               |  47 +-
>  pbs-config/Cargo.toml                         |   1 +
>  pbs-config/src/lib.rs                         |   1 +
>  pbs-config/src/s3.rs                          |  89 +++
>  pbs-datastore/Cargo.toml                      |   5 +
>  pbs-datastore/src/backup_info.rs              |  63 +-
>  pbs-datastore/src/cached_chunk_reader.rs      |   6 +-
>  pbs-datastore/src/chunk_store.rs              |  29 +-
>  pbs-datastore/src/datastore.rs                | 688 ++++++++++++++++--
>  pbs-datastore/src/dynamic_index.rs            |   1 +
>  pbs-datastore/src/lib.rs                      |   5 +
>  pbs-datastore/src/local_chunk_reader.rs       |  61 +-
>  .../src/local_datastore_lru_cache.rs          | 180 +++++
>  pbs-datastore/src/s3.rs                       | 114 +++
>  pbs-tools/src/async_lru_cache.rs              |  46 +-
>  pbs-tools/src/lru_cache.rs                    |  42 +-
>  proxmox-backup-client/src/benchmark.rs        |  17 +-
>  proxmox-backup-client/src/main.rs             |  26 +-
>  src/api2/admin/datastore.rs                   |  85 ++-
>  src/api2/admin/mod.rs                         |   2 +
>  src/api2/admin/s3.rs                          |  82 +++
>  src/api2/backup/environment.rs                |  82 ++-
>  src/api2/backup/mod.rs                        | 131 ++--
>  src/api2/backup/upload_chunk.rs               | 114 ++-
>  src/api2/config/datastore.rs                  | 147 +++-
>  src/api2/config/mod.rs                        |   2 +
>  src/api2/config/s3.rs                         | 310 ++++++++
>  src/api2/node/disks/directory.rs              |   2 +-
>  src/api2/node/disks/zfs.rs                    |   2 +-
>  src/api2/reader/environment.rs                |  12 +-
>  src/api2/reader/mod.rs                        |  62 +-
>  src/backup/verify.rs                          | 134 +++-
>  src/bin/proxmox-backup-manager.rs             |   1 +
>  src/bin/proxmox_backup_manager/datastore.rs   |  42 ++
>  src/bin/proxmox_backup_manager/mod.rs         |   2 +
>  src/bin/proxmox_backup_manager/s3.rs          | 102 +++
>  src/server/pull.rs                            |  68 +-
>  src/server/push.rs                            |  19 +-
>  src/server/verify_job.rs                      |   2 +-
>  www/Makefile                                  |   3 +
>  www/NavigationTree.js                         |   6 +
>  www/Utils.js                                  |   4 +
>  www/config/S3ClientView.js                    | 141 ++++
>  www/datastore/Content.js                      |  48 ++
>  www/form/S3ClientSelector.js                  |  33 +
>  www/window/DataStoreEdit.js                   | 132 +++-
>  www/window/MaintenanceOptions.js              |   6 +-
>  www/window/S3ClientEdit.js                    | 148 ++++
>  51 files changed, 3104 insertions(+), 328 deletions(-)
>  create mode 100644 pbs-config/src/s3.rs
>  create mode 100644 pbs-datastore/src/local_datastore_lru_cache.rs
>  create mode 100644 pbs-datastore/src/s3.rs
>  create mode 100644 src/api2/admin/s3.rs
>  create mode 100644 src/api2/config/s3.rs
>  create mode 100644 src/bin/proxmox_backup_manager/s3.rs
>  create mode 100644 www/config/S3ClientView.js
>  create mode 100644 www/form/S3ClientSelector.js
>  create mode 100644 www/window/S3ClientEdit.js
>
>
> Summary over all repositories:
>   57 files changed, 3255 insertions(+), 331 deletions(-)



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 36/46] api/datastore: implement refresh endpoint for stores with s3 backend
  2025-07-21 14:16   ` Hannes Laimer
@ 2025-07-21 14:26     ` Christian Ebner
  2025-07-21 14:31       ` Hannes Laimer
  0 siblings, 1 reply; 74+ messages in thread
From: Christian Ebner @ 2025-07-21 14:26 UTC (permalink / raw)
  To: pbs-devel

On 7/21/25 4:16 PM, Hannes Laimer wrote:
> On Sat Jul 19, 2025 at 2:50 PM CEST, Christian Ebner wrote:
>> Allows to easily refresh the contents on the local cache store for
>> datastores backed by an S3 object store.
>>
>> In order to guarantee that no read or write operations are ongoing,
>> the store is first set into the maintenance mode `S3Refresh`. Objects
>> are then fetched into a temporary directory to avoid loosing contents
>> and consistency in case of an error. Once all objects have been
>> fetched, clears out existing contents and moves the newly fetched
>> contents in place.
>>
>> Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
>> ---
>> changes since version 8:
>> - refactor s3 refresh into more compact methods
>> - drop un-necessary drop(_lock)
>> - use missing tokio::task::spawn_blocking context for blocking
>>    maintenance mode setting
>>
>>   pbs-datastore/src/datastore.rs | 175 +++++++++++++++++++++++++++++++++
>>   src/api2/admin/datastore.rs    |  34 +++++++
>>   2 files changed, 209 insertions(+)
>>
>> diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
>> index a524d7b32..b2af05eac 100644
>> --- a/pbs-datastore/src/datastore.rs
>> +++ b/pbs-datastore/src/datastore.rs
>> @@ -10,6 +10,7 @@ use anyhow::{bail, format_err, Context, Error};
>>   use http_body_util::BodyExt;
>>   use nix::unistd::{unlinkat, UnlinkatFlags};
>>   use pbs_tools::lru_cache::LruCache;
>> +use tokio::io::AsyncWriteExt;
>>   use tracing::{info, warn};
>>   
>>   use proxmox_human_byte::HumanByte;
>> @@ -2200,4 +2201,178 @@ impl DataStore {
>>       pub fn old_locking(&self) -> bool {
>>           *OLD_LOCKING
>>       }
>> +
>> +    /// Set the datastore's maintenance mode to `S3Refresh`, fetch from S3 object store, clear and
>> +    /// replace the local cache store contents. Once finished disable the maintenance mode again.
>> +    /// Returns with error for other datastore backends without setting the maintenance mode.
>> +    pub async fn s3_refresh(self: &Arc<Self>) -> Result<(), Error> {
>> +        match self.backend()? {
>> +            DatastoreBackend::Filesystem => bail!("store '{}' not backed by S3", self.name()),
>> +            DatastoreBackend::S3(s3_client) => {
>> +                let self_clone = Arc::clone(self);
>> +                tokio::task::spawn_blocking(move || {
>> +                    self_clone.maintenance_mode(Some(MaintenanceMode {
>> +                        ty: MaintenanceType::S3Refresh,
>> +                        message: None,
>> +                    }))
>> +                })
>> +                .await?
>> +                .context("failed to set maintenance mode")?;
> 
> I think we should hold the config lock, so it can't be changed while we
> refresh, no?

Yes, but that is handled by the method itself, also to limit lock scope.

See further below...

> 
>> +
>> +                let tmp_base = proxmox_sys::fs::make_tmp_dir(&self.base_path(), None)
>> +                    .context("failed to create temporary content folder in {store_base}")?;
>> +
>> +                self.fetch_tmp_contents(&tmp_base, &s3_client).await?;
>> +                self.move_tmp_contents_in_place(&tmp_base).await?;
>> +
>> +                let self_clone = Arc::clone(self);
>> +                tokio::task::spawn_blocking(move || self_clone.maintenance_mode(None))
>> +                    .await?
>> +                    .context("failed to clear maintenance mode")?;
>> +            }
>> +        }
>> +        Ok(())
>> +    }
>> +
>> +    // Set or clear the datastores maintenance mode by locking and updating the datastore config
>> +    fn maintenance_mode(&self, maintenance_mode: Option<MaintenanceMode>) -> Result<(), Error> {
>> +        let _lock = pbs_config::datastore::lock_config()?;

... here the config is locked and the scope limited by the method.

>> +        let (mut section_config, _digest) = pbs_config::datastore::config()?;
>> +        let mut datastore: DataStoreConfig = section_config.lookup("datastore", self.name())?;
>> +        datastore.set_maintenance_mode(maintenance_mode)?;
>> +        section_config.set_data(self.name(), "datastore", &datastore)?;
>> +        pbs_config::datastore::save_config(&section_config)?;
>> +        Ok(())
>> +    }
>> +
>> +    // Fetch the contents (metadata, no chunks) of the datastore from the S3 object store to the
>> +    // provided temporaray directory
>> +    async fn fetch_tmp_contents(&self, tmp_base: &Path, s3_client: &S3Client) -> Result<(), Error> {
>> +        let backup_user = pbs_config::backup_user().context("failed to get backup user")?;
>> +        let mode = nix::sys::stat::Mode::from_bits_truncate(0o0644);
>> +        let file_create_options = CreateOptions::new()
>> +            .perm(mode)
>> +            .owner(backup_user.uid)
>> +            .group(backup_user.gid);
>> +        let mode = nix::sys::stat::Mode::from_bits_truncate(0o0755);
>> +        let dir_create_options = CreateOptions::new()
>> +            .perm(mode)
>> +            .owner(backup_user.uid)
>> +            .group(backup_user.gid);
>> +
>> +        let list_prefix = S3PathPrefix::Some(S3_CONTENT_PREFIX.to_string());
>> +        let store_prefix = format!("{}/{S3_CONTENT_PREFIX}/", self.name());
>> +        let mut next_continuation_token: Option<String> = None;
>> +        loop {
>> +            let list_objects_result = s3_client
>> +                .list_objects_v2(&list_prefix, next_continuation_token.as_deref())
>> +                .await
>> +                .context("failed to list object")?;
>> +
>> +            let objects_to_fetch: Vec<S3ObjectKey> = list_objects_result
>> +                .contents
>> +                .into_iter()
>> +                .map(|item| item.key)
>> +                .collect();
>> +
>> +            for object_key in objects_to_fetch {
>> +                let object_path = format!("{object_key}");
>> +                let object_path = object_path.strip_prefix(&store_prefix).with_context(|| {
>> +                    format!("failed to strip store context prefix {store_prefix} for {object_key}")
>> +                })?;
>> +                if object_path.ends_with(NAMESPACE_MARKER_FILENAME) {
>> +                    continue;
>> +                }
>> +
>> +                info!("Fetching object {object_path}");
>> +
>> +                let file_path = tmp_base.join(object_path);
>> +                if let Some(parent) = file_path.parent() {
>> +                    proxmox_sys::fs::create_path(
>> +                        parent,
>> +                        Some(dir_create_options),
>> +                        Some(dir_create_options),
>> +                    )?;
>> +                }
>> +
>> +                let mut target_file = tokio::fs::OpenOptions::new()
>> +                    .write(true)
>> +                    .create(true)
>> +                    .truncate(true)
>> +                    .read(true)
>> +                    .open(&file_path)
>> +                    .await
>> +                    .with_context(|| format!("failed to create target file {file_path:?}"))?;
>> +
>> +                if let Some(response) = s3_client
>> +                    .get_object(object_key)
>> +                    .await
>> +                    .with_context(|| format!("failed to fetch object {object_path}"))?
>> +                {
>> +                    let data = response
>> +                        .content
>> +                        .collect()
>> +                        .await
>> +                        .context("failed to collect object contents")?;
>> +                    target_file
>> +                        .write_all(&data.to_bytes())
>> +                        .await
>> +                        .context("failed to write to target file")?;
>> +                    file_create_options
>> +                        .apply_to(&mut target_file, &file_path)
>> +                        .context("failed to set target file create options")?;
>> +                    target_file
>> +                        .flush()
>> +                        .await
>> +                        .context("failed to flush target file")?;
>> +                } else {
>> +                    bail!("failed to download {object_path}, not found");
>> +                }
>> +            }
>> +
>> +            if list_objects_result.is_truncated {
>> +                next_continuation_token = list_objects_result
>> +                    .next_continuation_token
>> +                    .as_ref()
>> +                    .cloned();
>> +                continue;
>> +            }
>> +            break;
>> +        }
>> +        Ok(())
>> +    }
>> +
>> +    // Fetch the contents (metadata, no chunks) of the datastore from the S3 object store to the
>> +    // provided temporaray directory
>> +    async fn move_tmp_contents_in_place(&self, tmp_base: &PathBuf) -> Result<(), Error> {
>> +        for ty in ["vm", "ct", "host", "ns"] {
>> +            let store_base_clone = self.base_path().clone();
>> +            let tmp_base_clone = tmp_base.clone();
>> +            tokio::task::spawn_blocking(move || {
>> +                let type_dir = store_base_clone.join(ty);
>> +                if let Err(err) = std::fs::remove_dir_all(&type_dir) {
>> +                    if err.kind() != io::ErrorKind::NotFound {
>> +                        return Err(err).with_context(|| {
>> +                            format!("failed to remove old contents in {type_dir:?}")
>> +                        });
>> +                    }
>> +                }
>> +                let tmp_type_dir = tmp_base_clone.join(ty);
>> +                if let Err(err) = std::fs::rename(&tmp_type_dir, &type_dir) {
>> +                    if err.kind() != io::ErrorKind::NotFound {
>> +                        return Err(err)
>> +                            .with_context(|| format!("failed to rename {tmp_type_dir:?}"));
>> +                    }
>> +                }
>> +                Ok::<(), Error>(())
>> +            })
>> +            .await?
>> +            .with_context(|| format!("failed to refresh {:?}", self.base_path()))?;
>> +        }
>> +
>> +        std::fs::remove_dir_all(&tmp_base)
>> +            .with_context(|| format!("failed to cleanup temporary content in {tmp_base:?}"))?;
>> +
>> +        Ok(())
>> +    }
>>   }
>> diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs
>> index 87a8641bd..23b216bef 100644
>> --- a/src/api2/admin/datastore.rs
>> +++ b/src/api2/admin/datastore.rs
>> @@ -2707,6 +2707,39 @@ pub async fn unmount(store: String, rpcenv: &mut dyn RpcEnvironment) -> Result<V
>>       Ok(json!(upid))
>>   }
>>   
>> +#[api(
>> +    protected: true,
>> +    input: {
>> +        properties: {
>> +            store: {
>> +                schema: DATASTORE_SCHEMA,
>> +            },
>> +        }
>> +    },
>> +    returns: {
>> +        schema: UPID_SCHEMA,
>> +    },
>> +    access: {
>> +        permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_MODIFY, false),
>> +    },
>> +)]
>> +/// Refresh datastore contents from S3 to local cache store.
>> +pub async fn s3_refresh(store: String, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
>> +    let datastore = DataStore::lookup_datastore(&store, Some(Operation::Lookup))?;
>> +    let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
>> +    let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
>> +
>> +    let upid = WorkerTask::spawn(
>> +        "s3-refresh",
>> +        Some(store),
>> +        auth_id.to_string(),
>> +        to_stdout,
>> +        move |_worker| async move { datastore.s3_refresh().await },
>> +    )?;
>> +
>> +    Ok(json!(upid))
>> +}
>> +
>>   #[sortable]
>>   const DATASTORE_INFO_SUBDIRS: SubdirMap = &[
>>       (
>> @@ -2773,6 +2806,7 @@ const DATASTORE_INFO_SUBDIRS: SubdirMap = &[
>>           &Router::new().download(&API_METHOD_PXAR_FILE_DOWNLOAD),
>>       ),
>>       ("rrd", &Router::new().get(&API_METHOD_GET_RRD_STATS)),
>> +    ("s3-refresh", &Router::new().put(&API_METHOD_S3_REFRESH)),
>>       (
>>           "snapshots",
>>           &Router::new()
> 
> 
> 
> _______________________________________________
> pbs-devel mailing list
> pbs-devel@lists.proxmox.com
> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
> 
> 



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 36/46] api/datastore: implement refresh endpoint for stores with s3 backend
  2025-07-21 14:26     ` Christian Ebner
@ 2025-07-21 14:31       ` Hannes Laimer
  2025-07-21 14:42         ` Christian Ebner
  0 siblings, 1 reply; 74+ messages in thread
From: Hannes Laimer @ 2025-07-21 14:31 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion; +Cc: pbs-devel

On Mon Jul 21, 2025 at 4:26 PM CEST, Christian Ebner wrote:
> On 7/21/25 4:16 PM, Hannes Laimer wrote:
>> On Sat Jul 19, 2025 at 2:50 PM CEST, Christian Ebner wrote:
>>> Allows to easily refresh the contents on the local cache store for
>>> datastores backed by an S3 object store.
>>>
>>> In order to guarantee that no read or write operations are ongoing,
>>> the store is first set into the maintenance mode `S3Refresh`. Objects
>>> are then fetched into a temporary directory to avoid loosing contents
>>> and consistency in case of an error. Once all objects have been
>>> fetched, clears out existing contents and moves the newly fetched
>>> contents in place.
>>>
>>> Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
>>> ---
>>> changes since version 8:
>>> - refactor s3 refresh into more compact methods
>>> - drop un-necessary drop(_lock)
>>> - use missing tokio::task::spawn_blocking context for blocking
>>>    maintenance mode setting
>>>
>>>   pbs-datastore/src/datastore.rs | 175 +++++++++++++++++++++++++++++++++
>>>   src/api2/admin/datastore.rs    |  34 +++++++
>>>   2 files changed, 209 insertions(+)
>>>
>>> diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
>>> index a524d7b32..b2af05eac 100644
>>> --- a/pbs-datastore/src/datastore.rs
>>> +++ b/pbs-datastore/src/datastore.rs
>>> @@ -10,6 +10,7 @@ use anyhow::{bail, format_err, Context, Error};
>>>   use http_body_util::BodyExt;
>>>   use nix::unistd::{unlinkat, UnlinkatFlags};
>>>   use pbs_tools::lru_cache::LruCache;
>>> +use tokio::io::AsyncWriteExt;
>>>   use tracing::{info, warn};
>>>   
>>>   use proxmox_human_byte::HumanByte;
>>> @@ -2200,4 +2201,178 @@ impl DataStore {
>>>       pub fn old_locking(&self) -> bool {
>>>           *OLD_LOCKING
>>>       }
>>> +
>>> +    /// Set the datastore's maintenance mode to `S3Refresh`, fetch from S3 object store, clear and
>>> +    /// replace the local cache store contents. Once finished disable the maintenance mode again.
>>> +    /// Returns with error for other datastore backends without setting the maintenance mode.
>>> +    pub async fn s3_refresh(self: &Arc<Self>) -> Result<(), Error> {
>>> +        match self.backend()? {
>>> +            DatastoreBackend::Filesystem => bail!("store '{}' not backed by S3", self.name()),
>>> +            DatastoreBackend::S3(s3_client) => {
>>> +                let self_clone = Arc::clone(self);
>>> +                tokio::task::spawn_blocking(move || {
>>> +                    self_clone.maintenance_mode(Some(MaintenanceMode {
>>> +                        ty: MaintenanceType::S3Refresh,
>>> +                        message: None,
>>> +                    }))
>>> +                })
>>> +                .await?
>>> +                .context("failed to set maintenance mode")?;
>> 
>> I think we should hold the config lock, so it can't be changed while we
>> refresh, no?
>
> Yes, but that is handled by the method itself, also to limit lock scope.
>
> See further below...
>

maybe I'm missing something, but the limited scope is what I mean. I
think we should try to prevent changing the maintenance mode away from
`S3Refresh` before we're done, so basically holding the lock while we
refresh.

>> 
>>> +
>>> +                let tmp_base = proxmox_sys::fs::make_tmp_dir(&self.base_path(), None)
>>> +                    .context("failed to create temporary content folder in {store_base}")?;
>>> +
>>> +                self.fetch_tmp_contents(&tmp_base, &s3_client).await?;
>>> +                self.move_tmp_contents_in_place(&tmp_base).await?;
>>> +
>>> +                let self_clone = Arc::clone(self);
>>> +                tokio::task::spawn_blocking(move || self_clone.maintenance_mode(None))
>>> +                    .await?
>>> +                    .context("failed to clear maintenance mode")?;
>>> +            }
>>> +        }
>>> +        Ok(())
>>> +    }
>>> +
>>> +    // Set or clear the datastores maintenance mode by locking and updating the datastore config
>>> +    fn maintenance_mode(&self, maintenance_mode: Option<MaintenanceMode>) -> Result<(), Error> {
>>> +        let _lock = pbs_config::datastore::lock_config()?;
>
> ... here the config is locked and the scope limited by the method.
>
>>> +        let (mut section_config, _digest) = pbs_config::datastore::config()?;
>>> +        let mut datastore: DataStoreConfig = section_config.lookup("datastore", self.name())?;
>>> +        datastore.set_maintenance_mode(maintenance_mode)?;
>>> +        section_config.set_data(self.name(), "datastore", &datastore)?;
>>> +        pbs_config::datastore::save_config(&section_config)?;
>>> +        Ok(())
>>> +    }
>>> +
>>> +    // Fetch the contents (metadata, no chunks) of the datastore from the S3 object store to the
>>> +    // provided temporaray directory
>>> +    async fn fetch_tmp_contents(&self, tmp_base: &Path, s3_client: &S3Client) -> Result<(), Error> {
>>> +        let backup_user = pbs_config::backup_user().context("failed to get backup user")?;
>>> +        let mode = nix::sys::stat::Mode::from_bits_truncate(0o0644);
>>> +        let file_create_options = CreateOptions::new()
>>> +            .perm(mode)
>>> +            .owner(backup_user.uid)
>>> +            .group(backup_user.gid);
>>> +        let mode = nix::sys::stat::Mode::from_bits_truncate(0o0755);
>>> +        let dir_create_options = CreateOptions::new()
>>> +            .perm(mode)
>>> +            .owner(backup_user.uid)
>>> +            .group(backup_user.gid);
>>> +
>>> +        let list_prefix = S3PathPrefix::Some(S3_CONTENT_PREFIX.to_string());
>>> +        let store_prefix = format!("{}/{S3_CONTENT_PREFIX}/", self.name());
>>> +        let mut next_continuation_token: Option<String> = None;
>>> +        loop {
>>> +            let list_objects_result = s3_client
>>> +                .list_objects_v2(&list_prefix, next_continuation_token.as_deref())
>>> +                .await
>>> +                .context("failed to list object")?;
>>> +
>>> +            let objects_to_fetch: Vec<S3ObjectKey> = list_objects_result
>>> +                .contents
>>> +                .into_iter()
>>> +                .map(|item| item.key)
>>> +                .collect();
>>> +
>>> +            for object_key in objects_to_fetch {
>>> +                let object_path = format!("{object_key}");
>>> +                let object_path = object_path.strip_prefix(&store_prefix).with_context(|| {
>>> +                    format!("failed to strip store context prefix {store_prefix} for {object_key}")
>>> +                })?;
>>> +                if object_path.ends_with(NAMESPACE_MARKER_FILENAME) {
>>> +                    continue;
>>> +                }
>>> +
>>> +                info!("Fetching object {object_path}");
>>> +
>>> +                let file_path = tmp_base.join(object_path);
>>> +                if let Some(parent) = file_path.parent() {
>>> +                    proxmox_sys::fs::create_path(
>>> +                        parent,
>>> +                        Some(dir_create_options),
>>> +                        Some(dir_create_options),
>>> +                    )?;
>>> +                }
>>> +
>>> +                let mut target_file = tokio::fs::OpenOptions::new()
>>> +                    .write(true)
>>> +                    .create(true)
>>> +                    .truncate(true)
>>> +                    .read(true)
>>> +                    .open(&file_path)
>>> +                    .await
>>> +                    .with_context(|| format!("failed to create target file {file_path:?}"))?;
>>> +
>>> +                if let Some(response) = s3_client
>>> +                    .get_object(object_key)
>>> +                    .await
>>> +                    .with_context(|| format!("failed to fetch object {object_path}"))?
>>> +                {
>>> +                    let data = response
>>> +                        .content
>>> +                        .collect()
>>> +                        .await
>>> +                        .context("failed to collect object contents")?;
>>> +                    target_file
>>> +                        .write_all(&data.to_bytes())
>>> +                        .await
>>> +                        .context("failed to write to target file")?;
>>> +                    file_create_options
>>> +                        .apply_to(&mut target_file, &file_path)
>>> +                        .context("failed to set target file create options")?;
>>> +                    target_file
>>> +                        .flush()
>>> +                        .await
>>> +                        .context("failed to flush target file")?;
>>> +                } else {
>>> +                    bail!("failed to download {object_path}, not found");
>>> +                }
>>> +            }
>>> +
>>> +            if list_objects_result.is_truncated {
>>> +                next_continuation_token = list_objects_result
>>> +                    .next_continuation_token
>>> +                    .as_ref()
>>> +                    .cloned();
>>> +                continue;
>>> +            }
>>> +            break;
>>> +        }
>>> +        Ok(())
>>> +    }
>>> +
>>> +    // Fetch the contents (metadata, no chunks) of the datastore from the S3 object store to the
>>> +    // provided temporaray directory
>>> +    async fn move_tmp_contents_in_place(&self, tmp_base: &PathBuf) -> Result<(), Error> {
>>> +        for ty in ["vm", "ct", "host", "ns"] {
>>> +            let store_base_clone = self.base_path().clone();
>>> +            let tmp_base_clone = tmp_base.clone();
>>> +            tokio::task::spawn_blocking(move || {
>>> +                let type_dir = store_base_clone.join(ty);
>>> +                if let Err(err) = std::fs::remove_dir_all(&type_dir) {
>>> +                    if err.kind() != io::ErrorKind::NotFound {
>>> +                        return Err(err).with_context(|| {
>>> +                            format!("failed to remove old contents in {type_dir:?}")
>>> +                        });
>>> +                    }
>>> +                }
>>> +                let tmp_type_dir = tmp_base_clone.join(ty);
>>> +                if let Err(err) = std::fs::rename(&tmp_type_dir, &type_dir) {
>>> +                    if err.kind() != io::ErrorKind::NotFound {
>>> +                        return Err(err)
>>> +                            .with_context(|| format!("failed to rename {tmp_type_dir:?}"));
>>> +                    }
>>> +                }
>>> +                Ok::<(), Error>(())
>>> +            })
>>> +            .await?
>>> +            .with_context(|| format!("failed to refresh {:?}", self.base_path()))?;
>>> +        }
>>> +
>>> +        std::fs::remove_dir_all(&tmp_base)
>>> +            .with_context(|| format!("failed to cleanup temporary content in {tmp_base:?}"))?;
>>> +
>>> +        Ok(())
>>> +    }
>>>   }
>>> diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs
>>> index 87a8641bd..23b216bef 100644
>>> --- a/src/api2/admin/datastore.rs
>>> +++ b/src/api2/admin/datastore.rs
>>> @@ -2707,6 +2707,39 @@ pub async fn unmount(store: String, rpcenv: &mut dyn RpcEnvironment) -> Result<V
>>>       Ok(json!(upid))
>>>   }
>>>   
>>> +#[api(
>>> +    protected: true,
>>> +    input: {
>>> +        properties: {
>>> +            store: {
>>> +                schema: DATASTORE_SCHEMA,
>>> +            },
>>> +        }
>>> +    },
>>> +    returns: {
>>> +        schema: UPID_SCHEMA,
>>> +    },
>>> +    access: {
>>> +        permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_MODIFY, false),
>>> +    },
>>> +)]
>>> +/// Refresh datastore contents from S3 to local cache store.
>>> +pub async fn s3_refresh(store: String, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
>>> +    let datastore = DataStore::lookup_datastore(&store, Some(Operation::Lookup))?;
>>> +    let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
>>> +    let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
>>> +
>>> +    let upid = WorkerTask::spawn(
>>> +        "s3-refresh",
>>> +        Some(store),
>>> +        auth_id.to_string(),
>>> +        to_stdout,
>>> +        move |_worker| async move { datastore.s3_refresh().await },
>>> +    )?;
>>> +
>>> +    Ok(json!(upid))
>>> +}
>>> +
>>>   #[sortable]
>>>   const DATASTORE_INFO_SUBDIRS: SubdirMap = &[
>>>       (
>>> @@ -2773,6 +2806,7 @@ const DATASTORE_INFO_SUBDIRS: SubdirMap = &[
>>>           &Router::new().download(&API_METHOD_PXAR_FILE_DOWNLOAD),
>>>       ),
>>>       ("rrd", &Router::new().get(&API_METHOD_GET_RRD_STATS)),
>>> +    ("s3-refresh", &Router::new().put(&API_METHOD_S3_REFRESH)),
>>>       (
>>>           "snapshots",
>>>           &Router::new()
>> 
>> 
>> 
>> _______________________________________________
>> pbs-devel mailing list
>> pbs-devel@lists.proxmox.com
>> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
>> 
>> 
>
>
>
> _______________________________________________
> pbs-devel mailing list
> pbs-devel@lists.proxmox.com
> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 36/46] api/datastore: implement refresh endpoint for stores with s3 backend
  2025-07-21 14:31       ` Hannes Laimer
@ 2025-07-21 14:42         ` Christian Ebner
  2025-07-21 14:48           ` Hannes Laimer
  0 siblings, 1 reply; 74+ messages in thread
From: Christian Ebner @ 2025-07-21 14:42 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Hannes Laimer; +Cc: pbs-devel

On 7/21/25 4:31 PM, Hannes Laimer wrote:
> On Mon Jul 21, 2025 at 4:26 PM CEST, Christian Ebner wrote:
>> On 7/21/25 4:16 PM, Hannes Laimer wrote:
>>> On Sat Jul 19, 2025 at 2:50 PM CEST, Christian Ebner wrote:
>>>> Allows to easily refresh the contents on the local cache store for
>>>> datastores backed by an S3 object store.
>>>>
>>>> In order to guarantee that no read or write operations are ongoing,
>>>> the store is first set into the maintenance mode `S3Refresh`. Objects
>>>> are then fetched into a temporary directory to avoid loosing contents
>>>> and consistency in case of an error. Once all objects have been
>>>> fetched, clears out existing contents and moves the newly fetched
>>>> contents in place.
>>>>
>>>> Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
>>>> ---
>>>> changes since version 8:
>>>> - refactor s3 refresh into more compact methods
>>>> - drop un-necessary drop(_lock)
>>>> - use missing tokio::task::spawn_blocking context for blocking
>>>>     maintenance mode setting
>>>>
>>>>    pbs-datastore/src/datastore.rs | 175 +++++++++++++++++++++++++++++++++
>>>>    src/api2/admin/datastore.rs    |  34 +++++++
>>>>    2 files changed, 209 insertions(+)
>>>>
>>>> diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
>>>> index a524d7b32..b2af05eac 100644
>>>> --- a/pbs-datastore/src/datastore.rs
>>>> +++ b/pbs-datastore/src/datastore.rs
>>>> @@ -10,6 +10,7 @@ use anyhow::{bail, format_err, Context, Error};
>>>>    use http_body_util::BodyExt;
>>>>    use nix::unistd::{unlinkat, UnlinkatFlags};
>>>>    use pbs_tools::lru_cache::LruCache;
>>>> +use tokio::io::AsyncWriteExt;
>>>>    use tracing::{info, warn};
>>>>    
>>>>    use proxmox_human_byte::HumanByte;
>>>> @@ -2200,4 +2201,178 @@ impl DataStore {
>>>>        pub fn old_locking(&self) -> bool {
>>>>            *OLD_LOCKING
>>>>        }
>>>> +
>>>> +    /// Set the datastore's maintenance mode to `S3Refresh`, fetch from S3 object store, clear and
>>>> +    /// replace the local cache store contents. Once finished disable the maintenance mode again.
>>>> +    /// Returns with error for other datastore backends without setting the maintenance mode.
>>>> +    pub async fn s3_refresh(self: &Arc<Self>) -> Result<(), Error> {
>>>> +        match self.backend()? {
>>>> +            DatastoreBackend::Filesystem => bail!("store '{}' not backed by S3", self.name()),
>>>> +            DatastoreBackend::S3(s3_client) => {
>>>> +                let self_clone = Arc::clone(self);
>>>> +                tokio::task::spawn_blocking(move || {
>>>> +                    self_clone.maintenance_mode(Some(MaintenanceMode {
>>>> +                        ty: MaintenanceType::S3Refresh,
>>>> +                        message: None,
>>>> +                    }))
>>>> +                })
>>>> +                .await?
>>>> +                .context("failed to set maintenance mode")?;
>>>
>>> I think we should hold the config lock, so it can't be changed while we
>>> refresh, no?
>>
>> Yes, but that is handled by the method itself, also to limit lock scope.
>>
>> See further below...
>>
> 
> maybe I'm missing something, but the limited scope is what I mean. I
> think we should try to prevent changing the maintenance mode away from
> `S3Refresh` before we're done, so basically holding the lock while we
> refresh.

No, the intention here is to allow the user to manually clear the 
maintenance mode again, as the s3 refresh might fail. So the refresh 
manually sets it to guarantee consistency and clears it if finished.

If the lock on the config is kept for the whole refresh, than the user 
will never be able to recover from an error.


_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox-backup v9 36/46] api/datastore: implement refresh endpoint for stores with s3 backend
  2025-07-21 14:42         ` Christian Ebner
@ 2025-07-21 14:48           ` Hannes Laimer
  0 siblings, 0 replies; 74+ messages in thread
From: Hannes Laimer @ 2025-07-21 14:48 UTC (permalink / raw)
  To: Christian Ebner, Proxmox Backup Server development discussion,
	Hannes Laimer
  Cc: pbs-devel

On Mon Jul 21, 2025 at 4:42 PM CEST, Christian Ebner wrote:
> On 7/21/25 4:31 PM, Hannes Laimer wrote:
>> On Mon Jul 21, 2025 at 4:26 PM CEST, Christian Ebner wrote:
>>> On 7/21/25 4:16 PM, Hannes Laimer wrote:
>>>> On Sat Jul 19, 2025 at 2:50 PM CEST, Christian Ebner wrote:
>>>>> Allows to easily refresh the contents on the local cache store for
>>>>> datastores backed by an S3 object store.
>>>>>
>>>>> In order to guarantee that no read or write operations are ongoing,
>>>>> the store is first set into the maintenance mode `S3Refresh`. Objects
>>>>> are then fetched into a temporary directory to avoid loosing contents
>>>>> and consistency in case of an error. Once all objects have been
>>>>> fetched, clears out existing contents and moves the newly fetched
>>>>> contents in place.
>>>>>
>>>>> Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
>>>>> ---
>>>>> changes since version 8:
>>>>> - refactor s3 refresh into more compact methods
>>>>> - drop un-necessary drop(_lock)
>>>>> - use missing tokio::task::spawn_blocking context for blocking
>>>>>     maintenance mode setting
>>>>>
>>>>>    pbs-datastore/src/datastore.rs | 175 +++++++++++++++++++++++++++++++++
>>>>>    src/api2/admin/datastore.rs    |  34 +++++++
>>>>>    2 files changed, 209 insertions(+)
>>>>>
>>>>> diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs
>>>>> index a524d7b32..b2af05eac 100644
>>>>> --- a/pbs-datastore/src/datastore.rs
>>>>> +++ b/pbs-datastore/src/datastore.rs
>>>>> @@ -10,6 +10,7 @@ use anyhow::{bail, format_err, Context, Error};
>>>>>    use http_body_util::BodyExt;
>>>>>    use nix::unistd::{unlinkat, UnlinkatFlags};
>>>>>    use pbs_tools::lru_cache::LruCache;
>>>>> +use tokio::io::AsyncWriteExt;
>>>>>    use tracing::{info, warn};
>>>>>    
>>>>>    use proxmox_human_byte::HumanByte;
>>>>> @@ -2200,4 +2201,178 @@ impl DataStore {
>>>>>        pub fn old_locking(&self) -> bool {
>>>>>            *OLD_LOCKING
>>>>>        }
>>>>> +
>>>>> +    /// Set the datastore's maintenance mode to `S3Refresh`, fetch from S3 object store, clear and
>>>>> +    /// replace the local cache store contents. Once finished disable the maintenance mode again.
>>>>> +    /// Returns with error for other datastore backends without setting the maintenance mode.
>>>>> +    pub async fn s3_refresh(self: &Arc<Self>) -> Result<(), Error> {
>>>>> +        match self.backend()? {
>>>>> +            DatastoreBackend::Filesystem => bail!("store '{}' not backed by S3", self.name()),
>>>>> +            DatastoreBackend::S3(s3_client) => {
>>>>> +                let self_clone = Arc::clone(self);
>>>>> +                tokio::task::spawn_blocking(move || {
>>>>> +                    self_clone.maintenance_mode(Some(MaintenanceMode {
>>>>> +                        ty: MaintenanceType::S3Refresh,
>>>>> +                        message: None,
>>>>> +                    }))
>>>>> +                })
>>>>> +                .await?
>>>>> +                .context("failed to set maintenance mode")?;
>>>>
>>>> I think we should hold the config lock, so it can't be changed while we
>>>> refresh, no?
>>>
>>> Yes, but that is handled by the method itself, also to limit lock scope.
>>>
>>> See further below...
>>>
>> 
>> maybe I'm missing something, but the limited scope is what I mean. I
>> think we should try to prevent changing the maintenance mode away from
>> `S3Refresh` before we're done, so basically holding the lock while we
>> refresh.
>
> No, the intention here is to allow the user to manually clear the 
> maintenance mode again, as the s3 refresh might fail. So the refresh 
> manually sets it to guarantee consistency and clears it if finished.
>
> If the lock on the config is kept for the whole refresh, than the user 
> will never be able to recover from an error.

ACK, you is right!



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (49 preceding siblings ...)
  2025-07-21 14:24 ` [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Hannes Laimer
@ 2025-07-21 15:05 ` Lukas Wagner
  2025-07-21 15:37   ` Christian Ebner
  2025-07-21 16:46 ` Christian Ebner
  51 siblings, 1 reply; 74+ messages in thread
From: Lukas Wagner @ 2025-07-21 15:05 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Christian Ebner

Retested these patches on the latest master branch(es).

Retested basic backups, sync jobs, verification, GC, pruning, etc.

This time I tried to focus more on different failure scenarios, e.g. a
failing connection to the S3 server during different operations.

Here's what I found, most of these issues I did already discuss and
debug off-list with @Chris:

1.)

When doing an S3 Refresh and PBS cannot connect to S3, a `tmp_xxxxxxx`
directory is left over in the local datastore directory. After clearing
S3 Refresh maintenance mode (or doing a successful S3 refresh), GC jobs
will fail because they cannot access this left-over directory (it is
owned by root:root).
AFAIK Chris has already prepared a fix for this.

2.)

I backed up some VMs to my local MinIO server which ran out of disk
space during backup. Since even delete operations failed in this
scenario, PBS could not clean up the snapshot directory, which was
left over after this failed backup. In some instances, the snapshot
directory was completely empty, in some other case two blobs were
written, but the fidx files were missing:

  root@pbs-s3:/s3-store/ns/pali/vm# ls 160/2025-07-21T12\:51\:44Z/
  fw.conf.blob  qemu-server.conf.blob
  root@pbs-s3:/s3-store/ns/pali/vm# ls 165/
  2025-07-21T12:52:42Z/ owner                 
  root@pbs-s3:/s3-store/ns/pali/vm# ls 165/2025-07-21T12\:52\:42Z/
  root@pbs-s3:/s3-store/ns/pali/vm#

I could fix this by doing a "S3 Refresh" and then manually deleting the
affected snapshot under the "Content" view - something that could be
very annoying if one has hundred/thousands of snapshots, so I think we
need some form of automatic cleanup for fragments from incomplete/failed
backups. After all, I'm pretty sure that one could end up in a similar
situation by just cutting the network connection to the S3 server at the
right moment in time.

3.)

Cut the connection to my MinIO server during a verification job.
The task log was spammed by the following messages:

  2025-07-21T16:06:51+02:00: failed to copy corrupt chunk on s3 backend: 747835eb948591da7c4ebe892a9eb28c0daa8978bb80b70350f5b07225a1b9b0
  2025-07-21T16:06:51+02:00: corrupted chunk renamed to "/s3-store/.chunks/7478/747835eb948591da7c4ebe892a9eb28c0daa8978bb80b70350f5b07225a1b9b0.0.bad"
  2025-07-21T16:06:51+02:00: "can't verify chunk, load failed - client error (Connect)"
  2025-07-21T16:06:51+02:00: failed to copy corrupt chunk on s3 backend: 5680458c0dba35dd1b528b5e38d32d410aee285f4d0328bbd8814fb5eb129aaf
  2025-07-21T16:06:51+02:00: corrupted chunk renamed to "/s3-store/.chunks/5680/5680458c0dba35dd1b528b5e38d32d410aee285f4d0328bbd8814fb5eb129aaf.0.bad"

While not really catastrophic, since these chunks would then just be
refetched from S3 on the next access, this probably should be handled
better/more gracefully.

One thing that I spotted in the documentation was the following:

  proxmox-backup-manager s3 client create my-s3-client --secrets-id my-s3-client ...

The user has to specify the client ID twice, one for the regular config,
one for the secret config. This was implemented this way due to how
parameter flattening for API type structs work. I discussed this
with @Chris and suggested another approach, one that works without
duplicating the ID to hopefully make the UX a bit nicer.

Apart from these issues everything seemed to work fine.

Tested-by: Lukas Wagner <l.wagner@proxmox.com>


_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores
  2025-07-21 15:05 ` Lukas Wagner
@ 2025-07-21 15:37   ` Christian Ebner
  0 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-21 15:37 UTC (permalink / raw)
  To: Lukas Wagner, Proxmox Backup Server development discussion

On 7/21/25 5:05 PM, Lukas Wagner wrote:
> Retested these patches on the latest master branch(es).
> 
> Retested basic backups, sync jobs, verification, GC, pruning, etc.
> 
> This time I tried to focus more on different failure scenarios, e.g. a
> failing connection to the S3 server during different operations.
> 
> Here's what I found, most of these issues I did already discuss and
> debug off-list with @Chris:
> 
> 1.)
> 
> When doing an S3 Refresh and PBS cannot connect to S3, a `tmp_xxxxxxx`
> directory is left over in the local datastore directory. After clearing
> S3 Refresh maintenance mode (or doing a successful S3 refresh), GC jobs
> will fail because they cannot access this left-over directory (it is
> owned by root:root).
> AFAIK Chris has already prepared a fix for this.

Will be fixed in the next version of the patch series, thanks!

> 
> 2.)
> 
> I backed up some VMs to my local MinIO server which ran out of disk
> space during backup. Since even delete operations failed in this
> scenario, PBS could not clean up the snapshot directory, which was
> left over after this failed backup. In some instances, the snapshot
> directory was completely empty, in some other case two blobs were
> written, but the fidx files were missing:
> 
>    root@pbs-s3:/s3-store/ns/pali/vm# ls 160/2025-07-21T12\:51\:44Z/
>    fw.conf.blob  qemu-server.conf.blob
>    root@pbs-s3:/s3-store/ns/pali/vm# ls 165/
>    2025-07-21T12:52:42Z/ owner
>    root@pbs-s3:/s3-store/ns/pali/vm# ls 165/2025-07-21T12\:52\:42Z/
>    root@pbs-s3:/s3-store/ns/pali/vm#
> 
> I could fix this by doing a "S3 Refresh" and then manually deleting the
> affected snapshot under the "Content" view - something that could be
> very annoying if one has hundred/thousands of snapshots, so I think we
> need some form of automatic cleanup for fragments from incomplete/failed
> backups. After all, I'm pretty sure that one could end up in a similar
> situation by just cutting the network connection to the S3 server at the
> right moment in time.

As discussed already a bit off-list, this would indeed be nice to have, 
however I see no way of doing this consistently atm without manual user 
interaction. In your tests cleanup of objects from the s3 backend failed 
because of the out-of-memory, so the user needs to fix that first.

And automatic cleanup of fragments from the S3 store after a connection 
loss might be doable during garbage collection, or verification, I will 
however have to think this through in detail. So best for a followup.

> 
> 3.)
> 
> Cut the connection to my MinIO server during a verification job.
> The task log was spammed by the following messages:
> 
>    2025-07-21T16:06:51+02:00: failed to copy corrupt chunk on s3 backend: 747835eb948591da7c4ebe892a9eb28c0daa8978bb80b70350f5b07225a1b9b0
>    2025-07-21T16:06:51+02:00: corrupted chunk renamed to "/s3-store/.chunks/7478/747835eb948591da7c4ebe892a9eb28c0daa8978bb80b70350f5b07225a1b9b0.0.bad"
>    2025-07-21T16:06:51+02:00: "can't verify chunk, load failed - client error (Connect)"
>    2025-07-21T16:06:51+02:00: failed to copy corrupt chunk on s3 backend: 5680458c0dba35dd1b528b5e38d32d410aee285f4d0328bbd8814fb5eb129aaf
>    2025-07-21T16:06:51+02:00: corrupted chunk renamed to "/s3-store/.chunks/5680/5680458c0dba35dd1b528b5e38d32d410aee285f4d0328bbd8814fb5eb129aaf.0.bad"
> 
> While not really catastrophic, since these chunks would then just be
> refetched from S3 on the next access, this probably should be handled
> better/more gracefully.

Fixed this as well already for the upcoming v10 of the patches, thanks!

> 
> One thing that I spotted in the documentation was the following:
> 
>    proxmox-backup-manager s3 client create my-s3-client --secrets-id my-s3-client ...
> 
> The user has to specify the client ID twice, one for the regular config,
> one for the secret config. This was implemented this way due to how
> parameter flattening for API type structs work. I discussed this
> with @Chris and suggested another approach, one that works without
> duplicating the ID to hopefully make the UX a bit nicer.

Same, this will be fixed with the next iteration.

> Apart from these issues everything seemed to work fine.
> 
> Tested-by: Lukas Wagner <l.wagner@proxmox.com>



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* [pbs-devel] applied: [PATCH proxmox v9 3/3] s3 client: wrap upload with retry into dedicated methods
  2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox v9 3/3] s3 client: wrap upload with retry into dedicated methods Christian Ebner
@ 2025-07-21 15:37   ` Thomas Lamprecht
  0 siblings, 0 replies; 74+ messages in thread
From: Thomas Lamprecht @ 2025-07-21 15:37 UTC (permalink / raw)
  To: pve-devel, pbs-devel, Christian Ebner

On Sat, 19 Jul 2025 14:49:49 +0200, Christian Ebner wrote:
> The `replace` boolean flag in the upload_with_retry() method
> signature unfortunately is not very ergonomic and hinders code
> readability.
> 
> Therefore, wrap the method into helpers covering both variants,
> encoding their behaviour in the method name instead.
> 
> [...]

Applied, thanks!

[3/3] s3 client: wrap upload with retry into dedicated methods
      commit: 7bf21f9da58e74b77dfcd4c6ae00100d80768dad


_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

* Re: [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores
  2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
                   ` (50 preceding siblings ...)
  2025-07-21 15:05 ` Lukas Wagner
@ 2025-07-21 16:46 ` Christian Ebner
  51 siblings, 0 replies; 74+ messages in thread
From: Christian Ebner @ 2025-07-21 16:46 UTC (permalink / raw)
  To: pbs-devel

superseded-by version 10:
https://lore.proxmox.com/pbs-devel/20250721164507.1045869-1-c.ebner@proxmox.com/T/


_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


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

end of thread, other threads:[~2025-07-21 16:45 UTC | newest]

Thread overview: 74+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-07-19 12:49 [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Christian Ebner
2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox v9 1/3] pbs-api-types: extend datastore config by backend config enum Christian Ebner
2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox v9 2/3] pbs-api-types: maintenance: add new maintenance mode S3 refresh Christian Ebner
2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox v9 3/3] s3 client: wrap upload with retry into dedicated methods Christian Ebner
2025-07-21 15:37   ` [pbs-devel] applied: " Thomas Lamprecht
2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 01/46] datastore: add helpers for path/digest to s3 object key conversion Christian Ebner
2025-07-21 12:29   ` Hannes Laimer
2025-07-21 12:51     ` Christian Ebner
2025-07-21 12:55       ` Hannes Laimer
2025-07-21 13:58   ` Hannes Laimer
2025-07-21 14:15     ` Christian Ebner
2025-07-21 14:20       ` Hannes Laimer
2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 02/46] config: introduce s3 object store client configuration Christian Ebner
2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 03/46] api: config: implement endpoints to manipulate and list s3 configs Christian Ebner
2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 04/46] api: datastore: check s3 backend bucket access on datastore create Christian Ebner
2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 05/46] api/cli: add endpoint and command to check s3 client connection Christian Ebner
2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 06/46] datastore: allow to get the backend for a datastore Christian Ebner
2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 07/46] api: backup: store datastore backend in runtime environment Christian Ebner
2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 08/46] api: backup: conditionally upload chunks to s3 object store backend Christian Ebner
2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 09/46] api: backup: conditionally upload blobs " Christian Ebner
2025-07-19 12:49 ` [pbs-devel] [PATCH proxmox-backup v9 10/46] api: backup: conditionally upload indices " Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 11/46] api: backup: conditionally upload manifest " Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 12/46] api: datastore: conditionally upload client log to s3 backend Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 13/46] sync: pull: conditionally upload content " Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 14/46] api: reader: fetch chunks based on datastore backend Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 15/46] datastore: local chunk reader: read chunks based on backend Christian Ebner
2025-07-21 13:12   ` Hannes Laimer
2025-07-21 13:24     ` Christian Ebner
2025-07-21 13:36     ` Lukas Wagner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 16/46] verify worker: add datastore backed to verify worker Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 17/46] verify: implement chunk verification for stores with s3 backend Christian Ebner
2025-07-21 13:35   ` Hannes Laimer
2025-07-21 13:38     ` Christian Ebner
2025-07-21 13:55       ` Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 18/46] datastore: create namespace marker in " Christian Ebner
2025-07-21 13:52   ` Hannes Laimer
2025-07-21 14:01     ` Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 19/46] datastore: create/delete protected marker file on s3 storage backend Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 20/46] datastore: prune groups/snapshots from s3 object store backend Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 21/46] datastore: get and set owner for s3 " Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 22/46] datastore: implement garbage collection for s3 backend Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 23/46] ui: add datastore type selector and reorganize component layout Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 24/46] ui: add s3 client edit window for configuration create/edit Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 25/46] ui: add s3 client view for configuration Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 26/46] ui: expose the s3 client view in the navigation tree Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 27/46] ui: add s3 client selector and bucket field for s3 backend setup Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 28/46] tools: lru cache: add removed callback for evicted cache nodes Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 29/46] tools: async lru cache: implement insert, remove and contains methods Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 30/46] datastore: add local datastore cache for network attached storages Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 31/46] api: backup: use local datastore cache on s3 backend chunk upload Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 32/46] api: reader: use local datastore cache on s3 backend chunk fetching Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 33/46] datastore: local chunk reader: get cached chunk from local cache store Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 34/46] backup writer: refactor parameters into backup writer options struct Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 35/46] api: backup: add no-cache flag to bypass local datastore cache Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 36/46] api/datastore: implement refresh endpoint for stores with s3 backend Christian Ebner
2025-07-21 14:16   ` Hannes Laimer
2025-07-21 14:26     ` Christian Ebner
2025-07-21 14:31       ` Hannes Laimer
2025-07-21 14:42         ` Christian Ebner
2025-07-21 14:48           ` Hannes Laimer
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 37/46] cli: add dedicated subcommand for datastore s3 refresh Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 38/46] ui: render s3 refresh as valid maintenance type and task description Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 39/46] ui: expose s3 refresh button for datastores backed by object store Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 40/46] datastore: conditionally upload atime marker chunk to s3 backend Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 41/46] bin: implement client subcommands for s3 configuration manipulation Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 42/46] bin: expose reuse-datastore flag for proxmox-backup-manager Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 43/46] datastore: mark store as in-use by setting marker on s3 backend Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 44/46] datastore: run s3-refresh when reusing a datastore with " Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 45/46] api/ui: add flag to allow overwriting in-use marker for " Christian Ebner
2025-07-19 12:50 ` [pbs-devel] [PATCH proxmox-backup v9 46/46] docs: Add section describing how to setup s3 backed datastore Christian Ebner
2025-07-21 14:24 ` [pbs-devel] [PATCH proxmox{, -backup} v9 00/49] fix #2943: S3 storage backend for datastores Hannes Laimer
2025-07-21 15:05 ` Lukas Wagner
2025-07-21 15:37   ` Christian Ebner
2025-07-21 16:46 ` Christian Ebner

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