linkrs-server 0.1.0

Linkrs network service layer (HTTP/gRPC)
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
use axum::{
    extract::{Extension, Query, State},
    response::Json as JsonResponse,
    Json,
};
use serde::{Deserialize, Serialize};

use crate::http::{error::HttpError, state::AppState};
use crate::storage::{
    StorageClient, StorageOperationContextOps, StorageSchemaContextOps, StorageSyncContextOps,
};

/// Sync status response
#[derive(Debug, Serialize, utoipa::ToSchema)]
pub struct SyncStatusResponse {
    pub is_running: bool,
    pub dlq_size: usize,
    pub unrecovered_dlq_size: usize,
    pub outbox_pending: usize,
    pub outbox_retries: u64,
    pub outbox_dead_lettered: usize,
    pub outbox_leased: usize,
    pub outbox_oldest_event_age_ms: u64,
    pub outbox_write_amplification_bytes: u64,
    pub outbox_lock_wait_nanos: u64,
    pub outbox_persist_operations: u64,
}

#[utoipa::path(
    get,
    operation_id = "get_v1_sync_status",
    path = "/v1/sync/status",
    tag = "Sync",
    responses(
        (status = 200, body = SyncStatusResponse, description = "Sync status"),
        (status = 500, description = "Internal error")
    )
)]
/// Get sync status
pub async fn status<
    S: StorageClient
        + StorageSchemaContextOps
        + StorageSyncContextOps
        + StorageOperationContextOps
        + Clone
        + Send
        + Sync
        + 'static,
>(
    State(state): State<AppState<S>>,
    Extension(session_id): Extension<i64>,
) -> Result<JsonResponse<SyncStatusResponse>, HttpError> {
    super::authz::require_admin_session(&state, session_id)?;
    let graph_service = state.server.get_graph_service();
    let sync_api = graph_service.sync_api();

    if let Some(sync_api) = sync_api {
        let outbox = sync_api.outbox_stats();
        Ok(JsonResponse(SyncStatusResponse {
            is_running: sync_api.is_running(),
            dlq_size: sync_api.get_dlq_size(),
            unrecovered_dlq_size: sync_api.get_unrecovered_dlq_size(),
            outbox_pending: outbox.pending,
            outbox_retries: outbox.retries,
            outbox_dead_lettered: outbox.dead_lettered,
            outbox_leased: outbox.leased,
            outbox_oldest_event_age_ms: outbox.oldest_event_age_ms,
            outbox_write_amplification_bytes: outbox.write_amplification_bytes,
            outbox_lock_wait_nanos: outbox.lock_wait_nanos,
            outbox_persist_operations: outbox.persist_operations,
        }))
    } else {
        // Sync manager not available, return disabled status
        Ok(JsonResponse(SyncStatusResponse {
            is_running: false,
            dlq_size: 0,
            unrecovered_dlq_size: 0,
            outbox_pending: 0,
            outbox_retries: 0,
            outbox_dead_lettered: 0,
            outbox_leased: 0,
            outbox_oldest_event_age_ms: 0,
            outbox_write_amplification_bytes: 0,
            outbox_lock_wait_nanos: 0,
            outbox_persist_operations: 0,
        }))
    }
}

#[utoipa::path(
    post,
    path = "/v1/sync/outbox/retry",
    tag = "Sync",
    responses(
        (status = 200, body = serde_json::Value, description = "Outbox delivery retried"),
        (status = 500, description = "Internal error")
    )
)]
/// Retry delivery of pending durable outbox entries.
pub async fn retry_outbox<
    S: StorageClient
        + StorageSchemaContextOps
        + StorageSyncContextOps
        + StorageOperationContextOps
        + Clone
        + Send
        + Sync
        + 'static,
>(
    State(state): State<AppState<S>>,
    Extension(session_id): Extension<i64>,
) -> Result<JsonResponse<serde_json::Value>, HttpError> {
    super::authz::require_admin_session(&state, session_id)?;
    let graph_service = state.server.get_graph_service();
    let sync_api = graph_service
        .sync_api()
        .ok_or_else(|| HttpError::bad_request("Synchronization is not configured"))?;
    let delivered = sync_api
        .retry_outbox_projection()
        .map_err(HttpError::from)?;
    Ok(JsonResponse(serde_json::json!({
        "delivered": delivered,
        "status": "completed",
    })))
}

#[utoipa::path(
    get,
    path = "/v1/sync/outbox/diagnostics",
    tag = "Sync",
    responses(
        (status = 200, body = serde_json::Value, description = "Outbox diagnostics"),
        (status = 500, description = "Internal error")
    )
)]
/// Diagnostics query for outbox frontiers and degraded state.
pub async fn diagnostics<
    S: StorageClient
        + StorageSchemaContextOps
        + StorageSyncContextOps
        + StorageOperationContextOps
        + Clone
        + Send
        + Sync
        + 'static,
>(
    State(state): State<AppState<S>>,
    Extension(session_id): Extension<i64>,
) -> Result<JsonResponse<serde_json::Value>, HttpError> {
    super::authz::require_admin_session(&state, session_id)?;
    let graph_service = state.server.get_graph_service();
    let sync_api = graph_service
        .sync_api()
        .ok_or_else(|| HttpError::bad_request("Synchronization is not configured"))?;
    let diag = sync_api.sync_diagnostics().map_err(HttpError::from)?;
    Ok(JsonResponse(serde_json::json!(diag)))
}

#[derive(Debug, Deserialize, utoipa::ToSchema)]
pub struct DeadLetterQuery {
    pub target: Option<String>,
    pub index_id: Option<u64>,
    pub generation: Option<u64>,
    pub limit: Option<usize>,
    pub offset: Option<usize>,
}

#[utoipa::path(
    get,
    path = "/v1/sync/outbox/dead_letters",
    tag = "Sync",
    params(
        ("target" = Option<String>, Query, description = "Sync target filter"),
        ("index_id" = Option<u64>, Query, description = "Index id filter"),
        ("generation" = Option<u64>, Query, description = "Generation filter"),
        ("limit" = Option<usize>, Query, description = "Maximum rows returned"),
        ("offset" = Option<usize>, Query, description = "Rows to skip")
    ),
    responses(
        (status = 200, body = serde_json::Value, description = "Dead letter entries"),
        (status = 500, description = "Internal error")
    )
)]
pub async fn dead_letters<
    S: StorageClient
        + StorageSchemaContextOps
        + StorageSyncContextOps
        + StorageOperationContextOps
        + Clone
        + Send
        + Sync
        + 'static,
>(
    State(state): State<AppState<S>>,
    Extension(session_id): Extension<i64>,
    Query(params): Query<DeadLetterQuery>,
) -> Result<JsonResponse<serde_json::Value>, HttpError> {
    super::authz::require_admin_session(&state, session_id)?;
    let graph_service = state.server.get_graph_service();
    let sync_api = graph_service
        .sync_api()
        .ok_or_else(|| HttpError::bad_request("Synchronization is not configured"))?;
    let target = params
        .target
        .as_deref()
        .map(|s| linkrs_core::types::TargetId::new(s.to_string()).map_err(HttpError::bad_request))
        .transpose()?;
    let rows = sync_api
        .list_dead_letters(
            target.as_ref(),
            params.index_id,
            params.generation,
            params.limit.unwrap_or(100),
            params.offset.unwrap_or(0),
        )
        .map_err(HttpError::from)?;
    Ok(JsonResponse(serde_json::json!({ "dead_letters": rows })))
}

#[derive(Debug, Deserialize, Serialize, utoipa::ToSchema)]
pub struct RequeueRequest {
    pub target: Option<String>,
    pub index_id: Option<u64>,
    pub generation: Option<u64>,
    pub limit: Option<usize>,
    pub event_ids: Option<Vec<i64>>,
}

#[utoipa::path(
    post,
    path = "/v1/sync/outbox/requeue",
    tag = "Sync",
    request_body = RequeueRequest,
    responses(
        (status = 200, body = serde_json::Value, description = "Dead letters requeued"),
        (status = 500, description = "Internal error")
    )
)]
pub async fn requeue<
    S: StorageClient
        + StorageSchemaContextOps
        + StorageSyncContextOps
        + StorageOperationContextOps
        + Clone
        + Send
        + Sync
        + 'static,
>(
    State(state): State<AppState<S>>,
    Extension(session_id): Extension<i64>,
    Json(payload): Json<RequeueRequest>,
) -> Result<JsonResponse<serde_json::Value>, HttpError> {
    super::authz::require_admin_session(&state, session_id)?;
    let graph_service = state.server.get_graph_service();
    let sync_api = graph_service
        .sync_api()
        .ok_or_else(|| HttpError::bad_request("Synchronization is not configured"))?;
    if let Some(ids) = payload.event_ids {
        let mut requeued = 0usize;
        for id in ids {
            if sync_api.requeue_dead_letter(id).map_err(HttpError::from)? {
                requeued += 1;
            }
        }
        return Ok(JsonResponse(serde_json::json!({ "requeued": requeued })));
    }
    let target = payload
        .target
        .as_deref()
        .map(|s| linkrs_core::types::TargetId::new(s.to_string()).map_err(HttpError::bad_request))
        .transpose()?;
    let requeued = sync_api
        .requeue_dead_letters_batch(
            target.as_ref(),
            payload.index_id,
            payload.generation,
            payload.limit.unwrap_or(100),
        )
        .map_err(HttpError::from)?;
    Ok(JsonResponse(serde_json::json!({ "requeued": requeued })))
}

#[derive(Debug, Deserialize, utoipa::ToSchema)]
pub struct DegradedQuery {
    pub target: Option<String>,
    pub index_id: Option<u64>,
    pub generation: Option<u64>,
}

#[utoipa::path(
    get,
    path = "/v1/sync/outbox/degraded_ranges",
    tag = "Sync",
    params(
        ("target" = Option<String>, Query, description = "Sync target filter"),
        ("index_id" = Option<u64>, Query, description = "Index id filter"),
        ("generation" = Option<u64>, Query, description = "Generation filter")
    ),
    responses(
        (status = 200, body = serde_json::Value, description = "Degraded ranges"),
        (status = 500, description = "Internal error")
    )
)]
pub async fn degraded_ranges<
    S: StorageClient
        + StorageSchemaContextOps
        + StorageSyncContextOps
        + StorageOperationContextOps
        + Clone
        + Send
        + Sync
        + 'static,
>(
    State(state): State<AppState<S>>,
    Extension(session_id): Extension<i64>,
    Query(params): Query<DegradedQuery>,
) -> Result<JsonResponse<serde_json::Value>, HttpError> {
    super::authz::require_admin_session(&state, session_id)?;
    let graph_service = state.server.get_graph_service();
    let sync_api = graph_service
        .sync_api()
        .ok_or_else(|| HttpError::bad_request("Synchronization is not configured"))?;
    let target = params
        .target
        .as_deref()
        .map(|s| linkrs_core::types::TargetId::new(s.to_string()).map_err(HttpError::bad_request))
        .transpose()?;
    let rows = sync_api
        .list_degraded_ranges(target.as_ref(), params.index_id, params.generation)
        .map_err(HttpError::from)?;
    Ok(JsonResponse(serde_json::json!({ "degraded_ranges": rows })))
}

#[derive(Debug, Deserialize, utoipa::ToSchema)]
pub struct ClearDegradedRequest {
    pub target: String,
    pub index_id: u64,
    pub generation: u64,
    pub start_lsn: u64,
    pub end_lsn: u64,
}

#[utoipa::path(
    post,
    path = "/v1/sync/outbox/degraded/clear",
    tag = "Sync",
    request_body = ClearDegradedRequest,
    responses(
        (status = 200, body = serde_json::Value, description = "Degraded range cleared"),
        (status = 500, description = "Internal error")
    )
)]
pub async fn degraded_clear<
    S: StorageClient
        + StorageSchemaContextOps
        + StorageSyncContextOps
        + StorageOperationContextOps
        + Clone
        + Send
        + Sync
        + 'static,
>(
    State(state): State<AppState<S>>,
    Extension(session_id): Extension<i64>,
    Json(payload): Json<ClearDegradedRequest>,
) -> Result<JsonResponse<serde_json::Value>, HttpError> {
    super::authz::require_admin_session(&state, session_id)?;
    let graph_service = state.server.get_graph_service();
    let sync_api = graph_service
        .sync_api()
        .ok_or_else(|| HttpError::bad_request("Synchronization is not configured"))?;
    let target =
        linkrs_core::types::TargetId::new(payload.target).map_err(HttpError::bad_request)?;
    let cleared = sync_api
        .clear_degraded_range(
            &target,
            payload.index_id,
            payload.generation,
            linkrs_core::types::CommitLsn::new(payload.start_lsn),
            linkrs_core::types::CommitLsn::new(payload.end_lsn),
        )
        .map_err(HttpError::from)?;
    Ok(JsonResponse(serde_json::json!({ "cleared": cleared })))
}

#[derive(Debug, Deserialize, utoipa::ToSchema)]
pub struct RetentionRunRequest {
    pub grace_lsn_distance: Option<u64>,
    pub max_age_ms: Option<u64>,
}

#[utoipa::path(
    post,
    path = "/v1/sync/outbox/retention/run",
    tag = "Sync",
    request_body = RetentionRunRequest,
    responses(
        (status = 200, body = serde_json::Value, description = "Retention pass completed"),
        (status = 500, description = "Internal error")
    )
)]
pub async fn retention_run<
    S: StorageClient
        + StorageSchemaContextOps
        + StorageSyncContextOps
        + StorageOperationContextOps
        + Clone
        + Send
        + Sync
        + 'static,
>(
    State(state): State<AppState<S>>,
    Extension(session_id): Extension<i64>,
    Json(payload): Json<RetentionRunRequest>,
) -> Result<JsonResponse<serde_json::Value>, HttpError> {
    super::authz::require_admin_session(&state, session_id)?;
    let graph_service = state.server.get_graph_service();
    let sync_api = graph_service
        .sync_api()
        .ok_or_else(|| HttpError::bad_request("Synchronization is not configured"))?;
    let grace = payload.grace_lsn_distance.unwrap_or(10_000);
    let max_age = payload.max_age_ms.unwrap_or(86_400_000 * 30);
    let (pruned, archived, retention_lsn) = sync_api
        .run_retention_once(grace, max_age)
        .map_err(HttpError::from)?;
    Ok(JsonResponse(serde_json::json!({
        "pruned": pruned,
        "archived": archived,
        "retention_lsn": retention_lsn,
    })))
}

#[utoipa::path(
    get,
    path = "/v1/sync/outbox/retention/status",
    tag = "Sync",
    responses(
        (status = 200, body = serde_json::Value, description = "Retention watermark"),
        (status = 500, description = "Internal error")
    )
)]
pub async fn retention_status<
    S: StorageClient
        + StorageSchemaContextOps
        + StorageSyncContextOps
        + StorageOperationContextOps
        + Clone
        + Send
        + Sync
        + 'static,
>(
    State(state): State<AppState<S>>,
    Extension(session_id): Extension<i64>,
) -> Result<JsonResponse<serde_json::Value>, HttpError> {
    super::authz::require_admin_session(&state, session_id)?;
    let graph_service = state.server.get_graph_service();
    let sync_api = graph_service
        .sync_api()
        .ok_or_else(|| HttpError::bad_request("Synchronization is not configured"))?;
    let retention = sync_api.retention_lsn().map_err(HttpError::from)?;
    Ok(JsonResponse(
        serde_json::json!({ "retention_lsn": retention.get() }),
    ))
}