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 @@ -39,12 +39,16 @@ pub(crate) async fn check_launch_claim(
Ok(transaction)
}

/// A retry matches on its stored `change`; derived fields such as resource columns vary by server version.
pub(crate) fn validate_retry(
previous: &Row,
request: &RunChangeRequest,
body: &serde_json::Value,
) -> Result<(), ControlError> {
if previous.get("payload") != Some(body)
let change =
|payload: Option<&serde_json::Value>| payload.and_then(|p| p.get("change")).cloned();
if change(previous.get("payload")).is_none()
|| change(previous.get("payload")) != change(Some(body))
|| previous
.get("invocation_id")
.and_then(serde_json::Value::as_str)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Publish facts atomically; launch observations never project state or release physical ownership.

use serde_json::json;
use serde_json::{Value, json};

use crate::platform::models::PlatformEvent;
use crate::runs::_helpers::lifecycle::events::append_run_event;
Expand All @@ -13,6 +13,20 @@ use crate::store::main::append_control_event::append_control_event;
use crate::store::main::query::query;
use crate::store::models::{ControlEvent, ControlTransaction, Parameter};

/// A change's event payload; node and check events also name their node for Recall's resource columns.
fn change_payload(request: &RunChangeRequest) -> Value {
let mut payload = json!({"change": request});
if let Some((kind, name)) = request
.change
.node_key()
.and_then(|key| key.split_once(':'))
{
payload["resource_kind"] = json!(kind);
payload["resource_name"] = json!(name);
}
payload
}

pub(crate) async fn publish_change(
transaction: ControlTransaction,
request: &RunChangeRequest,
Expand All @@ -22,7 +36,7 @@ pub(crate) async fn publish_change(
transaction,
(request.run_id, request.event_id),
request.change.event_type(),
json!({"change": request}),
change_payload(request),
)
.await?;
if let (RunChange::LaunchFailed { code }, Some(claim_id)) = (&request.change, request.claim_id)
Expand Down
11 changes: 11 additions & 0 deletions crates/sqlbuild-orchestration/src/runs/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,17 @@ impl RunChange {
}
}

/// The node or check the change is about, keyed `<kind>:<name>`.
pub(crate) fn node_key(&self) -> Option<&str> {
match self {
Self::NodeStarted { node_key }
| Self::NodeFinished { node_key, .. }
| Self::CheckStarted { node_key }
| Self::CheckFinished { node_key, .. } => Some(node_key),
_ => None,
}
}

pub(crate) fn finished_node(&self) -> Option<(&str, NodeOutcome)> {
match self {
Self::NodeFinished {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ use crate::store::main::{begin::begin, commit::commit};
use crate::store::models::{ControlStore, ControlTransaction, Parameter, TransactionMode};
use crate::store::types::Row;

/// Events whose node may be named only in their payload: older node and check events, and audit results.
const NODE_EVENTS: &str = "'run_node_started','run_node_finished','run_check_started','run_check_finished','audit_completed'";

/// The payload fields that name the node of an event in `NODE_EVENTS`, as `(column, JSON path)`.
const NODE_FIELDS: [(&str, &str); 5] = [
("node_key", "payload->'change'->'change'->>'node_key'"),
("attached_target_kind", "payload->>'attached_target_kind'"),
("attached_target_name", "payload->>'attached_target_name'"),
("attachment_kind", "payload->>'attachment_kind'"),
("target_name", "payload->>'target_name'"),
];

/// The Recall invocations of one run and the nodes it selected.
pub(crate) struct RunRecallScope {
namespaces: Vec<String>,
Expand Down Expand Up @@ -222,7 +234,7 @@ pub(crate) async fn run_events_in(
format!("SELECT storage_id, event_id, invocation_id, occurred_at, event_type, severity, producer, resource_id, \
resource_kind, resource_name, resource_attempt_id, statement_id, error_type, error_code, duration_ms, outcome, \
row_count, affected_rows, CASE WHEN event_type IN ('{MEMBER_RETRYING_EVENT}','{MEMBER_RECOVERED_EVENT}') \
THEN payload END AS member_payload FROM events WHERE {filter} AND storage_id>${next} ORDER BY storage_id LIMIT ${}", next + 1)
THEN payload END AS member_payload, {} FROM events WHERE {filter} AND storage_id>${next} ORDER BY storage_id LIMIT ${}", node_columns(), next + 1)
},
vec![
Parameter::Integer(after),
Expand All @@ -234,6 +246,14 @@ pub(crate) async fn run_events_in(
if let Some(message) = member_message(&payload)? {
row.insert("message".into(), Value::String(message));
}
let node = node_of(row);
for (column, _) in NODE_FIELDS {
row.remove(column);
}
if let Some((kind, name)) = node {
row.insert("resource_kind".into(), Value::String(kind));
row.insert("resource_name".into(), Value::String(name));
}
Ok(())
},
)
Expand Down Expand Up @@ -261,6 +281,35 @@ async fn recall_records<T: serde::de::DeserializeOwned>(
Ok((transaction, records))
}

/// Selects only the node's identity from the payload of an event without resource columns.
fn node_columns() -> String {
NODE_FIELDS
.iter()
.map(|(column, path)| {
format!("CASE WHEN resource_name IS NULL AND event_type IN ({NODE_EVENTS}) THEN {path} END AS {column}")
})
.collect::<Vec<_>>()
.join(", ")
}

/// The `(kind, name)` an event is about, from its `NODE_FIELDS`: a change's node key, else an audit's target.
pub(crate) fn node_of(row: &Row) -> Option<(String, String)> {
let text = |column: &str| {
row.get(column)
.and_then(Value::as_str)
.filter(|s| !s.is_empty())
};
if let Some(key) = text("node_key") {
let (kind, name) = key.split_once(':')?;
return Some((kind.into(), name.into()));
}
let (kind, name) = match (text("attached_target_kind"), text("attached_target_name")) {
(Some(kind), Some(name)) => (kind, name),
_ => (text("attachment_kind")?, text("target_name")?),
};
Some((kind.into(), name.into()))
}

/// The scheduler's summary of a member retry or recovery on its group run, from its own payload.
fn member_message(payload: &Value) -> Result<Option<String>, ControlError> {
let payload: Value = match payload {
Expand Down
2 changes: 2 additions & 0 deletions crates/sqlbuild-orchestration/tests/runs/test_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ mod test_lifecycle;
mod test_monitor;
#[path = "test_node_errors.rs"]
mod test_node_errors;
#[path = "test_node_event_resources.rs"]
mod test_node_event_resources;
#[path = "test_permissions.rs"]
mod test_permissions;
#[path = "test_project_limits.rs"]
Expand Down
Loading
Loading