athena_rs 3.26.2

Hyper performant polyglot 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
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
use actix_web::http::StatusCode;
use actix_web::web::Data;
use chrono::Utc;
use serde_json::{Map, Value, json};
use std::time::Duration as StdDuration;
use std::time::Instant;
use tokio::time::Duration as TokioDuration;
use tracing::{error, info, warn};

use crate::AppState;
use crate::api::gateway::lifecycle::log_gateway_operation_result;
use crate::athena::postgres_clients::ensure_catalog_database_client_loaded;
use crate::data::events::post_event;
use crate::data::outbox::{OutboxEventInsert, insert_outbox_event_tx};
use crate::utils::request_logging::LoggedRequest;
use crate::webhooks::GatewayWebhookTrigger;

use super::backend::{InsertDriverError, postgres_insert_with_timeout};
use super::config::insert_db_timeout_ms;
use super::dedupe::{build_insert_duplicate_signature, lookup_recent_unique_violation};
use super::error::{
    InsertError, WindowInsertOutcome, build_error_metadata, log_gateway_insert_success,
    observe_insert_error, prefilter_unique_violation_error, trace_insert_error,
};
use super::window::WindowInsertJob;

pub(crate) fn insert_request_has_update_body(body: &Value) -> bool {
    body.get("update_body").is_some()
}

pub(crate) fn should_invalidate_cache_after_insert(inserted_row: &Value) -> bool {
    match inserted_row {
        Value::Null => false,
        Value::Object(map) => !map.is_empty(),
        _ => true,
    }
}

fn build_diff_resource(body: &Value, insert_body: &Value, user_id: &str) -> Value {
    let mut diff_resource: Value = json!({});
    if let (Some(update_body), Some(insert_body_obj)) = (
        body.get("update_body").and_then(Value::as_object),
        insert_body.as_object(),
    ) {
        for (key, new_value) in update_body {
            if !new_value.is_null() {
                let should_include = match insert_body_obj.get(key) {
                    Some(existing) => existing != new_value,
                    None => true,
                };
                if should_include {
                    diff_resource[key] = json!({
                        "blame": { "user_id": user_id },
                        "new": new_value,
                        "old": null,
                        "time": chrono::Utc::now().timestamp(),
                        "void": false
                    });
                }
            }
        }
    }
    diff_resource
}

#[allow(clippy::too_many_arguments)]
pub(crate) async fn record_insert_audit_log(
    _table_name: String,
    _resource_id: String,
    _insert_body: Value,
    _company_id: String,
    _organization_id: String,
    _domain: String,
    _user_id: String,
    _message: String,
    _status: String,
    _source: String,
    _actor_id: String,
    _action: String,
    _diff_resource: Value,
    _meta: Value,
    _success: bool,
    _path: String,
    _client_name: &str,
) -> Result<Value, String> {
    Ok(json!({ "audit": true }))
}

/// Ensures the window worker always resolves its response channel.
pub(crate) async fn finish_postgres_insert_success_json_with_recovery(
    app_state: Data<AppState>,
    job: &WindowInsertJob,
    inserted_row: Value,
) -> WindowInsertOutcome {
    let timeout: StdDuration = TokioDuration::from_secs(120);
    match tokio::time::timeout(
        timeout,
        finish_postgres_insert_success_json(app_state.clone(), job, inserted_row),
    )
    .await
    {
        Ok(body) => WindowInsertOutcome::Success(body),
        Err(_) => {
            error!(
                trace_id = %job.trace_id,
                client = %job.client_name,
                table = %job.table_name,
                "Response serialization timeout (exceeded 120s) - audit log, webhooks, or cache invalidation likely hung"
            );
            app_state
                .metrics_state
                .record_gateway_insert_window_event("response_serialization_timeout");

            let mut details: Map<String, Value> = Map::new();
            details.insert("table".to_string(), Value::String(job.table_name.clone()));
            details.insert("client".to_string(), Value::String(job.client_name.clone()));
            details.insert(
                "likely_cause".to_string(),
                Value::String("audit_log_or_webhook_delay".to_string()),
            );

            WindowInsertOutcome::Error(InsertError::new(
                StatusCode::INTERNAL_SERVER_ERROR,
                "response_serialization_timeout",
                "Response serialization exceeded maximum time limit (120s); check audit logging, webhooks, and cache invalidation".to_string(),
                job.trace_id.clone(),
                details,
            ))
        }
    }
}

pub(crate) async fn finish_postgres_insert_success_json(
    app_state: Data<AppState>,
    job: &WindowInsertJob,
    inserted_row: Value,
) -> Value {
    let response_started: Instant = Instant::now();
    let diff_resource: Value = build_diff_resource(&job.body, &job.insert_body, &job.user_id);
    let resource_id: String = inserted_row
        .as_object()
        .and_then(|object| object.get(&job.resource_id_key))
        .and_then(Value::as_str)
        .map(str::to_string)
        .unwrap_or_else(|| job.trace_id.clone());

    let audit_message: String = format!(
        "User {} inserted data into [{}] at {}",
        job.user_id,
        job.table_name,
        Utc::now().timestamp()
    );

    let audit_log_started: Instant = Instant::now();
    let audit_log_result: Result<Value, String> = record_insert_audit_log(
        job.table_name.clone(),
        resource_id.clone(),
        job.insert_body.clone(),
        job.company_id.clone(),
        job.organization_id.clone(),
        "domain".to_string(),
        job.user_id.clone(),
        audit_message,
        "success".to_string(),
        "request".to_string(),
        job.user_id.clone(),
        format!("insert-{}", job.table_name),
        diff_resource,
        json!({}),
        true,
        "/data/insert".to_string(),
        &job.client_name,
    )
    .await;
    let audit_log_duration: StdDuration = audit_log_started.elapsed();

    match &audit_log_result {
        Ok(audit_log_data) => {
            if job.verbose_logging {
                let audit_log_message = if job.ansi_enabled {
                    "\u{001b}[32mAudit log inserted successfully\u{001b}[0m (ATHENA_VERBOSE_LOGGING=1)"
                } else {
                    "Audit log inserted successfully (ATHENA_VERBOSE_LOGGING=1)"
                };
                info!(
                    trace_id = %job.trace_id,
                    client = %job.client_name,
                    table = %job.table_name,
                    resource_id = %resource_id,
                    audit_log_duration_ms = audit_log_duration.as_millis(),
                    audit_payload = ?audit_log_data,
                    "{}",
                    audit_log_message
                );
            }
            if audit_log_duration.as_millis() > 5000 {
                warn!(
                    trace_id = %job.trace_id,
                    client = %job.client_name,
                    table = %job.table_name,
                    audit_log_duration_ms = audit_log_duration.as_millis(),
                    "Slow audit log insertion (>5s)"
                );
            }
        }
        Err(err) => {
            error!(
                trace_id = %job.trace_id,
                client = %job.client_name,
                table = %job.table_name,
                audit_log_duration_ms = audit_log_duration.as_millis(),
                error = %err,
                "Failed to insert audit log"
            );
        }
    }

    let success_json: Value = json!({
        "status": "success",
        "success": true,
        "message": "Data inserted successfully",
        "data": inserted_row,
        "resource_id": resource_id,
        "table_name": job.table_name,
    });
    if job.x_publish_event {
        if let Some(company_for_event) = job.resolved_company_for_event.clone() {
            let event: Value = json!({
                "event": "INSERT",
                "resource": job.table_name.clone(),
                "inserted_by_user": job.user_id.clone(),
                "company_id": company_for_event.clone(),
                "insert_body": job.insert_body.clone(),
            });
            post_event(company_for_event, event).await;
        } else {
            info!(
                trace_id = %job.trace_id,
                "Skipping publish event because company_id is missing"
            );
        }
    }

    let logged_request: LoggedRequest = LoggedRequest {
        request_id: job.logged_request_id.clone(),
        client_name: job.logged_client_name.clone(),
        method: job.logged_method.clone(),
        path: job.logged_path.clone(),
        status_code: 0,
        time: 0,
    };
    log_gateway_operation_result(
        Some(app_state.get_ref()),
        &logged_request,
        "insert",
        Some(&job.table_name),
        job.operation_start,
        StatusCode::OK,
        Some(json!({
            "resource_id": resource_id,
            "client": job.client_name,
        })),
    );

    let mut wh_headers = Vec::new();
    if !job.user_id.is_empty() {
        wh_headers.push(("x-user-id".to_string(), job.user_id.clone()));
    }
    let trigger: GatewayWebhookTrigger = GatewayWebhookTrigger {
        client_name: job.client_name.clone(),
        route_key: crate::webhooks::ROUTE_GATEWAY_INSERT.to_string(),
        table_name: Some(job.table_name.clone()),
        request_id: Some(logged_request.request_id.clone()),
        request_method: job.logged_method.clone(),
        request_path: job.logged_path.clone(),
        headers: wh_headers,
        payload: Some(job.body.clone()),
        response: Some(success_json.clone()),
    };

    // Shadow outbox write: persist side-effect intent so the relay can retry on crash.
    write_insert_outbox_shadow(&app_state, &job, &trigger, &success_json).await;

    crate::webhooks::spawn_gateway_webhook_dispatch(app_state.clone(), trigger);

    let response_duration: StdDuration = response_started.elapsed();
    if response_duration.as_millis() > 10000 {
        warn!(
            trace_id = %job.trace_id,
            client = %job.client_name,
            table = %job.table_name,
            response_serialize_duration_ms = response_duration.as_millis(),
            "Slow response serialization phase (>10s) - check audit log, webhooks, cache invalidation"
        );
    } else if response_duration.as_millis() > 5000 {
        info!(
            trace_id = %job.trace_id,
            client = %job.client_name,
            table = %job.table_name,
            response_serialize_duration_ms = response_duration.as_millis(),
            "Response serialization took >5s"
        );
    }

    success_json
}

/// Shadow-write insert side-effect intent to the outbox (logging DB).
/// Non-fatal — failures are warned but don't block the response.
async fn write_insert_outbox_shadow(
    app_state: &AppState,
    job: &WindowInsertJob,
    trigger: &crate::webhooks::GatewayWebhookTrigger,
    response_payload: &Value,
) {
    let Some(logging_client) = app_state.logging_client_name.as_ref() else {
        return;
    };
    let Some(pool) = app_state.pg_registry.get_pool(logging_client) else {
        return;
    };

    let headers = json!({
        "client_name": job.client_name,
        "request_id": job.logged_request_id,
        "company_id": job.resolved_company_for_event.as_deref().unwrap_or(""),
    });

    let mut tx = match pool.begin().await {
        Ok(t) => t,
        Err(err) => {
            warn!(client = %job.client_name, error = %err, "Insert outbox shadow: begin failed");
            return;
        }
    };

    // CDC mutation event (if applicable).
    if job.x_publish_event {
        if let Some(company) = &job.resolved_company_for_event {
            let cdc_payload: Value = json!({
                "event": "INSERT",
                "resource": job.table_name,
                "inserted_by_user": job.user_id,
                "company_id": company,
                "insert_body": job.insert_body,
            });
            let insert = OutboxEventInsert {
                aggregate_type: "gateway".into(),
                aggregate_id: job.table_name.clone(),
                event_type: "mutation.insert".into(),
                payload: cdc_payload,
                headers: headers.clone(),
                available_at: None,
            };
            if let Err(err) = insert_outbox_event_tx(&mut tx, insert).await {
                warn!(client = %job.client_name, error = %err, "Insert outbox shadow: mutation event failed");
            }
        }
    }

    // Webhook trigger event.
    let wh_payload = json!({
        "route_key": trigger.route_key,
        "table_name": trigger.table_name,
        "request_method": trigger.request_method,
        "request_path": trigger.request_path,
        "request_payload": trigger.payload,
        "response_payload": response_payload,
        "headers": trigger.headers,
    });
    let wh_insert = OutboxEventInsert {
        aggregate_type: "gateway".into(),
        aggregate_id: job.table_name.clone(),
        event_type: "webhook.trigger".into(),
        payload: wh_payload,
        headers,
        available_at: None,
    };
    if let Err(err) = insert_outbox_event_tx(&mut tx, wh_insert).await {
        warn!(client = %job.client_name, error = %err, "Insert outbox shadow: webhook event failed");
    }

    if let Err(err) = tx.commit().await {
        warn!(client = %job.client_name, error = %err, "Insert outbox shadow: commit failed");
    }
}

pub(crate) async fn run_postgres_insert_to_outcome(
    app_state: Data<AppState>,
    job: WindowInsertJob,
) -> WindowInsertOutcome {
    let trace_id: String = job.trace_id.clone();
    let table_name: String = job.table_name.clone();
    let client_name: String = job.client_name.clone();
    let user_id: String = job.user_id.clone();
    let metadata_user_id: Option<&str> = job.metadata_user_id.as_deref();
    let metadata_company_id: Option<&str> = job.metadata_company_id.as_deref();
    let metadata_organization_id: Option<&str> = job.metadata_organization_id.as_deref();

    let pool = if let Some(pool) = app_state.pg_registry.get_pool(&client_name) {
        pool
    } else {
        match ensure_catalog_database_client_loaded(app_state.get_ref(), &client_name).await {
            Ok(Some(_)) => {
                let Some(pool) = app_state.pg_registry.get_pool(&client_name) else {
                    let mut details: Map<String, Value> = build_error_metadata(
                        &trace_id,
                        Some(&table_name),
                        &client_name,
                        metadata_user_id,
                        metadata_company_id,
                        metadata_organization_id,
                    );
                    details.insert("unknown_client".to_string(), json!(client_name.clone()));
                    let insert_error = InsertError::new(
                        StatusCode::BAD_REQUEST,
                        "unknown_client",
                        format!("Postgres client '{}' is not configured", client_name),
                        trace_id.clone(),
                        details,
                    );
                    observe_insert_error(
                        app_state.get_ref(),
                        &client_name,
                        Some(&table_name),
                        Some(&job.insert_body),
                        &insert_error,
                    );
                    trace_insert_error(&table_name, &client_name, &user_id, &insert_error);
                    return WindowInsertOutcome::Error(insert_error);
                };
                pool
            }
            Ok(None) | Err(_) => {
                let mut details: Map<String, Value> = build_error_metadata(
                    &trace_id,
                    Some(&table_name),
                    &client_name,
                    metadata_user_id,
                    metadata_company_id,
                    metadata_organization_id,
                );
                details.insert("unknown_client".to_string(), json!(client_name.clone()));
                let insert_error: InsertError = InsertError::new(
                    StatusCode::BAD_REQUEST,
                    "unknown_client",
                    format!("Postgres client '{}' is not configured", client_name),
                    trace_id.clone(),
                    details,
                );
                observe_insert_error(
                    app_state.get_ref(),
                    &client_name,
                    Some(&table_name),
                    Some(&job.insert_body),
                    &insert_error,
                );
                trace_insert_error(&table_name, &client_name, &user_id, &insert_error);
                return WindowInsertOutcome::Error(insert_error);
            }
        }
    };

    if let Some(signature) =
        build_insert_duplicate_signature(&client_name, &table_name, &job.insert_body)
    {
        let dedupe_started: Instant = Instant::now();
        if let Some(cached_constraint) = lookup_recent_unique_violation(&signature) {
            app_state
                .metrics_state
                .record_gateway_insert_window_event("gateway_recent_conflict_cache_hit");
            app_state
                .metrics_state
                .record_gateway_insert_phase_duration(
                    "dedupe_check",
                    dedupe_started.elapsed().as_secs_f64(),
                );
            let insert_error: InsertError = prefilter_unique_violation_error(
                trace_id.clone(),
                &table_name,
                &client_name,
                metadata_user_id,
                metadata_company_id,
                metadata_organization_id,
                cached_constraint.as_deref(),
                "recent_unique_violation_cache_window",
            );
            observe_insert_error(
                app_state.get_ref(),
                &client_name,
                Some(&table_name),
                Some(&job.insert_body),
                &insert_error,
            );
            trace_insert_error(&table_name, &client_name, &user_id, &insert_error);
            return WindowInsertOutcome::Error(insert_error);
        }
        app_state
            .metrics_state
            .record_gateway_insert_window_event("gateway_recent_conflict_cache_miss");
        app_state
            .metrics_state
            .record_gateway_insert_phase_duration(
                "dedupe_check",
                dedupe_started.elapsed().as_secs_f64(),
            );
    }

    let db_insert_started: Instant = Instant::now();
    let db_timeout_ms: u64 = insert_db_timeout_ms(app_state.get_ref());
    let insert_result: Result<Value, InsertDriverError> =
        postgres_insert_with_timeout(&pool, &table_name, &job.insert_body, db_timeout_ms).await;
    app_state
        .metrics_state
        .record_gateway_insert_phase_duration(
            "db_insert",
            db_insert_started.elapsed().as_secs_f64(),
        );

    match insert_result {
        Ok(inserted_row) => {
            log_gateway_insert_success(
                "athena",
                &trace_id,
                &client_name,
                &table_name,
                job.operation_start.elapsed().as_millis(),
                db_insert_started.elapsed().as_millis() as u64,
            );
            finish_postgres_insert_success_json_with_recovery(app_state, &job, inserted_row).await
        }
        Err(driver_error) => {
            let insert_error: InsertError = InsertError::from_driver(
                driver_error,
                Some(&table_name),
                &client_name,
                metadata_user_id,
                metadata_company_id,
                metadata_organization_id,
                trace_id,
            );
            observe_insert_error(
                app_state.get_ref(),
                &client_name,
                Some(&table_name),
                Some(&job.insert_body),
                &insert_error,
            );
            trace_insert_error(&table_name, &client_name, &user_id, &insert_error);
            WindowInsertOutcome::Error(insert_error)
        }
    }
}