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 E2DC871887 for ; Tue, 29 Jun 2021 11:58:46 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id D9AEEC94A for ; Tue, 29 Jun 2021 11:58:16 +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 1C321C934 for ; Tue, 29 Jun 2021 11:58:16 +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 E83FF467E0 for ; Tue, 29 Jun 2021 11:58:15 +0200 (CEST) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Tue, 29 Jun 2021 11:58:14 +0200 Message-Id: <20210629095814.19169-1-d.csapak@proxmox.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.724 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. [mod.rs] Subject: [pbs-devel] [PATCH proxmox-backup] tape/drive: fix logging when requesting media 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: Tue, 29 Jun 2021 09:58:46 -0000 we try to load the correct media in a loop until we find the correct tape. when encountering an error or wrong tape, we want to log that (and send an email if one is set) that requests the correct tape. while trying to avoid printing the same errors more than once in a row, we had at least one case (starting with an empty tape in the drive) which would not print/send any tape request. reworking that code to use a custom 'TapeRequest' enum, which contains the state + error message, and a helper that prints and sends an email when the state changes this reduces the change check/log to a single variable, instead of 4 (tried, last_media_uuid, last_error, failure_reason) Signed-off-by: Dominik Csapak --- src/tape/drive/mod.rs | 138 +++++++++++++++++++++++++----------------- 1 file changed, 83 insertions(+), 55 deletions(-) diff --git a/src/tape/drive/mod.rs b/src/tape/drive/mod.rs index 7bc02f9e..2264ce0c 100644 --- a/src/tape/drive/mod.rs +++ b/src/tape/drive/mod.rs @@ -321,6 +321,29 @@ pub fn open_drive( } } +#[derive(PartialEq, Eq)] +enum TapeRequest { + None, + OpenFailed(String), + WrongLabel(String), + EmptyTape(String), + ReadFailed(String), +} + +impl std::fmt::Display for TapeRequest { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let txt = match self { + TapeRequest::None => "", + TapeRequest::OpenFailed(reason) => reason, + TapeRequest::WrongLabel(reason) => reason, + TapeRequest::EmptyTape(reason) => reason, + TapeRequest::ReadFailed(reason) => reason, + }; + + write!(f, "{}", txt) + } +} + /// Requests a specific 'media' to be inserted into 'drive'. Within a /// loop, this then tries to read the media label and waits until it /// finds the requested media. @@ -388,84 +411,89 @@ pub fn request_and_load_media( return Ok((handle, media_id)); } - let mut last_media_uuid = None; - let mut last_error = None; - - let mut tried = false; - let mut failure_reason = None; + let mut request_state = TapeRequest::None; + + let update_and_log_request_state = |old_state: &mut TapeRequest, + new_state: TapeRequest| + -> Result<(), Error> { + if new_state != *old_state { + task_log!(worker, "{}", &new_state); + task_log!( + worker, + "Please insert media '{}' into drive '{}'", + label_text, + drive + ); + if let Some(to) = notify_email { + send_load_media_email( + drive, + &label_text, + to, + Some(new_state.to_string()), + )?; + } + *old_state = new_state; + } + Ok(()) + }; loop { worker.check_abort()?; - if tried { - if let Some(reason) = failure_reason { - task_log!(worker, "Please insert media '{}' into drive '{}'", label_text, drive); - if let Some(to) = notify_email { - send_load_media_email(drive, &label_text, to, Some(reason))?; - } - } - - failure_reason = None; - + if request_state != TapeRequest::None { for _ in 0..50 { // delay 5 seconds worker.check_abort()?; std::thread::sleep(std::time::Duration::from_millis(100)); } + } else { + task_log!( + worker, + "Checking for media '{}' in drive '{}'", + label_text, + drive + ); } - tried = true; - let mut handle = match drive_config.open() { Ok(handle) => handle, Err(err) => { - let err = err.to_string(); - if Some(err.clone()) != last_error { - task_log!(worker, "tape open failed - {}", err); - last_error = Some(err); - failure_reason = last_error.clone(); - } + let err = format!("tape open failed - {}", &err); + update_and_log_request_state( + &mut request_state, + TapeRequest::OpenFailed(err), + )?; continue; } }; - match handle.read_label() { + let new_state = match handle.read_label() { + Ok((Some(media_id), _)) if media_id.label.uuid == label.uuid => { + task_log!( + worker, + "found media label {} ({})", + media_id.label.label_text, + media_id.label.uuid.to_string(), + ); + return Ok((Box::new(handle), media_id)); + } Ok((Some(media_id), _)) => { - if media_id.label.uuid == label.uuid { - task_log!( - worker, - "found media label {} ({})", - media_id.label.label_text, - media_id.label.uuid.to_string(), - ); - return Ok((Box::new(handle), media_id)); - } else if Some(media_id.label.uuid.clone()) != last_media_uuid { - let err = format!( - "wrong media label {} ({})", - media_id.label.label_text, - media_id.label.uuid.to_string(), - ); - task_log!(worker, "{}", err); - last_media_uuid = Some(media_id.label.uuid); - failure_reason = Some(err); - } + let err = format!( + "wrong media label {} ({})", + media_id.label.label_text, + media_id.label.uuid.to_string(), + ); + TapeRequest::WrongLabel(err.clone()) } Ok((None, _)) => { - if last_media_uuid.is_some() { - let err = "found empty media without label (please label all tapes first)"; - task_log!(worker, "{}", err); - last_media_uuid = None; - failure_reason = Some(err.to_string()); - } + let err = "found empty media without label (please label all tapes first)"; + TapeRequest::EmptyTape(err.to_string()) } Err(err) => { - let err = err.to_string(); - if Some(err.clone()) != last_error { - task_log!(worker, "tape open failed - {}", err); - last_error = Some(err); - failure_reason = last_error.clone(); - } + TapeRequest::ReadFailed(format!("tape open failed - {}", err)) } - } + }; + + update_and_log_request_state(&mut request_state, new_state)?; } } _ => bail!("drive type '{}' not implemented!"), -- 2.20.1