-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(workflows): add worker instance failover
- Loading branch information
1 parent
6a0c6ba
commit 59f7c3a
Showing
14 changed files
with
243 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Errors | ||
|
||
Only errors from inside of activities will be retried. Errors thrown in the workflow body will not be retried because they will never succeed (the state is consistent up the point of error). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "workflow-gc" | ||
version = "0.0.1" | ||
edition = "2021" | ||
authors = ["Rivet Gaming, LLC <developer@rivet.gg>"] | ||
license = "Apache-2.0" | ||
|
||
[dependencies] | ||
chirp-client = { path = "../../../../../lib/chirp/client" } | ||
rivet-connection = { path = "../../../../../lib/connection" } | ||
rivet-health-checks = { path = "../../../../../lib/health-checks" } | ||
rivet-metrics = { path = "../../../../../lib/metrics" } | ||
rivet-operation = { path = "../../../../../lib/operation/core" } | ||
rivet-runtime = { path = "../../../../../lib/runtime" } | ||
tokio = { version = "1.29", features = ["full"] } | ||
tracing = "0.1" | ||
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt", "json", "ansi"] } | ||
|
||
[dev-dependencies] | ||
chirp-worker = { path = "../../../../../lib/chirp/worker" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[service] | ||
name = "workflow-gc" | ||
|
||
[runtime] | ||
kind = "rust" | ||
|
||
[headless] | ||
singleton = true | ||
|
||
[databases] | ||
db-workflow = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use std::{collections::HashSet, time::Duration}; | ||
|
||
use rivet_operation::prelude::*; | ||
|
||
const WORKER_INSTANCE_LOST_THRESHOLD: i64 = util::duration::seconds(30); | ||
|
||
#[tracing::instrument(skip_all)] | ||
pub async fn run_from_env(ts: i64, pools: rivet_pools::Pools) -> GlobalResult<()> { | ||
let client = chirp_client::SharedClient::from_env(pools.clone())?.wrap_new("workflow-gc"); | ||
let cache = rivet_cache::CacheInner::from_env(pools.clone())?; | ||
let ctx = OperationContext::new( | ||
"workflow-gc".into(), | ||
Duration::from_secs(60), | ||
rivet_connection::Connection::new(client, pools, cache), | ||
Uuid::new_v4(), | ||
Uuid::new_v4(), | ||
util::timestamp::now(), | ||
util::timestamp::now(), | ||
(), | ||
); | ||
|
||
// Reset all workflows on worker instances that have not had a ping in the last 30 seconds | ||
let rows = sql_fetch_all!( | ||
[ctx, (Uuid, Uuid,)] | ||
" | ||
UPDATE db_workflow.workflows AS w | ||
SET | ||
worker_instance_id = NULL, | ||
wake_immediate = true, | ||
wake_deadline_ts = NULL, | ||
wake_signals = ARRAY[], | ||
wake_sub_workflow_id = NULL | ||
FROM db_workflow.worker_instances AS wi | ||
WHERE | ||
wi.last_ping_ts < $1 AND | ||
wi.worker_instance_id = w.worker_instance_id AND | ||
w.output IS NULL | ||
RETURNING w.workflow_id, wi.worker_instance_id | ||
", | ||
ts - WORKER_INSTANCE_LOST_THRESHOLD, | ||
) | ||
.await?; | ||
|
||
if !rows.is_empty() { | ||
let unique_worker_instance_ids = rows | ||
.iter() | ||
.map(|(_, worker_instance_id)| worker_instance_id) | ||
.collect::<HashSet<_>>(); | ||
|
||
tracing::info!( | ||
worker_instance_ids=?unique_worker_instance_ids, | ||
total_workflows=%rows.len(), | ||
"handled failover", | ||
); | ||
} | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.