all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: Proxmox Backup Server development discussion
	<pbs-devel@lists.proxmox.com>,
	Dominik Csapak <d.csapak@proxmox.com>
Subject: Re: [pbs-devel] [PATCH proxmox-backup 1/4] proxmox-backup-proxy: fix leftover references on datastore removal
Date: Tue, 1 Jun 2021 16:25:28 +0200	[thread overview]
Message-ID: <189a22d1-9e4d-d386-f787-33496b4d30c5@proxmox.com> (raw)
In-Reply-To: <20210601121350.19919-2-d.csapak@proxmox.com>

On 01.06.21 14:13, Dominik Csapak wrote:
> when we remove a datastore via api/cli, the proxy
> has sometimes leftover references to that datastore in its
> DATASTORE_MAP which includes an open filehandle on the
> '.lock' file
> 
> this prevents unmounting/exporting the datastore even after removal,
> only a reload/restart of the proxy did help
> 
> add a command to our command socket, which removes all non
> configured datastores from the map, dropping the open filehandle
> 
> Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
> ---
>  src/api2/config/datastore.rs    |  4 +++-
>  src/backup/datastore.rs         | 16 ++++++++++++++++
>  src/bin/proxmox-backup-proxy.rs | 11 +++++++++++
>  src/server.rs                   |  8 ++++++++
>  4 files changed, 38 insertions(+), 1 deletion(-)
> 
> diff --git a/src/api2/config/datastore.rs b/src/api2/config/datastore.rs
> index 6ca98b53..33e76b27 100644
> --- a/src/api2/config/datastore.rs
> +++ b/src/api2/config/datastore.rs
> @@ -403,7 +403,7 @@ pub fn update_datastore(
>      },
>  )]
>  /// Remove a datastore configuration.
> -pub fn delete_datastore(name: String, digest: Option<String>) -> Result<(), Error> {
> +pub async fn delete_datastore(name: String, digest: Option<String>) -> Result<(), Error> {
>  
>      let _lock = open_file_locked(datastore::DATASTORE_CFG_LOCKFILE, std::time::Duration::new(10, 0), true)?;
>  
> @@ -425,6 +425,8 @@ pub fn delete_datastore(name: String, digest: Option<String>) -> Result<(), Erro
>      let _ = jobstate::remove_state_file("prune", &name);
>      let _ = jobstate::remove_state_file("garbage_collection", &name);
>  
> +    crate::server::refresh_datastores().await?;

two methods named exactly the same, one async the other not is a bit weird, even
if in different modules. 

I'd, Either follow the name scheme from the cert command for the send command over
socket helper, e.g., refresh_proxy_datastores or notify_datastore_removed (in
theory the refresh of the internal datastore map may not be the only thing we
want to do (in the future)); just to name two examples for improving on the
duplicate function names

> +
>      Ok(())
>  }
>  
> diff --git a/src/backup/datastore.rs b/src/backup/datastore.rs
> index 584b2020..6989313d 100644
> --- a/src/backup/datastore.rs
> +++ b/src/backup/datastore.rs
> @@ -69,6 +69,22 @@ impl DataStore {
>          Ok(datastore)
>      }
>  
> +    /// removes all datastores that are not configured anymore
> +    pub fn refresh_datastores() -> Result<(), Error>{
> +        let (config, _digest) = datastore::config()?;
> +        let mut stores = HashSet::new();
> +        for (store, _) in config.sections {
> +            stores.insert(store);
> +        }

above feels a bit odd as the section config effectively is a hashmap already
and it's loaded anyway

> +
> +        let mut map = DATASTORE_MAP.lock().unwrap();
> +        // removes all elements that are not in the config
> +        map.retain(|key, _| {
> +            stores.contains(key)
> +        });

Single expression closures do not need { }

map.retain(|key| stores.contains(key)); // drop removed datastores

> +        Ok(())
> +    }
> +
>      fn open_with_path(store_name: &str, path: &Path, config: DataStoreConfig) -> Result<Self, Error> {
>          let chunk_store = ChunkStore::open(store_name, path)?;
>  
> diff --git a/src/bin/proxmox-backup-proxy.rs b/src/bin/proxmox-backup-proxy.rs
> index a53f554a..e2953417 100644
> --- a/src/bin/proxmox-backup-proxy.rs
> +++ b/src/bin/proxmox-backup-proxy.rs
> @@ -136,6 +136,17 @@ async fn run() -> Result<(), Error> {
>          },
>      )?;
>  
> +    // to remove references for not configured datastores
> +    commando_sock.register_command(
> +        "refresh-datastores".to_string(),
> +        |_value| {
> +            if let Err(err) = proxmox_backup::backup::DataStore::refresh_datastores() {
> +                log::error!("could not refresh datastores: {}", err);
> +            }
> +            Ok(Value::Null)
> +        }
> +    )?;
> +
>      let server = daemon::create_daemon(
>          ([0,0,0,0,0,0,0,0], 8007).into(),
>          move |listener, ready| {
> diff --git a/src/server.rs b/src/server.rs
> index ba25617d..aa068a07 100644
> --- a/src/server.rs
> +++ b/src/server.rs
> @@ -100,3 +100,11 @@ pub(crate) async fn reload_proxy_certificate() -> Result<(), Error> {
>          .await?;
>      Ok(())
>  }
> +
> +pub(crate) async fn refresh_datastores() -> Result<(), Error> {
> +    let proxy_pid = crate::server::read_pid(buildcfg::PROXMOX_BACKUP_PROXY_PID_FN)?;
> +    let sock = crate::server::ctrl_sock_from_pid(proxy_pid);
> +    let _: Value = crate::server::send_raw_command(sock, "{\"command\":\"refresh-datastores\"}\n")
> +        .await?;
> +    Ok(())
> +}
> 





  reply	other threads:[~2021-06-01 14:26 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-01 12:13 [pbs-devel] [PATCH proxmox-backup 0/4] improve datastore removal/creation Dominik Csapak
2021-06-01 12:13 ` [pbs-devel] [PATCH proxmox-backup 1/4] proxmox-backup-proxy: fix leftover references on datastore removal Dominik Csapak
2021-06-01 14:25   ` Thomas Lamprecht [this message]
2021-06-01 12:13 ` [pbs-devel] [PATCH proxmox-backup 2/4] api2/confif/datastore: add create datastore api call in a worker Dominik Csapak
2021-06-01 14:33   ` Thomas Lamprecht
2021-06-02  6:34     ` Dietmar Maurer
2021-06-01 12:13 ` [pbs-devel] [PATCH proxmox-backup 3/4] backup/chunk_store: optionally log progress on creation Dominik Csapak
2021-06-01 12:13 ` [pbs-devel] [PATCH proxmox-backup 4/4] ui: DataStoreList: add remove button Dominik Csapak
2021-06-01 14:37   ` Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=189a22d1-9e4d-d386-f787-33496b4d30c5@proxmox.com \
    --to=t.lamprecht@proxmox.com \
    --cc=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal