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
//! Batched writes for `client_statistics`, `client_table_statistics`, and debounced
//! `athena_clients.last_seen_at` updates to avoid hot-row lock contention under high gateway RPS.

use std::collections::{HashMap, HashSet};
use std::time::Duration;

use sqlx::PgPool;
use tokio::sync::mpsc;

/// Configuration for [`ClientStatsBatcher`].
#[derive(Clone, Debug)]
pub struct ClientStatsBatcherConfig {
    pub channel_capacity: usize,
    pub flush_interval: Duration,
    /// Flush immediately when distinct client accumulators exceed this (bounds memory).
    pub flush_max_clients: usize,
}

impl Default for ClientStatsBatcherConfig {
    fn default() -> Self {
        Self {
            channel_capacity: 50_000,
            flush_interval: Duration::from_millis(250),
            flush_max_clients: 2_000,
        }
    }
}

#[derive(Debug)]
enum BatcherCommand {
    RequestDelta {
        client_name: String,
        d_req: i64,
        d_succ: i64,
        d_fail: i64,
        d_cached: i64,
    },
    OperationDelta {
        client_name: String,
        d_op: i64,
    },
    TableDelta {
        client_name: String,
        table_name: String,
        operation: String,
        d_total: i64,
        d_err: i64,
    },
    LastSeen {
        client_name: String,
    },
}

#[derive(Default, Clone)]
struct ClientAccum {
    d_req: i64,
    d_succ: i64,
    d_fail: i64,
    d_cached: i64,
    d_op: i64,
}

#[derive(Default)]
struct BatcherState {
    clients: HashMap<String, ClientAccum>,
    tables: HashMap<(String, String, String), (i64, i64)>,
    last_seen: HashSet<String>,
}

impl BatcherState {
    fn apply(&mut self, cmd: BatcherCommand) {
        match cmd {
            BatcherCommand::RequestDelta {
                client_name,
                d_req,
                d_succ,
                d_fail,
                d_cached,
            } => {
                let e = self.clients.entry(client_name).or_default();
                e.d_req += d_req;
                e.d_succ += d_succ;
                e.d_fail += d_fail;
                e.d_cached += d_cached;
            }
            BatcherCommand::OperationDelta { client_name, d_op } => {
                let e = self.clients.entry(client_name).or_default();
                e.d_op += d_op;
            }
            BatcherCommand::TableDelta {
                client_name,
                table_name,
                operation,
                d_total,
                d_err,
            } => {
                let k = (client_name, table_name, operation);
                let t = self.tables.entry(k).or_insert((0, 0));
                t.0 += d_total;
                t.1 += d_err;
            }
            BatcherCommand::LastSeen { client_name } => {
                self.last_seen.insert(client_name);
            }
        }
    }

    fn len_clients(&self) -> usize {
        self.clients.len() + self.tables.len()
    }

    fn is_empty(&self) -> bool {
        self.clients.is_empty() && self.tables.is_empty() && self.last_seen.is_empty()
    }

    async fn flush_all(&mut self, pool: &PgPool) {
        if self.is_empty() {
            return;
        }

        for (name, acc) in std::mem::take(&mut self.clients) {
            if acc.d_req == 0 && acc.d_op == 0 {
                continue;
            }

            // TODO: raw_query
            if let Err(err) = sqlx::query(
                r#"
                INSERT INTO client_statistics (
                    client_name,
                    total_requests,
                    successful_requests,
                    failed_requests,
                    total_cached_requests,
                    total_operations,
                    last_request_at,
                    last_operation_at
                )
                VALUES ($1, $2, $3, $4, $5, $6,
                    CASE WHEN $7::boolean THEN now() ELSE NULL END,
                    CASE WHEN $8::boolean THEN now() ELSE NULL END
                )
                ON CONFLICT (client_name) DO UPDATE
                SET total_requests = client_statistics.total_requests + EXCLUDED.total_requests,
                    successful_requests = client_statistics.successful_requests
                        + EXCLUDED.successful_requests,
                    failed_requests = client_statistics.failed_requests + EXCLUDED.failed_requests,
                    total_cached_requests = client_statistics.total_cached_requests
                        + EXCLUDED.total_cached_requests,
                    total_operations = client_statistics.total_operations + EXCLUDED.total_operations,
                    last_request_at = CASE
                        WHEN EXCLUDED.total_requests > 0 THEN now()
                        ELSE client_statistics.last_request_at
                    END,
                    last_operation_at = CASE
                        WHEN EXCLUDED.total_operations > 0 THEN now()
                        ELSE client_statistics.last_operation_at
                    END,
                    updated_at = now()
                "#,
            )
            .bind(&name)
            .bind(acc.d_req)
            .bind(acc.d_succ)
            .bind(acc.d_fail)
            .bind(acc.d_cached)
            .bind(acc.d_op)
            .bind(acc.d_req > 0)
            .bind(acc.d_op > 0)
            .execute(pool)
            .await
            {
                tracing::error!(
                    error = %err,
                    client = %name,
                    "client_stats_batcher: flush client_statistics failed"
                );
            }
        }

        for ((client_name, table_name, operation), (d_total, d_err)) in
            std::mem::take(&mut self.tables)
        {
            if d_total == 0 && d_err == 0 {
                continue;
            }

            // TODO: raw_query
            if let Err(err) = sqlx::query(
                r#"
                INSERT INTO client_table_statistics (
                    client_name,
                    table_name,
                    operation,
                    total_operations,
                    error_operations,
                    last_operation_at
                )
                VALUES ($1, $2, $3, $4, $5, now())
                ON CONFLICT (client_name, table_name, operation) DO UPDATE
                SET total_operations = client_table_statistics.total_operations
                    + EXCLUDED.total_operations,
                    error_operations = client_table_statistics.error_operations
                        + EXCLUDED.error_operations,
                    last_operation_at = now(),
                    updated_at = now()
                "#,
            )
            .bind(&client_name)
            .bind(&table_name)
            .bind(&operation)
            .bind(d_total)
            .bind(d_err)
            .execute(pool)
            .await
            {
                tracing::error!(
                    error = %err,
                    client = %client_name,
                    table = %table_name,
                    operation = %operation,
                    "client_stats_batcher: flush client_table_statistics failed"
                );
            }
        }

        let names: Vec<String> = std::mem::take(&mut self.last_seen).into_iter().collect();
        if names.is_empty() {
            return;
        }

        // TODO: raw_query
        if let Err(err) = sqlx::query(
            r#"
            UPDATE athena_clients
            SET last_seen_at = now(),
                updated_at = now()
            WHERE deleted_at IS NULL
              AND lower(client_name) IN (SELECT lower(x) FROM unnest($1::text[]) AS t(x))
            "#,
        )
        .bind(names.as_slice())
        .execute(pool)
        .await
        {
            tracing::error!(
                error = %err,
                clients = ?names,
                "client_stats_batcher: batch last_seen update failed"
            );
        }
    }
}

async fn run_worker(
    mut rx: mpsc::Receiver<BatcherCommand>,
    pool: PgPool,
    config: ClientStatsBatcherConfig,
) {
    let mut tick = tokio::time::interval(config.flush_interval);
    tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
    let mut state = BatcherState::default();

    loop {
        tokio::select! {
            biased;
            cmd = rx.recv() => {
                match cmd {
                    Some(c) => {
                        state.apply(c);
                        if state.len_clients() >= config.flush_max_clients {
                            state.flush_all(&pool).await;
                        }
                    }
                    None => {
                        state.flush_all(&pool).await;
                        return;
                    }
                }
            }
            _ = tick.tick() => {
                state.flush_all(&pool).await;
            }
        }
    }
}

/// Handle for enqueueing client statistics and catalog touches without blocking request paths.
#[derive(Clone)]
pub struct ClientStatsBatcher {
    tx: mpsc::Sender<BatcherCommand>,
}

impl ClientStatsBatcher {
    pub fn spawn(pool: PgPool, config: ClientStatsBatcherConfig) -> Self {
        let cap: usize = config.channel_capacity.max(1);
        let (tx, rx) = mpsc::channel(cap);
        tokio::spawn(run_worker(rx, pool, config));
        Self { tx }
    }

    fn try_send(&self, cmd: BatcherCommand) {
        match self.tx.try_send(cmd) {
            Err(mpsc::error::TrySendError::Full(_)) => {
                tracing::warn!(
                    target: "athena_rs::client_stats_batcher",
                    "client stats batcher channel full; dropping delta"
                );
            }
            Err(mpsc::error::TrySendError::Closed(_)) => {}
            Ok(()) => {}
        }
    }

    pub fn try_enqueue_request_stats(&self, client_name: &str, status_code: i32, cached: bool) {
        let d_succ: i64 = i64::from((200..400).contains(&status_code));
        let d_fail: i64 = i64::from(status_code >= 400);
        let d_cached: i64 = i64::from(cached);
        self.try_send(BatcherCommand::RequestDelta {
            client_name: client_name.to_string(),
            d_req: 1,
            d_succ,
            d_fail,
            d_cached,
        });
    }

    pub fn try_enqueue_operation_stats(&self, client_name: &str) {
        self.try_send(BatcherCommand::OperationDelta {
            client_name: client_name.to_string(),
            d_op: 1,
        });
    }

    pub fn try_enqueue_table_stats(
        &self,
        client_name: &str,
        table_name: &str,
        operation: &str,
        is_error: bool,
    ) {
        self.try_send(BatcherCommand::TableDelta {
            client_name: client_name.to_string(),
            table_name: table_name.to_string(),
            operation: operation.to_string(),
            d_total: 1,
            d_err: i64::from(is_error),
        });
    }

    pub fn try_enqueue_last_seen(&self, client_name: &str) {
        self.try_send(BatcherCommand::LastSeen {
            client_name: client_name.to_string(),
        });
    }
}

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

    #[test]
    fn merge_deltas_accumulates() {
        let mut s: BatcherState = BatcherState::default();
        s.apply(BatcherCommand::RequestDelta {
            client_name: "c1".into(),
            d_req: 1,
            d_succ: 1,
            d_fail: 0,
            d_cached: 0,
        });
        s.apply(BatcherCommand::RequestDelta {
            client_name: "c1".into(),
            d_req: 1,
            d_succ: 0,
            d_fail: 1,
            d_cached: 1,
        });
        s.apply(BatcherCommand::OperationDelta {
            client_name: "c1".into(),
            d_op: 3,
        });
        let acc = s.clients.get("c1").unwrap();
        assert_eq!(acc.d_req, 2);
        assert_eq!(acc.d_succ, 1);
        assert_eq!(acc.d_fail, 1);
        assert_eq!(acc.d_cached, 1);
        assert_eq!(acc.d_op, 3);
    }

    #[test]
    fn last_seen_dedupes_per_flush_batch() {
        let mut s: BatcherState = BatcherState::default();
        s.apply(BatcherCommand::LastSeen {
            client_name: "c".into(),
        });
        s.apply(BatcherCommand::LastSeen {
            client_name: "c".into(),
        });
        assert_eq!(s.last_seen.len(), 1);
    }

    #[test]
    fn table_merge_accumulates() {
        let mut s: BatcherState = BatcherState::default();
        s.apply(BatcherCommand::TableDelta {
            client_name: "c".into(),
            table_name: "t".into(),
            operation: "insert".into(),
            d_total: 1,
            d_err: 0,
        });
        s.apply(BatcherCommand::TableDelta {
            client_name: "c".into(),
            table_name: "t".into(),
            operation: "insert".into(),
            d_total: 1,
            d_err: 1,
        });
        let k = ("c".into(), "t".into(), "insert".into());
        assert_eq!(s.tables.get(&k), Some(&(2, 1)));
    }
}