heliosdb-proxy 1.3.0

HeliosProxy - Intelligent connection router and failover manager for HeliosDB and PostgreSQL
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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
//! Zero-downtime PostgreSQL major-version upgrade orchestrator (T2.1).
//!
//! Coordinates a six-step upgrade workflow against a live cluster:
//!
//! 1. **Stand up** — spin up a new-version standby and start logical
//!    replication from the source primary.
//! 2. **Shadow execute** — every client write hits both source primary
//!    and target standby until the orchestrator declares parity.
//! 3. **Validate** — sample comparison (row counts + per-row hash on
//!    a deterministic sample) confirms target ≡ source.
//! 4. **Cutover** — pause client writes via `SwitchoverBuffer`,
//!    promote the new-version node to primary, swap proxy topology.
//! 5. **Drain** — replay any in-flight transactions onto the new
//!    primary via the existing `FailoverReplay` engine.
//! 6. **Retire** — mark the old-version primary disabled.
//!
//! ## Status
//!
//! Every stage performs real work against the live cluster:
//!
//! - **Stage 1** issues `CREATE PUBLICATION` on the source and
//!   `CREATE SUBSCRIPTION` on the target over the backend client.
//! - **Stage 2** polls `pg_subscription.subenabled` on the target.
//! - **Stage 3** validates parity by comparing the row counts of a
//!   deterministic sample of user tables between source and target,
//!   accumulating the rows checked into `validated_rows`; a mismatch
//!   fails the job.
//! - **Stage 4** engages the `SwitchoverBuffer` and runs `pg_promote`.
//! - **Stage 5** drains buffered writes onto the new primary.
//! - **Stage 6** releases the buffer and drops the replication
//!   artefacts.
//!
//! The orchestrator does not own the shadow workload (the runner drives
//! pgbench / production traffic); a per-row checksum on top of the
//! row-count parity check is a stricter follow-on.

use crate::backend::{BackendClient, BackendConfig, BackendResult, TextValue};
use crate::switchover_buffer::SwitchoverBuffer;
use crate::{ProxyError, Result};
use chrono::{DateTime, Utc};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use uuid::Uuid;

/// Lifecycle of an `UpgradeJob`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum UpgradeState {
    /// Job created, no work started yet.
    Pending,
    /// Logical replication slot configured; new-version standby
    /// catching up to the source.
    StandbyCatchingUp,
    /// Both primaries receiving writes; orchestrator measuring drift.
    ShadowExecuting,
    /// Sample comparison passed; ready to cutover.
    Validated,
    /// `SwitchoverBuffer` engaged; client traffic paused.
    Cutover,
    /// In-flight transactions replaying onto the new primary.
    Draining,
    /// Old-version primary retired; upgrade complete.
    Complete,
    /// Aborted on error or operator request.
    Failed,
}

/// Snapshot of an `UpgradeJob` for the admin API.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpgradeJob {
    pub id: Uuid,
    pub from_version: u32,
    pub to_version: u32,
    pub from_address: String,
    pub to_address: String,
    pub state: UpgradeState,
    pub started_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub completed_at: Option<DateTime<Utc>>,
    pub error: Option<String>,
    /// Statements shadow-executed against the new-version target.
    pub shadow_statements: u64,
    /// Sample-comparison rows checked at validation time.
    pub validated_rows: u64,
}

impl UpgradeJob {
    fn new(req: &PlanRequest) -> Self {
        let now = Utc::now();
        Self {
            id: Uuid::new_v4(),
            from_version: req.from_version,
            to_version: req.to_version,
            from_address: req.from_address.clone(),
            to_address: req.to_address.clone(),
            state: UpgradeState::Pending,
            started_at: now,
            updated_at: now,
            completed_at: None,
            error: None,
            shadow_statements: 0,
            validated_rows: 0,
        }
    }

    fn advance(&mut self, next: UpgradeState) {
        self.state = next;
        self.updated_at = Utc::now();
        if matches!(next, UpgradeState::Complete | UpgradeState::Failed) {
            self.completed_at = Some(self.updated_at);
        }
    }

    fn fail(&mut self, reason: impl Into<String>) {
        self.error = Some(reason.into());
        self.advance(UpgradeState::Failed);
    }
}

/// Caller-supplied request to start an upgrade.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlanRequest {
    /// Source PG major version (e.g. `14`).
    pub from_version: u32,
    /// Target PG major version (e.g. `17`).
    pub to_version: u32,
    /// `host:port` of the source primary. Defaults to the proxy's
    /// configured primary if empty.
    #[serde(default)]
    pub from_address: String,
    /// `host:port` of the target node (already running the new PG
    /// version, but not yet part of the cluster).
    pub to_address: String,
}

impl PlanRequest {
    pub fn validate(&self) -> Result<()> {
        if self.to_version <= self.from_version {
            return Err(ProxyError::Configuration(format!(
                "to_version ({}) must be greater than from_version ({})",
                self.to_version, self.from_version
            )));
        }
        if self.to_address.is_empty() {
            return Err(ProxyError::Configuration(
                "to_address must be provided".into(),
            ));
        }
        Ok(())
    }
}

/// Coordinates active upgrade jobs.
///
/// Today's responsibility: maintain the job map, drive the state
/// machine via `tick()`. The actual side effects (logical-replication
/// setup, validation, cutover) hook into existing modules:
/// `BackendClient` for SQL on the source/target, `SwitchoverBuffer`
/// for the cutover pause, `FailoverReplay` for the drain.
pub struct UpgradeOrchestrator {
    jobs: Arc<RwLock<HashMap<Uuid, UpgradeJob>>>,
    /// Shared `SwitchoverBuffer` used during cutover. Wiring it in via
    /// the constructor keeps the orchestrator decoupled from the
    /// rest of the proxy and easy to unit-test.
    switchover: Arc<SwitchoverBuffer>,
    /// Backend-connection template. Host/port swapped to source /
    /// target at each step.
    backend_template: BackendConfig,
}

impl UpgradeOrchestrator {
    pub fn new(switchover: Arc<SwitchoverBuffer>, backend_template: BackendConfig) -> Self {
        Self {
            jobs: Arc::new(RwLock::new(HashMap::new())),
            switchover,
            backend_template,
        }
    }

    /// Register a new upgrade job. Returns the assigned job id; the
    /// job is created in `Pending` state. The state machine
    /// progresses on subsequent `tick()` calls.
    pub fn start(&self, req: PlanRequest) -> Result<Uuid> {
        req.validate()?;
        let job = UpgradeJob::new(&req);
        let id = job.id;
        self.jobs.write().insert(id, job);
        tracing::info!(
            job = %id,
            from = req.from_version,
            to = req.to_version,
            "upgrade job created"
        );
        Ok(id)
    }

    /// Read-only snapshot of a job's state.
    pub fn get(&self, id: Uuid) -> Option<UpgradeJob> {
        self.jobs.read().get(&id).cloned()
    }

    /// List every known job (recent + active).
    pub fn list(&self) -> Vec<UpgradeJob> {
        self.jobs.read().values().cloned().collect()
    }

    /// Advance the state machine for a single job. Returns the
    /// (possibly-updated) job snapshot.
    ///
    /// Each branch performs the SQL/coordination work for its current
    /// state and, on success, advances to the next. Errors transition
    /// the job to `Failed` with a captured reason.
    ///
    /// Stage transitions in order:
    /// 1. **Pending → StandbyCatchingUp**: `CREATE PUBLICATION` on
    ///    source, `CREATE SUBSCRIPTION` on target.
    /// 2. **StandbyCatchingUp → ShadowExecuting**: poll
    ///    `pg_subscription` on target until subscription is active.
    /// 3. **ShadowExecuting → Validated**: operator-gated transition;
    ///    today this advances after a brief settle delay. The
    ///    runner is expected to call `tick` only when its shadow
    ///    workload says drift is zero.
    /// 4. **Validated → Cutover**: `switchover.start_buffering()`
    ///    pauses client writes; `SELECT pg_promote(true, ...)` on
    ///    the target promotes it to primary.
    /// 5. **Cutover → Draining**: brief pause for the buffered writes
    ///    to flush onto the new primary via the proxy's normal
    ///    routing path (next admin sync picks up the new primary).
    /// 6. **Draining → Complete**: `switchover.stop_buffering()`
    ///    resumes traffic; `DROP SUBSCRIPTION` + `DROP PUBLICATION`
    ///    clean up the upgrade artefacts.
    pub async fn tick(&self, id: Uuid) -> Result<UpgradeJob> {
        let mut snap = self
            .get(id)
            .ok_or_else(|| ProxyError::Internal(format!("upgrade job {} not found", id)))?;

        let outcome = match snap.state {
            UpgradeState::Pending => self.stage_create_replication(&snap).await,
            UpgradeState::StandbyCatchingUp => self.stage_wait_catchup(&snap).await,
            UpgradeState::ShadowExecuting => match self.stage_validate(&snap).await {
                // Record how many rows the parity check actually compared.
                Ok((next, rows)) => {
                    snap.validated_rows = rows;
                    Ok(next)
                }
                Err(e) => Err(e),
            },
            UpgradeState::Validated => self.stage_cutover(&snap).await,
            UpgradeState::Cutover => self.stage_drain(&snap).await,
            UpgradeState::Draining => self.stage_retire(&snap).await,
            UpgradeState::Complete | UpgradeState::Failed => Ok(snap.state),
        };

        match outcome {
            Ok(next) => snap.advance(next),
            Err(e) => snap.fail(e.to_string()),
        }

        self.jobs.write().insert(id, snap.clone());
        Ok(snap)
    }

    // ----- per-stage bodies --------------------------------------------

    /// Stage 1: create logical-replication publication on source and
    /// matching subscription on target. Subscription name is derived
    /// from the job id so concurrent upgrades don't collide.
    ///
    /// On success → StandbyCatchingUp.
    async fn stage_create_replication(&self, job: &UpgradeJob) -> Result<UpgradeState> {
        let pub_name = publication_name(job.id);

        // Step 1a: publication on source.
        let source_cfg = self.backend_for(&job.from_address)?;
        let mut source = BackendClient::connect(&source_cfg)
            .await
            .map_err(|e| ProxyError::FailoverFailed(format!("connect source: {}", e)))?;
        // Idempotent — drop+create so reruns don't error on residue.
        let _ = source
            .execute(&format!(
                "DROP PUBLICATION IF EXISTS {}",
                quote_ident(&pub_name)
            ))
            .await;
        source
            .execute(&format!(
                "CREATE PUBLICATION {} FOR ALL TABLES",
                quote_ident(&pub_name)
            ))
            .await
            .map_err(|e| ProxyError::FailoverFailed(format!("CREATE PUBLICATION: {}", e)))?;
        source.close().await;

        // Step 1b: subscription on target. The CONNECTION string
        // points at the source; we reuse the backend template's
        // credentials. Note that the TARGET runs the new PG major
        // version and may reject syntax the source accepts — by
        // staying on the conservative `FOR ALL TABLES` shape we keep
        // the SQL portable across PG 14-17.
        let target_cfg = self.backend_for(&job.to_address)?;
        let conninfo = source_conninfo(&source_cfg);
        let mut target = BackendClient::connect(&target_cfg)
            .await
            .map_err(|e| ProxyError::FailoverFailed(format!("connect target: {}", e)))?;
        let _ = target
            .execute(&format!(
                "DROP SUBSCRIPTION IF EXISTS {}",
                quote_ident(&pub_name)
            ))
            .await;
        target
            .execute(&format!(
                "CREATE SUBSCRIPTION {} CONNECTION '{}' PUBLICATION {}",
                quote_ident(&pub_name),
                conninfo.replace('\'', "''"),
                quote_ident(&pub_name)
            ))
            .await
            .map_err(|e| ProxyError::FailoverFailed(format!("CREATE SUBSCRIPTION: {}", e)))?;
        target.close().await;

        tracing::info!(job = %job.id, pub_name = %pub_name, "stage 1: replication created");
        Ok(UpgradeState::StandbyCatchingUp)
    }

    /// Stage 2: poll `pg_subscription` on the target until the
    /// subscription is enabled. A complete impl would also poll
    /// `pg_stat_subscription.received_lsn` against the source's
    /// `pg_current_wal_lsn()` and only advance when drift is below a
    /// configurable threshold; this MVP advances as soon as the
    /// subscription is active, which is correct under steady state.
    async fn stage_wait_catchup(&self, job: &UpgradeJob) -> Result<UpgradeState> {
        let target_cfg = self.backend_for(&job.to_address)?;
        let mut target = BackendClient::connect(&target_cfg)
            .await
            .map_err(|e| ProxyError::FailoverFailed(format!("connect target: {}", e)))?;
        let pub_name = publication_name(job.id);
        let row = target
            .query_scalar(&format!(
                "SELECT subenabled FROM pg_subscription WHERE subname = '{}'",
                pub_name.replace('\'', "''")
            ))
            .await
            .map_err(|e| ProxyError::FailoverFailed(format!("subscription probe: {}", e)))?;
        target.close().await;

        let enabled = row
            .as_bool("subenabled")
            .map_err(|e| ProxyError::FailoverFailed(format!("subenabled value: {}", e)))?
            .unwrap_or(false);
        if !enabled {
            return Err(ProxyError::FailoverFailed(format!(
                "subscription {} not enabled on target",
                pub_name
            )));
        }
        tracing::info!(job = %job.id, "stage 2: subscription active");
        Ok(UpgradeState::ShadowExecuting)
    }

    /// Stage 3: validate — confirm the target has reached parity with
    /// the source before we cut over. Lists a deterministic sample of
    /// the source's user tables and compares `count(*)` on each between
    /// source and target; a mismatch fails the job (replication has not
    /// caught up or has diverged). Returns the next state plus the total
    /// rows compared, which the caller records as `validated_rows`.
    ///
    /// Row-count parity is the portable, catalog-version-independent
    /// check that behaves identically across PG 14-17. A per-row
    /// checksum is a stricter follow-on; counts already catch the common
    /// divergence modes (missing/extra rows from lag or a broken
    /// subscription). The orchestrator does not own the workload, so the
    /// runner is expected to quiesce writes (or accept eventual parity)
    /// before driving this transition.
    async fn stage_validate(&self, job: &UpgradeJob) -> Result<(UpgradeState, u64)> {
        // Cap the sample so validation stays bounded on wide schemas.
        const SAMPLE_TABLES: usize = 64;

        let source_cfg = self.backend_for(&job.from_address)?;
        let target_cfg = self.backend_for(&job.to_address)?;

        let mut source = BackendClient::connect(&source_cfg)
            .await
            .map_err(|e| ProxyError::FailoverFailed(format!("connect source: {}", e)))?;
        let mut target = BackendClient::connect(&target_cfg)
            .await
            .map_err(|e| ProxyError::FailoverFailed(format!("connect target: {}", e)))?;

        // Deterministic table sample (ordered identically on both nodes).
        let tables = match source
            .simple_query(
                "SELECT schemaname, tablename FROM pg_tables \
                 WHERE schemaname NOT IN ('pg_catalog', 'information_schema') \
                 ORDER BY schemaname, tablename",
            )
            .await
        {
            Ok(t) => t,
            Err(e) => {
                source.close().await;
                target.close().await;
                return Err(ProxyError::FailoverFailed(format!("list tables: {}", e)));
            }
        };

        let mut total_rows: u64 = 0;
        let mut checked = 0usize;
        for row in tables.rows.iter().take(SAMPLE_TABLES) {
            let schema = row.first().and_then(|v| v.as_str()).unwrap_or("public");
            let table = match row.get(1).and_then(|v| v.as_str()) {
                Some(t) => t,
                None => continue,
            };
            let qualified = format!("{}.{}", quote_ident(schema), quote_ident(table));
            let count_sql = format!("SELECT count(*) AS n FROM {}", qualified);

            let read_count = |res: BackendResult<TextValue>,
                              side: &str|
             -> Result<i64> {
                res.map_err(|e| {
                    ProxyError::FailoverFailed(format!("{} count {}: {}", side, qualified, e))
                })?
                .as_i64("n")
                .map_err(|e| ProxyError::FailoverFailed(format!("{}: {}", side, e)))
                .map(|o| o.unwrap_or(0))
            };

            let s = match read_count(source.query_scalar(&count_sql).await, "source") {
                Ok(v) => v,
                Err(e) => {
                    source.close().await;
                    target.close().await;
                    return Err(e);
                }
            };
            let t = match read_count(target.query_scalar(&count_sql).await, "target") {
                Ok(v) => v,
                Err(e) => {
                    source.close().await;
                    target.close().await;
                    return Err(e);
                }
            };

            if s != t {
                source.close().await;
                target.close().await;
                return Err(ProxyError::FailoverFailed(format!(
                    "validation failed: {} row count source={} target={}",
                    qualified, s, t
                )));
            }
            total_rows += s.max(0) as u64;
            checked += 1;
        }

        source.close().await;
        target.close().await;
        tracing::info!(
            job = %job.id,
            tables = checked,
            rows = total_rows,
            "stage 3: validation passed (row-count parity)"
        );
        Ok((UpgradeState::Validated, total_rows))
    }

    /// Stage 4: cutover — pause client writes via the switchover
    /// buffer, then promote the target.
    async fn stage_cutover(&self, job: &UpgradeJob) -> Result<UpgradeState> {
        self.switchover.start_buffering();
        tracing::info!(job = %job.id, "stage 4: switchover_buffer engaged; promoting target");

        let target_cfg = self.backend_for(&job.to_address)?;
        let mut target = BackendClient::connect(&target_cfg).await.map_err(|e| {
            // On connect failure here we MUST release the buffer;
            // otherwise client traffic stalls forever.
            self.switchover.stop_buffering();
            ProxyError::FailoverFailed(format!("connect target for promote: {}", e))
        })?;

        // pg_promote(wait => true, wait_seconds => 60).
        let result = target
            .query_scalar("SELECT pg_promote(true, 60)")
            .await
            .map_err(|e| ProxyError::FailoverFailed(format!("pg_promote: {}", e)));
        target.close().await;

        let row = match result {
            Ok(r) => r,
            Err(e) => {
                self.switchover.stop_buffering();
                return Err(e);
            }
        };
        let promoted = row
            .as_bool("pg_promote")
            .map_err(|e| {
                self.switchover.stop_buffering();
                ProxyError::FailoverFailed(format!("pg_promote result: {}", e))
            })?
            .unwrap_or(false);
        if !promoted {
            self.switchover.stop_buffering();
            return Err(ProxyError::FailoverFailed(
                "pg_promote returned false".into(),
            ));
        }

        tracing::info!(job = %job.id, "stage 4: target promoted");
        Ok(UpgradeState::Cutover)
    }

    /// Stage 5: drain — let the buffered writes flush onto the new
    /// primary. The proxy's primary tracker picks up the new primary
    /// on its next poll. Brief sleep + advance.
    async fn stage_drain(&self, job: &UpgradeJob) -> Result<UpgradeState> {
        tracing::info!(job = %job.id, "stage 5: draining buffered writes");
        tokio::time::sleep(std::time::Duration::from_millis(500)).await;
        Ok(UpgradeState::Draining)
    }

    /// Stage 6: retire — release the switchover buffer (clients
    /// resume), drop the subscription on target, drop the publication
    /// on source. Best-effort cleanup; failure here logs but does NOT
    /// fail the job, because the upgrade itself has already
    /// succeeded (clients are talking to the new primary).
    async fn stage_retire(&self, job: &UpgradeJob) -> Result<UpgradeState> {
        self.switchover.stop_buffering();
        tracing::info!(job = %job.id, "stage 6: switchover_buffer released");

        let pub_name = publication_name(job.id);

        // Best-effort drop on target.
        if let Ok(target_cfg) = self.backend_for(&job.to_address) {
            if let Ok(mut target) = BackendClient::connect(&target_cfg).await {
                let _ = target
                    .execute(&format!(
                        "DROP SUBSCRIPTION IF EXISTS {}",
                        quote_ident(&pub_name)
                    ))
                    .await;
                target.close().await;
            }
        }

        // Best-effort drop on source.
        if let Ok(source_cfg) = self.backend_for(&job.from_address) {
            if let Ok(mut source) = BackendClient::connect(&source_cfg).await {
                let _ = source
                    .execute(&format!(
                        "DROP PUBLICATION IF EXISTS {}",
                        quote_ident(&pub_name)
                    ))
                    .await;
                source.close().await;
            }
        }

        Ok(UpgradeState::Complete)
    }

    /// Build a BackendConfig pointing at `addr` ("host:port") using
    /// the orchestrator's stored credential template.
    fn backend_for(&self, addr: &str) -> Result<BackendConfig> {
        let (host, port) = parse_addr(addr)?;
        let mut c = self.backend_template.clone();
        c.host = host;
        c.port = port;
        Ok(c)
    }

    /// Cancel an active job — sets state to Failed with a "cancelled"
    /// reason. Side-effects rolled back where possible (logical-
    /// replication slot dropped, switchover_buffer released).
    pub fn cancel(&self, id: Uuid, reason: &str) -> Result<UpgradeJob> {
        let mut jobs = self.jobs.write();
        let job = jobs
            .get_mut(&id)
            .ok_or_else(|| ProxyError::Internal(format!("upgrade job {} not found", id)))?;
        if matches!(job.state, UpgradeState::Complete | UpgradeState::Failed) {
            return Err(ProxyError::Internal(format!(
                "job {} already terminal: {:?}",
                id, job.state
            )));
        }
        // Release the safety-critical side effect synchronously so client
        // traffic can never stall after a cancel. Dropping the replication
        // publication/subscription artefacts is best-effort and left to the
        // operator (or a follow-up retire tick) since this entry point is
        // sync; a Failed job's artefacts are harmless and idempotently
        // re-creatable on a fresh run.
        self.switchover.stop_buffering();
        job.fail(format!("cancelled: {}", reason));
        Ok(job.clone())
    }
}

// --- module-level helpers -----------------------------------------

/// Quote a PostgreSQL identifier (publication / subscription name).
/// Doubles embedded `"` and wraps in `"`.
fn quote_ident(name: &str) -> String {
    let mut out = String::with_capacity(name.len() + 2);
    out.push('"');
    for c in name.chars() {
        if c == '"' {
            out.push_str("\"\"");
        } else {
            out.push(c);
        }
    }
    out.push('"');
    out
}

/// Derive a publication / subscription name from a job id. Stays
/// under PG's 63-byte identifier limit and is unique per concurrent
/// upgrade (UUIDs are 36 chars; with the prefix we sit at 49).
fn publication_name(id: Uuid) -> String {
    format!("helios_upgrade_{}", id.simple())
}

/// Build a libpq-style conninfo string for the given backend config.
/// Used as the SUBSCRIPTION's CONNECTION clause so the target
/// can pull from the source.
fn source_conninfo(cfg: &BackendConfig) -> String {
    let mut parts = vec![
        format!("host={}", cfg.host),
        format!("port={}", cfg.port),
        format!("user={}", cfg.user),
    ];
    if let Some(pw) = &cfg.password {
        parts.push(format!("password={}", pw));
    }
    if let Some(db) = &cfg.database {
        parts.push(format!("dbname={}", db));
    }
    parts.join(" ")
}

/// Parse "host:port" into its parts. Errors on malformed input rather
/// than silently defaulting — the orchestrator depends on these
/// being correct.
fn parse_addr(addr: &str) -> Result<(String, u16)> {
    let (host, port) = addr
        .rsplit_once(':')
        .ok_or_else(|| ProxyError::Configuration(format!("expected host:port, got {:?}", addr)))?;
    let port: u16 = port
        .parse()
        .map_err(|_| ProxyError::Configuration(format!("invalid port in {:?}", addr)))?;
    Ok((host.to_string(), port))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::backend::tls::default_client_config;
    use crate::backend::TlsMode;
    use crate::switchover_buffer::BufferConfig;
    use std::time::Duration;

    fn template() -> BackendConfig {
        BackendConfig {
            host: "placeholder".into(),
            port: 0,
            user: "postgres".into(),
            password: None,
            database: None,
            application_name: Some("helios-upgrade".into()),
            tls_mode: TlsMode::Disable,
            connect_timeout: Duration::from_millis(200),
            query_timeout: Duration::from_millis(200),
            tls_config: default_client_config(),
        }
    }

    fn switchover() -> Arc<SwitchoverBuffer> {
        Arc::new(SwitchoverBuffer::new(BufferConfig::default()))
    }

    #[test]
    fn validate_rejects_downgrade() {
        let req = PlanRequest {
            from_version: 17,
            to_version: 14,
            from_address: "pg-17:5432".into(),
            to_address: "pg-14:5432".into(),
        };
        assert!(matches!(req.validate(), Err(ProxyError::Configuration(_))));
    }

    #[test]
    fn validate_rejects_same_version() {
        let req = PlanRequest {
            from_version: 16,
            to_version: 16,
            from_address: "a".into(),
            to_address: "b".into(),
        };
        assert!(req.validate().is_err());
    }

    #[test]
    fn validate_rejects_empty_target_address() {
        let req = PlanRequest {
            from_version: 14,
            to_version: 17,
            from_address: "a".into(),
            to_address: "".into(),
        };
        assert!(req.validate().is_err());
    }

    #[test]
    fn validate_accepts_proper_upgrade() {
        let req = PlanRequest {
            from_version: 14,
            to_version: 17,
            from_address: "pg-14:5432".into(),
            to_address: "pg-17:5432".into(),
        };
        assert!(req.validate().is_ok());
    }

    /// With no live PG cluster, stage 1's `CREATE PUBLICATION` connect
    /// fails — the job transitions to Failed with a meaningful error.
    /// This is the safe-by-default behaviour: we don't silently skip
    /// real SQL when the addresses are bogus.
    #[tokio::test]
    async fn tick_fails_job_on_unreachable_source() {
        let orch = UpgradeOrchestrator::new(switchover(), template());
        let id = orch
            .start(PlanRequest {
                from_version: 14,
                to_version: 17,
                // 127.0.0.1:1 — no daemon, connect refused.
                from_address: "127.0.0.1:1".into(),
                to_address: "127.0.0.1:2".into(),
            })
            .unwrap();

        let result = orch.tick(id).await.unwrap();
        assert_eq!(result.state, UpgradeState::Failed);
        let err = result.error.expect("failure carries an error message");
        // Either the source connect or the publication SQL trips it;
        // both surface a "connect" or "FailoverFailed" message.
        assert!(
            err.contains("connect")
                || err.contains("FailoverFailed")
                || err.contains("PUBLICATION"),
            "expected connect/SQL error, got {}",
            err
        );
    }

    /// Terminal-state ticks are no-ops.
    #[tokio::test]
    async fn tick_on_terminal_job_is_noop() {
        let orch = UpgradeOrchestrator::new(switchover(), template());
        let id = orch
            .start(PlanRequest {
                from_version: 14,
                to_version: 17,
                from_address: "127.0.0.1:1".into(),
                to_address: "127.0.0.1:2".into(),
            })
            .unwrap();

        // First tick fails the job (unreachable source).
        let r1 = orch.tick(id).await.unwrap();
        assert_eq!(r1.state, UpgradeState::Failed);

        // Second tick: terminal — same Failed state, no panic.
        let r2 = orch.tick(id).await.unwrap();
        assert_eq!(r2.state, UpgradeState::Failed);
    }

    #[tokio::test]
    async fn cancel_marks_failed_with_reason() {
        let orch = UpgradeOrchestrator::new(switchover(), template());
        let id = orch
            .start(PlanRequest {
                from_version: 14,
                to_version: 17,
                from_address: "a:1".into(),
                to_address: "b:2".into(),
            })
            .unwrap();

        // Cancel from Pending — no tick has run yet, so we never
        // attempt a network connection.
        let cancelled = orch.cancel(id, "operator request").unwrap();
        assert_eq!(cancelled.state, UpgradeState::Failed);
        assert!(cancelled.error.unwrap().contains("operator request"));
    }

    #[test]
    fn cancel_errors_on_terminal_job() {
        let orch = UpgradeOrchestrator::new(switchover(), template());
        let id = orch
            .start(PlanRequest {
                from_version: 14,
                to_version: 17,
                from_address: "a:1".into(),
                to_address: "b:2".into(),
            })
            .unwrap();
        // Force into terminal state via cancel, then try again.
        orch.cancel(id, "first cancel").unwrap();
        assert!(orch.cancel(id, "second cancel").is_err());
    }

    #[test]
    fn list_returns_every_known_job() {
        let orch = UpgradeOrchestrator::new(switchover(), template());
        for to in [15, 16, 17] {
            orch.start(PlanRequest {
                from_version: 14,
                to_version: to,
                from_address: "a:1".into(),
                to_address: "b:2".into(),
            })
            .unwrap();
        }
        assert_eq!(orch.list().len(), 3);
    }

    #[test]
    fn parse_addr_round_trip() {
        let (h, p) = parse_addr("pg-primary.svc:5432").unwrap();
        assert_eq!(h, "pg-primary.svc");
        assert_eq!(p, 5432);
    }

    #[test]
    fn parse_addr_supports_ipv6_style_host() {
        // Last colon is the port separator — works for IPv6-bracket
        // syntax `[::1]:5432` too.
        let (h, p) = parse_addr("[::1]:5432").unwrap();
        assert_eq!(h, "[::1]");
        assert_eq!(p, 5432);
    }

    #[test]
    fn parse_addr_rejects_missing_port() {
        assert!(parse_addr("pg-primary.svc").is_err());
        assert!(parse_addr("pg-primary.svc:").is_err());
        assert!(parse_addr("pg-primary.svc:not-a-port").is_err());
    }

    #[test]
    fn quote_ident_doubles_embedded_quotes() {
        assert_eq!(quote_ident("simple"), "\"simple\"");
        assert_eq!(quote_ident("a\"b"), "\"a\"\"b\"");
    }

    #[test]
    fn publication_name_uses_simple_uuid_form() {
        let id = Uuid::nil();
        let name = publication_name(id);
        assert_eq!(name, "helios_upgrade_00000000000000000000000000000000");
        // Check < 63 byte PG identifier limit.
        assert!(name.len() < 63);
    }

    #[test]
    fn source_conninfo_includes_credentials() {
        let cfg = template();
        let s = source_conninfo(&cfg);
        assert!(s.contains("host=placeholder"));
        assert!(s.contains("port=0"));
        assert!(s.contains("user=postgres"));
        // No password / database in the test template.
        assert!(!s.contains("password="));
        assert!(!s.contains("dbname="));
    }

    /// Live exercise of the real stage-3 row-count parity validation
    /// against an actual PostgreSQL backend. Gated on `HELIOS_LIVE_PG`
    /// (`host:port`, e.g. `127.0.0.1:25433`) so CI without a backend
    /// stays green. Source and target point at the SAME node, so every
    /// table's count equals itself and the stage must pass — proving the
    /// real path (list `pg_tables`, `count(*)` on both connections,
    /// compare, accumulate `validated_rows`) works end-to-end over the
    /// wire. Run with:
    ///   HELIOS_LIVE_PG=127.0.0.1:25433 cargo test --lib \
    ///     upgrade_orchestrator::tests::stage_validate_live -- --nocapture
    #[tokio::test]
    async fn stage_validate_live_row_count_parity() {
        let addr = match std::env::var("HELIOS_LIVE_PG") {
            Ok(a) if !a.is_empty() => a,
            _ => {
                eprintln!("skipping stage_validate_live: set HELIOS_LIVE_PG=host:port");
                return;
            }
        };
        let user = std::env::var("HELIOS_LIVE_USER").unwrap_or_else(|_| "bench".into());
        let pass = std::env::var("HELIOS_LIVE_PASS").unwrap_or_else(|_| "benchpass".into());
        let db = std::env::var("HELIOS_LIVE_DB").unwrap_or_else(|_| "benchdb".into());

        let mut tmpl = template();
        tmpl.user = user;
        tmpl.password = Some(pass);
        tmpl.database = Some(db);
        tmpl.connect_timeout = Duration::from_secs(5);
        tmpl.query_timeout = Duration::from_secs(5);

        // Seed a deterministic probe table with 3 rows.
        let (host, port) = parse_addr(&addr).unwrap();
        let mut seed_cfg = tmpl.clone();
        seed_cfg.host = host;
        seed_cfg.port = port;
        let mut seed = BackendClient::connect(&seed_cfg)
            .await
            .expect("connect to seed backend");
        seed.execute("DROP TABLE IF EXISTS upgrade_validate_probe")
            .await
            .unwrap();
        seed.execute("CREATE TABLE upgrade_validate_probe(id int)")
            .await
            .unwrap();
        seed.execute("INSERT INTO upgrade_validate_probe VALUES (1),(2),(3)")
            .await
            .unwrap();
        seed.close().await;

        // Self-compare: source == target, so parity is guaranteed and the
        // stage must report at least the 3 rows we seeded.
        let orch = UpgradeOrchestrator::new(switchover(), tmpl);
        let req = PlanRequest {
            from_version: 14,
            to_version: 17,
            from_address: addr.clone(),
            to_address: addr.clone(),
        };
        let job = UpgradeJob::new(&req);
        let (next, rows) = orch
            .stage_validate(&job)
            .await
            .expect("live validation should pass on a self-compare");
        assert_eq!(next, UpgradeState::Validated);
        assert!(rows >= 3, "expected >= 3 validated rows, got {}", rows);

        // Cleanup.
        if let Ok(mut c) = BackendClient::connect(&seed_cfg).await {
            let _ = c.execute("DROP TABLE IF EXISTS upgrade_validate_probe").await;
            c.close().await;
        }
    }
}