From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <l.wagner@proxmox.com> Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 2F06AEFF1 for <pve-devel@lists.proxmox.com>; Thu, 28 Sep 2023 13:50:51 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id CB1B015AA3 for <pve-devel@lists.proxmox.com>; Thu, 28 Sep 2023 13:50:20 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for <pve-devel@lists.proxmox.com>; Thu, 28 Sep 2023 13:50:17 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 564C748E2F for <pve-devel@lists.proxmox.com>; Thu, 28 Sep 2023 13:50:16 +0200 (CEST) From: Lukas Wagner <l.wagner@proxmox.com> To: pve-devel@lists.proxmox.com Date: Thu, 28 Sep 2023 13:50:11 +0200 Message-Id: <20230928115012.326777-7-l.wagner@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230928115012.326777-1-l.wagner@proxmox.com> References: <20230928115012.326777-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.031 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [PATCH v2 proxmox-perl-rs 6/7] cache: add bindings for `SharedCache` X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com> List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe> List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/> List-Post: <mailto:pve-devel@lists.proxmox.com> List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help> List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe> X-List-Received-Date: Thu, 28 Sep 2023 11:50:51 -0000 These bindings are contained in the `SharedCacheBase` class, which is subclassed by `SharedCache` in Perl. The subclass was needed to implement the `get_or_update` method since that requires to call a closure as a passed parameter. Signed-off-by: Lukas Wagner <l.wagner@proxmox.com> --- Notes: Changes v1 -> v2: - Added lock/unlock - Added get_or_update in perl subclass - added *_with_lock methods common/pkg/Makefile | 1 + common/pkg/Proxmox/RS/SharedCache.pm | 46 ++++++++++++++ common/src/mod.rs | 1 + common/src/shared_cache.rs | 89 ++++++++++++++++++++++++++++ pve-rs/Cargo.toml | 1 + 5 files changed, 138 insertions(+) create mode 100644 common/pkg/Proxmox/RS/SharedCache.pm create mode 100644 common/src/shared_cache.rs diff --git a/common/pkg/Makefile b/common/pkg/Makefile index 7bf669f..a99c30d 100644 --- a/common/pkg/Makefile +++ b/common/pkg/Makefile @@ -26,6 +26,7 @@ Proxmox/RS/CalendarEvent.pm: Proxmox::RS::APT::Repositories \ Proxmox::RS::CalendarEvent \ Proxmox::RS::Notify \ + Proxmox::RS::SharedCacheBase \ Proxmox::RS::Subscription all: Proxmox/RS/CalendarEvent.pm diff --git a/common/pkg/Proxmox/RS/SharedCache.pm b/common/pkg/Proxmox/RS/SharedCache.pm new file mode 100644 index 0000000..a35e0c5 --- /dev/null +++ b/common/pkg/Proxmox/RS/SharedCache.pm @@ -0,0 +1,46 @@ +package Proxmox::RS::SharedCache; + +use base 'Proxmox::RS::SharedCacheBase'; + +use strict; +use warnings; + +# This part has to be implemented in perl, since we calculate the new value on +# demand from a passed closure. +sub get_or_update { + my ($self, $key, $value_func, $timeout) = @_; + + #Lookup value + my $val = $self->get($key); + + if (!$val) { + my $lock = undef; + eval { + # If expired, lock cache entry. This makes sure that other processes + # cannot update it at the same time. + $lock = $self->lock($key, 1); + + # Check again, may somebody else has already updated the value + $val = $self->get_with_lock($key, $lock); + + # If still expired, update it + if (!$val) { + $val = $value_func->(); + $self->set_with_lock($key, $val, $timeout, $lock); + } + }; + + my $err = $@; + + # If the file has been locked, we *must* unlock it, no matter what + if (defined($lock)) { + $self->unlock($lock) + } + + die $err if $err; + } + + return $val; +} + +1; diff --git a/common/src/mod.rs b/common/src/mod.rs index c3574f4..badfc98 100644 --- a/common/src/mod.rs +++ b/common/src/mod.rs @@ -2,4 +2,5 @@ pub mod apt; mod calendar_event; pub mod logger; pub mod notify; +pub mod shared_cache; mod subscription; diff --git a/common/src/shared_cache.rs b/common/src/shared_cache.rs new file mode 100644 index 0000000..0e2b561 --- /dev/null +++ b/common/src/shared_cache.rs @@ -0,0 +1,89 @@ +#[perlmod::package(name = "Proxmox::RS::SharedCacheBase")] +mod export { + use std::os::fd::{FromRawFd, IntoRawFd, RawFd}; + + use anyhow::Error; + use nix::sys::stat::Mode; + use perlmod::Value; + use serde_json::Value as JSONValue; + + use proxmox_shared_cache::{SharedCache, CacheLockGuard}; + use proxmox_sys::fs::CreateOptions; + + pub struct CacheWrapper(SharedCache); + + perlmod::declare_magic!(Box<CacheWrapper> : &CacheWrapper as "Proxmox::RS::SharedCacheBase"); + + #[export(raw_return)] + fn new(#[raw] class: Value, base_dir: &str) -> Result<Value, Error> { + // TODO: Make this configurable once we need to cache values that should be + // accessible by other users. + let options = CreateOptions::new() + .root_only() + .perm(Mode::from_bits_truncate(0o700)); + + Ok(perlmod::instantiate_magic!(&class, MAGIC => Box::new( + CacheWrapper ( + SharedCache::new(base_dir, options)? + ) + ))) + } + + #[export] + fn set( + #[try_from_ref] this: &CacheWrapper, + key: &str, + value: JSONValue, + expires_in: Option<i64>, + ) -> Result<(), Error> { + this.0.set(key, &value, expires_in) + } + + #[export] + fn set_with_lock( + #[try_from_ref] this: &CacheWrapper, + key: &str, + value: JSONValue, + expires_in: Option<i64>, + lock: RawFd, + ) -> Result<(), Error> { + let lock = unsafe { CacheLockGuard::from_raw_fd(lock) }; + this.0.set_with_lock(key, &value, expires_in, &lock) + } + + #[export] + fn get(#[try_from_ref] this: &CacheWrapper, key: &str) -> Result<Option<JSONValue>, Error> { + this.0.get(key) + } + + #[export] + fn get_with_lock(#[try_from_ref] this: &CacheWrapper, key: &str, lock: RawFd) -> Result<Option<JSONValue>, Error> { + let lock = unsafe { CacheLockGuard::from_raw_fd(lock) }; + this.0.get_with_lock(key, &lock) + } + + #[export] + fn delete(#[try_from_ref] this: &CacheWrapper, key: &str) -> Result<(), Error> { + this.0.delete(key) + } + + #[export] + fn delete_with_lock(#[try_from_ref] this: &CacheWrapper, key: &str, lock: RawFd) -> Result<(), Error> { + let lock = unsafe { CacheLockGuard::from_raw_fd(lock) }; + this.0.delete_with_lock(key, &lock) + } + + #[export] + fn lock(#[try_from_ref] this: &CacheWrapper, key: &str, exclusive: bool) -> Result<RawFd, Error> { + let file = this.0.lock(key, exclusive)?; + Ok(file.into_raw_fd()) + } + + #[export] + fn unlock(#[try_from_ref] _this: &CacheWrapper, lock: RawFd) -> Result<(), Error> { + // advisory file locks using flock are unlocked once the FD is closed + let _ = unsafe { CacheLockGuard::from_raw_fd(lock) }; + + Ok(()) + } +} diff --git a/pve-rs/Cargo.toml b/pve-rs/Cargo.toml index f9e3291..fa78dd6 100644 --- a/pve-rs/Cargo.toml +++ b/pve-rs/Cargo.toml @@ -39,6 +39,7 @@ proxmox-http-error = "0.1.0" proxmox-notify = "0.2" proxmox-openid = "0.10" proxmox-resource-scheduling = "0.3.0" +proxmox-shared-cache = "0.1.0" proxmox-subscription = "0.4" proxmox-sys = "0.5" proxmox-tfa = { version = "4.0.4", features = ["api"] } -- 2.39.2