Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions crates/twigpine-node/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,6 @@ fn observe_duration(histogram: &OnceLock<Histogram>, duration: Duration) {
}
}

/// Test-only: current `gitlawb_sync_lag_seconds` sample count (0 if the
/// registry is not initialized). Lets the sync lag-gate tests assert that a
/// skipped observation really never reached the histogram.
#[cfg(test)]
pub fn sync_lag_sample_count_for_test() -> u64 {
SYNC_LAG.get().map(|h| h.get_sample_count()).unwrap_or(0)
}

/// Test-only: current `gitlawb_fetches_total` value for `repo` (0 if the registry
/// is not initialized). Lets api-layer tests assert the completed-fetch count with
/// a unique label instead of scraping the encoded text.
Expand Down Expand Up @@ -623,9 +615,6 @@ mod tests {
.inc();
observe_push_duration(Duration::from_millis(42));
observe_fetch_duration(Duration::from_millis(24));
// `observe_sync_lag` is deliberately NOT called. The sync lag-gate
// test asserts a before/after delta on the process-wide histogram, so
// a sample from this test running concurrently would flake it.
record_reconciliation_gaps_found(7);
record_reconciliation_gaps_filled(3);

Expand Down Expand Up @@ -695,9 +684,7 @@ mod tests {
// here. It is a label-less process-wide counter and the seen-set test
// that asserts it asserts a before/after delta, so an increment from a
// second test running concurrently in this binary would be a flake with
// no diagnostic value. `observe_sync_lag` is withheld for the same
// reason: the sync lag-gate test asserts a sample-count delta on the
// process-wide histogram.
// no diagnostic value.
record_gossip_ingest("accepted");
observe_pack_size(1024.0);
observe_push_duration(Duration::from_secs(1));
Expand Down
46 changes: 23 additions & 23 deletions crates/twigpine-node/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,13 @@ async fn process_batch(
.is_repo_quarantined(&registration_id)
.await
.unwrap_or(true);
observe_sync_lag_if_outcome_visible(&local_path, &item, withheld).await;
observe_sync_lag_if_outcome_visible(
&local_path,
&item,
withheld,
crate::metrics::observe_sync_lag,
)
.await;
// Option B2: carry the encrypted withheld-blob envelopes too, so an
// authorized reader can recover private content from this mirror if
// the origin dies. `registration_id` is the id the mirror
Expand Down Expand Up @@ -1315,6 +1321,7 @@ async fn observe_sync_lag_if_outcome_visible(
local_path: &Path,
item: &crate::db::SyncQueueItem,
withheld: bool,
observe: impl FnOnce(Duration),
) {
if withheld {
return;
Expand All @@ -1331,7 +1338,7 @@ async fn observe_sync_lag_if_outcome_visible(
}
if let Some(lag) = sync_lag_from_origin_timestamp(item.origin_timestamp.as_deref(), Utc::now())
{
crate::metrics::observe_sync_lag(lag);
observe(lag);
}
}

Expand Down Expand Up @@ -1961,6 +1968,7 @@ async fn fetch_repo(local_path: &Path, net: &NetworkGit, mode: MirrorMode) -> an
mod tests {
use super::*;
use sqlx::PgPool;
use std::cell::Cell;
use std::process::Command;
use std::sync::atomic::{AtomicUsize, Ordering};
use tempfile::TempDir;
Expand Down Expand Up @@ -2594,63 +2602,55 @@ mod tests {
/// arm and sampled a deletion nobody proved.
#[tokio::test]
async fn lag_gate_skips_the_sample_when_the_tip_read_fails() {
crate::metrics::init("0.0.0-test", "did:key:test");
let ts = (Utc::now() - Duration::from_secs(60)).to_rfc3339();
let item = lag_item("refs/heads/main", &"0".repeat(40), Some(&ts));

let before = crate::metrics::sync_lag_sample_count_for_test();
observe_sync_lag_if_outcome_visible(
Path::new("/nonexistent/gitlawb-tip-probe-xyz"),
&item,
false,
|_| panic!("a failed tip read must not emit a lag sample for a delete row"),
)
.await;
assert_eq!(
crate::metrics::sync_lag_sample_count_for_test(),
before,
"a failed tip read must not emit a lag sample for a delete row"
);
}

/// The live controls around the skip: a confirmed-absent ref on a delete
/// row does sample (the skip above is not vacuously green), while a
/// still-resolving ref and a hidden or unregistered mirror do not.
#[tokio::test]
async fn lag_gate_samples_only_when_the_queued_outcome_is_proven() {
crate::metrics::init("0.0.0-test", "did:key:test");
let (td, _url) = bare_remote(&[("a.txt", b"hi\n")]);
let repo = td.path().join("bare.git");
let ts = (Utc::now() - Duration::from_secs(60)).to_rfc3339();
let mut count = crate::metrics::sync_lag_sample_count_for_test();
let count = Cell::new(0);
let observe = |_| count.set(count.get() + 1);

// Confirmed absent on a delete-sentinel row: samples.
let gone = lag_item("refs/heads/never-pushed", &"0".repeat(40), Some(&ts));
observe_sync_lag_if_outcome_visible(&repo, &gone, false).await;
count += 1;
assert_eq!(crate::metrics::sync_lag_sample_count_for_test(), count);
observe_sync_lag_if_outcome_visible(&repo, &gone, false, observe).await;
assert_eq!(count.get(), 1);

// The ref still resolves, so the mirror carries a different state than
// the queued deletion: no sample.
let still_there = lag_item("HEAD", &"0".repeat(40), Some(&ts));
observe_sync_lag_if_outcome_visible(&repo, &still_there, false).await;
assert_eq!(crate::metrics::sync_lag_sample_count_for_test(), count);
observe_sync_lag_if_outcome_visible(&repo, &still_there, false, observe).await;
assert_eq!(count.get(), 1);

// A non-delete row samples only when the tip equals the queued sha.
let tip = match mirror_ref_tip(&repo, "HEAD").await {
TipProbe::Tip(sha) => sha,
other => panic!("expected a resolved tip, got {other:?}"),
};
let landed = lag_item("HEAD", &tip, Some(&ts));
observe_sync_lag_if_outcome_visible(&repo, &landed, false).await;
count += 1;
assert_eq!(crate::metrics::sync_lag_sample_count_for_test(), count);
observe_sync_lag_if_outcome_visible(&repo, &landed, false, observe).await;
assert_eq!(count.get(), 2);
let wrong_sha = lag_item("HEAD", &"f".repeat(40), Some(&ts));
observe_sync_lag_if_outcome_visible(&repo, &wrong_sha, false).await;
assert_eq!(crate::metrics::sync_lag_sample_count_for_test(), count);
observe_sync_lag_if_outcome_visible(&repo, &wrong_sha, false, observe).await;
assert_eq!(count.get(), 2);

// A quarantined mirror never samples.
observe_sync_lag_if_outcome_visible(&repo, &gone, true).await;
assert_eq!(crate::metrics::sync_lag_sample_count_for_test(), count);
observe_sync_lag_if_outcome_visible(&repo, &gone, true, observe).await;
assert_eq!(count.get(), 2);
}

fn dir_entries(dir: &Path) -> Vec<String> {
Expand Down