← Back to Pull requests

feat(provider): store snapshots in a sparse format #9

Open
unknown opened this pull request with 540 lines diff across 6 files
View on GitHub
unknown unknown commented on Nov 07, 2026 06:47 UTC
Author

Snapshot files, above all a microVM's guest memory image (memory-ranges), are mostly holes. Today UploadSnapshot reads them as zeros and FetchSnapshot writes those zeros back with io.Copy, so each file crosses Azure and lands on disk at its full logical size. A Nov 6 benchmark (3,000 glutton agents, 256 MiB, AKS) measured ~268 MB of Blob egress per wake (exactly 256 MiB) and restored memory-ranges files allocated at ≥244 MB p95, against ~98 MB from local snapshots. Disk busy and iowait climbed with wake rate. The benchmark below shows that keeping snapshots sparse removes that I/O, the checkpoint slowdown and the capacity failures that followed.

  • Format (internal/provider/sparse.go): each blob is a header (AZSPARSE, version, logical size, frame count) followed by one (offset, length, data) frame per populated range, found with SEEK_DATA/SEEK_HOLE. It is uncompressed. A restore holds one frame's position at a time, so a crafted blob can't inflate memory. Modeled on substrate's in-tree sparse-extent format (pkg/objectstorage/sparsezstd.go), minus zstd; not compatible with it.

  • Fetch writes the frames into a new temporary file of the logical size, so the holes stay unallocated. It verifies the SHA-256 of the blob's bytes before renaming into place. The strict decoder returns DataLoss on anything malformed.

  • Metadata substrate_format=sparse-v1 picks the decoder. Version-specific code is suffixed V1, so a later format (for example zstd) adds a decoder and a case in decoderFor. Copy keeps the metadata without decoding and refuses blobs without it.

  • Error codes: wrapped EOFs from the Azure body (dropped connections) stay Unavailable in fetch, buffered copy and ranged copy. The two copy paths were already misclassifying these before this change.

  • Tests cover byte equality plus allocated-size checks, all-hole, trailing-hole, unaligned, empty and dense files, 21 malformed-blob cases, and an Azurite round trip of a 320 MiB file holding 80 MiB of data.

  • The README Compatibility section is updated for substrate v1.0.0.
  • Not included: compression, parallel ranged download, server-side copy, fsync changes.

    Benchmark

    Substrate v1.0.0 on AKS westus3: 12 × Standard_D16ds_v7, 3,000 glutton agents (256 MiB, ½ vCPU), every wake restored from Blob. Wake rate 10 → 20 → 30/s, measuring the last 4 minutes of each step. main is 5639af0; this PR is c3a4e52, the same code as the head (the later commit is docs only). One run each, on fresh clusters and storage accounts.

    | | main | this PR |
    |---|---|---|
    | Blob bytes per wake / per suspend | ~266 MB / ~266 MB | ~78 MB / ~78 MB |
    | memory-ranges allocated, p95 (histogram) | ~488 MB | ~98 MB |
    | Suspend p95 at 10 / 20 / 30 per s | 1.1 / 12 / 19 s | 0.21 / 0.24 / 0.41 s |
    | worker_checkpoint p95 at 20 / 30 per s | 19.3 / 19.6 s | 0.24 / 0.44 s |
    | Resume p95 at 10 / 20 / 30 per s | 2.5 / 6.6 / 6.1 s | 1.3 / 1.4 / 1.5 s |
    | Succeeded at the 30/s step | 17.9/s, 39.9% failed (no worker has room) | 30.1/s, 0 failures |
    | Node disk busy / iowait at 30/s (median) | 67% / 37% | 45% / 6% |

    Restored guests stay sparse, so their next snapshots stay at the populated size (~76 MB) instead of 256 MiB. Checkpoints stop writing full guest memory, suspends finish in well under a second, and worker slots free quickly enough to remove the capacity failures. Resume is now mostly the single-stream download (p95 ~2.2–2.4 s for ~76 MB), which parallel ranged downloads could address in a follow-up.

    Follow-ups

  • #10: download snapshot blobs with parallel ranged reads (resume is now mostly the single-stream download).

  • #11: add a zstd-compressed snapshot format (sparse-v2).
  • AI use: AI-assisted (GitHub Copilot CLI); I've reviewed the diff.

    Breaking change: blobs written before this change have no format metadata and fail FetchSnapshot / CopySnapshot with DataLoss. The plugin has no released users yet; on upgrade, start from empty snapshot containers and upgrade every node and control sidecar together. Requesting the breaking-change label.

  • Tests pass (make verify, go test -race ./..., make test-integration)

  • Docs updated
  • Diff: 540 lines (+405 / -135) across 6 files Reviewer: aramase Review duration: 7353m
    All Vouch review confidence checks passed
    Residual risk 0.26 = Change Risk (0.48) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 540 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.377
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #9"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48.0% (MEDIUM) · Higher = larger blast radius
    48.0%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.6% · Safe to merge
    25.6%