api: add get_active_operations endpoint
Signed-off-by: Hannes Laimer <h.laimer@proxmox.com>
This commit is contained in:
parent
758c6ed588
commit
5fd823c3f2
|
@ -1,6 +1,7 @@
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use libc::pid_t;
|
use libc::pid_t;
|
||||||
use nix::unistd::Pid;
|
use nix::unistd::Pid;
|
||||||
|
use std::iter::Sum;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use pbs_api_types::Operation;
|
use pbs_api_types::Operation;
|
||||||
|
@ -8,12 +9,46 @@ use proxmox_sys::fs::{file_read_optional_string, open_file_locked, replace_file,
|
||||||
use proxmox_sys::linux::procfs;
|
use proxmox_sys::linux::procfs;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, Clone, Copy, Default)]
|
||||||
|
pub struct ActiveOperationStats {
|
||||||
|
pub read: i64,
|
||||||
|
pub write: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sum<Self> for ActiveOperationStats {
|
||||||
|
fn sum<I>(iter: I) -> Self
|
||||||
|
where
|
||||||
|
I: Iterator<Item = Self>,
|
||||||
|
{
|
||||||
|
iter.fold(Self::default(), |a, b| Self {
|
||||||
|
read: a.read + b.read,
|
||||||
|
write: a.write + b.write,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Clone)]
|
#[derive(Deserialize, Serialize, Clone)]
|
||||||
struct TaskOperations {
|
struct TaskOperations {
|
||||||
pid: u32,
|
pid: u32,
|
||||||
starttime: u64,
|
starttime: u64,
|
||||||
reading_operations: i64,
|
active_operations: ActiveOperationStats,
|
||||||
writing_operations: i64,
|
}
|
||||||
|
|
||||||
|
pub fn get_active_operations(name: &str) -> Result<ActiveOperationStats, Error> {
|
||||||
|
let path = PathBuf::from(format!("{}/{}", crate::ACTIVE_OPERATIONS_DIR, name));
|
||||||
|
|
||||||
|
Ok(match file_read_optional_string(&path)? {
|
||||||
|
Some(data) => serde_json::from_str::<Vec<TaskOperations>>(&data)?
|
||||||
|
.iter()
|
||||||
|
.filter_map(
|
||||||
|
|task| match procfs::check_process_running(task.pid as pid_t) {
|
||||||
|
Some(stat) if task.starttime == stat.starttime => Some(task.active_operations),
|
||||||
|
_ => None,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.sum(),
|
||||||
|
None => ActiveOperationStats::default(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_active_operations(name: &str, operation: Operation, count: i64) -> Result<(), Error> {
|
pub fn update_active_operations(name: &str, operation: Operation, count: i64) -> Result<(), Error> {
|
||||||
|
@ -43,8 +78,8 @@ pub fn update_active_operations(name: &str, operation: Operation, count: i64) ->
|
||||||
if pid == task.pid {
|
if pid == task.pid {
|
||||||
updated = true;
|
updated = true;
|
||||||
match operation {
|
match operation {
|
||||||
Operation::Read => task.reading_operations += count,
|
Operation::Read => task.active_operations.read += count,
|
||||||
Operation::Write => task.writing_operations += count,
|
Operation::Write => task.active_operations.write += count,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Some(task.clone())
|
Some(task.clone())
|
||||||
|
@ -57,18 +92,12 @@ pub fn update_active_operations(name: &str, operation: Operation, count: i64) ->
|
||||||
};
|
};
|
||||||
|
|
||||||
if !updated {
|
if !updated {
|
||||||
updated_tasks.push(match operation {
|
updated_tasks.push(TaskOperations {
|
||||||
Operation::Read => TaskOperations {
|
pid,
|
||||||
pid,
|
starttime,
|
||||||
starttime,
|
active_operations: match operation {
|
||||||
reading_operations: 1,
|
Operation::Read => ActiveOperationStats { read: 1, write: 0 },
|
||||||
writing_operations: 0,
|
Operation::Write => ActiveOperationStats { read: 0, write: 1 },
|
||||||
},
|
|
||||||
Operation::Write => TaskOperations {
|
|
||||||
pid,
|
|
||||||
starttime,
|
|
||||||
reading_operations: 0,
|
|
||||||
writing_operations: 1,
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,7 @@ use pbs_api_types::{ Authid, BackupContent, Counts, CryptMode,
|
||||||
use pbs_client::pxar::create_zip;
|
use pbs_client::pxar::create_zip;
|
||||||
use pbs_datastore::{
|
use pbs_datastore::{
|
||||||
check_backup_owner, DataStore, BackupDir, BackupGroup, StoreProgress, LocalChunkReader,
|
check_backup_owner, DataStore, BackupDir, BackupGroup, StoreProgress, LocalChunkReader,
|
||||||
CATALOG_NAME,
|
CATALOG_NAME, task_tracking
|
||||||
};
|
};
|
||||||
use pbs_datastore::backup_info::BackupInfo;
|
use pbs_datastore::backup_info::BackupInfo;
|
||||||
use pbs_datastore::cached_chunk_reader::CachedChunkReader;
|
use pbs_datastore::cached_chunk_reader::CachedChunkReader;
|
||||||
|
@ -1590,6 +1590,30 @@ pub fn get_rrd_stats(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[api(
|
||||||
|
input: {
|
||||||
|
properties: {
|
||||||
|
store: {
|
||||||
|
schema: DATASTORE_SCHEMA,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
access: {
|
||||||
|
permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_AUDIT, true),
|
||||||
|
},
|
||||||
|
)]
|
||||||
|
/// Read datastore stats
|
||||||
|
pub fn get_active_operations(
|
||||||
|
store: String,
|
||||||
|
_param: Value,
|
||||||
|
) -> Result<Value, Error> {
|
||||||
|
let active_operations = task_tracking::get_active_operations(&store)?;
|
||||||
|
Ok(json!({
|
||||||
|
"read": active_operations.read,
|
||||||
|
"write": active_operations.write,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
#[api(
|
#[api(
|
||||||
input: {
|
input: {
|
||||||
properties: {
|
properties: {
|
||||||
|
@ -1947,6 +1971,11 @@ pub fn set_backup_owner(
|
||||||
|
|
||||||
#[sortable]
|
#[sortable]
|
||||||
const DATASTORE_INFO_SUBDIRS: SubdirMap = &[
|
const DATASTORE_INFO_SUBDIRS: SubdirMap = &[
|
||||||
|
(
|
||||||
|
"active-operations",
|
||||||
|
&Router::new()
|
||||||
|
.get(&API_METHOD_GET_ACTIVE_OPERATIONS)
|
||||||
|
),
|
||||||
(
|
(
|
||||||
"catalog",
|
"catalog",
|
||||||
&Router::new()
|
&Router::new()
|
||||||
|
|
Loading…
Reference in New Issue