vl-convert-server 2.0.0-rc2

HTTP server for converting Vega-Lite and Vega specifications to static images
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//! Admin reconfig coordination primitives.
//!
//! Drain completion is based only on admitted requests releasing their
//! [`InflightGuard`] while [`ReconfigCoordinator::gate_closed`] is `true`.
//! Long-lived clones of shared state such as `Arc<RuntimeSnapshot>` or
//! `Arc<AppState>` do not block drain.
//!
//! The admission gate increments `inflight` before rechecking `gate_closed`,
//! ensuring the drain loop cannot observe zero while a request is being
//! admitted.

use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{Mutex, Notify};
use tokio_util::sync::CancellationToken;
use vl_convert_rs::converter::VlcConfig;

use crate::health::ReadinessState;
use crate::types::{ConfigPatch, ConfigValidationError, FieldError, FieldErrorCode};

/// Error produced by [`ReconfigCoordinator::drain`] when the drain window
/// cannot complete cleanly.
#[derive(Debug)]
pub(crate) enum DrainError {
    /// Shutdown token fired before drain finished.
    Cancelled,
    /// Drain deadline exceeded; `inflight` is the count at the moment the
    /// timeout fired.
    Timeout { inflight: usize },
}

/// Coordinates the admin-reconfig lifecycle: admission gate, in-flight
/// counting, drain notification, shutdown integration, and serialization of
/// concurrent admin-mutating requests.
///
/// Held as `Arc<ReconfigCoordinator>` inside [`crate::config::AppState`] and
/// [`crate::admin::AdminState`]; the same `Arc` is shared between the gate
/// middleware (main listener) and the admin handlers (admin listener) so
/// both participate in the same drain domain.
pub(crate) struct ReconfigCoordinator {
    /// Set `true` while a reconfig is draining or rebuilding. Reads by the
    /// gate middleware and drain loop use `SeqCst` so admission and drain
    /// agree on one total order with `inflight`.
    gate_closed: AtomicBool,
    /// Number of admitted, not-yet-completed requests on the gated router.
    /// Incremented by gate middleware at admission, decremented by
    /// `InflightGuard::drop`.
    inflight: AtomicUsize,
    /// Woken on every `inflight` decrement so the drain loop does not have
    /// to poll.
    drained: Notify,
    /// Serializes all admin-mutating endpoints (PATCH, PUT, DELETE, POST
    /// /admin/config/fonts/directories). Last-writer-wins.
    reconfig_lock: Mutex<()>,
    /// Shared with `serve()` so SIGTERM / SIGINT / stdin-EOF aborts a
    /// reconfig mid-flight.
    shutdown_token: CancellationToken,
    /// Absolute deadline for a single drain call.
    drain_timeout: Duration,
}

impl ReconfigCoordinator {
    /// Construct a fresh coordinator. Clone the returned `Arc` into any
    /// state container that needs it.
    pub(crate) fn new(shutdown_token: CancellationToken, drain_timeout: Duration) -> Arc<Self> {
        Arc::new(Self {
            gate_closed: AtomicBool::new(false),
            inflight: AtomicUsize::new(0),
            drained: Notify::new(),
            reconfig_lock: Mutex::new(()),
            shutdown_token,
            drain_timeout,
        })
    }

    /// Acquire the reconfig lock. Returned guard serializes against every
    /// other admin-mutating request. Last-writer-wins.
    pub(crate) async fn lock(&self) -> tokio::sync::MutexGuard<'_, ()> {
        self.reconfig_lock.lock().await
    }

    /// Close the admission gate. New requests hitting the gate middleware
    /// will be rejected with 503 until [`Self::open_gate`] fires.
    pub(crate) fn close_gate(&self) {
        self.gate_closed.store(true, Ordering::SeqCst);
    }

    /// Reopen the admission gate. Called by [`ReconfigScopeGuard::drop`]
    /// on any exit path (success, error, admin-caller disconnect).
    pub(crate) fn open_gate(&self) {
        self.gate_closed.store(false, Ordering::SeqCst);
    }

    #[cfg(test)]
    pub(crate) fn is_gate_closed(&self) -> bool {
        self.gate_closed.load(Ordering::SeqCst)
    }

    #[cfg(test)]
    pub(crate) fn inflight(&self) -> usize {
        self.inflight.load(Ordering::SeqCst)
    }

    /// Admission handshake used by the gate middleware.
    ///
    /// Increments `inflight` first, then rechecks `gate_closed`. If the
    /// gate closed between the bump and this load, decrements and returns
    /// `Err(())` so the middleware can reject the request with 503.
    ///
    /// On success returns an [`InflightGuard`] that decrements and wakes
    /// the drain loop on drop.
    pub(crate) fn try_admit(self: &Arc<Self>) -> Result<InflightGuard, ()> {
        self.inflight.fetch_add(1, Ordering::SeqCst);
        if self.gate_closed.load(Ordering::SeqCst) {
            self.inflight.fetch_sub(1, Ordering::SeqCst);
            self.drained.notify_waiters();
            return Err(());
        }
        Ok(InflightGuard {
            coord: self.clone(),
        })
    }

    /// Close the gate and wait for admitted requests to finish.
    ///
    /// Registers for drain notifications before loading `inflight`, so a
    /// request finishing between checks cannot be missed. The gate remains
    /// closed on both success and error; callers reopen it via
    /// [`ReconfigScopeGuard`].
    pub(crate) async fn drain(&self) -> Result<(), DrainError> {
        self.close_gate();
        let deadline = tokio::time::Instant::now() + self.drain_timeout;

        loop {
            // Register before loading `inflight` so a concurrent decrement
            // cannot be missed.
            let notified = self.drained.notified();
            tokio::pin!(notified);

            if self.inflight.load(Ordering::SeqCst) == 0 {
                return Ok(());
            }

            tokio::select! {
                biased;
                _ = self.shutdown_token.cancelled() => return Err(DrainError::Cancelled),
                _ = notified => continue,
                _ = tokio::time::sleep_until(deadline) => {
                    return Err(DrainError::Timeout {
                        inflight: self.inflight.load(Ordering::SeqCst),
                    });
                }
            }
        }
    }
}

/// Drop guard that decrements [`ReconfigCoordinator::inflight`] and wakes the
/// drain loop when an admitted request exits.
pub(crate) struct InflightGuard {
    coord: Arc<ReconfigCoordinator>,
}

impl Drop for InflightGuard {
    fn drop(&mut self) {
        self.coord.inflight.fetch_sub(1, Ordering::SeqCst);
        self.coord.drained.notify_waiters();
    }
}

/// Scope guard for admin reconfig handlers.
///
/// If the handler marks the gate closed, `Drop` reopens the gate and clears
/// `readiness.reconfig_in_progress` on every exit path.
pub(crate) struct ReconfigScopeGuard<'a> {
    coord: &'a Arc<ReconfigCoordinator>,
    readiness: &'a Arc<ReadinessState>,
    /// Whether the guard should reopen the gate + clear readiness on drop.
    /// The handler sets this to `true` when it calls `close_gate()` (at
    /// the start of the drain/rebuild path); identity short-circuits and
    /// dedicated font-dir endpoints leave it `false`.
    gate_was_closed: bool,
}

impl<'a> ReconfigScopeGuard<'a> {
    /// Create a new scope guard. Call at the start of the admin handler.
    pub(crate) fn new(
        coord: &'a Arc<ReconfigCoordinator>,
        readiness: &'a Arc<ReadinessState>,
    ) -> Self {
        Self {
            coord,
            readiness,
            gate_was_closed: false,
        }
    }

    /// Record that the handler closed the admission gate and marked
    /// `reconfig_in_progress`. Drop will undo both.
    pub(crate) fn mark_gate_closed(&mut self) {
        self.gate_was_closed = true;
        self.readiness
            .reconfig_in_progress
            .store(true, Ordering::Release);
    }
}

impl<'a> Drop for ReconfigScopeGuard<'a> {
    fn drop(&mut self) {
        if self.gate_was_closed {
            self.coord.open_gate();
            self.readiness
                .reconfig_in_progress
                .store(false, Ordering::Release);
        }
    }
}

/// A patch rejection that must be surfaced to the admin caller. Variants
/// determine the HTTP status code: `NonNullable` → 400, `Invalid` → 422.
#[derive(Debug)]
pub(crate) enum PatchRejection {
    /// One or more non-nullable fields received an explicit `null` in the
    /// patch body. Parse-level rejection: the wire shape is illegal.
    NonNullable(ConfigValidationError),
    /// Semantic validation failure from the normalize/rebuild pipeline.
    #[allow(dead_code)]
    Invalid(ConfigValidationError),
}

/// Merge a [`ConfigPatch`] onto a current [`VlcConfig`] snapshot.
///
/// Patch semantics:
/// * Field absent from the patch (outer `None`) → preserve current value.
/// * Field present (`Some(inner)`) → replace current with `inner`.
///
/// For VlcConfig fields whose library type is `Option<T>`, the inner
/// `Option` is stored as-is (so `null` → `None`). For non-optional
/// VlcConfig fields, `null` on the wire is illegal: the corresponding
/// `Option<Option<T>>` arrives as `Some(None)` and becomes a
/// `PatchRejection::NonNullable` (the admin handler maps it to 400).
///
/// Cross-field invariants are checked later by
/// `normalize_converter_config`, which returns a 422
/// `ConfigValidationError`.
pub(crate) fn apply_patch(
    current: &VlcConfig,
    patch: &ConfigPatch,
) -> Result<VlcConfig, PatchRejection> {
    let mut new = current.clone();
    let mut null_fields: Vec<FieldError> = Vec::new();

    // Optional<T> VlcConfig fields (null → None).
    if let Some(v) = patch.max_v8_heap_size_mb.as_ref() {
        new.max_v8_heap_size_mb = *v;
    }
    if let Some(v) = patch.google_font_variant_threshold.as_ref() {
        new.google_font_variant_threshold = *v;
    }
    if let Some(v) = patch.max_v8_execution_time_secs.as_ref() {
        new.max_v8_execution_time_secs = *v;
    }
    if let Some(v) = patch.max_ephemeral_workers.as_ref() {
        new.max_ephemeral_workers = *v;
    }
    if let Some(v) = patch.default_theme.as_ref() {
        new.default_theme = v.clone();
    }
    if let Some(v) = patch.default_format_locale.as_ref() {
        new.default_format_locale = v.clone();
    }
    if let Some(v) = patch.default_time_format_locale.as_ref() {
        new.default_time_format_locale = v.clone();
    }

    // Non-optional VlcConfig fields. Outer Option distinguishes
    // "absent" from "present"; inner None = explicit wire null → reject.
    macro_rules! apply_non_nullable {
        ($field:ident, $apply:expr) => {
            match patch.$field.as_ref() {
                None => {}
                Some(None) => null_fields.push(FieldError {
                    path: stringify!($field).to_string(),
                    code: FieldErrorCode::NonNullable,
                    message: format!("field '{}' is not nullable", stringify!($field),),
                }),
                Some(Some(v)) => $apply(&mut new, v),
            }
        };
    }

    apply_non_nullable!(num_workers, |n: &mut VlcConfig, v: &_| n.num_workers = *v);
    apply_non_nullable!(
        base_url,
        |n: &mut VlcConfig, v: &vl_convert_rs::converter::BaseUrlSetting| {
            n.base_url = v.clone();
        }
    );
    apply_non_nullable!(allowed_base_urls, |n: &mut VlcConfig, v: &Vec<String>| {
        n.allowed_base_urls = v.clone();
    });
    apply_non_nullable!(auto_google_fonts, |n: &mut VlcConfig, v: &bool| {
        n.auto_google_fonts = *v;
    });
    apply_non_nullable!(embed_local_fonts, |n: &mut VlcConfig, v: &bool| {
        n.embed_local_fonts = *v;
    });
    apply_non_nullable!(subset_fonts, |n: &mut VlcConfig, v: &bool| n.subset_fonts =
        *v);
    apply_non_nullable!(missing_fonts, |n: &mut VlcConfig, v: &_| {
        n.missing_fonts = *v;
    });
    apply_non_nullable!(google_fonts, |n: &mut VlcConfig, v: &Vec<_>| {
        n.google_fonts = v.clone();
    });
    apply_non_nullable!(gc_after_conversion, |n: &mut VlcConfig, v: &bool| {
        n.gc_after_conversion = *v;
    });
    apply_non_nullable!(vega_plugins, |n: &mut VlcConfig, v: &Vec<String>| {
        n.vega_plugins = v.clone();
    });
    apply_non_nullable!(
        plugin_import_domains,
        |n: &mut VlcConfig, v: &Vec<String>| {
            n.plugin_import_domains = v.clone();
        }
    );
    apply_non_nullable!(allow_per_request_plugins, |n: &mut VlcConfig, v: &bool| {
        n.allow_per_request_plugins = *v;
    });
    apply_non_nullable!(allow_google_fonts, |n: &mut VlcConfig, v: &bool| {
        n.allow_google_fonts = *v;
    });
    apply_non_nullable!(
        per_request_plugin_import_domains,
        |n: &mut VlcConfig, v: &Vec<String>| {
            n.per_request_plugin_import_domains = v.clone();
        }
    );
    apply_non_nullable!(themes, |n: &mut VlcConfig,
                                 v: &std::collections::HashMap<
        String,
        serde_json::Value,
    >| {
        n.themes = v.clone();
    });

    if !null_fields.is_empty() {
        return Err(PatchRejection::NonNullable(ConfigValidationError {
            error: "null received on non-nullable field(s)".to_string(),
            field_errors: null_fields,
        }));
    }

    Ok(new)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::AtomicU32;

    fn coord_with_timeout(ms: u64) -> Arc<ReconfigCoordinator> {
        ReconfigCoordinator::new(CancellationToken::new(), Duration::from_millis(ms))
    }

    #[tokio::test]
    async fn test_drain_returns_immediately_when_no_inflight() {
        let coord = coord_with_timeout(5_000);
        assert!(matches!(coord.drain().await, Ok(())));
        assert!(coord.is_gate_closed());
    }

    #[tokio::test]
    async fn test_drain_waits_for_guards_to_drop() {
        let coord = coord_with_timeout(5_000);
        let guard = coord.try_admit().expect("gate should be open before drain");
        assert_eq!(coord.inflight(), 1);

        // Spawn the drain; it must block on the inflight guard.
        let c = coord.clone();
        let drain_handle = tokio::spawn(async move { c.drain().await });

        // Give the drain a chance to enter the wait loop.
        tokio::time::sleep(Duration::from_millis(20)).await;
        // try_admit should now fail because gate_closed is set.
        assert!(coord.try_admit().is_err());

        // Dropping the guard lets drain complete.
        drop(guard);
        assert!(matches!(drain_handle.await.unwrap(), Ok(())));
        assert_eq!(coord.inflight(), 0);
    }

    #[tokio::test]
    async fn test_drain_aborts_on_shutdown_cancel() {
        let shutdown = CancellationToken::new();
        let coord = Arc::new(ReconfigCoordinator {
            gate_closed: AtomicBool::new(false),
            inflight: AtomicUsize::new(0),
            drained: Notify::new(),
            reconfig_lock: Mutex::new(()),
            shutdown_token: shutdown.clone(),
            drain_timeout: Duration::from_secs(60),
        });

        // Hold a guard so drain blocks.
        let _guard = coord.try_admit().unwrap();

        let c = coord.clone();
        let drain_handle = tokio::spawn(async move { c.drain().await });

        tokio::time::sleep(Duration::from_millis(20)).await;
        shutdown.cancel();

        let result = drain_handle.await.unwrap();
        assert!(matches!(result, Err(DrainError::Cancelled)));
    }

    #[tokio::test]
    async fn test_drain_returns_timeout_error_when_bounded_time_exceeded() {
        let coord = coord_with_timeout(50);
        let _guard = coord.try_admit().unwrap();

        let start = std::time::Instant::now();
        let result = coord.drain().await;
        let elapsed = start.elapsed();

        match result {
            Err(DrainError::Timeout { inflight }) => {
                assert_eq!(inflight, 1);
            }
            other => panic!("expected Timeout, got {other:?}"),
        }
        assert!(
            elapsed >= Duration::from_millis(40) && elapsed < Duration::from_millis(500),
            "drain returned in {elapsed:?}; expected ~50ms"
        );
    }

    /// Stress-tests the admission handshake against a racing `close_gate`.
    /// N concurrent tasks call `try_admit()`. A drainer closes the gate.
    /// Invariant: every successful admit must also be counted in the drain
    /// loop's inflight read at some point (or rejected cleanly). Neither
    /// case should deadlock or leak a count.
    #[tokio::test]
    async fn test_admission_race_regression() {
        const TASKS: usize = 200;
        let coord = coord_with_timeout(2_000);
        let accepted = Arc::new(AtomicU32::new(0));
        let rejected = Arc::new(AtomicU32::new(0));

        let mut handles = Vec::with_capacity(TASKS);
        for _ in 0..TASKS {
            let c = coord.clone();
            let a = accepted.clone();
            let r = rejected.clone();
            handles.push(tokio::spawn(async move {
                // Stagger admission attempts slightly.
                tokio::task::yield_now().await;
                match c.try_admit() {
                    Ok(_guard) => {
                        a.fetch_add(1, Ordering::SeqCst);
                        // The guard releases on task exit.
                    }
                    Err(()) => {
                        r.fetch_add(1, Ordering::SeqCst);
                    }
                }
            }));
        }

        // Drain closes the gate while admission attempts are in flight.
        let drain_handle = {
            let c = coord.clone();
            tokio::spawn(async move { c.drain().await })
        };

        for h in handles {
            h.await.unwrap();
        }
        let drain_result = drain_handle.await.unwrap();

        // Drain must succeed (all admits' guards were dropped at task exit).
        assert!(
            matches!(drain_result, Ok(()) | Err(DrainError::Timeout { .. })),
            "unexpected drain result: {drain_result:?}"
        );

        // Every task must have either admitted or been rejected.
        let total = accepted.load(Ordering::SeqCst) + rejected.load(Ordering::SeqCst);
        assert_eq!(total as usize, TASKS, "lost admit/reject accounting");

        // After all tasks complete, inflight must be 0 (drop guards fired).
        assert_eq!(coord.inflight(), 0);
    }

    // --- apply_patch -----------------------------------------------------

    use vl_convert_rs::converter::MissingFontsPolicy;

    #[test]
    fn apply_patch_empty_preserves_current() {
        let cur = VlcConfig::default();
        let patch = ConfigPatch::default();
        let new = apply_patch(&cur, &patch).unwrap();
        assert_eq!(new, cur);
    }

    #[test]
    fn apply_patch_sets_non_optional_field() {
        let cur = VlcConfig::default();
        let patch = ConfigPatch {
            auto_google_fonts: Some(Some(true)),
            ..Default::default()
        };
        let new = apply_patch(&cur, &patch).unwrap();
        assert!(new.auto_google_fonts);
        // Other fields unchanged.
        assert_eq!(new.num_workers, cur.num_workers);
    }

    #[test]
    fn apply_patch_sets_option_field_to_some() {
        let cur = VlcConfig::default();
        let patch = ConfigPatch {
            default_theme: Some(Some("dark".to_string())),
            ..Default::default()
        };
        let new = apply_patch(&cur, &patch).unwrap();
        assert_eq!(new.default_theme, Some("dark".to_string()));
    }

    #[test]
    fn apply_patch_clears_option_field_to_none_on_null() {
        // Start from a state with default_theme set, patch with null to clear.
        let cur = VlcConfig {
            default_theme: Some("dark".to_string()),
            ..Default::default()
        };
        let patch = ConfigPatch {
            default_theme: Some(None),
            ..Default::default()
        };
        let new = apply_patch(&cur, &patch).unwrap();
        assert_eq!(new.default_theme, None);
    }

    #[test]
    fn apply_patch_missing_fonts_enum() {
        let cur = VlcConfig::default();
        let patch = ConfigPatch {
            missing_fonts: Some(Some(MissingFontsPolicy::Warn)),
            ..Default::default()
        };
        let new = apply_patch(&cur, &patch).unwrap();
        assert_eq!(new.missing_fonts, MissingFontsPolicy::Warn);
    }

    #[test]
    fn apply_patch_null_on_non_nullable_is_rejected() {
        let cur = VlcConfig::default();
        // `allowed_base_urls: null`: library field is `Vec<String>`, so
        // null is illegal.
        let patch = ConfigPatch {
            allowed_base_urls: Some(None),
            ..Default::default()
        };
        let err = apply_patch(&cur, &patch).unwrap_err();
        match err {
            PatchRejection::NonNullable(e) => {
                assert_eq!(e.field_errors.len(), 1);
                assert_eq!(e.field_errors[0].path, "allowed_base_urls");
                assert_eq!(e.field_errors[0].code, FieldErrorCode::NonNullable);
            }
            PatchRejection::Invalid(_) => panic!("expected NonNullable, got Invalid"),
        }
    }

    #[test]
    fn apply_patch_multiple_nulls_on_non_nullables_collected() {
        let cur = VlcConfig::default();
        let patch = ConfigPatch {
            allowed_base_urls: Some(None),
            subset_fonts: Some(None),
            themes: Some(None),
            ..Default::default()
        };
        let err = apply_patch(&cur, &patch).unwrap_err();
        match err {
            PatchRejection::NonNullable(e) => {
                assert_eq!(e.field_errors.len(), 3);
                let paths: Vec<&str> = e.field_errors.iter().map(|fe| fe.path.as_str()).collect();
                assert!(paths.contains(&"allowed_base_urls"));
                assert!(paths.contains(&"subset_fonts"));
                assert!(paths.contains(&"themes"));
            }
            PatchRejection::Invalid(_) => panic!("expected NonNullable"),
        }
    }
}