From af76234112ed31095e279a5405bb4b81f0efa57d Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Tue, 9 Mar 2021 08:33:21 +0100 Subject: [PATCH] tape: improve MediaPool allocation by sorting tapes by ctime and label_text --- src/api2/tape/drive.rs | 5 ++++- src/tape/inventory.rs | 2 +- src/tape/media_pool.rs | 23 +++++++++++++++++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/api2/tape/drive.rs b/src/api2/tape/drive.rs index 27e54b3b..db9b919e 100644 --- a/src/api2/tape/drive.rs +++ b/src/api2/tape/drive.rs @@ -1012,7 +1012,10 @@ fn barcode_label_media_worker( ) -> Result<(), Error> { let (mut changer, changer_name) = required_media_changer(drive_config, &drive)?; - let label_text_list = changer.online_media_label_texts()?; + let mut label_text_list = changer.online_media_label_texts()?; + + // make sure we label them in the right order + label_text_list.sort(); let state_path = Path::new(TAPE_STATUS_DIR); diff --git a/src/tape/inventory.rs b/src/tape/inventory.rs index 49bd2985..5b731ace 100644 --- a/src/tape/inventory.rs +++ b/src/tape/inventory.rs @@ -276,7 +276,7 @@ impl Inventory { continue; // belong to another pool } - if set.uuid.as_ref() == [0u8;16] { // should we do this?? + if set.uuid.as_ref() == [0u8;16] { list.push(MediaId { label: entry.id.label.clone(), media_set_label: None, diff --git a/src/tape/media_pool.rs b/src/tape/media_pool.rs index 4e712be2..12f22bd8 100644 --- a/src/tape/media_pool.rs +++ b/src/tape/media_pool.rs @@ -386,7 +386,13 @@ impl MediaPool { } // sort empty_media, newest first -> oldest last - empty_media.sort_unstable_by(|a, b| b.label().ctime.cmp(&a.label().ctime)); + empty_media.sort_unstable_by(|a, b| { + let mut res = b.label().ctime.cmp(&a.label().ctime); + if res == std::cmp::Ordering::Equal { + res = b.label().label_text.cmp(&a.label().label_text); + } + res + }); if let Some(media) = empty_media.pop() { // found empty media, add to media set an use it @@ -416,7 +422,11 @@ impl MediaPool { // sort expired_media, newest first -> oldest last expired_media.sort_unstable_by(|a, b| { - b.media_set_label().unwrap().ctime.cmp(&a.media_set_label().unwrap().ctime) + let mut res = b.media_set_label().unwrap().ctime.cmp(&a.media_set_label().unwrap().ctime); + if res == std::cmp::Ordering::Equal { + res = b.label().label_text.cmp(&a.label().label_text); + } + res }); if let Some(media) = expired_media.pop() { @@ -447,6 +457,15 @@ impl MediaPool { free_media.push(media_id); } + // sort free_media, newest first -> oldest last + free_media.sort_unstable_by(|a, b| { + let mut res = b.label.ctime.cmp(&a.label.ctime); + if res == std::cmp::Ordering::Equal { + res = b.label.label_text.cmp(&a.label.label_text); + } + res + }); + if let Some(media_id) = free_media.pop() { println!("use free media '{}'", media_id.label.label_text); let uuid = media_id.label.uuid.clone();