awa 0.6.2

Postgres-native background job queue — transactional enqueue, heartbeat crash recovery, SKIP LOCKED dispatch
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
//! Failure-mode benchmark tests for Awa.
//!
//! These benchmarks measure throughput, drain time, and recovery behaviour
//! when a configurable percentage of jobs fail, retry, hang, or trigger
//! callback timeouts.
//!
//! Run with:
//! ```
//! DATABASE_URL=postgres://postgres:test@localhost:15432/awa_test \
//!   cargo test --package awa --test failure_benchmark_test -- --ignored --nocapture
//! ```
//!
//! Each test emits human-readable summaries and one @@BENCH_JSON@@ JSONL
//! record per scenario using the shared schema (schema_version=1).

mod bench_output;

use async_trait::async_trait;
use awa::model::migrations;
use awa::{Client, JobArgs, JobContext, JobError, JobResult, QueueConfig, Worker};
use bench_output::{BenchMetrics, BenchRescue, BenchThroughput, BenchmarkResult, SCHEMA_VERSION};
use opentelemetry_sdk::metrics::data::{AggregatedMetrics, MetricData};
use opentelemetry_sdk::metrics::{InMemoryMetricExporter, SdkMeterProvider};
use serde::{Deserialize, Serialize};
use sqlx::postgres::PgPoolOptions;
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

fn database_url() -> String {
    std::env::var("DATABASE_URL")
        .unwrap_or_else(|_| "postgres://postgres:test@localhost:15432/awa_test".to_string())
}

async fn setup(max_conns: u32) -> sqlx::PgPool {
    let pool = PgPoolOptions::new()
        .max_connections(max_conns)
        .connect(&database_url())
        .await
        .expect("Failed to connect to database");
    migrations::run(&pool).await.expect("Failed to migrate");
    pool
}

async fn reset_runtime_state(pool: &sqlx::PgPool) {
    sqlx::query(
        "TRUNCATE awa.jobs_hot, awa.scheduled_jobs, awa.queue_meta, awa.job_unique_claims RESTART IDENTITY CASCADE",
    )
    .execute(pool)
    .await
    .expect("Failed to reset runtime state");
}

async fn queue_state_counts(pool: &sqlx::PgPool, queue: &str) -> HashMap<String, i64> {
    // Query both hot and scheduled tables to capture the full picture —
    // retried jobs may temporarily live in scheduled_jobs before promotion.
    let rows: Vec<(String, i64)> = sqlx::query_as(
        r#"
        SELECT state, sum(cnt)::bigint
        FROM (
            SELECT state::text AS state, count(*) AS cnt
            FROM awa.jobs_hot WHERE queue = $1 GROUP BY state
            UNION ALL
            SELECT state::text AS state, count(*) AS cnt
            FROM awa.scheduled_jobs WHERE queue = $1 GROUP BY state
        ) combined
        GROUP BY state
        "#,
    )
    .bind(queue)
    .fetch_all(pool)
    .await
    .expect("Failed to query state counts");
    rows.into_iter().collect()
}

fn sum_counter_metric(
    resource_metrics: &[opentelemetry_sdk::metrics::data::ResourceMetrics],
    name: &str,
) -> u64 {
    let mut total = 0;
    for rm in resource_metrics {
        for scope_metrics in rm.scope_metrics() {
            for metric in scope_metrics.metrics() {
                if metric.name() == name {
                    if let AggregatedMetrics::U64(MetricData::Sum(sum)) = metric.data() {
                        total += sum.data_points().map(|dp| dp.value()).sum::<u64>();
                    }
                }
            }
        }
    }
    total
}

fn sum_counter_metric_with_attribute(
    resource_metrics: &[opentelemetry_sdk::metrics::data::ResourceMetrics],
    name: &str,
    attr_name: &str,
    attr_value: &str,
) -> u64 {
    let mut total = 0;
    for rm in resource_metrics {
        for scope_metrics in rm.scope_metrics() {
            for metric in scope_metrics.metrics() {
                if metric.name() != name {
                    continue;
                }
                if let AggregatedMetrics::U64(MetricData::Sum(sum)) = metric.data() {
                    total += sum
                        .data_points()
                        .filter(|dp| {
                            dp.attributes().any(|kv| {
                                kv.key.as_str() == attr_name && kv.value.as_str() == attr_value
                            })
                        })
                        .map(|dp| dp.value())
                        .sum::<u64>();
                }
            }
        }
    }
    total
}

// ─── Job types ───────────────────────────────────────────────────────

#[derive(Debug, Serialize, Deserialize, JobArgs)]
struct FailureBenchJob {
    seq: i64,
    mode: String,
}

/// Worker that handles failure benchmark jobs deterministically.
struct FailureBenchWorker {
    handler_count: Arc<AtomicU64>,
}

#[async_trait]
impl Worker for FailureBenchWorker {
    fn kind(&self) -> &'static str {
        "failure_bench_job"
    }

    async fn perform(&self, ctx: &JobContext) -> Result<JobResult, JobError> {
        self.handler_count.fetch_add(1, Ordering::Relaxed);

        let args: FailureBenchJob = serde_json::from_value(ctx.job.args.clone())
            .map_err(|err| JobError::terminal(format!("failed to decode args: {err}")))?;

        match args.mode.as_str() {
            "complete" => Ok(JobResult::Completed),
            "terminal" => Err(JobError::terminal("intentional benchmark failure")),
            "retryable" => {
                if ctx.job.attempt == 1 {
                    Ok(JobResult::RetryAfter(Duration::from_millis(50)))
                } else {
                    Ok(JobResult::Completed)
                }
            }
            "callback_timeout" => {
                if ctx.job.attempt == 1 {
                    let callback = ctx
                        .register_callback(Duration::from_millis(300))
                        .await
                        .map_err(JobError::retryable)?;
                    Ok(JobResult::WaitForCallback(callback))
                } else {
                    Ok(JobResult::Completed)
                }
            }
            "deadline_hang" => {
                if ctx.job.attempt == 1 {
                    // Set a very short deadline so rescue fires quickly
                    sqlx::query(
                        "UPDATE awa.jobs_hot SET deadline_at = now() + make_interval(secs => $2) WHERE id = $1 AND run_lease = $3",
                    )
                    .bind(ctx.job.id)
                    .bind(0.2_f64)
                    .bind(ctx.job.run_lease)
                    .execute(ctx.pool())
                    .await
                    .map_err(JobError::retryable)?;

                    // Spin until cancelled
                    for _ in 0..200 {
                        if ctx.is_cancelled() {
                            break;
                        }
                        tokio::time::sleep(Duration::from_millis(25)).await;
                    }
                    Ok(JobResult::RetryAfter(Duration::from_millis(50)))
                } else {
                    Ok(JobResult::Completed)
                }
            }
            "snooze_once" => {
                // Snooze decrements attempt, so we can't use attempt to detect
                // "first time". Instead check if metadata has a snooze marker.
                let already_snoozed = ctx.job.metadata.get("snoozed").is_some();
                if !already_snoozed {
                    // Set the marker via a direct SQL update before snoozing
                    sqlx::query(
                        "UPDATE awa.jobs_hot SET metadata = metadata || '{\"snoozed\":true}'::jsonb WHERE id = $1 AND run_lease = $2",
                    )
                    .bind(ctx.job.id)
                    .bind(ctx.job.run_lease)
                    .execute(ctx.pool())
                    .await
                    .map_err(JobError::retryable)?;
                    Ok(JobResult::Snooze(Duration::from_millis(100)))
                } else {
                    Ok(JobResult::Completed)
                }
            }
            other => Err(JobError::terminal(format!(
                "unknown benchmark mode: {other}"
            ))),
        }
    }
}

// ─── Scenario runner ─────────────────────────────────────────────────

struct ScenarioConfig {
    name: String,
    total_jobs: i64,
    /// Maps mode -> count
    mode_distribution: Vec<(String, i64)>,
    max_workers: u32,
}

async fn run_scenario(pool: &sqlx::PgPool, config: &ScenarioConfig) {
    let queue = format!("bench_fail_{}", config.name);
    reset_runtime_state(pool).await;

    // Seed jobs per mode
    let mut seq: i64 = 0;
    for (mode, count) in &config.mode_distribution {
        if *count == 0 {
            continue;
        }
        sqlx::query(
            r#"
            INSERT INTO awa.jobs_hot
                (kind, queue, args, state, priority, max_attempts, run_at, metadata, tags)
            SELECT
                'failure_bench_job',
                $1,
                jsonb_build_object('seq', $2 + g, 'mode', $3),
                'available'::awa.job_state,
                2,
                5,
                now(),
                '{}'::jsonb,
                '{}'::text[]
            FROM generate_series(1, $4) AS g
            "#,
        )
        .bind(&queue)
        .bind(seq)
        .bind(mode)
        .bind(*count)
        .execute(pool)
        .await
        .unwrap_or_else(|e| panic!("Failed to seed {mode} jobs: {e}"));
        seq += count;
    }

    // Set up metrics
    let exporter = InMemoryMetricExporter::default();
    let meter_provider = SdkMeterProvider::builder()
        .with_periodic_exporter(exporter.clone())
        .build();
    opentelemetry::global::set_meter_provider(meter_provider.clone());

    let handler_count = Arc::new(AtomicU64::new(0));
    let client = Client::builder(pool.clone())
        .queue(
            &queue,
            QueueConfig {
                max_workers: config.max_workers,
                poll_interval: Duration::from_millis(25),
                ..QueueConfig::default()
            },
        )
        .heartbeat_interval(Duration::from_millis(50))
        .deadline_rescue_interval(Duration::from_millis(100))
        .callback_rescue_interval(Duration::from_millis(100))
        .promote_interval(Duration::from_millis(50))
        .leader_election_interval(Duration::from_millis(100))
        .register_worker(FailureBenchWorker {
            handler_count: handler_count.clone(),
        })
        .build()
        .expect("Failed to build client");

    let started = Instant::now();
    client.start().await.expect("Failed to start client");

    // Wait for all jobs to reach terminal states
    // Scenarios with deadline_hang or callback_timeout need extra time for
    // rescue cycles; 180s is generous enough for the full mixed matrix.
    let timeout = Duration::from_secs(180);
    let deadline = Instant::now() + timeout;
    loop {
        let counts = queue_state_counts(pool, &queue).await;
        let in_flight = counts.get("available").copied().unwrap_or(0)
            + counts.get("running").copied().unwrap_or(0)
            + counts.get("retryable").copied().unwrap_or(0)
            + counts.get("scheduled").copied().unwrap_or(0)
            + counts.get("waiting_external").copied().unwrap_or(0);
        if in_flight == 0 {
            break;
        }
        assert!(
            Instant::now() < deadline,
            "Timed out waiting for {} jobs to settle. In-flight: {in_flight}. Counts: {counts:?}",
            config.name
        );
        tokio::time::sleep(Duration::from_millis(200)).await;
    }

    let drain_time = started.elapsed();
    client.shutdown(Duration::from_secs(5)).await;

    // Collect metrics
    meter_provider
        .force_flush()
        .expect("Failed to flush metrics");
    let resource_metrics = exporter
        .get_finished_metrics()
        .expect("Failed to get finished metrics");

    let final_counts = queue_state_counts(pool, &queue).await;
    let completed = final_counts.get("completed").copied().unwrap_or(0) as u64;
    let failed = final_counts.get("failed").copied().unwrap_or(0) as u64;
    let cancelled = final_counts.get("cancelled").copied().unwrap_or(0) as u64;
    let handler_total = handler_count.load(Ordering::Relaxed);
    let finalized_total = completed + failed + cancelled;

    let handler_per_s = handler_total as f64 / drain_time.as_secs_f64();
    let db_per_s = finalized_total as f64 / drain_time.as_secs_f64();

    let rescues = sum_counter_metric(&resource_metrics, "awa.maintenance.rescues");
    let callback_timeouts = sum_counter_metric(&resource_metrics, "awa.job.waiting_external");
    let deadline_rescued = sum_counter_metric_with_attribute(
        &resource_metrics,
        "awa.maintenance.rescues",
        "awa.rescue.kind",
        "deadline",
    );

    // Human-readable output
    println!(
        "[failure-bench] scenario={} total={} drain={:.2}s handler={:.0}/s db_finalized={:.0}/s completed={} failed={} cancelled={} rescues={}",
        config.name, config.total_jobs, drain_time.as_secs_f64(),
        handler_per_s, db_per_s,
        completed, failed, cancelled, rescues
    );

    // JSONL output
    let mut outcomes: HashMap<String, u64> = HashMap::new();
    for (state, count) in &final_counts {
        if *count > 0 {
            outcomes.insert(state.clone(), *count as u64);
        }
    }

    let rescue_metrics = if rescues > 0 || callback_timeouts > 0 || deadline_rescued > 0 {
        Some(BenchRescue {
            deadline_rescued: if deadline_rescued > 0 {
                Some(deadline_rescued)
            } else {
                None
            },
            callback_timeouts: if callback_timeouts > 0 {
                Some(callback_timeouts)
            } else {
                None
            },
            heartbeat_rescued: None,
        })
    } else {
        None
    };

    BenchmarkResult {
        schema_version: SCHEMA_VERSION,
        scenario: config.name.clone(),
        language: "rust".to_string(),
        seeded: config.total_jobs as u64,
        metrics: BenchMetrics {
            throughput: Some(BenchThroughput {
                handler_per_s,
                db_finalized_per_s: db_per_s,
            }),
            enqueue_per_s: None,
            drain_time_s: Some(drain_time.as_secs_f64()),
            latency_ms: None,
            rescue: rescue_metrics,
        },
        outcomes,
        metadata: Some(serde_json::json!({
            "max_workers": config.max_workers,
            "mode_distribution": config.mode_distribution
                .iter()
                .map(|(m, c)| format!("{m}:{c}"))
                .collect::<Vec<_>>(),
        })),
    }
    .emit();

    let _ = meter_provider.shutdown();
}

fn scenario(
    name: &str,
    total: i64,
    failure_pct: i64,
    mode: &str,
    max_workers: u32,
) -> ScenarioConfig {
    let failure_count = total * failure_pct / 100;
    let success_count = total - failure_count;
    ScenarioConfig {
        name: name.to_string(),
        total_jobs: total,
        mode_distribution: vec![
            ("complete".to_string(), success_count),
            (mode.to_string(), failure_count),
        ],
        max_workers,
    }
}

fn mixed_scenario(name: &str, total: i64, max_workers: u32) -> ScenarioConfig {
    // Split evenly: 50% success, then equal parts of each failure mode
    let success_count = total / 2;
    let failure_count = total - success_count;
    let per_mode = failure_count / 5;
    let remainder = failure_count - per_mode * 5;
    ScenarioConfig {
        name: name.to_string(),
        total_jobs: total,
        mode_distribution: vec![
            ("complete".to_string(), success_count + remainder),
            ("terminal".to_string(), per_mode),
            ("retryable".to_string(), per_mode),
            ("callback_timeout".to_string(), per_mode),
            ("deadline_hang".to_string(), per_mode),
            ("snooze_once".to_string(), per_mode),
        ],
        max_workers,
    }
}

// ═══════════════════════════════════════════════════════════════════════
// Individual benchmark tests
// ═══════════════════════════════════════════════════════════════════════

const TOTAL: i64 = 5_000;
const WORKERS: u32 = 64;

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore]
async fn test_failure_bench_terminal_1pct() {
    let pool = setup(20).await;
    run_scenario(
        &pool,
        &scenario("terminal_1pct", TOTAL, 1, "terminal", WORKERS),
    )
    .await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore]
async fn test_failure_bench_terminal_10pct() {
    let pool = setup(20).await;
    run_scenario(
        &pool,
        &scenario("terminal_10pct", TOTAL, 10, "terminal", WORKERS),
    )
    .await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore]
async fn test_failure_bench_terminal_50pct() {
    let pool = setup(20).await;
    run_scenario(
        &pool,
        &scenario("terminal_50pct", TOTAL, 50, "terminal", WORKERS),
    )
    .await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore]
async fn test_failure_bench_retryable_1pct() {
    let pool = setup(20).await;
    run_scenario(
        &pool,
        &scenario("retryable_1pct", TOTAL, 1, "retryable", WORKERS),
    )
    .await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore]
async fn test_failure_bench_retryable_10pct() {
    let pool = setup(20).await;
    run_scenario(
        &pool,
        &scenario("retryable_10pct", TOTAL, 10, "retryable", WORKERS),
    )
    .await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore]
async fn test_failure_bench_retryable_50pct() {
    let pool = setup(20).await;
    run_scenario(
        &pool,
        &scenario("retryable_50pct", TOTAL, 50, "retryable", WORKERS),
    )
    .await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore]
async fn test_failure_bench_callback_timeout_10pct() {
    let pool = setup(20).await;
    run_scenario(
        &pool,
        &scenario(
            "callback_timeout_10pct",
            TOTAL,
            10,
            "callback_timeout",
            WORKERS,
        ),
    )
    .await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore]
async fn test_failure_bench_deadline_hang_10pct() {
    let pool = setup(20).await;
    run_scenario(
        &pool,
        &scenario("deadline_hang_10pct", TOTAL, 10, "deadline_hang", WORKERS),
    )
    .await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore]
async fn test_failure_bench_snooze_once_10pct() {
    let pool = setup(20).await;
    run_scenario(
        &pool,
        &scenario("snooze_once_10pct", TOTAL, 10, "snooze_once", WORKERS),
    )
    .await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore]
async fn test_failure_bench_mixed() {
    let pool = setup(20).await;
    run_scenario(&pool, &mixed_scenario("mixed_all_modes", TOTAL, WORKERS)).await;
}

/// Run the full failure benchmark matrix in one go.
/// Useful for local comparison runs.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore]
async fn test_failure_bench_full_matrix() {
    let pool = setup(20).await;

    let scenarios = vec![
        scenario("terminal_1pct", TOTAL, 1, "terminal", WORKERS),
        scenario("terminal_10pct", TOTAL, 10, "terminal", WORKERS),
        scenario("terminal_50pct", TOTAL, 50, "terminal", WORKERS),
        scenario("retryable_1pct", TOTAL, 1, "retryable", WORKERS),
        scenario("retryable_10pct", TOTAL, 10, "retryable", WORKERS),
        scenario("retryable_50pct", TOTAL, 50, "retryable", WORKERS),
        scenario(
            "callback_timeout_10pct",
            TOTAL,
            10,
            "callback_timeout",
            WORKERS,
        ),
        scenario("deadline_hang_10pct", TOTAL, 10, "deadline_hang", WORKERS),
        scenario("snooze_once_10pct", TOTAL, 10, "snooze_once", WORKERS),
        mixed_scenario("mixed_all_modes", TOTAL, WORKERS),
    ];

    for s in &scenarios {
        run_scenario(&pool, s).await;
    }
}

// ═══════════════════════════════════════════════════════════════════════
// Stale-heartbeat rescue benchmark
//
// Seeds N jobs directly in "running" state with backdated heartbeat_at,
// then starts a client with aggressive heartbeat rescue intervals and
// measures how quickly all jobs are rescued and re-completed.
//
// This simulates the scenario where a worker node dies without draining.
// No subprocess management needed — we manipulate the DB directly.
// ═══════════════════════════════════════════════════════════════════════

/// No-op worker that completes immediately — used for rescued jobs on attempt 2+.
struct RescueBenchWorker {
    handler_count: Arc<AtomicU64>,
}

#[async_trait]
impl Worker for RescueBenchWorker {
    fn kind(&self) -> &'static str {
        "rescue_bench_job"
    }

    async fn perform(&self, _ctx: &JobContext) -> Result<JobResult, JobError> {
        self.handler_count.fetch_add(1, Ordering::Relaxed);
        Ok(JobResult::Completed)
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore]
async fn test_failure_bench_stale_heartbeat_rescue() {
    let pool = setup(20).await;
    let queue = "bench_stale_rescue";
    reset_runtime_state(&pool).await;

    let total_stale: i64 = 500;

    // Seed jobs directly in "running" state with a stale heartbeat.
    // run_lease is set to a value that won't match any real worker, ensuring
    // these look like orphaned jobs from a dead node.
    sqlx::query(
        r#"
        INSERT INTO awa.jobs_hot
            (kind, queue, args, state, priority, max_attempts, attempt,
             run_at, heartbeat_at, attempted_at, run_lease, metadata, tags)
        SELECT
            'rescue_bench_job',
            $1,
            jsonb_build_object('seq', g),
            'running'::awa.job_state,
            2,
            5,
            1,
            now() - interval '1 minute',
            now() - interval '10 minutes',
            now() - interval '1 minute',
            -1,
            '{}'::jsonb,
            '{}'::text[]
        FROM generate_series(1, $2) AS g
        "#,
    )
    .bind(queue)
    .bind(total_stale)
    .execute(&pool)
    .await
    .expect("Failed to seed stale running jobs");

    // Verify they're seeded correctly
    let running: i64 = sqlx::query_scalar(
        "SELECT count(*) FROM awa.jobs_hot WHERE queue = $1 AND state = 'running'",
    )
    .bind(queue)
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(running, total_stale, "All seeded jobs should be running");

    let exporter = InMemoryMetricExporter::default();
    let meter_provider = SdkMeterProvider::builder()
        .with_periodic_exporter(exporter.clone())
        .build();
    opentelemetry::global::set_meter_provider(meter_provider.clone());

    let handler_count = Arc::new(AtomicU64::new(0));
    let client = Client::builder(pool.clone())
        .queue(
            queue,
            QueueConfig {
                max_workers: 64,
                poll_interval: Duration::from_millis(25),
                ..QueueConfig::default()
            },
        )
        // Aggressive rescue intervals for benchmark
        .heartbeat_interval(Duration::from_millis(50))
        .heartbeat_rescue_interval(Duration::from_millis(100))
        .leader_election_interval(Duration::from_millis(100))
        .leader_check_interval(Duration::from_millis(100))
        .register_worker(RescueBenchWorker {
            handler_count: handler_count.clone(),
        })
        .build()
        .expect("Failed to build rescue benchmark client");

    let started = Instant::now();
    client.start().await.expect("Failed to start client");

    // Wait for all jobs to reach completed
    let timeout = Duration::from_secs(60);
    let deadline = Instant::now() + timeout;
    loop {
        let counts = queue_state_counts(&pool, queue).await;
        let completed = counts.get("completed").copied().unwrap_or(0);
        let still_running = counts.get("running").copied().unwrap_or(0);
        let retryable = counts.get("retryable").copied().unwrap_or(0);
        if completed == total_stale {
            break;
        }
        assert!(
            Instant::now() < deadline,
            "Timed out: completed={completed} running={still_running} retryable={retryable}"
        );
        tokio::time::sleep(Duration::from_millis(100)).await;
    }

    let rescue_time = started.elapsed();
    client.shutdown(Duration::from_secs(5)).await;

    meter_provider
        .force_flush()
        .expect("Failed to flush metrics");
    let resource_metrics = exporter
        .get_finished_metrics()
        .expect("Failed to get metrics");

    let rescues = sum_counter_metric(&resource_metrics, "awa.maintenance.rescues");
    let handler_total = handler_count.load(Ordering::Relaxed);

    println!(
        "[stale-rescue] stale_jobs={} rescue_time={:.2}s rescued={} handler_completed={} rate={:.0}/s",
        total_stale,
        rescue_time.as_secs_f64(),
        rescues,
        handler_total,
        total_stale as f64 / rescue_time.as_secs_f64()
    );

    let mut outcomes: HashMap<String, u64> = HashMap::new();
    outcomes.insert("completed".to_string(), total_stale as u64);

    BenchmarkResult {
        schema_version: SCHEMA_VERSION,
        scenario: "stale_heartbeat_rescue".to_string(),
        language: "rust".to_string(),
        seeded: total_stale as u64,
        metrics: BenchMetrics {
            throughput: Some(BenchThroughput {
                handler_per_s: handler_total as f64 / rescue_time.as_secs_f64(),
                db_finalized_per_s: total_stale as f64 / rescue_time.as_secs_f64(),
            }),
            enqueue_per_s: None,
            drain_time_s: Some(rescue_time.as_secs_f64()),
            latency_ms: None,
            rescue: Some(BenchRescue {
                heartbeat_rescued: Some(rescues),
                deadline_rescued: None,
                callback_timeouts: None,
            }),
        },
        outcomes,
        metadata: Some(serde_json::json!({
            "stale_age_minutes": 10,
            "rescue_interval_ms": 100,
        })),
    }
    .emit();

    let _ = meter_provider.shutdown();
}