Skip to content
35 changes: 22 additions & 13 deletions oxa/oxa-work/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::path::Path;
use std::time::{Duration, Instant};

mod landing_history;
pub mod publication;
use landing_history::{AdmissionEvidence, Admissions};

pub const INTERVAL: Duration = Duration::from_secs(300);
Expand Down Expand Up @@ -69,6 +70,8 @@ struct Sample<'a> {
landing_main: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
app_quota: Option<AppQuota>,
#[serde(skip_serializing_if = "Option::is_none")]
landing_publication: Option<&'a publication::Publication>,
}

pub(crate) const OWNER_FILE_FLAGS: libc::c_int = libc::O_NOFOLLOW | libc::O_NONBLOCK | libc::O_CLOEXEC;
Expand Down Expand Up @@ -397,7 +400,7 @@ pub fn run_with_landing(
&queue_rejections,
&matched_queue_admissions,
);
let mut sample = Sample {
let sample = Sample {
schema_version: 1,
observed_at: board.observed_at,
would_take: board.entries.iter().filter(|entry| entry.eligible).collect(),
Expand All @@ -408,19 +411,9 @@ pub fn run_with_landing(
landing: &landing,
landing_main: landing_main.as_deref(),
app_quota,
landing_publication: None,
};
let mut line = serde_json::to_vec(&sample).map_err(|_| "cannot encode shadow board")?;
if sample.app_quota.is_some() && line.len() as u64 > MAX_LEAD_SAMPLE_BYTES {
sample.app_quota = None;
line = serde_json::to_vec(&sample).map_err(|_| "cannot encode shadow board")?;
}
line.push(b'\n');
let length = log.metadata().map_err(|_| "cannot inspect shadow log")?.len();
if length.saturating_add(line.len() as u64) > MAX_LOG_BYTES {
return Err("shadow log reached its size limit; rotate it while stopped".into());
}
log.write_all(&line).map_err(|_| "cannot append shadow board")?;
log.sync_data().map_err(|_| "cannot sync shadow board")?;
append(log, sample)?;
if let Some(history) = &mut history {
history.observe(
&board,
Expand Down Expand Up @@ -500,3 +493,19 @@ fn plan_matched_rejections(
}
}
}

fn append(log: &mut File, mut sample: Sample<'_>) -> Result<()> {
let mut line = serde_json::to_vec(&sample).map_err(|_| "cannot encode shadow board")?;
if sample.app_quota.is_some() && line.len() as u64 > MAX_LEAD_SAMPLE_BYTES {
sample.app_quota = None;
line = serde_json::to_vec(&sample).map_err(|_| "cannot encode shadow board")?;
}
line.push(b'\n');
let length = log.metadata().map_err(|_| "cannot inspect shadow log")?.len();
if length.saturating_add(line.len() as u64) > MAX_LOG_BYTES {
return Err("shadow log reached its size limit; rotate it while stopped".into());
}
log.write_all(&line).map_err(|_| "cannot append shadow board")?;
log.sync_data().map_err(|_| "cannot sync shadow board")?;
Ok(())
}
10 changes: 9 additions & 1 deletion oxa/oxa-work/src/shadow/landing_history.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Correlate a verified rejection with an earlier owner-observed queue admission.
use super::{Board, File, QueueObservation, Read, Seek, SeekFrom, MAX_LOG_BYTES};
use super::{
publication::{Publication, Publications},
Board, File, QueueObservation, Read, Seek, SeekFrom, MAX_LOG_BYTES,
};
use crate::board::queue::{is_object_id, Removal};
use crate::Result;
use serde::{Deserialize, Serialize};
Expand All @@ -18,6 +21,7 @@ struct PriorSample {
#[serde(default)]
queue_rejections: Vec<PriorRejection>,
landing_main: Option<String>,
landing_publication: Option<Publication>,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -68,6 +72,7 @@ impl AdmissionEvidence {

#[derive(Default)]
pub(super) struct Admissions {
pub(super) publications: Publications,
queued: BTreeMap<u64, Admission>,
matched: BTreeMap<u64, AdmissionEvidence>,
}
Expand Down Expand Up @@ -97,6 +102,9 @@ impl Admissions {
if prior.schema_version != 1 || prior.observed_at != prior.board.observed_at {
return Err("invalid owner shadow journal sample".into());
}
if let Some(publication) = prior.landing_publication {
history.publications.observe(publication)?;
}
if prior.landing.is_empty() {
continue;
}
Expand Down
Loading
Loading