LCOV - code coverage report
Current view: top level - libs/remote_storage/src - error.rs (source / functions) Coverage Total Hit
Test: 465a86b0c1fda0069b3e0f6c1c126e6b635a1f72.info Lines: 49.4 % 85 42
Test Date: 2024-06-25 15:47:26 Functions: 53.3 % 15 8

            Line data    Source code
       1              : /// Reasons for downloads or listings to fail.
       2              : #[derive(Debug)]
       3              : pub enum DownloadError {
       4              :     /// Validation or other error happened due to user input.
       5              :     BadInput(anyhow::Error),
       6              :     /// The file was not found in the remote storage.
       7              :     NotFound,
       8              :     /// A cancellation token aborted the download, typically during
       9              :     /// tenant detach or process shutdown.
      10              :     Cancelled,
      11              :     /// A timeout happened while executing the request. Possible reasons:
      12              :     /// - stuck tcp connection
      13              :     ///
      14              :     /// Concurrency control is not timed within timeout.
      15              :     Timeout,
      16              :     /// The file was found in the remote storage, but the download failed.
      17              :     Other(anyhow::Error),
      18              : }
      19              : 
      20              : impl std::fmt::Display for DownloadError {
      21            9 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
      22            9 :         match self {
      23            0 :             DownloadError::BadInput(e) => {
      24            0 :                 write!(f, "Failed to download a remote file due to user input: {e}")
      25              :             }
      26            0 :             DownloadError::NotFound => write!(f, "No file found for the remote object id given"),
      27            0 :             DownloadError::Cancelled => write!(f, "Cancelled, shutting down"),
      28            0 :             DownloadError::Timeout => write!(f, "timeout"),
      29            9 :             DownloadError::Other(e) => write!(f, "Failed to download a remote file: {e:?}"),
      30              :         }
      31            9 :     }
      32              : }
      33              : 
      34              : impl std::error::Error for DownloadError {}
      35              : 
      36              : impl DownloadError {
      37              :     /// Returns true if the error should not be retried with backoff
      38           14 :     pub fn is_permanent(&self) -> bool {
      39           14 :         use DownloadError::*;
      40           14 :         match self {
      41           14 :             BadInput(_) | NotFound | Cancelled => true,
      42            0 :             Timeout | Other(_) => false,
      43              :         }
      44           14 :     }
      45              : }
      46              : 
      47              : impl From<std::io::Error> for DownloadError {
      48           10 :     fn from(value: std::io::Error) -> Self {
      49           10 :         let needs_unwrap = value.kind() == std::io::ErrorKind::Other
      50           10 :             && value
      51           10 :                 .get_ref()
      52           10 :                 .and_then(|x| x.downcast_ref::<DownloadError>())
      53           10 :                 .is_some();
      54              : 
      55           10 :         if needs_unwrap {
      56           10 :             *value
      57           10 :                 .into_inner()
      58           10 :                 .expect("just checked")
      59           10 :                 .downcast::<DownloadError>()
      60           10 :                 .expect("just checked")
      61              :         } else {
      62            0 :             DownloadError::Other(value.into())
      63              :         }
      64           10 :     }
      65              : }
      66              : 
      67              : #[derive(Debug)]
      68              : pub enum TimeTravelError {
      69              :     /// Validation or other error happened due to user input.
      70              :     BadInput(anyhow::Error),
      71              :     /// The used remote storage does not have time travel recovery implemented
      72              :     Unimplemented,
      73              :     /// The number of versions/deletion markers is above our limit.
      74              :     TooManyVersions,
      75              :     /// A cancellation token aborted the process, typically during
      76              :     /// request closure or process shutdown.
      77              :     Cancelled,
      78              :     /// Other errors
      79              :     Other(anyhow::Error),
      80              : }
      81              : 
      82              : impl std::fmt::Display for TimeTravelError {
      83            0 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
      84            0 :         match self {
      85            0 :             TimeTravelError::BadInput(e) => {
      86            0 :                 write!(
      87            0 :                     f,
      88            0 :                     "Failed to time travel recover a prefix due to user input: {e}"
      89            0 :                 )
      90              :             }
      91            0 :             TimeTravelError::Unimplemented => write!(
      92            0 :                 f,
      93            0 :                 "time travel recovery is not implemented for the current storage backend"
      94            0 :             ),
      95            0 :             TimeTravelError::Cancelled => write!(f, "Cancelled, shutting down"),
      96              :             TimeTravelError::TooManyVersions => {
      97            0 :                 write!(f, "Number of versions/delete markers above limit")
      98              :             }
      99            0 :             TimeTravelError::Other(e) => write!(f, "Failed to time travel recover a prefix: {e:?}"),
     100              :         }
     101            0 :     }
     102              : }
     103              : 
     104              : impl std::error::Error for TimeTravelError {}
     105              : 
     106              : /// Plain cancelled error.
     107              : ///
     108              : /// By design this type does not not implement `std::error::Error` so it cannot be put as the root
     109              : /// cause of `std::io::Error` or `anyhow::Error`. It should never need to be exposed out of this
     110              : /// crate.
     111              : ///
     112              : /// It exists to implement permit acquiring in `{Download,TimeTravel}Error` and `anyhow::Error` returning
     113              : /// operations and ensuring that those get converted to proper versions with just `?`.
     114              : #[derive(Debug)]
     115              : pub(crate) struct Cancelled;
     116              : 
     117              : impl From<Cancelled> for anyhow::Error {
     118            0 :     fn from(_: Cancelled) -> Self {
     119            0 :         anyhow::Error::new(TimeoutOrCancel::Cancel)
     120            0 :     }
     121              : }
     122              : 
     123              : impl From<Cancelled> for TimeTravelError {
     124            0 :     fn from(_: Cancelled) -> Self {
     125            0 :         TimeTravelError::Cancelled
     126            0 :     }
     127              : }
     128              : 
     129              : impl From<Cancelled> for TimeoutOrCancel {
     130            0 :     fn from(_: Cancelled) -> Self {
     131            0 :         TimeoutOrCancel::Cancel
     132            0 :     }
     133              : }
     134              : 
     135              : impl From<Cancelled> for DownloadError {
     136            0 :     fn from(_: Cancelled) -> Self {
     137            0 :         DownloadError::Cancelled
     138            0 :     }
     139              : }
     140              : 
     141              : /// This type is used at as the root cause for timeouts and cancellations with `anyhow::Error` returning
     142              : /// RemoteStorage methods.
     143              : ///
     144              : /// For use with `utils::backoff::retry` and `anyhow::Error` returning operations there is
     145              : /// `TimeoutOrCancel::caused_by_cancel` method to query "proper form" errors.
     146              : #[derive(Debug)]
     147              : pub enum TimeoutOrCancel {
     148              :     Timeout,
     149              :     Cancel,
     150              : }
     151              : 
     152              : impl std::fmt::Display for TimeoutOrCancel {
     153            0 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
     154            0 :         use TimeoutOrCancel::*;
     155            0 :         match self {
     156            0 :             Timeout => write!(f, "timeout"),
     157            0 :             Cancel => write!(f, "cancel"),
     158              :         }
     159            0 :     }
     160              : }
     161              : 
     162              : impl std::error::Error for TimeoutOrCancel {}
     163              : 
     164              : impl TimeoutOrCancel {
     165              :     /// Returns true if the error was caused by [`TimeoutOrCancel::Cancel`].
     166           28 :     pub fn caused_by_cancel(error: &anyhow::Error) -> bool {
     167           28 :         error
     168           28 :             .root_cause()
     169           28 :             .downcast_ref::<Self>()
     170           28 :             .is_some_and(Self::is_cancel)
     171           28 :     }
     172              : 
     173            4 :     pub fn is_cancel(&self) -> bool {
     174            4 :         matches!(self, TimeoutOrCancel::Cancel)
     175            4 :     }
     176              : 
     177            0 :     pub fn is_timeout(&self) -> bool {
     178            0 :         matches!(self, TimeoutOrCancel::Timeout)
     179            0 :     }
     180              : }
     181              : 
     182              : /// This conversion is used when [`crate::support::DownloadStream`] notices a cancellation or
     183              : /// timeout to wrap it in an `std::io::Error`.
     184              : impl From<TimeoutOrCancel> for std::io::Error {
     185           16 :     fn from(value: TimeoutOrCancel) -> Self {
     186           16 :         let e = DownloadError::from(value);
     187           16 :         std::io::Error::other(e)
     188           16 :     }
     189              : }
     190              : 
     191              : impl From<TimeoutOrCancel> for DownloadError {
     192           16 :     fn from(value: TimeoutOrCancel) -> Self {
     193           16 :         use TimeoutOrCancel::*;
     194           16 : 
     195           16 :         match value {
     196            6 :             Timeout => DownloadError::Timeout,
     197           10 :             Cancel => DownloadError::Cancelled,
     198              :         }
     199           16 :     }
     200              : }
        

Generated by: LCOV version 2.1-beta