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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
use ansi_term::Colour;
use crate::pgmap::PGMap;
use crate::osdmap::OsdMap;
use crate::pgstate::RmSafety;
use crate::error::CSDError;
use crate::from::FromCeph;

use std::collections::BinaryHeap;
use std::fmt;

// Format for printing
#[derive(Clone, Copy, Debug)]
pub enum Format {
    Pretty,
    Json,
}

// The removability status of an OSD. Using an enum for precedence:
// Safe < Unknown < NonSafe
#[derive(Serialize, Debug, Copy, Clone, Ord, Eq, PartialEq, PartialOrd)]
pub enum Status {
    Safe,
    Unknown,
    NonSafe,
}

impl fmt::Display for Status {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Status::Unknown => write!(f, "Pending"),
            Status::Safe => write!(f, "Removable"),
            Status::NonSafe => write!(f, "Not removable"),
        }
    }
}

pub struct PgDiag {
    osd_id: i32,
    pg_info: PgInfo,
}

impl PgDiag {
    fn new(osd_id: i32, pg_info: PgInfo) -> PgDiag {
        PgDiag { osd_id, pg_info }
    }
}

// Holds information about a PG's status, it's ID and state
#[derive(Debug, Clone, Ord, Eq, PartialEq, PartialOrd)]
pub struct PgInfo {
    pg_id: String,
    pg_state: String,
    rm_safety: RmSafety,
}

impl PgInfo {
    fn new(states: &str, pgid: String) -> PgInfo {
        PgInfo {
            pg_id: pgid,
            pg_state: states.to_string(),
            rm_safety: RmSafety::new(&states),
        }
    }
}

#[derive(Debug, Serialize)]
pub struct OsdDiag {
    osd_id: i32,
    osd_status: BinaryHeap<Status>,
}

impl OsdDiag {
    fn new(osd_id: i32) -> OsdDiag {
        OsdDiag {
            osd_id,
            osd_status: BinaryHeap::new(),
        }
    }
}

// Used to print ClusterDiag in a nicer way. Since ClusterDiag.osd_diags use
// binary heaps to order status priority then it is very inconvenient for
// printing as JSON
#[derive(Serialize, Default)]
pub struct ClusterReview {
    #[serde(rename = "Removable")]
    removable: Vec<i32>,
    #[serde(rename = "Not Removable")]
    not_removable: Vec<i32>,
    #[serde(rename = "Pending")]
    pending: Vec<i32>,
}

impl ClusterReview {
    fn from_diag(cluster_diag: &ClusterDiag) -> ClusterReview {
        let mut review: ClusterReview = Default::default();
        for osd in &cluster_diag.osd_diags {
            if let Some(osd_status) = osd.osd_status.peek() {
                match *osd_status {
                    Status::NonSafe => review.not_removable.push(osd.osd_id),
                    Status::Safe => review.removable.push(osd.osd_id),
                    Status::Unknown => review.pending.push(osd.osd_id),
                }
            }
        }
        review
    }
}

#[derive(Debug, Serialize)]
pub struct ClusterDiag {
    status: Status,
    osd_diags: Vec<OsdDiag>,
}

impl ClusterDiag {
    fn new() -> ClusterDiag {
        ClusterDiag {
            status: Status::Safe,
            osd_diags: Vec::new(),
        }
    }

    fn print(&mut self, format: Format) {
        match format {
            Format::Pretty => self.print_pretty(),
            Format::Json => self.print_json(),
        };
    }

    fn status(&mut self) -> Status {
        for osd in &self.osd_diags {
            if let Some(osd_status) = osd.osd_status.peek() {
                // ClusterDiag.status defaults to safe and is only changed once
                // an OSD that is unsafe to remove or pending is found
                match *osd_status {
                    // Short circuit if we find non safe status
                    Status::NonSafe => return Status::NonSafe,
                    Status::Unknown => return Status::Unknown,
                    _ => (),
                };
            }
        }
        // Edge case where no osds are found
        if self.osd_diags.is_empty() {
            return Status::NonSafe;
        }
        self.status
    }

    fn print_pretty(&self) {
        println!("Current OSD statuses:");
        for osd in &self.osd_diags {
            if let Some(osd_status) = osd.osd_status.peek() {
                match *osd_status {
                    Status::NonSafe => println!(
                        "{} {}: {}",
                        Colour::Red.paint("●"),
                        osd.osd_id,
                        osd_status
                    ),
                    Status::Safe => println!(
                        "{} {}: {}",
                        Colour::Green.paint("●"),
                        osd.osd_id,
                        osd_status
                    ),
                    Status::Unknown => println!(
                        "{} {}: {}",
                        Colour::Yellow.paint("●"),
                        osd.osd_id,
                        osd_status
                    ),
                }
            }
        }
    }

    fn print_json(&self) {
        if let Ok(json) = serde_json::to_string(&ClusterReview::from_diag(&self)) {
            println!("{}", json);
        }
    }
}

#[derive(Debug, Clone)]
pub struct DiagMap {
    pg_map: PGMap,
    osd_map: OsdMap,
}

impl DiagMap {
    pub fn new() -> Result<DiagMap, CSDError> {
        Ok(DiagMap {
            pg_map: PGMap::from_ceph("pg dump")?,
            osd_map: OsdMap::from_ceph("osd dump")?,
        })
    }

    // Quick check to see if `min_size +1` is satisfied
    pub fn quick_diag(self, format: Format) -> bool {
        let mut safe: bool = false;
        for stat in self.pg_map.pg_stats {
            for pool in self.osd_map.pools.iter() {
                if (stat.up.len() as i32) >= (pool.min_size + 1) {
                    safe = true;
                }
            }
        }
        match format {
            Format::Pretty => {
                if safe {
                    println!("{} Safe to remove an OSD", Colour::Green.paint("●"));
                } else {
                    println!("{} Not safe to remove an OSD", Colour::Red.paint("●"));
                };
            }
            Format::Json => println!("{{\"Safe to remove an OSD\":{}}}", safe),
        };
        safe
    }

    // Maps out PGs and their states to each OSD in their `acting` list.
    // Returns a more general `Status` based on whether there is a removable
    // OSD or not.
    // `cluster_diag` holds an OSD's removability status. Using a binary heap we
    // can always know which state it has that holds the highest precedent.
    pub fn exhaustive_diag(self, format: Format) -> Status {
        let mut pg_diags: Vec<PgDiag> = Vec::new();
        let mut cluster_diag = ClusterDiag::new();

        // Populate PG statuses. For each PG we push it's list of acting OSDs
        // and the state of the PG
        for pg_stat in self.pg_map.pg_stats {
            for acting in pg_stat.acting {
                pg_diags.push(PgDiag::new(
                    acting,
                    PgInfo::new(&pg_stat.state, pg_stat.pgid.clone()),
                ));
            }
        }

        // Generate OSD removability.
        for pg in &pg_diags {
            if cluster_diag
                .osd_diags
                .iter_mut()
                .find(|ref osd| osd.osd_id == pg.osd_id)
                .is_none()
            {
                cluster_diag.osd_diags.push(OsdDiag::new(pg.osd_id));
            } else if let Some(osd) = cluster_diag
                .osd_diags
                .iter_mut()
                .find(|ref osd| osd.osd_id == pg.osd_id)
            {
                match pg.pg_info.rm_safety {
                    RmSafety::None => osd.osd_status.push(Status::NonSafe),
                    RmSafety::Pending => osd.osd_status.push(Status::Unknown),
                    RmSafety::Total => osd.osd_status.push(Status::Safe),
                }
            }
        }

        // Print the statuses of OSDs based on `format`
        cluster_diag.print(format);
        cluster_diag.status()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::from::FromFile;
    use crate::osdmap::OsdMap;
    use crate::pgmap::PGMap;

    #[test]
    fn quick_diag_jewel_safe() {
        let status = DiagMap {
            pg_map: PGMap::from_file("test/jewel/pg_dump_safe.json").unwrap(),
            osd_map: OsdMap::from_file("test/jewel/osd_dump_safe.json").unwrap(),
        }.quick_diag(Format::Pretty);

        assert_eq!(status, true);
    }

    #[test]
    fn exhaustive_diag_jewel_safe() {
        let status: Status = DiagMap {
            pg_map: PGMap::from_file("test/jewel/pg_dump_safe.json").unwrap(),
            osd_map: OsdMap::from_file("test/jewel/osd_dump_safe.json").unwrap(),
        }.exhaustive_diag(Format::Json);

        assert_eq!(status, Status::Safe);
    }

    #[test]
    fn exhaustive_diag_jewel_non_safe() {
        let status: Status = DiagMap {
            pg_map: PGMap::from_file("test/jewel/pg_dump_non_safe.json").unwrap(),
            osd_map: OsdMap::from_file("test/jewel/osd_dump_non_safe.json").unwrap(),
        }.exhaustive_diag(Format::Pretty);

        assert_eq!(status, Status::NonSafe);
    }

    #[test]
    fn exhaustive_diag_luminous_safe() {
        let status: Status = DiagMap {
            pg_map: PGMap::from_file("test/jewel/pg_dump_safe.json").unwrap(),
            osd_map: OsdMap::from_file("test/jewel/osd_dump_safe.json").unwrap(),
        }.exhaustive_diag(Format::Json);

        assert_eq!(status, Status::Safe);
    }

    #[test]
    fn exhaustive_diag_luminous_non_safe() {
        let status: Status = DiagMap {
            pg_map: PGMap::from_file("test/luminous/pg_dump_non_safe.json").unwrap(),
            osd_map: OsdMap::from_file("test/luminous/osd_dump_non_safe.json").unwrap(),
        }.exhaustive_diag(Format::Pretty);

        assert_eq!(status, Status::NonSafe);
    }

    #[test]
    fn exhaustive_diag_jewel_pending() {
        let status: Status = DiagMap {
            pg_map: PGMap::from_file("test/jewel/pg_dump_pending.json").unwrap(),
            osd_map: OsdMap::from_file("test/jewel/osd_dump_pending.json").unwrap(),
        }.exhaustive_diag(Format::Json);

        assert_eq!(status, Status::Unknown);
    }

    #[test]
    fn quick_diag_firefly_safe() {
        let status = DiagMap {
            pg_map: PGMap::from_file("test/firefly/pg_dump_safe.json").unwrap(),
            osd_map: OsdMap::from_file("test/firefly/osd_dump_safe.json").unwrap(),
        }.quick_diag(Format::Json);

        assert_eq!(status, true);
    }

    #[test]
    fn exhaustive_diag_firefly_safe() {
        let status: Status = DiagMap {
            pg_map: PGMap::from_file("test/firefly/pg_dump_safe.json").unwrap(),
            osd_map: OsdMap::from_file("test/firefly/osd_dump_safe.json").unwrap(),
        }.exhaustive_diag(Format::Pretty);

        assert_eq!(status, Status::Safe);
    }

}