← Back to Pull requests

objectfs: SQLite as the controller's metadata store #152

Closed
unknown closed this pull request without merging on Oct 03, 2026 17:01 UTC
View on GitHub
unknown unknown commented on Oct 03, 2026 15:10 UTC
Author

Summary

This PR implements SQLite as the ObjectFS controller's metadata store behind the --metadata-store=legacy|sqlite flag (defaulting to legacy for now), backed by pkg/sds/projection/sqlite.

Key Changes & Optimizations

1. Cached MessageType Resolution (pkg/sds/record):
- Cached resolved protoreflect.MessageType per type ID in record.Registry keyed by type fingerprint, eliminating repetitive descriptor re-serialization, hashing, and dynamicpb construction.
- DB.Get drops from ~50 µs to ~1.4–10 µs.

2. Point Lookups & Direct Directory Operations (pkg/objectfs/controller):
- In SQLite mode, Lookup, CreateFile, Mkdir, Unlink, Rmdir, and Rename perform primary-key lookups (Get) of DirEntry(parent_ino, name) instead of scanning whole directories into memory.
- For Rmdir's emptiness check, uses ScanLimit(ctx, typeName, prefixBytes, 1).
- ReadDir is the only operation that enumerates directory rows.
- Completely eliminates the $O(N)$ directory scaling penalty (adding to a 10k-entry directory takes ~1.3 ms, same as an empty directory).

3. Pragmas via DSN & Exclusive Locking (pkg/sds/projection/sqlite):
- Configured pragmas directly in DSN so modernc applies them to every connection:
path + "?_pragma=locking_mode(EXCLUSIVE)&_pragma=journal_mode(WAL)&_pragma=synchronous(NORMAL)&_pragma=busy_timeout(5000)"
- Single connection pool via sqlDB.SetMaxOpenConns(1) with locking_mode=EXCLUSIVE, keeping the WAL index in memory without POSIX advisory locks on -shm.

4. Prepared Statement Caching & Hot-Path Contexts:
- Prepares and caches *sql.Stmt per table for Get, Insert/Replace, Delete, Scan, ScanLimit, and Count.
- Strips cancellable context wrappers via context.WithoutCancel(ctx) on database hot paths to eliminate modernc's interruptOnDone goroutines.

5. Startup & Snapshot Recovery Hierarchy:
- Local SQLite DB: Reopened at position $P$ with stream replay from position $P$.
- Published SQLite Snapshot: Restored via sqlite.RestoreSnapshot with stream replay from snapshot position.
- EROFS Snapshot: Loaded and imported into SQLite via importErofsToSQLiteLocked with stream replay.
- Snapshot publishing during flush via sqlite.PublishSnapshot (VACUUM INTO).

6. End-to-End & POSIX Gates:
- Added -mode=ops to tests/bench/benchmark.go for FUSE end-to-end benchmarking.
- Configured dev/tasks/fuse-start and dev/ci/presubmits/ap-posix to test both legacy and sqlite stores in CI.

---

Benchmark Comparison (Before vs After Optimization)

Single-Client Latency

| Operation | Legacy (In-Memory LRU) | SQLite (Initial PR) | SQLite (Optimized) | Speedup vs Initial |
| :--- | :--- | :--- | :--- | :--- |
| Stat (GetAttr) | 0.34 µs | 126.4 µs | 10.6 µs | 12x faster |
| Lookup | 0.41 µs | 375.9 µs | 15.9 µs | 23x faster |
| ReadDir (100 entries) | 27.7 µs | 12.8 ms | 0.70 ms | 18x faster |
| Rename | 0.82 ms | 2.24 ms | 1.65 ms | 1.4x faster |
| Create | 0.82 ms | 2.02 ms | 1.68 ms | 1.2x faster |
| 64 KiB fsynced write | 1.15 ms | 2.53 ms | 1.85 ms | 1.4x faster |

Directory Entry Addition Scaling

| Scenario | Legacy Store | SQLite (Initial PR) | SQLite (Optimized) |
| :--- | :--- | :--- | :--- |
| Add to empty directory | 0.77 ms | 1.45 ms | 1.45 ms |
| Add to 10k entry directory | 0.99 ms | 11.23 ms | 1.42 ms |

Scale & Cache-Miss Latency (Exceeding Legacy 10,000 Inode LRU)

When the dataset exceeds the legacy in-memory cache capacity (tested with 15,000 files across directories):

| Scenario | Legacy Store (Disk/EROFS Miss) | SQLite Store | SQLite Advantage |
| :--- | :--- | :--- | :--- |
| Stat 1,000 evicted inodes | 353.4 ms (353.4 µs/op) | 8.7 ms (8.7 µs/op) | 40x faster |
| Lookup 10 dirs | 386.3 µs | 25.5 µs | 15x faster |
| Memory (HeapAlloc) | 16,998 KB | 7,599 KB | 55% less memory |

Cold Start Recovery Time (First Request Served)

  • From local SQLite file: 8.50 ms

  • From published SQLite snapshot: 14.91 ms

  • From EROFS snapshot import: 147.78 ms
  • ---

    Fixes #109

    Diff: 532 lines (+399 / -133) across 6 files Reviewer: justinsb Review duration: 111m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 532 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    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.376
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #152"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% 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: 47.9% (MEDIUM) · Higher = larger blast radius
    47.9%
    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: 60.5% · Lower = rushed or shallow review
    60.5%
    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: 18.9% · Safe to merge
    18.9%