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
6 changes: 6 additions & 0 deletions rust/src/runtime/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,7 @@ where
}
let mut task: Value = serde_json::from_str(&DB::string(&row, "value")?)?;
if task["query_task_id"] != task_id || !self.query_matches_poll(&task, queue, &types)
|| !Self::group_policies_supported(&mut *read, &worker, task["run_id"].as_str().unwrap_or_default()).await?
|| self.query_has_live_lease(&mut *read, &task, config).await?
|| Self::query_hold(&mut *read, &task).await?.is_some()
{
Expand Down Expand Up @@ -1434,6 +1435,11 @@ where
if !self.query_matches_poll(&task, queue, &types) {
continue;
}
// A query replays the run's history: the worker must register
// every explicit parallel group policy it records (.github#232).
if !Self::group_policies_supported(&mut **tx, &worker, task["run_id"].as_str().unwrap_or_default()).await? {
continue;
}
if self.query_has_live_lease(&mut **tx, &task, config).await? {
let existing_owner = task["lease_owner"].as_str().unwrap_or("");
// A live lease belongs to the poll request that claimed it. As in
Expand Down
28 changes: 23 additions & 5 deletions rust/src/runtime/update_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,20 @@ where
return Ok(Some(("update_validator_worker_incompatible",
"Active workers do not advertise a validator-capable implementation matching this workflow run.", true)));
}
Ok(None)
// .github#232: a validator replays the run, so it must also register
// every explicit parallel group policy the run's history records.
let mut connection = self.pool.acquire().await?;
for row in &rows {
let worker = self.registration(row)?;
if worker.fresh && worker.capable
&& worker.serves(request.workflow_type, request.name, request.compatibility, request.fingerprint)
&& Self::group_policies_supported(&mut *connection, row, request.run_id).await?
{
return Ok(None);
}
}
Ok(Some(("update_validator_parallel_group_policy_unsupported",
"Active validator workers do not register every parallel group policy this workflow run's history records.", true)))
}

/// `findOrCreateTask`: one task per run, update name and request id.
Expand Down Expand Up @@ -535,7 +548,9 @@ where
let worker = self.registration(&row)?;
if worker.fresh && worker.capable {
for task in self.validation_claim_candidates(&mut read, queue, now(), None).await? {
if !Self::validator_matches_task(&worker, &task)? {
if !Self::validator_matches_task(&worker, &task)?
|| !Self::group_policies_supported(&mut *read, &row, &DB::string(&task, "workflow_run_id")?).await?
{
continue;
}
let observed = DB::number(&task, "attempt_count")?;
Expand All @@ -561,23 +576,26 @@ where
if tx.claim_only.as_ref().is_some_and(|target| target.kind != "update_validation") {
return Ok(None);
}
let Some(worker) = Self::query("SELECT * FROM workflow_worker_registrations WHERE worker_id=$1 AND namespace=$2")
let Some(row) = Self::query("SELECT * FROM workflow_worker_registrations WHERE worker_id=$1 AND namespace=$2")
.bind(worker_id)
.bind(self.namespace.as_ref())
.fetch_optional(&mut **tx)
.await?
else {
return Ok(None);
};
let worker = self.registration(&worker)?;
let worker = self.registration(&row)?;
if !worker.fresh || !worker.capable {
return Ok(None);
}
let at = now();
let target = tx.claim_only.clone();
let tasks = self.validation_claim_candidates(tx, task_queue, at, target.as_ref().map(|scope| scope.task_id.as_str())).await?;
for task in tasks {
if !Self::validator_matches_task(&worker, &task)? {
// A validator replays the run (.github#232 policy eligibility).
if !Self::validator_matches_task(&worker, &task)?
|| !Self::group_policies_supported(&mut **tx, &row, &DB::string(&task, "workflow_run_id")?).await?
{
continue;
}
let attempt = DB::number(&task, "attempt_count")?;
Expand Down
2 changes: 2 additions & 0 deletions rust/tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ mod signal_claims;
mod parallel_cancellation;
#[path = "execution/parallel_group_policies.rs"]
mod parallel_group_policies;
#[path = "execution/parallel_group_replay_tasks.rs"]
mod parallel_group_replay_tasks;
#[path = "execution/runtime_credentials.rs"]
mod runtime_credentials;
#[path = "execution/selected_run_signals.rs"]
Expand Down
116 changes: 116 additions & 0 deletions rust/tests/execution/parallel_group_replay_tasks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//! server#1382 (.github#232): query and update-validation tasks replay the
//! run's history, so they take the same parallel group policy eligibility as
//! workflow tasks. A worker without a recorded policy never receives them.
use super::*;

const FLAG: &str = "parallel_group_policies";

fn int_contract(name: &str) -> Value {
json!({"name": name, "parameters": [{"name": "value", "position": 0, "type": "int", "required": true, "variadic": false,
"default_available": false, "allows_null": false}]})
}

/// A worker on `test` that answers queries and validates `approve_me`, with
/// or without the policy descriptor.
async fn register_replayer(app: &Router, worker: &str, policies: Option<&[&str]>) {
let entry = json!({"supported": false, "minimum_protocol_version": "1.18"});
let mut manifest = json!({"local_activities": entry, "worker_sessions": entry, "sticky_execution": entry});
let mut capabilities = vec!["query_tasks", "workflow_updates", "update_validation_tasks"];
if let Some(policies) = policies {
capabilities.push(FLAG);
manifest[FLAG] = json!({"supported": true, "version": 1, "policies": policies});
}
let contract = json!({"queries": ["state"], "query_contracts": [int_contract("state")], "signals": [], "signal_contracts": [],
"updates": ["approve_me"], "update_contracts": [int_contract("approve_me")], "update_validators": ["approve_me"]});
let (status, body) = request_protocol(app, "POST", "/api/worker/register", json!({"worker_id": worker, "task_queue": "test",
"runtime": "python", "supported_workflow_types": ["echo"], "supported_activity_types": ["echo_activity"],
"capabilities": capabilities, "capability_manifest": manifest, "workflow_command_contracts": {"echo": contract}}), "1.20").await;
assert_eq!(status, StatusCode::CREATED, "{body}");
}

/// A run whose first task admitted an explicit `propagate` group. Its
/// members stay open, so no workflow task holds a query, and the author is
/// then silent.
async fn explicit_run(database: &TestDatabase, app: &Router, workflow: &str) -> Value {
register_replayer(app, "author", Some(&["propagate"])).await;
let run = start(app, workflow).await;
let task = poll(app, "author", "workflow").await;
let commands = json!((0..2).map(|index| {
let frame = json!({"parallel_group_id": "parallel-activities:1:2", "parallel_group_kind": "activity",
"parallel_group_base_sequence": 1, "parallel_group_size": 2, "parallel_group_index": index,
"parallel_group_policy": "propagate"});
let mut command = json!({"type": "schedule_activity", "activity_type": "echo_activity", "arguments": envelope(Payload::Array(vec![]))});
for (field, value) in frame.as_object().unwrap() {
command[field] = value.clone();
}
command["parallel_group_path"] = json!([frame]);
command
}).collect::<Vec<_>>());
let admitted = finish_task(app, &task, commands).await;
assert_eq!(admitted.0, StatusCode::OK, "{}", admitted.1);
database.execute("UPDATE workflow_worker_registrations SET last_heartbeat_at='2000-01-01 00:00:00' WHERE worker_id='author'").await;
run
}

#[tokio::test]
async fn a_legacy_worker_never_receives_an_explicit_runs_query() {
let database = TestDatabase::new().await;
let runtime = database.open().await.unwrap();
let app = router(runtime.clone());
let run = explicit_run(&database, &app, "policy-query").await;
register_replayer(&app, "legacy", None).await;
register_replayer(&app, "capable", Some(&["propagate"])).await;
let pending = {
let app = app.clone();
let path = format!("/api/workflows/policy-query/runs/{}/query/state", run["run_id"].as_str().unwrap());
tokio::spawn(async move { request(&app, "POST", &path, json!({"input": envelope(Payload::Array(vec![Payload::Long(1)]))})).await })
};
// The legacy worker keeps polling and never gets it.
let deadline = tokio::time::Instant::now() + std::time::Duration::from_millis(800);
while tokio::time::Instant::now() < deadline {
assert!(poll(&app, "legacy", "query").await.is_null(), "a legacy worker cannot replay the explicit run");
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
}
let claim = await_query(&app, "capable").await;
assert_eq!(claim["run_id"], run["run_id"], "{claim}");
pending.abort();
runtime.close().await;
database.remove().await;
}

#[tokio::test]
async fn a_legacy_validator_never_receives_an_explicit_runs_update_validation() {
let database = TestDatabase::new().await;
let runtime = database.open().await.unwrap();
let app = router(runtime.clone());
let run = explicit_run(&database, &app, "policy-update").await;
register_replayer(&app, "legacy", None).await;
let body = |request_id: &str| json!({"input": envelope(Payload::Array(vec![Payload::Long(1)])), "request_id": request_id});
// With only a legacy validator, the update is refused at once with a
// stable reason, and nothing is recorded.
let before = run_history(&app, &run["workflow_id"], &run["run_id"]).await;
let (status, refused) = request(&app, "POST", "/api/workflows/policy-update/update/approve_me", body("u1")).await;
assert_eq!(status, StatusCode::CONFLICT, "{refused}");
assert_eq!(refused["reason"], "update_validator_parallel_group_policy_unsupported", "{refused}");
assert_eq!(run_history(&app, &run["workflow_id"], &run["run_id"]).await, before);
// A capable validator arrives: the task goes to it and never to the
// legacy validator.
register_replayer(&app, "capable", Some(&["propagate"])).await;
let pending = {
let app = app.clone();
tokio::spawn(async move { request(&app, "POST", "/api/workflows/policy-update/update/approve_me", body("u2")).await })
};
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
let (status, legacy) = request(&app, "POST", "/api/worker/update-validation-tasks/poll",
json!({"worker_id": "legacy", "task_queue": "test", "timeout_seconds": 0})).await;
assert_eq!(status, StatusCode::OK, "{legacy}");
assert!(legacy["task"].is_null(), "{legacy}");
let (status, capable) = request(&app, "POST", "/api/worker/update-validation-tasks/poll",
json!({"worker_id": "capable", "task_queue": "test", "timeout_seconds": 5})).await;
assert_eq!(status, StatusCode::OK, "{capable}");
assert_eq!(capable["poll_status"], "leased", "{capable}");
assert_eq!(capable["task"]["workflow_run_id"].as_str().or(capable["task"]["run_id"].as_str()), run["run_id"].as_str(), "{capable}");
pending.abort();
runtime.close().await;
database.remove().await;
}