halephant-core 0.5.2

A proxy and pooler for Postgres
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
// The `OpenApi` derive macro generates code that uses `for_each`.
#![allow(clippy::needless_for_each)]
use std::sync::Arc;

use arc_swap::ArcSwap;
use axum::Router;
use axum::extract::State;
use axum::response::Json;
use axum::routing::get;
use serde::Serialize;
use tokio::net::TcpListener;
use utoipa::{OpenApi, ToSchema};
use utoipa_scalar::{Scalar, Servable};

use crate::clients::ClientRegistry;
use crate::config::Config;
use crate::pool::PoolManager;
use crate::topology::TopologyManager;

// ---------------------------------------------------------------------------
// Shared state
// ---------------------------------------------------------------------------

#[derive(Clone)]
struct AppState {
    config: Arc<ArcSwap<Config>>,
    pools: Arc<PoolManager>,
    topology: Arc<TopologyManager>,
    clients: Arc<ClientRegistry>,
}

// ---------------------------------------------------------------------------
// OpenAPI
// ---------------------------------------------------------------------------

#[derive(OpenApi)]
#[openapi(
    paths(health, ready, get_pools, get_servers, get_clients, get_queues),
    components(schemas(
        HealthResponse,
        HealthStatus,
        ReadyResponse,
        ClusterReadiness,
        PoolInfo,
        ClusterServers,
        ServerInfo,
        ServerRole,
        ClientInfo,
        ClientStateDto,
        WaitTargetDto,
        WaitRoleDto,
        QueueInfoDto,
    )),
    info(
        title = "Halephant Admin API",
        version = "0.1.0",
        description = "Operational endpoints for health checks and pool introspection."
    )
)]
struct ApiDoc;

// ---------------------------------------------------------------------------
// Response types
// ---------------------------------------------------------------------------

/// Health check response.
#[derive(Serialize, ToSchema)]
struct HealthResponse {
    status: HealthStatus,
}

#[derive(Serialize, ToSchema)]
#[serde(rename_all = "lowercase")]
enum HealthStatus {
    Ok,
}

/// Readiness check response.
#[derive(Serialize, ToSchema)]
struct ReadyResponse {
    ready: bool,
    clusters: Vec<ClusterReadiness>,
}

#[derive(Serialize, ToSchema)]
struct ClusterReadiness {
    name: String,
    has_primary: bool,
}

/// Pool connection statistics.
#[derive(Serialize, ToSchema)]
struct PoolInfo {
    node: String,
    database: String,
    user: String,
    active: u32,
    idle: u32,
    resetting: u32,
}

/// Upstream server status within a cluster.
#[derive(Serialize, ToSchema)]
struct ServerInfo {
    address: String,
    role: ServerRole,
}

/// Upstream server role.
#[derive(Serialize, ToSchema)]
#[serde(rename_all = "lowercase")]
enum ServerRole {
    Primary,
    Replica,
    Unreachable,
}

/// Cluster server topology.
#[derive(Serialize, ToSchema)]
struct ClusterServers {
    cluster: String,
    servers: Vec<ServerInfo>,
}

/// Point-in-time state of a single connected client.
#[derive(Serialize, ToSchema)]
struct ClientInfo {
    /// Stable client identifier within this halephant process.
    id: u64,
    /// Remote address of the client's TCP socket.
    remote: String,
    /// High-level state of the client.
    state: ClientStateDto,
    /// Seconds since the client was accepted.
    uptime_secs: f64,
    /// Seconds the client has been in its current state.
    state_since_secs: f64,
    /// Database name from the client's startup message, if known.
    database: Option<String>,
    /// PostgreSQL user from the client's startup message, if known.
    user: Option<String>,
    /// Populated only while `state` is `waiting` — identifies the
    /// specific role queue the client is blocked on.
    #[serde(skip_serializing_if = "Option::is_none")]
    waiting_for: Option<WaitTargetDto>,
}

/// Client state category as exposed by the admin API.
#[derive(Serialize, ToSchema)]
#[serde(rename_all = "snake_case")]
enum ClientStateDto {
    Negotiating,
    Authenticating,
    Idle,
    InTransaction,
    Waiting,
}

impl From<crate::clients::ClientState> for ClientStateDto {
    fn from(s: crate::clients::ClientState) -> Self {
        use crate::clients::ClientState;
        match s {
            ClientState::Negotiating => Self::Negotiating,
            ClientState::Authenticating => Self::Authenticating,
            ClientState::Idle => Self::Idle,
            ClientState::InTransaction => Self::InTransaction,
            ClientState::Waiting => Self::Waiting,
        }
    }
}

/// The `(database, user, role)` queue key a client is blocked on while
/// waiting for pool capacity.
#[derive(Serialize, ToSchema)]
struct WaitTargetDto {
    database: String,
    user: String,
    role: WaitRoleDto,
}

#[derive(Serialize, ToSchema)]
#[serde(rename_all = "snake_case")]
enum WaitRoleDto {
    Primary,
    Replica,
}

impl From<crate::pool::Routing> for WaitRoleDto {
    fn from(role: crate::pool::Routing) -> Self {
        use crate::pool::Routing;
        match role {
            Routing::Primary => WaitRoleDto::Primary,
            Routing::Replica => WaitRoleDto::Replica,
        }
    }
}

impl From<crate::clients::WaitTarget> for WaitTargetDto {
    fn from(t: crate::clients::WaitTarget) -> Self {
        use crate::clients::WaitRole;
        Self {
            database: t.database,
            user: t.user,
            role: match t.role {
                WaitRole::Primary => WaitRoleDto::Primary,
                WaitRole::Replica => WaitRoleDto::Replica,
            },
        }
    }
}

/// Snapshot of one wait queue: how many clients are currently blocked
/// on this `(database, user, role)` and how long the oldest has been
/// waiting. Only active (non-empty) queues are returned.
#[derive(Serialize, ToSchema)]
struct QueueInfoDto {
    database: String,
    user: String,
    role: WaitRoleDto,
    /// Number of clients currently blocked on this queue.
    depth: u32,
    /// Seconds the oldest queued waiter has been blocked.
    oldest_wait_secs: f64,
}

// ---------------------------------------------------------------------------
// Handlers
// ---------------------------------------------------------------------------

/// Liveness probe. Always returns 200.
#[utoipa::path(
    get,
    path = "/health",
    responses((status = 200, body = HealthResponse)),
    tag = "health"
)]
async fn health() -> Json<HealthResponse> {
    Json(HealthResponse {
        status: HealthStatus::Ok,
    })
}

/// Readiness probe. Returns 200 when all clusters have a discovered primary,
/// 503 otherwise.
#[utoipa::path(
    get,
    path = "/ready",
    responses(
        (status = 200, body = ReadyResponse, description = "All clusters have a primary"),
        (status = 503, body = ReadyResponse, description = "One or more clusters lack a primary")
    ),
    tag = "health"
)]
async fn ready(State(state): State<AppState>) -> (axum::http::StatusCode, Json<ReadyResponse>) {
    let cfg = state.config.load_full();
    let clusters: Vec<ClusterReadiness> = cfg
        .cluster
        .keys()
        .map(|name| {
            let has_primary = state.topology.primary(name).is_some();
            ClusterReadiness {
                name: name.clone(),
                has_primary,
            }
        })
        .collect();

    let all_ready = clusters.iter().all(|c| c.has_primary);
    let status = if all_ready {
        axum::http::StatusCode::OK
    } else {
        axum::http::StatusCode::SERVICE_UNAVAILABLE
    };

    (
        status,
        Json(ReadyResponse {
            ready: all_ready,
            clusters,
        }),
    )
}

/// Pool connection statistics per node, database, and user.
#[utoipa::path(
    get,
    path = "/admin/pools",
    responses((status = 200, body = Vec<PoolInfo>)),
    tag = "admin"
)]
async fn get_pools(State(state): State<AppState>) -> Json<Vec<PoolInfo>> {
    let stats = state
        .pools
        .pool_stats()
        .into_iter()
        .map(|(key, stats)| PoolInfo {
            node: key.node,
            database: key.database,
            user: key.user,
            active: stats.active,
            idle: stats.idle,
            resetting: stats.resetting,
        })
        .collect();
    Json(stats)
}

/// Upstream server topology per cluster.
#[utoipa::path(
    get,
    path = "/admin/servers",
    responses((status = 200, body = Vec<ClusterServers>)),
    tag = "admin"
)]
async fn get_servers(State(state): State<AppState>) -> Json<Vec<ClusterServers>> {
    let cfg = state.config.load_full();
    let result = cfg
        .cluster
        .keys()
        .map(|name| {
            let mut servers = Vec::new();
            if let Some(topo) = state.topology.get(name) {
                if let Some(ref primary) = topo.primary {
                    servers.push(ServerInfo {
                        address: primary.clone(),
                        role: ServerRole::Primary,
                    });
                }
                for replica in &topo.replicas {
                    servers.push(ServerInfo {
                        address: replica.clone(),
                        role: ServerRole::Replica,
                    });
                }
                for node in &topo.unreachable {
                    servers.push(ServerInfo {
                        address: node.clone(),
                        role: ServerRole::Unreachable,
                    });
                }
            }
            ClusterServers {
                cluster: name.clone(),
                servers,
            }
        })
        .collect();
    Json(result)
}

/// Currently connected clients with their state and identity, if known.
#[utoipa::path(
    get,
    path = "/admin/clients",
    responses((status = 200, body = Vec<ClientInfo>)),
    tag = "admin"
)]
async fn get_clients(State(state): State<AppState>) -> Json<Vec<ClientInfo>> {
    let now = std::time::Instant::now();
    let result = state
        .clients
        .snapshot()
        .into_iter()
        .map(|entry| ClientInfo {
            id: entry.id.as_u64(),
            remote: entry.remote.to_string(),
            state: entry.state.into(),
            uptime_secs: now
                .saturating_duration_since(entry.accepted_at)
                .as_secs_f64(),
            state_since_secs: now
                .saturating_duration_since(entry.state_since)
                .as_secs_f64(),
            database: entry.database,
            user: entry.user,
            waiting_for: entry.waiting_for.map(Into::into),
        })
        .collect();
    Json(result)
}

/// Active wait queues — one entry per `(database, user, role)` with
/// current depth and oldest wait duration. Empty queues are omitted so
/// the response only reflects live contention.
#[utoipa::path(
    get,
    path = "/admin/queues",
    responses((status = 200, body = Vec<QueueInfoDto>)),
    tag = "admin"
)]
async fn get_queues(State(state): State<AppState>) -> Json<Vec<QueueInfoDto>> {
    let result = state
        .pools
        .queue_stats()
        .into_iter()
        .map(|q| QueueInfoDto {
            database: q.database,
            user: q.user,
            role: WaitRoleDto::from(q.role),
            depth: q.depth,
            oldest_wait_secs: q.oldest_wait_secs,
        })
        .collect();
    Json(result)
}

// ---------------------------------------------------------------------------
// Server
// ---------------------------------------------------------------------------

/// Build the admin API router.
pub fn router(
    config: Arc<ArcSwap<Config>>,
    pools: Arc<PoolManager>,
    topology: Arc<TopologyManager>,
    clients: Arc<ClientRegistry>,
) -> Router {
    let state = AppState {
        config,
        pools,
        topology,
        clients,
    };

    Router::new()
        .route("/health", get(health))
        .route("/ready", get(ready))
        .route("/admin/pools", get(get_pools))
        .route("/admin/servers", get(get_servers))
        .route("/admin/clients", get(get_clients))
        .route("/admin/queues", get(get_queues))
        .merge(Scalar::with_url("/docs", ApiDoc::openapi()))
        .with_state(state)
}

/// Start the admin HTTP server on the given address.
pub async fn serve(
    addr: &str,
    config: Arc<ArcSwap<Config>>,
    pools: Arc<PoolManager>,
    topology: Arc<TopologyManager>,
    clients: Arc<ClientRegistry>,
) -> std::io::Result<()> {
    let app = router(config, pools, topology, clients);
    let listener = TcpListener::bind(addr).await?;
    tracing::info!(%addr, "admin API listening");
    axum::serve(listener, app).await?;
    Ok(())
}