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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::warehouse_metadata::_helpers::ingestion::{
};
use crate::warehouse_metadata::models::{MetadataBatch, StateEventImport, WarehouseSyncResponse};

/// Mirror a state-history page; a source id keeps its earliest row (its latest for kinds whose `mirror_keeps_latest` holds), then its smallest record text in byte order. SQLite and PostgreSQL format that text differently, so on an exact tie each store is deterministic but they may keep different rows; a store migration rebuilds the mirror.
pub(crate) async fn ingest_events(
store: &ControlStore,
(location_id, target): (Uuid, &str),
Expand All @@ -33,6 +34,16 @@ pub(crate) async fn ingest_events(
batch.kind.as_str()
))
})?;
let record_text = if matches!(store, ControlStore::Postgres(_)) {
"record_json::text COLLATE \"C\""
} else {
"record_json"
};
let wins = if batch.kind.mirror_keeps_latest() {
">"
} else {
"<"
};
let mut transaction = begin_ingestion(
store,
(location_id, target),
Expand Down Expand Up @@ -78,9 +89,16 @@ pub(crate) async fn ingest_events(
Parameter::Time(transaction.now),
]);
let placeholders = Parameter::placeholders(1..=parameters.len());
let replaced: Vec<String> = columns[3..]
.iter()
.map(|column| format!("{column}=excluded.{column}"))
.collect();
let sql = format!(
"INSERT INTO {table} ({}) VALUES ({placeholders}) ON CONFLICT (event_id) DO NOTHING RETURNING event_id",
columns.join(",")
"INSERT INTO {table} ({}) VALUES ({placeholders}) ON CONFLICT (event_id) DO UPDATE SET {} \
WHERE excluded.observed_at{wins}{table}.observed_at OR (excluded.observed_at={table}.observed_at \
AND excluded.{record_text}<{table}.{record_text}) RETURNING event_id",
columns.join(","),
replaced.join(",")
);
let inserted;
(transaction, inserted) = query(transaction, &sql, parameters).await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,12 @@ impl WarehouseHistoryKind {
pub const fn mirror_table(self) -> &'static str {
self.names().1
}

/// Two builds refreshing one run's cost estimate at once write the same source id and the warehouse's latest row is current, so cost mirrors keep the latest row; every other kind keeps the earliest.
#[must_use]
pub const fn mirror_keeps_latest(self) -> bool {
matches!(self, Self::ResourceCosts)
}
}

/// Derive progress from committed mirror rows, scoped to the active physical reader.
Expand Down
34 changes: 33 additions & 1 deletion crates/sqlbuild-orchestration/tests/api/test_types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use crate::runs::models::RunStatus;
use crate::store::errors::ControlError;
use crate::store::models::ControlStore;
use crate::store::tests::store_test_types::Backend;
use crate::warehouse_metadata::models::WarehouseHistoryKind;
use crate::warehouse_metadata::models::{
MetadataBatch, WarehouseHistoryKind, WarehouseSyncResponse,
};
use chrono::{DateTime, Utc};
use futures_util::future::BoxFuture;
use uuid::Uuid;

pub(crate) struct ApiContractTestCase {
pub(crate) description: &'static str,
Expand Down Expand Up @@ -116,6 +122,32 @@ pub(crate) struct WarehouseMetadataTestCase {
pub(crate) expected_observations: usize,
}

pub(crate) struct MirrorConflictTestCase {
pub(crate) description: &'static str,
pub(crate) backend: Backend,
pub(crate) kind: WarehouseHistoryKind,
pub(crate) fixture: &'static str,
/// The record's source id, time and value fields.
pub(crate) fields: [&'static str; 3],
pub(crate) ingest: MirrorIngest,
/// Two rows of one source id as (time, value), each delivered in its own page.
pub(crate) rows: [(&'static str, &'static str); 2],
pub(crate) expected_row: (&'static str, &'static str),
/// The kept value in its typed mirror column, for kinds that have one.
pub(crate) expected_column: Option<&'static str>,
}

/// A kept mirror row as (source id, record time, record value, column time, typed column value).
pub(crate) type KeptMirrorRow = (String, DateTime<Utc>, String, DateTime<Utc>, Option<String>);

/// Mirrors one page of a history kind.
pub(crate) type MirrorIngest =
for<'a> fn(
&'a ControlStore,
(Uuid, &'a str),
&'a MetadataBatch,
) -> BoxFuture<'a, Result<WarehouseSyncResponse, ControlError>>;

pub(crate) struct PasswordApiTestCase {
pub(crate) description: &'static str,
pub(crate) backend: Backend,
Expand Down
210 changes: 208 additions & 2 deletions crates/sqlbuild-orchestration/tests/api/test_warehouse_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
//! A scoped HTTP reader mirrors immutable warehouse observations, including external runs.

use chrono::{Duration, Utc};
use chrono::{DateTime, Duration, Utc};
use reqwest::StatusCode;
use serde_json::json;
use sqlbuild_core::constants::STATE_TABLE_NAMES;
use uuid::Uuid;

use crate::api::tests::changes_helpers::{connect, expect_topics};
use crate::api::tests::helpers::{start_api, stop_api};
use crate::api::tests::test_types::{StateTableClassificationTestCase, WarehouseMetadataTestCase};
use crate::api::tests::test_types::{
KeptMirrorRow, MirrorConflictTestCase, StateTableClassificationTestCase,
WarehouseMetadataTestCase,
};
use crate::api::tests::warehouse_history_helpers::{
audit_page, batch, cost_page, fixture, seed_target,
};
use crate::credentials::main::tokens::issue_token::issue_token;
use crate::credentials::models::{AuditContext, CredentialScope};
use crate::credentials::tests::location_helpers::create_location;
Expand Down Expand Up @@ -444,3 +450,203 @@ fn given_warehouse_observations_when_pages_retry_then_scope_history_and_atomicit
});
}
}

#[test]
fn given_one_source_id_in_two_pages_when_mirrored_in_either_order_then_the_same_row_is_kept() {
let test_cases = [
MirrorConflictTestCase {
description: "SQLite audit results: page two's earlier row arrives second",
backend: Backend::Sqlite,
kind: WarehouseHistoryKind::AuditResults,
fixture: "audit_result_history.json",
fields: ["result_id", "occurred_at", "outcome"],
ingest: audit_page,
rows: [
("2026-02-03T04:05:06.000001Z", "pass"),
("2026-02-03T04:05:06Z", "pass"),
],
expected_row: ("2026-02-03T04:05:06Z", "pass"),
expected_column: Some("pass"),
},
MirrorConflictTestCase {
description: "SQLite audit results: two rows at the same time",
backend: Backend::Sqlite,
kind: WarehouseHistoryKind::AuditResults,
fixture: "audit_result_history.json",
fields: ["result_id", "occurred_at", "outcome"],
ingest: audit_page,
rows: [
("2026-02-03T04:05:06Z", "pass"),
("2026-02-03T04:05:06Z", "error"),
],
expected_row: ("2026-02-03T04:05:06Z", "error"),
expected_column: Some("error"),
},
MirrorConflictTestCase {
description: "SQLite resource costs: a refreshed observation keeps the later row",
backend: Backend::Sqlite,
kind: WarehouseHistoryKind::ResourceCosts,
fixture: "resource_cost_history.json",
fields: ["observation_id", "observed_at", "status"],
ingest: cost_page,
rows: [
("2026-02-03T04:05:06Z", "partial"),
("2026-02-03T04:05:06.000001Z", "complete"),
],
expected_row: ("2026-02-03T04:05:06.000001Z", "complete"),
expected_column: None,
},
MirrorConflictTestCase {
description: "SQLite resource costs: two rows at the same time",
backend: Backend::Sqlite,
kind: WarehouseHistoryKind::ResourceCosts,
fixture: "resource_cost_history.json",
fields: ["observation_id", "observed_at", "status"],
ingest: cost_page,
rows: [
("2026-02-03T04:05:06Z", "partial"),
("2026-02-03T04:05:06Z", "complete"),
],
expected_row: ("2026-02-03T04:05:06Z", "complete"),
expected_column: None,
},
MirrorConflictTestCase {
description: "PostgreSQL audit results: page two's earlier row arrives second",
backend: Backend::Postgres,
kind: WarehouseHistoryKind::AuditResults,
fixture: "audit_result_history.json",
fields: ["result_id", "occurred_at", "outcome"],
ingest: audit_page,
rows: [
("2026-02-03T04:05:06.000001Z", "pass"),
("2026-02-03T04:05:06Z", "pass"),
],
expected_row: ("2026-02-03T04:05:06Z", "pass"),
expected_column: Some("pass"),
},
MirrorConflictTestCase {
description: "PostgreSQL audit results: two rows at the same time",
backend: Backend::Postgres,
kind: WarehouseHistoryKind::AuditResults,
fixture: "audit_result_history.json",
fields: ["result_id", "occurred_at", "outcome"],
ingest: audit_page,
rows: [
("2026-02-03T04:05:06Z", "pass"),
("2026-02-03T04:05:06Z", "error"),
],
expected_row: ("2026-02-03T04:05:06Z", "error"),
expected_column: Some("error"),
},
MirrorConflictTestCase {
description: "PostgreSQL resource costs: a refreshed observation keeps the later row",
backend: Backend::Postgres,
kind: WarehouseHistoryKind::ResourceCosts,
fixture: "resource_cost_history.json",
fields: ["observation_id", "observed_at", "status"],
ingest: cost_page,
rows: [
("2026-02-03T04:05:06Z", "partial"),
("2026-02-03T04:05:06.000001Z", "complete"),
],
expected_row: ("2026-02-03T04:05:06.000001Z", "complete"),
expected_column: None,
},
MirrorConflictTestCase {
description: "PostgreSQL resource costs: two rows at the same time",
backend: Backend::Postgres,
kind: WarehouseHistoryKind::ResourceCosts,
fixture: "resource_cost_history.json",
fields: ["observation_id", "observed_at", "status"],
ingest: cost_page,
rows: [
("2026-02-03T04:05:06Z", "partial"),
("2026-02-03T04:05:06Z", "complete"),
],
expected_row: ("2026-02-03T04:05:06Z", "complete"),
expected_column: None,
},
];
for test_case in test_cases {
run(async {
let Some(fixture_store) = open_test_store(test_case.backend).await else {
return;
};
let store = &fixture_store.store;
let (location, session_id) = seed_target(store).await;
let [id_field, time_field, value_field] = test_case.fields;
let base = fixture(test_case.fixture)["record"].clone();
let record = |id: &str, (time, value): (&str, &str)| {
let mut record = base.clone();
record[id_field] = json!(id);
record[time_field] = json!(time);
record[value_field] = json!(value);
record
};
let page = |record| {
let page = batch(session_id, test_case.kind.as_str(), vec![record]);
async move {
(test_case.ingest)(store, (location, "development"), &page)
.await
.expect("one page")
.applied
}
};
for (order, rows) in [
("arrival order", test_case.rows),
("reverse order", [test_case.rows[1], test_case.rows[0]]),
] {
for row in rows {
page(record(order, row)).await;
}
}
assert!(
!page(record("arrival order", test_case.expected_row)).await,
"{}: re-sending the kept row changes nothing",
test_case.description
);
let (transaction, rows) = query(
begin(store, TransactionMode::Read).await.expect("read"),
&format!("SELECT * FROM {}", test_case.kind.mirror_table()),
vec![],
)
.await
.expect("mirrored rows");
commit(transaction).await.expect("close read");
let mut kept: Vec<KeptMirrorRow> = rows
.iter()
.map(|row| {
let record = &row["record_json"];
(
record[id_field].as_str().expect("id").to_owned(),
record[time_field]
.as_str()
.expect("time")
.parse()
.expect("RFC 3339"),
record[value_field].as_str().expect("value").to_owned(),
serde_json::from_value(row["observed_at"].clone()).expect("column time"),
row.get(value_field)
.and_then(|value| value.as_str())
.map(str::to_owned),
)
})
.collect();
kept.sort();
let expected_at: DateTime<Utc> = test_case.expected_row.0.parse().expect("RFC 3339");
assert_eq!(
kept,
["arrival order", "reverse order"].map(|order| (
order.to_owned(),
expected_at,
test_case.expected_row.1.to_owned(),
expected_at,
test_case.expected_column.map(str::to_owned)
)),
"{}: the kind's winning time, then the smallest record, wins whichever page lands first",
test_case.description
);
close_test_store(fixture_store).await;
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A seeded target with scoped people, password sessions, fixtures and audit reads for warehouse history API tests.

use chrono::{Duration, Utc};
use futures_util::future::BoxFuture;
use reqwest::{Client, StatusCode, header};
use serde_json::{Value, json};
use uuid::Uuid;
Expand All @@ -23,11 +24,14 @@ use crate::platform::main::report_location_health::report_location_health;
use crate::platform::models::{LocationHealth, LocationHealthReport};
use crate::runs::tests::helpers::snapshot;
use crate::snapshots::main::publish_snapshot::publish_snapshot;
use crate::store::errors::ControlError;
use crate::store::main::{
begin::begin, commit::commit, migrate_store::migrate_store, query::query,
};
use crate::store::models::{ControlStore, TransactionMode};
use crate::warehouse_metadata::models::MetadataBatch;
use crate::warehouse_metadata::main::ingest::ingest_audit_results::ingest_audit_results;
use crate::warehouse_metadata::main::ingest::ingest_resource_costs::ingest_resource_costs;
use crate::warehouse_metadata::models::{MetadataBatch, WarehouseSyncResponse};

pub(crate) const ORIGIN: &str = "https://control.example.com";
pub(crate) const PASSWORD: &str = "orders-password-2026";
Expand Down Expand Up @@ -260,3 +264,21 @@ pub(crate) fn cost_groups(summary: &Value, field: &str) -> Vec<(Value, f64, Valu
})
.collect()
}

/// Mirrors one audit results page.
pub(crate) fn audit_page<'a>(
store: &'a ControlStore,
scope: (Uuid, &'a str),
page: &'a MetadataBatch,
) -> BoxFuture<'a, Result<WarehouseSyncResponse, ControlError>> {
Box::pin(ingest_audit_results(store, scope, page))
}

/// Mirrors one resource costs page.
pub(crate) fn cost_page<'a>(
store: &'a ControlStore,
scope: (Uuid, &'a str),
page: &'a MetadataBatch,
) -> BoxFuture<'a, Result<WarehouseSyncResponse, ControlError>> {
Box::pin(ingest_resource_costs(store, scope, page))
}
Loading
Loading