use super::log::{self, Level};
use super::slots::{SLOTS, SlotSet};
use super::topology::Cluster;
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Listing {
Show,
Hide,
}
pub(crate) fn run(c: &mut Cluster) -> bool {
run_with(c, Listing::Show)
}
pub(crate) fn run_with(c: &mut Cluster, listing: Listing) -> bool {
count_errors(c, listing) == 0
}
pub(crate) fn count_errors(c: &mut Cluster, listing: Listing) -> usize {
header(c, listing);
let mut errors = usize::from(!agreement(c));
errors += usize::from(!open_slots(c).is_empty());
errors += usize::from(!coverage(c));
if c.cfg.search_multiple_owners {
errors += usize::from(!super::owners::report(c).is_empty());
}
errors
}
pub(crate) fn header(c: &Cluster, listing: Listing) {
let color = c.cfg.color;
let entry = c.nodes.first().map(|n| n.shown()).unwrap_or_default();
log::line(
color,
Level::Info,
&[b">>> Performing Cluster Check (using node ", &entry[..], b")"].concat(),
);
if listing == Listing::Show {
for n in &c.nodes {
super::show::node(n);
}
}
}
pub(crate) fn agreement(c: &Cluster) -> bool {
let first = c.nodes.first().map(|n| &n.signature);
let agree = c.nodes.iter().all(|n| Some(&n.signature) == first);
if agree {
log::line(c.cfg.color, Level::Ok, b"[OK] All nodes agree about slots configuration.");
} else {
log::line(c.cfg.color, Level::Err, b"[ERR] Nodes don't agree about configuration!");
}
agree
}
pub(crate) fn open_slots(c: &Cluster) -> Vec<u16> {
let color = c.cfg.color;
log::line(color, Level::Info, b">>> Check for open slots...");
let mut open: Vec<u16> = Vec::new();
for n in &c.nodes {
for (state, list) in
[(&b"migrating"[..], &n.rec.migrating), (b"importing", &n.rec.importing)]
{
if list.is_empty() {
continue;
}
let slots = joined(list.iter().map(|(s, _)| *s));
let text = [
b"[WARNING] Node ",
&n.shown()[..],
b" has slots in ",
state,
b" state ",
&slots,
b".",
];
log::line(color, Level::Err, &text.concat());
for &(slot, _) in list.iter() {
if !open.contains(&slot) {
open.push(slot);
}
}
}
}
if open.is_empty() {
return open;
}
open.sort_unstable();
let text =
[&b"[WARNING] The following slots are open: "[..], &joined(open.iter().copied()), b"."];
log::line(color, Level::Err, &text.concat());
open
}
pub(crate) fn coverage(c: &Cluster) -> bool {
let color = c.cfg.color;
log::line(color, Level::Info, b">>> Check slots coverage...");
let covered = covered(c).count();
if covered == SLOTS {
log::line(color, Level::Ok, format!("[OK] All {SLOTS} slots covered.").as_bytes());
return true;
}
log::line(
color,
Level::Err,
format!("[ERR] Not all {SLOTS} slots are covered by nodes.\n").as_bytes(),
);
false
}
pub(crate) fn covered(c: &Cluster) -> SlotSet {
let mut all = SlotSet::default();
for n in &c.nodes {
for s in n.rec.slots.iter() {
all.insert(s);
}
}
all
}
pub(crate) fn joined(slots: impl Iterator<Item = u16>) -> Vec<u8> {
slots.map(|s| s.to_string()).collect::<Vec<_>>().join(",").into_bytes()
}