use super::v7::RepoStatus;
use crate::db::types::DbTid;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct RepoMetadata {
pub tracked: bool,
pub index_id: u64,
/// failed backfills in a row. it lives here and not on the resync entry
/// because requeueing a repo deletes that entry, and the backoff has to
/// keep growing across requeues
#[serde(default)]
pub retry_count: u32,
/// the status a relay listing made hydrant ask the pds about, synced when it
/// listed an inactive repo as active, so later listings with the same status
/// don't fetch it again
#[serde(default)]
pub relay_inactive: Option<RepoStatus>,
/// the rev of the last car a backfill wrote. a commit the relay validated before that
/// fetch can still reach the shard after the car, and has to leave its records alone
#[serde(default)]
pub backfilled_rev: Option<DbTid>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn v14_repo_metadata_golden_fixtures() {
let metadata = RepoMetadata {
tracked: true,
index_id: 7,
retry_count: 3,
relay_inactive: Some(RepoStatus::Takendown),
backfilled_rev: Some(DbTid::new_from_bytes(0x0102030405060708_u64.to_be_bytes())),
};
let encoded = rmp_serde::to_vec(&metadata).unwrap();
assert_eq!(
crate::util::hex::encode(&encoded),
"95c30703a954616b656e646f776ec4080102030405060708"
);
let decoded: RepoMetadata = rmp_serde::from_slice(&encoded).unwrap();
assert!(decoded.tracked);
assert_eq!(decoded.index_id, 7);
assert_eq!(decoded.retry_count, 3);
assert_eq!(decoded.relay_inactive, Some(RepoStatus::Takendown));
assert_eq!(decoded.backfilled_rev, metadata.backfilled_rev);
let healthy = rmp_serde::to_vec(&RepoMetadata {
relay_inactive: None,
retry_count: 0,
backfilled_rev: None,
..metadata
})
.unwrap();
assert_eq!(crate::util::hex::encode(&healthy), "95c30700c0c0");
}
#[test]
fn v4_repo_metadata_rows_decode_with_no_retries() {
let old = super::super::v4::RepoMetadata {
tracked: false,
index_id: 7,
};
let encoded = rmp_serde::to_vec(&old).unwrap();
assert_eq!(crate::util::hex::encode(&encoded), "92c207");
let decoded: RepoMetadata = rmp_serde::from_slice(&encoded).unwrap();
assert!(!decoded.tracked);
assert_eq!(decoded.index_id, 7);
assert_eq!(decoded.retry_count, 0);
assert_eq!(decoded.relay_inactive, None);
assert_eq!(decoded.backfilled_rev, None);
}
#[test]
fn v4_reader_rejects_v14_repo_metadata() {
let encoded = crate::util::hex::decode("95c30700c0c0").unwrap();
assert!(rmp_serde::from_slice::<super::super::v4::RepoMetadata>(&encoded).is_err());
}
}