athena_rs 3.4.7

Database driver
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
//! Postgres client bootstrap: config targets, catalog merge planning, and merge reporting.
//!
//! Keeps connection orchestration testable without running the full `build_shared_state` stack.

use std::collections::HashSet;

use futures::stream::{self, StreamExt};

use crate::config::Config;
use crate::config_validation::runtime_env_settings;
use crate::data::clients::AthenaClientRecord;
use crate::drivers::postgresql::pool_manager::ConnectionPoolManager;
use crate::drivers::postgresql::sqlx_driver::{
    ClientConnectionTarget, PostgresClientRegistry, normalize_postgres_client_key,
};
use crate::parser::{parse_env_reference, resolve_postgres_uri};

/// Concurrent `upsert_client` calls while merging `athena_clients` so many unreachable
/// hosts do not add up to one long sequential stall before the HTTP server binds.
const CATALOG_UPSERT_CONCURRENCY: usize = 8;

/// ## `PostgresCatalogMergeReport`
/// Counters for [`merge_catalog_targets_into_registry`].
///
/// # Fields
///
/// * `inactive_or_frozen` - The number of clients that are inactive or frozen.
/// * `already_connected` - The number of clients that are already connected.
/// * `skipped_prior_config_failure` - The number of clients that should be skipped due to a prior config failure.
/// * `upsert_succeeded` - The number of clients that were upserted successfully.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PostgresCatalogMergeReport {
    pub inactive_or_frozen: usize,
    pub already_connected: usize,
    pub skipped_prior_config_failure: usize,
    pub upsert_succeeded: usize,
    pub upsert_failed: usize,
}

/// ## `CatalogClientStep`
/// Decides how a single catalog row should be handled before any async work.
///
/// # Variants
///
/// * `InactiveOrFrozen` - The client is inactive or frozen.
/// * `AlreadyConnected` - The client is already connected.
/// * `SkipDueToPriorConfigFailure` - The client should be skipped due to a prior config failure.
/// * `LoadFromCatalog` - The client should be loaded from the catalog.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CatalogClientStep {
    InactiveOrFrozen,
    AlreadyConnected,
    SkipDueToPriorConfigFailure,
    LoadFromCatalog,
}

/// ## `client_connection_targets_from_config`
/// Build [`ClientConnectionTarget`] entries from `config.postgres_clients`
/// (same rules as server bootstrap).
///
/// # Arguments
///
/// * `config` - The configuration to build the targets from.
///
/// # Returns
///
/// A vector of [`ClientConnectionTarget`] entries.
///
pub fn client_connection_targets_from_config(config: &Config) -> Vec<ClientConnectionTarget> {
    config
        .postgres_clients
        .iter()
        .flat_map(|map| {
            map.iter().map(|(key, uri)| ClientConnectionTarget {
                client_name: key.clone(),
                source: "config".to_string(),
                description: None,
                pg_uri: parse_env_reference(uri)
                    .is_none()
                    .then(|| resolve_postgres_uri(uri)),
                pg_uri_env_var: parse_env_reference(uri),
                config_uri_template: Some(uri.clone()),
                is_active: true,
                is_frozen: false,
            })
        })
        .collect()
}

/// ## `postgres_registry_entries_from_targets`
/// `(client_name, resolved_uri)` pairs used by [`PostgresClientRegistry::from_entries`].
///
/// # Arguments
///
/// * `targets` - The targets to build the entries from.
///
/// # Returns
///
pub fn postgres_registry_entries_from_targets(
    targets: &[ClientConnectionTarget],
) -> Vec<(String, String)> {
    targets
        .iter()
        .filter_map(|target| {
            let uri = target
                .config_uri_template
                .as_ref()
                .map(|value| resolve_postgres_uri(value))
                .or_else(|| target.pg_uri.clone());
            uri.map(|uri| (target.client_name.clone(), uri))
        })
        .collect()
}

/// ## `failed_config_keys_from_errors`
/// Keys for clients that failed during config-time pool creation (trim + lowercase).
///
/// # Arguments
///
/// * `errors` - The errors to build the keys from.
///
/// # Returns
///
/// A set of normalized client keys.
///
pub fn failed_config_keys_from_errors(errors: &[(String, anyhow::Error)]) -> HashSet<String> {
    errors
        .iter()
        .map(|(name, _)| normalize_postgres_client_key(name))
        .collect()
}

/// ## `plan_catalog_client_step`
/// Pure planning step for unit tests and for documenting merge behavior.
///
/// # Arguments
///
/// * `target` - The target to plan the step for.
/// * `has_pool` - Whether the client has a pool.
/// * `failed_config_keys` - The keys of clients that failed during config-time pool creation.
///
/// # Returns
///
///
pub fn plan_catalog_client_step(
    target: &ClientConnectionTarget,
    has_pool: bool,
    failed_config_keys: &HashSet<String>,
) -> CatalogClientStep {
    if !target.is_active || target.is_frozen {
        return CatalogClientStep::InactiveOrFrozen;
    }
    if has_pool {
        return CatalogClientStep::AlreadyConnected;
    }
    if failed_config_keys.contains(&normalize_postgres_client_key(&target.client_name)) {
        return CatalogClientStep::SkipDueToPriorConfigFailure;
    }
    CatalogClientStep::LoadFromCatalog
}

/// ## `target_from_athena_record`
/// Builds a [`ClientConnectionTarget`] from an [`AthenaClientRecord`].
///
/// # Arguments
///
/// * `record` - The record to build the target from.
///
/// # Returns
///
/// A [`ClientConnectionTarget`] instance.
///
fn target_from_athena_record(record: &AthenaClientRecord) -> ClientConnectionTarget {
    ClientConnectionTarget {
        client_name: record.client_name.clone(),
        source: record.source.clone(),
        description: record.description.clone(),
        pg_uri: record.pg_uri.clone(),
        pg_uri_env_var: record.pg_uri_env_var.clone(),
        config_uri_template: record.config_uri_template.clone(),
        is_active: record.is_active,
        is_frozen: record.is_frozen,
    }
}

/// ## `merge_catalog_targets_into_registry`
/// Applies catalog rows to the registry: inactive handling, dedupe of config failures, then `upsert_client` for the rest.
///
/// # Arguments
///
/// * `registry` - The registry to merge the targets into.
/// * `targets` - The targets to merge.
/// * `failed_config_keys` - The keys of clients that failed during config-time pool creation.
///
/// # Returns
///
/// A [`PostgresCatalogMergeReport`] instance.
///
pub async fn merge_catalog_targets_into_registry(
    registry: &PostgresClientRegistry,
    targets: Vec<ClientConnectionTarget>,
    failed_config_keys: &HashSet<String>,
) -> PostgresCatalogMergeReport {
    let mut report: PostgresCatalogMergeReport = PostgresCatalogMergeReport::default();
    let mut pending_load: Vec<ClientConnectionTarget> = Vec::new();

    for target in targets {
        let has_pool: bool = registry.get_pool(&target.client_name).is_some();
        match plan_catalog_client_step(&target, has_pool, failed_config_keys) {
            CatalogClientStep::InactiveOrFrozen => {
                registry.remember_client(target.clone(), false);
                registry.mark_unavailable(&target.client_name);
                report.inactive_or_frozen += 1;
            }
            CatalogClientStep::AlreadyConnected => {
                registry.remember_client(target, true);
                report.already_connected += 1;
            }
            CatalogClientStep::SkipDueToPriorConfigFailure => {
                registry.remember_client(target.clone(), false);
                registry.mark_unavailable(&target.client_name);
                report.skipped_prior_config_failure += 1;
            }
            CatalogClientStep::LoadFromCatalog => {
                pending_load.push(target);
            }
        }
    }

    if !pending_load.is_empty() {
        tracing::info!(
            count = pending_load.len(),
            concurrency = CATALOG_UPSERT_CONCURRENCY,
            "Loading database-backed Postgres clients from catalog"
        );
    }

    let upsert_results: Vec<(ClientConnectionTarget, anyhow::Result<()>)> =
        stream::iter(pending_load.into_iter().map(|target| async {
            let outcome: anyhow::Result<()> = registry.upsert_client(target.clone()).await;
            (target, outcome)
        }))
        .buffer_unordered(CATALOG_UPSERT_CONCURRENCY)
        .collect()
        .await;

    for (target, outcome) in upsert_results {
        match outcome {
            Ok(()) => {
                report.upsert_succeeded += 1;
            }
            Err(err) => {
                tracing::warn!(
                    client = %target.client_name,
                    error = %err,
                    "Failed to load database-backed client into local registry"
                );
                registry.remember_client(target, false);
                report.upsert_failed += 1;
            }
        }
    }

    tracing::info!(
        inactive_or_frozen = report.inactive_or_frozen,
        already_connected = report.already_connected,
        skipped_prior_config_failure = report.skipped_prior_config_failure,
        upsert_succeeded = report.upsert_succeeded,
        upsert_failed = report.upsert_failed,
        "Postgres catalog merge complete"
    );

    report
}

/// ## `merge_athena_clients_from_records`
/// Converts `athena_clients` rows and merges them (used by server bootstrap).
///
/// # Arguments
///
/// * `registry` - The registry to merge the records into.
/// * `records` - The records to merge.
/// * `failed_config_keys` - The keys of clients that failed during config-time pool creation.
///
/// # Returns
///
/// A [`PostgresCatalogMergeReport`] instance.
///
pub async fn merge_athena_clients_from_records(
    registry: &PostgresClientRegistry,
    records: Vec<AthenaClientRecord>,
    failed_config_keys: &HashSet<String>,
) -> PostgresCatalogMergeReport {
    let targets: Vec<ClientConnectionTarget> =
        records.iter().map(target_from_athena_record).collect();
    merge_catalog_targets_into_registry(registry, targets, failed_config_keys).await
}

/// ## `parse_bool_env`
/// Parses a boolean environment variable.
///
/// # Arguments
///
/// * `value` - The value to parse.
/// * `default` - The default value.
///
/// # Returns
fn parse_bool_env(value: Option<String>, default: bool) -> bool {
    value
        .as_deref()
        .map(|raw| {
            let normalized = raw.trim().to_ascii_lowercase();
            matches!(normalized.as_str(), "1" | "true" | "yes" | "on")
        })
        .unwrap_or(default)
}

/// Builds the same [`ConnectionPoolManager`] used at server startup (env-driven).
pub fn connection_pool_manager_from_env() -> ConnectionPoolManager {
    let runtime_env = runtime_env_settings();
    let pool_max_lifetime_secs: u64 = runtime_env.pg_pool_max_lifetime_secs;

    let pool_test_before_acquire: bool = parse_bool_env(
        std::env::var("ATHENA_PG_POOL_TEST_BEFORE_ACQUIRE").ok(),
        true,
    );

    ConnectionPoolManager::new(crate::client::config::PoolConfig {
        max_connections: runtime_env.pg_pool_max_connections,
        min_connections: runtime_env.pg_pool_min_connections,
        connection_timeout: std::time::Duration::from_secs(
            runtime_env.pg_pool_acquire_timeout_secs,
        ),
        idle_timeout: std::time::Duration::from_secs(runtime_env.pg_pool_idle_timeout_secs),
    })
    .with_max_lifetime(std::time::Duration::from_secs(pool_max_lifetime_secs))
    .with_test_before_acquire(pool_test_before_acquire)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn sample_target(name: &str, active: bool, frozen: bool) -> ClientConnectionTarget {
        ClientConnectionTarget {
            client_name: name.to_string(),
            source: "db".to_string(),
            description: None,
            pg_uri: Some("postgres://localhost/db".to_string()),
            pg_uri_env_var: None,
            config_uri_template: None,
            is_active: active,
            is_frozen: frozen,
        }
    }

    #[test]
    fn plan_inactive() {
        let t = sample_target("x", false, false);
        assert_eq!(
            plan_catalog_client_step(&t, false, &HashSet::new()),
            CatalogClientStep::InactiveOrFrozen
        );
    }

    #[test]
    fn plan_frozen() {
        let t = sample_target("x", true, true);
        assert_eq!(
            plan_catalog_client_step(&t, false, &HashSet::new()),
            CatalogClientStep::InactiveOrFrozen
        );
    }

    #[test]
    fn plan_already_connected() {
        let t = sample_target("x", true, false);
        assert_eq!(
            plan_catalog_client_step(&t, true, &HashSet::new()),
            CatalogClientStep::AlreadyConnected
        );
    }

    #[test]
    fn plan_skip_prior_config_failure() {
        let t = sample_target("Railway_XBP", true, false);
        let mut keys: HashSet<String> = HashSet::new();
        keys.insert("railway_xbp".to_string());
        assert_eq!(
            plan_catalog_client_step(&t, false, &keys),
            CatalogClientStep::SkipDueToPriorConfigFailure
        );
    }

    #[test]
    fn plan_load_from_catalog() {
        let t = sample_target("neon_only", true, false);
        assert_eq!(
            plan_catalog_client_step(&t, false, &HashSet::new()),
            CatalogClientStep::LoadFromCatalog
        );
    }

    #[test]
    fn failed_config_keys_normalizes() {
        let errors: Vec<(String, anyhow::Error)> = vec![
            ("  Spaced  ".to_string(), anyhow::anyhow!("e")),
            ("UPPER".to_string(), anyhow::anyhow!("e")),
        ];
        let keys: HashSet<String> = failed_config_keys_from_errors(&errors);
        assert!(keys.contains("spaced"));
        assert!(keys.contains("upper"));
    }

    #[test]
    fn client_targets_from_minimal_yaml_config() {
        let yaml = r#"
urls: []
hosts: []
api: []
authenticator: []
postgres_clients:
  - alpha: "postgres://u:p@localhost:5432/alpha"
  - beta: "${POSTGRES_BETA_URI}"
gateway: []
backup: []
"#;
        let config: Config = serde_yaml::from_str(yaml).expect("parse config");
        let targets = client_connection_targets_from_config(&config);
        assert_eq!(targets.len(), 2);
        let alpha = targets.iter().find(|t| t.client_name == "alpha").unwrap();
        assert_eq!(alpha.source, "config");
        assert!(alpha.pg_uri.is_some());
        assert!(alpha.pg_uri_env_var.is_none());
        let beta = targets.iter().find(|t| t.client_name == "beta").unwrap();
        assert_eq!(beta.pg_uri_env_var.as_deref(), Some("POSTGRES_BETA_URI"));
        assert!(beta.pg_uri.is_none());
    }

    #[test]
    fn registry_entries_resolve_config_uris() {
        let yaml = r#"
urls: []
hosts: []
api: []
authenticator: []
postgres_clients:
  - gamma: "postgres://user:pass@host:5432/dbname"
gateway: []
backup: []
"#;
        let config: Config = serde_yaml::from_str(yaml).expect("parse config");
        let targets = client_connection_targets_from_config(&config);
        let pairs = postgres_registry_entries_from_targets(&targets);
        assert_eq!(pairs.len(), 1);
        assert_eq!(pairs[0].0, "gamma");
        assert!(pairs[0].1.contains("host:5432"));
    }

    #[tokio::test]
    async fn merge_athena_records_equivalent_to_targets() {
        use chrono::Utc;
        use uuid::Uuid;

        let record = AthenaClientRecord {
            id: Uuid::nil().to_string(),
            client_name: "merge_via_record".to_string(),
            description: None,
            pg_uri: Some("postgres://127.0.0.1:1/nope".to_string()),
            pg_uri_env_var: None,
            config_uri_template: None,
            source: "test".to_string(),
            is_active: true,
            is_frozen: false,
            last_synced_from_config_at: None,
            last_seen_at: None,
            metadata: serde_json::json!({}),
            created_at: Utc::now(),
            updated_at: Utc::now(),
            deleted_at: None,
        };

        let pool_manager = connection_pool_manager_from_env();
        let (registry, _) = PostgresClientRegistry::from_entries(vec![], pool_manager)
            .await
            .expect("registry");
        let report =
            merge_athena_clients_from_records(&registry, vec![record], &HashSet::new()).await;
        assert_eq!(report.upsert_failed, 1);
    }

    #[test]
    fn connection_pool_manager_from_env_builds() {
        let _mgr = connection_pool_manager_from_env();
    }
}