use uuid::Uuid;
pub const MIGRATING: Uuid = Uuid::from_u64_pair(0, 0);
pub const UNASSIGNED: Uuid = Uuid::from_u64_pair(0, 1);
pub const LOST: Uuid = Uuid::from_u64_pair(0, 2);
#[must_use]
pub fn reserved(dir: Uuid) -> bool {
let (most, least) = dir.as_u64_pair();
most == 0 && least < 100
}
#[must_use]
pub fn is_online(dir: Uuid, online_dirs: &[Uuid]) -> bool {
if dir == UNASSIGNED || dir == MIGRATING {
return true;
}
if dir == LOST {
return false;
}
online_dirs.is_empty() || online_dirs.contains(&dir)
}
#[cfg(test)]
mod tests {
use assert2::check;
use super::*;
#[test]
fn sentinels_match_kafka() {
check!(
[MIGRATING, UNASSIGNED, LOST] == [Uuid::nil(), Uuid::from_u128(1), Uuid::from_u128(2)]
);
}
#[test]
fn reserved_is_the_first_hundred_uuids() {
for (case, dir, want) in [
("MIGRATING", MIGRATING, true),
("UNASSIGNED", UNASSIGNED, true),
("LOST", LOST, true),
("last reserved", Uuid::from_u128(99), true),
("first unreserved", Uuid::from_u128(100), false),
("high bits set", Uuid::from_u64_pair(1, 0), false),
] {
check!(reserved(dir) == want, "{case}");
}
}
#[test]
fn is_online_matches_kafka() {
let (a, b) = (Uuid::from_u128(0xA00), Uuid::from_u128(0xB00));
for (case, dir, online, want) in [
("UNASSIGNED", UNASSIGNED, &[a][..], true),
("MIGRATING", MIGRATING, &[a][..], true),
("LOST", LOST, &[a][..], false),
("LOST with no dirs", LOST, &[][..], false),
("listed dir", a, &[a, b][..], true),
("unlisted dir", b, &[a][..], false),
("no dirs registered", b, &[][..], true),
] {
check!(is_online(dir, online) == want, "{case}");
}
}
}