From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 2ADC375379 for ; Wed, 13 Oct 2021 10:25:05 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 08C2DE61A for ; Wed, 13 Oct 2021 10:25:04 +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 id A52C4E4FF for ; Wed, 13 Oct 2021 10:24:57 +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 77ED145FF2; Wed, 13 Oct 2021 10:24:57 +0200 (CEST) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Wed, 13 Oct 2021 10:24:42 +0200 Message-Id: <20211013082452.619406-6-dietmar@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211013082452.619406-1-dietmar@proxmox.com> References: <20211013082452.619406-1-dietmar@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.543 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% 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 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [cache.rs, rrd.rs, status.rs, lib.rs] Subject: [pbs-devel] [PATCH proxmox-backup 05/15] proxmox-rrd: remove dependency to proxmox-rrd-api-types X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Oct 2021 08:25:05 -0000 --- proxmox-rrd/Cargo.toml | 2 -- proxmox-rrd/src/cache.rs | 30 ++++++++---------------------- proxmox-rrd/src/rrd.rs | 13 ++++++++++--- src/api2/node/rrd.rs | 9 +++------ src/api2/status.rs | 9 ++------- src/lib.rs | 32 +++++++++++++++++++++++++++++++- 6 files changed, 54 insertions(+), 41 deletions(-) diff --git a/proxmox-rrd/Cargo.toml b/proxmox-rrd/Cargo.toml index b3dd02c3..69a68530 100644 --- a/proxmox-rrd/Cargo.toml +++ b/proxmox-rrd/Cargo.toml @@ -17,5 +17,3 @@ serde_cbor = "0.11.1" proxmox = { version = "0.14.0" } proxmox-time = "1" proxmox-schema = { version = "1", features = [ "api-macro" ] } - -proxmox-rrd-api-types = { path = "../proxmox-rrd-api-types" } diff --git a/proxmox-rrd/src/cache.rs b/proxmox-rrd/src/cache.rs index f14837fc..5e359d9b 100644 --- a/proxmox-rrd/src/cache.rs +++ b/proxmox-rrd/src/cache.rs @@ -11,8 +11,6 @@ use nix::fcntl::OFlag; use proxmox::tools::fs::{atomic_open_or_create_file, create_path, CreateOptions}; -use proxmox_rrd_api_types::{RRDMode, RRDTimeFrameResolution}; - use crate::rrd::{DST, CF, RRD, RRA}; const RRD_JOURNAL_NAME: &str = "rrd.journal"; @@ -280,35 +278,23 @@ impl RRDCache { } /// Extract data from cached RRD + /// + /// `start`: Start time. If not sepecified, we simply extract 10 data points. + /// `end`: End time. Default is to use the current time. pub fn extract_cached_data( &self, base: &str, name: &str, - now: f64, - timeframe: RRDTimeFrameResolution, - mode: RRDMode, + cf: CF, + resolution: u64, + start: Option, + end: Option, ) -> Result>)>, Error> { let state = self.state.read().unwrap(); - let cf = match mode { - RRDMode::Max => CF::Maximum, - RRDMode::Average => CF::Average, - }; - - let now = now as u64; - - let (start, resolution) = match timeframe { - RRDTimeFrameResolution::Hour => (now - 3600, 60), - RRDTimeFrameResolution::Day => (now - 3600*24, 60), - RRDTimeFrameResolution::Week => (now - 3600*24*7, 30*60), - RRDTimeFrameResolution::Month => (now - 3600*24*30, 30*60), - RRDTimeFrameResolution::Year => (now - 3600*24*365, 6*60*60), - RRDTimeFrameResolution::Decade => (now - 10*3600*24*366, 7*86400), - }; - match state.rrd_map.get(&format!("{}/{}", base, name)) { - Some(rrd) => Ok(Some(rrd.extract_data(start, now, cf, resolution)?)), + Some(rrd) => Ok(Some(rrd.extract_data(cf, resolution, start, end)?)), None => Ok(None), } } diff --git a/proxmox-rrd/src/rrd.rs b/proxmox-rrd/src/rrd.rs index 82fa5a3a..c97be96d 100644 --- a/proxmox-rrd/src/rrd.rs +++ b/proxmox-rrd/src/rrd.rs @@ -339,12 +339,15 @@ impl RRD { /// /// This selects the RRA with specified [CF] and (minimum) /// resolution, and extract data from `start` to `end`. + /// + /// `start`: Start time. If not sepecified, we simply extract 10 data points. + /// `end`: End time. Default is to use the current time. pub fn extract_data( &self, - start: u64, - end: u64, cf: CF, resolution: u64, + start: Option, + end: Option, ) -> Result<(u64, u64, Vec>), Error> { let mut rra: Option<&RRA> = None; @@ -362,7 +365,11 @@ impl RRD { } match rra { - Some(rra) => Ok(rra.extract_data(start, end, self.source.last_update)), + Some(rra) => { + let end = end.unwrap_or_else(|| proxmox_time::epoch_f64() as u64); + let start = start.unwrap_or(end - 10*rra.resolution); + Ok(rra.extract_data(start, end, self.source.last_update)) + } None => bail!("unable to find RRA suitable ({:?}:{})", cf, resolution), } } diff --git a/src/api2/node/rrd.rs b/src/api2/node/rrd.rs index 4fe18f46..21a55bb7 100644 --- a/src/api2/node/rrd.rs +++ b/src/api2/node/rrd.rs @@ -9,26 +9,23 @@ use pbs_api_types::{ NODE_SCHEMA, RRDMode, RRDTimeFrameResolution, PRIV_SYS_AUDIT, }; -use crate::get_rrd_cache; +use crate::extract_rrd_data; pub fn create_value_from_rrd( basedir: &str, list: &[&str], timeframe: RRDTimeFrameResolution, - cf: RRDMode, + mode: RRDMode, ) -> Result { let mut result: Vec = Vec::new(); - let now = proxmox_time::epoch_f64(); - - let rrd_cache = get_rrd_cache()?; let mut timemap = BTreeMap::new(); let mut last_resolution = None; for name in list { - let (start, reso, data) = match rrd_cache.extract_cached_data(basedir, name, now, timeframe, cf)? { + let (start, reso, data) = match extract_rrd_data(basedir, name, timeframe, mode)? { Some(result) => result, None => continue, }; diff --git a/src/api2/status.rs b/src/api2/status.rs index 2476fe97..9a72ce52 100644 --- a/src/api2/status.rs +++ b/src/api2/status.rs @@ -22,7 +22,7 @@ use pbs_datastore::DataStore; use pbs_config::CachedUserInfo; use crate::tools::statistics::{linear_regression}; -use crate::get_rrd_cache; +use crate::extract_rrd_data; #[api( returns: { @@ -90,8 +90,6 @@ pub fn datastore_status( let mut list = Vec::new(); - let rrd_cache = get_rrd_cache()?; - for (store, (_, _)) in &config.sections { let user_privs = user_info.lookup_privs(&auth_id, &["datastore", &store]); let allowed = (user_privs & (PRIV_DATASTORE_AUDIT| PRIV_DATASTORE_BACKUP)) != 0; @@ -123,13 +121,10 @@ pub fn datastore_status( }); let rrd_dir = format!("datastore/{}", store); - let now = proxmox_time::epoch_f64(); - - let get_rrd = |what: &str| rrd_cache.extract_cached_data( + let get_rrd = |what: &str| extract_rrd_data( &rrd_dir, what, - now, RRDTimeFrameResolution::Month, RRDMode::Average, ); diff --git a/src/lib.rs b/src/lib.rs index a1ac23bf..6745627a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,9 +10,10 @@ use anyhow::{format_err, Error}; use proxmox::tools::fs::CreateOptions; +use pbs_api_types::{RRDMode, RRDTimeFrameResolution}; use pbs_buildcfg::configdir; use pbs_tools::cert::CertInfo; -use proxmox_rrd::RRDCache; +use proxmox_rrd::{rrd::CF, RRDCache}; #[macro_use] pub mod tools; @@ -78,3 +79,32 @@ pub fn initialize_rrd_cache() -> Result<&'static RRDCache, Error> { Ok(RRD_CACHE.get().unwrap()) } + +/// Extracts data for the specified time frame from from RRD cache +pub fn extract_rrd_data( + basedir: &str, + name: &str, + timeframe: RRDTimeFrameResolution, + mode: RRDMode, +) -> Result>)>, Error> { + + let end = proxmox_time::epoch_f64() as u64; + + let (start, resolution) = match timeframe { + RRDTimeFrameResolution::Hour => (end - 3600, 60), + RRDTimeFrameResolution::Day => (end - 3600*24, 60), + RRDTimeFrameResolution::Week => (end - 3600*24*7, 30*60), + RRDTimeFrameResolution::Month => (end - 3600*24*30, 30*60), + RRDTimeFrameResolution::Year => (end - 3600*24*365, 6*60*60), + RRDTimeFrameResolution::Decade => (end - 10*3600*24*366, 7*86400), + }; + + let cf = match mode { + RRDMode::Max => CF::Maximum, + RRDMode::Average => CF::Average, + }; + + let rrd_cache = get_rrd_cache()?; + + rrd_cache.extract_cached_data(basedir, name, cf, resolution, Some(start), Some(end)) +} -- 2.30.2