1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::sync::Arc;

use thiserror::Error;

use crate::append::AppendError;
use crate::state::{LogEntryIdOf, LogEntryOf, State};
use crate::CoordNum;

pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;

#[derive(Debug, Error)]
pub enum SpawnError {
    #[error("invalid log file")]
    InvalidObligationsFile(std::path::PathBuf, #[source] BoxError),

    #[error("invalid working directory")]
    InvalidWorkingDir(std::path::PathBuf, #[source] BoxError),

    #[error("I/O error")]
    IoError(#[from] IoError),

    #[error("other error")]
    Other(#[source] BoxError),
}

#[derive(Debug, Error)]
#[error("{0}")]
pub struct IoError(String, #[source] std::io::Error);

impl IoError {
    pub(crate) fn new(context: impl Into<String>, source: std::io::Error) -> Self {
        Self(context.into(), source)
    }

    pub(crate) fn invalid_data(
        context: impl Into<String>,
        source: impl Into<Box<dyn std::error::Error + Send + Sync>>,
    ) -> Self {
        Self::new(
            context,
            std::io::Error::new(std::io::ErrorKind::InvalidData, source),
        )
    }
}

#[derive(Debug, Error)]
pub enum PrepareSnapshotError {
    #[error("I/O error")]
    IoError(#[from] IoError),

    #[error("node is disoriented")]
    Disoriented,

    #[error("node is shut down")]
    ShutDown,
}

#[derive(Debug, Error)]
pub enum AffirmSnapshotError {
    #[error("I/O error")]
    IoError(#[from] IoError),

    #[error("unknown snapshot")]
    Unknown,

    #[error("node is shut down")]
    ShutDown,
}

#[derive(Debug, Error)]
pub enum InstallSnapshotError {
    #[error("I/O error")]
    IoError(#[from] IoError),

    #[error("snapshot is outdated")]
    Outdated,

    #[error("node is shut down")]
    ShutDown,
}

#[derive(Debug, Error)]
pub enum ReadStaleError {
    #[error("node is disoriented")]
    Disoriented,

    #[error("node is shut down")]
    ShutDown,
}

#[derive(Debug, Error)]
pub enum PrepareError<S: State, C: CoordNum> {
    #[error("conflicting promise")]
    Conflict(C),

    #[error("round already converged")]
    Converged(C, Option<Arc<LogEntryOf<S>>>),

    #[error("I/O error")]
    IoError(#[from] IoError),

    #[error("node is passive")]
    Passive,

    #[error("node is stalled")]
    Stalled,

    #[error("node is shut down")]
    ShutDown,
}

impl<S: State, C: CoordNum> From<PrepareError<S, C>> for AppendError {
    fn from(e: PrepareError<S, C>) -> Self {
        match e {
            PrepareError::Conflict(_) => AppendError::Lost,
            PrepareError::Converged(_, _) => AppendError::Converged,
            PrepareError::IoError(e) => AppendError::IoError(e),
            PrepareError::Passive => AppendError::Passive,
            PrepareError::Stalled => AppendError::Stalled,
            PrepareError::ShutDown => AppendError::ShutDown,
        }
    }
}

#[derive(Debug, Error)]
pub enum AcceptError<S: State, C: CoordNum> {
    #[error("conflicting promise")]
    Conflict(C),

    #[error("round already converged")]
    Converged(C, Option<Arc<LogEntryOf<S>>>),

    #[error("I/O error")]
    IoError(#[from] IoError),

    #[error("node is passive")]
    Passive,

    #[error("node is stalled")]
    Stalled,

    #[error("node is shut down")]
    ShutDown,
}

impl<S: State, C: CoordNum> From<AcceptError<S, C>> for AppendError {
    fn from(e: AcceptError<S, C>) -> Self {
        match e {
            AcceptError::Conflict(_) => AppendError::Lost,
            AcceptError::Converged(_, _) => AppendError::Converged,
            AcceptError::IoError(e) => AppendError::IoError(e),
            AcceptError::Passive => AppendError::Passive,
            AcceptError::Stalled => AppendError::Stalled,
            AcceptError::ShutDown => AppendError::ShutDown,
        }
    }
}

#[non_exhaustive]
#[derive(Debug, Error)]
pub enum CommitError<S: State> {
    #[error("node is disoriented")]
    Disoriented,

    /// The given id could not be resolved to a log entry.
    #[error("given log entry id is invalid")]
    InvalidEntryId(LogEntryIdOf<S>),

    /// The paxakos node was shut down.
    #[error("node is shut down")]
    ShutDown,
}

impl<S: State> From<CommitError<S>> for AppendError {
    fn from(e: CommitError<S>) -> Self {
        match e {
            CommitError::Disoriented => AppendError::Disoriented,
            CommitError::InvalidEntryId(_) => unreachable!(),
            CommitError::ShutDown => AppendError::ShutDown,
        }
    }
}