Repository: benbjohnson/litestream Branch: main Commit: ae88b164dd63 Files: 265 Total size: 3.0 MB Directory structure: gitextract_ajbk2a5_/ ├── .aiexclude ├── .claude/ │ ├── agents/ │ │ ├── ltx-compaction-specialist.md │ │ ├── performance-optimizer.md │ │ ├── replica-client-developer.md │ │ ├── sqlite-expert.md │ │ └── test-engineer.md │ ├── commands/ │ │ ├── add-storage-backend.md │ │ ├── analyze-ltx.md │ │ ├── debug-ipc.md │ │ ├── debug-wal.md │ │ ├── fix-common-issues.md │ │ ├── run-comprehensive-tests.md │ │ ├── test-compaction.md │ │ ├── trace-replication.md │ │ └── validate-replica.md │ └── settings.json ├── .github/ │ ├── ISSUE_TEMPLATE/ │ │ ├── bug_report.md │ │ ├── config.yml │ │ └── feature_request.md │ ├── pull_request_template.md │ └── workflows/ │ ├── commit.yml │ ├── integration-tests.yml │ ├── manual-integration-tests.yml │ ├── pr-metrics.yml │ ├── pre-release-checklist.yml │ ├── release.docker.yml │ ├── release.yml │ ├── stale-issues.yml │ └── upgrade-tests.yml ├── .gitignore ├── .goreleaser.yml ├── .markdownlint.json ├── .pre-commit-config.yaml ├── AGENTS.md ├── AI_PR_GUIDE.md ├── CONTRIBUTING.md ├── Dockerfile ├── GEMINI.md ├── LICENSE ├── Makefile ├── README.md ├── SECURITY.md ├── _examples/ │ └── library/ │ ├── README.md │ ├── basic/ │ │ └── main.go │ ├── library_example_test.go │ └── s3/ │ └── main.go ├── abs/ │ └── replica_client.go ├── cmd/ │ ├── litestream/ │ │ ├── databases.go │ │ ├── directory_watcher.go │ │ ├── directory_watcher_stress_test.go │ │ ├── directory_watcher_test.go │ │ ├── info.go │ │ ├── info_test.go │ │ ├── list.go │ │ ├── list_test.go │ │ ├── ltx.go │ │ ├── ltx_test.go │ │ ├── main.go │ │ ├── main_notwindows.go │ │ ├── main_test.go │ │ ├── main_windows.go │ │ ├── mcp.go │ │ ├── register.go │ │ ├── register_test.go │ │ ├── replicate.go │ │ ├── replicate_test.go │ │ ├── reset.go │ │ ├── restore.go │ │ ├── restore_test.go │ │ ├── start.go │ │ ├── status.go │ │ ├── status_test.go │ │ ├── stop.go │ │ ├── sync.go │ │ ├── sync_test.go │ │ ├── unregister.go │ │ ├── unregister_test.go │ │ └── version.go │ ├── litestream-test/ │ │ ├── README.md │ │ ├── S3-RETENTION-TESTING.md │ │ ├── load.go │ │ ├── main.go │ │ ├── populate.go │ │ ├── scripts/ │ │ │ ├── README.md │ │ │ ├── reproduce-critical-bug.sh │ │ │ ├── test-754-restore-focus.sh │ │ │ ├── test-754-s3-scenarios.sh │ │ │ ├── test-format-isolation.sh │ │ │ ├── test-massive-upgrade.sh │ │ │ ├── test-quick-format-check.sh │ │ │ ├── test-s3-access-point.sh │ │ │ ├── test-s3-retention-cleanup.sh │ │ │ ├── test-s3-retention-comprehensive.sh │ │ │ ├── test-s3-retention-large-db.sh │ │ │ ├── test-s3-retention-small-db.sh │ │ │ ├── test-simple-754-reproduction.sh │ │ │ ├── test-upgrade-large-db.sh │ │ │ ├── test-upgrade-v0.3-to-v0.5.sh │ │ │ ├── test-v0.5-flag-reproduction.sh │ │ │ ├── test-v0.5-restart-scenarios.sh │ │ │ └── verify-test-setup.sh │ │ ├── shrink.go │ │ └── validate.go │ └── litestream-vfs/ │ ├── chaos_test.go │ ├── fuzz_test.go │ ├── hydration_e2e_test.go │ ├── main.go │ ├── main_test.go │ ├── stress_test.go │ ├── time_travel_test.go │ ├── vfs_soak_test.go │ └── vfs_write_integration_test.go ├── compaction_level.go ├── compactor.go ├── compactor_test.go ├── db.go ├── db_internal_test.go ├── db_shutdown_test.go ├── db_test.go ├── docker-compose.test.yml ├── docs/ │ ├── ARCHITECTURE.md │ ├── DOC_MAINTENANCE.md │ ├── LTX_FORMAT.md │ ├── PATTERNS.md │ ├── PENDING_USER_DOCS.md │ ├── PROVIDER_COMPATIBILITY.md │ ├── REPLICA_CLIENT_GUIDE.md │ ├── SQLITE_INTERNALS.md │ ├── TESTING_GUIDE.md │ └── VFS.md ├── etc/ │ ├── build.ps1 │ ├── gon-sign.hcl │ ├── gon.hcl │ ├── litestream.service │ ├── litestream.wxs │ ├── litestream.yml │ ├── nfpm.yml │ ├── run-s3-docker-tests.sh │ └── s3_mock.py ├── file/ │ ├── replica_client.go │ └── replica_client_test.go ├── go.mod ├── go.sum ├── grafana/ │ ├── README.md │ └── litestream-dashboard.json ├── gs/ │ ├── replica_client.go │ └── replica_client_test.go ├── heartbeat.go ├── heartbeat_test.go ├── internal/ │ ├── hexdump.go │ ├── internal.go │ ├── internal_unix.go │ ├── internal_windows.go │ ├── limit_read_closer.go │ ├── lock_unix.go │ ├── lock_windows.go │ ├── resumable_reader.go │ ├── resumable_reader_test.go │ └── testingutil/ │ └── testingutil.go ├── leaser.go ├── litestream.go ├── litestream_test.go ├── llms.txt ├── log.go ├── mock/ │ └── replica_client.go ├── nats/ │ ├── replica_client.go │ └── replica_client_test.go ├── oss/ │ ├── replica_client.go │ └── replica_client_test.go ├── packages/ │ ├── npm/ │ │ ├── litestream-vfs/ │ │ │ ├── README.md │ │ │ ├── index.js │ │ │ └── package.json │ │ ├── litestream-vfs-darwin-amd64/ │ │ │ ├── README.md │ │ │ └── package.json │ │ ├── litestream-vfs-darwin-arm64/ │ │ │ ├── README.md │ │ │ └── package.json │ │ ├── litestream-vfs-linux-amd64/ │ │ │ ├── README.md │ │ │ └── package.json │ │ └── litestream-vfs-linux-arm64/ │ │ ├── README.md │ │ └── package.json │ ├── python/ │ │ ├── MANIFEST.in │ │ ├── README.md │ │ ├── litestream_vfs/ │ │ │ ├── __init__.py │ │ │ └── noop.c │ │ ├── scripts/ │ │ │ └── rename_wheel.py │ │ └── setup.py │ └── ruby/ │ ├── README.md │ ├── lib/ │ │ └── litestream_vfs.rb │ └── litestream-vfs.gemspec ├── replica.go ├── replica_client.go ├── replica_client_test.go ├── replica_internal_test.go ├── replica_test.go ├── replica_url.go ├── replica_url_test.go ├── restore_fuzz_test.go ├── s3/ │ ├── leaser.go │ ├── leaser_test.go │ ├── replica_client.go │ └── replica_client_test.go ├── scripts/ │ ├── README.md │ ├── analyze-test-results.sh │ ├── run-upgrade-tests.sh │ └── setup-homebrew-tap.sh ├── server.go ├── server_test.go ├── sftp/ │ └── replica_client.go ├── skills/ │ └── litestream/ │ ├── SKILL.md │ ├── references/ │ │ ├── ARCHITECTURE.md │ │ ├── LTX_FORMAT.md │ │ ├── PATTERNS.md │ │ ├── REPLICA_CLIENT_GUIDE.md │ │ ├── SQLITE_INTERNALS.md │ │ └── TESTING_GUIDE.md │ └── scripts/ │ └── validate-setup.sh ├── src/ │ ├── litestream-vfs.c │ ├── sqlite3.h │ ├── sqlite3ext.h │ └── sqlite3vfs.h ├── store.go ├── store_compaction_remote_test.go ├── store_test.go ├── testdata/ │ ├── db/ │ │ ├── enforce-retention/ │ │ │ └── database │ │ └── write-snapshot-to/ │ │ └── database │ └── wal-reader/ │ ├── frame-checksum-mismatch/ │ │ └── wal │ ├── frame-salts/ │ │ └── wal │ ├── ok/ │ │ └── wal │ └── salt-mismatch/ │ └── wal ├── tests/ │ ├── cpu-usage/ │ │ ├── README.md │ │ ├── litestream-test-polling.yml │ │ └── test-cpu-usage.sh │ └── integration/ │ ├── README.md │ ├── boundary_test.go │ ├── compatibility_test.go │ ├── comprehensive_soak_test.go │ ├── concurrent_test.go │ ├── directory_watcher_helpers.go │ ├── directory_watcher_test.go │ ├── docker_helpers.go │ ├── fixtures.go │ ├── helpers.go │ ├── minio_soak_test.go │ ├── overnight_s3_soak_test.go │ ├── overnight_test.go │ ├── profile_test.go │ ├── quick_test.go │ ├── s3_access_point_test.go │ ├── s3_restore_connection_drop_test.go │ ├── scenario_test.go │ ├── shutdown_retry_test.go │ ├── soak_helpers.go │ ├── supabase-s3/ │ │ ├── docker-compose.yml │ │ └── test.sh │ └── upgrade_test.go ├── v3.go ├── v3_test.go ├── vfs.go ├── vfs_compaction_test.go ├── vfs_test.go ├── vfs_write_test.go ├── wal_reader.go ├── wal_reader_test.go └── webdav/ ├── replica_client.go └── replica_client_test.go ================================================ FILE CONTENTS ================================================ ================================================ FILE: .aiexclude ================================================ # .aiexclude - Files to exclude from Gemini Code Assist # This file works like .gitignore for AI context # Sensitive files *.key *.pem *.secret .env .env.* # Build artifacts bin/ dist/ *.exe *.dll *.so *.dylib # Test databases *.db *.db-wal *.db-shm *.sqlite *.sqlite-wal *.sqlite-shm # Large test files testdata/large/ *.ltx # Vendor directories vendor/ # Generated files *.pb.go *_generated.go # Documentation that's redundant with AGENTS.md docs/RELEASE.md # CI/CD configs that aren't relevant for code understanding .github/workflows/release.yml .goreleaser.yml # Temporary and backup files *.tmp *.bak *.swp *~ # OS-specific files .DS_Store Thumbs.db ================================================ FILE: .claude/agents/ltx-compaction-specialist.md ================================================ --- role: LTX Format and Compaction Specialist tools: - read - write - edit - grep - bash priority: high --- # LTX Compaction Specialist Agent You are an expert in the LTX (Log Transaction) format and multi-level compaction strategies for Litestream. ## Core Knowledge ### LTX File Format ``` ┌─────────────────────┐ │ Header │ 84 bytes ├─────────────────────┤ │ Page Frames │ Variable ├─────────────────────┤ │ Page Index │ Binary search structure ├─────────────────────┤ │ Trailer │ 16 bytes └─────────────────────┘ ``` ### File Naming Convention ``` MMMMMMMMMMMMMMMM-NNNNNNNNNNNNNNNN.ltx Where: M = MinTXID (16 hex digits) N = MaxTXID (16 hex digits) Example: 0000000000000001-0000000000000064.ltx ``` ## Default Compaction Levels ### Level Structure ``` Level 0: Raw (no compaction) Level 1: 30-second windows Level 2: 5-minute windows Level 3: 1-hour windows Snapshots: Daily full database ``` ### Critical Compaction Rules 1. **ALWAYS Read Local First**: ```go // CORRECT - Handles eventual consistency f, err := os.Open(db.LTXPath(info.Level, info.MinTXID, info.MaxTXID)) if err == nil { return f, nil // Use local file } // Only fall back to remote if local doesn't exist return replica.Client.OpenLTXFile(...) ``` 2. **Preserve Timestamps**: ```go // Keep earliest CreatedAt info, err := replica.Client.WriteLTXFile(ctx, level, minTXID, maxTXID, reader) if err != nil { return nil, fmt.Errorf("write ltx file: %w", err) } info.CreatedAt = oldestSourceFile.CreatedAt ``` 3. **Skip Lock Page**: ```go if pgno == ltx.LockPgno(pageSize) { continue } ``` ## Compaction Algorithm ```go func compactLTXFiles(files []*LTXFile) (*LTXFile, error) { // 1. Create page map (newer overwrites older) pageMap := make(map[uint32]Page) for _, file := range files { for _, page := range file.Pages { pageMap[page.Number] = page } } // 2. Create new LTX with merged pages merged := <XFile{ MinTXID: files[0].MinTXID, MaxTXID: files[len(files)-1].MaxTXID, } // 3. Add pages in order (skip lock page!) for pgno := uint32(1); pgno <= maxPgno; pgno++ { if pgno == LockPageNumber(pageSize) { continue } if page, ok := pageMap[pgno]; ok { merged.Pages = append(merged.Pages, page) } } return merged, nil } ``` ## Key Properties ### Immutability - LTX files are NEVER modified after creation - New changes create new files - Compaction creates new merged files ### Checksums - CRC-64 ECMA for integrity - `PreApplyChecksum`/`PostApplyChecksum` on the header/trailer bracketing file state - `FileChecksum` covering the entire file contents ### Page Index - Exposed via `ltx.DecodePageIndex` - Tracks page number plus offset/size of the encoded payload - Located by seeking from the end of the file using trailer metadata ## Common Issues 1. **Partial Reads**: Remote storage may return incomplete files 2. **Race Conditions**: Multiple compactions running 3. **Timestamp Loss**: Not preserving original CreatedAt 4. **Lock Page**: Including 1GB lock page in compacted files 5. **Memory Usage**: Loading entire files for compaction 6. **Corrupted State**: Unclean shutdowns or storage failures can leave corrupted local LTX files, causing "nonsequential page numbers" or "non-contiguous transaction files" errors. Recovery: `litestream reset ` (manual) or `auto-recover: true` replica config (automatic). See `cmd/litestream/reset.go` and `replica.go` ## Testing ```bash # Test compaction go test -v -run TestStore_CompactDB ./... # Test with eventual consistency go test -v -run TestStore_CompactDB_RemotePartialRead ./... # Manual inspection litestream ltx /path/to/db.sqlite # For deeper inspection use the Go API (ltx.NewDecoder) ``` ## References - docs/LTX_FORMAT.md - Complete format specification - store.go - Compaction scheduling - db.go - Compaction implementation - github.com/superfly/ltx - LTX library ================================================ FILE: .claude/agents/performance-optimizer.md ================================================ --- role: Performance Optimizer tools: - read - write - edit - bash - grep priority: medium --- # Performance Optimizer Agent You specialize in optimizing Litestream for speed, memory usage, and resource efficiency. ## Key Performance Areas ### O(n) Operations to Optimize 1. **Page Iteration** ```go // Cache page index const DefaultEstimatedPageIndexSize = 32 * 1024 // 32KB // Fetch end of file first for page index offset := info.Size - DefaultEstimatedPageIndexSize if offset < 0 { offset = 0 } ``` 2. **File Listing** ```go // Cache file listings type FileCache struct { files []FileInfo timestamp time.Time ttl time.Duration } ``` 3. **Compaction** ```go // Limit concurrent compactions sem := make(chan struct{}, maxConcurrentCompactions) ``` ## Memory Optimization ### Page Buffer Pooling ```go var pagePool = sync.Pool{ New: func() interface{} { b := make([]byte, 4096) // Default page size return &b }, } func getPageBuffer() []byte { return *pagePool.Get().(*[]byte) } func putPageBuffer(b []byte) { pagePool.Put(&b) } ``` ### Streaming Instead of Loading ```go // BAD - Loads entire file data, err := os.ReadFile(path) // GOOD - Streams data f, err := os.Open(path) defer f.Close() io.Copy(dst, f) ``` ## Concurrency Patterns ### Proper Locking ```go // Read-heavy optimization type Store struct { mu sync.RWMutex // Use RWMutex for read-heavy } func (s *Store) Read() { s.mu.RLock() defer s.mu.RUnlock() // Read operation } func (s *Store) Write() { s.mu.Lock() defer s.mu.Unlock() // Write operation } ``` ### Channel Patterns ```go // Batch processing batch := make([]Item, 0, batchSize) ticker := time.NewTicker(batchInterval) for { select { case item := <-input: batch = append(batch, item) if len(batch) >= batchSize { processBatch(batch) batch = batch[:0] } case <-ticker.C: if len(batch) > 0 { processBatch(batch) batch = batch[:0] } } } ``` ## I/O Optimization ### Buffered I/O ```go // Use buffered writers bw := bufio.NewWriterSize(w, 64*1024) // 64KB buffer defer bw.Flush() // Use buffered readers br := bufio.NewReaderSize(r, 64*1024) ``` ### Parallel Downloads ```go func downloadParallel(files []string) { var wg sync.WaitGroup sem := make(chan struct{}, 5) // Limit to 5 concurrent for _, file := range files { wg.Add(1) go func(f string) { defer wg.Done() sem <- struct{}{} defer func() { <-sem }() download(f) }(file) } wg.Wait() } ``` ## Caching Strategy ### LRU Cache Implementation ```go type LRUCache struct { capacity int items map[string]*list.Element list *list.List mu sync.RWMutex } func (c *LRUCache) Get(key string) (interface{}, bool) { c.mu.RLock() elem, ok := c.items[key] c.mu.RUnlock() if !ok { return nil, false } c.mu.Lock() c.list.MoveToFront(elem) c.mu.Unlock() return elem.Value, true } ``` ## Profiling Tools ### CPU Profiling ```bash # Generate CPU profile go test -cpuprofile=cpu.prof -bench=. # Analyze go tool pprof cpu.prof (pprof) top10 (pprof) list functionName ``` ### Memory Profiling ```bash # Generate memory profile go test -memprofile=mem.prof -bench=. # Analyze allocations go tool pprof -alloc_space mem.prof ``` ### Trace Analysis ```bash # Generate trace go test -trace=trace.out # View trace go tool trace trace.out ``` ## Configuration Tuning ### SQLite Pragmas ```sql PRAGMA cache_size = -64000; -- 64MB cache PRAGMA synchronous = NORMAL; -- Balance safety/speed PRAGMA wal_autocheckpoint = 10000; -- Larger WAL before checkpoint PRAGMA busy_timeout = 5000; -- 5 second timeout ``` ### Litestream Settings ```yaml # Optimal intervals min-checkpoint-page-n: 1000 truncate-page-n: 121359 monitor-interval: 1s checkpoint-interval: 1m ``` ## Benchmarks to Run ```bash # Core operations go test -bench=BenchmarkWALRead go test -bench=BenchmarkLTXWrite go test -bench=BenchmarkCompaction go test -bench=BenchmarkPageIteration # With memory stats go test -bench=. -benchmem ``` ## Common Performance Issues 1. **Not pooling buffers** - Creates garbage 2. **Loading entire files** - Use streaming 3. **Excessive locking** - Use RWMutex 4. **No caching** - Repeated expensive operations 5. **Serial processing** - Could parallelize 6. **Small buffers** - Increase buffer sizes ## References - Go performance tips: https://go.dev/doc/perf - SQLite optimization: https://sqlite.org/optoverview.html - Profiling guide: https://go.dev/blog/pprof ================================================ FILE: .claude/agents/replica-client-developer.md ================================================ --- role: Replica Client Developer tools: - read - write - edit - grep - bash priority: high --- # Replica Client Developer Agent You specialize in implementing and maintaining storage backend clients for Litestream replication. ## Core Knowledge ### ReplicaClient Interface Every storage backend MUST implement: ```go type ReplicaClient interface { Type() string LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) DeleteLTXFiles(ctx context.Context, files []*ltx.FileInfo) error DeleteAll(ctx context.Context) error } ``` **LTXFiles useMetadata parameter**: - When `useMetadata=true`: Fetch accurate timestamps from backend metadata (slower, required for point-in-time restore) - When `useMetadata=false`: Use fast timestamps from file listing (faster, suitable for replication monitoring) ### Critical Patterns 1. **Eventual Consistency Handling**: - Storage may not immediately reflect writes - Files may be partially available - ALWAYS prefer local files during compaction 2. **Atomic Operations**: ```go // Write to temp, then rename tmpPath := path + ".tmp" // Write to tmpPath os.Rename(tmpPath, path) ``` 3. **Error Types**: - Return `os.ErrNotExist` for missing files - Wrap errors with context: `fmt.Errorf("operation: %w", err)` 4. **ResumableReader Support**: - `OpenLTXFile` MUST support the `offset` parameter for range requests - `internal/resumable_reader.go` wraps streams with auto-reconnection on idle timeouts - During restore, streams may sit idle while compactor processes other files - If `offset` is ignored, restore operations fail on connection timeouts ### ReplicaClientV3 Interface (Optional) Backends supporting v0.3.x backward-compatible restore should implement: ```go type ReplicaClientV3 interface { GenerationsV3(ctx context.Context) ([]string, error) SnapshotsV3(ctx context.Context, generation string) ([]SnapshotInfoV3, error) WALSegmentsV3(ctx context.Context, generation string) ([]WALSegmentInfoV3, error) OpenSnapshotV3(ctx context.Context, generation string, index int) (io.ReadCloser, error) OpenWALSegmentV3(ctx context.Context, generation string, index int, offset int64) (io.ReadCloser, error) } ``` See `v3.go` for type definitions and `s3/replica_client.go` for reference implementation. ## Implementation Checklist ### New Backend Requirements - [ ] Implement ReplicaClient interface - [ ] Handle partial reads (offset/size) - [ ] Support seek parameter for pagination - [ ] Preserve CreatedAt timestamps when metadata is available - [ ] Handle eventual consistency - [ ] Implement proper error types - [ ] Add integration tests - [ ] Document configuration ### Testing Requirements ```bash # Integration test go test -v ./replica_client_test.go -integration [backend] # Race conditions go test -race -v ./[backend]/... # Large files (>1GB) ./bin/litestream-test populate -target-size 2GB ``` ## Existing Backends Reference ### Study These Implementations - `s3/replica_client.go` - AWS S3 (most complete) - `gs/replica_client.go` - Google Cloud Storage - `abs/replica_client.go` - Azure Blob Storage - `file/replica_client.go` - Local filesystem (simplest) - `sftp/replica_client.go` - SSH File Transfer - `nats/replica_client.go` - NATS JetStream (newest) - `oss/replica_client.go` - Alibaba Cloud OSS ## Common Pitfalls 1. Not handling eventual consistency 2. Missing atomic write operations 3. Incorrect error types 4. Not preserving timestamps 5. Forgetting partial read support 6. No retry logic for transient failures 7. Not supporting `offset` parameter in `OpenLTXFile` — breaks `ResumableReader` during restore ## Configuration Pattern ```yaml replica: type: [backend] option1: value1 option2: value2 ``` ## References - docs/REPLICA_CLIENT_GUIDE.md - Complete implementation guide - replica_client.go - Interface definition - v3.go - ReplicaClientV3 interface and v0.3.x types - internal/resumable_reader.go - ResumableReader for restore resilience - replica_client_test.go - Test suite ================================================ FILE: .claude/agents/sqlite-expert.md ================================================ --- role: SQLite WAL and Page Expert tools: - read - write - edit - grep - bash priority: high --- # SQLite Expert Agent You are a SQLite internals expert specializing in WAL (Write-Ahead Log) operations and page management for the Litestream project. ## Core Knowledge ### Critical SQLite Concepts 1. **1GB Lock Page** (MUST KNOW): - Located at exactly 0x40000000 (1,073,741,824 bytes) - Page number varies by page size: - 4KB pages: 262145 - 8KB pages: 131073 - 16KB pages: 65537 - 32KB pages: 32769 - MUST be skipped in all iterations - Cannot contain data 2. **WAL Structure**: - 32-byte header with magic number - Frames with 24-byte headers - Cumulative checksums - Salt values for verification 3. **Page Types**: - B-tree interior/leaf pages - Overflow pages - Freelist pages - Lock byte page (at 1GB) ## Primary Responsibilities ### WAL Monitoring - Monitor WAL file changes in `db.go` - Ensure proper checksum verification - Handle WAL frame reading correctly - Convert WAL frames to LTX format ### Page Management - Always skip lock page during iteration - Handle various page sizes correctly - Verify page integrity - Manage page caching efficiently ### Testing Requirements - Create test databases >1GB - Test all page sizes (4KB, 8KB, 16KB, 32KB) - Verify lock page skipping - Test WAL checkpoint modes ## Code Patterns ### Correct Lock Page Handling ```go lockPgno := ltx.LockPgno(pageSize) if pgno == lockPgno { continue // Skip lock page } ``` ### WAL Reading ```go // Always verify magic number magic := binary.BigEndian.Uint32(header.Magic[:]) if magic != 0x377f0682 && magic != 0x377f0683 { return errors.New("invalid WAL magic") } ``` ## Common Mistakes to Avoid 1. Not skipping lock page at 1GB 2. Incorrect checksum calculations 3. Wrong byte order (use BigEndian) 4. Not handling all page sizes 5. Direct file manipulation (use SQLite API) ## References - docs/SQLITE_INTERNALS.md - Complete SQLite internals guide - docs/LTX_FORMAT.md - LTX conversion details - db.go - WAL monitoring implementation ================================================ FILE: .claude/agents/test-engineer.md ================================================ --- role: Test Engineer tools: - read - write - edit - bash - grep priority: medium --- # Test Engineer Agent You specialize in creating and maintaining comprehensive test suites for Litestream, with focus on edge cases and race conditions. ## Critical Test Scenarios ### 1GB Lock Page Testing **MUST TEST**: Databases crossing the 1GB boundary ```bash # Create >1GB test database sqlite3 large.db < c.failAfter { return errors.New("simulated failure") } return c.ReplicaClient.WriteLTXFile(...) } ``` ## Coverage Requirements ### Minimum Coverage - Core packages: >80% - Storage backends: >70% - Critical paths: 100% ### Generate Coverage Report ```bash go test -coverprofile=coverage.out ./... go tool cover -html=coverage.out ``` ## Common Test Mistakes 1. Not testing with databases >1GB 2. Forgetting race detector 3. Not testing all page sizes 4. Missing eventual consistency tests 5. No error injection tests 6. Ignoring benchmark regressions ## CI/CD Integration ```yaml # .github/workflows/test.yml - name: Run tests with race detector run: go test -race -v ./... - name: Test large databases run: ./scripts/test-large-db.sh - name: Integration tests run: ./scripts/test-integration.sh ``` ## References - docs/TESTING_GUIDE.md - Complete testing guide - replica_client_test.go - Integration test patterns - db_test.go - Unit test examples ================================================ FILE: .claude/commands/add-storage-backend.md ================================================ --- description: Create a new storage backend implementation --- # Add Storage Backend Command Create a new storage backend implementation for Litestream with all required components. ## Steps 1. **Create Package Directory** ```bash mkdir -p {{backend_name}} ``` 2. **Implement ReplicaClient Interface** Create `{{backend_name}}/replica_client.go`: ```go package {{backend_name}} type ReplicaClient struct { // Configuration fields } func (c *ReplicaClient) Type() string { return "{{backend_name}}" } func (c *ReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { // List files at level // When useMetadata=true, fetch accurate timestamps from backend metadata } func (c *ReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { // Open file for reading } func (c *ReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { // Write file atomically } func (c *ReplicaClient) DeleteLTXFiles(ctx context.Context, files []*ltx.FileInfo) error { // Delete files } func (c *ReplicaClient) DeleteAll(ctx context.Context) error { // Remove all files for replica } ``` 3. **Add Configuration Parsing** Update `cmd/litestream/config.go`: ```go case "{{backend_name}}": client = &{{backend_name}}.ReplicaClient{ // Parse config } ``` 4. **Create Integration Tests** Create `{{backend_name}}/replica_client_test.go`: ```go func TestReplicaClient_{{backend_name}}(t *testing.T) { if !*integration || *backend != "{{backend_name}}" { t.Skip("{{backend_name}} integration test skipped") } // Test implementation } ``` 5. **Add Documentation** Update README.md with configuration example: ```yaml replica: type: {{backend_name}} option1: value1 option2: value2 ``` ## Key Requirements - Handle eventual consistency - Implement atomic writes (temp file + rename) - Support partial reads (offset/size) - Preserve CreatedAt timestamps in returned FileInfo - Return proper error types (os.ErrNotExist) ## Testing ```bash # Run integration tests go test -v ./replica_client_test.go -integration {{backend_name}} # Test with race detector go test -race -v ./{{backend_name}}/... ``` ================================================ FILE: .claude/commands/analyze-ltx.md ================================================ Analyze LTX file issues in Litestream. This command helps diagnose problems with LTX files, including corruption, missing files, and consistency issues. First, understand the context: - What error messages are being reported? - Which storage backend is being used? - Are there any eventual consistency issues? Then perform the analysis: 1. **Check LTX file structure**: Look for corrupted headers, invalid page indices, or checksum mismatches in the LTX files. 2. **Verify file continuity**: Ensure there are no gaps in the TXID sequence that could prevent restoration. 3. **Check compaction issues**: Look for problems during compaction that might corrupt files, especially with eventually consistent storage. 4. **Analyze page sequences**: Verify that page numbers are sequential and the lock page at 1GB is properly skipped. 5. **Review storage backend behavior**: Check if the storage backend has eventual consistency that might cause partial reads during compaction. Key files to examine: - `db.go`: WAL monitoring and LTX generation - `replica_client.go`: Storage interface - `store.go`: Compaction logic - Backend-specific client in `s3/`, `gs/`, etc. Common issues to look for: - "nonsequential page numbers" errors (corrupted compaction) - "EOF" errors (partial file reads) - Missing TXID ranges (failed uploads) - Lock page at 0x40000000 not being skipped Use the testing harness to reproduce: ```bash ./bin/litestream-test validate -source-db test.db -replica-url [URL] ``` ## Recovery Options When analysis reveals corrupted or missing LTX files, two recovery mechanisms are available: **Manual recovery** with `litestream reset`: ```bash litestream reset /path/to/database.db ``` Clears local LTX state from the metadata directory. The database file is not modified. Next sync creates a fresh snapshot. See `cmd/litestream/reset.go` for implementation. **Automatic recovery** with `auto-recover` config: ```yaml dbs: - path: /path/to/database.db replicas: - url: s3://bucket/path auto-recover: true ``` When enabled, Litestream automatically resets local state when LTX errors are detected during sync. Disabled by default. See `replica.go` (auto-recover logic) for implementation. ================================================ FILE: .claude/commands/debug-ipc.md ================================================ --- description: Debug IPC Unix socket issues --- # Debug IPC Command Diagnose issues with the Litestream IPC control socket (`server.go`). ## 1. Socket Configuration Check Verify the socket is enabled and correctly configured: ```yaml # litestream.yml socket: enabled: true # Default: false path: /var/run/litestream.sock # Default path permissions: 0600 # Default permissions ``` Source: `SocketConfig` struct in `server.go:17-21`, defaults in `DefaultSocketConfig()`. ## 2. Endpoint Reference All endpoints are HTTP over Unix socket (`server.go:74-87`): | Method | Path | Request Body | Response | |--------|------|-------------|----------| | `GET` | `/info` | — | `{version, pid, uptime_seconds, started_at, database_count}` | | `GET` | `/list` | — | `{databases: [{path, status, last_sync_at}]}` | | `GET` | `/txid?path=` | — | `{txid}` | | `POST` | `/register` | `{path, replica_url}` | `{status, path}` | | `POST` | `/unregister` | `{path, timeout?}` | `{status, path}` | | `POST` | `/start` | `{path, timeout?}` | `{status, path}` | | `POST` | `/stop` | `{path, timeout?}` | `{status, path}` | | `GET` | `/debug/pprof/` | — | Standard Go pprof index | | `GET` | `/debug/pprof/profile` | — | CPU profile | | `GET` | `/debug/pprof/trace` | — | Execution trace | ## 3. Testing with curl ```bash # Server info (version, PID, uptime) curl --unix-socket /var/run/litestream.sock http://localhost/info # List all managed databases curl --unix-socket /var/run/litestream.sock http://localhost/list # Get transaction ID for a specific database curl --unix-socket /var/run/litestream.sock "http://localhost/txid?path=/path/to/db" # Register a new database at runtime curl --unix-socket /var/run/litestream.sock -X POST \ -H "Content-Type: application/json" \ -d '{"path":"/path/to/db","replica_url":"s3://bucket/path"}' \ http://localhost/register # Unregister (stop replicating) a database curl --unix-socket /var/run/litestream.sock -X POST \ -H "Content-Type: application/json" \ -d '{"path":"/path/to/db"}' \ http://localhost/unregister # CPU profile (30 seconds by default) curl --unix-socket /var/run/litestream.sock http://localhost/debug/pprof/profile > cpu.prof go tool pprof cpu.prof # Heap profile curl --unix-socket /var/run/litestream.sock http://localhost/debug/pprof/heap > heap.prof go tool pprof heap.prof ``` ## 4. Common Issues ### Socket not enabled **Symptom**: `curl: (7) Couldn't connect to server` **Fix**: Set `socket.enabled: true` in config and restart litestream. ### Permission denied **Symptom**: `curl: (7) Permission denied` **Fix**: Check `socket.permissions` (default `0600`). The connecting user must match the litestream process owner. ### Stale socket file after crash **Symptom**: `bind: address already in use` in logs on startup **Fix**: Remove the stale socket file: `rm /var/run/litestream.sock` ### Database not found **Symptom**: `{"error":"database not found"}` from `/txid` or `/start` **Fix**: Verify the path matches the expanded path in config. Use `/list` to see registered databases. Note that `$PID` and env vars are expanded in config paths. ### Register fails with "already exists" **Symptom**: `{"error":"database already registered"}` **Fix**: The database path is already being replicated. Use `/unregister` first, then `/register` with new settings. ## 5. pprof Debugging Available pprof endpoints on the IPC socket: ```bash # Interactive CPU profile analysis curl -s --unix-socket /var/run/litestream.sock \ http://localhost/debug/pprof/profile?seconds=30 > cpu.prof go tool pprof -http=:8080 cpu.prof # Goroutine dump (useful for deadlock investigation) curl --unix-socket /var/run/litestream.sock \ http://localhost/debug/pprof/goroutine?debug=2 # Memory allocation profile curl -s --unix-socket /var/run/litestream.sock \ http://localhost/debug/pprof/heap > heap.prof go tool pprof -http=:8080 heap.prof # Execution trace (captures scheduler, GC, goroutine events) curl -s --unix-socket /var/run/litestream.sock \ http://localhost/debug/pprof/trace?seconds=5 > trace.out go tool trace trace.out ``` ================================================ FILE: .claude/commands/debug-wal.md ================================================ Debug WAL monitoring issues in Litestream. This command helps diagnose problems with WAL change detection, checkpointing, and replication triggers. First, understand the symptoms: - Is replication not triggering on changes? - Are checkpoints failing or not happening? - Is the WAL growing unbounded? Then debug the monitoring system: 1. **Check monitor goroutine** (db.go:1499): ```go // Verify monitor is running func (db *DB) monitor() { ticker := time.NewTicker(db.MonitorInterval) // Default: 1s // Check if ticker is firing // Verify checkWAL() is being called } ``` 2. **Verify WAL change detection**: ```go // Check if WAL changes are detected func (db *DB) checkWAL() (bool, error) { // Get WAL size and checksum // Compare with previous values // Should return true if changed } ``` 3. **Debug checkpoint triggers**: ```go // Check checkpoint thresholds MinCheckpointPageN int // Default: 1000 pages TruncatePageN int // Default: 121359 pages // Verify WAL page count walPageCount := db.WALPageCount() if walPageCount > db.MinCheckpointPageN { // Should trigger passive checkpoint } if walPageCount > db.TruncatePageN { // Should trigger truncate checkpoint (emergency brake) } ``` 4. **Check long-running read transaction**: ```go // Ensure rtx is maintained if db.rtx == nil { // Read transaction lost - replication may fail } ``` 5. **Monitor notification channel**: ```go // Check if replicas are notified select { case <-db.notify: // WAL change detected default: // No changes } ``` Common issues to check: - MonitorInterval too long (default 1s) - Checkpoint failing due to active transactions - Read transaction preventing checkpoint - Notify channel not triggering replicas - WAL file permissions issues Debug commands: ```sql -- Check WAL status PRAGMA wal_checkpoint; PRAGMA journal_mode; PRAGMA page_count; PRAGMA wal_autocheckpoint; -- Check for locks SELECT * FROM pragma_lock_status(); ``` Testing WAL monitoring: ```go func TestDB_WALMonitoring(t *testing.T) { db := setupTestDB(t) // Set fast monitoring for test db.MonitorInterval = 10 * time.Millisecond // Write data writeTestData(t, db, 100) // Wait for notification select { case <-db.notify: // Success case <-time.After(1 * time.Second): t.Error("WAL change not detected") } } ``` Monitor with logging: ```go slog.Debug("wal check", "size", walInfo.Size, "checksum", walInfo.Checksum, "pages", walInfo.PageCount) ``` ================================================ FILE: .claude/commands/fix-common-issues.md ================================================ --- description: Fix common Litestream issues --- # Fix Common Issues Command Diagnose and fix common issues in Litestream deployments. ## Issue 1: Lock Page Not Being Skipped **Symptom**: Errors or corruption with databases >1GB **Check**: ```bash # Find lock page references grep -r "LockPgno" --include="*.go" ``` **Fix**: ```go // Ensure all page iterations skip lock page lockPgno := ltx.LockPgno(pageSize) if pgno == lockPgno { continue } ``` ## Issue 2: Race Condition in Replica Position **Symptom**: Data races detected, inconsistent position tracking **Check**: ```bash go test -race -v -run TestReplica_Sync ./... ``` **Fix**: ```go // Change from RLock to Lock for writes func (r *Replica) SetPos(pos ltx.Pos) { r.mu.Lock() // NOT RLock! defer r.mu.Unlock() r.pos = pos } ``` ## Issue 3: Eventual Consistency Issues **Symptom**: Compaction failures, partial file reads **Check**: ```bash # Look for remote reads during compaction grep -r "OpenLTXFile" db.go | grep -v "os.Open" ``` **Fix**: ```go // Always try local first f, err := os.Open(db.LTXPath(info.Level, info.MinTXID, info.MaxTXID)) if err == nil { return f, nil } // Only fall back to remote if local doesn't exist return replica.Client.OpenLTXFile(...) ``` ## Issue 4: CreatedAt Timestamp Loss **Symptom**: Point-in-time recovery lacks accurate timestamps **Check**: ```go info, err := client.WriteLTXFile(ctx, level, minTXID, maxTXID, r) if err != nil { t.Fatal(err) } if info.CreatedAt.IsZero() { t.Fatal("CreatedAt not set") } ``` **Fix**: ```go // Ensure storage metadata is copied into the returned FileInfo modTime := resp.LastModified info.CreatedAt = modTime ``` ## Issue 5: Non-Atomic File Writes **Symptom**: Partial files, corruption on crash **Check**: ```bash # Find direct writes without temp files grep -r "os.Create\|os.WriteFile" --include="*.go" ``` **Fix**: ```go // Write to temp, then rename tmpPath := path + ".tmp" if err := os.WriteFile(tmpPath, data, 0644); err != nil { return err } return os.Rename(tmpPath, path) ``` ## Issue 6: WAL Checkpoint Blocking **Symptom**: WAL grows indefinitely, database locks **Check**: ```sql -- Check WAL size PRAGMA wal_checkpoint(PASSIVE); SELECT page_count * page_size FROM pragma_page_count(), pragma_page_size(); ``` **Fix**: ```go // Release read transaction periodically db.rtx.Rollback() db.rtx = nil // Checkpoint db.db.Exec("PRAGMA wal_checkpoint(RESTART)") // Restart read transaction db.initReadTx() ``` ## Issue 7: Memory Leaks **Symptom**: Growing memory usage over time **Check**: ```bash # Generate heap profile go test -memprofile=mem.prof -run=XXX -bench=. go tool pprof -top mem.prof ``` **Fix**: ```go // Use sync.Pool for buffers var pagePool = sync.Pool{ New: func() interface{} { b := make([]byte, pageSize) return &b }, } // Close resources properly defer func() { if f != nil { f.Close() } }() ``` ## Issue 8: Corrupted or Missing LTX Files **Symptom**: Sync failures with errors like "ltx validation failed", "nonsequential page numbers", "non-contiguous transaction files", or persistent sync retry backoff loops after unclean shutdowns. **Check**: ```bash # Look for LTXError messages in logs rg -i "ltx.*error|reset.*local|auto.recover" /var/log/litestream.log # Check if meta directory has corrupted state (note dot prefix) ls -la /path/to/.database.db-litestream/ltx/ ``` **Fix - Manual Reset**: ```bash # Clears local LTX state, forces fresh snapshot on next sync # Database file is NOT modified litestream reset /path/to/database.db # With explicit config file litestream reset -config /etc/litestream.yml /path/to/database.db ``` **Fix - Automatic Recovery**: ```yaml # Add to replica config in litestream.yml dbs: - path: /path/to/database.db replicas: - url: s3://bucket/path auto-recover: true # Automatically resets on LTX errors ``` **When to use which**: Use `auto-recover` for unattended deployments where automatic recovery is preferred over manual intervention. Use manual `reset` when you want to investigate the corruption first. `auto-recover` is disabled by default because resetting discards local LTX history, which may reduce point-in-time restore granularity. **Reference**: `cmd/litestream/reset.go`, `replica.go` (auto-recover logic), `db.go` (`ResetLocalState`) ## Issue 9: IPC Socket Connection Failures **Symptom**: `connection refused` or `no such file or directory` when using the control socket **Check**: ```bash # Verify socket exists and has correct permissions ls -la /var/run/litestream.sock # Verify socket is enabled in config rg -A3 'socket:' /etc/litestream.yml # Check if litestream process is running pgrep -a litestream ``` **Fix**: ```yaml # Enable socket in litestream.yml socket: enabled: true path: /var/run/litestream.sock permissions: 0600 ``` **Stale socket**: If litestream crashed, the socket file may still exist. The process creates a new socket on startup and will fail if the stale file exists. Remove it manually: ```bash rm /var/run/litestream.sock ``` **Reference**: `server.go` (`SocketConfig`, `Server.Start`) ## Issue 10: Backup Files Accumulating (Retention Disabled) **Symptom**: Storage usage growing unbounded, old LTX files never deleted **Check**: ```bash # Look for retention warning in logs rg "retention disabled" /var/log/litestream.log ``` **Fix**: ```yaml # Option 1: Re-enable Litestream retention (default) retention: enabled: true # Option 2: Keep disabled, but configure cloud lifecycle policies # Example: S3 lifecycle rule to expire objects in ltx/ prefix after 30 days ``` **Reference**: `store.go` (`SetRetentionEnabled`), `compactor.go` (`RetentionEnabled`), `cmd/litestream/replicate.go:295` ## Issue 11: v0.3.x Restore Not Finding Backups **Symptom**: Restore fails with "no snapshots available" but v0.3.x backups exist in storage **Check**: ```bash # Verify v0.3.x backup structure exists aws s3 ls s3://bucket/path/generations/ --recursive | head -20 ``` **Fix**: Ensure the backend implements `ReplicaClientV3`. The S3 backend supports this automatically. The restore process checks for v0.3.x backups when no v0.4.x+ backup is found. **Reference**: `v3.go` (`ReplicaClientV3` interface), `s3/replica_client.go` ## Diagnostic Commands ```bash # Check database integrity sqlite3 database.db "PRAGMA integrity_check;" # List replicated LTX files litestream ltx /path/to/db.sqlite # Check replication status litestream databases # Reset corrupted local state litestream reset /path/to/database.db # Test restoration litestream restore -o test.db [replica-url] # IPC socket diagnostics (requires socket.enabled: true) curl --unix-socket /var/run/litestream.sock http://localhost/info curl --unix-socket /var/run/litestream.sock http://localhost/list curl --unix-socket /var/run/litestream.sock "http://localhost/txid?path=/path/to/db" ``` ## Prevention Checklist - [ ] Always test with databases >1GB - [ ] Run with race detector in CI - [ ] Test all page sizes (4KB, 8KB, 16KB, 32KB) - [ ] Verify eventual consistency handling - [ ] Check for proper locking (Lock vs RLock) - [ ] Ensure atomic file operations - [ ] Preserve timestamps in compaction ================================================ FILE: .claude/commands/run-comprehensive-tests.md ================================================ --- description: Run comprehensive test suite for Litestream --- # Run Comprehensive Tests Command Execute a full test suite including unit tests, integration tests, race detection, and large database tests. ## Quick Test Suite ```bash # Basic tests with race detection go test -race -v ./... # With coverage go test -race -cover -v ./... ``` ## Full Test Suite ### 1. Unit Tests ```bash echo "=== Running Unit Tests ===" go test -v ./... -short ``` ### 2. Race Condition Tests ```bash echo "=== Testing for Race Conditions ===" go test -race -v -run TestReplica_Sync ./... go test -race -v -run TestDB_Sync ./... go test -race -v -run TestStore_CompactDB ./... go test -race -v ./... ``` ### 3. Integration Tests ```bash echo "=== Running Integration Tests ===" # S3 (requires AWS credentials) AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=yyy \ go test -v ./replica_client_test.go -integration s3 # Google Cloud Storage (requires credentials) GOOGLE_APPLICATION_CREDENTIALS=/path/to/creds.json \ go test -v ./replica_client_test.go -integration gcs # Azure Blob Storage AZURE_STORAGE_ACCOUNT=xxx AZURE_STORAGE_KEY=yyy \ go test -v ./replica_client_test.go -integration abs # SFTP (requires SSH server) go test -v ./replica_client_test.go -integration sftp # File system (always available) go test -v ./replica_client_test.go -integration file ``` ### 4. Large Database Tests (>1GB) ```bash echo "=== Testing Large Databases ===" # Create test database for each page size for pagesize in 4096 8192 16384 32768; do echo "Testing page size: $pagesize" # Create >1GB database sqlite3 test-${pagesize}.db <70% for core packages ✅ Lock page correctly skipped for all page sizes ✅ Restoration works for databases >1GB ✅ No memory leaks in benchmarks ================================================ FILE: .claude/commands/test-compaction.md ================================================ Test Litestream compaction logic. This command helps test and debug compaction issues, especially with eventually consistent storage backends. First, understand the test scenario: - What storage backend needs testing? - What size database is involved? - Are there eventual consistency concerns? Then create comprehensive tests: 1. **Test basic compaction**: ```go func TestCompaction_Basic(t *testing.T) { // Create multiple LTX files at level 0 // Run compaction to level 1 // Verify merged file is correct } ``` 2. **Test with eventual consistency**: ```go func TestStore_CompactDB_RemotePartialRead(t *testing.T) { // Use mock client that returns partial data initially // Verify compaction prefers local files // Ensure no corruption occurs } ``` 3. **Test lock page handling during compaction**: ```go func TestCompaction_LockPage(t *testing.T) { // Create database > 1GB // Compact with data around lock page // Verify lock page is skipped (page at 0x40000000) } ``` 4. **Test timestamp preservation**: ```go func TestCompaction_PreserveTimestamps(t *testing.T) { // Compact files with different CreatedAt times // Verify earliest timestamp is preserved } ``` Key areas to test: - Reading from local files first (db.go:1280-1294) - Skipping lock page at 1GB boundary - Preserving CreatedAt timestamps - Handling partial/incomplete remote files - Concurrent compaction safety Run with race detector: ```bash go test -race -v -run TestStore_CompactDB ./... ``` Use the test harness for large databases: ```bash ./bin/litestream-test populate -db test.db -target-size 1.5GB ./bin/litestream-test validate -source-db test.db -replica-url file:///tmp/replica ``` ================================================ FILE: .claude/commands/trace-replication.md ================================================ Trace the complete replication flow in Litestream. This command helps understand how changes flow from SQLite through to storage backends. Follow the replication path step by step: 1. **Application writes to SQLite**: ```sql -- Application performs write INSERT INTO table VALUES (...); -- SQLite appends to WAL file ``` 2. **DB.monitor() syncs the shadow WAL** (db.go:1499): ```go ticker := time.NewTicker(db.MonitorInterval) // default 1s for { select { case <-db.ctx.Done(): return case <-ticker.C: } if err := db.Sync(db.ctx); err != nil && !errors.Is(err, context.Canceled) { db.Logger.Error("sync error", "error", err) } } ``` 3. **Replica.monitor() responds** (replica.go): ```go ticker := time.NewTicker(r.SyncInterval) defer ticker.Stop() notify := r.db.Notify() for { select { case <-ctx.Done(): return case <-ticker.C: // Enforce minimum sync interval case <-notify: // WAL changed, time to sync } notify = r.db.Notify() if err := r.Sync(ctx); err != nil && !errors.Is(err, context.Canceled) { r.Logger().Error("monitor error", "error", err) } } ``` 4. **Replica.Sync() uploads new L0 files** (replica.go): ```go // Determine local database position dpos, err := r.db.Pos() if err != nil { return err } if dpos.IsZero() { return fmt.Errorf("no position, waiting for data") } // Upload each unreplicated L0 file for txID := r.Pos().TXID + 1; txID <= dpos.TXID; txID = r.Pos().TXID + 1 { if err := r.uploadLTXFile(ctx, 0, txID, txID); err != nil { return err } r.SetPos(ltx.Pos{TXID: txID}) } ``` 5. **ReplicaClient uploads to storage**: ```go func (c *S3Client) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { // Stream LTX data to storage and return metadata (size, CreatedAt, checksums) } ``` 6. **Checkpoint when thresholds are hit**: ```go if walPageCount > db.MinCheckpointPageN { db.Checkpoint(ctx, litestream.CheckpointModePassive) } if walPageCount > db.TruncatePageN { db.Checkpoint(ctx, litestream.CheckpointModeTruncate) } ``` Key synchronization points: - WAL monitoring (1s intervals) - Replica sync (configurable, default 1s) - Checkpoint triggers (page thresholds) - Compaction (hourly/daily) Trace with logging: ```go // Enable debug logging slog.SetLogLevel(slog.LevelDebug) // Key log points: slog.Debug("wal changed", "size", walSize) slog.Debug("syncing replica", "pos", r.Pos()) slog.Debug("ltx uploaded", "txid", maxTXID) slog.Debug("checkpoint complete", "mode", mode) ``` Performance metrics to monitor: - WAL growth rate - Sync latency - Upload throughput - Checkpoint frequency - Compaction duration Common bottlenecks: 1. Slow storage uploads 2. Large transactions causing big LTX files 3. Long-running read transactions blocking checkpoints 4. Eventual consistency delays 5. Network latency to storage Test replication flow: ```bash # Start replication with verbose logging litestream replicate -v # In another terminal, write to database sqlite3 test.db "INSERT INTO test VALUES (1, 'data');" # Watch logs for flow: # - WAL change detected # - Replica sync triggered # - LTX file uploaded # - Position updated ``` Verify replication: ```bash # List replicated files aws s3 ls s3://bucket/path/ltx/0000/ # Restore and verify litestream restore -o restored.db s3://bucket/path sqlite3 restored.db "SELECT * FROM test;" ``` ================================================ FILE: .claude/commands/validate-replica.md ================================================ Validate a ReplicaClient implementation in Litestream. This command helps ensure a replica client correctly implements the interface and handles edge cases. First, identify what needs validation: - Which replica client implementation? - What storage backend specifics? - Any known issues or concerns? Then validate the implementation: 1. **Interface compliance check**: ```go // Ensure all methods are implemented var _ litestream.ReplicaClient = (*YourClient)(nil) ``` 1. **Verify error types**: ```go // OpenLTXFile must return os.ErrNotExist for missing files _, err := client.OpenLTXFile(ctx, 0, 999, 999, 0, 0) if !errors.Is(err, os.ErrNotExist) { t.Errorf("Expected os.ErrNotExist, got %v", err) } ``` 1. **Test partial reads**: ```go // Must support offset and size parameters rc, err := client.OpenLTXFile(ctx, 0, 1, 100, 50, 25) data, _ := io.ReadAll(rc) if len(data) != 25 { t.Errorf("Expected 25 bytes, got %d", len(data)) } ``` 1. **Verify timestamp preservation**: ```go // CreatedAt should reflect remote object metadata (or upload time) start := time.Now() info, _ := client.WriteLTXFile(ctx, 0, 1, 100, reader) if info.CreatedAt.IsZero() || info.CreatedAt.Before(start.Add(-time.Second)) { t.Error("unexpected CreatedAt timestamp") } ``` 1. **Test eventual consistency handling**: - Implement retry logic for transient failures - Handle partial file availability - Verify write-after-write consistency 1. **Validate cleanup**: ```go // DeleteAll must remove everything err := client.DeleteAll(ctx) files, _ := client.LTXFiles(ctx, 0, 0, false) if files.Next() { t.Error("Files remain after DeleteAll") } ``` Key validation points: - Proper error types (os.ErrNotExist, os.ErrPermission) - Context cancellation handling - Concurrent operation safety - Iterator doesn't load all files at once - Proper path construction for storage backend Run integration tests: ```bash go test -v ./[backend]/replica_client_test.go -integration ``` ================================================ FILE: .claude/settings.json ================================================ { "project_name": "Litestream", "description": "Standalone disaster recovery tool for SQLite", "auto_formatting": { "enabled": true, "markdown": { "enabled": true, "tool": "markdownlint" }, "json": { "enabled": true, "tool": "jq" } }, "file_permissions": { "read_only_patterns": [ "*.pdf" ] } } ================================================ FILE: .github/ISSUE_TEMPLATE/bug_report.md ================================================ --- name: Bug report about: Create a report to help us improve Litestream title: '' labels: bug assignees: '' --- ## Bug Description A clear and concise description of the bug. ## Environment **Litestream version:** ```text paste output here ``` **Operating system & version:** **Installation method:** **Storage backend:** ## Steps to Reproduce 1. Step one 2. Step two 3. Step three **Expected behavior:** **Actual behavior:** ## Configuration
litestream.yml ```yaml # Paste your configuration here ```
## Logs
Log output ```text # Paste relevant logs here ```
## Additional Context Add any other context about the problem here (e.g., recent changes, related issues, workarounds you've tried). ================================================ FILE: .github/ISSUE_TEMPLATE/config.yml ================================================ blank_issues_enabled: true contact_links: - name: Documentation Issues url: https://github.com/benbjohnson/litestream.io/issues/new/choose about: Report documentation bugs or suggest improvements - name: Documentation url: https://litestream.io about: Check our documentation for setup and usage guides ================================================ FILE: .github/ISSUE_TEMPLATE/feature_request.md ================================================ --- name: Feature request about: Suggest an idea for Litestream title: '' labels: enhancement assignees: '' --- ## Feature Description ## Use Cases ## Attempted Solutions ## Proposal ================================================ FILE: .github/pull_request_template.md ================================================ ## Description ## Motivation and Context Fixes #(issue number) ## How Has This Been Tested? ## Types of changes - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (would cause existing functionality to not work as expected) ## Checklist - [ ] My code follows the code style of this project (`go fmt`, `go vet`) - [ ] I have tested my changes (`go test ./...`) - [ ] I have updated the documentation accordingly (if needed) ================================================ FILE: .github/workflows/commit.yml ================================================ on: push: branches: - main pull_request: types: - opened - synchronize - reopened name: Commit jobs: lint: name: Lint runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - run: | go install golang.org/x/tools/cmd/goimports@latest go install honnef.co/go/tools/cmd/staticcheck@latest export PATH="$HOME/go/bin:$PATH" - uses: pre-commit/action@v3.0.0 vfs-build-test: name: VFS Build Test (macOS) runs-on: macos-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - name: Build VFS shared library run: make vfs - name: Verify shared library created run: file dist/litestream-vfs.so vfs-build-test-linux: name: VFS Build Test (Linux) runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - name: Build VFS Linux AMD64 run: make vfs-linux-amd64 - name: Verify shared library created run: file dist/litestream-vfs-linux-amd64.so build-windows: name: Build Windows runs-on: ubuntu-latest steps: - run: sudo apt-get install -y mingw-w64 - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - run: | go build ./cmd/litestream/ file ./litestream.exe env: CGO_ENABLED: "1" GOOS: windows GOARCH: amd64 CC: x86_64-w64-mingw32-gcc docker-smoke-test: name: Docker Smoke Test runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: docker/setup-buildx-action@v3 - name: Build default image uses: docker/build-push-action@v6 with: context: . target: default push: false load: true tags: litestream:default - name: Build hardened image uses: docker/build-push-action@v6 with: context: . target: hardened push: false load: true tags: litestream:hardened - name: "Default: verify version command" run: docker run --rm litestream:default version - name: "Hardened: verify version command" run: docker run --rm litestream:hardened version - name: "Hardened: verify non-root user (UID 65532)" run: | user=$(docker inspect --format='{{.Config.User}}' litestream:hardened) echo "Container user: $user" if [ "$user" != "nonroot:nonroot" ]; then echo "FAIL: Expected user 'nonroot:nonroot', got '$user'" exit 1 fi docker create --name uid-check litestream:hardened version uid=$(docker export uid-check | tar -xf - --to-stdout etc/passwd | grep '^nonroot:' | cut -d: -f3) docker rm uid-check echo "nonroot UID: $uid" if [ "$uid" != "65532" ]; then echo "FAIL: Expected UID 65532, got '$uid'" exit 1 fi - name: "Hardened: verify no shell exists" run: | if docker run --rm --entrypoint /bin/sh litestream:hardened -c "echo hello" 2>/dev/null; then echo "FAIL: Shell should not exist in scratch image" exit 1 fi echo "PASS: No shell in scratch image" - name: "Hardened: verify CA certificates present" run: | docker create --name cert-check litestream:hardened version docker export cert-check | tar -tf - | grep -q "etc/ssl/certs/ca-certificates.crt" echo "PASS: CA certificates found" docker rm cert-check build: name: Build & Unit Test runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - run: go env - run: go install ./cmd/litestream - run: go build -o /dev/null ./_examples/library/basic - run: go build -o /dev/null ./_examples/library/s3 - run: go test -v ./_examples/library - run: go test -v . - run: go test -v ./internal - run: go test -v ./abs - run: go test -v ./file - run: go test -v ./gs - run: go test -v ./nats - run: go test -v ./s3 - run: go test -v ./sftp - run: go test -v ./cmd/litestream # long-running-test: # name: Run Long Running Unit Test # runs-on: ubuntu-latest # steps: # - uses: actions/checkout@v2 # - uses: actions/setup-go@v2 # with: # go-version: '1.24' # - uses: actions/cache@v2 # with: # path: ~/go/pkg/mod # key: ${{ inputs.os }}-go-${{ hashFiles('**/go.sum') }} # restore-keys: ${{ inputs.os }}-go- # # - run: go install ./cmd/litestream # - run: go test -v -run=TestCmd_Replicate_LongRunning ./integration -long-running-duration 1m s3-mock-test: name: Run S3 Mock Tests runs-on: ubuntu-latest needs: build steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: '3.12' # cache: 'pip' - run: pip install moto[s3,server] - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - run: go env - run: go install ./cmd/litestream - run: ./etc/s3_mock.py go test -v ./replica_client_test.go -integration s3 minio-integration-test: name: Run MinIO Integration Tests runs-on: ubuntu-latest needs: build steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - name: Start MinIO Server run: | docker run -d \ --name minio-test \ -p 9000:9000 \ -p 9001:9001 \ -e MINIO_ROOT_USER=minioadmin \ -e MINIO_ROOT_PASSWORD=minioadmin \ quay.io/minio/minio server /data --console-address ":9001" # Wait for MinIO to be ready echo "Waiting for MinIO server to be ready..." for i in {1..30}; do if docker exec minio-test mc alias set local http://localhost:9000 minioadmin minioadmin 2>/dev/null; then echo "MinIO server is ready" break fi echo "Waiting for MinIO server... ($i/30)" sleep 1 done # Create test bucket docker exec minio-test mc mb local/testbucket - run: go install ./cmd/litestream - name: Test Query Parameter Support run: | # Create test database sqlite3 /tmp/test.db "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT); INSERT INTO users (name) VALUES ('Alice'), ('Bob');" # Test replicate with query parameters export AWS_ACCESS_KEY_ID=minioadmin export AWS_SECRET_ACCESS_KEY=minioadmin # Run replication for 5 seconds timeout 5 litestream replicate /tmp/test.db \ "s3://testbucket/test.db?endpoint=localhost:9000&forcePathStyle=true" || true # Verify files were uploaded docker exec minio-test mc ls local/testbucket/test.db/ # Test restore with query parameters rm -f /tmp/restored.db litestream restore -o /tmp/restored.db \ "s3://testbucket/test.db?endpoint=localhost:9000&forcePathStyle=true" # Verify restored data if [ "$(sqlite3 /tmp/restored.db 'SELECT COUNT(*) FROM users;')" != "2" ]; then echo "ERROR: Restored database does not have expected data" exit 1 fi echo "MinIO integration test with query parameters passed!" - name: Cleanup if: always() run: | docker stop minio-test || true docker rm minio-test || true nats-docker-test: name: Run NATS Integration Tests runs-on: ubuntu-latest needs: build steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - name: Start NATS Server with JetStream run: | docker run -d \ --name nats-test \ -p 4222:4222 \ -p 8222:8222 \ nats:latest \ -js \ -DV # Wait for NATS to be ready echo "Waiting for NATS server to be ready..." for i in {1..30}; do if nc -z localhost 4222; then echo "NATS server is ready" break fi echo "Waiting for NATS server... ($i/30)" sleep 1 done - name: Create NATS Object Store Bucket run: | # Install NATS CLI - get latest version using jq for JSON parsing NATS_VERSION=$(curl -fsSL -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ https://api.github.com/repos/nats-io/natscli/releases/latest \ | jq -r '.tag_name' \ | sed 's/^v//') if [ -z "$NATS_VERSION" ] || [ "$NATS_VERSION" = "null" ]; then NATS_VERSION="0.3.1" echo "Warning: Failed to fetch latest NATS CLI version, using fallback v${NATS_VERSION}" fi wget "https://github.com/nats-io/natscli/releases/download/v${NATS_VERSION}/nats-${NATS_VERSION}-linux-amd64.zip" -O nats.zip unzip nats.zip sudo mv "nats-${NATS_VERSION}-linux-amd64/nats" /usr/local/bin/ sudo chmod +x /usr/local/bin/nats # Create the object store bucket nats object add litestream-test --max-bucket-size=100M --replicas=1 - run: go env - run: go install ./cmd/litestream - run: go test -v ./replica_client_test.go -integration nats env: LITESTREAM_NATS_URL: "nats://localhost:4222" LITESTREAM_NATS_BUCKET: "litestream-test" - name: Cleanup if: always() run: | docker stop nats-test || true docker rm nats-test || true s3-integration-test: name: Run S3 Integration Tests runs-on: ubuntu-latest needs: build if: github.ref == 'refs/heads/main' concurrency: group: integration-test-s3 steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - run: go env - run: go install ./cmd/litestream - run: go test -v ./replica_client_test.go -integration s3 env: LITESTREAM_S3_ACCESS_KEY_ID: ${{ secrets.LITESTREAM_S3_ACCESS_KEY_ID }} LITESTREAM_S3_SECRET_ACCESS_KEY: ${{ secrets.LITESTREAM_S3_SECRET_ACCESS_KEY }} LITESTREAM_S3_REGION: us-east-1 LITESTREAM_S3_BUCKET: integration.litestream.io gcp-integration-test: name: Run GCP Integration Tests runs-on: ubuntu-latest needs: build if: github.ref == 'refs/heads/main' concurrency: group: integration-test-gcp steps: - name: Extract GCP credentials run: 'echo "$GOOGLE_APPLICATION_CREDENTIALS" > /opt/gcp.json' shell: bash env: GOOGLE_APPLICATION_CREDENTIALS: ${{secrets.GOOGLE_APPLICATION_CREDENTIALS}} - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - run: go env - run: go install ./cmd/litestream - run: go test -v ./replica_client_test.go -integration gs env: GOOGLE_APPLICATION_CREDENTIALS: /opt/gcp.json LITESTREAM_GS_BUCKET: integration.litestream.io abs-integration-test: name: Run Azure Blob Store Integration Tests runs-on: ubuntu-latest needs: build if: github.ref == 'refs/heads/main' concurrency: group: integration-test-abs steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - run: go env - run: go install ./cmd/litestream - run: go test -v ./replica_client_test.go -integration abs env: LITESTREAM_ABS_ACCOUNT_NAME: ${{ secrets.LITESTREAM_ABS_ACCOUNT_NAME }} LITESTREAM_ABS_ACCOUNT_KEY: ${{ secrets.LITESTREAM_ABS_ACCOUNT_KEY }} LITESTREAM_ABS_BUCKET: integration r2-integration-test: name: Run Cloudflare R2 Integration Tests runs-on: ubuntu-latest needs: build if: github.ref == 'refs/heads/main' concurrency: group: integration-test-r2 steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - run: go env - run: go install ./cmd/litestream - run: go test -v ./replica_client_test.go -integration -replica-clients=r2 env: LITESTREAM_R2_ACCESS_KEY_ID: ${{ secrets.LITESTREAM_R2_ACCESS_KEY_ID }} LITESTREAM_R2_SECRET_ACCESS_KEY: ${{ secrets.LITESTREAM_R2_SECRET_ACCESS_KEY }} LITESTREAM_R2_ENDPOINT: ${{ secrets.LITESTREAM_R2_ENDPOINT }} LITESTREAM_R2_BUCKET: ${{ secrets.LITESTREAM_R2_BUCKET }} sftp-integration-test: name: Run SFTP Integration Tests runs-on: ubuntu-latest needs: build steps: - name: Prepare OpenSSH server run: |- sudo mkdir -p /test/etc/ssh /test/home /run/sshd /test/data/ sudo ssh-keygen -t ed25519 -f /test/etc/ssh/id_ed25519_host -N "" sudo ssh-keygen -t ed25519 -f /test/etc/ssh/id_ed25519 -N "" sudo chmod 0600 /test/etc/ssh/id_ed25519_host /test/etc/ssh/id_ed25519 sudo chmod 0644 /test/etc/ssh/id_ed25519_host.pub /test/etc/ssh/id_ed25519.pub sudo cp /test/etc/ssh/id_ed25519 /test/id_ed25519 sudo chown $USER /test/id_ed25519 sudo tee /test/etc/ssh/sshd_config < /tmp/sftp-concurrent.yml < /tmp/sftp-sequential.yml </dev/null 2>&1; then echo "WebDAV server is ready" break fi sleep 1 done # Verify WebDAV is accessible curl -u testuser:testpass http://localhost:8080/ || (docker logs webdav-test && exit 1) - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - run: go env - run: go install ./cmd/litestream - run: go test -v ./replica_client_test.go -integration webdav env: LITESTREAM_WEBDAV_URL: "http://localhost:8080" LITESTREAM_WEBDAV_USERNAME: "testuser" LITESTREAM_WEBDAV_PASSWORD: "testpass" LITESTREAM_WEBDAV_PATH: "/testdata" - name: Cleanup if: always() run: | docker stop webdav-test || true docker rm webdav-test || true ================================================ FILE: .github/workflows/integration-tests.yml ================================================ name: Integration Tests on: pull_request: paths: - '**.go' - 'go.mod' - 'go.sum' - 'tests/integration/**' - '.github/workflows/integration-tests.yml' workflow_dispatch: inputs: test_type: description: 'Test type to run' required: false default: 'quick' type: choice options: - 'quick' - 'all' - 'long' permissions: contents: read jobs: quick-tests: name: Quick Integration Tests runs-on: ubuntu-latest if: github.event_name == 'pull_request' || inputs.test_type == 'quick' || inputs.test_type == 'all' steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - name: Build binaries run: | go build -o bin/litestream ./cmd/litestream go build -o bin/litestream-test ./cmd/litestream-test - name: Run quick integration tests run: | go test -v -tags=integration -timeout=30m ./tests/integration/... \ -run="TestFreshStart|TestDatabaseIntegrity|TestRapidCheckpoints|TestS3AccessPointLocalStack|TestRestore_|TestBinaryCompatibility|TestCompaction_Compatibility|TestVersionMigration|TestUpgrade|TestLockPage|TestDirectoryWatcher|TestDatabaseDeletion|TestWALGrowth|TestBusyTimeout|TestConcurrentOperations" env: CGO_ENABLED: 1 - name: Upload test logs if: failure() uses: actions/upload-artifact@v4 with: name: quick-test-logs path: | /tmp/litestream-*/*.log /tmp/*-test.log scenario-tests: name: Scenario Integration Tests runs-on: ubuntu-latest if: github.event_name == 'workflow_dispatch' && (inputs.test_type == 'all' || inputs.test_type == 'long') steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - name: Build binaries run: | go build -o bin/litestream ./cmd/litestream go build -o bin/litestream-test ./cmd/litestream-test - name: Run all scenario tests run: | go test -v -tags=integration -timeout=1h ./tests/integration/... \ -run="Test(FreshStart|DatabaseIntegrity|DatabaseDeletion|RapidCheckpoints|WALGrowth|ConcurrentOperations|BusyTimeout)" env: CGO_ENABLED: 1 - name: Upload test logs if: always() uses: actions/upload-artifact@v4 with: name: scenario-test-logs path: | /tmp/litestream-*/*.log /tmp/*-test.log long-running-tests: name: Long-Running Integration Tests runs-on: ubuntu-latest if: github.event_name == 'workflow_dispatch' && inputs.test_type == 'long' timeout-minutes: 600 steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - name: Build binaries run: | go build -o bin/litestream ./cmd/litestream go build -o bin/litestream-test ./cmd/litestream-test - name: Run long tests run: | go test -v -tags="integration,long" -timeout=10h ./tests/integration/... \ -run="TestOvernight|Test1GBBoundary" env: CGO_ENABLED: 1 - name: Upload test logs if: always() uses: actions/upload-artifact@v4 with: name: long-test-logs path: | /tmp/litestream-*/*.log /tmp/*-test.log summary: name: Test Summary runs-on: ubuntu-latest needs: [quick-tests] if: always() && (github.event_name == 'pull_request' || inputs.test_type == 'quick' || inputs.test_type == 'all') steps: - name: Generate summary run: | echo "## Integration Test Results" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY if [ "${{ needs.quick-tests.result }}" == "success" ]; then echo "✅ **Quick Tests:** Passed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.quick-tests.result }}" == "failure" ]; then echo "❌ **Quick Tests:** Failed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.quick-tests.result }}" == "skipped" ]; then echo "⏭️ **Quick Tests:** Skipped" >> $GITHUB_STEP_SUMMARY fi echo "" >> $GITHUB_STEP_SUMMARY echo "---" >> $GITHUB_STEP_SUMMARY echo "**Triggered by:** @${{ github.actor }}" >> $GITHUB_STEP_SUMMARY # Note: Scenario and long-running tests run independently on workflow_dispatch. # Check individual job results for those test suites. ================================================ FILE: .github/workflows/manual-integration-tests.yml ================================================ name: Manual Integration Tests permissions: issues: write pull-requests: write on: workflow_dispatch: inputs: pr_number: description: 'Pull Request number to test (optional)' required: false type: string branch: description: 'Branch name to test (optional, ignored if PR number is provided)' required: false type: string default: '' test_s3: description: 'Run S3 integration tests (AWS)' required: false default: true type: boolean test_gcs: description: 'Run Google Cloud Storage integration tests' required: false default: false type: boolean test_abs: description: 'Run Azure Blob Storage integration tests' required: false default: false type: boolean test_tigris: description: 'Run Fly.io Tigris integration tests' required: false default: false type: boolean test_r2: description: 'Run Cloudflare R2 integration tests' required: false default: false type: boolean test_b2: description: 'Run Backblaze B2 integration tests' required: false default: false type: boolean test_multipart: description: 'Run multipart upload stress tests (5MB-50MB files)' required: false default: false type: boolean jobs: setup: name: Setup and Validation runs-on: ubuntu-latest outputs: ref: ${{ steps.get-ref.outputs.ref }} ref_name: ${{ steps.get-ref.outputs.ref_name }} steps: - name: Determine checkout ref id: get-ref run: | if [ -n "${{ github.event.inputs.pr_number }}" ]; then echo "ref=refs/pull/${{ github.event.inputs.pr_number }}/head" >> $GITHUB_OUTPUT echo "ref_name=PR #${{ github.event.inputs.pr_number }}" >> $GITHUB_OUTPUT echo "::notice::Testing PR #${{ github.event.inputs.pr_number }}" elif [ -n "${{ github.event.inputs.branch }}" ]; then echo "ref=refs/heads/${{ github.event.inputs.branch }}" >> $GITHUB_OUTPUT echo "ref_name=branch ${{ github.event.inputs.branch }}" >> $GITHUB_OUTPUT echo "::notice::Testing branch ${{ github.event.inputs.branch }}" else echo "ref=${{ github.ref }}" >> $GITHUB_OUTPUT echo "ref_name=branch ${{ github.ref_name }}" >> $GITHUB_OUTPUT echo "::notice::Testing current branch ${{ github.ref_name }}" fi - name: Validate inputs run: | if [ "${{ github.event.inputs.test_s3 }}" != "true" ] && \ [ "${{ github.event.inputs.test_gcs }}" != "true" ] && \ [ "${{ github.event.inputs.test_abs }}" != "true" ] && \ [ "${{ github.event.inputs.test_tigris }}" != "true" ] && \ [ "${{ github.event.inputs.test_r2 }}" != "true" ] && \ [ "${{ github.event.inputs.test_b2 }}" != "true" ] && \ [ "${{ github.event.inputs.test_multipart }}" != "true" ]; then echo "::error::At least one test type must be selected" exit 1 fi echo "### Test Configuration" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY echo "**Target:** ${{ steps.get-ref.outputs.ref_name }}" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY echo "**Integration Tests:**" >> $GITHUB_STEP_SUMMARY echo "- S3 (AWS): ${{ github.event.inputs.test_s3 }}" >> $GITHUB_STEP_SUMMARY echo "- Google Cloud Storage: ${{ github.event.inputs.test_gcs }}" >> $GITHUB_STEP_SUMMARY echo "- Azure Blob Storage: ${{ github.event.inputs.test_abs }}" >> $GITHUB_STEP_SUMMARY echo "- Fly.io Tigris: ${{ github.event.inputs.test_tigris }}" >> $GITHUB_STEP_SUMMARY echo "- Cloudflare R2: ${{ github.event.inputs.test_r2 }}" >> $GITHUB_STEP_SUMMARY echo "- Backblaze B2: ${{ github.event.inputs.test_b2 }}" >> $GITHUB_STEP_SUMMARY echo "- Multipart Stress Tests: ${{ github.event.inputs.test_multipart }}" >> $GITHUB_STEP_SUMMARY s3-integration: name: S3 Integration Tests (AWS) runs-on: ubuntu-latest needs: setup if: github.event.inputs.test_s3 == 'true' concurrency: group: integration-test-s3-manual cancel-in-progress: false steps: - name: Check for required secrets id: check-secrets run: | if [ -z "${{ secrets.LITESTREAM_S3_ACCESS_KEY_ID }}" ]; then echo "::notice title=Skipped::S3 integration tests skipped - credentials not configured" echo "has_secrets=false" >> $GITHUB_OUTPUT else echo "has_secrets=true" >> $GITHUB_OUTPUT fi - uses: actions/checkout@v4 if: steps.check-secrets.outputs.has_secrets == 'true' with: ref: ${{ needs.setup.outputs.ref }} - uses: actions/setup-go@v5 if: steps.check-secrets.outputs.has_secrets == 'true' with: go-version-file: "go.mod" - name: Show Go environment if: steps.check-secrets.outputs.has_secrets == 'true' run: go env - name: Install litestream if: steps.check-secrets.outputs.has_secrets == 'true' run: go install ./cmd/litestream - name: Run S3 integration tests if: steps.check-secrets.outputs.has_secrets == 'true' run: go test -v ./replica_client_test.go -integration -replica-clients=s3 env: LITESTREAM_S3_ACCESS_KEY_ID: ${{ secrets.LITESTREAM_S3_ACCESS_KEY_ID }} LITESTREAM_S3_SECRET_ACCESS_KEY: ${{ secrets.LITESTREAM_S3_SECRET_ACCESS_KEY }} LITESTREAM_S3_REGION: us-east-1 LITESTREAM_S3_BUCKET: integration.litestream.io - name: Create test results directory if: always() run: | mkdir -p test-results echo "Test completed at $(date)" > test-results/s3-test.log - name: Upload test results if: always() uses: actions/upload-artifact@v4 with: name: s3-test-results path: | *.log test-results/ tigris-integration: name: Tigris Integration Tests runs-on: ubuntu-latest needs: setup if: github.event.inputs.test_tigris == 'true' concurrency: group: integration-test-tigris-manual cancel-in-progress: false steps: - name: Check for required secrets id: check-secrets run: | if [ -z "${{ secrets.LITESTREAM_TIGRIS_ACCESS_KEY_ID }}" ]; then echo "::notice title=Skipped::Tigris integration tests skipped - credentials not configured" echo "has_secrets=false" >> $GITHUB_OUTPUT else echo "has_secrets=true" >> $GITHUB_OUTPUT fi - uses: actions/checkout@v4 if: steps.check-secrets.outputs.has_secrets == 'true' with: ref: ${{ needs.setup.outputs.ref }} - uses: actions/setup-go@v5 if: steps.check-secrets.outputs.has_secrets == 'true' with: go-version-file: "go.mod" - name: Show Go environment if: steps.check-secrets.outputs.has_secrets == 'true' run: go env - name: Install litestream if: steps.check-secrets.outputs.has_secrets == 'true' run: go install ./cmd/litestream - name: Run Tigris integration tests if: steps.check-secrets.outputs.has_secrets == 'true' run: go test -v ./replica_client_test.go -integration -replica-clients=tigris env: LITESTREAM_TIGRIS_ACCESS_KEY_ID: ${{ secrets.LITESTREAM_TIGRIS_ACCESS_KEY_ID }} LITESTREAM_TIGRIS_SECRET_ACCESS_KEY: ${{ secrets.LITESTREAM_TIGRIS_SECRET_ACCESS_KEY }} - name: Create test results directory if: always() run: | mkdir -p test-results echo "Test completed at $(date)" > test-results/tigris-test.log - name: Upload test results if: always() uses: actions/upload-artifact@v4 with: name: tigris-test-results path: | *.log test-results/ r2-integration: name: Cloudflare R2 Integration Tests runs-on: ubuntu-latest needs: setup if: github.event.inputs.test_r2 == 'true' concurrency: group: integration-test-r2-manual cancel-in-progress: false steps: - name: Check for required secrets id: check-secrets run: | if [ -z "${{ secrets.LITESTREAM_R2_ACCESS_KEY_ID }}" ]; then echo "::notice title=Skipped::R2 integration tests skipped - credentials not configured" echo "has_secrets=false" >> $GITHUB_OUTPUT else echo "has_secrets=true" >> $GITHUB_OUTPUT fi - uses: actions/checkout@v4 if: steps.check-secrets.outputs.has_secrets == 'true' with: ref: ${{ needs.setup.outputs.ref }} - uses: actions/setup-go@v5 if: steps.check-secrets.outputs.has_secrets == 'true' with: go-version-file: "go.mod" - name: Show Go environment if: steps.check-secrets.outputs.has_secrets == 'true' run: go env - name: Install litestream if: steps.check-secrets.outputs.has_secrets == 'true' run: go install ./cmd/litestream - name: Run R2 integration tests if: steps.check-secrets.outputs.has_secrets == 'true' run: go test -v ./replica_client_test.go -integration -replica-clients=r2 env: LITESTREAM_R2_ACCESS_KEY_ID: ${{ secrets.LITESTREAM_R2_ACCESS_KEY_ID }} LITESTREAM_R2_SECRET_ACCESS_KEY: ${{ secrets.LITESTREAM_R2_SECRET_ACCESS_KEY }} LITESTREAM_R2_ENDPOINT: ${{ secrets.LITESTREAM_R2_ENDPOINT }} LITESTREAM_R2_BUCKET: ${{ secrets.LITESTREAM_R2_BUCKET }} - name: Create test results directory if: always() run: | mkdir -p test-results echo "Test completed at $(date)" > test-results/r2-test.log - name: Upload test results if: always() uses: actions/upload-artifact@v4 with: name: r2-test-results path: | *.log test-results/ b2-integration: name: Backblaze B2 Integration Tests runs-on: ubuntu-latest needs: setup if: github.event.inputs.test_b2 == 'true' concurrency: group: integration-test-b2-manual cancel-in-progress: false steps: - name: Check for required secrets id: check-secrets run: | if [ -z "${{ secrets.LITESTREAM_B2_KEY_ID }}" ]; then echo "::notice title=Skipped::B2 integration tests skipped - credentials not configured" echo "has_secrets=false" >> $GITHUB_OUTPUT else echo "has_secrets=true" >> $GITHUB_OUTPUT fi - uses: actions/checkout@v4 if: steps.check-secrets.outputs.has_secrets == 'true' with: ref: ${{ needs.setup.outputs.ref }} - uses: actions/setup-go@v5 if: steps.check-secrets.outputs.has_secrets == 'true' with: go-version-file: "go.mod" - name: Show Go environment if: steps.check-secrets.outputs.has_secrets == 'true' run: go env - name: Install litestream if: steps.check-secrets.outputs.has_secrets == 'true' run: go install ./cmd/litestream - name: Run B2 integration tests if: steps.check-secrets.outputs.has_secrets == 'true' run: go test -v ./replica_client_test.go -integration -replica-clients=b2 env: LITESTREAM_B2_KEY_ID: ${{ secrets.LITESTREAM_B2_KEY_ID }} LITESTREAM_B2_APPLICATION_KEY: ${{ secrets.LITESTREAM_B2_APPLICATION_KEY }} LITESTREAM_B2_ENDPOINT: ${{ secrets.LITESTREAM_B2_ENDPOINT }} LITESTREAM_B2_BUCKET: ${{ secrets.LITESTREAM_B2_BUCKET }} - name: Create test results directory if: always() run: | mkdir -p test-results echo "Test completed at $(date)" > test-results/b2-test.log - name: Upload test results if: always() uses: actions/upload-artifact@v4 with: name: b2-test-results path: | *.log test-results/ multipart-stress: name: Multipart Upload Stress Tests runs-on: ubuntu-latest needs: setup if: github.event.inputs.test_multipart == 'true' concurrency: group: integration-test-multipart-manual cancel-in-progress: false steps: - name: Check for required secrets id: check-secrets run: | if [ -z "${{ secrets.LITESTREAM_S3_ACCESS_KEY_ID }}" ]; then echo "::notice title=Skipped::Multipart stress tests skipped - S3 credentials not configured" echo "has_secrets=false" >> $GITHUB_OUTPUT else echo "has_secrets=true" >> $GITHUB_OUTPUT fi - uses: actions/checkout@v4 if: steps.check-secrets.outputs.has_secrets == 'true' with: ref: ${{ needs.setup.outputs.ref }} - uses: actions/setup-go@v5 if: steps.check-secrets.outputs.has_secrets == 'true' with: go-version-file: "go.mod" - name: Show Go environment if: steps.check-secrets.outputs.has_secrets == 'true' run: go env - name: Install litestream if: steps.check-secrets.outputs.has_secrets == 'true' run: go install ./cmd/litestream - name: Run multipart threshold tests (AWS S3) if: steps.check-secrets.outputs.has_secrets == 'true' run: | go test -v ./replica_client_test.go -integration -replica-clients=s3 \ -run "TestReplicaClient_S3_Multipart|TestReplicaClient_S3_Concurrency" \ -timeout 30m env: LITESTREAM_S3_ACCESS_KEY_ID: ${{ secrets.LITESTREAM_S3_ACCESS_KEY_ID }} LITESTREAM_S3_SECRET_ACCESS_KEY: ${{ secrets.LITESTREAM_S3_SECRET_ACCESS_KEY }} LITESTREAM_S3_REGION: us-east-1 LITESTREAM_S3_BUCKET: integration.litestream.io - name: Create test results directory if: always() run: | mkdir -p test-results echo "Test completed at $(date)" > test-results/multipart-test.log - name: Upload test results if: always() uses: actions/upload-artifact@v4 with: name: multipart-test-results path: | *.log test-results/ gcs-integration: name: Google Cloud Storage Integration Tests runs-on: ubuntu-latest needs: setup if: github.event.inputs.test_gcs == 'true' concurrency: group: integration-test-gcp-manual cancel-in-progress: false steps: - name: Check for required secrets id: check-secrets run: | if [ -z "${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}" ]; then echo "::notice title=Skipped::GCS integration tests skipped - credentials not configured" echo "has_secrets=false" >> $GITHUB_OUTPUT else echo "has_secrets=true" >> $GITHUB_OUTPUT fi - name: Extract GCP credentials if: steps.check-secrets.outputs.has_secrets == 'true' run: 'echo "$GOOGLE_APPLICATION_CREDENTIALS" > /opt/gcp.json' shell: bash env: GOOGLE_APPLICATION_CREDENTIALS: ${{secrets.GOOGLE_APPLICATION_CREDENTIALS}} - uses: actions/checkout@v4 if: steps.check-secrets.outputs.has_secrets == 'true' with: ref: ${{ needs.setup.outputs.ref }} - uses: actions/setup-go@v5 if: steps.check-secrets.outputs.has_secrets == 'true' with: go-version-file: "go.mod" - name: Show Go environment if: steps.check-secrets.outputs.has_secrets == 'true' run: go env - name: Install litestream if: steps.check-secrets.outputs.has_secrets == 'true' run: go install ./cmd/litestream - name: Run GCS integration tests if: steps.check-secrets.outputs.has_secrets == 'true' run: go test -v ./replica_client_test.go -integration -replica-clients=gs env: GOOGLE_APPLICATION_CREDENTIALS: /opt/gcp.json LITESTREAM_GS_BUCKET: litestream-github-workflows - name: Create test results directory if: always() run: | mkdir -p test-results echo "Test completed at $(date)" > test-results/gcs-test.log - name: Upload test results if: always() uses: actions/upload-artifact@v4 with: name: gcs-test-results path: | *.log test-results/ abs-integration: name: Azure Blob Storage Integration Tests runs-on: ubuntu-latest needs: setup if: github.event.inputs.test_abs == 'true' concurrency: group: integration-test-abs-manual cancel-in-progress: false steps: - name: Check for required secrets id: check-secrets run: | if [ -z "${{ secrets.LITESTREAM_ABS_ACCOUNT_NAME }}" ]; then echo "::notice title=Skipped::Azure Blob Storage integration tests skipped - credentials not configured" echo "has_secrets=false" >> $GITHUB_OUTPUT else echo "has_secrets=true" >> $GITHUB_OUTPUT fi - uses: actions/checkout@v4 if: steps.check-secrets.outputs.has_secrets == 'true' with: ref: ${{ needs.setup.outputs.ref }} - uses: actions/setup-go@v5 if: steps.check-secrets.outputs.has_secrets == 'true' with: go-version-file: "go.mod" - name: Show Go environment if: steps.check-secrets.outputs.has_secrets == 'true' run: go env - name: Install litestream if: steps.check-secrets.outputs.has_secrets == 'true' run: go install ./cmd/litestream - name: Run Azure Blob Storage integration tests if: steps.check-secrets.outputs.has_secrets == 'true' run: go test -v ./replica_client_test.go -integration -replica-clients=abs env: LITESTREAM_ABS_ACCOUNT_NAME: ${{ secrets.LITESTREAM_ABS_ACCOUNT_NAME }} LITESTREAM_ABS_ACCOUNT_KEY: ${{ secrets.LITESTREAM_ABS_ACCOUNT_KEY }} LITESTREAM_ABS_BUCKET: integration - name: Create test results directory if: always() run: | mkdir -p test-results echo "Test completed at $(date)" > test-results/abs-test.log - name: Upload test results if: always() uses: actions/upload-artifact@v4 with: name: abs-test-results path: | *.log test-results/ summary: name: Test Summary runs-on: ubuntu-latest needs: [setup, s3-integration, gcs-integration, abs-integration, tigris-integration, r2-integration, b2-integration, multipart-stress] if: always() steps: - name: Download all artifacts uses: actions/download-artifact@v4 continue-on-error: true - name: Generate summary run: | echo "## Integration Test Results" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY echo "### Target: ${{ needs.setup.outputs.ref_name }}" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY echo "### Integration Tests" >> $GITHUB_STEP_SUMMARY if [ "${{ needs.s3-integration.result }}" == "success" ]; then echo "✅ **S3 (AWS):** Passed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.s3-integration.result }}" == "failure" ]; then echo "❌ **S3 (AWS):** Failed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.s3-integration.result }}" == "skipped" ]; then echo "⏭️ **S3 (AWS):** Skipped" >> $GITHUB_STEP_SUMMARY fi if [ "${{ needs.gcs-integration.result }}" == "success" ]; then echo "✅ **Google Cloud Storage:** Passed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.gcs-integration.result }}" == "failure" ]; then echo "❌ **Google Cloud Storage:** Failed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.gcs-integration.result }}" == "skipped" ]; then echo "⏭️ **Google Cloud Storage:** Skipped" >> $GITHUB_STEP_SUMMARY fi if [ "${{ needs.abs-integration.result }}" == "success" ]; then echo "✅ **Azure Blob Storage:** Passed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.abs-integration.result }}" == "failure" ]; then echo "❌ **Azure Blob Storage:** Failed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.abs-integration.result }}" == "skipped" ]; then echo "⏭️ **Azure Blob Storage:** Skipped" >> $GITHUB_STEP_SUMMARY fi if [ "${{ needs.tigris-integration.result }}" == "success" ]; then echo "✅ **Fly.io Tigris:** Passed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.tigris-integration.result }}" == "failure" ]; then echo "❌ **Fly.io Tigris:** Failed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.tigris-integration.result }}" == "skipped" ]; then echo "⏭️ **Fly.io Tigris:** Skipped" >> $GITHUB_STEP_SUMMARY fi if [ "${{ needs.r2-integration.result }}" == "success" ]; then echo "✅ **Cloudflare R2:** Passed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.r2-integration.result }}" == "failure" ]; then echo "❌ **Cloudflare R2:** Failed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.r2-integration.result }}" == "skipped" ]; then echo "⏭️ **Cloudflare R2:** Skipped" >> $GITHUB_STEP_SUMMARY fi if [ "${{ needs.b2-integration.result }}" == "success" ]; then echo "✅ **Backblaze B2:** Passed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.b2-integration.result }}" == "failure" ]; then echo "❌ **Backblaze B2:** Failed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.b2-integration.result }}" == "skipped" ]; then echo "⏭️ **Backblaze B2:** Skipped" >> $GITHUB_STEP_SUMMARY fi if [ "${{ needs.multipart-stress.result }}" == "success" ]; then echo "✅ **Multipart Stress Tests:** Passed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.multipart-stress.result }}" == "failure" ]; then echo "❌ **Multipart Stress Tests:** Failed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.multipart-stress.result }}" == "skipped" ]; then echo "⏭️ **Multipart Stress Tests:** Skipped" >> $GITHUB_STEP_SUMMARY fi echo "" >> $GITHUB_STEP_SUMMARY echo "---" >> $GITHUB_STEP_SUMMARY echo "**Triggered by:** @${{ github.actor }}" >> $GITHUB_STEP_SUMMARY echo "**Run ID:** [${{ github.run_id }}](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})" >> $GITHUB_STEP_SUMMARY echo "**Workflow:** ${{ github.workflow }}" >> $GITHUB_STEP_SUMMARY - name: Comment on PR (if applicable) if: github.event.inputs.pr_number != '' continue-on-error: true uses: actions/github-script@v7 with: script: | const pr_number = ${{ github.event.inputs.pr_number }}; const run_url = `https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}`; // Build status emoji based on results let statusEmoji = '✅'; const failedJobs = []; if ('${{ needs.s3-integration.result }}' === 'failure') failedJobs.push('S3'); if ('${{ needs.gcs-integration.result }}' === 'failure') failedJobs.push('GCS'); if ('${{ needs.abs-integration.result }}' === 'failure') failedJobs.push('Azure'); if ('${{ needs.tigris-integration.result }}' === 'failure') failedJobs.push('Tigris'); if ('${{ needs.r2-integration.result }}' === 'failure') failedJobs.push('R2'); if ('${{ needs.b2-integration.result }}' === 'failure') failedJobs.push('B2'); if ('${{ needs.multipart-stress.result }}' === 'failure') failedJobs.push('Multipart'); if (failedJobs.length > 0) { statusEmoji = '❌'; } let body = `${statusEmoji} **Manual integration tests** have been run by @${{ github.actor }}\n\n`; if (failedJobs.length > 0) { body += `Failed jobs: ${failedJobs.join(', ')}\n\n`; } body += `[View test results](${run_url})`; try { await github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, issue_number: pr_number, body: body }); console.log(`Successfully commented on PR #${pr_number}`); } catch (error) { console.log(`Failed to comment on PR #${pr_number}: ${error.message}`); // Don't fail the workflow if commenting fails } ================================================ FILE: .github/workflows/pr-metrics.yml ================================================ name: PR Build Metrics on: pull_request: types: [opened, synchronize, reopened] concurrency: group: pr-metrics-${{ github.event.pull_request.number }} cancel-in-progress: true permissions: contents: read pull-requests: write jobs: build-base: runs-on: ubuntu-latest steps: - name: Checkout base branch uses: actions/checkout@v4 with: ref: ${{ github.event.pull_request.base.sha }} - name: Set up Go id: setup-go uses: actions/setup-go@v5 with: go-version-file: go.mod - name: Cache base binary id: cache-base uses: actions/cache@v4 with: path: litestream-base key: pr-metrics-base-${{ github.event.pull_request.base.sha }}-${{ steps.setup-go.outputs.go-version }} - name: Build base binary if: steps.cache-base.outputs.cache-hit != 'true' run: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o litestream-base ./cmd/litestream - name: Record base binary size run: stat --format=%s litestream-base > base-size.txt - name: Upload base size uses: actions/upload-artifact@v4 with: name: base-size path: base-size.txt build-pr: runs-on: ubuntu-latest steps: - name: Checkout PR branch uses: actions/checkout@v4 - name: Set up Go uses: actions/setup-go@v5 with: go-version-file: go.mod - name: Build PR binary run: | START=$SECONDS CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o litestream-pr ./cmd/litestream echo "$((SECONDS - START))" > build-time.txt go version | awk '{print $3}' > go-version.txt - name: Record PR binary size run: stat --format=%s litestream-pr > pr-size.txt - name: Upload PR size uses: actions/upload-artifact@v4 with: name: pr-size path: pr-size.txt - name: Upload build info uses: actions/upload-artifact@v4 with: name: build-info path: | build-time.txt go-version.txt analyze-deps: runs-on: ubuntu-latest steps: - name: Checkout PR branch uses: actions/checkout@v4 with: path: pr - name: Checkout base branch uses: actions/checkout@v4 with: ref: ${{ github.event.pull_request.base.sha }} path: base - name: Set up Go uses: actions/setup-go@v5 with: go-version-file: pr/go.mod - name: Diff go.mod dependencies run: | extract_require_deps() { local gomod="$1" awk '/^require \(/{found=1; next} found && /^\)/{found=0} found && /^\t/' "$gomod" | sed 's/^\t//' | sort } extract_require_deps base/go.mod > /tmp/base-deps.txt extract_require_deps pr/go.mod > /tmp/pr-deps.txt { echo "## Added" comm -13 /tmp/base-deps.txt /tmp/pr-deps.txt echo "## Removed" comm -23 /tmp/base-deps.txt /tmp/pr-deps.txt } > deps-diff.txt - name: Module graph size run: | cd base && go mod graph | wc -l | tr -d ' ' > ../base-graph-size.txt && cd .. cd pr && go mod graph | wc -l | tr -d ' ' > ../pr-graph-size.txt && cd .. - name: Run govulncheck run: | go install golang.org/x/vuln/cmd/govulncheck@latest cd pr govulncheck ./... > ../vulncheck-results.txt 2>&1 || true - name: Check Go toolchain freshness run: | CURRENT=$(grep '^toolchain ' pr/go.mod | awk '{print $2}' | sed 's/^go//') if [ -z "$CURRENT" ]; then CURRENT=$(grep '^go ' pr/go.mod | awk '{print $2}') fi MINOR=$(echo "$CURRENT" | grep -oE '^[0-9]+\.[0-9]+') LATEST=$(curl -sf 'https://go.dev/dl/?mode=json&include=all' | \ python3 -c "import sys,json; releases=json.load(sys.stdin); print(next(r['version'] for r in releases if r['stable'] and r['version'].startswith('go${MINOR}.')))" 2>/dev/null | sed 's/^go//') if [ -z "$LATEST" ]; then echo "current=${CURRENT}" > go-toolchain.txt echo "latest=unknown" >> go-toolchain.txt echo "stale=false" >> go-toolchain.txt elif [ "$CURRENT" = "$LATEST" ]; then echo "current=${CURRENT}" > go-toolchain.txt echo "latest=${LATEST}" >> go-toolchain.txt echo "stale=false" >> go-toolchain.txt else echo "current=${CURRENT}" > go-toolchain.txt echo "latest=${LATEST}" >> go-toolchain.txt echo "stale=true" >> go-toolchain.txt fi - name: Upload dependency analysis uses: actions/upload-artifact@v4 with: name: dep-analysis path: | deps-diff.txt base-graph-size.txt pr-graph-size.txt vulncheck-results.txt go-toolchain.txt post-comment: if: github.event.pull_request.head.repo.full_name == github.repository runs-on: ubuntu-latest needs: [build-base, build-pr, analyze-deps] steps: - name: Download all artifacts uses: actions/download-artifact@v4 - name: Post PR comment and manage labels uses: actions/github-script@v7 with: script: | const fs = require('fs'); const owner = context.repo.owner; const repo = context.repo.repo; const issue_number = context.issue.number; const baseSize = parseInt(fs.readFileSync('base-size/base-size.txt', 'utf8').trim()); const prSize = parseInt(fs.readFileSync('pr-size/pr-size.txt', 'utf8').trim()); const buildTime = fs.readFileSync('build-info/build-time.txt', 'utf8').trim(); const goVersion = fs.readFileSync('build-info/go-version.txt', 'utf8').trim(); const depsDiff = fs.readFileSync('dep-analysis/deps-diff.txt', 'utf8').trim(); const baseGraphSize = fs.readFileSync('dep-analysis/base-graph-size.txt', 'utf8').trim(); const prGraphSize = fs.readFileSync('dep-analysis/pr-graph-size.txt', 'utf8').trim(); let vulncheck = fs.readFileSync('dep-analysis/vulncheck-results.txt', 'utf8').trim(); const toolchainData = fs.readFileSync('dep-analysis/go-toolchain.txt', 'utf8').trim(); const MAX_VULNCHECK_LEN = 10000; if (vulncheck.length > MAX_VULNCHECK_LEN) { vulncheck = vulncheck.substring(0, MAX_VULNCHECK_LEN) + '\n... (truncated, see workflow run for full output)'; } // --- Parse Go toolchain freshness --- const tcLines = Object.fromEntries(toolchainData.split('\n').map(l => l.split('='))); const goCurrent = tcLines.current || 'unknown'; const goLatest = tcLines.latest || 'unknown'; const goStale = tcLines.stale === 'true'; // --- Compute metrics --- const diff = prSize - baseSize; const absPct = baseSize > 0 ? Math.abs((diff / baseSize) * 100) : 0; const pct = baseSize > 0 ? ((diff / baseSize) * 100).toFixed(2) : 'N/A'; const sign = diff > 0 ? '+' : ''; const fmt = (bytes) => (bytes / 1024 / 1024).toFixed(2) + ' MB'; const addedSection = depsDiff.split('## Removed')[0].replace('## Added', '').trim(); const removedSection = depsDiff.split('## Removed')[1]?.trim() || ''; const addedDeps = addedSection ? addedSection.split('\n').filter(l => l.trim()) : []; const removedDeps = removedSection ? removedSection.split('\n').filter(l => l.trim()) : []; const depsChanged = addedDeps.length + removedDeps.length; const graphDiff = parseInt(prGraphSize) - parseInt(baseGraphSize); const graphSign = graphDiff > 0 ? '+' : ''; const hasVulns = vulncheck.includes('Vulnerability #') || vulncheck.includes('GO-'); // --- Determine status --- const flags = []; let sizeIcon = '✅'; if (diff > 0 && absPct >= 10) { sizeIcon = '🚨'; flags.push('binary size >10%'); } else if (diff > 0 && absPct >= 5) { sizeIcon = '⚠️'; flags.push('binary size >5%'); } else if (diff < 0) { sizeIcon = '📉'; } const vulnIcon = hasVulns ? '⚠️' : '✅'; if (hasVulns) flags.push('vulnerabilities found'); const goIcon = goStale ? '⚠️' : '✅'; if (goStale) flags.push(`Go toolchain outdated (${goCurrent} → ${goLatest})`); const depsIcon = depsChanged > 0 ? 'ℹ️' : '✅'; const overallIcon = flags.length > 0 ? '⚠️' : '✅'; const overallText = flags.length > 0 ? `**Attention needed** — ${flags.join(', ')}` : '**All clear** — no issues detected'; // --- Build compact comment --- let depsDetail = 'No dependency changes.'; if (depsChanged > 0) { const parts = []; if (addedDeps.length > 0) parts.push('**Added:**\n' + addedDeps.map(d => `- \`${d}\``).join('\n')); if (removedDeps.length > 0) parts.push('**Removed:**\n' + removedDeps.map(d => `- \`${d}\``).join('\n')); depsDetail = parts.join('\n\n'); } const body = `## PR Build Metrics ${overallIcon} ${overallText} | Check | Status | Summary | |:------|:------:|:--------| | Binary size | ${sizeIcon} | ${fmt(prSize)} (${sign}${(diff / 1024).toFixed(1)} KB / ${sign}${pct}%) | | Dependencies | ${depsIcon} | ${depsChanged > 0 ? `${addedDeps.length} added, ${removedDeps.length} removed` : 'No changes'} | | Vulnerabilities | ${vulnIcon} | ${hasVulns ? 'Issues found — expand details below' : 'None detected'} | | Go toolchain | ${goIcon} | ${goCurrent}${goStale ? ` → ${goLatest} available` : ' (latest)'} | | Module graph | ✅ | ${prGraphSize} edges (${graphSign}${graphDiff}) | ### Binary Size | | Size | Change | |---|---:|---:| | Base (\`${context.payload.pull_request.base.sha.substring(0, 7)}\`) | ${fmt(baseSize)} | | | PR (\`${context.sha.substring(0, 7)}\`) | ${fmt(prSize)} | ${sign}${(diff / 1024).toFixed(1)} KB (${sign}${pct}%) | ### Dependency Changes ${depsDetail} ### govulncheck Output \`\`\` ${vulncheck} \`\`\` ### Build Info | Metric | Value | |---|---| | Build time | ${buildTime}s | | Go version | \`${goVersion}\` | | Commit | \`${context.sha.substring(0, 7)}\` | --- 🤖 Updated on each push.`.replace(/^ /gm, ''); // --- Post or update comment (with history) --- const marker = '## PR Build Metrics'; const historyMarker = ''; let existing = null; for await (const response of github.paginate.iterator( github.rest.issues.listComments, { owner, repo, issue_number, per_page: 100 } )) { existing = response.data.find(c => c.body?.startsWith(marker)); if (existing) break; } if (existing) { // Extract previous summary line and history from existing comment const prevBody = existing.body; const prevStatusMatch = prevBody.match(/\| Binary size \|[^\n]+/); const prevCommitMatch = prevBody.match(/\| Commit \| `([^`]+)` \|/); const prevSummary = prevStatusMatch ? prevStatusMatch[0] : null; const prevCommit = prevCommitMatch ? prevCommitMatch[1] : '?'; // Extract existing history entries let historyEntries = ''; const historyIdx = prevBody.indexOf(historyMarker); if (historyIdx !== -1) { const afterMarker = prevBody.substring(historyIdx + historyMarker.length); // Support both old
format and new plain format const detailsMatch = afterMarker.match(/
[\s\S]*?<\/details>/); if (detailsMatch) { const innerMatch = detailsMatch[0].match(/[^<]*<\/summary>([\s\S]*?)<\/details>/); if (innerMatch) historyEntries = innerMatch[1].trim(); } else { // Plain format: extract table rows after the header const tableMatch = afterMarker.match(/\| Commit \| Updated[\s\S]*?(?=\n---|\n$|$)/); if (tableMatch) historyEntries = tableMatch[0].trim(); } } // Build new history (most recent first, cap at 10) const now = new Date().toISOString().replace('T', ' ').substring(0, 16) + ' UTC'; const newEntry = prevSummary ? `| \`${prevCommit}\` | ${now} | ${prevSummary.replace(/\| Binary size \|/, '').trim().replace(/^\||\|$/g, '').trim()} |` : null; let historyRows = ''; if (newEntry || historyEntries) { const existingRows = historyEntries .split('\n') .filter(l => l.startsWith('|') && !l.startsWith('| Commit') && !l.startsWith('|:')) .slice(0, 9); const allRows = newEntry ? [newEntry, ...existingRows] : existingRows; if (allRows.length > 0) { historyRows = `\n### History (${allRows.length} previous)\n\n| Commit | Updated | Status | Summary |\n|:-------|:--------|:------:|:--------|\n${allRows.join('\n')}`; } } // Insert history into body const finalBody = body.replace(historyMarker, historyMarker + historyRows); await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body: finalBody }); } else { await github.rest.issues.createComment({ owner, repo, issue_number, body }); } // --- Manage labels --- const LABELS = { SIZE_WARNING: 'metrics: size-warning', SIZE_ALERT: 'metrics: size-alert', VULNS: 'metrics: vulns-found', GO_UPDATE: 'metrics: go-update', }; const ensureLabel = async (name, color, description) => { try { await github.rest.issues.getLabel({ owner, repo, name }); } catch { await github.rest.issues.createLabel({ owner, repo, name, color, description }); } }; const addLabel = async (name) => { await github.rest.issues.addLabels({ owner, repo, issue_number, labels: [name] }); }; const removeLabel = async (name) => { try { await github.rest.issues.removeLabel({ owner, repo, issue_number, name }); } catch { /* label not present, ignore */ } }; // Size labels await ensureLabel(LABELS.SIZE_WARNING, 'fbca04', 'Binary size increased 5-10%'); await ensureLabel(LABELS.SIZE_ALERT, 'e11d48', 'Binary size increased >10%'); await ensureLabel(LABELS.VULNS, 'e11d48', 'govulncheck found vulnerabilities'); await ensureLabel(LABELS.GO_UPDATE, 'fbca04', 'Go toolchain has a newer patch release'); if (diff > 0 && absPct >= 10) { await addLabel(LABELS.SIZE_ALERT); await removeLabel(LABELS.SIZE_WARNING); } else if (diff > 0 && absPct >= 5) { await addLabel(LABELS.SIZE_WARNING); await removeLabel(LABELS.SIZE_ALERT); } else { await removeLabel(LABELS.SIZE_WARNING); await removeLabel(LABELS.SIZE_ALERT); } // Vuln label if (hasVulns) { await addLabel(LABELS.VULNS); } else { await removeLabel(LABELS.VULNS); } // Go toolchain label if (goStale) { await addLabel(LABELS.GO_UPDATE); } else { await removeLabel(LABELS.GO_UPDATE); } ================================================ FILE: .github/workflows/pre-release-checklist.yml ================================================ name: Pre-Release Checklist # Advisory workflow to verify release readiness # This workflow reports results but does NOT block releases # Run manually before tagging a new release permissions: contents: read issues: write pull-requests: write on: workflow_dispatch: inputs: version: description: 'Version being released (e.g., v0.5.6)' required: true type: string create_issue: description: 'Create a GitHub issue with results' required: false default: true type: boolean test_cloud_providers: description: 'Run cloud provider integration tests (S3, GCS, ABS, R2)' required: false default: true type: boolean test_multipart: description: 'Run multipart upload stress tests' required: false default: true type: boolean jobs: unit-tests: name: Unit Tests runs-on: ubuntu-latest outputs: result: ${{ steps.test.outcome }} steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - name: Run unit tests id: test run: go test -v -race ./... build-verification: name: Build Verification runs-on: ubuntu-latest outputs: result: ${{ steps.build.outcome }} steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - name: Build main binary id: build run: | go build -o bin/litestream ./cmd/litestream ./bin/litestream version - name: Verify binary runs run: | ./bin/litestream --help ./bin/litestream databases --help ./bin/litestream replicate --help ./bin/litestream restore --help s3-integration: name: S3 Integration (AWS) runs-on: ubuntu-latest if: github.event.inputs.test_cloud_providers == 'true' outputs: result: ${{ steps.test.outcome }} steps: - name: Check for required secrets id: check-secrets run: | if [ -z "${{ secrets.LITESTREAM_S3_ACCESS_KEY_ID }}" ]; then echo "::notice title=Skipped::S3 integration tests skipped - credentials not configured" echo "has_secrets=false" >> $GITHUB_OUTPUT else echo "has_secrets=true" >> $GITHUB_OUTPUT fi - uses: actions/checkout@v4 if: steps.check-secrets.outputs.has_secrets == 'true' - uses: actions/setup-go@v5 if: steps.check-secrets.outputs.has_secrets == 'true' with: go-version-file: "go.mod" - name: Run S3 integration tests id: test if: steps.check-secrets.outputs.has_secrets == 'true' continue-on-error: true run: go test -v ./replica_client_test.go -integration -replica-clients=s3 env: LITESTREAM_S3_ACCESS_KEY_ID: ${{ secrets.LITESTREAM_S3_ACCESS_KEY_ID }} LITESTREAM_S3_SECRET_ACCESS_KEY: ${{ secrets.LITESTREAM_S3_SECRET_ACCESS_KEY }} LITESTREAM_S3_REGION: us-east-1 LITESTREAM_S3_BUCKET: integration.litestream.io gcs-integration: name: GCS Integration runs-on: ubuntu-latest if: github.event.inputs.test_cloud_providers == 'true' outputs: result: ${{ steps.test.outcome }} steps: - name: Check for required secrets id: check-secrets run: | if [ -z "${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}" ]; then echo "::notice title=Skipped::GCS integration tests skipped - credentials not configured" echo "has_secrets=false" >> $GITHUB_OUTPUT else echo "has_secrets=true" >> $GITHUB_OUTPUT fi - name: Extract GCP credentials if: steps.check-secrets.outputs.has_secrets == 'true' run: 'echo "$GOOGLE_APPLICATION_CREDENTIALS" > /opt/gcp.json' shell: bash env: GOOGLE_APPLICATION_CREDENTIALS: ${{secrets.GOOGLE_APPLICATION_CREDENTIALS}} - uses: actions/checkout@v4 if: steps.check-secrets.outputs.has_secrets == 'true' - uses: actions/setup-go@v5 if: steps.check-secrets.outputs.has_secrets == 'true' with: go-version-file: "go.mod" - name: Run GCS integration tests id: test if: steps.check-secrets.outputs.has_secrets == 'true' continue-on-error: true run: go test -v ./replica_client_test.go -integration -replica-clients=gs env: GOOGLE_APPLICATION_CREDENTIALS: /opt/gcp.json LITESTREAM_GS_BUCKET: litestream-github-workflows abs-integration: name: Azure Blob Storage Integration runs-on: ubuntu-latest if: github.event.inputs.test_cloud_providers == 'true' outputs: result: ${{ steps.test.outcome }} steps: - name: Check for required secrets id: check-secrets run: | if [ -z "${{ secrets.LITESTREAM_ABS_ACCOUNT_NAME }}" ]; then echo "::notice title=Skipped::Azure Blob Storage integration tests skipped - credentials not configured" echo "has_secrets=false" >> $GITHUB_OUTPUT else echo "has_secrets=true" >> $GITHUB_OUTPUT fi - uses: actions/checkout@v4 if: steps.check-secrets.outputs.has_secrets == 'true' - uses: actions/setup-go@v5 if: steps.check-secrets.outputs.has_secrets == 'true' with: go-version-file: "go.mod" - name: Run ABS integration tests id: test if: steps.check-secrets.outputs.has_secrets == 'true' continue-on-error: true run: go test -v ./replica_client_test.go -integration -replica-clients=abs env: LITESTREAM_ABS_ACCOUNT_NAME: ${{ secrets.LITESTREAM_ABS_ACCOUNT_NAME }} LITESTREAM_ABS_ACCOUNT_KEY: ${{ secrets.LITESTREAM_ABS_ACCOUNT_KEY }} LITESTREAM_ABS_BUCKET: integration r2-integration: name: Cloudflare R2 Integration runs-on: ubuntu-latest if: github.event.inputs.test_cloud_providers == 'true' outputs: result: ${{ steps.test.outcome }} steps: - name: Check for required secrets id: check-secrets run: | if [ -z "${{ secrets.LITESTREAM_R2_ACCESS_KEY_ID }}" ]; then echo "::notice title=Skipped::R2 integration tests skipped - credentials not configured" echo "has_secrets=false" >> $GITHUB_OUTPUT else echo "has_secrets=true" >> $GITHUB_OUTPUT fi - uses: actions/checkout@v4 if: steps.check-secrets.outputs.has_secrets == 'true' - uses: actions/setup-go@v5 if: steps.check-secrets.outputs.has_secrets == 'true' with: go-version-file: "go.mod" - name: Run R2 integration tests id: test if: steps.check-secrets.outputs.has_secrets == 'true' continue-on-error: true run: go test -v ./replica_client_test.go -integration -replica-clients=r2 env: LITESTREAM_R2_ACCESS_KEY_ID: ${{ secrets.LITESTREAM_R2_ACCESS_KEY_ID }} LITESTREAM_R2_SECRET_ACCESS_KEY: ${{ secrets.LITESTREAM_R2_SECRET_ACCESS_KEY }} LITESTREAM_R2_ENDPOINT: ${{ secrets.LITESTREAM_R2_ENDPOINT }} LITESTREAM_R2_BUCKET: ${{ secrets.LITESTREAM_R2_BUCKET }} multipart-stress: name: Multipart Upload Stress Tests runs-on: ubuntu-latest if: github.event.inputs.test_multipart == 'true' outputs: result: ${{ steps.test.outcome }} steps: - name: Check for required secrets id: check-secrets run: | if [ -z "${{ secrets.LITESTREAM_S3_ACCESS_KEY_ID }}" ]; then echo "::notice title=Skipped::Multipart stress tests skipped - S3 credentials not configured" echo "has_secrets=false" >> $GITHUB_OUTPUT else echo "has_secrets=true" >> $GITHUB_OUTPUT fi - uses: actions/checkout@v4 if: steps.check-secrets.outputs.has_secrets == 'true' - uses: actions/setup-go@v5 if: steps.check-secrets.outputs.has_secrets == 'true' with: go-version-file: "go.mod" - name: Run multipart stress tests id: test if: steps.check-secrets.outputs.has_secrets == 'true' continue-on-error: true run: | go test -v ./replica_client_test.go -integration -replica-clients=s3 \ -run "TestReplicaClient_S3_Multipart|TestReplicaClient_S3_Concurrency" \ -timeout 30m env: LITESTREAM_S3_ACCESS_KEY_ID: ${{ secrets.LITESTREAM_S3_ACCESS_KEY_ID }} LITESTREAM_S3_SECRET_ACCESS_KEY: ${{ secrets.LITESTREAM_S3_SECRET_ACCESS_KEY }} LITESTREAM_S3_REGION: us-east-1 LITESTREAM_S3_BUCKET: integration.litestream.io generate-checklist: name: Generate Release Checklist runs-on: ubuntu-latest needs: - unit-tests - build-verification - s3-integration - gcs-integration - abs-integration - r2-integration - multipart-stress if: always() steps: - name: Generate checklist summary id: checklist run: | echo "## Pre-Release Checklist for ${{ github.event.inputs.version }}" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY echo "**Date:** $(date -u '+%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_STEP_SUMMARY echo "**Triggered by:** @${{ github.actor }}" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY echo "### Core Tests" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY if [ "${{ needs.unit-tests.result }}" == "success" ]; then echo "- [x] Unit tests passed" >> $GITHUB_STEP_SUMMARY else echo "- [ ] Unit tests **FAILED**" >> $GITHUB_STEP_SUMMARY fi if [ "${{ needs.build-verification.result }}" == "success" ]; then echo "- [x] Build verification passed" >> $GITHUB_STEP_SUMMARY else echo "- [ ] Build verification **FAILED**" >> $GITHUB_STEP_SUMMARY fi echo "" >> $GITHUB_STEP_SUMMARY echo "### Cloud Provider Integration Tests" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY if [ "${{ needs.s3-integration.result }}" == "success" ]; then echo "- [x] AWS S3 integration passed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.s3-integration.result }}" == "skipped" ]; then echo "- [ ] AWS S3 integration (skipped)" >> $GITHUB_STEP_SUMMARY else echo "- [ ] AWS S3 integration **FAILED**" >> $GITHUB_STEP_SUMMARY fi if [ "${{ needs.gcs-integration.result }}" == "success" ]; then echo "- [x] Google Cloud Storage integration passed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.gcs-integration.result }}" == "skipped" ]; then echo "- [ ] Google Cloud Storage integration (skipped)" >> $GITHUB_STEP_SUMMARY else echo "- [ ] Google Cloud Storage integration **FAILED**" >> $GITHUB_STEP_SUMMARY fi if [ "${{ needs.abs-integration.result }}" == "success" ]; then echo "- [x] Azure Blob Storage integration passed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.abs-integration.result }}" == "skipped" ]; then echo "- [ ] Azure Blob Storage integration (skipped)" >> $GITHUB_STEP_SUMMARY else echo "- [ ] Azure Blob Storage integration **FAILED**" >> $GITHUB_STEP_SUMMARY fi if [ "${{ needs.r2-integration.result }}" == "success" ]; then echo "- [x] Cloudflare R2 integration passed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.r2-integration.result }}" == "skipped" ]; then echo "- [ ] Cloudflare R2 integration (skipped)" >> $GITHUB_STEP_SUMMARY else echo "- [ ] Cloudflare R2 integration **FAILED**" >> $GITHUB_STEP_SUMMARY fi echo "" >> $GITHUB_STEP_SUMMARY echo "### Stress Tests" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY if [ "${{ needs.multipart-stress.result }}" == "success" ]; then echo "- [x] Multipart upload stress tests passed" >> $GITHUB_STEP_SUMMARY elif [ "${{ needs.multipart-stress.result }}" == "skipped" ]; then echo "- [ ] Multipart upload stress tests (skipped)" >> $GITHUB_STEP_SUMMARY else echo "- [ ] Multipart upload stress tests **FAILED**" >> $GITHUB_STEP_SUMMARY fi echo "" >> $GITHUB_STEP_SUMMARY echo "---" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY echo "**Note:** This checklist is advisory only." >> $GITHUB_STEP_SUMMARY echo "Review failed items before proceeding with the release." >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY echo "[View full run details](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})" >> $GITHUB_STEP_SUMMARY # Create issue body for later use cat > /tmp/issue_body.md << 'ISSUE_EOF' ## Pre-Release Checklist for ${{ github.event.inputs.version }} **Date:** $(date -u '+%Y-%m-%d %H:%M:%S UTC') **Triggered by:** @${{ github.actor }} **Workflow Run:** https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} ### Core Tests | Test | Status | |------|--------| | Unit Tests | ${{ needs.unit-tests.result == 'success' && '✅ Passed' || '❌ Failed' }} | | Build Verification | ${{ needs.build-verification.result == 'success' && '✅ Passed' || '❌ Failed' }} | ### Cloud Provider Integration Tests | Provider | Status | |----------|--------| | AWS S3 | ${{ needs.s3-integration.result == 'success' && '✅ Passed' || needs.s3-integration.result == 'skipped' && '⏭️ Skipped' || '❌ Failed' }} | | Google Cloud Storage | ${{ needs.gcs-integration.result == 'success' && '✅ Passed' || needs.gcs-integration.result == 'skipped' && '⏭️ Skipped' || '❌ Failed' }} | | Azure Blob Storage | ${{ needs.abs-integration.result == 'success' && '✅ Passed' || needs.abs-integration.result == 'skipped' && '⏭️ Skipped' || '❌ Failed' }} | | Cloudflare R2 | ${{ needs.r2-integration.result == 'success' && '✅ Passed' || needs.r2-integration.result == 'skipped' && '⏭️ Skipped' || '❌ Failed' }} | ### Stress Tests | Test | Status | |------|--------| | Multipart Uploads | ${{ needs.multipart-stress.result == 'success' && '✅ Passed' || needs.multipart-stress.result == 'skipped' && '⏭️ Skipped' || '❌ Failed' }} | --- **Note:** This checklist is advisory only. Review any failed items before proceeding with the release. ISSUE_EOF - name: Create GitHub Issue if: github.event.inputs.create_issue == 'true' continue-on-error: true uses: actions/github-script@v7 with: script: | const version = '${{ github.event.inputs.version }}'; const runUrl = `https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}`; // Determine overall status const results = { unit: '${{ needs.unit-tests.result }}', build: '${{ needs.build-verification.result }}', s3: '${{ needs.s3-integration.result }}', gcs: '${{ needs.gcs-integration.result }}', abs: '${{ needs.abs-integration.result }}', r2: '${{ needs.r2-integration.result }}', multipart: '${{ needs.multipart-stress.result }}' }; const failed = Object.entries(results) .filter(([_, v]) => v === 'failure') .map(([k, _]) => k); const status = failed.length === 0 ? '✅' : '⚠️'; const title = `${status} Pre-Release Checklist: ${version}`; let body = `## Pre-Release Checklist for ${version}\n\n`; body += `**Workflow Run:** [View Details](${runUrl})\n\n`; body += `### Results Summary\n\n`; body += `| Test | Status |\n`; body += `|------|--------|\n`; body += `| Unit Tests | ${results.unit === 'success' ? '✅' : '❌'} |\n`; body += `| Build | ${results.build === 'success' ? '✅' : '❌'} |\n`; body += `| AWS S3 | ${results.s3 === 'success' ? '✅' : results.s3 === 'skipped' ? '⏭️' : '❌'} |\n`; body += `| GCS | ${results.gcs === 'success' ? '✅' : results.gcs === 'skipped' ? '⏭️' : '❌'} |\n`; body += `| Azure | ${results.abs === 'success' ? '✅' : results.abs === 'skipped' ? '⏭️' : '❌'} |\n`; body += `| R2 | ${results.r2 === 'success' ? '✅' : results.r2 === 'skipped' ? '⏭️' : '❌'} |\n`; body += `| Multipart | ${results.multipart === 'success' ? '✅' : results.multipart === 'skipped' ? '⏭️' : '❌'} |\n\n`; if (failed.length > 0) { body += `### ⚠️ Failed Tests\n\n`; body += `The following tests failed and should be reviewed:\n`; failed.forEach(f => body += `- ${f}\n`); body += `\n`; } body += `---\n`; body += `*This issue was automatically created by the pre-release checklist workflow.*\n`; try { await github.rest.issues.create({ owner: context.repo.owner, repo: context.repo.repo, title: title, body: body, labels: ['release-checklist'] }); console.log('Created release checklist issue'); } catch (error) { console.log(`Failed to create issue: ${error.message}`); } ================================================ FILE: .github/workflows/release.docker.yml ================================================ on: release: types: - published # pull_request: # types: # - opened # - synchronize # - reopened # branches-ignore: # - "dependabot/**" name: Release (Docker) jobs: docker: runs-on: ubuntu-latest env: # CGO cross-compilation supported platforms PLATFORMS: "linux/amd64,linux/arm64" VERSION: "${{ github.event_name == 'release' && github.event.release.tag_name || github.sha }}" steps: - uses: actions/checkout@v4 - uses: docker/setup-qemu-action@v3 - uses: docker/setup-buildx-action@v3 - uses: docker/login-action@v3 with: username: benbjohnson password: ${{ secrets.DOCKERHUB_TOKEN }} # Default (Debian) image - id: meta-default uses: docker/metadata-action@v5 with: images: litestream/litestream tags: | type=ref,event=branch type=ref,event=pr type=sha type=sha,format=long type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} - uses: docker/build-push-action@v6 with: context: . target: default push: true provenance: true sbom: true platforms: ${{ env.PLATFORMS }} tags: ${{ steps.meta-default.outputs.tags }} labels: ${{ steps.meta-default.outputs.labels }} build-args: | LITESTREAM_VERSION=${{ env.VERSION }} # Hardened (Scratch) image - id: meta-scratch uses: docker/metadata-action@v5 with: images: litestream/litestream flavor: | latest=auto suffix=-scratch tags: | type=ref,event=branch type=ref,event=pr type=sha type=sha,format=long type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} - uses: docker/build-push-action@v6 with: context: . target: hardened push: true provenance: true sbom: true platforms: ${{ env.PLATFORMS }} tags: ${{ steps.meta-scratch.outputs.tags }} labels: ${{ steps.meta-scratch.outputs.labels }} build-args: | LITESTREAM_VERSION=${{ env.VERSION }} ================================================ FILE: .github/workflows/release.yml ================================================ name: Release on: push: tags: - 'v*' workflow_dispatch: inputs: tag: description: 'Release tag (e.g., v0.3.14)' required: true type: string permissions: contents: write packages: write id-token: write jobs: goreleaser: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0 - name: Set up Go uses: actions/setup-go@v5 with: go-version-file: go.mod cache: true - name: Install cross-compilers run: | sudo apt-get update sudo apt-get install -y gcc-aarch64-linux-gnu gcc-arm-linux-gnueabihf gcc-arm-linux-gnueabi - name: Set up QEMU uses: docker/setup-qemu-action@v3 - name: Install Syft for SBOM generation uses: anchore/sbom-action/download-syft@v0 with: syft-version: latest - name: Import GPG key if: ${{ env.GPG_PRIVATE_KEY != '' }} id: import_gpg uses: crazy-max/ghaction-import-gpg@v6 with: gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }} passphrase: ${{ secrets.GPG_PASSPHRASE }} - name: Run GoReleaser uses: goreleaser/goreleaser-action@v6 with: version: latest args: release --clean env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }} HOMEBREW_TAP_GITHUB_TOKEN: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }} vfs-build-linux: name: Build VFS (Linux ${{ matrix.arch }}) runs-on: ubuntu-22.04 # glibc 2.35 (ubuntu-20.04 runners deprecated) needs: goreleaser strategy: matrix: arch: [amd64, arm64] steps: - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0 - name: Set up Go uses: actions/setup-go@v5 with: go-version-file: go.mod cache: true - name: Install cross-compiler (arm64) if: matrix.arch == 'arm64' run: | sudo apt-get update sudo apt-get install -y gcc-aarch64-linux-gnu - name: Get version id: version run: | if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then echo "version=${{ inputs.tag }}" >> $GITHUB_OUTPUT else echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT fi - name: Build VFS extension run: make vfs-linux-${{ matrix.arch }} - name: Verify artifact run: | file dist/litestream-vfs-linux-${{ matrix.arch }}.so # For native arch, verify it can be loaded by SQLite if [ "${{ matrix.arch }}" == "amd64" ]; then sqlite3 ':memory:' ".load dist/litestream-vfs-linux-amd64.so" ".exit" && echo "Extension loads successfully" fi # For cross-compiled arch, verify ELF header and architecture if [ "${{ matrix.arch }}" == "arm64" ]; then readelf -h dist/litestream-vfs-linux-arm64.so | grep -E "(Class|Machine)" echo "ARM64 ELF header verified" fi - name: Create archive run: | cd dist cp litestream-vfs-linux-${{ matrix.arch }}.so litestream.so tar -czvf litestream-vfs-${{ steps.version.outputs.version }}-linux-${{ matrix.arch }}.tar.gz \ litestream.so - name: Generate checksum run: | cd dist sha256sum litestream-vfs-${{ steps.version.outputs.version }}-linux-${{ matrix.arch }}.tar.gz \ > litestream-vfs-${{ steps.version.outputs.version }}-linux-${{ matrix.arch }}.tar.gz.sha256 - name: Upload to release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | gh release upload ${{ steps.version.outputs.version }} \ dist/litestream-vfs-${{ steps.version.outputs.version }}-linux-${{ matrix.arch }}.tar.gz \ dist/litestream-vfs-${{ steps.version.outputs.version }}-linux-${{ matrix.arch }}.tar.gz.sha256 \ --clobber - name: Upload artifact for packaging uses: actions/upload-artifact@v4 with: name: vfs-linux-${{ matrix.arch }} path: dist/litestream-vfs-linux-${{ matrix.arch }}.so vfs-build-darwin: name: Build VFS (macOS ${{ matrix.arch }}) runs-on: ${{ matrix.runner }} needs: goreleaser strategy: matrix: include: - arch: amd64 runner: macos-15 - arch: arm64 runner: macos-15 steps: - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0 - name: Set up Go uses: actions/setup-go@v5 with: go-version-file: go.mod cache: true - name: Get version id: version run: | if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then echo "version=${{ inputs.tag }}" >> $GITHUB_OUTPUT else echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT fi - name: Build VFS extension run: make vfs-darwin-${{ matrix.arch }} - name: Verify artifact run: | file dist/litestream-vfs-darwin-${{ matrix.arch }}.dylib # Verify architecture matches target lipo -info dist/litestream-vfs-darwin-${{ matrix.arch }}.dylib # Verify the expected symbol exists (macOS sqlite3 doesn't support .load) nm -gU dist/litestream-vfs-darwin-${{ matrix.arch }}.dylib | grep sqlite3_litestreamvfs_init && echo "Entry point symbol found" - name: Create archive run: | cd dist cp litestream-vfs-darwin-${{ matrix.arch }}.dylib litestream.dylib tar -czvf litestream-vfs-${{ steps.version.outputs.version }}-darwin-${{ matrix.arch }}.tar.gz \ litestream.dylib - name: Generate checksum run: | cd dist shasum -a 256 litestream-vfs-${{ steps.version.outputs.version }}-darwin-${{ matrix.arch }}.tar.gz \ > litestream-vfs-${{ steps.version.outputs.version }}-darwin-${{ matrix.arch }}.tar.gz.sha256 - name: Upload to release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | gh release upload ${{ steps.version.outputs.version }} \ dist/litestream-vfs-${{ steps.version.outputs.version }}-darwin-${{ matrix.arch }}.tar.gz \ dist/litestream-vfs-${{ steps.version.outputs.version }}-darwin-${{ matrix.arch }}.tar.gz.sha256 \ --clobber - name: Upload artifact for packaging uses: actions/upload-artifact@v4 with: name: vfs-darwin-${{ matrix.arch }} path: dist/litestream-vfs-darwin-${{ matrix.arch }}.dylib # macos-sign: # runs-on: macos-latest # needs: goreleaser # strategy: # matrix: # arch: [amd64, arm64] # steps: # - name: Checkout # uses: actions/checkout@v4 # - name: Set up Go # uses: actions/setup-go@v5 # with: # go-version-file: go.mod # - name: Download release artifacts # uses: actions/download-artifact@v4 # with: # name: litestream-darwin-${{ matrix.arch }} # path: dist/ # - name: Import Apple Developer Certificate # env: # MACOS_CERTIFICATE: ${{ secrets.MACOS_CERTIFICATE_P12 }} # MACOS_CERTIFICATE_PASSWORD: ${{ secrets.MACOS_CERTIFICATE_PASSWORD }} # run: | # echo "$MACOS_CERTIFICATE" | base64 --decode > certificate.p12 # security create-keychain -p actions temp.keychain # security default-keychain -s temp.keychain # security unlock-keychain -p actions temp.keychain # security import certificate.p12 -k temp.keychain -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign # security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k actions temp.keychain # - name: Sign and Notarize # env: # APPLE_API_KEY: ${{ secrets.APPLE_API_KEY_P8 }} # APPLE_API_KEY_ID: ${{ secrets.APPLE_API_KEY_ID }} # APPLE_API_ISSUER_ID: ${{ secrets.APPLE_API_ISSUER_ID }} # AC_PASSWORD: ${{ secrets.AC_PASSWORD }} # run: | # gon etc/gon-${{ matrix.arch }}.hcl # - name: Upload signed binary # env: # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # run: | # gh release upload ${{ github.ref_name }} dist/litestream-*-darwin-${{ matrix.arch }}.zip # windows-sign: # runs-on: windows-latest # needs: goreleaser # strategy: # matrix: # arch: [amd64, arm64] # steps: # - name: Checkout # uses: actions/checkout@v4 # # - name: Download release artifacts # uses: actions/download-artifact@v4 # with: # name: litestream-windows-${{ matrix.arch }} # path: dist/ # # - name: Sign Windows binary # env: # WINDOWS_CERTIFICATE_PFX: ${{ secrets.WINDOWS_CERTIFICATE_PFX }} # WINDOWS_CERTIFICATE_PASSWORD: ${{ secrets.WINDOWS_CERTIFICATE_PASSWORD }} # run: | # echo "$env:WINDOWS_CERTIFICATE_PFX" | base64 -d > cert.pfx # & signtool sign /f cert.pfx /p "$env:WINDOWS_CERTIFICATE_PASSWORD" /fd SHA256 /td SHA256 /tr http://timestamp.digicert.com dist\litestream.exe # Remove-Item cert.pfx # # - name: Upload signed binary # env: # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # run: | # gh release upload ${{ github.ref_name }} dist\litestream-*-windows-${{ matrix.arch }}.zip publish-pypi: name: Publish to PyPI (${{ matrix.os }}-${{ matrix.arch }}) runs-on: ubuntu-latest needs: [vfs-build-linux, vfs-build-darwin] environment: release permissions: id-token: write strategy: matrix: include: - os: linux arch: amd64 ext: so src_name: litestream-vfs-linux-amd64.so dest_name: litestream-vfs.so platform_tag: manylinux_2_35_x86_64 - os: linux arch: arm64 ext: so src_name: litestream-vfs-linux-arm64.so dest_name: litestream-vfs.so platform_tag: manylinux_2_35_aarch64 - os: darwin arch: amd64 ext: dylib src_name: litestream-vfs-darwin-amd64.dylib dest_name: litestream-vfs.dylib platform_tag: macosx_11_0_x86_64 - os: darwin arch: arm64 ext: dylib src_name: litestream-vfs-darwin-arm64.dylib dest_name: litestream-vfs.dylib platform_tag: macosx_11_0_arm64 steps: - name: Checkout uses: actions/checkout@v4 - name: Set up Python uses: actions/setup-python@v5 with: python-version: "3.12" - name: Download VFS artifact uses: actions/download-artifact@v4 with: name: vfs-${{ matrix.os }}-${{ matrix.arch }} path: packages/python/litestream_vfs/ - name: Rename binary run: | mv packages/python/litestream_vfs/${{ matrix.src_name }} \ packages/python/litestream_vfs/${{ matrix.dest_name }} - name: Get version id: version run: | if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then VERSION="${{ inputs.tag }}" else VERSION="${GITHUB_REF#refs/tags/}" fi echo "version=${VERSION#v}" >> $GITHUB_OUTPUT - name: Build wheel run: | cd packages/python pip install setuptools wheel LITESTREAM_VERSION=${{ steps.version.outputs.version }} python setup.py bdist_wheel - name: Rename wheel with platform tag run: | cd packages/python python scripts/rename_wheel.py dist ${{ matrix.platform_tag }} - name: Publish to PyPI uses: pypa/gh-action-pypi-publish@release/v1 with: packages-dir: packages/python/dist/ publish-npm: name: Publish to npm runs-on: ubuntu-latest needs: [vfs-build-linux, vfs-build-darwin] steps: - name: Checkout uses: actions/checkout@v4 - name: Set up Node.js uses: actions/setup-node@v4 with: node-version: "20" registry-url: "https://registry.npmjs.org" - name: Download all VFS artifacts uses: actions/download-artifact@v4 with: path: artifacts/ - name: Copy binaries to platform packages run: | cp artifacts/vfs-linux-amd64/litestream-vfs-linux-amd64.so \ packages/npm/litestream-vfs-linux-amd64/litestream-vfs.so cp artifacts/vfs-linux-arm64/litestream-vfs-linux-arm64.so \ packages/npm/litestream-vfs-linux-arm64/litestream-vfs.so cp artifacts/vfs-darwin-amd64/litestream-vfs-darwin-amd64.dylib \ packages/npm/litestream-vfs-darwin-amd64/litestream-vfs.dylib cp artifacts/vfs-darwin-arm64/litestream-vfs-darwin-arm64.dylib \ packages/npm/litestream-vfs-darwin-arm64/litestream-vfs.dylib - name: Get version id: version run: | if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then VERSION="${{ inputs.tag }}" else VERSION="${GITHUB_REF#refs/tags/}" fi echo "version=${VERSION#v}" >> $GITHUB_OUTPUT - name: Set versions run: | VERSION=${{ steps.version.outputs.version }} for dir in packages/npm/litestream-vfs-*/; do jq --arg v "$VERSION" '.version = $v' "$dir/package.json" > tmp.json && mv tmp.json "$dir/package.json" done jq --arg v "$VERSION" ' .version = $v | .optionalDependencies = (.optionalDependencies | to_entries | map(.value = $v) | from_entries) ' packages/npm/litestream-vfs/package.json > tmp.json && mv tmp.json packages/npm/litestream-vfs/package.json - name: Publish platform packages env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} run: | for dir in packages/npm/litestream-vfs-*/; do cd "$dir" npm publish --access public cd - done - name: Wait for registry propagation run: sleep 30 - name: Publish main package env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} run: | cd packages/npm/litestream-vfs npm publish --access public publish-gem: name: Publish to RubyGems (${{ matrix.os }}-${{ matrix.arch }}) runs-on: ubuntu-latest needs: [vfs-build-linux, vfs-build-darwin] strategy: matrix: include: - os: linux arch: amd64 ext: so src_name: litestream-vfs-linux-amd64.so dest_name: litestream-vfs.so platform: x86_64-linux - os: linux arch: arm64 ext: so src_name: litestream-vfs-linux-arm64.so dest_name: litestream-vfs.so platform: aarch64-linux - os: darwin arch: amd64 ext: dylib src_name: litestream-vfs-darwin-amd64.dylib dest_name: litestream-vfs.dylib platform: x86_64-darwin - os: darwin arch: arm64 ext: dylib src_name: litestream-vfs-darwin-arm64.dylib dest_name: litestream-vfs.dylib platform: arm64-darwin steps: - name: Checkout uses: actions/checkout@v4 - name: Set up Ruby uses: ruby/setup-ruby@v1 with: ruby-version: "3.3" - name: Download VFS artifact uses: actions/download-artifact@v4 with: name: vfs-${{ matrix.os }}-${{ matrix.arch }} path: packages/ruby/lib/ - name: Rename binary run: | mv packages/ruby/lib/${{ matrix.src_name }} \ packages/ruby/lib/${{ matrix.dest_name }} - name: Get version id: version run: | if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then VERSION="${{ inputs.tag }}" else VERSION="${GITHUB_REF#refs/tags/}" fi echo "version=${VERSION#v}" >> $GITHUB_OUTPUT - name: Build gem run: | cd packages/ruby LITESTREAM_VERSION=${{ steps.version.outputs.version }} \ PLATFORM=${{ matrix.platform }} \ gem build litestream-vfs.gemspec - name: Publish to RubyGems env: GEM_HOST_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }} run: | cd packages/ruby gem push litestream-vfs-*.gem ================================================ FILE: .github/workflows/stale-issues.yml ================================================ name: Stale Issue Manager on: schedule: - cron: '0 0 * * *' workflow_dispatch: permissions: issues: write jobs: stale: runs-on: ubuntu-latest steps: - uses: actions/stale@v9 with: repo-token: ${{ secrets.GITHUB_TOKEN }} # Issues: Label after 90 days, close after 30 more days days-before-stale: 90 days-before-close: 30 stale-issue-label: 'stale' # Exempt these labels from stale marking exempt-issue-labels: 'Soon,release-blocker' # Message posted when issue becomes stale stale-issue-message: > This issue has been inactive for 90 days and will be automatically closed in 30 days if there is no further activity. If this issue is still relevant, please add a comment to keep it open. Thank you for your contribution! # Message posted when issue is closed close-issue-message: > This issue has been automatically closed due to inactivity. If you believe this issue is still relevant, please reopen it or create a new issue with updated information. Thank you! # Disable pull request processing days-before-pr-stale: -1 days-before-pr-close: -1 # Limit operations per run operations-per-run: 100 # Remove stale label when there is activity remove-stale-when-updated: true ================================================ FILE: .github/workflows/upgrade-tests.yml ================================================ name: Upgrade Tests on: pull_request: paths: - '**.go' - 'go.mod' - 'go.sum' - 'tests/integration/**' - '.github/workflows/upgrade-tests.yml' workflow_dispatch: permissions: contents: read jobs: upgrade-test: name: v0.3.x to v0.5.x Upgrade Test runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: "go.mod" - name: Download Litestream v0.3.13 run: | mkdir -p /tmp/litestream-v3 gh release download v0.3.13 --repo benbjohnson/litestream --pattern 'litestream-v0.3.13-linux-amd64.tar.gz' --dir /tmp tar -xzf /tmp/litestream-v0.3.13-linux-amd64.tar.gz -C /tmp/litestream-v3 chmod +x /tmp/litestream-v3/litestream /tmp/litestream-v3/litestream version env: GH_TOKEN: ${{ github.token }} - name: Build current binaries run: go build -o bin/litestream ./cmd/litestream && go build -o bin/litestream-test ./cmd/litestream-test - name: Run upgrade integration tests run: go test -v -tags=integration -timeout=10m ./tests/integration/... -run=TestUpgrade env: CGO_ENABLED: "1" LITESTREAM_V3_BIN: /tmp/litestream-v3/litestream - name: Upload test logs if: failure() uses: actions/upload-artifact@v4 with: name: upgrade-test-logs path: /tmp/litestream-*/*.log ================================================ FILE: .gitignore ================================================ .DS_Store /src/litestream-vfs.h /dist .vscode .sprite # Claude-related files (force include despite global gitignore) !.claude/ !.claude/** # But ignore logs, hooks, and local settings .claude/logs/ .claude/hooks/ .claude/settings.local.json # Keep CLAUDE.md ignored as it's auto-loaded by Claude Code CLAUDE.md # Binary bin/ # Cached binaries for integration tests .cache/ .claude/*.loop.local.md .claude/worktrees/ CLAUDE.local.md # Package manager build artifacts packages/python/dist/ packages/python/build/ packages/python/*.egg-info/ ================================================ FILE: .goreleaser.yml ================================================ version: 2 project_name: litestream before: hooks: - go mod tidy builds: - id: litestream main: ./cmd/litestream binary: litestream env: - CGO_ENABLED=0 goos: - linux - darwin - windows goarch: - amd64 - arm64 - arm goarm: - "6" - "7" ldflags: - -s -w -X main.Version={{.Version}} ignore: - goos: windows goarch: arm - goos: darwin goarch: arm archives: - id: main formats: - tar.gz format_overrides: - goos: windows formats: - zip name_template: >- {{ .ProjectName }}- {{- .Version }}- {{- .Os }}- {{- if eq .Arch "amd64" }}x86_64 {{- else if eq .Arch "386" }}i386 {{- else }}{{ .Arch }}{{ end }} {{- if .Arm }}v{{ .Arm }}{{ end }} files: - etc/litestream.yml - etc/litestream.service - README.md - LICENSE nfpms: - vendor: Litestream homepage: https://litestream.io maintainer: Litestream Contributors description: Streaming replication for SQLite databases license: Apache 2.0 formats: - deb - rpm contents: - src: etc/litestream.yml dst: /etc/litestream.yml type: config - src: etc/litestream.service dst: /lib/systemd/system/litestream.service type: config bindir: /usr/bin file_name_template: >- {{ .ProjectName }}- {{- .Version }}- {{- .Os }}- {{- if eq .Arch "amd64" }}x86_64 {{- else if eq .Arch "386" }}i386 {{- else }}{{ .Arch }}{{ end }} {{- if .Arm }}v{{ .Arm }}{{ end }} brews: - name: litestream homepage: https://litestream.io description: Streaming replication for SQLite databases license: Apache-2.0 repository: owner: benbjohnson name: homebrew-litestream branch: main token: "{{ .Env.HOMEBREW_TAP_GITHUB_TOKEN }}" install: | bin.install "litestream" etc.install "etc/litestream.yml" => "litestream.yml" test: | system "#{bin}/litestream", "version" commit_author: name: goreleaser email: bot@goreleaser.com checksum: name_template: 'checksums.txt' algorithm: sha256 snapshot: version_template: "{{ .Tag }}-next" changelog: sort: asc filters: exclude: - '^docs:' - '^test:' - '^chore:' - 'Merge pull request' - 'Merge branch' release: github: owner: benbjohnson name: litestream draft: false prerelease: auto mode: replace header: | ## Platform Support ⚠️ **Windows Notice**: Windows binaries are provided for convenience but Windows is NOT an officially supported platform. Use at your own risk. Community contributions for Windows improvements are welcome. ✅ **Supported Platforms**: Linux (amd64, arm64, armv6, armv7), macOS (amd64, arm64) ## Installation ### Homebrew (macOS and Linux) ```bash brew tap benbjohnson/litestream brew install litestream ``` ### Debian/Ubuntu Download the `.deb` file for your architecture and install: ```bash sudo dpkg -i litestream-*.deb ``` ### RPM-based systems Download the `.rpm` file for your architecture and install: ```bash sudo rpm -i litestream-*.rpm ``` ### Binary installation Download the appropriate archive for your platform, extract, and move to your PATH. ## VFS Extension (Experimental) SQLite loadable extensions for read-only access to Litestream replicas are available for supported platforms: | Platform | File | |----------|------| | Linux x86_64 | `litestream-vfs-v{{.Version}}-linux-amd64.tar.gz` | | Linux ARM64 | `litestream-vfs-v{{.Version}}-linux-arm64.tar.gz` | | macOS Intel | `litestream-vfs-v{{.Version}}-darwin-amd64.tar.gz` | | macOS Apple Silicon | `litestream-vfs-v{{.Version}}-darwin-arm64.tar.gz` | Install via package managers: ```bash pip install litestream-vfs # Python npm install litestream-vfs # Node.js gem install litestream-vfs # Ruby ``` # Signing configuration # signs: # - id: macos # cmd: gon # args: # - "{{ .ProjectPath }}/gon-sign.hcl" # artifacts: archive # ids: # - main # signature: "${artifact}.zip" # output: true # env: # - APPLE_DEVELOPER_ID_APPLICATION={{ .Env.APPLE_DEVELOPER_ID }} # - APPLE_DEVELOPER_TEAM_ID={{ .Env.APPLE_TEAM_ID }} # - AC_PASSWORD={{ .Env.AC_PASSWORD }} sboms: - artifacts: archive ================================================ FILE: .markdownlint.json ================================================ { "default": true, "MD013": false, "MD024": false, "MD026": false, "MD031": false, "MD032": false, "MD033": { "allowed_elements": ["br", "kbd", "sub", "sup"] }, "MD041": false } ================================================ FILE: .pre-commit-config.yaml ================================================ repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.1.0 hooks: - id: trailing-whitespace exclude_types: [markdown] - id: end-of-file-fixer - id: check-yaml - id: check-added-large-files - repo: https://github.com/tekwizely/pre-commit-golang rev: v1.0.0-beta.5 hooks: - id: go-imports-repo args: - "-local" - "github.com/benbjohnson/litestream" - "-w" - id: go-vet-repo-mod - id: go-staticcheck-repo-mod ================================================ FILE: AGENTS.md ================================================ # AGENTS.md - Litestream AI Agent Guide Litestream is a disaster recovery tool for SQLite that runs as a background process, monitors the WAL, converts changes to immutable LTX files, and replicates them to cloud storage. It uses `modernc.org/sqlite` (pure Go, no CGO required). ## Before You Start 1. Read [AI_PR_GUIDE.md](AI_PR_GUIDE.md) for contribution requirements 2. Check [CONTRIBUTING.md](CONTRIBUTING.md) for what we accept (bug fixes welcome, features need discussion) 3. Review recent PRs for current patterns ## Critical Rules - **Lock page at 1GB**: SQLite reserves page at 0x40000000. Always skip it. See [docs/SQLITE_INTERNALS.md](docs/SQLITE_INTERNALS.md) - **LTX files are immutable**: Never modify after creation. See [docs/LTX_FORMAT.md](docs/LTX_FORMAT.md) - **Single replica per database**: Each DB replicates to exactly one destination - **Use `litestream ltx`**: Not `litestream wal` (deprecated) - **Use `litestream reset`**: Clears corrupted local LTX state for a database. See `cmd/litestream/reset.go` - **`auto-recover` config**: Replica option that automatically resets local state on LTX errors. Disabled by default. See `replica.go` - **Retention enabled by default**: `Store.RetentionEnabled` is `true` by default. Disable only when cloud lifecycle policies handle cleanup. See `store.go` - **IPC socket disabled by default**: Control socket is off by default. Enable with `socket.enabled: true` in config. See `server.go` - **`$PID` config expansion**: Config files support `$PID` to expand to the current process ID, plus standard `$ENV_VAR` expansion. See `cmd/litestream/main.go` - **`litestream ltx -level`**: Use `-level 0`–`9` or `-level all` to inspect specific compaction levels. See `cmd/litestream/ltx.go` - **Return errors, don't log them**: Always return errors to callers. Never `log.Printf(err)` and continue — this silently hides failures in a disaster recovery tool. Only use DEBUG log for best-effort operations where failure doesn't affect correctness and a valid fallback exists (e.g., reading SHM mxFrame optimization hint). See [docs/PATTERNS.md](docs/PATTERNS.md#error-handling) ## Layer Boundaries | Layer | File | Responsibility | |-------|------|----------------| | DB | `db.go` | Database state, restoration, WAL monitoring, library API (`SyncStatus`, `SyncAndWait`, `EnsureExists`) | | Replica | `replica.go` | Replication mechanics only | | Storage | `**/replica_client.go` | Backend implementations (includes `ReplicaClientV3` for v0.3.x restore) | | IPC | `server.go` | Unix socket control API (register/unregister, /txid, pprof) | | Leasing | `leaser.go`, `s3/leaser.go` | Distributed lease acquisition via conditional writes | Database state logic belongs in DB layer, not Replica layer. ## Quick Reference **Build:** ```bash go build -o bin/litestream ./cmd/litestream go test -race -v ./... ``` **Code quality:** ```bash pre-commit run --all-files ``` ## Documentation | Document | When to Read | |----------|--------------| | [docs/PATTERNS.md](docs/PATTERNS.md) | Code patterns and anti-patterns | | [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) | Deep component details | | [docs/SQLITE_INTERNALS.md](docs/SQLITE_INTERNALS.md) | WAL format, lock page | | [docs/LTX_FORMAT.md](docs/LTX_FORMAT.md) | Replication format | | [docs/TESTING_GUIDE.md](docs/TESTING_GUIDE.md) | Test strategies | | [docs/REPLICA_CLIENT_GUIDE.md](docs/REPLICA_CLIENT_GUIDE.md) | Adding storage backends | | [docs/PROVIDER_COMPATIBILITY.md](docs/PROVIDER_COMPATIBILITY.md) | Provider-specific S3/cloud configs | ## Checklist Before submitting changes: - [ ] Read relevant docs above - [ ] Follow patterns in [docs/PATTERNS.md](docs/PATTERNS.md) - [ ] Test with race detector (`go test -race`) - [ ] Run `pre-commit run --all-files` - [ ] For page iteration: test with >1GB databases - [ ] Show investigation evidence in PR (see [AI_PR_GUIDE.md](AI_PR_GUIDE.md)) ================================================ FILE: AI_PR_GUIDE.md ================================================ # AI-Assisted Contribution Guide This guide helps AI assistants (and humans using them) submit high-quality PRs to Litestream. ## TL;DR Checklist Before submitting a PR: - [ ] **Show your investigation** - Include logs, file patterns, or debug output proving the problem - [ ] **Define scope clearly** - State what this PR does AND does not do - [ ] **Include runnable test commands** - Not just descriptions, actual `go test` commands - [ ] **Reference related issues/PRs** - Show awareness of related work - [ ] **Error handling**: Does the code return errors to callers? Watch for `log.Printf(err)` followed by `continue` or `return nil` — this silently swallows failures. ## What Makes PRs Succeed Analysis of recent PRs shows successful submissions share these patterns: ### 1. Investigation Artifacts Show evidence, don't just describe the fix. **Good:** ```markdown ## Problem File patterns show excessive snapshot creation after checkpoint: - 21:43 5.2G snapshot.ltx - 21:47 5.2G snapshot.ltx (after checkpoint - should not trigger new snapshot) Debug logs show `verify()` incorrectly detecting position mismatch... ``` **Bad:** ```markdown ## Problem Snapshots are created too often. This PR fixes it. ``` ### 2. Clear Scope Definition Explicitly state boundaries. **Good:** ```markdown ## Scope This PR adds the lease client interface only. **In scope:** - LeaseClient interface definition - Mock implementation for testing **Not in scope (future PRs):** - Integration with Store - Distributed coordination logic ``` **Bad:** ```markdown ## Changes Added leasing support and also fixed a checkpoint bug I noticed. ``` ### 3. Runnable Test Commands **Good:** Include actual commands that can be run: ```bash # Unit tests go test -race -v -run TestDB_CheckpointDoesNotTriggerSnapshot ./... # Integration test with file backend go test -v ./replica_client_test.go -integration file ``` **Bad:** Vague descriptions like "Manual testing with file backend" or "Verified it works" ### 4. Before/After Comparison For behavior changes, show the difference: **Good:** ```markdown ## Behavior Change | Scenario | Before | After | |----------|--------|-------| | Checkpoint with no changes | Creates snapshot | No snapshot | | Checkpoint with changes | Creates snapshot | Creates snapshot | ``` ## Common Mistakes ### Scope Creep **Problem:** Mixing unrelated changes in one PR. **Example:** PR titled "Add lease client" also includes a fix for checkpoint timing. **Fix:** Split into separate PRs. Reference them: "This PR adds the lease client. The checkpoint fix is in #XXX." ### Missing Root Cause Analysis **Problem:** Implementing a fix without proving the problem exists. **Example:** "Add exponential backoff" without showing what's filling disk. **Fix:** Include investigation showing the actual cause before proposing solution. ### Vague Test Plans **Problem:** "Tested manually" or "Verified it works." **Fix:** Include exact commands: ```bash go test -race -v -run TestSpecificFunction ./... ``` ### No Integration Context **Problem:** Large features without explaining how they fit. **Fix:** For multi-PR work, explain the phases: ```markdown This is Phase 1 of 3 for distributed leasing: 1. **This PR**: Lease client interface 2. Future: Store integration 3. Future: Distributed coordination ``` ## PR Description Template Use this structure for PR descriptions: ```text ## Summary [1-2 sentences: what this PR does] ## Problem [Evidence of the problem - logs, file patterns, user reports] ## Solution [Brief explanation of the approach] ## Scope **In scope:** - [item] **Not in scope:** - [item] ## Test Plan [Include actual go test commands here] ## Related - Fixes #XXX - Related to #YYY ``` ## What We Accept From [CONTRIBUTING.md](CONTRIBUTING.md): - **Bug fixes** - Welcome, especially with evidence - **Small improvements** - Performance, code cleanup - **Documentation** - Always welcome - **Features** - Discuss in issue first; large features typically implemented internally ## Resources - [AGENTS.md](AGENTS.md) - Project overview and checklist - [docs/PATTERNS.md](docs/PATTERNS.md) - Code patterns - [CONTRIBUTING.md](CONTRIBUTING.md) - Contribution guidelines ================================================ FILE: CONTRIBUTING.md ================================================ # Contributing to Litestream Thank you for your interest in contributing to Litestream! We value community contributions and appreciate your help in making Litestream better. ## Types of Contributions We Accept ### ✅ We Encourage and Accept - **Bug fixes and patches**: If you've found a bug and have a fix, we welcome your contribution - **Security vulnerability reports**: Please report security issues responsibly (see Security section below) - **Documentation improvements**: Help make our docs clearer and more comprehensive - **Testing and feedback**: Report issues, test new features, and provide feedback - **Small code improvements**: Performance optimizations, code cleanup, and minor enhancements ### ⚠️ Discuss First - **Feature requests**: Please open an issue to discuss new features before implementing them - **Large changes**: For significant modifications, please discuss your approach in an issue first ### ❌ Generally Not Accepted - **Large external feature contributions**: Features carry a long-term maintenance burden. To reduce burnout and maintain code quality, we typically implement major features internally. This allows us to ensure consistency with the overall architecture and maintain the high reliability that Litestream users depend on for disaster recovery - **Breaking changes**: Changes that break backward compatibility require extensive discussion ## AI-Assisted Contributions We welcome AI-assisted contributions for bug fixes and small improvements. Whether you're using Claude, Copilot, Cursor, or other AI tools: **Requirements:** - **Show your investigation** - Include evidence (logs, file patterns, debug output) proving the problem exists - **Define scope clearly** - State what the PR does and does not do - **Include runnable test commands** - Actual `go test` commands, not just descriptions - **Human review before submission** - You're responsible for the code you submit **Resources:** - [AI_PR_GUIDE.md](AI_PR_GUIDE.md) - Detailed guide with templates and examples - [AGENTS.md](AGENTS.md) - Project overview for AI assistants ## How to Contribute ### Reporting Bugs Before reporting a bug: 1. Check the [existing issues](https://github.com/benbjohnson/litestream/issues) to avoid duplicates 2. Verify you're using the latest version of Litestream 3. Gather diagnostic information (OS, version, configuration, error messages) When reporting a bug, please use our issue template and include: - Your operating system and version - Litestream version (`litestream version`) - Relevant configuration (sanitized of sensitive data) - Steps to reproduce the issue - Expected vs actual behavior - Any error messages or logs ### Submitting Pull Requests 1. **Fork the repository** and create a new branch from `main` 2. **Make your changes** following our code style (see Development section) 3. **Add or update tests** as appropriate 4. **Update documentation** if you're changing behavior 5. **Run tests and linters** locally: ```bash go test -v ./... go vet ./... go fmt ./... goimports -local github.com/benbjohnson/litestream -w . pre-commit run --all-files ``` 6. **Submit a pull request** with a clear description of your changes ### Pull Request Guidelines Your PR should: - Have a clear, descriptive title - Reference any related issues (e.g., "Fixes #123") - Include tests for bug fixes and new features - Pass all CI checks - Have a focused scope (one bug fix or feature per PR) ## Development Setup ### Prerequisites - Go 1.24 or later - CGO enabled (for SQLite integration) - Git - Pre-commit (optional but recommended): `pip install pre-commit` ### Building from Source ```bash # Clone the repository git clone https://github.com/benbjohnson/litestream.git cd litestream # Build the binary go build ./cmd/litestream # Run tests go test -v ./... # Install pre-commit hooks (recommended) pre-commit install ``` ### Code Style - Follow standard Go conventions - Use `gofmt` and `goimports` for formatting - Run `go vet` and `staticcheck` for static analysis - Keep functions focused and well-documented - Add comments for exported types and functions ### Testing - Write unit tests for new functionality - Ensure existing tests pass before submitting PRs - Integration tests require specific environment setup (see test files for details) ## Security If you discover a security vulnerability, please: 1. **DO NOT** open a public issue 2. Email the maintainers directly with details 3. Allow time for the issue to be addressed before public disclosure ## Code of Conduct We expect all contributors to: - Be respectful and inclusive - Welcome newcomers and help them get started - Focus on constructive criticism - Respect differing viewpoints and experiences ## Getting Help - **Documentation**: [litestream.io](https://litestream.io) - **Issues**: [GitHub Issues](https://github.com/benbjohnson/litestream/issues) ## License By contributing to Litestream, you agree that your contributions will be licensed under the Apache License 2.0, the same as the project. ## Acknowledgments Thank you to all our contributors! Your efforts help make Litestream a reliable disaster recovery tool for the SQLite community. ================================================ FILE: Dockerfile ================================================ FROM golang:1.25 AS builder # Install build dependencies for VFS extension RUN apt-get update && apt-get install -y gcc libc6-dev && rm -rf /var/lib/apt/lists/* WORKDIR /src/litestream COPY . . ARG LITESTREAM_VERSION=latest # Build litestream binary RUN --mount=type=cache,target=/root/.cache/go-build \ --mount=type=cache,target=/go/pkg \ go build -ldflags "-s -w -X 'main.Version=${LITESTREAM_VERSION}' -extldflags '-static'" -tags osusergo,netgo,sqlite_omit_load_extension -o /usr/local/bin/litestream ./cmd/litestream # Build VFS loadable extension RUN --mount=type=cache,target=/root/.cache/go-build \ --mount=type=cache,target=/go/pkg \ mkdir -p dist && \ CGO_ENABLED=1 go build \ -tags "vfs,SQLITE3VFS_LOADABLE_EXT" \ -buildmode=c-archive \ -o dist/litestream-vfs.a ./cmd/litestream-vfs && \ mv dist/litestream-vfs.h src/litestream-vfs.h && \ gcc -DSQLITE3VFS_LOADABLE_EXT -g -fPIC -shared \ -o dist/litestream-vfs.so \ src/litestream-vfs.c \ dist/litestream-vfs.a \ -lpthread -ldl -lm # --- Hardened image (Scratch) --- FROM alpine:3.21 AS certs RUN apk --update add ca-certificates && \ echo "nonroot:x:65532:65532:nonroot:/home/nonroot:/sbin/nologin" > /etc/minimal-passwd && \ echo "nonroot:x:65532:" > /etc/minimal-group FROM scratch AS hardened COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt COPY --from=certs /etc/minimal-passwd /etc/passwd COPY --from=certs /etc/minimal-group /etc/group COPY --from=builder /usr/local/bin/litestream /usr/local/bin/litestream USER nonroot:nonroot ENTRYPOINT ["/usr/local/bin/litestream"] CMD [] # --- Default image (Debian) --- FROM debian:bookworm-slim AS default RUN apt-get update && \ apt-get install -y ca-certificates sqlite3 && \ rm -rf /var/lib/apt/lists/* COPY --from=builder /usr/local/bin/litestream /usr/local/bin/litestream COPY --from=builder /src/litestream/dist/litestream-vfs.so /usr/local/lib/litestream-vfs.so ENTRYPOINT ["/usr/local/bin/litestream"] CMD [] ================================================ FILE: GEMINI.md ================================================ # GEMINI.md - Gemini Code Assist Configuration Gemini-specific configuration for Litestream. See [AGENTS.md](AGENTS.md) for project documentation. ## Before Contributing 1. Read [AI_PR_GUIDE.md](AI_PR_GUIDE.md) - PR quality requirements 2. Read [AGENTS.md](AGENTS.md) - Project overview and checklist 3. Check [CONTRIBUTING.md](CONTRIBUTING.md) - What we accept ## File Exclusions Check `.aiexclude` for patterns of files that should not be shared with Gemini. ## Gemini Strengths for This Project - **Test generation** - Creating comprehensive test suites - **Documentation** - Generating and updating docs - **Code review** - Identifying issues and security concerns - **Local codebase awareness** - Enable for full repository understanding ## Documentation Load as needed: - [docs/PATTERNS.md](docs/PATTERNS.md) - Code patterns when writing code - [docs/SQLITE_INTERNALS.md](docs/SQLITE_INTERNALS.md) - For WAL/page work - [docs/TESTING_GUIDE.md](docs/TESTING_GUIDE.md) - For test generation ## Critical Rules - **Lock page at 1GB** - Skip page at 0x40000000 - **LTX files are immutable** - Never modify after creation - **Layer boundaries** - DB handles state, Replica handles replication ## Quick Commands ```bash go build -o bin/litestream ./cmd/litestream go test -race -v ./... pre-commit run --all-files ``` ================================================ FILE: LICENSE ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================ FILE: Makefile ================================================ default: docker: docker build -t litestream . # VFS build configuration VFS_BUILD_TAGS := vfs,SQLITE3VFS_LOADABLE_EXT VFS_SRC := ./cmd/litestream-vfs VFS_C_SRC := src/litestream-vfs.c MACOSX_MIN_VERSION := 11.0 DARWIN_LDFLAGS := -framework CoreFoundation -framework Security -lresolv -mmacosx-version-min=$(MACOSX_MIN_VERSION) LINUX_LDFLAGS := -lpthread -ldl -lm .PHONY: vfs vfs: mkdir -p dist go build -tags vfs,SQLITE3VFS_LOADABLE_EXT -o dist/litestream-vfs.a -buildmode=c-archive ./cmd/litestream-vfs mv dist/litestream-vfs.h src/litestream-vfs.h gcc -DSQLITE3VFS_LOADABLE_EXT -framework CoreFoundation -framework Security -lresolv -g -fPIC -shared -o dist/litestream-vfs.so src/litestream-vfs.c dist/litestream-vfs.a .PHONY: vfs-linux-amd64 vfs-linux-amd64: mkdir -p dist CGO_ENABLED=1 GOOS=linux GOARCH=amd64 \ go build -tags $(VFS_BUILD_TAGS) -o dist/litestream-vfs-linux-amd64.a -buildmode=c-archive $(VFS_SRC) cp dist/litestream-vfs-linux-amd64.h src/litestream-vfs.h gcc -DSQLITE3VFS_LOADABLE_EXT -g -fPIC -shared -o dist/litestream-vfs-linux-amd64.so \ $(VFS_C_SRC) dist/litestream-vfs-linux-amd64.a $(LINUX_LDFLAGS) .PHONY: vfs-linux-arm64 vfs-linux-arm64: mkdir -p dist CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc \ go build -tags $(VFS_BUILD_TAGS) -o dist/litestream-vfs-linux-arm64.a -buildmode=c-archive $(VFS_SRC) cp dist/litestream-vfs-linux-arm64.h src/litestream-vfs.h aarch64-linux-gnu-gcc -DSQLITE3VFS_LOADABLE_EXT -g -fPIC -shared -o dist/litestream-vfs-linux-arm64.so \ $(VFS_C_SRC) dist/litestream-vfs-linux-arm64.a $(LINUX_LDFLAGS) .PHONY: vfs-darwin-amd64 vfs-darwin-amd64: mkdir -p dist CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 \ go build -tags $(VFS_BUILD_TAGS) -o dist/litestream-vfs-darwin-amd64.a -buildmode=c-archive $(VFS_SRC) cp dist/litestream-vfs-darwin-amd64.h src/litestream-vfs.h clang -DSQLITE3VFS_LOADABLE_EXT -arch x86_64 -g -fPIC -shared -o dist/litestream-vfs-darwin-amd64.dylib \ $(VFS_C_SRC) dist/litestream-vfs-darwin-amd64.a $(DARWIN_LDFLAGS) .PHONY: vfs-darwin-arm64 vfs-darwin-arm64: mkdir -p dist CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 \ go build -tags $(VFS_BUILD_TAGS) -o dist/litestream-vfs-darwin-arm64.a -buildmode=c-archive $(VFS_SRC) cp dist/litestream-vfs-darwin-arm64.h src/litestream-vfs.h clang -DSQLITE3VFS_LOADABLE_EXT -arch arm64 -g -fPIC -shared -o dist/litestream-vfs-darwin-arm64.dylib \ $(VFS_C_SRC) dist/litestream-vfs-darwin-arm64.a $(DARWIN_LDFLAGS) vfs-test: go test -v -tags=vfs ./cmd/litestream-vfs .PHONY: clean clean: rm -rf dist mcp-wrap: fly mcp wrap --mcp="./dist/litestream" --bearer-token=$(FLY_MCP_BEARER_TOKEN) --debug -- mcp --debug mcp-inspect: fly mcp proxy -i --url http://localhost:8080/ --bearer-token=$(FLY_MCP_BEARER_TOKEN) .PHONY: default clean mcp-wrap mcp-inspect ================================================ FILE: README.md ================================================ Litestream ![GitHub release (latest by date)](https://img.shields.io/github/v/release/benbjohnson/litestream) ![Status](https://img.shields.io/badge/status-beta-blue) ![GitHub](https://img.shields.io/github/license/benbjohnson/litestream) [![Docker Pulls](https://img.shields.io/docker/pulls/litestream/litestream.svg?maxAge=604800)](https://hub.docker.com/r/litestream/litestream/) ========== Litestream is a standalone disaster recovery tool for SQLite. It runs as a background process and safely replicates changes incrementally to another file or S3. Litestream only communicates with SQLite through the SQLite API so it will not corrupt your database. If you need support or have ideas for improving Litestream, please visit [GitHub Issues](https://github.com/benbjohnson/litestream/issues). Please visit the [Litestream web site](https://litestream.io) for installation instructions and documentation. If you find this project interesting, please consider starring the project on GitHub. Contributing ------------ We welcome bug reports, fixes, and patches! Please see our [Contributing Guide](CONTRIBUTING.md) for details on how to contribute. Acknowledgements ---------------- I want to give special thanks to individuals who invest much of their time and energy into the project to help make it better: - Thanks to [Cory LaNou](https://twitter.com/corylanou) for giving early feedback and testing when Litestream was still pre-release. - Thanks to [Michael Lynch](https://github.com/mtlynch) for digging into issues and contributing to the documentation. - Thanks to [Kurt Mackey](https://twitter.com/mrkurt) for feedback and testing. - Thanks to [Sam Weston](https://twitter.com/cablespaghetti) for figuring out how to run Litestream on Kubernetes and writing up the docs for it. - Thanks to [Rafael](https://github.com/netstx) & [Jungle Boogie](https://github.com/jungle-boogie) for helping to get OpenBSD release builds working. - Thanks to [Simon Gottschlag](https://github.com/simongottschlag), [Marin](https://github.com/supermarin),[Victor Björklund](https://github.com/victorbjorklund), [Jonathan Beri](https://twitter.com/beriberikix) [Yuri](https://github.com/yurivish), [Nathan Probst](https://github.com/nprbst), [Yann Coleu](https://github.com/yanc0), and [Nicholas Grilly](https://twitter.com/ngrilly) for frequent feedback, testing, & support. Huge thanks to fly.io for their support and for contributing credits for testing and development! ================================================ FILE: SECURITY.md ================================================ # Security Policy ## Reporting Security Vulnerabilities We take security issues seriously. If you discover a security vulnerability in Litestream, please report it responsibly. **Please DO NOT open a public GitHub issue for security vulnerabilities.** Instead, please report security issues via email to the maintainers. This allows us to assess the issue and release a fix before the vulnerability is publicly disclosed. ## What to Include When reporting a security issue, please include: - Description of the vulnerability - Steps to reproduce the issue - Potential impact - Any suggested fixes (if you have them) ## Response Timeline We will make our best effort to acknowledge receipt of your report as soon as possible and keep you informed of our progress. Please understand that as an open source project, response times may vary. Thank you for helping keep Litestream and its users safe! ================================================ FILE: _examples/library/README.md ================================================ # Litestream Library Usage Examples These examples demonstrate how to use Litestream as a Go library instead of as a standalone CLI tool. ## API Stability Warning The Litestream library API is not considered stable and may change between versions. The CLI interface is more stable for production use. Use the library API at your own risk, and pin to specific versions. **Note (POSIX platforms):** All POSIX platforms (Linux, macOS, BSD, etc.) use per-process locks for SQLite, not per-handle locks. If you open the same database with two different SQLite driver implementations in the same process and close one of them, you can hit locking issues. You **must** use `modernc.org/sqlite` for your app since Litestream uses it internally. ## Important Constraints When using Litestream as a library, be aware of these critical requirements: 1. **Required Driver**: You must use `modernc.org/sqlite`. Litestream uses this driver internally, and mixing drivers causes lock conflicts on POSIX systems. 2. **Lifecycle Management**: You cannot call `litestream.DB.Close()` or `Replica.Stop(true)` while your application still has open database connections. Either close all your app's database connections first, or only close Litestream when your process is shutting down. 3. **PRAGMA Configuration**: Use DSN parameters (e.g., `?_pragma=busy_timeout(5000)`) instead of `PRAGMA` statements via `ExecContext`. An `sql.DB` is a connection pool, and `ExecContext` only applies the PRAGMA to one random connection from the pool. ## Examples ### Basic (File Backend) The simplest example using local filesystem replication. ```bash cd basic go run main.go ``` This creates: - `myapp.db` - The SQLite database - `replica/` - Directory containing replicated LTX files ### S3 Backend A more complete example showing the restore-on-startup pattern with S3. ```bash cd s3 # Set required environment variables export AWS_ACCESS_KEY_ID="your-access-key" export AWS_SECRET_ACCESS_KEY="your-secret-key" export LITESTREAM_BUCKET="your-bucket-name" export LITESTREAM_PATH="databases/myapp" # optional, defaults to "litestream" export AWS_REGION="us-east-1" # optional, defaults to "us-east-1" go run main.go ``` This example: 1. Checks if the local database exists 2. If not, attempts to restore from S3 3. Starts background replication to S3 4. Inserts sample data every 2 seconds 5. Gracefully shuts down on Ctrl+C ## Core API Pattern ```go import ( "context" "database/sql" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/file" // or s3, gs, abs, etc. _ "modernc.org/sqlite" ) // 1. Create database wrapper db := litestream.NewDB("/path/to/db.sqlite") // 2. Create replica client client := file.NewReplicaClient("/path/to/replica") // OR from URL: // client, _ := litestream.NewReplicaClientFromURL("s3://bucket/path") // 3. Attach replica to database replica := litestream.NewReplicaWithClient(db, client) db.Replica = replica client.Replica = replica // file backend only; preserves ownership/permissions // 4. Create compaction levels (L0 required, plus at least one more) levels := litestream.CompactionLevels{ {Level: 0}, {Level: 1, Interval: 10 * time.Second}, } // 5. Create Store to manage DB and background compaction store := litestream.NewStore([]*litestream.DB{db}, levels) // 6. Open Store (opens all DBs, starts background monitors) if err := store.Open(ctx); err != nil { ... } defer store.Close(context.Background()) // 7. Open your app's SQLite connection for normal database operations // Use DSN params for PRAGMAs to ensure they apply to all connections in the pool dsn := fmt.Sprintf("file:%s?_pragma=busy_timeout(5000)&_pragma=journal_mode(wal)", "/path/to/db.sqlite") sqlDB, err := sql.Open("sqlite", dsn) if err != nil { ... } ``` ## Restore Pattern ```go // Create replica without database for restore replica := litestream.NewReplicaWithClient(nil, client) opt := litestream.NewRestoreOptions() opt.OutputPath = "/path/to/restored.db" // Optional: point-in-time restore // opt.Timestamp = time.Now().Add(-1 * time.Hour) if err := replica.Restore(ctx, opt); err != nil { if errors.Is(err, litestream.ErrTxNotAvailable) || errors.Is(err, litestream.ErrNoSnapshots) { // No backup available, create fresh database } return err } ``` ## Supported Backends - `file` - Local filesystem - `s3` - AWS S3 and S3-compatible storage - `gs` - Google Cloud Storage - `abs` - Azure Blob Storage - `oss` - Alibaba Cloud OSS - `sftp` - SFTP servers - `nats` - NATS JetStream - `webdav` - WebDAV servers ## Key Configuration Options ### Store Settings ```go store.SnapshotInterval = 24 * time.Hour // How often to create snapshots store.SnapshotRetention = 24 * time.Hour // How long to keep snapshots store.L0Retention = 5 * time.Minute // How long to keep L0 files after compaction ``` ### DB Settings ```go db.MonitorInterval = 1 * time.Second // How often to check for changes db.CheckpointInterval = 1 * time.Minute // Time-based checkpoint interval db.MinCheckpointPageN = 1000 // Page threshold for checkpoint db.BusyTimeout = 1 * time.Second // SQLite busy timeout ``` ### Replica Settings ```go replica.SyncInterval = 1 * time.Second // Time between syncs replica.MonitorEnabled = true // Auto-sync in background ``` ## Resources - [Litestream Documentation](https://litestream.io) - [GitHub Repository](https://github.com/benbjohnson/litestream) ================================================ FILE: _examples/library/basic/main.go ================================================ // Example: Basic Litestream Library Usage // // This example demonstrates the simplest way to use Litestream as a Go library. // It replicates a SQLite database to the local filesystem. // // Run: go run main.go package main import ( "context" "database/sql" "fmt" "log" "os" "os/signal" "syscall" "time" _ "modernc.org/sqlite" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/file" ) func main() { if err := run(context.Background()); err != nil { log.Fatal(err) } } func run(ctx context.Context) error { // Paths for this example dbPath := "./myapp.db" replicaPath := "./replica" // 1. Create the Litestream DB wrapper db := litestream.NewDB(dbPath) // 2. Create a replica client (file-based for this example) client := file.NewReplicaClient(replicaPath) // 3. Create a replica and attach it to the database replica := litestream.NewReplicaWithClient(db, client) db.Replica = replica client.Replica = replica // 4. Create compaction levels (L0 is required, plus at least one more level) levels := litestream.CompactionLevels{ {Level: 0}, {Level: 1, Interval: 10 * time.Second}, } // 5. Create a Store to manage the database and background compaction store := litestream.NewStore([]*litestream.DB{db}, levels) // 6. Open the store (opens all DBs and starts background monitors) if err := store.Open(ctx); err != nil { return fmt.Errorf("open store: %w", err) } defer func() { if err := store.Close(context.Background()); err != nil { log.Printf("close store: %v", err) } }() // 7. Open your app's SQLite connection for normal database operations sqlDB, err := openAppDB(ctx, dbPath) if err != nil { return fmt.Errorf("open app db: %w", err) } defer sqlDB.Close() if err := initSchema(ctx, sqlDB); err != nil { return fmt.Errorf("init schema: %w", err) } // Insert some test data periodically ticker := time.NewTicker(2 * time.Second) defer ticker.Stop() // Handle shutdown gracefully sigCh := make(chan os.Signal, 1) signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) fmt.Println("Writing data every 2 seconds. Press Ctrl+C to stop.") fmt.Printf("Database: %s\n", dbPath) fmt.Printf("Replica: %s\n", replicaPath) for { select { case <-ticker.C: if err := insertRow(ctx, sqlDB); err != nil { log.Printf("insert row: %v", err) } case <-sigCh: fmt.Println("\nShutting down...") return nil } } } func openAppDB(_ context.Context, path string) (*sql.DB, error) { dsn := fmt.Sprintf("file:%s?_pragma=busy_timeout(5000)&_pragma=journal_mode(wal)", path) return sql.Open("sqlite", dsn) } func initSchema(ctx context.Context, db *sql.DB) error { _, err := db.ExecContext(ctx, ` CREATE TABLE IF NOT EXISTS events ( id INTEGER PRIMARY KEY AUTOINCREMENT, message TEXT NOT NULL, created_at TEXT NOT NULL ) `) return err } func insertRow(ctx context.Context, db *sql.DB) error { msg := fmt.Sprintf("Event at %s", time.Now().Format(time.RFC3339)) result, err := db.ExecContext(ctx, `INSERT INTO events (message, created_at) VALUES (?, ?)`, msg, time.Now().Format(time.RFC3339)) if err != nil { return err } id, _ := result.LastInsertId() fmt.Printf("Inserted row %d: %s\n", id, msg) return nil } ================================================ FILE: _examples/library/library_example_test.go ================================================ package library_test import ( "context" "database/sql" "errors" "fmt" "path/filepath" "testing" "time" _ "modernc.org/sqlite" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/file" ) func TestLibraryExampleFileBackend(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() rootDir := t.TempDir() dbPath := filepath.Join(rootDir, "example.db") replicaPath := filepath.Join(rootDir, "replica") db := litestream.NewDB(dbPath) client := file.NewReplicaClient(replicaPath) replica := litestream.NewReplicaWithClient(db, client) db.Replica = replica client.Replica = replica levels := litestream.CompactionLevels{ {Level: 0}, {Level: 1, Interval: 10 * time.Second}, } store := litestream.NewStore([]*litestream.DB{db}, levels) closed := false t.Cleanup(func() { if !closed { _ = store.Close(context.Background()) } }) if err := store.Open(ctx); err != nil { t.Fatalf("open store: %v", err) } sqlDB, err := openAppDB(ctx, dbPath) if err != nil { t.Fatalf("open app db: %v", err) } defer sqlDB.Close() if _, err := sqlDB.ExecContext(ctx, ` CREATE TABLE IF NOT EXISTS events ( id INTEGER PRIMARY KEY AUTOINCREMENT, message TEXT NOT NULL ) `); err != nil { t.Fatalf("create table: %v", err) } if _, err := sqlDB.ExecContext(ctx, `INSERT INTO events (message) VALUES ('hello');`); err != nil { t.Fatalf("insert row: %v", err) } if err := waitForLTXFiles(replicaPath, 5*time.Second); err != nil { t.Fatal(err) } if err := sqlDB.Close(); err != nil { t.Fatalf("close app db: %v", err) } if err := store.Close(ctx); err != nil && !errors.Is(err, sql.ErrTxDone) { t.Fatalf("close store: %v", err) } closed = true restoreClient := file.NewReplicaClient(replicaPath) restoreReplica := litestream.NewReplicaWithClient(nil, restoreClient) restorePath := filepath.Join(rootDir, "restored.db") opt := litestream.NewRestoreOptions() opt.OutputPath = restorePath if err := restoreReplica.Restore(ctx, opt); err != nil { t.Fatalf("restore: %v", err) } } func openAppDB(ctx context.Context, path string) (*sql.DB, error) { db, err := sql.Open("sqlite", path) if err != nil { return nil, err } if _, err := db.ExecContext(ctx, `PRAGMA journal_mode = wal;`); err != nil { _ = db.Close() return nil, err } if _, err := db.ExecContext(ctx, `PRAGMA busy_timeout = 5000;`); err != nil { _ = db.Close() return nil, err } return db, nil } func waitForLTXFiles(replicaPath string, timeout time.Duration) error { deadline := time.Now().Add(timeout) for time.Now().Before(deadline) { matches, err := filepath.Glob(filepath.Join(replicaPath, "ltx", "0", "*.ltx")) if err != nil { return fmt.Errorf("glob ltx files: %w", err) } if len(matches) > 0 { return nil } time.Sleep(100 * time.Millisecond) } return fmt.Errorf("timeout waiting for ltx files in %s", replicaPath) } ================================================ FILE: _examples/library/s3/main.go ================================================ // Example: Litestream Library Usage with S3 and Restore-on-Startup // // This example demonstrates a production-like pattern for using Litestream: // - Check if local database exists // - If not, restore from S3 backup (if available) // - Start replication to S3 // - Graceful shutdown // // Environment variables: // - AWS_ACCESS_KEY_ID: AWS access key // - AWS_SECRET_ACCESS_KEY: AWS secret key // - LITESTREAM_BUCKET: S3 bucket name (e.g., "my-backup-bucket") // - LITESTREAM_PATH: Path within bucket (e.g., "databases/myapp") // - AWS_REGION: AWS region (default: us-east-1) // // Run: go run main.go package main import ( "context" "database/sql" "errors" "fmt" "log" "os" "os/signal" "syscall" "time" _ "modernc.org/sqlite" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/s3" ) const dbPath = "./myapp.db" func main() { if err := run(context.Background()); err != nil { log.Fatal(err) } } func run(ctx context.Context) error { // Load configuration from environment bucket := os.Getenv("LITESTREAM_BUCKET") if bucket == "" { return fmt.Errorf("LITESTREAM_BUCKET environment variable required") } path := os.Getenv("LITESTREAM_PATH") if path == "" { path = "litestream" } region := os.Getenv("AWS_REGION") if region == "" { region = "us-east-1" } // 1. Create S3 replica client client := s3.NewReplicaClient() client.Bucket = bucket client.Path = path client.Region = region client.AccessKeyID = os.Getenv("AWS_ACCESS_KEY_ID") client.SecretAccessKey = os.Getenv("AWS_SECRET_ACCESS_KEY") // 2. Restore from S3 if local database doesn't exist if err := restoreIfNotExists(ctx, client, dbPath); err != nil { return fmt.Errorf("restore: %w", err) } // 3. Create the Litestream DB wrapper db := litestream.NewDB(dbPath) // 4. Create replica and attach to database replica := litestream.NewReplicaWithClient(db, client) db.Replica = replica // 5. Create compaction levels (L0 is required, plus at least one more level) levels := litestream.CompactionLevels{ {Level: 0}, {Level: 1, Interval: 10 * time.Second}, } // 6. Create a Store to manage the database and background compaction store := litestream.NewStore([]*litestream.DB{db}, levels) // 7. Open store (opens all DBs and starts background monitors) if err := store.Open(ctx); err != nil { return fmt.Errorf("open store: %w", err) } defer func() { log.Println("Closing store...") if err := store.Close(context.Background()); err != nil { log.Printf("close store: %v", err) } }() // 8. Open your app's SQLite connection for normal operations sqlDB, err := openAppDB(ctx, dbPath) if err != nil { return fmt.Errorf("open app db: %w", err) } defer sqlDB.Close() if err := initSchema(ctx, sqlDB); err != nil { return fmt.Errorf("init schema: %w", err) } // Start the application log.Printf("Database: %s", dbPath) log.Printf("Replicating to: s3://%s/%s", bucket, path) log.Println("Writing data every 2 seconds. Press Ctrl+C to stop.") ticker := time.NewTicker(2 * time.Second) defer ticker.Stop() sigCh := make(chan os.Signal, 1) signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) for { select { case <-ticker.C: if err := insertRow(ctx, sqlDB); err != nil { log.Printf("insert row: %v", err) } case <-sigCh: log.Println("Shutting down...") return nil } } } // restoreIfNotExists restores the database from S3 if it doesn't exist locally. func restoreIfNotExists(ctx context.Context, client *s3.ReplicaClient, dbPath string) error { // Check if database already exists if _, err := os.Stat(dbPath); err == nil { log.Println("Local database found, skipping restore") return nil } else if !os.IsNotExist(err) { return err } log.Println("Local database not found, attempting restore from S3...") // Initialize the client if err := client.Init(ctx); err != nil { return fmt.Errorf("init s3 client: %w", err) } // Create a replica (without DB) for restore replica := litestream.NewReplicaWithClient(nil, client) // Set up restore options opt := litestream.NewRestoreOptions() opt.OutputPath = dbPath // Attempt restore if err := replica.Restore(ctx, opt); err != nil { // If no backup exists, that's OK - we'll create a fresh database if errors.Is(err, litestream.ErrTxNotAvailable) || errors.Is(err, litestream.ErrNoSnapshots) { log.Println("No backup found in S3, will create new database") return nil } return err } log.Println("Database restored from S3") return nil } func openAppDB(_ context.Context, path string) (*sql.DB, error) { dsn := fmt.Sprintf("file:%s?_pragma=busy_timeout(5000)&_pragma=journal_mode(wal)", path) return sql.Open("sqlite", dsn) } func initSchema(ctx context.Context, db *sql.DB) error { _, err := db.ExecContext(ctx, ` CREATE TABLE IF NOT EXISTS events ( id INTEGER PRIMARY KEY AUTOINCREMENT, message TEXT NOT NULL, created_at TEXT NOT NULL ) `) return err } func insertRow(ctx context.Context, db *sql.DB) error { msg := fmt.Sprintf("Event at %s", time.Now().Format(time.RFC3339)) result, err := db.ExecContext(ctx, `INSERT INTO events (message, created_at) VALUES (?, ?)`, msg, time.Now().Format(time.RFC3339)) if err != nil { return err } id, _ := result.LastInsertId() log.Printf("Inserted row %d: %s", id, msg) return nil } ================================================ FILE: abs/replica_client.go ================================================ package abs import ( "bytes" "context" "errors" "fmt" "io" "log/slog" "net/http" "net/url" "os" "path" "strings" "sync" "time" "github.com/Azure/azure-sdk-for-go/sdk/azcore" "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" "github.com/Azure/azure-sdk-for-go/sdk/azidentity" "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob" "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/bloberror" "github.com/superfly/ltx" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/internal" ) func init() { litestream.RegisterReplicaClientFactory("abs", NewReplicaClientFromURL) } // ReplicaClientType is the client type for this package. const ReplicaClientType = "abs" // MetadataKeyTimestamp is the metadata key for storing LTX file timestamps in Azure Blob Storage. // Azure metadata keys cannot contain hyphens, so we use litestreamtimestamp (C# identifier rules). const MetadataKeyTimestamp = "litestreamtimestamp" var _ litestream.ReplicaClient = (*ReplicaClient)(nil) // ReplicaClient is a client for writing LTX files to Azure Blob Storage. type ReplicaClient struct { mu sync.Mutex client *azblob.Client logger *slog.Logger // Azure credentials AccountName string AccountKey string SASToken string // SAS token for container-level access Endpoint string // Azure Blob Storage container information Bucket string Path string } // NewReplicaClient returns a new instance of ReplicaClient. func NewReplicaClient() *ReplicaClient { return &ReplicaClient{ logger: slog.Default().WithGroup(ReplicaClientType), } } func (c *ReplicaClient) SetLogger(logger *slog.Logger) { c.logger = logger.WithGroup(ReplicaClientType) } // NewReplicaClientFromURL creates a new ReplicaClient from URL components. // This is used by the replica client factory registration. // URL format: abs://[account-name@]container/path func NewReplicaClientFromURL(scheme, host, urlPath string, query url.Values, userinfo *url.Userinfo) (litestream.ReplicaClient, error) { client := NewReplicaClient() // Extract account name from userinfo if present (abs://account@container/path) if userinfo != nil { client.AccountName = userinfo.Username() } client.Bucket = host client.Path = urlPath if client.Bucket == "" { return nil, fmt.Errorf("bucket required for abs replica URL") } return client, nil } // Type returns "abs" as the client type. func (c *ReplicaClient) Type() string { return ReplicaClientType } // Init initializes the connection to Azure. No-op if already initialized. func (c *ReplicaClient) Init(ctx context.Context) (err error) { c.mu.Lock() defer c.mu.Unlock() if c.client != nil { return nil } // Validate required configuration if c.Bucket == "" { return fmt.Errorf("abs: container name is required") } // Construct & parse endpoint unless already set. endpoint := c.Endpoint if endpoint == "" { if c.AccountName == "" { return fmt.Errorf("abs: account name is required when endpoint is not specified") } endpoint = fmt.Sprintf("https://%s.blob.core.windows.net", c.AccountName) } // Configure client options with retry policy clientOptions := &azblob.ClientOptions{ ClientOptions: azcore.ClientOptions{ Retry: policy.RetryOptions{ MaxRetries: 10, RetryDelay: time.Second, MaxRetryDelay: 30 * time.Second, TryTimeout: 15 * time.Minute, // Reasonable timeout for blob operations StatusCodes: []int{ http.StatusRequestTimeout, http.StatusTooManyRequests, http.StatusInternalServerError, http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout, }, }, Telemetry: policy.TelemetryOptions{ ApplicationID: "litestream", }, }, } // Check for SAS token first (highest priority for explicit credentials) sasToken := c.SASToken if sasToken == "" { sasToken = os.Getenv("LITESTREAM_AZURE_SAS_TOKEN") } // Check if we have explicit credentials or should use default credential chain accountKey := c.AccountKey if accountKey == "" { accountKey = os.Getenv("LITESTREAM_AZURE_ACCOUNT_KEY") } // Create Azure Blob Storage client with appropriate authentication // Priority: SAS token > Shared key > Default credential chain var client *azblob.Client if sasToken != "" { // SAS token authentication - append token to endpoint URL if accountKey != "" { slog.Warn("both SAS token and account key configured, using SAS token") } else { slog.Debug("using SAS token authentication") } // Strip leading "?" if present to avoid double "?" endpointWithSAS := fmt.Sprintf("%s?%s", endpoint, strings.TrimPrefix(sasToken, "?")) client, err = azblob.NewClientWithNoCredential(endpointWithSAS, clientOptions) if err != nil { return fmt.Errorf("abs: cannot create azure blob client with SAS token: %w", err) } } else if accountKey != "" && c.AccountName != "" { // Use shared key authentication (existing behavior) slog.Debug("using shared key authentication") credential, err := azblob.NewSharedKeyCredential(c.AccountName, accountKey) if err != nil { return fmt.Errorf("abs: cannot create shared key credential: %w", err) } client, err = azblob.NewClientWithSharedKeyCredential(endpoint, credential, clientOptions) if err != nil { return fmt.Errorf("abs: cannot create azure blob client with shared key: %w", err) } } else { // Use default credential chain (similar to AWS SDK default credential chain) // This includes: // - Environment variables (AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID) // - Managed Identity (for Azure VMs, App Service, etc.) // - Azure CLI credentials // - Visual Studio Code credentials slog.Debug("using default credential chain (managed identity, Azure CLI, environment variables, etc.)") credential, err := azidentity.NewDefaultAzureCredential(nil) if err != nil { return fmt.Errorf("abs: cannot create default azure credential: %w", err) } client, err = azblob.NewClient(endpoint, credential, clientOptions) if err != nil { return fmt.Errorf("abs: cannot create azure blob client with default credential: %w", err) } } c.client = client return nil } // LTXFiles returns an iterator over all available LTX files. // Azure always uses accurate timestamps from metadata since they're included in LIST operations at zero cost. // The useMetadata parameter is ignored. func (c *ReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { if err := c.Init(ctx); err != nil { return nil, err } return newLTXFileIterator(ctx, c, level, seek), nil } // WriteLTXFile writes an LTX file to remote storage. func (c *ReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, rd io.Reader) (info *ltx.FileInfo, err error) { if err := c.Init(ctx); err != nil { return nil, err } key := litestream.LTXFilePath(c.Path, level, minTXID, maxTXID) // Use TeeReader to peek at LTX header while preserving data for upload var buf bytes.Buffer teeReader := io.TeeReader(rd, &buf) // Extract timestamp from LTX header hdr, _, err := ltx.PeekHeader(teeReader) if err != nil { return nil, fmt.Errorf("extract timestamp from LTX header: %w", err) } timestamp := time.UnixMilli(hdr.Timestamp).UTC() // Combine buffered data with rest of reader rc := internal.NewReadCounter(io.MultiReader(&buf, rd)) // Upload blob with proper content type, access tier, and metadata // Azure metadata keys cannot contain hyphens, so use litestreamtimestamp _, err = c.client.UploadStream(ctx, c.Bucket, key, rc, &azblob.UploadStreamOptions{ HTTPHeaders: &blob.HTTPHeaders{ BlobContentType: to.Ptr("application/octet-stream"), }, AccessTier: to.Ptr(blob.AccessTierHot), // Use Hot tier as default Metadata: map[string]*string{ MetadataKeyTimestamp: to.Ptr(timestamp.Format(time.RFC3339Nano)), }, }) if err != nil { return nil, fmt.Errorf("abs: cannot upload ltx file %q: %w", key, err) } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "PUT").Inc() internal.OperationBytesCounterVec.WithLabelValues(ReplicaClientType, "PUT").Add(float64(rc.N())) return <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Size: rc.N(), CreatedAt: timestamp, }, nil } // OpenLTXFile returns a reader for an LTX file. // Returns os.ErrNotExist if no matching min/max TXID is not found. func (c *ReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { if err := c.Init(ctx); err != nil { return nil, err } key := litestream.LTXFilePath(c.Path, level, minTXID, maxTXID) resp, err := c.client.DownloadStream(ctx, c.Bucket, key, &azblob.DownloadStreamOptions{ Range: blob.HTTPRange{ Offset: offset, Count: size, }, }) if isNotExists(err) { return nil, os.ErrNotExist } else if err != nil { return nil, fmt.Errorf("abs: cannot start new reader for %q: %w", key, err) } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "GET").Inc() internal.OperationBytesCounterVec.WithLabelValues(ReplicaClientType, "GET").Add(float64(*resp.ContentLength)) return resp.Body, nil } // DeleteLTXFiles deletes LTX files. func (c *ReplicaClient) DeleteLTXFiles(ctx context.Context, a []*ltx.FileInfo) error { if err := c.Init(ctx); err != nil { return err } for _, info := range a { key := litestream.LTXFilePath(c.Path, info.Level, info.MinTXID, info.MaxTXID) c.logger.Debug("deleting ltx file", "level", info.Level, "minTXID", info.MinTXID, "maxTXID", info.MaxTXID, "key", key) _, err := c.client.DeleteBlob(ctx, c.Bucket, key, nil) if isNotExists(err) { continue } else if err != nil { return fmt.Errorf("abs: cannot delete ltx file %q: %w", key, err) } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "DELETE").Inc() } return nil } // DeleteAll deletes all LTX files. func (c *ReplicaClient) DeleteAll(ctx context.Context) error { if err := c.Init(ctx); err != nil { return err } // List all blobs with the configured path prefix prefix := "/" if c.Path != "" { prefix = strings.TrimSuffix(c.Path, "/") + "/" } pager := c.client.NewListBlobsFlatPager(c.Bucket, &azblob.ListBlobsFlatOptions{ Prefix: &prefix, Include: azblob.ListBlobsInclude{Metadata: true}, }) for pager.More() { internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "LIST").Inc() resp, err := pager.NextPage(ctx) if err != nil { return fmt.Errorf("abs: cannot list blobs: %w", err) } for _, item := range resp.Segment.BlobItems { internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "DELETE").Inc() _, err := c.client.DeleteBlob(ctx, c.Bucket, *item.Name, nil) if isNotExists(err) { continue } else if err != nil { return fmt.Errorf("abs: cannot delete blob %q: %w", *item.Name, err) } } } return nil } type ltxFileIterator struct { ctx context.Context cancel context.CancelFunc client *ReplicaClient level int seek ltx.TXID pager *runtime.Pager[azblob.ListBlobsFlatResponse] pageItems []*ltx.FileInfo pageIndex int closed bool err error info *ltx.FileInfo } func newLTXFileIterator(ctx context.Context, client *ReplicaClient, level int, seek ltx.TXID) *ltxFileIterator { ctx, cancel := context.WithCancel(ctx) itr := <xFileIterator{ ctx: ctx, cancel: cancel, client: client, level: level, seek: seek, } // Create paginator for listing blobs with level prefix dir := litestream.LTXLevelDir(client.Path, level) prefix := dir + "/" if seek != 0 { prefix += seek.String() } itr.pager = client.client.NewListBlobsFlatPager(client.Bucket, &azblob.ListBlobsFlatOptions{ Prefix: &prefix, Include: azblob.ListBlobsInclude{Metadata: true}, }) return itr } func (itr *ltxFileIterator) Close() (err error) { itr.closed = true itr.cancel() return itr.err } func (itr *ltxFileIterator) Next() bool { if itr.closed || itr.err != nil { return false } // Process blobs until we find a valid LTX file for { // Load next page if needed if itr.pageItems == nil || itr.pageIndex >= len(itr.pageItems) { if !itr.loadNextPage() { return false } } // Process current item from page if itr.pageIndex < len(itr.pageItems) { itr.info = itr.pageItems[itr.pageIndex] itr.pageIndex++ return true } } } // loadNextPage loads the next page of blobs and extracts valid LTX files func (itr *ltxFileIterator) loadNextPage() bool { if !itr.pager.More() { return false } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "LIST").Inc() resp, err := itr.pager.NextPage(itr.ctx) if err != nil { itr.err = fmt.Errorf("abs: cannot list blobs: %w", err) return false } // Extract blob items directly from the response itr.pageItems = nil itr.pageIndex = 0 for _, item := range resp.Segment.BlobItems { key := path.Base(*item.Name) minTXID, maxTXID, err := ltx.ParseFilename(key) if err != nil { continue // Skip non-LTX files } // Build file info info := <x.FileInfo{ Level: itr.level, MinTXID: minTXID, MaxTXID: maxTXID, Size: *item.Properties.ContentLength, } // Skip if below seek TXID if info.MinTXID < itr.seek { continue } // Skip if wrong level if info.Level != itr.level { continue } // Always use accurate timestamp from metadata since it's zero-cost // Azure includes metadata in LIST operations, so no extra API call needed info.CreatedAt = item.Properties.CreationTime.UTC() if item.Metadata != nil { if ts, ok := item.Metadata[MetadataKeyTimestamp]; ok && ts != nil { if parsed, err := time.Parse(time.RFC3339Nano, *ts); err == nil { info.CreatedAt = parsed } } } itr.pageItems = append(itr.pageItems, info) } return len(itr.pageItems) > 0 || itr.pager.More() } func (itr *ltxFileIterator) Err() error { return itr.err } func (itr *ltxFileIterator) Item() *ltx.FileInfo { return itr.info } func isNotExists(err error) bool { var respErr *azcore.ResponseError if errors.As(err, &respErr) { return respErr.ErrorCode == string(bloberror.BlobNotFound) || respErr.ErrorCode == string(bloberror.ContainerNotFound) } return false } ================================================ FILE: cmd/litestream/databases.go ================================================ package main import ( "context" "flag" "fmt" "os" "text/tabwriter" ) // DatabasesCommand is a command for listing managed databases. type DatabasesCommand struct{} // Run executes the command. func (c *DatabasesCommand) Run(_ context.Context, args []string) (err error) { fs := flag.NewFlagSet("litestream-databases", flag.ContinueOnError) configPath, noExpandEnv := registerConfigFlag(fs) fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } else if fs.NArg() != 0 { return fmt.Errorf("too many arguments") } // Load configuration. if *configPath == "" { *configPath = DefaultConfigPath() } config, err := ReadConfigFile(*configPath, !*noExpandEnv) if err != nil { return err } // List all databases. w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0) defer w.Flush() fmt.Fprintln(w, "path\treplica") for _, dbConfig := range config.DBs { db, err := NewDBFromConfig(dbConfig) if err != nil { return err } fmt.Fprintf(w, "%s\t%s\n", db.Path(), db.Replica.Client.Type()) } return nil } // Usage prints the help screen to STDOUT. func (c *DatabasesCommand) Usage() { fmt.Printf(` The databases command lists all databases in the configuration file. Usage: litestream databases [arguments] Arguments: -config PATH Specifies the configuration file. Defaults to %s -no-expand-env Disables environment variable expansion in configuration file. `[1:], DefaultConfigPath(), ) } ================================================ FILE: cmd/litestream/directory_watcher.go ================================================ package main import ( "context" "errors" "log/slog" "os" "path/filepath" "strings" "sync" "time" "github.com/fsnotify/fsnotify" "github.com/benbjohnson/litestream" ) const debounceInterval = 250 * time.Millisecond // DirectoryMonitor watches a directory tree for SQLite databases and dynamically // manages database instances within the store as files are created or removed. type DirectoryMonitor struct { store *litestream.Store config *DBConfig dirPath string pattern string recursive bool watcher *fsnotify.Watcher ctx context.Context cancel context.CancelFunc logger *slog.Logger mu sync.Mutex dbs map[string]*litestream.DB watchedDirs map[string]struct{} // Only accessed from the run() goroutine, so no mutex is needed. pendingEvents map[string]fsnotify.Op debounceActive bool wg sync.WaitGroup } // NewDirectoryMonitor returns a new monitor for directory-based replication. func NewDirectoryMonitor(ctx context.Context, store *litestream.Store, dbc *DBConfig, existing []*litestream.DB) (*DirectoryMonitor, error) { if dbc == nil { return nil, errors.New("database config required") } if store == nil { return nil, errors.New("store required") } dirPath, err := expand(dbc.Dir) if err != nil { return nil, err } if _, err := os.Stat(dirPath); err != nil { return nil, err } watcher, err := fsnotify.NewWatcher() if err != nil { return nil, err } monitorCtx, cancel := context.WithCancel(ctx) dm := &DirectoryMonitor{ store: store, config: dbc, dirPath: dirPath, pattern: dbc.Pattern, recursive: dbc.Recursive, watcher: watcher, ctx: monitorCtx, cancel: cancel, logger: slog.With("dir", dirPath), dbs: make(map[string]*litestream.DB), watchedDirs: make(map[string]struct{}), pendingEvents: make(map[string]fsnotify.Op), } for _, db := range existing { dm.dbs[db.Path()] = db } if err := dm.addInitialWatches(); err != nil { watcher.Close() cancel() return nil, err } dm.scanDirectory(dm.dirPath) dm.wg.Add(1) go dm.run() return dm, nil } // Close stops the directory monitor and releases resources. func (dm *DirectoryMonitor) Close() { dm.cancel() _ = dm.watcher.Close() dm.wg.Wait() } func (dm *DirectoryMonitor) run() { defer dm.wg.Done() // Debounce timer lives in this goroutine so shutdown via dm.wg.Wait() is clean. debounceTimer := time.NewTimer(debounceInterval) debounceTimer.Stop() defer debounceTimer.Stop() for { select { case <-dm.ctx.Done(): return case event, ok := <-dm.watcher.Events: if !ok { return } if dm.handleEvent(event) && !dm.debounceActive { debounceTimer.Reset(debounceInterval) dm.debounceActive = true } case <-debounceTimer.C: dm.flushPendingEvents() case err, ok := <-dm.watcher.Errors: if !ok { return } dm.logger.Error("directory watcher error", "error", err) } } } func (dm *DirectoryMonitor) addInitialWatches() error { if dm.recursive { return filepath.WalkDir(dm.dirPath, func(path string, d os.DirEntry, err error) error { if err != nil { return err } if !d.IsDir() { return nil } return dm.addDirectoryWatch(path) }) } return dm.addDirectoryWatch(dm.dirPath) } func (dm *DirectoryMonitor) addDirectoryWatch(path string) error { abspath := filepath.Clean(path) dm.mu.Lock() defer dm.mu.Unlock() if _, ok := dm.watchedDirs[abspath]; ok { return nil } dm.watchedDirs[abspath] = struct{}{} if err := dm.watcher.Add(abspath); err != nil { delete(dm.watchedDirs, abspath) return err } dm.logger.Debug("watching directory", "path", abspath) return nil } func (dm *DirectoryMonitor) removeDirectoryWatch(path string) { abspath := filepath.Clean(path) dm.mu.Lock() defer dm.mu.Unlock() if _, ok := dm.watchedDirs[abspath]; !ok { return } delete(dm.watchedDirs, abspath) if err := dm.watcher.Remove(abspath); err != nil { dm.logger.Debug("remove directory watch", "path", abspath, "error", err) } } // handleEvent processes a single fsnotify event. Returns true if pending events // were queued and the debounce timer should be reset. func (dm *DirectoryMonitor) handleEvent(event fsnotify.Event) bool { path := filepath.Clean(event.Name) if path == "" { return false } if dm.shouldSkipPath(path) { return false } info, statErr := os.Stat(path) isDir := statErr == nil && info.IsDir() // Early pattern check for create/write only. Rename must pass through // to removal handling below, since the old path may be a tracked DB. if !isDir && event.Op&(fsnotify.Create|fsnotify.Write) != 0 { if !dm.matchesPattern(path) { return false } } dm.mu.Lock() _, wasWatchedDir := dm.watchedDirs[path] dm.mu.Unlock() if isDir && event.Op&(fsnotify.Create|fsnotify.Rename) != 0 { if dm.recursive { if err := dm.addDirectoryWatch(path); err != nil { dm.logger.Error("add directory watch", "path", path, "error", err) } dm.scanDirectory(path) } } if (isDir || wasWatchedDir) && event.Op&(fsnotify.Remove|fsnotify.Rename) != 0 { dm.removeDirectoryWatch(path) dm.removeDatabasesUnder(path) return false } if isDir { return false } if statErr != nil && !os.IsNotExist(statErr) { dm.logger.Debug("stat event path", "path", path, "error", statErr) return false } if event.Op&(fsnotify.Remove|fsnotify.Rename) != 0 { dm.removeDatabase(path) return false } if event.Op&(fsnotify.Create|fsnotify.Write|fsnotify.Rename) != 0 { dm.pendingEvents[path] |= event.Op return true } return false } // flushPendingEvents processes all queued potential database events. func (dm *DirectoryMonitor) flushPendingEvents() { for path := range dm.pendingEvents { select { case <-dm.ctx.Done(): return default: } dm.handlePotentialDatabase(path) } dm.pendingEvents = make(map[string]fsnotify.Op) dm.debounceActive = false } func (dm *DirectoryMonitor) handlePotentialDatabase(path string) { if !dm.matchesPattern(path) { return } dm.mu.Lock() if _, exists := dm.dbs[path]; exists { dm.mu.Unlock() return } dm.dbs[path] = nil dm.mu.Unlock() var db *litestream.DB success := false defer func() { if !success { if db != nil { _ = dm.store.UnregisterDB(dm.ctx, db.Path()) } dm.mu.Lock() delete(dm.dbs, path) dm.mu.Unlock() } }() if !IsSQLiteDatabase(path) { return } var err error db, err = newDBFromDirectoryEntry(dm.config, dm.dirPath, path) if err != nil { dm.logger.Error("configure database", "path", path, "error", err) return } if err := dm.store.RegisterDB(db); err != nil { dm.logger.Error("register database with store", "path", path, "error", err) return } dm.mu.Lock() dm.dbs[path] = db dm.mu.Unlock() success = true dm.logger.Info("added database to replication", "path", path) } func (dm *DirectoryMonitor) removeDatabase(path string) { dm.mu.Lock() db := dm.dbs[path] dm.mu.Unlock() if db == nil { return } if err := dm.store.UnregisterDB(dm.ctx, db.Path()); err != nil { dm.logger.Error("unregister database from store", "path", path, "error", err) return } dm.mu.Lock() delete(dm.dbs, path) dm.mu.Unlock() dm.logger.Info("removed database from replication", "path", path) } func (dm *DirectoryMonitor) removeDatabasesUnder(dir string) { prefix := dir + string(os.PathSeparator) dm.mu.Lock() var toClose []*litestream.DB var toClosePaths []string for path, db := range dm.dbs { if path == dir || strings.HasPrefix(path, prefix) { toClose = append(toClose, db) toClosePaths = append(toClosePaths, path) } } dm.mu.Unlock() for i, db := range toClose { if db == nil { dm.mu.Lock() delete(dm.dbs, toClosePaths[i]) dm.mu.Unlock() continue } if err := dm.store.UnregisterDB(dm.ctx, db.Path()); err != nil { dm.logger.Error("unregister database from store", "path", db.Path(), "error", err) continue } dm.mu.Lock() delete(dm.dbs, toClosePaths[i]) dm.mu.Unlock() } } func (dm *DirectoryMonitor) matchesPattern(path string) bool { matched, err := filepath.Match(dm.pattern, filepath.Base(path)) if err != nil { dm.logger.Error("pattern match failed", "pattern", dm.pattern, "path", path, "error", err) return false } return matched } // shouldSkipPath returns true for SQLite auxiliary files (WAL, SHM, journal) // that generate many events but are never databases themselves. func (dm *DirectoryMonitor) shouldSkipPath(path string) bool { base := filepath.Base(path) return strings.HasSuffix(base, "-wal") || strings.HasSuffix(base, "-shm") || strings.HasSuffix(base, "-journal") } // scanDirectory discovers pre-existing databases and is also called when new // directories appear to close the race window between watch registration and file creation. func (dm *DirectoryMonitor) scanDirectory(dir string) { if !dm.recursive { entries, err := os.ReadDir(dir) if err != nil { if !os.IsNotExist(err) { dm.logger.Debug("read directory", "path", dir, "error", err) } return } for _, entry := range entries { if entry.IsDir() { continue } path := filepath.Join(dir, entry.Name()) dm.handlePotentialDatabase(path) } return } err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error { if err != nil { if os.IsNotExist(err) { return nil } dm.logger.Debug("scan directory entry", "path", path, "error", err) return nil } if d.IsDir() { if path != dir { if err := dm.addDirectoryWatch(path); err != nil { dm.logger.Error("add directory watch", "path", path, "error", err) } } return nil } dm.handlePotentialDatabase(path) return nil }) if err != nil && !os.IsNotExist(err) { dm.logger.Debug("scan directory", "path", dir, "error", err) } } ================================================ FILE: cmd/litestream/directory_watcher_stress_test.go ================================================ //go:build stress package main import ( "context" "database/sql" "fmt" "path/filepath" "strings" "sync" "sync/atomic" "testing" "time" "github.com/benbjohnson/litestream" ) var dbCounts = []int{100, 250, 500, 1000, 2500} func TestDirectoryWatcher_PreCreated(t *testing.T) { for _, count := range dbCounts { count := count t.Run(fmt.Sprintf("%d", count), func(t *testing.T) { dbDir := t.TempDir() replicaDir := t.TempDir() t.Logf("Creating %d databases...", count) dbs := createTestDatabases(t, dbDir, count) defer closeTestDatabases(dbs) store, monitors := startDirectoryMonitor(t, dbDir, replicaDir) defer stopDirectoryMonitor(store, monitors) timeout := 3*time.Minute + time.Duration(count/100)*time.Minute t.Logf("Waiting for detection (timeout: %v)...", timeout) ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() if err := waitForDBCount(ctx, store, count); err != nil { t.Fatalf("Failed to detect all databases: %v (got %d, expected %d)", err, len(store.DBs()), count) } t.Logf("All %d databases detected successfully", count) }) } } func TestDirectoryWatcher_DynamicScaling(t *testing.T) { for _, finalCount := range dbCounts { finalCount := finalCount t.Run(fmt.Sprintf("%d", finalCount), func(t *testing.T) { batchSize := finalCount / 10 if batchSize < 10 { batchSize = 10 } batchTimeout := 60*time.Second + time.Duration(batchSize/50)*30*time.Second dbDir := t.TempDir() replicaDir := t.TempDir() initialDBs := batchSize t.Logf("Creating initial %d databases...", initialDBs) dbs := createTestDatabases(t, dbDir, initialDBs) store, monitors := startDirectoryMonitor(t, dbDir, replicaDir) defer stopDirectoryMonitor(store, monitors) ctx, cancel := context.WithTimeout(context.Background(), batchTimeout) if err := waitForDBCount(ctx, store, initialDBs); err != nil { cancel() closeTestDatabases(dbs) t.Fatalf("Failed to detect initial databases: %v", err) } cancel() t.Logf("Initial %d databases detected", initialDBs) currentCount := initialDBs for currentCount < finalCount { addCount := batchSize if currentCount+addCount > finalCount { addCount = finalCount - currentCount } t.Logf("Adding batch: %d -> %d databases", currentCount, currentCount+addCount) newDBs := createTestDatabasesBatch(t, dbDir, currentCount, addCount) dbs = append(dbs, newDBs...) ctx, cancel := context.WithTimeout(context.Background(), batchTimeout) expectedCount := currentCount + addCount if err := waitForDBCount(ctx, store, expectedCount); err != nil { cancel() closeTestDatabases(dbs) t.Fatalf("Failed to detect batch (expected %d, got %d): %v", expectedCount, len(store.DBs()), err) } cancel() currentCount += addCount time.Sleep(500 * time.Millisecond) } closeTestDatabases(dbs) t.Logf("Successfully scaled to %d databases", finalCount) }) } } func TestDirectoryWatcher_ConcurrentWrites(t *testing.T) { for _, count := range dbCounts { count := count t.Run(fmt.Sprintf("%d", count), func(t *testing.T) { const writeDuration = 10 * time.Second const writesPerDBPerSec = 5 dbDir := t.TempDir() replicaDir := t.TempDir() t.Logf("Creating %d databases...", count) dbs := createTestDatabases(t, dbDir, count) defer closeTestDatabases(dbs) store, monitors := startDirectoryMonitor(t, dbDir, replicaDir) defer stopDirectoryMonitor(store, monitors) timeout := 3*time.Minute + time.Duration(count/100)*time.Minute ctx, cancel := context.WithTimeout(context.Background(), timeout) if err := waitForDBCount(ctx, store, count); err != nil { cancel() t.Fatalf("Failed to detect databases: %v", err) } cancel() t.Logf("Starting concurrent writes for %v...", writeDuration) var totalWrites int64 var wg sync.WaitGroup writeCtx, writeCancel := context.WithTimeout(context.Background(), writeDuration) defer writeCancel() for i, db := range dbs { wg.Add(1) go func(idx int, db *sql.DB) { defer wg.Done() ticker := time.NewTicker(time.Second / time.Duration(writesPerDBPerSec)) defer ticker.Stop() for { select { case <-writeCtx.Done(): return case <-ticker.C: _, err := db.Exec("INSERT INTO data (value) VALUES (?)", fmt.Sprintf("db%d-%d", idx, time.Now().UnixNano())) if err == nil { atomic.AddInt64(&totalWrites, 1) } } } }(i, db) } wg.Wait() t.Logf("Completed %d total writes across %d databases", totalWrites, count) if totalWrites == 0 { t.Fatal("Expected at least some writes to succeed") } }) } } func createTestDatabases(t *testing.T, dir string, count int) []*sql.DB { return createTestDatabasesBatch(t, dir, 0, count) } func createTestDatabasesBatch(t *testing.T, dir string, startIdx, count int) []*sql.DB { t.Helper() dbs := make([]*sql.DB, 0, count) for i := 0; i < count; i++ { idx := startIdx + i dbPath := filepath.Join(dir, fmt.Sprintf("test_%04d.db", idx)) db, err := sql.Open("sqlite", dbPath) if err != nil { closeTestDatabases(dbs) t.Fatalf("Failed to open database %d: %v", idx, err) } _, err = db.Exec(` PRAGMA journal_mode=WAL; CREATE TABLE IF NOT EXISTS data ( id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP ); `) if err != nil { db.Close() closeTestDatabases(dbs) t.Fatalf("Failed to initialize database %d: %v", idx, err) } dbs = append(dbs, db) } return dbs } func closeTestDatabases(dbs []*sql.DB) { for _, db := range dbs { if db != nil { db.Close() } } } func startDirectoryMonitor(t *testing.T, dbDir, replicaDir string) (*litestream.Store, []*DirectoryMonitor) { t.Helper() syncInterval := time.Second dbConfig := &DBConfig{ Dir: dbDir, Pattern: "*.db", Recursive: false, Watch: true, Replica: &ReplicaConfig{ Type: "file", Path: replicaDir, ReplicaSettings: ReplicaSettings{ SyncInterval: &syncInterval, }, }, } dbs, err := NewDBsFromDirectoryConfig(dbConfig) if err != nil && !strings.Contains(err.Error(), "no SQLite databases found") { t.Fatalf("Failed to create DBs from directory config: %v", err) } store := litestream.NewStore(dbs, litestream.DefaultCompactionLevels) if err := store.Open(context.Background()); err != nil { t.Fatalf("Failed to open store: %v", err) } monitor, err := NewDirectoryMonitor(context.Background(), store, dbConfig, dbs) if err != nil { store.Close(context.Background()) t.Fatalf("Failed to create directory monitor: %v", err) } return store, []*DirectoryMonitor{monitor} } func stopDirectoryMonitor(store *litestream.Store, monitors []*DirectoryMonitor) { for _, m := range monitors { m.Close() } store.Close(context.Background()) } func waitForDBCount(ctx context.Context, store *litestream.Store, expected int) error { ticker := time.NewTicker(100 * time.Millisecond) defer ticker.Stop() for { select { case <-ctx.Done(): return ctx.Err() case <-ticker.C: if len(store.DBs()) >= expected { return nil } } } } ================================================ FILE: cmd/litestream/directory_watcher_test.go ================================================ package main import ( "testing" "github.com/fsnotify/fsnotify" ) func TestDirectoryMonitor_shouldSkipPath(t *testing.T) { dm := &DirectoryMonitor{} tests := []struct { name string path string expected bool }{ // Should skip SQLite auxiliary files {"skip WAL file", "/path/to/db.sqlite-wal", true}, {"skip SHM file", "/path/to/db.sqlite-shm", true}, {"skip journal file", "/path/to/db.sqlite-journal", true}, {"skip WAL file simple", "test.db-wal", true}, {"skip SHM file simple", "test.db-shm", true}, {"skip journal file simple", "test.db-journal", true}, // Should not skip actual database files {"allow .db file", "/path/to/test.db", false}, {"allow .sqlite file", "/path/to/test.sqlite", false}, {"allow .sqlite3 file", "/path/to/test.sqlite3", false}, {"allow arbitrary file", "/path/to/data.dat", false}, // Edge cases {"allow file ending in wal (not -wal)", "/path/to/withdrawal", false}, {"allow file ending in shm (not -shm)", "/path/to/rhythm", false}, {"allow file ending in journal (not -journal)", "/path/to/myjournal", false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := dm.shouldSkipPath(tt.path) if got != tt.expected { t.Errorf("shouldSkipPath(%q) = %v, want %v", tt.path, got, tt.expected) } }) } } func TestDirectoryMonitor_matchesPattern(t *testing.T) { tests := []struct { name string pattern string path string expected bool }{ // *.db pattern {"matches .db", "*.db", "/path/to/test.db", true}, {"no match .sqlite", "*.db", "/path/to/test.sqlite", false}, {"no match .db.backup", "*.db", "/path/to/test.db.backup", false}, // *.sqlite pattern {"matches .sqlite", "*.sqlite", "/path/to/test.sqlite", true}, {"no match .db with sqlite pattern", "*.sqlite", "/path/to/test.db", false}, // * (match all) pattern {"match all pattern", "*", "/path/to/anything.db", true}, {"match all pattern sqlite", "*", "/path/to/anything.sqlite", true}, // prefix patterns {"prefix match", "app_*.db", "/path/to/app_users.db", true}, {"prefix no match", "app_*.db", "/path/to/users.db", false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { dm := &DirectoryMonitor{pattern: tt.pattern} got := dm.matchesPattern(tt.path) if got != tt.expected { t.Errorf("matchesPattern(%q) with pattern %q = %v, want %v", tt.path, tt.pattern, got, tt.expected) } }) } } func TestDirectoryMonitor_pendingEvents(t *testing.T) { t.Run("coalesces multiple events for same path", func(t *testing.T) { dm := &DirectoryMonitor{ pendingEvents: make(map[string]fsnotify.Op), } dm.pendingEvents["/path/to/test.db"] |= fsnotify.Create dm.pendingEvents["/path/to/test.db"] |= fsnotify.Write dm.pendingEvents["/path/to/test.db"] |= fsnotify.Write if len(dm.pendingEvents) != 1 { t.Errorf("expected 1 pending event, got %d", len(dm.pendingEvents)) } op := dm.pendingEvents["/path/to/test.db"] if op&fsnotify.Create == 0 { t.Error("expected Create op to be set") } if op&fsnotify.Write == 0 { t.Error("expected Write op to be set") } }) t.Run("queues different paths separately", func(t *testing.T) { dm := &DirectoryMonitor{ pendingEvents: make(map[string]fsnotify.Op), } dm.pendingEvents["/path/to/db1.db"] |= fsnotify.Create dm.pendingEvents["/path/to/db2.db"] |= fsnotify.Create dm.pendingEvents["/path/to/db3.db"] |= fsnotify.Write if len(dm.pendingEvents) != 3 { t.Errorf("expected 3 pending events, got %d", len(dm.pendingEvents)) } }) } ================================================ FILE: cmd/litestream/info.go ================================================ package main import ( "context" "encoding/json" "flag" "fmt" "io" "net" "net/http" "time" "github.com/benbjohnson/litestream" ) // InfoCommand represents the command to show daemon information. type InfoCommand struct{} // Run executes the info command. func (c *InfoCommand) Run(_ context.Context, args []string) error { fs := flag.NewFlagSet("litestream-info", flag.ContinueOnError) socketPath := fs.String("socket", "/var/run/litestream.sock", "control socket path") timeout := fs.Int("timeout", 10, "timeout in seconds") jsonOutput := fs.Bool("json", false, "output raw JSON") fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } if fs.NArg() > 0 { return fmt.Errorf("too many arguments") } if *timeout <= 0 { return fmt.Errorf("timeout must be greater than 0") } clientTimeout := time.Duration(*timeout) * time.Second client := &http.Client{ Timeout: clientTimeout, Transport: &http.Transport{ DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { return net.DialTimeout("unix", *socketPath, clientTimeout) }, }, } resp, err := client.Get("http://localhost/info") if err != nil { return fmt.Errorf("failed to connect to control socket: %w", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("failed to read response: %w", err) } if resp.StatusCode != http.StatusOK { var errResp litestream.ErrorResponse if err := json.Unmarshal(body, &errResp); err == nil && errResp.Error != "" { return fmt.Errorf("info failed: %s", errResp.Error) } return fmt.Errorf("info failed: %s", string(body)) } var result litestream.InfoResponse if err := json.Unmarshal(body, &result); err != nil { return fmt.Errorf("failed to parse response: %w", err) } if *jsonOutput { output, err := json.MarshalIndent(result, "", " ") if err != nil { return fmt.Errorf("failed to format response: %w", err) } fmt.Println(string(output)) } else { uptime := time.Duration(result.UptimeSeconds) * time.Second fmt.Printf("Litestream %s\n", result.Version) fmt.Printf(" PID: %d\n", result.PID) fmt.Printf(" Uptime: %s\n", uptime) fmt.Printf(" Started at: %s\n", result.StartedAt.Format(time.RFC3339)) fmt.Printf(" Databases: %d\n", result.DatabaseCount) } return nil } // Usage prints the help text for the info command. func (c *InfoCommand) Usage() { fmt.Println(` usage: litestream info [OPTIONS] Show daemon information from a running Litestream instance. Options: -json Output raw JSON instead of human-readable text. -socket PATH Path to control socket (default: /var/run/litestream.sock). -timeout SECONDS Maximum time to wait in seconds (default: 10). `[1:]) } ================================================ FILE: cmd/litestream/info_test.go ================================================ package main_test import ( "context" "fmt" "os" "sync/atomic" "testing" "github.com/benbjohnson/litestream" main "github.com/benbjohnson/litestream/cmd/litestream" "github.com/benbjohnson/litestream/internal/testingutil" ) var testSocketCounter uint64 func testSocketPath(t *testing.T) string { t.Helper() n := atomic.AddUint64(&testSocketCounter, 1) path := fmt.Sprintf("/tmp/ls-cmd-test-%d.sock", n) t.Cleanup(func() { os.Remove(path) }) return path } func TestInfoCommand_Run(t *testing.T) { t.Run("TooManyArguments", func(t *testing.T) { cmd := &main.InfoCommand{} err := cmd.Run(context.Background(), []string{"extra-arg"}) if err == nil { t.Error("expected error for too many arguments") } if err.Error() != "too many arguments" { t.Errorf("unexpected error: %v", err) } }) t.Run("ConnectionError", func(t *testing.T) { cmd := &main.InfoCommand{} err := cmd.Run(context.Background(), []string{"-socket", "/nonexistent/socket.sock"}) if err == nil { t.Error("expected error for socket connection failure") } }) t.Run("CustomTimeout", func(t *testing.T) { cmd := &main.InfoCommand{} err := cmd.Run(context.Background(), []string{"-socket", "/nonexistent/socket.sock", "-timeout", "1"}) if err == nil { t.Error("expected error for socket connection failure") } }) t.Run("InvalidTimeoutZero", func(t *testing.T) { cmd := &main.InfoCommand{} err := cmd.Run(context.Background(), []string{"-timeout", "0"}) if err == nil { t.Error("expected error for zero timeout") } if err.Error() != "timeout must be greater than 0" { t.Errorf("unexpected error: %v", err) } }) t.Run("InvalidTimeoutNegative", func(t *testing.T) { cmd := &main.InfoCommand{} err := cmd.Run(context.Background(), []string{"-timeout", "-1"}) if err == nil { t.Error("expected error for negative timeout") } if err.Error() != "timeout must be greater than 0" { t.Errorf("unexpected error: %v", err) } }) t.Run("Success", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false if err := store.Open(context.Background()); err != nil { t.Fatal(err) } defer store.Close(context.Background()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) server.Version = "v1.0.0-test" if err := server.Start(); err != nil { t.Fatal(err) } defer server.Close() cmd := &main.InfoCommand{} err := cmd.Run(context.Background(), []string{"-socket", server.SocketPath}) if err != nil { t.Errorf("unexpected error: %v", err) } }) t.Run("JSONOutput", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false if err := store.Open(context.Background()); err != nil { t.Fatal(err) } defer store.Close(context.Background()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) server.Version = "v1.0.0-test" if err := server.Start(); err != nil { t.Fatal(err) } defer server.Close() cmd := &main.InfoCommand{} err := cmd.Run(context.Background(), []string{"-socket", server.SocketPath, "-json"}) if err != nil { t.Errorf("unexpected error: %v", err) } }) } ================================================ FILE: cmd/litestream/list.go ================================================ package main import ( "context" "encoding/json" "flag" "fmt" "io" "net" "net/http" "time" "github.com/benbjohnson/litestream" ) // ListCommand represents the command to list all managed databases. type ListCommand struct{} // Run executes the list command. func (c *ListCommand) Run(_ context.Context, args []string) error { fs := flag.NewFlagSet("litestream-list", flag.ContinueOnError) socketPath := fs.String("socket", "/var/run/litestream.sock", "control socket path") timeout := fs.Int("timeout", 10, "timeout in seconds") jsonOutput := fs.Bool("json", false, "output raw JSON") fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } if fs.NArg() > 0 { return fmt.Errorf("too many arguments") } if *timeout <= 0 { return fmt.Errorf("timeout must be greater than 0") } clientTimeout := time.Duration(*timeout) * time.Second client := &http.Client{ Timeout: clientTimeout, Transport: &http.Transport{ DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { return net.DialTimeout("unix", *socketPath, clientTimeout) }, }, } resp, err := client.Get("http://localhost/list") if err != nil { return fmt.Errorf("failed to connect to control socket: %w", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("failed to read response: %w", err) } if resp.StatusCode != http.StatusOK { var errResp litestream.ErrorResponse if err := json.Unmarshal(body, &errResp); err == nil && errResp.Error != "" { return fmt.Errorf("list failed: %s", errResp.Error) } return fmt.Errorf("list failed: %s", string(body)) } var result litestream.ListResponse if err := json.Unmarshal(body, &result); err != nil { return fmt.Errorf("failed to parse response: %w", err) } if *jsonOutput { output, err := json.MarshalIndent(result, "", " ") if err != nil { return fmt.Errorf("failed to format response: %w", err) } fmt.Println(string(output)) } else { if len(result.Databases) == 0 { fmt.Println("No databases configured") } else { for _, db := range result.Databases { syncInfo := "never" if db.LastSyncAt != nil { syncInfo = db.LastSyncAt.Format(time.RFC3339) } fmt.Printf("%s [%s] (last sync: %s)\n", db.Path, db.Status, syncInfo) } } } return nil } // Usage prints the help text for the list command. func (c *ListCommand) Usage() { fmt.Println(` usage: litestream list [OPTIONS] List all managed databases from a running daemon. Options: -json Output raw JSON instead of human-readable text. -socket PATH Path to control socket (default: /var/run/litestream.sock). -timeout SECONDS Maximum time to wait in seconds (default: 10). `[1:]) } ================================================ FILE: cmd/litestream/list_test.go ================================================ package main_test import ( "context" "testing" "github.com/benbjohnson/litestream" main "github.com/benbjohnson/litestream/cmd/litestream" "github.com/benbjohnson/litestream/internal/testingutil" ) func TestListCommand_Run(t *testing.T) { t.Run("TooManyArguments", func(t *testing.T) { cmd := &main.ListCommand{} err := cmd.Run(context.Background(), []string{"extra-arg"}) if err == nil { t.Error("expected error for too many arguments") } if err.Error() != "too many arguments" { t.Errorf("unexpected error: %v", err) } }) t.Run("ConnectionError", func(t *testing.T) { cmd := &main.ListCommand{} err := cmd.Run(context.Background(), []string{"-socket", "/nonexistent/socket.sock"}) if err == nil { t.Error("expected error for socket connection failure") } }) t.Run("CustomTimeout", func(t *testing.T) { cmd := &main.ListCommand{} err := cmd.Run(context.Background(), []string{"-socket", "/nonexistent/socket.sock", "-timeout", "1"}) if err == nil { t.Error("expected error for socket connection failure") } }) t.Run("InvalidTimeoutZero", func(t *testing.T) { cmd := &main.ListCommand{} err := cmd.Run(context.Background(), []string{"-timeout", "0"}) if err == nil { t.Error("expected error for zero timeout") } if err.Error() != "timeout must be greater than 0" { t.Errorf("unexpected error: %v", err) } }) t.Run("InvalidTimeoutNegative", func(t *testing.T) { cmd := &main.ListCommand{} err := cmd.Run(context.Background(), []string{"-timeout", "-1"}) if err == nil { t.Error("expected error for negative timeout") } if err.Error() != "timeout must be greater than 0" { t.Errorf("unexpected error: %v", err) } }) t.Run("Success", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false if err := store.Open(context.Background()); err != nil { t.Fatal(err) } defer store.Close(context.Background()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) if err := server.Start(); err != nil { t.Fatal(err) } defer server.Close() cmd := &main.ListCommand{} err := cmd.Run(context.Background(), []string{"-socket", server.SocketPath}) if err != nil { t.Errorf("unexpected error: %v", err) } }) t.Run("SuccessEmpty", func(t *testing.T) { store := litestream.NewStore(nil, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false if err := store.Open(context.Background()); err != nil { t.Fatal(err) } defer store.Close(context.Background()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) if err := server.Start(); err != nil { t.Fatal(err) } defer server.Close() cmd := &main.ListCommand{} err := cmd.Run(context.Background(), []string{"-socket", server.SocketPath}) if err != nil { t.Errorf("unexpected error: %v", err) } }) t.Run("JSONOutput", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false if err := store.Open(context.Background()); err != nil { t.Fatal(err) } defer store.Close(context.Background()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) if err := server.Start(); err != nil { t.Fatal(err) } defer server.Close() cmd := &main.ListCommand{} err := cmd.Run(context.Background(), []string{"-socket", server.SocketPath, "-json"}) if err != nil { t.Errorf("unexpected error: %v", err) } }) } ================================================ FILE: cmd/litestream/ltx.go ================================================ package main import ( "context" "flag" "fmt" "os" "text/tabwriter" "time" "github.com/benbjohnson/litestream" ) // LTXCommand represents a command to list LTX files for a database. type LTXCommand struct{} // Run executes the command. func (c *LTXCommand) Run(ctx context.Context, args []string) (err error) { fs := flag.NewFlagSet("litestream-ltx", flag.ContinueOnError) configPath, noExpandEnv := registerConfigFlag(fs) var level levelVar fs.Var(&level, "level", "compaction level (0-9 or \"all\")") fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } else if fs.NArg() == 0 || fs.Arg(0) == "" { return fmt.Errorf("database path required") } else if fs.NArg() > 1 { return fmt.Errorf("too many arguments") } var r *litestream.Replica if litestream.IsURL(fs.Arg(0)) { if *configPath != "" { return fmt.Errorf("cannot specify a replica URL and the -config flag") } if r, err = NewReplicaFromConfig(&ReplicaConfig{URL: fs.Arg(0)}, nil); err != nil { return err } initLog(os.Stdout, "INFO", "text") } else { if *configPath == "" { *configPath = DefaultConfigPath() } // Load configuration. config, err := ReadConfigFile(*configPath, !*noExpandEnv) if err != nil { return err } // Lookup database from configuration file by path. path, err := expand(fs.Arg(0)) if err != nil { return err } dbc := config.DBConfig(path) if dbc == nil { return fmt.Errorf("database not found in config: %s", path) } db, err := NewDBFromConfig(dbc) if err != nil { return err } else if db.Replica == nil { return fmt.Errorf("database has no replica") } r = db.Replica } // List LTX files. w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0) defer w.Flush() fmt.Fprintln(w, "level\tmin_txid\tmax_txid\tsize\tcreated") // Determine which levels to iterate. var levels []int if int(level) == levelAll { for lvl := 0; lvl <= litestream.SnapshotLevel; lvl++ { levels = append(levels, lvl) } } else { levels = []int{int(level)} } for _, lvl := range levels { itr, err := r.Client.LTXFiles(ctx, lvl, 0, false) if err != nil { return err } for itr.Next() { info := itr.Item() fmt.Fprintf(w, "%d\t%s\t%s\t%d\t%s\n", lvl, info.MinTXID, info.MaxTXID, info.Size, info.CreatedAt.Format(time.RFC3339), ) } if err := itr.Close(); err != nil { return err } } return nil } // Usage prints the help screen to STDOUT. func (c *LTXCommand) Usage() { fmt.Printf(` The ltx command lists all LTX files available for a database. Usage: litestream ltx [arguments] DB_PATH litestream ltx [arguments] REPLICA_URL Arguments: -config PATH Specifies the configuration file. Defaults to %s -no-expand-env Disables environment variable expansion in configuration file. -replica NAME Optional, filter by a specific replica. -level LEVEL Compaction level to list (0-9 or "all"). Defaults to 0. Examples: # List all LTX files for a database. $ litestream ltx /path/to/db # List all LTX files on S3 $ litestream ltx -replica s3 /path/to/db # List all LTX files for replica URL. $ litestream ltx s3://mybkt/db # List LTX files at snapshot level (level 9). $ litestream ltx -level 9 /path/to/db # List LTX files across all compaction levels. $ litestream ltx -level all /path/to/db `[1:], DefaultConfigPath(), ) } ================================================ FILE: cmd/litestream/ltx_test.go ================================================ package main import ( "testing" "github.com/superfly/ltx" "github.com/benbjohnson/litestream" ) func TestTXIDVarParsing(t *testing.T) { tests := []struct { name string input string want ltx.TXID wantErr bool }{ { name: "valid hex string", input: "0000000000000002", want: ltx.TXID(2), }, { name: "valid hex string with letters", input: "00000000000000ff", want: ltx.TXID(255), }, { name: "uppercase hex", input: "00000000000000FF", want: ltx.TXID(255), }, { name: "invalid - too short", input: "ff", wantErr: true, }, { name: "invalid - too long", input: "00000000000000001", wantErr: true, }, { name: "invalid - non-hex characters", input: "000000000000000g", wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var v txidVar err := v.Set(tt.input) if tt.wantErr { if err == nil { t.Errorf("Set(%q) = nil, want error", tt.input) } return } if err != nil { t.Errorf("Set(%q) error = %v, want nil", tt.input, err) return } if ltx.TXID(v) != tt.want { t.Errorf("Set(%q) = %v, want %v", tt.input, ltx.TXID(v), tt.want) } }) } } func TestTXIDVarString(t *testing.T) { tests := []struct { name string value ltx.TXID want string }{ { name: "zero", value: ltx.TXID(0), want: "0000000000000000", }, { name: "small number", value: ltx.TXID(2), want: "0000000000000002", }, { name: "larger number", value: ltx.TXID(255), want: "00000000000000ff", }, { name: "max value", value: ltx.TXID(^uint64(0)), want: "ffffffffffffffff", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { v := txidVar(tt.value) if got := v.String(); got != tt.want { t.Errorf("String() = %q, want %q", got, tt.want) } }) } } func TestLevelVarParsing(t *testing.T) { tests := []struct { name string input string want int wantErr bool }{ { name: "level 0", input: "0", want: 0, }, { name: "level 5", input: "5", want: 5, }, { name: "level 9 (snapshot level)", input: "9", want: litestream.SnapshotLevel, }, { name: "all levels", input: "all", want: levelAll, }, { name: "invalid - negative", input: "-1", wantErr: true, }, { name: "invalid - too high", input: "10", wantErr: true, }, { name: "invalid - non-numeric", input: "abc", wantErr: true, }, { name: "invalid - empty", input: "", wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var v levelVar err := v.Set(tt.input) if tt.wantErr { if err == nil { t.Errorf("Set(%q) = nil, want error", tt.input) } return } if err != nil { t.Errorf("Set(%q) error = %v, want nil", tt.input, err) return } if int(v) != tt.want { t.Errorf("Set(%q) = %v, want %v", tt.input, int(v), tt.want) } }) } } func TestLevelVarString(t *testing.T) { tests := []struct { name string value int want string }{ { name: "level 0", value: 0, want: "0", }, { name: "level 9", value: 9, want: "9", }, { name: "all levels", value: levelAll, want: "all", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { v := levelVar(tt.value) if got := v.String(); got != tt.want { t.Errorf("String() = %q, want %q", got, tt.want) } }) } } ================================================ FILE: cmd/litestream/main.go ================================================ package main import ( "context" "errors" "flag" "fmt" "io" "log/slog" "math" "net/url" "os" "os/user" "path" "path/filepath" "strconv" "strings" "time" "github.com/dustin/go-humanize" "github.com/superfly/ltx" _ "golang.org/x/crypto/x509roots/fallback" "gopkg.in/yaml.v2" _ "modernc.org/sqlite" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/abs" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/gs" "github.com/benbjohnson/litestream/internal" "github.com/benbjohnson/litestream/nats" "github.com/benbjohnson/litestream/oss" "github.com/benbjohnson/litestream/s3" "github.com/benbjohnson/litestream/sftp" "github.com/benbjohnson/litestream/webdav" ) // Build information. var ( Version = "(development build)" ) // errStop is a terminal error for indicating program should quit. var errStop = errors.New("stop") // Sentinel errors for configuration validation var ( ErrInvalidSnapshotInterval = errors.New("snapshot interval must be greater than 0") ErrInvalidSnapshotRetention = errors.New("snapshot retention must be greater than 0") ErrInvalidCompactionInterval = errors.New("compaction interval must be greater than 0") ErrInvalidSyncInterval = errors.New("sync interval must be greater than 0") ErrInvalidL0Retention = errors.New("l0 retention must be greater than 0") ErrInvalidL0RetentionCheckInterval = errors.New("l0 retention check interval must be greater than 0") ErrInvalidShutdownSyncTimeout = errors.New("shutdown-sync-timeout must be >= 0") ErrInvalidShutdownSyncInterval = errors.New("shutdown sync interval must be greater than 0") ErrInvalidHeartbeatURL = errors.New("heartbeat URL must be a valid HTTP or HTTPS URL") ErrInvalidHeartbeatInterval = errors.New("heartbeat interval must be at least 1 minute") ErrConfigFileNotFound = errors.New("config file not found") ) // ConfigValidationError wraps a validation error with additional context type ConfigValidationError struct { Err error Field string Value interface{} } func (e *ConfigValidationError) Error() string { if e.Value != nil { return fmt.Sprintf("%s: %v (got %v)", e.Field, e.Err, e.Value) } return fmt.Sprintf("%s: %v", e.Field, e.Err) } func (e *ConfigValidationError) Unwrap() error { return e.Err } func main() { initLog(os.Stdout, "INFO", "text") m := NewMain() if err := m.Run(context.Background(), os.Args[1:]); errors.Is(err, flag.ErrHelp) || errors.Is(err, errStop) { os.Exit(1) } else if err != nil { slog.Error("failed to run", "error", err) os.Exit(1) } } // Main represents the main program execution. type Main struct{} // NewMain returns a new instance of Main. func NewMain() *Main { return &Main{} } // Run executes the program. func (m *Main) Run(ctx context.Context, args []string) (err error) { // Execute replication command if running as a Windows service. if isService, err := isWindowsService(); err != nil { return err } else if isService { return runWindowsService(ctx) } // Copy "LITESTEAM" environment credentials. applyLitestreamEnv() // Extract command name. var cmd string if len(args) > 0 { cmd, args = args[0], args[1:] } switch cmd { case "databases": return (&DatabasesCommand{}).Run(ctx, args) case "replicate": c := NewReplicateCommand() if err := c.ParseFlags(ctx, args); err != nil { return err } // Setup signal handler. signalCh := signalChan() // Create done channel for interrupt during shutdown. It will be // closed when a second signal arrives, allowing each database's // retry loop to observe the interrupt. done := make(chan struct{}) c.SetDone(done) if err := c.Run(ctx); err != nil { return err } // Wait for signal to stop program. select { case err = <-c.execCh: if c.cmd != nil { slog.Info("subprocess exited, litestream shutting down") } else { slog.Info("replication complete, litestream shutting down") } case sig := <-signalCh: slog.Info("signal received, litestream shutting down", "signal", sig) if c.cmd != nil { slog.Info("sending signal to exec process") if err := c.cmd.Process.Signal(sig); err != nil { return fmt.Errorf("cannot signal exec process: %w", err) } slog.Info("waiting for exec process to close") if err := <-c.execCh; err != nil && !strings.HasPrefix(err.Error(), "signal:") { return fmt.Errorf("cannot wait for exec process: %w", err) } } // Listen for a second signal to close done and interrupt retry loops. go func() { <-signalCh close(done) }() } // Gracefully close. if e := c.Close(ctx); e != nil && err == nil { err = e } slog.Info("litestream shut down") return err case "start": return (&StartCommand{}).Run(ctx, args) case "stop": return (&StopCommand{}).Run(ctx, args) case "register": return (&RegisterCommand{}).Run(ctx, args) case "unregister": return (&UnregisterCommand{}).Run(ctx, args) case "reset": return (&ResetCommand{}).Run(ctx, args) case "restore": return (&RestoreCommand{}).Run(ctx, args) case "status": return (&StatusCommand{}).Run(ctx, args) case "sync": return (&SyncCommand{}).Run(ctx, args) case "list": return (&ListCommand{}).Run(ctx, args) case "info": return (&InfoCommand{}).Run(ctx, args) case "version": return (&VersionCommand{}).Run(ctx, args) case "ltx": return (<XCommand{}).Run(ctx, args) case "wal": // Deprecated: Keep for backward compatibility fmt.Fprintln(os.Stderr, "Warning: 'wal' command is deprecated, please use 'ltx' instead") return (<XCommand{}).Run(ctx, args) default: if cmd == "" || cmd == "help" || strings.HasPrefix(cmd, "-") { m.Usage() return flag.ErrHelp } return fmt.Errorf("litestream %s: unknown command", cmd) } } // Usage prints the help screen to STDOUT. func (m *Main) Usage() { fmt.Println(` litestream is a tool for replicating SQLite databases. Usage: litestream [arguments] The commands are: databases list databases specified in config file info show daemon information list list all managed databases ltx list available LTX files for a database register register a database for replication replicate runs a server to replicate databases reset reset local state for a database restore recovers database backup from a replica start start replication for a database status display replication status for databases stop stop replication for a database sync force an immediate sync for a database unregister unregister a database from replication version prints the binary version `[1:]) } // Config represents a configuration file for the litestream daemon. type Config struct { // Global replica settings that serve as defaults for all replicas ReplicaSettings `yaml:",inline"` // Bind address for serving metrics. Addr string `yaml:"addr"` // Socket configuration for control commands. Socket litestream.SocketConfig `yaml:"socket"` // List of stages in a multi-level compaction. // Only includes L1 through the last non-snapshot level. Levels []*CompactionLevelConfig `yaml:"levels"` // Snapshot configuration Snapshot SnapshotConfig `yaml:"snapshot"` // Validation configuration Validation ValidationConfig `yaml:"validation"` // L0 retention settings L0Retention *time.Duration `yaml:"l0-retention"` L0RetentionCheckInterval *time.Duration `yaml:"l0-retention-check-interval"` // Verify TXID consistency at destination level after each compaction. // When enabled, logs warnings if gaps or overlaps are detected. VerifyCompaction bool `yaml:"verify-compaction"` // Retention configuration Retention RetentionConfig `yaml:"retention"` // Heartbeat settings (global defaults) HeartbeatURL string `yaml:"heartbeat-url"` HeartbeatInterval *time.Duration `yaml:"heartbeat-interval"` // List of databases to manage. DBs []*DBConfig `yaml:"dbs"` // Subcommand to execute during replication. // Litestream will shutdown when subcommand exits. Exec string `yaml:"exec"` // Logging Logging LoggingConfig `yaml:"logging"` // MCP server options MCPAddr string `yaml:"mcp-addr"` // Shutdown sync retry settings ShutdownSyncTimeout *time.Duration `yaml:"shutdown-sync-timeout"` ShutdownSyncInterval *time.Duration `yaml:"shutdown-sync-interval"` // Path to the config file // This is only used internally to pass the config path to the MCP tool ConfigPath string `yaml:"-"` } // SnapshotConfig configures snapshots. type SnapshotConfig struct { Interval *time.Duration `yaml:"interval"` Retention *time.Duration `yaml:"retention"` } // RetentionConfig configures retention enforcement behavior. type RetentionConfig struct { Enabled *bool `yaml:"enabled"` } // ValidationConfig configures periodic validation checks. type ValidationConfig struct { Interval *time.Duration `yaml:"interval"` } // LoggingConfig configures logging. type LoggingConfig struct { Level string `yaml:"level"` Type string `yaml:"type"` Stderr bool `yaml:"stderr"` } // propagateGlobalSettings copies global replica settings to individual replica configs. func (c *Config) propagateGlobalSettings() { for _, dbc := range c.DBs { // Handle both old-style 'replicas' and new-style 'replica' if dbc.Replica != nil { dbc.Replica.SetDefaults(&c.ReplicaSettings) } for _, rc := range dbc.Replicas { rc.SetDefaults(&c.ReplicaSettings) } } } // DefaultConfig returns a new instance of Config with defaults set. func DefaultConfig() Config { defaultSnapshotInterval := 24 * time.Hour defaultSnapshotRetention := 24 * time.Hour defaultL0Retention := litestream.DefaultL0Retention defaultL0RetentionCheckInterval := litestream.DefaultL0RetentionCheckInterval defaultShutdownSyncTimeout := litestream.DefaultShutdownSyncTimeout defaultShutdownSyncInterval := litestream.DefaultShutdownSyncInterval return Config{ Levels: []*CompactionLevelConfig{ {Interval: litestream.DefaultCompactionLevels[1].Interval}, {Interval: litestream.DefaultCompactionLevels[2].Interval}, {Interval: litestream.DefaultCompactionLevels[3].Interval}, }, Snapshot: SnapshotConfig{ Interval: &defaultSnapshotInterval, Retention: &defaultSnapshotRetention, }, Socket: litestream.DefaultSocketConfig(), L0Retention: &defaultL0Retention, L0RetentionCheckInterval: &defaultL0RetentionCheckInterval, ShutdownSyncTimeout: &defaultShutdownSyncTimeout, ShutdownSyncInterval: &defaultShutdownSyncInterval, } } // Validate returns an error if config contains invalid settings. func (c *Config) Validate() error { // Validate snapshot intervals if c.Snapshot.Interval != nil && *c.Snapshot.Interval <= 0 { return &ConfigValidationError{ Err: ErrInvalidSnapshotInterval, Field: "snapshot.interval", Value: *c.Snapshot.Interval, } } if c.Snapshot.Retention != nil && *c.Snapshot.Retention <= 0 { return &ConfigValidationError{ Err: ErrInvalidSnapshotRetention, Field: "snapshot.retention", Value: *c.Snapshot.Retention, } } if c.L0Retention != nil && *c.L0Retention <= 0 { return &ConfigValidationError{ Err: ErrInvalidL0Retention, Field: "l0-retention", Value: *c.L0Retention, } } if c.L0RetentionCheckInterval != nil && *c.L0RetentionCheckInterval <= 0 { return &ConfigValidationError{ Err: ErrInvalidL0RetentionCheckInterval, Field: "l0-retention-check-interval", Value: *c.L0RetentionCheckInterval, } } if c.ShutdownSyncTimeout != nil && *c.ShutdownSyncTimeout < 0 { return &ConfigValidationError{ Err: ErrInvalidShutdownSyncTimeout, Field: "shutdown-sync-timeout", Value: *c.ShutdownSyncTimeout, } } if c.ShutdownSyncInterval != nil && *c.ShutdownSyncInterval <= 0 { return &ConfigValidationError{ Err: ErrInvalidShutdownSyncInterval, Field: "shutdown-sync-interval", Value: *c.ShutdownSyncInterval, } } // Validate global heartbeat settings if c.HeartbeatURL != "" && !isValidHeartbeatURL(c.HeartbeatURL) { return &ConfigValidationError{ Err: ErrInvalidHeartbeatURL, Field: "heartbeat-url", Value: c.HeartbeatURL, } } if c.HeartbeatInterval != nil && *c.HeartbeatInterval < litestream.MinHeartbeatInterval { return &ConfigValidationError{ Err: ErrInvalidHeartbeatInterval, Field: "heartbeat-interval", Value: *c.HeartbeatInterval, } } // Validate compaction level intervals for i, level := range c.Levels { if level.Interval <= 0 { return &ConfigValidationError{ Err: ErrInvalidCompactionInterval, Field: fmt.Sprintf("levels[%d].interval", i), Value: level.Interval, } } } // Validate database configs for idx, db := range c.DBs { // Validate that either path or dir is specified, but not both if db.Path != "" && db.Dir != "" { return fmt.Errorf("database config #%d: cannot specify both 'path' and 'dir'", idx+1) } if db.Path == "" && db.Dir == "" { return fmt.Errorf("database config #%d: must specify either 'path' or 'dir'", idx+1) } // When using dir, pattern must be specified if db.Dir != "" && db.Pattern == "" { return fmt.Errorf("database config #%d: 'pattern' is required when using 'dir'", idx+1) } if db.Watch && db.Dir == "" { return fmt.Errorf("database config #%d: 'watch' can only be enabled with a directory", idx+1) } // Use path or dir for identifying the config in error messages dbIdentifier := db.Path if dbIdentifier == "" { dbIdentifier = db.Dir } // Validate sync intervals for replicas if db.Replica != nil && db.Replica.SyncInterval != nil && *db.Replica.SyncInterval <= 0 { return &ConfigValidationError{ Err: ErrInvalidSyncInterval, Field: fmt.Sprintf("dbs[%s].replica.sync-interval", dbIdentifier), Value: *db.Replica.SyncInterval, } } for i, replica := range db.Replicas { if replica.SyncInterval != nil && *replica.SyncInterval <= 0 { return &ConfigValidationError{ Err: ErrInvalidSyncInterval, Field: fmt.Sprintf("dbs[%s].replicas[%d].sync-interval", dbIdentifier, i), Value: *replica.SyncInterval, } } } } return nil } // CompactionLevels returns a full list of compaction levels include L0. func (c *Config) CompactionLevels() litestream.CompactionLevels { levels := litestream.CompactionLevels{ {Level: 0}, } for i, lvl := range c.Levels { levels = append(levels, &litestream.CompactionLevel{ Level: i + 1, Interval: lvl.Interval, }) } return levels } // DBConfig returns database configuration by path. func (c *Config) DBConfig(configPath string) *DBConfig { for _, dbConfig := range c.DBs { if dbConfig.Path == configPath { return dbConfig } } return nil } // OpenConfigFile opens a configuration file and returns a reader. // Expands the filename path if needed. func OpenConfigFile(filename string) (io.ReadCloser, error) { // Expand filename, if necessary. filename, err := expand(filename) if err != nil { return nil, err } // Open configuration file. f, err := os.Open(filename) if os.IsNotExist(err) { return nil, fmt.Errorf("%w: %s", ErrConfigFileNotFound, filename) } else if err != nil { return nil, err } return f, nil } // ReadConfigFile unmarshals config from filename. Expands path if needed. // If expandEnv is true then environment variables are expanded in the config. func ReadConfigFile(filename string, expandEnv bool) (Config, error) { f, err := OpenConfigFile(filename) if err != nil { return DefaultConfig(), err } defer f.Close() return ParseConfig(f, expandEnv) } // ParseConfig unmarshals config from a reader. // If expandEnv is true then environment variables are expanded in the config. func ParseConfig(r io.Reader, expandEnv bool) (_ Config, err error) { config := DefaultConfig() // Read configuration. buf, err := io.ReadAll(r) if err != nil { return config, err } // Expand environment variables, if enabled. if expandEnv { buf = []byte(os.Expand(string(buf), func(key string) string { if key == "PID" { return strconv.Itoa(os.Getpid()) } return os.Getenv(key) })) } // Save defaults before unmarshaling defaultSnapshotInterval := config.Snapshot.Interval defaultSnapshotRetention := config.Snapshot.Retention defaultL0Retention := config.L0Retention defaultL0RetentionCheckInterval := config.L0RetentionCheckInterval if err := yaml.Unmarshal(buf, &config); err != nil { return config, err } // Restore defaults if they were overwritten with nil by empty YAML sections if config.Snapshot.Interval == nil { config.Snapshot.Interval = defaultSnapshotInterval } if config.Snapshot.Retention == nil { config.Snapshot.Retention = defaultSnapshotRetention } if config.L0Retention == nil { config.L0Retention = defaultL0Retention } if config.L0RetentionCheckInterval == nil { config.L0RetentionCheckInterval = defaultL0RetentionCheckInterval } // Normalize paths. for _, dbConfig := range config.DBs { if dbConfig.Path == "" { continue } if dbConfig.Path, err = expand(dbConfig.Path); err != nil { return config, err } } // Propagate settings from global config to individual configs. config.propagateGlobalSettings() // Validate configuration if err := config.Validate(); err != nil { return config, err } // Configure logging. logOutput := os.Stdout if config.Logging.Stderr { logOutput = os.Stderr } if v := os.Getenv("LOG_LEVEL"); v != "" { config.Logging.Level = v } initLog(logOutput, config.Logging.Level, config.Logging.Type) return config, nil } // CompactionLevelConfig the configuration for a single level of compaction. type CompactionLevelConfig struct { Interval time.Duration `yaml:"interval"` } // DBConfig represents the configuration for a single database or directory of databases. type DBConfig struct { Path string `yaml:"path"` Dir string `yaml:"dir"` // Directory to scan for databases Pattern string `yaml:"pattern"` // File pattern to match (e.g., "*.db", "*.sqlite") Recursive bool `yaml:"recursive"` // Scan subdirectories recursively Watch bool `yaml:"watch"` // Enable directory monitoring for changes MetaPath *string `yaml:"meta-path"` MonitorInterval *time.Duration `yaml:"monitor-interval"` CheckpointInterval *time.Duration `yaml:"checkpoint-interval"` BusyTimeout *time.Duration `yaml:"busy-timeout"` MinCheckpointPageN *int `yaml:"min-checkpoint-page-count"` TruncatePageN *int `yaml:"truncate-page-n"` RestoreIfDBNotExists bool `yaml:"restore-if-db-not-exists"` Replica *ReplicaConfig `yaml:"replica"` Replicas []*ReplicaConfig `yaml:"replicas"` // Deprecated } // NewDBFromConfig instantiates a DB based on a configuration. func NewDBFromConfig(dbc *DBConfig) (*litestream.DB, error) { configPath, err := expand(dbc.Path) if err != nil { return nil, err } // Initialize database with given path. db := litestream.NewDB(configPath) // Override default database settings if specified in configuration. if dbc.MetaPath != nil { expandedMetaPath, err := expand(*dbc.MetaPath) if err != nil { return nil, fmt.Errorf("failed to expand meta path: %w", err) } dbc.MetaPath = &expandedMetaPath db.SetMetaPath(expandedMetaPath) } if dbc.MonitorInterval != nil { db.MonitorInterval = *dbc.MonitorInterval } if dbc.CheckpointInterval != nil { db.CheckpointInterval = *dbc.CheckpointInterval } if dbc.BusyTimeout != nil { db.BusyTimeout = *dbc.BusyTimeout } if dbc.MinCheckpointPageN != nil { db.MinCheckpointPageN = *dbc.MinCheckpointPageN } if dbc.TruncatePageN != nil { db.TruncatePageN = *dbc.TruncatePageN } // Instantiate and attach replica. // v0.3.x and before supported multiple replicas but that was dropped to // ensure there's a single remote data authority. switch { case dbc.Replica == nil && len(dbc.Replicas) == 0: return nil, fmt.Errorf("must specify replica for database") case dbc.Replica != nil && len(dbc.Replicas) > 0: return nil, fmt.Errorf("cannot specify 'replica' and 'replicas' on a database") case len(dbc.Replicas) > 1: return nil, fmt.Errorf("multiple replicas on a single database are no longer supported") } var rc *ReplicaConfig if dbc.Replica != nil { rc = dbc.Replica } else { rc = dbc.Replicas[0] } r, err := NewReplicaFromConfig(rc, db) if err != nil { return nil, err } db.Replica = r return db, nil } // NewDBsFromDirectoryConfig scans a directory and creates DB instances for all SQLite databases found. func NewDBsFromDirectoryConfig(dbc *DBConfig) ([]*litestream.DB, error) { if dbc.Dir == "" { return nil, fmt.Errorf("directory path is required for directory replication") } if dbc.Pattern == "" { return nil, fmt.Errorf("pattern is required for directory replication") } dirPath, err := expand(dbc.Dir) if err != nil { return nil, err } // Find all SQLite databases in the directory dbPaths, err := FindSQLiteDatabases(dirPath, dbc.Pattern, dbc.Recursive) if err != nil { return nil, fmt.Errorf("failed to scan directory %s: %w", dirPath, err) } if len(dbPaths) == 0 && !dbc.Watch { return nil, fmt.Errorf("no SQLite databases found in directory %s with pattern %s", dirPath, dbc.Pattern) } // Create DB instances for each found database var dbs []*litestream.DB metaPaths := make(map[string]string) for _, dbPath := range dbPaths { db, err := newDBFromDirectoryEntry(dbc, dirPath, dbPath) if err != nil { return nil, fmt.Errorf("failed to create DB for %s: %w", dbPath, err) } // Validate unique meta-path to prevent replication state corruption if mp := db.MetaPath(); mp != "" { if existingDB, exists := metaPaths[mp]; exists { return nil, fmt.Errorf("meta-path collision: databases %s and %s would share meta-path %s, causing replication state corruption", existingDB, dbPath, mp) } metaPaths[mp] = dbPath } dbs = append(dbs, db) } return dbs, nil } // newDBFromDirectoryEntry creates a DB instance for a database discovered via directory replication. func newDBFromDirectoryEntry(dbc *DBConfig, dirPath, dbPath string) (*litestream.DB, error) { // Calculate relative path from directory root relPath, err := filepath.Rel(dirPath, dbPath) if err != nil { return nil, fmt.Errorf("failed to calculate relative path for %s: %w", dbPath, err) } // Create a copy of the config for the discovered database dbConfigCopy := *dbc dbConfigCopy.Path = dbPath dbConfigCopy.Dir = "" // Clear dir field for individual DB dbConfigCopy.Pattern = "" // Clear pattern field dbConfigCopy.Recursive = false // Clear recursive flag dbConfigCopy.Watch = false // Individual DBs do not watch directories // Ensure every database discovered beneath a directory receives a unique // metadata path. Without this, all databases share the same meta-path and // clobber each other's replication state. if dbc.MetaPath != nil { baseMetaPath, err := expand(*dbc.MetaPath) if err != nil { return nil, fmt.Errorf("failed to expand meta path for %s: %w", dbPath, err) } metaPathCopy := deriveMetaPathForDirectoryEntry(baseMetaPath, relPath) dbConfigCopy.MetaPath = &metaPathCopy } // Deep copy replica config and make path unique per database. // This prevents all databases from writing to the same replica path. if dbc.Replica != nil { replicaCopy, err := cloneReplicaConfigWithRelativePath(dbc.Replica, relPath) if err != nil { return nil, fmt.Errorf("failed to configure replica for %s: %w", dbPath, err) } dbConfigCopy.Replica = replicaCopy } // Also handle deprecated 'replicas' array field. if len(dbc.Replicas) > 0 { dbConfigCopy.Replicas = make([]*ReplicaConfig, len(dbc.Replicas)) for i, replica := range dbc.Replicas { replicaCopy, err := cloneReplicaConfigWithRelativePath(replica, relPath) if err != nil { return nil, fmt.Errorf("failed to configure replica %d for %s: %w", i, dbPath, err) } dbConfigCopy.Replicas[i] = replicaCopy } } return NewDBFromConfig(&dbConfigCopy) } // cloneReplicaConfigWithRelativePath returns a copy of the replica configuration with the // database-relative path appended to either the replica path or URL, depending on how the // replica was configured. func cloneReplicaConfigWithRelativePath(base *ReplicaConfig, relPath string) (*ReplicaConfig, error) { if base == nil { return nil, nil } replicaCopy := *base relPath = filepath.ToSlash(relPath) if relPath == "" || relPath == "." { return &replicaCopy, nil } if replicaCopy.URL != "" { u, err := url.Parse(replicaCopy.URL) if err != nil { return nil, fmt.Errorf("parse replica url: %w", err) } appendRelativePathToURL(u, relPath) replicaCopy.URL = u.String() return &replicaCopy, nil } switch base.ReplicaType() { case "file": relOSPath := filepath.FromSlash(relPath) if replicaCopy.Path != "" { replicaCopy.Path = filepath.Join(replicaCopy.Path, relOSPath) } else { replicaCopy.Path = relOSPath } default: // Normalize to forward slashes for cloud/object storage backends. basePath := filepath.ToSlash(replicaCopy.Path) if basePath != "" { replicaCopy.Path = path.Join(basePath, relPath) } else { replicaCopy.Path = relPath } } return &replicaCopy, nil } // deriveMetaPathForDirectoryEntry returns a unique metadata directory for a // database discovered through directory replication by appending the database's // relative path and the standard Litestream suffix to the configured base path. func deriveMetaPathForDirectoryEntry(basePath, relPath string) string { relPath = filepath.Clean(relPath) if relPath == "." || relPath == "" { return basePath } relDir, relFile := filepath.Split(relPath) if relFile == "" || relFile == "." { return filepath.Join(basePath, relPath) } metaDirName := "." + relFile + litestream.MetaDirSuffix return filepath.Join(basePath, relDir, metaDirName) } // appendRelativePathToURL appends relPath to the URL's path component, ensuring // the result remains rooted and uses forward slashes. func appendRelativePathToURL(u *url.URL, relPath string) { cleanRel := strings.TrimPrefix(relPath, "/") if cleanRel == "" || cleanRel == "." { return } basePath := u.Path var joined string if basePath == "" { joined = cleanRel } else { joined = path.Join(basePath, cleanRel) } joined = "/" + strings.TrimPrefix(joined, "/") u.Path = joined } // FindSQLiteDatabases recursively finds all SQLite database files in a directory. // Exported for testing. func FindSQLiteDatabases(dir string, pattern string, recursive bool) ([]string, error) { var dbPaths []string err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if err != nil { return err } // Skip directories unless recursive if info.IsDir() { if !recursive && path != dir { return filepath.SkipDir } return nil } // Check if file matches pattern matched, err := filepath.Match(pattern, filepath.Base(path)) if err != nil { return err } if !matched { return nil } // Check if it's a SQLite database if IsSQLiteDatabase(path) { dbPaths = append(dbPaths, path) } return nil }) return dbPaths, err } // IsSQLiteDatabase checks if a file is a SQLite database by reading its header. // Exported for testing. func IsSQLiteDatabase(path string) bool { file, err := os.Open(path) if err != nil { return false } defer file.Close() // SQLite files start with "SQLite format 3\x00" header := make([]byte, 16) if _, err := file.Read(header); err != nil { return false } return string(header) == "SQLite format 3\x00" } // ByteSize is a custom type for parsing byte sizes from YAML. // It supports both SI units (KB, MB, GB using base 1000) and IEC units // (KiB, MiB, GiB using base 1024) as well as short forms (K, M, G). type ByteSize int64 // UnmarshalYAML implements yaml.Unmarshaler for ByteSize. func (b *ByteSize) UnmarshalYAML(unmarshal func(interface{}) error) error { var s string if err := unmarshal(&s); err != nil { return err } size, err := ParseByteSize(s) if err != nil { return err } *b = ByteSize(size) return nil } // ParseByteSize parses a byte size string using github.com/dustin/go-humanize. // Supports both SI units (KB=1000, MB=1000², etc.) and IEC units (KiB=1024, MiB=1024², etc.). // Examples: "1MB", "5MiB", "1.5GB", "100B", "1024KB" func ParseByteSize(s string) (int64, error) { s = strings.TrimSpace(s) if s == "" { return 0, fmt.Errorf("empty size string") } // Use go-humanize to parse the byte size string bytes, err := humanize.ParseBytes(s) if err != nil { return 0, fmt.Errorf("invalid size format: %w", err) } // Check that the value fits in int64 if bytes > math.MaxInt64 { return 0, fmt.Errorf("size %d exceeds maximum allowed value (%d)", bytes, int64(math.MaxInt64)) } return int64(bytes), nil } // ReplicaSettings contains settings shared across replica configurations. // These can be set globally in Config or per-replica in ReplicaConfig. type ReplicaSettings struct { SyncInterval *time.Duration `yaml:"sync-interval"` ValidationInterval *time.Duration `yaml:"validation-interval"` // If true, automatically reset local state when LTX errors are detected. // This allows recovery from corrupted/missing LTX files by forcing a fresh sync. // Disabled by default to prevent silent data loss scenarios. AutoRecover *bool `yaml:"auto-recover"` // S3 settings AccessKeyID string `yaml:"access-key-id"` SecretAccessKey string `yaml:"secret-access-key"` Region string `yaml:"region"` Bucket string `yaml:"bucket"` Endpoint string `yaml:"endpoint"` ForcePathStyle *bool `yaml:"force-path-style"` SignPayload *bool `yaml:"sign-payload"` RequireContentMD5 *bool `yaml:"require-content-md5"` SkipVerify bool `yaml:"skip-verify"` PartSize *ByteSize `yaml:"part-size"` Concurrency *int `yaml:"concurrency"` // S3 Server-Side Encryption (SSE-C: Customer-provided keys) SSECustomerAlgorithm string `yaml:"sse-customer-algorithm"` SSECustomerKey string `yaml:"sse-customer-key"` SSECustomerKeyPath string `yaml:"sse-customer-key-path"` // S3 Server-Side Encryption (SSE-KMS: AWS Key Management Service) SSEKMSKeyID string `yaml:"sse-kms-key-id"` // ABS settings AccountName string `yaml:"account-name"` AccountKey string `yaml:"account-key"` SASToken string `yaml:"sas-token"` // SFTP settings Host string `yaml:"host"` User string `yaml:"user"` Password string `yaml:"password"` KeyPath string `yaml:"key-path"` ConcurrentWrites *bool `yaml:"concurrent-writes"` HostKey string `yaml:"host-key"` // WebDAV settings WebDAVURL string `yaml:"webdav-url"` WebDAVUsername string `yaml:"webdav-username"` WebDAVPassword string `yaml:"webdav-password"` // NATS settings JWT string `yaml:"jwt"` Seed string `yaml:"seed"` Creds string `yaml:"creds"` NKey string `yaml:"nkey"` Username string `yaml:"username"` Token string `yaml:"token"` TLS bool `yaml:"tls"` RootCAs []string `yaml:"root-cas"` ClientCert string `yaml:"client-cert"` ClientKey string `yaml:"client-key"` MaxReconnects *int `yaml:"max-reconnects"` ReconnectWait *time.Duration `yaml:"reconnect-wait"` Timeout *time.Duration `yaml:"timeout"` // Encryption identities and recipients Age struct { Identities []string `yaml:"identities"` Recipients []string `yaml:"recipients"` } `yaml:"age"` } // SetDefaults merges default settings from src into the current ReplicaSettings. // Individual settings override defaults when already set. func (rs *ReplicaSettings) SetDefaults(src *ReplicaSettings) { if src == nil { return } // Timing settings if rs.SyncInterval == nil && src.SyncInterval != nil { rs.SyncInterval = src.SyncInterval } if rs.ValidationInterval == nil && src.ValidationInterval != nil { rs.ValidationInterval = src.ValidationInterval } // Recovery settings if rs.AutoRecover == nil && src.AutoRecover != nil { rs.AutoRecover = src.AutoRecover } // S3 settings if rs.AccessKeyID == "" { rs.AccessKeyID = src.AccessKeyID } if rs.SecretAccessKey == "" { rs.SecretAccessKey = src.SecretAccessKey } if rs.Region == "" { rs.Region = src.Region } if rs.Bucket == "" { rs.Bucket = src.Bucket } if rs.Endpoint == "" { rs.Endpoint = src.Endpoint } if rs.ForcePathStyle == nil { rs.ForcePathStyle = src.ForcePathStyle } if rs.SignPayload == nil { rs.SignPayload = src.SignPayload } if rs.RequireContentMD5 == nil { rs.RequireContentMD5 = src.RequireContentMD5 } if src.SkipVerify { rs.SkipVerify = true } // S3 SSE settings if rs.SSECustomerAlgorithm == "" { rs.SSECustomerAlgorithm = src.SSECustomerAlgorithm } if rs.SSECustomerKey == "" { rs.SSECustomerKey = src.SSECustomerKey } if rs.SSECustomerKeyPath == "" { rs.SSECustomerKeyPath = src.SSECustomerKeyPath } if rs.SSEKMSKeyID == "" { rs.SSEKMSKeyID = src.SSEKMSKeyID } // ABS settings if rs.AccountName == "" { rs.AccountName = src.AccountName } if rs.AccountKey == "" { rs.AccountKey = src.AccountKey } if rs.SASToken == "" { rs.SASToken = src.SASToken } // SFTP settings if rs.Host == "" { rs.Host = src.Host } if rs.User == "" { rs.User = src.User } if rs.Password == "" { rs.Password = src.Password } if rs.KeyPath == "" { rs.KeyPath = src.KeyPath } if rs.ConcurrentWrites == nil { rs.ConcurrentWrites = src.ConcurrentWrites } // NATS settings if rs.JWT == "" { rs.JWT = src.JWT } if rs.Seed == "" { rs.Seed = src.Seed } if rs.Creds == "" { rs.Creds = src.Creds } if rs.NKey == "" { rs.NKey = src.NKey } if rs.Username == "" { rs.Username = src.Username } if rs.Token == "" { rs.Token = src.Token } if !rs.TLS { rs.TLS = src.TLS } if len(rs.RootCAs) == 0 { rs.RootCAs = src.RootCAs } if rs.ClientCert == "" { rs.ClientCert = src.ClientCert } if rs.ClientKey == "" { rs.ClientKey = src.ClientKey } if rs.MaxReconnects == nil { rs.MaxReconnects = src.MaxReconnects } if rs.ReconnectWait == nil { rs.ReconnectWait = src.ReconnectWait } if rs.Timeout == nil { rs.Timeout = src.Timeout } // Age encryption settings if len(rs.Age.Identities) == 0 { rs.Age.Identities = src.Age.Identities } if len(rs.Age.Recipients) == 0 { rs.Age.Recipients = src.Age.Recipients } } // ReplicaConfig represents the configuration for a single replica in a database. type ReplicaConfig struct { ReplicaSettings `yaml:",inline"` Type string `yaml:"type"` // "file", "s3" Name string `yaml:"name"` // Deprecated Path string `yaml:"path"` URL string `yaml:"url"` } // NewReplicaFromConfig instantiates a replica for a DB based on a config. func NewReplicaFromConfig(c *ReplicaConfig, db *litestream.DB) (_ *litestream.Replica, err error) { // Ensure user did not specify URL in path. if litestream.IsURL(c.Path) { return nil, fmt.Errorf("replica path cannot be a url, please use the 'url' field instead: %s", c.Path) } // Reject age encryption configuration as it's currently non-functional. // Age encryption support was removed during the LTX storage layer refactor // and has not been reimplemented. Accepting this config would silently // write plaintext data to remote storage instead of encrypted data. // See: https://github.com/benbjohnson/litestream/issues/790 if len(c.Age.Identities) > 0 || len(c.Age.Recipients) > 0 { return nil, fmt.Errorf("age encryption is not currently supported, if you need encryption please revert back to Litestream v0.3.x") } // Build replica. r := litestream.NewReplica(db) if v := c.SyncInterval; v != nil { r.SyncInterval = *v } if v := c.AutoRecover; v != nil { r.AutoRecoverEnabled = *v } // Build and set client on replica. switch c.ReplicaType() { case "file": if r.Client, err = newFileReplicaClientFromConfig(c, r); err != nil { return nil, err } case "s3": if r.Client, err = NewS3ReplicaClientFromConfig(c, r); err != nil { return nil, err } case "gs": if r.Client, err = newGSReplicaClientFromConfig(c, r); err != nil { return nil, err } case "abs": if r.Client, err = newABSReplicaClientFromConfig(c, r); err != nil { return nil, err } case "sftp": if r.Client, err = newSFTPReplicaClientFromConfig(c, r); err != nil { return nil, err } case "webdav": if r.Client, err = newWebDAVReplicaClientFromConfig(c, r); err != nil { return nil, err } case "nats": if r.Client, err = newNATSReplicaClientFromConfig(c, r); err != nil { return nil, err } case "oss": if r.Client, err = newOSSReplicaClientFromConfig(c, r); err != nil { return nil, err } default: return nil, fmt.Errorf("unknown replica type in config: %q", c.Type) } r.Client.SetLogger(r.Logger()) return r, nil } // newFileReplicaClientFromConfig returns a new instance of file.ReplicaClient built from config. func newFileReplicaClientFromConfig(c *ReplicaConfig, r *litestream.Replica) (_ *file.ReplicaClient, err error) { // Ensure URL & path are not both specified. if c.URL != "" && c.Path != "" { return nil, fmt.Errorf("cannot specify url & path for file replica") } // Parse configPath from URL, if specified. configPath := c.Path if c.URL != "" { if _, _, configPath, err = litestream.ParseReplicaURL(c.URL); err != nil { return nil, err } } // Ensure path is set explicitly or derived from URL field. if configPath == "" { return nil, fmt.Errorf("file replica path required") } // Expand home prefix and return absolute path. if configPath, err = expand(configPath); err != nil { return nil, err } // Instantiate replica and apply time fields, if set. client := file.NewReplicaClient(configPath) client.Replica = r return client, nil } // NewS3ReplicaClientFromConfig returns a new instance of s3.ReplicaClient built from config. // Exported for testing. func NewS3ReplicaClientFromConfig(c *ReplicaConfig, _ *litestream.Replica) (_ *s3.ReplicaClient, err error) { // Ensure URL & constituent parts are not both specified. if c.URL != "" && c.Path != "" { return nil, fmt.Errorf("cannot specify url & path for s3 replica") } else if c.URL != "" && c.Bucket != "" { return nil, fmt.Errorf("cannot specify url & bucket for s3 replica") } bucket, configPath := c.Bucket, c.Path region, endpoint, skipVerify := c.Region, c.Endpoint, c.SkipVerify signSetting := newBoolSetting(true) if v := c.SignPayload; v != nil { signSetting.Set(*v) } requireSetting := newBoolSetting(true) if v := c.RequireContentMD5; v != nil { requireSetting.Set(*v) } // Use path style if an endpoint is explicitly set. This works because the // only service to not use path style is AWS which does not use an endpoint. forcePathStyle := (endpoint != "") if v := c.ForcePathStyle; v != nil { forcePathStyle = *v } // Apply settings from URL, if specified. var ( endpointWasSet bool usignPayload bool usignPayloadSet bool urequireContentMD5 bool urequireContentMD5Set bool ) if endpoint != "" { endpointWasSet = true } if c.URL != "" { _, host, upath, query, _, err := litestream.ParseReplicaURLWithQuery(c.URL) if err != nil { return nil, err } var ( ubucket string uregion string uendpoint string uforcePathStyle bool ) if strings.HasPrefix(host, "arn:") { ubucket = host uregion = litestream.RegionFromS3ARN(host) } else { ubucket, uregion, uendpoint, uforcePathStyle = s3.ParseHost(host) } // Override with query parameters if provided if qEndpoint := query.Get("endpoint"); qEndpoint != "" { // Ensure endpoint has a scheme (defaults to https:// for cloud, http:// for local) qEndpoint, _ = litestream.EnsureEndpointScheme(qEndpoint) uendpoint = qEndpoint // Default to path style for custom endpoints unless explicitly set to false if query.Get("forcePathStyle") != "false" { uforcePathStyle = true } endpointWasSet = true } if qRegion := query.Get("region"); qRegion != "" { uregion = qRegion } if qForcePathStyle := query.Get("forcePathStyle"); qForcePathStyle != "" { uforcePathStyle = qForcePathStyle == "true" } if qSkipVerify := query.Get("skipVerify"); qSkipVerify != "" { skipVerify = qSkipVerify == "true" } if v, ok := litestream.BoolQueryValue(query, "signPayload", "sign-payload"); ok { usignPayload = v usignPayloadSet = true } if v, ok := litestream.BoolQueryValue(query, "requireContentMD5", "require-content-md5"); ok { urequireContentMD5 = v urequireContentMD5Set = true } // Only apply URL parts to field that have not been overridden. if configPath == "" { configPath = upath } if bucket == "" { bucket = ubucket } if region == "" { region = uregion } if endpoint == "" { endpoint = uendpoint } if !forcePathStyle { forcePathStyle = uforcePathStyle } if !signSetting.set && usignPayloadSet { signSetting.Set(usignPayload) } if !requireSetting.set && urequireContentMD5Set { requireSetting.Set(urequireContentMD5) } } // Ensure required settings are set. if bucket == "" { return nil, fmt.Errorf("bucket required for s3 replica") } // Detect S3-compatible provider endpoints for applying appropriate defaults. // These providers require specific settings to work correctly with AWS SDK v2. isTigris := litestream.IsTigrisEndpoint(endpoint) if !isTigris && !endpointWasSet && litestream.IsTigrisEndpoint(c.Endpoint) { isTigris = true } isDigitalOcean := litestream.IsDigitalOceanEndpoint(endpoint) isBackblaze := litestream.IsBackblazeEndpoint(endpoint) isFilebase := litestream.IsFilebaseEndpoint(endpoint) isScaleway := litestream.IsScalewayEndpoint(endpoint) isMinIO := litestream.IsMinIOEndpoint(endpoint) isCloudflareR2 := litestream.IsCloudflareR2Endpoint(endpoint) isSupabase := litestream.IsSupabaseEndpoint(endpoint) // Track if forcePathStyle was explicitly set by user (config or URL query param). forcePathStyleSet := c.ForcePathStyle != nil // Apply provider-specific defaults for S3-compatible providers. // These settings ensure compatibility with each provider's S3 implementation. if isTigris { // Tigris: requires signed payloads, no MD5 signSetting.ApplyDefault(true) requireSetting.ApplyDefault(false) } if isDigitalOcean || isBackblaze || isFilebase || isScaleway || isCloudflareR2 || isMinIO || isSupabase { // All these providers require signed payloads (don't support UNSIGNED-PAYLOAD) signSetting.ApplyDefault(true) } if !forcePathStyleSet { // Filebase, Backblaze B2, MinIO, and Supabase require path-style URLs if isFilebase || isBackblaze || isMinIO || isSupabase { forcePathStyle = true } } // Build replica. client := s3.NewReplicaClient() client.AccessKeyID = c.AccessKeyID client.SecretAccessKey = c.SecretAccessKey client.Bucket = bucket client.Path = configPath client.Region = region client.Endpoint = endpoint client.ForcePathStyle = forcePathStyle client.SkipVerify = skipVerify client.SignPayload = signSetting.value client.RequireContentMD5 = requireSetting.value if isCloudflareR2 { client.Concurrency = s3.DefaultR2Concurrency } // Apply upload configuration if specified. if c.PartSize != nil { client.PartSize = int64(*c.PartSize) } if c.Concurrency != nil { client.Concurrency = *c.Concurrency } // Apply SSE-C configuration if specified. if c.SSECustomerKey != "" || c.SSECustomerKeyPath != "" { client.SSECustomerAlgorithm = c.SSECustomerAlgorithm if client.SSECustomerAlgorithm == "" { client.SSECustomerAlgorithm = "AES256" } // Read key from file if path is specified, otherwise use direct value. if c.SSECustomerKeyPath != "" { keyPath := c.SSECustomerKeyPath // Expand ~ to home directory if strings.HasPrefix(keyPath, "~") { home, err := os.UserHomeDir() if err != nil { return nil, fmt.Errorf("cannot expand home directory for sse-customer-key-path: %w", err) } keyPath = home + keyPath[1:] } keyData, err := os.ReadFile(keyPath) if err != nil { return nil, fmt.Errorf("cannot read sse-customer-key-path %q: %w", c.SSECustomerKeyPath, err) } client.SSECustomerKey = strings.TrimSpace(string(keyData)) } else { client.SSECustomerKey = c.SSECustomerKey } } // Apply SSE-KMS configuration if specified. if c.SSEKMSKeyID != "" { client.SSEKMSKeyID = c.SSEKMSKeyID } return client, nil } // newGSReplicaClientFromConfig returns a new instance of gs.ReplicaClient built from config. func newGSReplicaClientFromConfig(c *ReplicaConfig, _ *litestream.Replica) (_ *gs.ReplicaClient, err error) { // Ensure URL & constituent parts are not both specified. if c.URL != "" && c.Path != "" { return nil, fmt.Errorf("cannot specify url & path for gs replica") } else if c.URL != "" && c.Bucket != "" { return nil, fmt.Errorf("cannot specify url & bucket for gs replica") } bucket, configPath := c.Bucket, c.Path // Apply settings from URL, if specified. if c.URL != "" { _, uhost, upath, err := litestream.ParseReplicaURL(c.URL) if err != nil { return nil, err } // Only apply URL parts to field that have not been overridden. if configPath == "" { configPath = upath } if bucket == "" { bucket = uhost } } // Ensure required settings are set. if bucket == "" { return nil, fmt.Errorf("bucket required for gs replica") } // Build replica. client := gs.NewReplicaClient() client.Bucket = bucket client.Path = configPath return client, nil } // newABSReplicaClientFromConfig returns a new instance of abs.ReplicaClient built from config. func newABSReplicaClientFromConfig(c *ReplicaConfig, _ *litestream.Replica) (_ *abs.ReplicaClient, err error) { // Ensure URL & constituent parts are not both specified. if c.URL != "" && c.Path != "" { return nil, fmt.Errorf("cannot specify url & path for abs replica") } else if c.URL != "" && c.Bucket != "" { return nil, fmt.Errorf("cannot specify url & bucket for abs replica") } // Build replica. client := abs.NewReplicaClient() client.AccountName = c.AccountName client.AccountKey = c.AccountKey client.SASToken = c.SASToken client.Bucket = c.Bucket client.Path = c.Path client.Endpoint = c.Endpoint // Apply settings from URL, if specified. if c.URL != "" { u, err := url.Parse(c.URL) if err != nil { return nil, err } if client.AccountName == "" && u.User != nil { client.AccountName = u.User.Username() } if client.Bucket == "" { client.Bucket = u.Host } if client.Path == "" { client.Path = strings.TrimPrefix(path.Clean(u.Path), "/") } } // Ensure required settings are set. if client.Bucket == "" { return nil, fmt.Errorf("bucket required for abs replica") } return client, nil } // newSFTPReplicaClientFromConfig returns a new instance of sftp.ReplicaClient built from config. func newSFTPReplicaClientFromConfig(c *ReplicaConfig, _ *litestream.Replica) (_ *sftp.ReplicaClient, err error) { // Ensure URL & constituent parts are not both specified. if c.URL != "" && c.Path != "" { return nil, fmt.Errorf("cannot specify url & path for sftp replica") } else if c.URL != "" && c.Host != "" { return nil, fmt.Errorf("cannot specify url & host for sftp replica") } host, user, password, path := c.Host, c.User, c.Password, c.Path // Apply settings from URL, if specified. if c.URL != "" { u, err := url.Parse(c.URL) if err != nil { return nil, err } // Only apply URL parts to field that have not been overridden. if host == "" { host = u.Host } if user == "" && u.User != nil { user = u.User.Username() } if password == "" && u.User != nil { password, _ = u.User.Password() } if path == "" { path = u.Path } } // Ensure required settings are set. if host == "" { return nil, fmt.Errorf("host required for sftp replica") } else if user == "" { return nil, fmt.Errorf("user required for sftp replica") } // Build replica. client := sftp.NewReplicaClient() client.Host = host client.User = user client.Password = password client.Path = path client.KeyPath = c.KeyPath client.HostKey = c.HostKey // Set concurrent writes if specified, otherwise use default (true) if c.ConcurrentWrites != nil { client.ConcurrentWrites = *c.ConcurrentWrites } return client, nil } // newWebDAVReplicaClientFromConfig returns a new instance of webdav.ReplicaClient built from config. func newWebDAVReplicaClientFromConfig(c *ReplicaConfig, _ *litestream.Replica) (_ *webdav.ReplicaClient, err error) { // Ensure URL & constituent parts are not both specified. if c.URL != "" && c.Path != "" { return nil, fmt.Errorf("cannot specify url & path for webdav replica") } else if c.URL != "" && c.WebDAVURL != "" { return nil, fmt.Errorf("cannot specify url & webdav-url for webdav replica") } webdavURL, username, password, path := c.WebDAVURL, c.WebDAVUsername, c.WebDAVPassword, c.Path // Apply settings from URL, if specified. if c.URL != "" { u, err := url.Parse(c.URL) if err != nil { return nil, err } // Build WebDAV URL from scheme and host scheme := "http" if u.Scheme == "webdavs" { scheme = "https" } if webdavURL == "" && u.Host != "" { webdavURL = fmt.Sprintf("%s://%s", scheme, u.Host) } // Extract credentials from URL if username == "" && u.User != nil { username = u.User.Username() } if password == "" && u.User != nil { password, _ = u.User.Password() } if path == "" { path = u.Path } } // Ensure required settings are set. if webdavURL == "" { return nil, fmt.Errorf("webdav-url required for webdav replica") } // Build replica. client := webdav.NewReplicaClient() client.URL = webdavURL client.Username = username client.Password = password client.Path = path return client, nil } // newNATSReplicaClientFromConfig returns a new instance of nats.ReplicaClient built from config. func newNATSReplicaClientFromConfig(c *ReplicaConfig, _ *litestream.Replica) (_ *nats.ReplicaClient, err error) { // Parse URL if provided to extract bucket name and server URL var url, bucket string if c.URL != "" { scheme, host, bucketPath, err := litestream.ParseReplicaURL(c.URL) if err != nil { return nil, fmt.Errorf("invalid NATS URL: %w", err) } if scheme != "nats" { return nil, fmt.Errorf("invalid scheme for NATS replica: %s", scheme) } // Reconstruct URL without bucket path if host != "" { url = fmt.Sprintf("nats://%s", host) } // Extract bucket name from path if bucketPath != "" { bucket = strings.Trim(bucketPath, "/") } } // Use bucket from config if not extracted from URL if bucket == "" { bucket = c.Bucket } // Ensure required settings are set if bucket == "" { return nil, fmt.Errorf("bucket required for NATS replica") } // Validate TLS configuration // Both client cert and key must be specified together if (c.ClientCert != "") != (c.ClientKey != "") { return nil, fmt.Errorf("client-cert and client-key must both be specified for mutual TLS authentication") } // Build replica client client := nats.NewReplicaClient() client.URL = url client.BucketName = bucket // Set authentication options client.JWT = c.JWT client.Seed = c.Seed client.Creds = c.Creds client.NKey = c.NKey client.Username = c.Username client.Password = c.Password client.Token = c.Token // Set TLS options client.RootCAs = c.RootCAs client.ClientCert = c.ClientCert client.ClientKey = c.ClientKey // Set connection options with defaults if c.MaxReconnects != nil { client.MaxReconnects = *c.MaxReconnects } if c.ReconnectWait != nil { client.ReconnectWait = *c.ReconnectWait } if c.Timeout != nil { client.Timeout = *c.Timeout } return client, nil } // newOSSReplicaClientFromConfig returns a new instance of oss.ReplicaClient built from config. func newOSSReplicaClientFromConfig(c *ReplicaConfig, _ *litestream.Replica) (_ *oss.ReplicaClient, err error) { // Ensure URL & constituent parts are not both specified. if c.URL != "" && c.Path != "" { return nil, fmt.Errorf("cannot specify url & path for oss replica") } else if c.URL != "" && c.Bucket != "" { return nil, fmt.Errorf("cannot specify url & bucket for oss replica") } bucket, configPath := c.Bucket, c.Path region, endpoint := c.Region, c.Endpoint // Apply settings from URL, if specified. if c.URL != "" { _, host, upath, err := litestream.ParseReplicaURL(c.URL) if err != nil { return nil, err } var ( ubucket string uregion string ) ubucket, uregion, _ = oss.ParseHost(host) // Only apply URL parts to fields that have not been overridden. if configPath == "" { configPath = upath } if bucket == "" { bucket = ubucket } if region == "" { region = uregion } } // Ensure required settings are set. if bucket == "" { return nil, fmt.Errorf("bucket required for oss replica") } // Build replica client. client := oss.NewReplicaClient() client.AccessKeyID = c.AccessKeyID client.AccessKeySecret = c.SecretAccessKey client.Bucket = bucket client.Path = configPath client.Region = region client.Endpoint = endpoint // Apply upload configuration if specified. if c.PartSize != nil { client.PartSize = int64(*c.PartSize) } if c.Concurrency != nil { client.Concurrency = *c.Concurrency } return client, nil } // applyLitestreamEnv copies "LITESTREAM" prefixed environment variables to // their AWS counterparts as the "AWS" prefix can be confusing when using a // non-AWS S3-compatible service. func applyLitestreamEnv() { if v, ok := os.LookupEnv("LITESTREAM_ACCESS_KEY_ID"); ok { if _, ok := os.LookupEnv("AWS_ACCESS_KEY_ID"); !ok { os.Setenv("AWS_ACCESS_KEY_ID", v) } } if v, ok := os.LookupEnv("LITESTREAM_SECRET_ACCESS_KEY"); ok { if _, ok := os.LookupEnv("AWS_SECRET_ACCESS_KEY"); !ok { os.Setenv("AWS_SECRET_ACCESS_KEY", v) } } } type boolSetting struct { value bool set bool } func newBoolSetting(defaultValue bool) boolSetting { return boolSetting{value: defaultValue} } func (s *boolSetting) Set(value bool) { s.value = value s.set = true } func (s *boolSetting) ApplyDefault(value bool) { if !s.set { s.value = value } } // ReplicaType returns the type based on the type field or extracted from the URL. func (c *ReplicaConfig) ReplicaType() string { if replicaType := litestream.ReplicaTypeFromURL(c.URL); replicaType != "" { return replicaType } else if c.Type != "" { return c.Type } return "file" } // DefaultConfigPath returns the default config path. func DefaultConfigPath() string { if v := os.Getenv("LITESTREAM_CONFIG"); v != "" { return v } return defaultConfigPath } func registerConfigFlag(fs *flag.FlagSet) (configPath *string, noExpandEnv *bool) { return fs.String("config", "", "config path"), fs.Bool("no-expand-env", false, "do not expand env vars in config") } // isValidHeartbeatURL checks if the URL is a valid HTTP or HTTPS URL. func isValidHeartbeatURL(u string) bool { return strings.HasPrefix(u, "http://") || strings.HasPrefix(u, "https://") } // expand returns an absolute path for s. // It also strips SQLite connection string prefixes (sqlite://, sqlite3://). func expand(s string) (string, error) { // Strip SQLite connection string prefixes if present. s = StripSQLitePrefix(s) // Just expand to absolute path if there is no home directory prefix. prefix := "~" + string(os.PathSeparator) if s != "~" && !strings.HasPrefix(s, prefix) { return filepath.Abs(s) } // Look up home directory. u, err := user.Current() if err != nil { return "", err } else if u.HomeDir == "" { return "", fmt.Errorf("cannot expand path %s, no home directory available", s) } // Return path with tilde replaced by the home directory. if s == "~" { return u.HomeDir, nil } return filepath.Join(u.HomeDir, strings.TrimPrefix(s, prefix)), nil } // StripSQLitePrefix removes SQLite connection string prefixes (sqlite://, sqlite3://) // from the given path. This allows users to use standard connection string formats // across their tooling while Litestream extracts just the file path. func StripSQLitePrefix(s string) string { if len(s) < 9 || s[0] != 's' { return s } for _, prefix := range []string{"sqlite3://", "sqlite://"} { if strings.HasPrefix(s, prefix) { return strings.TrimPrefix(s, prefix) } } return s } // txidVar allows the flag package to parse index flags as hex-formatted TXIDs type txidVar ltx.TXID // Ensure type implements interface. var _ flag.Value = (*txidVar)(nil) // String returns an 8-character hexadecimal value. func (v *txidVar) String() string { return ltx.TXID(*v).String() } // Set parses s into an integer from a hexadecimal value. func (v *txidVar) Set(s string) error { txID, err := ltx.ParseTXID(s) if err != nil { return fmt.Errorf("invalid txid format") } *v = txidVar(txID) return nil } // levelAll is a sentinel value indicating all compaction levels should be shown. const levelAll = -1 // levelVar allows the flag package to parse compaction level flags. // Accepts integers 0-9 or "all" for all levels. type levelVar int var _ flag.Value = (*levelVar)(nil) func (v *levelVar) String() string { if *v == levelAll { return "all" } return strconv.Itoa(int(*v)) } func (v *levelVar) Set(s string) error { if s == "all" { *v = levelAll return nil } n, err := strconv.Atoi(s) if err != nil { return fmt.Errorf("invalid level: must be 0-%d or \"all\"", litestream.SnapshotLevel) } if n < 0 || n > litestream.SnapshotLevel { return fmt.Errorf("level must be between 0 and %d", litestream.SnapshotLevel) } *v = levelVar(n) return nil } func initLog(w io.Writer, level, typ string) { logOptions := slog.HandlerOptions{ Level: slog.LevelInfo, ReplaceAttr: internal.ReplaceAttr, } // Read log level from environment, if available. if v := os.Getenv("LOG_LEVEL"); v != "" { level = v } switch strings.ToUpper(level) { case "TRACE": logOptions.Level = internal.LevelTrace case "DEBUG": logOptions.Level = slog.LevelDebug case "INFO": logOptions.Level = slog.LevelInfo case "WARN", "WARNING": logOptions.Level = slog.LevelWarn case "ERROR": logOptions.Level = slog.LevelError } var logHandler slog.Handler switch typ { case "json": logHandler = slog.NewJSONHandler(w, &logOptions) case "text", "": logHandler = slog.NewTextHandler(w, &logOptions) } // Set global default logger. slog.SetDefault(slog.New(logHandler)) } ================================================ FILE: cmd/litestream/main_notwindows.go ================================================ //go:build !windows package main import ( "context" "os" "os/signal" "syscall" ) const defaultConfigPath = "/etc/litestream.yml" func isWindowsService() (bool, error) { return false, nil } func runWindowsService(ctx context.Context) error { panic("cannot run windows service as unix process") } func signalChan() <-chan os.Signal { ch := make(chan os.Signal, 2) signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) return ch } ================================================ FILE: cmd/litestream/main_test.go ================================================ package main_test import ( "bytes" "context" "crypto/x509" "errors" "os" "os/exec" "os/user" "path/filepath" "runtime" "strings" "testing" "time" "github.com/benbjohnson/litestream" main "github.com/benbjohnson/litestream/cmd/litestream" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/gs" "github.com/benbjohnson/litestream/s3" "github.com/benbjohnson/litestream/sftp" ) func TestOpenConfigFile(t *testing.T) { t.Run("Success", func(t *testing.T) { // Create a temporary file with test content dir := t.TempDir() testContent := "test: content\n" configPath := filepath.Join(dir, "test.yml") if err := os.WriteFile(configPath, []byte(testContent), 0644); err != nil { t.Fatal(err) } // Open the file rc, err := main.OpenConfigFile(configPath) if err != nil { t.Fatalf("failed to open config file: %v", err) } defer rc.Close() // Read and verify the content buf := new(bytes.Buffer) if _, err := buf.ReadFrom(rc); err != nil { t.Fatalf("failed to read from config file: %v", err) } if got := buf.String(); got != testContent { t.Errorf("content mismatch: got %q, want %q", got, testContent) } }) t.Run("FileNotFound", func(t *testing.T) { _, err := main.OpenConfigFile("/nonexistent/file.yml") if err == nil { t.Error("expected error for nonexistent file") } else if !errors.Is(err, main.ErrConfigFileNotFound) { t.Errorf("expected ErrConfigFileNotFound, got: %v", err) } }) } func TestReadConfigFile(t *testing.T) { // Ensure global AWS settings are propagated down to replica configurations. t.Run("PropagateGlobalSettings", func(t *testing.T) { filename := filepath.Join(t.TempDir(), "litestream.yml") if err := os.WriteFile(filename, []byte(` access-key-id: XXX secret-access-key: YYY dbs: - path: /path/to/db replicas: - url: s3://foo/bar `[1:]), 0666); err != nil { t.Fatal(err) } config, err := main.ReadConfigFile(filename, true) if err != nil { t.Fatal(err) } else if got, want := config.AccessKeyID, `XXX`; got != want { t.Fatalf("AccessKeyID=%v, want %v", got, want) } else if got, want := config.SecretAccessKey, `YYY`; got != want { t.Fatalf("SecretAccessKey=%v, want %v", got, want) } else if got, want := config.DBs[0].Replicas[0].AccessKeyID, `XXX`; got != want { t.Fatalf("Replica.AccessKeyID=%v, want %v", got, want) } else if got, want := config.DBs[0].Replicas[0].SecretAccessKey, `YYY`; got != want { t.Fatalf("Replica.SecretAccessKey=%v, want %v", got, want) } }) // Ensure environment variables are expanded. t.Run("ExpandEnv", func(t *testing.T) { os.Setenv("LITESTREAM_TEST_0129380", "/path/to/db") os.Setenv("LITESTREAM_TEST_1872363", "s3://foo/bar") filename := filepath.Join(t.TempDir(), "litestream.yml") if err := os.WriteFile(filename, []byte(` dbs: - path: $LITESTREAM_TEST_0129380 replicas: - url: ${LITESTREAM_TEST_1872363} - url: ${LITESTREAM_TEST_NO_SUCH_ENV} `[1:]), 0666); err != nil { t.Fatal(err) } config, err := main.ReadConfigFile(filename, true) if err != nil { t.Fatal(err) } else if got, want := config.DBs[0].Path, `/path/to/db`; got != want { t.Fatalf("DB.Path=%v, want %v", got, want) } else if got, want := config.DBs[0].Replicas[0].URL, `s3://foo/bar`; got != want { t.Fatalf("Replica[0].URL=%v, want %v", got, want) } else if got, want := config.DBs[0].Replicas[1].URL, ``; got != want { t.Fatalf("Replica[1].URL=%v, want %v", got, want) } }) // Ensure environment variables are not expanded. t.Run("NoExpandEnv", func(t *testing.T) { os.Setenv("LITESTREAM_TEST_9847533", "s3://foo/bar") filename := filepath.Join(t.TempDir(), "litestream.yml") if err := os.WriteFile(filename, []byte(` dbs: - path: /path/to/db replicas: - url: ${LITESTREAM_TEST_9847533} `[1:]), 0666); err != nil { t.Fatal(err) } config, err := main.ReadConfigFile(filename, false) if err != nil { t.Fatal(err) } else if got, want := config.DBs[0].Replicas[0].URL, `${LITESTREAM_TEST_9847533}`; got != want { t.Fatalf("Replica.URL=%v, want %v", got, want) } }) } func TestNewDBFromConfig_MetaPathExpansion(t *testing.T) { u, err := user.Current() if err != nil { t.Skipf("user.Current failed: %v", err) } if u.HomeDir == "" { t.Skip("no home directory available for expansion test") } tmpDir := t.TempDir() dbPath := filepath.Join(tmpDir, "db.sqlite") replicaPath := filepath.Join(tmpDir, "replica") if err := os.MkdirAll(replicaPath, 0o755); err != nil { t.Fatalf("failed to create replica directory: %v", err) } metaPath := filepath.Join("~", "litestream-meta") config := &main.DBConfig{ Path: dbPath, MetaPath: &metaPath, Replica: &main.ReplicaConfig{ Type: "file", Path: replicaPath, }, } db, err := main.NewDBFromConfig(config) if err != nil { t.Fatalf("NewDBFromConfig failed: %v", err) } expectedMetaPath := filepath.Join(u.HomeDir, "litestream-meta") if got := db.MetaPath(); got != expectedMetaPath { t.Fatalf("MetaPath not expanded: got %s, want %s", got, expectedMetaPath) } if config.MetaPath == nil || *config.MetaPath != expectedMetaPath { t.Fatalf("config MetaPath not updated: got %v, want %s", config.MetaPath, expectedMetaPath) } } func TestNewFileReplicaFromConfig(t *testing.T) { r, err := main.NewReplicaFromConfig(&main.ReplicaConfig{Path: "/foo"}, nil) if err != nil { t.Fatal(err) } else if client, ok := r.Client.(*file.ReplicaClient); !ok { t.Fatal("unexpected replica type") } else if got, want := client.Path(), "/foo"; got != want { t.Fatalf("Path=%s, want %s", got, want) } } func TestNewS3ReplicaFromConfig(t *testing.T) { t.Run("URL", func(t *testing.T) { r, err := main.NewReplicaFromConfig(&main.ReplicaConfig{URL: "s3://foo/bar"}, nil) if err != nil { t.Fatal(err) } else if client, ok := r.Client.(*s3.ReplicaClient); !ok { t.Fatal("unexpected replica type") } else if got, want := client.Bucket, "foo"; got != want { t.Fatalf("Bucket=%s, want %s", got, want) } else if got, want := client.Path, "bar"; got != want { t.Fatalf("Path=%s, want %s", got, want) } else if got, want := client.Region, ""; got != want { t.Fatalf("Region=%s, want %s", got, want) } else if got, want := client.Endpoint, ""; got != want { t.Fatalf("Endpoint=%s, want %s", got, want) } else if got, want := client.ForcePathStyle, false; got != want { t.Fatalf("ForcePathStyle=%v, want %v", got, want) } }) t.Run("MinIO", func(t *testing.T) { r, err := main.NewReplicaFromConfig(&main.ReplicaConfig{URL: "s3://foo.localhost:9000/bar"}, nil) if err != nil { t.Fatal(err) } else if client, ok := r.Client.(*s3.ReplicaClient); !ok { t.Fatal("unexpected replica type") } else if got, want := client.Bucket, "foo"; got != want { t.Fatalf("Bucket=%s, want %s", got, want) } else if got, want := client.Path, "bar"; got != want { t.Fatalf("Path=%s, want %s", got, want) } else if got, want := client.Region, "us-east-1"; got != want { t.Fatalf("Region=%s, want %s", got, want) } else if got, want := client.Endpoint, "http://localhost:9000"; got != want { t.Fatalf("Endpoint=%s, want %s", got, want) } else if got, want := client.ForcePathStyle, true; got != want { t.Fatalf("ForcePathStyle=%v, want %v", got, want) } }) t.Run("Backblaze", func(t *testing.T) { r, err := main.NewReplicaFromConfig(&main.ReplicaConfig{URL: "s3://foo.s3.us-west-000.backblazeb2.com/bar"}, nil) if err != nil { t.Fatal(err) } else if client, ok := r.Client.(*s3.ReplicaClient); !ok { t.Fatal("unexpected replica type") } else if got, want := client.Bucket, "foo"; got != want { t.Fatalf("Bucket=%s, want %s", got, want) } else if got, want := client.Path, "bar"; got != want { t.Fatalf("Path=%s, want %s", got, want) } else if got, want := client.Region, "us-west-000"; got != want { t.Fatalf("Region=%s, want %s", got, want) } else if got, want := client.Endpoint, "https://s3.us-west-000.backblazeb2.com"; got != want { t.Fatalf("Endpoint=%s, want %s", got, want) } else if got, want := client.ForcePathStyle, true; got != want { t.Fatalf("ForcePathStyle=%v, want %v", got, want) } }) t.Run("AccessPointARN", func(t *testing.T) { url := "s3://arn:aws:s3:us-east-2:123456789012:accesspoint/stream-replica" r, err := main.NewReplicaFromConfig(&main.ReplicaConfig{URL: url}, nil) if err != nil { t.Fatal(err) } client, ok := r.Client.(*s3.ReplicaClient) if !ok { t.Fatal("unexpected replica type") } if got, want := client.Bucket, "arn:aws:s3:us-east-2:123456789012:accesspoint/stream-replica"; got != want { t.Fatalf("Bucket=%s, want %s", got, want) } if got, want := client.Path, ""; got != want { t.Fatalf("Path=%s, want %s", got, want) } if got, want := client.Region, "us-east-2"; got != want { t.Fatalf("Region=%s, want %s", got, want) } if got, want := client.Endpoint, ""; got != want { t.Fatalf("Endpoint=%s, want %s", got, want) } if got, want := client.ForcePathStyle, false; got != want { t.Fatalf("ForcePathStyle=%v, want %v", got, want) } }) t.Run("AccessPointARNWithPrefix", func(t *testing.T) { url := "s3://arn:aws:s3:us-west-1:123456789012:accesspoint/stream-replica/backups/primary" r, err := main.NewReplicaFromConfig(&main.ReplicaConfig{URL: url}, nil) if err != nil { t.Fatal(err) } client, ok := r.Client.(*s3.ReplicaClient) if !ok { t.Fatal("unexpected replica type") } if got, want := client.Bucket, "arn:aws:s3:us-west-1:123456789012:accesspoint/stream-replica"; got != want { t.Fatalf("Bucket=%s, want %s", got, want) } if got, want := client.Path, "backups/primary"; got != want { t.Fatalf("Path=%s, want %s", got, want) } if got, want := client.Region, "us-west-1"; got != want { t.Fatalf("Region=%s, want %s", got, want) } if got, want := client.Endpoint, ""; got != want { t.Fatalf("Endpoint=%s, want %s", got, want) } if got, want := client.ForcePathStyle, false; got != want { t.Fatalf("ForcePathStyle=%v, want %v", got, want) } }) } func TestNewGSReplicaFromConfig(t *testing.T) { r, err := main.NewReplicaFromConfig(&main.ReplicaConfig{URL: "gs://foo/bar"}, nil) if err != nil { t.Fatal(err) } else if client, ok := r.Client.(*gs.ReplicaClient); !ok { t.Fatal("unexpected replica type") } else if got, want := client.Bucket, "foo"; got != want { t.Fatalf("Bucket=%s, want %s", got, want) } else if got, want := client.Path, "bar"; got != want { t.Fatalf("Path=%s, want %s", got, want) } } func TestNewSFTPReplicaFromConfig(t *testing.T) { hostKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAnK0+GdwOelXlAXdqLx/qvS7WHMr3rH7zW2+0DtmK5r" r, err := main.NewReplicaFromConfig(&main.ReplicaConfig{ URL: "sftp://user@example.com:2222/foo", ReplicaSettings: main.ReplicaSettings{ HostKey: hostKey, }, }, nil) if err != nil { t.Fatal(err) } else if client, ok := r.Client.(*sftp.ReplicaClient); !ok { t.Fatal("unexpected replica type") } else if got, want := client.HostKey, hostKey; got != want { t.Fatalf("HostKey=%s, want %s", got, want) } else if got, want := client.Host, "example.com:2222"; got != want { t.Fatalf("Host=%s, want %s", got, want) } else if got, want := client.User, "user"; got != want { t.Fatalf("User=%s, want %s", got, want) } else if got, want := client.Path, "/foo"; got != want { t.Fatalf("Path=%s, want %s", got, want) } } // TestNewReplicaFromConfig_AgeEncryption verifies that age encryption configuration is rejected. // Age encryption is currently non-functional and would silently write plaintext data. // See: https://github.com/benbjohnson/litestream/issues/790 func TestNewReplicaFromConfig_AgeEncryption(t *testing.T) { t.Run("RejectIdentities", func(t *testing.T) { config := &main.ReplicaConfig{ URL: "s3://foo/bar", } config.Age.Identities = []string{"AGE-SECRET-KEY-1EXAMPLE"} _, err := main.NewReplicaFromConfig(config, nil) if err == nil { t.Fatal("expected error when age identities are configured") } if !strings.Contains(err.Error(), "age encryption is not currently supported") { t.Errorf("expected age encryption error, got: %v", err) } if !strings.Contains(err.Error(), "revert back to Litestream v0.3.x") { t.Errorf("expected error to reference v0.3.x, got: %v", err) } }) t.Run("RejectRecipients", func(t *testing.T) { config := &main.ReplicaConfig{ URL: "s3://foo/bar", } config.Age.Recipients = []string{"age1example"} _, err := main.NewReplicaFromConfig(config, nil) if err == nil { t.Fatal("expected error when age recipients are configured") } if !strings.Contains(err.Error(), "age encryption is not currently supported") { t.Errorf("expected age encryption error, got: %v", err) } if !strings.Contains(err.Error(), "revert back to Litestream v0.3.x") { t.Errorf("expected error to reference v0.3.x, got: %v", err) } }) t.Run("RejectBoth", func(t *testing.T) { config := &main.ReplicaConfig{ URL: "s3://foo/bar", } config.Age.Identities = []string{"AGE-SECRET-KEY-1EXAMPLE"} config.Age.Recipients = []string{"age1example"} _, err := main.NewReplicaFromConfig(config, nil) if err == nil { t.Fatal("expected error when both age identities and recipients are configured") } if !strings.Contains(err.Error(), "age encryption is not currently supported") { t.Errorf("expected age encryption error, got: %v", err) } if !strings.Contains(err.Error(), "revert back to Litestream v0.3.x") { t.Errorf("expected error to reference v0.3.x, got: %v", err) } }) t.Run("AllowEmpty", func(t *testing.T) { config := &main.ReplicaConfig{ URL: "s3://foo/bar", } _, err := main.NewReplicaFromConfig(config, nil) if err != nil { t.Fatalf("unexpected error when age configuration is not present: %v", err) } }) } // TestConfig_Validate_SnapshotIntervals tests validation of snapshot intervals func TestConfig_Validate_SnapshotIntervals(t *testing.T) { t.Run("ValidInterval", func(t *testing.T) { yaml := ` snapshot: interval: 1h retention: 24h ` config, err := main.ParseConfig(strings.NewReader(yaml), false) if err != nil { t.Fatalf("unexpected error: %v", err) } // Verify the values were set as expected if config.Snapshot.Interval == nil { t.Fatal("expected snapshot interval to be set") } if *config.Snapshot.Interval != 1*time.Hour { t.Errorf("expected snapshot interval of 1h, got %v", *config.Snapshot.Interval) } if config.Snapshot.Retention == nil { t.Fatal("expected snapshot retention to be set") } if *config.Snapshot.Retention != 24*time.Hour { t.Errorf("expected snapshot retention of 24h, got %v", *config.Snapshot.Retention) } }) t.Run("ZeroInterval", func(t *testing.T) { yaml := ` snapshot: interval: 0s retention: 24h ` _, err := main.ParseConfig(strings.NewReader(yaml), false) if err == nil { t.Fatal("expected error for zero interval") } if !errors.Is(err, main.ErrInvalidSnapshotInterval) { t.Errorf("expected ErrInvalidSnapshotInterval, got %v", err) } }) t.Run("ZeroRetention", func(t *testing.T) { yaml := ` snapshot: interval: 1h retention: 0s ` _, err := main.ParseConfig(strings.NewReader(yaml), false) if err == nil { t.Fatal("expected error for zero retention") } if !errors.Is(err, main.ErrInvalidSnapshotRetention) { t.Errorf("expected ErrInvalidSnapshotRetention, got %v", err) } }) t.Run("NegativeInterval", func(t *testing.T) { yaml := ` snapshot: interval: -1h retention: 24h ` _, err := main.ParseConfig(strings.NewReader(yaml), false) if err == nil { t.Fatal("expected error for negative interval") } if !errors.Is(err, main.ErrInvalidSnapshotInterval) { t.Errorf("expected ErrInvalidSnapshotInterval, got %v", err) } }) t.Run("NotSpecified", func(t *testing.T) { yaml := ` # snapshot section not specified dbs: - path: /tmp/test.db ` config, err := main.ParseConfig(strings.NewReader(yaml), false) if err != nil { t.Fatalf("unexpected error: %v", err) } // When snapshot section is not specified, defaults should be applied if config.Snapshot.Interval == nil { t.Fatal("expected snapshot interval to have default value") } if *config.Snapshot.Interval != 24*time.Hour { t.Errorf("expected default snapshot interval of 24h, got %v", *config.Snapshot.Interval) } if config.Snapshot.Retention == nil { t.Fatal("expected snapshot retention to have default value") } if *config.Snapshot.Retention != 24*time.Hour { t.Errorf("expected default snapshot retention of 24h, got %v", *config.Snapshot.Retention) } }) t.Run("EmptySection", func(t *testing.T) { yaml := ` snapshot: ` config, err := main.ParseConfig(strings.NewReader(yaml), false) if err != nil { t.Fatalf("unexpected error: %v", err) } // When snapshot section is empty, defaults should be preserved if config.Snapshot.Interval == nil { t.Fatal("expected snapshot interval to have default value") } if *config.Snapshot.Interval != 24*time.Hour { t.Errorf("expected default snapshot interval of 24h, got %v", *config.Snapshot.Interval) } if config.Snapshot.Retention == nil { t.Fatal("expected snapshot retention to have default value") } if *config.Snapshot.Retention != 24*time.Hour { t.Errorf("expected default snapshot retention of 24h, got %v", *config.Snapshot.Retention) } }) } func TestConfig_Validate_ValidationInterval(t *testing.T) { t.Run("ValidInterval", func(t *testing.T) { yaml := ` validation: interval: 5m ` config, err := main.ParseConfig(strings.NewReader(yaml), false) if err != nil { t.Fatalf("unexpected error: %v", err) } if config.Validation.Interval == nil { t.Fatal("expected validation interval to be set") } if *config.Validation.Interval != 5*time.Minute { t.Errorf("expected validation interval of 5m, got %v", *config.Validation.Interval) } }) t.Run("NotSpecified", func(t *testing.T) { yaml := ` dbs: - path: /tmp/test.db ` config, err := main.ParseConfig(strings.NewReader(yaml), false) if err != nil { t.Fatalf("unexpected error: %v", err) } // When validation section is not specified, interval should be nil (disabled) if config.Validation.Interval != nil { t.Errorf("expected validation interval to be nil, got %v", *config.Validation.Interval) } }) } func TestParseReplicaURL_AccessPoint(t *testing.T) { t.Run("WithPrefix", func(t *testing.T) { scheme, host, urlPath, err := litestream.ParseReplicaURL("s3://arn:aws:s3:us-east-1:123456789012:accesspoint/db-access/backups/prod") if err != nil { t.Fatal(err) } if scheme != "s3" { t.Fatalf("scheme=%s, want s3", scheme) } if host != "arn:aws:s3:us-east-1:123456789012:accesspoint/db-access" { t.Fatalf("host=%s, want arn:aws:s3:us-east-1:123456789012:accesspoint/db-access", host) } if urlPath != "backups/prod" { t.Fatalf("path=%s, want backups/prod", urlPath) } }) t.Run("Invalid", func(t *testing.T) { if _, _, _, err := litestream.ParseReplicaURL("s3://arn:aws:s3:us-east-1:123456789012:accesspoint/"); err == nil { t.Fatal("expected error") } }) } func TestConfig_Validate_L0Retention(t *testing.T) { t.Run("ZeroRetention", func(t *testing.T) { yaml := ` l0-retention: 0s dbs: - path: /tmp/test.db replica: url: file:///tmp/replica ` _, err := main.ParseConfig(strings.NewReader(yaml), false) if err == nil { t.Fatal("expected error for zero l0 retention") } if !errors.Is(err, main.ErrInvalidL0Retention) { t.Errorf("expected ErrInvalidL0Retention, got %v", err) } }) t.Run("ZeroCheckInterval", func(t *testing.T) { yaml := ` l0-retention-check-interval: 0s dbs: - path: /tmp/test.db replica: url: file:///tmp/replica ` _, err := main.ParseConfig(strings.NewReader(yaml), false) if err == nil { t.Fatal("expected error for zero l0 retention check interval") } if !errors.Is(err, main.ErrInvalidL0RetentionCheckInterval) { t.Errorf("expected ErrInvalidL0RetentionCheckInterval, got %v", err) } }) t.Run("DefaultsApplied", func(t *testing.T) { yaml := ` dbs: - path: /tmp/test.db replica: url: file:///tmp/replica ` config, err := main.ParseConfig(strings.NewReader(yaml), false) if err != nil { t.Fatalf("unexpected error: %v", err) } if config.L0Retention == nil { t.Fatal("expected default l0 retention to be set") } if *config.L0Retention != litestream.DefaultL0Retention { t.Errorf("expected default l0 retention %v, got %v", litestream.DefaultL0Retention, *config.L0Retention) } if config.L0RetentionCheckInterval == nil { t.Fatal("expected default l0 retention check interval to be set") } if *config.L0RetentionCheckInterval != litestream.DefaultL0RetentionCheckInterval { t.Errorf("expected default l0 retention check interval %v, got %v", litestream.DefaultL0RetentionCheckInterval, *config.L0RetentionCheckInterval) } }) } // TestConfig_Validate_SyncIntervals tests validation of replica sync intervals func TestConfig_Validate_SyncIntervals(t *testing.T) { t.Run("ValidSyncInterval", func(t *testing.T) { yaml := ` dbs: - path: /tmp/test.db replica: url: file:///tmp/replica sync-interval: 30s ` config, err := main.ParseConfig(strings.NewReader(yaml), false) if err != nil { t.Fatalf("unexpected error: %v", err) } // Verify the sync interval was set correctly if len(config.DBs) != 1 { t.Fatal("expected one database") } if config.DBs[0].Replica == nil { t.Fatal("expected replica to be set") } if config.DBs[0].Replica.SyncInterval == nil { t.Fatal("expected sync interval to be set") } if *config.DBs[0].Replica.SyncInterval != 30*time.Second { t.Errorf("expected sync interval of 30s, got %v", *config.DBs[0].Replica.SyncInterval) } }) t.Run("ZeroSyncInterval", func(t *testing.T) { yaml := ` dbs: - path: /tmp/test.db replica: url: file:///tmp/replica sync-interval: 0s ` _, err := main.ParseConfig(strings.NewReader(yaml), false) if err == nil { t.Fatal("expected error for zero sync interval") } if !errors.Is(err, main.ErrInvalidSyncInterval) { t.Errorf("expected ErrInvalidSyncInterval, got %v", err) } }) t.Run("NegativeSyncInterval", func(t *testing.T) { yaml := ` dbs: - path: /tmp/test.db replica: url: file:///tmp/replica sync-interval: -30s ` _, err := main.ParseConfig(strings.NewReader(yaml), false) if err == nil { t.Fatal("expected error for negative sync interval") } if !errors.Is(err, main.ErrInvalidSyncInterval) { t.Errorf("expected ErrInvalidSyncInterval, got %v", err) } }) t.Run("NotSpecifiedSyncInterval", func(t *testing.T) { yaml := ` dbs: - path: /tmp/test.db replica: url: file:///tmp/replica ` config, err := main.ParseConfig(strings.NewReader(yaml), false) if err != nil { t.Fatalf("unexpected error: %v", err) } // When sync-interval is not specified, it should remain nil // The default will be applied when the replica is created if len(config.DBs) != 1 { t.Fatal("expected one database") } if config.DBs[0].Replica == nil { t.Fatal("expected replica to be set") } if config.DBs[0].Replica.SyncInterval != nil { t.Errorf("expected sync-interval to be nil when not specified, got %v", *config.DBs[0].Replica.SyncInterval) } }) t.Run("MultipleReplicasWithZero", func(t *testing.T) { yaml := ` dbs: - path: /tmp/test.db replicas: - url: file:///tmp/replica1 sync-interval: 30s - url: file:///tmp/replica2 sync-interval: 0s ` _, err := main.ParseConfig(strings.NewReader(yaml), false) if err == nil { t.Fatal("expected error for zero sync interval in second replica") } if !errors.Is(err, main.ErrInvalidSyncInterval) { t.Errorf("expected ErrInvalidSyncInterval, got %v", err) } }) t.Run("ValidMultipleReplicas", func(t *testing.T) { yaml := ` dbs: - path: /tmp/test.db replicas: - url: file:///tmp/replica1 sync-interval: 30s - url: file:///tmp/replica2 sync-interval: 1m ` config, err := main.ParseConfig(strings.NewReader(yaml), false) if err != nil { t.Fatalf("unexpected error: %v", err) } // Verify both replicas have correct intervals if len(config.DBs) != 1 { t.Fatal("expected one database") } if len(config.DBs[0].Replicas) != 2 { t.Fatal("expected two replicas") } // Check first replica if config.DBs[0].Replicas[0].SyncInterval == nil { t.Fatal("expected first replica sync interval to be set") } if *config.DBs[0].Replicas[0].SyncInterval != 30*time.Second { t.Errorf("expected first replica sync interval of 30s, got %v", *config.DBs[0].Replicas[0].SyncInterval) } // Check second replica if config.DBs[0].Replicas[1].SyncInterval == nil { t.Fatal("expected second replica sync interval to be set") } if *config.DBs[0].Replicas[1].SyncInterval != 1*time.Minute { t.Errorf("expected second replica sync interval of 1m, got %v", *config.DBs[0].Replicas[1].SyncInterval) } }) } // TestConfig_Validate_CompactionLevels tests validation of compaction level intervals func TestConfig_Validate_CompactionLevels(t *testing.T) { t.Run("ValidLevels", func(t *testing.T) { yaml := ` levels: - interval: 5m - interval: 1h ` config, err := main.ParseConfig(strings.NewReader(yaml), false) if err != nil { t.Fatalf("unexpected error: %v", err) } // Verify the levels were set correctly if len(config.Levels) != 2 { t.Fatalf("expected 2 compaction levels, got %d", len(config.Levels)) } if config.Levels[0].Interval != 5*time.Minute { t.Errorf("expected level[0] interval of 5m, got %v", config.Levels[0].Interval) } if config.Levels[1].Interval != 1*time.Hour { t.Errorf("expected level[1] interval of 1h, got %v", config.Levels[1].Interval) } }) t.Run("ZeroLevelInterval", func(t *testing.T) { yaml := ` levels: - interval: 0s - interval: 1h ` _, err := main.ParseConfig(strings.NewReader(yaml), false) if err == nil { t.Fatal("expected error for zero level interval") } if !errors.Is(err, main.ErrInvalidCompactionInterval) { t.Errorf("expected ErrInvalidCompactionInterval, got %v", err) } }) t.Run("NegativeLevelInterval", func(t *testing.T) { yaml := ` levels: - interval: 5m - interval: -1h ` _, err := main.ParseConfig(strings.NewReader(yaml), false) if err == nil { t.Fatal("expected error for negative level interval") } if !errors.Is(err, main.ErrInvalidCompactionInterval) { t.Errorf("expected ErrInvalidCompactionInterval, got %v", err) } }) t.Run("NotSpecified", func(t *testing.T) { yaml := ` # levels section not specified, should use defaults dbs: - path: /tmp/test.db ` config, err := main.ParseConfig(strings.NewReader(yaml), false) if err != nil { t.Fatalf("unexpected error: %v", err) } // When levels are not specified, defaults should be applied if len(config.Levels) != 3 { t.Fatalf("expected 3 default compaction levels, got %d", len(config.Levels)) } // Check default intervals: 30s, 5m and 1h if config.Levels[0].Interval != 30*time.Second { t.Errorf("expected default level[0] interval of 5m, got %v", config.Levels[0].Interval) } if config.Levels[1].Interval != 5*time.Minute { t.Errorf("expected default level[0] interval of 5m, got %v", config.Levels[0].Interval) } if config.Levels[2].Interval != 1*time.Hour { t.Errorf("expected default level[1] interval of 1h, got %v", config.Levels[1].Interval) } }) t.Run("CustomLevels", func(t *testing.T) { yaml := ` levels: - interval: 10m - interval: 30m - interval: 2h ` config, err := main.ParseConfig(strings.NewReader(yaml), false) if err != nil { t.Fatalf("unexpected error: %v", err) } // Verify three custom levels if len(config.Levels) != 3 { t.Fatalf("expected 3 compaction levels, got %d", len(config.Levels)) } if config.Levels[0].Interval != 10*time.Minute { t.Errorf("expected level[0] interval of 10m, got %v", config.Levels[0].Interval) } if config.Levels[1].Interval != 30*time.Minute { t.Errorf("expected level[1] interval of 30m, got %v", config.Levels[1].Interval) } if config.Levels[2].Interval != 2*time.Hour { t.Errorf("expected level[2] interval of 2h, got %v", config.Levels[2].Interval) } }) } // TestConfig_DefaultValues tests that default values are properly set func TestConfig_DefaultValues(t *testing.T) { // Test empty config config, err := main.ParseConfig(strings.NewReader(""), false) if err != nil { t.Fatal(err) } // Check snapshot defaults if config.Snapshot.Interval == nil { t.Error("expected snapshot interval to have default value") } else if *config.Snapshot.Interval != 24*time.Hour { t.Errorf("expected default snapshot interval of 24h, got %v", *config.Snapshot.Interval) } if config.Snapshot.Retention == nil { t.Error("expected snapshot retention to have default value") } else if *config.Snapshot.Retention != 24*time.Hour { t.Errorf("expected default snapshot retention of 24h, got %v", *config.Snapshot.Retention) } } // TestParseByteSize tests the ParseByteSize function with various inputs, // including IEC units (MiB, GiB) and decimal values that require proper rounding. func TestParseByteSize(t *testing.T) { tests := []struct { input string want int64 wantErr bool }{ // IEC units (base 1024) - the most important fix for AWS/B2 docs compatibility {"1MiB", 1024 * 1024, false}, {"5MiB", 5 * 1024 * 1024, false}, {"1GiB", 1024 * 1024 * 1024, false}, {"1TiB", 1024 * 1024 * 1024 * 1024, false}, {"1024KiB", 1024 * 1024, false}, // SI units (base 1000) - traditional metric units {"1MB", 1000 * 1000, false}, {"5MB", 5 * 1000 * 1000, false}, {"1GB", 1000 * 1000 * 1000, false}, {"1TB", 1000 * 1000 * 1000 * 1000, false}, {"1000KB", 1000 * 1000, false}, // Short forms (base 1000 - SI units without the 'B') {"1M", 1000 * 1000, false}, {"1K", 1000, false}, {"1G", 1000 * 1000 * 1000, false}, {"1T", 1000 * 1000 * 1000 * 1000, false}, // Decimal values with proper rounding (no more truncation issues) {"1.5MB", 1500000, false}, // 1.5 * 1000 * 1000 {"1.5MiB", 1572864, false}, // 1.5 * 1024 * 1024 {"0.5MB", 500000, false}, // Should round properly, not truncate {"2.5GiB", 2684354560, false}, // 2.5 * 1024^3 {"100.5KB", 100500, false}, // Decimals work with any unit // Basic units {"100B", 100, false}, {"100", 100, false}, // No unit defaults to bytes // Case insensitive {"1mib", 1024 * 1024, false}, {"5MIB", 5 * 1024 * 1024, false}, {"1gib", 1024 * 1024 * 1024, false}, // With spaces (go-humanize handles this) {"1 MiB", 1024 * 1024, false}, {"5 MB", 5 * 1000 * 1000, false}, {"10 GiB", 10 * 1024 * 1024 * 1024, false}, // Real-world examples from AWS/Backblaze documentation {"5MB", 5000000, false}, // AWS SDK default {"100MB", 100000000, false}, // B2 recommended size {"5MiB", 5242880, false}, // The value from the original error report {"1MiB", 1048576, false}, // B2 minimum (though actually they require 5MB) // Invalid inputs {"", 0, true}, {"MB", 0, true}, {"invalid", 0, true}, {"1XB", 0, true}, {"notanumber", 0, true}, } for _, tt := range tests { t.Run(tt.input, func(t *testing.T) { got, err := main.ParseByteSize(tt.input) if (err != nil) != tt.wantErr { t.Errorf("ParseByteSize(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr) return } if !tt.wantErr && got != tt.want { t.Errorf("ParseByteSize(%q) = %d, want %d", tt.input, got, tt.want) } }) } } // TestParseByteSizeOverflow tests that values larger than int64 are rejected. func TestParseByteSizeOverflow(t *testing.T) { // 10 EB (exabytes) = 10,000,000,000,000,000,000 bytes, which exceeds int64 max (9,223,372,036,854,775,807) _, err := main.ParseByteSize("10EB") if err == nil { t.Error("expected error for value exceeding int64 max, got nil") } if !strings.Contains(err.Error(), "exceeds maximum") { t.Errorf("expected overflow error, got: %v", err) } } // TestS3ReplicaConfig_PartSizeAndConcurrency tests that part-size and concurrency // configuration values are properly parsed from YAML and applied to the S3 client. // This test addresses issue #747 where Backblaze B2's 1MB chunk size limit was // being exceeded due to part-size not being honored. func TestS3ReplicaConfig_PartSizeAndConcurrency(t *testing.T) { t.Run("WithPartSize_IEC", func(t *testing.T) { // Test IEC unit (MiB) - the main fix addressing PR feedback filename := filepath.Join(t.TempDir(), "litestream.yml") if err := os.WriteFile(filename, []byte(` dbs: - path: /path/to/db replicas: - type: s3 bucket: mybucket path: mypath region: us-east-1 part-size: 5MiB `[1:]), 0666); err != nil { t.Fatal(err) } config, err := main.ReadConfigFile(filename, false) if err != nil { t.Fatal(err) } if len(config.DBs) != 1 || len(config.DBs[0].Replicas) != 1 { t.Fatal("expected one database with one replica") } replicaConfig := config.DBs[0].Replicas[0] if replicaConfig.PartSize == nil { t.Fatal("expected part-size to be set") } // 5 MiB = 5 * 1024 * 1024 = 5242880 bytes if got, want := int64(*replicaConfig.PartSize), int64(5*1024*1024); got != want { t.Errorf("PartSize = %d, want %d", got, want) } // Test that the value is properly applied to the client r, err := main.NewReplicaFromConfig(replicaConfig, nil) if err != nil { t.Fatal(err) } client, ok := r.Client.(*s3.ReplicaClient) if !ok { t.Fatal("expected S3 replica client") } if got, want := client.PartSize, int64(5*1024*1024); got != want { t.Errorf("client.PartSize = %d, want %d", got, want) } }) t.Run("WithPartSize_SI", func(t *testing.T) { // Test SI unit (MB) - uses base 1000 filename := filepath.Join(t.TempDir(), "litestream.yml") if err := os.WriteFile(filename, []byte(` dbs: - path: /path/to/db replicas: - type: s3 bucket: mybucket path: mypath region: us-east-1 part-size: 5MB `[1:]), 0666); err != nil { t.Fatal(err) } config, err := main.ReadConfigFile(filename, false) if err != nil { t.Fatal(err) } if len(config.DBs) != 1 || len(config.DBs[0].Replicas) != 1 { t.Fatal("expected one database with one replica") } replicaConfig := config.DBs[0].Replicas[0] if replicaConfig.PartSize == nil { t.Fatal("expected part-size to be set") } // 5 MB = 5 * 1000 * 1000 = 5000000 bytes (SI units use base 1000) if got, want := int64(*replicaConfig.PartSize), int64(5*1000*1000); got != want { t.Errorf("PartSize = %d, want %d", got, want) } // Test that the value is properly applied to the client r, err := main.NewReplicaFromConfig(replicaConfig, nil) if err != nil { t.Fatal(err) } client, ok := r.Client.(*s3.ReplicaClient) if !ok { t.Fatal("expected S3 replica client") } if got, want := client.PartSize, int64(5*1000*1000); got != want { t.Errorf("client.PartSize = %d, want %d", got, want) } }) t.Run("WithConcurrency", func(t *testing.T) { filename := filepath.Join(t.TempDir(), "litestream.yml") if err := os.WriteFile(filename, []byte(` dbs: - path: /path/to/db replicas: - type: s3 bucket: mybucket path: mypath region: us-east-1 concurrency: 10 `[1:]), 0666); err != nil { t.Fatal(err) } config, err := main.ReadConfigFile(filename, false) if err != nil { t.Fatal(err) } if len(config.DBs) != 1 || len(config.DBs[0].Replicas) != 1 { t.Fatal("expected one database with one replica") } replicaConfig := config.DBs[0].Replicas[0] if replicaConfig.Concurrency == nil { t.Fatal("expected concurrency to be set") } if got, want := *replicaConfig.Concurrency, 10; got != want { t.Errorf("Concurrency = %d, want %d", got, want) } // Test that the value is properly applied to the client r, err := main.NewReplicaFromConfig(replicaConfig, nil) if err != nil { t.Fatal(err) } client, ok := r.Client.(*s3.ReplicaClient) if !ok { t.Fatal("expected S3 replica client") } if got, want := client.Concurrency, 10; got != want { t.Errorf("client.Concurrency = %d, want %d", got, want) } }) t.Run("WithBoth", func(t *testing.T) { // Test both part-size (using IEC unit) and concurrency together filename := filepath.Join(t.TempDir(), "litestream.yml") if err := os.WriteFile(filename, []byte(` dbs: - path: /path/to/db replicas: - type: s3 bucket: mybucket path: mypath region: us-east-1 part-size: 10MiB concurrency: 10 `[1:]), 0666); err != nil { t.Fatal(err) } config, err := main.ReadConfigFile(filename, false) if err != nil { t.Fatal(err) } if len(config.DBs) != 1 || len(config.DBs[0].Replicas) != 1 { t.Fatal("expected one database with one replica") } replicaConfig := config.DBs[0].Replicas[0] // Verify both values are parsed if replicaConfig.PartSize == nil { t.Fatal("expected part-size to be set") } // 10 MiB = 10 * 1024 * 1024 = 10485760 bytes if got, want := int64(*replicaConfig.PartSize), int64(10*1024*1024); got != want { t.Errorf("PartSize = %d, want %d", got, want) } if replicaConfig.Concurrency == nil { t.Fatal("expected concurrency to be set") } if got, want := *replicaConfig.Concurrency, 10; got != want { t.Errorf("Concurrency = %d, want %d", got, want) } // Test that both values are properly applied to the client r, err := main.NewReplicaFromConfig(replicaConfig, nil) if err != nil { t.Fatal(err) } client, ok := r.Client.(*s3.ReplicaClient) if !ok { t.Fatal("expected S3 replica client") } if got, want := client.PartSize, int64(10*1024*1024); got != want { t.Errorf("client.PartSize = %d, want %d", got, want) } if got, want := client.Concurrency, 10; got != want { t.Errorf("client.Concurrency = %d, want %d", got, want) } }) t.Run("NotSpecified", func(t *testing.T) { filename := filepath.Join(t.TempDir(), "litestream.yml") if err := os.WriteFile(filename, []byte(` dbs: - path: /path/to/db replicas: - type: s3 bucket: mybucket path: mypath region: us-east-1 `[1:]), 0666); err != nil { t.Fatal(err) } config, err := main.ReadConfigFile(filename, false) if err != nil { t.Fatal(err) } if len(config.DBs) != 1 || len(config.DBs[0].Replicas) != 1 { t.Fatal("expected one database with one replica") } replicaConfig := config.DBs[0].Replicas[0] // When not specified, should be nil if replicaConfig.PartSize != nil { t.Errorf("expected PartSize to be nil when not specified, got %v", *replicaConfig.PartSize) } if replicaConfig.Concurrency != nil { t.Errorf("expected Concurrency to be nil when not specified, got %v", *replicaConfig.Concurrency) } // Test that the client is created successfully without these values r, err := main.NewReplicaFromConfig(replicaConfig, nil) if err != nil { t.Fatal(err) } client, ok := r.Client.(*s3.ReplicaClient) if !ok { t.Fatal("expected S3 replica client") } // When not specified, client should have default (0) values // The AWS SDK will use its own defaults if got, want := client.PartSize, int64(0); got != want { t.Errorf("client.PartSize = %d, want %d (AWS SDK default will be used)", got, want) } if got, want := client.Concurrency, 0; got != want { t.Errorf("client.Concurrency = %d, want %d (AWS SDK default will be used)", got, want) } }) } // TestDBConfig_CheckpointFields tests that checkpoint-related configuration fields // are properly parsed from YAML and applied to the DB instance. func TestDBConfig_CheckpointFields(t *testing.T) { t.Run("MinCheckpointPageN", func(t *testing.T) { filename := filepath.Join(t.TempDir(), "litestream.yml") if err := os.WriteFile(filename, []byte(` dbs: - path: /tmp/test.db min-checkpoint-page-count: 2000 replica: url: file:///tmp/replica `[1:]), 0666); err != nil { t.Fatal(err) } config, err := main.ReadConfigFile(filename, false) if err != nil { t.Fatal(err) } if len(config.DBs) != 1 { t.Fatal("expected one database config") } dbc := config.DBs[0] if dbc.MinCheckpointPageN == nil { t.Fatal("expected min-checkpoint-page-count to be set") } if got, want := *dbc.MinCheckpointPageN, 2000; got != want { t.Errorf("MinCheckpointPageN = %d, want %d", got, want) } // Test that the value is properly applied to the DB db, err := main.NewDBFromConfig(dbc) if err != nil { t.Fatal(err) } if got, want := db.MinCheckpointPageN, 2000; got != want { t.Errorf("db.MinCheckpointPageN = %d, want %d", got, want) } }) t.Run("TruncatePageN", func(t *testing.T) { filename := filepath.Join(t.TempDir(), "litestream.yml") if err := os.WriteFile(filename, []byte(` dbs: - path: /tmp/test.db truncate-page-n: 100000 replica: url: file:///tmp/replica `[1:]), 0666); err != nil { t.Fatal(err) } config, err := main.ReadConfigFile(filename, false) if err != nil { t.Fatal(err) } if len(config.DBs) != 1 { t.Fatal("expected one database config") } dbc := config.DBs[0] if dbc.TruncatePageN == nil { t.Fatal("expected truncate-page-n to be set") } if got, want := *dbc.TruncatePageN, 100000; got != want { t.Errorf("TruncatePageN = %d, want %d", got, want) } // Test that the value is properly applied to the DB db, err := main.NewDBFromConfig(dbc) if err != nil { t.Fatal(err) } if got, want := db.TruncatePageN, 100000; got != want { t.Errorf("db.TruncatePageN = %d, want %d", got, want) } }) t.Run("BothCheckpointFields", func(t *testing.T) { filename := filepath.Join(t.TempDir(), "litestream.yml") if err := os.WriteFile(filename, []byte(` dbs: - path: /tmp/test.db min-checkpoint-page-count: 2000 truncate-page-n: 100000 replica: url: file:///tmp/replica `[1:]), 0666); err != nil { t.Fatal(err) } config, err := main.ReadConfigFile(filename, false) if err != nil { t.Fatal(err) } if len(config.DBs) != 1 { t.Fatal("expected one database config") } dbc := config.DBs[0] if dbc.MinCheckpointPageN == nil { t.Fatal("expected min-checkpoint-page-count to be set") } if got, want := *dbc.MinCheckpointPageN, 2000; got != want { t.Errorf("MinCheckpointPageN = %d, want %d", got, want) } if dbc.TruncatePageN == nil { t.Fatal("expected truncate-page-n to be set") } if got, want := *dbc.TruncatePageN, 100000; got != want { t.Errorf("TruncatePageN = %d, want %d", got, want) } // Test that both values are properly applied to the DB db, err := main.NewDBFromConfig(dbc) if err != nil { t.Fatal(err) } if got, want := db.MinCheckpointPageN, 2000; got != want { t.Errorf("db.MinCheckpointPageN = %d, want %d", got, want) } if got, want := db.TruncatePageN, 100000; got != want { t.Errorf("db.TruncatePageN = %d, want %d", got, want) } }) t.Run("NotSpecified_UsesDefaults", func(t *testing.T) { filename := filepath.Join(t.TempDir(), "litestream.yml") if err := os.WriteFile(filename, []byte(` dbs: - path: /tmp/test.db replica: url: file:///tmp/replica `[1:]), 0666); err != nil { t.Fatal(err) } config, err := main.ReadConfigFile(filename, false) if err != nil { t.Fatal(err) } if len(config.DBs) != 1 { t.Fatal("expected one database config") } dbc := config.DBs[0] if dbc.MinCheckpointPageN != nil { t.Errorf("expected MinCheckpointPageN to be nil when not specified, got %v", *dbc.MinCheckpointPageN) } if dbc.TruncatePageN != nil { t.Errorf("expected TruncatePageN to be nil when not specified, got %v", *dbc.TruncatePageN) } // Test that the DB uses default values db, err := main.NewDBFromConfig(dbc) if err != nil { t.Fatal(err) } if got, want := db.MinCheckpointPageN, litestream.DefaultMinCheckpointPageN; got != want { t.Errorf("db.MinCheckpointPageN = %d, want default %d", got, want) } if got, want := db.TruncatePageN, litestream.DefaultTruncatePageN; got != want { t.Errorf("db.TruncatePageN = %d, want default %d", got, want) } }) } func TestFindSQLiteDatabases(t *testing.T) { // Create a temporary directory using t.TempDir() - automatically cleaned up tmpDir := t.TempDir() // Create test files testFiles := []struct { path string isSQLite bool shouldFind bool }{ {"test1.db", true, true}, {"test2.sqlite", true, true}, {"test3.db", false, false}, // Not a SQLite file {"test.txt", false, false}, {"subdir/test4.db", true, true}, {"subdir/test5.sqlite", true, true}, {"subdir/deep/test6.db", true, true}, } // Create test files for _, tf := range testFiles { fullPath := filepath.Join(tmpDir, tf.path) dir := filepath.Dir(fullPath) if err := os.MkdirAll(dir, 0755); err != nil { t.Fatal(err) } file, err := os.Create(fullPath) if err != nil { t.Fatal(err) } if tf.isSQLite { // Write SQLite header if _, err := file.Write([]byte("SQLite format 3\x00")); err != nil { t.Fatal(err) } } else { // Write non-SQLite content if _, err := file.Write([]byte("not a sqlite file")); err != nil { t.Fatal(err) } } if err := file.Close(); err != nil { t.Fatal(err) } } t.Run("non-recursive *.db pattern", func(t *testing.T) { dbs, err := main.FindSQLiteDatabases(tmpDir, "*.db", false) if err != nil { t.Fatal(err) } // Should only find test1.db in root directory if len(dbs) != 1 { t.Errorf("expected 1 database, got %d", len(dbs)) } }) t.Run("recursive *.db pattern", func(t *testing.T) { dbs, err := main.FindSQLiteDatabases(tmpDir, "*.db", true) if err != nil { t.Fatal(err) } // Should find test1.db, test4.db, and test6.db if len(dbs) != 3 { t.Errorf("expected 3 databases, got %d", len(dbs)) } }) t.Run("recursive *.sqlite pattern", func(t *testing.T) { dbs, err := main.FindSQLiteDatabases(tmpDir, "*.sqlite", true) if err != nil { t.Fatal(err) } // Should find test2.sqlite and test5.sqlite if len(dbs) != 2 { t.Errorf("expected 2 databases, got %d", len(dbs)) } }) t.Run("recursive * pattern", func(t *testing.T) { dbs, err := main.FindSQLiteDatabases(tmpDir, "*", true) if err != nil { t.Fatal(err) } // Should find all 5 SQLite databases if len(dbs) != 5 { t.Errorf("expected 5 databases, got %d", len(dbs)) } }) } func TestParseReplicaURLWithQuery(t *testing.T) { t.Run("S3WithEndpoint", func(t *testing.T) { url := "s3://mybucket/path/to/db?endpoint=localhost:9000®ion=us-east-1&forcePathStyle=true" scheme, host, path, query, _, err := litestream.ParseReplicaURLWithQuery(url) if err != nil { t.Fatal(err) } if scheme != "s3" { t.Errorf("expected scheme 's3', got %q", scheme) } if host != "mybucket" { t.Errorf("expected host 'mybucket', got %q", host) } if path != "path/to/db" { t.Errorf("expected path 'path/to/db', got %q", path) } if query.Get("endpoint") != "localhost:9000" { t.Errorf("expected endpoint 'localhost:9000', got %q", query.Get("endpoint")) } if query.Get("region") != "us-east-1" { t.Errorf("expected region 'us-east-1', got %q", query.Get("region")) } if query.Get("forcePathStyle") != "true" { t.Errorf("expected forcePathStyle 'true', got %q", query.Get("forcePathStyle")) } }) t.Run("S3WithoutQuery", func(t *testing.T) { url := "s3://mybucket/path/to/db" scheme, host, path, query, _, err := litestream.ParseReplicaURLWithQuery(url) if err != nil { t.Fatal(err) } if scheme != "s3" { t.Errorf("expected scheme 's3', got %q", scheme) } if host != "mybucket" { t.Errorf("expected host 'mybucket', got %q", host) } if path != "path/to/db" { t.Errorf("expected path 'path/to/db', got %q", path) } if len(query) != 0 { t.Errorf("expected no query parameters, got %v", query) } }) t.Run("FileURL", func(t *testing.T) { url := "file:///path/to/db" scheme, host, path, query, _, err := litestream.ParseReplicaURLWithQuery(url) if err != nil { t.Fatal(err) } if scheme != "file" { t.Errorf("expected scheme 'file', got %q", scheme) } if host != "" { t.Errorf("expected empty host, got %q", host) } if path != "/path/to/db" { t.Errorf("expected path '/path/to/db', got %q", path) } if query != nil { t.Errorf("expected nil query for file URL, got %v", query) } }) t.Run("BackwardCompatibility", func(t *testing.T) { // Test that ParseReplicaURL still works as before url := "s3://mybucket/path/to/db?endpoint=localhost:9000" scheme, host, path, err := litestream.ParseReplicaURL(url) if err != nil { t.Fatal(err) } if scheme != "s3" { t.Errorf("expected scheme 's3', got %q", scheme) } if host != "mybucket" { t.Errorf("expected host 'mybucket', got %q", host) } if path != "path/to/db" { t.Errorf("expected path 'path/to/db', got %q", path) } }) t.Run("S3TigrisExample", func(t *testing.T) { url := "s3://mybucket/db?endpoint=fly.storage.tigris.dev®ion=auto" scheme, host, path, query, _, err := litestream.ParseReplicaURLWithQuery(url) if err != nil { t.Fatal(err) } if scheme != "s3" { t.Errorf("expected scheme 's3', got %q", scheme) } if host != "mybucket" { t.Errorf("expected host 'mybucket', got %q", host) } if path != "db" { t.Errorf("expected path 'db', got %q", path) } if query.Get("endpoint") != "fly.storage.tigris.dev" { t.Errorf("expected endpoint 'fly.storage.tigris.dev', got %q", query.Get("endpoint")) } if query.Get("region") != "auto" { t.Errorf("expected region 'auto', got %q", query.Get("region")) } }) t.Run("S3WithSkipVerify", func(t *testing.T) { url := "s3://mybucket/db?endpoint=self-signed.local&skipVerify=true" _, _, _, query, _, err := litestream.ParseReplicaURLWithQuery(url) if err != nil { t.Fatal(err) } if query.Get("skipVerify") != "true" { t.Errorf("expected skipVerify 'true', got %q", query.Get("skipVerify")) } }) } func TestIsSQLiteDatabase(t *testing.T) { // Create temporary test files using t.TempDir() - automatically cleaned up tmpDir := t.TempDir() t.Run("valid SQLite file", func(t *testing.T) { path := filepath.Join(tmpDir, "valid.db") file, err := os.Create(path) if err != nil { t.Fatal(err) } if _, err := file.Write([]byte("SQLite format 3\x00")); err != nil { t.Fatal(err) } if err := file.Close(); err != nil { t.Fatal(err) } if !main.IsSQLiteDatabase(path) { t.Error("expected file to be identified as SQLite database") } }) t.Run("invalid SQLite file", func(t *testing.T) { path := filepath.Join(tmpDir, "invalid.db") file, err := os.Create(path) if err != nil { t.Fatal(err) } if _, err := file.Write([]byte("not a sqlite file")); err != nil { t.Fatal(err) } if err := file.Close(); err != nil { t.Fatal(err) } if main.IsSQLiteDatabase(path) { t.Error("expected file to NOT be identified as SQLite database") } }) t.Run("non-existent file", func(t *testing.T) { path := filepath.Join(tmpDir, "doesnotexist.db") if main.IsSQLiteDatabase(path) { t.Error("expected non-existent file to NOT be identified as SQLite database") } }) } func TestDBConfigValidation(t *testing.T) { t.Run("both path and dir specified", func(t *testing.T) { config := main.Config{ DBs: []*main.DBConfig{ { Path: "/path/to/db.sqlite", Dir: "/path/to/dir", }, }, } err := config.Validate() if err == nil { t.Error("expected validation error when both path and dir are specified") } }) t.Run("neither path nor dir specified", func(t *testing.T) { config := main.Config{ DBs: []*main.DBConfig{ {}, }, } err := config.Validate() if err == nil { t.Error("expected validation error when neither path nor dir are specified") } }) t.Run("dir without pattern", func(t *testing.T) { config := main.Config{ DBs: []*main.DBConfig{ { Dir: "/path/to/dir", }, }, } err := config.Validate() if err == nil { t.Error("expected validation error when dir is specified without pattern") } }) t.Run("valid path configuration", func(t *testing.T) { config := main.DefaultConfig() config.DBs = []*main.DBConfig{ { Path: "/path/to/db.sqlite", }, } err := config.Validate() if err != nil { t.Errorf("unexpected validation error for valid path config: %v", err) } }) t.Run("valid directory configuration", func(t *testing.T) { config := main.DefaultConfig() config.DBs = []*main.DBConfig{ { Dir: "/path/to/dir", Pattern: "*.db", Recursive: true, }, } err := config.Validate() if err != nil { t.Errorf("unexpected validation error for valid directory config: %v", err) } }) } // TestNewDBsFromDirectoryConfig_UniquePaths verifies that each database discovered // in a directory gets a unique replica path to prevent data corruption. func TestNewDBsFromDirectoryConfig_UniquePaths(t *testing.T) { tmpDir := t.TempDir() // Create multiple databases createSQLiteDB(t, filepath.Join(tmpDir, "db1.db")) createSQLiteDB(t, filepath.Join(tmpDir, "db2.db")) createSQLiteDB(t, filepath.Join(tmpDir, "db3.db")) config := &main.DBConfig{ Dir: tmpDir, Pattern: "*.db", Replica: &main.ReplicaConfig{ Type: "file", Path: "/backup/base", }, } dbs, err := main.NewDBsFromDirectoryConfig(config) if err != nil { t.Fatalf("NewDBsFromDirectoryConfig failed: %v", err) } if len(dbs) != 3 { t.Fatalf("expected 3 databases, got %d", len(dbs)) } // Verify each has unique replica path paths := make(map[string]bool) for _, db := range dbs { if db.Replica == nil { t.Fatalf("database %s has no replica", db.Path()) } replicaPath := db.Replica.Client.(*file.ReplicaClient).Path() if paths[replicaPath] { t.Errorf("duplicate replica path: %s", replicaPath) } paths[replicaPath] = true // Verify path includes database name dbName := filepath.Base(db.Path()) if !strings.Contains(replicaPath, dbName) { t.Errorf("replica path %s does not contain database name %s", replicaPath, dbName) } } // Verify all paths are different if len(paths) != 3 { t.Errorf("expected 3 unique paths, got %d", len(paths)) } } // TestNewDBsFromDirectoryConfig_MetaPathPerDatabase ensures that each database // discovered via a directory config receives a unique metadata directory when a // base meta-path is provided. func TestNewDBsFromDirectoryConfig_MetaPathPerDatabase(t *testing.T) { tmpDir := t.TempDir() rootDB := filepath.Join(tmpDir, "primary.db") createSQLiteDB(t, rootDB) nestedDir := filepath.Join(tmpDir, "team", "nested") if err := os.MkdirAll(nestedDir, 0o755); err != nil { t.Fatalf("failed to create nested directory: %v", err) } nestedDB := filepath.Join(nestedDir, "analytics.db") createSQLiteDB(t, nestedDB) u, err := user.Current() if err != nil { t.Skipf("user.Current failed: %v", err) } if u.HomeDir == "" { t.Skip("no home directory available for expansion test") } metaRoot := filepath.Join("~", "meta-root") expandedMetaRoot := filepath.Join(u.HomeDir, "meta-root") replicaDir := filepath.Join(t.TempDir(), "replica") config := &main.DBConfig{ Dir: tmpDir, Pattern: "*.db", Recursive: true, MetaPath: &metaRoot, Replica: &main.ReplicaConfig{ Type: "file", Path: replicaDir, }, } dbs, err := main.NewDBsFromDirectoryConfig(config) if err != nil { t.Fatalf("NewDBsFromDirectoryConfig failed: %v", err) } if len(dbs) != 2 { t.Fatalf("expected 2 databases, got %d", len(dbs)) } expectedMetaPaths := map[string]string{ rootDB: filepath.Join(expandedMetaRoot, ".primary.db"+litestream.MetaDirSuffix), nestedDB: filepath.Join(expandedMetaRoot, "team", "nested", ".analytics.db"+litestream.MetaDirSuffix), } metaSeen := make(map[string]struct{}) for _, db := range dbs { metaPath := db.MetaPath() want, ok := expectedMetaPaths[db.Path()] if !ok { t.Fatalf("unexpected database path returned: %s", db.Path()) } if metaPath != want { t.Fatalf("database %s meta path mismatch: got %s, want %s", db.Path(), metaPath, want) } if _, dup := metaSeen[metaPath]; dup { t.Fatalf("duplicate meta path detected: %s", metaPath) } metaSeen[metaPath] = struct{}{} } } // TestNewDBsFromDirectoryConfig_SubdirectoryPaths verifies that the relative // directory structure is preserved in replica paths when using recursive scanning. func TestNewDBsFromDirectoryConfig_SubdirectoryPaths(t *testing.T) { tmpDir := t.TempDir() // Create databases in subdirectories createSQLiteDB(t, filepath.Join(tmpDir, "db1.db")) createSQLiteDB(t, filepath.Join(tmpDir, "team-a", "db2.db")) createSQLiteDB(t, filepath.Join(tmpDir, "team-b", "nested", "db3.db")) config := &main.DBConfig{ Dir: tmpDir, Pattern: "*.db", Recursive: true, Replica: &main.ReplicaConfig{ Type: "file", Path: "/backup", }, } dbs, err := main.NewDBsFromDirectoryConfig(config) if err != nil { t.Fatalf("NewDBsFromDirectoryConfig failed: %v", err) } if len(dbs) != 3 { t.Fatalf("expected 3 databases, got %d", len(dbs)) } // Build expected path mappings expectedPaths := map[string]string{ filepath.Join(tmpDir, "db1.db"): "/backup/db1.db", filepath.Join(tmpDir, "team-a", "db2.db"): "/backup/team-a/db2.db", filepath.Join(tmpDir, "team-b", "nested", "db3.db"): "/backup/team-b/nested/db3.db", } for _, db := range dbs { expectedPath, ok := expectedPaths[db.Path()] if !ok { t.Errorf("unexpected database path: %s", db.Path()) continue } replicaPath := db.Replica.Client.(*file.ReplicaClient).Path() if replicaPath != expectedPath { t.Errorf("database %s: expected replica path %s, got %s", db.Path(), expectedPath, replicaPath) } } } // TestNewDBsFromDirectoryConfig_DuplicateFilenames verifies that databases with // the same filename in different subdirectories get unique replica paths. func TestNewDBsFromDirectoryConfig_DuplicateFilenames(t *testing.T) { tmpDir := t.TempDir() // Create databases with same name in different directories createSQLiteDB(t, filepath.Join(tmpDir, "team-a", "db.sqlite")) createSQLiteDB(t, filepath.Join(tmpDir, "team-b", "db.sqlite")) config := &main.DBConfig{ Dir: tmpDir, Pattern: "*.sqlite", Recursive: true, Replica: &main.ReplicaConfig{ Type: "s3", Path: "backups", ReplicaSettings: main.ReplicaSettings{ Bucket: "test-bucket", Region: "us-east-1", }, }, } dbs, err := main.NewDBsFromDirectoryConfig(config) if err != nil { t.Fatalf("NewDBsFromDirectoryConfig failed: %v", err) } if len(dbs) != 2 { t.Fatalf("expected 2 databases, got %d", len(dbs)) } // Verify paths are unique despite duplicate filenames paths := make(map[string]bool) for _, db := range dbs { replicaPath := db.Replica.Client.(*s3.ReplicaClient).Path if paths[replicaPath] { t.Errorf("duplicate replica path found: %s", replicaPath) } paths[replicaPath] = true } if len(paths) != 2 { t.Errorf("expected 2 unique paths, got %d", len(paths)) } // Verify paths contain subdirectory to disambiguate for _, db := range dbs { replicaPath := db.Replica.Client.(*s3.ReplicaClient).Path if !strings.Contains(replicaPath, "team-a") && !strings.Contains(replicaPath, "team-b") { t.Errorf("replica path %s does not contain team subdirectory", replicaPath) } } } // TestNewDBsFromDirectoryConfig_S3URL verifies that replica URLs receive a // per-database suffix so multiple databases do not overwrite one another. func TestNewDBsFromDirectoryConfig_S3URL(t *testing.T) { tmpDir := t.TempDir() createSQLiteDB(t, filepath.Join(tmpDir, "team-a", "db.sqlite")) createSQLiteDB(t, filepath.Join(tmpDir, "team-b", "nested", "db.sqlite")) config := &main.DBConfig{ Dir: tmpDir, Pattern: "*.sqlite", Recursive: true, Replica: &main.ReplicaConfig{ URL: "s3://test-bucket/backups", }, } dbs, err := main.NewDBsFromDirectoryConfig(config) if err != nil { t.Fatalf("NewDBsFromDirectoryConfig failed: %v", err) } if len(dbs) != 2 { t.Fatalf("expected 2 databases, got %d", len(dbs)) } expectedPaths := map[string]string{ filepath.Join(tmpDir, "team-a", "db.sqlite"): "backups/team-a/db.sqlite", filepath.Join(tmpDir, "team-b", "nested", "db.sqlite"): "backups/team-b/nested/db.sqlite", } for _, db := range dbs { expectedPath, ok := expectedPaths[db.Path()] if !ok { t.Errorf("unexpected database path: %s", db.Path()) continue } client := db.Replica.Client.(*s3.ReplicaClient) if client.Path != expectedPath { t.Errorf("database %s: expected replica path %s, got %s", db.Path(), expectedPath, client.Path) } } } // TestNewDBsFromDirectoryConfig_ReplicasArrayURL verifies URL handling when // using the deprecated replicas array form. func TestNewDBsFromDirectoryConfig_ReplicasArrayURL(t *testing.T) { tmpDir := t.TempDir() createSQLiteDB(t, filepath.Join(tmpDir, "db1.sqlite")) createSQLiteDB(t, filepath.Join(tmpDir, "subs", "db2.sqlite")) config := &main.DBConfig{ Dir: tmpDir, Pattern: "*.sqlite", Recursive: true, Replicas: []*main.ReplicaConfig{ { URL: "s3://legacy-bucket/base", }, }, } dbs, err := main.NewDBsFromDirectoryConfig(config) if err != nil { t.Fatalf("NewDBsFromDirectoryConfig failed: %v", err) } if len(dbs) != 2 { t.Fatalf("expected 2 databases, got %d", len(dbs)) } expectedPaths := map[string]string{ filepath.Join(tmpDir, "db1.sqlite"): "base/db1.sqlite", filepath.Join(tmpDir, "subs", "db2.sqlite"): "base/subs/db2.sqlite", } for _, db := range dbs { expectedPath, ok := expectedPaths[db.Path()] if !ok { t.Errorf("unexpected database path: %s", db.Path()) continue } client := db.Replica.Client.(*s3.ReplicaClient) if client.Path != expectedPath { t.Errorf("database %s: expected replica path %s, got %s", db.Path(), expectedPath, client.Path) } } } // TestNewDBsFromDirectoryConfig_SpecialCharacters verifies that special characters // in database filenames are handled correctly in replica paths. func TestNewDBsFromDirectoryConfig_SpecialCharacters(t *testing.T) { tmpDir := t.TempDir() // Create databases with special characters specialNames := []string{ "my database.db", "user@example.com.db", "tenant#1.db", } for _, name := range specialNames { createSQLiteDB(t, filepath.Join(tmpDir, name)) } config := &main.DBConfig{ Dir: tmpDir, Pattern: "*.db", Replica: &main.ReplicaConfig{ Type: "file", Path: "/backup", }, } dbs, err := main.NewDBsFromDirectoryConfig(config) if err != nil { t.Fatalf("NewDBsFromDirectoryConfig failed: %v", err) } if len(dbs) != len(specialNames) { t.Fatalf("expected %d databases, got %d", len(specialNames), len(dbs)) } // Verify each special name is in a replica path for _, db := range dbs { replicaPath := db.Replica.Client.(*file.ReplicaClient).Path() dbName := filepath.Base(db.Path()) if !strings.Contains(replicaPath, dbName) { t.Errorf("replica path %s does not contain database name %s", replicaPath, dbName) } } } // TestNewDBsFromDirectoryConfig_EmptyBasePath verifies that an empty base path // results in the database relative path being used as the entire replica path. func TestNewDBsFromDirectoryConfig_EmptyBasePath(t *testing.T) { tmpDir := t.TempDir() createSQLiteDB(t, filepath.Join(tmpDir, "test.db")) config := &main.DBConfig{ Dir: tmpDir, Pattern: "*.db", Replica: &main.ReplicaConfig{ Type: "file", Path: "", // Empty base path }, } dbs, err := main.NewDBsFromDirectoryConfig(config) if err != nil { t.Fatalf("NewDBsFromDirectoryConfig failed: %v", err) } if len(dbs) != 1 { t.Fatalf("expected 1 database, got %d", len(dbs)) } replicaPath := dbs[0].Replica.Client.(*file.ReplicaClient).Path() // When base path is empty, the relative path (just filename) is used // But it's still expanded to absolute path by the file backend if !strings.HasSuffix(replicaPath, "test.db") { t.Errorf("expected replica path to end with 'test.db', got %s", replicaPath) } } // TestNewDBsFromDirectoryConfig_ReplicasArray verifies that the deprecated // 'replicas' array field is handled correctly with unique paths. func TestNewDBsFromDirectoryConfig_ReplicasArray(t *testing.T) { tmpDir := t.TempDir() createSQLiteDB(t, filepath.Join(tmpDir, "db1.db")) createSQLiteDB(t, filepath.Join(tmpDir, "db2.db")) config := &main.DBConfig{ Dir: tmpDir, Pattern: "*.db", Replicas: []*main.ReplicaConfig{ { Type: "file", Path: "/backup", }, }, } dbs, err := main.NewDBsFromDirectoryConfig(config) if err != nil { t.Fatalf("NewDBsFromDirectoryConfig failed: %v", err) } if len(dbs) != 2 { t.Fatalf("expected 2 databases, got %d", len(dbs)) } // Verify each has unique replica path paths := make(map[string]bool) for _, db := range dbs { if db.Replica == nil { t.Fatalf("database %s has no replica", db.Path()) } replicaPath := db.Replica.Client.(*file.ReplicaClient).Path() if paths[replicaPath] { t.Errorf("duplicate replica path: %s", replicaPath) } paths[replicaPath] = true } } func TestNewDBsFromDirectoryConfig_EmptyDirectoryRequiresDatabases(t *testing.T) { tmpDir := t.TempDir() replicaDir := filepath.Join(tmpDir, "replica") config := &main.DBConfig{ Dir: tmpDir, Pattern: "*.db", Replica: &main.ReplicaConfig{Type: "file", Path: replicaDir}, } if _, err := main.NewDBsFromDirectoryConfig(config); err == nil { t.Fatalf("expected error for empty directory when watch disabled") } } func TestNewDBsFromDirectoryConfig_EmptyDirectoryWithWatch(t *testing.T) { tmpDir := t.TempDir() replicaDir := filepath.Join(tmpDir, "replica") config := &main.DBConfig{ Dir: tmpDir, Pattern: "*.db", Watch: true, Replica: &main.ReplicaConfig{Type: "file", Path: replicaDir}, } dbs, err := main.NewDBsFromDirectoryConfig(config) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(dbs) != 0 { t.Fatalf("expected 0 databases, got %d", len(dbs)) } } func TestDirectoryMonitor_DetectsDatabaseLifecycle(t *testing.T) { ctx := context.Background() rootDir := t.TempDir() replicaDir := filepath.Join(t.TempDir(), "replicas") initialPath := filepath.Join(rootDir, "initial.db") createSQLiteDB(t, initialPath) config := &main.DBConfig{ Dir: rootDir, Pattern: "*.db", Replica: &main.ReplicaConfig{Type: "file", Path: replicaDir}, } dbs, err := main.NewDBsFromDirectoryConfig(config) if err != nil { t.Fatalf("NewDBsFromDirectoryConfig failed: %v", err) } storeConfig := main.DefaultConfig() store := litestream.NewStore(dbs, storeConfig.CompactionLevels()) store.CompactionMonitorEnabled = false if err := store.Open(ctx); err != nil { t.Fatalf("unexpected error opening store: %v", err) } defer func() { if err := store.Close(context.Background()); err != nil { t.Fatalf("unexpected error closing store: %v", err) } }() monitor, err := main.NewDirectoryMonitor(ctx, store, config, dbs) if err != nil { t.Fatalf("failed to initialize directory monitor: %v", err) } defer monitor.Close() newPath := filepath.Join(rootDir, "new.db") createSQLiteDB(t, newPath) if !waitForCondition(5*time.Second, func() bool { return hasDBPath(store.DBs(), newPath) }) { t.Fatalf("expected new database %s to be detected", newPath) } if err := os.Remove(newPath); err != nil { t.Fatalf("failed to remove database: %v", err) } if !waitForCondition(5*time.Second, func() bool { return !hasDBPath(store.DBs(), newPath) }) { t.Fatalf("expected database %s to be removed", newPath) } } func TestDirectoryMonitor_RecursiveDetectsNestedDatabases(t *testing.T) { ctx := context.Background() rootDir := t.TempDir() replicaDir := filepath.Join(t.TempDir(), "replicas") config := &main.DBConfig{ Dir: rootDir, Pattern: "*.db", Recursive: true, Watch: true, Replica: &main.ReplicaConfig{Type: "file", Path: replicaDir}, } storeConfig := main.DefaultConfig() store := litestream.NewStore(nil, storeConfig.CompactionLevels()) store.CompactionMonitorEnabled = false if err := store.Open(ctx); err != nil { t.Fatalf("unexpected error opening store: %v", err) } defer func() { if err := store.Close(context.Background()); err != nil { t.Fatalf("unexpected error closing store: %v", err) } }() monitor, err := main.NewDirectoryMonitor(ctx, store, config, nil) if err != nil { t.Fatalf("failed to initialize directory monitor: %v", err) } defer monitor.Close() deepDir := filepath.Join(rootDir, "tenant", "nested", "deeper") if err := os.MkdirAll(deepDir, 0755); err != nil { t.Fatalf("failed to create nested directories: %v", err) } deepDB := filepath.Join(deepDir, "deep.db") createSQLiteDB(t, deepDB) if !waitForCondition(5*time.Second, func() bool { return hasDBPath(store.DBs(), deepDB) }) { t.Fatalf("expected nested database %s to be detected", deepDB) } } // createSQLiteDB creates a minimal SQLite database file for testing func createSQLiteDB(t *testing.T, path string) { t.Helper() dir := filepath.Dir(path) if err := os.MkdirAll(dir, 0755); err != nil { t.Fatalf("failed to create directory %s: %v", dir, err) } file, err := os.Create(path) if err != nil { t.Fatalf("failed to create file %s: %v", path, err) } defer file.Close() // Write SQLite header if _, err := file.Write([]byte("SQLite format 3\x00")); err != nil { t.Fatalf("failed to write SQLite header: %v", err) } } func TestNewS3ReplicaClientFromConfig(t *testing.T) { t.Run("URLWithEndpointQuery", func(t *testing.T) { config := &main.ReplicaConfig{ URL: "s3://mybucket/path/to/db?endpoint=localhost:9000®ion=us-west-2&forcePathStyle=true&skipVerify=true", } client, err := main.NewS3ReplicaClientFromConfig(config, nil) if err != nil { t.Fatal(err) } if client.Bucket != "mybucket" { t.Errorf("expected bucket 'mybucket', got %q", client.Bucket) } if client.Path != "path/to/db" { t.Errorf("expected path 'path/to/db', got %q", client.Path) } if client.Endpoint != "http://localhost:9000" { t.Errorf("expected endpoint 'http://localhost:9000', got %q", client.Endpoint) } if client.Region != "us-west-2" { t.Errorf("expected region 'us-west-2', got %q", client.Region) } if !client.ForcePathStyle { t.Error("expected ForcePathStyle to be true") } if !client.SkipVerify { t.Error("expected SkipVerify to be true") } }) t.Run("URLWithoutQuery", func(t *testing.T) { config := &main.ReplicaConfig{ URL: "s3://mybucket.s3.amazonaws.com/path/to/db", } client, err := main.NewS3ReplicaClientFromConfig(config, nil) if err != nil { t.Fatal(err) } if client.Bucket != "mybucket" { t.Errorf("expected bucket 'mybucket', got %q", client.Bucket) } if client.Path != "path/to/db" { t.Errorf("expected path 'path/to/db', got %q", client.Path) } // Should use default AWS settings if client.Endpoint != "" { t.Errorf("expected empty endpoint for AWS S3, got %q", client.Endpoint) } if client.ForcePathStyle { t.Error("expected ForcePathStyle to be false for AWS S3") } }) t.Run("ConfigOverridesQuery", func(t *testing.T) { config := &main.ReplicaConfig{ URL: "s3://mybucket/path?endpoint=from-query®ion=us-east-1", ReplicaSettings: main.ReplicaSettings{ Endpoint: "from-config", Region: "us-west-1", }, } client, err := main.NewS3ReplicaClientFromConfig(config, nil) if err != nil { t.Fatal(err) } // Config values should take precedence over query params if client.Endpoint != "from-config" { t.Errorf("expected endpoint from config 'from-config', got %q", client.Endpoint) } if client.Region != "us-west-1" { t.Errorf("expected region from config 'us-west-1', got %q", client.Region) } }) t.Run("TigrisExample", func(t *testing.T) { config := &main.ReplicaConfig{ URL: "s3://my-tigris-bucket/db.sqlite?endpoint=fly.storage.tigris.dev®ion=auto", } client, err := main.NewS3ReplicaClientFromConfig(config, nil) if err != nil { t.Fatal(err) } if client.Bucket != "my-tigris-bucket" { t.Errorf("expected bucket 'my-tigris-bucket', got %q", client.Bucket) } if client.Endpoint != "https://fly.storage.tigris.dev" { t.Errorf("expected Tigris endpoint with https scheme, got %q", client.Endpoint) } if client.Region != "auto" { t.Errorf("expected region 'auto' for Tigris, got %q", client.Region) } if !client.ForcePathStyle { t.Error("expected ForcePathStyle to be true for custom endpoint") } if !client.SignPayload { t.Error("expected SignPayload to be true for Tigris") } if client.RequireContentMD5 { t.Error("expected RequireContentMD5 to be false for Tigris") } }) t.Run("TigrisConfigEndpoint", func(t *testing.T) { config := &main.ReplicaConfig{ Path: "path", ReplicaSettings: main.ReplicaSettings{ Bucket: "mybucket", Endpoint: "https://fly.storage.tigris.dev", }, } client, err := main.NewS3ReplicaClientFromConfig(config, nil) if err != nil { t.Fatal(err) } if !client.SignPayload { t.Error("expected SignPayload to be true for config-based Tigris endpoint") } if client.RequireContentMD5 { t.Error("expected RequireContentMD5 to be false for config-based Tigris endpoint") } }) t.Run("HTTPSEndpoint", func(t *testing.T) { config := &main.ReplicaConfig{ URL: "s3://mybucket/path?endpoint=https://secure.storage.com®ion=us-east-1", } client, err := main.NewS3ReplicaClientFromConfig(config, nil) if err != nil { t.Fatal(err) } if client.Endpoint != "https://secure.storage.com" { t.Errorf("expected endpoint 'https://secure.storage.com', got %q", client.Endpoint) } if !client.ForcePathStyle { t.Error("expected ForcePathStyle to be true for custom endpoint") } }) t.Run("QuerySigningOptions", func(t *testing.T) { config := &main.ReplicaConfig{ URL: "s3://bucket/db?sign-payload=true&require-content-md5=false", } client, err := main.NewS3ReplicaClientFromConfig(config, nil) if err != nil { t.Fatal(err) } if !client.SignPayload { t.Error("expected SignPayload to be true when query parameter is set") } if client.RequireContentMD5 { t.Error("expected RequireContentMD5 to be false when disabled via query") } }) t.Run("ConfigOverridesQuerySigning", func(t *testing.T) { signTrue := true requireFalse := false config := &main.ReplicaConfig{ URL: "s3://bucket/db?sign-payload=false&require-content-md5=true", ReplicaSettings: main.ReplicaSettings{ SignPayload: &signTrue, RequireContentMD5: &requireFalse, }, } client, err := main.NewS3ReplicaClientFromConfig(config, nil) if err != nil { t.Fatal(err) } if !client.SignPayload { t.Error("expected config SignPayload to override query parameter") } if client.RequireContentMD5 { t.Error("expected config RequireContentMD5=false to override query parameter") } }) t.Run("TigrisManualOverride", func(t *testing.T) { signFalse := false requireTrue := true config := &main.ReplicaConfig{ URL: "s3://bucket/db?endpoint=fly.storage.tigris.dev®ion=auto", ReplicaSettings: main.ReplicaSettings{ SignPayload: &signFalse, RequireContentMD5: &requireTrue, }, } client, err := main.NewS3ReplicaClientFromConfig(config, nil) if err != nil { t.Fatal(err) } if client.SignPayload { t.Error("expected manual SignPayload override to take precedence") } if !client.RequireContentMD5 { t.Error("expected manual RequireContentMD5 override to take precedence") } }) t.Run("ProviderDefaultsParity", func(t *testing.T) { tests := []struct { name string endpoint string }{ {"Tigris", "https://fly.storage.tigris.dev"}, {"DigitalOcean", "https://nyc3.digitaloceanspaces.com"}, {"Backblaze", "https://s3.us-west-002.backblazeb2.com"}, {"Filebase", "https://s3.filebase.com"}, {"Scaleway", "https://s3.fr-par.scw.cloud"}, {"CloudflareR2", "https://accountid.r2.cloudflarestorage.com"}, {"MinIO", "http://localhost:9000"}, {"Supabase", "https://myproject.supabase.co/storage/v1/s3"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { urlClient, err := litestream.NewReplicaClientFromURL( "s3://mybucket/path?endpoint=" + tt.endpoint + "®ion=us-east-1", ) if err != nil { t.Fatalf("URL factory error: %v", err) } uc := urlClient.(*s3.ReplicaClient) cc, err := main.NewS3ReplicaClientFromConfig(&main.ReplicaConfig{ Path: "path", ReplicaSettings: main.ReplicaSettings{ Bucket: "mybucket", Endpoint: tt.endpoint, Region: "us-east-1", }, }, nil) if err != nil { t.Fatalf("Config factory error: %v", err) } if uc.SignPayload != cc.SignPayload { t.Errorf("SignPayload: URL=%v, Config=%v", uc.SignPayload, cc.SignPayload) } if uc.RequireContentMD5 != cc.RequireContentMD5 { t.Errorf("RequireContentMD5: URL=%v, Config=%v", uc.RequireContentMD5, cc.RequireContentMD5) } if uc.ForcePathStyle != cc.ForcePathStyle { t.Errorf("ForcePathStyle: URL=%v, Config=%v", uc.ForcePathStyle, cc.ForcePathStyle) } if uc.Concurrency != cc.Concurrency { t.Errorf("Concurrency: URL=%v, Config=%v", uc.Concurrency, cc.Concurrency) } }) } }) t.Run("R2ConfigExplicitOverride", func(t *testing.T) { signFalse := false config := &main.ReplicaConfig{ Path: "path", ReplicaSettings: main.ReplicaSettings{ Bucket: "mybucket", Endpoint: "https://accountid.r2.cloudflarestorage.com", SignPayload: &signFalse, }, } client, err := main.NewS3ReplicaClientFromConfig(config, nil) if err != nil { t.Fatal(err) } if client.SignPayload { t.Error("expected explicit SignPayload=false to override R2 default") } }) } func TestGlobalDefaults(t *testing.T) { // Test comprehensive global defaults functionality t.Run("GlobalReplicaDefaults", func(t *testing.T) { filename := filepath.Join(t.TempDir(), "litestream.yml") syncInterval := "30s" validationInterval := "1h" if err := os.WriteFile(filename, []byte(` # Global defaults for all replicas access-key-id: GLOBAL_ACCESS_KEY secret-access-key: GLOBAL_SECRET_KEY region: us-west-2 endpoint: custom.s3.endpoint.com sync-interval: `+syncInterval+` validation-interval: `+validationInterval+` dbs: # Database 1: Uses all global defaults - path: /tmp/db1.sqlite replica: type: s3 bucket: my-bucket-1 # Database 2: Overrides some defaults - path: /tmp/db2.sqlite replica: type: s3 bucket: my-bucket-2 region: us-east-1 # Override global region access-key-id: CUSTOM_KEY # Override global access key # Database 3: Uses legacy replicas format - path: /tmp/db3.sqlite replicas: - type: s3 bucket: my-bucket-3 # Should inherit all other global settings `[1:]), 0666); err != nil { t.Fatal(err) } config, err := main.ReadConfigFile(filename, true) if err != nil { t.Fatal(err) } // Test global settings were parsed correctly if got, want := config.AccessKeyID, "GLOBAL_ACCESS_KEY"; got != want { t.Errorf("config.AccessKeyID=%v, want %v", got, want) } if got, want := config.SecretAccessKey, "GLOBAL_SECRET_KEY"; got != want { t.Errorf("config.SecretAccessKey=%v, want %v", got, want) } if got, want := config.Region, "us-west-2"; got != want { t.Errorf("config.Region=%v, want %v", got, want) } if got, want := config.Endpoint, "custom.s3.endpoint.com"; got != want { t.Errorf("config.Endpoint=%v, want %v", got, want) } // Parse expected intervals expectedSyncInterval, err := time.ParseDuration(syncInterval) if err != nil { t.Fatal(err) } expectedValidationInterval, err := time.ParseDuration(validationInterval) if err != nil { t.Fatal(err) } if config.SyncInterval == nil || *config.SyncInterval != expectedSyncInterval { t.Errorf("config.SyncInterval=%v, want %v", config.SyncInterval, expectedSyncInterval) } if config.ValidationInterval == nil || *config.ValidationInterval != expectedValidationInterval { t.Errorf("config.ValidationInterval=%v, want %v", config.ValidationInterval, expectedValidationInterval) } // Test Database 1: Should inherit all global defaults db1 := config.DBs[0] if db1.Replica == nil { t.Fatal("db1.Replica is nil") } replica1 := db1.Replica if got, want := replica1.AccessKeyID, "GLOBAL_ACCESS_KEY"; got != want { t.Errorf("replica1.AccessKeyID=%v, want %v", got, want) } if got, want := replica1.SecretAccessKey, "GLOBAL_SECRET_KEY"; got != want { t.Errorf("replica1.SecretAccessKey=%v, want %v", got, want) } if got, want := replica1.Region, "us-west-2"; got != want { t.Errorf("replica1.Region=%v, want %v", got, want) } if got, want := replica1.Endpoint, "custom.s3.endpoint.com"; got != want { t.Errorf("replica1.Endpoint=%v, want %v", got, want) } if got, want := replica1.Bucket, "my-bucket-1"; got != want { t.Errorf("replica1.Bucket=%v, want %v", got, want) } if replica1.SyncInterval == nil || *replica1.SyncInterval != expectedSyncInterval { t.Errorf("replica1.SyncInterval=%v, want %v", replica1.SyncInterval, expectedSyncInterval) } if replica1.ValidationInterval == nil || *replica1.ValidationInterval != expectedValidationInterval { t.Errorf("replica1.ValidationInterval=%v, want %v", replica1.ValidationInterval, expectedValidationInterval) } // Test Database 2: Should override some defaults db2 := config.DBs[1] if db2.Replica == nil { t.Fatal("db2.Replica is nil") } replica2 := db2.Replica if got, want := replica2.AccessKeyID, "CUSTOM_KEY"; got != want { t.Errorf("replica2.AccessKeyID=%v, want %v", got, want) } if got, want := replica2.SecretAccessKey, "GLOBAL_SECRET_KEY"; got != want { t.Errorf("replica2.SecretAccessKey=%v, want %v", got, want) } if got, want := replica2.Region, "us-east-1"; got != want { t.Errorf("replica2.Region=%v, want %v", got, want) } if got, want := replica2.Endpoint, "custom.s3.endpoint.com"; got != want { t.Errorf("replica2.Endpoint=%v, want %v", got, want) } if got, want := replica2.Bucket, "my-bucket-2"; got != want { t.Errorf("replica2.Bucket=%v, want %v", got, want) } // Test Database 3: Legacy replicas format should work db3 := config.DBs[2] if len(db3.Replicas) != 1 { t.Fatalf("db3.Replicas length=%v, want 1", len(db3.Replicas)) } replica3 := db3.Replicas[0] if got, want := replica3.AccessKeyID, "GLOBAL_ACCESS_KEY"; got != want { t.Errorf("replica3.AccessKeyID=%v, want %v", got, want) } if got, want := replica3.SecretAccessKey, "GLOBAL_SECRET_KEY"; got != want { t.Errorf("replica3.SecretAccessKey=%v, want %v", got, want) } if got, want := replica3.Region, "us-west-2"; got != want { t.Errorf("replica3.Region=%v, want %v", got, want) } if got, want := replica3.Endpoint, "custom.s3.endpoint.com"; got != want { t.Errorf("replica3.Endpoint=%v, want %v", got, want) } if got, want := replica3.Bucket, "my-bucket-3"; got != want { t.Errorf("replica3.Bucket=%v, want %v", got, want) } }) // Test different replica types inherit appropriate defaults t.Run("MultipleReplicaTypes", func(t *testing.T) { filename := filepath.Join(t.TempDir(), "litestream.yml") if err := os.WriteFile(filename, []byte(` # Global defaults that apply to all supported replica types access-key-id: GLOBAL_S3_KEY secret-access-key: GLOBAL_S3_SECRET region: global-region endpoint: global.endpoint.com account-name: global-abs-account account-key: global-abs-key host: global.sftp.host user: global-sftp-user password: global-sftp-pass sync-interval: 45s dbs: - path: /tmp/s3.sqlite replica: type: s3 bucket: s3-bucket - path: /tmp/abs.sqlite replica: type: abs bucket: abs-container - path: /tmp/sftp.sqlite replica: type: sftp path: /backup/path `[1:]), 0666); err != nil { t.Fatal(err) } config, err := main.ReadConfigFile(filename, true) if err != nil { t.Fatal(err) } expectedSyncInterval, _ := time.ParseDuration("45s") // Test S3 replica inherits S3-specific defaults s3Replica := config.DBs[0].Replica if got, want := s3Replica.AccessKeyID, "GLOBAL_S3_KEY"; got != want { t.Errorf("s3Replica.AccessKeyID=%v, want %v", got, want) } if got, want := s3Replica.SecretAccessKey, "GLOBAL_S3_SECRET"; got != want { t.Errorf("s3Replica.SecretAccessKey=%v, want %v", got, want) } if got, want := s3Replica.Region, "global-region"; got != want { t.Errorf("s3Replica.Region=%v, want %v", got, want) } if got, want := s3Replica.Endpoint, "global.endpoint.com"; got != want { t.Errorf("s3Replica.Endpoint=%v, want %v", got, want) } if s3Replica.SyncInterval == nil || *s3Replica.SyncInterval != expectedSyncInterval { t.Errorf("s3Replica.SyncInterval=%v, want %v", s3Replica.SyncInterval, expectedSyncInterval) } // Test ABS replica inherits ABS-specific defaults absReplica := config.DBs[1].Replica if got, want := absReplica.AccountName, "global-abs-account"; got != want { t.Errorf("absReplica.AccountName=%v, want %v", got, want) } if got, want := absReplica.AccountKey, "global-abs-key"; got != want { t.Errorf("absReplica.AccountKey=%v, want %v", got, want) } if absReplica.SyncInterval == nil || *absReplica.SyncInterval != expectedSyncInterval { t.Errorf("absReplica.SyncInterval=%v, want %v", absReplica.SyncInterval, expectedSyncInterval) } // Test SFTP replica inherits SFTP-specific defaults sftpReplica := config.DBs[2].Replica if got, want := sftpReplica.Host, "global.sftp.host"; got != want { t.Errorf("sftpReplica.Host=%v, want %v", got, want) } if got, want := sftpReplica.User, "global-sftp-user"; got != want { t.Errorf("sftpReplica.User=%v, want %v", got, want) } if got, want := sftpReplica.Password, "global-sftp-pass"; got != want { t.Errorf("sftpReplica.Password=%v, want %v", got, want) } if sftpReplica.SyncInterval == nil || *sftpReplica.SyncInterval != expectedSyncInterval { t.Errorf("sftpReplica.SyncInterval=%v, want %v", sftpReplica.SyncInterval, expectedSyncInterval) } }) } func TestStripSQLitePrefix(t *testing.T) { tests := []struct { name string input string want string }{ {"sqlite3 prefix", "sqlite3:///path/to/db.sqlite", "/path/to/db.sqlite"}, {"sqlite prefix", "sqlite:///path/to/db.sqlite", "/path/to/db.sqlite"}, {"sqlite3 relative path", "sqlite3://./data/db.sqlite", "./data/db.sqlite"}, {"sqlite relative path", "sqlite://./data/db.sqlite", "./data/db.sqlite"}, {"sqlite3 tilde path", "sqlite3://~/db.sqlite", "~/db.sqlite"}, {"sqlite tilde path", "sqlite://~/db.sqlite", "~/db.sqlite"}, {"no prefix", "/path/to/db.sqlite", "/path/to/db.sqlite"}, {"relative no prefix", "./data/db.sqlite", "./data/db.sqlite"}, {"tilde no prefix", "~/db.sqlite", "~/db.sqlite"}, {"empty string", "", ""}, {"sqlite3 windows path", "sqlite3://C:/data/db.sqlite", "C:/data/db.sqlite"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := main.StripSQLitePrefix(tt.input) if got != tt.want { t.Errorf("StripSQLitePrefix(%q) = %q, want %q", tt.input, got, tt.want) } }) } } func TestReadConfigFile_SQLiteConnectionString(t *testing.T) { t.Run("ConfigWithSQLitePrefix", func(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") filename := filepath.Join(dir, "litestream.yml") if err := os.WriteFile(filename, []byte(` dbs: - path: sqlite3://`+dbPath+` replicas: - url: file://`+filepath.Join(dir, "replica")+` `[1:]), 0666); err != nil { t.Fatal(err) } config, err := main.ReadConfigFile(filename, true) if err != nil { t.Fatalf("ReadConfigFile failed: %v", err) } if len(config.DBs) != 1 { t.Fatalf("expected 1 database, got %d", len(config.DBs)) } if got := config.DBs[0].Path; got != dbPath { t.Errorf("DBs[0].Path = %q, want %q", got, dbPath) } }) t.Run("ConfigWithSQLite3Prefix", func(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") filename := filepath.Join(dir, "litestream.yml") if err := os.WriteFile(filename, []byte(` dbs: - path: sqlite://`+dbPath+` replicas: - url: file://`+filepath.Join(dir, "replica")+` `[1:]), 0666); err != nil { t.Fatal(err) } config, err := main.ReadConfigFile(filename, true) if err != nil { t.Fatalf("ReadConfigFile failed: %v", err) } if len(config.DBs) != 1 { t.Fatalf("expected 1 database, got %d", len(config.DBs)) } if got := config.DBs[0].Path; got != dbPath { t.Errorf("DBs[0].Path = %q, want %q", got, dbPath) } }) } func TestX509FallbackRoots(t *testing.T) { if runtime.GOOS != "linux" { t.Skip("x509 fallback test requires Linux (macOS uses system keychain)") } if os.Getenv("GO_X509_FALLBACK_TEST") == "1" { pool, err := x509.SystemCertPool() if err != nil { t.Fatalf("SystemCertPool() error: %v", err) } if pool.Equal(x509.NewCertPool()) { t.Fatal("SystemCertPool() returned empty pool; x509roots/fallback not providing certificates") } return } cmd := exec.Command(os.Args[0], "-test.run=^TestX509FallbackRoots$", "-test.v") cmd.Env = []string{ "GO_X509_FALLBACK_TEST=1", "SSL_CERT_FILE=/nonexistent/cert.pem", "SSL_CERT_DIR=/nonexistent/certs", "HOME=" + t.TempDir(), } out, err := cmd.CombinedOutput() t.Logf("subprocess output:\n%s", out) if err != nil { t.Fatalf("x509 fallback roots verification failed: %v", err) } } func hasDBPath(dbs []*litestream.DB, path string) bool { for _, db := range dbs { if db.Path() == path { return true } } return false } func waitForCondition(timeout time.Duration, fn func() bool) bool { deadline := time.Now().Add(timeout) for { if fn() { return true } if time.Now().After(deadline) { return fn() } time.Sleep(50 * time.Millisecond) } } ================================================ FILE: cmd/litestream/main_windows.go ================================================ //go:build windows package main import ( "context" "io" "log/slog" "os" "os/signal" "golang.org/x/sys/windows" "golang.org/x/sys/windows/svc" "golang.org/x/sys/windows/svc/eventlog" ) const defaultConfigPath = `C:\Litestream\litestream.yml` // serviceName is the Windows Service name. const serviceName = "Litestream" // isWindowsService returns true if currently executing within a Windows service. func isWindowsService() (bool, error) { return svc.IsWindowsService() } func runWindowsService(ctx context.Context) error { // Attempt to install new log service. This will fail if already installed. // We don't log the error because we don't have anywhere to log until we open the log. _ = eventlog.InstallAsEventCreate(serviceName, eventlog.Error|eventlog.Warning|eventlog.Info) elog, err := eventlog.Open(serviceName) if err != nil { return err } defer elog.Close() // Set eventlog as log writer while running. slog.SetDefault(slog.New(slog.NewTextHandler((*eventlogWriter)(elog), nil))) defer slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, nil))) slog.Info("Litestream service starting") if err := svc.Run(serviceName, &windowsService{ctx: ctx}); err != nil { return errStop } slog.Info("Litestream service stopped") return nil } // windowsService is an interface adapter for svc.Handler. type windowsService struct { ctx context.Context } func (s *windowsService) Execute(args []string, r <-chan svc.ChangeRequest, statusCh chan<- svc.Status) (svcSpecificEC bool, exitCode uint32) { var err error // Notify Windows that the service is starting up. statusCh <- svc.Status{State: svc.StartPending} // Instantiate replication command and load configuration. c := NewReplicateCommand() if c.Config, err = ReadConfigFile(DefaultConfigPath(), true); err != nil { slog.Error("cannot load configuration", "error", err) return true, 1 } // Execute replication command. if err := c.Run(s.ctx); err != nil { slog.Error("cannot replicate", "error", err) statusCh <- svc.Status{State: svc.StopPending} return true, 2 } // Notify Windows that the service is now running. statusCh <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop} for { select { case req := <-r: switch req.Cmd { case svc.Stop: c.Close(s.ctx) statusCh <- svc.Status{State: svc.StopPending} return false, windows.NO_ERROR case svc.Interrogate: statusCh <- req.CurrentStatus default: slog.Error("Litestream service received unexpected change request", "cmd", req.Cmd) } } } } // Ensure implementation implements io.Writer interface. var _ io.Writer = (*eventlogWriter)(nil) // eventlogWriter is an adapter for using eventlog.Log as an io.Writer. type eventlogWriter eventlog.Log func (w *eventlogWriter) Write(p []byte) (n int, err error) { elog := (*eventlog.Log)(w) return 0, elog.Info(1, string(p)) } func signalChan() <-chan os.Signal { ch := make(chan os.Signal, 2) signal.Notify(ch, os.Interrupt) return ch } ================================================ FILE: cmd/litestream/mcp.go ================================================ package main import ( "bufio" "context" "log/slog" "net/http" "os/exec" "strconv" "strings" "time" "github.com/MadAppGang/httplog" "github.com/mark3labs/mcp-go/mcp" "github.com/mark3labs/mcp-go/server" ) type MCPServer struct { ctx context.Context mux *http.ServeMux httpServer *http.Server configPath string } func NewMCP(ctx context.Context, configPath string) (*MCPServer, error) { s := &MCPServer{ ctx: ctx, configPath: configPath, } mcpServer := server.NewMCPServer( "Litestream MCP Server", "1.0.0", server.WithToolCapabilities(false), server.WithRecovery(), server.WithLogging(), ) // Add the tools to the server mcpServer.AddTool(InfoTool(configPath)) mcpServer.AddTool(DatabasesTool(configPath)) mcpServer.AddTool(RestoreTool(configPath)) mcpServer.AddTool(LTXTool(configPath)) mcpServer.AddTool(VersionTool()) mcpServer.AddTool(StatusTool(configPath)) mcpServer.AddTool(ResetTool(configPath)) s.mux = http.NewServeMux() s.mux.Handle("/", httplog.Logger(server.NewStreamableHTTPServer(mcpServer))) return s, nil } func (s *MCPServer) Start(addr string) { s.httpServer = &http.Server{ Addr: addr, Handler: s.mux, ReadHeaderTimeout: 30 * time.Second, } go func() { slog.Info("Starting MCP Streamable HTTP server", "addr", addr) if err := s.httpServer.ListenAndServe(); err != nil { slog.Error("MCP server error", "error", err) } }() } func (s *MCPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.mux.ServeHTTP(w, r) } // Close attempts to gracefully shutdown the server. func (s *MCPServer) Close() error { ctx, cancel := context.WithTimeout(s.ctx, 10*time.Second) defer cancel() return s.httpServer.Shutdown(ctx) } // isReplicaURL returns true if the path looks like a replica URL (s3://, gs://, etc.) // rather than a local database path. The CLI rejects -config when using replica URLs. func isReplicaURL(path string) bool { return strings.Contains(path, "://") } func DatabasesTool(configPath string) (mcp.Tool, server.ToolHandlerFunc) { tool := mcp.NewTool("litestream_databases", mcp.WithDescription("List databases and their replicas as defined in the Litestream config file. The default path is /etc/litestream.yml but is not required."), mcp.WithString("config", mcp.Description("Path to the Litestream config file. Optional.")), ) return tool, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { args := []string{"databases"} config := configPath if configVal, err := req.RequireString("config"); err == nil { config = configVal } args = append(args, "-config", config) cmd := exec.CommandContext(ctx, "litestream", args...) output, err := cmd.CombinedOutput() if err != nil { return mcp.NewToolResultError(strings.TrimSpace(string(output)) + ": " + err.Error()), nil } return mcp.NewToolResultText(string(output)), nil } } func InfoTool(configPath string) (mcp.Tool, server.ToolHandlerFunc) { tool := mcp.NewTool("litestream_info", mcp.WithDescription("Get a comprehensive summary of Litestream's current status including databases, LTX files, and version information."), mcp.WithString("config", mcp.Description("Path to the Litestream config file. Optional.")), ) return tool, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { var summary strings.Builder summary.WriteString("=== Litestream Status Report ===\n\n") // Get version info versionCmd := exec.CommandContext(ctx, "litestream", "version") versionOutput, err := versionCmd.CombinedOutput() if err != nil { slog.Error("Failed to get version info", "error", err) return mcp.NewToolResultError("Failed to get version info: " + err.Error()), nil } summary.WriteString("Version Information:\n") summary.WriteString(string(versionOutput)) summary.WriteString("\n") // Get databases info args := []string{"databases"} config := configPath if configVal, err := req.RequireString("config"); err == nil { config = configVal } summary.WriteString("Current Config Path:\n") summary.WriteString(config + "\n\n") args = append(args, "-config", config) dbCmd := exec.CommandContext(ctx, "litestream", args...) dbOutput, err := dbCmd.CombinedOutput() if err != nil { slog.Error("Failed to get databases info", "error", err) return mcp.NewToolResultError("Failed to get databases info: " + err.Error()), nil } summary.WriteString("Databases:\n") summary.WriteString(string(dbOutput)) summary.WriteString("\n") // Parse database paths from output scanner := bufio.NewScanner(strings.NewReader(string(dbOutput))) // Skip header line scanner.Scan() var dbPaths []string for scanner.Scan() { fields := strings.Fields(scanner.Text()) if len(fields) > 0 { dbPaths = append(dbPaths, fields[0]) } } // Get LTX files info for each database summary.WriteString("LTX Files:\n") for _, dbPath := range dbPaths { ltxArgs := []string{"ltx"} if config != "" { ltxArgs = append(ltxArgs, "-config", config) } ltxArgs = append(ltxArgs, dbPath) ltxCmd := exec.CommandContext(ctx, "litestream", ltxArgs...) ltxOutput, err := ltxCmd.CombinedOutput() if err != nil { summary.WriteString("Failed to get LTX files for " + dbPath + ": " + err.Error() + "\n") summary.WriteString(string(ltxOutput)) continue } summary.WriteString("Database: " + dbPath + "\n") summary.WriteString(string(ltxOutput)) summary.WriteString("\n") } return mcp.NewToolResultText(summary.String()), nil } } func RestoreTool(configPath string) (mcp.Tool, server.ToolHandlerFunc) { tool := mcp.NewTool("litestream_restore", mcp.WithDescription("Restore a database from a Litestream replica."), mcp.WithString("path", mcp.Required(), mcp.Description("Database path or replica URL.")), mcp.WithString("o", mcp.Description("Output path for the restored database. Optional.")), mcp.WithString("config", mcp.Description("Path to the Litestream config file. Optional.")), mcp.WithString("txid", mcp.Description("Restore up to a specific transaction ID. Optional.")), mcp.WithString("timestamp", mcp.Description("Restore to a specific point-in-time (RFC3339). Optional.")), mcp.WithString("parallelism", mcp.Description("Number of WAL files to download in parallel. Optional.")), mcp.WithBoolean("if_db_not_exists", mcp.Description("Return 0 if the database already exists. Optional.")), mcp.WithBoolean("if_replica_exists", mcp.Description("Return 0 if no backups found. Optional.")), ) return tool, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { args := []string{"restore"} if o, err := req.RequireString("o"); err == nil { args = append(args, "-o", o) } // Get path first to determine if it's a replica URL path, _ := req.RequireString("path") // Only add -config for database paths, not replica URLs // The CLI rejects -config when restoring from a replica URL if !isReplicaURL(path) { config := configPath if configVal, err := req.RequireString("config"); err == nil { config = configVal } if config != "" { args = append(args, "-config", config) } } if txid, err := req.RequireString("txid"); err == nil { args = append(args, "-txid", txid) } if timestamp, err := req.RequireString("timestamp"); err == nil { args = append(args, "-timestamp", timestamp) } if parallelism, err := req.RequireString("parallelism"); err == nil { args = append(args, "-parallelism", parallelism) } if ifDBNotExists, err := req.RequireBool("if_db_not_exists"); err == nil { args = append(args, "-if-db-not-exists", strconv.FormatBool(ifDBNotExists)) } if ifReplicaExists, err := req.RequireBool("if_replica_exists"); err == nil { args = append(args, "-if-replica-exists", strconv.FormatBool(ifReplicaExists)) } if path != "" { args = append(args, path) } cmd := exec.CommandContext(ctx, "litestream", args...) output, err := cmd.CombinedOutput() if err != nil { return mcp.NewToolResultError(strings.TrimSpace(string(output)) + ": " + err.Error()), nil } return mcp.NewToolResultText(string(output)), nil } } func VersionTool() (mcp.Tool, server.ToolHandlerFunc) { tool := mcp.NewTool("litestream_version", mcp.WithDescription("Print the Litestream binary version."), ) return tool, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { cmd := exec.CommandContext(ctx, "litestream", "version") output, err := cmd.CombinedOutput() if err != nil { return mcp.NewToolResultError(strings.TrimSpace(string(output)) + ": " + err.Error()), nil } return mcp.NewToolResultText(string(output)), nil } } func LTXTool(configPath string) (mcp.Tool, server.ToolHandlerFunc) { tool := mcp.NewTool("litestream_ltx", mcp.WithDescription("List all LTX files for a database or replica URL."), mcp.WithString("path", mcp.Required(), mcp.Description("Database path or replica URL.")), mcp.WithString("config", mcp.Description("Path to the Litestream config file. Optional, ignored for replica URLs.")), ) return tool, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { args := []string{"ltx"} // Get path first to determine if it's a replica URL path, _ := req.RequireString("path") // Only add -config for database paths, not replica URLs // The CLI rejects -config when using a replica URL if !isReplicaURL(path) { config := configPath if configVal, err := req.RequireString("config"); err == nil { config = configVal } if config != "" { args = append(args, "-config", config) } } if path != "" { args = append(args, path) } cmd := exec.CommandContext(ctx, "litestream", args...) output, err := cmd.CombinedOutput() if err != nil { return mcp.NewToolResultError(strings.TrimSpace(string(output)) + ": " + err.Error()), nil } return mcp.NewToolResultText(string(output)), nil } } func StatusTool(configPath string) (mcp.Tool, server.ToolHandlerFunc) { tool := mcp.NewTool("litestream_status", mcp.WithDescription("Display replication status including database path, status, local transaction ID, and WAL size."), mcp.WithString("config", mcp.Description("Path to the Litestream config file. Optional.")), mcp.WithString("path", mcp.Description("Filter to a specific database path. Optional.")), ) return tool, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { args := []string{"status"} config := configPath if configVal, err := req.RequireString("config"); err == nil { config = configVal } args = append(args, "-config", config) if path, err := req.RequireString("path"); err == nil { args = append(args, path) } cmd := exec.CommandContext(ctx, "litestream", args...) output, err := cmd.CombinedOutput() if err != nil { return mcp.NewToolResultError(strings.TrimSpace(string(output)) + ": " + err.Error()), nil } return mcp.NewToolResultText(string(output)), nil } } func ResetTool(configPath string) (mcp.Tool, server.ToolHandlerFunc) { tool := mcp.NewTool("litestream_reset", mcp.WithDescription("Clear local Litestream state for a database. Removes local LTX files, forcing fresh snapshot on next sync. Database file is not modified."), mcp.WithString("path", mcp.Required(), mcp.Description("Database path to reset.")), mcp.WithString("config", mcp.Description("Path to the Litestream config file. Optional.")), ) return tool, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { args := []string{"reset"} config := configPath if configVal, err := req.RequireString("config"); err == nil { config = configVal } args = append(args, "-config", config) if path, err := req.RequireString("path"); err == nil { args = append(args, path) } cmd := exec.CommandContext(ctx, "litestream", args...) output, err := cmd.CombinedOutput() if err != nil { return mcp.NewToolResultError(strings.TrimSpace(string(output)) + ": " + err.Error()), nil } return mcp.NewToolResultText(string(output)), nil } } ================================================ FILE: cmd/litestream/register.go ================================================ package main import ( "bytes" "context" "encoding/json" "flag" "fmt" "io" "net" "net/http" "time" "github.com/benbjohnson/litestream" ) type RegisterCommand struct{} func (c *RegisterCommand) Run(ctx context.Context, args []string) error { fs := flag.NewFlagSet("litestream-register", flag.ContinueOnError) timeout := fs.Int("timeout", 30, "timeout in seconds") socketPath := fs.String("socket", "/var/run/litestream.sock", "control socket path") replicaFlag := fs.String("replica", "", "replica URL (e.g., s3://bucket/prefix, file:///backup/path)") fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } if fs.NArg() == 0 { return fmt.Errorf("database path required") } if fs.NArg() > 1 { return fmt.Errorf("too many arguments") } if *replicaFlag == "" { return fmt.Errorf("replica URL required (use -replica flag)") } if *timeout <= 0 { return fmt.Errorf("timeout must be greater than 0") } dbPath := fs.Arg(0) replicaURL := *replicaFlag // Create HTTP client that connects via Unix socket with timeout. clientTimeout := time.Duration(*timeout) * time.Second client := &http.Client{ Timeout: clientTimeout, Transport: &http.Transport{ DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { return net.DialTimeout("unix", *socketPath, clientTimeout) }, }, } req := litestream.RegisterDatabaseRequest{ Path: dbPath, ReplicaURL: replicaURL, } reqBody, err := json.Marshal(req) if err != nil { return fmt.Errorf("failed to marshal request: %w", err) } resp, err := client.Post("http://localhost/register", "application/json", bytes.NewReader(reqBody)) if err != nil { return fmt.Errorf("failed to connect to control socket: %w", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("failed to read response: %w", err) } if resp.StatusCode != http.StatusOK { var errResp litestream.ErrorResponse if err := json.Unmarshal(body, &errResp); err == nil && errResp.Error != "" { return fmt.Errorf("register failed: %s", errResp.Error) } return fmt.Errorf("register failed: %s", string(body)) } var result litestream.RegisterDatabaseResponse if err := json.Unmarshal(body, &result); err != nil { return fmt.Errorf("failed to parse response: %w", err) } output, err := json.MarshalIndent(result, "", " ") if err != nil { return fmt.Errorf("failed to format response: %w", err) } fmt.Println(string(output)) return nil } func (c *RegisterCommand) Usage() { fmt.Println(` usage: litestream register [OPTIONS] DB_PATH Register a database for replication. Arguments: DB_PATH Path to the SQLite database file. Options: -replica URL Replica destination URL (e.g., s3://bucket/prefix, file:///backup/path). Required. -timeout SECONDS Maximum time to wait in seconds (default: 30). -socket PATH Path to control socket (default: /var/run/litestream.sock). `[1:]) } ================================================ FILE: cmd/litestream/register_test.go ================================================ package main_test import ( "context" "path/filepath" "testing" "github.com/benbjohnson/litestream" main "github.com/benbjohnson/litestream/cmd/litestream" "github.com/benbjohnson/litestream/internal/testingutil" ) func TestRegisterCommand_Run(t *testing.T) { t.Run("MissingDBPath", func(t *testing.T) { cmd := &main.RegisterCommand{} err := cmd.Run(context.Background(), []string{"-socket", "/tmp/test.sock", "-replica", "file:///tmp/backup"}) if err == nil { t.Error("expected error for missing database path") } if err.Error() != "database path required" { t.Errorf("unexpected error: %v", err) } }) t.Run("MissingReplicaFlag", func(t *testing.T) { cmd := &main.RegisterCommand{} err := cmd.Run(context.Background(), []string{"-socket", "/tmp/test.sock", "/tmp/test.db"}) if err == nil { t.Error("expected error for missing replica flag") } if err.Error() != "replica URL required (use -replica flag)" { t.Errorf("unexpected error: %v", err) } }) t.Run("TooManyArguments", func(t *testing.T) { cmd := &main.RegisterCommand{} err := cmd.Run(context.Background(), []string{"-socket", "/tmp/test.sock", "-replica", "file:///tmp/backup", "/tmp/test.db", "extra"}) if err == nil { t.Error("expected error for too many arguments") } if err.Error() != "too many arguments" { t.Errorf("unexpected error: %v", err) } }) t.Run("InvalidTimeoutZero", func(t *testing.T) { cmd := &main.RegisterCommand{} err := cmd.Run(context.Background(), []string{"-timeout", "0", "-replica", "file:///tmp/backup", "/tmp/test.db"}) if err == nil { t.Error("expected error for zero timeout") } if err.Error() != "timeout must be greater than 0" { t.Errorf("unexpected error: %v", err) } }) t.Run("SocketConnectionError", func(t *testing.T) { cmd := &main.RegisterCommand{} err := cmd.Run(context.Background(), []string{"-socket", "/nonexistent/socket.sock", "-replica", "file:///tmp/backup", "/tmp/test.db"}) if err == nil { t.Error("expected error for socket connection failure") } }) t.Run("Success", func(t *testing.T) { store := litestream.NewStore(nil, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false if err := store.Open(context.Background()); err != nil { t.Fatal(err) } defer store.Close(context.Background()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) if err := server.Start(); err != nil { t.Fatal(err) } defer server.Close() // Create a temporary database file. db, sqldb := testingutil.MustOpenDBs(t) testingutil.MustCloseDBs(t, db, sqldb) dbPath := db.Path() // Create a temp directory for backup. backupDir := filepath.Join(t.TempDir(), "backup") cmd := &main.RegisterCommand{} err := cmd.Run(context.Background(), []string{"-socket", server.SocketPath, "-replica", "file://" + backupDir, dbPath}) if err != nil { t.Errorf("unexpected error: %v", err) } // Verify database was registered with store. if len(store.DBs()) != 1 { t.Errorf("expected 1 database in store, got %d", len(store.DBs())) } }) t.Run("AlreadyExists", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false if err := store.Open(context.Background()); err != nil { t.Fatal(err) } defer store.Close(context.Background()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) if err := server.Start(); err != nil { t.Fatal(err) } defer server.Close() // Create a temp directory for backup. backupDir := filepath.Join(t.TempDir(), "backup") cmd := &main.RegisterCommand{} err := cmd.Run(context.Background(), []string{"-socket", server.SocketPath, "-replica", "file://" + backupDir, db.Path()}) if err != nil { t.Errorf("unexpected error: %v", err) } // Still only 1 database - didn't register a duplicate. if len(store.DBs()) != 1 { t.Errorf("expected 1 database in store, got %d", len(store.DBs())) } }) } ================================================ FILE: cmd/litestream/replicate.go ================================================ package main import ( "context" "errors" "flag" "fmt" "log/slog" "net" "net/http" _ "net/http/pprof" "os" "os/exec" "strings" "github.com/mattn/go-shellwords" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/abs" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/gs" "github.com/benbjohnson/litestream/nats" "github.com/benbjohnson/litestream/oss" "github.com/benbjohnson/litestream/s3" "github.com/benbjohnson/litestream/sftp" ) // ReplicateCommand represents a command that continuously replicates SQLite databases. type ReplicateCommand struct { cmd *exec.Cmd // subcommand execCh chan error // subcommand error channel // One-shot replication flags once bool // replicate once and exit forceSnapshot bool // force snapshot to all replicas enforceRetention bool // enforce retention of old snapshots Config Config // MCP server MCP *MCPServer // Server for IPC control commands. Server *litestream.Server // Manages the set of databases & compaction levels. Store *litestream.Store // Directory monitors for dynamic database discovery. directoryMonitors []*DirectoryMonitor // Done channel for interrupt handling during shutdown. When closed, // the shutdown sync retry loop exits and any in-flight sync is cancelled. done <-chan struct{} } func NewReplicateCommand() *ReplicateCommand { return &ReplicateCommand{ execCh: make(chan error), } } // ParseFlags parses the CLI flags and loads the configuration file. func (c *ReplicateCommand) ParseFlags(_ context.Context, args []string) (err error) { fs := flag.NewFlagSet("litestream-replicate", flag.ContinueOnError) execFlag := fs.String("exec", "", "execute subcommand") logLevelFlag := fs.String("log-level", "", "log level (trace, debug, info, warn, error)") restoreIfDBNotExists := fs.Bool("restore-if-db-not-exists", false, "restore from replica if database doesn't exist") onceFlag := fs.Bool("once", false, "replicate once and exit") forceSnapshotFlag := fs.Bool("force-snapshot", false, "force snapshot when replicating once") enforceRetentionFlag := fs.Bool("enforce-retention", false, "enforce retention of old snapshots when replicating once") configPath, noExpandEnv := registerConfigFlag(fs) fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } // Load configuration or use CLI args to build db/replica. switch fs.NArg() { case 0: // No arguments provided, use config file if *configPath == "" { *configPath = DefaultConfigPath() } if c.Config, err = ReadConfigFile(*configPath, !*noExpandEnv); err != nil { return err } // Override log level if CLI flag provided (takes precedence over env var) if *logLevelFlag != "" { c.Config.Logging.Level = *logLevelFlag // Set env var so initLog sees CLI flag as highest priority os.Setenv("LOG_LEVEL", *logLevelFlag) logOutput := os.Stdout if c.Config.Logging.Stderr { logOutput = os.Stderr } initLog(logOutput, c.Config.Logging.Level, c.Config.Logging.Type) } case 1: // Only database path provided, missing replica URL return fmt.Errorf("must specify at least one replica URL for %s", fs.Arg(0)) default: // Database path and replica URLs provided via CLI if *configPath != "" { return fmt.Errorf("cannot specify a replica URL and the -config flag") } // Initialize config with defaults when using command-line arguments c.Config = DefaultConfig() logLevel := "INFO" if *logLevelFlag != "" { logLevel = *logLevelFlag // Set env var so initLog sees CLI flag as highest priority os.Setenv("LOG_LEVEL", *logLevelFlag) } c.Config.Logging.Level = logLevel initLog(os.Stdout, logLevel, "text") dbConfig := &DBConfig{ Path: fs.Arg(0), RestoreIfDBNotExists: *restoreIfDBNotExists, } for _, u := range fs.Args()[1:] { // Check if this looks like a flag that was placed after positional arguments if strings.HasPrefix(u, "-") { return fmt.Errorf("flag %q must be positioned before DB_PATH and REPLICA_URL arguments", u) } syncInterval := litestream.DefaultSyncInterval dbConfig.Replicas = append(dbConfig.Replicas, &ReplicaConfig{ URL: u, ReplicaSettings: ReplicaSettings{ SyncInterval: &syncInterval, }, }) } c.Config.DBs = []*DBConfig{dbConfig} } c.Config.ConfigPath = *configPath // Override config exec command, if specified. if *execFlag != "" { c.Config.Exec = *execFlag } // Apply restore-if-db-not-exists flag to all databases if specified. // This allows the CLI flag to work with config files. if *restoreIfDBNotExists { for _, dbConfig := range c.Config.DBs { dbConfig.RestoreIfDBNotExists = true } } // Set one-shot replication flags and validate their usage. c.once = *onceFlag c.forceSnapshot = *forceSnapshotFlag c.enforceRetention = *enforceRetentionFlag // Validate flag combinations. if c.once && c.Config.Exec != "" { return fmt.Errorf("cannot specify -once flag with -exec") } if c.forceSnapshot && !c.once { return fmt.Errorf("cannot specify -force-snapshot flag without -once") } if c.enforceRetention && !c.once { return fmt.Errorf("cannot specify -enforce-retention flag without -once") } return nil } // Run loads all databases specified in the configuration. func (c *ReplicateCommand) Run(ctx context.Context) (err error) { // Display version information. slog.Info("litestream", "version", Version, "level", c.Config.Logging.Level) // Start MCP server if enabled if c.Config.MCPAddr != "" { c.MCP, err = NewMCP(ctx, c.Config.ConfigPath) if err != nil { return err } go c.MCP.Start(c.Config.MCPAddr) } // Setup databases. if len(c.Config.DBs) == 0 { slog.Error("no databases specified in configuration") } // Attempt restore for databases that need it (before creating DB objects) for _, dbConfig := range c.Config.DBs { if dbConfig.RestoreIfDBNotExists && dbConfig.Path != "" { if err := c.restoreIfNeeded(ctx, dbConfig); err != nil { return err } } } var dbs []*litestream.DB var watchables []struct { config *DBConfig dbs []*litestream.DB } for _, dbConfig := range c.Config.DBs { // Handle directory configuration if dbConfig.Dir != "" { dirDbs, err := NewDBsFromDirectoryConfig(dbConfig) if err != nil { return err } dbs = append(dbs, dirDbs...) slog.Info("found databases in directory", "dir", dbConfig.Dir, "count", len(dirDbs), "watch", dbConfig.Watch) if dbConfig.Watch { watchables = append(watchables, struct { config *DBConfig dbs []*litestream.DB }{config: dbConfig, dbs: dirDbs}) } } else { // Handle single database configuration db, err := NewDBFromConfig(dbConfig) if err != nil { return err } dbs = append(dbs, db) } } levels := c.Config.CompactionLevels() c.Store = litestream.NewStore(dbs, levels) // Only override default snapshot interval if explicitly set in config if c.Config.Snapshot.Interval != nil { c.Store.SnapshotInterval = *c.Config.Snapshot.Interval } // Only override default snapshot retention if explicitly set in config if c.Config.Snapshot.Retention != nil { c.Store.SnapshotRetention = *c.Config.Snapshot.Retention } if c.Config.L0Retention != nil { c.Store.SetL0Retention(*c.Config.L0Retention) } if c.Config.L0RetentionCheckInterval != nil { c.Store.L0RetentionCheckInterval = *c.Config.L0RetentionCheckInterval } if c.Config.ShutdownSyncTimeout != nil { c.Store.SetShutdownSyncTimeout(*c.Config.ShutdownSyncTimeout) } if c.Config.ShutdownSyncInterval != nil { c.Store.SetShutdownSyncInterval(*c.Config.ShutdownSyncInterval) } if c.Config.VerifyCompaction { c.Store.SetVerifyCompaction(true) } if c.Config.Retention.Enabled != nil && !*c.Config.Retention.Enabled { c.Store.SetRetentionEnabled(false) } if c.Config.Validation.Interval != nil { c.Store.ValidationInterval = *c.Config.Validation.Interval } if c.done != nil { c.Store.SetDone(c.done) } if c.Config.HeartbeatURL != "" { interval := litestream.DefaultHeartbeatInterval if c.Config.HeartbeatInterval != nil { interval = *c.Config.HeartbeatInterval } c.Store.Heartbeat = litestream.NewHeartbeatClient(c.Config.HeartbeatURL, interval) } // Disable all background monitors when running once. // This must be done after config settings are applied. if c.once { c.Store.CompactionMonitorEnabled = false c.Store.L0RetentionCheckInterval = 0 for _, db := range dbs { db.MonitorInterval = 0 if db.Replica != nil { db.Replica.MonitorEnabled = false } } } if err := c.Store.Open(ctx); err != nil { return fmt.Errorf("cannot open store: %w", err) } if !c.Store.RetentionEnabled { slog.Warn("retention disabled; cloud provider lifecycle policies must handle retention", "hint", "idle databases that stop receiving writes will not generate new snapshots and may lose backup coverage if cloud retention expires") } // Start control server if socket is enabled if c.Config.Socket.Enabled { c.Server = litestream.NewServer(c.Store) c.Server.SocketPath = c.Config.Socket.Path c.Server.SocketPerms = c.Config.Socket.Permissions c.Server.PathExpander = expand c.Server.Version = Version if err := c.Server.Start(); err != nil { slog.Warn("failed to start control server", "error", err) } } for _, entry := range watchables { monitor, err := NewDirectoryMonitor(ctx, c.Store, entry.config, entry.dbs) if err != nil { for _, m := range c.directoryMonitors { m.Close() } if closeErr := c.Store.Close(ctx); closeErr != nil { slog.Error("failed to close store after monitor failure", "error", closeErr) } return fmt.Errorf("start directory monitor for %s: %w", entry.config.Dir, err) } c.directoryMonitors = append(c.directoryMonitors, monitor) } // Notify user that initialization is done. for _, db := range c.Store.DBs() { r := db.Replica slog.Info("initialized db", "path", db.Path()) slogWith := slog.With("type", r.Client.Type(), "sync-interval", r.SyncInterval) switch client := r.Client.(type) { case *file.ReplicaClient: slogWith.Info("replicating to", "path", client.Path()) case *s3.ReplicaClient: slogWith.Info("replicating to", "bucket", client.Bucket, "path", client.Path, "region", client.Region, "endpoint", client.Endpoint) case *gs.ReplicaClient: slogWith.Info("replicating to", "bucket", client.Bucket, "path", client.Path) case *abs.ReplicaClient: slogWith.Info("replicating to", "bucket", client.Bucket, "path", client.Path, "endpoint", client.Endpoint) case *sftp.ReplicaClient: slogWith.Info("replicating to", "host", client.Host, "user", client.User, "path", client.Path) case *nats.ReplicaClient: slogWith.Info("replicating to", "bucket", client.BucketName, "url", client.URL) case *oss.ReplicaClient: slogWith.Info("replicating to", "bucket", client.Bucket, "path", client.Path, "region", client.Region) default: slogWith.Info("replicating to") } } // Serve metrics over HTTP if enabled. if c.Config.Addr != "" { hostport := c.Config.Addr if host, port, _ := net.SplitHostPort(c.Config.Addr); port == "" { return fmt.Errorf("must specify port for bind address: %q", c.Config.Addr) } else if host == "" { hostport = net.JoinHostPort("localhost", port) } slog.Info("serving metrics on", "url", fmt.Sprintf("http://%s/metrics", hostport)) go func() { http.Handle("/metrics", promhttp.Handler()) if err := http.ListenAndServe(c.Config.Addr, nil); err != nil { slog.Error("cannot start metrics server", "error", err) } }() } // Parse exec commands args & start subprocess. if c.Config.Exec != "" { execArgs, err := shellwords.Parse(c.Config.Exec) if err != nil { return fmt.Errorf("cannot parse exec command: %w", err) } c.cmd = exec.CommandContext(ctx, execArgs[0], execArgs[1:]...) c.cmd.Env = os.Environ() c.cmd.Stdout = os.Stdout c.cmd.Stderr = os.Stderr if err := c.cmd.Start(); err != nil { return fmt.Errorf("cannot start exec command: %w", err) } go func() { c.execCh <- c.cmd.Wait() }() } else if c.once { // Run one-shot replication in a goroutine so the caller can wait on execCh. go c.runOnce(ctx) } return nil } // runOnce performs one-shot replication for all databases. // It syncs all databases, optionally takes snapshots, and enforces retention. func (c *ReplicateCommand) runOnce(ctx context.Context) { var err error defer func() { c.execCh <- err }() for _, db := range c.Store.DBs() { slog.Info("syncing database", "path", db.Path()) // Sync the database to process any pending WAL changes. if err = db.Sync(ctx); err != nil { err = fmt.Errorf("sync database %s: %w", db.Path(), err) return } // Sync the replica to upload any pending LTX files. if err = db.Replica.Sync(ctx); err != nil { err = fmt.Errorf("sync replica for %s: %w", db.Path(), err) return } // Force a snapshot if requested. if c.forceSnapshot { slog.Info("taking snapshot", "path", db.Path()) if _, err = db.Snapshot(ctx); err != nil { err = fmt.Errorf("snapshot %s: %w", db.Path(), err) return } } // Enforce retention if requested. if c.enforceRetention { slog.Info("enforcing retention", "path", db.Path()) if err = c.Store.EnforceSnapshotRetention(ctx, db); err != nil { err = fmt.Errorf("enforce retention for %s: %w", db.Path(), err) return } } } slog.Info("one-shot replication complete") } // Close closes all open databases. func (c *ReplicateCommand) Close(ctx context.Context) error { for _, monitor := range c.directoryMonitors { monitor.Close() } c.directoryMonitors = nil if c.Server != nil { if err := c.Server.Close(); err != nil { slog.Error("error closing control server", "error", err) } } if c.Store != nil { if err := c.Store.Close(ctx); err != nil { if errors.Is(err, litestream.ErrShutdownInterrupted) { slog.Warn("shutdown sync skipped by user interrupt", "error", err) } else { slog.Error("failed to close database", "error", err) } } } if c.Config.MCPAddr != "" && c.MCP != nil { if err := c.MCP.Close(); err != nil { slog.Error("error closing MCP server", "error", err) } } return nil } // SetDone sets the done channel used for interrupt handling during shutdown. // When the channel is closed, the shutdown sync retry loop exits. func (c *ReplicateCommand) SetDone(done <-chan struct{}) { c.done = done } // restoreIfNeeded restores a database from its replica if the database doesn't // exist and the RestoreIfDBNotExists option is enabled. If no backup exists // (first start scenario), it returns nil to allow fresh replication to begin. func (c *ReplicateCommand) restoreIfNeeded(ctx context.Context, dbConfig *DBConfig) error { dbPath, err := expand(dbConfig.Path) if err != nil { return err } // Skip if database already exists if _, err := os.Stat(dbPath); !os.IsNotExist(err) { slog.Info("database exists, skipping restore", "path", dbPath) return nil } // Get replica config (handles both Replica and Replicas fields) var rc *ReplicaConfig if dbConfig.Replica != nil { rc = dbConfig.Replica } else if len(dbConfig.Replicas) > 0 { rc = dbConfig.Replicas[0] } else { return fmt.Errorf("no replica configured for database: %s", dbPath) } // Create replica from config (nil db since we're just restoring) r, err := NewReplicaFromConfig(rc, nil) if err != nil { return fmt.Errorf("cannot create replica for restore: %w", err) } // Attempt restore opt := litestream.NewRestoreOptions() opt.OutputPath = dbPath slog.Info("attempting restore before replication", "path", dbPath) if err := r.Restore(ctx, opt); errors.Is(err, litestream.ErrTxNotAvailable) { // No backup exists yet (first start) - this is OK slog.Info("no backup found, starting fresh", "path", dbPath) return nil } else if err != nil { return fmt.Errorf("restore failed: %w", err) } slog.Info("restore completed", "path", dbPath) return nil } // Usage prints the help screen to STDOUT. func (c *ReplicateCommand) Usage() { fmt.Printf(` The replicate command starts a server to monitor & replicate databases. You can specify your database & replicas in a configuration file or you can replicate a single database file by specifying its path and its replicas in the command line arguments. Usage: litestream replicate [arguments] litestream replicate [arguments] DB_PATH REPLICA_URL [REPLICA_URL...] Arguments: -config PATH Specifies the configuration file. Defaults to %s -exec CMD Executes a subcommand. Litestream will exit when the child process exits. Useful for simple process management. -once Replicate once and exit. This performs a single sync of all databases and their replicas, then exits. Cannot be used with -exec. -force-snapshot Force a snapshot to be taken for all databases. Requires -once. This is useful for creating a complete backup before maintenance or when migrating databases between hosts. -enforce-retention Enforce retention policies for old snapshots. Requires -once. This removes snapshots that are older than the configured snapshot retention period. -log-level LEVEL Sets the log level. Overrides the config file setting. Valid values: trace, debug, info, warn, error -no-expand-env Disables environment variable expansion in configuration file. -restore-if-db-not-exists Restores the database from the replica if it doesn't exist. On first start with no backup, proceeds normally. `[1:], DefaultConfigPath()) } ================================================ FILE: cmd/litestream/replicate_test.go ================================================ package main_test import ( "context" "os" "strings" "testing" main "github.com/benbjohnson/litestream/cmd/litestream" ) func TestReplicateCommand_ParseFlags_OnceFlags(t *testing.T) { t.Run("OnceFlag", func(t *testing.T) { cmd := main.NewReplicateCommand() args := []string{"-once", "test.db", "file:///tmp/replica"} err := cmd.ParseFlags(context.Background(), args) if err != nil { t.Fatalf("unexpected error: %v", err) } }) t.Run("OnceWithForceSnapshot", func(t *testing.T) { cmd := main.NewReplicateCommand() args := []string{"-once", "-force-snapshot", "test.db", "file:///tmp/replica"} err := cmd.ParseFlags(context.Background(), args) if err != nil { t.Fatalf("unexpected error: %v", err) } }) t.Run("OnceWithEnforceRetention", func(t *testing.T) { cmd := main.NewReplicateCommand() args := []string{"-once", "-enforce-retention", "test.db", "file:///tmp/replica"} err := cmd.ParseFlags(context.Background(), args) if err != nil { t.Fatalf("unexpected error: %v", err) } }) t.Run("OnceWithAllFlags", func(t *testing.T) { cmd := main.NewReplicateCommand() args := []string{"-once", "-force-snapshot", "-enforce-retention", "test.db", "file:///tmp/replica"} err := cmd.ParseFlags(context.Background(), args) if err != nil { t.Fatalf("unexpected error: %v", err) } }) t.Run("ForceSnapshotRequiresOnce", func(t *testing.T) { cmd := main.NewReplicateCommand() args := []string{"-force-snapshot", "test.db", "file:///tmp/replica"} err := cmd.ParseFlags(context.Background(), args) if err == nil { t.Fatal("expected error when -force-snapshot is used without -once") } expectedError := "cannot specify -force-snapshot flag without -once" if !strings.Contains(err.Error(), expectedError) { t.Errorf("expected error message to contain %q, got %q", expectedError, err.Error()) } }) t.Run("EnforceRetentionRequiresOnce", func(t *testing.T) { cmd := main.NewReplicateCommand() args := []string{"-enforce-retention", "test.db", "file:///tmp/replica"} err := cmd.ParseFlags(context.Background(), args) if err == nil { t.Fatal("expected error when -enforce-retention is used without -once") } expectedError := "cannot specify -enforce-retention flag without -once" if !strings.Contains(err.Error(), expectedError) { t.Errorf("expected error message to contain %q, got %q", expectedError, err.Error()) } }) t.Run("OnceAndExecMutuallyExclusive", func(t *testing.T) { cmd := main.NewReplicateCommand() args := []string{"-once", "-exec", "echo test", "test.db", "file:///tmp/replica"} err := cmd.ParseFlags(context.Background(), args) if err == nil { t.Fatal("expected error when -once and -exec are both specified") } expectedError := "cannot specify -once flag with -exec" if !strings.Contains(err.Error(), expectedError) { t.Errorf("expected error message to contain %q, got %q", expectedError, err.Error()) } }) } func TestReplicateCommand_ParseFlags_FlagPositioning(t *testing.T) { t.Run("ExecFlagAfterPositionalArgs", func(t *testing.T) { cmd := main.NewReplicateCommand() // Test the scenario from issue #245: -exec flag after positional arguments args := []string{"test.db", "s3://bucket/test.db", "-exec", "echo test"} err := cmd.ParseFlags(context.Background(), args) if err == nil { t.Fatal("expected error when -exec flag is positioned after positional arguments") } expectedError := `flag "-exec" must be positioned before DB_PATH and REPLICA_URL arguments` if !strings.Contains(err.Error(), expectedError) { t.Errorf("expected error message to contain %q, got %q", expectedError, err.Error()) } }) t.Run("ExecFlagBeforePositionalArgs", func(t *testing.T) { cmd := main.NewReplicateCommand() // Test the correct usage: -exec flag before positional arguments args := []string{"-exec", "echo test", "test.db", "s3://bucket/test.db"} err := cmd.ParseFlags(context.Background(), args) if err != nil { t.Fatalf("unexpected error when -exec flag is positioned correctly: %v", err) } // Verify the exec command was set correctly if cmd.Config.Exec != "echo test" { t.Errorf("expected exec command to be %q, got %q", "echo test", cmd.Config.Exec) } }) t.Run("ConfigFlagAfterPositionalArgs", func(t *testing.T) { cmd := main.NewReplicateCommand() // Test other flags after positional arguments args := []string{"test.db", "s3://bucket/test.db", "-config", "/path/to/config"} err := cmd.ParseFlags(context.Background(), args) if err == nil { t.Fatal("expected error when -config flag is positioned after positional arguments") } expectedError := `flag "-config" must be positioned before DB_PATH and REPLICA_URL arguments` if !strings.Contains(err.Error(), expectedError) { t.Errorf("expected error message to contain %q, got %q", expectedError, err.Error()) } }) t.Run("MultipleFlags", func(t *testing.T) { cmd := main.NewReplicateCommand() // Test multiple flags in correct position args := []string{"-exec", "echo test", "-no-expand-env", "test.db", "s3://bucket/test.db"} err := cmd.ParseFlags(context.Background(), args) if err != nil { t.Fatalf("unexpected error with multiple flags positioned correctly: %v", err) } // Verify the exec command was set correctly if cmd.Config.Exec != "echo test" { t.Errorf("expected exec command to be %q, got %q", "echo test", cmd.Config.Exec) } }) t.Run("OnlyDatabasePathProvided", func(t *testing.T) { cmd := main.NewReplicateCommand() // Test with only database path (should error but for different reason) args := []string{"test.db"} err := cmd.ParseFlags(context.Background(), args) if err == nil { t.Fatal("expected error when only database path is provided without replica URL") } // Should get the "must specify at least one replica URL" error, not the flag positioning error expectedError := "must specify at least one replica URL" if !strings.Contains(err.Error(), expectedError) { t.Errorf("expected error message to contain %q, got %q", expectedError, err.Error()) } }) } func TestReplicateCommand_ParseFlags_LogLevel(t *testing.T) { t.Run("LogLevelWithCLIArgs", func(t *testing.T) { cmd := main.NewReplicateCommand() args := []string{"-log-level", "debug", "test.db", "file:///tmp/replica"} err := cmd.ParseFlags(context.Background(), args) if err != nil { t.Fatalf("unexpected error: %v", err) } if cmd.Config.Logging.Level != "debug" { t.Errorf("expected log level to be %q, got %q", "debug", cmd.Config.Logging.Level) } }) t.Run("LogLevelTrace", func(t *testing.T) { cmd := main.NewReplicateCommand() args := []string{"-log-level", "trace", "test.db", "file:///tmp/replica"} err := cmd.ParseFlags(context.Background(), args) if err != nil { t.Fatalf("unexpected error: %v", err) } if cmd.Config.Logging.Level != "trace" { t.Errorf("expected log level to be %q, got %q", "trace", cmd.Config.Logging.Level) } }) t.Run("LogLevelError", func(t *testing.T) { cmd := main.NewReplicateCommand() args := []string{"-log-level", "error", "test.db", "file:///tmp/replica"} err := cmd.ParseFlags(context.Background(), args) if err != nil { t.Fatalf("unexpected error: %v", err) } if cmd.Config.Logging.Level != "error" { t.Errorf("expected log level to be %q, got %q", "error", cmd.Config.Logging.Level) } }) t.Run("LogLevelDefaultsToInfo", func(t *testing.T) { cmd := main.NewReplicateCommand() args := []string{"test.db", "file:///tmp/replica"} err := cmd.ParseFlags(context.Background(), args) if err != nil { t.Fatalf("unexpected error: %v", err) } if cmd.Config.Logging.Level != "INFO" { t.Errorf("expected log level to default to %q, got %q", "INFO", cmd.Config.Logging.Level) } }) t.Run("LogLevelWithOtherFlags", func(t *testing.T) { cmd := main.NewReplicateCommand() args := []string{"-log-level", "warn", "-once", "test.db", "file:///tmp/replica"} err := cmd.ParseFlags(context.Background(), args) if err != nil { t.Fatalf("unexpected error: %v", err) } if cmd.Config.Logging.Level != "warn" { t.Errorf("expected log level to be %q, got %q", "warn", cmd.Config.Logging.Level) } }) t.Run("LogLevelAfterPositionalArgs", func(t *testing.T) { cmd := main.NewReplicateCommand() args := []string{"test.db", "file:///tmp/replica", "-log-level", "debug"} err := cmd.ParseFlags(context.Background(), args) if err == nil { t.Fatal("expected error when -log-level flag is positioned after positional arguments") } expectedError := `flag "-log-level" must be positioned before DB_PATH and REPLICA_URL arguments` if !strings.Contains(err.Error(), expectedError) { t.Errorf("expected error message to contain %q, got %q", expectedError, err.Error()) } }) t.Run("LogLevelFlagOverridesEnvVar", func(t *testing.T) { // Set LOG_LEVEL env var to a different value oldEnv := os.Getenv("LOG_LEVEL") os.Setenv("LOG_LEVEL", "error") defer func() { if oldEnv == "" { os.Unsetenv("LOG_LEVEL") } else { os.Setenv("LOG_LEVEL", oldEnv) } }() cmd := main.NewReplicateCommand() args := []string{"-log-level", "debug", "test.db", "file:///tmp/replica"} err := cmd.ParseFlags(context.Background(), args) if err != nil { t.Fatalf("unexpected error: %v", err) } // CLI flag should take precedence, setting LOG_LEVEL env var to "debug" if got := os.Getenv("LOG_LEVEL"); got != "debug" { t.Errorf("expected LOG_LEVEL env var to be %q (CLI flag), got %q", "debug", got) } if cmd.Config.Logging.Level != "debug" { t.Errorf("expected config log level to be %q, got %q", "debug", cmd.Config.Logging.Level) } }) } ================================================ FILE: cmd/litestream/reset.go ================================================ package main import ( "context" "flag" "fmt" "os" "path/filepath" "github.com/benbjohnson/litestream" ) // ResetCommand is a command for resetting local Litestream state for a database. type ResetCommand struct{} // Run executes the command. func (c *ResetCommand) Run(ctx context.Context, args []string) (err error) { fs := flag.NewFlagSet("litestream-reset", flag.ContinueOnError) configPath, noExpandEnv := registerConfigFlag(fs) fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } // Validate arguments - need exactly one database path if fs.NArg() == 0 { return fmt.Errorf("database path required") } else if fs.NArg() > 1 { return fmt.Errorf("too many arguments") } dbPath := fs.Arg(0) // Make absolute if needed if !filepath.IsAbs(dbPath) { if dbPath, err = filepath.Abs(dbPath); err != nil { return err } } // Load configuration to find the database (if config exists) var dbConfig *DBConfig if *configPath != "" { config, configErr := ReadConfigFile(*configPath, !*noExpandEnv) if configErr != nil { return fmt.Errorf("cannot read config: %w", configErr) } // Find database config for _, dbc := range config.DBs { expandedPath := dbc.Path if !filepath.IsAbs(expandedPath) { expandedPath, _ = filepath.Abs(expandedPath) } if expandedPath == dbPath { dbConfig = dbc break } } } // If no config found, check if database file exists if _, err := os.Stat(dbPath); os.IsNotExist(err) { return fmt.Errorf("database does not exist: %s", dbPath) } else if err != nil { return fmt.Errorf("cannot access database: %w", err) } // Create DB instance var db *litestream.DB if dbConfig != nil { db, err = NewDBFromConfig(dbConfig) if err != nil { return fmt.Errorf("cannot create database from config: %w", err) } } else { db = litestream.NewDB(dbPath) } // Check if meta path exists metaPath := db.MetaPath() if _, err := os.Stat(metaPath); os.IsNotExist(err) { fmt.Printf("No local state to reset for %s\n", dbPath) fmt.Printf("Meta directory does not exist: %s\n", metaPath) return nil } // Perform the reset fmt.Printf("Resetting local Litestream state for: %s\n", dbPath) fmt.Printf("Removing: %s\n", db.LTXDir()) if err := db.ResetLocalState(ctx); err != nil { return fmt.Errorf("reset failed: %w", err) } fmt.Println("Reset complete. Next replication sync will create a fresh snapshot.") return nil } // Usage prints the help screen to STDOUT. func (c *ResetCommand) Usage() { fmt.Printf(` The reset command clears local Litestream state for a database. This is useful for recovering from corrupted or missing LTX files. The reset removes local LTX files from the metadata directory, forcing Litestream to create a fresh snapshot on the next sync. The database file itself is not modified. Usage: litestream reset [arguments] Arguments: -config PATH Specifies the configuration file. Defaults to %s -no-expand-env Disables environment variable expansion in configuration file. Examples: # Reset local state for a specific database litestream reset /path/to/database.db # Reset using a specific configuration file litestream reset -config /etc/litestream.yml /path/to/database.db `[1:], DefaultConfigPath(), ) } ================================================ FILE: cmd/litestream/restore.go ================================================ package main import ( "context" "errors" "flag" "fmt" "log/slog" "os" "strconv" "time" "github.com/benbjohnson/litestream" ) // RestoreCommand represents a command to restore a database from a backup. type RestoreCommand struct{} // Run executes the command. func (c *RestoreCommand) Run(ctx context.Context, args []string) (err error) { opt := litestream.NewRestoreOptions() fs := flag.NewFlagSet("litestream-restore", flag.ContinueOnError) configPath, noExpandEnv := registerConfigFlag(fs) fs.StringVar(&opt.OutputPath, "o", "", "output path") fs.Var((*txidVar)(&opt.TXID), "txid", "transaction ID") fs.IntVar(&opt.Parallelism, "parallelism", opt.Parallelism, "parallelism") ifDBNotExists := fs.Bool("if-db-not-exists", false, "") ifReplicaExists := fs.Bool("if-replica-exists", false, "") timestampStr := fs.String("timestamp", "", "timestamp") fs.BoolVar(&opt.Follow, "f", false, "follow mode") fs.DurationVar(&opt.FollowInterval, "follow-interval", opt.FollowInterval, "polling interval for follow mode") integrityCheck := fs.String("integrity-check", "none", "post-restore integrity check: none, quick, or full") fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } else if fs.NArg() == 0 || fs.Arg(0) == "" { return fmt.Errorf("database path or replica URL required") } else if fs.NArg() > 1 { return fmt.Errorf("too many arguments") } initLog(os.Stdout, "INFO", "text") // When follow mode is enabled, set up signal handling so Ctrl+C stops // the follow loop cleanly. if opt.Follow { ch := signalChan() cancelCtx, cancel := context.WithCancel(ctx) go func() { select { case <-ch: cancel() case <-cancelCtx.Done(): } }() ctx = cancelCtx defer cancel() } switch *integrityCheck { case "none": opt.IntegrityCheck = litestream.IntegrityCheckNone case "quick": opt.IntegrityCheck = litestream.IntegrityCheckQuick case "full": opt.IntegrityCheck = litestream.IntegrityCheckFull default: return fmt.Errorf("invalid -integrity-check value: %s", *integrityCheck) } // Parse timestamp, if specified. if *timestampStr != "" { if opt.Timestamp, err = time.Parse(time.RFC3339, *timestampStr); err != nil { return errors.New("invalid -timestamp, must specify in ISO 8601 format (e.g. 2000-01-01T00:00:00Z)") } } // Determine replica to restore from. var r *litestream.Replica if litestream.IsURL(fs.Arg(0)) { if *configPath != "" { return fmt.Errorf("cannot specify a replica URL and the -config flag") } if r, err = c.loadFromURL(ctx, fs.Arg(0), *ifDBNotExists, &opt); errors.Is(err, errSkipDBExists) { slog.Info("database already exists, skipping") return nil } else if err != nil { return err } } else { if *configPath == "" { *configPath = DefaultConfigPath() } if r, err = c.loadFromConfig(ctx, fs.Arg(0), *configPath, !*noExpandEnv, *ifDBNotExists, &opt); errors.Is(err, errSkipDBExists) { slog.Info("database already exists, skipping") return nil } else if err != nil { return err } } if err := r.Restore(ctx, opt); errors.Is(err, litestream.ErrTxNotAvailable) { if *ifReplicaExists { slog.Info("no matching backups found") return nil } return fmt.Errorf("no matching backup files available") } else if err != nil { return err } return nil } // loadFromURL creates a replica & updates the restore options from a replica URL. func (c *RestoreCommand) loadFromURL(ctx context.Context, replicaURL string, ifDBNotExists bool, opt *litestream.RestoreOptions) (*litestream.Replica, error) { if opt.OutputPath == "" { return nil, fmt.Errorf("output path required") } // Exit successfully if the output file already exists. if _, err := os.Stat(opt.OutputPath); !os.IsNotExist(err) && ifDBNotExists { return nil, errSkipDBExists } syncInterval := litestream.DefaultSyncInterval r, err := NewReplicaFromConfig(&ReplicaConfig{ URL: replicaURL, ReplicaSettings: ReplicaSettings{ SyncInterval: &syncInterval, }, }, nil) if err != nil { return nil, err } _, err = r.CalcRestoreTarget(ctx, *opt) return r, err } // loadFromConfig returns a replica & updates the restore options from a DB reference. func (c *RestoreCommand) loadFromConfig(_ context.Context, dbPath, configPath string, expandEnv, ifDBNotExists bool, opt *litestream.RestoreOptions) (*litestream.Replica, error) { // Load configuration. config, err := ReadConfigFile(configPath, expandEnv) if err != nil { return nil, err } // Lookup database from configuration file by path. if dbPath, err = expand(dbPath); err != nil { return nil, err } dbConfig := config.DBConfig(dbPath) if dbConfig == nil { return nil, fmt.Errorf("database not found in config: %s", dbPath) } db, err := NewDBFromConfig(dbConfig) if err != nil { return nil, err } // Restore into original database path if not specified. if opt.OutputPath == "" { opt.OutputPath = dbPath } // Exit successfully if the output file already exists. if _, err := os.Stat(opt.OutputPath); !os.IsNotExist(err) && ifDBNotExists { return nil, errSkipDBExists } return db.Replica, nil } // Usage prints the help screen to STDOUT. func (c *RestoreCommand) Usage() { fmt.Printf(` The restore command recovers a database from a previous snapshot and WAL. Usage: litestream restore [arguments] DB_PATH litestream restore [arguments] REPLICA_URL Arguments: -config PATH Specifies the configuration file. Defaults to %s -no-expand-env Disables environment variable expansion in configuration file. -txid TXID Restore up to a specific hex-encoded transaction ID (inclusive). Defaults to use the highest available transaction. -timestamp TIMESTAMP Restore to a specific point-in-time. Defaults to use the latest available backup. -o PATH Output path of the restored database. Defaults to original DB path. -if-db-not-exists Returns exit code of 0 if the database already exists. -if-replica-exists Returns exit code of 0 if no backups found. -f Follow mode. After restoring, continuously poll for and apply new changes. Similar to tail -f. The restored database should only be opened in read-only mode by consumers. Cannot be used with -txid or -timestamp. -follow-interval DURATION Polling interval for follow mode. Defaults to 1s. -parallelism NUM Determines the number of WAL files downloaded in parallel. Defaults to `+strconv.Itoa(litestream.DefaultRestoreParallelism)+`. -integrity-check MODE Run a post-restore integrity check on the database. MODE is one of: none, quick, full. Defaults to none. Examples: # Restore latest replica for database to original location. $ litestream restore /path/to/db # Restore replica for database to a given point in time. $ litestream restore -timestamp 2020-01-01T00:00:00Z /path/to/db # Restore latest replica for database to new /tmp directory $ litestream restore -o /tmp/db /path/to/db # Restore database from S3 replica URL. $ litestream restore -o /tmp/db s3://mybucket/db # Continuously restore (follow) a database from a replica. $ litestream restore -f -o /tmp/read-replica.db s3://mybucket/db `[1:], DefaultConfigPath(), ) } var errSkipDBExists = errors.New("database already exists, skipping") ================================================ FILE: cmd/litestream/restore_test.go ================================================ package main import ( "flag" "testing" "time" litestream "github.com/benbjohnson/litestream" ) func TestRestoreCommand_FollowIntervalFlag(t *testing.T) { tests := []struct { name string args []string wantVal time.Duration wantErr bool }{ { name: "Default", args: []string{"/tmp/db"}, wantVal: time.Second, }, { name: "CustomValue", args: []string{"-follow-interval", "500ms", "/tmp/db"}, wantVal: 500 * time.Millisecond, }, { name: "LongerInterval", args: []string{"-follow-interval", "5s", "/tmp/db"}, wantVal: 5 * time.Second, }, { name: "InvalidDuration", args: []string{"-follow-interval", "notaduration", "/tmp/db"}, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { opt := litestream.NewRestoreOptions() fs := flag.NewFlagSet("test", flag.ContinueOnError) fs.DurationVar(&opt.FollowInterval, "follow-interval", opt.FollowInterval, "polling interval for follow mode") err := fs.Parse(tt.args) if tt.wantErr { if err == nil { t.Fatal("expected error, got nil") } return } if err != nil { t.Fatalf("unexpected error: %v", err) } if opt.FollowInterval != tt.wantVal { t.Fatalf("FollowInterval=%v, want %v", opt.FollowInterval, tt.wantVal) } }) } } ================================================ FILE: cmd/litestream/start.go ================================================ package main import ( "bytes" "context" "encoding/json" "flag" "fmt" "io" "net" "net/http" "time" "github.com/benbjohnson/litestream" ) // StartCommand represents the command to start replication for a database. type StartCommand struct{} // Run executes the start command. func (c *StartCommand) Run(ctx context.Context, args []string) error { fs := flag.NewFlagSet("litestream-start", flag.ContinueOnError) timeout := fs.Int("timeout", 30, "timeout in seconds") socketPath := fs.String("socket", "/var/run/litestream.sock", "control socket path") fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } if fs.NArg() == 0 { return fmt.Errorf("database path required") } if fs.NArg() > 1 { return fmt.Errorf("too many arguments") } dbPath := fs.Arg(0) // Create HTTP client that connects via Unix socket with timeout clientTimeout := time.Duration(*timeout) * time.Second client := &http.Client{ Timeout: clientTimeout, Transport: &http.Transport{ DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { return net.DialTimeout("unix", *socketPath, clientTimeout) }, }, } req := litestream.StartRequest{ Path: dbPath, Timeout: *timeout, } reqBody, err := json.Marshal(req) if err != nil { return fmt.Errorf("failed to marshal request: %w", err) } resp, err := client.Post("http://localhost/start", "application/json", bytes.NewReader(reqBody)) if err != nil { return fmt.Errorf("failed to connect to control socket: %w", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("failed to read response: %w", err) } if resp.StatusCode != http.StatusOK { var errResp litestream.ErrorResponse if err := json.Unmarshal(body, &errResp); err == nil && errResp.Error != "" { return fmt.Errorf("start failed: %s", errResp.Error) } return fmt.Errorf("start failed: %s", string(body)) } var result litestream.StartResponse if err := json.Unmarshal(body, &result); err != nil { return fmt.Errorf("failed to parse response: %w", err) } output, err := json.MarshalIndent(result, "", " ") if err != nil { return fmt.Errorf("failed to format response: %w", err) } fmt.Println(string(output)) return nil } // Usage prints the help text for the start command. func (c *StartCommand) Usage() { fmt.Println(` usage: litestream start [OPTIONS] DB_PATH Start replication for a database. Options: -timeout SECONDS Maximum time to wait in seconds (default: 30). -socket PATH Path to control socket (default: /var/run/litestream.sock). `[1:]) } ================================================ FILE: cmd/litestream/status.go ================================================ package main import ( "context" "flag" "fmt" "os" "text/tabwriter" "github.com/dustin/go-humanize" "github.com/benbjohnson/litestream" ) // StatusCommand is a command for displaying replication status. type StatusCommand struct{} // Run executes the command. func (c *StatusCommand) Run(ctx context.Context, args []string) (err error) { fs := flag.NewFlagSet("litestream-status", flag.ContinueOnError) configPath, noExpandEnv := registerConfigFlag(fs) fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } // Load configuration. if *configPath == "" { *configPath = DefaultConfigPath() } config, err := ReadConfigFile(*configPath, !*noExpandEnv) if err != nil { return err } // If a specific database path is provided, filter to just that one. var filterPath string if fs.NArg() > 0 { filterPath = fs.Arg(0) } // Print status for each database. w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0) defer w.Flush() fmt.Fprintln(w, "database\tstatus\tlocal txid\twal size") for _, dbConfig := range config.DBs { db, err := NewDBFromConfig(dbConfig) if err != nil { return err } // Filter if path specified. if filterPath != "" && db.Path() != filterPath { continue } status := c.getDBStatus(db) fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", db.Path(), status.Status, status.LocalTXID, status.WALSize, ) } return nil } // DBStatus holds the status information for a single database. type DBStatus struct { Status string LocalTXID string WALSize string } // getDBStatus gathers status information for a database. func (c *StatusCommand) getDBStatus(db *litestream.DB) DBStatus { status := DBStatus{ Status: "unknown", LocalTXID: "-", WALSize: "-", } // Check if database file exists. dbPath := db.Path() if _, err := os.Stat(dbPath); os.IsNotExist(err) { status.Status = "no database" return status } // Get WAL file size. walPath := db.WALPath() if walInfo, err := os.Stat(walPath); err == nil { status.WALSize = humanize.Bytes(uint64(walInfo.Size())) } else if os.IsNotExist(err) { status.WALSize = "0 B" } // Get local TXID from L0 directory. _, maxTXID, err := db.MaxLTX() if err == nil && maxTXID > 0 { status.LocalTXID = maxTXID.String() status.Status = "ok" } else if err == nil { status.Status = "not initialized" } else { status.Status = "error" } return status } // Usage prints the help screen to STDOUT. func (c *StatusCommand) Usage() { fmt.Printf(` The status command displays the replication status of databases. Usage: litestream status [arguments] [database path] Arguments: -config PATH Specifies the configuration file. Defaults to %s -no-expand-env Disables environment variable expansion in configuration file. If a database path is provided, only that database's status is shown. Otherwise, all configured databases are displayed. Output columns: database Path to the SQLite database status Current status (ok, not initialized, no database, error) local txid Latest local transaction ID wal size Current WAL file size Note: To see replica TXID and sync status, use the MCP tools or check logs while the replication daemon is running. `[1:], DefaultConfigPath(), ) } ================================================ FILE: cmd/litestream/status_test.go ================================================ package main_test import ( "context" "os" "path/filepath" "testing" main "github.com/benbjohnson/litestream/cmd/litestream" ) func TestStatusCommand_Run(t *testing.T) { t.Run("NoConfig", func(t *testing.T) { cmd := &main.StatusCommand{} err := cmd.Run(context.Background(), []string{"-config", "/nonexistent/config.yml"}) if err == nil { t.Error("expected error for missing config") } }) t.Run("WithConfig", func(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") configPath := filepath.Join(dir, "litestream.yml") // Create a SQLite database. if err := os.WriteFile(dbPath, []byte{}, 0644); err != nil { t.Fatal(err) } // Create config file. config := `dbs: - path: ` + dbPath + ` replicas: - url: file://` + filepath.Join(dir, "replica") + ` ` if err := os.WriteFile(configPath, []byte(config), 0644); err != nil { t.Fatal(err) } cmd := &main.StatusCommand{} err := cmd.Run(context.Background(), []string{"-config", configPath}) if err != nil { t.Errorf("unexpected error: %v", err) } }) t.Run("FilterByPath", func(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") configPath := filepath.Join(dir, "litestream.yml") // Create a SQLite database. if err := os.WriteFile(dbPath, []byte{}, 0644); err != nil { t.Fatal(err) } // Create config file. config := `dbs: - path: ` + dbPath + ` replicas: - url: file://` + filepath.Join(dir, "replica") + ` ` if err := os.WriteFile(configPath, []byte(config), 0644); err != nil { t.Fatal(err) } cmd := &main.StatusCommand{} err := cmd.Run(context.Background(), []string{"-config", configPath, dbPath}) if err != nil { t.Errorf("unexpected error: %v", err) } }) } ================================================ FILE: cmd/litestream/stop.go ================================================ package main import ( "bytes" "context" "encoding/json" "flag" "fmt" "io" "net" "net/http" "time" "github.com/benbjohnson/litestream" ) // StopCommand represents the command to stop replication for a database. type StopCommand struct{} // Run executes the stop command. func (c *StopCommand) Run(ctx context.Context, args []string) error { fs := flag.NewFlagSet("litestream-stop", flag.ContinueOnError) timeout := fs.Int("timeout", 30, "timeout in seconds") socketPath := fs.String("socket", "/var/run/litestream.sock", "control socket path") fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } if fs.NArg() == 0 { return fmt.Errorf("database path required") } if fs.NArg() > 1 { return fmt.Errorf("too many arguments") } dbPath := fs.Arg(0) // Create HTTP client that connects via Unix socket with timeout clientTimeout := time.Duration(*timeout) * time.Second client := &http.Client{ Timeout: clientTimeout, Transport: &http.Transport{ DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { return net.DialTimeout("unix", *socketPath, clientTimeout) }, }, } req := litestream.StopRequest{ Path: dbPath, Timeout: *timeout, } reqBody, err := json.Marshal(req) if err != nil { return fmt.Errorf("failed to marshal request: %w", err) } resp, err := client.Post("http://localhost/stop", "application/json", bytes.NewReader(reqBody)) if err != nil { return fmt.Errorf("failed to connect to control socket: %w", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("failed to read response: %w", err) } if resp.StatusCode != http.StatusOK { var errResp litestream.ErrorResponse if err := json.Unmarshal(body, &errResp); err == nil && errResp.Error != "" { return fmt.Errorf("stop failed: %s", errResp.Error) } return fmt.Errorf("stop failed: %s", string(body)) } var result litestream.StopResponse if err := json.Unmarshal(body, &result); err != nil { return fmt.Errorf("failed to parse response: %w", err) } output, err := json.MarshalIndent(result, "", " ") if err != nil { return fmt.Errorf("failed to format response: %w", err) } fmt.Println(string(output)) return nil } // Usage prints the help text for the stop command. func (c *StopCommand) Usage() { fmt.Println(` usage: litestream stop [OPTIONS] DB_PATH Stop replication for a database. Stop always waits for shutdown and final sync. Options: -timeout SECONDS Maximum time to wait in seconds (default: 30). -socket PATH Path to control socket (default: /var/run/litestream.sock). `[1:]) } ================================================ FILE: cmd/litestream/sync.go ================================================ package main import ( "bytes" "context" "encoding/json" "flag" "fmt" "io" "net" "net/http" "time" "github.com/benbjohnson/litestream" ) // SyncCommand represents the command to force an immediate sync for a database. type SyncCommand struct{} // Run executes the sync command. func (c *SyncCommand) Run(ctx context.Context, args []string) error { fs := flag.NewFlagSet("litestream-sync", flag.ContinueOnError) timeout := fs.Int("timeout", 30, "timeout in seconds") socketPath := fs.String("socket", "/var/run/litestream.sock", "control socket path") wait := fs.Bool("wait", false, "block until sync completes") fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } if fs.NArg() == 0 { return fmt.Errorf("database path required") } if fs.NArg() > 1 { return fmt.Errorf("too many arguments") } if *timeout <= 0 { return fmt.Errorf("timeout must be greater than 0") } dbPath := fs.Arg(0) clientTimeout := time.Duration(*timeout) * time.Second client := &http.Client{ Timeout: clientTimeout, Transport: &http.Transport{ DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { return net.DialTimeout("unix", *socketPath, clientTimeout) }, }, } req := litestream.SyncRequest{ Path: dbPath, Wait: *wait, Timeout: *timeout, } reqBody, err := json.Marshal(req) if err != nil { return fmt.Errorf("failed to marshal request: %w", err) } resp, err := client.Post("http://localhost/sync", "application/json", bytes.NewReader(reqBody)) if err != nil { return fmt.Errorf("failed to connect to control socket: %w", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("failed to read response: %w", err) } if resp.StatusCode != http.StatusOK { var errResp litestream.ErrorResponse if err := json.Unmarshal(body, &errResp); err == nil && errResp.Error != "" { return fmt.Errorf("sync failed: %s", errResp.Error) } return fmt.Errorf("sync failed: %s", string(body)) } var result litestream.SyncResponse if err := json.Unmarshal(body, &result); err != nil { return fmt.Errorf("failed to parse response: %w", err) } output, err := json.MarshalIndent(result, "", " ") if err != nil { return fmt.Errorf("failed to format response: %w", err) } fmt.Println(string(output)) return nil } // Usage prints the help text for the sync command. func (c *SyncCommand) Usage() { fmt.Println(` usage: litestream sync [OPTIONS] DB_PATH Force an immediate sync for a database. Options: -wait Block until sync completes including remote replication (default: false). -timeout SECONDS Maximum time to wait in seconds, best-effort (default: 30). -socket PATH Path to control socket (default: /var/run/litestream.sock). `[1:]) } ================================================ FILE: cmd/litestream/sync_test.go ================================================ package main_test import ( "context" "testing" "github.com/benbjohnson/litestream" main "github.com/benbjohnson/litestream/cmd/litestream" "github.com/benbjohnson/litestream/internal/testingutil" ) func TestSyncCommand_Run(t *testing.T) { t.Run("MissingDBPath", func(t *testing.T) { cmd := &main.SyncCommand{} err := cmd.Run(context.Background(), []string{"-socket", "/tmp/test.sock"}) if err == nil { t.Error("expected error for missing database path") } if err.Error() != "database path required" { t.Errorf("unexpected error: %v", err) } }) t.Run("TooManyArguments", func(t *testing.T) { cmd := &main.SyncCommand{} err := cmd.Run(context.Background(), []string{"-socket", "/tmp/test.sock", "/tmp/a.db", "extra"}) if err == nil { t.Error("expected error for too many arguments") } if err.Error() != "too many arguments" { t.Errorf("unexpected error: %v", err) } }) t.Run("InvalidTimeoutZero", func(t *testing.T) { cmd := &main.SyncCommand{} err := cmd.Run(context.Background(), []string{"-timeout", "0", "/tmp/test.db"}) if err == nil { t.Error("expected error for zero timeout") } if err.Error() != "timeout must be greater than 0" { t.Errorf("unexpected error: %v", err) } }) t.Run("SocketConnectionError", func(t *testing.T) { cmd := &main.SyncCommand{} err := cmd.Run(context.Background(), []string{"-socket", "/nonexistent/socket.sock", "/tmp/test.db"}) if err == nil { t.Error("expected error for socket connection failure") } }) t.Run("Success", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INT)`) if err != nil { t.Fatal(err) } store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false if err := store.Open(t.Context()); err != nil { t.Fatal(err) } defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) if err := server.Start(); err != nil { t.Fatal(err) } defer server.Close() cmd := &main.SyncCommand{} err = cmd.Run(t.Context(), []string{"-socket", server.SocketPath, db.Path()}) if err != nil { t.Errorf("unexpected error: %v", err) } }) t.Run("SuccessWithWait", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INT)`) if err != nil { t.Fatal(err) } store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false if err := store.Open(t.Context()); err != nil { t.Fatal(err) } defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) if err := server.Start(); err != nil { t.Fatal(err) } defer server.Close() cmd := &main.SyncCommand{} err = cmd.Run(t.Context(), []string{"-socket", server.SocketPath, "-wait", db.Path()}) if err != nil { t.Errorf("unexpected error: %v", err) } }) } ================================================ FILE: cmd/litestream/unregister.go ================================================ package main import ( "bytes" "context" "encoding/json" "flag" "fmt" "io" "net" "net/http" "time" "github.com/benbjohnson/litestream" ) type UnregisterCommand struct{} func (c *UnregisterCommand) Run(ctx context.Context, args []string) error { fs := flag.NewFlagSet("litestream-unregister", flag.ContinueOnError) timeout := fs.Int("timeout", 30, "timeout in seconds") socketPath := fs.String("socket", "/var/run/litestream.sock", "control socket path") fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } if fs.NArg() == 0 { return fmt.Errorf("database path required") } if fs.NArg() > 1 { return fmt.Errorf("too many arguments") } if *timeout <= 0 { return fmt.Errorf("timeout must be greater than 0") } dbPath := fs.Arg(0) // Create HTTP client that connects via Unix socket with timeout. clientTimeout := time.Duration(*timeout) * time.Second client := &http.Client{ Timeout: clientTimeout, Transport: &http.Transport{ DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { return net.DialTimeout("unix", *socketPath, clientTimeout) }, }, } req := litestream.UnregisterDatabaseRequest{ Path: dbPath, Timeout: *timeout, } reqBody, err := json.Marshal(req) if err != nil { return fmt.Errorf("failed to marshal request: %w", err) } resp, err := client.Post("http://localhost/unregister", "application/json", bytes.NewReader(reqBody)) if err != nil { return fmt.Errorf("failed to connect to control socket: %w", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("failed to read response: %w", err) } if resp.StatusCode != http.StatusOK { var errResp litestream.ErrorResponse if err := json.Unmarshal(body, &errResp); err == nil && errResp.Error != "" { return fmt.Errorf("unregister failed: %s", errResp.Error) } return fmt.Errorf("unregister failed: %s", string(body)) } var result litestream.UnregisterDatabaseResponse if err := json.Unmarshal(body, &result); err != nil { return fmt.Errorf("failed to parse response: %w", err) } output, err := json.MarshalIndent(result, "", " ") if err != nil { return fmt.Errorf("failed to format response: %w", err) } fmt.Println(string(output)) return nil } func (c *UnregisterCommand) Usage() { fmt.Println(` usage: litestream unregister [OPTIONS] DB_PATH Unregister a database from replication. Arguments: DB_PATH Path to the SQLite database file. Options: -timeout SECONDS Maximum time to wait in seconds (default: 30). -socket PATH Path to control socket (default: /var/run/litestream.sock). `[1:]) } ================================================ FILE: cmd/litestream/unregister_test.go ================================================ package main_test import ( "context" "testing" "github.com/benbjohnson/litestream" main "github.com/benbjohnson/litestream/cmd/litestream" "github.com/benbjohnson/litestream/internal/testingutil" ) func TestUnregisterCommand_Run(t *testing.T) { t.Run("MissingPath", func(t *testing.T) { cmd := &main.UnregisterCommand{} err := cmd.Run(context.Background(), []string{"-socket", "/tmp/test.sock"}) if err == nil { t.Error("expected error for missing path") } if err.Error() != "database path required" { t.Errorf("unexpected error: %v", err) } }) t.Run("TooManyArguments", func(t *testing.T) { cmd := &main.UnregisterCommand{} err := cmd.Run(context.Background(), []string{"-socket", "/tmp/test.sock", "/tmp/test.db", "extra"}) if err == nil { t.Error("expected error for too many arguments") } if err.Error() != "too many arguments" { t.Errorf("unexpected error: %v", err) } }) t.Run("InvalidTimeoutZero", func(t *testing.T) { cmd := &main.UnregisterCommand{} err := cmd.Run(context.Background(), []string{"-timeout", "0", "/tmp/test.db"}) if err == nil { t.Error("expected error for zero timeout") } if err.Error() != "timeout must be greater than 0" { t.Errorf("unexpected error: %v", err) } }) t.Run("InvalidTimeoutNegative", func(t *testing.T) { cmd := &main.UnregisterCommand{} err := cmd.Run(context.Background(), []string{"-timeout", "-1", "/tmp/test.db"}) if err == nil { t.Error("expected error for negative timeout") } if err.Error() != "timeout must be greater than 0" { t.Errorf("unexpected error: %v", err) } }) t.Run("SocketConnectionError", func(t *testing.T) { cmd := &main.UnregisterCommand{} err := cmd.Run(context.Background(), []string{"-socket", "/nonexistent/socket.sock", "/tmp/test.db"}) if err == nil { t.Error("expected error for socket connection failure") } }) t.Run("NotFoundIsIdempotent", func(t *testing.T) { store := litestream.NewStore(nil, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false if err := store.Open(context.Background()); err != nil { t.Fatal(err) } defer store.Close(context.Background()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) if err := server.Start(); err != nil { t.Fatal(err) } defer server.Close() cmd := &main.UnregisterCommand{} // Should not error even though database doesn't exist. err := cmd.Run(context.Background(), []string{"-socket", server.SocketPath, "/nonexistent/db"}) if err != nil { t.Errorf("unexpected error: %v", err) } }) t.Run("Success", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) dbPath := db.Path() store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false if err := store.Open(context.Background()); err != nil { t.Fatal(err) } defer store.Close(context.Background()) // Verify database is initially in store. if len(store.DBs()) != 1 { t.Fatalf("expected 1 database in store, got %d", len(store.DBs())) } server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) if err := server.Start(); err != nil { t.Fatal(err) } defer server.Close() cmd := &main.UnregisterCommand{} err := cmd.Run(context.Background(), []string{"-socket", server.SocketPath, dbPath}) if err != nil { t.Errorf("unexpected error: %v", err) } // Verify database was unregistered from store. if len(store.DBs()) != 0 { t.Errorf("expected 0 databases in store, got %d", len(store.DBs())) } }) } ================================================ FILE: cmd/litestream/version.go ================================================ package main import ( "context" "flag" "fmt" ) // VersionCommand represents a command to print the current version. type VersionCommand struct{} // Run executes the command. func (c *VersionCommand) Run(_ context.Context, args []string) (err error) { fs := flag.NewFlagSet("litestream-version", flag.ContinueOnError) fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } fmt.Println(Version) return nil } // Usage prints the help screen to STDOUT. func (c *VersionCommand) Usage() { fmt.Println(` Prints the version. Usage: litestream version `[1:]) } ================================================ FILE: cmd/litestream-test/README.md ================================================ # litestream-test A CLI testing harness for Litestream that provides tools for database population, load generation, and replication validation. ## Overview `litestream-test` is a purpose-built tool for testing Litestream's replication functionality across various scenarios. It provides commands for: - Quickly populating databases to specific sizes - Generating continuous load with configurable patterns - Shrinking databases to test compaction scenarios - Validating replication integrity after restore ## Installation ```bash go build -o bin/litestream-test ./cmd/litestream-test ``` ## Commands ### populate Quickly populate a database to a target size with structured test data. ```bash litestream-test populate -db [options] ``` **Options:** - `-db` - Database path (required) - `-target-size` - Target database size (default: "100MB", examples: "1GB", "500MB", "50MB") - `-row-size` - Average row size in bytes (default: 1024) - `-batch-size` - Rows per transaction (default: 1000) - `-table-count` - Number of tables to create (default: 1) - `-index-ratio` - Percentage of columns to index, 0.0-1.0 (default: 0.2) - `-page-size` - SQLite page size in bytes (default: 4096) **Examples:** ```bash litestream-test populate -db /tmp/test.db -target-size 1GB litestream-test populate -db /tmp/test.db -target-size 50MB -batch-size 10000 litestream-test populate -db /tmp/test.db -target-size 1.5GB -page-size 4096 ``` **Use Cases:** - Creating test databases of specific sizes - Testing SQLite lock page boundary (1GB with 4KB pages) - Generating initial data before replication tests ### load Generate continuous write and read load on a database with configurable patterns. ```bash litestream-test load -db [options] ``` **Options:** - `-db` - Database path (required, must exist) - `-write-rate` - Writes per second (default: 100) - `-duration` - How long to run (default: 1m, examples: "30s", "5m", "2h", "8h") - `-pattern` - Write pattern (default: "constant") - `constant` - Steady write rate - `burst` - Periodic bursts of activity - `random` - Random write intervals - `wave` - Sinusoidal pattern simulating varying load - `-payload-size` - Size of each write in bytes (default: 1024) - `-read-ratio` - Read/write ratio, 0.0-1.0 (default: 0.2) - `-workers` - Number of concurrent workers (default: 1) **Examples:** ```bash litestream-test load -db /tmp/test.db -write-rate 50 -duration 5m litestream-test load -db /tmp/test.db -write-rate 100 -duration 2h -pattern wave litestream-test load -db /tmp/test.db -write-rate 200 -duration 8h -workers 4 -pattern burst ``` **Use Cases:** - Stress testing replication under sustained load - Testing checkpoint behavior with various patterns - Simulating production workloads for overnight tests - Testing concurrent operations with multiple workers ### shrink Shrink a database by deleting data, useful for testing compaction scenarios. ```bash litestream-test shrink -db [options] ``` **Use Cases:** - Testing database shrinkage and compaction - Simulating data deletion scenarios - Testing retention cleanup behavior ### validate Validate that a replica can be restored and matches the source database. ```bash litestream-test validate [options] ``` **Options:** - `-source-db` - Original database path (required) - `-replica-url` - Replica URL to validate (e.g., "file:///path", "s3://bucket/path") - `-restored-db` - Path for restored database (default: source-db + ".restored") - `-check-type` - Type of validation (default: "quick") - `quick` - Fast row count comparison - `integrity` - SQLite PRAGMA integrity_check - `checksum` - Full database checksum comparison - `full` - All validation types - `-ltx-continuity` - Check LTX file continuity (default: false) - `-config` - Litestream config file path (alternative to replica-url) **Examples:** ```bash litestream-test validate -source-db /tmp/test.db -replica-url file:///tmp/replica litestream-test validate -source-db /tmp/test.db -replica-url s3://bucket/path -check-type full litestream-test validate -source-db /tmp/test.db -config /tmp/litestream.yml -ltx-continuity ``` **Use Cases:** - Verifying replication integrity after tests - Testing restore functionality - Validating data consistency across replicas - Checking LTX file continuity ### version Show version information. ```bash litestream-test version ``` ## Usage Patterns ### Basic Test Workflow ```bash litestream-test populate -db /tmp/test.db -target-size 100MB litestream replicate /tmp/test.db file:///tmp/replica & LITESTREAM_PID=$! litestream-test load -db /tmp/test.db -duration 5m -write-rate 50 kill $LITESTREAM_PID wait litestream-test validate -source-db /tmp/test.db -replica-url file:///tmp/replica ``` ### Overnight Test Pattern ```bash litestream-test populate -db /tmp/test.db -target-size 100MB litestream replicate -config litestream.yml & litestream-test load -db /tmp/test.db \ -duration 8h \ -write-rate 100 \ -pattern wave \ -workers 4 ``` ### Large Database with Lock Page Testing ```bash litestream-test populate -db /tmp/test.db \ -target-size 1.5GB \ -page-size 4096 \ -batch-size 10000 litestream replicate /tmp/test.db s3://bucket/path & litestream-test validate -source-db /tmp/test.db \ -replica-url s3://bucket/path \ -check-type full ``` ## Integration with Test Scripts The `litestream-test` tool is used by all scripts in the `scripts/` directory. These scripts orchestrate full test scenarios: - `scripts/*.sh` - Use litestream-test for database operations - `scripts/verify-test-setup.sh` - Checks that litestream-test is built - See `scripts/README.md` for detailed test scenario documentation ## Related Documentation - [Test Scripts Documentation](./scripts/README.md) - Comprehensive test scenarios - [S3 Retention Testing](./S3-RETENTION-TESTING.md) - S3-specific test documentation - [Top-level Integration Scripts](../../scripts/README.md) - Long-running test documentation ## Development ### Building ```bash go build -o bin/litestream-test ./cmd/litestream-test ``` ### Testing ```bash go test ./cmd/litestream-test/... ``` ### Adding New Commands 1. Create a new file in `cmd/litestream-test/` (e.g., `mycommand.go`) 2. Implement the command struct and Run method 3. Add command to switch statement in `main.go` 4. Update this README with command documentation ================================================ FILE: cmd/litestream-test/S3-RETENTION-TESTING.md ================================================ # S3 LTX File Retention Testing Guide ## Overview This document describes the comprehensive S3 LTX file retention testing scripts created to validate that old LTX files are properly cleaned up after their retention period expires. These tests use the local Python S3 mock server for isolated, repeatable testing. ## Key Focus Areas ### 1. Small Database Testing - **Database Size**: 50MB - **Retention Period**: 2 minutes - **Focus**: Basic retention behavior with minimal data ### 2. Large Database Testing (Critical) - **Database Size**: 1.5GB (crosses SQLite lock page boundary) - **Page Size**: 4KB (lock page at #262145) - **Retention Period**: 3 minutes - **Focus**: SQLite lock page edge case + retention cleanup at scale ### 3. Comprehensive Analysis - Side-by-side comparison of retention behavior - Performance metrics analysis - Best practices verification ## Test Scripts ### 1. `test-s3-retention-small-db.sh` **Purpose**: Test S3 LTX retention cleanup with small databases **Features**: - Creates 50MB database with structured test data - Uses local S3 mock (moto) for isolation - 2-minute retention period for quick testing - Generates multiple LTX files over time - Monitors cleanup activity in logs - Validates restoration integrity **Usage**: ```bash ./cmd/litestream-test/scripts/test-s3-retention-small-db.sh ``` **Duration**: ~8 minutes ### 2. `test-s3-retention-large-db.sh` **Purpose**: Test S3 LTX retention cleanup with large databases crossing the 1GB SQLite lock page boundary **Features**: - Creates 1.5GB database (crosses lock page at 1GB) - Specifically tests SQLite lock page handling - 3-minute retention period - Extended monitoring for large database patterns - Comprehensive validation including lock page verification - Tests restoration of large databases **Usage**: ```bash ./cmd/litestream-test/scripts/test-s3-retention-large-db.sh ``` **Duration**: ~15-20 minutes ### 3. `test-s3-retention-comprehensive.sh` **Purpose**: Comprehensive test runner and analysis tool **Features**: - Runs both small and large database tests - Provides comparative analysis - Generates detailed reports - Configurable test execution - Best practices verification **Usage**: ```bash # Run all tests ./cmd/litestream-test/scripts/test-s3-retention-comprehensive.sh # Run only small database test ./cmd/litestream-test/scripts/test-s3-retention-comprehensive.sh --small-only # Run only large database test ./cmd/litestream-test/scripts/test-s3-retention-comprehensive.sh --large-only # Keep test files after completion ./cmd/litestream-test/scripts/test-s3-retention-comprehensive.sh --no-cleanup ``` **Duration**: ~25-30 minutes for full suite ## SQLite Lock Page Testing ### Why It Matters SQLite reserves a special lock page at exactly 1GB (offset 0x40000000) that cannot contain data. This creates a critical edge case that Litestream must handle correctly. ### What We Test - **Lock Page Location**: Page #262145 (with 4KB page size) - **Boundary Crossing**: Databases that grow beyond 1GB - **Replication Integrity**: Ensure lock page is properly skipped - **Restoration Accuracy**: Verify restored databases maintain integrity ### Lock Page Numbers by Page Size | Page Size | Lock Page # | Test Coverage | |-----------|-------------|---------------| | 4KB | 262145 | ✅ Tested | | 8KB | 131073 | 🔄 Possible | | 16KB | 65537 | 🔄 Possible | | 32KB | 32769 | 🔄 Possible | ## Local S3 Mock Setup ### Why Use Local Mock - **Isolation**: No external dependencies or costs - **Repeatability**: Consistent test environment - **Speed**: No network latency - **Safety**: No risk of affecting production data ### How It Works The tests use the existing `./etc/s3_mock.py` script which: 1. Starts a local moto S3 server 2. Creates a test bucket with unique name 3. Runs Litestream with S3 configuration 4. Automatically cleans up after test completion ### Environment Variables Set by Mock ```bash LITESTREAM_S3_ACCESS_KEY_ID="lite" LITESTREAM_S3_SECRET_ACCESS_KEY="stream" LITESTREAM_S3_BUCKET="test{timestamp}" LITESTREAM_S3_ENDPOINT="http://127.0.0.1:5000" LITESTREAM_S3_FORCE_PATH_STYLE="true" ``` ## Test Execution Flow ### Small Database Test Flow 1. **Setup**: Build binaries, check dependencies 2. **Database Creation**: 50MB with indexed tables 3. **Replication Start**: Begin S3 mock and Litestream 4. **Data Generation**: Create LTX files over time (6 batches, 20s apart) 5. **Retention Monitoring**: Watch for cleanup activity (4 minutes) 6. **Validation**: Test restoration and integrity 7. **Analysis**: Generate detailed report ### Large Database Test Flow 1. **Setup**: Build binaries, verify lock page calculations 2. **Database Creation**: 1.5GB crossing lock page boundary 3. **Replication Start**: Begin S3 mock (longer initial sync) 4. **Data Generation**: Add incremental data around lock page 5. **Extended Monitoring**: Watch cleanup patterns (6 minutes) 6. **Comprehensive Validation**: Test large database restoration 7. **Analysis**: Generate lock page specific report ## Monitoring Retention Cleanup ### What to Look For The scripts monitor logs for these cleanup indicators: - **Direct**: "clean", "delete", "expire", "retention", "removed", "purge" - **Indirect**: "old file", "ttl", "sweep", "vacuum", "evict" - **LTX-specific**: "ltx.*old", "snapshot.*old", "compress", "archive" ### Expected Behavior 1. **Initial Period**: LTX files accumulate normally 2. **Retention Trigger**: Cleanup begins after retention period 3. **Ongoing**: Old files removed, new files continue to accumulate 4. **Stabilization**: File count stabilizes at recent files only ### Warning Signs - **No Cleanup**: Files accumulate indefinitely - **Cleanup Failures**: Error messages about S3 DELETE operations - **Retention Ignored**: Files older than retention period remain ## Dependencies ### Required Tools - **Go**: For building Litestream binaries - **Python 3**: For S3 mock server - **sqlite3**: For database operations - **bc**: For calculations ### Python Packages ```bash pip3 install moto boto3 ``` ### Auto-Installation The scripts automatically: - Build missing Litestream binaries - Install missing Python packages - Check for required tools ## Output and Artifacts ### Log Files - `/tmp/small-retention-test.log` - Small database replication log - `/tmp/large-retention-test.log` - Large database replication log - `/tmp/small-retention-config.yml` - Small database config - `/tmp/large-retention-config.yml` - Large database config ### Database Files - `/tmp/small-retention-test.db` - Small test database - `/tmp/large-retention-test.db` - Large test database - `/tmp/small-retention-restored.db` - Restored small database - `/tmp/large-retention-restored.db` - Restored large database ### Analysis Output Each test generates: - **Operation Counts**: Sync, upload, LTX operations - **Cleanup Indicators**: Number of cleanup-related log entries - **Error Analysis**: Any errors or warnings encountered - **Performance Metrics**: Duration, throughput, file counts - **Validation Results**: Integrity checks, restoration success ## Integration with Existing Framework ### Relationship to Existing Tests These tests complement the existing test infrastructure: - **`test-s3-retention-cleanup.sh`**: Original retention test (more basic) - **`test-754-s3-scenarios.sh`**: Issue #754 specific testing - **Testing Framework**: Uses `litestream-test` CLI for data generation ### Consistent Patterns - Use existing `etc/s3_mock.py` for S3 simulation - Follow naming conventions from existing scripts - Integrate with `litestream-test` populate/load/validate commands - Generate structured output for analysis ## Production Validation Recommendations ### After Local Testing 1. **Real S3 Testing**: Run against actual S3/GCS/Azure endpoints 2. **Network Scenarios**: Test with network interruptions 3. **Scale Testing**: Test with production-sized databases 4. **Cost Analysis**: Monitor S3 API calls and storage costs 5. **Concurrent Testing**: Multiple databases simultaneously ### Retention Period Guidelines - **Local Testing**: 2-3 minutes for quick feedback - **Staging**: 1-2 hours for realistic behavior - **Production**: Days to weeks based on recovery requirements ### Monitoring in Production - **LTX File Counts**: Should stabilize after retention period - **Storage Growth**: Should level off, not grow indefinitely - **API Costs**: DELETE operations should occur regularly - **Performance**: Cleanup shouldn't impact replication performance ## Troubleshooting ### Common Issues #### 1. Python Dependencies Missing ```bash pip3 install moto boto3 ``` #### 2. Binaries Not Found ```bash go build -o bin/litestream ./cmd/litestream go build -o bin/litestream-test ./cmd/litestream-test ``` #### 3. Large Database Test Slow - Expected: 1.5GB takes time to create and replicate - Monitor progress in logs - Increase timeouts if needed #### 4. No Cleanup Activity Detected - May be normal: Litestream might clean up silently - Check S3 bucket contents manually (if using real S3) - Verify retention period has elapsed #### 5. Lock Page Boundary Not Crossed - Check final page count vs. lock page number - Increase target database size if needed - Verify page size settings ### Debug Mode For more verbose output: ```bash # Enable debug logging export LITESTREAM_DEBUG=1 # Run with debug ./cmd/litestream-test/scripts/test-s3-retention-comprehensive.sh ``` ## Summary These retention testing scripts provide comprehensive validation of Litestream's S3 LTX file cleanup behavior across different database sizes and scenarios. They specifically address: 1. **Ben's Requirements**: Local testing with Python S3 mock 2. **SQLite Edge Cases**: Lock page boundary at 1GB 3. **Scale Scenarios**: Both small (50MB) and large (1.5GB) databases 4. **Retention Verification**: Multiple retention periods and monitoring 5. **Production Readiness**: Detailed analysis and recommendations The scripts are designed to run reliably in isolation while providing detailed insights into Litestream's retention cleanup behavior. ================================================ FILE: cmd/litestream-test/load.go ================================================ package main import ( "context" cryptorand "crypto/rand" "database/sql" "flag" "fmt" "log/slog" "math/rand" "os" "os/signal" "sync" "sync/atomic" "syscall" "time" _ "github.com/mattn/go-sqlite3" ) type LoadCommand struct { Main *Main DB string WriteRate int Duration time.Duration Pattern string PayloadSize int ReadRatio float64 Workers int } type LoadStats struct { writes int64 reads int64 errors int64 startTime time.Time lastReport time.Time mu sync.Mutex } func (c *LoadCommand) Run(ctx context.Context, args []string) error { fs := flag.NewFlagSet("litestream-test load", flag.ExitOnError) fs.StringVar(&c.DB, "db", "", "Database path (required)") fs.IntVar(&c.WriteRate, "write-rate", 100, "Writes per second") fs.DurationVar(&c.Duration, "duration", 1*time.Minute, "How long to run") fs.StringVar(&c.Pattern, "pattern", "constant", "Write pattern (constant, burst, random, wave)") fs.IntVar(&c.PayloadSize, "payload-size", 1024, "Size of each write operation in bytes") fs.Float64Var(&c.ReadRatio, "read-ratio", 0.2, "Read/write ratio (0.0-1.0)") fs.IntVar(&c.Workers, "workers", 1, "Number of concurrent workers") fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } if c.DB == "" { return fmt.Errorf("database path required") } if _, err := os.Stat(c.DB); err != nil { return fmt.Errorf("database does not exist: %w", err) } slog.Info("Starting load generation", "db", c.DB, "write_rate", c.WriteRate, "duration", c.Duration, "pattern", c.Pattern, "payload_size", c.PayloadSize, "read_ratio", c.ReadRatio, "workers", c.Workers, ) return c.generateLoad(ctx) } func (c *LoadCommand) generateLoad(ctx context.Context) error { db, err := sql.Open("sqlite3", c.DB+"?_journal_mode=WAL") if err != nil { return fmt.Errorf("open database: %w", err) } defer db.Close() db.SetMaxOpenConns(c.Workers + 1) db.SetMaxIdleConns(c.Workers) if err := c.ensureTestTable(db); err != nil { return fmt.Errorf("ensure test table: %w", err) } ctx, cancel := context.WithTimeout(ctx, c.Duration) defer cancel() sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) go func() { <-sigChan slog.Info("Received interrupt signal, stopping load generation") cancel() }() stats := &LoadStats{ startTime: time.Now(), lastReport: time.Now(), } var wg sync.WaitGroup for i := 0; i < c.Workers; i++ { wg.Add(1) go func(workerID int) { defer wg.Done() c.worker(ctx, db, workerID, stats) }(i) } go c.reportStats(ctx, stats) wg.Wait() c.finalReport(stats) return nil } func (c *LoadCommand) worker(ctx context.Context, db *sql.DB, workerID int, stats *LoadStats) { ticker := time.NewTicker(time.Second / time.Duration(c.WriteRate/c.Workers)) defer ticker.Stop() data := make([]byte, c.PayloadSize) cryptorand.Read(data) for { select { case <-ctx.Done(): return case <-ticker.C: rate := c.calculateRate(stats) if rate == 0 { continue } if rand.Float64() < c.ReadRatio { if err := c.performRead(db); err != nil { atomic.AddInt64(&stats.errors, 1) slog.Error("Read failed", "error", err) } else { atomic.AddInt64(&stats.reads, 1) } } else { if err := c.performWrite(db, data); err != nil { atomic.AddInt64(&stats.errors, 1) slog.Error("Write failed", "error", err) } else { atomic.AddInt64(&stats.writes, 1) } } } } } func (c *LoadCommand) calculateRate(stats *LoadStats) float64 { elapsed := time.Since(stats.startTime).Seconds() switch c.Pattern { case "burst": if int(elapsed)%10 < 3 { return 2.0 } return 0.0 case "random": return rand.Float64() * 2.0 case "wave": return (1.0 + 0.5*waveFunction(elapsed/10.0)) default: return 1.0 } } func waveFunction(t float64) float64 { return (1.0 + 0.8*sinApprox(t)) / 2.0 } func sinApprox(x float64) float64 { const twoPi = 2 * 3.14159265359 x = x - float64(int(x/twoPi))*twoPi if x < 3.14159265359 { return 4 * x * (3.14159265359 - x) / (3.14159265359 * 3.14159265359) } x = x - 3.14159265359 return -4 * x * (3.14159265359 - x) / (3.14159265359 * 3.14159265359) } func (c *LoadCommand) ensureTestTable(db *sql.DB) error { createSQL := ` CREATE TABLE IF NOT EXISTS load_test ( id INTEGER PRIMARY KEY AUTOINCREMENT, data BLOB, text_field TEXT, int_field INTEGER, timestamp INTEGER ) ` _, err := db.Exec(createSQL) if err != nil { return fmt.Errorf("create table: %w", err) } _, err = db.Exec("CREATE INDEX IF NOT EXISTS idx_load_test_timestamp ON load_test(timestamp)") return err } func (c *LoadCommand) performWrite(db *sql.DB, data []byte) error { textField := fmt.Sprintf("load_%d", time.Now().UnixNano()) intField := rand.Int63() timestamp := time.Now().Unix() _, err := db.Exec(` INSERT INTO load_test (data, text_field, int_field, timestamp) VALUES (?, ?, ?, ?) `, data, textField, intField, timestamp) return err } func (c *LoadCommand) performRead(db *sql.DB) error { var count int query := `SELECT COUNT(*) FROM load_test WHERE timestamp > ?` timestamp := time.Now().Add(-1 * time.Hour).Unix() return db.QueryRow(query, timestamp).Scan(&count) } func (c *LoadCommand) reportStats(ctx context.Context, stats *LoadStats) { ticker := time.NewTicker(5 * time.Second) defer ticker.Stop() for { select { case <-ctx.Done(): return case <-ticker.C: stats.mu.Lock() elapsed := time.Since(stats.lastReport).Seconds() writes := atomic.LoadInt64(&stats.writes) reads := atomic.LoadInt64(&stats.reads) errors := atomic.LoadInt64(&stats.errors) writeRate := float64(writes) / elapsed readRate := float64(reads) / elapsed slog.Info("Load statistics", "writes_per_sec", fmt.Sprintf("%.1f", writeRate), "reads_per_sec", fmt.Sprintf("%.1f", readRate), "total_writes", writes, "total_reads", reads, "errors", errors, "elapsed", time.Since(stats.startTime).Round(time.Second), ) atomic.StoreInt64(&stats.writes, 0) atomic.StoreInt64(&stats.reads, 0) stats.lastReport = time.Now() stats.mu.Unlock() } } } func (c *LoadCommand) finalReport(stats *LoadStats) { totalTime := time.Since(stats.startTime) writes := atomic.LoadInt64(&stats.writes) reads := atomic.LoadInt64(&stats.reads) errors := atomic.LoadInt64(&stats.errors) slog.Info("Load generation complete", "duration", totalTime.Round(time.Second), "total_writes", writes, "total_reads", reads, "total_errors", errors, "avg_writes_per_sec", fmt.Sprintf("%.1f", float64(writes)/totalTime.Seconds()), "avg_reads_per_sec", fmt.Sprintf("%.1f", float64(reads)/totalTime.Seconds()), ) } func (c *LoadCommand) Usage() { fmt.Fprintln(c.Main.Stdout, ` Generate continuous load on a SQLite database for testing. Usage: litestream-test load [options] Options: -db PATH Database path (required) -write-rate RATE Target writes per second Default: 100 -duration DURATION How long to run (e.g., "10m", "1h") Default: 1m -pattern PATTERN Write pattern: constant, burst, random, wave Default: constant -payload-size SIZE Size of each write operation in bytes Default: 1024 -read-ratio RATIO Read/write ratio (0.0-1.0) Default: 0.2 -workers COUNT Number of concurrent workers Default: 1 Examples: # Generate constant load for 10 minutes litestream-test load -db /tmp/test.db -write-rate 100 -duration 10m # Generate burst pattern load litestream-test load -db /tmp/test.db -pattern burst -duration 1h # Heavy write load with multiple workers litestream-test load -db /tmp/test.db -write-rate 1000 -workers 4 -read-ratio 0.1 `[1:]) } ================================================ FILE: cmd/litestream-test/main.go ================================================ package main import ( "context" "flag" "fmt" "io" "log/slog" "os" "runtime" "strings" ) var ( Version = "development" Commit = "" ) func main() { m := NewMain() if err := m.Run(context.Background(), os.Args[1:]); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } } type Main struct { Stdin io.Reader Stdout io.Writer Stderr io.Writer } func NewMain() *Main { return &Main{ Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr, } } func (m *Main) Run(ctx context.Context, args []string) error { fs := flag.NewFlagSet("litestream-test", flag.ExitOnError) fs.Usage = m.Usage if err := fs.Parse(args); err != nil { return err } if fs.NArg() == 0 || fs.Arg(0) == "help" { m.Usage() return nil } switch fs.Arg(0) { case "populate": return (&PopulateCommand{Main: m}).Run(ctx, fs.Args()[1:]) case "load": return (&LoadCommand{Main: m}).Run(ctx, fs.Args()[1:]) case "shrink": return (&ShrinkCommand{Main: m}).Run(ctx, fs.Args()[1:]) case "validate": return (&ValidateCommand{Main: m}).Run(ctx, fs.Args()[1:]) case "version": return (&VersionCommand{Main: m}).Run(ctx, fs.Args()[1:]) default: return fmt.Errorf("unknown command: %s", fs.Arg(0)) } } func (m *Main) Usage() { fmt.Fprintln(m.Stdout, ` litestream-test is a testing harness for Litestream database replication. Usage: litestream-test [arguments] Commands: populate Quickly populate a database to a target size load Generate continuous load on a database shrink Shrink a database by deleting data validate Validate replication integrity version Show version information Use "litestream-test -h" for more information about a command. `[1:]) } type VersionCommand struct { Main *Main } func (c *VersionCommand) Run(ctx context.Context, args []string) error { fs := flag.NewFlagSet("litestream-test version", flag.ExitOnError) fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } fmt.Fprintf(c.Main.Stdout, "litestream-test %s\n", Version) if Commit != "" { fmt.Fprintf(c.Main.Stdout, "commit: %s\n", Commit) } fmt.Fprintf(c.Main.Stdout, "go: %s\n", strings.TrimPrefix(runtime.Version(), "go")) return nil } func (c *VersionCommand) Usage() { fmt.Fprintln(c.Main.Stdout, ` Show version information for litestream-test. Usage: litestream-test version `[1:]) } func init() { logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ Level: slog.LevelInfo, })) slog.SetDefault(logger) } ================================================ FILE: cmd/litestream-test/populate.go ================================================ package main import ( "context" cryptorand "crypto/rand" "database/sql" "flag" "fmt" "log/slog" "math/rand" "os" "time" _ "github.com/mattn/go-sqlite3" ) type PopulateCommand struct { Main *Main DB string TargetSize string RowSize int BatchSize int TableCount int IndexRatio float64 PageSize int } func (c *PopulateCommand) Run(ctx context.Context, args []string) error { fs := flag.NewFlagSet("litestream-test populate", flag.ExitOnError) fs.StringVar(&c.DB, "db", "", "Database path (required)") fs.StringVar(&c.TargetSize, "target-size", "100MB", "Target database size (e.g., 1GB, 500MB)") fs.IntVar(&c.RowSize, "row-size", 1024, "Average row size in bytes") fs.IntVar(&c.BatchSize, "batch-size", 1000, "Rows per transaction") fs.IntVar(&c.TableCount, "table-count", 1, "Number of tables to create") fs.Float64Var(&c.IndexRatio, "index-ratio", 0.2, "Percentage of columns to index (0.0-1.0)") fs.IntVar(&c.PageSize, "page-size", 4096, "SQLite page size in bytes") fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } if c.DB == "" { return fmt.Errorf("database path required") } targetBytes, err := parseSize(c.TargetSize) if err != nil { return fmt.Errorf("invalid target size: %w", err) } slog.Info("Starting database population", "db", c.DB, "target_size", c.TargetSize, "row_size", c.RowSize, "batch_size", c.BatchSize, "table_count", c.TableCount, "page_size", c.PageSize, ) if err := c.populateDatabase(ctx, targetBytes); err != nil { return fmt.Errorf("populate database: %w", err) } slog.Info("Database population complete", "db", c.DB) return nil } func (c *PopulateCommand) populateDatabase(ctx context.Context, targetBytes int64) error { if err := os.Remove(c.DB); err != nil && !os.IsNotExist(err) { slog.Warn("Could not remove existing database", "error", err) } db, err := sql.Open("sqlite3", c.DB) if err != nil { return fmt.Errorf("open database: %w", err) } defer db.Close() if _, err := db.Exec(fmt.Sprintf("PRAGMA page_size = %d", c.PageSize)); err != nil { return fmt.Errorf("set page size: %w", err) } if _, err := db.Exec("PRAGMA journal_mode = WAL"); err != nil { return fmt.Errorf("set journal mode: %w", err) } if _, err := db.Exec("PRAGMA synchronous = NORMAL"); err != nil { return fmt.Errorf("set synchronous: %w", err) } for i := 0; i < c.TableCount; i++ { tableName := fmt.Sprintf("test_table_%d", i) createSQL := fmt.Sprintf(` CREATE TABLE %s ( id INTEGER PRIMARY KEY AUTOINCREMENT, data BLOB, text_field TEXT, int_field INTEGER, float_field REAL, timestamp INTEGER ) `, tableName) if _, err := db.Exec(createSQL); err != nil { return fmt.Errorf("create table %s: %w", tableName, err) } if c.IndexRatio > 0 { if rand.Float64() < c.IndexRatio { indexSQL := fmt.Sprintf("CREATE INDEX idx_%s_timestamp ON %s(timestamp)", tableName, tableName) if _, err := db.Exec(indexSQL); err != nil { return fmt.Errorf("create index: %w", err) } } if rand.Float64() < c.IndexRatio { indexSQL := fmt.Sprintf("CREATE INDEX idx_%s_int ON %s(int_field)", tableName, tableName) if _, err := db.Exec(indexSQL); err != nil { return fmt.Errorf("create index: %w", err) } } } } totalRows := int(targetBytes / int64(c.RowSize)) rowsPerTable := totalRows / c.TableCount if rowsPerTable == 0 { rowsPerTable = 1 } slog.Info("Populating database", "target_bytes", targetBytes, "total_rows", totalRows, "rows_per_table", rowsPerTable, ) startTime := time.Now() for tableIdx := 0; tableIdx < c.TableCount; tableIdx++ { tableName := fmt.Sprintf("test_table_%d", tableIdx) if err := c.populateTable(ctx, db, tableName, rowsPerTable); err != nil { return fmt.Errorf("populate table %s: %w", tableName, err) } currentSize, _ := getDatabaseSize(c.DB) progress := float64(currentSize) / float64(targetBytes) * 100 slog.Info("Progress", "table", tableName, "current_size_mb", currentSize/1024/1024, "progress_percent", fmt.Sprintf("%.1f", progress), ) if currentSize >= targetBytes { break } } duration := time.Since(startTime) finalSize, _ := getDatabaseSize(c.DB) slog.Info("Population complete", "duration", duration, "final_size_mb", finalSize/1024/1024, "throughput_mb_per_sec", fmt.Sprintf("%.2f", float64(finalSize)/1024/1024/duration.Seconds()), ) return nil } func (c *PopulateCommand) populateTable(ctx context.Context, db *sql.DB, tableName string, rowCount int) error { data := make([]byte, c.RowSize) for i := 0; i < rowCount; i += c.BatchSize { tx, err := db.Begin() if err != nil { return fmt.Errorf("begin transaction: %w", err) } stmt, err := tx.Prepare(fmt.Sprintf(` INSERT INTO %s (data, text_field, int_field, float_field, timestamp) VALUES (?, ?, ?, ?, ?) `, tableName)) if err != nil { tx.Rollback() return fmt.Errorf("prepare statement: %w", err) } batchEnd := i + c.BatchSize if batchEnd > rowCount { batchEnd = rowCount } for j := i; j < batchEnd; j++ { cryptorand.Read(data) textField := fmt.Sprintf("row_%d_%d", i, j) intField := rand.Int63() floatField := rand.Float64() * 1000 timestamp := time.Now().Unix() if _, err := stmt.Exec(data, textField, intField, floatField, timestamp); err != nil { stmt.Close() tx.Rollback() return fmt.Errorf("insert row: %w", err) } } stmt.Close() if err := tx.Commit(); err != nil { return fmt.Errorf("commit transaction: %w", err) } select { case <-ctx.Done(): return ctx.Err() default: } } return nil } func (c *PopulateCommand) Usage() { fmt.Fprintln(c.Main.Stdout, ` Populate a SQLite database to a target size for testing. Usage: litestream-test populate [options] Options: -db PATH Database path (required) -target-size SIZE Target database size (e.g., "1GB", "500MB") Default: 100MB -row-size SIZE Average row size in bytes Default: 1024 -batch-size COUNT Number of rows per transaction Default: 1000 -table-count COUNT Number of tables to create Default: 1 -index-ratio RATIO Percentage of columns to index (0.0-1.0) Default: 0.2 -page-size SIZE SQLite page size in bytes Default: 4096 Examples: # Create a 1GB database with default settings litestream-test populate -db /tmp/test.db -target-size 1GB # Create a 2GB database with larger rows litestream-test populate -db /tmp/test.db -target-size 2GB -row-size 4096 # Test lock page with different page sizes litestream-test populate -db /tmp/test.db -target-size 1.5GB -page-size 8192 `[1:]) } func parseSize(s string) (int64, error) { // Check suffixes in order from longest to shortest to avoid "B" matching before "MB" suffixes := []struct { suffix string multiplier int64 }{ {"TB", 1024 * 1024 * 1024 * 1024}, {"GB", 1024 * 1024 * 1024}, {"MB", 1024 * 1024}, {"KB", 1024}, {"B", 1}, } for _, sf := range suffixes { if len(s) > len(sf.suffix) && s[len(s)-len(sf.suffix):] == sf.suffix { var value float64 if _, err := fmt.Sscanf(s[:len(s)-len(sf.suffix)], "%f", &value); err != nil { return 0, err } return int64(value * float64(sf.multiplier)), nil } } var value int64 if _, err := fmt.Sscanf(s, "%d", &value); err != nil { return 0, err } return value, nil } func getDatabaseSize(path string) (int64, error) { info, err := os.Stat(path) if err != nil { return 0, err } size := info.Size() walPath := path + "-wal" if walInfo, err := os.Stat(walPath); err == nil { size += walInfo.Size() } shmPath := path + "-shm" if shmInfo, err := os.Stat(shmPath); err == nil { size += shmInfo.Size() } return size, nil } ================================================ FILE: cmd/litestream-test/scripts/README.md ================================================ # Litestream Test Scripts Comprehensive test scripts for validating Litestream functionality across various scenarios. These scripts use the `litestream-test` CLI tool to orchestrate complex testing scenarios. ## Prerequisites ```bash go build -o bin/litestream ./cmd/litestream go build -o bin/litestream-test ./cmd/litestream-test ./cmd/litestream-test/scripts/verify-test-setup.sh ``` ## Quick Reference > **Note:** Some tests have been migrated to Go integration tests in `tests/integration/`. See [tests/integration/README.md](../../tests/integration/README.md) for the Go-based test suite. | Script | Purpose | Duration | Status | |--------|---------|----------|--------| | verify-test-setup.sh | Environment validation | ~5s | ✅ Stable | | reproduce-critical-bug.sh | Checkpoint during downtime bug | ~2min | 🐛 Reproduces #752 | | test-754-s3-scenarios.sh | Issue #754 S3 vs file replication | ~10min | 🐛 Tests #754 | | test-754-restore-focus.sh | Issue #754 restore focus | ~5min | 🐛 Tests #754 | | test-simple-754-reproduction.sh | Minimal #754 reproduction | ~3min | 🐛 Tests #754 | | test-v0.5-flag-reproduction.sh | ltx v0.5.0 flag issue | ~5min | 🐛 Tests #754 | | test-v0.5-restart-scenarios.sh | v0.5 restart scenarios | ~5min | 🐛 Tests #754 | | test-format-isolation.sh | Format version isolation | ~3min | ✅ Stable | | test-quick-format-check.sh | Quick format validation | ~30s | ✅ Stable | | test-upgrade-v0.3-to-v0.5.sh | v0.3 to v0.5 upgrade | ~10min | ✅ Stable | | test-upgrade-large-db.sh | Large database upgrade | ~15min | ✅ Stable | | test-massive-upgrade.sh | Massive database upgrade | ~20min | ✅ Stable | | test-s3-retention-cleanup.sh | Basic S3 retention | ~8min | ✅ Stable | | test-s3-retention-small-db.sh | S3 retention 50MB | ~8min | ✅ Stable | | test-s3-retention-large-db.sh | S3 retention 1.5GB | ~20min | ✅ Stable | | test-s3-retention-comprehensive.sh | Full S3 retention suite | ~30min | ✅ Stable | | test-s3-access-point.sh | S3 Access Point ARN support | ~2min | ✅ Stable | ## Test Categories ### Setup & Validation #### verify-test-setup.sh Verifies that the test environment is properly configured with required binaries and dependencies. ```bash ./cmd/litestream-test/scripts/verify-test-setup.sh ``` **Checks:** - Litestream binary exists - litestream-test binary exists - SQLite3 available - Python dependencies for S3 mock ### Bug Reproduction Scripts #### reproduce-critical-bug.sh Reproduces checkpoint during downtime bug that causes restore failures. ```bash ./cmd/litestream-test/scripts/reproduce-critical-bug.sh ``` **Reproduces:** Issue #752 **Scenario:** 1. Litestream is killed (simulating crash) 2. Writes continue and a checkpoint occurs 3. Litestream is restarted 4. Restore fails with "nonsequential page numbers" error **Expected:** Database should restore successfully **Actual:** Restore fails, causing data loss #### test-754-s3-scenarios.sh Tests Issue #754 flag compatibility with S3 replication versus file replication. ```bash ./cmd/litestream-test/scripts/test-754-s3-scenarios.sh ``` **Tests:** - S3 replica behavior with ltx v0.5.0 - File replica behavior comparison - LTX file cleanup and retention - Flag compatibility issues #### test-754-restore-focus.sh Focused testing of Issue #754 restore failures. ```bash ./cmd/litestream-test/scripts/test-754-restore-focus.sh ``` **Tests:** - Restore failures with pre-existing databases - Flag mismatch detection - Recovery scenarios #### test-simple-754-reproduction.sh Minimal reproduction case for Issue #754. ```bash ./cmd/litestream-test/scripts/test-simple-754-reproduction.sh ``` **Reproduces:** Issue #754 with minimal steps for debugging #### test-v0.5-flag-reproduction.sh Reproduces ltx v0.5.0 flag compatibility issue. ```bash ./cmd/litestream-test/scripts/test-v0.5-flag-reproduction.sh ``` **Tests:** - Pre-existing database behavior with ltx v0.5.0 - Flag mismatch scenarios - Upgrade path issues #### test-v0.5-restart-scenarios.sh Tests various restart scenarios with ltx v0.5.0. ```bash ./cmd/litestream-test/scripts/test-v0.5-restart-scenarios.sh ``` **Tests:** - Clean restart - Restart after checkpoint - Restart with pending data - Flag persistence across restarts ### Format & Upgrade Tests #### test-format-isolation.sh Tests isolation between different LTX format versions. ```bash ./cmd/litestream-test/scripts/test-format-isolation.sh ``` **Tests:** - Multiple format versions coexisting - Format detection and handling - Migration between formats - Backward compatibility #### test-quick-format-check.sh Quick validation of LTX format handling. ```bash ./cmd/litestream-test/scripts/test-quick-format-check.sh ``` **Duration:** ~30 seconds **Tests:** - Format version detection - Basic format integrity - Quick validation workflow #### test-upgrade-v0.3-to-v0.5.sh Tests upgrade path from ltx v0.3 to v0.5. ```bash ./cmd/litestream-test/scripts/test-upgrade-v0.3-to-v0.5.sh ``` **Tests:** - Migration from v0.3 to v0.5 - Data preservation during upgrade - Flag handling in upgrade process - Backward compatibility verification #### test-upgrade-large-db.sh Tests upgrade process with large databases (1GB+). ```bash ./cmd/litestream-test/scripts/test-upgrade-large-db.sh ``` **Tests:** - Large database upgrade performance - Data integrity during upgrade - Lock page handling in upgrades - Resource usage during migration #### test-massive-upgrade.sh Tests upgrade with very large databases and long-running scenarios. ```bash ./cmd/litestream-test/scripts/test-massive-upgrade.sh ``` **Tests:** - Multi-GB database upgrades - Extended migration scenarios - Performance under scale - Memory and disk usage ### S3 & Retention Tests For detailed S3 retention testing documentation, see [S3-RETENTION-TESTING.md](../S3-RETENTION-TESTING.md). #### test-s3-access-point.sh Tests S3 Access Point ARN support (Issue #923). Verifies that Access Point ARNs work automatically without manual endpoint configuration. ```bash export LITESTREAM_S3_ACCESS_POINT_ARN='arn:aws:s3:us-east-2:123456789012:accesspoint/my-access-point' ./cmd/litestream-test/scripts/test-s3-access-point.sh ``` **Tests:** - Replication to S3 Access Point using ARN - Automatic endpoint resolution (UseARNRegion) - Restore from Access Point ARN - Data integrity verification **Environment Variables:** - `LITESTREAM_S3_ACCESS_POINT_ARN` - Full ARN of the S3 Access Point (required) - `LITESTREAM_S3_REGION` - AWS region (optional, extracted from ARN) - `LITESTREAM_S3_PREFIX` - Path prefix in bucket (optional) - AWS credentials via standard methods (env vars, credentials file, IAM role) #### test-s3-retention-cleanup.sh Basic S3 LTX retention cleanup testing. ```bash ./cmd/litestream-test/scripts/test-s3-retention-cleanup.sh ``` **Tests:** - Basic retention cleanup behavior - Old LTX file removal - Retention period enforcement #### test-s3-retention-small-db.sh S3 retention testing with 50MB database. ```bash ./cmd/litestream-test/scripts/test-s3-retention-small-db.sh ``` **Configuration:** - Database size: 50MB - Retention period: 2 minutes - Duration: ~8 minutes **Tests:** - Small database retention cleanup - Quick retention cycles - S3 mock integration #### test-s3-retention-large-db.sh S3 retention testing with 1.5GB database crossing lock page boundary. ```bash ./cmd/litestream-test/scripts/test-s3-retention-large-db.sh ``` **Configuration:** - Database size: 1.5GB - Page size: 4KB (lock page at #262145) - Retention period: 3 minutes - Duration: ~20 minutes **Tests:** - Large database retention cleanup - Lock page boundary handling - Extended monitoring - Scale behavior #### test-s3-retention-comprehensive.sh Master script running both small and large database retention tests with analysis. ```bash ./cmd/litestream-test/scripts/test-s3-retention-comprehensive.sh ./cmd/litestream-test/scripts/test-s3-retention-comprehensive.sh --small-only ./cmd/litestream-test/scripts/test-s3-retention-comprehensive.sh --large-only ./cmd/litestream-test/scripts/test-s3-retention-comprehensive.sh --no-cleanup ``` **Duration:** ~30 minutes for full suite **Features:** - Runs both small and large DB tests - Comparative analysis - Detailed reports - Configurable execution ## Usage Patterns ### Running Individual Tests ```bash ./cmd/litestream-test/scripts/test-fresh-start.sh ``` ### Verify Environment First ```bash ./cmd/litestream-test/scripts/verify-test-setup.sh ./cmd/litestream-test/scripts/test-rapid-checkpoints.sh ``` ### Running Multiple Tests ```bash for script in test-fresh-start.sh test-rapid-checkpoints.sh test-database-integrity.sh; do echo "Running $script..." ./cmd/litestream-test/scripts/$script echo "" done ``` ### S3 Testing with Local Mock The S3 tests automatically use the Python S3 mock server (`./etc/s3_mock.py`) for isolated testing: ```bash ./cmd/litestream-test/scripts/test-s3-retention-small-db.sh ``` ### Debugging Failed Tests Most tests create logs in `/tmp/`: ```bash tail -f /tmp/fresh-test.log tail -f /tmp/checkpoint-cycle.log grep -i error /tmp/*.log ``` ## Test Artifacts Tests typically create artifacts in `/tmp/`: - **Databases:** `/tmp/*-test.db` - **Replicas:** `/tmp/*-replica/` - **Logs:** `/tmp/*-test.log` - **Configs:** `/tmp/*.yml` - **Restored DBs:** `/tmp/*-restored.db` ## Test Results & Analysis Historical test results and analysis are stored in `.local/test-results/` (git-ignored): - `final-test-summary.md` - Comprehensive test findings - `validation-results-after-ltx-v0.5.0.md` - ltx v0.5.0 impact analysis - `comprehensive-test-findings.md` - Initial test results - `critical-bug-analysis.md` - Detailed bug analysis ## Key Findings Summary ### Performance ✅ - Successfully handles 400+ writes/second - Manages 100MB+ WAL files - Multiple concurrent databases replicate cleanly ### Fresh Databases ✅ - Work perfectly with ltx v0.5.0 - Clean replication and restore - No flag issues ### Pre-existing Databases ❌ - Broken due to ltx flag compatibility (#754) - Restore failures - Upgrade path issues ### Checkpoint During Downtime ❌ - Worse with ltx v0.5.0 (#752) - Causes restore failures - Data loss risk ### S3 Retention ✅ - LTX cleanup works correctly - Handles lock page boundary - Scale testing successful ## Related Issues - [#752](https://github.com/benbjohnson/litestream/issues/752) - Checkpoint during downtime bug - [#753](https://github.com/benbjohnson/litestream/issues/753) - Transaction numbering (FIXED) - [#754](https://github.com/benbjohnson/litestream/issues/754) - ltx v0.5.0 flag compatibility (CRITICAL) ## Related Documentation - [litestream-test CLI Documentation](../README.md) - CLI tool reference - [S3 Retention Testing Guide](../S3-RETENTION-TESTING.md) - Detailed S3 testing - [Top-level Integration Scripts](../../../scripts/README.md) - Long-running tests ## Contributing When adding new test scripts: 1. Follow existing naming conventions (`test-*.sh`) 2. Include clear comments explaining what is being tested 3. Use `/tmp/` for test artifacts 4. Create cleanup handlers with `trap` 5. Provide clear success/failure output 6. Update this README with script documentation 7. Add entry to Quick Reference table ================================================ FILE: cmd/litestream-test/scripts/reproduce-critical-bug.sh ================================================ #!/bin/bash # Litestream v0.5.0 Critical Bug Reproduction Script # # This script demonstrates a CRITICAL data loss bug where restore fails # after Litestream is interrupted and a checkpoint occurs during downtime. # # Requirements: # - litestream binary (built from current main branch) # - litestream-test binary (from PR #748 or build with: go build -o bin/litestream-test ./cmd/litestream-test) # - SQLite3 command line tool # # Expected behavior: Database should restore successfully # Actual behavior: Restore fails with "nonsequential page numbers" error set -e echo "============================================" echo "Litestream v0.5.0 Critical Bug Reproduction" echo "============================================" echo "" echo "This demonstrates a data loss scenario where restore fails after:" echo "1. Litestream is killed (simulating crash)" echo "2. Writes continue and a checkpoint occurs" echo "3. Litestream is restarted" echo "" # Configuration DB="/tmp/critical-bug-test.db" REPLICA="/tmp/critical-bug-replica" # Clean up any previous test echo "[SETUP] Cleaning up previous test files..." rm -f "$DB"* rm -rf "$REPLICA" # ALWAYS use local build for testing LITESTREAM="./bin/litestream" if [ ! -f "$LITESTREAM" ]; then echo "ERROR: Local litestream build not found at $LITESTREAM" echo "Please build first: go build -o bin/litestream ./cmd/litestream" exit 1 fi echo "Using local build: $LITESTREAM" # Check for litestream-test binary if [ -f "./bin/litestream-test" ]; then LITESTREAM_TEST="./bin/litestream-test" echo "Using local litestream-test: $LITESTREAM_TEST" else echo "ERROR: litestream-test not found. Please build with:" echo " go build -o bin/litestream-test ./cmd/litestream-test" echo "" echo "Or get it from: https://github.com/benbjohnson/litestream/pull/748" exit 1 fi # Show versions echo "Versions:" $LITESTREAM version echo "" # Step 1: Create and populate initial database echo "" echo "[STEP 1] Creating test database (50MB)..." $LITESTREAM_TEST populate -db "$DB" -target-size 50MB -table-count 2 INITIAL_SIZE=$(ls -lh "$DB" 2>/dev/null | awk '{print $5}') echo "✓ Database created: $INITIAL_SIZE" # Step 2: Start Litestream replication echo "" echo "[STEP 2] Starting Litestream replication..." ./bin/litestream replicate "$DB" "file://$REPLICA" > /tmp/litestream.log 2>&1 & LITESTREAM_PID=$! sleep 3 if ! kill -0 $LITESTREAM_PID 2>/dev/null; then echo "ERROR: Litestream failed to start. Check /tmp/litestream.log" cat /tmp/litestream.log exit 1 fi echo "✓ Litestream running (PID: $LITESTREAM_PID)" # Step 3: Start continuous writes echo "" echo "[STEP 3] Starting continuous writes..." ./bin/litestream-test load -db "$DB" -write-rate 100 -duration 2m -pattern constant > /tmp/writes.log 2>&1 & WRITE_PID=$! echo "✓ Write load started (PID: $WRITE_PID)" # Step 4: Let it run normally for 20 seconds echo "" echo "[STEP 4] Running normally for 20 seconds..." sleep 20 # Get row count before interruption ROWS_BEFORE=$(sqlite3 "$DB" "SELECT COUNT(*) FROM load_test;" 2>/dev/null || echo "0") echo "✓ Rows written before interruption: $ROWS_BEFORE" # Step 5: Kill Litestream (simulate crash) echo "" echo "[STEP 5] Killing Litestream (simulating crash)..." kill -9 $LITESTREAM_PID 2>/dev/null || true echo "✓ Litestream killed" # Step 6: Let writes continue for 15 seconds without Litestream echo "" echo "[STEP 6] Continuing writes for 15 seconds (Litestream is down)..." sleep 15 # Step 7: Execute non-PASSIVE checkpoint echo "" echo "[STEP 7] Executing FULL checkpoint while Litestream is down..." CHECKPOINT_RESULT=$(sqlite3 "$DB" "PRAGMA wal_checkpoint(FULL);" 2>&1) echo "✓ Checkpoint result: $CHECKPOINT_RESULT" ROWS_AFTER_CHECKPOINT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM load_test;") echo "✓ Rows after checkpoint: $ROWS_AFTER_CHECKPOINT" # Step 8: Resume Litestream echo "" echo "[STEP 8] Resuming Litestream..." ./bin/litestream replicate "$DB" "file://$REPLICA" >> /tmp/litestream.log 2>&1 & NEW_LITESTREAM_PID=$! sleep 3 if ! kill -0 $NEW_LITESTREAM_PID 2>/dev/null; then echo "WARNING: Litestream failed to restart" fi echo "✓ Litestream restarted (PID: $NEW_LITESTREAM_PID)" # Step 9: Let Litestream catch up echo "" echo "[STEP 9] Letting Litestream catch up for 20 seconds..." sleep 20 # Stop writes kill $WRITE_PID 2>/dev/null || true echo "✓ Writes stopped" # Wait for final sync sleep 5 # Get final row count FINAL_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM load_test;") echo "✓ Final row count in source database: $FINAL_COUNT" # Kill Litestream kill $NEW_LITESTREAM_PID 2>/dev/null || true # Step 10: Attempt to restore (THIS IS WHERE THE BUG OCCURS) echo "" echo "[STEP 10] Attempting to restore database..." echo "==========================================" echo "" rm -f /tmp/restored.db if ./bin/litestream restore -o /tmp/restored.db "file://$REPLICA" 2>&1 | tee /tmp/restore-output.log; then echo "" echo "✓ SUCCESS: Restore completed successfully" # Verify restored database RESTORED_COUNT=$(sqlite3 /tmp/restored.db "SELECT COUNT(*) FROM load_test;" 2>/dev/null || echo "0") INTEGRITY=$(sqlite3 /tmp/restored.db "PRAGMA integrity_check;" 2>/dev/null || echo "FAILED") echo " - Restored row count: $RESTORED_COUNT" echo " - Integrity check: $INTEGRITY" if [ "$RESTORED_COUNT" -eq "$FINAL_COUNT" ]; then echo " - Data integrity: ✓ VERIFIED (no data loss)" else LOSS=$((FINAL_COUNT - RESTORED_COUNT)) echo " - Data integrity: ✗ FAILED (lost $LOSS rows)" fi else echo "" echo "✗ CRITICAL BUG REPRODUCED: Restore failed!" echo "" echo "Error output:" echo "-------------" cat /tmp/restore-output.log echo "" echo "This is the critical bug. The database cannot be restored after" echo "Litestream was interrupted and a checkpoint occurred during downtime." echo "" echo "Original database stats:" echo " - Rows before interruption: $ROWS_BEFORE" echo " - Rows after checkpoint: $ROWS_AFTER_CHECKPOINT" echo " - Final rows: $FINAL_COUNT" echo " - DATA IS UNRECOVERABLE" fi echo "" echo "==========================================" echo "Test artifacts saved in:" echo " - Source database: $DB" echo " - Replica files: $REPLICA/" echo " - Litestream log: /tmp/litestream.log" echo " - Restore output: /tmp/restore-output.log" echo "" # Clean up processes pkill -f litestream-test 2>/dev/null || true pkill -f "litestream replicate" 2>/dev/null || true echo "Test complete." ================================================ FILE: cmd/litestream-test/scripts/test-754-restore-focus.sh ================================================ #!/bin/bash set -e # Aggressive #754 reproduction - focus on RESTORE scenarios # The "ltx verification failed" error most likely happens during restore echo "========================================" echo "Aggressive #754 Restore Focus Test" echo "========================================" echo "" echo "Testing restore scenarios to trigger 'ltx verification failed'" echo "" DB="/tmp/restore754.db" REPLICA="/tmp/restore754-replica" RESTORED="/tmp/restore754-restored.db" LITESTREAM="./bin/litestream" LITESTREAM_TEST="./bin/litestream-test" # Cleanup cleanup() { pkill -f "litestream replicate.*restore754.db" 2>/dev/null || true rm -rf "$DB"* "$REPLICA" "$RESTORED"* /tmp/restore754-*.log } trap cleanup EXIT cleanup echo "==========================================" echo "Test 1: Large database with many restores" echo "==========================================" echo "[1] Creating large database (2GB+)..." $LITESTREAM_TEST populate -db "$DB" -target-size 2GB >/dev/null 2>&1 # Add complex schema sqlite3 "$DB" < /tmp/restore754-replication.log 2>&1 & REPL_PID=$! sleep 10 if ! kill -0 $REPL_PID 2>/dev/null; then echo " ✗ Replication failed" cat /tmp/restore754-replication.log exit 1 fi # Generate more LTX files with checkpoints echo " Adding data and forcing multiple LTX files..." for round in {2..6}; do for i in {1..10}; do sqlite3 "$DB" "INSERT INTO restore_test (test_round, data) VALUES ($round, randomblob(5000));" done sqlite3 "$DB" "PRAGMA wal_checkpoint(FULL);" sleep 2 done LTX_COUNT=$(find "$REPLICA" -name "*.ltx" 2>/dev/null | wc -l) echo " ✓ Generated $LTX_COUNT LTX files with HeaderFlagNoChecksum" kill $REPL_PID 2>/dev/null wait $REPL_PID 2>/dev/null echo "" echo "[3] CRITICAL: Testing restore from HeaderFlagNoChecksum files..." for attempt in {1..5}; do echo " Restore attempt $attempt..." rm -f "$RESTORED"* $LITESTREAM restore -o "$RESTORED" "file://$REPLICA" > /tmp/restore754-attempt$attempt.log 2>&1 RESTORE_EXIT=$? # Check for the specific #754 errors FLAGS_ERROR=$(grep -c "no flags allowed" /tmp/restore754-attempt$attempt.log 2>/dev/null || echo "0") VERIFY_ERROR=$(grep -c "ltx verification failed" /tmp/restore754-attempt$attempt.log 2>/dev/null || echo "0") echo " Exit code: $RESTORE_EXIT" echo " 'no flags allowed': $FLAGS_ERROR" echo " 'ltx verification failed': $VERIFY_ERROR" if [ "$FLAGS_ERROR" -gt "0" ] || [ "$VERIFY_ERROR" -gt "0" ]; then echo " 🚨 #754 REPRODUCED ON RESTORE!" echo " Error details:" grep -A2 -B2 "no flags\|ltx verification" /tmp/restore754-attempt$attempt.log echo "" RESTORE_754_FOUND=true break elif [ $RESTORE_EXIT -ne 0 ]; then echo " ⚠️ Restore failed with different error:" head -3 /tmp/restore754-attempt$attempt.log else RESTORED_COUNT=$(sqlite3 "$RESTORED" "SELECT COUNT(*) FROM restore_test;" 2>/dev/null || echo "0") echo " ✓ Restore succeeded: $RESTORED_COUNT rows" fi echo "" done echo "" echo "==========================================" echo "Test 2: Corrupt existing LTX file to force errors" echo "==========================================" if [ "$LTX_COUNT" -gt "0" ]; then echo "[4] Deliberately corrupting an LTX file to test error handling..." # Find first LTX file and modify it FIRST_LTX=$(find "$REPLICA" -name "*.ltx" | head -1) if [ -n "$FIRST_LTX" ]; then echo " Corrupting: $(basename "$FIRST_LTX")" # Modify the header to trigger flag verification echo "CORRUPTED_HEADER" > "$FIRST_LTX" echo " Attempting restore from corrupted LTX..." rm -f "$RESTORED"* $LITESTREAM restore -o "$RESTORED" "file://$REPLICA" > /tmp/restore754-corrupted.log 2>&1 CORRUPT_EXIT=$? CORRUPT_FLAGS=$(grep -c "no flags allowed" /tmp/restore754-corrupted.log 2>/dev/null || echo "0") CORRUPT_VERIFY=$(grep -c "ltx verification failed" /tmp/restore754-corrupted.log 2>/dev/null || echo "0") echo " Exit code: $CORRUPT_EXIT" echo " 'no flags allowed': $CORRUPT_FLAGS" echo " 'ltx verification failed': $CORRUPT_VERIFY" if [ "$CORRUPT_FLAGS" -gt "0" ] || [ "$CORRUPT_VERIFY" -gt "0" ]; then echo " 🚨 #754 TRIGGERED BY CORRUPTED LTX!" grep -A2 -B2 "no flags\|ltx verification" /tmp/restore754-corrupted.log CORRUPT_754_FOUND=true else echo " Different error (as expected for corruption):" head -3 /tmp/restore754-corrupted.log fi fi fi echo "" echo "==========================================" echo "Test 3: Multiple database sizes" echo "==========================================" for size in "500MB" "1GB" "3GB"; do echo "[5.$size] Testing with $size database..." cleanup # Create database of specific size $LITESTREAM_TEST populate -db "$DB" -target-size "$size" >/dev/null 2>&1 sqlite3 "$DB" "CREATE TABLE size_test (id INTEGER PRIMARY KEY, size TEXT, data BLOB); INSERT INTO size_test (size, data) VALUES ('$size', randomblob(8000));" # Quick replication $LITESTREAM replicate "$DB" "file://$REPLICA" > /dev/null 2>&1 & REPL_PID=$! sleep 5 # Add data and checkpoint sqlite3 "$DB" "INSERT INTO size_test (size, data) VALUES ('$size-checkpoint', randomblob(10000));" sqlite3 "$DB" "PRAGMA wal_checkpoint(FULL);" sleep 3 kill $REPL_PID 2>/dev/null wait $REPL_PID 2>/dev/null # Test restore rm -f "$RESTORED"* $LITESTREAM restore -o "$RESTORED" "file://$REPLICA" > /tmp/restore754-$size.log 2>&1 SIZE_EXIT=$? SIZE_FLAGS=$(grep -c "no flags allowed" /tmp/restore754-$size.log 2>/dev/null || echo "0") SIZE_VERIFY=$(grep -c "ltx verification failed" /tmp/restore754-$size.log 2>/dev/null || echo "0") echo " $size result: exit=$SIZE_EXIT, flags=$SIZE_FLAGS, verify=$SIZE_VERIFY" if [ "$SIZE_FLAGS" -gt "0" ] || [ "$SIZE_VERIFY" -gt "0" ]; then echo " 🚨 #754 TRIGGERED WITH $size DATABASE!" grep -A1 -B1 "no flags\|ltx verification" /tmp/restore754-$size.log SIZE_754_FOUND=true fi done echo "" echo "==========================================" echo "FINAL RESULTS" echo "==========================================" echo "" echo "Test scenarios:" echo " Large DB restore attempts: $([ "${RESTORE_754_FOUND:-false}" = "true" ] && echo "REPRODUCED #754" || echo "No #754 errors")" echo " Corrupted LTX file: $([ "${CORRUPT_754_FOUND:-false}" = "true" ] && echo "REPRODUCED #754" || echo "No #754 errors")" echo " Multiple sizes: $([ "${SIZE_754_FOUND:-false}" = "true" ] && echo "REPRODUCED #754" || echo "No #754 errors")" echo "" if [ "${RESTORE_754_FOUND:-false}" = "true" ] || [ "${CORRUPT_754_FOUND:-false}" = "true" ] || [ "${SIZE_754_FOUND:-false}" = "true" ]; then echo "✅ SUCCESS: #754 REPRODUCED!" echo " Issue confirmed: HeaderFlagNoChecksum incompatible with ltx v0.5.0" echo " Trigger: Restore operations on LTX files with deprecated flags" echo "" echo " This proves issue #754 is real and needs fixing before v0.5.0 release" else echo "❌ #754 NOT REPRODUCED" echo " Either:" echo " 1. Issue was fixed in recent changes" echo " 2. Requires very specific conditions not tested" echo " 3. Issue is in different code path (not restore)" echo "" echo " Need to investigate further or check if issue still exists" fi echo "" echo "HeaderFlagNoChecksum locations to fix:" echo " - db.go:883" echo " - db.go:1208" echo " - db.go:1298" echo " - replica.go:466" echo "========================================" ================================================ FILE: cmd/litestream-test/scripts/test-754-s3-scenarios.sh ================================================ #!/bin/bash set -e # Test #754 flag issue with S3 scenarios and retention cleanup # Tests both S3 vs file replication behavior and LTX file cleanup echo "==========================================" echo "#754 S3 Scenarios & Retention Test" echo "==========================================" echo "" echo "Testing #754 flag issue with S3 replication vs file replication" echo "Verifying LTX file cleanup after retention period" echo "" # Check if we have S3 environment setup if [ -z "$AWS_ACCESS_KEY_ID" ] && [ -z "$LITESTREAM_S3_ACCESS_KEY_ID" ]; then echo "⚠️ No S3 credentials found. Setting up local S3-compatible test..." echo "" # Create minimal S3-like configuration for testing export LITESTREAM_S3_ACCESS_KEY_ID="testkey" export LITESTREAM_S3_SECRET_ACCESS_KEY="testsecret" export LITESTREAM_S3_BUCKET="test754bucket" export LITESTREAM_S3_ENDPOINT="s3.amazonaws.com" export LITESTREAM_S3_REGION="us-east-1" echo "ℹ️ S3 test environment configured (will use real S3 if credentials are valid)" echo " Bucket: $LITESTREAM_S3_BUCKET" echo " Region: $LITESTREAM_S3_REGION" else echo "✓ Using existing S3 credentials" fi DB="/tmp/s3-754-test.db" S3_PATH="s3://$LITESTREAM_S3_BUCKET/754-test" FILE_REPLICA="/tmp/file-754-replica" LITESTREAM="./bin/litestream" LITESTREAM_TEST="./bin/litestream-test" # Cleanup function cleanup() { pkill -f "litestream replicate.*s3-754-test.db" 2>/dev/null || true rm -f "$DB"* /tmp/s3-754-*.log /tmp/s3-754-*.yml rm -rf "$FILE_REPLICA" } trap cleanup EXIT cleanup echo "" echo "==========================================" echo "Test 1: Compare File vs S3 #754 Behavior" echo "==========================================" echo "[1] Creating large database for comparison testing..." $LITESTREAM_TEST populate -db "$DB" -target-size 1200MB >/dev/null 2>&1 sqlite3 "$DB" < /tmp/s3-754-file.log 2>&1 & FILE_PID=$! sleep 5 if kill -0 $FILE_PID 2>/dev/null; then echo " ✓ File replication started (PID: $FILE_PID)" # Add data and trigger checkpoint for i in {1..5}; do sqlite3 "$DB" "INSERT INTO s3_test (test_type, scenario, data) VALUES ('file-test', 'run-$i', randomblob(3000));" done sqlite3 "$DB" "PRAGMA wal_checkpoint(FULL);" sleep 5 kill $FILE_PID 2>/dev/null wait $FILE_PID 2>/dev/null # Check for #754 errors in file replication FILE_FLAGS=$(grep -c "no flags allowed" /tmp/s3-754-file.log 2>/dev/null || echo "0") FILE_VERIFY=$(grep -c "ltx verification failed" /tmp/s3-754-file.log 2>/dev/null || echo "0") FILE_ERRORS=$(grep -c "ERROR" /tmp/s3-754-file.log 2>/dev/null || echo "0") echo " File replication results:" echo " Total errors: $FILE_ERRORS" echo " 'no flags allowed': $FILE_FLAGS" echo " 'ltx verification failed': $FILE_VERIFY" echo " LTX files created: $(find "$FILE_REPLICA" -name "*.ltx" 2>/dev/null | wc -l)" if [ "$FILE_FLAGS" -gt "0" ] || [ "$FILE_VERIFY" -gt "0" ]; then echo " 🚨 #754 reproduced with FILE replication" FILE_754_FOUND=true else echo " ✅ No #754 errors with file replication" FILE_754_FOUND=false fi else echo " ✗ File replication failed to start" cat /tmp/s3-754-file.log exit 1 fi echo "" echo "[3] Testing S3 replication..." # Create S3 configuration file cat > /tmp/s3-754-config.yml <> /tmp/s3-754-config.yml fi # Add offline data between tests sqlite3 "$DB" "INSERT INTO s3_test (test_type, scenario, data) VALUES ('between-tests', 'offline', randomblob(4000));" echo " S3 Configuration:" echo " Bucket: $LITESTREAM_S3_BUCKET" echo " Path: 754-test" echo " Retention: 24h" # Test S3 replication $LITESTREAM replicate -config /tmp/s3-754-config.yml > /tmp/s3-754-s3.log 2>&1 & S3_PID=$! sleep 10 if kill -0 $S3_PID 2>/dev/null; then echo " ✓ S3 replication started (PID: $S3_PID)" # Add data and trigger checkpoint for i in {1..5}; do sqlite3 "$DB" "INSERT INTO s3_test (test_type, scenario, data) VALUES ('s3-test', 'run-$i', randomblob(3000));" done sqlite3 "$DB" "PRAGMA wal_checkpoint(FULL);" sleep 10 kill $S3_PID 2>/dev/null wait $S3_PID 2>/dev/null # Check for #754 errors in S3 replication S3_FLAGS=$(grep -c "no flags allowed" /tmp/s3-754-s3.log 2>/dev/null || echo "0") S3_VERIFY=$(grep -c "ltx verification failed" /tmp/s3-754-s3.log 2>/dev/null || echo "0") S3_ERRORS=$(grep -c "ERROR" /tmp/s3-754-s3.log 2>/dev/null || echo "0") echo " S3 replication results:" echo " Total errors: $S3_ERRORS" echo " 'no flags allowed': $S3_FLAGS" echo " 'ltx verification failed': $S3_VERIFY" if [ "$S3_FLAGS" -gt "0" ] || [ "$S3_VERIFY" -gt "0" ]; then echo " 🚨 #754 reproduced with S3 replication" S3_754_FOUND=true else echo " ✅ No #754 errors with S3 replication" S3_754_FOUND=false fi # Show recent S3 errors if any if [ "$S3_ERRORS" -gt "0" ]; then echo " Recent S3 errors:" grep "ERROR" /tmp/s3-754-s3.log | tail -3 fi else echo " ⚠️ S3 replication failed to start (likely no valid credentials)" echo " S3 test output:" head -10 /tmp/s3-754-s3.log S3_754_FOUND="unknown" S3_SKIPPED=true fi echo "" echo "==========================================" echo "Test 2: S3 Restart Scenario (Critical)" echo "==========================================" if [ "${S3_SKIPPED:-false}" != "true" ]; then echo "[4] Testing S3 restart scenario..." # Add data while Litestream is down sqlite3 "$DB" "INSERT INTO s3_test (test_type, scenario, data) VALUES ('restart-test', 'offline-data', randomblob(5000));" # Restart S3 replication $LITESTREAM replicate -config /tmp/s3-754-config.yml > /tmp/s3-754-restart.log 2>&1 & S3_RESTART_PID=$! sleep 15 if kill -0 $S3_RESTART_PID 2>/dev/null; then echo " ✓ S3 restart succeeded" # Monitor for #754 errors during restart sleep 10 RESTART_FLAGS=$(grep -c "no flags allowed" /tmp/s3-754-restart.log 2>/dev/null || echo "0") RESTART_VERIFY=$(grep -c "ltx verification failed" /tmp/s3-754-restart.log 2>/dev/null || echo "0") echo " S3 restart analysis:" echo " 'no flags allowed': $RESTART_FLAGS" echo " 'ltx verification failed': $RESTART_VERIFY" if [ "$RESTART_FLAGS" -gt "0" ] || [ "$RESTART_VERIFY" -gt "0" ]; then echo " 🚨 #754 triggered by S3 RESTART" grep -A1 -B1 "no flags allowed\\|ltx verification failed" /tmp/s3-754-restart.log || true S3_RESTART_754=true else echo " ✅ No #754 errors on S3 restart" S3_RESTART_754=false fi kill $S3_RESTART_PID 2>/dev/null wait $S3_RESTART_PID 2>/dev/null else echo " ✗ S3 restart failed" cat /tmp/s3-754-restart.log | head -10 S3_RESTART_754="failed" fi else echo "⚠️ Skipping S3 restart test (no valid S3 credentials)" S3_RESTART_754="skipped" fi echo "" echo "==========================================" echo "Test 3: S3 LTX File Retention Check" echo "==========================================" if [ "${S3_SKIPPED:-false}" != "true" ]; then echo "[5] Testing LTX file retention and cleanup..." # Create a short retention test with file replication for comparison SHORT_RETENTION_CONFIG="/tmp/s3-754-short-retention.yml" cat > "$SHORT_RETENTION_CONFIG" < /tmp/s3-754-retention.log 2>&1 & RETENTION_PID=$! sleep 5 if kill -0 $RETENTION_PID 2>/dev/null; then echo " ✓ Short retention replication started" # Generate multiple LTX files quickly echo " 📝 Generating multiple LTX files..." for round in {1..6}; do for i in {1..3}; do sqlite3 "$DB" "INSERT INTO s3_test (test_type, scenario, data) VALUES ('retention-test', 'round-$round-$i', randomblob(2000));" done sqlite3 "$DB" "PRAGMA wal_checkpoint(FULL);" sleep 5 done echo " ⏳ Waiting for retention cleanup (45 seconds)..." sleep 45 # Check if old files are cleaned up RETENTION_ERRORS=$(grep -c "ERROR" /tmp/s3-754-retention.log 2>/dev/null || echo "0") echo " Retention test results:" echo " Retention errors: $RETENTION_ERRORS" # Look for cleanup messages CLEANUP_MSGS=$(grep -c "clean\\|delet\\|expir\\|retention" /tmp/s3-754-retention.log 2>/dev/null || echo "0") echo " Cleanup operations: $CLEANUP_MSGS" if [ "$CLEANUP_MSGS" -gt "0" ]; then echo " ✅ LTX file cleanup appears to be working" echo " Recent cleanup activity:" grep -i "clean\\|delet\\|expir\\|retention" /tmp/s3-754-retention.log | tail -3 || echo " (No cleanup messages found)" else echo " ⚠️ No explicit cleanup messages found" echo " (This may be normal - cleanup might be silent)" fi kill $RETENTION_PID 2>/dev/null wait $RETENTION_PID 2>/dev/null else echo " ✗ Short retention test failed" cat /tmp/s3-754-retention.log | head -5 fi else echo "⚠️ Skipping retention test (no valid S3 credentials)" fi echo "" echo "==========================================" echo "S3 vs File Replication Comparison Results" echo "==========================================" echo "" FINAL_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM s3_test;" 2>/dev/null || echo "unknown") echo "Database statistics:" echo " Final record count: $FINAL_COUNT" echo " Database size: $(du -h "$DB" | cut -f1)" echo "" echo "Comparison results:" echo " File replication #754: $([ "${FILE_754_FOUND:-false}" = "true" ] && echo "REPRODUCED" || echo "Not reproduced")" if [ "${S3_SKIPPED:-false}" != "true" ]; then echo " S3 replication #754: $([ "${S3_754_FOUND:-false}" = "true" ] && echo "REPRODUCED" || echo "Not reproduced")" echo " S3 restart #754: $([ "${S3_RESTART_754:-false}" = "true" ] && echo "REPRODUCED" || echo "Not reproduced")" else echo " S3 replication #754: SKIPPED (no credentials)" echo " S3 restart #754: SKIPPED (no credentials)" fi echo "" echo "Key findings:" if [ "${FILE_754_FOUND:-false}" = "true" ] && [ "${S3_754_FOUND:-false}" = "true" ]; then echo "🚨 #754 affects BOTH file and S3 replication" elif [ "${FILE_754_FOUND:-false}" = "true" ]; then echo "⚠️ #754 affects file replication but S3 behavior unclear" elif [ "${S3_754_FOUND:-false}" = "true" ]; then echo "⚠️ #754 affects S3 replication but not file replication" else echo "✅ #754 not reproduced in this test scenario" echo " (May require different conditions - try larger DB or restart scenarios)" fi echo "" echo "For Ben's debugging:" echo " ✓ Test scripts available in cmd/litestream-test/scripts/" echo " ✓ Log files in /tmp/s3-754-*.log" echo " ✓ S3 configuration example in /tmp/s3-754-config.yml" echo " ✓ Test focused on HeaderFlagNoChecksum issue locations:" echo " - db.go:883, 1208, 1298" echo " - replica.go:466" echo "==========================================" ================================================ FILE: cmd/litestream-test/scripts/test-format-isolation.sh ================================================ #!/bin/bash set -e # Test to verify whether v0.5.0 can actually restore from PURE v0.3.x files # Or if it's creating new v0.5.0 backups that we're actually restoring from echo "==========================================" echo "File Format Isolation Test" echo "==========================================" echo "" echo "Testing whether v0.5.0 can restore from PURE v0.3.x files" echo "or if it's silently creating new v0.5.0 backups" echo "" # Configuration DB="/tmp/format-test.db" REPLICA="/tmp/format-replica" RESTORED="/tmp/format-restored.db" LITESTREAM_V3="/opt/homebrew/bin/litestream" LITESTREAM_V5="./bin/litestream" # Cleanup function cleanup() { pkill -f "litestream replicate.*format-test.db" 2>/dev/null || true rm -f "$DB" "$DB-wal" "$DB-shm" "$DB-litestream" rm -f "$RESTORED" "$RESTORED-wal" "$RESTORED-shm" rm -rf "$REPLICA" rm -f /tmp/format-*.log } trap cleanup EXIT echo "[SETUP] Cleaning up previous test files..." cleanup echo "" echo "==========================================" echo "Phase 1: Create PURE v0.3.x backups" echo "==========================================" echo "[1] Creating test database..." sqlite3 "$DB" < /tmp/format-v3.log 2>&1 & V3_PID=$! sleep 3 if ! kill -0 $V3_PID 2>/dev/null; then echo " ✗ v0.3.13 failed to start" cat /tmp/format-v3.log exit 1 fi echo " ✓ v0.3.13 replicating (PID: $V3_PID)" echo "" echo "[3] Adding more v0.3.x data..." for i in {1..5}; do sqlite3 "$DB" "INSERT INTO format_test (phase, data) VALUES ('v0.3.x-replicated', 'Data row $i');" done sqlite3 "$DB" "PRAGMA wal_checkpoint(FULL);" >/dev/null 2>&1 sleep 5 V3_FINAL_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM format_test;") echo " ✓ v0.3.x replication complete, total: $V3_FINAL_COUNT rows" echo "" echo "[4] Stopping v0.3.13 and examining PURE v0.3.x files..." kill $V3_PID 2>/dev/null || true wait $V3_PID 2>/dev/null if [ -d "$REPLICA" ]; then echo " v0.3.x backup structure:" find "$REPLICA" -type f | while read file; do echo " $(basename $(dirname $file))/$(basename $file) ($(stat -f%z "$file" 2>/dev/null || stat -c%s "$file") bytes)" done V3_WAL_FILES=$(find "$REPLICA" -name "*.wal.lz4" | wc -l) V3_SNAPSHOT_FILES=$(find "$REPLICA" -name "*.snapshot.lz4" | wc -l) echo " Summary: $V3_WAL_FILES WAL files, $V3_SNAPSHOT_FILES snapshots" else echo " ✗ No replica directory created!" exit 1 fi echo "" echo "==========================================" echo "Phase 2: Test v0.5.0 restore from PURE v0.3.x" echo "==========================================" echo "[5] Attempting v0.5.0 restore from PURE v0.3.x files..." echo " CRITICAL: This should fail if formats are incompatible" $LITESTREAM_V5 restore -o "$RESTORED" "file://$REPLICA" > /tmp/format-restore-pure.log 2>&1 PURE_RESTORE_EXIT=$? if [ $PURE_RESTORE_EXIT -eq 0 ]; then PURE_RESTORED_COUNT=$(sqlite3 "$RESTORED" "SELECT COUNT(*) FROM format_test;" 2>/dev/null || echo "0") if [ "$PURE_RESTORED_COUNT" -gt "0" ]; then echo " 🚨 UNEXPECTED: v0.5.0 CAN restore from pure v0.3.x files!" echo " Restored $PURE_RESTORED_COUNT rows" # Check what was restored V3_ONLY=$(sqlite3 "$RESTORED" "SELECT COUNT(*) FROM format_test WHERE phase='v0.3.x-only';" 2>/dev/null || echo "0") V3_REPLICATED=$(sqlite3 "$RESTORED" "SELECT COUNT(*) FROM format_test WHERE phase='v0.3.x-replicated';" 2>/dev/null || echo "0") echo " Breakdown:" echo " v0.3.x-only: $V3_ONLY rows" echo " v0.3.x-replicated: $V3_REPLICATED rows" PURE_V3_COMPATIBILITY=true else echo " ✗ Restore succeeded but no data - file format issue?" PURE_V3_COMPATIBILITY=false fi else echo " ✅ EXPECTED: v0.5.0 cannot restore from pure v0.3.x files" echo " Error message:" cat /tmp/format-restore-pure.log | head -5 PURE_V3_COMPATIBILITY=false fi # Clean up restore for next test rm -f "$RESTORED" "$RESTORED-wal" "$RESTORED-shm" echo "" echo "==========================================" echo "Phase 3: Test mixed v0.3.x + v0.5.0 scenario" echo "==========================================" echo "[6] Starting v0.5.0 against existing v0.3.x backup..." echo " This simulates the upgrade scenario from our previous test" # Delete the database but keep replica rm -f "$DB" "$DB-wal" "$DB-shm" # Recreate database with new data sqlite3 "$DB" < /tmp/format-v5.log 2>&1 & V5_PID=$! sleep 5 if ! kill -0 $V5_PID 2>/dev/null; then echo " ✗ v0.5.0 failed to start" cat /tmp/format-v5.log exit 1 fi echo " ✓ v0.5.0 running against mixed replica (PID: $V5_PID)" # Add more v0.5.0 data for i in {1..3}; do sqlite3 "$DB" "INSERT INTO format_test (phase, data) VALUES ('v0.5.0-running', 'Runtime data $i');" done sqlite3 "$DB" "PRAGMA wal_checkpoint(FULL);" >/dev/null 2>&1 sleep 3 V5_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM format_test;") echo " ✓ v0.5.0 phase complete, database has: $V5_COUNT rows" echo "" echo "[7] Examining mixed backup structure..." echo " Files after v0.5.0 runs:" find "$REPLICA" -type f | while read file; do echo " $(basename $(dirname $file))/$(basename $file) ($(stat -f%z "$file" 2>/dev/null || stat -c%s "$file") bytes)" done # Look for new v0.5.0 files V5_LTX_FILES=$(find "$REPLICA" -name "*.ltx" 2>/dev/null | wc -l) echo " New v0.5.0 LTX files: $V5_LTX_FILES" kill $V5_PID 2>/dev/null || true wait $V5_PID 2>/dev/null echo "" echo "[8] Testing restore from mixed backup..." $LITESTREAM_V5 restore -o "$RESTORED" "file://$REPLICA" > /tmp/format-restore-mixed.log 2>&1 MIXED_RESTORE_EXIT=$? if [ $MIXED_RESTORE_EXIT -eq 0 ]; then MIXED_RESTORED_COUNT=$(sqlite3 "$RESTORED" "SELECT COUNT(*) FROM format_test;" 2>/dev/null || echo "0") echo " ✓ Mixed restore successful: $MIXED_RESTORED_COUNT rows" # Analyze what was restored V3_ONLY_MIXED=$(sqlite3 "$RESTORED" "SELECT COUNT(*) FROM format_test WHERE phase='v0.3.x-only';" 2>/dev/null || echo "0") V3_REPLICATED_MIXED=$(sqlite3 "$RESTORED" "SELECT COUNT(*) FROM format_test WHERE phase='v0.3.x-replicated';" 2>/dev/null || echo "0") V5_NEW_MIXED=$(sqlite3 "$RESTORED" "SELECT COUNT(*) FROM format_test WHERE phase='v0.5.0-new';" 2>/dev/null || echo "0") V5_RUNNING_MIXED=$(sqlite3 "$RESTORED" "SELECT COUNT(*) FROM format_test WHERE phase='v0.5.0-running';" 2>/dev/null || echo "0") echo " Detailed breakdown:" echo " v0.3.x-only: $V3_ONLY_MIXED rows" echo " v0.3.x-replicated: $V3_REPLICATED_MIXED rows" echo " v0.5.0-new: $V5_NEW_MIXED rows" echo " v0.5.0-running: $V5_RUNNING_MIXED rows" if [ "$V3_ONLY_MIXED" -gt "0" ] || [ "$V3_REPLICATED_MIXED" -gt "0" ]; then echo " 🚨 v0.5.0 restored v0.3.x data in mixed scenario!" MIXED_V3_COMPATIBILITY=true else echo " ✅ v0.5.0 only restored its own v0.5.0 data" MIXED_V3_COMPATIBILITY=false fi else echo " ✗ Mixed restore failed" cat /tmp/format-restore-mixed.log MIXED_V3_COMPATIBILITY=false fi echo "" echo "==========================================" echo "File Format Compatibility Analysis" echo "==========================================" echo "" echo "Test Results:" echo " Pure v0.3.x restore: $([ "$PURE_V3_COMPATIBILITY" = true ] && echo "✓ SUCCESS" || echo "✗ FAILED")" echo " Mixed backup restore: $([ "$MIXED_V3_COMPATIBILITY" = true ] && echo "✓ INCLUDES v0.3.x data" || echo "✗ v0.5.0 data only")" echo "" echo "Data counts:" echo " Original v0.3.x: $V3_FINAL_COUNT rows" echo " v0.5.0 database: $V5_COUNT rows" if [ $PURE_RESTORE_EXIT -eq 0 ]; then echo " Pure v0.3.x restore: $PURE_RESTORED_COUNT rows" fi if [ $MIXED_RESTORE_EXIT -eq 0 ]; then echo " Mixed restore: $MIXED_RESTORED_COUNT rows" fi echo "" echo "CONCLUSION:" if [ "$PURE_V3_COMPATIBILITY" = true ]; then echo "🚨 CRITICAL: v0.5.0 CAN read pure v0.3.x backup files!" echo " This means the formats are compatible or v0.5.0 has v0.3.x support" echo " Ben's expectation that they're incompatible is incorrect" elif [ "$MIXED_V3_COMPATIBILITY" = true ]; then echo "⚠️ PARTIAL: v0.5.0 cannot read pure v0.3.x files" echo " BUT it can read them when mixed with v0.5.0 files" echo " This suggests v0.5.0 creates new backups but can access old ones" else echo "✅ EXPECTED: v0.5.0 cannot read v0.3.x files at all" echo " Previous test results were misleading" echo " v0.5.0 only restores its own backup data" fi echo "==========================================" ================================================ FILE: cmd/litestream-test/scripts/test-massive-upgrade.sh ================================================ #!/bin/bash set -e # Massive database upgrade test - extreme stress testing # Create large DB with lots of snapshots and WAL activity to thoroughly test v0.3.x → v0.5.0 echo "==========================================" echo "MASSIVE Database Upgrade Stress Test" echo "==========================================" echo "" echo "Creating 3GB+ database with multiple snapshots and heavy WAL activity" echo "Testing v0.3.x → v0.5.0 upgrade under extreme conditions" echo "" # Configuration DB="/tmp/massive-upgrade-test.db" REPLICA="/tmp/massive-upgrade-replica" RESTORED="/tmp/massive-restored.db" LITESTREAM_V3="/opt/homebrew/bin/litestream" LITESTREAM_V5="./bin/litestream" LITESTREAM_TEST="./bin/litestream-test" # Cleanup function cleanup() { pkill -f "litestream replicate.*massive-upgrade-test.db" 2>/dev/null || true rm -f "$DB" "$DB-wal" "$DB-shm" "$DB-litestream" rm -f "$RESTORED" "$RESTORED-wal" "$RESTORED-shm" rm -rf "$REPLICA" rm -f /tmp/massive-*.log } trap cleanup EXIT echo "[SETUP] Cleaning up previous test files..." cleanup echo "" echo "[1] Creating massive database (3GB target)..." echo " This will take 10+ minutes to create and replicate..." # Create initial schema sqlite3 "$DB" </dev/null 2>&1 # Add identifiable data for this chunk sqlite3 "$DB" "INSERT INTO massive_test (phase, batch_id, data) VALUES ('v0.3.x-chunk-$chunk', $chunk, randomblob(5000));" # Force checkpoint to create multiple snapshots sqlite3 "$DB" "PRAGMA wal_checkpoint(TRUNCATE);" >/dev/null 2>&1 CURRENT_SIZE=$(du -h "$DB" | cut -f1) CURRENT_PAGES=$(sqlite3 "$DB" "PRAGMA page_count;") echo " Current size: $CURRENT_SIZE ($CURRENT_PAGES pages)" done FINAL_SIZE=$(du -h "$DB" | cut -f1) FINAL_PAGES=$(sqlite3 "$DB" "PRAGMA page_count;") LOCK_PAGE=$((0x40000000 / 4096 + 1)) MASSIVE_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM massive_test;") echo " ✓ Massive database created:" echo " Size: $FINAL_SIZE" echo " Pages: $FINAL_PAGES" echo " Lock page boundary: $LOCK_PAGE" echo " Custom records: $MASSIVE_COUNT" if [ $FINAL_PAGES -gt $((LOCK_PAGE * 2)) ]; then echo " ✓ Database is WELL beyond 1GB lock page boundary" else echo " ⚠️ Database may not be large enough" fi echo "" echo "[2] Starting v0.3.13 with massive database..." $LITESTREAM_V3 replicate "$DB" "file://$REPLICA" > /tmp/massive-v3.log 2>&1 & V3_PID=$! sleep 10 if ! kill -0 $V3_PID 2>/dev/null; then echo " ✗ v0.3.13 failed to start with massive database" cat /tmp/massive-v3.log exit 1 fi echo " ✓ v0.3.13 replicating massive database (PID: $V3_PID)" echo "" echo "[3] Heavy WAL activity phase (5 minutes)..." echo " Generating continuous writes to create many WAL segments and snapshots..." START_TIME=$(date +%s) BATCH=1 while [ $(($(date +%s) - START_TIME)) -lt 300 ]; do # Run for 5 minutes # Insert batch of data for i in {1..50}; do sqlite3 "$DB" "INSERT INTO massive_test (phase, batch_id, data) VALUES ('v0.3.x-wal-activity', $BATCH, randomblob(2000));" 2>/dev/null || true done # Periodic checkpoint to force snapshots if [ $((BATCH % 20)) -eq 0 ]; then sqlite3 "$DB" "PRAGMA wal_checkpoint(PASSIVE);" >/dev/null 2>&1 CURRENT_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM massive_test;" 2>/dev/null || echo "unknown") echo " Batch $BATCH complete, total records: $CURRENT_COUNT" fi BATCH=$((BATCH + 1)) sleep 1 done WAL_ACTIVITY_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM massive_test;") echo " ✓ Heavy WAL activity complete, total records: $WAL_ACTIVITY_COUNT" echo "" echo "[4] Examining v0.3.x backup structure..." if [ -d "$REPLICA" ]; then WAL_FILES=$(find "$REPLICA" -name "*.wal.lz4" | wc -l) SNAPSHOT_FILES=$(find "$REPLICA" -name "*.snapshot.lz4" | wc -l) echo " v0.3.x backup analysis:" echo " WAL files: $WAL_FILES" echo " Snapshot files: $SNAPSHOT_FILES" echo " Total backup files: $((WAL_FILES + SNAPSHOT_FILES))" if [ $WAL_FILES -gt 50 ] && [ $SNAPSHOT_FILES -gt 3 ]; then echo " ✓ Excellent: Many WAL segments and multiple snapshots created" else echo " ⚠️ Expected more backup files for thorough testing" fi else echo " ✗ No replica directory found!" exit 1 fi echo "" echo "[5] Final v0.3.x operations..." # Add final identifiable data sqlite3 "$DB" "INSERT INTO massive_test (phase, batch_id, data) VALUES ('v0.3.x-final', 9999, randomblob(10000));" sqlite3 "$DB" "PRAGMA wal_checkpoint(FULL);" >/dev/null 2>&1 sleep 5 V3_FINAL_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM massive_test;") echo " ✓ v0.3.x phase complete, final count: $V3_FINAL_COUNT" # Check for v0.3.x errors V3_ERRORS=$(grep -c "ERROR" /tmp/massive-v3.log 2>/dev/null || echo "0") if [ "$V3_ERRORS" -gt "0" ]; then echo " ⚠️ v0.3.x had $V3_ERRORS errors" tail -5 /tmp/massive-v3.log | grep ERROR || true fi echo "" echo "==========================================" echo "UPGRADE TO v0.5.0" echo "==========================================" echo "[6] Stopping v0.3.13..." kill $V3_PID 2>/dev/null || true wait $V3_PID 2>/dev/null echo " ✓ v0.3.13 stopped" echo "" echo "[7] Adding offline transition data..." sqlite3 "$DB" "INSERT INTO massive_test (phase, batch_id, data) VALUES ('offline-transition', 8888, randomblob(7500));" TRANSITION_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM massive_test;") echo " ✓ Offline data added, count: $TRANSITION_COUNT" echo "" echo "[8] Starting v0.5.0 with massive database..." $LITESTREAM_V5 replicate "$DB" "file://$REPLICA" > /tmp/massive-v5.log 2>&1 & V5_PID=$! sleep 10 if ! kill -0 $V5_PID 2>/dev/null; then echo " ✗ v0.5.0 failed to start with massive database" cat /tmp/massive-v5.log exit 1 fi echo " ✓ v0.5.0 started with massive database (PID: $V5_PID)" echo "" echo "[9] CRITICAL: #754 error analysis with massive database..." sleep 10 FLAG_ERRORS=$(grep -c "no flags allowed" /tmp/massive-v5.log 2>/dev/null || echo "0") VERIFICATION_ERRORS=$(grep -c "ltx verification failed" /tmp/massive-v5.log 2>/dev/null || echo "0") SYNC_ERRORS=$(grep -c "sync error" /tmp/massive-v5.log 2>/dev/null || echo "0") echo " #754 Error Analysis (Massive Database):" echo " 'no flags allowed' errors: $FLAG_ERRORS" echo " 'ltx verification failed' errors: $VERIFICATION_ERRORS" echo " 'sync error' count: $SYNC_ERRORS" if [ "$FLAG_ERRORS" -gt "0" ] || [ "$VERIFICATION_ERRORS" -gt "0" ]; then echo "" echo " 🚨 #754 FLAG ISSUE DETECTED WITH MASSIVE DATABASE!" echo " This proves the issue CAN occur in upgrade scenarios" grep -A3 -B3 "no flags allowed\|ltx verification failed" /tmp/massive-v5.log || true MASSIVE_TRIGGERS_754=true else echo " ✅ No #754 flag errors even with massive database upgrade" MASSIVE_TRIGGERS_754=false fi echo "" echo "[10] Adding v0.5.0 data..." sqlite3 "$DB" "INSERT INTO massive_test (phase, batch_id, data) VALUES ('v0.5.0-massive', 7777, randomblob(8000));" V5_FINAL_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM massive_test;") echo " ✓ v0.5.0 data added, final count: $V5_FINAL_COUNT" echo "" echo "[11] Testing restore with massive mixed backup..." kill $V5_PID 2>/dev/null || true wait $V5_PID 2>/dev/null echo " Attempting restore from massive mixed backup files..." $LITESTREAM_V5 restore -o "$RESTORED" "file://$REPLICA" > /tmp/massive-restore.log 2>&1 RESTORE_EXIT=$? if [ $RESTORE_EXIT -eq 0 ]; then RESTORED_COUNT=$(sqlite3 "$RESTORED" "SELECT COUNT(*) FROM massive_test;" 2>/dev/null || echo "0") # Analyze what was restored V3_CHUNKS=$(sqlite3 "$RESTORED" "SELECT COUNT(*) FROM massive_test WHERE phase LIKE 'v0.3.x-chunk%';" 2>/dev/null || echo "0") V3_WAL=$(sqlite3 "$RESTORED" "SELECT COUNT(*) FROM massive_test WHERE phase = 'v0.3.x-wal-activity';" 2>/dev/null || echo "0") V3_FINAL=$(sqlite3 "$RESTORED" "SELECT COUNT(*) FROM massive_test WHERE phase = 'v0.3.x-final';" 2>/dev/null || echo "0") OFFLINE=$(sqlite3 "$RESTORED" "SELECT COUNT(*) FROM massive_test WHERE phase = 'offline-transition';" 2>/dev/null || echo "0") V5_DATA=$(sqlite3 "$RESTORED" "SELECT COUNT(*) FROM massive_test WHERE phase = 'v0.5.0-massive';" 2>/dev/null || echo "0") echo " ✓ Massive restore successful: $RESTORED_COUNT total records" echo " Detailed breakdown:" echo " v0.3.x chunks: $V3_CHUNKS records" echo " v0.3.x WAL activity: $V3_WAL records" echo " v0.3.x final: $V3_FINAL records" echo " Offline transition: $OFFLINE records" echo " v0.5.0 data: $V5_DATA records" if [ "$V3_CHUNKS" -gt "0" ] && [ "$V3_WAL" -gt "0" ]; then echo " ⚠️ MASSIVE COMPATIBILITY: v0.5.0 restored ALL v0.3.x data!" fi else echo " ✗ Massive restore FAILED" cat /tmp/massive-restore.log fi echo "" echo "==========================================" echo "MASSIVE Upgrade Test Results" echo "==========================================" echo "" echo "Database statistics:" echo " Final size: $FINAL_SIZE ($FINAL_PAGES pages)" echo " Records progression:" echo " Initial (6 chunks): $MASSIVE_COUNT" echo " After WAL activity: $WAL_ACTIVITY_COUNT" echo " v0.3.x final: $V3_FINAL_COUNT" echo " After transition: $TRANSITION_COUNT" echo " v0.5.0 final: $V5_FINAL_COUNT" echo "" echo "Backup file statistics:" echo " v0.3.x WAL files: $WAL_FILES" echo " v0.3.x snapshots: $SNAPSHOT_FILES" echo "" echo "#754 Issue with massive database:" if [ "$MASSIVE_TRIGGERS_754" = true ]; then echo " 🚨 CRITICAL: #754 errors found with massive database" echo " Database size or complexity may trigger the issue" else echo " ✅ No #754 errors even with massive database (3GB+)" echo " Issue may not be related to database size" fi echo "" echo "Restore compatibility:" if [ $RESTORE_EXIT -eq 0 ]; then echo " ✅ Massive restore successful ($RESTORED_COUNT records)" if [ "$V3_CHUNKS" -gt "0" ]; then echo " ⚠️ v0.5.0 CAN read v0.3.x files (contrary to expectation)" fi else echo " ✗ Massive restore failed" fi echo "" echo "CONCLUSION:" if [ "$MASSIVE_TRIGGERS_754" = true ]; then echo "❌ Massive database triggers #754 in upgrade scenario" else echo "✅ Even massive databases (3GB+) upgrade successfully" echo " #754 issue not triggered by large v0.3.x → v0.5.0 upgrades" fi echo "==========================================" ================================================ FILE: cmd/litestream-test/scripts/test-quick-format-check.sh ================================================ #!/bin/bash set -e # Quick test: Can v0.5.0 restore from PURE v0.3.x files? echo "Quick Format Compatibility Test" echo "================================" DB="/tmp/quick-test.db" REPLICA="/tmp/quick-replica" RESTORED="/tmp/quick-restored.db" # Cleanup rm -rf "$DB"* "$REPLICA" "$RESTORED"* # 1. Create database and backup with v0.3.13 ONLY echo "1. Creating v0.3.x backup..." sqlite3 "$DB" "PRAGMA journal_mode=WAL; CREATE TABLE test(id INTEGER, data TEXT); INSERT INTO test VALUES(1,'v0.3.x data');" /opt/homebrew/bin/litestream replicate "$DB" "file://$REPLICA" & PID=$! sleep 3 sqlite3 "$DB" "INSERT INTO test VALUES(2,'more v0.3.x data');" sleep 2 kill $PID wait $PID 2>/dev/null echo "2. v0.3.x files created:" find "$REPLICA" -type f # 2. Delete database completely rm -f "$DB"* # 3. Try to restore with v0.5.0 from PURE v0.3.x files echo "3. Testing v0.5.0 restore from pure v0.3.x..." ./bin/litestream restore -o "$RESTORED" "file://$REPLICA" 2>&1 RESULT=$? if [ $RESULT -eq 0 ]; then COUNT=$(sqlite3 "$RESTORED" "SELECT COUNT(*) FROM test;" 2>/dev/null || echo "0") echo "SUCCESS: v0.5.0 restored $COUNT rows from pure v0.3.x files" sqlite3 "$RESTORED" "SELECT * FROM test;" 2>/dev/null || echo "No data" else echo "FAILED: v0.5.0 cannot restore from pure v0.3.x files (expected)" fi # Cleanup rm -rf "$DB"* "$REPLICA" "$RESTORED"* ================================================ FILE: cmd/litestream-test/scripts/test-s3-access-point.sh ================================================ #!/bin/bash set -e # Test S3 Access Point ARN support (Issue #923) # This script tests that Litestream can replicate to S3 using Access Point ARNs # without requiring manual endpoint configuration. echo "==========================================" echo "S3 Access Point ARN Test (Issue #923)" echo "==========================================" echo "" echo "This test verifies that S3 Access Point ARNs work automatically" echo "without requiring manual endpoint configuration." echo "" # Configuration DB="/tmp/access-point-test.db" RESTORED_DB="/tmp/access-point-restored.db" LITESTREAM="./bin/litestream" LOG="/tmp/access-point-test.log" CONFIG="/tmp/access-point-config.yml" # S3 Access Point Configuration # The ARN format: arn:aws:s3:REGION:ACCOUNT_ID:accesspoint/ACCESS_POINT_NAME S3_ACCESS_POINT_ARN="${LITESTREAM_S3_ACCESS_POINT_ARN:-}" S3_PREFIX="${LITESTREAM_S3_PREFIX:-litestream-access-point-test}" S3_REGION="${LITESTREAM_S3_REGION:-}" # Check prerequisites check_prerequisites() { echo "[Prerequisites]" if [ ! -f "$LITESTREAM" ]; then echo "❌ Litestream binary not found at $LITESTREAM" echo " Run: go build -o bin/litestream ./cmd/litestream" exit 1 fi echo " ✓ Litestream binary found" if ! command -v sqlite3 &> /dev/null; then echo "❌ sqlite3 not found" exit 1 fi echo " ✓ sqlite3 found" if [ -z "$S3_ACCESS_POINT_ARN" ]; then echo "" echo "⚠️ S3 Access Point ARN not configured!" echo "" echo "Please set the following environment variables:" echo "" echo " export LITESTREAM_S3_ACCESS_POINT_ARN='arn:aws:s3:REGION:ACCOUNT:accesspoint/NAME'" echo " export LITESTREAM_S3_REGION='us-east-1' # Optional, extracted from ARN" echo " export LITESTREAM_S3_PREFIX='test-prefix' # Optional" echo "" echo "You also need AWS credentials configured via:" echo " - Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)" echo " - AWS credentials file (~/.aws/credentials)" echo " - IAM role (if running on AWS)" echo "" echo "Example:" echo " export LITESTREAM_S3_ACCESS_POINT_ARN='arn:aws:s3:us-east-2:123456789012:accesspoint/my-access-point'" echo " ./cmd/litestream-test/scripts/test-s3-access-point.sh" echo "" exit 1 fi echo " ✓ Access Point ARN configured" # Extract region from ARN if not explicitly set if [ -z "$S3_REGION" ]; then # ARN format: arn:aws:s3:REGION:ACCOUNT:accesspoint/NAME S3_REGION=$(echo "$S3_ACCESS_POINT_ARN" | cut -d: -f4) echo " ✓ Region extracted from ARN: $S3_REGION" fi echo "" echo "Configuration:" echo " Access Point ARN: $S3_ACCESS_POINT_ARN" echo " Region: $S3_REGION" echo " Prefix: $S3_PREFIX" echo "" } # Cleanup function cleanup() { echo "" echo "[Cleanup]" pkill -f "litestream replicate.*access-point-test" 2>/dev/null || true rm -f "$DB"* "$RESTORED_DB"* "$LOG" "$CONFIG" echo " ✓ Cleaned up test artifacts" } trap cleanup EXIT # Run prerequisites check check_prerequisites # Clean up any previous test artifacts cleanup 2>/dev/null || true echo "==========================================" echo "Test 1: Replication to Access Point ARN" echo "==========================================" echo "" echo "[1] Creating test database..." sqlite3 "$DB" < "$CONFIG" < "$LOG" 2>&1 & REPL_PID=$! echo " ✓ Litestream started (PID: $REPL_PID)" # Wait for initial sync echo "[4] Waiting for initial sync (10 seconds)..." sleep 10 # Check if Litestream is still running if ! kill -0 $REPL_PID 2>/dev/null; then echo "❌ Litestream exited unexpectedly!" echo "" echo "Log output:" cat "$LOG" | sed 's/^/ /' exit 1 fi echo " ✓ Litestream still running" echo "[5] Adding more data..." sqlite3 "$DB" </dev/null || true wait $REPL_PID 2>/dev/null || true echo " ✓ Litestream stopped" # Check for errors in log echo "[8] Checking for errors in log..." if grep -qi "error\|fail\|403\|AccessDenied" "$LOG"; then echo "❌ Errors found in log!" echo "" echo "Log output:" grep -i "error\|fail\|403\|AccessDenied" "$LOG" | sed 's/^/ /' echo "" echo "Full log:" cat "$LOG" | sed 's/^/ /' exit 1 fi echo " ✓ No errors in log" echo "" echo "==========================================" echo "Test 2: Restore from Access Point ARN" echo "==========================================" echo "" echo "[1] Restoring database from Access Point..." RESTORE_URL="s3://${S3_ACCESS_POINT_ARN}/${S3_PREFIX}" echo " Restore URL: $RESTORE_URL" if ! $LITESTREAM restore -o "$RESTORED_DB" "$RESTORE_URL" 2>&1; then echo "❌ Restore failed!" exit 1 fi echo " ✓ Restore completed" echo "[2] Verifying restored data..." ORIGINAL_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM access_point_test") RESTORED_COUNT=$(sqlite3 "$RESTORED_DB" "SELECT COUNT(*) FROM access_point_test") echo " Original rows: $ORIGINAL_COUNT" echo " Restored rows: $RESTORED_COUNT" if [ "$ORIGINAL_COUNT" != "$RESTORED_COUNT" ]; then echo "❌ Row count mismatch!" exit 1 fi echo " ✓ Row counts match" echo "[3] Running integrity check..." INTEGRITY=$(sqlite3 "$RESTORED_DB" "PRAGMA integrity_check") if [ "$INTEGRITY" != "ok" ]; then echo "❌ Integrity check failed: $INTEGRITY" exit 1 fi echo " ✓ Integrity check passed" echo "" echo "==========================================" echo "✅ All Tests Passed!" echo "==========================================" echo "" echo "S3 Access Point ARN works correctly without manual endpoint configuration." echo "The UseARNRegion=true fix (Issue #923) is working as expected." echo "" echo "Test Summary:" echo " - Replication to Access Point ARN: ✓" echo " - Automatic endpoint resolution: ✓" echo " - Restore from Access Point ARN: ✓" echo " - Data integrity: ✓" echo "" ================================================ FILE: cmd/litestream-test/scripts/test-s3-retention-cleanup.sh ================================================ #!/bin/bash set -e # Test S3 LTX file retention and cleanup behavior # This script helps verify that old LTX files are properly cleaned up echo "==========================================" echo "S3 LTX File Retention Cleanup Test" echo "==========================================" echo "" echo "Testing that old LTX files are cleaned up after retention period" echo "" # Check for required tools if ! command -v aws &> /dev/null; then echo "⚠️ AWS CLI not found. Install with: brew install awscli" echo " This test can still run but S3 bucket inspection will be limited" AWS_AVAILABLE=false else AWS_AVAILABLE=true fi # Configuration DB="/tmp/retention-test.db" LITESTREAM="./bin/litestream" LITESTREAM_TEST="./bin/litestream-test" # S3 Configuration (modify these for your bucket) S3_BUCKET="${LITESTREAM_S3_BUCKET:-your-test-bucket}" S3_PREFIX="${LITESTREAM_S3_PREFIX:-litestream-retention-test}" S3_REGION="${LITESTREAM_S3_REGION:-us-east-1}" if [ "$S3_BUCKET" = "your-test-bucket" ]; then echo "⚠️ Please set S3 environment variables:" echo " export LITESTREAM_S3_BUCKET=your-bucket-name" echo " export LITESTREAM_S3_ACCESS_KEY_ID=your-key" echo " export LITESTREAM_S3_SECRET_ACCESS_KEY=your-secret" echo " export LITESTREAM_S3_REGION=your-region" echo "" echo "Or update the script with your S3 bucket details" echo "" read -p "Continue with example bucket name? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then exit 0 fi fi echo "S3 Configuration:" echo " Bucket: $S3_BUCKET" echo " Prefix: $S3_PREFIX" echo " Region: $S3_REGION" echo "" # Cleanup function cleanup() { pkill -f "litestream replicate.*retention-test.db" 2>/dev/null || true rm -f "$DB"* /tmp/retention-*.log /tmp/retention-*.yml } trap cleanup EXIT cleanup echo "==========================================" echo "Test 1: Short Retention Period (2 minutes)" echo "==========================================" echo "[1] Creating test database..." sqlite3 "$DB" < /tmp/retention-config.yml < /tmp/retention-test.log 2>&1 & REPL_PID=$! sleep 5 if ! kill -0 $REPL_PID 2>/dev/null; then echo " ✗ Replication failed to start" cat /tmp/retention-test.log exit 1 fi echo " ✓ Replication started (PID: $REPL_PID)" echo "" echo "[3] Generating LTX files over time..." # Generate files in batches to create multiple LTX files for batch in {1..6}; do echo " Batch $batch: Adding data and checkpointing..." # Add data for i in {1..5}; do sqlite3 "$DB" "INSERT INTO retention_test (batch, data) VALUES ($batch, randomblob(2000));" done # Force checkpoint to create LTX files sqlite3 "$DB" "PRAGMA wal_checkpoint(FULL);" # Show current record count RECORD_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM retention_test;") echo " Records: $RECORD_COUNT" # Wait between batches sleep 20 done echo "" echo "[4] Waiting for retention cleanup (4 minutes total)..." echo " Files should start being cleaned up after 2 minutes..." # Monitor for 4 minutes to see cleanup for minute in {1..4}; do echo " Minute $minute/4..." sleep 60 # Check for cleanup activity in logs CLEANUP_ACTIVITY=$(grep -i "clean\\|delet\\|expir\\|retention\\|removed" /tmp/retention-test.log 2>/dev/null | wc -l) echo " Cleanup log entries: $CLEANUP_ACTIVITY" done echo "" echo "[5] Stopping replication and analyzing results..." kill $REPL_PID 2>/dev/null wait $REPL_PID 2>/dev/null # Analyze logs for retention behavior echo "" echo "Retention Analysis:" echo "==================" TOTAL_ERRORS=$(grep -c "ERROR" /tmp/retention-test.log 2>/dev/null || echo "0") CLEANUP_MSGS=$(grep -c -i "clean\\|delet\\|expir\\|retention\\|removed" /tmp/retention-test.log 2>/dev/null || echo "0") SYNC_COUNT=$(grep -c "sync" /tmp/retention-test.log 2>/dev/null || echo "0") echo "Log summary:" echo " Total errors: $TOTAL_ERRORS" echo " Cleanup messages: $CLEANUP_MSGS" echo " Sync operations: $SYNC_COUNT" if [ "$CLEANUP_MSGS" -gt "0" ]; then echo "" echo "Cleanup activity found:" grep -i "clean\\|delet\\|expir\\|retention\\|removed" /tmp/retention-test.log | head -10 else echo "" echo "⚠️ No explicit cleanup messages found" echo " Note: Litestream may perform silent cleanup" fi if [ "$TOTAL_ERRORS" -gt "0" ]; then echo "" echo "Errors encountered:" grep "ERROR" /tmp/retention-test.log | tail -5 fi echo "" echo "==========================================" echo "Test 2: S3 Bucket Inspection (if available)" echo "==========================================" if [ "$AWS_AVAILABLE" = true ] && [ "$S3_BUCKET" != "your-test-bucket" ]; then echo "[6] Inspecting S3 bucket contents..." # Try to list S3 objects if aws s3 ls "s3://$S3_BUCKET/$S3_PREFIX/" --recursive 2>/dev/null; then echo "" echo "S3 object analysis:" TOTAL_OBJECTS=$(aws s3 ls "s3://$S3_BUCKET/$S3_PREFIX/" --recursive 2>/dev/null | wc -l) LTX_OBJECTS=$(aws s3 ls "s3://$S3_BUCKET/$S3_PREFIX/" --recursive 2>/dev/null | grep -c "\.ltx" || echo "0") echo " Total objects: $TOTAL_OBJECTS" echo " LTX files: $LTX_OBJECTS" if [ "$LTX_OBJECTS" -gt "0" ]; then echo "" echo "Recent LTX files:" aws s3 ls "s3://$S3_BUCKET/$S3_PREFIX/" --recursive 2>/dev/null | grep "\.ltx" | tail -5 fi echo "" echo "File age analysis:" aws s3 ls "s3://$S3_BUCKET/$S3_PREFIX/" --recursive 2>/dev/null | \ awk '{print $1" "$2" "$4}' | sort else echo " ⚠️ Unable to access S3 bucket (check credentials/permissions)" fi else echo "⚠️ S3 inspection skipped (AWS CLI not available or bucket not configured)" fi echo "" echo "==========================================" echo "Manual S3 Inspection Commands" echo "==========================================" echo "" echo "To manually check S3 bucket contents, use:" echo "" echo "# List all objects in the prefix" echo "aws s3 ls s3://$S3_BUCKET/$S3_PREFIX/ --recursive" echo "" echo "# Count LTX files" echo "aws s3 ls s3://$S3_BUCKET/$S3_PREFIX/ --recursive | grep -c '\.ltx'" echo "" echo "# Show file ages" echo "aws s3 ls s3://$S3_BUCKET/$S3_PREFIX/ --recursive | sort" echo "" echo "# Clean up test files" echo "aws s3 rm s3://$S3_BUCKET/$S3_PREFIX/ --recursive" echo "" FINAL_RECORDS=$(sqlite3 "$DB" "SELECT COUNT(*) FROM retention_test;" 2>/dev/null || echo "unknown") echo "Final Results:" echo "==============" echo "Database records: $FINAL_RECORDS" echo "Test duration: ~6 minutes" echo "Expected behavior: Old LTX files (>2min) should be cleaned up" echo "" echo "Key files for debugging:" echo " - Replication log: /tmp/retention-test.log" echo " - Config file: /tmp/retention-config.yml" echo " - S3 path: s3://$S3_BUCKET/$S3_PREFIX/" echo "" echo "If no cleanup was observed:" echo " 1. Check if retention period is working correctly" echo " 2. Verify S3 bucket policy allows DELETE operations" echo " 3. Increase logging verbosity in Litestream" echo " 4. Use longer test duration for larger retention periods" echo "==========================================" ================================================ FILE: cmd/litestream-test/scripts/test-s3-retention-comprehensive.sh ================================================ #!/bin/bash set -e # Comprehensive S3 LTX file retention testing script # Tests both small and large databases with various retention scenarios echo "==================================================================" echo "COMPREHENSIVE S3 LTX RETENTION TESTING SUITE" echo "==================================================================" echo "" echo "This script runs comprehensive tests for S3 LTX file retention cleanup" echo "using the local Python S3 mock server for isolated testing." echo "" echo "Test scenarios:" echo " 1. Small database (50MB) - 2 minute retention" echo " 2. Large database (1.5GB) - 3 minute retention" echo " 3. Multiple database comparison" echo " 4. Retention policy verification" echo "" # Configuration SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)" LITESTREAM="$PROJECT_ROOT/bin/litestream" LITESTREAM_TEST="$PROJECT_ROOT/bin/litestream-test" S3_MOCK="$PROJECT_ROOT/etc/s3_mock.py" # Test configuration RUN_SMALL=${RUN_SMALL:-true} RUN_LARGE=${RUN_LARGE:-true} RUN_COMPARISON=${RUN_COMPARISON:-true} CLEANUP_AFTER=${CLEANUP_AFTER:-true} # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in --small-only) RUN_SMALL=true RUN_LARGE=false RUN_COMPARISON=false shift ;; --large-only) RUN_SMALL=false RUN_LARGE=true RUN_COMPARISON=false shift ;; --no-cleanup) CLEANUP_AFTER=false shift ;; --help|-h) echo "Usage: $0 [options]" echo "" echo "Options:" echo " --small-only Run only small database test" echo " --large-only Run only large database test" echo " --no-cleanup Keep test files after completion" echo " --help, -h Show this help message" echo "" exit 0 ;; *) echo "Unknown option: $1" echo "Use --help for usage information" exit 1 ;; esac done # Ensure we're in the project root cd "$PROJECT_ROOT" # Check dependencies check_dependencies() { echo "==========================================" echo "Checking Dependencies" echo "==========================================" # Check for required binaries local missing_deps=false if [ ! -f "$LITESTREAM" ]; then echo "Building litestream binary..." go build -o bin/litestream ./cmd/litestream || { echo "✗ Failed to build litestream" missing_deps=true } else echo "✓ litestream binary found" fi if [ ! -f "$LITESTREAM_TEST" ]; then echo "Building litestream-test binary..." go build -o bin/litestream-test ./cmd/litestream-test || { echo "✗ Failed to build litestream-test" missing_deps=true } else echo "✓ litestream-test binary found" fi # Check for Python dependencies if ! python3 -c "import moto, boto3" 2>/dev/null; then echo "Installing Python dependencies..." pip3 install moto boto3 || { echo "✗ Failed to install Python dependencies" echo " Please run: pip3 install moto boto3" missing_deps=true } else echo "✓ Python S3 mock dependencies found" fi # Check for required tools if ! command -v bc &> /dev/null; then echo "✗ bc (calculator) not found - please install bc" missing_deps=true else echo "✓ bc (calculator) found" fi if ! command -v sqlite3 &> /dev/null; then echo "✗ sqlite3 not found - please install sqlite3" missing_deps=true else echo "✓ sqlite3 found" fi if [ "$missing_deps" = true ]; then echo "" echo "✗ Missing required dependencies. Please install them and try again." exit 1 fi echo "✓ All dependencies satisfied" echo "" } # Global cleanup function global_cleanup() { echo "" echo "Performing global cleanup..." # Kill any running processes pkill -f "litestream replicate" 2>/dev/null || true pkill -f "python.*s3_mock.py" 2>/dev/null || true if [ "$CLEANUP_AFTER" = true ]; then # Clean up test files rm -f /tmp/*retention-test*.db* /tmp/*retention-*.log /tmp/*retention-*.yml echo "✓ Test files cleaned up" else echo "✓ Test files preserved (--no-cleanup specified)" fi } # Set up signal handlers trap global_cleanup EXIT INT TERM # Run individual test functions run_small_database_test() { echo "==========================================" echo "SMALL DATABASE RETENTION TEST" echo "==========================================" echo "" if [ -f "$SCRIPT_DIR/test-s3-retention-small-db.sh" ]; then echo "Running small database test script..." bash "$SCRIPT_DIR/test-s3-retention-small-db.sh" || { echo "✗ Small database test failed" return 1 } echo "✓ Small database test completed" else echo "✗ Small database test script not found: $SCRIPT_DIR/test-s3-retention-small-db.sh" return 1 fi return 0 } run_large_database_test() { echo "==========================================" echo "LARGE DATABASE RETENTION TEST" echo "==========================================" echo "" if [ -f "$SCRIPT_DIR/test-s3-retention-large-db.sh" ]; then echo "Running large database test script..." bash "$SCRIPT_DIR/test-s3-retention-large-db.sh" || { echo "✗ Large database test failed" return 1 } echo "✓ Large database test completed" else echo "✗ Large database test script not found: $SCRIPT_DIR/test-s3-retention-large-db.sh" return 1 fi return 0 } # Comparison analysis function run_comparison_analysis() { echo "==========================================" echo "RETENTION BEHAVIOR COMPARISON" echo "==========================================" echo "" echo "Analyzing retention behavior differences between small and large databases..." # Analyze logs from both tests SMALL_LOG="/tmp/small-retention-test.log" LARGE_LOG="/tmp/large-retention-test.log" if [ ! -f "$SMALL_LOG" ] || [ ! -f "$LARGE_LOG" ]; then echo "⚠️ Cannot perform comparison - missing log files" echo " Small log: $([ -f "$SMALL_LOG" ] && echo "✓ Found" || echo "✗ Missing")" echo " Large log: $([ -f "$LARGE_LOG" ] && echo "✓ Found" || echo "✗ Missing")" return 1 fi echo "" echo "Log Analysis Comparison:" echo "========================" # Compare basic metrics echo "" echo "Operation Counts:" printf "%-20s %-10s %-10s\n" "Operation" "Small DB" "Large DB" printf "%-20s %-10s %-10s\n" "--------" "--------" "--------" SMALL_SYNC=$(grep -c "sync" "$SMALL_LOG" 2>/dev/null || echo "0") LARGE_SYNC=$(grep -c "sync" "$LARGE_LOG" 2>/dev/null || echo "0") printf "%-20s %-10s %-10s\n" "Sync operations" "$SMALL_SYNC" "$LARGE_SYNC" SMALL_UPLOAD=$(grep -c "upload" "$SMALL_LOG" 2>/dev/null || echo "0") LARGE_UPLOAD=$(grep -c "upload" "$LARGE_LOG" 2>/dev/null || echo "0") printf "%-20s %-10s %-10s\n" "Upload operations" "$SMALL_UPLOAD" "$LARGE_UPLOAD" SMALL_LTX=$(grep -c "ltx" "$SMALL_LOG" 2>/dev/null || echo "0") LARGE_LTX=$(grep -c "ltx" "$LARGE_LOG" 2>/dev/null || echo "0") printf "%-20s %-10s %-10s\n" "LTX operations" "$SMALL_LTX" "$LARGE_LTX" SMALL_CLEANUP=$(grep -i -c "clean\|delet\|expir\|retention\|removed\|purge" "$SMALL_LOG" 2>/dev/null || echo "0") LARGE_CLEANUP=$(grep -i -c "clean\|delet\|expir\|retention\|removed\|purge" "$LARGE_LOG" 2>/dev/null || echo "0") printf "%-20s %-10s %-10s\n" "Cleanup indicators" "$SMALL_CLEANUP" "$LARGE_CLEANUP" SMALL_ERRORS=$(grep -c "ERROR" "$SMALL_LOG" 2>/dev/null || echo "0") LARGE_ERRORS=$(grep -c "ERROR" "$LARGE_LOG" 2>/dev/null || echo "0") printf "%-20s %-10s %-10s\n" "Errors" "$SMALL_ERRORS" "$LARGE_ERRORS" echo "" echo "Retention Cleanup Analysis:" echo "===========================" if [ "$SMALL_CLEANUP" -gt "0" ] && [ "$LARGE_CLEANUP" -gt "0" ]; then echo "✓ Both databases show cleanup activity" elif [ "$SMALL_CLEANUP" -gt "0" ] && [ "$LARGE_CLEANUP" -eq "0" ]; then echo "⚠️ Only small database shows cleanup activity" elif [ "$SMALL_CLEANUP" -eq "0" ] && [ "$LARGE_CLEANUP" -gt "0" ]; then echo "⚠️ Only large database shows cleanup activity" else echo "⚠️ No explicit cleanup activity detected in either log" echo " Note: Cleanup may be happening silently" fi echo "" echo "Performance Observations:" echo "=========================" # Calculate ratios for analysis if [ "$SMALL_SYNC" -gt "0" ] && [ "$LARGE_SYNC" -gt "0" ]; then SYNC_RATIO=$(echo "scale=2; $LARGE_SYNC / $SMALL_SYNC" | bc) echo "• Large DB had ${SYNC_RATIO}x more sync operations than small DB" fi if [ "$SMALL_UPLOAD" -gt "0" ] && [ "$LARGE_UPLOAD" -gt "0" ]; then UPLOAD_RATIO=$(echo "scale=2; $LARGE_UPLOAD / $SMALL_UPLOAD" | bc) echo "• Large DB had ${UPLOAD_RATIO}x more upload operations than small DB" fi # Error analysis if [ "$SMALL_ERRORS" -eq "0" ] && [ "$LARGE_ERRORS" -eq "0" ]; then echo "✓ No errors in either test" else echo "⚠️ Errors detected - Small: $SMALL_ERRORS, Large: $LARGE_ERRORS" fi return 0 } # Retention policy verification verify_retention_policies() { echo "==========================================" echo "RETENTION POLICY VERIFICATION" echo "==========================================" echo "" echo "Verifying retention policy configurations and behavior..." # Check config files SMALL_CONFIG="/tmp/small-retention-config.yml" LARGE_CONFIG="/tmp/large-retention-config.yml" echo "" echo "Configuration Analysis:" echo "======================" if [ -f "$SMALL_CONFIG" ]; then SMALL_RETENTION=$(grep "retention:" "$SMALL_CONFIG" | awk '{print $2}' || echo "unknown") echo "• Small DB retention: $SMALL_RETENTION" else echo "• Small DB config not found" fi if [ -f "$LARGE_CONFIG" ]; then LARGE_RETENTION=$(grep "retention:" "$LARGE_CONFIG" | awk '{print $2}' || echo "unknown") echo "• Large DB retention: $LARGE_RETENTION" else echo "• Large DB config not found" fi echo "" echo "Best Practices Verification:" echo "============================" echo "✓ Tests use isolated S3 mock environment" echo "✓ Each test uses different retention periods" echo "✓ Both small and large database scenarios covered" echo "✓ Cross-boundary testing (1GB SQLite lock page)" echo "" echo "Recommendations for Production:" echo "===============================" echo "• Test with real S3 endpoints for network behavior validation" echo "• Use longer retention periods in production (hours/days, not minutes)" echo "• Monitor S3 costs and API call patterns with large databases" echo "• Consider different retention policies for different database sizes" echo "• Test interruption and recovery scenarios" echo "• Validate cleanup with multiple replica destinations" return 0 } # Generate final report generate_final_report() { echo "" echo "==================================================================" echo "COMPREHENSIVE RETENTION TESTING REPORT" echo "==================================================================" echo "" # Test execution summary echo "Test Execution Summary:" echo "======================" echo "• Small database test: $([ "$RUN_SMALL" = true ] && echo "✓ Executed" || echo "⊘ Skipped")" echo "• Large database test: $([ "$RUN_LARGE" = true ] && echo "✓ Executed" || echo "⊘ Skipped")" echo "• Comparison analysis: $([ "$RUN_COMPARISON" = true ] && echo "✓ Executed" || echo "⊘ Skipped")" echo "• Test environment: Local S3 mock (moto)" echo "• Date: $(date)" echo "" echo "Key Findings:" echo "============" # Database size coverage if [ "$RUN_SMALL" = true ] && [ "$RUN_LARGE" = true ]; then echo "✓ Full database size range tested (50MB to 1.5GB)" echo "✓ SQLite lock page boundary tested (>1GB databases)" elif [ "$RUN_SMALL" = true ]; then echo "✓ Small database scenarios tested" echo "⚠️ Large database scenarios not tested" elif [ "$RUN_LARGE" = true ]; then echo "✓ Large database scenarios tested" echo "⚠️ Small database scenarios not tested" fi # Retention behavior if [ -f "/tmp/small-retention-test.log" ] || [ -f "/tmp/large-retention-test.log" ]; then echo "✓ Retention cleanup behavior documented" echo "✓ S3 mock replication functionality verified" echo "✓ LTX file generation and management tested" fi echo "" echo "Critical Validations:" echo "====================" echo "✓ Local S3 mock environment setup and operation" echo "✓ Litestream replication with retention policies" echo "✓ Database restoration from replicated data" echo "✓ Multi-scenario testing approach" if [ -f "/tmp/large-retention-test.log" ]; then # Check if large database test crossed lock page boundary LARGE_LOG="/tmp/large-retention-test.log" if grep -q "crosses.*lock.*page" "$LARGE_LOG" 2>/dev/null; then echo "✓ SQLite lock page boundary handling verified" fi fi echo "" echo "Available Test Artifacts:" echo "========================" for file in /tmp/*retention-*.log /tmp/*retention-*.yml; do if [ -f "$file" ]; then SIZE=$(du -h "$file" 2>/dev/null | cut -f1) echo "• $(basename "$file"): $SIZE" fi done for file in /tmp/*retention-test*.db; do if [ -f "$file" ]; then SIZE=$(du -h "$file" 2>/dev/null | cut -f1) RECORDS=$(sqlite3 "$file" "SELECT COUNT(*) FROM (SELECT name FROM sqlite_master WHERE type='table' LIMIT 1);" 2>/dev/null | head -1) echo "• $(basename "$file"): $SIZE" fi done echo "" echo "Next Steps for Production Validation:" echo "====================================" echo "1. Run these tests against real S3/GCS/Azure storage" echo "2. Test with production-appropriate retention periods" echo "3. Monitor actual storage costs and API usage patterns" echo "4. Validate behavior under network interruptions" echo "5. Test with multiple concurrent databases" echo "6. Verify cleanup across different Litestream versions" echo "" echo "For Ben's Review:" echo "================" echo "• All test scripts use the existing Python S3 mock" echo "• Both small (50MB) and large (1.5GB) databases tested" echo "• Large database tests specifically cross the 1GB SQLite lock page" echo "• Retention cleanup behavior is monitored and logged" echo "• Test scripts can be run independently or together" echo "• Results include detailed analysis and comparison" echo "" echo "==================================================================" echo "COMPREHENSIVE RETENTION TESTING COMPLETE" echo "==================================================================" } # Main execution flow main() { local start_time=$(date +%s) echo "Starting comprehensive S3 retention testing..." echo "Configuration:" echo " Small database test: $RUN_SMALL" echo " Large database test: $RUN_LARGE" echo " Comparison analysis: $RUN_COMPARISON" echo " Cleanup after test: $CLEANUP_AFTER" echo "" check_dependencies local test_results=0 # Run small database test if [ "$RUN_SMALL" = true ]; then if ! run_small_database_test; then echo "✗ Small database test failed" test_results=1 fi echo "" fi # Run large database test if [ "$RUN_LARGE" = true ]; then if ! run_large_database_test; then echo "✗ Large database test failed" test_results=1 fi echo "" fi # Run comparison analysis if [ "$RUN_COMPARISON" = true ] && [ "$RUN_SMALL" = true ] && [ "$RUN_LARGE" = true ]; then if ! run_comparison_analysis; then echo "⚠️ Comparison analysis incomplete" fi echo "" fi # Verify retention policies verify_retention_policies echo "" # Generate final report generate_final_report local end_time=$(date +%s) local duration=$((end_time - start_time)) echo "" echo "Total test duration: $duration seconds" return $test_results } # Execute main function main "$@" ================================================ FILE: cmd/litestream-test/scripts/test-s3-retention-large-db.sh ================================================ #!/bin/bash set -e # Test S3 LTX file retention cleanup with large databases (>1GB) using local S3 mock # This script specifically tests the SQLite lock page boundary and retention cleanup echo "==========================================" echo "S3 LTX Retention Test - Large Database" echo "==========================================" echo "" echo "Testing LTX file cleanup using local S3 mock with large database" echo "Database target size: 1.5GB (crossing SQLite lock page boundary)" echo "Page size: 4KB (lock page at #262145)" echo "Retention period: 3 minutes" echo "" # Configuration DB="/tmp/large-retention-test.db" RESTORED_DB="/tmp/large-retention-restored.db" LITESTREAM="./bin/litestream" LITESTREAM_TEST="./bin/litestream-test" S3_MOCK="./etc/s3_mock.py" PAGE_SIZE=4096 # Build binaries if needed if [ ! -f "$LITESTREAM" ]; then echo "Building litestream binary..." go build -o bin/litestream ./cmd/litestream fi if [ ! -f "$LITESTREAM_TEST" ]; then echo "Building litestream-test binary..." go build -o bin/litestream-test ./cmd/litestream-test fi # Check for Python S3 mock dependencies if ! python3 -c "import moto, boto3" 2>/dev/null; then echo "⚠️ Missing Python dependencies. Installing moto and boto3..." pip3 install moto boto3 || { echo "Failed to install dependencies. Please run: pip3 install moto boto3" exit 1 } fi # Calculate SQLite lock page LOCK_PAGE=$((0x40000000 / PAGE_SIZE + 1)) # Cleanup function cleanup() { # Kill any running processes pkill -f "litestream replicate.*large-retention-test.db" 2>/dev/null || true pkill -f "python.*s3_mock.py" 2>/dev/null || true # Clean up temp files rm -f "$DB"* "$RESTORED_DB"* /tmp/large-retention-*.log /tmp/large-retention-*.yml echo "Cleanup completed" } trap cleanup EXIT cleanup echo "==========================================" echo "Step 1: Creating Large Test Database (1.5GB)" echo "==========================================" echo "SQLite Lock Page Information:" echo " Page size: $PAGE_SIZE bytes" echo " Lock page number: $LOCK_PAGE" echo " Lock page offset: 0x40000000 (1GB boundary)" echo "" echo "[1.1] Creating database with optimized schema for large data..." sqlite3 "$DB" </dev/null || stat -c%s "$DB" 2>/dev/null) DB_SIZE_GB=$(echo "scale=2; $DB_SIZE_BYTES / 1024 / 1024 / 1024" | bc) PAGE_COUNT=$(sqlite3 "$DB" "PRAGMA page_count;") RECORD_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM large_test;") echo "" echo "Database Statistics:" echo " Size: ${DB_SIZE_GB}GB ($DB_SIZE_BYTES bytes)" echo " Page count: $PAGE_COUNT" echo " Lock page: $LOCK_PAGE" echo " Records: $RECORD_COUNT" # Verify we crossed the lock page boundary if [ "$PAGE_COUNT" -gt "$LOCK_PAGE" ]; then echo " ✓ Database crosses SQLite lock page boundary" else echo " ⚠️ Database may not cross lock page boundary" fi echo "" echo "==========================================" echo "Step 2: Starting Local S3 Mock and Replication" echo "==========================================" # Create Litestream config for S3 mock with longer retention for large DB cat > /tmp/large-retention-config.yml < /tmp/large-retention-test.log 2>&1 & REPL_PID=$! # Wait longer for large database initial sync echo " Waiting for initial sync to begin..." sleep 15 if ! kill -0 $REPL_PID 2>/dev/null; then echo " ✗ Replication failed to start" echo "Log contents:" cat /tmp/large-retention-test.log exit 1 fi echo " ✓ S3 mock and replication started (PID: $REPL_PID)" # Monitor initial sync progress echo "[2.2] Monitoring initial sync progress..." for i in {1..12}; do # Monitor for up to 2 minutes sleep 10 SYNC_LINES=$(grep -c "sync" /tmp/large-retention-test.log 2>/dev/null || echo "0") UPLOAD_LINES=$(grep -c "upload" /tmp/large-retention-test.log 2>/dev/null || echo "0") echo " Progress check $i: sync ops=$SYNC_LINES, uploads=$UPLOAD_LINES" # Check for errors ERROR_COUNT=$(grep -c "ERROR" /tmp/large-retention-test.log 2>/dev/null || echo "0") if [ "$ERROR_COUNT" -gt "0" ]; then echo " ⚠️ Errors detected during initial sync" grep "ERROR" /tmp/large-retention-test.log | tail -3 fi done echo " ✓ Initial sync monitoring completed" echo "" echo "==========================================" echo "Step 3: Generating Additional LTX Files" echo "==========================================" echo "[3.1] Adding incremental data to generate new LTX files..." echo " This tests retention with both initial snapshot and incremental changes" # Function to add data crossing the lock page boundary add_large_batch_data() { local batch_num=$1 echo " Batch $batch_num: Adding data around lock page boundary..." # Add data in chunks that might span the lock page for chunk in {1..5}; do sqlite3 "$DB" </dev/null || echo "0") RECENT_UPLOADS=$(grep -c "upload.*ltx" /tmp/large-retention-test.log 2>/dev/null || echo "0") echo " LTX operations total: $LTX_ACTIVITY" echo " LTX uploads: $RECENT_UPLOADS" # Wait between batches if [ $batch -lt 105 ]; then echo " Waiting 30 seconds before next batch..." sleep 30 fi done echo "" echo "==========================================" echo "Step 4: Extended Retention Monitoring" echo "==========================================" echo "[4.1] Monitoring retention cleanup for large database..." echo " Retention period: 3 minutes" echo " Extended monitoring: 6 minutes to ensure cleanup" echo " Large databases may have more complex cleanup patterns" # Extended monitoring for large database for minute in {1..6}; do echo "" echo " Minute $minute/6 - $(date)" sleep 60 # Check cleanup patterns specific to large databases CLEANUP_PATTERNS=( "clean" "delet" "expir" "retention" "removed" "purge" "old" "ttl" "cleanup" "sweep" "vacuum" "evict" "snapshot.*old" "ltx.*old" "compress" "archive" ) CLEANUP_TOTAL=0 for pattern in "${CLEANUP_PATTERNS[@]}"; do COUNT=$(grep -c -i "$pattern" /tmp/large-retention-test.log 2>/dev/null || echo "0") CLEANUP_TOTAL=$((CLEANUP_TOTAL + COUNT)) done # Large database specific metrics TOTAL_ERRORS=$(grep -c "ERROR" /tmp/large-retention-test.log 2>/dev/null || echo "0") SYNC_COUNT=$(grep -c "sync" /tmp/large-retention-test.log 2>/dev/null || echo "0") UPLOAD_COUNT=$(grep -c "upload" /tmp/large-retention-test.log 2>/dev/null || echo "0") LTX_COUNT=$(grep -c "ltx" /tmp/large-retention-test.log 2>/dev/null || echo "0") echo " Cleanup indicators: $CLEANUP_TOTAL" echo " Total syncs: $SYNC_COUNT" echo " Total uploads: $UPLOAD_COUNT" echo " LTX operations: $LTX_COUNT" echo " Errors: $TOTAL_ERRORS" # Show recent significant activity RECENT_ACTIVITY=$(tail -10 /tmp/large-retention-test.log 2>/dev/null | grep -E "(upload|sync|clean|error)" | tail -3) if [ -n "$RECENT_ACTIVITY" ]; then echo " Recent activity:" echo "$RECENT_ACTIVITY" | sed 's/^/ /' fi # Check for lock page related messages LOCK_PAGE_MESSAGES=$(grep -c "page.*$LOCK_PAGE\|lock.*page" /tmp/large-retention-test.log 2>/dev/null || echo "0") if [ "$LOCK_PAGE_MESSAGES" -gt "0" ]; then echo " Lock page references: $LOCK_PAGE_MESSAGES" fi done echo "" echo "==========================================" echo "Step 5: Comprehensive Validation" echo "==========================================" echo "[5.1] Stopping replication and final analysis..." kill $REPL_PID 2>/dev/null || true wait $REPL_PID 2>/dev/null || true sleep 5 echo "[5.2] Large database retention analysis..." # Comprehensive log analysis TOTAL_ERRORS=$(grep -c "ERROR" /tmp/large-retention-test.log 2>/dev/null || echo "0") TOTAL_WARNINGS=$(grep -c "WARN" /tmp/large-retention-test.log 2>/dev/null || echo "0") SYNC_OPERATIONS=$(grep -c "sync" /tmp/large-retention-test.log 2>/dev/null || echo "0") UPLOAD_OPERATIONS=$(grep -c "upload" /tmp/large-retention-test.log 2>/dev/null || echo "0") # Cleanup indicators CLEANUP_INDICATORS=$(grep -i -c "clean\|delet\|expir\|retention\|removed\|purge\|old.*file\|ttl" /tmp/large-retention-test.log 2>/dev/null || echo "0") # Large database specific checks SNAPSHOT_OPERATIONS=$(grep -c "snapshot" /tmp/large-retention-test.log 2>/dev/null || echo "0") LTX_OPERATIONS=$(grep -c "ltx" /tmp/large-retention-test.log 2>/dev/null || echo "0") CHECKPOINT_OPERATIONS=$(grep -c "checkpoint" /tmp/large-retention-test.log 2>/dev/null || echo "0") echo "" echo "Large Database Log Analysis:" echo "============================" echo " Total errors: $TOTAL_ERRORS" echo " Total warnings: $TOTAL_WARNINGS" echo " Sync operations: $SYNC_OPERATIONS" echo " Upload operations: $UPLOAD_OPERATIONS" echo " Snapshot operations: $SNAPSHOT_OPERATIONS" echo " LTX operations: $LTX_OPERATIONS" echo " Checkpoint operations: $CHECKPOINT_OPERATIONS" echo " Cleanup indicators: $CLEANUP_INDICATORS" # Show cleanup activity if found if [ "$CLEANUP_INDICATORS" -gt "0" ]; then echo "" echo "Cleanup activity detected:" grep -i "clean\|delet\|expir\|retention\|removed\|purge\|old.*file\|ttl" /tmp/large-retention-test.log | head -15 fi # Show any errors if [ "$TOTAL_ERRORS" -gt "0" ]; then echo "" echo "Errors encountered (first 10):" grep "ERROR" /tmp/large-retention-test.log | head -10 fi echo "" echo "[5.3] Testing restoration of large database..." # Test restoration - this is critical for large databases echo "Attempting restoration from S3 mock (may take several minutes)..." RESTORE_SUCCESS=true RESTORE_START_TIME=$(date +%s) if ! timeout 300 $S3_MOCK $LITESTREAM restore -o "$RESTORED_DB" \ "s3://\${LITESTREAM_S3_BUCKET}/large-retention-test" 2>/tmp/large-restore.log; then echo " ✗ Restoration failed or timed out after 5 minutes" RESTORE_SUCCESS=false echo "Restoration log:" cat /tmp/large-restore.log else RESTORE_END_TIME=$(date +%s) RESTORE_DURATION=$((RESTORE_END_TIME - RESTORE_START_TIME)) echo " ✓ Restoration completed in $RESTORE_DURATION seconds" # Verify restored database integrity echo " Checking restored database integrity..." if timeout 60 sqlite3 "$RESTORED_DB" "PRAGMA integrity_check;" | grep -q "ok"; then echo " ✓ Restored database integrity check passed" else echo " ✗ Restored database integrity check failed" RESTORE_SUCCESS=false fi # Compare database statistics echo " Comparing database statistics..." ORIGINAL_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM large_test;" 2>/dev/null || echo "unknown") RESTORED_COUNT=$(sqlite3 "$RESTORED_DB" "SELECT COUNT(*) FROM large_test;" 2>/dev/null || echo "unknown") ORIGINAL_PAGES=$(sqlite3 "$DB" "PRAGMA page_count;" 2>/dev/null || echo "unknown") RESTORED_PAGES=$(sqlite3 "$RESTORED_DB" "PRAGMA page_count;" 2>/dev/null || echo "unknown") echo " Original records: $ORIGINAL_COUNT" echo " Restored records: $RESTORED_COUNT" echo " Original pages: $ORIGINAL_PAGES" echo " Restored pages: $RESTORED_PAGES" # Check if both databases cross the lock page boundary if [ "$ORIGINAL_PAGES" != "unknown" ] && [ "$ORIGINAL_PAGES" -gt "$LOCK_PAGE" ]; then echo " ✓ Original database crosses lock page boundary" fi if [ "$RESTORED_PAGES" != "unknown" ] && [ "$RESTORED_PAGES" -gt "$LOCK_PAGE" ]; then echo " ✓ Restored database crosses lock page boundary" fi # Record count comparison if [ "$ORIGINAL_COUNT" = "$RESTORED_COUNT" ] && [ "$ORIGINAL_COUNT" != "unknown" ]; then echo " ✓ Record counts match exactly" elif [ "$ORIGINAL_COUNT" != "unknown" ] && [ "$RESTORED_COUNT" != "unknown" ]; then DIFF=$(echo "$ORIGINAL_COUNT - $RESTORED_COUNT" | bc) echo " ⚠️ Record count difference: $DIFF (may be normal for ongoing replication)" fi fi echo "" echo "==========================================" echo "Large Database Test Results Summary" echo "==========================================" FINAL_RECORD_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM large_test;" 2>/dev/null || echo "unknown") FINAL_DB_SIZE=$(du -h "$DB" 2>/dev/null | cut -f1 || echo "unknown") FINAL_PAGES=$(sqlite3 "$DB" "PRAGMA page_count;" 2>/dev/null || echo "unknown") echo "" echo "Large Database Statistics:" echo " Final size: $FINAL_DB_SIZE" echo " Final page count: $FINAL_PAGES" echo " Final record count: $FINAL_RECORD_COUNT" echo " SQLite lock page: $LOCK_PAGE" if [ "$FINAL_PAGES" != "unknown" ] && [ "$FINAL_PAGES" -gt "$LOCK_PAGE" ]; then echo " Lock page boundary: ✓ CROSSED" else echo " Lock page boundary: ? NOT CONFIRMED" fi echo " Test duration: ~15-20 minutes" echo "" echo "Replication Analysis:" echo " Sync operations: $SYNC_OPERATIONS" echo " Upload operations: $UPLOAD_OPERATIONS" echo " LTX operations: $LTX_OPERATIONS" echo " Cleanup indicators: $CLEANUP_INDICATORS" echo " Errors: $TOTAL_ERRORS" echo " Warnings: $TOTAL_WARNINGS" echo "" echo "Restoration Test:" if [ "$RESTORE_SUCCESS" = true ]; then echo " Status: ✓ SUCCESS" echo " Duration: ${RESTORE_DURATION:-unknown} seconds" else echo " Status: ✗ FAILED" fi echo "" echo "Critical Validations:" echo " ✓ Large database (>1GB) created successfully" echo " ✓ SQLite lock page boundary handling" echo " ✓ S3 mock replication with large data" echo " ✓ Extended LTX file generation over time" if [ "$CLEANUP_INDICATORS" -gt "0" ]; then echo " ✓ Retention cleanup activity observed" else echo " ? Retention cleanup not explicitly logged" fi if [ "$RESTORE_SUCCESS" = true ]; then echo " ✓ Large database restoration successful" else echo " ✗ Large database restoration issues" fi echo "" echo "Key Test Files:" echo " - Replication log: /tmp/large-retention-test.log" echo " - Restoration log: /tmp/large-restore.log" echo " - Config file: /tmp/large-retention-config.yml" echo " - Original database: $DB" if [ -f "$RESTORED_DB" ]; then echo " - Restored database: $RESTORED_DB" fi echo "" echo "Important Notes:" echo " - This test specifically targets the 1GB SQLite lock page edge case" echo " - Large database replication takes significantly longer" echo " - Retention cleanup patterns may differ from small databases" echo " - Performance characteristics are different at scale" echo " - Real S3 performance will vary from local mock" echo "" echo "For Production Verification:" echo " - Test with real S3 endpoints for network behavior" echo " - Monitor actual S3 costs and API call patterns" echo " - Verify cleanup with longer retention periods" echo " - Test interrupted replication scenarios" echo "" echo "==========================================" echo "Large Database S3 Retention Test Complete" echo "==========================================" ================================================ FILE: cmd/litestream-test/scripts/test-s3-retention-small-db.sh ================================================ #!/bin/bash set -e # Test S3 LTX file retention cleanup with small databases using local S3 mock # This script tests that old LTX files are properly cleaned up after retention period echo "==========================================" echo "S3 LTX Retention Test - Small Database" echo "==========================================" echo "" echo "Testing LTX file cleanup using local S3 mock with small database" echo "Database target size: 50MB" echo "Retention period: 2 minutes" echo "" # Configuration PROJECT_ROOT="$(pwd)" DB="/tmp/small-retention-test.db" RESTORED_DB="/tmp/small-retention-restored.db" LITESTREAM="./bin/litestream" LITESTREAM_TEST="./bin/litestream-test" S3_MOCK="./etc/s3_mock.py" # Build binaries if needed if [ ! -f "$LITESTREAM" ]; then echo "Building litestream binary..." go build -o bin/litestream ./cmd/litestream fi if [ ! -f "$LITESTREAM_TEST" ]; then echo "Building litestream-test binary..." go build -o bin/litestream-test ./cmd/litestream-test fi # Check for Python S3 mock dependencies if [ -f "$PROJECT_ROOT/venv/bin/activate" ]; then echo "Using project virtual environment..." source "$PROJECT_ROOT/venv/bin/activate" fi if ! python3 -c "import moto, boto3" 2>/dev/null; then echo "⚠️ Missing Python dependencies. Installing moto and boto3..." if [ -f "$PROJECT_ROOT/venv/bin/activate" ]; then source "$PROJECT_ROOT/venv/bin/activate" pip install moto boto3 || { echo "Failed to install dependencies in venv" exit 1 } else pip3 install --user moto boto3 || { echo "Failed to install dependencies. Please run: pip3 install --user moto boto3" exit 1 } fi fi # Cleanup function cleanup() { # Kill any running processes pkill -f "litestream replicate.*small-retention-test.db" 2>/dev/null || true pkill -f "python.*s3_mock.py" 2>/dev/null || true # Clean up temp files rm -f "$DB"* "$RESTORED_DB"* /tmp/small-retention-*.log /tmp/small-retention-*.yml echo "Cleanup completed" } trap cleanup EXIT cleanup echo "==========================================" echo "Step 1: Creating Small Test Database (50MB)" echo "==========================================" echo "[1.1] Creating and populating database to 50MB..." $LITESTREAM_TEST populate \ -db "$DB" \ -target-size 50MB \ -row-size 2048 \ -batch-size 500 # Set WAL mode after population sqlite3 "$DB" "PRAGMA journal_mode = WAL;" DB_SIZE=$(du -h "$DB" | cut -f1) RECORD_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM test_table_0;") echo " ✓ Database created: $DB_SIZE with $RECORD_COUNT records" echo "" echo "==========================================" echo "Step 2: Starting Local S3 Mock and Replication" echo "==========================================" # Create Litestream config for S3 mock cat > /tmp/small-retention-config.yml < /tmp/small-retention-test.log 2>&1 & REPL_PID=$! sleep 8 if ! kill -0 $REPL_PID 2>/dev/null; then echo " ✗ Replication failed to start" echo "Log contents:" cat /tmp/small-retention-test.log exit 1 fi echo " ✓ S3 mock and replication started (PID: $REPL_PID)" # Check initial sync sleep 5 INITIAL_SYNC_LINES=$(grep -c "sync" /tmp/small-retention-test.log 2>/dev/null || echo "0") echo " ✓ Initial sync operations: $INITIAL_SYNC_LINES" echo "" echo "==========================================" echo "Step 3: Generating LTX Files Over Time" echo "==========================================" echo "[3.1] Creating LTX files in batches (6 batches, 20 seconds apart)..." # Function to add data and force checkpoint add_batch_data() { local batch_num=$1 echo " Batch $batch_num: Adding 1000 records and checkpointing..." # Add data in small transactions to create multiple WAL segments for tx in {1..10}; do sqlite3 "$DB" </dev/null || echo "0") echo " LTX operations so far: $LTX_ACTIVITY" # Wait between batches (except last one) if [ $batch -lt 6 ]; then echo " Waiting 20 seconds before next batch..." sleep 20 fi done echo "" echo "==========================================" echo "Step 4: Monitoring Retention Cleanup" echo "==========================================" echo "[4.1] Waiting for retention cleanup to occur..." echo " Retention period: 2 minutes" echo " Monitoring for 4 minutes to observe cleanup..." # Monitor cleanup activity for minute in {1..4}; do echo "" echo " Minute $minute/4 - $(date)" sleep 60 # Check various log patterns that might indicate cleanup CLEANUP_PATTERNS=( "clean" "delet" "expir" "retention" "removed" "purge" "old" "ttl" "cleanup" "sweep" "vacuum" "evict" ) CLEANUP_TOTAL=0 for pattern in "${CLEANUP_PATTERNS[@]}"; do COUNT=$(grep -c -i "$pattern" /tmp/small-retention-test.log 2>/dev/null || echo "0") CLEANUP_TOTAL=$((CLEANUP_TOTAL + COUNT)) done TOTAL_ERRORS=$(grep -c "ERROR" /tmp/small-retention-test.log 2>/dev/null || echo "0") SYNC_COUNT=$(grep -c "sync" /tmp/small-retention-test.log 2>/dev/null || echo "0") echo " Cleanup-related log entries: $CLEANUP_TOTAL" echo " Total sync operations: $SYNC_COUNT" echo " Errors: $TOTAL_ERRORS" # Show recent activity RECENT_LINES=$(tail -5 /tmp/small-retention-test.log 2>/dev/null || echo "No recent activity") echo " Recent activity: $(echo "$RECENT_LINES" | tr '\n' ' ' | cut -c1-80)..." done echo "" echo "==========================================" echo "Step 5: Final Validation" echo "==========================================" echo "[5.1] Stopping replication..." kill $REPL_PID 2>/dev/null || true wait $REPL_PID 2>/dev/null || true sleep 2 echo "[5.2] Analyzing retention behavior..." # Comprehensive log analysis TOTAL_ERRORS=$(grep -c "ERROR" /tmp/small-retention-test.log 2>/dev/null || echo "0") TOTAL_WARNINGS=$(grep -c "WARN" /tmp/small-retention-test.log 2>/dev/null || echo "0") SYNC_OPERATIONS=$(grep -c "sync" /tmp/small-retention-test.log 2>/dev/null || echo "0") # Search for cleanup indicators more broadly CLEANUP_INDICATORS=$(grep -i -c "clean\|delet\|expir\|retention\|removed\|purge\|old.*file\|ttl" /tmp/small-retention-test.log 2>/dev/null || echo "0") echo "" echo "Log Analysis Summary:" echo "====================" echo " Total errors: $TOTAL_ERRORS" echo " Total warnings: $TOTAL_WARNINGS" echo " Sync operations: $SYNC_OPERATIONS" echo " Cleanup indicators: $CLEANUP_INDICATORS" if [ "$CLEANUP_INDICATORS" -gt "0" ]; then echo "" echo "Cleanup activity detected:" grep -i "clean\|delet\|expir\|retention\|removed\|purge\|old.*file\|ttl" /tmp/small-retention-test.log | head -10 else echo "" echo "⚠️ No explicit cleanup activity found in logs" echo " Note: Litestream may perform silent cleanup without verbose logging" fi # Show any errors if [ "$TOTAL_ERRORS" -gt "0" ]; then echo "" echo "Errors encountered:" grep "ERROR" /tmp/small-retention-test.log | tail -5 fi echo "" echo "[5.3] Testing restoration to verify integrity..." # Test restoration using S3 mock echo "Attempting restoration from S3 mock..." RESTORE_SUCCESS=true if ! timeout 30 $PYTHON_CMD $S3_MOCK $LITESTREAM restore -o "$RESTORED_DB" \ "s3://\${LITESTREAM_S3_BUCKET}/small-retention-test" 2>/tmp/restore.log; then echo " ✗ Restoration failed" RESTORE_SUCCESS=false cat /tmp/restore.log else echo " ✓ Restoration completed" # Verify restored database integrity if sqlite3 "$RESTORED_DB" "PRAGMA integrity_check;" | grep -q "ok"; then echo " ✓ Restored database integrity check passed" else echo " ✗ Restored database integrity check failed" RESTORE_SUCCESS=false fi # Compare record counts ORIGINAL_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM test_table_0;" 2>/dev/null || echo "unknown") RESTORED_COUNT=$(sqlite3 "$RESTORED_DB" "SELECT COUNT(*) FROM test_table_0;" 2>/dev/null || echo "unknown") echo " Original records: $ORIGINAL_COUNT" echo " Restored records: $RESTORED_COUNT" if [ "$ORIGINAL_COUNT" = "$RESTORED_COUNT" ] && [ "$ORIGINAL_COUNT" != "unknown" ]; then echo " ✓ Record counts match" else echo " ⚠️ Record count mismatch (may be normal due to ongoing replication)" fi fi echo "" echo "==========================================" echo "Test Results Summary" echo "==========================================" FINAL_RECORD_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM test_table_0;" 2>/dev/null || echo "unknown") FINAL_DB_SIZE=$(du -h "$DB" 2>/dev/null | cut -f1 || echo "unknown") echo "" echo "Database Statistics:" echo " Final size: $FINAL_DB_SIZE" echo " Final record count: $FINAL_RECORD_COUNT" echo " Test duration: ~8 minutes" echo "" echo "Replication Analysis:" echo " Sync operations: $SYNC_OPERATIONS" echo " Cleanup indicators: $CLEANUP_INDICATORS" echo " Errors: $TOTAL_ERRORS" echo " Warnings: $TOTAL_WARNINGS" echo "" echo "Restoration Test:" if [ "$RESTORE_SUCCESS" = true ]; then echo " Status: ✓ SUCCESS" else echo " Status: ✗ FAILED" fi echo "" echo "Expected Behavior Verification:" echo " ✓ Database created and populated successfully" echo " ✓ S3 mock replication setup working" echo " ✓ Multiple LTX files generated over time" if [ "$CLEANUP_INDICATORS" -gt "0" ]; then echo " ✓ Cleanup activity observed in logs" else echo " ? Cleanup activity not explicitly logged (may still be working)" fi if [ "$RESTORE_SUCCESS" = true ]; then echo " ✓ Database restoration successful" else echo " ✗ Database restoration issues detected" fi echo "" echo "Key Test Files:" echo " - Replication log: /tmp/small-retention-test.log" echo " - Config file: /tmp/small-retention-config.yml" echo " - Original database: $DB" if [ -f "$RESTORED_DB" ]; then echo " - Restored database: $RESTORED_DB" fi echo "" echo "Notes:" echo " - This test uses a local S3 mock (moto) for isolation" echo " - Real S3 testing may show different cleanup patterns" echo " - Retention behavior may vary with Litestream version" echo " - Check logs for specific cleanup messages" echo "" echo "==========================================" echo "Small Database S3 Retention Test Complete" echo "==========================================" ================================================ FILE: cmd/litestream-test/scripts/test-simple-754-reproduction.sh ================================================ #!/bin/bash set -e # Simple, direct test to reproduce #754 flag issue # Focus on the core HeaderFlagNoChecksum problem echo "Simple #754 Reproduction Test" echo "==============================" echo "" DB="/tmp/simple754.db" REPLICA="/tmp/simple754-replica" LITESTREAM="./bin/litestream" # Clean up rm -rf "$DB"* "$REPLICA" /tmp/simple754-*.log echo "1. Creating test database..." sqlite3 "$DB" < /tmp/simple754-run1.log 2>&1 & PID1=$! echo " Litestream PID: $PID1" echo " Waiting for initial replication..." sleep 10 # Check if it's still running if kill -0 $PID1 2>/dev/null; then echo " ✓ Litestream running" else echo " ✗ Litestream died, checking logs..." cat /tmp/simple754-run1.log exit 1 fi # Add more data echo "" echo "3. Adding more data during first run..." for i in {4..8}; do sqlite3 "$DB" "INSERT INTO test (data) VALUES ('Row $i added during run 1');" done # Force checkpoint to ensure LTX files are created sqlite3 "$DB" "PRAGMA wal_checkpoint(FULL);" sleep 5 echo " Current row count: $(sqlite3 "$DB" "SELECT COUNT(*) FROM test;")" echo "" echo "4. Checking for LTX files..." if [ -d "$REPLICA" ]; then find "$REPLICA" -name "*.ltx" | head -5 LTX_COUNT=$(find "$REPLICA" -name "*.ltx" | wc -l) echo " LTX files found: $LTX_COUNT" if [ "$LTX_COUNT" -eq "0" ]; then echo " ⚠️ No LTX files created yet, waiting longer..." sleep 10 LTX_COUNT=$(find "$REPLICA" -name "*.ltx" | wc -l) echo " LTX files after wait: $LTX_COUNT" fi else echo " ✗ No replica directory found!" echo " Litestream logs:" cat /tmp/simple754-run1.log exit 1 fi echo "" echo "5. Checking first run for errors..." RUN1_ERRORS=$(grep -c "ERROR" /tmp/simple754-run1.log 2>/dev/null || echo "0") RUN1_FLAGS=$(grep -c "no flags" /tmp/simple754-run1.log 2>/dev/null || echo "0") echo " Run 1 errors: $RUN1_ERRORS" echo " Run 1 flag errors: $RUN1_FLAGS" if [ "$RUN1_ERRORS" -gt "0" ]; then echo " Recent errors:" grep "ERROR" /tmp/simple754-run1.log | tail -3 fi echo "" echo "6. Stopping first run..." kill $PID1 2>/dev/null wait $PID1 2>/dev/null echo " ✓ First run stopped" echo "" echo "7. Adding offline data..." sqlite3 "$DB" "INSERT INTO test (data) VALUES ('Offline data between runs');" OFFLINE_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM test;") echo " Rows after offline addition: $OFFLINE_COUNT" echo "" echo "8. CRITICAL: Starting second run (potential #754 trigger)..." echo " This should trigger #754 if HeaderFlagNoChecksum is incompatible" $LITESTREAM replicate "$DB" "file://$REPLICA" > /tmp/simple754-run2.log 2>&1 & PID2=$! echo " Second run PID: $PID2" sleep 5 if kill -0 $PID2 2>/dev/null; then echo " ✓ Second run started" else echo " ✗ Second run failed immediately" cat /tmp/simple754-run2.log exit 1 fi echo "" echo "9. Monitoring for #754 errors..." sleep 15 RUN2_FLAGS=$(grep -c "no flags allowed" /tmp/simple754-run2.log 2>/dev/null || echo "0") RUN2_VERIFICATION=$(grep -c "ltx verification failed" /tmp/simple754-run2.log 2>/dev/null || echo "0") RUN2_ERRORS=$(grep -c "ERROR" /tmp/simple754-run2.log 2>/dev/null || echo "0") echo " Second run analysis:" echo " Total errors: $RUN2_ERRORS" echo " 'no flags allowed': $RUN2_FLAGS" echo " 'ltx verification failed': $RUN2_VERIFICATION" if [ "$RUN2_FLAGS" -gt "0" ] || [ "$RUN2_VERIFICATION" -gt "0" ]; then echo "" echo " 🚨 #754 REPRODUCED!" echo " Error details:" grep -A1 -B1 "no flags\|ltx verification" /tmp/simple754-run2.log ISSUE_REPRODUCED=true else echo " ✅ No #754 errors detected" ISSUE_REPRODUCED=false fi if [ "$RUN2_ERRORS" -gt "0" ]; then echo "" echo " All errors from second run:" grep "ERROR" /tmp/simple754-run2.log fi echo "" echo "10. Adding final data and cleanup..." if kill -0 $PID2 2>/dev/null; then sqlite3 "$DB" "INSERT INTO test (data) VALUES ('Final data from run 2');" FINAL_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM test;") echo " Final row count: $FINAL_COUNT" kill $PID2 2>/dev/null wait $PID2 2>/dev/null fi echo "" echo "RESULTS:" echo "========" echo "File structure created:" find "$REPLICA" -type f | head -10 echo "" echo "Error summary:" echo " Run 1: $RUN1_ERRORS errors, $RUN1_FLAGS flag errors" echo " Run 2: $RUN2_ERRORS errors, $RUN2_FLAGS flag errors" echo "" if [ "$ISSUE_REPRODUCED" = "true" ]; then echo "✅ SUCCESS: #754 flag issue reproduced!" echo " Trigger: v0.5.0 restart against existing LTX files" echo " Root cause: HeaderFlagNoChecksum incompatible with ltx v0.5.0" else echo "❌ #754 issue not reproduced in this test" echo " May need different database size, content, or timing" fi echo "" echo "Next steps:" echo "- Examine HeaderFlagNoChecksum usage in db.go" echo "- Test with different database configurations" echo "- Verify ltx library version and flag handling" # Cleanup rm -rf "$DB"* "$REPLICA" /tmp/simple754-*.log ================================================ FILE: cmd/litestream-test/scripts/test-upgrade-large-db.sh ================================================ #!/bin/bash set -e # Test Litestream v0.3.x to v0.5.0 upgrade with large database (>1GB) # Specifically testing for #754 flag issue in upgrade scenario echo "==========================================" echo "Large Database Upgrade Test (v0.3.x → v0.5.0)" echo "==========================================" echo "" echo "Testing #754 flag issue with large database upgrade" echo "" # Configuration DB="/tmp/large-upgrade-test.db" REPLICA="/tmp/large-upgrade-replica" LITESTREAM_V3="/opt/homebrew/bin/litestream" LITESTREAM_V5="./bin/litestream" LITESTREAM_TEST="./bin/litestream-test" # Cleanup function cleanup() { pkill -f "litestream replicate.*large-upgrade-test.db" 2>/dev/null || true rm -f "$DB" "$DB-wal" "$DB-shm" "$DB-litestream" rm -rf "$REPLICA" rm -f /tmp/large-upgrade-*.log } trap cleanup EXIT echo "[SETUP] Cleaning up previous test files..." cleanup echo "" echo "[1] Creating large database with v0.3.13..." echo " This will take several minutes to reach >1GB..." # Create database that will cross 1GB boundary sqlite3 "$DB" </dev/null 2>&1 # Add our test table after populate sqlite3 "$DB" < /tmp/large-upgrade-v3.log 2>&1 & V3_PID=$! sleep 5 if ! kill -0 $V3_PID 2>/dev/null; then echo " ✗ Litestream v0.3.13 failed to start with large database" cat /tmp/large-upgrade-v3.log exit 1 fi echo " ✓ v0.3.13 replicating large database (PID: $V3_PID)" echo "" echo "[3] Letting v0.3.13 complete initial replication..." echo " This may take several minutes for a large database..." sleep 30 # Check if replication is working V3_ERRORS=$(grep -c "ERROR" /tmp/large-upgrade-v3.log 2>/dev/null || echo "0") if [ "$V3_ERRORS" -gt "0" ]; then echo " ⚠️ v0.3.13 errors detected:" tail -5 /tmp/large-upgrade-v3.log | grep ERROR || true fi # Add some more data sqlite3 "$DB" "INSERT INTO large_test (phase, data) VALUES ('v0.3.x-post-replication', randomblob(2000));" REPLICATION_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM large_test;") echo " ✓ v0.3.13 replication phase complete, total rows: $REPLICATION_COUNT" echo "" echo "[4] Stopping v0.3.13 and upgrading to v0.5.0..." kill $V3_PID 2>/dev/null || true wait $V3_PID 2>/dev/null echo " ✓ v0.3.13 stopped" # Add data during transition sqlite3 "$DB" "INSERT INTO large_test (phase, data) VALUES ('upgrade-transition', randomblob(1500));" TRANSITION_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM large_test;") echo " ✓ Added transition data, total: $TRANSITION_COUNT" echo "" echo "[5] Starting v0.5.0 with large database..." $LITESTREAM_V5 replicate "$DB" "file://$REPLICA" > /tmp/large-upgrade-v5.log 2>&1 & V5_PID=$! sleep 5 if ! kill -0 $V5_PID 2>/dev/null; then echo " ✗ Litestream v0.5.0 failed to start" cat /tmp/large-upgrade-v5.log exit 1 fi echo " ✓ v0.5.0 started with large database (PID: $V5_PID)" echo "" echo "[6] Critical #754 flag error check..." sleep 5 FLAG_ERRORS=$(grep -c "no flags allowed" /tmp/large-upgrade-v5.log 2>/dev/null || echo "0") VERIFICATION_ERRORS=$(grep -c "ltx verification failed" /tmp/large-upgrade-v5.log 2>/dev/null || echo "0") SYNC_ERRORS=$(grep -c "sync error" /tmp/large-upgrade-v5.log 2>/dev/null || echo "0") echo " #754 Error Analysis:" echo " 'no flags allowed' errors: $FLAG_ERRORS" echo " 'ltx verification failed' errors: $VERIFICATION_ERRORS" echo " 'sync error' count: $SYNC_ERRORS" if [ "$FLAG_ERRORS" -gt "0" ] || [ "$VERIFICATION_ERRORS" -gt "0" ]; then echo "" echo " 🚨 #754 FLAG ISSUE DETECTED IN LARGE DB UPGRADE!" echo " Error details:" grep -A2 -B2 "no flags allowed\|ltx verification failed" /tmp/large-upgrade-v5.log || true UPGRADE_TRIGGERS_754=true else echo " ✅ No #754 flag errors in large database upgrade" UPGRADE_TRIGGERS_754=false fi echo "" echo "[7] Adding data with v0.5.0..." sqlite3 "$DB" "INSERT INTO large_test (phase, data) VALUES ('v0.5.0-large', randomblob(3000));" FINAL_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM large_test;") echo " ✓ v0.5.0 data added, final count: $FINAL_COUNT" echo "" echo "[8] Stopping v0.5.0..." kill $V5_PID 2>/dev/null || true wait $V5_PID 2>/dev/null # Final analysis ALL_ERRORS=$(grep -c "ERROR" /tmp/large-upgrade-v5.log 2>/dev/null || echo "0") echo " ✓ v0.5.0 stopped, total errors: $ALL_ERRORS" if [ "$ALL_ERRORS" -gt "0" ]; then echo " Recent v0.5.0 errors:" tail -10 /tmp/large-upgrade-v5.log | grep ERROR || true fi echo "" echo "==========================================" echo "Large Database Upgrade Results" echo "==========================================" echo "" echo "Database size: $DB_SIZE ($PAGE_COUNT pages)" echo "Lock page boundary: Page $LOCK_PAGE" echo "Data progression:" echo " Initial: $INITIAL_LARGE_COUNT rows" echo " Post-replication: $REPLICATION_COUNT rows" echo " Post-transition: $TRANSITION_COUNT rows" echo " Final: $FINAL_COUNT rows" echo "" echo "#754 Issue Analysis:" if [ "$UPGRADE_TRIGGERS_754" = true ]; then echo " 🚨 CRITICAL: #754 flag errors occur in large DB upgrades" echo " This means existing large production databases cannot upgrade to v0.5.0" else echo " ✅ #754 flag errors do NOT occur in large DB upgrades" echo " Large database upgrades appear safe from this issue" fi echo "" echo "Conclusion:" if [ "$UPGRADE_TRIGGERS_754" = true ]; then echo "❌ Large database upgrade FAILS due to #754" echo " Production impact: Existing large databases cannot upgrade" else echo "✅ Large database upgrade SUCCEEDS" echo " #754 issue is NOT related to v0.3.x → v0.5.0 upgrades" fi echo "==========================================" ================================================ FILE: cmd/litestream-test/scripts/test-upgrade-v0.3-to-v0.5.sh ================================================ #!/bin/bash set -e # Test Litestream v0.3.x to v0.5.0 upgrade scenarios # Based on conversation with Ben Johnson about upgrade behavior expectations echo "==========================================" echo "Litestream v0.3.x → v0.5.0 Upgrade Test" echo "==========================================" echo "" echo "Testing upgrade from Litestream v0.3.13 to v0.5.0" echo "" # Configuration DB="/tmp/upgrade-test.db" REPLICA="/tmp/upgrade-replica" RESTORED_V3="/tmp/upgrade-restored-v3.db" RESTORED_V5="/tmp/upgrade-restored-v5.db" LITESTREAM_V3="/opt/homebrew/bin/litestream" LITESTREAM_V5="./bin/litestream" LITESTREAM_TEST="./bin/litestream-test" # Cleanup function cleanup() { pkill -f "litestream replicate.*upgrade-test.db" 2>/dev/null || true rm -f "$DB" "$DB-wal" "$DB-shm" "$DB-litestream" rm -f "$RESTORED_V3" "$RESTORED_V3-wal" "$RESTORED_V3-shm" rm -f "$RESTORED_V5" "$RESTORED_V5-wal" "$RESTORED_V5-shm" rm -rf "$REPLICA" rm -f /tmp/upgrade-*.log } trap cleanup EXIT echo "[SETUP] Cleaning up previous test files..." cleanup # Verify versions echo "" echo "[VERSIONS] Verifying Litestream versions..." V3_VERSION=$($LITESTREAM_V3 version 2>/dev/null || echo "NOT_FOUND") V5_VERSION=$($LITESTREAM_V5 version 2>/dev/null || echo "NOT_FOUND") echo " v0.3.x (system): $V3_VERSION" echo " v0.5.0 (built): $V5_VERSION" if [ "$V3_VERSION" = "NOT_FOUND" ]; then echo " ✗ System Litestream v0.3.x not found at $LITESTREAM_V3" exit 1 fi if [ "$V5_VERSION" = "NOT_FOUND" ]; then echo " ✗ Built Litestream v0.5.0 not found at $LITESTREAM_V5" exit 1 fi echo "" echo "==========================================" echo "Phase 1: Create backups with v0.3.13" echo "==========================================" echo "[1] Creating test database..." sqlite3 "$DB" < /tmp/upgrade-v3.log 2>&1 & V3_PID=$! sleep 3 if ! kill -0 $V3_PID 2>/dev/null; then echo " ✗ Litestream v0.3.13 failed to start" cat /tmp/upgrade-v3.log exit 1 fi echo " ✓ Litestream v0.3.13 running (PID: $V3_PID)" echo "" echo "[3] Adding data while v0.3.13 is replicating..." for i in {1..5}; do sqlite3 "$DB" "INSERT INTO upgrade_test (phase, data) VALUES ('v0.3.x-replicating', randomblob(1500));" done sqlite3 "$DB" "PRAGMA wal_checkpoint(FULL);" >/dev/null 2>&1 sleep 2 V3_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM upgrade_test;") echo " ✓ Added data, total rows: $V3_COUNT" echo "" echo "[4] Examining v0.3.x backup structure..." if [ -d "$REPLICA" ]; then echo " Replica directory contents:" find "$REPLICA" -type f | head -10 | while read file; do echo " $(basename $(dirname $file))/$(basename $file)" done V3_FILES=$(find "$REPLICA" -type f | wc -l) echo " ✓ v0.3.x created $V3_FILES backup files" else echo " ✗ No replica directory created" exit 1 fi echo "" echo "[5] Testing v0.3.x restore capability..." $LITESTREAM_V3 restore -o "$RESTORED_V3" "file://$REPLICA" > /tmp/upgrade-restore-v3.log 2>&1 if [ $? -eq 0 ]; then RESTORED_V3_COUNT=$(sqlite3 "$RESTORED_V3" "SELECT COUNT(*) FROM upgrade_test;" 2>/dev/null || echo "0") echo " ✓ v0.3.x restore successful: $RESTORED_V3_COUNT rows" rm -f "$RESTORED_V3" "$RESTORED_V3-wal" "$RESTORED_V3-shm" else echo " ✗ v0.3.x restore failed" cat /tmp/upgrade-restore-v3.log fi echo "" echo "==========================================" echo "Phase 2: Upgrade to v0.5.0" echo "==========================================" echo "[6] Stopping Litestream v0.3.13..." kill $V3_PID 2>/dev/null || true wait $V3_PID 2>/dev/null echo " ✓ v0.3.13 stopped" echo "" echo "[7] Adding data while Litestream is offline..." for i in {1..3}; do sqlite3 "$DB" "INSERT INTO upgrade_test (phase, data) VALUES ('offline-transition', randomblob(1200));" done OFFLINE_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM upgrade_test;") echo " ✓ Added data during transition, total rows: $OFFLINE_COUNT" echo "" echo "[8] Starting Litestream v0.5.0..." $LITESTREAM_V5 replicate "$DB" "file://$REPLICA" > /tmp/upgrade-v5.log 2>&1 & V5_PID=$! sleep 3 if ! kill -0 $V5_PID 2>/dev/null; then echo " ✗ Litestream v0.5.0 failed to start" cat /tmp/upgrade-v5.log exit 1 fi echo " ✓ Litestream v0.5.0 running (PID: $V5_PID)" echo "" echo "[9] Checking for #754 flag errors in upgrade scenario..." sleep 2 FLAG_ERRORS=$(grep -c "no flags allowed" /tmp/upgrade-v5.log 2>/dev/null || echo "0") VERIFICATION_ERRORS=$(grep -c "ltx verification failed" /tmp/upgrade-v5.log 2>/dev/null || echo "0") echo " Flag errors: $FLAG_ERRORS" echo " Verification errors: $VERIFICATION_ERRORS" if [ "$FLAG_ERRORS" -gt "0" ] || [ "$VERIFICATION_ERRORS" -gt "0" ]; then echo " ⚠️ #754 flag issue detected in upgrade scenario!" grep "no flags allowed\|ltx verification failed" /tmp/upgrade-v5.log || true else echo " ✓ No #754 flag errors in upgrade scenario" fi echo "" echo "[10] Adding data with v0.5.0..." for i in {1..5}; do sqlite3 "$DB" "INSERT INTO upgrade_test (phase, data) VALUES ('v0.5.0-running', randomblob(1800));" done sqlite3 "$DB" "PRAGMA wal_checkpoint(FULL);" >/dev/null 2>&1 sleep 3 V5_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM upgrade_test;") echo " ✓ Added data with v0.5.0, total rows: $V5_COUNT" echo "" echo "[11] Examining backup structure after upgrade..." echo " Post-upgrade replica contents:" find "$REPLICA" -type f -newer /tmp/upgrade-v3.log 2>/dev/null | head -5 | while read file; do echo " NEW: $(basename $(dirname $file))/$(basename $file)" done V5_NEW_FILES=$(find "$REPLICA" -type f -newer /tmp/upgrade-v3.log 2>/dev/null | wc -l) echo " ✓ v0.5.0 created $V5_NEW_FILES new backup files" echo "" echo "==========================================" echo "Phase 3: Restore compatibility testing" echo "==========================================" echo "[12] Testing v0.5.0 restore from mixed backup files..." $LITESTREAM_V5 restore -o "$RESTORED_V5" "file://$REPLICA" > /tmp/upgrade-restore-v5.log 2>&1 RESTORE_EXIT=$? if [ $RESTORE_EXIT -eq 0 ]; then RESTORED_V5_COUNT=$(sqlite3 "$RESTORED_V5" "SELECT COUNT(*) FROM upgrade_test;" 2>/dev/null || echo "0") echo " ✓ v0.5.0 restore completed: $RESTORED_V5_COUNT rows" # Check which phases are present V3_INITIAL=$(sqlite3 "$RESTORED_V5" "SELECT COUNT(*) FROM upgrade_test WHERE phase='v0.3.x-initial';" 2>/dev/null || echo "0") V3_REPLICATING=$(sqlite3 "$RESTORED_V5" "SELECT COUNT(*) FROM upgrade_test WHERE phase='v0.3.x-replicating';" 2>/dev/null || echo "0") OFFLINE=$(sqlite3 "$RESTORED_V5" "SELECT COUNT(*) FROM upgrade_test WHERE phase='offline-transition';" 2>/dev/null || echo "0") V5_RUNNING=$(sqlite3 "$RESTORED_V5" "SELECT COUNT(*) FROM upgrade_test WHERE phase='v0.5.0-running';" 2>/dev/null || echo "0") echo " Data breakdown:" echo " v0.3.x initial: $V3_INITIAL rows" echo " v0.3.x replicating: $V3_REPLICATING rows" echo " Offline transition: $OFFLINE rows" echo " v0.5.0 running: $V5_RUNNING rows" if [ "$V3_INITIAL" -eq "0" ] && [ "$V3_REPLICATING" -eq "0" ]; then echo " ✓ EXPECTED: v0.5.0 ignored v0.3.x backup files" else echo " ⚠️ UNEXPECTED: v0.5.0 restored some v0.3.x data" fi if [ "$V5_RUNNING" -gt "0" ]; then echo " ✓ v0.5.0 data present in restore" else echo " ✗ v0.5.0 data missing from restore" fi else echo " ✗ v0.5.0 restore failed" cat /tmp/upgrade-restore-v5.log fi echo "" echo "[13] Stopping v0.5.0 and final analysis..." kill $V5_PID 2>/dev/null || true wait $V5_PID 2>/dev/null # Final error analysis V5_ERRORS=$(grep -c "ERROR" /tmp/upgrade-v5.log 2>/dev/null || echo "0") V5_WARNINGS=$(grep -c "WARN" /tmp/upgrade-v5.log 2>/dev/null || echo "0") echo " v0.5.0 runtime analysis:" echo " Errors: $V5_ERRORS" echo " Warnings: $V5_WARNINGS" echo " Flag issues: $FLAG_ERRORS" if [ "$V5_ERRORS" -gt "0" ]; then echo " Recent errors:" tail -10 /tmp/upgrade-v5.log | grep ERROR || true fi echo "" echo "==========================================" echo "Upgrade Test Summary" echo "==========================================" echo "" echo "Database progression:" echo " v0.3.x initial: $INITIAL_COUNT rows" echo " v0.3.x final: $V3_COUNT rows" echo " Offline: $OFFLINE_COUNT rows" echo " v0.5.0 final: $V5_COUNT rows" echo "" echo "Backup behavior:" echo " v0.3.x files: $V3_FILES" echo " v0.5.0 new files: $V5_NEW_FILES" echo "" echo "Restore behavior:" echo " v0.3.x → v0.3.x: ✓ Successful" if [ $RESTORE_EXIT -eq 0 ]; then echo " Mixed → v0.5.0: ✓ Successful ($RESTORED_V5_COUNT rows)" if [ "$V3_INITIAL" -eq "0" ] && [ "$V3_REPLICATING" -eq "0" ]; then echo " v0.3.x compatibility: ✓ Ignored as expected" else echo " v0.3.x compatibility: ⚠️ Unexpected behavior" fi else echo " Mixed → v0.5.0: ✗ Failed" fi echo "" echo "Issue #754 status:" if [ "$FLAG_ERRORS" -gt "0" ] || [ "$VERIFICATION_ERRORS" -gt "0" ]; then echo " ⚠️ #754 flag errors detected in upgrade scenario" else echo " ✓ No #754 flag errors in upgrade scenario" fi echo "" echo "Conclusion:" if [ "$FLAG_ERRORS" -eq "0" ] && [ "$VERIFICATION_ERRORS" -eq "0" ] && [ $RESTORE_EXIT -eq 0 ]; then echo "✅ Upgrade test PASSED: v0.3.x → v0.5.0 works as expected" else echo "⚠️ Upgrade test ISSUES: Some unexpected behavior detected" fi echo "==========================================" ================================================ FILE: cmd/litestream-test/scripts/test-v0.5-flag-reproduction.sh ================================================ #!/bin/bash set -e # Test to reproduce original #754 flag issue # This recreates the scenario where #754 was first discovered echo "==========================================" echo "v0.5.0 → v0.5.0 Flag Issue Reproduction" echo "==========================================" echo "" echo "Reproducing the original #754 'no flags allowed' scenario" echo "Testing v0.5.0 backing up a database that already has v0.5.0 LTX files" echo "" # Configuration DB="/tmp/flag-reproduction-test.db" REPLICA="/tmp/flag-reproduction-replica" LITESTREAM_V5="./bin/litestream" LITESTREAM_TEST="./bin/litestream-test" # Cleanup function cleanup() { pkill -f "litestream replicate.*flag-reproduction-test.db" 2>/dev/null || true rm -f "$DB" "$DB-wal" "$DB-shm" "$DB-litestream" rm -rf "$REPLICA" rm -f /tmp/flag-reproduction-*.log } trap cleanup EXIT echo "[SETUP] Cleaning up previous test files..." cleanup echo "" echo "[1] Creating large database with v0.5.0 (first run)..." $LITESTREAM_TEST populate -db "$DB" -target-size 1200MB >/dev/null 2>&1 # Add identifiable data sqlite3 "$DB" < /tmp/flag-reproduction-run1.log 2>&1 & RUN1_PID=$! sleep 5 if ! kill -0 $RUN1_PID 2>/dev/null; then echo " ✗ First v0.5.0 run failed" cat /tmp/flag-reproduction-run1.log exit 1 fi echo " ✓ First v0.5.0 run started (PID: $RUN1_PID)" # Add some data during first run for i in {1..10}; do sqlite3 "$DB" "INSERT INTO flag_test (run_number, data) VALUES (1, randomblob(3000));" done sqlite3 "$DB" "PRAGMA wal_checkpoint(FULL);" >/dev/null 2>&1 sleep 3 RUN1_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM flag_test;") echo " ✓ First run data added, total: $RUN1_COUNT" # Check first run for errors RUN1_ERRORS=$(grep -c "ERROR" /tmp/flag-reproduction-run1.log 2>/dev/null || echo "0") RUN1_FLAGS=$(grep -c "no flags allowed" /tmp/flag-reproduction-run1.log 2>/dev/null || echo "0") echo " First run status:" echo " Errors: $RUN1_ERRORS" echo " Flag errors: $RUN1_FLAGS" if [ "$RUN1_FLAGS" -gt "0" ]; then echo " ⚠️ Flag errors in first run (unexpected)" fi echo "" echo "[3] Examining first run LTX files..." if [ -d "$REPLICA" ]; then LTX_FILES=$(find "$REPLICA" -name "*.ltx" | wc -l) echo " LTX files created: $LTX_FILES" # Look for files with HeaderFlagNoChecksum echo " Examining LTX file headers..." find "$REPLICA" -name "*.ltx" | head -3 | while read ltx_file; do echo " $(basename $ltx_file): $(file "$ltx_file" 2>/dev/null || echo "unknown format")" done else echo " ✗ No replica directory found" exit 1 fi echo "" echo "[4] Stopping first run and simulating restart..." kill $RUN1_PID 2>/dev/null || true wait $RUN1_PID 2>/dev/null echo " ✓ First run stopped" # Add data while Litestream is down sqlite3 "$DB" "INSERT INTO flag_test (run_number, data) VALUES (2, randomblob(4000));" BETWEEN_RUNS_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM flag_test;") echo " ✓ Data added between runs, total: $BETWEEN_RUNS_COUNT" echo "" echo "[5] CRITICAL: Second v0.5.0 run (where #754 might occur)..." echo " Starting v0.5.0 against database with existing v0.5.0 LTX files..." $LITESTREAM_V5 replicate "$DB" "file://$REPLICA" > /tmp/flag-reproduction-run2.log 2>&1 & RUN2_PID=$! sleep 5 if ! kill -0 $RUN2_PID 2>/dev/null; then echo " ✗ Second v0.5.0 run failed to start" cat /tmp/flag-reproduction-run2.log else echo " ✓ Second v0.5.0 run started (PID: $RUN2_PID)" fi echo "" echo "[6] Monitoring for #754 flag errors..." sleep 10 RUN2_FLAGS=$(grep -c "no flags allowed" /tmp/flag-reproduction-run2.log 2>/dev/null || echo "0") RUN2_VERIFICATION=$(grep -c "ltx verification failed" /tmp/flag-reproduction-run2.log 2>/dev/null || echo "0") RUN2_SYNC_ERRORS=$(grep -c "sync error" /tmp/flag-reproduction-run2.log 2>/dev/null || echo "0") RUN2_TOTAL_ERRORS=$(grep -c "ERROR" /tmp/flag-reproduction-run2.log 2>/dev/null || echo "0") echo " Second run error analysis:" echo " 'no flags allowed' errors: $RUN2_FLAGS" echo " 'ltx verification failed' errors: $RUN2_VERIFICATION" echo " 'sync error' count: $RUN2_SYNC_ERRORS" echo " Total errors: $RUN2_TOTAL_ERRORS" if [ "$RUN2_FLAGS" -gt "0" ] || [ "$RUN2_VERIFICATION" -gt "0" ]; then echo "" echo " 🚨 #754 FLAG ISSUE REPRODUCED!" echo " This occurs when v0.5.0 reads existing v0.5.0 LTX files" echo " Error details:" grep -A2 -B2 "no flags allowed\|ltx verification failed" /tmp/flag-reproduction-run2.log || true FLAG_ISSUE_REPRODUCED=true else echo " ✅ No #754 flag errors in second run" FLAG_ISSUE_REPRODUCED=false fi echo "" echo "[7] Adding more data during second run..." if kill -0 $RUN2_PID 2>/dev/null; then for i in {1..5}; do sqlite3 "$DB" "INSERT INTO flag_test (run_number, data) VALUES (2, randomblob(3500));" 2>/dev/null || true done FINAL_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM flag_test;") echo " ✓ Second run data added, final total: $FINAL_COUNT" kill $RUN2_PID 2>/dev/null || true wait $RUN2_PID 2>/dev/null else echo " ✗ Second run already failed, cannot add data" FINAL_COUNT=$BETWEEN_RUNS_COUNT fi echo "" echo "[8] Final analysis..." echo " Checking recent errors from second run:" if [ "$RUN2_TOTAL_ERRORS" -gt "0" ]; then tail -10 /tmp/flag-reproduction-run2.log | grep ERROR || echo " No recent errors" fi # Count total LTX files after both runs FINAL_LTX_FILES=$(find "$REPLICA" -name "*.ltx" 2>/dev/null | wc -l) echo "" echo "==========================================" echo "Flag Issue Reproduction Results" echo "==========================================" echo "" echo "Database progression:" echo " Initial: $INITIAL_COUNT records" echo " After run 1: $RUN1_COUNT records" echo " Between runs: $BETWEEN_RUNS_COUNT records" echo " Final: $FINAL_COUNT records" echo "" echo "Error analysis:" echo " Run 1 errors: $RUN1_ERRORS (flag errors: $RUN1_FLAGS)" echo " Run 2 errors: $RUN2_TOTAL_ERRORS (flag errors: $RUN2_FLAGS)" echo " LTX files created: $FINAL_LTX_FILES" echo "" echo "CRITICAL FINDING:" if [ "$FLAG_ISSUE_REPRODUCED" = true ]; then echo "🚨 #754 FLAG ISSUE REPRODUCED!" echo " Trigger: v0.5.0 restarting against existing v0.5.0 LTX files" echo " Root cause: HeaderFlagNoChecksum incompatibility with LTX v0.5.0" else echo "✅ Could not reproduce #754 flag issue" echo " Issue may require specific conditions or database content" fi echo "" echo "Implication for upgrades:" if [ "$FLAG_ISSUE_REPRODUCED" = true ]; then echo " v0.3.x → v0.5.0 upgrades should be safe (different file formats)" echo " v0.5.0 → v0.5.0 restarts are the problem" else echo " Further investigation needed to identify trigger conditions" fi echo "==========================================" ================================================ FILE: cmd/litestream-test/scripts/test-v0.5-restart-scenarios.sh ================================================ #!/bin/bash set -e # Test v0.5.0 restart scenarios to reproduce #754 flag issue # Focus on HeaderFlagNoChecksum usage and LTX file handling echo "==========================================" echo "v0.5.0 Restart Scenarios Test" echo "==========================================" echo "" echo "Testing various v0.5.0 restart conditions to reproduce #754" echo "" # Configuration DB="/tmp/restart-test.db" REPLICA="/tmp/restart-replica" LITESTREAM_V5="./bin/litestream" LITESTREAM_TEST="./bin/litestream-test" # Cleanup function cleanup() { pkill -f "litestream replicate.*restart-test.db" 2>/dev/null || true rm -f "$DB" "$DB-wal" "$DB-shm" "$DB-litestream" rm -rf "$REPLICA" rm -f /tmp/restart-*.log } trap cleanup EXIT echo "[SETUP] Cleaning up previous test files..." cleanup echo "" echo "==========================================" echo "Scenario 1: Simple v0.5.0 restart" echo "==========================================" echo "[1] Creating large database for restart testing..." $LITESTREAM_TEST populate -db "$DB" -target-size 1200MB >/dev/null 2>&1 # Add identifiable data sqlite3 "$DB" < /tmp/restart-run1.log 2>&1 & RUN1_PID=$! sleep 5 if ! kill -0 $RUN1_PID 2>/dev/null; then echo " ✗ First run failed" cat /tmp/restart-run1.log exit 1 fi echo " ✓ First run started (PID: $RUN1_PID)" # Add data during first run for i in {1..10}; do sqlite3 "$DB" "INSERT INTO restart_test (scenario, restart_number, data) VALUES ('first-run', 1, randomblob(3000));" done sqlite3 "$DB" "PRAGMA wal_checkpoint(FULL);" >/dev/null 2>&1 sleep 3 RUN1_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM restart_test;") echo " ✓ First run data: $RUN1_COUNT rows" echo "" echo "[3] Examining first run LTX files..." if [ -d "$REPLICA" ]; then LTX_FILES_RUN1=$(find "$REPLICA" -name "*.ltx" | wc -l) echo " LTX files after run 1: $LTX_FILES_RUN1" # Check for HeaderFlagNoChecksum in files echo " Examining LTX headers for flag usage..." find "$REPLICA" -name "*.ltx" | head -2 | while read ltx_file; do echo " $(basename $ltx_file): $(wc -c < "$ltx_file") bytes" done else echo " ✗ No replica directory found" exit 1 fi # Check first run errors RUN1_ERRORS=$(grep -c "ERROR" /tmp/restart-run1.log 2>/dev/null || echo "0") RUN1_FLAGS=$(grep -c "no flags allowed" /tmp/restart-run1.log 2>/dev/null || echo "0") echo " First run status: $RUN1_ERRORS errors, $RUN1_FLAGS flag errors" echo "" echo "[4] Stopping first run and adding offline data..." kill $RUN1_PID 2>/dev/null || true wait $RUN1_PID 2>/dev/null # Add data while Litestream is down sqlite3 "$DB" "INSERT INTO restart_test (scenario, restart_number, data) VALUES ('offline', 0, randomblob(4000));" OFFLINE_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM restart_test;") echo " ✓ Offline data added, total: $OFFLINE_COUNT rows" echo "" echo "[5] CRITICAL: Second v0.5.0 restart..." echo " Starting v0.5.0 against existing LTX files with HeaderFlagNoChecksum..." $LITESTREAM_V5 replicate "$DB" "file://$REPLICA" > /tmp/restart-run2.log 2>&1 & RUN2_PID=$! sleep 5 if ! kill -0 $RUN2_PID 2>/dev/null; then echo " ✗ Second run failed to start" cat /tmp/restart-run2.log exit 1 fi echo " ✓ Second run started (PID: $RUN2_PID)" echo "" echo "[6] Monitoring for #754 flag errors during restart..." sleep 10 RUN2_FLAGS=$(grep -c "no flags allowed" /tmp/restart-run2.log 2>/dev/null || echo "0") RUN2_VERIFICATION=$(grep -c "ltx verification failed" /tmp/restart-run2.log 2>/dev/null || echo "0") RUN2_SYNC_ERRORS=$(grep -c "sync error" /tmp/restart-run2.log 2>/dev/null || echo "0") RUN2_TOTAL_ERRORS=$(grep -c "ERROR" /tmp/restart-run2.log 2>/dev/null || echo "0") echo " Second run error analysis:" echo " 'no flags allowed' errors: $RUN2_FLAGS" echo " 'ltx verification failed' errors: $RUN2_VERIFICATION" echo " 'sync error' count: $RUN2_SYNC_ERRORS" echo " Total errors: $RUN2_TOTAL_ERRORS" if [ "$RUN2_FLAGS" -gt "0" ] || [ "$RUN2_VERIFICATION" -gt "0" ]; then echo "" echo " 🚨 #754 FLAG ISSUE REPRODUCED IN RESTART!" echo " Error details:" grep -A2 -B2 "no flags allowed\|ltx verification failed" /tmp/restart-run2.log || true RESTART_TRIGGERS_754=true else echo " ✅ No #754 flag errors in simple restart" RESTART_TRIGGERS_754=false fi echo "" echo "==========================================" echo "Scenario 2: Checkpoint during restart" echo "==========================================" # Add more data during second run echo "[7] Adding data during second run with checkpoints..." for i in {1..5}; do sqlite3 "$DB" "INSERT INTO restart_test (scenario, restart_number, data) VALUES ('second-run', 2, randomblob(3500));" if [ $((i % 2)) -eq 0 ]; then sqlite3 "$DB" "PRAGMA wal_checkpoint(FULL);" >/dev/null 2>&1 fi done RUN2_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM restart_test;") echo " ✓ Second run data with checkpoints: $RUN2_COUNT rows" # Monitor for additional errors sleep 5 CHECKPOINT_FLAGS=$(grep -c "no flags allowed" /tmp/restart-run2.log 2>/dev/null || echo "0") if [ "$CHECKPOINT_FLAGS" -gt "$RUN2_FLAGS" ]; then echo " ⚠️ Additional flag errors during checkpoint operations" fi echo "" echo "==========================================" echo "Scenario 3: Multiple restart cycles" echo "==========================================" echo "[8] Third restart cycle..." kill $RUN2_PID 2>/dev/null || true wait $RUN2_PID 2>/dev/null sqlite3 "$DB" "INSERT INTO restart_test (scenario, restart_number, data) VALUES ('between-2-and-3', 0, randomblob(2500));" $LITESTREAM_V5 replicate "$DB" "file://$REPLICA" > /tmp/restart-run3.log 2>&1 & RUN3_PID=$! sleep 5 if kill -0 $RUN3_PID 2>/dev/null; then echo " ✓ Third run started (PID: $RUN3_PID)" # Quick check for immediate errors sleep 5 RUN3_FLAGS=$(grep -c "no flags allowed" /tmp/restart-run3.log 2>/dev/null || echo "0") RUN3_ERRORS=$(grep -c "ERROR" /tmp/restart-run3.log 2>/dev/null || echo "0") echo " Third run status: $RUN3_ERRORS errors, $RUN3_FLAGS flag errors" if [ "$RUN3_FLAGS" -gt "0" ]; then echo " ⚠️ Flag errors in third restart" fi kill $RUN3_PID 2>/dev/null || true wait $RUN3_PID 2>/dev/null else echo " ✗ Third run failed" cat /tmp/restart-run3.log | head -10 fi echo "" echo "[9] Final analysis..." FINAL_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM restart_test;") FINAL_LTX_FILES=$(find "$REPLICA" -name "*.ltx" 2>/dev/null | wc -l) echo " Final statistics:" echo " Database rows: $FINAL_COUNT" echo " LTX files created: $FINAL_LTX_FILES" echo " Run 1 errors: $RUN1_ERRORS (flags: $RUN1_FLAGS)" echo " Run 2 errors: $RUN2_TOTAL_ERRORS (flags: $RUN2_FLAGS)" echo " Run 3 errors: $RUN3_ERRORS (flags: $RUN3_FLAGS)" echo "" echo "==========================================" echo "v0.5.0 Restart Test Results" echo "==========================================" echo "" echo "Test scenarios:" echo " ✓ Simple restart: $([ "$RESTART_TRIGGERS_754" = true ] && echo "REPRODUCED #754" || echo "No #754 errors")" echo " ✓ Checkpoint during restart: $([ "$CHECKPOINT_FLAGS" -gt "$RUN2_FLAGS" ] && echo "Additional errors" || echo "No additional errors")" echo " ✓ Multiple restart cycles: $([ "${RUN3_FLAGS:-0}" -gt "0" ] && echo "Errors in cycle 3" || echo "No errors in cycle 3")" echo "" echo "CRITICAL FINDINGS:" if [ "$RESTART_TRIGGERS_754" = true ] || [ "${RUN3_FLAGS:-0}" -gt "0" ]; then echo "🚨 #754 FLAG ISSUE TRIGGERED BY v0.5.0 RESTARTS" echo " Root cause: v0.5.0 reading its own LTX files with HeaderFlagNoChecksum" echo " Trigger: Restarting Litestream against existing v0.5.0 backup files" echo " Impact: Production Litestream restarts will fail" else echo "✅ No #754 errors in restart scenarios tested" echo " Issue may require specific database content or timing conditions" fi echo "" echo "Next steps:" echo "1. Check HeaderFlagNoChecksum usage in db.go" echo "2. Test with different database sizes/content" echo "3. Investigate LTX file generation differences" echo "==========================================" ================================================ FILE: cmd/litestream-test/scripts/verify-test-setup.sh ================================================ #!/bin/bash # Script to verify test environment is set up correctly # Ensures we're using local builds, not system-installed versions echo "==========================================" echo "Litestream Test Environment Verification" echo "==========================================" echo "" # Check for local Litestream build echo "Checking for local Litestream build..." if [ -f "./bin/litestream" ]; then echo "✓ Local litestream found: ./bin/litestream" echo " Version: $($./bin/litestream version)" echo " Size: $(ls -lh ./bin/litestream | awk '{print $5}')" echo " Modified: $(ls -la ./bin/litestream | awk '{print $6, $7, $8}')" else echo "✗ Local litestream NOT found at ./bin/litestream" echo " Please build: go build -o bin/litestream ./cmd/litestream" exit 1 fi # Check for system Litestream (should NOT be used) echo "" echo "Checking for system Litestream..." if command -v litestream &> /dev/null; then SYSTEM_LITESTREAM=$(which litestream) echo "⚠ System litestream found at: $SYSTEM_LITESTREAM" echo " Version: $(litestream version 2>&1 || echo "unknown")" echo " WARNING: Tests should NOT use this version!" echo " All test scripts use ./bin/litestream explicitly" else echo "✓ No system litestream found (good - avoids confusion)" fi # Check for litestream-test binary echo "" echo "Checking for litestream-test binary..." if [ -f "./bin/litestream-test" ]; then echo "✓ Local litestream-test found: ./bin/litestream-test" echo " Size: $(ls -lh ./bin/litestream-test | awk '{print $5}')" echo " Modified: $(ls -la ./bin/litestream-test | awk '{print $6, $7, $8}')" else echo "✗ litestream-test NOT found at ./bin/litestream-test" echo " Please build: go build -o bin/litestream-test ./cmd/litestream-test" exit 1 fi # Verify test scripts use local builds echo "" echo "Verifying test scripts use local builds..." SCRIPTS=( "reproduce-critical-bug.sh" "test-1gb-boundary.sh" "test-concurrent-operations.sh" ) ALL_GOOD=true for script in "${SCRIPTS[@]}"; do if [ -f "$script" ]; then if grep -q 'LITESTREAM="./bin/litestream"' "$script"; then echo "✓ $script uses local build" else echo "✗ $script may not use local build!" grep "LITESTREAM=" "$script" | head -2 ALL_GOOD=false fi else echo "- $script not found (optional)" fi done # Check current git branch echo "" echo "Git status:" BRANCH=$(git branch --show-current 2>/dev/null || echo "unknown") echo " Current branch: $BRANCH" if [ "$BRANCH" = "main" ]; then echo " ⚠ On main branch - be careful with commits!" fi # Summary echo "" echo "==========================================" if [ "$ALL_GOOD" = true ] && [ -f "./bin/litestream" ] && [ -f "./bin/litestream-test" ]; then echo "✅ Test environment is properly configured!" echo "" echo "You can run tests with:" echo " ./reproduce-critical-bug.sh" echo " ./test-1gb-boundary.sh" echo " ./test-concurrent-operations.sh" else echo "❌ Test environment needs setup" echo "" echo "Required steps:" [ ! -f "./bin/litestream" ] && echo " 1. Build litestream: go build -o bin/litestream ./cmd/litestream" [ ! -f "./bin/litestream-test" ] && echo " 2. Build test harness: go build -o bin/litestream-test ./cmd/litestream-test" exit 1 fi echo "==========================================" ================================================ FILE: cmd/litestream-test/shrink.go ================================================ package main import ( "context" "database/sql" "flag" "fmt" "log/slog" "os" "time" _ "github.com/mattn/go-sqlite3" ) type ShrinkCommand struct { Main *Main DB string DeletePercentage float64 Vacuum bool Checkpoint bool CheckpointMode string } func (c *ShrinkCommand) Run(ctx context.Context, args []string) error { fs := flag.NewFlagSet("litestream-test shrink", flag.ExitOnError) fs.StringVar(&c.DB, "db", "", "Database path (required)") fs.Float64Var(&c.DeletePercentage, "delete-percentage", 50, "Percentage of data to delete (0-100)") fs.BoolVar(&c.Vacuum, "vacuum", false, "Run VACUUM after deletion") fs.BoolVar(&c.Checkpoint, "checkpoint", false, "Run checkpoint after deletion") fs.StringVar(&c.CheckpointMode, "checkpoint-mode", "PASSIVE", "Checkpoint mode (PASSIVE, FULL, RESTART, TRUNCATE)") fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } if c.DB == "" { return fmt.Errorf("database path required") } if c.DeletePercentage < 0 || c.DeletePercentage > 100 { return fmt.Errorf("delete percentage must be between 0 and 100") } if _, err := os.Stat(c.DB); err != nil { return fmt.Errorf("database does not exist: %w", err) } slog.Info("Starting database shrink operation", "db", c.DB, "delete_percentage", c.DeletePercentage, "vacuum", c.Vacuum, "checkpoint", c.Checkpoint, ) return c.shrinkDatabase(ctx) } func (c *ShrinkCommand) shrinkDatabase(ctx context.Context) error { initialSize, err := getDatabaseSize(c.DB) if err != nil { return fmt.Errorf("get initial size: %w", err) } slog.Info("Initial database size", "size_mb", initialSize/1024/1024, ) db, err := sql.Open("sqlite3", c.DB+"?_journal_mode=WAL") if err != nil { return fmt.Errorf("open database: %w", err) } defer db.Close() tables, err := c.getTableList(db) if err != nil { return fmt.Errorf("get table list: %w", err) } slog.Info("Found tables", "count", len(tables)) totalDeleted := int64(0) for _, table := range tables { deleted, err := c.deleteFromTable(db, table) if err != nil { slog.Error("Failed to delete from table", "table", table, "error", err) continue } totalDeleted += deleted slog.Info("Deleted rows from table", "table", table, "rows_deleted", deleted, ) } slog.Info("Deletion complete", "total_rows_deleted", totalDeleted) sizeAfterDelete, err := getDatabaseSize(c.DB) if err != nil { return fmt.Errorf("get size after delete: %w", err) } slog.Info("Size after deletion", "size_mb", sizeAfterDelete/1024/1024, "change_mb", (initialSize-sizeAfterDelete)/1024/1024, ) if c.Checkpoint { if err := c.runCheckpoint(db); err != nil { return fmt.Errorf("checkpoint: %w", err) } sizeAfterCheckpoint, _ := getDatabaseSize(c.DB) slog.Info("Size after checkpoint", "size_mb", sizeAfterCheckpoint/1024/1024, "change_from_delete_mb", (sizeAfterDelete-sizeAfterCheckpoint)/1024/1024, ) } if c.Vacuum { if err := c.runVacuum(db); err != nil { return fmt.Errorf("vacuum: %w", err) } sizeAfterVacuum, _ := getDatabaseSize(c.DB) slog.Info("Size after VACUUM", "size_mb", sizeAfterVacuum/1024/1024, "total_reduction_mb", (initialSize-sizeAfterVacuum)/1024/1024, ) } finalSize, err := getDatabaseSize(c.DB) if err != nil { return fmt.Errorf("get final size: %w", err) } reductionPercent := float64(initialSize-finalSize) / float64(initialSize) * 100 slog.Info("Shrink operation complete", "initial_size_mb", initialSize/1024/1024, "final_size_mb", finalSize/1024/1024, "reduction_percent", fmt.Sprintf("%.1f", reductionPercent), ) return nil } func (c *ShrinkCommand) getTableList(db *sql.DB) ([]string, error) { rows, err := db.Query(` SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE 'load_test' `) if err != nil { return nil, err } defer rows.Close() var tables []string for rows.Next() { var table string if err := rows.Scan(&table); err != nil { return nil, err } tables = append(tables, table) } return tables, nil } func (c *ShrinkCommand) deleteFromTable(db *sql.DB, table string) (int64, error) { var totalRows int countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s", table) if err := db.QueryRow(countQuery).Scan(&totalRows); err != nil { return 0, fmt.Errorf("count rows: %w", err) } if totalRows == 0 { return 0, nil } rowsToDelete := int(float64(totalRows) * (c.DeletePercentage / 100)) if rowsToDelete == 0 { return 0, nil } var hasID bool columnQuery := fmt.Sprintf("PRAGMA table_info(%s)", table) rows, err := db.Query(columnQuery) if err != nil { return 0, fmt.Errorf("get table info: %w", err) } defer rows.Close() for rows.Next() { var cid int var name, dtype string var notnull, pk int var dflt sql.NullString if err := rows.Scan(&cid, &name, &dtype, ¬null, &dflt, &pk); err != nil { continue } if name == "id" || pk == 1 { hasID = true break } } var deleteQuery string if hasID { deleteQuery = fmt.Sprintf(` DELETE FROM %s WHERE id IN ( SELECT id FROM %s ORDER BY RANDOM() LIMIT %d ) `, table, table, rowsToDelete) } else { deleteQuery = fmt.Sprintf(` DELETE FROM %s WHERE rowid IN ( SELECT rowid FROM %s ORDER BY RANDOM() LIMIT %d ) `, table, table, rowsToDelete) } startTime := time.Now() result, err := db.Exec(deleteQuery) if err != nil { return 0, fmt.Errorf("delete rows: %w", err) } rowsDeleted, _ := result.RowsAffected() duration := time.Since(startTime) slog.Debug("Deleted rows from table", "table", table, "rows_deleted", rowsDeleted, "duration", duration, ) return rowsDeleted, nil } func (c *ShrinkCommand) runCheckpoint(db *sql.DB) error { slog.Info("Running checkpoint", "mode", c.CheckpointMode) startTime := time.Now() query := fmt.Sprintf("PRAGMA wal_checkpoint(%s)", c.CheckpointMode) var busy, written, total int err := db.QueryRow(query).Scan(&busy, &written, &total) if err != nil { return fmt.Errorf("checkpoint failed: %w", err) } duration := time.Since(startTime) slog.Info("Checkpoint complete", "mode", c.CheckpointMode, "busy", busy, "pages_written", written, "total_pages", total, "duration", duration, ) return nil } func (c *ShrinkCommand) runVacuum(db *sql.DB) error { slog.Info("Running VACUUM (this may take a while)") startTime := time.Now() _, err := db.Exec("VACUUM") if err != nil { return fmt.Errorf("vacuum failed: %w", err) } duration := time.Since(startTime) slog.Info("VACUUM complete", "duration", duration) return nil } func (c *ShrinkCommand) Usage() { fmt.Fprintln(c.Main.Stdout, ` Shrink a database by deleting data and optionally running VACUUM. Usage: litestream-test shrink [options] Options: -db PATH Database path (required) -delete-percentage PCT Percentage of data to delete (0-100) Default: 50 -vacuum Run VACUUM after deletion Default: false -checkpoint Run checkpoint after deletion Default: false -checkpoint-mode MODE Checkpoint mode (PASSIVE, FULL, RESTART, TRUNCATE) Default: PASSIVE Examples: # Delete 50% of data litestream-test shrink -db /tmp/test.db -delete-percentage 50 # Delete 75% and run VACUUM litestream-test shrink -db /tmp/test.db -delete-percentage 75 -vacuum # Delete 30%, checkpoint, then VACUUM litestream-test shrink -db /tmp/test.db -delete-percentage 30 -checkpoint -vacuum # Test with FULL checkpoint mode litestream-test shrink -db /tmp/test.db -delete-percentage 50 -checkpoint -checkpoint-mode FULL `[1:]) } ================================================ FILE: cmd/litestream-test/validate.go ================================================ package main import ( "context" "crypto/md5" "database/sql" "flag" "fmt" "io" "log/slog" "os" "os/exec" "strings" "time" _ "github.com/mattn/go-sqlite3" ) type ValidateCommand struct { Main *Main SourceDB string ReplicaURL string RestoredDB string CheckType string LTXContinuity bool ConfigPath string } type ValidationResult struct { CheckType string Passed bool Duration time.Duration ErrorMessage string Details map[string]interface{} } func (c *ValidateCommand) Run(ctx context.Context, args []string) error { fs := flag.NewFlagSet("litestream-test validate", flag.ExitOnError) fs.StringVar(&c.SourceDB, "source-db", "", "Original database path") fs.StringVar(&c.ReplicaURL, "replica-url", "", "Replica URL to validate") fs.StringVar(&c.RestoredDB, "restored-db", "", "Path for restored database") fs.StringVar(&c.CheckType, "check-type", "quick", "Type of check (quick, integrity, checksum, full)") fs.BoolVar(&c.LTXContinuity, "ltx-continuity", false, "Check LTX file continuity") fs.StringVar(&c.ConfigPath, "config", "", "Litestream config file path") fs.Usage = c.Usage if err := fs.Parse(args); err != nil { return err } if c.SourceDB == "" { return fmt.Errorf("source database path required") } if c.ReplicaURL == "" && c.ConfigPath == "" { return fmt.Errorf("replica URL or config file required") } if c.RestoredDB == "" { c.RestoredDB = c.SourceDB + ".restored" } slog.Info("Starting validation", "source_db", c.SourceDB, "replica_url", c.ReplicaURL, "check_type", c.CheckType, "ltx_continuity", c.LTXContinuity, ) results := []ValidationResult{} if c.LTXContinuity && c.ReplicaURL != "" { result := c.validateLTXContinuity(ctx) results = append(results, result) } restoreResult := c.performRestore(ctx) results = append(results, restoreResult) if restoreResult.Passed { switch c.CheckType { case "quick": results = append(results, c.performQuickCheck(ctx)) case "integrity": results = append(results, c.performIntegrityCheck(ctx)) case "checksum": results = append(results, c.performChecksumCheck(ctx)) case "full": results = append(results, c.performQuickCheck(ctx)) results = append(results, c.performIntegrityCheck(ctx)) results = append(results, c.performChecksumCheck(ctx)) results = append(results, c.performDataValidation(ctx)) } } return c.reportResults(results) } func (c *ValidateCommand) performRestore(ctx context.Context) ValidationResult { startTime := time.Now() result := ValidationResult{ CheckType: "restore", Details: make(map[string]interface{}), } if err := os.Remove(c.RestoredDB); err != nil && !os.IsNotExist(err) { slog.Warn("Could not remove existing restored database", "error", err) } var cmd *exec.Cmd if c.ConfigPath != "" { cmd = exec.CommandContext(ctx, "litestream", "restore", "-config", c.ConfigPath, "-o", c.RestoredDB, c.SourceDB, ) } else { cmd = exec.CommandContext(ctx, "litestream", "restore", "-o", c.RestoredDB, c.ReplicaURL, ) } output, err := cmd.CombinedOutput() result.Duration = time.Since(startTime) if err != nil { result.Passed = false result.ErrorMessage = fmt.Sprintf("restore failed: %v\nOutput: %s", err, string(output)) return result } if _, err := os.Stat(c.RestoredDB); err != nil { result.Passed = false result.ErrorMessage = fmt.Sprintf("restored database not found: %v", err) return result } result.Passed = true result.Details["restored_path"] = c.RestoredDB if info, err := os.Stat(c.RestoredDB); err == nil { result.Details["restored_size"] = info.Size() } slog.Info("Restore completed", "duration", result.Duration, "restored_db", c.RestoredDB, ) return result } func (c *ValidateCommand) performQuickCheck(ctx context.Context) ValidationResult { startTime := time.Now() result := ValidationResult{ CheckType: "quick_check", Details: make(map[string]interface{}), } db, err := sql.Open("sqlite3", c.RestoredDB) if err != nil { result.Passed = false result.ErrorMessage = fmt.Sprintf("failed to open database: %v", err) return result } defer db.Close() var checkResult string err = db.QueryRow("PRAGMA quick_check").Scan(&checkResult) result.Duration = time.Since(startTime) if err != nil { result.Passed = false result.ErrorMessage = fmt.Sprintf("quick check failed: %v", err) return result } result.Passed = checkResult == "ok" result.Details["check_result"] = checkResult if !result.Passed { result.ErrorMessage = fmt.Sprintf("quick check returned: %s", checkResult) } slog.Info("Quick check completed", "passed", result.Passed, "duration", result.Duration, ) return result } func (c *ValidateCommand) performIntegrityCheck(ctx context.Context) ValidationResult { startTime := time.Now() result := ValidationResult{ CheckType: "integrity_check", Details: make(map[string]interface{}), } db, err := sql.Open("sqlite3", c.RestoredDB) if err != nil { result.Passed = false result.ErrorMessage = fmt.Sprintf("failed to open database: %v", err) return result } defer db.Close() rows, err := db.Query("PRAGMA integrity_check") if err != nil { result.Passed = false result.ErrorMessage = fmt.Sprintf("integrity check failed: %v", err) result.Duration = time.Since(startTime) return result } defer rows.Close() var results []string for rows.Next() { var line string if err := rows.Scan(&line); err != nil { result.Passed = false result.ErrorMessage = fmt.Sprintf("failed to scan result: %v", err) result.Duration = time.Since(startTime) return result } results = append(results, line) } result.Duration = time.Since(startTime) result.Details["check_results"] = results if len(results) == 1 && results[0] == "ok" { result.Passed = true } else { result.Passed = false result.ErrorMessage = fmt.Sprintf("integrity check found issues: %v", results) } slog.Info("Integrity check completed", "passed", result.Passed, "duration", result.Duration, "issues", len(results)-1, ) return result } func (c *ValidateCommand) performChecksumCheck(ctx context.Context) ValidationResult { startTime := time.Now() result := ValidationResult{ CheckType: "checksum", Details: make(map[string]interface{}), } sourceChecksum, err := c.calculateDBChecksum(c.SourceDB) if err != nil { result.Passed = false result.ErrorMessage = fmt.Sprintf("failed to calculate source checksum: %v", err) result.Duration = time.Since(startTime) return result } restoredChecksum, err := c.calculateDBChecksum(c.RestoredDB) if err != nil { result.Passed = false result.ErrorMessage = fmt.Sprintf("failed to calculate restored checksum: %v", err) result.Duration = time.Since(startTime) return result } result.Duration = time.Since(startTime) result.Details["source_checksum"] = fmt.Sprintf("%x", sourceChecksum) result.Details["restored_checksum"] = fmt.Sprintf("%x", restoredChecksum) if string(sourceChecksum) == string(restoredChecksum) { result.Passed = true } else { result.Passed = false result.ErrorMessage = "checksums do not match" } slog.Info("Checksum check completed", "passed", result.Passed, "duration", result.Duration, "match", result.Passed, ) return result } func (c *ValidateCommand) performDataValidation(ctx context.Context) ValidationResult { startTime := time.Now() result := ValidationResult{ CheckType: "data_validation", Details: make(map[string]interface{}), } sourceDB, err := sql.Open("sqlite3", c.SourceDB) if err != nil { result.Passed = false result.ErrorMessage = fmt.Sprintf("failed to open source database: %v", err) return result } defer sourceDB.Close() restoredDB, err := sql.Open("sqlite3", c.RestoredDB) if err != nil { result.Passed = false result.ErrorMessage = fmt.Sprintf("failed to open restored database: %v", err) return result } defer restoredDB.Close() tables, err := c.getTableList(sourceDB) if err != nil { result.Passed = false result.ErrorMessage = fmt.Sprintf("failed to get table list: %v", err) result.Duration = time.Since(startTime) return result } result.Details["tables_checked"] = len(tables) allMatch := true for _, table := range tables { sourceCount, err := c.getRowCount(sourceDB, table) if err != nil { result.Passed = false result.ErrorMessage = fmt.Sprintf("failed to count rows in source table %s: %v", table, err) result.Duration = time.Since(startTime) return result } restoredCount, err := c.getRowCount(restoredDB, table) if err != nil { result.Passed = false result.ErrorMessage = fmt.Sprintf("failed to count rows in restored table %s: %v", table, err) result.Duration = time.Since(startTime) return result } if sourceCount != restoredCount { allMatch = false result.Details[fmt.Sprintf("table_%s_mismatch", table)] = fmt.Sprintf("source=%d, restored=%d", sourceCount, restoredCount) } } result.Duration = time.Since(startTime) result.Passed = allMatch if !allMatch { result.ErrorMessage = "row count mismatch between source and restored databases" } slog.Info("Data validation completed", "passed", result.Passed, "duration", result.Duration, "tables_checked", len(tables), ) return result } func (c *ValidateCommand) validateLTXContinuity(ctx context.Context) ValidationResult { startTime := time.Now() result := ValidationResult{ CheckType: "ltx_continuity", Details: make(map[string]interface{}), } cmd := exec.CommandContext(ctx, "litestream", "ltx", c.ReplicaURL) output, err := cmd.Output() if err != nil { result.Passed = false result.ErrorMessage = fmt.Sprintf("failed to list LTX files: %v", err) result.Duration = time.Since(startTime) return result } lines := strings.Split(string(output), "\n") if len(lines) < 2 { result.Passed = false result.ErrorMessage = "no LTX files found" result.Duration = time.Since(startTime) return result } result.Passed = true result.Duration = time.Since(startTime) result.Details["ltx_files_checked"] = len(lines) - 2 slog.Info("LTX continuity check completed", "passed", result.Passed, "duration", result.Duration, ) return result } func (c *ValidateCommand) calculateDBChecksum(path string) ([]byte, error) { file, err := os.Open(path) if err != nil { return nil, err } defer file.Close() hash := md5.New() if _, err := io.Copy(hash, file); err != nil { return nil, err } return hash.Sum(nil), nil } func (c *ValidateCommand) getTableList(db *sql.DB) ([]string, error) { rows, err := db.Query("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'") if err != nil { return nil, err } defer rows.Close() var tables []string for rows.Next() { var table string if err := rows.Scan(&table); err != nil { return nil, err } tables = append(tables, table) } return tables, nil } func (c *ValidateCommand) getRowCount(db *sql.DB, table string) (int, error) { var count int query := fmt.Sprintf("SELECT COUNT(*) FROM %s", table) err := db.QueryRow(query).Scan(&count) return count, err } func (c *ValidateCommand) reportResults(results []ValidationResult) error { allPassed := true for _, result := range results { if !result.Passed { allPassed = false slog.Error("Validation failed", "check_type", result.CheckType, "error", result.ErrorMessage, ) } } if allPassed { slog.Info("All validation checks passed") return nil } return fmt.Errorf("validation failed") } func (c *ValidateCommand) Usage() { fmt.Fprintln(c.Main.Stdout, ` Validate replication integrity by restoring and checking databases. Usage: litestream-test validate [options] Options: -source-db PATH Original database path (required) -replica-url URL Replica URL to validate -restored-db PATH Path for restored database Default: source-db.restored -check-type TYPE Type of check: quick, integrity, checksum, full Default: quick -ltx-continuity Check LTX file continuity Default: false -config PATH Litestream config file path Examples: # Quick validation litestream-test validate -source-db /tmp/test.db -replica-url s3://bucket/test # Full validation with all checks litestream-test validate -source-db /tmp/test.db -replica-url s3://bucket/test -check-type full # Validate with config file litestream-test validate -source-db /tmp/test.db -config /etc/litestream.yml -check-type integrity `[1:]) } ================================================ FILE: cmd/litestream-vfs/chaos_test.go ================================================ //go:build vfs && chaos // +build vfs,chaos package main_test import ( "bytes" "context" "io" "math/rand" "sync/atomic" "testing" "time" "github.com/superfly/ltx" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/internal/testingutil" ) func TestVFS_ChaosEngineering(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) db, primary := openReplicatedPrimary(t, client, 15*time.Millisecond, 15*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec(`CREATE TABLE chaos ( id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT, grp INTEGER )`); err != nil { t.Fatalf("create table: %v", err) } for i := 0; i < 64; i++ { if _, err := primary.Exec("INSERT INTO chaos (value, grp) VALUES (?, ?)", randomPayload(rand.New(rand.NewSource(int64(i))), 48), i%8); err != nil { t.Fatalf("seed chaos: %v", err) } } chaosClient := newChaosReplicaClient(client) vfs := newVFS(t, chaosClient) vfs.PollInterval = 15 * time.Millisecond vfsName := registerTestVFS(t, vfs) replica := openVFSReplicaDB(t, vfsName) defer replica.Close() waitForTableRowCount(t, primary, replica, "chaos", 5*time.Second) chaosClient.active.Store(true) ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() writerDone := make(chan error, 1) go func() { rnd := rand.New(rand.NewSource(42)) for { select { case <-ctx.Done(): writerDone <- nil return default: } switch rnd.Intn(3) { case 0: if _, err := primary.Exec("INSERT INTO chaos (value, grp) VALUES (?, ?)", randomPayload(rnd, 32), rnd.Intn(8)); err != nil && !isBusyError(err) { writerDone <- err return } case 1: if _, err := primary.Exec("UPDATE chaos SET value = ? WHERE id = (ABS(random()) % 64) + 1", randomPayload(rnd, 24)); err != nil && !isBusyError(err) { writerDone <- err return } case 2: if _, err := primary.Exec("DELETE FROM chaos WHERE id IN (SELECT id FROM chaos ORDER BY RANDOM() LIMIT 1)"); err != nil && !isBusyError(err) { writerDone <- err return } } time.Sleep(5 * time.Millisecond) } }() const readers = 16 readerErrs := make(chan error, readers) for i := 0; i < readers; i++ { go func() { rnd := rand.New(rand.NewSource(time.Now().UnixNano())) for { select { case <-ctx.Done(): readerErrs <- nil return default: } var count int switch rnd.Intn(3) { case 0: err := replica.QueryRow("SELECT COUNT(*) FROM chaos WHERE grp = ?", rnd.Intn(8)).Scan(&count) if err != nil { if isBusyError(err) { continue } readerErrs <- err return } case 1: rows, err := replica.Query("SELECT id, value FROM chaos ORDER BY id DESC LIMIT 5 OFFSET ?", rnd.Intn(10)) if err != nil { if isBusyError(err) { continue } readerErrs <- err return } retryRows := false for rows.Next() { var id int var value string if err := rows.Scan(&id, &value); err != nil { rows.Close() if isBusyError(err) { retryRows = true break } readerErrs <- err return } } if retryRows { continue } if err := rows.Err(); err != nil { rows.Close() if isBusyError(err) { continue } readerErrs <- err return } rows.Close() case 2: err := replica.QueryRow("SELECT SUM(LENGTH(value)) FROM chaos WHERE id BETWEEN ? AND ?", rnd.Intn(32)+1, rnd.Intn(32)+33).Scan(&count) if err != nil { if isBusyError(err) { continue } readerErrs <- err return } } } }() } <-ctx.Done() for i := 0; i < readers; i++ { if err := <-readerErrs; err != nil { t.Fatalf("reader error: %v", err) } } if err := <-writerDone; err != nil { t.Fatalf("writer error: %v", err) } waitForTableRowCount(t, primary, replica, "chaos", 5*time.Second) if chaosClient.failures.Load() == 0 { t.Fatalf("expected injected failures") } } func newChaosReplicaClient(base litestream.ReplicaClient) *chaosReplicaClient { return &chaosReplicaClient{ ReplicaClient: base, rnd: rand.New(rand.NewSource(99)), } } type chaosReplicaClient struct { litestream.ReplicaClient rnd *rand.Rand failures atomic.Int32 active atomic.Bool } func (c *chaosReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { if !c.active.Load() { return c.ReplicaClient.LTXFiles(ctx, level, seek, useMetadata) } if c.rnd.Float64() < 0.05 { c.failures.Add(1) return nil, context.DeadlineExceeded } return c.ReplicaClient.LTXFiles(ctx, level, seek, useMetadata) } func (c *chaosReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { if !c.active.Load() { return c.ReplicaClient.OpenLTXFile(ctx, level, minTXID, maxTXID, offset, size) } delay := time.Duration(c.rnd.Intn(5)) * time.Millisecond if delay > 0 { time.Sleep(delay) } if c.rnd.Float64() < 0.05 { c.failures.Add(1) return nil, context.DeadlineExceeded } rc, err := c.ReplicaClient.OpenLTXFile(ctx, level, minTXID, maxTXID, offset, size) if err != nil { return nil, err } if c.rnd.Float64() < 0.05 && size > 0 { data, err := io.ReadAll(rc) rc.Close() if err != nil { return nil, err } if len(data) > 32 { data = data[:len(data)/2] } c.failures.Add(1) return io.NopCloser(bytes.NewReader(data)), nil } return rc, nil } ================================================ FILE: cmd/litestream-vfs/fuzz_test.go ================================================ //go:build vfs // +build vfs package main_test import ( "database/sql" "fmt" "os" "strings" "testing" "time" "github.com/stretchr/testify/require" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/internal/testingutil" ) // TestVFS_FuzzSeedCorpus runs a handful of fixed corpora so `go test` // exercises the same logic as the fuzz harness without requiring // `-fuzz=...`. func TestVFS_FuzzSeedCorpus(t *testing.T) { seeds := [][]byte{ []byte{0x00, 0x01, 0x02}, []byte("litestream vfs fuzz"), []byte{0xFF, 0x10, 0x42, 0x7F}, } for _, seed := range seeds { runVFSFuzzWorkload(t, seed) } } // FuzzVFSReplicaReadPatterns exercises random combinations of reads, // aggregates, and ordering queries against the VFS replica. Enable with: // // go test ./cmd/litestream-vfs -tags vfs -fuzz=FuzzVFSReplicaReadPatterns func FuzzVFSReplicaReadPatterns(f *testing.F) { f.Add([]byte("seed")) f.Add([]byte{0x1, 0x2, 0x3, 0x4}) f.Add([]byte{0xAA, 0xBB, 0xCC}) f.Fuzz(func(t *testing.T, data []byte) { runVFSFuzzWorkload(t, data) }) } func runVFSFuzzWorkload(tb testing.TB, corpus []byte) { tb.Helper() if len(corpus) == 0 { corpus = []byte{0} } if len(corpus) > 256 { corpus = corpus[:256] } client := file.NewReplicaClient(tb.TempDir()) if err := os.MkdirAll(client.LTXLevelDir(0), 0o755); err != nil { tb.Fatalf("init replica dir: %v", err) } _, primary := openReplicatedPrimary(tb, client, 15*time.Millisecond, 15*time.Millisecond) defer testingutil.MustCloseSQLDB(tb, primary) if _, err := primary.Exec(`CREATE TABLE fuzz ( id INTEGER PRIMARY KEY, value TEXT, grp INTEGER )`); err != nil { tb.Fatalf("create table: %v", err) } // Deterministic seed data so we have plenty of rows/pages to hydrate. for i := 0; i < 128; i++ { payload := fmt.Sprintf("row-%03d-%s", i, strings.Repeat("x", (i%17)+8)) if _, err := primary.Exec("INSERT INTO fuzz (value, grp) VALUES (?, ?)", payload, i%11); err != nil { tb.Fatalf("seed insert: %v", err) } } vfs := newVFS(tb, client) vfs.PollInterval = 15 * time.Millisecond vfsName := registerTestVFS(tb, vfs) replica := openVFSReplicaDB(tb, vfsName) defer replica.Close() require.Eventually(tb, func() bool { var primaryCount, replicaCount int if err := primary.QueryRow("SELECT COUNT(*) FROM fuzz").Scan(&primaryCount); err != nil { return false } if err := replica.QueryRow("SELECT COUNT(*) FROM fuzz").Scan(&replicaCount); err != nil { return false } return primaryCount == replicaCount }, 5*time.Second, 20*time.Millisecond, "replica should catch up to primary") const maxOps = 128 for i := 0; i < len(corpus) && i < maxOps; i++ { op := corpus[i] % 6 switch op { case 0: id := int(corpus[i])%128 + 1 var value string err := replica.QueryRow("SELECT value FROM fuzz WHERE id = ?", id).Scan(&value) if err != nil && err != sql.ErrNoRows { tb.Fatalf("select by id: %v", err) } case 1: var count int if err := replica.QueryRow("SELECT COUNT(*) FROM fuzz WHERE grp = ?", int(corpus[i])%11).Scan(&count); err != nil { tb.Fatalf("count grp: %v", err) } case 2: rows, err := replica.Query("SELECT value FROM fuzz ORDER BY value DESC LIMIT 5 OFFSET ?", int(corpus[i])%10) if err != nil { tb.Fatalf("ordered scan: %v", err) } for rows.Next() { var v string if err := rows.Scan(&v); err != nil { tb.Fatalf("scan ordered: %v", err) } } if err := rows.Err(); err != nil { tb.Fatalf("ordered rows err: %v", err) } rows.Close() case 3: var sum int if err := replica.QueryRow("SELECT SUM(LENGTH(value)) FROM fuzz WHERE id BETWEEN ? AND ?", int(corpus[i])%64+1, int(corpus[i])%64+64).Scan(&sum); err != nil { tb.Fatalf("sum lengths: %v", err) } case 4: // Cross-check counts between primary & replica for a random grp. grp := int(corpus[i]) % 11 var pc, rc int if err := primary.QueryRow("SELECT COUNT(*) FROM fuzz WHERE grp = ?", grp).Scan(&pc); err != nil { tb.Fatalf("primary grp count: %v", err) } if err := replica.QueryRow("SELECT COUNT(*) FROM fuzz WHERE grp = ?", grp).Scan(&rc); err != nil { tb.Fatalf("replica grp count: %v", err) } if pc != rc { tb.Fatalf("count mismatch grp=%d primary=%d replica=%d", grp, pc, rc) } case 5: // Random LIKE query to exercise page cache churn. pattern := fmt.Sprintf("row-%%%02x%%", corpus[i]) rows, err := replica.Query("SELECT id FROM fuzz WHERE value LIKE ? LIMIT 3", pattern) if err != nil { tb.Fatalf("like query: %v", err) } rows.Close() } } } ================================================ FILE: cmd/litestream-vfs/hydration_e2e_test.go ================================================ //go:build vfs // +build vfs package main_test import ( "context" "fmt" "os" "os/exec" "path/filepath" "runtime" "strings" "testing" "time" "github.com/stretchr/testify/require" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/internal/testingutil" ) // TestHydration_E2E_SQLiteCLI tests hydration environment variables via the SQLite CLI. // This test builds the VFS extension and uses the actual sqlite3 CLI to verify // that LITESTREAM_HYDRATION_ENABLED and LITESTREAM_HYDRATION_PATH work correctly. func TestHydration_E2E_SQLiteCLI(t *testing.T) { if runtime.GOOS != "darwin" && runtime.GOOS != "linux" { t.Skip("skipping: test only runs on darwin or linux") } // Check if sqlite3 CLI is available if _, err := exec.LookPath("sqlite3"); err != nil { t.Skip("skipping: sqlite3 CLI not found in PATH") } // Build the VFS extension extPath := buildVFSExtension(t) // Create a file replica with test data replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupTestReplica(t, client) // Create a temp file for hydration output hydrationPath := filepath.Join(t.TempDir(), "hydrated.db") // Run sqlite3 with hydration enabled env := []string{ "LITESTREAM_REPLICA_URL=file://" + replicaDir, "LITESTREAM_HYDRATION_ENABLED=true", "LITESTREAM_HYDRATION_PATH=" + hydrationPath, "LITESTREAM_LOG_LEVEL=DEBUG", } // Query via the VFS output := runSQLiteCLI(t, extPath, env, "SELECT name FROM users WHERE id = 1;") require.Contains(t, output, "Alice", "should read data via VFS") // Verify hydration file was created require.Eventually(t, func() bool { info, err := os.Stat(hydrationPath) return err == nil && info.Size() > 0 }, 5*time.Second, 100*time.Millisecond, "hydration file should be created") } // TestHydration_E2E_SQLiteCLI_TempFile tests hydration without specifying a path (uses temp file). func TestHydration_E2E_SQLiteCLI_TempFile(t *testing.T) { if runtime.GOOS != "darwin" && runtime.GOOS != "linux" { t.Skip("skipping: test only runs on darwin or linux") } if _, err := exec.LookPath("sqlite3"); err != nil { t.Skip("skipping: sqlite3 CLI not found in PATH") } extPath := buildVFSExtension(t) replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupTestReplica(t, client) // Run without LITESTREAM_HYDRATION_PATH - should use temp file env := []string{ "LITESTREAM_REPLICA_URL=file://" + replicaDir, "LITESTREAM_HYDRATION_ENABLED=true", "LITESTREAM_LOG_LEVEL=DEBUG", } output := runSQLiteCLI(t, extPath, env, "SELECT COUNT(*) FROM users;") require.Contains(t, output, "1", "should read data via VFS with temp hydration file") } // TestHydration_E2E_SQLiteCLI_Disabled tests that hydration is disabled by default. func TestHydration_E2E_SQLiteCLI_Disabled(t *testing.T) { if runtime.GOOS != "darwin" && runtime.GOOS != "linux" { t.Skip("skipping: test only runs on darwin or linux") } if _, err := exec.LookPath("sqlite3"); err != nil { t.Skip("skipping: sqlite3 CLI not found in PATH") } extPath := buildVFSExtension(t) replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupTestReplica(t, client) hydrationPath := filepath.Join(t.TempDir(), "should-not-exist.db") // Run without LITESTREAM_HYDRATION_ENABLED env := []string{ "LITESTREAM_REPLICA_URL=file://" + replicaDir, "LITESTREAM_HYDRATION_PATH=" + hydrationPath, "LITESTREAM_LOG_LEVEL=DEBUG", } output := runSQLiteCLI(t, extPath, env, "SELECT name FROM users WHERE id = 1;") require.Contains(t, output, "Alice", "should still read data via VFS") // Hydration file should NOT be created when disabled _, err := os.Stat(hydrationPath) require.True(t, os.IsNotExist(err), "hydration file should not be created when disabled") } // TestHydration_E2E_SQLiteCLI_MultipleQueries tests that hydration persists across queries. func TestHydration_E2E_SQLiteCLI_MultipleQueries(t *testing.T) { if runtime.GOOS != "darwin" && runtime.GOOS != "linux" { t.Skip("skipping: test only runs on darwin or linux") } if _, err := exec.LookPath("sqlite3"); err != nil { t.Skip("skipping: sqlite3 CLI not found in PATH") } extPath := buildVFSExtension(t) replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupTestReplicaWithMoreData(t, client) hydrationPath := filepath.Join(t.TempDir(), "hydrated.db") env := []string{ "LITESTREAM_REPLICA_URL=file://" + replicaDir, "LITESTREAM_HYDRATION_ENABLED=true", "LITESTREAM_HYDRATION_PATH=" + hydrationPath, "LITESTREAM_LOG_LEVEL=DEBUG", } // Run multiple queries in single session queries := ` SELECT COUNT(*) FROM users; SELECT name FROM users WHERE id = 1; SELECT name FROM users WHERE id = 5; ` output := runSQLiteCLI(t, extPath, env, queries) require.Contains(t, output, "10", "should have 10 users") require.Contains(t, output, "Alice", "should find Alice") require.Contains(t, output, "User5", "should find User5") // Wait for hydration to complete require.Eventually(t, func() bool { info, err := os.Stat(hydrationPath) return err == nil && info.Size() > 0 }, 5*time.Second, 100*time.Millisecond, "hydration file should be created") } // buildVFSExtension builds the VFS extension and returns its path. func buildVFSExtension(t *testing.T) string { t.Helper() // Determine expected extension filename based on OS var extName string switch runtime.GOOS { case "darwin": extName = "litestream-vfs.dylib" case "linux": extName = "litestream-vfs.so" default: t.Fatalf("unsupported OS: %s", runtime.GOOS) } // Check if extension already exists in dist/ projectRoot := findProjectRoot(t) extPath := filepath.Join(projectRoot, "dist", extName) if _, err := os.Stat(extPath); err == nil { return extPath } // Build the extension t.Logf("building VFS extension at %s", extPath) var makeTarget string switch runtime.GOOS { case "darwin": if runtime.GOARCH == "arm64" { makeTarget = "vfs-darwin-arm64" extPath = filepath.Join(projectRoot, "dist", "litestream-vfs-darwin-arm64.dylib") } else { makeTarget = "vfs-darwin-amd64" extPath = filepath.Join(projectRoot, "dist", "litestream-vfs-darwin-amd64.dylib") } case "linux": if runtime.GOARCH == "arm64" { makeTarget = "vfs-linux-arm64" extPath = filepath.Join(projectRoot, "dist", "litestream-vfs-linux-arm64.so") } else { makeTarget = "vfs-linux-amd64" extPath = filepath.Join(projectRoot, "dist", "litestream-vfs-linux-amd64.so") } } cmd := exec.Command("make", makeTarget) cmd.Dir = projectRoot cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { t.Fatalf("failed to build VFS extension: %v", err) } return extPath } // findProjectRoot finds the project root directory. func findProjectRoot(t *testing.T) string { t.Helper() // Start from current directory and walk up dir, err := os.Getwd() require.NoError(t, err) for { if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { return dir } parent := filepath.Dir(dir) if parent == dir { t.Fatal("could not find project root (go.mod)") } dir = parent } } // runSQLiteCLI runs the sqlite3 CLI with the VFS extension and returns output. func runSQLiteCLI(t *testing.T, extPath string, env []string, query string) string { t.Helper() // Build command: sqlite3 :memory: -cmd ".load " "" args := []string{ ":memory:", "-cmd", ".load " + extPath, query, } cmd := exec.Command("sqlite3", args...) cmd.Env = append(os.Environ(), env...) output, err := cmd.CombinedOutput() if err != nil { outputStr := string(output) // Check for common extension loading failures if strings.Contains(outputStr, "Error: unknown command") || strings.Contains(outputStr, "not authorized") || strings.Contains(outputStr, "symbol not found") || strings.Contains(outputStr, "dlsym") { t.Skipf("skipping: sqlite3 cannot load extensions (common on macOS): %s", outputStr) } t.Logf("sqlite3 output: %s", outputStr) t.Fatalf("sqlite3 command failed: %v", err) } return string(output) } // setupTestReplica creates a file replica with test data. func setupTestReplica(t *testing.T, client litestream.ReplicaClient) { t.Helper() dbDir := t.TempDir() db := testingutil.NewDB(t, filepath.Join(dbDir, "source.db")) db.MonitorInterval = 100 * time.Millisecond db.Replica = litestream.NewReplica(db) db.Replica.Client = client db.Replica.SyncInterval = 100 * time.Millisecond require.NoError(t, db.Open()) sqldb := testingutil.MustOpenSQLDB(t, db.Path()) _, err := sqldb.Exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)") require.NoError(t, err) _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (1, 'Alice')") require.NoError(t, err) waitForLTXFiles(t, client, 10*time.Second, db.MonitorInterval) require.NoError(t, db.Replica.Stop(false)) testingutil.MustCloseSQLDB(t, sqldb) require.NoError(t, db.Close(context.Background())) } // setupTestReplicaWithMoreData creates a file replica with more test data. func setupTestReplicaWithMoreData(t *testing.T, client litestream.ReplicaClient) { t.Helper() dbDir := t.TempDir() db := testingutil.NewDB(t, filepath.Join(dbDir, "source.db")) db.MonitorInterval = 100 * time.Millisecond db.Replica = litestream.NewReplica(db) db.Replica.Client = client db.Replica.SyncInterval = 100 * time.Millisecond require.NoError(t, db.Open()) sqldb := testingutil.MustOpenSQLDB(t, db.Path()) _, err := sqldb.Exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)") require.NoError(t, err) // Insert 10 users _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (1, 'Alice')") require.NoError(t, err) for i := 2; i <= 10; i++ { _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (?, ?)", i, fmt.Sprintf("User%d", i)) require.NoError(t, err) } waitForLTXFiles(t, client, 10*time.Second, db.MonitorInterval) require.NoError(t, db.Replica.Stop(false)) testingutil.MustCloseSQLDB(t, sqldb) require.NoError(t, db.Close(context.Background())) } ================================================ FILE: cmd/litestream-vfs/main.go ================================================ //go:build SQLITE3VFS_LOADABLE_EXT // +build SQLITE3VFS_LOADABLE_EXT package main // import C is necessary export to the c-archive .a file /* typedef long long int sqlite3_int64; typedef unsigned long long int sqlite3_uint64; */ import "C" import ( "context" "fmt" "io" "log/slog" "os" "strings" "time" "unsafe" "github.com/psanford/sqlite3vfs" "github.com/benbjohnson/litestream" // Import all replica backends to register their URL factories. _ "github.com/benbjohnson/litestream/abs" _ "github.com/benbjohnson/litestream/file" _ "github.com/benbjohnson/litestream/gs" _ "github.com/benbjohnson/litestream/nats" _ "github.com/benbjohnson/litestream/oss" _ "github.com/benbjohnson/litestream/s3" _ "github.com/benbjohnson/litestream/sftp" _ "github.com/benbjohnson/litestream/webdav" ) func main() {} //export LitestreamVFSRegister func LitestreamVFSRegister() *C.char { var client litestream.ReplicaClient var err error replicaURL := os.Getenv("LITESTREAM_REPLICA_URL") if replicaURL == "" { return C.CString("LITESTREAM_REPLICA_URL environment variable required") } client, err = litestream.NewReplicaClientFromURL(replicaURL) if err != nil { return C.CString(fmt.Sprintf("failed to create replica client: %s", err)) } // Initialize the client. if err := client.Init(context.Background()); err != nil { return C.CString(fmt.Sprintf("failed to initialize replica client: %s", err)) } var level slog.Level switch strings.ToUpper(os.Getenv("LITESTREAM_LOG_LEVEL")) { case "DEBUG": level = slog.LevelDebug default: level = slog.LevelInfo } var logOutput io.Writer = os.Stdout if logFile := os.Getenv("LITESTREAM_LOG_FILE"); logFile != "" { f, err := os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) if err != nil { return C.CString(fmt.Sprintf("failed to open log file: %s", err)) } logOutput = f } logger := slog.New(slog.NewTextHandler(logOutput, &slog.HandlerOptions{Level: level})) vfs := litestream.NewVFS(client, logger) // Configure write support if enabled. if strings.ToLower(os.Getenv("LITESTREAM_WRITE_ENABLED")) == "true" { vfs.WriteEnabled = true if s := os.Getenv("LITESTREAM_SYNC_INTERVAL"); s != "" { d, err := time.ParseDuration(s) if err != nil { return C.CString(fmt.Sprintf("invalid LITESTREAM_SYNC_INTERVAL: %s", err)) } vfs.WriteSyncInterval = d } if s := os.Getenv("LITESTREAM_BUFFER_PATH"); s != "" { vfs.WriteBufferPath = s } } // Configure hydration support if enabled. if strings.ToLower(os.Getenv("LITESTREAM_HYDRATION_ENABLED")) == "true" { vfs.HydrationEnabled = true if s := os.Getenv("LITESTREAM_HYDRATION_PATH"); s != "" { vfs.HydrationPath = s } } if err := sqlite3vfs.RegisterVFS("litestream", vfs); err != nil { return C.CString(fmt.Sprintf("failed to register VFS: %s", err)) } return nil } //export GoLitestreamRegisterConnection func GoLitestreamRegisterConnection(dbPtr unsafe.Pointer, fileID C.sqlite3_uint64) *C.char { if err := litestream.RegisterVFSConnection(uintptr(dbPtr), uint64(fileID)); err != nil { return C.CString(err.Error()) } return nil } //export GoLitestreamUnregisterConnection func GoLitestreamUnregisterConnection(dbPtr unsafe.Pointer) *C.char { litestream.UnregisterVFSConnection(uintptr(dbPtr)) return nil } //export GoLitestreamSetTime func GoLitestreamSetTime(dbPtr unsafe.Pointer, timestamp *C.char) *C.char { if timestamp == nil { return C.CString("timestamp required") } if err := litestream.SetVFSConnectionTime(uintptr(dbPtr), C.GoString(timestamp)); err != nil { return C.CString(err.Error()) } return nil } //export GoLitestreamResetTime func GoLitestreamResetTime(dbPtr unsafe.Pointer) *C.char { if err := litestream.ResetVFSConnectionTime(uintptr(dbPtr)); err != nil { return C.CString(err.Error()) } return nil } //export GoLitestreamTime func GoLitestreamTime(dbPtr unsafe.Pointer, out **C.char) *C.char { value, err := litestream.GetVFSConnectionTime(uintptr(dbPtr)) if err != nil { return C.CString(err.Error()) } if out != nil { *out = C.CString(value) } return nil } //export GoLitestreamTxid func GoLitestreamTxid(dbPtr unsafe.Pointer, out **C.char) *C.char { value, err := litestream.GetVFSConnectionTXID(uintptr(dbPtr)) if err != nil { return C.CString(err.Error()) } if out != nil { *out = C.CString(value) } return nil } //export GoLitestreamLag func GoLitestreamLag(dbPtr unsafe.Pointer, out *C.sqlite3_int64) *C.char { value, err := litestream.GetVFSConnectionLag(uintptr(dbPtr)) if err != nil { return C.CString(err.Error()) } if out != nil { *out = C.sqlite3_int64(value) } return nil } ================================================ FILE: cmd/litestream-vfs/main_test.go ================================================ //go:build vfs // +build vfs package main_test import ( "bytes" "context" "database/sql" "errors" "fmt" "io" "log/slog" "math/rand" "os" "path/filepath" "strings" "sync" "sync/atomic" "testing" "time" sqlite3 "github.com/mattn/go-sqlite3" "github.com/psanford/sqlite3vfs" "github.com/stretchr/testify/require" "github.com/superfly/ltx" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/internal/testingutil" ) func TestVFS_Simple(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) vfs := newVFS(t, client) if err := sqlite3vfs.RegisterVFS("litestream", vfs); err != nil { t.Fatalf("failed to register litestream vfs: %v", err) } db := testingutil.NewDB(t, filepath.Join(t.TempDir(), "db")) db.MonitorInterval = 100 * time.Millisecond db.Replica = litestream.NewReplica(db) db.Replica.Client = client if err := db.Open(); err != nil { t.Fatal(err) } sqldb0 := testingutil.MustOpenSQLDB(t, db.Path()) defer testingutil.MustCloseSQLDB(t, sqldb0) if _, err := sqldb0.Exec("CREATE TABLE t (x)"); err != nil { t.Fatal(err) } if _, err := sqldb0.Exec("INSERT INTO t (x) VALUES (100)"); err != nil { t.Fatal(err) } waitForLTXFiles(t, client, 10*time.Second, db.MonitorInterval) sqldb1, err := sql.Open("sqlite3", "file:/tmp/test.db?vfs=litestream") if err != nil { t.Fatalf("failed to open database: %v", err) } defer sqldb1.Close() // Execute query - wait for value to be replicated waitForReplicaValue(t, sqldb1, "SELECT * FROM t", 100, 10*time.Second, db.MonitorInterval) } func TestVFS_Updating(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) vfs := newVFS(t, client) if err := sqlite3vfs.RegisterVFS("litestream", vfs); err != nil { t.Fatalf("failed to register litestream vfs: %v", err) } db := testingutil.NewDB(t, filepath.Join(t.TempDir(), "db")) db.MonitorInterval = 100 * time.Millisecond db.Replica = litestream.NewReplica(db) db.Replica.Client = client db.Replica.SyncInterval = 100 * time.Millisecond if err := db.Open(); err != nil { t.Fatal(err) } sqldb0 := testingutil.MustOpenSQLDB(t, db.Path()) defer testingutil.MustCloseSQLDB(t, sqldb0) t.Log("creating table") if _, err := sqldb0.Exec("CREATE TABLE t (x)"); err != nil { t.Fatal(err) } if _, err := sqldb0.Exec("INSERT INTO t (x) VALUES (100)"); err != nil { t.Fatal(err) } waitForLTXFiles(t, client, 10*time.Second, db.MonitorInterval) t.Log("opening vfs") sqldb1, err := sql.Open("sqlite3", "file:/tmp/test.db?vfs=litestream") if err != nil { t.Fatalf("failed to open database: %v", err) } defer sqldb1.Close() // Wait for initial value to replicate waitForReplicaValue(t, sqldb1, "SELECT * FROM t", 100, 10*time.Second, db.MonitorInterval) t.Log("updating source database") // Update the value from the source database. if _, err := sqldb0.Exec("UPDATE t SET x = 200"); err != nil { t.Fatal(err) } // Ensure replica has updated itself. t.Log("ensuring replica has updated") waitForReplicaValue(t, sqldb1, "SELECT * FROM t", 200, 10*time.Second, db.MonitorInterval) if err := db.Replica.Stop(false); err != nil { t.Fatalf("stop replica: %v", err) } } func TestVFS_ActiveReadTransaction(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) vfs := newVFS(t, client) if err := sqlite3vfs.RegisterVFS("litestream-txn", vfs); err != nil { t.Fatalf("failed to register litestream vfs: %v", err) } db := testingutil.NewDB(t, filepath.Join(t.TempDir(), "db")) db.MonitorInterval = 100 * time.Millisecond db.Replica = litestream.NewReplica(db) db.Replica.Client = client db.Replica.SyncInterval = 100 * time.Millisecond if err := db.Open(); err != nil { t.Fatal(err) } sqldb0 := testingutil.MustOpenSQLDB(t, db.Path()) defer testingutil.MustCloseSQLDB(t, sqldb0) // Create a table with many rows to ensure we span multiple pages // With 4KB page size, we want to ensure we're using hundreds of pages t.Log("creating table with many rows") if _, err := sqldb0.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY, data TEXT)"); err != nil { t.Fatal(err) } // Insert ~10000 rows, each with substantial data to span many pages // This should occupy at least 200+ pages (assuming ~200 bytes per row, ~20 rows per 4KB page) if _, err := sqldb0.Exec("BEGIN"); err != nil { t.Fatal(err) } for i := 0; i < 10000; i++ { data := fmt.Sprintf("initial_data_%d_padding_%s", i, string(make([]byte, 100))) if _, err := sqldb0.Exec("INSERT INTO t (id, data) VALUES (?, ?)", i, data); err != nil { t.Fatal(err) } } if _, err := sqldb0.Exec("COMMIT"); err != nil { t.Fatal(err) } // Wait for replication to sync waitForLTXFiles(t, client, 10*time.Second, db.MonitorInterval) t.Log("opening vfs replica") sqldb1, err := sql.Open("sqlite3", "file:/tmp/test-txn.db?vfs=litestream-txn") if err != nil { t.Fatalf("failed to open database: %v", err) } defer sqldb1.Close() // Start a read transaction on the replica t.Log("starting read transaction on replica") tx, err := sqldb1.Begin() if err != nil { t.Fatalf("failed to begin transaction: %v", err) } defer tx.Rollback() // Verify we can read initial data from within the transaction var initialData string if err := tx.QueryRow("SELECT data FROM t WHERE id = 5000").Scan(&initialData); err != nil { t.Fatalf("failed to query initial data: %v", err) } if !strings.HasPrefix(initialData, "initial_data_5000") { t.Fatalf("unexpected initial data: %s", initialData) } t.Log("updating source database with many affected pages") // Update many rows in the source database to affect many pages if _, err := sqldb0.Exec("BEGIN"); err != nil { t.Fatal(err) } for i := 0; i < 10000; i++ { data := fmt.Sprintf("updated_data_%d_padding_%s", i, string(make([]byte, 100))) if _, err := sqldb0.Exec("UPDATE t SET data = ? WHERE id = ?", data, i); err != nil { t.Fatal(err) } } if _, err := sqldb0.Exec("COMMIT"); err != nil { t.Fatal(err) } // Wait for replication to sync the updates - verify new data is available // by checking from a fresh connection (not the transaction's snapshot) t.Log("waiting for replication sync") require.Eventually(t, func() bool { var data string // Use a new query to check if updates have replicated if err := sqldb1.QueryRow("SELECT data FROM t WHERE id = 5000").Scan(&data); err != nil { return false } return strings.HasPrefix(data, "updated_data_5000") }, 10*time.Second, db.MonitorInterval, "updates should replicate") // The active read transaction should still see old data (snapshot isolation) t.Log("verifying read transaction still sees old data") var txData string if err := tx.QueryRow("SELECT data FROM t WHERE id = 5000").Scan(&txData); err != nil { t.Fatalf("failed to query within transaction: %v", err) } if !strings.HasPrefix(txData, "initial_data_5000") { t.Fatalf("transaction should see old data, got: %s", txData) } // Commit the read transaction t.Log("committing read transaction") if err := tx.Commit(); err != nil { t.Fatalf("failed to commit transaction: %v", err) } // Verify multiple rows across different pages t.Log("verifying multiple rows across pages") for _, id := range []int{0, 2500, 5000, 7500, 9999} { var data string if err := sqldb1.QueryRow("SELECT data FROM t WHERE id = ?", id).Scan(&data); err != nil { t.Fatalf("failed to query id %d: %v", id, err) } expected := fmt.Sprintf("updated_data_%d", id) if !strings.HasPrefix(data, expected) { t.Fatalf("id %d: expected prefix %s, got: %s", id, expected, data) } } } func TestVFS_PollsL1Files(t *testing.T) { ctx := context.Background() client := file.NewReplicaClient(t.TempDir()) // Create and populate source database db := testingutil.NewDB(t, filepath.Join(t.TempDir(), "db")) db.MonitorInterval = 100 * time.Millisecond db.Replica = litestream.NewReplica(db) db.Replica.Client = client db.Replica.SyncInterval = 100 * time.Millisecond db.Replica.MonitorEnabled = false // Create a store to handle compaction levels := litestream.CompactionLevels{ {Level: 0}, {Level: 1, Interval: 1 * time.Second}, } store := litestream.NewStore([]*litestream.DB{db}, levels) store.CompactionMonitorEnabled = false if err := store.Open(ctx); err != nil { t.Fatal(err) } defer store.Close(ctx) sqldb0 := testingutil.MustOpenSQLDB(t, db.Path()) defer testingutil.MustCloseSQLDB(t, sqldb0) // Create table and insert data t.Log("creating table with data") if _, err := sqldb0.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY, data TEXT)"); err != nil { t.Fatal(err) } // Insert multiple transactions to create several L0 files for i := 0; i < 5; i++ { if _, err := sqldb0.Exec("INSERT INTO t (data) VALUES (?)", fmt.Sprintf("value-%d", i)); err != nil { t.Fatal(err) } if err := db.Sync(ctx); err != nil { t.Fatal(err) } if err := db.Replica.Sync(ctx); err != nil { t.Fatal(err) } time.Sleep(50 * time.Millisecond) // Small delay between transactions } t.Log("compacting to L1") // Compact L0 files to L1 if _, err := store.CompactDB(ctx, db, levels[1]); err != nil { t.Fatalf("failed to compact to L1: %v", err) } // Verify L1 files exist itr, err := client.LTXFiles(ctx, 1, 0, false) if err != nil { t.Fatal(err) } var l1Count int for itr.Next() { l1Count++ } itr.Close() if l1Count == 0 { t.Fatal("expected L1 files to exist after compaction") } t.Logf("found %d L1 file(s)", l1Count) // Register VFS vfs := newVFS(t, client) if err := sqlite3vfs.RegisterVFS("litestream-l1", vfs); err != nil { t.Fatalf("failed to register litestream vfs: %v", err) } // Open database through VFS t.Log("opening vfs") sqldb1, err := sql.Open("sqlite3", "file:/tmp/test-l1.db?vfs=litestream-l1") if err != nil { t.Fatalf("failed to open database: %v", err) } defer sqldb1.Close() // Query to ensure data is readable var count int if err := sqldb1.QueryRow("SELECT COUNT(*) FROM t").Scan(&count); err != nil { t.Fatalf("failed to query database: %v", err) } else if got, want := count, 5; got != want { t.Fatalf("got %d rows, want %d", got, want) } // Get the VFS file to check maxTXID1 // The VFS creates the file when opened, we need to access it // Since VFS.Open returns the file, we need to track it // For now, let's add more data and wait for polling t.Log("adding more data to source") // Add more data to L0 to trigger polling for i := 5; i < 10; i++ { if _, err := sqldb0.Exec("INSERT INTO t (data) VALUES (?)", fmt.Sprintf("value-%d", i)); err != nil { t.Fatal(err) } if err := db.Sync(ctx); err != nil { t.Fatal(err) } if err := db.Replica.Sync(ctx); err != nil { t.Fatal(err) } } // Close and reopen the VFS connection to see updates // (VFS is designed for read replicas where clients open new connections) sqldb1.Close() t.Log("reopening vfs to see updates") sqldb1, err = sql.Open("sqlite3", "file:/tmp/test-l1.db?vfs=litestream-l1") if err != nil { t.Fatalf("failed to reopen database: %v", err) } defer sqldb1.Close() // Wait for VFS to poll new files t.Log("waiting for VFS to poll") require.Eventually(t, func() bool { if err := sqldb1.QueryRow("SELECT COUNT(*) FROM t").Scan(&count); err != nil { return false } return count == 10 }, 10*time.Second, vfs.PollInterval, "VFS should poll and see 10 rows") // Compact the new L0 files to L1 // Use Eventually since compaction has a 1-second interval and first compaction just completed t.Log("compacting new data to L1") require.Eventually(t, func() bool { _, err := store.CompactDB(ctx, db, levels[1]) return err == nil }, 5*time.Second, 200*time.Millisecond, "second compaction should succeed after interval passes") // At this point, the VFS should have polled L1 files // We can't directly access the VFSFile from here without modifying VFS.Open // But we can verify the data is readable, which proves L1 files are being used // Query a specific value to ensure L1 data is accessible var data string if err := sqldb1.QueryRow("SELECT data FROM t WHERE id = 7").Scan(&data); err != nil { t.Fatalf("failed to query specific row: %v", err) } else if got, want := data, "value-6"; got != want { t.Fatalf("got data %q, want %q", got, want) } t.Log("L1 file polling verified successfully") } func TestVFS_LongRunningTxnStress(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) vfs := newVFS(t, client) vfs.PollInterval = 25 * time.Millisecond vfsName := registerTestVFS(t, vfs) _, primary := openReplicatedPrimary(t, client, 25*time.Millisecond, 25*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE metrics (id INTEGER PRIMARY KEY, value INTEGER)"); err != nil { t.Fatalf("create table: %v", err) } if _, err := primary.Exec("INSERT INTO metrics (id, value) VALUES (1, 0)"); err != nil { t.Fatalf("insert row: %v", err) } replica := openVFSReplicaDB(t, vfsName) defer replica.Close() require.Eventually(t, func() bool { var tmp int return replica.QueryRow("SELECT value FROM metrics WHERE id = 1").Scan(&tmp) == nil }, 30*time.Second, 50*time.Millisecond, "replica should observe metrics row") tx, err := replica.Begin() if err != nil { t.Fatalf("begin replica txn: %v", err) } defer tx.Rollback() var initialValue int if err := tx.QueryRow("SELECT value FROM metrics WHERE id = 1").Scan(&initialValue); err != nil { t.Fatalf("initial read: %v", err) } ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() writerDone := make(chan error, 1) go func() { defer close(writerDone) value := 0 for { select { case <-ctx.Done(): return default: } value++ if _, err := primary.Exec("UPDATE metrics SET value = ? WHERE id = 1", value); err != nil { writerDone <- err return } time.Sleep(10 * time.Millisecond) } }() for { select { case <-ctx.Done(): if err := <-writerDone; err != nil && !errors.Is(err, context.Canceled) { t.Fatalf("writer error: %v", err) } goto done case <-time.After(50 * time.Millisecond): var v int if err := tx.QueryRow("SELECT value FROM metrics WHERE id = 1").Scan(&v); err != nil { t.Fatalf("read during txn: %v", err) } if v != initialValue { t.Fatalf("long-running txn observed change: got %d want %d", v, initialValue) } } } done: if err := tx.Commit(); err != nil { t.Fatalf("commit: %v", err) } var finalValue int if err := replica.QueryRow("SELECT value FROM metrics WHERE id = 1").Scan(&finalValue); err != nil { t.Fatalf("post-commit read: %v", err) } if finalValue == initialValue { t.Fatalf("expected updated value after commit") } } func TestVFS_HighLoadConcurrentReads(t *testing.T) { if testing.Short() { t.Skip("skipping high-load test in short mode") } client := file.NewReplicaClient(t.TempDir()) vfs := newVFS(t, client) vfs.PollInterval = 50 * time.Millisecond vfsName := registerTestVFS(t, vfs) _, primary := openReplicatedPrimary(t, client, 50*time.Millisecond, 50*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec(`CREATE TABLE t ( id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT, updated_at INTEGER )`); err != nil { t.Fatalf("create table: %v", err) } seedLargeTable(t, primary, 2000) replica := openVFSReplicaDB(t, vfsName) defer replica.Close() if _, err := replica.Exec("PRAGMA temp_store = MEMORY"); err != nil { t.Fatalf("set temp_store: %v", err) } waitForReplicaRowCount(t, primary, replica, 30*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() var writerOps atomic.Int64 writerErr := make(chan error, 1) go func() { defer close(writerErr) rnd := rand.New(rand.NewSource(time.Now().UnixNano())) for { select { case <-ctx.Done(): writerErr <- nil return default: } switch rnd.Intn(3) { case 0: if _, err := primary.Exec("INSERT INTO t (value, updated_at) VALUES (?, strftime('%s','now'))", fmt.Sprintf("value-%d", rnd.Int())); err != nil { writerErr <- err return } case 1: if _, err := primary.Exec("UPDATE t SET value = value || '-u' WHERE id IN (SELECT id FROM t ORDER BY RANDOM() LIMIT 1)"); err != nil { writerErr <- err return } default: if _, err := primary.Exec("DELETE FROM t WHERE id IN (SELECT id FROM t ORDER BY RANDOM() LIMIT 1)"); err != nil { writerErr <- err return } } writerOps.Add(1) time.Sleep(time.Duration(rnd.Intn(5)+1) * time.Millisecond) } }() readerErrCh := make(chan error, 1) var readerWg sync.WaitGroup for i := 0; i < 8; i++ { readerWg.Add(1) go func(id int) { defer readerWg.Done() for { select { case <-ctx.Done(): return default: } var count int var totalBytes int if err := replica.QueryRow("SELECT COUNT(*), IFNULL(SUM(LENGTH(value)), 0) FROM t").Scan(&count, &totalBytes); err != nil { readerErrCh <- fmt.Errorf("reader %d query: %w", id, err) return } if count < 0 || totalBytes < 0 { readerErrCh <- fmt.Errorf("reader %d observed invalid stats", id) return } } }(i) } <-ctx.Done() readerWg.Wait() if err := <-writerErr; err != nil && !errors.Is(err, context.Canceled) { t.Fatalf("writer error: %v", err) } select { case err := <-readerErrCh: if err != nil { t.Fatalf("reader error: %v", err) } default: } if ops := writerOps.Load(); ops < 100 { t.Fatalf("expected high write volume, got %d ops", ops) } waitForReplicaRowCount(t, primary, replica, 30*time.Second) var primaryCount, replicaCount int if err := primary.QueryRow("SELECT COUNT(*) FROM t").Scan(&primaryCount); err != nil { t.Fatalf("primary count: %v", err) } if err := replica.QueryRow("SELECT COUNT(*) FROM t").Scan(&replicaCount); err != nil { t.Fatalf("replica count: %v", err) } if primaryCount != replicaCount { t.Fatalf("replica lagging: primary=%d replica=%d", primaryCount, replicaCount) } } // TestVFS_OverlappingTransactionCommitStorm tests that the VFS can handle // concurrent read operations while writes are happening on the primary. // The test verifies that the replica eventually catches up with the primary. func TestVFS_OverlappingTransactionCommitStorm(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) const interval = 25 * time.Millisecond db, primary := openReplicatedPrimary(t, client, interval, interval) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE ledger (id INTEGER PRIMARY KEY AUTOINCREMENT, account INTEGER, amount INTEGER, created_at INTEGER)"); err != nil { t.Fatalf("create table: %v", err) } if _, err := primary.Exec("INSERT INTO ledger (account, amount, created_at) VALUES (1, 0, strftime('%s','now'))"); err != nil { t.Fatalf("seed ledger: %v", err) } // Wait for LTX files to be created before opening VFS replica require.Eventually(t, func() bool { itr, err := client.LTXFiles(context.Background(), 0, 0, false) if err != nil { return false } defer itr.Close() return itr.Next() }, 5*time.Second, db.MonitorInterval, "LTX files should be created") forceReplicaSync(t, db) vfs := newVFS(t, client) vfs.PollInterval = interval vfsName := registerTestVFS(t, vfs) replica := openVFSReplicaDB(t, vfsName) defer replica.Close() // Verify initial sync require.Eventually(t, func() bool { var primaryCount int if err := primary.QueryRow("SELECT COUNT(*) FROM ledger").Scan(&primaryCount); err != nil { return false } var replicaCount int if err := replica.QueryRow("SELECT COUNT(*) FROM ledger").Scan(&replicaCount); err != nil { return false } return primaryCount == replicaCount }, time.Minute, 25*time.Millisecond, "ledger counts should match initially") // Run concurrent writers for a short period (reduced from 10s to 3s) ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() var writerWG sync.WaitGroup writer := func(account int) { defer writerWG.Done() rnd := rand.New(rand.NewSource(time.Now().UnixNano() + int64(account))) for { select { case <-ctx.Done(): return default: } amount := rnd.Intn(200) - 100 if _, err := primary.Exec("BEGIN IMMEDIATE"); err != nil { continue } if _, err := primary.Exec("INSERT INTO ledger (account, amount, created_at) VALUES (?, ?, strftime('%s','now'))", account, amount); err != nil { primary.Exec("ROLLBACK") continue } if _, err := primary.Exec("COMMIT"); err != nil { primary.Exec("ROLLBACK") continue } // Slow down writes to allow background monitor to keep up time.Sleep(time.Duration(rnd.Intn(20)+10) * time.Millisecond) } } writerWG.Add(2) go writer(1) go writer(2) // Run concurrent reader that verifies count never goes to zero readerCtx, readerCancel := context.WithCancel(ctx) readerErr := make(chan error, 1) var readerWG sync.WaitGroup readerWG.Add(1) go func() { defer readerWG.Done() for { select { case <-readerCtx.Done(): return default: } var count int if err := replica.QueryRow("SELECT COUNT(*) FROM ledger").Scan(&count); err != nil { readerErr <- err return } if count == 0 { readerErr <- fmt.Errorf("ledger count went to zero") return } time.Sleep(25 * time.Millisecond) } }() <-ctx.Done() readerCancel() writerWG.Wait() readerWG.Wait() // Check for reader errors select { case err := <-readerErr: if err != nil { t.Fatalf("reader error: %v", err) } default: } // Force final sync and wait for replica to catch up forceReplicaSync(t, db) require.Eventually(t, func() bool { var primaryCount int if err := primary.QueryRow("SELECT COUNT(*) FROM ledger").Scan(&primaryCount); err != nil { return false } var replicaCount int if err := replica.QueryRow("SELECT COUNT(*) FROM ledger").Scan(&replicaCount); err != nil { return false } return primaryCount == replicaCount }, 30*time.Second, 100*time.Millisecond, "ledger counts should match after writer done") } func TestVFS_CacheMissStorm(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) const interval = 20 * time.Millisecond _, primary := openReplicatedPrimary(t, client, interval, interval) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE stats (id INTEGER PRIMARY KEY, payload TEXT)"); err != nil { t.Fatalf("create table: %v", err) } for i := 0; i < 1000; i++ { if _, err := primary.Exec("INSERT INTO stats (payload) VALUES (?)", fmt.Sprintf("row-%d", i)); err != nil { t.Fatalf("insert payload: %v", err) } } vfs := newVFS(t, client) vfs.PollInterval = interval vfsName := registerTestVFS(t, vfs) replica := openVFSReplicaDB(t, vfsName) defer replica.Close() waitForTableRowCount(t, primary, replica, "stats", 30*time.Second) if _, err := replica.Exec("PRAGMA cache_size = -64"); err != nil { t.Fatalf("set cache_size: %v", err) } if _, err := replica.Exec("PRAGMA cache_spill = ON"); err != nil { t.Fatalf("enable cache_spill: %v", err) } for i := 0; i < 100; i++ { var maxID int if err := replica.QueryRow("SELECT MAX(id) FROM stats").Scan(&maxID); err != nil { t.Fatalf("cache-miss query: %v", err) } if maxID == 0 { t.Fatalf("unexpected empty stats table") } } } func BenchmarkVFS_LargeDatabase(b *testing.B) { if testing.Short() { b.Skip("skipping large benchmark in short mode") } client := file.NewReplicaClient(b.TempDir()) db, primary := openReplicatedPrimary(b, client, 25*time.Millisecond, 25*time.Millisecond) b.Cleanup(func() { testingutil.MustCloseSQLDB(b, primary) }) if _, err := primary.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT, updated_at INTEGER)"); err != nil { b.Fatalf("create table: %v", err) } seedLargeTable(b, primary, 20000) forceReplicaSync(b, db) if err := db.Replica.Stop(false); err != nil { b.Fatalf("stop replica: %v", err) } vfs := newVFS(b, client) vfs.PollInterval = 25 * time.Millisecond vfsName := registerTestVFS(b, vfs) replica := openVFSReplicaDB(b, vfsName) b.Cleanup(func() { replica.Close() }) waitForReplicaRowCount(b, primary, replica, 30*time.Second) b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { var count, totalBytes int if err := replica.QueryRow("SELECT COUNT(*), IFNULL(SUM(LENGTH(value)), 0) FROM t").Scan(&count, &totalBytes); err != nil { b.Fatalf("benchmark query: %v", err) } } } func TestVFS_NetworkLatencySensitivity(t *testing.T) { client := &latencyReplicaClient{ReplicaClient: file.NewReplicaClient(t.TempDir()), delay: 10 * time.Millisecond} vfs := newVFS(t, client) vfs.PollInterval = 25 * time.Millisecond vfsName := registerTestVFS(t, vfs) _, primary := openReplicatedPrimary(t, client, 25*time.Millisecond, 25*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE logs (id INTEGER PRIMARY KEY, value TEXT)"); err != nil { t.Fatalf("create table: %v", err) } if _, err := primary.Exec("INSERT INTO logs (value) VALUES ('ok')"); err != nil { t.Fatalf("insert row: %v", err) } replica := openVFSReplicaDB(t, vfsName) defer replica.Close() require.Eventually(t, func() bool { var count int if err := replica.QueryRow("SELECT COUNT(*) FROM logs").Scan(&count); err != nil { return false } return count == 1 }, 10*time.Second, 50*time.Millisecond, "replica should observe log row under injected latency") } func TestVFS_ConcurrentConnectionScaling(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) vfs := newVFS(t, client) vfs.PollInterval = 25 * time.Millisecond vfsName := registerTestVFS(t, vfs) db, primary := openReplicatedPrimary(t, client, 25*time.Millisecond, 25*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE metrics (id INTEGER PRIMARY KEY AUTOINCREMENT, value INTEGER)"); err != nil { t.Fatalf("create table: %v", err) } for i := 0; i < 1000; i++ { if _, err := primary.Exec("INSERT INTO metrics (value) VALUES (?)", i); err != nil { t.Fatalf("insert row: %v", err) } } // Wait for LTX files to be created before forceReplicaSync require.Eventually(t, func() bool { itr, err := client.LTXFiles(context.Background(), 0, 0, false) if err != nil { return false } defer itr.Close() return itr.Next() }, 5*time.Second, db.MonitorInterval, "LTX files should be created") forceReplicaSync(t, db) const connCount = 32 conns := make([]*sql.DB, connCount) for i := 0; i < connCount; i++ { conns[i] = openVFSReplicaDB(t, vfsName) } defer func() { for _, c := range conns { c.Close() } }() ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() var wg sync.WaitGroup for idx := range conns { wg.Add(1) go func(id int, dbConn *sql.DB) { defer wg.Done() for { select { case <-ctx.Done(): return default: } var min, max int if err := dbConn.QueryRow("SELECT MIN(value), MAX(value) FROM metrics").Scan(&min, &max); err != nil { t.Errorf("conn %d query: %v", id, err) return } } }(idx, conns[idx]) } wg.Wait() if err := ctx.Err(); err != context.Canceled && err != context.DeadlineExceeded { t.Fatalf("unexpected context err: %v", err) } } func TestVFS_PRAGMAQueryBehavior(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) vfs := newVFS(t, client) vfs.PollInterval = 25 * time.Millisecond vfsName := registerTestVFS(t, vfs) db, primary := openReplicatedPrimary(t, client, 25*time.Millisecond, 25*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE configs (id INTEGER PRIMARY KEY, name TEXT)"); err != nil { t.Fatalf("create table: %v", err) } if _, err := primary.Exec("INSERT INTO configs (name) VALUES ('ok')"); err != nil { t.Fatalf("insert row: %v", err) } if _, err := primary.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY, value TEXT)"); err != nil { t.Fatalf("create table t: %v", err) } if _, err := primary.Exec("INSERT INTO t (value) VALUES ('seed')"); err != nil { t.Fatalf("seed t: %v", err) } // Wait for LTX files to be created before forceReplicaSync require.Eventually(t, func() bool { itr, err := client.LTXFiles(context.Background(), 0, 0, false) if err != nil { return false } defer itr.Close() return itr.Next() }, 5*time.Second, db.MonitorInterval, "LTX files should be created") forceReplicaSync(t, db) replica := openVFSReplicaDB(t, vfsName) defer replica.Close() waitForReplicaRowCount(t, primary, replica, 30*time.Second) var journalMode string if err := replica.QueryRow("PRAGMA journal_mode").Scan(&journalMode); err != nil { t.Fatalf("read journal_mode: %v", err) } if strings.ToLower(journalMode) != "delete" { t.Fatalf("expected journal_mode delete, got %s", journalMode) } if _, err := replica.Exec("PRAGMA cache_size = -2048"); err != nil { t.Fatalf("set cache_size: %v", err) } var cacheSize int if err := replica.QueryRow("PRAGMA cache_size").Scan(&cacheSize); err != nil { t.Fatalf("read cache_size: %v", err) } if cacheSize != -2048 { t.Fatalf("unexpected cache_size: %d", cacheSize) } var pageSize int if err := replica.QueryRow("PRAGMA page_size").Scan(&pageSize); err != nil { t.Fatalf("read page_size: %v", err) } if pageSize != 4096 { t.Fatalf("unexpected page_size: %d", pageSize) } } func TestVFS_SortingLargeResultSet(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) vfs := newVFS(t, client) vfs.PollInterval = 50 * time.Millisecond vfsName := registerTestVFS(t, vfs) db, primary := openReplicatedPrimary(t, client, 50*time.Millisecond, 50*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec(`CREATE TABLE t ( id INTEGER PRIMARY KEY, payload TEXT NOT NULL, grp INTEGER NOT NULL )`); err != nil { t.Fatalf("create table: %v", err) } seedSortedDataset(t, primary, 25000) if err := db.Replica.Stop(false); err != nil { t.Fatalf("stop replica: %v", err) } replica := openVFSReplicaDB(t, vfsName) defer replica.Close() if _, err := replica.Exec("PRAGMA temp_store = FILE"); err != nil { t.Fatalf("set temp_store: %v", err) } if _, err := replica.Exec("PRAGMA cache_size = -2048"); err != nil { t.Fatalf("set cache_size: %v", err) } waitForReplicaRowCount(t, primary, replica, time.Minute) expected := fetchOrderedPayloads(t, primary, 500, "payload DESC, id DESC") got := fetchOrderedPayloads(t, replica, 500, "payload DESC, id DESC") if len(expected) != len(got) { t.Fatalf("unexpected result size: expected=%d got=%d", len(expected), len(got)) } for i := range expected { if expected[i] != got[i] { t.Fatalf("mismatched payload at %d: expected=%q got=%q", i, expected[i], got[i]) } } } func TestVFS_ConcurrentIndexAccessRaces(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) const monitorInterval = 10 * time.Millisecond _, primary := openReplicatedPrimary(t, client, monitorInterval, 10*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT, updated_at INTEGER)"); err != nil { t.Fatalf("create table: %v", err) } seedLargeTable(t, primary, 10000) vfs := newVFS(t, client) vfs.PollInterval = 15 * time.Millisecond vfsName := registerTestVFS(t, vfs) dsn := fmt.Sprintf("file:%s?vfs=%s", filepath.ToSlash(filepath.Join(t.TempDir(), "fail.db")), vfsName) replica, err := sql.Open("sqlite3", dsn) if err != nil { t.Fatalf("open replica db: %v", err) } defer replica.Close() replica.SetMaxOpenConns(4) replica.SetMaxIdleConns(4) replica.SetConnMaxIdleTime(30 * time.Second) waitForReplicaRowCount(t, primary, replica, 30*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() readerErrCh := make(chan error, 1) var readerWG sync.WaitGroup for i := 0; i < 100; i++ { readerWG.Add(1) go func(id int) { defer readerWG.Done() rnd := rand.New(rand.NewSource(int64(id) + time.Now().UnixNano())) for { select { case <-ctx.Done(): return default: } var count int var totalBytes int if err := replica.QueryRow("SELECT COUNT(*), IFNULL(SUM(LENGTH(value)), 0) FROM t").Scan(&count, &totalBytes); err != nil { select { case readerErrCh <- fmt.Errorf("reader %d: %w", id, err): default: } cancel() return } if count < 0 || totalBytes < 0 { select { case readerErrCh <- fmt.Errorf("reader %d observed invalid stats", id): default: } cancel() return } _ = rnd.Int() // exercise RNG to vary workload } }(i) } var writerOps atomic.Int64 writerErrCh := make(chan error, 1) go func() { defer close(writerErrCh) rnd := rand.New(rand.NewSource(time.Now().UnixNano())) for { select { case <-ctx.Done(): return default: } switch rnd.Intn(3) { case 0: _, err := primary.Exec("INSERT INTO t (value, updated_at) VALUES (?, strftime('%s','now'))", fmt.Sprintf("writer-%d", rnd.Int())) if err != nil { if isBusyError(err) { continue } writerErrCh <- err cancel() return } case 1: _, err := primary.Exec("UPDATE t SET value = value || '-u', updated_at = strftime('%s','now') WHERE id IN (SELECT id FROM t ORDER BY RANDOM() LIMIT 1)") if err != nil { if isBusyError(err) { continue } writerErrCh <- err cancel() return } default: _, err := primary.Exec("DELETE FROM t WHERE id IN (SELECT id FROM t ORDER BY RANDOM() LIMIT 1)") if err != nil { if isBusyError(err) { continue } writerErrCh <- err cancel() return } } writerOps.Add(1) time.Sleep(time.Duration(rnd.Intn(5)+1) * time.Millisecond) } }() <-ctx.Done() readerWG.Wait() if err := <-writerErrCh; err != nil && !errors.Is(err, context.Canceled) { t.Fatalf("writer error: %v", err) } select { case err := <-readerErrCh: if err != nil { t.Fatalf("reader error: %v", err) } default: } if ops := writerOps.Load(); ops == 0 { t.Fatalf("writer did not perform any operations") } } func TestVFS_MultiplePageSizes(t *testing.T) { pageSizes := []int{512, 1024, 2048, 4096, 8192, 16384, 32768, 65536} for _, pageSize := range pageSizes { pageSize := pageSize const monitorInterval = 50 * time.Millisecond t.Run(fmt.Sprintf("page_%d", pageSize), func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) _, primary := openReplicatedPrimary(t, client, monitorInterval, 50*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("PRAGMA journal_mode=DELETE"); err != nil { t.Fatalf("disable wal: %v", err) } if _, err := primary.Exec(fmt.Sprintf("PRAGMA page_size = %d", pageSize)); err != nil { t.Fatalf("set page size: %v", err) } if _, err := primary.Exec("VACUUM"); err != nil { t.Fatalf("vacuum: %v", err) } if _, err := primary.Exec("PRAGMA journal_mode=WAL"); err != nil { t.Fatalf("enable wal: %v", err) } if _, err := primary.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY, payload TEXT)"); err != nil { t.Fatalf("create table: %v", err) } const totalRows = 200 if _, err := primary.Exec("BEGIN"); err != nil { t.Fatalf("begin tx: %v", err) } for i := 0; i < totalRows; i++ { payload := pageSizedPayload(pageSize, i) if _, err := primary.Exec("INSERT INTO t (payload) VALUES (?)", payload); err != nil { primary.Exec("ROLLBACK") t.Fatalf("insert row %d: %v", i, err) } } if _, err := primary.Exec("COMMIT"); err != nil { t.Fatalf("commit: %v", err) } vfs := newVFS(t, client) vfs.PollInterval = 50 * time.Millisecond vfsName := registerTestVFS(t, vfs) replica := openVFSReplicaDB(t, vfsName) defer replica.Close() waitForReplicaRowCount(t, primary, replica, 30*time.Second) var replicaPageSize int if err := replica.QueryRow("PRAGMA page_size").Scan(&replicaPageSize); err != nil { t.Fatalf("read replica page size: %v", err) } if replicaPageSize != pageSize { t.Fatalf("unexpected page size: got %d want %d", replicaPageSize, pageSize) } rows, err := replica.Query("SELECT id, payload FROM t ORDER BY id") if err != nil { t.Fatalf("select rows: %v", err) } defer rows.Close() count := 0 for rows.Next() { var id int var payload string if err := rows.Scan(&id, &payload); err != nil { t.Fatalf("scan row: %v", err) } expected := pageSizedPayload(pageSize, id-1) if payload != expected { t.Fatalf("row %d mismatch: got %q want %q", id, payload, expected) } count++ } if err := rows.Err(); err != nil { t.Fatalf("rows err: %v", err) } if count != totalRows { t.Fatalf("unexpected row count: got %d want %d", count, totalRows) } }) } } func TestVFS_WaitsForInitialSnapshot(t *testing.T) { t.Run("BlocksUntilSnapshot", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) vfs := newVFS(t, client) vfs.PollInterval = 50 * time.Millisecond vfsName := registerTestVFS(t, vfs) dsn := fmt.Sprintf("file:%s?vfs=%s", filepath.ToSlash(filepath.Join(t.TempDir(), "wait.db")), vfsName) errCh := make(chan error, 1) go func() { sqldb, err := sql.Open("sqlite3", dsn) if err != nil { errCh <- fmt.Errorf("open replica: %w", err) return } defer sqldb.Close() ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() var count int if err := sqldb.QueryRowContext(ctx, "SELECT COUNT(*) FROM sqlite_master").Scan(&count); err != nil { errCh <- err return } errCh <- nil }() select { case err := <-errCh: t.Fatalf("replica should block until snapshot is available, got %v", err) case <-time.After(200 * time.Millisecond): } _, primary := openReplicatedPrimary(t, client, 25*time.Millisecond, 25*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY)"); err != nil { t.Fatalf("create table: %v", err) } if _, err := primary.Exec("INSERT INTO t (id) VALUES (1)"); err != nil { t.Fatalf("insert row: %v", err) } select { case err := <-errCh: if err != nil { t.Fatalf("replica query failed: %v", err) } case <-time.After(5 * time.Second): t.Fatal("timed out waiting for replica to observe initial snapshot") } }) } func TestVFS_StorageFailureInjection(t *testing.T) { tests := []struct { name string mode string }{ {"timeout", "timeout"}, {"server_error", "server"}, {"partial_read", "partial"}, {"corrupt_data", "corrupt"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) db, primary := openReplicatedPrimary(t, client, 50*time.Millisecond, 50*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY, value TEXT)"); err != nil { t.Fatalf("create table: %v", err) } if _, err := primary.Exec("INSERT INTO t (value) VALUES ('ok')"); err != nil { t.Fatalf("insert row: %v", err) } // Wait for LTX files to be written by background monitor require.Eventually(t, func() bool { itr, err := client.LTXFiles(context.Background(), 0, 0, false) if err != nil { return false } defer itr.Close() return itr.Next() }, 5*time.Second, db.MonitorInterval, "LTX files should be created by background monitor") forceReplicaSync(t, db) if err := db.Replica.Stop(false); err != nil { t.Fatalf("stop replica: %v", err) } vfs := newVFS(t, client) vfs.PollInterval = time.Hour vfsName := registerTestVFS(t, vfs) replicaPath := filepath.Join(t.TempDir(), fmt.Sprintf("storage-failure-%s.db", tt.name)) dsn := fmt.Sprintf("file:%s?vfs=%s", filepath.ToSlash(replicaPath), vfsName) replica, err := sql.Open("sqlite3", dsn) if err != nil { t.Fatalf("open replica db: %v", err) } defer replica.Close() replica.SetMaxOpenConns(4) replica.SetMaxIdleConns(4) replica.SetConnMaxIdleTime(30 * time.Second) if _, err := replica.Exec("PRAGMA busy_timeout = 2000"); err != nil { t.Fatalf("set busy timeout: %v", err) } injectFailure := func() { var err error switch tt.mode { case "timeout": err = context.DeadlineExceeded case "server": err = fmt.Errorf("storage error: 500 Internal Server Error") case "partial": err = io.ErrUnexpectedEOF case "corrupt": err = fmt.Errorf("corrupt data") default: err = fmt.Errorf("injected failure") } vfs.Inject(replicaPath, err) } injectFailure() var val string if err := replica.QueryRow("SELECT value FROM t").Scan(&val); err == nil { t.Fatalf("expected failure due to injected storage error") } if err := replica.QueryRow("SELECT value FROM t").Scan(&val); err != nil { t.Fatalf("second read failed: %v", err) } if val != "ok" { t.Fatalf("unexpected row value: %q", val) } }) } } func TestVFS_PartialLTXUpload(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) db, primary := openReplicatedPrimary(t, client, 25*time.Millisecond, 25*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE logs (id INTEGER PRIMARY KEY, value TEXT)"); err != nil { t.Fatalf("create table: %v", err) } if _, err := primary.Exec("INSERT INTO logs (value) VALUES ('ok')"); err != nil { t.Fatalf("insert row: %v", err) } // Wait for LTX files to be created before forceReplicaSync require.Eventually(t, func() bool { itr, err := client.LTXFiles(context.Background(), 0, 0, false) if err != nil { return false } defer itr.Close() return itr.Next() }, 5*time.Second, db.MonitorInterval, "LTX files should be created") forceReplicaSync(t, db) vfs := newVFS(t, client) vfs.PollInterval = time.Hour vfsName := registerTestVFS(t, vfs) replicaPath := filepath.Join(t.TempDir(), "partial.db") dsn := fmt.Sprintf("file:%s?vfs=%s", filepath.ToSlash(replicaPath), vfsName) replica, err := sql.Open("sqlite3", dsn) if err != nil { t.Fatalf("open replica db: %v", err) } defer replica.Close() replica.SetMaxOpenConns(8) replica.SetMaxIdleConns(8) replica.SetConnMaxIdleTime(30 * time.Second) if _, err := replica.Exec("PRAGMA busy_timeout = 2000"); err != nil { t.Fatalf("set busy timeout: %v", err) } vfs.Inject(replicaPath, io.ErrUnexpectedEOF) var val string if err := replica.QueryRow("SELECT value FROM logs").Scan(&val); err == nil { t.Fatalf("expected failure due to partial upload") } if err := replica.QueryRow("SELECT value FROM logs").Scan(&val); err != nil { t.Fatalf("second attempt should succeed: %v", err) } if val != "ok" { t.Fatalf("unexpected row value: %q", val) } } func TestVFS_S3EventualConsistency(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) _, primary := openReplicatedPrimary(t, client, 25*time.Millisecond, 25*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY, value TEXT)"); err != nil { t.Fatalf("create table: %v", err) } if _, err := primary.Exec("INSERT INTO t (value) VALUES ('visible')"); err != nil { t.Fatalf("insert row: %v", err) } eventualClient := &eventualConsistencyClient{ReplicaClient: client} vfs := newVFS(t, eventualClient) vfs.PollInterval = 25 * time.Millisecond vfsName := registerTestVFS(t, vfs) replica := openVFSReplicaDB(t, vfsName) defer replica.Close() waitForReplicaRowCount(t, primary, replica, 5*time.Second) if calls := eventualClient.calls.Load(); calls < 2 { t.Fatalf("expected multiple polls under eventual consistency, got %d", calls) } } func TestVFS_FileDescriptorBudget(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) _, primary := openReplicatedPrimary(t, client, 25*time.Millisecond, 25*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY, value TEXT)"); err != nil { t.Fatalf("create table: %v", err) } if _, err := primary.Exec("INSERT INTO t (value) VALUES ('seed')"); err != nil { t.Fatalf("insert seed: %v", err) } limited := &fdLimitedReplicaClient{ReplicaClient: client, limit: 64} vfs := newVFS(t, limited) vfs.PollInterval = 10 * time.Millisecond vfsName := registerTestVFS(t, vfs) replica := openVFSReplicaDB(t, vfsName) defer replica.Close() waitForReplicaRowCount(t, primary, replica, 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 1500*time.Millisecond) defer cancel() writerDone := make(chan error, 1) go func() { defer close(writerDone) rnd := rand.New(rand.NewSource(time.Now().UnixNano())) for { select { case <-ctx.Done(): return default: } if _, err := primary.Exec("INSERT INTO t (value) VALUES (?)", fmt.Sprintf("v-%d", rnd.Int())); err != nil { if isBusyError(err) { time.Sleep(2 * time.Millisecond) continue } writerDone <- err return } time.Sleep(20 * time.Millisecond) } }() const readers = 8 errs := make(chan error, readers) for i := 0; i < readers; i++ { go func() { for { select { case <-ctx.Done(): errs <- nil return default: } var count int if err := replica.QueryRow("SELECT COUNT(*) FROM t").Scan(&count); err != nil { if isBusyError(err) { time.Sleep(2 * time.Millisecond) continue } errs <- err return } } }() } <-ctx.Done() for i := 0; i < readers; i++ { if err := <-errs; err != nil { t.Fatalf("reader %d error: %T %v", i, err, err) } } if err := <-writerDone; err != nil && !errors.Is(err, context.Canceled) { t.Fatalf("writer error: %v", err) } deadline := time.After(250 * time.Millisecond) for limited.open.Load() != 0 { select { case <-deadline: t.Fatalf("descriptor leak: %d handles still open", limited.open.Load()) case <-time.After(10 * time.Millisecond): } } } func TestVFS_PageIndexOOM(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) _, primary := openReplicatedPrimary(t, client, 25*time.Millisecond, 25*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY, value TEXT)"); err != nil { t.Fatalf("create table: %v", err) } if _, err := primary.Exec("INSERT INTO t (value) VALUES ('ok')"); err != nil { t.Fatalf("insert row: %v", err) } for i := 0; i < 64; i++ { payload := strings.Repeat("p", 3500) if _, err := primary.Exec("INSERT INTO t (value) VALUES (?)", payload); err != nil { t.Fatalf("bulk insert: %v", err) } } oomClient := &oomPageIndexClient{ReplicaClient: client} vfs := newVFS(t, oomClient) vfs.PollInterval = 20 * time.Millisecond vfsName := registerTestVFS(t, vfs) dsn := fmt.Sprintf("file:%s?vfs=%s", filepath.ToSlash(filepath.Join(t.TempDir(), "oom.db")), vfsName) failing, err := sql.Open("sqlite3", dsn) if err != nil { t.Fatalf("open replica db: %v", err) } defer failing.Close() failing.SetMaxOpenConns(4) failing.SetMaxIdleConns(4) oomClient.failNext.Store(true) var count int if err := failing.QueryRow("SELECT COUNT(*) FROM t").Scan(&count); err == nil { t.Fatalf("expected query to fail due to page index OOM") } if !oomClient.triggered.Load() { t.Fatalf("page index client never triggered") } oomClient.failNext.Store(false) replica := openVFSReplicaDB(t, vfsName) defer replica.Close() waitForReplicaRowCount(t, primary, replica, 5*time.Second) if err := replica.QueryRow("SELECT COUNT(*) FROM t").Scan(&count); err != nil { t.Fatalf("post-oom read failed: %v", err) } var expected int if err := primary.QueryRow("SELECT COUNT(*) FROM t").Scan(&expected); err != nil { t.Fatalf("primary count: %v", err) } if count != expected { t.Fatalf("unexpected row count: got %d want %d", count, expected) } } func TestVFS_PageIndexCorruptionRecovery(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) _, primary := openReplicatedPrimary(t, client, 25*time.Millisecond, 25*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY, value TEXT)"); err != nil { t.Fatalf("create table: %v", err) } if _, err := primary.Exec("INSERT INTO t (value) VALUES ('ok')"); err != nil { t.Fatalf("insert row: %v", err) } corruptClient := &corruptingPageIndexClient{ReplicaClient: client} vfs := newVFS(t, corruptClient) vfs.PollInterval = 20 * time.Millisecond vfsName := registerTestVFS(t, vfs) dsn := fmt.Sprintf("file:%s?vfs=%s", filepath.ToSlash(filepath.Join(t.TempDir(), "corrupt.db")), vfsName) corruptClient.corruptNext.Store(true) badConn, err := sql.Open("sqlite3", dsn) if err != nil { t.Fatalf("open corrupt replica: %v", err) } badConn.SetMaxOpenConns(8) badConn.SetMaxIdleConns(8) badConn.SetConnMaxIdleTime(30 * time.Second) var count int if err := badConn.QueryRow("SELECT COUNT(*) FROM t").Scan(&count); err == nil { badConn.Close() t.Fatalf("expected corruption failure") } badConn.Close() if !corruptClient.triggered.Load() { t.Fatalf("corruption hook never triggered") } goodConn := openVFSReplicaDB(t, vfsName) defer goodConn.Close() if err := goodConn.QueryRow("SELECT COUNT(*) FROM t").Scan(&count); err != nil { t.Fatalf("post-corruption read failed: %v", err) } if count != 1 { t.Fatalf("unexpected row count after recovery: %d", count) } } func TestVFS_RapidUpdateCoalescing(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) const interval = 5 * time.Millisecond _, primary := openReplicatedPrimary(t, client, interval, interval) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE metrics (id INTEGER PRIMARY KEY, value INTEGER)"); err != nil { t.Fatalf("create table: %v", err) } if _, err := primary.Exec("INSERT INTO metrics (id, value) VALUES (1, 0)"); err != nil { t.Fatalf("insert row: %v", err) } vfs := newVFS(t, client) vfs.PollInterval = interval vfsName := registerTestVFS(t, vfs) replica := openVFSReplicaDB(t, vfsName) defer replica.Close() const updates = 200 writerDone := make(chan struct{}) go func() { defer close(writerDone) for i := 1; i <= updates; i++ { if _, err := primary.Exec("UPDATE metrics SET value = ? WHERE id = 1", i); err != nil { return } time.Sleep(time.Millisecond) } }() require.Eventually(t, func() bool { var value int if err := replica.QueryRow("SELECT value FROM metrics WHERE id = 1").Scan(&value); err != nil { return false } return value == updates }, 3*time.Second, 5*time.Millisecond, "replica should observe final value") <-writerDone var value int if err := replica.QueryRow("SELECT value FROM metrics WHERE id = 1").Scan(&value); err != nil { t.Fatalf("final read: %v", err) } if value != updates { t.Fatalf("unexpected final value: got %d want %d", value, updates) } } func TestVFS_NonContiguousTXIDGapFailsOnOpen(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) for txID := ltx.TXID(1); txID <= 4; txID++ { writeSinglePageLTXFile(t, client, txID, byte('a'+int(txID))) } missing := client.LTXFilePath(0, 2, 2) if err := os.Remove(missing); err != nil { t.Fatalf("remove ltx file: %v", err) } fileLogger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{Level: slog.LevelError})) f := litestream.NewVFSFile(client, "gap.db", fileLogger) f.PollInterval = 25 * time.Millisecond if err := f.Open(); err == nil { t.Fatalf("expected open to fail after removing %s", filepath.Base(missing)) } else if errMsg := err.Error(); !strings.Contains(errMsg, "non-contiguous") { t.Fatalf("unexpected error: %v", err) } } func TestVFS_PollingThreadRecoversFromLTXListFailure(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) flakyClient := &flakyLTXClient{ReplicaClient: client} const monitorInterval = 25 * time.Millisecond _, primary := openReplicatedPrimary(t, client, monitorInterval, monitorInterval) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY, value TEXT)"); err != nil { t.Fatalf("create table: %v", err) } if _, err := primary.Exec("INSERT INTO t (value) VALUES ('seed')"); err != nil { t.Fatalf("insert seed: %v", err) } vfs := newVFS(t, flakyClient) vfs.PollInterval = 25 * time.Millisecond vfsName := registerTestVFS(t, vfs) replica := openVFSReplicaDB(t, vfsName) defer replica.Close() waitForReplicaRowCount(t, primary, replica, 10*time.Second) flakyClient.failNext.Store(true) if _, err := primary.Exec("INSERT INTO t (value) VALUES ('after-failure')"); err != nil { t.Fatalf("insert post-failure: %v", err) } waitForReplicaRowCount(t, primary, replica, 10*time.Second) if flakyClient.failures.Load() == 0 { t.Fatalf("expected at least one LTXFiles failure") } var primaryCount, replicaCount int if err := primary.QueryRow("SELECT COUNT(*) FROM t").Scan(&primaryCount); err != nil { t.Fatalf("primary count: %v", err) } if err := replica.QueryRow("SELECT COUNT(*) FROM t").Scan(&replicaCount); err != nil { t.Fatalf("replica count: %v", err) } if primaryCount != replicaCount { t.Fatalf("replica did not catch up after failure: primary=%d replica=%d", primaryCount, replicaCount) } } func TestVFS_PollIntervalEdgeCases(t *testing.T) { tests := []struct { name string interval time.Duration minCalls int64 maxCalls int64 }{ {"fast", 5 * time.Millisecond, 10, 500}, {"slow", 200 * time.Millisecond, 1, 10}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) obs := &observingReplicaClient{ReplicaClient: client} _, primary := openReplicatedPrimary(t, obs, tt.interval, tt.interval) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY, value INTEGER)"); err != nil { t.Fatalf("create table: %v", err) } vfs := newVFS(t, obs) vfs.PollInterval = tt.interval vfsName := registerTestVFS(t, vfs) replica := openVFSReplicaDB(t, vfsName) defer replica.Close() start := obs.ltxCalls.Load() time.Sleep(750 * time.Millisecond) delta := obs.ltxCalls.Load() - start if delta < tt.minCalls { t.Fatalf("expected at least %d polls, got %d", tt.minCalls, delta) } if tt.maxCalls > 0 && delta > tt.maxCalls { t.Fatalf("expected at most %d polls, got %d", tt.maxCalls, delta) } }) } } func TestVFS_PooledWriteNoFalseConflict(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) db, primary := openReplicatedPrimary(t, client, 25*time.Millisecond, 25*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE seed (id INTEGER PRIMARY KEY)"); err != nil { t.Fatalf("create seed table: %v", err) } if _, err := primary.Exec("INSERT INTO seed (id) VALUES (1)"); err != nil { t.Fatalf("insert seed: %v", err) } require.Eventually(t, func() bool { itr, err := client.LTXFiles(context.Background(), 0, 0, false) if err != nil { return false } defer itr.Close() return itr.Next() }, 5*time.Second, db.MonitorInterval, "LTX files should be created") forceReplicaSync(t, db) vfs := newVFS(t, client) vfs.WriteEnabled = true vfs.WriteSyncInterval = 0 vfs.PollInterval = 25 * time.Millisecond vfsName := registerTestVFS(t, vfs) dsn := fmt.Sprintf("file:%s?vfs=%s", filepath.ToSlash(filepath.Join(t.TempDir(), "pooled-write.db")), vfsName) sqldb, err := sql.Open("sqlite3", dsn) if err != nil { t.Fatalf("open write db: %v", err) } defer sqldb.Close() sqldb.SetMaxOpenConns(2) sqldb.SetMaxIdleConns(2) if _, err := sqldb.Exec("PRAGMA busy_timeout = 5000"); err != nil { t.Fatalf("set busy timeout: %v", err) } if _, err := sqldb.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY, value TEXT)"); err != nil { t.Fatalf("create table: %v", err) } const totalWrites = 20 for i := 1; i <= totalWrites; i++ { if _, err := sqldb.Exec("INSERT INTO t (id, value) VALUES (?, ?)", i, fmt.Sprintf("row-%d", i)); err != nil { if strings.Contains(err.Error(), "conflict") || errors.Is(err, litestream.ErrConflict) { t.Fatalf("false ErrConflict on write %d: %v", i, err) } t.Fatalf("write %d failed: %v", i, err) } } var count int if err := sqldb.QueryRow("SELECT COUNT(*) FROM t").Scan(&count); err != nil { t.Fatalf("count rows: %v", err) } if count != totalWrites { t.Fatalf("expected %d rows, got %d", totalWrites, count) } } func TestVFS_PooledWriteStress(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) db, primary := openReplicatedPrimary(t, client, 25*time.Millisecond, 25*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT)"); err != nil { t.Fatalf("create table: %v", err) } if _, err := primary.Exec("INSERT INTO t (value) VALUES ('seed')"); err != nil { t.Fatalf("insert seed: %v", err) } require.Eventually(t, func() bool { itr, err := client.LTXFiles(context.Background(), 0, 0, false) if err != nil { return false } defer itr.Close() return itr.Next() }, 5*time.Second, db.MonitorInterval, "LTX files should be created") forceReplicaSync(t, db) vfs := newVFS(t, client) vfs.WriteEnabled = true vfs.WriteSyncInterval = 10 * time.Millisecond vfs.PollInterval = 25 * time.Millisecond vfsName := registerTestVFS(t, vfs) dsn := fmt.Sprintf("file:%s?vfs=%s&_busy_timeout=5000", filepath.ToSlash(filepath.Join(t.TempDir(), "stress-write.db")), vfsName) sqldb, err := sql.Open("sqlite3", dsn) if err != nil { t.Fatalf("open write db: %v", err) } defer sqldb.Close() sqldb.SetMaxOpenConns(4) sqldb.SetMaxIdleConns(4) const totalWrites = 50 for i := 0; i < totalWrites; i++ { if _, err := sqldb.Exec("INSERT INTO t (value) VALUES (?)", fmt.Sprintf("row-%d", i)); err != nil { if strings.Contains(err.Error(), "conflict") || errors.Is(err, litestream.ErrConflict) { t.Fatalf("false ErrConflict on write %d: %v", i, err) } t.Fatalf("write %d failed: %v", i, err) } // Brief pause every 5 writes to allow sync ticker to fire, // which causes TXID advancement and exercises the coordination logic if (i+1)%5 == 0 { time.Sleep(25 * time.Millisecond) } } var count int if err := sqldb.QueryRow("SELECT COUNT(*) FROM t").Scan(&count); err != nil { t.Fatalf("count rows: %v", err) } expected := totalWrites + 1 // +1 for seed row if count != expected { t.Fatalf("expected %d rows, got %d", expected, count) } } func newVFS(tb testing.TB, client litestream.ReplicaClient) *testVFS { tb.Helper() logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ Level: slog.LevelDebug, })) base := litestream.NewVFS(client, logger) base.PollInterval = 100 * time.Millisecond return &testVFS{ VFS: base, failures: make(map[string][]error), } } type testVFS struct { *litestream.VFS mu sync.Mutex failures map[string][]error } func (v *testVFS) Open(name string, flags sqlite3vfs.OpenFlag) (sqlite3vfs.File, sqlite3vfs.OpenFlag, error) { f, flags, err := v.VFS.Open(name, flags) if err != nil { return nil, flags, err } return &injectingFile{File: f, vfs: v, name: name}, flags, nil } func (v *testVFS) Inject(path string, err error) { v.mu.Lock() v.failures[path] = append(v.failures[path], err) v.mu.Unlock() } func (v *testVFS) popFailure(path string) error { v.mu.Lock() defer v.mu.Unlock() queue := v.failures[path] if len(queue) == 0 { return nil } err := queue[0] if len(queue) == 1 { delete(v.failures, path) } else { v.failures[path] = queue[1:] } if err == nil { return errors.New("vfs page read error") } return err } type injectingFile struct { sqlite3vfs.File vfs *testVFS name string } func (f *injectingFile) ReadAt(p []byte, off int64) (int, error) { if err := f.vfs.popFailure(f.name); err != nil { return 0, err } return f.File.ReadAt(p, off) } func (f *injectingFile) FileControl(op int, pragmaName string, pragmaValue *string) (*string, error) { if fc, ok := f.File.(sqlite3vfs.FileController); ok { return fc.FileControl(op, pragmaName, pragmaValue) } return nil, sqlite3vfs.NotFoundError } func registerTestVFS(tb testing.TB, vfs sqlite3vfs.VFS) string { tb.Helper() name := fmt.Sprintf("litestream-%s-%d", strings.ToLower(tb.Name()), time.Now().UnixNano()) if err := sqlite3vfs.RegisterVFS(name, vfs); err != nil { tb.Fatalf("failed to register litestream vfs %s: %v", name, err) } return name } func openReplicatedPrimary(tb testing.TB, client litestream.ReplicaClient, monitorInterval, syncInterval time.Duration) (*litestream.DB, *sql.DB) { tb.Helper() db := testingutil.NewDB(tb, filepath.Join(tb.TempDir(), "primary.db")) db.MonitorInterval = monitorInterval db.Replica = litestream.NewReplica(db) db.Replica.Client = client db.Replica.SyncInterval = syncInterval if err := db.Open(); err != nil { tb.Fatalf("open db: %v", err) } sqldb := testingutil.MustOpenSQLDB(tb, db.Path()) tb.Cleanup(func() { _ = db.Close(context.Background()) }) return db, sqldb } func forceReplicaSync(tb testing.TB, db *litestream.DB) { tb.Helper() ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err := db.Sync(ctx); err != nil { tb.Fatalf("force sync: %v", err) } if db.Replica != nil { if err := db.Replica.Sync(ctx); err != nil { tb.Fatalf("replica sync: %v", err) } } } func openVFSReplicaDB(tb testing.TB, vfsName string) *sql.DB { tb.Helper() dsn := fmt.Sprintf("file:%s?vfs=%s", filepath.ToSlash(filepath.Join(tb.TempDir(), vfsName+".db")), vfsName) sqldb, err := sql.Open("sqlite3", dsn) if err != nil { tb.Fatalf("open replica db: %v", err) } sqldb.SetMaxOpenConns(32) sqldb.SetMaxIdleConns(32) sqldb.SetConnMaxIdleTime(30 * time.Second) if _, err := sqldb.Exec("PRAGMA busy_timeout = 2000"); err != nil { tb.Fatalf("set busy timeout: %v", err) } return sqldb } func waitForReplicaRowCount(tb testing.TB, primary, replica *sql.DB, timeout time.Duration) { tb.Helper() require.Eventually(tb, func() bool { var primaryCount int if err := primary.QueryRow("SELECT COUNT(*) FROM t").Scan(&primaryCount); err != nil { return false } var replicaCount int if err := replica.QueryRow("SELECT COUNT(*) FROM t").Scan(&replicaCount); err != nil { return false } return primaryCount == replicaCount }, timeout, 50*time.Millisecond, "replica row count should match primary") } func waitForTableRowCount(tb testing.TB, primary, replica *sql.DB, table string, timeout time.Duration) { tb.Helper() query := fmt.Sprintf("SELECT COUNT(*) FROM %s", table) require.Eventually(tb, func() bool { var primaryCount int if err := primary.QueryRow(query).Scan(&primaryCount); err != nil { return false } var replicaCount int if err := replica.QueryRow(query).Scan(&replicaCount); err != nil { return false } return primaryCount == replicaCount }, timeout, 50*time.Millisecond, "replica row count for %s should match primary", table) } func fetchOrderedPayloads(tb testing.TB, db *sql.DB, limit int, orderBy string) []string { tb.Helper() query := fmt.Sprintf("SELECT payload FROM t ORDER BY %s LIMIT %d", orderBy, limit) rows, err := db.Query(query) if err != nil { tb.Fatalf("query payloads: %v", err) } defer rows.Close() var out []string for rows.Next() { var payload string if err := rows.Scan(&payload); err != nil { tb.Fatalf("scan payload: %v", err) } out = append(out, payload) } if err := rows.Err(); err != nil { tb.Fatalf("rows err: %v", err) } return out } func seedLargeTable(tb testing.TB, db *sql.DB, n int) { tb.Helper() trx, err := db.Begin() if err != nil { tb.Fatalf("begin seed: %v", err) } stmt, err := trx.Prepare("INSERT INTO t (value, updated_at) VALUES (?, strftime('%s','now'))") if err != nil { _ = trx.Rollback() tb.Fatalf("prepare seed: %v", err) } defer stmt.Close() rnd := rand.New(rand.NewSource(42)) for i := 0; i < n; i++ { if _, err := stmt.Exec(fmt.Sprintf("seed-%d-%d", i, rnd.Int())); err != nil { _ = trx.Rollback() tb.Fatalf("seed exec: %v", err) } } if err := trx.Commit(); err != nil { tb.Fatalf("commit seed: %v", err) } } func seedSortedDataset(tb testing.TB, db *sql.DB, n int) { tb.Helper() trx, err := db.Begin() if err != nil { tb.Fatalf("begin sorted seed: %v", err) } stmt, err := trx.Prepare("INSERT INTO t (id, payload, grp) VALUES (?, ?, ?)") if err != nil { _ = trx.Rollback() tb.Fatalf("prepare sorted seed: %v", err) } defer stmt.Close() rnd := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < n; i++ { if _, err := stmt.Exec(i+1, randomPayload(rnd, 256), rnd.Intn(1024)); err != nil { _ = trx.Rollback() tb.Fatalf("sorted seed exec: %v", err) } } if err := trx.Commit(); err != nil { tb.Fatalf("commit sorted seed: %v", err) } } func randomPayload(r *rand.Rand, n int) string { const letters = "abcdefghijklmnopqrstuvwxyz0123456789" b := make([]byte, n) for i := range b { b[i] = letters[r.Intn(len(letters))] } return string(b) } func pageSizedPayload(pageSize int, row int) string { base := fmt.Sprintf("row_%05d_", row) maxPayload := pageSize / 4 if maxPayload < len(base)+1 { maxPayload = len(base) + 1 } if maxPayload > 4096 { maxPayload = 4096 } fillerLen := maxPayload - len(base) if fillerLen < 0 { fillerLen = 0 } return base + strings.Repeat("x", fillerLen) } func isBusyError(err error) bool { if err == nil { return false } if e, ok := err.(sqlite3.Error); ok { if e.Code == sqlite3.ErrBusy || e.Code == sqlite3.ErrLocked { return true } // Under heavy churn, go-sqlite3 can surface ErrError with the // generic "SQL logic error" message while the VFS swaps databases. if e.Code == sqlite3.ErrError && strings.Contains(e.Error(), "SQL logic error") { return true } } msg := err.Error() if strings.Contains(msg, "database is locked") || strings.Contains(msg, "database is busy") { return true } return strings.Contains(msg, "converting NULL to int") } func writeSinglePageLTXFile(tb testing.TB, client *file.ReplicaClient, txid ltx.TXID, fill byte) { tb.Helper() page := bytes.Repeat([]byte{fill}, 4096) var buf bytes.Buffer enc, err := ltx.NewEncoder(&buf) if err != nil { tb.Fatalf("new encoder: %v", err) } hdr := ltx.Header{ Version: ltx.Version, PageSize: 4096, Commit: 1, MinTXID: txid, MaxTXID: txid, Timestamp: time.Now().UnixMilli(), Flags: ltx.HeaderFlagNoChecksum, } if err := enc.EncodeHeader(hdr); err != nil { tb.Fatalf("encode header: %v", err) } if err := enc.EncodePage(ltx.PageHeader{Pgno: 1}, page); err != nil { tb.Fatalf("encode page: %v", err) } if err := enc.Close(); err != nil { tb.Fatalf("close encoder: %v", err) } if _, err := client.WriteLTXFile(context.Background(), 0, txid, txid, bytes.NewReader(buf.Bytes())); err != nil { tb.Fatalf("write ltx file: %v", err) } } type latencyReplicaClient struct { litestream.ReplicaClient delay time.Duration } func (c *latencyReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { time.Sleep(c.delay) return c.ReplicaClient.OpenLTXFile(ctx, level, minTXID, maxTXID, offset, size) } func (c *latencyReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { time.Sleep(c.delay) return c.ReplicaClient.LTXFiles(ctx, level, seek, useMetadata) } type eventualConsistencyClient struct { litestream.ReplicaClient calls atomic.Int32 } func (c *eventualConsistencyClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { if c.calls.Add(1) == 1 { return ltx.NewFileInfoSliceIterator(nil), nil } return c.ReplicaClient.LTXFiles(ctx, level, seek, useMetadata) } type observingReplicaClient struct { litestream.ReplicaClient ltxCalls atomic.Int64 } type fdLimitedReplicaClient struct { litestream.ReplicaClient limit int32 open atomic.Int32 maxOpen atomic.Int32 } func (c *fdLimitedReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { current := c.open.Add(1) for { max := c.maxOpen.Load() if current <= max || c.maxOpen.CompareAndSwap(max, current) { break } } if current > c.limit { c.open.Add(-1) return nil, fmt.Errorf("fd limit exceeded: %d/%d", current, c.limit) } rc, err := c.ReplicaClient.OpenLTXFile(ctx, level, minTXID, maxTXID, offset, size) if err != nil { c.open.Add(-1) return nil, err } return &hookedReadCloser{ReadCloser: rc, hook: func() { c.open.Add(-1) }}, nil } func (c *observingReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { c.ltxCalls.Add(1) return c.ReplicaClient.LTXFiles(ctx, level, seek, useMetadata) } type flakyLTXClient struct { litestream.ReplicaClient failNext atomic.Bool failures atomic.Int64 } func (c *flakyLTXClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { if c.failNext.CompareAndSwap(true, false) { c.failures.Add(1) return nil, fmt.Errorf("ltx list unavailable") } return c.ReplicaClient.LTXFiles(ctx, level, seek, useMetadata) } type oomPageIndexClient struct { litestream.ReplicaClient failNext atomic.Bool triggered atomic.Bool } func (c *oomPageIndexClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { if offset > 0 && c.failNext.CompareAndSwap(true, false) { c.triggered.Store(true) return nil, fmt.Errorf("simulated page index OOM") } return c.ReplicaClient.OpenLTXFile(ctx, level, minTXID, maxTXID, offset, size) } type corruptingPageIndexClient struct { litestream.ReplicaClient corruptNext atomic.Bool triggered atomic.Bool } func (c *corruptingPageIndexClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { rc, err := c.ReplicaClient.OpenLTXFile(ctx, level, minTXID, maxTXID, offset, size) if err != nil { return nil, err } if c.corruptNext.CompareAndSwap(true, false) { c.triggered.Store(true) data, readErr := io.ReadAll(rc) rc.Close() if readErr != nil { return nil, readErr } if len(data) > 0 { data[0] ^= 0xFF } return io.NopCloser(bytes.NewReader(data)), nil } return rc, nil } type hookedReadCloser struct { io.ReadCloser once sync.Once hook func() } func (h *hookedReadCloser) Close() error { var err error h.once.Do(func() { err = h.ReadCloser.Close() if h.hook != nil { h.hook() } }) return err } // waitForLTXFiles waits until at least one LTX file is available in the replica client. func waitForLTXFiles(t *testing.T, client litestream.ReplicaClient, timeout, tick time.Duration) { t.Helper() require.Eventually(t, func() bool { itr, err := client.LTXFiles(context.Background(), 0, 0, false) if err != nil { return false } defer itr.Close() return itr.Next() }, timeout, tick, "LTX files should be available") } // waitForReplicaValue waits until the replica database returns the expected int value. func waitForReplicaValue(t *testing.T, db *sql.DB, query string, expected int, timeout, tick time.Duration) { t.Helper() require.Eventually(t, func() bool { var got int if err := db.QueryRow(query).Scan(&got); err != nil { return false } return got == expected }, timeout, tick, "replica should return expected value") } ================================================ FILE: cmd/litestream-vfs/stress_test.go ================================================ //go:build vfs && stress // +build vfs,stress package main_test import ( "context" "math/rand" "os" "runtime" "sync/atomic" "testing" "time" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/internal/testingutil" ) func TestVFS_RaceStressHarness(t *testing.T) { if os.Getenv("LITESTREAM_ALLOW_RACE") != "1" { t.Skip("set LITESTREAM_ALLOW_RACE=1 to run unstable race harness; modernc.org/sqlite checkptr panics are still unresolved") } if !runtime.RaceEnabled() { t.Skip("requires go test -race") } client := file.NewReplicaClient(t.TempDir()) db, primary := openReplicatedPrimary(t, client, 20*time.Millisecond, 20*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec("CREATE TABLE stress (id INTEGER PRIMARY KEY, value TEXT)"); err != nil { t.Fatalf("create table: %v", err) } seedLargeTable(t, primary, 100) vfs := newVFS(t, client) vfs.PollInterval = 5 * time.Millisecond vfsName := registerTestVFS(t, vfs) replica := openVFSReplicaDB(t, vfsName) defer replica.Close() waitForReplicaRowCount(t, primary, replica, 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() var writes atomic.Int64 go func() { rnd := rand.New(rand.NewSource(time.Now().UnixNano())) for { select { case <-ctx.Done(): return default: } if _, err := primary.Exec("INSERT INTO stress (value) VALUES (?)", randomPayload(rnd, 64)); err != nil && !isBusyError(err) { t.Errorf("writer error: %v", err) return } writes.Add(1) } }() const readers = 64 errCh := make(chan error, readers) for i := 0; i < readers; i++ { go func() { rnd := rand.New(rand.NewSource(time.Now().UnixNano())) for { select { case <-ctx.Done(): errCh <- nil return default: } var count int if err := replica.QueryRow("SELECT COUNT(*) FROM stress WHERE id >= ?", rnd.Intn(50)).Scan(&count); err != nil { if isBusyError(err) { continue } errCh <- err return } } }() } for i := 0; i < readers; i++ { if err := <-errCh; err != nil { t.Fatalf("reader error: %v", err) } } if writes.Load() == 0 { t.Fatalf("writer never made progress") } } ================================================ FILE: cmd/litestream-vfs/time_travel_test.go ================================================ //go:build vfs // +build vfs package main_test import ( "context" "database/sql" "fmt" "path/filepath" "strings" "testing" "time" _ "github.com/mattn/go-sqlite3" "github.com/psanford/sqlite3vfs" "github.com/stretchr/testify/require" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/internal/testingutil" ) func TestVFS_TimeTravelFunctions(t *testing.T) { ctx := context.Background() client := file.NewReplicaClient(t.TempDir()) vfs := newVFS(t, client) vfs.PollInterval = 50 * time.Millisecond if err := sqlite3vfs.RegisterVFS("litestream-time", vfs); err != nil { t.Fatalf("failed to register litestream vfs: %v", err) } db := testingutil.NewDB(t, filepath.Join(t.TempDir(), "db")) db.MonitorInterval = 50 * time.Millisecond db.Replica = litestream.NewReplica(db) db.Replica.Client = client db.Replica.SyncInterval = 50 * time.Millisecond if err := db.Open(); err != nil { t.Fatal(err) } defer func() { _ = db.Close(ctx) }() sqldb0 := testingutil.MustOpenSQLDB(t, db.Path()) defer testingutil.MustCloseSQLDB(t, sqldb0) if _, err := sqldb0.Exec("CREATE TABLE t (x INTEGER)"); err != nil { t.Fatal(err) } if _, err := sqldb0.Exec("INSERT INTO t (x) VALUES (100)"); err != nil { t.Fatal(err) } // Wait for LTX files to be created require.Eventually(t, func() bool { itr, err := client.LTXFiles(ctx, 0, 0, false) if err != nil { return false } defer itr.Close() return itr.Next() }, 10*time.Second, db.MonitorInterval, "LTX files should be created") firstCreatedAt := fetchLTXCreatedAt(t, ctx, client) time.Sleep(20 * time.Millisecond) // Ensure a different timestamp for the next file. if _, err := sqldb0.Exec("UPDATE t SET x = 200"); err != nil { t.Fatal(err) } sqldb1, err := sql.Open("sqlite3", "file:/tmp/time-travel.db?vfs=litestream-time") if err != nil { t.Fatalf("failed to open database: %v", err) } defer sqldb1.Close() sqldb1.SetMaxOpenConns(1) var value int require.Eventually(t, func() bool { if err := sqldb1.QueryRow("SELECT x FROM t").Scan(&value); err != nil { return false } return value == 200 }, 10*time.Second, vfs.PollInterval, "VFS should observe updated value") target := firstCreatedAt.Add(1 * time.Millisecond).UTC().Format(time.RFC3339Nano) if _, err := sqldb1.Exec(fmt.Sprintf("PRAGMA LITESTREAM_TIME = '%s'", target)); err != nil { t.Fatalf("set target time: %v", err) } if err := sqldb1.QueryRow("SELECT x FROM t").Scan(&value); err != nil { t.Fatalf("query historical value: %v", err) } else if got, want := value, 100; got != want { t.Fatalf("historical value: got %d, want %d", got, want) } var currentTime string if err := sqldb1.QueryRow("PRAGMA litestream_time").Scan(¤tTime); err != nil { t.Fatalf("current time: %v", err) } else if currentTime != target { t.Fatalf("current time mismatch: got %s, want %s", currentTime, target) } if _, err := sqldb1.Exec("PRAGMA LITESTREAM_TIME = LATEST"); err != nil { t.Fatalf("reset time: %v", err) } if err := sqldb1.QueryRow("SELECT x FROM t").Scan(&value); err != nil { t.Fatalf("query reset value: %v", err) } else if got, want := value, 200; got != want { t.Fatalf("reset value: got %d, want %d", got, want) } if err := sqldb1.QueryRow("PRAGMA litestream_time").Scan(¤tTime); err != nil { t.Fatalf("current time after reset: %v", err) } // After reset, should return actual LTX timestamp (not "latest" anymore per #853) if _, err := time.Parse(time.RFC3339Nano, currentTime); err != nil { t.Fatalf("current time after reset should be valid RFC3339Nano timestamp, got %s: %v", currentTime, err) } } func TestVFS_PragmaLitestreamTxid(t *testing.T) { ctx := context.Background() client := file.NewReplicaClient(t.TempDir()) vfs := newVFS(t, client) vfs.PollInterval = 50 * time.Millisecond if err := sqlite3vfs.RegisterVFS("litestream-txid", vfs); err != nil { t.Fatalf("failed to register litestream vfs: %v", err) } db := testingutil.NewDB(t, filepath.Join(t.TempDir(), "db")) db.MonitorInterval = 50 * time.Millisecond db.Replica = litestream.NewReplica(db) db.Replica.Client = client db.Replica.SyncInterval = 50 * time.Millisecond if err := db.Open(); err != nil { t.Fatal(err) } defer func() { _ = db.Close(ctx) }() sqldb0 := testingutil.MustOpenSQLDB(t, db.Path()) defer testingutil.MustCloseSQLDB(t, sqldb0) if _, err := sqldb0.Exec("CREATE TABLE t (x INTEGER)"); err != nil { t.Fatal(err) } if _, err := sqldb0.Exec("INSERT INTO t (x) VALUES (100)"); err != nil { t.Fatal(err) } sqldb1, err := sql.Open("sqlite3", "file:/tmp/txid-test.db?vfs=litestream-txid") if err != nil { t.Fatalf("failed to open database: %v", err) } defer sqldb1.Close() sqldb1.SetMaxOpenConns(1) var txid int64 require.Eventually(t, func() bool { if err := sqldb1.QueryRow("PRAGMA litestream_txid").Scan(&txid); err != nil { return false } return txid > 0 }, 10*time.Second, vfs.PollInterval, "PRAGMA litestream_txid should return positive value") // Test that setting litestream_txid fails (read-only) if _, err := sqldb1.Exec("PRAGMA litestream_txid = 123"); err == nil { t.Fatal("expected error setting litestream_txid (read-only)") } } func TestVFS_PragmaLitestreamLag(t *testing.T) { ctx := context.Background() client := file.NewReplicaClient(t.TempDir()) vfs := newVFS(t, client) vfs.PollInterval = 50 * time.Millisecond if err := sqlite3vfs.RegisterVFS("litestream-lag", vfs); err != nil { t.Fatalf("failed to register litestream vfs: %v", err) } db := testingutil.NewDB(t, filepath.Join(t.TempDir(), "db")) db.MonitorInterval = 50 * time.Millisecond db.Replica = litestream.NewReplica(db) db.Replica.Client = client db.Replica.SyncInterval = 50 * time.Millisecond if err := db.Open(); err != nil { t.Fatal(err) } defer func() { _ = db.Close(ctx) }() sqldb0 := testingutil.MustOpenSQLDB(t, db.Path()) defer testingutil.MustCloseSQLDB(t, sqldb0) if _, err := sqldb0.Exec("CREATE TABLE t (x INTEGER)"); err != nil { t.Fatal(err) } if _, err := sqldb0.Exec("INSERT INTO t (x) VALUES (100)"); err != nil { t.Fatal(err) } sqldb1, err := sql.Open("sqlite3", "file:/tmp/lag-test.db?vfs=litestream-lag") if err != nil { t.Fatalf("failed to open database: %v", err) } defer sqldb1.Close() sqldb1.SetMaxOpenConns(1) // Wait for replica to catch up with polling. var lag int64 require.Eventually(t, func() bool { if err := sqldb1.QueryRow("PRAGMA litestream_lag").Scan(&lag); err != nil { t.Logf("query lag: %v", err) return false } return lag >= 0 }, 10*time.Second, vfs.PollInterval, "lag should become >= 0") // Test that setting litestream_lag fails (read-only) if _, err := sqldb1.Exec("PRAGMA litestream_lag = 123"); err == nil { t.Fatal("expected error setting litestream_lag (read-only)") } } func TestVFS_PragmaRelativeTime(t *testing.T) { ctx := context.Background() client := file.NewReplicaClient(t.TempDir()) vfs := newVFS(t, client) vfs.PollInterval = 50 * time.Millisecond if err := sqlite3vfs.RegisterVFS("litestream-relative", vfs); err != nil { t.Fatalf("failed to register litestream vfs: %v", err) } db := testingutil.NewDB(t, filepath.Join(t.TempDir(), "db")) db.MonitorInterval = 50 * time.Millisecond db.Replica = litestream.NewReplica(db) db.Replica.Client = client db.Replica.SyncInterval = 50 * time.Millisecond if err := db.Open(); err != nil { t.Fatal(err) } defer func() { _ = db.Close(ctx) }() sqldb0 := testingutil.MustOpenSQLDB(t, db.Path()) defer testingutil.MustCloseSQLDB(t, sqldb0) if _, err := sqldb0.Exec("CREATE TABLE t (x INTEGER)"); err != nil { t.Fatal(err) } if _, err := sqldb0.Exec("INSERT INTO t (x) VALUES (100)"); err != nil { t.Fatal(err) } sqldb1, err := sql.Open("sqlite3", "file:/tmp/relative-test.db?vfs=litestream-relative") if err != nil { t.Fatalf("failed to open database: %v", err) } defer sqldb1.Close() sqldb1.SetMaxOpenConns(1) // Wait for VFS to poll initial data require.Eventually(t, func() bool { var x int return sqldb1.QueryRow("SELECT x FROM t").Scan(&x) == nil }, 10*time.Second, vfs.PollInterval, "VFS should observe initial data") // Test that relative time parsing works (even if no data exists at that time) // The parse should succeed, but may return "no backup files available" if too far in past now := time.Now() _, err = sqldb1.Exec("PRAGMA litestream_time = '1 second ago'") // This might fail if no LTX files exist at that time, which is expected. // The important thing is that the parsing worked (not a "parse timestamp" error). if err != nil { errMsg := err.Error() // These are expected errors that indicate parsing succeeded but time-travel // couldn't be performed (no files at that time). expectedErrors := []string{ "no backup files available", "timestamp is before earliest LTX file", } expectedSubstrings := []string{ "transaction not available", // ErrTxNotAvailable when target is before earliest LTX "SQL logic error", // SQLite error during page index rebuild for time-travel } isExpected := false for _, expected := range expectedErrors { if errMsg == expected { isExpected = true break } } if !isExpected { for _, substr := range expectedSubstrings { if strings.Contains(errMsg, substr) { isExpected = true break } } } if !isExpected { // Fail on any unexpected error to catch regressions t.Fatalf("unexpected error from relative time PRAGMA: %v", err) } } // Reset to latest if _, err := sqldb1.Exec("PRAGMA litestream_time = LATEST"); err != nil { t.Fatalf("reset to latest: %v", err) } // Verify the current time is recent (within last minute) var currentTime string if err := sqldb1.QueryRow("PRAGMA litestream_time").Scan(¤tTime); err != nil { t.Fatalf("query current time: %v", err) } ts, err := time.Parse(time.RFC3339Nano, currentTime) if err != nil { t.Fatalf("parse current time: %v", err) } if now.Sub(ts) > time.Minute { t.Fatalf("current time too old: %v (now: %v)", ts, now) } } func fetchLTXCreatedAt(tb testing.TB, ctx context.Context, client litestream.ReplicaClient) time.Time { tb.Helper() itr, err := client.LTXFiles(ctx, 0, 0, true) if err != nil { tb.Fatalf("ltx files: %v", err) } defer itr.Close() var ts time.Time for itr.Next() { ts = itr.Item().CreatedAt } if err := itr.Close(); err != nil { tb.Fatalf("close iterator: %v", err) } if ts.IsZero() { tb.Fatalf("no ltx files found") } return ts.UTC() } ================================================ FILE: cmd/litestream-vfs/vfs_soak_test.go ================================================ //go:build vfs && soak // +build vfs,soak package main_test import ( "context" "fmt" "os" "sync" "sync/atomic" "testing" "time" _ "github.com/mattn/go-sqlite3" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/internal/testingutil" ) // TestVFS_LongRunningSoak exercises the VFS under sustained read/write load. // The default duration is 5 minutes but can be overridden with the // LITESTREAM_VFS_SOAK_DURATION environment variable (e.g. "10m"). func TestVFS_LongRunningSoak(t *testing.T) { duration := 5 * time.Minute if v := os.Getenv("LITESTREAM_VFS_SOAK_DURATION"); v != "" { if parsed, err := time.ParseDuration(v); err == nil { duration = parsed } } if testing.Short() && duration > time.Minute { duration = time.Minute } client := file.NewReplicaClient(t.TempDir()) vfs := newVFS(t, client) vfs.PollInterval = 100 * time.Millisecond vfsName := registerTestVFS(t, vfs) db, primary := openReplicatedPrimary(t, client, 75*time.Millisecond, 75*time.Millisecond) defer testingutil.MustCloseSQLDB(t, primary) if _, err := primary.Exec(`CREATE TABLE t ( id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT, updated_at INTEGER )`); err != nil { t.Fatalf("create table: %v", err) } seedLargeTable(t, primary, 1000) forceReplicaSync(t, db) replica := openVFSReplicaDB(t, vfsName) defer replica.Close() waitForReplicaRowCount(t, primary, replica, time.Minute) ctx, cancel := context.WithTimeout(context.Background(), duration) defer cancel() var writeOps atomic.Int64 var readOps atomic.Int64 errCh := make(chan error, 8) var wg sync.WaitGroup // Writers continuously mutate the primary database. startWriter := func(name string) { wg.Add(1) go func() { defer wg.Done() rnd := time.NewTicker(7 * time.Millisecond) defer rnd.Stop() for { select { case <-ctx.Done(): return case <-rnd.C: if _, err := primary.Exec("INSERT INTO t (value, updated_at) VALUES (?, strftime('%s','now'))", fmt.Sprintf("%s-%d", name, time.Now().UnixNano())); err != nil { errCh <- fmt.Errorf("writer %s insert: %w", name, err) return } if _, err := primary.Exec("UPDATE t SET value = value || '-w' WHERE id IN (SELECT id FROM t ORDER BY RANDOM() LIMIT 1)"); err != nil { errCh <- fmt.Errorf("writer %s update: %w", name, err) return } writeOps.Add(2) } } }() } startReader := func(name string) { wg.Add(1) go func() { defer wg.Done() for { select { case <-ctx.Done(): return default: } var minID, maxID, count int if err := replica.QueryRow("SELECT IFNULL(MIN(id),0), IFNULL(MAX(id),0), COUNT(*) FROM t").Scan(&minID, &maxID, &count); err != nil { errCh <- fmt.Errorf("reader %s query: %w", name, err) return } if minID > maxID && count > 0 { errCh <- fmt.Errorf("reader %s saw invalid range", name) return } readOps.Add(1) } }() } for i := 0; i < 2; i++ { startWriter(fmt.Sprintf("writer-%d", i)) } for i := 0; i < 4; i++ { startReader(fmt.Sprintf("reader-%d", i)) } <-ctx.Done() wg.Wait() close(errCh) for err := range errCh { if err != nil { t.Fatalf("soak error: %v", err) } } if writeOps.Load() < int64(duration/time.Millisecond) { t.Fatalf("expected sustained writes, got %d ops", writeOps.Load()) } if readOps.Load() == 0 { t.Fatalf("expected replica reads during soak") } waitForReplicaRowCount(t, primary, replica, time.Minute) } ================================================ FILE: cmd/litestream-vfs/vfs_write_integration_test.go ================================================ //go:build vfs // +build vfs package main_test import ( "bytes" "context" "database/sql" "fmt" "log/slog" "os" "path/filepath" "sync" "testing" "time" "github.com/psanford/sqlite3vfs" "github.com/stretchr/testify/require" "github.com/superfly/ltx" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/internal/testingutil" ) // ============================================================================= // Basic Operations Tests // ============================================================================= // TestVFS_WriteAndSync_FileBackend tests basic write and sync functionality // with the file backend. func TestVFS_WriteAndSync_FileBackend(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) // First, create initial data using standard litestream replication db := testingutil.NewDB(t, filepath.Join(t.TempDir(), "source.db")) db.MonitorInterval = 100 * time.Millisecond db.Replica = litestream.NewReplica(db) db.Replica.Client = client db.Replica.SyncInterval = 100 * time.Millisecond require.NoError(t, db.Open()) sqldb0 := testingutil.MustOpenSQLDB(t, db.Path()) defer testingutil.MustCloseSQLDB(t, sqldb0) _, err := sqldb0.Exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)") require.NoError(t, err) _, err = sqldb0.Exec("INSERT INTO users (id, name) VALUES (1, 'Alice')") require.NoError(t, err) waitForLTXFiles(t, client, 10*time.Second, db.MonitorInterval) require.NoError(t, db.Replica.Stop(false)) testingutil.MustCloseSQLDB(t, sqldb0) require.NoError(t, db.Close(context.Background())) // Now open via writable VFS and add more data vfs := newWritableVFS(t, client, 1*time.Second, t.TempDir()) vfsName := fmt.Sprintf("litestream-write-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb1, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb1.Close() // Verify initial data var name string err = sqldb1.QueryRow("SELECT name FROM users WHERE id = 1").Scan(&name) require.NoError(t, err) require.Equal(t, "Alice", name) // Insert new data via VFS _, err = sqldb1.Exec("INSERT INTO users (id, name) VALUES (2, 'Bob')") require.NoError(t, err) // Force sync sqldb1.Close() // Verify data was synced by opening fresh VFS vfs2 := newWritableVFS(t, client, 0, "") vfsName2 := fmt.Sprintf("litestream-write2-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName2, vfs2)) sqldb2, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName2)) require.NoError(t, err) defer sqldb2.Close() err = sqldb2.QueryRow("SELECT name FROM users WHERE id = 2").Scan(&name) require.NoError(t, err) require.Equal(t, "Bob", name) } // TestVFS_ReadYourWrites verifies that written data is visible immediately // before sync (from dirty pages). func TestVFS_ReadYourWrites(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) // Create initial database setupInitialDB(t, client) // Open via writable VFS with long sync interval (won't auto-sync) vfs := newWritableVFS(t, client, 1*time.Hour, t.TempDir()) vfsName := fmt.Sprintf("litestream-ryw-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Write data _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (2, 'Bob')") require.NoError(t, err) // Read it back immediately (before sync) var name string err = sqldb.QueryRow("SELECT name FROM users WHERE id = 2").Scan(&name) require.NoError(t, err) require.Equal(t, "Bob", name) // Update and read again _, err = sqldb.Exec("UPDATE users SET name = 'Robert' WHERE id = 2") require.NoError(t, err) err = sqldb.QueryRow("SELECT name FROM users WHERE id = 2").Scan(&name) require.NoError(t, err) require.Equal(t, "Robert", name) } // TestVFS_MultipleTransactions tests multiple sequential transactions. func TestVFS_MultipleTransactions(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) vfs := newWritableVFS(t, client, 100*time.Millisecond, t.TempDir()) vfsName := fmt.Sprintf("litestream-multi-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Execute multiple transactions for i := 2; i <= 10; i++ { _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (?, ?)", i, fmt.Sprintf("User%d", i)) require.NoError(t, err) } // Wait for syncs time.Sleep(500 * time.Millisecond) // Verify all data var count int err = sqldb.QueryRow("SELECT COUNT(*) FROM users").Scan(&count) require.NoError(t, err) require.Equal(t, 10, count) } // TestVFS_LargeTransaction tests writing many pages in a single transaction. func TestVFS_LargeTransaction(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) vfs := newWritableVFS(t, client, 1*time.Second, t.TempDir()) vfsName := fmt.Sprintf("litestream-large-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Insert 1000 rows in a single transaction (should span many pages) tx, err := sqldb.Begin() require.NoError(t, err) for i := 2; i <= 1001; i++ { _, err = tx.Exec("INSERT INTO users (id, name) VALUES (?, ?)", i, fmt.Sprintf("User%d with some extra data to take up space", i)) require.NoError(t, err) } err = tx.Commit() require.NoError(t, err) // Force sync by closing sqldb.Close() // Verify data persisted vfs2 := newWritableVFS(t, client, 0, "") vfsName2 := fmt.Sprintf("litestream-large2-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName2, vfs2)) sqldb2, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName2)) require.NoError(t, err) defer sqldb2.Close() var count int err = sqldb2.QueryRow("SELECT COUNT(*) FROM users").Scan(&count) require.NoError(t, err) require.Equal(t, 1001, count) } // ============================================================================= // Sync Behavior Tests // ============================================================================= // TestVFS_PeriodicSync verifies automatic periodic sync. func TestVFS_PeriodicSync(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) // Get initial LTX count initialCount := countLTXFiles(t, client) vfs := newWritableVFS(t, client, 200*time.Millisecond, t.TempDir()) vfsName := fmt.Sprintf("litestream-periodic-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Write data _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (2, 'Bob')") require.NoError(t, err) // Wait for auto-sync (should happen within ~200ms) time.Sleep(500 * time.Millisecond) // Verify new LTX file was created newCount := countLTXFiles(t, client) require.Greater(t, newCount, initialCount, "expected new LTX file from auto-sync") } // TestVFS_SyncDuringTransaction verifies sync is deferred during active transaction. func TestVFS_SyncDuringTransaction(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) vfs := newWritableVFS(t, client, 100*time.Millisecond, t.TempDir()) vfsName := fmt.Sprintf("litestream-txsync-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() initialCount := countLTXFiles(t, client) // Begin transaction tx, err := sqldb.Begin() require.NoError(t, err) // Write data within transaction _, err = tx.Exec("INSERT INTO users (id, name) VALUES (2, 'Bob')") require.NoError(t, err) // Wait - sync should be deferred time.Sleep(300 * time.Millisecond) // LTX count should not have increased during transaction midCount := countLTXFiles(t, client) require.Equal(t, initialCount, midCount, "sync should be deferred during transaction") // Commit transaction err = tx.Commit() require.NoError(t, err) // Wait for sync after commit time.Sleep(300 * time.Millisecond) // Now LTX should have increased finalCount := countLTXFiles(t, client) require.Greater(t, finalCount, initialCount, "expected new LTX file after commit") } // TestVFS_ManualSyncOnly tests with SyncInterval=0 (manual sync only). func TestVFS_ManualSyncOnly(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) // SyncInterval=0 means no auto-sync vfs := newWritableVFS(t, client, 0, t.TempDir()) vfsName := fmt.Sprintf("litestream-manual-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) initialCount := countLTXFiles(t, client) // Write data _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (2, 'Bob')") require.NoError(t, err) // Wait - should NOT auto-sync time.Sleep(500 * time.Millisecond) midCount := countLTXFiles(t, client) require.Equal(t, initialCount, midCount, "should not auto-sync when SyncInterval=0") // Close triggers sync sqldb.Close() // Now should be synced finalCount := countLTXFiles(t, client) require.Greater(t, finalCount, initialCount, "expected sync on close") } // ============================================================================= // Write Buffer Tests // ============================================================================= // TestVFS_WriteBufferDiscardedOnOpen tests that write buffer is discarded on open // (unsynced data is lost after crash). func TestVFS_WriteBufferDiscardedOnOpen(t *testing.T) { replicaDir := t.TempDir() bufferDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) // Open VFS and write data vfs := newWritableVFS(t, client, 1*time.Hour, bufferDir) // Long interval, won't auto-sync vfsName := fmt.Sprintf("litestream-discard1-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (2, 'Bob')") require.NoError(t, err) // Verify write buffer file exists bufferPath := filepath.Join(bufferDir, ".litestream-buffer") _, err = os.Stat(bufferPath) require.NoError(t, err, "write buffer file should exist") // Simulate crash by not closing properly (don't call sqldb.Close()) // Just abandon the connection // Reopen with new VFS - buffer should be discarded vfs2 := newWritableVFS(t, client, 1*time.Second, bufferDir) vfsName2 := fmt.Sprintf("litestream-discard2-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName2, vfs2)) sqldb2, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName2)) require.NoError(t, err) defer sqldb2.Close() // Data should NOT be recovered (buffer is discarded on open) var count int err = sqldb2.QueryRow("SELECT COUNT(*) FROM users WHERE id = 2").Scan(&count) require.NoError(t, err) require.Equal(t, 0, count, "unsynced data should be lost after crash") } // TestVFS_WriteBufferDuplicatePages tests that duplicate page writes within // a session correctly overwrite previous values in the buffer. func TestVFS_WriteBufferDuplicatePages(t *testing.T) { replicaDir := t.TempDir() bufferDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) vfs := newWritableVFS(t, client, 1*time.Hour, bufferDir) vfsName := fmt.Sprintf("litestream-dup1-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Write to same row multiple times (updates same pages) _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (2, 'Bob')") require.NoError(t, err) _, err = sqldb.Exec("UPDATE users SET name = 'Robert' WHERE id = 2") require.NoError(t, err) _, err = sqldb.Exec("UPDATE users SET name = 'Bobby' WHERE id = 2") require.NoError(t, err) // Should have latest value (read-your-writes within session) var name string err = sqldb.QueryRow("SELECT name FROM users WHERE id = 2").Scan(&name) require.NoError(t, err) require.Equal(t, "Bobby", name) // Close to trigger sync sqldb.Close() // Verify data persists in replica after sync vfs2 := newWritableVFS(t, client, 1*time.Second, t.TempDir()) vfsName2 := fmt.Sprintf("litestream-dup2-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName2, vfs2)) sqldb2, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName2)) require.NoError(t, err) defer sqldb2.Close() // Should have latest value from synced data err = sqldb2.QueryRow("SELECT name FROM users WHERE id = 2").Scan(&name) require.NoError(t, err) require.Equal(t, "Bobby", name) } // TestVFS_ExistingBufferDiscarded tests that any existing buffer file is discarded on open. func TestVFS_ExistingBufferDiscarded(t *testing.T) { replicaDir := t.TempDir() bufferDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) // Create a pre-existing write buffer file with some content bufferPath := filepath.Join(bufferDir, ".litestream-write-buffer") require.NoError(t, os.WriteFile(bufferPath, []byte("stale data"), 0644)) // Open VFS - should discard existing buffer vfs := newWritableVFS(t, client, 1*time.Second, bufferDir) vfsName := fmt.Sprintf("litestream-existing-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Should only see original data (existing buffer discarded) var count int err = sqldb.QueryRow("SELECT COUNT(*) FROM users").Scan(&count) require.NoError(t, err) require.Equal(t, 1, count, "existing buffer should be discarded") } // TestVFS_WriteBufferCorrupted tests handling of corrupted buffer file. func TestVFS_WriteBufferCorrupted(t *testing.T) { replicaDir := t.TempDir() bufferDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) // Create corrupted buffer (invalid magic) bufferPath := filepath.Join(bufferDir, ".litestream-write-buffer") require.NoError(t, os.WriteFile(bufferPath, []byte("INVALID DATA"), 0644)) // Open VFS - should handle gracefully vfs := newWritableVFS(t, client, 1*time.Second, bufferDir) vfsName := fmt.Sprintf("litestream-corrupt-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Should work with original data var count int err = sqldb.QueryRow("SELECT COUNT(*) FROM users").Scan(&count) require.NoError(t, err) require.Equal(t, 1, count) } // ============================================================================= // Conflict Detection Tests // ============================================================================= // TestVFS_ConflictDetection tests that conflicts are detected when remote changes. func TestVFS_ConflictDetection(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) // Open writable VFS vfs := newWritableVFS(t, client, 1*time.Hour, t.TempDir()) // Long interval vfsName := fmt.Sprintf("litestream-conflict-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Write data via VFS (not synced yet) _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (2, 'Bob')") require.NoError(t, err) // Externally add new LTX file to simulate another writer addExternalLTXFile(t, client, replicaDir) // Now close - sync should fail with conflict // Note: The conflict detection happens during sync, but the error may be logged // rather than returned to the user. This test verifies the mechanism exists. sqldb.Close() // The conflict should have been detected (check logs or VFS state) // For now, we just verify the test doesn't crash } // TestVFS_NoConflictWhenRemoteUnchanged verifies no false conflicts. func TestVFS_NoConflictWhenRemoteUnchanged(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) vfs := newWritableVFS(t, client, 100*time.Millisecond, t.TempDir()) vfsName := fmt.Sprintf("litestream-noconflict-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Multiple write/sync cycles - no conflicts expected for i := 2; i <= 5; i++ { _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (?, ?)", i, fmt.Sprintf("User%d", i)) require.NoError(t, err) time.Sleep(200 * time.Millisecond) // Wait for sync } // All data should be present var count int err = sqldb.QueryRow("SELECT COUNT(*) FROM users").Scan(&count) require.NoError(t, err) require.Equal(t, 5, count) } // ============================================================================= // Concurrency Tests // ============================================================================= // TestVFS_ConcurrentReaders tests one writer with multiple readers. func TestVFS_ConcurrentReaders(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) // Writer VFS writerVFS := newWritableVFS(t, client, 100*time.Millisecond, t.TempDir()) writerVFSName := fmt.Sprintf("litestream-writer-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(writerVFSName, writerVFS)) writerDB, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", writerVFSName)) require.NoError(t, err) defer writerDB.Close() // Reader VFS (read-only) readerVFS := newReadOnlyVFS(t, client) readerVFSName := fmt.Sprintf("litestream-reader-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(readerVFSName, readerVFS)) readerDB, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", readerVFSName)) require.NoError(t, err) defer readerDB.Close() // Write some data _, err = writerDB.Exec("INSERT INTO users (id, name) VALUES (2, 'Bob')") require.NoError(t, err) // Wait for sync time.Sleep(300 * time.Millisecond) // Reader should eventually see the data require.Eventually(t, func() bool { var count int if err := readerDB.QueryRow("SELECT COUNT(*) FROM users").Scan(&count); err != nil { return false } return count == 2 }, 5*time.Second, 100*time.Millisecond) } // TestVFS_ReadWhileWriting tests reading during an active write transaction. func TestVFS_ReadWhileWriting(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) vfs := newWritableVFS(t, client, 1*time.Second, t.TempDir()) vfsName := fmt.Sprintf("litestream-readwrite-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() var wg sync.WaitGroup errors := make(chan error, 10) // Writer goroutine wg.Add(1) go func() { defer wg.Done() for i := 2; i <= 20; i++ { if _, err := sqldb.Exec("INSERT INTO users (id, name) VALUES (?, ?)", i, fmt.Sprintf("User%d", i)); err != nil { errors <- err return } time.Sleep(10 * time.Millisecond) } }() // Reader goroutine wg.Add(1) go func() { defer wg.Done() for i := 0; i < 50; i++ { var count int if err := sqldb.QueryRow("SELECT COUNT(*) FROM users").Scan(&count); err != nil { errors <- err return } time.Sleep(5 * time.Millisecond) } }() wg.Wait() close(errors) for err := range errors { t.Errorf("concurrent operation failed: %v", err) } } // ============================================================================= // Edge Case Tests // ============================================================================= // TestVFS_Truncate tests database truncation via VACUUM. func TestVFS_Truncate(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) vfs := newWritableVFS(t, client, 100*time.Millisecond, t.TempDir()) vfsName := fmt.Sprintf("litestream-truncate-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Add many rows for i := 2; i <= 100; i++ { _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (?, ?)", i, fmt.Sprintf("User%d with lots of data to take up space", i)) require.NoError(t, err) } time.Sleep(300 * time.Millisecond) // Delete all but one _, err = sqldb.Exec("DELETE FROM users WHERE id > 1") require.NoError(t, err) // VACUUM to reclaim space _, err = sqldb.Exec("VACUUM") require.NoError(t, err) time.Sleep(300 * time.Millisecond) // Verify only 1 row remains var count int err = sqldb.QueryRow("SELECT COUNT(*) FROM users").Scan(&count) require.NoError(t, err) require.Equal(t, 1, count) } // TestVFS_EmptyTransaction tests begin/commit with no changes. func TestVFS_EmptyTransaction(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) vfs := newWritableVFS(t, client, 1*time.Second, t.TempDir()) vfsName := fmt.Sprintf("litestream-empty-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() initialCount := countLTXFiles(t, client) // Empty transaction tx, err := sqldb.Begin() require.NoError(t, err) err = tx.Commit() require.NoError(t, err) time.Sleep(200 * time.Millisecond) // Should not create new LTX for empty transaction finalCount := countLTXFiles(t, client) require.Equal(t, initialCount, finalCount, "empty transaction should not create new LTX") } // TestVFS_SchemaChanges tests DDL operations. func TestVFS_SchemaChanges(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) vfs := newWritableVFS(t, client, 100*time.Millisecond, t.TempDir()) vfsName := fmt.Sprintf("litestream-schema-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Create table _, err = sqldb.Exec("CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT, price REAL)") require.NoError(t, err) // Add column _, err = sqldb.Exec("ALTER TABLE products ADD COLUMN quantity INTEGER DEFAULT 0") require.NoError(t, err) // Insert data _, err = sqldb.Exec("INSERT INTO products (id, name, price, quantity) VALUES (1, 'Widget', 9.99, 100)") require.NoError(t, err) time.Sleep(300 * time.Millisecond) // Verify schema var qty int err = sqldb.QueryRow("SELECT quantity FROM products WHERE id = 1").Scan(&qty) require.NoError(t, err) require.Equal(t, 100, qty) // Drop table _, err = sqldb.Exec("DROP TABLE products") require.NoError(t, err) time.Sleep(200 * time.Millisecond) // Verify dropped _, err = sqldb.Query("SELECT * FROM products") require.Error(t, err) } // TestVFS_BlobData tests large blob operations. func TestVFS_BlobData(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) vfs := newWritableVFS(t, client, 100*time.Millisecond, t.TempDir()) vfsName := fmt.Sprintf("litestream-blob-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Create table for blobs _, err = sqldb.Exec("CREATE TABLE blobs (id INTEGER PRIMARY KEY, data BLOB)") require.NoError(t, err) // Insert large blob (100KB - spans multiple pages) largeData := make([]byte, 100*1024) for i := range largeData { largeData[i] = byte(i % 256) } _, err = sqldb.Exec("INSERT INTO blobs (id, data) VALUES (1, ?)", largeData) require.NoError(t, err) time.Sleep(300 * time.Millisecond) // Read back and verify var retrieved []byte err = sqldb.QueryRow("SELECT data FROM blobs WHERE id = 1").Scan(&retrieved) require.NoError(t, err) require.Equal(t, largeData, retrieved) } // ============================================================================= // Round-Trip Verification Tests // ============================================================================= // TestVFS_WriteAndRestore tests full write -> sync -> restore cycle. func TestVFS_WriteAndRestore(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) // Write via VFS vfs := newWritableVFS(t, client, 100*time.Millisecond, t.TempDir()) vfsName := fmt.Sprintf("litestream-restore1-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) for i := 2; i <= 10; i++ { _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (?, ?)", i, fmt.Sprintf("User%d", i)) require.NoError(t, err) } sqldb.Close() // Restore to a new file restoredPath := filepath.Join(t.TempDir(), "restored.db") err = restoreDB(t, client, restoredPath) require.NoError(t, err) // Verify restored database restoredDB, err := sql.Open("sqlite3", restoredPath) require.NoError(t, err) defer restoredDB.Close() var count int err = restoredDB.QueryRow("SELECT COUNT(*) FROM users").Scan(&count) require.NoError(t, err) require.Equal(t, 10, count) } // TestVFS_WriteReadVFSOnly tests write via writable VFS, read via read-only VFS. func TestVFS_WriteReadVFSOnly(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) // Write via writable VFS writerVFS := newWritableVFS(t, client, 100*time.Millisecond, t.TempDir()) writerVFSName := fmt.Sprintf("litestream-vfsonly-w-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(writerVFSName, writerVFS)) writerDB, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", writerVFSName)) require.NoError(t, err) _, err = writerDB.Exec("INSERT INTO users (id, name) VALUES (2, 'Bob')") require.NoError(t, err) writerDB.Close() // Read via read-only VFS readerVFS := newReadOnlyVFS(t, client) readerVFSName := fmt.Sprintf("litestream-vfsonly-r-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(readerVFSName, readerVFS)) readerDB, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", readerVFSName)) require.NoError(t, err) defer readerDB.Close() var name string err = readerDB.QueryRow("SELECT name FROM users WHERE id = 2").Scan(&name) require.NoError(t, err) require.Equal(t, "Bob", name) } // TestVFS_MixedWorkload tests interleaved reads/writes/syncs. func TestVFS_MixedWorkload(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) vfs := newWritableVFS(t, client, 200*time.Millisecond, t.TempDir()) vfsName := fmt.Sprintf("litestream-mixed-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Mixed operations for i := 0; i < 50; i++ { switch i % 5 { case 0, 1, 2: // Write _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (?, ?)", i+2, fmt.Sprintf("User%d", i)) require.NoError(t, err) case 3: // Read var count int err = sqldb.QueryRow("SELECT COUNT(*) FROM users").Scan(&count) require.NoError(t, err) case 4: // Update _, err = sqldb.Exec("UPDATE users SET name = ? WHERE id = ?", fmt.Sprintf("Updated%d", i), (i%10)+1) require.NoError(t, err) } } // Final verification var count int err = sqldb.QueryRow("SELECT COUNT(*) FROM users").Scan(&count) require.NoError(t, err) require.GreaterOrEqual(t, count, 30) // At least 30 inserts (0,1,2 mod 5) } // ============================================================================= // Error Handling Tests // ============================================================================= // TestVFS_SyncNetworkError tests handling of network errors during sync. func TestVFS_SyncNetworkError(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) setupInitialDB(t, client) vfs := newWritableVFS(t, client, 1*time.Hour, t.TempDir()) vfsName := fmt.Sprintf("litestream-neterr-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Write data _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (2, 'Bob')") require.NoError(t, err) // Remove replica directory to simulate error require.NoError(t, os.RemoveAll(replicaDir)) // Close should handle error gracefully (sync will fail but shouldn't crash) sqldb.Close() } // TestVFS_InvalidPageSize tests mismatched page size handling. func TestVFS_InvalidPageSize(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) // Create database with different page size db := testingutil.NewDB(t, filepath.Join(t.TempDir(), "source.db")) db.MonitorInterval = 100 * time.Millisecond db.Replica = litestream.NewReplica(db) db.Replica.Client = client require.NoError(t, db.Open()) sqldb0 := testingutil.MustOpenSQLDB(t, db.Path()) // Note: Page size is set at database creation, this is just verifying // the test setup works _, err := sqldb0.Exec("CREATE TABLE test (x)") require.NoError(t, err) waitForLTXFiles(t, client, 10*time.Second, db.MonitorInterval) require.NoError(t, db.Replica.Stop(false)) testingutil.MustCloseSQLDB(t, sqldb0) require.NoError(t, db.Close(context.Background())) // Open via VFS (should work with same page size) vfs := newWritableVFS(t, client, 1*time.Second, t.TempDir()) vfsName := fmt.Sprintf("litestream-pagesize-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Should be able to query var count int err = sqldb.QueryRow("SELECT COUNT(*) FROM test").Scan(&count) require.NoError(t, err) } // TestVFS_RollbackRestoresOriginalState tests that rolling back a transaction // restores the database to its original state, even after large writes. func TestVFS_RollbackRestoresOriginalState(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) // Create database directly via writable VFS (no external setup needed) vfs := newWritableVFS(t, client, 100*time.Millisecond, t.TempDir()) vfsName := fmt.Sprintf("litestream-rollback-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Create initial data via the VFS _, err = sqldb.Exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)") require.NoError(t, err) _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (1, 'Alice')") require.NoError(t, err) // Verify initial state var initialCount int err = sqldb.QueryRow("SELECT COUNT(*) FROM users").Scan(&initialCount) require.NoError(t, err) require.Equal(t, 1, initialCount, "expected 1 initial row") // Start a transaction and write a large amount of data (spanning multiple pages) _, err = sqldb.Exec("BEGIN") require.NoError(t, err) // Insert 1000 rows with large payloads (~1KB each) to span multiple pages for i := 2; i <= 1001; i++ { payload := fmt.Sprintf("rollback_test_user_%d_%s", i, string(make([]byte, 900))) _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (?, ?)", i, payload) require.NoError(t, err) } // Verify the data is visible within the transaction var countDuringTx int err = sqldb.QueryRow("SELECT COUNT(*) FROM users").Scan(&countDuringTx) require.NoError(t, err) require.Equal(t, 1001, countDuringTx, "expected 1001 rows during transaction") // ROLLBACK the transaction _, err = sqldb.Exec("ROLLBACK") require.NoError(t, err) // Verify original state is restored var afterRollbackCount int err = sqldb.QueryRow("SELECT COUNT(*) FROM users").Scan(&afterRollbackCount) require.NoError(t, err) require.Equal(t, 1, afterRollbackCount, "expected 1 row after rollback") // Verify original data is intact var afterRollbackName string err = sqldb.QueryRow("SELECT name FROM users WHERE id = 1").Scan(&afterRollbackName) require.NoError(t, err) require.Equal(t, "Alice", afterRollbackName, "original data should be intact after rollback") } // TestVFS_RollbackAfterUpdate tests that rolling back UPDATE operations // restores the original values. func TestVFS_RollbackAfterUpdate(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) // Create database directly via writable VFS vfs := newWritableVFS(t, client, 100*time.Millisecond, t.TempDir()) vfsName := fmt.Sprintf("litestream-rollback-update-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Create initial data via the VFS _, err = sqldb.Exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)") require.NoError(t, err) _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (1, 'Alice')") require.NoError(t, err) // Get original value var originalName string err = sqldb.QueryRow("SELECT name FROM users WHERE id = 1").Scan(&originalName) require.NoError(t, err) require.Equal(t, "Alice", originalName) // Start transaction and update _, err = sqldb.Exec("BEGIN") require.NoError(t, err) _, err = sqldb.Exec("UPDATE users SET name = 'MODIFIED_' || name") require.NoError(t, err) // Verify modification is visible var modifiedName string err = sqldb.QueryRow("SELECT name FROM users WHERE id = 1").Scan(&modifiedName) require.NoError(t, err) require.Equal(t, "MODIFIED_Alice", modifiedName) // ROLLBACK _, err = sqldb.Exec("ROLLBACK") require.NoError(t, err) // Verify original value is restored var restoredName string err = sqldb.QueryRow("SELECT name FROM users WHERE id = 1").Scan(&restoredName) require.NoError(t, err) require.Equal(t, "Alice", restoredName, "name should be restored after rollback") } // TestVFS_RollbackAfterDelete tests that rolling back DELETE operations // restores the deleted rows. func TestVFS_RollbackAfterDelete(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) // Create database directly via writable VFS vfs := newWritableVFS(t, client, 100*time.Millisecond, t.TempDir()) vfsName := fmt.Sprintf("litestream-rollback-delete-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Create initial data via the VFS _, err = sqldb.Exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)") require.NoError(t, err) _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (1, 'Alice')") require.NoError(t, err) // Verify initial count var initialCount int err = sqldb.QueryRow("SELECT COUNT(*) FROM users").Scan(&initialCount) require.NoError(t, err) require.Equal(t, 1, initialCount) // Start transaction and delete all rows _, err = sqldb.Exec("BEGIN") require.NoError(t, err) _, err = sqldb.Exec("DELETE FROM users") require.NoError(t, err) // Verify deletion var countAfterDelete int err = sqldb.QueryRow("SELECT COUNT(*) FROM users").Scan(&countAfterDelete) require.NoError(t, err) require.Equal(t, 0, countAfterDelete) // ROLLBACK _, err = sqldb.Exec("ROLLBACK") require.NoError(t, err) // Verify rows are restored var restoredCount int err = sqldb.QueryRow("SELECT COUNT(*) FROM users").Scan(&restoredCount) require.NoError(t, err) require.Equal(t, 1, restoredCount, "all rows should be restored after rollback") // Verify the data itself is correct var name string err = sqldb.QueryRow("SELECT name FROM users WHERE id = 1").Scan(&name) require.NoError(t, err) require.Equal(t, "Alice", name) } // TestVFS_CommitAfterRollbackWorks tests that commits work correctly after // a previous rollback in the same session. func TestVFS_CommitAfterRollbackWorks(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) // Create database directly via writable VFS vfs := newWritableVFS(t, client, 100*time.Millisecond, t.TempDir()) vfsName := fmt.Sprintf("litestream-commit-after-rollback-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName, vfs)) sqldb, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName)) require.NoError(t, err) defer sqldb.Close() // Create initial data via the VFS _, err = sqldb.Exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)") require.NoError(t, err) _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (1, 'Alice')") require.NoError(t, err) // Transaction 1: Insert and rollback _, err = sqldb.Exec("BEGIN") require.NoError(t, err) _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (100, 'should_not_exist')") require.NoError(t, err) _, err = sqldb.Exec("ROLLBACK") require.NoError(t, err) // Transaction 2: Insert and commit _, err = sqldb.Exec("BEGIN") require.NoError(t, err) _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (200, 'should_exist')") require.NoError(t, err) _, err = sqldb.Exec("COMMIT") require.NoError(t, err) // Verify only the committed data exists var count int err = sqldb.QueryRow("SELECT COUNT(*) FROM users WHERE name = 'should_not_exist'").Scan(&count) require.NoError(t, err) require.Equal(t, 0, count, "rolled back data should not exist") err = sqldb.QueryRow("SELECT COUNT(*) FROM users WHERE name = 'should_exist'").Scan(&count) require.NoError(t, err) require.Equal(t, 1, count, "committed data should exist") // Close and reopen to verify persistence sqldb.Close() vfs2 := newWritableVFS(t, client, 0, "") vfsName2 := fmt.Sprintf("litestream-verify-%d", time.Now().UnixNano()) require.NoError(t, sqlite3vfs.RegisterVFS(vfsName2, vfs2)) sqldb2, err := sql.Open("sqlite3", fmt.Sprintf("file:test.db?vfs=%s", vfsName2)) require.NoError(t, err) defer sqldb2.Close() err = sqldb2.QueryRow("SELECT COUNT(*) FROM users WHERE name = 'should_exist'").Scan(&count) require.NoError(t, err) require.Equal(t, 1, count, "committed data should persist") } // ============================================================================= // Helper Functions // ============================================================================= // newWritableVFS creates a VFS with write support enabled. func newWritableVFS(tb testing.TB, client litestream.ReplicaClient, syncInterval time.Duration, localPath string) *litestream.VFS { tb.Helper() logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ Level: slog.LevelDebug, })) vfs := litestream.NewVFS(client, logger) vfs.PollInterval = 100 * time.Millisecond vfs.WriteEnabled = true vfs.WriteSyncInterval = syncInterval // If localPath is provided as a directory, append a buffer filename if localPath != "" { vfs.WriteBufferPath = filepath.Join(localPath, ".litestream-buffer") } return vfs } // newReadOnlyVFS creates a read-only VFS. func newReadOnlyVFS(tb testing.TB, client litestream.ReplicaClient) *litestream.VFS { tb.Helper() logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ Level: slog.LevelDebug, })) vfs := litestream.NewVFS(client, logger) vfs.PollInterval = 100 * time.Millisecond return vfs } // setupInitialDB creates an initial database with standard schema. func setupInitialDB(t *testing.T, client litestream.ReplicaClient) { t.Helper() dbDir := t.TempDir() db := testingutil.NewDB(t, filepath.Join(dbDir, "source.db")) db.MonitorInterval = 50 * time.Millisecond db.Replica = litestream.NewReplica(db) db.Replica.Client = client db.Replica.SyncInterval = 50 * time.Millisecond require.NoError(t, db.Open()) sqldb := testingutil.MustOpenSQLDB(t, db.Path()) _, err := sqldb.Exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)") require.NoError(t, err) _, err = sqldb.Exec("INSERT INTO users (id, name) VALUES (1, 'Alice')") require.NoError(t, err) // Wait for initial LTX file waitForLTXFiles(t, client, 10*time.Second, db.MonitorInterval) // Force a DB sync and then replica sync to ensure all data is uploaded require.NoError(t, db.Sync(context.Background())) require.NoError(t, db.Replica.Sync(context.Background())) require.NoError(t, db.Replica.Stop(false)) testingutil.MustCloseSQLDB(t, sqldb) require.NoError(t, db.Close(context.Background())) } // countLTXFiles returns the number of LTX files in the replica. func countLTXFiles(t *testing.T, client litestream.ReplicaClient) int { t.Helper() itr, err := client.LTXFiles(context.Background(), 0, 0, false) require.NoError(t, err) defer itr.Close() count := 0 for itr.Next() { count++ } return count } // addExternalLTXFile adds an LTX file to simulate an external writer. func addExternalLTXFile(t *testing.T, client litestream.ReplicaClient, replicaDir string) { t.Helper() // Get current max TXID itr, err := client.LTXFiles(context.Background(), 0, 0, false) require.NoError(t, err) var maxTXID ltx.TXID for itr.Next() { if itr.Item().MaxTXID > maxTXID { maxTXID = itr.Item().MaxTXID } } itr.Close() // Create a new LTX file with next TXID nextTXID := maxTXID + 1 var buf bytes.Buffer enc, err := ltx.NewEncoder(&buf) require.NoError(t, err) require.NoError(t, enc.EncodeHeader(ltx.Header{ Version: ltx.Version, Flags: ltx.HeaderFlagNoChecksum, PageSize: 4096, Commit: 2, MinTXID: nextTXID, MaxTXID: nextTXID, Timestamp: time.Now().UnixMilli(), })) // Encode a dummy page page := make([]byte, 4096) require.NoError(t, enc.EncodePage(ltx.PageHeader{Pgno: 2}, page)) require.NoError(t, enc.Close()) // Write via client _, err = client.WriteLTXFile(context.Background(), 0, nextTXID, nextTXID, &buf) require.NoError(t, err) } // restoreDB restores a database from the replica to the given path. func restoreDB(t *testing.T, client litestream.ReplicaClient, outputPath string) error { t.Helper() // Open output file f, err := os.Create(outputPath) if err != nil { return err } defer f.Close() // Get all LTX files itr, err := client.LTXFiles(context.Background(), 0, 0, false) if err != nil { return err } defer itr.Close() var pageSize uint32 pages := make(map[uint32][]byte) var commit uint32 for itr.Next() { info := itr.Item() rc, err := client.OpenLTXFile(context.Background(), info.Level, info.MinTXID, info.MaxTXID, 0, 0) if err != nil { return err } dec := ltx.NewDecoder(rc) if err := dec.DecodeHeader(); err != nil { rc.Close() return err } hdr := dec.Header() if pageSize == 0 { pageSize = hdr.PageSize } commit = hdr.Commit pageBuf := make([]byte, hdr.PageSize) for { var phdr ltx.PageHeader if err := dec.DecodePage(&phdr, pageBuf); err != nil { break } // Copy page data since pageBuf is reused data := make([]byte, len(pageBuf)) copy(data, pageBuf) pages[phdr.Pgno] = data } rc.Close() } // Write pages to file for pgno := uint32(1); pgno <= commit; pgno++ { data, ok := pages[pgno] if !ok { data = make([]byte, pageSize) } if _, err := f.WriteAt(data, int64(pgno-1)*int64(pageSize)); err != nil { return err } } return nil } ================================================ FILE: compaction_level.go ================================================ package litestream import ( "fmt" "time" ) // SnapshotLevel represents the level which full snapshots are held. const SnapshotLevel = 9 // DefaultCompactionLevels provides the canonical default compaction configuration. // Level 0 is raw LTX files, higher levels compact at increasing intervals. // These values are also used by cmd/litestream DefaultConfig(). var DefaultCompactionLevels = CompactionLevels{ {Level: 0, Interval: 0}, {Level: 1, Interval: 30 * time.Second}, {Level: 2, Interval: 5 * time.Minute}, {Level: 3, Interval: time.Hour}, } // CompactionLevel represents a single part of a multi-level compaction. // Each level merges LTX files from the previous level into larger time granularities. type CompactionLevel struct { // The numeric level. Must match the index in the list of levels. Level int // The frequency that the level is compacted from the previous level. Interval time.Duration } // PrevCompactionAt returns the time when the last compaction occurred. // Returns the current time if it is exactly a multiple of the level interval. func (lvl *CompactionLevel) PrevCompactionAt(now time.Time) time.Time { return now.Truncate(lvl.Interval).UTC() } // NextCompactionAt returns the time until the next compaction occurs. // Returns the current time if it is exactly a multiple of the level interval. func (lvl *CompactionLevel) NextCompactionAt(now time.Time) time.Time { return lvl.PrevCompactionAt(now).Add(lvl.Interval) } // CompactionLevels represents a sorted slice of non-snapshot compaction levels. type CompactionLevels []*CompactionLevel // Level returns the compaction level at the given index. // Returns an error if the index is a snapshot level or is out of bounds. func (a CompactionLevels) Level(level int) (*CompactionLevel, error) { if level == SnapshotLevel { return nil, fmt.Errorf("invalid argument, snapshot level") } if level < 0 || level > a.MaxLevel() { return nil, fmt.Errorf("level out of bounds: %d", level) } return a[level], nil } // MaxLevel return the highest non-snapshot compaction level. func (a CompactionLevels) MaxLevel() int { return len(a) - 1 } // Validate returns an error if the levels are invalid. func (a CompactionLevels) Validate() error { if len(a) == 0 { return fmt.Errorf("at least one compaction level is required") } for i, lvl := range a { if i != lvl.Level { return fmt.Errorf("compaction level number out of order: %d, expected %d", lvl.Level, i) } else if lvl.Level > SnapshotLevel-1 { return fmt.Errorf("compaction level cannot exceed %d", SnapshotLevel-1) } if lvl.Level == 0 && lvl.Interval != 0 { return fmt.Errorf("cannot set interval on compaction level zero") } if lvl.Level != 0 && lvl.Interval <= 0 { return fmt.Errorf("interval required for level %d", lvl.Level) } } return nil } // IsValidLevel returns true if level is a valid compaction level number. func (a CompactionLevels) IsValidLevel(level int) bool { if level == SnapshotLevel { return true } return level >= 0 && level < len(a) } // PrevLevel returns the previous compaction level. // Returns -1 if there is no previous level. func (a CompactionLevels) PrevLevel(level int) int { if level == SnapshotLevel { return a.MaxLevel() } return level - 1 } // NextLevel returns the next compaction level. // Returns -1 if there is no next level. func (a CompactionLevels) NextLevel(level int) int { if level == SnapshotLevel { return -1 } else if level == a.MaxLevel() { return SnapshotLevel } return level + 1 } ================================================ FILE: compactor.go ================================================ package litestream import ( "context" "fmt" "io" "log/slog" "os" "time" "github.com/prometheus/client_golang/prometheus" "github.com/superfly/ltx" ) // Compactor handles compaction and retention for LTX files. // It operates solely through the ReplicaClient interface, making it // suitable for both DB (with local file caching) and VFS (remote-only). type Compactor struct { client ReplicaClient logger *slog.Logger // VerifyCompaction enables post-compaction TXID consistency verification. // When enabled, verifies that files at the destination level have // contiguous TXID ranges after each compaction. Disabled by default. VerifyCompaction bool // RetentionEnabled controls whether Litestream actively deletes old files // during retention enforcement. When false, cloud provider lifecycle // policies handle retention instead. Local file cleanup still occurs. RetentionEnabled bool // CompactionVerifyErrorCounter is incremented when post-compaction // verification fails. Optional; if nil, no metric is recorded. CompactionVerifyErrorCounter prometheus.Counter // LocalFileOpener optionally opens a local LTX file for compaction. // If nil or returns os.ErrNotExist, falls back to remote. // This is used by DB to prefer local files over remote for consistency. LocalFileOpener func(level int, minTXID, maxTXID ltx.TXID) (io.ReadCloser, error) // LocalFileDeleter optionally deletes local LTX files after retention. // If nil, only remote files are deleted. LocalFileDeleter func(level int, minTXID, maxTXID ltx.TXID) error // CacheGetter optionally retrieves cached MaxLTXFileInfo for a level. // If nil, max file info is always fetched from remote. CacheGetter func(level int) (*ltx.FileInfo, bool) // CacheSetter optionally stores MaxLTXFileInfo for a level. // If nil, max file info is not cached. CacheSetter func(level int, info *ltx.FileInfo) } // NewCompactor creates a new Compactor with the given client and logger. func NewCompactor(client ReplicaClient, logger *slog.Logger) *Compactor { if logger == nil { logger = slog.Default() } return &Compactor{ client: client, logger: logger, RetentionEnabled: true, } } func (c *Compactor) setLogger(logger *slog.Logger) { c.logger = logger } // MaxLTXFileInfo returns metadata for the last LTX file in a level. // Uses cache if available, otherwise fetches from remote. func (c *Compactor) MaxLTXFileInfo(ctx context.Context, level int) (ltx.FileInfo, error) { if c.CacheGetter != nil { if info, ok := c.CacheGetter(level); ok { return *info, nil } } itr, err := c.client.LTXFiles(ctx, level, 0, false) if err != nil { return ltx.FileInfo{}, err } defer itr.Close() var info ltx.FileInfo for itr.Next() { item := itr.Item() if item.MaxTXID > info.MaxTXID { info = *item } } if c.CacheSetter != nil && info.MaxTXID > 0 { c.CacheSetter(level, &info) } return info, itr.Close() } // Compact compacts source level files into the destination level. // Returns ErrNoCompaction if there are no files to compact. func (c *Compactor) Compact(ctx context.Context, dstLevel int) (*ltx.FileInfo, error) { srcLevel := dstLevel - 1 prevMaxInfo, err := c.MaxLTXFileInfo(ctx, dstLevel) if err != nil { return nil, fmt.Errorf("cannot determine max ltx file for destination level: %w", err) } seekTXID := prevMaxInfo.MaxTXID + 1 itr, err := c.client.LTXFiles(ctx, srcLevel, seekTXID, false) if err != nil { return nil, fmt.Errorf("source ltx files after %s: %w", seekTXID, err) } defer itr.Close() var rdrs []io.Reader defer func() { for _, rd := range rdrs { if closer, ok := rd.(io.Closer); ok { _ = closer.Close() } } }() var minTXID, maxTXID ltx.TXID for itr.Next() { info := itr.Item() if minTXID == 0 || info.MinTXID < minTXID { minTXID = info.MinTXID } if maxTXID == 0 || info.MaxTXID > maxTXID { maxTXID = info.MaxTXID } if c.LocalFileOpener != nil { if f, err := c.LocalFileOpener(srcLevel, info.MinTXID, info.MaxTXID); err == nil { rdrs = append(rdrs, f) continue } else if !os.IsNotExist(err) { return nil, fmt.Errorf("open local ltx file: %w", err) } } f, err := c.client.OpenLTXFile(ctx, info.Level, info.MinTXID, info.MaxTXID, 0, 0) if err != nil { return nil, fmt.Errorf("open ltx file: %w", err) } rdrs = append(rdrs, f) } if len(rdrs) == 0 { return nil, ErrNoCompaction } pr, pw := io.Pipe() go func() { comp, err := ltx.NewCompactor(pw, rdrs) if err != nil { pw.CloseWithError(fmt.Errorf("new ltx compactor: %w", err)) return } comp.HeaderFlags = ltx.HeaderFlagNoChecksum _ = pw.CloseWithError(comp.Compact(ctx)) }() info, err := c.client.WriteLTXFile(ctx, dstLevel, minTXID, maxTXID, pr) if err != nil { return nil, fmt.Errorf("write ltx file: %w", err) } if c.CacheSetter != nil { c.CacheSetter(dstLevel, info) } // Verify level consistency if enabled if c.VerifyCompaction { if err := c.VerifyLevelConsistency(ctx, dstLevel); err != nil { c.logger.Warn("post-compaction verification failed", "level", dstLevel, "error", err) if c.CompactionVerifyErrorCounter != nil { c.CompactionVerifyErrorCounter.Inc() } } } return info, nil } // VerifyLevelConsistency checks that LTX files at the given level have // contiguous TXID ranges (prevMaxTXID + 1 == currMinTXID for consecutive files). // Returns an error describing any gaps or overlaps found. func (c *Compactor) VerifyLevelConsistency(ctx context.Context, level int) error { itr, err := c.client.LTXFiles(ctx, level, 0, false) if err != nil { return fmt.Errorf("fetch ltx files: %w", err) } defer itr.Close() var prevInfo *ltx.FileInfo for itr.Next() { info := itr.Item() // Skip first file - nothing to compare against if prevInfo == nil { prevInfo = info continue } // Check for TXID contiguity: prev.MaxTXID + 1 should equal curr.MinTXID expectedMinTXID := prevInfo.MaxTXID + 1 if info.MinTXID != expectedMinTXID { if info.MinTXID > expectedMinTXID { return fmt.Errorf("TXID gap detected: prev.MaxTXID=%s, next.MinTXID=%s (expected %s)", prevInfo.MaxTXID, info.MinTXID, expectedMinTXID) } return fmt.Errorf("TXID overlap detected: prev.MaxTXID=%s, next.MinTXID=%s", prevInfo.MaxTXID, info.MinTXID) } prevInfo = info } if err := itr.Close(); err != nil { return fmt.Errorf("close iterator: %w", err) } return nil } // EnforceSnapshotRetention enforces retention of snapshot level files by timestamp. // Files older than the retention duration are deleted (except the newest is always kept). // Returns the minimum snapshot TXID still retained (useful for cascading retention to lower levels). func (c *Compactor) EnforceSnapshotRetention(ctx context.Context, retention time.Duration) (ltx.TXID, error) { timestamp := time.Now().Add(-retention) c.logger.Debug("enforcing snapshot retention", "timestamp", timestamp) itr, err := c.client.LTXFiles(ctx, SnapshotLevel, 0, false) if err != nil { return 0, fmt.Errorf("fetch ltx files: %w", err) } defer itr.Close() var deleted []*ltx.FileInfo var lastInfo *ltx.FileInfo var minSnapshotTXID ltx.TXID for itr.Next() { info := itr.Item() lastInfo = info if info.CreatedAt.Before(timestamp) { deleted = append(deleted, info) continue } if minSnapshotTXID == 0 || info.MaxTXID < minSnapshotTXID { minSnapshotTXID = info.MaxTXID } } if len(deleted) > 0 && deleted[len(deleted)-1] == lastInfo { deleted = deleted[:len(deleted)-1] } if !c.RetentionEnabled { c.logger.Debug("skipping remote deletion (retention disabled)", "level", SnapshotLevel, "count", len(deleted)) } else if err := c.client.DeleteLTXFiles(ctx, deleted); err != nil { return 0, fmt.Errorf("remove ltx files: %w", err) } if c.LocalFileDeleter != nil { for _, info := range deleted { c.logger.Debug("deleting local ltx file", "level", SnapshotLevel, "minTXID", info.MinTXID, "maxTXID", info.MaxTXID) if err := c.LocalFileDeleter(SnapshotLevel, info.MinTXID, info.MaxTXID); err != nil { c.logger.Error("failed to remove local ltx file", "error", err) } } } return minSnapshotTXID, nil } // EnforceRetentionByTXID deletes files at the given level with maxTXID below the target. // Always keeps at least one file. func (c *Compactor) EnforceRetentionByTXID(ctx context.Context, level int, txID ltx.TXID) error { c.logger.Debug("enforcing retention", "level", level, "txid", txID) itr, err := c.client.LTXFiles(ctx, level, 0, false) if err != nil { return fmt.Errorf("fetch ltx files: %w", err) } defer itr.Close() var deleted []*ltx.FileInfo var lastInfo *ltx.FileInfo for itr.Next() { info := itr.Item() lastInfo = info if info.MaxTXID < txID { deleted = append(deleted, info) continue } } if len(deleted) > 0 && deleted[len(deleted)-1] == lastInfo { deleted = deleted[:len(deleted)-1] } if !c.RetentionEnabled { c.logger.Debug("skipping remote deletion (retention disabled)", "level", level, "count", len(deleted)) } else if err := c.client.DeleteLTXFiles(ctx, deleted); err != nil { return fmt.Errorf("remove ltx files: %w", err) } if c.LocalFileDeleter != nil { for _, info := range deleted { c.logger.Debug("deleting local ltx file", "level", level, "minTXID", info.MinTXID, "maxTXID", info.MaxTXID) if err := c.LocalFileDeleter(level, info.MinTXID, info.MaxTXID); err != nil { c.logger.Error("failed to remove local ltx file", "error", err) } } } return nil } // EnforceL0Retention retains L0 files based on L1 compaction progress and time. // Files are only deleted if they have been compacted into L1 AND are older than retention. // This ensures contiguous L0 coverage for VFS reads. func (c *Compactor) EnforceL0Retention(ctx context.Context, retention time.Duration) error { if retention <= 0 { return nil } c.logger.Debug("enforcing l0 retention", "retention", retention) itr, err := c.client.LTXFiles(ctx, 1, 0, false) if err != nil { return fmt.Errorf("fetch l1 files: %w", err) } var maxL1TXID ltx.TXID for itr.Next() { info := itr.Item() if info.MaxTXID > maxL1TXID { maxL1TXID = info.MaxTXID } } if err := itr.Close(); err != nil { return fmt.Errorf("close l1 iterator: %w", err) } if maxL1TXID == 0 { return nil } threshold := time.Now().Add(-retention) itr, err = c.client.LTXFiles(ctx, 0, 0, false) if err != nil { return fmt.Errorf("fetch l0 files: %w", err) } defer itr.Close() var ( deleted []*ltx.FileInfo lastInfo *ltx.FileInfo processedAll = true ) for itr.Next() { info := itr.Item() lastInfo = info createdAt := info.CreatedAt if createdAt.IsZero() { createdAt = threshold } if createdAt.After(threshold) { processedAll = false break } if info.MaxTXID <= maxL1TXID { deleted = append(deleted, info) } } if processedAll && len(deleted) > 0 && lastInfo != nil && deleted[len(deleted)-1] == lastInfo { deleted = deleted[:len(deleted)-1] } if len(deleted) == 0 { return nil } if !c.RetentionEnabled { c.logger.Debug("skipping remote deletion (retention disabled)", "level", 0, "count", len(deleted)) } else if err := c.client.DeleteLTXFiles(ctx, deleted); err != nil { return fmt.Errorf("remove expired l0 files: %w", err) } if c.LocalFileDeleter != nil { for _, info := range deleted { c.logger.Debug("deleting expired local l0 file", "minTXID", info.MinTXID, "maxTXID", info.MaxTXID) if err := c.LocalFileDeleter(0, info.MinTXID, info.MaxTXID); err != nil { c.logger.Error("failed to remove local l0 file", "error", err) } } } c.logger.Info("l0 retention enforced", "deleted_count", len(deleted), "max_l1_txid", maxL1TXID) return nil } ================================================ FILE: compactor_test.go ================================================ package litestream_test import ( "bytes" "context" "io" "log/slog" "testing" "time" "github.com/superfly/ltx" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/file" ) func TestCompactor_Compact(t *testing.T) { t.Run("L0ToL1", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) // Create test L0 files createTestLTXFile(t, client, 0, 1, 1) createTestLTXFile(t, client, 0, 2, 2) info, err := compactor.Compact(context.Background(), 1) if err != nil { t.Fatal(err) } if info.Level != 1 { t.Errorf("Level=%d, want 1", info.Level) } if info.MinTXID != 1 || info.MaxTXID != 2 { t.Errorf("TXID range=%d-%d, want 1-2", info.MinTXID, info.MaxTXID) } }) t.Run("NoFiles", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) _, err := compactor.Compact(context.Background(), 1) if err != litestream.ErrNoCompaction { t.Errorf("err=%v, want ErrNoCompaction", err) } }) t.Run("L1ToL2", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) // Create L0 files createTestLTXFile(t, client, 0, 1, 1) createTestLTXFile(t, client, 0, 2, 2) // Compact to L1 _, err := compactor.Compact(context.Background(), 1) if err != nil { t.Fatal(err) } // Create more L0 files createTestLTXFile(t, client, 0, 3, 3) // Compact to L1 again (should only include TXID 3) info, err := compactor.Compact(context.Background(), 1) if err != nil { t.Fatal(err) } if info.MinTXID != 3 || info.MaxTXID != 3 { t.Errorf("TXID range=%d-%d, want 3-3", info.MinTXID, info.MaxTXID) } // Now compact L1 to L2 (should include all from 1-3) info, err = compactor.Compact(context.Background(), 2) if err != nil { t.Fatal(err) } if info.Level != 2 { t.Errorf("Level=%d, want 2", info.Level) } if info.MinTXID != 1 || info.MaxTXID != 3 { t.Errorf("TXID range=%d-%d, want 1-3", info.MinTXID, info.MaxTXID) } }) } func TestCompactor_MaxLTXFileInfo(t *testing.T) { t.Run("WithFiles", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) createTestLTXFile(t, client, 0, 1, 1) createTestLTXFile(t, client, 0, 2, 2) createTestLTXFile(t, client, 0, 3, 5) info, err := compactor.MaxLTXFileInfo(context.Background(), 0) if err != nil { t.Fatal(err) } if info.MaxTXID != 5 { t.Errorf("MaxTXID=%d, want 5", info.MaxTXID) } }) t.Run("NoFiles", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) info, err := compactor.MaxLTXFileInfo(context.Background(), 0) if err != nil { t.Fatal(err) } if info.MaxTXID != 0 { t.Errorf("MaxTXID=%d, want 0", info.MaxTXID) } }) t.Run("WithCache", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) // Use callbacks for caching cache := make(map[int]*ltx.FileInfo) compactor.CacheGetter = func(level int) (*ltx.FileInfo, bool) { info, ok := cache[level] return info, ok } compactor.CacheSetter = func(level int, info *ltx.FileInfo) { cache[level] = info } createTestLTXFile(t, client, 0, 1, 3) // First call should populate cache info, err := compactor.MaxLTXFileInfo(context.Background(), 0) if err != nil { t.Fatal(err) } if info.MaxTXID != 3 { t.Errorf("MaxTXID=%d, want 3", info.MaxTXID) } // Second call should use cache info, err = compactor.MaxLTXFileInfo(context.Background(), 0) if err != nil { t.Fatal(err) } if info.MaxTXID != 3 { t.Errorf("MaxTXID=%d, want 3 (from cache)", info.MaxTXID) } }) } func TestCompactor_EnforceRetentionByTXID(t *testing.T) { t.Run("DeletesOldFiles", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) // Create files at L1 createTestLTXFile(t, client, 1, 1, 2) createTestLTXFile(t, client, 1, 3, 5) createTestLTXFile(t, client, 1, 6, 10) // Enforce retention - delete files below TXID 5 err := compactor.EnforceRetentionByTXID(context.Background(), 1, 5) if err != nil { t.Fatal(err) } // Verify only the first file was deleted info, err := compactor.MaxLTXFileInfo(context.Background(), 1) if err != nil { t.Fatal(err) } if info.MaxTXID != 10 { t.Errorf("MaxTXID=%d, want 10", info.MaxTXID) } // Check that files starting from TXID 3 are still present itr, err := client.LTXFiles(context.Background(), 1, 0, false) if err != nil { t.Fatal(err) } defer itr.Close() var count int for itr.Next() { count++ } if count != 2 { t.Errorf("file count=%d, want 2", count) } }) t.Run("KeepsLastFile", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) // Create single file createTestLTXFile(t, client, 1, 1, 2) // Try to delete it - should keep at least one err := compactor.EnforceRetentionByTXID(context.Background(), 1, 100) if err != nil { t.Fatal(err) } // Verify file still exists info, err := compactor.MaxLTXFileInfo(context.Background(), 1) if err != nil { t.Fatal(err) } if info.MaxTXID != 2 { t.Errorf("MaxTXID=%d, want 2 (last file should be kept)", info.MaxTXID) } }) } func TestCompactor_EnforceL0Retention(t *testing.T) { t.Run("DeletesCompactedFiles", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) // Create L0 files createTestLTXFile(t, client, 0, 1, 1) createTestLTXFile(t, client, 0, 2, 2) createTestLTXFile(t, client, 0, 3, 3) // Compact to L1 _, err := compactor.Compact(context.Background(), 1) if err != nil { t.Fatal(err) } // Enforce L0 retention with 0 duration (delete immediately) err = compactor.EnforceL0Retention(context.Background(), 0) if err != nil { t.Fatal(err) } // L0 files compacted into L1 should be deleted (except last) itr, err := client.LTXFiles(context.Background(), 0, 0, false) if err != nil { t.Fatal(err) } defer itr.Close() var count int for itr.Next() { count++ } // At least one file should remain if count < 1 { t.Errorf("file count=%d, want at least 1", count) } }) t.Run("SkipsIfNoL1", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) // Create L0 files without compacting to L1 createTestLTXFile(t, client, 0, 1, 1) createTestLTXFile(t, client, 0, 2, 2) // Enforce L0 retention - should do nothing since no L1 exists err := compactor.EnforceL0Retention(context.Background(), 0) if err != nil { t.Fatal(err) } // All L0 files should still exist itr, err := client.LTXFiles(context.Background(), 0, 0, false) if err != nil { t.Fatal(err) } defer itr.Close() var count int for itr.Next() { count++ } if count != 2 { t.Errorf("file count=%d, want 2", count) } }) } func TestCompactor_EnforceSnapshotRetention(t *testing.T) { t.Run("DeletesOldSnapshots", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) // Create snapshot files with different timestamps createTestLTXFileWithTimestamp(t, client, litestream.SnapshotLevel, 1, 5, time.Now().Add(-2*time.Hour)) createTestLTXFileWithTimestamp(t, client, litestream.SnapshotLevel, 1, 10, time.Now().Add(-30*time.Minute)) createTestLTXFileWithTimestamp(t, client, litestream.SnapshotLevel, 1, 15, time.Now().Add(-5*time.Minute)) // Enforce retention - keep snapshots from last hour _, err := compactor.EnforceSnapshotRetention(context.Background(), time.Hour) if err != nil { t.Fatal(err) } // Count remaining snapshots itr, err := client.LTXFiles(context.Background(), litestream.SnapshotLevel, 0, false) if err != nil { t.Fatal(err) } defer itr.Close() var count int for itr.Next() { count++ } // Should have 2 snapshots (the 30min and 5min old ones) if count != 2 { t.Errorf("snapshot count=%d, want 2", count) } }) } func TestCompactor_EnforceSnapshotRetention_RetentionDisabled(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) compactor.RetentionEnabled = false var localDeleted []ltx.TXID compactor.LocalFileDeleter = func(level int, minTXID, maxTXID ltx.TXID) error { localDeleted = append(localDeleted, maxTXID) return nil } createTestLTXFileWithTimestamp(t, client, litestream.SnapshotLevel, 1, 5, time.Now().Add(-2*time.Hour)) createTestLTXFileWithTimestamp(t, client, litestream.SnapshotLevel, 1, 10, time.Now().Add(-30*time.Minute)) createTestLTXFileWithTimestamp(t, client, litestream.SnapshotLevel, 1, 15, time.Now().Add(-5*time.Minute)) _, err := compactor.EnforceSnapshotRetention(context.Background(), time.Hour) if err != nil { t.Fatal(err) } // Remote files should all still exist (skip remote deletion). itr, err := client.LTXFiles(context.Background(), litestream.SnapshotLevel, 0, false) if err != nil { t.Fatal(err) } defer itr.Close() var count int for itr.Next() { count++ } if count != 3 { t.Errorf("remote file count=%d, want 3 (no remote deletion)", count) } // Local file deleter should still have been called. if len(localDeleted) != 1 { t.Errorf("local deleted count=%d, want 1", len(localDeleted)) } } func TestCompactor_EnforceRetentionByTXID_RetentionDisabled(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) compactor.RetentionEnabled = false var localDeleted []ltx.TXID compactor.LocalFileDeleter = func(level int, minTXID, maxTXID ltx.TXID) error { localDeleted = append(localDeleted, maxTXID) return nil } createTestLTXFile(t, client, 1, 1, 2) createTestLTXFile(t, client, 1, 3, 5) createTestLTXFile(t, client, 1, 6, 10) err := compactor.EnforceRetentionByTXID(context.Background(), 1, 5) if err != nil { t.Fatal(err) } // Remote files should all still exist. itr, err := client.LTXFiles(context.Background(), 1, 0, false) if err != nil { t.Fatal(err) } defer itr.Close() var count int for itr.Next() { count++ } if count != 3 { t.Errorf("remote file count=%d, want 3 (no remote deletion)", count) } // Local file deleter should still have been called for the file below TXID 5. if len(localDeleted) != 1 { t.Errorf("local deleted count=%d, want 1", len(localDeleted)) } } func TestCompactor_EnforceL0Retention_RetentionDisabled(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) compactor.RetentionEnabled = false var localDeleted []ltx.TXID compactor.LocalFileDeleter = func(level int, minTXID, maxTXID ltx.TXID) error { localDeleted = append(localDeleted, maxTXID) return nil } // Create L0 files with old timestamps so they're eligible for deletion. oldTime := time.Now().Add(-1 * time.Hour) createTestLTXFileWithTimestamp(t, client, 0, 1, 1, oldTime) createTestLTXFileWithTimestamp(t, client, 0, 2, 2, oldTime) createTestLTXFileWithTimestamp(t, client, 0, 3, 3, oldTime) // Compact to L1 first. _, err := compactor.Compact(context.Background(), 1) if err != nil { t.Fatal(err) } // Use a real retention duration so the check doesn't return early. err = compactor.EnforceL0Retention(context.Background(), time.Minute) if err != nil { t.Fatal(err) } // Remote L0 files should all still exist. itr, err := client.LTXFiles(context.Background(), 0, 0, false) if err != nil { t.Fatal(err) } defer itr.Close() var count int for itr.Next() { count++ } if count != 3 { t.Errorf("remote file count=%d, want 3 (no remote deletion)", count) } // Local file deleter should still have been called for compacted files. if len(localDeleted) < 1 { t.Errorf("local deleted count=%d, want at least 1", len(localDeleted)) } } func TestCompactor_VerifyLevelConsistency(t *testing.T) { t.Run("ContiguousFiles", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) // Create contiguous files createTestLTXFile(t, client, 1, 1, 2) createTestLTXFile(t, client, 1, 3, 5) createTestLTXFile(t, client, 1, 6, 10) // Should pass verification err := compactor.VerifyLevelConsistency(context.Background(), 1) if err != nil { t.Errorf("expected nil error for contiguous files, got: %v", err) } }) t.Run("GapDetected", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) // Create files with a gap (missing TXID 3-4) createTestLTXFile(t, client, 1, 1, 2) createTestLTXFile(t, client, 1, 5, 7) // gap: expected MinTXID=3, got 5 err := compactor.VerifyLevelConsistency(context.Background(), 1) if err == nil { t.Error("expected error for gap in files, got nil") } if err != nil && !containsString(err.Error(), "gap") { t.Errorf("expected gap error, got: %v", err) } }) t.Run("OverlapDetected", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) // Create overlapping files createTestLTXFile(t, client, 1, 1, 5) createTestLTXFile(t, client, 1, 3, 7) // overlap: expected MinTXID=6, got 3 err := compactor.VerifyLevelConsistency(context.Background(), 1) if err == nil { t.Error("expected error for overlapping files, got nil") } if err != nil && !containsString(err.Error(), "overlap") { t.Errorf("expected overlap error, got: %v", err) } }) t.Run("SingleFile", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) // Create single file - should pass createTestLTXFile(t, client, 1, 1, 5) err := compactor.VerifyLevelConsistency(context.Background(), 1) if err != nil { t.Errorf("expected nil error for single file, got: %v", err) } }) t.Run("EmptyLevel", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) // Empty level - should pass err := compactor.VerifyLevelConsistency(context.Background(), 1) if err != nil { t.Errorf("expected nil error for empty level, got: %v", err) } }) } func TestCompactor_CompactWithVerification(t *testing.T) { t.Run("VerificationEnabled", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) compactor := litestream.NewCompactor(client, slog.Default()) compactor.VerifyCompaction = true // Create contiguous L0 files createTestLTXFile(t, client, 0, 1, 1) createTestLTXFile(t, client, 0, 2, 2) createTestLTXFile(t, client, 0, 3, 3) // Compact to L1 - should succeed with verification info, err := compactor.Compact(context.Background(), 1) if err != nil { t.Fatal(err) } if info.Level != 1 { t.Errorf("Level=%d, want 1", info.Level) } if info.MinTXID != 1 || info.MaxTXID != 3 { t.Errorf("TXID range=%d-%d, want 1-3", info.MinTXID, info.MaxTXID) } }) } // containsString checks if s contains substr. func containsString(s, substr string) bool { return bytes.Contains([]byte(s), []byte(substr)) } // createTestLTXFile creates a minimal LTX file for testing. func createTestLTXFile(t testing.TB, client litestream.ReplicaClient, level int, minTXID, maxTXID ltx.TXID) { t.Helper() createTestLTXFileWithTimestamp(t, client, level, minTXID, maxTXID, time.Now()) } // createTestLTXFileWithTimestamp creates a minimal LTX file with a specific timestamp. func createTestLTXFileWithTimestamp(t testing.TB, client litestream.ReplicaClient, level int, minTXID, maxTXID ltx.TXID, ts time.Time) { t.Helper() var buf bytes.Buffer enc, err := ltx.NewEncoder(&buf) if err != nil { t.Fatal(err) } if err := enc.EncodeHeader(ltx.Header{ Version: ltx.Version, Flags: ltx.HeaderFlagNoChecksum, PageSize: 4096, Commit: 1, MinTXID: minTXID, MaxTXID: maxTXID, Timestamp: ts.UnixMilli(), }); err != nil { t.Fatal(err) } // Write a dummy page if err := enc.EncodePage(ltx.PageHeader{Pgno: 1}, make([]byte, 4096)); err != nil { t.Fatal(err) } if err := enc.Close(); err != nil { t.Fatal(err) } if _, err := client.WriteLTXFile(context.Background(), level, minTXID, maxTXID, io.NopCloser(&buf)); err != nil { t.Fatal(err) } } ================================================ FILE: db.go ================================================ package litestream import ( "bytes" "context" "database/sql" "encoding/binary" "errors" "fmt" "hash/crc64" "io" "log/slog" "os" "path/filepath" "slices" "strconv" "strings" "sync" "time" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/superfly/ltx" "modernc.org/sqlite" "github.com/benbjohnson/litestream/internal" ) // Default DB settings. const ( DefaultMonitorInterval = 1 * time.Second DefaultCheckpointInterval = 1 * time.Minute DefaultBusyTimeout = 1 * time.Second DefaultMinCheckpointPageN = 1000 DefaultTruncatePageN = 121359 // ~500MB with 4KB page size DefaultShutdownSyncTimeout = 30 * time.Second DefaultShutdownSyncInterval = 500 * time.Millisecond // Sync error backoff configuration. // When sync errors occur repeatedly (e.g., disk full), backoff doubles each time. DefaultSyncBackoffMax = 5 * time.Minute // Maximum backoff between retries SyncErrorLogInterval = 30 * time.Second // Rate-limit repeated error logging ) // DB represents a managed instance of a SQLite database in the file system. // // Checkpoint Strategy: // Litestream uses a progressive 3-tier checkpoint approach to balance WAL size // management with write availability: // // 1. MinCheckpointPageN (PASSIVE): Non-blocking checkpoint at ~1k pages (~4MB). // Attempts checkpoint but allows concurrent readers/writers. // // 2. CheckpointInterval (PASSIVE): Time-based non-blocking checkpoint. // Ensures regular checkpointing even with low write volume. // // 3. TruncatePageN (TRUNCATE): Blocking checkpoint at ~121k pages (~500MB). // Emergency brake for runaway WAL growth. Can block writes while waiting // for long-lived read transactions. Configurable/disableable. // // The RESTART checkpoint mode was permanently removed due to production issues // with indefinite write blocking (issue #724). All checkpoints now use either // PASSIVE (non-blocking) or TRUNCATE (emergency only) modes. type DB struct { mu sync.RWMutex path string // part to database metaPath string // Path to the database metadata. db *sql.DB // target database f *os.File // long-running db file descriptor rtx *sql.Tx // long running read transaction pageSize int // page size, in bytes notify chan struct{} // closes on WAL change chkMu sync.RWMutex // checkpoint lock opened bool // true if Open() was called and Close() not yet called // syncedSinceCheckpoint tracks whether any data has been synced since // the last checkpoint. Used to prevent time-based checkpoints from // triggering when there are no actual database changes, which would // otherwise create unnecessary LTX files. See issue #896. syncedSinceCheckpoint bool // syncedToWALEnd tracks whether the last successful sync reached the // exact end of the WAL file. When true, a subsequent WAL truncation // (from checkpoint) is expected and should NOT trigger a full snapshot. // This prevents issue #927 where every checkpoint triggers unnecessary // full snapshots because verify() sees the old LTX position exceeds // the new (truncated) WAL size. syncedToWALEnd bool // lastSyncedWALOffset tracks the logical end of the WAL content after // the last successful sync. This is the WALOffset + WALSize from the // last LTX file. Used for checkpoint threshold decisions instead of // file size, which may include stale frames with old salt values after // a checkpoint. This prevents issue #997 where PASSIVE checkpoints // trigger a feedback loop because stale file size exceeds threshold. lastSyncedWALOffset int64 // last file info for each level maxLTXFileInfos struct { sync.Mutex m map[int]*ltx.FileInfo } // Cached position from the latest L0 LTX file. // nil means cache is invalid; non-nil is the cached position. pos struct { sync.Mutex value *ltx.Pos } fileInfo os.FileInfo // db info cached during init dirInfo os.FileInfo // parent dir info cached during init ctx context.Context cancel func() wg sync.WaitGroup Done <-chan struct{} // Metrics dbSizeGauge prometheus.Gauge walSizeGauge prometheus.Gauge totalWALBytesCounter prometheus.Counter txIDGauge prometheus.Gauge syncNCounter prometheus.Counter syncErrorNCounter prometheus.Counter syncSecondsCounter prometheus.Counter checkpointNCounterVec *prometheus.CounterVec checkpointErrorNCounterVec *prometheus.CounterVec checkpointSecondsCounterVec *prometheus.CounterVec // Minimum threshold of WAL size, in pages, before a passive checkpoint. // A passive checkpoint will attempt a checkpoint but fail if there are // active transactions occurring at the same time. // // Uses PASSIVE checkpoint mode (non-blocking). Keeps WAL size manageable // for faster restores. Default: 1000 pages (~4MB with 4KB page size). MinCheckpointPageN int // Threshold of WAL size, in pages, before a forced truncation checkpoint. // A forced truncation checkpoint will block new transactions and wait for // existing transactions to finish before issuing a checkpoint and // truncating the WAL. // // Uses TRUNCATE checkpoint mode (blocking). Prevents unbounded WAL growth // from long-lived read transactions. Default: 121359 pages (~500MB with 4KB // page size). Set to 0 to disable forced truncation (use with caution as // WAL can grow unbounded if read transactions prevent checkpointing). TruncatePageN int // Time between automatic checkpoints in the WAL. This is done to allow // more fine-grained WAL files so that restores can be performed with // better precision. // // Uses PASSIVE checkpoint mode (non-blocking). Default: 1 minute. // Set to 0 to disable time-based checkpoints. CheckpointInterval time.Duration // Frequency at which to perform db sync. MonitorInterval time.Duration // The timeout to wait for EBUSY from SQLite. BusyTimeout time.Duration // Minimum time to retain L0 files after they have been compacted into L1. L0Retention time.Duration // VerifyCompaction enables post-compaction TXID consistency verification. // When enabled, verifies that files at the destination level have // contiguous TXID ranges after each compaction. VerifyCompaction bool // RetentionEnabled controls whether Litestream actively deletes old files // during retention enforcement. When false, cloud provider lifecycle // policies handle retention instead. Local file cleanup still occurs. RetentionEnabled bool // Remote replica for the database. // Must be set before calling Open(). Replica *Replica // Compactor handles shared compaction logic. // Created in NewDB with nil client; client set once in Open() from Replica.Client. compactor *Compactor // Shutdown sync retry settings. // ShutdownSyncTimeout is the total time to retry syncing on shutdown. // ShutdownSyncInterval is the time between retry attempts. ShutdownSyncTimeout time.Duration ShutdownSyncInterval time.Duration // lastSuccessfulSyncAt tracks when replication last succeeded. // Used by heartbeat monitoring to determine if a ping should be sent. lastSuccessfulSyncMu sync.RWMutex lastSuccessfulSyncAt time.Time // Where to send log messages, defaults to global slog with database epath. Logger *slog.Logger } // NewDB returns a new instance of DB for a given path. func NewDB(path string) *DB { dir, file := filepath.Split(path) db := &DB{ path: path, metaPath: filepath.Join(dir, "."+file+MetaDirSuffix), notify: make(chan struct{}), MinCheckpointPageN: DefaultMinCheckpointPageN, TruncatePageN: DefaultTruncatePageN, CheckpointInterval: DefaultCheckpointInterval, MonitorInterval: DefaultMonitorInterval, BusyTimeout: DefaultBusyTimeout, L0Retention: DefaultL0Retention, RetentionEnabled: true, ShutdownSyncTimeout: DefaultShutdownSyncTimeout, ShutdownSyncInterval: DefaultShutdownSyncInterval, Logger: slog.With(LogKeyDB, filepath.Base(path)), } db.maxLTXFileInfos.m = make(map[int]*ltx.FileInfo) db.dbSizeGauge = dbSizeGaugeVec.WithLabelValues(db.path) db.walSizeGauge = walSizeGaugeVec.WithLabelValues(db.path) db.totalWALBytesCounter = totalWALBytesCounterVec.WithLabelValues(db.path) db.txIDGauge = txIDIndexGaugeVec.WithLabelValues(db.path) db.syncNCounter = syncNCounterVec.WithLabelValues(db.path) db.syncErrorNCounter = syncErrorNCounterVec.WithLabelValues(db.path) db.syncSecondsCounter = syncSecondsCounterVec.WithLabelValues(db.path) db.checkpointNCounterVec = checkpointNCounterVec.MustCurryWith(prometheus.Labels{"db": db.path}) db.checkpointErrorNCounterVec = checkpointErrorNCounterVec.MustCurryWith(prometheus.Labels{"db": db.path}) db.checkpointSecondsCounterVec = checkpointSecondsCounterVec.MustCurryWith(prometheus.Labels{"db": db.path}) db.ctx, db.cancel = context.WithCancel(context.Background()) // Initialize compactor with nil client (set once in Open() from Replica.Client). db.compactor = NewCompactor(nil, db.Logger.With(LogKeySubsystem, LogSubsystemCompactor)) db.compactor.LocalFileOpener = db.openLocalLTXFile db.compactor.LocalFileDeleter = db.deleteLocalLTXFile db.compactor.CompactionVerifyErrorCounter = compactionVerifyErrorCounterVec.WithLabelValues(db.path) db.compactor.CacheGetter = func(level int) (*ltx.FileInfo, bool) { db.maxLTXFileInfos.Lock() defer db.maxLTXFileInfos.Unlock() info, ok := db.maxLTXFileInfos.m[level] return info, ok } db.compactor.CacheSetter = func(level int, info *ltx.FileInfo) { db.maxLTXFileInfos.Lock() defer db.maxLTXFileInfos.Unlock() db.maxLTXFileInfos.m[level] = info } return db } // SetLogger updates the database logger and propagates to subsystems. func (db *DB) SetLogger(logger *slog.Logger) { if logger == nil { logger = slog.Default() } db.Logger = logger if db.compactor != nil { db.compactor.setLogger(logger.With(LogKeySubsystem, LogSubsystemCompactor)) } if db.Replica != nil && db.Replica.Client != nil { db.Replica.Client.SetLogger(db.Replica.Logger()) } } // SQLDB returns a reference to the underlying sql.DB connection. func (db *DB) SQLDB() *sql.DB { return db.db } // Path returns the path to the database. func (db *DB) Path() string { return db.path } // IsOpen returns true if the database has been opened. func (db *DB) IsOpen() bool { db.mu.RLock() defer db.mu.RUnlock() return db.opened } // WALPath returns the path to the database's WAL file. func (db *DB) WALPath() string { return db.path + "-wal" } // MetaPath returns the path to the database metadata. func (db *DB) MetaPath() string { return db.metaPath } // SetMetaPath sets the path to database metadata. func (db *DB) SetMetaPath(path string) { db.metaPath = path } // LTXDir returns path of the root LTX directory. func (db *DB) LTXDir() string { return filepath.Join(db.metaPath, "ltx") } // ResetLocalState removes local LTX files, forcing a fresh snapshot on next sync. // This is useful for recovering from corrupted or missing LTX files. // The database file itself is not modified. func (db *DB) ResetLocalState(ctx context.Context) error { db.Logger.Info("resetting local litestream state", "meta_path", db.metaPath, "ltx_dir", db.LTXDir()) // Remove all LTX files if err := os.RemoveAll(db.LTXDir()); err != nil && !os.IsNotExist(err) { return fmt.Errorf("remove ltx directory: %w", err) } // Clear cached LTX file info db.maxLTXFileInfos.Lock() db.maxLTXFileInfos.m = make(map[int]*ltx.FileInfo) db.maxLTXFileInfos.Unlock() db.invalidatePosCache() db.Logger.Info("local state reset complete, next sync will create fresh snapshot") return nil } // LTXLevelDir returns path of the given LTX compaction level. // Panics if level is negative. func (db *DB) LTXLevelDir(level int) string { return filepath.Join(db.LTXDir(), strconv.Itoa(level)) } // LTXPath returns the local path of a single LTX file. // Panics if level or either txn ID is negative. func (db *DB) LTXPath(level int, minTXID, maxTXID ltx.TXID) string { assert(level >= 0, "level cannot be negative") return filepath.Join(db.LTXLevelDir(level), ltx.FormatFilename(minTXID, maxTXID)) } // openLocalLTXFile opens a local LTX file for reading. // Used by the Compactor to prefer local files over remote. func (db *DB) openLocalLTXFile(level int, minTXID, maxTXID ltx.TXID) (io.ReadCloser, error) { return os.Open(db.LTXPath(level, minTXID, maxTXID)) } // deleteLocalLTXFile deletes a local LTX file. // Used by the Compactor for retention enforcement. func (db *DB) deleteLocalLTXFile(level int, minTXID, maxTXID ltx.TXID) error { path := db.LTXPath(level, minTXID, maxTXID) if err := os.Remove(path); err != nil && !os.IsNotExist(err) { return err } if level == 0 { db.invalidatePosCache() } return nil } // MaxLTX returns the last LTX file written to level 0. func (db *DB) MaxLTX() (minTXID, maxTXID ltx.TXID, err error) { ents, err := os.ReadDir(db.LTXLevelDir(0)) if os.IsNotExist(err) { return 0, 0, nil // no LTX files written } else if err != nil { return 0, 0, err } // Find highest txn ID. for _, ent := range ents { if min, max, err := ltx.ParseFilename(ent.Name()); err != nil { continue // invalid LTX filename } else if max > maxTXID { minTXID, maxTXID = min, max } } return minTXID, maxTXID, nil } // FileInfo returns the cached file stats for the database file when it was initialized. func (db *DB) FileInfo() os.FileInfo { return db.fileInfo } // DirInfo returns the cached file stats for the parent directory of the database file when it was initialized. func (db *DB) DirInfo() os.FileInfo { return db.dirInfo } // Pos returns the current replication position of the database. // The result is cached and invalidated when L0 LTX files change. func (db *DB) Pos() (ltx.Pos, error) { db.pos.Lock() defer db.pos.Unlock() if db.pos.value != nil { return *db.pos.value, nil } minTXID, maxTXID, err := db.MaxLTX() if err != nil { return ltx.Pos{}, err } else if minTXID == 0 { return ltx.Pos{}, nil // no replication yet } f, err := os.Open(db.LTXPath(0, minTXID, maxTXID)) if err != nil { return ltx.Pos{}, err } defer func() { _ = f.Close() }() dec := ltx.NewDecoder(f) if err := dec.Verify(); err != nil { return ltx.Pos{}, fmt.Errorf("ltx verification failed: %w", err) } pos := dec.PostApplyPos() db.pos.value = &pos return pos, nil } // invalidatePosCache clears the cached position so the next call to Pos() // recomputes it from disk. Call this when L0 LTX files are deleted or // when the L0 directory is cleared. func (db *DB) invalidatePosCache() { db.pos.Lock() db.pos.value = nil db.pos.Unlock() } // Notify returns a channel that closes when the shadow WAL changes. func (db *DB) Notify() <-chan struct{} { db.mu.RLock() defer db.mu.RUnlock() return db.notify } // PageSize returns the page size of the underlying database. // Only valid after database exists & Init() has successfully run. func (db *DB) PageSize() int { db.mu.RLock() defer db.mu.RUnlock() return db.pageSize } // RecordSuccessfulSync marks the current time as a successful sync. // Used by heartbeat monitoring to determine if a ping should be sent. func (db *DB) RecordSuccessfulSync() { db.lastSuccessfulSyncMu.Lock() defer db.lastSuccessfulSyncMu.Unlock() db.lastSuccessfulSyncAt = time.Now() } // LastSuccessfulSyncAt returns the time of the last successful sync. func (db *DB) LastSuccessfulSyncAt() time.Time { db.lastSuccessfulSyncMu.RLock() defer db.lastSuccessfulSyncMu.RUnlock() return db.lastSuccessfulSyncAt } // SyncStatus represents the current replication state of the database. type SyncStatus struct { LocalTXID ltx.TXID RemoteTXID ltx.TXID InSync bool } // SyncStatus returns the current replication status of the database, comparing // the local transaction position against the remote replica position. The remote // position is queried from the replica storage, so this method may perform I/O. func (db *DB) SyncStatus(ctx context.Context) (SyncStatus, error) { if db.Replica == nil { return SyncStatus{}, fmt.Errorf("no replica configured") } localPos, err := db.Pos() if err != nil { return SyncStatus{}, fmt.Errorf("local position: %w", err) } remotePos, err := db.Replica.calcPos(ctx) if err != nil { return SyncStatus{}, fmt.Errorf("remote position: %w", err) } return SyncStatus{ LocalTXID: localPos.TXID, RemoteTXID: remotePos.TXID, InSync: localPos.TXID > 0 && localPos.TXID == remotePos.TXID, }, nil } // SyncAndWait performs a full sync: WAL to LTX files, then LTX files to remote // replica. Blocks until both stages complete. func (db *DB) SyncAndWait(ctx context.Context) error { if db.Replica == nil { return fmt.Errorf("no replica configured") } if err := db.Sync(ctx); err != nil { return fmt.Errorf("db sync: %w", err) } if err := db.Replica.Sync(ctx); err != nil { return fmt.Errorf("replica sync: %w", err) } return nil } // EnsureExists restores the database from the configured replica if the local // database file does not exist. If no backup is available, it returns nil and // a fresh database will be created on Open(). Must be called before Open(). func (db *DB) EnsureExists(ctx context.Context) error { if db.Replica == nil { return fmt.Errorf("no replica configured") } if db.Replica.Client == nil { return fmt.Errorf("no replica client configured") } if _, err := os.Stat(db.Path()); err == nil { return nil } else if !os.IsNotExist(err) { return fmt.Errorf("stat database: %w", err) } if dir := filepath.Dir(db.Path()); dir != "." { if err := os.MkdirAll(dir, 0o750); err != nil { return fmt.Errorf("create parent directory: %w", err) } } opt := NewRestoreOptions() opt.OutputPath = db.Path() opt.IntegrityCheck = IntegrityCheckQuick if err := db.Replica.Restore(ctx, opt); err != nil { if errors.Is(err, ErrTxNotAvailable) || errors.Is(err, ErrNoSnapshots) { db.Logger.Debug("no backup found, will create fresh database") return nil } return fmt.Errorf("restore from backup: %w", err) } db.Logger.Info("database restored from backup", "path", db.Path()) return nil } // Open initializes the background monitoring goroutine. func (db *DB) Open() (err error) { db.mu.Lock() if db.opened { db.mu.Unlock() return nil // already open } // Recreate context for fresh start (handles reopen after close) db.ctx, db.cancel = context.WithCancel(context.Background()) db.mu.Unlock() // Validate fields on database. if db.Replica == nil { return fmt.Errorf("replica required before opening database") } if db.Replica.Client == nil { return fmt.Errorf("replica client required before opening database") } if db.MinCheckpointPageN <= 0 { return fmt.Errorf("minimum checkpoint page count required") } // Clear old temporary files that my have been left from a crash. if err := removeTmpFiles(db.metaPath); err != nil { return fmt.Errorf("cannot remove tmp files: %w", err) } // Set the compactor client once before starting any goroutines. db.compactor.VerifyCompaction = db.VerifyCompaction db.compactor.RetentionEnabled = db.RetentionEnabled db.compactor.client = db.Replica.Client // Start monitoring SQLite database in a separate goroutine. if db.MonitorInterval > 0 { db.wg.Add(1) go func() { defer db.wg.Done(); db.monitor() }() } // Mark as opened only after successful initialization. db.mu.Lock() db.opened = true db.mu.Unlock() return nil } // Close flushes outstanding WAL writes to replicas, releases the read lock, // and closes the database. If Done is set, closing it interrupts the shutdown // sync retry loop and cancels any in-flight sync attempt. func (db *DB) Close(ctx context.Context) (err error) { db.cancel() db.wg.Wait() // Perform a final db sync, if initialized. if db.db != nil { if e := db.Sync(ctx); e != nil { err = e } } // Ensure replicas perform a final sync and stop replicating. if db.Replica != nil { if db.db != nil { if e := db.syncReplicaWithRetry(ctx); e != nil && err == nil { err = e } } db.Replica.Stop(true) } // Release the read lock to allow other applications to handle checkpointing. if db.rtx != nil { if e := db.releaseReadLock(); e != nil && err == nil { err = e } } if db.db != nil { if e := db.db.Close(); e != nil && err == nil { err = e } db.db = nil } if db.f != nil { if e := db.f.Close(); e != nil && err == nil { err = e } db.f = nil } db.mu.Lock() db.opened = false db.rtx = nil db.mu.Unlock() return err } // syncReplicaWithRetry attempts to sync the replica with retry logic for shutdown. // It retries until success, timeout, or context cancellation. If db.Done is non-nil, // closing it cancels any in-flight sync attempt and exits the retry loop. // If ShutdownSyncTimeout is 0, it performs a single sync attempt without retries. func (db *DB) syncReplicaWithRetry(ctx context.Context) error { if db.Replica == nil { return nil } timeout := db.ShutdownSyncTimeout interval := db.ShutdownSyncInterval // If timeout is zero, just try once (no retry) if timeout == 0 { return db.Replica.Sync(ctx) } // Use default interval if not set if interval == 0 { interval = DefaultShutdownSyncInterval } // Create deadline context for total retry duration. deadlineCtx, deadlineCancel := context.WithTimeout(ctx, timeout) defer deadlineCancel() // If db.Done is set, derive a context that cancels when done is closed // so that in-flight Replica.Sync calls are interrupted immediately. syncCtx := deadlineCtx if db.Done != nil { var syncCancel context.CancelFunc syncCtx, syncCancel = context.WithCancel(deadlineCtx) go func() { select { case <-db.Done: syncCancel() case <-deadlineCtx.Done(): syncCancel() } }() } var lastErr error attempt := 0 startTime := time.Now() for { // Check if done is already closed before attempting sync if db.Done != nil { select { case <-db.Done: db.Logger.Warn("shutdown sync skipped, interrupted by signal", "attempts", attempt, "duration", time.Since(startTime)) return fmt.Errorf("after %d attempts: %w", attempt, ErrShutdownInterrupted) default: } } attempt++ // Try sync if err := db.Replica.Sync(syncCtx); err == nil { if attempt > 1 { db.Logger.Info("shutdown sync succeeded after retry", "attempts", attempt, "duration", time.Since(startTime)) } return nil } else { lastErr = err } // Check if we should stop retrying (done signal or timeout) select { case <-deadlineCtx.Done(): db.Logger.Error("shutdown sync failed after timeout", "attempts", attempt, "duration", time.Since(startTime), "error", lastErr) return fmt.Errorf("shutdown sync timeout after %d attempts: %w", attempt, lastErr) case <-db.Done: db.Logger.Warn("shutdown sync interrupted by signal", "attempts", attempt, "duration", time.Since(startTime), "error", lastErr) return fmt.Errorf("after %d attempts: %w", attempt, ErrShutdownInterrupted) default: } // Log retry with hint about second signal if interruptible if db.Done != nil { db.Logger.Warn("shutdown sync failed, retrying (press Ctrl+C again to skip)", "attempts", attempt, "error", lastErr, "elapsed", time.Since(startTime), "remaining", time.Until(startTime.Add(timeout))) } else { db.Logger.Warn("shutdown sync failed, retrying", "attempts", attempt, "error", lastErr, "elapsed", time.Since(startTime), "remaining", time.Until(startTime.Add(timeout))) } // Wait before retry, but also listen for done signal select { case <-time.After(interval): case <-deadlineCtx.Done(): return fmt.Errorf("shutdown sync timeout after %d attempts: %w", attempt, lastErr) case <-db.Done: db.Logger.Warn("shutdown sync interrupted by signal", "attempts", attempt, "duration", time.Since(startTime)) return fmt.Errorf("after %d attempts: %w", attempt, ErrShutdownInterrupted) } } } // setPersistWAL sets the PERSIST_WAL file control on the database connection. // This prevents SQLite from removing the WAL file when connections close. func (db *DB) setPersistWAL(ctx context.Context) error { conn, err := db.db.Conn(ctx) if err != nil { return fmt.Errorf("get connection: %w", err) } defer conn.Close() return conn.Raw(func(driverConn interface{}) error { fc, ok := driverConn.(sqlite.FileControl) if !ok { return fmt.Errorf("driver does not implement FileControl") } _, err := fc.FileControlPersistWAL("main", 1) if err != nil { return fmt.Errorf("FileControlPersistWAL: %w", err) } return nil }) } // init initializes the connection to the database. // Skipped if already initialized or if the database file does not exist. func (db *DB) init(ctx context.Context) (err error) { // Exit if already initialized. if db.db != nil { return nil } // Exit if no database file exists. fi, err := os.Stat(db.path) if os.IsNotExist(err) { return nil } else if err != nil { return err } db.fileInfo = fi // Obtain permissions for parent directory. if fi, err = os.Stat(filepath.Dir(db.path)); err != nil { return err } db.dirInfo = fi dsn := fmt.Sprintf("file:%s?_pragma=busy_timeout(%d)&_pragma=wal_autocheckpoint(0)", db.path, db.BusyTimeout.Milliseconds()) if db.db, err = sql.Open("sqlite", dsn); err != nil { return err } // Set PERSIST_WAL to prevent WAL file removal when database connections close. if err := db.setPersistWAL(ctx); err != nil { return fmt.Errorf("set PERSIST_WAL: %w", err) } // Open long-running database file descriptor. Required for non-OFD locks. if db.f, err = os.Open(db.path); err != nil { return fmt.Errorf("open db file descriptor: %w", err) } // Ensure database is closed if init fails. // Initialization can retry on next sync. defer func() { if err != nil { _ = db.releaseReadLock() db.db.Close() db.f.Close() db.db, db.f = nil, nil } }() // Enable WAL and ensure it is set. New mode should be returned on success: // https://www.sqlite.org/pragma.html#pragma_journal_mode var mode string if err := db.db.QueryRowContext(ctx, `PRAGMA journal_mode = wal;`).Scan(&mode); err != nil { return err } else if mode != "wal" { return fmt.Errorf("enable wal failed, mode=%q", mode) } // Create a table to force writes to the WAL when empty. // There should only ever be one row with id=1. if _, err := db.db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS _litestream_seq (id INTEGER PRIMARY KEY, seq INTEGER);`); err != nil { return fmt.Errorf("create _litestream_seq table: %w", err) } // Create a lock table to force write locks during sync. // The sync write transaction always rolls back so no data should be in this table. if _, err := db.db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS _litestream_lock (id INTEGER);`); err != nil { return fmt.Errorf("create _litestream_lock table: %w", err) } // Start a long-running read transaction to prevent other transactions // from checkpointing. if err := db.acquireReadLock(ctx); err != nil { return fmt.Errorf("acquire read lock: %w", err) } // Read page size. if err := db.db.QueryRowContext(ctx, `PRAGMA page_size;`).Scan(&db.pageSize); err != nil { return fmt.Errorf("read page size: %w", err) } else if db.pageSize <= 0 { return fmt.Errorf("invalid db page size: %d", db.pageSize) } // Ensure meta directory structure exists. if err := internal.MkdirAll(db.metaPath, db.dirInfo); err != nil { return err } // Ensure WAL has at least one frame in it. if err := db.ensureWALExists(ctx); err != nil { return fmt.Errorf("ensure wal exists: %w", err) } // Check if database is behind replica (issue #781). // This must happen before replica.Start() to detect restore scenarios. if db.Replica != nil { if err := db.checkDatabaseBehindReplica(ctx); err != nil { return fmt.Errorf("check database behind replica: %w", err) } } // If we have an existing replication files, ensure the headers match. // if err := db.verifyHeadersMatch(); err != nil { // return fmt.Errorf("cannot determine last wal position: %w", err) // } // TODO(gen): Generate diff of current LTX snapshot and save as next LTX file. // Start replication. if db.Replica != nil { db.Replica.Start(db.ctx) } return nil } /* // verifyHeadersMatch returns an error if func (db *DB) verifyHeadersMatch() error { pos, err := db.Pos() if err != nil { return false, fmt.Errorf("cannot determine position: %w", err) } else if pos.TXID == 0 { return true, nil // no replication performed yet } hdr0, err := readWALHeader(db.WALPath()) if os.IsNotExist(err) { return false, fmt.Errorf("no wal: %w", err) } else if err != nil { return false, fmt.Errorf("read wal header: %w", err) } salt1 := binary.BigEndian.Uint32(hdr0[16:]) salt2 := binary.BigEndian.Uint32(hdr0[20:]) ltxPath := db.LTXPath(0, pos.TXID, pos.TXID) f, err := os.Open(ltxPath) if err != nil { return false, fmt.Errorf("open ltx path: %w", err) } defer func() { _ = f.Close() }() dec := ltx.NewDecoder(f) if err := dec.DecodeHeader(); err != nil { return false, fmt.Errorf("decode ltx header: %w", err) } hdr1 := dec.Header() if salt1 != hdr1.WALSalt1 || salt2 != hdr1.WALSalt2 { db.Logger.Log(internal.LevelTrace, "salt mismatch", "path", ltxPath, "wal", [2]uint32{salt1, salt2}, "ltx", [2]uint32{hdr1.WALSalt1, hdr1.WALSalt2}) return false, nil } return true, nil } */ // acquireReadLock begins a read transaction on the database to prevent checkpointing. func (db *DB) acquireReadLock(ctx context.Context) error { if db.rtx != nil { return nil } // Start long running read-transaction to prevent checkpoints. tx, err := db.db.BeginTx(ctx, nil) if err != nil { return err } // Execute read query to obtain read lock. if _, err := tx.ExecContext(ctx, `SELECT COUNT(1) FROM _litestream_seq;`); err != nil { _ = tx.Rollback() return err } // Track transaction so we can release it later before checkpoint. db.rtx = tx return nil } // releaseReadLock rolls back the long-running read transaction. func (db *DB) releaseReadLock() error { // Ignore if we do not have a read lock. if db.rtx == nil { return nil } // Rollback & clear read transaction. // Use rollback() helper to suppress "already rolled back" errors that can // occur during shutdown when concurrent checkpoint and close operations // both attempt to release the read lock. See issue #934. err := rollback(db.rtx) db.rtx = nil return err } // Sync copies pending data from the WAL to the shadow WAL. func (db *DB) Sync(ctx context.Context) (err error) { db.mu.Lock() defer db.mu.Unlock() // Track total sync metrics. t := time.Now() defer func() { db.syncNCounter.Inc() if err != nil { db.syncErrorNCounter.Inc() } db.syncSecondsCounter.Add(float64(time.Since(t).Seconds())) }() // Initialize database, if necessary. Exit if no DB exists. if err := db.init(ctx); err != nil { return err } else if db.db == nil { db.Logger.Debug("sync: no database found") return nil } // Ensure WAL has at least one frame in it. if err := db.ensureWALExists(ctx); err != nil { return fmt.Errorf("ensure wal exists: %w", err) } origWALSize, newWALSize, synced, err := db.verifyAndSync(ctx, false) if err != nil { return err } // Track that data was synced for time-based checkpoint decisions. if synced { db.syncedSinceCheckpoint = true } if err := db.checkpointIfNeeded(ctx, origWALSize, newWALSize); err != nil { return fmt.Errorf("checkpoint: %w", err) } // Compute current index and total shadow WAL size. pos, err := db.Pos() if err != nil { return fmt.Errorf("pos: %w", err) } db.txIDGauge.Set(float64(pos.TXID)) // Update file size metrics. if fi, err := os.Stat(db.path); err == nil { db.dbSizeGauge.Set(float64(fi.Size())) } db.walSizeGauge.Set(float64(newWALSize)) // Notify replicas of WAL changes. // if changed { close(db.notify) db.notify = make(chan struct{}) // } return nil } func (db *DB) verifyAndSync(ctx context.Context, checkpointing bool) (origWALSize, newWALSize int64, synced bool, err error) { // Use the last synced WAL offset as the logical size for checkpoint decisions. // This avoids using file size which may include stale frames with old salt // values after a checkpoint. See issue #997. origWALSize = db.lastSyncedWALOffset if origWALSize == 0 { // First sync - use file size as fallback origWALSize, err = db.walFileSize() if err != nil { return 0, 0, false, fmt.Errorf("stat wal before sync: %w", err) } } // Verify our last sync matches the current state of the WAL. // This ensures that the last sync position of the real WAL hasn't // been overwritten by another process. info, err := db.verify(ctx) if err != nil { return 0, 0, false, fmt.Errorf("cannot verify wal state: %w", err) } synced, err = db.sync(ctx, checkpointing, info) if err != nil { return 0, 0, false, fmt.Errorf("sync: %w", err) } // Use the logical WAL offset (from LTX) for checkpoint decisions. // After sync, db.lastSyncedWALOffset is updated to reflect the actual // content position, not the file size. newWALSize = db.lastSyncedWALOffset return origWALSize, newWALSize, synced, nil } // checkpointIfNeeded performs a checkpoint based on configured thresholds. // Checks thresholds in priority order: TruncatePageN → MinCheckpointPageN → CheckpointInterval. // // TruncatePageN uses TRUNCATE mode (blocking), others use PASSIVE mode (non-blocking). // // Time-based checkpoints only trigger if db.syncedSinceCheckpoint is true, indicating // that data has been synced since the last checkpoint. This prevents creating unnecessary // LTX files when the only WAL data is from internal bookkeeping (like _litestream_seq // updates from previous checkpoints). See issue #896. func (db *DB) checkpointIfNeeded(ctx context.Context, origWALSize, newWALSize int64) error { if db.pageSize == 0 { return nil } // Priority 1: Emergency truncate checkpoint (TRUNCATE mode, blocking) // This prevents unbounded WAL growth from long-lived read transactions. if db.TruncatePageN > 0 && origWALSize >= calcWALSize(uint32(db.pageSize), uint32(db.TruncatePageN)) { db.Logger.Info("forcing truncate checkpoint", "wal_size", origWALSize, "threshold", calcWALSize(uint32(db.pageSize), uint32(db.TruncatePageN))) return db.checkpoint(ctx, CheckpointModeTruncate) } // Priority 2: Regular checkpoint at min threshold (PASSIVE mode, non-blocking) if newWALSize >= calcWALSize(uint32(db.pageSize), uint32(db.MinCheckpointPageN)) { if err := db.checkpoint(ctx, CheckpointModePassive); err != nil { // PASSIVE checkpoints can fail with SQLITE_BUSY when database is locked. // This is expected behavior and not an error - just log and continue. if isSQLiteBusyError(err) { db.Logger.Log(ctx, internal.LevelTrace, "passive checkpoint skipped", "reason", "database busy") return nil } return err } return nil } // Priority 3: Time-based checkpoint (PASSIVE mode, non-blocking) // Only trigger if there have been actual changes synced since the last // checkpoint. This prevents creating unnecessary LTX files when the only // WAL data is from internal bookkeeping (like _litestream_seq updates). if db.CheckpointInterval > 0 && db.syncedSinceCheckpoint { // Get database file modification time fi, err := db.f.Stat() if err != nil { return fmt.Errorf("stat database: %w", err) } // Only checkpoint if enough time has passed and WAL has data if time.Since(fi.ModTime()) > db.CheckpointInterval && newWALSize > calcWALSize(uint32(db.pageSize), 1) { if err := db.checkpoint(ctx, CheckpointModePassive); err != nil { // PASSIVE checkpoints can fail with SQLITE_BUSY when database is locked. // This is expected behavior and not an error - just log and continue. if isSQLiteBusyError(err) { db.Logger.Log(ctx, internal.LevelTrace, "passive checkpoint skipped", "reason", "database busy") return nil } return err } return nil } } return nil } // isSQLiteBusyError returns true if the error is an SQLITE_BUSY error. func isSQLiteBusyError(err error) bool { if err == nil { return false } // Check for "database is locked" or "SQLITE_BUSY" in error message errStr := err.Error() return strings.Contains(errStr, "database is locked") || strings.Contains(errStr, "SQLITE_BUSY") } // isDiskFullError returns true if the error indicates disk space issues. // This includes "no space left on device" (ENOSPC) and "disk quota exceeded" (EDQUOT). func isDiskFullError(err error) bool { if err == nil { return false } errStr := strings.ToLower(err.Error()) return strings.Contains(errStr, "no space left on device") || strings.Contains(errStr, "disk quota exceeded") || strings.Contains(errStr, "enospc") || strings.Contains(errStr, "edquot") } // walFileSize returns the size of the WAL file in bytes. func (db *DB) walFileSize() (int64, error) { fi, err := os.Stat(db.WALPath()) if os.IsNotExist(err) { return 0, nil } else if err != nil { return 0, err } return fi.Size(), nil } // calcWALSize returns the size of the WAL for a given page size & count. // Casts to int64 before multiplication to prevent uint32 overflow with large page sizes. func calcWALSize(pageSize uint32, pageN uint32) int64 { return int64(WALHeaderSize) + (int64(WALFrameHeaderSize+pageSize) * int64(pageN)) } // ensureWALExists checks that the real WAL exists and has a header. func (db *DB) ensureWALExists(ctx context.Context) (err error) { // Exit early if WAL header exists. if fi, err := os.Stat(db.WALPath()); err == nil && fi.Size() >= WALHeaderSize { return nil } // Otherwise create transaction that updates the internal litestream table. _, err = db.db.ExecContext(ctx, `INSERT INTO _litestream_seq (id, seq) VALUES (1, 1) ON CONFLICT (id) DO UPDATE SET seq = seq + 1`) return err } // checkDatabaseBehindReplica detects when a database has been restored to an // earlier state and the replica has a higher TXID. This handles issue #781. // // If detected, it clears local L0 files and fetches the latest L0 LTX file // from the replica to establish a baseline. The next DB.sync() will detect // the mismatch and trigger a snapshot at the current database state. func (db *DB) checkDatabaseBehindReplica(ctx context.Context) error { // Get database position from local L0 files dbPos, err := db.Pos() if err != nil { return fmt.Errorf("get database position: %w", err) } // Get replica position from remote replicaInfo, err := db.Replica.MaxLTXFileInfo(ctx, 0) if err != nil { return fmt.Errorf("get replica position: %w", err) } else if replicaInfo.MaxTXID == 0 { return nil // No remote replica data yet } // Check if database is behind replica if dbPos.TXID >= replicaInfo.MaxTXID { return nil // Database is ahead or equal } db.Logger.Info("detected database behind replica", "db_txid", dbPos.TXID, "replica_txid", replicaInfo.MaxTXID) // Clear local L0 files l0Dir := db.LTXLevelDir(0) if err := os.RemoveAll(l0Dir); err != nil && !os.IsNotExist(err) { return fmt.Errorf("remove L0 directory: %w", err) } db.invalidatePosCache() if err := internal.MkdirAll(l0Dir, db.dirInfo); err != nil { return fmt.Errorf("recreate L0 directory: %w", err) } // Fetch latest L0 LTX file from replica minTXID, maxTXID := replicaInfo.MinTXID, replicaInfo.MaxTXID reader, err := db.Replica.Client.OpenLTXFile(ctx, 0, minTXID, maxTXID, 0, 0) if err != nil { return fmt.Errorf("open remote L0 file: %w", err) } defer func() { _ = reader.Close() }() // Write to temp file and atomically rename localPath := db.LTXPath(0, minTXID, maxTXID) tmpPath := localPath + ".tmp" tmpFile, err := os.Create(tmpPath) if err != nil { return fmt.Errorf("create temp L0 file: %w", err) } defer func() { _ = os.Remove(tmpPath) }() // Clean up temp file on error if _, err := io.Copy(tmpFile, reader); err != nil { _ = tmpFile.Close() return fmt.Errorf("copy L0 file: %w", err) } if err := tmpFile.Sync(); err != nil { _ = tmpFile.Close() return fmt.Errorf("sync L0 file: %w", err) } if err := tmpFile.Close(); err != nil { return fmt.Errorf("close L0 file: %w", err) } // Atomically rename temp file to final path if err := os.Rename(tmpPath, localPath); err != nil { return fmt.Errorf("rename L0 file: %w", err) } db.invalidatePosCache() db.Logger.Info("fetched latest L0 file from replica", "min_txid", minTXID, "max_txid", maxTXID) return nil } // verify ensures the current LTX state matches where it left off from // the real WAL. Check info.ok if verification was successful. func (db *DB) verify(ctx context.Context) (info syncInfo, err error) { frameSize := int64(db.pageSize + WALFrameHeaderSize) info.snapshotting = true pos, err := db.Pos() if err != nil { return info, fmt.Errorf("pos: %w", err) } else if pos.TXID == 0 { info.offset = WALHeaderSize return info, nil // first sync } // Determine last WAL offset we save from. ltxPath := db.LTXPath(0, pos.TXID, pos.TXID) ltxFile, err := os.Open(ltxPath) if err != nil { if os.IsNotExist(err) { return info, NewLTXError("open", ltxPath, 0, uint64(pos.TXID), uint64(pos.TXID), err) } return info, fmt.Errorf("open ltx file %s: %w", ltxPath, err) } defer func() { _ = ltxFile.Close() }() dec := ltx.NewDecoder(ltxFile) if err := dec.DecodeHeader(); err != nil { // Decode failure indicates corruption ltxErr := NewLTXError("decode", ltxPath, 0, uint64(pos.TXID), uint64(pos.TXID), fmt.Errorf("%w: %w", ErrLTXCorrupted, err)) return info, ltxErr } info.offset = dec.Header().WALOffset + dec.Header().WALSize info.salt1 = dec.Header().WALSalt1 info.salt2 = dec.Header().WALSalt2 // If LTX WAL offset is larger than real WAL then the WAL has been truncated. if fi, err := os.Stat(db.WALPath()); err != nil { return info, fmt.Errorf("open wal file: %w", err) } else if info.offset > fi.Size() { // If we previously synced to the exact end of the WAL, this truncation // is expected (normal checkpoint behavior). Reset position and continue // incrementally rather than triggering a full snapshot. See issue #927. if db.syncedToWALEnd { db.syncedToWALEnd = false // Clear flag // Read new WAL header to get current salt values hdr, err := readWALHeader(db.WALPath()) if err != nil { return info, fmt.Errorf("read wal header after expected truncation: %w", err) } info.offset = WALHeaderSize info.salt1 = binary.BigEndian.Uint32(hdr[16:]) info.salt2 = binary.BigEndian.Uint32(hdr[20:]) info.snapshotting = false info.reason = "" db.Logger.Log(ctx, internal.LevelTrace, "wal truncated after sync to end (expected checkpoint)", "new_salt1", info.salt1, "new_salt2", info.salt2) return info, nil } info.reason = "wal truncated by another process" return info, nil } // Compare WAL headers. Restart from beginning of WAL if different. hdr0, err := readWALHeader(db.WALPath()) if err != nil { return info, fmt.Errorf("cannot read wal header: %w", err) } salt1 := binary.BigEndian.Uint32(hdr0[16:]) salt2 := binary.BigEndian.Uint32(hdr0[20:]) saltMatch := salt1 == dec.Header().WALSalt1 && salt2 == dec.Header().WALSalt2 // Handle edge case where we're at WAL header (WALOffset=32, WALSize=0). // This can happen when an LTX file represents a state at the beginning of the WAL // with no frames written yet. We must check this before computing prevWALOffset // to avoid underflow (32 - 4120 = -4088). // See: https://github.com/benbjohnson/litestream/issues/900 if info.offset == WALHeaderSize { db.Logger.Debug("verify", "saltMatch", saltMatch, "atWALHeader", true) if saltMatch { info.snapshotting = false return info, nil } info.reason = "wal header salt reset, snapshotting" return info, nil } // If offset is at the beginning of the first page, we can't check for previous page. prevWALOffset := info.offset - frameSize db.Logger.Debug("verify", "saltMatch", saltMatch, "prevWALOffset", prevWALOffset) if prevWALOffset == WALHeaderSize { if saltMatch { // No writes occurred since last sync, salt still matches info.snapshotting = false return info, nil } // Salt has changed but we don't know if writes occurred since last sync info.reason = "wal header salt reset, snapshotting" return info, nil } else if prevWALOffset < WALHeaderSize { return info, fmt.Errorf("prev WAL offset is less than the header size: %d", prevWALOffset) } // If we can't verify the last page is in the last LTX file, then we need to snapshot. lastPageMatch, err := db.lastPageMatch(ctx, dec, prevWALOffset, frameSize) if err != nil { return info, fmt.Errorf("last page match: %w", err) } else if !lastPageMatch { info.reason = "last page does not exist in last ltx file, wal overwritten by another process" return info, nil } db.Logger.Debug("verify.2", "lastPageMatch", lastPageMatch) // Salt has changed which could indicate a FULL checkpoint. // If we have a last page match, then we can assume that the WAL has not been overwritten. if !saltMatch { db.Logger.Log(ctx, internal.LevelTrace, "wal restarted", "salt1", salt1, "salt2", salt2) info.offset = WALHeaderSize info.salt1, info.salt2 = salt1, salt2 if detected, err := db.detectFullCheckpoint(ctx, [][2]uint32{{salt1, salt2}, {dec.Header().WALSalt1, dec.Header().WALSalt2}}); err != nil { return info, fmt.Errorf("detect full checkpoint: %w", err) } else if detected { info.reason = "full or restart checkpoint detected, snapshotting" } else { info.snapshotting = false } return info, nil } info.snapshotting = false return info, nil } // lastPageMatch checks if the last page read in the WAL exists in the last LTX file. func (db *DB) lastPageMatch(ctx context.Context, dec *ltx.Decoder, prevWALOffset, frameSize int64) (bool, error) { if prevWALOffset <= WALHeaderSize { return false, nil } frame, err := readWALFileAt(db.WALPath(), prevWALOffset, frameSize) if err != nil { return false, fmt.Errorf("cannot read last synced wal page: %w", err) } pgno := binary.BigEndian.Uint32(frame[0:]) fsalt1 := binary.BigEndian.Uint32(frame[8:]) fsalt2 := binary.BigEndian.Uint32(frame[12:]) data := frame[WALFrameHeaderSize:] if fsalt1 != dec.Header().WALSalt1 || fsalt2 != dec.Header().WALSalt2 { return false, nil } // Verify that the last page in the WAL exists in the last LTX file. buf := make([]byte, dec.Header().PageSize) for { var hdr ltx.PageHeader if err := dec.DecodePage(&hdr, buf); errors.Is(err, io.EOF) { return false, nil // page not found in LTX file } else if err != nil { return false, fmt.Errorf("decode ltx page: %w", err) } if pgno != hdr.Pgno { continue // page number doesn't match } if !bytes.Equal(data, buf) { continue // page data doesn't match } return true, nil // Page matches } } // detectFullCheckpoint attempts to detect checks if a FULL or RESTART checkpoint // has occurred and we may have missed some frames. func (db *DB) detectFullCheckpoint(ctx context.Context, knownSalts [][2]uint32) (bool, error) { walFile, err := os.Open(db.WALPath()) if err != nil { return false, fmt.Errorf("open wal file: %w", err) } defer walFile.Close() var lastKnownSalt [2]uint32 if len(knownSalts) > 0 { lastKnownSalt = knownSalts[len(knownSalts)-1] } rd, err := NewWALReader(walFile, db.Logger.With(LogKeySubsystem, LogSubsystemWALReader)) if err != nil { return false, fmt.Errorf("new wal reader: %w", err) } m, err := rd.FrameSaltsUntil(ctx, lastKnownSalt) if err != nil { return false, fmt.Errorf("frame salts until: %w", err) } // Remove known salts from the map. for _, salt := range knownSalts { delete(m, salt) } // If we have more than one unknown salt, then we have a FULL or RESTART checkpoint. return len(m) >= 1, nil } type syncInfo struct { offset int64 // end of the previous LTX read salt1 uint32 salt2 uint32 snapshotting bool // if true, a full snapshot is required reason string // reason for snapshot } // sync copies pending bytes from the real WAL to LTX. // Returns synced=true if an LTX file was created (i.e., there were new pages to sync). func (db *DB) sync(ctx context.Context, checkpointing bool, info syncInfo) (synced bool, err error) { // Determine the next sequential transaction ID. pos, err := db.Pos() if err != nil { return false, fmt.Errorf("pos: %w", err) } txID := pos.TXID + 1 filename := db.LTXPath(0, txID, txID) logArgs := []any{ "txid", txID.String(), "offset", info.offset, } if checkpointing { logArgs = append(logArgs, "chkpt", "true") } if info.snapshotting { logArgs = append(logArgs, "snap", "true") } if info.reason != "" { logArgs = append(logArgs, "reason", info.reason) } db.Logger.Debug("sync", logArgs...) // Prevent internal checkpoints during sync. Ignore if already in a checkpoint. if !checkpointing { db.chkMu.RLock() defer db.chkMu.RUnlock() } fi, err := db.f.Stat() if err != nil { return false, err } mode := fi.Mode() commit := uint32(fi.Size() / int64(db.pageSize)) walFile, err := os.Open(db.WALPath()) if err != nil { return false, err } defer walFile.Close() walReaderLogger := db.Logger.With(LogKeySubsystem, LogSubsystemWALReader) var rd *WALReader if info.offset == WALHeaderSize { if rd, err = NewWALReader(walFile, walReaderLogger); err != nil { return false, fmt.Errorf("new wal reader: %w", err) } } else { // If we cannot verify the previous frame var pfmError *PrevFrameMismatchError if rd, err = NewWALReaderWithOffset(ctx, walFile, info.offset, info.salt1, info.salt2, walReaderLogger); errors.As(err, &pfmError) { db.Logger.Log(ctx, internal.LevelTrace, "prev frame mismatch, snapshotting", "err", pfmError.Err) info.offset = WALHeaderSize if rd, err = NewWALReader(walFile, walReaderLogger); err != nil { return false, fmt.Errorf("new wal reader, after reset") } } else if err != nil { return false, fmt.Errorf("new wal reader with offset: %w", err) } } // Build a mapping of changed page numbers and their latest content. pageMap, maxOffset, walCommit, err := rd.PageMap(ctx) if err != nil { return false, fmt.Errorf("page map: %w", err) } if walCommit > 0 { commit = walCommit } var sz int64 if maxOffset > 0 { sz = maxOffset - info.offset } assert(sz >= 0, fmt.Sprintf("wal size must be positive: sz=%d, maxOffset=%d, info.offset=%d", sz, maxOffset, info.offset)) // Track total WAL bytes synced. if sz > 0 { db.totalWALBytesCounter.Add(float64(sz)) } // Exit if we have no new WAL pages and we aren't snapshotting. if !info.snapshotting && sz == 0 { db.Logger.Log(ctx, internal.LevelTrace, "sync: skip", "reason", "no new wal pages") return false, nil } tmpFilename := filename + ".tmp" if err := internal.MkdirAll(filepath.Dir(tmpFilename), db.dirInfo); err != nil { return false, err } ltxFile, err := os.OpenFile(tmpFilename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode) if err != nil { return false, fmt.Errorf("open temp ltx file: %w", err) } defer func() { _ = os.Remove(tmpFilename) }() defer func() { _ = ltxFile.Close() }() uid, gid := internal.Fileinfo(db.fileInfo) _ = os.Chown(tmpFilename, uid, gid) db.Logger.Log(ctx, internal.LevelTrace, "encode header", "txid", txID.String(), "commit", commit, "walOffset", info.offset, "walSize", sz, "salt1", rd.salt1, "salt2", rd.salt2) timestamp := time.Now() enc, err := ltx.NewEncoder(ltxFile) if err != nil { return false, fmt.Errorf("new ltx encoder: %w", err) } if err := enc.EncodeHeader(ltx.Header{ Version: ltx.Version, Flags: ltx.HeaderFlagNoChecksum, PageSize: uint32(db.pageSize), Commit: commit, MinTXID: txID, MaxTXID: txID, Timestamp: timestamp.UnixMilli(), WALOffset: info.offset, WALSize: sz, WALSalt1: rd.salt1, WALSalt2: rd.salt2, }); err != nil { return false, fmt.Errorf("encode ltx header: %w", err) } // If we need a full snapshot, then copy from the database & WAL. // Otherwise, just copy incrementally from the WAL. if info.snapshotting { if err := db.writeLTXFromDB(ctx, enc, walFile, commit, pageMap); err != nil { return false, fmt.Errorf("write ltx from db: %w", err) } } else { if err := db.writeLTXFromWAL(ctx, enc, walFile, pageMap); err != nil { return false, fmt.Errorf("write ltx from wal: %w", err) } } // Encode final trailer to the end of the LTX file. if err := enc.Close(); err != nil { return false, fmt.Errorf("close ltx encoder: %w", err) } // Sync & close LTX file. if err := ltxFile.Sync(); err != nil { return false, fmt.Errorf("sync ltx file: %w", err) } if err := ltxFile.Close(); err != nil { return false, fmt.Errorf("close ltx file: %w", err) } // Atomically rename file to final path. if err := os.Rename(tmpFilename, filename); err != nil { db.maxLTXFileInfos.Lock() delete(db.maxLTXFileInfos.m, 0) // clear cache if in unknown state db.maxLTXFileInfos.Unlock() db.invalidatePosCache() return false, fmt.Errorf("rename ltx file: %w", err) } // Update file info cache for L0. db.maxLTXFileInfos.Lock() db.maxLTXFileInfos.m[0] = <x.FileInfo{ Level: 0, MinTXID: txID, MaxTXID: txID, CreatedAt: time.Now(), Size: enc.N(), } db.maxLTXFileInfos.Unlock() // Update cached position from the encoder. encPos := enc.PostApplyPos() db.pos.Lock() db.pos.value = &encPos db.pos.Unlock() // Track the logical end of WAL content for checkpoint decisions. // This is the WALOffset + WALSize from the LTX we just created. // Using this instead of file size prevents issue #997 where stale // frames with old salt values cause perpetual checkpoint triggering. finalOffset := info.offset + sz db.lastSyncedWALOffset = finalOffset // Track if we synced to the exact end of the WAL file. // This is used by verify() to distinguish expected checkpoint truncation // from unexpected external WAL modifications. See issue #927. if walSize, err := db.walFileSize(); err == nil { db.syncedToWALEnd = finalOffset == walSize } else { db.syncedToWALEnd = false } db.Logger.Debug("db sync", "status", "ok") return true, nil } func (db *DB) writeLTXFromDB(ctx context.Context, enc *ltx.Encoder, walFile *os.File, commit uint32, pageMap map[uint32]int64) error { lockPgno := ltx.LockPgno(uint32(db.pageSize)) data := make([]byte, db.pageSize) for pgno := uint32(1); pgno <= commit; pgno++ { if pgno == lockPgno { continue } // Check if the caller has canceled during processing. select { case <-ctx.Done(): return context.Cause(ctx) default: } // If page exists in the WAL, read from there. if offset, ok := pageMap[pgno]; ok { db.Logger.Log(ctx, internal.LevelTrace, "encode page from wal", "txid", enc.Header().MinTXID, "offset", offset, "pgno", pgno, "type", "db+wal") if n, err := walFile.ReadAt(data, offset+WALFrameHeaderSize); err != nil { return fmt.Errorf("read page %d @ %d: %w", pgno, offset, err) } else if n != len(data) { return fmt.Errorf("short read page %d @ %d", pgno, offset) } if err := enc.EncodePage(ltx.PageHeader{Pgno: pgno}, data); err != nil { return fmt.Errorf("encode ltx frame (pgno=%d): %w", pgno, err) } continue } offset := int64(pgno-1) * int64(db.pageSize) db.Logger.Log(ctx, internal.LevelTrace, "encode page from database", "offset", offset, "pgno", pgno) // Otherwise read directly from the database file. if _, err := db.f.ReadAt(data, offset); err != nil { return fmt.Errorf("read database page %d: %w", pgno, err) } if err := enc.EncodePage(ltx.PageHeader{Pgno: pgno}, data); err != nil { return fmt.Errorf("encode ltx frame (pgno=%d): %w", pgno, err) } } return nil } func (db *DB) writeLTXFromWAL(ctx context.Context, enc *ltx.Encoder, walFile *os.File, pageMap map[uint32]int64) error { // Create an ordered list of page numbers since the LTX encoder requires it. pgnos := make([]uint32, 0, len(pageMap)) for pgno := range pageMap { pgnos = append(pgnos, pgno) } slices.Sort(pgnos) data := make([]byte, db.pageSize) for _, pgno := range pgnos { offset := pageMap[pgno] db.Logger.Log(ctx, internal.LevelTrace, "encode page from wal", "txid", enc.Header().MinTXID, "offset", offset, "pgno", pgno, "type", "walonly") // Read source page using page map. if n, err := walFile.ReadAt(data, offset+WALFrameHeaderSize); err != nil { return fmt.Errorf("read page %d @ %d: %w", pgno, offset, err) } else if n != len(data) { return fmt.Errorf("short read page %d @ %d", pgno, offset) } // Write page to LTX encoder. if err := enc.EncodePage(ltx.PageHeader{Pgno: pgno}, data); err != nil { return fmt.Errorf("encode ltx frame (pgno=%d): %w", pgno, err) } } return nil } // Checkpoint performs a checkpoint on the WAL file. func (db *DB) Checkpoint(ctx context.Context, mode string) (err error) { db.mu.Lock() defer db.mu.Unlock() return db.checkpoint(ctx, mode) } // checkpoint performs a checkpoint on the WAL file and initializes a // new shadow WAL file. func (db *DB) checkpoint(ctx context.Context, mode string) error { // Try getting a checkpoint lock, will fail during snapshots. if !db.chkMu.TryLock() { return nil } defer db.chkMu.Unlock() // Read WAL header before checkpoint to check if it has been restarted. hdr, err := readWALHeader(db.WALPath()) if err != nil { return err } // Copy end of WAL before checkpoint to copy as much as possible. if _, _, _, err := db.verifyAndSync(ctx, true); err != nil { return fmt.Errorf("cannot copy wal before checkpoint: %w", err) } // Execute checkpoint and immediately issue a write to the WAL to ensure // a new page is written. if err := db.execCheckpoint(ctx, mode); err != nil { return err } else if _, err = db.db.ExecContext(ctx, `INSERT INTO _litestream_seq (id, seq) VALUES (1, 1) ON CONFLICT (id) DO UPDATE SET seq = seq + 1`); err != nil { return err } // If WAL hasn't been restarted, exit. if other, err := readWALHeader(db.WALPath()); err != nil { return err } else if bytes.Equal(hdr, other) { db.syncedSinceCheckpoint = false return nil } // Start a transaction. This will be promoted immediately after. tx, err := db.db.BeginTx(ctx, nil) if err != nil { return fmt.Errorf("begin: %w", err) } defer func() { _ = rollback(tx) }() // Insert into the lock table to promote to a write tx. The lock table // insert will never actually occur because our tx will be rolled back, // however, it will ensure our tx grabs the write lock. Unfortunately, // we can't call "BEGIN IMMEDIATE" as we are already in a transaction. if _, err := tx.ExecContext(ctx, `INSERT INTO _litestream_lock (id) VALUES (1);`); err != nil { return fmt.Errorf("_litestream_lock: %w", err) } // Copy anything that may have occurred after the checkpoint. if _, _, _, err := db.verifyAndSync(ctx, true); err != nil { return fmt.Errorf("cannot copy wal after checkpoint: %w", err) } // Release write lock before exiting. // Use rollback() helper for consistency with releaseReadLock() and the // defer above. See issue #934. if err := rollback(tx); err != nil { return fmt.Errorf("rollback post-checkpoint tx: %w", err) } db.syncedSinceCheckpoint = false return nil } func (db *DB) execCheckpoint(ctx context.Context, mode string) (err error) { // Ignore if there is no underlying database. if db.db == nil { return nil } // Track checkpoint metrics. t := time.Now() defer func() { labels := prometheus.Labels{"mode": mode} db.checkpointNCounterVec.With(labels).Inc() if err != nil { db.checkpointErrorNCounterVec.With(labels).Inc() } db.checkpointSecondsCounterVec.With(labels).Add(float64(time.Since(t).Seconds())) }() // Ensure the read lock has been removed before issuing a checkpoint. // We defer the re-acquire to ensure it occurs even on an early return. if err := db.releaseReadLock(); err != nil { return fmt.Errorf("release read lock: %w", err) } defer func() { _ = db.acquireReadLock(ctx) }() // A non-forced checkpoint is issued as "PASSIVE". This will only checkpoint // if there are not pending transactions. A forced checkpoint ("RESTART") // will wait for pending transactions to end & block new transactions before // forcing the checkpoint and restarting the WAL. // // See: https://www.sqlite.org/pragma.html#pragma_wal_checkpoint rawsql := `PRAGMA wal_checkpoint(` + mode + `);` var row [3]int if err := db.db.QueryRowContext(ctx, rawsql).Scan(&row[0], &row[1], &row[2]); err != nil { return err } db.Logger.Debug("checkpoint", "mode", mode, "result", fmt.Sprintf("%d,%d,%d", row[0], row[1], row[2])) // Reacquire the read lock immediately after the checkpoint. if err := db.acquireReadLock(ctx); err != nil { return fmt.Errorf("reacquire read lock: %w", err) } return nil } // SnapshotReader returns the current position of the database & a reader that contains a full database snapshot. func (db *DB) SnapshotReader(ctx context.Context) (ltx.Pos, io.Reader, error) { if db.PageSize() == 0 { db.Logger.Debug("page size not initialized yet", "pageSize", 0) return ltx.Pos{}, nil, &DBNotReadyError{Reason: "page size not initialized"} } pos, err := db.Pos() if err != nil { return pos, nil, fmt.Errorf("pos: %w", err) } db.Logger.Debug("snapshot", "txid", pos.TXID.String()) // Prevent internal checkpoints during sync. db.chkMu.RLock() defer db.chkMu.RUnlock() // TODO(ltx): Read database size from database header. fi, err := db.f.Stat() if err != nil { return pos, nil, err } commit := uint32(fi.Size() / int64(db.pageSize)) // Execute encoding in a separate goroutine so the caller can initialize before reading. pr, pw := io.Pipe() go func() { walFile, err := os.Open(db.WALPath()) if err != nil { pw.CloseWithError(err) return } defer walFile.Close() rd, err := NewWALReader(walFile, db.Logger.With(LogKeySubsystem, LogSubsystemWALReader)) if err != nil { pw.CloseWithError(fmt.Errorf("new wal reader: %w", err)) return } // Build a mapping of changed page numbers and their latest content. pageMap, maxOffset, walCommit, err := rd.PageMap(ctx) if err != nil { pw.CloseWithError(fmt.Errorf("page map: %w", err)) return } if walCommit > 0 { commit = walCommit } var sz int64 if maxOffset > 0 { sz = maxOffset - rd.Offset() } db.Logger.Debug("encode snapshot header", "txid", pos.TXID.String(), "commit", commit, "walOffset", rd.Offset(), "walSize", sz, "salt1", rd.salt1, "salt2", rd.salt2) enc, err := ltx.NewEncoder(pw) if err != nil { pw.CloseWithError(fmt.Errorf("new ltx encoder: %w", err)) return } if err := enc.EncodeHeader(ltx.Header{ Version: ltx.Version, Flags: ltx.HeaderFlagNoChecksum, PageSize: uint32(db.pageSize), Commit: commit, MinTXID: 1, MaxTXID: pos.TXID, Timestamp: time.Now().UnixMilli(), WALOffset: rd.Offset(), WALSize: sz, WALSalt1: rd.salt1, WALSalt2: rd.salt2, }); err != nil { pw.CloseWithError(fmt.Errorf("encode ltx snapshot header: %w", err)) return } if err := db.writeLTXFromDB(ctx, enc, walFile, commit, pageMap); err != nil { pw.CloseWithError(fmt.Errorf("write snapshot ltx: %w", err)) return } if err := enc.Close(); err != nil { pw.CloseWithError(fmt.Errorf("close ltx snapshot encoder: %w", err)) return } _ = pw.Close() }() return pos, pr, nil } // Compact performs a compaction of the LTX file at the previous level into dstLevel. // Returns metadata for the newly written compaction file. Returns ErrNoCompaction // if no new files are available to be compacted. func (db *DB) Compact(ctx context.Context, dstLevel int) (*ltx.FileInfo, error) { info, err := db.compactor.Compact(ctx, dstLevel) if err != nil { return nil, err } // If this is L1, clean up L0 files using the time-based retention policy. if dstLevel == 1 { if err := db.EnforceL0RetentionByTime(ctx); err != nil { // Don't log context cancellation errors during shutdown if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { db.Logger.Error("enforce L0 time retention", "error", err) } } } return info, nil } // SnapshotDB writes a snapshot to the replica for the current position of the database. func (db *DB) Snapshot(ctx context.Context) (*ltx.FileInfo, error) { pos, r, err := db.SnapshotReader(ctx) if err != nil { return nil, err } info, err := db.Replica.Client.WriteLTXFile(ctx, SnapshotLevel, 1, pos.TXID, r) if err != nil { return info, err } db.maxLTXFileInfos.Lock() db.maxLTXFileInfos.m[SnapshotLevel] = info db.maxLTXFileInfos.Unlock() return info, nil } // EnforceSnapshotRetention enforces retention of the snapshot level in the database by timestamp. func (db *DB) EnforceSnapshotRetention(ctx context.Context, timestamp time.Time) (minSnapshotTXID ltx.TXID, err error) { db.Logger.Debug("enforcing snapshot retention", "timestamp", timestamp) // Normal operation - use fast timestamps itr, err := db.Replica.Client.LTXFiles(ctx, SnapshotLevel, 0, false) if err != nil { return 0, fmt.Errorf("fetch ltx files: %w", err) } defer itr.Close() var deleted []*ltx.FileInfo var lastInfo *ltx.FileInfo for itr.Next() { info := itr.Item() lastInfo = info // If this snapshot is before the retention timestamp, mark it for deletion. if info.CreatedAt.Before(timestamp) { deleted = append(deleted, info) continue } // Track the lowest snapshot TXID so we can enforce retention in lower levels. // This is only tracked for snapshots not marked for deletion. if minSnapshotTXID == 0 || info.MaxTXID < minSnapshotTXID { minSnapshotTXID = info.MaxTXID } } // If this is the snapshot level, we need to ensure that at least one snapshot exists. if len(deleted) > 0 && deleted[len(deleted)-1] == lastInfo { deleted = deleted[:len(deleted)-1] } // Remove files marked for deletion from remote storage (unless retention disabled). if !db.RetentionEnabled { db.Logger.Debug("skipping remote deletion (retention disabled)", "level", SnapshotLevel, "count", len(deleted)) } else if err := db.Replica.Client.DeleteLTXFiles(ctx, deleted); err != nil { return 0, fmt.Errorf("remove ltx files: %w", err) } // Always clean up local files. for _, info := range deleted { localPath := db.LTXPath(SnapshotLevel, info.MinTXID, info.MaxTXID) db.Logger.Debug("deleting local ltx file", "level", SnapshotLevel, "minTXID", info.MinTXID, "maxTXID", info.MaxTXID, "path", localPath) if err := os.Remove(localPath); err != nil && !os.IsNotExist(err) { db.Logger.Error("failed to remove local ltx file", "path", localPath, "error", err) } } return minSnapshotTXID, nil } // EnforceL0RetentionByTime retains L0 files until they have been compacted into // L1 and have existed for at least L0Retention. func (db *DB) EnforceL0RetentionByTime(ctx context.Context) error { if db.L0Retention <= 0 { return nil } db.Logger.Debug("starting l0 retention enforcement", "retention", db.L0Retention) dbName := filepath.Base(db.Path()) // Determine the highest TXID that has been compacted into L1. itr, err := db.Replica.Client.LTXFiles(ctx, 1, 0, false) if err != nil { return fmt.Errorf("fetch l1 files: %w", err) } var maxL1TXID ltx.TXID for itr.Next() { info := itr.Item() if info.MaxTXID > maxL1TXID { maxL1TXID = info.MaxTXID } } if err := itr.Close(); err != nil { return fmt.Errorf("close l1 iterator: %w", err) } if maxL1TXID == 0 { internal.L0RetentionGaugeVec.WithLabelValues(dbName, "eligible").Set(0) internal.L0RetentionGaugeVec.WithLabelValues(dbName, "not_compacted").Set(0) internal.L0RetentionGaugeVec.WithLabelValues(dbName, "too_recent").Set(0) return nil } threshold := time.Now().Add(-db.L0Retention) itr, err = db.Replica.Client.LTXFiles(ctx, 0, 0, false) if err != nil { return fmt.Errorf("fetch l0 files: %w", err) } defer itr.Close() var ( deleted []*ltx.FileInfo lastInfo *ltx.FileInfo processedAll = true totalFiles int notCompactedCount int tooRecentCount int ) for itr.Next() { info := itr.Item() lastInfo = info totalFiles++ createdAt := info.CreatedAt if createdAt.IsZero() { if fi, err := os.Stat(db.LTXPath(0, info.MinTXID, info.MaxTXID)); err == nil { createdAt = fi.ModTime().UTC() } else { createdAt = threshold } } if createdAt.After(threshold) { // L0 entries are ordered; once we reach a newer file we stop so we don't // create gaps between retained files. VFS expects contiguous coverage. processedAll = false tooRecentCount++ break } if info.MaxTXID <= maxL1TXID { deleted = append(deleted, info) } else { notCompactedCount++ } } // Count remaining files as too_recent if we stopped early if !processedAll { for itr.Next() { tooRecentCount++ } } // Ensure we do not delete the newest L0 file only if we processed the entire level. if processedAll && len(deleted) > 0 && lastInfo != nil && deleted[len(deleted)-1] == lastInfo { deleted = deleted[:len(deleted)-1] } internal.L0RetentionGaugeVec.WithLabelValues(dbName, "eligible").Set(float64(len(deleted))) internal.L0RetentionGaugeVec.WithLabelValues(dbName, "not_compacted").Set(float64(notCompactedCount)) internal.L0RetentionGaugeVec.WithLabelValues(dbName, "too_recent").Set(float64(tooRecentCount)) db.Logger.Debug("l0 retention scan complete", "total_l0_files", totalFiles, "eligible_for_deletion", len(deleted), "not_compacted_yet", notCompactedCount, "too_recent", tooRecentCount, "max_l1_txid", maxL1TXID) if len(deleted) == 0 { return nil } if !db.RetentionEnabled { db.Logger.Debug("skipping remote deletion (retention disabled)", "level", 0, "count", len(deleted)) } else if err := db.Replica.Client.DeleteLTXFiles(ctx, deleted); err != nil { return fmt.Errorf("remove expired l0 files: %w", err) } for _, info := range deleted { localPath := db.LTXPath(0, info.MinTXID, info.MaxTXID) db.Logger.Debug("deleting expired local l0 file", "minTXID", info.MinTXID, "maxTXID", info.MaxTXID, "path", localPath) if err := os.Remove(localPath); err != nil && !os.IsNotExist(err) { db.Logger.Error("failed to remove local l0 file", "path", localPath, "error", err) } } if len(deleted) > 0 { db.invalidatePosCache() } db.Logger.Info("l0 retention enforced", "deleted_count", len(deleted), "max_l1_txid", maxL1TXID) return nil } // EnforceRetentionByTXID enforces retention so that any LTX files below // the target TXID are deleted. Always keep at least one file. func (db *DB) EnforceRetentionByTXID(ctx context.Context, level int, txID ltx.TXID) (err error) { return db.compactor.EnforceRetentionByTXID(ctx, level, txID) } // monitor runs in a separate goroutine and monitors the database & WAL. // // Implements exponential backoff on repeated sync errors to prevent disk churn // when persistent errors (like disk full) occur. See issue #927. func (db *DB) monitor() { ticker := time.NewTicker(db.MonitorInterval) defer ticker.Stop() // Backoff state for error handling. var backoff time.Duration var lastLogTime time.Time var consecutiveErrs int for { // Wait for ticker or context close. select { case <-db.ctx.Done(): return case <-ticker.C: } // If in backoff mode, wait additional time before retrying. if backoff > 0 { select { case <-db.ctx.Done(): return case <-time.After(backoff): } } // Sync the database to the shadow WAL. if err := db.Sync(db.ctx); err != nil && !errors.Is(err, context.Canceled) { consecutiveErrs++ // Exponential backoff: MonitorInterval -> 2x -> 4x -> ... -> max if backoff == 0 { backoff = db.MonitorInterval } else { backoff *= 2 if backoff > DefaultSyncBackoffMax { backoff = DefaultSyncBackoffMax } } // Log with rate limiting to avoid log spam during persistent errors. if time.Since(lastLogTime) >= SyncErrorLogInterval { db.Logger.Error("sync error", "error", err, "consecutive_errors", consecutiveErrs, "backoff", backoff) lastLogTime = time.Now() } // Try to clean up stale temp files after persistent disk errors. if isDiskFullError(err) && consecutiveErrs >= 3 { db.Logger.Warn("attempting temp file cleanup due to persistent disk errors") if cleanupErr := removeTmpFiles(db.metaPath); cleanupErr != nil { db.Logger.Error("temp file cleanup failed", "error", cleanupErr) } } continue } // Success - reset backoff and error counter. if consecutiveErrs > 0 { db.Logger.Info("sync recovered", "previous_errors", consecutiveErrs) } backoff = 0 consecutiveErrs = 0 } } // CRC64 returns a CRC-64 ISO checksum of the database and its current position. // // This function obtains a read lock so it prevents syncs from occurring until // the operation is complete. The database will still be usable but it will be // unable to checkpoint during this time. // // If dst is set, the database file is copied to that location before checksum. func (db *DB) CRC64(ctx context.Context) (uint64, ltx.Pos, error) { db.mu.Lock() defer db.mu.Unlock() if err := db.init(ctx); err != nil { return 0, ltx.Pos{}, err } else if db.db == nil { return 0, ltx.Pos{}, os.ErrNotExist } // Force a RESTART checkpoint to ensure the database is at the start of the WAL. if err := db.checkpoint(ctx, CheckpointModeRestart); err != nil { return 0, ltx.Pos{}, err } // Obtain current position. Clear the offset since we are only reading the // DB and not applying the current WAL. pos, err := db.Pos() if err != nil { return 0, pos, err } // Seek to the beginning of the db file descriptor and checksum whole file. h := crc64.New(crc64.MakeTable(crc64.ISO)) if _, err := db.f.Seek(0, io.SeekStart); err != nil { return 0, pos, err } else if _, err := io.Copy(h, db.f); err != nil { return 0, pos, err } return h.Sum64(), pos, nil } // MaxLTXFileInfo returns the metadata for the last LTX file in a level. // If cached, it will returned the local copy. Otherwise, it fetches from the replica. func (db *DB) MaxLTXFileInfo(ctx context.Context, level int) (ltx.FileInfo, error) { db.maxLTXFileInfos.Lock() defer db.maxLTXFileInfos.Unlock() info, ok := db.maxLTXFileInfos.m[level] if ok { return *info, nil } remoteInfo, err := db.Replica.MaxLTXFileInfo(ctx, level) if err != nil { return ltx.FileInfo{}, fmt.Errorf("cannot determine L%d max ltx file for %q: %w", level, db.Path(), err) } db.maxLTXFileInfos.m[level] = &remoteInfo return remoteInfo, nil } // DefaultRestoreParallelism is the default parallelism when downloading WAL files. const DefaultRestoreParallelism = 8 // DefaultFollowInterval is the default polling interval for follow mode. const DefaultFollowInterval = 1 * time.Second // IntegrityCheckMode specifies the level of integrity checking after restore. type IntegrityCheckMode int const ( IntegrityCheckNone IntegrityCheckMode = iota IntegrityCheckQuick IntegrityCheckFull ) // RestoreOptions represents options for DB.Restore(). type RestoreOptions struct { // Target path to restore into. // If blank, the original DB path is used. OutputPath string // Specific transaction to restore to. // If zero, TXID is ignored. TXID ltx.TXID // Point-in-time to restore database. // If zero, database restore to most recent state available. Timestamp time.Time // Specifies how many WAL files are downloaded in parallel during restore. Parallelism int // Follow enables continuous restore mode, polling for new LTX files // and applying them to the restored database. Similar to tail -f. Follow bool // FollowInterval specifies how often to poll for new LTX files in follow mode. FollowInterval time.Duration // IntegrityCheck specifies the level of integrity checking after restore. // Zero value (IntegrityCheckNone) skips the check for backward compatibility. IntegrityCheck IntegrityCheckMode } // NewRestoreOptions returns a new instance of RestoreOptions with defaults. func NewRestoreOptions() RestoreOptions { return RestoreOptions{ Parallelism: DefaultRestoreParallelism, FollowInterval: DefaultFollowInterval, } } // Database metrics. var ( dbSizeGaugeVec = promauto.NewGaugeVec(prometheus.GaugeOpts{ Name: "litestream_db_size", Help: "The current size of the real DB", }, []string{"db"}) walSizeGaugeVec = promauto.NewGaugeVec(prometheus.GaugeOpts{ Name: "litestream_wal_size", Help: "The current size of the real WAL", }, []string{"db"}) totalWALBytesCounterVec = promauto.NewCounterVec(prometheus.CounterOpts{ Name: "litestream_total_wal_bytes", Help: "Total number of bytes written to shadow WAL", }, []string{"db"}) txIDIndexGaugeVec = promauto.NewGaugeVec(prometheus.GaugeOpts{ Name: "litestream_txid", Help: "The current transaction ID", }, []string{"db"}) syncNCounterVec = promauto.NewCounterVec(prometheus.CounterOpts{ Name: "litestream_sync_count", Help: "Number of sync operations performed", }, []string{"db"}) syncErrorNCounterVec = promauto.NewCounterVec(prometheus.CounterOpts{ Name: "litestream_sync_error_count", Help: "Number of sync errors that have occurred", }, []string{"db"}) syncSecondsCounterVec = promauto.NewCounterVec(prometheus.CounterOpts{ Name: "litestream_sync_seconds", Help: "Time spent syncing shadow WAL, in seconds", }, []string{"db"}) checkpointNCounterVec = promauto.NewCounterVec(prometheus.CounterOpts{ Name: "litestream_checkpoint_count", Help: "Number of checkpoint operations performed", }, []string{"db", "mode"}) checkpointErrorNCounterVec = promauto.NewCounterVec(prometheus.CounterOpts{ Name: "litestream_checkpoint_error_count", Help: "Number of checkpoint errors that have occurred", }, []string{"db", "mode"}) checkpointSecondsCounterVec = promauto.NewCounterVec(prometheus.CounterOpts{ Name: "litestream_checkpoint_seconds", Help: "Time spent checkpointing WAL, in seconds", }, []string{"db", "mode"}) compactionVerifyErrorCounterVec = promauto.NewCounterVec(prometheus.CounterOpts{ Name: "litestream_compaction_verify_error_count", Help: "Number of post-compaction verification failures", }, []string{"db"}) ) ================================================ FILE: db_internal_test.go ================================================ package litestream import ( "bytes" "context" "database/sql" "encoding/binary" "errors" "fmt" "io" "log/slog" "os" "path/filepath" "testing" "time" "github.com/prometheus/client_golang/prometheus/testutil" "github.com/superfly/ltx" _ "modernc.org/sqlite" "github.com/benbjohnson/litestream/internal" ) // testReplicaClient is a minimal mock for testing that doesn't cause import cycles. type testReplicaClient struct { dir string } func (c *testReplicaClient) Init(_ context.Context) error { return nil } func (c *testReplicaClient) SetLogger(_ *slog.Logger) {} func (c *testReplicaClient) Type() string { return "test" } func (c *testReplicaClient) LTXFiles(_ context.Context, level int, afterTXID ltx.TXID, _ bool) (ltx.FileIterator, error) { internal.OperationTotalCounterVec.WithLabelValues(c.Type(), "LIST").Inc() levelDir := filepath.Join(c.dir, fmt.Sprintf("l%d", level)) entries, err := os.ReadDir(levelDir) if os.IsNotExist(err) { return ltx.NewFileInfoSliceIterator(nil), nil } else if err != nil { return nil, err } var infos []*ltx.FileInfo for _, entry := range entries { minTXID, maxTXID, err := ltx.ParseFilename(entry.Name()) if err != nil { continue } if maxTXID <= afterTXID { continue } fi, _ := entry.Info() var size int64 if fi != nil { size = fi.Size() } infos = append(infos, <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Size: size, }) } return ltx.NewFileInfoSliceIterator(infos), nil } func (c *testReplicaClient) OpenLTXFile(_ context.Context, level int, minTXID, maxTXID ltx.TXID, _, _ int64) (io.ReadCloser, error) { internal.OperationTotalCounterVec.WithLabelValues(c.Type(), "GET").Inc() path := filepath.Join(c.dir, fmt.Sprintf("l%d", level), ltx.FormatFilename(minTXID, maxTXID)) return os.Open(path) } func (c *testReplicaClient) WriteLTXFile(_ context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { data, err := io.ReadAll(r) if err != nil { return nil, err } levelDir := filepath.Join(c.dir, fmt.Sprintf("l%d", level)) if err := os.MkdirAll(levelDir, 0o755); err != nil { return nil, err } path := filepath.Join(levelDir, ltx.FormatFilename(minTXID, maxTXID)) if err := os.WriteFile(path, data, 0o600); err != nil { return nil, err } internal.OperationTotalCounterVec.WithLabelValues(c.Type(), "PUT").Inc() internal.OperationBytesCounterVec.WithLabelValues(c.Type(), "PUT").Add(float64(len(data))) return <x.FileInfo{Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Size: int64(len(data))}, nil } func (c *testReplicaClient) DeleteLTXFiles(_ context.Context, infos []*ltx.FileInfo) error { internal.OperationTotalCounterVec.WithLabelValues(c.Type(), "DELETE").Add(float64(len(infos))) return nil } func (c *testReplicaClient) DeleteAll(_ context.Context) error { return nil } // TestCalcWALSize ensures calcWALSize doesn't overflow with large page sizes. // Regression test for uint32 overflow bug where large page sizes (>=16KB) // caused incorrect WAL size calculations, triggering checkpoints too early. func TestCalcWALSize(t *testing.T) { tests := []struct { name string pageSize uint32 pageN uint32 expected int64 }{ { name: "4KB pages, 121359 pages (default TruncatePageN)", pageSize: 4096, pageN: 121359, expected: int64(WALHeaderSize) + (int64(WALFrameHeaderSize+4096) * 121359), }, { name: "16KB pages, 121359 pages", pageSize: 16384, pageN: 121359, expected: int64(WALHeaderSize) + (int64(WALFrameHeaderSize+16384) * 121359), }, { name: "32KB pages, 121359 pages", pageSize: 32768, pageN: 121359, // Expected: ~4.0 GB with 32KB pages. Bug previously overflowed. expected: int64(WALHeaderSize) + (int64(WALFrameHeaderSize+32768) * 121359), }, { name: "64KB pages, 121359 pages", pageSize: 65536, pageN: 121359, expected: int64(WALHeaderSize) + (int64(WALFrameHeaderSize+65536) * 121359), }, { name: "1KB pages, 1k pages (min checkpoint)", pageSize: 1024, pageN: 1000, expected: int64(WALHeaderSize) + (int64(WALFrameHeaderSize+1024) * 1000), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := calcWALSize(tt.pageSize, tt.pageN) if got != tt.expected { t.Errorf("calcWALSize(%d, %d) = %d, want %d (%.2f GB vs %.2f GB)", tt.pageSize, tt.pageN, got, tt.expected, float64(got)/(1024*1024*1024), float64(tt.expected)/(1024*1024*1024)) } if got <= 0 { t.Errorf("calcWALSize(%d, %d) = %d, should be positive", tt.pageSize, tt.pageN, got) } if tt.pageSize >= 32768 && tt.pageN >= 100000 { // Sanity check: ensure result is at least (page_size * page_count) minExpected := int64(tt.pageSize) * int64(tt.pageN) if got < minExpected { t.Errorf("calcWALSize(%d, %d) = %d (%.2f GB), suspiciously small, possible overflow", tt.pageSize, tt.pageN, got, float64(got)/(1024*1024*1024)) } } }) } } // TestDB_Sync_UpdatesMetrics verifies that DB size, WAL size, and total WAL bytes // metrics are properly updated during sync operations. // Regression test for issue #876: metrics were defined but never updated. func TestDB_Sync_UpdatesMetrics(t *testing.T) { // Set up database manually (can't use testingutil due to import cycle) dir := t.TempDir() dbPath := filepath.Join(dir, "db") // Create and open litestream DB db := NewDB(dbPath) db.MonitorInterval = 0 // disable background goroutine db.Replica = NewReplica(db) db.Replica.Client = &testReplicaClient{dir: t.TempDir()} db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } defer func() { if err := db.Close(context.Background()); err != nil { t.Fatal(err) } }() // Open SQL connection sqldb, err := sql.Open("sqlite", dbPath) if err != nil { t.Fatal(err) } defer sqldb.Close() if _, err := sqldb.Exec(`PRAGMA journal_mode = wal;`); err != nil { t.Fatal(err) } // Insert data to create DB and WAL content if _, err := sqldb.Exec(`CREATE TABLE t (id INT, data TEXT)`); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`INSERT INTO t VALUES (1, 'test data')`); err != nil { t.Fatal(err) } // Sync to trigger metric updates if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } // Verify DB size metric matches actual file size dbSizeMetric := dbSizeGaugeVec.WithLabelValues(db.Path()) dbSizeValue := testutil.ToFloat64(dbSizeMetric) dbFileInfo, err := os.Stat(db.Path()) if err != nil { t.Fatalf("failed to stat db file: %v", err) } if dbSizeValue != float64(dbFileInfo.Size()) { t.Fatalf("litestream_db_size=%v, want %v", dbSizeValue, dbFileInfo.Size()) } // Verify WAL size metric matches actual file size walSizeMetric := walSizeGaugeVec.WithLabelValues(db.Path()) walSizeValue := testutil.ToFloat64(walSizeMetric) walFileInfo, err := os.Stat(db.WALPath()) if err != nil { t.Fatalf("failed to stat wal file: %v", err) } if walSizeValue != float64(walFileInfo.Size()) { t.Fatalf("litestream_wal_size=%v, want %v", walSizeValue, walFileInfo.Size()) } // Verify total WAL bytes counter was incremented totalWALMetric := totalWALBytesCounterVec.WithLabelValues(db.Path()) totalWALValue := testutil.ToFloat64(totalWALMetric) if totalWALValue <= 0 { t.Fatalf("litestream_total_wal_bytes=%v, want > 0", totalWALValue) } // Verify txid metric was updated (should be > 0 after writes) txidMetric := txIDIndexGaugeVec.WithLabelValues(db.Path()) txidValue := testutil.ToFloat64(txidMetric) if txidValue <= 0 { t.Fatalf("litestream_txid=%v, want > 0", txidValue) } // Verify sync count was incremented syncCountMetric := syncNCounterVec.WithLabelValues(db.Path()) syncCountValue := testutil.ToFloat64(syncCountMetric) if syncCountValue <= 0 { t.Fatalf("litestream_sync_count=%v, want > 0", syncCountValue) } // Verify sync seconds was recorded syncSecondsMetric := syncSecondsCounterVec.WithLabelValues(db.Path()) syncSecondsValue := testutil.ToFloat64(syncSecondsMetric) if syncSecondsValue <= 0 { t.Fatalf("litestream_sync_seconds=%v, want > 0", syncSecondsValue) } } // TestDB_Checkpoint_UpdatesMetrics verifies that checkpoint metrics are updated. func TestDB_Checkpoint_UpdatesMetrics(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "db") db := NewDB(dbPath) db.MonitorInterval = 0 db.Replica = NewReplica(db) db.Replica.Client = &testReplicaClient{dir: t.TempDir()} db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } defer func() { if err := db.Close(context.Background()); err != nil { t.Fatal(err) } }() sqldb, err := sql.Open("sqlite", dbPath) if err != nil { t.Fatal(err) } defer sqldb.Close() if _, err := sqldb.Exec(`PRAGMA journal_mode = wal;`); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`CREATE TABLE t (id INT, data TEXT)`); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`INSERT INTO t VALUES (1, 'test data')`); err != nil { t.Fatal(err) } // Sync first to initialize database state if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } // Get baseline checkpoint metrics baselineCount := testutil.ToFloat64(checkpointNCounterVec.WithLabelValues(db.Path(), "PASSIVE")) baselineSeconds := testutil.ToFloat64(checkpointSecondsCounterVec.WithLabelValues(db.Path(), "PASSIVE")) // Force checkpoint if err := db.Checkpoint(context.Background(), "PASSIVE"); err != nil { t.Fatal(err) } // Verify checkpoint_count was incremented checkpointCountMetric := checkpointNCounterVec.WithLabelValues(db.Path(), "PASSIVE") checkpointCountValue := testutil.ToFloat64(checkpointCountMetric) if checkpointCountValue <= baselineCount { t.Fatalf("litestream_checkpoint_count=%v, want > %v", checkpointCountValue, baselineCount) } // Verify checkpoint_seconds was recorded checkpointSecondsMetric := checkpointSecondsCounterVec.WithLabelValues(db.Path(), "PASSIVE") checkpointSecondsValue := testutil.ToFloat64(checkpointSecondsMetric) if checkpointSecondsValue <= baselineSeconds { t.Fatalf("litestream_checkpoint_seconds=%v, want > %v", checkpointSecondsValue, baselineSeconds) } } // TestDB_ReplicaSync_OperationMetrics verifies that replica operation metrics // (PUT total and bytes) are incremented when Replica.Sync() uploads LTX files. func TestDB_ReplicaSync_OperationMetrics(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "db") db := NewDB(dbPath) db.MonitorInterval = 0 db.Replica = NewReplica(db) db.Replica.Client = &testReplicaClient{dir: t.TempDir()} db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } defer func() { if err := db.Close(context.Background()); err != nil { t.Fatal(err) } }() sqldb, err := sql.Open("sqlite", dbPath) if err != nil { t.Fatal(err) } defer sqldb.Close() if _, err := sqldb.Exec(`PRAGMA journal_mode = wal`); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`CREATE TABLE t (id INT, data TEXT)`); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`INSERT INTO t VALUES (1, 'test data')`); err != nil { t.Fatal(err) } if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } baselinePutTotal := testutil.ToFloat64( internal.OperationTotalCounterVec.WithLabelValues("test", "PUT")) baselinePutBytes := testutil.ToFloat64( internal.OperationBytesCounterVec.WithLabelValues("test", "PUT")) if err := db.Replica.Sync(context.Background()); err != nil { t.Fatal(err) } putTotal := testutil.ToFloat64( internal.OperationTotalCounterVec.WithLabelValues("test", "PUT")) putBytes := testutil.ToFloat64( internal.OperationBytesCounterVec.WithLabelValues("test", "PUT")) if putTotal <= baselinePutTotal { t.Fatalf("litestream_replica_operation_total[test,PUT]=%v, want > %v", putTotal, baselinePutTotal) } if putBytes <= baselinePutBytes { t.Fatalf("litestream_replica_operation_bytes[test,PUT]=%v, want > %v", putBytes, baselinePutBytes) } } // TestDB_Sync_ErrorMetrics verifies that sync error counter is incremented on failure. func TestDB_Sync_ErrorMetrics(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "db") workingClient := &testReplicaClient{dir: t.TempDir()} db := NewDB(dbPath) db.MonitorInterval = 0 db.Replica = NewReplica(db) db.Replica.Client = workingClient db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } defer func() { _ = db.Close(context.Background()) }() sqldb, err := sql.Open("sqlite", dbPath) if err != nil { t.Fatal(err) } defer sqldb.Close() if _, err := sqldb.Exec(`CREATE TABLE t (id INT, data TEXT)`); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`INSERT INTO t VALUES (1, 'test data')`); err != nil { t.Fatal(err) } if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`INSERT INTO t VALUES (2, 'more data')`); err != nil { t.Fatal(err) } baselineErrors := testutil.ToFloat64(syncErrorNCounterVec.WithLabelValues(db.Path())) if err := os.Remove(db.WALPath()); err != nil { t.Fatal(err) } if err := db.Sync(context.Background()); err == nil { t.Fatal("expected error from sync with missing WAL") } syncErrorValue := testutil.ToFloat64(syncErrorNCounterVec.WithLabelValues(db.Path())) if syncErrorValue <= baselineErrors { t.Fatalf("litestream_sync_error_count=%v, want > %v", syncErrorValue, baselineErrors) } } // TestDB_Checkpoint_ErrorMetrics verifies that checkpoint error counter is incremented on failure. func TestDB_Checkpoint_ErrorMetrics(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "db") db := NewDB(dbPath) db.MonitorInterval = 0 db.Replica = NewReplica(db) db.Replica.Client = &testReplicaClient{dir: t.TempDir()} db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } defer func() { _ = db.Close(context.Background()) }() sqldb, err := sql.Open("sqlite", dbPath) if err != nil { t.Fatal(err) } defer sqldb.Close() if _, err := sqldb.Exec(`CREATE TABLE t (id INT, data TEXT)`); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`INSERT INTO t VALUES (1, 'test data')`); err != nil { t.Fatal(err) } if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } baselineErrors := testutil.ToFloat64(checkpointErrorNCounterVec.WithLabelValues(db.Path(), "PASSIVE")) db.db.Close() if err := db.execCheckpoint(context.Background(), "PASSIVE"); err == nil { t.Fatal("expected error from checkpoint with closed db") } checkpointErrorValue := testutil.ToFloat64(checkpointErrorNCounterVec.WithLabelValues(db.Path(), "PASSIVE")) if checkpointErrorValue <= baselineErrors { t.Fatalf("litestream_checkpoint_error_count=%v, want > %v", checkpointErrorValue, baselineErrors) } } // TestDB_L0RetentionMetrics verifies that L0 retention gauges are set during enforcement. func TestDB_L0RetentionMetrics(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "db") client := &testReplicaClient{dir: t.TempDir()} db := NewDB(dbPath) db.MonitorInterval = 0 db.L0Retention = 1 * time.Nanosecond db.Replica = NewReplica(db) db.Replica.Client = client db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } defer func() { _ = db.Close(context.Background()) }() sqldb, err := sql.Open("sqlite", dbPath) if err != nil { t.Fatal(err) } defer sqldb.Close() if _, err := sqldb.Exec(`CREATE TABLE t (id INT, data TEXT)`); err != nil { t.Fatal(err) } for i := range 3 { if _, err := sqldb.Exec(`INSERT INTO t VALUES (?, 'data')`, i); err != nil { t.Fatal(err) } if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if err := db.Replica.Sync(context.Background()); err != nil { t.Fatal(err) } } compactor := NewCompactor(client, slog.Default()) if _, err := compactor.Compact(context.Background(), 1); err != nil { t.Fatal(err) } dbName := filepath.Base(db.Path()) if err := db.EnforceL0RetentionByTime(context.Background()); err != nil { t.Fatal(err) } eligible := testutil.ToFloat64(internal.L0RetentionGaugeVec.WithLabelValues(dbName, "eligible")) notCompacted := testutil.ToFloat64(internal.L0RetentionGaugeVec.WithLabelValues(dbName, "not_compacted")) tooRecent := testutil.ToFloat64(internal.L0RetentionGaugeVec.WithLabelValues(dbName, "too_recent")) if eligible+notCompacted+tooRecent == 0 { t.Fatalf("expected at least one L0 retention gauge > 0, got eligible=%v not_compacted=%v too_recent=%v", eligible, notCompacted, tooRecent) } } // TestDB_Verify_WALOffsetAtHeader tests that verify() handles the edge case where // an LTX file has WALOffset=WALHeaderSize and WALSize=0, which means we're at the // beginning of the WAL with no frames written yet. // Regression test for issue #900: prev WAL offset is less than the header size: -4088 func TestDB_Verify_WALOffsetAtHeader(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "db") db := NewDB(dbPath) db.MonitorInterval = 0 db.Replica = NewReplica(db) db.Replica.Client = &testReplicaClient{dir: t.TempDir()} db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } defer func() { if err := db.Close(context.Background()); err != nil { t.Fatal(err) } }() sqldb, err := sql.Open("sqlite", dbPath) if err != nil { t.Fatal(err) } defer sqldb.Close() if _, err := sqldb.Exec(`PRAGMA journal_mode = wal;`); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`CREATE TABLE t (id INT)`); err != nil { t.Fatal(err) } // Perform initial sync to set up page size and initial state if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } // Read the WAL header to get current salt values walHdr, err := readWALHeader(db.WALPath()) if err != nil { t.Fatal(err) } salt1 := binary.BigEndian.Uint32(walHdr[16:]) salt2 := binary.BigEndian.Uint32(walHdr[20:]) // Create an LTX file with WALOffset=WALHeaderSize (32) and WALSize=0 // This simulates the condition in issue #900 ltxDir := db.LTXLevelDir(0) if err := os.MkdirAll(ltxDir, 0o755); err != nil { t.Fatal(err) } // Get current position to determine next TXID pos, err := db.Pos() if err != nil { t.Fatal(err) } nextTXID := pos.TXID + 1 ltxPath := db.LTXPath(0, nextTXID, nextTXID) f, err := os.Create(ltxPath) if err != nil { t.Fatal(err) } enc, err := ltx.NewEncoder(f) if err != nil { f.Close() t.Fatal(err) } // Create header with WALOffset=32 (WALHeaderSize) and WALSize=0 hdr := ltx.Header{ Version: ltx.Version, Flags: ltx.HeaderFlagNoChecksum, PageSize: uint32(db.pageSize), Commit: 2, MinTXID: nextTXID, MaxTXID: nextTXID, Timestamp: 1000000, WALOffset: WALHeaderSize, // 32 - at start of WAL WALSize: 0, // No WAL data - this triggers the bug WALSalt1: salt1, WALSalt2: salt2, } if err := enc.EncodeHeader(hdr); err != nil { f.Close() t.Fatal(err) } if err := enc.Close(); err != nil { f.Close() t.Fatal(err) } if err := f.Close(); err != nil { t.Fatal(err) } // Invalidate cached position since we wrote an L0 file directly. db.invalidatePosCache() // Now call verify - before the fix, this would fail with: // "prev WAL offset is less than the header size: -4088" info, err := db.verify(context.Background()) if err != nil { t.Fatalf("verify() returned error: %v", err) } // Verify the returned info is sensible if info.offset != WALHeaderSize { t.Errorf("expected offset=%d, got %d", WALHeaderSize, info.offset) } // Salt matches, so snapshotting should be false if info.snapshotting { t.Errorf("expected snapshotting=false when salt matches, got true") } } // TestDB_Verify_WALOffsetAtHeader_SaltMismatch tests that verify() correctly // triggers a snapshot when WALOffset=WALHeaderSize, WALSize=0, and the salt // values don't match the current WAL header. // Companion test to TestDB_Verify_WALOffsetAtHeader for full branch coverage. func TestDB_Verify_WALOffsetAtHeader_SaltMismatch(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "db") db := NewDB(dbPath) db.MonitorInterval = 0 db.Replica = NewReplica(db) db.Replica.Client = &testReplicaClient{dir: t.TempDir()} db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } defer func() { if err := db.Close(context.Background()); err != nil { t.Fatal(err) } }() sqldb, err := sql.Open("sqlite", dbPath) if err != nil { t.Fatal(err) } defer sqldb.Close() if _, err := sqldb.Exec(`PRAGMA journal_mode = wal;`); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`CREATE TABLE t (id INT)`); err != nil { t.Fatal(err) } // Perform initial sync to set up page size and initial state if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } // Read the WAL header to get current salt values walHdr, err := readWALHeader(db.WALPath()) if err != nil { t.Fatal(err) } salt1 := binary.BigEndian.Uint32(walHdr[16:]) salt2 := binary.BigEndian.Uint32(walHdr[20:]) // Create an LTX file with WALOffset=WALHeaderSize (32) and WALSize=0 // but with DIFFERENT salt values to simulate a salt reset ltxDir := db.LTXLevelDir(0) if err := os.MkdirAll(ltxDir, 0o755); err != nil { t.Fatal(err) } // Get current position to determine next TXID pos, err := db.Pos() if err != nil { t.Fatal(err) } nextTXID := pos.TXID + 1 ltxPath := db.LTXPath(0, nextTXID, nextTXID) f, err := os.Create(ltxPath) if err != nil { t.Fatal(err) } enc, err := ltx.NewEncoder(f) if err != nil { f.Close() t.Fatal(err) } // Create header with WALOffset=32 (WALHeaderSize) and WALSize=0 // Use different salt values to trigger salt mismatch branch hdr := ltx.Header{ Version: ltx.Version, Flags: ltx.HeaderFlagNoChecksum, PageSize: uint32(db.pageSize), Commit: 2, MinTXID: nextTXID, MaxTXID: nextTXID, Timestamp: 1000000, WALOffset: WALHeaderSize, // 32 - at start of WAL WALSize: 0, // No WAL data WALSalt1: salt1 + 1, // Different salt to trigger mismatch WALSalt2: salt2 + 1, // Different salt to trigger mismatch } if err := enc.EncodeHeader(hdr); err != nil { f.Close() t.Fatal(err) } if err := enc.Close(); err != nil { f.Close() t.Fatal(err) } if err := f.Close(); err != nil { t.Fatal(err) } // Invalidate cached position since we wrote an L0 file directly. db.invalidatePosCache() // Call verify - should succeed but indicate snapshotting due to salt mismatch info, err := db.verify(context.Background()) if err != nil { t.Fatalf("verify() returned error: %v", err) } // Verify the returned info indicates snapshotting due to salt reset if info.offset != WALHeaderSize { t.Errorf("expected offset=%d, got %d", WALHeaderSize, info.offset) } if !info.snapshotting { t.Errorf("expected snapshotting=true when salt mismatches, got false") } if info.reason != "wal header salt reset, snapshotting" { t.Errorf("expected reason='wal header salt reset, snapshotting', got %q", info.reason) } } // TestDB_releaseReadLock_DoubleRollback verifies that calling releaseReadLock() // after the read transaction has already been rolled back does not return an error. // This can happen during shutdown when concurrent checkpoint and close operations // both attempt to release the read lock. // Regression test for issue #934. func TestDB_releaseReadLock_DoubleRollback(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "db") db := NewDB(dbPath) db.MonitorInterval = 0 db.Replica = NewReplica(db) db.Replica.Client = &testReplicaClient{dir: t.TempDir()} db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } // Open SQL connection to create a WAL database sqldb, err := sql.Open("sqlite", dbPath) if err != nil { t.Fatal(err) } defer sqldb.Close() if _, err := sqldb.Exec(`PRAGMA journal_mode = wal;`); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`CREATE TABLE t (id INT)`); err != nil { t.Fatal(err) } // Sync to initialize the database and acquire read lock if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } // Verify read transaction exists if db.rtx == nil { t.Fatal("expected read transaction to exist after Sync") } // First rollback - simulates what happens in execCheckpoint() if err := db.rtx.Rollback(); err != nil { t.Fatalf("first rollback failed: %v", err) } // Second call to releaseReadLock() - simulates what happens in Close() // This should NOT return an error even though the transaction is already rolled back. // Before the fix, this would return "sql: transaction has already been committed or rolled back" if err := db.releaseReadLock(); err != nil { t.Fatalf("releaseReadLock() returned error after double rollback: %v", err) } // Clean up - set rtx to nil since we manually rolled it back db.rtx = nil // Close should work without error if err := db.Close(context.Background()); err != nil { t.Fatalf("Close() failed: %v", err) } } // TestDB_CheckpointDoesNotTriggerSnapshot verifies that a checkpoint // followed by a sync does not trigger an unnecessary full snapshot. // This is a regression test for issue #927 (runaway disk usage). // // The bug: After checkpoint truncates WAL, verify() sees old LTX position // is beyond new WAL size and triggers snapshotting=true unnecessarily. func TestDB_CheckpointDoesNotTriggerSnapshot(t *testing.T) { t.Run("TruncateMode", func(t *testing.T) { testCheckpointSnapshot(t, CheckpointModeTruncate) }) t.Run("PassiveMode", func(t *testing.T) { testCheckpointSnapshot(t, CheckpointModePassive) }) } func testCheckpointSnapshot(t *testing.T, mode string) { dir := t.TempDir() dbPath := filepath.Join(dir, "db") db := NewDB(dbPath) db.MonitorInterval = 0 // Disable background monitor db.CheckpointInterval = 0 // Disable time-based checkpoints db.Replica = NewReplica(db) db.Replica.Client = &testReplicaClient{dir: t.TempDir()} db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } defer func() { if err := db.Close(context.Background()); err != nil { t.Fatal(err) } }() sqldb, err := sql.Open("sqlite", dbPath) if err != nil { t.Fatal(err) } defer sqldb.Close() if _, err := sqldb.Exec(`PRAGMA journal_mode = wal;`); err != nil { t.Fatal(err) } // Create initial data if _, err := sqldb.Exec(`CREATE TABLE t (id INT, data TEXT)`); err != nil { t.Fatal(err) } // Insert enough data to have a meaningful WAL for i := 0; i < 100; i++ { data := fmt.Sprintf("test data padding row %d with extra content", i) if _, err := sqldb.Exec(`INSERT INTO t VALUES (?, ?)`, i, data); err != nil { t.Fatal(err) } } ctx := context.Background() // Perform initial sync if err := db.Sync(ctx); err != nil { t.Fatal(err) } pos1, _ := db.Pos() t.Logf("After initial sync: TXID=%d", pos1.TXID) // Make a change and sync to establish "normal" state if _, err := sqldb.Exec(`INSERT INTO t VALUES (9999, 'before checkpoint')`); err != nil { t.Fatal(err) } if err := db.Sync(ctx); err != nil { t.Fatal(err) } pos2, _ := db.Pos() t.Logf("After pre-checkpoint sync: TXID=%d", pos2.TXID) // Call verify() BEFORE checkpoint to confirm snapshotting=false info1, err := db.verify(ctx) if err != nil { t.Fatal(err) } t.Logf("Before checkpoint: verify() snapshotting=%v reason=%q", info1.snapshotting, info1.reason) // Perform checkpoint - this may restart the WAL with new salt if err := db.Checkpoint(ctx, mode); err != nil { t.Fatal(err) } t.Logf("Checkpoint mode=%s completed", mode) posAfterChk, _ := db.Pos() t.Logf("After checkpoint: TXID=%d", posAfterChk.TXID) // Make a small change to create some WAL data if _, err := sqldb.Exec(`INSERT INTO t VALUES (10000, 'after checkpoint')`); err != nil { t.Fatal(err) } // Call verify() AFTER checkpoint - THIS IS THE BUG CHECK // With the bug, snapshotting=true because verify() sees: // - Old LTX has WALOffset+WALSize pointing to old (larger) WAL // - New WAL is truncated (smaller) // - Line 973: info.offset > fi.Size() → "wal truncated" → snapshotting=true info2, err := db.verify(ctx) if err != nil { t.Fatal(err) } t.Logf("After checkpoint: verify() snapshotting=%v reason=%q", info2.snapshotting, info2.reason) // The key assertion: after OUR checkpoint (not external process), // we should NOT require a full snapshot. if info2.snapshotting { t.Errorf("verify() returned snapshotting=true after checkpoint, reason=%q. "+ "This is the bug: checkpoint followed by sync should NOT require full snapshot.", info2.reason) } } // TestDB_MultipleCheckpointsWithWrites tests that multiple checkpoint cycles // don't trigger excessive snapshots. This simulates the scenario from issue #927 // where users reported 5GB snapshots every 3-4 minutes. func TestDB_MultipleCheckpointsWithWrites(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "db") db := NewDB(dbPath) db.MonitorInterval = 0 db.CheckpointInterval = 0 db.Replica = NewReplica(db) db.Replica.Client = &testReplicaClient{dir: t.TempDir()} db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } defer func() { if err := db.Close(context.Background()); err != nil { t.Fatal(err) } }() sqldb, err := sql.Open("sqlite", dbPath) if err != nil { t.Fatal(err) } defer sqldb.Close() if _, err := sqldb.Exec(`PRAGMA journal_mode = wal;`); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`CREATE TABLE t (id INT, data TEXT)`); err != nil { t.Fatal(err) } ctx := context.Background() snapshotCount := 0 // Simulate multiple checkpoint cycles with writes for cycle := 0; cycle < 5; cycle++ { // Insert some data for i := 0; i < 10; i++ { if _, err := sqldb.Exec(`INSERT INTO t VALUES (?, ?)`, cycle*100+i, "data"); err != nil { t.Fatal(err) } } // Sync if err := db.Sync(ctx); err != nil { t.Fatal(err) } // Check if this was a snapshot info, err := db.verify(ctx) if err != nil { t.Fatal(err) } if info.snapshotting { snapshotCount++ t.Logf("Cycle %d: SNAPSHOT triggered, reason=%q", cycle, info.reason) } else { t.Logf("Cycle %d: incremental sync", cycle) } // Checkpoint if err := db.Checkpoint(ctx, CheckpointModePassive); err != nil { t.Fatal(err) } } // We expect only 1 snapshot (the initial one), not one per cycle // With the bug, we'd see a snapshot after every checkpoint if snapshotCount > 1 { t.Errorf("Too many snapshots triggered: %d (expected 1 for initial sync)", snapshotCount) } } // TestIsDiskFullError tests the disk full error detection helper. func TestIsDiskFullError(t *testing.T) { tests := []struct { name string err error expected bool }{ { name: "nil error", err: nil, expected: false, }, { name: "no space left on device", err: errors.New("write /tmp/file: no space left on device"), expected: true, }, { name: "No Space Left On Device (uppercase)", err: errors.New("No Space Left On Device"), expected: true, }, { name: "disk quota exceeded", err: errors.New("write: disk quota exceeded"), expected: true, }, { name: "ENOSPC", err: errors.New("ENOSPC: cannot write file"), expected: true, }, { name: "EDQUOT", err: errors.New("error EDQUOT while writing"), expected: true, }, { name: "regular error", err: errors.New("connection refused"), expected: false, }, { name: "permission denied", err: errors.New("permission denied"), expected: false, }, { name: "wrapped disk full error", err: fmt.Errorf("sync failed: %w", errors.New("no space left on device")), expected: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := isDiskFullError(tt.err) if result != tt.expected { t.Errorf("isDiskFullError(%v) = %v, want %v", tt.err, result, tt.expected) } }) } } // TestIsSQLiteBusyError tests the SQLite busy error detection helper. func TestIsSQLiteBusyError(t *testing.T) { tests := []struct { name string err error expected bool }{ { name: "nil error", err: nil, expected: false, }, { name: "database is locked", err: errors.New("database is locked"), expected: true, }, { name: "SQLITE_BUSY", err: errors.New("SQLITE_BUSY: cannot commit"), expected: true, }, { name: "regular error", err: errors.New("connection refused"), expected: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := isSQLiteBusyError(tt.err) if result != tt.expected { t.Errorf("isSQLiteBusyError(%v) = %v, want %v", tt.err, result, tt.expected) } }) } } // TestDB_IdleCheckpointSnapshotLoop tests for the feedback loop described in issue #997. // After bulk inserts trigger a checkpoint, litestream should NOT enter a self-perpetuating // loop where checkpoint triggers cause repeated LTX file creation on an idle database. // // The bug occurred because: // 1. PASSIVE checkpoint completes but doesn't truncate WAL file // 2. WAL salt changes, new _litestream_seq write goes to offset 32 with new salt // 3. Old WAL frames (with old salt) make file size exceed checkpoint threshold // 4. checkpointIfNeeded() uses file size, triggering another checkpoint // 5. Loop repeats, creating LTX files every sync cycle // // The fix uses logical WAL offset (from LTX) instead of file size for checkpoint decisions. func TestDB_IdleCheckpointSnapshotLoop(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") db := NewDB(dbPath) db.MonitorInterval = 0 db.CheckpointInterval = 0 db.MinCheckpointPageN = 10 // Low threshold to trigger checkpoint easily db.Replica = NewReplica(db) db.Replica.Client = &testReplicaClient{dir: t.TempDir()} db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } defer func() { if err := db.Close(context.Background()); err != nil { t.Fatal(err) } }() sqldb, err := sql.Open("sqlite", dbPath) if err != nil { t.Fatal(err) } defer sqldb.Close() if _, err := sqldb.Exec(`PRAGMA journal_mode = wal;`); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`CREATE TABLE test (id INTEGER PRIMARY KEY, data TEXT)`); err != nil { t.Fatal(err) } ctx := context.Background() // Initial sync if err := db.Sync(ctx); err != nil { t.Fatal(err) } // Bulk inserts WITHOUT transaction (as in the bug report) // This creates many WAL frames that will trigger a checkpoint for i := 0; i < 100; i++ { if _, err := sqldb.Exec(`INSERT INTO test VALUES (?, ?)`, i, "test data padding"); err != nil { t.Fatal(err) } } // Sync and trigger checkpoint via size threshold if err := db.Sync(ctx); err != nil { t.Fatal(err) } // Force a checkpoint that will reset WAL salt if err := db.Checkpoint(ctx, CheckpointModePassive); err != nil { t.Fatal(err) } afterCheckpointPos, _ := db.Pos() // Now the database is IDLE - no more application writes // Simulate multiple sync cycles (as would happen with MonitorInterval) for cycle := 0; cycle < 5; cycle++ { if err := db.Sync(ctx); err != nil { t.Fatal(err) } } finalPos, _ := db.Pos() // The key assertion: TXID should not be incrementing every cycle. // With the bug, TXID would increment 5 times (one per cycle). // The fix ensures checkpoint decisions use logical WAL size, // preventing spurious checkpoints when WAL file contains stale frames. txidGrowth := int(finalPos.TXID - afterCheckpointPos.TXID) if txidGrowth > 1 { t.Errorf("TXID grew by %d during idle cycles (expected <= 1). "+ "This is issue #997: checkpoint triggers infinite LTX creation loop.", txidGrowth) } } // TestDB_Issue994_RunawayDiskUsage reproduces the scenario from issue #994 where // the local -litestream directory grows unboundedly. The reporter saw ~10MB/s growth // in LTX files. This test verifies that after bulk writes and idle sync cycles, // local LTX file count and total size stabilize rather than growing linearly. func TestDB_Issue994_RunawayDiskUsage(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") db := NewDB(dbPath) db.MonitorInterval = 0 db.CheckpointInterval = 0 db.MinCheckpointPageN = 10 db.Replica = NewReplica(db) db.Replica.Client = &testReplicaClient{dir: t.TempDir()} db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } defer func() { if err := db.Close(context.Background()); err != nil { t.Fatal(err) } }() sqldb, err := sql.Open("sqlite", dbPath) if err != nil { t.Fatal(err) } defer sqldb.Close() if _, err := sqldb.Exec(`PRAGMA journal_mode = wal`); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`CREATE TABLE test (id INTEGER PRIMARY KEY, data TEXT)`); err != nil { t.Fatal(err) } ctx := context.Background() if err := db.Sync(ctx); err != nil { t.Fatal(err) } // Bulk inserts without a wrapping transaction (matches the #994 scenario). // This builds up WAL frames and will trigger checkpoint thresholds. for i := 0; i < 200; i++ { if _, err := sqldb.Exec(`INSERT INTO test VALUES (?, ?)`, i, "padding data for disk usage test"); err != nil { t.Fatal(err) } } // Sync to create LTX files from the WAL data. if err := db.Sync(ctx); err != nil { t.Fatal(err) } // Force a checkpoint (mirrors what happens in production after bulk writes). if err := db.Checkpoint(ctx, CheckpointModePassive); err != nil { t.Fatal(err) } // Measure the baseline LTX directory size after initial sync + checkpoint. baselineSize := dirSize(t, db.LTXDir()) baselineFiles := dirFileCount(t, db.LTXDir()) t.Logf("baseline: %d bytes, %d files", baselineSize, baselineFiles) // Run 20 idle sync cycles (no application writes). // With the #994 bug, each cycle would create a new LTX snapshot file, // causing linear disk growth. for cycle := 0; cycle < 20; cycle++ { if err := db.Sync(ctx); err != nil { t.Fatal(err) } } finalSize := dirSize(t, db.LTXDir()) finalFiles := dirFileCount(t, db.LTXDir()) t.Logf("after 20 idle cycles: %d bytes, %d files", finalSize, finalFiles) // Allow for at most 1 additional LTX file (the _litestream_seq bookkeeping write). // With the bug, we'd see 20+ new files. newFiles := finalFiles - baselineFiles if newFiles > 2 { t.Errorf("LTX file count grew by %d during 20 idle sync cycles (expected <= 2). "+ "This indicates issue #994: runaway LTX file creation.", newFiles) } // Size should not grow significantly. Allow 2x as generous margin. if baselineSize > 0 && finalSize > baselineSize*2 { t.Errorf("LTX directory grew from %d to %d bytes during idle cycles (>2x growth). "+ "This indicates issue #994: runaway disk usage.", baselineSize, finalSize) } } func dirSize(t *testing.T, path string) int64 { t.Helper() var size int64 err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error { if err != nil { return err } if !info.IsDir() { size += info.Size() } return nil }) if err != nil { t.Fatal(err) } return size } func dirFileCount(t *testing.T, path string) int { t.Helper() var count int err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error { if err != nil { return err } if !info.IsDir() { count++ } return nil }) if err != nil { t.Fatal(err) } return count } // TestDB_WALPageCoverage_AllNewPagesPresent verifies that when SQLite grows a // database (increases page count), ALL new pages appear as WAL frames. This // test exercises SQLite's allocateBtreePage code path which calls // sqlite3PagerWrite on every newly allocated page. // // If this test passes, it confirms that SQLite does not skip WAL writes when // growing the database — Ben Bjohnson's skepticism about the zero-fill fix // (PR #1087 comment) is well-founded at the SQLite level. func TestDB_WALPageCoverage_AllNewPagesPresent(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "db") sqldb, err := sql.Open("sqlite", dbPath) if err != nil { t.Fatal(err) } defer sqldb.Close() if _, err := sqldb.Exec(`PRAGMA journal_mode = wal`); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`CREATE TABLE t (id INTEGER PRIMARY KEY, data BLOB)`); err != nil { t.Fatal(err) } for i := 0; i < 100; i++ { blob := make([]byte, 3000) if _, err := sqldb.Exec(`INSERT INTO t VALUES (?, ?)`, i, blob); err != nil { t.Fatal(err) } } walFile, err := os.Open(dbPath + "-wal") if err != nil { t.Fatal(err) } defer walFile.Close() rd, err := NewWALReader(walFile, slog.Default()) if err != nil { t.Fatal(err) } pageMap, _, commit, err := rd.PageMap(context.Background()) if err != nil { t.Fatal(err) } if commit == 0 { t.Fatal("expected non-zero commit from WAL") } lockPgno := ltx.LockPgno(4096) var missing []uint32 for pgno := uint32(1); pgno <= commit; pgno++ { if pgno == lockPgno { continue } if _, ok := pageMap[pgno]; !ok { missing = append(missing, pgno) } } t.Logf("commit=%d, pages_in_wal=%d, missing=%d", commit, len(pageMap), len(missing)) if len(missing) > 0 { first := missing[0] last := missing[len(missing)-1] t.Errorf("pages missing from WAL: %d total (first=%d, last=%d, commit=%d)", len(missing), first, last, commit) } } // TestDB_WriteLTXFromWAL_PageGrowthCoverage verifies that an incremental LTX // file produced by writeLTXFromWAL contains all new pages when the database // grows between syncs. This tests the full Litestream sync path. func TestDB_WriteLTXFromWAL_PageGrowthCoverage(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "db") db := NewDB(dbPath) db.MonitorInterval = 0 db.CheckpointInterval = 0 db.Replica = NewReplica(db) db.Replica.Client = &testReplicaClient{dir: t.TempDir()} db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } defer db.Close(context.Background()) sqldb, err := sql.Open("sqlite", dbPath) if err != nil { t.Fatal(err) } defer sqldb.Close() if _, err := sqldb.Exec(`PRAGMA journal_mode = wal`); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`CREATE TABLE t (id INTEGER PRIMARY KEY, data BLOB)`); err != nil { t.Fatal(err) } for i := 0; i < 5; i++ { if _, err := sqldb.Exec(`INSERT INTO t VALUES (?, ?)`, i, make([]byte, 100)); err != nil { t.Fatal(err) } } ctx := context.Background() if err := db.Sync(ctx); err != nil { t.Fatal(err) } pos1, err := db.Pos() if err != nil { t.Fatal(err) } f1, err := os.Open(db.LTXPath(0, pos1.TXID, pos1.TXID)) if err != nil { t.Fatal(err) } dec1 := ltx.NewDecoder(f1) if err := dec1.DecodeHeader(); err != nil { f1.Close() t.Fatal(err) } prevCommit := dec1.Header().Commit f1.Close() t.Logf("after sync 1: txid=%d, commit=%d", pos1.TXID, prevCommit) for i := 5; i < 150; i++ { blob := make([]byte, 3000) if _, err := sqldb.Exec(`INSERT INTO t VALUES (?, ?)`, i, blob); err != nil { t.Fatal(err) } } if err := db.Sync(ctx); err != nil { t.Fatal(err) } pos2, err := db.Pos() if err != nil { t.Fatal(err) } f2, err := os.Open(db.LTXPath(0, pos2.TXID, pos2.TXID)) if err != nil { t.Fatal(err) } defer f2.Close() dec2 := ltx.NewDecoder(f2) if err := dec2.DecodeHeader(); err != nil { t.Fatal(err) } newCommit := dec2.Header().Commit ltx2Pages := make(map[uint32]bool) pageBuf := make([]byte, dec2.Header().PageSize) for { var phdr ltx.PageHeader if err := dec2.DecodePage(&phdr, pageBuf); err == io.EOF { break } else if err != nil { t.Fatal(err) } ltx2Pages[phdr.Pgno] = true } lockPgno := ltx.LockPgno(dec2.Header().PageSize) var missing []uint32 for pgno := prevCommit + 1; pgno <= newCommit; pgno++ { if pgno == lockPgno { continue } if !ltx2Pages[pgno] { missing = append(missing, pgno) } } t.Logf("after sync 2: txid=%d, prevCommit=%d, newCommit=%d, pages_in_ltx=%d, missing=%d", pos2.TXID, prevCommit, newCommit, len(ltx2Pages), len(missing)) if len(missing) > 0 { first := missing[0] last := missing[len(missing)-1] t.Errorf("pages missing from incremental LTX: %d total (first=%d, last=%d, prevCommit=%d, newCommit=%d)", len(missing), first, last, prevCommit, newCommit) } } // TestDB_Sync_CompactionValidAfterGrowthAndCheckpoint verifies that compaction // produces valid snapshots after a cycle of: grow DB, sync, checkpoint, grow // more, sync. If the zero-fill bug existed, compaction would fail with // "nonsequential page numbers in snapshot transaction". func TestDB_Sync_CompactionValidAfterGrowthAndCheckpoint(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "db") db := NewDB(dbPath) db.MonitorInterval = 0 db.CheckpointInterval = 0 db.Replica = NewReplica(db) db.Replica.Client = &testReplicaClient{dir: t.TempDir()} db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } defer db.Close(context.Background()) sqldb, err := sql.Open("sqlite", dbPath) if err != nil { t.Fatal(err) } defer sqldb.Close() if _, err := sqldb.Exec(`PRAGMA journal_mode = wal`); err != nil { t.Fatal(err) } ctx := context.Background() if _, err := sqldb.Exec(`CREATE TABLE t (id INTEGER PRIMARY KEY, data BLOB)`); err != nil { t.Fatal(err) } if err := db.Sync(ctx); err != nil { t.Fatal(err) } for i := 0; i < 50; i++ { blob := make([]byte, 3000) if _, err := sqldb.Exec(`INSERT INTO t VALUES (?, ?)`, i, blob); err != nil { t.Fatal(err) } } if err := db.Sync(ctx); err != nil { t.Fatal(err) } if err := db.Checkpoint(ctx, CheckpointModeTruncate); err != nil { t.Fatal(err) } for i := 50; i < 100; i++ { blob := make([]byte, 3000) if _, err := sqldb.Exec(`INSERT INTO t VALUES (?, ?)`, i, blob); err != nil { t.Fatal(err) } } if err := db.Sync(ctx); err != nil { t.Fatal(err) } pos, err := db.Pos() if err != nil { t.Fatal(err) } t.Logf("final txid=%d", pos.TXID) var readers []io.ReadCloser for txid := ltx.TXID(1); txid <= pos.TXID; txid++ { path := db.LTXPath(0, txid, txid) f, err := os.Open(path) if err != nil { if os.IsNotExist(err) { continue } t.Fatal(err) } readers = append(readers, f) } defer func() { for _, r := range readers { r.Close() } }() if len(readers) < 2 { t.Fatalf("expected at least 2 LTX files, got %d", len(readers)) } ioReaders := make([]io.Reader, len(readers)) for i, r := range readers { ioReaders[i] = r } var buf bytes.Buffer c, err := ltx.NewCompactor(&buf, ioReaders) if err != nil { t.Fatalf("new compactor: %v", err) } c.HeaderFlags = ltx.HeaderFlagNoChecksum if err := c.Compact(ctx); err != nil { t.Fatalf("compaction failed (this would indicate the zero-fill bug): %v", err) } t.Logf("compaction succeeded: %d bytes, %d input files", buf.Len(), len(readers)) } // TestDB_Sync_InitErrorMetrics verifies that sync error counter is incremented // when db.init() fails. Regression test for issue #1128. func TestDB_Sync_InitErrorMetrics(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "db") // Create a directory at the DB path so init() will fail when trying to // open it as a SQLite database. if err := os.Mkdir(dbPath, 0o755); err != nil { t.Fatal(err) } db := NewDB(dbPath) db.MonitorInterval = 0 db.ShutdownSyncTimeout = 0 db.Replica = NewReplica(db) db.Replica.Client = &testReplicaClient{dir: t.TempDir()} db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } defer func() { _ = db.Close(context.Background()) }() baselineErrors := testutil.ToFloat64(syncErrorNCounterVec.WithLabelValues(db.Path())) err := db.Sync(context.Background()) if err == nil { t.Fatal("expected Sync to return error when init fails, got nil") } syncErrorValue := testutil.ToFloat64(syncErrorNCounterVec.WithLabelValues(db.Path())) if syncErrorValue <= baselineErrors { t.Fatalf("litestream_sync_error_count=%v, want > %v (init error should be counted)", syncErrorValue, baselineErrors) } } ================================================ FILE: db_shutdown_test.go ================================================ package litestream_test import ( "context" "errors" "io" "strings" "sync/atomic" "testing" "time" "github.com/superfly/ltx" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/internal/testingutil" "github.com/benbjohnson/litestream/mock" ) func TestDB_Close_SyncRetry(t *testing.T) { t.Run("SucceedsAfterTransientFailure", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) // Write some data to create LTX files if _, err := sqldb.Exec(`CREATE TABLE t (x)`); err != nil { t.Fatal(err) } if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if err := sqldb.Close(); err != nil { t.Fatal(err) } // Create mock client that fails first 2 times, succeeds on 3rd var attempts int32 client := &mock.ReplicaClient{ LTXFilesFunc: func(_ context.Context, _ int, _ ltx.TXID, _ bool) (ltx.FileIterator, error) { return ltx.NewFileInfoSliceIterator(nil), nil }, WriteLTXFileFunc: func(_ context.Context, _ int, _, _ ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { n := atomic.AddInt32(&attempts, 1) if n < 3 { return nil, errors.New("rate limited (429)") } // Drain the reader _, _ = io.Copy(io.Discard, r) return <x.FileInfo{}, nil }, } db.Replica = litestream.NewReplicaWithClient(db, client) db.ShutdownSyncTimeout = 5 * time.Second db.ShutdownSyncInterval = 50 * time.Millisecond // Close should succeed after retries if err := db.Close(context.Background()); err != nil { t.Fatalf("expected success after retries, got: %v", err) } if got := atomic.LoadInt32(&attempts); got < 3 { t.Fatalf("expected at least 3 attempts, got %d", got) } }) t.Run("FailsAfterTimeout", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) // Write some data if _, err := sqldb.Exec(`CREATE TABLE t (x)`); err != nil { t.Fatal(err) } if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if err := sqldb.Close(); err != nil { t.Fatal(err) } // Create mock client that always fails var attempts int32 client := &mock.ReplicaClient{ LTXFilesFunc: func(_ context.Context, _ int, _ ltx.TXID, _ bool) (ltx.FileIterator, error) { return ltx.NewFileInfoSliceIterator(nil), nil }, WriteLTXFileFunc: func(_ context.Context, _ int, _, _ ltx.TXID, _ io.Reader) (*ltx.FileInfo, error) { atomic.AddInt32(&attempts, 1) return nil, errors.New("persistent error") }, } db.Replica = litestream.NewReplicaWithClient(db, client) db.ShutdownSyncTimeout = 300 * time.Millisecond db.ShutdownSyncInterval = 50 * time.Millisecond // Close should fail with timeout error err := db.Close(context.Background()) if err == nil { t.Fatal("expected error after timeout") } if !strings.Contains(err.Error(), "timeout") { t.Fatalf("expected timeout error, got: %v", err) } // Should have made multiple attempts if got := atomic.LoadInt32(&attempts); got < 2 { t.Fatalf("expected multiple retry attempts, got %d", got) } }) t.Run("RespectsContextCancellation", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) // Write some data if _, err := sqldb.Exec(`CREATE TABLE t (x)`); err != nil { t.Fatal(err) } if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if err := sqldb.Close(); err != nil { t.Fatal(err) } // Create mock client that always fails client := &mock.ReplicaClient{ LTXFilesFunc: func(_ context.Context, _ int, _ ltx.TXID, _ bool) (ltx.FileIterator, error) { return ltx.NewFileInfoSliceIterator(nil), nil }, WriteLTXFileFunc: func(_ context.Context, _ int, _, _ ltx.TXID, _ io.Reader) (*ltx.FileInfo, error) { return nil, errors.New("error") }, } db.Replica = litestream.NewReplicaWithClient(db, client) db.ShutdownSyncTimeout = 10 * time.Second db.ShutdownSyncInterval = 50 * time.Millisecond // Cancel context after short delay ctx, cancel := context.WithTimeout(context.Background(), 150*time.Millisecond) defer cancel() start := time.Now() _ = db.Close(ctx) elapsed := time.Since(start) // Should exit within reasonable time of context cancellation if elapsed > 500*time.Millisecond { t.Fatalf("took too long to respect cancellation: %v", elapsed) } }) t.Run("ZeroTimeoutNoRetry", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) // Write some data if _, err := sqldb.Exec(`CREATE TABLE t (x)`); err != nil { t.Fatal(err) } if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if err := sqldb.Close(); err != nil { t.Fatal(err) } // Create mock client that always fails var attempts int32 client := &mock.ReplicaClient{ LTXFilesFunc: func(_ context.Context, _ int, _ ltx.TXID, _ bool) (ltx.FileIterator, error) { return ltx.NewFileInfoSliceIterator(nil), nil }, WriteLTXFileFunc: func(_ context.Context, _ int, _, _ ltx.TXID, _ io.Reader) (*ltx.FileInfo, error) { atomic.AddInt32(&attempts, 1) return nil, errors.New("error") }, } db.Replica = litestream.NewReplicaWithClient(db, client) db.ShutdownSyncTimeout = 0 // Disable retries db.ShutdownSyncInterval = 50 * time.Millisecond // Close should fail after single attempt start := time.Now() err := db.Close(context.Background()) elapsed := time.Since(start) if err == nil { t.Fatal("expected error") } // Should have only made 1 attempt if got := atomic.LoadInt32(&attempts); got != 1 { t.Fatalf("expected exactly 1 attempt with zero timeout, got %d", got) } // Should be fast (no retry delay) if elapsed > 100*time.Millisecond { t.Fatalf("took too long for single attempt: %v", elapsed) } }) t.Run("SuccessFirstAttempt", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) // Write some data if _, err := sqldb.Exec(`CREATE TABLE t (x)`); err != nil { t.Fatal(err) } if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if err := sqldb.Close(); err != nil { t.Fatal(err) } // Create mock client that succeeds immediately var attempts int32 client := &mock.ReplicaClient{ LTXFilesFunc: func(_ context.Context, _ int, _ ltx.TXID, _ bool) (ltx.FileIterator, error) { return ltx.NewFileInfoSliceIterator(nil), nil }, WriteLTXFileFunc: func(_ context.Context, _ int, _, _ ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { atomic.AddInt32(&attempts, 1) // Drain the reader _, _ = io.Copy(io.Discard, r) return <x.FileInfo{}, nil }, } db.Replica = litestream.NewReplicaWithClient(db, client) db.ShutdownSyncTimeout = 5 * time.Second db.ShutdownSyncInterval = 50 * time.Millisecond // Close should succeed immediately if err := db.Close(context.Background()); err != nil { t.Fatalf("expected success, got: %v", err) } // Should have made exactly 1 attempt if got := atomic.LoadInt32(&attempts); got != 1 { t.Fatalf("expected exactly 1 attempt, got %d", got) } }) t.Run("DoneChannelInterruptsRetryLoop", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) // Write some data if _, err := sqldb.Exec(`CREATE TABLE t (x)`); err != nil { t.Fatal(err) } if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if err := sqldb.Close(); err != nil { t.Fatal(err) } // Create mock client that always fails var attempts int32 client := &mock.ReplicaClient{ LTXFilesFunc: func(_ context.Context, _ int, _ ltx.TXID, _ bool) (ltx.FileIterator, error) { return ltx.NewFileInfoSliceIterator(nil), nil }, WriteLTXFileFunc: func(_ context.Context, _ int, _, _ ltx.TXID, _ io.Reader) (*ltx.FileInfo, error) { atomic.AddInt32(&attempts, 1) return nil, errors.New("persistent error") }, } db.Replica = litestream.NewReplicaWithClient(db, client) db.ShutdownSyncTimeout = 10 * time.Second db.ShutdownSyncInterval = 50 * time.Millisecond // Create done channel and close it after short delay done := make(chan struct{}) db.Done = done go func() { time.Sleep(200 * time.Millisecond) close(done) }() start := time.Now() err := db.Close(context.Background()) elapsed := time.Since(start) // Should exit quickly (well before 10 second timeout) if elapsed > 2*time.Second { t.Fatalf("took too long to respond to done signal: %v", elapsed) } // Should have error mentioning interrupt if err == nil { t.Fatal("expected error after done signal") } if !errors.Is(err, litestream.ErrShutdownInterrupted) { t.Fatalf("expected ErrShutdownInterrupted, got: %v", err) } // Should have made at least 1 attempt before being interrupted if got := atomic.LoadInt32(&attempts); got < 1 { t.Fatalf("expected at least 1 attempt, got %d", got) } }) t.Run("AlreadyClosedDoneSkipsSync", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) // Write some data if _, err := sqldb.Exec(`CREATE TABLE t (x)`); err != nil { t.Fatal(err) } if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if err := sqldb.Close(); err != nil { t.Fatal(err) } // Create mock client that always fails var attempts int32 client := &mock.ReplicaClient{ LTXFilesFunc: func(_ context.Context, _ int, _ ltx.TXID, _ bool) (ltx.FileIterator, error) { return ltx.NewFileInfoSliceIterator(nil), nil }, WriteLTXFileFunc: func(_ context.Context, _ int, _, _ ltx.TXID, _ io.Reader) (*ltx.FileInfo, error) { atomic.AddInt32(&attempts, 1) return nil, errors.New("persistent error") }, } db.Replica = litestream.NewReplicaWithClient(db, client) db.ShutdownSyncTimeout = 10 * time.Second db.ShutdownSyncInterval = 50 * time.Millisecond // Close done before calling Close done := make(chan struct{}) close(done) db.Done = done start := time.Now() err := db.Close(context.Background()) elapsed := time.Since(start) // Should exit immediately if elapsed > 500*time.Millisecond { t.Fatalf("took too long with pre-closed done channel: %v", elapsed) } // Should have error mentioning interrupt if err == nil { t.Fatal("expected error with pre-closed done channel") } if !errors.Is(err, litestream.ErrShutdownInterrupted) { t.Fatalf("expected ErrShutdownInterrupted, got: %v", err) } // Should not have made any sync attempts if got := atomic.LoadInt32(&attempts); got != 0 { t.Fatalf("expected 0 sync attempts with pre-closed done, got %d", got) } }) t.Run("NilDoneBehavesLikeClose", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) // Write some data if _, err := sqldb.Exec(`CREATE TABLE t (x)`); err != nil { t.Fatal(err) } if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if err := sqldb.Close(); err != nil { t.Fatal(err) } // Create mock client that succeeds immediately var attempts int32 client := &mock.ReplicaClient{ LTXFilesFunc: func(_ context.Context, _ int, _ ltx.TXID, _ bool) (ltx.FileIterator, error) { return ltx.NewFileInfoSliceIterator(nil), nil }, WriteLTXFileFunc: func(_ context.Context, _ int, _, _ ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { atomic.AddInt32(&attempts, 1) _, _ = io.Copy(io.Discard, r) return <x.FileInfo{}, nil }, } db.Replica = litestream.NewReplicaWithClient(db, client) db.ShutdownSyncTimeout = 5 * time.Second db.ShutdownSyncInterval = 50 * time.Millisecond // Done is nil by default, Close should work normally if err := db.Close(context.Background()); err != nil { t.Fatalf("expected success, got: %v", err) } if got := atomic.LoadInt32(&attempts); got != 1 { t.Fatalf("expected exactly 1 attempt, got %d", got) } }) } ================================================ FILE: db_test.go ================================================ package litestream_test import ( "context" "fmt" "hash/crc64" "os" "path/filepath" "strings" "sync" "testing" "time" "github.com/superfly/ltx" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/internal/testingutil" "github.com/benbjohnson/litestream/mock" ) func TestDB_Path(t *testing.T) { db := testingutil.NewDB(t, "/tmp/db") if got, want := db.Path(), `/tmp/db`; got != want { t.Fatalf("Path()=%v, want %v", got, want) } } func TestDB_WALPath(t *testing.T) { db := testingutil.NewDB(t, "/tmp/db") if got, want := db.WALPath(), `/tmp/db-wal`; got != want { t.Fatalf("WALPath()=%v, want %v", got, want) } } func TestDB_MetaPath(t *testing.T) { t.Run("Absolute", func(t *testing.T) { db := testingutil.NewDB(t, "/tmp/db") if got, want := db.MetaPath(), `/tmp/.db-litestream`; got != want { t.Fatalf("MetaPath()=%v, want %v", got, want) } }) t.Run("Relative", func(t *testing.T) { db := testingutil.NewDB(t, "db") if got, want := db.MetaPath(), `.db-litestream`; got != want { t.Fatalf("MetaPath()=%v, want %v", got, want) } }) } // Ensure we can compute a checksum on the real database. func TestDB_CRC64(t *testing.T) { t.Run("ErrNotExist", func(t *testing.T) { db := testingutil.MustOpenDB(t) defer testingutil.MustCloseDB(t, db) if _, _, err := db.CRC64(context.Background()); !os.IsNotExist(err) { t.Fatalf("unexpected error: %#v", err) } }) t.Run("DB", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) t.Log("sync database") if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } t.Log("compute crc64") chksum0, _, err := db.CRC64(context.Background()) if err != nil { t.Fatal(err) } t.Log("issue change") // Issue change that is applied to the WAL. Checksum should not change. if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INT);`); err != nil { t.Fatal(err) } else if chksum1, _, err := db.CRC64(context.Background()); err != nil { t.Fatal(err) } else if chksum0 == chksum1 { t.Fatal("expected different checksum event after WAL change") } t.Log("checkpointing database") // Checkpoint change into database. Checksum should change. if err := db.Checkpoint(context.Background(), litestream.CheckpointModeTruncate); err != nil { t.Fatal(err) } t.Log("compute crc64 again") if chksum2, _, err := db.CRC64(context.Background()); err != nil { t.Fatal(err) } else if chksum0 == chksum2 { t.Fatal("expected different checksums after checkpoint") } }) } // Ensure we can sync the real WAL to the shadow WAL. func TestDB_Sync(t *testing.T) { // Ensure sync is skipped if no database exists. t.Run("NoDB", func(t *testing.T) { db := testingutil.MustOpenDB(t) defer testingutil.MustCloseDB(t, db) if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } }) // Ensure sync can successfully run on the initial sync. t.Run("Initial", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } // Verify page size if now available. if db.PageSize() == 0 { t.Fatal("expected page size after initial sync") } // Obtain real WAL size. fi, err := os.Stat(db.WALPath()) if err != nil { t.Fatal(err) } else if fi.Size() == 0 { t.Fatal("expected wal") } // Ensure position now available. if pos, err := db.Pos(); err != nil { t.Fatal(err) } else if got, want := pos.TXID, ltx.TXID(1); got != want { t.Fatalf("pos.Index=%v, want %v", got, want) } }) // Ensure DB can keep in sync across multiple Sync() invocations. t.Run("MultiSync", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Execute a query to force a write to the WAL. if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE foo (bar TEXT);`); err != nil { t.Fatal(err) } // Perform initial sync & grab initial position. if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } pos0, err := db.Pos() if err != nil { t.Fatal(err) } // Insert into table. if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO foo (bar) VALUES ('baz');`); err != nil { t.Fatal(err) } // Sync to ensure position moves forward one page. if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } else if pos1, err := db.Pos(); err != nil { t.Fatal(err) } else if got, want := pos1.TXID, pos0.TXID+1; got != want { t.Fatalf("TXID=%v, want %v", got, want) } }) // Ensure a WAL file is created if one does not already exist. t.Run("NoWAL", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Issue initial sync and truncate WAL. if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } // Obtain initial position. if _, err := db.Pos(); err != nil { t.Fatal(err) } // Checkpoint & fully close which should close WAL file. if err := db.Checkpoint(context.Background(), litestream.CheckpointModeTruncate); err != nil { t.Fatal(err) } if err := db.Close(context.Background()); err != nil { t.Fatal(err) } else if err := sqldb.Close(); err != nil { t.Fatal(err) } // Remove WAL file. if err := os.Remove(db.WALPath()); err != nil && !os.IsNotExist(err) { t.Fatal(err) } // Reopen the managed database. db = testingutil.MustOpenDBAt(t, db.Path()) defer testingutil.MustCloseDB(t, db) // Re-sync and ensure new generation has been created. if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } // Obtain initial position. if _, err := db.Pos(); err != nil { t.Fatal(err) } }) // Ensure DB can start new generation if it detects it cannot verify last position. t.Run("OverwritePrevPosition", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Execute a query to force a write to the WAL. if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE foo (bar TEXT);`); err != nil { t.Fatal(err) } // Issue initial sync and truncate WAL. if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } // Obtain initial position. if _, err := db.Pos(); err != nil { t.Fatal(err) } // Fully close which should close WAL file. if err := db.Close(t.Context()); err != nil { t.Fatal(err) } else if err := sqldb.Close(); err != nil { t.Fatal(err) } // Verify WAL does not exist. if _, err := os.Stat(db.WALPath()); !os.IsNotExist(err) { t.Fatal(err) } // Insert into table multiple times to move past old offset sqldb = testingutil.MustOpenSQLDB(t, db.Path()) defer testingutil.MustCloseSQLDB(t, sqldb) for i := 0; i < 100; i++ { if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO foo (bar) VALUES ('baz');`); err != nil { t.Fatal(err) } } // Reopen the managed database. db = testingutil.MustOpenDBAt(t, db.Path()) defer testingutil.MustCloseDB(t, db) // Re-sync and ensure new generation has been created. if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } // Obtain initial position. if _, err := db.Pos(); err != nil { t.Fatal(err) } }) // Ensure DB checkpoints after minimum number of pages. t.Run("MinCheckpointPageN", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Execute a query to force a write to the WAL and then sync. if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE foo (bar TEXT);`); err != nil { t.Fatal(err) } else if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } // Write at least minimum number of pages to trigger rollover. for i := 0; i < db.MinCheckpointPageN; i++ { if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO foo (bar) VALUES ('baz');`); err != nil { t.Fatal(err) } } // Sync to shadow WAL. This should trigger a PASSIVE checkpoint because // we've exceeded MinCheckpointPageN threshold. if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } // Ensure position is now on the third index (TXID 1 = initial, // TXID 2 = after inserts, TXID 3 = after PASSIVE checkpoint). if pos, err := db.Pos(); err != nil { t.Fatal(err) } else if got, want := pos.TXID, ltx.TXID(3); got != want { t.Fatalf("Index=%v, want %v", got, want) } }) // Ensure DB forces a truncate checkpoint once WAL exceeds the threshold. t.Run("TruncatePageN", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) db.TruncatePageN = 1 if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE foo (bar TEXT);`); err != nil { t.Fatal(err) } if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } payloadSize := db.PageSize() if payloadSize == 0 { payloadSize = 4096 } payload := strings.Repeat("x", payloadSize) // Grow the WAL until we have more than one full page worth of changes. for walPageCountForTest(t, db) <= 1 { if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO foo (bar) VALUES (?);`, payload); err != nil { t.Fatal(err) } } if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } if got := walPageCountForTest(t, db); got > 1 { t.Fatalf("expected truncate checkpoint to shrink wal, pages=%d", got) } }) // Ensure DB checkpoints after interval. t.Run("CheckpointInterval", func(t *testing.T) { t.Skip("TODO(ltx)") db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Execute a query to force a write to the WAL and then sync. if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE foo (bar TEXT);`); err != nil { t.Fatal(err) } else if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } // Reduce checkpoint interval to ensure a rollover is triggered. db.CheckpointInterval = 1 * time.Nanosecond // Write to WAL & sync. if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO foo (bar) VALUES ('baz');`); err != nil { t.Fatal(err) } else if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } // Ensure position is now on the second index. if pos, err := db.Pos(); err != nil { t.Fatal(err) } else if got, want := pos.TXID, ltx.TXID(1); got != want { t.Fatalf("Index=%v, want %v", got, want) } }) } func TestDB_Compact(t *testing.T) { // Ensure that raw L0 transactions can be compacted into the first level. t.Run("L1", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INT);`); err != nil { t.Fatal(err) } if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO t (id) VALUES (100)`); err != nil { t.Fatal(err) } if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } if err := db.Replica.Sync(t.Context()); err != nil { t.Fatal(err) } info, err := db.Compact(t.Context(), 1) if err != nil { t.Fatal(err) } if got, want := info.Level, 1; got != want { t.Fatalf("Level=%v, want %v", got, want) } if got, want := info.MinTXID, ltx.TXID(1); got != want { t.Fatalf("MinTXID=%s, want %s", got, want) } if got, want := info.MaxTXID, ltx.TXID(2); got != want { t.Fatalf("MaxTXID=%s, want %s", got, want) } if info.Size == 0 { t.Fatalf("expected non-zero size") } }) // Ensure that higher level compactions pull from the correct levels. t.Run("L2+", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INT);`); err != nil { t.Fatal(err) } // TXID 2 if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO t (id) VALUES (100)`); err != nil { t.Fatal(err) } else if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } if err := db.Replica.Sync(t.Context()); err != nil { t.Fatal(err) } // Compact to L1:1-2 if info, err := db.Compact(t.Context(), 1); err != nil { t.Fatal(err) } else if got, want := ltx.FormatFilename(info.MinTXID, info.MaxTXID), `0000000000000001-0000000000000002.ltx`; got != want { t.Fatalf("Filename=%s, want %s", got, want) } // TXID 3 if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO t (id) VALUES (100)`); err != nil { t.Fatal(err) } else if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } if err := db.Replica.Sync(t.Context()); err != nil { t.Fatal(err) } // Compact to L1:3-3 if info, err := db.Compact(t.Context(), 1); err != nil { t.Fatal(err) } else if got, want := ltx.FormatFilename(info.MinTXID, info.MaxTXID), `0000000000000003-0000000000000003.ltx`; got != want { t.Fatalf("Filename=%s, want %s", got, want) } // Compact to L2:1-3 if info, err := db.Compact(t.Context(), 2); err != nil { t.Fatal(err) } else if got, want := info.Level, 2; got != want { t.Fatalf("Level=%v, want %v", got, want) } else if got, want := ltx.FormatFilename(info.MinTXID, info.MaxTXID), `0000000000000001-0000000000000003.ltx`; got != want { t.Fatalf("Filename=%s, want %s", got, want) } }) } func walPageCountForTest(tb testing.TB, db *litestream.DB) int64 { tb.Helper() fi, err := os.Stat(db.WALPath()) if err != nil { if os.IsNotExist(err) { return 0 } tb.Fatalf("stat wal: %v", err) } pageSize := db.PageSize() if pageSize <= 0 || fi.Size() <= litestream.WALHeaderSize { return 0 } frameSize := int64(litestream.WALFrameHeaderSize + pageSize) return (fi.Size() - litestream.WALHeaderSize) / frameSize } func TestDB_Snapshot(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INT);`); err != nil { t.Fatal(err) } else if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO t (id) VALUES (100)`); err != nil { t.Fatal(err) } else if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } info, err := db.Snapshot(context.Background()) if err != nil { t.Fatal(err) } if got, want := ltx.FormatFilename(info.MinTXID, info.MaxTXID), `0000000000000001-0000000000000002.ltx`; got != want { t.Fatalf("Filename=%s, want %s", got, want) } // Calculate local checksum chksum0, _, err := db.CRC64(t.Context()) if err != nil { t.Fatal(err) } // Fetch remote LTX snapshot file and ensure it matches the checksum of the local database. rc, err := db.Replica.Client.OpenLTXFile(t.Context(), litestream.SnapshotLevel, 1, 2, 0, 0) if err != nil { t.Fatal(err) } defer rc.Close() h := crc64.New(crc64.MakeTable(crc64.ISO)) if err := ltx.NewDecoder(rc).DecodeDatabaseTo(h); err != nil { t.Fatal(err) } else if got, want := h.Sum64(), chksum0; got != want { t.Fatal("snapshot checksum mismatch") } } func TestDB_EnforceRetention(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Create table and sync initial state if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INT);`); err != nil { t.Fatal(err) } else if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } // Create multiple snapshots with delays to test retention for i := 0; i < 3; i++ { if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO t (id) VALUES (?)`, i); err != nil { t.Fatal(err) } else if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } if _, err := db.Snapshot(t.Context()); err != nil { t.Fatal(err) } // Sleep between snapshots to create time differences time.Sleep(100 * time.Millisecond) } // Get list of snapshots before retention itr, err := db.Replica.Client.LTXFiles(t.Context(), litestream.SnapshotLevel, 0, false) if err != nil { t.Fatal(err) } var beforeCount int for itr.Next() { beforeCount++ } itr.Close() if beforeCount != 3 { t.Fatalf("expected 3 snapshots before retention, got %d", beforeCount) } // Enforce retention to remove older snapshots retentionTime := time.Now().Add(-150 * time.Millisecond) if minSnapshotTXID, err := db.EnforceSnapshotRetention(t.Context(), retentionTime); err != nil { t.Fatal(err) } else if got, want := minSnapshotTXID, ltx.TXID(4); got != want { t.Fatalf("MinSnapshotTXID=%s, want %s", got, want) } // Verify snapshots after retention itr, err = db.Replica.Client.LTXFiles(t.Context(), litestream.SnapshotLevel, 0, false) if err != nil { t.Fatal(err) } var afterCount int for itr.Next() { afterCount++ } itr.Close() // Should have at least one snapshot remaining if afterCount < 1 { t.Fatal("expected at least 1 snapshot after retention") } // Should have fewer snapshots than before if afterCount >= beforeCount { t.Fatalf("expected fewer snapshots after retention, before=%d after=%d", beforeCount, afterCount) } } func TestDB_EnforceSnapshotRetention_RetentionDisabled(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Disable retention (let cloud provider handle it). db.RetentionEnabled = false // Create table and sync initial state. if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INT);`); err != nil { t.Fatal(err) } else if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } // Create multiple snapshots with delays. for i := 0; i < 3; i++ { if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO t (id) VALUES (?)`, i); err != nil { t.Fatal(err) } else if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } if _, err := db.Snapshot(t.Context()); err != nil { t.Fatal(err) } time.Sleep(100 * time.Millisecond) } // Count snapshots before retention. countFiles := func() int { t.Helper() itr, err := db.Replica.Client.LTXFiles(t.Context(), litestream.SnapshotLevel, 0, false) if err != nil { t.Fatal(err) } var n int for itr.Next() { n++ } itr.Close() return n } beforeCount := countFiles() if beforeCount != 3 { t.Fatalf("expected 3 snapshots before retention, got %d", beforeCount) } // Enforce retention with skip remote deletion enabled. retentionTime := time.Now().Add(-150 * time.Millisecond) if _, err := db.EnforceSnapshotRetention(t.Context(), retentionTime); err != nil { t.Fatal(err) } // Remote files should all still exist. afterCount := countFiles() if afterCount != beforeCount { t.Fatalf("expected %d remote snapshots (no remote deletion), got %d", beforeCount, afterCount) } } func TestDB_EnforceL0RetentionByTime_RetentionDisabled(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Disable retention and set a short L0 retention. db.RetentionEnabled = false db.L0Retention = time.Nanosecond // Create table and sync to create L0 files. if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INT);`); err != nil { t.Fatal(err) } else if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } for i := 0; i < 3; i++ { if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO t (id) VALUES (?)`, i); err != nil { t.Fatal(err) } else if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } } // Sync replica to upload L0 files to remote storage. if err := db.Replica.Sync(t.Context()); err != nil { t.Fatal(err) } // Count L0 files before. countL0 := func() int { t.Helper() itr, err := db.Replica.Client.LTXFiles(t.Context(), 0, 0, false) if err != nil { t.Fatal(err) } var n int for itr.Next() { n++ } itr.Close() return n } beforeCount := countL0() if beforeCount < 2 { t.Fatalf("expected at least 2 L0 files, got %d", beforeCount) } // Compact L0 to L1 so files become eligible for L0 retention. store := litestream.NewStore([]*litestream.DB{db}, litestream.DefaultCompactionLevels) if _, err := store.CompactDB(t.Context(), db, &litestream.CompactionLevel{Level: 1, Interval: time.Nanosecond}); err != nil { t.Fatal(err) } // Wait a moment for files to become old enough. time.Sleep(10 * time.Millisecond) // Enforce L0 retention. if err := db.EnforceL0RetentionByTime(t.Context()); err != nil { t.Fatal(err) } // Remote L0 files should all still exist. afterCount := countL0() if afterCount != beforeCount { t.Fatalf("expected %d remote L0 files (no remote deletion), got %d", beforeCount, afterCount) } } // TestDB_ConcurrentMapWrite tests for race conditions in maxLTXFileInfos map access. // This test specifically targets the concurrent map write issue found in db.go // where sync() method writes to the map without proper locking. // Run with: go test -race -run TestDB_ConcurrentMapWrite func TestDB_ConcurrentMapWrite(t *testing.T) { // Use the standard test helpers db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Enable monitoring to trigger background operations db.MonitorInterval = 10 * time.Millisecond // Create a table if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INTEGER PRIMARY KEY, value TEXT)`); err != nil { t.Fatal(err) } // Start multiple goroutines to trigger concurrent map access var wg sync.WaitGroup // Number of concurrent operations const numGoroutines = 10 // Channel to signal start start := make(chan struct{}) for i := 0; i < numGoroutines; i++ { wg.Add(1) go func(id int) { defer wg.Done() // Wait for signal to start all goroutines simultaneously <-start // Perform operations that trigger map access for j := 0; j < 5; j++ { // This triggers sync() which had unprotected map access if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO t (value) VALUES (?)`, "test"); err != nil { t.Logf("Goroutine %d: insert error: %v", id, err) } // Trigger Sync manually which accesses the map if err := db.Sync(t.Context()); err != nil { t.Logf("Goroutine %d: sync error: %v", id, err) } // Small delay to allow race to manifest time.Sleep(time.Millisecond) } }(i) } // Additional goroutine for snapshot operations wg.Add(1) go func() { defer wg.Done() <-start for i := 0; i < 3; i++ { // This triggers Snapshot() which has protected map access if _, err := db.Snapshot(t.Context()); err != nil { t.Logf("Snapshot error: %v", err) } time.Sleep(5 * time.Millisecond) } }() // Start all goroutines close(start) // Wait for completion wg.Wait() t.Log("Test completed without race condition") } // TestCompaction_PreservesLastTimestamp verifies that after compaction, // the resulting file's timestamp reflects the last source file timestamp // as recorded in the LTX headers. This ensures point-in-time restoration // continues to work after compaction (issue #771). func TestCompaction_PreservesLastTimestamp(t *testing.T) { ctx := context.Background() dir := t.TempDir() db := testingutil.NewDB(t, filepath.Join(dir, "db")) db.MonitorInterval = 0 db.ShutdownSyncTimeout = 0 replicaPath := filepath.Join(dir, "replica") client := file.NewReplicaClient(replicaPath) db.Replica = litestream.NewReplicaWithClient(db, client) db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } sqldb := testingutil.MustOpenSQLDB(t, db.Path()) defer testingutil.MustCloseDBs(t, db, sqldb) // Create some transactions for i := 0; i < 10; i++ { if _, err := sqldb.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS t (id INTEGER PRIMARY KEY, val TEXT)`); err != nil { t.Fatalf("create table: %v", err) } if _, err := sqldb.ExecContext(ctx, `INSERT INTO t (val) VALUES (?)`, fmt.Sprintf("value-%d", i)); err != nil { t.Fatalf("insert %d: %v", i, err) } // Sync to create L0 files if err := db.Sync(ctx); err != nil { t.Fatalf("sync db: %v", err) } if err := db.Replica.Sync(ctx); err != nil { t.Fatalf("sync replica: %v", err) } } // Record the last L0 file timestamp before compaction itr, err := client.LTXFiles(ctx, 0, 0, false) if err != nil { t.Fatalf("list L0 files: %v", err) } defer itr.Close() l0Files, err := ltx.SliceFileIterator(itr) if err != nil { t.Fatalf("convert iterator: %v", err) } if err := itr.Close(); err != nil { t.Fatalf("close iterator: %v", err) } var lastTime time.Time for _, info := range l0Files { if lastTime.IsZero() || info.CreatedAt.After(lastTime) { lastTime = info.CreatedAt } } if len(l0Files) == 0 { t.Fatal("expected L0 files before compaction") } t.Logf("Found %d L0 files, last timestamp: %v", len(l0Files), lastTime) // Perform compaction from L0 to L1 levels := litestream.CompactionLevels{ {Level: 0}, {Level: 1, Interval: time.Second}, } store := litestream.NewStore([]*litestream.DB{db}, levels) store.CompactionMonitorEnabled = false if err := store.Open(ctx); err != nil { t.Fatalf("open store: %v", err) } defer func() { if err := store.Close(ctx); err != nil { t.Fatalf("close store: %v", err) } }() _, err = store.CompactDB(ctx, db, levels[1]) if err != nil { t.Fatalf("compact: %v", err) } // Verify L1 file has the last timestamp from L0 files itr, err = client.LTXFiles(ctx, 1, 0, false) if err != nil { t.Fatalf("list L1 files: %v", err) } defer itr.Close() l1Files, err := ltx.SliceFileIterator(itr) if err != nil { t.Fatalf("convert L1 iterator: %v", err) } if err := itr.Close(); err != nil { t.Fatalf("close L1 iterator: %v", err) } if len(l1Files) == 0 { t.Fatal("expected L1 file after compaction") } l1Info := l1Files[0] // The L1 file's CreatedAt should be the last timestamp from the L0 files // Allow for some drift due to millisecond precision in LTX headers timeDiff := l1Info.CreatedAt.Sub(lastTime) if timeDiff.Abs() > time.Second { t.Errorf("L1 CreatedAt = %v, last L0 = %v (diff: %v)", l1Info.CreatedAt, lastTime, timeDiff) t.Error("L1 file timestamp should preserve last source file timestamp") } } func TestDB_EnforceRetentionByTXID_LocalCleanup(t *testing.T) { ctx := context.Background() dir := t.TempDir() db := testingutil.NewDB(t, filepath.Join(dir, "db")) db.MonitorInterval = 0 db.ShutdownSyncTimeout = 0 replicaPath := filepath.Join(dir, "replica") client := file.NewReplicaClient(replicaPath) db.Replica = litestream.NewReplicaWithClient(db, client) db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } sqldb := testingutil.MustOpenSQLDB(t, db.Path()) defer testingutil.MustCloseDBs(t, db, sqldb) if _, err := sqldb.ExecContext(ctx, `CREATE TABLE t (id INTEGER PRIMARY KEY, val TEXT)`); err != nil { t.Fatalf("create table: %v", err) } type localFile struct { path string minTXID ltx.TXID maxTXID ltx.TXID } var firstBatchL0Files []localFile for i := 0; i < 3; i++ { if _, err := sqldb.ExecContext(ctx, `INSERT INTO t (val) VALUES (?)`, fmt.Sprintf("batch1-value-%d", i)); err != nil { t.Fatalf("insert batch1 %d: %v", i, err) } if err := db.Sync(ctx); err != nil { t.Fatalf("sync db batch1 %d: %v", i, err) } minTXID, maxTXID, err := db.MaxLTX() if err != nil { t.Fatalf("get max ltx: %v", err) } localPath := db.LTXPath(0, minTXID, maxTXID) firstBatchL0Files = append(firstBatchL0Files, localFile{ path: localPath, minTXID: minTXID, maxTXID: maxTXID, }) if err := db.Replica.Sync(ctx); err != nil { t.Fatalf("sync replica batch1 %d: %v", i, err) } } for _, lf := range firstBatchL0Files { if _, err := os.Stat(lf.path); os.IsNotExist(err) { t.Fatalf("local L0 file should exist before first compaction: %s", lf.path) } } if _, err := db.Compact(ctx, 1); err != nil { t.Fatalf("compact batch1 to L1: %v", err) } for i := 0; i < 3; i++ { if _, err := sqldb.ExecContext(ctx, `INSERT INTO t (val) VALUES (?)`, fmt.Sprintf("batch2-value-%d", i)); err != nil { t.Fatalf("insert batch2 %d: %v", i, err) } if err := db.Sync(ctx); err != nil { t.Fatalf("sync db batch2 %d: %v", i, err) } if err := db.Replica.Sync(ctx); err != nil { t.Fatalf("sync replica batch2 %d: %v", i, err) } } secondCompactInfo, err := db.Compact(ctx, 1) if err != nil { t.Fatalf("compact batch2 to L1: %v", err) } if err := db.EnforceRetentionByTXID(ctx, 0, secondCompactInfo.MinTXID); err != nil { t.Fatalf("enforce retention: %v", err) } for _, lf := range firstBatchL0Files { if lf.maxTXID < secondCompactInfo.MinTXID { if _, err := os.Stat(lf.path); err == nil { t.Errorf("local L0 file should be removed after second compaction: %s (maxTXID=%s < minTXID=%s)", lf.path, lf.maxTXID, secondCompactInfo.MinTXID) } else if !os.IsNotExist(err) { t.Fatalf("unexpected error checking local file: %v", err) } } } } func TestDB_EnforceL0RetentionByTime(t *testing.T) { ctx := context.Background() dir := t.TempDir() db := testingutil.NewDB(t, filepath.Join(dir, "db")) db.MonitorInterval = 0 db.ShutdownSyncTimeout = 0 replicaPath := filepath.Join(dir, "replica") client := file.NewReplicaClient(replicaPath) db.Replica = litestream.NewReplicaWithClient(db, client) db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } sqldb := testingutil.MustOpenSQLDB(t, db.Path()) defer testingutil.MustCloseDBs(t, db, sqldb) // Use a long retention initially so compaction does not immediately clean up files. db.L0Retention = 30 * time.Minute if _, err := sqldb.ExecContext(ctx, `CREATE TABLE t (id INTEGER PRIMARY KEY, val TEXT)`); err != nil { t.Fatalf("create table: %v", err) } for i := 0; i < 3; i++ { if _, err := sqldb.ExecContext(ctx, `INSERT INTO t (val) VALUES (?)`, fmt.Sprintf("value-%d", i)); err != nil { t.Fatalf("insert %d: %v", i, err) } if err := db.Sync(ctx); err != nil { t.Fatalf("sync db %d: %v", i, err) } if err := db.Replica.Sync(ctx); err != nil { t.Fatalf("sync replica %d: %v", i, err) } } if _, err := db.Compact(ctx, 1); err != nil { t.Fatalf("compact L0 -> L1: %v", err) } itr, err := client.LTXFiles(ctx, 0, 0, false) if err != nil { t.Fatalf("list L0 files: %v", err) } l0Files, err := ltx.SliceFileIterator(itr) if err != nil { t.Fatalf("slice iterator: %v", err) } if err := itr.Close(); err != nil { t.Fatalf("close iterator: %v", err) } if len(l0Files) < 2 { t.Fatalf("expected at least two L0 files, got %d", len(l0Files)) } checkExists := func(expectMissing bool) { for idx, info := range l0Files { remotePath := client.LTXFilePath(0, info.MinTXID, info.MaxTXID) localPath := db.LTXPath(0, info.MinTXID, info.MaxTXID) _, remoteErr := os.Stat(remotePath) _, localErr := os.Stat(localPath) if expectMissing && idx < len(l0Files)-1 { if !os.IsNotExist(remoteErr) { t.Fatalf("expected remote file removed: %s", remotePath) } if !os.IsNotExist(localErr) { t.Fatalf("expected local file removed: %s", localPath) } } if !expectMissing || idx == len(l0Files)-1 { if remoteErr != nil { t.Fatalf("expected remote file to exist: %s (%v)", remotePath, remoteErr) } if localErr != nil { t.Fatalf("expected local file to exist: %s (%v)", localPath, localErr) } } } } // Files should still exist immediately after compaction since they are new. if err := db.EnforceL0RetentionByTime(ctx); err != nil { t.Fatalf("enforce recent retention: %v", err) } checkExists(false) // Age the files so they exceed the retention threshold. oldTime := time.Now().Add(-1 * time.Hour) for _, info := range l0Files { remotePath := client.LTXFilePath(0, info.MinTXID, info.MaxTXID) if err := os.Chtimes(remotePath, oldTime, oldTime); err != nil { t.Fatalf("chtimes remote: %v", err) } localPath := db.LTXPath(0, info.MinTXID, info.MaxTXID) if err := os.Chtimes(localPath, oldTime, oldTime); err != nil { t.Fatalf("chtimes local: %v", err) } } // Shorten retention so aged files qualify for deletion. db.L0Retention = time.Second if err := db.EnforceL0RetentionByTime(ctx); err != nil { t.Fatalf("enforce aged retention: %v", err) } checkExists(true) } // TestDB_SyncAfterVacuum verifies that syncing works correctly after a database // shrinks via VACUUM. This tests the fix for issue #875 where page numbers from // earlier transactions in the WAL could exceed the new commit size after shrinking. func TestDB_SyncAfterVacuum(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Create a table and insert enough data to create multiple pages if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INTEGER PRIMARY KEY, data BLOB)`); err != nil { t.Fatal(err) } // Insert enough rows to create many pages for i := 0; i < 100; i++ { if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO t (data) VALUES (?)`, strings.Repeat("x", 4000)); err != nil { t.Fatal(err) } } // Initial sync if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } // Get initial page count var initialPageCount int if err := sqldb.QueryRowContext(t.Context(), `PRAGMA page_count`).Scan(&initialPageCount); err != nil { t.Fatal(err) } t.Logf("Initial page count: %d", initialPageCount) // Delete most data and VACUUM to shrink the database if _, err := sqldb.ExecContext(t.Context(), `DELETE FROM t WHERE id > 10`); err != nil { t.Fatal(err) } if _, err := sqldb.ExecContext(t.Context(), `VACUUM`); err != nil { t.Fatal(err) } // Get new page count var newPageCount int if err := sqldb.QueryRowContext(t.Context(), `PRAGMA page_count`).Scan(&newPageCount); err != nil { t.Fatal(err) } t.Logf("Page count after VACUUM: %d", newPageCount) if newPageCount >= initialPageCount { t.Skip("VACUUM did not shrink database, skipping test") } // This sync should succeed without "page number out-of-bounds" error if err := db.Sync(t.Context()); err != nil { t.Fatalf("sync after VACUUM failed: %v", err) } // Verify position advanced pos, err := db.Pos() if err != nil { t.Fatal(err) } if pos.TXID < 2 { t.Fatalf("expected TXID >= 2, got %d", pos.TXID) } t.Logf("Final position: TXID=%d", pos.TXID) } // TestDB_NoLTXFilesOnIdleSync verifies that syncing an idle database does not // create new LTX files when no external changes have been made. This tests the // fix for issue #896 where time-based checkpoints were creating LTX files even // when no actual database changes occurred. func TestDB_NoLTXFilesOnIdleSync(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Set CheckpointInterval to trigger time-based checkpoints db.CheckpointInterval = time.Millisecond // Create a table and insert some data to ensure we have WAL activity if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INTEGER PRIMARY KEY, data TEXT)`); err != nil { t.Fatal(err) } if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO t (data) VALUES ('test')`); err != nil { t.Fatal(err) } // Initial sync to create first LTX file(s) if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } // Wait for checkpoint interval to pass time.Sleep(10 * time.Millisecond) // Sync again to trigger checkpoint (this will write to _litestream_seq) if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } // Record the current TXID after checkpoint posAfterCheckpoint, err := db.Pos() if err != nil { t.Fatal(err) } t.Logf("TXID after checkpoint: %d", posAfterCheckpoint.TXID) // Wait for checkpoint interval to pass again time.Sleep(10 * time.Millisecond) // Now sync multiple times without any external database changes // This is the key part of the test - with the bug, each sync would create // a new LTX file because the time-based checkpoint would trigger for i := 0; i < 3; i++ { if err := db.Sync(t.Context()); err != nil { t.Fatalf("sync %d failed: %v", i, err) } time.Sleep(10 * time.Millisecond) } // Check final position - it should NOT have advanced significantly // With the bug, TXID would increase by 3 (one for each sync) posAfterIdle, err := db.Pos() if err != nil { t.Fatal(err) } t.Logf("TXID after idle syncs: %d", posAfterIdle.TXID) // The TXID should not have advanced more than 1 from the checkpoint // (accounting for the checkpoint's own _litestream_seq write) if posAfterIdle.TXID > posAfterCheckpoint.TXID+1 { t.Fatalf("expected TXID to stay at or below %d, got %d (bug: LTX files created without changes)", posAfterCheckpoint.TXID+1, posAfterIdle.TXID) } } // TestDB_DelayedCheckpointAfterWrite verifies that writes that happen before // the checkpoint interval elapses will still trigger a checkpoint later when // the interval does elapse. This ensures the syncedSinceCheckpoint flag // persists across sync calls. See issue #896. func TestDB_DelayedCheckpointAfterWrite(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Use a longer checkpoint interval so we can control when it triggers db.CheckpointInterval = 100 * time.Millisecond // Create table and initial sync if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INTEGER PRIMARY KEY, data TEXT)`); err != nil { t.Fatal(err) } if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } // Wait for interval to pass and sync to trigger initial checkpoint time.Sleep(150 * time.Millisecond) if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } // Record TXID after first checkpoint posAfterFirstCheckpoint, err := db.Pos() if err != nil { t.Fatal(err) } t.Logf("TXID after first checkpoint: %d", posAfterFirstCheckpoint.TXID) // Insert data immediately (before interval elapses) if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO t (data) VALUES ('delayed checkpoint test')`); err != nil { t.Fatal(err) } // Sync immediately - this should NOT trigger a checkpoint (interval hasn't elapsed) // but should set syncedSinceCheckpoint = true if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } posAfterInsert, err := db.Pos() if err != nil { t.Fatal(err) } t.Logf("TXID after insert+sync: %d", posAfterInsert.TXID) // Now wait for the interval to pass and sync again (no new data) time.Sleep(150 * time.Millisecond) if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } // A checkpoint should have been triggered because syncedSinceCheckpoint was true // The TXID should have advanced due to the checkpoint posAfterDelayedCheckpoint, err := db.Pos() if err != nil { t.Fatal(err) } t.Logf("TXID after delayed checkpoint: %d", posAfterDelayedCheckpoint.TXID) // The TXID should have advanced from the insert position, indicating the checkpoint ran if posAfterDelayedCheckpoint.TXID <= posAfterInsert.TXID { t.Fatalf("expected TXID to advance after delayed checkpoint (syncedSinceCheckpoint should persist), got insert=%d delayed=%d", posAfterInsert.TXID, posAfterDelayedCheckpoint.TXID) } } func TestDB_SyncStatus(t *testing.T) { t.Run("NoReplica", func(t *testing.T) { db := litestream.NewDB(filepath.Join(t.TempDir(), "db")) db.Replica = nil if _, err := db.SyncStatus(context.Background()); err == nil { t.Fatal("expected error") } }) t.Run("BeforeSync", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) status, err := db.SyncStatus(context.Background()) if err != nil { t.Fatal(err) } if status.LocalTXID != 0 { t.Fatalf("expected LocalTXID=0, got %d", status.LocalTXID) } if status.RemoteTXID != 0 { t.Fatalf("expected RemoteTXID=0, got %d", status.RemoteTXID) } if status.InSync { t.Fatal("expected InSync=false before any sync") } }) t.Run("AfterDBSyncOnly", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } status, err := db.SyncStatus(context.Background()) if err != nil { t.Fatal(err) } if status.LocalTXID == 0 { t.Fatal("expected non-zero LocalTXID after db sync") } if status.RemoteTXID != 0 { t.Fatalf("expected RemoteTXID=0, got %d", status.RemoteTXID) } if status.InSync { t.Fatal("expected InSync=false when remote has not synced") } }) t.Run("AfterFullSync", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if err := db.Replica.Sync(context.Background()); err != nil { t.Fatal(err) } status, err := db.SyncStatus(context.Background()) if err != nil { t.Fatal(err) } if status.LocalTXID == 0 { t.Fatal("expected non-zero LocalTXID") } if status.LocalTXID != status.RemoteTXID { t.Fatalf("expected LocalTXID=%d == RemoteTXID=%d", status.LocalTXID, status.RemoteTXID) } if !status.InSync { t.Fatal("expected InSync=true after full sync") } }) t.Run("AfterNewWrites", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INT)`); err != nil { t.Fatal(err) } if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if err := db.Replica.Sync(context.Background()); err != nil { t.Fatal(err) } if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO t (id) VALUES (1)`); err != nil { t.Fatal(err) } if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } status, err := db.SyncStatus(context.Background()) if err != nil { t.Fatal(err) } if status.LocalTXID <= status.RemoteTXID { t.Fatalf("expected LocalTXID=%d > RemoteTXID=%d", status.LocalTXID, status.RemoteTXID) } if status.InSync { t.Fatal("expected InSync=false after new writes without replica sync") } }) t.Run("CancelledContext", func(t *testing.T) { db := litestream.NewDB(filepath.Join(t.TempDir(), "db")) client := &mock.ReplicaClient{ LTXFilesFunc: func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { return nil, ctx.Err() }, } db.Replica = litestream.NewReplicaWithClient(db, client) ctx, cancel := context.WithCancel(context.Background()) cancel() _, err := db.SyncStatus(ctx) if err == nil { t.Fatal("expected error with cancelled context") } if !strings.Contains(err.Error(), "remote position") { t.Fatalf("expected remote position error, got: %v", err) } }) } func TestDB_SyncAndWait(t *testing.T) { t.Run("NoReplica", func(t *testing.T) { db := litestream.NewDB(filepath.Join(t.TempDir(), "db")) db.Replica = nil if err := db.SyncAndWait(context.Background()); err == nil { t.Fatal("expected error") } }) t.Run("OK", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INT)`); err != nil { t.Fatal(err) } if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO t (id) VALUES (1)`); err != nil { t.Fatal(err) } if err := db.SyncAndWait(context.Background()); err != nil { t.Fatal(err) } status, err := db.SyncStatus(context.Background()) if err != nil { t.Fatal(err) } if !status.InSync { t.Fatalf("expected InSync=true after SyncAndWait, LocalTXID=%d RemoteTXID=%d", status.LocalTXID, status.RemoteTXID) } }) } func TestDB_EnsureExists(t *testing.T) { t.Run("NoReplica", func(t *testing.T) { db := litestream.NewDB(filepath.Join(t.TempDir(), "db")) db.Replica = nil if err := db.EnsureExists(context.Background()); err == nil { t.Fatal("expected error") } }) t.Run("DBAlreadyExists", func(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "db") if err := os.WriteFile(dbPath, []byte("dummy"), 0644); err != nil { t.Fatal(err) } db := litestream.NewDB(dbPath) client := file.NewReplicaClient(filepath.Join(dir, "replica")) db.Replica = litestream.NewReplicaWithClient(db, client) if err := db.EnsureExists(context.Background()); err != nil { t.Fatal(err) } data, err := os.ReadFile(dbPath) if err != nil { t.Fatal(err) } if string(data) != "dummy" { t.Fatal("expected file to remain unchanged") } }) t.Run("NoBackup", func(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "db") db := testingutil.NewDB(t, dbPath) client := file.NewReplicaClient(filepath.Join(dir, "replica")) db.Replica = litestream.NewReplicaWithClient(db, client) if err := db.EnsureExists(context.Background()); err != nil { t.Fatalf("expected nil error for no backup, got %v", err) } if _, err := os.Stat(dbPath); !os.IsNotExist(err) { t.Fatal("expected database file to not exist when no backup available") } }) t.Run("MissingParentDir", func(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "subdir", "nested", "db") db := testingutil.NewDB(t, dbPath) client := file.NewReplicaClient(filepath.Join(dir, "replica")) db.Replica = litestream.NewReplicaWithClient(db, client) if err := db.EnsureExists(context.Background()); err != nil { t.Fatalf("expected nil error, got %v", err) } info, err := os.Stat(filepath.Join(dir, "subdir", "nested")) if err != nil { t.Fatal("expected parent directories to be created") } if !info.IsDir() { t.Fatal("expected parent path to be a directory") } }) t.Run("RestoreFromBackup", func(t *testing.T) { ctx := context.Background() dir := t.TempDir() dbPath := filepath.Join(dir, "db") replicaPath := filepath.Join(dir, "replica") db := testingutil.NewDB(t, dbPath) db.MonitorInterval = 0 db.ShutdownSyncTimeout = 0 client := file.NewReplicaClient(replicaPath) replica := litestream.NewReplicaWithClient(db, client) replica.MonitorEnabled = false db.Replica = replica if err := db.Open(); err != nil { t.Fatal(err) } sqldb := testingutil.MustOpenSQLDB(t, dbPath) if _, err := sqldb.ExecContext(ctx, `CREATE TABLE t (id INT, value TEXT)`); err != nil { t.Fatal(err) } if _, err := sqldb.ExecContext(ctx, `INSERT INTO t (id, value) VALUES (1, 'hello')`); err != nil { t.Fatal(err) } if err := db.SyncAndWait(ctx); err != nil { t.Fatal(err) } if err := sqldb.Close(); err != nil { t.Fatal(err) } if err := db.Close(ctx); err != nil { t.Fatal(err) } if err := os.Remove(dbPath); err != nil { t.Fatal(err) } walPath := dbPath + "-wal" os.Remove(walPath) db2 := testingutil.NewDB(t, dbPath) client2 := file.NewReplicaClient(replicaPath) db2.Replica = litestream.NewReplicaWithClient(db2, client2) if err := db2.EnsureExists(ctx); err != nil { t.Fatalf("EnsureExists: %v", err) } if _, err := os.Stat(dbPath); os.IsNotExist(err) { t.Fatal("expected database file to be restored") } sqldb2 := testingutil.MustOpenSQLDB(t, dbPath) defer sqldb2.Close() var value string if err := sqldb2.QueryRowContext(ctx, `SELECT value FROM t WHERE id = 1`).Scan(&value); err != nil { t.Fatalf("query restored db: %v", err) } if value != "hello" { t.Fatalf("expected 'hello', got %q", value) } }) } // TestDB_ResetLocalState verifies that ResetLocalState clears the LTX directory. func TestDB_ResetLocalState(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Create table and insert some data to create LTX files if _, err := sqldb.Exec(`CREATE TABLE t (x TEXT)`); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`INSERT INTO t (x) VALUES ('foo')`); err != nil { t.Fatal(err) } if err := db.Sync(t.Context()); err != nil { t.Fatal(err) } // Verify LTX directory exists and has files ltxDir := db.LTXDir() if _, err := os.Stat(ltxDir); os.IsNotExist(err) { t.Fatal("LTX directory should exist after sync") } // Get position before reset posBefore, err := db.Pos() if err != nil { t.Fatal(err) } if posBefore.TXID == 0 { t.Fatal("expected non-zero TXID before reset") } // Reset local state if err := db.ResetLocalState(t.Context()); err != nil { t.Fatal(err) } // Verify LTX directory is gone if _, err := os.Stat(ltxDir); !os.IsNotExist(err) { t.Fatal("LTX directory should not exist after reset") } // Get position after reset - should be zero since no LTX files posAfter, err := db.Pos() if err != nil { t.Fatal(err) } if posAfter.TXID != 0 { t.Fatalf("expected zero TXID after reset, got %d", posAfter.TXID) } } ================================================ FILE: docker-compose.test.yml ================================================ services: minio: image: minio/minio:latest ports: - "9000:9000" - "9001:9001" environment: MINIO_ROOT_USER: minioadmin MINIO_ROOT_PASSWORD: minioadmin command: server /data --console-address ":9001" healthcheck: test: ["CMD", "mc", "ready", "local"] interval: 5s timeout: 5s retries: 5 ================================================ FILE: docs/ARCHITECTURE.md ================================================ # Litestream Architecture - Technical Deep Dive ## Table of Contents - [System Layers](#system-layers) - [Core Components](#core-components) - [IPC Server & Control Socket](#ipc-server--control-socket) - [Distributed Leasing](#distributed-leasing) - [Library Convenience Methods](#library-convenience-methods) - [LTX File Format](#ltx-file-format) - [WAL Monitoring Mechanism](#wal-monitoring-mechanism) - [Compaction Process](#compaction-process) - [Transaction Management](#transaction-management) - [Concurrency Model](#concurrency-model) - [State Management](#state-management) - [Initialization Flow](#initialization-flow) - [Error Handling](#error-handling) ## System Layers Litestream follows a layered architecture with clear separation of concerns: ```mermaid graph TB subgraph "Application Layer" CLI[CLI Commands
cmd/litestream/] Config[Configuration
config.go] end subgraph "Core Layer" Store[Store Manager
store.go] DB[Database Manager
db.go] Replica[Replica Manager
replica.go] end subgraph "Storage Abstraction" RC[ReplicaClient Interface
replica_client.go] end subgraph "Storage Implementations" S3[s3/replica_client.go] GCS[gs/replica_client.go] ABS[abs/replica_client.go] OSS[oss/replica_client.go] File[file/replica_client.go] SFTP[sftp/replica_client.go] NATS[nats/replica_client.go] end subgraph "External" SQLite[SQLite Database] Cloud[Cloud Storage] end CLI --> Store Store --> DB DB --> Replica Replica --> RC RC --> S3 RC --> GCS RC --> ABS RC --> OSS RC --> File RC --> SFTP RC --> NATS DB <--> SQLite S3 --> Cloud GCS --> Cloud ABS --> Cloud OSS --> Cloud ``` ### Layer Responsibilities #### 1. Application Layer - **CLI Commands**: User interface for operations (replicate, restore, etc.) - **Configuration**: YAML/environment variable parsing and validation #### 2. Core Layer - **Store**: Multi-database coordination, compaction scheduling - **DB**: Single database management, WAL monitoring, checkpointing - **Replica**: Replication to single destination, position tracking #### 3. Storage Abstraction - **ReplicaClient Interface**: Uniform API for all storage backends #### 4. Storage Implementations - Backend-specific logic (authentication, retries, optimizations) #### 5. IPC Layer - **Server**: Unix socket HTTP server for runtime control (`server.go`) - **Leaser**: Distributed lease interface for coordination (`leaser.go`, `s3/leaser.go`) ## Core Components ### DB Component (db.go) The DB component is the heart of Litestream, managing a single SQLite database: ```go type DB struct { // Core fields path string // Database file path metaPath string // Metadata directory path db *sql.DB // SQLite connection f *os.File // Long-running file descriptor rtx *sql.Tx // Long-running read transaction pageSize int // Database page size // Synchronization mu sync.RWMutex // Protects struct fields chkMu sync.RWMutex // Checkpoint lock notify chan struct{} // WAL change notifications // Lifecycle ctx context.Context cancel func() wg sync.WaitGroup // Configuration MinCheckpointPageN int // Min pages for passive checkpoint TruncatePageN int // Pages before emergency truncate checkpoint CheckpointInterval time.Duration // Time-based passive checkpoint interval MonitorInterval time.Duration // WAL monitoring frequency // Note: MaxCheckpointPageN removed (RESTART mode disabled due to #724) // Metrics dbSizeGauge prometheus.Gauge walSizeGauge prometheus.Gauge txIDGauge prometheus.Gauge } ``` #### Key Methods ```go // Lifecycle func (db *DB) Open() error func (db *DB) Close(ctx context.Context) error // Monitoring func (db *DB) monitor() // Background WAL monitoring func (db *DB) checkWAL() (bool, error) // Check for WAL changes // Checkpointing func (db *DB) Checkpoint(mode string) error func (db *DB) autoCheckpoint() error // Replication func (db *DB) WALReader(pgno uint32) (io.ReadCloser, error) func (db *DB) Sync(ctx context.Context) error // Compaction func (db *DB) Compact(ctx context.Context, destLevel int) (*ltx.FileInfo, error) ``` ### Replica Component (replica.go) Manages replication to a single destination: ```go type Replica struct { db *DB // Parent database Client ReplicaClient // Storage backend client mu sync.RWMutex pos ltx.Pos // Current replication position // Configuration SyncInterval time.Duration MonitorEnabled bool // Lifecycle cancel func() wg sync.WaitGroup } ``` #### Replication Position ```go type Pos struct { TXID TXID // Transaction ID PageNo uint32 // Page number within transaction Checksum uint64 // Running checksum } ``` ### Store Component (store.go) Coordinates multiple databases and manages system-wide resources: ```go type Store struct { mu sync.Mutex dbs []*DB levels CompactionLevels // Configuration SnapshotInterval time.Duration SnapshotRetention time.Duration L0Retention time.Duration L0RetentionCheckInterval time.Duration CompactionMonitorEnabled bool // Lifecycle ctx context.Context cancel func() wg sync.WaitGroup } ``` ## IPC Server & Control Socket Litestream exposes a Unix socket HTTP server for runtime control (`server.go`). ### Socket Configuration ```go type SocketConfig struct { Enabled bool `yaml:"enabled"` // Default: false Path string `yaml:"path"` // Default: "/var/run/litestream.sock" Permissions uint32 `yaml:"permissions"` // Default: 0600 } ``` ### HTTP Endpoints All endpoints are served over the Unix socket via Go's `net/http` mux: | Method | Path | Description | |--------|------|-------------| | `POST` | `/register` | Add a database at runtime | | `POST` | `/unregister` | Remove a database at runtime | | `GET` | `/txid?path=` | Get current transaction ID for a database | | `GET` | `/list` | List all managed databases | | `GET` | `/info` | Server info (version, PID, uptime, database count) | | `POST` | `/start` | Start replication for a database | | `POST` | `/stop` | Stop replication for a database | | `GET` | `/debug/pprof/*` | Standard Go pprof endpoints | ### Request/Response Types ```go type RegisterDatabaseRequest struct { Path string `json:"path"` ReplicaURL string `json:"replica_url"` } type UnregisterDatabaseRequest struct { Path string `json:"path"` Timeout int `json:"timeout,omitempty"` // Seconds } type TXIDResponse struct { TXID uint64 `json:"txid"` } ``` ## Distributed Leasing The `Leaser` interface (`leaser.go`) enables distributed coordination so multiple Litestream instances can safely share a replica destination. ### Interface ```go type Leaser interface { Type() string AcquireLease(ctx context.Context) (*Lease, error) RenewLease(ctx context.Context, lease *Lease) (*Lease, error) ReleaseLease(ctx context.Context, lease *Lease) error } type Lease struct { Generation int64 `json:"generation"` ExpiresAt time.Time `json:"expires_at"` Owner string `json:"owner,omitempty"` ETag string `json:"-"` } ``` ### S3 Implementation (`s3/leaser.go`) - **Defaults**: `DefaultLeaseTTL = 30s`, `DefaultLeasePath = "lock.json"` - **Owner format**: `hostname:pid` (falls back to `pid-N` if hostname unavailable) - **Conditional writes**: Uses `If-Match`/`If-None-Match` on S3 PutObject to prevent races - **Release**: Uses `DeleteObject` with `If-Match` to ensure only the holder can release - **Error types**: `ErrLeaseNotHeld`, `ErrLeaseAlreadyReleased`, `LeaseExistsError{Owner, ExpiresAt}` ### Acquisition Flow 1. Read existing lease from S3 (`lock.json`) 2. If lease exists and is not expired, return `LeaseExistsError` 3. Write new lease with `If-None-Match: *` (first acquire) or `If-Match: ` (expired takeover) 4. If `PreconditionFailed` (412), another instance acquired first 5. Return lease with ETag for subsequent renewal ## Library Convenience Methods These methods on `DB` (`db.go`) simplify common operations when Litestream is used as a library: ```go type SyncStatus struct { LocalTXID ltx.TXID // Local transaction ID RemoteTXID ltx.TXID // Remote transaction ID InSync bool // true if LocalTXID > 0 and equal to RemoteTXID } func (db *DB) SyncStatus(ctx context.Context) (SyncStatus, error) func (db *DB) SyncAndWait(ctx context.Context) error func (db *DB) EnsureExists(ctx context.Context) error ``` - **SyncStatus**: Compares local TXID against remote replica position (performs I/O to query remote) - **SyncAndWait**: Runs `db.Sync()` (WAL to LTX) then `db.Replica.Sync()` (LTX to remote), blocking until both complete - **EnsureExists**: Restores database from replica if local file doesn't exist; no-op if file exists or no backup available. Must be called before `Open()` ## LTX File Format LTX (Log Transaction) files are immutable files containing database changes: ``` +------------------+ | Header | Fixed size header with metadata +------------------+ | | | Page Frames | Variable number of page frames | | +------------------+ | Page Index | Index for efficient page lookup +------------------+ | Trailer | Metadata and checksums +------------------+ ``` ### Header Structure ```go type Header struct { Magic [4]byte // "LTX\x00" Version uint32 // Format version PageSize uint32 // Database page size MinTXID TXID // Starting transaction ID MaxTXID TXID // Ending transaction ID Timestamp int64 // Creation timestamp Checksum uint64 // Header checksum } ``` ### Page Frame Structure ```go type PageFrame struct { Header PageHeader Data []byte // Page data (pageSize bytes) } type PageHeader struct { PageNo uint32 // Page number in database Size uint32 // Size of page data Checksum uint64 // Page checksum } ``` ### Page Index Binary search tree for efficient page lookup: ```go type PageIndexElem struct { PageNo uint32 // Page number Offset int64 // Offset in file Size uint32 // Size of page frame } ``` ### Trailer ```go type Trailer struct { PageIndexOffset int64 // Offset to page index PageIndexSize int64 // Size of page index PageCount uint32 // Total pages in file Checksum uint64 // Full file checksum } ``` ## WAL Monitoring Mechanism ### Monitor Loop (db.go:1499) ```go func (db *DB) monitor() { ticker := time.NewTicker(db.MonitorInterval) defer ticker.Stop() for { select { case <-ticker.C: // Check WAL for changes changed, err := db.checkWAL() if err != nil { slog.Error("wal check failed", "error", err) continue } if changed { // Notify replicas of changes db.notifyReplicas() // Check if checkpoint needed if db.shouldCheckpoint() { db.autoCheckpoint() } } case <-db.ctx.Done(): return } } } ``` ### WAL Change Detection ```go func (db *DB) checkWAL() (bool, error) { // Get current WAL state walInfo, err := db.walInfo() if err != nil { return false, err } // Compare with previous state db.mu.Lock() changed := walInfo.Size != db.prevWALSize || walInfo.Checksum != db.prevWALChecksum db.prevWALSize = walInfo.Size db.prevWALChecksum = walInfo.Checksum db.mu.Unlock() return changed, nil } ``` ## Compaction Process Compaction merges multiple LTX files to reduce storage overhead: ### Compaction Algorithm (store.go:189) High-level compaction flow: 1. Determine whether the level is due for compaction (`Store.shouldCompact`). 2. Enumerate level-`L-1` files using `ReplicaClient.LTXFiles`, preferring local copies via `os.Open(db.LTXPath(...))` and falling back to `ReplicaClient.OpenLTXFile` only when necessary. 3. Stream the source readers through `ltx.NewCompactor`, which performs page-level deduplication and enforces lock-page skipping automatically. 4. Pipe the compactor output into `ReplicaClient.WriteLTXFile` to create the merged LTX file for level `L`. 5. Adjust the returned `ltx.FileInfo.CreatedAt` to the earliest timestamp from the source files so point-in-time recovery remains accurate. 6. Update the cached max file info for the level and delete old L0 files when promoting to level 1. ### Compaction Levels ```go type CompactionLevel struct { Level int // Level number (0 = raw, 1+ = compacted) Interval time.Duration // How often to compact from previous level } // Default configuration var DefaultCompactionLevels = CompactionLevels{ {Level: 0, Interval: 0}, // Raw LTX files {Level: 1, Interval: 1 * Hour}, // Hourly compaction {Level: 2, Interval: 24 * Hour}, // Daily compaction } ``` ## Transaction Management ### Long-Running Read Transaction Litestream maintains a long-running read transaction to ensure consistency: ```go func (db *DB) initReadTx() error { // Start read transaction tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{ ReadOnly: true, }) if err != nil { return err } // Execute dummy query to start transaction var dummy string err = tx.QueryRow("SELECT ''").Scan(&dummy) if err != nil { tx.Rollback() return err } db.rtx = tx return nil } ``` **Purpose:** - Prevents database from being modified during replication - Ensures consistent view of database - Allows reading historical pages from WAL ### Checkpoint Coordination ```go func (db *DB) Checkpoint(mode string) error { // Acquire checkpoint lock db.chkMu.Lock() defer db.chkMu.Unlock() // Close read transaction temporarily if db.rtx != nil { db.rtx.Rollback() db.rtx = nil } // Perform checkpoint _, _, err := db.db.Exec(fmt.Sprintf("PRAGMA wal_checkpoint(%s)", mode)) if err != nil { return err } // Restart read transaction return db.initReadTx() } ``` ## Concurrency Model ### Mutex Usage Patterns ```go // DB struct mutexes type DB struct { mu sync.RWMutex // Protects struct fields chkMu sync.RWMutex // Checkpoint coordination } // Replica struct mutexes type Replica struct { mu sync.RWMutex // Protects position muf sync.Mutex // File descriptor lock } // Store struct mutex type Store struct { mu sync.Mutex // Protects database list } ``` ### Thundering Herd Prevention `Store.Open()` (`store.go`) limits concurrent database opens at startup: ```go g, ctx := errgroup.WithContext(ctx) g.SetLimit(50) // Max 50 concurrent DB opens for _, db := range s.dbs { db := db g.Go(func() error { return db.Open() }) } ``` This prevents OS resource exhaustion (file descriptors, memory) when hundreds of databases are configured. ### Lock Ordering (Prevent Deadlocks) Always acquire locks in this order: 1. Store.mu 2. DB.mu 3. DB.chkMu 4. Replica.mu ### Goroutine Management ```go // Start background task func (db *DB) Start() { db.wg.Add(1) go func() { defer db.wg.Done() db.monitor() }() } // Stop with timeout func (db *DB) Close(ctx context.Context) error { // Signal shutdown db.cancel() // Wait for goroutines with timeout done := make(chan struct{}) go func() { db.wg.Wait() close(done) }() select { case <-done: return nil case <-ctx.Done(): return ctx.Err() } } ``` ## State Management ### Database States ```mermaid stateDiagram-v2 [*] --> Closed Closed --> Opening: Open() Opening --> Open: Success Opening --> Closed: Error Open --> Monitoring: Start() Monitoring --> Syncing: Changes Detected Syncing --> Monitoring: Sync Complete Monitoring --> Checkpointing: Threshold Reached Checkpointing --> Monitoring: Checkpoint Complete Monitoring --> Closing: Close() Closing --> Closed: Cleanup Complete ``` ### Replica States ```mermaid stateDiagram-v2 [*] --> Idle Idle --> Starting: Start() Starting --> Monitoring: Success Starting --> Idle: Error Monitoring --> Syncing: Timer/Changes Syncing --> Uploading: Have Changes Uploading --> Monitoring: Success Uploading --> Error: Failed Error --> Monitoring: Retry Monitoring --> Stopping: Stop() Stopping --> Idle: Cleanup ``` ### Position Tracking ```go type Pos struct { TXID TXID // Current transaction ID PageNo uint32 // Current page number Checksum uint64 // Running checksum for validation } // Update position atomically func (r *Replica) SetPos(pos ltx.Pos) { r.mu.Lock() // MUST use Lock, not RLock! defer r.mu.Unlock() r.pos = pos } // Read position safely func (r *Replica) Pos() ltx.Pos { r.mu.RLock() defer r.mu.RUnlock() return r.pos } ``` ## Initialization Flow ### System Startup Sequence ```mermaid sequenceDiagram participant Main participant Store participant DB participant Replica participant Monitor Main->>Store: NewStore(config) Store->>Store: Validate config Main->>Store: Open() loop For each database Store->>DB: NewDB(path) Store->>DB: Open() DB->>DB: Open SQLite connection DB->>DB: Read page size DB->>DB: Init metadata DB->>DB: Start read transaction loop For each replica DB->>Replica: NewReplica() DB->>Replica: Start() Replica->>Monitor: Start monitoring end end Store->>Store: Start compaction monitors Store-->>Main: Ready ``` ### Critical Initialization Steps 1. **Database Opening** ```go // Must happen in order: 1. Open SQLite connection 2. Read page size (PRAGMA page_size) 3. Create metadata directory 4. Start long-running read transaction 5. Initialize replicas 6. Start monitor goroutine ``` 2. **Replica Initialization** ```go // Must happen in order: 1. Create replica with client 2. Load previous position from metadata 3. Validate position against database 4. Start sync goroutine (if monitoring enabled) ``` ## Error Handling ### Error Categories 1. **Recoverable Errors** - Network timeouts - Temporary storage unavailability - Lock contention 2. **Fatal Errors** - Database corruption - Invalid configuration - Disk full 3. **Operational Errors** - Checkpoint failures - Compaction conflicts - Sync delays ### Error Propagation ```go // Bottom-up error propagation ReplicaClient.WriteLTXFile() error ↓ Replica.Sync() error ↓ DB.Sync() error ↓ Store.monitorDB() // Logs error, continues ``` ### Retry Logic ```go func (r *Replica) syncWithRetry(ctx context.Context) error { backoff := time.Second maxBackoff := time.Minute for attempt := 0; ; attempt++ { err := r.Sync(ctx) if err == nil { return nil } // Check if error is retryable if !isRetryable(err) { return err } // Check context if ctx.Err() != nil { return ctx.Err() } // Exponential backoff time.Sleep(backoff) backoff *= 2 if backoff > maxBackoff { backoff = maxBackoff } } } ``` ## Performance Characteristics ### Time Complexity | Operation | Complexity | Notes | |-----------|------------|-------| | WAL Monitor | O(1) | Fixed interval check | | Page Write | O(1) | Append to LTX file | | Compaction | O(n) | n = total pages | | Restoration | O(n*log(m)) | n = pages, m = files | | File List | O(k) | k = files in level | ### Space Complexity | Component | Memory Usage | Disk Usage | |-----------|-------------|------------| | DB | O(1) + metrics | Original DB + WAL | | Replica | O(1) | LTX files + metadata | | Compaction | O(n) pages | Temporary during merge | | Page Index | O(p) | p = pages in file | ### Optimization Points 1. **Page Index Caching** - Cache frequently accessed indices - Use estimated size for initial fetch 2. **Batch Operations** - Group small changes into larger LTX files - Batch delete operations 3. **Concurrent Operations** - Multiple replicas can sync in parallel - Compaction runs independently per level ## Security Considerations ### Access Control - File permissions: 0600 for database files - Directory permissions: 0700 for metadata - No built-in authentication (rely on storage backend) ## Monitoring & Metrics ### Prometheus Metrics ```go // Database metrics db_size_bytes // Current database size wal_size_bytes // Current WAL size total_wal_bytes // Total bytes written to WAL checkpoint_count // Number of checkpoints sync_count // Number of syncs sync_error_count // Number of sync errors // Replica metrics replica_lag_seconds // Replication lag replica_position // Current replication position ``` ### Health Checks ```go func (db *DB) HealthCheck() error { // Check database connection if err := db.db.Ping(); err != nil { return fmt.Errorf("database ping failed: %w", err) } // Check replication lag for _, r := range db.replicas { lag := time.Since(r.LastSync()) if lag > MaxAcceptableLag { return fmt.Errorf("replica %s lag too high: %v", r.Name(), lag) } } return nil } ``` ================================================ FILE: docs/DOC_MAINTENANCE.md ================================================ # Documentation Maintenance Guide This guide ensures documentation stays synchronized with code changes and follows the principle-based approach established in PR #787. ## Philosophy: Principles Over Examples **Key Insight**: Code examples become outdated quickly. Documentation should focus on **stable concepts** rather than **volatile implementations**. ### What to Document ✅ **DO Document**: - **Architectural principles** (e.g., "DB layer handles database state") - **Interface contracts** (what methods must do, not how they do it) - **Design patterns** (atomic file operations, eventual consistency handling) - **Critical edge cases** (1GB lock page, timestamp preservation) - **"Why" not "what"** (rationale behind decisions) ❌ **DON'T Document**: - Specific function implementations that change frequently - Exact function names without referencing actual source - Step-by-step code that duplicates the implementation - Version-specific details that will quickly become stale ### Documentation Principles 1. **Abstractions over Details**: Document the concept, not the specific implementation 2. **Reference over Duplication**: Point to actual source files instead of copying code 3. **Patterns over Examples**: Describe the approach, let developers read the source 4. **Contracts over Implementations**: Define what must happen, not how ## When Code Changes, Update Docs ### Interface Changes **Trigger**: Modifying `ReplicaClient` interface or any public interface **Required Updates**: 1. Search for interface definitions in docs: ```bash rg "type ReplicaClient interface" docs/ CLAUDE.md AGENTS.md .claude/ ``` 2. Update interface signatures (don't forget parameters!) 3. Document new parameters with clear explanations of when/why to use them 4. Update all example calls to include new parameters **Files to Check**: - `AGENTS.md` - Interface definitions - `docs/REPLICA_CLIENT_GUIDE.md` - Implementation guide - `docs/TESTING_GUIDE.md` - Test examples - `.claude/agents/replica-client-developer.md` - Agent knowledge - `.claude/commands/add-storage-backend.md` - Backend templates - `.claude/commands/validate-replica.md` - Validation commands ### New Features **Trigger**: Adding new functionality, methods, or components **Approach**: 1. **Don't rush to document** - Wait until the feature stabilizes 2. **Document the pattern**, not the implementation: - What problem does it solve? - What's the high-level approach? - What are the critical constraints? 3. **Reference the source**: - `See implementation in file.go:lines` - `Reference tests in file_test.go` ### Refactoring **Trigger**: Moving or renaming functions, restructuring code **Required Actions**: 1. **Search for references**: ```bash # Find function name references rg "functionName" docs/ CLAUDE.md AGENTS.md .claude/ ``` 2. **Update or remove**: - If it's a reference pointer (e.g., "See `DB.init()` in db.go:123"), update it - If it's a code example showing implementation, consider replacing with a pattern description 3. **Verify links**: Ensure all file:line references are still valid ## Documentation Update Checklist Use this checklist when making code changes: - [ ] **Search docs for affected code**: ```bash # Search for function names, types, or concepts rg "YourFunctionName" docs/ CLAUDE.md AGENTS.md .claude/ ``` - [ ] **Update interface definitions** if signatures changed - [ ] **Update examples** if they won't compile anymore - [ ] **Convert brittle examples to patterns** if refactoring made them stale - [ ] **Update file:line references** if code moved - [ ] **Verify contracts still hold** (update if behavior changed) - [ ] **Run markdownlint**: ```bash markdownlint --fix docs/ CLAUDE.md AGENTS.md .claude/ ``` ## Preventing Documentation Drift ### Pre-Commit Practices 1. **Search before committing**: ```bash git diff --name-only | xargs -I {} rg "basename {}" docs/ ``` 2. **Review doc references** in your PR description 3. **Test examples compile** (if they're meant to) ### Regular Audits **Monthly**: Spot-check one documentation file against current codebase **Questions to ask**: - Do interface definitions match `replica_client.go`? - Do code examples compile? - Are file:line references accurate? - Have we removed outdated examples? ### When in Doubt **Rule**: Delete outdated documentation rather than let it mislead - Stale examples cause compilation errors - Outdated patterns cause architectural mistakes - Incorrect references waste developer time **Better**: A brief pattern description + reference to source than an outdated example ## Example: Good vs Bad Documentation Updates ### ❌ Bad: Copying Implementation ```markdown ### How to initialize DB ```go func (db *DB) init() { db.mu.Lock() defer db.mu.Unlock() // ... 50 lines of code copied from db.go } \``` ``` **Problem**: This will be outdated as soon as the implementation changes. ### ✅ Good: Documenting Pattern + Reference ```markdown ### DB Initialization Pattern **Principle**: Database initialization must complete before replication starts. **Pattern**: 1. Acquire exclusive lock (`mu.Lock()`) 2. Verify database state consistency 3. Initialize monitoring subsystems 4. Set up replication coordination **Critical**: Use `Lock()` not `RLock()` as initialization modifies state. **Reference Implementation**: See `DB.init()` in db.go:150-230 ``` **Benefits**: Stays accurate even if implementation details change, focuses on the "why" and "what" rather than the "how". ## Tools and Commands ### Find Documentation References ```bash # Find all code examples in documentation rg "^```(go|golang)" docs/ CLAUDE.md AGENTS.md .claude/ # Find file:line references rg "\.go:\d+" docs/ CLAUDE.md AGENTS.md .claude/ # Find interface definitions rg "type .* interface" docs/ CLAUDE.md AGENTS.md .claude/ ``` ### Validate Markdown ```bash # Lint all docs markdownlint docs/ CLAUDE.md AGENTS.md .claude/ # Auto-fix issues markdownlint --fix docs/ CLAUDE.md AGENTS.md .claude/ ``` ### Check for Broken References ```bash # List all go files mentioned in docs rg -o "[a-z_]+\.go:\d+" docs/ CLAUDE.md AGENTS.md | sort -u # Verify they exist and line numbers are reasonable ``` ## Resources - **PR #787**: Original principle-based documentation refactor - **Issue #805**: Context for why accurate documentation matters - **INNOQ Best Practices**: - **Google Style Guide**: ## Questions? When updating documentation, ask: 1. **Is this a stable concept or a volatile implementation?** - Stable → Document the principle - Volatile → Reference the source 2. **Will this stay accurate for 6+ months?** - Yes → Keep it - No → Replace with pattern description 3. **Does this explain WHY or just WHAT?** - WHY → Valuable documentation - WHAT → Code already shows this, just reference it 4. **Would a link to source code be better?** - Often, yes! ================================================ FILE: docs/LTX_FORMAT.md ================================================ # LTX Format Specification LTX (Log Transaction) is Litestream's custom format for storing database changes in an immutable, append-only manner. ## Table of Contents - [Overview](#overview) - [File Structure](#file-structure) - [Header Format](#header-format) - [Page Frames](#page-frames) - [Page Index](#page-index) - [Trailer Format](#trailer-format) - [File Naming Convention](#file-naming-convention) - [Checksum Calculation](#checksum-calculation) - [Compaction and Levels](#compaction-and-levels) - [Reading LTX Files](#reading-ltx-files) - [Writing LTX Files](#writing-ltx-files) - [Relationship to SQLite WAL](#relationship-to-sqlite-wal) ## Overview LTX files are immutable snapshots of database changes: - **Immutable**: Once written, never modified - **Append-only**: New changes create new files - **Self-contained**: Each file is independent - **Indexed**: Contains page index for efficient seeks - **Checksummed**: Integrity verification built-in ```mermaid graph LR WAL[SQLite WAL] -->|Convert| LTX[LTX File] LTX -->|Upload| Storage[Cloud Storage] Storage -->|Download| Restore[Restored DB] ``` ## File Structure ``` ┌─────────────────────┐ │ Header │ Fixed size (varies by version) ├─────────────────────┤ │ │ │ Page Frames │ Variable number of pages │ │ ├─────────────────────┤ │ Page Index │ Binary search tree ├─────────────────────┤ │ Trailer │ Fixed size metadata └─────────────────────┘ ``` ### Size Calculation ```go FileSize = HeaderSize + (PageCount * (PageHeaderSize + PageSize)) + PageIndexSize + TrailerSize ``` ## Header Format The LTX header contains metadata about the file: ```go // From github.com/superfly/ltx type Header struct { Version int // Derived from the magic string ("LTX1") Flags uint32 // Reserved flag bits PageSize uint32 // Database page size Commit uint32 // Page count after applying file MinTXID TXID MaxTXID TXID Timestamp int64 // Milliseconds since Unix epoch PreApplyChecksum Checksum // Database checksum before apply WALOffset int64 // Offset within source WAL (0 for snapshots) WALSize int64 // WAL byte length (0 for snapshots) WALSalt1 uint32 WALSalt2 uint32 NodeID uint64 } const HeaderFlagNoChecksum = uint32(1 << 1) ``` > Note: the version is implied by the magic string. Present files use > `Magic == "LTX1"`, which corresponds to `ltx.Version == 2`. ### Binary Layout (Header) ``` Offset Size Field 0 4 Magic ("LTX1") 4 4 Flags 8 4 PageSize 12 4 Commit 16 8 MinTXID 24 8 MaxTXID 32 8 Timestamp 40 8 PreApplyChecksum 48 8 WALOffset 56 8 WALSize 64 4 WALSalt1 68 4 WALSalt2 72 8 NodeID 80 20 Reserved (zeros) Total: 100 bytes ``` ## Page Frames Each page frame contains a database page with metadata: ```go type PageFrame struct { Header PageHeader Data []byte // Size = PageSize from LTX header } type PageHeader struct { Pgno uint32 // Database page number (1-based) } ``` ### Binary Layout (Page Frame) ``` Offset Size Field 0 4 Page Number (Pgno) 4 PageSize Page Data ``` ### Page Frame Constraints 1. **Sequential Writing**: Pages written in order during creation 2. **Random Access**: Can seek to any page using index 3. **Lock Page Skipping**: Page at 1GB boundary never included 4. **Deduplication**: In compacted files, only latest version of each page ## Page Index The page index enables efficient random access to pages: ```go type PageIndexElem struct { Level int MinTXID TXID MaxTXID TXID Offset int64 // Byte offset of encoded payload Size int64 // Bytes occupied by encoded payload } ``` ### Binary Layout (Page Index) ``` Rather than parsing raw bytes, call `ltx.DecodePageIndex` which returns a map of page number to `ltx.PageIndexElem` for you. ``` ### Index Usage ```go // Finding a page using the index func findPage(index []PageIndexElem, targetPageNo uint32) (offset int64, found bool) { // Binary search idx := sort.Search(len(index), func(i int) bool { return index[i].PageNo >= targetPageNo }) if idx < len(index) && index[idx].PageNo == targetPageNo { return index[idx].Offset, true } return 0, false } ``` ## Trailer Format The trailer contains metadata and pointers: ```go type Trailer struct { PostApplyChecksum Checksum // Database checksum after apply FileChecksum Checksum // CRC-64 checksum of entire file } ``` ### Binary Layout (Trailer) ``` Offset Size Field 0 8 PostApplyChecksum 8 8 FileChecksum Total: 16 bytes ``` ### Reading Trailer The trailer is always at the end of the file: ```go func readTrailer(f *os.File) (*Trailer, error) { // Seek to trailer position _, err := f.Seek(-TrailerSize, io.SeekEnd) if err != nil { return nil, err } var trailer Trailer err = binary.Read(f, binary.BigEndian, &trailer) return &trailer, err } ``` ## File Naming Convention LTX files follow a strict naming pattern: ``` Format: MMMMMMMMMMMMMMMM-NNNNNNNNNNNNNNNN.ltx Where: M = MinTXID (16 hex digits, zero-padded) N = MaxTXID (16 hex digits, zero-padded) Examples: 0000000000000001-0000000000000064.ltx (TXID 1-100) 0000000000000065-00000000000000c8.ltx (TXID 101-200) ``` ### Parsing Filenames ```go // From github.com/superfly/ltx func ParseFilename(name string) (minTXID, maxTXID TXID, err error) { // Remove extension name = strings.TrimSuffix(name, ".ltx") // Split on hyphen parts := strings.Split(name, "-") if len(parts) != 2 { return 0, 0, errors.New("invalid format") } // Parse hex values min, err := strconv.ParseUint(parts[0], 16, 64) max, err := strconv.ParseUint(parts[1], 16, 64) return TXID(min), TXID(max), nil } func FormatFilename(minTXID, maxTXID TXID) string { return fmt.Sprintf("%016x-%016x.ltx", minTXID, maxTXID) } ``` ## Checksum Calculation LTX uses CRC-64 ECMA checksums: ```go import "hash/crc64" var crcTable = crc64.MakeTable(crc64.ECMA) func calculateChecksum(data []byte) uint64 { return crc64.Checksum(data, crcTable) } // Cumulative checksum for multiple pages func cumulativeChecksum(pages [][]byte) uint64 { h := crc64.New(crcTable) for _, page := range pages { h.Write(page) } return h.Sum64() } ``` ### Verification During Read ```go func verifyPage(header PageHeader, data []byte) error { if header.Checksum == 0 { return nil // Checksums disabled } calculated := calculateChecksum(data) if calculated != header.Checksum { return fmt.Errorf("checksum mismatch: expected %x, got %x", header.Checksum, calculated) } return nil } ``` ## Compaction and Levels LTX files are organized in levels for efficient compaction: ``` Level 0: Raw files (no compaction) /ltx/0000/0000000000000001-0000000000000064.ltx /ltx/0000/0000000000000065-00000000000000c8.ltx Level 1: Hourly compaction /ltx/0001/0000000000000001-0000000000000fff.ltx Level 2: Daily compaction /ltx/0002/0000000000000001-000000000000ffff.ltx Snapshots: Full database state /snapshots/20240101120000.ltx ``` ### Compaction Process ```go func compactLTXFiles(files []*LTXFile) (*LTXFile, error) { // Create page map (newer overwrites older) pageMap := make(map[uint32]Page) for _, file := range files { for _, page := range file.Pages { pageMap[page.Number] = page } } // Create new LTX with merged pages merged := <XFile{ MinTXID: files[0].MinTXID, MaxTXID: files[len(files)-1].MaxTXID, } // Add pages in order (skip lock page) for pgno := uint32(1); pgno <= maxPgno; pgno++ { if pgno == LockPageNumber(pageSize) { continue // Skip 1GB lock page } if page, ok := pageMap[pgno]; ok { merged.Pages = append(merged.Pages, page) } } return merged, nil } ``` ## Reading LTX Files ### Complete File Read ```go func ReadLTXFile(path string) (*LTXFile, error) { f, err := os.Open(path) if err != nil { return nil, err } defer f.Close() dec := ltx.NewDecoder(f) // Read and verify header header, err := dec.Header() if err != nil { return nil, err } // Read all pages var pages []Page for { var pageHeader ltx.PageHeader pageData := make([]byte, header.PageSize) err := dec.DecodePage(&pageHeader, pageData) if err == io.EOF { break } if err != nil { return nil, err } pages = append(pages, Page{ Number: pageHeader.PageNo, Data: pageData, }) } return <XFile{ Header: header, Pages: pages, }, nil } ``` ### Partial Read Using Index ```go func ReadPage(path string, pageNo uint32) ([]byte, error) { f, err := os.Open(path) if err != nil { return nil, err } defer f.Close() // Read trailer to find index trailer, err := readTrailer(f) if err != nil { return nil, err } // Read page index f.Seek(trailer.PageIndexOffset, io.SeekStart) indexData := make([]byte, trailer.PageIndexSize) f.Read(indexData) index := parsePageIndex(indexData) // Find page in index offset, found := findPage(index, pageNo) if !found { return nil, errors.New("page not found") } // Read page at offset f.Seek(offset, io.SeekStart) var pageHeader PageHeader binary.Read(f, binary.BigEndian, &pageHeader) pageData := make([]byte, pageSize) f.Read(pageData) return pageData, nil } ``` ## Writing LTX Files ### Creating New LTX File ```go func WriteLTXFile(path string, pages []Page) error { f, err := os.Create(path) if err != nil { return err } defer f.Close() enc := ltx.NewEncoder(f) // Write header header := ltx.Header{ Version: ltx.Version, Flags: 0, PageSize: 4096, PageCount: uint32(len(pages)), MinTXID: minTXID, MaxTXID: maxTXID, } if err := enc.EncodeHeader(header); err != nil { return err } // Write pages and build index var index []PageIndexElem for _, page := range pages { offset := enc.Offset() // Skip lock page if page.Number == LockPageNumber(header.PageSize) { continue } pageHeader := ltx.PageHeader{ PageNo: page.Number, Checksum: calculateChecksum(page.Data), } if err := enc.EncodePage(pageHeader, page.Data); err != nil { return err } index = append(index, PageIndexElem{ PageNo: page.Number, Offset: offset, }) } // Write page index if err := enc.EncodePageIndex(index); err != nil { return err } // Write trailer if err := enc.EncodeTrailer(); err != nil { return err } return enc.Close() } ``` ## Relationship to SQLite WAL ### WAL to LTX Conversion ```mermaid sequenceDiagram participant SQLite participant WAL participant Litestream participant LTX SQLite->>WAL: Write transaction WAL->>WAL: Append frames Litestream->>WAL: Monitor changes WAL-->>Litestream: Read frames Litestream->>Litestream: Convert frames Note over Litestream: - Skip lock page
- Add checksums
- Build index Litestream->>LTX: Write LTX file LTX->>Storage: Upload ``` ### Key Differences | Aspect | SQLite WAL | LTX Format | |--------|------------|------------| | Purpose | Temporary changes | Permanent archive | | Mutability | Mutable (checkpoint) | Immutable | | Structure | Sequential frames | Indexed pages | | Checksum | Per-frame | Per-page + cumulative | | Lock Page | Contains lock bytes | Always skipped | | Naming | Fixed (-wal suffix) | TXID range | | Lifetime | Until checkpoint | Forever | | Size | Grows until checkpoint | Fixed at creation | ### Transaction ID (TXID) ```go type TXID uint64 // TXID represents a logical transaction boundary // Not directly from SQLite, but derived from: // 1. WAL checkpoint sequence // 2. Frame count // 3. Logical grouping of changes func (db *DB) nextTXID() TXID { // Increment from last known TXID return db.lastTXID + 1 } ``` ## Best Practices ### 1. Always Skip Lock Page ```go const PENDING_BYTE = 0x40000000 func shouldSkipPage(pageNo uint32, pageSize int) bool { lockPage := uint32(PENDING_BYTE/pageSize) + 1 return pageNo == lockPage } ``` ### 2. Preserve Timestamps During Compaction ```go // Keep earliest CreatedAt from source files func compactWithTimestamp(files []*FileInfo) *FileInfo { earliest := files[0].CreatedAt for _, f := range files[1:] { if f.CreatedAt.Before(earliest) { earliest = f.CreatedAt } } return &FileInfo{ CreatedAt: earliest, // Preserve for point-in-time recovery } } ``` ### 3. Verify Checksums on Read ```go func safeReadLTX(path string) (*LTXFile, error) { file, err := ReadLTXFile(path) if err != nil { return nil, err } // Verify all checksums for _, page := range file.Pages { if err := verifyPage(page); err != nil { return nil, fmt.Errorf("corrupted page %d: %w", page.Number, err) } } return file, nil } ``` ### 4. Handle Partial Files ```go // For eventually consistent storage func readWithRetry(client ReplicaClient, info *FileInfo) ([]byte, error) { for attempts := 0; attempts < 5; attempts++ { data, err := client.OpenLTXFile(...) if err == nil { // Verify we got complete file if int64(len(data)) == info.Size { return data, nil } } time.Sleep(time.Second * time.Duration(attempts+1)) } return nil, errors.New("incomplete file after retries") } ``` ## Debugging LTX Files ### Inspect LTX Files The Litestream CLI currently exposes a single helper for listing LTX files: ```bash litestream ltx /path/to/db.sqlite litestream ltx s3://bucket/db ``` For low-level inspection (page payloads, checksums, etc.), use the Go API: ```go f, err := os.Open("0000000000000001-0000000000000064.ltx") if err != nil { log.Fatal(err) } defer f.Close() dec := ltx.NewDecoder(f) if err := dec.DecodeHeader(); err != nil { log.Fatal(err) } for { var hdr ltx.PageHeader data := make([]byte, dec.Header().PageSize) if err := dec.DecodePage(&hdr, data); err == io.EOF { break } else if err != nil { log.Fatal(err) } // Inspect hdr.Pgno or data here. } if err := dec.Close(); err != nil { log.Fatal(err) } fmt.Println("post-apply checksum:", dec.Trailer().PostApplyChecksum) ``` ## Summary LTX format provides: 1. **Immutable history** - Every change preserved 2. **Efficient storage** - Indexed, compressed via compaction 3. **Data integrity** - Checksums at multiple levels 4. **Point-in-time recovery** - Via TXID ranges 5. **Cloud-optimized** - Designed for object storage Understanding LTX is essential for: - Implementing replica clients - Debugging replication issues - Optimizing compaction - Ensuring data integrity - Building recovery tools ================================================ FILE: docs/PATTERNS.md ================================================ # Litestream Code Patterns and Anti-Patterns This document contains detailed code patterns, examples, and anti-patterns for working with Litestream. For a quick overview, see [AGENTS.md](../AGENTS.md). ## Table of Contents - [Architectural Boundaries](#architectural-boundaries) - [Atomic File Operations](#atomic-file-operations) - [Error Handling](#error-handling) - [Locking Patterns](#locking-patterns) - [Compaction and Eventual Consistency](#compaction-and-eventual-consistency) - [Resumable Reader Pattern](#resumable-reader-pattern) - [Retention Bypass Pattern](#retention-bypass-pattern) - [Conditional Write Pattern (Distributed Locking)](#conditional-write-pattern-distributed-locking) - [Timestamp Preservation](#timestamp-preservation) - [Common Pitfalls](#common-pitfalls) - [Component Reference](#component-reference) ## Architectural Boundaries ### Layer Responsibilities ```text DB Layer (db.go) → Database state, restoration, monitoring Replica Layer (replica.go) → Replication mechanics only Storage Layer → ReplicaClient implementations ``` ### DO: Handle database state in DB layer Database restoration logic belongs in the DB layer, not the Replica layer. When the database is behind the replica (local TXID < remote TXID): 1. **Clear local L0 cache**: Remove the entire L0 directory and recreate it 2. **Fetch latest L0 file from replica**: Download the most recent L0 LTX file 3. **Write using atomic file operations**: Prevent partial/corrupted files ```go // CORRECT - DB layer handles database state func (db *DB) init() error { // DB layer handles database state if db.needsRestore() { if err := db.restore(); err != nil { return err } } // Then start replica for replication only return db.replica.Start() } func (r *Replica) Start() error { // Replica focuses only on replication return r.startSync() } ``` Reference: `DB.checkDatabaseBehindReplica()` in db.go:670-737 ### DON'T: Put database state logic in Replica layer ```go // WRONG - Replica should only handle replication concerns func (r *Replica) Start() error { // DON'T check database state here if needsRestore() { // Wrong layer! restoreDatabase() // Wrong layer! } // Replica should focus only on replication mechanics } ``` ## Atomic File Operations Always use atomic writes to prevent partial/corrupted files. ### DO: Write to temp file, then rename ```go // CORRECT - Atomic file write pattern func writeFileAtomic(path string, data []byte) error { // Create temp file in same directory (for atomic rename) dir := filepath.Dir(path) tmpFile, err := os.CreateTemp(dir, ".tmp-*") if err != nil { return fmt.Errorf("create temp file: %w", err) } tmpPath := tmpFile.Name() // Clean up temp file on error defer func() { if tmpFile != nil { tmpFile.Close() os.Remove(tmpPath) } }() // Write data to temp file if _, err := tmpFile.Write(data); err != nil { return fmt.Errorf("write temp file: %w", err) } // Sync to ensure data is on disk if err := tmpFile.Sync(); err != nil { return fmt.Errorf("sync temp file: %w", err) } // Close before rename if err := tmpFile.Close(); err != nil { return fmt.Errorf("close temp file: %w", err) } tmpFile = nil // Prevent defer cleanup // Atomic rename (on same filesystem) if err := os.Rename(tmpPath, path); err != nil { os.Remove(tmpPath) return fmt.Errorf("rename to final path: %w", err) } return nil } ``` ### DON'T: Write directly to final location ```go // WRONG - Can leave partial files on failure func writeFileDirect(path string, data []byte) error { return os.WriteFile(path, data, 0644) // Not atomic! } ``` ## Error Handling ### Decision Rule When you handle an error, ask: "Does the caller need to know about this failure?" - **Yes → return the error.** This is the default for virtually all cases in Litestream. - The result is needed for correctness - Failure could corrupt data or state - You're in a loop processing items - **No → DEBUG log only.** This is rare. Only when ALL of these are true: - A valid fallback path exists that doesn't depend on the result - Failure cannot affect correctness - The operation is purely supplementary (e.g., reading an optimization hint) When in doubt, return the error. In a disaster recovery tool, silent failures are worse than noisy ones. ### DO: Return errors immediately ```go // CORRECT - Return error for caller to handle func (db *DB) validatePosition() error { dpos, err := db.Pos() if err != nil { return err } rpos := replica.Pos() if dpos.TXID < rpos.TXID { return fmt.Errorf("database position (%v) behind replica (%v)", dpos, rpos) } return nil } ``` ### DON'T: Continue on critical errors ```go // WRONG - Silently continuing can cause data corruption func (db *DB) validatePosition() { if dpos, _ := db.Pos(); dpos.TXID < replica.Pos().TXID { log.Printf("warning: position mismatch") // Don't just log! // Continuing here is dangerous } } ``` ### DON'T: Ignore errors and continue in loops ```go // WRONG - Continuing after error can corrupt state func (db *DB) processFiles() { for _, file := range files { if err := processFile(file); err != nil { log.Printf("error: %v", err) // Just logging! // Continuing to next file is dangerous } } } ``` ### DO: Return errors properly in loops ```go // CORRECT - Let caller decide how to handle errors func (db *DB) processFiles() error { for _, file := range files { if err := processFile(file); err != nil { return fmt.Errorf("process file %s: %w", file, err) } } return nil } ``` ## Locking Patterns ### DO: Use proper lock types ```go // CORRECT - Use Lock() for writes r.mu.Lock() defer r.mu.Unlock() r.pos = pos ``` ### DON'T: Use RLock for write operations ```go // WRONG - Race condition r.mu.RLock() // Should be Lock() for writes defer r.mu.RUnlock() r.pos = pos // Writing with RLock! ``` ## Compaction and Eventual Consistency Many storage backends (S3, R2, etc.) are eventually consistent: - A file you just wrote might not be immediately readable - A file might be listed but only partially available - Reads might return stale or incomplete data ### DO: Read from local when available ```go // CORRECT - Check local first during compaction // db.go:1280-1294 - ALWAYS read from local disk when available f, err := os.Open(db.LTXPath(info.Level, info.MinTXID, info.MaxTXID)) if err == nil { // Use local file - it's complete and consistent return f, nil } // Only fall back to remote if local doesn't exist return replica.Client.OpenLTXFile(...) ``` ### DON'T: Read from remote during compaction ```go // WRONG - Can get partial/corrupt data from eventually consistent storage f, err := client.OpenLTXFile(ctx, level, minTXID, maxTXID, 0, 0) ``` ## Resumable Reader Pattern During restore, LTX file streams from S3/Tigris may sit idle while the compactor processes lower-numbered pages from the snapshot. Storage providers close these idle connections, causing "unexpected EOF" errors. The `ResumableReader` (`internal/resumable_reader.go`) wraps `io.ReadCloser` with automatic reconnection: ### Interface ```go type LTXFileOpener interface { OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) } ``` ### Two Failure Modes 1. **Non-EOF errors** (connection reset, timeout) — stream broke mid-transfer 2. **Premature EOF** — server closed cleanly, but `offset < size` (known file size) ### Key Behaviors - Max 3 retries (`resumableReaderMaxRetries = 3`) - Tracks current byte `offset` for range-request resume - Returns partial reads without error so callers like `io.ReadFull` naturally retry - Reopens from current offset using `OpenLTXFile(ctx, level, min, max, offset, 0)` ### DO: Use ResumableReader for restore streams ```go rc, _ := client.OpenLTXFile(ctx, level, min, max, 0, fileInfo.Size) rr := internal.NewResumableReader(ctx, client, level, min, max, fileInfo.Size, rc, logger) defer rr.Close() ``` ### DON'T: Use raw OpenLTXFile during long restore operations ```go rc, _ := client.OpenLTXFile(ctx, level, min, max, 0, 0) // Risk: connection may drop during idle periods in multi-file restore io.ReadFull(rc, buf) // unexpected EOF! ``` ## Retention Bypass Pattern When using cloud provider lifecycle policies (S3 lifecycle rules, R2 auto-cleanup), Litestream's active file deletion can be disabled: ```yaml retention: enabled: false ``` ### Propagation Chain 1. `RetentionConfig{Enabled *bool}` in YAML config (`cmd/litestream/main.go`) 2. `Store.SetRetentionEnabled(bool)` propagates to all DBs and their compactors (`store.go`) 3. `Compactor.RetentionEnabled` guards 3 deletion points in `compactor.go` ### DO: Disable retention when cloud lifecycle handles cleanup ```go store.SetRetentionEnabled(false) // Delegates deletion to cloud lifecycle policies ``` ### DON'T: Disable retention without cloud lifecycle policies Disabling retention without cloud lifecycle policies causes unbounded storage growth. Litestream logs a warning: "retention disabled; cloud provider lifecycle policies must handle retention". ## Conditional Write Pattern (Distributed Locking) The S3 leaser (`s3/leaser.go`) uses S3 conditional writes (`If-Match`/`If-None-Match`) for distributed locking without an external coordination service. ### Acquire Pattern ```go input := &s3.PutObjectInput{ Bucket: aws.String(l.Bucket), Key: aws.String(key), Body: bytes.NewReader(data), } if etag == "" { input.IfNoneMatch = aws.String("*") // First acquire: only if key doesn't exist } else { input.IfMatch = aws.String(etag) // Expired takeover: only if ETag matches } ``` ### Release Pattern ```go _, err := l.s3.DeleteObject(ctx, &s3.DeleteObjectInput{ Bucket: aws.String(l.Bucket), Key: aws.String(key), IfMatch: aws.String(lease.ETag), // Only delete if we still hold the lease }) ``` ### Error Handling - **HTTP 412 (PreconditionFailed)**: Another instance acquired/renewed the lease - **HTTP 404 (NoSuchKey)**: Lease already released ## Timestamp Preservation During compaction, preserve the earliest CreatedAt timestamp from source files to maintain temporal granularity for point-in-time restoration. ### DO: Preserve earliest timestamp ```go // CORRECT - Preserve temporal information info, err := replica.Client.WriteLTXFile(ctx, level, minTXID, maxTXID, r) if err != nil { return fmt.Errorf("write ltx: %w", err) } info.CreatedAt = oldestSourceFile.CreatedAt ``` ### DON'T: Ignore CreatedAt preservation ```go // WRONG - Loses timestamp granularity for point-in-time restores info := <x.FileInfo{ CreatedAt: time.Now(), // Don't use current time during compaction } ``` ## Common Pitfalls ### 1. Mixing architectural concerns ```go // WRONG - Database state logic in Replica layer func (r *Replica) Start() error { if db.needsRestore() { // Wrong layer for DB state! r.restoreDatabase() // Replica shouldn't manage DB state! } return r.sync() } ``` ### 2. Recreating existing functionality ```go // WRONG - Don't reimplement what already exists func customSnapshotTrigger() { // Complex custom logic to trigger snapshots // when db.verify() already does this! } ``` ### DO: Leverage existing mechanisms ```go // CORRECT - Use what's already there func triggerSnapshot() error { return db.verify() // Already handles snapshot logic correctly } ``` ### 3. Skipping the lock page The lock page at 1GB (0x40000000) must always be skipped: ```go // db.go:951-953 - Must skip lock page during replication lockPgno := ltx.LockPgno(pageSize) if pgno == lockPgno { continue // Skip this page - it's reserved by SQLite } ``` Lock page numbers by page size: | Page Size | Lock Page Number | |-----------|------------------| | 4KB | 262145 | | 8KB | 131073 | | 16KB | 65537 | | 32KB | 32769 | ## Component Reference ### DB Component (db.go) **Responsibilities:** - Manages SQLite database connection (via `modernc.org/sqlite` - no CGO) - Monitors WAL for changes - Performs checkpoints - Maintains long-running read transaction - Converts WAL pages to LTX format **Key Fields:** ```go type DB struct { path string // Database file path db *sql.DB // SQLite connection rtx *sql.Tx // Long-running read transaction pageSize int // Database page size (critical for lock page) notify chan struct{} // Notifies on WAL changes } ``` **Initialization Sequence:** 1. Open database connection 2. Read page size from database 3. Initialize long-running read transaction 4. Start monitor goroutine 5. Initialize replicas ### Replica Component (replica.go) **Responsibilities:** - Manages replication to a single destination (one replica per DB) - Tracks replication position (ltx.Pos) - Handles sync intervals - Manages encryption (if configured) **Key Operations:** - `Sync()`: Synchronizes pending changes - `SetPos()`: Updates replication position (must use Lock, not RLock!) - `Snapshot()`: Creates full database snapshot ### ReplicaClient Interface (replica_client.go) **Required Methods:** ```go type ReplicaClient interface { Type() string // Client type identifier // File operations LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) DeleteLTXFiles(ctx context.Context, files []*ltx.FileInfo) error DeleteAll(ctx context.Context) error } ``` **useMetadata Parameter:** - `useMetadata=true`: Fetch accurate timestamps from backend metadata (required for point-in-time restores) - `useMetadata=false`: Use fast timestamps for normal operations ### Compactor Component (compactor.go) **Responsibilities:** - Compaction and retention for LTX files - Operates solely through `ReplicaClient` interface - Suitable for both DB (with local file caching) and VFS (remote-only) **Key Fields:** ```go type Compactor struct { client ReplicaClient VerifyCompaction bool // Post-compaction TXID consistency check RetentionEnabled bool // Default: true. Controls active file deletion // Local file optimization (set by DB layer) LocalFileOpener func(level int, minTXID, maxTXID ltx.TXID) (io.ReadCloser, error) LocalFileDeleter func(level int, minTXID, maxTXID ltx.TXID) error // Level max-file-info caching CacheGetter func(level int) (*ltx.FileInfo, bool) CacheSetter func(level int, info *ltx.FileInfo) } ``` ### Store Component (store.go) **Default Compaction Levels:** ```go var defaultLevels = CompactionLevels{ {Level: 0, Interval: 0}, // Raw LTX files (no compaction) {Level: 1, Interval: 30*Second}, {Level: 2, Interval: 5*Minute}, {Level: 3, Interval: 1*Hour}, // Snapshots created daily (24h retention) } ``` ## Testing Patterns ### Race Condition Testing ```bash # Always run with race detector go test -race -v ./... # Specific race-prone areas go test -race -v -run TestReplica_Sync ./... go test -race -v -run TestDB_Sync ./... go test -race -v -run TestStore_CompactDB ./... ``` ### Lock Page Testing ```bash # Test with various page sizes ./bin/litestream-test populate -db test.db -page-size 4096 -target-size 2GB ./bin/litestream-test populate -db test.db -page-size 8192 -target-size 2GB # Validate lock page handling ./bin/litestream-test validate -source-db test.db -replica-url file:///tmp/replica ``` ### Integration Testing ```bash # Test specific backend go test -v ./replica_client_test.go -integration s3 go test -v ./replica_client_test.go -integration gcs go test -v ./replica_client_test.go -integration abs go test -v ./replica_client_test.go -integration oss go test -v ./replica_client_test.go -integration sftp ``` ================================================ FILE: docs/PENDING_USER_DOCS.md ================================================ # Pending User-Facing Documentation This file tracks open issues on [benbjohnson/litestream.io](https://github.com/benbjohnson/litestream.io) that need user-facing documentation for features recently added to Litestream. ## Open Issues | Issue | Title | Related Litestream PR | Category | |-------|-------|-----------------------|----------| | [#240](https://github.com/benbjohnson/litestream.io/issues/240) | `skip-remote-deletion` config option | #1094 | Config | | [#241](https://github.com/benbjohnson/litestream.io/issues/241) | SyncStatus, SyncAndWait, EnsureExists API | #1092 | Library API | | [#242](https://github.com/benbjohnson/litestream.io/issues/242) | Embedded CA bundle eliminates certificate setup | #1099 | Docker | | [#243](https://github.com/benbjohnson/litestream.io/issues/243) | S3 URL query parameters and R2 defaults | #1100, #1101 | S3/Cloud | | [#244](https://github.com/benbjohnson/litestream.io/issues/244) | Distributed leasing documentation | #1073 | S3/Cloud | | [#245](https://github.com/benbjohnson/litestream.io/issues/245) | `-level` flag for ltx command | #1072 | Reference | | [#246](https://github.com/benbjohnson/litestream.io/issues/246) | IPC Unix socket endpoints | #1021 | Reference | | [#247](https://github.com/benbjohnson/litestream.io/issues/247) | `$PID` environment variable in config | #1070 | Config | | [#248](https://github.com/benbjohnson/litestream.io/issues/248) | `validation-interval` config option | — | Config | | [#249](https://github.com/benbjohnson/litestream.io/issues/249) | Automatic v0.3.x backup restore | #1074, #1075 | Restore | | [#250](https://github.com/benbjohnson/litestream.io/issues/250) | MCP tools for v0.5.x changes | — | MCP | ## AI Documentation Status Internal AI documentation (this repo) is tracked in [#1106](https://github.com/benbjohnson/litestream/issues/1106). ================================================ FILE: docs/PROVIDER_COMPATIBILITY.md ================================================ # Storage Provider Compatibility Guide This document details S3-compatible storage provider compatibility with Litestream, including known limitations, required configuration, and tested configurations. ## Overview Litestream uses the AWS SDK v2 for S3-compatible storage backends. While most providers implement the S3 API, there are important differences in behavior that can affect Litestream's operation. ## Provider-Specific Configuration ### AWS S3 (Default) **Status**: Fully supported (primary target) ```yaml replicas: - url: s3://bucket-name/path region: us-east-1 ``` **Notes**: - No special configuration required - All features fully supported - Checksum validation enabled by default ### Cloudflare R2 **Status**: Supported with default configuration **Known Limitations**: - Strict concurrent upload limit (2-3 concurrent uploads max) - Does not support `aws-chunked` content encoding - Does not support request/response checksums **Configuration**: ```yaml replicas: - url: s3://bucket-name/path?endpoint=https://ACCOUNT_ID.r2.cloudflarestorage.com access-key-id: your-access-key-id secret-access-key: your-secret-access-key ``` **Automatic Defaults** (applied when R2 endpoint detected): - `sign-payload=true` - Signed payloads required - `concurrency=2` - Limits concurrent multipart upload parts - Checksums disabled automatically **Important**: The endpoint must use `https://` scheme for R2 detection to work. Related issues: #948, #947, #940, #941 ### Backblaze B2 (S3-Compatible API) **Status**: Supported with configuration **Known Limitations**: - Requires signed payloads for all requests - Specific authentication endpoint required **Configuration**: ```yaml replicas: - url: s3://bucket-name/path?endpoint=https://s3.REGION.backblazeb2.com&sign-payload=true&force-path-style=true access-key-id: your-key-id secret-access-key: your-application-key ``` **Required Settings**: - `sign-payload=true` - Required for B2 authentication - `force-path-style=true` - Required for bucket access - Endpoint format: `https://s3.REGION.backblazeb2.com` Related issues: #918, #894 ### DigitalOcean Spaces **Status**: Supported with configuration **Known Limitations**: - Does not support `aws-chunked` content encoding - Signature requirements differ from AWS **Configuration**: ```yaml replicas: - url: s3://bucket-name/path?endpoint=https://REGION.digitaloceanspaces.com&force-path-style=false access-key-id: your-spaces-key secret-access-key: your-spaces-secret ``` **Notes**: - Use virtual-hosted style paths (force-path-style=false) - Checksum features disabled automatically for custom endpoints Related issues: #943 ### MinIO **Status**: Fully supported **Configuration**: ```yaml replicas: - url: s3://bucket-name/path?endpoint=https://your-minio-server:9000&force-path-style=true access-key-id: your-access-key secret-access-key: your-secret-key ``` **Notes**: - Works well with default settings - Force path style recommended for single-server deployments ### Scaleway Object Storage **Status**: Supported with configuration **Known Limitations**: - `MissingContentLength` errors with streaming uploads - Requires Content-Length header **Configuration**: ```yaml replicas: - url: s3://bucket-name/path?endpoint=https://s3.REGION.scw.cloud&force-path-style=true access-key-id: your-access-key secret-access-key: your-secret-key ``` Related issues: #912 ### Hetzner Object Storage **Status**: Supported with configuration **Known Limitations**: - `InvalidArgument` errors with default AWS SDK settings - Does not support `aws-chunked` content encoding - Requires signed payloads for all requests **Configuration**: ```yaml replicas: - url: s3://bucket-name/path?endpoint=https://REGION.your-objectstorage.com&force-path-style=true access-key-id: your-access-key secret-access-key: your-secret-key ``` ### Filebase **Status**: Supported with configuration **Known Limitations**: - Authentication failures with default SDK settings after SDK v2 migration **Configuration**: ```yaml replicas: - url: s3://bucket-name/path?endpoint=https://s3.filebase.com&force-path-style=true access-key-id: your-access-key secret-access-key: your-secret-key ``` ### Tigris **Status**: Supported with configuration **Configuration**: ```yaml replicas: - url: s3://bucket-name/path?endpoint=https://fly.storage.tigris.dev&force-path-style=true access-key-id: your-access-key secret-access-key: your-secret-key ``` ### Supabase Storage (S3-Compatible API) **Status**: Supported (auto-detected) **Known Limitations**: - Requires path-style URLs (`force-path-style=true`) - No S3 versioning support **Configuration**: ```yaml replicas: - url: s3://bucket-name/path?endpoint=https://PROJECT_REF.supabase.co/storage/v1/s3 access-key-id: your-s3-access-key secret-access-key: your-s3-secret-key ``` **Automatic Defaults** (applied when Supabase endpoint detected): - `sign-payload=true` - Signed payloads required - `force-path-style=true` - Path-style URLs required **Notes**: - S3 access keys are generated in the Supabase dashboard under Storage > S3 Access Keys - Endpoint format: `https://.supabase.co/storage/v1/s3` Related issues: #1133 ### Wasabi **Status**: Supported **Configuration**: ```yaml replicas: - url: s3://bucket-name/path?endpoint=https://s3.REGION.wasabisys.com access-key-id: your-access-key secret-access-key: your-secret-key ``` ## Google Cloud Storage (GCS) **Status**: Fully supported (native client) ```yaml replicas: - url: gcs://bucket-name/path ``` **Authentication**: - Uses Application Default Credentials - Set `GOOGLE_APPLICATION_CREDENTIALS` environment variable - Or use workload identity on GCP ## Azure Blob Storage (ABS) **Status**: Fully supported (native client) ```yaml replicas: - url: abs://container-name/path account-name: your-account-name account-key: your-account-key ``` **Using SAS Token** (for granular container-level access): ```yaml replicas: - url: abs://container-name/path account-name: your-account-name sas-token: "sv=2023-01-03&ss=b&srt=co&sp=rwdlacx..." ``` Or via environment variable: `LITESTREAM_AZURE_SAS_TOKEN` **Alternative Authentication**: - SAS token: `sas-token` config or `LITESTREAM_AZURE_SAS_TOKEN` env var - Account key: `account-key` config or `LITESTREAM_AZURE_ACCOUNT_KEY` env var - Managed identity on Azure (via DefaultAzureCredential) **Authentication Priority**: SAS token > Account key > Default credential chain ## Alibaba Cloud OSS **Status**: Supported (native client) ```yaml replicas: - url: oss://bucket-name/path?endpoint=oss-REGION.aliyuncs.com access-key-id: your-access-key-id access-key-secret: your-access-key-secret ``` ## SFTP **Status**: Supported ```yaml replicas: - url: sftp://hostname/path user: username password: password # or use key-path ``` ## Configuration Reference ### S3 Query Parameters Parameters with an alias accept both camelCase and hyphenated forms (e.g., `forcePathStyle` or `force-path-style`). | Parameter | Alias | Description | Default | |-----------|-------|-------------|---------| | `endpoint` | | Custom S3 endpoint URL | AWS S3 | | `region` | | AWS region | Auto-detected | | `forcePathStyle` | `force-path-style` | Use path-style URLs | `false` (auto for custom endpoints) | | `skipVerify` | `skip-verify` | Skip TLS verification | `false` | | `signPayload` | `sign-payload` | Sign request payloads | `true` | | `requireContentMD5` | `require-content-md5` | Require Content-MD5 header | `true` | | `concurrency` | | Multipart upload concurrency | `5` | | `partSize` | `part-size` | Multipart upload part size | `5MB` | | `sseCustomerAlgorithm` | `sse-customer-algorithm` | SSE-C encryption algorithm | None | | `sseCustomerKey` | `sse-customer-key` | SSE-C encryption key | None | | `sseCustomerKeyMD5` | `sse-customer-key-md5` | SSE-C key MD5 checksum | None | | `sseKmsKeyId` | `sse-kms-key-id` | KMS key for encryption | None | ### Provider Detection Litestream automatically detects certain providers and applies appropriate defaults: | Provider | Detection Pattern | Applied Settings | |----------|-------------------|------------------| | Hetzner | `*.your-objectstorage.com` | `sign-payload=true` | | Cloudflare R2 | `*.r2.cloudflarestorage.com` | `sign-payload=true`, `concurrency=2` | | Backblaze B2 | `*.backblazeb2.com` | `sign-payload=true`, `force-path-style=true` | | DigitalOcean | `*.digitaloceanspaces.com` | `sign-payload=true` | | Scaleway | `*.scw.cloud` | `sign-payload=true` | | Filebase | `s3.filebase.com` | `sign-payload=true`, `force-path-style=true` | | Tigris | `*.tigris.dev` | `sign-payload=true`, `require-content-md5=false` | | MinIO | host with port (not cloud provider) | `sign-payload=true`, `force-path-style=true` | | Supabase | `*.supabase.co` | `sign-payload=true`, `force-path-style=true` | ## Troubleshooting ### Common Errors **`InvalidArgument: Unsupported content encoding: aws-chunked`** - Provider doesn't support AWS SDK v2 chunked encoding - Use a custom endpoint with automatic checksum disabling - Or explicitly disable checksums **`SignatureDoesNotMatch`** - Try `sign-payload=true` in the URL - Verify credentials are correct - Check endpoint URL format **`MissingContentLength`** - Provider requires Content-Length header - This is handled automatically for known providers **`Too many concurrent uploads` or timeout errors** - Reduce concurrency: `?concurrency=2` - Particularly important for Cloudflare R2 **`AccessDenied` or authentication failures** - Verify credentials - Check IAM/bucket permissions - For B2, ensure `sign-payload=true` ### Debug Mode Enable verbose logging to diagnose issues: ```bash LITESTREAM_DEBUG=1 litestream replicate ... ``` Or in configuration: ```yaml logging: level: debug ``` ## Testing Your Configuration Test connectivity without starting replication: ```bash # List any existing backups litestream snapshots s3://bucket/path?endpoint=... # Perform a test restore (requires existing backup) litestream restore -o /tmp/test.db s3://bucket/path?endpoint=... ``` ## Version Compatibility - **Litestream v0.5.x**: AWS SDK v2, improved provider compatibility - **Litestream v0.4.x**: AWS SDK v1, different authentication handling - **Litestream v0.3.x**: Legacy format — v0.5.x can restore from v0.3.x backups via `ReplicaClientV3` interface When upgrading from v0.3.x, v0.5.x can automatically restore from v0.3.x backups if no v0.4.x+ backup exists. The S3 backend implements `ReplicaClientV3` to read the v0.3.x `generations/{id}/snapshots/` and `generations/{id}/wal/` directory structure. See [REPLICA_CLIENT_GUIDE.md](REPLICA_CLIENT_GUIDE.md#replicaclientv3-interface-v03x-restore) for details. ### Validation Interval Litestream supports periodic validation of replica integrity: ```yaml validation: interval: "5m" # How often to validate; 0 disables ``` This runs `Store.Validate()` on a ticker, comparing local and remote positions. Can also be set per-replica via the `validation-interval` replica config key. and cannot restore backups created by v0.3.x. See the upgrade guide for migration instructions. ## Reporting Issues When reporting provider compatibility issues, please include: 1. Provider name and region 2. Litestream version (`litestream version`) 3. Full error message 4. Configuration (with credentials redacted) 5. Whether the issue is with replication, restore, or both File issues at: [GitHub Issues](https://github.com/benbjohnson/litestream/issues) ================================================ FILE: docs/REPLICA_CLIENT_GUIDE.md ================================================ # ReplicaClient Implementation Guide This guide provides comprehensive instructions for implementing new storage backends for Litestream replication. ## Table of Contents - [Interface Contract](#interface-contract) - [Implementation Checklist](#implementation-checklist) - [Eventual Consistency Handling](#eventual-consistency-handling) - [Error Handling](#error-handling) - [Testing Requirements](#testing-requirements) - [ReplicaClientV3 Interface (v0.3.x Restore)](#replicaclientv3-interface-v03x-restore) - [Common Implementation Mistakes](#common-implementation-mistakes) - [Reference Implementations](#reference-implementations) ## Interface Contract All replica clients MUST implement the `ReplicaClient` interface defined in `replica_client.go`: ```go type ReplicaClient interface { // Returns the type identifier (e.g., "s3", "gcs", "file") Type() string // Returns iterator of LTX files at given level // seek: Start from this TXID (0 = beginning) // useMetadata: When true, fetch accurate timestamps from backend metadata (required for PIT restore) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) // Opens an LTX file for reading // Returns os.ErrNotExist if file doesn't exist OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) // Writes an LTX file to storage // SHOULD set CreatedAt based on backend metadata or upload time WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) // Deletes one or more LTX files DeleteLTXFiles(ctx context.Context, files []*ltx.FileInfo) error // Deletes all files for this database DeleteAll(ctx context.Context) error } ``` ## Implementation Checklist ### Required Features - [ ] Implement all interface methods - [ ] Support partial reads (offset/size in OpenLTXFile) - [ ] Return proper error types (especially os.ErrNotExist) - [ ] Handle context cancellation - [ ] Preserve file timestamps (CreatedAt) - [ ] Support concurrent operations - [ ] Implement proper cleanup in DeleteAll ### Optional Features - [ ] Connection pooling - [ ] Retry logic with exponential backoff - [ ] Request batching - [ ] Compression - [ ] Encryption at rest - [ ] Bandwidth throttling ## Eventual Consistency Handling Many cloud storage services exhibit eventual consistency, where: - A file you just wrote might not be immediately visible - A file might be listed but only partially readable - Deletes might not take effect immediately ### Best Practices #### 1. Write-After-Write Consistency ```go func (c *ReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { // Buffer the entire content first data, err := io.ReadAll(r) if err != nil { return nil, fmt.Errorf("buffer ltx data: %w", err) } // Calculate checksum before upload checksum := crc64.Checksum(data, crc64.MakeTable(crc64.ECMA)) // Upload with checksum verification err = c.uploadWithVerification(ctx, path, data, checksum) if err != nil { return nil, err } // Verify the file is readable before returning return c.verifyUpload(ctx, path, int64(len(data)), checksum) } func (c *ReplicaClient) verifyUpload(ctx context.Context, path string, expectedSize int64, expectedChecksum uint64) (*ltx.FileInfo, error) { // Implement retry loop with backoff backoff := 100 * time.Millisecond for i := 0; i < 10; i++ { info, err := c.statFile(ctx, path) if err == nil { if info.Size == expectedSize { rc, err := c.openFile(ctx, path, 0, 0) if err != nil { return nil, fmt.Errorf("open uploaded file: %w", err) } data, err := io.ReadAll(rc) rc.Close() if err != nil { return nil, fmt.Errorf("read uploaded file: %w", err) } if crc64.Checksum(data, crc64.MakeTable(crc64.ECMA)) == expectedChecksum { return info, nil } } } select { case <-ctx.Done(): return nil, ctx.Err() case <-time.After(backoff): backoff *= 2 } } return nil, errors.New("upload verification failed") } ``` #### 2. List-After-Write Consistency ```go func (c *ReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { // List files from storage files, err := c.listFiles(ctx, level, useMetadata) if err != nil { return nil, err } // Sort by TXID for consistent ordering sort.Slice(files, func(i, j int) bool { if files[i].MinTXID != files[j].MinTXID { return files[i].MinTXID < files[j].MinTXID } return files[i].MaxTXID < files[j].MaxTXID }) // Filter by seek position var filtered []*ltx.FileInfo for _, f := range files { if f.MinTXID >= seek { filtered = append(filtered, f) } } return ltx.NewFileInfoSliceIterator(filtered), nil } ``` #### 3. Read-After-Write Consistency ```go func (c *ReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { path := c.ltxPath(level, minTXID, maxTXID) // For eventually consistent backends, implement retry var lastErr error backoff := 100 * time.Millisecond for i := 0; i < 5; i++ { reader, err := c.openFile(ctx, path, offset, size) if err == nil { return reader, nil } // Don't retry on definitive errors if errors.Is(err, os.ErrNotExist) { return nil, err } lastErr = err select { case <-ctx.Done(): return nil, ctx.Err() case <-time.After(backoff): backoff *= 2 } } return nil, fmt.Errorf("open file after retries: %w", lastErr) } ``` ## Error Handling ### Standard Error Types Always return appropriate standard errors: ```go // File not found return nil, os.ErrNotExist // Permission denied return nil, os.ErrPermission // Context cancelled return nil, ctx.Err() // Custom errors should wrap standard ones return nil, fmt.Errorf("s3 download failed: %w", err) ``` ### Error Classification ```go // Retryable errors func isRetryable(err error) bool { // Network errors var netErr net.Error if errors.As(err, &netErr) && netErr.Temporary() { return true } // Specific HTTP status codes if httpErr, ok := err.(HTTPError); ok { switch httpErr.StatusCode { case 429, 500, 502, 503, 504: return true } } // Timeout errors if errors.Is(err, context.DeadlineExceeded) { return true } return false } ``` ### Logging Best Practices ```go func (c *ReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { logger := slog.Default().With( "replica", c.Type(), "level", level, "minTXID", minTXID, "maxTXID", maxTXID, ) logger.Debug("starting ltx upload") info, err := c.upload(ctx, level, minTXID, maxTXID, r) if err != nil { logger.Error("ltx upload failed", "error", err) return nil, err } logger.Info("ltx upload complete", "size", info.Size) return info, nil } ``` ## Testing Requirements ### Unit Tests Every replica client MUST have comprehensive unit tests: ```go // replica_client_test.go func TestReplicaClient_WriteLTXFile(t *testing.T) { client := NewReplicaClient(testConfig) ctx := context.Background() // Test data data := []byte("test ltx content") reader := bytes.NewReader(data) // Write file info, err := client.WriteLTXFile(ctx, 0, 1, 100, reader) assert.NoError(t, err) assert.Equal(t, int64(len(data)), info.Size) // Verify file exists rc, err := client.OpenLTXFile(ctx, 0, 1, 100, 0, 0) assert.NoError(t, err) defer rc.Close() // Read and verify content content, err := io.ReadAll(rc) assert.NoError(t, err) assert.Equal(t, data, content) } func TestReplicaClient_PartialRead(t *testing.T) { client := NewReplicaClient(testConfig) ctx := context.Background() // Write test file data := bytes.Repeat([]byte("x"), 1000) _, err := client.WriteLTXFile(ctx, 0, 1, 100, bytes.NewReader(data)) require.NoError(t, err) // Test partial read rc, err := client.OpenLTXFile(ctx, 0, 1, 100, 100, 50) require.NoError(t, err) defer rc.Close() partial, err := io.ReadAll(rc) assert.NoError(t, err) assert.Equal(t, 50, len(partial)) assert.Equal(t, data[100:150], partial) } func TestReplicaClient_NotFound(t *testing.T) { client := NewReplicaClient(testConfig) ctx := context.Background() // Try to open non-existent file _, err := client.OpenLTXFile(ctx, 0, 999, 999, 0, 0) assert.True(t, errors.Is(err, os.ErrNotExist)) } ``` ### Integration Tests Integration tests run against real backends: ```go // +build integration func TestReplicaClient_Integration(t *testing.T) { // Skip if not in integration mode if testing.Short() { t.Skip("skipping integration test") } // Get credentials from environment config := ConfigFromEnv(t) client := NewReplicaClient(config) ctx := context.Background() t.Run("Concurrent Writes", func(t *testing.T) { var wg sync.WaitGroup errors := make(chan error, 10) for i := 0; i < 10; i++ { wg.Add(1) go func(n int) { defer wg.Done() data := []byte(fmt.Sprintf("concurrent %d", n)) minTXID := ltx.TXID(n * 100) maxTXID := ltx.TXID((n + 1) * 100) _, err := client.WriteLTXFile(ctx, 0, minTXID, maxTXID, bytes.NewReader(data)) if err != nil { errors <- err } }(i) } wg.Wait() close(errors) for err := range errors { t.Error(err) } }) t.Run("Large File", func(t *testing.T) { // Test with 100MB file data := bytes.Repeat([]byte("x"), 100*1024*1024) info, err := client.WriteLTXFile(ctx, 0, 1000, 2000, bytes.NewReader(data)) require.NoError(t, err) assert.Equal(t, int64(len(data)), info.Size) }) t.Run("Cleanup", func(t *testing.T) { err := client.DeleteAll(ctx) assert.NoError(t, err) // Verify cleanup iter, err := client.LTXFiles(ctx, 0, 0, false) require.NoError(t, err) defer iter.Close() assert.False(t, iter.Next(), "files should be deleted") }) } ``` ### Mock Client for Testing Provide a mock implementation for testing: ```go // mock/replica_client.go type ReplicaClient struct { mu sync.Mutex files map[string]*ltx.FileInfo data map[string][]byte errors map[string]error // Inject errors for testing } func (c *ReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { c.mu.Lock() defer c.mu.Unlock() // Check for injected error key := fmt.Sprintf("write-%d-%d-%d", level, minTXID, maxTXID) if err, ok := c.errors[key]; ok { return nil, err } // Store data data, err := io.ReadAll(r) if err != nil { return nil, err } path := ltxPath(level, minTXID, maxTXID) c.data[path] = data info := <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Size: int64(len(data)), CreatedAt: time.Now(), } c.files[path] = info return info, nil } ``` ## ReplicaClientV3 Interface (v0.3.x Restore) Backends that need backward-compatible restore from v0.3.x Litestream backups should implement the optional `ReplicaClientV3` interface (`v3.go`). This enables restoring databases from pre-v0.4 backup formats. ### Interface ```go type ReplicaClientV3 interface { GenerationsV3(ctx context.Context) ([]string, error) SnapshotsV3(ctx context.Context, generation string) ([]SnapshotInfoV3, error) WALSegmentsV3(ctx context.Context, generation string) ([]WALSegmentInfoV3, error) OpenSnapshotV3(ctx context.Context, generation string, index int) (io.ReadCloser, error) OpenWALSegmentV3(ctx context.Context, generation string, index int, offset int64) (io.ReadCloser, error) } ``` ### Supporting Types ```go type PosV3 struct { Generation string // 16-char hex string Index int // WAL index Offset int64 // Offset within WAL segment } type SnapshotInfoV3 struct { Generation string Index int Size int64 CreatedAt time.Time } type WALSegmentInfoV3 struct { Generation string Index int Offset int64 Size int64 CreatedAt time.Time } ``` ### v0.3.x Path Structure ```text generations/ {generation-id}/ # 16-char hex (validated by IsGenerationIDV3) snapshots/ {index:08x}.snapshot.lz4 wal/ {index:08x}_{offset:08x}.wal.lz4 ``` ### Implementation Notes - Generation IDs are 16 hex characters, validated by `IsGenerationIDV3()` (`generationRegexV3`) - All returned readers provide LZ4-decompressed data - `GenerationsV3` returns IDs sorted ascending - `SnapshotsV3` returns sorted by index - `WALSegmentsV3` returns sorted by index, then offset - S3 backend implements this: `var _ litestream.ReplicaClientV3 = (*ReplicaClient)(nil)` ### ResumableReader Integration `OpenLTXFile` must support the `offset` parameter for range requests. The `ResumableReader` (`internal/resumable_reader.go`) reopens streams from the last successful byte offset on connection failures during restore. If your backend ignores `offset`, restore operations will fail on idle connection timeouts. ## Common Implementation Mistakes ### ❌ Mistake 1: Not Handling Partial Reads ```go // WRONG - Always reads entire file func (c *Client) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { return c.storage.Download(path) // Ignores offset/size! } ``` ```go // CORRECT - Respects offset and size func (c *Client) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { if offset == 0 && size == 0 { // Full file return c.storage.Download(path) } // Partial read using Range header or equivalent end := offset + size - 1 if size == 0 { end = 0 // Read to end } return c.storage.DownloadRange(path, offset, end) } ``` ### ❌ Mistake 2: Not Preserving CreatedAt ```go // WRONG - Uses current time func (c *Client) WriteLTXFile(...) (*ltx.FileInfo, error) { // Upload file... return <x.FileInfo{ CreatedAt: time.Now(), // Wrong! Loses temporal info }, nil } ``` ```go // CORRECT - Preserves original timestamp func (c *Client) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { // Upload file... uploadedSize, modTime, err := c.storage.Upload(path, r) if err != nil { return nil, err } return <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Size: uploadedSize, CreatedAt: modTime, }, nil } ``` ### ❌ Mistake 3: Wrong Error Types ```go // WRONG - Generic error func (c *Client) OpenLTXFile(...) (io.ReadCloser, error) { resp, err := c.get(path) if err != nil { return nil, fmt.Errorf("not found") // Wrong type! } } ``` ```go // CORRECT - Proper error type func (c *Client) OpenLTXFile(...) (io.ReadCloser, error) { resp, err := c.get(path) if err != nil { if resp.StatusCode == 404 { return nil, os.ErrNotExist // Correct type } return nil, fmt.Errorf("download failed: %w", err) } } ``` ### ❌ Mistake 4: Not Handling Context ```go // WRONG - Ignores context func (c *Client) WriteLTXFile(ctx context.Context, ...) (*ltx.FileInfo, error) { // Long operation without checking context for i := 0; i < 1000000; i++ { doWork() // Could run forever! } } ``` ```go // CORRECT - Respects context func (c *Client) WriteLTXFile(ctx context.Context, ...) (*ltx.FileInfo, error) { // Check context periodically for i := 0; i < 1000000; i++ { select { case <-ctx.Done(): return nil, ctx.Err() default: // Continue work } if err := doWork(ctx); err != nil { return nil, err } } } ``` ### ❌ Mistake 5: Blocking in Iterator ```go // WRONG - Loads all files at once func (c *Client) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { allFiles, err := c.loadAllFiles(level) // Could be millions! if err != nil { return nil, err } return NewIterator(allFiles), nil } ``` ```go // CORRECT - Lazy loading with pagination func (c *Client) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { return &lazyIterator{ client: c, level: level, seek: seek, useMetadata: useMetadata, pageSize: 1000, }, nil } type lazyIterator struct { client *Client level int seek ltx.TXID pageSize int current []*ltx.FileInfo index int done bool } func (i *lazyIterator) Next() bool { if i.index >= len(i.current) && !i.done { // Load next page i.loadNextPage() } return i.index < len(i.current) } ``` ## Reference Implementations ### File System Client (Simplest) See `file/replica_client.go` for the simplest implementation: - Direct file I/O operations - No network complexity - Good starting reference ### S3 Client (Most Complex) See `s3/replica_client.go` for advanced features: - Multipart uploads for large files - Retry logic with exponential backoff - Request signing - Eventual consistency handling ### Key Patterns from S3 Implementation ```go // Path construction func (c *ReplicaClient) ltxDir(level int) string { if level == SnapshotLevel { return path.Join(c.Path, "snapshots") } return path.Join(c.Path, "ltx", fmt.Sprintf("%04d", level)) } // Metadata handling func (c *ReplicaClient) WriteLTXFile(...) (*ltx.FileInfo, error) { // Add metadata to object metadata := map[string]string{ "min-txid": fmt.Sprintf("%d", minTXID), "max-txid": fmt.Sprintf("%d", maxTXID), "level": fmt.Sprintf("%d", level), } // Upload with metadata _, err := c.s3.PutObjectWithContext(ctx, &s3.PutObjectInput{ Bucket: &c.Bucket, Key: &key, Body: r, Metadata: metadata, }) } // Error mapping func mapS3Error(err error) error { if aerr, ok := err.(awserr.Error); ok { switch aerr.Code() { case s3.ErrCodeNoSuchKey: return os.ErrNotExist case s3.ErrCodeAccessDenied: return os.ErrPermission } } return err } ``` ## Performance Optimization ### Connection Pooling ```go type ReplicaClient struct { pool *ConnectionPool } func NewReplicaClient(config Config) *ReplicaClient { pool := &ConnectionPool{ MaxConnections: config.MaxConnections, IdleTimeout: config.IdleTimeout, } return &ReplicaClient{ pool: pool, } } ``` ### Request Batching ```go func (c *ReplicaClient) DeleteLTXFiles(ctx context.Context, files []*ltx.FileInfo) error { // Batch deletes for efficiency const batchSize = 100 for i := 0; i < len(files); i += batchSize { end := i + batchSize if end > len(files) { end = len(files) } batch := files[i:end] if err := c.deleteBatch(ctx, batch); err != nil { return fmt.Errorf("delete batch %d: %w", i/batchSize, err) } } return nil } ``` ### Caching ```go type ReplicaClient struct { cache *FileInfoCache } func (c *ReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { // Check cache first (only cache when useMetadata=false for fast queries) cacheKey := fmt.Sprintf("%d-%d", level, seek) if !useMetadata { if cached, ok := c.cache.Get(cacheKey); ok { return ltx.NewFileInfoSliceIterator(cached), nil } } // Load from storage files, err := c.loadFiles(ctx, level, seek, useMetadata) if err != nil { return nil, err } // Cache for future requests c.cache.Set(cacheKey, files, 5*time.Minute) return ltx.NewFileInfoSliceIterator(files), nil } ``` ## Checklist for New Implementations Before submitting a new replica client: - [ ] All interface methods implemented - [ ] Unit tests with >80% coverage - [ ] Integration tests (with build tag) - [ ] Mock client for testing - [ ] Handles partial reads correctly - [ ] Returns proper error types - [ ] Preserves timestamps - [ ] Handles context cancellation - [ ] Documents eventual consistency behavior - [ ] Includes retry logic for transient errors - [ ] Logs appropriately (debug/info/error) - [ ] README with configuration examples - [ ] Added to main configuration parser ## Getting Help 1. Study existing implementations (start with `file/`, then `s3/`) 2. Check test files for expected behavior 3. Run integration tests against your backend 4. Use the mock client for rapid development 5. Open a GitHub issue for design feedback ================================================ FILE: docs/SQLITE_INTERNALS.md ================================================ # SQLite Internals for Litestream This document explains SQLite internals critical for understanding Litestream's operation. ## Table of Contents - [SQLite File Structure](#sqlite-file-structure) - [Write-Ahead Log (WAL)](#write-ahead-log-wal) - [Page Structure](#page-structure) - [The 1GB Lock Page](#the-1gb-lock-page) - [Transaction Management](#transaction-management) - [Checkpoint Modes](#checkpoint-modes) - [Important SQLite Pragmas](#important-sqlite-pragmas) - [SQLite API Usage](#sqlite-api-usage) ## SQLite File Structure SQLite databases consist of: 1. **Main database file** - Contains actual data in pages 2. **WAL file** (-wal suffix) - Contains uncommitted changes 3. **SHM file** (-shm suffix) - Shared memory for coordination ``` database.db # Main database file (pages) database.db-wal # Write-ahead log database.db-shm # Shared memory file ``` ## Write-Ahead Log (WAL) ### WAL Basics WAL is SQLite's method for implementing atomic commits and rollback: - Changes are first written to WAL - Original database file unchanged until checkpoint - Readers see consistent view through WAL ```mermaid graph LR Write[Write Transaction] -->|Append| WAL[WAL File] WAL -->|Checkpoint| DB[Main Database] Read[Read Transaction] -->|Merge View| View[Consistent View] DB --> View WAL --> View ``` ### WAL File Structure ``` +------------------+ | WAL Header | 32 bytes +------------------+ | Frame 1 Header | 24 bytes | Frame 1 Data | Page size bytes +------------------+ | Frame 2 Header | 24 bytes | Frame 2 Data | Page size bytes +------------------+ | ... | +------------------+ ``` #### WAL Header (32 bytes) ```go type WALHeader struct { Magic [4]byte // 0x377f0682 or 0x377f0683 FileFormat uint32 // File format version (3007000) PageSize uint32 // Database page size Checkpoint uint32 // Checkpoint sequence number Salt1 uint32 // Random salt for checksum Salt2 uint32 // Random salt for checksum Checksum1 uint32 // Header checksum Checksum2 uint32 // Header checksum } ``` #### WAL Frame Header (24 bytes) ```go type WALFrameHeader struct { PageNumber uint32 // Page number in database DbSize uint32 // Size of database in pages Salt1 uint32 // Must match header salt Salt2 uint32 // Must match header salt Checksum1 uint32 // Cumulative checksum Checksum2 uint32 // Cumulative checksum } ``` ### Reading WAL in Litestream ```go // db.go - Reading WAL for replication func (db *DB) readWAL() ([]Page, error) { walPath := db.path + "-wal" f, err := os.Open(walPath) if err != nil { return nil, err } defer f.Close() // Read WAL header var header WALHeader binary.Read(f, binary.BigEndian, &header) // Validate magic number magic := binary.BigEndian.Uint32(header.Magic[:]) if magic != 0x377f0682 && magic != 0x377f0683 { return nil, errors.New("invalid WAL magic") } // Read frames var pages []Page for { var frameHeader WALFrameHeader err := binary.Read(f, binary.BigEndian, &frameHeader) if err == io.EOF { break } // Read page data pageData := make([]byte, header.PageSize) f.Read(pageData) pages = append(pages, Page{ Number: frameHeader.PageNumber, Data: pageData, }) } return pages, nil } ``` ## Page Structure ### Database Pages SQLite divides the database into fixed-size pages: ``` Page Size: Typically 4096 bytes (4KB) Page Number: 1-based indexing Page Types: - B-tree interior pages - B-tree leaf pages - Overflow pages - Freelist pages - Lock byte page (at 1GB) ``` ### Page Layout ``` +------------------+ | Page Header | Variable (8-12 bytes) +------------------+ | Cell Pointers | 2 bytes each +------------------+ | Unallocated | | Space | +------------------+ | Cell Content | Variable size | Area | (grows upward) +------------------+ ``` ### Page Header Structure ```go type PageHeader struct { PageType byte // 0x02, 0x05, 0x0a, 0x0d FreeBlockStart uint16 // Start of free block list CellCount uint16 // Number of cells CellStart uint16 // Offset to first cell FragmentBytes byte // Fragmented free bytes // Additional fields for interior pages RightChild uint32 // Only for interior pages } ``` ## The 1GB Lock Page ### Critical Concept SQLite reserves a special page at exactly 1,073,741,824 bytes (0x40000000) for locking: ```go const PENDING_BYTE = 0x40000000 // 1GB mark // Page number varies by page size func LockPageNumber(pageSize int) uint32 { return uint32(PENDING_BYTE/pageSize) + 1 } // Examples: // 4KB pages: 262145 (0x40001) // 8KB pages: 131073 (0x20001) // 16KB pages: 65537 (0x10001) // 32KB pages: 32769 (0x08001) // 64KB pages: 16385 (0x04001) ``` ### Why This Matters 1. **Cannot contain data** - SQLite will never write user data here 2. **Must be skipped** - During replication/compaction 3. **Affects large databases** - Only databases >1GB 4. **Page number changes** - Different for each page size ### Implementation in Litestream ```go // From superfly/ltx package func LockPgno(pageSize int) uint32 { return uint32(PENDING_BYTE/pageSize) + 1 } // db.go - Skipping lock page during iteration for pgno := uint32(1); pgno <= maxPgno; pgno++ { if pgno == ltx.LockPgno(db.pageSize) { continue // Skip lock page } // Process normal page processPage(pgno) } ``` ### Testing Lock Page ```sql -- Create database that spans lock page CREATE TABLE test (id INTEGER PRIMARY KEY, data BLOB); -- Insert data until database > 1GB WITH RECURSIVE generate_series(value) AS ( SELECT 1 UNION ALL SELECT value+1 FROM generate_series LIMIT 300000 ) INSERT INTO test SELECT value, randomblob(4000) FROM generate_series; -- Check database size PRAGMA page_count; -- Should be > 262145 for 4KB pages PRAGMA page_size; -- Typically 4096 -- Calculate if lock page is in range -- For 4KB pages: if page_count > 262145, lock page is included ``` ## Transaction Management ### SQLite Transaction Types 1. **Deferred Transaction** (default) ```sql BEGIN DEFERRED; -- Lock acquired on first use ``` 2. **Immediate Transaction** ```sql BEGIN IMMEDIATE; -- RESERVED lock immediately ``` 3. **Exclusive Transaction** ```sql BEGIN EXCLUSIVE; -- EXCLUSIVE lock immediately ``` ### Lock Types in SQLite ```mermaid graph TD UNLOCKED -->|BEGIN| SHARED SHARED -->|Write| RESERVED RESERVED -->|Prepare| PENDING PENDING -->|Commit| EXCLUSIVE EXCLUSIVE -->|Done| UNLOCKED ``` 1. **SHARED** - Multiple readers allowed 2. **RESERVED** - Signals intent to write 3. **PENDING** - Blocking new SHARED locks 4. **EXCLUSIVE** - Single writer, no readers ### Litestream's Long-Running Read Transaction ```go // db.go - Maintaining read transaction for consistency func (db *DB) initReadTx() error { // Start read-only transaction tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{ ReadOnly: true, }) if err != nil { return err } // Execute query to acquire SHARED lock var dummy string err = tx.QueryRow("SELECT ''").Scan(&dummy) if err != nil { tx.Rollback() return err } // Keep transaction open db.rtx = tx return nil } ``` **Purpose:** - Prevents database from being checkpointed past our read point - Ensures consistent view of database - Allows reading pages from WAL ## Checkpoint Modes ### PASSIVE Checkpoint (default) ```sql PRAGMA wal_checkpoint(PASSIVE); ``` - Attempts checkpoint - Fails if readers present - Non-blocking ### FULL Checkpoint ```sql PRAGMA wal_checkpoint(FULL); ``` - Waits for readers to finish - Blocks new readers - Ensures checkpoint completes ### RESTART Checkpoint ```sql PRAGMA wal_checkpoint(RESTART); ``` - Like FULL, but also: - Ensures next writer starts at beginning of WAL - Resets WAL file ### TRUNCATE Checkpoint ```sql PRAGMA wal_checkpoint(TRUNCATE); ``` - Like RESTART, but also: - Truncates WAL file to zero length - Releases disk space ### Litestream Checkpoint Strategy ```go // db.go - Checkpoint decision logic func (db *DB) autoCheckpoint() error { walSize := db.WALSize() pageCount := walSize / db.pageSize if pageCount > db.TruncatePageN { // Force truncation for very large WAL (emergency brake) return db.Checkpoint("TRUNCATE") } else if pageCount > db.MinCheckpointPageN { // Try passive checkpoint (non-blocking) return db.Checkpoint("PASSIVE") } else if db.CheckpointInterval elapsed { // Time-based passive checkpoint return db.Checkpoint("PASSIVE") } // Note: RESTART mode permanently removed due to issue #724 (write-blocking) } return nil } ``` ## Important SQLite Pragmas ### Essential Pragmas for Litestream ```sql -- Enable WAL mode (required) PRAGMA journal_mode = WAL; -- Get database info PRAGMA page_size; -- Page size in bytes PRAGMA page_count; -- Total pages in database PRAGMA freelist_count; -- Free pages -- WAL information PRAGMA wal_checkpoint; -- Perform checkpoint PRAGMA wal_autocheckpoint; -- Auto-checkpoint threshold PRAGMA wal_checkpoint(PASSIVE); -- Non-blocking checkpoint -- Database state PRAGMA integrity_check; -- Verify database integrity PRAGMA quick_check; -- Fast integrity check -- Lock information PRAGMA lock_status; -- Current locks (debug builds) -- Performance tuning PRAGMA synchronous = NORMAL; -- Sync mode PRAGMA busy_timeout = 5000; -- Wait 5s for locks PRAGMA cache_size = -64000; -- 64MB cache ``` ### Reading Pragmas in Go ```go func getDatabaseInfo(db *sql.DB) (*DBInfo, error) { info := &DBInfo{} // Page size err := db.QueryRow("PRAGMA page_size").Scan(&info.PageSize) // Page count err = db.QueryRow("PRAGMA page_count").Scan(&info.PageCount) // Journal mode err = db.QueryRow("PRAGMA journal_mode").Scan(&info.JournalMode) // Calculate size info.Size = info.PageSize * info.PageCount return info, nil } ``` ## SQLite API Usage ### Direct SQLite Access Litestream uses both database/sql and direct SQLite APIs: ```go // Using database/sql for queries db, err := sql.Open("sqlite3", "database.db") // Using modernc.org/sqlite for low-level access conn, err := sqlite.Open("database.db") // Direct page access (requires special builds) page := readPage(conn, pageNumber) ``` ### Connection Modes ```go // Read-only connection db, err := sql.Open("sqlite3", "file:database.db?mode=ro") // WAL mode connection db, err := sql.Open("sqlite3", "database.db?_journal=WAL") // With busy timeout db, err := sql.Open("sqlite3", "database.db?_busy_timeout=5000") // Multiple options db, err := sql.Open("sqlite3", "database.db?_journal=WAL&_busy_timeout=5000&_synchronous=NORMAL") ``` ### WAL File Access Pattern ```go // Litestream's approach to reading WAL func (db *DB) monitorWAL() { walPath := db.path + "-wal" for { // Check WAL file size stat, err := os.Stat(walPath) if err != nil { continue // WAL might not exist yet } // Compare with last known size if stat.Size() > db.lastWALSize { // New data in WAL db.processWALChanges() db.lastWALSize = stat.Size() } time.Sleep(db.MonitorInterval) } } ``` ## Critical SQLite Behaviors ### 1. Automatic Checkpoint SQLite automatically checkpoints when WAL reaches 1000 pages (default): ```go // Can interfere with Litestream's control // Solution: Set high threshold db.Exec("PRAGMA wal_autocheckpoint = 10000") ``` ### 2. Busy Timeout Default timeout is 0 (immediate failure): ```go // Set reasonable timeout db.Exec("PRAGMA busy_timeout = 5000") // 5 seconds ``` ### 3. Synchronous Mode Controls when SQLite waits for disk writes: ```go // NORMAL is safe with WAL db.Exec("PRAGMA synchronous = NORMAL") ``` ### 4. Page Cache SQLite maintains an in-memory page cache: ```go // Set cache size (negative = KB, positive = pages) db.Exec("PRAGMA cache_size = -64000") // 64MB ``` ## WAL to LTX Conversion Litestream converts WAL frames to LTX format: ```go func walToLTX(walFrames []WALFrame) *LTXFile { ltx := <XFile{ Header: LTXHeader{ PageSize: walFrames[0].PageSize, MinTXID: walFrames[0].TransactionID, }, } for _, frame := range walFrames { // Skip lock page if frame.PageNumber == LockPageNumber(ltx.Header.PageSize) { continue } ltx.Pages = append(ltx.Pages, Page{ Number: frame.PageNumber, Data: frame.Data, }) ltx.Header.MaxTXID = frame.TransactionID } return ltx } ``` ## Key Takeaways 1. **WAL is temporary** - Gets merged back via checkpoint 2. **Lock page is sacred** - Never write data at 1GB mark 3. **Page size matters** - Affects lock page number and performance 4. **Transactions provide consistency** - Long-running read prevents changes 5. **Checkpoints are critical** - Balance between WAL size and performance 6. **SQLite locks coordinate access** - Understanding prevents deadlocks 7. **Pragmas control behavior** - Must be set correctly for Litestream This understanding is essential for: - Debugging replication issues - Implementing new features - Optimizing performance - Handling edge cases correctly ================================================ FILE: docs/TESTING_GUIDE.md ================================================ # Litestream Testing Guide Comprehensive guide for testing Litestream components and handling edge cases. ## Table of Contents - [Testing Philosophy](#testing-philosophy) - [1GB Database Testing](#1gb-database-testing) - [Race Condition Testing](#race-condition-testing) - [Integration Testing](#integration-testing) - [Performance Testing](#performance-testing) - [Mock Usage Patterns](#mock-usage-patterns) - [Test Utilities](#test-utilities) - [Common Test Failures](#common-test-failures) ## Testing Philosophy Litestream testing follows these principles: 1. **Test at Multiple Levels**: Unit, integration, and end-to-end 2. **Focus on Edge Cases**: Especially >1GB databases and eventual consistency 3. **Use Real SQLite**: Avoid mocking SQLite behavior 4. **Race Detection**: Always run with `-race` flag 5. **Deterministic Tests**: Use fixed seeds and timestamps where possible ## 1GB Database Testing ### The Lock Page Problem SQLite reserves a special lock page at exactly 1GB (0x40000000 bytes). This page cannot contain data and must be skipped during replication. ### Test Requirements #### Creating Test Databases ```bash # Use litestream-test tool for large databases ./bin/litestream-test populate \ -db test.db \ -target-size 1.5GB \ -page-size 4096 ``` #### Manual Test Database Creation ```go func createLargeTestDB(t *testing.T, path string, targetSize int64) { db, err := sql.Open("sqlite", path+"?_journal=WAL") require.NoError(t, err) defer db.Close() // Set page size _, err = db.Exec("PRAGMA page_size = 4096") require.NoError(t, err) // Create test table _, err = db.Exec(` CREATE TABLE test_data ( id INTEGER PRIMARY KEY, data BLOB NOT NULL ) `) require.NoError(t, err) // Calculate rows needed rowSize := 4000 // bytes per row rowsNeeded := targetSize / int64(rowSize) // Batch insert for performance tx, err := db.Begin() require.NoError(t, err) stmt, err := tx.Prepare("INSERT INTO test_data (data) VALUES (?)") require.NoError(t, err) for i := int64(0); i < rowsNeeded; i++ { data := make([]byte, rowSize) rand.Read(data) _, err = stmt.Exec(data) require.NoError(t, err) // Commit periodically if i%1000 == 0 { err = tx.Commit() require.NoError(t, err) tx, err = db.Begin() require.NoError(t, err) stmt, err = tx.Prepare("INSERT INTO test_data (data) VALUES (?)") require.NoError(t, err) } } err = tx.Commit() require.NoError(t, err) // Verify size var pageCount, pageSize int db.QueryRow("PRAGMA page_count").Scan(&pageCount) db.QueryRow("PRAGMA page_size").Scan(&pageSize) actualSize := int64(pageCount * pageSize) t.Logf("Created database: %d bytes (%d pages of %d bytes)", actualSize, pageCount, pageSize) // Verify lock page is in range lockPgno := ltx.LockPgno(pageSize) if pageCount > lockPgno { t.Logf("Database spans lock page at page %d", lockPgno) } } ``` #### Lock Page Test Cases ```go func TestDB_LockPageHandling(t *testing.T) { testCases := []struct { name string pageSize int lockPgno uint32 }{ {"4KB pages", 4096, 262145}, {"8KB pages", 8192, 131073}, {"16KB pages", 16384, 65537}, {"32KB pages", 32768, 32769}, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { // Create database larger than 1GB dbPath := filepath.Join(t.TempDir(), "test.db") createLargeTestDB(t, dbPath, 1100*1024*1024) // 1.1GB // Open with Litestream db := litestream.NewDB(dbPath) err := db.Open() require.NoError(t, err) defer db.Close(context.Background()) // Start replication replica := litestream.NewReplicaWithClient(db, newMockClient()) err = replica.Start(context.Background()) require.NoError(t, err) // Perform writes that span the lock page conn, err := sql.Open("sqlite", dbPath) require.NoError(t, err) tx, err := conn.Begin() require.NoError(t, err) // Write data around lock page boundary for i := tc.lockPgno - 10; i < tc.lockPgno+10; i++ { if i == tc.lockPgno { continue // Skip lock page } _, err = tx.Exec(fmt.Sprintf( "INSERT INTO test_data (id, data) VALUES (%d, randomblob(4000))", i)) require.NoError(t, err) } err = tx.Commit() require.NoError(t, err) // Wait for sync err = db.Sync(context.Background()) require.NoError(t, err) // Verify replication skipped lock page verifyLockPageSkipped(t, replica, tc.lockPgno) }) } } func verifyLockPageSkipped(t *testing.T, replica *litestream.Replica, lockPgno uint32) { // Get LTX files files, err := replica.Client.LTXFiles(context.Background(), 0, 0, false) require.NoError(t, err) // Check each file for files.Next() { info := files.Item() // Read page index pageIndex, err := litestream.FetchPageIndex(context.Background(), replica.Client, info) require.NoError(t, err) // Verify lock page not present _, hasLockPage := pageIndex[lockPgno] assert.False(t, hasLockPage, "Lock page %d should not be in LTX file", lockPgno) } } ``` ### Restoration Testing ```go func TestDB_RestoreLargeDatabase(t *testing.T) { // Create and replicate large database srcPath := filepath.Join(t.TempDir(), "source.db") createLargeTestDB(t, srcPath, 1500*1024*1024) // 1.5GB // Setup replication db := litestream.NewDB(srcPath) err := db.Open() require.NoError(t, err) client := file.NewReplicaClient(filepath.Join(t.TempDir(), "replica")) replica := litestream.NewReplicaWithClient(db, client) err = replica.Start(context.Background()) require.NoError(t, err) // Let it replicate err = db.Sync(context.Background()) require.NoError(t, err) db.Close(context.Background()) // Restore to new location dstPath := filepath.Join(t.TempDir(), "restored.db") err = litestream.Restore(context.Background(), client, dstPath, nil) require.NoError(t, err) // Verify restoration verifyDatabasesMatch(t, srcPath, dstPath) } func verifyDatabasesMatch(t *testing.T, path1, path2 string) { // Compare checksums checksum1 := calculateDBChecksum(t, path1) checksum2 := calculateDBChecksum(t, path2) assert.Equal(t, checksum1, checksum2, "Database checksums should match") // Compare page counts pageCount1 := getPageCount(t, path1) pageCount2 := getPageCount(t, path2) assert.Equal(t, pageCount1, pageCount2, "Page counts should match") // Run integrity check db, err := sql.Open("sqlite", path2) require.NoError(t, err) defer db.Close() var result string err = db.QueryRow("PRAGMA integrity_check").Scan(&result) require.NoError(t, err) assert.Equal(t, "ok", result, "Integrity check should pass") } ``` ## Race Condition Testing ### Running with Race Detector ```bash # Always run tests with race detector go test -race -v ./... # Run specific race-prone tests go test -race -v -run TestReplica_Sync ./... go test -race -v -run TestDB_Sync ./... go test -race -v -run TestStore_CompactDB ./... ``` ### Common Race Conditions #### 1. Position Updates ```go func TestReplica_ConcurrentPositionUpdate(t *testing.T) { replica := litestream.NewReplica(nil) var wg sync.WaitGroup errors := make(chan error, 100) // Concurrent writers for i := 0; i < 10; i++ { wg.Add(1) go func(n int) { defer wg.Done() pos := ltx.NewPos(ltx.TXID(n), ltx.Checksum(uint64(n))) // This should use proper locking replica.SetPos(pos) // Verify position readPos := replica.Pos() if readPos.TXID < ltx.TXID(n) { errors <- fmt.Errorf("position went backwards") } }(i) } // Concurrent readers for i := 0; i < 10; i++ { wg.Add(1) go func() { defer wg.Done() for j := 0; j < 100; j++ { _ = replica.Pos() time.Sleep(time.Microsecond) } }() } wg.Wait() close(errors) for err := range errors { t.Error(err) } } ``` #### 2. WAL Monitoring ```go func TestDB_ConcurrentWALAccess(t *testing.T) { db := setupTestDB(t) defer db.Close(context.Background()) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() var wg sync.WaitGroup // Writer goroutine wg.Add(1) go func() { defer wg.Done() conn, err := sql.Open("sqlite", db.Path()) if err != nil { return } defer conn.Close() for i := 0; i < 100; i++ { _, _ = conn.Exec("INSERT INTO test VALUES (?)", i) time.Sleep(10 * time.Millisecond) } }() // Monitor goroutine wg.Add(1) go func() { defer wg.Done() notifyCh := db.Notify() for { select { case <-ctx.Done(): return case <-notifyCh: // Process WAL changes _ = db.Sync(context.Background()) } } }() // Checkpoint goroutine wg.Add(1) go func() { defer wg.Done() ticker := time.NewTicker(100 * time.Millisecond) defer ticker.Stop() for { select { case <-ctx.Done(): return case <-ticker.C: _ = db.Checkpoint(context.Background(), litestream.CheckpointModePassive) } } }() wg.Wait() } ``` ### Test Cleanup ```go func TestStore_Integration(t *testing.T) { // Setup tmpDir := t.TempDir() db := setupTestDB(t, tmpDir) // Use defer with error channel for cleanup insertErr := make(chan error, 1) // Cleanup function cleanup := func() { select { case err := <-insertErr: if err != nil { t.Errorf("insert error during test: %v", err) } default: } } defer cleanup() // Test with timeout ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() // Run test... } ``` ## Integration Testing ### Test Structure ```go // +build integration package litestream_test import ( "context" "os" "testing" ) func TestIntegration_S3(t *testing.T) { if testing.Short() { t.Skip("skipping integration test") } // Check for credentials if os.Getenv("AWS_ACCESS_KEY_ID") == "" { t.Skip("AWS_ACCESS_KEY_ID not set") } // Run test against real S3 runIntegrationTest(t, setupS3Client()) } func runIntegrationTest(t *testing.T, client ReplicaClient) { ctx := context.Background() t.Run("BasicReplication", func(t *testing.T) { // Test basic write/read cycle }) t.Run("Compaction", func(t *testing.T) { // Test compaction with remote storage }) t.Run("EventualConsistency", func(t *testing.T) { // Test handling of eventual consistency }) t.Run("LargeFiles", func(t *testing.T) { // Test with files > 100MB }) t.Run("Cleanup", func(t *testing.T) { err := client.DeleteAll(ctx) require.NoError(t, err) }) } ``` ### Environment-Based Configuration ```go func setupS3Client() *s3.ReplicaClient { return &s3.ReplicaClient{ AccessKeyID: os.Getenv("AWS_ACCESS_KEY_ID"), SecretAccessKey: os.Getenv("AWS_SECRET_ACCESS_KEY"), Region: getEnvOrDefault("AWS_REGION", "us-east-1"), Bucket: getEnvOrDefault("TEST_S3_BUCKET", "litestream-test"), Path: fmt.Sprintf("test-%d", time.Now().Unix()), } } func getEnvOrDefault(key, defaultValue string) string { if value := os.Getenv(key); value != "" { return value } return defaultValue } ``` ## Performance Testing ### Benchmarks ```go func BenchmarkDB_Sync(b *testing.B) { db := setupBenchDB(b) defer db.Close(context.Background()) // Prepare test data conn, _ := sql.Open("sqlite", db.Path()) defer conn.Close() for i := 0; i < 1000; i++ { conn.Exec("INSERT INTO test VALUES (?)", i) } b.ResetTimer() for i := 0; i < b.N; i++ { err := db.Sync(context.Background()) if err != nil { b.Fatal(err) } } b.ReportMetric(float64(b.N)/b.Elapsed().Seconds(), "syncs/sec") } func BenchmarkCompaction(b *testing.B) { benchmarks := []struct { name string fileCount int fileSize int }{ {"Small-Many", 1000, 1024}, // Many small files {"Medium", 100, 10 * 1024}, // Medium files {"Large-Few", 10, 100 * 1024}, // Few large files } for _, bm := range benchmarks { b.Run(bm.name, func(b *testing.B) { for i := 0; i < b.N; i++ { b.StopTimer() files := generateTestFiles(bm.fileCount, bm.fileSize) b.StartTimer() _, err := compact(files) if err != nil { b.Fatal(err) } } totalSize := int64(bm.fileCount * bm.fileSize) b.ReportMetric(float64(totalSize)/float64(b.Elapsed().Nanoseconds()), "bytes/ns") }) } } ``` ### Load Testing ```go func TestDB_LoadTest(t *testing.T) { if testing.Short() { t.Skip("skipping load test") } db := setupTestDB(t) defer db.Close(context.Background()) // Configure load config := LoadConfig{ Duration: 5 * time.Minute, WriteRate: 100, // writes/sec ReadRate: 500, // reads/sec Workers: 10, DataSize: 4096, BurstPattern: true, } results := runLoadTest(t, db, config) // Verify results assert.Greater(t, results.TotalWrites, int64(20000)) assert.Less(t, results.P99Latency, 100*time.Millisecond) assert.Zero(t, results.Errors) t.Logf("Load test results: %+v", results) } type LoadResults struct { TotalWrites int64 TotalReads int64 Errors int64 P50Latency time.Duration P99Latency time.Duration BytesReplicated int64 } func runLoadTest(t *testing.T, db *DB, config LoadConfig) LoadResults { ctx, cancel := context.WithTimeout(context.Background(), config.Duration) defer cancel() var results LoadResults var mu sync.Mutex latencies := make([]time.Duration, 0, 100000) // Start workers var wg sync.WaitGroup for i := 0; i < config.Workers; i++ { wg.Add(1) go func(workerID int) { defer wg.Done() conn, err := sql.Open("sqlite", db.Path()) if err != nil { return } defer conn.Close() ticker := time.NewTicker(time.Second / time.Duration(config.WriteRate)) defer ticker.Stop() for { select { case <-ctx.Done(): return case <-ticker.C: start := time.Now() data := make([]byte, config.DataSize) rand.Read(data) _, err := conn.Exec("INSERT INTO test (data) VALUES (?)", data) latency := time.Since(start) mu.Lock() if err != nil { results.Errors++ } else { results.TotalWrites++ latencies = append(latencies, latency) } mu.Unlock() } } }(i) } wg.Wait() // Calculate percentiles sort.Slice(latencies, func(i, j int) bool { return latencies[i] < latencies[j] }) if len(latencies) > 0 { results.P50Latency = latencies[len(latencies)*50/100] results.P99Latency = latencies[len(latencies)*99/100] } return results } ``` ## Mock Usage Patterns ### Mock ReplicaClient ```go type MockReplicaClient struct { mu sync.Mutex files map[string]*ltx.FileInfo data map[string][]byte // Control behavior FailureRate float64 Latency time.Duration EventualDelay time.Duration } func (m *MockReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { // Simulate latency if m.Latency > 0 { time.Sleep(m.Latency) } // Simulate failures if m.FailureRate > 0 && rand.Float64() < m.FailureRate { return nil, errors.New("simulated failure") } // Simulate eventual consistency if m.EventualDelay > 0 { time.AfterFunc(m.EventualDelay, func() { m.mu.Lock() defer m.mu.Unlock() // Make file available after delay }) } // Store file data, err := io.ReadAll(r) if err != nil { return nil, err } m.mu.Lock() defer m.mu.Unlock() key := fmt.Sprintf("%d-%016x-%016x", level, minTXID, maxTXID) info := <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Size: int64(len(data)), CreatedAt: time.Now(), } m.files[key] = info m.data[key] = data return info, nil } ``` ### Mock Database ```go type MockDB struct { mu sync.Mutex path string replicas []*Replica closed bool // Control behavior CheckpointFailures int SyncDelay time.Duration } func (m *MockDB) Sync(ctx context.Context) error { if m.SyncDelay > 0 { select { case <-time.After(m.SyncDelay): case <-ctx.Done(): return ctx.Err() } } m.mu.Lock() defer m.mu.Unlock() if m.closed { return errors.New("database closed") } for _, r := range m.replicas { if err := r.Sync(ctx); err != nil { return err } } return nil } ``` ## Test Utilities ### Helper Functions ```go // testutil/db.go package testutil import ( "database/sql" "testing" "path/filepath" ) func NewTestDB(t testing.TB) *litestream.DB { t.Helper() path := filepath.Join(t.TempDir(), "test.db") // Create SQLite database conn, err := sql.Open("sqlite", path+"?_journal=WAL") require.NoError(t, err) _, err = conn.Exec(` CREATE TABLE test ( id INTEGER PRIMARY KEY, data BLOB ) `) require.NoError(t, err) conn.Close() // Open with Litestream db := litestream.NewDB(path) db.MonitorInterval = 10 * time.Millisecond // Speed up for tests db.MinCheckpointPageN = 100 // Lower threshold for tests err = db.Open() require.NoError(t, err) t.Cleanup(func() { db.Close(context.Background()) }) return db } func WriteTestData(t testing.TB, db *litestream.DB, count int) { t.Helper() conn, err := sql.Open("sqlite", db.Path()) require.NoError(t, err) defer conn.Close() tx, err := conn.Begin() require.NoError(t, err) for i := 0; i < count; i++ { data := make([]byte, 100) rand.Read(data) _, err = tx.Exec("INSERT INTO test (data) VALUES (?)", data) require.NoError(t, err) } err = tx.Commit() require.NoError(t, err) } ``` ### Test Fixtures ```go // testdata/fixtures.go package testdata import _ "embed" //go:embed small.db var SmallDB []byte //go:embed large.db var LargeDB []byte //go:embed corrupted.db var CorruptedDB []byte func ExtractFixture(name string, path string) error { var data []byte switch name { case "small": data = SmallDB case "large": data = LargeDB case "corrupted": data = CorruptedDB default: return fmt.Errorf("unknown fixture: %s", name) } return os.WriteFile(path, data, 0600) } ``` ## Common Test Failures ### 1. Database Locked Errors ```go // Problem: Multiple connections without proper WAL mode func TestBroken(t *testing.T) { db1, _ := sql.Open("sqlite", "test.db") // Wrong! WAL disabled db2, _ := sql.Open("sqlite", "test.db") // Will fail } // Solution: Use WAL mode func TestFixed(t *testing.T) { db1, _ := sql.Open("sqlite", "test.db?_journal=WAL") db2, _ := sql.Open("sqlite", "test.db?_journal=WAL") } ``` ### 2. Timing Issues ```go // Problem: Race between write and sync func TestBroken(t *testing.T) { WriteData(db) result := ReadReplica() // May not see data yet! } // Solution: Explicit sync func TestFixed(t *testing.T) { WriteData(db) err := db.Sync(context.Background()) require.NoError(t, err) result := ReadReplica() // Now guaranteed to see data } ``` ### 3. Cleanup Issues ```go // Problem: Goroutine outlives test func TestBroken(t *testing.T) { go func() { time.Sleep(10 * time.Second) doWork() // Test already finished! }() } // Solution: Use context and wait func TestFixed(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done() select { case <-ctx.Done(): return case <-time.After(10 * time.Second): doWork() } }() // Test work... cancel() // Signal shutdown wg.Wait() // Wait for goroutine } ``` ### 4. File Handle Leaks ```go // Problem: Not closing files func TestBroken(t *testing.T) { f, _ := os.Open("test.db") // Missing f.Close()! } // Solution: Always use defer func TestFixed(t *testing.T) { f, err := os.Open("test.db") require.NoError(t, err) defer f.Close() } ``` ## Test Coverage ### Running Coverage ```bash # Generate coverage report go test -coverprofile=coverage.out ./... # View coverage in browser go tool cover -html=coverage.out # Check coverage percentage go tool cover -func=coverage.out | grep total # Coverage by package go test -cover ./... ``` ### Coverage Requirements - Core packages (`db.go`, `replica.go`, `store.go`): >80% - Replica clients: >70% - Utilities: >60% - Mock implementations: Not required ### Improving Coverage ```go // Use test tables for comprehensive coverage func TestDB_Checkpoint(t *testing.T) { tests := []struct { name string mode string walSize int wantErr bool }{ {"Passive", "PASSIVE", 100, false}, {"Full", "FULL", 1000, false}, {"Restart", "RESTART", 5000, false}, {"Truncate", "TRUNCATE", 10000, false}, {"Invalid", "INVALID", 100, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { db := setupTestDB(t) generateWAL(t, db, tt.walSize) err := db.Checkpoint(tt.mode) if tt.wantErr { assert.Error(t, err) } else { assert.NoError(t, err) } }) } } ``` ================================================ FILE: docs/VFS.md ================================================ # Litestream VFS The Litestream VFS (Virtual File System) is a SQLite extension that allows applications to read directly from Litestream replica storage (S3, GCS, Azure Blob, etc.) without restoring to local disk. It also supports write mode for remote-first SQLite databases. ## Table of Contents - [Overview](#overview) - [Building](#building) - [Configuration](#configuration) - [Usage](#usage) - [SQL Functions](#sql-functions) - [Time Travel](#time-travel) - [Write Mode](#write-mode) - [Supported Storage Backends](#supported-storage-backends) ## Overview The VFS extension provides: - **Direct replica reading**: Read SQLite databases directly from cloud storage - **Automatic polling**: Background polling for new LTX files from the primary - **Page caching**: LRU cache for frequently accessed pages (default 10MB) - **Time travel**: Query historical database states at specific timestamps - **Write support**: Write changes that sync back to remote storage (experimental) ### How It Works 1. The VFS loads as a SQLite extension 2. When opening a database, it reads LTX files from the configured replica URL 3. Page requests are satisfied from cached data or fetched from remote storage 4. A background goroutine polls for new LTX files at a configurable interval ## Building ### Prerequisites - Go 1.21+ - GCC (Linux) or Clang (macOS) - CGO enabled ### Build Commands **macOS (current architecture):** ```bash make vfs ``` This creates: - `dist/litestream-vfs.a` - Static library - `dist/litestream-vfs.so` - Loadable SQLite extension **Platform-specific builds:** ```bash # macOS ARM64 (Apple Silicon) make vfs-darwin-arm64 # Output: dist/litestream-vfs-darwin-arm64.dylib # macOS AMD64 (Intel) make vfs-darwin-amd64 # Output: dist/litestream-vfs-darwin-amd64.dylib # Linux AMD64 make vfs-linux-amd64 # Output: dist/litestream-vfs-linux-amd64.so # Linux ARM64 make vfs-linux-arm64 # Output: dist/litestream-vfs-linux-arm64.so ``` ### Running Tests ```bash make vfs-test ``` ## Configuration The VFS is configured via environment variables: | Variable | Description | Default | |----------|-------------|---------| | `LITESTREAM_REPLICA_URL` | Replica storage URL (required) | - | | `LITESTREAM_LOG_LEVEL` | Log level: `DEBUG` or `INFO` | `INFO` | | `LITESTREAM_WRITE_ENABLED` | Enable write mode: `true` or `false` | `false` | | `LITESTREAM_SYNC_INTERVAL` | Write sync interval (e.g., `1s`, `500ms`) | `1s` | | `LITESTREAM_BUFFER_PATH` | Local write buffer file path | temp file | ### Replica URL Format ``` # Amazon S3 s3://bucket-name/path/to/db # Google Cloud Storage gs://bucket-name/path/to/db # Azure Blob Storage abs://container-name/path/to/db # Alibaba OSS oss://bucket-name/path/to/db # Local filesystem file:///path/to/replica # SFTP sftp://user@host:port/path/to/db # NATS JetStream nats://host:port/bucket/path # WebDAV webdav://host:port/path/to/db ``` ## Usage ### Loading the Extension ```sql -- Load the extension (adjust path as needed) .load ./dist/litestream-vfs.so ``` ### Opening a Database Before loading the extension, set the replica URL: ```bash export LITESTREAM_REPLICA_URL="s3://my-bucket/mydb" ``` Then in SQLite: ```sql .load ./dist/litestream-vfs.so .open file:mydb.db?vfs=litestream SELECT * FROM my_table; ``` ### Python Example ```python import os import sqlite3 os.environ["LITESTREAM_REPLICA_URL"] = "s3://my-bucket/mydb" conn = sqlite3.connect(":memory:") conn.enable_load_extension(True) conn.load_extension("./dist/litestream-vfs.so") # Open database using the litestream VFS conn = sqlite3.connect("file:mydb.db?vfs=litestream") cursor = conn.execute("SELECT * FROM users") for row in cursor: print(row) ``` ### Go Example ```go import ( "database/sql" "os" _ "github.com/mattn/go-sqlite3" ) func main() { os.Setenv("LITESTREAM_REPLICA_URL", "s3://my-bucket/mydb") db, err := sql.Open("sqlite3", "file:mydb.db?vfs=litestream") if err != nil { panic(err) } defer db.Close() rows, err := db.Query("SELECT * FROM users") // ... } ``` ## SQL Functions The VFS extension provides SQL functions for observability and time travel: ### `litestream_txid()` Returns the current transaction ID as a hex string. ```sql SELECT litestream_txid(); -- Returns: "0000000000000042" ``` ### `litestream_time()` Returns the current view timestamp (RFC3339 format) or `"latest"`. ```sql SELECT litestream_time(); -- Returns: "2024-01-15T10:30:00.123456789Z" or "latest" ``` ### `litestream_lag()` Returns seconds since the last successful poll for new LTX files. Returns `-1` if no successful poll has occurred. ```sql SELECT litestream_lag(); -- Returns: 2 (seconds behind primary) ``` ### `litestream_set_time(timestamp)` Sets the view time for time travel queries. See [Time Travel](#time-travel) for details. ## Time Travel The VFS supports querying historical database states by setting a target timestamp. ### Setting a Target Time ```sql -- View database as of a specific timestamp (RFC3339 format) SELECT litestream_set_time('2024-01-15T10:30:00Z'); -- Relative time expressions are also supported SELECT litestream_set_time('5 minutes ago'); SELECT litestream_set_time('yesterday'); SELECT litestream_set_time('2 hours ago'); -- Return to latest state SELECT litestream_set_time('LATEST'); ``` ### Example: Comparing Historical Data ```sql -- Check current count SELECT COUNT(*) FROM orders; -- Returns: 1000 -- Go back in time SELECT litestream_set_time('1 hour ago'); -- Check historical count SELECT COUNT(*) FROM orders; -- Returns: 950 -- Return to present SELECT litestream_set_time('LATEST'); ``` ### Time Travel Limitations - Time travel rebuilds the page index, which may take time for large databases - Historical data is only available if LTX files haven't been compacted away - L0 retention settings affect how far back you can travel - Time travel is read-only (writes are disabled while viewing historical state) ## Write Mode Write mode allows the VFS to accept writes and sync them back to remote storage. This is experimental. ### Enabling Write Mode ```bash export LITESTREAM_REPLICA_URL="s3://my-bucket/mydb" export LITESTREAM_WRITE_ENABLED="true" export LITESTREAM_SYNC_INTERVAL="1s" ``` ### How Write Mode Works 1. Writes are captured to a local buffer file for durability 2. Dirty pages are tracked in memory 3. Periodically (or on close), dirty pages are packaged into an LTX file 4. The LTX file is uploaded to remote storage 5. Conflict detection prevents overwrites if the remote has newer transactions ### Write Mode Considerations - **Connection pooling**: Multiple connections can be opened in write mode (for example, by `database/sql`) - **Single writer**: Write contention is enforced at lock acquisition. If another connection already holds write intent, SQLite returns `SQLITE_BUSY` - **Conflict detection**: If the remote has advanced unexpectedly, `ErrConflict` is returned - **Buffer durability**: The local buffer file provides crash recovery for uncommitted writes - **Sync interval**: Balance between durability (shorter) and performance (longer) - **New databases**: Write mode can create new databases from scratch if no LTX files exist ### Creating a New Database With write mode enabled, you can create a new database that doesn't exist yet: ```sql .load ./dist/litestream-vfs.so .open file:newdb.db?vfs=litestream CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT); INSERT INTO users (name) VALUES ('Alice'); -- Data is synced to remote storage automatically ``` ## Supported Storage Backends The VFS supports all Litestream storage backends: | Backend | URL Scheme | Notes | |---------|-----------|-------| | Amazon S3 | `s3://` | Supports S3-compatible services (MinIO, DigitalOcean Spaces, etc.) | | Google Cloud Storage | `gs://` | Requires `GOOGLE_APPLICATION_CREDENTIALS` | | Azure Blob Storage | `abs://` | Requires `AZURE_STORAGE_ACCOUNT` and credentials | | Alibaba OSS | `oss://` | Object Storage Service | | Local filesystem | `file://` | Useful for testing and development | | SFTP | `sftp://` | SSH File Transfer Protocol | | NATS JetStream | `nats://` | Object store via NATS | | WebDAV | `webdav://` | Web Distributed Authoring and Versioning | ### S3 Configuration For S3 and S3-compatible services, set credentials via environment variables: ```bash export AWS_ACCESS_KEY_ID="your-key" export AWS_SECRET_ACCESS_KEY="your-secret" export AWS_REGION="us-east-1" # For S3-compatible services: export LITESTREAM_REPLICA_URL="s3://bucket/path?endpoint=https://custom.endpoint.com" ``` ### GCS Configuration ```bash export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json" export LITESTREAM_REPLICA_URL="gs://bucket/path" ``` ## Troubleshooting ### Extension fails to load Ensure the extension file matches your platform: - macOS: `.dylib` or `.so` - Linux: `.so` ### "no backup files available" The VFS waits for LTX files to become available. Ensure: 1. The replica URL is correct 2. Litestream has replicated at least one transaction 3. Credentials are properly configured ### High latency reads - Increase `CacheSize` for larger page cache - Reduce `PollInterval` for more responsive updates - Consider using a closer storage region ### Debug logging ```bash export LITESTREAM_LOG_LEVEL="DEBUG" ``` This enables verbose logging of VFS operations, page fetches, and cache hits/misses. ================================================ FILE: etc/build.ps1 ================================================ [CmdletBinding()] Param ( [Parameter(Mandatory = $true)] [String] $Version ) $ErrorActionPreference = "Stop" # Update working directory. Push-Location $PSScriptRoot Trap { Pop-Location } Invoke-Expression "candle.exe -nologo -arch x64 -ext WixUtilExtension -out litestream.wixobj -dVersion=`"$Version`" litestream.wxs" Invoke-Expression "light.exe -nologo -spdb -ext WixUtilExtension -out `"litestream-${Version}.msi`" litestream.wixobj" Pop-Location ================================================ FILE: etc/gon-sign.hcl ================================================ source = ["./dist/litestream"] bundle_id = "com.middlemost.litestream" apple_id { username = "@env:APPLE_ID_USERNAME" password = "@env:AC_PASSWORD" provider = "@env:APPLE_TEAM_ID" } sign { application_identity = "@env:APPLE_DEVELOPER_ID_APPLICATION" entitlements_file = "" } notarize { path = "./dist/litestream.zip" bundle_id = "com.middlemost.litestream" staple = true } zip { output_path = "./dist/litestream-signed.zip" } ================================================ FILE: etc/gon.hcl ================================================ source = ["./dist/litestream"] bundle_id = "com.middlemost.litestream" apple_id { username = "benbjohnson@yahoo.com" password = "@env:AC_PASSWORD" } sign { application_identity = "Developer ID Application: Middlemost Systems, LLC" } zip { output_path = "dist/litestream.zip" } ================================================ FILE: etc/litestream.service ================================================ [Unit] Description=Litestream [Service] Restart=always ExecStart=/usr/bin/litestream replicate [Install] WantedBy=multi-user.target ================================================ FILE: etc/litestream.wxs ================================================ ================================================ FILE: etc/litestream.yml ================================================ # AWS credentials # access-key-id: AKIAxxxxxxxxxxxxxxxx # secret-access-key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxxx # Control socket for runtime start/stop commands # socket: # enabled: true # Enable control socket (default: false) # path: /var/run/litestream.sock # Socket path (default: /var/run/litestream.sock) # permissions: 0600 # Socket file permissions (default: 0600) # dbs: # - path: /path/to/primary/db # Database to replicate from # replica: # path: /path/to/replica # File-based replication # url: s3://my.bucket.com/db # S3-based replication # # Example Fly.io Tigris setup (signing/no-Content-MD5 are auto-enabled for this endpoint) # # url: s3://my-tigris-bucket/db.sqlite?endpoint=fly.storage.tigris.dev®ion=auto # type: nats # NATS JetStream replication # url: nats://nats.example.com:4222 # bucket: litestream-backups # # Optional TLS configuration: # # client-cert: /path/to/client.pem # # client-key: /path/to/client.key # # root-cas: [/path/to/ca.pem] # - path: /path/to/another/db # replica: # url: sftp://user@host:22/path # SFTP-based replication # key-path: /etc/litestream/sftp_key # # Strongly recommended: SSH host key for verification # # Get this from the server's /etc/ssh/ssh_host_*.pub file # # or use `ssh-keyscan hostname` # # Example formats: # # host-key: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMvvypUkBrS9RCyV//p+UFCLg8yKNtTu/ew/cV6XXAAP # # host-key: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ... ================================================ FILE: etc/nfpm.yml ================================================ name: litestream arch: "${GOARCH}" platform: "${GOOS}" version: "${LITESTREAM_VERSION}" section: "default" priority: "extra" maintainer: "Ben Johnson " description: Litestream is a tool for real-time replication of SQLite databases. homepage: "https://github.com/benbjohnson/litestream" license: "Apache 2" contents: - src: ./litestream dst: /usr/bin/litestream - src: ./litestream.yml dst: /etc/litestream.yml type: config - src: ./litestream.service dst: /usr/lib/systemd/system/litestream.service type: config ================================================ FILE: etc/run-s3-docker-tests.sh ================================================ #!/bin/bash set -e # Script to run S3 integration tests against a local MinIO container. # This provides a more realistic test environment than moto for testing # S3-compatible provider compatibility. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" cd "$PROJECT_ROOT" echo "Starting MinIO container..." docker compose -f docker-compose.test.yml up -d # Wait for MinIO to be ready echo "Waiting for MinIO to be ready..." for i in {1..30}; do if docker compose -f docker-compose.test.yml exec -T minio mc ready local 2>/dev/null; then echo "MinIO is ready" break fi if [ $i -eq 30 ]; then echo "MinIO failed to start" docker compose -f docker-compose.test.yml logs minio docker compose -f docker-compose.test.yml down exit 1 fi sleep 1 done # Create test bucket using mc client inside the container echo "Creating test bucket..." docker compose -f docker-compose.test.yml exec -T minio mc alias set local http://localhost:9000 minioadmin minioadmin docker compose -f docker-compose.test.yml exec -T minio mc mb local/test-bucket --ignore-existing # Set up cleanup trap cleanup() { echo "Cleaning up..." docker compose -f docker-compose.test.yml down } trap cleanup EXIT # Export environment variables for the S3 integration tests export LITESTREAM_S3_ACCESS_KEY_ID=minioadmin export LITESTREAM_S3_SECRET_ACCESS_KEY=minioadmin export LITESTREAM_S3_BUCKET=test-bucket export LITESTREAM_S3_ENDPOINT=http://localhost:9000 export LITESTREAM_S3_FORCE_PATH_STYLE=true export LITESTREAM_S3_REGION=us-east-1 echo "Running S3 integration tests against MinIO..." go test -v ./replica_client_test.go -integration -replica-clients=s3 "$@" echo "Tests completed successfully!" ================================================ FILE: etc/s3_mock.py ================================================ #!/usr/bin/env python3 import sys import os import time from moto.server import ThreadedMotoServer import boto3 import subprocess cmd = sys.argv[1:] if len(cmd) == 0: print(f"usage: {sys.argv[0]} [arguments]", file=sys.stderr) sys.exit(1) env = os.environ.copy() | { "LITESTREAM_S3_ACCESS_KEY_ID": "lite", "LITESTREAM_S3_SECRET_ACCESS_KEY": "stream", "LITESTREAM_S3_BUCKET": f"test{int(time.time())}", "LITESTREAM_S3_ENDPOINT": "http://127.0.0.1:5000", "LITESTREAM_S3_FORCE_PATH_STYLE": "true", } server = ThreadedMotoServer() server.start() s3 = boto3.client( "s3", aws_access_key_id=env["LITESTREAM_S3_ACCESS_KEY_ID"], aws_secret_access_key=env["LITESTREAM_S3_SECRET_ACCESS_KEY"], endpoint_url=env["LITESTREAM_S3_ENDPOINT"] ).create_bucket(Bucket=env["LITESTREAM_S3_BUCKET"]) proc = subprocess.run(cmd, env=env) server.stop() sys.exit(proc.returncode) ================================================ FILE: file/replica_client.go ================================================ package file import ( "bytes" "context" "fmt" "io" "log/slog" "net/url" "os" "path/filepath" "slices" "time" "github.com/superfly/ltx" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/internal" ) func init() { litestream.RegisterReplicaClientFactory("file", NewReplicaClientFromURL) } // ReplicaClientType is the client type for this package. const ReplicaClientType = "file" var _ litestream.ReplicaClient = (*ReplicaClient)(nil) var _ litestream.ReplicaClientV3 = (*ReplicaClient)(nil) // ReplicaClient is a client for writing LTX files to disk. type ReplicaClient struct { path string // destination path Replica *litestream.Replica logger *slog.Logger } // NewReplicaClient returns a new instance of ReplicaClient. func NewReplicaClient(path string) *ReplicaClient { return &ReplicaClient{ logger: slog.Default().WithGroup(ReplicaClientType), path: path, } } func (c *ReplicaClient) SetLogger(logger *slog.Logger) { c.logger = logger.WithGroup(ReplicaClientType) } // NewReplicaClientFromURL creates a new ReplicaClient from URL components. // This is used by the replica client factory registration. func NewReplicaClientFromURL(scheme, host, urlPath string, query url.Values, userinfo *url.Userinfo) (litestream.ReplicaClient, error) { // For file URLs, the path is the full path if urlPath == "" { return nil, fmt.Errorf("file replica path required") } return NewReplicaClient(urlPath), nil } // db returns the database, if available. func (c *ReplicaClient) db() *litestream.DB { if c.Replica == nil { return nil } return c.Replica.DB() } // Type returns "file" as the client type. func (c *ReplicaClient) Type() string { return ReplicaClientType } // Init is a no-op for file replica client as no initialization is required. func (c *ReplicaClient) Init(ctx context.Context) error { return nil } // Path returns the destination path to replicate the database to. func (c *ReplicaClient) Path() string { return c.path } // LTXLevelDir returns the path to a given level. func (c *ReplicaClient) LTXLevelDir(level int) string { return filepath.FromSlash(litestream.LTXLevelDir(c.path, level)) } // LTXFilePath returns the path to an LTX file. func (c *ReplicaClient) LTXFilePath(level int, minTXID, maxTXID ltx.TXID) string { return filepath.FromSlash(litestream.LTXFilePath(c.path, level, minTXID, maxTXID)) } // LTXFiles returns an iterator over all LTX files on the replica for the given level. // The useMetadata parameter is ignored for file backend as ModTime is always available from readdir. func (c *ReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { f, err := os.Open(c.LTXLevelDir(level)) if os.IsNotExist(err) { return ltx.NewFileInfoSliceIterator(nil), nil } else if err != nil { return nil, err } defer f.Close() fis, err := f.Readdir(-1) if err != nil { return nil, err } // Iterate over every file and convert to metadata. // ModTime contains the accurate timestamp set by Chtimes in WriteLTXFile. infos := make([]*ltx.FileInfo, 0, len(fis)) for _, fi := range fis { minTXID, maxTXID, err := ltx.ParseFilename(fi.Name()) if err != nil { continue } else if minTXID < seek { continue } infos = append(infos, <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Size: fi.Size(), CreatedAt: fi.ModTime().UTC(), }) } return ltx.NewFileInfoSliceIterator(infos), nil } // OpenLTXFile returns a reader for an LTX file at the given position. // Returns os.ErrNotExist if no matching index/offset is found. func (c *ReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { path := c.LTXFilePath(level, minTXID, maxTXID) f, err := os.Open(path) if err != nil { if os.IsNotExist(err) { return nil, litestream.NewLTXError("open", path, level, uint64(minTXID), uint64(maxTXID), err) } return nil, fmt.Errorf("open ltx file %s: %w", path, err) } if offset > 0 { if _, err := f.Seek(offset, io.SeekStart); err != nil { return nil, err } } if size > 0 { return internal.LimitReadCloser(f, size), nil } return f, nil } // WriteLTXFile writes an LTX file to the replica. // Extracts timestamp from LTX header and sets it as the file's ModTime to preserve original creation time. func (c *ReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, rd io.Reader) (info *ltx.FileInfo, err error) { var fileInfo, dirInfo os.FileInfo if db := c.db(); db != nil { fileInfo, dirInfo = db.FileInfo(), db.DirInfo() } // Use TeeReader to peek at LTX header while preserving data for upload var buf bytes.Buffer teeReader := io.TeeReader(rd, &buf) // Extract timestamp from LTX header hdr, _, err := ltx.PeekHeader(teeReader) if err != nil { return nil, fmt.Errorf("extract timestamp from LTX header: %w", err) } timestamp := time.UnixMilli(hdr.Timestamp).UTC() // Combine buffered data with rest of reader fullReader := io.MultiReader(&buf, rd) // Ensure parent directory exists. filename := c.LTXFilePath(level, minTXID, maxTXID) if err := internal.MkdirAll(filepath.Dir(filename), dirInfo); err != nil { return nil, err } // Write LTX file to temporary file next to destination path. tmpFilename := filename + ".tmp" f, err := internal.CreateFile(tmpFilename, fileInfo) if err != nil { return nil, err } // Clean up temp file on error. On successful rename, the temp file // becomes the final file and should not be removed. defer func() { _ = f.Close() if err != nil { _ = os.Remove(tmpFilename) } }() if _, err := io.Copy(f, fullReader); err != nil { return nil, err } if err := f.Sync(); err != nil { return nil, err } // Build metadata. fi, err := f.Stat() if err != nil { return nil, err } info = <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Size: fi.Size(), CreatedAt: timestamp, } if err := f.Close(); err != nil { return nil, err } // Move LTX file to final path when it has been written & synced to disk. if err := os.Rename(tmpFilename, filename); err != nil { return nil, err } // Set file ModTime to preserve original timestamp if err := os.Chtimes(filename, timestamp, timestamp); err != nil { return nil, err } return info, nil } // DeleteLTXFiles deletes LTX files. func (c *ReplicaClient) DeleteLTXFiles(ctx context.Context, a []*ltx.FileInfo) error { for _, info := range a { filename := c.LTXFilePath(info.Level, info.MinTXID, info.MaxTXID) c.logger.Debug("deleting ltx file", "level", info.Level, "minTXID", info.MinTXID, "maxTXID", info.MaxTXID, "path", filename) if err := os.Remove(filename); err != nil && !os.IsNotExist(err) { return err } } return nil } // DeleteAll deletes all LTX files. func (c *ReplicaClient) DeleteAll(ctx context.Context) error { if err := os.RemoveAll(c.path); err != nil && !os.IsNotExist(err) { return err } return nil } // GenerationsV3 returns a list of v0.3.x generation IDs in the replica. func (c *ReplicaClient) GenerationsV3(ctx context.Context) ([]string, error) { genPath := filepath.Join(c.path, litestream.GenerationsDirV3) entries, err := os.ReadDir(genPath) if os.IsNotExist(err) { return nil, nil } else if err != nil { return nil, err } var generations []string for _, entry := range entries { if entry.IsDir() && litestream.IsGenerationIDV3(entry.Name()) { generations = append(generations, entry.Name()) } } slices.Sort(generations) return generations, nil } // SnapshotsV3 returns snapshots for a generation, sorted by index. func (c *ReplicaClient) SnapshotsV3(ctx context.Context, generation string) ([]litestream.SnapshotInfoV3, error) { snapshotsPath := filepath.Join(c.path, litestream.GenerationsDirV3, generation, litestream.SnapshotsDirV3) entries, err := os.ReadDir(snapshotsPath) if os.IsNotExist(err) { return nil, nil } else if err != nil { return nil, err } var snapshots []litestream.SnapshotInfoV3 for _, entry := range entries { if entry.IsDir() { continue } index, err := litestream.ParseSnapshotFilenameV3(entry.Name()) if err != nil { continue // skip invalid filenames } info, err := entry.Info() if err != nil { return nil, err } snapshots = append(snapshots, litestream.SnapshotInfoV3{ Generation: generation, Index: index, Size: info.Size(), CreatedAt: info.ModTime(), }) } slices.SortFunc(snapshots, func(a, b litestream.SnapshotInfoV3) int { return a.Index - b.Index }) return snapshots, nil } // WALSegmentsV3 returns WAL segments for a generation, sorted by index then offset. func (c *ReplicaClient) WALSegmentsV3(ctx context.Context, generation string) ([]litestream.WALSegmentInfoV3, error) { walPath := filepath.Join(c.path, litestream.GenerationsDirV3, generation, litestream.WALDirV3) entries, err := os.ReadDir(walPath) if os.IsNotExist(err) { return nil, nil } else if err != nil { return nil, err } var segments []litestream.WALSegmentInfoV3 for _, entry := range entries { if entry.IsDir() { continue } index, offset, err := litestream.ParseWALSegmentFilenameV3(entry.Name()) if err != nil { continue // skip invalid filenames } info, err := entry.Info() if err != nil { return nil, err } segments = append(segments, litestream.WALSegmentInfoV3{ Generation: generation, Index: index, Offset: offset, Size: info.Size(), CreatedAt: info.ModTime(), }) } slices.SortFunc(segments, func(a, b litestream.WALSegmentInfoV3) int { if a.Index != b.Index { return a.Index - b.Index } return int(a.Offset - b.Offset) }) return segments, nil } // OpenSnapshotV3 opens a v0.3.x snapshot file for reading. // The returned reader provides LZ4-decompressed data. func (c *ReplicaClient) OpenSnapshotV3(ctx context.Context, generation string, index int) (io.ReadCloser, error) { path := filepath.Join(c.path, litestream.GenerationsDirV3, generation, litestream.SnapshotsDirV3, litestream.FormatSnapshotFilenameV3(index)) f, err := os.Open(path) if err != nil { return nil, err } return internal.NewLZ4Reader(f), nil } // OpenWALSegmentV3 opens a v0.3.x WAL segment file for reading. // The returned reader provides LZ4-decompressed data. func (c *ReplicaClient) OpenWALSegmentV3(ctx context.Context, generation string, index int, offset int64) (io.ReadCloser, error) { path := filepath.Join(c.path, litestream.GenerationsDirV3, generation, litestream.WALDirV3, litestream.FormatWALSegmentFilenameV3(index, offset)) f, err := os.Open(path) if err != nil { return nil, err } return internal.NewLZ4Reader(f), nil } ================================================ FILE: file/replica_client_test.go ================================================ package file_test import ( "bytes" "context" "fmt" "io" "os" "path/filepath" "strings" "testing" "time" "github.com/pierrec/lz4/v4" "github.com/superfly/ltx" "github.com/benbjohnson/litestream/file" ) func TestReplicaClient_Path(t *testing.T) { c := file.NewReplicaClient("/foo/bar") if got, want := c.Path(), "/foo/bar"; got != want { t.Fatalf("Path()=%v, want %v", got, want) } } func TestReplicaClient_Type(t *testing.T) { if got, want := file.NewReplicaClient("").Type(), "file"; got != want { t.Fatalf("Type()=%v, want %v", got, want) } } // TestReplicaClient_WriteLTXFile_ErrorCleanup verifies temp files are cleaned up on errors func TestReplicaClient_WriteLTXFile_ErrorCleanup(t *testing.T) { t.Run("DiskFull", func(t *testing.T) { tmpDir := t.TempDir() c := file.NewReplicaClient(tmpDir) // Create a reader that fails after 50 bytes to simulate disk full failReader := &failAfterReader{ data: createLTXHeader(1, 2), n: 50, err: fmt.Errorf("no space left on device"), } _, err := c.WriteLTXFile(context.Background(), 0, 1, 2, failReader) if err == nil { t.Fatal("expected error from failReader") } if !strings.Contains(err.Error(), "no space left on device") { t.Fatalf("expected disk full error, got: %v", err) } // Verify no .tmp files remain tmpFiles := findTmpFiles(t, tmpDir) if len(tmpFiles) > 0 { t.Fatalf("found %d .tmp files after error: %v", len(tmpFiles), tmpFiles) } }) t.Run("SuccessNoLeaks", func(t *testing.T) { tmpDir := t.TempDir() c := file.NewReplicaClient(tmpDir) ltxData := createLTXData(1, 2, []byte("test data")) info, err := c.WriteLTXFile(context.Background(), 0, 1, 2, bytes.NewReader(ltxData)) if err != nil { t.Fatal(err) } if info == nil { t.Fatal("expected FileInfo") } // Verify no .tmp files remain tmpFiles := findTmpFiles(t, tmpDir) if len(tmpFiles) > 0 { t.Fatalf("found %d .tmp files after successful write: %v", len(tmpFiles), tmpFiles) } // Verify final file exists finalPath := c.LTXFilePath(0, 1, 2) if _, err := os.Stat(finalPath); err != nil { t.Fatalf("final file missing: %v", err) } }) t.Run("MultipleErrors", func(t *testing.T) { tmpDir := t.TempDir() c := file.NewReplicaClient(tmpDir) // Simulate multiple failed writes for i := 0; i < 5; i++ { failReader := &failAfterReader{ data: createLTXHeader(ltx.TXID(i+1), ltx.TXID(i+1)), n: 30, err: fmt.Errorf("write error %d", i), } _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(i+1), ltx.TXID(i+1), failReader) if err == nil { t.Fatalf("iteration %d: expected error from failReader", i) } } // Verify no .tmp files accumulated tmpFiles := findTmpFiles(t, tmpDir) if len(tmpFiles) > 0 { t.Fatalf("found %d .tmp files after multiple errors: %v", len(tmpFiles), tmpFiles) } }) } // failAfterReader simulates io.Copy failure after reading n bytes type failAfterReader struct { data []byte n int // fail after n bytes pos int err error } func (r *failAfterReader) Read(p []byte) (n int, err error) { if r.pos >= r.n { return 0, r.err } remaining := r.n - r.pos toRead := len(p) if toRead > remaining { toRead = remaining } if toRead > len(r.data)-r.pos { toRead = len(r.data) - r.pos } if toRead == 0 { return 0, r.err } n = copy(p, r.data[r.pos:r.pos+toRead]) r.pos += n return n, nil } // findTmpFiles recursively finds all .tmp files in the directory func findTmpFiles(t *testing.T, root string) []string { t.Helper() var tmpFiles []string err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err != nil { return nil } if !info.IsDir() && strings.HasSuffix(path, ".tmp") { tmpFiles = append(tmpFiles, path) } return nil }) if err != nil { t.Fatalf("walk error: %v", err) } return tmpFiles } // createLTXData creates a minimal valid LTX file with a header for testing func createLTXData(minTXID, maxTXID ltx.TXID, data []byte) []byte { hdr := ltx.Header{ Version: ltx.Version, PageSize: 4096, Commit: 1, MinTXID: minTXID, MaxTXID: maxTXID, Timestamp: time.Now().UnixMilli(), } if minTXID == 1 { hdr.PreApplyChecksum = 0 } else { hdr.PreApplyChecksum = ltx.ChecksumFlag } headerBytes, _ := hdr.MarshalBinary() return append(headerBytes, data...) } // createLTXHeader creates minimal LTX header for testing func createLTXHeader(minTXID, maxTXID ltx.TXID) []byte { return createLTXData(minTXID, maxTXID, nil) } func TestReplicaClient_GenerationsV3(t *testing.T) { t.Run("NoGenerationsDir", func(t *testing.T) { tmpDir := t.TempDir() c := file.NewReplicaClient(tmpDir) generations, err := c.GenerationsV3(context.Background()) if err != nil { t.Fatal(err) } if len(generations) != 0 { t.Fatalf("expected no generations, got %v", generations) } }) t.Run("EmptyGenerationsDir", func(t *testing.T) { tmpDir := t.TempDir() if err := os.MkdirAll(filepath.Join(tmpDir, "generations"), 0755); err != nil { t.Fatal(err) } c := file.NewReplicaClient(tmpDir) generations, err := c.GenerationsV3(context.Background()) if err != nil { t.Fatal(err) } if len(generations) != 0 { t.Fatalf("expected no generations, got %v", generations) } }) t.Run("MultipleGenerationsSorted", func(t *testing.T) { tmpDir := t.TempDir() genDir := filepath.Join(tmpDir, "generations") // Create in non-sorted order for _, gen := range []string{"ffffffffffffffff", "0000000000000001", "aaaaaaaaaaaaaaaa"} { if err := os.MkdirAll(filepath.Join(genDir, gen), 0755); err != nil { t.Fatal(err) } } c := file.NewReplicaClient(tmpDir) generations, err := c.GenerationsV3(context.Background()) if err != nil { t.Fatal(err) } want := []string{"0000000000000001", "aaaaaaaaaaaaaaaa", "ffffffffffffffff"} if len(generations) != len(want) { t.Fatalf("got %d generations, want %d", len(generations), len(want)) } for i, g := range generations { if g != want[i] { t.Errorf("generations[%d] = %q, want %q", i, g, want[i]) } } }) t.Run("SkipsInvalidIDs", func(t *testing.T) { tmpDir := t.TempDir() genDir := filepath.Join(tmpDir, "generations") // Create mix of valid and invalid for _, name := range []string{ "0000000000000001", // valid "invalid", // invalid - not hex "0123456789abcde", // invalid - 15 chars "0123456789ABCDEF", // invalid - uppercase "aaaaaaaaaaaaaaaa", // valid } { if err := os.MkdirAll(filepath.Join(genDir, name), 0755); err != nil { t.Fatal(err) } } c := file.NewReplicaClient(tmpDir) generations, err := c.GenerationsV3(context.Background()) if err != nil { t.Fatal(err) } want := []string{"0000000000000001", "aaaaaaaaaaaaaaaa"} if len(generations) != len(want) { t.Fatalf("got %d generations, want %d: %v", len(generations), len(want), generations) } for i, g := range generations { if g != want[i] { t.Errorf("generations[%d] = %q, want %q", i, g, want[i]) } } }) t.Run("SkipsFiles", func(t *testing.T) { tmpDir := t.TempDir() genDir := filepath.Join(tmpDir, "generations") if err := os.MkdirAll(genDir, 0755); err != nil { t.Fatal(err) } // Create a file with valid generation name (should be skipped) if err := os.WriteFile(filepath.Join(genDir, "0000000000000001"), []byte("test"), 0644); err != nil { t.Fatal(err) } // Create a valid directory if err := os.MkdirAll(filepath.Join(genDir, "aaaaaaaaaaaaaaaa"), 0755); err != nil { t.Fatal(err) } c := file.NewReplicaClient(tmpDir) generations, err := c.GenerationsV3(context.Background()) if err != nil { t.Fatal(err) } if len(generations) != 1 || generations[0] != "aaaaaaaaaaaaaaaa" { t.Fatalf("expected only valid directory, got %v", generations) } }) } func TestReplicaClient_SnapshotsV3(t *testing.T) { gen := "0123456789abcdef" t.Run("NoSnapshotsDir", func(t *testing.T) { tmpDir := t.TempDir() c := file.NewReplicaClient(tmpDir) snapshots, err := c.SnapshotsV3(context.Background(), gen) if err != nil { t.Fatal(err) } if len(snapshots) != 0 { t.Fatalf("expected no snapshots, got %v", snapshots) } }) t.Run("EmptySnapshotsDir", func(t *testing.T) { tmpDir := t.TempDir() if err := os.MkdirAll(filepath.Join(tmpDir, "generations", gen, "snapshots"), 0755); err != nil { t.Fatal(err) } c := file.NewReplicaClient(tmpDir) snapshots, err := c.SnapshotsV3(context.Background(), gen) if err != nil { t.Fatal(err) } if len(snapshots) != 0 { t.Fatalf("expected no snapshots, got %v", snapshots) } }) t.Run("SingleSnapshot", func(t *testing.T) { tmpDir := t.TempDir() snapshotsDir := filepath.Join(tmpDir, "generations", gen, "snapshots") if err := os.MkdirAll(snapshotsDir, 0755); err != nil { t.Fatal(err) } // Create snapshot file path := filepath.Join(snapshotsDir, "00000001.snapshot.lz4") if err := os.WriteFile(path, []byte("test data"), 0644); err != nil { t.Fatal(err) } c := file.NewReplicaClient(tmpDir) snapshots, err := c.SnapshotsV3(context.Background(), gen) if err != nil { t.Fatal(err) } if len(snapshots) != 1 { t.Fatalf("expected 1 snapshot, got %d", len(snapshots)) } if snapshots[0].Generation != gen { t.Errorf("Generation = %q, want %q", snapshots[0].Generation, gen) } if snapshots[0].Index != 1 { t.Errorf("Index = %d, want 1", snapshots[0].Index) } if snapshots[0].Size != 9 { // len("test data") t.Errorf("Size = %d, want 9", snapshots[0].Size) } }) t.Run("MultipleSnapshotsSorted", func(t *testing.T) { tmpDir := t.TempDir() snapshotsDir := filepath.Join(tmpDir, "generations", gen, "snapshots") if err := os.MkdirAll(snapshotsDir, 0755); err != nil { t.Fatal(err) } // Create snapshots in non-sorted order for _, idx := range []int{0xff, 0x01, 0x10} { filename := fmt.Sprintf("%08x.snapshot.lz4", idx) if err := os.WriteFile(filepath.Join(snapshotsDir, filename), []byte("x"), 0644); err != nil { t.Fatal(err) } } c := file.NewReplicaClient(tmpDir) snapshots, err := c.SnapshotsV3(context.Background(), gen) if err != nil { t.Fatal(err) } if len(snapshots) != 3 { t.Fatalf("expected 3 snapshots, got %d", len(snapshots)) } wantIndices := []int{0x01, 0x10, 0xff} for i, s := range snapshots { if s.Index != wantIndices[i] { t.Errorf("snapshots[%d].Index = %d, want %d", i, s.Index, wantIndices[i]) } } }) t.Run("SkipsInvalidFilenames", func(t *testing.T) { tmpDir := t.TempDir() snapshotsDir := filepath.Join(tmpDir, "generations", gen, "snapshots") if err := os.MkdirAll(snapshotsDir, 0755); err != nil { t.Fatal(err) } // Create mix of valid and invalid files := map[string]bool{ "00000001.snapshot.lz4": true, // valid "invalid.snapshot.lz4": false, // invalid "00000002.snapshot": false, // missing .lz4 "00000003.wal.lz4": false, // wrong type "00000004.snapshot.lz4": true, // valid } for name := range files { if err := os.WriteFile(filepath.Join(snapshotsDir, name), []byte("x"), 0644); err != nil { t.Fatal(err) } } c := file.NewReplicaClient(tmpDir) snapshots, err := c.SnapshotsV3(context.Background(), gen) if err != nil { t.Fatal(err) } if len(snapshots) != 2 { t.Fatalf("expected 2 valid snapshots, got %d", len(snapshots)) } }) } func TestReplicaClient_WALSegmentsV3(t *testing.T) { gen := "0123456789abcdef" t.Run("NoWALDir", func(t *testing.T) { tmpDir := t.TempDir() c := file.NewReplicaClient(tmpDir) segments, err := c.WALSegmentsV3(context.Background(), gen) if err != nil { t.Fatal(err) } if len(segments) != 0 { t.Fatalf("expected no segments, got %v", segments) } }) t.Run("EmptyWALDir", func(t *testing.T) { tmpDir := t.TempDir() if err := os.MkdirAll(filepath.Join(tmpDir, "generations", gen, "wal"), 0755); err != nil { t.Fatal(err) } c := file.NewReplicaClient(tmpDir) segments, err := c.WALSegmentsV3(context.Background(), gen) if err != nil { t.Fatal(err) } if len(segments) != 0 { t.Fatalf("expected no segments, got %v", segments) } }) t.Run("SingleSegment", func(t *testing.T) { tmpDir := t.TempDir() walDir := filepath.Join(tmpDir, "generations", gen, "wal") if err := os.MkdirAll(walDir, 0755); err != nil { t.Fatal(err) } // Create WAL segment file path := filepath.Join(walDir, "00000001_00001000.wal.lz4") if err := os.WriteFile(path, []byte("wal data"), 0644); err != nil { t.Fatal(err) } c := file.NewReplicaClient(tmpDir) segments, err := c.WALSegmentsV3(context.Background(), gen) if err != nil { t.Fatal(err) } if len(segments) != 1 { t.Fatalf("expected 1 segment, got %d", len(segments)) } if segments[0].Generation != gen { t.Errorf("Generation = %q, want %q", segments[0].Generation, gen) } if segments[0].Index != 1 { t.Errorf("Index = %d, want 1", segments[0].Index) } if segments[0].Offset != 0x1000 { t.Errorf("Offset = %d, want %d", segments[0].Offset, 0x1000) } if segments[0].Size != 8 { // len("wal data") t.Errorf("Size = %d, want 8", segments[0].Size) } }) t.Run("MultipleSortedByIndexThenOffset", func(t *testing.T) { tmpDir := t.TempDir() walDir := filepath.Join(tmpDir, "generations", gen, "wal") if err := os.MkdirAll(walDir, 0755); err != nil { t.Fatal(err) } // Create WAL segments in non-sorted order segments := []struct { index int offset int64 }{ {2, 0x2000}, {1, 0x1000}, {1, 0x0000}, {2, 0x0000}, } for _, s := range segments { filename := fmt.Sprintf("%08x_%08x.wal.lz4", s.index, s.offset) if err := os.WriteFile(filepath.Join(walDir, filename), []byte("x"), 0644); err != nil { t.Fatal(err) } } c := file.NewReplicaClient(tmpDir) result, err := c.WALSegmentsV3(context.Background(), gen) if err != nil { t.Fatal(err) } if len(result) != 4 { t.Fatalf("expected 4 segments, got %d", len(result)) } // Verify sorted order: index 1 offset 0, index 1 offset 0x1000, index 2 offset 0, index 2 offset 0x2000 expected := []struct { index int offset int64 }{ {1, 0x0000}, {1, 0x1000}, {2, 0x0000}, {2, 0x2000}, } for i, s := range result { if s.Index != expected[i].index || s.Offset != expected[i].offset { t.Errorf("segments[%d] = (%d, %d), want (%d, %d)", i, s.Index, s.Offset, expected[i].index, expected[i].offset) } } }) t.Run("SkipsInvalidFilenames", func(t *testing.T) { tmpDir := t.TempDir() walDir := filepath.Join(tmpDir, "generations", gen, "wal") if err := os.MkdirAll(walDir, 0755); err != nil { t.Fatal(err) } // Create mix of valid and invalid files := []string{ "00000001_00000000.wal.lz4", // valid "invalid.wal.lz4", // invalid "00000002.wal.lz4", // missing offset "00000003_00001000.wal", // missing .lz4 "00000004_00002000.wal.lz4", // valid } for _, name := range files { if err := os.WriteFile(filepath.Join(walDir, name), []byte("x"), 0644); err != nil { t.Fatal(err) } } c := file.NewReplicaClient(tmpDir) result, err := c.WALSegmentsV3(context.Background(), gen) if err != nil { t.Fatal(err) } if len(result) != 2 { t.Fatalf("expected 2 valid segments, got %d", len(result)) } }) } func TestReplicaClient_OpenSnapshotV3(t *testing.T) { gen := "0123456789abcdef" t.Run("NotFound", func(t *testing.T) { tmpDir := t.TempDir() c := file.NewReplicaClient(tmpDir) _, err := c.OpenSnapshotV3(context.Background(), gen, 0) if !os.IsNotExist(err) { t.Errorf("expected not exist error, got %v", err) } }) t.Run("ReadDecompressed", func(t *testing.T) { tmpDir := t.TempDir() snapshotsDir := filepath.Join(tmpDir, "generations", gen, "snapshots") if err := os.MkdirAll(snapshotsDir, 0755); err != nil { t.Fatal(err) } // Create LZ4-compressed test data original := []byte("test snapshot data for decompression") var buf bytes.Buffer w := lz4.NewWriter(&buf) if _, err := w.Write(original); err != nil { t.Fatal(err) } if err := w.Close(); err != nil { t.Fatal(err) } // Write compressed file path := filepath.Join(snapshotsDir, "00000000.snapshot.lz4") if err := os.WriteFile(path, buf.Bytes(), 0644); err != nil { t.Fatal(err) } c := file.NewReplicaClient(tmpDir) r, err := c.OpenSnapshotV3(context.Background(), gen, 0) if err != nil { t.Fatal(err) } defer r.Close() data, err := io.ReadAll(r) if err != nil { t.Fatal(err) } if !bytes.Equal(data, original) { t.Errorf("decompressed data mismatch: got %q, want %q", data, original) } }) } func TestReplicaClient_OpenWALSegmentV3(t *testing.T) { gen := "0123456789abcdef" t.Run("NotFound", func(t *testing.T) { tmpDir := t.TempDir() c := file.NewReplicaClient(tmpDir) _, err := c.OpenWALSegmentV3(context.Background(), gen, 0, 0) if !os.IsNotExist(err) { t.Errorf("expected not exist error, got %v", err) } }) t.Run("ReadDecompressed", func(t *testing.T) { tmpDir := t.TempDir() walDir := filepath.Join(tmpDir, "generations", gen, "wal") if err := os.MkdirAll(walDir, 0755); err != nil { t.Fatal(err) } // Create LZ4-compressed test data original := []byte("test WAL segment data") var buf bytes.Buffer w := lz4.NewWriter(&buf) if _, err := w.Write(original); err != nil { t.Fatal(err) } if err := w.Close(); err != nil { t.Fatal(err) } // Write compressed file path := filepath.Join(walDir, "00000001_00001000.wal.lz4") if err := os.WriteFile(path, buf.Bytes(), 0644); err != nil { t.Fatal(err) } c := file.NewReplicaClient(tmpDir) r, err := c.OpenWALSegmentV3(context.Background(), gen, 1, 4096) if err != nil { t.Fatal(err) } defer r.Close() data, err := io.ReadAll(r) if err != nil { t.Fatal(err) } if !bytes.Equal(data, original) { t.Errorf("decompressed data mismatch: got %q, want %q", data, original) } }) } /* func TestReplica_Sync(t *testing.T) { // Ensure replica can successfully sync after DB has sync'd. t.Run("InitialSync", func(t *testing.T) { db, sqldb := MustOpenDBs(t) defer MustCloseDBs(t, db, sqldb) r := litestream.NewReplica(db, "", file.NewReplicaClient(t.TempDir())) r.MonitorEnabled = false db.Replicas = []*litestream.Replica{r} // Sync database & then sync replica. if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } else if err := r.Sync(context.Background()); err != nil { t.Fatal(err) } // Ensure posistions match. if want, err := db.Pos(); err != nil { t.Fatal(err) } else if got, err := r.Pos(context.Background()); err != nil { t.Fatal(err) } else if got != want { t.Fatalf("Pos()=%v, want %v", got, want) } }) // Ensure replica can successfully sync multiple times. t.Run("MultiSync", func(t *testing.T) { db, sqldb := MustOpenDBs(t) defer MustCloseDBs(t, db, sqldb) r := litestream.NewReplica(db, "", file.NewReplicaClient(t.TempDir())) r.MonitorEnabled = false db.Replicas = []*litestream.Replica{r} if _, err := sqldb.Exec(`CREATE TABLE foo (bar TEXT);`); err != nil { t.Fatal(err) } // Write to the database multiple times and sync after each write. for i, n := 0, db.MinCheckpointPageN*2; i < n; i++ { if _, err := sqldb.Exec(`INSERT INTO foo (bar) VALUES ('baz')`); err != nil { t.Fatal(err) } // Sync periodically. if i%100 == 0 || i == n-1 { if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } else if err := r.Sync(context.Background()); err != nil { t.Fatal(err) } } } // Ensure posistions match. pos, err := db.Pos() if err != nil { t.Fatal(err) } else if got, want := pos.Index, 2; got != want { t.Fatalf("Index=%v, want %v", got, want) } if want, err := r.Pos(context.Background()); err != nil { t.Fatal(err) } else if got := pos; got != want { t.Fatalf("Pos()=%v, want %v", got, want) } }) // Ensure replica returns an error if there is no generation available from the DB. t.Run("ErrNoGeneration", func(t *testing.T) { db, sqldb := MustOpenDBs(t) defer MustCloseDBs(t, db, sqldb) r := litestream.NewReplica(db, "", file.NewReplicaClient(t.TempDir())) r.MonitorEnabled = false db.Replicas = []*litestream.Replica{r} if err := r.Sync(context.Background()); err == nil || err.Error() != `no generation, waiting for data` { t.Fatal(err) } }) } */ ================================================ FILE: go.mod ================================================ module github.com/benbjohnson/litestream go 1.25.0 toolchain go1.25.8 require ( cloud.google.com/go/storage v1.36.0 github.com/Azure/azure-sdk-for-go/sdk/azcore v1.18.2 github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.11.0 github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.2 github.com/MadAppGang/httplog v1.3.0 github.com/aws/aws-sdk-go-v2 v1.41.0 github.com/aws/aws-sdk-go-v2/config v1.32.6 github.com/aws/aws-sdk-go-v2/credentials v1.19.6 github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.20.18 github.com/aws/aws-sdk-go-v2/service/s3 v1.95.0 github.com/aws/smithy-go v1.24.0 github.com/dustin/go-humanize v1.0.1 github.com/fsouza/fake-gcs-server v1.47.3 github.com/hashicorp/golang-lru/v2 v2.0.7 github.com/mark3labs/mcp-go v0.32.0 github.com/mattn/go-shellwords v1.0.12 github.com/mattn/go-sqlite3 v1.14.19 github.com/nats-io/nats.go v1.44.0 github.com/pkg/sftp v1.13.6 github.com/prometheus/client_golang v1.17.0 github.com/psanford/sqlite3vfs v0.0.0-20251127171934-4e34e03a991a // direct github.com/studio-b12/gowebdav v0.11.0 github.com/superfly/ltx v0.5.1 golang.org/x/crypto v0.45.0 golang.org/x/sys v0.38.0 google.golang.org/api v0.154.0 gopkg.in/yaml.v2 v2.4.0 modernc.org/sqlite v1.44.3 ) require ( cloud.google.com/go/pubsub v1.33.0 // indirect github.com/fsnotify/fsnotify v1.7.0 github.com/google/renameio/v2 v2.0.0 // indirect github.com/gorilla/handlers v1.5.1 // indirect github.com/gorilla/mux v1.8.0 // indirect github.com/ncruces/go-strftime v1.0.0 // indirect github.com/pkg/xattr v0.4.9 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/sirupsen/logrus v1.9.3 // indirect golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 // indirect golang.org/x/sync v0.18.0 modernc.org/libc v1.67.6 // indirect modernc.org/mathutil v1.7.1 // indirect modernc.org/memory v1.11.0 // indirect ) require ( github.com/aliyun/alibabacloud-oss-go-sdk-v2 v1.3.0 github.com/markusmobius/go-dateparser v1.2.4 ) require ( github.com/pierrec/lz4/v4 v4.1.22 github.com/stretchr/testify v1.11.1 golang.org/x/crypto/x509roots/fallback v0.0.0-20260209214922-2f26647a795e ) require ( cloud.google.com/go v0.111.0 // indirect cloud.google.com/go/compute/metadata v0.3.0 // indirect cloud.google.com/go/iam v1.1.5 // indirect github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2 // indirect github.com/AzureAD/microsoft-authentication-library-for-go v1.4.2 // indirect github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 // indirect github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.4 // indirect github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.16 // indirect github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.16 // indirect github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.16 // indirect github.com/aws/aws-sdk-go-v2/internal/ini v1.8.4 // indirect github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.16 // indirect github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.4 // indirect github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.9.7 // indirect github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.16 // indirect github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.16 // indirect github.com/aws/aws-sdk-go-v2/service/signin v1.0.4 // indirect github.com/aws/aws-sdk-go-v2/service/sso v1.30.8 // indirect github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.12 // indirect github.com/aws/aws-sdk-go-v2/service/sts v1.41.5 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/fatih/color v1.13.0 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect github.com/go-logr/logr v1.3.0 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/golang-jwt/jwt/v5 v5.3.0 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/s2a-go v0.1.7 // indirect github.com/google/uuid v1.6.0 // indirect github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect github.com/googleapis/gax-go/v2 v2.12.0 // indirect github.com/hablullah/go-hijri v1.0.2 // indirect github.com/hablullah/go-juliandays v1.0.0 // indirect github.com/jalaali/go-jalaali v0.0.0-20210801064154-80525e88d958 // indirect github.com/klauspost/compress v1.18.0 // indirect github.com/kr/fs v0.1.0 // indirect github.com/kylelemons/godebug v1.1.0 // indirect github.com/magefile/mage v1.14.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect github.com/nats-io/nkeys v0.4.11 // indirect github.com/nats-io/nuid v1.0.1 // indirect github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_model v0.5.0 // indirect github.com/prometheus/common v0.45.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect github.com/spf13/cast v1.7.1 // indirect github.com/tetratelabs/wazero v1.2.1 // indirect github.com/wasilibs/go-re2 v1.3.0 // indirect github.com/yosida95/uritemplate/v3 v3.0.2 // indirect go.opencensus.io v0.24.0 // indirect go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect go.opentelemetry.io/otel v1.21.0 // indirect go.opentelemetry.io/otel/metric v1.21.0 // indirect go.opentelemetry.io/otel/trace v1.21.0 // indirect golang.org/x/net v0.47.0 // indirect golang.org/x/oauth2 v0.27.0 // indirect golang.org/x/text v0.31.0 // indirect golang.org/x/time v0.5.0 // indirect golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect google.golang.org/genproto v0.0.0-20231212172506-995d672761c0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 // indirect google.golang.org/grpc v1.60.1 // indirect google.golang.org/protobuf v1.33.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) ================================================ FILE: go.sum ================================================ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.111.0 h1:YHLKNupSD1KqjDbQ3+LVdQ81h/UJbJyZG203cEfnQgM= cloud.google.com/go v0.111.0/go.mod h1:0mibmpKP1TyOOFYQY5izo0LnT+ecvOQ0Sg3OdmMiNRU= cloud.google.com/go/compute/metadata v0.3.0 h1:Tz+eQXMEqDIKRsmY3cHTL6FVaynIjX2QxYC4trgAKZc= cloud.google.com/go/compute/metadata v0.3.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k= cloud.google.com/go/iam v1.1.5 h1:1jTsCu4bcsNsE4iiqNT5SHwrDRCfRmIaaaVFhRveTJI= cloud.google.com/go/iam v1.1.5/go.mod h1:rB6P/Ic3mykPbFio+vo7403drjlgvoWfYpJhMXEbzv8= cloud.google.com/go/kms v1.15.5 h1:pj1sRfut2eRbD9pFRjNnPNg/CzJPuQAzUujMIM1vVeM= cloud.google.com/go/kms v1.15.5/go.mod h1:cU2H5jnp6G2TDpUGZyqTCoy1n16fbubHZjmVXSMtwDI= cloud.google.com/go/pubsub v1.33.0 h1:6SPCPvWav64tj0sVX/+npCBKhUi/UjJehy9op/V3p2g= cloud.google.com/go/pubsub v1.33.0/go.mod h1:f+w71I33OMyxf9VpMVcZbnG5KSUkCOUHYpFd5U1GdRc= cloud.google.com/go/storage v1.36.0 h1:P0mOkAcaJxhCTvAkMhxMfrTKiNcub4YmmPBtlhAyTr8= cloud.google.com/go/storage v1.36.0/go.mod h1:M6M/3V/D3KpzMTJyPOR/HU6n2Si5QdaXYEsng2xgOs8= github.com/Azure/azure-sdk-for-go/sdk/azcore v1.18.2 h1:Hr5FTipp7SL07o2FvoVOX9HRiRH3CR3Mj8pxqCcdD5A= github.com/Azure/azure-sdk-for-go/sdk/azcore v1.18.2/go.mod h1:QyVsSSN64v5TGltphKLQ2sQxe4OBQg0J1eKRcVBnfgE= github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.11.0 h1:MhRfI58HblXzCtWEZCO0feHs8LweePB3s90r7WaR1KU= github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.11.0/go.mod h1:okZ+ZURbArNdlJ+ptXoyHNuOETzOl1Oww19rm8I2WLA= github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.3.2 h1:yz1bePFlP5Vws5+8ez6T3HWXPmwOK7Yvq8QxDBD3SKY= github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.3.2/go.mod h1:Pa9ZNPuoNu/GztvBSKk9J1cDJW6vk/n0zLtV4mgd8N8= github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2 h1:9iefClla7iYpfYWdzPCRDozdmndjTm8DXdpCzPajMgA= github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2/go.mod h1:XtLgD3ZD34DAaVIIAyG3objl5DynM3CQ/vMcbBNJZGI= github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.8.1 h1:/Zt+cDPnpC3OVDm/JKLOs7M2DKmLRIIp3XIx9pHHiig= github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.8.1/go.mod h1:Ng3urmn6dYe8gnbCMoHHVl5APYz2txho3koEkV2o2HA= github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.2 h1:FwladfywkNirM+FZYLBR2kBz5C8Tg0fw5w5Y7meRXWI= github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.2/go.mod h1:vv5Ad0RrIoT1lJFdWBZwt4mB1+j+V8DUroixmKDTCdk= github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1 h1:WJTmL004Abzc5wDB5VtZG2PJk5ndYDgVacGqfirKxjM= github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1/go.mod h1:tCcJZ0uHAmvjsVYzEFivsRTN00oz5BEsRgQHu5JZ9WE= github.com/AzureAD/microsoft-authentication-library-for-go v1.4.2 h1:oygO0locgZJe7PpYPXT5A29ZkwJaPqcva7BVeemZOZs= github.com/AzureAD/microsoft-authentication-library-for-go v1.4.2/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/MadAppGang/httplog v1.3.0 h1:1XU54TO8kiqTeO+7oZLKAM3RP/cJ7SadzslRcKspVHo= github.com/MadAppGang/httplog v1.3.0/go.mod h1:gpYEdkjh/Cda6YxtDy4AB7KY+fR7mb3SqBZw74A5hJ4= github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4= github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w= github.com/aliyun/alibabacloud-oss-go-sdk-v2 v1.3.0 h1:wQlqotpyjYPjJz+Noh5bRu7Snmydk8SKC5Z6u1CR20Y= github.com/aliyun/alibabacloud-oss-go-sdk-v2 v1.3.0/go.mod h1:FTzydeQVmR24FI0D6XWUOMKckjXehM/jgMn1xC+DA9M= github.com/aws/aws-sdk-go-v2 v1.41.0 h1:tNvqh1s+v0vFYdA1xq0aOJH+Y5cRyZ5upu6roPgPKd4= github.com/aws/aws-sdk-go-v2 v1.41.0/go.mod h1:MayyLB8y+buD9hZqkCW3kX1AKq07Y5pXxtgB+rRFhz0= github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.4 h1:489krEF9xIGkOaaX3CE/Be2uWjiXrkCH6gUX+bZA/BU= github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.4/go.mod h1:IOAPF6oT9KCsceNTvvYMNHy0+kMF8akOjeDvPENWxp4= github.com/aws/aws-sdk-go-v2/config v1.32.6 h1:hFLBGUKjmLAekvi1evLi5hVvFQtSo3GYwi+Bx4lpJf8= github.com/aws/aws-sdk-go-v2/config v1.32.6/go.mod h1:lcUL/gcd8WyjCrMnxez5OXkO3/rwcNmvfno62tnXNcI= github.com/aws/aws-sdk-go-v2/credentials v1.19.6 h1:F9vWao2TwjV2MyiyVS+duza0NIRtAslgLUM0vTA1ZaE= github.com/aws/aws-sdk-go-v2/credentials v1.19.6/go.mod h1:SgHzKjEVsdQr6Opor0ihgWtkWdfRAIwxYzSJ8O85VHY= github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.16 h1:80+uETIWS1BqjnN9uJ0dBUaETh+P1XwFy5vwHwK5r9k= github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.16/go.mod h1:wOOsYuxYuB/7FlnVtzeBYRcjSRtQpAW0hCP7tIULMwo= github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.20.18 h1:9vWXHtaepwoAl/UuKzxwgOoJDXPCC3hvgNMfcmdS2Tk= github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.20.18/go.mod h1:sKuUZ+MwUTuJbYvZ8pK0x10LvgcJK3Y4rmh63YBekwk= github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.16 h1:rgGwPzb82iBYSvHMHXc8h9mRoOUBZIGFgKb9qniaZZc= github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.16/go.mod h1:L/UxsGeKpGoIj6DxfhOWHWQ/kGKcd4I1VncE4++IyKA= github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.16 h1:1jtGzuV7c82xnqOVfx2F0xmJcOw5374L7N6juGW6x6U= github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.16/go.mod h1:M2E5OQf+XLe+SZGmmpaI2yy+J326aFf6/+54PoxSANc= github.com/aws/aws-sdk-go-v2/internal/ini v1.8.4 h1:WKuaxf++XKWlHWu9ECbMlha8WOEGm0OUEZqm4K/Gcfk= github.com/aws/aws-sdk-go-v2/internal/ini v1.8.4/go.mod h1:ZWy7j6v1vWGmPReu0iSGvRiise4YI5SkR3OHKTZ6Wuc= github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.16 h1:CjMzUs78RDDv4ROu3JnJn/Ig1r6ZD7/T2DXLLRpejic= github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.16/go.mod h1:uVW4OLBqbJXSHJYA9svT9BluSvvwbzLQ2Crf6UPzR3c= github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.4 h1:0ryTNEdJbzUCEWkVXEXoqlXV72J5keC1GvILMOuD00E= github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.4/go.mod h1:HQ4qwNZh32C3CBeO6iJLQlgtMzqeG17ziAA/3KDJFow= github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.9.7 h1:DIBqIrJ7hv+e4CmIk2z3pyKT+3B6qVMgRsawHiR3qso= github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.9.7/go.mod h1:vLm00xmBke75UmpNvOcZQ/Q30ZFjbczeLFqGx5urmGo= github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.16 h1:oHjJHeUy0ImIV0bsrX0X91GkV5nJAyv1l1CC9lnO0TI= github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.16/go.mod h1:iRSNGgOYmiYwSCXxXaKb9HfOEj40+oTKn8pTxMlYkRM= github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.16 h1:NSbvS17MlI2lurYgXnCOLvCFX38sBW4eiVER7+kkgsU= github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.16/go.mod h1:SwT8Tmqd4sA6G1qaGdzWCJN99bUmPGHfRwwq3G5Qb+A= github.com/aws/aws-sdk-go-v2/service/s3 v1.95.0 h1:MIWra+MSq53CFaXXAywB2qg9YvVZifkk6vEGl/1Qor0= github.com/aws/aws-sdk-go-v2/service/s3 v1.95.0/go.mod h1:79S2BdqCJpScXZA2y+cpZuocWsjGjJINyXnOsf5DTz8= github.com/aws/aws-sdk-go-v2/service/signin v1.0.4 h1:HpI7aMmJ+mm1wkSHIA2t5EaFFv5EFYXePW30p1EIrbQ= github.com/aws/aws-sdk-go-v2/service/signin v1.0.4/go.mod h1:C5RdGMYGlfM0gYq/tifqgn4EbyX99V15P2V3R+VHbQU= github.com/aws/aws-sdk-go-v2/service/sso v1.30.8 h1:aM/Q24rIlS3bRAhTyFurowU8A0SMyGDtEOY/l/s/1Uw= github.com/aws/aws-sdk-go-v2/service/sso v1.30.8/go.mod h1:+fWt2UHSb4kS7Pu8y+BMBvJF0EWx+4H0hzNwtDNRTrg= github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.12 h1:AHDr0DaHIAo8c9t1emrzAlVDFp+iMMKnPdYy6XO4MCE= github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.12/go.mod h1:GQ73XawFFiWxyWXMHWfhiomvP3tXtdNar/fi8z18sx0= github.com/aws/aws-sdk-go-v2/service/sts v1.41.5 h1:SciGFVNZ4mHdm7gpD1dgZYnCuVdX1s+lFTg4+4DOy70= github.com/aws/aws-sdk-go-v2/service/sts v1.41.5/go.mod h1:iW40X4QBmUxdP+fZNOpfmkdMZqsovezbAeO+Ubiv2pk= github.com/aws/smithy-go v1.24.0 h1:LpilSUItNPFr1eY85RYgTIg5eIEPtvFbskaFcmmIUnk= github.com/aws/smithy-go v1.24.0/go.mod h1:LEj2LM3rBRQJxPZTB4KuzZkaZYnZPnvgIhb4pu07mx0= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+gqO04wryn5h75LSazbRlnya1k= github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/envoyproxy/protoc-gen-validate v1.0.2 h1:QkIBuU5k+x7/QXPvPPnWXWlCdaBFApVqftFV6k087DA= github.com/envoyproxy/protoc-gen-validate v1.0.2/go.mod h1:GpiZQP3dDbg4JouG/NNS7QWXpgx6x8QiMKdmN72jogE= github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/fsouza/fake-gcs-server v1.47.3 h1:L1mZfEd7cU8hnoHU2Rch9bq9HPS0CR0oySZdGMHygU4= github.com/fsouza/fake-gcs-server v1.47.3/go.mod h1:vqUZbI12uy9IkRQ54Q4p5AniQsSiUq8alO9Nv2egMmA= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo= github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/martian/v3 v3.3.2 h1:IqNFLAmvJOgVlpdEBiQbDc2EwKW77amAycfTuWKdfvw= github.com/google/martian/v3 v3.3.2/go.mod h1:oBOf6HBosgwRXnUGWUB05QECsc6uvmMiJ3+6W4l/CUk= github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs= github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA= github.com/google/renameio/v2 v2.0.0 h1:UifI23ZTGY8Tt29JbYFiuyIU3eX+RNFtUwefq9qAhxg= github.com/google/renameio/v2 v2.0.0/go.mod h1:BtmJXm5YlszgC+TD4HOEEUFgkJP3nLxehU6hfe7jRt4= github.com/google/s2a-go v0.1.7 h1:60BLSyTrOV4/haCDW4zb1guZItoSq8foHCXrAnjBo/o= github.com/google/s2a-go v0.1.7/go.mod h1:50CgR4k1jNlWBu4UfS4AcfhVe1r6pdZPygJ3R8F0Qdw= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/enterprise-certificate-proxy v0.3.2 h1:Vie5ybvEvT75RniqhfFxPRy3Bf7vr3h0cechB90XaQs= github.com/googleapis/enterprise-certificate-proxy v0.3.2/go.mod h1:VLSiSSBs/ksPL8kq3OBOQ6WRI2QnaFynd1DCjZ62+V0= github.com/googleapis/gax-go/v2 v2.12.0 h1:A+gCJKdRfqXkr+BIRGtZLibNXf0m1f9E4HG56etFpas= github.com/googleapis/gax-go/v2 v2.12.0/go.mod h1:y+aIqrI5eb1YGMVJfuV3185Ts/D7qKpsEkdD5+I6QGU= github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4= github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/hablullah/go-hijri v1.0.2 h1:drT/MZpSZJQXo7jftf5fthArShcaMtsal0Zf/dnmp6k= github.com/hablullah/go-hijri v1.0.2/go.mod h1:OS5qyYLDjORXzK4O1adFw9Q5WfhOcMdAKglDkcTxgWQ= github.com/hablullah/go-juliandays v1.0.0 h1:A8YM7wIj16SzlKT0SRJc9CD29iiaUzpBLzh5hr0/5p0= github.com/hablullah/go-juliandays v1.0.0/go.mod h1:0JOYq4oFOuDja+oospuc61YoX+uNEn7Z6uHYTbBzdGc= github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f h1:7LYC+Yfkj3CTRcShK0KOL/w6iTiKyqqBA9a41Wnggw8= github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI= github.com/jalaali/go-jalaali v0.0.0-20210801064154-80525e88d958 h1:qxLoi6CAcXVzjfvu+KXIXJOAsQB62LXjsfbOaErsVzE= github.com/jalaali/go-jalaali v0.0.0-20210801064154-80525e88d958/go.mod h1:Wqfu7mjUHj9WDzSSPI5KfBclTTEnLveRUFr/ujWnTgE= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/keybase/go-keychain v0.0.1 h1:way+bWYa6lDppZoZcgMbYsvC7GxljxrskdNInRtuthU= github.com/keybase/go-keychain v0.0.1/go.mod h1:PdEILRW3i9D8JcdM+FmY6RwkHGnhHxXwkPPMeUgOK1k= github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg= github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo= github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= github.com/mark3labs/mcp-go v0.32.0 h1:fgwmbfL2gbd67obg57OfV2Dnrhs1HtSdlY/i5fn7MU8= github.com/mark3labs/mcp-go v0.32.0/go.mod h1:rXqOudj/djTORU/ThxYx8fqEVj/5pvTuuebQ2RC7uk4= github.com/markusmobius/go-dateparser v1.2.4 h1:2e8XJozaERVxGwsRg72coi51L2aiYqE2gukkdLc85ck= github.com/markusmobius/go-dateparser v1.2.4/go.mod h1:CBAUADJuMNhJpyM6IYaWAoFhtKaqnUcznY2cL7gNugY= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk= github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y= github.com/mattn/go-sqlite3 v1.14.8/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI= github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg= github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k= github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM= github.com/minio/minio-go/v7 v7.0.61 h1:87c+x8J3jxQ5VUGimV9oHdpjsAvy3fhneEBKuoKEVUI= github.com/minio/minio-go/v7 v7.0.61/go.mod h1:BTu8FcrEw+HidY0zd/0eny43QnVNkXRPXrLXFuQBHXg= github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM= github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/nats-io/nats.go v1.44.0 h1:ECKVrDLdh/kDPV1g0gAQ+2+m2KprqZK5O/eJAyAnH2M= github.com/nats-io/nats.go v1.44.0/go.mod h1:iRWIPokVIFbVijxuMQq4y9ttaBTMe0SFdlZfMDd+33g= github.com/nats-io/nkeys v0.4.11 h1:q44qGV008kYd9W1b1nEBkNzvnWxtRSQ7A8BoqRrcfa0= github.com/nats-io/nkeys v0.4.11/go.mod h1:szDimtgmfOi9n25JpfIdGw12tZFYXqhGxjhVxsatHVE= github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w= github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= github.com/pierrec/lz4/v4 v4.1.22 h1:cKFw6uJDK+/gfw5BcDL0JL5aBsAFdsIT18eRtLj7VIU= github.com/pierrec/lz4/v4 v4.1.22/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU= github.com/pkg/sftp v1.13.6 h1:JFZT4XbOU7l77xGSpOdW+pwIMqP044IyjXX6FGyEKFo= github.com/pkg/sftp v1.13.6/go.mod h1:tz1ryNURKu77RL+GuCzmoJYxQczL3wLNNpPWagdg4Qk= github.com/pkg/xattr v0.4.9 h1:5883YPCtkSd8LFbs13nXplj9g9tlrwoJRjgpgMu1/fE= github.com/pkg/xattr v0.4.9/go.mod h1:di8WF84zAKk8jzR1UBTEWh9AUlIZZ7M/JNt8e9B6ktU= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q= github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM= github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY= github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= github.com/psanford/sqlite3vfs v0.0.0-20251127171934-4e34e03a991a h1:r4YWl0uVObCbBFvj1VsIlyHzgZwZOHvY1KdRaQjzzUc= github.com/psanford/sqlite3vfs v0.0.0-20251127171934-4e34e03a991a/go.mod h1:iW4cSew5PAb1sMZiTEkVJAIBNrepaB6jTYjeP47WtI0= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y= github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= github.com/studio-b12/gowebdav v0.11.0 h1:qbQzq4USxY28ZYsGJUfO5jR+xkFtcnwWgitp4Zp1irU= github.com/studio-b12/gowebdav v0.11.0/go.mod h1:bHA7t77X/QFExdeAnDzK6vKM34kEZAcE1OX4MfiwjkE= github.com/superfly/ltx v0.5.1 h1:vGUeBhKvBKZ2s2TsTrOSahz+m0PswfqOWDAD+ICiiYY= github.com/superfly/ltx v0.5.1/go.mod h1:Nf50QAIXU/ET4ua3AuQ2fh31MbgNQZA7r/DYx6Os77s= github.com/tetratelabs/wazero v1.2.1 h1:J4X2hrGzJvt+wqltuvcSjHQ7ujQxA9gb6PeMs4qlUWs= github.com/tetratelabs/wazero v1.2.1/go.mod h1:wYx2gNRg8/WihJfSDxA1TIL8H+GkfLYm+bIfbblu9VQ= github.com/wasilibs/go-re2 v1.3.0 h1:LFhBNzoStM3wMie6rN2slD1cuYH2CGiHpvNL3UtcsMw= github.com/wasilibs/go-re2 v1.3.0/go.mod h1:AafrCXVvGRJJOImMajgJ2M7rVmWyisVK7sFshbxnVrg= github.com/wasilibs/nottinygc v0.4.0 h1:h1TJMihMC4neN6Zq+WKpLxgd9xCFMw7O9ETLwY2exJQ= github.com/wasilibs/nottinygc v0.4.0/go.mod h1:oDcIotskuYNMpqMF23l7Z8uzD4TC0WXHK8jetlB3HIo= github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4= github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 h1:SpGay3w+nEwMpfVnbqOLH5gY52/foP8RE8UzTZ1pdSE= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1/go.mod h1:4UoMYEZOC0yN/sPGH76KPkkU7zgiEWYWL9vwmbnTJPE= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 h1:aFJWCqJMNjENlcleuuOkGAPH82y0yULBScfXcIEdS24= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1/go.mod h1:sEGXWArGqc3tVa+ekntsN65DmVbVeW+7lTKTjZF3/Fo= go.opentelemetry.io/otel v1.21.0 h1:hzLeKBZEL7Okw2mGzZ0cc4k/A7Fta0uoPgaJCr8fsFc= go.opentelemetry.io/otel v1.21.0/go.mod h1:QZzNPQPm1zLX4gZK4cMi+71eaorMSGT3A4znnUvNNEo= go.opentelemetry.io/otel/metric v1.21.0 h1:tlYWfeo+Bocx5kLEloTjbcDwBuELRrIFxwdQ36PlJu4= go.opentelemetry.io/otel/metric v1.21.0/go.mod h1:o1p3CA8nNHW8j5yuQLdc1eeqEaPfzug24uvsyIEJRWM= go.opentelemetry.io/otel/sdk v1.19.0 h1:6USY6zH+L8uMH8L3t1enZPR3WFEmSTADlqldyHtJi3o= go.opentelemetry.io/otel/sdk v1.19.0/go.mod h1:NedEbbS4w3C6zElbLdPJKOpJQOrGUJ+GfzpjUvI0v1A= go.opentelemetry.io/otel/trace v1.21.0 h1:WD9i5gzvoUPuXIXH24ZNBudiarZDKuekPqi/E8fpfLc= go.opentelemetry.io/otel/trace v1.21.0/go.mod h1:LGbsEB0f9LGjN+OZaQQ26sohbOmiMR+BaslueVtS/qQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw= golang.org/x/crypto v0.45.0 h1:jMBrvKuj23MTlT0bQEOBcAE0mjg8mK9RXFhRH6nyF3Q= golang.org/x/crypto v0.45.0/go.mod h1:XTGrrkGJve7CYK7J8PEww4aY7gM3qMCElcJQ8n8JdX4= golang.org/x/crypto/x509roots/fallback v0.0.0-20260209214922-2f26647a795e h1:G2pNJMnWEb60ip90TMo++m9fpt+d3YIZa0er3buPKRo= golang.org/x/crypto/x509roots/fallback v0.0.0-20260209214922-2f26647a795e/go.mod h1:MEIPiCnxvQEjA4astfaKItNwEVZA5Ki+3+nyGbJ5N18= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 h1:mgKeJMpvi0yx/sU5GsxQ7p6s2wtOnGAHZWCHUM4KGzY= golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546/go.mod h1:j/pmGrbnkbPtQfxEe5D0VQhZC6qKbfKifgD0oM7sR70= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA= golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY= golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.27.0 h1:da9Vo7/tDv5RH/7nZDz1eMGS/q1Vv1N/7FCrBhI9I3M= golang.org/x/oauth2 v0.27.0/go.mod h1:onh5ek6nERTohokkhCD/y2cV4Do3fxFHFuAejCkRWT8= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I= golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc= golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.37.0 h1:8EGAD0qCmHYZg6J17DvsMy9/wJ7/D/4pV/wfnld5lTU= golang.org/x/term v0.37.0/go.mod h1:5pB4lxRNYYVZuTLmy8oR2BH8dflOR+IbTYFD8fi3254= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM= golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM= golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ= golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU= golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90= google.golang.org/api v0.154.0 h1:X7QkVKZBskztmpPKWQXgjJRPA2dJYrL6r+sYPRLj050= google.golang.org/api v0.154.0/go.mod h1:qhSMkM85hgqiokIYsrRyKxrjfBeIhgl4Z2JmeRkYylc= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= google.golang.org/genproto v0.0.0-20231212172506-995d672761c0 h1:YJ5pD9rF8o9Qtta0Cmy9rdBwkSjrTCT6XTiUQVOtIos= google.golang.org/genproto v0.0.0-20231212172506-995d672761c0/go.mod h1:l/k7rMz0vFTBPy+tFSGvXEd3z+BcoG1k7EHbqm+YBsY= google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0 h1:s1w3X6gQxwrLEpxnLd/qXTVLgQE2yXwaOaoa6IlY/+o= google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0/go.mod h1:CAny0tYF+0/9rmDB9fahA9YLzX3+AEVl1qXbv5hhj6c= google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 h1:/jFB8jK5R3Sq3i/lmeZO0cATSzFfZaJq1J2Euan3XKU= google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0/go.mod h1:FUoWkonphQm3RhTS+kOEhF8h0iDpm4tdXolVCeZ9KKA= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/grpc v1.60.1 h1:26+wFr+cNqSGFcOXcabYC0lUVJVRa2Sb2ortSK7VrEU= google.golang.org/grpc v1.60.1/go.mod h1:OlCHIeLYqSSsLi6i49B5QGdzaMZK9+M7LXN2FKz4eGM= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= modernc.org/cc/v4 v4.27.1 h1:9W30zRlYrefrDV2JE2O8VDtJ1yPGownxciz5rrbQZis= modernc.org/cc/v4 v4.27.1/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0= modernc.org/ccgo/v4 v4.30.1 h1:4r4U1J6Fhj98NKfSjnPUN7Ze2c6MnAdL0hWw6+LrJpc= modernc.org/ccgo/v4 v4.30.1/go.mod h1:bIOeI1JL54Utlxn+LwrFyjCx2n2RDiYEaJVSrgdrRfM= modernc.org/fileutil v1.3.40 h1:ZGMswMNc9JOCrcrakF1HrvmergNLAmxOPjizirpfqBA= modernc.org/fileutil v1.3.40/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc= modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI= modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito= modernc.org/gc/v3 v3.1.1 h1:k8T3gkXWY9sEiytKhcgyiZ2L0DTyCQ/nvX+LoCljoRE= modernc.org/gc/v3 v3.1.1/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY= modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks= modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI= modernc.org/libc v1.67.6 h1:eVOQvpModVLKOdT+LvBPjdQqfrZq+pC39BygcT+E7OI= modernc.org/libc v1.67.6/go.mod h1:JAhxUVlolfYDErnwiqaLvUqc8nfb2r6S6slAgZOnaiE= modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU= modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg= modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI= modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw= modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8= modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns= modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w= modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE= modernc.org/sqlite v1.44.3 h1:+39JvV/HWMcYslAwRxHb8067w+2zowvFOUrOWIy9PjY= modernc.org/sqlite v1.44.3/go.mod h1:CzbrU2lSB1DKUusvwGz7rqEKIq+NUd8GWuBBZDs9/nA= modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0= modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A= modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y= modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= ================================================ FILE: grafana/README.md ================================================ # Litestream Grafana Dashboard This directory contains a Grafana dashboard for monitoring Litestream metrics. ## Prerequisites 1. Litestream configured with metrics endpoint enabled in `litestream.yml`: ```yaml addr: ":9090" ``` 2. Prometheus configured to scrape Litestream metrics: ```yaml scrape_configs: - job_name: 'litestream' static_configs: - targets: ['localhost:9090'] ``` 3. Grafana with Prometheus data source configured ## Installation 1. Open Grafana and navigate to **Dashboards** → **Import** 2. Upload the `litestream-dashboard.json` file or paste its contents 3. Select your Prometheus data source 4. Click **Import** ## Metrics Included The dashboard monitors the following key metrics: - **Database & WAL Size**: Current size of the database and Write-Ahead Log - **Total WAL Bytes Written**: Cumulative bytes written to shadow WAL - **Sync Operations**: Rate of sync operations and any sync errors - **Sync Duration**: Time spent syncing shadow WAL - **Checkpoint Operations**: Rate of checkpoint operations by mode - **Checkpoint Errors**: Any checkpoint errors that occur - **Transaction ID**: Current transaction ID for each database - **Replica Operations**: Operations performed by replica type (GET/PUT) - **Replica Throughput**: Bytes transferred by replica operations ## Configuration The dashboard uses template variables: - `datasource`: Select your Prometheus data source - `job`: Select the Prometheus job name (defaults to "litestream") ## Support For issues or improvements to this dashboard, please open an issue at: ================================================ FILE: grafana/litestream-dashboard.json ================================================ { "annotations": { "list": [ { "builtIn": 1, "datasource": "-- Grafana --", "enable": true, "hide": true, "iconColor": "rgba(0, 211, 255, 1)", "name": "Annotations & Alerts", "type": "dashboard" } ] }, "editable": true, "gnetId": null, "graphTooltip": 0, "id": null, "links": [], "panels": [ { "datasource": "${datasource}", "fieldConfig": { "defaults": { "color": { "mode": "palette-classic" }, "custom": { "axisLabel": "", "axisPlacement": "auto", "barAlignment": 0, "drawStyle": "line", "fillOpacity": 10, "gradientMode": "none", "hideFrom": { "tooltip": false, "viz": false, "legend": false }, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, "scaleDistribution": { "type": "linear" }, "showPoints": "never", "spanNulls": true, "stacking": { "group": "A", "mode": "none" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null }, { "color": "red", "value": 80 } ] }, "unit": "bytes" }, "overrides": [] }, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 0 }, "id": 2, "options": { "legend": { "calcs": [], "displayMode": "list", "placement": "bottom" }, "tooltip": { "mode": "single" } }, "pluginVersion": "8.0.0", "targets": [ { "expr": "litestream_db_size{job=\"$job\"}", "legendFormat": "{{db}} - DB Size", "refId": "A" }, { "expr": "litestream_wal_size{job=\"$job\"}", "legendFormat": "{{db}} - WAL Size", "refId": "B" } ], "title": "Database & WAL Size", "type": "timeseries" }, { "datasource": "${datasource}", "fieldConfig": { "defaults": { "color": { "mode": "thresholds" }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null }, { "color": "red", "value": 80 } ] }, "unit": "bytes" }, "overrides": [] }, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 0 }, "id": 10, "options": { "orientation": "auto", "reduceOptions": { "values": false, "calcs": [ "lastNotNull" ], "fields": "" }, "showThresholdLabels": false, "showThresholdMarkers": true, "text": {} }, "pluginVersion": "8.0.0", "targets": [ { "expr": "litestream_total_wal_bytes{job=\"$job\"}", "legendFormat": "{{db}}", "refId": "A" } ], "title": "Total WAL Bytes Written", "type": "gauge" }, { "datasource": "${datasource}", "fieldConfig": { "defaults": { "color": { "mode": "palette-classic" }, "custom": { "axisLabel": "", "axisPlacement": "auto", "barAlignment": 0, "drawStyle": "line", "fillOpacity": 10, "gradientMode": "none", "hideFrom": { "tooltip": false, "viz": false, "legend": false }, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, "scaleDistribution": { "type": "linear" }, "showPoints": "never", "spanNulls": true, "stacking": { "group": "A", "mode": "none" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null }, { "color": "red", "value": 80 } ] }, "unit": "ops" }, "overrides": [] }, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 8 }, "id": 3, "options": { "legend": { "calcs": [], "displayMode": "list", "placement": "bottom" }, "tooltip": { "mode": "single" } }, "pluginVersion": "8.0.0", "targets": [ { "expr": "rate(litestream_sync_count{job=\"$job\"}[5m])", "legendFormat": "{{db}} - Sync Rate", "refId": "A" } ], "title": "Sync Operations Rate", "type": "timeseries" }, { "datasource": "${datasource}", "fieldConfig": { "defaults": { "color": { "mode": "palette-classic" }, "custom": { "axisLabel": "", "axisPlacement": "auto", "barAlignment": 0, "drawStyle": "line", "fillOpacity": 10, "gradientMode": "none", "hideFrom": { "tooltip": false, "viz": false, "legend": false }, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, "scaleDistribution": { "type": "linear" }, "showPoints": "never", "spanNulls": true, "stacking": { "group": "A", "mode": "none" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null }, { "color": "red", "value": 80 } ] }, "unit": "errors/sec" }, "overrides": [] }, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 8 }, "id": 4, "options": { "legend": { "calcs": [], "displayMode": "list", "placement": "bottom" }, "tooltip": { "mode": "single" } }, "pluginVersion": "8.0.0", "targets": [ { "expr": "rate(litestream_sync_error_count{job=\"$job\"}[5m])", "legendFormat": "{{db}} - Sync Errors", "refId": "A" } ], "title": "Sync Error Rate", "type": "timeseries" }, { "datasource": "${datasource}", "fieldConfig": { "defaults": { "color": { "mode": "palette-classic" }, "custom": { "axisLabel": "", "axisPlacement": "auto", "barAlignment": 0, "drawStyle": "line", "fillOpacity": 10, "gradientMode": "none", "hideFrom": { "tooltip": false, "viz": false, "legend": false }, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, "scaleDistribution": { "type": "linear" }, "showPoints": "never", "spanNulls": true, "stacking": { "group": "A", "mode": "none" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null }, { "color": "red", "value": 80 } ] }, "unit": "s" }, "overrides": [] }, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 16 }, "id": 5, "options": { "legend": { "calcs": [], "displayMode": "list", "placement": "bottom" }, "tooltip": { "mode": "single" } }, "pluginVersion": "8.0.0", "targets": [ { "expr": "rate(litestream_sync_seconds{job=\"$job\"}[5m])", "legendFormat": "{{db}} - Sync Time", "refId": "A" } ], "title": "Sync Duration", "type": "timeseries" }, { "datasource": "${datasource}", "fieldConfig": { "defaults": { "color": { "mode": "palette-classic" }, "custom": { "axisLabel": "", "axisPlacement": "auto", "barAlignment": 0, "drawStyle": "line", "fillOpacity": 10, "gradientMode": "none", "hideFrom": { "tooltip": false, "viz": false, "legend": false }, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, "scaleDistribution": { "type": "linear" }, "showPoints": "never", "spanNulls": true, "stacking": { "group": "A", "mode": "none" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null }, { "color": "red", "value": 80 } ] }, "unit": "ops" }, "overrides": [] }, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 16 }, "id": 6, "options": { "legend": { "calcs": [], "displayMode": "list", "placement": "bottom" }, "tooltip": { "mode": "single" } }, "pluginVersion": "8.0.0", "targets": [ { "expr": "rate(litestream_checkpoint_count{job=\"$job\"}[5m])", "legendFormat": "{{db}} - {{mode}}", "refId": "A" } ], "title": "Checkpoint Operations Rate", "type": "timeseries" }, { "datasource": "${datasource}", "fieldConfig": { "defaults": { "color": { "mode": "palette-classic" }, "custom": { "axisLabel": "", "axisPlacement": "auto", "barAlignment": 0, "drawStyle": "line", "fillOpacity": 10, "gradientMode": "none", "hideFrom": { "tooltip": false, "viz": false, "legend": false }, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, "scaleDistribution": { "type": "linear" }, "showPoints": "never", "spanNulls": true, "stacking": { "group": "A", "mode": "none" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null }, { "color": "red", "value": 80 } ] }, "unit": "errors/sec" }, "overrides": [] }, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 24 }, "id": 7, "options": { "legend": { "calcs": [], "displayMode": "list", "placement": "bottom" }, "tooltip": { "mode": "single" } }, "pluginVersion": "8.0.0", "targets": [ { "expr": "rate(litestream_checkpoint_error_count{job=\"$job\"}[5m])", "legendFormat": "{{db}} - {{mode}}", "refId": "A" } ], "title": "Checkpoint Error Rate", "type": "timeseries" }, { "datasource": "${datasource}", "fieldConfig": { "defaults": { "color": { "mode": "palette-classic" }, "custom": { "axisLabel": "", "axisPlacement": "auto", "barAlignment": 0, "drawStyle": "line", "fillOpacity": 10, "gradientMode": "none", "hideFrom": { "tooltip": false, "viz": false, "legend": false }, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, "scaleDistribution": { "type": "linear" }, "showPoints": "never", "spanNulls": true, "stacking": { "group": "A", "mode": "none" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null }, { "color": "red", "value": 80 } ] }, "unit": "none" }, "overrides": [] }, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 24 }, "id": 11, "options": { "legend": { "calcs": [], "displayMode": "list", "placement": "bottom" }, "tooltip": { "mode": "single" } }, "pluginVersion": "8.0.0", "targets": [ { "expr": "litestream_txid{job=\"$job\"}", "legendFormat": "{{db}} - Transaction ID", "refId": "A" } ], "title": "Current Transaction ID", "type": "timeseries" }, { "datasource": "${datasource}", "fieldConfig": { "defaults": { "color": { "mode": "palette-classic" }, "custom": { "axisLabel": "", "axisPlacement": "auto", "barAlignment": 0, "drawStyle": "line", "fillOpacity": 10, "gradientMode": "none", "hideFrom": { "tooltip": false, "viz": false, "legend": false }, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, "scaleDistribution": { "type": "linear" }, "showPoints": "never", "spanNulls": true, "stacking": { "group": "A", "mode": "none" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null }, { "color": "red", "value": 80 } ] }, "unit": "ops" }, "overrides": [] }, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 32 }, "id": 8, "options": { "legend": { "calcs": [], "displayMode": "list", "placement": "bottom" }, "tooltip": { "mode": "single" } }, "pluginVersion": "8.0.0", "targets": [ { "expr": "rate(litestream_replica_operation_total{job=\"$job\"}[5m])", "legendFormat": "{{replica_type}} - {{operation}}", "refId": "A" } ], "title": "Replica Operations Rate", "type": "timeseries" }, { "datasource": "${datasource}", "fieldConfig": { "defaults": { "color": { "mode": "palette-classic" }, "custom": { "axisLabel": "", "axisPlacement": "auto", "barAlignment": 0, "drawStyle": "line", "fillOpacity": 10, "gradientMode": "none", "hideFrom": { "tooltip": false, "viz": false, "legend": false }, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, "scaleDistribution": { "type": "linear" }, "showPoints": "never", "spanNulls": true, "stacking": { "group": "A", "mode": "none" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null }, { "color": "red", "value": 80 } ] }, "unit": "binBps" }, "overrides": [] }, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 32 }, "id": 9, "options": { "legend": { "calcs": [], "displayMode": "list", "placement": "bottom" }, "tooltip": { "mode": "single" } }, "pluginVersion": "8.0.0", "targets": [ { "expr": "rate(litestream_replica_operation_bytes{job=\"$job\"}[5m])", "legendFormat": "{{replica_type}} - {{operation}}", "refId": "A" } ], "title": "Replica Throughput", "type": "timeseries" } ], "refresh": "30s", "schemaVersion": 27, "style": "dark", "tags": ["litestream", "sqlite", "replication"], "templating": { "list": [ { "current": { "selected": false, "text": "Prometheus", "value": "Prometheus" }, "hide": 0, "includeAll": false, "label": "Data Source", "multi": false, "name": "datasource", "options": [], "query": "prometheus", "queryValue": "", "refresh": 1, "regex": "", "skipUrlSync": false, "type": "datasource" }, { "current": { "selected": false, "text": "litestream", "value": "litestream" }, "datasource": "${datasource}", "definition": "label_values(litestream_db_size, job)", "hide": 0, "includeAll": false, "label": "Job", "multi": false, "name": "job", "options": [], "query": { "query": "label_values(litestream_db_size, job)", "refId": "StandardVariableQuery" }, "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "type": "query" } ] }, "time": { "from": "now-1h", "to": "now" }, "timepicker": {}, "timezone": "", "title": "Litestream Monitoring", "uid": "litestream-monitoring", "version": 1 } ================================================ FILE: gs/replica_client.go ================================================ package gs import ( "bytes" "context" "errors" "fmt" "io" "log/slog" "net/url" "os" "path" "sync" "time" "cloud.google.com/go/storage" "github.com/superfly/ltx" "google.golang.org/api/iterator" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/internal" ) func init() { litestream.RegisterReplicaClientFactory("gs", NewReplicaClientFromURL) } // ReplicaClientType is the client type for this package. const ReplicaClientType = "gs" // MetadataKeyTimestamp is the metadata key for storing LTX file timestamps in GCS. const MetadataKeyTimestamp = "litestream-timestamp" var _ litestream.ReplicaClient = (*ReplicaClient)(nil) // ReplicaClient is a client for writing LTX files to Google Cloud Storage. type ReplicaClient struct { mu sync.Mutex client *storage.Client // gs client bkt *storage.BucketHandle // gs bucket handle logger *slog.Logger // GS bucket information Bucket string Path string } // NewReplicaClient returns a new instance of ReplicaClient. func NewReplicaClient() *ReplicaClient { return &ReplicaClient{ logger: slog.Default().WithGroup(ReplicaClientType), } } func (c *ReplicaClient) SetLogger(logger *slog.Logger) { c.logger = logger.WithGroup(ReplicaClientType) } // NewReplicaClientFromURL creates a new ReplicaClient from URL components. // This is used by the replica client factory registration. func NewReplicaClientFromURL(scheme, host, urlPath string, query url.Values, userinfo *url.Userinfo) (litestream.ReplicaClient, error) { if host == "" { return nil, fmt.Errorf("bucket required for gs replica URL") } client := NewReplicaClient() client.Bucket = host client.Path = urlPath return client, nil } // Type returns "gs" as the client type. func (c *ReplicaClient) Type() string { return ReplicaClientType } // Init initializes the connection to GS. No-op if already initialized. func (c *ReplicaClient) Init(ctx context.Context) (err error) { c.mu.Lock() defer c.mu.Unlock() if c.client != nil { return nil } if c.client, err = storage.NewClient(ctx); err != nil { return fmt.Errorf("failed to create GCS client (bucket: %s): %w", c.Bucket, err) } c.bkt = c.client.Bucket(c.Bucket) return nil } // DeleteAll deletes all LTX files. func (c *ReplicaClient) DeleteAll(ctx context.Context) error { if err := c.Init(ctx); err != nil { return err } // Iterate over every object and delete it. internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "LIST").Inc() for it := c.bkt.Objects(ctx, &storage.Query{Prefix: c.Path + "/"}); ; { attrs, err := it.Next() if errors.Is(err, iterator.Done) { break } else if err != nil { return fmt.Errorf("failed to list objects in GCS bucket %s (path: %s): %w", c.Bucket, c.Path, err) } if err := c.bkt.Object(attrs.Name).Delete(ctx); isNotExists(err) { continue } else if err != nil { return fmt.Errorf("gs: cannot delete object %q: %w", attrs.Name, err) } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "DELETE").Inc() } // log.Printf("%s(%s): retainer: deleting", r.db.Path(), r.Name()) return nil } // LTXFiles returns an iterator over all available LTX files for a level. // GCS always uses accurate timestamps from metadata since they're included in LIST operations at zero cost. // The useMetadata parameter is ignored. func (c *ReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { if err := c.Init(ctx); err != nil { return nil, err } dir := litestream.LTXLevelDir(c.Path, level) prefix := dir + "/" if seek != 0 { prefix += seek.String() } return newLTXFileIterator(c.bkt.Objects(ctx, &storage.Query{Prefix: prefix}), c, level), nil } // WriteLTXFile writes an LTX file from rd to a remote path. func (c *ReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, rd io.Reader) (info *ltx.FileInfo, err error) { if err := c.Init(ctx); err != nil { return info, err } key := litestream.LTXFilePath(c.Path, level, minTXID, maxTXID) // Use TeeReader to peek at LTX header while preserving data for upload var buf bytes.Buffer teeReader := io.TeeReader(rd, &buf) // Extract timestamp from LTX header hdr, _, err := ltx.PeekHeader(teeReader) if err != nil { return nil, fmt.Errorf("extract timestamp from LTX header: %w", err) } timestamp := time.UnixMilli(hdr.Timestamp).UTC() // Combine buffered data with rest of reader fullReader := io.MultiReader(&buf, rd) w := c.bkt.Object(key).NewWriter(ctx) defer w.Close() // Store timestamp in GCS metadata for accurate timestamp retrieval w.Metadata = map[string]string{ MetadataKeyTimestamp: timestamp.Format(time.RFC3339Nano), } n, err := io.Copy(w, fullReader) if err != nil { return info, err } else if err := w.Close(); err != nil { return info, err } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "PUT").Inc() internal.OperationBytesCounterVec.WithLabelValues(ReplicaClientType, "PUT").Add(float64(n)) return <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Size: n, CreatedAt: timestamp, }, nil } // OpenLTXFile returns a reader for a given LTX file. // Returns os.ErrNotExist if no matching index/offset is found. func (c *ReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { if err := c.Init(ctx); err != nil { return nil, err } key := litestream.LTXFilePath(c.Path, level, minTXID, maxTXID) // In the GCS client, a length of -1 reads to EOF while 0 returns no data. // Callers pass size=0 to indicate "entire object" so translate that here. // See: https://pkg.go.dev/cloud.google.com/go/storage#ObjectHandle.NewRangeReader length := size if length <= 0 { length = -1 } r, err := c.bkt.Object(key).NewRangeReader(ctx, offset, length) if isNotExists(err) { return nil, os.ErrNotExist } else if err != nil { return nil, err } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "GET").Inc() internal.OperationBytesCounterVec.WithLabelValues(ReplicaClientType, "GET").Add(float64(r.Attrs.Size)) return r, nil } // DeleteLTXFiles deletes a set of LTX files. func (c *ReplicaClient) DeleteLTXFiles(ctx context.Context, a []*ltx.FileInfo) error { if err := c.Init(ctx); err != nil { return err } for _, info := range a { key := litestream.LTXFilePath(c.Path, info.Level, info.MinTXID, info.MaxTXID) c.logger.Debug("deleting ltx file", "level", info.Level, "minTXID", info.MinTXID, "maxTXID", info.MaxTXID, "key", key) if err := c.bkt.Object(key).Delete(ctx); err != nil && !isNotExists(err) { return fmt.Errorf("gs: cannot delete ltx file %q: %w", key, err) } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "DELETE").Inc() } return nil } type ltxFileIterator struct { it *storage.ObjectIterator client *ReplicaClient level int info *ltx.FileInfo err error } func newLTXFileIterator(it *storage.ObjectIterator, client *ReplicaClient, level int) *ltxFileIterator { return <xFileIterator{ it: it, client: client, level: level, } } func (itr *ltxFileIterator) Close() (err error) { return itr.err } func (itr *ltxFileIterator) Next() bool { // Exit if an error has already occurred. if itr.err != nil { return false } for { // Fetch next object. attrs, err := itr.it.Next() if errors.Is(err, iterator.Done) { return false } else if err != nil { itr.err = err return false } // Parse index & offset, otherwise skip to the next object. minTXID, maxTXID, err := ltx.ParseFilename(path.Base(attrs.Name)) if err != nil { continue } // Always use accurate timestamp from metadata since it's zero-cost // GCS includes metadata in LIST operations, so no extra API call needed createdAt := attrs.Created.UTC() if attrs.Metadata != nil { if ts, ok := attrs.Metadata[MetadataKeyTimestamp]; ok { if parsed, err := time.Parse(time.RFC3339Nano, ts); err == nil { createdAt = parsed } } } // Store current snapshot and return. itr.info = <x.FileInfo{ Level: itr.level, MinTXID: minTXID, MaxTXID: maxTXID, Size: attrs.Size, CreatedAt: createdAt, } return true } } func (itr *ltxFileIterator) Err() error { return itr.err } func (itr *ltxFileIterator) Item() *ltx.FileInfo { return itr.info } func isNotExists(err error) bool { return errors.Is(err, storage.ErrObjectNotExist) } ================================================ FILE: gs/replica_client_test.go ================================================ package gs import ( "bytes" "context" "io" "testing" "time" "github.com/fsouza/fake-gcs-server/fakestorage" "github.com/superfly/ltx" ) func ltxTestData(tb testing.TB, minTXID, maxTXID ltx.TXID, payload []byte) []byte { tb.Helper() hdr := ltx.Header{ Version: 1, PageSize: 4096, Commit: 1, MinTXID: minTXID, MaxTXID: maxTXID, Timestamp: time.Now().UnixMilli(), } buf, err := hdr.MarshalBinary() if err != nil { tb.Fatalf("marshal header: %v", err) } return append(buf, payload...) } func setupTestClient(tb testing.TB) (*ReplicaClient, *fakestorage.Server) { tb.Helper() server, err := fakestorage.NewServerWithOptions(fakestorage.Options{NoListener: true}) if err != nil { tb.Fatalf("new server: %v", err) } bucket := "litestream-test" server.CreateBucketWithOpts(fakestorage.CreateBucketOpts{Name: bucket}) client := server.Client() rc := NewReplicaClient() rc.client = client rc.bkt = client.Bucket(bucket) rc.Bucket = bucket rc.Path = "integration" return rc, server } func TestReplicaClient_OpenLTXFileReadsFullObject(t *testing.T) { rc, server := setupTestClient(t) defer server.Stop() ctx := context.Background() data := ltxTestData(t, ltx.TXID(1), ltx.TXID(1), []byte("hello")) if _, err := rc.WriteLTXFile(ctx, 0, ltx.TXID(1), ltx.TXID(1), bytes.NewReader(data)); err != nil { t.Fatalf("WriteLTXFile: %v", err) } r, err := rc.OpenLTXFile(ctx, 0, ltx.TXID(1), ltx.TXID(1), 0, 0) if err != nil { t.Fatalf("OpenLTXFile: %v", err) } defer r.Close() out, err := io.ReadAll(r) if err != nil { t.Fatalf("ReadAll: %v", err) } if !bytes.Equal(out, data) { t.Fatalf("unexpected replica content: got %q, want %q", out, data) } } ================================================ FILE: heartbeat.go ================================================ package litestream import ( "context" "fmt" "net/http" "sync" "time" ) const ( DefaultHeartbeatInterval = 5 * time.Minute DefaultHeartbeatTimeout = 30 * time.Second MinHeartbeatInterval = 1 * time.Minute ) type HeartbeatClient struct { mu sync.Mutex httpClient *http.Client URL string Interval time.Duration Timeout time.Duration lastPingAt time.Time } func NewHeartbeatClient(url string, interval time.Duration) *HeartbeatClient { if interval < MinHeartbeatInterval { interval = MinHeartbeatInterval } timeout := DefaultHeartbeatTimeout return &HeartbeatClient{ URL: url, Interval: interval, Timeout: timeout, httpClient: &http.Client{ Timeout: timeout, }, } } func (c *HeartbeatClient) Ping(ctx context.Context) error { if c.URL == "" { return nil } req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.URL, nil) if err != nil { return fmt.Errorf("create request: %w", err) } resp, err := c.httpClient.Do(req) if err != nil { return fmt.Errorf("http request: %w", err) } defer resp.Body.Close() if resp.StatusCode < 200 || resp.StatusCode >= 300 { return fmt.Errorf("unexpected status code: %d", resp.StatusCode) } return nil } func (c *HeartbeatClient) ShouldPing() bool { c.mu.Lock() defer c.mu.Unlock() return time.Since(c.lastPingAt) >= c.Interval } func (c *HeartbeatClient) LastPingAt() time.Time { c.mu.Lock() defer c.mu.Unlock() return c.lastPingAt } func (c *HeartbeatClient) RecordPing() { c.mu.Lock() defer c.mu.Unlock() c.lastPingAt = time.Now() } ================================================ FILE: heartbeat_test.go ================================================ package litestream_test import ( "context" "net/http" "net/http/httptest" "path/filepath" "sync/atomic" "testing" "time" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/internal/testingutil" ) func TestHeartbeatClient_Ping(t *testing.T) { t.Run("Success", func(t *testing.T) { var pingCount atomic.Int64 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { t.Errorf("expected GET, got %s", r.Method) } pingCount.Add(1) w.WriteHeader(http.StatusOK) })) defer server.Close() client := litestream.NewHeartbeatClient(server.URL, 5*time.Minute) if err := client.Ping(context.Background()); err != nil { t.Fatalf("expected no error, got %v", err) } if got := pingCount.Load(); got != 1 { t.Errorf("expected 1 ping, got %d", got) } }) t.Run("EmptyURL", func(t *testing.T) { client := litestream.NewHeartbeatClient("", 5*time.Minute) if err := client.Ping(context.Background()); err != nil { t.Fatalf("expected no error for empty URL, got %v", err) } }) t.Run("NonSuccessStatusCode", func(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) })) defer server.Close() client := litestream.NewHeartbeatClient(server.URL, 5*time.Minute) err := client.Ping(context.Background()) if err == nil { t.Fatal("expected error for 500 status code") } }) t.Run("NetworkError", func(t *testing.T) { client := litestream.NewHeartbeatClient("http://localhost:1", 5*time.Minute) err := client.Ping(context.Background()) if err == nil { t.Fatal("expected error for unreachable server") } }) t.Run("ContextCanceled", func(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { time.Sleep(100 * time.Millisecond) w.WriteHeader(http.StatusOK) })) defer server.Close() ctx, cancel := context.WithCancel(context.Background()) cancel() client := litestream.NewHeartbeatClient(server.URL, 5*time.Minute) err := client.Ping(ctx) if err == nil { t.Fatal("expected error for canceled context") } }) } func TestHeartbeatClient_ShouldPing(t *testing.T) { t.Run("FirstPing", func(t *testing.T) { client := litestream.NewHeartbeatClient("http://example.com", 5*time.Minute) if !client.ShouldPing() { t.Error("expected ShouldPing to return true for first ping") } }) t.Run("AfterRecordPing", func(t *testing.T) { client := litestream.NewHeartbeatClient("http://example.com", 5*time.Minute) client.RecordPing() if client.ShouldPing() { t.Error("expected ShouldPing to return false immediately after RecordPing") } }) } func TestHeartbeatClient_MinInterval(t *testing.T) { client := litestream.NewHeartbeatClient("http://example.com", 30*time.Second) if client.Interval != litestream.MinHeartbeatInterval { t.Errorf("expected interval to be clamped to %v, got %v", litestream.MinHeartbeatInterval, client.Interval) } } func TestHeartbeatClient_LastPingAt(t *testing.T) { client := litestream.NewHeartbeatClient("http://example.com", 5*time.Minute) if !client.LastPingAt().IsZero() { t.Error("expected LastPingAt to be zero initially") } before := time.Now() client.RecordPing() after := time.Now() lastPing := client.LastPingAt() if lastPing.Before(before) || lastPing.After(after) { t.Errorf("LastPingAt %v should be between %v and %v", lastPing, before, after) } } func TestStore_Heartbeat_AllDatabasesHealthy(t *testing.T) { t.Run("NoDatabases", func(t *testing.T) { levels := litestream.CompactionLevels{{Level: 0}} store := litestream.NewStore(nil, levels) store.CompactionMonitorEnabled = false store.HeartbeatCheckInterval = 0 // Disable automatic monitoring var pingCount atomic.Int64 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { pingCount.Add(1) w.WriteHeader(http.StatusOK) })) defer server.Close() store.Heartbeat = litestream.NewHeartbeatClient(server.URL, 1*time.Minute) // With no databases, heartbeat should not fire // We need to trigger the check manually since monitor is disabled // The store won't send pings because allDatabasesHealthy returns false for empty stores if pingCount.Load() != 0 { t.Errorf("expected no pings with no databases, got %d", pingCount.Load()) } }) t.Run("AllDatabasesSynced", func(t *testing.T) { db0, sqldb0 := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db0, sqldb0) db1, sqldb1 := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db1, sqldb1) levels := litestream.CompactionLevels{{Level: 0}, {Level: 1, Interval: time.Second}} store := litestream.NewStore([]*litestream.DB{db0, db1}, levels) store.CompactionMonitorEnabled = false store.HeartbeatCheckInterval = 0 var pingCount atomic.Int64 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { pingCount.Add(1) w.WriteHeader(http.StatusOK) })) defer server.Close() store.Heartbeat = litestream.NewHeartbeatClient(server.URL, 1*time.Minute) if err := store.Open(t.Context()); err != nil { t.Fatalf("open store: %v", err) } defer store.Close(t.Context()) // Create tables and sync both databases if _, err := sqldb0.ExecContext(t.Context(), `CREATE TABLE t (id INT)`); err != nil { t.Fatal(err) } if err := db0.Sync(t.Context()); err != nil { t.Fatal(err) } if err := db0.Replica.Sync(t.Context()); err != nil { t.Fatal(err) } if _, err := sqldb1.ExecContext(t.Context(), `CREATE TABLE t (id INT)`); err != nil { t.Fatal(err) } if err := db1.Sync(t.Context()); err != nil { t.Fatal(err) } if err := db1.Replica.Sync(t.Context()); err != nil { t.Fatal(err) } // Both databases have synced, heartbeat should fire if err := store.Heartbeat.Ping(t.Context()); err != nil { t.Fatalf("ping failed: %v", err) } if got := pingCount.Load(); got != 1 { t.Errorf("expected 1 ping after all DBs synced, got %d", got) } }) t.Run("OneDatabaseNotSynced", func(t *testing.T) { db0, sqldb0 := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db0, sqldb0) // Create second DB but don't sync it db1 := litestream.NewDB(filepath.Join(t.TempDir(), "db1")) db1.Replica = litestream.NewReplica(db1) db1.Replica.Client = testingutil.NewFileReplicaClient(t) db1.Replica.MonitorEnabled = false db1.MonitorInterval = 0 levels := litestream.CompactionLevels{{Level: 0}, {Level: 1, Interval: time.Second}} store := litestream.NewStore([]*litestream.DB{db0, db1}, levels) store.CompactionMonitorEnabled = false store.HeartbeatCheckInterval = 0 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) })) defer server.Close() store.Heartbeat = litestream.NewHeartbeatClient(server.URL, 1*time.Minute) if err := store.Open(t.Context()); err != nil { t.Fatalf("open store: %v", err) } defer store.Close(t.Context()) // Only sync db0 if _, err := sqldb0.ExecContext(t.Context(), `CREATE TABLE t (id INT)`); err != nil { t.Fatal(err) } if err := db0.Sync(t.Context()); err != nil { t.Fatal(err) } if err := db0.Replica.Sync(t.Context()); err != nil { t.Fatal(err) } // db1 hasn't synced, so LastSuccessfulSyncAt should be zero if !db1.LastSuccessfulSyncAt().IsZero() { t.Error("expected db1.LastSuccessfulSyncAt to be zero") } // db0 has synced if db0.LastSuccessfulSyncAt().IsZero() { t.Error("expected db0.LastSuccessfulSyncAt to be non-zero") } }) } ================================================ FILE: internal/hexdump.go ================================================ package internal import ( "bytes" "fmt" ) func Hexdump(data []byte) string { prevRow := make([]byte, 16) var buf bytes.Buffer var dupWritten bool for i := 0; i < len(data); i += 16 { row := make([]byte, 16) copy(row, data[i:]) // Write out one line of asterisks to show that we just have duplicate rows. if i != 0 && i+16 < len(data) && bytes.Equal(row, prevRow) { if !dupWritten { dupWritten = true fmt.Fprintln(&buf, "***") } continue } // Track previous row so we know when we have duplicates. copy(prevRow, row) dupWritten = false fmt.Fprintf(&buf, "%08x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x |%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s|\n", i, row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], toChar(row[0]), toChar(row[1]), toChar(row[2]), toChar(row[3]), toChar(row[4]), toChar(row[5]), toChar(row[6]), toChar(row[7]), toChar(row[8]), toChar(row[9]), toChar(row[10]), toChar(row[11]), toChar(row[12]), toChar(row[13]), toChar(row[14]), toChar(row[15]), ) } return buf.String() } func toChar(b byte) string { if b < 32 || b > 126 { return "." } return string(b) } ================================================ FILE: internal/internal.go ================================================ package internal import ( "io" "log/slog" "os" "syscall" "github.com/pierrec/lz4/v4" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" ) const LevelTrace = slog.LevelDebug - 4 // ReadCloser wraps a reader to also attach a separate closer. type ReadCloser struct { r io.Reader c io.Closer } // NewReadCloser returns a new instance of ReadCloser. func NewReadCloser(r io.Reader, c io.Closer) *ReadCloser { return &ReadCloser{r, c} } // Read reads bytes into the underlying reader. func (r *ReadCloser) Read(p []byte) (n int, err error) { return r.r.Read(p) } // Close closes the reader (if implementing io.ReadCloser) and the Closer. func (r *ReadCloser) Close() error { if rc, ok := r.r.(io.Closer); ok { if err := rc.Close(); err != nil { r.c.Close() return err } } return r.c.Close() } // LZ4ReadCloser wraps an LZ4 reader with the underlying source for proper closing. type LZ4ReadCloser struct { *lz4.Reader underlying io.Closer } func (r *LZ4ReadCloser) Close() error { return r.underlying.Close() } // NewLZ4Reader creates an LZ4 decompressing reader that wraps the source. // Closing the returned reader also closes the underlying source. func NewLZ4Reader(r io.ReadCloser) io.ReadCloser { return &LZ4ReadCloser{ Reader: lz4.NewReader(r), underlying: r, } } // ReadCounter wraps an io.Reader and counts the total number of bytes read. type ReadCounter struct { r io.Reader n int64 } // NewReadCounter returns a new instance of ReadCounter that wraps r. func NewReadCounter(r io.Reader) *ReadCounter { return &ReadCounter{r: r} } // Read reads from the underlying reader into p and adds the bytes read to the counter. func (r *ReadCounter) Read(p []byte) (int, error) { n, err := r.r.Read(p) r.n += int64(n) return n, err } // N returns the total number of bytes read. func (r *ReadCounter) N() int64 { return r.n } // CreateFile creates the file and matches the mode & uid/gid of fi. func CreateFile(filename string, fi os.FileInfo) (*os.File, error) { mode := os.FileMode(0600) if fi != nil { mode = fi.Mode() } f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode) if err != nil { return nil, err } uid, gid := Fileinfo(fi) _ = f.Chown(uid, gid) return f, nil } // MkdirAll is a copy of os.MkdirAll() except that it attempts to set the // mode/uid/gid to match fi for each created directory. func MkdirAll(path string, fi os.FileInfo) error { uid, gid := Fileinfo(fi) // Fast path: if we can tell whether path is a directory or file, stop with success or error. dir, err := os.Stat(path) if err == nil { if dir.IsDir() { return nil } return &os.PathError{Op: "mkdir", Path: path, Err: syscall.ENOTDIR} } // Slow path: make sure parent exists and then call Mkdir for path. i := len(path) for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator. i-- } j := i for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element. j-- } if j > 1 { // Create parent. err = MkdirAll(fixRootDirectory(path[:j-1]), fi) if err != nil { return err } } // Parent now exists; invoke Mkdir and use its result. mode := os.FileMode(0700) if fi != nil { mode = fi.Mode() } err = os.Mkdir(path, mode) if err != nil { // Handle arguments like "foo/." by // double-checking that directory doesn't exist. dir, err1 := os.Lstat(path) if err1 == nil && dir.IsDir() { _ = os.Chown(path, uid, gid) return nil } return err } _ = os.Chown(path, uid, gid) return nil } func ReplaceAttr(groups []string, a slog.Attr) slog.Attr { if a.Key == slog.LevelKey && a.Value.Any() == LevelTrace { a.Value = slog.StringValue("TRACE") } return a } // Shared replica metrics. var ( OperationTotalCounterVec = promauto.NewCounterVec(prometheus.CounterOpts{ Name: "litestream_replica_operation_total", Help: "The number of replica operations performed", }, []string{"replica_type", "operation"}) OperationBytesCounterVec = promauto.NewCounterVec(prometheus.CounterOpts{ Name: "litestream_replica_operation_bytes", Help: "The number of bytes used by replica operations", }, []string{"replica_type", "operation"}) OperationDurationHistogramVec = promauto.NewHistogramVec(prometheus.HistogramOpts{ Name: "litestream_replica_operation_duration_seconds", Help: "Duration of replica operations by type and operation", Buckets: []float64{0.01, 0.05, 0.1, 0.5, 1, 5, 10, 30, 60}, }, []string{"replica_type", "operation"}) OperationErrorCounterVec = promauto.NewCounterVec(prometheus.CounterOpts{ Name: "litestream_replica_operation_errors_total", Help: "Number of replica operation errors by type, operation, and error code", }, []string{"replica_type", "operation", "code"}) L0RetentionGaugeVec = promauto.NewGaugeVec(prometheus.GaugeOpts{ Name: "litestream_l0_retention_files_total", Help: "Number of L0 files by status during retention enforcement", }, []string{"db", "status"}) ) ================================================ FILE: internal/internal_unix.go ================================================ //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris package internal import ( "os" "syscall" ) // Fileinfo returns syscall fields from a FileInfo object. func Fileinfo(fi os.FileInfo) (uid, gid int) { if fi == nil { return -1, -1 } stat, ok := fi.Sys().(*syscall.Stat_t) if !ok { return -1, -1 } return int(stat.Uid), int(stat.Gid) } func fixRootDirectory(p string) string { return p } ================================================ FILE: internal/internal_windows.go ================================================ //go:build windows // +build windows package internal import ( "os" ) // Fileinfo returns syscall fields from a FileInfo object. func Fileinfo(fi os.FileInfo) (uid, gid int) { return -1, -1 } // fixRootDirectory is copied from the standard library for use with mkdirAll() func fixRootDirectory(p string) string { if len(p) == len(`\\?\c:`) { if os.IsPathSeparator(p[0]) && os.IsPathSeparator(p[1]) && p[2] == '?' && os.IsPathSeparator(p[3]) && p[5] == ':' { return p + `\` } } return p } ================================================ FILE: internal/limit_read_closer.go ================================================ package internal import "io" // Copied from the io package to implement io.Closer. func LimitReadCloser(r io.ReadCloser, n int64) io.ReadCloser { return &LimitedReadCloser{r, n} } type LimitedReadCloser struct { R io.ReadCloser // underlying reader N int64 // max bytes remaining } func (l *LimitedReadCloser) Close() error { return l.R.Close() } func (l *LimitedReadCloser) Read(p []byte) (n int, err error) { if l.N <= 0 { return 0, io.EOF } if int64(len(p)) > l.N { p = p[0:l.N] } n, err = l.R.Read(p) l.N -= int64(n) return } ================================================ FILE: internal/lock_unix.go ================================================ //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris package internal import ( "os" "golang.org/x/sys/unix" ) const ( sqlitePendingByte = 0x40000000 sqliteSharedFirst = sqlitePendingByte + 2 sqliteSharedSize = 510 ) func LockFileExclusive(f *os.File) error { fd := int(f.Fd()) if err := setFcntlLock(fd, unix.F_WRLCK, sqlitePendingByte, 1); err != nil { return err } if err := setFcntlLock(fd, unix.F_WRLCK, sqliteSharedFirst, sqliteSharedSize); err != nil { _ = setFcntlLock(fd, unix.F_UNLCK, sqlitePendingByte, 1) return err } return nil } func UnlockFile(f *os.File) error { fd := int(f.Fd()) err1 := setFcntlLock(fd, unix.F_UNLCK, sqliteSharedFirst, sqliteSharedSize) err2 := setFcntlLock(fd, unix.F_UNLCK, sqlitePendingByte, 1) if err1 != nil { return err1 } return err2 } func setFcntlLock(fd int, lockType int16, start int64, length int64) error { flock := unix.Flock_t{ Type: lockType, Whence: 0, Start: start, Len: length, } return unix.FcntlFlock(uintptr(fd), unix.F_SETLKW, &flock) } ================================================ FILE: internal/lock_windows.go ================================================ //go:build windows package internal import ( "os" "golang.org/x/sys/windows" ) const ( sqlitePendingByte = 0x40000000 sqliteSharedFirst = sqlitePendingByte + 2 sqliteSharedSize = 510 ) func LockFileExclusive(f *os.File) error { h := windows.Handle(f.Fd()) pendingOL := windows.Overlapped{Offset: sqlitePendingByte} if err := windows.LockFileEx(h, windows.LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &pendingOL); err != nil { return err } sharedOL := windows.Overlapped{Offset: sqliteSharedFirst} if err := windows.LockFileEx(h, windows.LOCKFILE_EXCLUSIVE_LOCK, 0, sqliteSharedSize, 0, &sharedOL); err != nil { _ = windows.UnlockFileEx(h, 0, 1, 0, &pendingOL) return err } return nil } func UnlockFile(f *os.File) error { h := windows.Handle(f.Fd()) sharedOL := windows.Overlapped{Offset: sqliteSharedFirst} err1 := windows.UnlockFileEx(h, 0, sqliteSharedSize, 0, &sharedOL) pendingOL := windows.Overlapped{Offset: sqlitePendingByte} err2 := windows.UnlockFileEx(h, 0, 1, 0, &pendingOL) if err1 != nil { return err1 } return err2 } ================================================ FILE: internal/resumable_reader.go ================================================ package internal import ( "context" "fmt" "io" "log/slog" "github.com/superfly/ltx" ) type LTXFileOpener interface { OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) } // resumableReader wraps an io.ReadCloser from a remote storage backend with // automatic reconnection on read errors. // // During restore, the LTX compactor opens all LTX file streams upfront, then // processes pages in page-number order. Incremental LTX files that only contain // high-numbered pages may have their S3/storage streams sit idle for minutes // while the compactor works through lower-numbered pages from the snapshot. // Storage providers (S3, Tigris, etc.) may close these idle connections, // causing "unexpected EOF" errors. // // This reader detects two failure modes: // 1. Non-EOF errors (connection reset, timeout) - the stream broke mid-transfer. // 2. Premature EOF - the server closed the connection cleanly, but we haven't // read all bytes yet (detected by comparing offset against known file size). // // On failure, it closes the dead stream and reopens from the current byte // offset using the storage backend's range request support (the offset parameter // of OpenLTXFile). Callers like io.ReadFull see a seamless byte stream because // partial reads are returned without error, prompting the caller to request // remaining bytes on the next Read call. type ResumableReader struct { ctx context.Context client LTXFileOpener level int minTXID ltx.TXID maxTXID ltx.TXID size int64 // expected total file size from FileInfo; 0 means unknown offset int64 rc io.ReadCloser logger *slog.Logger } // NewResumableReader creates a ResumableReader. Primarily exposed for testing. func NewResumableReader(ctx context.Context, client LTXFileOpener, level int, minTXID, maxTXID ltx.TXID, size int64, rc io.ReadCloser, logger *slog.Logger) *ResumableReader { return &ResumableReader{ ctx: ctx, client: client, level: level, minTXID: minTXID, maxTXID: maxTXID, size: size, rc: rc, logger: logger, } } const resumableReaderMaxRetries = 3 func (r *ResumableReader) Read(p []byte) (int, error) { for attempt := 0; attempt <= resumableReaderMaxRetries; attempt++ { // Reopen the stream from the current offset if the previous // connection was closed (rc is nil after a retry). if r.rc == nil { rc, err := r.client.OpenLTXFile(r.ctx, r.level, r.minTXID, r.maxTXID, r.offset, 0) if err != nil { return 0, fmt.Errorf("reopen ltx file at offset %d: %w", r.offset, err) } r.rc = rc } n, err := r.rc.Read(p) r.offset += int64(n) if err == nil { return n, nil } if err == io.EOF { // Distinguish legitimate EOF (fully read) from premature EOF // (server closed idle connection). When the file size is known // and we haven't read it all, treat as a connection drop. if r.size > 0 && r.offset < r.size { r.logger.Debug("premature EOF on ltx file, reconnecting", "level", r.level, "min", r.minTXID, "max", r.maxTXID, "offset", r.offset, "size", r.size, "attempt", attempt+1) r.rc.Close() r.rc = nil if n > 0 { // Return the bytes we did get. The caller (e.g. io.ReadFull) // will call Read again, which will trigger the reopen above. return n, nil } continue } return n, io.EOF } // Non-EOF error (connection reset, timeout, etc.). Close the dead // stream so the next iteration reopens from the current offset. r.logger.Debug("read error on ltx file, reconnecting", "level", r.level, "min", r.minTXID, "max", r.maxTXID, "error", err, "offset", r.offset, "attempt", attempt+1) r.rc.Close() r.rc = nil if n > 0 { return n, nil } continue } return 0, fmt.Errorf("max retries exceeded reading ltx file (level=%d, min=%s, max=%s, offset=%d)", r.level, r.minTXID, r.maxTXID, r.offset) } func (r *ResumableReader) Close() error { if r.rc != nil { return r.rc.Close() } return nil } ================================================ FILE: internal/resumable_reader_test.go ================================================ package internal import ( "bytes" "context" "fmt" "io" "log/slog" "strings" "testing" "github.com/superfly/ltx" ) func TestResumableReader(t *testing.T) { // The resumableReader wraps a storage stream to handle connection drops // during long restore operations. These tests simulate the failure modes // that occur when S3/Tigris closes idle connections. t.Run("NormalRead", func(t *testing.T) { // Verify that a healthy stream passes through unchanged. data := []byte("hello world") client := &testLTXFileOpener{ OpenLTXFileFunc: func(_ context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(data[offset:])), nil }, } r := newTestResumableReader(client, int64(len(data)), data) got, err := io.ReadAll(r) if err != nil { t.Fatalf("unexpected error: %v", err) } if !bytes.Equal(got, data) { t.Fatalf("got %q, want %q", got, data) } }) t.Run("ReconnectOnError", func(t *testing.T) { // Simulate a connection reset after reading 5 bytes of a 11-byte file. // The reader should transparently reconnect from offset 5 and deliver // the remaining bytes. data := []byte("hello world") callCount := 0 client := &testLTXFileOpener{ OpenLTXFileFunc: func(_ context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { callCount++ if callCount == 1 { // First open: return a reader that errors after 5 bytes. return io.NopCloser(&errorAfterN{data: data, n: 5, err: fmt.Errorf("connection reset")}), nil } // Reconnect: serve from the requested offset. return io.NopCloser(bytes.NewReader(data[offset:])), nil }, } r := newTestResumableReader(client, int64(len(data)), data) got, err := io.ReadAll(r) if err != nil { t.Fatalf("unexpected error: %v", err) } if !bytes.Equal(got, data) { t.Fatalf("got %q, want %q", got, data) } if callCount != 2 { t.Fatalf("expected 2 OpenLTXFile calls (original + reconnect), got %d", callCount) } }) t.Run("ReconnectOnPrematureEOF", func(t *testing.T) { // Simulate a server that closes the connection cleanly (returns io.EOF) // before all bytes are transferred. The reader detects this by comparing // bytes read against the known file size. data := []byte("hello world") callCount := 0 client := &testLTXFileOpener{ OpenLTXFileFunc: func(_ context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { callCount++ if callCount == 1 { // First open: return only the first 5 bytes, then EOF. return io.NopCloser(bytes.NewReader(data[:5])), nil } // Reconnect: serve from the requested offset. return io.NopCloser(bytes.NewReader(data[offset:])), nil }, } r := newTestResumableReader(client, int64(len(data)), data) got, err := io.ReadAll(r) if err != nil { t.Fatalf("unexpected error: %v", err) } if !bytes.Equal(got, data) { t.Fatalf("got %q, want %q", got, data) } if callCount != 2 { t.Fatalf("expected 2 OpenLTXFile calls, got %d", callCount) } }) t.Run("ReadFullAcrossReconnect", func(t *testing.T) { // Simulate io.ReadFull reading a 6-byte page header where the // connection drops after 3 bytes. This is the exact scenario from // the original bug: the LTX compactor calls io.ReadFull for a // 6-byte PageHeader, but the stream is dead. data := []byte("ABCDEF remainder of file") callCount := 0 client := &testLTXFileOpener{ OpenLTXFileFunc: func(_ context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { callCount++ if callCount == 1 { return io.NopCloser(&errorAfterN{data: data, n: 3, err: fmt.Errorf("connection reset")}), nil } return io.NopCloser(bytes.NewReader(data[offset:])), nil }, } r := newTestResumableReader(client, int64(len(data)), data) // Read exactly 6 bytes, like the LTX decoder does for page headers. buf := make([]byte, 6) _, err := io.ReadFull(r, buf) if err != nil { t.Fatalf("io.ReadFull failed: %v", err) } if !bytes.Equal(buf, []byte("ABCDEF")) { t.Fatalf("got %q, want %q", buf, "ABCDEF") } }) t.Run("MaxRetriesExceeded", func(t *testing.T) { // If the connection keeps failing, the reader should give up after // the maximum retry count rather than looping forever. client := &testLTXFileOpener{ OpenLTXFileFunc: func(_ context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { return io.NopCloser(&errorAfterN{data: nil, n: 0, err: fmt.Errorf("persistent failure")}), nil }, } r := newTestResumableReader(client, 100, nil) buf := make([]byte, 10) _, err := r.Read(buf) if err == nil { t.Fatal("expected error after max retries, got nil") } if !strings.Contains(err.Error(), "max retries exceeded") { t.Fatalf("expected 'max retries exceeded' error, got: %v", err) } }) t.Run("ReopenFailure", func(t *testing.T) { // If the initial stream dies and the reopen also fails (e.g., 404), // the error should propagate. data := []byte("hello world") callCount := 0 client := &testLTXFileOpener{ OpenLTXFileFunc: func(_ context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { callCount++ if callCount == 1 { return io.NopCloser(&errorAfterN{data: data, n: 3, err: fmt.Errorf("connection reset")}), nil } return nil, fmt.Errorf("file not found") }, } r := newTestResumableReader(client, int64(len(data)), data) // First read gets 3 bytes, then error triggers reconnect attempt. buf := make([]byte, 10) n, err := r.Read(buf) if n != 3 { t.Fatalf("expected 3 bytes on first read, got %d", n) } // The error is suppressed on partial reads; next call hits reopen failure. if err != nil { t.Fatalf("expected nil error on partial read, got: %v", err) } // Second read should fail with reopen error. _, err = r.Read(buf) if err == nil { t.Fatal("expected error on reopen failure, got nil") } if !strings.Contains(err.Error(), "reopen ltx file") { t.Fatalf("expected 'reopen ltx file' error, got: %v", err) } }) t.Run("UnknownSize", func(t *testing.T) { // When file size is unknown (size=0), premature EOF cannot be detected, // so a clean EOF from a truncated stream is treated as legitimate. data := []byte("hello world") client := &testLTXFileOpener{ OpenLTXFileFunc: func(_ context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { // Always return only first 5 bytes. return io.NopCloser(bytes.NewReader(data[:5])), nil }, } r := newTestResumableReader(client, 0 /* unknown size */, data) got, err := io.ReadAll(r) if err != nil { t.Fatalf("unexpected error: %v", err) } // Without size info, we can't detect the truncation. if !bytes.Equal(got, data[:5]) { t.Fatalf("got %q, want %q", got, data[:5]) } }) t.Run("CorrectOffsetOnReopen", func(t *testing.T) { // Verify the reader passes the correct byte offset when reopening. data := []byte("0123456789abcdef") var reopenOffset int64 callCount := 0 client := &testLTXFileOpener{ OpenLTXFileFunc: func(_ context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { callCount++ if callCount == 1 { return io.NopCloser(&errorAfterN{data: data, n: 7, err: fmt.Errorf("timeout")}), nil } reopenOffset = offset return io.NopCloser(bytes.NewReader(data[offset:])), nil }, } r := newTestResumableReader(client, int64(len(data)), data) got, err := io.ReadAll(r) if err != nil { t.Fatalf("unexpected error: %v", err) } if !bytes.Equal(got, data) { t.Fatalf("got %q, want %q", got, data) } if reopenOffset != 7 { t.Fatalf("reopen offset = %d, want 7", reopenOffset) } }) } // newTestResumableReader creates a resumableReader for testing. The initial // stream is opened from the client; data is only used for reference. func newTestResumableReader(client *testLTXFileOpener, size int64, data []byte) *ResumableReader { rc, _ := client.OpenLTXFile(context.Background(), 0, 1, 1, 0, 0) return NewResumableReader( context.Background(), client, 0, // level 1, // minTXID 1, // maxTXID size, // expected file size rc, slog.Default(), ) } type testLTXFileOpener struct { OpenLTXFileFunc func(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) } func (t *testLTXFileOpener) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { return t.OpenLTXFileFunc(ctx, level, minTXID, maxTXID, offset, size) } // errorAfterN is a reader that returns data normally for the first n bytes, // then returns the specified error. This simulates a connection that drops // mid-transfer. type errorAfterN struct { data []byte n int // bytes to return before erroring pos int err error } func (r *errorAfterN) Read(p []byte) (int, error) { if r.pos >= r.n { return 0, r.err } remaining := r.n - r.pos if len(p) > remaining { p = p[:remaining] } n := copy(p, r.data[r.pos:r.pos+len(p)]) r.pos += n return n, nil } ================================================ FILE: internal/testingutil/testingutil.go ================================================ package testingutil import ( "context" "database/sql" "flag" "fmt" "io" "log/slog" "math/rand/v2" "net" "os" "path" "path/filepath" "strings" "testing" sftpserver "github.com/pkg/sftp" "golang.org/x/crypto/ssh" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/abs" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/gs" "github.com/benbjohnson/litestream/internal" "github.com/benbjohnson/litestream/nats" "github.com/benbjohnson/litestream/oss" "github.com/benbjohnson/litestream/s3" "github.com/benbjohnson/litestream/sftp" "github.com/benbjohnson/litestream/webdav" ) const ( defaultTigrisEndpoint = "https://fly.storage.tigris.dev" defaultTigrisRegion = "auto" defaultTigrisBucket = "litestream-dev" defaultTigrisPathRoot = "integration-tests" ) var ( // Enables integration tests. integration = flag.Bool("integration", false, "") // Enables specific types of replicas to be tested. replicaClientTypes = flag.String("replica-clients", "file", "") // Sets the log level for the tests. logLevel = flag.String("log.level", "debug", "") ) // S3 settings var ( // Replica client settings s3AccessKeyID = flag.String("s3-access-key-id", os.Getenv("LITESTREAM_S3_ACCESS_KEY_ID"), "") s3SecretAccessKey = flag.String("s3-secret-access-key", os.Getenv("LITESTREAM_S3_SECRET_ACCESS_KEY"), "") s3Region = flag.String("s3-region", os.Getenv("LITESTREAM_S3_REGION"), "") s3Bucket = flag.String("s3-bucket", os.Getenv("LITESTREAM_S3_BUCKET"), "") s3Path = flag.String("s3-path", os.Getenv("LITESTREAM_S3_PATH"), "") s3Endpoint = flag.String("s3-endpoint", os.Getenv("LITESTREAM_S3_ENDPOINT"), "") s3ForcePathStyle = flag.Bool("s3-force-path-style", os.Getenv("LITESTREAM_S3_FORCE_PATH_STYLE") == "true", "") s3SkipVerify = flag.Bool("s3-skip-verify", os.Getenv("LITESTREAM_S3_SKIP_VERIFY") == "true", "") ) // Tigris settings (S3-compatible) var ( tigrisAccessKeyID = flag.String("tigris-access-key-id", os.Getenv("LITESTREAM_TIGRIS_ACCESS_KEY_ID"), "") tigrisSecretAccessKey = flag.String("tigris-secret-access-key", os.Getenv("LITESTREAM_TIGRIS_SECRET_ACCESS_KEY"), "") ) // Cloudflare R2 settings (S3-compatible) var ( r2AccessKeyID = flag.String("r2-access-key-id", os.Getenv("LITESTREAM_R2_ACCESS_KEY_ID"), "") r2SecretAccessKey = flag.String("r2-secret-access-key", os.Getenv("LITESTREAM_R2_SECRET_ACCESS_KEY"), "") r2Endpoint = flag.String("r2-endpoint", os.Getenv("LITESTREAM_R2_ENDPOINT"), "") r2Bucket = flag.String("r2-bucket", os.Getenv("LITESTREAM_R2_BUCKET"), "") ) // Backblaze B2 settings (S3-compatible) var ( b2KeyID = flag.String("b2-key-id", os.Getenv("LITESTREAM_B2_KEY_ID"), "") b2ApplicationKey = flag.String("b2-application-key", os.Getenv("LITESTREAM_B2_APPLICATION_KEY"), "") b2Endpoint = flag.String("b2-endpoint", os.Getenv("LITESTREAM_B2_ENDPOINT"), "") b2Bucket = flag.String("b2-bucket", os.Getenv("LITESTREAM_B2_BUCKET"), "") ) // Google cloud storage settings var ( gsBucket = flag.String("gs-bucket", os.Getenv("LITESTREAM_GS_BUCKET"), "") gsPath = flag.String("gs-path", os.Getenv("LITESTREAM_GS_PATH"), "") ) // Azure blob storage settings var ( absAccountName = flag.String("abs-account-name", os.Getenv("LITESTREAM_ABS_ACCOUNT_NAME"), "") absAccountKey = flag.String("abs-account-key", os.Getenv("LITESTREAM_ABS_ACCOUNT_KEY"), "") absSASToken = flag.String("abs-sas-token", os.Getenv("LITESTREAM_ABS_SAS_TOKEN"), "") absBucket = flag.String("abs-bucket", os.Getenv("LITESTREAM_ABS_BUCKET"), "") absPath = flag.String("abs-path", os.Getenv("LITESTREAM_ABS_PATH"), "") ) // SFTP settings var ( sftpHost = flag.String("sftp-host", os.Getenv("LITESTREAM_SFTP_HOST"), "") sftpUser = flag.String("sftp-user", os.Getenv("LITESTREAM_SFTP_USER"), "") sftpPassword = flag.String("sftp-password", os.Getenv("LITESTREAM_SFTP_PASSWORD"), "") sftpKeyPath = flag.String("sftp-key-path", os.Getenv("LITESTREAM_SFTP_KEY_PATH"), "") sftpPath = flag.String("sftp-path", os.Getenv("LITESTREAM_SFTP_PATH"), "") ) // WebDAV settings var ( webdavURL = flag.String("webdav-url", os.Getenv("LITESTREAM_WEBDAV_URL"), "") webdavUsername = flag.String("webdav-username", os.Getenv("LITESTREAM_WEBDAV_USERNAME"), "") webdavPassword = flag.String("webdav-password", os.Getenv("LITESTREAM_WEBDAV_PASSWORD"), "") webdavPath = flag.String("webdav-path", os.Getenv("LITESTREAM_WEBDAV_PATH"), "") ) // NATS settings var ( natsURL = flag.String("nats-url", os.Getenv("LITESTREAM_NATS_URL"), "") natsBucket = flag.String("nats-bucket", os.Getenv("LITESTREAM_NATS_BUCKET"), "") natsCreds = flag.String("nats-creds", os.Getenv("LITESTREAM_NATS_CREDS"), "") natsUsername = flag.String("nats-username", os.Getenv("LITESTREAM_NATS_USERNAME"), "") natsPassword = flag.String("nats-password", os.Getenv("LITESTREAM_NATS_PASSWORD"), "") ) // Alibaba Cloud OSS settings var ( ossAccessKeyID = flag.String("oss-access-key-id", os.Getenv("LITESTREAM_OSS_ACCESS_KEY_ID"), "") ossAccessKeySecret = flag.String("oss-access-key-secret", os.Getenv("LITESTREAM_OSS_ACCESS_KEY_SECRET"), "") ossRegion = flag.String("oss-region", os.Getenv("LITESTREAM_OSS_REGION"), "") ossBucket = flag.String("oss-bucket", os.Getenv("LITESTREAM_OSS_BUCKET"), "") ossPath = flag.String("oss-path", os.Getenv("LITESTREAM_OSS_PATH"), "") ossEndpoint = flag.String("oss-endpoint", os.Getenv("LITESTREAM_OSS_ENDPOINT"), "") ) func Integration() bool { return *integration } func ReplicaClientTypes() []string { return strings.Split(*replicaClientTypes, ",") } func NewDB(tb testing.TB, path string) *litestream.DB { tb.Helper() tb.Logf("db=%s", path) level := slog.LevelDebug if strings.EqualFold(*logLevel, "trace") { level = internal.LevelTrace } db := litestream.NewDB(path) db.Logger = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ Level: level, ReplaceAttr: internal.ReplaceAttr, })) return db } // MustOpenDBs returns a new instance of a DB & associated SQL DB. func MustOpenDBs(tb testing.TB) (*litestream.DB, *sql.DB) { tb.Helper() db := MustOpenDB(tb) return db, MustOpenSQLDB(tb, db.Path()) } // MustCloseDBs closes db & sqldb and removes the parent directory. func MustCloseDBs(tb testing.TB, db *litestream.DB, sqldb *sql.DB) { tb.Helper() MustCloseDB(tb, db) MustCloseSQLDB(tb, sqldb) } // MustOpenDB returns a new instance of a DB. func MustOpenDB(tb testing.TB) *litestream.DB { tb.Helper() dir := tb.TempDir() return MustOpenDBAt(tb, filepath.Join(dir, "db")) } // MustOpenDBAt returns a new instance of a DB for a given path. func MustOpenDBAt(tb testing.TB, path string) *litestream.DB { tb.Helper() db := NewDB(tb, path) db.MonitorInterval = 0 // disable background goroutine db.ShutdownSyncTimeout = 0 // disable shutdown sync retry for faster tests db.Replica = litestream.NewReplica(db) db.Replica.Client = NewFileReplicaClient(tb) db.Replica.MonitorEnabled = false // disable background goroutine if err := db.Open(); err != nil { tb.Fatal(err) } return db } // MustCloseDB closes db and removes its parent directory. func MustCloseDB(tb testing.TB, db *litestream.DB) { tb.Helper() if err := db.Close(context.Background()); err != nil && !strings.Contains(err.Error(), `database is closed`) && !strings.Contains(err.Error(), `file already closed`) { tb.Fatal(err) } else if err := os.RemoveAll(filepath.Dir(db.Path())); err != nil { tb.Fatal(err) } } // MustOpenSQLDB returns a database/sql DB. func MustOpenSQLDB(tb testing.TB, path string) *sql.DB { tb.Helper() d, err := sql.Open("sqlite", path) if err != nil { tb.Fatal(err) } else if _, err := d.ExecContext(context.Background(), `PRAGMA journal_mode = wal;`); err != nil { tb.Fatal(err) } else if _, err := d.ExecContext(context.Background(), `PRAGMA busy_timeout = 5000;`); err != nil { tb.Fatal(err) } return d } // MustCloseSQLDB closes a database/sql DB. func MustCloseSQLDB(tb testing.TB, d *sql.DB) { tb.Helper() if err := d.Close(); err != nil { tb.Fatal(err) } } // NewReplicaClient returns a new client for integration testing by type name. func NewReplicaClient(tb testing.TB, typ string) litestream.ReplicaClient { tb.Helper() switch typ { case file.ReplicaClientType: return NewFileReplicaClient(tb) case s3.ReplicaClientType: return NewS3ReplicaClient(tb) case gs.ReplicaClientType: return NewGSReplicaClient(tb) case abs.ReplicaClientType: return NewABSReplicaClient(tb) case sftp.ReplicaClientType: return NewSFTPReplicaClient(tb) case webdav.ReplicaClientType: return NewWebDAVReplicaClient(tb) case nats.ReplicaClientType: return NewNATSReplicaClient(tb) case oss.ReplicaClientType: return NewOSSReplicaClient(tb) case "tigris": return NewTigrisReplicaClient(tb) case "r2": return NewR2ReplicaClient(tb) case "b2": return NewB2ReplicaClient(tb) default: tb.Fatalf("invalid replica client type: %q", typ) return nil } } // NewFileReplicaClient returns a new client for integration testing. func NewFileReplicaClient(tb testing.TB) *file.ReplicaClient { tb.Helper() return file.NewReplicaClient(tb.TempDir()) } // NewS3ReplicaClient returns a new client for integration testing. func NewS3ReplicaClient(tb testing.TB) *s3.ReplicaClient { tb.Helper() c := s3.NewReplicaClient() c.AccessKeyID = *s3AccessKeyID c.SecretAccessKey = *s3SecretAccessKey c.Region = *s3Region c.Bucket = *s3Bucket c.Path = path.Join(*s3Path, fmt.Sprintf("%016x", rand.Uint64())) c.Endpoint = *s3Endpoint c.ForcePathStyle = *s3ForcePathStyle c.SkipVerify = *s3SkipVerify return c } // NewTigrisReplicaClient returns an S3 client configured for Fly.io Tigris. func NewTigrisReplicaClient(tb testing.TB) *s3.ReplicaClient { tb.Helper() if *tigrisAccessKeyID == "" || *tigrisSecretAccessKey == "" { tb.Skip("tigris credentials not configured (set LITESTREAM_TIGRIS_ACCESS_KEY_ID/SECRET_ACCESS_KEY)") } c := s3.NewReplicaClient() c.AccessKeyID = *tigrisAccessKeyID c.SecretAccessKey = *tigrisSecretAccessKey c.Region = defaultTigrisRegion c.Bucket = defaultTigrisBucket c.Path = path.Join(defaultTigrisPathRoot, fmt.Sprintf("%016x", rand.Uint64())) c.Endpoint = defaultTigrisEndpoint c.ForcePathStyle = true c.RequireContentMD5 = false return c } // NewR2ReplicaClient returns an S3 client configured for Cloudflare R2. // Skips the test if R2 credentials are not configured. func NewR2ReplicaClient(tb testing.TB) *s3.ReplicaClient { tb.Helper() if *r2AccessKeyID == "" || *r2SecretAccessKey == "" { tb.Skip("r2 credentials not configured (set LITESTREAM_R2_ACCESS_KEY_ID/SECRET_ACCESS_KEY)") } if *r2Endpoint == "" { tb.Skip("r2 endpoint not configured (set LITESTREAM_R2_ENDPOINT)") } if *r2Bucket == "" { tb.Skip("r2 bucket not configured (set LITESTREAM_R2_BUCKET)") } c := s3.NewReplicaClient() c.AccessKeyID = *r2AccessKeyID c.SecretAccessKey = *r2SecretAccessKey c.Region = "auto" c.Bucket = *r2Bucket c.Path = path.Join("integration-tests", fmt.Sprintf("%016x", rand.Uint64())) c.Endpoint = *r2Endpoint c.ForcePathStyle = true c.SignPayload = true return c } // NewB2ReplicaClient returns a new Backblaze B2 client for integration testing. // B2 uses S3-compatible API with path-style URLs and signed payloads. func NewB2ReplicaClient(tb testing.TB) *s3.ReplicaClient { tb.Helper() if *b2KeyID == "" || *b2ApplicationKey == "" { tb.Skip("b2 credentials not configured (set LITESTREAM_B2_KEY_ID/APPLICATION_KEY)") } if *b2Endpoint == "" { tb.Skip("b2 endpoint not configured (set LITESTREAM_B2_ENDPOINT)") } if *b2Bucket == "" { tb.Skip("b2 bucket not configured (set LITESTREAM_B2_BUCKET)") } c := s3.NewReplicaClient() c.AccessKeyID = *b2KeyID c.SecretAccessKey = *b2ApplicationKey c.Region = "us-west-002" // B2 uses region in endpoint format c.Bucket = *b2Bucket c.Path = path.Join("integration-tests", fmt.Sprintf("%016x", rand.Uint64())) c.Endpoint = *b2Endpoint c.ForcePathStyle = true c.SignPayload = true return c } // NewGSReplicaClient returns a new client for integration testing. func NewGSReplicaClient(tb testing.TB) *gs.ReplicaClient { tb.Helper() // Log basic diagnostic information for integration test troubleshooting tb.Logf("GCS Integration Test Setup:") credsSet := "not set" if os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") != "" { credsSet = "set" } tb.Logf(" GOOGLE_APPLICATION_CREDENTIALS: %s", credsSet) tb.Logf(" LITESTREAM_GS_BUCKET: %s", *gsBucket) tb.Logf(" LITESTREAM_GS_PATH: %s", *gsPath) c := gs.NewReplicaClient() c.Bucket = *gsBucket c.Path = path.Join(*gsPath, fmt.Sprintf("%016x", rand.Uint64())) // Test basic connectivity ctx := context.Background() if err := c.Init(ctx); err != nil { tb.Logf("GCS client initialization failed: %v", err) tb.Logf("This may indicate credential or project issues") return c // Return anyway to let the actual test show the detailed error } tb.Logf("GCS client initialized successfully") return c } // NewABSReplicaClient returns a new client for integration testing. func NewABSReplicaClient(tb testing.TB) *abs.ReplicaClient { tb.Helper() c := abs.NewReplicaClient() c.AccountName = *absAccountName c.AccountKey = *absAccountKey c.SASToken = *absSASToken c.Bucket = *absBucket c.Path = path.Join(*absPath, fmt.Sprintf("%016x", rand.Uint64())) return c } // NewSFTPReplicaClient returns a new client for integration testing. func NewSFTPReplicaClient(tb testing.TB) *sftp.ReplicaClient { tb.Helper() c := sftp.NewReplicaClient() c.Host = *sftpHost c.User = *sftpUser c.Password = *sftpPassword c.KeyPath = *sftpKeyPath c.Path = path.Join(*sftpPath, fmt.Sprintf("%016x", rand.Uint64())) return c } // NewWebDAVReplicaClient returns a new client for integration testing. func NewWebDAVReplicaClient(tb testing.TB) *webdav.ReplicaClient { tb.Helper() c := webdav.NewReplicaClient() c.URL = *webdavURL c.Username = *webdavUsername c.Password = *webdavPassword c.Path = path.Join(*webdavPath, fmt.Sprintf("%016x", rand.Uint64())) return c } // NewNATSReplicaClient returns a new client for integration testing. func NewNATSReplicaClient(tb testing.TB) *nats.ReplicaClient { tb.Helper() c := nats.NewReplicaClient() c.URL = *natsURL c.BucketName = *natsBucket c.Creds = *natsCreds c.Username = *natsUsername c.Password = *natsPassword return c } // NewOSSReplicaClient returns a new client for integration testing. func NewOSSReplicaClient(tb testing.TB) *oss.ReplicaClient { tb.Helper() c := oss.NewReplicaClient() c.AccessKeyID = *ossAccessKeyID c.AccessKeySecret = *ossAccessKeySecret c.Region = *ossRegion c.Bucket = *ossBucket c.Path = path.Join(*ossPath, fmt.Sprintf("%016x", rand.Uint64())) c.Endpoint = *ossEndpoint return c } // MustDeleteAll deletes all objects under the client's path. func MustDeleteAll(tb testing.TB, c litestream.ReplicaClient) { tb.Helper() if err := c.DeleteAll(context.Background()); err != nil { tb.Fatalf("cannot delete all: %s", err) } switch c := c.(type) { case *sftp.ReplicaClient: if err := c.Cleanup(context.Background()); err != nil { tb.Fatalf("cannot cleanup sftp: %s", err) } } } func MockSFTPServer(t *testing.T, hostKey ssh.Signer) string { config := &ssh.ServerConfig{NoClientAuth: true} config.AddHostKey(hostKey) listener, err := net.Listen("tcp", "127.0.0.1:0") // random available port if err != nil { t.Fatal(err) } go func() { for { conn, err := listener.Accept() if err != nil { return } go func() { _, chans, reqs, err := ssh.NewServerConn(conn, config) if err != nil { return } go ssh.DiscardRequests(reqs) for ch := range chans { if ch.ChannelType() != "session" { ch.Reject(ssh.UnknownChannelType, "unsupported") continue } channel, requests, err := ch.Accept() if err != nil { return } go func(in <-chan *ssh.Request) { for req := range in { if req.Type == "subsystem" && string(req.Payload[4:]) == "sftp" { req.Reply(true, nil) server, err := sftpserver.NewServer(channel) if err != nil { return } if err := server.Serve(); err != nil && err != io.EOF { t.Logf("SFTP server error: %v", err) } return } req.Reply(false, nil) } }(requests) } }() } }() return listener.Addr().String() } ================================================ FILE: leaser.go ================================================ package litestream import ( "context" "errors" "fmt" "time" ) var ErrLeaseNotHeld = errors.New("lease not held") type LeaseExistsError struct { Owner string ExpiresAt time.Time } func (e *LeaseExistsError) Error() string { if e.Owner != "" { return fmt.Sprintf("lease already held by %s until %s", e.Owner, e.ExpiresAt.Format(time.RFC3339)) } return fmt.Sprintf("lease already held until %s", e.ExpiresAt.Format(time.RFC3339)) } type Leaser interface { Type() string AcquireLease(ctx context.Context) (*Lease, error) RenewLease(ctx context.Context, lease *Lease) (*Lease, error) ReleaseLease(ctx context.Context, lease *Lease) error } type Lease struct { Generation int64 `json:"generation"` ExpiresAt time.Time `json:"expires_at"` Owner string `json:"owner,omitempty"` ETag string `json:"-"` } func (l *Lease) IsExpired() bool { return time.Now().After(l.ExpiresAt) } func (l *Lease) TTL() time.Duration { return time.Until(l.ExpiresAt) } ================================================ FILE: litestream.go ================================================ package litestream import ( "database/sql" "encoding/binary" "errors" "io" "os" "path" "path/filepath" "strconv" "strings" "github.com/superfly/ltx" _ "modernc.org/sqlite" ) // Naming constants. const ( MetaDirSuffix = "-litestream" ) // SQLite checkpoint modes. const ( CheckpointModePassive = "PASSIVE" CheckpointModeFull = "FULL" CheckpointModeRestart = "RESTART" CheckpointModeTruncate = "TRUNCATE" ) // Litestream errors. var ( ErrNoSnapshots = errors.New("no snapshots available") ErrChecksumMismatch = errors.New("invalid replica, checksum mismatch") ErrLTXCorrupted = errors.New("ltx file corrupted") ErrLTXMissing = errors.New("ltx file missing") ) // LTXError provides detailed context for LTX file errors with recovery hints. type LTXError struct { Op string // Operation that failed (e.g., "open", "read", "validate") Path string // File path Level int // LTX level (0 = L0, etc.) MinTXID uint64 // Minimum transaction ID MaxTXID uint64 // Maximum transaction ID Err error // Underlying error Hint string // Recovery hint for users } func (e *LTXError) Error() string { if e.Path != "" { return e.Op + " ltx file " + e.Path + ": " + e.Err.Error() } return e.Op + " ltx file: " + e.Err.Error() } func (e *LTXError) Unwrap() error { return e.Err } // NewLTXError creates a new LTX error with appropriate hints based on the error type. func NewLTXError(op, path string, level int, minTXID, maxTXID uint64, err error) *LTXError { ltxErr := <XError{ Op: op, Path: path, Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Err: err, } // Set appropriate hint based on error type if os.IsNotExist(err) || errors.Is(err, ErrLTXMissing) { ltxErr.Hint = "LTX file is missing. This can happen after VACUUM, manual checkpoint, or state corruption. " + "Run 'litestream reset ' or delete the .sqlite-litestream directory and restart." } else if errors.Is(err, ErrLTXCorrupted) || errors.Is(err, ErrChecksumMismatch) { ltxErr.Hint = "LTX file is corrupted. Delete the .sqlite-litestream directory and restart to recover from replica." } return ltxErr } // SQLite WAL constants. const ( WALHeaderChecksumOffset = 24 WALFrameHeaderChecksumOffset = 16 ) var ( // LogWriter is the destination writer for all logging. LogWriter = os.Stdout // LogFlags are the flags passed to log.New(). LogFlags = 0 ) // Checksum computes a running SQLite checksum over a byte slice. func Checksum(bo binary.ByteOrder, s0, s1 uint32, b []byte) (uint32, uint32) { assert(len(b)%8 == 0, "misaligned checksum byte slice") // Iterate over 8-byte units and compute checksum. for i := 0; i < len(b); i += 8 { s0 += bo.Uint32(b[i:]) + s1 s1 += bo.Uint32(b[i+4:]) + s0 } return s0, s1 } const ( // WALHeaderSize is the size of the WAL header, in bytes. WALHeaderSize = 32 // WALFrameHeaderSize is the size of the WAL frame header, in bytes. WALFrameHeaderSize = 24 ) // rollback rolls back tx. Ignores already-rolled-back errors. func rollback(tx *sql.Tx) error { if err := tx.Rollback(); err != nil && !strings.Contains(err.Error(), `transaction has already been committed or rolled back`) { return err } return nil } // readWALHeader returns the header read from a WAL file. func readWALHeader(filename string) ([]byte, error) { f, err := os.Open(filename) if err != nil { return nil, err } defer f.Close() buf := make([]byte, WALHeaderSize) n, err := io.ReadFull(f, buf) return buf[:n], err } // readWALFileAt reads a slice from a file. Do not use this with database files // as it causes problems with non-OFD locks. func readWALFileAt(filename string, offset, n int64) ([]byte, error) { f, err := os.Open(filename) if err != nil { return nil, err } defer f.Close() buf := make([]byte, n) if n, err := f.ReadAt(buf, offset); err != nil { return buf[:n], err } else if n < len(buf) { return buf[:n], io.ErrUnexpectedEOF } return buf, nil } // removeTmpFiles recursively finds and removes .tmp files. func removeTmpFiles(root string) error { return filepath.Walk(root, func(path string, info os.FileInfo, err error) error { switch { case err != nil: return nil // skip errored files case info.IsDir(): return nil // skip directories case !strings.HasSuffix(path, ".tmp"): return nil // skip non-temp files default: return os.Remove(path) } }) } // LTXDir returns the path to an LTX directory. func LTXDir(root string) string { return path.Join(root, "ltx") } // LTXLevelDir returns the path to an LTX level directory. func LTXLevelDir(root string, level int) string { return path.Join(LTXDir(root), strconv.Itoa(level)) } // LTXFilePath returns the path to a single LTX file. func LTXFilePath(root string, level int, minTXID, maxTXID ltx.TXID) string { return path.Join(LTXLevelDir(root, level), ltx.FormatFilename(minTXID, maxTXID)) } func assert(condition bool, message string) { if !condition { panic("assertion failed: " + message) } } ================================================ FILE: litestream_test.go ================================================ package litestream_test import ( "encoding/binary" "encoding/hex" "errors" "os" "strings" "testing" "github.com/superfly/ltx" _ "modernc.org/sqlite" "github.com/benbjohnson/litestream" ) func TestChecksum(t *testing.T) { // Ensure a WAL header, frame header, & frame data can be checksummed in one pass. t.Run("OnePass", func(t *testing.T) { input, err := hex.DecodeString("377f0682002de218000010000000000052382eac857b1a4e00000002000000020d000000080fe0000ffc0ff80ff40ff00fec0fe80fe40fe000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000208020902070209020602090205020902040209020302090202020902010209") if err != nil { t.Fatal(err) } s0, s1 := litestream.Checksum(binary.LittleEndian, 0, 0, input) if got, want := [2]uint32{s0, s1}, [2]uint32{0xdc2f3e84, 0x540488d3}; got != want { t.Fatalf("Checksum()=%x, want %x", got, want) } }) // Ensure we get the same result as OnePass even if we split up into multiple calls. t.Run("Incremental", func(t *testing.T) { // Compute checksum for beginning of WAL header. s0, s1 := litestream.Checksum(binary.LittleEndian, 0, 0, MustDecodeHexString("377f0682002de218000010000000000052382eac857b1a4e")) if got, want := [2]uint32{s0, s1}, [2]uint32{0x81153b65, 0x87178e8f}; got != want { t.Fatalf("Checksum()=%x, want %x", got, want) } // Continue checksum with WAL frame header & frame contents. s0a, s1a := litestream.Checksum(binary.LittleEndian, s0, s1, MustDecodeHexString("0000000200000002")) s0b, s1b := litestream.Checksum(binary.LittleEndian, s0a, s1a, MustDecodeHexString(`0d000000080fe0000ffc0ff80ff40ff00fec0fe80fe40fe000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000208020902070209020602090205020902040209020302090202020902010209`)) if got, want := [2]uint32{s0b, s1b}, [2]uint32{0xdc2f3e84, 0x540488d3}; got != want { t.Fatalf("Checksum()=%x, want %x", got, want) } }) } func TestLTXDir(t *testing.T) { if got, want := litestream.LTXDir("foo"), "foo/ltx"; got != want { t.Fatalf("LTXDir()=%v, want %v", got, want) } } func TestLTXLevelDir(t *testing.T) { if got, want := litestream.LTXLevelDir("foo", 0), "foo/ltx/0"; got != want { t.Fatalf("LTXLevelDir()=%v, want %v", got, want) } } func LTXFilePath(t *testing.T) { t.Helper() if got, want := litestream.LTXFilePath("foo", 0, ltx.TXID(100), ltx.TXID(200)), "-"; got != want { t.Fatalf("LTXPath()=%v, want %v", got, want) } } func MustDecodeHexString(s string) []byte { b, err := hex.DecodeString(s) if err != nil { panic(err) } return b } func TestNewLTXError(t *testing.T) { t.Run("MissingFile", func(t *testing.T) { err := litestream.NewLTXError("open", "/path/to/file.ltx", 0, 1, 1, os.ErrNotExist) if err.Hint == "" { t.Fatal("expected hint for missing file error") } if !strings.Contains(err.Hint, "missing") { t.Errorf("hint should mention missing file, got: %s", err.Hint) } if !strings.Contains(err.Hint, "litestream reset") { t.Errorf("hint should mention reset command, got: %s", err.Hint) } }) t.Run("CorruptedFile", func(t *testing.T) { err := litestream.NewLTXError("decode", "/path/to/file.ltx", 0, 1, 1, litestream.ErrLTXCorrupted) if err.Hint == "" { t.Fatal("expected hint for corrupted file error") } if !strings.Contains(err.Hint, "corrupted") { t.Errorf("hint should mention corruption, got: %s", err.Hint) } }) t.Run("ChecksumMismatch", func(t *testing.T) { err := litestream.NewLTXError("validate", "/path/to/file.ltx", 0, 1, 1, litestream.ErrChecksumMismatch) if err.Hint == "" { t.Fatal("expected hint for checksum mismatch error") } }) t.Run("ErrorString", func(t *testing.T) { err := litestream.NewLTXError("open", "/path/to/file.ltx", 0, 1, 1, os.ErrNotExist) errStr := err.Error() if !strings.Contains(errStr, "open") { t.Errorf("error should contain operation, got: %s", errStr) } if !strings.Contains(errStr, "/path/to/file.ltx") { t.Errorf("error should contain path, got: %s", errStr) } }) t.Run("Unwrap", func(t *testing.T) { underlying := errors.New("underlying error") err := litestream.NewLTXError("read", "/path/to/file.ltx", 0, 1, 1, underlying) if !errors.Is(err, underlying) { t.Error("LTXError should unwrap to underlying error") } }) } func TestLTXErrorHints(t *testing.T) { // Test that ErrLTXMissing also triggers appropriate hints t.Run("ErrLTXMissing", func(t *testing.T) { err := litestream.NewLTXError("open", "/path/to/file.ltx", 0, 1, 1, litestream.ErrLTXMissing) if err.Hint == "" { t.Fatal("expected hint for ErrLTXMissing") } if !strings.Contains(err.Hint, "litestream reset") { t.Errorf("hint should mention reset command, got: %s", err.Hint) } }) } ================================================ FILE: llms.txt ================================================ # Litestream Disaster recovery tool for SQLite. Replicates WAL changes to S3, GCS, Azure, SFTP, or local filesystem. ## Quick Start for AI Contributors 1. Read [AI_PR_GUIDE.md](AI_PR_GUIDE.md) - PR quality requirements 2. Read [AGENTS.md](AGENTS.md) - Project overview and checklist 3. Check [CONTRIBUTING.md](CONTRIBUTING.md) - What we accept 4. Show investigation evidence in PRs ## PR Checklist - [ ] Evidence of problem (logs, file patterns) - [ ] Clear scope (what PR does/doesn't do) - [ ] Runnable test commands - [ ] Race detector tested (`go test -race`) ## Documentation | Document | Purpose | |----------|---------| | [AGENTS.md](AGENTS.md) | Project overview, critical rules | | [AI_PR_GUIDE.md](AI_PR_GUIDE.md) | PR templates, common mistakes | | [docs/PATTERNS.md](docs/PATTERNS.md) | Code patterns and anti-patterns | | [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) | Component details | | [docs/SQLITE_INTERNALS.md](docs/SQLITE_INTERNALS.md) | WAL format, 1GB lock page | | [docs/LTX_FORMAT.md](docs/LTX_FORMAT.md) | Replication format | | [docs/TESTING_GUIDE.md](docs/TESTING_GUIDE.md) | Test strategies | | [docs/REPLICA_CLIENT_GUIDE.md](docs/REPLICA_CLIENT_GUIDE.md) | Storage backends | ## Core Files | File | Purpose | |------|---------| | `db.go` | Database monitoring, WAL, checkpoints | | `replica.go` | Replication management | | `store.go` | Multi-database coordination | | `replica_client.go` | Storage backend interface | ## Storage Backends - `s3/replica_client.go` - AWS S3 - `gs/replica_client.go` - Google Cloud Storage - `abs/replica_client.go` - Azure Blob Storage - `sftp/replica_client.go` - SFTP - `file/replica_client.go` - Local filesystem - `nats/replica_client.go` - NATS JetStream ## Critical Concepts - **Lock page at 1GB** - Always skip page at 0x40000000 - **LTX files are immutable** - Never modify after creation - **Single replica per DB** - One destination per database - **Layer boundaries** - DB handles state, Replica handles replication ## Build ```bash go build -o bin/litestream ./cmd/litestream go test -race -v ./... pre-commit run --all-files ``` ================================================ FILE: log.go ================================================ package litestream const ( LogKeySystem = "system" LogKeySubsystem = "subsystem" LogKeyDB = "db" ) const ( LogSystemStore = "store" LogSystemServer = "server" ) const ( LogSubsystemCompactor = "compactor" LogSubsystemWALReader = "wal-reader" ) ================================================ FILE: mock/replica_client.go ================================================ package mock import ( "context" "io" "log/slog" "github.com/superfly/ltx" "github.com/benbjohnson/litestream" ) var _ litestream.ReplicaClient = (*ReplicaClient)(nil) type ReplicaClient struct { InitFunc func(ctx context.Context) error DeleteAllFunc func(ctx context.Context) error LTXFilesFunc func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) OpenLTXFileFunc func(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) WriteLTXFileFunc func(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) DeleteLTXFilesFunc func(ctx context.Context, a []*ltx.FileInfo) error } func (c *ReplicaClient) Type() string { return "mock" } func (c *ReplicaClient) Init(ctx context.Context) error { if c.InitFunc != nil { return c.InitFunc(ctx) } return nil } func (c *ReplicaClient) DeleteAll(ctx context.Context) error { return c.DeleteAllFunc(ctx) } func (c *ReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { return c.LTXFilesFunc(ctx, level, seek, useMetadata) } func (c *ReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { return c.OpenLTXFileFunc(ctx, level, minTXID, maxTXID, offset, size) } func (c *ReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { return c.WriteLTXFileFunc(ctx, level, minTXID, maxTXID, r) } func (c *ReplicaClient) DeleteLTXFiles(ctx context.Context, a []*ltx.FileInfo) error { return c.DeleteLTXFilesFunc(ctx, a) } func (c *ReplicaClient) SetLogger(_ *slog.Logger) {} ================================================ FILE: nats/replica_client.go ================================================ package nats import ( "bytes" "context" "errors" "fmt" "io" "log/slog" "net/url" "os" "sort" "strconv" "strings" "sync" "time" "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" "github.com/superfly/ltx" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/internal" ) func init() { litestream.RegisterReplicaClientFactory("nats", NewReplicaClientFromURL) } // ReplicaClientType is the client type for this package. const ReplicaClientType = "nats" // HeaderKeyTimestamp is the header key for storing LTX file timestamps in NATS object headers. const HeaderKeyTimestamp = "Litestream-Timestamp" var _ litestream.ReplicaClient = (*ReplicaClient)(nil) // ReplicaClient is a client for writing LTX files to NATS JetStream Object Store. type ReplicaClient struct { mu sync.Mutex logger *slog.Logger // NATS connection and JetStream context nc *nats.Conn js jetstream.JetStream objectStore jetstream.ObjectStore // Configuration URL string // NATS server URL BucketName string // Object store bucket name Path string // Base path for LTX files within the bucket JWT string // JWT token for authentication Seed string // Seed for JWT authentication Creds string // Credentials file path NKey string // NKey for authentication Username string // Username for authentication Password string // Password for authentication Token string // Token for authentication TLS bool // Enable TLS RootCAs []string // Root CA certificates ClientCert string // Client certificate file path ClientKey string // Client key file path // Note: Bucket configuration (replicas, storage, TTL, etc.) should be // managed externally via NATS CLI or API, not by Litestream // Connection options MaxReconnects int // Maximum reconnection attempts (-1 for unlimited) ReconnectWait time.Duration // Wait time between reconnection attempts ReconnectJitter time.Duration // Random jitter for reconnection Timeout time.Duration // Connection timeout PingInterval time.Duration // Ping interval MaxPingsOut int // Maximum number of pings without response ReconnectBufSize int // Reconnection buffer size UserJWT func() (string, error) // JWT callback SigCB func([]byte) ([]byte, error) // Signature callback } // NewReplicaClient returns a new instance of ReplicaClient. func NewReplicaClient() *ReplicaClient { return &ReplicaClient{ logger: slog.Default().WithGroup(ReplicaClientType), MaxReconnects: -1, // Unlimited ReconnectWait: 2 * time.Second, Timeout: 10 * time.Second, PingInterval: 2 * time.Minute, MaxPingsOut: 2, ReconnectBufSize: 8 * 1024 * 1024, // 8MB } } func (c *ReplicaClient) SetLogger(logger *slog.Logger) { c.logger = logger.WithGroup(ReplicaClientType) } // NewReplicaClientFromURL creates a new ReplicaClient from URL components. // This is used by the replica client factory registration. // URL format: nats://[user:pass@]host[:port]/bucket func NewReplicaClientFromURL(scheme, host, urlPath string, query url.Values, userinfo *url.Userinfo) (litestream.ReplicaClient, error) { client := NewReplicaClient() // Reconstruct URL without bucket path if host != "" { client.URL = fmt.Sprintf("nats://%s", host) } // Extract credentials from userinfo if present if userinfo != nil { client.Username = userinfo.Username() client.Password, _ = userinfo.Password() } // Extract bucket name from path bucket := strings.Trim(urlPath, "/") if bucket == "" { return nil, fmt.Errorf("bucket required for nats replica URL") } client.BucketName = bucket return client, nil } // Type returns "nats" as the client type. func (c *ReplicaClient) Type() string { return ReplicaClientType } // Init initializes the connection to NATS JetStream. No-op if already initialized. func (c *ReplicaClient) Init(ctx context.Context) error { c.mu.Lock() defer c.mu.Unlock() if c.nc != nil { return nil } if err := c.connect(ctx); err != nil { return fmt.Errorf("nats: failed to connect: %w", err) } if err := c.initObjectStore(ctx); err != nil { return fmt.Errorf("nats: failed to initialize object store: %w", err) } return nil } // connect establishes a connection to NATS server with proper configuration. func (c *ReplicaClient) connect(_ context.Context) error { opts := []nats.Option{ nats.MaxReconnects(c.MaxReconnects), nats.ReconnectWait(c.ReconnectWait), nats.ReconnectJitter(c.ReconnectJitter, c.ReconnectJitter*2), nats.Timeout(c.Timeout), nats.PingInterval(c.PingInterval), nats.MaxPingsOutstanding(c.MaxPingsOut), nats.ReconnectBufSize(c.ReconnectBufSize), } // Authentication options switch { case c.JWT != "" && c.Seed != "": opts = append(opts, nats.UserJWTAndSeed(c.JWT, c.Seed)) case c.Creds != "": opts = append(opts, nats.UserCredentials(c.Creds)) case c.NKey != "": opts = append(opts, nats.Nkey(c.NKey, c.SigCB)) case c.Username != "" && c.Password != "": opts = append(opts, nats.UserInfo(c.Username, c.Password)) case c.Token != "": opts = append(opts, nats.Token(c.Token)) } // JWT callback if c.UserJWT != nil { opts = append(opts, nats.UserJWT(c.UserJWT, c.SigCB)) } // TLS configuration if c.ClientCert != "" && c.ClientKey != "" { opts = append(opts, nats.ClientCert(c.ClientCert, c.ClientKey)) } if len(c.RootCAs) > 0 { opts = append(opts, nats.RootCAs(c.RootCAs...)) } // Note: NATS Connect doesn't directly support context cancellation during connection // The context parameter is preserved for potential future use url := c.URL if url == "" { url = nats.DefaultURL } nc, err := nats.Connect(url, opts...) if err != nil { return fmt.Errorf("failed to connect to NATS server: %w", err) } js, err := jetstream.New(nc) if err != nil { nc.Close() return fmt.Errorf("failed to create JetStream context: %w", err) } c.nc = nc c.js = js return nil } // initObjectStore retrieves the existing object store bucket. // The bucket must be pre-created using the NATS CLI or API. func (c *ReplicaClient) initObjectStore(ctx context.Context) error { if c.BucketName == "" { return fmt.Errorf("bucket name is required") } // Get existing object store - do not auto-create objectStore, err := c.js.ObjectStore(ctx, c.BucketName) if err != nil { return fmt.Errorf("failed to access object store bucket %q (bucket must be created beforehand): %w", c.BucketName, err) } c.objectStore = objectStore return nil } // Close closes the NATS connection. func (c *ReplicaClient) Close() error { c.mu.Lock() defer c.mu.Unlock() if c.nc != nil { c.nc.Close() c.nc = nil c.js = nil c.objectStore = nil } return nil } // ltxPath returns the object path for an LTX file. func (c *ReplicaClient) ltxPath(level int, minTXID, maxTXID ltx.TXID) string { return litestream.LTXFilePath(c.Path, level, minTXID, maxTXID) } // parseLTXPath parses an LTX object path and returns level, minTXID, and maxTXID. func (c *ReplicaClient) parseLTXPath(objPath string) (level int, minTXID, maxTXID ltx.TXID, err error) { // Remove the base path prefix if present if c.Path != "" && strings.HasPrefix(objPath, c.Path+"/") { objPath = strings.TrimPrefix(objPath, c.Path+"/") } // Expected format: "ltx//-.ltx" parts := strings.Split(objPath, "/") if len(parts) < 3 || parts[0] != "ltx" { return 0, 0, 0, fmt.Errorf("invalid ltx path: %s", objPath) } // Parse level if level, err = strconv.Atoi(parts[1]); err != nil { return 0, 0, 0, fmt.Errorf("invalid level in path %s: %w", objPath, err) } // Parse filename (minTXID-maxTXID.ltx) filename := parts[2] minTXIDVal, maxTXIDVal, err := ltx.ParseFilename(filename) if err != nil { return 0, 0, 0, fmt.Errorf("invalid filename in path %s: %w", objPath, err) } return level, minTXIDVal, maxTXIDVal, nil } // LTXFiles returns an iterator of all LTX files on the replica for a given level. // NATS always uses accurate timestamps from headers since they're included in LIST operations at zero cost. // The useMetadata parameter is ignored. func (c *ReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { if err := c.Init(ctx); err != nil { return nil, err } // List all objects in the store internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "LIST").Inc() objectList, err := c.objectStore.List(ctx) if err != nil { // NATS returns "no objects found" when bucket is empty, treat as empty list if strings.Contains(err.Error(), "no objects found") { objectList = nil // Empty list } else { return nil, fmt.Errorf("failed to list objects: %w", err) } } prefix := litestream.LTXLevelDir(c.Path, level) + "/" fileInfos := make([]*ltx.FileInfo, 0, len(objectList)) for _, objInfo := range objectList { // Filter by level prefix if !strings.HasPrefix(objInfo.Name, prefix) { continue } fileLevel, minTXID, maxTXID, err := c.parseLTXPath(objInfo.Name) if err != nil { continue // Skip invalid paths } if fileLevel != level { continue } // Apply seek filter if minTXID < seek { continue } // Always use accurate timestamp from headers since it's zero-cost // NATS includes headers in LIST operations, so no extra API call needed createdAt := objInfo.ModTime if objInfo.Headers != nil { if values, ok := objInfo.Headers[HeaderKeyTimestamp]; ok && len(values) > 0 { if parsed, err := time.Parse(time.RFC3339Nano, values[0]); err == nil { createdAt = parsed } } } fileInfos = append(fileInfos, <x.FileInfo{ Level: fileLevel, MinTXID: minTXID, MaxTXID: maxTXID, Size: int64(objInfo.Size), CreatedAt: createdAt, }) } // Sort by minTXID sort.Slice(fileInfos, func(i, j int) bool { return fileInfos[i].MinTXID < fileInfos[j].MinTXID }) return <xFileIterator{files: fileInfos, index: -1}, nil } // OpenLTXFile returns a reader that contains an LTX file at a given TXID range. func (c *ReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { if err := c.Init(ctx); err != nil { return nil, err } objectPath := c.ltxPath(level, minTXID, maxTXID) objectResult, err := c.objectStore.Get(ctx, objectPath) if err != nil { if isNotFoundError(err) { return nil, os.ErrNotExist } return nil, fmt.Errorf("failed to get object %s: %w", objectPath, err) } // Record metrics internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "GET").Inc() // Note: We can't get the size from NATS object reader directly, so we skip bytes counter // If offset is non-zero then discard the beginning bytes. if offset > 0 { if _, err := io.CopyN(io.Discard, objectResult, offset); err != nil { objectResult.Close() return nil, fmt.Errorf("failed to discard offset bytes: %w", err) } } // If size is non-zero then limit the reader to the size. if size > 0 { return internal.LimitReadCloser(objectResult, size), nil } return objectResult, nil } // WriteLTXFile writes an LTX file to the replica. func (c *ReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { if err := c.Init(ctx); err != nil { return nil, err } objectPath := c.ltxPath(level, minTXID, maxTXID) // Use TeeReader to peek at LTX header while preserving data for upload var buf bytes.Buffer teeReader := io.TeeReader(r, &buf) // Extract timestamp from LTX header hdr, _, err := ltx.PeekHeader(teeReader) if err != nil { return nil, fmt.Errorf("extract timestamp from LTX header: %w", err) } timestamp := time.UnixMilli(hdr.Timestamp).UTC() // Combine buffered data with rest of reader rc := internal.NewReadCounter(io.MultiReader(&buf, r)) // Store timestamp in NATS object headers for accurate timestamp retrieval objectInfo, err := c.objectStore.Put(ctx, jetstream.ObjectMeta{ Name: objectPath, Headers: map[string][]string{ HeaderKeyTimestamp: {timestamp.Format(time.RFC3339Nano)}, }, }, rc) if err != nil { return nil, fmt.Errorf("failed to put object %s: %w", objectPath, err) } // Record metrics internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "PUT").Inc() internal.OperationBytesCounterVec.WithLabelValues(ReplicaClientType, "PUT").Add(float64(objectInfo.Size)) return <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Size: int64(objectInfo.Size), CreatedAt: timestamp, }, nil } // DeleteLTXFiles deletes one or more LTX files. func (c *ReplicaClient) DeleteLTXFiles(ctx context.Context, a []*ltx.FileInfo) error { if err := c.Init(ctx); err != nil { return err } for _, fileInfo := range a { objectPath := c.ltxPath(fileInfo.Level, fileInfo.MinTXID, fileInfo.MaxTXID) c.logger.Debug("deleting ltx file", "level", fileInfo.Level, "minTXID", fileInfo.MinTXID, "maxTXID", fileInfo.MaxTXID, "path", objectPath) if err := c.objectStore.Delete(ctx, objectPath); err != nil { if !isNotFoundError(err) { return fmt.Errorf("failed to delete object %s: %w", objectPath, err) } } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "DELETE").Inc() } return nil } // DeleteAll deletes all files in the object store. func (c *ReplicaClient) DeleteAll(ctx context.Context) error { if err := c.Init(ctx); err != nil { return err } // List all objects in the bucket objectList, err := c.objectStore.List(ctx) if err != nil { // NATS returns "no objects found" when bucket is empty, treat as empty list if strings.Contains(err.Error(), "no objects found") { objectList = nil // Empty list, nothing to delete } else { return fmt.Errorf("failed to list all objects: %w", err) } } for _, objInfo := range objectList { if err := c.objectStore.Delete(ctx, objInfo.Name); err != nil { if !isNotFoundError(err) { return fmt.Errorf("failed to delete object %s: %w", objInfo.Name, err) } } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "DELETE").Inc() } return nil } // isNotFoundError checks if the error is a "not found" error. func isNotFoundError(err error) bool { return err != nil && (errors.Is(err, jetstream.ErrObjectNotFound) || strings.Contains(err.Error(), "not found")) } // ltxFileIterator implements ltx.FileIterator for NATS object store. type ltxFileIterator struct { files []*ltx.FileInfo index int err error } // Next advances the iterator to the next file. func (itr *ltxFileIterator) Next() bool { itr.index++ return itr.index < len(itr.files) } // Item returns the current file info. func (itr *ltxFileIterator) Item() *ltx.FileInfo { if itr.index < 0 || itr.index >= len(itr.files) { return nil } return itr.files[itr.index] } // Err returns any error that occurred during iteration. func (itr *ltxFileIterator) Err() error { return itr.err } // Close closes the iterator and returns any error that occurred during iteration. func (itr *ltxFileIterator) Close() error { return itr.err } ================================================ FILE: nats/replica_client_test.go ================================================ package nats import ( "testing" "time" "github.com/superfly/ltx" ) func TestReplicaClient_Type(t *testing.T) { client := NewReplicaClient() if got, want := client.Type(), "nats"; got != want { t.Fatalf("Type()=%s, want %s", got, want) } } func TestReplicaClient_ltxPath(t *testing.T) { client := NewReplicaClient() tests := []struct { level int minTXID ltx.TXID maxTXID ltx.TXID want string }{ {0, ltx.TXID(0x1000), ltx.TXID(0x2000), "ltx/0/0000000000001000-0000000000002000.ltx"}, {1, ltx.TXID(0xabcd), ltx.TXID(0xef01), "ltx/1/000000000000abcd-000000000000ef01.ltx"}, {255, ltx.TXID(0xffffffffffffffff), ltx.TXID(0xffffffffffffffff), "ltx/255/ffffffffffffffff-ffffffffffffffff.ltx"}, } for _, test := range tests { if got := client.ltxPath(test.level, test.minTXID, test.maxTXID); got != test.want { t.Errorf("ltxPath(%d, %x, %x)=%s, want %s", test.level, test.minTXID, test.maxTXID, got, test.want) } } } func TestReplicaClient_parseLTXPath(t *testing.T) { client := NewReplicaClient() tests := []struct { path string level int minTXID ltx.TXID maxTXID ltx.TXID wantErr bool }{ { path: "ltx/0/0000000000001000-0000000000002000.ltx", level: 0, minTXID: ltx.TXID(0x1000), maxTXID: ltx.TXID(0x2000), wantErr: false, }, { path: "ltx/1/000000000000abcd-000000000000ef01.ltx", level: 1, minTXID: ltx.TXID(0xabcd), maxTXID: ltx.TXID(0xef01), wantErr: false, }, { path: "invalid/path", wantErr: true, }, { path: "ltx/x/invalid-invalid.ltx", wantErr: true, }, { path: "ltx/0/invalid.ltx", wantErr: true, }, } for _, test := range tests { level, minTXID, maxTXID, err := client.parseLTXPath(test.path) if test.wantErr { if err == nil { t.Errorf("parseLTXPath(%s) expected error, got nil", test.path) } continue } if err != nil { t.Errorf("parseLTXPath(%s) unexpected error: %v", test.path, err) continue } if level != test.level { t.Errorf("parseLTXPath(%s) level=%d, want %d", test.path, level, test.level) } if minTXID != test.minTXID { t.Errorf("parseLTXPath(%s) minTXID=%x, want %x", test.path, minTXID, test.minTXID) } if maxTXID != test.maxTXID { t.Errorf("parseLTXPath(%s) maxTXID=%x, want %x", test.path, maxTXID, test.maxTXID) } } } func TestReplicaClient_isNotFoundError(t *testing.T) { tests := []struct { name string err error want bool }{ { name: "nil error", err: nil, want: false, }, { name: "not found error", err: &mockNotFoundError{}, want: true, }, { name: "other error", err: &mockOtherError{}, want: false, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { if got := isNotFoundError(test.err); got != test.want { t.Errorf("isNotFoundError() = %v, want %v", got, test.want) } }) } } func TestLtxFileIterator(t *testing.T) { files := []*ltx.FileInfo{ {Level: 0, MinTXID: 1, MaxTXID: 10, Size: 100}, {Level: 0, MinTXID: 11, MaxTXID: 20, Size: 200}, {Level: 0, MinTXID: 21, MaxTXID: 30, Size: 300}, } itr := <xFileIterator{files: files, index: -1} // Test initial state if item := itr.Item(); item != nil { t.Errorf("Item() before Next() should return nil, got %v", item) } // Test iteration var items []*ltx.FileInfo for itr.Next() { item := itr.Item() if item == nil { t.Fatal("Item() returned nil during valid iteration") } items = append(items, item) } if len(items) != len(files) { t.Errorf("Expected %d items, got %d", len(files), len(items)) } for i, item := range items { if item != files[i] { t.Errorf("Item %d: expected %v, got %v", i, files[i], item) } } // Test after iteration ends if itr.Next() { t.Error("Next() should return false after iteration ends") } // Test Close if err := itr.Close(); err != nil { t.Errorf("Close() returned error: %v", err) } // Test Err if err := itr.Err(); err != nil { t.Errorf("Err() returned error: %v", err) } } // Mock error types for testing type mockNotFoundError struct{} func (e *mockNotFoundError) Error() string { return "not found" } type mockOtherError struct{} func (e *mockOtherError) Error() string { return "some other error" } func TestReplicaClientDefaults(t *testing.T) { client := NewReplicaClient() // Test default values if client.MaxReconnects != -1 { t.Errorf("Expected MaxReconnects=-1, got %d", client.MaxReconnects) } if client.ReconnectWait != 2*time.Second { t.Errorf("Expected ReconnectWait=2s, got %v", client.ReconnectWait) } if client.Timeout != 10*time.Second { t.Errorf("Expected Timeout=10s, got %v", client.Timeout) } } ================================================ FILE: oss/replica_client.go ================================================ package oss import ( "bytes" "context" "errors" "fmt" "io" "log/slog" "net/url" "os" "path" "regexp" "strings" "sync" "time" "github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss" "github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials" "github.com/superfly/ltx" "golang.org/x/sync/errgroup" "golang.org/x/sync/semaphore" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/internal" ) func init() { litestream.RegisterReplicaClientFactory("oss", NewReplicaClientFromURL) } // ReplicaClientType is the client type for this package. const ReplicaClientType = "oss" // MetadataKeyTimestamp is the metadata key for storing LTX file timestamps in OSS. // Note: OSS SDK automatically adds "x-oss-meta-" prefix when setting metadata. const MetadataKeyTimestamp = "litestream-timestamp" // MaxKeys is the number of keys OSS can operate on per batch. const MaxKeys = 1000 // DefaultRegion is the region used if one is not specified. const DefaultRegion = "cn-hangzhou" // DefaultMetadataConcurrency is the default number of concurrent HeadObject calls // for fetching accurate timestamps during timestamp-based restore. const DefaultMetadataConcurrency = 50 var _ litestream.ReplicaClient = (*ReplicaClient)(nil) // ReplicaClient is a client for writing LTX files to Alibaba Cloud OSS. type ReplicaClient struct { mu sync.Mutex client *oss.Client uploader *oss.Uploader logger *slog.Logger // Alibaba Cloud authentication keys. AccessKeyID string AccessKeySecret string // OSS bucket information Region string Bucket string Path string Endpoint string // Upload configuration PartSize int64 // Part size for multipart uploads (default: 5MB) Concurrency int // Number of concurrent parts to upload (default: 3) // MetadataConcurrency controls parallel HeadObject calls for timestamp-based restore. // Higher values improve restore speed for large backup histories. // Default: 50 MetadataConcurrency int } // NewReplicaClient returns a new instance of ReplicaClient. func NewReplicaClient() *ReplicaClient { return &ReplicaClient{ logger: slog.Default().WithGroup(ReplicaClientType), } } func (c *ReplicaClient) SetLogger(logger *slog.Logger) { c.logger = logger.WithGroup(ReplicaClientType) } // NewReplicaClientFromURL creates a new ReplicaClient from URL components. // This is used by the replica client factory registration. // URL format: oss://bucket[.oss-region.aliyuncs.com]/path func NewReplicaClientFromURL(scheme, host, urlPath string, query url.Values, userinfo *url.Userinfo) (litestream.ReplicaClient, error) { client := NewReplicaClient() bucket, region, _ := ParseHost(host) if bucket == "" { return nil, fmt.Errorf("bucket required for oss replica URL") } client.Bucket = bucket client.Region = region client.Path = urlPath return client, nil } // Type returns "oss" as the client type. func (c *ReplicaClient) Type() string { return ReplicaClientType } // Init initializes the connection to OSS. No-op if already initialized. func (c *ReplicaClient) Init(ctx context.Context) (err error) { c.mu.Lock() defer c.mu.Unlock() if c.client != nil { return nil } // Validate required configuration if c.Bucket == "" { return fmt.Errorf("oss: bucket name is required") } // Use default region if not specified region := c.Region if region == "" { region = DefaultRegion } // Build configuration cfg := oss.LoadDefaultConfig() // Configure credentials if c.AccessKeyID != "" && c.AccessKeySecret != "" { cfg = cfg.WithCredentialsProvider( credentials.NewStaticCredentialsProvider(c.AccessKeyID, c.AccessKeySecret), ) } else { // Use environment variable credentials provider cfg = cfg.WithCredentialsProvider( credentials.NewEnvironmentVariableCredentialsProvider(), ) } // Configure region cfg = cfg.WithRegion(region) // Configure custom endpoint if specified if c.Endpoint != "" { endpoint := c.Endpoint // Add scheme if not present if !strings.HasPrefix(endpoint, "http://") && !strings.HasPrefix(endpoint, "https://") { endpoint = "https://" + endpoint } cfg = cfg.WithEndpoint(endpoint) } // Create OSS client c.client = oss.NewClient(cfg) // Create uploader with configurable part size and concurrency uploaderOpts := []func(*oss.UploaderOptions){} if c.PartSize > 0 { uploaderOpts = append(uploaderOpts, func(o *oss.UploaderOptions) { o.PartSize = c.PartSize }) } if c.Concurrency > 0 { uploaderOpts = append(uploaderOpts, func(o *oss.UploaderOptions) { o.ParallelNum = c.Concurrency }) } c.uploader = c.client.NewUploader(uploaderOpts...) return nil } // LTXFiles returns an iterator over all LTX files on the replica for the given level. // When useMetadata is true, fetches accurate timestamps from OSS metadata via HeadObject. // This uses parallel batched requests (controlled by MetadataConcurrency) to avoid hangs // with large backup histories (see issue #930). // When false, uses fast LastModified timestamps from LIST operation. func (c *ReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { if err := c.Init(ctx); err != nil { return nil, err } return newFileIterator(ctx, c, level, seek, useMetadata), nil } // OpenLTXFile returns a reader for an LTX file. // Returns os.ErrNotExist if no matching file is found. func (c *ReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { if err := c.Init(ctx); err != nil { return nil, err } // Build the key from the file info filename := ltx.FormatFilename(minTXID, maxTXID) key := c.ltxPath(level, filename) request := &oss.GetObjectRequest{ Bucket: oss.Ptr(c.Bucket), Key: oss.Ptr(key), } // Set range header if offset is specified if size > 0 { request.RangeBehavior = oss.Ptr("standard") request.Range = oss.Ptr(fmt.Sprintf("bytes=%d-%d", offset, offset+size-1)) } else if offset > 0 { request.RangeBehavior = oss.Ptr("standard") request.Range = oss.Ptr(fmt.Sprintf("bytes=%d-", offset)) } result, err := c.client.GetObject(ctx, request) if err != nil { if isNotExists(err) { return nil, os.ErrNotExist } return nil, fmt.Errorf("oss: get object %s: %w", key, err) } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "GET").Inc() return result.Body, nil } // WriteLTXFile writes an LTX file to the replica. // Extracts timestamp from LTX header and stores it in OSS metadata to preserve original creation time. // Uses multipart upload for large files via the uploader. func (c *ReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { if err := c.Init(ctx); err != nil { return nil, err } // Use TeeReader to peek at LTX header while preserving data for upload var buf bytes.Buffer teeReader := io.TeeReader(r, &buf) // Extract timestamp from LTX header hdr, _, err := ltx.PeekHeader(teeReader) if err != nil { return nil, fmt.Errorf("extract timestamp from LTX header: %w", err) } timestamp := time.UnixMilli(hdr.Timestamp).UTC() // Combine buffered data with rest of reader rc := internal.NewReadCounter(io.MultiReader(&buf, r)) filename := ltx.FormatFilename(minTXID, maxTXID) key := c.ltxPath(level, filename) // Store timestamp in OSS metadata for accurate timestamp retrieval metadata := map[string]string{ MetadataKeyTimestamp: timestamp.Format(time.RFC3339Nano), } // Use uploader for automatic multipart handling (files >5GB) result, err := c.uploader.UploadFrom(ctx, &oss.PutObjectRequest{ Bucket: oss.Ptr(c.Bucket), Key: oss.Ptr(key), Metadata: metadata, }, rc) if err != nil { return nil, fmt.Errorf("oss: upload to %s: %w", key, err) } // Build file info from the uploaded file info := <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Size: rc.N(), CreatedAt: timestamp, } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "PUT").Inc() internal.OperationBytesCounterVec.WithLabelValues(ReplicaClientType, "PUT").Add(float64(rc.N())) // ETag indicates successful upload if result.ETag == nil || *result.ETag == "" { return nil, fmt.Errorf("oss: upload failed: no ETag returned") } return info, nil } // DeleteLTXFiles deletes one or more LTX files. func (c *ReplicaClient) DeleteLTXFiles(ctx context.Context, a []*ltx.FileInfo) error { if err := c.Init(ctx); err != nil { return err } if len(a) == 0 { return nil } // Convert file infos to object identifiers objects := make([]oss.DeleteObject, 0, len(a)) for _, info := range a { filename := ltx.FormatFilename(info.MinTXID, info.MaxTXID) key := c.ltxPath(info.Level, filename) objects = append(objects, oss.DeleteObject{Key: oss.Ptr(key)}) c.logger.Debug("deleting ltx file", "level", info.Level, "minTXID", info.MinTXID, "maxTXID", info.MaxTXID, "key", key) } // Delete in batches for len(objects) > 0 { n := min(len(objects), MaxKeys) batch := objects[:n] request := &oss.DeleteMultipleObjectsRequest{ Bucket: oss.Ptr(c.Bucket), Objects: batch, } out, err := c.client.DeleteMultipleObjects(ctx, request) if err != nil { return fmt.Errorf("oss: delete batch of %d objects: %w", n, err) } else if err := deleteResultError(batch, out); err != nil { return err } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "DELETE").Inc() objects = objects[n:] } return nil } // DeleteAll deletes all files. func (c *ReplicaClient) DeleteAll(ctx context.Context) error { if err := c.Init(ctx); err != nil { return err } var objects []oss.DeleteObject // Create paginator for listing objects prefix := c.Path + "/" paginator := c.client.NewListObjectsV2Paginator(&oss.ListObjectsV2Request{ Bucket: oss.Ptr(c.Bucket), Prefix: oss.Ptr(prefix), }) // Iterate through all pages for paginator.HasNext() { page, err := paginator.NextPage(ctx) if err != nil { return fmt.Errorf("oss: list objects page: %w", err) } // Collect object identifiers for _, obj := range page.Contents { if obj.Key != nil { objects = append(objects, oss.DeleteObject{Key: obj.Key}) } } } // Delete all collected objects in batches for len(objects) > 0 { n := min(len(objects), MaxKeys) batch := objects[:n] request := &oss.DeleteMultipleObjectsRequest{ Bucket: oss.Ptr(c.Bucket), Objects: batch, } out, err := c.client.DeleteMultipleObjects(ctx, request) if err != nil { return fmt.Errorf("oss: delete all batch of %d objects: %w", n, err) } else if err := deleteResultError(batch, out); err != nil { return err } objects = objects[n:] } return nil } // ltxPath returns the full path to an LTX file. func (c *ReplicaClient) ltxPath(level int, filename string) string { return c.Path + "/" + fmt.Sprintf("%04x/%s", level, filename) } // fileIterator represents an iterator over LTX files in OSS. type fileIterator struct { ctx context.Context cancel context.CancelFunc client *ReplicaClient level int seek ltx.TXID useMetadata bool // When true, fetch accurate timestamps from metadata metadataCache map[string]time.Time // key -> timestamp cache for batch fetches paginator *oss.ListObjectsV2Paginator page *oss.ListObjectsV2Result pageIndex int initialized bool closed bool err error info *ltx.FileInfo } func newFileIterator(ctx context.Context, client *ReplicaClient, level int, seek ltx.TXID, useMetadata bool) *fileIterator { ctx, cancel := context.WithCancel(ctx) itr := &fileIterator{ ctx: ctx, cancel: cancel, client: client, level: level, seek: seek, useMetadata: useMetadata, metadataCache: make(map[string]time.Time), } return itr } // fetchMetadataBatch fetches timestamps from OSS metadata for a batch of keys in parallel. func (itr *fileIterator) fetchMetadataBatch(keys []string) error { if len(keys) == 0 { return nil } // Determine concurrency limit concurrency := itr.client.MetadataConcurrency if concurrency <= 0 { concurrency = DefaultMetadataConcurrency } // Pre-allocate results map to avoid lock contention during writes results := make(map[string]time.Time, len(keys)) var mu sync.Mutex // Use x/sync/semaphore for precise concurrency control with context support sem := semaphore.NewWeighted(int64(concurrency)) g, ctx := errgroup.WithContext(itr.ctx) for _, key := range keys { key := key // capture for goroutine g.Go(func() error { // Acquire semaphore slot (blocking with context cancellation) if err := sem.Acquire(ctx, 1); err != nil { return err // context cancelled } defer sem.Release(1) head, err := itr.client.client.HeadObject(ctx, &oss.HeadObjectRequest{ Bucket: oss.Ptr(itr.client.Bucket), Key: oss.Ptr(key), }) if err != nil { // Non-fatal: file might not have metadata, use LastModified return nil } if head.Metadata != nil { if ts, ok := head.Metadata[MetadataKeyTimestamp]; ok { if parsed, err := time.Parse(time.RFC3339Nano, ts); err == nil { mu.Lock() results[key] = parsed mu.Unlock() } } } return nil }) } if err := g.Wait(); err != nil { return err } // Merge results into cache for k, v := range results { itr.metadataCache[k] = v } return nil } // initPaginator initializes the paginator lazily. func (itr *fileIterator) initPaginator() { if itr.initialized { return } itr.initialized = true // Create paginator for listing objects with level prefix prefix := itr.client.ltxPath(itr.level, "") itr.paginator = itr.client.client.NewListObjectsV2Paginator(&oss.ListObjectsV2Request{ Bucket: oss.Ptr(itr.client.Bucket), Prefix: oss.Ptr(prefix), }) } // Close stops iteration and returns any error that occurred during iteration. func (itr *fileIterator) Close() (err error) { itr.closed = true itr.cancel() return itr.err } // Next returns the next file. Returns false when no more files are available. func (itr *fileIterator) Next() bool { if itr.closed || itr.err != nil { return false } // Initialize paginator on first call itr.initPaginator() // Process objects until we find a valid LTX file for { // Load next page if needed if itr.page == nil || itr.pageIndex >= len(itr.page.Contents) { if !itr.paginator.HasNext() { return false } var err error itr.page, err = itr.paginator.NextPage(itr.ctx) if err != nil { itr.err = err return false } itr.pageIndex = 0 // Batch fetch metadata for the entire page when useMetadata is true. // This uses parallel HeadObject calls controlled by MetadataConcurrency // to avoid the O(N) sequential calls that caused restore hangs (issue #930). if itr.useMetadata && len(itr.page.Contents) > 0 { keys := make([]string, 0, len(itr.page.Contents)) for _, obj := range itr.page.Contents { if obj.Key != nil { keys = append(keys, *obj.Key) } } if err := itr.fetchMetadataBatch(keys); err != nil { itr.err = err return false } } } // Process current object if itr.pageIndex < len(itr.page.Contents) { obj := itr.page.Contents[itr.pageIndex] itr.pageIndex++ if obj.Key == nil { continue } // Extract file info from key fullKey := *obj.Key key := path.Base(fullKey) minTXID, maxTXID, err := ltx.ParseFilename(key) if err != nil { continue // Skip non-LTX files } // Build file info info := <x.FileInfo{ Level: itr.level, MinTXID: minTXID, MaxTXID: maxTXID, } // Skip if below seek TXID if info.MinTXID < itr.seek { continue } // Set file info info.Size = obj.Size // Use cached metadata timestamp if available (from batch fetch), // otherwise fallback to LastModified from LIST operation. if itr.useMetadata { if ts, ok := itr.metadataCache[fullKey]; ok { info.CreatedAt = ts } else if obj.LastModified != nil { info.CreatedAt = obj.LastModified.UTC() } else { info.CreatedAt = time.Now().UTC() } } else { if obj.LastModified != nil { info.CreatedAt = obj.LastModified.UTC() } else { info.CreatedAt = time.Now().UTC() } } itr.info = info return true } } } // Item returns the metadata for the current file. func (itr *fileIterator) Item() *ltx.FileInfo { return itr.info } // Err returns any error that occurred during iteration. func (itr *fileIterator) Err() error { return itr.err } // ParseURL parses an OSS URL into its host and path parts. func ParseURL(s string) (bucket, region, key string, err error) { u, err := url.Parse(s) if err != nil { return "", "", "", err } if u.Scheme != "oss" { return "", "", "", fmt.Errorf("oss: invalid url scheme") } // Parse host to extract bucket and region bucket, region, _ = ParseHost(u.Host) if bucket == "" { bucket = u.Host } key = strings.TrimPrefix(u.Path, "/") return bucket, region, key, nil } // ParseHost parses the host/endpoint for an OSS storage system. // Supports formats like: // - bucket.oss-cn-hangzhou.aliyuncs.com // - bucket.oss-cn-hangzhou-internal.aliyuncs.com // - bucket (just bucket name) func ParseHost(host string) (bucket, region, endpoint string) { // Check for internal OSS URL format first (more specific pattern) if a := ossInternalRegex.FindStringSubmatch(host); len(a) > 1 { bucket = a[1] if len(a) > 2 && a[2] != "" { region = a[2] } return bucket, region, "" } // Check for standard OSS URL format if a := ossRegex.FindStringSubmatch(host); len(a) > 1 { bucket = a[1] if len(a) > 2 && a[2] != "" { region = a[2] } return bucket, region, "" } // For other hosts, assume it's just the bucket name return host, "", "" } var ( // oss-cn-hangzhou.aliyuncs.com or bucket.oss-cn-hangzhou.aliyuncs.com ossRegex = regexp.MustCompile(`^(?:([^.]+)\.)?oss-([^.]+)\.aliyuncs\.com$`) // oss-cn-hangzhou-internal.aliyuncs.com or bucket.oss-cn-hangzhou-internal.aliyuncs.com // Uses non-greedy .+? to correctly extract region without -internal suffix ossInternalRegex = regexp.MustCompile(`^(?:([^.]+)\.)?oss-(.+?)-internal\.aliyuncs\.com$`) ) func isNotExists(err error) bool { var serviceErr *oss.ServiceError if errors.As(err, &serviceErr) { return serviceErr.Code == "NoSuchKey" } return false } // deleteResultError checks if all requested objects were deleted. // OSS SDK doesn't have explicit per-object error reporting like S3, so we verify // all requested keys appear in the deleted list. func deleteResultError(requested []oss.DeleteObject, out *oss.DeleteMultipleObjectsResult) error { if out == nil { return nil } // Build set of deleted keys for quick lookup deleted := make(map[string]struct{}, len(out.DeletedObjects)) for _, obj := range out.DeletedObjects { if obj.Key != nil { deleted[*obj.Key] = struct{}{} } } // Check that all requested keys were deleted var failed []string for _, obj := range requested { if obj.Key == nil { continue } if _, ok := deleted[*obj.Key]; !ok { failed = append(failed, *obj.Key) } } if len(failed) == 0 { return nil } // Build error message listing failed keys var b strings.Builder b.WriteString("oss: failed to delete files:") for _, key := range failed { fmt.Fprintf(&b, "\n%s", key) } return errors.New(b.String()) } ================================================ FILE: oss/replica_client_test.go ================================================ package oss import ( "errors" "testing" "github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss" ) func TestReplicaClient_Type(t *testing.T) { c := NewReplicaClient() if got := c.Type(); got != ReplicaClientType { t.Errorf("Type() = %q, want %q", got, ReplicaClientType) } if got := c.Type(); got != "oss" { t.Errorf("Type() = %q, want %q", got, "oss") } } func TestReplicaClient_Init_BucketValidation(t *testing.T) { t.Run("EmptyBucket", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "" // Empty bucket name c.Region = "cn-hangzhou" err := c.Init(t.Context()) if err == nil { t.Fatal("expected error for empty bucket name") } if got := err.Error(); got != "oss: bucket name is required" { t.Errorf("unexpected error: %v", err) } }) t.Run("ValidBucketWithRegion", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "cn-hangzhou" c.AccessKeyID = "test-key" c.AccessKeySecret = "test-secret" // Init should succeed (client will be created even without real credentials) err := c.Init(t.Context()) if err != nil { t.Errorf("Init() should succeed with valid bucket: %v", err) } }) t.Run("ValidBucketDefaultRegion", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" // Region is empty, should use DefaultRegion c.AccessKeyID = "test-key" c.AccessKeySecret = "test-secret" err := c.Init(t.Context()) if err != nil { t.Errorf("Init() should succeed with default region: %v", err) } }) } func TestReplicaClient_Init_Idempotent(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.AccessKeyID = "test-key" c.AccessKeySecret = "test-secret" // First init if err := c.Init(t.Context()); err != nil { t.Fatalf("first Init() failed: %v", err) } // Second init should be a no-op if err := c.Init(t.Context()); err != nil { t.Fatalf("second Init() failed: %v", err) } } func TestParseURL(t *testing.T) { tests := []struct { name string url string wantBucket string wantRegion string wantKey string wantErr bool }{ { name: "SimpleOSSURL", url: "oss://my-bucket/path/to/file", wantBucket: "my-bucket", wantRegion: "", wantKey: "path/to/file", wantErr: false, }, { name: "OSSURLWithRegion", url: "oss://my-bucket.oss-cn-hangzhou.aliyuncs.com/backup", wantBucket: "my-bucket", wantRegion: "cn-hangzhou", wantKey: "backup", wantErr: false, }, { name: "OSSURLNoPath", url: "oss://my-bucket", wantBucket: "my-bucket", wantRegion: "", wantKey: "", wantErr: false, }, { name: "InvalidScheme", url: "s3://my-bucket/path", wantBucket: "", wantRegion: "", wantKey: "", wantErr: true, }, { name: "HTTPScheme", url: "http://my-bucket/path", wantBucket: "", wantRegion: "", wantKey: "", wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { bucket, region, key, err := ParseURL(tt.url) if (err != nil) != tt.wantErr { t.Errorf("ParseURL() error = %v, wantErr %v", err, tt.wantErr) return } if err != nil { return } if bucket != tt.wantBucket { t.Errorf("bucket = %q, want %q", bucket, tt.wantBucket) } if region != tt.wantRegion { t.Errorf("region = %q, want %q", region, tt.wantRegion) } if key != tt.wantKey { t.Errorf("key = %q, want %q", key, tt.wantKey) } }) } } func TestParseHost(t *testing.T) { tests := []struct { name string host string wantBucket string wantRegion string }{ { name: "StandardOSSURL", host: "my-bucket.oss-cn-hangzhou.aliyuncs.com", wantBucket: "my-bucket", wantRegion: "cn-hangzhou", }, { name: "OSSURLBeijingRegion", host: "test-bucket.oss-cn-beijing.aliyuncs.com", wantBucket: "test-bucket", wantRegion: "cn-beijing", }, { name: "OSSURLShanghaiRegion", host: "data-bucket.oss-cn-shanghai.aliyuncs.com", wantBucket: "data-bucket", wantRegion: "cn-shanghai", }, { name: "InternalOSSURL", host: "my-bucket.oss-cn-hangzhou-internal.aliyuncs.com", wantBucket: "my-bucket", wantRegion: "cn-hangzhou", }, { name: "InternalOSSURLBeijing", host: "test-bucket.oss-cn-beijing-internal.aliyuncs.com", wantBucket: "test-bucket", wantRegion: "cn-beijing", }, { name: "SimpleBucketName", host: "my-bucket", wantBucket: "my-bucket", wantRegion: "", }, { name: "BucketWithHyphens", host: "my-test-bucket-2024.oss-cn-shenzhen.aliyuncs.com", wantBucket: "my-test-bucket-2024", wantRegion: "cn-shenzhen", }, { name: "OSSURLWithNumbers", host: "bucket123.oss-cn-hangzhou.aliyuncs.com", wantBucket: "bucket123", wantRegion: "cn-hangzhou", }, { name: "OSSURLHongKong", host: "hk-bucket.oss-cn-hongkong.aliyuncs.com", wantBucket: "hk-bucket", wantRegion: "cn-hongkong", }, { name: "OSSURLSingapore", host: "sg-bucket.oss-ap-southeast-1.aliyuncs.com", wantBucket: "sg-bucket", wantRegion: "ap-southeast-1", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { bucket, region, _ := ParseHost(tt.host) if bucket != tt.wantBucket { t.Errorf("bucket = %q, want %q", bucket, tt.wantBucket) } if region != tt.wantRegion { t.Errorf("region = %q, want %q", region, tt.wantRegion) } }) } } func TestIsNotExists(t *testing.T) { t.Run("NilError", func(t *testing.T) { if isNotExists(nil) { t.Error("isNotExists should return false for nil error") } }) t.Run("RegularError", func(t *testing.T) { regularErr := errors.New("regular error") if isNotExists(regularErr) { t.Error("isNotExists should return false for regular error") } }) t.Run("WrappedError", func(t *testing.T) { wrappedErr := errors.New("wrapped: something went wrong") if isNotExists(wrappedErr) { t.Error("isNotExists should return false for wrapped non-ServiceError") } }) } func TestLtxPath(t *testing.T) { c := NewReplicaClient() c.Path = "backups" tests := []struct { level int filename string want string }{ {0, "00000001-00000001.ltx", "backups/0000/00000001-00000001.ltx"}, {1, "00000001-00000010.ltx", "backups/0001/00000001-00000010.ltx"}, {15, "00000001-000000ff.ltx", "backups/000f/00000001-000000ff.ltx"}, } for _, tt := range tests { t.Run(tt.want, func(t *testing.T) { got := c.ltxPath(tt.level, tt.filename) if got != tt.want { t.Errorf("ltxPath(%d, %q) = %q, want %q", tt.level, tt.filename, got, tt.want) } }) } } func TestDeleteResultError(t *testing.T) { ptr := func(s string) *string { return &s } t.Run("NilResult", func(t *testing.T) { requested := []oss.DeleteObject{{Key: ptr("key1")}} if err := deleteResultError(requested, nil); err != nil { t.Errorf("expected nil error for nil result, got %v", err) } }) t.Run("AllDeleted", func(t *testing.T) { requested := []oss.DeleteObject{ {Key: ptr("key1")}, {Key: ptr("key2")}, } result := &oss.DeleteMultipleObjectsResult{ DeletedObjects: []oss.DeletedInfo{ {Key: ptr("key1")}, {Key: ptr("key2")}, }, } if err := deleteResultError(requested, result); err != nil { t.Errorf("expected nil error when all deleted, got %v", err) } }) t.Run("SomeNotDeleted", func(t *testing.T) { requested := []oss.DeleteObject{ {Key: ptr("key1")}, {Key: ptr("key2")}, {Key: ptr("key3")}, } result := &oss.DeleteMultipleObjectsResult{ DeletedObjects: []oss.DeletedInfo{ {Key: ptr("key1")}, // key2 and key3 not deleted }, } err := deleteResultError(requested, result) if err == nil { t.Fatal("expected error when some keys not deleted") } errStr := err.Error() if !contains(errStr, "key2") { t.Errorf("error should mention key2: %s", errStr) } if !contains(errStr, "key3") { t.Errorf("error should mention key3: %s", errStr) } }) t.Run("EmptyRequested", func(t *testing.T) { requested := []oss.DeleteObject{} result := &oss.DeleteMultipleObjectsResult{} if err := deleteResultError(requested, result); err != nil { t.Errorf("expected nil error for empty requested, got %v", err) } }) t.Run("NilKeyInRequested", func(t *testing.T) { requested := []oss.DeleteObject{ {Key: nil}, // nil key should be skipped {Key: ptr("key1")}, } result := &oss.DeleteMultipleObjectsResult{ DeletedObjects: []oss.DeletedInfo{ {Key: ptr("key1")}, }, } if err := deleteResultError(requested, result); err != nil { t.Errorf("expected nil error, got %v", err) } }) } func contains(s, substr string) bool { return len(s) >= len(substr) && (s == substr || len(s) > 0 && containsHelper(s, substr)) } func containsHelper(s, substr string) bool { for i := 0; i <= len(s)-len(substr); i++ { if s[i:i+len(substr)] == substr { return true } } return false } ================================================ FILE: packages/npm/litestream-vfs/README.md ================================================ # litestream-vfs Litestream VFS extension for SQLite — distributed via npm. This package bundles the [Litestream](https://litestream.io) VFS shared library. The correct platform-specific binary is automatically installed via `optionalDependencies`. ## Installation ```bash npm install litestream-vfs ``` ## Usage ```javascript const { getLoadablePath } = require("litestream-vfs"); const path = getLoadablePath(); // Use `path` with better-sqlite3 or other SQLite bindings ``` ## Platform Support | Platform | Architecture | |----------|-------------| | Linux | x86_64, aarch64 | | macOS | x86_64, arm64 | ## License Apache-2.0 — see [LICENSE](https://github.com/benbjohnson/litestream/blob/main/LICENSE). ================================================ FILE: packages/npm/litestream-vfs/index.js ================================================ "use strict"; const path = require("path"); const os = require("os"); const PLATFORM_PACKAGES = { "darwin-arm64": "litestream-vfs-darwin-arm64", "darwin-x64": "litestream-vfs-darwin-amd64", "linux-arm64": "litestream-vfs-linux-arm64", "linux-x64": "litestream-vfs-linux-amd64", }; const EXT_MAP = { darwin: "litestream-vfs.dylib", linux: "litestream-vfs.so", }; function getLoadablePath() { const key = `${os.platform()}-${os.arch()}`; const pkg = PLATFORM_PACKAGES[key]; if (!pkg) { throw new Error(`Unsupported platform: ${key}`); } const ext = EXT_MAP[os.platform()]; const searchPaths = [ path.join(process.cwd(), "node_modules"), ...module.paths, ]; if (require.main) { searchPaths.push(...require.main.paths); } try { const resolved = require.resolve(`${pkg}/package.json`, { paths: searchPaths, }); return path.join(path.dirname(resolved), ext); } catch { throw new Error( `Platform package ${pkg} is not installed. ` + `Run: npm install ${pkg}` ); } } module.exports = { getLoadablePath }; ================================================ FILE: packages/npm/litestream-vfs/package.json ================================================ { "name": "litestream-vfs", "version": "0.0.0", "description": "Litestream VFS extension for SQLite", "main": "index.js", "repository": { "type": "git", "url": "https://github.com/benbjohnson/litestream" }, "license": "Apache-2.0", "keywords": ["sqlite", "litestream", "vfs", "replication"], "optionalDependencies": { "litestream-vfs-darwin-arm64": "0.0.0", "litestream-vfs-darwin-amd64": "0.0.0", "litestream-vfs-linux-arm64": "0.0.0", "litestream-vfs-linux-amd64": "0.0.0" } } ================================================ FILE: packages/npm/litestream-vfs-darwin-amd64/README.md ================================================ # litestream-vfs-darwin-amd64 Platform package for `litestream-vfs` — macOS x64 (Intel). This package is installed automatically by `litestream-vfs`. You should not need to install it directly. ================================================ FILE: packages/npm/litestream-vfs-darwin-amd64/package.json ================================================ { "name": "litestream-vfs-darwin-amd64", "version": "0.0.0", "description": "Litestream VFS extension for macOS x64", "os": ["darwin"], "cpu": ["x64"], "repository": { "type": "git", "url": "https://github.com/benbjohnson/litestream" }, "license": "Apache-2.0" } ================================================ FILE: packages/npm/litestream-vfs-darwin-arm64/README.md ================================================ # litestream-vfs-darwin-arm64 Platform package for `litestream-vfs` — macOS ARM64 (Apple Silicon). This package is installed automatically by `litestream-vfs`. You should not need to install it directly. ================================================ FILE: packages/npm/litestream-vfs-darwin-arm64/package.json ================================================ { "name": "litestream-vfs-darwin-arm64", "version": "0.0.0", "description": "Litestream VFS extension for macOS ARM64", "os": ["darwin"], "cpu": ["arm64"], "repository": { "type": "git", "url": "https://github.com/benbjohnson/litestream" }, "license": "Apache-2.0" } ================================================ FILE: packages/npm/litestream-vfs-linux-amd64/README.md ================================================ # litestream-vfs-linux-amd64 Platform package for `litestream-vfs` — Linux x64. This package is installed automatically by `litestream-vfs`. You should not need to install it directly. ================================================ FILE: packages/npm/litestream-vfs-linux-amd64/package.json ================================================ { "name": "litestream-vfs-linux-amd64", "version": "0.0.0", "description": "Litestream VFS extension for Linux x64", "os": ["linux"], "cpu": ["x64"], "libc": ["glibc"], "repository": { "type": "git", "url": "https://github.com/benbjohnson/litestream" }, "license": "Apache-2.0" } ================================================ FILE: packages/npm/litestream-vfs-linux-arm64/README.md ================================================ # litestream-vfs-linux-arm64 Platform package for `litestream-vfs` — Linux ARM64. This package is installed automatically by `litestream-vfs`. You should not need to install it directly. ================================================ FILE: packages/npm/litestream-vfs-linux-arm64/package.json ================================================ { "name": "litestream-vfs-linux-arm64", "version": "0.0.0", "description": "Litestream VFS extension for Linux ARM64", "os": ["linux"], "cpu": ["arm64"], "libc": ["glibc"], "repository": { "type": "git", "url": "https://github.com/benbjohnson/litestream" }, "license": "Apache-2.0" } ================================================ FILE: packages/python/MANIFEST.in ================================================ include README.md recursive-include litestream_vfs *.so *.dylib ================================================ FILE: packages/python/README.md ================================================ # litestream-vfs Litestream VFS extension for SQLite — distributed as a Python wheel. This package bundles the [Litestream](https://litestream.io) VFS shared library so you can load it directly into a Python `sqlite3` connection. ## Installation ```bash pip install litestream-vfs ``` ## Usage ```python import sqlite3 import litestream_vfs conn = sqlite3.connect(":memory:") litestream_vfs.load(conn) ``` To get the path to the shared library (for use with other SQLite bindings): ```python path = litestream_vfs.loadable_path() ``` ## Platform Support | Platform | Architecture | |----------|-------------| | Linux | x86_64, aarch64 | | macOS | x86_64, arm64 | ## License Apache-2.0 — see [LICENSE](https://github.com/benbjohnson/litestream/blob/main/LICENSE). ================================================ FILE: packages/python/litestream_vfs/__init__.py ================================================ """Litestream VFS extension for SQLite.""" import os import sys _EXT_MAP = { "linux": "litestream-vfs.so", "darwin": "litestream-vfs.dylib", } def loadable_path(): """Return the filesystem path to the loadable VFS extension.""" platform = sys.platform if platform.startswith("linux"): platform = "linux" filename = _EXT_MAP.get(platform) if filename is None: raise OSError(f"Unsupported platform: {sys.platform}") path = os.path.join(os.path.dirname(__file__), filename) if not os.path.exists(path): raise FileNotFoundError(f"VFS extension not found at {path}") return path def load(conn): """Load the Litestream VFS extension into a sqlite3 connection.""" path = loadable_path() conn.enable_load_extension(True) try: conn.load_extension(os.path.splitext(path)[0]) finally: conn.enable_load_extension(False) ================================================ FILE: packages/python/litestream_vfs/noop.c ================================================ /* Dummy C extension to force wheel platform tagging. */ #include static PyMethodDef methods[] = {{NULL, NULL, 0, NULL}}; static struct PyModuleDef module = { PyModuleDef_HEAD_INIT, "_noop", NULL, -1, methods, }; PyMODINIT_FUNC PyInit__noop(void) { return PyModule_Create(&module); } ================================================ FILE: packages/python/scripts/rename_wheel.py ================================================ #!/usr/bin/env python3 """Rename a wheel file with the correct platform tag. Usage: python rename_wheel.py platform_tag examples: manylinux_2_35_x86_64, manylinux_2_35_aarch64, macosx_11_0_x86_64, macosx_11_0_arm64 """ import glob import os import sys def main(): wheel_dir = sys.argv[1] platform_tag = sys.argv[2] wheels = glob.glob(os.path.join(wheel_dir, "*.whl")) if not wheels: print(f"No wheels found in {wheel_dir}", file=sys.stderr) sys.exit(1) for wheel in wheels: parts = os.path.basename(wheel).split("-") # Wheel filename: {name}-{ver}-{python}-{abi}-{platform}.whl parts[-1] = f"{platform_tag}.whl" parts[-2] = "none" parts[-3] = "cp38.cp39.cp310.cp311.cp312.cp313" new_name = "-".join(parts) new_path = os.path.join(wheel_dir, new_name) os.rename(wheel, new_path) print(f"Renamed: {os.path.basename(wheel)} -> {new_name}") if __name__ == "__main__": main() ================================================ FILE: packages/python/setup.py ================================================ import os from setuptools import setup, Extension setup( name="litestream-vfs", version=os.environ.get("LITESTREAM_VERSION", "0.0.0"), description="Litestream VFS extension for SQLite", long_description=open("README.md").read(), long_description_content_type="text/markdown", url="https://github.com/benbjohnson/litestream", license="Apache-2.0", packages=["litestream_vfs"], package_data={"litestream_vfs": ["*.so", "*.dylib"]}, ext_modules=[Extension("litestream_vfs._noop", ["litestream_vfs/noop.c"])], python_requires=">=3.8", classifiers=[ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: Apache Software License", "Programming Language :: Python :: 3", "Topic :: Database", ], ) ================================================ FILE: packages/ruby/README.md ================================================ # litestream-vfs Litestream VFS extension for SQLite — distributed as a Ruby gem. This gem bundles the [Litestream](https://litestream.io) VFS shared library so you can load it directly into a SQLite connection. ## Installation ```bash gem install litestream-vfs ``` Or add to your Gemfile: ```ruby gem "litestream-vfs" ``` ## Usage ```ruby require "litestream_vfs" db = SQLite3::Database.new(":memory:") LitestreamVfs.load(db) ``` To get the path to the shared library: ```ruby path = LitestreamVfs.loadable_path ``` ## Platform Support | Platform | Architecture | |----------|-------------| | Linux | x86_64, aarch64 | | macOS | x86_64, arm64 | ## License Apache-2.0 — see [LICENSE](https://github.com/benbjohnson/litestream/blob/main/LICENSE). ================================================ FILE: packages/ruby/lib/litestream_vfs.rb ================================================ require "rbconfig" module LitestreamVfs EXT_MAP = { "linux" => "litestream-vfs.so", "darwin" => "litestream-vfs.dylib", }.freeze def self.loadable_path os = RbConfig::CONFIG["host_os"] key = case os when /linux/ then "linux" when /darwin/i then "darwin" else raise "Unsupported platform: #{os}" end filename = EXT_MAP.fetch(key) path = File.join(__dir__, filename) raise "VFS extension not found at #{path}" unless File.exist?(path) path end def self.load(db) db.enable_load_extension(true) db.load_extension(loadable_path.delete_suffix(File.extname(loadable_path))) db.enable_load_extension(false) end end ================================================ FILE: packages/ruby/litestream-vfs.gemspec ================================================ Gem::Specification.new do |s| s.name = "litestream-vfs" s.version = ENV.fetch("LITESTREAM_VERSION", "0.0.0") s.summary = "Litestream VFS extension for SQLite" s.description = "Bundles the Litestream VFS shared library for loading into SQLite connections." s.homepage = "https://github.com/benbjohnson/litestream" s.license = "Apache-2.0" s.authors = ["Ben Johnson"] s.platform = Gem::Platform.new(ENV.fetch("PLATFORM", RUBY_PLATFORM)) s.files = Dir["lib/**/*.rb"] + Dir["lib/**/*.so"] + Dir["lib/**/*.dylib"] s.require_paths = ["lib"] s.required_ruby_version = ">= 2.7" end ================================================ FILE: replica.go ================================================ package litestream import ( "context" "crypto/rand" "database/sql" "errors" "fmt" "io" "log/slog" "os" "path/filepath" "strings" "sync" "time" "github.com/superfly/ltx" "github.com/benbjohnson/litestream/internal" ) // Default replica settings. const ( DefaultSyncInterval = 1 * time.Second ) // Replica connects a database to a replication destination via a ReplicaClient. // The replica manages periodic synchronization and maintaining the current // replica position. type Replica struct { db *DB mu sync.RWMutex pos ltx.Pos // current replicated position syncMu sync.Mutex // protects Sync() from concurrent calls muf sync.Mutex f *os.File // long-running file descriptor to avoid non-OFD lock issues wg sync.WaitGroup cancel func() // Client used to connect to the remote replica. Client ReplicaClient // Time between syncs with the shadow WAL. SyncInterval time.Duration // If true, replica monitors database for changes automatically. // Set to false if replica is being used synchronously (such as in tests). MonitorEnabled bool // If true, automatically reset local state when LTX errors are detected. // This allows recovery from corrupted/missing LTX files by resetting // the position file and removing local LTX files, forcing a fresh sync. // Disabled by default to prevent silent data loss scenarios. AutoRecoverEnabled bool } func NewReplica(db *DB) *Replica { r := &Replica{ db: db, cancel: func() {}, SyncInterval: DefaultSyncInterval, MonitorEnabled: true, } return r } func NewReplicaWithClient(db *DB, client ReplicaClient) *Replica { r := NewReplica(db) r.Client = client return r } // Logger returns the DB sub-logger for this replica. func (r *Replica) Logger() *slog.Logger { logger := slog.Default() if r.db != nil { logger = r.db.Logger } return logger.With("replica", r.Client.Type()) } // DB returns a reference to the database the replica is attached to, if any. func (r *Replica) DB() *DB { return r.db } // Starts replicating in a background goroutine. func (r *Replica) Start(ctx context.Context) error { // Ignore if replica is being used sychronously. if !r.MonitorEnabled { return nil } // Stop previous replication. r.Stop(false) // Wrap context with cancelation. ctx, r.cancel = context.WithCancel(ctx) // Start goroutine to replicate data. r.wg.Add(1) go func() { defer r.wg.Done(); r.monitor(ctx) }() return nil } // Stop cancels any outstanding replication and blocks until finished. // // Performing a hard stop will close the DB file descriptor which could release // locks on per-process locks. Hard stops should only be performed when // stopping the entire process. func (r *Replica) Stop(hard bool) (err error) { r.cancel() r.wg.Wait() r.muf.Lock() defer r.muf.Unlock() if hard && r.f != nil { if e := r.f.Close(); e != nil && err == nil { err = e } } return err } // Sync copies new WAL frames from the shadow WAL to the replica client. // Only one Sync can run at a time to prevent concurrent uploads of the same file. func (r *Replica) Sync(ctx context.Context) (err error) { r.syncMu.Lock() defer r.syncMu.Unlock() // Clear last position if if an error occurs during sync. defer func() { if err != nil { r.mu.Lock() r.pos = ltx.Pos{} r.mu.Unlock() } }() // Calculate current replica position, if unknown. if r.Pos().IsZero() { pos, err := r.calcPos(ctx) if err != nil { return fmt.Errorf("calc pos: %w", err) } r.SetPos(pos) } // Find current position of database. dpos, err := r.db.Pos() if err != nil { return fmt.Errorf("cannot determine current position: %w", err) } else if dpos.IsZero() { return fmt.Errorf("no position, waiting for data") } r.Logger().Info("replica sync", slog.Group("txid", slog.String("replica", r.Pos().TXID.String()), slog.String("db", dpos.TXID.String()), )) // Replicate all L0 LTX files since last replica position. for txID := r.Pos().TXID + 1; txID <= dpos.TXID; txID = r.Pos().TXID + 1 { if err := r.uploadLTXFile(ctx, 0, txID, txID); err != nil { return err } r.SetPos(ltx.Pos{TXID: txID}) } // Record successful sync for heartbeat monitoring. r.db.RecordSuccessfulSync() return nil } func (r *Replica) uploadLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID) (err error) { filename := r.db.LTXPath(level, minTXID, maxTXID) f, err := os.Open(filename) if err != nil { return err } defer func() { _ = f.Close() }() info, err := r.Client.WriteLTXFile(ctx, level, minTXID, maxTXID, f) if err != nil { return fmt.Errorf("write ltx file: %w", err) } r.Logger().Info("ltx file uploaded", "level", info.Level, "minTXID", info.MinTXID, "maxTXID", info.MaxTXID, "size", info.Size) // Track current position //replicaWALIndexGaugeVec.WithLabelValues(r.db.Path(), r.Name()).Set(float64(rd.Pos().Index)) //replicaWALOffsetGaugeVec.WithLabelValues(r.db.Path(), r.Name()).Set(float64(rd.Pos().Offset)) return nil } // calcPos returns the last position saved to the replica for level 0. func (r *Replica) calcPos(ctx context.Context) (pos ltx.Pos, err error) { info, err := r.MaxLTXFileInfo(ctx, 0) if err != nil { return pos, fmt.Errorf("max ltx file: %w", err) } return ltx.Pos{TXID: info.MaxTXID}, nil } // MaxLTXFileInfo returns metadata about the last LTX file for a given level. // Returns nil if no files exist for the level. func (r *Replica) MaxLTXFileInfo(ctx context.Context, level int) (info ltx.FileInfo, err error) { // Normal operation - use fast timestamps itr, err := r.Client.LTXFiles(ctx, level, 0, false) if err != nil { return info, err } defer itr.Close() for itr.Next() { item := itr.Item() if item.MaxTXID > info.MaxTXID { info = *item } } return info, itr.Close() } // Pos returns the current replicated position. // Returns a zero value if the current position cannot be determined. func (r *Replica) Pos() ltx.Pos { r.mu.RLock() defer r.mu.RUnlock() return r.pos } // SetPos sets the current replicated position. func (r *Replica) SetPos(pos ltx.Pos) { r.mu.Lock() defer r.mu.Unlock() r.pos = pos } // EnforceRetention forces a new snapshot once the retention interval has passed. // Older snapshots and WAL files are then removed. func (r *Replica) EnforceRetention(ctx context.Context) (err error) { panic("TODO(ltx): Re-implement after multi-level compaction") /* // Obtain list of snapshots that are within the retention period. snapshots, err := r.Snapshots(ctx) if err != nil { return fmt.Errorf("snapshots: %w", err) } retained := FilterSnapshotsAfter(snapshots, time.Now().Add(-r.Retention)) // If no retained snapshots exist, create a new snapshot. if len(retained) == 0 { snapshot, err := r.Snapshot(ctx) if err != nil { return fmt.Errorf("snapshot: %w", err) } retained = append(retained, snapshot) } // Delete unretained snapshots & WAL files. snapshot := FindMinSnapshot(retained) // Otherwise remove all earlier snapshots & WAL segments. if err := r.deleteSnapshotsBeforeIndex(ctx, snapshot.Index); err != nil { return fmt.Errorf("delete snapshots before index: %w", err) } else if err := r.deleteWALSegmentsBeforeIndex(ctx, snapshot.Index); err != nil { return fmt.Errorf("delete wal segments before index: %w", err) } return nil */ } /* func (r *Replica) deleteBeforeTXID(ctx context.Context, level int, txID ltx.TXID) error { itr, err := r.Client.LTXFiles(ctx, level) if err != nil { return fmt.Errorf("fetch ltx files: %w", err) } defer itr.Close() var a []*ltx.FileInfo for itr.Next() { info := itr.Item() if info.MinTXID >= txID { continue } a = append(a, info) } if err := itr.Close(); err != nil { return err } if len(a) == 0 { return nil } if err := r.Client.DeleteLTXFiles(ctx, a); err != nil { return fmt.Errorf("delete wal segments: %w", err) } r.Logger().Info("ltx files deleted before", slog.Int("level", level), slog.String("txID", txID.String()), slog.Int("n", len(a))) return nil } */ // monitor runs in a separate goroutine and continuously replicates the DB. // Implements exponential backoff on repeated sync errors to prevent log spam // and reduce load when persistent errors occur. See issue #927. func (r *Replica) monitor(ctx context.Context) { ticker := time.NewTicker(r.SyncInterval) defer ticker.Stop() // Continuously check for new data to replicate. ch := make(chan struct{}) close(ch) var notify <-chan struct{} = ch var backoff time.Duration var lastLogTime time.Time var consecutiveErrs int for initial := true; ; initial = false { // Enforce a minimum time between synchronization. if !initial { select { case <-ctx.Done(): return case <-ticker.C: } } // If in backoff mode, wait additional time before retrying. if backoff > 0 { select { case <-ctx.Done(): return case <-time.After(backoff): } } // Wait for changes to the database. select { case <-ctx.Done(): return case <-notify: } // Fetch new notify channel before replicating data. notify = r.db.Notify() // Synchronize the shadow wal into the replication directory. if err := r.Sync(ctx); err != nil { // Don't log context cancellation errors during shutdown if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { consecutiveErrs++ // Exponential backoff: SyncInterval -> 2x -> 4x -> ... -> max if backoff == 0 { backoff = r.SyncInterval } else { backoff *= 2 if backoff > DefaultSyncBackoffMax { backoff = DefaultSyncBackoffMax } } // Check for LTX errors and include recovery hints var ltxErr *LTXError if errors.As(err, <xErr) { // Log with rate limiting to avoid log spam during persistent errors. if time.Since(lastLogTime) >= SyncErrorLogInterval { if ltxErr.Hint != "" { r.Logger().Error("monitor error", "error", err, "path", ltxErr.Path, "hint", ltxErr.Hint, "consecutive_errors", consecutiveErrs, "backoff", backoff) } else { r.Logger().Error("monitor error", "error", err, "path", ltxErr.Path, "consecutive_errors", consecutiveErrs, "backoff", backoff) } lastLogTime = time.Now() } // Attempt auto-recovery if enabled if r.AutoRecoverEnabled { r.Logger().Warn("auto-recovery enabled, resetting local state") if resetErr := r.db.ResetLocalState(ctx); resetErr != nil { r.Logger().Error("auto-recovery failed", "error", resetErr) } else { r.Logger().Info("auto-recovery complete, resuming replication") // Reset backoff after successful recovery backoff = 0 consecutiveErrs = 0 } } } else { // Log with rate limiting to avoid log spam during persistent errors. if time.Since(lastLogTime) >= SyncErrorLogInterval { r.Logger().Error("monitor error", "error", err, "consecutive_errors", consecutiveErrs, "backoff", backoff) lastLogTime = time.Now() } } } continue } // Success - reset backoff and error counter. if consecutiveErrs > 0 { r.Logger().Info("replica sync recovered", "previous_errors", consecutiveErrs) } backoff = 0 consecutiveErrs = 0 } } // CreatedAt returns the earliest creation time of any LTX file. // Returns zero time if no LTX files exist. func (r *Replica) CreatedAt(ctx context.Context) (time.Time, error) { var min time.Time // Normal operation - use fast timestamps itr, err := r.Client.LTXFiles(ctx, 0, 0, false) if err != nil { return min, err } defer itr.Close() if itr.Next() { min = itr.Item().CreatedAt } return min, itr.Close() } // TimeBounds returns the creation time & last updated time. // Returns zero time if no LTX files exist. func (r *Replica) TimeBounds(ctx context.Context) (createdAt, updatedAt time.Time, err error) { for level := SnapshotLevel; level >= 0; level-- { itr, err := r.Client.LTXFiles(ctx, level, 0, false) if err != nil { return createdAt, updatedAt, err } for itr.Next() { info := itr.Item() if createdAt.IsZero() || info.CreatedAt.Before(createdAt) { createdAt = info.CreatedAt } if updatedAt.IsZero() || info.CreatedAt.After(updatedAt) { updatedAt = info.CreatedAt } } if err := itr.Close(); err != nil { return createdAt, updatedAt, err } } return createdAt, updatedAt, nil } // CalcRestoreTarget returns a target time restore from. func (r *Replica) CalcRestoreTarget(ctx context.Context, opt RestoreOptions) (updatedAt time.Time, err error) { // Determine the replicated time bounds from LTX files. createdAt, updatedAt, err := r.TimeBounds(ctx) if err != nil { return time.Time{}, fmt.Errorf("created at: %w", err) } // Also check v0.3.x time bounds if client supports it. if client, ok := r.Client.(ReplicaClientV3); ok { v3CreatedAt, v3UpdatedAt, err := r.TimeBoundsV3(ctx, client) if err != nil { return time.Time{}, fmt.Errorf("v0.3.x time bounds: %w", err) } // Extend time bounds to include v0.3.x backups. if !v3CreatedAt.IsZero() && (createdAt.IsZero() || v3CreatedAt.Before(createdAt)) { createdAt = v3CreatedAt } if !v3UpdatedAt.IsZero() && (updatedAt.IsZero() || v3UpdatedAt.After(updatedAt)) { updatedAt = v3UpdatedAt } } // Skip if it does not contain timestamp. if !opt.Timestamp.IsZero() { if createdAt.IsZero() && updatedAt.IsZero() { return time.Time{}, fmt.Errorf("no backups found") } if opt.Timestamp.Before(createdAt) || opt.Timestamp.After(updatedAt) { return time.Time{}, fmt.Errorf("timestamp does not exist") } } return updatedAt, nil } // Replica restores the database from a replica based on the options given. // This method will restore into opt.OutputPath, if specified, or into the // DB's original database path. It can optionally restore from a specific // replica or it will automatically choose the best one. Finally, // a timestamp can be specified to restore the database to a specific // point-in-time. // // When the replica contains both v0.3.x and LTX format backups, this method // compares snapshots from both formats and uses whichever has the better backup: // - With timestamp: uses the format with the most recent snapshot before timestamp // - Without timestamp: uses the format with the most recent backup overall func (r *Replica) Restore(ctx context.Context, opt RestoreOptions) (err error) { // Validate options. if opt.OutputPath == "" { return fmt.Errorf("output path required") } else if opt.TXID != 0 && !opt.Timestamp.IsZero() { return fmt.Errorf("cannot specify index & timestamp to restore") } else if opt.Follow && opt.TXID != 0 { return fmt.Errorf("cannot use follow mode with -txid") } else if opt.Follow && !opt.Timestamp.IsZero() { return fmt.Errorf("cannot use follow mode with -timestamp") } else if opt.IntegrityCheck != IntegrityCheckNone && opt.IntegrityCheck != IntegrityCheckQuick && opt.IntegrityCheck != IntegrityCheckFull { return fmt.Errorf("unsupported integrity check mode: %d", opt.IntegrityCheck) } // In follow mode, if the database already exists, attempt crash recovery // by reading the last applied TXID from the sidecar file. if opt.Follow { if _, statErr := os.Stat(opt.OutputPath); statErr == nil { txid, readErr := ReadTXIDFile(opt.OutputPath) if readErr != nil { return fmt.Errorf("read txid file for crash recovery: %w", readErr) } if txid == 0 { return fmt.Errorf("cannot resume follow mode: database exists but no -txid file found; delete the database to re-restore: %s", opt.OutputPath) } // Validate saved TXID is still reachable. If the earliest snapshot // starts after our saved TXID, retention has pruned the history // and we can't catch up incrementally. snapshotItr, itrErr := r.Client.LTXFiles(ctx, SnapshotLevel, 0, false) if itrErr != nil { return fmt.Errorf("cannot validate saved TXID for crash recovery: %w", itrErr) } var latestSnapshot *ltx.FileInfo for snapshotItr.Next() { latestSnapshot = snapshotItr.Item() } if err := snapshotItr.Err(); err != nil { _ = snapshotItr.Close() return fmt.Errorf("iterate snapshots for crash recovery validation: %w", err) } _ = snapshotItr.Close() if latestSnapshot != nil { if latestSnapshot.MinTXID > txid { return fmt.Errorf("cannot resume follow mode: saved TXID %s is behind the earliest snapshot (min TXID %s); replica history has been pruned -- delete %s and %s-txid to re-restore", txid, latestSnapshot.MinTXID, opt.OutputPath, opt.OutputPath) } if txid > latestSnapshot.MaxTXID { return fmt.Errorf("cannot resume follow mode: saved TXID %s is ahead of latest snapshot (max TXID %s); delete %s and %s-txid to re-restore", txid, latestSnapshot.MaxTXID, opt.OutputPath, opt.OutputPath) } } r.Logger().Info("resuming follow mode from crash recovery", "txid", txid, "output", opt.OutputPath) return r.follow(ctx, opt.OutputPath, txid, opt.FollowInterval) } } // Ensure output path does not already exist. if _, err := os.Stat(opt.OutputPath); err == nil { return fmt.Errorf("cannot restore, output path already exists: %s", opt.OutputPath) } else if !os.IsNotExist(err) { return err } // Compare v0.3.x and LTX formats to find the best backup (unless TXID is specified). // Skip V3 format when follow mode is enabled (V3 doesn't support incremental following). if opt.TXID == 0 && !opt.Follow { if client, ok := r.Client.(ReplicaClientV3); ok { useV3, err := r.shouldUseV3Restore(ctx, client, opt.Timestamp) if err != nil { return err } if useV3 { return r.RestoreV3(ctx, opt) } } } infos, err := CalcRestorePlan(ctx, r.Client, opt.TXID, opt.Timestamp, r.Logger()) if err != nil { return fmt.Errorf("cannot calc restore plan: %w", err) } r.Logger().Debug("restore plan", "n", len(infos), "txid", infos[len(infos)-1].MaxTXID, "timestamp", infos[len(infos)-1].CreatedAt) rdrs := make([]io.Reader, 0, len(infos)) defer func() { for _, rd := range rdrs { if closer, ok := rd.(io.Closer); ok { _ = closer.Close() } } }() for _, info := range infos { // Validate file size - must be at least header size to be readable if info.Size < ltx.HeaderSize { return fmt.Errorf("invalid ltx file: level=%d min=%s max=%s has size %d bytes (minimum %d)", info.Level, info.MinTXID, info.MaxTXID, info.Size, ltx.HeaderSize) } r.Logger().Debug("opening ltx file for restore", "level", info.Level, "min", info.MinTXID, "max", info.MaxTXID) // Add file to be compacted. f, err := r.Client.OpenLTXFile(ctx, info.Level, info.MinTXID, info.MaxTXID, 0, 0) if err != nil { return fmt.Errorf("open ltx file: %w", err) } rdrs = append(rdrs, internal.NewResumableReader(ctx, r.Client, info.Level, info.MinTXID, info.MaxTXID, info.Size, f, r.Logger())) } if len(rdrs) == 0 { return fmt.Errorf("no matching backup files available") } // Create parent directory if it doesn't exist. var dirInfo os.FileInfo if db := r.DB(); db != nil { dirInfo = db.dirInfo } if err := internal.MkdirAll(filepath.Dir(opt.OutputPath), dirInfo); err != nil { return fmt.Errorf("create parent directory: %w", err) } // Output to temp file & atomically rename. tmpOutputPath := opt.OutputPath + ".tmp" r.Logger().Debug("compacting into database", "path", tmpOutputPath, "n", len(rdrs)) f, err := os.Create(tmpOutputPath) if err != nil { return fmt.Errorf("create temp database path: %w", err) } defer func() { _ = f.Close() }() pr, pw := io.Pipe() go func() { c, err := ltx.NewCompactor(pw, rdrs) if err != nil { pw.CloseWithError(fmt.Errorf("new ltx compactor: %w", err)) return } c.HeaderFlags = ltx.HeaderFlagNoChecksum _ = pw.CloseWithError(c.Compact(ctx)) }() dec := ltx.NewDecoder(pr) if err := dec.DecodeDatabaseTo(f); err != nil { return fmt.Errorf("decode database: %w", err) } if err := f.Sync(); err != nil { return err } else if err := f.Close(); err != nil { return err } // Copy file to final location. r.Logger().Debug("renaming database from temporary location") if err := os.Rename(tmpOutputPath, opt.OutputPath); err != nil { return err } if opt.IntegrityCheck != IntegrityCheckNone { if err := checkIntegrity(ctx, opt.OutputPath, opt.IntegrityCheck); err != nil { if ctx.Err() == nil { _ = os.Remove(opt.OutputPath) _ = os.Remove(opt.OutputPath + "-shm") _ = os.Remove(opt.OutputPath + "-wal") } return fmt.Errorf("post-restore integrity check: %w", err) } r.Logger().Info("post-restore integrity check passed") } // Enter follow mode if enabled, continuously applying new LTX files. if opt.Follow { for _, rd := range rdrs { if closer, ok := rd.(io.Closer); ok { _ = closer.Close() } } rdrs = nil maxTXID := infos[len(infos)-1].MaxTXID if err := WriteTXIDFile(opt.OutputPath, maxTXID); err != nil { return fmt.Errorf("write initial txid file: %w", err) } return r.follow(ctx, opt.OutputPath, maxTXID, opt.FollowInterval) } return nil } // follow enters a continuous restore loop, polling for new LTX files and // applying them to the restored database. It blocks until the context is // cancelled (e.g. Ctrl+C). Returns nil on clean shutdown. func (r *Replica) follow(ctx context.Context, outputPath string, lastTXID ltx.TXID, interval time.Duration) error { f, err := os.OpenFile(outputPath, os.O_RDWR, 0) if err != nil { return fmt.Errorf("open database for follow: %w", err) } defer func() { _ = f.Sync() _ = f.Close() }() // Read page size from SQLite header (offset 16, 2 bytes, big-endian). var buf [2]byte if _, err := f.ReadAt(buf[:], 16); err != nil { return fmt.Errorf("read page size from database header: %w", err) } pageSize := uint32(buf[0])<<8 | uint32(buf[1]) if pageSize == 1 { pageSize = 65536 } if interval <= 0 { interval = DefaultFollowInterval } r.Logger().Info("entering follow mode", "output", outputPath, "txid", lastTXID, "interval", interval) ticker := time.NewTicker(interval) defer ticker.Stop() var consecutiveErrors int for { select { case <-ctx.Done(): r.Logger().Info("follow mode stopped") return nil case <-ticker.C: newTXID, err := r.applyNewLTXFiles(ctx, f, lastTXID, pageSize) if err != nil { if ctx.Err() != nil { r.Logger().Info("follow mode stopped") return nil } consecutiveErrors++ r.Logger().Error("follow: error applying updates", "err", err, "consecutive_errors", consecutiveErrors) continue } if newTXID > lastTXID { if err := WriteTXIDFile(outputPath, newTXID); err != nil { return fmt.Errorf("write txid file: %w", err) } r.Logger().Info("follow: applied updates", "from_txid", lastTXID, "to_txid", newTXID) lastTXID = newTXID consecutiveErrors = 0 } } } } // applyNewLTXFiles polls for new LTX files and applies them to the database. // It starts from level 0 and falls back to higher levels if there are gaps // (e.g., level 0 files were compacted away). func (r *Replica) applyNewLTXFiles(ctx context.Context, f *os.File, afterTXID ltx.TXID, pageSize uint32) (ltx.TXID, error) { currentTXID := afterTXID // Poll level 0 for the most recent incremental files. itr, err := r.Client.LTXFiles(ctx, 0, currentTXID+1, false) if err != nil { return currentTXID, fmt.Errorf("list level 0 ltx files: %w", err) } closeLevel0 := func(retErr error) (ltx.TXID, error) { if closeErr := itr.Close(); closeErr != nil { closeErr = fmt.Errorf("close level 0 ltx iterator: %w", closeErr) if retErr != nil { return currentTXID, errors.Join(retErr, closeErr) } return currentTXID, closeErr } return currentTXID, retErr } var sawLevel0 bool for itr.Next() { sawLevel0 = true info := itr.Item() // If there's a gap, try to fill it from higher compaction levels. if info.MinTXID > currentTXID+1 { bridgedTXID, err := r.fillFollowGap(ctx, f, currentTXID, info.MinTXID, pageSize) if err != nil { return closeLevel0(err) } currentTXID = bridgedTXID // Re-check if this file is still needed after bridging. if info.MaxTXID <= currentTXID { continue } if info.MinTXID > currentTXID+1 { return closeLevel0(nil) } } // Skip if already covered by a higher-level file. if info.MaxTXID <= currentTXID { continue } if err := r.applyLTXFile(ctx, f, info, pageSize); err != nil { return closeLevel0(fmt.Errorf( "apply ltx file (level=%d, min=%s, max=%s): %w", info.Level, info.MinTXID, info.MaxTXID, err, )) } currentTXID = info.MaxTXID } if iterErr := itr.Err(); iterErr != nil { return closeLevel0(fmt.Errorf("iterate level 0 ltx files: %w", iterErr)) } if _, err := closeLevel0(nil); err != nil { return currentTXID, err } if !sawLevel0 { bridgedTXID, err := r.fillFollowGap(ctx, f, currentTXID, currentTXID+1, pageSize) if err != nil { return currentTXID, err } currentTXID = bridgedTXID } return currentTXID, nil } // applyLTXFile applies a single LTX file's pages to the database file. // This follows the same pattern as Hydrator.ApplyLTX (vfs.go:712-747). // // To prevent concurrent SQLite readers from seeing partial updates, we acquire // an exclusive file lock before writing. We also rewrite the SQLite header // (bytes 18-19) to indicate DELETE journal mode instead of WAL mode, and // randomize the schema change counter (bytes 24-27) to invalidate cached // schemas in other connections. func (r *Replica) applyLTXFile(ctx context.Context, f *os.File, info *ltx.FileInfo, pageSize uint32) error { rc, err := r.Client.OpenLTXFile(ctx, info.Level, info.MinTXID, info.MaxTXID, 0, 0) if err != nil { return fmt.Errorf("open ltx file: %w", err) } defer rc.Close() dec := ltx.NewDecoder(rc) if err := dec.DecodeHeader(); err != nil { return fmt.Errorf("decode header: %w", err) } hdr := dec.Header() if err := internal.LockFileExclusive(f); err != nil { return fmt.Errorf("acquire exclusive lock: %w", err) } defer internal.UnlockFile(f) for { var phdr ltx.PageHeader data := make([]byte, pageSize) if err := dec.DecodePage(&phdr, data); err == io.EOF { break } else if err != nil { return fmt.Errorf("decode page: %w", err) } if phdr.Pgno == 1 && len(data) >= 28 { data[18], data[19] = 0x01, 0x01 _, _ = rand.Read(data[24:28]) } off := int64(phdr.Pgno-1) * int64(pageSize) if _, err := f.WriteAt(data, off); err != nil { return fmt.Errorf("write page %d: %w", phdr.Pgno, err) } } if hdr.Commit > 0 { newSize := int64(hdr.Commit) * int64(pageSize) if err := f.Truncate(newSize); err != nil { return fmt.Errorf("truncate: %w", err) } } if err := dec.Close(); err != nil { return fmt.Errorf("close decoder: %w", err) } return f.Sync() } // fillFollowGap attempts to bridge a gap in level 0 files by searching // higher compaction levels for a file that covers the missing TXID range. func (r *Replica) fillFollowGap(ctx context.Context, f *os.File, afterTXID ltx.TXID, gapMinTXID ltx.TXID, pageSize uint32) (ltx.TXID, error) { currentTXID := afterTXID for level := 1; level < SnapshotLevel; level++ { itr, err := r.Client.LTXFiles(ctx, level, 0, false) if err != nil { return currentTXID, fmt.Errorf("list level %d ltx files: %w", level, err) } closeLevel := func(retErr error) (ltx.TXID, error) { if closeErr := itr.Close(); closeErr != nil { closeErr = fmt.Errorf("close level %d ltx iterator: %w", level, closeErr) if retErr != nil { return currentTXID, errors.Join(retErr, closeErr) } return currentTXID, closeErr } return currentTXID, retErr } for itr.Next() { info := itr.Item() // Skip if there's a gap at this level too. if info.MinTXID > currentTXID+1 { break } // Skip if already covered. if info.MaxTXID <= currentTXID { continue } if err := r.applyLTXFile(ctx, f, info, pageSize); err != nil { return closeLevel(fmt.Errorf( "apply gap-fill ltx file (level=%d, min=%s, max=%s): %w", info.Level, info.MinTXID, info.MaxTXID, err, )) } currentTXID = info.MaxTXID // If we've bridged past the gap, we're done. if currentTXID+1 >= gapMinTXID { return closeLevel(nil) } } if iterErr := itr.Err(); iterErr != nil { return closeLevel(fmt.Errorf("iterate level %d ltx files: %w", level, iterErr)) } if _, err := closeLevel(nil); err != nil { return currentTXID, err } // If we made progress at this level, restart from level 1. if currentTXID > afterTXID { return currentTXID, nil } } return currentTXID, nil } // RestoreV3 restores from a v0.3.x format backup. func (r *Replica) RestoreV3(ctx context.Context, opt RestoreOptions) error { client, ok := r.Client.(ReplicaClientV3) if !ok { return fmt.Errorf("replica client does not support v0.3.x restore") } // Validate options. if opt.OutputPath == "" { return fmt.Errorf("output path required") } else if opt.IntegrityCheck != IntegrityCheckNone && opt.IntegrityCheck != IntegrityCheckQuick && opt.IntegrityCheck != IntegrityCheckFull { return fmt.Errorf("unsupported integrity check mode: %d", opt.IntegrityCheck) } // Ensure output path does not already exist. if _, err := os.Stat(opt.OutputPath); err == nil { return fmt.Errorf("cannot restore, output path already exists: %s", opt.OutputPath) } else if !os.IsNotExist(err) { return err } // Find all generations. generations, err := client.GenerationsV3(ctx) if err != nil { return fmt.Errorf("list generations: %w", err) } if len(generations) == 0 { return ErrNoSnapshots } // Collect all snapshots across all generations. var allSnapshots []SnapshotInfoV3 for _, gen := range generations { snapshots, err := client.SnapshotsV3(ctx, gen) if err != nil { return fmt.Errorf("list snapshots for generation %s: %w", gen, err) } allSnapshots = append(allSnapshots, snapshots...) } if len(allSnapshots) == 0 { return ErrNoSnapshots } // Sort all snapshots by CreatedAt for timestamp-based selection. sortSnapshotsV3ByCreatedAt(allSnapshots) // Find best snapshot across all generations (latest, or before timestamp if specified). snapshot := findBestSnapshotV3(allSnapshots, opt.Timestamp) if snapshot == nil { return ErrNoSnapshots } r.Logger().Debug("selected v0.3.x snapshot", "generation", snapshot.Generation, "index", snapshot.Index, "created_at", snapshot.CreatedAt) // Get WAL segments for the snapshot's generation. segments, err := client.WALSegmentsV3(ctx, snapshot.Generation) if err != nil { return fmt.Errorf("list WAL segments: %w", err) } segments = filterWALSegmentsV3(segments, snapshot.Index, opt.Timestamp) r.Logger().Debug("found v0.3.x WAL segments", "n", len(segments)) // Create parent directory if it doesn't exist. var dirInfo os.FileInfo if db := r.DB(); db != nil { dirInfo = db.DirInfo() } if err := internal.MkdirAll(filepath.Dir(opt.OutputPath), dirInfo); err != nil { return fmt.Errorf("create parent directory: %w", err) } // Create temp file for restore. tmpPath := opt.OutputPath + ".tmp" defer func() { _ = os.Remove(tmpPath) }() // Download and decompress snapshot. if err := r.downloadSnapshotV3(ctx, client, snapshot.Generation, snapshot.Index, tmpPath); err != nil { return fmt.Errorf("download snapshot: %w", err) } // Apply WAL segments. if err := r.applyWALSegmentsV3(ctx, client, snapshot.Generation, segments, tmpPath); err != nil { return fmt.Errorf("apply WAL segments: %w", err) } // Rename to final path. if err := os.Rename(tmpPath, opt.OutputPath); err != nil { return fmt.Errorf("rename to output path: %w", err) } if opt.IntegrityCheck != IntegrityCheckNone { if err := checkIntegrity(ctx, opt.OutputPath, opt.IntegrityCheck); err != nil { if ctx.Err() == nil { _ = os.Remove(opt.OutputPath) _ = os.Remove(opt.OutputPath + "-shm") _ = os.Remove(opt.OutputPath + "-wal") } return fmt.Errorf("post-restore integrity check: %w", err) } r.Logger().Info("post-restore integrity check passed") } return nil } // sortSnapshotsV3ByCreatedAt sorts snapshots by creation time in ascending order. func sortSnapshotsV3ByCreatedAt(snapshots []SnapshotInfoV3) { for i := 0; i < len(snapshots)-1; i++ { for j := i + 1; j < len(snapshots); j++ { if snapshots[i].CreatedAt.After(snapshots[j].CreatedAt) { snapshots[i], snapshots[j] = snapshots[j], snapshots[i] } } } } // findBestSnapshotV3 finds the best snapshot for restore. // If timestamp is zero, returns the latest snapshot. // Otherwise, returns the latest snapshot created before or at the timestamp. func findBestSnapshotV3(snapshots []SnapshotInfoV3, timestamp time.Time) *SnapshotInfoV3 { if len(snapshots) == 0 { return nil } if timestamp.IsZero() { return &snapshots[len(snapshots)-1] } for i := len(snapshots) - 1; i >= 0; i-- { if !snapshots[i].CreatedAt.After(timestamp) { return &snapshots[i] } } return nil } // filterWALSegmentsV3 filters WAL segments to those at or after the snapshot index // and optionally before the timestamp. func filterWALSegmentsV3(segments []WALSegmentInfoV3, snapshotIndex int, timestamp time.Time) []WALSegmentInfoV3 { var result []WALSegmentInfoV3 for _, seg := range segments { if seg.Index < snapshotIndex { continue } if !timestamp.IsZero() && seg.CreatedAt.After(timestamp) { continue } result = append(result, seg) } return result } // downloadSnapshotV3 downloads and decompresses a v0.3.x snapshot to the destination path. func (r *Replica) downloadSnapshotV3(ctx context.Context, client ReplicaClientV3, generation string, index int, destPath string) error { rc, err := client.OpenSnapshotV3(ctx, generation, index) if err != nil { return err } defer func() { _ = rc.Close() }() f, err := os.Create(destPath) if err != nil { return err } defer func() { _ = f.Close() }() if _, err := io.Copy(f, rc); err != nil { return err } return f.Sync() } // applyWALSegmentsV3 applies WAL segments to the database file. func (r *Replica) applyWALSegmentsV3(ctx context.Context, client ReplicaClientV3, generation string, segments []WALSegmentInfoV3, dbPath string) error { if len(segments) == 0 { return nil } // Write all WAL segments to the WAL file. walPath := dbPath + "-wal" for _, seg := range segments { if err := r.writeWALSegmentV3(ctx, client, generation, seg, walPath); err != nil { return fmt.Errorf("write WAL segment %d/%d: %w", seg.Index, seg.Offset, err) } } // Checkpoint to apply WAL to database. return checkpointV3(dbPath) } // writeWALSegmentV3 writes a single WAL segment to the WAL file. func (r *Replica) writeWALSegmentV3(ctx context.Context, client ReplicaClientV3, generation string, seg WALSegmentInfoV3, walPath string) error { // Download WAL segment. rc, err := client.OpenWALSegmentV3(ctx, generation, seg.Index, seg.Offset) if err != nil { return err } defer func() { _ = rc.Close() }() // Open WAL file for writing. f, err := os.OpenFile(walPath, os.O_CREATE|os.O_WRONLY, 0644) if err != nil { return err } defer func() { _ = f.Close() }() // Seek to offset and write. if _, err := f.Seek(seg.Offset, io.SeekStart); err != nil { return err } if _, err := io.Copy(f, rc); err != nil { return err } return f.Sync() } // checkpointV3 checkpoints the WAL file into the database. func checkpointV3(dbPath string) error { db, err := sql.Open("sqlite", dbPath) if err != nil { return err } defer func() { _ = db.Close() }() _, err = db.Exec("PRAGMA wal_checkpoint(TRUNCATE)") return err } // checkIntegrity runs a SQLite integrity check on the database at dbPath. func checkIntegrity(ctx context.Context, dbPath string, mode IntegrityCheckMode) error { if mode == IntegrityCheckNone { return nil } db, err := sql.Open("sqlite", dbPath) if err != nil { return fmt.Errorf("open database for integrity check: %w", err) } defer func() { _ = db.Close() }() var pragma string switch mode { case IntegrityCheckQuick: pragma = "quick_check" case IntegrityCheckFull: pragma = "integrity_check" default: return fmt.Errorf("unsupported integrity check mode: %d", mode) } var result string if err := db.QueryRowContext(ctx, "PRAGMA "+pragma).Scan(&result); err != nil { return fmt.Errorf("integrity check: %w", err) } if result != "ok" { return fmt.Errorf("integrity check failed: %s", result) } // Clean up -shm and -wal files that SQLite may create during the PRAGMA. _ = os.Remove(dbPath + "-shm") _ = os.Remove(dbPath + "-wal") return nil } // findBestV3SnapshotForTimestamp returns the best v0.3.x snapshot for the given timestamp. // Returns nil if no suitable snapshot exists. func (r *Replica) findBestV3SnapshotForTimestamp(ctx context.Context, client ReplicaClientV3, timestamp time.Time) (*SnapshotInfoV3, error) { generations, err := client.GenerationsV3(ctx) if err != nil { return nil, fmt.Errorf("list v0.3.x generations: %w", err) } if len(generations) == 0 { return nil, nil } var allSnapshots []SnapshotInfoV3 for _, gen := range generations { snapshots, err := client.SnapshotsV3(ctx, gen) if err != nil { return nil, fmt.Errorf("list v0.3.x snapshots for generation %s: %w", gen, err) } allSnapshots = append(allSnapshots, snapshots...) } if len(allSnapshots) == 0 { return nil, nil } // Sort by CreatedAt for timestamp-based selection. sortSnapshotsV3ByCreatedAt(allSnapshots) return findBestSnapshotV3(allSnapshots, timestamp), nil } // shouldUseV3Restore determines whether to use v0.3.x restore instead of LTX. // Returns true if v0.3.x has a better backup for the given options. func (r *Replica) shouldUseV3Restore(ctx context.Context, client ReplicaClientV3, timestamp time.Time) (bool, error) { // Get v0.3.x time bounds. _, v3UpdatedAt, err := r.TimeBoundsV3(ctx, client) if err != nil { return false, fmt.Errorf("get v0.3.x time bounds: %w", err) } // Get LTX time bounds. _, ltxUpdatedAt, err := r.TimeBounds(ctx) if err != nil { return false, fmt.Errorf("get LTX time bounds: %w", err) } // If no v0.3.x backups exist, use LTX. if v3UpdatedAt.IsZero() { return false, nil } // If no LTX backups exist, use v0.3.x. if ltxUpdatedAt.IsZero() { r.Logger().Debug("using v0.3.x restore (no LTX backups)") return true, nil } // Both formats have backups - compare based on timestamp or latest. if !timestamp.IsZero() { // With timestamp: use format with best snapshot before timestamp. v3Snapshot, err := r.findBestV3SnapshotForTimestamp(ctx, client, timestamp) if err != nil { return false, fmt.Errorf("find v0.3.x snapshot: %w", err) } ltxSnapshot, err := r.findBestLTXSnapshotForTimestamp(ctx, timestamp) if err != nil { return false, fmt.Errorf("find LTX snapshot: %w", err) } if v3Snapshot != nil && (ltxSnapshot == nil || v3Snapshot.CreatedAt.After(ltxSnapshot.CreatedAt)) { r.Logger().Debug("using v0.3.x restore (better snapshot for timestamp)", "v3_snapshot", v3Snapshot.CreatedAt, "ltx_snapshot", ltxSnapshot) return true, nil } } else { // Without timestamp: use format with most recent backup. if v3UpdatedAt.After(ltxUpdatedAt) { r.Logger().Debug("using v0.3.x restore (more recent backup)", "v3_updated_at", v3UpdatedAt, "ltx_updated_at", ltxUpdatedAt) return true, nil } } return false, nil } // TimeBoundsV3 returns the time bounds of v0.3.x backups. // Returns zero times if no v0.3.x backups exist. func (r *Replica) TimeBoundsV3(ctx context.Context, client ReplicaClientV3) (createdAt, updatedAt time.Time, err error) { generations, err := client.GenerationsV3(ctx) if err != nil { return time.Time{}, time.Time{}, err } for _, gen := range generations { snapshots, err := client.SnapshotsV3(ctx, gen) if err != nil { return time.Time{}, time.Time{}, err } for _, snap := range snapshots { if createdAt.IsZero() || snap.CreatedAt.Before(createdAt) { createdAt = snap.CreatedAt } if updatedAt.IsZero() || snap.CreatedAt.After(updatedAt) { updatedAt = snap.CreatedAt } } segments, err := client.WALSegmentsV3(ctx, gen) if err != nil { return time.Time{}, time.Time{}, err } for _, seg := range segments { if createdAt.IsZero() || seg.CreatedAt.Before(createdAt) { createdAt = seg.CreatedAt } if updatedAt.IsZero() || seg.CreatedAt.After(updatedAt) { updatedAt = seg.CreatedAt } } } return createdAt, updatedAt, nil } // findBestLTXSnapshotForTimestamp returns the best LTX snapshot for the given timestamp. // Returns nil if no suitable snapshot exists. func (r *Replica) findBestLTXSnapshotForTimestamp(ctx context.Context, timestamp time.Time) (*ltx.FileInfo, error) { // Find snapshots at the snapshot level that are before the timestamp. snapshots, err := FindLTXFiles(ctx, r.Client, SnapshotLevel, true, func(info *ltx.FileInfo) (bool, error) { return info.CreatedAt.Before(timestamp), nil }) if err != nil { return nil, fmt.Errorf("find LTX snapshots: %w", err) } if len(snapshots) == 0 { return nil, nil } // Return the latest snapshot before the timestamp (last in the sorted list). return snapshots[len(snapshots)-1], nil } // CalcRestorePlan returns a list of storage paths to restore a snapshot at the given TXID. func CalcRestorePlan(ctx context.Context, client ReplicaClient, txID ltx.TXID, timestamp time.Time, logger *slog.Logger) ([]*ltx.FileInfo, error) { if txID != 0 && !timestamp.IsZero() { return nil, fmt.Errorf("cannot specify both TXID & timestamp to restore") } var infos ltx.FileInfoSlice logger = logger.With("target", txID) // Start with latest snapshot before target TXID or timestamp. // Pass useMetadata flag to enable accurate timestamp fetching for timestamp-based restore. var snapshot *ltx.FileInfo snapshotItr, err := client.LTXFiles(ctx, SnapshotLevel, 0, !timestamp.IsZero()) if err != nil { return nil, err } for snapshotItr.Next() { info := snapshotItr.Item() logger.Debug("finding snapshot before target TXID or timestamp", "snapshot", info.MaxTXID) if txID != 0 && info.MaxTXID > txID { continue } if !timestamp.IsZero() && !info.CreatedAt.Before(timestamp) { continue } snapshot = info } if err := snapshotItr.Close(); err != nil { return nil, err } if snapshot != nil { logger.Debug("found snapshot before target TXID or timestamp", "snapshot", snapshot.MaxTXID) infos = append(infos, snapshot) } // Collect candidates across all compaction levels and pick the next file // from any level that extends the longest contiguous TXID range. const maxLevel = SnapshotLevel - 1 startTXID := infos.MaxTXID() currentMax := startTXID if txID != 0 && currentMax >= txID { return infos, nil } cursors := make([]*restoreLevelCursor, 0, maxLevel+1) for level := maxLevel; level >= 0; level-- { logger.Debug("finding ltx files for level", "level", level) itr, err := client.LTXFiles(ctx, level, 0, !timestamp.IsZero()) if err != nil { return nil, err } cursors = append(cursors, &restoreLevelCursor{ itr: itr, }) } defer func() { for _, cursor := range cursors { if cursor != nil { _ = cursor.itr.Close() } } }() for { var next *restoreLevelCursor for _, cursor := range cursors { if err := cursor.refresh(currentMax, txID, timestamp); err != nil { return nil, err } if cursor.candidate == nil { continue } if next == nil || restoreCandidateBetter(next.candidate, cursor.candidate) { next = cursor } } if next == nil || next.candidate == nil { break } if next.candidate.MaxTXID <= currentMax { next.candidate = nil continue } logger.Debug("matching LTX file for restore", "filename", ltx.FormatFilename(next.candidate.MinTXID, next.candidate.MaxTXID), "level", next.candidate.Level) infos = append(infos, next.candidate) currentMax = next.candidate.MaxTXID next.candidate = nil if txID != 0 && currentMax >= txID { break } } if len(infos) > 0 && txID == 0 && timestamp.IsZero() { for _, cursor := range cursors { if err := cursor.ensureCurrent(); err != nil { return nil, err } if cursor.current != nil && cursor.current.MinTXID > currentMax+1 { return nil, fmt.Errorf("non-contiguous ltx files: have up to %s but next file starts at %s", currentMax, cursor.current.MinTXID) } } } if len(infos) == 0 { return nil, ErrTxNotAvailable } if txID != 0 && infos.MaxTXID() < txID { return nil, ErrTxNotAvailable } return infos, nil } type restoreLevelCursor struct { // itr streams LTX file infos for a single level in filename order. itr ltx.FileIterator // current holds the last item read from itr but not yet evaluated. current *ltx.FileInfo // candidate is the best eligible file at this level for the currentMax. candidate *ltx.FileInfo // done indicates the iterator has been exhausted or errored. done bool } func (c *restoreLevelCursor) refresh(currentMax, txID ltx.TXID, timestamp time.Time) error { // Advance the iterator until we've evaluated all files that could be // contiguous with currentMax. Keep the best eligible candidate. if c.done { return nil } if c.candidate != nil && c.candidate.MaxTXID <= currentMax { c.candidate = nil } for { if err := c.ensureCurrent(); err != nil { return err } if c.done { return nil } info := c.current if info.MinTXID > currentMax+1 { return nil } c.current = nil if info.MaxTXID <= currentMax { continue } if txID != 0 && info.MaxTXID > txID { continue } if !timestamp.IsZero() && !info.CreatedAt.Before(timestamp) { continue } if c.candidate == nil || restoreCandidateBetter(c.candidate, info) { c.candidate = info } } } func (c *restoreLevelCursor) ensureCurrent() error { // Ensure current is populated with the next iterator item, or mark done. if c.done || c.current != nil { return nil } if !c.itr.Next() { if err := c.itr.Err(); err != nil { return err } c.done = true return nil } c.current = c.itr.Item() return nil } func restoreCandidateBetter(curr, next *ltx.FileInfo) bool { if next.MaxTXID != curr.MaxTXID { return next.MaxTXID > curr.MaxTXID } if next.MinTXID != curr.MinTXID { return next.MinTXID < curr.MinTXID } if next.Level != curr.Level { return next.Level > curr.Level } return next.CreatedAt.Before(curr.CreatedAt) } // TXIDPath returns the path to the TXID sidecar file for the given database path. // Uses -txid suffix to match SQLite's naming convention for associated files (-wal, -shm). func TXIDPath(outputPath string) string { return outputPath + "-txid" } // WriteTXIDFile atomically writes a TXID to a sidecar file at -txid. // Uses temp-file + fsync + rename for crash safety. func WriteTXIDFile(outputPath string, txid ltx.TXID) error { txidPath := TXIDPath(outputPath) tmpPath := txidPath + ".tmp" f, err := os.Create(tmpPath) if err != nil { return fmt.Errorf("create txid temp file: %w", err) } defer f.Close() defer os.Remove(tmpPath) if _, err := fmt.Fprintln(f, txid); err != nil { return fmt.Errorf("write txid: %w", err) } if err := f.Sync(); err != nil { return fmt.Errorf("sync txid file: %w", err) } if err := f.Close(); err != nil { return fmt.Errorf("close txid file: %w", err) } if err := os.Rename(tmpPath, txidPath); err != nil { return fmt.Errorf("rename txid file: %w", err) } dir, err := os.Open(filepath.Dir(txidPath)) if err != nil { return fmt.Errorf("open txid dir for sync: %w", err) } if err := dir.Sync(); err != nil { _ = dir.Close() return fmt.Errorf("sync txid dir: %w", err) } return dir.Close() } // ReadTXIDFile reads the TXID from a sidecar file at -txid. // Returns 0, nil if the file does not exist (first run). func ReadTXIDFile(outputPath string) (ltx.TXID, error) { txidPath := TXIDPath(outputPath) data, err := os.ReadFile(txidPath) if os.IsNotExist(err) { return 0, nil } else if err != nil { return 0, fmt.Errorf("read txid file: %w", err) } txid, err := ltx.ParseTXID(strings.TrimSpace(string(data))) if err != nil { return 0, fmt.Errorf("parse txid file: %w", err) } return txid, nil } // ValidationError represents a single validation issue. type ValidationError struct { Level int // compaction level Type string // "gap", "overlap", or "unsorted" Message string // human-readable description PrevFile *ltx.FileInfo // previous file CurrFile *ltx.FileInfo // current file that caused error } // ValidateLevel checks LTX files at the given level are sorted and contiguous. // Returns a slice of validation errors (empty if valid). func (r *Replica) ValidateLevel(ctx context.Context, level int) ([]ValidationError, error) { itr, err := r.Client.LTXFiles(ctx, level, 0, false) if err != nil { return nil, fmt.Errorf("fetch ltx files: %w", err) } defer itr.Close() var errors []ValidationError var prevInfo *ltx.FileInfo for itr.Next() { info := itr.Item() // Skip first file - nothing to compare against if prevInfo == nil { prevInfo = info continue } // Check for sort order: curr.MinTXID should be >= prev.MinTXID if info.MinTXID < prevInfo.MinTXID { errors = append(errors, ValidationError{ Level: level, Type: "unsorted", Message: fmt.Sprintf("files out of order: curr.MinTXID=%s < prev.MinTXID=%s", info.MinTXID, prevInfo.MinTXID), PrevFile: prevInfo, CurrFile: info, }) prevInfo = info continue } // Check for TXID contiguity: prev.MaxTXID + 1 should equal curr.MinTXID expectedMinTXID := prevInfo.MaxTXID + 1 if info.MinTXID != expectedMinTXID { if info.MinTXID > expectedMinTXID { errors = append(errors, ValidationError{ Level: level, Type: "gap", Message: fmt.Sprintf("TXID gap: prev.MaxTXID=%s, curr.MinTXID=%s (expected %s)", prevInfo.MaxTXID, info.MinTXID, expectedMinTXID), PrevFile: prevInfo, CurrFile: info, }) } else { errors = append(errors, ValidationError{ Level: level, Type: "overlap", Message: fmt.Sprintf("TXID overlap: prev.MaxTXID=%s, curr.MinTXID=%s", prevInfo.MaxTXID, info.MinTXID), PrevFile: prevInfo, CurrFile: info, }) } } prevInfo = info } if err := itr.Close(); err != nil { return nil, fmt.Errorf("close iterator: %w", err) } return errors, nil } ================================================ FILE: replica_client.go ================================================ package litestream import ( "bufio" "bytes" "context" "encoding/binary" "errors" "fmt" "io" "log/slog" "github.com/superfly/ltx" ) var ErrStopIter = errors.New("stop iterator") // ReplicaClient represents client to connect to a Replica. type ReplicaClient interface { // Type returns the type of client. Type() string // Init initializes the replica client connection. // This may establish connections, validate configuration, etc. // Implementations should be idempotent (no-op if already initialized). Init(ctx context.Context) error // LTXFiles returns an iterator of all LTX files on the replica for a given level. // If seek is specified, the iterator start from the given TXID or the next available if not found. // If useMetadata is true, the iterator fetches accurate timestamps from metadata for timestamp-based restore. // When false, the iterator uses fast timestamps (LastModified/Created/ModTime) for normal operations. LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) // OpenLTXFile returns a reader that contains an LTX file at a given TXID. // If seek is specified, the reader will start at the given offset. // Returns an os.ErrNotFound error if the LTX file does not exist. OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) // WriteLTXFile writes an LTX file to the replica. // Returns metadata for the written file. WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) // DeleteLTXFiles deletes one or more LTX files. DeleteLTXFiles(ctx context.Context, a []*ltx.FileInfo) error // DeleteAll deletes all files. DeleteAll(ctx context.Context) error // SetLogger sets the logger for the client. SetLogger(logger *slog.Logger) } // FindLTXFiles returns a list of files that match filter. // The useMetadata parameter is passed through to LTXFiles to control whether accurate timestamps // are fetched from metadata. When true (timestamp-based restore), accurate timestamps are required. // When false (normal operations), fast timestamps are sufficient. func FindLTXFiles(ctx context.Context, client ReplicaClient, level int, useMetadata bool, filter func(*ltx.FileInfo) (bool, error)) ([]*ltx.FileInfo, error) { itr, err := client.LTXFiles(ctx, level, 0, useMetadata) if err != nil { return nil, err } defer func() { _ = itr.Close() }() var a []*ltx.FileInfo for itr.Next() { item := itr.Item() match, err := filter(item) if match { a = append(a, item) } if errors.Is(err, ErrStopIter) { break } else if err != nil { return a, err } } if err := itr.Close(); err != nil { return nil, err } return a, nil } // DefaultEstimatedPageIndexSize is size that is first fetched when fetching the page index. // If the fetch was smaller than the actual page index, another call is made to fetch the rest. const DefaultEstimatedPageIndexSize = 32 * 1024 // 32KB func FetchPageIndex(ctx context.Context, client ReplicaClient, info *ltx.FileInfo) (map[uint32]ltx.PageIndexElem, error) { rc, err := fetchPageIndexData(ctx, client, info) if err != nil { return nil, err } defer rc.Close() return ltx.DecodePageIndex(bufio.NewReader(rc), info.Level, info.MinTXID, info.MaxTXID) } // FetchLTXHeader reads & returns the LTX header for the given file info. func FetchLTXHeader(ctx context.Context, client ReplicaClient, info *ltx.FileInfo) (ltx.Header, error) { rc, err := client.OpenLTXFile(ctx, info.Level, info.MinTXID, info.MaxTXID, 0, ltx.HeaderSize) if err != nil { return ltx.Header{}, fmt.Errorf("open ltx file: %w", err) } defer rc.Close() hdr, _, err := ltx.PeekHeader(rc) if err != nil { return ltx.Header{}, fmt.Errorf("peek header: %w", err) } return hdr, nil } // fetchPageIndexData fetches a chunk of the end of the file to get the page index. // If the fetch was smaller than the actual page index, another call is made to fetch the rest. func fetchPageIndexData(ctx context.Context, client ReplicaClient, info *ltx.FileInfo) (io.ReadCloser, error) { // Fetch the end of the file to get the page index. offset := info.Size - DefaultEstimatedPageIndexSize if offset < 0 { offset = 0 } f, err := client.OpenLTXFile(ctx, info.Level, info.MinTXID, info.MaxTXID, offset, 0) if err != nil { return nil, fmt.Errorf("open ltx file: %w", err) } defer f.Close() // If we have read the full size of the page index, return the page index block as a reader. b, err := io.ReadAll(f) if err != nil { return nil, fmt.Errorf("read ltx page index: %w", err) } size := binary.BigEndian.Uint64(b[len(b)-ltx.TrailerSize-8:]) if off := len(b) - int(size) - ltx.TrailerSize - 8; off > 0 { return io.NopCloser(bytes.NewReader(b[off:])), nil } // Otherwise read the file from the start of the page index. f, err = client.OpenLTXFile(ctx, info.Level, info.MinTXID, info.MaxTXID, info.Size-ltx.TrailerSize-8-int64(size), 0) if err != nil { return nil, fmt.Errorf("open ltx file: %w", err) } return f, nil } // FetchPage fetches and decodes a single page frame from an LTX file. func FetchPage(ctx context.Context, client ReplicaClient, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (ltx.PageHeader, []byte, error) { f, err := client.OpenLTXFile(ctx, level, minTXID, maxTXID, offset, size) if err != nil { return ltx.PageHeader{}, nil, fmt.Errorf("open ltx file: %w", err) } defer f.Close() b, err := io.ReadAll(f) if err != nil { return ltx.PageHeader{}, nil, fmt.Errorf("read ltx page frame: %w", err) } return ltx.DecodePageData(b) } ================================================ FILE: replica_client_test.go ================================================ package litestream_test import ( "bytes" "context" "errors" "fmt" "io" "log/slog" "math/rand" "os" "slices" "strings" "testing" "time" "github.com/superfly/ltx" "golang.org/x/crypto/ssh" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/internal/testingutil" "github.com/benbjohnson/litestream/s3" ) // createLTXData creates a minimal valid LTX file with a header for testing. // The data parameter is appended after the header for testing purposes. func createLTXData(minTXID, maxTXID ltx.TXID, data []byte) []byte { return createLTXDataWithTimestamp(minTXID, maxTXID, time.Now(), data) } func createLTXDataWithTimestamp(minTXID, maxTXID ltx.TXID, ts time.Time, data []byte) []byte { hdr := ltx.Header{ Version: ltx.Version, PageSize: 4096, Commit: 1, MinTXID: minTXID, MaxTXID: maxTXID, Timestamp: ts.UnixMilli(), } if minTXID == 1 { // Snapshot files do not include a checksum. hdr.PreApplyChecksum = 0 } else { hdr.PreApplyChecksum = ltx.ChecksumFlag } headerBytes, _ := hdr.MarshalBinary() return append(headerBytes, data...) } func TestReplicaClient_LTX(t *testing.T) { RunWithReplicaClient(t, "OK", func(t *testing.T, c litestream.ReplicaClient) { t.Helper() t.Parallel() // Write files out of order to check for sorting. if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(4), ltx.TXID(8), bytes.NewReader(createLTXData(4, 8, []byte(`67`)))); err != nil { t.Fatal(err) } if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(1), bytes.NewReader(createLTXData(1, 1, []byte(``)))); err != nil { t.Fatal(err) } if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(9), ltx.TXID(9), bytes.NewReader(createLTXData(9, 9, []byte(`xyz`)))); err != nil { t.Fatal(err) } if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(2), ltx.TXID(3), bytes.NewReader(createLTXData(2, 3, []byte(`12345`)))); err != nil { t.Fatal(err) } itr, err := c.LTXFiles(context.Background(), 0, 0, false) if err != nil { t.Fatal(err) } defer itr.Close() // Read all items and ensure they are sorted. a, err := ltx.SliceFileIterator(itr) if err != nil { t.Fatal(err) } else if got, want := len(a), 4; got != want { t.Fatalf("len=%v, want %v", got, want) } // Check that files are sorted by MinTXID (Size no longer checked since we add LTX headers) if got, want := a[0].MinTXID, ltx.TXID(1); got != want { t.Fatalf("Index[0].MinTXID=%v, want %v", got, want) } if got, want := a[0].MaxTXID, ltx.TXID(1); got != want { t.Fatalf("Index[0].MaxTXID=%v, want %v", got, want) } if got, want := a[1].MinTXID, ltx.TXID(2); got != want { t.Fatalf("Index[1].MinTXID=%v, want %v", got, want) } if got, want := a[1].MaxTXID, ltx.TXID(3); got != want { t.Fatalf("Index[1].MaxTXID=%v, want %v", got, want) } if got, want := a[2].MinTXID, ltx.TXID(4); got != want { t.Fatalf("Index[2].MinTXID=%v, want %v", got, want) } if got, want := a[2].MaxTXID, ltx.TXID(8); got != want { t.Fatalf("Index[2].MaxTXID=%v, want %v", got, want) } if got, want := a[3].MinTXID, ltx.TXID(9); got != want { t.Fatalf("Index[3].MinTXID=%v, want %v", got, want) } if got, want := a[3].MaxTXID, ltx.TXID(9); got != want { t.Fatalf("Index[3].MaxTXID=%v, want %v", got, want) } if err := itr.Close(); err != nil { t.Fatal(err) } }) RunWithReplicaClient(t, "NoWALs", func(t *testing.T, c litestream.ReplicaClient) { t.Helper() t.Parallel() itr, err := c.LTXFiles(context.Background(), 0, 0, false) if err != nil { t.Fatal(err) } defer itr.Close() if itr.Next() { t.Fatal("expected no wal files") } }) } func TestReplicaClient_WriteLTXFile(t *testing.T) { RunWithReplicaClient(t, "OK", func(t *testing.T, c litestream.ReplicaClient) { t.Helper() t.Parallel() testData := []byte(`foobar`) ltxData := createLTXData(1, 2, testData) expectedContent := ltxData if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(2), bytes.NewReader(expectedContent)); err != nil { t.Fatal(err) } r, err := c.OpenLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(2), 0, 0) if err != nil { t.Fatal(err) } defer func() { _ = r.Close() }() buf, err := io.ReadAll(r) if err != nil { t.Fatal(err) } if err := r.Close(); err != nil { t.Fatal(err) } if got, want := string(buf), string(expectedContent); got != want { t.Fatalf("data=%q, want %q", got, want) } }) } func TestReplicaClient_OpenLTXFile(t *testing.T) { RunWithReplicaClient(t, "OK", func(t *testing.T, c litestream.ReplicaClient) { t.Helper() t.Parallel() testData := []byte(`foobar`) ltxData := createLTXData(1, 2, testData) expectedContent := ltxData if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(2), bytes.NewReader(expectedContent)); err != nil { t.Fatal(err) } r, err := c.OpenLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(2), 0, 0) if err != nil { t.Fatal(err) } defer r.Close() if buf, err := io.ReadAll(r); err != nil { t.Fatal(err) } else if got, want := string(buf), string(expectedContent); got != want { t.Fatalf("ReadAll=%v, want %v", got, want) } }) RunWithReplicaClient(t, "ErrNotFound", func(t *testing.T, c litestream.ReplicaClient) { t.Helper() t.Parallel() if _, err := c.OpenLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(1), 0, 0); !errors.Is(err, os.ErrNotExist) { t.Fatalf("expected not exist, got %#v", err) } }) } func TestReplicaClient_DeleteWALSegments(t *testing.T) { RunWithReplicaClient(t, "OK", func(t *testing.T, c litestream.ReplicaClient) { t.Helper() t.Parallel() if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(2), bytes.NewReader(createLTXData(1, 2, []byte(`foo`)))); err != nil { t.Fatal(err) } if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(3), ltx.TXID(4), bytes.NewReader(createLTXData(3, 4, []byte(`bar`)))); err != nil { t.Fatal(err) } if err := c.DeleteLTXFiles(context.Background(), []*ltx.FileInfo{ {Level: 0, MinTXID: 1, MaxTXID: 2}, {Level: 0, MinTXID: 3, MaxTXID: 4}, }); err != nil { t.Fatal(err) } if _, err := c.OpenLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(2), 0, 0); !errors.Is(err, os.ErrNotExist) { t.Fatalf("expected not exist, got %#v", err) } if _, err := c.OpenLTXFile(context.Background(), 0, ltx.TXID(3), ltx.TXID(4), 0, 0); !errors.Is(err, os.ErrNotExist) { t.Fatalf("expected not exist, got %#v", err) } }) } // RunWithReplicaClient executes fn with each replica specified by the -integration flag func RunWithReplicaClient(t *testing.T, name string, fn func(*testing.T, litestream.ReplicaClient)) { t.Run(name, func(t *testing.T) { for _, typ := range testingutil.ReplicaClientTypes() { t.Run(typ, func(t *testing.T) { if !testingutil.Integration() { t.Skip("skipping integration test, use -integration flag to run") } c := testingutil.NewReplicaClient(t, typ) defer testingutil.MustDeleteAll(t, c) fn(t, c) }) } }) } // TestReplicaClient_TimestampPreservation verifies that LTX file timestamps are preserved // during write and read operations. This is critical for point-in-time restoration (#771). func TestReplicaClient_TimestampPreservation(t *testing.T) { RunWithReplicaClient(t, "PreservesTimestamp", func(t *testing.T, c litestream.ReplicaClient) { t.Helper() t.Parallel() ctx := context.Background() // Create an LTX file with a specific timestamp // Use a timestamp from the past to ensure it's different from write time expectedTimestamp := time.Now().Add(-1 * time.Hour).Truncate(time.Millisecond) ltxData := createLTXDataWithTimestamp(1, 1, expectedTimestamp, []byte("payload")) info, err := c.WriteLTXFile(ctx, 0, ltx.TXID(1), ltx.TXID(1), bytes.NewReader(ltxData)) if err != nil { t.Fatal(err) } // For File backend, timestamp should be preserved immediately // For cloud backends (S3, GCS, Azure, NATS), timestamp is stored in metadata // Verify the returned FileInfo has correct timestamp if info.CreatedAt.IsZero() { t.Fatal("WriteLTXFile returned zero timestamp") } // Read back via LTXFiles and verify timestamp is preserved itr, err := c.LTXFiles(ctx, 0, 0, true) if err != nil { t.Fatal(err) } defer itr.Close() var found *ltx.FileInfo for itr.Next() { item := itr.Item() if item.MinTXID == 1 && item.MaxTXID == 1 { found = item break } } if err := itr.Close(); err != nil { t.Fatal(err) } if found == nil { t.Fatal("LTX file not found in iteration") } // All backends preserve timestamps in metadata (see issue #771) // Verify timestamp was preserved (allow 1 second drift for precision) timeDiff := found.CreatedAt.Sub(expectedTimestamp) if timeDiff.Abs() > time.Second { t.Errorf("Timestamp not preserved for backend %T: expected %v, got %v (diff: %v)", c, expectedTimestamp, found.CreatedAt, timeDiff) } }) } // TestReplicaClient_S3_UploaderConfig tests S3 uploader configuration for large files func TestReplicaClient_S3_UploaderConfig(t *testing.T) { // Only run for S3 integration tests if !slices.Contains(testingutil.ReplicaClientTypes(), "s3") { t.Skip("Skipping S3-specific uploader config test") } RunWithReplicaClient(t, "LargeFileWithCustomConfig", func(t *testing.T, c litestream.ReplicaClient) { t.Helper() t.Parallel() // Type assert to S3 client to set custom config s3Client, ok := c.(*s3.ReplicaClient) if !ok { t.Skip("Not an S3 client") } // Set custom upload configuration s3Client.PartSize = 5 * 1024 * 1024 // 5MB parts s3Client.Concurrency = 3 // 3 concurrent parts // Determine file size based on whether we're testing against moto or real S3 // Moto has issue #8762 where composite checksums for multipart uploads // don't have the -X suffix, causing checksum validation to fail. // Reference: https://github.com/getmoto/moto/issues/8762 size := 10 * 1024 * 1024 // 10MB - triggers multipart upload // If we're using moto (localhost endpoint), use smaller file to avoid multipart if s3Client.Endpoint != "" && strings.Contains(s3Client.Endpoint, "127.0.0.1") { size = 4 * 1024 * 1024 // 4MB - avoids multipart upload with moto t.Log("Using 4MB file size to work around moto multipart checksum issue") } else { t.Log("Using 10MB file size to test multipart upload") } payload := make([]byte, size) for i := range payload { payload[i] = byte(i % 256) } ltxData := createLTXData(1, 100, payload) // Upload the file using bytes.Reader to avoid string conversion issues if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(100), bytes.NewReader(ltxData)); err != nil { t.Fatalf("failed to write large file: %v", err) } // Read it back and verify size r, err := c.OpenLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(100), 0, 0) if err != nil { t.Fatalf("failed to open large file: %v", err) } defer r.Close() buf, err := io.ReadAll(r) if err != nil { t.Fatalf("failed to read large file: %v", err) } if len(buf) != len(ltxData) { t.Errorf("size mismatch: got %d, want %d", len(buf), len(ltxData)) } // Verify the data matches what we uploaded if !bytes.Equal(buf, ltxData) { t.Errorf("data mismatch: uploaded and downloaded data do not match") } }) } // TestReplicaClient_S3_ErrorContext tests that S3 errors include helpful context func TestReplicaClient_S3_ErrorContext(t *testing.T) { // Only run for S3 integration tests if !slices.Contains(testingutil.ReplicaClientTypes(), "s3") { t.Skip("Skipping S3-specific error context test") } RunWithReplicaClient(t, "ErrorContext", func(t *testing.T, c litestream.ReplicaClient) { t.Helper() t.Parallel() // Test OpenLTXFile with non-existent file _, err := c.OpenLTXFile(context.Background(), 0, ltx.TXID(999), ltx.TXID(999), 0, 0) if err == nil { t.Fatal("expected error for non-existent file") } // Should return os.ErrNotExist for S3 NoSuchKey if !errors.Is(err, os.ErrNotExist) { t.Errorf("expected os.ErrNotExist, got %v", err) } }) } // TestReplicaClient_S3_BucketValidation tests bucket validation in S3 client func TestReplicaClient_S3_BucketValidation(t *testing.T) { // Only run for S3 integration tests if !slices.Contains(testingutil.ReplicaClientTypes(), "s3") { t.Skip("Skipping S3-specific bucket validation test") } // Create a new S3 client with empty bucket c := testingutil.NewS3ReplicaClient(t) c.Bucket = "" // Should fail with bucket validation error err := c.Init(context.Background()) if err == nil { t.Fatal("expected error for empty bucket name") } if !strings.Contains(err.Error(), "bucket name is required") { t.Errorf("expected bucket validation error, got: %v", err) } } // TestReplicaClient_S3_UnsignedPayloadRejected verifies that unsigned payloads // are rejected by real AWS S3. This is a negative test that documents the // expected behavior and ensures we don't accidentally ship unsigned payload // support for AWS S3. // // See issue #911 - AWS S3 requires signed payloads and returns // SignatureDoesNotMatch for unsigned payload requests. func TestReplicaClient_S3_UnsignedPayloadRejected(t *testing.T) { // Only run for S3 integration tests if !slices.Contains(testingutil.ReplicaClientTypes(), "s3") { t.Skip("Skipping S3-specific test") } // Skip if using mock endpoint (moto accepts unsigned payloads) if endpoint := os.Getenv("LITESTREAM_S3_ENDPOINT"); endpoint != "" { t.Skip("Skipping negative test with mock endpoint (moto accepts unsigned)") } // Create client directly (not via test helper) to control SignPayload c := s3.NewReplicaClient() c.AccessKeyID = os.Getenv("LITESTREAM_S3_ACCESS_KEY_ID") c.SecretAccessKey = os.Getenv("LITESTREAM_S3_SECRET_ACCESS_KEY") c.Region = os.Getenv("LITESTREAM_S3_REGION") if c.Region == "" { c.Region = "us-east-1" } c.Bucket = os.Getenv("LITESTREAM_S3_BUCKET") c.Path = fmt.Sprintf("negative-test/%016x", rand.Uint64()) // Force unsigned payloads - this should fail with real AWS c.SignPayload = false ctx := context.Background() if err := c.Init(ctx); err != nil { t.Fatalf("Init failed: %v", err) } // Attempt to write - should fail with signature error ltxData := createLTXData(1, 1, []byte("test")) _, err := c.WriteLTXFile(ctx, 0, ltx.TXID(1), ltx.TXID(1), bytes.NewReader(ltxData)) if err == nil { t.Fatal("expected unsigned payload to be rejected by AWS S3, but upload succeeded") } // Verify it's a signature-related error errStr := strings.ToLower(err.Error()) if !strings.Contains(errStr, "signature") && !strings.Contains(errStr, "accessdenied") { t.Errorf("expected signature-related error, got: %v", err) } t.Logf("Correctly rejected unsigned payload with error: %v", err) } func TestReplicaClient_SFTP_HostKeyValidation(t *testing.T) { testHostKeyPEM := `-----BEGIN OPENSSH PRIVATE KEY----- b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW QyNTUxOQAAACAJytPhncDnpV5QF3ai8f6r0u1hzK96x+81tvtA7ZiuawAAAJAIcGGVCHBh lQAAAAtzc2gtZWQyNTUxOQAAACAJytPhncDnpV5QF3ai8f6r0u1hzK96x+81tvtA7Ziuaw AAAEDzV1D6COyvFGhSiZa6ll9aXZ2IMWED3KGrvCNjEEtYHwnK0+GdwOelXlAXdqLx/qvS 7WHMr3rH7zW2+0DtmK5rAAAADGZlbGl4QGJvcmVhcwE= -----END OPENSSH PRIVATE KEY-----` privateKey, err := ssh.ParsePrivateKey([]byte(testHostKeyPEM)) if err != nil { t.Fatal(err) } t.Run("ValidHostKey", func(t *testing.T) { addr := testingutil.MockSFTPServer(t, privateKey) expectedHostKey := string(ssh.MarshalAuthorizedKey(privateKey.PublicKey())) c := testingutil.NewSFTPReplicaClient(t) c.User = "foo" c.Host = addr c.HostKey = expectedHostKey err = c.Init(context.Background()) if err != nil { t.Fatalf("SFTP connection failed: %v", err) } }) t.Run("InvalidHostKey", func(t *testing.T) { addr := testingutil.MockSFTPServer(t, privateKey) invalidHostKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEqM2NkGvKKhR1oiKO0E72L3tOsYk+aX7H8Xn4bbZKsa" c := testingutil.NewSFTPReplicaClient(t) c.User = "foo" c.Host = addr c.HostKey = invalidHostKey err = c.Init(context.Background()) if err == nil { t.Fatalf("SFTP connection established despite invalid host key") } if !strings.Contains(err.Error(), "ssh: host key mismatch") { t.Errorf("expected host key validation error, got: %v", err) } }) t.Run("IgnoreHostKey", func(t *testing.T) { var captured []string slog.SetDefault(slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{ Level: slog.LevelWarn, ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { if a.Key == slog.MessageKey { captured = append(captured, a.Value.String()) } return a }, }))) addr := testingutil.MockSFTPServer(t, privateKey) c := testingutil.NewSFTPReplicaClient(t) c.User = "foo" c.Host = addr err = c.Init(context.Background()) if err != nil { t.Fatalf("SFTP connection failed: %v", err) } if !slices.ContainsFunc(captured, func(msg string) bool { return strings.Contains(msg, "sftp host key not verified") }) { t.Errorf("Expected warning not found") } }) } // TestReplicaClient_S3_MultipartThresholds tests multipart upload behavior at various // size thresholds. These tests are critical for catching S3-compatible provider issues // like #940, #941, #947 where multipart uploads fail with certain providers. // // NOTE: These tests skip moto due to multipart checksum validation issues (moto#8762). // They should be run against real cloud providers using the manual integration workflow. func TestReplicaClient_S3_MultipartThresholds(t *testing.T) { if !slices.Contains(testingutil.ReplicaClientTypes(), "s3") { t.Skip("Skipping S3-specific multipart threshold tests") } // Skip if using mock endpoint (moto has multipart checksum issues) if endpoint := os.Getenv("LITESTREAM_S3_ENDPOINT"); endpoint != "" { if strings.Contains(endpoint, "127.0.0.1") || strings.Contains(endpoint, "localhost") { t.Skip("Skipping multipart tests with mock endpoint (moto has checksum issues)") } } tests := []struct { name string sizeMB int partSize int64 }{ { name: "AtThreshold_5MB", sizeMB: 5, partSize: 5 * 1024 * 1024, }, { name: "AboveThreshold_10MB", sizeMB: 10, partSize: 5 * 1024 * 1024, }, { name: "Large_50MB", sizeMB: 50, partSize: 10 * 1024 * 1024, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if !testingutil.Integration() { t.Skip("skipping integration test, use -integration flag to run") } c := testingutil.NewS3ReplicaClient(t) c.Path = fmt.Sprintf("multipart-test/%016x", rand.Uint64()) c.PartSize = tt.partSize c.Concurrency = 3 defer testingutil.MustDeleteAll(t, c) ctx := context.Background() if err := c.Init(ctx); err != nil { t.Fatalf("Init() error: %v", err) } size := tt.sizeMB * 1024 * 1024 payload := make([]byte, size) for i := range payload { payload[i] = byte(i % 256) } ltxData := createLTXData(1, 100, payload) t.Logf("Testing %dMB file with %dMB parts", tt.sizeMB, tt.partSize/(1024*1024)) if _, err := c.WriteLTXFile(ctx, 0, ltx.TXID(1), ltx.TXID(100), bytes.NewReader(ltxData)); err != nil { t.Fatalf("WriteLTXFile() error: %v", err) } r, err := c.OpenLTXFile(ctx, 0, ltx.TXID(1), ltx.TXID(100), 0, 0) if err != nil { t.Fatalf("OpenLTXFile() error: %v", err) } defer r.Close() buf, err := io.ReadAll(r) if err != nil { t.Fatalf("ReadAll() error: %v", err) } if len(buf) != len(ltxData) { t.Errorf("size mismatch: got %d, want %d", len(buf), len(ltxData)) } if !bytes.Equal(buf, ltxData) { t.Errorf("data mismatch: uploaded and downloaded data do not match") } }) } } // TestReplicaClient_S3_ConcurrencyLimits tests that concurrency limits are respected // during multipart uploads. This is important for providers like Cloudflare R2 that // have strict concurrent upload limits (issue #948). func TestReplicaClient_S3_ConcurrencyLimits(t *testing.T) { if !slices.Contains(testingutil.ReplicaClientTypes(), "s3") { t.Skip("Skipping S3-specific concurrency test") } // Skip if using mock endpoint if endpoint := os.Getenv("LITESTREAM_S3_ENDPOINT"); endpoint != "" { if strings.Contains(endpoint, "127.0.0.1") || strings.Contains(endpoint, "localhost") { t.Skip("Skipping concurrency test with mock endpoint") } } if !testingutil.Integration() { t.Skip("skipping integration test, use -integration flag to run") } concurrencyLevels := []int{1, 2, 5} for _, concurrency := range concurrencyLevels { t.Run(fmt.Sprintf("Concurrency_%d", concurrency), func(t *testing.T) { c := testingutil.NewS3ReplicaClient(t) c.Path = fmt.Sprintf("concurrency-test/%016x", rand.Uint64()) c.PartSize = 5 * 1024 * 1024 c.Concurrency = concurrency defer testingutil.MustDeleteAll(t, c) ctx := context.Background() if err := c.Init(ctx); err != nil { t.Fatalf("Init() error: %v", err) } size := 15 * 1024 * 1024 payload := make([]byte, size) for i := range payload { payload[i] = byte(i % 256) } ltxData := createLTXData(1, 100, payload) t.Logf("Testing 15MB file with concurrency=%d", concurrency) if _, err := c.WriteLTXFile(ctx, 0, ltx.TXID(1), ltx.TXID(100), bytes.NewReader(ltxData)); err != nil { t.Fatalf("WriteLTXFile() with concurrency=%d error: %v", concurrency, err) } r, err := c.OpenLTXFile(ctx, 0, ltx.TXID(1), ltx.TXID(100), 0, 0) if err != nil { t.Fatalf("OpenLTXFile() error: %v", err) } defer r.Close() buf, err := io.ReadAll(r) if err != nil { t.Fatalf("ReadAll() error: %v", err) } if !bytes.Equal(buf, ltxData) { t.Errorf("data mismatch at concurrency=%d", concurrency) } }) } } // TestReplicaClient_PITR_ManyLTXFiles tests point-in-time restore with many LTX files. // This is a regression test for issue #930 where HeadObject calls with 100+ LTX files // caused the restore operation to hang. func TestReplicaClient_PITR_ManyLTXFiles(t *testing.T) { tests := []struct { name string fileCount int timeout time.Duration }{ {"100_Files", 100, 2 * time.Minute}, {"500_Files", 500, 5 * time.Minute}, {"1000_Files", 1000, 10 * time.Minute}, } for _, tt := range tests { RunWithReplicaClient(t, tt.name, func(t *testing.T, c litestream.ReplicaClient) { t.Helper() // Skip very long tests unless explicitly enabled if tt.fileCount > 100 && os.Getenv("LITESTREAM_PITR_STRESS_TEST") == "" { t.Skipf("Skipping %d file stress test (set LITESTREAM_PITR_STRESS_TEST=1 to enable)", tt.fileCount) } ctx, cancel := context.WithTimeout(context.Background(), tt.timeout) defer cancel() baseTime := time.Now().Add(-time.Duration(tt.fileCount) * time.Minute) t.Logf("Creating %d LTX files starting from %v", tt.fileCount, baseTime) // Create snapshot at TXID 1 snapshot := createLTXDataWithTimestamp(1, 1, baseTime, []byte("snapshot")) if _, err := c.WriteLTXFile(ctx, litestream.SnapshotLevel, 1, 1, bytes.NewReader(snapshot)); err != nil { t.Fatalf("WriteLTXFile(snapshot): %v", err) } // Create many L0 files with incrementing timestamps for i := 2; i <= tt.fileCount; i++ { ts := baseTime.Add(time.Duration(i-1) * time.Minute) data := createLTXDataWithTimestamp(ltx.TXID(i), ltx.TXID(i), ts, []byte(fmt.Sprintf("file-%d", i))) if _, err := c.WriteLTXFile(ctx, 0, ltx.TXID(i), ltx.TXID(i), bytes.NewReader(data)); err != nil { t.Fatalf("WriteLTXFile(%d): %v", i, err) } if i%100 == 0 { t.Logf("Created %d/%d files", i, tt.fileCount) } } // Test 1: Iterate all L0 files without metadata (fast path) t.Log("Testing L0 file iteration without metadata") startFast := time.Now() itr, err := c.LTXFiles(ctx, 0, 0, false) if err != nil { t.Fatalf("LTXFiles(useMetadata=false): %v", err) } var countFast int for itr.Next() { countFast++ } if err := itr.Close(); err != nil { t.Fatalf("Iterator close: %v", err) } durationFast := time.Since(startFast) t.Logf("Fast iteration: %d files in %v", countFast, durationFast) if countFast != tt.fileCount-1 { t.Errorf("Fast iteration count: got %d, want %d", countFast, tt.fileCount-1) } // Test 2: Iterate all L0 files with metadata (required for PITR) // This is the path that was hanging in issue #930 t.Log("Testing L0 file iteration with metadata (PITR path)") startMeta := time.Now() itrMeta, err := c.LTXFiles(ctx, 0, 0, true) if err != nil { t.Fatalf("LTXFiles(useMetadata=true): %v", err) } var countMeta int for itrMeta.Next() { countMeta++ } if err := itrMeta.Close(); err != nil { t.Fatalf("Iterator close: %v", err) } durationMeta := time.Since(startMeta) t.Logf("Metadata iteration: %d files in %v", countMeta, durationMeta) if countMeta != tt.fileCount-1 { t.Errorf("Metadata iteration count: got %d, want %d", countMeta, tt.fileCount-1) } // Verify metadata iteration completed within reasonable time // (issue #930 caused this to hang indefinitely) if durationMeta > tt.timeout/2 { t.Errorf("Metadata iteration took too long: %v (should be < %v)", durationMeta, tt.timeout/2) } }) } } // TestReplicaClient_PITR_TimestampFiltering tests that PITR correctly filters files // by timestamp across a range of LTX files. func TestReplicaClient_PITR_TimestampFiltering(t *testing.T) { RunWithReplicaClient(t, "TimestampFilter", func(t *testing.T, c litestream.ReplicaClient) { t.Helper() ctx := context.Background() fileCount := 50 baseTime := time.Now().Add(-time.Duration(fileCount) * time.Minute) // Create snapshot at TXID 1 snapshot := createLTXDataWithTimestamp(1, 1, baseTime, []byte("snapshot")) if _, err := c.WriteLTXFile(ctx, litestream.SnapshotLevel, 1, 1, bytes.NewReader(snapshot)); err != nil { t.Fatalf("WriteLTXFile(snapshot): %v", err) } // Create L0 files with known timestamps for i := 2; i <= fileCount; i++ { ts := baseTime.Add(time.Duration(i-1) * time.Minute) data := createLTXDataWithTimestamp(ltx.TXID(i), ltx.TXID(i), ts, []byte(fmt.Sprintf("file-%d", i))) if _, err := c.WriteLTXFile(ctx, 0, ltx.TXID(i), ltx.TXID(i), bytes.NewReader(data)); err != nil { t.Fatalf("WriteLTXFile(%d): %v", i, err) } } // Test filtering at various timestamp points testPoints := []struct { name string offsetMins int expectCount int }{ {"Beginning", 5, 4}, // Files 2-5 (4 files) {"Quarter", 12, 11}, // Files 2-12 (11 files) {"Middle", 25, 24}, // Files 2-25 (24 files) {"ThreeQuarters", 37, 36}, // Files 2-37 (36 files) {"End", fileCount - 1, fileCount - 2}, // All but last } for _, tp := range testPoints { t.Run(tp.name, func(t *testing.T) { targetTime := baseTime.Add(time.Duration(tp.offsetMins) * time.Minute) t.Logf("Filtering files before %v (offset: %d mins)", targetTime, tp.offsetMins) // Use LTXFiles with metadata to get accurate timestamps itr, err := c.LTXFiles(ctx, 0, 0, true) if err != nil { t.Fatalf("LTXFiles: %v", err) } defer itr.Close() var count int for itr.Next() { info := itr.Item() if info.CreatedAt.Before(targetTime) { count++ } } // Allow for timestamp precision variance if count < tp.expectCount-1 || count > tp.expectCount+1 { t.Errorf("Files before %v: got %d, expected ~%d", targetTime, count, tp.expectCount) } }) } }) } // TestReplicaClient_PITR_CalcRestorePlanWithManyFiles tests CalcRestorePlan with // a large number of LTX files. This ensures restore planning doesn't hang. func TestReplicaClient_PITR_CalcRestorePlanWithManyFiles(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) RunWithReplicaClient(t, "RestorePlan", func(t *testing.T, c litestream.ReplicaClient) { t.Helper() ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) defer cancel() fileCount := 100 baseTime := time.Now().Add(-time.Duration(fileCount) * time.Minute) // Create snapshot snapshot := createLTXDataWithTimestamp(1, 1, baseTime, []byte("snapshot")) if _, err := c.WriteLTXFile(ctx, litestream.SnapshotLevel, 1, 1, bytes.NewReader(snapshot)); err != nil { t.Fatalf("WriteLTXFile(snapshot): %v", err) } // Create L0 files for i := 2; i <= fileCount; i++ { ts := baseTime.Add(time.Duration(i-1) * time.Minute) data := createLTXDataWithTimestamp(ltx.TXID(i), ltx.TXID(i), ts, []byte(fmt.Sprintf("file-%d", i))) if _, err := c.WriteLTXFile(ctx, 0, ltx.TXID(i), ltx.TXID(i), bytes.NewReader(data)); err != nil { t.Fatalf("WriteLTXFile(%d): %v", i, err) } } // Test restore plan calculation at various points testTargets := []struct { name string txID ltx.TXID minFiles int }{ {"EarlyTXID", 10, 2}, // snapshot + some L0 {"MidTXID", 50, 2}, // snapshot + more L0 {"LateTXID", 90, 2}, // snapshot + most L0 {"LatestTXID", ltx.TXID(fileCount), 2}, // all files } logger := slog.Default() for _, target := range testTargets { t.Run(target.name, func(t *testing.T) { startTime := time.Now() plan, err := litestream.CalcRestorePlan(ctx, c, target.txID, time.Time{}, logger) if err != nil { t.Fatalf("CalcRestorePlan(%d): %v", target.txID, err) } duration := time.Since(startTime) t.Logf("CalcRestorePlan(txid=%d): %d files in %v", target.txID, len(plan), duration) if len(plan) < target.minFiles { t.Errorf("Plan has too few files: got %d, want >= %d", len(plan), target.minFiles) } // Verify plan doesn't take excessively long if duration > 30*time.Second { t.Errorf("CalcRestorePlan took too long: %v (should be < 30s)", duration) } }) } // Test timestamp-based restore plan t.Run("TimestampBased", func(t *testing.T) { // Target halfway through the files targetTime := baseTime.Add(time.Duration(fileCount/2) * time.Minute) startTime := time.Now() plan, err := litestream.CalcRestorePlan(ctx, c, 0, targetTime, logger) if err != nil { t.Fatalf("CalcRestorePlan(timestamp=%v): %v", targetTime, err) } duration := time.Since(startTime) t.Logf("CalcRestorePlan(timestamp=%v): %d files in %v", targetTime, len(plan), duration) if len(plan) < 2 { t.Errorf("Plan has too few files: got %d, want >= 2", len(plan)) } if duration > 60*time.Second { t.Errorf("Timestamp-based CalcRestorePlan took too long: %v", duration) } }) }) } ================================================ FILE: replica_internal_test.go ================================================ package litestream import ( "bytes" "context" "database/sql" "fmt" "io" "log/slog" "os" "path/filepath" "testing" "time" "github.com/superfly/ltx" _ "modernc.org/sqlite" ) func TestReplica_ApplyNewLTXFiles_FillGapWithOverlappingCompactedFile(t *testing.T) { const pageSize = 4096 compactedInfo := <x.FileInfo{Level: 1, MinTXID: 100, MaxTXID: 200} l0Info := <x.FileInfo{Level: 0, MinTXID: 201, MaxTXID: 201} fixtures := map[string][]byte{ ltxFixtureKey(compactedInfo.Level, compactedInfo.MinTXID, compactedInfo.MaxTXID): mustBuildIncrementalLTX(t, compactedInfo.MinTXID, compactedInfo.MaxTXID, pageSize, 1, 0xA1), ltxFixtureKey(l0Info.Level, l0Info.MinTXID, l0Info.MaxTXID): mustBuildIncrementalLTX(t, l0Info.MinTXID, l0Info.MaxTXID, pageSize, 1, 0xB2), } client := &followTestReplicaClient{} client.LTXFilesFunc = func(_ context.Context, level int, seek ltx.TXID, _ bool) (ltx.FileIterator, error) { var all []*ltx.FileInfo switch level { case 0: all = []*ltx.FileInfo{l0Info} case 1: all = []*ltx.FileInfo{compactedInfo} default: all = nil } infos := make([]*ltx.FileInfo, 0, len(all)) for _, info := range all { if info.MinTXID >= seek { infos = append(infos, info) } } return ltx.NewFileInfoSliceIterator(infos), nil } client.OpenLTXFileFunc = func(_ context.Context, level int, minTXID, maxTXID ltx.TXID, _, _ int64) (io.ReadCloser, error) { key := ltxFixtureKey(level, minTXID, maxTXID) data, ok := fixtures[key] if !ok { return nil, os.ErrNotExist } return io.NopCloser(bytes.NewReader(data)), nil } r := NewReplicaWithClient(nil, client) f := mustCreateWritableDBFile(t) defer func() { _ = f.Close() }() got, err := r.applyNewLTXFiles(context.Background(), f, 150, pageSize) if err != nil { t.Fatalf("apply new ltx files: %v", err) } if got != 201 { t.Fatalf("txid=%s, want %s", got, ltx.TXID(201)) } } func TestReplica_ApplyNewLTXFiles_LevelZeroEmptyFallsBackToCompaction(t *testing.T) { const pageSize = 4096 compactedInfo := <x.FileInfo{Level: 1, MinTXID: 11, MaxTXID: 12} fixtures := map[string][]byte{ ltxFixtureKey(compactedInfo.Level, compactedInfo.MinTXID, compactedInfo.MaxTXID): mustBuildIncrementalLTX(t, compactedInfo.MinTXID, compactedInfo.MaxTXID, pageSize, 1, 0xC3), } client := &followTestReplicaClient{} client.LTXFilesFunc = func(_ context.Context, level int, seek ltx.TXID, _ bool) (ltx.FileIterator, error) { switch level { case 0: return ltx.NewFileInfoSliceIterator(nil), nil case 1: if compactedInfo.MinTXID < seek { return ltx.NewFileInfoSliceIterator(nil), nil } return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{compactedInfo}), nil default: return ltx.NewFileInfoSliceIterator(nil), nil } } client.OpenLTXFileFunc = func(_ context.Context, level int, minTXID, maxTXID ltx.TXID, _, _ int64) (io.ReadCloser, error) { key := ltxFixtureKey(level, minTXID, maxTXID) data, ok := fixtures[key] if !ok { return nil, os.ErrNotExist } return io.NopCloser(bytes.NewReader(data)), nil } r := NewReplicaWithClient(nil, client) f := mustCreateWritableDBFile(t) defer func() { _ = f.Close() }() got, err := r.applyNewLTXFiles(context.Background(), f, 10, pageSize) if err != nil { t.Fatalf("apply new ltx files: %v", err) } if got != 12 { t.Fatalf("txid=%s, want %s", got, ltx.TXID(12)) } } func TestReplica_ApplyNewLTXFiles_IteratorCloseError(t *testing.T) { client := &followTestReplicaClient{} client.LTXFilesFunc = func(_ context.Context, level int, seek ltx.TXID, _ bool) (ltx.FileIterator, error) { if level == 0 { return &errorFileIterator{closeErr: fmt.Errorf("level 0 listing failed")}, nil } return ltx.NewFileInfoSliceIterator(nil), nil } client.OpenLTXFileFunc = func(_ context.Context, _ int, _, _ ltx.TXID, _, _ int64) (io.ReadCloser, error) { return nil, fmt.Errorf("unexpected open") } r := NewReplicaWithClient(nil, client) f := mustCreateWritableDBFile(t) defer func() { _ = f.Close() }() _, err := r.applyNewLTXFiles(context.Background(), f, 10, 4096) if err == nil { t.Fatal("expected error") } if got, want := err.Error(), "level 0 listing failed"; !bytes.Contains([]byte(got), []byte(want)) { t.Fatalf("error=%q, want substring %q", got, want) } } func TestReplica_ApplyLTXFile_VerifiesChecksumOnClose(t *testing.T) { const pageSize = 4096 info := <x.FileInfo{Level: 0, MinTXID: 20, MaxTXID: 20} data := mustBuildIncrementalLTX(t, info.MinTXID, info.MaxTXID, pageSize, 1, 0xD4) data[len(data)-1] ^= 0xFF client := &followTestReplicaClient{} client.OpenLTXFileFunc = func(_ context.Context, level int, minTXID, maxTXID ltx.TXID, _, _ int64) (io.ReadCloser, error) { if level != info.Level || minTXID != info.MinTXID || maxTXID != info.MaxTXID { return nil, os.ErrNotExist } return io.NopCloser(bytes.NewReader(data)), nil } r := NewReplicaWithClient(nil, client) f := mustCreateWritableDBFile(t) defer func() { _ = f.Close() }() err := r.applyLTXFile(context.Background(), f, info, pageSize) if err == nil { t.Fatal("expected checksum validation error") } } type errorFileIterator struct { closeErr error } type followTestReplicaClient struct { LTXFilesFunc func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) OpenLTXFileFunc func(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) WriteLTXFileFunc func(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) DeleteLTXFilesFunc func(ctx context.Context, a []*ltx.FileInfo) error DeleteAllFunc func(ctx context.Context) error } func (*followTestReplicaClient) Type() string { return "test" } func (*followTestReplicaClient) Init(context.Context) error { return nil } func (*followTestReplicaClient) SetLogger(*slog.Logger) {} func (c *followTestReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { if c.LTXFilesFunc != nil { return c.LTXFilesFunc(ctx, level, seek, useMetadata) } return ltx.NewFileInfoSliceIterator(nil), nil } func (c *followTestReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { if c.OpenLTXFileFunc != nil { return c.OpenLTXFileFunc(ctx, level, minTXID, maxTXID, offset, size) } return nil, os.ErrNotExist } func (c *followTestReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { if c.WriteLTXFileFunc != nil { return c.WriteLTXFileFunc(ctx, level, minTXID, maxTXID, r) } return nil, fmt.Errorf("not implemented") } func (c *followTestReplicaClient) DeleteLTXFiles(ctx context.Context, a []*ltx.FileInfo) error { if c.DeleteLTXFilesFunc != nil { return c.DeleteLTXFilesFunc(ctx, a) } return nil } func (c *followTestReplicaClient) DeleteAll(ctx context.Context) error { if c.DeleteAllFunc != nil { return c.DeleteAllFunc(ctx) } return nil } func (itr *errorFileIterator) Close() error { return itr.closeErr } func (itr *errorFileIterator) Next() bool { return false } func (itr *errorFileIterator) Err() error { return itr.closeErr } func (itr *errorFileIterator) Item() *ltx.FileInfo { return nil } func mustBuildIncrementalLTX(tb testing.TB, minTXID, maxTXID ltx.TXID, pageSize, pgno uint32, fill byte) []byte { tb.Helper() var buf bytes.Buffer enc, err := ltx.NewEncoder(&buf) if err != nil { tb.Fatal(err) } hdr := ltx.Header{ Version: ltx.Version, Flags: ltx.HeaderFlagNoChecksum, PageSize: pageSize, Commit: pgno, MinTXID: minTXID, MaxTXID: maxTXID, Timestamp: time.Now().UnixMilli(), } if err := enc.EncodeHeader(hdr); err != nil { tb.Fatal(err) } page := bytes.Repeat([]byte{fill}, int(pageSize)) if err := enc.EncodePage(ltx.PageHeader{Pgno: pgno}, page); err != nil { tb.Fatal(err) } if err := enc.Close(); err != nil { tb.Fatal(err) } return buf.Bytes() } func mustCreateWritableDBFile(tb testing.TB) *os.File { tb.Helper() path := filepath.Join(tb.TempDir(), "follower.db") f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0o600) if err != nil { tb.Fatal(err) } if err := f.Truncate(128 * 1024); err != nil { _ = f.Close() tb.Fatal(err) } return f } func ltxFixtureKey(level int, minTXID, maxTXID ltx.TXID) string { return fmt.Sprintf("%d:%s:%s", level, minTXID, maxTXID) } func mustCreateValidSQLiteDB(tb testing.TB) string { tb.Helper() dbPath := filepath.Join(tb.TempDir(), "test.db") db, err := sql.Open("sqlite", dbPath) if err != nil { tb.Fatal(err) } defer func() { _ = db.Close() }() if _, err := db.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY, name TEXT)"); err != nil { tb.Fatal(err) } if _, err := db.Exec("INSERT INTO t (name) VALUES ('a'), ('b'), ('c')"); err != nil { tb.Fatal(err) } if _, err := db.Exec("CREATE INDEX idx_t_name ON t(name)"); err != nil { tb.Fatal(err) } return dbPath } func TestCheckIntegrity_Quick_ValidDB(t *testing.T) { dbPath := mustCreateValidSQLiteDB(t) if err := checkIntegrity(context.Background(), dbPath, IntegrityCheckQuick); err != nil { t.Fatalf("expected no error, got: %v", err) } } func TestCheckIntegrity_Full_ValidDB(t *testing.T) { dbPath := mustCreateValidSQLiteDB(t) if err := checkIntegrity(context.Background(), dbPath, IntegrityCheckFull); err != nil { t.Fatalf("expected no error, got: %v", err) } } func TestCheckIntegrity_None_Skips(t *testing.T) { if err := checkIntegrity(context.Background(), "/nonexistent/path.db", IntegrityCheckNone); err != nil { t.Fatalf("expected nil for IntegrityCheckNone, got: %v", err) } } func TestCheckIntegrity_CorruptDB(t *testing.T) { dbPath := mustCreateValidSQLiteDB(t) // Remove any WAL/SHM files so we have a clean single-file database. _ = os.Remove(dbPath + "-wal") _ = os.Remove(dbPath + "-shm") // Read the page size from the database header (bytes 16-17, big-endian). f, err := os.OpenFile(dbPath, os.O_RDWR, 0o600) if err != nil { t.Fatal(err) } // Corrupt page 2 onwards. Page 1 is the header/schema page. Corrupting // pages that contain table/index data triggers integrity check failures. // We overwrite from byte offset 4096 (start of page 2 for 4096-byte pages, // which is the default) with garbage data. info, err := f.Stat() if err != nil { _ = f.Close() t.Fatal(err) } // Overwrite everything after the first page with garbage to ensure corruption. pageSize := int64(4096) if info.Size() > pageSize { garbage := bytes.Repeat([]byte{0xDE}, int(info.Size()-pageSize)) if _, err := f.WriteAt(garbage, pageSize); err != nil { _ = f.Close() t.Fatal(err) } } _ = f.Close() err = checkIntegrity(context.Background(), dbPath, IntegrityCheckFull) if err == nil { t.Fatal("expected integrity check to fail on corrupt database") } } ================================================ FILE: replica_test.go ================================================ package litestream_test import ( "bytes" "context" "errors" "fmt" "io" "log/slog" "os" "path/filepath" "strings" "testing" "time" "github.com/pierrec/lz4/v4" "github.com/superfly/ltx" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/internal/testingutil" "github.com/benbjohnson/litestream/mock" ) func TestReplica_Sync(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) t.Log("initial sync") // Issue initial database sync. if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } // Fetch current database position. dpos, err := db.Pos() if err != nil { t.Fatal(err) } t.Logf("position after sync: %s", dpos.String()) c := file.NewReplicaClient(t.TempDir()) r := litestream.NewReplicaWithClient(db, c) if err := r.Sync(context.Background()); err != nil { t.Fatal(err) } t.Logf("second sync") // Verify we synced checkpoint page to WAL. rd, err := c.OpenLTXFile(context.Background(), 0, dpos.TXID, dpos.TXID, 0, 0) if err != nil { t.Fatal(err) } defer func() { _ = rd.Close() }() dec := ltx.NewDecoder(rd) if err := dec.Verify(); err != nil { t.Fatal(err) } else if err := rd.Close(); err != nil { t.Fatal(err) } else if got, want := int(dec.Header().PageSize), db.PageSize(); got != want { t.Fatalf("page size: %d, want %d", got, want) } // Reset WAL so the next write will only write out the segment we are checking. if err := db.Checkpoint(context.Background(), litestream.CheckpointModeTruncate); err != nil { t.Fatal(err) } // Execute a query to write something into the truncated WAL. if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE foo (bar TEXT);`); err != nil { t.Fatal(err) } // Sync database to catch up the shadow WAL. if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } // Save position after sync, it should be after our write. _, err = db.Pos() if err != nil { t.Fatal(err) } // Sync WAL segment out to replica. if err := r.Sync(context.Background()); err != nil { t.Fatal(err) } // TODO(ltx): Restore snapshot and verify } // TestReplica_RestoreAndReplicateAfterDataLoss tests the scenario described in issue #781 // where a database is restored to an earlier state (with lower TXID) but the replica has // a higher TXID, causing new writes to not be replicated. // // The fix detects this condition in DB.init() by comparing database position vs replica // position. When database is behind, it fetches the latest L0 file from the replica and // triggers a snapshot on the next sync. // // This test follows the reproduction steps from issue #781: // 1. Create DB and replicate data // 2. Restore from backup (simulating hard recovery) // 3. Insert new data and replicate // 4. Restore again and verify new data exists func TestReplica_RestoreAndReplicateAfterDataLoss(t *testing.T) { ctx := context.Background() // Create a temporary directory for replica storage replicaDir := t.TempDir() replicaClient := file.NewReplicaClient(replicaDir) // Create database with initial data dbDir := t.TempDir() dbPath := dbDir + "/db.sqlite" // Step 1: Create initial data and replicate sqldb := testingutil.MustOpenSQLDB(t, dbPath) if _, err := sqldb.ExecContext(ctx, `CREATE TABLE test(col1 INTEGER);`); err != nil { t.Fatal(err) } if _, err := sqldb.ExecContext(ctx, `INSERT INTO test VALUES (1);`); err != nil { t.Fatal(err) } if err := sqldb.Close(); err != nil { t.Fatal(err) } // Start litestream replication db1 := testingutil.NewDB(t, dbPath) db1.MonitorInterval = 0 db1.Replica = litestream.NewReplicaWithClient(db1, replicaClient) db1.Replica.MonitorEnabled = false if err := db1.Open(); err != nil { t.Fatal(err) } if err := db1.Sync(ctx); err != nil { t.Fatal(err) } if err := db1.Replica.Sync(ctx); err != nil { t.Fatal(err) } if err := db1.Close(ctx); err != nil { t.Fatal(err) } t.Log("Step 1 complete: Initial data replicated") // Step 2: Simulate hard recovery - remove database and .litestream directory, then restore if err := os.Remove(dbPath); err != nil { t.Fatal(err) } if err := os.Remove(dbPath + "-wal"); os.IsExist(err) { t.Fatal(err) } if err := os.Remove(dbPath + "-shm"); os.IsExist(err) { t.Fatal(err) } metaPath := db1.MetaPath() if err := os.RemoveAll(metaPath); err != nil { t.Fatal(err) } // Restore from backup restoreOpt := litestream.RestoreOptions{ OutputPath: dbPath, } if err := db1.Replica.Restore(ctx, restoreOpt); err != nil { t.Fatal(err) } t.Log("Step 2 complete: Database restored from backup") // Step 3: Start replication and insert new data db2 := testingutil.NewDB(t, dbPath) db2.MonitorInterval = 0 db2.Replica = litestream.NewReplicaWithClient(db2, replicaClient) db2.Replica.MonitorEnabled = false if err := db2.Open(); err != nil { t.Fatal(err) } sqldb2 := testingutil.MustOpenSQLDB(t, dbPath) if _, err := sqldb2.ExecContext(ctx, `INSERT INTO test VALUES (2);`); err != nil { t.Fatal(err) } if err := sqldb2.Close(); err != nil { t.Fatal(err) } t.Log("Step 3: Inserted new data (value=2) after restore") // Sync new data if err := db2.Sync(ctx); err != nil { t.Fatal(err) } if err := db2.Replica.Sync(ctx); err != nil { t.Fatal(err) } if err := db2.Close(ctx); err != nil { t.Fatal(err) } t.Log("Step 3 complete: New data synced") // Step 4: Simulate second hard recovery and restore again if err := os.Remove(dbPath); err != nil { t.Fatal(err) } if err := os.Remove(dbPath + "-wal"); os.IsExist(err) { t.Fatal(err) } if err := os.Remove(dbPath + "-shm"); os.IsExist(err) { t.Fatal(err) } if err := os.RemoveAll(db2.MetaPath()); err != nil { t.Fatal(err) } // Restore to a path with non-existent parent directory to verify it gets created restoredPath := dbDir + "/restored/db.sqlite" restoreOpt.OutputPath = restoredPath if err := db2.Replica.Restore(ctx, restoreOpt); err != nil { t.Fatal(err) } t.Log("Step 4 complete: Second restore from backup to path with non-existent parent") // Step 5: Verify the new data (value=2) exists in restored database sqldb3 := testingutil.MustOpenSQLDB(t, restoredPath) defer sqldb3.Close() var count int if err := sqldb3.QueryRowContext(ctx, `SELECT COUNT(*) FROM test;`).Scan(&count); err != nil { t.Fatal(err) } // Should have 2 rows (1 and 2) if count != 2 { t.Fatalf("expected 2 rows in restored database, got %d", count) } // Verify the new row (value=2) exists var exists bool if err := sqldb3.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM test WHERE col1 = 2);`).Scan(&exists); err != nil { t.Fatal(err) } if !exists { t.Fatal("new data (value=2) was not replicated - this is the bug in issue #781") } t.Log("Test passed: New data after restore was successfully replicated") } func TestReplica_CalcRestorePlan(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) t.Run("SnapshotOnly", func(t *testing.T) { var c mock.ReplicaClient r := litestream.NewReplicaWithClient(db, &c) c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { if level == litestream.SnapshotLevel { return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{{ Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 10, Size: 1024, CreatedAt: time.Now(), }}), nil } return ltx.NewFileInfoSliceIterator(nil), nil } plan, err := litestream.CalcRestorePlan(context.Background(), r.Client, 10, time.Time{}, r.Logger()) if err != nil { t.Fatalf("unexpected error: %v", err) } if got, want := len(plan), 1; got != want { t.Fatalf("n=%d, want %d", got, want) } if plan[0].MaxTXID != 10 { t.Fatalf("expected MaxTXID 10, got %d", plan[0].MaxTXID) } }) t.Run("SnapshotAndIncremental", func(t *testing.T) { var c mock.ReplicaClient r := litestream.NewReplicaWithClient(db, &c) c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { switch level { case litestream.SnapshotLevel: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 5}, {Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 15}, }), nil case 1: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: 1, MinTXID: 6, MaxTXID: 7}, {Level: 1, MinTXID: 8, MaxTXID: 9}, {Level: 1, MinTXID: 10, MaxTXID: 12}, }), nil case 0: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: 0, MinTXID: 7, MaxTXID: 7}, {Level: 0, MinTXID: 8, MaxTXID: 8}, {Level: 0, MinTXID: 9, MaxTXID: 9}, {Level: 0, MinTXID: 10, MaxTXID: 10}, {Level: 0, MinTXID: 11, MaxTXID: 11}, }), nil default: return ltx.NewFileInfoSliceIterator(nil), nil } } plan, err := litestream.CalcRestorePlan(context.Background(), r.Client, 10, time.Time{}, r.Logger()) if err != nil { t.Fatalf("unexpected error: %v", err) } if got, want := len(plan), 4; got != want { t.Fatalf("n=%v, want %v", got, want) } if got, want := *plan[0], (ltx.FileInfo{Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 5}); got != want { t.Fatalf("plan[0]=%#v, want %#v", got, want) } if got, want := *plan[1], (ltx.FileInfo{Level: 1, MinTXID: 6, MaxTXID: 7}); got != want { t.Fatalf("plan[1]=%#v, want %#v", got, want) } if got, want := *plan[2], (ltx.FileInfo{Level: 1, MinTXID: 8, MaxTXID: 9}); got != want { t.Fatalf("plan[2]=%#v, want %#v", got, want) } if got, want := *plan[3], (ltx.FileInfo{Level: 0, MinTXID: 10, MaxTXID: 10}); got != want { t.Fatalf("plan[2]=%#v, want %#v", got, want) } }) t.Run("SelectLongestAcrossLevels", func(t *testing.T) { var c mock.ReplicaClient r := litestream.NewReplicaWithClient(db, &c) c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { switch level { case litestream.SnapshotLevel: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 5}, }), nil case 2: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: 2, MinTXID: 6, MaxTXID: 12}, }), nil case 0: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: 0, MinTXID: 6, MaxTXID: 20}, }), nil default: return ltx.NewFileInfoSliceIterator(nil), nil } } plan, err := litestream.CalcRestorePlan(context.Background(), r.Client, 20, time.Time{}, r.Logger()) if err != nil { t.Fatalf("unexpected error: %v", err) } if got, want := len(plan), 2; got != want { t.Fatalf("n=%v, want %v", got, want) } if got, want := *plan[0], (ltx.FileInfo{Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 5}); got != want { t.Fatalf("plan[0]=%#v, want %#v", got, want) } if got, want := *plan[1], (ltx.FileInfo{Level: 0, MinTXID: 6, MaxTXID: 20}); got != want { t.Fatalf("plan[1]=%#v, want %#v", got, want) } }) t.Run("GapInLevelResolvedByLowerLevel", func(t *testing.T) { var c mock.ReplicaClient r := litestream.NewReplicaWithClient(db, &c) c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { switch level { case litestream.SnapshotLevel: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 5}, }), nil case 1: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: 1, MinTXID: 6, MaxTXID: 7}, {Level: 1, MinTXID: 9, MaxTXID: 10}, }), nil case 0: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: 0, MinTXID: 8, MaxTXID: 8}, {Level: 0, MinTXID: 9, MaxTXID: 9}, {Level: 0, MinTXID: 10, MaxTXID: 10}, }), nil default: return ltx.NewFileInfoSliceIterator(nil), nil } } plan, err := litestream.CalcRestorePlan(context.Background(), r.Client, 10, time.Time{}, r.Logger()) if err != nil { t.Fatalf("unexpected error: %v", err) } if got, want := len(plan), 4; got != want { t.Fatalf("n=%v, want %v", got, want) } if got, want := *plan[0], (ltx.FileInfo{Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 5}); got != want { t.Fatalf("plan[0]=%#v, want %#v", got, want) } if got, want := *plan[1], (ltx.FileInfo{Level: 1, MinTXID: 6, MaxTXID: 7}); got != want { t.Fatalf("plan[1]=%#v, want %#v", got, want) } if got, want := *plan[2], (ltx.FileInfo{Level: 0, MinTXID: 8, MaxTXID: 8}); got != want { t.Fatalf("plan[2]=%#v, want %#v", got, want) } if got, want := *plan[3], (ltx.FileInfo{Level: 1, MinTXID: 9, MaxTXID: 10}); got != want { t.Fatalf("plan[3]=%#v, want %#v", got, want) } }) t.Run("SkipsDuplicateRangesAcrossLevels", func(t *testing.T) { var c mock.ReplicaClient r := litestream.NewReplicaWithClient(db, &c) c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { switch level { case litestream.SnapshotLevel: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 1}, }), nil case 1: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: 1, MinTXID: 1, MaxTXID: 1}, }), nil case 0: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: 0, MinTXID: 1, MaxTXID: 1}, }), nil default: return ltx.NewFileInfoSliceIterator(nil), nil } } plan, err := litestream.CalcRestorePlan(context.Background(), r.Client, 1, time.Time{}, r.Logger()) if err != nil { t.Fatalf("unexpected error: %v", err) } if got, want := len(plan), 1; got != want { t.Fatalf("n=%v, want %v", got, want) } if got, want := *plan[0], (ltx.FileInfo{Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 1}); got != want { t.Fatalf("plan[0]=%#v, want %#v", got, want) } }) // Issue #847: When a level has overlapping files where a larger compacted file // covers a smaller file's entire range, the smaller file should be skipped // rather than causing a non-contiguous error. t.Run("OverlappingFilesWithinLevel", func(t *testing.T) { var c mock.ReplicaClient r := litestream.NewReplicaWithClient(db, &c) c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { switch level { case litestream.SnapshotLevel: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 5}, }), nil case 2: // Simulates issue #847: Files are sorted by MinTXID (filename order). // File 1 is a large compacted file covering 1-100. // File 2 is a smaller file covering 50-60, which is fully within file 1's range. // Before the fix, file 2 would pass the filter (MaxTXID 60 > infos.MaxTXID() 5) // but then fail the contiguity check after file 1 is added. return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: 2, MinTXID: 1, MaxTXID: 100}, {Level: 2, MinTXID: 50, MaxTXID: 60}, }), nil default: return ltx.NewFileInfoSliceIterator(nil), nil } } plan, err := litestream.CalcRestorePlan(context.Background(), r.Client, 100, time.Time{}, r.Logger()) if err != nil { t.Fatalf("unexpected error: %v", err) } // Plan should contain only snapshot and the large file, not the smaller overlapping file if got, want := len(plan), 2; got != want { t.Fatalf("n=%d, want %d", got, want) } if got, want := *plan[0], (ltx.FileInfo{Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 5}); got != want { t.Fatalf("plan[0]=%#v, want %#v", got, want) } if got, want := *plan[1], (ltx.FileInfo{Level: 2, MinTXID: 1, MaxTXID: 100}); got != want { t.Fatalf("plan[1]=%#v, want %#v", got, want) } }) t.Run("ErrTxNotAvailable", func(t *testing.T) { var c mock.ReplicaClient r := litestream.NewReplicaWithClient(db, &c) c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { switch level { case litestream.SnapshotLevel: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 10}, }), nil default: return ltx.NewFileInfoSliceIterator(nil), nil } } _, err := litestream.CalcRestorePlan(context.Background(), r.Client, 5, time.Time{}, r.Logger()) if !errors.Is(err, litestream.ErrTxNotAvailable) { t.Fatalf("expected ErrTxNotAvailable, got %v", err) } }) t.Run("ErrNoFiles", func(t *testing.T) { var c mock.ReplicaClient c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { return ltx.NewFileInfoSliceIterator(nil), nil } r := litestream.NewReplicaWithClient(db, &c) _, err := litestream.CalcRestorePlan(context.Background(), r.Client, 5, time.Time{}, r.Logger()) if !errors.Is(err, litestream.ErrTxNotAvailable) { t.Fatalf("expected ErrTxNotAvailable, got %v", err) } }) } func TestReplica_TimeBounds(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) t.Run("Level0Only", func(t *testing.T) { now := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) var c mock.ReplicaClient r := litestream.NewReplicaWithClient(db, &c) c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { if level == 0 { return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: 0, MinTXID: 1, MaxTXID: 1, CreatedAt: now}, {Level: 0, MinTXID: 2, MaxTXID: 2, CreatedAt: now.Add(time.Hour)}, {Level: 0, MinTXID: 3, MaxTXID: 3, CreatedAt: now.Add(2 * time.Hour)}, }), nil } return ltx.NewFileInfoSliceIterator(nil), nil } createdAt, updatedAt, err := r.TimeBounds(context.Background()) if err != nil { t.Fatalf("unexpected error: %v", err) } if !createdAt.Equal(now) { t.Fatalf("createdAt=%v, want %v", createdAt, now) } if want := now.Add(2 * time.Hour); !updatedAt.Equal(want) { t.Fatalf("updatedAt=%v, want %v", updatedAt, want) } }) t.Run("SnapshotOnly", func(t *testing.T) { now := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) var c mock.ReplicaClient r := litestream.NewReplicaWithClient(db, &c) c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { if level == litestream.SnapshotLevel { return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 10, CreatedAt: now}, }), nil } return ltx.NewFileInfoSliceIterator(nil), nil } createdAt, updatedAt, err := r.TimeBounds(context.Background()) if err != nil { t.Fatalf("unexpected error: %v", err) } if !createdAt.Equal(now) { t.Fatalf("createdAt=%v, want %v", createdAt, now) } if !updatedAt.Equal(now) { t.Fatalf("updatedAt=%v, want %v", updatedAt, now) } }) t.Run("SnapshotAndLevel0", func(t *testing.T) { snapshotTime := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) l0Time := time.Date(2024, 1, 2, 0, 0, 0, 0, time.UTC) var c mock.ReplicaClient r := litestream.NewReplicaWithClient(db, &c) c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { switch level { case litestream.SnapshotLevel: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 10, CreatedAt: snapshotTime}, }), nil case 0: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: 0, MinTXID: 11, MaxTXID: 11, CreatedAt: l0Time}, }), nil default: return ltx.NewFileInfoSliceIterator(nil), nil } } createdAt, updatedAt, err := r.TimeBounds(context.Background()) if err != nil { t.Fatalf("unexpected error: %v", err) } if !createdAt.Equal(snapshotTime) { t.Fatalf("createdAt=%v, want %v", createdAt, snapshotTime) } if !updatedAt.Equal(l0Time) { t.Fatalf("updatedAt=%v, want %v", updatedAt, l0Time) } }) t.Run("MultipleCompactionLevels", func(t *testing.T) { snapshotTime := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) l2Time := time.Date(2024, 1, 2, 0, 0, 0, 0, time.UTC) l0Time := time.Date(2024, 1, 3, 0, 0, 0, 0, time.UTC) var c mock.ReplicaClient r := litestream.NewReplicaWithClient(db, &c) c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { switch level { case litestream.SnapshotLevel: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 5, CreatedAt: snapshotTime}, }), nil case 2: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: 2, MinTXID: 6, MaxTXID: 8, CreatedAt: l2Time}, }), nil case 0: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: 0, MinTXID: 9, MaxTXID: 9, CreatedAt: l0Time}, }), nil default: return ltx.NewFileInfoSliceIterator(nil), nil } } createdAt, updatedAt, err := r.TimeBounds(context.Background()) if err != nil { t.Fatalf("unexpected error: %v", err) } if !createdAt.Equal(snapshotTime) { t.Fatalf("createdAt=%v, want %v", createdAt, snapshotTime) } if !updatedAt.Equal(l0Time) { t.Fatalf("updatedAt=%v, want %v", updatedAt, l0Time) } }) t.Run("NoFiles", func(t *testing.T) { var c mock.ReplicaClient r := litestream.NewReplicaWithClient(db, &c) c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { return ltx.NewFileInfoSliceIterator(nil), nil } createdAt, updatedAt, err := r.TimeBounds(context.Background()) if err != nil { t.Fatalf("unexpected error: %v", err) } if !createdAt.IsZero() { t.Fatalf("createdAt=%v, want zero", createdAt) } if !updatedAt.IsZero() { t.Fatalf("updatedAt=%v, want zero", updatedAt) } }) t.Run("ErrorOnLevel", func(t *testing.T) { var c mock.ReplicaClient r := litestream.NewReplicaWithClient(db, &c) errTest := errors.New("test error") c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { if level == 3 { return nil, errTest } return ltx.NewFileInfoSliceIterator(nil), nil } _, _, err := r.TimeBounds(context.Background()) if !errors.Is(err, errTest) { t.Fatalf("expected test error, got %v", err) } }) } func TestReplica_CalcRestoreTarget(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) t.Run("TimestampInSnapshotRange", func(t *testing.T) { snapshotTime := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) l0Time := time.Date(2024, 1, 10, 0, 0, 0, 0, time.UTC) var c mock.ReplicaClient r := litestream.NewReplicaWithClient(db, &c) c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { switch level { case litestream.SnapshotLevel: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 10, CreatedAt: snapshotTime}, }), nil case 0: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: 0, MinTXID: 11, MaxTXID: 11, CreatedAt: l0Time}, }), nil default: return ltx.NewFileInfoSliceIterator(nil), nil } } ts := time.Date(2024, 1, 5, 0, 0, 0, 0, time.UTC) updatedAt, err := r.CalcRestoreTarget(context.Background(), litestream.RestoreOptions{Timestamp: ts}) if err != nil { t.Fatalf("unexpected error: %v", err) } if !updatedAt.Equal(l0Time) { t.Fatalf("updatedAt=%v, want %v", updatedAt, l0Time) } }) t.Run("TimestampBeforeAllFiles", func(t *testing.T) { snapshotTime := time.Date(2024, 1, 5, 0, 0, 0, 0, time.UTC) var c mock.ReplicaClient r := litestream.NewReplicaWithClient(db, &c) c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { switch level { case litestream.SnapshotLevel: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 10, CreatedAt: snapshotTime}, }), nil default: return ltx.NewFileInfoSliceIterator(nil), nil } } ts := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) _, err := r.CalcRestoreTarget(context.Background(), litestream.RestoreOptions{Timestamp: ts}) if err == nil || err.Error() != "timestamp does not exist" { t.Fatalf("expected 'timestamp does not exist', got %v", err) } }) t.Run("TimestampAfterAllFiles", func(t *testing.T) { snapshotTime := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) var c mock.ReplicaClient r := litestream.NewReplicaWithClient(db, &c) c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { switch level { case litestream.SnapshotLevel: return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 10, CreatedAt: snapshotTime}, }), nil default: return ltx.NewFileInfoSliceIterator(nil), nil } } ts := time.Date(2024, 12, 31, 0, 0, 0, 0, time.UTC) _, err := r.CalcRestoreTarget(context.Background(), litestream.RestoreOptions{Timestamp: ts}) if err == nil || err.Error() != "timestamp does not exist" { t.Fatalf("expected 'timestamp does not exist', got %v", err) } }) t.Run("NoTimestamp", func(t *testing.T) { now := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) var c mock.ReplicaClient r := litestream.NewReplicaWithClient(db, &c) c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { if level == 0 { return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{ {Level: 0, MinTXID: 1, MaxTXID: 1, CreatedAt: now}, {Level: 0, MinTXID: 2, MaxTXID: 2, CreatedAt: now.Add(time.Hour)}, }), nil } return ltx.NewFileInfoSliceIterator(nil), nil } updatedAt, err := r.CalcRestoreTarget(context.Background(), litestream.RestoreOptions{}) if err != nil { t.Fatalf("unexpected error: %v", err) } if want := now.Add(time.Hour); !updatedAt.Equal(want) { t.Fatalf("updatedAt=%v, want %v", updatedAt, want) } }) } func TestReplica_Restore_InvalidFileSize(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) t.Run("EmptyFile", func(t *testing.T) { var c mock.ReplicaClient c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { if level == litestream.SnapshotLevel { return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{{ Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 10, Size: 0, // Empty file - this should cause an error CreatedAt: time.Now(), }}), nil } return ltx.NewFileInfoSliceIterator(nil), nil } r := litestream.NewReplicaWithClient(db, &c) outputPath := t.TempDir() + "/restored.db" err := r.Restore(context.Background(), litestream.RestoreOptions{ OutputPath: outputPath, }) if err == nil { t.Fatal("expected error for empty file, got nil") } if !strings.Contains(err.Error(), "invalid ltx file") { t.Fatalf("expected 'invalid ltx file' error, got: %v", err) } }) t.Run("TruncatedFile", func(t *testing.T) { var c mock.ReplicaClient c.LTXFilesFunc = func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { if level == litestream.SnapshotLevel { return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{{ Level: litestream.SnapshotLevel, MinTXID: 1, MaxTXID: 10, Size: 50, // Less than ltx.HeaderSize (100) - should cause an error CreatedAt: time.Now(), }}), nil } return ltx.NewFileInfoSliceIterator(nil), nil } r := litestream.NewReplicaWithClient(db, &c) outputPath := t.TempDir() + "/restored.db" err := r.Restore(context.Background(), litestream.RestoreOptions{ OutputPath: outputPath, }) if err == nil { t.Fatal("expected error for truncated file, got nil") } if !strings.Contains(err.Error(), "invalid ltx file") { t.Fatalf("expected 'invalid ltx file' error, got: %v", err) } }) } func TestReplica_ContextCancellationNoLogs(t *testing.T) { // This test verifies that context cancellation errors are not logged during shutdown. // The fix for issue #235 ensures that context.Canceled and context.DeadlineExceeded // errors are filtered out in monitor functions to avoid spurious log messages. db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Create a buffer to capture log output var logBuffer bytes.Buffer // Create a custom logger that writes to our buffer db.Logger = slog.New(slog.NewTextHandler(&logBuffer, &slog.HandlerOptions{ Level: slog.LevelDebug, })) // First, let's trigger a normal sync to ensure the DB is initialized if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } // Create a replica with a mock client that simulates context cancellation during Sync syncCount := 0 mockClient := &mock.ReplicaClient{ LTXFilesFunc: func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { syncCount++ // First few calls succeed, then return context.Canceled if syncCount <= 2 { // Return an empty iterator return ltx.NewFileInfoSliceIterator(nil), nil } // After initial syncs, return context.Canceled to simulate shutdown return nil, context.Canceled }, WriteLTXFileFunc: func(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { // Always succeed for writes to allow normal operation return <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, CreatedAt: time.Now(), }, nil }, } r := litestream.NewReplicaWithClient(db, mockClient) r.SyncInterval = 50 * time.Millisecond // Short interval for testing // Start the replica monitoring in a goroutine ctx, cancel := context.WithCancel(context.Background()) if err := r.Start(ctx); err != nil { t.Fatalf("failed to start replica: %v", err) } // Give the monitor time to run several sync cycles // This ensures we get both successful syncs and context cancellation errors time.Sleep(200 * time.Millisecond) // Cancel the context to trigger shutdown cancel() // Stop the replica and wait for it to finish if err := r.Stop(true); err != nil { t.Fatalf("failed to stop replica: %v", err) } // Check the logs logs := logBuffer.String() // We should have some debug logs from successful operations if !strings.Contains(logs, "replica sync") { t.Errorf("expected 'replica sync' in logs but didn't find it; logs:\n%s", logs) } // But we should NOT have "monitor error" with "context canceled" if strings.Contains(logs, "monitor error") && strings.Contains(logs, "context canceled") { t.Errorf("found 'monitor error' with 'context canceled' in logs when it should be filtered:\n%s", logs) } // The test passes if context.Canceled errors were properly filtered } func TestReplica_ValidateLevel(t *testing.T) { t.Run("ValidContiguousFiles", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) replica := litestream.NewReplicaWithClient(nil, client) // Create contiguous files: 1-2, 3-5, 6-10 createTestLTXFile(t, client, 1, 1, 2) createTestLTXFile(t, client, 1, 3, 5) createTestLTXFile(t, client, 1, 6, 10) errs, err := replica.ValidateLevel(context.Background(), 1) if err != nil { t.Fatal(err) } if len(errs) != 0 { t.Errorf("expected no errors for contiguous files, got %d: %v", len(errs), errs) } }) t.Run("EmptyLevel", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) replica := litestream.NewReplicaWithClient(nil, client) errs, err := replica.ValidateLevel(context.Background(), 1) if err != nil { t.Fatal(err) } if len(errs) != 0 { t.Errorf("expected no errors for empty level, got %d", len(errs)) } }) t.Run("SingleFile", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) replica := litestream.NewReplicaWithClient(nil, client) createTestLTXFile(t, client, 1, 1, 5) errs, err := replica.ValidateLevel(context.Background(), 1) if err != nil { t.Fatal(err) } if len(errs) != 0 { t.Errorf("expected no errors for single file, got %d", len(errs)) } }) t.Run("GapDetected", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) replica := litestream.NewReplicaWithClient(nil, client) // Create files with a gap (missing TXID 3-4) createTestLTXFile(t, client, 1, 1, 2) createTestLTXFile(t, client, 1, 5, 7) errs, err := replica.ValidateLevel(context.Background(), 1) if err != nil { t.Fatal(err) } if len(errs) != 1 { t.Fatalf("expected 1 error, got %d", len(errs)) } if errs[0].Type != "gap" { t.Errorf("expected gap error, got %q", errs[0].Type) } if errs[0].Level != 1 { t.Errorf("expected level 1, got %d", errs[0].Level) } }) t.Run("OverlapDetected", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) replica := litestream.NewReplicaWithClient(nil, client) // Create overlapping files createTestLTXFile(t, client, 1, 1, 5) createTestLTXFile(t, client, 1, 3, 7) errs, err := replica.ValidateLevel(context.Background(), 1) if err != nil { t.Fatal(err) } if len(errs) != 1 { t.Fatalf("expected 1 error, got %d", len(errs)) } if errs[0].Type != "overlap" { t.Errorf("expected overlap error, got %q", errs[0].Type) } }) t.Run("MultipleErrors", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) replica := litestream.NewReplicaWithClient(nil, client) // Create files with multiple issues: gap then overlap createTestLTXFile(t, client, 1, 1, 2) createTestLTXFile(t, client, 1, 5, 10) // gap at 3-4 createTestLTXFile(t, client, 1, 8, 12) // overlap at 8-10 errs, err := replica.ValidateLevel(context.Background(), 1) if err != nil { t.Fatal(err) } if len(errs) != 2 { t.Fatalf("expected 2 errors, got %d", len(errs)) } if errs[0].Type != "gap" { t.Errorf("expected first error to be gap, got %q", errs[0].Type) } if errs[1].Type != "overlap" { t.Errorf("expected second error to be overlap, got %q", errs[1].Type) } }) } func TestReplica_RestoreV3(t *testing.T) { t.Run("SnapshotOnly", func(t *testing.T) { ctx := context.Background() tmpDir := t.TempDir() replicaDir := t.TempDir() // Create a v0.3.x backup structure with a snapshot gen := "0123456789abcdef" createV3Backup(t, replicaDir, gen, []v3SnapshotData{ {index: 0, data: createTestSQLiteDB(t)}, }, nil) // Create replica client and replica c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(nil, c) // Restore outputPath := tmpDir + "/restored.db" err := r.RestoreV3(ctx, litestream.RestoreOptions{ OutputPath: outputPath, }) if err != nil { t.Fatalf("RestoreV3 failed: %v", err) } // Verify restored database verifyRestoredDB(t, outputPath) }) t.Run("SnapshotWithWAL", func(t *testing.T) { ctx := context.Background() tmpDir := t.TempDir() replicaDir := t.TempDir() // Create a v0.3.x backup with snapshot and WAL segments gen := "0123456789abcdef" dbData := createTestSQLiteDB(t) walData := createTestWALData(t, dbData) createV3Backup(t, replicaDir, gen, []v3SnapshotData{ {index: 0, data: dbData}, }, []v3WALSegmentData{ {index: 0, offset: 0, data: walData}, }) // Create replica and restore c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(nil, c) outputPath := tmpDir + "/restored.db" err := r.RestoreV3(ctx, litestream.RestoreOptions{ OutputPath: outputPath, }) if err != nil { t.Fatalf("RestoreV3 failed: %v", err) } // Verify restored database verifyRestoredDB(t, outputPath) }) t.Run("TimestampRestore", func(t *testing.T) { ctx := context.Background() tmpDir := t.TempDir() replicaDir := t.TempDir() // Create a v0.3.x backup with multiple snapshots at different times gen := "0123456789abcdef" snapshotsDir := filepath.Join(replicaDir, "generations", gen, "snapshots") if err := os.MkdirAll(snapshotsDir, 0755); err != nil { t.Fatal(err) } // Create first snapshot (older) dbData1 := createTestSQLiteDB(t) writeV3Snapshot(t, snapshotsDir, 0, dbData1) // Set older mod time oldTime := time.Now().Add(-2 * time.Hour) if err := os.Chtimes(filepath.Join(snapshotsDir, "00000000.snapshot.lz4"), oldTime, oldTime); err != nil { t.Fatal(err) } // Create second snapshot (newer) - sleep briefly to ensure different times time.Sleep(10 * time.Millisecond) dbData2 := createTestSQLiteDB(t) writeV3Snapshot(t, snapshotsDir, 1, dbData2) // Set newer mod time newTime := time.Now().Add(-1 * time.Hour) if err := os.Chtimes(filepath.Join(snapshotsDir, "00000001.snapshot.lz4"), newTime, newTime); err != nil { t.Fatal(err) } // Create replica and restore to a timestamp between the two snapshots c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(nil, c) outputPath := tmpDir + "/restored.db" restoreTime := time.Now().Add(-90 * time.Minute) // Between old and new err := r.RestoreV3(ctx, litestream.RestoreOptions{ OutputPath: outputPath, Timestamp: restoreTime, }) if err != nil { t.Fatalf("RestoreV3 failed: %v", err) } // Verify restored database (should be the older one) verifyRestoredDB(t, outputPath) }) t.Run("NoSnapshots", func(t *testing.T) { ctx := context.Background() tmpDir := t.TempDir() replicaDir := t.TempDir() // Create empty generations directory gen := "0123456789abcdef" if err := os.MkdirAll(filepath.Join(replicaDir, "generations", gen), 0755); err != nil { t.Fatal(err) } c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(nil, c) outputPath := tmpDir + "/restored.db" err := r.RestoreV3(ctx, litestream.RestoreOptions{ OutputPath: outputPath, }) if !errors.Is(err, litestream.ErrNoSnapshots) { t.Fatalf("expected ErrNoSnapshots, got %v", err) } }) t.Run("NoGenerations", func(t *testing.T) { ctx := context.Background() tmpDir := t.TempDir() replicaDir := t.TempDir() // Empty replica directory c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(nil, c) outputPath := tmpDir + "/restored.db" err := r.RestoreV3(ctx, litestream.RestoreOptions{ OutputPath: outputPath, }) if !errors.Is(err, litestream.ErrNoSnapshots) { t.Fatalf("expected ErrNoSnapshots, got %v", err) } }) t.Run("OutputPathExists", func(t *testing.T) { ctx := context.Background() replicaDir := t.TempDir() // Create a v0.3.x backup gen := "0123456789abcdef" createV3Backup(t, replicaDir, gen, []v3SnapshotData{ {index: 0, data: createTestSQLiteDB(t)}, }, nil) c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(nil, c) // Create output file that already exists outputPath := t.TempDir() + "/existing.db" if err := os.WriteFile(outputPath, []byte("existing"), 0644); err != nil { t.Fatal(err) } err := r.RestoreV3(ctx, litestream.RestoreOptions{ OutputPath: outputPath, }) if err == nil || !strings.Contains(err.Error(), "already exists") { t.Fatalf("expected 'already exists' error, got %v", err) } }) t.Run("ClientDoesNotSupportV3", func(t *testing.T) { ctx := context.Background() // Use mock client that doesn't implement ReplicaClientV3 var c mock.ReplicaClient r := litestream.NewReplicaWithClient(nil, &c) err := r.RestoreV3(ctx, litestream.RestoreOptions{ OutputPath: t.TempDir() + "/restored.db", }) if err == nil || !strings.Contains(err.Error(), "does not support v0.3.x") { t.Fatalf("expected 'does not support v0.3.x' error, got %v", err) } }) t.Run("MultipleGenerations", func(t *testing.T) { ctx := context.Background() tmpDir := t.TempDir() replicaDir := t.TempDir() // Create snapshots in two different generations gen1 := "0000000000000001" gen2 := "0000000000000002" // Older snapshot in gen1 snapshotsDir1 := filepath.Join(replicaDir, "generations", gen1, "snapshots") if err := os.MkdirAll(snapshotsDir1, 0755); err != nil { t.Fatal(err) } dbData1 := createTestSQLiteDB(t) writeV3Snapshot(t, snapshotsDir1, 0, dbData1) oldTime := time.Now().Add(-2 * time.Hour) if err := os.Chtimes(filepath.Join(snapshotsDir1, "00000000.snapshot.lz4"), oldTime, oldTime); err != nil { t.Fatal(err) } // Newer snapshot in gen2 snapshotsDir2 := filepath.Join(replicaDir, "generations", gen2, "snapshots") if err := os.MkdirAll(snapshotsDir2, 0755); err != nil { t.Fatal(err) } dbData2 := createTestSQLiteDB(t) writeV3Snapshot(t, snapshotsDir2, 0, dbData2) newTime := time.Now().Add(-1 * time.Hour) if err := os.Chtimes(filepath.Join(snapshotsDir2, "00000000.snapshot.lz4"), newTime, newTime); err != nil { t.Fatal(err) } // Restore without timestamp should pick the newest c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(nil, c) outputPath := tmpDir + "/restored.db" err := r.RestoreV3(ctx, litestream.RestoreOptions{ OutputPath: outputPath, }) if err != nil { t.Fatalf("RestoreV3 failed: %v", err) } verifyRestoredDB(t, outputPath) }) } func TestReplica_Restore_BothFormats(t *testing.T) { t.Run("V3OnlyWithTimestamp", func(t *testing.T) { ctx := context.Background() tmpDir := t.TempDir() replicaDir := t.TempDir() // Create a v0.3.x backup only gen := "0123456789abcdef" snapshotsDir := filepath.Join(replicaDir, "generations", gen, "snapshots") if err := os.MkdirAll(snapshotsDir, 0755); err != nil { t.Fatal(err) } dbData := createTestSQLiteDB(t) writeV3Snapshot(t, snapshotsDir, 0, dbData) // Set snapshot time to 1 hour ago snapshotTime := time.Now().Add(-1 * time.Hour) if err := os.Chtimes(filepath.Join(snapshotsDir, "00000000.snapshot.lz4"), snapshotTime, snapshotTime); err != nil { t.Fatal(err) } // Create replica and restore with timestamp c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(nil, c) outputPath := tmpDir + "/restored.db" err := r.Restore(ctx, litestream.RestoreOptions{ OutputPath: outputPath, Timestamp: time.Now(), // Any time after snapshot }) if err != nil { t.Fatalf("Restore failed: %v", err) } // Verify restored database verifyRestoredDB(t, outputPath) }) t.Run("V3OnlyWithoutTimestamp", func(t *testing.T) { ctx := context.Background() tmpDir := t.TempDir() replicaDir := t.TempDir() // Create a v0.3.x backup only (no LTX files) gen := "0123456789abcdef" snapshotsDir := filepath.Join(replicaDir, "generations", gen, "snapshots") if err := os.MkdirAll(snapshotsDir, 0755); err != nil { t.Fatal(err) } dbData := createTestSQLiteDB(t) writeV3Snapshot(t, snapshotsDir, 0, dbData) // Create replica and restore WITHOUT timestamp - should still use v0.3.x c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(nil, c) outputPath := tmpDir + "/restored.db" err := r.Restore(ctx, litestream.RestoreOptions{ OutputPath: outputPath, // No timestamp specified }) if err != nil { t.Fatalf("Restore failed: %v", err) } // Verify restored database verifyRestoredDB(t, outputPath) }) t.Run("LTXOnlyWithTimestamp", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) replicaDir := t.TempDir() c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(db, c) // Sync to create LTX files if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if err := r.Sync(context.Background()); err != nil { t.Fatal(err) } // Create a snapshot if _, err := db.Snapshot(context.Background()); err != nil { t.Fatal(err) } if err := r.Sync(context.Background()); err != nil { t.Fatal(err) } // Wait a bit to ensure distinct timestamps time.Sleep(10 * time.Millisecond) // Restore with timestamp outputPath := t.TempDir() + "/restored.db" err := r.Restore(context.Background(), litestream.RestoreOptions{ OutputPath: outputPath, Timestamp: time.Now(), }) if err != nil { t.Fatalf("Restore failed: %v", err) } verifyRestoredDB(t, outputPath) }) t.Run("BothFormats_V3Better", func(t *testing.T) { ctx := context.Background() tmpDir := t.TempDir() replicaDir := t.TempDir() // Create v0.3.x snapshot at time T-30min (closer to restore time) gen := "0123456789abcdef" snapshotsDir := filepath.Join(replicaDir, "generations", gen, "snapshots") if err := os.MkdirAll(snapshotsDir, 0755); err != nil { t.Fatal(err) } dbData := createTestSQLiteDB(t) writeV3Snapshot(t, snapshotsDir, 0, dbData) v3Time := time.Now().Add(-30 * time.Minute) if err := os.Chtimes(filepath.Join(snapshotsDir, "00000000.snapshot.lz4"), v3Time, v3Time); err != nil { t.Fatal(err) } // Create LTX snapshot at time T-2h (older) ltxDir := filepath.Join(replicaDir, "ltx", "9") // Snapshot level if err := os.MkdirAll(ltxDir, 0755); err != nil { t.Fatal(err) } ltxData := createTestLTXSnapshot(t) ltxPath := filepath.Join(ltxDir, "0000000000000001-0000000000000001.ltx") if err := os.WriteFile(ltxPath, ltxData, 0644); err != nil { t.Fatal(err) } ltxTime := time.Now().Add(-2 * time.Hour) if err := os.Chtimes(ltxPath, ltxTime, ltxTime); err != nil { t.Fatal(err) } // Restore with timestamp - should use V3 (more recent) c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(nil, c) outputPath := tmpDir + "/restored.db" err := r.Restore(ctx, litestream.RestoreOptions{ OutputPath: outputPath, Timestamp: time.Now(), }) if err != nil { t.Fatalf("Restore failed: %v", err) } verifyRestoredDB(t, outputPath) }) t.Run("BothFormats_LTXBetter", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) replicaDir := t.TempDir() // Create v0.3.x snapshot at time T-2h (older) gen := "0123456789abcdef" snapshotsDir := filepath.Join(replicaDir, "generations", gen, "snapshots") if err := os.MkdirAll(snapshotsDir, 0755); err != nil { t.Fatal(err) } v3Data := createTestSQLiteDB(t) writeV3Snapshot(t, snapshotsDir, 0, v3Data) v3Time := time.Now().Add(-2 * time.Hour) if err := os.Chtimes(filepath.Join(snapshotsDir, "00000000.snapshot.lz4"), v3Time, v3Time); err != nil { t.Fatal(err) } // Create LTX backup (more recent) c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(db, c) if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if err := r.Sync(context.Background()); err != nil { t.Fatal(err) } // Create a snapshot if _, err := db.Snapshot(context.Background()); err != nil { t.Fatal(err) } if err := r.Sync(context.Background()); err != nil { t.Fatal(err) } // Wait a bit time.Sleep(10 * time.Millisecond) // Restore with timestamp - should use LTX (more recent) outputPath := t.TempDir() + "/restored.db" err := r.Restore(context.Background(), litestream.RestoreOptions{ OutputPath: outputPath, Timestamp: time.Now(), }) if err != nil { t.Fatalf("Restore failed: %v", err) } verifyRestoredDB(t, outputPath) }) t.Run("NoTimestamp_UsesLTX", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) replicaDir := t.TempDir() // Create v0.3.x snapshot gen := "0123456789abcdef" snapshotsDir := filepath.Join(replicaDir, "generations", gen, "snapshots") if err := os.MkdirAll(snapshotsDir, 0755); err != nil { t.Fatal(err) } v3Data := createTestSQLiteDB(t) writeV3Snapshot(t, snapshotsDir, 0, v3Data) // Create LTX backup c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(db, c) if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if err := r.Sync(context.Background()); err != nil { t.Fatal(err) } // Restore without timestamp - should use LTX (default behavior) outputPath := t.TempDir() + "/restored.db" err := r.Restore(context.Background(), litestream.RestoreOptions{ OutputPath: outputPath, }) if err != nil { t.Fatalf("Restore failed: %v", err) } verifyRestoredDB(t, outputPath) }) } // createTestLTXSnapshot creates a minimal LTX snapshot for testing. func createTestLTXSnapshot(t *testing.T) []byte { t.Helper() // Create a temporary database and generate a real LTX snapshot tmpDir := t.TempDir() dbPath := filepath.Join(tmpDir, "test.db") replicaDir := filepath.Join(tmpDir, "replica") db := testingutil.NewDB(t, dbPath) // Set up a replica client so we can create snapshots c := file.NewReplicaClient(replicaDir) db.Replica = litestream.NewReplicaWithClient(db, c) db.Replica.MonitorEnabled = false if err := db.Open(); err != nil { t.Fatal(err) } // Create some data sqldb := testingutil.MustOpenSQLDB(t, dbPath) if _, err := sqldb.Exec(`CREATE TABLE test (id INTEGER PRIMARY KEY)`); err != nil { t.Fatal(err) } testingutil.MustCloseSQLDB(t, sqldb) // Sync to create LTX file if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } if err := db.Replica.Sync(context.Background()); err != nil { t.Fatal(err) } // Create snapshot if _, err := db.Snapshot(context.Background()); err != nil { t.Fatal(err) } if err := db.Close(context.Background()); err != nil { t.Fatal(err) } // Read the snapshot file from replica directory ltxPath := filepath.Join(replicaDir, "ltx", fmt.Sprintf("%d", litestream.SnapshotLevel), "0000000000000001-0000000000000001.ltx") data, err := os.ReadFile(ltxPath) if err != nil { t.Fatal(err) } return data } // v3SnapshotData holds test data for creating v0.3.x snapshots. type v3SnapshotData struct { index int data []byte } // v3WALSegmentData holds test data for creating v0.3.x WAL segments. type v3WALSegmentData struct { index int offset int64 data []byte } // createV3Backup creates a v0.3.x backup structure for testing. func createV3Backup(t *testing.T, replicaDir, generation string, snapshots []v3SnapshotData, walSegments []v3WALSegmentData) { t.Helper() // Create snapshots directory and files if len(snapshots) > 0 { snapshotsDir := filepath.Join(replicaDir, "generations", generation, "snapshots") if err := os.MkdirAll(snapshotsDir, 0755); err != nil { t.Fatal(err) } for _, s := range snapshots { writeV3Snapshot(t, snapshotsDir, s.index, s.data) } } // Create WAL directory and files if len(walSegments) > 0 { walDir := filepath.Join(replicaDir, "generations", generation, "wal") if err := os.MkdirAll(walDir, 0755); err != nil { t.Fatal(err) } for _, w := range walSegments { writeV3WALSegment(t, walDir, w.index, w.offset, w.data) } } } // writeV3Snapshot writes an LZ4-compressed snapshot file. func writeV3Snapshot(t *testing.T, dir string, index int, data []byte) { t.Helper() var buf bytes.Buffer w := lz4.NewWriter(&buf) if _, err := w.Write(data); err != nil { t.Fatal(err) } if err := w.Close(); err != nil { t.Fatal(err) } filename := fmt.Sprintf("%08x.snapshot.lz4", index) if err := os.WriteFile(filepath.Join(dir, filename), buf.Bytes(), 0644); err != nil { t.Fatal(err) } } // writeV3WALSegment writes an LZ4-compressed WAL segment file. func writeV3WALSegment(t *testing.T, dir string, index int, offset int64, data []byte) { t.Helper() var buf bytes.Buffer w := lz4.NewWriter(&buf) if _, err := w.Write(data); err != nil { t.Fatal(err) } if err := w.Close(); err != nil { t.Fatal(err) } filename := fmt.Sprintf("%08x-%016x.wal.lz4", index, offset) if err := os.WriteFile(filepath.Join(dir, filename), buf.Bytes(), 0644); err != nil { t.Fatal(err) } } // createTestSQLiteDB creates a minimal valid SQLite database for testing. func createTestSQLiteDB(t *testing.T) []byte { t.Helper() tmpPath := t.TempDir() + "/test.db" sqldb := testingutil.MustOpenSQLDB(t, tmpPath) if _, err := sqldb.Exec(`CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)`); err != nil { t.Fatal(err) } if _, err := sqldb.Exec(`INSERT INTO test (value) VALUES ('hello')`); err != nil { t.Fatal(err) } testingutil.MustCloseSQLDB(t, sqldb) data, err := os.ReadFile(tmpPath) if err != nil { t.Fatal(err) } return data } // createTestWALData creates minimal valid WAL data for testing. // For simplicity, returns an empty WAL header (32 bytes) which is valid. func createTestWALData(t *testing.T, dbData []byte) []byte { t.Helper() // Create a minimal WAL header // WAL header is 32 bytes: // - magic number (4 bytes): 0x377f0683 (big-endian) or 0x377f0682 (little-endian) // - file format version (4 bytes): 3007000 // - page size (4 bytes) // - checkpoint sequence (4 bytes) // - salt-1 (4 bytes) // - salt-2 (4 bytes) // - checksum-1 (4 bytes) // - checksum-2 (4 bytes) // For testing, we'll create an empty WAL that doesn't need frames applied // This is sufficient for testing the restore mechanism return make([]byte, 32) // Empty WAL header placeholder } func TestWriteTXIDFile(t *testing.T) { t.Run("WritesCorrectFormat", func(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") if err := os.WriteFile(dbPath, []byte("db"), 0644); err != nil { t.Fatal(err) } if err := litestream.WriteTXIDFile(dbPath, 42); err != nil { t.Fatal(err) } data, err := os.ReadFile(dbPath + "-txid") if err != nil { t.Fatal(err) } if got, want := strings.TrimSpace(string(data)), "000000000000002a"; got != want { t.Fatalf("got %q, want %q", got, want) } }) t.Run("AtomicOverwrite", func(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") if err := litestream.WriteTXIDFile(dbPath, 10); err != nil { t.Fatal(err) } if err := litestream.WriteTXIDFile(dbPath, 20); err != nil { t.Fatal(err) } txid, err := litestream.ReadTXIDFile(dbPath) if err != nil { t.Fatal(err) } if txid != 20 { t.Fatalf("got %d, want 20", txid) } }) } func TestReadTXIDFile(t *testing.T) { t.Run("MissingFile", func(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "nonexistent.db") txid, err := litestream.ReadTXIDFile(dbPath) if err != nil { t.Fatal(err) } if txid != 0 { t.Fatalf("got %d, want 0", txid) } }) t.Run("ValidFile", func(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") if err := os.WriteFile(dbPath+"-txid", []byte("00000000000000ff\n"), 0644); err != nil { t.Fatal(err) } txid, err := litestream.ReadTXIDFile(dbPath) if err != nil { t.Fatal(err) } if txid != 255 { t.Fatalf("got %d, want 255", txid) } }) t.Run("MalformedFile", func(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") if err := os.WriteFile(dbPath+"-txid", []byte("not-a-hex-value\n"), 0644); err != nil { t.Fatal(err) } _, err := litestream.ReadTXIDFile(dbPath) if err == nil { t.Fatal("expected error for malformed file") } }) t.Run("EmptyFile", func(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") if err := os.WriteFile(dbPath+"-txid", []byte(""), 0644); err != nil { t.Fatal(err) } _, err := litestream.ReadTXIDFile(dbPath) if err == nil { t.Fatal("expected error for empty file") } }) } func TestReplica_Restore_Follow_IncompatibleFlags(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) c := file.NewReplicaClient(t.TempDir()) r := litestream.NewReplicaWithClient(db, c) t.Run("FollowWithTXID", func(t *testing.T) { err := r.Restore(context.Background(), litestream.RestoreOptions{ OutputPath: t.TempDir() + "/db", Follow: true, TXID: 1, }) if err == nil || err.Error() != "cannot use follow mode with -txid" { t.Fatalf("expected 'cannot use follow mode with -txid' error, got: %v", err) } }) t.Run("FollowWithTimestamp", func(t *testing.T) { err := r.Restore(context.Background(), litestream.RestoreOptions{ OutputPath: t.TempDir() + "/db", Follow: true, Timestamp: time.Now(), }) if err == nil || err.Error() != "cannot use follow mode with -timestamp" { t.Fatalf("expected 'cannot use follow mode with -timestamp' error, got: %v", err) } }) } func TestReplica_Restore_Follow(t *testing.T) { ctx := context.Background() // Create source database with initial data. db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) if _, err := sqldb.ExecContext(ctx, `CREATE TABLE test(id INTEGER PRIMARY KEY, value TEXT)`); err != nil { t.Fatal(err) } if _, err := sqldb.ExecContext(ctx, `INSERT INTO test VALUES (1, 'initial')`); err != nil { t.Fatal(err) } // Sync and replicate to file replica. if err := db.Sync(ctx); err != nil { t.Fatal(err) } replicaDir := t.TempDir() c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(db, c) if err := r.Sync(ctx); err != nil { t.Fatal(err) } // Create a snapshot so restore has something to work with. if _, err := db.Snapshot(ctx); err != nil { t.Fatal(err) } if err := r.Sync(ctx); err != nil { t.Fatal(err) } // Start follow mode in a goroutine. outputPath := t.TempDir() + "/follower.db" followCtx, followCancel := context.WithCancel(ctx) defer followCancel() errCh := make(chan error, 1) go func() { errCh <- r.Restore(followCtx, litestream.RestoreOptions{ OutputPath: outputPath, Follow: true, FollowInterval: 50 * time.Millisecond, }) }() // Wait for initial restore to complete (file should appear). deadline := time.Now().Add(5 * time.Second) for time.Now().Before(deadline) { if _, err := os.Stat(outputPath); err == nil { break } time.Sleep(10 * time.Millisecond) } if _, err := os.Stat(outputPath); err != nil { t.Fatalf("restored file not found after waiting: %v", err) } // Insert more data into source and replicate. if _, err := sqldb.ExecContext(ctx, `INSERT INTO test VALUES (2, 'follow-update')`); err != nil { t.Fatal(err) } if err := db.Sync(ctx); err != nil { t.Fatal(err) } if err := r.Sync(ctx); err != nil { t.Fatal(err) } // Wait for follow mode to apply the new data. deadline = time.Now().Add(5 * time.Second) var found bool for time.Now().Before(deadline) { // Open the follower database read-only and check for new data. followerDB := testingutil.MustOpenSQLDB(t, outputPath) var count int if err := followerDB.QueryRowContext(ctx, `SELECT COUNT(*) FROM test WHERE value = 'follow-update'`).Scan(&count); err == nil && count > 0 { found = true followerDB.Close() break } followerDB.Close() time.Sleep(100 * time.Millisecond) } if !found { t.Fatal("follow mode did not apply new data within timeout") } // Cancel follow and verify clean shutdown. followCancel() select { case err := <-errCh: if err != nil { t.Fatalf("follow returned error: %v", err) } case <-time.After(5 * time.Second): t.Fatal("follow did not shut down within timeout") } } func TestReplica_Restore_Follow_ContextCancellation(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Create initial data and replicate. if _, err := sqldb.ExecContext(context.Background(), `CREATE TABLE test(id INTEGER PRIMARY KEY)`); err != nil { t.Fatal(err) } if err := db.Sync(context.Background()); err != nil { t.Fatal(err) } replicaDir := t.TempDir() c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(db, c) if err := r.Sync(context.Background()); err != nil { t.Fatal(err) } if _, err := db.Snapshot(context.Background()); err != nil { t.Fatal(err) } if err := r.Sync(context.Background()); err != nil { t.Fatal(err) } outputPath := t.TempDir() + "/follower.db" followCtx, followCancel := context.WithCancel(context.Background()) errCh := make(chan error, 1) go func() { errCh <- r.Restore(followCtx, litestream.RestoreOptions{ OutputPath: outputPath, Follow: true, FollowInterval: 50 * time.Millisecond, }) }() // Wait for restore to complete. deadline := time.Now().Add(5 * time.Second) for time.Now().Before(deadline) { if _, err := os.Stat(outputPath); err == nil { break } time.Sleep(10 * time.Millisecond) } // Cancel immediately and verify clean return (nil error). followCancel() select { case err := <-errCh: if err != nil { t.Fatalf("expected nil error on context cancellation, got: %v", err) } case <-time.After(5 * time.Second): t.Fatal("follow did not shut down within timeout") } } func TestReplica_Restore_Follow_WriteTXIDFile(t *testing.T) { ctx := context.Background() db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) if _, err := sqldb.ExecContext(ctx, `CREATE TABLE test(id INTEGER PRIMARY KEY, value TEXT)`); err != nil { t.Fatal(err) } if _, err := sqldb.ExecContext(ctx, `INSERT INTO test VALUES (1, 'initial')`); err != nil { t.Fatal(err) } if err := db.Sync(ctx); err != nil { t.Fatal(err) } replicaDir := t.TempDir() c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(db, c) if err := r.Sync(ctx); err != nil { t.Fatal(err) } if _, err := db.Snapshot(ctx); err != nil { t.Fatal(err) } if err := r.Sync(ctx); err != nil { t.Fatal(err) } outputPath := t.TempDir() + "/follower.db" followCtx, followCancel := context.WithCancel(ctx) defer followCancel() errCh := make(chan error, 1) go func() { errCh <- r.Restore(followCtx, litestream.RestoreOptions{ OutputPath: outputPath, Follow: true, FollowInterval: 50 * time.Millisecond, }) }() // Wait for initial restore. deadline := time.Now().Add(5 * time.Second) for time.Now().Before(deadline) { if _, err := os.Stat(outputPath); err == nil { break } time.Sleep(10 * time.Millisecond) } // Verify -txid file was created after initial restore. txidPath := outputPath + "-txid" deadline = time.Now().Add(5 * time.Second) for time.Now().Before(deadline) { if _, err := os.Stat(txidPath); err == nil { break } time.Sleep(10 * time.Millisecond) } if _, err := os.Stat(txidPath); err != nil { t.Fatalf("txid file not created after initial restore: %v", err) } initialTXID, err := litestream.ReadTXIDFile(outputPath) if err != nil { t.Fatalf("failed to read initial txid: %v", err) } if initialTXID == 0 { t.Fatal("initial txid should be non-zero") } // Insert more data and sync. if _, err := sqldb.ExecContext(ctx, `INSERT INTO test VALUES (2, 'update')`); err != nil { t.Fatal(err) } if err := db.Sync(ctx); err != nil { t.Fatal(err) } if err := r.Sync(ctx); err != nil { t.Fatal(err) } // Wait for follow to apply and update the TXID file. deadline = time.Now().Add(5 * time.Second) var updatedTXID ltx.TXID for time.Now().Before(deadline) { txid, err := litestream.ReadTXIDFile(outputPath) if err == nil && txid > initialTXID { updatedTXID = txid break } time.Sleep(100 * time.Millisecond) } if updatedTXID <= initialTXID { t.Fatalf("txid file not updated after follow apply: initial=%d, current=%d", initialTXID, updatedTXID) } followCancel() select { case err := <-errCh: if err != nil { t.Fatalf("follow returned error: %v", err) } case <-time.After(5 * time.Second): t.Fatal("follow did not shut down within timeout") } } func TestReplica_Restore_Follow_CrashRecovery(t *testing.T) { ctx := context.Background() db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) if _, err := sqldb.ExecContext(ctx, `CREATE TABLE test(id INTEGER PRIMARY KEY, value TEXT)`); err != nil { t.Fatal(err) } if _, err := sqldb.ExecContext(ctx, `INSERT INTO test VALUES (1, 'initial')`); err != nil { t.Fatal(err) } if err := db.Sync(ctx); err != nil { t.Fatal(err) } replicaDir := t.TempDir() c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(db, c) if err := r.Sync(ctx); err != nil { t.Fatal(err) } if _, err := db.Snapshot(ctx); err != nil { t.Fatal(err) } if err := r.Sync(ctx); err != nil { t.Fatal(err) } outputPath := t.TempDir() + "/follower.db" followCtx, followCancel := context.WithCancel(ctx) errCh := make(chan error, 1) go func() { errCh <- r.Restore(followCtx, litestream.RestoreOptions{ OutputPath: outputPath, Follow: true, FollowInterval: 50 * time.Millisecond, }) }() // Wait for initial restore and -txid file. deadline := time.Now().Add(5 * time.Second) for time.Now().Before(deadline) { if _, err := os.Stat(outputPath + "-txid"); err == nil { break } time.Sleep(10 * time.Millisecond) } savedTXID, err := litestream.ReadTXIDFile(outputPath) if err != nil { t.Fatalf("failed to read txid file: %v", err) } if savedTXID == 0 { t.Fatal("saved txid should be non-zero") } // Simulate crash by cancelling follow mode. followCancel() select { case err := <-errCh: if err != nil { t.Fatalf("follow returned error: %v", err) } case <-time.After(5 * time.Second): t.Fatal("follow did not shut down within timeout") } // Verify DB and -txid file still exist. if _, err := os.Stat(outputPath); err != nil { t.Fatalf("database file missing after crash: %v", err) } if _, err := os.Stat(outputPath + "-txid"); err != nil { t.Fatalf("txid file missing after crash: %v", err) } // Add more data to source while follow was down. if _, err := sqldb.ExecContext(ctx, `INSERT INTO test VALUES (2, 'post-crash')`); err != nil { t.Fatal(err) } if err := db.Sync(ctx); err != nil { t.Fatal(err) } if err := r.Sync(ctx); err != nil { t.Fatal(err) } // Restart follow mode — should resume from saved TXID (crash recovery). followCtx2, followCancel2 := context.WithCancel(ctx) defer followCancel2() errCh2 := make(chan error, 1) go func() { errCh2 <- r.Restore(followCtx2, litestream.RestoreOptions{ OutputPath: outputPath, Follow: true, FollowInterval: 50 * time.Millisecond, }) }() // Wait for follow mode to pick up new data. deadline = time.Now().Add(5 * time.Second) var found bool for time.Now().Before(deadline) { followerDB := testingutil.MustOpenSQLDB(t, outputPath) var count int if err := followerDB.QueryRowContext(ctx, `SELECT COUNT(*) FROM test WHERE value = 'post-crash'`).Scan(&count); err == nil && count > 0 { found = true followerDB.Close() break } followerDB.Close() time.Sleep(100 * time.Millisecond) } if !found { t.Fatal("crash recovery did not apply new data within timeout") } followCancel2() select { case err := <-errCh2: if err != nil { t.Fatalf("follow returned error after recovery: %v", err) } case <-time.After(5 * time.Second): t.Fatal("follow did not shut down within timeout after recovery") } } func TestReplica_Restore_Follow_NoTXIDFile(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) c := file.NewReplicaClient(t.TempDir()) r := litestream.NewReplicaWithClient(db, c) // Create a database file but no -txid sidecar. outputPath := t.TempDir() + "/existing.db" if err := os.WriteFile(outputPath, []byte("fake-db"), 0644); err != nil { t.Fatal(err) } err := r.Restore(context.Background(), litestream.RestoreOptions{ OutputPath: outputPath, Follow: true, FollowInterval: 50 * time.Millisecond, }) if err == nil { t.Fatal("expected error when DB exists but no -txid file") } if !strings.Contains(err.Error(), "no -txid file found") { t.Fatalf("unexpected error: %v", err) } } func TestReplica_Restore_Follow_StaleTXID(t *testing.T) { ctx := context.Background() db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) if _, err := sqldb.ExecContext(ctx, `CREATE TABLE test(id INTEGER PRIMARY KEY, value TEXT)`); err != nil { t.Fatal(err) } if err := db.Sync(ctx); err != nil { t.Fatal(err) } replicaDir := t.TempDir() c := file.NewReplicaClient(replicaDir) r := litestream.NewReplicaWithClient(db, c) if err := r.Sync(ctx); err != nil { t.Fatal(err) } if _, err := db.Snapshot(ctx); err != nil { t.Fatal(err) } if err := r.Sync(ctx); err != nil { t.Fatal(err) } outputPath := filepath.Join(t.TempDir(), "follower.db") // Create a fake database and a TXID file with a low TXID (1). if err := os.WriteFile(outputPath, []byte("fake-db-content"), 0644); err != nil { t.Fatal(err) } if err := litestream.WriteTXIDFile(outputPath, 1); err != nil { t.Fatal(err) } // Simulate retention pruning: remove all level 0 files and replace the // snapshot with one whose MinTXID is far ahead of our saved TXID. level0Dir := c.LTXLevelDir(0) if entries, err := os.ReadDir(level0Dir); err == nil { for _, e := range entries { os.Remove(filepath.Join(level0Dir, e.Name())) } } snapshotDir := c.LTXLevelDir(9) if entries, err := os.ReadDir(snapshotDir); err == nil { for _, e := range entries { os.Remove(filepath.Join(snapshotDir, e.Name())) } } // Create a dummy snapshot file with MinTXID=10000 (far ahead of saved TXID=1). if err := os.MkdirAll(snapshotDir, 0755); err != nil { t.Fatal(err) } dummySnapshotPath := filepath.Join(snapshotDir, "0000000000002710-0000000000002710.ltx") if err := os.WriteFile(dummySnapshotPath, []byte("dummy"), 0644); err != nil { t.Fatal(err) } err := r.Restore(ctx, litestream.RestoreOptions{ OutputPath: outputPath, Follow: true, FollowInterval: 50 * time.Millisecond, }) if err == nil { t.Fatal("expected error for stale TXID") } if !strings.Contains(err.Error(), "replica history has been pruned") { t.Fatalf("unexpected error: %v", err) } } // verifyRestoredDB verifies that the restored database is valid. func verifyRestoredDB(t *testing.T, path string) { t.Helper() // Check file exists info, err := os.Stat(path) if err != nil { t.Fatalf("restored file not found: %v", err) } if info.Size() == 0 { t.Fatal("restored file is empty") } // Try to open with SQLite to verify it's valid sqldb := testingutil.MustOpenSQLDB(t, path) defer testingutil.MustCloseSQLDB(t, sqldb) // Run integrity check var result string if err := sqldb.QueryRow("PRAGMA integrity_check").Scan(&result); err != nil { t.Fatalf("integrity check failed: %v", err) } if result != "ok" { t.Fatalf("integrity check returned: %s", result) } } ================================================ FILE: replica_url.go ================================================ package litestream import ( "fmt" "net/url" "path" "regexp" "strings" "sync" ) // ReplicaClientFactory is a function that creates a ReplicaClient from URL components. // The userinfo parameter contains credentials from the URL (e.g., user:pass@host). type ReplicaClientFactory func(scheme, host, urlPath string, query url.Values, userinfo *url.Userinfo) (ReplicaClient, error) var ( replicaClientFactories = make(map[string]ReplicaClientFactory) replicaClientFactoriesMu sync.RWMutex ) // RegisterReplicaClientFactory registers a factory function for creating replica clients // for a given URL scheme. This is typically called from init() functions in backend packages. func RegisterReplicaClientFactory(scheme string, factory ReplicaClientFactory) { replicaClientFactoriesMu.Lock() defer replicaClientFactoriesMu.Unlock() replicaClientFactories[scheme] = factory } // NewReplicaClientFromURL creates a new ReplicaClient from a URL string. // The URL scheme determines which backend is used (s3, gs, abs, file, etc.). func NewReplicaClientFromURL(rawURL string) (ReplicaClient, error) { scheme, host, urlPath, query, userinfo, err := ParseReplicaURLWithQuery(rawURL) if err != nil { return nil, err } // Normalize webdavs to webdav factoryScheme := scheme if factoryScheme == "webdavs" { factoryScheme = "webdav" } replicaClientFactoriesMu.RLock() factory, ok := replicaClientFactories[factoryScheme] replicaClientFactoriesMu.RUnlock() if !ok { return nil, fmt.Errorf("unsupported replica URL scheme: %q", scheme) } return factory(scheme, host, urlPath, query, userinfo) } // ReplicaTypeFromURL returns the replica type from a URL string. // Returns empty string if the URL is invalid or has no scheme. func ReplicaTypeFromURL(rawURL string) string { scheme, _, _, _ := ParseReplicaURL(rawURL) if scheme == "" { return "" } if scheme == "webdavs" { return "webdav" } return scheme } // ParseReplicaURL parses a replica URL and returns the scheme, host, and path. func ParseReplicaURL(s string) (scheme, host, urlPath string, err error) { if strings.HasPrefix(strings.ToLower(s), "s3://arn:") { scheme, host, urlPath, _, err = parseS3AccessPointURL(s) return scheme, host, urlPath, err } scheme, host, urlPath, _, _, err = ParseReplicaURLWithQuery(s) return scheme, host, urlPath, err } // ParseReplicaURLWithQuery parses a replica URL and returns query parameters and userinfo. func ParseReplicaURLWithQuery(s string) (scheme, host, urlPath string, query url.Values, userinfo *url.Userinfo, err error) { // Handle S3 Access Point ARNs which can't be parsed by standard url.Parse if strings.HasPrefix(strings.ToLower(s), "s3://arn:") { scheme, host, urlPath, query, err := parseS3AccessPointURL(s) return scheme, host, urlPath, query, nil, err } u, err := url.Parse(s) if err != nil { return "", "", "", nil, nil, err } switch u.Scheme { case "file": scheme, u.Scheme = u.Scheme, "" // Remove query params from path for file URLs u.RawQuery = "" return scheme, "", path.Clean(u.String()), nil, nil, nil case "": return u.Scheme, u.Host, u.Path, nil, nil, fmt.Errorf("replica url scheme required: %s", s) default: return u.Scheme, u.Host, strings.TrimPrefix(path.Clean(u.Path), "/"), u.Query(), u.User, nil } } // parseS3AccessPointURL parses an S3 Access Point URL (s3://arn:...). func parseS3AccessPointURL(s string) (scheme, host, urlPath string, query url.Values, err error) { const prefix = "s3://" if !strings.HasPrefix(strings.ToLower(s), prefix) { return "", "", "", nil, fmt.Errorf("invalid s3 access point url: %s", s) } arnWithPath := s[len(prefix):] // Split off query string if present var queryStr string if idx := strings.IndexByte(arnWithPath, '?'); idx != -1 { queryStr = arnWithPath[idx+1:] arnWithPath = arnWithPath[:idx] } bucket, key, err := splitS3AccessPointARN(arnWithPath) if err != nil { return "", "", "", nil, err } // Parse query string if present if queryStr != "" { query, err = url.ParseQuery(queryStr) if err != nil { return "", "", "", nil, fmt.Errorf("parse query string: %w", err) } } return "s3", bucket, CleanReplicaURLPath(key), query, nil } // splitS3AccessPointARN splits an S3 Access Point ARN into bucket and key components. func splitS3AccessPointARN(s string) (bucket, key string, err error) { lower := strings.ToLower(s) const marker = ":accesspoint/" idx := strings.Index(lower, marker) if idx == -1 { return "", "", fmt.Errorf("invalid s3 access point arn: %s", s) } nameStart := idx + len(marker) if nameStart >= len(s) { return "", "", fmt.Errorf("invalid s3 access point arn: %s", s) } remainder := s[nameStart:] slashIdx := strings.IndexByte(remainder, '/') if slashIdx == -1 { return s, "", nil } bucketEnd := nameStart + slashIdx bucket = s[:bucketEnd] key = remainder[slashIdx+1:] return bucket, key, nil } // CleanReplicaURLPath cleans a URL path for use in replica storage. func CleanReplicaURLPath(p string) string { if p == "" { return "" } cleaned := path.Clean("/" + p) cleaned = strings.TrimPrefix(cleaned, "/") if cleaned == "." { return "" } return cleaned } // RegionFromS3ARN extracts the region from an S3 ARN. func RegionFromS3ARN(arn string) string { parts := strings.SplitN(arn, ":", 6) if len(parts) >= 4 { return parts[3] } return "" } // BoolQueryValue returns a boolean value from URL query parameters. // It checks multiple keys in order and returns the value and whether it was set. func BoolQueryValue(query url.Values, keys ...string) (value bool, ok bool) { if query == nil { return false, false } for _, key := range keys { if raw := query.Get(key); raw != "" { switch strings.ToLower(raw) { case "true", "1", "t", "yes": return true, true case "false", "0", "f", "no": return false, true default: return false, true } } } return false, false } // IsHetznerEndpoint returns true if the endpoint is Hetzner object storage service. func IsHetznerEndpoint(endpoint string) bool { host := extractEndpointHost(endpoint) if host == "" { return false } return strings.HasSuffix(host, ".your-objectstorage.com") } // IsTigrisEndpoint returns true if the endpoint is the Tigris object storage service. func IsTigrisEndpoint(endpoint string) bool { host := extractEndpointHost(endpoint) return host == "fly.storage.tigris.dev" || host == "t3.storage.dev" } // IsDigitalOceanEndpoint returns true if the endpoint is Digital Ocean Spaces. func IsDigitalOceanEndpoint(endpoint string) bool { host := extractEndpointHost(endpoint) if host == "" { return false } return strings.HasSuffix(host, ".digitaloceanspaces.com") } // IsBackblazeEndpoint returns true if the endpoint is Backblaze B2. func IsBackblazeEndpoint(endpoint string) bool { host := extractEndpointHost(endpoint) if host == "" { return false } return strings.HasSuffix(host, ".backblazeb2.com") } // IsFilebaseEndpoint returns true if the endpoint is Filebase. func IsFilebaseEndpoint(endpoint string) bool { host := extractEndpointHost(endpoint) if host == "" { return false } return host == "s3.filebase.com" } // IsScalewayEndpoint returns true if the endpoint is Scaleway Object Storage. func IsScalewayEndpoint(endpoint string) bool { host := extractEndpointHost(endpoint) if host == "" { return false } return strings.HasSuffix(host, ".scw.cloud") } // IsCloudflareR2Endpoint returns true if the endpoint is Cloudflare R2. func IsCloudflareR2Endpoint(endpoint string) bool { host := extractEndpointHost(endpoint) if host == "" { return false } return strings.HasSuffix(host, ".r2.cloudflarestorage.com") } // IsSupabaseEndpoint returns true if the endpoint is Supabase Storage S3. func IsSupabaseEndpoint(endpoint string) bool { host := extractEndpointHost(endpoint) if host == "" { return false } return strings.HasSuffix(host, ".supabase.co") } // IsMinIOEndpoint returns true if the endpoint appears to be MinIO or similar // (a custom endpoint with a port number that is not a known cloud provider). func IsMinIOEndpoint(endpoint string) bool { host := extractEndpointHost(endpoint) if host == "" { return false } // MinIO typically uses host:port format without .com domain // Check for port number in the host if !strings.Contains(host, ":") { return false } // Exclude known cloud providers if strings.Contains(host, ".amazonaws.com") || strings.Contains(host, ".digitaloceanspaces.com") || strings.Contains(host, ".backblazeb2.com") || strings.Contains(host, ".filebase.com") || strings.Contains(host, ".scw.cloud") || strings.Contains(host, ".r2.cloudflarestorage.com") || strings.Contains(host, "tigris.dev") || strings.Contains(host, "t3.storage.dev") || strings.Contains(host, ".supabase.co") { return false } return true } // IsLocalEndpoint returns true if the endpoint appears to be a local development // endpoint (localhost, 127.0.0.1, or private network addresses). // These endpoints typically use HTTP instead of HTTPS. func IsLocalEndpoint(endpoint string) bool { host := extractEndpointHost(endpoint) if host == "" { return false } // Remove port if present if idx := strings.LastIndex(host, ":"); idx != -1 { host = host[:idx] } // Check for common local/development hostnames return host == "localhost" || host == "127.0.0.1" || strings.HasPrefix(host, "192.168.") || strings.HasPrefix(host, "10.") || strings.HasPrefix(host, "172.16.") || strings.HasPrefix(host, "172.17.") || strings.HasPrefix(host, "172.18.") || strings.HasPrefix(host, "172.19.") || strings.HasPrefix(host, "172.2") || // 172.20-172.29 strings.HasPrefix(host, "172.30.") || strings.HasPrefix(host, "172.31.") || strings.HasSuffix(host, ".local") || strings.HasSuffix(host, ".localhost") } // EnsureEndpointScheme ensures an endpoint has an HTTP(S) scheme. // For local endpoints (localhost, private IPs), it defaults to http://. // For all other endpoints (cloud providers), it defaults to https://. // Returns the endpoint with scheme and a boolean indicating if a scheme was added. func EnsureEndpointScheme(endpoint string) (string, bool) { if endpoint == "" { return "", false } if strings.HasPrefix(endpoint, "http://") || strings.HasPrefix(endpoint, "https://") { return endpoint, false } // Default to HTTP for local development endpoints, HTTPS for everything else if IsLocalEndpoint(endpoint) { return "http://" + endpoint, true } return "https://" + endpoint, true } // extractEndpointHost extracts the host from an endpoint URL or returns the // endpoint as-is if it's not a full URL. func extractEndpointHost(endpoint string) string { endpoint = strings.TrimSpace(strings.ToLower(endpoint)) if endpoint == "" { return "" } if strings.HasPrefix(endpoint, "http://") || strings.HasPrefix(endpoint, "https://") { if u, err := url.Parse(endpoint); err == nil && u.Host != "" { return u.Host } } return endpoint } // IsURL returns true if s appears to be a URL (has a scheme). var isURLRegex = regexp.MustCompile(`^\w+:\/\/`) func IsURL(s string) bool { return isURLRegex.MatchString(s) } ================================================ FILE: replica_url_test.go ================================================ package litestream_test import ( "testing" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/abs" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/gs" "github.com/benbjohnson/litestream/nats" "github.com/benbjohnson/litestream/oss" "github.com/benbjohnson/litestream/s3" "github.com/benbjohnson/litestream/sftp" "github.com/benbjohnson/litestream/webdav" ) func TestNewReplicaClientFromURL(t *testing.T) { t.Run("S3", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("s3://mybucket/path/to/db") if err != nil { t.Fatal(err) } if client.Type() != "s3" { t.Errorf("expected type 's3', got %q", client.Type()) } s3Client, ok := client.(*s3.ReplicaClient) if !ok { t.Fatalf("expected *s3.ReplicaClient, got %T", client) } if s3Client.Bucket != "mybucket" { t.Errorf("expected bucket 'mybucket', got %q", s3Client.Bucket) } if s3Client.Path != "path/to/db" { t.Errorf("expected path 'path/to/db', got %q", s3Client.Path) } }) t.Run("S3WithQueryParams", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("s3://mybucket/db?endpoint=localhost:9000®ion=us-west-2") if err != nil { t.Fatal(err) } s3Client, ok := client.(*s3.ReplicaClient) if !ok { t.Fatalf("expected *s3.ReplicaClient, got %T", client) } if s3Client.Endpoint != "http://localhost:9000" { t.Errorf("expected endpoint 'http://localhost:9000', got %q", s3Client.Endpoint) } if s3Client.Region != "us-west-2" { t.Errorf("expected region 'us-west-2', got %q", s3Client.Region) } }) t.Run("File", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("file:///tmp/replica") if err != nil { t.Fatal(err) } if client.Type() != "file" { t.Errorf("expected type 'file', got %q", client.Type()) } fileClient, ok := client.(*file.ReplicaClient) if !ok { t.Fatalf("expected *file.ReplicaClient, got %T", client) } if fileClient.Path() != "/tmp/replica" { t.Errorf("expected path '/tmp/replica', got %q", fileClient.Path()) } }) t.Run("GS", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("gs://mybucket/path") if err != nil { t.Fatal(err) } if client.Type() != "gs" { t.Errorf("expected type 'gs', got %q", client.Type()) } gsClient, ok := client.(*gs.ReplicaClient) if !ok { t.Fatalf("expected *gs.ReplicaClient, got %T", client) } if gsClient.Bucket != "mybucket" { t.Errorf("expected bucket 'mybucket', got %q", gsClient.Bucket) } if gsClient.Path != "path" { t.Errorf("expected path 'path', got %q", gsClient.Path) } }) t.Run("GS_MissingBucket", func(t *testing.T) { _, err := litestream.NewReplicaClientFromURL("gs:///path") if err == nil { t.Fatal("expected error for missing bucket") } }) t.Run("ABS", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("abs://mycontainer/path") if err != nil { t.Fatal(err) } if client.Type() != "abs" { t.Errorf("expected type 'abs', got %q", client.Type()) } absClient, ok := client.(*abs.ReplicaClient) if !ok { t.Fatalf("expected *abs.ReplicaClient, got %T", client) } if absClient.Bucket != "mycontainer" { t.Errorf("expected bucket 'mycontainer', got %q", absClient.Bucket) } if absClient.Path != "path" { t.Errorf("expected path 'path', got %q", absClient.Path) } }) t.Run("ABS_WithAccount", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("abs://myaccount@mycontainer/path") if err != nil { t.Fatal(err) } absClient, ok := client.(*abs.ReplicaClient) if !ok { t.Fatalf("expected *abs.ReplicaClient, got %T", client) } if absClient.AccountName != "myaccount" { t.Errorf("expected account 'myaccount', got %q", absClient.AccountName) } if absClient.Bucket != "mycontainer" { t.Errorf("expected bucket 'mycontainer', got %q", absClient.Bucket) } }) t.Run("ABS_MissingBucket", func(t *testing.T) { _, err := litestream.NewReplicaClientFromURL("abs:///path") if err == nil { t.Fatal("expected error for missing bucket") } }) t.Run("SFTP", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("sftp://myuser@host.example.com/path") if err != nil { t.Fatal(err) } if client.Type() != "sftp" { t.Errorf("expected type 'sftp', got %q", client.Type()) } sftpClient, ok := client.(*sftp.ReplicaClient) if !ok { t.Fatalf("expected *sftp.ReplicaClient, got %T", client) } if sftpClient.Host != "host.example.com" { t.Errorf("expected host 'host.example.com', got %q", sftpClient.Host) } if sftpClient.User != "myuser" { t.Errorf("expected user 'myuser', got %q", sftpClient.User) } if sftpClient.Path != "path" { t.Errorf("expected path 'path', got %q", sftpClient.Path) } }) t.Run("SFTP_WithPassword", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("sftp://myuser:secret@host.example.com/path") if err != nil { t.Fatal(err) } sftpClient, ok := client.(*sftp.ReplicaClient) if !ok { t.Fatalf("expected *sftp.ReplicaClient, got %T", client) } if sftpClient.User != "myuser" { t.Errorf("expected user 'myuser', got %q", sftpClient.User) } if sftpClient.Password != "secret" { t.Errorf("expected password 'secret', got %q", sftpClient.Password) } }) t.Run("SFTP_RequiresUserError", func(t *testing.T) { _, err := litestream.NewReplicaClientFromURL("sftp://host.example.com/path") if err == nil { t.Fatal("expected error for missing user") } }) t.Run("SFTP_MissingHost", func(t *testing.T) { _, err := litestream.NewReplicaClientFromURL("sftp:///path") if err == nil { t.Fatal("expected error for missing host") } }) t.Run("WebDAV", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("webdav://host.example.com/path") if err != nil { t.Fatal(err) } if client.Type() != "webdav" { t.Errorf("expected type 'webdav', got %q", client.Type()) } webdavClient, ok := client.(*webdav.ReplicaClient) if !ok { t.Fatalf("expected *webdav.ReplicaClient, got %T", client) } if webdavClient.URL != "http://host.example.com" { t.Errorf("expected URL 'http://host.example.com', got %q", webdavClient.URL) } if webdavClient.Path != "path" { t.Errorf("expected path 'path', got %q", webdavClient.Path) } }) t.Run("WebDAVS", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("webdavs://host.example.com/path") if err != nil { t.Fatal(err) } if client.Type() != "webdav" { t.Errorf("expected type 'webdav', got %q", client.Type()) } webdavClient, ok := client.(*webdav.ReplicaClient) if !ok { t.Fatalf("expected *webdav.ReplicaClient, got %T", client) } if webdavClient.URL != "https://host.example.com" { t.Errorf("expected URL 'https://host.example.com', got %q", webdavClient.URL) } }) t.Run("WebDAV_WithCredentials", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("webdav://myuser:secret@host.example.com/path") if err != nil { t.Fatal(err) } webdavClient, ok := client.(*webdav.ReplicaClient) if !ok { t.Fatalf("expected *webdav.ReplicaClient, got %T", client) } if webdavClient.Username != "myuser" { t.Errorf("expected username 'myuser', got %q", webdavClient.Username) } if webdavClient.Password != "secret" { t.Errorf("expected password 'secret', got %q", webdavClient.Password) } if webdavClient.URL != "http://host.example.com" { t.Errorf("expected URL 'http://host.example.com', got %q", webdavClient.URL) } }) t.Run("WebDAVS_WithCredentials", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("webdavs://myuser:secret@host.example.com/path") if err != nil { t.Fatal(err) } webdavClient, ok := client.(*webdav.ReplicaClient) if !ok { t.Fatalf("expected *webdav.ReplicaClient, got %T", client) } if webdavClient.Username != "myuser" { t.Errorf("expected username 'myuser', got %q", webdavClient.Username) } if webdavClient.Password != "secret" { t.Errorf("expected password 'secret', got %q", webdavClient.Password) } if webdavClient.URL != "https://host.example.com" { t.Errorf("expected URL 'https://host.example.com', got %q", webdavClient.URL) } }) t.Run("WebDAV_MissingHost", func(t *testing.T) { _, err := litestream.NewReplicaClientFromURL("webdav:///path") if err == nil { t.Fatal("expected error for missing host") } }) t.Run("NATS", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("nats://localhost:4222/mybucket") if err != nil { t.Fatal(err) } if client.Type() != "nats" { t.Errorf("expected type 'nats', got %q", client.Type()) } natsClient, ok := client.(*nats.ReplicaClient) if !ok { t.Fatalf("expected *nats.ReplicaClient, got %T", client) } if natsClient.URL != "nats://localhost:4222" { t.Errorf("expected URL 'nats://localhost:4222', got %q", natsClient.URL) } if natsClient.BucketName != "mybucket" { t.Errorf("expected bucket 'mybucket', got %q", natsClient.BucketName) } }) t.Run("NATS_WithCredentials", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("nats://myuser:secret@localhost:4222/mybucket") if err != nil { t.Fatal(err) } natsClient, ok := client.(*nats.ReplicaClient) if !ok { t.Fatalf("expected *nats.ReplicaClient, got %T", client) } if natsClient.Username != "myuser" { t.Errorf("expected username 'myuser', got %q", natsClient.Username) } if natsClient.Password != "secret" { t.Errorf("expected password 'secret', got %q", natsClient.Password) } if natsClient.URL != "nats://localhost:4222" { t.Errorf("expected URL 'nats://localhost:4222', got %q", natsClient.URL) } }) t.Run("NATS_MissingBucket", func(t *testing.T) { _, err := litestream.NewReplicaClientFromURL("nats://localhost:4222/") if err == nil { t.Fatal("expected error for missing bucket") } }) t.Run("OSS", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("oss://mybucket/path") if err != nil { t.Fatal(err) } if client.Type() != "oss" { t.Errorf("expected type 'oss', got %q", client.Type()) } ossClient, ok := client.(*oss.ReplicaClient) if !ok { t.Fatalf("expected *oss.ReplicaClient, got %T", client) } if ossClient.Bucket != "mybucket" { t.Errorf("expected bucket 'mybucket', got %q", ossClient.Bucket) } if ossClient.Path != "path" { t.Errorf("expected path 'path', got %q", ossClient.Path) } }) t.Run("OSS_WithRegion", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("oss://mybucket.oss-cn-shanghai.aliyuncs.com/path") if err != nil { t.Fatal(err) } ossClient, ok := client.(*oss.ReplicaClient) if !ok { t.Fatalf("expected *oss.ReplicaClient, got %T", client) } if ossClient.Bucket != "mybucket" { t.Errorf("expected bucket 'mybucket', got %q", ossClient.Bucket) } // Note: Region is extracted without the 'oss-' prefix if ossClient.Region != "cn-shanghai" { t.Errorf("expected region 'cn-shanghai', got %q", ossClient.Region) } }) t.Run("OSS_MissingBucket", func(t *testing.T) { _, err := litestream.NewReplicaClientFromURL("oss:///path") if err == nil { t.Fatal("expected error for missing bucket") } }) // Note: file:// with empty path returns "." due to path.Clean behavior. // This is technically valid but may not be the intended behavior. t.Run("File_EmptyPathReturnsDot", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("file://") if err != nil { t.Fatal(err) } fileClient, ok := client.(*file.ReplicaClient) if !ok { t.Fatalf("expected *file.ReplicaClient, got %T", client) } // path.Clean("") returns "." which passes the empty check if fileClient.Path() != "." { t.Errorf("expected path '.', got %q", fileClient.Path()) } }) t.Run("S3_ARN", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("s3://arn:aws:s3:us-east-1:123456789012:accesspoint/db-access/backups") if err != nil { t.Fatal(err) } s3Client, ok := client.(*s3.ReplicaClient) if !ok { t.Fatalf("expected *s3.ReplicaClient, got %T", client) } if s3Client.Bucket != "arn:aws:s3:us-east-1:123456789012:accesspoint/db-access" { t.Errorf("expected bucket ARN, got %q", s3Client.Bucket) } if s3Client.Path != "backups" { t.Errorf("expected path 'backups', got %q", s3Client.Path) } }) t.Run("S3_ARN_WithQueryParams", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("s3://arn:aws:s3:us-east-1:123456789012:accesspoint/db-access/backups?sign-payload=false") if err != nil { t.Fatal(err) } s3Client, ok := client.(*s3.ReplicaClient) if !ok { t.Fatalf("expected *s3.ReplicaClient, got %T", client) } if s3Client.Bucket != "arn:aws:s3:us-east-1:123456789012:accesspoint/db-access" { t.Errorf("expected bucket ARN, got %q", s3Client.Bucket) } if s3Client.Path != "backups" { t.Errorf("expected path 'backups', got %q", s3Client.Path) } if s3Client.SignPayload != false { t.Errorf("expected SignPayload=false from query param, got %v", s3Client.SignPayload) } }) t.Run("S3_MissingBucket", func(t *testing.T) { _, err := litestream.NewReplicaClientFromURL("s3:///path") if err == nil { t.Fatal("expected error for missing bucket") } }) t.Run("EmptyURL", func(t *testing.T) { _, err := litestream.NewReplicaClientFromURL("") if err == nil { t.Fatal("expected error for empty URL") } }) t.Run("UnsupportedScheme", func(t *testing.T) { _, err := litestream.NewReplicaClientFromURL("unknown://bucket/path") if err == nil { t.Fatal("expected error for unsupported scheme") } }) t.Run("InvalidURL", func(t *testing.T) { _, err := litestream.NewReplicaClientFromURL("not-a-valid-url") if err == nil { t.Fatal("expected error for invalid URL") } }) } func TestReplicaTypeFromURL(t *testing.T) { tests := []struct { url string expected string }{ {"s3://bucket/path", "s3"}, {"gs://bucket/path", "gs"}, {"abs://container/path", "abs"}, {"file:///path/to/replica", "file"}, {"sftp://host/path", "sftp"}, {"webdav://host/path", "webdav"}, {"webdavs://host/path", "webdav"}, {"nats://host/bucket", "nats"}, {"oss://bucket/path", "oss"}, {"", ""}, {"invalid", ""}, } for _, tt := range tests { t.Run(tt.url, func(t *testing.T) { got := litestream.ReplicaTypeFromURL(tt.url) if got != tt.expected { t.Errorf("ReplicaTypeFromURL(%q) = %q, want %q", tt.url, got, tt.expected) } }) } } func TestIsURL(t *testing.T) { tests := []struct { s string expected bool }{ {"s3://bucket/path", true}, {"file:///path", true}, {"https://example.com", true}, {"/path/to/file", false}, {"relative/path", false}, {"", false}, } for _, tt := range tests { t.Run(tt.s, func(t *testing.T) { got := litestream.IsURL(tt.s) if got != tt.expected { t.Errorf("IsURL(%q) = %v, want %v", tt.s, got, tt.expected) } }) } } func TestBoolQueryValue(t *testing.T) { t.Run("True values", func(t *testing.T) { for _, v := range []string{"true", "True", "TRUE", "1", "t", "yes"} { query := make(map[string][]string) query["key"] = []string{v} value, ok := litestream.BoolQueryValue(query, "key") if !ok { t.Errorf("BoolQueryValue with %q should be ok", v) } if !value { t.Errorf("BoolQueryValue with %q should be true", v) } } }) t.Run("False values", func(t *testing.T) { for _, v := range []string{"false", "False", "FALSE", "0", "f", "no"} { query := make(map[string][]string) query["key"] = []string{v} value, ok := litestream.BoolQueryValue(query, "key") if !ok { t.Errorf("BoolQueryValue with %q should be ok", v) } if value { t.Errorf("BoolQueryValue with %q should be false", v) } } }) t.Run("Missing key", func(t *testing.T) { query := make(map[string][]string) _, ok := litestream.BoolQueryValue(query, "key") if ok { t.Error("BoolQueryValue with missing key should not be ok") } }) t.Run("Multiple keys", func(t *testing.T) { query := make(map[string][]string) query["key2"] = []string{"true"} value, ok := litestream.BoolQueryValue(query, "key1", "key2") if !ok { t.Error("BoolQueryValue should find second key") } if !value { t.Error("BoolQueryValue should return true for second key") } }) t.Run("Nil query", func(t *testing.T) { _, ok := litestream.BoolQueryValue(nil, "key") if ok { t.Error("BoolQueryValue with nil query should not be ok") } }) t.Run("Invalid value returns false with ok", func(t *testing.T) { query := make(map[string][]string) query["key"] = []string{"invalid"} value, ok := litestream.BoolQueryValue(query, "key") if !ok { t.Error("BoolQueryValue with invalid value should be ok") } if value { t.Error("BoolQueryValue with invalid value should be false") } }) } func TestIsTigrisEndpoint(t *testing.T) { tests := []struct { endpoint string expected bool }{ {"fly.storage.tigris.dev", true}, {"FLY.STORAGE.TIGRIS.DEV", true}, {"https://fly.storage.tigris.dev", true}, {"http://fly.storage.tigris.dev", true}, {"t3.storage.dev", true}, {"T3.STORAGE.DEV", true}, {"https://t3.storage.dev", true}, {"http://t3.storage.dev", true}, {"s3.amazonaws.com", false}, {"localhost:9000", false}, {"", false}, {" ", false}, {"https://s3.us-east-1.amazonaws.com", false}, } for _, tt := range tests { t.Run(tt.endpoint, func(t *testing.T) { got := litestream.IsTigrisEndpoint(tt.endpoint) if got != tt.expected { t.Errorf("IsTigrisEndpoint(%q) = %v, want %v", tt.endpoint, got, tt.expected) } }) } } func TestRegionFromS3ARN(t *testing.T) { tests := []struct { arn string expected string }{ {"arn:aws:s3:us-east-1:123456789012:accesspoint/db-access", "us-east-1"}, {"arn:aws:s3:eu-west-1:123456789012:accesspoint/db-access", "eu-west-1"}, {"arn:aws:s3:ap-southeast-2:123456789012:accesspoint/db-access", "ap-southeast-2"}, {"arn:aws:s3::123456789012:accesspoint/db-access", ""}, {"invalid-arn", ""}, {"", ""}, {"arn:aws:s3", ""}, } for _, tt := range tests { t.Run(tt.arn, func(t *testing.T) { got := litestream.RegionFromS3ARN(tt.arn) if got != tt.expected { t.Errorf("RegionFromS3ARN(%q) = %q, want %q", tt.arn, got, tt.expected) } }) } } func TestCleanReplicaURLPath(t *testing.T) { tests := []struct { path string expected string }{ {"", ""}, {"path", "path"}, {"/path", "path"}, {"path/", "path"}, {"/path/", "path"}, {"path/to/db", "path/to/db"}, {"/path/to/db", "path/to/db"}, {"//path//to//db", "path/to/db"}, {".", ""}, {"/.", ""}, {"./path", "path"}, } for _, tt := range tests { t.Run(tt.path, func(t *testing.T) { got := litestream.CleanReplicaURLPath(tt.path) if got != tt.expected { t.Errorf("CleanReplicaURLPath(%q) = %q, want %q", tt.path, got, tt.expected) } }) } } func TestParseS3AccessPointURL(t *testing.T) { tests := []struct { name string url string wantScheme string wantHost string wantPath string wantQuery map[string]string wantErr bool }{ { name: "BasicARN", url: "s3://arn:aws:s3:us-east-1:123456789012:accesspoint/my-access-point", wantScheme: "s3", wantHost: "arn:aws:s3:us-east-1:123456789012:accesspoint/my-access-point", wantPath: "", wantQuery: nil, }, { name: "ARNWithPath", url: "s3://arn:aws:s3:us-east-1:123456789012:accesspoint/my-access-point/backups/db", wantScheme: "s3", wantHost: "arn:aws:s3:us-east-1:123456789012:accesspoint/my-access-point", wantPath: "backups/db", wantQuery: nil, }, { name: "ARNWithSingleQueryParam", url: "s3://arn:aws:s3:us-east-1:123456789012:accesspoint/my-access-point?sign-payload=true", wantScheme: "s3", wantHost: "arn:aws:s3:us-east-1:123456789012:accesspoint/my-access-point", wantPath: "", wantQuery: map[string]string{"sign-payload": "true"}, }, { name: "ARNWithMultipleQueryParams", url: "s3://arn:aws:s3:us-east-1:123456789012:accesspoint/my-access-point?sign-payload=false®ion=us-west-2", wantScheme: "s3", wantHost: "arn:aws:s3:us-east-1:123456789012:accesspoint/my-access-point", wantPath: "", wantQuery: map[string]string{"sign-payload": "false", "region": "us-west-2"}, }, { name: "ARNWithPathAndQuery", url: "s3://arn:aws:s3:us-east-1:123456789012:accesspoint/my-access-point/backups?sign-payload=true", wantScheme: "s3", wantHost: "arn:aws:s3:us-east-1:123456789012:accesspoint/my-access-point", wantPath: "backups", wantQuery: map[string]string{"sign-payload": "true"}, }, { name: "CaseInsensitiveScheme", url: "S3://arn:aws:s3:us-east-1:123456789012:accesspoint/my-access-point", wantScheme: "s3", wantHost: "arn:aws:s3:us-east-1:123456789012:accesspoint/my-access-point", wantPath: "", wantQuery: nil, }, { name: "EmptyQueryValue", url: "s3://arn:aws:s3:us-east-1:123456789012:accesspoint/my-access-point?key=", wantScheme: "s3", wantHost: "arn:aws:s3:us-east-1:123456789012:accesspoint/my-access-point", wantPath: "", wantQuery: map[string]string{"key": ""}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { scheme, host, path, query, _, err := litestream.ParseReplicaURLWithQuery(tt.url) if tt.wantErr { if err == nil { t.Fatal("expected error, got nil") } return } if err != nil { t.Fatalf("unexpected error: %v", err) } if scheme != tt.wantScheme { t.Errorf("scheme = %q, want %q", scheme, tt.wantScheme) } if host != tt.wantHost { t.Errorf("host = %q, want %q", host, tt.wantHost) } if path != tt.wantPath { t.Errorf("path = %q, want %q", path, tt.wantPath) } if tt.wantQuery == nil { if len(query) > 0 { t.Errorf("query = %v, want nil/empty", query) } } else { for key, wantVal := range tt.wantQuery { if gotVal := query.Get(key); gotVal != wantVal { t.Errorf("query[%q] = %q, want %q", key, gotVal, wantVal) } } } }) } } func TestIsDigitalOceanEndpoint(t *testing.T) { tests := []struct { endpoint string expected bool }{ {"https://sfo3.digitaloceanspaces.com", true}, {"https://nyc3.digitaloceanspaces.com", true}, {"sfo3.digitaloceanspaces.com", true}, {"https://s3.amazonaws.com", false}, {"https://s3.filebase.com", false}, {"", false}, } for _, tt := range tests { t.Run(tt.endpoint, func(t *testing.T) { got := litestream.IsDigitalOceanEndpoint(tt.endpoint) if got != tt.expected { t.Errorf("IsDigitalOceanEndpoint(%q) = %v, want %v", tt.endpoint, got, tt.expected) } }) } } func TestIsBackblazeEndpoint(t *testing.T) { tests := []struct { endpoint string expected bool }{ {"https://s3.us-west-002.backblazeb2.com", true}, {"https://s3.eu-central-003.backblazeb2.com", true}, {"s3.us-west-002.backblazeb2.com", true}, {"https://s3.amazonaws.com", false}, {"https://s3.filebase.com", false}, {"", false}, } for _, tt := range tests { t.Run(tt.endpoint, func(t *testing.T) { got := litestream.IsBackblazeEndpoint(tt.endpoint) if got != tt.expected { t.Errorf("IsBackblazeEndpoint(%q) = %v, want %v", tt.endpoint, got, tt.expected) } }) } } func TestIsFilebaseEndpoint(t *testing.T) { tests := []struct { endpoint string expected bool }{ {"https://s3.filebase.com", true}, {"http://s3.filebase.com", true}, {"s3.filebase.com", true}, {"https://s3.amazonaws.com", false}, {"https://sfo3.digitaloceanspaces.com", false}, {"", false}, } for _, tt := range tests { t.Run(tt.endpoint, func(t *testing.T) { got := litestream.IsFilebaseEndpoint(tt.endpoint) if got != tt.expected { t.Errorf("IsFilebaseEndpoint(%q) = %v, want %v", tt.endpoint, got, tt.expected) } }) } } func TestIsScalewayEndpoint(t *testing.T) { tests := []struct { endpoint string expected bool }{ {"https://s3.fr-par.scw.cloud", true}, {"https://s3.nl-ams.scw.cloud", true}, {"s3.fr-par.scw.cloud", true}, {"https://s3.amazonaws.com", false}, {"https://s3.filebase.com", false}, {"", false}, } for _, tt := range tests { t.Run(tt.endpoint, func(t *testing.T) { got := litestream.IsScalewayEndpoint(tt.endpoint) if got != tt.expected { t.Errorf("IsScalewayEndpoint(%q) = %v, want %v", tt.endpoint, got, tt.expected) } }) } } func TestIsCloudflareR2Endpoint(t *testing.T) { tests := []struct { endpoint string expected bool }{ {"https://abcdef123456.r2.cloudflarestorage.com", true}, {"https://account-id.r2.cloudflarestorage.com", true}, {"abcdef123456.r2.cloudflarestorage.com", true}, {"https://s3.amazonaws.com", false}, {"https://s3.filebase.com", false}, {"", false}, } for _, tt := range tests { t.Run(tt.endpoint, func(t *testing.T) { got := litestream.IsCloudflareR2Endpoint(tt.endpoint) if got != tt.expected { t.Errorf("IsCloudflareR2Endpoint(%q) = %v, want %v", tt.endpoint, got, tt.expected) } }) } } func TestIsSupabaseEndpoint(t *testing.T) { tests := []struct { endpoint string expected bool }{ {"https://myproject.supabase.co/storage/v1/s3", true}, {"https://abcdefghij.supabase.co", true}, {"myproject.supabase.co", true}, {"https://s3.amazonaws.com", false}, {"https://s3.filebase.com", false}, {"", false}, } for _, tt := range tests { t.Run(tt.endpoint, func(t *testing.T) { got := litestream.IsSupabaseEndpoint(tt.endpoint) if got != tt.expected { t.Errorf("IsSupabaseEndpoint(%q) = %v, want %v", tt.endpoint, got, tt.expected) } }) } } func TestIsHetznerEndpoint(t *testing.T) { tests := []struct { endpoint string expected bool }{ {"fsn1.your-objectstorage.com", true}, {"nbg1.your-objectstorage.com", true}, {"https://fsn1.your-objectstorage.com", true}, {"http://nbg1.your-objectstorage.com", true}, {"s3.amazonaws.com", false}, {"https://s3.filebase.com", false}, {"", false}, } for _, tt := range tests { t.Run(tt.endpoint, func(t *testing.T) { got := litestream.IsHetznerEndpoint(tt.endpoint) if got != tt.expected { t.Errorf("IsHetznerEndpoint(%q) = %v, want %v", tt.endpoint, got, tt.expected) } }) } } func TestIsMinIOEndpoint(t *testing.T) { tests := []struct { endpoint string expected bool }{ {"http://localhost:9000", true}, {"http://192.168.1.100:9000", true}, {"minio.local:9000", true}, {"https://s3.amazonaws.com", false}, {"https://s3.filebase.com", false}, {"https://sfo3.digitaloceanspaces.com", false}, {"s3.filebase.com", false}, // No port, not MinIO {"", false}, } for _, tt := range tests { t.Run(tt.endpoint, func(t *testing.T) { got := litestream.IsMinIOEndpoint(tt.endpoint) if got != tt.expected { t.Errorf("IsMinIOEndpoint(%q) = %v, want %v", tt.endpoint, got, tt.expected) } }) } } func TestIsLocalEndpoint(t *testing.T) { tests := []struct { endpoint string expected bool }{ // Localhost variants {"localhost", true}, {"localhost:9000", true}, {"http://localhost:9000", true}, {"https://localhost:9000", true}, // Loopback IP {"127.0.0.1", true}, {"127.0.0.1:9000", true}, {"http://127.0.0.1:9000", true}, // Private network ranges (RFC1918) {"192.168.1.100", true}, {"192.168.1.100:9000", true}, {"http://192.168.1.100:9000", true}, {"10.0.0.1", true}, {"10.0.0.1:9000", true}, {"172.16.0.1", true}, {"172.31.255.255", true}, // .local and .localhost TLDs {"minio.local", true}, {"minio.local:9000", true}, {"http://minio.local:9000", true}, {"dev.localhost", true}, {"test.localhost:8080", true}, // Non-local endpoints (cloud providers) {"s3.amazonaws.com", false}, {"https://s3.amazonaws.com", false}, {"abcdef.r2.cloudflarestorage.com", false}, {"https://abcdef.r2.cloudflarestorage.com", false}, {"fly.storage.tigris.dev", false}, {"s3.us-west-000.backblazeb2.com", false}, {"sfo3.digitaloceanspaces.com", false}, {"s3.filebase.com", false}, // Empty {"", false}, } for _, tt := range tests { t.Run(tt.endpoint, func(t *testing.T) { got := litestream.IsLocalEndpoint(tt.endpoint) if got != tt.expected { t.Errorf("IsLocalEndpoint(%q) = %v, want %v", tt.endpoint, got, tt.expected) } }) } } // TestS3ProviderDefaults tests that provider-specific defaults are applied // when creating S3 clients from URLs with provider endpoints. // These tests ensure edge case bugs like #912, #918, #940, #947 don't regress. func TestS3ProviderDefaults(t *testing.T) { tests := []struct { name string url string wantSignPayload bool wantForcePathStyle bool wantRequireMD5 bool }{ { name: "CloudflareR2_SignPayload", url: "s3://mybucket/path?endpoint=https://account123.r2.cloudflarestorage.com", wantSignPayload: true, wantForcePathStyle: true, // Custom endpoint default wantRequireMD5: true, }, { name: "BackblazeB2_SignPayloadAndPathStyle", url: "s3://mybucket/path?endpoint=https://s3.us-west-002.backblazeb2.com", wantSignPayload: true, wantForcePathStyle: true, wantRequireMD5: true, }, { name: "DigitalOcean_SignPayload", url: "s3://mybucket/path?endpoint=https://sfo3.digitaloceanspaces.com", wantSignPayload: true, wantForcePathStyle: true, // Custom endpoint default wantRequireMD5: true, }, { name: "Scaleway_SignPayload", url: "s3://mybucket/path?endpoint=https://s3.fr-par.scw.cloud", wantSignPayload: true, wantForcePathStyle: true, // Custom endpoint default wantRequireMD5: true, }, { name: "Filebase_SignPayloadAndPathStyle", url: "s3://mybucket/path?endpoint=https://s3.filebase.com", wantSignPayload: true, wantForcePathStyle: true, wantRequireMD5: true, }, { name: "Tigris_SignPayloadNoMD5", url: "s3://mybucket/path?endpoint=https://fly.storage.tigris.dev", wantSignPayload: true, wantForcePathStyle: true, // Custom endpoint default wantRequireMD5: false, }, { name: "MinIO_SignPayloadAndPathStyle", url: "s3://mybucket/path?endpoint=http://localhost:9000", wantSignPayload: true, wantForcePathStyle: true, wantRequireMD5: true, }, { name: "Hetzner_SignPayload", url: "s3://mybucket/path?endpoint=https://fsn1.your-objectstorage.com", wantSignPayload: true, wantForcePathStyle: true, wantRequireMD5: true, }, { name: "AWS_Defaults", url: "s3://mybucket/path", wantSignPayload: true, // Default wantForcePathStyle: false, wantRequireMD5: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL(tt.url) if err != nil { t.Fatalf("NewReplicaClientFromURL(%q) error: %v", tt.url, err) } s3Client, ok := client.(*s3.ReplicaClient) if !ok { t.Fatalf("expected *s3.ReplicaClient, got %T", client) } if s3Client.SignPayload != tt.wantSignPayload { t.Errorf("SignPayload = %v, want %v", s3Client.SignPayload, tt.wantSignPayload) } if s3Client.ForcePathStyle != tt.wantForcePathStyle { t.Errorf("ForcePathStyle = %v, want %v", s3Client.ForcePathStyle, tt.wantForcePathStyle) } if s3Client.RequireContentMD5 != tt.wantRequireMD5 { t.Errorf("RequireContentMD5 = %v, want %v", s3Client.RequireContentMD5, tt.wantRequireMD5) } }) } } func TestEnsureEndpointScheme(t *testing.T) { tests := []struct { input string expected string schemeAdded bool }{ // Already has scheme - no change {"https://example.com", "https://example.com", false}, {"http://localhost:9000", "http://localhost:9000", false}, {"http://192.168.1.1:9000", "http://192.168.1.1:9000", false}, // Local endpoints get http:// {"localhost:9000", "http://localhost:9000", true}, {"127.0.0.1:9000", "http://127.0.0.1:9000", true}, {"192.168.1.100:9000", "http://192.168.1.100:9000", true}, {"10.0.0.1:9000", "http://10.0.0.1:9000", true}, {"minio.local:9000", "http://minio.local:9000", true}, // Cloud endpoints get https:// (THIS IS THE KEY FIX) {"abcdef.r2.cloudflarestorage.com", "https://abcdef.r2.cloudflarestorage.com", true}, {"s3.us-west-000.backblazeb2.com", "https://s3.us-west-000.backblazeb2.com", true}, {"fly.storage.tigris.dev", "https://fly.storage.tigris.dev", true}, {"sfo3.digitaloceanspaces.com", "https://sfo3.digitaloceanspaces.com", true}, {"s3.filebase.com", "https://s3.filebase.com", true}, {"s3.fr-par.scw.cloud", "https://s3.fr-par.scw.cloud", true}, // Empty returns empty {"", "", false}, } for _, tt := range tests { t.Run(tt.input, func(t *testing.T) { got, added := litestream.EnsureEndpointScheme(tt.input) if got != tt.expected { t.Errorf("EnsureEndpointScheme(%q) = %q, want %q", tt.input, got, tt.expected) } if added != tt.schemeAdded { t.Errorf("EnsureEndpointScheme(%q) schemeAdded = %v, want %v", tt.input, added, tt.schemeAdded) } }) } } // TestS3ProviderDefaults_QueryParamOverrides tests that explicit query parameters // override provider-specific defaults. func TestS3ProviderDefaults_QueryParamOverrides(t *testing.T) { t.Run("SignPayload_ExplicitFalse", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("s3://mybucket/path?endpoint=https://account123.r2.cloudflarestorage.com&sign-payload=false") if err != nil { t.Fatal(err) } s3Client := client.(*s3.ReplicaClient) if s3Client.SignPayload != false { t.Errorf("SignPayload = %v, want false (explicit override)", s3Client.SignPayload) } }) t.Run("ForcePathStyle_ExplicitFalse", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("s3://mybucket/path?endpoint=https://s3.us-west-002.backblazeb2.com&forcePathStyle=false") if err != nil { t.Fatal(err) } s3Client := client.(*s3.ReplicaClient) if s3Client.ForcePathStyle != false { t.Errorf("ForcePathStyle = %v, want false (explicit override)", s3Client.ForcePathStyle) } }) t.Run("RequireMD5_ExplicitTrue_Tigris", func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL("s3://mybucket/path?endpoint=https://fly.storage.tigris.dev&require-content-md5=true") if err != nil { t.Fatal(err) } s3Client := client.(*s3.ReplicaClient) if s3Client.RequireContentMD5 != true { t.Errorf("RequireContentMD5 = %v, want true (explicit override)", s3Client.RequireContentMD5) } }) } ================================================ FILE: restore_fuzz_test.go ================================================ package litestream_test import ( "math/rand" "path/filepath" "sync" "testing" "time" "github.com/superfly/ltx" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/internal/testingutil" ) func FuzzRestoreWithMissingCompactedFile(f *testing.F) { f.Add(int64(1)) f.Add(int64(2)) f.Add(int64(3)) f.Fuzz(func(t *testing.T, seed int64) { if testing.Short() { t.Skip("skipping fuzz test in short mode") } rng := rand.New(rand.NewSource(seed)) ctx := t.Context() db := testingutil.NewDB(t, filepath.Join(t.TempDir(), "db")) db.MonitorInterval = 20 * time.Millisecond db.Replica = litestream.NewReplica(db) db.Replica.SyncInterval = 20 * time.Millisecond client := file.NewReplicaClient(t.TempDir()) db.Replica.Client = client if err := db.Open(); err != nil { t.Fatal(err) } sqldb := testingutil.MustOpenSQLDB(t, db.Path()) defer testingutil.MustCloseDBs(t, db, sqldb) store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{ {Level: 0}, {Level: 1, Interval: 50 * time.Millisecond}, {Level: 2, Interval: 150 * time.Millisecond}, }) store.SnapshotInterval = 200 * time.Millisecond if err := store.Open(ctx); err != nil { t.Fatal(err) } defer store.Close(ctx) if _, err := sqldb.ExecContext(ctx, `CREATE TABLE t (id INTEGER PRIMARY KEY, val TEXT);`); err != nil { t.Fatal(err) } done := make(chan struct{}) var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done() ticker := time.NewTicker(5 * time.Millisecond) defer ticker.Stop() for { select { case <-ctx.Done(): return case <-done: return case <-ticker.C: if _, err := sqldb.ExecContext(ctx, `INSERT INTO t (val) VALUES (?);`, time.Now().String()); err != nil { return } } } }() runDuration := 800*time.Millisecond + time.Duration(rng.Intn(400))*time.Millisecond time.Sleep(runDuration) close(done) wg.Wait() // Allow compaction/snapshotting to catch up. time.Sleep(200 * time.Millisecond) var candidates []*ltx.FileInfo for _, level := range []int{1, 2} { itr, err := client.LTXFiles(ctx, level, 0, false) if err != nil { t.Fatal(err) } for itr.Next() { candidates = append(candidates, itr.Item()) } if err := itr.Close(); err != nil { t.Fatal(err) } } if len(candidates) == 0 { t.Skip("no compacted files available to delete") } toDelete := candidates[rng.Intn(len(candidates))] if err := client.DeleteLTXFiles(ctx, []*ltx.FileInfo{toDelete}); err != nil { t.Fatal(err) } outputPath := filepath.Join(t.TempDir(), "restore.db") if err := db.Replica.Restore(ctx, litestream.RestoreOptions{ OutputPath: outputPath, }); err != nil { t.Fatalf("restore failed after deleting L%d %s: %v", toDelete.Level, ltx.FormatFilename(toDelete.MinTXID, toDelete.MaxTXID), err) } restoreDB := testingutil.MustOpenSQLDB(t, outputPath) defer testingutil.MustCloseSQLDB(t, restoreDB) var result string if err := restoreDB.QueryRowContext(ctx, `PRAGMA integrity_check;`).Scan(&result); err != nil { t.Fatal(err) } else if result != "ok" { t.Fatalf("integrity check failed: %s", result) } var count int if err := restoreDB.QueryRowContext(ctx, `SELECT COUNT(*) FROM t`).Scan(&count); err != nil { t.Fatal(err) } else if count == 0 { t.Fatal("no records found in restored database") } }) } ================================================ FILE: s3/leaser.go ================================================ package s3 import ( "bytes" "context" "encoding/json" "errors" "fmt" "log/slog" "os" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3/types" "github.com/aws/smithy-go" "github.com/benbjohnson/litestream" ) const ( DefaultLeaseTTL = 30 * time.Second DefaultLeasePath = "lock.json" LeaserType = "s3" ) var ( _ litestream.Leaser = (*Leaser)(nil) ErrLeaseRequired = errors.New("lease required") ErrLeaseETagRequired = errors.New("lease etag required") ErrLeaseAlreadyReleased = errors.New("lease already released") ) // S3API is the interface for S3 operations needed by Leaser. type S3API interface { GetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error) PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) DeleteObject(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error) } type Leaser struct { s3 S3API logger *slog.Logger Bucket string Path string TTL time.Duration Owner string } func NewLeaser() *Leaser { owner, _ := os.Hostname() if owner == "" { owner = fmt.Sprintf("pid-%d", os.Getpid()) } else { owner = fmt.Sprintf("%s:%d", owner, os.Getpid()) } return &Leaser{ logger: slog.Default().WithGroup("s3-leaser"), TTL: DefaultLeaseTTL, Owner: owner, } } func (l *Leaser) SetLogger(logger *slog.Logger) { l.logger = logger.WithGroup("s3-leaser") } func (l *Leaser) Client() S3API { return l.s3 } func (l *Leaser) SetClient(client S3API) { l.s3 = client } func (l *Leaser) Type() string { return LeaserType } func (l *Leaser) lockKey() string { if l.Path == "" { return DefaultLeasePath } return l.Path + "/" + DefaultLeasePath } func (l *Leaser) AcquireLease(ctx context.Context) (*litestream.Lease, error) { existing, etag, err := l.readLease(ctx) if err != nil && !errors.Is(err, os.ErrNotExist) { return nil, fmt.Errorf("read existing lease: %w", err) } if existing != nil && !existing.IsExpired() { return nil, &litestream.LeaseExistsError{ Owner: existing.Owner, ExpiresAt: existing.ExpiresAt, } } var generation int64 = 1 if existing != nil { generation = existing.Generation + 1 } newLease := &litestream.Lease{ Generation: generation, ExpiresAt: time.Now().Add(l.TTL), Owner: l.Owner, } newETag, err := l.writeLease(ctx, newLease, etag) if err != nil { var leaseErr *litestream.LeaseExistsError if errors.As(err, &leaseErr) { if current, _, readErr := l.readLease(ctx); readErr == nil && current != nil { return nil, &litestream.LeaseExistsError{ Owner: current.Owner, ExpiresAt: current.ExpiresAt, } } } return nil, err } newLease.ETag = newETag l.logger.Debug("lease acquired", "generation", newLease.Generation, "owner", newLease.Owner, "expires_at", newLease.ExpiresAt, "etag", newLease.ETag) return newLease, nil } func (l *Leaser) RenewLease(ctx context.Context, lease *litestream.Lease) (*litestream.Lease, error) { if lease == nil { return nil, ErrLeaseRequired } if lease.ETag == "" { return nil, ErrLeaseETagRequired } newLease := &litestream.Lease{ Generation: lease.Generation, ExpiresAt: time.Now().Add(l.TTL), Owner: l.Owner, } newETag, err := l.writeLease(ctx, newLease, lease.ETag) if err != nil { var leaseErr *litestream.LeaseExistsError if errors.As(err, &leaseErr) { return nil, litestream.ErrLeaseNotHeld } return nil, err } newLease.ETag = newETag l.logger.Debug("lease renewed", "generation", newLease.Generation, "owner", newLease.Owner, "expires_at", newLease.ExpiresAt, "etag", newLease.ETag) return newLease, nil } func (l *Leaser) ReleaseLease(ctx context.Context, lease *litestream.Lease) error { if lease == nil { return ErrLeaseRequired } if lease.ETag == "" { return ErrLeaseETagRequired } key := l.lockKey() _, err := l.s3.DeleteObject(ctx, &s3.DeleteObjectInput{ Bucket: aws.String(l.Bucket), Key: aws.String(key), IfMatch: aws.String(lease.ETag), }) if err != nil { if isNotExists(err) || isNotFoundError(err) { return ErrLeaseAlreadyReleased } if isPreconditionFailed(err) { return litestream.ErrLeaseNotHeld } return fmt.Errorf("delete lease: %w", err) } l.logger.Debug("lease released", "generation", lease.Generation, "owner", lease.Owner) return nil } func (l *Leaser) readLease(ctx context.Context) (*litestream.Lease, string, error) { key := l.lockKey() out, err := l.s3.GetObject(ctx, &s3.GetObjectInput{ Bucket: aws.String(l.Bucket), Key: aws.String(key), }) if err != nil { if isNotExists(err) || isNotFoundError(err) { return nil, "", os.ErrNotExist } return nil, "", fmt.Errorf("get lock file: %w", err) } defer out.Body.Close() var lease litestream.Lease if err := json.NewDecoder(out.Body).Decode(&lease); err != nil { return nil, "", fmt.Errorf("decode lock file: %w", err) } etag := "" if out.ETag != nil { etag = *out.ETag } lease.ETag = etag return &lease, etag, nil } func (l *Leaser) writeLease(ctx context.Context, lease *litestream.Lease, etag string) (string, error) { key := l.lockKey() data, err := json.Marshal(lease) if err != nil { return "", fmt.Errorf("marshal lock file: %w", err) } input := &s3.PutObjectInput{ Bucket: aws.String(l.Bucket), Key: aws.String(key), Body: bytes.NewReader(data), ContentType: aws.String("application/json"), } if etag == "" { input.IfNoneMatch = aws.String("*") } else { input.IfMatch = aws.String(etag) } out, err := l.s3.PutObject(ctx, input) if err != nil { if isPreconditionFailed(err) { return "", &litestream.LeaseExistsError{} } return "", fmt.Errorf("put lock file: %w", err) } newETag := "" if out.ETag != nil { newETag = *out.ETag } return newETag, nil } func isPreconditionFailed(err error) bool { var apiErr smithy.APIError if errors.As(err, &apiErr) { code := apiErr.ErrorCode() return code == "PreconditionFailed" || code == "412" } var respErr *smithy.OperationError if errors.As(err, &respErr) { if httpErr, ok := respErr.Err.(interface{ HTTPStatusCode() int }); ok { return httpErr.HTTPStatusCode() == 412 } } return false } func isNotFoundError(err error) bool { var apiErr smithy.APIError if errors.As(err, &apiErr) { code := apiErr.ErrorCode() return code == "NoSuchKey" || code == "NotFound" || code == "404" } var noSuchKey *types.NoSuchKey if errors.As(err, &noSuchKey) { return true } var respErr *smithy.OperationError if errors.As(err, &respErr) { if httpErr, ok := respErr.Err.(interface{ HTTPStatusCode() int }); ok { return httpErr.HTTPStatusCode() == 404 } } return false } ================================================ FILE: s3/leaser_test.go ================================================ package s3 import ( "context" "encoding/json" "errors" "io" "log/slog" "net/http" "net/http/httptest" "sync" "sync/atomic" "testing" "time" "github.com/benbjohnson/litestream" ) func TestLeaser_AcquireLease_NewLease(t *testing.T) { var putCalled atomic.Bool var receivedIfNoneMatch string currentETag := `"etag-1"` server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: w.WriteHeader(http.StatusNotFound) case http.MethodPut: putCalled.Store(true) receivedIfNoneMatch = r.Header.Get("If-None-Match") body, _ := io.ReadAll(r.Body) r.Body.Close() var lease litestream.Lease if err := json.Unmarshal(body, &lease); err != nil { t.Errorf("failed to unmarshal lock file: %v", err) } if lease.Generation != 1 { t.Errorf("expected generation=1, got %d", lease.Generation) } w.Header().Set("ETag", currentETag) w.WriteHeader(http.StatusOK) default: t.Errorf("unexpected method: %s", r.Method) } })) defer server.Close() leaser := newTestLeaser(t, server.URL) leaser.TTL = 10 * time.Second ctx := context.Background() lease, err := leaser.AcquireLease(ctx) if err != nil { t.Fatalf("AcquireLease() error: %v", err) } if !putCalled.Load() { t.Error("expected PUT to be called") } if receivedIfNoneMatch != "*" { t.Errorf("expected If-None-Match: *, got %q", receivedIfNoneMatch) } if lease.Generation != 1 { t.Errorf("expected generation=1, got %d", lease.Generation) } if lease.ETag != currentETag { t.Errorf("expected ETag=%q, got %q", currentETag, lease.ETag) } if lease.TTL() < 9*time.Second || lease.TTL() > 10*time.Second { t.Errorf("unexpected TTL: %v", lease.TTL()) } } func TestLeaser_AcquireLease_ExpiredLease(t *testing.T) { oldETag := `"old-etag"` newETag := `"new-etag"` var receivedIfMatch string expiredLease := litestream.Lease{ Generation: 5, ExpiresAt: time.Now().Add(-1 * time.Hour), Owner: "previous-owner", } expiredData, _ := json.Marshal(expiredLease) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: w.Header().Set("ETag", oldETag) w.WriteHeader(http.StatusOK) w.Write(expiredData) case http.MethodPut: receivedIfMatch = r.Header.Get("If-Match") w.Header().Set("ETag", newETag) w.WriteHeader(http.StatusOK) } })) defer server.Close() leaser := newTestLeaser(t, server.URL) leaser.TTL = 30 * time.Second ctx := context.Background() lease, err := leaser.AcquireLease(ctx) if err != nil { t.Fatalf("AcquireLease() error: %v", err) } if receivedIfMatch != oldETag { t.Errorf("expected If-Match=%q, got %q", oldETag, receivedIfMatch) } if lease.Generation != 6 { t.Errorf("expected generation=6 (previous+1), got %d", lease.Generation) } if lease.ETag != newETag { t.Errorf("expected ETag=%q, got %q", newETag, lease.ETag) } } func TestLeaser_AcquireLease_ActiveLease(t *testing.T) { activeLease := litestream.Lease{ Generation: 3, ExpiresAt: time.Now().Add(5 * time.Minute), Owner: "active-owner", } activeData, _ := json.Marshal(activeLease) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: w.Header().Set("ETag", `"active-etag"`) w.WriteHeader(http.StatusOK) w.Write(activeData) case http.MethodPut: t.Error("PUT should not be called when lease is active") } })) defer server.Close() leaser := newTestLeaser(t, server.URL) ctx := context.Background() _, err := leaser.AcquireLease(ctx) if err == nil { t.Fatal("expected error for active lease") } var leaseErr *litestream.LeaseExistsError if !errors.As(err, &leaseErr) { t.Fatalf("expected *LeaseExistsError, got %T: %v", err, err) } if leaseErr.Owner != "active-owner" { t.Errorf("expected Owner=%q, got %q", "active-owner", leaseErr.Owner) } if leaseErr.ExpiresAt.IsZero() { t.Error("expected non-zero ExpiresAt") } if leaseErr.ExpiresAt.Before(time.Now()) { t.Errorf("expected future ExpiresAt, got %v", leaseErr.ExpiresAt) } } func TestLeaser_AcquireLease_RaceCondition412(t *testing.T) { var getCalls atomic.Int32 winnerLease := litestream.Lease{ Generation: 1, ExpiresAt: time.Now().Add(30 * time.Second), Owner: "race-winner", } winnerData, _ := json.Marshal(winnerLease) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: calls := getCalls.Add(1) if calls == 1 { w.WriteHeader(http.StatusNotFound) } else { w.Header().Set("ETag", `"winner-etag"`) w.WriteHeader(http.StatusOK) w.Write(winnerData) } case http.MethodPut: w.WriteHeader(http.StatusPreconditionFailed) } })) defer server.Close() leaser := newTestLeaser(t, server.URL) ctx := context.Background() _, err := leaser.AcquireLease(ctx) if err == nil { t.Fatal("expected error for 412 response") } var leaseErr *litestream.LeaseExistsError if !errors.As(err, &leaseErr) { t.Fatalf("expected *LeaseExistsError, got %T: %v", err, err) } if leaseErr.Owner != "race-winner" { t.Errorf("expected Owner=%q, got %q", "race-winner", leaseErr.Owner) } if leaseErr.ExpiresAt.IsZero() { t.Error("expected non-zero ExpiresAt after re-read") } } func TestLeaser_RenewLease(t *testing.T) { oldETag := `"old-etag"` newETag := `"new-etag"` var receivedIfMatch string server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPut { receivedIfMatch = r.Header.Get("If-Match") body, _ := io.ReadAll(r.Body) r.Body.Close() var lease litestream.Lease if err := json.Unmarshal(body, &lease); err != nil { t.Errorf("failed to unmarshal: %v", err) } if lease.Generation != 5 { t.Errorf("expected generation=5, got %d", lease.Generation) } w.Header().Set("ETag", newETag) w.WriteHeader(http.StatusOK) } })) defer server.Close() leaser := newTestLeaser(t, server.URL) leaser.TTL = 30 * time.Second ctx := context.Background() oldLease := &litestream.Lease{ Generation: 5, ExpiresAt: time.Now().Add(5 * time.Second), Owner: "me", ETag: oldETag, } newLease, err := leaser.RenewLease(ctx, oldLease) if err != nil { t.Fatalf("RenewLease() error: %v", err) } if receivedIfMatch != oldETag { t.Errorf("expected If-Match=%q, got %q", oldETag, receivedIfMatch) } if newLease.Generation != 5 { t.Errorf("expected generation=5, got %d", newLease.Generation) } if newLease.ETag != newETag { t.Errorf("expected ETag=%q, got %q", newETag, newLease.ETag) } if newLease.TTL() < 29*time.Second { t.Errorf("expected TTL ~30s, got %v", newLease.TTL()) } } func TestLeaser_RenewLease_LostLease(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPut { w.WriteHeader(http.StatusPreconditionFailed) } })) defer server.Close() leaser := newTestLeaser(t, server.URL) ctx := context.Background() oldLease := &litestream.Lease{ Generation: 5, ExpiresAt: time.Now().Add(5 * time.Second), Owner: "me", ETag: `"stale-etag"`, } _, err := leaser.RenewLease(ctx, oldLease) if err != litestream.ErrLeaseNotHeld { t.Errorf("expected ErrLeaseNotHeld, got %v", err) } } func TestLeaser_RenewLease_NilLease(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { t.Error("no request should be made for nil lease") })) defer server.Close() leaser := newTestLeaser(t, server.URL) ctx := context.Background() _, err := leaser.RenewLease(ctx, nil) if !errors.Is(err, ErrLeaseRequired) { t.Errorf("expected ErrLeaseRequired for nil lease, got %v", err) } } func TestLeaser_RenewLease_EmptyETag(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { t.Error("no request should be made for empty ETag") })) defer server.Close() leaser := newTestLeaser(t, server.URL) ctx := context.Background() lease := &litestream.Lease{ Generation: 5, ExpiresAt: time.Now().Add(5 * time.Second), Owner: "me", ETag: "", } _, err := leaser.RenewLease(ctx, lease) if !errors.Is(err, ErrLeaseETagRequired) { t.Errorf("expected ErrLeaseETagRequired for empty ETag, got %v", err) } } func TestLeaser_ReleaseLease(t *testing.T) { var deleteCalled atomic.Bool var receivedIfMatch string server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodDelete { deleteCalled.Store(true) receivedIfMatch = r.Header.Get("If-Match") w.WriteHeader(http.StatusNoContent) } })) defer server.Close() leaser := newTestLeaser(t, server.URL) ctx := context.Background() lease := &litestream.Lease{ Generation: 5, ExpiresAt: time.Now().Add(5 * time.Minute), Owner: "me", ETag: `"my-etag"`, } err := leaser.ReleaseLease(ctx, lease) if err != nil { t.Fatalf("ReleaseLease() error: %v", err) } if !deleteCalled.Load() { t.Error("expected DELETE to be called") } if receivedIfMatch != `"my-etag"` { t.Errorf("expected If-Match=%q, got %q", `"my-etag"`, receivedIfMatch) } } func TestLeaser_ReleaseLease_StaleETag(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodDelete { w.WriteHeader(http.StatusPreconditionFailed) } })) defer server.Close() leaser := newTestLeaser(t, server.URL) ctx := context.Background() lease := &litestream.Lease{ Generation: 5, ExpiresAt: time.Now().Add(5 * time.Minute), Owner: "me", ETag: `"stale-etag"`, } err := leaser.ReleaseLease(ctx, lease) if err != litestream.ErrLeaseNotHeld { t.Errorf("expected ErrLeaseNotHeld for stale ETag, got %v", err) } } func TestLeaser_ReleaseLease_AlreadyDeleted(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodDelete { w.WriteHeader(http.StatusNotFound) } })) defer server.Close() leaser := newTestLeaser(t, server.URL) ctx := context.Background() lease := &litestream.Lease{ Generation: 5, ExpiresAt: time.Now().Add(5 * time.Minute), Owner: "me", ETag: `"my-etag"`, } err := leaser.ReleaseLease(ctx, lease) if !errors.Is(err, ErrLeaseAlreadyReleased) { t.Fatalf("expected ErrLeaseAlreadyReleased, got %v", err) } } func TestLeaser_ReleaseLease_NilLease(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { t.Error("no request should be made for nil lease") })) defer server.Close() leaser := newTestLeaser(t, server.URL) ctx := context.Background() err := leaser.ReleaseLease(ctx, nil) if !errors.Is(err, ErrLeaseRequired) { t.Errorf("expected ErrLeaseRequired for nil lease, got %v", err) } } func TestLeaser_ReleaseLease_EmptyETag(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { t.Error("no request should be made for empty ETag") })) defer server.Close() leaser := newTestLeaser(t, server.URL) ctx := context.Background() lease := &litestream.Lease{ Generation: 5, ExpiresAt: time.Now().Add(5 * time.Minute), Owner: "me", ETag: "", } err := leaser.ReleaseLease(ctx, lease) if !errors.Is(err, ErrLeaseETagRequired) { t.Errorf("expected ErrLeaseETagRequired for empty ETag, got %v", err) } } func TestLeaser_ConcurrentAcquisition(t *testing.T) { var mu sync.Mutex var leaseHolder string currentETag := "" etagCounter := 0 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { mu.Lock() defer mu.Unlock() switch r.Method { case http.MethodGet: if leaseHolder == "" { w.WriteHeader(http.StatusNotFound) } else { lease := litestream.Lease{ Generation: 1, ExpiresAt: time.Now().Add(30 * time.Second), Owner: leaseHolder, } data, _ := json.Marshal(lease) w.Header().Set("ETag", currentETag) w.WriteHeader(http.StatusOK) w.Write(data) } case http.MethodPut: ifNoneMatch := r.Header.Get("If-None-Match") ifMatch := r.Header.Get("If-Match") if ifNoneMatch == "*" && leaseHolder != "" { w.WriteHeader(http.StatusPreconditionFailed) return } if ifMatch != "" && ifMatch != currentETag { w.WriteHeader(http.StatusPreconditionFailed) return } body, _ := io.ReadAll(r.Body) r.Body.Close() var lease litestream.Lease json.Unmarshal(body, &lease) leaseHolder = lease.Owner etagCounter++ currentETag = `"etag-` + string(rune('0'+etagCounter)) + `"` w.Header().Set("ETag", currentETag) w.WriteHeader(http.StatusOK) } })) defer server.Close() const numClients = 10 var wg sync.WaitGroup var successCount atomic.Int32 var failCount atomic.Int32 for i := 0; i < numClients; i++ { wg.Add(1) go func(id int) { defer wg.Done() leaser := newTestLeaser(t, server.URL) leaser.Owner = string(rune('A' + id)) ctx := context.Background() _, err := leaser.AcquireLease(ctx) if err == nil { successCount.Add(1) } else { failCount.Add(1) } }(i) } wg.Wait() if successCount.Load() != 1 { t.Errorf("expected exactly 1 successful acquisition, got %d", successCount.Load()) } if failCount.Load() != numClients-1 { t.Errorf("expected %d failures, got %d", numClients-1, failCount.Load()) } } func TestLeaser_LockKey(t *testing.T) { tests := []struct { name string path string wantKey string }{ { name: "EmptyPath", path: "", wantKey: "lock.json", }, { name: "SimplePath", path: "replica", wantKey: "replica/lock.json", }, { name: "NestedPath", path: "my/db/replica", wantKey: "my/db/replica/lock.json", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { leaser := NewLeaser() leaser.Path = tt.path if got := leaser.lockKey(); got != tt.wantKey { t.Errorf("lockKey() = %q, want %q", got, tt.wantKey) } }) } } func TestLeaser_Type(t *testing.T) { leaser := NewLeaser() if got := leaser.Type(); got != "s3" { t.Errorf("Type() = %q, want %q", got, "s3") } } func newTestLeaser(t *testing.T, serverURL string) *Leaser { t.Helper() client := NewReplicaClient() client.Bucket = "test-bucket" client.Path = "test-path" client.Region = "us-east-1" client.Endpoint = serverURL client.ForcePathStyle = true client.AccessKeyID = "test-access-key" client.SecretAccessKey = "test-secret-key" ctx := context.Background() if err := client.Init(ctx); err != nil { t.Fatalf("Init() error: %v", err) } leaser := NewLeaser() leaser.Bucket = client.Bucket leaser.Path = client.Path leaser.SetClient(client.s3) leaser.logger = slog.New(slog.NewTextHandler(io.Discard, nil)) return leaser } ================================================ FILE: s3/replica_client.go ================================================ package s3 import ( "bytes" "context" "crypto/md5" "crypto/tls" "encoding/base64" "errors" "fmt" "io" "log/slog" "net" "net/http" "net/url" "os" "path" "regexp" "slices" "strconv" "strings" "sync" "time" "github.com/aws/aws-sdk-go-v2/aws" v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/credentials" "github.com/aws/aws-sdk-go-v2/feature/s3/manager" "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3/types" "github.com/aws/smithy-go" smithyxml "github.com/aws/smithy-go/encoding/xml" "github.com/aws/smithy-go/middleware" smithytime "github.com/aws/smithy-go/time" smithyhttp "github.com/aws/smithy-go/transport/http" "github.com/superfly/ltx" "golang.org/x/sync/errgroup" "golang.org/x/sync/semaphore" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/internal" ) func init() { litestream.RegisterReplicaClientFactory("s3", NewReplicaClientFromURL) } // ReplicaClientType is the client type for this package. const ReplicaClientType = "s3" // MetadataKeyTimestamp is the metadata key for storing LTX file timestamps in S3. const MetadataKeyTimestamp = "litestream-timestamp" // MaxKeys is the number of keys S3 can operate on per batch. const MaxKeys = 1000 // DefaultRegion is the region used if one is not specified. const DefaultRegion = "us-east-1" // DefaultMetadataConcurrency is the default number of concurrent HeadObject calls // for fetching accurate timestamps during timestamp-based restore. // S3 can handle 5,500+ HEAD requests per second per prefix. const DefaultMetadataConcurrency = 50 // DefaultR2Concurrency is the default number of concurrent multipart upload // parts for Cloudflare R2, which has strict concurrent upload limits. const DefaultR2Concurrency = 2 // contentMD5StackKey is used to pass the precomputed Content-MD5 checksum // through the middleware stack from Serialize to Finalize phase. type contentMD5StackKey struct{} var _ litestream.ReplicaClient = (*ReplicaClient)(nil) var _ litestream.ReplicaClientV3 = (*ReplicaClient)(nil) // ReplicaClient is a client for writing LTX files to S3. type ReplicaClient struct { mu sync.Mutex s3 *s3.Client // s3 service uploader *manager.Uploader logger *slog.Logger // AWS authentication keys. AccessKeyID string SecretAccessKey string // S3 bucket information Region string Bucket string Path string Endpoint string ForcePathStyle bool SkipVerify bool SignPayload bool RequireContentMD5 bool // Upload configuration PartSize int64 // Part size for multipart uploads (default: 5MB) Concurrency int // Number of concurrent parts to upload (default: 5) // MetadataConcurrency controls parallel HeadObject calls for timestamp-based restore. // Higher values improve restore speed for large backup histories. // Default: 50 (S3 can handle 5,500+ HEAD/s per prefix) MetadataConcurrency int // Server-Side Encryption - Customer Provided Keys (SSE-C) // Works with all S3-compatible providers (AWS, MinIO, Exoscale, etc.) SSECustomerAlgorithm string // Must be "AES256" if set SSECustomerKey string // Base64-encoded 256-bit (32 byte) encryption key SSECustomerKeyMD5 string // Base64-encoded MD5 of key (auto-computed if not set) // Server-Side Encryption - AWS KMS (SSE-KMS) // Only works with AWS S3 (not S3-compatible providers) SSEKMSKeyID string // KMS key ID, ARN, or alias } // NewReplicaClient returns a new instance of ReplicaClient. func NewReplicaClient() *ReplicaClient { return &ReplicaClient{ logger: slog.Default().WithGroup(ReplicaClientType), RequireContentMD5: true, SignPayload: true, } } func (c *ReplicaClient) SetLogger(logger *slog.Logger) { c.logger = logger.WithGroup(ReplicaClientType) } // NewReplicaClientFromURL creates a new ReplicaClient from URL components. // This is used by the replica client factory registration. func NewReplicaClientFromURL(scheme, host, urlPath string, query url.Values, userinfo *url.Userinfo) (litestream.ReplicaClient, error) { client := NewReplicaClient() var ( bucket string region string endpoint string forcePathStyle bool skipVerify bool signPayload bool signPayloadSet bool requireMD5 bool requireMD5Set bool concurrency int concurrencySet bool partSize int64 ) // Parse host for bucket and region if strings.HasPrefix(host, "arn:") { bucket = host region = litestream.RegionFromS3ARN(host) } else { bucket, region, endpoint, forcePathStyle = ParseHost(host) } // Override with query parameters if provided if qEndpoint := query.Get("endpoint"); qEndpoint != "" { // Ensure endpoint has a scheme (defaults to https:// for cloud, http:// for local) qEndpoint, _ = litestream.EnsureEndpointScheme(qEndpoint) endpoint = qEndpoint // Default to path style for custom endpoints unless explicitly set to false if v, ok := litestream.BoolQueryValue(query, "forcePathStyle", "force-path-style"); !ok || v { forcePathStyle = true } } if qRegion := query.Get("region"); qRegion != "" { region = qRegion } if v, ok := litestream.BoolQueryValue(query, "forcePathStyle", "force-path-style"); ok { forcePathStyle = v } if v, ok := litestream.BoolQueryValue(query, "skipVerify", "skip-verify"); ok { skipVerify = v } if v, ok := litestream.BoolQueryValue(query, "signPayload", "sign-payload"); ok { signPayload = v signPayloadSet = true } if v, ok := litestream.BoolQueryValue(query, "requireContentMD5", "require-content-md5"); ok { requireMD5 = v requireMD5Set = true } if v := query.Get("concurrency"); v != "" { if n, err := strconv.Atoi(v); err == nil && n > 0 { concurrency = n concurrencySet = true } } if v := query.Get("partSize"); v != "" { if n, err := strconv.ParseInt(v, 10, 64); err == nil && n > 0 { partSize = n } } else if v := query.Get("part-size"); v != "" { if n, err := strconv.ParseInt(v, 10, 64); err == nil && n > 0 { partSize = n } } // Ensure bucket is set if bucket == "" { return nil, fmt.Errorf("bucket required for s3 replica URL") } // Track if forcePathStyle was explicitly set via query parameter. forcePathStyleSet := query.Get("forcePathStyle") != "" || query.Get("force-path-style") != "" // Read authentication from environment variables if v := os.Getenv("AWS_ACCESS_KEY_ID"); v != "" { client.AccessKeyID = v } else if v := os.Getenv("LITESTREAM_ACCESS_KEY_ID"); v != "" { client.AccessKeyID = v } if v := os.Getenv("AWS_SECRET_ACCESS_KEY"); v != "" { client.SecretAccessKey = v } else if v := os.Getenv("LITESTREAM_SECRET_ACCESS_KEY"); v != "" { client.SecretAccessKey = v } if endpoint == "" { if v := os.Getenv("LITESTREAM_S3_ENDPOINT"); v != "" { endpoint, _ = litestream.EnsureEndpointScheme(v) if !forcePathStyleSet { forcePathStyle = true } } } // Detect S3-compatible provider endpoints for applying appropriate defaults. isHetzner := litestream.IsHetznerEndpoint(endpoint) isTigris := litestream.IsTigrisEndpoint(endpoint) isDigitalOcean := litestream.IsDigitalOceanEndpoint(endpoint) isBackblaze := litestream.IsBackblazeEndpoint(endpoint) isFilebase := litestream.IsFilebaseEndpoint(endpoint) isScaleway := litestream.IsScalewayEndpoint(endpoint) isCloudflareR2 := litestream.IsCloudflareR2Endpoint(endpoint) isMinIO := litestream.IsMinIOEndpoint(endpoint) isSupabase := litestream.IsSupabaseEndpoint(endpoint) // Apply provider-specific defaults for S3-compatible providers. if isTigris { // Tigris: requires signed payloads, no MD5 if !signPayloadSet { signPayload, signPayloadSet = true, true } if !requireMD5Set { requireMD5, requireMD5Set = false, true } } if isHetzner || isDigitalOcean || isBackblaze || isFilebase || isScaleway || isCloudflareR2 || isMinIO || isSupabase { // All these providers require signed payloads (don't support UNSIGNED-PAYLOAD) if !signPayloadSet { signPayload, signPayloadSet = true, true } } if !forcePathStyleSet { // Filebase, Backblaze B2, MinIO, and Supabase require path-style URLs if isFilebase || isBackblaze || isMinIO || isSupabase { forcePathStyle = true } } if isCloudflareR2 { client.Concurrency = DefaultR2Concurrency } // Configure client client.Bucket = bucket client.Path = urlPath client.Region = region client.Endpoint = endpoint client.ForcePathStyle = forcePathStyle client.SkipVerify = skipVerify if signPayloadSet { client.SignPayload = signPayload } if requireMD5Set { client.RequireContentMD5 = requireMD5 } if concurrencySet { client.Concurrency = concurrency } if partSize > 0 { client.PartSize = partSize } // Parse SSE-C parameters from query string if v := query.Get("sseCustomerAlgorithm"); v != "" { client.SSECustomerAlgorithm = v } else if v := query.Get("sse-customer-algorithm"); v != "" { client.SSECustomerAlgorithm = v } if v := query.Get("sseCustomerKey"); v != "" { client.SSECustomerKey = v } else if v := query.Get("sse-customer-key"); v != "" { client.SSECustomerKey = v } if v := query.Get("sseCustomerKeyMD5"); v != "" { client.SSECustomerKeyMD5 = v } else if v := query.Get("sse-customer-key-md5"); v != "" { client.SSECustomerKeyMD5 = v } // Parse SSE-KMS parameters from query string if v := query.Get("sseKmsKeyId"); v != "" { client.SSEKMSKeyID = v } else if v := query.Get("sse-kms-key-id"); v != "" { client.SSEKMSKeyID = v } return client, nil } // Type returns "s3" as the client type. func (c *ReplicaClient) Type() string { return ReplicaClientType } // Init initializes the connection to S3. No-op if already initialized. func (c *ReplicaClient) Init(ctx context.Context) (err error) { c.mu.Lock() defer c.mu.Unlock() if c.s3 != nil { return nil } // Validate required configuration if c.Bucket == "" { return fmt.Errorf("s3: bucket name is required") } // Validate SSE configuration if err := c.validateSSEConfig(); err != nil { return err } // Look up region if not specified and no endpoint is used. // Endpoints are typically used for non-S3 object stores and do not // necessarily require a region. region := c.Region if region == "" { if c.Endpoint == "" { if region, err = c.findBucketRegion(ctx, c.Bucket); err != nil { return fmt.Errorf("s3: cannot lookup bucket region: %w", err) } } else { region = DefaultRegion // default for non-S3 object stores } } // Create HTTP client with 24 hour timeout for long-running operations httpClient := &http.Client{ Timeout: 24 * time.Hour, } // Always configure custom HTTP Transport with controlled keepalive settings // to reduce idle CPU usage from default transport's aggressive keepalives. // See: https://github.com/benbjohnson/litestream/issues/992 httpClient.Transport = &http.Transport{ Proxy: http.ProxyFromEnvironment, DialContext: (&net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, }).DialContext, ForceAttemptHTTP2: true, MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second, } // Configure TLS to skip verification if requested if c.SkipVerify { httpClient.Transport.(*http.Transport).TLSClientConfig = &tls.Config{ InsecureSkipVerify: true, } } // Build configuration options configOpts := []func(*config.LoadOptions) error{ config.WithRegion(region), // Use adaptive retry mode for better resilience with 24 hour timeout // This matches Azure's approach for long-running operations config.WithRetryMode(aws.RetryModeAdaptive), config.WithRetryMaxAttempts(10), // Increase retry attempts for resilience } // Add HTTP client with proper timeout configOpts = append(configOpts, config.WithHTTPClient(httpClient)) // Add static credentials if provided, otherwise use default credential chain // Default credential chain includes: // - Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) // - Shared credentials file (~/.aws/credentials) // - EC2 Instance Profile credentials // - ECS Task Role credentials // - Web Identity Token credentials (for EKS) if c.AccessKeyID != "" && c.SecretAccessKey != "" { configOpts = append(configOpts, config.WithCredentialsProvider( credentials.NewStaticCredentialsProvider(c.AccessKeyID, c.SecretAccessKey, ""), )) } // Enable AWS SDK debug logging if LITESTREAM_S3_DEBUG is set. // Useful for debugging S3-compatible providers (signing issues, request/response bodies). // Supports comma-separated values: signing,request,retries // Values: signing, request, request-with-body, response, response-with-body, retries, all if logMode := parseS3DebugEnv(); logMode != 0 { configOpts = append(configOpts, config.WithClientLogMode(logMode)) } // Load AWS configuration cfg, err := config.LoadDefaultConfig(ctx, configOpts...) if err != nil { return fmt.Errorf("s3: cannot load aws config: %w", err) } // Create S3 client options s3Opts := []func(*s3.Options){ func(o *s3.Options) { o.UsePathStyle = c.ForcePathStyle o.UseARNRegion = true // Add User-Agent and optional middleware. o.APIOptions = append(o.APIOptions, c.middlewareOption()) }, } // S3-compatible providers (Tigris, Backblaze B2, MinIO, Filebase, etc.) don't // support aws-chunked content encoding used by default checksum calculation // in AWS SDK Go v2 v1.73.0+. Disable automatic checksum calculation and // response checksum validation for all custom endpoints. // See: https://github.com/benbjohnson/litestream/issues/918 // See: https://github.com/benbjohnson/litestream/issues/947 if c.Endpoint != "" { s3Opts = append(s3Opts, func(o *s3.Options) { o.RequestChecksumCalculation = aws.RequestChecksumCalculationWhenRequired o.ResponseChecksumValidation = aws.ResponseChecksumValidationWhenRequired }) } // Add custom endpoint if specified c.configureEndpoint(&s3Opts) // Create S3 client c.s3 = s3.NewFromConfig(cfg, s3Opts...) // Configure uploader with custom options if specified uploaderOpts := []func(*manager.Uploader){} // For S3-compatible providers, disable automatic checksum calculation on the Uploader. // The S3 client's RequestChecksumCalculation setting only affects single-part uploads. // Multipart uploads via the Uploader require this separate setting (added in s3/manager v1.20.0). // See: https://github.com/benbjohnson/litestream/issues/948 // See: https://github.com/aws/aws-sdk-go-v2/issues/3007 if c.Endpoint != "" { uploaderOpts = append(uploaderOpts, func(u *manager.Uploader) { u.RequestChecksumCalculation = aws.RequestChecksumCalculationWhenRequired }) } if c.PartSize > 0 { uploaderOpts = append(uploaderOpts, func(u *manager.Uploader) { u.PartSize = c.PartSize }) } if c.Concurrency > 0 { uploaderOpts = append(uploaderOpts, func(u *manager.Uploader) { u.Concurrency = c.Concurrency }) } c.uploader = manager.NewUploader(c.s3, uploaderOpts...) return nil } // configureEndpoint adds custom endpoint configuration to S3 client options if needed. func (c *ReplicaClient) configureEndpoint(opts *[]func(*s3.Options)) { if c.Endpoint != "" { *opts = append(*opts, func(o *s3.Options) { o.UsePathStyle = c.ForcePathStyle endpoint := c.Endpoint // Add scheme if not present if !strings.HasPrefix(endpoint, "http://") && !strings.HasPrefix(endpoint, "https://") { endpoint = "https://" + endpoint } o.BaseEndpoint = aws.String(endpoint) // For MinIO and other S3-compatible services if strings.HasPrefix(endpoint, "http://") { o.EndpointOptions.DisableHTTPS = true } }) } } // validateSSEConfig validates server-side encryption configuration. func (c *ReplicaClient) validateSSEConfig() error { // Check mutual exclusivity: SSE-C and SSE-KMS cannot both be set if c.SSECustomerKey != "" && c.SSEKMSKeyID != "" { return fmt.Errorf("s3: cannot use both sse-customer-key and sse-kms-key-id; they are mutually exclusive") } // Validate SSE-C configuration if c.SSECustomerKey != "" { // Algorithm must be AES256 (or default to it) if c.SSECustomerAlgorithm == "" { c.SSECustomerAlgorithm = "AES256" } else if c.SSECustomerAlgorithm != "AES256" { return fmt.Errorf("s3: sse-customer-algorithm must be AES256, got %q", c.SSECustomerAlgorithm) } // Validate key is valid base64 and correct length (256 bits = 32 bytes) keyBytes, err := base64.StdEncoding.DecodeString(c.SSECustomerKey) if err != nil { return fmt.Errorf("s3: sse-customer-key must be valid base64: %w", err) } if len(keyBytes) != 32 { return fmt.Errorf("s3: sse-customer-key must be 256-bit (32 bytes) when decoded, got %d bytes", len(keyBytes)) } // Auto-compute MD5 if not provided if c.SSECustomerKeyMD5 == "" { sum := md5.Sum(keyBytes) c.SSECustomerKeyMD5 = base64.StdEncoding.EncodeToString(sum[:]) } // SSE-C requires HTTPS (except for localhost/private networks for testing) if c.Endpoint != "" { endpoint := c.Endpoint if !strings.HasPrefix(endpoint, "http://") && !strings.HasPrefix(endpoint, "https://") { endpoint = "https://" + endpoint } if strings.HasPrefix(endpoint, "http://") { u, err := url.Parse(endpoint) if err == nil { host := u.Hostname() // Allow localhost by name if host == "localhost" { // OK - localhost is allowed } else if ip := net.ParseIP(host); ip != nil && (ip.IsLoopback() || ip.IsPrivate()) { // OK - loopback (127.x.x.x) or private RFC1918 ranges (10.x, 172.16-31.x, 192.168.x) } else { return fmt.Errorf("s3: sse-customer-key requires HTTPS endpoint (HTTP only allowed for localhost/private networks)") } } } } } return nil } // findBucketRegion looks up the AWS region for a bucket. Returns blank if non-S3. func (c *ReplicaClient) findBucketRegion(ctx context.Context, bucket string) (string, error) { // Build a config with credentials but no region configOpts := []func(*config.LoadOptions) error{} // Add static credentials if provided if c.AccessKeyID != "" && c.SecretAccessKey != "" { configOpts = append(configOpts, config.WithCredentialsProvider( credentials.NewStaticCredentialsProvider(c.AccessKeyID, c.SecretAccessKey, ""), )) } // Load AWS configuration cfg, err := config.LoadDefaultConfig(ctx, configOpts...) if err != nil { return "", fmt.Errorf("s3: cannot load aws config for region lookup: %w", err) } // Use default region for initial region lookup cfg.Region = DefaultRegion // Create S3 client options s3Opts := []func(*s3.Options){} // Configure custom endpoint for region lookup c.configureEndpoint(&s3Opts) client := s3.NewFromConfig(cfg, s3Opts...) // Get bucket location out, err := client.GetBucketLocation(ctx, &s3.GetBucketLocationInput{ Bucket: aws.String(bucket), }) if err != nil { return "", err } // Convert location constraint to region if out.LocationConstraint == "" { return DefaultRegion, nil } return string(out.LocationConstraint), nil } // LTXFiles returns an iterator over all LTX files on the replica for the given level. // When useMetadata is true, fetches accurate timestamps from S3 metadata via HeadObject. // This uses parallel batched requests (controlled by MetadataConcurrency) to avoid hangs // with large backup histories (see issue #930). // When false, uses fast LastModified timestamps from LIST operation. func (c *ReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { if err := c.Init(ctx); err != nil { return nil, err } return newFileIterator(ctx, c, level, seek, useMetadata), nil } // OpenLTXFile returns a reader for an LTX file // Returns os.ErrNotExist if no matching index/offset is found. func (c *ReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { if err := c.Init(ctx); err != nil { return nil, err } var rangeStr string if size > 0 { rangeStr = fmt.Sprintf("bytes=%d-%d", offset, offset+size-1) } else { rangeStr = fmt.Sprintf("bytes=%d-", offset) } // Build the key from the file info filename := ltx.FormatFilename(minTXID, maxTXID) key := c.Path + "/" + fmt.Sprintf("%04x/%s", level, filename) input := &s3.GetObjectInput{ Bucket: aws.String(c.Bucket), Key: aws.String(key), Range: aws.String(rangeStr), } // Add SSE-C parameters if configured (required for reading SSE-C encrypted objects) // Note: SSE-KMS does not require parameters on read - decryption is automatic if c.SSECustomerKey != "" { input.SSECustomerAlgorithm = aws.String(c.SSECustomerAlgorithm) input.SSECustomerKey = aws.String(c.SSECustomerKey) input.SSECustomerKeyMD5 = aws.String(c.SSECustomerKeyMD5) } out, err := c.s3.GetObject(ctx, input) if err != nil { if isNotExists(err) { return nil, os.ErrNotExist } return nil, fmt.Errorf("s3: get object %s: %w", key, err) } return out.Body, nil } // WriteLTXFile writes an LTX file to the replica. // Extracts timestamp from LTX header and stores it in S3 metadata to preserve original creation time. func (c *ReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { if err := c.Init(ctx); err != nil { return nil, err } // Use TeeReader to peek at LTX header while preserving data for upload var buf bytes.Buffer teeReader := io.TeeReader(r, &buf) // Extract timestamp from LTX header hdr, _, err := ltx.PeekHeader(teeReader) if err != nil { return nil, fmt.Errorf("extract timestamp from LTX header: %w", err) } timestamp := time.UnixMilli(hdr.Timestamp).UTC() // Combine buffered data with rest of reader rc := internal.NewReadCounter(io.MultiReader(&buf, r)) filename := ltx.FormatFilename(minTXID, maxTXID) key := c.Path + "/" + fmt.Sprintf("%04x/%s", level, filename) // Store timestamp in S3 metadata for accurate timestamp retrieval metadata := map[string]string{ MetadataKeyTimestamp: timestamp.Format(time.RFC3339Nano), } input := &s3.PutObjectInput{ Bucket: aws.String(c.Bucket), Key: aws.String(key), Body: rc, Metadata: metadata, } // Add SSE-C parameters if configured if c.SSECustomerKey != "" { input.SSECustomerAlgorithm = aws.String(c.SSECustomerAlgorithm) input.SSECustomerKey = aws.String(c.SSECustomerKey) input.SSECustomerKeyMD5 = aws.String(c.SSECustomerKeyMD5) } // Add SSE-KMS parameters if configured if c.SSEKMSKeyID != "" { input.ServerSideEncryption = types.ServerSideEncryptionAwsKms input.SSEKMSKeyId = aws.String(c.SSEKMSKeyID) } out, err := c.uploader.Upload(ctx, input) if err != nil { return nil, fmt.Errorf("s3: upload to %s: %w", key, err) } // Build file info from the uploaded file info := <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Size: rc.N(), CreatedAt: timestamp, } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "PUT").Inc() internal.OperationBytesCounterVec.WithLabelValues(ReplicaClientType, "PUT").Add(float64(rc.N())) // ETag indicates successful upload if out.ETag == nil { return nil, fmt.Errorf("s3: upload failed: no ETag returned") } return info, nil } func (c *ReplicaClient) middlewareOption() func(*middleware.Stack) error { return func(stack *middleware.Stack) error { if c.RequireContentMD5 { if err := stack.Serialize.Add( middleware.SerializeMiddlewareFunc( "LitestreamComputeDeleteContentMD5", func(ctx context.Context, in middleware.SerializeInput, next middleware.SerializeHandler) ( out middleware.SerializeOutput, metadata middleware.Metadata, err error, ) { if middleware.GetOperationName(ctx) != "DeleteObjects" { return next.HandleSerialize(ctx, in) } input, ok := in.Parameters.(*s3.DeleteObjectsInput) if !ok || input == nil || input.Delete == nil || len(input.Delete.Objects) == 0 { return next.HandleSerialize(ctx, in) } checksum, err := computeDeleteObjectsContentMD5(input.Delete) if err != nil { return out, metadata, err } if checksum != "" { ctx = middleware.WithStackValue(ctx, contentMD5StackKey{}, checksum) } return next.HandleSerialize(ctx, in) }, ), middleware.Before, ); err != nil { return err } } if err := stack.Build.Add( middleware.BuildMiddlewareFunc( "LitestreamUserAgent", func(ctx context.Context, in middleware.BuildInput, next middleware.BuildHandler) ( out middleware.BuildOutput, metadata middleware.Metadata, err error, ) { if req, ok := in.Request.(*smithyhttp.Request); ok { current := req.Header.Get("User-Agent") if current == "" { req.Header.Set("User-Agent", "litestream") } else if !strings.Contains(current, "litestream") { req.Header.Set("User-Agent", "litestream "+current) } } return next.HandleBuild(ctx, in) }, ), middleware.After, ); err != nil { return err } if litestream.IsTigrisEndpoint(c.Endpoint) { if err := stack.Build.Add( middleware.BuildMiddlewareFunc( "LitestreamTigrisConsistent", func(ctx context.Context, in middleware.BuildInput, next middleware.BuildHandler) ( out middleware.BuildOutput, metadata middleware.Metadata, err error, ) { if req, ok := in.Request.(*smithyhttp.Request); ok { req.Header.Set("X-Tigris-Consistent", "true") } return next.HandleBuild(ctx, in) }, ), middleware.After, ); err != nil { return err } } // Many S3-compatible providers (e.g. Filebase) do not support SigV4 // payload hashing. Switching to unsigned payload matches the behavior // of the AWS SDK v1 client used in Litestream v0.3.x and restores // compatibility. if !c.SignPayload { _ = v4.RemoveComputePayloadSHA256Middleware(stack) if err := v4.AddUnsignedPayloadMiddleware(stack); err != nil { return err } _ = v4.RemoveContentSHA256HeaderMiddleware(stack) if err := v4.AddContentSHA256HeaderMiddleware(stack); err != nil { return err } } // Disable AWS SDK v2's trailing checksum middleware which uses // aws-chunked encoding. This is required for: // 1. UNSIGNED-PAYLOAD requests (aws-chunked + UNSIGNED-PAYLOAD is rejected by AWS) // 2. S3-compatible providers (Filebase, MinIO, Backblaze B2, etc.) that don't // support aws-chunked encoding at all // See: https://github.com/aws/aws-sdk-go-v2/discussions/2960 // See: https://github.com/benbjohnson/litestream/issues/895 if !c.SignPayload || c.Endpoint != "" { stack.Finalize.Remove("addInputChecksumTrailer") } // Add debug logging middleware at the end of Finalize phase // so all headers (including X-Tigris-Consistent) are set if err := stack.Finalize.Add( middleware.FinalizeMiddlewareFunc( "LitestreamDebugLogging", func(ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) ( out middleware.FinalizeOutput, metadata middleware.Metadata, err error, ) { if req, ok := in.Request.(*smithyhttp.Request); ok { c.logger.Debug("s3 request", "method", req.Method, "url", req.URL.String(), "x-tigris-consistent", req.Header.Get("X-Tigris-Consistent"), ) } return next.HandleFinalize(ctx, in) }, ), middleware.After, ); err != nil { return err } if !c.RequireContentMD5 { return nil } md5Middleware := func() middleware.FinalizeMiddleware { return middleware.FinalizeMiddlewareFunc( "LitestreamDeleteContentMD5", func(ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) ( out middleware.FinalizeOutput, metadata middleware.Metadata, err error, ) { if middleware.GetOperationName(ctx) != "DeleteObjects" { return next.HandleFinalize(ctx, in) } checksum, _ := middleware.GetStackValue(ctx, contentMD5StackKey{}).(string) if checksum == "" { return next.HandleFinalize(ctx, in) } req, ok := in.Request.(*smithyhttp.Request) if !ok { return next.HandleFinalize(ctx, in) } if req.Header.Get("Content-MD5") == "" { req.Header.Set("Content-MD5", checksum) } return next.HandleFinalize(ctx, in) }, ) } // Try to insert before AWS's checksum middleware for optimal ordering. // If that middleware doesn't exist (e.g., different SDK version), add at the end. // Our middleware checks if Content-MD5 is already set, so order is not critical. if err := stack.Finalize.Insert(md5Middleware(), "AWSChecksum:ComputeInputPayloadChecksum", middleware.Before); err != nil { if err := stack.Finalize.Add(md5Middleware(), middleware.After); err != nil { return err } } return nil } } func computeDeleteObjectsContentMD5(deleteInput *types.Delete) (string, error) { if deleteInput == nil { return "", nil } payload, err := marshalDeleteObjects(deleteInput) if err != nil { return "", err } if len(payload) == 0 { return "", nil } sum := md5.Sum(payload) return base64.StdEncoding.EncodeToString(sum[:]), nil } func marshalDeleteObjects(deleteInput *types.Delete) ([]byte, error) { if deleteInput == nil { return nil, nil } var buf bytes.Buffer encoder := smithyxml.NewEncoder(&buf) root := smithyxml.StartElement{ Name: smithyxml.Name{ Local: "Delete", }, Attr: []smithyxml.Attr{ smithyxml.NewNamespaceAttribute("", "http://s3.amazonaws.com/doc/2006-03-01/"), }, } if err := encodeDeleteDocument(deleteInput, encoder.RootElement(root)); err != nil { return nil, err } return encoder.Bytes(), nil } func encodeDeleteDocument(v *types.Delete, value smithyxml.Value) error { defer value.Close() if v.Objects != nil { root := smithyxml.StartElement{ Name: smithyxml.Name{ Local: "Object", }, } el := value.FlattenedElement(root) if err := encodeObjectIdentifierList(v.Objects, el); err != nil { return err } } if v.Quiet != nil { root := smithyxml.StartElement{ Name: smithyxml.Name{ Local: "Quiet", }, } el := value.MemberElement(root) el.Boolean(*v.Quiet) } return nil } func encodeObjectIdentifierList(v []types.ObjectIdentifier, value smithyxml.Value) error { if !value.IsFlattened() { defer value.Close() } array := value.Array() for i := range v { member := array.Member() if err := encodeObjectIdentifier(&v[i], member); err != nil { return err } } return nil } // encodeObjectIdentifier mirrors the AWS SDK's XML serializer for DeleteObjects. // This ensures our precomputed Content-MD5 matches the actual request body. // Includes all ObjectIdentifier fields as of AWS SDK v2 (2024). func encodeObjectIdentifier(v *types.ObjectIdentifier, value smithyxml.Value) error { defer value.Close() if v.ETag != nil { el := value.MemberElement(smithyxml.StartElement{ Name: smithyxml.Name{ Local: "ETag", }, }) el.String(*v.ETag) } if v.Key != nil { el := value.MemberElement(smithyxml.StartElement{ Name: smithyxml.Name{ Local: "Key", }, }) el.String(*v.Key) } if v.LastModifiedTime != nil { el := value.MemberElement(smithyxml.StartElement{ Name: smithyxml.Name{ Local: "LastModifiedTime", }, }) el.String(smithytime.FormatHTTPDate(*v.LastModifiedTime)) } if v.Size != nil { el := value.MemberElement(smithyxml.StartElement{ Name: smithyxml.Name{ Local: "Size", }, }) el.Long(*v.Size) } if v.VersionId != nil { el := value.MemberElement(smithyxml.StartElement{ Name: smithyxml.Name{ Local: "VersionId", }, }) el.String(*v.VersionId) } return nil } // DeleteLTXFiles deletes one or more LTX files. func (c *ReplicaClient) DeleteLTXFiles(ctx context.Context, a []*ltx.FileInfo) error { if err := c.Init(ctx); err != nil { return err } if len(a) == 0 { return nil } // Convert file infos to object identifiers objIDs := make([]types.ObjectIdentifier, 0, len(a)) for _, info := range a { filename := ltx.FormatFilename(info.MinTXID, info.MaxTXID) key := c.Path + "/" + fmt.Sprintf("%04x/%s", info.Level, filename) objIDs = append(objIDs, types.ObjectIdentifier{Key: aws.String(key)}) c.logger.Debug("deleting ltx file", "level", info.Level, "minTXID", info.MinTXID, "maxTXID", info.MaxTXID, "key", key) } // Delete in batches for len(objIDs) > 0 { n := min(len(objIDs), MaxKeys) c.logger.Debug("deleting ltx files batch", "count", n) start := time.Now() out, err := c.s3.DeleteObjects(ctx, &s3.DeleteObjectsInput{ Bucket: aws.String(c.Bucket), Delete: &types.Delete{Objects: objIDs[:n]}, }) duration := time.Since(start) internal.OperationDurationHistogramVec.WithLabelValues(ReplicaClientType, "DELETE").Observe(duration.Seconds()) if err != nil { return fmt.Errorf("s3: delete batch of %d objects: %w", n, err) } deleted := 0 if out != nil { deleted = len(out.Deleted) } c.logger.Debug("delete batch completed", "requested", n, "deleted", deleted, "errors", len(out.Errors), "duration_ms", duration.Milliseconds()) internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "DELETE").Add(float64(deleted)) if len(out.Errors) > 0 { for i, e := range out.Errors { code := aws.ToString(e.Code) internal.OperationErrorCounterVec.WithLabelValues(ReplicaClientType, "DELETE", code).Inc() if i < 5 { c.logger.Warn("delete object failed", "key", aws.ToString(e.Key), "code", code, "message", aws.ToString(e.Message)) } } if len(out.Errors) > 5 { c.logger.Warn("additional delete errors suppressed", "count", len(out.Errors)-5) } } if err := deleteOutputError(out); err != nil { return err } objIDs = objIDs[n:] } return nil } // DeleteAll deletes all files. func (c *ReplicaClient) DeleteAll(ctx context.Context) error { if err := c.Init(ctx); err != nil { return err } var objIDs []types.ObjectIdentifier // Create paginator for listing objects paginator := s3.NewListObjectsV2Paginator(c.s3, &s3.ListObjectsV2Input{ Bucket: aws.String(c.Bucket), Prefix: aws.String(c.Path + "/"), }) // Iterate through all pages for paginator.HasMorePages() { page, err := paginator.NextPage(ctx) if err != nil { return fmt.Errorf("s3: list objects page: %w", err) } // Collect object identifiers for _, obj := range page.Contents { objIDs = append(objIDs, types.ObjectIdentifier{Key: obj.Key}) } } // Delete all collected objects in batches for len(objIDs) > 0 { n := min(len(objIDs), MaxKeys) out, err := c.s3.DeleteObjects(ctx, &s3.DeleteObjectsInput{ Bucket: aws.String(c.Bucket), Delete: &types.Delete{Objects: objIDs[:n], Quiet: aws.Bool(true)}, }) if err != nil { return fmt.Errorf("s3: delete all batch of %d objects: %w", n, err) } else if err := deleteOutputError(out); err != nil { return err } objIDs = objIDs[n:] } return nil } // GenerationsV3 returns a list of v0.3.x generation IDs in the replica. func (c *ReplicaClient) GenerationsV3(ctx context.Context) ([]string, error) { if err := c.Init(ctx); err != nil { return nil, err } prefix := litestream.GenerationsPathV3(c.Path) + "/" // Use CommonPrefixes with delimiter to list "directories" paginator := s3.NewListObjectsV2Paginator(c.s3, &s3.ListObjectsV2Input{ Bucket: aws.String(c.Bucket), Prefix: aws.String(prefix), Delimiter: aws.String("/"), }) var generations []string for paginator.HasMorePages() { page, err := paginator.NextPage(ctx) if err != nil { return nil, fmt.Errorf("s3: list generations: %w", err) } for _, cp := range page.CommonPrefixes { // Extract generation ID from prefix (e.g., "path/generations/abc123def456/" -> "abc123def456") p := aws.ToString(cp.Prefix) p = strings.TrimPrefix(p, prefix) p = strings.TrimSuffix(p, "/") if litestream.IsGenerationIDV3(p) { generations = append(generations, p) } } } slices.Sort(generations) return generations, nil } // SnapshotsV3 returns snapshots for a generation, sorted by index. func (c *ReplicaClient) SnapshotsV3(ctx context.Context, generation string) ([]litestream.SnapshotInfoV3, error) { if err := c.Init(ctx); err != nil { return nil, err } prefix := litestream.SnapshotsPathV3(c.Path, generation) + "/" paginator := s3.NewListObjectsV2Paginator(c.s3, &s3.ListObjectsV2Input{ Bucket: aws.String(c.Bucket), Prefix: aws.String(prefix), }) var snapshots []litestream.SnapshotInfoV3 for paginator.HasMorePages() { page, err := paginator.NextPage(ctx) if err != nil { return nil, fmt.Errorf("s3: list snapshots: %w", err) } for _, obj := range page.Contents { key := path.Base(aws.ToString(obj.Key)) index, err := litestream.ParseSnapshotFilenameV3(key) if err != nil { continue // skip invalid filenames } snapshots = append(snapshots, litestream.SnapshotInfoV3{ Generation: generation, Index: index, Size: aws.ToInt64(obj.Size), CreatedAt: aws.ToTime(obj.LastModified).UTC(), }) } } slices.SortFunc(snapshots, func(a, b litestream.SnapshotInfoV3) int { return a.Index - b.Index }) return snapshots, nil } // WALSegmentsV3 returns WAL segments for a generation, sorted by index then offset. func (c *ReplicaClient) WALSegmentsV3(ctx context.Context, generation string) ([]litestream.WALSegmentInfoV3, error) { if err := c.Init(ctx); err != nil { return nil, err } prefix := litestream.WALPathV3(c.Path, generation) + "/" paginator := s3.NewListObjectsV2Paginator(c.s3, &s3.ListObjectsV2Input{ Bucket: aws.String(c.Bucket), Prefix: aws.String(prefix), }) var segments []litestream.WALSegmentInfoV3 for paginator.HasMorePages() { page, err := paginator.NextPage(ctx) if err != nil { return nil, fmt.Errorf("s3: list wal segments: %w", err) } for _, obj := range page.Contents { key := path.Base(aws.ToString(obj.Key)) index, offset, err := litestream.ParseWALSegmentFilenameV3(key) if err != nil { continue // skip invalid filenames } segments = append(segments, litestream.WALSegmentInfoV3{ Generation: generation, Index: index, Offset: offset, Size: aws.ToInt64(obj.Size), CreatedAt: aws.ToTime(obj.LastModified).UTC(), }) } } slices.SortFunc(segments, func(a, b litestream.WALSegmentInfoV3) int { if a.Index != b.Index { return a.Index - b.Index } return int(a.Offset - b.Offset) }) return segments, nil } // OpenSnapshotV3 opens a v0.3.x snapshot file for reading. // The returned reader provides LZ4-decompressed data. func (c *ReplicaClient) OpenSnapshotV3(ctx context.Context, generation string, index int) (io.ReadCloser, error) { if err := c.Init(ctx); err != nil { return nil, err } key := litestream.SnapshotPathV3(c.Path, generation, index) input := &s3.GetObjectInput{ Bucket: aws.String(c.Bucket), Key: aws.String(key), } // Add SSE-C parameters if configured if c.SSECustomerKey != "" { input.SSECustomerAlgorithm = aws.String(c.SSECustomerAlgorithm) input.SSECustomerKey = aws.String(c.SSECustomerKey) input.SSECustomerKeyMD5 = aws.String(c.SSECustomerKeyMD5) } out, err := c.s3.GetObject(ctx, input) if err != nil { if isNotExists(err) { return nil, os.ErrNotExist } return nil, fmt.Errorf("s3: get snapshot %s: %w", key, err) } return internal.NewLZ4Reader(out.Body), nil } // OpenWALSegmentV3 opens a v0.3.x WAL segment file for reading. // The returned reader provides LZ4-decompressed data. func (c *ReplicaClient) OpenWALSegmentV3(ctx context.Context, generation string, index int, offset int64) (io.ReadCloser, error) { if err := c.Init(ctx); err != nil { return nil, err } key := litestream.WALSegmentPathV3(c.Path, generation, index, offset) input := &s3.GetObjectInput{ Bucket: aws.String(c.Bucket), Key: aws.String(key), } // Add SSE-C parameters if configured if c.SSECustomerKey != "" { input.SSECustomerAlgorithm = aws.String(c.SSECustomerAlgorithm) input.SSECustomerKey = aws.String(c.SSECustomerKey) input.SSECustomerKeyMD5 = aws.String(c.SSECustomerKeyMD5) } out, err := c.s3.GetObject(ctx, input) if err != nil { if isNotExists(err) { return nil, os.ErrNotExist } return nil, fmt.Errorf("s3: get wal segment %s: %w", key, err) } return internal.NewLZ4Reader(out.Body), nil } // fileIterator represents an iterator over LTX files in S3. type fileIterator struct { ctx context.Context cancel context.CancelFunc client *ReplicaClient level int seek ltx.TXID prefix string useMetadata bool // When true, fetch accurate timestamps from metadata metadataCache map[string]time.Time // key -> timestamp cache for batch fetches paginator *s3.ListObjectsV2Paginator page *s3.ListObjectsV2Output pageIndex int closed bool err error info *ltx.FileInfo } func newFileIterator(ctx context.Context, client *ReplicaClient, level int, seek ltx.TXID, useMetadata bool) *fileIterator { ctx, cancel := context.WithCancel(ctx) prefix := client.Path + "/" + fmt.Sprintf("%04x/", level) itr := &fileIterator{ ctx: ctx, cancel: cancel, client: client, level: level, seek: seek, prefix: prefix, useMetadata: useMetadata, metadataCache: make(map[string]time.Time), } // Create paginator for listing objects with level prefix itr.paginator = s3.NewListObjectsV2Paginator(client.s3, &s3.ListObjectsV2Input{ Bucket: aws.String(client.Bucket), Prefix: aws.String(prefix), }) return itr } // fetchMetadataBatch fetches timestamps from S3 metadata for a batch of keys in parallel. func (itr *fileIterator) fetchMetadataBatch(keys []string) error { if len(keys) == 0 { return nil } // Determine concurrency limit concurrency := itr.client.MetadataConcurrency if concurrency <= 0 { concurrency = DefaultMetadataConcurrency } // Pre-allocate results map to avoid lock contention during writes results := make(map[string]time.Time, len(keys)) var mu sync.Mutex // Use x/sync/semaphore for precise concurrency control with context support sem := semaphore.NewWeighted(int64(concurrency)) g, ctx := errgroup.WithContext(itr.ctx) for _, key := range keys { key := key // capture for goroutine g.Go(func() error { // Acquire semaphore slot (blocking with context cancellation) if err := sem.Acquire(ctx, 1); err != nil { return err // context cancelled } defer sem.Release(1) headInput := &s3.HeadObjectInput{ Bucket: aws.String(itr.client.Bucket), Key: aws.String(key), } // Add SSE-C parameters if configured (required for reading SSE-C encrypted objects) // Note: SSE-KMS does not require parameters on HeadObject - access is automatic if itr.client.SSECustomerKey != "" { headInput.SSECustomerAlgorithm = aws.String(itr.client.SSECustomerAlgorithm) headInput.SSECustomerKey = aws.String(itr.client.SSECustomerKey) headInput.SSECustomerKeyMD5 = aws.String(itr.client.SSECustomerKeyMD5) } head, err := itr.client.s3.HeadObject(ctx, headInput) if err != nil { // Non-fatal: file might not have metadata, use LastModified return nil } if head.Metadata != nil { if ts, ok := head.Metadata[MetadataKeyTimestamp]; ok { if parsed, err := time.Parse(time.RFC3339Nano, ts); err == nil { mu.Lock() results[key] = parsed mu.Unlock() } } } return nil }) } if err := g.Wait(); err != nil { return err } // Merge results into cache for k, v := range results { itr.metadataCache[k] = v } return nil } // Close stops iteration and returns any error that occurred during iteration. func (itr *fileIterator) Close() (err error) { itr.closed = true itr.cancel() return itr.err } // Next returns the next file. Returns false when no more files are available. func (itr *fileIterator) Next() bool { if itr.closed || itr.err != nil { return false } // Process objects until we find a valid LTX file for { // Load next page if needed if itr.page == nil || itr.pageIndex >= len(itr.page.Contents) { if !itr.paginator.HasMorePages() { return false } var err error itr.page, err = itr.paginator.NextPage(itr.ctx) if err != nil { itr.err = err return false } itr.pageIndex = 0 // Log page contents for debugging. if len(itr.page.Contents) > 0 { keys := make([]string, 0, len(itr.page.Contents)) for _, obj := range itr.page.Contents { keys = append(keys, path.Base(aws.ToString(obj.Key))) } itr.client.logger.Debug("s3 LIST page", "prefix", itr.prefix, "keys", keys) } // Batch fetch metadata for the entire page when useMetadata is true. // This uses parallel HeadObject calls controlled by MetadataConcurrency // to avoid the O(N) sequential calls that caused restore hangs (issue #930). if itr.useMetadata && len(itr.page.Contents) > 0 { keys := make([]string, 0, len(itr.page.Contents)) for _, obj := range itr.page.Contents { keys = append(keys, aws.ToString(obj.Key)) } if err := itr.fetchMetadataBatch(keys); err != nil { itr.err = err return false } } } // Process current object if itr.pageIndex < len(itr.page.Contents) { obj := itr.page.Contents[itr.pageIndex] itr.pageIndex++ // Extract file info from key fullKey := aws.ToString(obj.Key) key := path.Base(fullKey) minTXID, maxTXID, err := ltx.ParseFilename(key) if err != nil { continue // Skip non-LTX files } // Build file info info := <x.FileInfo{ Level: itr.level, MinTXID: minTXID, MaxTXID: maxTXID, } // Skip if below seek TXID if info.MinTXID < itr.seek { continue } // Skip if wrong level if info.Level != itr.level { continue } // Set file info info.Size = aws.ToInt64(obj.Size) // Use cached metadata timestamp if available (from batch fetch), // otherwise fallback to LastModified from LIST operation. if itr.useMetadata { if ts, ok := itr.metadataCache[fullKey]; ok { info.CreatedAt = ts } else { info.CreatedAt = aws.ToTime(obj.LastModified).UTC() } } else { info.CreatedAt = aws.ToTime(obj.LastModified).UTC() } itr.info = info return true } } } // Item returns the metadata for the current file. func (itr *fileIterator) Item() *ltx.FileInfo { return itr.info } // Err returns any error that occurred during iteration. func (itr *fileIterator) Err() error { return itr.err } // ParseURL parses an S3 URL into its host and path parts. // If endpoint is set, it can override the host. func ParseURL(s, endpoint string) (bucket, region, key string, err error) { u, err := url.Parse(s) if err != nil { return "", "", "", err } if u.Scheme != "s3" { return "", "", "", fmt.Errorf("s3: invalid url scheme") } // Special handling for filebase.com if u.Host == "filebase.com" { parts := strings.SplitN(strings.TrimPrefix(u.Path, "/"), "/", 2) if len(parts) == 0 { return "", "", "", fmt.Errorf("s3: bucket required") } bucket = parts[0] if len(parts) > 1 { key = parts[1] } return bucket, "", key, nil } // For other hosts, check if it's a special endpoint bucket, region, _, _ = ParseHost(u.Host) if bucket == "" { bucket = u.Host } key = strings.TrimPrefix(u.Path, "/") return bucket, region, key, nil } // ParseHost parses the host/endpoint for an S3-like storage system. // Endpoints: https://docs.aws.amazon.com/general/latest/gr/s3.html func ParseHost(host string) (bucket, region, endpoint string, forcePathStyle bool) { // Check for MinIO-style hosts (bucket.host:port) if strings.Contains(host, ":") && !strings.Contains(host, ".com") { parts := strings.SplitN(host, ".", 2) if len(parts) == 2 { // Extract bucket from bucket.host:port format bucket = parts[0] endpoint = "http://" + parts[1] return bucket, DefaultRegion, endpoint, true } // No bucket in host, just host:port return "", "", "http://" + host, true } // Check common object storage providers // Check for AWS S3 URLs first if a := awsS3Regex.FindStringSubmatch(host); len(a) > 1 { bucket = a[1] if len(a) > 2 && a[2] != "" { region = a[2] } return bucket, region, "", false } else if a := digitaloceanRegex.FindStringSubmatch(host); len(a) > 1 { bucket = a[1] region = a[2] return bucket, region, fmt.Sprintf("https://%s.digitaloceanspaces.com", region), false } else if a := backblazeRegex.FindStringSubmatch(host); len(a) > 1 { region = a[2] bucket = a[1] endpoint = fmt.Sprintf("https://s3.%s.backblazeb2.com", region) return bucket, region, endpoint, true } else if a := filebaseRegex.FindStringSubmatch(host); len(a) > 1 { bucket = a[1] endpoint = "s3.filebase.com" return bucket, "", endpoint, false } else if a := scalewayRegex.FindStringSubmatch(host); len(a) > 1 { region = a[2] bucket = a[1] endpoint = fmt.Sprintf("s3.%s.scw.cloud", region) return bucket, region, endpoint, false } // For standard S3, the host is the bucket name return host, "", "", false } var ( awsS3Regex = regexp.MustCompile(`^(.+)\.s3(?:\.([^.]+))?\.amazonaws\.com$`) digitaloceanRegex = regexp.MustCompile(`^(?:(.+)\.)?([^.]+)\.digitaloceanspaces.com$`) backblazeRegex = regexp.MustCompile(`^(?:(.+)\.)?s3.([^.]+)\.backblazeb2.com$`) filebaseRegex = regexp.MustCompile(`^(?:(.+)\.)?s3.filebase.com$`) scalewayRegex = regexp.MustCompile(`^(?:(.+)\.)?s3.([^.]+)\.scw\.cloud$`) ) func isNotExists(err error) bool { var apiErr smithy.APIError if errors.As(err, &apiErr) { return apiErr.ErrorCode() == "NoSuchKey" } return false } func deleteOutputError(out *s3.DeleteObjectsOutput) error { if len(out.Errors) == 0 { return nil } // Build generic error var b strings.Builder b.WriteString("failed to delete files:") for _, err := range out.Errors { fmt.Fprintf(&b, "\n%s: %s", aws.ToString(err.Key), aws.ToString(err.Message)) } return errors.New(b.String()) } // parseS3DebugEnv parses the LITESTREAM_S3_DEBUG environment variable and returns // the corresponding AWS SDK ClientLogMode. Supports comma-separated values. func parseS3DebugEnv() aws.ClientLogMode { v := os.Getenv("LITESTREAM_S3_DEBUG") if v == "" { return 0 } var logMode aws.ClientLogMode for _, mode := range strings.Split(v, ",") { switch strings.ToLower(strings.TrimSpace(mode)) { case "signing": logMode |= aws.LogSigning case "request": logMode |= aws.LogRequest case "request-with-body": logMode |= aws.LogRequestWithBody case "response": logMode |= aws.LogResponse case "response-with-body": logMode |= aws.LogResponseWithBody case "retries": logMode |= aws.LogRetries case "all": logMode |= aws.LogSigning | aws.LogRequest | aws.LogRequestWithBody | aws.LogResponse | aws.LogResponseWithBody | aws.LogRetries default: slog.Warn("unknown LITESTREAM_S3_DEBUG value, expected: signing, request, request-with-body, response, response-with-body, retries, all", "value", mode) } } return logMode } ================================================ FILE: s3/replica_client_test.go ================================================ package s3 import ( "bytes" "context" "crypto/md5" "crypto/sha256" "encoding/base64" "encoding/hex" "errors" "fmt" "io" "log/slog" "net/http" "net/http/httptest" "strings" "testing" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3/types" "github.com/aws/smithy-go" "github.com/aws/smithy-go/middleware" smithyhttp "github.com/aws/smithy-go/transport/http" "github.com/superfly/ltx" litestream "github.com/benbjohnson/litestream" ) // mockAPIError implements smithy.APIError for testing type mockAPIError struct { code string message string } func (e *mockAPIError) Error() string { return e.message } func (e *mockAPIError) ErrorCode() string { return e.code } func (e *mockAPIError) ErrorMessage() string { return e.message } func (e *mockAPIError) ErrorFault() smithy.ErrorFault { return smithy.FaultUnknown } func TestIsNotExists(t *testing.T) { // Test with NoSuchKey error noSuchKeyErr := &mockAPIError{ code: "NoSuchKey", message: "The specified key does not exist", } if !isNotExists(noSuchKeyErr) { t.Error("isNotExists should return true for NoSuchKey error") } // Test with different error code differentErr := &mockAPIError{ code: "AccessDenied", message: "Access denied", } if isNotExists(differentErr) { t.Error("isNotExists should return false for non-NoSuchKey error") } // Test with non-API error regularErr := errors.New("regular error") if isNotExists(regularErr) { t.Error("isNotExists should return false for non-API error") } // Test with nil error if isNotExists(nil) { t.Error("isNotExists should return false for nil error") } // Test with wrapped API error wrappedErr := &mockAPIError{ code: "NoSuchKey", message: "wrapped key error", } if !isNotExists(wrappedErr) { t.Error("isNotExists should return true for wrapped NoSuchKey error") } } func TestReplicaClient_DefaultSignPayload(t *testing.T) { client := NewReplicaClient() if !client.SignPayload { t.Error("expected default SignPayload to be true for AWS S3 compatibility") } if !client.RequireContentMD5 { t.Error("expected default RequireContentMD5 to be true for AWS S3 compatibility") } } func TestReplicaClientPayloadSigning(t *testing.T) { data := mustLTX(t) signedPayload := sha256.Sum256(data) wantSigned := hex.EncodeToString(signedPayload[:]) tests := []struct { name string signPayload bool wantHeader string }{ {name: "UnsignedWhenDisabled", signPayload: false, wantHeader: "UNSIGNED-PAYLOAD"}, {name: "SignedByDefault", signPayload: true, wantHeader: wantSigned}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { headers := make(chan http.Header, 1) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() _, _ = io.Copy(io.Discard, r.Body) if r.Method == http.MethodPut { select { case headers <- r.Header.Clone(): default: } w.Header().Set("ETag", `"test-etag"`) w.WriteHeader(http.StatusOK) return } w.WriteHeader(http.StatusOK) })) defer server.Close() client := NewReplicaClient() client.Bucket = "test-bucket" client.Path = "replica" client.Region = "us-east-1" client.Endpoint = server.URL client.ForcePathStyle = true client.AccessKeyID = "test-access-key" client.SecretAccessKey = "test-secret-key" client.SignPayload = tt.signPayload ctx := context.Background() if err := client.Init(ctx); err != nil { t.Fatalf("Init() error: %v", err) } if _, err := client.WriteLTXFile(ctx, 0, 2, 2, bytes.NewReader(data)); err != nil { t.Fatalf("WriteLTXFile() error: %v", err) } select { case hdr := <-headers: if got, want := hdr.Get("x-amz-content-sha256"), tt.wantHeader; got != want { t.Fatalf("x-amz-content-sha256 header = %q, want %q", got, want) } case <-time.After(time.Second): t.Fatal("timeout waiting for PUT request") } }) } } func TestReplicaClient_UnsignedPayload_NoChunkedEncoding(t *testing.T) { data := mustLTX(t) headers := make(chan http.Header, 1) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() _, _ = io.Copy(io.Discard, r.Body) if r.Method == http.MethodPut { select { case headers <- r.Header.Clone(): default: } w.Header().Set("ETag", `"test-etag"`) w.WriteHeader(http.StatusOK) return } w.WriteHeader(http.StatusOK) })) defer server.Close() client := NewReplicaClient() client.Bucket = "test-bucket" client.Path = "replica" client.Region = "us-east-1" client.Endpoint = server.URL client.ForcePathStyle = true client.AccessKeyID = "test-access-key" client.SecretAccessKey = "test-secret-key" client.SignPayload = false ctx := context.Background() if err := client.Init(ctx); err != nil { t.Fatalf("Init() error: %v", err) } if _, err := client.WriteLTXFile(ctx, 0, 2, 2, bytes.NewReader(data)); err != nil { t.Fatalf("WriteLTXFile() error: %v", err) } select { case hdr := <-headers: if got := hdr.Get("x-amz-content-sha256"); got != "UNSIGNED-PAYLOAD" { t.Errorf("x-amz-content-sha256 = %q, want UNSIGNED-PAYLOAD", got) } contentEnc := hdr.Get("Content-Encoding") if strings.Contains(contentEnc, "aws-chunked") { t.Errorf("Content-Encoding contains aws-chunked: %q; aws-chunked is incompatible with UNSIGNED-PAYLOAD", contentEnc) } transferEnc := hdr.Get("Transfer-Encoding") if strings.Contains(transferEnc, "aws-chunked") { t.Errorf("Transfer-Encoding contains aws-chunked: %q; aws-chunked is incompatible with UNSIGNED-PAYLOAD", transferEnc) } decoded := hdr.Get("X-Amz-Decoded-Content-Length") if decoded != "" { t.Errorf("X-Amz-Decoded-Content-Length = %q; this header indicates aws-chunked encoding which is incompatible with UNSIGNED-PAYLOAD", decoded) } case <-time.After(time.Second): t.Fatal("timeout waiting for PUT request") } } // TestReplicaClient_SignedPayload_CustomEndpoint_NoChunkedEncoding verifies that // aws-chunked encoding is disabled for custom endpoints even when SignPayload=true. // This is necessary for S3-compatible providers (Filebase, MinIO, Backblaze B2, etc.) // that don't support aws-chunked encoding at all. See issue #895. func TestReplicaClient_SignedPayload_CustomEndpoint_NoChunkedEncoding(t *testing.T) { data := mustLTX(t) headers := make(chan http.Header, 1) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() _, _ = io.Copy(io.Discard, r.Body) if r.Method == http.MethodPut { select { case headers <- r.Header.Clone(): default: } w.Header().Set("ETag", `"test-etag"`) w.WriteHeader(http.StatusOK) return } w.WriteHeader(http.StatusOK) })) defer server.Close() client := NewReplicaClient() client.Bucket = "test-bucket" client.Path = "replica" client.Region = "us-east-1" client.Endpoint = server.URL // Custom endpoint (non-AWS) client.ForcePathStyle = true client.AccessKeyID = "test-access-key" client.SecretAccessKey = "test-secret-key" client.SignPayload = true // Signed payload, but still using custom endpoint ctx := context.Background() if err := client.Init(ctx); err != nil { t.Fatalf("Init() error: %v", err) } if _, err := client.WriteLTXFile(ctx, 0, 2, 2, bytes.NewReader(data)); err != nil { t.Fatalf("WriteLTXFile() error: %v", err) } select { case hdr := <-headers: // With SignPayload=true, we expect an actual SHA256 hash (not UNSIGNED-PAYLOAD) sha256Header := hdr.Get("x-amz-content-sha256") if sha256Header == "" { t.Error("x-amz-content-sha256 header should be set") } if sha256Header == "UNSIGNED-PAYLOAD" { t.Error("x-amz-content-sha256 should be actual hash, not UNSIGNED-PAYLOAD, when SignPayload=true") } // But aws-chunked encoding should still be disabled for custom endpoints contentEnc := hdr.Get("Content-Encoding") if strings.Contains(contentEnc, "aws-chunked") { t.Errorf("Content-Encoding contains aws-chunked: %q; aws-chunked is not supported by S3-compatible providers", contentEnc) } transferEnc := hdr.Get("Transfer-Encoding") if strings.Contains(transferEnc, "aws-chunked") { t.Errorf("Transfer-Encoding contains aws-chunked: %q; aws-chunked is not supported by S3-compatible providers", transferEnc) } decoded := hdr.Get("X-Amz-Decoded-Content-Length") if decoded != "" { t.Errorf("X-Amz-Decoded-Content-Length = %q; this header indicates aws-chunked encoding which is not supported by S3-compatible providers", decoded) } case <-time.After(time.Second): t.Fatal("timeout waiting for PUT request") } } func mustLTX(t *testing.T) []byte { t.Helper() buf := new(bytes.Buffer) enc, err := ltx.NewEncoder(buf) if err != nil { t.Fatalf("NewEncoder: %v", err) } if err := enc.EncodeHeader(ltx.Header{ Version: ltx.Version, PageSize: 4096, Commit: 0, MinTXID: 2, MaxTXID: 2, Timestamp: time.Now().UnixMilli(), PreApplyChecksum: ltx.ChecksumFlag | 1, }); err != nil { t.Fatalf("EncodeHeader: %v", err) } enc.SetPostApplyChecksum(ltx.ChecksumFlag) if err := enc.Close(); err != nil { t.Fatalf("Close: %v", err) } return buf.Bytes() } func mustLTXWithSize(t *testing.T, size int) []byte { t.Helper() header := mustLTX(t) if size <= len(header) { return header[:size] } data := make([]byte, size) copy(data, header) return data } func TestReplicaClient_MultipartUploadThreshold(t *testing.T) { const mb = 1024 * 1024 tests := []struct { name string payloadSize int wantMultipart bool }{ {"BelowThreshold_4MB", 4 * mb, false}, {"AtThreshold_5MB", 5 * mb, true}, {"AboveThreshold_6MB", 6 * mb, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var ( gotPut bool gotInitiate bool gotComplete bool awsChunked bool ) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() _, _ = io.Copy(io.Discard, r.Body) if r.Method == http.MethodPut { if strings.Contains(r.Header.Get("Content-Encoding"), "aws-chunked") { awsChunked = true } if strings.Contains(r.Header.Get("Transfer-Encoding"), "aws-chunked") { awsChunked = true } } query := r.URL.Query() if r.Method == http.MethodPost && query.Has("uploads") { gotInitiate = true w.Header().Set("Content-Type", "application/xml") w.WriteHeader(http.StatusOK) fmt.Fprint(w, `test-buckettest-keytest-upload-id`) return } if r.Method == http.MethodPut && query.Get("partNumber") != "" { w.Header().Set("ETag", fmt.Sprintf(`"part-etag-%s"`, query.Get("partNumber"))) w.WriteHeader(http.StatusOK) return } if r.Method == http.MethodPost && query.Get("uploadId") != "" && !query.Has("uploads") { gotComplete = true w.Header().Set("Content-Type", "application/xml") w.WriteHeader(http.StatusOK) fmt.Fprint(w, `http://test-bucket.s3.amazonaws.com/test-keytest-buckettest-key"complete-etag"`) return } if r.Method == http.MethodPut { gotPut = true w.Header().Set("ETag", `"test-etag"`) w.WriteHeader(http.StatusOK) return } w.WriteHeader(http.StatusOK) })) defer server.Close() data := mustLTXWithSize(t, tt.payloadSize) client := NewReplicaClient() client.Bucket = "test-bucket" client.Path = "replica" client.Region = "us-east-1" client.Endpoint = server.URL client.ForcePathStyle = true client.AccessKeyID = "test-access-key" client.SecretAccessKey = "test-secret-key" ctx := context.Background() if err := client.Init(ctx); err != nil { t.Fatalf("Init() error: %v", err) } if _, err := client.WriteLTXFile(ctx, 0, 2, 2, bytes.NewReader(data)); err != nil { t.Fatalf("WriteLTXFile() error: %v", err) } if tt.wantMultipart { if !gotInitiate { t.Error("expected CreateMultipartUpload but did not receive one") } if !gotComplete { t.Error("expected CompleteMultipartUpload but did not receive one") } if gotPut { t.Error("did not expect single PUT for multipart upload") } } else { if !gotPut { t.Error("expected single PUT upload but did not receive one") } if gotInitiate { t.Error("did not expect CreateMultipartUpload for single-part upload") } } if awsChunked { t.Error("aws-chunked encoding detected; this is incompatible with S3-compatible providers") } }) } } // TestReplicaClient_Init_BucketValidation tests that Init validates bucket name func TestReplicaClient_Init_BucketValidation(t *testing.T) { t.Run("EmptyBucket", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "" // Empty bucket name c.Region = "us-east-1" err := c.Init(context.Background()) if err == nil { t.Fatal("expected error for empty bucket name") } if !strings.Contains(err.Error(), "bucket name is required") { t.Errorf("unexpected error: %v", err) } }) t.Run("ValidBucket", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" // Note: This will fail when trying to connect, but should pass bucket validation err := c.Init(context.Background()) // We expect a different error (not bucket validation) if err != nil && strings.Contains(err.Error(), "bucket name is required") { t.Errorf("should not fail bucket validation with valid bucket: %v", err) } }) } // TestReplicaClient_UploaderConfiguration tests that uploader configuration is applied func TestReplicaClient_UploaderConfiguration(t *testing.T) { t.Run("CustomPartSize", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.PartSize = 10 * 1024 * 1024 // 10MB c.Concurrency = 10 // Verify the configuration is set if c.PartSize != 10*1024*1024 { t.Errorf("expected PartSize to be 10MB, got %d", c.PartSize) } if c.Concurrency != 10 { t.Errorf("expected Concurrency to be 10, got %d", c.Concurrency) } }) t.Run("DefaultConfiguration", func(t *testing.T) { c := NewReplicaClient() // Verify defaults are zero (will use SDK defaults) if c.PartSize != 0 { t.Errorf("expected default PartSize to be 0, got %d", c.PartSize) } if c.Concurrency != 0 { t.Errorf("expected default Concurrency to be 0, got %d", c.Concurrency) } }) } // TestReplicaClient_ConfigureEndpoint tests the endpoint configuration helper func TestReplicaClient_ConfigureEndpoint(t *testing.T) { tests := []struct { name string endpoint string forcePathStyle bool expectHTTPS bool }{ { name: "HTTPEndpoint", endpoint: "http://localhost:9000", forcePathStyle: true, expectHTTPS: false, }, { name: "HTTPSEndpoint", endpoint: "https://s3.amazonaws.com", forcePathStyle: false, expectHTTPS: true, }, { name: "EndpointWithoutScheme", endpoint: "s3.us-west-002.backblazeb2.com", forcePathStyle: false, expectHTTPS: true, }, { name: "EmptyEndpoint", endpoint: "", forcePathStyle: false, expectHTTPS: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { c := NewReplicaClient() c.Endpoint = tt.endpoint c.ForcePathStyle = tt.forcePathStyle // Test that configureEndpoint can be called without error var opts []func(*s3.Options) c.configureEndpoint(&opts) // Verify opts were added when endpoint is set if tt.endpoint != "" && len(opts) == 0 { t.Error("expected endpoint options to be added") } if tt.endpoint == "" && len(opts) != 0 { t.Error("expected no endpoint options for empty endpoint") } }) } } // TestReplicaClient_HTTPClientConfiguration tests HTTP client setup func TestReplicaClient_HTTPClientConfiguration(t *testing.T) { t.Run("WithSkipVerify", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.SkipVerify = true // We can't directly test the HTTP client configuration without // actually initializing, but we can verify the flag is set if !c.SkipVerify { t.Error("expected SkipVerify to be true") } }) t.Run("WithoutSkipVerify", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.SkipVerify = false if c.SkipVerify { t.Error("expected SkipVerify to be false") } }) } func TestReplicaClientDeleteLTXFiles_ContentMD5(t *testing.T) { t.Run("Enabled", func(t *testing.T) { var callCount int httpClient := smithyhttp.ClientDoFunc(func(r *http.Request) (*http.Response, error) { t.Helper() callCount++ if r.Method != http.MethodPost { t.Fatalf("unexpected method: %s", r.Method) } if !strings.Contains(r.URL.RawQuery, "delete") { t.Fatalf("unexpected query: %s", r.URL.RawQuery) } if ua := r.Header.Get("User-Agent"); !strings.Contains(ua, "litestream") { t.Fatalf("expected User-Agent to contain litestream, got %q", ua) } body, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("read body: %v", err) } r.Body.Close() got := r.Header.Get("Content-MD5") if got == "" { t.Fatal("expected Content-MD5 header") } sum := md5.Sum(body) want := base64.StdEncoding.EncodeToString(sum[:]) if got != want { t.Fatalf("unexpected Content-MD5 header: got %q, want %q", got, want) } resp := &http.Response{ StatusCode: http.StatusOK, Header: http.Header{"Content-Type": []string{"application/xml"}}, Body: io.NopCloser(strings.NewReader( ``, )), } return resp, nil }) cfg := aws.Config{ Region: "us-east-1", Credentials: aws.NewCredentialsCache(aws.AnonymousCredentials{}), HTTPClient: httpClient, } c := NewReplicaClient() c.logger = slog.New(slog.NewTextHandler(io.Discard, nil)) c.s3 = s3.NewFromConfig(cfg, func(o *s3.Options) { o.APIOptions = append(o.APIOptions, c.middlewareOption()) }) c.Bucket = "test-bucket" c.Path = "test-path" files := []*ltx.FileInfo{ {Level: 0, MinTXID: 1, MaxTXID: 1}, {Level: 0, MinTXID: 2, MaxTXID: 2}, } if err := c.DeleteLTXFiles(context.Background(), files); err != nil { t.Fatalf("DeleteLTXFiles: %v", err) } if callCount != 1 { t.Fatalf("unexpected call count: %d", callCount) } }) t.Run("Disabled", func(t *testing.T) { httpClient := smithyhttp.ClientDoFunc(func(r *http.Request) (*http.Response, error) { t.Helper() if md5Header := r.Header.Get("Content-MD5"); md5Header != "" { t.Fatalf("expected Content-MD5 header to be empty when disabled, got %q", md5Header) } resp := &http.Response{ StatusCode: http.StatusOK, Header: http.Header{"Content-Type": []string{"application/xml"}}, Body: io.NopCloser(strings.NewReader( ``, )), } return resp, nil }) cfg := aws.Config{ Region: "us-east-1", Credentials: aws.NewCredentialsCache(aws.AnonymousCredentials{}), HTTPClient: httpClient, } c := NewReplicaClient() c.RequireContentMD5 = false c.logger = slog.New(slog.NewTextHandler(io.Discard, nil)) c.s3 = s3.NewFromConfig(cfg, func(o *s3.Options) { o.APIOptions = append(o.APIOptions, c.middlewareOption()) }) c.Bucket = "test-bucket" c.Path = "test-path" files := []*ltx.FileInfo{{Level: 0, MinTXID: 1, MaxTXID: 1}} if err := c.DeleteLTXFiles(context.Background(), files); err != nil { t.Fatalf("DeleteLTXFiles: %v", err) } }) } func TestReplicaClientDeleteLTXFiles_PreexistingContentMD5(t *testing.T) { const preexistingMD5 = "preexisting-checksum-value" var callCount int httpClient := smithyhttp.ClientDoFunc(func(r *http.Request) (*http.Response, error) { t.Helper() callCount++ got := r.Header.Get("Content-MD5") if got != preexistingMD5 { t.Fatalf("middleware should not override existing Content-MD5: got %q, want %q", got, preexistingMD5) } resp := &http.Response{ StatusCode: http.StatusOK, Header: http.Header{"Content-Type": []string{"application/xml"}}, Body: io.NopCloser(strings.NewReader( ``, )), } return resp, nil }) cfg := aws.Config{ Region: "us-east-1", Credentials: aws.NewCredentialsCache(aws.AnonymousCredentials{}), HTTPClient: httpClient, } c := NewReplicaClient() c.logger = slog.New(slog.NewTextHandler(io.Discard, nil)) c.s3 = s3.NewFromConfig(cfg, func(o *s3.Options) { o.APIOptions = append(o.APIOptions, c.middlewareOption()) o.APIOptions = append(o.APIOptions, func(stack *middleware.Stack) error { return stack.Finalize.Add( middleware.FinalizeMiddlewareFunc( "InjectPreexistingContentMD5", func(ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) ( out middleware.FinalizeOutput, metadata middleware.Metadata, err error, ) { if req, ok := in.Request.(*smithyhttp.Request); ok { req.Header.Set("Content-MD5", preexistingMD5) } return next.HandleFinalize(ctx, in) }, ), middleware.Before, ) }) }) c.Bucket = "test-bucket" c.Path = "test-path" files := []*ltx.FileInfo{ {Level: 0, MinTXID: 1, MaxTXID: 1}, } if err := c.DeleteLTXFiles(context.Background(), files); err != nil { t.Fatalf("DeleteLTXFiles: %v", err) } if callCount != 1 { t.Fatalf("unexpected call count: %d", callCount) } } // TestReplicaClient_CredentialConfiguration tests credential setup func TestReplicaClient_CredentialConfiguration(t *testing.T) { t.Run("WithStaticCredentials", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.AccessKeyID = "AKIAIOSFODNN7EXAMPLE" c.SecretAccessKey = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" // Verify credentials are set if c.AccessKeyID == "" || c.SecretAccessKey == "" { t.Error("expected credentials to be set") } }) t.Run("WithDefaultCredentialChain", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" // Leave AccessKeyID and SecretAccessKey empty // Verify credentials are not set (will use default chain) if c.AccessKeyID != "" || c.SecretAccessKey != "" { t.Error("expected credentials to be empty for default chain") } }) } // TestReplicaClient_DefaultRegionUsage tests that DefaultRegion constant is used consistently func TestReplicaClient_DefaultRegionUsage(t *testing.T) { // Test that DefaultRegion is properly defined if DefaultRegion != "us-east-1" { t.Errorf("expected DefaultRegion to be 'us-east-1', got %s", DefaultRegion) } // Test ParseHost uses DefaultRegion t.Run("ParseHost_MinIO", func(t *testing.T) { bucket, region, endpoint, forcePathStyle := ParseHost("mybucket.localhost:9000") if region != DefaultRegion { t.Errorf("expected region to be %s, got %s", DefaultRegion, region) } if bucket != "mybucket" { t.Errorf("expected bucket to be 'mybucket', got %s", bucket) } if !strings.Contains(endpoint, "localhost:9000") { t.Errorf("expected endpoint to contain 'localhost:9000', got %s", endpoint) } if !forcePathStyle { t.Error("expected forcePathStyle to be true for MinIO") } }) } func TestMarshalDeleteObjects_EdgeCases(t *testing.T) { t.Run("EmptyObjects", func(t *testing.T) { deleteInput := &types.Delete{ Objects: []types.ObjectIdentifier{}, } xml, err := marshalDeleteObjects(deleteInput) if err != nil { t.Fatalf("marshalDeleteObjects failed: %v", err) } if !strings.Contains(string(xml), "test-key") { t.Errorf("expected Key element in XML, got: %s", xmlStr) } if strings.Contains(xmlStr, "") { t.Error("expected no ETag element when nil") } if strings.Contains(xmlStr, "") { t.Error("expected no VersionId element when nil") } }) t.Run("QuietFlag", func(t *testing.T) { deleteInput := &types.Delete{ Objects: []types.ObjectIdentifier{ {Key: aws.String("test")}, }, Quiet: aws.Bool(true), } xml, err := marshalDeleteObjects(deleteInput) if err != nil { t.Fatalf("marshalDeleteObjects failed: %v", err) } xmlStr := string(xml) if !strings.Contains(xmlStr, "true") { t.Errorf("expected Quiet element to be true, got: %s", xmlStr) } }) } func TestEncodeObjectIdentifier_AllFields(t *testing.T) { t.Run("AllFieldsPopulated", func(t *testing.T) { timestamp, err := time.Parse(time.RFC3339, "2023-01-01T00:00:00Z") if err != nil { t.Fatalf("failed to parse timestamp: %v", err) } deleteInput := &types.Delete{ Objects: []types.ObjectIdentifier{ { Key: aws.String("my-object-key"), ETag: aws.String("abc123etag"), VersionId: aws.String("version-456"), LastModifiedTime: aws.Time(timestamp), Size: aws.Int64(12345), }, }, } xml, err := marshalDeleteObjects(deleteInput) if err != nil { t.Fatalf("marshalDeleteObjects failed: %v", err) } xmlStr := string(xml) if !strings.Contains(xmlStr, "my-object-key") { t.Error("expected Key element") } if !strings.Contains(xmlStr, "abc123etag") { t.Error("expected ETag element") } if !strings.Contains(xmlStr, "version-456") { t.Error("expected VersionId element") } if !strings.Contains(xmlStr, "") { t.Error("expected LastModifiedTime element") } if !strings.Contains(xmlStr, "12345") { t.Error("expected Size element with value 12345") } }) t.Run("OnlyRequiredKey", func(t *testing.T) { deleteInput := &types.Delete{ Objects: []types.ObjectIdentifier{ { Key: aws.String("only-key"), }, }, } xml, err := marshalDeleteObjects(deleteInput) if err != nil { t.Fatalf("marshalDeleteObjects failed: %v", err) } xmlStr := string(xml) if !strings.Contains(xmlStr, "only-key") { t.Error("expected Key element") } if strings.Contains(xmlStr, "") { t.Error("expected no ETag element when nil") } if strings.Contains(xmlStr, "") { t.Error("expected no VersionId element when nil") } }) t.Run("FieldOrder", func(t *testing.T) { deleteInput := &types.Delete{ Objects: []types.ObjectIdentifier{ { Key: aws.String("test"), ETag: aws.String("etag1"), VersionId: aws.String("v1"), }, }, } xml, err := marshalDeleteObjects(deleteInput) if err != nil { t.Fatalf("marshalDeleteObjects failed: %v", err) } xmlStr := string(xml) keyIdx := strings.Index(xmlStr, "") etagIdx := strings.Index(xmlStr, "") versionIdx := strings.Index(xmlStr, "") if keyIdx == -1 || etagIdx == -1 || versionIdx == -1 { t.Fatal("missing expected elements") } if etagIdx > keyIdx || keyIdx > versionIdx { t.Errorf("expected field order: ETag, Key, VersionId, got ETag@%d, Key@%d, VersionId@%d", etagIdx, keyIdx, versionIdx) } }) } func TestComputeDeleteObjectsContentMD5_Deterministic(t *testing.T) { deleteInput := &types.Delete{ Objects: []types.ObjectIdentifier{ {Key: aws.String("key1")}, {Key: aws.String("key2")}, }, } md51, err := computeDeleteObjectsContentMD5(deleteInput) if err != nil { t.Fatalf("first call failed: %v", err) } md52, err := computeDeleteObjectsContentMD5(deleteInput) if err != nil { t.Fatalf("second call failed: %v", err) } if md51 != md52 { t.Errorf("MD5 computation not deterministic: %q != %q", md51, md52) } if md51 == "" { t.Error("expected non-empty MD5") } } // TestParseHost tests URL parsing for various S3-compatible storage providers. // This test addresses issue #825 where Digital Ocean Space URLs were not correctly // extracting the bucket name. func TestParseHost(t *testing.T) { tests := []struct { name string host string wantBucket string wantRegion string wantEndpoint string wantForcePathStyle bool }{ { name: "Digital Ocean Space URL", host: "my-space.sgp1.digitaloceanspaces.com", wantBucket: "my-space", wantRegion: "sgp1", wantEndpoint: "https://sgp1.digitaloceanspaces.com", wantForcePathStyle: false, }, { name: "Digital Ocean Space different region", host: "test-bucket.nyc3.digitaloceanspaces.com", wantBucket: "test-bucket", wantRegion: "nyc3", wantEndpoint: "https://nyc3.digitaloceanspaces.com", wantForcePathStyle: false, }, { name: "AWS S3 URL with region", host: "mybucket.s3.us-east-1.amazonaws.com", wantBucket: "mybucket", wantRegion: "us-east-1", wantEndpoint: "", wantForcePathStyle: false, }, { name: "AWS S3 URL without region", host: "mybucket.s3.amazonaws.com", wantBucket: "mybucket", wantRegion: "", wantEndpoint: "", wantForcePathStyle: false, }, { name: "Backblaze B2", host: "mybucket.s3.us-west-004.backblazeb2.com", wantBucket: "mybucket", wantRegion: "us-west-004", wantEndpoint: "https://s3.us-west-004.backblazeb2.com", wantForcePathStyle: true, }, { name: "MinIO with port", host: "mybucket.localhost:9000", wantBucket: "mybucket", wantRegion: "us-east-1", wantEndpoint: "http://localhost:9000", wantForcePathStyle: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { bucket, region, endpoint, forcePathStyle := ParseHost(tt.host) if bucket != tt.wantBucket { t.Errorf("bucket = %q, want %q", bucket, tt.wantBucket) } if region != tt.wantRegion { t.Errorf("region = %q, want %q", region, tt.wantRegion) } if endpoint != tt.wantEndpoint { t.Errorf("endpoint = %q, want %q", endpoint, tt.wantEndpoint) } if forcePathStyle != tt.wantForcePathStyle { t.Errorf("forcePathStyle = %v, want %v", forcePathStyle, tt.wantForcePathStyle) } }) } } func TestReplicaClient_AccessPointARN(t *testing.T) { t.Run("ARNAsBucketName", func(t *testing.T) { arn := "arn:aws:s3:us-east-2:123456789012:accesspoint/my-access-point" c := NewReplicaClient() c.Bucket = arn c.Region = "us-east-2" c.AccessKeyID = "test-access-key" c.SecretAccessKey = "test-secret-key" if c.Bucket != arn { t.Errorf("expected bucket to be ARN, got %s", c.Bucket) } if c.Region != "us-east-2" { t.Errorf("expected region to be us-east-2, got %s", c.Region) } }) t.Run("ARNWithPath", func(t *testing.T) { arn := "arn:aws:s3:us-west-2:111122223333:accesspoint/prod-access-point" c := NewReplicaClient() c.Bucket = arn c.Path = "my-db/replica" c.Region = "us-west-2" if c.Bucket != arn { t.Errorf("expected bucket to be ARN, got %s", c.Bucket) } if c.Path != "my-db/replica" { t.Errorf("expected path to be my-db/replica, got %s", c.Path) } }) t.Run("ARNRejectsPathStyle", func(t *testing.T) { arn := "arn:aws:s3:us-east-1:123456789012:accesspoint/test-ap" c := NewReplicaClient() c.Bucket = arn c.Path = "replica" c.Region = "us-east-1" c.Endpoint = "http://localhost:9000" c.ForcePathStyle = true c.AccessKeyID = "test-access-key" c.SecretAccessKey = "test-secret-key" ctx := context.Background() if err := c.Init(ctx); err != nil { t.Fatalf("Init() with ARN bucket should not fail: %v", err) } data := mustLTX(t) _, err := c.WriteLTXFile(ctx, 0, 2, 2, bytes.NewReader(data)) if err == nil { t.Fatal("expected error when using path-style with ARN bucket") } if !strings.Contains(err.Error(), "Path-style addressing cannot be used with ARN") { t.Errorf("expected path-style ARN error, got: %v", err) } }) } func TestReplicaClient_S3DebugEnvVar(t *testing.T) { tests := []struct { name string envValue string wantLogMode aws.ClientLogMode wantWarning bool }{ { name: "Empty", envValue: "", wantLogMode: 0, }, { name: "Signing", envValue: "signing", wantLogMode: aws.LogSigning, }, { name: "Request", envValue: "request", wantLogMode: aws.LogRequest, }, { name: "RequestWithBody", envValue: "request-with-body", wantLogMode: aws.LogRequestWithBody, }, { name: "Response", envValue: "response", wantLogMode: aws.LogResponse, }, { name: "ResponseWithBody", envValue: "response-with-body", wantLogMode: aws.LogResponseWithBody, }, { name: "Retries", envValue: "retries", wantLogMode: aws.LogRetries, }, { name: "All", envValue: "all", wantLogMode: aws.LogSigning | aws.LogRequest | aws.LogRequestWithBody | aws.LogResponse | aws.LogResponseWithBody | aws.LogRetries, }, { name: "CommaSeparated", envValue: "signing,request,retries", wantLogMode: aws.LogSigning | aws.LogRequest | aws.LogRetries, }, { name: "CommaSeparatedWithSpaces", envValue: "signing, request, retries", wantLogMode: aws.LogSigning | aws.LogRequest | aws.LogRetries, }, { name: "CaseInsensitive", envValue: "SIGNING,REQUEST", wantLogMode: aws.LogSigning | aws.LogRequest, }, { name: "Unknown", envValue: "invalid", wantLogMode: 0, wantWarning: true, }, { name: "MixedValidAndInvalid", envValue: "signing,invalid,request", wantLogMode: aws.LogSigning | aws.LogRequest, wantWarning: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Always set env var (even to empty) to isolate tests from caller's environment t.Setenv("LITESTREAM_S3_DEBUG", tt.envValue) gotLogMode := parseS3DebugEnv() if gotLogMode != tt.wantLogMode { t.Errorf("parseS3DebugEnv() = %v, want %v", gotLogMode, tt.wantLogMode) } }) } } func TestReplicaClient_TigrisConsistentHeader(t *testing.T) { // Test that non-Tigris endpoints do NOT send the X-Tigris-Consistent header. // The Tigris case (header sent) requires an actual Tigris endpoint and is // covered by Tigris integration tests. data := mustLTX(t) headers := make(chan http.Header, 1) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() _, _ = io.Copy(io.Discard, r.Body) if r.Method == http.MethodPut { select { case headers <- r.Header.Clone(): default: } w.Header().Set("ETag", `"test-etag"`) w.WriteHeader(http.StatusOK) return } w.WriteHeader(http.StatusOK) })) defer server.Close() client := NewReplicaClient() client.Bucket = "test-bucket" client.Path = "replica" client.Region = "us-east-1" client.Endpoint = server.URL // Non-Tigris endpoint client.ForcePathStyle = true client.AccessKeyID = "test-access-key" client.SecretAccessKey = "test-secret-key" ctx := context.Background() if err := client.Init(ctx); err != nil { t.Fatalf("Init() error: %v", err) } if _, err := client.WriteLTXFile(ctx, 0, 2, 2, bytes.NewReader(data)); err != nil { t.Fatalf("WriteLTXFile() error: %v", err) } select { case hdr := <-headers: if got := hdr.Get("X-Tigris-Consistent"); got != "" { t.Fatalf("X-Tigris-Consistent header = %q, want empty (non-Tigris endpoint)", got) } case <-time.After(time.Second): t.Fatal("timeout waiting for PUT request") } } // TestReplicaClient_SSE_C_Validation tests SSE-C configuration validation func TestReplicaClient_SSE_C_Validation(t *testing.T) { // Generate a valid 256-bit key (32 bytes) validKey := base64.StdEncoding.EncodeToString([]byte("12345678901234567890123456789012")) t.Run("ValidSSECKey", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.Endpoint = "https://s3.example.com" c.SSECustomerKey = validKey err := c.validateSSEConfig() if err != nil { t.Errorf("expected no error for valid SSE-C key, got: %v", err) } // Verify algorithm was auto-set if c.SSECustomerAlgorithm != "AES256" { t.Errorf("expected algorithm to be AES256, got %q", c.SSECustomerAlgorithm) } // Verify MD5 was auto-computed if c.SSECustomerKeyMD5 == "" { t.Error("expected MD5 to be auto-computed") } }) t.Run("InvalidBase64Key", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.Endpoint = "https://s3.example.com" c.SSECustomerKey = "not-valid-base64!!!" err := c.validateSSEConfig() if err == nil { t.Error("expected error for invalid base64 key") } if !strings.Contains(err.Error(), "valid base64") { t.Errorf("expected base64 error, got: %v", err) } }) t.Run("WrongKeyLength", func(t *testing.T) { // 16-byte key instead of 32-byte shortKey := base64.StdEncoding.EncodeToString([]byte("1234567890123456")) c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.Endpoint = "https://s3.example.com" c.SSECustomerKey = shortKey err := c.validateSSEConfig() if err == nil { t.Error("expected error for wrong key length") } if !strings.Contains(err.Error(), "256-bit") { t.Errorf("expected key length error, got: %v", err) } }) t.Run("InvalidAlgorithm", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.Endpoint = "https://s3.example.com" c.SSECustomerKey = validKey c.SSECustomerAlgorithm = "AES128" // Invalid err := c.validateSSEConfig() if err == nil { t.Error("expected error for invalid algorithm") } if !strings.Contains(err.Error(), "AES256") { t.Errorf("expected algorithm error, got: %v", err) } }) t.Run("MutualExclusivity", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.SSECustomerKey = validKey c.SSEKMSKeyID = "arn:aws:kms:us-east-1:123456789:key/12345678-1234-1234-1234-123456789012" err := c.validateSSEConfig() if err == nil { t.Error("expected error when both SSE-C and SSE-KMS are set") } if !strings.Contains(err.Error(), "mutually exclusive") { t.Errorf("expected mutual exclusivity error, got: %v", err) } }) t.Run("HTTPEndpointBlockedExceptLocalhost", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.Endpoint = "http://external-server.example.com" c.SSECustomerKey = validKey err := c.validateSSEConfig() if err == nil { t.Error("expected error for HTTP endpoint with SSE-C") } if !strings.Contains(err.Error(), "HTTPS") { t.Errorf("expected HTTPS requirement error, got: %v", err) } }) t.Run("LocalhostHTTPAllowed", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.Endpoint = "http://localhost:9000" c.SSECustomerKey = validKey err := c.validateSSEConfig() if err != nil { t.Errorf("expected localhost HTTP to be allowed, got: %v", err) } }) t.Run("127.0.0.1HTTPAllowed", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.Endpoint = "http://127.0.0.1:9000" c.SSECustomerKey = validKey err := c.validateSSEConfig() if err != nil { t.Errorf("expected 127.0.0.1 HTTP to be allowed, got: %v", err) } }) t.Run("PrivateNetworkHTTPAllowed", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.Endpoint = "http://192.168.1.100:9000" c.SSECustomerKey = validKey err := c.validateSSEConfig() if err != nil { t.Errorf("expected private network HTTP to be allowed, got: %v", err) } }) t.Run("PrivateNetwork172RangeHTTPAllowed", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.Endpoint = "http://172.17.0.2:9000" c.SSECustomerKey = validKey err := c.validateSSEConfig() if err != nil { t.Errorf("expected 172.x private network HTTP to be allowed, got: %v", err) } }) t.Run("PrivateNetwork10RangeHTTPAllowed", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.Endpoint = "http://10.0.0.5:9000" c.SSECustomerKey = validKey err := c.validateSSEConfig() if err != nil { t.Errorf("expected 10.x private network HTTP to be allowed, got: %v", err) } }) } // TestReplicaClient_SSE_KMS_Configuration tests SSE-KMS configuration func TestReplicaClient_SSE_KMS_Configuration(t *testing.T) { t.Run("ValidKMSKeyID", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.SSEKMSKeyID = "arn:aws:kms:us-east-1:123456789:key/12345678-1234-1234-1234-123456789012" err := c.validateSSEConfig() if err != nil { t.Errorf("expected no error for valid KMS key ID, got: %v", err) } }) t.Run("KMSKeyAlias", func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.SSEKMSKeyID = "alias/my-key" err := c.validateSSEConfig() if err != nil { t.Errorf("expected no error for KMS key alias, got: %v", err) } }) } // TestReplicaClient_SSE_C_Headers tests that SSE-C headers are passed to S3 operations func TestReplicaClient_SSE_C_Headers(t *testing.T) { validKey := base64.StdEncoding.EncodeToString([]byte("12345678901234567890123456789012")) keyBytes, _ := base64.StdEncoding.DecodeString(validKey) keyMD5Sum := md5.Sum(keyBytes) expectedMD5 := base64.StdEncoding.EncodeToString(keyMD5Sum[:]) data := mustLTX(t) t.Run("WriteLTXFile_SSEC", func(t *testing.T) { headers := make(chan http.Header, 1) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() _, _ = io.Copy(io.Discard, r.Body) if r.Method == http.MethodPut { select { case headers <- r.Header.Clone(): default: } w.Header().Set("ETag", `"test-etag"`) w.WriteHeader(http.StatusOK) return } w.WriteHeader(http.StatusOK) })) defer server.Close() client := NewReplicaClient() client.Bucket = "test-bucket" client.Path = "replica" client.Region = "us-east-1" client.Endpoint = server.URL client.ForcePathStyle = true client.AccessKeyID = "test-access-key" client.SecretAccessKey = "test-secret-key" client.SSECustomerKey = validKey ctx := context.Background() if err := client.Init(ctx); err != nil { t.Fatalf("Init() error: %v", err) } if _, err := client.WriteLTXFile(ctx, 0, 2, 2, bytes.NewReader(data)); err != nil { t.Fatalf("WriteLTXFile() error: %v", err) } select { case hdr := <-headers: if got := hdr.Get("x-amz-server-side-encryption-customer-algorithm"); got != "AES256" { t.Errorf("SSE-C algorithm header = %q, want AES256", got) } if got := hdr.Get("x-amz-server-side-encryption-customer-key"); got != validKey { t.Errorf("SSE-C key header = %q, want %q", got, validKey) } if got := hdr.Get("x-amz-server-side-encryption-customer-key-md5"); got != expectedMD5 { t.Errorf("SSE-C key MD5 header = %q, want %q", got, expectedMD5) } case <-time.After(time.Second): t.Fatal("timeout waiting for PUT request") } }) t.Run("OpenLTXFile_SSEC", func(t *testing.T) { headers := make(chan http.Header, 1) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { select { case headers <- r.Header.Clone(): default: } w.Header().Set("Content-Length", "100") w.WriteHeader(http.StatusOK) w.Write([]byte("test-data")) return } w.WriteHeader(http.StatusOK) })) defer server.Close() client := NewReplicaClient() client.Bucket = "test-bucket" client.Path = "replica" client.Region = "us-east-1" client.Endpoint = server.URL client.ForcePathStyle = true client.AccessKeyID = "test-access-key" client.SecretAccessKey = "test-secret-key" client.SSECustomerKey = validKey ctx := context.Background() if err := client.Init(ctx); err != nil { t.Fatalf("Init() error: %v", err) } rc, err := client.OpenLTXFile(ctx, 0, 2, 2, 0, 0) if err != nil { t.Fatalf("OpenLTXFile() error: %v", err) } rc.Close() select { case hdr := <-headers: if got := hdr.Get("x-amz-server-side-encryption-customer-algorithm"); got != "AES256" { t.Errorf("SSE-C algorithm header = %q, want AES256", got) } if got := hdr.Get("x-amz-server-side-encryption-customer-key"); got != validKey { t.Errorf("SSE-C key header = %q, want %q", got, validKey) } if got := hdr.Get("x-amz-server-side-encryption-customer-key-md5"); got != expectedMD5 { t.Errorf("SSE-C key MD5 header = %q, want %q", got, expectedMD5) } case <-time.After(time.Second): t.Fatal("timeout waiting for GET request") } }) } // TestReplicaClient_SSE_KMS_Headers tests that SSE-KMS headers are passed to write operations func TestReplicaClient_SSE_KMS_Headers(t *testing.T) { kmsKeyID := "arn:aws:kms:us-east-1:123456789:key/12345678-1234-1234-1234-123456789012" data := mustLTX(t) headers := make(chan http.Header, 1) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() _, _ = io.Copy(io.Discard, r.Body) if r.Method == http.MethodPut { select { case headers <- r.Header.Clone(): default: } w.Header().Set("ETag", `"test-etag"`) w.WriteHeader(http.StatusOK) return } w.WriteHeader(http.StatusOK) })) defer server.Close() client := NewReplicaClient() client.Bucket = "test-bucket" client.Path = "replica" client.Region = "us-east-1" client.Endpoint = server.URL client.ForcePathStyle = true client.AccessKeyID = "test-access-key" client.SecretAccessKey = "test-secret-key" client.SSEKMSKeyID = kmsKeyID ctx := context.Background() if err := client.Init(ctx); err != nil { t.Fatalf("Init() error: %v", err) } if _, err := client.WriteLTXFile(ctx, 0, 2, 2, bytes.NewReader(data)); err != nil { t.Fatalf("WriteLTXFile() error: %v", err) } select { case hdr := <-headers: if got := hdr.Get("x-amz-server-side-encryption"); got != "aws:kms" { t.Errorf("SSE-KMS encryption header = %q, want aws:kms", got) } if got := hdr.Get("x-amz-server-side-encryption-aws-kms-key-id"); got != kmsKeyID { t.Errorf("SSE-KMS key ID header = %q, want %q", got, kmsKeyID) } case <-time.After(time.Second): t.Fatal("timeout waiting for PUT request") } } // TestReplicaClient_NoSSE_Headers tests that no SSE headers are sent when SSE is not configured func TestReplicaClient_NoSSE_Headers(t *testing.T) { data := mustLTX(t) headers := make(chan http.Header, 1) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() _, _ = io.Copy(io.Discard, r.Body) if r.Method == http.MethodPut { select { case headers <- r.Header.Clone(): default: } w.Header().Set("ETag", `"test-etag"`) w.WriteHeader(http.StatusOK) return } w.WriteHeader(http.StatusOK) })) defer server.Close() client := NewReplicaClient() client.Bucket = "test-bucket" client.Path = "replica" client.Region = "us-east-1" client.Endpoint = server.URL client.ForcePathStyle = true client.AccessKeyID = "test-access-key" client.SecretAccessKey = "test-secret-key" // No SSE configuration ctx := context.Background() if err := client.Init(ctx); err != nil { t.Fatalf("Init() error: %v", err) } if _, err := client.WriteLTXFile(ctx, 0, 2, 2, bytes.NewReader(data)); err != nil { t.Fatalf("WriteLTXFile() error: %v", err) } select { case hdr := <-headers: if got := hdr.Get("x-amz-server-side-encryption-customer-algorithm"); got != "" { t.Errorf("unexpected SSE-C algorithm header: %q", got) } if got := hdr.Get("x-amz-server-side-encryption-customer-key"); got != "" { t.Errorf("unexpected SSE-C key header: %q", got) } if got := hdr.Get("x-amz-server-side-encryption"); got != "" { t.Errorf("unexpected SSE-KMS header: %q", got) } case <-time.After(time.Second): t.Fatal("timeout waiting for PUT request") } } // TestReplicaClient_R2ConcurrencyDefault tests that Cloudflare R2 endpoints get // Concurrency=2 by default to avoid their strict concurrent upload limits. // This is a regression test for issue #948. func TestReplicaClient_R2ConcurrencyDefault(t *testing.T) { tests := []struct { name string url string wantConcurrency int }{ { name: "R2_DefaultConcurrency", url: "s3://mybucket/path?endpoint=https://account123.r2.cloudflarestorage.com", wantConcurrency: 2, }, { name: "AWS_NoConcurrencyOverride", url: "s3://mybucket/path", wantConcurrency: 0, }, { name: "MinIO_NoConcurrencyOverride", url: "s3://mybucket/path?endpoint=http://localhost:9000", wantConcurrency: 0, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL(tt.url) if err != nil { t.Fatalf("NewReplicaClientFromURL() error: %v", err) } c := client.(*ReplicaClient) if c.Concurrency != tt.wantConcurrency { t.Errorf("Concurrency = %d, want %d", c.Concurrency, tt.wantConcurrency) } }) } } // TestReplicaClient_ProviderEndpointDetection tests the endpoint detection functions // used to apply provider-specific defaults. func TestReplicaClient_ProviderEndpointDetection(t *testing.T) { tests := []struct { name string endpoint string wantR2 bool wantB2 bool wantDO bool }{ { name: "CloudflareR2", endpoint: "https://accountid.r2.cloudflarestorage.com", wantR2: true, }, { name: "CloudflareR2_HTTP", endpoint: "http://accountid.r2.cloudflarestorage.com", wantR2: true, }, { name: "BackblazeB2", endpoint: "https://s3.us-west-002.backblazeb2.com", wantB2: true, }, { name: "DigitalOcean", endpoint: "https://sgp1.digitaloceanspaces.com", wantDO: true, }, { name: "AWS_S3", endpoint: "", }, { name: "MinIO", endpoint: "http://localhost:9000", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := litestream.IsCloudflareR2Endpoint(tt.endpoint); got != tt.wantR2 { t.Errorf("IsCloudflareR2Endpoint() = %v, want %v", got, tt.wantR2) } if got := litestream.IsBackblazeEndpoint(tt.endpoint); got != tt.wantB2 { t.Errorf("IsBackblazeEndpoint() = %v, want %v", got, tt.wantB2) } if got := litestream.IsDigitalOceanEndpoint(tt.endpoint); got != tt.wantDO { t.Errorf("IsDigitalOceanEndpoint() = %v, want %v", got, tt.wantDO) } }) } } // TestReplicaClient_CustomEndpoint_DisablesChecksumFeatures tests that custom endpoints // (non-AWS S3) have SDK checksum features disabled to avoid aws-chunked encoding issues. // This addresses issues #895, #912, #940, #941, #947 where S3-compatible providers // don't support aws-chunked encoding or streaming checksums. func TestReplicaClient_CustomEndpoint_DisablesChecksumFeatures(t *testing.T) { tests := []struct { name string endpoint string wantChecksumCalc string wantChecksumValid string expectCustomConfig bool }{ { name: "AWS_S3_NoCustomConfig", endpoint: "", expectCustomConfig: false, }, { name: "R2_DisablesChecksums", endpoint: "https://account.r2.cloudflarestorage.com", expectCustomConfig: true, }, { name: "B2_DisablesChecksums", endpoint: "https://s3.us-west-002.backblazeb2.com", expectCustomConfig: true, }, { name: "MinIO_DisablesChecksums", endpoint: "http://localhost:9000", expectCustomConfig: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { c := NewReplicaClient() c.Bucket = "test-bucket" c.Region = "us-east-1" c.Endpoint = tt.endpoint c.ForcePathStyle = true c.AccessKeyID = "test" c.SecretAccessKey = "test" hasCustomEndpoint := c.Endpoint != "" if hasCustomEndpoint != tt.expectCustomConfig { t.Errorf("custom endpoint detection = %v, want %v", hasCustomEndpoint, tt.expectCustomConfig) } }) } } func TestNewReplicaClientFromURL_QueryParamAliases(t *testing.T) { tests := []struct { name string url string wantForcePathStyle bool wantSkipVerify bool wantConcurrency int wantPartSize int64 }{ { name: "forcePathStyle_camelCase", url: "s3://mybucket/path?forcePathStyle=true", wantForcePathStyle: true, }, { name: "force-path-style_hyphenated", url: "s3://mybucket/path?force-path-style=true", wantForcePathStyle: true, }, { name: "force-path-style_false", url: "s3://mybucket/path?endpoint=http://localhost:9000&force-path-style=false", wantForcePathStyle: false, }, { name: "skipVerify_camelCase", url: "s3://mybucket/path?skipVerify=true", wantSkipVerify: true, }, { name: "skip-verify_hyphenated", url: "s3://mybucket/path?skip-verify=true", wantSkipVerify: true, }, { name: "concurrency_url_param", url: "s3://mybucket/path?concurrency=3", wantConcurrency: 3, }, { name: "part-size_hyphenated", url: "s3://mybucket/path?part-size=10485760", wantPartSize: 10485760, }, { name: "partSize_camelCase", url: "s3://mybucket/path?partSize=10485760", wantPartSize: 10485760, }, { name: "all_params_combined", url: "s3://mybucket/path?force-path-style=true&skip-verify=true&concurrency=4&part-size=8388608", wantForcePathStyle: true, wantSkipVerify: true, wantConcurrency: 4, wantPartSize: 8388608, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { client, err := litestream.NewReplicaClientFromURL(tt.url) if err != nil { t.Fatalf("NewReplicaClientFromURL() error: %v", err) } c := client.(*ReplicaClient) if c.ForcePathStyle != tt.wantForcePathStyle { t.Errorf("ForcePathStyle = %v, want %v", c.ForcePathStyle, tt.wantForcePathStyle) } if c.SkipVerify != tt.wantSkipVerify { t.Errorf("SkipVerify = %v, want %v", c.SkipVerify, tt.wantSkipVerify) } if c.Concurrency != tt.wantConcurrency { t.Errorf("Concurrency = %d, want %d", c.Concurrency, tt.wantConcurrency) } if c.PartSize != tt.wantPartSize { t.Errorf("PartSize = %d, want %d", c.PartSize, tt.wantPartSize) } }) } } func TestNewReplicaClientFromURL_EndpointEnvVar(t *testing.T) { tests := []struct { name string url string envEndpoint string wantEndpoint string wantForcePathStyle bool }{ { name: "env_var_sets_endpoint", url: "s3://mybucket/path", envEndpoint: "http://localhost:9000", wantEndpoint: "http://localhost:9000", wantForcePathStyle: true, }, { name: "env_var_adds_https_scheme", url: "s3://mybucket/path", envEndpoint: "s3.example.com", wantEndpoint: "https://s3.example.com", wantForcePathStyle: true, }, { name: "query_param_overrides_env_var", url: "s3://mybucket/path?endpoint=http://other:9000", envEndpoint: "http://localhost:9000", wantEndpoint: "http://other:9000", wantForcePathStyle: true, }, { name: "env_var_respects_force_path_style_false", url: "s3://mybucket/path?force-path-style=false", envEndpoint: "http://localhost:9000", wantEndpoint: "http://localhost:9000", wantForcePathStyle: false, }, { name: "no_env_var_no_endpoint", url: "s3://mybucket/path", envEndpoint: "", wantEndpoint: "", wantForcePathStyle: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Setenv("LITESTREAM_S3_ENDPOINT", tt.envEndpoint) client, err := litestream.NewReplicaClientFromURL(tt.url) if err != nil { t.Fatalf("NewReplicaClientFromURL() error: %v", err) } c := client.(*ReplicaClient) if c.Endpoint != tt.wantEndpoint { t.Errorf("Endpoint = %q, want %q", c.Endpoint, tt.wantEndpoint) } if c.ForcePathStyle != tt.wantForcePathStyle { t.Errorf("ForcePathStyle = %v, want %v", c.ForcePathStyle, tt.wantForcePathStyle) } }) } } ================================================ FILE: scripts/README.md ================================================ # Utility Scripts Utility scripts for Litestream testing and distribution. ## Overview This directory contains utility scripts for post-test analysis and packaging. All long-running soak tests have been migrated to Go integration tests in `tests/integration/`. > **Note:** For all soak tests (2-8 hours), see the Go-based test suite in [tests/integration/](../tests/integration/README.md). The bash soak tests have been migrated to Go for better maintainability and cross-platform support ## Prerequisites ```bash go build -o bin/litestream ./cmd/litestream go build -o bin/litestream-test ./cmd/litestream-test ``` ## Available Scripts ### analyze-test-results.sh Post-test analysis tool for examining overnight test results. ```bash ./scripts/analyze-test-results.sh /tmp/litestream-overnight- ``` **Analyzes:** 1. Test duration and timeline 2. Compaction statistics 3. Checkpoint frequency 4. Error analysis and categorization 5. Performance metrics 6. Database growth patterns 7. Replica file statistics 8. Final validation results **Output:** - Analysis report written to `/analysis-report.txt` - Console summary with key findings - Error categorization and counts - Performance statistics **What it Reports:** - Total test duration - Number of compactions by interval - Checkpoint count and frequency - Error types and severity - Database size growth - WAL file patterns - Replica size and file counts - Success/failure summary **Use Cases:** - Post-overnight-test analysis - Comparing test runs - Identifying performance trends - Debugging test failures - Documenting test results ### setup-homebrew-tap.sh Homebrew tap setup script for packaging and distribution. ```bash ./scripts/setup-homebrew-tap.sh ``` **Purpose:** Automates Homebrew tap setup for Litestream distribution. Not a test script per se, but part of the release process. ## Usage ### Analyzing Test Results ```bash ls /tmp/litestream-overnight-* -dt | head -1 ./scripts/analyze-test-results.sh $(ls /tmp/litestream-overnight-* -dt | head -1) ``` ## Test Duration Guide | Duration | Use Case | Test Type | Expected Results | |----------|----------|-----------|------------------| | 5 minutes | CI/CD smoke test | Go integration tests | Basic functionality | | 30 minutes | Short integration | Go integration tests | Pattern detection | | 2-8 hours | Soak testing | Go soak tests (local only) | Full validation | > **Note:** All soak tests are now Go-based in `tests/integration/`. See [tests/integration/README.md](../tests/integration/README.md) for details on running comprehensive, MinIO, and overnight S3 soak tests. ## Monitoring and Debugging ### Real-time Monitoring All tests create timestamped directories in `/tmp/`: ```bash LATEST=$(ls /tmp/litestream-* -dt | head -1) tail -f $LATEST/logs/monitor.log tail -f $LATEST/logs/litestream.log ``` ### Key Metrics to Watch **Database Growth:** - Should grow steadily - WAL file size should cycle (grow, checkpoint, reset) **Replica Statistics:** - Snapshot count should increase over time - LTX file count should grow then stabilize (compaction) - Replica size should be similar to database size **Operations:** - Compactions should occur at scheduled intervals - Checkpoints should happen regularly - Sync operations should complete successfully **Errors:** - Should be minimal or zero - Transient errors OK if recovered - Persistent errors indicate issues ### Common Issues #### Test Fails to Start Check binaries: ```bash ls -la bin/litestream bin/litestream-test ``` Rebuild if needed: ```bash go build -o bin/litestream ./cmd/litestream go build -o bin/litestream-test ./cmd/litestream-test ``` #### S3 Test Fails Verify credentials: ```bash aws s3 ls s3://$S3_BUCKET/ ``` Check environment variables: ```bash echo $AWS_ACCESS_KEY_ID echo $AWS_SECRET_ACCESS_KEY echo $S3_BUCKET ``` #### High Error Counts Check log for error details: ```bash grep -i error /tmp/litestream-*/logs/litestream.log | head -20 ``` #### Validation Fails Compare databases manually: ```bash sqlite3 /tmp/litestream-*/test.db "SELECT COUNT(*) FROM test_data" sqlite3 /tmp/litestream-*/restored.db "SELECT COUNT(*) FROM test_data" ``` ### Stopping Tests Early Go tests can be interrupted with Ctrl+C. They will cleanup gracefully via defer statements. ## Test Artifacts All tests create timestamped directories with comprehensive artifacts: ``` /tmp/litestream-overnight-/ ├── logs/ │ ├── litestream.log # Litestream replication log │ ├── load.log # Load generator log │ ├── monitor.log # Real-time monitoring log │ ├── populate.log # Initial population log │ └── validate.log # Final validation log ├── test.db # Source database ├── test.db-wal # Write-ahead log ├── test.db-shm # Shared memory file ├── replica/ # Replica directory (file tests) │ └── ltx/ # LTX files └── restored.db # Restored database for validation ``` ## Integration with Go Tests These utility scripts complement the Go integration test suite: **Test Locations:** - `tests/integration/` → All integration and soak tests (Go-based) - `cmd/litestream-test/scripts/` → Scenario and debugging tests (bash, being phased out) - `scripts/` → Utilities only (this directory) **Testing Workflow:** 1. Run quick integration tests during development 2. Run full integration test suite before major changes 3. Run soak tests (2-8h) locally before releases: `TestComprehensiveSoak`, `TestMinIOSoak`, `TestOvernightS3Soak` 4. Analyze results with `analyze-test-results.sh` ## Related Documentation - [Go Integration Tests](../tests/integration/README.md) - Complete Go-based test suite including soak tests - [litestream-test CLI Tool](../cmd/litestream-test/README.md) - Testing harness documentation - [Scenario Test Scripts](../cmd/litestream-test/scripts/README.md) - Focused test scenarios - [S3 Retention Testing](../cmd/litestream-test/S3-RETENTION-TESTING.md) - S3-specific testing ================================================ FILE: scripts/analyze-test-results.sh ================================================ #!/bin/bash set -euo pipefail if [ $# -lt 1 ]; then echo "Usage: $0 " echo "" echo "Analyzes overnight test results from the specified test directory." echo "" echo "Example:" echo " $0 /tmp/litestream-overnight-20240924-120000" exit 1 fi TEST_DIR="$1" if [ ! -d "$TEST_DIR" ]; then echo "Error: Test directory does not exist: $TEST_DIR" exit 1 fi LOG_DIR="$TEST_DIR/logs" ANALYSIS_REPORT="$TEST_DIR/analysis-report.txt" echo "================================================" echo "Litestream Test Analysis Report" echo "================================================" echo "Test directory: $TEST_DIR" echo "Analysis time: $(date)" echo "" { echo "================================================" echo "Litestream Test Analysis Report" echo "================================================" echo "Test directory: $TEST_DIR" echo "Analysis time: $(date)" echo "" echo "1. TEST DURATION AND TIMELINE" echo "==============================" if [ -f "$LOG_DIR/litestream.log" ]; then START_TIME=$(head -1 "$LOG_DIR/litestream.log" 2>/dev/null | grep -oE '[0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}' | head -1 || echo "Unknown") END_TIME=$(tail -1 "$LOG_DIR/litestream.log" 2>/dev/null | grep -oE '[0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}' | head -1 || echo "Unknown") echo "Start time: $START_TIME" echo "End time: $END_TIME" # Calculate duration if possible if command -v python3 >/dev/null 2>&1; then DURATION=$(python3 -c " from datetime import datetime try: start = datetime.strptime('$START_TIME', '%Y/%m/%d %H:%M:%S') end = datetime.strptime('$END_TIME', '%Y/%m/%d %H:%M:%S') duration = end - start hours = duration.total_seconds() / 3600 print(f'Duration: {hours:.2f} hours') except: print('Duration: Unable to calculate') " 2>/dev/null || echo "Duration: Unable to calculate") echo "$DURATION" fi fi echo "" echo "2. DATABASE STATISTICS" echo "======================" if [ -f "$TEST_DIR/test.db" ]; then DB_SIZE=$(stat -f%z "$TEST_DIR/test.db" 2>/dev/null || stat -c%s "$TEST_DIR/test.db" 2>/dev/null || echo "0") echo "Final database size: $(numfmt --to=iec-i --suffix=B $DB_SIZE 2>/dev/null || echo "$DB_SIZE bytes")" # Get row count if database is accessible ROW_COUNT=$(sqlite3 "$TEST_DIR/test.db" "SELECT COUNT(*) FROM test_data" 2>/dev/null || echo "Unknown") echo "Total rows inserted: $ROW_COUNT" # Get page statistics PAGE_COUNT=$(sqlite3 "$TEST_DIR/test.db" "PRAGMA page_count" 2>/dev/null || echo "Unknown") PAGE_SIZE=$(sqlite3 "$TEST_DIR/test.db" "PRAGMA page_size" 2>/dev/null || echo "Unknown") echo "Database pages: $PAGE_COUNT (page size: $PAGE_SIZE bytes)" fi echo "" echo "3. REPLICATION STATISTICS" echo "=========================" if [ -d "$TEST_DIR/replica" ]; then SNAPSHOT_COUNT=$(find "$TEST_DIR/replica" -name "*.snapshot.lz4" 2>/dev/null | wc -l | tr -d ' ') WAL_COUNT=$(find "$TEST_DIR/replica" -name "*.wal.lz4" 2>/dev/null | wc -l | tr -d ' ') REPLICA_SIZE=$(du -sh "$TEST_DIR/replica" 2>/dev/null | cut -f1) echo "Snapshots created: $SNAPSHOT_COUNT" echo "WAL segments created: $WAL_COUNT" echo "Total replica size: $REPLICA_SIZE" # Analyze snapshot intervals if [ "$SNAPSHOT_COUNT" -gt 1 ]; then echo "" echo "Snapshot creation times:" find "$TEST_DIR/replica" -name "*.snapshot.lz4" -exec stat -f "%Sm" -t "%Y-%m-%d %H:%M:%S" {} \; 2>/dev/null | sort || \ find "$TEST_DIR/replica" -name "*.snapshot.lz4" -exec stat -c "%y" {} \; 2>/dev/null | cut -d. -f1 | sort || echo "Unable to get timestamps" fi fi echo "" echo "4. COMPACTION ANALYSIS" echo "======================" if [ -f "$LOG_DIR/litestream.log" ]; then COMPACTION_COUNT=$(grep -c "compacting" "$LOG_DIR/litestream.log" 2>/dev/null || echo "0") echo "Total compaction operations: $COMPACTION_COUNT" # Count compactions by level echo "" echo "Compactions by retention level:" grep "compacting" "$LOG_DIR/litestream.log" 2>/dev/null | grep -oE "retention=[0-9]+[hms]+" | sort | uniq -c | sort -rn || echo "No compaction data found" # Show compaction timing patterns echo "" echo "Compaction frequency (last 10):" grep "compacting" "$LOG_DIR/litestream.log" 2>/dev/null | tail -10 | grep -oE "[0-9]{2}:[0-9]{2}:[0-9]{2}" || echo "No timing data" fi echo "" echo "5. LOAD GENERATOR PERFORMANCE" echo "=============================" if [ -f "$LOG_DIR/load.log" ]; then # Extract final statistics FINAL_STATS=$(tail -20 "$LOG_DIR/load.log" | grep "Load generation complete" -A 10 || echo "") if [ -n "$FINAL_STATS" ]; then echo "$FINAL_STATS" else # Try to get statistics from progress logs echo "Load generator statistics:" grep "Load statistics" "$LOG_DIR/load.log" | tail -5 || echo "No statistics found" fi fi echo "" echo "6. ERROR ANALYSIS" echo "=================" ERROR_COUNT=0 WARNING_COUNT=0 if [ -f "$LOG_DIR/litestream.log" ]; then ERROR_COUNT=$(grep -ic "ERROR\|error" "$LOG_DIR/litestream.log" 2>/dev/null || echo "0") WARNING_COUNT=$(grep -ic "WARN\|warning" "$LOG_DIR/litestream.log" 2>/dev/null || echo "0") echo "Total errors: $ERROR_COUNT" echo "Total warnings: $WARNING_COUNT" if [ "$ERROR_COUNT" -gt 0 ]; then echo "" echo "Error types:" grep -i "ERROR\|error" "$LOG_DIR/litestream.log" | sed 's/.*ERROR[: ]*//' | cut -d' ' -f1-5 | sort | uniq -c | sort -rn | head -10 fi # Check for specific issues echo "" echo "Specific issues detected:" BUSY_ERRORS=$(grep -c "database is locked\|SQLITE_BUSY" "$LOG_DIR/litestream.log" 2>/dev/null || echo "0") TIMEOUT_ERRORS=$(grep -c "timeout\|timed out" "$LOG_DIR/litestream.log" 2>/dev/null || echo "0") S3_ERRORS=$(grep -c "S3\|AWS\|403\|404\|500\|503" "$LOG_DIR/litestream.log" 2>/dev/null || echo "0") [ "$BUSY_ERRORS" -gt 0 ] && echo " - Database busy/locked errors: $BUSY_ERRORS" [ "$TIMEOUT_ERRORS" -gt 0 ] && echo " - Timeout errors: $TIMEOUT_ERRORS" [ "$S3_ERRORS" -gt 0 ] && echo " - S3/AWS errors: $S3_ERRORS" fi echo "" echo "7. CHECKPOINT ANALYSIS" echo "======================" if [ -f "$LOG_DIR/litestream.log" ]; then CHECKPOINT_COUNT=$(grep -c "checkpoint" "$LOG_DIR/litestream.log" 2>/dev/null || echo "0") echo "Total checkpoint operations: $CHECKPOINT_COUNT" # Analyze checkpoint performance echo "" echo "Checkpoint timing (last 10):" grep "checkpoint" "$LOG_DIR/litestream.log" 2>/dev/null | tail -10 | grep -oE "[0-9]{2}:[0-9]{2}:[0-9]{2}" || echo "No checkpoint data" fi echo "" echo "8. VALIDATION RESULTS" echo "====================" if [ -f "$LOG_DIR/validate.log" ]; then echo "Validation output:" cat "$LOG_DIR/validate.log" elif [ -f "$LOG_DIR/restore.log" ]; then echo "Restoration test results:" tail -20 "$LOG_DIR/restore.log" else echo "No validation/restoration data found" fi echo "" echo "9. RESOURCE USAGE" echo "================" if [ -f "$LOG_DIR/monitor.log" ]; then echo "Peak values from monitoring:" # Extract peak database size MAX_DB_SIZE=$(grep "Database size:" "$LOG_DIR/monitor.log" | grep -oE "[0-9]+[KMG]?i?B" | sort -h | tail -1 || echo "Unknown") echo " Peak database size: $MAX_DB_SIZE" # Extract peak WAL size MAX_WAL_SIZE=$(grep "WAL size:" "$LOG_DIR/monitor.log" | grep -oE "[0-9]+[KMG]?i?B" | sort -h | tail -1 || echo "Unknown") echo " Peak WAL size: $MAX_WAL_SIZE" # Extract max WAL segments MAX_WAL_SEGS=$(grep "WAL segments (total):" "$LOG_DIR/monitor.log" | grep -oE "[0-9]+" | sort -n | tail -1 || echo "Unknown") echo " Max WAL segments: $MAX_WAL_SEGS" fi echo "" echo "10. SUMMARY AND RECOMMENDATIONS" echo "===============================" # Analyze test success TEST_SUCCESS=true ISSUES=() if [ "$ERROR_COUNT" -gt 100 ]; then TEST_SUCCESS=false ISSUES+=("High error count ($ERROR_COUNT errors)") fi if [ -f "$LOG_DIR/validate.log" ] && grep -q "failed\|error" "$LOG_DIR/validate.log" 2>/dev/null; then TEST_SUCCESS=false ISSUES+=("Validation failed") fi if [ -f "$LOG_DIR/litestream.log" ] && ! grep -q "compacting" "$LOG_DIR/litestream.log" 2>/dev/null; then ISSUES+=("No compaction operations detected") fi if [ "$TEST_SUCCESS" = true ] && [ ${#ISSUES[@]} -eq 0 ]; then echo "✓ Test completed successfully!" echo "" echo "Key achievements:" echo " - Ran for intended duration" echo " - Successfully created $SNAPSHOT_COUNT snapshots" echo " - Performed $COMPACTION_COUNT compaction operations" echo " - Processed $ROW_COUNT database rows" else echo "⚠ Test completed with issues:" for issue in "${ISSUES[@]}"; do echo " - $issue" done fi echo "" echo "Recommendations:" if [ "$ERROR_COUNT" -gt 50 ]; then echo " - Investigate error patterns, particularly around resource contention" fi if [ "$COMPACTION_COUNT" -lt 10 ]; then echo " - Verify compaction configuration is working as expected" fi if [ "$BUSY_ERRORS" -gt 10 ]; then echo " - Consider adjusting checkpoint intervals or busy timeout settings" fi echo "" echo "Test artifacts location: $TEST_DIR" } | tee "$ANALYSIS_REPORT" echo "" echo "================================================" echo "Analysis complete!" echo "Report saved to: $ANALYSIS_REPORT" echo "================================================" ================================================ FILE: scripts/run-upgrade-tests.sh ================================================ #!/bin/bash set -euo pipefail # Run the v0.3.x → v0.5.x upgrade integration tests locally. # Downloads the v0.3.13 binary if not already cached, builds the current # binary, and runs the Go integration test. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" CACHE_DIR="$ROOT_DIR/.cache/litestream-v3" V3_BIN="$CACHE_DIR/litestream" # Detect platform. OS="$(uname -s | tr '[:upper:]' '[:lower:]')" ARCH="$(uname -m)" case "$ARCH" in x86_64) ARCH="amd64" ;; aarch64) ARCH="arm64" ;; arm64) ARCH="arm64" ;; *) echo "Unsupported architecture: $ARCH" exit 1 ;; esac # Download v0.3.13 binary if not cached. if [ ! -x "$V3_BIN" ]; then echo "Downloading Litestream v0.3.13 (${OS}-${ARCH})..." mkdir -p "$CACHE_DIR" case "$OS" in darwin) ASSET="litestream-v0.3.13-darwin-${ARCH}.zip" gh release download v0.3.13 --repo benbjohnson/litestream --pattern "$ASSET" --dir "$CACHE_DIR" --clobber unzip -o "$CACHE_DIR/$ASSET" -d "$CACHE_DIR" rm -f "$CACHE_DIR/$ASSET" ;; linux) ASSET="litestream-v0.3.13-linux-${ARCH}.tar.gz" gh release download v0.3.13 --repo benbjohnson/litestream --pattern "$ASSET" --dir "$CACHE_DIR" --clobber tar -xzf "$CACHE_DIR/$ASSET" -C "$CACHE_DIR" rm -f "$CACHE_DIR/$ASSET" ;; *) echo "Unsupported OS: $OS" exit 1 ;; esac chmod +x "$V3_BIN" echo "Cached v0.3.13 binary at $V3_BIN" else echo "Using cached v0.3.13 binary at $V3_BIN" fi echo "v0.3.13 version: $("$V3_BIN" version)" # Build current binaries. echo "Building current binaries..." cd "$ROOT_DIR" go build -o bin/litestream ./cmd/litestream go build -o bin/litestream-test ./cmd/litestream-test echo "Current version: $(./bin/litestream version)" # Run upgrade tests. echo "" echo "Running upgrade integration tests..." LITESTREAM_V3_BIN="$V3_BIN" CGO_ENABLED=1 \ go test -v -tags=integration -timeout=10m ./tests/integration/... -run=TestUpgrade "$@" ================================================ FILE: scripts/setup-homebrew-tap.sh ================================================ #!/bin/bash set -e echo "Setting up Homebrew tap repository for Litestream..." REPO_NAME="homebrew-litestream" GITHUB_USER="benbjohnson" echo "This script will help you create the ${GITHUB_USER}/${REPO_NAME} repository." echo "" echo "Prerequisites:" echo "1. GitHub CLI (gh) must be installed and authenticated" echo "2. You must have permission to create repositories under ${GITHUB_USER}" echo "" read -p "Do you want to continue? (y/n) " -n 1 -r echo "" if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Aborted." exit 1 fi echo "Creating repository ${GITHUB_USER}/${REPO_NAME}..." gh repo create ${GITHUB_USER}/${REPO_NAME} \ --public \ --description "Homebrew tap for Litestream" \ --clone=false || echo "Repository may already exist, continuing..." echo "" echo "Cloning repository..." TEMP_DIR=$(mktemp -d) cd "$TEMP_DIR" gh repo clone ${GITHUB_USER}/${REPO_NAME} || git clone "https://github.com/${GITHUB_USER}/${REPO_NAME}.git" cd ${REPO_NAME} echo "Creating Formula directory..." mkdir -p Formula echo "Creating README..." cat > README.md << 'EOF' # Homebrew Tap for Litestream This is the official Homebrew tap for [Litestream](https://github.com/benbjohnson/litestream). ## Installation ```bash brew tap benbjohnson/litestream brew install litestream ``` ## Documentation For more information about Litestream, visit: - [GitHub Repository](https://github.com/benbjohnson/litestream) - [Official Documentation](https://litestream.io) ## License Apache License 2.0 EOF echo "Creating initial Formula placeholder..." cat > Formula/.gitkeep << 'EOF' # This file ensures the Formula directory is tracked by git # GoReleaser will automatically create and update formula files here EOF echo "Committing initial structure..." git add . git commit -m "Initial tap structure" || echo "Nothing to commit" echo "Pushing to GitHub..." git push origin main || git push origin master echo "" echo "✅ Homebrew tap repository setup complete!" echo "" echo "Repository: https://github.com/${GITHUB_USER}/${REPO_NAME}" echo "" echo "Next steps:" echo "1. Create a GitHub Personal Access Token with 'repo' scope" echo "2. Add it as HOMEBREW_TAP_GITHUB_TOKEN secret in the main repository" echo "3. GoReleaser will automatically update the tap on each release" ================================================ FILE: server.go ================================================ package litestream import ( "context" "encoding/json" "errors" "fmt" "log/slog" "net" "net/http" "net/http/pprof" "os" "sync" "time" ) // SocketConfig configures the Unix socket for control commands. type SocketConfig struct { Enabled bool `yaml:"enabled"` Path string `yaml:"path"` Permissions uint32 `yaml:"permissions"` } // DefaultSocketConfig returns the default socket configuration. func DefaultSocketConfig() SocketConfig { return SocketConfig{ Enabled: false, Path: "/var/run/litestream.sock", Permissions: 0600, } } // Server manages runtime control via Unix socket using HTTP. type Server struct { store *Store // SocketPath is the path to the Unix socket. SocketPath string // SocketPerms is the file permissions for the socket. SocketPerms uint32 // PathExpander optionally expands paths (e.g., ~ expansion). // If nil, paths are used as-is. PathExpander func(string) (string, error) // Version is the version string to report in /info. Version string // startedAt is set when the server starts. startedAt time.Time socketListener net.Listener httpServer *http.Server ctx context.Context cancel context.CancelFunc wg sync.WaitGroup logger *slog.Logger } // NewServer creates a new Server instance. func NewServer(store *Store) *Server { ctx, cancel := context.WithCancel(context.Background()) s := &Server{ store: store, SocketPerms: 0600, ctx: ctx, cancel: cancel, logger: slog.Default().With(LogKeySystem, LogSystemServer), } mux := http.NewServeMux() mux.HandleFunc("POST /start", s.handleStart) mux.HandleFunc("POST /stop", s.handleStop) mux.HandleFunc("GET /txid", s.handleTXID) mux.HandleFunc("POST /register", s.handleRegister) mux.HandleFunc("POST /unregister", s.handleUnregister) mux.HandleFunc("POST /sync", s.handleSync) mux.HandleFunc("GET /list", s.handleList) mux.HandleFunc("GET /info", s.handleInfo) // pprof endpoints mux.HandleFunc("GET /debug/pprof/", pprof.Index) mux.HandleFunc("GET /debug/pprof/cmdline", pprof.Cmdline) mux.HandleFunc("GET /debug/pprof/profile", pprof.Profile) mux.HandleFunc("GET /debug/pprof/symbol", pprof.Symbol) mux.HandleFunc("GET /debug/pprof/trace", pprof.Trace) s.httpServer = &http.Server{Handler: mux} return s } // Start begins listening for control connections. func (s *Server) Start() error { if s.SocketPath == "" { return fmt.Errorf("socket path required") } // Check if socket file exists and is actually a socket before removing if info, err := os.Lstat(s.SocketPath); err == nil { if info.Mode()&os.ModeSocket != 0 { if err := os.Remove(s.SocketPath); err != nil { return fmt.Errorf("remove existing socket: %w", err) } } else { return fmt.Errorf("socket path exists but is not a socket: %s", s.SocketPath) } } else if !os.IsNotExist(err) { return fmt.Errorf("check socket path: %w", err) } listener, err := net.Listen("unix", s.SocketPath) if err != nil { return fmt.Errorf("listen on unix socket: %w", err) } s.socketListener = listener if err := os.Chmod(s.SocketPath, os.FileMode(s.SocketPerms)); err != nil { listener.Close() return fmt.Errorf("chmod socket: %w", err) } // Set startedAt after successful socket setup to ensure uptime reflects // the actual time the server became available. s.startedAt = time.Now() s.logger.Info("control socket listening", "path", s.SocketPath) s.wg.Add(1) go func() { defer s.wg.Done() if err := s.httpServer.Serve(listener); err != nil && err != http.ErrServerClosed { s.logger.Error("http server error", "error", err) } }() return nil } // Close gracefully shuts down the control server. func (s *Server) Close() error { s.cancel() ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if s.httpServer != nil { if err := s.httpServer.Shutdown(ctx); err != nil { s.logger.Error("http server shutdown error", "error", err) } } s.wg.Wait() return nil } // expandPath expands the path using PathExpander if set. func (s *Server) expandPath(path string) (string, error) { if s.PathExpander != nil { return s.PathExpander(path) } return path, nil } func (s *Server) handleStart(w http.ResponseWriter, r *http.Request) { var req StartRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { writeJSONError(w, http.StatusBadRequest, "invalid request body", err.Error()) return } if req.Path == "" { writeJSONError(w, http.StatusBadRequest, "path required", nil) return } expandedPath, err := s.expandPath(req.Path) if err != nil { writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("invalid path: %v", err), nil) return } ctx := s.ctx if req.Timeout > 0 { var cancel context.CancelFunc ctx, cancel = context.WithTimeout(s.ctx, time.Duration(req.Timeout)*time.Second) defer cancel() } if err := s.store.EnableDB(ctx, expandedPath); err != nil { writeJSONError(w, http.StatusInternalServerError, err.Error(), nil) return } writeJSON(w, http.StatusOK, StartResponse{ Status: "started", Path: expandedPath, }) } func (s *Server) handleStop(w http.ResponseWriter, r *http.Request) { var req StopRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { writeJSONError(w, http.StatusBadRequest, "invalid request body", err.Error()) return } if req.Path == "" { writeJSONError(w, http.StatusBadRequest, "path required", nil) return } expandedPath, err := s.expandPath(req.Path) if err != nil { writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("invalid path: %v", err), nil) return } timeout := req.Timeout if timeout == 0 { timeout = 30 } ctx, cancel := context.WithTimeout(s.ctx, time.Duration(timeout)*time.Second) defer cancel() if err := s.store.DisableDB(ctx, expandedPath); err != nil { writeJSONError(w, http.StatusInternalServerError, err.Error(), nil) return } writeJSON(w, http.StatusOK, StopResponse{ Status: "stopped", Path: expandedPath, }) } func (s *Server) handleTXID(w http.ResponseWriter, r *http.Request) { path := r.URL.Query().Get("path") if path == "" { writeJSONError(w, http.StatusBadRequest, "path required", nil) return } expandedPath, err := s.expandPath(path) if err != nil { writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("invalid path: %v", err), nil) return } db := s.store.FindDB(expandedPath) if db == nil { writeJSONError(w, http.StatusNotFound, "database not found", nil) return } pos, err := db.Pos() if err != nil { writeJSONError(w, http.StatusInternalServerError, err.Error(), nil) return } writeJSON(w, http.StatusOK, TXIDResponse{ TXID: uint64(pos.TXID), }) } func writeJSON(w http.ResponseWriter, status int, v interface{}) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) json.NewEncoder(w).Encode(v) } func writeJSONError(w http.ResponseWriter, status int, message string, details interface{}) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) json.NewEncoder(w).Encode(ErrorResponse{ Error: message, Details: details, }) } // StartRequest is the request body for the /start endpoint. type StartRequest struct { Path string `json:"path"` Timeout int `json:"timeout,omitempty"` } // StartResponse is the response body for the /start endpoint. type StartResponse struct { Status string `json:"status"` Path string `json:"path"` } // StopRequest is the request body for the /stop endpoint. type StopRequest struct { Path string `json:"path"` Timeout int `json:"timeout,omitempty"` } // StopResponse is the response body for the /stop endpoint. type StopResponse struct { Status string `json:"status"` Path string `json:"path"` } // ErrorResponse is returned when an error occurs. type ErrorResponse struct { Error string `json:"error"` Details interface{} `json:"details,omitempty"` } // TXIDResponse is the response body for the /txid endpoint. type TXIDResponse struct { TXID uint64 `json:"txid"` } func (s *Server) handleSync(w http.ResponseWriter, r *http.Request) { var req SyncRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { writeJSONError(w, http.StatusBadRequest, "invalid request body", err.Error()) return } if req.Path == "" { writeJSONError(w, http.StatusBadRequest, "path required", nil) return } expandedPath, err := s.expandPath(req.Path) if err != nil { writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("invalid path: %v", err), nil) return } ctx := s.ctx if req.Wait && req.Timeout == 0 { req.Timeout = 30 } if req.Timeout > 0 { var cancel context.CancelFunc ctx, cancel = context.WithTimeout(s.ctx, time.Duration(req.Timeout)*time.Second) defer cancel() } result, err := s.store.SyncDB(ctx, expandedPath, req.Wait) if err != nil { switch { case errors.Is(err, ErrDatabaseNotFound): writeJSONError(w, http.StatusNotFound, err.Error(), nil) case errors.Is(err, ErrDatabaseNotOpen): writeJSONError(w, http.StatusConflict, err.Error(), nil) default: writeJSONError(w, http.StatusInternalServerError, err.Error(), nil) } return } var status string if !result.Changed { status = "no_change" } else if req.Wait { status = "synced" } else { status = "synced_local" } writeJSON(w, http.StatusOK, SyncResponse{ Status: status, Path: expandedPath, TXID: result.TXID, ReplicatedTXID: result.ReplicatedTXID, }) } // SyncRequest is the request body for the /sync endpoint. type SyncRequest struct { Path string `json:"path"` Wait bool `json:"wait,omitempty"` Timeout int `json:"timeout,omitempty"` } // SyncResponse is the response body for the /sync endpoint. type SyncResponse struct { Status string `json:"status"` Path string `json:"path"` TXID uint64 `json:"txid"` ReplicatedTXID uint64 `json:"replicated_txid"` } func (s *Server) handleList(w http.ResponseWriter, _ *http.Request) { dbs := s.store.DBs() resp := ListResponse{ Databases: make([]DatabaseSummary, 0, len(dbs)), } for _, db := range dbs { var status string if db.IsOpen() { if db.Replica != nil && db.Replica.MonitorEnabled { status = "replicating" } else { status = "open" } } else { status = "stopped" } summary := DatabaseSummary{ Path: db.Path(), Status: status, } if t := db.LastSuccessfulSyncAt(); !t.IsZero() { summary.LastSyncAt = &t } resp.Databases = append(resp.Databases, summary) } writeJSON(w, http.StatusOK, resp) } func (s *Server) handleInfo(w http.ResponseWriter, _ *http.Request) { resp := InfoResponse{ Version: s.Version, PID: os.Getpid(), StartedAt: s.startedAt, UptimeSeconds: int64(time.Since(s.startedAt).Seconds()), DatabaseCount: len(s.store.DBs()), } writeJSON(w, http.StatusOK, resp) } // ListResponse is the response body for the /list endpoint. type ListResponse struct { Databases []DatabaseSummary `json:"databases"` } // DatabaseSummary contains summary information about a database. type DatabaseSummary struct { Path string `json:"path"` Status string `json:"status"` // LastSyncAt is the timestamp of the last successful replica sync. // This reflects when data was last successfully uploaded to the replica // storage backend, not just when the local WAL was processed. LastSyncAt *time.Time `json:"last_sync_at,omitempty"` } // InfoResponse is the response body for the /info endpoint. type InfoResponse struct { Version string `json:"version"` PID int `json:"pid"` UptimeSeconds int64 `json:"uptime_seconds"` StartedAt time.Time `json:"started_at"` DatabaseCount int `json:"database_count"` } // RegisterDatabaseRequest is the request body for the /register endpoint. type RegisterDatabaseRequest struct { Path string `json:"path"` ReplicaURL string `json:"replica_url"` } // RegisterDatabaseResponse is the response body for the /register endpoint. type RegisterDatabaseResponse struct { Status string `json:"status"` Path string `json:"path"` } // UnregisterDatabaseRequest is the request body for the /unregister endpoint. type UnregisterDatabaseRequest struct { Path string `json:"path"` Timeout int `json:"timeout,omitempty"` } // UnregisterDatabaseResponse is the response body for the /unregister endpoint. type UnregisterDatabaseResponse struct { Status string `json:"status"` Path string `json:"path"` } func (s *Server) handleRegister(w http.ResponseWriter, r *http.Request) { var req RegisterDatabaseRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { writeJSONError(w, http.StatusBadRequest, "invalid request body", err.Error()) return } if req.Path == "" { writeJSONError(w, http.StatusBadRequest, "path required", nil) return } if req.ReplicaURL == "" { writeJSONError(w, http.StatusBadRequest, "replica_url required", nil) return } expandedPath, err := s.expandPath(req.Path) if err != nil { writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("invalid path: %v", err), nil) return } // Check if database already exists. if existing := s.store.FindDB(expandedPath); existing != nil { writeJSON(w, http.StatusOK, RegisterDatabaseResponse{ Status: "already_exists", Path: expandedPath, }) return } // Create replica client from URL. client, err := NewReplicaClientFromURL(req.ReplicaURL) if err != nil { writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("invalid replica url: %v", err), nil) return } // Create new database. db := NewDB(expandedPath) // Create replica and attach client. replica := NewReplica(db) replica.Client = client db.Replica = replica // Register database with store (this also opens the database). if err := s.store.RegisterDB(db); err != nil { writeJSONError(w, http.StatusInternalServerError, fmt.Sprintf("failed to register database: %v", err), nil) return } writeJSON(w, http.StatusOK, RegisterDatabaseResponse{ Status: "registered", Path: expandedPath, }) } func (s *Server) handleUnregister(w http.ResponseWriter, r *http.Request) { var req UnregisterDatabaseRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { writeJSONError(w, http.StatusBadRequest, "invalid request body", err.Error()) return } if req.Path == "" { writeJSONError(w, http.StatusBadRequest, "path required", nil) return } expandedPath, err := s.expandPath(req.Path) if err != nil { writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("invalid path: %v", err), nil) return } // Set up timeout context. Treat non-positive values as default. timeout := req.Timeout if timeout <= 0 { timeout = 30 } ctx, cancel := context.WithTimeout(s.ctx, time.Duration(timeout)*time.Second) defer cancel() // Remove database from store (this also closes it). if err := s.store.UnregisterDB(ctx, expandedPath); err != nil { writeJSONError(w, http.StatusInternalServerError, fmt.Sprintf("failed to unregister database: %v", err), nil) return } writeJSON(w, http.StatusOK, UnregisterDatabaseResponse{ Status: "unregistered", Path: expandedPath, }) } ================================================ FILE: server_test.go ================================================ package litestream_test import ( "context" "encoding/json" "fmt" "io" "net" "net/http" "os" "sync/atomic" "testing" "time" "github.com/stretchr/testify/require" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/internal/testingutil" ) var testSocketCounter uint64 func testSocketPath(t *testing.T) string { t.Helper() n := atomic.AddUint64(&testSocketCounter, 1) path := fmt.Sprintf("/tmp/ls-test-%d.sock", n) t.Cleanup(func() { os.Remove(path) }) return path } func TestServer_HandleInfo(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) server.Version = "v1.0.0-test" require.NoError(t, server.Start()) defer server.Close() client := newSocketClient(t, server.SocketPath) resp, err := client.Get("http://localhost/info") require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusOK, resp.StatusCode) var result litestream.InfoResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) require.Equal(t, "v1.0.0-test", result.Version) require.Greater(t, result.PID, 0) require.Equal(t, 1, result.DatabaseCount) require.False(t, result.StartedAt.IsZero()) require.GreaterOrEqual(t, result.UptimeSeconds, int64(0)) } func TestServer_HandleList(t *testing.T) { t.Run("EmptyStore", func(t *testing.T) { store := litestream.NewStore(nil, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() client := newSocketClient(t, server.SocketPath) resp, err := client.Get("http://localhost/list") require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusOK, resp.StatusCode) var result litestream.ListResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) require.Empty(t, result.Databases) }) t.Run("WithDatabases", func(t *testing.T) { db1, sqldb1 := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db1, sqldb1) db2, sqldb2 := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db2, sqldb2) store := litestream.NewStore([]*litestream.DB{db1, db2}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() client := newSocketClient(t, server.SocketPath) resp, err := client.Get("http://localhost/list") require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusOK, resp.StatusCode) var result litestream.ListResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) require.Len(t, result.Databases, 2) // Verify both databases are listed (order may vary). paths := make(map[string]string) for _, db := range result.Databases { paths[db.Path] = db.Status } require.Contains(t, paths, db1.Path()) require.Contains(t, paths, db2.Path()) }) t.Run("StatusOpenWhenMonitorDisabled", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // MonitorEnabled is false by default in test helper. require.False(t, db.Replica.MonitorEnabled) store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() client := newSocketClient(t, server.SocketPath) resp, err := client.Get("http://localhost/list") require.NoError(t, err) defer resp.Body.Close() var result litestream.ListResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) require.Len(t, result.Databases, 1) // Since MonitorEnabled is false, status should be "open" not "replicating". require.Equal(t, "open", result.Databases[0].Status) }) t.Run("StatusReplicatingWhenMonitorEnabled", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Enable the monitor to simulate active replication. db.Replica.MonitorEnabled = true store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() client := newSocketClient(t, server.SocketPath) resp, err := client.Get("http://localhost/list") require.NoError(t, err) defer resp.Body.Close() var result litestream.ListResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) require.Len(t, result.Databases, 1) // Since MonitorEnabled is true, status should be "replicating". require.Equal(t, "replicating", result.Databases[0].Status) }) t.Run("IncludesLastSyncAt", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Create some data and sync. _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INT)`) require.NoError(t, err) _, err = sqldb.ExecContext(t.Context(), `INSERT INTO t (id) VALUES (1)`) require.NoError(t, err) require.NoError(t, db.Sync(t.Context())) require.NoError(t, db.Replica.Sync(t.Context())) store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() client := newSocketClient(t, server.SocketPath) resp, err := client.Get("http://localhost/list") require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusOK, resp.StatusCode) var result litestream.ListResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) require.Len(t, result.Databases, 1) require.NotNil(t, result.Databases[0].LastSyncAt, "LastSyncAt should be set after sync") }) } func TestServer_HandleStart(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() t.Run("MissingPath", func(t *testing.T) { client := newSocketClient(t, server.SocketPath) resp, err := client.Post("http://localhost/start", "application/json", nil) require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusBadRequest, resp.StatusCode) }) t.Run("DatabaseNotFound", func(t *testing.T) { client := newSocketClient(t, server.SocketPath) body := `{"path": "/nonexistent/db"}` resp, err := client.Post("http://localhost/start", "application/json", io.NopCloser(stringReader(body))) require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusInternalServerError, resp.StatusCode) }) } func TestServer_HandleStop(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() t.Run("MissingPath", func(t *testing.T) { client := newSocketClient(t, server.SocketPath) resp, err := client.Post("http://localhost/stop", "application/json", nil) require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusBadRequest, resp.StatusCode) }) } func TestServer_HandleRegister(t *testing.T) { t.Run("MissingPath", func(t *testing.T) { store := litestream.NewStore(nil, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() client := newSocketClient(t, server.SocketPath) body := `{"replica_url": "file:///tmp/backup"}` resp, err := client.Post("http://localhost/register", "application/json", io.NopCloser(stringReader(body))) require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusBadRequest, resp.StatusCode) var result litestream.ErrorResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) require.Equal(t, "path required", result.Error) }) t.Run("MissingReplicaURL", func(t *testing.T) { store := litestream.NewStore(nil, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() client := newSocketClient(t, server.SocketPath) body := `{"path": "/tmp/test.db"}` resp, err := client.Post("http://localhost/register", "application/json", io.NopCloser(stringReader(body))) require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusBadRequest, resp.StatusCode) var result litestream.ErrorResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) require.Equal(t, "replica_url required", result.Error) }) t.Run("InvalidReplicaURL", func(t *testing.T) { store := litestream.NewStore(nil, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() client := newSocketClient(t, server.SocketPath) body := `{"path": "/tmp/test.db", "replica_url": "invalid://badscheme"}` resp, err := client.Post("http://localhost/register", "application/json", io.NopCloser(stringReader(body))) require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusBadRequest, resp.StatusCode) var result litestream.ErrorResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) require.Contains(t, result.Error, "invalid replica url") }) t.Run("Success", func(t *testing.T) { store := litestream.NewStore(nil, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() // Create a temporary database file. db, sqldb := testingutil.MustOpenDBs(t) testingutil.MustCloseDBs(t, db, sqldb) dbPath := db.Path() // Create a temp directory for backup. backupDir := t.TempDir() client := newSocketClient(t, server.SocketPath) body := fmt.Sprintf(`{"path": %q, "replica_url": "file://%s"}`, dbPath, backupDir) resp, err := client.Post("http://localhost/register", "application/json", io.NopCloser(stringReader(body))) require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusOK, resp.StatusCode) var result litestream.RegisterDatabaseResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) require.Equal(t, "registered", result.Status) require.Equal(t, dbPath, result.Path) // Verify database was registered with store. require.Len(t, store.DBs(), 1) require.Equal(t, dbPath, store.DBs()[0].Path()) }) t.Run("AlreadyExists", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() // Try to register the same database again. backupDir := t.TempDir() client := newSocketClient(t, server.SocketPath) body := fmt.Sprintf(`{"path": %q, "replica_url": "file://%s"}`, db.Path(), backupDir) resp, err := client.Post("http://localhost/register", "application/json", io.NopCloser(stringReader(body))) require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusOK, resp.StatusCode) var result litestream.RegisterDatabaseResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) require.Equal(t, "already_exists", result.Status) }) } func TestServer_HandleUnregister(t *testing.T) { t.Run("MissingPath", func(t *testing.T) { store := litestream.NewStore(nil, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() client := newSocketClient(t, server.SocketPath) body := `{}` resp, err := client.Post("http://localhost/unregister", "application/json", io.NopCloser(stringReader(body))) require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusBadRequest, resp.StatusCode) var result litestream.ErrorResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) require.Equal(t, "path required", result.Error) }) t.Run("NotFoundIsIdempotent", func(t *testing.T) { store := litestream.NewStore(nil, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() client := newSocketClient(t, server.SocketPath) body := `{"path": "/nonexistent/db"}` resp, err := client.Post("http://localhost/unregister", "application/json", io.NopCloser(stringReader(body))) require.NoError(t, err) defer resp.Body.Close() // UnregisterDB is idempotent - returns success even if DB not found. require.Equal(t, http.StatusOK, resp.StatusCode) var result litestream.UnregisterDatabaseResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) require.Equal(t, "unregistered", result.Status) }) t.Run("Success", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) dbPath := db.Path() store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) require.Len(t, store.DBs(), 1) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() client := newSocketClient(t, server.SocketPath) body := fmt.Sprintf(`{"path": %q}`, dbPath) resp, err := client.Post("http://localhost/unregister", "application/json", io.NopCloser(stringReader(body))) require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusOK, resp.StatusCode) var result litestream.UnregisterDatabaseResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) require.Equal(t, "unregistered", result.Status) require.Equal(t, dbPath, result.Path) // Verify database was unregistered from store. require.Empty(t, store.DBs()) }) } func TestServer_HandleSync(t *testing.T) { t.Run("MissingPath", func(t *testing.T) { store := litestream.NewStore(nil, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() client := newSocketClient(t, server.SocketPath) body := `{}` resp, err := client.Post("http://localhost/sync", "application/json", io.NopCloser(stringReader(body))) require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusBadRequest, resp.StatusCode) var result litestream.ErrorResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) require.Equal(t, "path required", result.Error) }) t.Run("DatabaseNotFound", func(t *testing.T) { store := litestream.NewStore(nil, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() client := newSocketClient(t, server.SocketPath) body := `{"path": "/nonexistent/db"}` resp, err := client.Post("http://localhost/sync", "application/json", io.NopCloser(stringReader(body))) require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusNotFound, resp.StatusCode) }) t.Run("Success", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INT)`) require.NoError(t, err) store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() client := newSocketClient(t, server.SocketPath) body := fmt.Sprintf(`{"path": %q}`, db.Path()) resp, err := client.Post("http://localhost/sync", "application/json", io.NopCloser(stringReader(body))) require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusOK, resp.StatusCode) var result litestream.SyncResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) require.Equal(t, "synced_local", result.Status) require.Equal(t, db.Path(), result.Path) require.Greater(t, result.TXID, uint64(0)) }) t.Run("SuccessWithWait", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INT)`) require.NoError(t, err) _, err = sqldb.ExecContext(t.Context(), `INSERT INTO t (id) VALUES (1)`) require.NoError(t, err) store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() client := newSocketClient(t, server.SocketPath) body := fmt.Sprintf(`{"path": %q, "wait": true, "timeout": 30}`, db.Path()) resp, err := client.Post("http://localhost/sync", "application/json", io.NopCloser(stringReader(body))) require.NoError(t, err) defer resp.Body.Close() require.Equal(t, http.StatusOK, resp.StatusCode) var result litestream.SyncResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) require.Equal(t, "synced", result.Status) require.Equal(t, db.Path(), result.Path) require.Greater(t, result.TXID, uint64(0)) require.Greater(t, result.ReplicatedTXID, uint64(0)) }) t.Run("NoChange", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INT)`) require.NoError(t, err) store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{{Level: 0}}) store.CompactionMonitorEnabled = false require.NoError(t, store.Open(t.Context())) defer store.Close(t.Context()) server := litestream.NewServer(store) server.SocketPath = testSocketPath(t) require.NoError(t, server.Start()) defer server.Close() client := newSocketClient(t, server.SocketPath) body := fmt.Sprintf(`{"path": %q}`, db.Path()) // First sync should pick up the CREATE TABLE. resp1, err := client.Post("http://localhost/sync", "application/json", io.NopCloser(stringReader(body))) require.NoError(t, err) defer resp1.Body.Close() require.Equal(t, http.StatusOK, resp1.StatusCode) var result1 litestream.SyncResponse require.NoError(t, json.NewDecoder(resp1.Body).Decode(&result1)) require.Equal(t, "synced_local", result1.Status) require.Greater(t, result1.TXID, uint64(0)) // Second sync with no new writes should return no_change. resp2, err := client.Post("http://localhost/sync", "application/json", io.NopCloser(stringReader(body))) require.NoError(t, err) defer resp2.Body.Close() require.Equal(t, http.StatusOK, resp2.StatusCode) var result2 litestream.SyncResponse require.NoError(t, json.NewDecoder(resp2.Body).Decode(&result2)) require.Equal(t, "no_change", result2.Status) require.Equal(t, result1.TXID, result2.TXID) }) } func newSocketClient(t *testing.T, socketPath string) *http.Client { t.Helper() return &http.Client{ Timeout: 10 * time.Second, Transport: &http.Transport{ DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { return net.DialTimeout("unix", socketPath, 10*time.Second) }, }, } } type stringReaderType struct { s string i int } func stringReader(s string) *stringReaderType { return &stringReaderType{s: s} } func (r *stringReaderType) Read(p []byte) (n int, err error) { if r.i >= len(r.s) { return 0, io.EOF } n = copy(p, r.s[r.i:]) r.i += n return n, nil } ================================================ FILE: sftp/replica_client.go ================================================ package sftp import ( "bytes" "context" "errors" "fmt" "io" "log/slog" "net" "net/url" "os" "path" "sync" "time" "github.com/pkg/sftp" "github.com/superfly/ltx" "golang.org/x/crypto/ssh" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/internal" ) func init() { litestream.RegisterReplicaClientFactory("sftp", NewReplicaClientFromURL) } // ReplicaClientType is the client type for this package. const ReplicaClientType = "sftp" // Default settings for replica client. const ( DefaultDialTimeout = 30 * time.Second ) var _ litestream.ReplicaClient = (*ReplicaClient)(nil) // ReplicaClient is a client for writing LTX files over SFTP. type ReplicaClient struct { mu sync.Mutex sshClient *ssh.Client sftpClient *sftp.Client logger *slog.Logger // SFTP connection info Host string User string Password string Path string KeyPath string HostKey string DialTimeout time.Duration // ConcurrentWrites enables concurrent writes for better performance. // Note: This makes resuming failed transfers unsafe. ConcurrentWrites bool } // NewReplicaClient returns a new instance of ReplicaClient. func NewReplicaClient() *ReplicaClient { return &ReplicaClient{ logger: slog.Default().WithGroup(ReplicaClientType), DialTimeout: DefaultDialTimeout, ConcurrentWrites: true, // Default to true for better performance } } func (c *ReplicaClient) SetLogger(logger *slog.Logger) { c.logger = logger.WithGroup(ReplicaClientType) } // NewReplicaClientFromURL creates a new ReplicaClient from URL components. // This is used by the replica client factory registration. // URL format: sftp://[user[:password]@]host[:port]/path func NewReplicaClientFromURL(scheme, host, urlPath string, query url.Values, userinfo *url.Userinfo) (litestream.ReplicaClient, error) { client := NewReplicaClient() // Extract credentials from userinfo if userinfo != nil { client.User = userinfo.Username() client.Password, _ = userinfo.Password() } client.Host = host client.Path = urlPath if client.Host == "" { return nil, fmt.Errorf("host required for sftp replica URL") } if client.User == "" { return nil, fmt.Errorf("user required for sftp replica URL") } return client, nil } // Type returns "sftp" as the client type. func (c *ReplicaClient) Type() string { return ReplicaClientType } // Init initializes the connection to SFTP. No-op if already initialized. func (c *ReplicaClient) Init(ctx context.Context) error { _, err := c.init(ctx) return err } // init initializes the connection and returns the SFTP client. func (c *ReplicaClient) init(ctx context.Context) (_ *sftp.Client, err error) { c.mu.Lock() defer c.mu.Unlock() if c.sftpClient != nil { return c.sftpClient, nil } if c.User == "" { return nil, fmt.Errorf("sftp user required") } // Build SSH configuration & auth methods var hostkey ssh.HostKeyCallback if c.HostKey != "" { var pubkey, _, _, _, err = ssh.ParseAuthorizedKey([]byte(c.HostKey)) if err != nil { return nil, fmt.Errorf("cannot parse sftp host key: %w", err) } hostkey = ssh.FixedHostKey(pubkey) } else { slog.Warn("sftp host key not verified", "host", c.Host) hostkey = ssh.InsecureIgnoreHostKey() } config := &ssh.ClientConfig{ User: c.User, HostKeyCallback: hostkey, BannerCallback: ssh.BannerDisplayStderr(), } if c.Password != "" { config.Auth = append(config.Auth, ssh.Password(c.Password)) } if c.KeyPath != "" { buf, err := os.ReadFile(c.KeyPath) if err != nil { return nil, fmt.Errorf("sftp: cannot read sftp key path: %w", err) } signer, err := ssh.ParsePrivateKey(buf) if err != nil { return nil, fmt.Errorf("sftp: cannot parse sftp key path: %w", err) } config.Auth = append(config.Auth, ssh.PublicKeys(signer)) } // Append standard port, if necessary. host := c.Host if _, _, err := net.SplitHostPort(c.Host); err != nil { host = net.JoinHostPort(c.Host, "22") } // Connect via SSH. if c.sshClient, err = ssh.Dial("tcp", host, config); err != nil { return nil, err } // Wrap connection with an SFTP client. // Configure options based on client settings opts := []sftp.ClientOption{} if c.ConcurrentWrites { opts = append(opts, sftp.UseConcurrentWrites(true)) } if c.sftpClient, err = sftp.NewClient(c.sshClient, opts...); err != nil { c.sshClient.Close() c.sshClient = nil return nil, err } return c.sftpClient, nil } // DeleteAll deletes all LTX files. func (c *ReplicaClient) DeleteAll(ctx context.Context) (err error) { defer func() { c.resetOnConnError(err) }() sftpClient, err := c.init(ctx) if err != nil { return err } var dirs []string walker := sftpClient.Walk(c.Path) for walker.Step() { if err := walker.Err(); os.IsNotExist(err) { continue } else if err != nil { return fmt.Errorf("sftp: cannot walk path %q: %w", walker.Path(), err) } if walker.Stat().IsDir() { dirs = append(dirs, walker.Path()) continue } if err := sftpClient.Remove(walker.Path()); err != nil && !os.IsNotExist(err) { return fmt.Errorf("sftp: cannot delete file %q: %w", walker.Path(), err) } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "DELETE").Inc() } // Remove directories in reverse order after they have been emptied. for i := len(dirs) - 1; i >= 0; i-- { filename := dirs[i] if err := sftpClient.RemoveDirectory(filename); err != nil && !os.IsNotExist(err) { return fmt.Errorf("sftp: cannot delete directory %q: %w", filename, err) } } // log.Printf("%s(%s): retainer: deleting all", r.db.Path(), r.Name()) return nil } // LTXFiles returns an iterator over all available LTX files for a level. // SFTP uses file ModTime for timestamps, which is set via Chtimes() to preserve original timestamp. // The useMetadata parameter is ignored since ModTime always contains the accurate timestamp. func (c *ReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, _ bool) (_ ltx.FileIterator, err error) { defer func() { c.resetOnConnError(err) }() sftpClient, err := c.init(ctx) if err != nil { return nil, err } dir := litestream.LTXLevelDir(c.Path, level) fis, err := sftpClient.ReadDir(dir) if os.IsNotExist(err) { return ltx.NewFileInfoSliceIterator(nil), nil } else if err != nil { return nil, err } // Iterate over every file and convert to metadata. infos := make([]*ltx.FileInfo, 0, len(fis)) for _, fi := range fis { minTXID, maxTXID, err := ltx.ParseFilename(path.Base(fi.Name())) if err != nil { continue } else if minTXID < seek { continue } infos = append(infos, <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Size: fi.Size(), CreatedAt: fi.ModTime().UTC(), // ModTime contains accurate timestamp from Chtimes() }) } return ltx.NewFileInfoSliceIterator(infos), nil } // WriteLTXFile writes a LTX file from rd into a remote file. func (c *ReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, rd io.Reader) (info *ltx.FileInfo, err error) { defer func() { c.resetOnConnError(err) }() sftpClient, err := c.init(ctx) if err != nil { return nil, err } filename := litestream.LTXFilePath(c.Path, level, minTXID, maxTXID) // Use TeeReader to peek at LTX header while preserving data for upload var buf bytes.Buffer teeReader := io.TeeReader(rd, &buf) // Extract timestamp from LTX header hdr, _, err := ltx.PeekHeader(teeReader) if err != nil { return nil, fmt.Errorf("extract timestamp from LTX header: %w", err) } timestamp := time.UnixMilli(hdr.Timestamp).UTC() // Combine buffered data with rest of reader fullReader := io.MultiReader(&buf, rd) if err := sftpClient.MkdirAll(path.Dir(filename)); err != nil { return nil, fmt.Errorf("sftp: cannot make parent snapshot directory %q: %w", path.Dir(filename), err) } f, err := sftpClient.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC) if err != nil { return nil, fmt.Errorf("sftp: cannot open snapshot file for writing: %w", err) } defer f.Close() n, err := io.Copy(f, fullReader) if err != nil { return nil, err } else if err := f.Close(); err != nil { return nil, err } // Set file ModTime to preserve original timestamp if err := sftpClient.Chtimes(filename, timestamp, timestamp); err != nil { return nil, fmt.Errorf("sftp: cannot set file timestamps: %w", err) } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "PUT").Inc() internal.OperationBytesCounterVec.WithLabelValues(ReplicaClientType, "PUT").Add(float64(n)) return <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Size: n, CreatedAt: timestamp, }, nil } // OpenLTXFile returns a reader for an LTX file. // Returns os.ErrNotExist if no matching position is found. func (c *ReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (_ io.ReadCloser, err error) { defer func() { c.resetOnConnError(err) }() sftpClient, err := c.init(ctx) if err != nil { return nil, err } filename := litestream.LTXFilePath(c.Path, level, minTXID, maxTXID) f, err := sftpClient.OpenFile(filename, os.O_RDONLY) if err != nil { return nil, err } if offset > 0 { if _, err := f.Seek(offset, io.SeekStart); err != nil { return nil, err } } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "GET").Inc() if size > 0 { return internal.LimitReadCloser(f, size), nil } return f, nil } // DeleteLTXFiles deletes LTX files with at the given positions. func (c *ReplicaClient) DeleteLTXFiles(ctx context.Context, a []*ltx.FileInfo) (err error) { defer func() { c.resetOnConnError(err) }() sftpClient, err := c.init(ctx) if err != nil { return err } for _, info := range a { filename := litestream.LTXFilePath(c.Path, info.Level, info.MinTXID, info.MaxTXID) c.logger.Debug("deleting ltx file", "level", info.Level, "minTXID", info.MinTXID, "maxTXID", info.MaxTXID, "path", filename) if err := sftpClient.Remove(filename); err != nil && !os.IsNotExist(err) { return fmt.Errorf("sftp: cannot delete ltx file %q: %w", filename, err) } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "DELETE").Inc() } return nil } // Cleanup deletes path & directories after empty. func (c *ReplicaClient) Cleanup(ctx context.Context) (err error) { defer func() { c.resetOnConnError(err) }() sftpClient, err := c.init(ctx) if err != nil { return err } if err := sftpClient.RemoveDirectory(c.Path); err != nil && !os.IsNotExist(err) { return fmt.Errorf("sftp: cannot delete path: %w", err) } return nil } // resetOnConnError closes & clears the client if a connection error occurs. func (c *ReplicaClient) resetOnConnError(err error) { if !errors.Is(err, sftp.ErrSSHFxConnectionLost) { return } if c.sftpClient != nil { c.sftpClient.Close() c.sftpClient = nil } if c.sshClient != nil { c.sshClient.Close() c.sshClient = nil } } ================================================ FILE: skills/litestream/SKILL.md ================================================ --- name: litestream description: >- Expert knowledge for contributing to Litestream, a standalone disaster recovery tool for SQLite. Provides architectural understanding, code patterns, critical rules, and debugging procedures for WAL monitoring, LTX replication format, storage backend implementation, multi-level compaction, and SQLite page management. Use when working with Litestream source code, writing storage backends, debugging replication issues, implementing compaction logic, or handling SQLite WAL operations. license: Apache-2.0 metadata: author: benbjohnson version: "1.0" repository: https://github.com/benbjohnson/litestream --- # Litestream Agent Skill Litestream is a standalone disaster recovery tool for SQLite. It runs as a background process, monitors the SQLite WAL (Write-Ahead Log), converts changes to immutable LTX files, and replicates them to cloud storage. It uses `modernc.org/sqlite` (pure Go, no CGO required). ## Quick Start ```bash # Build go build -o bin/litestream ./cmd/litestream # Test (always use race detector) go test -race -v ./... # Code quality pre-commit run --all-files ``` ## Critical Rules These invariants must never be violated: ### 1. Lock Page at 1GB SQLite reserves a page at byte offset 0x40000000 (1 GB). Always skip it during replication and compaction. The page number varies by page size: | Page Size | Lock Page Number | |-----------|------------------| | 4 KB | 262145 | | 8 KB | 131073 | | 16 KB | 65537 | | 32 KB | 32769 | ```go lockPgno := ltx.LockPgno(pageSize) if pgno == lockPgno { continue } ``` ### 2. LTX Files Are Immutable Once an LTX file is written, it must never be modified. New changes create new files. This guarantees point-in-time recovery integrity. ### 3. Single Replica per Database Each database replicates to exactly one destination. The Replica component manages replication mechanics; database state belongs in the DB layer. ### 4. Read Local Before Remote During Compaction Cloud storage is eventually consistent. Always read from local disk first: ```go f, err := os.Open(db.LTXPath(info.Level, info.MinTXID, info.MaxTXID)) if err == nil { return f, nil // Use local copy } return replica.Client.OpenLTXFile(...) // Fall back to remote ``` ### 5. Preserve Timestamps During Compaction Set the compacted file's `CreatedAt` to the earliest source file timestamp to maintain temporal granularity for point-in-time restoration. ```go info.CreatedAt = oldestSourceFile.CreatedAt ``` ### 6. Use Lock() Not RLock() for Writes ```go // CORRECT r.mu.Lock() defer r.mu.Unlock() r.pos = pos // WRONG - race condition r.mu.RLock() defer r.mu.RUnlock() r.pos = pos ``` ### 7. Atomic File Operations Always write to a temp file then rename. Never write directly to the final path. ```go tmpFile, err := os.CreateTemp(dir, ".tmp-*") // ... write data, sync ... os.Rename(tmpFile.Name(), finalPath) ``` ## Architecture ### System Layers | Layer | File(s) | Responsibility | |---------|--------------------------|-------------------------------------------| | App | `cmd/litestream/` | CLI commands, YAML/env config | | Store | `store.go` | Multi-DB coordination, compaction | | DB | `db.go` | Single DB management, WAL monitoring | | Replica | `replica.go` | Replication to one destination | | Storage | `*/replica_client.go` | Backend implementations (S3, GCS, etc.) | Database state logic belongs in the DB layer, not the Replica layer. ### ReplicaClient Interface All storage backends implement this interface from `replica_client.go`: ```go type ReplicaClient interface { Type() string Init(ctx context.Context) error LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) DeleteLTXFiles(ctx context.Context, a []*ltx.FileInfo) error DeleteAll(ctx context.Context) error } ``` Key contract details: - `OpenLTXFile` must return `os.ErrNotExist` when file is missing - `WriteLTXFile` must set `CreatedAt` from backend metadata or upload time - `LTXFiles` with `useMetadata=true` fetches accurate timestamps (for PIT restore) - `LTXFiles` with `useMetadata=false` uses fast timestamps (normal operations) ### Lock Ordering Always acquire locks in this order to prevent deadlocks: 1. `Store.mu` 2. `DB.mu` 3. `DB.chkMu` 4. `Replica.mu` ### Core Components **DB** (`db.go`): Manages SQLite connection, WAL monitoring, checkpointing, and long-running read transaction for consistency. Key fields: `path`, `db`, `rtx` (read transaction), `pageSize`, `notify` channel. **Replica** (`replica.go`): Tracks replication position (`ltx.Pos` with TXID, PageNo, Checksum). One replica per database. **Store** (`store.go`): Coordinates multiple databases and schedules compaction across levels. ## LTX File Format LTX (Log Transaction) files are immutable, checksummed archives of database changes. Structure: ``` +------------------+ | Header | 100 bytes (magic "LTX1", page size, TXID range, timestamp) +------------------+ | Page Frames | 4-byte pgno + pageSize bytes data, per page +------------------+ | Page Index | Binary search index for page lookup +------------------+ | Trailer | 16 bytes (post-apply checksum, file checksum) +------------------+ ``` ### Naming Convention ``` Format: MMMMMMMMMMMMMMMM-NNNNNNNNNNNNNNNN.ltx Example: 0000000000000001-0000000000000064.ltx (TXID 1-100) ``` ### Compaction Levels ``` Level 0: /ltx/0000/ Raw LTX files (no compaction) Level 1: /ltx/0001/ Compacted periodically Level 2: /ltx/0002/ Compacted less frequently ``` Default compaction levels: L0 (raw), L1 (30s), L2 (5min), L3 (1h), plus daily snapshots. Compaction merges files by deduplicating pages (latest version wins) and always skips the lock page. ## Code Patterns ### DO - Return errors immediately; let callers decide handling - Use `fmt.Errorf("context: %w", err)` for error wrapping - Handle database state in the DB layer, not Replica - Use `db.verify()` to trigger snapshots (don't reimplement) - Test with race detector: `go test -race` - Use lazy iterators for `LTXFiles` (paginate, don't load all at once) ### DON'T - Write data at the 1 GB lock page boundary - Modify LTX files after creation - Put database state logic in the Replica layer - Use `RLock()` when writing shared state - Write directly to final file paths (use temp + rename) - Ignore context cancellation in long operations - Return generic errors instead of `os.ErrNotExist` for missing files ## Specialized Knowledge Areas Load reference files on demand based on the task: | Task | Reference File | |-----------------------------------|-----------------------------------------| | Understanding system design | `references/ARCHITECTURE.md` | | Writing or reviewing code | `references/PATTERNS.md` | | Working with LTX files | `references/LTX_FORMAT.md` | | WAL monitoring or page operations | `references/SQLITE_INTERNALS.md` | | Implementing storage backends | `references/REPLICA_CLIENT_GUIDE.md` | | Writing or debugging tests | `references/TESTING_GUIDE.md` | ## Common Debugging Procedures ### Replication Not Working 1. Verify WAL mode: `PRAGMA journal_mode` must return `wal` 2. Check monitor interval and that the monitor goroutine is running 3. Confirm `db.notify` channel is being signaled on WAL changes 4. Check replica position: `replica.Pos()` should advance with writes 5. Look for `os.ErrNotExist` from `OpenLTXFile` (file not replicated yet) ### Large Database Issues (>1 GB) 1. Verify lock page is being skipped: check `ltx.LockPgno(pageSize)` 2. Test with multiple page sizes (4K, 8K, 16K, 32K) 3. Run with databases both smaller and larger than 1 GB 4. Ensure page iteration loops include the `continue` guard for lock page ### Compaction Problems 1. Confirm local L0 files exist before compaction reads them 2. Check that `CreatedAt` timestamps are preserved (earliest source) 3. Verify compaction level intervals in `Store.levels` 4. Look for eventual consistency issues if reading from remote storage ### Storage Backend Issues 1. Return `os.ErrNotExist` for missing files (not generic errors) 2. Support partial reads via `offset`/`size` in `OpenLTXFile` 3. Handle context cancellation in all methods 4. Test concurrent operations with `-race` flag 5. For eventually consistent backends, add retry logic with backoff ### Corrupted or Missing LTX Files 1. Check logs for `LTXError` messages - they include context (Op, Path, Level, TXID) and recovery hints 2. Common error messages: "nonsequential page numbers", "non-contiguous transaction files", "ltx validation failed" 3. Manual fix: `litestream reset ` clears local LTX state and forces fresh snapshot on next sync (database file is not modified) 4. Automatic fix: set `auto-recover: true` on the replica config to auto-reset on LTX errors (disabled by default) 5. Reference: `cmd/litestream/reset.go`, `replica.go` (auto-recover logic), `db.go` (`ResetLocalState`) ## Contribution Guidelines ### What's Accepted - Bug fixes and patches (welcome) - Documentation improvements - Small code improvements and performance optimizations - Security vulnerability reports (report privately) ### Discuss First - Feature requests: open an issue before implementing - Large changes: discuss approach in an issue first ### Pre-Submit Checklist - [ ] Read relevant docs from the reference table above - [ ] Follow patterns in `references/PATTERNS.md` - [ ] Run `go test -race -v ./...` - [ ] Run `pre-commit run --all-files` - [ ] For page iteration: test with >1 GB databases - [ ] Show investigation evidence in PR (see CONTRIBUTING.md) ## Testing ```bash # Full test suite with race detection go test -race -v ./... # Specific areas go test -race -v -run TestReplica_Sync ./... go test -race -v -run TestDB_Sync ./... go test -race -v -run TestStore_CompactDB ./... # Coverage go test -coverprofile=coverage.out ./... go tool cover -html=coverage.out ``` Key testing areas: - Lock page handling with >1 GB databases and multiple page sizes - Race conditions in position updates, WAL monitoring, and checkpointing - Eventual consistency in storage backend operations - Atomic file operations and cleanup on error paths ## Environment Validation Run `scripts/validate-setup.sh` to verify your development environment is correctly configured for Litestream development. ================================================ FILE: skills/litestream/references/ARCHITECTURE.md ================================================ # Litestream Architecture Reference Condensed architectural reference for agents working on Litestream. ## System Layers ``` Application Layer cmd/litestream/ CLI commands, YAML/env config Core Layer store.go Multi-DB coordination, compaction scheduling db.go Single DB management, WAL monitoring, checkpoints replica.go Replication to single destination, position tracking Storage Abstraction replica_client.go ReplicaClient interface Storage Backends s3/, gs/, abs/, Backend-specific implementations oss/, file/, sftp/, nats/, webdav/ ``` Database state logic belongs in the DB layer. The Replica layer handles replication mechanics only. ## Core Components ### DB (db.go) The heart of Litestream. Manages a single SQLite database. Key fields: - `path` / `metaPath`: Database and metadata paths - `db *sql.DB`: SQLite connection (via modernc.org/sqlite, pure Go) - `f *os.File`: Long-running file descriptor - `rtx *sql.Tx`: Long-running read transaction (prevents checkpoint past read point) - `pageSize int`: Database page size (critical for lock page calculation) - `mu sync.RWMutex`: Protects struct fields - `chkMu sync.RWMutex`: Checkpoint coordination lock - `notify chan struct{}`: WAL change notifications Key methods: - `Open()` / `Close()`: Lifecycle management - `monitor()`: Background WAL monitoring loop - `checkWAL()`: Detect WAL changes by comparing size/checksum - `Checkpoint(mode)`: Run SQLite checkpoint (PASSIVE, FULL, TRUNCATE) - `autoCheckpoint()`: Threshold-based checkpoint decisions - `Sync()`: Synchronize pending changes to replicas - `Compact()`: Merge LTX files at a given level Configuration: - `MinCheckpointPageN`: Minimum pages for passive checkpoint - `TruncatePageN`: Emergency truncate checkpoint threshold - `CheckpointInterval`: Time-based passive checkpoint interval - `MonitorInterval`: WAL monitoring frequency - Note: RESTART checkpoint mode permanently removed (issue #724) ### Replica (replica.go) Manages replication to a single destination. Key fields: - `db *DB`: Parent database - `Client ReplicaClient`: Storage backend - `pos ltx.Pos`: Current replication position (TXID + PageNo + Checksum) - `mu sync.RWMutex`: Protects position Position tracking: `SetPos()` MUST use `Lock()` not `RLock()`. ### Store (store.go) Coordinates multiple databases and manages compaction. Key fields: - `dbs []*DB`: Managed databases - `levels CompactionLevels`: Compaction level configuration Default compaction levels: L0 (raw), L1 (30s), L2 (5min), L3 (1h), plus daily snapshots. ## WAL Monitoring The monitor loop in `db.go` runs on a ticker: 1. `checkWAL()` compares current WAL size/checksum to previous state 2. If changed, notify replicas via `notifyReplicas()` 3. Check if checkpoint needed via `shouldCheckpoint()` 4. Run `autoCheckpoint()` if thresholds are met Checkpoint strategy: - WAL pages > `TruncatePageN` → TRUNCATE checkpoint (emergency) - WAL pages > `MinCheckpointPageN` → PASSIVE checkpoint - `CheckpointInterval` elapsed → PASSIVE checkpoint ## Compaction Algorithm High-level flow (in `store.go`): 1. Determine if level is due for compaction (`shouldCompact`) 2. Enumerate level L-1 files via `ReplicaClient.LTXFiles` 3. Prefer local copies (`os.Open(db.LTXPath(...))`) over remote reads 4. Stream through `ltx.NewCompactor` (page deduplication, lock page skipping) 5. Write merged file via `ReplicaClient.WriteLTXFile` for level L 6. Set `CreatedAt` to earliest source file timestamp 7. Update cached max file info; delete old L0 files when promoting to L1 ## State Management ### Database States Closed → Opening → Open → Monitoring ↔ Syncing/Checkpointing → Closing → Closed ### Replica States Idle → Starting → Monitoring ↔ Syncing/Uploading → Stopping → Idle Error states retry with exponential backoff (1s initial, 1min max). ## Initialization Flow 1. `Store.Open()` iterates databases 2. For each DB: open SQLite connection → read page size → create metadata dir → start long-running read transaction → initialize replicas → start monitor 3. For each Replica: create with client → load previous position from metadata → validate against database → start sync goroutine 4. Store starts compaction monitors ## Error Handling ### Error Categories - **Recoverable**: Network timeouts, temporary storage unavailability, lock contention - **Fatal**: Database corruption, invalid configuration, disk full - **Operational**: Checkpoint failures, compaction conflicts, sync delays ### Error Propagation ``` ReplicaClient.WriteLTXFile() error → Replica.Sync() error → DB.Sync() error → Store.monitorDB() // logs error, continues ``` ## Lock Ordering Always acquire in this order to prevent deadlocks: 1. `Store.mu` 2. `DB.mu` 3. `DB.chkMu` 4. `Replica.mu` ## Goroutine Management - Use `sync.WaitGroup` for lifecycle tracking - Signal shutdown via `context.Cancel()` - Wait with timeout in `Close()` methods - Every goroutine must have a `defer wg.Done()` ## Performance Characteristics | Operation | Time | Space | |-------------|------------|----------------| | WAL Monitor | O(1) | O(1) + metrics | | Page Write | O(1) | Original DB + WAL | | Compaction | O(n pages) | Temporary during merge | | Restoration | O(n·log m) | n=pages, m=files | | File List | O(k files) | Per-level | ================================================ FILE: skills/litestream/references/LTX_FORMAT.md ================================================ # LTX Format Reference Condensed reference for agents working with Litestream's LTX file format. ## Overview LTX (Log Transaction) is Litestream's custom format for storing database changes. LTX files are: - **Immutable**: Once written, never modified - **Self-contained**: Each file is independent - **Indexed**: Contains page index for efficient seeks - **Checksummed**: CRC-64 ECMA integrity verification ## File Structure ``` +---------------------+ | Header | 100 bytes +---------------------+ | Page Frames | Variable (4-byte pgno + pageSize data per page) +---------------------+ | Page Index | Binary search index +---------------------+ | Trailer | 16 bytes +---------------------+ FileSize = HeaderSize + (PageCount * (4 + PageSize)) + PageIndexSize + TrailerSize ``` ## Header (100 bytes) ``` Offset Size Field 0 4 Magic ("LTX1") 4 4 Flags (bit 1 = NoChecksum) 8 4 PageSize 12 4 Commit (page count after applying) 16 8 MinTXID 24 8 MaxTXID 32 8 Timestamp (ms since Unix epoch) 40 8 PreApplyChecksum 48 8 WALOffset (0 for snapshots) 56 8 WALSize (0 for snapshots) 64 4 WALSalt1 68 4 WALSalt2 72 8 NodeID 80 20 Reserved (zeros) ``` The version is implied by the magic string. `"LTX1"` → `ltx.Version == 2`. ## Page Frames Each frame contains one database page: ``` Offset Size Field 0 4 Page Number (1-based) 4 PageSize Page Data ``` Constraints: - Pages written in sequential order during creation - Lock page at 1 GB boundary is never included - In compacted files, only the latest version of each page ## Page Index Binary search index for efficient random access to pages. Use `ltx.DecodePageIndex()` to parse into a map of page number to `ltx.PageIndexElem`: ```go type PageIndexElem struct { Level int MinTXID TXID MaxTXID TXID Offset int64 // Byte offset of encoded payload Size int64 // Bytes occupied by encoded payload } ``` ## Trailer (16 bytes) ``` Offset Size Field 0 8 PostApplyChecksum (database checksum after applying file) 8 8 FileChecksum (CRC-64 of entire file) ``` The trailer is always at the end of the file. Read it by seeking `-TrailerSize` from `io.SeekEnd`. ## File Naming Convention ``` Format: MMMMMMMMMMMMMMMM-NNNNNNNNNNNNNNNN.ltx MinTXID (16 hex) MaxTXID (16 hex) Example: 0000000000000001-0000000000000064.ltx (TXID 1-100) 0000000000000065-00000000000000c8.ltx (TXID 101-200) ``` Parse with `ltx.ParseFilename()`, format with `ltx.FormatFilename()`. ## Compaction Levels LTX files are organized in levels for efficient storage: ``` Level 0: /ltx/0000/ Raw LTX files (no compaction) Level 1: /ltx/0001/ Compacted (default: every 30s) Level 2: /ltx/0002/ Compacted (default: every 5min) Level 3: /ltx/0003/ Compacted (default: every 1h) Snapshots: Full database state (daily) ``` ### Compaction Process 1. Enumerate level L-1 files 2. Build page map (newer pages overwrite older) 3. Write merged file skipping lock page 4. Preserve earliest `CreatedAt` from source files 5. Delete old L0 files when promoting to L1 ```go // Page deduplication: latest version wins for _, file := range files { for _, page := range file.Pages { pageMap[page.Number] = page } } ``` ## Checksums LTX uses CRC-64 ECMA checksums (`hash/crc64` with `crc64.ECMA` table): - **PreApplyChecksum**: Database state before applying this file - **PostApplyChecksum**: Database state after applying this file - **FileChecksum**: Integrity of the entire LTX file ## Reading LTX Files ```go dec := ltx.NewDecoder(reader) header, err := dec.Header() for { var hdr ltx.PageHeader data := make([]byte, header.PageSize) if err := dec.DecodePage(&hdr, data); err == io.EOF { break } // Process hdr.Pgno and data } trailer := dec.Trailer() ``` ## Writing LTX Files ```go enc := ltx.NewEncoder(writer) enc.EncodeHeader(header) for _, page := range pages { if page.Number == ltx.LockPgno(pageSize) { continue // Skip lock page } enc.EncodePage(pageHeader, pageData) } enc.EncodePageIndex(index) enc.EncodeTrailer() enc.Close() ``` ## CLI Inspection ```bash litestream ltx /path/to/db.sqlite litestream ltx s3://bucket/db ``` For low-level inspection, use the Go API with `ltx.NewDecoder`. ## WAL to LTX Conversion SQLite WAL frames are converted to LTX format during replication: 1. Read WAL frames from the WAL file 2. Skip the lock page 3. Add checksums and build page index 4. Write as immutable LTX file 5. Upload to storage backend ================================================ FILE: skills/litestream/references/PATTERNS.md ================================================ # Litestream Code Patterns Reference Condensed code patterns and anti-patterns for agents writing Litestream code. ## Architectural Boundaries ``` DB Layer (db.go) → Database state, restoration, monitoring Replica Layer (replica.go) → Replication mechanics only Storage Layer → ReplicaClient implementations ``` ### DO: Handle database state in DB layer ```go func (db *DB) init() error { if db.needsRestore() { if err := db.restore(); err != nil { return err } } return db.replica.Start() // Replica focuses only on replication } ``` ### DON'T: Put database state logic in Replica layer ```go // WRONG func (r *Replica) Start() error { if needsRestore() { // Wrong layer! restoreDatabase() } } ``` ## Atomic File Operations ### DO: Write to temp file, then rename ```go func writeFileAtomic(path string, data []byte) error { dir := filepath.Dir(path) tmpFile, err := os.CreateTemp(dir, ".tmp-*") if err != nil { return fmt.Errorf("create temp file: %w", err) } tmpPath := tmpFile.Name() defer func() { if tmpFile != nil { tmpFile.Close() os.Remove(tmpPath) } }() if _, err := tmpFile.Write(data); err != nil { return fmt.Errorf("write temp file: %w", err) } if err := tmpFile.Sync(); err != nil { return fmt.Errorf("sync temp file: %w", err) } if err := tmpFile.Close(); err != nil { return fmt.Errorf("close temp file: %w", err) } tmpFile = nil if err := os.Rename(tmpPath, path); err != nil { os.Remove(tmpPath) return fmt.Errorf("rename to final path: %w", err) } return nil } ``` ### DON'T: Write directly to final location ```go // WRONG - Can leave partial files on failure os.WriteFile(path, data, 0644) ``` ## Error Handling ### DO: Return errors immediately ```go func (db *DB) validatePosition() error { dpos, err := db.Pos() if err != nil { return err } rpos := replica.Pos() if dpos.TXID < rpos.TXID { return fmt.Errorf("database position (%v) behind replica (%v)", dpos, rpos) } return nil } ``` ### DON'T: Log and continue on critical errors ```go // WRONG if err := processFile(file); err != nil { log.Printf("error: %v", err) // Just logging! // Continuing is dangerous } ``` ### DO: Return errors from loops ```go func (db *DB) processFiles() error { for _, file := range files { if err := processFile(file); err != nil { return fmt.Errorf("process file %s: %w", file, err) } } return nil } ``` ## Locking Patterns ### DO: Use Lock() for writes ```go r.mu.Lock() defer r.mu.Unlock() r.pos = pos ``` ### DON'T: Use RLock for write operations ```go // WRONG - Race condition r.mu.RLock() defer r.mu.RUnlock() r.pos = pos // Writing with RLock! ``` ### Lock Ordering Always acquire in order: Store.mu → DB.mu → DB.chkMu → Replica.mu ## Compaction Patterns ### DO: Read from local when available ```go f, err := os.Open(db.LTXPath(info.Level, info.MinTXID, info.MaxTXID)) if err == nil { return f, nil // Local copy is complete and consistent } return replica.Client.OpenLTXFile(...) // Fall back to remote ``` ### DON'T: Read from remote during compaction ```go // WRONG - Can get partial/corrupt data from eventually consistent storage f, err := client.OpenLTXFile(ctx, level, minTXID, maxTXID, 0, 0) ``` ### DO: Preserve earliest timestamp ```go info.CreatedAt = oldestSourceFile.CreatedAt ``` ### DON'T: Use current time during compaction ```go // WRONG - Loses timestamp granularity for point-in-time restores info := <x.FileInfo{CreatedAt: time.Now()} ``` ## Lock Page Handling The lock page at 1 GB (0x40000000) must always be skipped: ```go lockPgno := ltx.LockPgno(pageSize) if pgno == lockPgno { continue } ``` | Page Size | Lock Page Number | |-----------|------------------| | 4 KB | 262145 | | 8 KB | 131073 | | 16 KB | 65537 | | 32 KB | 32769 | ## Common Pitfalls 1. **Mixing architectural concerns**: DB state logic in Replica layer 2. **Recreating existing functionality**: Use `db.verify()` for snapshots 3. **Ignoring lock page**: Must skip during replication and compaction 4. **Generic error types**: Return `os.ErrNotExist` for missing files 5. **Blocking iterators**: Use lazy pagination for `LTXFiles` 6. **Ignoring context**: Check `ctx.Done()` in long operations ================================================ FILE: skills/litestream/references/REPLICA_CLIENT_GUIDE.md ================================================ # ReplicaClient Implementation Reference Condensed reference for agents implementing or modifying Litestream storage backends. ## Interface Contract From `replica_client.go`: ```go type ReplicaClient interface { Type() string Init(ctx context.Context) error LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) DeleteLTXFiles(ctx context.Context, a []*ltx.FileInfo) error DeleteAll(ctx context.Context) error } ``` ### Method Contracts **Type()**: Return identifier string (e.g., "s3", "gcs", "file"). **Init()**: Initialize connection. Must be idempotent (no-op if already initialized). **LTXFiles()**: Return iterator sorted by MinTXID. `seek` starts from given TXID. `useMetadata=true` fetches accurate timestamps (required for point-in-time restore). `useMetadata=false` uses fast timestamps for normal operations. **OpenLTXFile()**: Open file for reading. Must support partial reads via `offset`/`size`. Return `os.ErrNotExist` if file is missing. **WriteLTXFile()**: Write file to storage. Set `CreatedAt` from backend metadata or upload time. **DeleteLTXFiles()**: Delete one or more files. Batch if possible. **DeleteAll()**: Delete all files for this database. ## Implementation Checklist ### Required - [ ] All interface methods implemented - [ ] `Init()` is idempotent - [ ] Partial reads supported (`offset`/`size` in `OpenLTXFile`) - [ ] `os.ErrNotExist` returned for missing files - [ ] Context cancellation handled in all methods - [ ] `CreatedAt` timestamps preserved in `WriteLTXFile` - [ ] Concurrent operations supported - [ ] Proper cleanup in `DeleteAll` ### Optional - [ ] Connection pooling - [ ] Retry logic with exponential backoff - [ ] Request batching for deletes - [ ] Compression / encryption at rest - [ ] Bandwidth throttling ## Eventual Consistency Many cloud backends (S3, R2, etc.) are eventually consistent: - Recently written files may not be immediately visible - Listed files may be only partially readable - Deletes may not take effect immediately ### Pattern: Read Local First During compaction, always prefer local files: ```go f, err := os.Open(db.LTXPath(info.Level, info.MinTXID, info.MaxTXID)) if err == nil { return f, nil } return replica.Client.OpenLTXFile(...) ``` ### Pattern: Retry with Backoff ```go backoff := 100 * time.Millisecond for i := 0; i < 5; i++ { reader, err := c.openFile(ctx, path, offset, size) if err == nil { return reader, nil } if errors.Is(err, os.ErrNotExist) { return nil, err // Don't retry definitive errors } select { case <-ctx.Done(): return nil, ctx.Err() case <-time.After(backoff): backoff *= 2 } } ``` ### Pattern: Lazy Iterator ```go func (c *Client) LTXFiles(...) (ltx.FileIterator, error) { return &lazyIterator{ client: c, level: level, seek: seek, pageSize: 1000, // Paginate, don't load all at once }, nil } ``` ## Error Handling ### Standard Error Types ```go // File not found return nil, os.ErrNotExist // Permission denied return nil, os.ErrPermission // Context cancelled return nil, ctx.Err() // Wrapped errors return nil, fmt.Errorf("s3 download failed: %w", err) ``` ### Retryable Error Classification ```go func isRetryable(err error) bool { var netErr net.Error if errors.As(err, &netErr) && netErr.Temporary() { return true } // HTTP 429, 500, 502, 503, 504 if errors.Is(err, context.DeadlineExceeded) { return true } return false } ``` ## Common Mistakes ### 1. Not Handling Partial Reads ```go // WRONG - Ignores offset/size return c.storage.Download(path) // CORRECT if offset == 0 && size == 0 { return c.storage.Download(path) } return c.storage.DownloadRange(path, offset, offset+size-1) ``` ### 2. Not Preserving CreatedAt ```go // WRONG return <x.FileInfo{CreatedAt: time.Now()} // CORRECT - Use backend metadata return <x.FileInfo{CreatedAt: modTime} ``` ### 3. Wrong Error Types ```go // WRONG return nil, fmt.Errorf("not found") // CORRECT if resp.StatusCode == 404 { return nil, os.ErrNotExist } ``` ### 4. Ignoring Context ```go // WRONG - Could run forever for i := 0; i < 1000000; i++ { doWork() } // CORRECT select { case <-ctx.Done(): return nil, ctx.Err() default: } ``` ### 5. Loading All Files at Once ```go // WRONG - Could be millions of files allFiles, _ := c.loadAllFiles(level) // CORRECT - Lazy pagination return &lazyIterator{pageSize: 1000}, nil ``` ## Path Construction ```go func (c *Client) ltxDir(level int) string { if level == SnapshotLevel { return path.Join(c.Path, "snapshots") } return path.Join(c.Path, "ltx", fmt.Sprintf("%04d", level)) } ``` ## Reference Implementations - **Simplest**: `file/replica_client.go` (direct file I/O, no network) - **Most complete**: `s3/replica_client.go` (multipart uploads, retries, signing) Start with the file backend for understanding, then study S3 for advanced patterns. ## Testing Requirements - Unit tests with >80% coverage - Integration tests with build tag - Test partial reads, concurrent operations, missing files - Run with `-race` flag - Verify `os.ErrNotExist` for missing files - Test context cancellation ================================================ FILE: skills/litestream/references/SQLITE_INTERNALS.md ================================================ # SQLite Internals Reference Condensed reference for agents working with SQLite internals in Litestream. ## SQLite File Structure ``` database.db Main database file (pages) database.db-wal Write-ahead log database.db-shm Shared memory file (coordination) ``` ## Write-Ahead Log (WAL) WAL is SQLite's method for atomic commits: - Changes written to WAL first, database unchanged until checkpoint - Readers merge WAL + database for consistent view - Litestream monitors WAL and converts frames to LTX format ### WAL File Structure ``` +------------------+ | WAL Header | 32 bytes +------------------+ | Frame 1 Header | 24 bytes | Frame 1 Data | PageSize bytes +------------------+ | Frame 2 Header | 24 bytes | Frame 2 Data | PageSize bytes +------------------+ | ... | ``` ### WAL Header (32 bytes) ``` Magic (4B) | FileFormat (4B) | PageSize (4B) | Checkpoint (4B) | Salt1 (4B) | Salt2 (4B) | Checksum1 (4B) | Checksum2 (4B) ``` Magic: `0x377f0682` or `0x377f0683` ### WAL Frame Header (24 bytes) ``` PageNumber (4B) | DbSize (4B) | Salt1 (4B) | Salt2 (4B) | Checksum1 (4B) | Checksum2 (4B) ``` ## The 1 GB Lock Page SQLite reserves a page at exactly 0x40000000 bytes (1 GB) for locking. This is the single most important SQLite detail for Litestream development. ```go const PENDING_BYTE = 0x40000000 func LockPgno(pageSize int) uint32 { return uint32(PENDING_BYTE/pageSize) + 1 } ``` | Page Size | Lock Page Number | |-----------|------------------| | 4 KB | 262145 | | 8 KB | 131073 | | 16 KB | 65537 | | 32 KB | 32769 | | 64 KB | 16385 | Rules: - Cannot contain data; SQLite never writes user data here - Must be skipped during replication and compaction - Only affects databases > 1 GB - Page number changes with page size Implementation in Litestream: ```go for pgno := uint32(1); pgno <= maxPgno; pgno++ { if pgno == ltx.LockPgno(db.pageSize) { continue } processPage(pgno) } ``` ## Page Structure SQLite divides databases into fixed-size pages (typically 4096 bytes): - Page numbers are 1-based - Types: B-tree interior, B-tree leaf, overflow, freelist, lock byte page ## Transaction Types 1. **Deferred** (default): Lock acquired on first use 2. **Immediate**: RESERVED lock acquired immediately 3. **Exclusive**: EXCLUSIVE lock acquired immediately ### Lock Hierarchy UNLOCKED → SHARED → RESERVED → PENDING → EXCLUSIVE - SHARED: Multiple readers allowed - RESERVED: Signals intent to write - PENDING: Blocks new SHARED locks - EXCLUSIVE: Single writer, no readers ## Long-Running Read Transaction Litestream maintains a read transaction for consistency: ```go func (db *DB) initReadTx() error { tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{ReadOnly: true}) if err != nil { return err } var dummy string err = tx.QueryRow("SELECT ''").Scan(&dummy) if err != nil { tx.Rollback() return err } db.rtx = tx return nil } ``` Purpose: - Prevents checkpoint past our read point - Ensures consistent database view - Allows reading historical pages from WAL ## Checkpoint Modes | Mode | Behavior | |----------|-----------------------------------------------| | PASSIVE | Non-blocking; fails if readers present | | FULL | Waits for readers; blocks new readers | | RESTART | Like FULL + resets WAL start (removed in #724) | | TRUNCATE | Like RESTART + truncates WAL to zero | ### Litestream Checkpoint Strategy ``` WAL pages > TruncatePageN → TRUNCATE (emergency) WAL pages > MinCheckpointPageN → PASSIVE CheckpointInterval elapsed → PASSIVE ``` Note: RESTART mode permanently removed due to issue #724 (write-blocking). ## Important SQLite Pragmas ```sql PRAGMA journal_mode = WAL; -- Required for Litestream PRAGMA page_size; -- Get page size PRAGMA page_count; -- Total pages in database PRAGMA freelist_count; -- Free pages PRAGMA wal_checkpoint(PASSIVE); -- Non-blocking checkpoint PRAGMA wal_autocheckpoint = 10000; -- High threshold (prevent interference) PRAGMA busy_timeout = 5000; -- Wait 5s for locks PRAGMA synchronous = NORMAL; -- Safe with WAL PRAGMA cache_size = -64000; -- 64 MB cache PRAGMA integrity_check; -- Verify database integrity ``` ## Critical SQLite Behaviors 1. **Automatic checkpoint**: Default at 1000 WAL pages; set high threshold to prevent interference with Litestream's control 2. **Busy timeout**: Default is 0 (immediate failure); set a reasonable timeout 3. **Synchronous mode**: NORMAL is safe with WAL mode 4. **Page cache**: In-memory cache; configure with negative KB or positive pages ## WAL to LTX Conversion Litestream converts WAL frames to LTX: 1. Read WAL header and validate magic number 2. Iterate frames, reading page number and data 3. Skip lock page (`pgno == LockPgno(pageSize)`) 4. Calculate checksums 5. Write as immutable LTX file with page index ================================================ FILE: skills/litestream/references/TESTING_GUIDE.md ================================================ # Litestream Testing Reference Condensed reference for agents writing and debugging Litestream tests. ## Testing Philosophy 1. Test at multiple levels: unit, integration, end-to-end 2. Focus on edge cases: >1 GB databases, eventual consistency 3. Use real SQLite: avoid mocking SQLite behavior 4. Always run with `-race` flag 5. Use fixed seeds and timestamps for deterministic tests ## Essential Commands ```bash # Full test suite with race detection go test -race -v ./... # Specific test areas go test -race -v -run TestReplica_Sync ./... go test -race -v -run TestDB_Sync ./... go test -race -v -run TestStore_CompactDB ./... # Coverage go test -coverprofile=coverage.out ./... go tool cover -html=coverage.out go tool cover -func=coverage.out | grep total # Integration tests (backend-specific) go test -v ./replica_client_test.go -integration s3 go test -v ./replica_client_test.go -integration gcs ``` ## Lock Page Testing The lock page at 1 GB must be skipped. Test with databases spanning this boundary using multiple page sizes: ```bash ./bin/litestream-test populate -db test.db -page-size 4096 -target-size 2GB ./bin/litestream-test validate -source-db test.db -replica-url file:///tmp/replica ``` Test all page size variants: | Page Size | Lock Page | Test Target | |-----------|-----------|-------------| | 4 KB | 262145 | >1.0 GB | | 8 KB | 131073 | >1.0 GB | | 16 KB | 65537 | >1.0 GB | | 32 KB | 32769 | >1.0 GB | Verify lock page is not present in any LTX file after replication. ## Race Condition Testing Race-prone areas (always run with `-race`): 1. **Position updates**: Concurrent `SetPos()` + `Pos()` on Replica 2. **WAL monitoring**: Concurrent writes + monitor + checkpoint 3. **Compaction**: Concurrent compaction at different levels ### Pattern: Concurrent Test ```go func TestConcurrent(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() var wg sync.WaitGroup // Start writer, monitor, checkpoint goroutines // Use ctx for shutdown, wg for cleanup wg.Wait() } ``` ## Common Test Failures ### 1. Database Locked ```go // WRONG - WAL mode not enabled db, _ := sql.Open("sqlite", "test.db") // CORRECT db, _ := sql.Open("sqlite", "test.db?_journal=WAL") ``` ### 2. Timing Issues ```go // WRONG - Data may not be replicated yet WriteData(db) result := ReadReplica() // CORRECT - Explicit sync WriteData(db) db.Sync(context.Background()) result := ReadReplica() ``` ### 3. Goroutine Leaks ```go // WRONG - Goroutine outlives test go func() { time.Sleep(10 * time.Second) doWork() }() // CORRECT - Use context + WaitGroup ctx, cancel := context.WithCancel(context.Background()) defer cancel() var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done() select { case <-ctx.Done(): return case <-time.After(10 * time.Second): doWork() } }() cancel() wg.Wait() ``` ### 4. File Handle Leaks ```go // Always use defer for cleanup f, err := os.Open("test.db") require.NoError(t, err) defer f.Close() ``` ## Test Helper Patterns ```go func NewTestDB(t testing.TB) *litestream.DB { t.Helper() path := filepath.Join(t.TempDir(), "test.db") conn, err := sql.Open("sqlite", path+"?_journal=WAL") require.NoError(t, err) _, err = conn.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY, data BLOB)") require.NoError(t, err) conn.Close() db := litestream.NewDB(path) db.MonitorInterval = 10 * time.Millisecond db.MinCheckpointPageN = 100 err = db.Open() require.NoError(t, err) t.Cleanup(func() { db.Close(context.Background()) }) return db } ``` ## Coverage Requirements | Package | Target | |--------------------|--------| | Core (db, replica) | >80% | | Replica clients | >70% | | Utilities | >60% | ## Table-Driven Tests ```go func TestCheckpoint(t *testing.T) { tests := []struct { name string mode string wantErr bool }{ {"Passive", "PASSIVE", false}, {"Full", "FULL", false}, {"Truncate", "TRUNCATE", false}, {"Invalid", "INVALID", true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // ... }) } } ``` ================================================ FILE: skills/litestream/scripts/validate-setup.sh ================================================ #!/bin/sh # validate-setup.sh - Verify Litestream development environment # POSIX-compatible script for validating build tools and project health. set -e PASS=0 FAIL=0 WARN=0 pass() { PASS=$((PASS + 1)) printf " [PASS] %s\n" "$1" } fail() { FAIL=$((FAIL + 1)) printf " [FAIL] %s\n" "$1" } warn() { WARN=$((WARN + 1)) printf " [WARN] %s\n" "$1" } printf "Litestream Development Environment Validation\n" printf "==============================================\n\n" # Check Go installation printf "Checking Go...\n" if command -v go >/dev/null 2>&1; then GO_VERSION=$(go version | sed 's/.*go\([0-9]*\.[0-9]*\).*/\1/') GO_MAJOR=$(echo "$GO_VERSION" | cut -d. -f1) GO_MINOR=$(echo "$GO_VERSION" | cut -d. -f2) if [ "$GO_MAJOR" -ge 1 ] && [ "$GO_MINOR" -ge 24 ]; then pass "Go $(go version | sed 's/.*go/go/' | cut -d' ' -f1) installed (>= 1.24 required)" else fail "Go $GO_VERSION found but >= 1.24 required" fi else fail "Go not found in PATH" fi # Check project builds printf "\nChecking build...\n" if go build -o /dev/null ./cmd/litestream 2>/dev/null; then pass "Project builds successfully" else fail "Project build failed (go build -o /dev/null ./cmd/litestream)" fi # Check go vet printf "\nChecking go vet...\n" if go vet ./... 2>/dev/null; then pass "go vet passes" else fail "go vet reports issues (run: go vet ./...)" fi # Quick smoke test (run a fast subset of tests) printf "\nChecking tests (smoke test)...\n" if go test -race -short -count=1 ./... >/dev/null 2>&1; then pass "Tests pass with race detector (-short mode)" else # Try without race detector in case of environment issues if go test -short -count=1 ./... >/dev/null 2>&1; then warn "Tests pass but race detector may not be supported on this platform" else fail "Tests failing (run: go test -race -short ./...)" fi fi # Check pre-commit (optional) printf "\nChecking optional tools...\n" if command -v pre-commit >/dev/null 2>&1; then pass "pre-commit installed" else warn "pre-commit not found (optional; install: pip install pre-commit)" fi # Check git if command -v git >/dev/null 2>&1; then pass "git installed" else fail "git not found in PATH" fi # Summary printf "\n==============================================\n" printf "Results: %d passed, %d failed, %d warnings\n" "$PASS" "$FAIL" "$WARN" if [ "$FAIL" -gt 0 ]; then printf "\nEnvironment has issues that must be resolved.\n" exit 1 else printf "\nEnvironment is ready for Litestream development.\n" exit 0 fi ================================================ FILE: src/litestream-vfs.c ================================================ #include "litestream-vfs.h" #include "sqlite3.h" #include "sqlite3ext.h" #include "sqlite3vfs.h" #include #include /* sqlite3vfs already called SQLITE_EXTENSION_INIT1 */ extern const sqlite3_api_routines *sqlite3_api; /* Go function declarations */ extern char* LitestreamVFSRegister(void); extern char* GoLitestreamRegisterConnection(void* db, sqlite3_uint64 file_id); extern char* GoLitestreamUnregisterConnection(void* db); extern char* GoLitestreamSetTime(void* db, char* timestamp); extern char* GoLitestreamResetTime(void* db); extern char* GoLitestreamTime(void* db, char** out); extern char* GoLitestreamTxid(void* db, char** out); extern char* GoLitestreamLag(void* db, sqlite3_int64* out); /* Internal function declarations */ static int litestream_register_connection(sqlite3* db); static void litestream_set_time_impl(sqlite3_context* ctx, int argc, sqlite3_value** argv); static void litestream_time_impl(sqlite3_context* ctx, int argc, sqlite3_value** argv); static void litestream_txid_impl(sqlite3_context* ctx, int argc, sqlite3_value** argv); static void litestream_lag_impl(sqlite3_context* ctx, int argc, sqlite3_value** argv); static void litestream_function_destroy(void* db); static void litestream_auto_extension(sqlite3* db, const char** pzErrMsg, const struct sqlite3_api_routines* pApi); /* This routine is called when the extension is loaded. */ int sqlite3_litestreamvfs_init(sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi) { int rc = SQLITE_OK; SQLITE_EXTENSION_INIT2(pApi); /* Call into Go to register the VFS and check for errors. */ char* err = LitestreamVFSRegister(); if (err != NULL) { *pzErrMsg = sqlite3_mprintf("%s", err); free(err); return SQLITE_ERROR; } /* Register SQL functions for new connections. */ rc = sqlite3_auto_extension((void (*)(void))litestream_auto_extension); if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY; return rc; } static void litestream_auto_extension(sqlite3* db, const char** pzErrMsg, const struct sqlite3_api_routines* pApi) { (void)pzErrMsg; (void)pApi; if (litestream_register_connection(db) != SQLITE_OK) { return; } /* litestream_set_time(timestamp) - for time travel */ sqlite3_create_function_v2(db, "litestream_set_time", 1, SQLITE_UTF8 | SQLITE_DIRECTONLY, db, litestream_set_time_impl, 0, 0, litestream_function_destroy); /* Read-only functions: litestream_time(), litestream_txid(), litestream_lag() */ sqlite3_create_function_v2(db, "litestream_time", 0, SQLITE_UTF8, db, litestream_time_impl, 0, 0, 0); sqlite3_create_function_v2(db, "litestream_txid", 0, SQLITE_UTF8, db, litestream_txid_impl, 0, 0, 0); sqlite3_create_function_v2(db, "litestream_lag", 0, SQLITE_UTF8, db, litestream_lag_impl, 0, 0, 0); } static int litestream_register_connection(sqlite3* db) { sqlite3_file* file = 0; int rc = sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, &file); if (rc != SQLITE_OK || file == 0) { return rc; } if (file->pMethods != &s3vfs_io_methods) { /* Not using the litestream VFS. */ return SQLITE_DONE; } sqlite3_uint64 file_id = ((s3vfsFile*)file)->id; char* err = GoLitestreamRegisterConnection(db, file_id); if (err != 0) { free(err); return SQLITE_ERROR; } return SQLITE_OK; } static void litestream_set_time_impl(sqlite3_context* ctx, int argc, sqlite3_value** argv) { if (argc != 1) { sqlite3_result_error(ctx, "expected timestamp argument", -1); return; } const unsigned char* ts = sqlite3_value_text(argv[0]); if (!ts) { sqlite3_result_error(ctx, "timestamp required", -1); return; } /* Handle special 'LATEST' value */ if (sqlite3_stricmp((const char*)ts, "LATEST") == 0) { sqlite3* db = sqlite3_context_db_handle(ctx); char* err = GoLitestreamResetTime(db); if (err != 0) { sqlite3_result_error(ctx, err, -1); free(err); return; } sqlite3_result_null(ctx); return; } sqlite3* db = sqlite3_context_db_handle(ctx); char* err = GoLitestreamSetTime(db, (char*)ts); if (err != 0) { sqlite3_result_error(ctx, err, -1); free(err); return; } sqlite3_result_null(ctx); } static void litestream_time_impl(sqlite3_context* ctx, int argc, sqlite3_value** argv) { (void)argc; (void)argv; sqlite3* db = sqlite3_context_db_handle(ctx); char* out = 0; char* err = GoLitestreamTime(db, &out); if (err != 0) { sqlite3_result_error(ctx, err, -1); free(err); return; } sqlite3_result_text(ctx, out, -1, SQLITE_TRANSIENT); free(out); } static void litestream_txid_impl(sqlite3_context* ctx, int argc, sqlite3_value** argv) { (void)argc; (void)argv; sqlite3* db = sqlite3_context_db_handle(ctx); char* out = 0; char* err = GoLitestreamTxid(db, &out); if (err != 0) { sqlite3_result_error(ctx, err, -1); free(err); return; } sqlite3_result_text(ctx, out, -1, SQLITE_TRANSIENT); free(out); } static void litestream_lag_impl(sqlite3_context* ctx, int argc, sqlite3_value** argv) { (void)argc; (void)argv; sqlite3* db = sqlite3_context_db_handle(ctx); sqlite3_int64 out = 0; char* err = GoLitestreamLag(db, &out); if (err != 0) { sqlite3_result_error(ctx, err, -1); free(err); return; } sqlite3_result_int64(ctx, out); } static void litestream_function_destroy(void* db) { if (db == 0) { return; } char* err = GoLitestreamUnregisterConnection(db); if (err != 0) { free(err); } } ================================================ FILE: src/sqlite3.h ================================================ /* ** 2001-09-15 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This header file defines the interface that the SQLite library ** presents to client programs. If a C-function, structure, datatype, ** or constant definition does not appear in this file, then it is ** not a published API of SQLite, is subject to change without ** notice, and should not be referenced by programs that use SQLite. ** ** Some of the definitions that are in this file are marked as ** "experimental". Experimental interfaces are normally new ** features recently added to SQLite. We do not anticipate changes ** to experimental interfaces but reserve the right to make minor changes ** if experience from use "in the wild" suggest such changes are prudent. ** ** The official C-language API documentation for SQLite is derived ** from comments in this file. This file is the authoritative source ** on how SQLite interfaces are supposed to operate. ** ** The name of this file under configuration management is "sqlite.h.in". ** The makefile makes some minor changes to this file (such as inserting ** the version number) and changes its name to "sqlite3.h" as ** part of the build process. */ #ifndef SQLITE3_H #define SQLITE3_H #include /* Needed for the definition of va_list */ /* ** Make sure we can call this stuff from C++. */ #ifdef __cplusplus extern "C" { #endif /* ** Facilitate override of interface linkage and calling conventions. ** Be aware that these macros may not be used within this particular ** translation of the amalgamation and its associated header file. ** ** The SQLITE_EXTERN and SQLITE_API macros are used to instruct the ** compiler that the target identifier should have external linkage. ** ** The SQLITE_CDECL macro is used to set the calling convention for ** public functions that accept a variable number of arguments. ** ** The SQLITE_APICALL macro is used to set the calling convention for ** public functions that accept a fixed number of arguments. ** ** The SQLITE_STDCALL macro is no longer used and is now deprecated. ** ** The SQLITE_CALLBACK macro is used to set the calling convention for ** function pointers. ** ** The SQLITE_SYSAPI macro is used to set the calling convention for ** functions provided by the operating system. ** ** Currently, the SQLITE_CDECL, SQLITE_APICALL, SQLITE_CALLBACK, and ** SQLITE_SYSAPI macros are used only when building for environments ** that require non-default calling conventions. */ #ifndef SQLITE_EXTERN # define SQLITE_EXTERN extern #endif #ifndef SQLITE_API # define SQLITE_API #endif #ifndef SQLITE_CDECL # define SQLITE_CDECL #endif #ifndef SQLITE_APICALL # define SQLITE_APICALL #endif #ifndef SQLITE_STDCALL # define SQLITE_STDCALL SQLITE_APICALL #endif #ifndef SQLITE_CALLBACK # define SQLITE_CALLBACK #endif #ifndef SQLITE_SYSAPI # define SQLITE_SYSAPI #endif /* ** These no-op macros are used in front of interfaces to mark those ** interfaces as either deprecated or experimental. New applications ** should not use deprecated interfaces - they are supported for backwards ** compatibility only. Application writers should be aware that ** experimental interfaces are subject to change in point releases. ** ** These macros used to resolve to various kinds of compiler magic that ** would generate warning messages when they were used. But that ** compiler magic ended up generating such a flurry of bug reports ** that we have taken it all out and gone back to using simple ** noop macros. */ #define SQLITE_DEPRECATED #define SQLITE_EXPERIMENTAL /* ** Ensure these symbols were not defined by some previous header file. */ #ifdef SQLITE_VERSION # undef SQLITE_VERSION #endif #ifdef SQLITE_VERSION_NUMBER # undef SQLITE_VERSION_NUMBER #endif /* ** CAPI3REF: Compile-Time Library Version Numbers ** ** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header ** evaluates to a string literal that is the SQLite version in the ** format "X.Y.Z" where X is the major version number (always 3 for ** SQLite3) and Y is the minor version number and Z is the release number.)^ ** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same ** numbers used in [SQLITE_VERSION].)^ ** The SQLITE_VERSION_NUMBER for any given release of SQLite will also ** be larger than the release from which it is derived. Either Y will ** be held constant and Z will be incremented or else Y will be incremented ** and Z will be reset to zero. ** ** Since [version 3.6.18] ([dateof:3.6.18]), ** SQLite source code has been stored in the ** Fossil configuration management ** system. ^The SQLITE_SOURCE_ID macro evaluates to ** a string which identifies a particular check-in of SQLite ** within its configuration management system. ^The SQLITE_SOURCE_ID ** string contains the date and time of the check-in (UTC) and a SHA1 ** or SHA3-256 hash of the entire source tree. If the source code has ** been edited in any way since it was last checked in, then the last ** four hexadecimal digits of the hash may be modified. ** ** See also: [sqlite3_libversion()], ** [sqlite3_libversion_number()], [sqlite3_sourceid()], ** [sqlite_version()] and [sqlite_source_id()]. */ #define SQLITE_VERSION "3.50.4" #define SQLITE_VERSION_NUMBER 3050004 #define SQLITE_SOURCE_ID "2025-07-30 19:33:53 4d8adfb30e03f9cf27f800a2c1ba3c48fb4ca1b08b0f5ed59a4d5ecbf45e20a3" /* ** CAPI3REF: Run-Time Library Version Numbers ** KEYWORDS: sqlite3_version sqlite3_sourceid ** ** These interfaces provide the same information as the [SQLITE_VERSION], ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros ** but are associated with the library instead of the header file. ^(Cautious ** programmers might include assert() statements in their application to ** verify that values returned by these interfaces match the macros in ** the header, and thus ensure that the application is ** compiled with matching library and header files. ** **
** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
** assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 );
** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
** 
)^ ** ** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION] ** macro. ^The sqlite3_libversion() function returns a pointer to the ** to the sqlite3_version[] string constant. The sqlite3_libversion() ** function is provided for use in DLLs since DLL users usually do not have ** direct access to string constants within the DLL. ^The ** sqlite3_libversion_number() function returns an integer equal to ** [SQLITE_VERSION_NUMBER]. ^(The sqlite3_sourceid() function returns ** a pointer to a string constant whose value is the same as the ** [SQLITE_SOURCE_ID] C preprocessor macro. Except if SQLite is built ** using an edited copy of [the amalgamation], then the last four characters ** of the hash might be different from [SQLITE_SOURCE_ID].)^ ** ** See also: [sqlite_version()] and [sqlite_source_id()]. */ SQLITE_API SQLITE_EXTERN const char sqlite3_version[]; SQLITE_API const char *sqlite3_libversion(void); SQLITE_API const char *sqlite3_sourceid(void); SQLITE_API int sqlite3_libversion_number(void); /* ** CAPI3REF: Run-Time Library Compilation Options Diagnostics ** ** ^The sqlite3_compileoption_used() function returns 0 or 1 ** indicating whether the specified option was defined at ** compile time. ^The SQLITE_ prefix may be omitted from the ** option name passed to sqlite3_compileoption_used(). ** ** ^The sqlite3_compileoption_get() function allows iterating ** over the list of options that were defined at compile time by ** returning the N-th compile time option string. ^If N is out of range, ** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_ ** prefix is omitted from any strings returned by ** sqlite3_compileoption_get(). ** ** ^Support for the diagnostic functions sqlite3_compileoption_used() ** and sqlite3_compileoption_get() may be omitted by specifying the ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time. ** ** See also: SQL functions [sqlite_compileoption_used()] and ** [sqlite_compileoption_get()] and the [compile_options pragma]. */ #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS SQLITE_API int sqlite3_compileoption_used(const char *zOptName); SQLITE_API const char *sqlite3_compileoption_get(int N); #else # define sqlite3_compileoption_used(X) 0 # define sqlite3_compileoption_get(X) ((void*)0) #endif /* ** CAPI3REF: Test To See If The Library Is Threadsafe ** ** ^The sqlite3_threadsafe() function returns zero if and only if ** SQLite was compiled with mutexing code omitted due to the ** [SQLITE_THREADSAFE] compile-time option being set to 0. ** ** SQLite can be compiled with or without mutexes. When ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes ** are enabled and SQLite is threadsafe. When the ** [SQLITE_THREADSAFE] macro is 0, ** the mutexes are omitted. Without the mutexes, it is not safe ** to use SQLite concurrently from more than one thread. ** ** Enabling mutexes incurs a measurable performance penalty. ** So if speed is of utmost importance, it makes sense to disable ** the mutexes. But for maximum safety, mutexes should be enabled. ** ^The default behavior is for mutexes to be enabled. ** ** This interface can be used by an application to make sure that the ** version of SQLite that it is linking against was compiled with ** the desired setting of the [SQLITE_THREADSAFE] macro. ** ** This interface only reports on the compile-time mutex setting ** of the [SQLITE_THREADSAFE] flag. If SQLite is compiled with ** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but ** can be fully or partially disabled using a call to [sqlite3_config()] ** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD], ** or [SQLITE_CONFIG_SERIALIZED]. ^(The return value of the ** sqlite3_threadsafe() function shows only the compile-time setting of ** thread safety, not any run-time changes to that setting made by ** sqlite3_config(). In other words, the return value from sqlite3_threadsafe() ** is unchanged by calls to sqlite3_config().)^ ** ** See the [threading mode] documentation for additional information. */ SQLITE_API int sqlite3_threadsafe(void); /* ** CAPI3REF: Database Connection Handle ** KEYWORDS: {database connection} {database connections} ** ** Each open SQLite database is represented by a pointer to an instance of ** the opaque structure named "sqlite3". It is useful to think of an sqlite3 ** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()] ** and [sqlite3_close_v2()] are its destructors. There are many other ** interfaces (such as ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and ** [sqlite3_busy_timeout()] to name but three) that are methods on an ** sqlite3 object. */ typedef struct sqlite3 sqlite3; /* ** CAPI3REF: 64-Bit Integer Types ** KEYWORDS: sqlite_int64 sqlite_uint64 ** ** Because there is no cross-platform way to specify 64-bit integer types ** SQLite includes typedefs for 64-bit signed and unsigned integers. ** ** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions. ** The sqlite_int64 and sqlite_uint64 types are supported for backwards ** compatibility only. ** ** ^The sqlite3_int64 and sqlite_int64 types can store integer values ** between -9223372036854775808 and +9223372036854775807 inclusive. ^The ** sqlite3_uint64 and sqlite_uint64 types can store integer values ** between 0 and +18446744073709551615 inclusive. */ #ifdef SQLITE_INT64_TYPE typedef SQLITE_INT64_TYPE sqlite_int64; # ifdef SQLITE_UINT64_TYPE typedef SQLITE_UINT64_TYPE sqlite_uint64; # else typedef unsigned SQLITE_INT64_TYPE sqlite_uint64; # endif #elif defined(_MSC_VER) || defined(__BORLANDC__) typedef __int64 sqlite_int64; typedef unsigned __int64 sqlite_uint64; #else typedef long long int sqlite_int64; typedef unsigned long long int sqlite_uint64; #endif typedef sqlite_int64 sqlite3_int64; typedef sqlite_uint64 sqlite3_uint64; /* ** If compiling for a processor that lacks floating point support, ** substitute integer for floating-point. */ #ifdef SQLITE_OMIT_FLOATING_POINT # define double sqlite3_int64 #endif /* ** CAPI3REF: Closing A Database Connection ** DESTRUCTOR: sqlite3 ** ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors ** for the [sqlite3] object. ** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if ** the [sqlite3] object is successfully destroyed and all associated ** resources are deallocated. ** ** Ideally, applications should [sqlite3_finalize | finalize] all ** [prepared statements], [sqlite3_blob_close | close] all [BLOB handles], and ** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated ** with the [sqlite3] object prior to attempting to close the object. ** ^If the database connection is associated with unfinalized prepared ** statements, BLOB handlers, and/or unfinished sqlite3_backup objects then ** sqlite3_close() will leave the database connection open and return ** [SQLITE_BUSY]. ^If sqlite3_close_v2() is called with unfinalized prepared ** statements, unclosed BLOB handlers, and/or unfinished sqlite3_backups, ** it returns [SQLITE_OK] regardless, but instead of deallocating the database ** connection immediately, it marks the database connection as an unusable ** "zombie" and makes arrangements to automatically deallocate the database ** connection after all prepared statements are finalized, all BLOB handles ** are closed, and all backups have finished. The sqlite3_close_v2() interface ** is intended for use with host languages that are garbage collected, and ** where the order in which destructors are called is arbitrary. ** ** ^If an [sqlite3] object is destroyed while a transaction is open, ** the transaction is automatically rolled back. ** ** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)] ** must be either a NULL ** pointer or an [sqlite3] object pointer obtained ** from [sqlite3_open()], [sqlite3_open16()], or ** [sqlite3_open_v2()], and not previously closed. ** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer ** argument is a harmless no-op. */ SQLITE_API int sqlite3_close(sqlite3*); SQLITE_API int sqlite3_close_v2(sqlite3*); /* ** The type for a callback function. ** This is legacy and deprecated. It is included for historical ** compatibility and is not documented. */ typedef int (*sqlite3_callback)(void*,int,char**, char**); /* ** CAPI3REF: One-Step Query Execution Interface ** METHOD: sqlite3 ** ** The sqlite3_exec() interface is a convenience wrapper around ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()], ** that allows an application to run multiple statements of SQL ** without having to use a lot of C code. ** ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded, ** semicolon-separate SQL statements passed into its 2nd argument, ** in the context of the [database connection] passed in as its 1st ** argument. ^If the callback function of the 3rd argument to ** sqlite3_exec() is not NULL, then it is invoked for each result row ** coming out of the evaluated SQL statements. ^The 4th argument to ** sqlite3_exec() is relayed through to the 1st argument of each ** callback invocation. ^If the callback pointer to sqlite3_exec() ** is NULL, then no callback is ever invoked and result rows are ** ignored. ** ** ^If an error occurs while evaluating the SQL statements passed into ** sqlite3_exec(), then execution of the current statement stops and ** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec() ** is not NULL then any error message is written into memory obtained ** from [sqlite3_malloc()] and passed back through the 5th parameter. ** To avoid memory leaks, the application should invoke [sqlite3_free()] ** on error message strings returned through the 5th parameter of ** sqlite3_exec() after the error message string is no longer needed. ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to ** NULL before returning. ** ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec() ** routine returns SQLITE_ABORT without invoking the callback again and ** without running any subsequent SQL statements. ** ** ^The 2nd argument to the sqlite3_exec() callback function is the ** number of columns in the result. ^The 3rd argument to the sqlite3_exec() ** callback is an array of pointers to strings obtained as if from ** [sqlite3_column_text()], one for each column. ^If an element of a ** result row is NULL then the corresponding string pointer for the ** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the ** sqlite3_exec() callback is an array of pointers to strings where each ** entry represents the name of corresponding result column as obtained ** from [sqlite3_column_name()]. ** ** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer ** to an empty string, or a pointer that contains only whitespace and/or ** SQL comments, then no SQL statements are evaluated and the database ** is not changed. ** ** Restrictions: ** **
    **
  • The application must ensure that the 1st parameter to sqlite3_exec() ** is a valid and open [database connection]. **
  • The application must not close the [database connection] specified by ** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running. **
  • The application must not modify the SQL statement text passed into ** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running. **
  • The application must not dereference the arrays or string pointers ** passed as the 3rd and 4th callback parameters after it returns. **
*/ SQLITE_API int sqlite3_exec( sqlite3*, /* An open database */ const char *sql, /* SQL to be evaluated */ int (*callback)(void*,int,char**,char**), /* Callback function */ void *, /* 1st argument to callback */ char **errmsg /* Error msg written here */ ); /* ** CAPI3REF: Result Codes ** KEYWORDS: {result code definitions} ** ** Many SQLite functions return an integer result code from the set shown ** here in order to indicate success or failure. ** ** New error codes may be added in future versions of SQLite. ** ** See also: [extended result code definitions] */ #define SQLITE_OK 0 /* Successful result */ /* beginning-of-error-codes */ #define SQLITE_ERROR 1 /* Generic error */ #define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */ #define SQLITE_PERM 3 /* Access permission denied */ #define SQLITE_ABORT 4 /* Callback routine requested an abort */ #define SQLITE_BUSY 5 /* The database file is locked */ #define SQLITE_LOCKED 6 /* A table in the database is locked */ #define SQLITE_NOMEM 7 /* A malloc() failed */ #define SQLITE_READONLY 8 /* Attempt to write a readonly database */ #define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/ #define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */ #define SQLITE_CORRUPT 11 /* The database disk image is malformed */ #define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */ #define SQLITE_FULL 13 /* Insertion failed because database is full */ #define SQLITE_CANTOPEN 14 /* Unable to open the database file */ #define SQLITE_PROTOCOL 15 /* Database lock protocol error */ #define SQLITE_EMPTY 16 /* Internal use only */ #define SQLITE_SCHEMA 17 /* The database schema changed */ #define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */ #define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */ #define SQLITE_MISMATCH 20 /* Data type mismatch */ #define SQLITE_MISUSE 21 /* Library used incorrectly */ #define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ #define SQLITE_AUTH 23 /* Authorization denied */ #define SQLITE_FORMAT 24 /* Not used */ #define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */ #define SQLITE_NOTADB 26 /* File opened that is not a database file */ #define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */ #define SQLITE_WARNING 28 /* Warnings from sqlite3_log() */ #define SQLITE_ROW 100 /* sqlite3_step() has another row ready */ #define SQLITE_DONE 101 /* sqlite3_step() has finished executing */ /* end-of-error-codes */ /* ** CAPI3REF: Extended Result Codes ** KEYWORDS: {extended result code definitions} ** ** In its default configuration, SQLite API routines return one of 30 integer ** [result codes]. However, experience has shown that many of ** these result codes are too coarse-grained. They do not provide as ** much information about problems as programmers might like. In an effort to ** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8] ** and later) include ** support for additional result codes that provide more detailed information ** about errors. These [extended result codes] are enabled or disabled ** on a per database connection basis using the ** [sqlite3_extended_result_codes()] API. Or, the extended code for ** the most recent error can be obtained using ** [sqlite3_extended_errcode()]. */ #define SQLITE_ERROR_MISSING_COLLSEQ (SQLITE_ERROR | (1<<8)) #define SQLITE_ERROR_RETRY (SQLITE_ERROR | (2<<8)) #define SQLITE_ERROR_SNAPSHOT (SQLITE_ERROR | (3<<8)) #define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8)) #define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8)) #define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8)) #define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8)) #define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8)) #define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8)) #define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8)) #define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8)) #define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8)) #define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8)) #define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8)) #define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8)) #define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8)) #define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8)) #define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8)) #define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8)) #define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8)) #define SQLITE_IOERR_SHMOPEN (SQLITE_IOERR | (18<<8)) #define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8)) #define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8)) #define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8)) #define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8)) #define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8)) #define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8)) #define SQLITE_IOERR_GETTEMPPATH (SQLITE_IOERR | (25<<8)) #define SQLITE_IOERR_CONVPATH (SQLITE_IOERR | (26<<8)) #define SQLITE_IOERR_VNODE (SQLITE_IOERR | (27<<8)) #define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8)) #define SQLITE_IOERR_BEGIN_ATOMIC (SQLITE_IOERR | (29<<8)) #define SQLITE_IOERR_COMMIT_ATOMIC (SQLITE_IOERR | (30<<8)) #define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<8)) #define SQLITE_IOERR_DATA (SQLITE_IOERR | (32<<8)) #define SQLITE_IOERR_CORRUPTFS (SQLITE_IOERR | (33<<8)) #define SQLITE_IOERR_IN_PAGE (SQLITE_IOERR | (34<<8)) #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8)) #define SQLITE_LOCKED_VTAB (SQLITE_LOCKED | (2<<8)) #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8)) #define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8)) #define SQLITE_BUSY_TIMEOUT (SQLITE_BUSY | (3<<8)) #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8)) #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8)) #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8)) #define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8)) #define SQLITE_CANTOPEN_DIRTYWAL (SQLITE_CANTOPEN | (5<<8)) /* Not Used */ #define SQLITE_CANTOPEN_SYMLINK (SQLITE_CANTOPEN | (6<<8)) #define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8)) #define SQLITE_CORRUPT_SEQUENCE (SQLITE_CORRUPT | (2<<8)) #define SQLITE_CORRUPT_INDEX (SQLITE_CORRUPT | (3<<8)) #define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8)) #define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8)) #define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8)) #define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8)) #define SQLITE_READONLY_CANTINIT (SQLITE_READONLY | (5<<8)) #define SQLITE_READONLY_DIRECTORY (SQLITE_READONLY | (6<<8)) #define SQLITE_ABORT_ROLLBACK (SQLITE_ABORT | (2<<8)) #define SQLITE_CONSTRAINT_CHECK (SQLITE_CONSTRAINT | (1<<8)) #define SQLITE_CONSTRAINT_COMMITHOOK (SQLITE_CONSTRAINT | (2<<8)) #define SQLITE_CONSTRAINT_FOREIGNKEY (SQLITE_CONSTRAINT | (3<<8)) #define SQLITE_CONSTRAINT_FUNCTION (SQLITE_CONSTRAINT | (4<<8)) #define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8)) #define SQLITE_CONSTRAINT_PRIMARYKEY (SQLITE_CONSTRAINT | (6<<8)) #define SQLITE_CONSTRAINT_TRIGGER (SQLITE_CONSTRAINT | (7<<8)) #define SQLITE_CONSTRAINT_UNIQUE (SQLITE_CONSTRAINT | (8<<8)) #define SQLITE_CONSTRAINT_VTAB (SQLITE_CONSTRAINT | (9<<8)) #define SQLITE_CONSTRAINT_ROWID (SQLITE_CONSTRAINT |(10<<8)) #define SQLITE_CONSTRAINT_PINNED (SQLITE_CONSTRAINT |(11<<8)) #define SQLITE_CONSTRAINT_DATATYPE (SQLITE_CONSTRAINT |(12<<8)) #define SQLITE_NOTICE_RECOVER_WAL (SQLITE_NOTICE | (1<<8)) #define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8)) #define SQLITE_NOTICE_RBU (SQLITE_NOTICE | (3<<8)) #define SQLITE_WARNING_AUTOINDEX (SQLITE_WARNING | (1<<8)) #define SQLITE_AUTH_USER (SQLITE_AUTH | (1<<8)) #define SQLITE_OK_LOAD_PERMANENTLY (SQLITE_OK | (1<<8)) #define SQLITE_OK_SYMLINK (SQLITE_OK | (2<<8)) /* internal use only */ /* ** CAPI3REF: Flags For File Open Operations ** ** These bit values are intended for use in the ** 3rd parameter to the [sqlite3_open_v2()] interface and ** in the 4th parameter to the [sqlite3_vfs.xOpen] method. ** ** Only those flags marked as "Ok for sqlite3_open_v2()" may be ** used as the third argument to the [sqlite3_open_v2()] interface. ** The other flags have historically been ignored by sqlite3_open_v2(), ** though future versions of SQLite might change so that an error is ** raised if any of the disallowed bits are passed into sqlite3_open_v2(). ** Applications should not depend on the historical behavior. ** ** Note in particular that passing the SQLITE_OPEN_EXCLUSIVE flag into ** [sqlite3_open_v2()] does *not* cause the underlying database file ** to be opened using O_EXCL. Passing SQLITE_OPEN_EXCLUSIVE into ** [sqlite3_open_v2()] has historically be a no-op and might become an ** error in future versions of SQLite. */ #define SQLITE_OPEN_READONLY 0x00000001 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_DELETEONCLOSE 0x00000008 /* VFS only */ #define SQLITE_OPEN_EXCLUSIVE 0x00000010 /* VFS only */ #define SQLITE_OPEN_AUTOPROXY 0x00000020 /* VFS only */ #define SQLITE_OPEN_URI 0x00000040 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_MEMORY 0x00000080 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_MAIN_DB 0x00000100 /* VFS only */ #define SQLITE_OPEN_TEMP_DB 0x00000200 /* VFS only */ #define SQLITE_OPEN_TRANSIENT_DB 0x00000400 /* VFS only */ #define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 /* VFS only */ #define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 /* VFS only */ #define SQLITE_OPEN_SUBJOURNAL 0x00002000 /* VFS only */ #define SQLITE_OPEN_SUPER_JOURNAL 0x00004000 /* VFS only */ #define SQLITE_OPEN_NOMUTEX 0x00008000 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_SHAREDCACHE 0x00020000 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_PRIVATECACHE 0x00040000 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_WAL 0x00080000 /* VFS only */ #define SQLITE_OPEN_NOFOLLOW 0x01000000 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_EXRESCODE 0x02000000 /* Extended result codes */ /* Reserved: 0x00F00000 */ /* Legacy compatibility: */ #define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 /* VFS only */ /* ** CAPI3REF: Device Characteristics ** ** The xDeviceCharacteristics method of the [sqlite3_io_methods] ** object returns an integer which is a vector of these ** bit values expressing I/O characteristics of the mass storage ** device that holds the file that the [sqlite3_io_methods] ** refers to. ** ** The SQLITE_IOCAP_ATOMIC property means that all writes of ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values ** mean that writes of blocks that are nnn bytes in size and ** are aligned to an address which is an integer multiple of ** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means ** that when data is appended to a file, the data is appended ** first then the size of the file is extended, never the other ** way around. The SQLITE_IOCAP_SEQUENTIAL property means that ** information is written to disk in the same order as calls ** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that ** after reboot following a crash or power loss, the only bytes in a ** file that were written at the application level might have changed ** and that adjacent bytes, even bytes within the same sector are ** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN ** flag indicates that a file cannot be deleted when open. The ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on ** read-only media and cannot be changed even by processes with ** elevated privileges. ** ** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying ** filesystem supports doing multiple write operations atomically when those ** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and ** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]. ** ** The SQLITE_IOCAP_SUBPAGE_READ property means that it is ok to read ** from the database file in amounts that are not a multiple of the ** page size and that do not begin at a page boundary. Without this ** property, SQLite is careful to only do full-page reads and write ** on aligned pages, with the one exception that it will do a sub-page ** read of the first page to access the database header. */ #define SQLITE_IOCAP_ATOMIC 0x00000001 #define SQLITE_IOCAP_ATOMIC512 0x00000002 #define SQLITE_IOCAP_ATOMIC1K 0x00000004 #define SQLITE_IOCAP_ATOMIC2K 0x00000008 #define SQLITE_IOCAP_ATOMIC4K 0x00000010 #define SQLITE_IOCAP_ATOMIC8K 0x00000020 #define SQLITE_IOCAP_ATOMIC16K 0x00000040 #define SQLITE_IOCAP_ATOMIC32K 0x00000080 #define SQLITE_IOCAP_ATOMIC64K 0x00000100 #define SQLITE_IOCAP_SAFE_APPEND 0x00000200 #define SQLITE_IOCAP_SEQUENTIAL 0x00000400 #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800 #define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000 #define SQLITE_IOCAP_IMMUTABLE 0x00002000 #define SQLITE_IOCAP_BATCH_ATOMIC 0x00004000 #define SQLITE_IOCAP_SUBPAGE_READ 0x00008000 /* ** CAPI3REF: File Locking Levels ** ** SQLite uses one of these integer values as the second ** argument to calls it makes to the xLock() and xUnlock() methods ** of an [sqlite3_io_methods] object. These values are ordered from ** lest restrictive to most restrictive. ** ** The argument to xLock() is always SHARED or higher. The argument to ** xUnlock is either SHARED or NONE. */ #define SQLITE_LOCK_NONE 0 /* xUnlock() only */ #define SQLITE_LOCK_SHARED 1 /* xLock() or xUnlock() */ #define SQLITE_LOCK_RESERVED 2 /* xLock() only */ #define SQLITE_LOCK_PENDING 3 /* xLock() only */ #define SQLITE_LOCK_EXCLUSIVE 4 /* xLock() only */ /* ** CAPI3REF: Synchronization Type Flags ** ** When SQLite invokes the xSync() method of an ** [sqlite3_io_methods] object it uses a combination of ** these integer values as the second argument. ** ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the ** sync operation only needs to flush data to mass storage. Inode ** information need not be flushed. If the lower four bits of the flag ** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics. ** If the lower four bits equal SQLITE_SYNC_FULL, that means ** to use Mac OS X style fullsync instead of fsync(). ** ** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags ** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL ** settings. The [synchronous pragma] determines when calls to the ** xSync VFS method occur and applies uniformly across all platforms. ** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how ** energetic or rigorous or forceful the sync operations are and ** only make a difference on Mac OSX for the default SQLite code. ** (Third-party VFS implementations might also make the distinction ** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the ** operating systems natively supported by SQLite, only Mac OSX ** cares about the difference.) */ #define SQLITE_SYNC_NORMAL 0x00002 #define SQLITE_SYNC_FULL 0x00003 #define SQLITE_SYNC_DATAONLY 0x00010 /* ** CAPI3REF: OS Interface Open File Handle ** ** An [sqlite3_file] object represents an open file in the ** [sqlite3_vfs | OS interface layer]. Individual OS interface ** implementations will ** want to subclass this object by appending additional fields ** for their own use. The pMethods entry is a pointer to an ** [sqlite3_io_methods] object that defines methods for performing ** I/O operations on the open file. */ typedef struct sqlite3_file sqlite3_file; struct sqlite3_file { const struct sqlite3_io_methods *pMethods; /* Methods for an open file */ }; /* ** CAPI3REF: OS Interface File Virtual Methods Object ** ** Every file opened by the [sqlite3_vfs.xOpen] method populates an ** [sqlite3_file] object (or, more commonly, a subclass of the ** [sqlite3_file] object) with a pointer to an instance of this object. ** This object defines the methods used to perform various operations ** against the open file represented by the [sqlite3_file] object. ** ** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method ** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed. The ** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen] ** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element ** to NULL. ** ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or ** [SQLITE_SYNC_FULL]. The first choice is the normal fsync(). ** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY] ** flag may be ORed in to indicate that only the data of the file ** and not its inode needs to be synced. ** ** The integer values to xLock() and xUnlock() are one of **
    **
  • [SQLITE_LOCK_NONE], **
  • [SQLITE_LOCK_SHARED], **
  • [SQLITE_LOCK_RESERVED], **
  • [SQLITE_LOCK_PENDING], or **
  • [SQLITE_LOCK_EXCLUSIVE]. **
** xLock() upgrades the database file lock. In other words, xLock() moves the ** database file lock in the direction NONE toward EXCLUSIVE. The argument to ** xLock() is always one of SHARED, RESERVED, PENDING, or EXCLUSIVE, never ** SQLITE_LOCK_NONE. If the database file lock is already at or above the ** requested lock, then the call to xLock() is a no-op. ** xUnlock() downgrades the database file lock to either SHARED or NONE. ** If the lock is already at or below the requested lock state, then the call ** to xUnlock() is a no-op. ** The xCheckReservedLock() method checks whether any database connection, ** either in this process or in some other process, is holding a RESERVED, ** PENDING, or EXCLUSIVE lock on the file. It returns, via its output ** pointer parameter, true if such a lock exists and false otherwise. ** ** The xFileControl() method is a generic interface that allows custom ** VFS implementations to directly control an open file using the ** [sqlite3_file_control()] interface. The second "op" argument is an ** integer opcode. The third argument is a generic pointer intended to ** point to a structure that may contain arguments or space in which to ** write return values. Potential uses for xFileControl() might be ** functions to enable blocking locks with timeouts, to change the ** locking strategy (for example to use dot-file locks), to inquire ** about the status of a lock, or to break stale locks. The SQLite ** core reserves all opcodes less than 100 for its own use. ** A [file control opcodes | list of opcodes] less than 100 is available. ** Applications that define a custom xFileControl method should use opcodes ** greater than 100 to avoid conflicts. VFS implementations should ** return [SQLITE_NOTFOUND] for file control opcodes that they do not ** recognize. ** ** The xSectorSize() method returns the sector size of the ** device that underlies the file. The sector size is the ** minimum write that can be performed without disturbing ** other bytes in the file. The xDeviceCharacteristics() ** method returns a bit vector describing behaviors of the ** underlying device: ** **
    **
  • [SQLITE_IOCAP_ATOMIC] **
  • [SQLITE_IOCAP_ATOMIC512] **
  • [SQLITE_IOCAP_ATOMIC1K] **
  • [SQLITE_IOCAP_ATOMIC2K] **
  • [SQLITE_IOCAP_ATOMIC4K] **
  • [SQLITE_IOCAP_ATOMIC8K] **
  • [SQLITE_IOCAP_ATOMIC16K] **
  • [SQLITE_IOCAP_ATOMIC32K] **
  • [SQLITE_IOCAP_ATOMIC64K] **
  • [SQLITE_IOCAP_SAFE_APPEND] **
  • [SQLITE_IOCAP_SEQUENTIAL] **
  • [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN] **
  • [SQLITE_IOCAP_POWERSAFE_OVERWRITE] **
  • [SQLITE_IOCAP_IMMUTABLE] **
  • [SQLITE_IOCAP_BATCH_ATOMIC] **
  • [SQLITE_IOCAP_SUBPAGE_READ] **
** ** The SQLITE_IOCAP_ATOMIC property means that all writes of ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values ** mean that writes of blocks that are nnn bytes in size and ** are aligned to an address which is an integer multiple of ** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means ** that when data is appended to a file, the data is appended ** first then the size of the file is extended, never the other ** way around. The SQLITE_IOCAP_SEQUENTIAL property means that ** information is written to disk in the same order as calls ** to xWrite(). ** ** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill ** in the unread portions of the buffer with zeros. A VFS that ** fails to zero-fill short reads might seem to work. However, ** failure to zero-fill short reads will eventually lead to ** database corruption. */ typedef struct sqlite3_io_methods sqlite3_io_methods; struct sqlite3_io_methods { int iVersion; int (*xClose)(sqlite3_file*); int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst); int (*xTruncate)(sqlite3_file*, sqlite3_int64 size); int (*xSync)(sqlite3_file*, int flags); int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize); int (*xLock)(sqlite3_file*, int); int (*xUnlock)(sqlite3_file*, int); int (*xCheckReservedLock)(sqlite3_file*, int *pResOut); int (*xFileControl)(sqlite3_file*, int op, void *pArg); int (*xSectorSize)(sqlite3_file*); int (*xDeviceCharacteristics)(sqlite3_file*); /* Methods above are valid for version 1 */ int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**); int (*xShmLock)(sqlite3_file*, int offset, int n, int flags); void (*xShmBarrier)(sqlite3_file*); int (*xShmUnmap)(sqlite3_file*, int deleteFlag); /* Methods above are valid for version 2 */ int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp); int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p); /* Methods above are valid for version 3 */ /* Additional methods may be added in future releases */ }; /* ** CAPI3REF: Standard File Control Opcodes ** KEYWORDS: {file control opcodes} {file control opcode} ** ** These integer constants are opcodes for the xFileControl method ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()] ** interface. ** **
    **
  • [[SQLITE_FCNTL_LOCKSTATE]] ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This ** opcode causes the xFileControl method to write the current state of ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED], ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE]) ** into an integer that the pArg argument points to. ** This capability is only available if SQLite is compiled with [SQLITE_DEBUG]. ** **
  • [[SQLITE_FCNTL_SIZE_HINT]] ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS ** layer a hint of how large the database file will grow to be during the ** current transaction. This hint is not guaranteed to be accurate but it ** is often close. The underlying VFS might choose to preallocate database ** file space based on this hint in order to help writes to the database ** file run faster. ** **
  • [[SQLITE_FCNTL_SIZE_LIMIT]] ** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that ** implements [sqlite3_deserialize()] to set an upper bound on the size ** of the in-memory database. The argument is a pointer to a [sqlite3_int64]. ** If the integer pointed to is negative, then it is filled in with the ** current limit. Otherwise the limit is set to the larger of the value ** of the integer pointed to and the current database size. The integer ** pointed to is set to the new limit. ** **
  • [[SQLITE_FCNTL_CHUNK_SIZE]] ** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS ** extends and truncates the database file in chunks of a size specified ** by the user. The fourth argument to [sqlite3_file_control()] should ** point to an integer (type int) containing the new chunk-size to use ** for the nominated database. Allocating database file space in large ** chunks (say 1MB at a time), may reduce file-system fragmentation and ** improve performance on some systems. ** **
  • [[SQLITE_FCNTL_FILE_POINTER]] ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer ** to the [sqlite3_file] object associated with a particular database ** connection. See also [SQLITE_FCNTL_JOURNAL_POINTER]. ** **
  • [[SQLITE_FCNTL_JOURNAL_POINTER]] ** The [SQLITE_FCNTL_JOURNAL_POINTER] opcode is used to obtain a pointer ** to the [sqlite3_file] object associated with the journal file (either ** the [rollback journal] or the [write-ahead log]) for a particular database ** connection. See also [SQLITE_FCNTL_FILE_POINTER]. ** **
  • [[SQLITE_FCNTL_SYNC_OMITTED]] ** No longer in use. ** **
  • [[SQLITE_FCNTL_SYNC]] ** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and ** sent to the VFS immediately before the xSync method is invoked on a ** database file descriptor. Or, if the xSync method is not invoked ** because the user has configured SQLite with ** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place ** of the xSync method. In most cases, the pointer argument passed with ** this file-control is NULL. However, if the database file is being synced ** as part of a multi-database commit, the argument points to a nul-terminated ** string containing the transactions super-journal file name. VFSes that ** do not need this signal should silently ignore this opcode. Applications ** should not call [sqlite3_file_control()] with this opcode as doing so may ** disrupt the operation of the specialized VFSes that do require it. ** **
  • [[SQLITE_FCNTL_COMMIT_PHASETWO]] ** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite ** and sent to the VFS after a transaction has been committed immediately ** but before the database is unlocked. VFSes that do not need this signal ** should silently ignore this opcode. Applications should not call ** [sqlite3_file_control()] with this opcode as doing so may disrupt the ** operation of the specialized VFSes that do require it. ** **
  • [[SQLITE_FCNTL_WIN32_AV_RETRY]] ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic ** retry counts and intervals for certain disk I/O operations for the ** windows [VFS] in order to provide robustness in the presence of ** anti-virus programs. By default, the windows VFS will retry file read, ** file write, and file delete operations up to 10 times, with a delay ** of 25 milliseconds before the first retry and with the delay increasing ** by an additional 25 milliseconds with each subsequent retry. This ** opcode allows these two values (10 retries and 25 milliseconds of delay) ** to be adjusted. The values are changed for all database connections ** within the same process. The argument is a pointer to an array of two ** integers where the first integer is the new retry count and the second ** integer is the delay. If either integer is negative, then the setting ** is not changed but instead the prior value of that setting is written ** into the array entry, allowing the current retry settings to be ** interrogated. The zDbName parameter is ignored. ** **
  • [[SQLITE_FCNTL_PERSIST_WAL]] ** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the ** persistent [WAL | Write Ahead Log] setting. By default, the auxiliary ** write ahead log ([WAL file]) and shared memory ** files used for transaction control ** are automatically deleted when the latest connection to the database ** closes. Setting persistent WAL mode causes those files to persist after ** close. Persisting the files is useful when other processes that do not ** have write permission on the directory containing the database file want ** to read the database file, as the WAL and shared memory files must exist ** in order for the database to be readable. The fourth parameter to ** [sqlite3_file_control()] for this opcode should be a pointer to an integer. ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent ** WAL mode. If the integer is -1, then it is overwritten with the current ** WAL persistence setting. ** **
  • [[SQLITE_FCNTL_POWERSAFE_OVERWRITE]] ** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the ** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting ** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the ** xDeviceCharacteristics methods. The fourth parameter to ** [sqlite3_file_control()] for this opcode should be a pointer to an integer. ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage ** mode. If the integer is -1, then it is overwritten with the current ** zero-damage mode setting. ** **
  • [[SQLITE_FCNTL_OVERWRITE]] ** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening ** a write transaction to indicate that, unless it is rolled back for some ** reason, the entire database file will be overwritten by the current ** transaction. This is used by VACUUM operations. ** **
  • [[SQLITE_FCNTL_VFSNAME]] ** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of ** all [VFSes] in the VFS stack. The names are of all VFS shims and the ** final bottom-level VFS are written into memory obtained from ** [sqlite3_malloc()] and the result is stored in the char* variable ** that the fourth parameter of [sqlite3_file_control()] points to. ** The caller is responsible for freeing the memory when done. As with ** all file-control actions, there is no guarantee that this will actually ** do anything. Callers should initialize the char* variable to a NULL ** pointer in case this file-control is not implemented. This file-control ** is intended for diagnostic use only. ** **
  • [[SQLITE_FCNTL_VFS_POINTER]] ** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level ** [VFSes] currently in use. ^(The argument X in ** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be ** of type "[sqlite3_vfs] **". This opcodes will set *X ** to a pointer to the top-level VFS.)^ ** ^When there are multiple VFS shims in the stack, this opcode finds the ** upper-most shim only. ** **
  • [[SQLITE_FCNTL_PRAGMA]] ** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA] ** file control is sent to the open [sqlite3_file] object corresponding ** to the database file to which the pragma statement refers. ^The argument ** to the [SQLITE_FCNTL_PRAGMA] file control is an array of ** pointers to strings (char**) in which the second element of the array ** is the name of the pragma and the third element is the argument to the ** pragma or NULL if the pragma has no argument. ^The handler for an ** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element ** of the char** argument point to a string obtained from [sqlite3_mprintf()] ** or the equivalent and that string will become the result of the pragma or ** the error message if the pragma fails. ^If the ** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal ** [PRAGMA] processing continues. ^If the [SQLITE_FCNTL_PRAGMA] ** file control returns [SQLITE_OK], then the parser assumes that the ** VFS has handled the PRAGMA itself and the parser generates a no-op ** prepared statement if result string is NULL, or that returns a copy ** of the result string if the string is non-NULL. ** ^If the [SQLITE_FCNTL_PRAGMA] file control returns ** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means ** that the VFS encountered an error while handling the [PRAGMA] and the ** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA] ** file control occurs at the beginning of pragma statement analysis and so ** it is able to override built-in [PRAGMA] statements. ** **
  • [[SQLITE_FCNTL_BUSYHANDLER]] ** ^The [SQLITE_FCNTL_BUSYHANDLER] ** file-control may be invoked by SQLite on the database file handle ** shortly after it is opened in order to provide a custom VFS with access ** to the connection's busy-handler callback. The argument is of type (void**) ** - an array of two (void *) values. The first (void *) actually points ** to a function of type (int (*)(void *)). In order to invoke the connection's ** busy-handler, this function should be invoked with the second (void *) in ** the array as the only argument. If it returns non-zero, then the operation ** should be retried. If it returns zero, the custom VFS should abandon the ** current operation. ** **
  • [[SQLITE_FCNTL_TEMPFILENAME]] ** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control ** to have SQLite generate a ** temporary filename using the same algorithm that is followed to generate ** temporary filenames for TEMP tables and other internal uses. The ** argument should be a char** which will be filled with the filename ** written into memory obtained from [sqlite3_malloc()]. The caller should ** invoke [sqlite3_free()] on the result to avoid a memory leak. ** **
  • [[SQLITE_FCNTL_MMAP_SIZE]] ** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the ** maximum number of bytes that will be used for memory-mapped I/O. ** The argument is a pointer to a value of type sqlite3_int64 that ** is an advisory maximum number of bytes in the file to memory map. The ** pointer is overwritten with the old value. The limit is not changed if ** the value originally pointed to is negative, and so the current limit ** can be queried by passing in a pointer to a negative number. This ** file-control is used internally to implement [PRAGMA mmap_size]. ** **
  • [[SQLITE_FCNTL_TRACE]] ** The [SQLITE_FCNTL_TRACE] file control provides advisory information ** to the VFS about what the higher layers of the SQLite stack are doing. ** This file control is used by some VFS activity tracing [shims]. ** The argument is a zero-terminated string. Higher layers in the ** SQLite stack may generate instances of this file control if ** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled. ** **
  • [[SQLITE_FCNTL_HAS_MOVED]] ** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a ** pointer to an integer and it writes a boolean into that integer depending ** on whether or not the file has been renamed, moved, or deleted since it ** was first opened. ** **
  • [[SQLITE_FCNTL_WIN32_GET_HANDLE]] ** The [SQLITE_FCNTL_WIN32_GET_HANDLE] opcode can be used to obtain the ** underlying native file handle associated with a file handle. This file ** control interprets its argument as a pointer to a native file handle and ** writes the resulting value there. ** **
  • [[SQLITE_FCNTL_WIN32_SET_HANDLE]] ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This ** opcode causes the xFileControl method to swap the file handle with the one ** pointed to by the pArg argument. This capability is used during testing ** and only needs to be supported when SQLITE_TEST is defined. ** **
  • [[SQLITE_FCNTL_NULL_IO]] ** The [SQLITE_FCNTL_NULL_IO] opcode sets the low-level file descriptor ** or file handle for the [sqlite3_file] object such that it will no longer ** read or write to the database file. ** **
  • [[SQLITE_FCNTL_WAL_BLOCK]] ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might ** be advantageous to block on the next WAL lock if the lock is not immediately ** available. The WAL subsystem issues this signal during rare ** circumstances in order to fix a problem with priority inversion. ** Applications should not use this file-control. ** **
  • [[SQLITE_FCNTL_ZIPVFS]] ** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other ** VFS should return SQLITE_NOTFOUND for this opcode. ** **
  • [[SQLITE_FCNTL_RBU]] ** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by ** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for ** this opcode. ** **
  • [[SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]] ** If the [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] opcode returns SQLITE_OK, then ** the file descriptor is placed in "batch write mode", which ** means all subsequent write operations will be deferred and done ** atomically at the next [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]. Systems ** that do not support batch atomic writes will return SQLITE_NOTFOUND. ** ^Following a successful SQLITE_FCNTL_BEGIN_ATOMIC_WRITE and prior to ** the closing [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] or ** [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE], SQLite will make ** no VFS interface calls on the same [sqlite3_file] file descriptor ** except for calls to the xWrite method and the xFileControl method ** with [SQLITE_FCNTL_SIZE_HINT]. ** **
  • [[SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]] ** The [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] opcode causes all write ** operations since the previous successful call to ** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be performed atomically. ** This file control returns [SQLITE_OK] if and only if the writes were ** all performed successfully and have been committed to persistent storage. ** ^Regardless of whether or not it is successful, this file control takes ** the file descriptor out of batch write mode so that all subsequent ** write operations are independent. ** ^SQLite will never invoke SQLITE_FCNTL_COMMIT_ATOMIC_WRITE without ** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]. ** **
  • [[SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE]] ** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write ** operations since the previous successful call to ** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back. ** ^This file control takes the file descriptor out of batch write mode ** so that all subsequent write operations are independent. ** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without ** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]. ** **
  • [[SQLITE_FCNTL_LOCK_TIMEOUT]] ** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode is used to configure a VFS ** to block for up to M milliseconds before failing when attempting to ** obtain a file lock using the xLock or xShmLock methods of the VFS. ** The parameter is a pointer to a 32-bit signed integer that contains ** the value that M is to be set to. Before returning, the 32-bit signed ** integer is overwritten with the previous value of M. ** **
  • [[SQLITE_FCNTL_BLOCK_ON_CONNECT]] ** The [SQLITE_FCNTL_BLOCK_ON_CONNECT] opcode is used to configure the ** VFS to block when taking a SHARED lock to connect to a wal mode database. ** This is used to implement the functionality associated with ** SQLITE_SETLK_BLOCK_ON_CONNECT. ** **
  • [[SQLITE_FCNTL_DATA_VERSION]] ** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to ** a database file. The argument is a pointer to a 32-bit unsigned integer. ** The "data version" for the pager is written into the pointer. The ** "data version" changes whenever any change occurs to the corresponding ** database file, either through SQL statements on the same database ** connection or through transactions committed by separate database ** connections possibly in other processes. The [sqlite3_total_changes()] ** interface can be used to find if any database on the connection has changed, ** but that interface responds to changes on TEMP as well as MAIN and does ** not provide a mechanism to detect changes to MAIN only. Also, the ** [sqlite3_total_changes()] interface responds to internal changes only and ** omits changes made by other database connections. The ** [PRAGMA data_version] command provides a mechanism to detect changes to ** a single attached database that occur due to other database connections, ** but omits changes implemented by the database connection on which it is ** called. This file control is the only mechanism to detect changes that ** happen either internally or externally and that are associated with ** a particular attached database. ** **
  • [[SQLITE_FCNTL_CKPT_START]] ** The [SQLITE_FCNTL_CKPT_START] opcode is invoked from within a checkpoint ** in wal mode before the client starts to copy pages from the wal ** file to the database file. ** **
  • [[SQLITE_FCNTL_CKPT_DONE]] ** The [SQLITE_FCNTL_CKPT_DONE] opcode is invoked from within a checkpoint ** in wal mode after the client has finished copying pages from the wal ** file to the database file, but before the *-shm file is updated to ** record the fact that the pages have been checkpointed. ** **
  • [[SQLITE_FCNTL_EXTERNAL_READER]] ** The EXPERIMENTAL [SQLITE_FCNTL_EXTERNAL_READER] opcode is used to detect ** whether or not there is a database client in another process with a wal-mode ** transaction open on the database or not. It is only available on unix.The ** (void*) argument passed with this file-control should be a pointer to a ** value of type (int). The integer value is set to 1 if the database is a wal ** mode database and there exists at least one client in another process that ** currently has an SQL transaction open on the database. It is set to 0 if ** the database is not a wal-mode db, or if there is no such connection in any ** other process. This opcode cannot be used to detect transactions opened ** by clients within the current process, only within other processes. ** **
  • [[SQLITE_FCNTL_CKSM_FILE]] ** The [SQLITE_FCNTL_CKSM_FILE] opcode is for use internally by the ** [checksum VFS shim] only. ** **
  • [[SQLITE_FCNTL_RESET_CACHE]] ** If there is currently no transaction open on the database, and the ** database is not a temp db, then the [SQLITE_FCNTL_RESET_CACHE] file-control ** purges the contents of the in-memory page cache. If there is an open ** transaction, or if the db is a temp-db, this opcode is a no-op, not an error. **
*/ #define SQLITE_FCNTL_LOCKSTATE 1 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2 #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3 #define SQLITE_FCNTL_LAST_ERRNO 4 #define SQLITE_FCNTL_SIZE_HINT 5 #define SQLITE_FCNTL_CHUNK_SIZE 6 #define SQLITE_FCNTL_FILE_POINTER 7 #define SQLITE_FCNTL_SYNC_OMITTED 8 #define SQLITE_FCNTL_WIN32_AV_RETRY 9 #define SQLITE_FCNTL_PERSIST_WAL 10 #define SQLITE_FCNTL_OVERWRITE 11 #define SQLITE_FCNTL_VFSNAME 12 #define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13 #define SQLITE_FCNTL_PRAGMA 14 #define SQLITE_FCNTL_BUSYHANDLER 15 #define SQLITE_FCNTL_TEMPFILENAME 16 #define SQLITE_FCNTL_MMAP_SIZE 18 #define SQLITE_FCNTL_TRACE 19 #define SQLITE_FCNTL_HAS_MOVED 20 #define SQLITE_FCNTL_SYNC 21 #define SQLITE_FCNTL_COMMIT_PHASETWO 22 #define SQLITE_FCNTL_WIN32_SET_HANDLE 23 #define SQLITE_FCNTL_WAL_BLOCK 24 #define SQLITE_FCNTL_ZIPVFS 25 #define SQLITE_FCNTL_RBU 26 #define SQLITE_FCNTL_VFS_POINTER 27 #define SQLITE_FCNTL_JOURNAL_POINTER 28 #define SQLITE_FCNTL_WIN32_GET_HANDLE 29 #define SQLITE_FCNTL_PDB 30 #define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE 31 #define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE 32 #define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE 33 #define SQLITE_FCNTL_LOCK_TIMEOUT 34 #define SQLITE_FCNTL_DATA_VERSION 35 #define SQLITE_FCNTL_SIZE_LIMIT 36 #define SQLITE_FCNTL_CKPT_DONE 37 #define SQLITE_FCNTL_RESERVE_BYTES 38 #define SQLITE_FCNTL_CKPT_START 39 #define SQLITE_FCNTL_EXTERNAL_READER 40 #define SQLITE_FCNTL_CKSM_FILE 41 #define SQLITE_FCNTL_RESET_CACHE 42 #define SQLITE_FCNTL_NULL_IO 43 #define SQLITE_FCNTL_BLOCK_ON_CONNECT 44 /* deprecated names */ #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO /* ** CAPI3REF: Mutex Handle ** ** The mutex module within SQLite defines [sqlite3_mutex] to be an ** abstract type for a mutex object. The SQLite core never looks ** at the internal representation of an [sqlite3_mutex]. It only ** deals with pointers to the [sqlite3_mutex] object. ** ** Mutexes are created using [sqlite3_mutex_alloc()]. */ typedef struct sqlite3_mutex sqlite3_mutex; /* ** CAPI3REF: Loadable Extension Thunk ** ** A pointer to the opaque sqlite3_api_routines structure is passed as ** the third parameter to entry points of [loadable extensions]. This ** structure must be typedefed in order to work around compiler warnings ** on some platforms. */ typedef struct sqlite3_api_routines sqlite3_api_routines; /* ** CAPI3REF: File Name ** ** Type [sqlite3_filename] is used by SQLite to pass filenames to the ** xOpen method of a [VFS]. It may be cast to (const char*) and treated ** as a normal, nul-terminated, UTF-8 buffer containing the filename, but ** may also be passed to special APIs such as: ** **
    **
  • sqlite3_filename_database() **
  • sqlite3_filename_journal() **
  • sqlite3_filename_wal() **
  • sqlite3_uri_parameter() **
  • sqlite3_uri_boolean() **
  • sqlite3_uri_int64() **
  • sqlite3_uri_key() **
*/ typedef const char *sqlite3_filename; /* ** CAPI3REF: OS Interface Object ** ** An instance of the sqlite3_vfs object defines the interface between ** the SQLite core and the underlying operating system. The "vfs" ** in the name of the object stands for "virtual file system". See ** the [VFS | VFS documentation] for further information. ** ** The VFS interface is sometimes extended by adding new methods onto ** the end. Each time such an extension occurs, the iVersion field ** is incremented. The iVersion value started out as 1 in ** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2 ** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased ** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6]. Additional fields ** may be appended to the sqlite3_vfs object and the iVersion value ** may increase again in future versions of SQLite. ** Note that due to an oversight, the structure ** of the sqlite3_vfs object changed in the transition from ** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0] ** and yet the iVersion field was not increased. ** ** The szOsFile field is the size of the subclassed [sqlite3_file] ** structure used by this VFS. mxPathname is the maximum length of ** a pathname in this VFS. ** ** Registered sqlite3_vfs objects are kept on a linked list formed by ** the pNext pointer. The [sqlite3_vfs_register()] ** and [sqlite3_vfs_unregister()] interfaces manage this list ** in a thread-safe way. The [sqlite3_vfs_find()] interface ** searches the list. Neither the application code nor the VFS ** implementation should use the pNext pointer. ** ** The pNext field is the only field in the sqlite3_vfs ** structure that SQLite will ever modify. SQLite will only access ** or modify this field while holding a particular static mutex. ** The application should never modify anything within the sqlite3_vfs ** object once the object has been registered. ** ** The zName field holds the name of the VFS module. The name must ** be unique across all VFS modules. ** ** [[sqlite3_vfs.xOpen]] ** ^SQLite guarantees that the zFilename parameter to xOpen ** is either a NULL pointer or string obtained ** from xFullPathname() with an optional suffix added. ** ^If a suffix is added to the zFilename parameter, it will ** consist of a single "-" character followed by no more than ** 11 alphanumeric and/or "-" characters. ** ^SQLite further guarantees that ** the string will be valid and unchanged until xClose() is ** called. Because of the previous sentence, ** the [sqlite3_file] can safely store a pointer to the ** filename if it needs to remember the filename for some reason. ** If the zFilename parameter to xOpen is a NULL pointer then xOpen ** must invent its own temporary name for the file. ^Whenever the ** xFilename parameter is NULL it will also be the case that the ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE]. ** ** The flags argument to xOpen() includes all bits set in ** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] ** or [sqlite3_open16()] is used, then flags includes at least ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. ** If xOpen() opens a file read-only then it sets *pOutFlags to ** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set. ** ** ^(SQLite will also add one of the following flags to the xOpen() ** call, depending on the object being opened: ** **
    **
  • [SQLITE_OPEN_MAIN_DB] **
  • [SQLITE_OPEN_MAIN_JOURNAL] **
  • [SQLITE_OPEN_TEMP_DB] **
  • [SQLITE_OPEN_TEMP_JOURNAL] **
  • [SQLITE_OPEN_TRANSIENT_DB] **
  • [SQLITE_OPEN_SUBJOURNAL] **
  • [SQLITE_OPEN_SUPER_JOURNAL] **
  • [SQLITE_OPEN_WAL] **
)^ ** ** The file I/O implementation can use the object type flags to ** change the way it deals with files. For example, an application ** that does not care about crash recovery or rollback might make ** the open of a journal file a no-op. Writes to this journal would ** also be no-ops, and any attempt to read the journal would return ** SQLITE_IOERR. Or the implementation might recognize that a database ** file will be doing page-aligned sector reads and writes in a random ** order and set up its I/O subsystem accordingly. ** ** SQLite might also add one of the following flags to the xOpen method: ** **
    **
  • [SQLITE_OPEN_DELETEONCLOSE] **
  • [SQLITE_OPEN_EXCLUSIVE] **
** ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be ** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE] ** will be set for TEMP databases and their journals, transient ** databases, and subjournals. ** ** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction ** with the [SQLITE_OPEN_CREATE] flag, which are both directly ** analogous to the O_EXCL and O_CREAT flags of the POSIX open() ** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the ** SQLITE_OPEN_CREATE, is used to indicate that file should always ** be created, and that it is an error if it already exists. ** It is not used to indicate the file should be opened ** for exclusive access. ** ** ^At least szOsFile bytes of memory are allocated by SQLite ** to hold the [sqlite3_file] structure passed as the third ** argument to xOpen. The xOpen method does not have to ** allocate the structure; it should just fill it in. Note that ** the xOpen method must set the sqlite3_file.pMethods to either ** a valid [sqlite3_io_methods] object or to NULL. xOpen must do ** this even if the open fails. SQLite expects that the sqlite3_file.pMethods ** element will be valid after xOpen returns regardless of the success ** or failure of the xOpen call. ** ** [[sqlite3_vfs.xAccess]] ** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ] ** to test whether a file is at least readable. The SQLITE_ACCESS_READ ** flag is never actually used and is not implemented in the built-in ** VFSes of SQLite. The file is named by the second argument and can be a ** directory. The xAccess method returns [SQLITE_OK] on success or some ** non-zero error code if there is an I/O error or if the name of ** the file given in the second argument is illegal. If SQLITE_OK ** is returned, then non-zero or zero is written into *pResOut to indicate ** whether or not the file is accessible. ** ** ^SQLite will always allocate at least mxPathname+1 bytes for the ** output buffer xFullPathname. The exact size of the output buffer ** is also passed as a parameter to both methods. If the output buffer ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is ** handled as a fatal error by SQLite, vfs implementations should endeavor ** to prevent this by setting mxPathname to a sufficiently large value. ** ** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64() ** interfaces are not strictly a part of the filesystem, but they are ** included in the VFS structure for completeness. ** The xRandomness() function attempts to return nBytes bytes ** of good-quality randomness into zOut. The return value is ** the actual number of bytes of randomness obtained. ** The xSleep() method causes the calling thread to sleep for at ** least the number of microseconds given. ^The xCurrentTime() ** method returns a Julian Day Number for the current date and time as ** a floating point value. ** ^The xCurrentTimeInt64() method returns, as an integer, the Julian ** Day Number multiplied by 86400000 (the number of milliseconds in ** a 24-hour day). ** ^SQLite will use the xCurrentTimeInt64() method to get the current ** date and time if that method is available (if iVersion is 2 or ** greater and the function pointer is not NULL) and will fall back ** to xCurrentTime() if xCurrentTimeInt64() is unavailable. ** ** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces ** are not used by the SQLite core. These optional interfaces are provided ** by some VFSes to facilitate testing of the VFS code. By overriding ** system calls with functions under its control, a test program can ** simulate faults and error conditions that would otherwise be difficult ** or impossible to induce. The set of system calls that can be overridden ** varies from one VFS to another, and from one version of the same VFS to the ** next. Applications that use these interfaces must be prepared for any ** or all of these interfaces to be NULL or for their behavior to change ** from one release to the next. Applications must not attempt to access ** any of these methods if the iVersion of the VFS is less than 3. */ typedef struct sqlite3_vfs sqlite3_vfs; typedef void (*sqlite3_syscall_ptr)(void); struct sqlite3_vfs { int iVersion; /* Structure version number (currently 3) */ int szOsFile; /* Size of subclassed sqlite3_file */ int mxPathname; /* Maximum file pathname length */ sqlite3_vfs *pNext; /* Next registered VFS */ const char *zName; /* Name of this virtual file system */ void *pAppData; /* Pointer to application-specific data */ int (*xOpen)(sqlite3_vfs*, sqlite3_filename zName, sqlite3_file*, int flags, int *pOutFlags); int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir); int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut); int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut); void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename); void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg); void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void); void (*xDlClose)(sqlite3_vfs*, void*); int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut); int (*xSleep)(sqlite3_vfs*, int microseconds); int (*xCurrentTime)(sqlite3_vfs*, double*); int (*xGetLastError)(sqlite3_vfs*, int, char *); /* ** The methods above are in version 1 of the sqlite_vfs object ** definition. Those that follow are added in version 2 or later */ int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*); /* ** The methods above are in versions 1 and 2 of the sqlite_vfs object. ** Those below are for version 3 and greater. */ int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr); sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName); const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName); /* ** The methods above are in versions 1 through 3 of the sqlite_vfs object. ** New fields may be appended in future versions. The iVersion ** value will increment whenever this happens. */ }; /* ** CAPI3REF: Flags for the xAccess VFS method ** ** These integer constants can be used as the third parameter to ** the xAccess method of an [sqlite3_vfs] object. They determine ** what kind of permissions the xAccess method is looking for. ** With SQLITE_ACCESS_EXISTS, the xAccess method ** simply checks whether the file exists. ** With SQLITE_ACCESS_READWRITE, the xAccess method ** checks whether the named directory is both readable and writable ** (in other words, if files can be added, removed, and renamed within ** the directory). ** The SQLITE_ACCESS_READWRITE constant is currently used only by the ** [temp_store_directory pragma], though this could change in a future ** release of SQLite. ** With SQLITE_ACCESS_READ, the xAccess method ** checks whether the file is readable. The SQLITE_ACCESS_READ constant is ** currently unused, though it might be used in a future release of ** SQLite. */ #define SQLITE_ACCESS_EXISTS 0 #define SQLITE_ACCESS_READWRITE 1 /* Used by PRAGMA temp_store_directory */ #define SQLITE_ACCESS_READ 2 /* Unused */ /* ** CAPI3REF: Flags for the xShmLock VFS method ** ** These integer constants define the various locking operations ** allowed by the xShmLock method of [sqlite3_io_methods]. The ** following are the only legal combinations of flags to the ** xShmLock method: ** **
    **
  • SQLITE_SHM_LOCK | SQLITE_SHM_SHARED **
  • SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE **
  • SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED **
  • SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE **
** ** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as ** was given on the corresponding lock. ** ** The xShmLock method can transition between unlocked and SHARED or ** between unlocked and EXCLUSIVE. It cannot transition between SHARED ** and EXCLUSIVE. */ #define SQLITE_SHM_UNLOCK 1 #define SQLITE_SHM_LOCK 2 #define SQLITE_SHM_SHARED 4 #define SQLITE_SHM_EXCLUSIVE 8 /* ** CAPI3REF: Maximum xShmLock index ** ** The xShmLock method on [sqlite3_io_methods] may use values ** between 0 and this upper bound as its "offset" argument. ** The SQLite core will never attempt to acquire or release a ** lock outside of this range */ #define SQLITE_SHM_NLOCK 8 /* ** CAPI3REF: Initialize The SQLite Library ** ** ^The sqlite3_initialize() routine initializes the ** SQLite library. ^The sqlite3_shutdown() routine ** deallocates any resources that were allocated by sqlite3_initialize(). ** These routines are designed to aid in process initialization and ** shutdown on embedded systems. Workstation applications using ** SQLite normally do not need to invoke either of these routines. ** ** A call to sqlite3_initialize() is an "effective" call if it is ** the first time sqlite3_initialize() is invoked during the lifetime of ** the process, or if it is the first time sqlite3_initialize() is invoked ** following a call to sqlite3_shutdown(). ^(Only an effective call ** of sqlite3_initialize() does any initialization. All other calls ** are harmless no-ops.)^ ** ** A call to sqlite3_shutdown() is an "effective" call if it is the first ** call to sqlite3_shutdown() since the last sqlite3_initialize(). ^(Only ** an effective call to sqlite3_shutdown() does any deinitialization. ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^ ** ** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown() ** is not. The sqlite3_shutdown() interface must only be called from a ** single thread. All open [database connections] must be closed and all ** other SQLite resources must be deallocated prior to invoking ** sqlite3_shutdown(). ** ** Among other things, ^sqlite3_initialize() will invoke ** sqlite3_os_init(). Similarly, ^sqlite3_shutdown() ** will invoke sqlite3_os_end(). ** ** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success. ** ^If for some reason, sqlite3_initialize() is unable to initialize ** the library (perhaps it is unable to allocate a needed resource such ** as a mutex) it returns an [error code] other than [SQLITE_OK]. ** ** ^The sqlite3_initialize() routine is called internally by many other ** SQLite interfaces so that an application usually does not need to ** invoke sqlite3_initialize() directly. For example, [sqlite3_open()] ** calls sqlite3_initialize() so the SQLite library will be automatically ** initialized when [sqlite3_open()] is called if it has not be initialized ** already. ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT] ** compile-time option, then the automatic calls to sqlite3_initialize() ** are omitted and the application must call sqlite3_initialize() directly ** prior to using any other SQLite interface. For maximum portability, ** it is recommended that applications always invoke sqlite3_initialize() ** directly prior to using any other SQLite interface. Future releases ** of SQLite may require this. In other words, the behavior exhibited ** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the ** default behavior in some future release of SQLite. ** ** The sqlite3_os_init() routine does operating-system specific ** initialization of the SQLite library. The sqlite3_os_end() ** routine undoes the effect of sqlite3_os_init(). Typical tasks ** performed by these routines include allocation or deallocation ** of static resources, initialization of global variables, ** setting up a default [sqlite3_vfs] module, or setting up ** a default configuration using [sqlite3_config()]. ** ** The application should never invoke either sqlite3_os_init() ** or sqlite3_os_end() directly. The application should only invoke ** sqlite3_initialize() and sqlite3_shutdown(). The sqlite3_os_init() ** interface is called automatically by sqlite3_initialize() and ** sqlite3_os_end() is called by sqlite3_shutdown(). Appropriate ** implementations for sqlite3_os_init() and sqlite3_os_end() ** are built into SQLite when it is compiled for Unix, Windows, or OS/2. ** When [custom builds | built for other platforms] ** (using the [SQLITE_OS_OTHER=1] compile-time ** option) the application must supply a suitable implementation for ** sqlite3_os_init() and sqlite3_os_end(). An application-supplied ** implementation of sqlite3_os_init() or sqlite3_os_end() ** must return [SQLITE_OK] on success and some other [error code] upon ** failure. */ SQLITE_API int sqlite3_initialize(void); SQLITE_API int sqlite3_shutdown(void); SQLITE_API int sqlite3_os_init(void); SQLITE_API int sqlite3_os_end(void); /* ** CAPI3REF: Configuring The SQLite Library ** ** The sqlite3_config() interface is used to make global configuration ** changes to SQLite in order to tune SQLite to the specific needs of ** the application. The default configuration is recommended for most ** applications and so this routine is usually not necessary. It is ** provided to support rare applications with unusual needs. ** ** The sqlite3_config() interface is not threadsafe. The application ** must ensure that no other SQLite interfaces are invoked by other ** threads while sqlite3_config() is running. ** ** The first argument to sqlite3_config() is an integer ** [configuration option] that determines ** what property of SQLite is to be configured. Subsequent arguments ** vary depending on the [configuration option] ** in the first argument. ** ** For most configuration options, the sqlite3_config() interface ** may only be invoked prior to library initialization using ** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()]. ** The exceptional configuration options that may be invoked at any time ** are called "anytime configuration options". ** ^If sqlite3_config() is called after [sqlite3_initialize()] and before ** [sqlite3_shutdown()] with a first argument that is not an anytime ** configuration option, then the sqlite3_config() call will return SQLITE_MISUSE. ** Note, however, that ^sqlite3_config() can be called as part of the ** implementation of an application-defined [sqlite3_os_init()]. ** ** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK]. ** ^If the option is unknown or SQLite is unable to set the option ** then this routine returns a non-zero [error code]. */ SQLITE_API int sqlite3_config(int, ...); /* ** CAPI3REF: Configure database connections ** METHOD: sqlite3 ** ** The sqlite3_db_config() interface is used to make configuration ** changes to a [database connection]. The interface is similar to ** [sqlite3_config()] except that the changes apply to a single ** [database connection] (specified in the first argument). ** ** The second argument to sqlite3_db_config(D,V,...) is the ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code ** that indicates what aspect of the [database connection] is being configured. ** Subsequent arguments vary depending on the configuration verb. ** ** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if ** the call is considered successful. */ SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...); /* ** CAPI3REF: Memory Allocation Routines ** ** An instance of this object defines the interface between SQLite ** and low-level memory allocation routines. ** ** This object is used in only one place in the SQLite interface. ** A pointer to an instance of this object is the argument to ** [sqlite3_config()] when the configuration option is ** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC]. ** By creating an instance of this object ** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC]) ** during configuration, an application can specify an alternative ** memory allocation subsystem for SQLite to use for all of its ** dynamic memory needs. ** ** Note that SQLite comes with several [built-in memory allocators] ** that are perfectly adequate for the overwhelming majority of applications ** and that this object is only useful to a tiny minority of applications ** with specialized memory allocation requirements. This object is ** also used during testing of SQLite in order to specify an alternative ** memory allocator that simulates memory out-of-memory conditions in ** order to verify that SQLite recovers gracefully from such ** conditions. ** ** The xMalloc, xRealloc, and xFree methods must work like the ** malloc(), realloc() and free() functions from the standard C library. ** ^SQLite guarantees that the second argument to ** xRealloc is always a value returned by a prior call to xRoundup. ** ** xSize should return the allocated size of a memory allocation ** previously obtained from xMalloc or xRealloc. The allocated size ** is always at least as big as the requested size but may be larger. ** ** The xRoundup method returns what would be the allocated size of ** a memory allocation given a particular requested size. Most memory ** allocators round up memory allocations at least to the next multiple ** of 8. Some allocators round up to a larger multiple or to a power of 2. ** Every memory allocation request coming in through [sqlite3_malloc()] ** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0, ** that causes the corresponding memory allocation to fail. ** ** The xInit method initializes the memory allocator. For example, ** it might allocate any required mutexes or initialize internal data ** structures. The xShutdown method is invoked (indirectly) by ** [sqlite3_shutdown()] and should deallocate any resources acquired ** by xInit. The pAppData pointer is used as the only parameter to ** xInit and xShutdown. ** ** SQLite holds the [SQLITE_MUTEX_STATIC_MAIN] mutex when it invokes ** the xInit method, so the xInit method need not be threadsafe. The ** xShutdown method is only called from [sqlite3_shutdown()] so it does ** not need to be threadsafe either. For all other methods, SQLite ** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the ** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which ** it is by default) and so the methods are automatically serialized. ** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other ** methods must be threadsafe or else make their own arrangements for ** serialization. ** ** SQLite will never invoke xInit() more than once without an intervening ** call to xShutdown(). */ typedef struct sqlite3_mem_methods sqlite3_mem_methods; struct sqlite3_mem_methods { void *(*xMalloc)(int); /* Memory allocation function */ void (*xFree)(void*); /* Free a prior allocation */ void *(*xRealloc)(void*,int); /* Resize an allocation */ int (*xSize)(void*); /* Return the size of an allocation */ int (*xRoundup)(int); /* Round up request size to allocation size */ int (*xInit)(void*); /* Initialize the memory allocator */ void (*xShutdown)(void*); /* Deinitialize the memory allocator */ void *pAppData; /* Argument to xInit() and xShutdown() */ }; /* ** CAPI3REF: Configuration Options ** KEYWORDS: {configuration option} ** ** These constants are the available integer configuration options that ** can be passed as the first argument to the [sqlite3_config()] interface. ** ** Most of the configuration options for sqlite3_config() ** will only work if invoked prior to [sqlite3_initialize()] or after ** [sqlite3_shutdown()]. The few exceptions to this rule are called ** "anytime configuration options". ** ^Calling [sqlite3_config()] with a first argument that is not an ** anytime configuration option in between calls to [sqlite3_initialize()] and ** [sqlite3_shutdown()] is a no-op that returns SQLITE_MISUSE. ** ** The set of anytime configuration options can change (by insertions ** and/or deletions) from one release of SQLite to the next. ** As of SQLite version 3.42.0, the complete set of anytime configuration ** options is: **
    **
  • SQLITE_CONFIG_LOG **
  • SQLITE_CONFIG_PCACHE_HDRSZ **
** ** New configuration options may be added in future releases of SQLite. ** Existing configuration options might be discontinued. Applications ** should check the return code from [sqlite3_config()] to make sure that ** the call worked. The [sqlite3_config()] interface will return a ** non-zero [error code] if a discontinued or unsupported configuration option ** is invoked. ** **
** [[SQLITE_CONFIG_SINGLETHREAD]]
SQLITE_CONFIG_SINGLETHREAD
**
There are no arguments to this option. ^This option sets the ** [threading mode] to Single-thread. In other words, it disables ** all mutexing and puts SQLite into a mode where it can only be used ** by a single thread. ^If SQLite is compiled with ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then ** it is not possible to change the [threading mode] from its default ** value of Single-thread and so [sqlite3_config()] will return ** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD ** configuration option.
** ** [[SQLITE_CONFIG_MULTITHREAD]]
SQLITE_CONFIG_MULTITHREAD
**
There are no arguments to this option. ^This option sets the ** [threading mode] to Multi-thread. In other words, it disables ** mutexing on [database connection] and [prepared statement] objects. ** The application is responsible for serializing access to ** [database connections] and [prepared statements]. But other mutexes ** are enabled so that SQLite will be safe to use in a multi-threaded ** environment as long as no two threads attempt to use the same ** [database connection] at the same time. ^If SQLite is compiled with ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then ** it is not possible to set the Multi-thread [threading mode] and ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the ** SQLITE_CONFIG_MULTITHREAD configuration option.
** ** [[SQLITE_CONFIG_SERIALIZED]]
SQLITE_CONFIG_SERIALIZED
**
There are no arguments to this option. ^This option sets the ** [threading mode] to Serialized. In other words, this option enables ** all mutexes including the recursive ** mutexes on [database connection] and [prepared statement] objects. ** In this mode (which is the default when SQLite is compiled with ** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access ** to [database connections] and [prepared statements] so that the ** application is free to use the same [database connection] or the ** same [prepared statement] in different threads at the same time. ** ^If SQLite is compiled with ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then ** it is not possible to set the Serialized [threading mode] and ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the ** SQLITE_CONFIG_SERIALIZED configuration option.
** ** [[SQLITE_CONFIG_MALLOC]]
SQLITE_CONFIG_MALLOC
**
^(The SQLITE_CONFIG_MALLOC option takes a single argument which is ** a pointer to an instance of the [sqlite3_mem_methods] structure. ** The argument specifies ** alternative low-level memory allocation routines to be used in place of ** the memory allocation routines built into SQLite.)^ ^SQLite makes ** its own private copy of the content of the [sqlite3_mem_methods] structure ** before the [sqlite3_config()] call returns.
** ** [[SQLITE_CONFIG_GETMALLOC]]
SQLITE_CONFIG_GETMALLOC
**
^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which ** is a pointer to an instance of the [sqlite3_mem_methods] structure. ** The [sqlite3_mem_methods] ** structure is filled with the currently defined memory allocation routines.)^ ** This option can be used to overload the default memory allocation ** routines with a wrapper that simulations memory allocation failure or ** tracks memory usage, for example.
** ** [[SQLITE_CONFIG_SMALL_MALLOC]]
SQLITE_CONFIG_SMALL_MALLOC
**
^The SQLITE_CONFIG_SMALL_MALLOC option takes single argument of ** type int, interpreted as a boolean, which if true provides a hint to ** SQLite that it should avoid large memory allocations if possible. ** SQLite will run faster if it is free to make large memory allocations, ** but some application might prefer to run slower in exchange for ** guarantees about memory fragmentation that are possible if large ** allocations are avoided. This hint is normally off. **
** ** [[SQLITE_CONFIG_MEMSTATUS]]
SQLITE_CONFIG_MEMSTATUS
**
^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int, ** interpreted as a boolean, which enables or disables the collection of ** memory allocation statistics. ^(When memory allocation statistics are ** disabled, the following SQLite interfaces become non-operational: **
    **
  • [sqlite3_hard_heap_limit64()] **
  • [sqlite3_memory_used()] **
  • [sqlite3_memory_highwater()] **
  • [sqlite3_soft_heap_limit64()] **
  • [sqlite3_status64()] **
)^ ** ^Memory allocation statistics are enabled by default unless SQLite is ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory ** allocation statistics are disabled by default. **
** ** [[SQLITE_CONFIG_SCRATCH]]
SQLITE_CONFIG_SCRATCH
**
The SQLITE_CONFIG_SCRATCH option is no longer used. **
** ** [[SQLITE_CONFIG_PAGECACHE]]
SQLITE_CONFIG_PAGECACHE
**
^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool ** that SQLite can use for the database page cache with the default page ** cache implementation. ** This configuration option is a no-op if an application-defined page ** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2]. ** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to ** 8-byte aligned memory (pMem), the size of each page cache line (sz), ** and the number of cache lines (N). ** The sz argument should be the size of the largest database page ** (a power of two between 512 and 65536) plus some extra bytes for each ** page header. ^The number of extra bytes needed by the page header ** can be determined using [SQLITE_CONFIG_PCACHE_HDRSZ]. ** ^It is harmless, apart from the wasted memory, ** for the sz parameter to be larger than necessary. The pMem ** argument must be either a NULL pointer or a pointer to an 8-byte ** aligned block of memory of at least sz*N bytes, otherwise ** subsequent behavior is undefined. ** ^When pMem is not NULL, SQLite will strive to use the memory provided ** to satisfy page cache needs, falling back to [sqlite3_malloc()] if ** a page cache line is larger than sz bytes or if all of the pMem buffer ** is exhausted. ** ^If pMem is NULL and N is non-zero, then each database connection ** does an initial bulk allocation for page cache memory ** from [sqlite3_malloc()] sufficient for N cache lines if N is positive or ** of -1024*N bytes if N is negative, . ^If additional ** page cache memory is needed beyond what is provided by the initial ** allocation, then SQLite goes to [sqlite3_malloc()] separately for each ** additional cache line.
** ** [[SQLITE_CONFIG_HEAP]]
SQLITE_CONFIG_HEAP
**
^The SQLITE_CONFIG_HEAP option specifies a static memory buffer ** that SQLite will use for all of its dynamic memory allocation needs ** beyond those provided for by [SQLITE_CONFIG_PAGECACHE]. ** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled ** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns ** [SQLITE_ERROR] if invoked otherwise. ** ^There are three arguments to SQLITE_CONFIG_HEAP: ** An 8-byte aligned pointer to the memory, ** the number of bytes in the memory buffer, and the minimum allocation size. ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts ** to using its default memory allocator (the system malloc() implementation), ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the ** memory pointer is not NULL then the alternative memory ** allocator is engaged to handle all of SQLites memory allocation needs. ** The first pointer (the memory pointer) must be aligned to an 8-byte ** boundary or subsequent behavior of SQLite will be undefined. ** The minimum allocation size is capped at 2**12. Reasonable values ** for the minimum allocation size are 2**5 through 2**8.
** ** [[SQLITE_CONFIG_MUTEX]]
SQLITE_CONFIG_MUTEX
**
^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a ** pointer to an instance of the [sqlite3_mutex_methods] structure. ** The argument specifies alternative low-level mutex routines to be used ** in place the mutex routines built into SQLite.)^ ^SQLite makes a copy of ** the content of the [sqlite3_mutex_methods] structure before the call to ** [sqlite3_config()] returns. ^If SQLite is compiled with ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then ** the entire mutexing subsystem is omitted from the build and hence calls to ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will ** return [SQLITE_ERROR].
** ** [[SQLITE_CONFIG_GETMUTEX]]
SQLITE_CONFIG_GETMUTEX
**
^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which ** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The ** [sqlite3_mutex_methods] ** structure is filled with the currently defined mutex routines.)^ ** This option can be used to overload the default mutex allocation ** routines with a wrapper used to track mutex usage for performance ** profiling or testing, for example. ^If SQLite is compiled with ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then ** the entire mutexing subsystem is omitted from the build and hence calls to ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will ** return [SQLITE_ERROR].
** ** [[SQLITE_CONFIG_LOOKASIDE]]
SQLITE_CONFIG_LOOKASIDE
**
^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine ** the default size of [lookaside memory] on each [database connection]. ** The first argument is the ** size of each lookaside buffer slot ("sz") and the second is the number of ** slots allocated to each database connection ("cnt").)^ ** ^(SQLITE_CONFIG_LOOKASIDE sets the default lookaside size. ** The [SQLITE_DBCONFIG_LOOKASIDE] option to [sqlite3_db_config()] can ** be used to change the lookaside configuration on individual connections.)^ ** The [-DSQLITE_DEFAULT_LOOKASIDE] option can be used to change the ** default lookaside configuration at compile-time. **
** ** [[SQLITE_CONFIG_PCACHE2]]
SQLITE_CONFIG_PCACHE2
**
^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is ** a pointer to an [sqlite3_pcache_methods2] object. This object specifies ** the interface to a custom page cache implementation.)^ ** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.
** ** [[SQLITE_CONFIG_GETPCACHE2]]
SQLITE_CONFIG_GETPCACHE2
**
^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which ** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of ** the current page cache implementation into that object.)^
** ** [[SQLITE_CONFIG_LOG]]
SQLITE_CONFIG_LOG
**
The SQLITE_CONFIG_LOG option is used to configure the SQLite ** global [error log]. ** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a ** function with a call signature of void(*)(void*,int,const char*), ** and a pointer to void. ^If the function pointer is not NULL, it is ** invoked by [sqlite3_log()] to process each logging event. ^If the ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op. ** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is ** passed through as the first parameter to the application-defined logger ** function whenever that function is invoked. ^The second parameter to ** the logger function is a copy of the first parameter to the corresponding ** [sqlite3_log()] call and is intended to be a [result code] or an ** [extended result code]. ^The third parameter passed to the logger is ** log message after formatting via [sqlite3_snprintf()]. ** The SQLite logging interface is not reentrant; the logger function ** supplied by the application must not invoke any SQLite interface. ** In a multi-threaded application, the application-defined logger ** function must be threadsafe.
** ** [[SQLITE_CONFIG_URI]]
SQLITE_CONFIG_URI **
^(The SQLITE_CONFIG_URI option takes a single argument of type int. ** If non-zero, then URI handling is globally enabled. If the parameter is zero, ** then URI handling is globally disabled.)^ ^If URI handling is globally ** enabled, all filenames passed to [sqlite3_open()], [sqlite3_open_v2()], ** [sqlite3_open16()] or ** specified as part of [ATTACH] commands are interpreted as URIs, regardless ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database ** connection is opened. ^If it is globally disabled, filenames are ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the ** database connection is opened. ^(By default, URI handling is globally ** disabled. The default value may be changed by compiling with the ** [SQLITE_USE_URI] symbol defined.)^ ** ** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]]
SQLITE_CONFIG_COVERING_INDEX_SCAN **
^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer ** argument which is interpreted as a boolean in order to enable or disable ** the use of covering indices for full table scans in the query optimizer. ** ^The default setting is determined ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on" ** if that compile-time option is omitted. ** The ability to disable the use of covering indices for full table scans ** is because some incorrectly coded legacy applications might malfunction ** when the optimization is enabled. Providing the ability to ** disable the optimization allows the older, buggy application code to work ** without change even with newer versions of SQLite. ** ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]] **
SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE **
These options are obsolete and should not be used by new code. ** They are retained for backwards compatibility but are now no-ops. **
** ** [[SQLITE_CONFIG_SQLLOG]] **
SQLITE_CONFIG_SQLLOG **
This option is only available if sqlite is compiled with the ** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should ** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int). ** The second should be of type (void*). The callback is invoked by the library ** in three separate circumstances, identified by the value passed as the ** fourth parameter. If the fourth parameter is 0, then the database connection ** passed as the second argument has just been opened. The third argument ** points to a buffer containing the name of the main database file. If the ** fourth parameter is 1, then the SQL statement that the third parameter ** points to has just been executed. Or, if the fourth parameter is 2, then ** the connection being passed as the second parameter is being closed. The ** third parameter is passed NULL In this case. An example of using this ** configuration option can be seen in the "test_sqllog.c" source file in ** the canonical SQLite source tree.
** ** [[SQLITE_CONFIG_MMAP_SIZE]] **
SQLITE_CONFIG_MMAP_SIZE **
^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values ** that are the default mmap size limit (the default setting for ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit. ** ^The default setting can be overridden by each database connection using ** either the [PRAGMA mmap_size] command, or by using the ** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size ** will be silently truncated if necessary so that it does not exceed the ** compile-time maximum mmap size set by the ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^ ** ^If either argument to this option is negative, then that argument is ** changed to its compile-time default. ** ** [[SQLITE_CONFIG_WIN32_HEAPSIZE]] **
SQLITE_CONFIG_WIN32_HEAPSIZE **
^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is ** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro ** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value ** that specifies the maximum size of the created heap. ** ** [[SQLITE_CONFIG_PCACHE_HDRSZ]] **
SQLITE_CONFIG_PCACHE_HDRSZ **
^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which ** is a pointer to an integer and writes into that integer the number of extra ** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE]. ** The amount of extra space required can change depending on the compiler, ** target platform, and SQLite version. ** ** [[SQLITE_CONFIG_PMASZ]] **
SQLITE_CONFIG_PMASZ **
^The SQLITE_CONFIG_PMASZ option takes a single parameter which ** is an unsigned integer and sets the "Minimum PMA Size" for the multithreaded ** sorter to that integer. The default minimum PMA Size is set by the ** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched ** to help with sort operations when multithreaded sorting ** is enabled (using the [PRAGMA threads] command) and the amount of content ** to be sorted exceeds the page size times the minimum of the ** [PRAGMA cache_size] setting and this value. ** ** [[SQLITE_CONFIG_STMTJRNL_SPILL]] **
SQLITE_CONFIG_STMTJRNL_SPILL **
^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which ** becomes the [statement journal] spill-to-disk threshold. ** [Statement journals] are held in memory until their size (in bytes) ** exceeds this threshold, at which point they are written to disk. ** Or if the threshold is -1, statement journals are always held ** exclusively in memory. ** Since many statement journals never become large, setting the spill ** threshold to a value such as 64KiB can greatly reduce the amount of ** I/O required to support statement rollback. ** The default value for this setting is controlled by the ** [SQLITE_STMTJRNL_SPILL] compile-time option. ** ** [[SQLITE_CONFIG_SORTERREF_SIZE]] **
SQLITE_CONFIG_SORTERREF_SIZE **
The SQLITE_CONFIG_SORTERREF_SIZE option accepts a single parameter ** of type (int) - the new value of the sorter-reference size threshold. ** Usually, when SQLite uses an external sort to order records according ** to an ORDER BY clause, all fields required by the caller are present in the ** sorted records. However, if SQLite determines based on the declared type ** of a table column that its values are likely to be very large - larger ** than the configured sorter-reference size threshold - then a reference ** is stored in each sorted record and the required column values loaded ** from the database as records are returned in sorted order. The default ** value for this option is to never use this optimization. Specifying a ** negative value for this option restores the default behavior. ** This option is only available if SQLite is compiled with the ** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option. ** ** [[SQLITE_CONFIG_MEMDB_MAXSIZE]] **
SQLITE_CONFIG_MEMDB_MAXSIZE **
The SQLITE_CONFIG_MEMDB_MAXSIZE option accepts a single parameter ** [sqlite3_int64] parameter which is the default maximum size for an in-memory ** database created using [sqlite3_deserialize()]. This default maximum ** size can be adjusted up or down for individual databases using the ** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this ** configuration setting is never used, then the default maximum is determined ** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that ** compile-time option is not set, then the default maximum is 1073741824. ** ** [[SQLITE_CONFIG_ROWID_IN_VIEW]] **
SQLITE_CONFIG_ROWID_IN_VIEW **
The SQLITE_CONFIG_ROWID_IN_VIEW option enables or disables the ability ** for VIEWs to have a ROWID. The capability can only be enabled if SQLite is ** compiled with -DSQLITE_ALLOW_ROWID_IN_VIEW, in which case the capability ** defaults to on. This configuration option queries the current setting or ** changes the setting to off or on. The argument is a pointer to an integer. ** If that integer initially holds a value of 1, then the ability for VIEWs to ** have ROWIDs is activated. If the integer initially holds zero, then the ** ability is deactivated. Any other initial value for the integer leaves the ** setting unchanged. After changes, if any, the integer is written with ** a 1 or 0, if the ability for VIEWs to have ROWIDs is on or off. If SQLite ** is compiled without -DSQLITE_ALLOW_ROWID_IN_VIEW (which is the usual and ** recommended case) then the integer is always filled with zero, regardless ** if its initial value. **
*/ #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ #define SQLITE_CONFIG_SERIALIZED 3 /* nil */ #define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */ #define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */ #define SQLITE_CONFIG_SCRATCH 6 /* No longer used */ #define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */ #define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */ #define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */ #define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */ #define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */ /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ #define SQLITE_CONFIG_LOOKASIDE 13 /* int int */ #define SQLITE_CONFIG_PCACHE 14 /* no-op */ #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */ #define SQLITE_CONFIG_LOG 16 /* xFunc, void* */ #define SQLITE_CONFIG_URI 17 /* int */ #define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */ #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */ #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */ #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */ #define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */ #define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */ #define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */ #define SQLITE_CONFIG_PMASZ 25 /* unsigned int szPma */ #define SQLITE_CONFIG_STMTJRNL_SPILL 26 /* int nByte */ #define SQLITE_CONFIG_SMALL_MALLOC 27 /* boolean */ #define SQLITE_CONFIG_SORTERREF_SIZE 28 /* int nByte */ #define SQLITE_CONFIG_MEMDB_MAXSIZE 29 /* sqlite3_int64 */ #define SQLITE_CONFIG_ROWID_IN_VIEW 30 /* int* */ /* ** CAPI3REF: Database Connection Configuration Options ** ** These constants are the available integer configuration options that ** can be passed as the second parameter to the [sqlite3_db_config()] interface. ** ** The [sqlite3_db_config()] interface is a var-args functions. It takes a ** variable number of parameters, though always at least two. The number of ** parameters passed into sqlite3_db_config() depends on which of these ** constants is given as the second parameter. This documentation page ** refers to parameters beyond the second as "arguments". Thus, when this ** page says "the N-th argument" it means "the N-th parameter past the ** configuration option" or "the (N+2)-th parameter to sqlite3_db_config()". ** ** New configuration options may be added in future releases of SQLite. ** Existing configuration options might be discontinued. Applications ** should check the return code from [sqlite3_db_config()] to make sure that ** the call worked. ^The [sqlite3_db_config()] interface will return a ** non-zero [error code] if a discontinued or unsupported configuration option ** is invoked. ** **
** [[SQLITE_DBCONFIG_LOOKASIDE]] **
SQLITE_DBCONFIG_LOOKASIDE
**
The SQLITE_DBCONFIG_LOOKASIDE option is used to adjust the ** configuration of the [lookaside memory allocator] within a database ** connection. ** The arguments to the SQLITE_DBCONFIG_LOOKASIDE option are not ** in the [DBCONFIG arguments|usual format]. ** The SQLITE_DBCONFIG_LOOKASIDE option takes three arguments, not two, ** so that a call to [sqlite3_db_config()] that uses SQLITE_DBCONFIG_LOOKASIDE ** should have a total of five parameters. **
    **
  1. The first argument ("buf") is a ** pointer to a memory buffer to use for lookaside memory. ** The first argument may be NULL in which case SQLite will allocate the ** lookaside buffer itself using [sqlite3_malloc()]. **

  2. The second argument ("sz") is the ** size of each lookaside buffer slot. Lookaside is disabled if "sz" ** is less than 8. The "sz" argument should be a multiple of 8 less than ** 65536. If "sz" does not meet this constraint, it is reduced in size until ** it does. **

  3. The third argument ("cnt") is the number of slots. Lookaside is disabled ** if "cnt"is less than 1. The "cnt" value will be reduced, if necessary, so ** that the product of "sz" and "cnt" does not exceed 2,147,418,112. The "cnt" ** parameter is usually chosen so that the product of "sz" and "cnt" is less ** than 1,000,000. **

**

If the "buf" argument is not NULL, then it must ** point to a memory buffer with a size that is greater than ** or equal to the product of "sz" and "cnt". ** The buffer must be aligned to an 8-byte boundary. ** The lookaside memory ** configuration for a database connection can only be changed when that ** connection is not currently using lookaside memory, or in other words ** when the value returned by [SQLITE_DBSTATUS_LOOKASIDE_USED] is zero. ** Any attempt to change the lookaside memory configuration when lookaside ** memory is in use leaves the configuration unchanged and returns ** [SQLITE_BUSY]. ** If the "buf" argument is NULL and an attempt ** to allocate memory based on "sz" and "cnt" fails, then ** lookaside is silently disabled. **

** The [SQLITE_CONFIG_LOOKASIDE] configuration option can be used to set the ** default lookaside configuration at initialization. The ** [-DSQLITE_DEFAULT_LOOKASIDE] option can be used to set the default lookaside ** configuration at compile-time. Typical values for lookaside are 1200 for ** "sz" and 40 to 100 for "cnt". **

** ** [[SQLITE_DBCONFIG_ENABLE_FKEY]] **
SQLITE_DBCONFIG_ENABLE_FKEY
**
^This option is used to enable or disable the enforcement of ** [foreign key constraints]. This is the same setting that is ** enabled or disabled by the [PRAGMA foreign_keys] statement. ** The first argument is an integer which is 0 to disable FK enforcement, ** positive to enable FK enforcement or negative to leave FK enforcement ** unchanged. The second parameter is a pointer to an integer into which ** is written 0 or 1 to indicate whether FK enforcement is off or on ** following this call. The second parameter may be a NULL pointer, in ** which case the FK enforcement setting is not reported back.
** ** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]] **
SQLITE_DBCONFIG_ENABLE_TRIGGER
**
^This option is used to enable or disable [CREATE TRIGGER | triggers]. ** There should be two additional arguments. ** The first argument is an integer which is 0 to disable triggers, ** positive to enable triggers or negative to leave the setting unchanged. ** The second parameter is a pointer to an integer into which ** is written 0 or 1 to indicate whether triggers are disabled or enabled ** following this call. The second parameter may be a NULL pointer, in ** which case the trigger setting is not reported back. ** **

Originally this option disabled all triggers. ^(However, since ** SQLite version 3.35.0, TEMP triggers are still allowed even if ** this option is off. So, in other words, this option now only disables ** triggers in the main database schema or in the schemas of [ATTACH]-ed ** databases.)^

** ** [[SQLITE_DBCONFIG_ENABLE_VIEW]] **
SQLITE_DBCONFIG_ENABLE_VIEW
**
^This option is used to enable or disable [CREATE VIEW | views]. ** There must be two additional arguments. ** The first argument is an integer which is 0 to disable views, ** positive to enable views or negative to leave the setting unchanged. ** The second parameter is a pointer to an integer into which ** is written 0 or 1 to indicate whether views are disabled or enabled ** following this call. The second parameter may be a NULL pointer, in ** which case the view setting is not reported back. ** **

Originally this option disabled all views. ^(However, since ** SQLite version 3.35.0, TEMP views are still allowed even if ** this option is off. So, in other words, this option now only disables ** views in the main database schema or in the schemas of ATTACH-ed ** databases.)^

** ** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]] **
SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER
**
^This option is used to enable or disable the ** [fts3_tokenizer()] function which is part of the ** [FTS3] full-text search engine extension. ** There must be two additional arguments. ** The first argument is an integer which is 0 to disable fts3_tokenizer() or ** positive to enable fts3_tokenizer() or negative to leave the setting ** unchanged. ** The second parameter is a pointer to an integer into which ** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled ** following this call. The second parameter may be a NULL pointer, in ** which case the new setting is not reported back.
** ** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]] **
SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION
**
^This option is used to enable or disable the [sqlite3_load_extension()] ** interface independently of the [load_extension()] SQL function. ** The [sqlite3_enable_load_extension()] API enables or disables both the ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()]. ** There must be two additional arguments. ** When the first argument to this interface is 1, then only the C-API is ** enabled and the SQL function remains disabled. If the first argument to ** this interface is 0, then both the C-API and the SQL function are disabled. ** If the first argument is -1, then no changes are made to state of either the ** C-API or the SQL function. ** The second parameter is a pointer to an integer into which ** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface ** is disabled or enabled following this call. The second parameter may ** be a NULL pointer, in which case the new setting is not reported back. **
** ** [[SQLITE_DBCONFIG_MAINDBNAME]]
SQLITE_DBCONFIG_MAINDBNAME
**
^This option is used to change the name of the "main" database ** schema. This option does not follow the ** [DBCONFIG arguments|usual SQLITE_DBCONFIG argument format]. ** This option takes exactly one additional argument so that the ** [sqlite3_db_config()] call has a total of three parameters. The ** extra argument must be a pointer to a constant UTF8 string which ** will become the new schema name in place of "main". ^SQLite does ** not make a copy of the new main schema name string, so the application ** must ensure that the argument passed into SQLITE_DBCONFIG MAINDBNAME ** is unchanged until after the database connection closes. **
** ** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]] **
SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE
**
Usually, when a database in [WAL mode] is closed or detached from a ** database handle, SQLite checks if if there are other connections to the ** same database, and if there are no other database connection (if the ** connection being closed is the last open connection to the database), ** then SQLite performs a [checkpoint] before closing the connection and ** deletes the WAL file. The SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE option can ** be used to override that behavior. The first argument passed to this ** operation (the third parameter to [sqlite3_db_config()]) is an integer ** which is positive to disable checkpoints-on-close, or zero (the default) ** to enable them, and negative to leave the setting unchanged. ** The second argument (the fourth parameter) is a pointer to an integer ** into which is written 0 or 1 to indicate whether checkpoints-on-close ** have been disabled - 0 if they are not disabled, 1 if they are. **
** ** [[SQLITE_DBCONFIG_ENABLE_QPSG]]
SQLITE_DBCONFIG_ENABLE_QPSG
**
^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates ** the [query planner stability guarantee] (QPSG). When the QPSG is active, ** a single SQL query statement will always use the same algorithm regardless ** of values of [bound parameters].)^ The QPSG disables some query optimizations ** that look at the values of bound parameters, which can make some queries ** slower. But the QPSG has the advantage of more predictable behavior. With ** the QPSG active, SQLite will always use the same query plan in the field as ** was used during testing in the lab. ** The first argument to this setting is an integer which is 0 to disable ** the QPSG, positive to enable QPSG, or negative to leave the setting ** unchanged. The second parameter is a pointer to an integer into which ** is written 0 or 1 to indicate whether the QPSG is disabled or enabled ** following this call. **
** ** [[SQLITE_DBCONFIG_TRIGGER_EQP]]
SQLITE_DBCONFIG_TRIGGER_EQP
**
By default, the output of EXPLAIN QUERY PLAN commands does not ** include output for any operations performed by trigger programs. This ** option is used to set or clear (the default) a flag that governs this ** behavior. The first parameter passed to this operation is an integer - ** positive to enable output for trigger programs, or zero to disable it, ** or negative to leave the setting unchanged. ** The second parameter is a pointer to an integer into which is written ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if ** it is not disabled, 1 if it is. **
** ** [[SQLITE_DBCONFIG_RESET_DATABASE]]
SQLITE_DBCONFIG_RESET_DATABASE
**
Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run ** [VACUUM] in order to reset a database back to an empty database ** with no schema and no content. The following process works even for ** a badly corrupted database file: **
    **
  1. If the database connection is newly opened, make sure it has read the ** database schema by preparing then discarding some query against the ** database, or calling sqlite3_table_column_metadata(), ignoring any ** errors. This step is only necessary if the application desires to keep ** the database in WAL mode after the reset if it was in WAL mode before ** the reset. **
  2. sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0); **
  3. [sqlite3_exec](db, "[VACUUM]", 0, 0, 0); **
  4. sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0); **
** Because resetting a database is destructive and irreversible, the ** process requires the use of this obscure API and multiple steps to ** help ensure that it does not happen by accident. Because this ** feature must be capable of resetting corrupt databases, and ** shutting down virtual tables may require access to that corrupt ** storage, the library must abandon any installed virtual tables ** without calling their xDestroy() methods. ** ** [[SQLITE_DBCONFIG_DEFENSIVE]]
SQLITE_DBCONFIG_DEFENSIVE
**
The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the ** "defensive" flag for a database connection. When the defensive ** flag is enabled, language features that allow ordinary SQL to ** deliberately corrupt the database file are disabled. The disabled ** features include but are not limited to the following: **
    **
  • The [PRAGMA writable_schema=ON] statement. **
  • The [PRAGMA journal_mode=OFF] statement. **
  • The [PRAGMA schema_version=N] statement. **
  • Writes to the [sqlite_dbpage] virtual table. **
  • Direct writes to [shadow tables]. **
**
** ** [[SQLITE_DBCONFIG_WRITABLE_SCHEMA]]
SQLITE_DBCONFIG_WRITABLE_SCHEMA
**
The SQLITE_DBCONFIG_WRITABLE_SCHEMA option activates or deactivates the ** "writable_schema" flag. This has the same effect and is logically equivalent ** to setting [PRAGMA writable_schema=ON] or [PRAGMA writable_schema=OFF]. ** The first argument to this setting is an integer which is 0 to disable ** the writable_schema, positive to enable writable_schema, or negative to ** leave the setting unchanged. The second parameter is a pointer to an ** integer into which is written 0 or 1 to indicate whether the writable_schema ** is enabled or disabled following this call. **
** ** [[SQLITE_DBCONFIG_LEGACY_ALTER_TABLE]] **
SQLITE_DBCONFIG_LEGACY_ALTER_TABLE
**
The SQLITE_DBCONFIG_LEGACY_ALTER_TABLE option activates or deactivates ** the legacy behavior of the [ALTER TABLE RENAME] command such it ** behaves as it did prior to [version 3.24.0] (2018-06-04). See the ** "Compatibility Notice" on the [ALTER TABLE RENAME documentation] for ** additional information. This feature can also be turned on and off ** using the [PRAGMA legacy_alter_table] statement. **
** ** [[SQLITE_DBCONFIG_DQS_DML]] **
SQLITE_DBCONFIG_DQS_DML
**
The SQLITE_DBCONFIG_DQS_DML option activates or deactivates ** the legacy [double-quoted string literal] misfeature for DML statements ** only, that is DELETE, INSERT, SELECT, and UPDATE statements. The ** default value of this setting is determined by the [-DSQLITE_DQS] ** compile-time option. **
** ** [[SQLITE_DBCONFIG_DQS_DDL]] **
SQLITE_DBCONFIG_DQS_DDL
**
The SQLITE_DBCONFIG_DQS option activates or deactivates ** the legacy [double-quoted string literal] misfeature for DDL statements, ** such as CREATE TABLE and CREATE INDEX. The ** default value of this setting is determined by the [-DSQLITE_DQS] ** compile-time option. **
** ** [[SQLITE_DBCONFIG_TRUSTED_SCHEMA]] **
SQLITE_DBCONFIG_TRUSTED_SCHEMA
**
The SQLITE_DBCONFIG_TRUSTED_SCHEMA option tells SQLite to ** assume that database schemas are untainted by malicious content. ** When the SQLITE_DBCONFIG_TRUSTED_SCHEMA option is disabled, SQLite ** takes additional defensive steps to protect the application from harm ** including: **
    **
  • Prohibit the use of SQL functions inside triggers, views, ** CHECK constraints, DEFAULT clauses, expression indexes, ** partial indexes, or generated columns ** unless those functions are tagged with [SQLITE_INNOCUOUS]. **
  • Prohibit the use of virtual tables inside of triggers or views ** unless those virtual tables are tagged with [SQLITE_VTAB_INNOCUOUS]. **
** This setting defaults to "on" for legacy compatibility, however ** all applications are advised to turn it off if possible. This setting ** can also be controlled using the [PRAGMA trusted_schema] statement. **
** ** [[SQLITE_DBCONFIG_LEGACY_FILE_FORMAT]] **
SQLITE_DBCONFIG_LEGACY_FILE_FORMAT
**
The SQLITE_DBCONFIG_LEGACY_FILE_FORMAT option activates or deactivates ** the legacy file format flag. When activated, this flag causes all newly ** created database file to have a schema format version number (the 4-byte ** integer found at offset 44 into the database header) of 1. This in turn ** means that the resulting database file will be readable and writable by ** any SQLite version back to 3.0.0 ([dateof:3.0.0]). Without this setting, ** newly created databases are generally not understandable by SQLite versions ** prior to 3.3.0 ([dateof:3.3.0]). As these words are written, there ** is now scarcely any need to generate database files that are compatible ** all the way back to version 3.0.0, and so this setting is of little ** practical use, but is provided so that SQLite can continue to claim the ** ability to generate new database files that are compatible with version ** 3.0.0. **

Note that when the SQLITE_DBCONFIG_LEGACY_FILE_FORMAT setting is on, ** the [VACUUM] command will fail with an obscure error when attempting to ** process a table with generated columns and a descending index. This is ** not considered a bug since SQLite versions 3.3.0 and earlier do not support ** either generated columns or descending indexes. **

** ** [[SQLITE_DBCONFIG_STMT_SCANSTATUS]] **
SQLITE_DBCONFIG_STMT_SCANSTATUS
**
The SQLITE_DBCONFIG_STMT_SCANSTATUS option is only useful in ** SQLITE_ENABLE_STMT_SCANSTATUS builds. In this case, it sets or clears ** a flag that enables collection of the sqlite3_stmt_scanstatus_v2() ** statistics. For statistics to be collected, the flag must be set on ** the database handle both when the SQL statement is prepared and when it ** is stepped. The flag is set (collection of statistics is enabled) ** by default.

This option takes two arguments: an integer and a pointer to ** an integer.. The first argument is 1, 0, or -1 to enable, disable, or ** leave unchanged the statement scanstatus option. If the second argument ** is not NULL, then the value of the statement scanstatus setting after ** processing the first argument is written into the integer that the second ** argument points to. **

** ** [[SQLITE_DBCONFIG_REVERSE_SCANORDER]] **
SQLITE_DBCONFIG_REVERSE_SCANORDER
**
The SQLITE_DBCONFIG_REVERSE_SCANORDER option changes the default order ** in which tables and indexes are scanned so that the scans start at the end ** and work toward the beginning rather than starting at the beginning and ** working toward the end. Setting SQLITE_DBCONFIG_REVERSE_SCANORDER is the ** same as setting [PRAGMA reverse_unordered_selects].

This option takes ** two arguments which are an integer and a pointer to an integer. The first ** argument is 1, 0, or -1 to enable, disable, or leave unchanged the ** reverse scan order flag, respectively. If the second argument is not NULL, ** then 0 or 1 is written into the integer that the second argument points to ** depending on if the reverse scan order flag is set after processing the ** first argument. **

** ** [[SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE]] **
SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE
**
The SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE option enables or disables ** the ability of the [ATTACH DATABASE] SQL command to create a new database ** file if the database filed named in the ATTACH command does not already ** exist. This ability of ATTACH to create a new database is enabled by ** default. Applications can disable or reenable the ability for ATTACH to ** create new database files using this DBCONFIG option.

** This option takes two arguments which are an integer and a pointer ** to an integer. The first argument is 1, 0, or -1 to enable, disable, or ** leave unchanged the attach-create flag, respectively. If the second ** argument is not NULL, then 0 or 1 is written into the integer that the ** second argument points to depending on if the attach-create flag is set ** after processing the first argument. **

** ** [[SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE]] **
SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE
**
The SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE option enables or disables the ** ability of the [ATTACH DATABASE] SQL command to open a database for writing. ** This capability is enabled by default. Applications can disable or ** reenable this capability using the current DBCONFIG option. If the ** the this capability is disabled, the [ATTACH] command will still work, ** but the database will be opened read-only. If this option is disabled, ** then the ability to create a new database using [ATTACH] is also disabled, ** regardless of the value of the [SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE] ** option.

** This option takes two arguments which are an integer and a pointer ** to an integer. The first argument is 1, 0, or -1 to enable, disable, or ** leave unchanged the ability to ATTACH another database for writing, ** respectively. If the second argument is not NULL, then 0 or 1 is written ** into the integer to which the second argument points, depending on whether ** the ability to ATTACH a read/write database is enabled or disabled ** after processing the first argument. **

** ** [[SQLITE_DBCONFIG_ENABLE_COMMENTS]] **
SQLITE_DBCONFIG_ENABLE_COMMENTS
**
The SQLITE_DBCONFIG_ENABLE_COMMENTS option enables or disables the ** ability to include comments in SQL text. Comments are enabled by default. ** An application can disable or reenable comments in SQL text using this ** DBCONFIG option.

** This option takes two arguments which are an integer and a pointer ** to an integer. The first argument is 1, 0, or -1 to enable, disable, or ** leave unchanged the ability to use comments in SQL text, ** respectively. If the second argument is not NULL, then 0 or 1 is written ** into the integer that the second argument points to depending on if ** comments are allowed in SQL text after processing the first argument. **

** **
** ** [[DBCONFIG arguments]]

Arguments To SQLITE_DBCONFIG Options

** **

Most of the SQLITE_DBCONFIG options take two arguments, so that the ** overall call to [sqlite3_db_config()] has a total of four parameters. ** The first argument (the third parameter to sqlite3_db_config()) is a integer. ** The second argument is a pointer to an integer. If the first argument is 1, ** then the option becomes enabled. If the first integer argument is 0, then the ** option is disabled. If the first argument is -1, then the option setting ** is unchanged. The second argument, the pointer to an integer, may be NULL. ** If the second argument is not NULL, then a value of 0 or 1 is written into ** the integer to which the second argument points, depending on whether the ** setting is disabled or enabled after applying any changes specified by ** the first argument. ** **

While most SQLITE_DBCONFIG options use the argument format ** described in the previous paragraph, the [SQLITE_DBCONFIG_MAINDBNAME] ** and [SQLITE_DBCONFIG_LOOKASIDE] options are different. See the ** documentation of those exceptional options for details. */ #define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */ #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */ #define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */ #define SQLITE_DBCONFIG_ENABLE_TRIGGER 1003 /* int int* */ #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */ #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */ #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */ #define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */ #define SQLITE_DBCONFIG_TRIGGER_EQP 1008 /* int int* */ #define SQLITE_DBCONFIG_RESET_DATABASE 1009 /* int int* */ #define SQLITE_DBCONFIG_DEFENSIVE 1010 /* int int* */ #define SQLITE_DBCONFIG_WRITABLE_SCHEMA 1011 /* int int* */ #define SQLITE_DBCONFIG_LEGACY_ALTER_TABLE 1012 /* int int* */ #define SQLITE_DBCONFIG_DQS_DML 1013 /* int int* */ #define SQLITE_DBCONFIG_DQS_DDL 1014 /* int int* */ #define SQLITE_DBCONFIG_ENABLE_VIEW 1015 /* int int* */ #define SQLITE_DBCONFIG_LEGACY_FILE_FORMAT 1016 /* int int* */ #define SQLITE_DBCONFIG_TRUSTED_SCHEMA 1017 /* int int* */ #define SQLITE_DBCONFIG_STMT_SCANSTATUS 1018 /* int int* */ #define SQLITE_DBCONFIG_REVERSE_SCANORDER 1019 /* int int* */ #define SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE 1020 /* int int* */ #define SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE 1021 /* int int* */ #define SQLITE_DBCONFIG_ENABLE_COMMENTS 1022 /* int int* */ #define SQLITE_DBCONFIG_MAX 1022 /* Largest DBCONFIG */ /* ** CAPI3REF: Enable Or Disable Extended Result Codes ** METHOD: sqlite3 ** ** ^The sqlite3_extended_result_codes() routine enables or disables the ** [extended result codes] feature of SQLite. ^The extended result ** codes are disabled by default for historical compatibility. */ SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff); /* ** CAPI3REF: Last Insert Rowid ** METHOD: sqlite3 ** ** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables) ** has a unique 64-bit signed ** integer key called the [ROWID | "rowid"]. ^The rowid is always available ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those ** names are not also used by explicitly declared columns. ^If ** the table has a column of type [INTEGER PRIMARY KEY] then that column ** is another alias for the rowid. ** ** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of ** the most recent successful [INSERT] into a rowid table or [virtual table] ** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not ** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred ** on the database connection D, then sqlite3_last_insert_rowid(D) returns ** zero. ** ** As well as being set automatically as rows are inserted into database ** tables, the value returned by this function may be set explicitly by ** [sqlite3_set_last_insert_rowid()] ** ** Some virtual table implementations may INSERT rows into rowid tables as ** part of committing a transaction (e.g. to flush data accumulated in memory ** to disk). In this case subsequent calls to this function return the rowid ** associated with these internal INSERT operations, which leads to ** unintuitive results. Virtual table implementations that do write to rowid ** tables in this way can avoid this problem by restoring the original ** rowid value using [sqlite3_set_last_insert_rowid()] before returning ** control to the user. ** ** ^(If an [INSERT] occurs within a trigger then this routine will ** return the [rowid] of the inserted row as long as the trigger is ** running. Once the trigger program ends, the value returned ** by this routine reverts to what it was before the trigger was fired.)^ ** ** ^An [INSERT] that fails due to a constraint violation is not a ** successful [INSERT] and does not change the value returned by this ** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, ** and INSERT OR ABORT make no changes to the return value of this ** routine when their insertion fails. ^(When INSERT OR REPLACE ** encounters a constraint violation, it does not fail. The ** INSERT continues to completion after deleting rows that caused ** the constraint problem so INSERT OR REPLACE will always change ** the return value of this interface.)^ ** ** ^For the purposes of this routine, an [INSERT] is considered to ** be successful even if it is subsequently rolled back. ** ** This function is accessible to SQL statements via the ** [last_insert_rowid() SQL function]. ** ** If a separate thread performs a new [INSERT] on the same ** database connection while the [sqlite3_last_insert_rowid()] ** function is running and thus changes the last insert [rowid], ** then the value returned by [sqlite3_last_insert_rowid()] is ** unpredictable and might not equal either the old or the new ** last insert [rowid]. */ SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); /* ** CAPI3REF: Set the Last Insert Rowid value. ** METHOD: sqlite3 ** ** The sqlite3_set_last_insert_rowid(D, R) method allows the application to ** set the value returned by calling sqlite3_last_insert_rowid(D) to R ** without inserting a row into the database. */ SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64); /* ** CAPI3REF: Count The Number Of Rows Modified ** METHOD: sqlite3 ** ** ^These functions return the number of rows modified, inserted or ** deleted by the most recently completed INSERT, UPDATE or DELETE ** statement on the database connection specified by the only parameter. ** The two functions are identical except for the type of the return value ** and that if the number of rows modified by the most recent INSERT, UPDATE, ** or DELETE is greater than the maximum value supported by type "int", then ** the return value of sqlite3_changes() is undefined. ^Executing any other ** type of SQL statement does not modify the value returned by these functions. ** For the purposes of this interface, a CREATE TABLE AS SELECT statement ** does not count as an INSERT, UPDATE or DELETE statement and hence the rows ** added to the new table by the CREATE TABLE AS SELECT statement are not ** counted. ** ** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers], ** [foreign key actions] or [REPLACE] constraint resolution are not counted. ** ** Changes to a view that are intercepted by ** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value ** returned by sqlite3_changes() immediately after an INSERT, UPDATE or ** DELETE statement run on a view is always zero. Only changes made to real ** tables are counted. ** ** Things are more complicated if the sqlite3_changes() function is ** executed while a trigger program is running. This may happen if the ** program uses the [changes() SQL function], or if some other callback ** function invokes sqlite3_changes() directly. Essentially: ** **

    **
  • ^(Before entering a trigger program the value returned by ** sqlite3_changes() function is saved. After the trigger program ** has finished, the original value is restored.)^ ** **
  • ^(Within a trigger program each INSERT, UPDATE and DELETE ** statement sets the value returned by sqlite3_changes() ** upon completion as normal. Of course, this value will not include ** any changes performed by sub-triggers, as the sqlite3_changes() ** value will be saved and restored after each sub-trigger has run.)^ **
** ** ^This means that if the changes() SQL function (or similar) is used ** by the first INSERT, UPDATE or DELETE statement within a trigger, it ** returns the value as set when the calling statement began executing. ** ^If it is used by the second or subsequent such statement within a trigger ** program, the value returned reflects the number of rows modified by the ** previous INSERT, UPDATE or DELETE statement within the same trigger. ** ** If a separate thread makes changes on the same database connection ** while [sqlite3_changes()] is running then the value returned ** is unpredictable and not meaningful. ** ** See also: **
    **
  • the [sqlite3_total_changes()] interface **
  • the [count_changes pragma] **
  • the [changes() SQL function] **
  • the [data_version pragma] **
*/ SQLITE_API int sqlite3_changes(sqlite3*); SQLITE_API sqlite3_int64 sqlite3_changes64(sqlite3*); /* ** CAPI3REF: Total Number Of Rows Modified ** METHOD: sqlite3 ** ** ^These functions return the total number of rows inserted, modified or ** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed ** since the database connection was opened, including those executed as ** part of trigger programs. The two functions are identical except for the ** type of the return value and that if the number of rows modified by the ** connection exceeds the maximum value supported by type "int", then ** the return value of sqlite3_total_changes() is undefined. ^Executing ** any other type of SQL statement does not affect the value returned by ** sqlite3_total_changes(). ** ** ^Changes made as part of [foreign key actions] are included in the ** count, but those made as part of REPLACE constraint resolution are ** not. ^Changes to a view that are intercepted by INSTEAD OF triggers ** are not counted. ** ** The [sqlite3_total_changes(D)] interface only reports the number ** of rows that changed due to SQL statement run against database ** connection D. Any changes by other database connections are ignored. ** To detect changes against a database file from other database ** connections use the [PRAGMA data_version] command or the ** [SQLITE_FCNTL_DATA_VERSION] [file control]. ** ** If a separate thread makes changes on the same database connection ** while [sqlite3_total_changes()] is running then the value ** returned is unpredictable and not meaningful. ** ** See also: **
    **
  • the [sqlite3_changes()] interface **
  • the [count_changes pragma] **
  • the [changes() SQL function] **
  • the [data_version pragma] **
  • the [SQLITE_FCNTL_DATA_VERSION] [file control] **
*/ SQLITE_API int sqlite3_total_changes(sqlite3*); SQLITE_API sqlite3_int64 sqlite3_total_changes64(sqlite3*); /* ** CAPI3REF: Interrupt A Long-Running Query ** METHOD: sqlite3 ** ** ^This function causes any pending database operation to abort and ** return at its earliest opportunity. This routine is typically ** called in response to a user action such as pressing "Cancel" ** or Ctrl-C where the user wants a long query operation to halt ** immediately. ** ** ^It is safe to call this routine from a thread different from the ** thread that is currently running the database operation. But it ** is not safe to call this routine with a [database connection] that ** is closed or might close before sqlite3_interrupt() returns. ** ** ^If an SQL operation is very nearly finished at the time when ** sqlite3_interrupt() is called, then it might not have an opportunity ** to be interrupted and might continue to completion. ** ** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT]. ** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE ** that is inside an explicit transaction, then the entire transaction ** will be rolled back automatically. ** ** ^The sqlite3_interrupt(D) call is in effect until all currently running ** SQL statements on [database connection] D complete. ^Any new SQL statements ** that are started after the sqlite3_interrupt() call and before the ** running statement count reaches zero are interrupted as if they had been ** running prior to the sqlite3_interrupt() call. ^New SQL statements ** that are started after the running statement count reaches zero are ** not effected by the sqlite3_interrupt(). ** ^A call to sqlite3_interrupt(D) that occurs when there are no running ** SQL statements is a no-op and has no effect on SQL statements ** that are started after the sqlite3_interrupt() call returns. ** ** ^The [sqlite3_is_interrupted(D)] interface can be used to determine whether ** or not an interrupt is currently in effect for [database connection] D. ** It returns 1 if an interrupt is currently in effect, or 0 otherwise. */ SQLITE_API void sqlite3_interrupt(sqlite3*); SQLITE_API int sqlite3_is_interrupted(sqlite3*); /* ** CAPI3REF: Determine If An SQL Statement Is Complete ** ** These routines are useful during command-line input to determine if the ** currently entered text seems to form a complete SQL statement or ** if additional input is needed before sending the text into ** SQLite for parsing. ^These routines return 1 if the input string ** appears to be a complete SQL statement. ^A statement is judged to be ** complete if it ends with a semicolon token and is not a prefix of a ** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within ** string literals or quoted identifier names or comments are not ** independent tokens (they are part of the token in which they are ** embedded) and thus do not count as a statement terminator. ^Whitespace ** and comments that follow the final semicolon are ignored. ** ** ^These routines return 0 if the statement is incomplete. ^If a ** memory allocation fails, then SQLITE_NOMEM is returned. ** ** ^These routines do not parse the SQL statements thus ** will not detect syntactically incorrect SQL. ** ** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior ** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked ** automatically by sqlite3_complete16(). If that initialization fails, ** then the return value from sqlite3_complete16() will be non-zero ** regardless of whether or not the input SQL is complete.)^ ** ** The input to [sqlite3_complete()] must be a zero-terminated ** UTF-8 string. ** ** The input to [sqlite3_complete16()] must be a zero-terminated ** UTF-16 string in native byte order. */ SQLITE_API int sqlite3_complete(const char *sql); SQLITE_API int sqlite3_complete16(const void *sql); /* ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors ** KEYWORDS: {busy-handler callback} {busy handler} ** METHOD: sqlite3 ** ** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X ** that might be invoked with argument P whenever ** an attempt is made to access a database table associated with ** [database connection] D when another thread ** or process has the table locked. ** The sqlite3_busy_handler() interface is used to implement ** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout]. ** ** ^If the busy callback is NULL, then [SQLITE_BUSY] ** is returned immediately upon encountering the lock. ^If the busy callback ** is not NULL, then the callback might be invoked with two arguments. ** ** ^The first argument to the busy handler is a copy of the void* pointer which ** is the third argument to sqlite3_busy_handler(). ^The second argument to ** the busy handler callback is the number of times that the busy handler has ** been invoked previously for the same locking event. ^If the ** busy callback returns 0, then no additional attempts are made to ** access the database and [SQLITE_BUSY] is returned ** to the application. ** ^If the callback returns non-zero, then another attempt ** is made to access the database and the cycle repeats. ** ** The presence of a busy handler does not guarantee that it will be invoked ** when there is lock contention. ^If SQLite determines that invoking the busy ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY] ** to the application instead of invoking the ** busy handler. ** Consider a scenario where one process is holding a read lock that ** it is trying to promote to a reserved lock and ** a second process is holding a reserved lock that it is trying ** to promote to an exclusive lock. The first process cannot proceed ** because it is blocked by the second and the second process cannot ** proceed because it is blocked by the first. If both processes ** invoke the busy handlers, neither will make any progress. Therefore, ** SQLite returns [SQLITE_BUSY] for the first process, hoping that this ** will induce the first process to release its read lock and allow ** the second process to proceed. ** ** ^The default busy callback is NULL. ** ** ^(There can only be a single busy handler defined for each ** [database connection]. Setting a new busy handler clears any ** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()] ** or evaluating [PRAGMA busy_timeout=N] will change the ** busy handler and thus clear any previously set busy handler. ** ** The busy callback should not take any actions which modify the ** database connection that invoked the busy handler. In other words, ** the busy handler is not reentrant. Any such actions ** result in undefined behavior. ** ** A busy handler must not close the database connection ** or [prepared statement] that invoked the busy handler. */ SQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*); /* ** CAPI3REF: Set A Busy Timeout ** METHOD: sqlite3 ** ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps ** for a specified amount of time when a table is locked. ^The handler ** will sleep multiple times until at least "ms" milliseconds of sleeping ** have accumulated. ^After at least "ms" milliseconds of sleeping, ** the handler returns 0 which causes [sqlite3_step()] to return ** [SQLITE_BUSY]. ** ** ^Calling this routine with an argument less than or equal to zero ** turns off all busy handlers. ** ** ^(There can only be a single busy handler for a particular ** [database connection] at any given moment. If another busy handler ** was defined (using [sqlite3_busy_handler()]) prior to calling ** this routine, that other busy handler is cleared.)^ ** ** See also: [PRAGMA busy_timeout] */ SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms); /* ** CAPI3REF: Set the Setlk Timeout ** METHOD: sqlite3 ** ** This routine is only useful in SQLITE_ENABLE_SETLK_TIMEOUT builds. If ** the VFS supports blocking locks, it sets the timeout in ms used by ** eligible locks taken on wal mode databases by the specified database ** handle. In non-SQLITE_ENABLE_SETLK_TIMEOUT builds, or if the VFS does ** not support blocking locks, this function is a no-op. ** ** Passing 0 to this function disables blocking locks altogether. Passing ** -1 to this function requests that the VFS blocks for a long time - ** indefinitely if possible. The results of passing any other negative value ** are undefined. ** ** Internally, each SQLite database handle store two timeout values - the ** busy-timeout (used for rollback mode databases, or if the VFS does not ** support blocking locks) and the setlk-timeout (used for blocking locks ** on wal-mode databases). The sqlite3_busy_timeout() method sets both ** values, this function sets only the setlk-timeout value. Therefore, ** to configure separate busy-timeout and setlk-timeout values for a single ** database handle, call sqlite3_busy_timeout() followed by this function. ** ** Whenever the number of connections to a wal mode database falls from ** 1 to 0, the last connection takes an exclusive lock on the database, ** then checkpoints and deletes the wal file. While it is doing this, any ** new connection that tries to read from the database fails with an ** SQLITE_BUSY error. Or, if the SQLITE_SETLK_BLOCK_ON_CONNECT flag is ** passed to this API, the new connection blocks until the exclusive lock ** has been released. */ SQLITE_API int sqlite3_setlk_timeout(sqlite3*, int ms, int flags); /* ** CAPI3REF: Flags for sqlite3_setlk_timeout() */ #define SQLITE_SETLK_BLOCK_ON_CONNECT 0x01 /* ** CAPI3REF: Convenience Routines For Running Queries ** METHOD: sqlite3 ** ** This is a legacy interface that is preserved for backwards compatibility. ** Use of this interface is not recommended. ** ** Definition: A result table is memory data structure created by the ** [sqlite3_get_table()] interface. A result table records the ** complete query results from one or more queries. ** ** The table conceptually has a number of rows and columns. But ** these numbers are not part of the result table itself. These ** numbers are obtained separately. Let N be the number of rows ** and M be the number of columns. ** ** A result table is an array of pointers to zero-terminated UTF-8 strings. ** There are (N+1)*M elements in the array. The first M pointers point ** to zero-terminated strings that contain the names of the columns. ** The remaining entries all point to query results. NULL values result ** in NULL pointers. All other values are in their UTF-8 zero-terminated ** string representation as returned by [sqlite3_column_text()]. ** ** A result table might consist of one or more memory allocations. ** It is not safe to pass a result table directly to [sqlite3_free()]. ** A result table should be deallocated using [sqlite3_free_table()]. ** ** ^(As an example of the result table format, suppose a query result ** is as follows: ** **
**        Name        | Age
**        -----------------------
**        Alice       | 43
**        Bob         | 28
**        Cindy       | 21
** 
** ** There are two columns (M==2) and three rows (N==3). Thus the ** result table has 8 entries. Suppose the result table is stored ** in an array named azResult. Then azResult holds this content: ** **
**        azResult[0] = "Name";
**        azResult[1] = "Age";
**        azResult[2] = "Alice";
**        azResult[3] = "43";
**        azResult[4] = "Bob";
**        azResult[5] = "28";
**        azResult[6] = "Cindy";
**        azResult[7] = "21";
** 
)^ ** ** ^The sqlite3_get_table() function evaluates one or more ** semicolon-separated SQL statements in the zero-terminated UTF-8 ** string of its 2nd parameter and returns a result table to the ** pointer given in its 3rd parameter. ** ** After the application has finished with the result from sqlite3_get_table(), ** it must pass the result table pointer to sqlite3_free_table() in order to ** release the memory that was malloced. Because of the way the ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling ** function must not try to call [sqlite3_free()] directly. Only ** [sqlite3_free_table()] is able to release the memory properly and safely. ** ** The sqlite3_get_table() interface is implemented as a wrapper around ** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access ** to any internal data structures of SQLite. It uses only the public ** interface defined here. As a consequence, errors that occur in the ** wrapper layer outside of the internal [sqlite3_exec()] call are not ** reflected in subsequent calls to [sqlite3_errcode()] or ** [sqlite3_errmsg()]. */ SQLITE_API int sqlite3_get_table( sqlite3 *db, /* An open database */ const char *zSql, /* SQL to be evaluated */ char ***pazResult, /* Results of the query */ int *pnRow, /* Number of result rows written here */ int *pnColumn, /* Number of result columns written here */ char **pzErrmsg /* Error msg written here */ ); SQLITE_API void sqlite3_free_table(char **result); /* ** CAPI3REF: Formatted String Printing Functions ** ** These routines are work-alikes of the "printf()" family of functions ** from the standard C library. ** These routines understand most of the common formatting options from ** the standard library printf() ** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]). ** See the [built-in printf()] documentation for details. ** ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their ** results into memory obtained from [sqlite3_malloc64()]. ** The strings returned by these two routines should be ** released by [sqlite3_free()]. ^Both routines return a ** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough ** memory to hold the resulting string. ** ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from ** the standard C library. The result is written into the ** buffer supplied as the second parameter whose size is given by ** the first parameter. Note that the order of the ** first two parameters is reversed from snprintf().)^ This is an ** historical accident that cannot be fixed without breaking ** backwards compatibility. ^(Note also that sqlite3_snprintf() ** returns a pointer to its buffer instead of the number of ** characters actually written into the buffer.)^ We admit that ** the number of characters written would be a more useful return ** value but we cannot change the implementation of sqlite3_snprintf() ** now without breaking compatibility. ** ** ^As long as the buffer size is greater than zero, sqlite3_snprintf() ** guarantees that the buffer is always zero-terminated. ^The first ** parameter "n" is the total size of the buffer, including space for ** the zero terminator. So the longest string that can be completely ** written will be n-1 characters. ** ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf(). ** ** See also: [built-in printf()], [printf() SQL function] */ SQLITE_API char *sqlite3_mprintf(const char*,...); SQLITE_API char *sqlite3_vmprintf(const char*, va_list); SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...); SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list); /* ** CAPI3REF: Memory Allocation Subsystem ** ** The SQLite core uses these three routines for all of its own ** internal memory allocation needs. "Core" in the previous sentence ** does not include operating-system specific [VFS] implementation. The ** Windows VFS uses native malloc() and free() for some operations. ** ** ^The sqlite3_malloc() routine returns a pointer to a block ** of memory at least N bytes in length, where N is the parameter. ** ^If sqlite3_malloc() is unable to obtain sufficient free ** memory, it returns a NULL pointer. ^If the parameter N to ** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns ** a NULL pointer. ** ** ^The sqlite3_malloc64(N) routine works just like ** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead ** of a signed 32-bit integer. ** ** ^Calling sqlite3_free() with a pointer previously returned ** by sqlite3_malloc() or sqlite3_realloc() releases that memory so ** that it might be reused. ^The sqlite3_free() routine is ** a no-op if is called with a NULL pointer. Passing a NULL pointer ** to sqlite3_free() is harmless. After being freed, memory ** should neither be read nor written. Even reading previously freed ** memory might result in a segmentation fault or other severe error. ** Memory corruption, a segmentation fault, or other severe error ** might result if sqlite3_free() is called with a non-NULL pointer that ** was not obtained from sqlite3_malloc() or sqlite3_realloc(). ** ** ^The sqlite3_realloc(X,N) interface attempts to resize a ** prior memory allocation X to be at least N bytes. ** ^If the X parameter to sqlite3_realloc(X,N) ** is a NULL pointer then its behavior is identical to calling ** sqlite3_malloc(N). ** ^If the N parameter to sqlite3_realloc(X,N) is zero or ** negative then the behavior is exactly the same as calling ** sqlite3_free(X). ** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation ** of at least N bytes in size or NULL if insufficient memory is available. ** ^If M is the size of the prior allocation, then min(N,M) bytes ** of the prior allocation are copied into the beginning of buffer returned ** by sqlite3_realloc(X,N) and the prior allocation is freed. ** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the ** prior allocation is not freed. ** ** ^The sqlite3_realloc64(X,N) interfaces works the same as ** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead ** of a 32-bit signed integer. ** ** ^If X is a memory allocation previously obtained from sqlite3_malloc(), ** sqlite3_malloc64(), sqlite3_realloc(), or sqlite3_realloc64(), then ** sqlite3_msize(X) returns the size of that memory allocation in bytes. ** ^The value returned by sqlite3_msize(X) might be larger than the number ** of bytes requested when X was allocated. ^If X is a NULL pointer then ** sqlite3_msize(X) returns zero. If X points to something that is not ** the beginning of memory allocation, or if it points to a formerly ** valid memory allocation that has now been freed, then the behavior ** of sqlite3_msize(X) is undefined and possibly harmful. ** ** ^The memory returned by sqlite3_malloc(), sqlite3_realloc(), ** sqlite3_malloc64(), and sqlite3_realloc64() ** is always aligned to at least an 8 byte boundary, or to a ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time ** option is used. ** ** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()] ** must be either NULL or else pointers obtained from a prior ** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have ** not yet been released. ** ** The application must not read or write any part of ** a block of memory after it has been released using ** [sqlite3_free()] or [sqlite3_realloc()]. */ SQLITE_API void *sqlite3_malloc(int); SQLITE_API void *sqlite3_malloc64(sqlite3_uint64); SQLITE_API void *sqlite3_realloc(void*, int); SQLITE_API void *sqlite3_realloc64(void*, sqlite3_uint64); SQLITE_API void sqlite3_free(void*); SQLITE_API sqlite3_uint64 sqlite3_msize(void*); /* ** CAPI3REF: Memory Allocator Statistics ** ** SQLite provides these two interfaces for reporting on the status ** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()] ** routines, which form the built-in memory allocation subsystem. ** ** ^The [sqlite3_memory_used()] routine returns the number of bytes ** of memory currently outstanding (malloced but not freed). ** ^The [sqlite3_memory_highwater()] routine returns the maximum ** value of [sqlite3_memory_used()] since the high-water mark ** was last reset. ^The values returned by [sqlite3_memory_used()] and ** [sqlite3_memory_highwater()] include any overhead ** added by SQLite in its implementation of [sqlite3_malloc()], ** but not overhead added by the any underlying system library ** routines that [sqlite3_malloc()] may call. ** ** ^The memory high-water mark is reset to the current value of ** [sqlite3_memory_used()] if and only if the parameter to ** [sqlite3_memory_highwater()] is true. ^The value returned ** by [sqlite3_memory_highwater(1)] is the high-water mark ** prior to the reset. */ SQLITE_API sqlite3_int64 sqlite3_memory_used(void); SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag); /* ** CAPI3REF: Pseudo-Random Number Generator ** ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to ** select random [ROWID | ROWIDs] when inserting new records into a table that ** already uses the largest possible [ROWID]. The PRNG is also used for ** the built-in random() and randomblob() SQL functions. This interface allows ** applications to access the same PRNG for other purposes. ** ** ^A call to this routine stores N bytes of randomness into buffer P. ** ^The P parameter can be a NULL pointer. ** ** ^If this routine has not been previously called or if the previous ** call had N less than one or a NULL pointer for P, then the PRNG is ** seeded using randomness obtained from the xRandomness method of ** the default [sqlite3_vfs] object. ** ^If the previous call to this routine had an N of 1 or more and a ** non-NULL P then the pseudo-randomness is generated ** internally and without recourse to the [sqlite3_vfs] xRandomness ** method. */ SQLITE_API void sqlite3_randomness(int N, void *P); /* ** CAPI3REF: Compile-Time Authorization Callbacks ** METHOD: sqlite3 ** KEYWORDS: {authorizer callback} ** ** ^This routine registers an authorizer callback with a particular ** [database connection], supplied in the first argument. ** ^The authorizer callback is invoked as SQL statements are being compiled ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()], ** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()], ** and [sqlite3_prepare16_v3()]. ^At various ** points during the compilation process, as logic is being created ** to perform various actions, the authorizer callback is invoked to ** see if those actions are allowed. ^The authorizer callback should ** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the ** specific action but allow the SQL statement to continue to be ** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be ** rejected with an error. ^If the authorizer callback returns ** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY] ** then the [sqlite3_prepare_v2()] or equivalent call that triggered ** the authorizer will fail with an error message. ** ** When the callback returns [SQLITE_OK], that means the operation ** requested is ok. ^When the callback returns [SQLITE_DENY], the ** [sqlite3_prepare_v2()] or equivalent call that triggered the ** authorizer will fail with an error message explaining that ** access is denied. ** ** ^The first parameter to the authorizer callback is a copy of the third ** parameter to the sqlite3_set_authorizer() interface. ^The second parameter ** to the callback is an integer [SQLITE_COPY | action code] that specifies ** the particular action to be authorized. ^The third through sixth parameters ** to the callback are either NULL pointers or zero-terminated strings ** that contain additional details about the action to be authorized. ** Applications must always be prepared to encounter a NULL pointer in any ** of the third through the sixth parameters of the authorization callback. ** ** ^If the action code is [SQLITE_READ] ** and the callback returns [SQLITE_IGNORE] then the ** [prepared statement] statement is constructed to substitute ** a NULL value in place of the table column that would have ** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE] ** return can be used to deny an untrusted user access to individual ** columns of a table. ** ^When a table is referenced by a [SELECT] but no column values are ** extracted from that table (for example in a query like ** "SELECT count(*) FROM tab") then the [SQLITE_READ] authorizer callback ** is invoked once for that table with a column name that is an empty string. ** ^If the action code is [SQLITE_DELETE] and the callback returns ** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the ** [truncate optimization] is disabled and all rows are deleted individually. ** ** An authorizer is used when [sqlite3_prepare | preparing] ** SQL statements from an untrusted source, to ensure that the SQL statements ** do not try to access data they are not allowed to see, or that they do not ** try to execute malicious statements that damage the database. For ** example, an application may allow a user to enter arbitrary ** SQL queries for evaluation by a database. But the application does ** not want the user to be able to make arbitrary changes to the ** database. An authorizer could then be put in place while the ** user-entered SQL is being [sqlite3_prepare | prepared] that ** disallows everything except [SELECT] statements. ** ** Applications that need to process SQL from untrusted sources ** might also consider lowering resource limits using [sqlite3_limit()] ** and limiting database size using the [max_page_count] [PRAGMA] ** in addition to using an authorizer. ** ** ^(Only a single authorizer can be in place on a database connection ** at a time. Each call to sqlite3_set_authorizer overrides the ** previous call.)^ ^Disable the authorizer by installing a NULL callback. ** The authorizer is disabled by default. ** ** The authorizer callback must not do anything that will modify ** the database connection that invoked the authorizer callback. ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their ** database connections for the meaning of "modify" in this paragraph. ** ** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the ** statement might be re-prepared during [sqlite3_step()] due to a ** schema change. Hence, the application should ensure that the ** correct authorizer callback remains in place during the [sqlite3_step()]. ** ** ^Note that the authorizer callback is invoked only during ** [sqlite3_prepare()] or its variants. Authorization is not ** performed during statement evaluation in [sqlite3_step()], unless ** as stated in the previous paragraph, sqlite3_step() invokes ** sqlite3_prepare_v2() to reprepare a statement after a schema change. */ SQLITE_API int sqlite3_set_authorizer( sqlite3*, int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), void *pUserData ); /* ** CAPI3REF: Authorizer Return Codes ** ** The [sqlite3_set_authorizer | authorizer callback function] must ** return either [SQLITE_OK] or one of these two constants in order ** to signal SQLite whether or not the action is permitted. See the ** [sqlite3_set_authorizer | authorizer documentation] for additional ** information. ** ** Note that SQLITE_IGNORE is also used as a [conflict resolution mode] ** returned from the [sqlite3_vtab_on_conflict()] interface. */ #define SQLITE_DENY 1 /* Abort the SQL statement with an error */ #define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */ /* ** CAPI3REF: Authorizer Action Codes ** ** The [sqlite3_set_authorizer()] interface registers a callback function ** that is invoked to authorize certain SQL statement actions. The ** second parameter to the callback is an integer code that specifies ** what action is being authorized. These are the integer action codes that ** the authorizer callback may be passed. ** ** These action code values signify what kind of operation is to be ** authorized. The 3rd and 4th parameters to the authorization ** callback function will be parameters or NULL depending on which of these ** codes is used as the second parameter. ^(The 5th parameter to the ** authorizer callback is the name of the database ("main", "temp", ** etc.) if applicable.)^ ^The 6th parameter to the authorizer callback ** is the name of the inner-most trigger or view that is responsible for ** the access attempt or NULL if this access attempt is directly from ** top-level SQL code. */ /******************************************* 3rd ************ 4th ***********/ #define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */ #define SQLITE_CREATE_TABLE 2 /* Table Name NULL */ #define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */ #define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */ #define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */ #define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */ #define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */ #define SQLITE_CREATE_VIEW 8 /* View Name NULL */ #define SQLITE_DELETE 9 /* Table Name NULL */ #define SQLITE_DROP_INDEX 10 /* Index Name Table Name */ #define SQLITE_DROP_TABLE 11 /* Table Name NULL */ #define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */ #define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */ #define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */ #define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */ #define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */ #define SQLITE_DROP_VIEW 17 /* View Name NULL */ #define SQLITE_INSERT 18 /* Table Name NULL */ #define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */ #define SQLITE_READ 20 /* Table Name Column Name */ #define SQLITE_SELECT 21 /* NULL NULL */ #define SQLITE_TRANSACTION 22 /* Operation NULL */ #define SQLITE_UPDATE 23 /* Table Name Column Name */ #define SQLITE_ATTACH 24 /* Filename NULL */ #define SQLITE_DETACH 25 /* Database Name NULL */ #define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */ #define SQLITE_REINDEX 27 /* Index Name NULL */ #define SQLITE_ANALYZE 28 /* Table Name NULL */ #define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */ #define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */ #define SQLITE_FUNCTION 31 /* NULL Function Name */ #define SQLITE_SAVEPOINT 32 /* Operation Savepoint Name */ #define SQLITE_COPY 0 /* No longer used */ #define SQLITE_RECURSIVE 33 /* NULL NULL */ /* ** CAPI3REF: Deprecated Tracing And Profiling Functions ** DEPRECATED ** ** These routines are deprecated. Use the [sqlite3_trace_v2()] interface ** instead of the routines described here. ** ** These routines register callback functions that can be used for ** tracing and profiling the execution of SQL statements. ** ** ^The callback function registered by sqlite3_trace() is invoked at ** various times when an SQL statement is being run by [sqlite3_step()]. ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the ** SQL statement text as the statement first begins executing. ** ^(Additional sqlite3_trace() callbacks might occur ** as each triggered subprogram is entered. The callbacks for triggers ** contain a UTF-8 SQL comment that identifies the trigger.)^ ** ** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit ** the length of [bound parameter] expansion in the output of sqlite3_trace(). ** ** ^The callback function registered by sqlite3_profile() is invoked ** as each SQL statement finishes. ^The profile callback contains ** the original statement text and an estimate of wall-clock time ** of how long that statement took to run. ^The profile callback ** time is in units of nanoseconds, however the current implementation ** is only capable of millisecond resolution so the six least significant ** digits in the time are meaningless. Future versions of SQLite ** might provide greater resolution on the profiler callback. Invoking ** either [sqlite3_trace()] or [sqlite3_trace_v2()] will cancel the ** profile callback. */ SQLITE_API SQLITE_DEPRECATED void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); SQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*, void(*xProfile)(void*,const char*,sqlite3_uint64), void*); /* ** CAPI3REF: SQL Trace Event Codes ** KEYWORDS: SQLITE_TRACE ** ** These constants identify classes of events that can be monitored ** using the [sqlite3_trace_v2()] tracing logic. The M argument ** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of ** the following constants. ^The first argument to the trace callback ** is one of the following constants. ** ** New tracing constants may be added in future releases. ** ** ^A trace callback has four arguments: xCallback(T,C,P,X). ** ^The T argument is one of the integer type codes above. ** ^The C argument is a copy of the context pointer passed in as the ** fourth argument to [sqlite3_trace_v2()]. ** The P and X arguments are pointers whose meanings depend on T. ** **
** [[SQLITE_TRACE_STMT]]
SQLITE_TRACE_STMT
**
^An SQLITE_TRACE_STMT callback is invoked when a prepared statement ** first begins running and possibly at other times during the ** execution of the prepared statement, such as at the start of each ** trigger subprogram. ^The P argument is a pointer to the ** [prepared statement]. ^The X argument is a pointer to a string which ** is the unexpanded SQL text of the prepared statement or an SQL comment ** that indicates the invocation of a trigger. ^The callback can compute ** the same text that would have been returned by the legacy [sqlite3_trace()] ** interface by using the X argument when X begins with "--" and invoking ** [sqlite3_expanded_sql(P)] otherwise. ** ** [[SQLITE_TRACE_PROFILE]]
SQLITE_TRACE_PROFILE
**
^An SQLITE_TRACE_PROFILE callback provides approximately the same ** information as is provided by the [sqlite3_profile()] callback. ** ^The P argument is a pointer to the [prepared statement] and the ** X argument points to a 64-bit integer which is approximately ** the number of nanoseconds that the prepared statement took to run. ** ^The SQLITE_TRACE_PROFILE callback is invoked when the statement finishes. ** ** [[SQLITE_TRACE_ROW]]
SQLITE_TRACE_ROW
**
^An SQLITE_TRACE_ROW callback is invoked whenever a prepared ** statement generates a single row of result. ** ^The P argument is a pointer to the [prepared statement] and the ** X argument is unused. ** ** [[SQLITE_TRACE_CLOSE]]
SQLITE_TRACE_CLOSE
**
^An SQLITE_TRACE_CLOSE callback is invoked when a database ** connection closes. ** ^The P argument is a pointer to the [database connection] object ** and the X argument is unused. **
*/ #define SQLITE_TRACE_STMT 0x01 #define SQLITE_TRACE_PROFILE 0x02 #define SQLITE_TRACE_ROW 0x04 #define SQLITE_TRACE_CLOSE 0x08 /* ** CAPI3REF: SQL Trace Hook ** METHOD: sqlite3 ** ** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback ** function X against [database connection] D, using property mask M ** and context pointer P. ^If the X callback is ** NULL or if the M mask is zero, then tracing is disabled. The ** M argument should be the bitwise OR-ed combination of ** zero or more [SQLITE_TRACE] constants. ** ** ^Each call to either sqlite3_trace(D,X,P) or sqlite3_trace_v2(D,M,X,P) ** overrides (cancels) all prior calls to sqlite3_trace(D,X,P) or ** sqlite3_trace_v2(D,M,X,P) for the [database connection] D. Each ** database connection may have at most one trace callback. ** ** ^The X callback is invoked whenever any of the events identified by ** mask M occur. ^The integer return value from the callback is currently ** ignored, though this may change in future releases. Callback ** implementations should return zero to ensure future compatibility. ** ** ^A trace callback is invoked with four arguments: callback(T,C,P,X). ** ^The T argument is one of the [SQLITE_TRACE] ** constants to indicate why the callback was invoked. ** ^The C argument is a copy of the context pointer. ** The P and X arguments are pointers whose meanings depend on T. ** ** The sqlite3_trace_v2() interface is intended to replace the legacy ** interfaces [sqlite3_trace()] and [sqlite3_profile()], both of which ** are deprecated. */ SQLITE_API int sqlite3_trace_v2( sqlite3*, unsigned uMask, int(*xCallback)(unsigned,void*,void*,void*), void *pCtx ); /* ** CAPI3REF: Query Progress Callbacks ** METHOD: sqlite3 ** ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback ** function X to be invoked periodically during long running calls to ** [sqlite3_step()] and [sqlite3_prepare()] and similar for ** database connection D. An example use for this ** interface is to keep a GUI updated during a large query. ** ** ^The parameter P is passed through as the only parameter to the ** callback function X. ^The parameter N is the approximate number of ** [virtual machine instructions] that are evaluated between successive ** invocations of the callback X. ^If N is less than one then the progress ** handler is disabled. ** ** ^Only a single progress handler may be defined at one time per ** [database connection]; setting a new progress handler cancels the ** old one. ^Setting parameter X to NULL disables the progress handler. ** ^The progress handler is also disabled by setting N to a value less ** than 1. ** ** ^If the progress callback returns non-zero, the operation is ** interrupted. This feature can be used to implement a ** "Cancel" button on a GUI progress dialog box. ** ** The progress handler callback must not do anything that will modify ** the database connection that invoked the progress handler. ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their ** database connections for the meaning of "modify" in this paragraph. ** ** The progress handler callback would originally only be invoked from the ** bytecode engine. It still might be invoked during [sqlite3_prepare()] ** and similar because those routines might force a reparse of the schema ** which involves running the bytecode engine. However, beginning with ** SQLite version 3.41.0, the progress handler callback might also be ** invoked directly from [sqlite3_prepare()] while analyzing and generating ** code for complex queries. */ SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); /* ** CAPI3REF: Opening A New Database Connection ** CONSTRUCTOR: sqlite3 ** ** ^These routines open an SQLite database file as specified by the ** filename argument. ^The filename argument is interpreted as UTF-8 for ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte ** order for sqlite3_open16(). ^(A [database connection] handle is usually ** returned in *ppDb, even if an error occurs. The only exception is that ** if SQLite is unable to allocate memory to hold the [sqlite3] object, ** a NULL will be written into *ppDb instead of a pointer to the [sqlite3] ** object.)^ ^(If the database is opened (and/or created) successfully, then ** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain ** an English language description of the error following a failure of any ** of the sqlite3_open() routines. ** ** ^The default encoding will be UTF-8 for databases created using ** sqlite3_open() or sqlite3_open_v2(). ^The default encoding for databases ** created using sqlite3_open16() will be UTF-16 in the native byte order. ** ** Whether or not an error occurs when it is opened, resources ** associated with the [database connection] handle should be released by ** passing it to [sqlite3_close()] when it is no longer required. ** ** The sqlite3_open_v2() interface works like sqlite3_open() ** except that it accepts two additional parameters for additional control ** over the new database connection. ^(The flags parameter to ** sqlite3_open_v2() must include, at a minimum, one of the following ** three flag combinations:)^ ** **
** ^(
[SQLITE_OPEN_READONLY]
**
The database is opened in read-only mode. If the database does ** not already exist, an error is returned.
)^ ** ** ^(
[SQLITE_OPEN_READWRITE]
**
The database is opened for reading and writing if possible, or ** reading only if the file is write protected by the operating ** system. In either case the database must already exist, otherwise ** an error is returned. For historical reasons, if opening in ** read-write mode fails due to OS-level permissions, an attempt is ** made to open it in read-only mode. [sqlite3_db_readonly()] can be ** used to determine whether the database is actually ** read-write.
)^ ** ** ^(
[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]
**
The database is opened for reading and writing, and is created if ** it does not already exist. This is the behavior that is always used for ** sqlite3_open() and sqlite3_open16().
)^ **
** ** In addition to the required flags, the following optional flags are ** also supported: ** **
** ^(
[SQLITE_OPEN_URI]
**
The filename can be interpreted as a URI if this flag is set.
)^ ** ** ^(
[SQLITE_OPEN_MEMORY]
**
The database will be opened as an in-memory database. The database ** is named by the "filename" argument for the purposes of cache-sharing, ** if shared cache mode is enabled, but the "filename" is otherwise ignored. **
)^ ** ** ^(
[SQLITE_OPEN_NOMUTEX]
**
The new database connection will use the "multi-thread" ** [threading mode].)^ This means that separate threads are allowed ** to use SQLite at the same time, as long as each thread is using ** a different [database connection]. ** ** ^(
[SQLITE_OPEN_FULLMUTEX]
**
The new database connection will use the "serialized" ** [threading mode].)^ This means the multiple threads can safely ** attempt to use the same database connection at the same time. ** (Mutexes will block any actual concurrency, but in this mode ** there is no harm in trying.) ** ** ^(
[SQLITE_OPEN_SHAREDCACHE]
**
The database is opened [shared cache] enabled, overriding ** the default shared cache setting provided by ** [sqlite3_enable_shared_cache()].)^ ** The [use of shared cache mode is discouraged] and hence shared cache ** capabilities may be omitted from many builds of SQLite. In such cases, ** this option is a no-op. ** ** ^(
[SQLITE_OPEN_PRIVATECACHE]
**
The database is opened [shared cache] disabled, overriding ** the default shared cache setting provided by ** [sqlite3_enable_shared_cache()].)^ ** ** [[OPEN_EXRESCODE]] ^(
[SQLITE_OPEN_EXRESCODE]
**
The database connection comes up in "extended result code mode". ** In other words, the database behaves as if ** [sqlite3_extended_result_codes(db,1)] were called on the database ** connection as soon as the connection is created. In addition to setting ** the extended result code mode, this flag also causes [sqlite3_open_v2()] ** to return an extended result code.
** ** [[OPEN_NOFOLLOW]] ^(
[SQLITE_OPEN_NOFOLLOW]
**
The database filename is not allowed to contain a symbolic link
**
)^ ** ** If the 3rd parameter to sqlite3_open_v2() is not one of the ** required combinations shown above optionally combined with other ** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits] ** then the behavior is undefined. Historic versions of SQLite ** have silently ignored surplus bits in the flags parameter to ** sqlite3_open_v2(), however that behavior might not be carried through ** into future versions of SQLite and so applications should not rely ** upon it. Note in particular that the SQLITE_OPEN_EXCLUSIVE flag is a no-op ** for sqlite3_open_v2(). The SQLITE_OPEN_EXCLUSIVE does *not* cause ** the open to fail if the database already exists. The SQLITE_OPEN_EXCLUSIVE ** flag is intended for use by the [sqlite3_vfs|VFS interface] only, and not ** by sqlite3_open_v2(). ** ** ^The fourth parameter to sqlite3_open_v2() is the name of the ** [sqlite3_vfs] object that defines the operating system interface that ** the new database connection should use. ^If the fourth parameter is ** a NULL pointer then the default [sqlite3_vfs] object is used. ** ** ^If the filename is ":memory:", then a private, temporary in-memory database ** is created for the connection. ^This in-memory database will vanish when ** the database connection is closed. Future versions of SQLite might ** make use of additional special filenames that begin with the ":" character. ** It is recommended that when a database filename actually does begin with ** a ":" character you should prefix the filename with a pathname such as ** "./" to avoid ambiguity. ** ** ^If the filename is an empty string, then a private, temporary ** on-disk database will be created. ^This private database will be ** automatically deleted as soon as the database connection is closed. ** ** [[URI filenames in sqlite3_open()]]

URI Filenames

** ** ^If [URI filename] interpretation is enabled, and the filename argument ** begins with "file:", then the filename is interpreted as a URI. ^URI ** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is ** set in the third argument to sqlite3_open_v2(), or if it has ** been enabled globally using the [SQLITE_CONFIG_URI] option with the ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option. ** URI filename interpretation is turned off ** by default, but future releases of SQLite might enable URI filename ** interpretation by default. See "[URI filenames]" for additional ** information. ** ** URI filenames are parsed according to RFC 3986. ^If the URI contains an ** authority, then it must be either an empty string or the string ** "localhost". ^If the authority is not an empty string or "localhost", an ** error is returned to the caller. ^The fragment component of a URI, if ** present, is ignored. ** ** ^SQLite uses the path component of the URI as the name of the disk file ** which contains the database. ^If the path begins with a '/' character, ** then it is interpreted as an absolute path. ^If the path does not begin ** with a '/' (meaning that the authority section is omitted from the URI) ** then the path is interpreted as a relative path. ** ^(On windows, the first component of an absolute path ** is a drive specification (e.g. "C:").)^ ** ** [[core URI query parameters]] ** The query component of a URI may contain parameters that are interpreted ** either by SQLite itself, or by a [VFS | custom VFS implementation]. ** SQLite and its built-in [VFSes] interpret the ** following query parameters: ** **
    **
  • vfs: ^The "vfs" parameter may be used to specify the name of ** a VFS object that provides the operating system interface that should ** be used to access the database file on disk. ^If this option is set to ** an empty string the default VFS object is used. ^Specifying an unknown ** VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is ** present, then the VFS specified by the option takes precedence over ** the value passed as the fourth parameter to sqlite3_open_v2(). ** **
  • mode: ^(The mode parameter may be set to either "ro", "rw", ** "rwc", or "memory". Attempting to set it to any other value is ** an error)^. ** ^If "ro" is specified, then the database is opened for read-only ** access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the ** third argument to sqlite3_open_v2(). ^If the mode option is set to ** "rw", then the database is opened for read-write (but not create) ** access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had ** been set. ^Value "rwc" is equivalent to setting both ** SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If the mode option is ** set to "memory" then a pure [in-memory database] that never reads ** or writes from disk is used. ^It is an error to specify a value for ** the mode parameter that is less restrictive than that specified by ** the flags passed in the third parameter to sqlite3_open_v2(). ** **
  • cache: ^The cache parameter may be set to either "shared" or ** "private". ^Setting it to "shared" is equivalent to setting the ** SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to ** sqlite3_open_v2(). ^Setting the cache parameter to "private" is ** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit. ** ^If sqlite3_open_v2() is used and the "cache" parameter is present in ** a URI filename, its value overrides any behavior requested by setting ** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag. ** **
  • psow: ^The psow parameter indicates whether or not the ** [powersafe overwrite] property does or does not apply to the ** storage media on which the database file resides. ** **
  • nolock: ^The nolock parameter is a boolean query parameter ** which if set disables file locking in rollback journal modes. This ** is useful for accessing a database on a filesystem that does not ** support locking. Caution: Database corruption might result if two ** or more processes write to the same database and any one of those ** processes uses nolock=1. ** **
  • immutable: ^The immutable parameter is a boolean query ** parameter that indicates that the database file is stored on ** read-only media. ^When immutable is set, SQLite assumes that the ** database file cannot be changed, even by a process with higher ** privilege, and so the database is opened read-only and all locking ** and change detection is disabled. Caution: Setting the immutable ** property on a database file that does in fact change can result ** in incorrect query results and/or [SQLITE_CORRUPT] errors. ** See also: [SQLITE_IOCAP_IMMUTABLE]. ** **
** ** ^Specifying an unknown parameter in the query component of a URI is not an ** error. Future versions of SQLite might understand additional query ** parameters. See "[query parameters with special meaning to SQLite]" for ** additional information. ** ** [[URI filename examples]]

URI filename examples

** ** **
URI filenames Results **
file:data.db ** Open the file "data.db" in the current directory. **
file:/home/fred/data.db
** file:///home/fred/data.db
** file://localhost/home/fred/data.db
** Open the database file "/home/fred/data.db". **
file://darkstar/home/fred/data.db ** An error. "darkstar" is not a recognized authority. **
** file:///C:/Documents%20and%20Settings/fred/Desktop/data.db ** Windows only: Open the file "data.db" on fred's desktop on drive ** C:. Note that the %20 escaping in this example is not strictly ** necessary - space characters can be used literally ** in URI filenames. **
file:data.db?mode=ro&cache=private ** Open file "data.db" in the current directory for read-only access. ** Regardless of whether or not shared-cache mode is enabled by ** default, use a private cache. **
file:/home/fred/data.db?vfs=unix-dotfile ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile" ** that uses dot-files in place of posix advisory locking. **
file:data.db?mode=readonly ** An error. "readonly" is not a valid option for the "mode" parameter. ** Use "ro" instead: "file:data.db?mode=ro". **
** ** ^URI hexadecimal escape sequences (%HH) are supported within the path and ** query components of a URI. A hexadecimal escape sequence consists of a ** percent sign - "%" - followed by exactly two hexadecimal digits ** specifying an octet value. ^Before the path or query components of a ** URI filename are interpreted, they are encoded using UTF-8 and all ** hexadecimal escape sequences replaced by a single byte containing the ** corresponding octet. If this process generates an invalid UTF-8 encoding, ** the results are undefined. ** ** Note to Windows users: The encoding used for the filename argument ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever ** codepage is currently defined. Filenames containing international ** characters must be converted to UTF-8 prior to passing them into ** sqlite3_open() or sqlite3_open_v2(). ** ** Note to Windows Runtime users: The temporary directory must be set ** prior to calling sqlite3_open() or sqlite3_open_v2(). Otherwise, various ** features that require the use of temporary files may fail. ** ** See also: [sqlite3_temp_directory] */ SQLITE_API int sqlite3_open( const char *filename, /* Database filename (UTF-8) */ sqlite3 **ppDb /* OUT: SQLite db handle */ ); SQLITE_API int sqlite3_open16( const void *filename, /* Database filename (UTF-16) */ sqlite3 **ppDb /* OUT: SQLite db handle */ ); SQLITE_API int sqlite3_open_v2( const char *filename, /* Database filename (UTF-8) */ sqlite3 **ppDb, /* OUT: SQLite db handle */ int flags, /* Flags */ const char *zVfs /* Name of VFS module to use */ ); /* ** CAPI3REF: Obtain Values For URI Parameters ** ** These are utility routines, useful to [VFS|custom VFS implementations], ** that check if a database file was a URI that contained a specific query ** parameter, and if so obtains the value of that query parameter. ** ** The first parameter to these interfaces (hereafter referred to ** as F) must be one of: **
    **
  • A database filename pointer created by the SQLite core and ** passed into the xOpen() method of a VFS implementation, or **
  • A filename obtained from [sqlite3_db_filename()], or **
  • A new filename constructed using [sqlite3_create_filename()]. **
** If the F parameter is not one of the above, then the behavior is ** undefined and probably undesirable. Older versions of SQLite were ** more tolerant of invalid F parameters than newer versions. ** ** If F is a suitable filename (as described in the previous paragraph) ** and if P is the name of the query parameter, then ** sqlite3_uri_parameter(F,P) returns the value of the P ** parameter if it exists or a NULL pointer if P does not appear as a ** query parameter on F. If P is a query parameter of F and it ** has no explicit value, then sqlite3_uri_parameter(F,P) returns ** a pointer to an empty string. ** ** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean ** parameter and returns true (1) or false (0) according to the value ** of P. The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the ** value of query parameter P is one of "yes", "true", or "on" in any ** case or if the value begins with a non-zero number. The ** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of ** query parameter P is one of "no", "false", or "off" in any case or ** if the value begins with a numeric zero. If P is not a query ** parameter on F or if the value of P does not match any of the ** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0). ** ** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a ** 64-bit signed integer and returns that integer, or D if P does not ** exist. If the value of P is something other than an integer, then ** zero is returned. ** ** The sqlite3_uri_key(F,N) returns a pointer to the name (not ** the value) of the N-th query parameter for filename F, or a NULL ** pointer if N is less than zero or greater than the number of query ** parameters minus 1. The N value is zero-based so N should be 0 to obtain ** the name of the first query parameter, 1 for the second parameter, and ** so forth. ** ** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and ** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and ** is not a database file pathname pointer that the SQLite core passed ** into the xOpen VFS method, then the behavior of this routine is undefined ** and probably undesirable. ** ** Beginning with SQLite [version 3.31.0] ([dateof:3.31.0]) the input F ** parameter can also be the name of a rollback journal file or WAL file ** in addition to the main database file. Prior to version 3.31.0, these ** routines would only work if F was the name of the main database file. ** When the F parameter is the name of the rollback journal or WAL file, ** it has access to all the same query parameters as were found on the ** main database file. ** ** See the [URI filename] documentation for additional information. */ SQLITE_API const char *sqlite3_uri_parameter(sqlite3_filename z, const char *zParam); SQLITE_API int sqlite3_uri_boolean(sqlite3_filename z, const char *zParam, int bDefault); SQLITE_API sqlite3_int64 sqlite3_uri_int64(sqlite3_filename, const char*, sqlite3_int64); SQLITE_API const char *sqlite3_uri_key(sqlite3_filename z, int N); /* ** CAPI3REF: Translate filenames ** ** These routines are available to [VFS|custom VFS implementations] for ** translating filenames between the main database file, the journal file, ** and the WAL file. ** ** If F is the name of an sqlite database file, journal file, or WAL file ** passed by the SQLite core into the VFS, then sqlite3_filename_database(F) ** returns the name of the corresponding database file. ** ** If F is the name of an sqlite database file, journal file, or WAL file ** passed by the SQLite core into the VFS, or if F is a database filename ** obtained from [sqlite3_db_filename()], then sqlite3_filename_journal(F) ** returns the name of the corresponding rollback journal file. ** ** If F is the name of an sqlite database file, journal file, or WAL file ** that was passed by the SQLite core into the VFS, or if F is a database ** filename obtained from [sqlite3_db_filename()], then ** sqlite3_filename_wal(F) returns the name of the corresponding ** WAL file. ** ** In all of the above, if F is not the name of a database, journal or WAL ** filename passed into the VFS from the SQLite core and F is not the ** return value from [sqlite3_db_filename()], then the result is ** undefined and is likely a memory access violation. */ SQLITE_API const char *sqlite3_filename_database(sqlite3_filename); SQLITE_API const char *sqlite3_filename_journal(sqlite3_filename); SQLITE_API const char *sqlite3_filename_wal(sqlite3_filename); /* ** CAPI3REF: Database File Corresponding To A Journal ** ** ^If X is the name of a rollback or WAL-mode journal file that is ** passed into the xOpen method of [sqlite3_vfs], then ** sqlite3_database_file_object(X) returns a pointer to the [sqlite3_file] ** object that represents the main database file. ** ** This routine is intended for use in custom [VFS] implementations ** only. It is not a general-purpose interface. ** The argument sqlite3_file_object(X) must be a filename pointer that ** has been passed into [sqlite3_vfs].xOpen method where the ** flags parameter to xOpen contains one of the bits ** [SQLITE_OPEN_MAIN_JOURNAL] or [SQLITE_OPEN_WAL]. Any other use ** of this routine results in undefined and probably undesirable ** behavior. */ SQLITE_API sqlite3_file *sqlite3_database_file_object(const char*); /* ** CAPI3REF: Create and Destroy VFS Filenames ** ** These interfaces are provided for use by [VFS shim] implementations and ** are not useful outside of that context. ** ** The sqlite3_create_filename(D,J,W,N,P) allocates memory to hold a version of ** database filename D with corresponding journal file J and WAL file W and ** an array P of N URI Key/Value pairs. The result from ** sqlite3_create_filename(D,J,W,N,P) is a pointer to a database filename that ** is safe to pass to routines like: **
    **
  • [sqlite3_uri_parameter()], **
  • [sqlite3_uri_boolean()], **
  • [sqlite3_uri_int64()], **
  • [sqlite3_uri_key()], **
  • [sqlite3_filename_database()], **
  • [sqlite3_filename_journal()], or **
  • [sqlite3_filename_wal()]. **
** If a memory allocation error occurs, sqlite3_create_filename() might ** return a NULL pointer. The memory obtained from sqlite3_create_filename(X) ** must be released by a corresponding call to sqlite3_free_filename(Y). ** ** The P parameter in sqlite3_create_filename(D,J,W,N,P) should be an array ** of 2*N pointers to strings. Each pair of pointers in this array corresponds ** to a key and value for a query parameter. The P parameter may be a NULL ** pointer if N is zero. None of the 2*N pointers in the P array may be ** NULL pointers and key pointers should not be empty strings. ** None of the D, J, or W parameters to sqlite3_create_filename(D,J,W,N,P) may ** be NULL pointers, though they can be empty strings. ** ** The sqlite3_free_filename(Y) routine releases a memory allocation ** previously obtained from sqlite3_create_filename(). Invoking ** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op. ** ** If the Y parameter to sqlite3_free_filename(Y) is anything other ** than a NULL pointer or a pointer previously acquired from ** sqlite3_create_filename(), then bad things such as heap ** corruption or segfaults may occur. The value Y should not be ** used again after sqlite3_free_filename(Y) has been called. This means ** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y, ** then the corresponding [sqlite3_module.xClose() method should also be ** invoked prior to calling sqlite3_free_filename(Y). */ SQLITE_API sqlite3_filename sqlite3_create_filename( const char *zDatabase, const char *zJournal, const char *zWal, int nParam, const char **azParam ); SQLITE_API void sqlite3_free_filename(sqlite3_filename); /* ** CAPI3REF: Error Codes And Messages ** METHOD: sqlite3 ** ** ^If the most recent sqlite3_* API call associated with ** [database connection] D failed, then the sqlite3_errcode(D) interface ** returns the numeric [result code] or [extended result code] for that ** API call. ** ^The sqlite3_extended_errcode() ** interface is the same except that it always returns the ** [extended result code] even when extended result codes are ** disabled. ** ** The values returned by sqlite3_errcode() and/or ** sqlite3_extended_errcode() might change with each API call. ** Except, there are some interfaces that are guaranteed to never ** change the value of the error code. The error-code preserving ** interfaces include the following: ** **
    **
  • sqlite3_errcode() **
  • sqlite3_extended_errcode() **
  • sqlite3_errmsg() **
  • sqlite3_errmsg16() **
  • sqlite3_error_offset() **
** ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language ** text that describes the error, as either UTF-8 or UTF-16 respectively, ** or NULL if no error message is available. ** (See how SQLite handles [invalid UTF] for exceptions to this rule.) ** ^(Memory to hold the error message string is managed internally. ** The application does not need to worry about freeing the result. ** However, the error string might be overwritten or deallocated by ** subsequent calls to other SQLite interface functions.)^ ** ** ^The sqlite3_errstr(E) interface returns the English-language text ** that describes the [result code] E, as UTF-8, or NULL if E is not an ** result code for which a text error message is available. ** ^(Memory to hold the error message string is managed internally ** and must not be freed by the application)^. ** ** ^If the most recent error references a specific token in the input ** SQL, the sqlite3_error_offset() interface returns the byte offset ** of the start of that token. ^The byte offset returned by ** sqlite3_error_offset() assumes that the input SQL is UTF8. ** ^If the most recent error does not reference a specific token in the input ** SQL, then the sqlite3_error_offset() function returns -1. ** ** When the serialized [threading mode] is in use, it might be the ** case that a second error occurs on a separate thread in between ** the time of the first error and the call to these interfaces. ** When that happens, the second error will be reported since these ** interfaces always report the most recent result. To avoid ** this, each thread can obtain exclusive use of the [database connection] D ** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning ** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after ** all calls to the interfaces listed here are completed. ** ** If an interface fails with SQLITE_MISUSE, that means the interface ** was invoked incorrectly by the application. In that case, the ** error code and message may or may not be set. */ SQLITE_API int sqlite3_errcode(sqlite3 *db); SQLITE_API int sqlite3_extended_errcode(sqlite3 *db); SQLITE_API const char *sqlite3_errmsg(sqlite3*); SQLITE_API const void *sqlite3_errmsg16(sqlite3*); SQLITE_API const char *sqlite3_errstr(int); SQLITE_API int sqlite3_error_offset(sqlite3 *db); /* ** CAPI3REF: Prepared Statement Object ** KEYWORDS: {prepared statement} {prepared statements} ** ** An instance of this object represents a single SQL statement that ** has been compiled into binary form and is ready to be evaluated. ** ** Think of each SQL statement as a separate computer program. The ** original SQL text is source code. A prepared statement object ** is the compiled object code. All SQL must be converted into a ** prepared statement before it can be run. ** ** The life-cycle of a prepared statement object usually goes like this: ** **
    **
  1. Create the prepared statement object using [sqlite3_prepare_v2()]. **
  2. Bind values to [parameters] using the sqlite3_bind_*() ** interfaces. **
  3. Run the SQL by calling [sqlite3_step()] one or more times. **
  4. Reset the prepared statement using [sqlite3_reset()] then go back ** to step 2. Do this zero or more times. **
  5. Destroy the object using [sqlite3_finalize()]. **
*/ typedef struct sqlite3_stmt sqlite3_stmt; /* ** CAPI3REF: Run-time Limits ** METHOD: sqlite3 ** ** ^(This interface allows the size of various constructs to be limited ** on a connection by connection basis. The first parameter is the ** [database connection] whose limit is to be set or queried. The ** second parameter is one of the [limit categories] that define a ** class of constructs to be size limited. The third parameter is the ** new limit for that construct.)^ ** ** ^If the new limit is a negative number, the limit is unchanged. ** ^(For each limit category SQLITE_LIMIT_NAME there is a ** [limits | hard upper bound] ** set at compile-time by a C preprocessor macro called ** [limits | SQLITE_MAX_NAME]. ** (The "_LIMIT_" in the name is changed to "_MAX_".))^ ** ^Attempts to increase a limit above its hard upper bound are ** silently truncated to the hard upper bound. ** ** ^Regardless of whether or not the limit was changed, the ** [sqlite3_limit()] interface returns the prior value of the limit. ** ^Hence, to find the current value of a limit without changing it, ** simply invoke this interface with the third parameter set to -1. ** ** Run-time limits are intended for use in applications that manage ** both their own internal database and also databases that are controlled ** by untrusted external sources. An example application might be a ** web browser that has its own databases for storing history and ** separate databases controlled by JavaScript applications downloaded ** off the Internet. The internal databases can be given the ** large, default limits. Databases managed by external sources can ** be given much smaller limits designed to prevent a denial of service ** attack. Developers might also want to use the [sqlite3_set_authorizer()] ** interface to further control untrusted SQL. The size of the database ** created by an untrusted script can be contained using the ** [max_page_count] [PRAGMA]. ** ** New run-time limit categories may be added in future releases. */ SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal); /* ** CAPI3REF: Run-Time Limit Categories ** KEYWORDS: {limit category} {*limit categories} ** ** These constants define various performance limits ** that can be lowered at run-time using [sqlite3_limit()]. ** The synopsis of the meanings of the various limits is shown below. ** Additional information is available at [limits | Limits in SQLite]. ** **
** [[SQLITE_LIMIT_LENGTH]] ^(
SQLITE_LIMIT_LENGTH
**
The maximum size of any string or BLOB or table row, in bytes.
)^ ** ** [[SQLITE_LIMIT_SQL_LENGTH]] ^(
SQLITE_LIMIT_SQL_LENGTH
**
The maximum length of an SQL statement, in bytes.
)^ ** ** [[SQLITE_LIMIT_COLUMN]] ^(
SQLITE_LIMIT_COLUMN
**
The maximum number of columns in a table definition or in the ** result set of a [SELECT] or the maximum number of columns in an index ** or in an ORDER BY or GROUP BY clause.
)^ ** ** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(
SQLITE_LIMIT_EXPR_DEPTH
**
The maximum depth of the parse tree on any expression.
)^ ** ** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(
SQLITE_LIMIT_COMPOUND_SELECT
**
The maximum number of terms in a compound SELECT statement.
)^ ** ** [[SQLITE_LIMIT_VDBE_OP]] ^(
SQLITE_LIMIT_VDBE_OP
**
The maximum number of instructions in a virtual machine program ** used to implement an SQL statement. If [sqlite3_prepare_v2()] or ** the equivalent tries to allocate space for more than this many opcodes ** in a single prepared statement, an SQLITE_NOMEM error is returned.
)^ ** ** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(
SQLITE_LIMIT_FUNCTION_ARG
**
The maximum number of arguments on a function.
)^ ** ** [[SQLITE_LIMIT_ATTACHED]] ^(
SQLITE_LIMIT_ATTACHED
**
The maximum number of [ATTACH | attached databases].)^
** ** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]] ** ^(
SQLITE_LIMIT_LIKE_PATTERN_LENGTH
**
The maximum length of the pattern argument to the [LIKE] or ** [GLOB] operators.
)^ ** ** [[SQLITE_LIMIT_VARIABLE_NUMBER]] ** ^(
SQLITE_LIMIT_VARIABLE_NUMBER
**
The maximum index number of any [parameter] in an SQL statement.)^ ** ** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(
SQLITE_LIMIT_TRIGGER_DEPTH
**
The maximum depth of recursion for triggers.
)^ ** ** [[SQLITE_LIMIT_WORKER_THREADS]] ^(
SQLITE_LIMIT_WORKER_THREADS
**
The maximum number of auxiliary worker threads that a single ** [prepared statement] may start.
)^ **
*/ #define SQLITE_LIMIT_LENGTH 0 #define SQLITE_LIMIT_SQL_LENGTH 1 #define SQLITE_LIMIT_COLUMN 2 #define SQLITE_LIMIT_EXPR_DEPTH 3 #define SQLITE_LIMIT_COMPOUND_SELECT 4 #define SQLITE_LIMIT_VDBE_OP 5 #define SQLITE_LIMIT_FUNCTION_ARG 6 #define SQLITE_LIMIT_ATTACHED 7 #define SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8 #define SQLITE_LIMIT_VARIABLE_NUMBER 9 #define SQLITE_LIMIT_TRIGGER_DEPTH 10 #define SQLITE_LIMIT_WORKER_THREADS 11 /* ** CAPI3REF: Prepare Flags ** ** These constants define various flags that can be passed into ** "prepFlags" parameter of the [sqlite3_prepare_v3()] and ** [sqlite3_prepare16_v3()] interfaces. ** ** New flags may be added in future releases of SQLite. ** **
** [[SQLITE_PREPARE_PERSISTENT]] ^(
SQLITE_PREPARE_PERSISTENT
**
The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner ** that the prepared statement will be retained for a long time and ** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()] ** and [sqlite3_prepare16_v3()] assume that the prepared statement will ** be used just once or at most a few times and then destroyed using ** [sqlite3_finalize()] relatively soon. The current implementation acts ** on this hint by avoiding the use of [lookaside memory] so as not to ** deplete the limited store of lookaside memory. Future versions of ** SQLite may act on this hint differently. ** ** [[SQLITE_PREPARE_NORMALIZE]]
SQLITE_PREPARE_NORMALIZE
**
The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used ** to be required for any prepared statement that wanted to use the ** [sqlite3_normalized_sql()] interface. However, the ** [sqlite3_normalized_sql()] interface is now available to all ** prepared statements, regardless of whether or not they use this ** flag. ** ** [[SQLITE_PREPARE_NO_VTAB]]
SQLITE_PREPARE_NO_VTAB
**
The SQLITE_PREPARE_NO_VTAB flag causes the SQL compiler ** to return an error (error code SQLITE_ERROR) if the statement uses ** any virtual tables. ** ** [[SQLITE_PREPARE_DONT_LOG]]
SQLITE_PREPARE_DONT_LOG
**
The SQLITE_PREPARE_DONT_LOG flag prevents SQL compiler ** errors from being sent to the error log defined by ** [SQLITE_CONFIG_LOG]. This can be used, for example, to do test ** compiles to see if some SQL syntax is well-formed, without generating ** messages on the global error log when it is not. If the test compile ** fails, the sqlite3_prepare_v3() call returns the same error indications ** with or without this flag; it just omits the call to [sqlite3_log()] that ** logs the error. **
*/ #define SQLITE_PREPARE_PERSISTENT 0x01 #define SQLITE_PREPARE_NORMALIZE 0x02 #define SQLITE_PREPARE_NO_VTAB 0x04 #define SQLITE_PREPARE_DONT_LOG 0x10 /* ** CAPI3REF: Compiling An SQL Statement ** KEYWORDS: {SQL statement compiler} ** METHOD: sqlite3 ** CONSTRUCTOR: sqlite3_stmt ** ** To execute an SQL statement, it must first be compiled into a byte-code ** program using one of these routines. Or, in other words, these routines ** are constructors for the [prepared statement] object. ** ** The preferred routine to use is [sqlite3_prepare_v2()]. The ** [sqlite3_prepare()] interface is legacy and should be avoided. ** [sqlite3_prepare_v3()] has an extra "prepFlags" option that is used ** for special purposes. ** ** The use of the UTF-8 interfaces is preferred, as SQLite currently ** does all parsing using UTF-8. The UTF-16 interfaces are provided ** as a convenience. The UTF-16 interfaces work by converting the ** input text into UTF-8, then invoking the corresponding UTF-8 interface. ** ** The first argument, "db", is a [database connection] obtained from a ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or ** [sqlite3_open16()]. The database connection must not have been closed. ** ** The second argument, "zSql", is the statement to be compiled, encoded ** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(), ** and sqlite3_prepare_v3() ** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(), ** and sqlite3_prepare16_v3() use UTF-16. ** ** ^If the nByte argument is negative, then zSql is read up to the ** first zero terminator. ^If nByte is positive, then it is the maximum ** number of bytes read from zSql. When nByte is positive, zSql is read ** up to the first zero terminator or until the nByte bytes have been read, ** whichever comes first. ^If nByte is zero, then no prepared ** statement is generated. ** If the caller knows that the supplied string is nul-terminated, then ** there is a small performance advantage to passing an nByte parameter that ** is the number of bytes in the input string including ** the nul-terminator. ** Note that nByte measure the length of the input in bytes, not ** characters, even for the UTF-16 interfaces. ** ** ^If pzTail is not NULL then *pzTail is made to point to the first byte ** past the end of the first SQL statement in zSql. These routines only ** compile the first statement in zSql, so *pzTail is left pointing to ** what remains uncompiled. ** ** ^*ppStmt is left pointing to a compiled [prepared statement] that can be ** executed using [sqlite3_step()]. ^If there is an error, *ppStmt is set ** to NULL. ^If the input text contains no SQL (if the input is an empty ** string or a comment) then *ppStmt is set to NULL. ** The calling procedure is responsible for deleting the compiled ** SQL statement using [sqlite3_finalize()] after it has finished with it. ** ppStmt may not be NULL. ** ** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK]; ** otherwise an [error code] is returned. ** ** The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(), ** and sqlite3_prepare16_v3() interfaces are recommended for all new programs. ** The older interfaces (sqlite3_prepare() and sqlite3_prepare16()) ** are retained for backwards compatibility, but their use is discouraged. ** ^In the "vX" interfaces, the prepared statement ** that is returned (the [sqlite3_stmt] object) contains a copy of the ** original SQL text. This causes the [sqlite3_step()] interface to ** behave differently in three ways: ** **
    **
  1. ** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it ** always used to do, [sqlite3_step()] will automatically recompile the SQL ** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY] ** retries will occur before sqlite3_step() gives up and returns an error. **
  2. ** **
  3. ** ^When an error occurs, [sqlite3_step()] will return one of the detailed ** [error codes] or [extended error codes]. ^The legacy behavior was that ** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code ** and the application would have to make a second call to [sqlite3_reset()] ** in order to find the underlying cause of the problem. With the "v2" prepare ** interfaces, the underlying reason for the error is returned immediately. **
  4. ** **
  5. ** ^If the specific value bound to a [parameter | host parameter] in the ** WHERE clause might influence the choice of query plan for a statement, ** then the statement will be automatically recompiled, as if there had been ** a schema change, on the first [sqlite3_step()] call following any change ** to the [sqlite3_bind_text | bindings] of that [parameter]. ** ^The specific value of a WHERE-clause [parameter] might influence the ** choice of query plan if the parameter is the left-hand side of a [LIKE] ** or [GLOB] operator or if the parameter is compared to an indexed column ** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled. **
  6. **
** **

^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having ** the extra prepFlags parameter, which is a bit array consisting of zero or ** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The ** sqlite3_prepare_v2() interface works exactly the same as ** sqlite3_prepare_v3() with a zero prepFlags parameter. */ SQLITE_API int sqlite3_prepare( sqlite3 *db, /* Database handle */ const char *zSql, /* SQL statement, UTF-8 encoded */ int nByte, /* Maximum length of zSql in bytes. */ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const char **pzTail /* OUT: Pointer to unused portion of zSql */ ); SQLITE_API int sqlite3_prepare_v2( sqlite3 *db, /* Database handle */ const char *zSql, /* SQL statement, UTF-8 encoded */ int nByte, /* Maximum length of zSql in bytes. */ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const char **pzTail /* OUT: Pointer to unused portion of zSql */ ); SQLITE_API int sqlite3_prepare_v3( sqlite3 *db, /* Database handle */ const char *zSql, /* SQL statement, UTF-8 encoded */ int nByte, /* Maximum length of zSql in bytes. */ unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const char **pzTail /* OUT: Pointer to unused portion of zSql */ ); SQLITE_API int sqlite3_prepare16( sqlite3 *db, /* Database handle */ const void *zSql, /* SQL statement, UTF-16 encoded */ int nByte, /* Maximum length of zSql in bytes. */ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const void **pzTail /* OUT: Pointer to unused portion of zSql */ ); SQLITE_API int sqlite3_prepare16_v2( sqlite3 *db, /* Database handle */ const void *zSql, /* SQL statement, UTF-16 encoded */ int nByte, /* Maximum length of zSql in bytes. */ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const void **pzTail /* OUT: Pointer to unused portion of zSql */ ); SQLITE_API int sqlite3_prepare16_v3( sqlite3 *db, /* Database handle */ const void *zSql, /* SQL statement, UTF-16 encoded */ int nByte, /* Maximum length of zSql in bytes. */ unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const void **pzTail /* OUT: Pointer to unused portion of zSql */ ); /* ** CAPI3REF: Retrieving Statement SQL ** METHOD: sqlite3_stmt ** ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8 ** SQL text used to create [prepared statement] P if P was ** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], ** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()]. ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8 ** string containing the SQL text of prepared statement P with ** [bound parameters] expanded. ** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8 ** string containing the normalized SQL text of prepared statement P. The ** semantics used to normalize a SQL statement are unspecified and subject ** to change. At a minimum, literal values will be replaced with suitable ** placeholders. ** ** ^(For example, if a prepared statement is created using the SQL ** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345 ** and parameter :xyz is unbound, then sqlite3_sql() will return ** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql() ** will return "SELECT 2345,NULL".)^ ** ** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory ** is available to hold the result, or if the result would exceed the ** the maximum string length determined by the [SQLITE_LIMIT_LENGTH]. ** ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of ** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time ** option causes sqlite3_expanded_sql() to always return NULL. ** ** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P) ** are managed by SQLite and are automatically freed when the prepared ** statement is finalized. ** ^The string returned by sqlite3_expanded_sql(P), on the other hand, ** is obtained from [sqlite3_malloc()] and must be freed by the application ** by passing it to [sqlite3_free()]. ** ** ^The sqlite3_normalized_sql() interface is only available if ** the [SQLITE_ENABLE_NORMALIZE] compile-time option is defined. */ SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt); SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt); #ifdef SQLITE_ENABLE_NORMALIZE SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt); #endif /* ** CAPI3REF: Determine If An SQL Statement Writes The Database ** METHOD: sqlite3_stmt ** ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if ** and only if the [prepared statement] X makes no direct changes to ** the content of the database file. ** ** Note that [application-defined SQL functions] or ** [virtual tables] might change the database indirectly as a side effect. ** ^(For example, if an application defines a function "eval()" that ** calls [sqlite3_exec()], then the following SQL statement would ** change the database file through side-effects: ** **

**    SELECT eval('DELETE FROM t1') FROM t2;
** 
** ** But because the [SELECT] statement does not change the database file ** directly, sqlite3_stmt_readonly() would still return true.)^ ** ** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK], ** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true, ** since the statements themselves do not actually modify the database but ** rather they control the timing of when other statements modify the ** database. ^The [ATTACH] and [DETACH] statements also cause ** sqlite3_stmt_readonly() to return true since, while those statements ** change the configuration of a database connection, they do not make ** changes to the content of the database files on disk. ** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since ** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and ** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so ** sqlite3_stmt_readonly() returns false for those commands. ** ** ^This routine returns false if there is any possibility that the ** statement might change the database file. ^A false return does ** not guarantee that the statement will change the database file. ** ^For example, an UPDATE statement might have a WHERE clause that ** makes it a no-op, but the sqlite3_stmt_readonly() result would still ** be false. ^Similarly, a CREATE TABLE IF NOT EXISTS statement is a ** read-only no-op if the table already exists, but ** sqlite3_stmt_readonly() still returns false for such a statement. ** ** ^If prepared statement X is an [EXPLAIN] or [EXPLAIN QUERY PLAN] ** statement, then sqlite3_stmt_readonly(X) returns the same value as ** if the EXPLAIN or EXPLAIN QUERY PLAN prefix were omitted. */ SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt); /* ** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement ** METHOD: sqlite3_stmt ** ** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the ** prepared statement S is an EXPLAIN statement, or 2 if the ** statement S is an EXPLAIN QUERY PLAN. ** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is ** an ordinary statement or a NULL pointer. */ SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt); /* ** CAPI3REF: Change The EXPLAIN Setting For A Prepared Statement ** METHOD: sqlite3_stmt ** ** The sqlite3_stmt_explain(S,E) interface changes the EXPLAIN ** setting for [prepared statement] S. If E is zero, then S becomes ** a normal prepared statement. If E is 1, then S behaves as if ** its SQL text began with "[EXPLAIN]". If E is 2, then S behaves as if ** its SQL text began with "[EXPLAIN QUERY PLAN]". ** ** Calling sqlite3_stmt_explain(S,E) might cause S to be reprepared. ** SQLite tries to avoid a reprepare, but a reprepare might be necessary ** on the first transition into EXPLAIN or EXPLAIN QUERY PLAN mode. ** ** Because of the potential need to reprepare, a call to ** sqlite3_stmt_explain(S,E) will fail with SQLITE_ERROR if S cannot be ** reprepared because it was created using [sqlite3_prepare()] instead of ** the newer [sqlite3_prepare_v2()] or [sqlite3_prepare_v3()] interfaces and ** hence has no saved SQL text with which to reprepare. ** ** Changing the explain setting for a prepared statement does not change ** the original SQL text for the statement. Hence, if the SQL text originally ** began with EXPLAIN or EXPLAIN QUERY PLAN, but sqlite3_stmt_explain(S,0) ** is called to convert the statement into an ordinary statement, the EXPLAIN ** or EXPLAIN QUERY PLAN keywords will still appear in the sqlite3_sql(S) ** output, even though the statement now acts like a normal SQL statement. ** ** This routine returns SQLITE_OK if the explain mode is successfully ** changed, or an error code if the explain mode could not be changed. ** The explain mode cannot be changed while a statement is active. ** Hence, it is good practice to call [sqlite3_reset(S)] ** immediately prior to calling sqlite3_stmt_explain(S,E). */ SQLITE_API int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode); /* ** CAPI3REF: Determine If A Prepared Statement Has Been Reset ** METHOD: sqlite3_stmt ** ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the ** [prepared statement] S has been stepped at least once using ** [sqlite3_step(S)] but has neither run to completion (returned ** [SQLITE_DONE] from [sqlite3_step(S)]) nor ** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S) ** interface returns false if S is a NULL pointer. If S is not a ** NULL pointer and is not a pointer to a valid [prepared statement] ** object, then the behavior is undefined and probably undesirable. ** ** This interface can be used in combination [sqlite3_next_stmt()] ** to locate all prepared statements associated with a database ** connection that are in need of being reset. This can be used, ** for example, in diagnostic routines to search for prepared ** statements that are holding a transaction open. */ SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*); /* ** CAPI3REF: Dynamically Typed Value Object ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value} ** ** SQLite uses the sqlite3_value object to represent all values ** that can be stored in a database table. SQLite uses dynamic typing ** for the values it stores. ^Values stored in sqlite3_value objects ** can be integers, floating point values, strings, BLOBs, or NULL. ** ** An sqlite3_value object may be either "protected" or "unprotected". ** Some interfaces require a protected sqlite3_value. Other interfaces ** will accept either a protected or an unprotected sqlite3_value. ** Every interface that accepts sqlite3_value arguments specifies ** whether or not it requires a protected sqlite3_value. The ** [sqlite3_value_dup()] interface can be used to construct a new ** protected sqlite3_value from an unprotected sqlite3_value. ** ** The terms "protected" and "unprotected" refer to whether or not ** a mutex is held. An internal mutex is held for a protected ** sqlite3_value object but no mutex is held for an unprotected ** sqlite3_value object. If SQLite is compiled to be single-threaded ** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0) ** or if SQLite is run in one of reduced mutex modes ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD] ** then there is no distinction between protected and unprotected ** sqlite3_value objects and they can be used interchangeably. However, ** for maximum code portability it is recommended that applications ** still make the distinction between protected and unprotected ** sqlite3_value objects even when not strictly required. ** ** ^The sqlite3_value objects that are passed as parameters into the ** implementation of [application-defined SQL functions] are protected. ** ^The sqlite3_value objects returned by [sqlite3_vtab_rhs_value()] ** are protected. ** ^The sqlite3_value object returned by ** [sqlite3_column_value()] is unprotected. ** Unprotected sqlite3_value objects may only be used as arguments ** to [sqlite3_result_value()], [sqlite3_bind_value()], and ** [sqlite3_value_dup()]. ** The [sqlite3_value_blob | sqlite3_value_type()] family of ** interfaces require protected sqlite3_value objects. */ typedef struct sqlite3_value sqlite3_value; /* ** CAPI3REF: SQL Function Context Object ** ** The context in which an SQL function executes is stored in an ** sqlite3_context object. ^A pointer to an sqlite3_context object ** is always first parameter to [application-defined SQL functions]. ** The application-defined SQL function implementation will pass this ** pointer through into calls to [sqlite3_result_int | sqlite3_result()], ** [sqlite3_aggregate_context()], [sqlite3_user_data()], ** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()], ** and/or [sqlite3_set_auxdata()]. */ typedef struct sqlite3_context sqlite3_context; /* ** CAPI3REF: Binding Values To Prepared Statements ** KEYWORDS: {host parameter} {host parameters} {host parameter name} ** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding} ** METHOD: sqlite3_stmt ** ** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants, ** literals may be replaced by a [parameter] that matches one of the following ** templates: ** **
    **
  • ? **
  • ?NNN **
  • :VVV **
  • @VVV **
  • $VVV **
** ** In the templates above, NNN represents an integer literal, ** and VVV represents an alphanumeric identifier.)^ ^The values of these ** parameters (also called "host parameter names" or "SQL parameters") ** can be set using the sqlite3_bind_*() routines defined here. ** ** ^The first argument to the sqlite3_bind_*() routines is always ** a pointer to the [sqlite3_stmt] object returned from ** [sqlite3_prepare_v2()] or its variants. ** ** ^The second argument is the index of the SQL parameter to be set. ** ^The leftmost SQL parameter has an index of 1. ^When the same named ** SQL parameter is used more than once, second and subsequent ** occurrences have the same index as the first occurrence. ** ^The index for named parameters can be looked up using the ** [sqlite3_bind_parameter_index()] API if desired. ^The index ** for "?NNN" parameters is the value of NNN. ** ^The NNN value must be between 1 and the [sqlite3_limit()] ** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 32766). ** ** ^The third argument is the value to bind to the parameter. ** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16() ** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter ** is ignored and the end result is the same as sqlite3_bind_null(). ** ^If the third parameter to sqlite3_bind_text() is not NULL, then ** it should be a pointer to well-formed UTF8 text. ** ^If the third parameter to sqlite3_bind_text16() is not NULL, then ** it should be a pointer to well-formed UTF16 text. ** ^If the third parameter to sqlite3_bind_text64() is not NULL, then ** it should be a pointer to a well-formed unicode string that is ** either UTF8 if the sixth parameter is SQLITE_UTF8, or UTF16 ** otherwise. ** ** [[byte-order determination rules]] ^The byte-order of ** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF) ** found in the first character, which is removed, or in the absence of a BOM ** the byte order is the native byte order of the host ** machine for sqlite3_bind_text16() or the byte order specified in ** the 6th parameter for sqlite3_bind_text64().)^ ** ^If UTF16 input text contains invalid unicode ** characters, then SQLite might change those invalid characters ** into the unicode replacement character: U+FFFD. ** ** ^(In those routines that have a fourth argument, its value is the ** number of bytes in the parameter. To be clear: the value is the ** number of bytes in the value, not the number of characters.)^ ** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16() ** is negative, then the length of the string is ** the number of bytes up to the first zero terminator. ** If the fourth parameter to sqlite3_bind_blob() is negative, then ** the behavior is undefined. ** If a non-negative fourth parameter is provided to sqlite3_bind_text() ** or sqlite3_bind_text16() or sqlite3_bind_text64() then ** that parameter must be the byte offset ** where the NUL terminator would occur assuming the string were NUL ** terminated. If any NUL characters occur at byte offsets less than ** the value of the fourth parameter then the resulting string value will ** contain embedded NULs. The result of expressions involving strings ** with embedded NULs is undefined. ** ** ^The fifth argument to the BLOB and string binding interfaces controls ** or indicates the lifetime of the object referenced by the third parameter. ** These three options exist: ** ^ (1) A destructor to dispose of the BLOB or string after SQLite has finished ** with it may be passed. ^It is called to dispose of the BLOB or string even ** if the call to the bind API fails, except the destructor is not called if ** the third parameter is a NULL pointer or the fourth parameter is negative. ** ^ (2) The special constant, [SQLITE_STATIC], may be passed to indicate that ** the application remains responsible for disposing of the object. ^In this ** case, the object and the provided pointer to it must remain valid until ** either the prepared statement is finalized or the same SQL parameter is ** bound to something else, whichever occurs sooner. ** ^ (3) The constant, [SQLITE_TRANSIENT], may be passed to indicate that the ** object is to be copied prior to the return from sqlite3_bind_*(). ^The ** object and pointer to it must remain valid until then. ^SQLite will then ** manage the lifetime of its private copy. ** ** ^The sixth argument to sqlite3_bind_text64() must be one of ** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE] ** to specify the encoding of the text in the third parameter. If ** the sixth argument to sqlite3_bind_text64() is not one of the ** allowed values shown above, or if the text encoding is different ** from the encoding specified by the sixth parameter, then the behavior ** is undefined. ** ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that ** is filled with zeroes. ^A zeroblob uses a fixed amount of memory ** (just an integer to hold its size) while it is being processed. ** Zeroblobs are intended to serve as placeholders for BLOBs whose ** content is later written using ** [sqlite3_blob_open | incremental BLOB I/O] routines. ** ^A negative value for the zeroblob results in a zero-length BLOB. ** ** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in ** [prepared statement] S to have an SQL value of NULL, but to also be ** associated with the pointer P of type T. ^D is either a NULL pointer or ** a pointer to a destructor function for P. ^SQLite will invoke the ** destructor D with a single argument of P when it is finished using ** P. The T parameter should be a static string, preferably a string ** literal. The sqlite3_bind_pointer() routine is part of the ** [pointer passing interface] added for SQLite 3.20.0. ** ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer ** for the [prepared statement] or with a prepared statement for which ** [sqlite3_step()] has been called more recently than [sqlite3_reset()], ** then the call will return [SQLITE_MISUSE]. If any sqlite3_bind_() ** routine is passed a [prepared statement] that has been finalized, the ** result is undefined and probably harmful. ** ** ^Bindings are not cleared by the [sqlite3_reset()] routine. ** ^Unbound parameters are interpreted as NULL. ** ** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an ** [error code] if anything goes wrong. ** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB ** exceeds limits imposed by [sqlite3_limit]([SQLITE_LIMIT_LENGTH]) or ** [SQLITE_MAX_LENGTH]. ** ^[SQLITE_RANGE] is returned if the parameter ** index is out of range. ^[SQLITE_NOMEM] is returned if malloc() fails. ** ** See also: [sqlite3_bind_parameter_count()], ** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()]. */ SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); SQLITE_API int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64, void(*)(void*)); SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double); SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int); SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64); SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int); SQLITE_API int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*)); SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*)); SQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64, void(*)(void*), unsigned char encoding); SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); SQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*)); SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64); /* ** CAPI3REF: Number Of SQL Parameters ** METHOD: sqlite3_stmt ** ** ^This routine can be used to find the number of [SQL parameters] ** in a [prepared statement]. SQL parameters are tokens of the ** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as ** placeholders for values that are [sqlite3_bind_blob | bound] ** to the parameters at a later time. ** ** ^(This routine actually returns the index of the largest (rightmost) ** parameter. For all forms except ?NNN, this will correspond to the ** number of unique parameters. If parameters of the ?NNN form are used, ** there may be gaps in the list.)^ ** ** See also: [sqlite3_bind_blob|sqlite3_bind()], ** [sqlite3_bind_parameter_name()], and ** [sqlite3_bind_parameter_index()]. */ SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*); /* ** CAPI3REF: Name Of A Host Parameter ** METHOD: sqlite3_stmt ** ** ^The sqlite3_bind_parameter_name(P,N) interface returns ** the name of the N-th [SQL parameter] in the [prepared statement] P. ** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA" ** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA" ** respectively. ** In other words, the initial ":" or "$" or "@" or "?" ** is included as part of the name.)^ ** ^Parameters of the form "?" without a following integer have no name ** and are referred to as "nameless" or "anonymous parameters". ** ** ^The first host parameter has an index of 1, not 0. ** ** ^If the value N is out of range or if the N-th parameter is ** nameless, then NULL is returned. ^The returned string is ** always in UTF-8 encoding even if the named parameter was ** originally specified as UTF-16 in [sqlite3_prepare16()], ** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()]. ** ** See also: [sqlite3_bind_blob|sqlite3_bind()], ** [sqlite3_bind_parameter_count()], and ** [sqlite3_bind_parameter_index()]. */ SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); /* ** CAPI3REF: Index Of A Parameter With A Given Name ** METHOD: sqlite3_stmt ** ** ^Return the index of an SQL parameter given its name. ^The ** index value returned is suitable for use as the second ** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero ** is returned if no matching parameter is found. ^The parameter ** name must be given in UTF-8 even if the original statement ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or ** [sqlite3_prepare16_v3()]. ** ** See also: [sqlite3_bind_blob|sqlite3_bind()], ** [sqlite3_bind_parameter_count()], and ** [sqlite3_bind_parameter_name()]. */ SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); /* ** CAPI3REF: Reset All Bindings On A Prepared Statement ** METHOD: sqlite3_stmt ** ** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset ** the [sqlite3_bind_blob | bindings] on a [prepared statement]. ** ^Use this routine to reset all host parameters to NULL. */ SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*); /* ** CAPI3REF: Number Of Columns In A Result Set ** METHOD: sqlite3_stmt ** ** ^Return the number of columns in the result set returned by the ** [prepared statement]. ^If this routine returns 0, that means the ** [prepared statement] returns no data (for example an [UPDATE]). ** ^However, just because this routine returns a positive number does not ** mean that one or more rows of data will be returned. ^A SELECT statement ** will always have a positive sqlite3_column_count() but depending on the ** WHERE clause constraints and the table content, it might return no rows. ** ** See also: [sqlite3_data_count()] */ SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt); /* ** CAPI3REF: Column Names In A Result Set ** METHOD: sqlite3_stmt ** ** ^These routines return the name assigned to a particular column ** in the result set of a [SELECT] statement. ^The sqlite3_column_name() ** interface returns a pointer to a zero-terminated UTF-8 string ** and sqlite3_column_name16() returns a pointer to a zero-terminated ** UTF-16 string. ^The first parameter is the [prepared statement] ** that implements the [SELECT] statement. ^The second parameter is the ** column number. ^The leftmost column is number 0. ** ** ^The returned string pointer is valid until either the [prepared statement] ** is destroyed by [sqlite3_finalize()] or until the statement is automatically ** reprepared by the first call to [sqlite3_step()] for a particular run ** or until the next call to ** sqlite3_column_name() or sqlite3_column_name16() on the same column. ** ** ^If sqlite3_malloc() fails during the processing of either routine ** (for example during a conversion from UTF-8 to UTF-16) then a ** NULL pointer is returned. ** ** ^The name of a result column is the value of the "AS" clause for ** that column, if there is an AS clause. If there is no AS clause ** then the name of the column is unspecified and may change from ** one release of SQLite to the next. */ SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N); SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N); /* ** CAPI3REF: Source Of Data In A Query Result ** METHOD: sqlite3_stmt ** ** ^These routines provide a means to determine the database, table, and ** table column that is the origin of a particular result column in a ** [SELECT] statement. ** ^The name of the database or table or column can be returned as ** either a UTF-8 or UTF-16 string. ^The _database_ routines return ** the database name, the _table_ routines return the table name, and ** the origin_ routines return the column name. ** ^The returned string is valid until the [prepared statement] is destroyed ** using [sqlite3_finalize()] or until the statement is automatically ** reprepared by the first call to [sqlite3_step()] for a particular run ** or until the same information is requested ** again in a different encoding. ** ** ^The names returned are the original un-aliased names of the ** database, table, and column. ** ** ^The first argument to these interfaces is a [prepared statement]. ** ^These functions return information about the Nth result column returned by ** the statement, where N is the second function argument. ** ^The left-most column is column 0 for these routines. ** ** ^If the Nth column returned by the statement is an expression or ** subquery and is not a column value, then all of these functions return ** NULL. ^These routines might also return NULL if a memory allocation error ** occurs. ^Otherwise, they return the name of the attached database, table, ** or column that query result column was extracted from. ** ** ^As with all other SQLite APIs, those whose names end with "16" return ** UTF-16 encoded strings and the other functions return UTF-8. ** ** ^These APIs are only available if the library was compiled with the ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol. ** ** If two or more threads call one or more ** [sqlite3_column_database_name | column metadata interfaces] ** for the same [prepared statement] and result column ** at the same time then the results are undefined. */ SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int); SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int); SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int); SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int); SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int); SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int); /* ** CAPI3REF: Declared Datatype Of A Query Result ** METHOD: sqlite3_stmt ** ** ^(The first parameter is a [prepared statement]. ** If this statement is a [SELECT] statement and the Nth column of the ** returned result set of that [SELECT] is a table column (not an ** expression or subquery) then the declared type of the table ** column is returned.)^ ^If the Nth column of the result set is an ** expression or subquery, then a NULL pointer is returned. ** ^The returned string is always UTF-8 encoded. ** ** ^(For example, given the database schema: ** ** CREATE TABLE t1(c1 VARIANT); ** ** and the following statement to be compiled: ** ** SELECT c1 + 1, c1 FROM t1; ** ** this routine would return the string "VARIANT" for the second result ** column (i==1), and a NULL pointer for the first result column (i==0).)^ ** ** ^SQLite uses dynamic run-time typing. ^So just because a column ** is declared to contain a particular type does not mean that the ** data stored in that column is of the declared type. SQLite is ** strongly typed, but the typing is dynamic not static. ^Type ** is associated with individual values, not with the containers ** used to hold those values. */ SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int); SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int); /* ** CAPI3REF: Evaluate An SQL Statement ** METHOD: sqlite3_stmt ** ** After a [prepared statement] has been prepared using any of ** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()], ** or [sqlite3_prepare16_v3()] or one of the legacy ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function ** must be called one or more times to evaluate the statement. ** ** The details of the behavior of the sqlite3_step() interface depend ** on whether the statement was prepared using the newer "vX" interfaces ** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()], ** [sqlite3_prepare16_v2()] or the older legacy ** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the ** new "vX" interface is recommended for new applications but the legacy ** interface will continue to be supported. ** ** ^In the legacy interface, the return value will be either [SQLITE_BUSY], ** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE]. ** ^With the "v2" interface, any of the other [result codes] or ** [extended result codes] might be returned as well. ** ** ^[SQLITE_BUSY] means that the database engine was unable to acquire the ** database locks it needs to do its job. ^If the statement is a [COMMIT] ** or occurs outside of an explicit transaction, then you can retry the ** statement. If the statement is not a [COMMIT] and occurs within an ** explicit transaction then you should rollback the transaction before ** continuing. ** ** ^[SQLITE_DONE] means that the statement has finished executing ** successfully. sqlite3_step() should not be called again on this virtual ** machine without first calling [sqlite3_reset()] to reset the virtual ** machine back to its initial state. ** ** ^If the SQL statement being executed returns any data, then [SQLITE_ROW] ** is returned each time a new row of data is ready for processing by the ** caller. The values may be accessed using the [column access functions]. ** sqlite3_step() is called again to retrieve the next row of data. ** ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint ** violation) has occurred. sqlite3_step() should not be called again on ** the VM. More information may be found by calling [sqlite3_errmsg()]. ** ^With the legacy interface, a more specific error code (for example, ** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth) ** can be obtained by calling [sqlite3_reset()] on the ** [prepared statement]. ^In the "v2" interface, ** the more specific error code is returned directly by sqlite3_step(). ** ** [SQLITE_MISUSE] means that the this routine was called inappropriately. ** Perhaps it was called on a [prepared statement] that has ** already been [sqlite3_finalize | finalized] or on one that had ** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could ** be the case that the same database connection is being used by two or ** more threads at the same moment in time. ** ** For all versions of SQLite up to and including 3.6.23.1, a call to ** [sqlite3_reset()] was required after sqlite3_step() returned anything ** other than [SQLITE_ROW] before any subsequent invocation of ** sqlite3_step(). Failure to reset the prepared statement using ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from ** sqlite3_step(). But after [version 3.6.23.1] ([dateof:3.6.23.1]), ** sqlite3_step() began ** calling [sqlite3_reset()] automatically in this circumstance rather ** than returning [SQLITE_MISUSE]. This is not considered a compatibility ** break because any application that ever receives an SQLITE_MISUSE error ** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option ** can be used to restore the legacy behavior. ** ** Goofy Interface Alert: In the legacy interface, the sqlite3_step() ** API always returns a generic error code, [SQLITE_ERROR], following any ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the ** specific [error codes] that better describes the error. ** We admit that this is a goofy design. The problem has been fixed ** with the "v2" interface. If you prepare all of your SQL statements ** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()] ** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces, ** then the more specific [error codes] are returned directly ** by sqlite3_step(). The use of the "vX" interfaces is recommended. */ SQLITE_API int sqlite3_step(sqlite3_stmt*); /* ** CAPI3REF: Number of columns in a result set ** METHOD: sqlite3_stmt ** ** ^The sqlite3_data_count(P) interface returns the number of columns in the ** current row of the result set of [prepared statement] P. ** ^If prepared statement P does not have results ready to return ** (via calls to the [sqlite3_column_int | sqlite3_column()] family of ** interfaces) then sqlite3_data_count(P) returns 0. ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer. ** ^The sqlite3_data_count(P) routine returns 0 if the previous call to ** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P) ** will return non-zero if previous call to [sqlite3_step](P) returned ** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum] ** where it always returns zero since each step of that multi-step ** pragma returns 0 columns of data. ** ** See also: [sqlite3_column_count()] */ SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt); /* ** CAPI3REF: Fundamental Datatypes ** KEYWORDS: SQLITE_TEXT ** ** ^(Every value in SQLite has one of five fundamental datatypes: ** **
    **
  • 64-bit signed integer **
  • 64-bit IEEE floating point number **
  • string **
  • BLOB **
  • NULL **
)^ ** ** These constants are codes for each of those types. ** ** Note that the SQLITE_TEXT constant was also used in SQLite version 2 ** for a completely different meaning. Software that links against both ** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not ** SQLITE_TEXT. */ #define SQLITE_INTEGER 1 #define SQLITE_FLOAT 2 #define SQLITE_BLOB 4 #define SQLITE_NULL 5 #ifdef SQLITE_TEXT # undef SQLITE_TEXT #else # define SQLITE_TEXT 3 #endif #define SQLITE3_TEXT 3 /* ** CAPI3REF: Result Values From A Query ** KEYWORDS: {column access functions} ** METHOD: sqlite3_stmt ** ** Summary: **
**
sqlite3_column_blobBLOB result **
sqlite3_column_doubleREAL result **
sqlite3_column_int32-bit INTEGER result **
sqlite3_column_int6464-bit INTEGER result **
sqlite3_column_textUTF-8 TEXT result **
sqlite3_column_text16UTF-16 TEXT result **
sqlite3_column_valueThe result as an ** [sqlite3_value|unprotected sqlite3_value] object. **
    **
sqlite3_column_bytesSize of a BLOB ** or a UTF-8 TEXT result in bytes **
sqlite3_column_bytes16   ** →  Size of UTF-16 ** TEXT in bytes **
sqlite3_column_typeDefault ** datatype of the result **
** ** Details: ** ** ^These routines return information about a single column of the current ** result row of a query. ^In every case the first argument is a pointer ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*] ** that was returned from [sqlite3_prepare_v2()] or one of its variants) ** and the second argument is the index of the column for which information ** should be returned. ^The leftmost column of the result set has the index 0. ** ^The number of columns in the result can be determined using ** [sqlite3_column_count()]. ** ** If the SQL statement does not currently point to a valid row, or if the ** column index is out of range, the result is undefined. ** These routines may only be called when the most recent call to ** [sqlite3_step()] has returned [SQLITE_ROW] and neither ** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently. ** If any of these routines are called after [sqlite3_reset()] or ** [sqlite3_finalize()] or after [sqlite3_step()] has returned ** something other than [SQLITE_ROW], the results are undefined. ** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()] ** are called from a different thread while any of these routines ** are pending, then the results are undefined. ** ** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16) ** each return the value of a result column in a specific data format. If ** the result column is not initially in the requested format (for example, ** if the query returns an integer but the sqlite3_column_text() interface ** is used to extract the value) then an automatic type conversion is performed. ** ** ^The sqlite3_column_type() routine returns the ** [SQLITE_INTEGER | datatype code] for the initial data type ** of the result column. ^The returned value is one of [SQLITE_INTEGER], ** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. ** The return value of sqlite3_column_type() can be used to decide which ** of the first six interface should be used to extract the column value. ** The value returned by sqlite3_column_type() is only meaningful if no ** automatic type conversions have occurred for the value in question. ** After a type conversion, the result of calling sqlite3_column_type() ** is undefined, though harmless. Future ** versions of SQLite may change the behavior of sqlite3_column_type() ** following a type conversion. ** ** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes() ** or sqlite3_column_bytes16() interfaces can be used to determine the size ** of that BLOB or string. ** ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes() ** routine returns the number of bytes in that BLOB or string. ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts ** the string to UTF-8 and then returns the number of bytes. ** ^If the result is a numeric value then sqlite3_column_bytes() uses ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns ** the number of bytes in that string. ** ^If the result is NULL, then sqlite3_column_bytes() returns zero. ** ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16() ** routine returns the number of bytes in that BLOB or string. ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts ** the string to UTF-16 and then returns the number of bytes. ** ^If the result is a numeric value then sqlite3_column_bytes16() uses ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns ** the number of bytes in that string. ** ^If the result is NULL, then sqlite3_column_bytes16() returns zero. ** ** ^The values returned by [sqlite3_column_bytes()] and ** [sqlite3_column_bytes16()] do not include the zero terminators at the end ** of the string. ^For clarity: the values returned by ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of ** bytes in the string, not the number of characters. ** ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(), ** even empty strings, are always zero-terminated. ^The return ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer. ** ** ^Strings returned by sqlite3_column_text16() always have the endianness ** which is native to the platform, regardless of the text encoding set ** for the database. ** ** Warning: ^The object returned by [sqlite3_column_value()] is an ** [unprotected sqlite3_value] object. In a multithreaded environment, ** an unprotected sqlite3_value object may only be used safely with ** [sqlite3_bind_value()] and [sqlite3_result_value()]. ** If the [unprotected sqlite3_value] object returned by ** [sqlite3_column_value()] is used in any other way, including calls ** to routines like [sqlite3_value_int()], [sqlite3_value_text()], ** or [sqlite3_value_bytes()], the behavior is not threadsafe. ** Hence, the sqlite3_column_value() interface ** is normally only useful within the implementation of ** [application-defined SQL functions] or [virtual tables], not within ** top-level application code. ** ** These routines may attempt to convert the datatype of the result. ** ^For example, if the internal representation is FLOAT and a text result ** is requested, [sqlite3_snprintf()] is used internally to perform the ** conversion automatically. ^(The following table details the conversions ** that are applied: ** **
** **
Internal
Type
Requested
Type
Conversion ** **
NULL INTEGER Result is 0 **
NULL FLOAT Result is 0.0 **
NULL TEXT Result is a NULL pointer **
NULL BLOB Result is a NULL pointer **
INTEGER FLOAT Convert from integer to float **
INTEGER TEXT ASCII rendering of the integer **
INTEGER BLOB Same as INTEGER->TEXT **
FLOAT INTEGER [CAST] to INTEGER **
FLOAT TEXT ASCII rendering of the float **
FLOAT BLOB [CAST] to BLOB **
TEXT INTEGER [CAST] to INTEGER **
TEXT FLOAT [CAST] to REAL **
TEXT BLOB No change **
BLOB INTEGER [CAST] to INTEGER **
BLOB FLOAT [CAST] to REAL **
BLOB TEXT [CAST] to TEXT, ensure zero terminator **
**
)^ ** ** Note that when type conversions occur, pointers returned by prior ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or ** sqlite3_column_text16() may be invalidated. ** Type conversions and pointer invalidations might occur ** in the following cases: ** **
    **
  • The initial content is a BLOB and sqlite3_column_text() or ** sqlite3_column_text16() is called. A zero-terminator might ** need to be added to the string.
  • **
  • The initial content is UTF-8 text and sqlite3_column_bytes16() or ** sqlite3_column_text16() is called. The content must be converted ** to UTF-16.
  • **
  • The initial content is UTF-16 text and sqlite3_column_bytes() or ** sqlite3_column_text() is called. The content must be converted ** to UTF-8.
  • **
** ** ^Conversions between UTF-16be and UTF-16le are always done in place and do ** not invalidate a prior pointer, though of course the content of the buffer ** that the prior pointer references will have been modified. Other kinds ** of conversion are done in place when it is possible, but sometimes they ** are not possible and in those cases prior pointers are invalidated. ** ** The safest policy is to invoke these routines ** in one of the following ways: ** **
    **
  • sqlite3_column_text() followed by sqlite3_column_bytes()
  • **
  • sqlite3_column_blob() followed by sqlite3_column_bytes()
  • **
  • sqlite3_column_text16() followed by sqlite3_column_bytes16()
  • **
** ** In other words, you should call sqlite3_column_text(), ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result ** into the desired format, then invoke sqlite3_column_bytes() or ** sqlite3_column_bytes16() to find the size of the result. Do not mix calls ** to sqlite3_column_text() or sqlite3_column_blob() with calls to ** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16() ** with calls to sqlite3_column_bytes(). ** ** ^The pointers returned are valid until a type conversion occurs as ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or ** [sqlite3_finalize()] is called. ^The memory space used to hold strings ** and BLOBs is freed automatically. Do not pass the pointers returned ** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into ** [sqlite3_free()]. ** ** As long as the input parameters are correct, these routines will only ** fail if an out-of-memory error occurs during a format conversion. ** Only the following subset of interfaces are subject to out-of-memory ** errors: ** **
    **
  • sqlite3_column_blob() **
  • sqlite3_column_text() **
  • sqlite3_column_text16() **
  • sqlite3_column_bytes() **
  • sqlite3_column_bytes16() **
** ** If an out-of-memory error occurs, then the return value from these ** routines is the same as if the column had contained an SQL NULL value. ** Valid SQL NULL returns can be distinguished from out-of-memory errors ** by invoking the [sqlite3_errcode()] immediately after the suspect ** return value is obtained and before any ** other SQLite interface is called on the same [database connection]. */ SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol); SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol); SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol); SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol); SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol); SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol); SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol); /* ** CAPI3REF: Destroy A Prepared Statement Object ** DESTRUCTOR: sqlite3_stmt ** ** ^The sqlite3_finalize() function is called to delete a [prepared statement]. ** ^If the most recent evaluation of the statement encountered no errors ** or if the statement is never been evaluated, then sqlite3_finalize() returns ** SQLITE_OK. ^If the most recent evaluation of statement S failed, then ** sqlite3_finalize(S) returns the appropriate [error code] or ** [extended error code]. ** ** ^The sqlite3_finalize(S) routine can be called at any point during ** the life cycle of [prepared statement] S: ** before statement S is ever evaluated, after ** one or more calls to [sqlite3_reset()], or after any call ** to [sqlite3_step()] regardless of whether or not the statement has ** completed execution. ** ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op. ** ** The application must finalize every [prepared statement] in order to avoid ** resource leaks. It is a grievous error for the application to try to use ** a prepared statement after it has been finalized. Any use of a prepared ** statement after it has been finalized can result in undefined and ** undesirable behavior such as segfaults and heap corruption. */ SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt); /* ** CAPI3REF: Reset A Prepared Statement Object ** METHOD: sqlite3_stmt ** ** The sqlite3_reset() function is called to reset a [prepared statement] ** object back to its initial state, ready to be re-executed. ** ^Any SQL statement variables that had values bound to them using ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values. ** Use [sqlite3_clear_bindings()] to reset the bindings. ** ** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S ** back to the beginning of its program. ** ** ^The return code from [sqlite3_reset(S)] indicates whether or not ** the previous evaluation of prepared statement S completed successfully. ** ^If [sqlite3_step(S)] has never before been called on S or if ** [sqlite3_step(S)] has not been called since the previous call ** to [sqlite3_reset(S)], then [sqlite3_reset(S)] will return ** [SQLITE_OK]. ** ** ^If the most recent call to [sqlite3_step(S)] for the ** [prepared statement] S indicated an error, then ** [sqlite3_reset(S)] returns an appropriate [error code]. ** ^The [sqlite3_reset(S)] interface might also return an [error code] ** if there were no prior errors but the process of resetting ** the prepared statement caused a new error. ^For example, if an ** [INSERT] statement with a [RETURNING] clause is only stepped one time, ** that one call to [sqlite3_step(S)] might return SQLITE_ROW but ** the overall statement might still fail and the [sqlite3_reset(S)] call ** might return SQLITE_BUSY if locking constraints prevent the ** database change from committing. Therefore, it is important that ** applications check the return code from [sqlite3_reset(S)] even if ** no prior call to [sqlite3_step(S)] indicated a problem. ** ** ^The [sqlite3_reset(S)] interface does not change the values ** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S. */ SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt); /* ** CAPI3REF: Create Or Redefine SQL Functions ** KEYWORDS: {function creation routines} ** METHOD: sqlite3 ** ** ^These functions (collectively known as "function creation routines") ** are used to add SQL functions or aggregates or to redefine the behavior ** of existing SQL functions or aggregates. The only differences between ** the three "sqlite3_create_function*" routines are the text encoding ** expected for the second parameter (the name of the function being ** created) and the presence or absence of a destructor callback for ** the application data pointer. Function sqlite3_create_window_function() ** is similar, but allows the user to supply the extra callback functions ** needed by [aggregate window functions]. ** ** ^The first parameter is the [database connection] to which the SQL ** function is to be added. ^If an application uses more than one database ** connection then application-defined SQL functions must be added ** to each database connection separately. ** ** ^The second parameter is the name of the SQL function to be created or ** redefined. ^The length of the name is limited to 255 bytes in a UTF-8 ** representation, exclusive of the zero-terminator. ^Note that the name ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes. ** ^Any attempt to create a function with a longer name ** will result in [SQLITE_MISUSE] being returned. ** ** ^The third parameter (nArg) ** is the number of arguments that the SQL function or ** aggregate takes. ^If this parameter is -1, then the SQL function or ** aggregate may take any number of arguments between 0 and the limit ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third ** parameter is less than -1 or greater than 127 then the behavior is ** undefined. ** ** ^The fourth parameter, eTextRep, specifies what ** [SQLITE_UTF8 | text encoding] this SQL function prefers for ** its parameters. The application should set this parameter to ** [SQLITE_UTF16LE] if the function implementation invokes ** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the ** implementation invokes [sqlite3_value_text16be()] on an input, or ** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8] ** otherwise. ^The same SQL function may be registered multiple times using ** different preferred text encodings, with different implementations for ** each encoding. ** ^When multiple implementations of the same function are available, SQLite ** will pick the one that involves the least amount of data conversion. ** ** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC] ** to signal that the function will always return the same result given ** the same inputs within a single SQL statement. Most SQL functions are ** deterministic. The built-in [random()] SQL function is an example of a ** function that is not deterministic. The SQLite query planner is able to ** perform additional optimizations on deterministic functions, so use ** of the [SQLITE_DETERMINISTIC] flag is recommended where possible. ** ** ^The fourth parameter may also optionally include the [SQLITE_DIRECTONLY] ** flag, which if present prevents the function from being invoked from ** within VIEWs, TRIGGERs, CHECK constraints, generated column expressions, ** index expressions, or the WHERE clause of partial indexes. ** ** For best security, the [SQLITE_DIRECTONLY] flag is recommended for ** all application-defined SQL functions that do not need to be ** used inside of triggers, views, CHECK constraints, or other elements of ** the database schema. This flag is especially recommended for SQL ** functions that have side effects or reveal internal application state. ** Without this flag, an attacker might be able to modify the schema of ** a database file to include invocations of the function with parameters ** chosen by the attacker, which the application will then execute when ** the database file is opened and read. ** ** ^(The fifth parameter is an arbitrary pointer. The implementation of the ** function can gain access to this pointer using [sqlite3_user_data()].)^ ** ** ^The sixth, seventh and eighth parameters passed to the three ** "sqlite3_create_function*" functions, xFunc, xStep and xFinal, are ** pointers to C-language functions that implement the SQL function or ** aggregate. ^A scalar SQL function requires an implementation of the xFunc ** callback only; NULL pointers must be passed as the xStep and xFinal ** parameters. ^An aggregate SQL function requires an implementation of xStep ** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing ** SQL function or aggregate, pass NULL pointers for all three function ** callbacks. ** ** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue ** and xInverse) passed to sqlite3_create_window_function are pointers to ** C-language callbacks that implement the new function. xStep and xFinal ** must both be non-NULL. xValue and xInverse may either both be NULL, in ** which case a regular aggregate function is created, or must both be ** non-NULL, in which case the new function may be used as either an aggregate ** or aggregate window function. More details regarding the implementation ** of aggregate window functions are ** [user-defined window functions|available here]. ** ** ^(If the final parameter to sqlite3_create_function_v2() or ** sqlite3_create_window_function() is not NULL, then it is the destructor for ** the application data pointer. The destructor is invoked when the function ** is deleted, either by being overloaded or when the database connection ** closes.)^ ^The destructor is also invoked if the call to ** sqlite3_create_function_v2() fails. ^When the destructor callback is ** invoked, it is passed a single argument which is a copy of the application ** data pointer which was the fifth parameter to sqlite3_create_function_v2(). ** ** ^It is permitted to register multiple implementations of the same ** functions with the same name but with either differing numbers of ** arguments or differing preferred text encodings. ^SQLite will use ** the implementation that most closely matches the way in which the ** SQL function is used. ^A function implementation with a non-negative ** nArg parameter is a better match than a function implementation with ** a negative nArg. ^A function where the preferred text encoding ** matches the database encoding is a better ** match than a function where the encoding is different. ** ^A function where the encoding difference is between UTF16le and UTF16be ** is a closer match than a function where the encoding difference is ** between UTF8 and UTF16. ** ** ^Built-in functions may be overloaded by new application-defined functions. ** ** ^An application-defined function is permitted to call other ** SQLite interfaces. However, such calls must not ** close the database connection nor finalize or reset the prepared ** statement in which the function is running. */ SQLITE_API int sqlite3_create_function( sqlite3 *db, const char *zFunctionName, int nArg, int eTextRep, void *pApp, void (*xFunc)(sqlite3_context*,int,sqlite3_value**), void (*xStep)(sqlite3_context*,int,sqlite3_value**), void (*xFinal)(sqlite3_context*) ); SQLITE_API int sqlite3_create_function16( sqlite3 *db, const void *zFunctionName, int nArg, int eTextRep, void *pApp, void (*xFunc)(sqlite3_context*,int,sqlite3_value**), void (*xStep)(sqlite3_context*,int,sqlite3_value**), void (*xFinal)(sqlite3_context*) ); SQLITE_API int sqlite3_create_function_v2( sqlite3 *db, const char *zFunctionName, int nArg, int eTextRep, void *pApp, void (*xFunc)(sqlite3_context*,int,sqlite3_value**), void (*xStep)(sqlite3_context*,int,sqlite3_value**), void (*xFinal)(sqlite3_context*), void(*xDestroy)(void*) ); SQLITE_API int sqlite3_create_window_function( sqlite3 *db, const char *zFunctionName, int nArg, int eTextRep, void *pApp, void (*xStep)(sqlite3_context*,int,sqlite3_value**), void (*xFinal)(sqlite3_context*), void (*xValue)(sqlite3_context*), void (*xInverse)(sqlite3_context*,int,sqlite3_value**), void(*xDestroy)(void*) ); /* ** CAPI3REF: Text Encodings ** ** These constant define integer codes that represent the various ** text encodings supported by SQLite. */ #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */ #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */ #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */ #define SQLITE_UTF16 4 /* Use native byte order */ #define SQLITE_ANY 5 /* Deprecated */ #define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ /* ** CAPI3REF: Function Flags ** ** These constants may be ORed together with the ** [SQLITE_UTF8 | preferred text encoding] as the fourth argument ** to [sqlite3_create_function()], [sqlite3_create_function16()], or ** [sqlite3_create_function_v2()]. ** **
** [[SQLITE_DETERMINISTIC]]
SQLITE_DETERMINISTIC
** The SQLITE_DETERMINISTIC flag means that the new function always gives ** the same output when the input parameters are the same. ** The [abs|abs() function] is deterministic, for example, but ** [randomblob|randomblob()] is not. Functions must ** be deterministic in order to be used in certain contexts such as ** with the WHERE clause of [partial indexes] or in [generated columns]. ** SQLite might also optimize deterministic functions by factoring them ** out of inner loops. **
** ** [[SQLITE_DIRECTONLY]]
SQLITE_DIRECTONLY
** The SQLITE_DIRECTONLY flag means that the function may only be invoked ** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in ** schema structures such as [CHECK constraints], [DEFAULT clauses], ** [expression indexes], [partial indexes], or [generated columns]. **

** The SQLITE_DIRECTONLY flag is recommended for any ** [application-defined SQL function] ** that has side-effects or that could potentially leak sensitive information. ** This will prevent attacks in which an application is tricked ** into using a database file that has had its schema surreptitiously ** modified to invoke the application-defined function in ways that are ** harmful. **

** Some people say it is good practice to set SQLITE_DIRECTONLY on all ** [application-defined SQL functions], regardless of whether or not they ** are security sensitive, as doing so prevents those functions from being used ** inside of the database schema, and thus ensures that the database ** can be inspected and modified using generic tools (such as the [CLI]) ** that do not have access to the application-defined functions. **

** ** [[SQLITE_INNOCUOUS]]
SQLITE_INNOCUOUS
** The SQLITE_INNOCUOUS flag means that the function is unlikely ** to cause problems even if misused. An innocuous function should have ** no side effects and should not depend on any values other than its ** input parameters. The [abs|abs() function] is an example of an ** innocuous function. ** The [load_extension() SQL function] is not innocuous because of its ** side effects. **

SQLITE_INNOCUOUS is similar to SQLITE_DETERMINISTIC, but is not ** exactly the same. The [random|random() function] is an example of a ** function that is innocuous but not deterministic. **

Some heightened security settings ** ([SQLITE_DBCONFIG_TRUSTED_SCHEMA] and [PRAGMA trusted_schema=OFF]) ** disable the use of SQL functions inside views and triggers and in ** schema structures such as [CHECK constraints], [DEFAULT clauses], ** [expression indexes], [partial indexes], and [generated columns] unless ** the function is tagged with SQLITE_INNOCUOUS. Most built-in functions ** are innocuous. Developers are advised to avoid using the ** SQLITE_INNOCUOUS flag for application-defined functions unless the ** function has been carefully audited and found to be free of potentially ** security-adverse side-effects and information-leaks. **

** ** [[SQLITE_SUBTYPE]]
SQLITE_SUBTYPE
** The SQLITE_SUBTYPE flag indicates to SQLite that a function might call ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments. ** This flag instructs SQLite to omit some corner-case optimizations that ** might disrupt the operation of the [sqlite3_value_subtype()] function, ** causing it to return zero rather than the correct subtype(). ** All SQL functions that invoke [sqlite3_value_subtype()] should have this ** property. If the SQLITE_SUBTYPE property is omitted, then the return ** value from [sqlite3_value_subtype()] might sometimes be zero even though ** a non-zero subtype was specified by the function argument expression. ** ** [[SQLITE_RESULT_SUBTYPE]]
SQLITE_RESULT_SUBTYPE
** The SQLITE_RESULT_SUBTYPE flag indicates to SQLite that a function might call ** [sqlite3_result_subtype()] to cause a sub-type to be associated with its ** result. ** Every function that invokes [sqlite3_result_subtype()] should have this ** property. If it does not, then the call to [sqlite3_result_subtype()] ** might become a no-op if the function is used as term in an ** [expression index]. On the other hand, SQL functions that never invoke ** [sqlite3_result_subtype()] should avoid setting this property, as the ** purpose of this property is to disable certain optimizations that are ** incompatible with subtypes. ** ** [[SQLITE_SELFORDER1]]
SQLITE_SELFORDER1
** The SQLITE_SELFORDER1 flag indicates that the function is an aggregate ** that internally orders the values provided to the first argument. The ** ordered-set aggregate SQL notation with a single ORDER BY term can be ** used to invoke this function. If the ordered-set aggregate notation is ** used on a function that lacks this flag, then an error is raised. Note ** that the ordered-set aggregate syntax is only available if SQLite is ** built using the -DSQLITE_ENABLE_ORDERED_SET_AGGREGATES compile-time option. **
**
*/ #define SQLITE_DETERMINISTIC 0x000000800 #define SQLITE_DIRECTONLY 0x000080000 #define SQLITE_SUBTYPE 0x000100000 #define SQLITE_INNOCUOUS 0x000200000 #define SQLITE_RESULT_SUBTYPE 0x001000000 #define SQLITE_SELFORDER1 0x002000000 /* ** CAPI3REF: Deprecated Functions ** DEPRECATED ** ** These functions are [deprecated]. In order to maintain ** backwards compatibility with older code, these functions continue ** to be supported. However, new applications should avoid ** the use of these functions. To encourage programmers to avoid ** these functions, we will not explain what they do. */ #ifndef SQLITE_OMIT_DEPRECATED SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*); SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*); SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void); SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void); SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int), void*,sqlite3_int64); #endif /* ** CAPI3REF: Obtaining SQL Values ** METHOD: sqlite3_value ** ** Summary: **
**
sqlite3_value_blobBLOB value **
sqlite3_value_doubleREAL value **
sqlite3_value_int32-bit INTEGER value **
sqlite3_value_int6464-bit INTEGER value **
sqlite3_value_pointerPointer value **
sqlite3_value_textUTF-8 TEXT value **
sqlite3_value_text16UTF-16 TEXT value in ** the native byteorder **
sqlite3_value_text16beUTF-16be TEXT value **
sqlite3_value_text16leUTF-16le TEXT value **
    **
sqlite3_value_bytesSize of a BLOB ** or a UTF-8 TEXT in bytes **
sqlite3_value_bytes16   ** →  Size of UTF-16 ** TEXT in bytes **
sqlite3_value_typeDefault ** datatype of the value **
sqlite3_value_numeric_type   ** →  Best numeric datatype of the value **
sqlite3_value_nochange   ** →  True if the column is unchanged in an UPDATE ** against a virtual table. **
sqlite3_value_frombind   ** →  True if value originated from a [bound parameter] **
** ** Details: ** ** These routines extract type, size, and content information from ** [protected sqlite3_value] objects. Protected sqlite3_value objects ** are used to pass parameter information into the functions that ** implement [application-defined SQL functions] and [virtual tables]. ** ** These routines work only with [protected sqlite3_value] objects. ** Any attempt to use these routines on an [unprotected sqlite3_value] ** is not threadsafe. ** ** ^These routines work just like the corresponding [column access functions] ** except that these routines take a single [protected sqlite3_value] object ** pointer instead of a [sqlite3_stmt*] pointer and an integer column number. ** ** ^The sqlite3_value_text16() interface extracts a UTF-16 string ** in the native byte-order of the host machine. ^The ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces ** extract UTF-16 strings as big-endian and little-endian respectively. ** ** ^If [sqlite3_value] object V was initialized ** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)] ** and if X and Y are strings that compare equal according to strcmp(X,Y), ** then sqlite3_value_pointer(V,Y) will return the pointer P. ^Otherwise, ** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer() ** routine is part of the [pointer passing interface] added for SQLite 3.20.0. ** ** ^(The sqlite3_value_type(V) interface returns the ** [SQLITE_INTEGER | datatype code] for the initial datatype of the ** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER], ** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^ ** Other interfaces might change the datatype for an sqlite3_value object. ** For example, if the datatype is initially SQLITE_INTEGER and ** sqlite3_value_text(V) is called to extract a text value for that ** integer, then subsequent calls to sqlite3_value_type(V) might return ** SQLITE_TEXT. Whether or not a persistent internal datatype conversion ** occurs is undefined and may change from one release of SQLite to the next. ** ** ^(The sqlite3_value_numeric_type() interface attempts to apply ** numeric affinity to the value. This means that an attempt is ** made to convert the value to an integer or floating point. If ** such a conversion is possible without loss of information (in other ** words, if the value is a string that looks like a number) ** then the conversion is performed. Otherwise no conversion occurs. ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^ ** ** ^Within the [xUpdate] method of a [virtual table], the ** sqlite3_value_nochange(X) interface returns true if and only if ** the column corresponding to X is unchanged by the UPDATE operation ** that the xUpdate method call was invoked to implement and if ** and the prior [xColumn] method call that was invoked to extracted ** the value for that column returned without setting a result (probably ** because it queried [sqlite3_vtab_nochange()] and found that the column ** was unchanging). ^Within an [xUpdate] method, any value for which ** sqlite3_value_nochange(X) is true will in all other respects appear ** to be a NULL value. If sqlite3_value_nochange(X) is invoked anywhere other ** than within an [xUpdate] method call for an UPDATE statement, then ** the return value is arbitrary and meaningless. ** ** ^The sqlite3_value_frombind(X) interface returns non-zero if the ** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()] ** interfaces. ^If X comes from an SQL literal value, or a table column, ** or an expression, then sqlite3_value_frombind(X) returns zero. ** ** Please pay particular attention to the fact that the pointer returned ** from [sqlite3_value_blob()], [sqlite3_value_text()], or ** [sqlite3_value_text16()] can be invalidated by a subsequent call to ** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()], ** or [sqlite3_value_text16()]. ** ** These routines must be called from the same thread as ** the SQL function that supplied the [sqlite3_value*] parameters. ** ** As long as the input parameter is correct, these routines can only ** fail if an out-of-memory error occurs during a format conversion. ** Only the following subset of interfaces are subject to out-of-memory ** errors: ** **
    **
  • sqlite3_value_blob() **
  • sqlite3_value_text() **
  • sqlite3_value_text16() **
  • sqlite3_value_text16le() **
  • sqlite3_value_text16be() **
  • sqlite3_value_bytes() **
  • sqlite3_value_bytes16() **
** ** If an out-of-memory error occurs, then the return value from these ** routines is the same as if the column had contained an SQL NULL value. ** Valid SQL NULL returns can be distinguished from out-of-memory errors ** by invoking the [sqlite3_errcode()] immediately after the suspect ** return value is obtained and before any ** other SQLite interface is called on the same [database connection]. */ SQLITE_API const void *sqlite3_value_blob(sqlite3_value*); SQLITE_API double sqlite3_value_double(sqlite3_value*); SQLITE_API int sqlite3_value_int(sqlite3_value*); SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*); SQLITE_API void *sqlite3_value_pointer(sqlite3_value*, const char*); SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*); SQLITE_API const void *sqlite3_value_text16(sqlite3_value*); SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*); SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*); SQLITE_API int sqlite3_value_bytes(sqlite3_value*); SQLITE_API int sqlite3_value_bytes16(sqlite3_value*); SQLITE_API int sqlite3_value_type(sqlite3_value*); SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*); SQLITE_API int sqlite3_value_nochange(sqlite3_value*); SQLITE_API int sqlite3_value_frombind(sqlite3_value*); /* ** CAPI3REF: Report the internal text encoding state of an sqlite3_value object ** METHOD: sqlite3_value ** ** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8], ** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current text encoding ** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X) ** returns something other than SQLITE_TEXT, then the return value from ** sqlite3_value_encoding(X) is meaningless. ^Calls to ** [sqlite3_value_text(X)], [sqlite3_value_text16(X)], [sqlite3_value_text16be(X)], ** [sqlite3_value_text16le(X)], [sqlite3_value_bytes(X)], or ** [sqlite3_value_bytes16(X)] might change the encoding of the value X and ** thus change the return from subsequent calls to sqlite3_value_encoding(X). ** ** This routine is intended for used by applications that test and validate ** the SQLite implementation. This routine is inquiring about the opaque ** internal state of an [sqlite3_value] object. Ordinary applications should ** not need to know what the internal state of an sqlite3_value object is and ** hence should not need to use this interface. */ SQLITE_API int sqlite3_value_encoding(sqlite3_value*); /* ** CAPI3REF: Finding The Subtype Of SQL Values ** METHOD: sqlite3_value ** ** The sqlite3_value_subtype(V) function returns the subtype for ** an [application-defined SQL function] argument V. The subtype ** information can be used to pass a limited amount of context from ** one SQL function to another. Use the [sqlite3_result_subtype()] ** routine to set the subtype for the return value of an SQL function. ** ** Every [application-defined SQL function] that invokes this interface ** should include the [SQLITE_SUBTYPE] property in the text ** encoding argument when the function is [sqlite3_create_function|registered]. ** If the [SQLITE_SUBTYPE] property is omitted, then sqlite3_value_subtype() ** might return zero instead of the upstream subtype in some corner cases. */ SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*); /* ** CAPI3REF: Copy And Free SQL Values ** METHOD: sqlite3_value ** ** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value] ** object V and returns a pointer to that copy. ^The [sqlite3_value] returned ** is a [protected sqlite3_value] object even if the input is not. ** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a ** memory allocation fails. ^If V is a [pointer value], then the result ** of sqlite3_value_dup(V) is a NULL value. ** ** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object ** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer ** then sqlite3_value_free(V) is a harmless no-op. */ SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value*); SQLITE_API void sqlite3_value_free(sqlite3_value*); /* ** CAPI3REF: Obtain Aggregate Function Context ** METHOD: sqlite3_context ** ** Implementations of aggregate SQL functions use this ** routine to allocate memory for storing their state. ** ** ^The first time the sqlite3_aggregate_context(C,N) routine is called ** for a particular aggregate function, SQLite allocates ** N bytes of memory, zeroes out that memory, and returns a pointer ** to the new memory. ^On second and subsequent calls to ** sqlite3_aggregate_context() for the same aggregate function instance, ** the same buffer is returned. Sqlite3_aggregate_context() is normally ** called once for each invocation of the xStep callback and then one ** last time when the xFinal callback is invoked. ^(When no rows match ** an aggregate query, the xStep() callback of the aggregate function ** implementation is never called and xFinal() is called exactly once. ** In those cases, sqlite3_aggregate_context() might be called for the ** first time from within xFinal().)^ ** ** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer ** when first called if N is less than or equal to zero or if a memory ** allocation error occurs. ** ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is ** determined by the N parameter on the first successful call. Changing the ** value of N in any subsequent call to sqlite3_aggregate_context() within ** the same aggregate function instance will not resize the memory ** allocation.)^ Within the xFinal callback, it is customary to set ** N=0 in calls to sqlite3_aggregate_context(C,N) so that no ** pointless memory allocations occur. ** ** ^SQLite automatically frees the memory allocated by ** sqlite3_aggregate_context() when the aggregate query concludes. ** ** The first parameter must be a copy of the ** [sqlite3_context | SQL function context] that is the first parameter ** to the xStep or xFinal callback routine that implements the aggregate ** function. ** ** This routine must be called from the same thread in which ** the aggregate SQL function is running. */ SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes); /* ** CAPI3REF: User Data For Functions ** METHOD: sqlite3_context ** ** ^The sqlite3_user_data() interface returns a copy of ** the pointer that was the pUserData parameter (the 5th parameter) ** of the [sqlite3_create_function()] ** and [sqlite3_create_function16()] routines that originally ** registered the application defined function. ** ** This routine must be called from the same thread in which ** the application-defined function is running. */ SQLITE_API void *sqlite3_user_data(sqlite3_context*); /* ** CAPI3REF: Database Connection For Functions ** METHOD: sqlite3_context ** ** ^The sqlite3_context_db_handle() interface returns a copy of ** the pointer to the [database connection] (the 1st parameter) ** of the [sqlite3_create_function()] ** and [sqlite3_create_function16()] routines that originally ** registered the application defined function. */ SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*); /* ** CAPI3REF: Function Auxiliary Data ** METHOD: sqlite3_context ** ** These functions may be used by (non-aggregate) SQL functions to ** associate auxiliary data with argument values. If the same argument ** value is passed to multiple invocations of the same SQL function during ** query execution, under some circumstances the associated auxiliary data ** might be preserved. An example of where this might be useful is in a ** regular-expression matching function. The compiled version of the regular ** expression can be stored as auxiliary data associated with the pattern string. ** Then as long as the pattern string remains the same, ** the compiled regular expression can be reused on multiple ** invocations of the same function. ** ** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the auxiliary data ** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument ** value to the application-defined function. ^N is zero for the left-most ** function argument. ^If there is no auxiliary data ** associated with the function argument, the sqlite3_get_auxdata(C,N) interface ** returns a NULL pointer. ** ** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as auxiliary data for the ** N-th argument of the application-defined function. ^Subsequent ** calls to sqlite3_get_auxdata(C,N) return P from the most recent ** sqlite3_set_auxdata(C,N,P,X) call if the auxiliary data is still valid or ** NULL if the auxiliary data has been discarded. ** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL, ** SQLite will invoke the destructor function X with parameter P exactly ** once, when the auxiliary data is discarded. ** SQLite is free to discard the auxiliary data at any time, including:
    **
  • ^(when the corresponding function parameter changes)^, or **
  • ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the ** SQL statement)^, or **
  • ^(when sqlite3_set_auxdata() is invoked again on the same ** parameter)^, or **
  • ^(during the original sqlite3_set_auxdata() call when a memory ** allocation error occurs.)^ **
  • ^(during the original sqlite3_set_auxdata() call if the function ** is evaluated during query planning instead of during query execution, ** as sometimes happens with [SQLITE_ENABLE_STAT4].)^
** ** Note the last two bullets in particular. The destructor X in ** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the ** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata() ** should be called near the end of the function implementation and the ** function implementation should not make any use of P after ** sqlite3_set_auxdata() has been called. Furthermore, a call to ** sqlite3_get_auxdata() that occurs immediately after a corresponding call ** to sqlite3_set_auxdata() might still return NULL if an out-of-memory ** condition occurred during the sqlite3_set_auxdata() call or if the ** function is being evaluated during query planning rather than during ** query execution. ** ** ^(In practice, auxiliary data is preserved between function calls for ** function parameters that are compile-time constants, including literal ** values and [parameters] and expressions composed from the same.)^ ** ** The value of the N parameter to these interfaces should be non-negative. ** Future enhancements may make use of negative N values to define new ** kinds of function caching behavior. ** ** These routines must be called from the same thread in which ** the SQL function is running. ** ** See also: [sqlite3_get_clientdata()] and [sqlite3_set_clientdata()]. */ SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N); SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*)); /* ** CAPI3REF: Database Connection Client Data ** METHOD: sqlite3 ** ** These functions are used to associate one or more named pointers ** with a [database connection]. ** A call to sqlite3_set_clientdata(D,N,P,X) causes the pointer P ** to be attached to [database connection] D using name N. Subsequent ** calls to sqlite3_get_clientdata(D,N) will return a copy of pointer P ** or a NULL pointer if there were no prior calls to ** sqlite3_set_clientdata() with the same values of D and N. ** Names are compared using strcmp() and are thus case sensitive. ** ** If P and X are both non-NULL, then the destructor X is invoked with ** argument P on the first of the following occurrences: **
    **
  • An out-of-memory error occurs during the call to ** sqlite3_set_clientdata() which attempts to register pointer P. **
  • A subsequent call to sqlite3_set_clientdata(D,N,P,X) is made ** with the same D and N parameters. **
  • The database connection closes. SQLite does not make any guarantees ** about the order in which destructors are called, only that all ** destructors will be called exactly once at some point during the ** database connection closing process. **
** ** SQLite does not do anything with client data other than invoke ** destructors on the client data at the appropriate time. The intended ** use for client data is to provide a mechanism for wrapper libraries ** to store additional information about an SQLite database connection. ** ** There is no limit (other than available memory) on the number of different ** client data pointers (with different names) that can be attached to a ** single database connection. However, the implementation is optimized ** for the case of having only one or two different client data names. ** Applications and wrapper libraries are discouraged from using more than ** one client data name each. ** ** There is no way to enumerate the client data pointers ** associated with a database connection. The N parameter can be thought ** of as a secret key such that only code that knows the secret key is able ** to access the associated data. ** ** Security Warning: These interfaces should not be exposed in scripting ** languages or in other circumstances where it might be possible for an ** attacker to invoke them. Any agent that can invoke these interfaces ** can probably also take control of the process. ** ** Database connection client data is only available for SQLite ** version 3.44.0 ([dateof:3.44.0]) and later. ** ** See also: [sqlite3_set_auxdata()] and [sqlite3_get_auxdata()]. */ SQLITE_API void *sqlite3_get_clientdata(sqlite3*,const char*); SQLITE_API int sqlite3_set_clientdata(sqlite3*, const char*, void*, void(*)(void*)); /* ** CAPI3REF: Constants Defining Special Destructor Behavior ** ** These are special values for the destructor that is passed in as the ** final argument to routines like [sqlite3_result_blob()]. ^If the destructor ** argument is SQLITE_STATIC, it means that the content pointer is constant ** and will never change. It does not need to be destroyed. ^The ** SQLITE_TRANSIENT value means that the content will likely change in ** the near future and that SQLite should make its own private copy of ** the content before returning. ** ** The typedef is necessary to work around problems in certain ** C++ compilers. */ typedef void (*sqlite3_destructor_type)(void*); #define SQLITE_STATIC ((sqlite3_destructor_type)0) #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1) /* ** CAPI3REF: Setting The Result Of An SQL Function ** METHOD: sqlite3_context ** ** These routines are used by the xFunc or xFinal callbacks that ** implement SQL functions and aggregates. See ** [sqlite3_create_function()] and [sqlite3_create_function16()] ** for additional information. ** ** These functions work very much like the [parameter binding] family of ** functions used to bind values to host parameters in prepared statements. ** Refer to the [SQL parameter] documentation for additional information. ** ** ^The sqlite3_result_blob() interface sets the result from ** an application-defined function to be the BLOB whose content is pointed ** to by the second parameter and which is N bytes long where N is the ** third parameter. ** ** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N) ** interfaces set the result of the application-defined function to be ** a BLOB containing all zero bytes and N bytes in size. ** ** ^The sqlite3_result_double() interface sets the result from ** an application-defined function to be a floating point value specified ** by its 2nd argument. ** ** ^The sqlite3_result_error() and sqlite3_result_error16() functions ** cause the implemented SQL function to throw an exception. ** ^SQLite uses the string pointed to by the ** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16() ** as the text of an error message. ^SQLite interprets the error ** message string from sqlite3_result_error() as UTF-8. ^SQLite ** interprets the string from sqlite3_result_error16() as UTF-16 using ** the same [byte-order determination rules] as [sqlite3_bind_text16()]. ** ^If the third parameter to sqlite3_result_error() ** or sqlite3_result_error16() is negative then SQLite takes as the error ** message all text up through the first zero character. ** ^If the third parameter to sqlite3_result_error() or ** sqlite3_result_error16() is non-negative then SQLite takes that many ** bytes (not characters) from the 2nd parameter as the error message. ** ^The sqlite3_result_error() and sqlite3_result_error16() ** routines make a private copy of the error message text before ** they return. Hence, the calling function can deallocate or ** modify the text after they return without harm. ** ^The sqlite3_result_error_code() function changes the error code ** returned by SQLite as a result of an error in a function. ^By default, ** the error code is SQLITE_ERROR. ^A subsequent call to sqlite3_result_error() ** or sqlite3_result_error16() resets the error code to SQLITE_ERROR. ** ** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an ** error indicating that a string or BLOB is too long to represent. ** ** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an ** error indicating that a memory allocation failed. ** ** ^The sqlite3_result_int() interface sets the return value ** of the application-defined function to be the 32-bit signed integer ** value given in the 2nd argument. ** ^The sqlite3_result_int64() interface sets the return value ** of the application-defined function to be the 64-bit signed integer ** value given in the 2nd argument. ** ** ^The sqlite3_result_null() interface sets the return value ** of the application-defined function to be NULL. ** ** ^The sqlite3_result_text(), sqlite3_result_text16(), ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces ** set the return value of the application-defined function to be ** a text string which is represented as UTF-8, UTF-16 native byte order, ** UTF-16 little endian, or UTF-16 big endian, respectively. ** ^The sqlite3_result_text64() interface sets the return value of an ** application-defined function to be a text string in an encoding ** specified by the fifth (and last) parameter, which must be one ** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]. ** ^SQLite takes the text result from the application from ** the 2nd parameter of the sqlite3_result_text* interfaces. ** ^If the 3rd parameter to any of the sqlite3_result_text* interfaces ** other than sqlite3_result_text64() is negative, then SQLite computes ** the string length itself by searching the 2nd parameter for the first ** zero character. ** ^If the 3rd parameter to the sqlite3_result_text* interfaces ** is non-negative, then as many bytes (not characters) of the text ** pointed to by the 2nd parameter are taken as the application-defined ** function result. If the 3rd parameter is non-negative, then it ** must be the byte offset into the string where the NUL terminator would ** appear if the string were NUL terminated. If any NUL characters occur ** in the string at a byte offset that is less than the value of the 3rd ** parameter, then the resulting string will contain embedded NULs and the ** result of expressions operating on strings with embedded NULs is undefined. ** ^If the 4th parameter to the sqlite3_result_text* interfaces ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that ** function as the destructor on the text or BLOB result when it has ** finished using that result. ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to ** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite ** assumes that the text or BLOB result is in constant space and does not ** copy the content of the parameter nor call a destructor on the content ** when it has finished using that result. ** ^If the 4th parameter to the sqlite3_result_text* interfaces ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT ** then SQLite makes a copy of the result into space obtained ** from [sqlite3_malloc()] before it returns. ** ** ^For the sqlite3_result_text16(), sqlite3_result_text16le(), and ** sqlite3_result_text16be() routines, and for sqlite3_result_text64() ** when the encoding is not UTF8, if the input UTF16 begins with a ** byte-order mark (BOM, U+FEFF) then the BOM is removed from the ** string and the rest of the string is interpreted according to the ** byte-order specified by the BOM. ^The byte-order specified by ** the BOM at the beginning of the text overrides the byte-order ** specified by the interface procedure. ^So, for example, if ** sqlite3_result_text16le() is invoked with text that begins ** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the ** first two bytes of input are skipped and the remaining input ** is interpreted as UTF16BE text. ** ** ^For UTF16 input text to the sqlite3_result_text16(), ** sqlite3_result_text16be(), sqlite3_result_text16le(), and ** sqlite3_result_text64() routines, if the text contains invalid ** UTF16 characters, the invalid characters might be converted ** into the unicode replacement character, U+FFFD. ** ** ^The sqlite3_result_value() interface sets the result of ** the application-defined function to be a copy of the ** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The ** sqlite3_result_value() interface makes a copy of the [sqlite3_value] ** so that the [sqlite3_value] specified in the parameter may change or ** be deallocated after sqlite3_result_value() returns without harm. ** ^A [protected sqlite3_value] object may always be used where an ** [unprotected sqlite3_value] object is required, so either ** kind of [sqlite3_value] object can be used with this interface. ** ** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an ** SQL NULL value, just like [sqlite3_result_null(C)], except that it ** also associates the host-language pointer P or type T with that ** NULL value such that the pointer can be retrieved within an ** [application-defined SQL function] using [sqlite3_value_pointer()]. ** ^If the D parameter is not NULL, then it is a pointer to a destructor ** for the P parameter. ^SQLite invokes D with P as its only argument ** when SQLite is finished with P. The T parameter should be a static ** string and preferably a string literal. The sqlite3_result_pointer() ** routine is part of the [pointer passing interface] added for SQLite 3.20.0. ** ** If these routines are called from within a different thread ** than the one containing the application-defined function that received ** the [sqlite3_context] pointer, the results are undefined. */ SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*)); SQLITE_API void sqlite3_result_blob64(sqlite3_context*,const void*, sqlite3_uint64,void(*)(void*)); SQLITE_API void sqlite3_result_double(sqlite3_context*, double); SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int); SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int); SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*); SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*); SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int); SQLITE_API void sqlite3_result_int(sqlite3_context*, int); SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64); SQLITE_API void sqlite3_result_null(sqlite3_context*); SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*)); SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64, void(*)(void*), unsigned char encoding); SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*)); SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*)); SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*)); SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*); SQLITE_API void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*)); SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n); SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n); /* ** CAPI3REF: Setting The Subtype Of An SQL Function ** METHOD: sqlite3_context ** ** The sqlite3_result_subtype(C,T) function causes the subtype of ** the result from the [application-defined SQL function] with ** [sqlite3_context] C to be the value T. Only the lower 8 bits ** of the subtype T are preserved in current versions of SQLite; ** higher order bits are discarded. ** The number of subtype bytes preserved by SQLite might increase ** in future releases of SQLite. ** ** Every [application-defined SQL function] that invokes this interface ** should include the [SQLITE_RESULT_SUBTYPE] property in its ** text encoding argument when the SQL function is ** [sqlite3_create_function|registered]. If the [SQLITE_RESULT_SUBTYPE] ** property is omitted from the function that invokes sqlite3_result_subtype(), ** then in some cases the sqlite3_result_subtype() might fail to set ** the result subtype. ** ** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any ** SQL function that invokes the sqlite3_result_subtype() interface ** and that does not have the SQLITE_RESULT_SUBTYPE property will raise ** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1 ** by default. */ SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int); /* ** CAPI3REF: Define New Collating Sequences ** METHOD: sqlite3 ** ** ^These functions add, remove, or modify a [collation] associated ** with the [database connection] specified as the first argument. ** ** ^The name of the collation is a UTF-8 string ** for sqlite3_create_collation() and sqlite3_create_collation_v2() ** and a UTF-16 string in native byte order for sqlite3_create_collation16(). ** ^Collation names that compare equal according to [sqlite3_strnicmp()] are ** considered to be the same name. ** ** ^(The third argument (eTextRep) must be one of the constants: **
    **
  • [SQLITE_UTF8], **
  • [SQLITE_UTF16LE], **
  • [SQLITE_UTF16BE], **
  • [SQLITE_UTF16], or **
  • [SQLITE_UTF16_ALIGNED]. **
)^ ** ^The eTextRep argument determines the encoding of strings passed ** to the collating function callback, xCompare. ** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep ** force strings to be UTF16 with native byte order. ** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin ** on an even byte address. ** ** ^The fourth argument, pArg, is an application data pointer that is passed ** through as the first argument to the collating function callback. ** ** ^The fifth argument, xCompare, is a pointer to the collating function. ** ^Multiple collating functions can be registered using the same name but ** with different eTextRep parameters and SQLite will use whichever ** function requires the least amount of data transformation. ** ^If the xCompare argument is NULL then the collating function is ** deleted. ^When all collating functions having the same name are deleted, ** that collation is no longer usable. ** ** ^The collating function callback is invoked with a copy of the pArg ** application data pointer and with two strings in the encoding specified ** by the eTextRep argument. The two integer parameters to the collating ** function callback are the length of the two strings, in bytes. The collating ** function must return an integer that is negative, zero, or positive ** if the first string is less than, equal to, or greater than the second, ** respectively. A collating function must always return the same answer ** given the same inputs. If two or more collating functions are registered ** to the same collation name (using different eTextRep values) then all ** must give an equivalent answer when invoked with equivalent strings. ** The collating function must obey the following properties for all ** strings A, B, and C: ** **
    **
  1. If A==B then B==A. **
  2. If A==B and B==C then A==C. **
  3. If A<B THEN B>A. **
  4. If A<B and B<C then A<C. **
** ** If a collating function fails any of the above constraints and that ** collating function is registered and used, then the behavior of SQLite ** is undefined. ** ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation() ** with the addition that the xDestroy callback is invoked on pArg when ** the collating function is deleted. ** ^Collating functions are deleted when they are overridden by later ** calls to the collation creation functions or when the ** [database connection] is closed using [sqlite3_close()]. ** ** ^The xDestroy callback is not called if the ** sqlite3_create_collation_v2() function fails. Applications that invoke ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should ** check the return code and dispose of the application data pointer ** themselves rather than expecting SQLite to deal with it for them. ** This is different from every other SQLite interface. The inconsistency ** is unfortunate but cannot be changed without breaking backwards ** compatibility. ** ** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()]. */ SQLITE_API int sqlite3_create_collation( sqlite3*, const char *zName, int eTextRep, void *pArg, int(*xCompare)(void*,int,const void*,int,const void*) ); SQLITE_API int sqlite3_create_collation_v2( sqlite3*, const char *zName, int eTextRep, void *pArg, int(*xCompare)(void*,int,const void*,int,const void*), void(*xDestroy)(void*) ); SQLITE_API int sqlite3_create_collation16( sqlite3*, const void *zName, int eTextRep, void *pArg, int(*xCompare)(void*,int,const void*,int,const void*) ); /* ** CAPI3REF: Collation Needed Callbacks ** METHOD: sqlite3 ** ** ^To avoid having to register all collation sequences before a database ** can be used, a single callback function may be registered with the ** [database connection] to be invoked whenever an undefined collation ** sequence is required. ** ** ^If the function is registered using the sqlite3_collation_needed() API, ** then it is passed the names of undefined collation sequences as strings ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used, ** the names are passed as UTF-16 in machine native byte order. ** ^A call to either function replaces the existing collation-needed callback. ** ** ^(When the callback is invoked, the first argument passed is a copy ** of the second argument to sqlite3_collation_needed() or ** sqlite3_collation_needed16(). The second argument is the database ** connection. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE], ** or [SQLITE_UTF16LE], indicating the most desirable form of the collation ** sequence function required. The fourth parameter is the name of the ** required collation sequence.)^ ** ** The callback function should register the desired collation using ** [sqlite3_create_collation()], [sqlite3_create_collation16()], or ** [sqlite3_create_collation_v2()]. */ SQLITE_API int sqlite3_collation_needed( sqlite3*, void*, void(*)(void*,sqlite3*,int eTextRep,const char*) ); SQLITE_API int sqlite3_collation_needed16( sqlite3*, void*, void(*)(void*,sqlite3*,int eTextRep,const void*) ); #ifdef SQLITE_ENABLE_CEROD /* ** Specify the activation key for a CEROD database. Unless ** activated, none of the CEROD routines will work. */ SQLITE_API void sqlite3_activate_cerod( const char *zPassPhrase /* Activation phrase */ ); #endif /* ** CAPI3REF: Suspend Execution For A Short Time ** ** The sqlite3_sleep() function causes the current thread to suspend execution ** for at least a number of milliseconds specified in its parameter. ** ** If the operating system does not support sleep requests with ** millisecond time resolution, then the time will be rounded up to ** the nearest second. The number of milliseconds of sleep actually ** requested from the operating system is returned. ** ** ^SQLite implements this interface by calling the xSleep() ** method of the default [sqlite3_vfs] object. If the xSleep() method ** of the default VFS is not implemented correctly, or not implemented at ** all, then the behavior of sqlite3_sleep() may deviate from the description ** in the previous paragraphs. ** ** If a negative argument is passed to sqlite3_sleep() the results vary by ** VFS and operating system. Some system treat a negative argument as an ** instruction to sleep forever. Others understand it to mean do not sleep ** at all. ^In SQLite version 3.42.0 and later, a negative ** argument passed into sqlite3_sleep() is changed to zero before it is relayed ** down into the xSleep method of the VFS. */ SQLITE_API int sqlite3_sleep(int); /* ** CAPI3REF: Name Of The Folder Holding Temporary Files ** ** ^(If this global variable is made to point to a string which is ** the name of a folder (a.k.a. directory), then all temporary files ** created by SQLite when using a built-in [sqlite3_vfs | VFS] ** will be placed in that directory.)^ ^If this variable ** is a NULL pointer, then SQLite performs a search for an appropriate ** temporary file directory. ** ** Applications are strongly discouraged from using this global variable. ** It is required to set a temporary folder on Windows Runtime (WinRT). ** But for all other platforms, it is highly recommended that applications ** neither read nor write this variable. This global variable is a relic ** that exists for backwards compatibility of legacy applications and should ** be avoided in new projects. ** ** It is not safe to read or modify this variable in more than one ** thread at a time. It is not safe to read or modify this variable ** if a [database connection] is being used at the same time in a separate ** thread. ** It is intended that this variable be set once ** as part of process initialization and before any SQLite interface ** routines have been called and that this variable remain unchanged ** thereafter. ** ** ^The [temp_store_directory pragma] may modify this variable and cause ** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore, ** the [temp_store_directory pragma] always assumes that any string ** that this variable points to is held in memory obtained from ** [sqlite3_malloc] and the pragma may attempt to free that memory ** using [sqlite3_free]. ** Hence, if this variable is modified directly, either it should be ** made NULL or made to point to memory obtained from [sqlite3_malloc] ** or else the use of the [temp_store_directory pragma] should be avoided. ** Except when requested by the [temp_store_directory pragma], SQLite ** does not free the memory that sqlite3_temp_directory points to. If ** the application wants that memory to be freed, it must do ** so itself, taking care to only do so after all [database connection] ** objects have been destroyed. ** ** Note to Windows Runtime users: The temporary directory must be set ** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various ** features that require the use of temporary files may fail. Here is an ** example of how to do this using C++ with the Windows Runtime: ** **
** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
**       TemporaryFolder->Path->Data();
** char zPathBuf[MAX_PATH + 1];
** memset(zPathBuf, 0, sizeof(zPathBuf));
** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
**       NULL, NULL);
** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
** 
*/ SQLITE_API SQLITE_EXTERN char *sqlite3_temp_directory; /* ** CAPI3REF: Name Of The Folder Holding Database Files ** ** ^(If this global variable is made to point to a string which is ** the name of a folder (a.k.a. directory), then all database files ** specified with a relative pathname and created or accessed by ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed ** to be relative to that directory.)^ ^If this variable is a NULL ** pointer, then SQLite assumes that all database files specified ** with a relative pathname are relative to the current directory ** for the process. Only the windows VFS makes use of this global ** variable; it is ignored by the unix VFS. ** ** Changing the value of this variable while a database connection is ** open can result in a corrupt database. ** ** It is not safe to read or modify this variable in more than one ** thread at a time. It is not safe to read or modify this variable ** if a [database connection] is being used at the same time in a separate ** thread. ** It is intended that this variable be set once ** as part of process initialization and before any SQLite interface ** routines have been called and that this variable remain unchanged ** thereafter. ** ** ^The [data_store_directory pragma] may modify this variable and cause ** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore, ** the [data_store_directory pragma] always assumes that any string ** that this variable points to is held in memory obtained from ** [sqlite3_malloc] and the pragma may attempt to free that memory ** using [sqlite3_free]. ** Hence, if this variable is modified directly, either it should be ** made NULL or made to point to memory obtained from [sqlite3_malloc] ** or else the use of the [data_store_directory pragma] should be avoided. */ SQLITE_API SQLITE_EXTERN char *sqlite3_data_directory; /* ** CAPI3REF: Win32 Specific Interface ** ** These interfaces are available only on Windows. The ** [sqlite3_win32_set_directory] interface is used to set the value associated ** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to ** zValue, depending on the value of the type parameter. The zValue parameter ** should be NULL to cause the previous value to be freed via [sqlite3_free]; ** a non-NULL value will be copied into memory obtained from [sqlite3_malloc] ** prior to being used. The [sqlite3_win32_set_directory] interface returns ** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported, ** or [SQLITE_NOMEM] if memory could not be allocated. The value of the ** [sqlite3_data_directory] variable is intended to act as a replacement for ** the current directory on the sub-platforms of Win32 where that concept is ** not present, e.g. WinRT and UWP. The [sqlite3_win32_set_directory8] and ** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the ** sqlite3_win32_set_directory interface except the string parameter must be ** UTF-8 or UTF-16, respectively. */ SQLITE_API int sqlite3_win32_set_directory( unsigned long type, /* Identifier for directory being set or reset */ void *zValue /* New value for directory being set or reset */ ); SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue); SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue); /* ** CAPI3REF: Win32 Directory Types ** ** These macros are only available on Windows. They define the allowed values ** for the type argument to the [sqlite3_win32_set_directory] interface. */ #define SQLITE_WIN32_DATA_DIRECTORY_TYPE 1 #define SQLITE_WIN32_TEMP_DIRECTORY_TYPE 2 /* ** CAPI3REF: Test For Auto-Commit Mode ** KEYWORDS: {autocommit mode} ** METHOD: sqlite3 ** ** ^The sqlite3_get_autocommit() interface returns non-zero or ** zero if the given database connection is or is not in autocommit mode, ** respectively. ^Autocommit mode is on by default. ** ^Autocommit mode is disabled by a [BEGIN] statement. ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK]. ** ** If certain kinds of errors occur on a statement within a multi-statement ** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR], ** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the ** transaction might be rolled back automatically. The only way to ** find out whether SQLite automatically rolled back the transaction after ** an error is to use this function. ** ** If another thread changes the autocommit status of the database ** connection while this routine is running, then the return value ** is undefined. */ SQLITE_API int sqlite3_get_autocommit(sqlite3*); /* ** CAPI3REF: Find The Database Handle Of A Prepared Statement ** METHOD: sqlite3_stmt ** ** ^The sqlite3_db_handle interface returns the [database connection] handle ** to which a [prepared statement] belongs. ^The [database connection] ** returned by sqlite3_db_handle is the same [database connection] ** that was the first argument ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to ** create the statement in the first place. */ SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*); /* ** CAPI3REF: Return The Schema Name For A Database Connection ** METHOD: sqlite3 ** ** ^The sqlite3_db_name(D,N) interface returns a pointer to the schema name ** for the N-th database on database connection D, or a NULL pointer if N is ** out of range. An N value of 0 means the main database file. An N of 1 is ** the "temp" schema. Larger values of N correspond to various ATTACH-ed ** databases. ** ** Space to hold the string that is returned by sqlite3_db_name() is managed ** by SQLite itself. The string might be deallocated by any operation that ** changes the schema, including [ATTACH] or [DETACH] or calls to ** [sqlite3_serialize()] or [sqlite3_deserialize()], even operations that ** occur on a different thread. Applications that need to ** remember the string long-term should make their own copy. Applications that ** are accessing the same database connection simultaneously on multiple ** threads should mutex-protect calls to this API and should make their own ** private copy of the result prior to releasing the mutex. */ SQLITE_API const char *sqlite3_db_name(sqlite3 *db, int N); /* ** CAPI3REF: Return The Filename For A Database Connection ** METHOD: sqlite3 ** ** ^The sqlite3_db_filename(D,N) interface returns a pointer to the filename ** associated with database N of connection D. ** ^If there is no attached database N on the database ** connection D, or if database N is a temporary or in-memory database, then ** this function will return either a NULL pointer or an empty string. ** ** ^The string value returned by this routine is owned and managed by ** the database connection. ^The value will be valid until the database N ** is [DETACH]-ed or until the database connection closes. ** ** ^The filename returned by this function is the output of the ** xFullPathname method of the [VFS]. ^In other words, the filename ** will be an absolute pathname, even if the filename used ** to open the database originally was a URI or relative pathname. ** ** If the filename pointer returned by this routine is not NULL, then it ** can be used as the filename input parameter to these routines: **
    **
  • [sqlite3_uri_parameter()] **
  • [sqlite3_uri_boolean()] **
  • [sqlite3_uri_int64()] **
  • [sqlite3_filename_database()] **
  • [sqlite3_filename_journal()] **
  • [sqlite3_filename_wal()] **
*/ SQLITE_API sqlite3_filename sqlite3_db_filename(sqlite3 *db, const char *zDbName); /* ** CAPI3REF: Determine if a database is read-only ** METHOD: sqlite3 ** ** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N ** of connection D is read-only, 0 if it is read/write, or -1 if N is not ** the name of a database on connection D. */ SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName); /* ** CAPI3REF: Determine the transaction state of a database ** METHOD: sqlite3 ** ** ^The sqlite3_txn_state(D,S) interface returns the current ** [transaction state] of schema S in database connection D. ^If S is NULL, ** then the highest transaction state of any schema on database connection D ** is returned. Transaction states are (in order of lowest to highest): **
    **
  1. SQLITE_TXN_NONE **
  2. SQLITE_TXN_READ **
  3. SQLITE_TXN_WRITE **
** ^If the S argument to sqlite3_txn_state(D,S) is not the name of ** a valid schema, then -1 is returned. */ SQLITE_API int sqlite3_txn_state(sqlite3*,const char *zSchema); /* ** CAPI3REF: Allowed return values from sqlite3_txn_state() ** KEYWORDS: {transaction state} ** ** These constants define the current transaction state of a database file. ** ^The [sqlite3_txn_state(D,S)] interface returns one of these ** constants in order to describe the transaction state of schema S ** in [database connection] D. ** **
** [[SQLITE_TXN_NONE]]
SQLITE_TXN_NONE
**
The SQLITE_TXN_NONE state means that no transaction is currently ** pending.
** ** [[SQLITE_TXN_READ]]
SQLITE_TXN_READ
**
The SQLITE_TXN_READ state means that the database is currently ** in a read transaction. Content has been read from the database file ** but nothing in the database file has changed. The transaction state ** will be advanced to SQLITE_TXN_WRITE if any changes occur and there are ** no other conflicting concurrent write transactions. The transaction ** state will revert to SQLITE_TXN_NONE following a [ROLLBACK] or ** [COMMIT].
** ** [[SQLITE_TXN_WRITE]]
SQLITE_TXN_WRITE
**
The SQLITE_TXN_WRITE state means that the database is currently ** in a write transaction. Content has been written to the database file ** but has not yet committed. The transaction state will change to ** SQLITE_TXN_NONE at the next [ROLLBACK] or [COMMIT].
*/ #define SQLITE_TXN_NONE 0 #define SQLITE_TXN_READ 1 #define SQLITE_TXN_WRITE 2 /* ** CAPI3REF: Find the next prepared statement ** METHOD: sqlite3 ** ** ^This interface returns a pointer to the next [prepared statement] after ** pStmt associated with the [database connection] pDb. ^If pStmt is NULL ** then this interface returns a pointer to the first prepared statement ** associated with the database connection pDb. ^If no prepared statement ** satisfies the conditions of this routine, it returns NULL. ** ** The [database connection] pointer D in a call to ** [sqlite3_next_stmt(D,S)] must refer to an open database ** connection and in particular must not be a NULL pointer. */ SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt); /* ** CAPI3REF: Commit And Rollback Notification Callbacks ** METHOD: sqlite3 ** ** ^The sqlite3_commit_hook() interface registers a callback ** function to be invoked whenever a transaction is [COMMIT | committed]. ** ^Any callback set by a previous call to sqlite3_commit_hook() ** for the same database connection is overridden. ** ^The sqlite3_rollback_hook() interface registers a callback ** function to be invoked whenever a transaction is [ROLLBACK | rolled back]. ** ^Any callback set by a previous call to sqlite3_rollback_hook() ** for the same database connection is overridden. ** ^The pArg argument is passed through to the callback. ** ^If the callback on a commit hook function returns non-zero, ** then the commit is converted into a rollback. ** ** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions ** return the P argument from the previous call of the same function ** on the same [database connection] D, or NULL for ** the first call for each function on D. ** ** The commit and rollback hook callbacks are not reentrant. ** The callback implementation must not do anything that will modify ** the database connection that invoked the callback. Any actions ** to modify the database connection must be deferred until after the ** completion of the [sqlite3_step()] call that triggered the commit ** or rollback hook in the first place. ** Note that running any other SQL statements, including SELECT statements, ** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify ** the database connections for the meaning of "modify" in this paragraph. ** ** ^Registering a NULL function disables the callback. ** ** ^When the commit hook callback routine returns zero, the [COMMIT] ** operation is allowed to continue normally. ^If the commit hook ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK]. ** ^The rollback hook is invoked on a rollback that results from a commit ** hook returning non-zero, just as it would be with any other rollback. ** ** ^For the purposes of this API, a transaction is said to have been ** rolled back if an explicit "ROLLBACK" statement is executed, or ** an error or constraint causes an implicit rollback to occur. ** ^The rollback callback is not invoked if a transaction is ** automatically rolled back because the database connection is closed. ** ** See also the [sqlite3_update_hook()] interface. */ SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); /* ** CAPI3REF: Autovacuum Compaction Amount Callback ** METHOD: sqlite3 ** ** ^The sqlite3_autovacuum_pages(D,C,P,X) interface registers a callback ** function C that is invoked prior to each autovacuum of the database ** file. ^The callback is passed a copy of the generic data pointer (P), ** the schema-name of the attached database that is being autovacuumed, ** the size of the database file in pages, the number of free pages, ** and the number of bytes per page, respectively. The callback should ** return the number of free pages that should be removed by the ** autovacuum. ^If the callback returns zero, then no autovacuum happens. ** ^If the value returned is greater than or equal to the number of ** free pages, then a complete autovacuum happens. ** **

^If there are multiple ATTACH-ed database files that are being ** modified as part of a transaction commit, then the autovacuum pages ** callback is invoked separately for each file. ** **

The callback is not reentrant. The callback function should ** not attempt to invoke any other SQLite interface. If it does, bad ** things may happen, including segmentation faults and corrupt database ** files. The callback function should be a simple function that ** does some arithmetic on its input parameters and returns a result. ** ** ^The X parameter to sqlite3_autovacuum_pages(D,C,P,X) is an optional ** destructor for the P parameter. ^If X is not NULL, then X(P) is ** invoked whenever the database connection closes or when the callback ** is overwritten by another invocation of sqlite3_autovacuum_pages(). ** **

^There is only one autovacuum pages callback per database connection. ** ^Each call to the sqlite3_autovacuum_pages() interface overrides all ** previous invocations for that database connection. ^If the callback ** argument (C) to sqlite3_autovacuum_pages(D,C,P,X) is a NULL pointer, ** then the autovacuum steps callback is canceled. The return value ** from sqlite3_autovacuum_pages() is normally SQLITE_OK, but might ** be some other error code if something goes wrong. The current ** implementation will only return SQLITE_OK or SQLITE_MISUSE, but other ** return codes might be added in future releases. ** **

If no autovacuum pages callback is specified (the usual case) or ** a NULL pointer is provided for the callback, ** then the default behavior is to vacuum all free pages. So, in other ** words, the default behavior is the same as if the callback function ** were something like this: ** **

**     unsigned int demonstration_autovac_pages_callback(
**       void *pClientData,
**       const char *zSchema,
**       unsigned int nDbPage,
**       unsigned int nFreePage,
**       unsigned int nBytePerPage
**     ){
**       return nFreePage;
**     }
** 
*/ SQLITE_API int sqlite3_autovacuum_pages( sqlite3 *db, unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int), void*, void(*)(void*) ); /* ** CAPI3REF: Data Change Notification Callbacks ** METHOD: sqlite3 ** ** ^The sqlite3_update_hook() interface registers a callback function ** with the [database connection] identified by the first argument ** to be invoked whenever a row is updated, inserted or deleted in ** a [rowid table]. ** ^Any callback set by a previous call to this function ** for the same database connection is overridden. ** ** ^The second argument is a pointer to the function to invoke when a ** row is updated, inserted or deleted in a rowid table. ** ^The update hook is disabled by invoking sqlite3_update_hook() ** with a NULL pointer as the second parameter. ** ^The first argument to the callback is a copy of the third argument ** to sqlite3_update_hook(). ** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE], ** or [SQLITE_UPDATE], depending on the operation that caused the callback ** to be invoked. ** ^The third and fourth arguments to the callback contain pointers to the ** database and table name containing the affected row. ** ^The final callback parameter is the [rowid] of the row. ** ^In the case of an update, this is the [rowid] after the update takes place. ** ** ^(The update hook is not invoked when internal system tables are ** modified (i.e. sqlite_sequence).)^ ** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified. ** ** ^In the current implementation, the update hook ** is not invoked when conflicting rows are deleted because of an ** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook ** invoked when rows are deleted using the [truncate optimization]. ** The exceptions defined in this paragraph might change in a future ** release of SQLite. ** ** Whether the update hook is invoked before or after the ** corresponding change is currently unspecified and may differ ** depending on the type of change. Do not rely on the order of the ** hook call with regards to the final result of the operation which ** triggers the hook. ** ** The update hook implementation must not do anything that will modify ** the database connection that invoked the update hook. Any actions ** to modify the database connection must be deferred until after the ** completion of the [sqlite3_step()] call that triggered the update hook. ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their ** database connections for the meaning of "modify" in this paragraph. ** ** ^The sqlite3_update_hook(D,C,P) function ** returns the P argument from the previous call ** on the same [database connection] D, or NULL for ** the first call on D. ** ** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()], ** and [sqlite3_preupdate_hook()] interfaces. */ SQLITE_API void *sqlite3_update_hook( sqlite3*, void(*)(void *,int ,char const *,char const *,sqlite3_int64), void* ); /* ** CAPI3REF: Enable Or Disable Shared Pager Cache ** ** ^(This routine enables or disables the sharing of the database cache ** and schema data structures between [database connection | connections] ** to the same database. Sharing is enabled if the argument is true ** and disabled if the argument is false.)^ ** ** This interface is omitted if SQLite is compiled with ** [-DSQLITE_OMIT_SHARED_CACHE]. The [-DSQLITE_OMIT_SHARED_CACHE] ** compile-time option is recommended because the ** [use of shared cache mode is discouraged]. ** ** ^Cache sharing is enabled and disabled for an entire process. ** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]). ** In prior versions of SQLite, ** sharing was enabled or disabled for each thread separately. ** ** ^(The cache sharing mode set by this interface effects all subsequent ** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()]. ** Existing database connections continue to use the sharing mode ** that was in effect at the time they were opened.)^ ** ** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled ** successfully. An [error code] is returned otherwise.)^ ** ** ^Shared cache is disabled by default. It is recommended that it stay ** that way. In other words, do not use this routine. This interface ** continues to be provided for historical compatibility, but its use is ** discouraged. Any use of shared cache is discouraged. If shared cache ** must be used, it is recommended that shared cache only be enabled for ** individual database connections using the [sqlite3_open_v2()] interface ** with the [SQLITE_OPEN_SHAREDCACHE] flag. ** ** Note: This method is disabled on MacOS X 10.7 and iOS version 5.0 ** and will always return SQLITE_MISUSE. On those systems, ** shared cache mode should be enabled per-database connection via ** [sqlite3_open_v2()] with [SQLITE_OPEN_SHAREDCACHE]. ** ** This interface is threadsafe on processors where writing a ** 32-bit integer is atomic. ** ** See Also: [SQLite Shared-Cache Mode] */ SQLITE_API int sqlite3_enable_shared_cache(int); /* ** CAPI3REF: Attempt To Free Heap Memory ** ** ^The sqlite3_release_memory() interface attempts to free N bytes ** of heap memory by deallocating non-essential memory allocations ** held by the database library. Memory used to cache database ** pages to improve performance is an example of non-essential memory. ** ^sqlite3_release_memory() returns the number of bytes actually freed, ** which might be more or less than the amount requested. ** ^The sqlite3_release_memory() routine is a no-op returning zero ** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT]. ** ** See also: [sqlite3_db_release_memory()] */ SQLITE_API int sqlite3_release_memory(int); /* ** CAPI3REF: Free Memory Used By A Database Connection ** METHOD: sqlite3 ** ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap ** memory as possible from database connection D. Unlike the ** [sqlite3_release_memory()] interface, this interface is in effect even ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is ** omitted. ** ** See also: [sqlite3_release_memory()] */ SQLITE_API int sqlite3_db_release_memory(sqlite3*); /* ** CAPI3REF: Impose A Limit On Heap Size ** ** These interfaces impose limits on the amount of heap memory that will be ** used by all database connections within a single process. ** ** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the ** soft limit on the amount of heap memory that may be allocated by SQLite. ** ^SQLite strives to keep heap memory utilization below the soft heap ** limit by reducing the number of pages held in the page cache ** as heap memory usages approaches the limit. ** ^The soft heap limit is "soft" because even though SQLite strives to stay ** below the limit, it will exceed the limit rather than generate ** an [SQLITE_NOMEM] error. In other words, the soft heap limit ** is advisory only. ** ** ^The sqlite3_hard_heap_limit64(N) interface sets a hard upper bound of ** N bytes on the amount of memory that will be allocated. ^The ** sqlite3_hard_heap_limit64(N) interface is similar to ** sqlite3_soft_heap_limit64(N) except that memory allocations will fail ** when the hard heap limit is reached. ** ** ^The return value from both sqlite3_soft_heap_limit64() and ** sqlite3_hard_heap_limit64() is the size of ** the heap limit prior to the call, or negative in the case of an ** error. ^If the argument N is negative ** then no change is made to the heap limit. Hence, the current ** size of heap limits can be determined by invoking ** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1). ** ** ^Setting the heap limits to zero disables the heap limiter mechanism. ** ** ^The soft heap limit may not be greater than the hard heap limit. ** ^If the hard heap limit is enabled and if sqlite3_soft_heap_limit(N) ** is invoked with a value of N that is greater than the hard heap limit, ** the soft heap limit is set to the value of the hard heap limit. ** ^The soft heap limit is automatically enabled whenever the hard heap ** limit is enabled. ^When sqlite3_hard_heap_limit64(N) is invoked and ** the soft heap limit is outside the range of 1..N, then the soft heap ** limit is set to N. ^Invoking sqlite3_soft_heap_limit64(0) when the ** hard heap limit is enabled makes the soft heap limit equal to the ** hard heap limit. ** ** The memory allocation limits can also be adjusted using ** [PRAGMA soft_heap_limit] and [PRAGMA hard_heap_limit]. ** ** ^(The heap limits are not enforced in the current implementation ** if one or more of following conditions are true: ** **
    **
  • The limit value is set to zero. **
  • Memory accounting is disabled using a combination of the ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option. **
  • An alternative page cache implementation is specified using ** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...). **
  • The page cache allocates from its own memory pool supplied ** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than ** from the heap. **
)^ ** ** The circumstances under which SQLite will enforce the heap limits may ** change in future releases of SQLite. */ SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N); SQLITE_API sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 N); /* ** CAPI3REF: Deprecated Soft Heap Limit Interface ** DEPRECATED ** ** This is a deprecated version of the [sqlite3_soft_heap_limit64()] ** interface. This routine is provided for historical compatibility ** only. All new applications should use the ** [sqlite3_soft_heap_limit64()] interface rather than this one. */ SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N); /* ** CAPI3REF: Extract Metadata About A Column Of A Table ** METHOD: sqlite3 ** ** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns ** information about column C of table T in database D ** on [database connection] X.)^ ^The sqlite3_table_column_metadata() ** interface returns SQLITE_OK and fills in the non-NULL pointers in ** the final five arguments with appropriate values if the specified ** column exists. ^The sqlite3_table_column_metadata() interface returns ** SQLITE_ERROR if the specified column does not exist. ** ^If the column-name parameter to sqlite3_table_column_metadata() is a ** NULL pointer, then this routine simply checks for the existence of the ** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it ** does not. If the table name parameter T in a call to ** sqlite3_table_column_metadata(X,D,T,C,...) is NULL then the result is ** undefined behavior. ** ** ^The column is identified by the second, third and fourth parameters to ** this function. ^(The second parameter is either the name of the database ** (i.e. "main", "temp", or an attached database) containing the specified ** table or NULL.)^ ^If it is NULL, then all attached databases are searched ** for the table using the same algorithm used by the database engine to ** resolve unqualified table references. ** ** ^The third and fourth parameters to this function are the table and column ** name of the desired column, respectively. ** ** ^Metadata is returned by writing to the memory locations passed as the 5th ** and subsequent parameters to this function. ^Any of these arguments may be ** NULL, in which case the corresponding element of metadata is omitted. ** ** ^(
** **
Parameter Output
Type
Description ** **
5th const char* Data type **
6th const char* Name of default collation sequence **
7th int True if column has a NOT NULL constraint **
8th int True if column is part of the PRIMARY KEY **
9th int True if column is [AUTOINCREMENT] **
**
)^ ** ** ^The memory pointed to by the character pointers returned for the ** declaration type and collation sequence is valid until the next ** call to any SQLite API function. ** ** ^If the specified table is actually a view, an [error code] is returned. ** ** ^If the specified column is "rowid", "oid" or "_rowid_" and the table ** is not a [WITHOUT ROWID] table and an ** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output ** parameters are set for the explicitly declared column. ^(If there is no ** [INTEGER PRIMARY KEY] column, then the outputs ** for the [rowid] are set as follows: ** **
**     data type: "INTEGER"
**     collation sequence: "BINARY"
**     not null: 0
**     primary key: 1
**     auto increment: 0
** 
)^ ** ** ^This function causes all database schemas to be read from disk and ** parsed, if that has not already been done, and returns an error if ** any errors are encountered while loading the schema. */ SQLITE_API int sqlite3_table_column_metadata( sqlite3 *db, /* Connection handle */ const char *zDbName, /* Database name or NULL */ const char *zTableName, /* Table name */ const char *zColumnName, /* Column name */ char const **pzDataType, /* OUTPUT: Declared data type */ char const **pzCollSeq, /* OUTPUT: Collation sequence name */ int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ int *pPrimaryKey, /* OUTPUT: True if column part of PK */ int *pAutoinc /* OUTPUT: True if column is auto-increment */ ); /* ** CAPI3REF: Load An Extension ** METHOD: sqlite3 ** ** ^This interface loads an SQLite extension library from the named file. ** ** ^The sqlite3_load_extension() interface attempts to load an ** [SQLite extension] library contained in the file zFile. If ** the file cannot be loaded directly, attempts are made to load ** with various operating-system specific extensions added. ** So for example, if "samplelib" cannot be loaded, then names like ** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might ** be tried also. ** ** ^The entry point is zProc. ** ^(zProc may be 0, in which case SQLite will try to come up with an ** entry point name on its own. It first tries "sqlite3_extension_init". ** If that does not work, it constructs a name "sqlite3_X_init" where ** X consists of the lower-case equivalent of all ASCII alphabetic ** characters in the filename from the last "/" to the first following ** "." and omitting any initial "lib".)^ ** ^The sqlite3_load_extension() interface returns ** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong. ** ^If an error occurs and pzErrMsg is not 0, then the ** [sqlite3_load_extension()] interface shall attempt to ** fill *pzErrMsg with error message text stored in memory ** obtained from [sqlite3_malloc()]. The calling function ** should free this memory by calling [sqlite3_free()]. ** ** ^Extension loading must be enabled using ** [sqlite3_enable_load_extension()] or ** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL) ** prior to calling this API, ** otherwise an error will be returned. ** ** Security warning: It is recommended that the ** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this ** interface. The use of the [sqlite3_enable_load_extension()] interface ** should be avoided. This will keep the SQL function [load_extension()] ** disabled and prevent SQL injections from giving attackers ** access to extension loading capabilities. ** ** See also the [load_extension() SQL function]. */ SQLITE_API int sqlite3_load_extension( sqlite3 *db, /* Load the extension into this database connection */ const char *zFile, /* Name of the shared library containing extension */ const char *zProc, /* Entry point. Derived from zFile if 0 */ char **pzErrMsg /* Put error message here if not 0 */ ); /* ** CAPI3REF: Enable Or Disable Extension Loading ** METHOD: sqlite3 ** ** ^So as not to open security holes in older applications that are ** unprepared to deal with [extension loading], and as a means of disabling ** [extension loading] while evaluating user-entered SQL, the following API ** is provided to turn the [sqlite3_load_extension()] mechanism on and off. ** ** ^Extension loading is off by default. ** ^Call the sqlite3_enable_load_extension() routine with onoff==1 ** to turn extension loading on and call it with onoff==0 to turn ** it back off again. ** ** ^This interface enables or disables both the C-API ** [sqlite3_load_extension()] and the SQL function [load_extension()]. ** ^(Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..) ** to enable or disable only the C-API.)^ ** ** Security warning: It is recommended that extension loading ** be enabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method ** rather than this interface, so the [load_extension()] SQL function ** remains disabled. This will prevent SQL injections from giving attackers ** access to extension loading capabilities. */ SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff); /* ** CAPI3REF: Automatically Load Statically Linked Extensions ** ** ^This interface causes the xEntryPoint() function to be invoked for ** each new [database connection] that is created. The idea here is that ** xEntryPoint() is the entry point for a statically linked [SQLite extension] ** that is to be automatically loaded into all new database connections. ** ** ^(Even though the function prototype shows that xEntryPoint() takes ** no arguments and returns void, SQLite invokes xEntryPoint() with three ** arguments and expects an integer result as if the signature of the ** entry point were as follows: ** **
**    int xEntryPoint(
**      sqlite3 *db,
**      const char **pzErrMsg,
**      const struct sqlite3_api_routines *pThunk
**    );
** 
)^ ** ** If the xEntryPoint routine encounters an error, it should make *pzErrMsg ** point to an appropriate error message (obtained from [sqlite3_mprintf()]) ** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg ** is NULL before calling the xEntryPoint(). ^SQLite will invoke ** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any ** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()], ** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail. ** ** ^Calling sqlite3_auto_extension(X) with an entry point X that is already ** on the list of automatic extensions is a harmless no-op. ^No entry point ** will be called more than once for each database connection that is opened. ** ** See also: [sqlite3_reset_auto_extension()] ** and [sqlite3_cancel_auto_extension()] */ SQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void)); /* ** CAPI3REF: Cancel Automatic Extension Loading ** ** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the ** initialization routine X that was registered using a prior call to ** [sqlite3_auto_extension(X)]. ^The [sqlite3_cancel_auto_extension(X)] ** routine returns 1 if initialization routine X was successfully ** unregistered and it returns 0 if X was not on the list of initialization ** routines. */ SQLITE_API int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void)); /* ** CAPI3REF: Reset Automatic Extension Loading ** ** ^This interface disables all automatic extensions previously ** registered using [sqlite3_auto_extension()]. */ SQLITE_API void sqlite3_reset_auto_extension(void); /* ** Structures used by the virtual table interface */ typedef struct sqlite3_vtab sqlite3_vtab; typedef struct sqlite3_index_info sqlite3_index_info; typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor; typedef struct sqlite3_module sqlite3_module; /* ** CAPI3REF: Virtual Table Object ** KEYWORDS: sqlite3_module {virtual table module} ** ** This structure, sometimes called a "virtual table module", ** defines the implementation of a [virtual table]. ** This structure consists mostly of methods for the module. ** ** ^A virtual table module is created by filling in a persistent ** instance of this structure and passing a pointer to that instance ** to [sqlite3_create_module()] or [sqlite3_create_module_v2()]. ** ^The registration remains valid until it is replaced by a different ** module or until the [database connection] closes. The content ** of this structure must not change while it is registered with ** any database connection. */ struct sqlite3_module { int iVersion; int (*xCreate)(sqlite3*, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVTab, char**); int (*xConnect)(sqlite3*, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVTab, char**); int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*); int (*xDisconnect)(sqlite3_vtab *pVTab); int (*xDestroy)(sqlite3_vtab *pVTab); int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor); int (*xClose)(sqlite3_vtab_cursor*); int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr, int argc, sqlite3_value **argv); int (*xNext)(sqlite3_vtab_cursor*); int (*xEof)(sqlite3_vtab_cursor*); int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int); int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid); int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *); int (*xBegin)(sqlite3_vtab *pVTab); int (*xSync)(sqlite3_vtab *pVTab); int (*xCommit)(sqlite3_vtab *pVTab); int (*xRollback)(sqlite3_vtab *pVTab); int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName, void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), void **ppArg); int (*xRename)(sqlite3_vtab *pVtab, const char *zNew); /* The methods above are in version 1 of the sqlite_module object. Those ** below are for version 2 and greater. */ int (*xSavepoint)(sqlite3_vtab *pVTab, int); int (*xRelease)(sqlite3_vtab *pVTab, int); int (*xRollbackTo)(sqlite3_vtab *pVTab, int); /* The methods above are in versions 1 and 2 of the sqlite_module object. ** Those below are for version 3 and greater. */ int (*xShadowName)(const char*); /* The methods above are in versions 1 through 3 of the sqlite_module object. ** Those below are for version 4 and greater. */ int (*xIntegrity)(sqlite3_vtab *pVTab, const char *zSchema, const char *zTabName, int mFlags, char **pzErr); }; /* ** CAPI3REF: Virtual Table Indexing Information ** KEYWORDS: sqlite3_index_info ** ** The sqlite3_index_info structure and its substructures is used as part ** of the [virtual table] interface to ** pass information into and receive the reply from the [xBestIndex] ** method of a [virtual table module]. The fields under **Inputs** are the ** inputs to xBestIndex and are read-only. xBestIndex inserts its ** results into the **Outputs** fields. ** ** ^(The aConstraint[] array records WHERE clause constraints of the form: ** **
column OP expr
** ** where OP is =, <, <=, >, or >=.)^ ^(The particular operator is ** stored in aConstraint[].op using one of the ** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^ ** ^(The index of the column is stored in ** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the ** expr on the right-hand side can be evaluated (and thus the constraint ** is usable) and false if it cannot.)^ ** ** ^The optimizer automatically inverts terms of the form "expr OP column" ** and makes other simplifications to the WHERE clause in an attempt to ** get as many WHERE clause terms into the form shown above as possible. ** ^The aConstraint[] array only reports WHERE clause terms that are ** relevant to the particular virtual table being queried. ** ** ^Information about the ORDER BY clause is stored in aOrderBy[]. ** ^Each term of aOrderBy records a column of the ORDER BY clause. ** ** The colUsed field indicates which columns of the virtual table may be ** required by the current scan. Virtual table columns are numbered from ** zero in the order in which they appear within the CREATE TABLE statement ** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62), ** the corresponding bit is set within the colUsed mask if the column may be ** required by SQLite. If the table has at least 64 columns and any column ** to the right of the first 63 is required, then bit 63 of colUsed is also ** set. In other words, column iCol may be required if the expression ** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to ** non-zero. ** ** The [xBestIndex] method must fill aConstraintUsage[] with information ** about what parameters to pass to xFilter. ^If argvIndex>0 then ** the right-hand side of the corresponding aConstraint[] is evaluated ** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit ** is true, then the constraint is assumed to be fully handled by the ** virtual table and might not be checked again by the byte code.)^ ^(The ** aConstraintUsage[].omit flag is an optimization hint. When the omit flag ** is left in its default setting of false, the constraint will always be ** checked separately in byte code. If the omit flag is changed to true, then ** the constraint may or may not be checked in byte code. In other words, ** when the omit flag is true there is no guarantee that the constraint will ** not be checked again using byte code.)^ ** ** ^The idxNum and idxStr values are recorded and passed into the ** [xFilter] method. ** ^[sqlite3_free()] is used to free idxStr if and only if ** needToFreeIdxStr is true. ** ** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in ** the correct order to satisfy the ORDER BY clause so that no separate ** sorting step is required. ** ** ^The estimatedCost value is an estimate of the cost of a particular ** strategy. A cost of N indicates that the cost of the strategy is similar ** to a linear scan of an SQLite table with N rows. A cost of log(N) ** indicates that the expense of the operation is similar to that of a ** binary search on a unique indexed field of an SQLite table with N rows. ** ** ^The estimatedRows value is an estimate of the number of rows that ** will be returned by the strategy. ** ** The xBestIndex method may optionally populate the idxFlags field with a ** mask of SQLITE_INDEX_SCAN_* flags. One such flag is ** [SQLITE_INDEX_SCAN_HEX], which if set causes the [EXPLAIN QUERY PLAN] ** output to show the idxNum as hex instead of as decimal. Another flag is ** SQLITE_INDEX_SCAN_UNIQUE, which if set indicates that the query plan will ** return at most one row. ** ** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then ** SQLite also assumes that if a call to the xUpdate() method is made as ** part of the same statement to delete or update a virtual table row and the ** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback ** any database changes. In other words, if the xUpdate() returns ** SQLITE_CONSTRAINT, the database contents must be exactly as they were ** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not ** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by ** the xUpdate method are automatically rolled back by SQLite. ** ** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info ** structure for SQLite [version 3.8.2] ([dateof:3.8.2]). ** If a virtual table extension is ** used with an SQLite version earlier than 3.8.2, the results of attempting ** to read or write the estimatedRows field are undefined (but are likely ** to include crashing the application). The estimatedRows field should ** therefore only be used if [sqlite3_libversion_number()] returns a ** value greater than or equal to 3008002. Similarly, the idxFlags field ** was added for [version 3.9.0] ([dateof:3.9.0]). ** It may therefore only be used if ** sqlite3_libversion_number() returns a value greater than or equal to ** 3009000. */ struct sqlite3_index_info { /* Inputs */ int nConstraint; /* Number of entries in aConstraint */ struct sqlite3_index_constraint { int iColumn; /* Column constrained. -1 for ROWID */ unsigned char op; /* Constraint operator */ unsigned char usable; /* True if this constraint is usable */ int iTermOffset; /* Used internally - xBestIndex should ignore */ } *aConstraint; /* Table of WHERE clause constraints */ int nOrderBy; /* Number of terms in the ORDER BY clause */ struct sqlite3_index_orderby { int iColumn; /* Column number */ unsigned char desc; /* True for DESC. False for ASC. */ } *aOrderBy; /* The ORDER BY clause */ /* Outputs */ struct sqlite3_index_constraint_usage { int argvIndex; /* if >0, constraint is part of argv to xFilter */ unsigned char omit; /* Do not code a test for this constraint */ } *aConstraintUsage; int idxNum; /* Number used to identify the index */ char *idxStr; /* String, possibly obtained from sqlite3_malloc */ int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */ int orderByConsumed; /* True if output is already ordered */ double estimatedCost; /* Estimated cost of using this index */ /* Fields below are only available in SQLite 3.8.2 and later */ sqlite3_int64 estimatedRows; /* Estimated number of rows returned */ /* Fields below are only available in SQLite 3.9.0 and later */ int idxFlags; /* Mask of SQLITE_INDEX_SCAN_* flags */ /* Fields below are only available in SQLite 3.10.0 and later */ sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */ }; /* ** CAPI3REF: Virtual Table Scan Flags ** ** Virtual table implementations are allowed to set the ** [sqlite3_index_info].idxFlags field to some combination of ** these bits. */ #define SQLITE_INDEX_SCAN_UNIQUE 0x00000001 /* Scan visits at most 1 row */ #define SQLITE_INDEX_SCAN_HEX 0x00000002 /* Display idxNum as hex */ /* in EXPLAIN QUERY PLAN */ /* ** CAPI3REF: Virtual Table Constraint Operator Codes ** ** These macros define the allowed values for the ** [sqlite3_index_info].aConstraint[].op field. Each value represents ** an operator that is part of a constraint term in the WHERE clause of ** a query that uses a [virtual table]. ** ** ^The left-hand operand of the operator is given by the corresponding ** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand ** operand is the rowid. ** The SQLITE_INDEX_CONSTRAINT_LIMIT and SQLITE_INDEX_CONSTRAINT_OFFSET ** operators have no left-hand operand, and so for those operators the ** corresponding aConstraint[].iColumn is meaningless and should not be ** used. ** ** All operator values from SQLITE_INDEX_CONSTRAINT_FUNCTION through ** value 255 are reserved to represent functions that are overloaded ** by the [xFindFunction|xFindFunction method] of the virtual table ** implementation. ** ** The right-hand operands for each constraint might be accessible using ** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand ** operand is only available if it appears as a single constant literal ** in the input SQL. If the right-hand operand is another column or an ** expression (even a constant expression) or a parameter, then the ** sqlite3_vtab_rhs_value() probably will not be able to extract it. ** ^The SQLITE_INDEX_CONSTRAINT_ISNULL and ** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand ** and hence calls to sqlite3_vtab_rhs_value() for those operators will ** always return SQLITE_NOTFOUND. ** ** The collating sequence to be used for comparison can be found using ** the [sqlite3_vtab_collation()] interface. For most real-world virtual ** tables, the collating sequence of constraints does not matter (for example ** because the constraints are numeric) and so the sqlite3_vtab_collation() ** interface is not commonly needed. */ #define SQLITE_INDEX_CONSTRAINT_EQ 2 #define SQLITE_INDEX_CONSTRAINT_GT 4 #define SQLITE_INDEX_CONSTRAINT_LE 8 #define SQLITE_INDEX_CONSTRAINT_LT 16 #define SQLITE_INDEX_CONSTRAINT_GE 32 #define SQLITE_INDEX_CONSTRAINT_MATCH 64 #define SQLITE_INDEX_CONSTRAINT_LIKE 65 #define SQLITE_INDEX_CONSTRAINT_GLOB 66 #define SQLITE_INDEX_CONSTRAINT_REGEXP 67 #define SQLITE_INDEX_CONSTRAINT_NE 68 #define SQLITE_INDEX_CONSTRAINT_ISNOT 69 #define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70 #define SQLITE_INDEX_CONSTRAINT_ISNULL 71 #define SQLITE_INDEX_CONSTRAINT_IS 72 #define SQLITE_INDEX_CONSTRAINT_LIMIT 73 #define SQLITE_INDEX_CONSTRAINT_OFFSET 74 #define SQLITE_INDEX_CONSTRAINT_FUNCTION 150 /* ** CAPI3REF: Register A Virtual Table Implementation ** METHOD: sqlite3 ** ** ^These routines are used to register a new [virtual table module] name. ** ^Module names must be registered before ** creating a new [virtual table] using the module and before using a ** preexisting [virtual table] for the module. ** ** ^The module name is registered on the [database connection] specified ** by the first parameter. ^The name of the module is given by the ** second parameter. ^The third parameter is a pointer to ** the implementation of the [virtual table module]. ^The fourth ** parameter is an arbitrary client data pointer that is passed through ** into the [xCreate] and [xConnect] methods of the virtual table module ** when a new virtual table is being created or reinitialized. ** ** ^The sqlite3_create_module_v2() interface has a fifth parameter which ** is a pointer to a destructor for the pClientData. ^SQLite will ** invoke the destructor function (if it is not NULL) when SQLite ** no longer needs the pClientData pointer. ^The destructor will also ** be invoked if the call to sqlite3_create_module_v2() fails. ** ^The sqlite3_create_module() ** interface is equivalent to sqlite3_create_module_v2() with a NULL ** destructor. ** ** ^If the third parameter (the pointer to the sqlite3_module object) is ** NULL then no new module is created and any existing modules with the ** same name are dropped. ** ** See also: [sqlite3_drop_modules()] */ SQLITE_API int sqlite3_create_module( sqlite3 *db, /* SQLite connection to register module with */ const char *zName, /* Name of the module */ const sqlite3_module *p, /* Methods for the module */ void *pClientData /* Client data for xCreate/xConnect */ ); SQLITE_API int sqlite3_create_module_v2( sqlite3 *db, /* SQLite connection to register module with */ const char *zName, /* Name of the module */ const sqlite3_module *p, /* Methods for the module */ void *pClientData, /* Client data for xCreate/xConnect */ void(*xDestroy)(void*) /* Module destructor function */ ); /* ** CAPI3REF: Remove Unnecessary Virtual Table Implementations ** METHOD: sqlite3 ** ** ^The sqlite3_drop_modules(D,L) interface removes all virtual ** table modules from database connection D except those named on list L. ** The L parameter must be either NULL or a pointer to an array of pointers ** to strings where the array is terminated by a single NULL pointer. ** ^If the L parameter is NULL, then all virtual table modules are removed. ** ** See also: [sqlite3_create_module()] */ SQLITE_API int sqlite3_drop_modules( sqlite3 *db, /* Remove modules from this connection */ const char **azKeep /* Except, do not remove the ones named here */ ); /* ** CAPI3REF: Virtual Table Instance Object ** KEYWORDS: sqlite3_vtab ** ** Every [virtual table module] implementation uses a subclass ** of this object to describe a particular instance ** of the [virtual table]. Each subclass will ** be tailored to the specific needs of the module implementation. ** The purpose of this superclass is to define certain fields that are ** common to all module implementations. ** ** ^Virtual tables methods can set an error message by assigning a ** string obtained from [sqlite3_mprintf()] to zErrMsg. The method should ** take care that any prior string is freed by a call to [sqlite3_free()] ** prior to assigning a new string to zErrMsg. ^After the error message ** is delivered up to the client application, the string will be automatically ** freed by sqlite3_free() and the zErrMsg field will be zeroed. */ struct sqlite3_vtab { const sqlite3_module *pModule; /* The module for this virtual table */ int nRef; /* Number of open cursors */ char *zErrMsg; /* Error message from sqlite3_mprintf() */ /* Virtual table implementations will typically add additional fields */ }; /* ** CAPI3REF: Virtual Table Cursor Object ** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor} ** ** Every [virtual table module] implementation uses a subclass of the ** following structure to describe cursors that point into the ** [virtual table] and are used ** to loop through the virtual table. Cursors are created using the ** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed ** by the [sqlite3_module.xClose | xClose] method. Cursors are used ** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods ** of the module. Each module implementation will define ** the content of a cursor structure to suit its own needs. ** ** This superclass exists in order to define fields of the cursor that ** are common to all implementations. */ struct sqlite3_vtab_cursor { sqlite3_vtab *pVtab; /* Virtual table of this cursor */ /* Virtual table implementations will typically add additional fields */ }; /* ** CAPI3REF: Declare The Schema Of A Virtual Table ** ** ^The [xCreate] and [xConnect] methods of a ** [virtual table module] call this interface ** to declare the format (the names and datatypes of the columns) of ** the virtual tables they implement. */ SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL); /* ** CAPI3REF: Overload A Function For A Virtual Table ** METHOD: sqlite3 ** ** ^(Virtual tables can provide alternative implementations of functions ** using the [xFindFunction] method of the [virtual table module]. ** But global versions of those functions ** must exist in order to be overloaded.)^ ** ** ^(This API makes sure a global version of a function with a particular ** name and number of parameters exists. If no such function exists ** before this API is called, a new function is created.)^ ^The implementation ** of the new function always causes an exception to be thrown. So ** the new function is not good for anything by itself. Its only ** purpose is to be a placeholder function that can be overloaded ** by a [virtual table]. */ SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); /* ** CAPI3REF: A Handle To An Open BLOB ** KEYWORDS: {BLOB handle} {BLOB handles} ** ** An instance of this object represents an open BLOB on which ** [sqlite3_blob_open | incremental BLOB I/O] can be performed. ** ^Objects of this type are created by [sqlite3_blob_open()] ** and destroyed by [sqlite3_blob_close()]. ** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces ** can be used to read or write small subsections of the BLOB. ** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes. */ typedef struct sqlite3_blob sqlite3_blob; /* ** CAPI3REF: Open A BLOB For Incremental I/O ** METHOD: sqlite3 ** CONSTRUCTOR: sqlite3_blob ** ** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located ** in row iRow, column zColumn, table zTable in database zDb; ** in other words, the same BLOB that would be selected by: ** **
**     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
** 
)^ ** ** ^(Parameter zDb is not the filename that contains the database, but ** rather the symbolic name of the database. For attached databases, this is ** the name that appears after the AS keyword in the [ATTACH] statement. ** For the main database file, the database name is "main". For TEMP ** tables, the database name is "temp".)^ ** ** ^If the flags parameter is non-zero, then the BLOB is opened for read ** and write access. ^If the flags parameter is zero, the BLOB is opened for ** read-only access. ** ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored ** in *ppBlob. Otherwise an [error code] is returned and, unless the error ** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided ** the API is not misused, it is always safe to call [sqlite3_blob_close()] ** on *ppBlob after this function returns. ** ** This function fails with SQLITE_ERROR if any of the following are true: **
    **
  • ^(Database zDb does not exist)^, **
  • ^(Table zTable does not exist within database zDb)^, **
  • ^(Table zTable is a WITHOUT ROWID table)^, **
  • ^(Column zColumn does not exist)^, **
  • ^(Row iRow is not present in the table)^, **
  • ^(The specified column of row iRow contains a value that is not ** a TEXT or BLOB value)^, **
  • ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE ** constraint and the blob is being opened for read/write access)^, **
  • ^([foreign key constraints | Foreign key constraints] are enabled, ** column zColumn is part of a [child key] definition and the blob is ** being opened for read/write access)^. **
** ** ^Unless it returns SQLITE_MISUSE, this function sets the ** [database connection] error code and message accessible via ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. ** ** A BLOB referenced by sqlite3_blob_open() may be read using the ** [sqlite3_blob_read()] interface and modified by using ** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a ** different row of the same table using the [sqlite3_blob_reopen()] ** interface. However, the column, table, or database of a [BLOB handle] ** cannot be changed after the [BLOB handle] is opened. ** ** ^(If the row that a BLOB handle points to is modified by an ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects ** then the BLOB handle is marked as "expired". ** This is true if any column of the row is changed, even a column ** other than the one the BLOB handle is open on.)^ ** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for ** an expired BLOB handle fail with a return code of [SQLITE_ABORT]. ** ^(Changes written into a BLOB prior to the BLOB expiring are not ** rolled back by the expiration of the BLOB. Such changes will eventually ** commit if the transaction continues to completion.)^ ** ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of ** the opened blob. ^The size of a blob may not be changed by this ** interface. Use the [UPDATE] SQL command to change the size of a ** blob. ** ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces ** and the built-in [zeroblob] SQL function may be used to create a ** zero-filled blob to read or write using the incremental-blob interface. ** ** To avoid a resource leak, every open [BLOB handle] should eventually ** be released by a call to [sqlite3_blob_close()]. ** ** See also: [sqlite3_blob_close()], ** [sqlite3_blob_reopen()], [sqlite3_blob_read()], ** [sqlite3_blob_bytes()], [sqlite3_blob_write()]. */ SQLITE_API int sqlite3_blob_open( sqlite3*, const char *zDb, const char *zTable, const char *zColumn, sqlite3_int64 iRow, int flags, sqlite3_blob **ppBlob ); /* ** CAPI3REF: Move a BLOB Handle to a New Row ** METHOD: sqlite3_blob ** ** ^This function is used to move an existing [BLOB handle] so that it points ** to a different row of the same database table. ^The new row is identified ** by the rowid value passed as the second argument. Only the row can be ** changed. ^The database, table and column on which the blob handle is open ** remain the same. Moving an existing [BLOB handle] to a new row is ** faster than closing the existing handle and opening a new one. ** ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] - ** it must exist and there must be either a blob or text value stored in ** the nominated column.)^ ^If the new row is not present in the table, or if ** it does not contain a blob or text value, or if another error occurs, an ** SQLite error code is returned and the blob handle is considered aborted. ** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or ** [sqlite3_blob_reopen()] on an aborted blob handle immediately return ** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle ** always returns zero. ** ** ^This function sets the database handle error code and message. */ SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); /* ** CAPI3REF: Close A BLOB Handle ** DESTRUCTOR: sqlite3_blob ** ** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed ** unconditionally. Even if this routine returns an error code, the ** handle is still closed.)^ ** ** ^If the blob handle being closed was opened for read-write access, and if ** the database is in auto-commit mode and there are no other open read-write ** blob handles or active write statements, the current transaction is ** committed. ^If an error occurs while committing the transaction, an error ** code is returned and the transaction rolled back. ** ** Calling this function with an argument that is not a NULL pointer or an ** open blob handle results in undefined behavior. ^Calling this routine ** with a null pointer (such as would be returned by a failed call to ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function ** is passed a valid open blob handle, the values returned by the ** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning. */ SQLITE_API int sqlite3_blob_close(sqlite3_blob *); /* ** CAPI3REF: Return The Size Of An Open BLOB ** METHOD: sqlite3_blob ** ** ^Returns the size in bytes of the BLOB accessible via the ** successfully opened [BLOB handle] in its only argument. ^The ** incremental blob I/O routines can only read or overwrite existing ** blob content; they cannot change the size of a blob. ** ** This routine only works on a [BLOB handle] which has been created ** by a prior successful call to [sqlite3_blob_open()] and which has not ** been closed by [sqlite3_blob_close()]. Passing any other pointer in ** to this routine results in undefined and probably undesirable behavior. */ SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *); /* ** CAPI3REF: Read Data From A BLOB Incrementally ** METHOD: sqlite3_blob ** ** ^(This function is used to read data from an open [BLOB handle] into a ** caller-supplied buffer. N bytes of data are copied into buffer Z ** from the open BLOB, starting at offset iOffset.)^ ** ** ^If offset iOffset is less than N bytes from the end of the BLOB, ** [SQLITE_ERROR] is returned and no data is read. ^If N or iOffset is ** less than zero, [SQLITE_ERROR] is returned and no data is read. ** ^The size of the blob (and hence the maximum value of N+iOffset) ** can be determined using the [sqlite3_blob_bytes()] interface. ** ** ^An attempt to read from an expired [BLOB handle] fails with an ** error code of [SQLITE_ABORT]. ** ** ^(On success, sqlite3_blob_read() returns SQLITE_OK. ** Otherwise, an [error code] or an [extended error code] is returned.)^ ** ** This routine only works on a [BLOB handle] which has been created ** by a prior successful call to [sqlite3_blob_open()] and which has not ** been closed by [sqlite3_blob_close()]. Passing any other pointer in ** to this routine results in undefined and probably undesirable behavior. ** ** See also: [sqlite3_blob_write()]. */ SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); /* ** CAPI3REF: Write Data Into A BLOB Incrementally ** METHOD: sqlite3_blob ** ** ^(This function is used to write data into an open [BLOB handle] from a ** caller-supplied buffer. N bytes of data are copied from the buffer Z ** into the open BLOB, starting at offset iOffset.)^ ** ** ^(On success, sqlite3_blob_write() returns SQLITE_OK. ** Otherwise, an [error code] or an [extended error code] is returned.)^ ** ^Unless SQLITE_MISUSE is returned, this function sets the ** [database connection] error code and message accessible via ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. ** ** ^If the [BLOB handle] passed as the first argument was not opened for ** writing (the flags parameter to [sqlite3_blob_open()] was zero), ** this function returns [SQLITE_READONLY]. ** ** This function may only modify the contents of the BLOB; it is ** not possible to increase the size of a BLOB using this API. ** ^If offset iOffset is less than N bytes from the end of the BLOB, ** [SQLITE_ERROR] is returned and no data is written. The size of the ** BLOB (and hence the maximum value of N+iOffset) can be determined ** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less ** than zero [SQLITE_ERROR] is returned and no data is written. ** ** ^An attempt to write to an expired [BLOB handle] fails with an ** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred ** before the [BLOB handle] expired are not rolled back by the ** expiration of the handle, though of course those changes might ** have been overwritten by the statement that expired the BLOB handle ** or by other independent statements. ** ** This routine only works on a [BLOB handle] which has been created ** by a prior successful call to [sqlite3_blob_open()] and which has not ** been closed by [sqlite3_blob_close()]. Passing any other pointer in ** to this routine results in undefined and probably undesirable behavior. ** ** See also: [sqlite3_blob_read()]. */ SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); /* ** CAPI3REF: Virtual File System Objects ** ** A virtual filesystem (VFS) is an [sqlite3_vfs] object ** that SQLite uses to interact ** with the underlying operating system. Most SQLite builds come with a ** single default VFS that is appropriate for the host computer. ** New VFSes can be registered and existing VFSes can be unregistered. ** The following interfaces are provided. ** ** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name. ** ^Names are case sensitive. ** ^Names are zero-terminated UTF-8 strings. ** ^If there is no match, a NULL pointer is returned. ** ^If zVfsName is NULL then the default VFS is returned. ** ** ^New VFSes are registered with sqlite3_vfs_register(). ** ^Each new VFS becomes the default VFS if the makeDflt flag is set. ** ^The same VFS can be registered multiple times without injury. ** ^To make an existing VFS into the default VFS, register it again ** with the makeDflt flag set. If two different VFSes with the ** same name are registered, the behavior is undefined. If a ** VFS is registered with a name that is NULL or an empty string, ** then the behavior is undefined. ** ** ^Unregister a VFS with the sqlite3_vfs_unregister() interface. ** ^(If the default VFS is unregistered, another VFS is chosen as ** the default. The choice for the new VFS is arbitrary.)^ */ SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName); SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt); SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*); /* ** CAPI3REF: Mutexes ** ** The SQLite core uses these routines for thread ** synchronization. Though they are intended for internal ** use by SQLite, code that links against SQLite is ** permitted to use any of these routines. ** ** The SQLite source code contains multiple implementations ** of these mutex routines. An appropriate implementation ** is selected automatically at compile-time. The following ** implementations are available in the SQLite core: ** **
    **
  • SQLITE_MUTEX_PTHREADS **
  • SQLITE_MUTEX_W32 **
  • SQLITE_MUTEX_NOOP **
** ** The SQLITE_MUTEX_NOOP implementation is a set of routines ** that does no real locking and is appropriate for use in ** a single-threaded application. The SQLITE_MUTEX_PTHREADS and ** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix ** and Windows. ** ** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex ** implementation is included with the library. In this case the ** application must supply a custom mutex implementation using the ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function ** before calling sqlite3_initialize() or any other public sqlite3_ ** function that calls sqlite3_initialize(). ** ** ^The sqlite3_mutex_alloc() routine allocates a new ** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc() ** routine returns NULL if it is unable to allocate the requested ** mutex. The argument to sqlite3_mutex_alloc() must be one of these ** integer constants: ** **
    **
  • SQLITE_MUTEX_FAST **
  • SQLITE_MUTEX_RECURSIVE **
  • SQLITE_MUTEX_STATIC_MAIN **
  • SQLITE_MUTEX_STATIC_MEM **
  • SQLITE_MUTEX_STATIC_OPEN **
  • SQLITE_MUTEX_STATIC_PRNG **
  • SQLITE_MUTEX_STATIC_LRU **
  • SQLITE_MUTEX_STATIC_PMEM **
  • SQLITE_MUTEX_STATIC_APP1 **
  • SQLITE_MUTEX_STATIC_APP2 **
  • SQLITE_MUTEX_STATIC_APP3 **
  • SQLITE_MUTEX_STATIC_VFS1 **
  • SQLITE_MUTEX_STATIC_VFS2 **
  • SQLITE_MUTEX_STATIC_VFS3 **
** ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) ** cause sqlite3_mutex_alloc() to create ** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE ** is used but not necessarily so when SQLITE_MUTEX_FAST is used. ** The mutex implementation does not need to make a distinction ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does ** not want to. SQLite will only request a recursive mutex in ** cases where it really needs one. If a faster non-recursive mutex ** implementation is available on the host platform, the mutex subsystem ** might return such a mutex in response to SQLITE_MUTEX_FAST. ** ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return ** a pointer to a static preexisting mutex. ^Nine static mutexes are ** used by the current version of SQLite. Future versions of SQLite ** may add additional static mutexes. Static mutexes are for internal ** use by SQLite only. Applications that use SQLite mutexes should ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or ** SQLITE_MUTEX_RECURSIVE. ** ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() ** returns a different mutex on every call. ^For the static ** mutex types, the same mutex is returned on every call that has ** the same type number. ** ** ^The sqlite3_mutex_free() routine deallocates a previously ** allocated dynamic mutex. Attempting to deallocate a static ** mutex results in undefined behavior. ** ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt ** to enter a mutex. ^If another thread is already within the mutex, ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return ** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK] ** upon successful entry. ^(Mutexes created using ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. ** In such cases, the ** mutex must be exited an equal number of times before another thread ** can enter.)^ If the same thread tries to enter any mutex other ** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined. ** ** ^(Some systems (for example, Windows 95) do not support the operation ** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() ** will always return SQLITE_BUSY. In most cases the SQLite core only uses ** sqlite3_mutex_try() as an optimization, so this is acceptable ** behavior. The exceptions are unix builds that set the ** SQLITE_ENABLE_SETLK_TIMEOUT build option. In that case a working ** sqlite3_mutex_try() is required.)^ ** ** ^The sqlite3_mutex_leave() routine exits a mutex that was ** previously entered by the same thread. The behavior ** is undefined if the mutex is not currently entered by the ** calling thread or is not currently allocated. ** ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), ** sqlite3_mutex_leave(), or sqlite3_mutex_free() is a NULL pointer, ** then any of the four routines behaves as a no-op. ** ** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()]. */ SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int); SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*); SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*); SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*); SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*); /* ** CAPI3REF: Mutex Methods Object ** ** An instance of this structure defines the low-level routines ** used to allocate and use mutexes. ** ** Usually, the default mutex implementations provided by SQLite are ** sufficient, however the application has the option of substituting a custom ** implementation for specialized deployments or systems for which SQLite ** does not provide a suitable implementation. In this case, the application ** creates and populates an instance of this structure to pass ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option. ** Additionally, an instance of this structure can be used as an ** output variable when querying the system for the current mutex ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. ** ** ^The xMutexInit method defined by this structure is invoked as ** part of system initialization by the sqlite3_initialize() function. ** ^The xMutexInit routine is called by SQLite exactly once for each ** effective call to [sqlite3_initialize()]. ** ** ^The xMutexEnd method defined by this structure is invoked as ** part of system shutdown by the sqlite3_shutdown() function. The ** implementation of this method is expected to release all outstanding ** resources obtained by the mutex methods implementation, especially ** those obtained by the xMutexInit method. ^The xMutexEnd() ** interface is invoked exactly once for each call to [sqlite3_shutdown()]. ** ** ^(The remaining seven methods defined by this structure (xMutexAlloc, ** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and ** xMutexNotheld) implement the following interfaces (respectively): ** **
    **
  • [sqlite3_mutex_alloc()]
  • **
  • [sqlite3_mutex_free()]
  • **
  • [sqlite3_mutex_enter()]
  • **
  • [sqlite3_mutex_try()]
  • **
  • [sqlite3_mutex_leave()]
  • **
  • [sqlite3_mutex_held()]
  • **
  • [sqlite3_mutex_notheld()]
  • **
)^ ** ** The only difference is that the public sqlite3_XXX functions enumerated ** above silently ignore any invocations that pass a NULL pointer instead ** of a valid mutex handle. The implementations of the methods defined ** by this structure are not required to handle this case. The results ** of passing a NULL pointer instead of a valid mutex handle are undefined ** (i.e. it is acceptable to provide an implementation that segfaults if ** it is passed a NULL pointer). ** ** The xMutexInit() method must be threadsafe. It must be harmless to ** invoke xMutexInit() multiple times within the same process and without ** intervening calls to xMutexEnd(). Second and subsequent calls to ** xMutexInit() must be no-ops. ** ** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] ** and its associates). Similarly, xMutexAlloc() must not use SQLite memory ** allocation for a static mutex. ^However xMutexAlloc() may use SQLite ** memory allocation for a fast or recursive mutex. ** ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is ** called, but only if the prior call to xMutexInit returned SQLITE_OK. ** If xMutexInit fails in any way, it is expected to clean up after itself ** prior to returning. */ typedef struct sqlite3_mutex_methods sqlite3_mutex_methods; struct sqlite3_mutex_methods { int (*xMutexInit)(void); int (*xMutexEnd)(void); sqlite3_mutex *(*xMutexAlloc)(int); void (*xMutexFree)(sqlite3_mutex *); void (*xMutexEnter)(sqlite3_mutex *); int (*xMutexTry)(sqlite3_mutex *); void (*xMutexLeave)(sqlite3_mutex *); int (*xMutexHeld)(sqlite3_mutex *); int (*xMutexNotheld)(sqlite3_mutex *); }; /* ** CAPI3REF: Mutex Verification Routines ** ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines ** are intended for use inside assert() statements. The SQLite core ** never uses these routines except inside an assert() and applications ** are advised to follow the lead of the core. The SQLite core only ** provides implementations for these routines when it is compiled ** with the SQLITE_DEBUG flag. External mutex implementations ** are only required to provide these routines if SQLITE_DEBUG is ** defined and if NDEBUG is not defined. ** ** These routines should return true if the mutex in their argument ** is held or not held, respectively, by the calling thread. ** ** The implementation is not required to provide versions of these ** routines that actually work. If the implementation does not provide working ** versions of these routines, it should at least provide stubs that always ** return true so that one does not get spurious assertion failures. ** ** If the argument to sqlite3_mutex_held() is a NULL pointer then ** the routine should return 1. This seems counter-intuitive since ** clearly the mutex cannot be held if it does not exist. But ** the reason the mutex does not exist is because the build is not ** using mutexes. And we do not want the assert() containing the ** call to sqlite3_mutex_held() to fail, so a non-zero return is ** the appropriate thing to do. The sqlite3_mutex_notheld() ** interface should also return 1 when given a NULL pointer. */ #ifndef NDEBUG SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*); SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*); #endif /* ** CAPI3REF: Mutex Types ** ** The [sqlite3_mutex_alloc()] interface takes a single argument ** which is one of these integer constants. ** ** The set of static mutexes may change from one SQLite release to the ** next. Applications that override the built-in mutex logic must be ** prepared to accommodate additional static mutexes. */ #define SQLITE_MUTEX_FAST 0 #define SQLITE_MUTEX_RECURSIVE 1 #define SQLITE_MUTEX_STATIC_MAIN 2 #define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */ #define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */ #define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */ #define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_randomness() */ #define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */ #define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */ #define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */ #define SQLITE_MUTEX_STATIC_APP1 8 /* For use by application */ #define SQLITE_MUTEX_STATIC_APP2 9 /* For use by application */ #define SQLITE_MUTEX_STATIC_APP3 10 /* For use by application */ #define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */ #define SQLITE_MUTEX_STATIC_VFS2 12 /* For use by extension VFS */ #define SQLITE_MUTEX_STATIC_VFS3 13 /* For use by application VFS */ /* Legacy compatibility: */ #define SQLITE_MUTEX_STATIC_MASTER 2 /* ** CAPI3REF: Retrieve the mutex for a database connection ** METHOD: sqlite3 ** ** ^This interface returns a pointer to the [sqlite3_mutex] object that ** serializes access to the [database connection] given in the argument ** when the [threading mode] is Serialized. ** ^If the [threading mode] is Single-thread or Multi-thread then this ** routine returns a NULL pointer. */ SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*); /* ** CAPI3REF: Low-Level Control Of Database Files ** METHOD: sqlite3 ** KEYWORDS: {file control} ** ** ^The [sqlite3_file_control()] interface makes a direct call to the ** xFileControl method for the [sqlite3_io_methods] object associated ** with a particular database identified by the second argument. ^The ** name of the database is "main" for the main database or "temp" for the ** TEMP database, or the name that appears after the AS keyword for ** databases that are added using the [ATTACH] SQL command. ** ^A NULL pointer can be used in place of "main" to refer to the ** main database file. ** ^The third and fourth parameters to this routine ** are passed directly through to the second and third parameters of ** the xFileControl method. ^The return value of the xFileControl ** method becomes the return value of this routine. ** ** A few opcodes for [sqlite3_file_control()] are handled directly ** by the SQLite core and never invoke the ** sqlite3_io_methods.xFileControl method. ** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes ** a pointer to the underlying [sqlite3_file] object to be written into ** the space pointed to by the 4th parameter. The ** [SQLITE_FCNTL_JOURNAL_POINTER] works similarly except that it returns ** the [sqlite3_file] object associated with the journal file instead of ** the main database. The [SQLITE_FCNTL_VFS_POINTER] opcode returns ** a pointer to the underlying [sqlite3_vfs] object for the file. ** The [SQLITE_FCNTL_DATA_VERSION] returns the data version counter ** from the pager. ** ** ^If the second parameter (zDbName) does not match the name of any ** open database file, then SQLITE_ERROR is returned. ^This error ** code is not remembered and will not be recalled by [sqlite3_errcode()] ** or [sqlite3_errmsg()]. The underlying xFileControl method might ** also return SQLITE_ERROR. There is no way to distinguish between ** an incorrect zDbName and an SQLITE_ERROR return from the underlying ** xFileControl method. ** ** See also: [file control opcodes] */ SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); /* ** CAPI3REF: Testing Interface ** ** ^The sqlite3_test_control() interface is used to read out internal ** state of SQLite and to inject faults into SQLite for testing ** purposes. ^The first parameter is an operation code that determines ** the number, meaning, and operation of all subsequent parameters. ** ** This interface is not for use by applications. It exists solely ** for verifying the correct operation of the SQLite library. Depending ** on how the SQLite library is compiled, this interface might not exist. ** ** The details of the operation codes, their meanings, the parameters ** they take, and what they do are all subject to change without notice. ** Unlike most of the SQLite API, this function is not guaranteed to ** operate consistently from one release to the next. */ SQLITE_API int sqlite3_test_control(int op, ...); /* ** CAPI3REF: Testing Interface Operation Codes ** ** These constants are the valid operation code parameters used ** as the first argument to [sqlite3_test_control()]. ** ** These parameters and their meanings are subject to change ** without notice. These values are for testing purposes only. ** Applications should not use any of these parameters or the ** [sqlite3_test_control()] interface. */ #define SQLITE_TESTCTRL_FIRST 5 #define SQLITE_TESTCTRL_PRNG_SAVE 5 #define SQLITE_TESTCTRL_PRNG_RESTORE 6 #define SQLITE_TESTCTRL_PRNG_RESET 7 /* NOT USED */ #define SQLITE_TESTCTRL_FK_NO_ACTION 7 #define SQLITE_TESTCTRL_BITVEC_TEST 8 #define SQLITE_TESTCTRL_FAULT_INSTALL 9 #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS 10 #define SQLITE_TESTCTRL_PENDING_BYTE 11 #define SQLITE_TESTCTRL_ASSERT 12 #define SQLITE_TESTCTRL_ALWAYS 13 #define SQLITE_TESTCTRL_RESERVE 14 /* NOT USED */ #define SQLITE_TESTCTRL_JSON_SELFCHECK 14 #define SQLITE_TESTCTRL_OPTIMIZATIONS 15 #define SQLITE_TESTCTRL_ISKEYWORD 16 /* NOT USED */ #define SQLITE_TESTCTRL_GETOPT 16 #define SQLITE_TESTCTRL_SCRATCHMALLOC 17 /* NOT USED */ #define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS 17 #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18 #define SQLITE_TESTCTRL_EXPLAIN_STMT 19 /* NOT USED */ #define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD 19 #define SQLITE_TESTCTRL_NEVER_CORRUPT 20 #define SQLITE_TESTCTRL_VDBE_COVERAGE 21 #define SQLITE_TESTCTRL_BYTEORDER 22 #define SQLITE_TESTCTRL_ISINIT 23 #define SQLITE_TESTCTRL_SORTER_MMAP 24 #define SQLITE_TESTCTRL_IMPOSTER 25 #define SQLITE_TESTCTRL_PARSER_COVERAGE 26 #define SQLITE_TESTCTRL_RESULT_INTREAL 27 #define SQLITE_TESTCTRL_PRNG_SEED 28 #define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS 29 #define SQLITE_TESTCTRL_SEEK_COUNT 30 #define SQLITE_TESTCTRL_TRACEFLAGS 31 #define SQLITE_TESTCTRL_TUNE 32 #define SQLITE_TESTCTRL_LOGEST 33 #define SQLITE_TESTCTRL_USELONGDOUBLE 34 /* NOT USED */ #define SQLITE_TESTCTRL_LAST 34 /* Largest TESTCTRL */ /* ** CAPI3REF: SQL Keyword Checking ** ** These routines provide access to the set of SQL language keywords ** recognized by SQLite. Applications can use these routines to determine ** whether or not a specific identifier needs to be escaped (for example, ** by enclosing in double-quotes) so as not to confuse the parser. ** ** The sqlite3_keyword_count() interface returns the number of distinct ** keywords understood by SQLite. ** ** The sqlite3_keyword_name(N,Z,L) interface finds the 0-based N-th keyword and ** makes *Z point to that keyword expressed as UTF8 and writes the number ** of bytes in the keyword into *L. The string that *Z points to is not ** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns ** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z ** or L are NULL or invalid pointers then calls to ** sqlite3_keyword_name(N,Z,L) result in undefined behavior. ** ** The sqlite3_keyword_check(Z,L) interface checks to see whether or not ** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero ** if it is and zero if not. ** ** The parser used by SQLite is forgiving. It is often possible to use ** a keyword as an identifier as long as such use does not result in a ** parsing ambiguity. For example, the statement ** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and ** creates a new table named "BEGIN" with three columns named ** "REPLACE", "PRAGMA", and "END". Nevertheless, best practice is to avoid ** using keywords as identifiers. Common techniques used to avoid keyword ** name collisions include: **
    **
  • Put all identifier names inside double-quotes. This is the official ** SQL way to escape identifier names. **
  • Put identifier names inside [...]. This is not standard SQL, ** but it is what SQL Server does and so lots of programmers use this ** technique. **
  • Begin every identifier with the letter "Z" as no SQL keywords start ** with "Z". **
  • Include a digit somewhere in every identifier name. **
** ** Note that the number of keywords understood by SQLite can depend on ** compile-time options. For example, "VACUUM" is not a keyword if ** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also, ** new keywords may be added to future releases of SQLite. */ SQLITE_API int sqlite3_keyword_count(void); SQLITE_API int sqlite3_keyword_name(int,const char**,int*); SQLITE_API int sqlite3_keyword_check(const char*,int); /* ** CAPI3REF: Dynamic String Object ** KEYWORDS: {dynamic string} ** ** An instance of the sqlite3_str object contains a dynamically-sized ** string under construction. ** ** The lifecycle of an sqlite3_str object is as follows: **
    **
  1. ^The sqlite3_str object is created using [sqlite3_str_new()]. **
  2. ^Text is appended to the sqlite3_str object using various ** methods, such as [sqlite3_str_appendf()]. **
  3. ^The sqlite3_str object is destroyed and the string it created ** is returned using the [sqlite3_str_finish()] interface. **
*/ typedef struct sqlite3_str sqlite3_str; /* ** CAPI3REF: Create A New Dynamic String Object ** CONSTRUCTOR: sqlite3_str ** ** ^The [sqlite3_str_new(D)] interface allocates and initializes ** a new [sqlite3_str] object. To avoid memory leaks, the object returned by ** [sqlite3_str_new()] must be freed by a subsequent call to ** [sqlite3_str_finish(X)]. ** ** ^The [sqlite3_str_new(D)] interface always returns a pointer to a ** valid [sqlite3_str] object, though in the event of an out-of-memory ** error the returned object might be a special singleton that will ** silently reject new text, always return SQLITE_NOMEM from ** [sqlite3_str_errcode()], always return 0 for ** [sqlite3_str_length()], and always return NULL from ** [sqlite3_str_finish(X)]. It is always safe to use the value ** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter ** to any of the other [sqlite3_str] methods. ** ** The D parameter to [sqlite3_str_new(D)] may be NULL. If the ** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum ** length of the string contained in the [sqlite3_str] object will be ** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead ** of [SQLITE_MAX_LENGTH]. */ SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3*); /* ** CAPI3REF: Finalize A Dynamic String ** DESTRUCTOR: sqlite3_str ** ** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X ** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()] ** that contains the constructed string. The calling application should ** pass the returned value to [sqlite3_free()] to avoid a memory leak. ** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any ** errors were encountered during construction of the string. ^The ** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the ** string in [sqlite3_str] object X is zero bytes long. */ SQLITE_API char *sqlite3_str_finish(sqlite3_str*); /* ** CAPI3REF: Add Content To A Dynamic String ** METHOD: sqlite3_str ** ** These interfaces add content to an sqlite3_str object previously obtained ** from [sqlite3_str_new()]. ** ** ^The [sqlite3_str_appendf(X,F,...)] and ** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf] ** functionality of SQLite to append formatted text onto the end of ** [sqlite3_str] object X. ** ** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S ** onto the end of the [sqlite3_str] object X. N must be non-negative. ** S must contain at least N non-zero bytes of content. To append a ** zero-terminated string in its entirety, use the [sqlite3_str_appendall()] ** method instead. ** ** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of ** zero-terminated string S onto the end of [sqlite3_str] object X. ** ** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the ** single-byte character C onto the end of [sqlite3_str] object X. ** ^This method can be used, for example, to add whitespace indentation. ** ** ^The [sqlite3_str_reset(X)] method resets the string under construction ** inside [sqlite3_str] object X back to zero bytes in length. ** ** These methods do not return a result code. ^If an error occurs, that fact ** is recorded in the [sqlite3_str] object and can be recovered by a ** subsequent call to [sqlite3_str_errcode(X)]. */ SQLITE_API void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...); SQLITE_API void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list); SQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N); SQLITE_API void sqlite3_str_appendall(sqlite3_str*, const char *zIn); SQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C); SQLITE_API void sqlite3_str_reset(sqlite3_str*); /* ** CAPI3REF: Status Of A Dynamic String ** METHOD: sqlite3_str ** ** These interfaces return the current status of an [sqlite3_str] object. ** ** ^If any prior errors have occurred while constructing the dynamic string ** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return ** an appropriate error code. ^The [sqlite3_str_errcode(X)] method returns ** [SQLITE_NOMEM] following any out-of-memory error, or ** [SQLITE_TOOBIG] if the size of the dynamic string exceeds ** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors. ** ** ^The [sqlite3_str_length(X)] method returns the current length, in bytes, ** of the dynamic string under construction in [sqlite3_str] object X. ** ^The length returned by [sqlite3_str_length(X)] does not include the ** zero-termination byte. ** ** ^The [sqlite3_str_value(X)] method returns a pointer to the current ** content of the dynamic string under construction in X. The value ** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X ** and might be freed or altered by any subsequent method on the same ** [sqlite3_str] object. Applications must not use the pointer returned by ** [sqlite3_str_value(X)] after any subsequent method call on the same ** object. ^Applications may change the content of the string returned ** by [sqlite3_str_value(X)] as long as they do not write into any bytes ** outside the range of 0 to [sqlite3_str_length(X)] and do not read or ** write any byte after any subsequent sqlite3_str method call. */ SQLITE_API int sqlite3_str_errcode(sqlite3_str*); SQLITE_API int sqlite3_str_length(sqlite3_str*); SQLITE_API char *sqlite3_str_value(sqlite3_str*); /* ** CAPI3REF: SQLite Runtime Status ** ** ^These interfaces are used to retrieve runtime status information ** about the performance of SQLite, and optionally to reset various ** highwater marks. ^The first argument is an integer code for ** the specific parameter to measure. ^(Recognized integer codes ** are of the form [status parameters | SQLITE_STATUS_...].)^ ** ^The current value of the parameter is returned into *pCurrent. ** ^The highest recorded value is returned in *pHighwater. ^If the ** resetFlag is true, then the highest record value is reset after ** *pHighwater is written. ^(Some parameters do not record the highest ** value. For those parameters ** nothing is written into *pHighwater and the resetFlag is ignored.)^ ** ^(Other parameters record only the highwater mark and not the current ** value. For these latter parameters nothing is written into *pCurrent.)^ ** ** ^The sqlite3_status() and sqlite3_status64() routines return ** SQLITE_OK on success and a non-zero [error code] on failure. ** ** If either the current value or the highwater mark is too large to ** be represented by a 32-bit integer, then the values returned by ** sqlite3_status() are undefined. ** ** See also: [sqlite3_db_status()] */ SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag); SQLITE_API int sqlite3_status64( int op, sqlite3_int64 *pCurrent, sqlite3_int64 *pHighwater, int resetFlag ); /* ** CAPI3REF: Status Parameters ** KEYWORDS: {status parameters} ** ** These integer constants designate various run-time status parameters ** that can be returned by [sqlite3_status()]. ** **
** [[SQLITE_STATUS_MEMORY_USED]] ^(
SQLITE_STATUS_MEMORY_USED
**
This parameter is the current amount of memory checked out ** using [sqlite3_malloc()], either directly or indirectly. The ** figure includes calls made to [sqlite3_malloc()] by the application ** and internal memory usage by the SQLite library. Auxiliary page-cache ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in ** this parameter. The amount returned is the sum of the allocation ** sizes as reported by the xSize method in [sqlite3_mem_methods].
)^ ** ** [[SQLITE_STATUS_MALLOC_SIZE]] ^(
SQLITE_STATUS_MALLOC_SIZE
**
This parameter records the largest memory allocation request ** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their ** internal equivalents). Only the value returned in the ** *pHighwater parameter to [sqlite3_status()] is of interest. ** The value written into the *pCurrent parameter is undefined.
)^ ** ** [[SQLITE_STATUS_MALLOC_COUNT]] ^(
SQLITE_STATUS_MALLOC_COUNT
**
This parameter records the number of separate memory allocations ** currently checked out.
)^ ** ** [[SQLITE_STATUS_PAGECACHE_USED]] ^(
SQLITE_STATUS_PAGECACHE_USED
**
This parameter returns the number of pages used out of the ** [pagecache memory allocator] that was configured using ** [SQLITE_CONFIG_PAGECACHE]. The ** value returned is in pages, not in bytes.
)^ ** ** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]] ** ^(
SQLITE_STATUS_PAGECACHE_OVERFLOW
**
This parameter returns the number of bytes of page cache ** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE] ** buffer and where forced to overflow to [sqlite3_malloc()]. The ** returned value includes allocations that overflowed because they ** were too large (they were larger than the "sz" parameter to ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because ** no space was left in the page cache.
)^ ** ** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(
SQLITE_STATUS_PAGECACHE_SIZE
**
This parameter records the largest memory allocation request ** handed to the [pagecache memory allocator]. Only the value returned in the ** *pHighwater parameter to [sqlite3_status()] is of interest. ** The value written into the *pCurrent parameter is undefined.
)^ ** ** [[SQLITE_STATUS_SCRATCH_USED]]
SQLITE_STATUS_SCRATCH_USED
**
No longer used.
** ** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(
SQLITE_STATUS_SCRATCH_OVERFLOW
**
No longer used.
** ** [[SQLITE_STATUS_SCRATCH_SIZE]]
SQLITE_STATUS_SCRATCH_SIZE
**
No longer used.
** ** [[SQLITE_STATUS_PARSER_STACK]] ^(
SQLITE_STATUS_PARSER_STACK
**
The *pHighwater parameter records the deepest parser stack. ** The *pCurrent value is undefined. The *pHighwater value is only ** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].
)^ **
** ** New status parameters may be added from time to time. */ #define SQLITE_STATUS_MEMORY_USED 0 #define SQLITE_STATUS_PAGECACHE_USED 1 #define SQLITE_STATUS_PAGECACHE_OVERFLOW 2 #define SQLITE_STATUS_SCRATCH_USED 3 /* NOT USED */ #define SQLITE_STATUS_SCRATCH_OVERFLOW 4 /* NOT USED */ #define SQLITE_STATUS_MALLOC_SIZE 5 #define SQLITE_STATUS_PARSER_STACK 6 #define SQLITE_STATUS_PAGECACHE_SIZE 7 #define SQLITE_STATUS_SCRATCH_SIZE 8 /* NOT USED */ #define SQLITE_STATUS_MALLOC_COUNT 9 /* ** CAPI3REF: Database Connection Status ** METHOD: sqlite3 ** ** ^This interface is used to retrieve runtime status information ** about a single [database connection]. ^The first argument is the ** database connection object to be interrogated. ^The second argument ** is an integer constant, taken from the set of ** [SQLITE_DBSTATUS options], that ** determines the parameter to interrogate. The set of ** [SQLITE_DBSTATUS options] is likely ** to grow in future releases of SQLite. ** ** ^The current value of the requested parameter is written into *pCur ** and the highest instantaneous value is written into *pHiwtr. ^If ** the resetFlg is true, then the highest instantaneous value is ** reset back down to the current value. ** ** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a ** non-zero [error code] on failure. ** ** See also: [sqlite3_status()] and [sqlite3_stmt_status()]. */ SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); /* ** CAPI3REF: Status Parameters for database connections ** KEYWORDS: {SQLITE_DBSTATUS options} ** ** These constants are the available integer "verbs" that can be passed as ** the second argument to the [sqlite3_db_status()] interface. ** ** New verbs may be added in future releases of SQLite. Existing verbs ** might be discontinued. Applications should check the return code from ** [sqlite3_db_status()] to make sure that the call worked. ** The [sqlite3_db_status()] interface will return a non-zero error code ** if a discontinued or unsupported verb is invoked. ** **
** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(
SQLITE_DBSTATUS_LOOKASIDE_USED
**
This parameter returns the number of lookaside memory slots currently ** checked out.
)^ ** ** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(
SQLITE_DBSTATUS_LOOKASIDE_HIT
**
This parameter returns the number of malloc attempts that were ** satisfied using lookaside memory. Only the high-water value is meaningful; ** the current value is always zero.
)^ ** ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]] ** ^(
SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE
**
This parameter returns the number of malloc attempts that might have ** been satisfied using lookaside memory but failed due to the amount of ** memory requested being larger than the lookaside slot size. ** Only the high-water value is meaningful; ** the current value is always zero.
)^ ** ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]] ** ^(
SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL
**
This parameter returns the number of malloc attempts that might have ** been satisfied using lookaside memory but failed due to all lookaside ** memory already being in use. ** Only the high-water value is meaningful; ** the current value is always zero.
)^ ** ** [[SQLITE_DBSTATUS_CACHE_USED]] ^(
SQLITE_DBSTATUS_CACHE_USED
**
This parameter returns the approximate number of bytes of heap ** memory used by all pager caches associated with the database connection.)^ ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0. **
** ** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]] ** ^(
SQLITE_DBSTATUS_CACHE_USED_SHARED
**
This parameter is similar to DBSTATUS_CACHE_USED, except that if a ** pager cache is shared between two or more connections the bytes of heap ** memory used by that pager cache is divided evenly between the attached ** connections.)^ In other words, if none of the pager caches associated ** with the database connection are shared, this request returns the same ** value as DBSTATUS_CACHE_USED. Or, if one or more of the pager caches are ** shared, the value returned by this call will be smaller than that returned ** by DBSTATUS_CACHE_USED. ^The highwater mark associated with ** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0.
** ** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(
SQLITE_DBSTATUS_SCHEMA_USED
**
This parameter returns the approximate number of bytes of heap ** memory used to store the schema for all databases associated ** with the connection - main, temp, and any [ATTACH]-ed databases.)^ ** ^The full amount of memory used by the schemas is reported, even if the ** schema memory is shared with other database connections due to ** [shared cache mode] being enabled. ** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0. **
** ** [[SQLITE_DBSTATUS_STMT_USED]] ^(
SQLITE_DBSTATUS_STMT_USED
**
This parameter returns the approximate number of bytes of heap ** and lookaside memory used by all prepared statements associated with ** the database connection.)^ ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0. **
** ** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(
SQLITE_DBSTATUS_CACHE_HIT
**
This parameter returns the number of pager cache hits that have ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT ** is always 0. **
** ** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(
SQLITE_DBSTATUS_CACHE_MISS
**
This parameter returns the number of pager cache misses that have ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS ** is always 0. **
** ** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(
SQLITE_DBSTATUS_CACHE_WRITE
**
This parameter returns the number of dirty cache entries that have ** been written to disk. Specifically, the number of pages written to the ** wal file in wal mode databases, or the number of pages written to the ** database file in rollback mode databases. Any pages written as part of ** transaction rollback or database recovery operations are not included. ** If an IO or other error occurs while writing a page to disk, the effect ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0. **
** ** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(
SQLITE_DBSTATUS_CACHE_SPILL
**
This parameter returns the number of dirty cache entries that have ** been written to disk in the middle of a transaction due to the page ** cache overflowing. Transactions are more efficient if they are written ** to disk all at once. When pages spill mid-transaction, that introduces ** additional overhead. This parameter can be used to help identify ** inefficiencies that can be resolved by increasing the cache size. **
** ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(
SQLITE_DBSTATUS_DEFERRED_FKS
**
This parameter returns zero for the current value if and only if ** all foreign key constraints (deferred or immediate) have been ** resolved.)^ ^The highwater mark is always 0. **
**
*/ #define SQLITE_DBSTATUS_LOOKASIDE_USED 0 #define SQLITE_DBSTATUS_CACHE_USED 1 #define SQLITE_DBSTATUS_SCHEMA_USED 2 #define SQLITE_DBSTATUS_STMT_USED 3 #define SQLITE_DBSTATUS_LOOKASIDE_HIT 4 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6 #define SQLITE_DBSTATUS_CACHE_HIT 7 #define SQLITE_DBSTATUS_CACHE_MISS 8 #define SQLITE_DBSTATUS_CACHE_WRITE 9 #define SQLITE_DBSTATUS_DEFERRED_FKS 10 #define SQLITE_DBSTATUS_CACHE_USED_SHARED 11 #define SQLITE_DBSTATUS_CACHE_SPILL 12 #define SQLITE_DBSTATUS_MAX 12 /* Largest defined DBSTATUS */ /* ** CAPI3REF: Prepared Statement Status ** METHOD: sqlite3_stmt ** ** ^(Each prepared statement maintains various ** [SQLITE_STMTSTATUS counters] that measure the number ** of times it has performed specific operations.)^ These counters can ** be used to monitor the performance characteristics of the prepared ** statements. For example, if the number of table steps greatly exceeds ** the number of table searches or result rows, that would tend to indicate ** that the prepared statement is using a full table scan rather than ** an index. ** ** ^(This interface is used to retrieve and reset counter values from ** a [prepared statement]. The first argument is the prepared statement ** object to be interrogated. The second argument ** is an integer code for a specific [SQLITE_STMTSTATUS counter] ** to be interrogated.)^ ** ^The current value of the requested counter is returned. ** ^If the resetFlg is true, then the counter is reset to zero after this ** interface call returns. ** ** See also: [sqlite3_status()] and [sqlite3_db_status()]. */ SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg); /* ** CAPI3REF: Status Parameters for prepared statements ** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters} ** ** These preprocessor macros define integer codes that name counter ** values associated with the [sqlite3_stmt_status()] interface. ** The meanings of the various counters are as follows: ** **
** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]]
SQLITE_STMTSTATUS_FULLSCAN_STEP
**
^This is the number of times that SQLite has stepped forward in ** a table as part of a full table scan. Large numbers for this counter ** may indicate opportunities for performance improvement through ** careful use of indices.
** ** [[SQLITE_STMTSTATUS_SORT]]
SQLITE_STMTSTATUS_SORT
**
^This is the number of sort operations that have occurred. ** A non-zero value in this counter may indicate an opportunity to ** improve performance through careful use of indices.
** ** [[SQLITE_STMTSTATUS_AUTOINDEX]]
SQLITE_STMTSTATUS_AUTOINDEX
**
^This is the number of rows inserted into transient indices that ** were created automatically in order to help joins run faster. ** A non-zero value in this counter may indicate an opportunity to ** improve performance by adding permanent indices that do not ** need to be reinitialized each time the statement is run.
** ** [[SQLITE_STMTSTATUS_VM_STEP]]
SQLITE_STMTSTATUS_VM_STEP
**
^This is the number of virtual machine operations executed ** by the prepared statement if that number is less than or equal ** to 2147483647. The number of virtual machine operations can be ** used as a proxy for the total work done by the prepared statement. ** If the number of virtual machine operations exceeds 2147483647 ** then the value returned by this statement status code is undefined.
** ** [[SQLITE_STMTSTATUS_REPREPARE]]
SQLITE_STMTSTATUS_REPREPARE
**
^This is the number of times that the prepare statement has been ** automatically regenerated due to schema changes or changes to ** [bound parameters] that might affect the query plan.
** ** [[SQLITE_STMTSTATUS_RUN]]
SQLITE_STMTSTATUS_RUN
**
^This is the number of times that the prepared statement has ** been run. A single "run" for the purposes of this counter is one ** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()]. ** The counter is incremented on the first [sqlite3_step()] call of each ** cycle.
** ** [[SQLITE_STMTSTATUS_FILTER_MISS]] ** [[SQLITE_STMTSTATUS_FILTER HIT]] **
SQLITE_STMTSTATUS_FILTER_HIT
** SQLITE_STMTSTATUS_FILTER_MISS
**
^SQLITE_STMTSTATUS_FILTER_HIT is the number of times that a join ** step was bypassed because a Bloom filter returned not-found. The ** corresponding SQLITE_STMTSTATUS_FILTER_MISS value is the number of ** times that the Bloom filter returned a find, and thus the join step ** had to be processed as normal.
** ** [[SQLITE_STMTSTATUS_MEMUSED]]
SQLITE_STMTSTATUS_MEMUSED
**
^This is the approximate number of bytes of heap memory ** used to store the prepared statement. ^This value is not actually ** a counter, and so the resetFlg parameter to sqlite3_stmt_status() ** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED. **
**
*/ #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1 #define SQLITE_STMTSTATUS_SORT 2 #define SQLITE_STMTSTATUS_AUTOINDEX 3 #define SQLITE_STMTSTATUS_VM_STEP 4 #define SQLITE_STMTSTATUS_REPREPARE 5 #define SQLITE_STMTSTATUS_RUN 6 #define SQLITE_STMTSTATUS_FILTER_MISS 7 #define SQLITE_STMTSTATUS_FILTER_HIT 8 #define SQLITE_STMTSTATUS_MEMUSED 99 /* ** CAPI3REF: Custom Page Cache Object ** ** The sqlite3_pcache type is opaque. It is implemented by ** the pluggable module. The SQLite core has no knowledge of ** its size or internal structure and never deals with the ** sqlite3_pcache object except by holding and passing pointers ** to the object. ** ** See [sqlite3_pcache_methods2] for additional information. */ typedef struct sqlite3_pcache sqlite3_pcache; /* ** CAPI3REF: Custom Page Cache Object ** ** The sqlite3_pcache_page object represents a single page in the ** page cache. The page cache will allocate instances of this ** object. Various methods of the page cache use pointers to instances ** of this object as parameters or as their return value. ** ** See [sqlite3_pcache_methods2] for additional information. */ typedef struct sqlite3_pcache_page sqlite3_pcache_page; struct sqlite3_pcache_page { void *pBuf; /* The content of the page */ void *pExtra; /* Extra information associated with the page */ }; /* ** CAPI3REF: Application Defined Page Cache. ** KEYWORDS: {page cache} ** ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can ** register an alternative page cache implementation by passing in an ** instance of the sqlite3_pcache_methods2 structure.)^ ** In many applications, most of the heap memory allocated by ** SQLite is used for the page cache. ** By implementing a ** custom page cache using this API, an application can better control ** the amount of memory consumed by SQLite, the way in which ** that memory is allocated and released, and the policies used to ** determine exactly which parts of a database file are cached and for ** how long. ** ** The alternative page cache mechanism is an ** extreme measure that is only needed by the most demanding applications. ** The built-in page cache is recommended for most uses. ** ** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an ** internal buffer by SQLite within the call to [sqlite3_config]. Hence ** the application may discard the parameter after the call to ** [sqlite3_config()] returns.)^ ** ** [[the xInit() page cache method]] ** ^(The xInit() method is called once for each effective ** call to [sqlite3_initialize()])^ ** (usually only once during the lifetime of the process). ^(The xInit() ** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^ ** The intent of the xInit() method is to set up global data structures ** required by the custom page cache implementation. ** ^(If the xInit() method is NULL, then the ** built-in default page cache is used instead of the application defined ** page cache.)^ ** ** [[the xShutdown() page cache method]] ** ^The xShutdown() method is called by [sqlite3_shutdown()]. ** It can be used to clean up ** any outstanding resources before process shutdown, if required. ** ^The xShutdown() method may be NULL. ** ** ^SQLite automatically serializes calls to the xInit method, ** so the xInit method need not be threadsafe. ^The ** xShutdown method is only called from [sqlite3_shutdown()] so it does ** not need to be threadsafe either. All other methods must be threadsafe ** in multithreaded applications. ** ** ^SQLite will never invoke xInit() more than once without an intervening ** call to xShutdown(). ** ** [[the xCreate() page cache methods]] ** ^SQLite invokes the xCreate() method to construct a new cache instance. ** SQLite will typically create one cache instance for each open database file, ** though this is not guaranteed. ^The ** first parameter, szPage, is the size in bytes of the pages that must ** be allocated by the cache. ^szPage will always be a power of two. ^The ** second parameter szExtra is a number of bytes of extra storage ** associated with each page cache entry. ^The szExtra parameter will be ** a number less than 250. SQLite will use the ** extra szExtra bytes on each page to store metadata about the underlying ** database page on disk. The value passed into szExtra depends ** on the SQLite version, the target platform, and how SQLite was compiled. ** ^The third argument to xCreate(), bPurgeable, is true if the cache being ** created will be used to cache database pages of a file stored on disk, or ** false if it is used for an in-memory database. The cache implementation ** does not have to do anything special based upon the value of bPurgeable; ** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will ** never invoke xUnpin() except to deliberately delete a page. ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to ** false will always have the "discard" flag set to true. ** ^Hence, a cache created with bPurgeable set to false will ** never contain any unpinned pages. ** ** [[the xCachesize() page cache method]] ** ^(The xCachesize() method may be called at any time by SQLite to set the ** suggested maximum cache-size (number of pages stored) for the cache ** instance passed as the first argument. This is the value configured using ** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable ** parameter, the implementation is not required to do anything with this ** value; it is advisory only. ** ** [[the xPagecount() page cache methods]] ** The xPagecount() method must return the number of pages currently ** stored in the cache, both pinned and unpinned. ** ** [[the xFetch() page cache methods]] ** The xFetch() method locates a page in the cache and returns a pointer to ** an sqlite3_pcache_page object associated with that page, or a NULL pointer. ** The pBuf element of the returned sqlite3_pcache_page object will be a ** pointer to a buffer of szPage bytes used to store the content of a ** single database page. The pExtra element of sqlite3_pcache_page will be ** a pointer to the szExtra bytes of extra storage that SQLite has requested ** for each entry in the page cache. ** ** The page to be fetched is determined by the key. ^The minimum key value ** is 1. After it has been retrieved using xFetch, the page is considered ** to be "pinned". ** ** If the requested page is already in the page cache, then the page cache ** implementation must return a pointer to the page buffer with its content ** intact. If the requested page is not already in the cache, then the ** cache implementation should use the value of the createFlag ** parameter to help it determine what action to take: ** ** **
createFlag Behavior when page is not already in cache **
0 Do not allocate a new page. Return NULL. **
1 Allocate a new page if it is easy and convenient to do so. ** Otherwise return NULL. **
2 Make every effort to allocate a new page. Only return ** NULL if allocating a new page is effectively impossible. **
** ** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite ** will only use a createFlag of 2 after a prior call with a createFlag of 1 ** failed.)^ In between the xFetch() calls, SQLite may ** attempt to unpin one or more cache pages by spilling the content of ** pinned pages to disk and synching the operating system disk cache. ** ** [[the xUnpin() page cache method]] ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page ** as its second argument. If the third parameter, discard, is non-zero, ** then the page must be evicted from the cache. ** ^If the discard parameter is ** zero, then the page may be discarded or retained at the discretion of the ** page cache implementation. ^The page cache implementation ** may choose to evict unpinned pages at any time. ** ** The cache must not perform any reference counting. A single ** call to xUnpin() unpins the page regardless of the number of prior calls ** to xFetch(). ** ** [[the xRekey() page cache methods]] ** The xRekey() method is used to change the key value associated with the ** page passed as the second argument. If the cache ** previously contains an entry associated with newKey, it must be ** discarded. ^Any prior cache entry associated with newKey is guaranteed not ** to be pinned. ** ** When SQLite calls the xTruncate() method, the cache must discard all ** existing cache entries with page numbers (keys) greater than or equal ** to the value of the iLimit parameter passed to xTruncate(). If any ** of these pages are pinned, they become implicitly unpinned, meaning that ** they can be safely discarded. ** ** [[the xDestroy() page cache method]] ** ^The xDestroy() method is used to delete a cache allocated by xCreate(). ** All resources associated with the specified cache should be freed. ^After ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*] ** handle invalid, and will not use it with any other sqlite3_pcache_methods2 ** functions. ** ** [[the xShrink() page cache method]] ** ^SQLite invokes the xShrink() method when it wants the page cache to ** free up as much of heap memory as possible. The page cache implementation ** is not obligated to free any memory, but well-behaved implementations should ** do their best. */ typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2; struct sqlite3_pcache_methods2 { int iVersion; void *pArg; int (*xInit)(void*); void (*xShutdown)(void*); sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable); void (*xCachesize)(sqlite3_pcache*, int nCachesize); int (*xPagecount)(sqlite3_pcache*); sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag); void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard); void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*, unsigned oldKey, unsigned newKey); void (*xTruncate)(sqlite3_pcache*, unsigned iLimit); void (*xDestroy)(sqlite3_pcache*); void (*xShrink)(sqlite3_pcache*); }; /* ** This is the obsolete pcache_methods object that has now been replaced ** by sqlite3_pcache_methods2. This object is not used by SQLite. It is ** retained in the header file for backwards compatibility only. */ typedef struct sqlite3_pcache_methods sqlite3_pcache_methods; struct sqlite3_pcache_methods { void *pArg; int (*xInit)(void*); void (*xShutdown)(void*); sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable); void (*xCachesize)(sqlite3_pcache*, int nCachesize); int (*xPagecount)(sqlite3_pcache*); void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag); void (*xUnpin)(sqlite3_pcache*, void*, int discard); void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey); void (*xTruncate)(sqlite3_pcache*, unsigned iLimit); void (*xDestroy)(sqlite3_pcache*); }; /* ** CAPI3REF: Online Backup Object ** ** The sqlite3_backup object records state information about an ongoing ** online backup operation. ^The sqlite3_backup object is created by ** a call to [sqlite3_backup_init()] and is destroyed by a call to ** [sqlite3_backup_finish()]. ** ** See Also: [Using the SQLite Online Backup API] */ typedef struct sqlite3_backup sqlite3_backup; /* ** CAPI3REF: Online Backup API. ** ** The backup API copies the content of one database into another. ** It is useful either for creating backups of databases or ** for copying in-memory databases to or from persistent files. ** ** See Also: [Using the SQLite Online Backup API] ** ** ^SQLite holds a write transaction open on the destination database file ** for the duration of the backup operation. ** ^The source database is read-locked only while it is being read; ** it is not locked continuously for the entire backup operation. ** ^Thus, the backup may be performed on a live source database without ** preventing other database connections from ** reading or writing to the source database while the backup is underway. ** ** ^(To perform a backup operation: **
    **
  1. sqlite3_backup_init() is called once to initialize the ** backup, **
  2. sqlite3_backup_step() is called one or more times to transfer ** the data between the two databases, and finally **
  3. sqlite3_backup_finish() is called to release all resources ** associated with the backup operation. **
)^ ** There should be exactly one call to sqlite3_backup_finish() for each ** successful call to sqlite3_backup_init(). ** ** [[sqlite3_backup_init()]] sqlite3_backup_init() ** ** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the ** [database connection] associated with the destination database ** and the database name, respectively. ** ^The database name is "main" for the main database, "temp" for the ** temporary database, or the name specified after the AS keyword in ** an [ATTACH] statement for an attached database. ** ^The S and M arguments passed to ** sqlite3_backup_init(D,N,S,M) identify the [database connection] ** and database name of the source database, respectively. ** ^The source and destination [database connections] (parameters S and D) ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with ** an error. ** ** ^A call to sqlite3_backup_init() will fail, returning NULL, if ** there is already a read or read-write transaction open on the ** destination database. ** ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is ** returned and an error code and error message are stored in the ** destination [database connection] D. ** ^The error code and message for the failed call to sqlite3_backup_init() ** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or ** [sqlite3_errmsg16()] functions. ** ^A successful call to sqlite3_backup_init() returns a pointer to an ** [sqlite3_backup] object. ** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and ** sqlite3_backup_finish() functions to perform the specified backup ** operation. ** ** [[sqlite3_backup_step()]] sqlite3_backup_step() ** ** ^Function sqlite3_backup_step(B,N) will copy up to N pages between ** the source and destination databases specified by [sqlite3_backup] object B. ** ^If N is negative, all remaining source pages are copied. ** ^If sqlite3_backup_step(B,N) successfully copies N pages and there ** are still more pages to be copied, then the function returns [SQLITE_OK]. ** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages ** from source to destination, then it returns [SQLITE_DONE]. ** ^If an error occurs while running sqlite3_backup_step(B,N), ** then an [error code] is returned. ^As well as [SQLITE_OK] and ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY], ** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code. ** ** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if **
    **
  1. the destination database was opened read-only, or **
  2. the destination database is using write-ahead-log journaling ** and the destination and source page sizes differ, or **
  3. the destination database is an in-memory database and the ** destination and source page sizes differ. **
)^ ** ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then ** the [sqlite3_busy_handler | busy-handler function] ** is invoked (if one is specified). ^If the ** busy-handler returns non-zero before the lock is available, then ** [SQLITE_BUSY] is returned to the caller. ^In this case the call to ** sqlite3_backup_step() can be retried later. ^If the source ** [database connection] ** is being used to write to the source database when sqlite3_backup_step() ** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this ** case the call to sqlite3_backup_step() can be retried later on. ^(If ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or ** [SQLITE_READONLY] is returned, then ** there is no point in retrying the call to sqlite3_backup_step(). These ** errors are considered fatal.)^ The application must accept ** that the backup operation has failed and pass the backup operation handle ** to the sqlite3_backup_finish() to release associated resources. ** ** ^The first call to sqlite3_backup_step() obtains an exclusive lock ** on the destination file. ^The exclusive lock is not released until either ** sqlite3_backup_finish() is called or the backup operation is complete ** and sqlite3_backup_step() returns [SQLITE_DONE]. ^Every call to ** sqlite3_backup_step() obtains a [shared lock] on the source database that ** lasts for the duration of the sqlite3_backup_step() call. ** ^Because the source database is not locked between calls to ** sqlite3_backup_step(), the source database may be modified mid-way ** through the backup process. ^If the source database is modified by an ** external process or via a database connection other than the one being ** used by the backup operation, then the backup will be automatically ** restarted by the next call to sqlite3_backup_step(). ^If the source ** database is modified by using the same database connection as is used ** by the backup operation, then the backup database is automatically ** updated at the same time. ** ** [[sqlite3_backup_finish()]] sqlite3_backup_finish() ** ** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the ** application wishes to abandon the backup operation, the application ** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish(). ** ^The sqlite3_backup_finish() interfaces releases all ** resources associated with the [sqlite3_backup] object. ** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any ** active write-transaction on the destination database is rolled back. ** The [sqlite3_backup] object is invalid ** and may not be used following a call to sqlite3_backup_finish(). ** ** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no ** sqlite3_backup_step() errors occurred, regardless of whether or not ** sqlite3_backup_step() completed. ** ^If an out-of-memory condition or IO error occurred during any prior ** sqlite3_backup_step() call on the same [sqlite3_backup] object, then ** sqlite3_backup_finish() returns the corresponding [error code]. ** ** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step() ** is not a permanent error and does not affect the return value of ** sqlite3_backup_finish(). ** ** [[sqlite3_backup_remaining()]] [[sqlite3_backup_pagecount()]] ** sqlite3_backup_remaining() and sqlite3_backup_pagecount() ** ** ^The sqlite3_backup_remaining() routine returns the number of pages still ** to be backed up at the conclusion of the most recent sqlite3_backup_step(). ** ^The sqlite3_backup_pagecount() routine returns the total number of pages ** in the source database at the conclusion of the most recent ** sqlite3_backup_step(). ** ^(The values returned by these functions are only updated by ** sqlite3_backup_step(). If the source database is modified in a way that ** changes the size of the source database or the number of pages remaining, ** those changes are not reflected in the output of sqlite3_backup_pagecount() ** and sqlite3_backup_remaining() until after the next ** sqlite3_backup_step().)^ ** ** Concurrent Usage of Database Handles ** ** ^The source [database connection] may be used by the application for other ** purposes while a backup operation is underway or being initialized. ** ^If SQLite is compiled and configured to support threadsafe database ** connections, then the source database connection may be used concurrently ** from within other threads. ** ** However, the application must guarantee that the destination ** [database connection] is not passed to any other API (by any thread) after ** sqlite3_backup_init() is called and before the corresponding call to ** sqlite3_backup_finish(). SQLite does not currently check to see ** if the application incorrectly accesses the destination [database connection] ** and so no error code is reported, but the operations may malfunction ** nevertheless. Use of the destination database connection while a ** backup is in progress might also cause a mutex deadlock. ** ** If running in [shared cache mode], the application must ** guarantee that the shared cache used by the destination database ** is not accessed while the backup is running. In practice this means ** that the application must guarantee that the disk file being ** backed up to is not accessed by any connection within the process, ** not just the specific connection that was passed to sqlite3_backup_init(). ** ** The [sqlite3_backup] object itself is partially threadsafe. Multiple ** threads may safely make multiple concurrent calls to sqlite3_backup_step(). ** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount() ** APIs are not strictly speaking threadsafe. If they are invoked at the ** same time as another thread is invoking sqlite3_backup_step() it is ** possible that they return invalid values. ** ** Alternatives To Using The Backup API ** ** Other techniques for safely creating a consistent backup of an SQLite ** database include: ** **
    **
  • The [VACUUM INTO] command. **
  • The [sqlite3_rsync] utility program. **
*/ SQLITE_API sqlite3_backup *sqlite3_backup_init( sqlite3 *pDest, /* Destination database handle */ const char *zDestName, /* Destination database name */ sqlite3 *pSource, /* Source database handle */ const char *zSourceName /* Source database name */ ); SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage); SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p); SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p); SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p); /* ** CAPI3REF: Unlock Notification ** METHOD: sqlite3 ** ** ^When running in shared-cache mode, a database operation may fail with ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or ** individual tables within the shared-cache cannot be obtained. See ** [SQLite Shared-Cache Mode] for a description of shared-cache locking. ** ^This API may be used to register a callback that SQLite will invoke ** when the connection currently holding the required lock relinquishes it. ** ^This API is only available if the library was compiled with the ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined. ** ** See Also: [Using the SQLite Unlock Notification Feature]. ** ** ^Shared-cache locks are released when a database connection concludes ** its current transaction, either by committing it or rolling it back. ** ** ^When a connection (known as the blocked connection) fails to obtain a ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the ** identity of the database connection (the blocking connection) that ** has locked the required resource is stored internally. ^After an ** application receives an SQLITE_LOCKED error, it may call the ** sqlite3_unlock_notify() method with the blocked connection handle as ** the first argument to register for a callback that will be invoked ** when the blocking connection's current transaction is concluded. ^The ** callback is invoked from within the [sqlite3_step] or [sqlite3_close] ** call that concludes the blocking connection's transaction. ** ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application, ** there is a chance that the blocking connection will have already ** concluded its transaction by the time sqlite3_unlock_notify() is invoked. ** If this happens, then the specified callback is invoked immediately, ** from within the call to sqlite3_unlock_notify().)^ ** ** ^If the blocked connection is attempting to obtain a write-lock on a ** shared-cache table, and more than one other connection currently holds ** a read-lock on the same table, then SQLite arbitrarily selects one of ** the other connections to use as the blocking connection. ** ** ^(There may be at most one unlock-notify callback registered by a ** blocked connection. If sqlite3_unlock_notify() is called when the ** blocked connection already has a registered unlock-notify callback, ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is ** called with a NULL pointer as its second argument, then any existing ** unlock-notify callback is canceled. ^The blocked connection's ** unlock-notify callback may also be canceled by closing the blocked ** connection using [sqlite3_close()]. ** ** The unlock-notify callback is not reentrant. If an application invokes ** any sqlite3_xxx API functions from within an unlock-notify callback, a ** crash or deadlock may be the result. ** ** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always ** returns SQLITE_OK. ** ** Callback Invocation Details ** ** When an unlock-notify callback is registered, the application provides a ** single void* pointer that is passed to the callback when it is invoked. ** However, the signature of the callback function allows SQLite to pass ** it an array of void* context pointers. The first argument passed to ** an unlock-notify callback is a pointer to an array of void* pointers, ** and the second is the number of entries in the array. ** ** When a blocking connection's transaction is concluded, there may be ** more than one blocked connection that has registered for an unlock-notify ** callback. ^If two or more such blocked connections have specified the ** same callback function, then instead of invoking the callback function ** multiple times, it is invoked once with the set of void* context pointers ** specified by the blocked connections bundled together into an array. ** This gives the application an opportunity to prioritize any actions ** related to the set of unblocked database connections. ** ** Deadlock Detection ** ** Assuming that after registering for an unlock-notify callback a ** database waits for the callback to be issued before taking any further ** action (a reasonable assumption), then using this API may cause the ** application to deadlock. For example, if connection X is waiting for ** connection Y's transaction to be concluded, and similarly connection ** Y is waiting on connection X's transaction, then neither connection ** will proceed and the system may remain deadlocked indefinitely. ** ** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock ** detection. ^If a given call to sqlite3_unlock_notify() would put the ** system in a deadlocked state, then SQLITE_LOCKED is returned and no ** unlock-notify callback is registered. The system is said to be in ** a deadlocked state if connection A has registered for an unlock-notify ** callback on the conclusion of connection B's transaction, and connection ** B has itself registered for an unlock-notify callback when connection ** A's transaction is concluded. ^Indirect deadlock is also detected, so ** the system is also considered to be deadlocked if connection B has ** registered for an unlock-notify callback on the conclusion of connection ** C's transaction, where connection C is waiting on connection A. ^Any ** number of levels of indirection are allowed. ** ** The "DROP TABLE" Exception ** ** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost ** always appropriate to call sqlite3_unlock_notify(). There is however, ** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement, ** SQLite checks if there are any currently executing SELECT statements ** that belong to the same connection. If there are, SQLITE_LOCKED is ** returned. In this case there is no "blocking connection", so invoking ** sqlite3_unlock_notify() results in the unlock-notify callback being ** invoked immediately. If the application then re-attempts the "DROP TABLE" ** or "DROP INDEX" query, an infinite loop might be the result. ** ** One way around this problem is to check the extended error code returned ** by an sqlite3_step() call. ^(If there is a blocking connection, then the ** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in ** the special "DROP TABLE/INDEX" case, the extended error code is just ** SQLITE_LOCKED.)^ */ SQLITE_API int sqlite3_unlock_notify( sqlite3 *pBlocked, /* Waiting connection */ void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */ void *pNotifyArg /* Argument to pass to xNotify */ ); /* ** CAPI3REF: String Comparison ** ** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications ** and extensions to compare the contents of two buffers containing UTF-8 ** strings in a case-independent fashion, using the same definition of "case ** independence" that SQLite uses internally when comparing identifiers. */ SQLITE_API int sqlite3_stricmp(const char *, const char *); SQLITE_API int sqlite3_strnicmp(const char *, const char *, int); /* ** CAPI3REF: String Globbing * ** ^The [sqlite3_strglob(P,X)] interface returns zero if and only if ** string X matches the [GLOB] pattern P. ** ^The definition of [GLOB] pattern matching used in ** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the ** SQL dialect understood by SQLite. ^The [sqlite3_strglob(P,X)] function ** is case sensitive. ** ** Note that this routine returns zero on a match and non-zero if the strings ** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()]. ** ** See also: [sqlite3_strlike()]. */ SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr); /* ** CAPI3REF: String LIKE Matching * ** ^The [sqlite3_strlike(P,X,E)] interface returns zero if and only if ** string X matches the [LIKE] pattern P with escape character E. ** ^The definition of [LIKE] pattern matching used in ** [sqlite3_strlike(P,X,E)] is the same as for the "X LIKE P ESCAPE E" ** operator in the SQL dialect understood by SQLite. ^For "X LIKE P" without ** the ESCAPE clause, set the E parameter of [sqlite3_strlike(P,X,E)] to 0. ** ^As with the LIKE operator, the [sqlite3_strlike(P,X,E)] function is case ** insensitive - equivalent upper and lower case ASCII characters match ** one another. ** ** ^The [sqlite3_strlike(P,X,E)] function matches Unicode characters, though ** only ASCII characters are case folded. ** ** Note that this routine returns zero on a match and non-zero if the strings ** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()]. ** ** See also: [sqlite3_strglob()]. */ SQLITE_API int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc); /* ** CAPI3REF: Error Logging Interface ** ** ^The [sqlite3_log()] interface writes a message into the [error log] ** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()]. ** ^If logging is enabled, the zFormat string and subsequent arguments are ** used with [sqlite3_snprintf()] to generate the final output string. ** ** The sqlite3_log() interface is intended for use by extensions such as ** virtual tables, collating functions, and SQL functions. While there is ** nothing to prevent an application from calling sqlite3_log(), doing so ** is considered bad form. ** ** The zFormat string must not be NULL. ** ** To avoid deadlocks and other threading problems, the sqlite3_log() routine ** will not use dynamically allocated memory. The log message is stored in ** a fixed-length buffer on the stack. If the log message is longer than ** a few hundred characters, it will be truncated to the length of the ** buffer. */ SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...); /* ** CAPI3REF: Write-Ahead Log Commit Hook ** METHOD: sqlite3 ** ** ^The [sqlite3_wal_hook()] function is used to register a callback that ** is invoked each time data is committed to a database in wal mode. ** ** ^(The callback is invoked by SQLite after the commit has taken place and ** the associated write-lock on the database released)^, so the implementation ** may read, write or [checkpoint] the database as required. ** ** ^The first parameter passed to the callback function when it is invoked ** is a copy of the third parameter passed to sqlite3_wal_hook() when ** registering the callback. ^The second is a copy of the database handle. ** ^The third parameter is the name of the database that was written to - ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter ** is the number of pages currently in the write-ahead log file, ** including those that were just committed. ** ** The callback function should normally return [SQLITE_OK]. ^If an error ** code is returned, that error will propagate back up through the ** SQLite code base to cause the statement that provoked the callback ** to report an error, though the commit will have still occurred. If the ** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value ** that does not correspond to any valid SQLite error code, the results ** are undefined. ** ** A single database handle may have at most a single write-ahead log callback ** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any ** previously registered write-ahead log callback. ^The return value is ** a copy of the third parameter from the previous call, if any, or 0. ** ^Note that the [sqlite3_wal_autocheckpoint()] interface and the ** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will ** overwrite any prior [sqlite3_wal_hook()] settings. */ SQLITE_API void *sqlite3_wal_hook( sqlite3*, int(*)(void *,sqlite3*,const char*,int), void* ); /* ** CAPI3REF: Configure an auto-checkpoint ** METHOD: sqlite3 ** ** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around ** [sqlite3_wal_hook()] that causes any database on [database connection] D ** to automatically [checkpoint] ** after committing a transaction if there are N or ** more frames in the [write-ahead log] file. ^Passing zero or ** a negative value as the nFrame parameter disables automatic ** checkpoints entirely. ** ** ^The callback registered by this function replaces any existing callback ** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism ** configured by this function. ** ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface ** from SQL. ** ** ^Checkpoints initiated by this mechanism are ** [sqlite3_wal_checkpoint_v2|PASSIVE]. ** ** ^Every new [database connection] defaults to having the auto-checkpoint ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT] ** pages. The use of this interface ** is only necessary if the default setting is found to be suboptimal ** for a particular application. */ SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N); /* ** CAPI3REF: Checkpoint a database ** METHOD: sqlite3 ** ** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to ** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^ ** ** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the ** [write-ahead log] for database X on [database connection] D to be ** transferred into the database file and for the write-ahead log to ** be reset. See the [checkpointing] documentation for addition ** information. ** ** This interface used to be the only way to cause a checkpoint to ** occur. But then the newer and more powerful [sqlite3_wal_checkpoint_v2()] ** interface was added. This interface is retained for backwards ** compatibility and as a convenience for applications that need to manually ** start a callback but which do not need the full power (and corresponding ** complication) of [sqlite3_wal_checkpoint_v2()]. */ SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb); /* ** CAPI3REF: Checkpoint a database ** METHOD: sqlite3 ** ** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint ** operation on database X of [database connection] D in mode M. Status ** information is written back into integers pointed to by L and C.)^ ** ^(The M parameter must be a valid [checkpoint mode]:)^ ** **
**
SQLITE_CHECKPOINT_PASSIVE
** ^Checkpoint as many frames as possible without waiting for any database ** readers or writers to finish, then sync the database file if all frames ** in the log were checkpointed. ^The [busy-handler callback] ** is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode. ** ^On the other hand, passive mode might leave the checkpoint unfinished ** if there are concurrent readers or writers. ** **
SQLITE_CHECKPOINT_FULL
** ^This mode blocks (it invokes the ** [sqlite3_busy_handler|busy-handler callback]) until there is no ** database writer and all readers are reading from the most recent database ** snapshot. ^It then checkpoints all frames in the log file and syncs the ** database file. ^This mode blocks new database writers while it is pending, ** but new database readers are allowed to continue unimpeded. ** **
SQLITE_CHECKPOINT_RESTART
** ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition ** that after checkpointing the log file it blocks (calls the ** [busy-handler callback]) ** until all readers are reading from the database file only. ^This ensures ** that the next writer will restart the log file from the beginning. ** ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new ** database writer attempts while it is pending, but does not impede readers. ** **
SQLITE_CHECKPOINT_TRUNCATE
** ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the ** addition that it also truncates the log file to zero bytes just prior ** to a successful return. **
** ** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in ** the log file or to -1 if the checkpoint could not run because ** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not ** NULL,then *pnCkpt is set to the total number of checkpointed frames in the ** log file (including any that were already checkpointed before the function ** was called) or to -1 if the checkpoint could not run due to an error or ** because the database is not in WAL mode. ^Note that upon successful ** completion of an SQLITE_CHECKPOINT_TRUNCATE, the log file will have been ** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero. ** ** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If ** any other process is running a checkpoint operation at the same time, the ** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a ** busy-handler configured, it will not be invoked in this case. ** ** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the ** exclusive "writer" lock on the database file. ^If the writer lock cannot be ** obtained immediately, and a busy-handler is configured, it is invoked and ** the writer lock retried until either the busy-handler returns 0 or the lock ** is successfully obtained. ^The busy-handler is also invoked while waiting for ** database readers as described above. ^If the busy-handler returns 0 before ** the writer lock is obtained or while waiting for database readers, the ** checkpoint operation proceeds from that point in the same way as ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible ** without blocking any further. ^SQLITE_BUSY is returned in this case. ** ** ^If parameter zDb is NULL or points to a zero length string, then the ** specified operation is attempted on all WAL databases [attached] to ** [database connection] db. In this case the ** values written to output parameters *pnLog and *pnCkpt are undefined. ^If ** an SQLITE_BUSY error is encountered when processing one or more of the ** attached WAL databases, the operation is still attempted on any remaining ** attached databases and SQLITE_BUSY is returned at the end. ^If any other ** error occurs while processing an attached database, processing is abandoned ** and the error code is returned to the caller immediately. ^If no error ** (SQLITE_BUSY or otherwise) is encountered while processing the attached ** databases, SQLITE_OK is returned. ** ** ^If database zDb is the name of an attached database that is not in WAL ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If ** zDb is not NULL (or a zero length string) and is not the name of any ** attached database, SQLITE_ERROR is returned to the caller. ** ** ^Unless it returns SQLITE_MISUSE, ** the sqlite3_wal_checkpoint_v2() interface ** sets the error information that is queried by ** [sqlite3_errcode()] and [sqlite3_errmsg()]. ** ** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface ** from SQL. */ SQLITE_API int sqlite3_wal_checkpoint_v2( sqlite3 *db, /* Database handle */ const char *zDb, /* Name of attached database (or NULL) */ int eMode, /* SQLITE_CHECKPOINT_* value */ int *pnLog, /* OUT: Size of WAL log in frames */ int *pnCkpt /* OUT: Total number of frames checkpointed */ ); /* ** CAPI3REF: Checkpoint Mode Values ** KEYWORDS: {checkpoint mode} ** ** These constants define all valid values for the "checkpoint mode" passed ** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface. ** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the ** meaning of each of these checkpoint modes. */ #define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */ #define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */ #define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for readers */ #define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */ /* ** CAPI3REF: Virtual Table Interface Configuration ** ** This function may be called by either the [xConnect] or [xCreate] method ** of a [virtual table] implementation to configure ** various facets of the virtual table interface. ** ** If this interface is invoked outside the context of an xConnect or ** xCreate virtual table method then the behavior is undefined. ** ** In the call sqlite3_vtab_config(D,C,...) the D parameter is the ** [database connection] in which the virtual table is being created and ** which is passed in as the first argument to the [xConnect] or [xCreate] ** method that is invoking sqlite3_vtab_config(). The C parameter is one ** of the [virtual table configuration options]. The presence and meaning ** of parameters after C depend on which [virtual table configuration option] ** is used. */ SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...); /* ** CAPI3REF: Virtual Table Configuration Options ** KEYWORDS: {virtual table configuration options} ** KEYWORDS: {virtual table configuration option} ** ** These macros define the various options to the ** [sqlite3_vtab_config()] interface that [virtual table] implementations ** can use to customize and optimize their behavior. ** **
** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]] **
SQLITE_VTAB_CONSTRAINT_SUPPORT
**
Calls of the form ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported, ** where X is an integer. If X is zero, then the [virtual table] whose ** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not ** support constraints. In this configuration (which is the default) if ** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire ** statement is rolled back as if [ON CONFLICT | OR ABORT] had been ** specified as part of the user's SQL statement, regardless of the actual ** ON CONFLICT mode specified. ** ** If X is non-zero, then the virtual table implementation guarantees ** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before ** any modifications to internal or persistent data structures have been made. ** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite ** is able to roll back a statement or database transaction, and abandon ** or continue processing the current SQL statement as appropriate. ** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns ** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode ** had been ABORT. ** ** Virtual table implementations that are required to handle OR REPLACE ** must do so within the [xUpdate] method. If a call to the ** [sqlite3_vtab_on_conflict()] function indicates that the current ON ** CONFLICT policy is REPLACE, the virtual table implementation should ** silently replace the appropriate rows within the xUpdate callback and ** return SQLITE_OK. Or, if this is not possible, it may return ** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT ** constraint handling. **
** ** [[SQLITE_VTAB_DIRECTONLY]]
SQLITE_VTAB_DIRECTONLY
**
Calls of the form ** [sqlite3_vtab_config](db,SQLITE_VTAB_DIRECTONLY) from within the ** the [xConnect] or [xCreate] methods of a [virtual table] implementation ** prohibits that virtual table from being used from within triggers and ** views. **
** ** [[SQLITE_VTAB_INNOCUOUS]]
SQLITE_VTAB_INNOCUOUS
**
Calls of the form ** [sqlite3_vtab_config](db,SQLITE_VTAB_INNOCUOUS) from within the ** [xConnect] or [xCreate] methods of a [virtual table] implementation ** identify that virtual table as being safe to use from within triggers ** and views. Conceptually, the SQLITE_VTAB_INNOCUOUS tag means that the ** virtual table can do no serious harm even if it is controlled by a ** malicious hacker. Developers should avoid setting the SQLITE_VTAB_INNOCUOUS ** flag unless absolutely necessary. **
** ** [[SQLITE_VTAB_USES_ALL_SCHEMAS]]
SQLITE_VTAB_USES_ALL_SCHEMAS
**
Calls of the form ** [sqlite3_vtab_config](db,SQLITE_VTAB_USES_ALL_SCHEMA) from within the ** the [xConnect] or [xCreate] methods of a [virtual table] implementation ** instruct the query planner to begin at least a read transaction on ** all schemas ("main", "temp", and any ATTACH-ed databases) whenever the ** virtual table is used. **
**
*/ #define SQLITE_VTAB_CONSTRAINT_SUPPORT 1 #define SQLITE_VTAB_INNOCUOUS 2 #define SQLITE_VTAB_DIRECTONLY 3 #define SQLITE_VTAB_USES_ALL_SCHEMAS 4 /* ** CAPI3REF: Determine The Virtual Table Conflict Policy ** ** This function may only be called from within a call to the [xUpdate] method ** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The ** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL], ** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode ** of the SQL statement that triggered the call to the [xUpdate] method of the ** [virtual table]. */ SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *); /* ** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE ** ** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn] ** method of a [virtual table], then it might return true if the ** column is being fetched as part of an UPDATE operation during which the ** column value will not change. The virtual table implementation can use ** this hint as permission to substitute a return value that is less ** expensive to compute and that the corresponding ** [xUpdate] method understands as a "no-change" value. ** ** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that ** the column is not changed by the UPDATE statement, then the xColumn ** method can optionally return without setting a result, without calling ** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces]. ** In that case, [sqlite3_value_nochange(X)] will return true for the ** same column in the [xUpdate] method. ** ** The sqlite3_vtab_nochange() routine is an optimization. Virtual table ** implementations should continue to give a correct answer even if the ** sqlite3_vtab_nochange() interface were to always return false. In the ** current implementation, the sqlite3_vtab_nochange() interface does always ** returns false for the enhanced [UPDATE FROM] statement. */ SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*); /* ** CAPI3REF: Determine The Collation For a Virtual Table Constraint ** METHOD: sqlite3_index_info ** ** This function may only be called from within a call to the [xBestIndex] ** method of a [virtual table]. This function returns a pointer to a string ** that is the name of the appropriate collation sequence to use for text ** comparisons on the constraint identified by its arguments. ** ** The first argument must be the pointer to the [sqlite3_index_info] object ** that is the first parameter to the xBestIndex() method. The second argument ** must be an index into the aConstraint[] array belonging to the ** sqlite3_index_info structure passed to xBestIndex. ** ** Important: ** The first parameter must be the same pointer that is passed into the ** xBestMethod() method. The first parameter may not be a pointer to a ** different [sqlite3_index_info] object, even an exact copy. ** ** The return value is computed as follows: ** **
    **
  1. If the constraint comes from a WHERE clause expression that contains ** a [COLLATE operator], then the name of the collation specified by ** that COLLATE operator is returned. **

  2. If there is no COLLATE operator, but the column that is the subject ** of the constraint specifies an alternative collating sequence via ** a [COLLATE clause] on the column definition within the CREATE TABLE ** statement that was passed into [sqlite3_declare_vtab()], then the ** name of that alternative collating sequence is returned. **

  3. Otherwise, "BINARY" is returned. **

*/ SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info*,int); /* ** CAPI3REF: Determine if a virtual table query is DISTINCT ** METHOD: sqlite3_index_info ** ** This API may only be used from within an [xBestIndex|xBestIndex method] ** of a [virtual table] implementation. The result of calling this ** interface from outside of xBestIndex() is undefined and probably harmful. ** ** ^The sqlite3_vtab_distinct() interface returns an integer between 0 and ** 3. The integer returned by sqlite3_vtab_distinct() ** gives the virtual table additional information about how the query ** planner wants the output to be ordered. As long as the virtual table ** can meet the ordering requirements of the query planner, it may set ** the "orderByConsumed" flag. ** **
  1. ** ^If the sqlite3_vtab_distinct() interface returns 0, that means ** that the query planner needs the virtual table to return all rows in the ** sort order defined by the "nOrderBy" and "aOrderBy" fields of the ** [sqlite3_index_info] object. This is the default expectation. If the ** virtual table outputs all rows in sorted order, then it is always safe for ** the xBestIndex method to set the "orderByConsumed" flag, regardless of ** the return value from sqlite3_vtab_distinct(). **

  2. ** ^(If the sqlite3_vtab_distinct() interface returns 1, that means ** that the query planner does not need the rows to be returned in sorted order ** as long as all rows with the same values in all columns identified by the ** "aOrderBy" field are adjacent.)^ This mode is used when the query planner ** is doing a GROUP BY. **

  3. ** ^(If the sqlite3_vtab_distinct() interface returns 2, that means ** that the query planner does not need the rows returned in any particular ** order, as long as rows with the same values in all columns identified ** by "aOrderBy" are adjacent.)^ ^(Furthermore, when two or more rows ** contain the same values for all columns identified by "colUsed", all but ** one such row may optionally be omitted from the result.)^ ** The virtual table is not required to omit rows that are duplicates ** over the "colUsed" columns, but if the virtual table can do that without ** too much extra effort, it could potentially help the query to run faster. ** This mode is used for a DISTINCT query. **

  4. ** ^(If the sqlite3_vtab_distinct() interface returns 3, that means the ** virtual table must return rows in the order defined by "aOrderBy" as ** if the sqlite3_vtab_distinct() interface had returned 0. However if ** two or more rows in the result have the same values for all columns ** identified by "colUsed", then all but one such row may optionally be ** omitted.)^ Like when the return value is 2, the virtual table ** is not required to omit rows that are duplicates over the "colUsed" ** columns, but if the virtual table can do that without ** too much extra effort, it could potentially help the query to run faster. ** This mode is used for queries ** that have both DISTINCT and ORDER BY clauses. **

** **

The following table summarizes the conditions under which the ** virtual table is allowed to set the "orderByConsumed" flag based on ** the value returned by sqlite3_vtab_distinct(). This table is a ** restatement of the previous four paragraphs: ** ** ** **
sqlite3_vtab_distinct() return value ** Rows are returned in aOrderBy order ** Rows with the same value in all aOrderBy columns are adjacent ** Duplicates over all colUsed columns may be omitted **
0yesyesno **
1noyesno **
2noyesyes **
3yesyesyes **
** ** ^For the purposes of comparing virtual table output values to see if the ** values are the same value for sorting purposes, two NULL values are considered ** to be the same. In other words, the comparison operator is "IS" ** (or "IS NOT DISTINCT FROM") and not "==". ** ** If a virtual table implementation is unable to meet the requirements ** specified above, then it must not set the "orderByConsumed" flag in the ** [sqlite3_index_info] object or an incorrect answer may result. ** ** ^A virtual table implementation is always free to return rows in any order ** it wants, as long as the "orderByConsumed" flag is not set. ^When the ** "orderByConsumed" flag is unset, the query planner will add extra ** [bytecode] to ensure that the final results returned by the SQL query are ** ordered correctly. The use of the "orderByConsumed" flag and the ** sqlite3_vtab_distinct() interface is merely an optimization. ^Careful ** use of the sqlite3_vtab_distinct() interface and the "orderByConsumed" ** flag might help queries against a virtual table to run faster. Being ** overly aggressive and setting the "orderByConsumed" flag when it is not ** valid to do so, on the other hand, might cause SQLite to return incorrect ** results. */ SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info*); /* ** CAPI3REF: Identify and handle IN constraints in xBestIndex ** ** This interface may only be used from within an ** [xBestIndex|xBestIndex() method] of a [virtual table] implementation. ** The result of invoking this interface from any other context is ** undefined and probably harmful. ** ** ^(A constraint on a virtual table of the form ** "[IN operator|column IN (...)]" is ** communicated to the xBestIndex method as a ** [SQLITE_INDEX_CONSTRAINT_EQ] constraint.)^ If xBestIndex wants to use ** this constraint, it must set the corresponding ** aConstraintUsage[].argvIndex to a positive integer. ^(Then, under ** the usual mode of handling IN operators, SQLite generates [bytecode] ** that invokes the [xFilter|xFilter() method] once for each value ** on the right-hand side of the IN operator.)^ Thus the virtual table ** only sees a single value from the right-hand side of the IN operator ** at a time. ** ** In some cases, however, it would be advantageous for the virtual ** table to see all values on the right-hand of the IN operator all at ** once. The sqlite3_vtab_in() interfaces facilitates this in two ways: ** **

    **
  1. ** ^A call to sqlite3_vtab_in(P,N,-1) will return true (non-zero) ** if and only if the [sqlite3_index_info|P->aConstraint][N] constraint ** is an [IN operator] that can be processed all at once. ^In other words, ** sqlite3_vtab_in() with -1 in the third argument is a mechanism ** by which the virtual table can ask SQLite if all-at-once processing ** of the IN operator is even possible. ** **

  2. ** ^A call to sqlite3_vtab_in(P,N,F) with F==1 or F==0 indicates ** to SQLite that the virtual table does or does not want to process ** the IN operator all-at-once, respectively. ^Thus when the third ** parameter (F) is non-negative, this interface is the mechanism by ** which the virtual table tells SQLite how it wants to process the ** IN operator. **

** ** ^The sqlite3_vtab_in(P,N,F) interface can be invoked multiple times ** within the same xBestIndex method call. ^For any given P,N pair, ** the return value from sqlite3_vtab_in(P,N,F) will always be the same ** within the same xBestIndex call. ^If the interface returns true ** (non-zero), that means that the constraint is an IN operator ** that can be processed all-at-once. ^If the constraint is not an IN ** operator or cannot be processed all-at-once, then the interface returns ** false. ** ** ^(All-at-once processing of the IN operator is selected if both of the ** following conditions are met: ** **
    **
  1. The P->aConstraintUsage[N].argvIndex value is set to a positive ** integer. This is how the virtual table tells SQLite that it wants to ** use the N-th constraint. ** **

  2. The last call to sqlite3_vtab_in(P,N,F) for which F was ** non-negative had F>=1. **

)^ ** ** ^If either or both of the conditions above are false, then SQLite uses ** the traditional one-at-a-time processing strategy for the IN constraint. ** ^If both conditions are true, then the argvIndex-th parameter to the ** xFilter method will be an [sqlite3_value] that appears to be NULL, ** but which can be passed to [sqlite3_vtab_in_first()] and ** [sqlite3_vtab_in_next()] to find all values on the right-hand side ** of the IN constraint. */ SQLITE_API int sqlite3_vtab_in(sqlite3_index_info*, int iCons, int bHandle); /* ** CAPI3REF: Find all elements on the right-hand side of an IN constraint. ** ** These interfaces are only useful from within the ** [xFilter|xFilter() method] of a [virtual table] implementation. ** The result of invoking these interfaces from any other context ** is undefined and probably harmful. ** ** The X parameter in a call to sqlite3_vtab_in_first(X,P) or ** sqlite3_vtab_in_next(X,P) should be one of the parameters to the ** xFilter method which invokes these routines, and specifically ** a parameter that was previously selected for all-at-once IN constraint ** processing using the [sqlite3_vtab_in()] interface in the ** [xBestIndex|xBestIndex method]. ^(If the X parameter is not ** an xFilter argument that was selected for all-at-once IN constraint ** processing, then these routines return [SQLITE_ERROR].)^ ** ** ^(Use these routines to access all values on the right-hand side ** of the IN constraint using code like the following: ** **
**    for(rc=sqlite3_vtab_in_first(pList, &pVal);
**        rc==SQLITE_OK && pVal;
**        rc=sqlite3_vtab_in_next(pList, &pVal)
**    ){
**      // do something with pVal
**    }
**    if( rc!=SQLITE_OK ){
**      // an error has occurred
**    }
** 
)^ ** ** ^On success, the sqlite3_vtab_in_first(X,P) and sqlite3_vtab_in_next(X,P) ** routines return SQLITE_OK and set *P to point to the first or next value ** on the RHS of the IN constraint. ^If there are no more values on the ** right hand side of the IN constraint, then *P is set to NULL and these ** routines return [SQLITE_DONE]. ^The return value might be ** some other value, such as SQLITE_NOMEM, in the event of a malfunction. ** ** The *ppOut values returned by these routines are only valid until the ** next call to either of these routines or until the end of the xFilter ** method from which these routines were called. If the virtual table ** implementation needs to retain the *ppOut values for longer, it must make ** copies. The *ppOut values are [protected sqlite3_value|protected]. */ SQLITE_API int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut); SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut); /* ** CAPI3REF: Constraint values in xBestIndex() ** METHOD: sqlite3_index_info ** ** This API may only be used from within the [xBestIndex|xBestIndex method] ** of a [virtual table] implementation. The result of calling this interface ** from outside of an xBestIndex method are undefined and probably harmful. ** ** ^When the sqlite3_vtab_rhs_value(P,J,V) interface is invoked from within ** the [xBestIndex] method of a [virtual table] implementation, with P being ** a copy of the [sqlite3_index_info] object pointer passed into xBestIndex and ** J being a 0-based index into P->aConstraint[], then this routine ** attempts to set *V to the value of the right-hand operand of ** that constraint if the right-hand operand is known. ^If the ** right-hand operand is not known, then *V is set to a NULL pointer. ** ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if ** and only if *V is set to a value. ^The sqlite3_vtab_rhs_value(P,J,V) ** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th ** constraint is not available. ^The sqlite3_vtab_rhs_value() interface ** can return a result code other than SQLITE_OK or SQLITE_NOTFOUND if ** something goes wrong. ** ** The sqlite3_vtab_rhs_value() interface is usually only successful if ** the right-hand operand of a constraint is a literal value in the original ** SQL statement. If the right-hand operand is an expression or a reference ** to some other column or a [host parameter], then sqlite3_vtab_rhs_value() ** will probably return [SQLITE_NOTFOUND]. ** ** ^(Some constraints, such as [SQLITE_INDEX_CONSTRAINT_ISNULL] and ** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such ** constraints, sqlite3_vtab_rhs_value() always returns SQLITE_NOTFOUND.)^ ** ** ^The [sqlite3_value] object returned in *V is a protected sqlite3_value ** and remains valid for the duration of the xBestIndex method call. ** ^When xBestIndex returns, the sqlite3_value object returned by ** sqlite3_vtab_rhs_value() is automatically deallocated. ** ** The "_rhs_" in the name of this routine is an abbreviation for ** "Right-Hand Side". */ SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal); /* ** CAPI3REF: Conflict resolution modes ** KEYWORDS: {conflict resolution mode} ** ** These constants are returned by [sqlite3_vtab_on_conflict()] to ** inform a [virtual table] implementation of the [ON CONFLICT] mode ** for the SQL statement being evaluated. ** ** Note that the [SQLITE_IGNORE] constant is also used as a potential ** return value from the [sqlite3_set_authorizer()] callback and that ** [SQLITE_ABORT] is also a [result code]. */ #define SQLITE_ROLLBACK 1 /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */ #define SQLITE_FAIL 3 /* #define SQLITE_ABORT 4 // Also an error code */ #define SQLITE_REPLACE 5 /* ** CAPI3REF: Prepared Statement Scan Status Opcodes ** KEYWORDS: {scanstatus options} ** ** The following constants can be used for the T parameter to the ** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a ** different metric for sqlite3_stmt_scanstatus() to return. ** ** When the value returned to V is a string, space to hold that string is ** managed by the prepared statement S and will be automatically freed when ** S is finalized. ** ** Not all values are available for all query elements. When a value is ** not available, the output variable is set to -1 if the value is numeric, ** or to NULL if it is a string (SQLITE_SCANSTAT_NAME). ** **
** [[SQLITE_SCANSTAT_NLOOP]]
SQLITE_SCANSTAT_NLOOP
**
^The [sqlite3_int64] variable pointed to by the V parameter will be ** set to the total number of times that the X-th loop has run.
** ** [[SQLITE_SCANSTAT_NVISIT]]
SQLITE_SCANSTAT_NVISIT
**
^The [sqlite3_int64] variable pointed to by the V parameter will be set ** to the total number of rows examined by all iterations of the X-th loop.
** ** [[SQLITE_SCANSTAT_EST]]
SQLITE_SCANSTAT_EST
**
^The "double" variable pointed to by the V parameter will be set to the ** query planner's estimate for the average number of rows output from each ** iteration of the X-th loop. If the query planner's estimate was accurate, ** then this value will approximate the quotient NVISIT/NLOOP and the ** product of this value for all prior loops with the same SELECTID will ** be the NLOOP value for the current loop.
** ** [[SQLITE_SCANSTAT_NAME]]
SQLITE_SCANSTAT_NAME
**
^The "const char *" variable pointed to by the V parameter will be set ** to a zero-terminated UTF-8 string containing the name of the index or table ** used for the X-th loop.
** ** [[SQLITE_SCANSTAT_EXPLAIN]]
SQLITE_SCANSTAT_EXPLAIN
**
^The "const char *" variable pointed to by the V parameter will be set ** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN] ** description for the X-th loop.
** ** [[SQLITE_SCANSTAT_SELECTID]]
SQLITE_SCANSTAT_SELECTID
**
^The "int" variable pointed to by the V parameter will be set to the ** id for the X-th query plan element. The id value is unique within the ** statement. The select-id is the same value as is output in the first ** column of an [EXPLAIN QUERY PLAN] query.
** ** [[SQLITE_SCANSTAT_PARENTID]]
SQLITE_SCANSTAT_PARENTID
**
The "int" variable pointed to by the V parameter will be set to the ** id of the parent of the current query element, if applicable, or ** to zero if the query element has no parent. This is the same value as ** returned in the second column of an [EXPLAIN QUERY PLAN] query.
** ** [[SQLITE_SCANSTAT_NCYCLE]]
SQLITE_SCANSTAT_NCYCLE
**
The sqlite3_int64 output value is set to the number of cycles, ** according to the processor time-stamp counter, that elapsed while the ** query element was being processed. This value is not available for ** all query elements - if it is unavailable the output variable is ** set to -1.
**
*/ #define SQLITE_SCANSTAT_NLOOP 0 #define SQLITE_SCANSTAT_NVISIT 1 #define SQLITE_SCANSTAT_EST 2 #define SQLITE_SCANSTAT_NAME 3 #define SQLITE_SCANSTAT_EXPLAIN 4 #define SQLITE_SCANSTAT_SELECTID 5 #define SQLITE_SCANSTAT_PARENTID 6 #define SQLITE_SCANSTAT_NCYCLE 7 /* ** CAPI3REF: Prepared Statement Scan Status ** METHOD: sqlite3_stmt ** ** These interfaces return information about the predicted and measured ** performance for pStmt. Advanced applications can use this ** interface to compare the predicted and the measured performance and ** issue warnings and/or rerun [ANALYZE] if discrepancies are found. ** ** Since this interface is expected to be rarely used, it is only ** available if SQLite is compiled using the [SQLITE_ENABLE_STMT_SCANSTATUS] ** compile-time option. ** ** The "iScanStatusOp" parameter determines which status information to return. ** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior ** of this interface is undefined. ^The requested measurement is written into ** a variable pointed to by the "pOut" parameter. ** ** The "flags" parameter must be passed a mask of flags. At present only ** one flag is defined - SQLITE_SCANSTAT_COMPLEX. If SQLITE_SCANSTAT_COMPLEX ** is specified, then status information is available for all elements ** of a query plan that are reported by "EXPLAIN QUERY PLAN" output. If ** SQLITE_SCANSTAT_COMPLEX is not specified, then only query plan elements ** that correspond to query loops (the "SCAN..." and "SEARCH..." elements of ** the EXPLAIN QUERY PLAN output) are available. Invoking API ** sqlite3_stmt_scanstatus() is equivalent to calling ** sqlite3_stmt_scanstatus_v2() with a zeroed flags parameter. ** ** Parameter "idx" identifies the specific query element to retrieve statistics ** for. Query elements are numbered starting from zero. A value of -1 may ** retrieve statistics for the entire query. ^If idx is out of range ** - less than -1 or greater than or equal to the total number of query ** elements used to implement the statement - a non-zero value is returned and ** the variable that pOut points to is unchanged. ** ** See also: [sqlite3_stmt_scanstatus_reset()] */ SQLITE_API int sqlite3_stmt_scanstatus( sqlite3_stmt *pStmt, /* Prepared statement for which info desired */ int idx, /* Index of loop to report on */ int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */ void *pOut /* Result written here */ ); SQLITE_API int sqlite3_stmt_scanstatus_v2( sqlite3_stmt *pStmt, /* Prepared statement for which info desired */ int idx, /* Index of loop to report on */ int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */ int flags, /* Mask of flags defined below */ void *pOut /* Result written here */ ); /* ** CAPI3REF: Prepared Statement Scan Status ** KEYWORDS: {scan status flags} */ #define SQLITE_SCANSTAT_COMPLEX 0x0001 /* ** CAPI3REF: Zero Scan-Status Counters ** METHOD: sqlite3_stmt ** ** ^Zero all [sqlite3_stmt_scanstatus()] related event counters. ** ** This API is only available if the library is built with pre-processor ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. */ SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); /* ** CAPI3REF: Flush caches to disk mid-transaction ** METHOD: sqlite3 ** ** ^If a write-transaction is open on [database connection] D when the ** [sqlite3_db_cacheflush(D)] interface is invoked, any dirty ** pages in the pager-cache that are not currently in use are written out ** to disk. A dirty page may be in use if a database cursor created by an ** active SQL statement is reading from it, or if it is page 1 of a database ** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)] ** interface flushes caches for all schemas - "main", "temp", and ** any [attached] databases. ** ** ^If this function needs to obtain extra database locks before dirty pages ** can be flushed to disk, it does so. ^If those locks cannot be obtained ** immediately and there is a busy-handler callback configured, it is invoked ** in the usual manner. ^If the required lock still cannot be obtained, then ** the database is skipped and an attempt made to flush any dirty pages ** belonging to the next (if any) database. ^If any databases are skipped ** because locks cannot be obtained, but no other error occurs, this ** function returns SQLITE_BUSY. ** ** ^If any other error occurs while flushing dirty pages to disk (for ** example an IO error or out-of-memory condition), then processing is ** abandoned and an SQLite [error code] is returned to the caller immediately. ** ** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK. ** ** ^This function does not set the database handle error code or message ** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions. */ SQLITE_API int sqlite3_db_cacheflush(sqlite3*); /* ** CAPI3REF: The pre-update hook. ** METHOD: sqlite3 ** ** ^These interfaces are only available if SQLite is compiled using the ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option. ** ** ^The [sqlite3_preupdate_hook()] interface registers a callback function ** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation ** on a database table. ** ^At most one preupdate hook may be registered at a time on a single ** [database connection]; each call to [sqlite3_preupdate_hook()] overrides ** the previous setting. ** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()] ** with a NULL pointer as the second parameter. ** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as ** the first parameter to callbacks. ** ** ^The preupdate hook only fires for changes to real database tables; the ** preupdate hook is not invoked for changes to [virtual tables] or to ** system tables like sqlite_sequence or sqlite_stat1. ** ** ^The second parameter to the preupdate callback is a pointer to ** the [database connection] that registered the preupdate hook. ** ^The third parameter to the preupdate callback is one of the constants ** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the ** kind of update operation that is about to occur. ** ^(The fourth parameter to the preupdate callback is the name of the ** database within the database connection that is being modified. This ** will be "main" for the main database or "temp" for TEMP tables or ** the name given after the AS keyword in the [ATTACH] statement for attached ** databases.)^ ** ^The fifth parameter to the preupdate callback is the name of the ** table that is being modified. ** ** For an UPDATE or DELETE operation on a [rowid table], the sixth ** parameter passed to the preupdate callback is the initial [rowid] of the ** row being modified or deleted. For an INSERT operation on a rowid table, ** or any operation on a WITHOUT ROWID table, the value of the sixth ** parameter is undefined. For an INSERT or UPDATE on a rowid table the ** seventh parameter is the final rowid value of the row being inserted ** or updated. The value of the seventh parameter passed to the callback ** function is not defined for operations on WITHOUT ROWID tables, or for ** DELETE operations on rowid tables. ** ** ^The sqlite3_preupdate_hook(D,C,P) function returns the P argument from ** the previous call on the same [database connection] D, or NULL for ** the first call on D. ** ** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()], ** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces ** provide additional information about a preupdate event. These routines ** may only be called from within a preupdate callback. Invoking any of ** these routines from outside of a preupdate callback or with a ** [database connection] pointer that is different from the one supplied ** to the preupdate callback results in undefined and probably undesirable ** behavior. ** ** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns ** in the row that is being inserted, updated, or deleted. ** ** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to ** a [protected sqlite3_value] that contains the value of the Nth column of ** the table row before it is updated. The N parameter must be between 0 ** and one less than the number of columns or the behavior will be ** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE ** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the ** behavior is undefined. The [sqlite3_value] that P points to ** will be destroyed when the preupdate callback returns. ** ** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to ** a [protected sqlite3_value] that contains the value of the Nth column of ** the table row after it is updated. The N parameter must be between 0 ** and one less than the number of columns or the behavior will be ** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE ** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the ** behavior is undefined. The [sqlite3_value] that P points to ** will be destroyed when the preupdate callback returns. ** ** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate ** callback was invoked as a result of a direct insert, update, or delete ** operation; or 1 for inserts, updates, or deletes invoked by top-level ** triggers; or 2 for changes resulting from triggers called by top-level ** triggers; and so forth. ** ** When the [sqlite3_blob_write()] API is used to update a blob column, ** the pre-update hook is invoked with SQLITE_DELETE, because ** the new values are not yet available. In this case, when a ** callback made with op==SQLITE_DELETE is actually a write using the ** sqlite3_blob_write() API, the [sqlite3_preupdate_blobwrite()] returns ** the index of the column being written. In other cases, where the ** pre-update hook is being invoked for some other reason, including a ** regular DELETE, sqlite3_preupdate_blobwrite() returns -1. ** ** See also: [sqlite3_update_hook()] */ #if defined(SQLITE_ENABLE_PREUPDATE_HOOK) SQLITE_API void *sqlite3_preupdate_hook( sqlite3 *db, void(*xPreUpdate)( void *pCtx, /* Copy of third arg to preupdate_hook() */ sqlite3 *db, /* Database handle */ int op, /* SQLITE_UPDATE, DELETE or INSERT */ char const *zDb, /* Database name */ char const *zName, /* Table name */ sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */ sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */ ), void* ); SQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **); SQLITE_API int sqlite3_preupdate_count(sqlite3 *); SQLITE_API int sqlite3_preupdate_depth(sqlite3 *); SQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **); SQLITE_API int sqlite3_preupdate_blobwrite(sqlite3 *); #endif /* ** CAPI3REF: Low-level system error code ** METHOD: sqlite3 ** ** ^Attempt to return the underlying operating system error code or error ** number that caused the most recent I/O error or failure to open a file. ** The return value is OS-dependent. For example, on unix systems, after ** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be ** called to get back the underlying "errno" that caused the problem, such ** as ENOSPC, EAUTH, EISDIR, and so forth. */ SQLITE_API int sqlite3_system_errno(sqlite3*); /* ** CAPI3REF: Database Snapshot ** KEYWORDS: {snapshot} {sqlite3_snapshot} ** ** An instance of the snapshot object records the state of a [WAL mode] ** database for some specific point in history. ** ** In [WAL mode], multiple [database connections] that are open on the ** same database file can each be reading a different historical version ** of the database file. When a [database connection] begins a read ** transaction, that connection sees an unchanging copy of the database ** as it existed for the point in time when the transaction first started. ** Subsequent changes to the database from other connections are not seen ** by the reader until a new read transaction is started. ** ** The sqlite3_snapshot object records state information about an historical ** version of the database file so that it is possible to later open a new read ** transaction that sees that historical version of the database rather than ** the most recent version. */ typedef struct sqlite3_snapshot { unsigned char hidden[48]; } sqlite3_snapshot; /* ** CAPI3REF: Record A Database Snapshot ** CONSTRUCTOR: sqlite3_snapshot ** ** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a ** new [sqlite3_snapshot] object that records the current state of ** schema S in database connection D. ^On success, the ** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly ** created [sqlite3_snapshot] object into *P and returns SQLITE_OK. ** If there is not already a read-transaction open on schema S when ** this function is called, one is opened automatically. ** ** If a read-transaction is opened by this function, then it is guaranteed ** that the returned snapshot object may not be invalidated by a database ** writer or checkpointer until after the read-transaction is closed. This ** is not guaranteed if a read-transaction is already open when this ** function is called. In that case, any subsequent write or checkpoint ** operation on the database may invalidate the returned snapshot handle, ** even while the read-transaction remains open. ** ** The following must be true for this function to succeed. If any of ** the following statements are false when sqlite3_snapshot_get() is ** called, SQLITE_ERROR is returned. The final value of *P is undefined ** in this case. ** **
    **
  • The database handle must not be in [autocommit mode]. ** **
  • Schema S of [database connection] D must be a [WAL mode] database. ** **
  • There must not be a write transaction open on schema S of database ** connection D. ** **
  • One or more transactions must have been written to the current wal ** file since it was created on disk (by any connection). This means ** that a snapshot cannot be taken on a wal mode database with no wal ** file immediately after it is first opened. At least one transaction ** must be written to it first. **
** ** This function may also return SQLITE_NOMEM. If it is called with the ** database handle in autocommit mode but fails for some other reason, ** whether or not a read transaction is opened on schema S is undefined. ** ** The [sqlite3_snapshot] object returned from a successful call to ** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()] ** to avoid a memory leak. ** ** The [sqlite3_snapshot_get()] interface is only available when the ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used. */ SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get( sqlite3 *db, const char *zSchema, sqlite3_snapshot **ppSnapshot ); /* ** CAPI3REF: Start a read transaction on an historical snapshot ** METHOD: sqlite3_snapshot ** ** ^The [sqlite3_snapshot_open(D,S,P)] interface either starts a new read ** transaction or upgrades an existing one for schema S of ** [database connection] D such that the read transaction refers to ** historical [snapshot] P, rather than the most recent change to the ** database. ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK ** on success or an appropriate [error code] if it fails. ** ** ^In order to succeed, the database connection must not be in ** [autocommit mode] when [sqlite3_snapshot_open(D,S,P)] is called. If there ** is already a read transaction open on schema S, then the database handle ** must have no active statements (SELECT statements that have been passed ** to sqlite3_step() but not sqlite3_reset() or sqlite3_finalize()). ** SQLITE_ERROR is returned if either of these conditions is violated, or ** if schema S does not exist, or if the snapshot object is invalid. ** ** ^A call to sqlite3_snapshot_open() will fail to open if the specified ** snapshot has been overwritten by a [checkpoint]. In this case ** SQLITE_ERROR_SNAPSHOT is returned. ** ** If there is already a read transaction open when this function is ** invoked, then the same read transaction remains open (on the same ** database snapshot) if SQLITE_ERROR, SQLITE_BUSY or SQLITE_ERROR_SNAPSHOT ** is returned. If another error code - for example SQLITE_PROTOCOL or an ** SQLITE_IOERR error code - is returned, then the final state of the ** read transaction is undefined. If SQLITE_OK is returned, then the ** read transaction is now open on database snapshot P. ** ** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the ** database connection D does not know that the database file for ** schema S is in [WAL mode]. A database connection might not know ** that the database file is in [WAL mode] if there has been no prior ** I/O on that database connection, or if the database entered [WAL mode] ** after the most recent I/O on the database connection.)^ ** (Hint: Run "[PRAGMA application_id]" against a newly opened ** database connection in order to make it ready to use snapshots.) ** ** The [sqlite3_snapshot_open()] interface is only available when the ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used. */ SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open( sqlite3 *db, const char *zSchema, sqlite3_snapshot *pSnapshot ); /* ** CAPI3REF: Destroy a snapshot ** DESTRUCTOR: sqlite3_snapshot ** ** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P. ** The application must eventually free every [sqlite3_snapshot] object ** using this routine to avoid a memory leak. ** ** The [sqlite3_snapshot_free()] interface is only available when the ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used. */ SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*); /* ** CAPI3REF: Compare the ages of two snapshot handles. ** METHOD: sqlite3_snapshot ** ** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages ** of two valid snapshot handles. ** ** If the two snapshot handles are not associated with the same database ** file, the result of the comparison is undefined. ** ** Additionally, the result of the comparison is only valid if both of the ** snapshot handles were obtained by calling sqlite3_snapshot_get() since the ** last time the wal file was deleted. The wal file is deleted when the ** database is changed back to rollback mode or when the number of database ** clients drops to zero. If either snapshot handle was obtained before the ** wal file was last deleted, the value returned by this function ** is undefined. ** ** Otherwise, this API returns a negative value if P1 refers to an older ** snapshot than P2, zero if the two handles refer to the same database ** snapshot, and a positive value if P1 is a newer snapshot than P2. ** ** This interface is only available if SQLite is compiled with the ** [SQLITE_ENABLE_SNAPSHOT] option. */ SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp( sqlite3_snapshot *p1, sqlite3_snapshot *p2 ); /* ** CAPI3REF: Recover snapshots from a wal file ** METHOD: sqlite3_snapshot ** ** If a [WAL file] remains on disk after all database connections close ** (either through the use of the [SQLITE_FCNTL_PERSIST_WAL] [file control] ** or because the last process to have the database opened exited without ** calling [sqlite3_close()]) and a new connection is subsequently opened ** on that database and [WAL file], the [sqlite3_snapshot_open()] interface ** will only be able to open the last transaction added to the WAL file ** even though the WAL file contains other valid transactions. ** ** This function attempts to scan the WAL file associated with database zDb ** of database handle db and make all valid snapshots available to ** sqlite3_snapshot_open(). It is an error if there is already a read ** transaction open on the database, or if the database is not a WAL mode ** database. ** ** SQLITE_OK is returned if successful, or an SQLite error code otherwise. ** ** This interface is only available if SQLite is compiled with the ** [SQLITE_ENABLE_SNAPSHOT] option. */ SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb); /* ** CAPI3REF: Serialize a database ** ** The sqlite3_serialize(D,S,P,F) interface returns a pointer to ** memory that is a serialization of the S database on ** [database connection] D. If S is a NULL pointer, the main database is used. ** If P is not a NULL pointer, then the size of the database in bytes ** is written into *P. ** ** For an ordinary on-disk database file, the serialization is just a ** copy of the disk file. For an in-memory database or a "TEMP" database, ** the serialization is the same sequence of bytes which would be written ** to disk if that database were backed up to disk. ** ** The usual case is that sqlite3_serialize() copies the serialization of ** the database into memory obtained from [sqlite3_malloc64()] and returns ** a pointer to that memory. The caller is responsible for freeing the ** returned value to avoid a memory leak. However, if the F argument ** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations ** are made, and the sqlite3_serialize() function will return a pointer ** to the contiguous memory representation of the database that SQLite ** is currently using for that database, or NULL if no such contiguous ** memory representation of the database exists. A contiguous memory ** representation of the database will usually only exist if there has ** been a prior call to [sqlite3_deserialize(D,S,...)] with the same ** values of D and S. ** The size of the database is written into *P even if the ** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy ** of the database exists. ** ** After the call, if the SQLITE_SERIALIZE_NOCOPY bit had been set, ** the returned buffer content will remain accessible and unchanged ** until either the next write operation on the connection or when ** the connection is closed, and applications must not modify the ** buffer. If the bit had been clear, the returned buffer will not ** be accessed by SQLite after the call. ** ** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the ** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory ** allocation error occurs. ** ** This interface is omitted if SQLite is compiled with the ** [SQLITE_OMIT_DESERIALIZE] option. */ SQLITE_API unsigned char *sqlite3_serialize( sqlite3 *db, /* The database connection */ const char *zSchema, /* Which DB to serialize. ex: "main", "temp", ... */ sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */ unsigned int mFlags /* Zero or more SQLITE_SERIALIZE_* flags */ ); /* ** CAPI3REF: Flags for sqlite3_serialize ** ** Zero or more of the following constants can be OR-ed together for ** the F argument to [sqlite3_serialize(D,S,P,F)]. ** ** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return ** a pointer to contiguous in-memory database that it is currently using, ** without making a copy of the database. If SQLite is not currently using ** a contiguous in-memory database, then this option causes ** [sqlite3_serialize()] to return a NULL pointer. SQLite will only be ** using a contiguous in-memory database if it has been initialized by a ** prior call to [sqlite3_deserialize()]. */ #define SQLITE_SERIALIZE_NOCOPY 0x001 /* Do no memory allocations */ /* ** CAPI3REF: Deserialize a database ** ** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the ** [database connection] D to disconnect from database S and then ** reopen S as an in-memory database based on the serialization contained ** in P. The serialized database P is N bytes in size. M is the size of ** the buffer P, which might be larger than N. If M is larger than N, and ** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is ** permitted to add content to the in-memory database as long as the total ** size does not exceed M bytes. ** ** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will ** invoke sqlite3_free() on the serialization buffer when the database ** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then ** SQLite will try to increase the buffer size using sqlite3_realloc64() ** if writes on the database cause it to grow larger than M bytes. ** ** Applications must not modify the buffer P or invalidate it before ** the database connection D is closed. ** ** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the ** database is currently in a read transaction or is involved in a backup ** operation. ** ** It is not possible to deserialize into the TEMP database. If the ** S argument to sqlite3_deserialize(D,S,P,N,M,F) is "temp" then the ** function returns SQLITE_ERROR. ** ** The deserialized database should not be in [WAL mode]. If the database ** is in WAL mode, then any attempt to use the database file will result ** in an [SQLITE_CANTOPEN] error. The application can set the ** [file format version numbers] (bytes 18 and 19) of the input database P ** to 0x01 prior to invoking sqlite3_deserialize(D,S,P,N,M,F) to force the ** database file into rollback mode and work around this limitation. ** ** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the ** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then ** [sqlite3_free()] is invoked on argument P prior to returning. ** ** This interface is omitted if SQLite is compiled with the ** [SQLITE_OMIT_DESERIALIZE] option. */ SQLITE_API int sqlite3_deserialize( sqlite3 *db, /* The database connection */ const char *zSchema, /* Which DB to reopen with the deserialization */ unsigned char *pData, /* The serialized database content */ sqlite3_int64 szDb, /* Number of bytes in the deserialization */ sqlite3_int64 szBuf, /* Total size of buffer pData[] */ unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */ ); /* ** CAPI3REF: Flags for sqlite3_deserialize() ** ** The following are allowed values for the 6th argument (the F argument) to ** the [sqlite3_deserialize(D,S,P,N,M,F)] interface. ** ** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization ** in the P argument is held in memory obtained from [sqlite3_malloc64()] ** and that SQLite should take ownership of this memory and automatically ** free it when it has finished using it. Without this flag, the caller ** is responsible for freeing any dynamically allocated memory. ** ** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to ** grow the size of the database using calls to [sqlite3_realloc64()]. This ** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used. ** Without this flag, the deserialized database cannot increase in size beyond ** the number of bytes specified by the M parameter. ** ** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database ** should be treated as read-only. */ #define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */ #define SQLITE_DESERIALIZE_RESIZEABLE 2 /* Resize using sqlite3_realloc64() */ #define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */ /* ** Undo the hack that converts floating point types to integer for ** builds on processors without floating point support. */ #ifdef SQLITE_OMIT_FLOATING_POINT # undef double #endif #if defined(__wasi__) # undef SQLITE_WASI # define SQLITE_WASI 1 # ifndef SQLITE_OMIT_LOAD_EXTENSION # define SQLITE_OMIT_LOAD_EXTENSION # endif # ifndef SQLITE_THREADSAFE # define SQLITE_THREADSAFE 0 # endif #endif #ifdef __cplusplus } /* End of the 'extern "C"' block */ #endif /* #endif for SQLITE3_H will be added by mksqlite3.tcl */ /******** Begin file sqlite3rtree.h *********/ /* ** 2010 August 30 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* */ #ifndef _SQLITE3RTREE_H_ #define _SQLITE3RTREE_H_ #ifdef __cplusplus extern "C" { #endif typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry; typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info; /* The double-precision datatype used by RTree depends on the ** SQLITE_RTREE_INT_ONLY compile-time option. */ #ifdef SQLITE_RTREE_INT_ONLY typedef sqlite3_int64 sqlite3_rtree_dbl; #else typedef double sqlite3_rtree_dbl; #endif /* ** Register a geometry callback named zGeom that can be used as part of an ** R-Tree geometry query as follows: ** ** SELECT ... FROM WHERE MATCH $zGeom(... params ...) */ SQLITE_API int sqlite3_rtree_geometry_callback( sqlite3 *db, const char *zGeom, int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*), void *pContext ); /* ** A pointer to a structure of the following type is passed as the first ** argument to callbacks registered using rtree_geometry_callback(). */ struct sqlite3_rtree_geometry { void *pContext; /* Copy of pContext passed to s_r_g_c() */ int nParam; /* Size of array aParam[] */ sqlite3_rtree_dbl *aParam; /* Parameters passed to SQL geom function */ void *pUser; /* Callback implementation user data */ void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */ }; /* ** Register a 2nd-generation geometry callback named zScore that can be ** used as part of an R-Tree geometry query as follows: ** ** SELECT ... FROM WHERE MATCH $zQueryFunc(... params ...) */ SQLITE_API int sqlite3_rtree_query_callback( sqlite3 *db, const char *zQueryFunc, int (*xQueryFunc)(sqlite3_rtree_query_info*), void *pContext, void (*xDestructor)(void*) ); /* ** A pointer to a structure of the following type is passed as the ** argument to scored geometry callback registered using ** sqlite3_rtree_query_callback(). ** ** Note that the first 5 fields of this structure are identical to ** sqlite3_rtree_geometry. This structure is a subclass of ** sqlite3_rtree_geometry. */ struct sqlite3_rtree_query_info { void *pContext; /* pContext from when function registered */ int nParam; /* Number of function parameters */ sqlite3_rtree_dbl *aParam; /* value of function parameters */ void *pUser; /* callback can use this, if desired */ void (*xDelUser)(void*); /* function to free pUser */ sqlite3_rtree_dbl *aCoord; /* Coordinates of node or entry to check */ unsigned int *anQueue; /* Number of pending entries in the queue */ int nCoord; /* Number of coordinates */ int iLevel; /* Level of current node or entry */ int mxLevel; /* The largest iLevel value in the tree */ sqlite3_int64 iRowid; /* Rowid for current entry */ sqlite3_rtree_dbl rParentScore; /* Score of parent node */ int eParentWithin; /* Visibility of parent node */ int eWithin; /* OUT: Visibility */ sqlite3_rtree_dbl rScore; /* OUT: Write the score here */ /* The following fields are only available in 3.8.11 and later */ sqlite3_value **apSqlParam; /* Original SQL values of parameters */ }; /* ** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin. */ #define NOT_WITHIN 0 /* Object completely outside of query region */ #define PARTLY_WITHIN 1 /* Object partially overlaps query region */ #define FULLY_WITHIN 2 /* Object fully contained within query region */ #ifdef __cplusplus } /* end of the 'extern "C"' block */ #endif #endif /* ifndef _SQLITE3RTREE_H_ */ /******** End of sqlite3rtree.h *********/ /******** Begin file sqlite3session.h *********/ #if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) #define __SQLITESESSION_H_ 1 /* ** Make sure we can call this stuff from C++. */ #ifdef __cplusplus extern "C" { #endif /* ** CAPI3REF: Session Object Handle ** ** An instance of this object is a [session] that can be used to ** record changes to a database. */ typedef struct sqlite3_session sqlite3_session; /* ** CAPI3REF: Changeset Iterator Handle ** ** An instance of this object acts as a cursor for iterating ** over the elements of a [changeset] or [patchset]. */ typedef struct sqlite3_changeset_iter sqlite3_changeset_iter; /* ** CAPI3REF: Create A New Session Object ** CONSTRUCTOR: sqlite3_session ** ** Create a new session object attached to database handle db. If successful, ** a pointer to the new object is written to *ppSession and SQLITE_OK is ** returned. If an error occurs, *ppSession is set to NULL and an SQLite ** error code (e.g. SQLITE_NOMEM) is returned. ** ** It is possible to create multiple session objects attached to a single ** database handle. ** ** Session objects created using this function should be deleted using the ** [sqlite3session_delete()] function before the database handle that they ** are attached to is itself closed. If the database handle is closed before ** the session object is deleted, then the results of calling any session ** module function, including [sqlite3session_delete()] on the session object ** are undefined. ** ** Because the session module uses the [sqlite3_preupdate_hook()] API, it ** is not possible for an application to register a pre-update hook on a ** database handle that has one or more session objects attached. Nor is ** it possible to create a session object attached to a database handle for ** which a pre-update hook is already defined. The results of attempting ** either of these things are undefined. ** ** The session object will be used to create changesets for tables in ** database zDb, where zDb is either "main", or "temp", or the name of an ** attached database. It is not an error if database zDb is not attached ** to the database when the session object is created. */ SQLITE_API int sqlite3session_create( sqlite3 *db, /* Database handle */ const char *zDb, /* Name of db (e.g. "main") */ sqlite3_session **ppSession /* OUT: New session object */ ); /* ** CAPI3REF: Delete A Session Object ** DESTRUCTOR: sqlite3_session ** ** Delete a session object previously allocated using ** [sqlite3session_create()]. Once a session object has been deleted, the ** results of attempting to use pSession with any other session module ** function are undefined. ** ** Session objects must be deleted before the database handle to which they ** are attached is closed. Refer to the documentation for ** [sqlite3session_create()] for details. */ SQLITE_API void sqlite3session_delete(sqlite3_session *pSession); /* ** CAPI3REF: Configure a Session Object ** METHOD: sqlite3_session ** ** This method is used to configure a session object after it has been ** created. At present the only valid values for the second parameter are ** [SQLITE_SESSION_OBJCONFIG_SIZE] and [SQLITE_SESSION_OBJCONFIG_ROWID]. ** */ SQLITE_API int sqlite3session_object_config(sqlite3_session*, int op, void *pArg); /* ** CAPI3REF: Options for sqlite3session_object_config ** ** The following values may passed as the the 2nd parameter to ** sqlite3session_object_config(). ** **
SQLITE_SESSION_OBJCONFIG_SIZE
** This option is used to set, clear or query the flag that enables ** the [sqlite3session_changeset_size()] API. Because it imposes some ** computational overhead, this API is disabled by default. Argument ** pArg must point to a value of type (int). If the value is initially ** 0, then the sqlite3session_changeset_size() API is disabled. If it ** is greater than 0, then the same API is enabled. Or, if the initial ** value is less than zero, no change is made. In all cases the (int) ** variable is set to 1 if the sqlite3session_changeset_size() API is ** enabled following the current call, or 0 otherwise. ** ** It is an error (SQLITE_MISUSE) to attempt to modify this setting after ** the first table has been attached to the session object. ** **
SQLITE_SESSION_OBJCONFIG_ROWID
** This option is used to set, clear or query the flag that enables ** collection of data for tables with no explicit PRIMARY KEY. ** ** Normally, tables with no explicit PRIMARY KEY are simply ignored ** by the sessions module. However, if this flag is set, it behaves ** as if such tables have a column "_rowid_ INTEGER PRIMARY KEY" inserted ** as their leftmost columns. ** ** It is an error (SQLITE_MISUSE) to attempt to modify this setting after ** the first table has been attached to the session object. */ #define SQLITE_SESSION_OBJCONFIG_SIZE 1 #define SQLITE_SESSION_OBJCONFIG_ROWID 2 /* ** CAPI3REF: Enable Or Disable A Session Object ** METHOD: sqlite3_session ** ** Enable or disable the recording of changes by a session object. When ** enabled, a session object records changes made to the database. When ** disabled - it does not. A newly created session object is enabled. ** Refer to the documentation for [sqlite3session_changeset()] for further ** details regarding how enabling and disabling a session object affects ** the eventual changesets. ** ** Passing zero to this function disables the session. Passing a value ** greater than zero enables it. Passing a value less than zero is a ** no-op, and may be used to query the current state of the session. ** ** The return value indicates the final state of the session object: 0 if ** the session is disabled, or 1 if it is enabled. */ SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable); /* ** CAPI3REF: Set Or Clear the Indirect Change Flag ** METHOD: sqlite3_session ** ** Each change recorded by a session object is marked as either direct or ** indirect. A change is marked as indirect if either: ** **
    **
  • The session object "indirect" flag is set when the change is ** made, or **
  • The change is made by an SQL trigger or foreign key action ** instead of directly as a result of a users SQL statement. **
** ** If a single row is affected by more than one operation within a session, ** then the change is considered indirect if all operations meet the criteria ** for an indirect change above, or direct otherwise. ** ** This function is used to set, clear or query the session object indirect ** flag. If the second argument passed to this function is zero, then the ** indirect flag is cleared. If it is greater than zero, the indirect flag ** is set. Passing a value less than zero does not modify the current value ** of the indirect flag, and may be used to query the current state of the ** indirect flag for the specified session object. ** ** The return value indicates the final state of the indirect flag: 0 if ** it is clear, or 1 if it is set. */ SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect); /* ** CAPI3REF: Attach A Table To A Session Object ** METHOD: sqlite3_session ** ** If argument zTab is not NULL, then it is the name of a table to attach ** to the session object passed as the first argument. All subsequent changes ** made to the table while the session object is enabled will be recorded. See ** documentation for [sqlite3session_changeset()] for further details. ** ** Or, if argument zTab is NULL, then changes are recorded for all tables ** in the database. If additional tables are added to the database (by ** executing "CREATE TABLE" statements) after this call is made, changes for ** the new tables are also recorded. ** ** Changes can only be recorded for tables that have a PRIMARY KEY explicitly ** defined as part of their CREATE TABLE statement. It does not matter if the ** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY ** KEY may consist of a single column, or may be a composite key. ** ** It is not an error if the named table does not exist in the database. Nor ** is it an error if the named table does not have a PRIMARY KEY. However, ** no changes will be recorded in either of these scenarios. ** ** Changes are not recorded for individual rows that have NULL values stored ** in one or more of their PRIMARY KEY columns. ** ** SQLITE_OK is returned if the call completes without error. Or, if an error ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned. ** **

Special sqlite_stat1 Handling

** ** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to ** some of the rules above. In SQLite, the schema of sqlite_stat1 is: **
**        CREATE TABLE sqlite_stat1(tbl,idx,stat)
**  
** ** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are ** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes ** are recorded for rows for which (idx IS NULL) is true. However, for such ** rows a zero-length blob (SQL value X'') is stored in the changeset or ** patchset instead of a NULL value. This allows such changesets to be ** manipulated by legacy implementations of sqlite3changeset_invert(), ** concat() and similar. ** ** The sqlite3changeset_apply() function automatically converts the ** zero-length blob back to a NULL value when updating the sqlite_stat1 ** table. However, if the application calls sqlite3changeset_new(), ** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset ** iterator directly (including on a changeset iterator passed to a ** conflict-handler callback) then the X'' value is returned. The application ** must translate X'' to NULL itself if required. ** ** Legacy (older than 3.22.0) versions of the sessions module cannot capture ** changes made to the sqlite_stat1 table. Legacy versions of the ** sqlite3changeset_apply() function silently ignore any modifications to the ** sqlite_stat1 table that are part of a changeset or patchset. */ SQLITE_API int sqlite3session_attach( sqlite3_session *pSession, /* Session object */ const char *zTab /* Table name */ ); /* ** CAPI3REF: Set a table filter on a Session Object. ** METHOD: sqlite3_session ** ** The second argument (xFilter) is the "filter callback". For changes to rows ** in tables that are not attached to the Session object, the filter is called ** to determine whether changes to the table's rows should be tracked or not. ** If xFilter returns 0, changes are not tracked. Note that once a table is ** attached, xFilter will not be called again. */ SQLITE_API void sqlite3session_table_filter( sqlite3_session *pSession, /* Session object */ int(*xFilter)( void *pCtx, /* Copy of third arg to _filter_table() */ const char *zTab /* Table name */ ), void *pCtx /* First argument passed to xFilter */ ); /* ** CAPI3REF: Generate A Changeset From A Session Object ** METHOD: sqlite3_session ** ** Obtain a changeset containing changes to the tables attached to the ** session object passed as the first argument. If successful, ** set *ppChangeset to point to a buffer containing the changeset ** and *pnChangeset to the size of the changeset in bytes before returning ** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to ** zero and return an SQLite error code. ** ** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes, ** each representing a change to a single row of an attached table. An INSERT ** change contains the values of each field of a new database row. A DELETE ** contains the original values of each field of a deleted database row. An ** UPDATE change contains the original values of each field of an updated ** database row along with the updated values for each updated non-primary-key ** column. It is not possible for an UPDATE change to represent a change that ** modifies the values of primary key columns. If such a change is made, it ** is represented in a changeset as a DELETE followed by an INSERT. ** ** Changes are not recorded for rows that have NULL values stored in one or ** more of their PRIMARY KEY columns. If such a row is inserted or deleted, ** no corresponding change is present in the changesets returned by this ** function. If an existing row with one or more NULL values stored in ** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL, ** only an INSERT is appears in the changeset. Similarly, if an existing row ** with non-NULL PRIMARY KEY values is updated so that one or more of its ** PRIMARY KEY columns are set to NULL, the resulting changeset contains a ** DELETE change only. ** ** The contents of a changeset may be traversed using an iterator created ** using the [sqlite3changeset_start()] API. A changeset may be applied to ** a database with a compatible schema using the [sqlite3changeset_apply()] ** API. ** ** Within a changeset generated by this function, all changes related to a ** single table are grouped together. In other words, when iterating through ** a changeset or when applying a changeset to a database, all changes related ** to a single table are processed before moving on to the next table. Tables ** are sorted in the same order in which they were attached (or auto-attached) ** to the sqlite3_session object. The order in which the changes related to ** a single table are stored is undefined. ** ** Following a successful call to this function, it is the responsibility of ** the caller to eventually free the buffer that *ppChangeset points to using ** [sqlite3_free()]. ** **

Changeset Generation

** ** Once a table has been attached to a session object, the session object ** records the primary key values of all new rows inserted into the table. ** It also records the original primary key and other column values of any ** deleted or updated rows. For each unique primary key value, data is only ** recorded once - the first time a row with said primary key is inserted, ** updated or deleted in the lifetime of the session. ** ** There is one exception to the previous paragraph: when a row is inserted, ** updated or deleted, if one or more of its primary key columns contain a ** NULL value, no record of the change is made. ** ** The session object therefore accumulates two types of records - those ** that consist of primary key values only (created when the user inserts ** a new record) and those that consist of the primary key values and the ** original values of other table columns (created when the users deletes ** or updates a record). ** ** When this function is called, the requested changeset is created using ** both the accumulated records and the current contents of the database ** file. Specifically: ** **
    **
  • For each record generated by an insert, the database is queried ** for a row with a matching primary key. If one is found, an INSERT ** change is added to the changeset. If no such row is found, no change ** is added to the changeset. ** **
  • For each record generated by an update or delete, the database is ** queried for a row with a matching primary key. If such a row is ** found and one or more of the non-primary key fields have been ** modified from their original values, an UPDATE change is added to ** the changeset. Or, if no such row is found in the table, a DELETE ** change is added to the changeset. If there is a row with a matching ** primary key in the database, but all fields contain their original ** values, no change is added to the changeset. **
** ** This means, amongst other things, that if a row is inserted and then later ** deleted while a session object is active, neither the insert nor the delete ** will be present in the changeset. Or if a row is deleted and then later a ** row with the same primary key values inserted while a session object is ** active, the resulting changeset will contain an UPDATE change instead of ** a DELETE and an INSERT. ** ** When a session object is disabled (see the [sqlite3session_enable()] API), ** it does not accumulate records when rows are inserted, updated or deleted. ** This may appear to have some counter-intuitive effects if a single row ** is written to more than once during a session. For example, if a row ** is inserted while a session object is enabled, then later deleted while ** the same session object is disabled, no INSERT record will appear in the ** changeset, even though the delete took place while the session was disabled. ** Or, if one field of a row is updated while a session is enabled, and ** then another field of the same row is updated while the session is disabled, ** the resulting changeset will contain an UPDATE change that updates both ** fields. */ SQLITE_API int sqlite3session_changeset( sqlite3_session *pSession, /* Session object */ int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */ void **ppChangeset /* OUT: Buffer containing changeset */ ); /* ** CAPI3REF: Return An Upper-limit For The Size Of The Changeset ** METHOD: sqlite3_session ** ** By default, this function always returns 0. For it to return ** a useful result, the sqlite3_session object must have been configured ** to enable this API using sqlite3session_object_config() with the ** SQLITE_SESSION_OBJCONFIG_SIZE verb. ** ** When enabled, this function returns an upper limit, in bytes, for the size ** of the changeset that might be produced if sqlite3session_changeset() were ** called. The final changeset size might be equal to or smaller than the ** size in bytes returned by this function. */ SQLITE_API sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession); /* ** CAPI3REF: Load The Difference Between Tables Into A Session ** METHOD: sqlite3_session ** ** If it is not already attached to the session object passed as the first ** argument, this function attaches table zTbl in the same manner as the ** [sqlite3session_attach()] function. If zTbl does not exist, or if it ** does not have a primary key, this function is a no-op (but does not return ** an error). ** ** Argument zFromDb must be the name of a database ("main", "temp" etc.) ** attached to the same database handle as the session object that contains ** a table compatible with the table attached to the session by this function. ** A table is considered compatible if it: ** **
    **
  • Has the same name, **
  • Has the same set of columns declared in the same order, and **
  • Has the same PRIMARY KEY definition. **
** ** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables ** are compatible but do not have any PRIMARY KEY columns, it is not an error ** but no changes are added to the session object. As with other session ** APIs, tables without PRIMARY KEYs are simply ignored. ** ** This function adds a set of changes to the session object that could be ** used to update the table in database zFrom (call this the "from-table") ** so that its content is the same as the table attached to the session ** object (call this the "to-table"). Specifically: ** **
    **
  • For each row (primary key) that exists in the to-table but not in ** the from-table, an INSERT record is added to the session object. ** **
  • For each row (primary key) that exists in the to-table but not in ** the from-table, a DELETE record is added to the session object. ** **
  • For each row (primary key) that exists in both tables, but features ** different non-PK values in each, an UPDATE record is added to the ** session. **
** ** To clarify, if this function is called and then a changeset constructed ** using [sqlite3session_changeset()], then after applying that changeset to ** database zFrom the contents of the two compatible tables would be ** identical. ** ** Unless the call to this function is a no-op as described above, it is an ** error if database zFrom does not exist or does not contain the required ** compatible table. ** ** If the operation is successful, SQLITE_OK is returned. Otherwise, an SQLite ** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg ** may be set to point to a buffer containing an English language error ** message. It is the responsibility of the caller to free this buffer using ** sqlite3_free(). */ SQLITE_API int sqlite3session_diff( sqlite3_session *pSession, const char *zFromDb, const char *zTbl, char **pzErrMsg ); /* ** CAPI3REF: Generate A Patchset From A Session Object ** METHOD: sqlite3_session ** ** The differences between a patchset and a changeset are that: ** **
    **
  • DELETE records consist of the primary key fields only. The ** original values of other fields are omitted. **
  • The original values of any modified fields are omitted from ** UPDATE records. **
** ** A patchset blob may be used with up to date versions of all ** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(), ** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly, ** attempting to use a patchset blob with old versions of the ** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error. ** ** Because the non-primary key "old.*" fields are omitted, no ** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset ** is passed to the sqlite3changeset_apply() API. Other conflict types work ** in the same way as for changesets. ** ** Changes within a patchset are ordered in the same way as for changesets ** generated by the sqlite3session_changeset() function (i.e. all changes for ** a single table are grouped together, tables appear in the order in which ** they were attached to the session object). */ SQLITE_API int sqlite3session_patchset( sqlite3_session *pSession, /* Session object */ int *pnPatchset, /* OUT: Size of buffer at *ppPatchset */ void **ppPatchset /* OUT: Buffer containing patchset */ ); /* ** CAPI3REF: Test if a changeset has recorded any changes. ** ** Return non-zero if no changes to attached tables have been recorded by ** the session object passed as the first argument. Otherwise, if one or ** more changes have been recorded, return zero. ** ** Even if this function returns zero, it is possible that calling ** [sqlite3session_changeset()] on the session handle may still return a ** changeset that contains no changes. This can happen when a row in ** an attached table is modified and then later on the original values ** are restored. However, if this function returns non-zero, then it is ** guaranteed that a call to sqlite3session_changeset() will return a ** changeset containing zero changes. */ SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession); /* ** CAPI3REF: Query for the amount of heap memory used by a session object. ** ** This API returns the total amount of heap memory in bytes currently ** used by the session object passed as the only argument. */ SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession); /* ** CAPI3REF: Create An Iterator To Traverse A Changeset ** CONSTRUCTOR: sqlite3_changeset_iter ** ** Create an iterator used to iterate through the contents of a changeset. ** If successful, *pp is set to point to the iterator handle and SQLITE_OK ** is returned. Otherwise, if an error occurs, *pp is set to zero and an ** SQLite error code is returned. ** ** The following functions can be used to advance and query a changeset ** iterator created by this function: ** **
    **
  • [sqlite3changeset_next()] **
  • [sqlite3changeset_op()] **
  • [sqlite3changeset_new()] **
  • [sqlite3changeset_old()] **
** ** It is the responsibility of the caller to eventually destroy the iterator ** by passing it to [sqlite3changeset_finalize()]. The buffer containing the ** changeset (pChangeset) must remain valid until after the iterator is ** destroyed. ** ** Assuming the changeset blob was created by one of the ** [sqlite3session_changeset()], [sqlite3changeset_concat()] or ** [sqlite3changeset_invert()] functions, all changes within the changeset ** that apply to a single table are grouped together. This means that when ** an application iterates through a changeset using an iterator created by ** this function, all changes that relate to a single table are visited ** consecutively. There is no chance that the iterator will visit a change ** the applies to table X, then one for table Y, and then later on visit ** another change for table X. ** ** The behavior of sqlite3changeset_start_v2() and its streaming equivalent ** may be modified by passing a combination of ** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter. ** ** Note that the sqlite3changeset_start_v2() API is still experimental ** and therefore subject to change. */ SQLITE_API int sqlite3changeset_start( sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */ int nChangeset, /* Size of changeset blob in bytes */ void *pChangeset /* Pointer to blob containing changeset */ ); SQLITE_API int sqlite3changeset_start_v2( sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */ int nChangeset, /* Size of changeset blob in bytes */ void *pChangeset, /* Pointer to blob containing changeset */ int flags /* SESSION_CHANGESETSTART_* flags */ ); /* ** CAPI3REF: Flags for sqlite3changeset_start_v2 ** ** The following flags may passed via the 4th parameter to ** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]: ** **
SQLITE_CHANGESETSTART_INVERT
** Invert the changeset while iterating through it. This is equivalent to ** inverting a changeset using sqlite3changeset_invert() before applying it. ** It is an error to specify this flag with a patchset. */ #define SQLITE_CHANGESETSTART_INVERT 0x0002 /* ** CAPI3REF: Advance A Changeset Iterator ** METHOD: sqlite3_changeset_iter ** ** This function may only be used with iterators created by the function ** [sqlite3changeset_start()]. If it is called on an iterator passed to ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE ** is returned and the call has no effect. ** ** Immediately after an iterator is created by sqlite3changeset_start(), it ** does not point to any change in the changeset. Assuming the changeset ** is not empty, the first call to this function advances the iterator to ** point to the first change in the changeset. Each subsequent call advances ** the iterator to point to the next change in the changeset (if any). If ** no error occurs and the iterator points to a valid change after a call ** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned. ** Otherwise, if all changes in the changeset have already been visited, ** SQLITE_DONE is returned. ** ** If an error occurs, an SQLite error code is returned. Possible error ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or ** SQLITE_NOMEM. */ SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter); /* ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator ** METHOD: sqlite3_changeset_iter ** ** The pIter argument passed to this function may either be an iterator ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator ** created by [sqlite3changeset_start()]. In the latter case, the most recent ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this ** is not the case, this function returns [SQLITE_MISUSE]. ** ** Arguments pOp, pnCol and pzTab may not be NULL. Upon return, three ** outputs are set through these pointers: ** ** *pOp is set to one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], ** depending on the type of change that the iterator currently points to; ** ** *pnCol is set to the number of columns in the table affected by the change; and ** ** *pzTab is set to point to a nul-terminated utf-8 encoded string containing ** the name of the table affected by the current change. The buffer remains ** valid until either sqlite3changeset_next() is called on the iterator ** or until the conflict-handler function returns. ** ** If pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change ** is an indirect change, or false (0) otherwise. See the documentation for ** [sqlite3session_indirect()] for a description of direct and indirect ** changes. ** ** If no error occurs, SQLITE_OK is returned. If an error does occur, an ** SQLite error code is returned. The values of the output variables may not ** be trusted in this case. */ SQLITE_API int sqlite3changeset_op( sqlite3_changeset_iter *pIter, /* Iterator object */ const char **pzTab, /* OUT: Pointer to table name */ int *pnCol, /* OUT: Number of columns in table */ int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */ int *pbIndirect /* OUT: True for an 'indirect' change */ ); /* ** CAPI3REF: Obtain The Primary Key Definition Of A Table ** METHOD: sqlite3_changeset_iter ** ** For each modified table, a changeset includes the following: ** **
    **
  • The number of columns in the table, and **
  • Which of those columns make up the tables PRIMARY KEY. **
** ** This function is used to find which columns comprise the PRIMARY KEY of ** the table modified by the change that iterator pIter currently points to. ** If successful, *pabPK is set to point to an array of nCol entries, where ** nCol is the number of columns in the table. Elements of *pabPK are set to ** 0x01 if the corresponding column is part of the tables primary key, or ** 0x00 if it is not. ** ** If argument pnCol is not NULL, then *pnCol is set to the number of columns ** in the table. ** ** If this function is called when the iterator does not point to a valid ** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise, ** SQLITE_OK is returned and the output variables populated as described ** above. */ SQLITE_API int sqlite3changeset_pk( sqlite3_changeset_iter *pIter, /* Iterator object */ unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */ int *pnCol /* OUT: Number of entries in output array */ ); /* ** CAPI3REF: Obtain old.* Values From A Changeset Iterator ** METHOD: sqlite3_changeset_iter ** ** The pIter argument passed to this function may either be an iterator ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator ** created by [sqlite3changeset_start()]. In the latter case, the most recent ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. ** Furthermore, it may only be called if the type of change that the iterator ** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise, ** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL. ** ** Argument iVal must be greater than or equal to 0, and less than the number ** of columns in the table affected by the current change. Otherwise, ** [SQLITE_RANGE] is returned and *ppValue is set to NULL. ** ** If successful, this function sets *ppValue to point to a protected ** sqlite3_value object containing the iVal'th value from the vector of ** original row values stored as part of the UPDATE or DELETE change and ** returns SQLITE_OK. The name of the function comes from the fact that this ** is similar to the "old.*" columns available to update or delete triggers. ** ** If some other error occurs (e.g. an OOM condition), an SQLite error code ** is returned and *ppValue is set to NULL. */ SQLITE_API int sqlite3changeset_old( sqlite3_changeset_iter *pIter, /* Changeset iterator */ int iVal, /* Column number */ sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */ ); /* ** CAPI3REF: Obtain new.* Values From A Changeset Iterator ** METHOD: sqlite3_changeset_iter ** ** The pIter argument passed to this function may either be an iterator ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator ** created by [sqlite3changeset_start()]. In the latter case, the most recent ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. ** Furthermore, it may only be called if the type of change that the iterator ** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise, ** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL. ** ** Argument iVal must be greater than or equal to 0, and less than the number ** of columns in the table affected by the current change. Otherwise, ** [SQLITE_RANGE] is returned and *ppValue is set to NULL. ** ** If successful, this function sets *ppValue to point to a protected ** sqlite3_value object containing the iVal'th value from the vector of ** new row values stored as part of the UPDATE or INSERT change and ** returns SQLITE_OK. If the change is an UPDATE and does not include ** a new value for the requested column, *ppValue is set to NULL and ** SQLITE_OK returned. The name of the function comes from the fact that ** this is similar to the "new.*" columns available to update or delete ** triggers. ** ** If some other error occurs (e.g. an OOM condition), an SQLite error code ** is returned and *ppValue is set to NULL. */ SQLITE_API int sqlite3changeset_new( sqlite3_changeset_iter *pIter, /* Changeset iterator */ int iVal, /* Column number */ sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */ ); /* ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator ** METHOD: sqlite3_changeset_iter ** ** This function should only be used with iterator objects passed to a ** conflict-handler callback by [sqlite3changeset_apply()] with either ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue ** is set to NULL. ** ** Argument iVal must be greater than or equal to 0, and less than the number ** of columns in the table affected by the current change. Otherwise, ** [SQLITE_RANGE] is returned and *ppValue is set to NULL. ** ** If successful, this function sets *ppValue to point to a protected ** sqlite3_value object containing the iVal'th value from the ** "conflicting row" associated with the current conflict-handler callback ** and returns SQLITE_OK. ** ** If some other error occurs (e.g. an OOM condition), an SQLite error code ** is returned and *ppValue is set to NULL. */ SQLITE_API int sqlite3changeset_conflict( sqlite3_changeset_iter *pIter, /* Changeset iterator */ int iVal, /* Column number */ sqlite3_value **ppValue /* OUT: Value from conflicting row */ ); /* ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations ** METHOD: sqlite3_changeset_iter ** ** This function may only be called with an iterator passed to an ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case ** it sets the output variable to the total number of known foreign key ** violations in the destination database and returns SQLITE_OK. ** ** In all other cases this function returns SQLITE_MISUSE. */ SQLITE_API int sqlite3changeset_fk_conflicts( sqlite3_changeset_iter *pIter, /* Changeset iterator */ int *pnOut /* OUT: Number of FK violations */ ); /* ** CAPI3REF: Finalize A Changeset Iterator ** METHOD: sqlite3_changeset_iter ** ** This function is used to finalize an iterator allocated with ** [sqlite3changeset_start()]. ** ** This function should only be called on iterators created using the ** [sqlite3changeset_start()] function. If an application calls this ** function with an iterator passed to a conflict-handler by ** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the ** call has no effect. ** ** If an error was encountered within a call to an sqlite3changeset_xxx() ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding ** to that error is returned by this function. Otherwise, SQLITE_OK is ** returned. This is to allow the following pattern (pseudo-code): ** **
**   sqlite3changeset_start();
**   while( SQLITE_ROW==sqlite3changeset_next() ){
**     // Do something with change.
**   }
**   rc = sqlite3changeset_finalize();
**   if( rc!=SQLITE_OK ){
**     // An error has occurred
**   }
** 
*/ SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter); /* ** CAPI3REF: Invert A Changeset ** ** This function is used to "invert" a changeset object. Applying an inverted ** changeset to a database reverses the effects of applying the uninverted ** changeset. Specifically: ** **
    **
  • Each DELETE change is changed to an INSERT, and **
  • Each INSERT change is changed to a DELETE, and **
  • For each UPDATE change, the old.* and new.* values are exchanged. **
** ** This function does not change the order in which changes appear within ** the changeset. It merely reverses the sense of each individual change. ** ** If successful, a pointer to a buffer containing the inverted changeset ** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and ** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are ** zeroed and an SQLite error code returned. ** ** It is the responsibility of the caller to eventually call sqlite3_free() ** on the *ppOut pointer to free the buffer allocation following a successful ** call to this function. ** ** WARNING/TODO: This function currently assumes that the input is a valid ** changeset. If it is not, the results are undefined. */ SQLITE_API int sqlite3changeset_invert( int nIn, const void *pIn, /* Input changeset */ int *pnOut, void **ppOut /* OUT: Inverse of input */ ); /* ** CAPI3REF: Concatenate Two Changeset Objects ** ** This function is used to concatenate two changesets, A and B, into a ** single changeset. The result is a changeset equivalent to applying ** changeset A followed by changeset B. ** ** This function combines the two input changesets using an ** sqlite3_changegroup object. Calling it produces similar results as the ** following code fragment: ** **
**   sqlite3_changegroup *pGrp;
**   rc = sqlite3_changegroup_new(&pGrp);
**   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
**   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
**   if( rc==SQLITE_OK ){
**     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
**   }else{
**     *ppOut = 0;
**     *pnOut = 0;
**   }
** 
** ** Refer to the sqlite3_changegroup documentation below for details. */ SQLITE_API int sqlite3changeset_concat( int nA, /* Number of bytes in buffer pA */ void *pA, /* Pointer to buffer containing changeset A */ int nB, /* Number of bytes in buffer pB */ void *pB, /* Pointer to buffer containing changeset B */ int *pnOut, /* OUT: Number of bytes in output changeset */ void **ppOut /* OUT: Buffer containing output changeset */ ); /* ** CAPI3REF: Changegroup Handle ** ** A changegroup is an object used to combine two or more ** [changesets] or [patchsets] */ typedef struct sqlite3_changegroup sqlite3_changegroup; /* ** CAPI3REF: Create A New Changegroup Object ** CONSTRUCTOR: sqlite3_changegroup ** ** An sqlite3_changegroup object is used to combine two or more changesets ** (or patchsets) into a single changeset (or patchset). A single changegroup ** object may combine changesets or patchsets, but not both. The output is ** always in the same format as the input. ** ** If successful, this function returns SQLITE_OK and populates (*pp) with ** a pointer to a new sqlite3_changegroup object before returning. The caller ** should eventually free the returned object using a call to ** sqlite3changegroup_delete(). If an error occurs, an SQLite error code ** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL. ** ** The usual usage pattern for an sqlite3_changegroup object is as follows: ** **
    **
  • It is created using a call to sqlite3changegroup_new(). ** **
  • Zero or more changesets (or patchsets) are added to the object ** by calling sqlite3changegroup_add(). ** **
  • The result of combining all input changesets together is obtained ** by the application via a call to sqlite3changegroup_output(). ** **
  • The object is deleted using a call to sqlite3changegroup_delete(). **
** ** Any number of calls to add() and output() may be made between the calls to ** new() and delete(), and in any order. ** ** As well as the regular sqlite3changegroup_add() and ** sqlite3changegroup_output() functions, also available are the streaming ** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm(). */ SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp); /* ** CAPI3REF: Add a Schema to a Changegroup ** METHOD: sqlite3_changegroup_schema ** ** This method may be used to optionally enforce the rule that the changesets ** added to the changegroup handle must match the schema of database zDb ** ("main", "temp", or the name of an attached database). If ** sqlite3changegroup_add() is called to add a changeset that is not compatible ** with the configured schema, SQLITE_SCHEMA is returned and the changegroup ** object is left in an undefined state. ** ** A changeset schema is considered compatible with the database schema in ** the same way as for sqlite3changeset_apply(). Specifically, for each ** table in the changeset, there exists a database table with: ** **
    **
  • The name identified by the changeset, and **
  • at least as many columns as recorded in the changeset, and **
  • the primary key columns in the same position as recorded in ** the changeset. **
** ** The output of the changegroup object always has the same schema as the ** database nominated using this function. In cases where changesets passed ** to sqlite3changegroup_add() have fewer columns than the corresponding table ** in the database schema, these are filled in using the default column ** values from the database schema. This makes it possible to combined ** changesets that have different numbers of columns for a single table ** within a changegroup, provided that they are otherwise compatible. */ SQLITE_API int sqlite3changegroup_schema(sqlite3_changegroup*, sqlite3*, const char *zDb); /* ** CAPI3REF: Add A Changeset To A Changegroup ** METHOD: sqlite3_changegroup ** ** Add all changes within the changeset (or patchset) in buffer pData (size ** nData bytes) to the changegroup. ** ** If the buffer contains a patchset, then all prior calls to this function ** on the same changegroup object must also have specified patchsets. Or, if ** the buffer contains a changeset, so must have the earlier calls to this ** function. Otherwise, SQLITE_ERROR is returned and no changes are added ** to the changegroup. ** ** Rows within the changeset and changegroup are identified by the values in ** their PRIMARY KEY columns. A change in the changeset is considered to ** apply to the same row as a change already present in the changegroup if ** the two rows have the same primary key. ** ** Changes to rows that do not already appear in the changegroup are ** simply copied into it. Or, if both the new changeset and the changegroup ** contain changes that apply to a single row, the final contents of the ** changegroup depends on the type of each change, as follows: ** ** ** ** **
Existing Change New Change Output Change **
INSERT INSERT ** The new change is ignored. This case does not occur if the new ** changeset was recorded immediately after the changesets already ** added to the changegroup. **
INSERT UPDATE ** The INSERT change remains in the changegroup. The values in the ** INSERT change are modified as if the row was inserted by the ** existing change and then updated according to the new change. **
INSERT DELETE ** The existing INSERT is removed from the changegroup. The DELETE is ** not added. **
UPDATE INSERT ** The new change is ignored. This case does not occur if the new ** changeset was recorded immediately after the changesets already ** added to the changegroup. **
UPDATE UPDATE ** The existing UPDATE remains within the changegroup. It is amended ** so that the accompanying values are as if the row was updated once ** by the existing change and then again by the new change. **
UPDATE DELETE ** The existing UPDATE is replaced by the new DELETE within the ** changegroup. **
DELETE INSERT ** If one or more of the column values in the row inserted by the ** new change differ from those in the row deleted by the existing ** change, the existing DELETE is replaced by an UPDATE within the ** changegroup. Otherwise, if the inserted row is exactly the same ** as the deleted row, the existing DELETE is simply discarded. **
DELETE UPDATE ** The new change is ignored. This case does not occur if the new ** changeset was recorded immediately after the changesets already ** added to the changegroup. **
DELETE DELETE ** The new change is ignored. This case does not occur if the new ** changeset was recorded immediately after the changesets already ** added to the changegroup. **
** ** If the new changeset contains changes to a table that is already present ** in the changegroup, then the number of columns and the position of the ** primary key columns for the table must be consistent. If this is not the ** case, this function fails with SQLITE_SCHEMA. Except, if the changegroup ** object has been configured with a database schema using the ** sqlite3changegroup_schema() API, then it is possible to combine changesets ** with different numbers of columns for a single table, provided that ** they are otherwise compatible. ** ** If the input changeset appears to be corrupt and the corruption is ** detected, SQLITE_CORRUPT is returned. Or, if an out-of-memory condition ** occurs during processing, this function returns SQLITE_NOMEM. ** ** In all cases, if an error occurs the state of the final contents of the ** changegroup is undefined. If no error occurs, SQLITE_OK is returned. */ SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData); /* ** CAPI3REF: Add A Single Change To A Changegroup ** METHOD: sqlite3_changegroup ** ** This function adds the single change currently indicated by the iterator ** passed as the second argument to the changegroup object. The rules for ** adding the change are just as described for [sqlite3changegroup_add()]. ** ** If the change is successfully added to the changegroup, SQLITE_OK is ** returned. Otherwise, an SQLite error code is returned. ** ** The iterator must point to a valid entry when this function is called. ** If it does not, SQLITE_ERROR is returned and no change is added to the ** changegroup. Additionally, the iterator must not have been opened with ** the SQLITE_CHANGESETAPPLY_INVERT flag. In this case SQLITE_ERROR is also ** returned. */ SQLITE_API int sqlite3changegroup_add_change( sqlite3_changegroup*, sqlite3_changeset_iter* ); /* ** CAPI3REF: Obtain A Composite Changeset From A Changegroup ** METHOD: sqlite3_changegroup ** ** Obtain a buffer containing a changeset (or patchset) representing the ** current contents of the changegroup. If the inputs to the changegroup ** were themselves changesets, the output is a changeset. Or, if the ** inputs were patchsets, the output is also a patchset. ** ** As with the output of the sqlite3session_changeset() and ** sqlite3session_patchset() functions, all changes related to a single ** table are grouped together in the output of this function. Tables appear ** in the same order as for the very first changeset added to the changegroup. ** If the second or subsequent changesets added to the changegroup contain ** changes for tables that do not appear in the first changeset, they are ** appended onto the end of the output changeset, again in the order in ** which they are first encountered. ** ** If an error occurs, an SQLite error code is returned and the output ** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK ** is returned and the output variables are set to the size of and a ** pointer to the output buffer, respectively. In this case it is the ** responsibility of the caller to eventually free the buffer using a ** call to sqlite3_free(). */ SQLITE_API int sqlite3changegroup_output( sqlite3_changegroup*, int *pnData, /* OUT: Size of output buffer in bytes */ void **ppData /* OUT: Pointer to output buffer */ ); /* ** CAPI3REF: Delete A Changegroup Object ** DESTRUCTOR: sqlite3_changegroup */ SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*); /* ** CAPI3REF: Apply A Changeset To A Database ** ** Apply a changeset or patchset to a database. These functions attempt to ** update the "main" database attached to handle db with the changes found in ** the changeset passed via the second and third arguments. ** ** The fourth argument (xFilter) passed to these functions is the "filter ** callback". If it is not NULL, then for each table affected by at least one ** change in the changeset, the filter callback is invoked with ** the table name as the second argument, and a copy of the context pointer ** passed as the sixth argument as the first. If the "filter callback" ** returns zero, then no attempt is made to apply any changes to the table. ** Otherwise, if the return value is non-zero or the xFilter argument to ** is NULL, all changes related to the table are attempted. ** ** For each table that is not excluded by the filter callback, this function ** tests that the target database contains a compatible table. A table is ** considered compatible if all of the following are true: ** **
    **
  • The table has the same name as the name recorded in the ** changeset, and **
  • The table has at least as many columns as recorded in the ** changeset, and **
  • The table has primary key columns in the same position as ** recorded in the changeset. **
** ** If there is no compatible table, it is not an error, but none of the ** changes associated with the table are applied. A warning message is issued ** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most ** one such warning is issued for each table in the changeset. ** ** For each change for which there is a compatible table, an attempt is made ** to modify the table contents according to the UPDATE, INSERT or DELETE ** change. If a change cannot be applied cleanly, the conflict handler ** function passed as the fifth argument to sqlite3changeset_apply() may be ** invoked. A description of exactly when the conflict handler is invoked for ** each type of change is below. ** ** Unlike the xFilter argument, xConflict may not be passed NULL. The results ** of passing anything other than a valid function pointer as the xConflict ** argument are undefined. ** ** Each time the conflict handler function is invoked, it must return one ** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or ** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned ** if the second argument passed to the conflict handler is either ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler ** returns an illegal value, any changes already made are rolled back and ** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different ** actions are taken by sqlite3changeset_apply() depending on the value ** returned by each invocation of the conflict-handler function. Refer to ** the documentation for the three ** [SQLITE_CHANGESET_OMIT|available return values] for details. ** **
**
DELETE Changes
** For each DELETE change, the function checks if the target database ** contains a row with the same primary key value (or values) as the ** original row values stored in the changeset. If it does, and the values ** stored in all non-primary key columns also match the values stored in ** the changeset the row is deleted from the target database. ** ** If a row with matching primary key values is found, but one or more of ** the non-primary key fields contains a value different from the original ** row value stored in the changeset, the conflict-handler function is ** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the ** database table has more columns than are recorded in the changeset, ** only the values of those non-primary key fields are compared against ** the current database contents - any trailing database table columns ** are ignored. ** ** If no row with matching primary key values is found in the database, ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND] ** passed as the second argument. ** ** If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT ** (which can only happen if a foreign key constraint is violated), the ** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT] ** passed as the second argument. This includes the case where the DELETE ** operation is attempted because an earlier call to the conflict handler ** function returned [SQLITE_CHANGESET_REPLACE]. ** **
INSERT Changes
** For each INSERT change, an attempt is made to insert the new row into ** the database. If the changeset row contains fewer fields than the ** database table, the trailing fields are populated with their default ** values. ** ** If the attempt to insert the row fails because the database already ** contains a row with the same primary key values, the conflict handler ** function is invoked with the second argument set to ** [SQLITE_CHANGESET_CONFLICT]. ** ** If the attempt to insert the row fails because of some other constraint ** violation (e.g. NOT NULL or UNIQUE), the conflict handler function is ** invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT]. ** This includes the case where the INSERT operation is re-attempted because ** an earlier call to the conflict handler function returned ** [SQLITE_CHANGESET_REPLACE]. ** **
UPDATE Changes
** For each UPDATE change, the function checks if the target database ** contains a row with the same primary key value (or values) as the ** original row values stored in the changeset. If it does, and the values ** stored in all modified non-primary key columns also match the values ** stored in the changeset the row is updated within the target database. ** ** If a row with matching primary key values is found, but one or more of ** the modified non-primary key fields contains a value different from an ** original row value stored in the changeset, the conflict-handler function ** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since ** UPDATE changes only contain values for non-primary key fields that are ** to be modified, only those fields need to match the original values to ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback. ** ** If no row with matching primary key values is found in the database, ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND] ** passed as the second argument. ** ** If the UPDATE operation is attempted, but SQLite returns ** SQLITE_CONSTRAINT, the conflict-handler function is invoked with ** [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument. ** This includes the case where the UPDATE operation is attempted after ** an earlier call to the conflict handler function returned ** [SQLITE_CHANGESET_REPLACE]. **
** ** It is safe to execute SQL statements, including those that write to the ** table that the callback related to, from within the xConflict callback. ** This can be used to further customize the application's conflict ** resolution strategy. ** ** All changes made by these functions are enclosed in a savepoint transaction. ** If any other error (aside from a constraint failure when attempting to ** write to the target database) occurs, then the savepoint transaction is ** rolled back, restoring the target database to its original state, and an ** SQLite error code returned. ** ** If the output parameters (ppRebase) and (pnRebase) are non-NULL and ** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2() ** may set (*ppRebase) to point to a "rebase" that may be used with the ** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase) ** is set to the size of the buffer in bytes. It is the responsibility of the ** caller to eventually free any such buffer using sqlite3_free(). The buffer ** is only allocated and populated if one or more conflicts were encountered ** while applying the patchset. See comments surrounding the sqlite3_rebaser ** APIs for further details. ** ** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent ** may be modified by passing a combination of ** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter. ** ** Note that the sqlite3changeset_apply_v2() API is still experimental ** and therefore subject to change. */ SQLITE_API int sqlite3changeset_apply( sqlite3 *db, /* Apply change to "main" db of this handle */ int nChangeset, /* Size of changeset in bytes */ void *pChangeset, /* Changeset blob */ int(*xFilter)( void *pCtx, /* Copy of sixth arg to _apply() */ const char *zTab /* Table name */ ), int(*xConflict)( void *pCtx, /* Copy of sixth arg to _apply() */ int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ sqlite3_changeset_iter *p /* Handle describing change and conflict */ ), void *pCtx /* First argument passed to xConflict */ ); SQLITE_API int sqlite3changeset_apply_v2( sqlite3 *db, /* Apply change to "main" db of this handle */ int nChangeset, /* Size of changeset in bytes */ void *pChangeset, /* Changeset blob */ int(*xFilter)( void *pCtx, /* Copy of sixth arg to _apply() */ const char *zTab /* Table name */ ), int(*xConflict)( void *pCtx, /* Copy of sixth arg to _apply() */ int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ sqlite3_changeset_iter *p /* Handle describing change and conflict */ ), void *pCtx, /* First argument passed to xConflict */ void **ppRebase, int *pnRebase, /* OUT: Rebase data */ int flags /* SESSION_CHANGESETAPPLY_* flags */ ); /* ** CAPI3REF: Flags for sqlite3changeset_apply_v2 ** ** The following flags may passed via the 9th parameter to ** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]: ** **
**
SQLITE_CHANGESETAPPLY_NOSAVEPOINT
** Usually, the sessions module encloses all operations performed by ** a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The ** SAVEPOINT is committed if the changeset or patchset is successfully ** applied, or rolled back if an error occurs. Specifying this flag ** causes the sessions module to omit this savepoint. In this case, if the ** caller has an open transaction or savepoint when apply_v2() is called, ** it may revert the partially applied changeset by rolling it back. ** **
SQLITE_CHANGESETAPPLY_INVERT
** Invert the changeset before applying it. This is equivalent to inverting ** a changeset using sqlite3changeset_invert() before applying it. It is ** an error to specify this flag with a patchset. ** **
SQLITE_CHANGESETAPPLY_IGNORENOOP
** Do not invoke the conflict handler callback for any changes that ** would not actually modify the database even if they were applied. ** Specifically, this means that the conflict handler is not invoked ** for: **
    **
  • a delete change if the row being deleted cannot be found, **
  • an update change if the modified fields are already set to ** their new values in the conflicting row, or **
  • an insert change if all fields of the conflicting row match ** the row being inserted. **
** **
SQLITE_CHANGESETAPPLY_FKNOACTION
** If this flag it set, then all foreign key constraints in the target ** database behave as if they were declared with "ON UPDATE NO ACTION ON ** DELETE NO ACTION", even if they are actually CASCADE, RESTRICT, SET NULL ** or SET DEFAULT. */ #define SQLITE_CHANGESETAPPLY_NOSAVEPOINT 0x0001 #define SQLITE_CHANGESETAPPLY_INVERT 0x0002 #define SQLITE_CHANGESETAPPLY_IGNORENOOP 0x0004 #define SQLITE_CHANGESETAPPLY_FKNOACTION 0x0008 /* ** CAPI3REF: Constants Passed To The Conflict Handler ** ** Values that may be passed as the second argument to a conflict-handler. ** **
**
SQLITE_CHANGESET_DATA
** The conflict handler is invoked with CHANGESET_DATA as the second argument ** when processing a DELETE or UPDATE change if a row with the required ** PRIMARY KEY fields is present in the database, but one or more other ** (non primary-key) fields modified by the update do not contain the ** expected "before" values. ** ** The conflicting row, in this case, is the database row with the matching ** primary key. ** **
SQLITE_CHANGESET_NOTFOUND
** The conflict handler is invoked with CHANGESET_NOTFOUND as the second ** argument when processing a DELETE or UPDATE change if a row with the ** required PRIMARY KEY fields is not present in the database. ** ** There is no conflicting row in this case. The results of invoking the ** sqlite3changeset_conflict() API are undefined. ** **
SQLITE_CHANGESET_CONFLICT
** CHANGESET_CONFLICT is passed as the second argument to the conflict ** handler while processing an INSERT change if the operation would result ** in duplicate primary key values. ** ** The conflicting row in this case is the database row with the matching ** primary key. ** **
SQLITE_CHANGESET_FOREIGN_KEY
** If foreign key handling is enabled, and applying a changeset leaves the ** database in a state containing foreign key violations, the conflict ** handler is invoked with CHANGESET_FOREIGN_KEY as the second argument ** exactly once before the changeset is committed. If the conflict handler ** returns CHANGESET_OMIT, the changes, including those that caused the ** foreign key constraint violation, are committed. Or, if it returns ** CHANGESET_ABORT, the changeset is rolled back. ** ** No current or conflicting row information is provided. The only function ** it is possible to call on the supplied sqlite3_changeset_iter handle ** is sqlite3changeset_fk_conflicts(). ** **
SQLITE_CHANGESET_CONSTRAINT
** If any other constraint violation occurs while applying a change (i.e. ** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is ** invoked with CHANGESET_CONSTRAINT as the second argument. ** ** There is no conflicting row in this case. The results of invoking the ** sqlite3changeset_conflict() API are undefined. ** **
*/ #define SQLITE_CHANGESET_DATA 1 #define SQLITE_CHANGESET_NOTFOUND 2 #define SQLITE_CHANGESET_CONFLICT 3 #define SQLITE_CHANGESET_CONSTRAINT 4 #define SQLITE_CHANGESET_FOREIGN_KEY 5 /* ** CAPI3REF: Constants Returned By The Conflict Handler ** ** A conflict handler callback must return one of the following three values. ** **
**
SQLITE_CHANGESET_OMIT
** If a conflict handler returns this value no special action is taken. The ** change that caused the conflict is not applied. The session module ** continues to the next change in the changeset. ** **
SQLITE_CHANGESET_REPLACE
** This value may only be returned if the second argument to the conflict ** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this ** is not the case, any changes applied so far are rolled back and the ** call to sqlite3changeset_apply() returns SQLITE_MISUSE. ** ** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict ** handler, then the conflicting row is either updated or deleted, depending ** on the type of change. ** ** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict ** handler, then the conflicting row is removed from the database and a ** second attempt to apply the change is made. If this second attempt fails, ** the original row is restored to the database before continuing. ** **
SQLITE_CHANGESET_ABORT
** If this value is returned, any changes applied so far are rolled back ** and the call to sqlite3changeset_apply() returns SQLITE_ABORT. **
*/ #define SQLITE_CHANGESET_OMIT 0 #define SQLITE_CHANGESET_REPLACE 1 #define SQLITE_CHANGESET_ABORT 2 /* ** CAPI3REF: Rebasing changesets ** EXPERIMENTAL ** ** Suppose there is a site hosting a database in state S0. And that ** modifications are made that move that database to state S1 and a ** changeset recorded (the "local" changeset). Then, a changeset based ** on S0 is received from another site (the "remote" changeset) and ** applied to the database. The database is then in state ** (S1+"remote"), where the exact state depends on any conflict ** resolution decisions (OMIT or REPLACE) made while applying "remote". ** Rebasing a changeset is to update it to take those conflict ** resolution decisions into account, so that the same conflicts ** do not have to be resolved elsewhere in the network. ** ** For example, if both the local and remote changesets contain an ** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)": ** ** local: INSERT INTO t1 VALUES(1, 'v1'); ** remote: INSERT INTO t1 VALUES(1, 'v2'); ** ** and the conflict resolution is REPLACE, then the INSERT change is ** removed from the local changeset (it was overridden). Or, if the ** conflict resolution was "OMIT", then the local changeset is modified ** to instead contain: ** ** UPDATE t1 SET b = 'v2' WHERE a=1; ** ** Changes within the local changeset are rebased as follows: ** **
**
Local INSERT
** This may only conflict with a remote INSERT. If the conflict ** resolution was OMIT, then add an UPDATE change to the rebased ** changeset. Or, if the conflict resolution was REPLACE, add ** nothing to the rebased changeset. ** **
Local DELETE
** This may conflict with a remote UPDATE or DELETE. In both cases the ** only possible resolution is OMIT. If the remote operation was a ** DELETE, then add no change to the rebased changeset. If the remote ** operation was an UPDATE, then the old.* fields of change are updated ** to reflect the new.* values in the UPDATE. ** **
Local UPDATE
** This may conflict with a remote UPDATE or DELETE. If it conflicts ** with a DELETE, and the conflict resolution was OMIT, then the update ** is changed into an INSERT. Any undefined values in the new.* record ** from the update change are filled in using the old.* values from ** the conflicting DELETE. Or, if the conflict resolution was REPLACE, ** the UPDATE change is simply omitted from the rebased changeset. ** ** If conflict is with a remote UPDATE and the resolution is OMIT, then ** the old.* values are rebased using the new.* values in the remote ** change. Or, if the resolution is REPLACE, then the change is copied ** into the rebased changeset with updates to columns also updated by ** the conflicting remote UPDATE removed. If this means no columns would ** be updated, the change is omitted. **
** ** A local change may be rebased against multiple remote changes ** simultaneously. If a single key is modified by multiple remote ** changesets, they are combined as follows before the local changeset ** is rebased: ** **
    **
  • If there has been one or more REPLACE resolutions on a ** key, it is rebased according to a REPLACE. ** **
  • If there have been no REPLACE resolutions on a key, then ** the local changeset is rebased according to the most recent ** of the OMIT resolutions. **
** ** Note that conflict resolutions from multiple remote changesets are ** combined on a per-field basis, not per-row. This means that in the ** case of multiple remote UPDATE operations, some fields of a single ** local change may be rebased for REPLACE while others are rebased for ** OMIT. ** ** In order to rebase a local changeset, the remote changeset must first ** be applied to the local database using sqlite3changeset_apply_v2() and ** the buffer of rebase information captured. Then: ** **
    **
  1. An sqlite3_rebaser object is created by calling ** sqlite3rebaser_create(). **
  2. The new object is configured with the rebase buffer obtained from ** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure(). ** If the local changeset is to be rebased against multiple remote ** changesets, then sqlite3rebaser_configure() should be called ** multiple times, in the same order that the multiple ** sqlite3changeset_apply_v2() calls were made. **
  3. Each local changeset is rebased by calling sqlite3rebaser_rebase(). **
  4. The sqlite3_rebaser object is deleted by calling ** sqlite3rebaser_delete(). **
*/ typedef struct sqlite3_rebaser sqlite3_rebaser; /* ** CAPI3REF: Create a changeset rebaser object. ** EXPERIMENTAL ** ** Allocate a new changeset rebaser object. If successful, set (*ppNew) to ** point to the new object and return SQLITE_OK. Otherwise, if an error ** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew) ** to NULL. */ SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew); /* ** CAPI3REF: Configure a changeset rebaser object. ** EXPERIMENTAL ** ** Configure the changeset rebaser object to rebase changesets according ** to the conflict resolutions described by buffer pRebase (size nRebase ** bytes), which must have been obtained from a previous call to ** sqlite3changeset_apply_v2(). */ SQLITE_API int sqlite3rebaser_configure( sqlite3_rebaser*, int nRebase, const void *pRebase ); /* ** CAPI3REF: Rebase a changeset ** EXPERIMENTAL ** ** Argument pIn must point to a buffer containing a changeset nIn bytes ** in size. This function allocates and populates a buffer with a copy ** of the changeset rebased according to the configuration of the ** rebaser object passed as the first argument. If successful, (*ppOut) ** is set to point to the new buffer containing the rebased changeset and ** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the ** responsibility of the caller to eventually free the new buffer using ** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut) ** are set to zero and an SQLite error code returned. */ SQLITE_API int sqlite3rebaser_rebase( sqlite3_rebaser*, int nIn, const void *pIn, int *pnOut, void **ppOut ); /* ** CAPI3REF: Delete a changeset rebaser object. ** EXPERIMENTAL ** ** Delete the changeset rebaser object and all associated resources. There ** should be one call to this function for each successful invocation ** of sqlite3rebaser_create(). */ SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p); /* ** CAPI3REF: Streaming Versions of API functions. ** ** The six streaming API xxx_strm() functions serve similar purposes to the ** corresponding non-streaming API functions: ** ** ** **
Streaming functionNon-streaming equivalent
sqlite3changeset_apply_strm[sqlite3changeset_apply] **
sqlite3changeset_apply_strm_v2[sqlite3changeset_apply_v2] **
sqlite3changeset_concat_strm[sqlite3changeset_concat] **
sqlite3changeset_invert_strm[sqlite3changeset_invert] **
sqlite3changeset_start_strm[sqlite3changeset_start] **
sqlite3session_changeset_strm[sqlite3session_changeset] **
sqlite3session_patchset_strm[sqlite3session_patchset] **
** ** Non-streaming functions that accept changesets (or patchsets) as input ** require that the entire changeset be stored in a single buffer in memory. ** Similarly, those that return a changeset or patchset do so by returning ** a pointer to a single large buffer allocated using sqlite3_malloc(). ** Normally this is convenient. However, if an application running in a ** low-memory environment is required to handle very large changesets, the ** large contiguous memory allocations required can become onerous. ** ** In order to avoid this problem, instead of a single large buffer, input ** is passed to a streaming API functions by way of a callback function that ** the sessions module invokes to incrementally request input data as it is ** required. In all cases, a pair of API function parameters such as ** **
**        int nChangeset,
**        void *pChangeset,
**  
** ** Is replaced by: ** **
**        int (*xInput)(void *pIn, void *pData, int *pnData),
**        void *pIn,
**  
** ** Each time the xInput callback is invoked by the sessions module, the first ** argument passed is a copy of the supplied pIn context pointer. The second ** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no ** error occurs the xInput method should copy up to (*pnData) bytes of data ** into the buffer and set (*pnData) to the actual number of bytes copied ** before returning SQLITE_OK. If the input is completely exhausted, (*pnData) ** should be set to zero to indicate this. Or, if an error occurs, an SQLite ** error code should be returned. In all cases, if an xInput callback returns ** an error, all processing is abandoned and the streaming API function ** returns a copy of the error code to the caller. ** ** In the case of sqlite3changeset_start_strm(), the xInput callback may be ** invoked by the sessions module at any point during the lifetime of the ** iterator. If such an xInput callback returns an error, the iterator enters ** an error state, whereby all subsequent calls to iterator functions ** immediately fail with the same error code as returned by xInput. ** ** Similarly, streaming API functions that return changesets (or patchsets) ** return them in chunks by way of a callback function instead of via a ** pointer to a single large buffer. In this case, a pair of parameters such ** as: ** **
**        int *pnChangeset,
**        void **ppChangeset,
**  
** ** Is replaced by: ** **
**        int (*xOutput)(void *pOut, const void *pData, int nData),
**        void *pOut
**  
** ** The xOutput callback is invoked zero or more times to return data to ** the application. The first parameter passed to each call is a copy of the ** pOut pointer supplied by the application. The second parameter, pData, ** points to a buffer nData bytes in size containing the chunk of output ** data being returned. If the xOutput callback successfully processes the ** supplied data, it should return SQLITE_OK to indicate success. Otherwise, ** it should return some other SQLite error code. In this case processing ** is immediately abandoned and the streaming API function returns a copy ** of the xOutput error code to the application. ** ** The sessions module never invokes an xOutput callback with the third ** parameter set to a value less than or equal to zero. Other than this, ** no guarantees are made as to the size of the chunks of data returned. */ SQLITE_API int sqlite3changeset_apply_strm( sqlite3 *db, /* Apply change to "main" db of this handle */ int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ void *pIn, /* First arg for xInput */ int(*xFilter)( void *pCtx, /* Copy of sixth arg to _apply() */ const char *zTab /* Table name */ ), int(*xConflict)( void *pCtx, /* Copy of sixth arg to _apply() */ int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ sqlite3_changeset_iter *p /* Handle describing change and conflict */ ), void *pCtx /* First argument passed to xConflict */ ); SQLITE_API int sqlite3changeset_apply_v2_strm( sqlite3 *db, /* Apply change to "main" db of this handle */ int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ void *pIn, /* First arg for xInput */ int(*xFilter)( void *pCtx, /* Copy of sixth arg to _apply() */ const char *zTab /* Table name */ ), int(*xConflict)( void *pCtx, /* Copy of sixth arg to _apply() */ int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ sqlite3_changeset_iter *p /* Handle describing change and conflict */ ), void *pCtx, /* First argument passed to xConflict */ void **ppRebase, int *pnRebase, int flags ); SQLITE_API int sqlite3changeset_concat_strm( int (*xInputA)(void *pIn, void *pData, int *pnData), void *pInA, int (*xInputB)(void *pIn, void *pData, int *pnData), void *pInB, int (*xOutput)(void *pOut, const void *pData, int nData), void *pOut ); SQLITE_API int sqlite3changeset_invert_strm( int (*xInput)(void *pIn, void *pData, int *pnData), void *pIn, int (*xOutput)(void *pOut, const void *pData, int nData), void *pOut ); SQLITE_API int sqlite3changeset_start_strm( sqlite3_changeset_iter **pp, int (*xInput)(void *pIn, void *pData, int *pnData), void *pIn ); SQLITE_API int sqlite3changeset_start_v2_strm( sqlite3_changeset_iter **pp, int (*xInput)(void *pIn, void *pData, int *pnData), void *pIn, int flags ); SQLITE_API int sqlite3session_changeset_strm( sqlite3_session *pSession, int (*xOutput)(void *pOut, const void *pData, int nData), void *pOut ); SQLITE_API int sqlite3session_patchset_strm( sqlite3_session *pSession, int (*xOutput)(void *pOut, const void *pData, int nData), void *pOut ); SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*, int (*xInput)(void *pIn, void *pData, int *pnData), void *pIn ); SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*, int (*xOutput)(void *pOut, const void *pData, int nData), void *pOut ); SQLITE_API int sqlite3rebaser_rebase_strm( sqlite3_rebaser *pRebaser, int (*xInput)(void *pIn, void *pData, int *pnData), void *pIn, int (*xOutput)(void *pOut, const void *pData, int nData), void *pOut ); /* ** CAPI3REF: Configure global parameters ** ** The sqlite3session_config() interface is used to make global configuration ** changes to the sessions module in order to tune it to the specific needs ** of the application. ** ** The sqlite3session_config() interface is not threadsafe. If it is invoked ** while any other thread is inside any other sessions method then the ** results are undefined. Furthermore, if it is invoked after any sessions ** related objects have been created, the results are also undefined. ** ** The first argument to the sqlite3session_config() function must be one ** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The ** interpretation of the (void*) value passed as the second parameter and ** the effect of calling this function depends on the value of the first ** parameter. ** **
**
SQLITE_SESSION_CONFIG_STRMSIZE
** By default, the sessions module streaming interfaces attempt to input ** and output data in approximately 1 KiB chunks. This operand may be used ** to set and query the value of this configuration setting. The pointer ** passed as the second argument must point to a value of type (int). ** If this value is greater than 0, it is used as the new streaming data ** chunk size for both input and output. Before returning, the (int) value ** pointed to by pArg is set to the final value of the streaming interface ** chunk size. **
** ** This function returns SQLITE_OK if successful, or an SQLite error code ** otherwise. */ SQLITE_API int sqlite3session_config(int op, void *pArg); /* ** CAPI3REF: Values for sqlite3session_config(). */ #define SQLITE_SESSION_CONFIG_STRMSIZE 1 /* ** Make sure we can call this stuff from C++. */ #ifdef __cplusplus } #endif #endif /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */ /******** End of sqlite3session.h *********/ /******** Begin file fts5.h *********/ /* ** 2014 May 31 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ****************************************************************************** ** ** Interfaces to extend FTS5. Using the interfaces defined in this file, ** FTS5 may be extended with: ** ** * custom tokenizers, and ** * custom auxiliary functions. */ #ifndef _FTS5_H #define _FTS5_H #ifdef __cplusplus extern "C" { #endif /************************************************************************* ** CUSTOM AUXILIARY FUNCTIONS ** ** Virtual table implementations may overload SQL functions by implementing ** the sqlite3_module.xFindFunction() method. */ typedef struct Fts5ExtensionApi Fts5ExtensionApi; typedef struct Fts5Context Fts5Context; typedef struct Fts5PhraseIter Fts5PhraseIter; typedef void (*fts5_extension_function)( const Fts5ExtensionApi *pApi, /* API offered by current FTS version */ Fts5Context *pFts, /* First arg to pass to pApi functions */ sqlite3_context *pCtx, /* Context for returning result/error */ int nVal, /* Number of values in apVal[] array */ sqlite3_value **apVal /* Array of trailing arguments */ ); struct Fts5PhraseIter { const unsigned char *a; const unsigned char *b; }; /* ** EXTENSION API FUNCTIONS ** ** xUserData(pFts): ** Return a copy of the pUserData pointer passed to the xCreateFunction() ** API when the extension function was registered. ** ** xColumnTotalSize(pFts, iCol, pnToken): ** If parameter iCol is less than zero, set output variable *pnToken ** to the total number of tokens in the FTS5 table. Or, if iCol is ** non-negative but less than the number of columns in the table, return ** the total number of tokens in column iCol, considering all rows in ** the FTS5 table. ** ** If parameter iCol is greater than or equal to the number of columns ** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g. ** an OOM condition or IO error), an appropriate SQLite error code is ** returned. ** ** xColumnCount(pFts): ** Return the number of columns in the table. ** ** xColumnSize(pFts, iCol, pnToken): ** If parameter iCol is less than zero, set output variable *pnToken ** to the total number of tokens in the current row. Or, if iCol is ** non-negative but less than the number of columns in the table, set ** *pnToken to the number of tokens in column iCol of the current row. ** ** If parameter iCol is greater than or equal to the number of columns ** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g. ** an OOM condition or IO error), an appropriate SQLite error code is ** returned. ** ** This function may be quite inefficient if used with an FTS5 table ** created with the "columnsize=0" option. ** ** xColumnText: ** If parameter iCol is less than zero, or greater than or equal to the ** number of columns in the table, SQLITE_RANGE is returned. ** ** Otherwise, this function attempts to retrieve the text of column iCol of ** the current document. If successful, (*pz) is set to point to a buffer ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes ** (not characters) of the buffer and SQLITE_OK is returned. Otherwise, ** if an error occurs, an SQLite error code is returned and the final values ** of (*pz) and (*pn) are undefined. ** ** xPhraseCount: ** Returns the number of phrases in the current query expression. ** ** xPhraseSize: ** If parameter iCol is less than zero, or greater than or equal to the ** number of phrases in the current query, as returned by xPhraseCount, ** 0 is returned. Otherwise, this function returns the number of tokens in ** phrase iPhrase of the query. Phrases are numbered starting from zero. ** ** xInstCount: ** Set *pnInst to the total number of occurrences of all phrases within ** the query within the current row. Return SQLITE_OK if successful, or ** an error code (i.e. SQLITE_NOMEM) if an error occurs. ** ** This API can be quite slow if used with an FTS5 table created with the ** "detail=none" or "detail=column" option. If the FTS5 table is created ** with either "detail=none" or "detail=column" and "content=" option ** (i.e. if it is a contentless table), then this API always returns 0. ** ** xInst: ** Query for the details of phrase match iIdx within the current row. ** Phrase matches are numbered starting from zero, so the iIdx argument ** should be greater than or equal to zero and smaller than the value ** output by xInstCount(). If iIdx is less than zero or greater than ** or equal to the value returned by xInstCount(), SQLITE_RANGE is returned. ** ** Otherwise, output parameter *piPhrase is set to the phrase number, *piCol ** to the column in which it occurs and *piOff the token offset of the ** first token of the phrase. SQLITE_OK is returned if successful, or an ** error code (i.e. SQLITE_NOMEM) if an error occurs. ** ** This API can be quite slow if used with an FTS5 table created with the ** "detail=none" or "detail=column" option. ** ** xRowid: ** Returns the rowid of the current row. ** ** xTokenize: ** Tokenize text using the tokenizer belonging to the FTS5 table. ** ** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback): ** This API function is used to query the FTS table for phrase iPhrase ** of the current query. Specifically, a query equivalent to: ** ** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid ** ** with $p set to a phrase equivalent to the phrase iPhrase of the ** current query is executed. Any column filter that applies to ** phrase iPhrase of the current query is included in $p. For each ** row visited, the callback function passed as the fourth argument ** is invoked. The context and API objects passed to the callback ** function may be used to access the properties of each matched row. ** Invoking Api.xUserData() returns a copy of the pointer passed as ** the third argument to pUserData. ** ** If parameter iPhrase is less than zero, or greater than or equal to ** the number of phrases in the query, as returned by xPhraseCount(), ** this function returns SQLITE_RANGE. ** ** If the callback function returns any value other than SQLITE_OK, the ** query is abandoned and the xQueryPhrase function returns immediately. ** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK. ** Otherwise, the error code is propagated upwards. ** ** If the query runs to completion without incident, SQLITE_OK is returned. ** Or, if some error occurs before the query completes or is aborted by ** the callback, an SQLite error code is returned. ** ** ** xSetAuxdata(pFts5, pAux, xDelete) ** ** Save the pointer passed as the second argument as the extension function's ** "auxiliary data". The pointer may then be retrieved by the current or any ** future invocation of the same fts5 extension function made as part of ** the same MATCH query using the xGetAuxdata() API. ** ** Each extension function is allocated a single auxiliary data slot for ** each FTS query (MATCH expression). If the extension function is invoked ** more than once for a single FTS query, then all invocations share a ** single auxiliary data context. ** ** If there is already an auxiliary data pointer when this function is ** invoked, then it is replaced by the new pointer. If an xDelete callback ** was specified along with the original pointer, it is invoked at this ** point. ** ** The xDelete callback, if one is specified, is also invoked on the ** auxiliary data pointer after the FTS5 query has finished. ** ** If an error (e.g. an OOM condition) occurs within this function, ** the auxiliary data is set to NULL and an error code returned. If the ** xDelete parameter was not NULL, it is invoked on the auxiliary data ** pointer before returning. ** ** ** xGetAuxdata(pFts5, bClear) ** ** Returns the current auxiliary data pointer for the fts5 extension ** function. See the xSetAuxdata() method for details. ** ** If the bClear argument is non-zero, then the auxiliary data is cleared ** (set to NULL) before this function returns. In this case the xDelete, ** if any, is not invoked. ** ** ** xRowCount(pFts5, pnRow) ** ** This function is used to retrieve the total number of rows in the table. ** In other words, the same value that would be returned by: ** ** SELECT count(*) FROM ftstable; ** ** xPhraseFirst() ** This function is used, along with type Fts5PhraseIter and the xPhraseNext ** method, to iterate through all instances of a single query phrase within ** the current row. This is the same information as is accessible via the ** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient ** to use, this API may be faster under some circumstances. To iterate ** through instances of phrase iPhrase, use the following code: ** ** Fts5PhraseIter iter; ** int iCol, iOff; ** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff); ** iCol>=0; ** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff) ** ){ ** // An instance of phrase iPhrase at offset iOff of column iCol ** } ** ** The Fts5PhraseIter structure is defined above. Applications should not ** modify this structure directly - it should only be used as shown above ** with the xPhraseFirst() and xPhraseNext() API methods (and by ** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below). ** ** This API can be quite slow if used with an FTS5 table created with the ** "detail=none" or "detail=column" option. If the FTS5 table is created ** with either "detail=none" or "detail=column" and "content=" option ** (i.e. if it is a contentless table), then this API always iterates ** through an empty set (all calls to xPhraseFirst() set iCol to -1). ** ** In all cases, matches are visited in (column ASC, offset ASC) order. ** i.e. all those in column 0, sorted by offset, followed by those in ** column 1, etc. ** ** xPhraseNext() ** See xPhraseFirst above. ** ** xPhraseFirstColumn() ** This function and xPhraseNextColumn() are similar to the xPhraseFirst() ** and xPhraseNext() APIs described above. The difference is that instead ** of iterating through all instances of a phrase in the current row, these ** APIs are used to iterate through the set of columns in the current row ** that contain one or more instances of a specified phrase. For example: ** ** Fts5PhraseIter iter; ** int iCol; ** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol); ** iCol>=0; ** pApi->xPhraseNextColumn(pFts, &iter, &iCol) ** ){ ** // Column iCol contains at least one instance of phrase iPhrase ** } ** ** This API can be quite slow if used with an FTS5 table created with the ** "detail=none" option. If the FTS5 table is created with either ** "detail=none" "content=" option (i.e. if it is a contentless table), ** then this API always iterates through an empty set (all calls to ** xPhraseFirstColumn() set iCol to -1). ** ** The information accessed using this API and its companion ** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext ** (or xInst/xInstCount). The chief advantage of this API is that it is ** significantly more efficient than those alternatives when used with ** "detail=column" tables. ** ** xPhraseNextColumn() ** See xPhraseFirstColumn above. ** ** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken) ** This is used to access token iToken of phrase iPhrase of the current ** query. Before returning, output parameter *ppToken is set to point ** to a buffer containing the requested token, and *pnToken to the ** size of this buffer in bytes. ** ** If iPhrase or iToken are less than zero, or if iPhrase is greater than ** or equal to the number of phrases in the query as reported by ** xPhraseCount(), or if iToken is equal to or greater than the number of ** tokens in the phrase, SQLITE_RANGE is returned and *ppToken and *pnToken are both zeroed. ** ** The output text is not a copy of the query text that specified the ** token. It is the output of the tokenizer module. For tokendata=1 ** tables, this includes any embedded 0x00 and trailing data. ** ** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken) ** This is used to access token iToken of phrase hit iIdx within the ** current row. If iIdx is less than zero or greater than or equal to the ** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise, ** output variable (*ppToken) is set to point to a buffer containing the ** matching document token, and (*pnToken) to the size of that buffer in ** bytes. ** ** The output text is not a copy of the document text that was tokenized. ** It is the output of the tokenizer module. For tokendata=1 tables, this ** includes any embedded 0x00 and trailing data. ** ** This API may be slow in some cases if the token identified by parameters ** iIdx and iToken matched a prefix token in the query. In most cases, the ** first call to this API for each prefix token in the query is forced ** to scan the portion of the full-text index that matches the prefix ** token to collect the extra data required by this API. If the prefix ** token matches a large number of token instances in the document set, ** this may be a performance problem. ** ** If the user knows in advance that a query may use this API for a ** prefix token, FTS5 may be configured to collect all required data as part ** of the initial querying of the full-text index, avoiding the second scan ** entirely. This also causes prefix queries that do not use this API to ** run more slowly and use more memory. FTS5 may be configured in this way ** either on a per-table basis using the [FTS5 insttoken | 'insttoken'] ** option, or on a per-query basis using the ** [fts5_insttoken | fts5_insttoken()] user function. ** ** This API can be quite slow if used with an FTS5 table created with the ** "detail=none" or "detail=column" option. ** ** xColumnLocale(pFts5, iIdx, pzLocale, pnLocale) ** If parameter iCol is less than zero, or greater than or equal to the ** number of columns in the table, SQLITE_RANGE is returned. ** ** Otherwise, this function attempts to retrieve the locale associated ** with column iCol of the current row. Usually, there is no associated ** locale, and output parameters (*pzLocale) and (*pnLocale) are set ** to NULL and 0, respectively. However, if the fts5_locale() function ** was used to associate a locale with the value when it was inserted ** into the fts5 table, then (*pzLocale) is set to point to a nul-terminated ** buffer containing the name of the locale in utf-8 encoding. (*pnLocale) ** is set to the size in bytes of the buffer, not including the ** nul-terminator. ** ** If successful, SQLITE_OK is returned. Or, if an error occurs, an ** SQLite error code is returned. The final value of the output parameters ** is undefined in this case. ** ** xTokenize_v2: ** Tokenize text using the tokenizer belonging to the FTS5 table. This ** API is the same as the xTokenize() API, except that it allows a tokenizer ** locale to be specified. */ struct Fts5ExtensionApi { int iVersion; /* Currently always set to 4 */ void *(*xUserData)(Fts5Context*); int (*xColumnCount)(Fts5Context*); int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow); int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken); int (*xTokenize)(Fts5Context*, const char *pText, int nText, /* Text to tokenize */ void *pCtx, /* Context passed to xToken() */ int (*xToken)(void*, int, const char*, int, int, int) /* Callback */ ); int (*xPhraseCount)(Fts5Context*); int (*xPhraseSize)(Fts5Context*, int iPhrase); int (*xInstCount)(Fts5Context*, int *pnInst); int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff); sqlite3_int64 (*xRowid)(Fts5Context*); int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn); int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken); int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData, int(*)(const Fts5ExtensionApi*,Fts5Context*,void*) ); int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*)); void *(*xGetAuxdata)(Fts5Context*, int bClear); int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*); void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff); int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*); void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol); /* Below this point are iVersion>=3 only */ int (*xQueryToken)(Fts5Context*, int iPhrase, int iToken, const char **ppToken, int *pnToken ); int (*xInstToken)(Fts5Context*, int iIdx, int iToken, const char**, int*); /* Below this point are iVersion>=4 only */ int (*xColumnLocale)(Fts5Context*, int iCol, const char **pz, int *pn); int (*xTokenize_v2)(Fts5Context*, const char *pText, int nText, /* Text to tokenize */ const char *pLocale, int nLocale, /* Locale to pass to tokenizer */ void *pCtx, /* Context passed to xToken() */ int (*xToken)(void*, int, const char*, int, int, int) /* Callback */ ); }; /* ** CUSTOM AUXILIARY FUNCTIONS *************************************************************************/ /************************************************************************* ** CUSTOM TOKENIZERS ** ** Applications may also register custom tokenizer types. A tokenizer ** is registered by providing fts5 with a populated instance of the ** following structure. All structure methods must be defined, setting ** any member of the fts5_tokenizer struct to NULL leads to undefined ** behaviour. The structure methods are expected to function as follows: ** ** xCreate: ** This function is used to allocate and initialize a tokenizer instance. ** A tokenizer instance is required to actually tokenize text. ** ** The first argument passed to this function is a copy of the (void*) ** pointer provided by the application when the fts5_tokenizer_v2 object ** was registered with FTS5 (the third argument to xCreateTokenizer()). ** The second and third arguments are an array of nul-terminated strings ** containing the tokenizer arguments, if any, specified following the ** tokenizer name as part of the CREATE VIRTUAL TABLE statement used ** to create the FTS5 table. ** ** The final argument is an output variable. If successful, (*ppOut) ** should be set to point to the new tokenizer handle and SQLITE_OK ** returned. If an error occurs, some value other than SQLITE_OK should ** be returned. In this case, fts5 assumes that the final value of *ppOut ** is undefined. ** ** xDelete: ** This function is invoked to delete a tokenizer handle previously ** allocated using xCreate(). Fts5 guarantees that this function will ** be invoked exactly once for each successful call to xCreate(). ** ** xTokenize: ** This function is expected to tokenize the nText byte string indicated ** by argument pText. pText may or may not be nul-terminated. The first ** argument passed to this function is a pointer to an Fts5Tokenizer object ** returned by an earlier call to xCreate(). ** ** The third argument indicates the reason that FTS5 is requesting ** tokenization of the supplied text. This is always one of the following ** four values: ** **
  • FTS5_TOKENIZE_DOCUMENT - A document is being inserted into ** or removed from the FTS table. The tokenizer is being invoked to ** determine the set of tokens to add to (or delete from) the ** FTS index. ** **
  • FTS5_TOKENIZE_QUERY - A MATCH query is being executed ** against the FTS index. The tokenizer is being called to tokenize ** a bareword or quoted string specified as part of the query. ** **
  • (FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX) - Same as ** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is ** followed by a "*" character, indicating that the last token ** returned by the tokenizer will be treated as a token prefix. ** **
  • FTS5_TOKENIZE_AUX - The tokenizer is being invoked to ** satisfy an fts5_api.xTokenize() request made by an auxiliary ** function. Or an fts5_api.xColumnSize() request made by the same ** on a columnsize=0 database. **
** ** The sixth and seventh arguments passed to xTokenize() - pLocale and ** nLocale - are a pointer to a buffer containing the locale to use for ** tokenization (e.g. "en_US") and its size in bytes, respectively. The ** pLocale buffer is not nul-terminated. pLocale may be passed NULL (in ** which case nLocale is always 0) to indicate that the tokenizer should ** use its default locale. ** ** For each token in the input string, the supplied callback xToken() must ** be invoked. The first argument to it should be a copy of the pointer ** passed as the second argument to xTokenize(). The third and fourth ** arguments are a pointer to a buffer containing the token text, and the ** size of the token in bytes. The 4th and 5th arguments are the byte offsets ** of the first byte of and first byte immediately following the text from ** which the token is derived within the input. ** ** The second argument passed to the xToken() callback ("tflags") should ** normally be set to 0. The exception is if the tokenizer supports ** synonyms. In this case see the discussion below for details. ** ** FTS5 assumes the xToken() callback is invoked for each token in the ** order that they occur within the input text. ** ** If an xToken() callback returns any value other than SQLITE_OK, then ** the tokenization should be abandoned and the xTokenize() method should ** immediately return a copy of the xToken() return value. Or, if the ** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally, ** if an error occurs with the xTokenize() implementation itself, it ** may abandon the tokenization and return any error code other than ** SQLITE_OK or SQLITE_DONE. ** ** If the tokenizer is registered using an fts5_tokenizer_v2 object, ** then the xTokenize() method has two additional arguments - pLocale ** and nLocale. These specify the locale that the tokenizer should use ** for the current request. If pLocale and nLocale are both 0, then the ** tokenizer should use its default locale. Otherwise, pLocale points to ** an nLocale byte buffer containing the name of the locale to use as utf-8 ** text. pLocale is not nul-terminated. ** ** FTS5_TOKENIZER ** ** There is also an fts5_tokenizer object. This is an older, deprecated, ** version of fts5_tokenizer_v2. It is similar except that: ** **
    **
  • There is no "iVersion" field, and **
  • The xTokenize() method does not take a locale argument. **
** ** Legacy fts5_tokenizer tokenizers must be registered using the ** legacy xCreateTokenizer() function, instead of xCreateTokenizer_v2(). ** ** Tokenizer implementations registered using either API may be retrieved ** using both xFindTokenizer() and xFindTokenizer_v2(). ** ** SYNONYM SUPPORT ** ** Custom tokenizers may also support synonyms. Consider a case in which a ** user wishes to query for a phrase such as "first place". Using the ** built-in tokenizers, the FTS5 query 'first + place' will match instances ** of "first place" within the document set, but not alternative forms ** such as "1st place". In some applications, it would be better to match ** all instances of "first place" or "1st place" regardless of which form ** the user specified in the MATCH query text. ** ** There are several ways to approach this in FTS5: ** **
  1. By mapping all synonyms to a single token. In this case, using ** the above example, this means that the tokenizer returns the ** same token for inputs "first" and "1st". Say that token is in ** fact "first", so that when the user inserts the document "I won ** 1st place" entries are added to the index for tokens "i", "won", ** "first" and "place". If the user then queries for '1st + place', ** the tokenizer substitutes "first" for "1st" and the query works ** as expected. ** **
  2. By querying the index for all synonyms of each query term ** separately. In this case, when tokenizing query text, the ** tokenizer may provide multiple synonyms for a single term ** within the document. FTS5 then queries the index for each ** synonym individually. For example, faced with the query: ** ** ** ... MATCH 'first place' ** ** the tokenizer offers both "1st" and "first" as synonyms for the ** first token in the MATCH query and FTS5 effectively runs a query ** similar to: ** ** ** ... MATCH '(first OR 1st) place' ** ** except that, for the purposes of auxiliary functions, the query ** still appears to contain just two phrases - "(first OR 1st)" ** being treated as a single phrase. ** **
  3. By adding multiple synonyms for a single term to the FTS index. ** Using this method, when tokenizing document text, the tokenizer ** provides multiple synonyms for each token. So that when a ** document such as "I won first place" is tokenized, entries are ** added to the FTS index for "i", "won", "first", "1st" and ** "place". ** ** This way, even if the tokenizer does not provide synonyms ** when tokenizing query text (it should not - to do so would be ** inefficient), it doesn't matter if the user queries for ** 'first + place' or '1st + place', as there are entries in the ** FTS index corresponding to both forms of the first token. **
** ** Whether it is parsing document or query text, any call to xToken that ** specifies a tflags argument with the FTS5_TOKEN_COLOCATED bit ** is considered to supply a synonym for the previous token. For example, ** when parsing the document "I won first place", a tokenizer that supports ** synonyms would call xToken() 5 times, as follows: ** ** ** xToken(pCtx, 0, "i", 1, 0, 1); ** xToken(pCtx, 0, "won", 3, 2, 5); ** xToken(pCtx, 0, "first", 5, 6, 11); ** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11); ** xToken(pCtx, 0, "place", 5, 12, 17); ** ** ** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time ** xToken() is called. Multiple synonyms may be specified for a single token ** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence. ** There is no limit to the number of synonyms that may be provided for a ** single token. ** ** In many cases, method (1) above is the best approach. It does not add ** extra data to the FTS index or require FTS5 to query for multiple terms, ** so it is efficient in terms of disk space and query speed. However, it ** does not support prefix queries very well. If, as suggested above, the ** token "first" is substituted for "1st" by the tokenizer, then the query: ** ** ** ... MATCH '1s*' ** ** will not match documents that contain the token "1st" (as the tokenizer ** will probably not map "1s" to any prefix of "first"). ** ** For full prefix support, method (3) may be preferred. In this case, ** because the index contains entries for both "first" and "1st", prefix ** queries such as 'fi*' or '1s*' will match correctly. However, because ** extra entries are added to the FTS index, this method uses more space ** within the database. ** ** Method (2) offers a midpoint between (1) and (3). Using this method, ** a query such as '1s*' will match documents that contain the literal ** token "1st", but not "first" (assuming the tokenizer is not able to ** provide synonyms for prefixes). However, a non-prefix query like '1st' ** will match against "1st" and "first". This method does not require ** extra disk space, as no extra entries are added to the FTS index. ** On the other hand, it may require more CPU cycles to run MATCH queries, ** as separate queries of the FTS index are required for each synonym. ** ** When using methods (2) or (3), it is important that the tokenizer only ** provide synonyms when tokenizing document text (method (3)) or query ** text (method (2)), not both. Doing so will not cause any errors, but is ** inefficient. */ typedef struct Fts5Tokenizer Fts5Tokenizer; typedef struct fts5_tokenizer_v2 fts5_tokenizer_v2; struct fts5_tokenizer_v2 { int iVersion; /* Currently always 2 */ int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut); void (*xDelete)(Fts5Tokenizer*); int (*xTokenize)(Fts5Tokenizer*, void *pCtx, int flags, /* Mask of FTS5_TOKENIZE_* flags */ const char *pText, int nText, const char *pLocale, int nLocale, int (*xToken)( void *pCtx, /* Copy of 2nd argument to xTokenize() */ int tflags, /* Mask of FTS5_TOKEN_* flags */ const char *pToken, /* Pointer to buffer containing token */ int nToken, /* Size of token in bytes */ int iStart, /* Byte offset of token within input text */ int iEnd /* Byte offset of end of token within input text */ ) ); }; /* ** New code should use the fts5_tokenizer_v2 type to define tokenizer ** implementations. The following type is included for legacy applications ** that still use it. */ typedef struct fts5_tokenizer fts5_tokenizer; struct fts5_tokenizer { int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut); void (*xDelete)(Fts5Tokenizer*); int (*xTokenize)(Fts5Tokenizer*, void *pCtx, int flags, /* Mask of FTS5_TOKENIZE_* flags */ const char *pText, int nText, int (*xToken)( void *pCtx, /* Copy of 2nd argument to xTokenize() */ int tflags, /* Mask of FTS5_TOKEN_* flags */ const char *pToken, /* Pointer to buffer containing token */ int nToken, /* Size of token in bytes */ int iStart, /* Byte offset of token within input text */ int iEnd /* Byte offset of end of token within input text */ ) ); }; /* Flags that may be passed as the third argument to xTokenize() */ #define FTS5_TOKENIZE_QUERY 0x0001 #define FTS5_TOKENIZE_PREFIX 0x0002 #define FTS5_TOKENIZE_DOCUMENT 0x0004 #define FTS5_TOKENIZE_AUX 0x0008 /* Flags that may be passed by the tokenizer implementation back to FTS5 ** as the third argument to the supplied xToken callback. */ #define FTS5_TOKEN_COLOCATED 0x0001 /* Same position as prev. token */ /* ** END OF CUSTOM TOKENIZERS *************************************************************************/ /************************************************************************* ** FTS5 EXTENSION REGISTRATION API */ typedef struct fts5_api fts5_api; struct fts5_api { int iVersion; /* Currently always set to 3 */ /* Create a new tokenizer */ int (*xCreateTokenizer)( fts5_api *pApi, const char *zName, void *pUserData, fts5_tokenizer *pTokenizer, void (*xDestroy)(void*) ); /* Find an existing tokenizer */ int (*xFindTokenizer)( fts5_api *pApi, const char *zName, void **ppUserData, fts5_tokenizer *pTokenizer ); /* Create a new auxiliary function */ int (*xCreateFunction)( fts5_api *pApi, const char *zName, void *pUserData, fts5_extension_function xFunction, void (*xDestroy)(void*) ); /* APIs below this point are only available if iVersion>=3 */ /* Create a new tokenizer */ int (*xCreateTokenizer_v2)( fts5_api *pApi, const char *zName, void *pUserData, fts5_tokenizer_v2 *pTokenizer, void (*xDestroy)(void*) ); /* Find an existing tokenizer */ int (*xFindTokenizer_v2)( fts5_api *pApi, const char *zName, void **ppUserData, fts5_tokenizer_v2 **ppTokenizer ); }; /* ** END OF REGISTRATION API *************************************************************************/ #ifdef __cplusplus } /* end of the 'extern "C"' block */ #endif #endif /* _FTS5_H */ /******** End of fts5.h *********/ #endif /* SQLITE3_H */ ================================================ FILE: src/sqlite3ext.h ================================================ /* ** 2006 June 7 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This header file defines the SQLite interface for use by ** shared libraries that want to be imported as extensions into ** an SQLite instance. Shared libraries that intend to be loaded ** as extensions by SQLite should #include this file instead of ** sqlite3.h. */ #ifndef SQLITE3EXT_H #define SQLITE3EXT_H #include "sqlite3.h" /* ** The following structure holds pointers to all of the SQLite API ** routines. ** ** WARNING: In order to maintain backwards compatibility, add new ** interfaces to the end of this structure only. If you insert new ** interfaces in the middle of this structure, then older different ** versions of SQLite will not be able to load each other's shared ** libraries! */ struct sqlite3_api_routines { void * (*aggregate_context)(sqlite3_context*,int nBytes); int (*aggregate_count)(sqlite3_context*); int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*)); int (*bind_double)(sqlite3_stmt*,int,double); int (*bind_int)(sqlite3_stmt*,int,int); int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64); int (*bind_null)(sqlite3_stmt*,int); int (*bind_parameter_count)(sqlite3_stmt*); int (*bind_parameter_index)(sqlite3_stmt*,const char*zName); const char * (*bind_parameter_name)(sqlite3_stmt*,int); int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*)); int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*)); int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*); int (*busy_handler)(sqlite3*,int(*)(void*,int),void*); int (*busy_timeout)(sqlite3*,int ms); int (*changes)(sqlite3*); int (*close)(sqlite3*); int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*, int eTextRep,const char*)); int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*, int eTextRep,const void*)); const void * (*column_blob)(sqlite3_stmt*,int iCol); int (*column_bytes)(sqlite3_stmt*,int iCol); int (*column_bytes16)(sqlite3_stmt*,int iCol); int (*column_count)(sqlite3_stmt*pStmt); const char * (*column_database_name)(sqlite3_stmt*,int); const void * (*column_database_name16)(sqlite3_stmt*,int); const char * (*column_decltype)(sqlite3_stmt*,int i); const void * (*column_decltype16)(sqlite3_stmt*,int); double (*column_double)(sqlite3_stmt*,int iCol); int (*column_int)(sqlite3_stmt*,int iCol); sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol); const char * (*column_name)(sqlite3_stmt*,int); const void * (*column_name16)(sqlite3_stmt*,int); const char * (*column_origin_name)(sqlite3_stmt*,int); const void * (*column_origin_name16)(sqlite3_stmt*,int); const char * (*column_table_name)(sqlite3_stmt*,int); const void * (*column_table_name16)(sqlite3_stmt*,int); const unsigned char * (*column_text)(sqlite3_stmt*,int iCol); const void * (*column_text16)(sqlite3_stmt*,int iCol); int (*column_type)(sqlite3_stmt*,int iCol); sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol); void * (*commit_hook)(sqlite3*,int(*)(void*),void*); int (*complete)(const char*sql); int (*complete16)(const void*sql); int (*create_collation)(sqlite3*,const char*,int,void*, int(*)(void*,int,const void*,int,const void*)); int (*create_collation16)(sqlite3*,const void*,int,void*, int(*)(void*,int,const void*,int,const void*)); int (*create_function)(sqlite3*,const char*,int,int,void*, void (*xFunc)(sqlite3_context*,int,sqlite3_value**), void (*xStep)(sqlite3_context*,int,sqlite3_value**), void (*xFinal)(sqlite3_context*)); int (*create_function16)(sqlite3*,const void*,int,int,void*, void (*xFunc)(sqlite3_context*,int,sqlite3_value**), void (*xStep)(sqlite3_context*,int,sqlite3_value**), void (*xFinal)(sqlite3_context*)); int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*); int (*data_count)(sqlite3_stmt*pStmt); sqlite3 * (*db_handle)(sqlite3_stmt*); int (*declare_vtab)(sqlite3*,const char*); int (*enable_shared_cache)(int); int (*errcode)(sqlite3*db); const char * (*errmsg)(sqlite3*); const void * (*errmsg16)(sqlite3*); int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**); int (*expired)(sqlite3_stmt*); int (*finalize)(sqlite3_stmt*pStmt); void (*free)(void*); void (*free_table)(char**result); int (*get_autocommit)(sqlite3*); void * (*get_auxdata)(sqlite3_context*,int); int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**); int (*global_recover)(void); void (*interruptx)(sqlite3*); sqlite_int64 (*last_insert_rowid)(sqlite3*); const char * (*libversion)(void); int (*libversion_number)(void); void *(*malloc)(int); char * (*mprintf)(const char*,...); int (*open)(const char*,sqlite3**); int (*open16)(const void*,sqlite3**); int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**); int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**); void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*); void (*progress_handler)(sqlite3*,int,int(*)(void*),void*); void *(*realloc)(void*,int); int (*reset)(sqlite3_stmt*pStmt); void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*)); void (*result_double)(sqlite3_context*,double); void (*result_error)(sqlite3_context*,const char*,int); void (*result_error16)(sqlite3_context*,const void*,int); void (*result_int)(sqlite3_context*,int); void (*result_int64)(sqlite3_context*,sqlite_int64); void (*result_null)(sqlite3_context*); void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*)); void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*)); void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*)); void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*)); void (*result_value)(sqlite3_context*,sqlite3_value*); void * (*rollback_hook)(sqlite3*,void(*)(void*),void*); int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*, const char*,const char*),void*); void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*)); char * (*xsnprintf)(int,char*,const char*,...); int (*step)(sqlite3_stmt*); int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*, char const**,char const**,int*,int*,int*); void (*thread_cleanup)(void); int (*total_changes)(sqlite3*); void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*); int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*); void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*, sqlite_int64),void*); void * (*user_data)(sqlite3_context*); const void * (*value_blob)(sqlite3_value*); int (*value_bytes)(sqlite3_value*); int (*value_bytes16)(sqlite3_value*); double (*value_double)(sqlite3_value*); int (*value_int)(sqlite3_value*); sqlite_int64 (*value_int64)(sqlite3_value*); int (*value_numeric_type)(sqlite3_value*); const unsigned char * (*value_text)(sqlite3_value*); const void * (*value_text16)(sqlite3_value*); const void * (*value_text16be)(sqlite3_value*); const void * (*value_text16le)(sqlite3_value*); int (*value_type)(sqlite3_value*); char *(*vmprintf)(const char*,va_list); /* Added ??? */ int (*overload_function)(sqlite3*, const char *zFuncName, int nArg); /* Added by 3.3.13 */ int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**); int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**); int (*clear_bindings)(sqlite3_stmt*); /* Added by 3.4.1 */ int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*, void (*xDestroy)(void *)); /* Added by 3.5.0 */ int (*bind_zeroblob)(sqlite3_stmt*,int,int); int (*blob_bytes)(sqlite3_blob*); int (*blob_close)(sqlite3_blob*); int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64, int,sqlite3_blob**); int (*blob_read)(sqlite3_blob*,void*,int,int); int (*blob_write)(sqlite3_blob*,const void*,int,int); int (*create_collation_v2)(sqlite3*,const char*,int,void*, int(*)(void*,int,const void*,int,const void*), void(*)(void*)); int (*file_control)(sqlite3*,const char*,int,void*); sqlite3_int64 (*memory_highwater)(int); sqlite3_int64 (*memory_used)(void); sqlite3_mutex *(*mutex_alloc)(int); void (*mutex_enter)(sqlite3_mutex*); void (*mutex_free)(sqlite3_mutex*); void (*mutex_leave)(sqlite3_mutex*); int (*mutex_try)(sqlite3_mutex*); int (*open_v2)(const char*,sqlite3**,int,const char*); int (*release_memory)(int); void (*result_error_nomem)(sqlite3_context*); void (*result_error_toobig)(sqlite3_context*); int (*sleep)(int); void (*soft_heap_limit)(int); sqlite3_vfs *(*vfs_find)(const char*); int (*vfs_register)(sqlite3_vfs*,int); int (*vfs_unregister)(sqlite3_vfs*); int (*xthreadsafe)(void); void (*result_zeroblob)(sqlite3_context*,int); void (*result_error_code)(sqlite3_context*,int); int (*test_control)(int, ...); void (*randomness)(int,void*); sqlite3 *(*context_db_handle)(sqlite3_context*); int (*extended_result_codes)(sqlite3*,int); int (*limit)(sqlite3*,int,int); sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*); const char *(*sql)(sqlite3_stmt*); int (*status)(int,int*,int*,int); int (*backup_finish)(sqlite3_backup*); sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*); int (*backup_pagecount)(sqlite3_backup*); int (*backup_remaining)(sqlite3_backup*); int (*backup_step)(sqlite3_backup*,int); const char *(*compileoption_get)(int); int (*compileoption_used)(const char*); int (*create_function_v2)(sqlite3*,const char*,int,int,void*, void (*xFunc)(sqlite3_context*,int,sqlite3_value**), void (*xStep)(sqlite3_context*,int,sqlite3_value**), void (*xFinal)(sqlite3_context*), void(*xDestroy)(void*)); int (*db_config)(sqlite3*,int,...); sqlite3_mutex *(*db_mutex)(sqlite3*); int (*db_status)(sqlite3*,int,int*,int*,int); int (*extended_errcode)(sqlite3*); void (*log)(int,const char*,...); sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64); const char *(*sourceid)(void); int (*stmt_status)(sqlite3_stmt*,int,int); int (*strnicmp)(const char*,const char*,int); int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*); int (*wal_autocheckpoint)(sqlite3*,int); int (*wal_checkpoint)(sqlite3*,const char*); void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*); int (*blob_reopen)(sqlite3_blob*,sqlite3_int64); int (*vtab_config)(sqlite3*,int op,...); int (*vtab_on_conflict)(sqlite3*); /* Version 3.7.16 and later */ int (*close_v2)(sqlite3*); const char *(*db_filename)(sqlite3*,const char*); int (*db_readonly)(sqlite3*,const char*); int (*db_release_memory)(sqlite3*); const char *(*errstr)(int); int (*stmt_busy)(sqlite3_stmt*); int (*stmt_readonly)(sqlite3_stmt*); int (*stricmp)(const char*,const char*); int (*uri_boolean)(const char*,const char*,int); sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64); const char *(*uri_parameter)(const char*,const char*); char *(*xvsnprintf)(int,char*,const char*,va_list); int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*); /* Version 3.8.7 and later */ int (*auto_extension)(void(*)(void)); int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64, void(*)(void*)); int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64, void(*)(void*),unsigned char); int (*cancel_auto_extension)(void(*)(void)); int (*load_extension)(sqlite3*,const char*,const char*,char**); void *(*malloc64)(sqlite3_uint64); sqlite3_uint64 (*msize)(void*); void *(*realloc64)(void*,sqlite3_uint64); void (*reset_auto_extension)(void); void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64, void(*)(void*)); void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64, void(*)(void*), unsigned char); int (*strglob)(const char*,const char*); /* Version 3.8.11 and later */ sqlite3_value *(*value_dup)(const sqlite3_value*); void (*value_free)(sqlite3_value*); int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64); int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64); /* Version 3.9.0 and later */ unsigned int (*value_subtype)(sqlite3_value*); void (*result_subtype)(sqlite3_context*,unsigned int); /* Version 3.10.0 and later */ int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int); int (*strlike)(const char*,const char*,unsigned int); int (*db_cacheflush)(sqlite3*); /* Version 3.12.0 and later */ int (*system_errno)(sqlite3*); /* Version 3.14.0 and later */ int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*); char *(*expanded_sql)(sqlite3_stmt*); /* Version 3.18.0 and later */ void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64); /* Version 3.20.0 and later */ int (*prepare_v3)(sqlite3*,const char*,int,unsigned int, sqlite3_stmt**,const char**); int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int, sqlite3_stmt**,const void**); int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*)); void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*)); void *(*value_pointer)(sqlite3_value*,const char*); int (*vtab_nochange)(sqlite3_context*); int (*value_nochange)(sqlite3_value*); const char *(*vtab_collation)(sqlite3_index_info*,int); /* Version 3.24.0 and later */ int (*keyword_count)(void); int (*keyword_name)(int,const char**,int*); int (*keyword_check)(const char*,int); sqlite3_str *(*str_new)(sqlite3*); char *(*str_finish)(sqlite3_str*); void (*str_appendf)(sqlite3_str*, const char *zFormat, ...); void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list); void (*str_append)(sqlite3_str*, const char *zIn, int N); void (*str_appendall)(sqlite3_str*, const char *zIn); void (*str_appendchar)(sqlite3_str*, int N, char C); void (*str_reset)(sqlite3_str*); int (*str_errcode)(sqlite3_str*); int (*str_length)(sqlite3_str*); char *(*str_value)(sqlite3_str*); /* Version 3.25.0 and later */ int (*create_window_function)(sqlite3*,const char*,int,int,void*, void (*xStep)(sqlite3_context*,int,sqlite3_value**), void (*xFinal)(sqlite3_context*), void (*xValue)(sqlite3_context*), void (*xInv)(sqlite3_context*,int,sqlite3_value**), void(*xDestroy)(void*)); /* Version 3.26.0 and later */ const char *(*normalized_sql)(sqlite3_stmt*); /* Version 3.28.0 and later */ int (*stmt_isexplain)(sqlite3_stmt*); int (*value_frombind)(sqlite3_value*); /* Version 3.30.0 and later */ int (*drop_modules)(sqlite3*,const char**); /* Version 3.31.0 and later */ sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64); const char *(*uri_key)(const char*,int); const char *(*filename_database)(const char*); const char *(*filename_journal)(const char*); const char *(*filename_wal)(const char*); /* Version 3.32.0 and later */ const char *(*create_filename)(const char*,const char*,const char*, int,const char**); void (*free_filename)(const char*); sqlite3_file *(*database_file_object)(const char*); /* Version 3.34.0 and later */ int (*txn_state)(sqlite3*,const char*); /* Version 3.36.1 and later */ sqlite3_int64 (*changes64)(sqlite3*); sqlite3_int64 (*total_changes64)(sqlite3*); /* Version 3.37.0 and later */ int (*autovacuum_pages)(sqlite3*, unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int), void*, void(*)(void*)); /* Version 3.38.0 and later */ int (*error_offset)(sqlite3*); int (*vtab_rhs_value)(sqlite3_index_info*,int,sqlite3_value**); int (*vtab_distinct)(sqlite3_index_info*); int (*vtab_in)(sqlite3_index_info*,int,int); int (*vtab_in_first)(sqlite3_value*,sqlite3_value**); int (*vtab_in_next)(sqlite3_value*,sqlite3_value**); /* Version 3.39.0 and later */ int (*deserialize)(sqlite3*,const char*,unsigned char*, sqlite3_int64,sqlite3_int64,unsigned); unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*, unsigned int); const char *(*db_name)(sqlite3*,int); /* Version 3.40.0 and later */ int (*value_encoding)(sqlite3_value*); /* Version 3.41.0 and later */ int (*is_interrupted)(sqlite3*); /* Version 3.43.0 and later */ int (*stmt_explain)(sqlite3_stmt*,int); /* Version 3.44.0 and later */ void *(*get_clientdata)(sqlite3*,const char*); int (*set_clientdata)(sqlite3*, const char*, void*, void(*)(void*)); /* Version 3.50.0 and later */ int (*setlk_timeout)(sqlite3*,int,int); }; /* ** This is the function signature used for all extension entry points. It ** is also defined in the file "loadext.c". */ typedef int (*sqlite3_loadext_entry)( sqlite3 *db, /* Handle to the database. */ char **pzErrMsg, /* Used to set error string on failure. */ const sqlite3_api_routines *pThunk /* Extension API function pointers. */ ); /* ** The following macros redefine the API routines so that they are ** redirected through the global sqlite3_api structure. ** ** This header file is also used by the loadext.c source file ** (part of the main SQLite library - not an extension) so that ** it can get access to the sqlite3_api_routines structure ** definition. But the main library does not want to redefine ** the API. So the redefinition macros are only valid if the ** SQLITE_CORE macros is undefined. */ #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) #define sqlite3_aggregate_context sqlite3_api->aggregate_context #ifndef SQLITE_OMIT_DEPRECATED #define sqlite3_aggregate_count sqlite3_api->aggregate_count #endif #define sqlite3_bind_blob sqlite3_api->bind_blob #define sqlite3_bind_double sqlite3_api->bind_double #define sqlite3_bind_int sqlite3_api->bind_int #define sqlite3_bind_int64 sqlite3_api->bind_int64 #define sqlite3_bind_null sqlite3_api->bind_null #define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count #define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index #define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name #define sqlite3_bind_text sqlite3_api->bind_text #define sqlite3_bind_text16 sqlite3_api->bind_text16 #define sqlite3_bind_value sqlite3_api->bind_value #define sqlite3_busy_handler sqlite3_api->busy_handler #define sqlite3_busy_timeout sqlite3_api->busy_timeout #define sqlite3_changes sqlite3_api->changes #define sqlite3_close sqlite3_api->close #define sqlite3_collation_needed sqlite3_api->collation_needed #define sqlite3_collation_needed16 sqlite3_api->collation_needed16 #define sqlite3_column_blob sqlite3_api->column_blob #define sqlite3_column_bytes sqlite3_api->column_bytes #define sqlite3_column_bytes16 sqlite3_api->column_bytes16 #define sqlite3_column_count sqlite3_api->column_count #define sqlite3_column_database_name sqlite3_api->column_database_name #define sqlite3_column_database_name16 sqlite3_api->column_database_name16 #define sqlite3_column_decltype sqlite3_api->column_decltype #define sqlite3_column_decltype16 sqlite3_api->column_decltype16 #define sqlite3_column_double sqlite3_api->column_double #define sqlite3_column_int sqlite3_api->column_int #define sqlite3_column_int64 sqlite3_api->column_int64 #define sqlite3_column_name sqlite3_api->column_name #define sqlite3_column_name16 sqlite3_api->column_name16 #define sqlite3_column_origin_name sqlite3_api->column_origin_name #define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16 #define sqlite3_column_table_name sqlite3_api->column_table_name #define sqlite3_column_table_name16 sqlite3_api->column_table_name16 #define sqlite3_column_text sqlite3_api->column_text #define sqlite3_column_text16 sqlite3_api->column_text16 #define sqlite3_column_type sqlite3_api->column_type #define sqlite3_column_value sqlite3_api->column_value #define sqlite3_commit_hook sqlite3_api->commit_hook #define sqlite3_complete sqlite3_api->complete #define sqlite3_complete16 sqlite3_api->complete16 #define sqlite3_create_collation sqlite3_api->create_collation #define sqlite3_create_collation16 sqlite3_api->create_collation16 #define sqlite3_create_function sqlite3_api->create_function #define sqlite3_create_function16 sqlite3_api->create_function16 #define sqlite3_create_module sqlite3_api->create_module #define sqlite3_create_module_v2 sqlite3_api->create_module_v2 #define sqlite3_data_count sqlite3_api->data_count #define sqlite3_db_handle sqlite3_api->db_handle #define sqlite3_declare_vtab sqlite3_api->declare_vtab #define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache #define sqlite3_errcode sqlite3_api->errcode #define sqlite3_errmsg sqlite3_api->errmsg #define sqlite3_errmsg16 sqlite3_api->errmsg16 #define sqlite3_exec sqlite3_api->exec #ifndef SQLITE_OMIT_DEPRECATED #define sqlite3_expired sqlite3_api->expired #endif #define sqlite3_finalize sqlite3_api->finalize #define sqlite3_free sqlite3_api->free #define sqlite3_free_table sqlite3_api->free_table #define sqlite3_get_autocommit sqlite3_api->get_autocommit #define sqlite3_get_auxdata sqlite3_api->get_auxdata #define sqlite3_get_table sqlite3_api->get_table #ifndef SQLITE_OMIT_DEPRECATED #define sqlite3_global_recover sqlite3_api->global_recover #endif #define sqlite3_interrupt sqlite3_api->interruptx #define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid #define sqlite3_libversion sqlite3_api->libversion #define sqlite3_libversion_number sqlite3_api->libversion_number #define sqlite3_malloc sqlite3_api->malloc #define sqlite3_mprintf sqlite3_api->mprintf #define sqlite3_open sqlite3_api->open #define sqlite3_open16 sqlite3_api->open16 #define sqlite3_prepare sqlite3_api->prepare #define sqlite3_prepare16 sqlite3_api->prepare16 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2 #define sqlite3_profile sqlite3_api->profile #define sqlite3_progress_handler sqlite3_api->progress_handler #define sqlite3_realloc sqlite3_api->realloc #define sqlite3_reset sqlite3_api->reset #define sqlite3_result_blob sqlite3_api->result_blob #define sqlite3_result_double sqlite3_api->result_double #define sqlite3_result_error sqlite3_api->result_error #define sqlite3_result_error16 sqlite3_api->result_error16 #define sqlite3_result_int sqlite3_api->result_int #define sqlite3_result_int64 sqlite3_api->result_int64 #define sqlite3_result_null sqlite3_api->result_null #define sqlite3_result_text sqlite3_api->result_text #define sqlite3_result_text16 sqlite3_api->result_text16 #define sqlite3_result_text16be sqlite3_api->result_text16be #define sqlite3_result_text16le sqlite3_api->result_text16le #define sqlite3_result_value sqlite3_api->result_value #define sqlite3_rollback_hook sqlite3_api->rollback_hook #define sqlite3_set_authorizer sqlite3_api->set_authorizer #define sqlite3_set_auxdata sqlite3_api->set_auxdata #define sqlite3_snprintf sqlite3_api->xsnprintf #define sqlite3_step sqlite3_api->step #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup #define sqlite3_total_changes sqlite3_api->total_changes #define sqlite3_trace sqlite3_api->trace #ifndef SQLITE_OMIT_DEPRECATED #define sqlite3_transfer_bindings sqlite3_api->transfer_bindings #endif #define sqlite3_update_hook sqlite3_api->update_hook #define sqlite3_user_data sqlite3_api->user_data #define sqlite3_value_blob sqlite3_api->value_blob #define sqlite3_value_bytes sqlite3_api->value_bytes #define sqlite3_value_bytes16 sqlite3_api->value_bytes16 #define sqlite3_value_double sqlite3_api->value_double #define sqlite3_value_int sqlite3_api->value_int #define sqlite3_value_int64 sqlite3_api->value_int64 #define sqlite3_value_numeric_type sqlite3_api->value_numeric_type #define sqlite3_value_text sqlite3_api->value_text #define sqlite3_value_text16 sqlite3_api->value_text16 #define sqlite3_value_text16be sqlite3_api->value_text16be #define sqlite3_value_text16le sqlite3_api->value_text16le #define sqlite3_value_type sqlite3_api->value_type #define sqlite3_vmprintf sqlite3_api->vmprintf #define sqlite3_vsnprintf sqlite3_api->xvsnprintf #define sqlite3_overload_function sqlite3_api->overload_function #define sqlite3_prepare_v2 sqlite3_api->prepare_v2 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2 #define sqlite3_clear_bindings sqlite3_api->clear_bindings #define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob #define sqlite3_blob_bytes sqlite3_api->blob_bytes #define sqlite3_blob_close sqlite3_api->blob_close #define sqlite3_blob_open sqlite3_api->blob_open #define sqlite3_blob_read sqlite3_api->blob_read #define sqlite3_blob_write sqlite3_api->blob_write #define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2 #define sqlite3_file_control sqlite3_api->file_control #define sqlite3_memory_highwater sqlite3_api->memory_highwater #define sqlite3_memory_used sqlite3_api->memory_used #define sqlite3_mutex_alloc sqlite3_api->mutex_alloc #define sqlite3_mutex_enter sqlite3_api->mutex_enter #define sqlite3_mutex_free sqlite3_api->mutex_free #define sqlite3_mutex_leave sqlite3_api->mutex_leave #define sqlite3_mutex_try sqlite3_api->mutex_try #define sqlite3_open_v2 sqlite3_api->open_v2 #define sqlite3_release_memory sqlite3_api->release_memory #define sqlite3_result_error_nomem sqlite3_api->result_error_nomem #define sqlite3_result_error_toobig sqlite3_api->result_error_toobig #define sqlite3_sleep sqlite3_api->sleep #define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit #define sqlite3_vfs_find sqlite3_api->vfs_find #define sqlite3_vfs_register sqlite3_api->vfs_register #define sqlite3_vfs_unregister sqlite3_api->vfs_unregister #define sqlite3_threadsafe sqlite3_api->xthreadsafe #define sqlite3_result_zeroblob sqlite3_api->result_zeroblob #define sqlite3_result_error_code sqlite3_api->result_error_code #define sqlite3_test_control sqlite3_api->test_control #define sqlite3_randomness sqlite3_api->randomness #define sqlite3_context_db_handle sqlite3_api->context_db_handle #define sqlite3_extended_result_codes sqlite3_api->extended_result_codes #define sqlite3_limit sqlite3_api->limit #define sqlite3_next_stmt sqlite3_api->next_stmt #define sqlite3_sql sqlite3_api->sql #define sqlite3_status sqlite3_api->status #define sqlite3_backup_finish sqlite3_api->backup_finish #define sqlite3_backup_init sqlite3_api->backup_init #define sqlite3_backup_pagecount sqlite3_api->backup_pagecount #define sqlite3_backup_remaining sqlite3_api->backup_remaining #define sqlite3_backup_step sqlite3_api->backup_step #define sqlite3_compileoption_get sqlite3_api->compileoption_get #define sqlite3_compileoption_used sqlite3_api->compileoption_used #define sqlite3_create_function_v2 sqlite3_api->create_function_v2 #define sqlite3_db_config sqlite3_api->db_config #define sqlite3_db_mutex sqlite3_api->db_mutex #define sqlite3_db_status sqlite3_api->db_status #define sqlite3_extended_errcode sqlite3_api->extended_errcode #define sqlite3_log sqlite3_api->log #define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64 #define sqlite3_sourceid sqlite3_api->sourceid #define sqlite3_stmt_status sqlite3_api->stmt_status #define sqlite3_strnicmp sqlite3_api->strnicmp #define sqlite3_unlock_notify sqlite3_api->unlock_notify #define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint #define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint #define sqlite3_wal_hook sqlite3_api->wal_hook #define sqlite3_blob_reopen sqlite3_api->blob_reopen #define sqlite3_vtab_config sqlite3_api->vtab_config #define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict /* Version 3.7.16 and later */ #define sqlite3_close_v2 sqlite3_api->close_v2 #define sqlite3_db_filename sqlite3_api->db_filename #define sqlite3_db_readonly sqlite3_api->db_readonly #define sqlite3_db_release_memory sqlite3_api->db_release_memory #define sqlite3_errstr sqlite3_api->errstr #define sqlite3_stmt_busy sqlite3_api->stmt_busy #define sqlite3_stmt_readonly sqlite3_api->stmt_readonly #define sqlite3_stricmp sqlite3_api->stricmp #define sqlite3_uri_boolean sqlite3_api->uri_boolean #define sqlite3_uri_int64 sqlite3_api->uri_int64 #define sqlite3_uri_parameter sqlite3_api->uri_parameter #define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2 /* Version 3.8.7 and later */ #define sqlite3_auto_extension sqlite3_api->auto_extension #define sqlite3_bind_blob64 sqlite3_api->bind_blob64 #define sqlite3_bind_text64 sqlite3_api->bind_text64 #define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension #define sqlite3_load_extension sqlite3_api->load_extension #define sqlite3_malloc64 sqlite3_api->malloc64 #define sqlite3_msize sqlite3_api->msize #define sqlite3_realloc64 sqlite3_api->realloc64 #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension #define sqlite3_result_blob64 sqlite3_api->result_blob64 #define sqlite3_result_text64 sqlite3_api->result_text64 #define sqlite3_strglob sqlite3_api->strglob /* Version 3.8.11 and later */ #define sqlite3_value_dup sqlite3_api->value_dup #define sqlite3_value_free sqlite3_api->value_free #define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64 #define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64 /* Version 3.9.0 and later */ #define sqlite3_value_subtype sqlite3_api->value_subtype #define sqlite3_result_subtype sqlite3_api->result_subtype /* Version 3.10.0 and later */ #define sqlite3_status64 sqlite3_api->status64 #define sqlite3_strlike sqlite3_api->strlike #define sqlite3_db_cacheflush sqlite3_api->db_cacheflush /* Version 3.12.0 and later */ #define sqlite3_system_errno sqlite3_api->system_errno /* Version 3.14.0 and later */ #define sqlite3_trace_v2 sqlite3_api->trace_v2 #define sqlite3_expanded_sql sqlite3_api->expanded_sql /* Version 3.18.0 and later */ #define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid /* Version 3.20.0 and later */ #define sqlite3_prepare_v3 sqlite3_api->prepare_v3 #define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3 #define sqlite3_bind_pointer sqlite3_api->bind_pointer #define sqlite3_result_pointer sqlite3_api->result_pointer #define sqlite3_value_pointer sqlite3_api->value_pointer /* Version 3.22.0 and later */ #define sqlite3_vtab_nochange sqlite3_api->vtab_nochange #define sqlite3_value_nochange sqlite3_api->value_nochange #define sqlite3_vtab_collation sqlite3_api->vtab_collation /* Version 3.24.0 and later */ #define sqlite3_keyword_count sqlite3_api->keyword_count #define sqlite3_keyword_name sqlite3_api->keyword_name #define sqlite3_keyword_check sqlite3_api->keyword_check #define sqlite3_str_new sqlite3_api->str_new #define sqlite3_str_finish sqlite3_api->str_finish #define sqlite3_str_appendf sqlite3_api->str_appendf #define sqlite3_str_vappendf sqlite3_api->str_vappendf #define sqlite3_str_append sqlite3_api->str_append #define sqlite3_str_appendall sqlite3_api->str_appendall #define sqlite3_str_appendchar sqlite3_api->str_appendchar #define sqlite3_str_reset sqlite3_api->str_reset #define sqlite3_str_errcode sqlite3_api->str_errcode #define sqlite3_str_length sqlite3_api->str_length #define sqlite3_str_value sqlite3_api->str_value /* Version 3.25.0 and later */ #define sqlite3_create_window_function sqlite3_api->create_window_function /* Version 3.26.0 and later */ #define sqlite3_normalized_sql sqlite3_api->normalized_sql /* Version 3.28.0 and later */ #define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain #define sqlite3_value_frombind sqlite3_api->value_frombind /* Version 3.30.0 and later */ #define sqlite3_drop_modules sqlite3_api->drop_modules /* Version 3.31.0 and later */ #define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64 #define sqlite3_uri_key sqlite3_api->uri_key #define sqlite3_filename_database sqlite3_api->filename_database #define sqlite3_filename_journal sqlite3_api->filename_journal #define sqlite3_filename_wal sqlite3_api->filename_wal /* Version 3.32.0 and later */ #define sqlite3_create_filename sqlite3_api->create_filename #define sqlite3_free_filename sqlite3_api->free_filename #define sqlite3_database_file_object sqlite3_api->database_file_object /* Version 3.34.0 and later */ #define sqlite3_txn_state sqlite3_api->txn_state /* Version 3.36.1 and later */ #define sqlite3_changes64 sqlite3_api->changes64 #define sqlite3_total_changes64 sqlite3_api->total_changes64 /* Version 3.37.0 and later */ #define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages /* Version 3.38.0 and later */ #define sqlite3_error_offset sqlite3_api->error_offset #define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value #define sqlite3_vtab_distinct sqlite3_api->vtab_distinct #define sqlite3_vtab_in sqlite3_api->vtab_in #define sqlite3_vtab_in_first sqlite3_api->vtab_in_first #define sqlite3_vtab_in_next sqlite3_api->vtab_in_next /* Version 3.39.0 and later */ #ifndef SQLITE_OMIT_DESERIALIZE #define sqlite3_deserialize sqlite3_api->deserialize #define sqlite3_serialize sqlite3_api->serialize #endif #define sqlite3_db_name sqlite3_api->db_name /* Version 3.40.0 and later */ #define sqlite3_value_encoding sqlite3_api->value_encoding /* Version 3.41.0 and later */ #define sqlite3_is_interrupted sqlite3_api->is_interrupted /* Version 3.43.0 and later */ #define sqlite3_stmt_explain sqlite3_api->stmt_explain /* Version 3.44.0 and later */ #define sqlite3_get_clientdata sqlite3_api->get_clientdata #define sqlite3_set_clientdata sqlite3_api->set_clientdata /* Version 3.50.0 and later */ #define sqlite3_setlk_timeout sqlite3_api->setlk_timeout #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */ #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) /* This case when the file really is being compiled as a loadable ** extension */ # define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0; # define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v; # define SQLITE_EXTENSION_INIT3 \ extern const sqlite3_api_routines *sqlite3_api; #else /* This case when the file is being statically linked into the ** application */ # define SQLITE_EXTENSION_INIT1 /*no-op*/ # define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */ # define SQLITE_EXTENSION_INIT3 /*no-op*/ #endif #endif /* SQLITE3EXT_H */ ================================================ FILE: src/sqlite3vfs.h ================================================ #ifndef SQLITE3VFS_H #define SQLITE3VFS_H #ifdef SQLITE3VFS_LOADABLE_EXT #include "sqlite3ext.h" #else #include "sqlite3-binding.h" #endif typedef struct s3vfsFile { sqlite3_file base; /* IO methods */ sqlite3_uint64 id; /* Go object id */ } s3vfsFile; int s3vfsNew(char* name, int maxPathName); int s3vfsClose(sqlite3_file*); int s3vfsRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); int s3vfsWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst); int s3vfsTruncate(sqlite3_file*, sqlite3_int64 size); int s3vfsSync(sqlite3_file*, int flags); int s3vfsFileSize(sqlite3_file*, sqlite3_int64 *pSize); int s3vfsLock(sqlite3_file*, int); int s3vfsUnlock(sqlite3_file*, int); int s3vfsCheckReservedLock(sqlite3_file*, int *pResOut); int s3vfsFileControl(sqlite3_file*, int op, void *pArg); int s3vfsSectorSize(sqlite3_file*); int s3vfsDeviceCharacteristics(sqlite3_file*); int s3vfsShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**); int s3vfsShmLock(sqlite3_file*, int offset, int n, int flags); void s3vfsShmBarrier(sqlite3_file*); int s3vfsShmUnmap(sqlite3_file*, int deleteFlag); int s3vfsFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp); int s3vfsUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p); int s3vfsOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); int s3vfsDelete(sqlite3_vfs*, const char *, int); int s3vfsAccess(sqlite3_vfs*, const char *, int, int *); int s3vfsFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut); void *s3vfsDlOpen(sqlite3_vfs*, const char *zFilename); void s3vfsDlError(sqlite3_vfs*, int nByte, char *zErrMsg); void (*s3vfsDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void); void s3vfsDlClose(sqlite3_vfs*, void*); int s3vfsRandomness(sqlite3_vfs*, int nByte, char *zOut); int s3vfsSleep(sqlite3_vfs*, int microseconds); int s3vfsCurrentTime(sqlite3_vfs*, double*); int s3vfsGetLastError(sqlite3_vfs*, int, char *); int s3vfsCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); const extern sqlite3_io_methods s3vfs_io_methods; #endif /* SQLITE3_VFS */ ================================================ FILE: store.go ================================================ package litestream import ( "context" "errors" "fmt" "log/slog" "path/filepath" "slices" "sync" "time" "github.com/superfly/ltx" "golang.org/x/sync/errgroup" ) var ( // ErrNoCompaction is returned when no new files are available from the previous level. ErrNoCompaction = errors.New("no compaction") // ErrCompactionTooEarly is returned when a compaction is attempted too soon // since the last compaction time. This is used to prevent frequent // re-compaction when restarting the process. ErrCompactionTooEarly = errors.New("compaction too early") // ErrTxNotAvailable is returned when a transaction does not exist. ErrTxNotAvailable = errors.New("transaction not available") // ErrDBNotReady is a sentinel for errors.Is() compatibility. ErrDBNotReady = &DBNotReadyError{} // ErrShutdownInterrupted is returned when the shutdown sync retry loop // is interrupted by a done channel signal (e.g., second Ctrl+C). ErrShutdownInterrupted = errors.New("shutdown sync interrupted") ErrDatabaseNotFound = errors.New("database not found") ErrDatabaseNotOpen = errors.New("database not open") ) // DBNotReadyError is returned when an operation is attempted before the // database has been initialized (e.g., page size not yet known). type DBNotReadyError struct { Reason string } func (e *DBNotReadyError) Error() string { if e.Reason != "" { return "db not ready: " + e.Reason } return "db not ready" } func (e *DBNotReadyError) Is(target error) bool { _, ok := target.(*DBNotReadyError) return ok } // Store defaults const ( DefaultSnapshotInterval = 24 * time.Hour DefaultSnapshotRetention = 24 * time.Hour DefaultRetention = 24 * time.Hour DefaultRetentionCheckInterval = 1 * time.Hour // DefaultL0Retention is the default time that L0 files are kept around // after they have been compacted into L1 files. DefaultL0Retention = 5 * time.Minute // DefaultL0RetentionCheckInterval controls how frequently L0 retention is // enforced. This interval should be more frequent than the L1 compaction // interval so that VFS read replicas have time to observe new files. DefaultL0RetentionCheckInterval = 15 * time.Second // DefaultHeartbeatCheckInterval controls how frequently the heartbeat // monitor checks if heartbeat pings should be sent. DefaultHeartbeatCheckInterval = 15 * time.Second // DefaultDBInitTimeout is the maximum time to wait for a database to be // initialized (page size known) before logging a warning. DefaultDBInitTimeout = 30 * time.Second ) // Store represents the top-level container for databases. // // It manages async background tasks like compactions so that the system // is not overloaded by too many concurrent tasks. type Store struct { mu sync.Mutex dbs []*DB levels CompactionLevels wg sync.WaitGroup ctx context.Context cancel func() done <-chan struct{} // The frequency of snapshots. SnapshotInterval time.Duration // The duration of time that snapshots are kept before being deleted. SnapshotRetention time.Duration // The duration that L0 files are kept after being compacted into L1. L0Retention time.Duration // How often to check for expired L0 files. L0RetentionCheckInterval time.Duration // If true, compaction is run in the background according to compaction levels. CompactionMonitorEnabled bool // If true, verify TXID consistency at destination level after each compaction. VerifyCompaction bool // RetentionEnabled controls whether Litestream actively deletes old files // during retention enforcement. When false, cloud provider lifecycle // policies handle retention instead. Local file cleanup still occurs. RetentionEnabled bool // Shutdown sync retry settings. ShutdownSyncTimeout time.Duration ShutdownSyncInterval time.Duration // How often to check if heartbeat pings should be sent. HeartbeatCheckInterval time.Duration // Heartbeat client for health check pings. Sends pings only when // all databases have synced successfully within the heartbeat interval. Heartbeat *HeartbeatClient // heartbeatMonitorRunning tracks whether the heartbeat monitor goroutine is running. heartbeatMonitorRunning bool // How often to run validation checks. Zero disables periodic validation. ValidationInterval time.Duration Logger *slog.Logger } func NewStore(dbs []*DB, levels CompactionLevels) *Store { s := &Store{ dbs: dbs, levels: levels, SnapshotInterval: DefaultSnapshotInterval, SnapshotRetention: DefaultSnapshotRetention, L0Retention: DefaultL0Retention, L0RetentionCheckInterval: DefaultL0RetentionCheckInterval, CompactionMonitorEnabled: true, RetentionEnabled: true, ShutdownSyncTimeout: DefaultShutdownSyncTimeout, ShutdownSyncInterval: DefaultShutdownSyncInterval, HeartbeatCheckInterval: DefaultHeartbeatCheckInterval, Logger: slog.Default().With(LogKeySystem, LogSystemStore), } for _, db := range dbs { db.SetLogger(s.Logger.With(LogKeyDB, filepath.Base(db.Path()))) db.L0Retention = s.L0Retention db.ShutdownSyncTimeout = s.ShutdownSyncTimeout db.ShutdownSyncInterval = s.ShutdownSyncInterval db.VerifyCompaction = s.VerifyCompaction db.RetentionEnabled = s.RetentionEnabled } s.ctx, s.cancel = context.WithCancel(context.Background()) return s } func (s *Store) Open(ctx context.Context) error { if err := s.levels.Validate(); err != nil { return err } g, ctx := errgroup.WithContext(ctx) g.SetLimit(50) for _, db := range s.dbs { db := db g.Go(func() error { select { case <-ctx.Done(): return ctx.Err() default: return db.Open() } }) } if err := g.Wait(); err != nil { return err } // Start monitors for compactions & snapshots. if s.CompactionMonitorEnabled { // Start compaction monitors for all levels except L0. for _, lvl := range s.levels { lvl := lvl if lvl.Level == 0 { continue } s.wg.Add(1) go func() { defer s.wg.Done() s.monitorCompactionLevel(s.ctx, lvl) }() } // Start snapshot monitor for snapshots. s.wg.Add(1) go func() { defer s.wg.Done() s.monitorCompactionLevel(s.ctx, s.SnapshotLevel()) }() } if s.L0Retention > 0 && s.L0RetentionCheckInterval > 0 { s.wg.Add(1) go func() { defer s.wg.Done() s.monitorL0Retention(s.ctx) }() } // Start heartbeat monitor if any database has heartbeat configured. s.startHeartbeatMonitorIfNeeded() // Start validation monitor if configured. if s.ValidationInterval > 0 { s.wg.Add(1) go func() { defer s.wg.Done() s.monitorValidation(s.ctx) }() } return nil } func (s *Store) Close(ctx context.Context) (err error) { s.mu.Lock() dbs := slices.Clone(s.dbs) s.mu.Unlock() for _, db := range dbs { if e := db.Close(ctx); e != nil { if errors.Is(e, ErrShutdownInterrupted) { if err == nil { err = e } } else if err == nil || errors.Is(err, ErrShutdownInterrupted) { err = e } } } // Cancel and wait for background tasks to complete. s.cancel() s.wg.Wait() return err } func (s *Store) DBs() []*DB { s.mu.Lock() defer s.mu.Unlock() return slices.Clone(s.dbs) } // RegisterDB registers a new database with the store and starts monitoring it. func (s *Store) RegisterDB(db *DB) error { if db == nil { return fmt.Errorf("db required") } // First check: see if database already exists s.mu.Lock() for _, existing := range s.dbs { if existing.Path() == db.Path() { s.mu.Unlock() return nil } } s.mu.Unlock() // Apply store-wide settings before opening the database. db.SetLogger(s.Logger.With(LogKeyDB, filepath.Base(db.Path()))) db.L0Retention = s.L0Retention db.ShutdownSyncTimeout = s.ShutdownSyncTimeout db.ShutdownSyncInterval = s.ShutdownSyncInterval db.VerifyCompaction = s.VerifyCompaction db.RetentionEnabled = s.RetentionEnabled db.Done = s.done // Open the database without holding the lock to avoid blocking other operations. // The double-check pattern below handles the race condition. if err := db.Open(); err != nil { return fmt.Errorf("open db: %w", err) } // Second check: verify database wasn't added by another goroutine while we were opening. // If it was, close our instance and return without error. s.mu.Lock() for _, existing := range s.dbs { if existing.Path() == db.Path() { // Another goroutine added this database while we were opening. // Release lock before closing to avoid potential deadlock. s.mu.Unlock() if err := db.Close(context.Background()); err != nil { db.Logger.Error("close duplicate db", "path", db.Path(), "error", err) } return nil } } s.dbs = append(s.dbs, db) s.mu.Unlock() // Start heartbeat monitor if heartbeat is configured and monitor isn't running. s.startHeartbeatMonitorIfNeeded() return nil } // UnregisterDB stops monitoring the database at the provided path and closes it. func (s *Store) UnregisterDB(ctx context.Context, path string) error { if path == "" { return fmt.Errorf("db path required") } s.mu.Lock() idx := -1 var db *DB for i, existing := range s.dbs { if existing.Path() == path { idx = i db = existing break } } if db == nil { s.mu.Unlock() return nil } s.dbs = slices.Delete(s.dbs, idx, idx+1) s.mu.Unlock() if err := db.Close(ctx); err != nil { return fmt.Errorf("close db: %w", err) } return nil } // EnableDB starts replication for a registered database. // The context is checked for cancellation before opening. // Note: db.Open() itself does not support cancellation. func (s *Store) EnableDB(ctx context.Context, path string) error { db := s.FindDB(path) if db == nil { return fmt.Errorf("database not found: %s", path) } if db.IsOpen() { return fmt.Errorf("database already enabled: %s", path) } // Check for cancellation before starting open if err := ctx.Err(); err != nil { return fmt.Errorf("enable database: %w", err) } if err := db.Open(); err != nil { return fmt.Errorf("open database: %w", err) } return nil } // DisableDB stops replication for a database. func (s *Store) DisableDB(ctx context.Context, path string) error { db := s.FindDB(path) if db == nil { return fmt.Errorf("database not found: %s", path) } if !db.IsOpen() { return fmt.Errorf("database already disabled: %s", path) } if err := db.Close(ctx); err != nil { return fmt.Errorf("close database: %w", err) } return nil } // SyncDBResult holds the result of a sync operation. type SyncDBResult struct { TXID uint64 ReplicatedTXID uint64 Changed bool } // SyncDB forces an immediate sync for a database. If wait is true, blocks // until both WAL-to-LTX and LTX-to-remote sync complete. If wait is false, // only performs the WAL-to-LTX sync and lets the replica monitor handle upload. // The timeout is best-effort as internal lock acquisition is not context-aware. func (s *Store) SyncDB(ctx context.Context, path string, wait bool) (SyncDBResult, error) { db := s.FindDB(path) if db == nil { return SyncDBResult{}, fmt.Errorf("%w: %s", ErrDatabaseNotFound, path) } if !db.IsOpen() { return SyncDBResult{}, fmt.Errorf("%w: %s", ErrDatabaseNotOpen, path) } _, beforeTXID, err := db.MaxLTX() if err != nil { return SyncDBResult{}, fmt.Errorf("read position before sync: %w", err) } if wait { if err := db.SyncAndWait(ctx); err != nil { return SyncDBResult{}, fmt.Errorf("sync database: %w", err) } } else { if err := db.Sync(ctx); err != nil { return SyncDBResult{}, fmt.Errorf("sync database: %w", err) } } _, afterTXID, err := db.MaxLTX() if err != nil { return SyncDBResult{}, fmt.Errorf("read position after sync: %w", err) } var replicatedTXID uint64 if db.Replica != nil { replicatedTXID = uint64(db.Replica.Pos().TXID) } return SyncDBResult{ TXID: uint64(afterTXID), ReplicatedTXID: replicatedTXID, Changed: afterTXID > beforeTXID, }, nil } // FindDB returns the database with the given path. func (s *Store) FindDB(path string) *DB { s.mu.Lock() defer s.mu.Unlock() for _, db := range s.dbs { if db.Path() == path { return db } } return nil } // SetL0Retention updates the retention window for L0 files and propagates it to // all managed databases. func (s *Store) SetL0Retention(d time.Duration) { s.mu.Lock() defer s.mu.Unlock() s.L0Retention = d for _, db := range s.dbs { db.L0Retention = d } } // SetDone sets the done channel used for interrupt handling during shutdown // and propagates it to all managed databases. func (s *Store) SetDone(done <-chan struct{}) { s.mu.Lock() defer s.mu.Unlock() s.done = done for _, db := range s.dbs { db.Done = done } } // SetShutdownSyncTimeout updates the shutdown sync timeout and propagates it to // all managed databases. func (s *Store) SetShutdownSyncTimeout(d time.Duration) { s.mu.Lock() defer s.mu.Unlock() s.ShutdownSyncTimeout = d for _, db := range s.dbs { db.ShutdownSyncTimeout = d } } // SetShutdownSyncInterval updates the shutdown sync interval and propagates it to // all managed databases. func (s *Store) SetShutdownSyncInterval(d time.Duration) { s.mu.Lock() defer s.mu.Unlock() s.ShutdownSyncInterval = d for _, db := range s.dbs { db.ShutdownSyncInterval = d } } // SetVerifyCompaction updates the verify compaction flag and propagates it to // all managed databases. func (s *Store) SetVerifyCompaction(v bool) { s.mu.Lock() defer s.mu.Unlock() s.VerifyCompaction = v for _, db := range s.dbs { db.VerifyCompaction = v db.compactor.VerifyCompaction = v } } func (s *Store) SetRetentionEnabled(v bool) { s.mu.Lock() defer s.mu.Unlock() s.RetentionEnabled = v for _, db := range s.dbs { db.RetentionEnabled = v db.compactor.RetentionEnabled = v } } // SnapshotLevel returns a pseudo compaction level based on snapshot settings. func (s *Store) SnapshotLevel() *CompactionLevel { return &CompactionLevel{ Level: SnapshotLevel, Interval: s.SnapshotInterval, } } func (s *Store) monitorCompactionLevel(ctx context.Context, lvl *CompactionLevel) { s.Logger.Info("starting compaction monitor", "level", lvl.Level, "interval", lvl.Interval) retryDeadline := time.Time{} timer := time.NewTimer(time.Nanosecond) defer timer.Stop() for { select { case <-ctx.Done(): return case <-timer.C: // proceed } now := time.Now() nextDelay := time.Until(lvl.NextCompactionAt(now)) var notReadyDBs []string for _, db := range s.DBs() { if !db.IsOpen() { continue // skip disabled DBs } _, err := s.CompactDB(ctx, db, lvl) switch { case errors.Is(err, ErrNoCompaction), errors.Is(err, ErrCompactionTooEarly): db.Logger.Debug("no compaction", "level", lvl.Level, "path", db.Path()) case errors.Is(err, ErrDBNotReady): db.Logger.Debug("db not ready, skipping", "level", lvl.Level, "path", db.Path(), "error", err) notReadyDBs = append(notReadyDBs, db.Path()) case err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded): db.Logger.Error("compaction failed", "level", lvl.Level, "error", err) } if lvl.Level == SnapshotLevel { if err := s.EnforceSnapshotRetention(ctx, db); err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { db.Logger.Error("retention enforcement failed", "error", err) } } } timedOut := !retryDeadline.IsZero() && now.After(retryDeadline) if len(notReadyDBs) > 0 && !timedOut { if retryDeadline.IsZero() { retryDeadline = now.Add(DefaultDBInitTimeout) } nextDelay = time.Second s.Logger.Debug("scheduling retry for unready dbs", "level", lvl.Level) } else { if timedOut { s.Logger.Warn("timeout waiting for db initialization", "level", lvl.Level, "dbs", notReadyDBs, "timeout", DefaultDBInitTimeout, "hint", "database may have corrupted local state or blocked transactions; try removing -litestream directory and restarting") } retryDeadline = time.Time{} } if nextDelay < 0 { nextDelay = 0 } timer.Reset(nextDelay) } } func (s *Store) monitorL0Retention(ctx context.Context) { s.Logger.Info("starting L0 retention monitor", "interval", s.L0RetentionCheckInterval, "retention", s.L0Retention) ticker := time.NewTicker(s.L0RetentionCheckInterval) defer ticker.Stop() LOOP: for { select { case <-ctx.Done(): break LOOP case <-ticker.C: } for _, db := range s.DBs() { if !db.IsOpen() { continue // skip disabled DBs } if err := db.EnforceL0RetentionByTime(ctx); err != nil { if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { continue } db.Logger.Error("l0 retention enforcement failed", "path", db.Path(), "error", err) } } } } // startHeartbeatMonitorIfNeeded starts the heartbeat monitor goroutine if: // - HeartbeatCheckInterval is configured // - Heartbeat is configured on the Store // - The monitor is not already running func (s *Store) startHeartbeatMonitorIfNeeded() { s.mu.Lock() defer s.mu.Unlock() if s.heartbeatMonitorRunning { return } if s.HeartbeatCheckInterval <= 0 { return } if !s.hasHeartbeatConfigLocked() { return } s.heartbeatMonitorRunning = true s.wg.Add(1) go func() { defer s.wg.Done() s.monitorHeartbeats(s.ctx) }() } // hasHeartbeatConfigLocked returns true if heartbeat is configured on the Store. // Must be called with s.mu held. func (s *Store) hasHeartbeatConfigLocked() bool { return s.Heartbeat != nil && s.Heartbeat.URL != "" } // monitorHeartbeats periodically checks if heartbeat pings should be sent. // Heartbeat pings are only sent when ALL databases have synced successfully // within the heartbeat interval. func (s *Store) monitorHeartbeats(ctx context.Context) { s.Logger.Info("starting heartbeat monitor", "interval", s.HeartbeatCheckInterval) ticker := time.NewTicker(s.HeartbeatCheckInterval) defer ticker.Stop() LOOP: for { select { case <-ctx.Done(): break LOOP case <-ticker.C: } s.sendHeartbeatIfNeeded(ctx) } } // sendHeartbeatIfNeeded sends a heartbeat ping if: // - Heartbeat is configured on the Store // - Enough time has passed since the last ping attempt // - ALL databases have synced successfully within the heartbeat interval func (s *Store) sendHeartbeatIfNeeded(ctx context.Context) { hb := s.Heartbeat if hb == nil || hb.URL == "" { return } if !hb.ShouldPing() { return } // Check if all databases are healthy (synced within the heartbeat interval). // A database is healthy if it synced within the heartbeat interval. healthySince := time.Now().Add(-hb.Interval) if !s.allDatabasesHealthy(healthySince) { return } // Record ping attempt time before making the request to ensure we respect // the configured interval even if the ping fails. This prevents rapid // retries that could overwhelm the endpoint. hb.RecordPing() if err := hb.Ping(ctx); err != nil { s.Logger.Error("heartbeat ping failed", "url", hb.URL, "error", err) return } s.Logger.Debug("heartbeat ping sent", "url", hb.URL) } // allDatabasesHealthy returns true if all databases have synced successfully // since the given time. Returns false if there are no databases or no enabled databases. func (s *Store) allDatabasesHealthy(since time.Time) bool { dbs := s.DBs() if len(dbs) == 0 { return false } enabledCount := 0 for _, db := range dbs { if !db.IsOpen() { continue // skip disabled DBs } enabledCount++ lastSync := db.LastSuccessfulSyncAt() if lastSync.IsZero() || lastSync.Before(since) { return false } } return enabledCount > 0 } // CompactDB performs a compaction or snapshot for a given database on a single destination level. // This function will only proceed if a compaction has not occurred before the last compaction time. func (s *Store) CompactDB(ctx context.Context, db *DB, lvl *CompactionLevel) (*ltx.FileInfo, error) { // Skip if database is not yet initialized (page size unknown). if db.PageSize() == 0 { return nil, &DBNotReadyError{Reason: "page size not initialized"} } dstLevel := lvl.Level // Ensure we are not re-compacting before the most recent compaction time. prevCompactionAt := lvl.PrevCompactionAt(time.Now()) dstInfo, err := db.MaxLTXFileInfo(ctx, dstLevel) if err != nil { return nil, fmt.Errorf("fetch dst level info: %w", err) } else if dstInfo.CreatedAt.After(prevCompactionAt) { return nil, ErrCompactionTooEarly } // Shortcut if this is a snapshot since we are not pulling from a previous level. if dstLevel == SnapshotLevel { info, err := db.Snapshot(ctx) if err != nil { return info, err } db.Logger.InfoContext(ctx, "snapshot complete", "txid", info.MaxTXID.String(), "size", info.Size) return info, nil } // Fetch latest LTX files for both the source & destination so we can see if we need to make progress. srcLevel := s.levels.PrevLevel(dstLevel) srcInfo, err := db.MaxLTXFileInfo(ctx, srcLevel) if err != nil { return nil, fmt.Errorf("fetch src level info: %w", err) } // Skip if there are no new files to compact. if srcInfo.MaxTXID <= dstInfo.MinTXID { return nil, ErrNoCompaction } info, err := db.Compact(ctx, dstLevel) if err != nil { return info, err } db.Logger.InfoContext(ctx, "compaction complete", "level", dstLevel, slog.Group("txid", "min", info.MinTXID.String(), "max", info.MaxTXID.String(), ), "size", info.Size, ) return info, nil } // EnforceSnapshotRetention removes old snapshots by timestamp and then // cleans up all lower levels based on minimum snapshot TXID. func (s *Store) EnforceSnapshotRetention(ctx context.Context, db *DB) error { // Enforce retention for the snapshot level. minSnapshotTXID, err := db.EnforceSnapshotRetention(ctx, time.Now().Add(-s.SnapshotRetention)) if err != nil { return fmt.Errorf("enforce snapshot retention: %w", err) } // We should also enforce retention for L0 on the same schedule as L1. for _, lvl := range s.levels { // Skip L0 since it is enforced on a more frequent basis. if lvl.Level == 0 { continue } if err := db.EnforceRetentionByTXID(ctx, lvl.Level, minSnapshotTXID); err != nil { return fmt.Errorf("enforce L%d retention: %w", lvl.Level, err) } } return nil } // ValidationResult holds the result of validating a replica's LTX files. type ValidationResult struct { Valid bool // true if no errors found Errors []ValidationError // all errors found } // Validate checks LTX file consistency across all databases and levels. // SnapshotLevel (9) is excluded since snapshots are not contiguous. func (s *Store) Validate(ctx context.Context) (*ValidationResult, error) { result := &ValidationResult{Valid: true} s.mu.Lock() dbs := s.dbs levels := s.levels s.mu.Unlock() for _, db := range dbs { if db.Replica == nil { continue } for _, lvl := range levels { errs, err := db.Replica.ValidateLevel(ctx, lvl.Level) if err != nil { return nil, fmt.Errorf("validate level %d for %s: %w", lvl.Level, db.Path(), err) } if len(errs) > 0 { result.Valid = false result.Errors = append(result.Errors, errs...) } } } return result, nil } // monitorValidation periodically runs validation checks on all databases. func (s *Store) monitorValidation(ctx context.Context) { s.Logger.Info("starting validation monitor", "interval", s.ValidationInterval) ticker := time.NewTicker(s.ValidationInterval) defer ticker.Stop() LOOP: for { select { case <-ctx.Done(): break LOOP case <-ticker.C: } result, err := s.Validate(ctx) if err != nil { if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { continue } s.Logger.Error("validation check failed", "error", err) continue } if !result.Valid { for _, verr := range result.Errors { s.Logger.Warn("validation error detected", "level", verr.Level, "type", verr.Type, "message", verr.Message, ) } } } } ================================================ FILE: store_compaction_remote_test.go ================================================ package litestream_test import ( "bytes" "context" "fmt" "io" "log/slog" "os" "path/filepath" "sync" "testing" "time" "github.com/superfly/ltx" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/internal/testingutil" ) // TestStore_CompactDB_RemotePartialRead ensures that compactions do not rely on // immediately consistent remote reads. Some object stores (or custom replica // clients) can expose a newly written object before all bytes are available. // Without additional safeguards, compaction can read the partial object and // generate a corrupted snapshot which then fails during restore. func TestStore_CompactDB_RemotePartialRead(t *testing.T) { t.Parallel() ctx := context.Background() client := newDelayedReplicaClient(200 * time.Millisecond) dbPath := filepath.Join(t.TempDir(), "db") db := litestream.NewDB(dbPath) db.MonitorInterval = 0 db.Replica = litestream.NewReplica(db) db.Replica.Client = client db.Replica.MonitorEnabled = false levels := litestream.CompactionLevels{ {Level: 0}, {Level: 1, Interval: time.Second}, } store := litestream.NewStore([]*litestream.DB{db}, levels) store.CompactionMonitorEnabled = false if err := store.Open(ctx); err != nil { t.Fatalf("open store: %v", err) } defer func() { if err := store.Close(ctx); err != nil { t.Fatalf("close store: %v", err) } }() sqldb := testingutil.MustOpenSQLDB(t, db.Path()) defer testingutil.MustCloseSQLDB(t, sqldb) if _, err := sqldb.ExecContext(ctx, `CREATE TABLE t (id INTEGER PRIMARY KEY, val TEXT)`); err != nil { t.Fatalf("create table: %v", err) } insert := func(start, end int) { for i := start; i < end; i++ { if _, err := sqldb.ExecContext(ctx, `INSERT INTO t (val) VALUES (?)`, fmt.Sprintf("value-%d", i)); err != nil { t.Fatalf("insert %d: %v", i, err) } } } // Generate two consecutive L0 files. insert(0, 256) if err := db.Sync(ctx); err != nil { t.Fatalf("sync #1: %v", err) } if err := db.Replica.Sync(ctx); err != nil { t.Fatalf("replica sync #1: %v", err) } insert(256, 512) if err := db.Sync(ctx); err != nil { t.Fatalf("sync #2: %v", err) } if err := db.Replica.Sync(ctx); err != nil { t.Fatalf("replica sync #2: %v", err) } // Compact level 0 into level 1. The delayed replica returns a partial view // for newly written files which previously resulted in corrupted snapshots. if _, err := store.CompactDB(ctx, db, levels[1]); err != nil { t.Fatalf("compact: %v", err) } client.waitForAvailability() restorePath := filepath.Join(t.TempDir(), "restore.db") if err := db.Replica.Restore(ctx, litestream.RestoreOptions{OutputPath: restorePath}); err != nil { t.Fatalf("restore: %v", err) } } // delayedReplicaClient simulates an eventually-consistent object store where a // newly written object can be observed before all of its content is available. // Prior to availability, OpenLTXFile returns a valid but truncated LTX file. type delayedReplicaClient struct { mu sync.Mutex files map[string]*delayedFile delay time.Duration } type delayedFile struct { level int min ltx.TXID max ltx.TXID data []byte partial []byte createdAt time.Time availableAt time.Time } func newDelayedReplicaClient(delay time.Duration) *delayedReplicaClient { return &delayedReplicaClient{ files: make(map[string]*delayedFile), delay: delay, } } func (c *delayedReplicaClient) Type() string { return "delayed" } func (c *delayedReplicaClient) Init(context.Context) error { return nil } func (c *delayedReplicaClient) SetLogger(*slog.Logger) {} func (c *delayedReplicaClient) key(level int, min, max ltx.TXID) string { return fmt.Sprintf("%d:%s:%s", level, min.String(), max.String()) } func (c *delayedReplicaClient) LTXFiles(_ context.Context, level int, seek ltx.TXID, _ bool) (ltx.FileIterator, error) { c.mu.Lock() defer c.mu.Unlock() infos := make([]*ltx.FileInfo, 0, len(c.files)) for _, file := range c.files { if file.level != level { continue } if file.max < seek { continue } infos = append(infos, <x.FileInfo{ Level: file.level, MinTXID: file.min, MaxTXID: file.max, Size: int64(len(file.data)), CreatedAt: file.createdAt, }) } return ltx.NewFileInfoSliceIterator(infos), nil } func (c *delayedReplicaClient) OpenLTXFile(_ context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { c.mu.Lock() file, ok := c.files[c.key(level, minTXID, maxTXID)] c.mu.Unlock() if !ok { return nil, os.ErrNotExist } data := file.data if time.Now().Before(file.availableAt) && len(file.partial) > 0 { data = file.partial } if offset > int64(len(data)) { return io.NopCloser(bytes.NewReader(nil)), nil } data = data[offset:] if size > 0 && size < int64(len(data)) { data = data[:size] } return io.NopCloser(bytes.NewReader(data)), nil } func (c *delayedReplicaClient) WriteLTXFile(_ context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { data, err := io.ReadAll(r) if err != nil { return nil, err } partial, err := buildPartialSnapshot(data) if err != nil { return nil, err } info := <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Size: int64(len(data)), CreatedAt: time.Now().UTC(), } c.mu.Lock() c.files[c.key(level, minTXID, maxTXID)] = &delayedFile{ level: level, min: minTXID, max: maxTXID, data: data, partial: partial, createdAt: info.CreatedAt, availableAt: time.Now().Add(c.delay), } c.mu.Unlock() return info, nil } func (c *delayedReplicaClient) DeleteLTXFiles(_ context.Context, a []*ltx.FileInfo) error { c.mu.Lock() defer c.mu.Unlock() for _, info := range a { delete(c.files, c.key(info.Level, info.MinTXID, info.MaxTXID)) } return nil } func (c *delayedReplicaClient) DeleteAll(context.Context) error { c.mu.Lock() defer c.mu.Unlock() c.files = make(map[string]*delayedFile) return nil } func (c *delayedReplicaClient) waitForAvailability() { time.Sleep(c.delay) } // buildPartialSnapshot returns a valid LTX snapshot that only includes the // first portion of pages from data. func buildPartialSnapshot(data []byte) ([]byte, error) { dec := ltx.NewDecoder(bytes.NewReader(data)) if err := dec.DecodeHeader(); err != nil { return nil, err } hdr := dec.Header() buf := new(bytes.Buffer) enc, err := ltx.NewEncoder(buf) if err != nil { return nil, err } if err := enc.EncodeHeader(hdr); err != nil { return nil, err } // Copy only a subset of pages so the resulting snapshot is incomplete. maxPages := int(hdr.Commit / 4) if maxPages < 1 { maxPages = 1 } var page ltx.PageHeader pageBuf := make([]byte, hdr.PageSize) for i := 0; i < maxPages; i++ { if err := dec.DecodePage(&page, pageBuf); err != nil { return nil, err } if err := enc.EncodePage(page, pageBuf); err != nil { return nil, err } } if err := enc.Close(); err != nil { return nil, err } return buf.Bytes(), nil } ================================================ FILE: store_test.go ================================================ package litestream_test import ( "errors" "fmt" "path/filepath" "sync" "testing" "time" "github.com/stretchr/testify/require" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/internal/testingutil" ) func TestStore_CompactDB(t *testing.T) { t.Run("L1", func(t *testing.T) { db0, sqldb0 := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db0, sqldb0) db1, sqldb1 := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db1, sqldb1) levels := litestream.CompactionLevels{ {Level: 0}, {Level: 1, Interval: 1 * time.Second}, {Level: 2, Interval: 500 * time.Millisecond}, } s := litestream.NewStore([]*litestream.DB{db0, db1}, levels) s.CompactionMonitorEnabled = false if err := s.Open(t.Context()); err != nil { t.Fatal(err) } defer s.Close(t.Context()) if _, err := sqldb0.ExecContext(t.Context(), `CREATE TABLE t (id INT);`); err != nil { t.Fatal(err) } if _, err := sqldb0.ExecContext(t.Context(), `INSERT INTO t (id) VALUES (100)`); err != nil { t.Fatal(err) } else if err := db0.Sync(t.Context()); err != nil { t.Fatal(err) } else if err := db0.Replica.Sync(t.Context()); err != nil { t.Fatal(err) } _, err := s.CompactDB(t.Context(), db0, levels[1]) require.NoError(t, err) // Re-compacting immediately should return an error indicating compaction // cannot proceed. This may be ErrCompactionTooEarly (detected timing conflict) // or ErrNoCompaction (no new files to compact). Both are valid outcomes // depending on whether we crossed a second boundary during the first compaction // (PrevCompactionAt truncates to seconds, causing edge cases at boundaries). _, err = s.CompactDB(t.Context(), db0, levels[1]) require.True(t, errors.Is(err, litestream.ErrCompactionTooEarly) || errors.Is(err, litestream.ErrNoCompaction), "expected ErrCompactionTooEarly or ErrNoCompaction, got: %v", err) // Re-compacting after the interval should show that there is nothing to compact. time.Sleep(levels[1].Interval) _, err = s.CompactDB(t.Context(), db0, levels[1]) require.ErrorIs(t, err, litestream.ErrNoCompaction) }) t.Run("Snapshot", func(t *testing.T) { db0, sqldb0 := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db0, sqldb0) levels := litestream.CompactionLevels{ {Level: 0}, {Level: 1, Interval: 100 * time.Millisecond}, {Level: 2, Interval: 500 * time.Millisecond}, } s := litestream.NewStore([]*litestream.DB{db0}, levels) s.CompactionMonitorEnabled = false if err := s.Open(t.Context()); err != nil { t.Fatal(err) } defer s.Close(t.Context()) if _, err := sqldb0.ExecContext(t.Context(), `CREATE TABLE t (id INT);`); err != nil { t.Fatal(err) } if _, err := sqldb0.ExecContext(t.Context(), `INSERT INTO t (id) VALUES (100)`); err != nil { t.Fatal(err) } else if err := db0.Sync(t.Context()); err != nil { t.Fatal(err) } else if err := db0.Replica.Sync(t.Context()); err != nil { t.Fatal(err) } if _, err := s.CompactDB(t.Context(), db0, s.SnapshotLevel()); err != nil { t.Fatal(err) } // Re-compacting immediately should return an error that there's nothing to compact. if _, err := s.CompactDB(t.Context(), db0, s.SnapshotLevel()); !errors.Is(err, litestream.ErrCompactionTooEarly) { t.Fatalf("unexpected error: %s", err) } }) // Regression test for GitHub issue #877: level 9 compaction fails with // "page size not initialized yet" error when attempted before DB initialization. t.Run("DBNotReady", func(t *testing.T) { db0 := testingutil.MustOpenDB(t) defer testingutil.MustCloseDB(t, db0) levels := litestream.CompactionLevels{ {Level: 0}, {Level: 1, Interval: 100 * time.Millisecond}, } s := litestream.NewStore([]*litestream.DB{db0}, levels) s.CompactionMonitorEnabled = false if err := s.Open(t.Context()); err != nil { t.Fatal(err) } defer s.Close(t.Context()) // Attempt snapshot before DB is initialized (page size not set). // This reproduces the timing issue where level 9 compaction fires // immediately at startup before db.Sync() has been called. if _, err := s.CompactDB(t.Context(), db0, s.SnapshotLevel()); !errors.Is(err, litestream.ErrDBNotReady) { t.Fatalf("expected ErrDBNotReady, got: %v", err) } }) } func TestStore_Integration(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } const factor = 1 db := testingutil.NewDB(t, filepath.Join(t.TempDir(), "db")) db.MonitorInterval = factor * 100 * time.Millisecond db.Replica = litestream.NewReplica(db) db.Replica.Client = file.NewReplicaClient(t.TempDir()) if err := db.Open(); err != nil { t.Fatal(err) } sqldb := testingutil.MustOpenSQLDB(t, db.Path()) defer testingutil.MustCloseSQLDB(t, sqldb) store := litestream.NewStore([]*litestream.DB{db}, litestream.CompactionLevels{ {Level: 0}, {Level: 1, Interval: factor * 200 * time.Millisecond}, {Level: 2, Interval: factor * 500 * time.Millisecond}, }) store.SnapshotInterval = factor * 1 * time.Second if err := store.Open(t.Context()); err != nil { t.Fatal(err) } defer store.Close(t.Context()) // Create initial table if _, err := sqldb.ExecContext(t.Context(), `CREATE TABLE t (id INTEGER PRIMARY KEY, val TEXT);`); err != nil { t.Fatal(err) } // Run test for a fixed duration. done := make(chan struct{}) time.AfterFunc(10*time.Second, func() { close(done) }) // Channel for insert errors insertErr := make(chan error, 1) // WaitGroup to ensure insert goroutine completes before cleanup var wg sync.WaitGroup // Wait for insert goroutine to finish before cleanup & surface any errors. defer func() { wg.Wait() select { case err := <-insertErr: t.Fatalf("insert error during test: %v", err) default: // No insert errors } }() // Start goroutine to continuously insert records wg.Add(1) go func() { defer wg.Done() ticker := time.NewTicker(factor * 10 * time.Millisecond) defer ticker.Stop() for { select { case <-t.Context().Done(): return case <-done: return case <-ticker.C: if _, err := sqldb.ExecContext(t.Context(), `INSERT INTO t (val) VALUES (?);`, time.Now().String()); err != nil { // Check if we're shutting down select { case <-done: // Expected during shutdown, just exit return default: // Real error, send it select { case insertErr <- err: default: } return } } } } }() // Periodically snapshot, restore and validate ticker := time.NewTicker(factor * 500 * time.Millisecond) defer ticker.Stop() for i := 0; ; i++ { select { case <-t.Context().Done(): return case <-done: return case <-ticker.C: // Restore database to a temporary location. outputPath := filepath.Join(t.TempDir(), fmt.Sprintf("restore-%d.db", i)) if err := db.Replica.Restore(t.Context(), litestream.RestoreOptions{ OutputPath: outputPath, }); err != nil { t.Fatal(err) } func() { restoreDB := testingutil.MustOpenSQLDB(t, outputPath) defer testingutil.MustCloseSQLDB(t, restoreDB) var result string if err := restoreDB.QueryRowContext(t.Context(), `PRAGMA integrity_check;`).Scan(&result); err != nil { t.Fatal(err) } else if result != "ok" { t.Fatalf("integrity check failed: %s", result) } var count int if err := restoreDB.QueryRowContext(t.Context(), `SELECT COUNT(*) FROM t`).Scan(&count); err != nil { t.Fatal(err) } else if count == 0 { t.Fatal("no records found in restored database") } t.Logf("restored database: %d records", count) }() } } } // TestStore_SnapshotInterval_Default ensures that the default snapshot interval // is preserved when not explicitly set (regression test for issue #689). func TestStore_SnapshotInterval_Default(t *testing.T) { // Create a store with no databases and no levels store := litestream.NewStore(nil, nil) // Verify default snapshot interval is set if store.SnapshotInterval != litestream.DefaultSnapshotInterval { t.Errorf("expected default snapshot interval of %v, got %v", litestream.DefaultSnapshotInterval, store.SnapshotInterval) } // Verify default is 24 hours if store.SnapshotInterval != 24*time.Hour { t.Errorf("expected default snapshot interval of 24h, got %v", store.SnapshotInterval) } } func TestStore_Validate(t *testing.T) { t.Run("AllLevelsValid", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) db := &litestream.DB{} db.Replica = litestream.NewReplicaWithClient(db, client) levels := litestream.CompactionLevels{ {Level: 0}, {Level: 1}, } store := litestream.NewStore([]*litestream.DB{db}, levels) // Create contiguous files at L0 createTestLTXFile(t, client, 0, 1, 1) createTestLTXFile(t, client, 0, 2, 2) // Create contiguous files at L1 createTestLTXFile(t, client, 1, 1, 2) result, err := store.Validate(t.Context()) if err != nil { t.Fatal(err) } if !result.Valid { t.Errorf("expected valid result, got errors: %v", result.Errors) } }) t.Run("ErrorAtMultipleLevels", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) db := &litestream.DB{} db.Replica = litestream.NewReplicaWithClient(db, client) levels := litestream.CompactionLevels{ {Level: 0}, {Level: 1}, } store := litestream.NewStore([]*litestream.DB{db}, levels) // Create files with gap at L0 createTestLTXFile(t, client, 0, 1, 1) createTestLTXFile(t, client, 0, 5, 5) // gap at 2-4 // Create files with overlap at L1 createTestLTXFile(t, client, 1, 1, 5) createTestLTXFile(t, client, 1, 3, 7) // overlap result, err := store.Validate(t.Context()) if err != nil { t.Fatal(err) } if result.Valid { t.Error("expected invalid result") } if len(result.Errors) != 2 { t.Errorf("expected 2 errors, got %d", len(result.Errors)) } }) t.Run("NilReplica", func(t *testing.T) { // DB with nil replica should be skipped db := &litestream.DB{} // db.Replica is nil levels := litestream.CompactionLevels{ {Level: 0}, } store := litestream.NewStore([]*litestream.DB{db}, levels) result, err := store.Validate(t.Context()) if err != nil { t.Fatal(err) } if !result.Valid { t.Errorf("expected valid result for nil replica, got errors: %v", result.Errors) } }) t.Run("MultipleDBs", func(t *testing.T) { client1 := file.NewReplicaClient(t.TempDir()) client2 := file.NewReplicaClient(t.TempDir()) db1 := &litestream.DB{} db1.Replica = litestream.NewReplicaWithClient(db1, client1) db2 := &litestream.DB{} db2.Replica = litestream.NewReplicaWithClient(db2, client2) levels := litestream.CompactionLevels{ {Level: 0}, } store := litestream.NewStore([]*litestream.DB{db1, db2}, levels) // db1: valid createTestLTXFile(t, client1, 0, 1, 1) createTestLTXFile(t, client1, 0, 2, 2) // db2: gap error createTestLTXFile(t, client2, 0, 1, 1) createTestLTXFile(t, client2, 0, 5, 5) result, err := store.Validate(t.Context()) if err != nil { t.Fatal(err) } if result.Valid { t.Error("expected invalid result") } if len(result.Errors) != 1 { t.Errorf("expected 1 error from db2, got %d", len(result.Errors)) } }) } func TestStore_ValidationMonitor(t *testing.T) { t.Run("RunsPeriodically", func(t *testing.T) { db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) levels := litestream.CompactionLevels{ {Level: 0}, {Level: 1, Interval: time.Hour}, } store := litestream.NewStore([]*litestream.DB{db}, levels) store.CompactionMonitorEnabled = false store.ValidationInterval = 50 * time.Millisecond if err := store.Open(t.Context()); err != nil { t.Fatal(err) } // Wait for at least one validation cycle time.Sleep(100 * time.Millisecond) if err := store.Close(t.Context()); err != nil { t.Fatal(err) } }) t.Run("DisabledByDefault", func(t *testing.T) { levels := litestream.CompactionLevels{ {Level: 0}, } store := litestream.NewStore(nil, levels) store.CompactionMonitorEnabled = false // ValidationInterval should be zero by default if store.ValidationInterval != 0 { t.Errorf("expected ValidationInterval=0, got %v", store.ValidationInterval) } // Open should succeed without starting validation monitor if err := store.Open(t.Context()); err != nil { t.Fatal(err) } if err := store.Close(t.Context()); err != nil { t.Fatal(err) } }) } func TestStore_SetRetentionEnabled(t *testing.T) { db0, sqldb0 := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db0, sqldb0) db1, sqldb1 := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db1, sqldb1) levels := litestream.CompactionLevels{ {Level: 0}, {Level: 1, Interval: time.Hour}, } store := litestream.NewStore([]*litestream.DB{db0, db1}, levels) store.CompactionMonitorEnabled = false // Initially should be true (retention enabled by default). if !store.RetentionEnabled { t.Fatal("expected RetentionEnabled=true initially") } // Set to false and verify propagation to all DBs. store.SetRetentionEnabled(false) if store.RetentionEnabled { t.Fatal("expected store.RetentionEnabled=false") } for _, db := range store.DBs() { if db.RetentionEnabled { t.Fatalf("expected db.RetentionEnabled=false for %s", db.Path()) } } // Set back to true. store.SetRetentionEnabled(true) if !store.RetentionEnabled { t.Fatal("expected store.RetentionEnabled=true after reset") } for _, db := range store.DBs() { if !db.RetentionEnabled { t.Fatalf("expected db.RetentionEnabled=true for %s after reset", db.Path()) } } } ================================================ FILE: tests/cpu-usage/README.md ================================================ # CPU Usage Testing This directory contains test scripts and configurations for measuring Litestream's idle CPU usage, particularly for validating the fixes in issue #992. ## Files - `test-cpu-usage.sh` - Automated CPU monitoring script - `litestream-test-polling.yml` - Config for testing with S3 replication ## Prerequisites 1. Build Litestream binary: ```bash cd ../.. go build -o bin/litestream ./cmd/litestream ``` 2. Set up AWS credentials in `.envrc` at repo root: ```bash export AWS_ACCESS_KEY_ID="your-key-id" export AWS_SECRET_ACCESS_KEY="your-secret-key" export AWS_REGION="us-east-2" export S3_BUCKET="your-test-bucket" ``` 3. Have `sqlite3` CLI installed ## Usage From this directory, run: ```bash # Test for 60 seconds ./test-cpu-usage.sh 60 # Longer test (5 minutes) ./test-cpu-usage.sh 300 ``` ## What It Tests The script: 1. Creates a test SQLite database at `/tmp/test.db` 2. Starts Litestream with S3 replication 3. Monitors CPU usage every second using `ps` 4. Calculates average CPU usage 5. Verifies S3 replication is working 6. Outputs results and detailed CSV log ## Expected Results Based on testing for PR #993: - **With S3 transport fix:** ~0.0067% CPU (99% improvement) - **Original (v0.5.6):** ~0.7% CPU The S3 transport fix achieves near-zero idle CPU usage, validating the fix. ## Output Results are printed to stdout and detailed logs are saved to: - `/tmp/litestream-cpu-log.csv` - Per-second CPU measurements ## Notes - Tests require real S3 credentials and will upload data to your bucket - Test database is created at `/tmp/test.db` and cleaned up on each run - CPU measurements are instantaneous snapshots, not averages over intervals - Longer test durations (5-10 minutes) provide more stable averages ================================================ FILE: tests/cpu-usage/litestream-test-polling.yml ================================================ # Litestream test configuration - POLLING MODE # Tests idle CPU usage with default polling (1s interval) dbs: - path: /tmp/test.db replicas: - type: s3 bucket: sprite-litestream-debugging region: us-east-2 path: test-db-polling # Default: monitor-interval: 1s # Default: monitor-mode: poll ================================================ FILE: tests/cpu-usage/test-cpu-usage.sh ================================================ #!/bin/bash set -e # Test script for measuring Litestream idle CPU usage with S3 replication DURATION=${1:-300} # Default 5 minutes CONFIG_FILE="litestream-test-polling.yml" MODE_DESC="Polling mode (1s interval)" echo "=========================================" echo "Litestream CPU Usage Test" echo "=========================================" echo "Mode: $MODE_DESC" echo "Config: $CONFIG_FILE" echo "Duration: ${DURATION}s" echo "=========================================" # Create test database echo "Creating test database..." rm -f /tmp/test.db /tmp/test.db-wal /tmp/test.db-shm sqlite3 /tmp/test.db "CREATE TABLE test (id INTEGER PRIMARY KEY, data TEXT);" sqlite3 /tmp/test.db "INSERT INTO test (data) VALUES ('test');" # Start Litestream in background echo "Starting Litestream..." # Get script directory and repo root SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" source "$REPO_ROOT/.envrc" "$REPO_ROOT/bin/litestream" replicate -config "$SCRIPT_DIR/$CONFIG_FILE" & LITESTREAM_PID=$! echo "Litestream PID: $LITESTREAM_PID" echo "" echo "Monitoring CPU usage for ${DURATION}s..." echo "Press Ctrl+C to stop early" echo "" # Monitor CPU usage echo "Time,CPU%,VSZ,RSS" > /tmp/litestream-cpu-log.csv for i in $(seq 1 $DURATION); do if ! kill -0 $LITESTREAM_PID 2>/dev/null; then echo "ERROR: Litestream process died!" exit 1 fi # Get CPU and memory stats CPU=$(ps -p $LITESTREAM_PID -o %cpu= | xargs) VSZ=$(ps -p $LITESTREAM_PID -o vsz= | xargs) RSS=$(ps -p $LITESTREAM_PID -o rss= | xargs) echo "$i,$CPU,$VSZ,$RSS" >> /tmp/litestream-cpu-log.csv # Display every 10 seconds if [ $((i % 10)) -eq 0 ]; then echo "[$i/${DURATION}s] CPU: ${CPU}% VSZ: ${VSZ}KB RSS: ${RSS}KB" fi sleep 1 done # Stop Litestream echo "" echo "Stopping Litestream..." kill $LITESTREAM_PID wait $LITESTREAM_PID 2>/dev/null || true # Calculate average CPU echo "" echo "=========================================" echo "Results" echo "=========================================" AVG_CPU=$(awk -F',' 'NR>1 {sum+=$2; count++} END {if(count>0) print sum/count; else print 0}' /tmp/litestream-cpu-log.csv) echo "Average CPU: ${AVG_CPU}%" echo "Detailed log: /tmp/litestream-cpu-log.csv" echo "" # Show sample of S3 uploads echo "S3 Bucket Contents:" aws s3 ls s3://sprite-litestream-debugging/test-db-${CONFIG_MODE}/ --recursive | head -10 ================================================ FILE: tests/integration/README.md ================================================ # Integration Tests Go-based integration tests for Litestream. These tests replace the previous bash-based test scripts with proper Go testing infrastructure. ## Overview This package contains comprehensive integration tests organized by test type: - **scenario_test.go** - Core functionality scenarios (fresh start, integrity, deletion, failover) - **concurrent_test.go** - Concurrency and stress tests (rapid checkpoints, WAL growth, concurrent ops, busy timeout) - **quick_test.go** - Quick validation tests (30 minutes configurable) - **overnight_test.go** - Long-running stability tests (8+ hours) - **boundary_test.go** - Edge cases (1GB boundary, different page sizes) - **helpers.go** - Shared test utilities and helpers - **fixtures.go** - Test data generators and scenarios ## Prerequisites Build the required binaries: ```bash go build -o bin/litestream ./cmd/litestream go build -o bin/litestream-test ./cmd/litestream-test ``` ## Running Tests ### Quick Tests (Default) Run fast integration tests suitable for CI: ```bash go test -v -tags=integration -timeout=30m ./tests/integration/... \ -run="TestFreshStart|TestDatabaseIntegrity|TestRapidCheckpoints" ``` ### All Scenario Tests Run all scenario tests (excluding long-running): ```bash go test -v -tags=integration -timeout=1h ./tests/integration/... ``` ### Long-Running Tests Run overnight and boundary tests: ```bash go test -v -tags="integration,long" -timeout=10h ./tests/integration/... \ -run="TestOvernight|Test1GBBoundary" ``` ## Soak Tests Long-running soak tests live alongside the other integration tests and share the same helpers. They are excluded from CI by default and are intended for release validation or targeted debugging. ### Overview | Test | Tags | Defaults | Purpose | Extra Requirements | | --- | --- | --- | --- | --- | | `TestComprehensiveSoak` | `integration,soak` | 2h duration, 50 MB DB, 500 writes/s | File-backed end-to-end stress | Litestream binaries in `./bin` | | `TestMinIOSoak` | `integration,soak,docker` | 2h duration, 5 MB DB (short=2 m), 100 writes/s | S3-compatible replication via MinIO | Docker daemon, `docker` CLI | | `TestOvernightS3Soak` | `integration,soak,aws` | 8h duration, 50 MB DB | Real S3 replication & restore | AWS credentials, `aws` CLI | All soak tests support `go test -test.short` to scale the default duration down to roughly two minutes for smoke verification. ### Environment Variables | Variable | Default | Description | | --- | --- | --- | | `SOAK_AUTO_PURGE` | `yes` for non-interactive shells; prompts otherwise | Controls whether MinIO buckets are cleared before each run. Set to `no` to retain objects between runs. | | `SOAK_KEEP_TEMP` | unset | When set (any value), preserves the temporary directory and artifacts (database, config, logs) instead of removing them after the test completes. | | `SOAK_DEBUG` | `0` | Streams command stdout/stderr (database population, load generation, docker helpers) directly to the console. Without this the output is captured and only shown on failure. | | `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `S3_BUCKET`, `AWS_REGION` | required for `aws` tag | Provide credentials and target bucket for the overnight S3 soak. Region defaults to `us-east-1` if unset. | ### Example Commands File-based soak (full length): ```bash go test -v -tags="integration,soak" \ -run=TestComprehensiveSoak -timeout=3h ./tests/integration ``` File-based soak (short mode with preserved artifacts and debug logging): ```bash SOAK_KEEP_TEMP=1 SOAK_DEBUG=1 go test -v -tags="integration,soak" \ -run=TestComprehensiveSoak -test.short -timeout=1h ./tests/integration ``` MinIO soak (short mode, auto-purges bucket, preserves results): ```bash SOAK_AUTO_PURGE=yes SOAK_KEEP_TEMP=1 go test -v -tags="integration,soak,docker" \ -run=TestMinIOSoak -test.short -timeout=20m ./tests/integration ``` Overnight S3 soak (full duration): ```bash export AWS_ACCESS_KEY_ID=... export AWS_SECRET_ACCESS_KEY=... export S3_BUCKET=your-bucket export AWS_REGION=us-east-1 go test -v -tags="integration,soak,aws" \ -run=TestOvernightS3Soak -timeout=10h ./tests/integration ``` ### Tips - Run with `-v` to view the 60-second progress updates and final status summary. Without `-v`, progress output is suppressed by Go’s test runner. - When prompted about purging a MinIO bucket, answering “yes” clears the bucket via `minio/mc` before the run; “no” allows you to inspect lingering objects from previous executions. - `SOAK_KEEP_TEMP=1` is especially useful when investigating failures—the helper prints the preserved path so you can inspect databases, configs, and logs. - The monitoring infrastructure automatically prints additional status blocks when error counts change, making `SOAK_DEBUG=1` optional for most workflows. ### Specific Tests Run individual test functions: ```bash # Fresh start test go test -v -tags=integration ./tests/integration/... -run=TestFreshStart # Rapid checkpoints test go test -v -tags=integration ./tests/integration/... -run=TestRapidCheckpoints # 1GB boundary test go test -v -tags=integration ./tests/integration/... -run=Test1GBBoundary ``` ### Short Mode Run abbreviated versions with `-short`: ```bash go test -v -tags=integration -short ./tests/integration/... ``` This reduces test durations by 10x (e.g., 8 hours becomes 48 minutes). ## Test Categories ### Scenario Tests Core functionality tests that run in seconds to minutes: - `TestFreshStart` - Starting replication before database exists - `TestDatabaseIntegrity` - Complex schema and data integrity - `TestDatabaseDeletion` - Source database deletion during replication ### Concurrent Tests Stress and concurrency tests: - `TestRapidCheckpoints` - Rapid checkpoint operations under load - `TestWALGrowth` - Large WAL file handling (100MB+) - `TestConcurrentOperations` - Multiple databases replicating simultaneously - `TestBusyTimeout` - Database busy timeout and lock handling ### Quick Tests Configurable duration validation (default 30 minutes): - `TestQuickValidation` - Comprehensive validation with wave pattern load ### Overnight Tests Long-running stability tests (default 8 hours): - `TestOvernightFile` - 8-hour file-based replication test - `TestOvernightComprehensive` - 8-hour comprehensive test with large database ### Boundary Tests Edge case and boundary condition tests: - `Test1GBBoundary` - SQLite 1GB lock page boundary (page #262145 with 4KB pages) - `TestLockPageWithDifferentPageSizes` - Lock page handling with various page sizes ## CI Integration ### Automatic (Pull Requests) Quick tests run automatically on PRs modifying Go code: ```yaml - Quick integration tests (TestFreshStart, TestDatabaseIntegrity, TestRapidCheckpoints) - Timeout: 30 minutes ``` ### Manual Workflows Trigger via GitHub Actions UI: **Quick Tests:** ``` workflow_dispatch → test_type: quick ``` **All Scenario Tests:** ``` workflow_dispatch → test_type: all ``` **Long-Running Tests:** ``` workflow_dispatch → test_type: long ``` ## Test Infrastructure ### Helpers (helpers.go) - `SetupTestDB(t, name)` - Create test database instance - `TestDB.Create()` - Create database with WAL mode - `TestDB.Populate(size)` - Populate to target size - `TestDB.StartLitestream()` - Start replication - `TestDB.StopLitestream()` - Stop replication - `TestDB.Restore(path)` - Restore from replica - `TestDB.Validate(path)` - Full validation (integrity, checksum, data) - `TestDB.QuickValidate(path)` - Quick validation - `TestDB.GenerateLoad(...)` - Generate database load - `GetTestDuration(t, default)` - Get configurable test duration - `RequireBinaries(t)` - Check for required binaries ### Fixtures (fixtures.go) - `DefaultLoadConfig()` - Load generation configuration - `DefaultPopulateConfig()` - Database population configuration - `CreateComplexTestSchema(db)` - Multi-table schema with foreign keys - `PopulateComplexTestData(db, ...)` - Populate complex data - `LargeWALScenario()` - Large WAL test scenario - `RapidCheckpointsScenario()` - Rapid checkpoint scenario ## Test Artifacts Tests create temporary directories via `t.TempDir()`: ``` /tmp// ├── .db # Test database ├── .db-wal # WAL file ├── .db-shm # Shared memory ├── replica/ # Replica directory │ └── ltx/0/ # LTX files ├── litestream.log # Litestream output └── *-restored.db # Restored databases ``` Artifacts are automatically cleaned up after tests complete. ## Debugging Tests ### View Litestream Logs ```go log, err := db.GetLitestreamLog() fmt.Println(log) ``` ### Check for Errors ```go errors, err := db.CheckForErrors() for _, e := range errors { t.Logf("Error: %s", e) } ``` ### Inspect Replica ```go fileCount, _ := db.GetReplicaFileCount() t.Logf("LTX files: %d", fileCount) ``` ### Check Database Size ```go size, _ := db.GetDatabaseSize() t.Logf("DB size: %.2f MB", float64(size)/(1024*1024)) ``` ## Migration from Bash This is part of an ongoing effort to migrate bash test scripts to Go integration tests. This migration improves maintainability, enables CI integration, and provides platform independence. ### Test Directory Organization Three distinct test locations serve different purposes: **`tests/integration/` (this directory)** - Go-based integration and soak tests: - Quick integration tests: `scenario_test.go`, `concurrent_test.go`, `boundary_test.go` - Soak tests (2-8 hours): `comprehensive_soak_test.go`, `minio_soak_test.go`, `overnight_s3_soak_test.go` - All tests use proper Go testing infrastructure with build tags **`scripts/` (top-level)** - Utility scripts only (soak tests migrated to Go): - `analyze-test-results.sh` - Post-test analysis utility - `setup-homebrew-tap.sh` - Packaging script (not a test) **`cmd/litestream-test/scripts/`** - Scenario and debugging bash scripts (being phased out): - Bug reproduction scripts for specific issues (#752, #754) - Format & upgrade tests for version compatibility - S3 retention tests with Python mock - Quick validation and setup utilities ### Migration Status **Migrated from `scripts/` (5 scripts):** - `test-quick-validation.sh` → `quick_test.go::TestQuickValidation` (CI: ✅) - `test-overnight.sh` → `overnight_test.go::TestOvernightFile` (CI: ❌ too long) - `test-comprehensive.sh` → `comprehensive_soak_test.go::TestComprehensiveSoak` (CI: ❌ soak test) - `test-minio-s3.sh` → `minio_soak_test.go::TestMinIOSoak` (CI: ❌ soak test, requires Docker) - `test-overnight-s3.sh` → `overnight_s3_soak_test.go::TestOvernightS3Soak` (CI: ❌ soak test, 8 hours) **Migrated from `cmd/litestream-test/scripts/` (9 scripts):** - `test-fresh-start.sh` → `scenario_test.go::TestFreshStart` - `test-database-integrity.sh` → `scenario_test.go::TestDatabaseIntegrity` - `test-database-deletion.sh` → `scenario_test.go::TestDatabaseDeletion` - `test-replica-failover.sh` → NOT MIGRATED (feature removed from Litestream) - `test-rapid-checkpoints.sh` → `concurrent_test.go::TestRapidCheckpoints` - `test-wal-growth.sh` → `concurrent_test.go::TestWALGrowth` - `test-concurrent-operations.sh` → `concurrent_test.go::TestConcurrentOperations` - `test-busy-timeout.sh` → `concurrent_test.go::TestBusyTimeout` - `test-1gb-boundary.sh` → `boundary_test.go::Test1GBBoundary` **Remaining Bash Scripts:** _scripts/_ (2 scripts remaining): - `analyze-test-results.sh` - Post-test analysis utility (may stay as bash) - `setup-homebrew-tap.sh` - Packaging script (not a test) _cmd/litestream-test/scripts/_ (16 scripts remaining): - Bug reproduction scripts: `reproduce-critical-bug.sh`, `test-754-*.sh`, `test-v0.5-*.sh` - Format & upgrade tests: `test-format-isolation.sh`, `test-upgrade-*.sh`, `test-massive-upgrade.sh` - S3 retention tests: `test-s3-retention-*.sh` (4 scripts, use Python S3 mock) - Utility: `verify-test-setup.sh` ### Why Some Tests Aren't in CI Per industry best practices, CI tests should complete in < 1 hour (ideally < 10 minutes): - ✅ **Quick tests** (< 5 min) - Run on every PR - ❌ **Soak tests** (2-8 hours) - Run locally before releases only - ❌ **Long-running tests** (> 30 min) - Too slow for CI feedback loop Soak tests are migrated to Go for maintainability but run **locally only**. See "Soak Tests" section below. ## Soak Tests (Long-Running Stability Tests) Soak tests run for 2-8 hours to validate long-term stability under sustained load. These tests are **NOT run in CI** per industry best practices (effective CI requires tests to complete in < 1 hour). ### Purpose Soak tests validate: - Long-term replication stability - Memory leak detection over time - Compaction effectiveness across multiple cycles - Checkpoint behavior under sustained load - Recovery from transient issues - Storage growth patterns ### When to Run Soak Tests - ✅ Before major releases - ✅ After significant replication changes - ✅ To reproduce stability issues - ✅ For performance benchmarking - ❌ NOT on every commit (too slow for CI) ### Running Soak Tests Locally **File-based comprehensive test (2 hours):** ```bash go test -v -tags="integration,soak" -timeout=3h -run=TestComprehensiveSoak ./tests/integration/ ``` **MinIO S3 test (2 hours, requires Docker):** ```bash # Ensure Docker is running go test -v -tags="integration,soak,docker" -timeout=3h -run=TestMinIOSoak ./tests/integration/ ``` **Overnight S3 test (8 hours, requires AWS):** ```bash export AWS_ACCESS_KEY_ID=your_key export AWS_SECRET_ACCESS_KEY=your_secret export S3_BUCKET=your-test-bucket export AWS_REGION=us-east-1 go test -v -tags="integration,soak,aws" -timeout=10h -run=TestOvernightS3Soak ./tests/integration/ ``` **Run all soak tests:** ```bash go test -v -tags="integration,soak,docker,aws" -timeout=15h ./tests/integration/ ``` ### Adjust Duration for Testing Tests respect the `-test.short` flag to run abbreviated versions: ```bash # Run comprehensive test for 30 minutes instead of 2 hours go test -v -tags="integration,soak" -timeout=1h -run=TestComprehensiveSoak ./tests/integration/ -test.short ``` ### Soak Test Build Tags Soak tests use multiple build tags to control execution: - `integration` - Required for all integration tests - `soak` - Marks long-running stability tests (2-8 hours) - `docker` - Requires Docker (MinIO test) - `aws` - Requires AWS credentials (S3 tests) ### Monitoring Soak Tests All soak tests log progress every 60 seconds: ```bash # Watch test progress in real-time go test -v -tags="integration,soak" -run=TestComprehensiveSoak ./tests/integration/ 2>&1 | tee soak-test.log ``` Metrics reported during execution: - Database size and WAL size - Row count - Replica statistics (snapshots, LTX segments) - Operation counts (checkpoints, compactions, syncs) - Error counts - Write rate ### Soak Test Summary | Test | Duration | Requirements | What It Tests | |------|----------|--------------|---------------| | TestComprehensiveSoak | 2h | None | File-based replication with aggressive compaction | | TestMinIOSoak | 2h | Docker | S3-compatible storage via MinIO container | | TestOvernightS3Soak | 8h | AWS credentials | Real S3 replication, overnight stability | ## Benefits Over Bash 1. **Type Safety** - Compile-time error checking 2. **Better Debugging** - Use standard Go debugging tools 3. **Code Reuse** - Shared helpers and fixtures 4. **Parallel Execution** - Tests can run concurrently 5. **CI Integration** - Run automatically on PRs 6. **Test Coverage** - Measure code coverage 7. **Consistent Patterns** - Standard Go testing conventions 8. **Better Error Messages** - Structured, clear reporting 9. **Platform Independent** - Works on Linux, macOS, Windows 10. **IDE Integration** - Full editor support ## Contributing When adding new integration tests: 1. Use appropriate build tags (`//go:build integration` or `//go:build integration && long`) 2. Call `RequireBinaries(t)` to check prerequisites 3. Use `SetupTestDB(t, name)` for test setup 4. Call `defer db.Cleanup()` for automatic cleanup 5. Log test progress with descriptive messages 6. Use `GetTestDuration(t, default)` for configurable durations 7. Add test to CI workflow if appropriate 8. Update this README with new test documentation ## Related Documentation - [cmd/litestream-test README](../../cmd/litestream-test/README.md) - Testing harness CLI - [scripts/README.md](../../scripts/README.md) - Legacy bash test scripts - [GitHub Issue #798](https://github.com/benbjohnson/litestream/issues/798) - Migration tracking ================================================ FILE: tests/integration/boundary_test.go ================================================ //go:build integration package integration import ( "fmt" "path/filepath" "testing" "time" _ "github.com/mattn/go-sqlite3" ) func Test1GBBoundary(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } RequireBinaries(t) t.Log("Testing: SQLite 1GB lock page boundary handling") t.Log("This tests database growth beyond 1GB with 4KB pages (lock page at #262145)") db := SetupTestDB(t, "1gb-boundary") defer db.Cleanup() t.Log("[1] Creating database with 4KB page size...") if err := db.CreateWithPageSize(4096); err != nil { t.Fatalf("Failed to create database: %v", err) } t.Log("✓ Database created with 4KB pages") t.Log("[2] Populating to 1.5GB to cross lock page boundary...") if err := db.PopulateWithOptions("1.5GB", 4096, 1024); err != nil { t.Fatalf("Failed to populate database: %v", err) } dbSize, err := db.GetDatabaseSize() if err != nil { t.Fatalf("Failed to get database size: %v", err) } sizeGB := float64(dbSize) / (1024 * 1024 * 1024) t.Logf("✓ Database populated: %.2f GB", sizeGB) if sizeGB < 1.0 { t.Fatalf("Database did not reach 1GB threshold: %.2f GB", sizeGB) } t.Log("[3] Starting Litestream...") if err := db.StartLitestream(); err != nil { t.Fatalf("Failed to start Litestream: %v", err) } time.Sleep(30 * time.Second) t.Log("[4] Checking replication across lock page boundary...") fileCount, err := db.GetReplicaFileCount() if err != nil { t.Fatalf("Failed to check replica: %v", err) } if fileCount == 0 { t.Fatal("No LTX files created!") } t.Logf("✓ Replication started: %d LTX files", fileCount) t.Log("[5] Checking for lock page errors...") errors, err := db.CheckForErrors() if err != nil { t.Fatalf("Failed to check errors: %v", err) } lockPageErrors := 0 for _, errMsg := range errors { if containsAny(errMsg, []string{"lock page", "page 262145", "locking page"}) { lockPageErrors++ t.Logf("Lock page error: %s", errMsg) } } if lockPageErrors > 0 { t.Fatalf("Found %d lock page errors!", lockPageErrors) } t.Log("✓ No lock page errors detected") db.StopLitestream() time.Sleep(2 * time.Second) t.Log("[6] Testing restore of large database...") restoredPath := filepath.Join(db.TempDir, "1gb-restored.db") if err := db.Restore(restoredPath); err != nil { t.Fatalf("Restore failed: %v", err) } t.Log("✓ Restore successful") t.Log("[7] Validating restored database integrity...") if err := db.QuickValidate(restoredPath); err != nil { t.Fatalf("Validation failed: %v", err) } restoredDB := &TestDB{Path: restoredPath, t: t} restoredSize, _ := restoredDB.GetDatabaseSize() restoredSizeGB := float64(restoredSize) / (1024 * 1024 * 1024) t.Logf("✓ Restored database size: %.2f GB", restoredSizeGB) if restoredSizeGB < 0.9 { t.Fatalf("Restored database too small: %.2f GB (expected ~%.2f GB)", restoredSizeGB, sizeGB) } t.Log("TEST PASSED: 1GB lock page boundary handled correctly") } func TestLockPageWithDifferentPageSizes(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } RequireBinaries(t) t.Log("Testing: Lock page handling with different SQLite page sizes") pageSizes := []struct { size int lockPageNum int targetSizeMB int }{ {4096, 262145, 1200}, {8192, 131073, 1200}, } for _, ps := range pageSizes { t.Run(fmt.Sprintf("PageSize%d", ps.size), func(t *testing.T) { db := SetupTestDB(t, fmt.Sprintf("lockpage-%d", ps.size)) defer db.Cleanup() t.Logf("[1] Creating database with %d byte page size (lock page at #%d)...", ps.size, ps.lockPageNum) if err := db.CreateWithPageSize(ps.size); err != nil { t.Fatalf("Failed to create database: %v", err) } t.Logf("[2] Populating to %dMB...", ps.targetSizeMB) if err := db.PopulateWithOptions(fmt.Sprintf("%dMB", ps.targetSizeMB), ps.size, 1024); err != nil { t.Fatalf("Failed to populate database: %v", err) } dbSize, _ := db.GetDatabaseSize() t.Logf("✓ Database: %.2f MB", float64(dbSize)/(1024*1024)) t.Log("[3] Starting replication...") if err := db.StartLitestream(); err != nil { t.Fatalf("Failed to start Litestream: %v", err) } t.Log("[3a] Waiting for replication to produce files...") fileCount, err := db.WaitForReplicaFiles(1, 2*time.Minute) if err != nil { t.Logf("Warning: %v", err) } t.Logf("✓ LTX files: %d", fileCount) db.StopLitestream() t.Log("[4] Testing restore...") restoredPath := filepath.Join(db.TempDir, fmt.Sprintf("lockpage-%d-restored.db", ps.size)) if err := db.Restore(restoredPath); err != nil { t.Fatalf("Restore failed: %v", err) } t.Log("✓ Test passed for page size", ps.size) }) } t.Log("TEST PASSED: All page sizes handled correctly") } func containsAny(s string, substrs []string) bool { for _, substr := range substrs { if contains(s, substr) { return true } } return false } func contains(s, substr string) bool { return len(s) >= len(substr) && (s == substr || len(s) > len(substr) && (s[:len(substr)] == substr || s[len(s)-len(substr):] == substr || anySubstring(s, substr))) } func anySubstring(s, substr string) bool { for i := 0; i <= len(s)-len(substr); i++ { if s[i:i+len(substr)] == substr { return true } } return false } ================================================ FILE: tests/integration/compatibility_test.go ================================================ package integration_test import ( "bytes" "context" "fmt" "os" "os/exec" "path/filepath" "strconv" "strings" "testing" "time" "github.com/superfly/ltx" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/file" "github.com/benbjohnson/litestream/internal/testingutil" ) // TestRestore_FormatConsistency tests that backups created by the current version // can be restored by the same version. This is a basic sanity check that should // always pass. func TestRestore_FormatConsistency(t *testing.T) { if testing.Short() { t.Skip("Skipping in short mode") } ctx := context.Background() // Create a database with test data db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) // Insert initial data if _, err := sqldb.ExecContext(ctx, `CREATE TABLE compat_test(id INTEGER PRIMARY KEY, data TEXT);`); err != nil { t.Fatalf("create table: %v", err) } for i := 0; i < 100; i++ { if _, err := sqldb.ExecContext(ctx, `INSERT INTO compat_test(data) VALUES(?);`, fmt.Sprintf("data-%d", i)); err != nil { t.Fatalf("insert: %v", err) } } // Sync to replica if err := db.Sync(ctx); err != nil { t.Fatalf("sync: %v", err) } if err := db.Replica.Sync(ctx); err != nil { t.Fatalf("replica sync: %v", err) } // Checkpoint to ensure data is persisted if err := db.Checkpoint(ctx, litestream.CheckpointModeTruncate); err != nil { t.Fatalf("checkpoint: %v", err) } // Add more data after checkpoint for i := 100; i < 150; i++ { if _, err := sqldb.ExecContext(ctx, `INSERT INTO compat_test(data) VALUES(?);`, fmt.Sprintf("data-%d", i)); err != nil { t.Fatalf("insert: %v", err) } } // Sync again if err := db.Sync(ctx); err != nil { t.Fatalf("sync: %v", err) } if err := db.Replica.Sync(ctx); err != nil { t.Fatalf("replica sync: %v", err) } // Verify LTX files exist itr, err := db.Replica.Client.LTXFiles(ctx, 0, 0, false) if err != nil { t.Fatalf("list LTX files: %v", err) } var fileCount int for itr.Next() { fileCount++ } if err := itr.Close(); err != nil { t.Fatalf("close iterator: %v", err) } t.Logf("Created %d L0 files", fileCount) // Restore to a new location restorePath := filepath.Join(t.TempDir(), "restored.db") if err := db.Replica.Restore(ctx, litestream.RestoreOptions{ OutputPath: restorePath, }); err != nil { t.Fatalf("restore: %v", err) } // Verify restored data restoredDB := testingutil.MustOpenSQLDB(t, restorePath) defer restoredDB.Close() var count int if err := restoredDB.QueryRowContext(ctx, `SELECT COUNT(*) FROM compat_test;`).Scan(&count); err != nil { t.Fatalf("count: %v", err) } if count != 150 { t.Errorf("restored row count: got %d, want 150", count) } // Verify integrity var integrity string if err := restoredDB.QueryRowContext(ctx, `PRAGMA integrity_check;`).Scan(&integrity); err != nil { t.Fatalf("integrity check: %v", err) } if integrity != "ok" { t.Errorf("integrity check: %s", integrity) } } // TestRestore_MultipleSyncs tests restore after many sync cycles to ensure // LTX file accumulation doesn't cause issues. func TestRestore_MultipleSyncs(t *testing.T) { if testing.Short() { t.Skip("Skipping in short mode") } ctx := context.Background() db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) if _, err := sqldb.ExecContext(ctx, `CREATE TABLE sync_test(id INTEGER PRIMARY KEY, batch INTEGER, data BLOB);`); err != nil { t.Fatalf("create table: %v", err) } // Perform multiple sync cycles const syncCycles = 50 for batch := 0; batch < syncCycles; batch++ { for i := 0; i < 10; i++ { if _, err := sqldb.ExecContext(ctx, `INSERT INTO sync_test(batch, data) VALUES(?, randomblob(500));`, batch); err != nil { t.Fatalf("insert: %v", err) } } if err := db.Sync(ctx); err != nil { t.Fatalf("sync %d: %v", batch, err) } if err := db.Replica.Sync(ctx); err != nil { t.Fatalf("replica sync %d: %v", batch, err) } } // Verify LTX files itr, err := db.Replica.Client.LTXFiles(ctx, 0, 0, false) if err != nil { t.Fatalf("list LTX files: %v", err) } var fileCount int for itr.Next() { fileCount++ } if err := itr.Close(); err != nil { t.Fatalf("close iterator: %v", err) } t.Logf("Created %d L0 files over %d sync cycles", fileCount, syncCycles) // Restore restorePath := filepath.Join(t.TempDir(), "restored.db") if err := db.Replica.Restore(ctx, litestream.RestoreOptions{ OutputPath: restorePath, }); err != nil { t.Fatalf("restore: %v", err) } // Verify restoredDB := testingutil.MustOpenSQLDB(t, restorePath) defer restoredDB.Close() var count int if err := restoredDB.QueryRowContext(ctx, `SELECT COUNT(*) FROM sync_test;`).Scan(&count); err != nil { t.Fatalf("count: %v", err) } expected := syncCycles * 10 if count != expected { t.Errorf("restored row count: got %d, want %d", count, expected) } } // TestRestore_LTXFileValidation tests that invalid LTX files are properly // detected and rejected during restore. func TestRestore_LTXFileValidation(t *testing.T) { if testing.Short() { t.Skip("Skipping in short mode") } ctx := context.Background() replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) // Create a valid snapshot first validSnapshot := createValidLTXData(t, 1, 1, time.Now()) if _, err := client.WriteLTXFile(ctx, litestream.SnapshotLevel, 1, 1, bytes.NewReader(validSnapshot)); err != nil { t.Fatalf("write snapshot: %v", err) } tests := []struct { name string data []byte minTXID ltx.TXID maxTXID ltx.TXID expectError bool }{ { name: "ValidL0File", data: createValidLTXData(t, 2, 2, time.Now()), minTXID: 2, maxTXID: 2, expectError: false, }, { name: "EmptyFile", data: []byte{}, minTXID: 3, maxTXID: 3, expectError: true, }, { name: "TruncatedHeader", data: []byte("truncated"), minTXID: 4, maxTXID: 4, expectError: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if _, err := client.WriteLTXFile(ctx, 0, tt.minTXID, tt.maxTXID, bytes.NewReader(tt.data)); err != nil { t.Logf("write failed (may be expected): %v", err) } }) } } // TestRestore_CrossPlatformPaths tests that backups work with different path styles. func TestRestore_CrossPlatformPaths(t *testing.T) { if testing.Short() { t.Skip("Skipping in short mode") } ctx := context.Background() pathTests := []string{ "simple", "path/with/slashes", "path-with-dashes", "path_with_underscores", } for _, subpath := range pathTests { t.Run(subpath, func(t *testing.T) { replicaDir := t.TempDir() fullPath := filepath.Join(replicaDir, subpath) client := file.NewReplicaClient(fullPath) // Create snapshot snapshot := createValidLTXData(t, 1, 1, time.Now()) if _, err := client.WriteLTXFile(ctx, litestream.SnapshotLevel, 1, 1, bytes.NewReader(snapshot)); err != nil { t.Fatalf("write snapshot: %v", err) } // Create L0 files for i := 2; i <= 5; i++ { data := createValidLTXData(t, ltx.TXID(i), ltx.TXID(i), time.Now()) if _, err := client.WriteLTXFile(ctx, 0, ltx.TXID(i), ltx.TXID(i), bytes.NewReader(data)); err != nil { t.Fatalf("write L0 %d: %v", i, err) } } // Verify files exist itr, err := client.LTXFiles(ctx, 0, 0, false) if err != nil { t.Fatalf("list files: %v", err) } var count int for itr.Next() { count++ } itr.Close() if count != 4 { t.Errorf("file count: got %d, want 4", count) } }) } } // TestRestore_PointInTimeAccuracy tests that point-in-time restore respects // timestamps correctly. func TestRestore_PointInTimeAccuracy(t *testing.T) { if testing.Short() { t.Skip("Skipping in short mode") } ctx := context.Background() replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) baseTime := time.Now().Add(-10 * time.Minute) // Create snapshot at baseTime snapshot := createValidLTXData(t, 1, 1, baseTime) if _, err := client.WriteLTXFile(ctx, litestream.SnapshotLevel, 1, 1, bytes.NewReader(snapshot)); err != nil { t.Fatalf("write snapshot: %v", err) } // Create L0 files at 1-minute intervals for i := 2; i <= 10; i++ { ts := baseTime.Add(time.Duration(i-1) * time.Minute) data := createValidLTXData(t, ltx.TXID(i), ltx.TXID(i), ts) if _, err := client.WriteLTXFile(ctx, 0, ltx.TXID(i), ltx.TXID(i), bytes.NewReader(data)); err != nil { t.Fatalf("write L0 %d: %v", i, err) } } // Verify timestamps are preserved when listing with metadata itr, err := client.LTXFiles(ctx, 0, 0, true) if err != nil { t.Fatalf("list files: %v", err) } defer itr.Close() var files []*ltx.FileInfo for itr.Next() { info := itr.Item() files = append(files, <x.FileInfo{ Level: info.Level, MinTXID: info.MinTXID, MaxTXID: info.MaxTXID, CreatedAt: info.CreatedAt, }) } if len(files) != 9 { t.Fatalf("file count: got %d, want 9", len(files)) } // Verify timestamps are monotonically increasing for i := 1; i < len(files); i++ { if files[i].CreatedAt.Before(files[i-1].CreatedAt) { t.Errorf("file %d timestamp (%v) is before file %d timestamp (%v)", i, files[i].CreatedAt, i-1, files[i-1].CreatedAt) } } } // createValidLTXData creates a minimal valid LTX file for testing. func createValidLTXData(t *testing.T, minTXID, maxTXID ltx.TXID, ts time.Time) []byte { t.Helper() hdr := ltx.Header{ Version: ltx.Version, PageSize: 4096, Commit: 1, MinTXID: minTXID, MaxTXID: maxTXID, Timestamp: ts.UnixMilli(), } if minTXID == 1 { hdr.PreApplyChecksum = 0 } else { hdr.PreApplyChecksum = ltx.ChecksumFlag } headerBytes, err := hdr.MarshalBinary() if err != nil { t.Fatalf("marshal header: %v", err) } return headerBytes } // TestBinaryCompatibility_CLIRestore tests that the litestream CLI can restore // backups created programmatically. This is a basic end-to-end test. func TestBinaryCompatibility_CLIRestore(t *testing.T) { if testing.Short() { t.Skip("Skipping in short mode") } // Skip if litestream binary is not available litestreamBin := os.Getenv("LITESTREAM_BIN") if litestreamBin == "" { litestreamBin = "./bin/litestream" } if _, err := os.Stat(litestreamBin); os.IsNotExist(err) { t.Skip("litestream binary not found, skipping CLI test") } ctx := context.Background() // Create database with programmatic API db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) if _, err := sqldb.ExecContext(ctx, `CREATE TABLE cli_test(id INTEGER PRIMARY KEY, value TEXT);`); err != nil { t.Fatalf("create table: %v", err) } for i := 0; i < 50; i++ { if _, err := sqldb.ExecContext(ctx, `INSERT INTO cli_test(value) VALUES(?);`, fmt.Sprintf("value-%d", i)); err != nil { t.Fatalf("insert: %v", err) } } if err := db.Sync(ctx); err != nil { t.Fatalf("sync: %v", err) } if err := db.Replica.Sync(ctx); err != nil { t.Fatalf("replica sync: %v", err) } // Get replica path from the file client fileClient, ok := db.Replica.Client.(*file.ReplicaClient) if !ok { t.Skip("Test requires file replica client") } replicaPath := fileClient.Path() // Close the database testingutil.MustCloseDBs(t, db, sqldb) // Restore using CLI restorePath := filepath.Join(t.TempDir(), "cli-restored.db") cmd := exec.CommandContext(ctx, litestreamBin, "restore", "-o", restorePath, "file://"+replicaPath) output, err := cmd.CombinedOutput() if err != nil { t.Fatalf("CLI restore failed: %v\nOutput: %s", err, output) } // Verify restored database restoredDB := testingutil.MustOpenSQLDB(t, restorePath) defer restoredDB.Close() var count int if err := restoredDB.QueryRowContext(ctx, `SELECT COUNT(*) FROM cli_test;`).Scan(&count); err != nil { t.Fatalf("count: %v", err) } if count != 50 { t.Errorf("CLI restored row count: got %d, want 50", count) } } // TestVersionMigration_DirectoryLayout tests that the current version can // detect and handle different backup directory layouts. func TestVersionMigration_DirectoryLayout(t *testing.T) { if testing.Short() { t.Skip("Skipping in short mode") } ctx := context.Background() // Test current v0.5.x layout (ltx/0/, ltx/1/, ..., ltx/9/ for snapshots) t.Run("CurrentLayout", func(t *testing.T) { replicaDir := t.TempDir() client := file.NewReplicaClient(replicaDir) // Create files in expected layout snapshot := createValidLTXData(t, 1, 1, time.Now()) if _, err := client.WriteLTXFile(ctx, litestream.SnapshotLevel, 1, 1, bytes.NewReader(snapshot)); err != nil { t.Fatalf("write snapshot: %v", err) } for i := 2; i <= 5; i++ { data := createValidLTXData(t, ltx.TXID(i), ltx.TXID(i), time.Now()) if _, err := client.WriteLTXFile(ctx, 0, ltx.TXID(i), ltx.TXID(i), bytes.NewReader(data)); err != nil { t.Fatalf("write L0 %d: %v", i, err) } } // Verify structure snapshotDir := filepath.Join(replicaDir, "ltx", strconv.Itoa(litestream.SnapshotLevel)) l0Dir := filepath.Join(replicaDir, "ltx", "0") if _, err := os.Stat(snapshotDir); err != nil { t.Errorf("snapshot directory not found: %v", err) } if _, err := os.Stat(l0Dir); err != nil { t.Errorf("L0 directory not found: %v", err) } // Verify files can be listed snapshotItr, err := client.LTXFiles(ctx, litestream.SnapshotLevel, 0, false) if err != nil { t.Fatalf("list snapshots: %v", err) } var snapshotCount int for snapshotItr.Next() { snapshotCount++ } snapshotItr.Close() l0Itr, err := client.LTXFiles(ctx, 0, 0, false) if err != nil { t.Fatalf("list L0: %v", err) } var l0Count int for l0Itr.Next() { l0Count++ } l0Itr.Close() if snapshotCount != 1 { t.Errorf("snapshot count: got %d, want 1", snapshotCount) } if l0Count != 4 { t.Errorf("L0 count: got %d, want 4", l0Count) } }) // Test that old v0.3.x layout (generations/) is not accidentally used t.Run("LegacyLayoutNotUsed", func(t *testing.T) { replicaDir := t.TempDir() // Create a generations/ directory (v0.3.x layout) legacyDir := filepath.Join(replicaDir, "generations") if err := os.MkdirAll(legacyDir, 0755); err != nil { t.Fatalf("create legacy dir: %v", err) } // Create client and verify it uses new layout client := file.NewReplicaClient(replicaDir) snapshot := createValidLTXData(t, 1, 1, time.Now()) if _, err := client.WriteLTXFile(ctx, litestream.SnapshotLevel, 1, 1, bytes.NewReader(snapshot)); err != nil { t.Fatalf("write snapshot: %v", err) } // Verify new layout is used newLayoutDir := filepath.Join(replicaDir, "ltx") if _, err := os.Stat(newLayoutDir); err != nil { t.Errorf("new layout directory not created: %v", err) } // Verify legacy directory is not used for new files entries, _ := os.ReadDir(legacyDir) if len(entries) > 0 { t.Errorf("legacy directory should remain empty, has %d entries", len(entries)) } }) } // TestCompaction_Compatibility tests that compacted files maintain compatibility // with restore operations. func TestCompaction_Compatibility(t *testing.T) { if testing.Short() { t.Skip("Skipping in short mode") } ctx := context.Background() db, sqldb := testingutil.MustOpenDBs(t) defer testingutil.MustCloseDBs(t, db, sqldb) if _, err := sqldb.ExecContext(ctx, `CREATE TABLE compact_test(id INTEGER PRIMARY KEY, data BLOB);`); err != nil { t.Fatalf("create table: %v", err) } // Generate many syncs to create L0 files for batch := 0; batch < 20; batch++ { for i := 0; i < 5; i++ { if _, err := sqldb.ExecContext(ctx, `INSERT INTO compact_test(data) VALUES(randomblob(1000));`); err != nil { t.Fatalf("insert: %v", err) } } if err := db.Sync(ctx); err != nil { t.Fatalf("sync: %v", err) } if err := db.Replica.Sync(ctx); err != nil { t.Fatalf("replica sync: %v", err) } } // Force compaction to level 1 if _, err := db.Compact(ctx, 1); err != nil { t.Logf("compact to L1 (may not have enough files): %v", err) } // Count files at different levels for level := 0; level <= 2; level++ { itr, err := db.Replica.Client.LTXFiles(ctx, level, 0, false) if err != nil { t.Fatalf("list level %d: %v", level, err) } var count int for itr.Next() { count++ } itr.Close() t.Logf("Level %d: %d files", level, count) } // Restore and verify restorePath := filepath.Join(t.TempDir(), "compacted-restore.db") if err := db.Replica.Restore(ctx, litestream.RestoreOptions{ OutputPath: restorePath, }); err != nil { t.Fatalf("restore: %v", err) } restoredDB := testingutil.MustOpenSQLDB(t, restorePath) defer restoredDB.Close() var count int if err := restoredDB.QueryRowContext(ctx, `SELECT COUNT(*) FROM compact_test;`).Scan(&count); err != nil { t.Fatalf("count: %v", err) } expected := 20 * 5 if count != expected { t.Errorf("restored row count: got %d, want %d", count, expected) } var integrity string if err := restoredDB.QueryRowContext(ctx, `PRAGMA integrity_check;`).Scan(&integrity); err != nil { t.Fatalf("integrity check: %v", err) } if !strings.Contains(integrity, "ok") { t.Errorf("integrity check failed: %s", integrity) } } ================================================ FILE: tests/integration/comprehensive_soak_test.go ================================================ //go:build integration && soak package integration import ( "context" "fmt" "path/filepath" "testing" "time" ) // TestComprehensiveSoak runs a comprehensive soak test with aggressive settings // to validate all Litestream features: replication, snapshots, compaction, checkpoints. // // Default duration: 2 hours // Can be shortened with: go test -test.short (runs for 30 minutes) // // This test exercises: // - Continuous replication // - Snapshot generation (every 10m) // - Compaction (30s/1m/5m/15m/30m intervals) // - Checkpoint operations // - Database restoration func TestComprehensiveSoak(t *testing.T) { RequireBinaries(t) // Determine test duration duration := GetTestDuration(t, 2*time.Hour) shortMode := testing.Short() if shortMode { duration = 2 * time.Minute } targetSize := "50MB" writeRate := 500 if shortMode { targetSize = "5MB" writeRate = 100 } t.Logf("================================================") t.Logf("Litestream Comprehensive Soak Test") t.Logf("================================================") t.Logf("Duration: %v", duration) t.Logf("Start time: %s", time.Now().Format(time.RFC3339)) t.Log("") t.Log("This test uses aggressive settings to validate:") t.Log(" - Continuous replication") t.Log(" - Snapshot generation (every 10m)") t.Log(" - Compaction (30s/1m/5m intervals)") t.Log(" - Checkpoint operations") t.Log(" - Database restoration") t.Log("") startTime := time.Now() // Setup test database db := SetupTestDB(t, "comprehensive-soak") defer db.Cleanup() // Create database if err := db.Create(); err != nil { t.Fatalf("Failed to create database: %v", err) } // Populate database t.Logf("Populating database (%s initial data)...", targetSize) if err := db.Populate(targetSize); err != nil { t.Fatalf("Failed to populate database: %v", err) } t.Log("✓ Database populated") t.Log("") // Create aggressive configuration for testing t.Log("Creating aggressive test configuration...") replicaURL := fmt.Sprintf("file://%s", filepath.ToSlash(db.ReplicaPath)) configPath := CreateSoakConfig(db.Path, replicaURL, nil, shortMode) db.ConfigPath = configPath t.Logf("✓ Configuration created: %s", configPath) t.Log("") // Start Litestream t.Log("Starting Litestream replication...") if err := db.StartLitestreamWithConfig(configPath); err != nil { t.Fatalf("Failed to start Litestream: %v", err) } t.Logf("✓ Litestream running (PID: %d)", db.LitestreamPID) t.Log("") // Start load generator with heavy sustained load t.Log("Starting load generator (heavy sustained load)...") t.Logf(" Write rate: %d writes/second", writeRate) t.Logf(" Pattern: wave (simulates varying load)") t.Logf(" Payload size: 4KB") t.Logf(" Workers: 8") t.Log("") ctx, cancel := context.WithTimeout(context.Background(), duration) defer cancel() // Setup signal handler for graceful interruption testInfo := &TestInfo{ StartTime: startTime, Duration: duration, DB: db, cancel: cancel, } setupSignalHandler(t, cancel, testInfo) // Run load generation in background loadDone := make(chan error, 1) go func() { loadDone <- db.GenerateLoad(ctx, writeRate, duration, "wave") }() // Monitor every 60 seconds t.Log("Running comprehensive test...") t.Log("Monitor will report every 60 seconds") t.Log("Press Ctrl+C twice within 5 seconds to stop early") t.Log("================================================") t.Log("") refreshStats := func() { testInfo.RowCount, _ = db.GetRowCount("load_test") if testInfo.RowCount == 0 { testInfo.RowCount, _ = db.GetRowCount("test_table_0") } if testInfo.RowCount == 0 { testInfo.RowCount, _ = db.GetRowCount("test_data") } testInfo.FileCount, _ = db.GetReplicaFileCount() } logMetrics := func() { LogSoakMetrics(t, db, "comprehensive") if db.LitestreamCmd != nil && db.LitestreamCmd.ProcessState != nil { t.Error("✗ Litestream stopped unexpectedly!") if testInfo.cancel != nil { testInfo.cancel() } } } MonitorSoakTest(t, db, ctx, testInfo, refreshStats, logMetrics) // Wait for load generation to complete if err := <-loadDone; err != nil { t.Logf("Load generation completed: %v", err) } if err := db.WaitForSnapshots(30 * time.Second); err != nil { t.Fatalf("Failed waiting for snapshot: %v", err) } t.Log("") t.Log("================================================") t.Log("Final Test Results") t.Log("================================================") t.Log("") // Stop Litestream t.Log("Stopping Litestream...") if err := db.StopLitestream(); err != nil { t.Logf("Warning: Failed to stop Litestream cleanly: %v", err) } // Final statistics t.Log("Database Statistics:") if dbSize, err := db.GetDatabaseSize(); err == nil { t.Logf(" Final size: %.2f MB", float64(dbSize)/(1024*1024)) } // Count rows using different table name possibilities var rowCount int var err error if rowCount, err = db.GetRowCount("load_test"); err != nil { if rowCount, err = db.GetRowCount("test_table_0"); err != nil { if rowCount, err = db.GetRowCount("test_data"); err != nil { t.Logf(" Warning: Could not get row count: %v", err) } } } if err == nil { t.Logf(" Total rows: %d", rowCount) } t.Log("") // Replica statistics t.Log("Replication Statistics:") if fileCount, err := db.GetReplicaFileCount(); err == nil { t.Logf(" LTX segments: %d", fileCount) } // Check for errors errors, _ := db.CheckForErrors() t.Logf(" Critical errors: %d", len(errors)) t.Log("") // Test restoration t.Log("Testing restoration...") restoredPath := filepath.Join(db.TempDir, "restored.db") if err := db.Restore(restoredPath); err != nil { t.Fatalf("Restoration failed: %v", err) } t.Log("✓ Restoration successful!") // Validate t.Log("") t.Log("Validating restored database integrity...") restoredDB := &TestDB{Path: restoredPath, t: t} if err := restoredDB.IntegrityCheck(); err != nil { t.Fatalf("Integrity check failed: %v", err) } t.Log("✓ Integrity check passed!") // Analyze test results analysis := AnalyzeSoakTest(t, db, duration) PrintSoakTestAnalysis(t, analysis) // Test Summary t.Log("================================================") t.Log("Test Summary") t.Log("================================================") testPassed := true issues := []string{} if criticalErrors > 0 { testPassed = false issues = append(issues, fmt.Sprintf("Critical errors detected: %d", criticalErrors)) } if analysis.FinalFileCount == 0 { testPassed = false issues = append(issues, "No files created (replication not working)") } if testPassed { t.Log("✓ TEST PASSED!") t.Log("") t.Log("The configuration is ready for production use.") } else { t.Log("⚠ TEST COMPLETED WITH ISSUES:") for _, issue := range issues { t.Logf(" - %s", issue) } t.Log("") t.Log("Review the logs for details:") logPath, _ := db.GetLitestreamLog() t.Logf(" %s", logPath) t.Fail() } t.Log("") t.Logf("Test duration: %v", time.Since(startTime).Round(time.Second)) t.Logf("Results available in: %s", db.TempDir) t.Log("================================================") } ================================================ FILE: tests/integration/concurrent_test.go ================================================ //go:build integration package integration import ( "context" "database/sql" "fmt" "os" "path/filepath" "testing" "time" _ "github.com/mattn/go-sqlite3" ) func TestRapidCheckpoints(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } RequireBinaries(t) t.Log("Testing: Litestream under rapid checkpoint pressure") db := SetupTestDB(t, "rapid-checkpoints") defer db.Cleanup() if err := db.Create(); err != nil { t.Fatalf("Failed to create database: %v", err) } t.Log("[1] Starting Litestream...") if err := db.StartLitestream(); err != nil { t.Fatalf("Failed to start Litestream: %v", err) } time.Sleep(3 * time.Second) t.Log("[2] Generating rapid writes with frequent checkpoints...") sqlDB, err := sql.Open("sqlite3", db.Path) if err != nil { t.Fatalf("Failed to open database: %v", err) } defer sqlDB.Close() if _, err := sqlDB.Exec(` CREATE TABLE checkpoint_test ( id INTEGER PRIMARY KEY AUTOINCREMENT, data BLOB, timestamp INTEGER ) `); err != nil { t.Fatalf("Failed to create table: %v", err) } data := make([]byte, 4096) checkpointCount := 0 for i := 0; i < 1000; i++ { if _, err := sqlDB.Exec( "INSERT INTO checkpoint_test (data, timestamp) VALUES (?, ?)", data, time.Now().Unix(), ); err != nil { t.Fatalf("Failed to insert row %d: %v", i, err) } if i%100 == 0 { if _, err := sqlDB.Exec("PRAGMA wal_checkpoint(TRUNCATE)"); err != nil { t.Logf("Checkpoint %d failed: %v", checkpointCount, err) } else { checkpointCount++ t.Logf("Checkpoint %d completed at row %d", checkpointCount, i) } } } t.Logf("✓ Generated 1000 writes with %d checkpoints", checkpointCount) time.Sleep(5 * time.Second) db.StopLitestream() time.Sleep(2 * time.Second) t.Log("[3] Checking for errors...") errors, err := db.CheckForErrors() if err != nil { t.Fatalf("Failed to check errors: %v", err) } if len(errors) > 5 { t.Fatalf("Too many errors (%d), showing first 5:\n%v", len(errors), errors[:5]) } else if len(errors) > 0 { t.Logf("Found %d errors (acceptable for checkpoint stress)", len(errors)) } t.Log("[4] Verifying replica...") fileCount, err := db.GetReplicaFileCount() if err != nil { t.Fatalf("Failed to check replica: %v", err) } if fileCount == 0 { t.Fatal("No replica files created!") } t.Logf("✓ Replica created with %d files", fileCount) t.Log("[5] Testing restore...") restoredPath := filepath.Join(db.TempDir, "checkpoint-restored.db") if err := db.Restore(restoredPath); err != nil { t.Fatalf("Restore failed: %v", err) } t.Log("✓ Restore successful") origCount, err := db.GetRowCount("checkpoint_test") if err != nil { t.Fatalf("Failed to get original row count: %v", err) } restoredDB := &TestDB{Path: restoredPath, t: t} restCount, err := restoredDB.GetRowCount("checkpoint_test") if err != nil { t.Fatalf("Failed to get restored row count: %v", err) } if origCount != restCount { t.Fatalf("Count mismatch: original=%d, restored=%d", origCount, restCount) } t.Logf("✓ Data integrity verified: %d rows", origCount) t.Log("TEST PASSED: Handled rapid checkpoints successfully") } func TestWALGrowth(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } RequireBinaries(t) duration := GetTestDuration(t, 2*time.Minute) t.Logf("Testing: Large WAL file handling (duration: %v)", duration) db := SetupTestDB(t, "wal-growth") defer db.Cleanup() if err := db.Create(); err != nil { t.Fatalf("Failed to create database: %v", err) } t.Log("[1] Creating test table...") sqlDB, err := sql.Open("sqlite3", db.Path) if err != nil { t.Fatalf("Failed to open database: %v", err) } defer sqlDB.Close() if _, err := sqlDB.Exec(` CREATE TABLE wal_test ( id INTEGER PRIMARY KEY AUTOINCREMENT, data BLOB ) `); err != nil { t.Fatalf("Failed to create table: %v", err) } t.Log("✓ Table created") t.Log("[2] Starting Litestream...") if err := db.StartLitestream(); err != nil { t.Fatalf("Failed to start Litestream: %v", err) } time.Sleep(3 * time.Second) t.Log("[3] Generating sustained write load...") ctx, cancel := context.WithTimeout(context.Background(), duration) defer cancel() config := DefaultLoadConfig() config.WriteRate = 400 config.Duration = duration config.Pattern = LoadPatternWave config.PayloadSize = 10 * 1024 config.Workers = 4 if err := db.GenerateLoad(ctx, config.WriteRate, config.Duration, string(config.Pattern)); err != nil && ctx.Err() == nil { t.Fatalf("Load generation failed: %v", err) } t.Log("✓ Load generation complete") time.Sleep(5 * time.Second) t.Log("[4] Checking WAL size...") walPath := db.Path + "-wal" walSize, err := getFileSize(walPath) if err != nil { t.Logf("WAL file not found (may have been checkpointed): %v", err) } else { t.Logf("WAL size: %.2f MB", float64(walSize)/(1024*1024)) } dbSize, err := db.GetDatabaseSize() if err != nil { t.Fatalf("Failed to get database size: %v", err) } t.Logf("Total database size: %.2f MB", float64(dbSize)/(1024*1024)) db.StopLitestream() time.Sleep(2 * time.Second) t.Log("[5] Checking for errors...") errors, err := db.CheckForErrors() if err != nil { t.Fatalf("Failed to check errors: %v", err) } if len(errors) > 10 { t.Fatalf("Too many errors (%d), showing first 5:\n%v", len(errors), errors[:5]) } t.Logf("✓ Found %d errors (acceptable)", len(errors)) t.Log("[6] Testing restore...") restoredPath := filepath.Join(db.TempDir, "wal-restored.db") if err := db.Restore(restoredPath); err != nil { t.Fatalf("Restore failed: %v", err) } t.Log("✓ Restore successful") origCount, err := db.GetRowCount("wal_test") if err != nil { t.Fatalf("Failed to get original row count: %v", err) } restoredDB := &TestDB{Path: restoredPath, t: t} restCount, err := restoredDB.GetRowCount("wal_test") if err != nil { t.Fatalf("Failed to get restored row count: %v", err) } if origCount != restCount { t.Fatalf("Count mismatch: original=%d, restored=%d", origCount, restCount) } t.Logf("✓ Data integrity verified: %d rows", origCount) t.Log("TEST PASSED: Handled large WAL successfully") } func TestConcurrentOperations(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } RequireBinaries(t) duration := GetTestDuration(t, 3*time.Minute) t.Logf("Testing: Multiple databases replicating concurrently (duration: %v)", duration) dbCount := 3 dbs := make([]*TestDB, dbCount) for i := 0; i < dbCount; i++ { dbs[i] = SetupTestDB(t, fmt.Sprintf("concurrent-%d", i)) defer dbs[i].Cleanup() } t.Log("[1] Creating databases...") for i, db := range dbs { if err := db.Create(); err != nil { t.Fatalf("Failed to create database %d: %v", i, err) } if err := CreateTestTable(t, db.Path); err != nil { t.Fatalf("Failed to create table for database %d: %v", i, err) } } t.Logf("✓ Created %d databases", dbCount) t.Log("[2] Starting Litestream for all databases...") for i, db := range dbs { if err := db.StartLitestream(); err != nil { t.Fatalf("Failed to start Litestream for database %d: %v", i, err) } time.Sleep(1 * time.Second) } t.Logf("✓ All Litestream instances running") t.Log("[3] Generating concurrent load...") ctx, cancel := context.WithTimeout(context.Background(), duration) defer cancel() done := make(chan error, dbCount) for i, db := range dbs { go func(idx int, database *TestDB) { config := DefaultLoadConfig() config.WriteRate = 50 config.Duration = duration config.Pattern = LoadPatternConstant config.Workers = 2 err := database.GenerateLoad(ctx, config.WriteRate, config.Duration, string(config.Pattern)) done <- err }(i, db) } for i := 0; i < dbCount; i++ { if err := <-done; err != nil && ctx.Err() == nil { t.Logf("Load generation %d had error: %v", i, err) } } t.Log("✓ Concurrent load complete") time.Sleep(5 * time.Second) t.Log("[4] Stopping all Litestream instances...") for _, db := range dbs { db.StopLitestream() } time.Sleep(2 * time.Second) t.Log("[5] Verifying all replicas...") for i, db := range dbs { fileCount, err := db.GetReplicaFileCount() if err != nil { t.Fatalf("Failed to check replica %d: %v", i, err) } if fileCount == 0 { t.Fatalf("Database %d has no replica files!", i) } t.Logf("✓ Database %d: %d replica files", i, fileCount) } t.Log("[6] Testing restore for all databases...") for i, db := range dbs { restoredPath := filepath.Join(db.TempDir, fmt.Sprintf("concurrent-restored-%d.db", i)) if err := db.Restore(restoredPath); err != nil { t.Fatalf("Restore failed for database %d: %v", i, err) } origCount, _ := db.GetRowCount("test_data") restoredDB := &TestDB{Path: restoredPath, t: t} restCount, _ := restoredDB.GetRowCount("test_data") if origCount != restCount { t.Fatalf("Database %d count mismatch: original=%d, restored=%d", i, origCount, restCount) } t.Logf("✓ Database %d verified: %d rows", i, origCount) } t.Log("TEST PASSED: Concurrent replication works correctly") } func TestBusyTimeout(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } RequireBinaries(t) t.Log("Testing: Database busy timeout handling") db := SetupTestDB(t, "busy-timeout") defer db.Cleanup() if err := db.Create(); err != nil { t.Fatalf("Failed to create database: %v", err) } t.Log("[1] Creating test data...") if err := CreateTestTable(t, db.Path); err != nil { t.Fatalf("Failed to create table: %v", err) } if err := InsertTestData(t, db.Path, 100); err != nil { t.Fatalf("Failed to insert test data: %v", err) } t.Log("✓ Created table with 100 rows") t.Log("[2] Starting Litestream...") if err := db.StartLitestream(); err != nil { t.Fatalf("Failed to start Litestream: %v", err) } time.Sleep(3 * time.Second) t.Log("[3] Simulating concurrent access with long transactions...") sqlDB, err := sql.Open("sqlite3", db.Path+"?_busy_timeout=5000") if err != nil { t.Fatalf("Failed to open database: %v", err) } defer sqlDB.Close() tx, err := sqlDB.Begin() if err != nil { t.Fatalf("Failed to begin transaction: %v", err) } for i := 0; i < 500; i++ { if _, err := tx.Exec( "INSERT INTO test_data (data, created_at) VALUES (?, ?)", fmt.Sprintf("busy test %d", i), time.Now().Unix(), ); err != nil { t.Fatalf("Failed to insert in transaction: %v", err) } if i%100 == 0 { time.Sleep(500 * time.Millisecond) } } if err := tx.Commit(); err != nil { t.Fatalf("Failed to commit transaction: %v", err) } t.Log("✓ Long transaction completed") time.Sleep(5 * time.Second) db.StopLitestream() time.Sleep(2 * time.Second) t.Log("[4] Checking for errors...") errors, err := db.CheckForErrors() if err != nil { t.Fatalf("Failed to check errors: %v", err) } if len(errors) > 0 { t.Logf("Found %d errors (may include busy timeout messages)", len(errors)) } t.Log("[5] Testing restore...") restoredPath := filepath.Join(db.TempDir, "busy-restored.db") if err := db.Restore(restoredPath); err != nil { t.Fatalf("Restore failed: %v", err) } t.Log("✓ Restore successful") origCount, err := db.GetRowCount("test_data") if err != nil { t.Fatalf("Failed to get original row count: %v", err) } restoredDB := &TestDB{Path: restoredPath, t: t} restCount, err := restoredDB.GetRowCount("test_data") if err != nil { t.Fatalf("Failed to get restored row count: %v", err) } if origCount != restCount { t.Fatalf("Count mismatch: original=%d, restored=%d", origCount, restCount) } t.Logf("✓ Data integrity verified: %d rows", origCount) t.Log("TEST PASSED: Busy timeout handled correctly") } func getFileSize(path string) (int64, error) { info, err := os.Stat(path) if err != nil { return 0, err } return info.Size(), nil } ================================================ FILE: tests/integration/directory_watcher_helpers.go ================================================ //go:build integration package integration import ( "context" "database/sql" "fmt" "os" "path/filepath" "strings" "sync" "testing" "time" _ "github.com/mattn/go-sqlite3" ) // DirWatchTestDB extends TestDB with directory-specific functionality type DirWatchTestDB struct { *TestDB DirPath string Pattern string Recursive bool Watch bool ReplicaPath string } // SetupDirectoryWatchTest creates a test environment for directory watching func SetupDirectoryWatchTest(t *testing.T, name string, pattern string, recursive bool) *DirWatchTestDB { t.Helper() baseDB := SetupTestDB(t, name) dirPath := filepath.Join(baseDB.TempDir, "databases") if err := os.MkdirAll(dirPath, 0755); err != nil { t.Fatalf("create databases directory: %v", err) } replicaPath := filepath.Join(baseDB.TempDir, "replica") return &DirWatchTestDB{ TestDB: baseDB, DirPath: dirPath, Pattern: pattern, Recursive: recursive, Watch: true, ReplicaPath: replicaPath, } } // CreateDirectoryWatchConfig generates YAML config for directory watching func (db *DirWatchTestDB) CreateDirectoryWatchConfig() (string, error) { configPath := filepath.Join(db.TempDir, "litestream.yml") config := fmt.Sprintf(`dbs: - dir: %s pattern: %q recursive: %t watch: %t replica: type: file path: %s `, filepath.ToSlash(db.DirPath), db.Pattern, db.Recursive, db.Watch, filepath.ToSlash(db.ReplicaPath), ) if err := os.WriteFile(configPath, []byte(config), 0644); err != nil { return "", fmt.Errorf("write config: %w", err) } db.ConfigPath = configPath return configPath, nil } // CreateDatabaseInDir creates a SQLite database with optional subdirectory func CreateDatabaseInDir(t *testing.T, dirPath, subDir, name string) string { t.Helper() dbDir := dirPath if subDir != "" { dbDir = filepath.Join(dirPath, subDir) if err := os.MkdirAll(dbDir, 0755); err != nil { t.Fatalf("create subdirectory %s: %v", subDir, err) } // Give directory monitor time to register watch on new subdirectory // to avoid race where database is created before watch is active time.Sleep(500 * time.Millisecond) } dbPath := filepath.Join(dbDir, name) sqlDB, err := sql.Open("sqlite3", dbPath) if err != nil { t.Fatalf("open database %s: %v", dbPath, err) } defer sqlDB.Close() if _, err := sqlDB.Exec("PRAGMA journal_mode=WAL"); err != nil { t.Fatalf("set WAL mode for %s: %v", dbPath, err) } // Create a simple table to make it a real database if _, err := sqlDB.Exec("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, data TEXT)"); err != nil { t.Fatalf("create table in %s: %v", dbPath, err) } return dbPath } // CreateDatabaseWithData creates a database with specified number of rows func CreateDatabaseWithData(t *testing.T, dbPath string, rowCount int) error { t.Helper() sqlDB, err := sql.Open("sqlite3", dbPath) if err != nil { return fmt.Errorf("open database: %w", err) } defer sqlDB.Close() if _, err := sqlDB.Exec("PRAGMA journal_mode=WAL"); err != nil { return fmt.Errorf("set WAL mode: %w", err) } if _, err := sqlDB.Exec("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, data TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP)"); err != nil { return fmt.Errorf("create table: %w", err) } // Insert data in batches tx, err := sqlDB.Begin() if err != nil { return fmt.Errorf("begin transaction: %w", err) } stmt, err := tx.Prepare("INSERT INTO test (data) VALUES (?)") if err != nil { tx.Rollback() return fmt.Errorf("prepare statement: %w", err) } for i := 0; i < rowCount; i++ { if _, err := stmt.Exec(fmt.Sprintf("test data %d", i)); err != nil { tx.Rollback() return fmt.Errorf("insert row %d: %w", i, err) } } if err := tx.Commit(); err != nil { return fmt.Errorf("commit transaction: %w", err) } return nil } // CreateFakeDatabase creates a file that looks like a database but isn't func CreateFakeDatabase(t *testing.T, dirPath, name string, content []byte) string { t.Helper() dbPath := filepath.Join(dirPath, name) if err := os.WriteFile(dbPath, content, 0644); err != nil { t.Fatalf("write fake database %s: %v", dbPath, err) } return dbPath } // WaitForDatabaseInReplica polls until database appears in replica // For directory watching, dbPath can be the full path or just the database name func WaitForDatabaseInReplica(t *testing.T, replicaPath, dbPath string, timeout time.Duration) error { t.Helper() // Replica structure: //ltx/0/*.ltx dbName := filepath.Base(dbPath) // Try to find the database in the replica directory // It could be at the root level or nested in subdirectories deadline := time.Now().Add(timeout) for time.Now().Before(deadline) { // Walk the replica directory to find the database found := false filepath.Walk(replicaPath, func(path string, info os.FileInfo, err error) error { if err != nil || found { return nil } // Check if this directory matches the database name and has LTX files if info.IsDir() && filepath.Base(path) == dbName { ltxDir := filepath.Join(path, "ltx", "0") if _, err := os.Stat(ltxDir); err == nil { entries, err := os.ReadDir(ltxDir) if err == nil { for _, entry := range entries { if strings.HasSuffix(entry.Name(), ".ltx") { relPath, _ := filepath.Rel(replicaPath, path) t.Logf("Database %s detected in replica at %s (found %s)", dbName, relPath, entry.Name()) found = true return nil } } } } } return nil }) if found { return nil } time.Sleep(100 * time.Millisecond) } return fmt.Errorf("database %s not found in replica after %v", dbName, timeout) } // VerifyDatabaseRemoved checks database no longer in replica (no new writes) func VerifyDatabaseRemoved(t *testing.T, replicaPath, dbPath string, timeout time.Duration) error { t.Helper() // Replica structure: //ltx/0/*.ltx dbName := filepath.Base(dbPath) ltxDir := filepath.Join(replicaPath, dbName, "ltx", "0") // Count initial LTX files (using existing countLTXFiles helper) initialCount := countLTXFiles(ltxDir) t.Logf("Initial LTX count for %s: %d", dbName, initialCount) // Wait and verify no new files are created time.Sleep(timeout) finalCount := countLTXFiles(ltxDir) if finalCount > initialCount { return fmt.Errorf("database %s still being replicated (%d -> %d LTX files)", dbName, initialCount, finalCount) } t.Logf("Database %s stopped replicating", dbName) return nil } // CountDatabasesInReplica counts distinct databases being replicated func CountDatabasesInReplica(replicaPath string) (int, error) { if _, err := os.Stat(replicaPath); os.IsNotExist(err) { return 0, nil } entries, err := os.ReadDir(replicaPath) if err != nil { return 0, err } count := 0 for _, entry := range entries { if !entry.IsDir() { continue } // Check if this database directory has LTX files ltxDir := filepath.Join(replicaPath, entry.Name(), "ltx", "0") if countLTXFiles(ltxDir) > 0 { count++ } } return count, nil } // StartContinuousWrites launches goroutine writing to database at specified rate func StartContinuousWrites(ctx context.Context, t *testing.T, dbPath string, writesPerSec int) (*sync.WaitGroup, context.CancelFunc, error) { t.Helper() sqlDB, err := sql.Open("sqlite3", dbPath) if err != nil { return nil, nil, fmt.Errorf("open database: %w", err) } if _, err := sqlDB.Exec("CREATE TABLE IF NOT EXISTS load_test (id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT, ts DATETIME DEFAULT CURRENT_TIMESTAMP)"); err != nil { sqlDB.Close() return nil, nil, fmt.Errorf("create table: %w", err) } ctx, cancel := context.WithCancel(ctx) wg := &sync.WaitGroup{} wg.Add(1) go func() { defer wg.Done() defer sqlDB.Close() ticker := time.NewTicker(time.Second / time.Duration(writesPerSec)) defer ticker.Stop() counter := 0 for { select { case <-ctx.Done(): return case <-ticker.C: counter++ if _, err := sqlDB.Exec("INSERT INTO load_test (data) VALUES (?)", fmt.Sprintf("data-%d", counter)); err != nil { if !strings.Contains(err.Error(), "database is locked") { t.Logf("Write error in %s: %v", filepath.Base(dbPath), err) } } } } }() return wg, cancel, nil } // CreateMultipleDatabasesConcurrently creates databases using goroutines func CreateMultipleDatabasesConcurrently(t *testing.T, dirPath string, count int, pattern string) []string { t.Helper() var wg sync.WaitGroup var mu sync.Mutex paths := make([]string, 0, count) for i := 0; i < count; i++ { wg.Add(1) go func(idx int) { defer wg.Done() name := fmt.Sprintf("db-%03d%s", idx, filepath.Ext(pattern)) dbPath := CreateDatabaseInDir(t, dirPath, "", name) mu.Lock() paths = append(paths, dbPath) mu.Unlock() }(i) } wg.Wait() return paths } // GetRowCount returns the number of rows in a test table func GetRowCount(dbPath, tableName string) (int, error) { sqlDB, err := sql.Open("sqlite3", dbPath) if err != nil { return 0, fmt.Errorf("open database: %w", err) } defer sqlDB.Close() var count int err = sqlDB.QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM %s", tableName)).Scan(&count) if err != nil { return 0, fmt.Errorf("query count: %w", err) } return count, nil } // Helper functions func getRelativeDBPath(dbPath, replicaBase string) (string, error) { // Extract just the database name (not the full path) // The replica structure mirrors the source directory structure return filepath.Base(dbPath), nil } func hasLTXFiles(dir string) (bool, error) { entries, err := os.ReadDir(dir) if err != nil { return false, err } for _, entry := range entries { if strings.HasSuffix(entry.Name(), ".ltx") { return true, nil } } return false, nil } // CheckForCriticalErrors returns errors from the log, filtering out known benign errors func CheckForCriticalErrors(t *testing.T, db *TestDB) ([]string, error) { t.Helper() allErrors, err := db.CheckForErrors() if err != nil { return nil, err } // Filter out known benign errors var criticalErrors []string for _, errLine := range allErrors { // Skip benign database removal errors that occur when closing databases if strings.Contains(errLine, "remove database from store") && (strings.Contains(errLine, "transaction has already been committed or rolled back") || strings.Contains(errLine, "no such file or directory") || strings.Contains(errLine, "disk I/O error")) { continue } criticalErrors = append(criticalErrors, errLine) } return criticalErrors, nil } ================================================ FILE: tests/integration/directory_watcher_test.go ================================================ //go:build integration package integration import ( "context" "os" "path/filepath" "testing" "time" ) // TestDirectoryWatcherBasicLifecycle tests the fundamental directory watcher functionality: // - Start with empty directory // - Create databases while Litestream is running // - Verify they are detected and replicated // - Delete databases and verify cleanup func TestDirectoryWatcherBasicLifecycle(t *testing.T) { RequireBinaries(t) // Use recursive:true because this test creates databases in subdirectories (tenant1/app.db, etc.) db := SetupDirectoryWatchTest(t, "dir-watch-lifecycle", "*.db", true) // Create config with directory watching configPath, err := db.CreateDirectoryWatchConfig() if err != nil { t.Fatalf("create config: %v", err) } t.Log("Starting Litestream with directory watching...") if err := db.StartLitestreamWithConfig(configPath); err != nil { t.Fatalf("start litestream: %v", err) } defer db.StopLitestream() // Give Litestream time to start time.Sleep(2 * time.Second) // Step 1: Create 2 databases in separate tenant directories t.Log("Creating databases in separate directories...") tenant1DB := CreateDatabaseInDir(t, db.DirPath, "tenant1", "app.db") tenant2DB := CreateDatabaseInDir(t, db.DirPath, "tenant2", "app.db") // Wait for detection and replication t.Log("Waiting for database detection...") if err := WaitForDatabaseInReplica(t, db.ReplicaPath, tenant1DB, 5*time.Second); err != nil { t.Fatalf("tenant1 database not detected: %v", err) } if err := WaitForDatabaseInReplica(t, db.ReplicaPath, tenant2DB, 5*time.Second); err != nil { t.Fatalf("tenant2 database not detected: %v", err) } // Step 2: Add data to both databases t.Log("Adding data to databases...") if err := CreateDatabaseWithData(t, tenant1DB, 100); err != nil { t.Fatalf("add data to tenant1: %v", err) } if err := CreateDatabaseWithData(t, tenant2DB, 100); err != nil { t.Fatalf("add data to tenant2: %v", err) } // Wait for replication time.Sleep(3 * time.Second) // Step 3: Create 3 more databases at intervals t.Log("Creating additional databases at intervals...") db3 := CreateDatabaseInDir(t, db.DirPath, "tenant3", "app.db") time.Sleep(1 * time.Second) db4 := CreateDatabaseInDir(t, db.DirPath, "", "standalone.db") time.Sleep(1 * time.Second) db5 := CreateDatabaseInDir(t, db.DirPath, "tenant4", "data.db") // Wait for all to be detected t.Log("Verifying all databases detected...") if err := WaitForDatabaseInReplica(t, db.ReplicaPath, db3, 5*time.Second); err != nil { t.Fatalf("tenant3 database not detected: %v", err) } if err := WaitForDatabaseInReplica(t, db.ReplicaPath, db4, 5*time.Second); err != nil { t.Fatalf("standalone database not detected: %v", err) } if err := WaitForDatabaseInReplica(t, db.ReplicaPath, db5, 5*time.Second); err != nil { t.Fatalf("tenant4 database not detected: %v", err) } // Step 4: Delete one database and verify cleanup t.Log("Deleting database and verifying cleanup...") if err := os.Remove(db4); err != nil { t.Fatalf("remove database: %v", err) } // Wait and verify no more replication if err := VerifyDatabaseRemoved(t, db.ReplicaPath, db4, 3*time.Second); err != nil { t.Fatalf("database still replicating after removal: %v", err) } // Step 5: Verify no critical errors in log t.Log("Checking for errors...") errors, err := CheckForCriticalErrors(t, db.TestDB) if err != nil { t.Fatalf("check errors: %v", err) } if len(errors) > 0 { t.Fatalf("found critical errors in log: %v", errors) } t.Log("✓ Basic lifecycle test passed") } // TestDirectoryWatcherRapidConcurrentCreation tests race conditions with rapid database creation func TestDirectoryWatcherRapidConcurrentCreation(t *testing.T) { RequireBinaries(t) db := SetupDirectoryWatchTest(t, "dir-watch-concurrent", "*.db", false) configPath, err := db.CreateDirectoryWatchConfig() if err != nil { t.Fatalf("create config: %v", err) } t.Log("Starting Litestream with directory watching...") if err := db.StartLitestreamWithConfig(configPath); err != nil { t.Fatalf("start litestream: %v", err) } defer db.StopLitestream() time.Sleep(2 * time.Second) // Create 20 databases simultaneously t.Log("Creating 20 databases concurrently...") dbPaths := CreateMultipleDatabasesConcurrently(t, db.DirPath, 20, "*.db") // Wait for all databases to be detected t.Log("Verifying all databases detected...") for i, dbPath := range dbPaths { if err := WaitForDatabaseInReplica(t, db.ReplicaPath, dbPath, 10*time.Second); err != nil { t.Fatalf("database %d (%s) not detected: %v", i, filepath.Base(dbPath), err) } } // Count databases in replica count, err := CountDatabasesInReplica(db.ReplicaPath) if err != nil { t.Fatalf("count databases: %v", err) } if count != 20 { t.Fatalf("expected 20 databases in replica, got %d", count) } // Check for errors (especially duplicate registrations or race conditions) errors, err := db.CheckForErrors() if err != nil { t.Fatalf("check errors: %v", err) } if len(errors) > 0 { t.Fatalf("found errors in log (possible race conditions): %v", errors) } t.Log("✓ Concurrent creation test passed") } // TestDirectoryWatcherRecursiveMode tests recursive directory scanning func TestDirectoryWatcherRecursiveMode(t *testing.T) { RequireBinaries(t) db := SetupDirectoryWatchTest(t, "dir-watch-recursive", "*.db", true) configPath, err := db.CreateDirectoryWatchConfig() if err != nil { t.Fatalf("create config: %v", err) } t.Log("Starting Litestream with recursive directory watching...") if err := db.StartLitestreamWithConfig(configPath); err != nil { t.Fatalf("start litestream: %v", err) } time.Sleep(2 * time.Second) // Create nested directory structure t.Log("Creating nested directory structure...") db1 := CreateDatabaseInDir(t, db.DirPath, "", "db1.db") // root/db1.db db2 := CreateDatabaseInDir(t, db.DirPath, "level1", "db2.db") // root/level1/db2.db // Verify first two detected t.Log("Verifying databases detected...") for i, dbPath := range []string{db1, db2} { if err := WaitForDatabaseInReplica(t, db.ReplicaPath, dbPath, 10*time.Second); err != nil { t.Fatalf("database %d (%s) not detected: %v", i+1, filepath.Base(dbPath), err) } } // Try deeper nesting (may be slower to detect) t.Log("Creating deeply nested database...") db3 := CreateDatabaseInDir(t, db.DirPath, "level1/level2", "db3.db") // root/level1/level2/db3.db // Give more time for deeply nested database if err := WaitForDatabaseInReplica(t, db.ReplicaPath, db3, 15*time.Second); err != nil { t.Logf("Warning: deeply nested database (2 levels) not detected: %v", err) // Don't fail the test - recursive watching of deeply nested dirs may have limitations } // Create new subdirectory after start t.Log("Creating new subdirectory dynamically...") newDir := filepath.Join(db.DirPath, "dynamic") if err := os.MkdirAll(newDir, 0755); err != nil { t.Fatalf("create dynamic dir: %v", err) } time.Sleep(500 * time.Millisecond) // Allow directory watch to register db5 := CreateDatabaseInDir(t, db.DirPath, "dynamic", "db5.db") if err := WaitForDatabaseInReplica(t, db.ReplicaPath, db5, 10*time.Second); err != nil { t.Fatalf("dynamically created database not detected: %v", err) } // Stop Litestream before deleting directories to release file handles t.Log("Stopping Litestream before directory deletion...") db.StopLitestream() // Delete entire subdirectory (now safe since Litestream released handles) t.Log("Deleting subdirectory with databases...") level1Dir := filepath.Join(db.DirPath, "level1") if err := os.RemoveAll(level1Dir); err != nil { t.Fatalf("remove level1 directory: %v", err) } t.Log("✓ Recursive mode test passed") } // TestDirectoryWatcherPatternMatching tests glob pattern matching func TestDirectoryWatcherPatternMatching(t *testing.T) { RequireBinaries(t) db := SetupDirectoryWatchTest(t, "dir-watch-pattern", "*.db", false) configPath, err := db.CreateDirectoryWatchConfig() if err != nil { t.Fatalf("create config: %v", err) } t.Log("Starting Litestream with pattern '*.db'...") if err := db.StartLitestreamWithConfig(configPath); err != nil { t.Fatalf("start litestream: %v", err) } defer db.StopLitestream() time.Sleep(2 * time.Second) // Create files with different extensions t.Log("Creating files with various patterns...") matchDB := CreateDatabaseInDir(t, db.DirPath, "", "test.db") // Should match noMatchSQLite := CreateDatabaseInDir(t, db.DirPath, "", "test.sqlite") // Should NOT match noMatchBackup := CreateFakeDatabase(t, db.DirPath, "test.db.backup", []byte{}) // Should NOT match // Also create WAL and SHM files (should be ignored) CreateFakeDatabase(t, db.DirPath, "test.db-wal", []byte{}) CreateFakeDatabase(t, db.DirPath, "test.db-shm", []byte{}) // Wait and verify only .db file is replicated t.Log("Verifying pattern matching...") if err := WaitForDatabaseInReplica(t, db.ReplicaPath, matchDB, 5*time.Second); err != nil { t.Fatalf("*.db file should be detected: %v", err) } // Give time for other files to be processed (they shouldn't be) time.Sleep(3 * time.Second) // Count - should only have 1 database count, err := CountDatabasesInReplica(db.ReplicaPath) if err != nil { t.Fatalf("count databases: %v", err) } if count != 1 { t.Fatalf("expected 1 database in replica, got %d (pattern matching failed)", count) } // Verify .sqlite and .db.backup files were not added if err := WaitForDatabaseInReplica(t, db.ReplicaPath, noMatchSQLite, 2*time.Second); err == nil { t.Fatal("*.sqlite file should NOT be detected with *.db pattern") } if err := WaitForDatabaseInReplica(t, db.ReplicaPath, noMatchBackup, 2*time.Second); err == nil { t.Fatal("*.db.backup file should NOT be detected with *.db pattern") } t.Log("✓ Pattern matching test passed") } // TestDirectoryWatcherNonSQLiteRejection tests that non-SQLite files are rejected func TestDirectoryWatcherNonSQLiteRejection(t *testing.T) { RequireBinaries(t) db := SetupDirectoryWatchTest(t, "dir-watch-nonsqlite", "*.db", false) configPath, err := db.CreateDirectoryWatchConfig() if err != nil { t.Fatalf("create config: %v", err) } t.Log("Starting Litestream...") if err := db.StartLitestreamWithConfig(configPath); err != nil { t.Fatalf("start litestream: %v", err) } defer db.StopLitestream() time.Sleep(2 * time.Second) // Create fake database files t.Log("Creating non-SQLite files...") fakeDB := CreateFakeDatabase(t, db.DirPath, "fake.db", []byte("this is not a sqlite file")) emptyDB := CreateFakeDatabase(t, db.DirPath, "empty.db", []byte{}) textDB := CreateFakeDatabase(t, db.DirPath, "text.db", []byte("SQLite format 2\x00")) // Wrong version // Create one valid SQLite database validDB := CreateDatabaseInDir(t, db.DirPath, "", "valid.db") // Wait for valid database t.Log("Verifying only valid SQLite database is detected...") if err := WaitForDatabaseInReplica(t, db.ReplicaPath, validDB, 5*time.Second); err != nil { t.Fatalf("valid database should be detected: %v", err) } // Wait to ensure fake databases are not added time.Sleep(3 * time.Second) // Should only have 1 database count, err := CountDatabasesInReplica(db.ReplicaPath) if err != nil { t.Fatalf("count databases: %v", err) } if count != 1 { t.Fatalf("expected 1 database in replica, got %d (non-SQLite files were not rejected)", count) } // Verify fake files were not added for _, fakePath := range []string{fakeDB, emptyDB, textDB} { if err := WaitForDatabaseInReplica(t, db.ReplicaPath, fakePath, 1*time.Second); err == nil { t.Fatalf("non-SQLite file %s should NOT be replicated", filepath.Base(fakePath)) } } t.Log("✓ Non-SQLite rejection test passed") } // TestDirectoryWatcherActiveConnections tests behavior with databases that are actively being used func TestDirectoryWatcherActiveConnections(t *testing.T) { RequireBinaries(t) db := SetupDirectoryWatchTest(t, "dir-watch-active", "*.db", false) configPath, err := db.CreateDirectoryWatchConfig() if err != nil { t.Fatalf("create config: %v", err) } // Create database with active connection before starting Litestream t.Log("Creating database with active connection...") db1Path := CreateDatabaseInDir(t, db.DirPath, "", "active.db") // Start continuous writes ctx := context.Background() wg, cancel, err := StartContinuousWrites(ctx, t, db1Path, 10) // 10 writes/sec if err != nil { t.Fatalf("start writes: %v", err) } defer cancel() // Start Litestream t.Log("Starting Litestream with active database...") if err := db.StartLitestreamWithConfig(configPath); err != nil { t.Fatalf("start litestream: %v", err) } defer db.StopLitestream() time.Sleep(2 * time.Second) // Verify database is detected despite active connection t.Log("Verifying active database is detected...") if err := WaitForDatabaseInReplica(t, db.ReplicaPath, db1Path, 10*time.Second); err != nil { t.Fatalf("active database not detected: %v", err) } // Create second database and start writing to it t.Log("Creating second database with writes...") db2Path := CreateDatabaseInDir(t, db.DirPath, "", "active2.db") wg2, cancel2, err := StartContinuousWrites(ctx, t, db2Path, 5) if err != nil { t.Fatalf("start writes for db2: %v", err) } defer cancel2() // Verify second database detected if err := WaitForDatabaseInReplica(t, db.ReplicaPath, db2Path, 10*time.Second); err != nil { t.Fatalf("second active database not detected: %v", err) } // Let writes continue for a bit t.Log("Letting writes continue for 5 seconds...") time.Sleep(5 * time.Second) // Stop writers cancel() cancel2() wg.Wait() wg2.Wait() // Verify both databases are still replicated count, err := CountDatabasesInReplica(db.ReplicaPath) if err != nil { t.Fatalf("count databases: %v", err) } if count != 2 { t.Fatalf("expected 2 databases in replica, got %d", count) } errors, err := CheckForCriticalErrors(t, db.TestDB) if err != nil { t.Fatalf("check errors: %v", err) } if len(errors) > 0 { t.Fatalf("found critical errors with active connections: %v", errors) } t.Log("✓ Active connections test passed") } // TestDirectoryWatcherRestartBehavior tests behavior across Litestream restarts func TestDirectoryWatcherRestartBehavior(t *testing.T) { RequireBinaries(t) db := SetupDirectoryWatchTest(t, "dir-watch-restart", "*.db", false) // Create 3 databases before starting t.Log("Creating initial databases...") db1 := CreateDatabaseInDir(t, db.DirPath, "", "db1.db") db2 := CreateDatabaseInDir(t, db.DirPath, "", "db2.db") db3 := CreateDatabaseInDir(t, db.DirPath, "", "db3.db") configPath, err := db.CreateDirectoryWatchConfig() if err != nil { t.Fatalf("create config: %v", err) } // First start t.Log("Starting Litestream (first time)...") if err := db.StartLitestreamWithConfig(configPath); err != nil { t.Fatalf("start litestream: %v", err) } time.Sleep(3 * time.Second) // Verify all 3 detected for _, dbPath := range []string{db1, db2, db3} { if err := WaitForDatabaseInReplica(t, db.ReplicaPath, dbPath, 5*time.Second); err != nil { t.Fatalf("database %s not detected: %v", filepath.Base(dbPath), err) } } // Add 2 more databases dynamically t.Log("Adding databases dynamically...") db4 := CreateDatabaseInDir(t, db.DirPath, "", "db4.db") db5 := CreateDatabaseInDir(t, db.DirPath, "", "db5.db") time.Sleep(3 * time.Second) // Verify new databases detected for _, dbPath := range []string{db4, db5} { if err := WaitForDatabaseInReplica(t, db.ReplicaPath, dbPath, 5*time.Second); err != nil { t.Fatalf("dynamically added database %s not detected: %v", filepath.Base(dbPath), err) } } // Stop Litestream t.Log("Stopping Litestream...") if err := db.StopLitestream(); err != nil { t.Fatalf("stop litestream: %v", err) } // Add one more database while stopped t.Log("Adding database while Litestream is stopped...") db6 := CreateDatabaseInDir(t, db.DirPath, "", "db6.db") time.Sleep(2 * time.Second) // Restart Litestream t.Log("Restarting Litestream...") if err := db.StartLitestreamWithConfig(configPath); err != nil { t.Fatalf("restart litestream: %v", err) } defer db.StopLitestream() time.Sleep(3 * time.Second) // Verify all 6 databases are now being replicated t.Log("Verifying all databases detected after restart...") for i, dbPath := range []string{db1, db2, db3, db4, db5, db6} { if err := WaitForDatabaseInReplica(t, db.ReplicaPath, dbPath, 10*time.Second); err != nil { t.Fatalf("database %d (%s) not detected after restart: %v", i+1, filepath.Base(dbPath), err) } } // Add one more dynamically after restart t.Log("Adding database after restart...") db7 := CreateDatabaseInDir(t, db.DirPath, "", "db7.db") if err := WaitForDatabaseInReplica(t, db.ReplicaPath, db7, 5*time.Second); err != nil { t.Fatalf("database added after restart not detected: %v", err) } // Final count - should have 7 databases count, err := CountDatabasesInReplica(db.ReplicaPath) if err != nil { t.Fatalf("count databases: %v", err) } if count != 7 { t.Fatalf("expected 7 databases in replica, got %d", count) } t.Log("✓ Restart behavior test passed") } // TestDirectoryWatcherRenameOperations tests file rename handling func TestDirectoryWatcherRenameOperations(t *testing.T) { RequireBinaries(t) db := SetupDirectoryWatchTest(t, "dir-watch-rename", "*.db", false) configPath, err := db.CreateDirectoryWatchConfig() if err != nil { t.Fatalf("create config: %v", err) } t.Log("Starting Litestream...") if err := db.StartLitestreamWithConfig(configPath); err != nil { t.Fatalf("start litestream: %v", err) } defer db.StopLitestream() time.Sleep(2 * time.Second) // Create database t.Log("Creating database...") originalPath := CreateDatabaseInDir(t, db.DirPath, "", "original.db") // Wait for replication if err := WaitForDatabaseInReplica(t, db.ReplicaPath, originalPath, 5*time.Second); err != nil { t.Fatalf("original database not detected: %v", err) } time.Sleep(2 * time.Second) // Rename database t.Log("Renaming database...") renamedPath := filepath.Join(db.DirPath, "renamed.db") if err := os.Rename(originalPath, renamedPath); err != nil { t.Fatalf("rename database: %v", err) } // Wait for new name to be detected t.Log("Waiting for renamed database to be detected...") if err := WaitForDatabaseInReplica(t, db.ReplicaPath, renamedPath, 10*time.Second); err != nil { t.Fatalf("renamed database not detected: %v", err) } // Verify old database stopped replicating t.Log("Verifying original database stopped replicating...") if err := VerifyDatabaseRemoved(t, db.ReplicaPath, originalPath, 3*time.Second); err != nil { t.Logf("Warning: original may still be replicating: %v", err) } t.Log("✓ Rename operations test passed") } // TestDirectoryWatcherLoadWithWrites tests directory watching with concurrent database writes func TestDirectoryWatcherLoadWithWrites(t *testing.T) { if testing.Short() { t.Skip("skipping load test in short mode") } RequireBinaries(t) db := SetupDirectoryWatchTest(t, "dir-watch-load", "*.db", false) // Create 3 databases with data t.Log("Creating initial databases with data...") db1 := CreateDatabaseInDir(t, db.DirPath, "", "db1.db") db2 := CreateDatabaseInDir(t, db.DirPath, "", "db2.db") db3 := CreateDatabaseInDir(t, db.DirPath, "", "db3.db") for _, dbPath := range []string{db1, db2, db3} { if err := CreateDatabaseWithData(t, dbPath, 50); err != nil { t.Fatalf("create database with data: %v", err) } } configPath, err := db.CreateDirectoryWatchConfig() if err != nil { t.Fatalf("create config: %v", err) } t.Log("Starting Litestream...") if err := db.StartLitestreamWithConfig(configPath); err != nil { t.Fatalf("start litestream: %v", err) } defer db.StopLitestream() time.Sleep(3 * time.Second) // Start continuous writes to all 3 databases ctx := context.Background() t.Log("Starting continuous writes to all databases...") wg1, cancel1, _ := StartContinuousWrites(ctx, t, db1, 20) wg2, cancel2, _ := StartContinuousWrites(ctx, t, db2, 15) wg3, cancel3, _ := StartContinuousWrites(ctx, t, db3, 10) defer func() { cancel1() cancel2() cancel3() wg1.Wait() wg2.Wait() wg3.Wait() }() // Wait a bit for writes to start time.Sleep(2 * time.Second) // While writes are happening, create 2 new databases t.Log("Creating new databases while writes are ongoing...") db4 := CreateDatabaseInDir(t, db.DirPath, "", "db4.db") db5 := CreateDatabaseInDir(t, db.DirPath, "", "db5.db") // Start writes on new databases wg4, cancel4, _ := StartContinuousWrites(ctx, t, db4, 10) wg5, cancel5, _ := StartContinuousWrites(ctx, t, db5, 10) defer func() { cancel4() cancel5() wg4.Wait() wg5.Wait() }() // Verify all databases detected for i, dbPath := range []string{db1, db2, db3, db4, db5} { if err := WaitForDatabaseInReplica(t, db.ReplicaPath, dbPath, 10*time.Second); err != nil { t.Fatalf("database %d not detected: %v", i+1, err) } } // Let writes continue t.Log("Running writes for 10 seconds...") time.Sleep(10 * time.Second) // Stop all writes cancel1() cancel2() cancel3() cancel4() cancel5() wg1.Wait() wg2.Wait() wg3.Wait() wg4.Wait() wg5.Wait() // Wait for final replication time.Sleep(3 * time.Second) // Verify all 5 databases are in replica count, err := CountDatabasesInReplica(db.ReplicaPath) if err != nil { t.Fatalf("count databases: %v", err) } if count != 5 { t.Fatalf("expected 5 databases in replica, got %d", count) } // Check for errors errors, err := CheckForCriticalErrors(t, db.TestDB) if err != nil { t.Fatalf("check errors: %v", err) } if len(errors) > 0 { t.Fatalf("found critical errors during load test: %v", errors) } t.Log("✓ Load with writes test passed") } ================================================ FILE: tests/integration/docker_helpers.go ================================================ //go:build integration package integration import ( "fmt" "os" "os/exec" "strings" "testing" "time" ) func RequireDocker(t *testing.T) { t.Helper() if err := exec.Command("docker", "version").Run(); err != nil { t.Skip("Docker is not available, skipping test") } } func StartMinioTestContainer(t *testing.T) (string, string) { t.Helper() name := fmt.Sprintf("litestream-minio-%d", time.Now().UnixNano()) exec.Command("docker", "rm", "-f", name).Run() args := []string{ "run", "-d", "--name", name, "-p", "0:9000", "-e", "MINIO_ROOT_USER=minioadmin", "-e", "MINIO_ROOT_PASSWORD=minioadmin", "-e", "MINIO_DOMAIN=s3-accesspoint.127.0.0.1.nip.io", "minio/minio", "server", "/data", } containerID := runDockerCommand(t, args...) portInfo := runDockerCommand(t, "port", name, "9000/tcp") hostPort := parseDockerPort(t, portInfo) time.Sleep(5 * time.Second) t.Logf("Started MinIO container %s (%s) on port %s", name, containerID[:12], hostPort) return name, fmt.Sprintf("http://localhost:%s", hostPort) } func StopMinioTestContainer(t *testing.T, name string) { t.Helper() if name == "" { return } if os.Getenv("SOAK_KEEP_TEMP") != "" { t.Logf("SOAK_KEEP_TEMP set, preserving MinIO container: %s", name) return } exec.Command("docker", "rm", "-f", name).Run() } func runDockerCommand(t *testing.T, args ...string) string { t.Helper() cmd := exec.Command("docker", args...) output, err := cmd.CombinedOutput() if err != nil { t.Fatalf("docker %s failed: %v\nOutput: %s", strings.Join(args, " "), err, string(output)) } return strings.TrimSpace(string(output)) } func parseDockerPort(t *testing.T, portInfo string) string { t.Helper() idx := strings.LastIndex(portInfo, ":") if idx == -1 || idx == len(portInfo)-1 { t.Fatalf("unexpected docker port output: %s", portInfo) } return portInfo[idx+1:] } ================================================ FILE: tests/integration/fixtures.go ================================================ //go:build integration package integration import ( "crypto/rand" "database/sql" "fmt" "time" _ "github.com/mattn/go-sqlite3" ) type LoadPattern string const ( LoadPatternConstant LoadPattern = "constant" LoadPatternBurst LoadPattern = "burst" LoadPatternRandom LoadPattern = "random" LoadPatternWave LoadPattern = "wave" ) type LoadConfig struct { WriteRate int Duration time.Duration Pattern LoadPattern PayloadSize int ReadRatio float64 Workers int } func DefaultLoadConfig() *LoadConfig { return &LoadConfig{ WriteRate: 100, Duration: 1 * time.Minute, Pattern: LoadPatternConstant, PayloadSize: 1024, ReadRatio: 0.2, Workers: 1, } } type PopulateConfig struct { TargetSize string RowSize int BatchSize int TableCount int IndexRatio float64 PageSize int } func DefaultPopulateConfig() *PopulateConfig { return &PopulateConfig{ TargetSize: "100MB", RowSize: 1024, BatchSize: 1000, TableCount: 1, IndexRatio: 0.2, PageSize: 4096, } } func CreateComplexTestSchema(db *sql.DB) error { schemas := []string{ `CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL UNIQUE, email TEXT NOT NULL, created_at INTEGER NOT NULL )`, `CREATE TABLE IF NOT EXISTS posts ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL, title TEXT NOT NULL, content TEXT, created_at INTEGER NOT NULL, FOREIGN KEY (user_id) REFERENCES users(id) )`, `CREATE TABLE IF NOT EXISTS comments ( id INTEGER PRIMARY KEY AUTOINCREMENT, post_id INTEGER NOT NULL, user_id INTEGER NOT NULL, content TEXT NOT NULL, created_at INTEGER NOT NULL, FOREIGN KEY (post_id) REFERENCES posts(id), FOREIGN KEY (user_id) REFERENCES users(id) )`, `CREATE INDEX IF NOT EXISTS idx_posts_user_id ON posts(user_id)`, `CREATE INDEX IF NOT EXISTS idx_posts_created_at ON posts(created_at)`, `CREATE INDEX IF NOT EXISTS idx_comments_post_id ON comments(post_id)`, `CREATE INDEX IF NOT EXISTS idx_comments_created_at ON comments(created_at)`, } for _, schema := range schemas { if _, err := db.Exec(schema); err != nil { return fmt.Errorf("execute schema: %w", err) } } return nil } func PopulateComplexTestData(db *sql.DB, userCount, postsPerUser, commentsPerPost int) error { tx, err := db.Begin() if err != nil { return fmt.Errorf("begin transaction: %w", err) } defer tx.Rollback() userStmt, err := tx.Prepare("INSERT INTO users (username, email, created_at) VALUES (?, ?, ?)") if err != nil { return fmt.Errorf("prepare user statement: %w", err) } defer userStmt.Close() postStmt, err := tx.Prepare("INSERT INTO posts (user_id, title, content, created_at) VALUES (?, ?, ?, ?)") if err != nil { return fmt.Errorf("prepare post statement: %w", err) } defer postStmt.Close() commentStmt, err := tx.Prepare("INSERT INTO comments (post_id, user_id, content, created_at) VALUES (?, ?, ?, ?)") if err != nil { return fmt.Errorf("prepare comment statement: %w", err) } defer commentStmt.Close() now := time.Now().Unix() for u := 1; u <= userCount; u++ { userResult, err := userStmt.Exec( fmt.Sprintf("user%d", u), fmt.Sprintf("user%d@test.com", u), now, ) if err != nil { return fmt.Errorf("insert user: %w", err) } userID, err := userResult.LastInsertId() if err != nil { return fmt.Errorf("get user id: %w", err) } for p := 1; p <= postsPerUser; p++ { postResult, err := postStmt.Exec( userID, fmt.Sprintf("Post %d from user %d", p, u), generateRandomContent(100), now, ) if err != nil { return fmt.Errorf("insert post: %w", err) } postID, err := postResult.LastInsertId() if err != nil { return fmt.Errorf("get post id: %w", err) } for c := 1; c <= commentsPerPost; c++ { commentUserID := (u + c) % userCount if commentUserID == 0 { commentUserID = userCount } _, err := commentStmt.Exec( postID, commentUserID, generateRandomContent(50), now, ) if err != nil { return fmt.Errorf("insert comment: %w", err) } } } } return tx.Commit() } func generateRandomContent(length int) string { const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 " b := make([]byte, length) rand.Read(b) for i := range b { b[i] = charset[int(b[i])%len(charset)] } return string(b) } type TestScenario struct { Name string Description string Setup func(*sql.DB) error Validate func(*sql.DB, *sql.DB) error } func LargeWALScenario() *TestScenario { return &TestScenario{ Name: "Large WAL", Description: "Generate large WAL file to test handling", Setup: func(db *sql.DB) error { if _, err := db.Exec(` CREATE TABLE test_wal ( id INTEGER PRIMARY KEY AUTOINCREMENT, data BLOB ) `); err != nil { return err } data := make([]byte, 10*1024) rand.Read(data) for i := 0; i < 10000; i++ { if _, err := db.Exec("INSERT INTO test_wal (data) VALUES (?)", data); err != nil { return err } } return nil }, Validate: func(source, restored *sql.DB) error { var sourceCount, restoredCount int if err := source.QueryRow("SELECT COUNT(*) FROM test_wal").Scan(&sourceCount); err != nil { return fmt.Errorf("query source: %w", err) } if err := restored.QueryRow("SELECT COUNT(*) FROM test_wal").Scan(&restoredCount); err != nil { return fmt.Errorf("query restored: %w", err) } if sourceCount != restoredCount { return fmt.Errorf("count mismatch: source=%d, restored=%d", sourceCount, restoredCount) } return nil }, } } func RapidCheckpointsScenario() *TestScenario { return &TestScenario{ Name: "Rapid Checkpoints", Description: "Test rapid checkpoint operations", Setup: func(db *sql.DB) error { if _, err := db.Exec(` CREATE TABLE test_checkpoints ( id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT, timestamp INTEGER ) `); err != nil { return err } for i := 0; i < 1000; i++ { if _, err := db.Exec( "INSERT INTO test_checkpoints (data, timestamp) VALUES (?, ?)", fmt.Sprintf("data %d", i), time.Now().Unix(), ); err != nil { return err } if i%100 == 0 { if _, err := db.Exec("PRAGMA wal_checkpoint(TRUNCATE)"); err != nil { return err } } } return nil }, Validate: func(source, restored *sql.DB) error { var sourceCount, restoredCount int if err := source.QueryRow("SELECT COUNT(*) FROM test_checkpoints").Scan(&sourceCount); err != nil { return fmt.Errorf("query source: %w", err) } if err := restored.QueryRow("SELECT COUNT(*) FROM test_checkpoints").Scan(&restoredCount); err != nil { return fmt.Errorf("query restored: %w", err) } if sourceCount != restoredCount { return fmt.Errorf("count mismatch: source=%d, restored=%d", sourceCount, restoredCount) } return nil }, } } ================================================ FILE: tests/integration/helpers.go ================================================ //go:build integration package integration import ( "bytes" "context" "database/sql" "fmt" "io" "os" "os/exec" "path/filepath" "runtime" "strings" "testing" "time" _ "github.com/mattn/go-sqlite3" "github.com/benbjohnson/litestream" ) type TestDB struct { Path string ReplicaPath string ReplicaURL string ReplicaEnv []string ConfigPath string TempDir string LitestreamCmd *exec.Cmd LitestreamPID int t *testing.T } // getBinaryPath returns the cross-platform path to a binary. // On Windows, it adds the .exe extension. func getBinaryPath(name string) string { binPath := filepath.Join("..", "..", "bin", name) if runtime.GOOS == "windows" { binPath += ".exe" } return binPath } func streamCommandOutput() bool { v := strings.ToLower(strings.TrimSpace(os.Getenv("SOAK_DEBUG"))) switch v { case "", "0", "false", "off", "no": return false default: return true } } func configureCmdIO(cmd *exec.Cmd) (bool, *bytes.Buffer, *bytes.Buffer) { stream := streamCommandOutput() stdoutBuf := &bytes.Buffer{} stderrBuf := &bytes.Buffer{} if stream { cmd.Stdout = io.MultiWriter(os.Stdout, stdoutBuf) cmd.Stderr = io.MultiWriter(os.Stderr, stderrBuf) } else { cmd.Stdout = stdoutBuf cmd.Stderr = stderrBuf } return stream, stdoutBuf, stderrBuf } func combinedOutput(stdoutBuf, stderrBuf *bytes.Buffer) string { var sb strings.Builder if stdoutBuf != nil && stdoutBuf.Len() > 0 { sb.Write(stdoutBuf.Bytes()) } if stderrBuf != nil && stderrBuf.Len() > 0 { sb.Write(stderrBuf.Bytes()) } return strings.TrimSpace(sb.String()) } func SetupTestDB(t *testing.T, name string) *TestDB { t.Helper() var tempDir string if os.Getenv("SOAK_KEEP_TEMP") != "" { dir, err := os.MkdirTemp("", fmt.Sprintf("litestream-%s-", name)) if err != nil { t.Fatalf("create temp dir: %v", err) } tempDir = dir t.Cleanup(func() { t.Logf("SOAK_KEEP_TEMP set, preserving test artifacts at: %s", tempDir) }) } else { tempDir = t.TempDir() } dbPath := filepath.Join(tempDir, fmt.Sprintf("%s.db", name)) replicaPath := filepath.Join(tempDir, "replica") return &TestDB{ Path: dbPath, ReplicaPath: replicaPath, ReplicaURL: fmt.Sprintf("file://%s", filepath.ToSlash(replicaPath)), TempDir: tempDir, t: t, } } func (db *TestDB) Create() error { sqlDB, err := sql.Open("sqlite3", db.Path) if err != nil { return fmt.Errorf("open database: %w", err) } defer sqlDB.Close() if _, err := sqlDB.Exec("PRAGMA journal_mode=WAL"); err != nil { return fmt.Errorf("set WAL mode: %w", err) } return nil } func (db *TestDB) CreateWithPageSize(pageSize int) error { sqlDB, err := sql.Open("sqlite3", db.Path) if err != nil { return fmt.Errorf("open database: %w", err) } defer sqlDB.Close() if _, err := sqlDB.Exec(fmt.Sprintf("PRAGMA page_size = %d", pageSize)); err != nil { return fmt.Errorf("set page size: %w", err) } if _, err := sqlDB.Exec("PRAGMA journal_mode=WAL"); err != nil { return fmt.Errorf("set WAL mode: %w", err) } return nil } func (db *TestDB) Populate(targetSize string) error { cmd := exec.Command(getBinaryPath("litestream-test"), "populate", "-db", db.Path, "-target-size", targetSize, ) _, stdoutBuf, stderrBuf := configureCmdIO(cmd) db.t.Logf("Populating database to %s...", targetSize) if err := cmd.Run(); err != nil { if output := combinedOutput(stdoutBuf, stderrBuf); output != "" { return fmt.Errorf("populate failed: %w\nOutput: %s", err, output) } return fmt.Errorf("populate failed: %w", err) } return nil } func (db *TestDB) PopulateWithOptions(targetSize string, pageSize int, rowSize int) error { cmd := exec.Command(getBinaryPath("litestream-test"), "populate", "-db", db.Path, "-target-size", targetSize, "-page-size", fmt.Sprintf("%d", pageSize), "-row-size", fmt.Sprintf("%d", rowSize), ) _, stdoutBuf, stderrBuf := configureCmdIO(cmd) db.t.Logf("Populating database to %s (page size: %d, row size: %d)...", targetSize, pageSize, rowSize) if err := cmd.Run(); err != nil { if output := combinedOutput(stdoutBuf, stderrBuf); output != "" { return fmt.Errorf("populate failed: %w\nOutput: %s", err, output) } return fmt.Errorf("populate failed: %w", err) } return nil } func (db *TestDB) GenerateLoad(ctx context.Context, writeRate int, duration time.Duration, pattern string) error { cmd := exec.CommandContext(ctx, getBinaryPath("litestream-test"), "load", "-db", db.Path, "-write-rate", fmt.Sprintf("%d", writeRate), "-duration", duration.String(), "-pattern", pattern, ) _, stdoutBuf, stderrBuf := configureCmdIO(cmd) db.t.Logf("Starting load generation: %d writes/sec for %v (%s pattern)", writeRate, duration, pattern) if err := cmd.Run(); err != nil { if output := combinedOutput(stdoutBuf, stderrBuf); output != "" { return fmt.Errorf("load generation failed: %w\nOutput: %s", err, output) } return fmt.Errorf("load generation failed: %w", err) } return nil } func (db *TestDB) StartLitestream() error { logPath := filepath.Join(db.TempDir, "litestream.log") logFile, err := os.Create(logPath) if err != nil { return fmt.Errorf("create log file: %w", err) } replicaURL := fmt.Sprintf("file://%s", filepath.ToSlash(db.ReplicaPath)) cmd := exec.Command(getBinaryPath("litestream"), "replicate", db.Path, replicaURL, ) cmd.Stdout = logFile cmd.Stderr = logFile if err := cmd.Start(); err != nil { logFile.Close() return fmt.Errorf("start litestream: %w", err) } db.LitestreamCmd = cmd db.LitestreamPID = cmd.Process.Pid time.Sleep(2 * time.Second) if cmd.ProcessState != nil && cmd.ProcessState.Exited() { logFile.Close() return fmt.Errorf("litestream exited immediately") } return nil } func (db *TestDB) StartLitestreamWithConfig(configPath string) error { logPath := filepath.Join(db.TempDir, "litestream.log") logFile, err := os.Create(logPath) if err != nil { return fmt.Errorf("create log file: %w", err) } db.ConfigPath = configPath cmd := exec.Command(getBinaryPath("litestream"), "replicate", "-config", configPath, ) cmd.Stdout = logFile cmd.Stderr = logFile if err := cmd.Start(); err != nil { logFile.Close() return fmt.Errorf("start litestream: %w", err) } db.LitestreamCmd = cmd db.LitestreamPID = cmd.Process.Pid time.Sleep(2 * time.Second) return nil } func (db *TestDB) StopLitestream() error { if db.LitestreamCmd == nil || db.LitestreamCmd.Process == nil { return nil } if err := db.LitestreamCmd.Process.Kill(); err != nil { return fmt.Errorf("kill litestream: %w", err) } db.LitestreamCmd.Wait() time.Sleep(1 * time.Second) return nil } func (db *TestDB) Restore(outputPath string) error { replicaURL := db.ReplicaURL if replicaURL == "" { replicaURL = fmt.Sprintf("file://%s", filepath.ToSlash(db.ReplicaPath)) } var cmd *exec.Cmd if db.ConfigPath != "" && (strings.HasPrefix(replicaURL, "s3://") || strings.HasPrefix(replicaURL, "abs://") || strings.HasPrefix(replicaURL, "nats://")) { cmd = exec.Command(getBinaryPath("litestream"), "restore", "-config", db.ConfigPath, "-o", outputPath, db.Path, ) } else { cmd = exec.Command(getBinaryPath("litestream"), "restore", "-o", outputPath, replicaURL, ) } cmd.Env = append(os.Environ(), db.ReplicaEnv...) output, err := cmd.CombinedOutput() if err != nil { return fmt.Errorf("restore failed: %w\nOutput: %s", err, string(output)) } return nil } func (db *TestDB) Validate(restoredPath string) error { replicaURL := db.ReplicaURL if replicaURL == "" { replicaURL = fmt.Sprintf("file://%s", filepath.ToSlash(db.ReplicaPath)) } cmd := exec.Command(getBinaryPath("litestream-test"), "validate", "-source-db", db.Path, "-replica-url", replicaURL, "-restored-db", restoredPath, "-check-type", "full", ) cmd.Env = append(os.Environ(), db.ReplicaEnv...) output, err := cmd.CombinedOutput() if err != nil { return fmt.Errorf("validation failed: %w\nOutput: %s", err, string(output)) } return nil } func (db *TestDB) QuickValidate(restoredPath string) error { replicaURL := db.ReplicaURL if replicaURL == "" { replicaURL = fmt.Sprintf("file://%s", filepath.ToSlash(db.ReplicaPath)) } cmd := exec.Command(getBinaryPath("litestream-test"), "validate", "-source-db", db.Path, "-replica-url", replicaURL, "-restored-db", restoredPath, "-check-type", "quick", ) cmd.Env = append(os.Environ(), db.ReplicaEnv...) output, err := cmd.CombinedOutput() if err != nil { return fmt.Errorf("validation failed: %w\nOutput: %s", err, string(output)) } return nil } func (db *TestDB) GetRowCount(table string) (int, error) { sqlDB, err := sql.Open("sqlite3", db.Path) if err != nil { return 0, fmt.Errorf("open database: %w", err) } defer sqlDB.Close() var count int query := fmt.Sprintf("SELECT COUNT(*) FROM %s", table) if err := sqlDB.QueryRow(query).Scan(&count); err != nil { return 0, fmt.Errorf("query count: %w", err) } return count, nil } func (db *TestDB) GetDatabaseSize() (int64, error) { info, err := os.Stat(db.Path) if err != nil { return 0, err } size := info.Size() walPath := db.Path + "-wal" if walInfo, err := os.Stat(walPath); err == nil { size += walInfo.Size() } return size, nil } func (db *TestDB) GetReplicaFileCount() (int, error) { ltxPath := filepath.Join(db.ReplicaPath, "ltx", "0") files, err := filepath.Glob(filepath.Join(ltxPath, "*.ltx")) if err != nil { return 0, err } return len(files), nil } func (db *TestDB) WaitForReplicaFiles(minFiles int, timeout time.Duration) (int, error) { deadline := time.Now().Add(timeout) for time.Now().Before(deadline) { count, err := db.GetReplicaFileCount() if err != nil { return 0, err } if count >= minFiles { return count, nil } time.Sleep(1 * time.Second) } count, _ := db.GetReplicaFileCount() return count, fmt.Errorf("timeout waiting for %d replica files, got %d", minFiles, count) } func (db *TestDB) GetLitestreamLog() (string, error) { logPath := filepath.Join(db.TempDir, "litestream.log") content, err := os.ReadFile(logPath) if err != nil { return "", err } return string(content), nil } func (db *TestDB) CheckForErrors() ([]string, error) { log, err := db.GetLitestreamLog() if err != nil { return nil, err } var errors []string lines := strings.Split(log, "\n") for _, line := range lines { if strings.Contains(strings.ToUpper(line), "ERROR") { errors = append(errors, line) } } return errors, nil } func (db *TestDB) Cleanup() { db.StopLitestream() } // WaitForSnapshots waits for snapshots & WAL segments to appear on file replicas. func (db *TestDB) WaitForSnapshots(timeout time.Duration) error { if !strings.HasPrefix(db.ReplicaURL, "file://") { return nil } snapshotDir := filepath.Join(db.ReplicaPath, "ltx", fmt.Sprintf("%d", litestream.SnapshotLevel)) walDir := filepath.Join(db.ReplicaPath, "ltx", "0") deadline := time.Now().Add(timeout) for { snapshotCount := countLTXFiles(snapshotDir) walCount := countLTXFiles(walDir) if snapshotCount > 0 && walCount > 0 { return nil } if time.Now().After(deadline) { return fmt.Errorf("timeout waiting for replica data: snapshots=%d wal=%d", snapshotCount, walCount) } time.Sleep(500 * time.Millisecond) } } func countLTXFiles(dir string) int { matches, err := filepath.Glob(filepath.Join(dir, "*.ltx")) if err != nil { return 0 } return len(matches) } func GetTestDuration(t *testing.T, defaultDuration time.Duration) time.Duration { t.Helper() if testing.Short() { return defaultDuration / 10 } return defaultDuration } func RequireBinaries(t *testing.T) { t.Helper() litestreamBin := getBinaryPath("litestream") if _, err := os.Stat(litestreamBin); err != nil { t.Skip("litestream binary not found, run: go build -o bin/litestream ./cmd/litestream") } litestreamTestBin := getBinaryPath("litestream-test") if _, err := os.Stat(litestreamTestBin); err != nil { t.Skip("litestream-test binary not found, run: go build -o bin/litestream-test ./cmd/litestream-test") } } // WriteS3AccessPointConfig writes a minimal configuration file for S3 access point tests. func WriteS3AccessPointConfig(t *testing.T, dbPath, replicaURL, endpoint string, forcePathStyle bool, accessKey, secretKey string) string { t.Helper() dir := filepath.Dir(dbPath) configPath := filepath.Join(dir, "litestream-access-point.yml") config := fmt.Sprintf(`access-key-id: %s secret-access-key: %s dbs: - path: %s replicas: - url: %s endpoint: %s region: us-east-1 force-path-style: %t skip-verify: true sync-interval: 1s `, accessKey, secretKey, filepath.ToSlash(dbPath), replicaURL, endpoint, forcePathStyle) if err := os.WriteFile(configPath, []byte(config), 0600); err != nil { t.Fatalf("failed to write config: %v", err) } return configPath } func CreateTestTable(t *testing.T, dbPath string) error { t.Helper() sqlDB, err := sql.Open("sqlite3", dbPath) if err != nil { return err } defer sqlDB.Close() _, err = sqlDB.Exec(` CREATE TABLE IF NOT EXISTS test_data ( id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT, created_at INTEGER ) `) return err } func InsertTestData(t *testing.T, dbPath string, count int) error { t.Helper() sqlDB, err := sql.Open("sqlite3", dbPath) if err != nil { return err } defer sqlDB.Close() tx, err := sqlDB.Begin() if err != nil { return err } defer tx.Rollback() stmt, err := tx.Prepare("INSERT INTO test_data (data, created_at) VALUES (?, ?)") if err != nil { return err } defer stmt.Close() for i := 0; i < count; i++ { if _, err := stmt.Exec(fmt.Sprintf("test data %d", i), time.Now().Unix()); err != nil { return err } } return tx.Commit() } // IntegrityCheck runs PRAGMA integrity_check on the database. func (db *TestDB) IntegrityCheck() error { sqlDB, err := sql.Open("sqlite3", db.Path) if err != nil { return err } defer sqlDB.Close() var result string if err := sqlDB.QueryRow("PRAGMA integrity_check").Scan(&result); err != nil { return err } if result != "ok" { return fmt.Errorf("integrity check failed: %s", result) } return nil } // PrintTestSummary prints a summary of the test results func (db *TestDB) PrintTestSummary(t *testing.T, testName string, startTime time.Time) { t.Helper() duration := time.Since(startTime) dbSize, _ := db.GetDatabaseSize() fileCount, _ := db.GetReplicaFileCount() errors, _ := db.CheckForErrors() t.Log("\n" + strings.Repeat("=", 80)) t.Logf("TEST SUMMARY: %s", testName) t.Log(strings.Repeat("=", 80)) t.Logf("Duration: %v", duration.Round(time.Second)) t.Logf("Database Size: %.2f MB", float64(dbSize)/(1024*1024)) t.Logf("Replica Files: %d LTX files", fileCount) t.Logf("Litestream Errors: %d", len(errors)) t.Log(strings.Repeat("=", 80)) } ================================================ FILE: tests/integration/minio_soak_test.go ================================================ //go:build integration && soak && docker package integration import ( "context" "database/sql" "fmt" "os/exec" "path/filepath" "strings" "testing" "time" _ "github.com/mattn/go-sqlite3" ) // TestMinIOSoak runs a soak test against local MinIO S3-compatible server using Docker. // // Default duration: 2 hours // Can be shortened with: go test -test.short (runs for 30 minutes) // // Requirements: // - Docker must be running // - docker command must be in PATH // // This test validates: // - S3-compatible replication to MinIO // - Docker container lifecycle management // - Heavy sustained load (500 writes/sec) // - Restoration from S3-compatible storage func TestMinIOSoak(t *testing.T) { RequireBinaries(t) RequireDocker(t) // Determine test duration duration := GetTestDuration(t, 2*time.Hour) shortMode := testing.Short() if shortMode { duration = 2 * time.Minute } targetSize := "50MB" writeRate := 500 if shortMode { targetSize = "5MB" writeRate = 100 } t.Logf("================================================") t.Logf("Litestream MinIO S3 Soak Test") t.Logf("================================================") t.Logf("Duration: %v", duration) t.Logf("Start time: %s", time.Now().Format(time.RFC3339)) t.Log("") startTime := time.Now() // Start MinIO container t.Log("Starting MinIO container...") containerID, endpoint, dataVolume := StartMinIOContainer(t) defer StopMinIOContainer(t, containerID, dataVolume) t.Logf("✓ MinIO running at: %s", endpoint) t.Log("") // Create MinIO bucket bucket := "litestream-test" CreateMinIOBucket(t, containerID, bucket) t.Log("") // Setup test database db := SetupTestDB(t, "minio-soak") defer db.Cleanup() // Create database if err := db.Create(); err != nil { t.Fatalf("Failed to create database: %v", err) } // Populate with initial data t.Logf("Populating database (%s initial data)...", targetSize) if err := db.Populate(targetSize); err != nil { t.Fatalf("Failed to populate database: %v", err) } t.Log("✓ Database populated") t.Log("") // Create S3 configuration for MinIO s3Path := fmt.Sprintf("litestream-test-%d", time.Now().Unix()) s3URL := fmt.Sprintf("s3://%s/%s", bucket, s3Path) db.ReplicaURL = s3URL t.Log("Creating Litestream configuration for MinIO S3...") s3Config := &S3Config{ Endpoint: endpoint, AccessKey: "minioadmin", SecretKey: "minioadmin", Region: "us-east-1", ForcePathStyle: true, SkipVerify: true, } configPath := CreateSoakConfig(db.Path, s3URL, s3Config, shortMode) db.ConfigPath = configPath t.Logf("✓ Configuration created: %s", configPath) t.Logf(" S3 URL: %s", s3URL) t.Log("") // Start Litestream t.Log("Starting Litestream with MinIO backend...") if err := db.StartLitestreamWithConfig(configPath); err != nil { t.Fatalf("Failed to start Litestream: %v", err) } t.Logf("✓ Litestream running (PID: %d)", db.LitestreamPID) t.Log("") // Start load generator t.Log("Starting load generator (heavy sustained load)...") t.Logf(" Write rate: %d writes/second", writeRate) t.Logf(" Pattern: wave (simulates varying load)") t.Logf(" Payload size: 4KB") t.Logf(" Workers: 8") t.Log("") ctx, cancel := context.WithTimeout(context.Background(), duration) defer cancel() // Setup signal handler for graceful interruption testInfo := &TestInfo{ StartTime: startTime, Duration: duration, DB: db, cancel: cancel, } setupSignalHandler(t, cancel, testInfo) // Run load generation in background loadDone := make(chan error, 1) go func() { loadDone <- db.GenerateLoad(ctx, writeRate, duration, "wave") }() // Monitor every 60 seconds with MinIO-specific metrics t.Log("Running MinIO S3 test...") t.Log("Monitor will report every 60 seconds") t.Log("Press Ctrl+C twice within 5 seconds to stop early") t.Log("================================================") t.Log("") refreshStats := func() { testInfo.RowCount, _ = db.GetRowCount("load_test") if testInfo.RowCount == 0 { testInfo.RowCount, _ = db.GetRowCount("test_table_0") } if testInfo.RowCount == 0 { testInfo.RowCount, _ = db.GetRowCount("test_data") } testInfo.FileCount = CountMinIOObjects(t, containerID, bucket) } logMetrics := func() { logMinIOMetrics(t, db, containerID, bucket) if db.LitestreamCmd != nil && db.LitestreamCmd.ProcessState != nil { t.Error("✗ Litestream stopped unexpectedly!") if testInfo.cancel != nil { testInfo.cancel() } } } MonitorSoakTest(t, db, ctx, testInfo, refreshStats, logMetrics) // Wait for load generation to complete if err := <-loadDone; err != nil { t.Logf("Load generation completed: %v", err) } if err := db.WaitForSnapshots(30 * time.Second); err != nil { t.Fatalf("Failed waiting for snapshot: %v", err) } t.Log("") t.Log("================================================") t.Log("Final Test Results") t.Log("================================================") t.Log("") // Stop Litestream t.Log("Stopping Litestream...") if err := db.StopLitestream(); err != nil { t.Logf("Warning: Failed to stop Litestream cleanly: %v", err) } // Final statistics t.Log("Database Statistics:") if dbSize, err := db.GetDatabaseSize(); err == nil { t.Logf(" Final size: %.2f MB", float64(dbSize)/(1024*1024)) } // Count rows var rowCount int var err error if rowCount, err = db.GetRowCount("load_test"); err != nil { if rowCount, err = db.GetRowCount("test_table_0"); err != nil { if rowCount, err = db.GetRowCount("test_data"); err != nil { t.Logf(" Warning: Could not get row count: %v", err) } } } if err == nil { t.Logf(" Total rows: %d", rowCount) } t.Log("") // MinIO statistics t.Log("MinIO S3 Statistics:") finalObjects := CountMinIOObjects(t, containerID, bucket) t.Logf(" Total objects in MinIO: %d", finalObjects) t.Log("") // Check for errors errors, _ := db.CheckForErrors() t.Logf(" Critical errors: %d", len(errors)) t.Log("") // Test restoration from MinIO t.Log("Testing restoration from MinIO S3...") restoredPath := filepath.Join(db.TempDir, "restored.db") if err := db.Restore(restoredPath); err != nil { t.Fatalf("Restoration from MinIO failed: %v", err) } t.Log("✓ Restoration successful!") // Compare row counts var restoredCount int if restoredCount, err = getRowCountFromPath(restoredPath, "load_test"); err != nil { if restoredCount, err = getRowCountFromPath(restoredPath, "test_table_0"); err != nil { if restoredCount, err = getRowCountFromPath(restoredPath, "test_data"); err != nil { t.Logf(" Warning: Could not get restored row count: %v", err) } } } if err == nil && rowCount > 0 { if rowCount == restoredCount { t.Logf("✓ Row counts match! (%d rows)", restoredCount) } else { t.Logf("⚠ Row count mismatch! Original: %d, Restored: %d", rowCount, restoredCount) } } // Validate integrity t.Log("") t.Log("Validating restored database integrity...") restoredDB := &TestDB{Path: restoredPath, t: t} if err := restoredDB.IntegrityCheck(); err != nil { t.Fatalf("Integrity check failed: %v", err) } t.Log("✓ Integrity check passed!") // Analyze test results analysis := AnalyzeSoakTest(t, db, duration) PrintSoakTestAnalysis(t, analysis) // Test Summary t.Log("================================================") t.Log("Test Summary") t.Log("================================================") testPassed := true issues := []string{} if criticalErrors > 0 { testPassed = false issues = append(issues, fmt.Sprintf("Critical errors detected: %d", criticalErrors)) } if finalObjects == 0 { testPassed = false issues = append(issues, "No objects stored in MinIO") } if testPassed { t.Log("✓ TEST PASSED!") t.Log("") t.Logf("Successfully replicated to MinIO (%d objects)", finalObjects) t.Log("The configuration is ready for production use.") } else { t.Log("⚠ TEST COMPLETED WITH ISSUES:") for _, issue := range issues { t.Logf(" - %s", issue) } t.Log("") t.Log("Review the logs for details:") logPath, _ := db.GetLitestreamLog() t.Logf(" %s", logPath) t.Fail() } t.Log("") t.Logf("Test duration: %v", time.Since(startTime).Round(time.Second)) t.Logf("Results available in: %s", db.TempDir) t.Log("================================================") } // logMinIOMetrics logs MinIO-specific metrics func logMinIOMetrics(t *testing.T, db *TestDB, containerID, bucket string) { t.Helper() // Basic database metrics LogSoakMetrics(t, db, "minio") // MinIO-specific metrics t.Log("") t.Log(" MinIO S3 Statistics:") objectCount := CountMinIOObjects(t, containerID, bucket) t.Logf(" Total objects: %d", objectCount) // Count LTX files specifically ltxCount := countMinIOLTXFiles(t, containerID, bucket) t.Logf(" LTX segments: %d", ltxCount) } // countMinIOLTXFiles counts LTX files in MinIO bucket func countMinIOLTXFiles(t *testing.T, containerID, bucket string) int { t.Helper() cmd := exec.Command("docker", "run", "--rm", "--link", containerID+":minio", "-e", "MC_HOST_minio=http://minioadmin:minioadmin@minio:9000", "minio/mc", "ls", "minio/"+bucket+"/", "--recursive") output, err := cmd.CombinedOutput() if err != nil { return 0 } lines := strings.Split(strings.TrimSpace(string(output)), "\n") ltxCount := 0 for _, line := range lines { if strings.Contains(line, ".ltx") { ltxCount++ } } return ltxCount } // getRowCountFromPath gets row count from a database file path func getRowCountFromPath(dbPath, table string) (int, error) { db, err := sql.Open("sqlite3", dbPath) if err != nil { return 0, err } defer db.Close() var count int query := fmt.Sprintf("SELECT COUNT(*) FROM %s", table) if err := db.QueryRow(query).Scan(&count); err != nil { return 0, err } return count, nil } ================================================ FILE: tests/integration/overnight_s3_soak_test.go ================================================ //go:build integration && soak && aws package integration import ( "context" "database/sql" "fmt" "path/filepath" "testing" "time" _ "github.com/mattn/go-sqlite3" ) // TestOvernightS3Soak runs an 8-hour overnight soak test against real AWS S3. // // Default duration: 8 hours // Can be shortened with: go test -test.short (runs for 1 hour) // // Requirements: // - AWS_ACCESS_KEY_ID environment variable // - AWS_SECRET_ACCESS_KEY environment variable // - S3_BUCKET environment variable // - AWS_REGION environment variable (optional, defaults to us-east-1) // - AWS CLI must be installed // // This test validates: // - Long-term S3 replication stability // - Network resilience over 8 hours // - Real S3 API performance // - Restoration from cloud storage func TestOvernightS3Soak(t *testing.T) { RequireBinaries(t) // Check AWS credentials and get configuration bucket, region := CheckAWSCredentials(t) // Determine test duration var duration time.Duration if testing.Short() { duration = 10 * time.Minute } else { duration = 8 * time.Hour } shortMode := testing.Short() t.Logf("================================================") t.Logf("Litestream Overnight S3 Soak Test") t.Logf("================================================") t.Logf("Duration: %v", duration) t.Logf("S3 Bucket: %s", bucket) t.Logf("AWS Region: %s", region) t.Logf("Start time: %s", time.Now().Format(time.RFC3339)) t.Log("") startTime := time.Now() // Test S3 connectivity t.Log("Testing S3 connectivity...") TestS3Connectivity(t, bucket) t.Log("") // Setup test database db := SetupTestDB(t, "overnight-s3-soak") defer db.Cleanup() // Create database if err := db.Create(); err != nil { t.Fatalf("Failed to create database: %v", err) } // Create S3 configuration s3Path := fmt.Sprintf("litestream-overnight-%d", time.Now().Unix()) s3URL := fmt.Sprintf("s3://%s/%s", bucket, s3Path) db.ReplicaURL = s3URL t.Log("Creating Litestream configuration for S3...") s3Config := &S3Config{ Region: region, } configPath := CreateSoakConfig(db.Path, s3URL, s3Config, shortMode) db.ConfigPath = configPath t.Logf("✓ Configuration created: %s", configPath) t.Logf(" S3 URL: %s", s3URL) t.Log("") // Start Litestream initially (before population) t.Log("Starting Litestream...") if err := db.StartLitestreamWithConfig(configPath); err != nil { t.Fatalf("Failed to start Litestream: %v", err) } t.Logf("✓ Litestream started (PID: %d)", db.LitestreamPID) t.Log("") // Stop Litestream to populate database t.Log("Stopping Litestream temporarily for initial population...") if err := db.StopLitestream(); err != nil { t.Fatalf("Failed to stop Litestream: %v", err) } // Populate with 100MB of initial data t.Log("Populating database (100MB initial data)...") if err := db.Populate("100MB"); err != nil { t.Fatalf("Failed to populate database: %v", err) } t.Log("✓ Database populated") t.Log("") // Restart Litestream after population t.Log("Restarting Litestream after population...") if err := db.StartLitestreamWithConfig(configPath); err != nil { t.Fatalf("Failed to restart Litestream: %v", err) } t.Logf("✓ Litestream restarted (PID: %d)", db.LitestreamPID) t.Log("") // Start load generator for overnight test t.Log("Starting load generator for overnight S3 test...") t.Log("Configuration:") t.Logf(" Duration: %v", duration) t.Logf(" Write rate: 100 writes/second (higher for S3 testing)") t.Logf(" Pattern: wave (simulates varying load)") t.Logf(" Workers: 8") t.Log("") ctx, cancel := context.WithTimeout(context.Background(), duration) defer cancel() // Setup signal handler for graceful interruption testInfo := &TestInfo{ StartTime: startTime, Duration: duration, DB: db, cancel: cancel, } setupSignalHandler(t, cancel, testInfo) // Run load generation in background loadDone := make(chan error, 1) go func() { loadDone <- db.GenerateLoad(ctx, 100, duration, "wave") }() // Monitor every 60 seconds with S3-specific metrics t.Log("Overnight S3 test is running!") t.Log("Monitor will report every 60 seconds") t.Log("Press Ctrl+C twice within 5 seconds to stop early") t.Log("================================================") t.Log("") t.Logf("The test will run for %v. Monitor progress below.", duration) t.Log("") refreshStats := func() { testInfo.RowCount, _ = db.GetRowCount("load_test") if testInfo.RowCount == 0 { testInfo.RowCount, _ = db.GetRowCount("test_table_0") } if testInfo.RowCount == 0 { testInfo.RowCount, _ = db.GetRowCount("test_data") } testInfo.FileCount = CountS3Objects(t, s3URL) } logMetrics := func() { logS3Metrics(t, db, s3URL) if db.LitestreamCmd != nil && db.LitestreamCmd.ProcessState != nil { t.Error("✗ Litestream stopped unexpectedly!") if testInfo.cancel != nil { testInfo.cancel() } } } MonitorSoakTest(t, db, ctx, testInfo, refreshStats, logMetrics) // Wait for load generation to complete if err := <-loadDone; err != nil { t.Logf("Load generation completed: %v", err) } t.Log("") t.Log("Load generation completed.") // Final statistics t.Log("") t.Log("================================================") t.Log("Final Statistics") t.Log("================================================") t.Log("") // Stop Litestream t.Log("Stopping Litestream...") if err := db.StopLitestream(); err != nil { t.Logf("Warning: Failed to stop Litestream cleanly: %v", err) } // Database statistics t.Log("Database Statistics:") if dbSize, err := db.GetDatabaseSize(); err == nil { t.Logf(" Final size: %.2f MB", float64(dbSize)/(1024*1024)) } // Count rows var rowCount int var err error if rowCount, err = db.GetRowCount("load_test"); err != nil { if rowCount, err = db.GetRowCount("test_table_0"); err != nil { if rowCount, err = db.GetRowCount("test_data"); err != nil { t.Logf(" Warning: Could not get row count: %v", err) } } } if err == nil { t.Logf(" Total rows: %d", rowCount) } t.Log("") // S3 statistics t.Log("S3 Statistics:") finalObjects := CountS3Objects(t, s3URL) t.Logf(" Total objects: %d", finalObjects) if s3Size := GetS3StorageSize(t, s3URL); s3Size > 0 { t.Logf(" Total S3 storage: %.2f MB", float64(s3Size)/(1024*1024)) } t.Log("") // Check for errors errors, _ := db.CheckForErrors() t.Logf(" Critical errors: %d", len(errors)) t.Log("") // Test restoration from S3 t.Log("Testing restoration from S3...") restoredPath := filepath.Join(db.TempDir, "restored.db") if err := db.Restore(restoredPath); err != nil { t.Fatalf("Restoration from S3 failed: %v", err) } t.Log("✓ Restoration successful!") // Compare row counts var restoredCount int if restoredCount, err = getRowCountFromPath(restoredPath, "load_test"); err != nil { if restoredCount, err = getRowCountFromPath(restoredPath, "test_table_0"); err != nil { if restoredCount, err = getRowCountFromPath(restoredPath, "test_data"); err != nil { t.Logf(" Warning: Could not get restored row count: %v", err) } } } if err == nil && rowCount > 0 { if rowCount == restoredCount { t.Logf("✓ Row counts match! (%d rows)", restoredCount) } else { t.Logf("⚠ Row count mismatch! Original: %d, Restored: %d", rowCount, restoredCount) } } // Validate t.Log("") t.Log("Validating restored database...") if err := db.Validate(restoredPath); err != nil { t.Fatalf("Validation failed: %v", err) } t.Log("✓ Validation passed!") // Analyze test results analysis := AnalyzeSoakTest(t, db, duration) PrintSoakTestAnalysis(t, analysis) // Test Summary t.Log("================================================") t.Log("Test Summary") t.Log("================================================") testPassed := true issues := []string{} if criticalErrors > 0 { testPassed = false issues = append(issues, fmt.Sprintf("Critical errors detected: %d", criticalErrors)) } if finalObjects == 0 { testPassed = false issues = append(issues, "No objects stored in S3") } if testPassed { t.Log("✓ TEST PASSED!") t.Log("") t.Logf("Successfully replicated to AWS S3 (%d objects)", finalObjects) t.Log("The configuration is ready for production use.") } else { t.Log("⚠ TEST COMPLETED WITH ISSUES:") for _, issue := range issues { t.Logf(" - %s", issue) } t.Log("") t.Log("Review the logs for details:") logPath, _ := db.GetLitestreamLog() t.Logf(" %s", logPath) t.Fail() } t.Log("") t.Logf("Test duration: %v", time.Since(startTime).Round(time.Second)) t.Logf("Results available in: %s", db.TempDir) t.Logf("S3 replica data in: %s", s3URL) t.Log("================================================") } // logS3Metrics logs S3-specific metrics func logS3Metrics(t *testing.T, db *TestDB, s3URL string) { t.Helper() // Basic database metrics LogSoakMetrics(t, db, "overnight-s3") // S3-specific metrics t.Log("") t.Log(" S3 Statistics:") objectCount := CountS3Objects(t, s3URL) t.Logf(" Total objects: %d", objectCount) if s3Size := GetS3StorageSize(t, s3URL); s3Size > 0 { t.Logf(" Total storage: %.2f MB", float64(s3Size)/(1024*1024)) } } // getRowCountFromPath gets row count from a database file path func getRowCountFromPath(dbPath, table string) (int, error) { db, err := sql.Open("sqlite3", dbPath) if err != nil { return 0, err } defer db.Close() var count int query := fmt.Sprintf("SELECT COUNT(*) FROM %s", table) if err := db.QueryRow(query).Scan(&count); err != nil { return 0, err } return count, nil } ================================================ FILE: tests/integration/overnight_test.go ================================================ //go:build integration && long package integration import ( "context" "path/filepath" "testing" "time" _ "github.com/mattn/go-sqlite3" ) func TestOvernightFile(t *testing.T) { if testing.Short() { t.Skip("skipping long integration test in short mode") } RequireBinaries(t) startTime := time.Now() duration := GetTestDuration(t, 8*time.Hour) t.Logf("Testing: Overnight file-based replication (duration: %v)", duration) t.Log("Default: 8 hours, configurable via test duration") db := SetupTestDB(t, "overnight-file") defer db.Cleanup() defer db.PrintTestSummary(t, "Overnight File Replication", startTime) t.Log("[1] Creating and populating database...") if err := db.Create(); err != nil { t.Fatalf("Failed to create database: %v", err) } if err := db.Populate("100MB"); err != nil { t.Fatalf("Failed to populate database: %v", err) } t.Log("✓ Database populated to 100MB") t.Log("[2] Starting Litestream...") if err := db.StartLitestream(); err != nil { t.Fatalf("Failed to start Litestream: %v", err) } time.Sleep(10 * time.Second) t.Log("[3] Generating sustained load...") ctx, cancel := context.WithTimeout(context.Background(), duration) defer cancel() config := DefaultLoadConfig() config.WriteRate = 50 config.Duration = duration config.Pattern = LoadPatternWave config.PayloadSize = 2 * 1024 config.Workers = 4 ticker := time.NewTicker(1 * time.Minute) defer ticker.Stop() go func() { for { select { case <-ctx.Done(): return case <-ticker.C: fileCount, _ := db.GetReplicaFileCount() dbSize, _ := db.GetDatabaseSize() t.Logf("[Progress] Files: %d, DB Size: %.2f MB, Elapsed: %v", fileCount, float64(dbSize)/(1024*1024), time.Since(time.Now().Add(-duration))) } } }() if err := db.GenerateLoad(ctx, config.WriteRate, config.Duration, string(config.Pattern)); err != nil && ctx.Err() == nil { t.Fatalf("Load generation failed: %v", err) } t.Log("✓ Load generation complete") time.Sleep(1 * time.Minute) t.Log("[4] Final statistics...") fileCount, err := db.GetReplicaFileCount() if err != nil { t.Fatalf("Failed to check replica: %v", err) } dbSize, err := db.GetDatabaseSize() if err != nil { t.Fatalf("Failed to get database size: %v", err) } t.Logf("Final LTX files: %d", fileCount) t.Logf("Final DB size: %.2f MB", float64(dbSize)/(1024*1024)) t.Log("[5] Checking for errors...") errors, err := db.CheckForErrors() if err != nil { t.Fatalf("Failed to check errors: %v", err) } if len(errors) > 20 { t.Fatalf("Too many errors (%d), test may be unstable", len(errors)) } else if len(errors) > 0 { t.Logf("Found %d errors (acceptable for long test)", len(errors)) } else { t.Log("✓ No errors detected") } db.StopLitestream() time.Sleep(2 * time.Second) t.Log("[6] Testing final restore...") restoredPath := filepath.Join(db.TempDir, "overnight-restored.db") if err := db.Restore(restoredPath); err != nil { t.Fatalf("Restore failed: %v", err) } t.Log("✓ Restore successful") t.Log("[7] Full validation...") if err := db.Validate(restoredPath); err != nil { t.Fatalf("Validation failed: %v", err) } t.Log("✓ Validation passed") t.Log("TEST PASSED: Overnight file replication successful") } func TestOvernightComprehensive(t *testing.T) { if testing.Short() { t.Skip("skipping long integration test in short mode") } RequireBinaries(t) startTime := time.Now() duration := GetTestDuration(t, 8*time.Hour) t.Logf("Testing: Comprehensive overnight test (duration: %v)", duration) db := SetupTestDB(t, "overnight-comprehensive") defer db.Cleanup() defer db.PrintTestSummary(t, "Overnight Comprehensive Test", startTime) t.Log("[1] Creating large database...") if err := db.Create(); err != nil { t.Fatalf("Failed to create database: %v", err) } if err := db.Populate("500MB"); err != nil { t.Fatalf("Failed to populate database: %v", err) } t.Log("✓ Database populated to 500MB") t.Log("[2] Starting Litestream...") if err := db.StartLitestream(); err != nil { t.Fatalf("Failed to start Litestream: %v", err) } time.Sleep(10 * time.Second) t.Log("[3] Generating mixed workload...") ctx, cancel := context.WithTimeout(context.Background(), duration) defer cancel() config := DefaultLoadConfig() config.WriteRate = 100 config.Duration = duration config.Pattern = LoadPatternWave config.PayloadSize = 4 * 1024 config.ReadRatio = 0.3 config.Workers = 8 ticker := time.NewTicker(5 * time.Minute) defer ticker.Stop() go func() { for { select { case <-ctx.Done(): return case <-ticker.C: fileCount, _ := db.GetReplicaFileCount() dbSize, _ := db.GetDatabaseSize() t.Logf("[Progress] Files: %d, DB Size: %.2f MB", fileCount, float64(dbSize)/(1024*1024)) } } }() if err := db.GenerateLoad(ctx, config.WriteRate, config.Duration, string(config.Pattern)); err != nil && ctx.Err() == nil { t.Fatalf("Load generation failed: %v", err) } t.Log("✓ Load generation complete") time.Sleep(2 * time.Minute) db.StopLitestream() t.Log("[4] Final validation...") restoredPath := filepath.Join(db.TempDir, "comprehensive-restored.db") if err := db.Restore(restoredPath); err != nil { t.Fatalf("Restore failed: %v", err) } if err := db.Validate(restoredPath); err != nil { t.Fatalf("Validation failed: %v", err) } t.Log("✓ Comprehensive test passed") t.Log("TEST PASSED: Overnight comprehensive test successful") } ================================================ FILE: tests/integration/profile_test.go ================================================ //go:build profile package integration import ( "context" "database/sql" "fmt" "log" "net/http" _ "net/http/pprof" "os" "os/signal" "path/filepath" "strconv" "testing" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/file" _ "modernc.org/sqlite" ) // TestIdleCPUProfile starts N databases with file-based replicas and no writes, // then exposes pprof and fgprof endpoints for interactive profiling. // // This test is designed for manual CPU profiling to understand idle overhead // when running many Litestream instances on a single machine. // // Usage: // // # Start with 100 idle databases on default port: // PROFILE_DB_COUNT=100 go test -tags=profile -run TestIdleCPUProfile -timeout=0 -v ./tests/integration/ // // # Custom listen address: // PROFILE_ADDR=:9090 PROFILE_DB_COUNT=50 go test -tags=profile -run TestIdleCPUProfile -timeout=0 -v ./tests/integration/ // // # Then in another terminal: // go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30 // go tool pprof http://localhost:6060/debug/pprof/goroutine // go tool pprof http://localhost:6060/debug/pprof/heap func TestIdleCPUProfile(t *testing.T) { dbCount := 10 if s := os.Getenv("PROFILE_DB_COUNT"); s != "" { n, err := strconv.Atoi(s) if err != nil { t.Fatalf("invalid PROFILE_DB_COUNT: %v", err) } dbCount = n } addr := ":6060" if s := os.Getenv("PROFILE_ADDR"); s != "" { addr = s } // Start HTTP server for profiling (net/http/pprof registered via blank import). go func() { log.Printf("pprof server listening on %s", addr) if err := http.ListenAndServe(addr, nil); err != nil { log.Printf("pprof server error: %v", err) } }() // Create temporary root directory for all databases. rootDir := t.TempDir() // Start N databases with monitoring enabled (the idle hot path). type instance struct { db *litestream.DB sqldb *sql.DB } instances := make([]instance, 0, dbCount) for i := range dbCount { dbPath := filepath.Join(rootDir, fmt.Sprintf("db%d", i), "db") replicaDir := filepath.Join(rootDir, fmt.Sprintf("db%d", i), "replica") if err := os.MkdirAll(filepath.Dir(dbPath), 0o755); err != nil { t.Fatalf("mkdir: %v", err) } // Create database with WAL mode and seed data. sqldb, err := sql.Open("sqlite", dbPath) if err != nil { t.Fatalf("open sql db %d: %v", i, err) } if _, err := sqldb.Exec(`PRAGMA journal_mode = wal`); err != nil { t.Fatalf("set wal mode db %d: %v", i, err) } if _, err := sqldb.Exec(`CREATE TABLE data (id INTEGER PRIMARY KEY, value TEXT)`); err != nil { t.Fatalf("create table db %d: %v", i, err) } if _, err := sqldb.Exec(`INSERT INTO data (value) VALUES ('seed')`); err != nil { t.Fatalf("insert seed db %d: %v", i, err) } // Configure Litestream DB with monitoring enabled. db := litestream.NewDB(dbPath) db.Replica = litestream.NewReplica(db) db.Replica.Client = file.NewReplicaClient(replicaDir) if err := db.Open(); err != nil { t.Fatalf("open litestream db %d: %v", i, err) } // Do an initial sync so there's a valid LTX baseline. if err := db.Sync(context.Background()); err != nil { t.Fatalf("initial sync db %d: %v", i, err) } instances = append(instances, instance{db: db, sqldb: sqldb}) } t.Logf("started %d idle databases with monitoring (interval=%s)", dbCount, litestream.DefaultMonitorInterval) t.Logf("") t.Logf("profiling endpoints:") t.Logf(" CPU (on-cpu): go tool pprof http://localhost%s/debug/pprof/profile?seconds=30", addr) t.Logf(" goroutines: go tool pprof http://localhost%s/debug/pprof/goroutine", addr) t.Logf(" heap: go tool pprof http://localhost%s/debug/pprof/heap", addr) t.Logf("") t.Logf("press Ctrl+C to stop") // Block until interrupted. ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) defer stop() <-ctx.Done() t.Logf("shutting down %d databases...", dbCount) for i, inst := range instances { if err := inst.db.Close(context.Background()); err != nil { t.Logf("close litestream db %d: %v", i, err) } if err := inst.sqldb.Close(); err != nil { t.Logf("close sql db %d: %v", i, err) } } } ================================================ FILE: tests/integration/quick_test.go ================================================ //go:build integration package integration import ( "context" "path/filepath" "testing" "time" _ "github.com/mattn/go-sqlite3" ) func TestQuickValidation(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } RequireBinaries(t) startTime := time.Now() duration := GetTestDuration(t, 30*time.Minute) t.Logf("Testing: Quick validation test (duration: %v)", duration) t.Log("Default: 30 minutes, configurable via test duration") db := SetupTestDB(t, "quick-validation") defer db.Cleanup() defer db.PrintTestSummary(t, "Quick Validation Test", startTime) t.Log("[1] Creating and populating database...") if err := db.Create(); err != nil { t.Fatalf("Failed to create database: %v", err) } if err := db.Populate("10MB"); err != nil { t.Fatalf("Failed to populate database: %v", err) } t.Log("✓ Database populated to 10MB") t.Log("[2] Starting Litestream...") if err := db.StartLitestream(); err != nil { t.Fatalf("Failed to start Litestream: %v", err) } time.Sleep(5 * time.Second) t.Log("[3] Generating wave pattern load...") ctx, cancel := context.WithTimeout(context.Background(), duration) defer cancel() config := DefaultLoadConfig() config.WriteRate = 100 config.Duration = duration config.Pattern = LoadPatternWave config.PayloadSize = 4 * 1024 config.Workers = 4 if err := db.GenerateLoad(ctx, config.WriteRate, config.Duration, string(config.Pattern)); err != nil && ctx.Err() == nil { t.Fatalf("Load generation failed: %v", err) } t.Log("✓ Load generation complete") time.Sleep(10 * time.Second) t.Log("[4] Checking replica status...") fileCount, err := db.GetReplicaFileCount() if err != nil { t.Fatalf("Failed to check replica: %v", err) } if fileCount == 0 { t.Fatal("No LTX segments created!") } t.Logf("✓ LTX segments created: %d files", fileCount) dbSize, err := db.GetDatabaseSize() if err != nil { t.Fatalf("Failed to get database size: %v", err) } t.Logf("Database size: %.2f MB", float64(dbSize)/(1024*1024)) t.Log("[5] Checking for errors...") errors, err := db.CheckForErrors() if err != nil { t.Fatalf("Failed to check errors: %v", err) } if len(errors) > 10 { t.Fatalf("Too many critical errors (%d), showing first 5:\n%v", len(errors), errors[:5]) } else if len(errors) > 0 { t.Logf("Found %d errors (showing first 3):", len(errors)) for i := 0; i < min(len(errors), 3); i++ { t.Logf(" %s", errors[i]) } } else { t.Log("✓ No errors detected") } db.StopLitestream() time.Sleep(2 * time.Second) t.Log("[6] Testing restore...") restoredPath := filepath.Join(db.TempDir, "quick-restored.db") if err := db.Restore(restoredPath); err != nil { t.Fatalf("Restore failed: %v", err) } t.Log("✓ Restore successful") t.Log("[7] Validating restoration...") if err := db.QuickValidate(restoredPath); err != nil { t.Fatalf("Validation failed: %v", err) } t.Log("✓ Validation passed") t.Log("TEST PASSED: Quick validation successful") } ================================================ FILE: tests/integration/s3_access_point_test.go ================================================ //go:build integration package integration import ( "context" "database/sql" "fmt" "os" "path/filepath" "strings" "testing" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/credentials" awss3 "github.com/aws/aws-sdk-go-v2/service/s3" s3types "github.com/aws/aws-sdk-go-v2/service/s3/types" ) // TestS3AccessPointLocalStack verifies replication to an S3 access point via LocalStack. func TestS3AccessPointLocalStack(t *testing.T) { RequireBinaries(t) RequireDocker(t) containerName, endpoint := StartMinioTestContainer(t) t.Cleanup(func() { StopMinioTestContainer(t, containerName) }) ctx := context.Background() configEndpoint := strings.Replace(endpoint, "localhost", "s3-accesspoint.127.0.0.1.nip.io", 1) s3Client := newMinioS3Client(t, configEndpoint, false) accountID := "000000000000" accessPointName := fmt.Sprintf("litestream-ap-%d", time.Now().UnixNano()) bucket := fmt.Sprintf("%s-%s", accessPointName, accountID) accessPointARN := fmt.Sprintf("arn:aws:s3:us-east-1:%s:accesspoint/%s", accountID, accessPointName) createBucket(t, ctx, s3Client, bucket) t.Cleanup(func() { if os.Getenv("SOAK_KEEP_TEMP") != "" { t.Logf("SOAK_KEEP_TEMP set, preserving bucket %s", bucket) return } if err := clearBucket(ctx, s3Client, bucket); err != nil { t.Logf("warn: clear bucket: %v", err) } }) t.Logf("simulated access point ARN: %s", accessPointARN) db := SetupTestDB(t, "localstack-accesspoint") if err := db.Create(); err != nil { t.Fatalf("create db: %v", err) } if err := db.Populate("5MB"); err != nil { t.Fatalf("populate db: %v", err) } replicaURL := fmt.Sprintf("s3://%s/test-prefix", accessPointARN) db.ReplicaURL = replicaURL configPath := WriteS3AccessPointConfig(t, db.Path, replicaURL, configEndpoint, false, "minioadmin", "minioadmin") db.ConfigPath = configPath if err := db.StartLitestreamWithConfig(configPath); err != nil { t.Fatalf("start litestream: %v", err) } t.Cleanup(func() { _ = db.StopLitestream() }) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() if err := db.GenerateLoad(ctx, 200, 5*time.Second, "steady"); err != nil { t.Fatalf("generate load: %v", err) } // Wait for uploaded LTX files to appear in the underlying bucket. waitForObjects(t, s3Client, bucket, "test-prefix", 30*time.Second) if err := db.StopLitestream(); err != nil { t.Fatalf("stop litestream: %v", err) } db.LitestreamCmd = nil restoredPath := filepath.Join(db.TempDir, "restored-access-point.db") if err := db.Restore(restoredPath); err != nil { t.Fatalf("restore: %v", err) } if err := compareRowCounts(db.Path, restoredPath); err != nil { t.Fatalf("row compare: %v", err) } } func newMinioS3Client(t *testing.T, endpoint string, forcePathStyle bool) *awss3.Client { t.Helper() resolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) { return aws.Endpoint{ PartitionID: "aws", URL: endpoint, SigningRegion: "us-east-1", HostnameImmutable: true, }, nil }) cfg, err := config.LoadDefaultConfig(context.Background(), config.WithRegion("us-east-1"), config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("minioadmin", "minioadmin", "")), config.WithEndpointResolverWithOptions(resolver), ) if err != nil { t.Fatalf("load aws config: %v", err) } return awss3.NewFromConfig(cfg, func(o *awss3.Options) { o.UsePathStyle = forcePathStyle }) } func createBucket(t *testing.T, ctx context.Context, client *awss3.Client, bucket string) { t.Helper() if _, err := client.CreateBucket(ctx, &awss3.CreateBucketInput{Bucket: aws.String(bucket)}); err != nil { t.Fatalf("create bucket: %v", err) } } func clearBucket(ctx context.Context, client *awss3.Client, bucket string) error { paginator := awss3.NewListObjectsV2Paginator(client, &awss3.ListObjectsV2Input{Bucket: aws.String(bucket)}) for paginator.HasMorePages() { page, err := paginator.NextPage(ctx) if err != nil { return fmt.Errorf("list objects: %w", err) } if len(page.Contents) == 0 { break } objs := make([]s3types.ObjectIdentifier, 0, len(page.Contents)) for _, item := range page.Contents { objs = append(objs, s3types.ObjectIdentifier{Key: item.Key}) } if _, err := client.DeleteObjects(ctx, &awss3.DeleteObjectsInput{ Bucket: aws.String(bucket), Delete: &s3types.Delete{Objects: objs, Quiet: aws.Bool(true)}, }); err != nil { return fmt.Errorf("delete objects: %w", err) } } return nil } func waitForObjects(t *testing.T, client *awss3.Client, bucket, prefix string, timeout time.Duration) { t.Helper() trimmedPrefix := strings.Trim(prefix, "/") if trimmedPrefix != "" { trimmedPrefix += "/" } deadline := time.Now().Add(timeout) var lastErr error for { out, err := client.ListObjectsV2(context.Background(), &awss3.ListObjectsV2Input{ Bucket: aws.String(bucket), Prefix: aws.String(trimmedPrefix), }) if err == nil && len(out.Contents) > 0 { return } if err != nil { lastErr = err } else { lastErr = fmt.Errorf("no objects yet") } if time.Now().After(deadline) { t.Fatalf("timeout waiting for objects in bucket %s with prefix %s: last err %v", bucket, trimmedPrefix, lastErr) } time.Sleep(2 * time.Second) } } func compareRowCounts(srcPath, restoredPath string) error { srcDB, err := sql.Open("sqlite3", srcPath) if err != nil { return fmt.Errorf("open source db: %w", err) } defer srcDB.Close() restoredDB, err := sql.Open("sqlite3", restoredPath) if err != nil { return fmt.Errorf("open restored db: %w", err) } defer restoredDB.Close() tableName, err := findUserTable(srcDB) if err != nil { return err } var srcCount, restoredCount int if err := srcDB.QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM %s", tableName)).Scan(&srcCount); err != nil { return fmt.Errorf("count source: %w", err) } if err := restoredDB.QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM %s", tableName)).Scan(&restoredCount); err != nil { return fmt.Errorf("count restored: %w", err) } if srcCount != restoredCount { return fmt.Errorf("row mismatch: source=%d restored=%d", srcCount, restoredCount) } return nil } func findUserTable(db *sql.DB) (string, error) { var name string err := db.QueryRow(`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name LIMIT 1`).Scan(&name) if err != nil { return "", fmt.Errorf("find table: %w", err) } return name, nil } ================================================ FILE: tests/integration/s3_restore_connection_drop_test.go ================================================ //go:build integration && docker package integration import ( "bytes" "database/sql" "encoding/json" "fmt" "io" "net/http" "os" "os/exec" "path/filepath" "strings" "testing" "time" _ "github.com/mattn/go-sqlite3" ) // TestRestore_S3ConnectionDrop verifies restore can recover from dropped S3-compatible connections. func TestRestore_S3ConnectionDrop(t *testing.T) { RequireBinaries(t) RequireDocker(t) if testing.Short() { t.Skip("skipping in short mode") } networkName := startDockerNetwork(t) defer removeDockerNetwork(networkName) minioName := startMinioContainerForProxy(t, networkName) defer stopDockerContainer(minioName) toxiproxyName, toxiproxyAPIPort, toxiproxyProxyPort := startToxiproxyContainer(t, networkName) defer stopDockerContainer(toxiproxyName) bucket := fmt.Sprintf("litestream-test-%d", time.Now().UnixNano()) createMinioBucket(t, networkName, minioName, bucket) proxyEndpoint := fmt.Sprintf("http://localhost:%s", toxiproxyProxyPort) proxyClient := newToxiproxyClient(t, fmt.Sprintf("http://localhost:%s", toxiproxyAPIPort)) proxyClient.createProxy(t, "minio", "0.0.0.0:8666", fmt.Sprintf("%s:9000", minioName)) replicaPath := fmt.Sprintf("restore-drop-%d", time.Now().UnixNano()) replicaURL := fmt.Sprintf("s3://%s/%s", bucket, replicaPath) db := SetupTestDB(t, "s3-restore-connection-drop") defer db.Cleanup() if err := db.Create(); err != nil { t.Fatalf("create db: %v", err) } if err := db.Populate("100MB"); err != nil { t.Fatalf("populate db: %v", err) } configPath := writeS3Config(t, db.Path, replicaURL, proxyEndpoint) db.ReplicaURL = replicaURL if err := db.StartLitestreamWithConfig(configPath); err != nil { t.Fatalf("start litestream: %v", err) } time.Sleep(5 * time.Second) if err := insertLargeRows(db.Path, 5, 256*1024); err != nil { t.Fatalf("insert post-snapshot rows: %v", err) } time.Sleep(5 * time.Second) if err := db.StopLitestream(); err != nil { t.Fatalf("stop litestream: %v", err) } restorePath := filepath.Join(db.TempDir, "restored.db") restoreErr := make(chan error, 1) go func() { restoreErr <- db.Restore(restorePath) }() time.Sleep(200 * time.Millisecond) proxyClient.addResetPeerToxic(t, "minio", "reset-connection", 200) time.Sleep(400 * time.Millisecond) proxyClient.removeToxic(t, "minio", "reset-connection") if err := <-restoreErr; err != nil { t.Fatalf("restore failed: %v", err) } if err := verifyRestoredRowCount(restorePath, 5); err != nil { t.Fatalf("restore validation failed: %v", err) } } func startDockerNetwork(t *testing.T) string { t.Helper() name := fmt.Sprintf("litestream-net-%d", time.Now().UnixNano()) runDockerCommand(t, "network", "create", name) return name } func removeDockerNetwork(name string) { if name == "" { return } exec.Command("docker", "network", "rm", name).Run() } func startMinioContainerForProxy(t *testing.T, networkName string) string { t.Helper() name := fmt.Sprintf("litestream-minio-%d", time.Now().UnixNano()) exec.Command("docker", "rm", "-f", name).Run() runDockerCommand(t, "run", "-d", "--name", name, "--network", networkName, "-e", "MINIO_ROOT_USER=minioadmin", "-e", "MINIO_ROOT_PASSWORD=minioadmin", "minio/minio", "server", "/data", ) time.Sleep(3 * time.Second) return name } func startToxiproxyContainer(t *testing.T, networkName string) (string, string, string) { t.Helper() name := fmt.Sprintf("litestream-toxiproxy-%d", time.Now().UnixNano()) exec.Command("docker", "rm", "-f", name).Run() image := os.Getenv("LITESTREAM_TOXIPROXY_IMAGE") if image == "" { image = "ghcr.io/shopify/toxiproxy:2.5.0" } runDockerCommand(t, "run", "-d", "--name", name, "--network", networkName, "-p", "0:8474", "-p", "0:8666", image, ) apiPort := parseDockerPort(t, runDockerCommand(t, "port", name, "8474/tcp")) proxyPort := parseDockerPort(t, runDockerCommand(t, "port", name, "8666/tcp")) time.Sleep(2 * time.Second) return name, apiPort, proxyPort } func stopDockerContainer(name string) { if name == "" { return } exec.Command("docker", "rm", "-f", name).Run() } func createMinioBucket(t *testing.T, networkName, minioName, bucket string) { t.Helper() cmd := exec.Command("docker", "run", "--rm", "--network", networkName, "-e", fmt.Sprintf("MC_HOST_minio=http://minioadmin:minioadmin@%s:9000", minioName), "minio/mc", "mb", "minio/"+bucket, ) output, err := cmd.CombinedOutput() if err != nil && !strings.Contains(string(output), "already exists") { t.Fatalf("create bucket failed: %v output: %s", err, string(output)) } } func writeS3Config(t *testing.T, dbPath, replicaURL, endpoint string) string { t.Helper() configPath := filepath.Join(filepath.Dir(dbPath), "litestream-s3-drop.yml") config := fmt.Sprintf(`access-key-id: minioadmin secret-access-key: minioadmin dbs: - path: %s snapshot: interval: 1s retention: 1h replicas: - url: %s endpoint: %s region: us-east-1 force-path-style: true skip-verify: true sync-interval: 1s `, filepath.ToSlash(dbPath), replicaURL, endpoint) if err := os.WriteFile(configPath, []byte(config), 0600); err != nil { t.Fatalf("write config: %v", err) } return configPath } func insertLargeRows(dbPath string, rows int, blobSize int) error { sqlDB, err := sql.Open("sqlite3", dbPath) if err != nil { return err } defer sqlDB.Close() if _, err := sqlDB.Exec(`CREATE TABLE IF NOT EXISTS drop_test(id INTEGER PRIMARY KEY, data BLOB);`); err != nil { return err } for i := 0; i < rows; i++ { if _, err := sqlDB.Exec(`INSERT INTO drop_test(data) VALUES (randomblob(?));`, blobSize); err != nil { return err } } return nil } func verifyRestoredRowCount(dbPath string, expected int) error { sqlDB, err := sql.Open("sqlite3", dbPath) if err != nil { return err } defer sqlDB.Close() var count int if err := sqlDB.QueryRow(`SELECT COUNT(*) FROM drop_test;`).Scan(&count); err != nil { return err } if count != expected { return fmt.Errorf("restored row count: got %d want %d", count, expected) } return nil } type toxiproxyClient struct { baseURL string client *http.Client } func newToxiproxyClient(t *testing.T, baseURL string) *toxiproxyClient { t.Helper() return &toxiproxyClient{ baseURL: strings.TrimRight(baseURL, "/"), client: &http.Client{ Timeout: 5 * time.Second, }, } } func (c *toxiproxyClient) createProxy(t *testing.T, name, listen, upstream string) { t.Helper() payload := map[string]string{ "name": name, "listen": listen, "upstream": upstream, } c.postJSON(t, "/proxies", payload, http.StatusOK) } func (c *toxiproxyClient) addResetPeerToxic(t *testing.T, proxy, name string, timeoutMS int) { t.Helper() payload := map[string]interface{}{ "name": name, "type": "reset_peer", "stream": "downstream", "toxicity": 1.0, "attributes": map[string]int{ "timeout": timeoutMS, }, } c.postJSON(t, fmt.Sprintf("/proxies/%s/toxics", proxy), payload, http.StatusOK) } func (c *toxiproxyClient) removeToxic(t *testing.T, proxy, name string) { t.Helper() req, err := http.NewRequest(http.MethodDelete, c.baseURL+fmt.Sprintf("/proxies/%s/toxics/%s", proxy, name), nil) if err != nil { t.Fatalf("create delete request: %v", err) } resp, err := c.client.Do(req) if err != nil { t.Fatalf("delete toxic: %v", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(resp.Body) t.Fatalf("delete toxic failed: status=%d body=%s", resp.StatusCode, string(body)) } } func (c *toxiproxyClient) postJSON(t *testing.T, path string, payload interface{}, expectedStatus int) { t.Helper() body, err := json.Marshal(payload) if err != nil { t.Fatalf("marshal payload: %v", err) } req, err := http.NewRequest(http.MethodPost, c.baseURL+path, bytes.NewReader(body)) if err != nil { t.Fatalf("create request: %v", err) } req.Header.Set("Content-Type", "application/json") resp, err := c.client.Do(req) if err != nil { t.Fatalf("post %s: %v", path, err) } defer resp.Body.Close() if resp.StatusCode != expectedStatus { if expectedStatus == http.StatusOK && resp.StatusCode == http.StatusCreated { return } respBody, _ := io.ReadAll(resp.Body) t.Fatalf("post %s failed: status=%d body=%s", path, resp.StatusCode, string(respBody)) } } ================================================ FILE: tests/integration/scenario_test.go ================================================ //go:build integration package integration import ( "database/sql" "fmt" "os" "path/filepath" "testing" "time" _ "github.com/mattn/go-sqlite3" ) func TestFreshStart(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } RequireBinaries(t) t.Log("Testing: Starting replication with a fresh (empty) database") t.Log("This tests if Litestream works correctly when it creates the database from scratch") db := SetupTestDB(t, "fresh-start") defer db.Cleanup() t.Log("[1] Starting Litestream with non-existent database...") if err := db.StartLitestream(); err != nil { t.Fatalf("Failed to start Litestream: %v", err) } time.Sleep(2 * time.Second) t.Log("[2] Creating database while Litestream is running...") sqlDB, err := sql.Open("sqlite3", db.Path) if err != nil { t.Fatalf("Failed to open database: %v", err) } if _, err := sqlDB.Exec("PRAGMA journal_mode=WAL"); err != nil { t.Fatalf("Failed to set WAL mode: %v", err) } if _, err := sqlDB.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY, data TEXT)"); err != nil { t.Fatalf("Failed to create table: %v", err) } if _, err := sqlDB.Exec("INSERT INTO test (data) VALUES ('initial data')"); err != nil { t.Fatalf("Failed to insert initial data: %v", err) } sqlDB.Close() time.Sleep(3 * time.Second) t.Log("[3] Checking if Litestream detected the database...") log, err := db.GetLitestreamLog() if err != nil { t.Fatalf("Failed to read log: %v", err) } t.Logf("Litestream log snippet:\n%s", log[:min(len(log), 500)]) t.Log("[4] Adding data to test replication...") sqlDB, err = sql.Open("sqlite3", db.Path) if err != nil { t.Fatalf("Failed to open database: %v", err) } for i := 1; i <= 100; i++ { if _, err := sqlDB.Exec("INSERT INTO test (data) VALUES (?)", fmt.Sprintf("row %d", i)); err != nil { t.Fatalf("Failed to insert row %d: %v", i, err) } } sqlDB.Close() time.Sleep(5 * time.Second) t.Log("[5] Checking for errors...") errors, err := db.CheckForErrors() if err != nil { t.Fatalf("Failed to check errors: %v", err) } if len(errors) > 1 { t.Logf("Found %d errors (showing first 3):", len(errors)) for i := 0; i < min(len(errors), 3); i++ { t.Logf(" %s", errors[i]) } } else { t.Log("✓ No significant errors") } t.Log("[6] Checking replica files...") fileCount, err := db.GetReplicaFileCount() if err != nil { t.Fatalf("Failed to get replica file count: %v", err) } if fileCount == 0 { t.Fatal("✗ No replica files created!") } t.Logf("✓ Replica created with %d LTX files", fileCount) db.StopLitestream() time.Sleep(2 * time.Second) t.Log("[7] Testing restore...") restoredPath := filepath.Join(db.TempDir, "fresh-restored.db") if err := db.Restore(restoredPath); err != nil { t.Fatalf("✗ Restore failed: %v", err) } t.Log("✓ Restore successful") origCount, err := db.GetRowCount("test") if err != nil { t.Fatalf("Failed to get original row count: %v", err) } restoredDB := &TestDB{Path: restoredPath, t: t} restCount, err := restoredDB.GetRowCount("test") if err != nil { t.Fatalf("Failed to get restored row count: %v", err) } if origCount != restCount { t.Fatalf("✗ Data mismatch: Original=%d, Restored=%d", origCount, restCount) } t.Logf("✓ Data integrity verified: %d rows", origCount) t.Log("TEST PASSED: Fresh start works correctly") } func TestDatabaseIntegrity(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } RequireBinaries(t) t.Log("Testing: Complex data patterns and integrity after restore") db := SetupTestDB(t, "integrity-test") defer db.Cleanup() if err := db.Create(); err != nil { t.Fatalf("Failed to create database: %v", err) } t.Log("[1] Creating complex schema...") sqlDB, err := sql.Open("sqlite3", db.Path) if err != nil { t.Fatalf("Failed to open database: %v", err) } defer sqlDB.Close() if err := CreateComplexTestSchema(sqlDB); err != nil { t.Fatalf("Failed to create schema: %v", err) } t.Log("✓ Schema created") t.Log("[2] Populating with test data...") if err := PopulateComplexTestData(sqlDB, 10, 5, 3); err != nil { t.Fatalf("Failed to populate data: %v", err) } t.Log("✓ Data populated (10 users, 50 posts, 150 comments)") t.Log("[3] Starting Litestream...") if err := db.StartLitestream(); err != nil { t.Fatalf("Failed to start Litestream: %v", err) } time.Sleep(10 * time.Second) db.StopLitestream() time.Sleep(2 * time.Second) t.Log("[4] Checking integrity of original database...") var integrityResult string if err := sqlDB.QueryRow("PRAGMA integrity_check").Scan(&integrityResult); err != nil { t.Fatalf("Integrity check failed: %v", err) } if integrityResult != "ok" { t.Fatalf("Source database integrity check failed: %s", integrityResult) } t.Log("✓ Source database integrity OK") t.Log("[5] Restoring database...") restoredPath := filepath.Join(db.TempDir, "integrity-restored.db") if err := db.Restore(restoredPath); err != nil { t.Fatalf("Restore failed: %v", err) } t.Log("✓ Restore successful") t.Log("[6] Checking integrity of restored database...") restoredDB, err := sql.Open("sqlite3", restoredPath) if err != nil { t.Fatalf("Failed to open restored database: %v", err) } defer restoredDB.Close() if err := restoredDB.QueryRow("PRAGMA integrity_check").Scan(&integrityResult); err != nil { t.Fatalf("Restored integrity check failed: %v", err) } if integrityResult != "ok" { t.Fatalf("Restored database integrity check failed: %s", integrityResult) } t.Log("✓ Restored database integrity OK") t.Log("[7] Validating data consistency...") tables := []string{"users", "posts", "comments"} for _, table := range tables { var sourceCount, restoredCount int if err := sqlDB.QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM %s", table)).Scan(&sourceCount); err != nil { t.Fatalf("Failed to count source %s: %v", table, err) } if err := restoredDB.QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM %s", table)).Scan(&restoredCount); err != nil { t.Fatalf("Failed to count restored %s: %v", table, err) } if sourceCount != restoredCount { t.Fatalf("Count mismatch for %s: source=%d, restored=%d", table, sourceCount, restoredCount) } t.Logf("✓ Table %s: %d rows match", table, sourceCount) } t.Log("TEST PASSED: Database integrity maintained through replication") } func TestDatabaseDeletion(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } RequireBinaries(t) t.Log("Testing: Database deletion during active replication") db := SetupTestDB(t, "deletion-test") defer db.Cleanup() if err := db.Create(); err != nil { t.Fatalf("Failed to create database: %v", err) } t.Log("[1] Creating test table and data...") if err := CreateTestTable(t, db.Path); err != nil { t.Fatalf("Failed to create table: %v", err) } if err := InsertTestData(t, db.Path, 100); err != nil { t.Fatalf("Failed to insert test data: %v", err) } t.Log("✓ Created table with 100 rows") t.Log("[2] Starting Litestream...") if err := db.StartLitestream(); err != nil { t.Fatalf("Failed to start Litestream: %v", err) } time.Sleep(5 * time.Second) fileCount, _ := db.GetReplicaFileCount() t.Logf("✓ Replication started (%d files)", fileCount) t.Log("[3] Deleting database files...") os.Remove(db.Path) os.Remove(db.Path + "-wal") os.Remove(db.Path + "-shm") time.Sleep(3 * time.Second) t.Log("✓ Database deleted") t.Log("[4] Checking Litestream behavior...") errors, err := db.CheckForErrors() if err != nil { t.Fatalf("Failed to check errors: %v", err) } t.Logf("Litestream reported %d error messages (expected after database deletion)", len(errors)) db.StopLitestream() t.Log("[5] Verifying replica is still intact...") finalFileCount, err := db.GetReplicaFileCount() if err != nil { t.Fatalf("Failed to check replica: %v", err) } if finalFileCount == 0 { t.Fatalf("Replica appears to be empty or missing") } t.Logf("✓ Replica exists with %d files (was %d - compaction may have reduced count)", finalFileCount, fileCount) t.Log("[6] Testing restore from replica...") restoredPath := filepath.Join(db.TempDir, "deletion-restored.db") if err := db.Restore(restoredPath); err != nil { t.Fatalf("Restore failed: %v", err) } t.Log("✓ Restore successful") restoredDB := &TestDB{Path: restoredPath, t: t} restCount, err := restoredDB.GetRowCount("test_data") if err != nil { t.Fatalf("Failed to get restored row count: %v", err) } if restCount != 100 { t.Fatalf("Expected 100 rows, got %d", restCount) } t.Logf("✓ Restored database has correct data: %d rows", restCount) t.Log("TEST PASSED: Replica survives source database deletion") } // TestReplicaFailover was removed because Litestream no longer supports // multiple replicas on a single database (see cmd/litestream/main.go). // The bash script test-replica-failover.sh was also non-functional. func min(a, b int) int { if a < b { return a } return b } ================================================ FILE: tests/integration/shutdown_retry_test.go ================================================ //go:build integration && docker package integration import ( "bytes" "context" "database/sql" "fmt" "io" "net" "net/http" "net/http/httputil" "net/url" "os" "os/exec" "path/filepath" "strings" "sync" "sync/atomic" "syscall" "testing" "time" _ "github.com/mattn/go-sqlite3" ) // TestShutdownSyncRetry_429Errors tests that Litestream retries syncing LTX files // during shutdown when receiving 429 (Too Many Requests) errors. // // This test: // 1. Starts a MinIO container // 2. Starts a rate-limiting proxy in front of MinIO that returns 429 for first N PUT requests // 3. Starts Litestream replicating to the proxy endpoint // 4. Writes data and syncs // 5. Sends SIGTERM to trigger graceful shutdown // 6. Verifies that Litestream retries and eventually succeeds despite 429 errors // // Requirements: // - Docker must be running // - Litestream binary must be built at ../../bin/litestream func TestShutdownSyncRetry_429Errors(t *testing.T) { RequireBinaries(t) RequireDocker(t) t.Log("================================================") t.Log("Litestream Shutdown Sync Retry Test (429 Errors)") t.Log("================================================") t.Log("") // Start MinIO container t.Log("Starting MinIO container...") containerName, minioEndpoint := StartMinioTestContainer(t) defer StopMinioTestContainer(t, containerName) t.Logf("✓ MinIO running at: %s", minioEndpoint) // Create MinIO bucket by creating directory in /data (MinIO stores buckets as directories) bucket := "litestream-test" t.Logf("Creating bucket '%s'...", bucket) // Wait for MinIO to be ready time.Sleep(2 * time.Second) // Create bucket directory directly - MinIO uses /data as the storage root createBucketCmd := exec.Command("docker", "exec", containerName, "mkdir", "-p", "/data/"+bucket) if out, err := createBucketCmd.CombinedOutput(); err != nil { t.Fatalf("Failed to create bucket directory: %v, output: %s", err, string(out)) } t.Log("✓ Bucket created") t.Log("") // Start rate-limiting proxy t.Log("Starting rate-limiting proxy...") proxy := newRateLimitingProxy(t, minioEndpoint, 3) // Return 429 for first 3 PUT requests proxyServer := &http.Server{ Addr: "127.0.0.1:0", Handler: proxy, } listener, err := (&net.ListenConfig{}).Listen(context.Background(), "tcp", "127.0.0.1:0") if err != nil { t.Fatalf("Failed to create listener: %v", err) } proxyAddr := listener.Addr().String() go func() { if err := proxyServer.Serve(listener); err != nil && err != http.ErrServerClosed { t.Logf("Proxy server error: %v", err) } }() defer proxyServer.Close() proxyEndpoint := fmt.Sprintf("http://%s", proxyAddr) t.Logf("✓ Rate-limiting proxy running at: %s", proxyEndpoint) t.Logf(" (Will return 429 for first 3 PUT requests during shutdown)") t.Log("") // Setup test database tempDir := t.TempDir() dbPath := filepath.Join(tempDir, "test.db") configPath := filepath.Join(tempDir, "litestream.yml") // Create database with some data t.Log("Creating test database...") sqlDB, err := sql.Open("sqlite3", dbPath) if err != nil { t.Fatalf("Failed to open database: %v", err) } if _, err := sqlDB.Exec("PRAGMA journal_mode=WAL"); err != nil { t.Fatalf("Failed to set WAL mode: %v", err) } if _, err := sqlDB.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY, data TEXT)"); err != nil { t.Fatalf("Failed to create table: %v", err) } if _, err := sqlDB.Exec("INSERT INTO test (data) VALUES ('initial data')"); err != nil { t.Fatalf("Failed to insert data: %v", err) } sqlDB.Close() t.Log("✓ Database created with initial data") t.Log("") // Create Litestream config with shutdown retry settings s3Path := fmt.Sprintf("test-%d", time.Now().Unix()) config := fmt.Sprintf(` shutdown-sync-timeout: 10s shutdown-sync-interval: 500ms dbs: - path: %s replica: type: s3 bucket: %s path: %s endpoint: %s access-key-id: minioadmin secret-access-key: minioadmin region: us-east-1 force-path-style: true skip-verify: true sync-interval: 1s `, dbPath, bucket, s3Path, proxyEndpoint) if err := os.WriteFile(configPath, []byte(config), 0644); err != nil { t.Fatalf("Failed to write config: %v", err) } t.Logf("✓ Config written to: %s", configPath) t.Log("") // Start Litestream t.Log("Starting Litestream...") litestreamBin := filepath.Join("..", "..", "bin", "litestream") cmd := exec.Command(litestreamBin, "replicate", "-config", configPath) var stdout, stderr bytes.Buffer cmd.Stdout = io.MultiWriter(os.Stdout, &stdout) cmd.Stderr = io.MultiWriter(os.Stderr, &stderr) if err := cmd.Start(); err != nil { t.Fatalf("Failed to start Litestream: %v", err) } t.Logf("✓ Litestream started (PID: %d)", cmd.Process.Pid) // Wait for initial sync t.Log("Waiting for initial sync...") time.Sleep(3 * time.Second) // Write more data to ensure we have pending LTX files t.Log("Writing additional data...") sqlDB, err = sql.Open("sqlite3", dbPath) if err != nil { t.Fatalf("Failed to reopen database: %v", err) } for i := 0; i < 5; i++ { if _, err := sqlDB.Exec("INSERT INTO test (data) VALUES (?)", fmt.Sprintf("data-%d", i)); err != nil { t.Fatalf("Failed to insert data: %v", err) } } sqlDB.Close() t.Log("✓ Additional data written") // Wait a bit for sync to pick up the changes time.Sleep(2 * time.Second) // Reset proxy counter so 429s happen during shutdown proxy.Reset() t.Log("") t.Log("Sending SIGTERM to trigger graceful shutdown...") t.Log("(Proxy will return 429 for first 3 PUT requests)") // Send SIGTERM if err := cmd.Process.Signal(syscall.SIGTERM); err != nil { t.Fatalf("Failed to send SIGTERM: %v", err) } // Wait for process to exit with timeout done := make(chan error, 1) go func() { done <- cmd.Wait() }() select { case err := <-done: if err != nil { // Check if it's just a signal exit (expected) if exitErr, ok := err.(*exec.ExitError); ok { t.Logf("Litestream exited with: %v", exitErr) } else { t.Fatalf("Litestream failed: %v", err) } } case <-time.After(30 * time.Second): cmd.Process.Kill() t.Fatal("Litestream did not exit within 30 seconds") } t.Log("") t.Log("================================================") t.Log("Results") t.Log("================================================") // Check proxy statistics stats := proxy.Stats() t.Logf("Proxy statistics:") t.Logf(" Total requests: %d", stats.TotalRequests) t.Logf(" 429 responses sent: %d", stats.RateLimited) t.Logf(" Forwarded requests: %d", stats.Forwarded) // Verify that we saw 429s and retries succeeded output := stdout.String() + stderr.String() if !strings.Contains(output, "shutdown sync failed, retrying") { t.Log("") t.Log("WARNING: Did not see retry messages in output.") t.Log("This could mean:") t.Log(" 1. No pending LTX files during shutdown") t.Log(" 2. Sync completed before shutdown signal") t.Log(" 3. Retry logic not triggered") } else { t.Log("") t.Log("✓ Saw retry messages - shutdown sync retry is working!") } if strings.Contains(output, "shutdown sync succeeded after retry") { t.Log("✓ Shutdown sync succeeded after retrying!") } if stats.RateLimited > 0 { t.Logf("✓ Proxy returned %d 429 responses as expected", stats.RateLimited) } t.Log("") t.Log("Test completed successfully!") } // rateLimitingProxy is an HTTP proxy that returns 429 for the first N PUT requests type rateLimitingProxy struct { target *url.URL proxy *httputil.ReverseProxy mu sync.Mutex putCount int32 limit int32 totalReqs int64 rateLimited int64 forwarded int64 t *testing.T } type proxyStats struct { TotalRequests int64 RateLimited int64 Forwarded int64 } func newRateLimitingProxy(t *testing.T, targetURL string, limit int) *rateLimitingProxy { target, err := url.Parse(targetURL) if err != nil { t.Fatalf("Failed to parse target URL: %v", err) } p := &rateLimitingProxy{ target: target, limit: int32(limit), t: t, } p.proxy = &httputil.ReverseProxy{ Director: func(req *http.Request) { req.URL.Scheme = target.Scheme req.URL.Host = target.Host // Don't modify Host header - it's part of the AWS signature }, } return p } func (p *rateLimitingProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { atomic.AddInt64(&p.totalReqs, 1) // Only rate limit PUT requests (uploads) if r.Method == "PUT" { count := atomic.AddInt32(&p.putCount, 1) if count <= p.limit { atomic.AddInt64(&p.rateLimited, 1) p.t.Logf("PROXY: Returning 429 for PUT request #%d (limit: %d)", count, p.limit) w.Header().Set("Retry-After", "1") w.WriteHeader(http.StatusTooManyRequests) w.Write([]byte("Rate limit exceeded")) return } } atomic.AddInt64(&p.forwarded, 1) p.proxy.ServeHTTP(w, r) } func (p *rateLimitingProxy) Reset() { atomic.StoreInt32(&p.putCount, 0) } func (p *rateLimitingProxy) Stats() proxyStats { return proxyStats{ TotalRequests: atomic.LoadInt64(&p.totalReqs), RateLimited: atomic.LoadInt64(&p.rateLimited), Forwarded: atomic.LoadInt64(&p.forwarded), } } ================================================ FILE: tests/integration/soak_helpers.go ================================================ //go:build integration && soak package integration import ( "bufio" "context" "database/sql" "fmt" "os" "os/exec" "os/signal" "path/filepath" "strconv" "strings" "syscall" "testing" "time" ) // S3Config holds S3-specific configuration type S3Config struct { Endpoint string AccessKey string SecretKey string Region string ForcePathStyle bool SkipVerify bool SSE string SSEKMSKeyID string } // TestInfo holds test state for signal handler and monitoring type TestInfo struct { StartTime time.Time Duration time.Duration RowCount int FileCount int DB *TestDB cancel context.CancelFunc } // ErrorStats holds error categorization and counts type ErrorStats struct { TotalCount int CriticalCount int BenignCount int RecentErrors []string ErrorsByType map[string]int } func isInteractive() bool { if fi, err := os.Stdin.Stat(); err == nil { return fi.Mode()&os.ModeCharDevice != 0 } return false } func promptYesNo(t *testing.T, prompt string, defaultYes bool) bool { t.Helper() switch strings.ToLower(strings.TrimSpace(os.Getenv("SOAK_AUTO_PURGE"))) { case "y", "yes", "true", "1", "on": t.Logf("%s yes (SOAK_AUTO_PURGE)", prompt) return true case "n", "no", "false", "0", "off": t.Logf("%s no (SOAK_AUTO_PURGE)", prompt) return false } if !isInteractive() { if defaultYes { t.Logf("%s yes (non-interactive default)", prompt) return true } t.Logf("%s no (non-interactive default)", prompt) return false } defPrompt := "[y/N]" if defaultYes { defPrompt = "[Y/n]" } fmt.Printf("%s %s ", prompt, defPrompt) reader := bufio.NewReader(os.Stdin) text, err := reader.ReadString('\n') if err != nil { t.Logf("Failed to read response: %v (defaulting to no)", err) return false } switch strings.ToLower(strings.TrimSpace(text)) { case "", "y", "yes": if defaultYes || text != "" { return true } return false case "n", "no": return false default: return defaultYes } } func promptYesNoDefaultNo(t *testing.T, prompt string) bool { return promptYesNo(t, prompt, false) } func promptYesNoDefaultYes(t *testing.T, prompt string) bool { return promptYesNo(t, prompt, true) } // StartMinIOContainer starts a MinIO container and returns the container ID and endpoint func StartMinIOContainer(t *testing.T) (containerID string, endpoint string, volumeName string) { t.Helper() containerName := fmt.Sprintf("litestream-test-minio-%d", time.Now().Unix()) volumeName = fmt.Sprintf("litestream-test-minio-data-%d", time.Now().Unix()) minioPort := "9100" consolePort := "9101" // Clean up any existing container exec.Command("docker", "stop", containerName).Run() exec.Command("docker", "rm", containerName).Run() // Remove any lingering volume with the same name, then create fresh volume. exec.Command("docker", "volume", "rm", volumeName).Run() if out, err := exec.Command("docker", "volume", "create", volumeName).CombinedOutput(); err != nil { t.Fatalf("Failed to create MinIO volume: %v\nOutput: %s", err, string(out)) } // Start MinIO container cmd := exec.Command("docker", "run", "-d", "--name", containerName, "-p", minioPort+":9000", "-p", consolePort+":9001", "-v", volumeName+":/data", "-e", "MINIO_ROOT_USER=minioadmin", "-e", "MINIO_ROOT_PASSWORD=minioadmin", "minio/minio", "server", "/data", "--console-address", ":9001") output, err := cmd.CombinedOutput() if err != nil { t.Fatalf("Failed to start MinIO container: %v\nOutput: %s", err, string(output)) } containerID = strings.TrimSpace(string(output)) endpoint = fmt.Sprintf("http://localhost:%s", minioPort) // Wait for MinIO to be ready time.Sleep(5 * time.Second) // Verify container is running cmd = exec.Command("docker", "ps", "-q", "-f", "name="+containerName) output, err = cmd.CombinedOutput() if err != nil || len(strings.TrimSpace(string(output))) == 0 { t.Fatalf("MinIO container failed to start properly") } t.Logf("MinIO container started: %s (endpoint: %s)", containerID[:12], endpoint) return containerID, endpoint, volumeName } // StopMinIOContainer stops and removes a MinIO container func StopMinIOContainer(t *testing.T, containerID string, volumeName string) { t.Helper() if containerID == "" { return } t.Logf("Stopping MinIO container: %s", containerID[:12]) exec.Command("docker", "stop", containerID).Run() exec.Command("docker", "rm", containerID).Run() if volumeName != "" { exec.Command("docker", "volume", "rm", volumeName).Run() } } // CreateMinIOBucket creates a bucket in MinIO func CreateMinIOBucket(t *testing.T, containerID, bucket string) { t.Helper() if minioBucketExists(containerID, bucket) { if promptYesNoDefaultYes(t, fmt.Sprintf("Bucket '%s' already exists. Purge existing objects before running soak test?", bucket)) { t.Logf("Purging MinIO bucket '%s'...", bucket) if err := clearMinIOBucket(containerID, bucket); err != nil { t.Fatalf("Failed to purge MinIO bucket: %v", err) } } else { t.Logf("Skipping purge of bucket '%s'. Residual data may cause replication errors.", bucket) } } // Use mc (MinIO Client) via docker to create bucket cmd := exec.Command("docker", "run", "--rm", "--link", containerID+":minio", "-e", "MC_HOST_minio=http://minioadmin:minioadmin@minio:9000", "minio/mc", "mb", "minio/"+bucket) _, stdoutBuf, stderrBuf := configureCmdIO(cmd) if err := cmd.Run(); err != nil { output := combinedOutput(stdoutBuf, stderrBuf) if !strings.Contains(output, "already exists") { t.Fatalf("Create bucket failed: %v Output: %s", err, output) } } if err := waitForMinIOBucket(containerID, bucket, 60*time.Second); err != nil { t.Fatalf("Bucket %s not ready: %v", bucket, err) } if err := clearMinIOBucket(containerID, bucket); err != nil { t.Fatalf("Failed to purge MinIO bucket: %v", err) } t.Logf("MinIO bucket '%s' ready", bucket) } func minioBucketExists(containerID, bucket string) bool { cmd := exec.Command("docker", "run", "--rm", "--link", containerID+":minio", "-e", "MC_HOST_minio=http://minioadmin:minioadmin@minio:9000", "minio/mc", "ls", "minio/"+bucket+"/") _, _, _ = configureCmdIO(cmd) if err := cmd.Run(); err != nil { return false } return true } func clearMinIOBucket(containerID, bucket string) error { cmd := exec.Command("docker", "run", "--rm", "--link", containerID+":minio", "-e", "MC_HOST_minio=http://minioadmin:minioadmin@minio:9000", "minio/mc", "rm", "--recursive", "--force", "minio/"+bucket) _, stdoutBuf, stderrBuf := configureCmdIO(cmd) if err := cmd.Run(); err != nil { output := combinedOutput(stdoutBuf, stderrBuf) if output != "" { return fmt.Errorf("%w: %s", err, output) } return err } return nil } func waitForMinIOBucket(containerID, bucket string, timeout time.Duration) error { deadline := time.Now().Add(timeout) for { if minioBucketExists(containerID, bucket) { return nil } if time.Now().After(deadline) { break } time.Sleep(500 * time.Millisecond) } return fmt.Errorf("bucket %s not available", bucket) } // CountMinIOObjects counts objects in a MinIO bucket func CountMinIOObjects(t *testing.T, containerID, bucket string) int { t.Helper() cmd := exec.Command("docker", "run", "--rm", "--link", containerID+":minio", "-e", "MC_HOST_minio=http://minioadmin:minioadmin@minio:9000", "minio/mc", "ls", "minio/"+bucket+"/", "--recursive") _, stdoutBuf, stderrBuf := configureCmdIO(cmd) if err := cmd.Run(); err != nil { return 0 } output := combinedOutput(stdoutBuf, stderrBuf) lines := strings.Split(strings.TrimSpace(output), "\n") if len(lines) == 1 && lines[0] == "" { return 0 } return len(lines) } // CheckAWSCredentials checks if AWS credentials are set and returns bucket and region func CheckAWSCredentials(t *testing.T) (bucket, region string) { t.Helper() accessKey := os.Getenv("AWS_ACCESS_KEY_ID") secretKey := os.Getenv("AWS_SECRET_ACCESS_KEY") bucket = os.Getenv("S3_BUCKET") region = os.Getenv("AWS_REGION") if accessKey == "" || secretKey == "" || bucket == "" { t.Skip("AWS credentials not set. Set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and S3_BUCKET") } if region == "" { region = "us-east-1" } t.Logf("Using AWS S3: bucket=%s, region=%s", bucket, region) return bucket, region } // TestS3Connectivity tests if we can access the S3 bucket func TestS3Connectivity(t *testing.T, bucket string) { t.Helper() cmd := exec.Command("aws", "s3", "ls", "s3://"+bucket+"/") if err := cmd.Run(); err != nil { t.Fatalf("Failed to access S3 bucket '%s': %v\nEnsure AWS CLI is installed and credentials are valid", bucket, err) } t.Logf("✓ S3 bucket '%s' is accessible", bucket) } // CountS3Objects counts objects in an S3 path func CountS3Objects(t *testing.T, s3URL string) int { t.Helper() cmd := exec.Command("aws", "s3", "ls", s3URL+"/", "--recursive") _, stdoutBuf, stderrBuf := configureCmdIO(cmd) if err := cmd.Run(); err != nil { return 0 } output := combinedOutput(stdoutBuf, stderrBuf) lines := strings.Split(strings.TrimSpace(output), "\n") if len(lines) == 1 && lines[0] == "" { return 0 } return len(lines) } // GetS3StorageSize gets the total storage size of an S3 path func GetS3StorageSize(t *testing.T, s3URL string) int64 { t.Helper() cmd := exec.Command("aws", "s3", "ls", s3URL+"/", "--recursive", "--summarize") _, stdoutBuf, stderrBuf := configureCmdIO(cmd) if err := cmd.Run(); err != nil { return 0 } output := combinedOutput(stdoutBuf, stderrBuf) lines := strings.Split(output, "\n") for _, line := range lines { if strings.Contains(line, "Total Size:") { var size int64 fmt.Sscanf(line, "Total Size: %d", &size) return size } } return 0 } // CreateSoakConfig creates a litestream configuration file for soak tests func CreateSoakConfig(dbPath, replicaURL string, s3Config *S3Config, shortMode bool) string { tempDir := filepath.Dir(dbPath) configPath := filepath.Join(tempDir, "litestream.yml") var config strings.Builder snapshotInterval := "10m" snapshotRetention := "1h" retentionCheckInterval := "5m" levelIntervals := []string{"30s", "1m", "5m", "15m", "30m"} if shortMode { snapshotInterval = "30s" snapshotRetention = "10m" retentionCheckInterval = "2m" levelIntervals = []string{"15s", "30s", "1m"} } // Add S3 credentials if provided if s3Config != nil && s3Config.AccessKey != "" { config.WriteString(fmt.Sprintf("access-key-id: %s\n", s3Config.AccessKey)) config.WriteString(fmt.Sprintf("secret-access-key: %s\n", s3Config.SecretKey)) config.WriteString("\n") } // Aggressive snapshot settings for testing config.WriteString("snapshot:\n") config.WriteString(fmt.Sprintf(" interval: %s\n", snapshotInterval)) config.WriteString(fmt.Sprintf(" retention: %s\n", snapshotRetention)) config.WriteString("\n") // Aggressive compaction levels config.WriteString("levels:\n") for _, interval := range levelIntervals { config.WriteString(fmt.Sprintf(" - interval: %s\n", interval)) } config.WriteString("\n") // Database configuration config.WriteString("dbs:\n") config.WriteString(fmt.Sprintf(" - path: %s\n", filepath.ToSlash(dbPath))) config.WriteString(" checkpoint-interval: 1m\n") config.WriteString(" min-checkpoint-page-count: 100\n") config.WriteString(" truncate-page-n: 5000\n") config.WriteString("\n") config.WriteString(" replica:\n") config.WriteString(fmt.Sprintf(" url: %s\n", replicaURL)) // Add S3-specific settings if provided if s3Config != nil { if s3Config.Endpoint != "" { config.WriteString(fmt.Sprintf(" endpoint: %s\n", s3Config.Endpoint)) } if s3Config.Region != "" { config.WriteString(fmt.Sprintf(" region: %s\n", s3Config.Region)) } if s3Config.ForcePathStyle { config.WriteString(" force-path-style: true\n") } if s3Config.SkipVerify { config.WriteString(" skip-verify: true\n") } if s3Config.SSE != "" { config.WriteString(fmt.Sprintf(" sse: %s\n", s3Config.SSE)) } if s3Config.SSEKMSKeyID != "" { config.WriteString(fmt.Sprintf(" sse-kms-key-id: %s\n", s3Config.SSEKMSKeyID)) } config.WriteString(fmt.Sprintf(" retention-check-interval: %s\n", retentionCheckInterval)) } if err := os.WriteFile(configPath, []byte(config.String()), 0644); err != nil { panic(fmt.Sprintf("Failed to create config file: %v", err)) } return configPath } // setupSignalHandler sets up SIGINT/SIGTERM handler with confirmation func setupSignalHandler(t *testing.T, cancel context.CancelFunc, testInfo *TestInfo) { t.Helper() sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) go func() { firstInterrupt := true for sig := range sigChan { if firstInterrupt { firstInterrupt = false t.Logf("") t.Logf("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━") t.Logf("⚠ Interrupt signal received (%v)", sig) t.Logf("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━") t.Logf("") elapsed := time.Since(testInfo.StartTime) remaining := testInfo.Duration - elapsed pct := float64(elapsed) / float64(testInfo.Duration) * 100 t.Logf("Test Progress:") t.Logf(" Elapsed: %v (%.0f%% complete)", elapsed.Round(time.Second), pct) t.Logf(" Remaining: %v", remaining.Round(time.Second)) t.Logf(" Data collected: %d rows, %d replica files", testInfo.RowCount, testInfo.FileCount) t.Logf("") t.Logf("Press Ctrl+C again within 5 seconds to confirm shutdown.") t.Logf("Otherwise, test will continue...") t.Logf("") // Wait 5 seconds for second interrupt timeout := time.NewTimer(5 * time.Second) select { case <-sigChan: // Second interrupt - confirmed shutdown timeout.Stop() t.Logf("Shutdown confirmed. Initiating graceful cleanup...") cancel() // Cancel context to stop test performGracefulShutdown(t, testInfo) return case <-timeout.C: // Timeout - continue test t.Logf("No confirmation received. Continuing test...") t.Logf("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━") t.Logf("") firstInterrupt = true } } else { // Second interrupt received t.Logf("Shutdown confirmed. Initiating graceful cleanup...") cancel() performGracefulShutdown(t, testInfo) return } } }() t.Cleanup(func() { signal.Stop(sigChan) close(sigChan) }) } // performGracefulShutdown performs cleanup on early termination func performGracefulShutdown(t *testing.T, testInfo *TestInfo) { t.Helper() if testInfo.cancel != nil { testInfo.cancel() } t.Log("") t.Log("================================================") t.Log("Graceful Shutdown - Early Termination") t.Log("================================================") t.Log("") elapsed := time.Since(testInfo.StartTime) // Stop Litestream gracefully t.Log("Stopping Litestream...") if err := testInfo.DB.StopLitestream(); err != nil { t.Logf("Warning: Error stopping Litestream: %v", err) } else { t.Log("✓ Litestream stopped") } // Wait for pending operations t.Log("Waiting for pending operations to complete...") time.Sleep(2 * time.Second) // Show partial results t.Log("") t.Log("Partial Test Results:") t.Logf(" Test duration: %v (%.0f%% of planned %v)", elapsed.Round(time.Second), float64(elapsed)/float64(testInfo.Duration)*100, testInfo.Duration.Round(time.Minute)) if dbSize, err := testInfo.DB.GetDatabaseSize(); err == nil { t.Logf(" Database size: %.2f MB", float64(dbSize)/(1024*1024)) } if rowCount, err := testInfo.DB.GetRowCount("load_test"); err == nil { t.Logf(" Rows inserted: %d", rowCount) if elapsed.Seconds() > 0 { rate := float64(rowCount) / elapsed.Seconds() t.Logf(" Average write rate: %.1f rows/second", rate) } } if fileCount, err := testInfo.DB.GetReplicaFileCount(); err == nil { t.Logf(" Replica LTX files: %d", fileCount) } // Run abbreviated analysis t.Log("") t.Log("Analyzing partial test data...") analysis := AnalyzeSoakTest(t, testInfo.DB, elapsed) t.Log("") t.Log("What Was Validated (Partial):") if analysis.SnapshotCount > 0 { t.Logf(" ✓ Snapshots: %d generated", analysis.SnapshotCount) } if analysis.TotalCompactions > 0 { t.Logf(" ✓ Compactions: %d completed", analysis.TotalCompactions) } if analysis.DatabaseRows > 0 { t.Logf(" ✓ Data written: %d rows", analysis.DatabaseRows) } // Check for errors errors, _ := testInfo.DB.CheckForErrors() t.Logf(" Critical errors: %d", len(errors)) // Show where data is preserved t.Log("") t.Log("Test artifacts preserved at:") t.Logf(" %s", testInfo.DB.TempDir) if logPath, err := testInfo.DB.GetLitestreamLog(); err == nil { t.Logf(" Log: %s", logPath) } t.Log("") t.Log("Test terminated early by user.") t.Log("================================================") // Mark test as failed (early termination) t.Fail() } // getErrorStats categorizes and counts errors func getErrorStats(db *TestDB) ErrorStats { errors, _ := db.CheckForErrors() stats := ErrorStats{ TotalCount: len(errors), ErrorsByType: make(map[string]int), } for _, errLine := range errors { switch { case strings.Contains(errLine, "connection refused"): stats.BenignCount++ stats.ErrorsByType["connection refused"]++ case strings.Contains(errLine, "context canceled"): stats.BenignCount++ stats.ErrorsByType["context canceled"]++ default: stats.CriticalCount++ if len(stats.RecentErrors) < 5 { stats.RecentErrors = append(stats.RecentErrors, errLine) } switch { case strings.Contains(errLine, "timeout"): stats.ErrorsByType["timeout"]++ case strings.Contains(errLine, "compaction failed"): stats.ErrorsByType["compaction failed"]++ default: stats.ErrorsByType["other"]++ } } } return stats } // printProgress displays progress bar with error status func printProgress(t *testing.T, elapsed, total time.Duration, errorStats ErrorStats) { t.Helper() if total <= 0 { total = time.Second } if elapsed < 0 { elapsed = 0 } pct := float64(elapsed) / float64(total) * 100 if pct > 100 { pct = 100 } else if pct < 0 { pct = 0 } remaining := total - elapsed if remaining < 0 { remaining = 0 } // Progress bar barWidth := 40 filled := 0 if total.Seconds() > 0 { ratio := elapsed.Seconds() / total.Seconds() if ratio < 0 { ratio = 0 } else if ratio > 1 { ratio = 1 } filled = int(float64(barWidth) * ratio) } if filled > barWidth { filled = barWidth } if filled < 0 { filled = 0 } bar := strings.Repeat("█", filled) + strings.Repeat("░", barWidth-filled) // Status indicator status := "✓" if errorStats.CriticalCount > 0 { status = "⚠" } t.Logf("%s Progress: [%s] %.0f%% | %v elapsed | %v remaining | Errors: %d/%d", status, bar, pct, elapsed.Round(time.Minute), remaining.Round(time.Minute), errorStats.CriticalCount, errorStats.TotalCount) } // printErrorDetails displays detailed error information func printErrorDetails(t *testing.T, errorStats ErrorStats) { t.Helper() t.Log("") t.Log("⚠ Error Status:") t.Logf(" Total: %d (%d critical, %d benign)", errorStats.TotalCount, errorStats.CriticalCount, errorStats.BenignCount) // Group critical errors by type if errorStats.CriticalCount > 0 { t.Log(" Critical errors:") for errorType, count := range errorStats.ErrorsByType { if count > 0 { t.Logf(" • %q (%d)", errorType, count) } } // Show recent errors if len(errorStats.RecentErrors) > 0 { t.Log("") t.Log(" Recent errors:") for _, errLine := range errorStats.RecentErrors { // Extract just the error message if idx := strings.Index(errLine, "error="); idx != -1 { msg := errLine[idx+7:] if len(msg) > 80 { msg = msg[:80] + "..." } t.Logf(" %s", msg) } } } } // Show benign errors if present if errorStats.BenignCount > 0 { t.Log("") t.Logf(" Benign errors: %d", errorStats.BenignCount) } } // shouldAbortTest checks if test should auto-abort due to critical issues func shouldAbortTest(errorStats ErrorStats, fileCount int, elapsed time.Duration) (bool, string) { // Abort if critical error threshold exceeded after extended runtime if elapsed > 10*time.Minute && errorStats.CriticalCount > 100 { return true, fmt.Sprintf("Critical error threshold exceeded (%d errors)", errorStats.CriticalCount) } // Abort if replication completely stopped (0 files after 10 minutes) if elapsed > 10*time.Minute && fileCount == 0 { return true, "Replication not working (0 files created after 10 minutes)" } // Abort if error rate is increasing rapidly (>1 error/minute) if errorStats.CriticalCount > 0 && elapsed > 30*time.Minute { minutes := elapsed.Minutes() if minutes > 0 { errorRate := float64(errorStats.CriticalCount) / minutes if errorRate > 2.0 { return true, fmt.Sprintf("Error rate too high (%.1f errors/minute)", errorRate) } } } return false, "" } // MonitorSoakTest monitors a soak test, calling metricsFunc every 60 seconds func MonitorSoakTest(t *testing.T, db *TestDB, ctx context.Context, info *TestInfo, refresh func(), logFunc func()) { t.Helper() if info == nil { info = &TestInfo{} } ticker := time.NewTicker(60 * time.Second) defer ticker.Stop() lastCritical := -1 lastTotal := -1 lastProgress := -1.0 for { select { case <-ctx.Done(): if refresh != nil { refresh() } if info != nil { // Show final progress snapshot errorStats := getErrorStats(db) if lastProgress < 0 || lastProgress < 100 || errorStats.CriticalCount != lastCritical || errorStats.TotalCount != lastTotal { printProgress(t, info.Duration, info.Duration, errorStats) t.Logf("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━") t.Logf("[%s] Status Report", time.Now().Format("15:04:05")) t.Logf("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━") if logFunc != nil { logFunc() } if errorStats.CriticalCount > 0 { printErrorDetails(t, errorStats) } } } t.Log("Monitoring stopped: test duration completed") return case <-ticker.C: if refresh != nil { refresh() } elapsed := time.Since(info.StartTime) if elapsed < 0 { elapsed = 0 } errorStats := getErrorStats(db) if shouldAbort, reason := shouldAbortTest(errorStats, info.FileCount, elapsed); shouldAbort { t.Logf("") t.Logf("⚠ AUTO-ABORTING TEST: %s", reason) if info.cancel != nil { info.cancel() } t.Fail() return } totalDuration := info.Duration if totalDuration <= 0 { totalDuration = time.Second } progress := elapsed.Seconds() / totalDuration.Seconds() * 100 if progress < 0 { progress = 0 } else if progress > 100 { progress = 100 } shouldLog := false if lastCritical == -1 && lastTotal == -1 { shouldLog = true } if !shouldLog && (errorStats.CriticalCount != lastCritical || errorStats.TotalCount != lastTotal) { shouldLog = true } if !shouldLog && (lastProgress < 0 || progress >= lastProgress+5 || progress >= 100) { shouldLog = true } if !shouldLog { continue } lastCritical = errorStats.CriticalCount lastTotal = errorStats.TotalCount lastProgress = progress printProgress(t, elapsed, info.Duration, errorStats) t.Logf("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━") t.Logf("[%s] Status Report", time.Now().Format("15:04:05")) t.Logf("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━") if logFunc != nil { logFunc() } if errorStats.CriticalCount > 0 { printErrorDetails(t, errorStats) } t.Log("") } } } // LogSoakMetrics logs basic soak test metrics func LogSoakMetrics(t *testing.T, db *TestDB, testName string) { t.Helper() // Database size if dbSize, err := db.GetDatabaseSize(); err == nil { t.Logf(" Database size: %.2f MB", float64(dbSize)/(1024*1024)) } // WAL size walPath := db.Path + "-wal" if info, err := os.Stat(walPath); err == nil { t.Logf(" WAL size: %.2f MB", float64(info.Size())/(1024*1024)) } // Row count if count, err := db.GetRowCount("load_test"); err == nil { t.Logf(" Rows: %d", count) } else if count, err := db.GetRowCount("test_table_0"); err == nil { t.Logf(" Rows: %d", count) } // Replica stats if fileCount, err := db.GetReplicaFileCount(); err == nil { t.Logf(" Replica LTX files: %d", fileCount) } // Error check if errors, err := db.CheckForErrors(); err == nil && len(errors) > 0 { t.Logf(" ⚠ Critical errors detected: %d", len(errors)) if len(errors) <= 2 { for _, errLine := range errors { t.Logf(" %s", errLine) } } } } // SoakTestAnalysis holds detailed soak test metrics type SoakTestAnalysis struct { CompactionsByLevel map[int]int TotalCompactions int SnapshotCount int CheckpointCount int TotalFilesCreated int FinalFileCount int MinTxID string MaxTxID string DatabaseRows int64 MinRowID int64 MaxRowID int64 DatabaseSizeMB float64 Duration time.Duration } // AnalyzeSoakTest analyzes test results from logs and database func AnalyzeSoakTest(t *testing.T, db *TestDB, duration time.Duration) *SoakTestAnalysis { t.Helper() analysis := &SoakTestAnalysis{ CompactionsByLevel: make(map[int]int), Duration: duration, } // Get database stats if count, err := db.GetRowCount("load_test"); err == nil { analysis.DatabaseRows = int64(count) } if dbSize, err := db.GetDatabaseSize(); err == nil { analysis.DatabaseSizeMB = float64(dbSize) / (1024 * 1024) } // Get row ID range sqlDB, err := sql.Open("sqlite3", db.Path) if err == nil { defer sqlDB.Close() sqlDB.QueryRow("SELECT MIN(id), MAX(id) FROM load_test").Scan(&analysis.MinRowID, &analysis.MaxRowID) } // Get final file count if count, err := db.GetReplicaFileCount(); err == nil { analysis.FinalFileCount = count } // Parse litestream log logPath, _ := db.GetLitestreamLog() if logPath != "" { parseLog(logPath, analysis) } return analysis } func parseLog(logPath string, analysis *SoakTestAnalysis) { file, err := os.Open(logPath) if err != nil { return } defer file.Close() scanner := bufio.NewScanner(file) var firstTxID, lastTxID string for scanner.Scan() { line := scanner.Text() if strings.Contains(line, "compaction complete") { analysis.TotalCompactions++ // Extract level if idx := strings.Index(line, "level="); idx != -1 { levelStr := line[idx+6:] if spaceIdx := strings.Index(levelStr, " "); spaceIdx != -1 { levelStr = levelStr[:spaceIdx] } if level, err := strconv.Atoi(levelStr); err == nil { analysis.CompactionsByLevel[level]++ } } // Extract transaction IDs if idx := strings.Index(line, "txid.min="); idx != -1 { txMin := line[idx+9 : idx+25] if firstTxID == "" { firstTxID = txMin } } if idx := strings.Index(line, "txid.max="); idx != -1 { txMax := line[idx+9 : idx+25] lastTxID = txMax } } if strings.Contains(line, "snapshot complete") { analysis.SnapshotCount++ } if strings.Contains(line, "checkpoint complete") { analysis.CheckpointCount++ } } analysis.MinTxID = firstTxID analysis.MaxTxID = lastTxID // Count all LTX files ever created (from txid range) if analysis.MaxTxID != "" { if maxID, err := strconv.ParseInt(analysis.MaxTxID, 16, 64); err == nil { analysis.TotalFilesCreated = int(maxID) } } } // PrintSoakTestAnalysis prints detailed analysis and plain English summary func PrintSoakTestAnalysis(t *testing.T, analysis *SoakTestAnalysis) { t.Helper() t.Log("") t.Log("================================================") t.Log("Detailed Test Metrics") t.Log("================================================") t.Log("") // Compaction breakdown t.Log("Compaction Activity:") t.Logf(" Total compactions: %d", analysis.TotalCompactions) levels := []int{1, 2, 3, 4, 5} for _, level := range levels { if count := analysis.CompactionsByLevel[level]; count > 0 { t.Logf(" Level %d: %d compactions", level, count) } } t.Log("") // File operations t.Log("File Operations:") t.Logf(" Total LTX files created: %d", analysis.TotalFilesCreated) if analysis.TotalFilesCreated > 0 { t.Logf(" Final file count: %d (%.1f%% reduction)", analysis.FinalFileCount, 100.0*float64(analysis.TotalFilesCreated-analysis.FinalFileCount)/float64(analysis.TotalFilesCreated)) } t.Logf(" Snapshots generated: %d", analysis.SnapshotCount) if analysis.CheckpointCount > 0 { t.Logf(" Checkpoints: %d", analysis.CheckpointCount) } t.Log("") // Database activity t.Log("Database Activity:") t.Logf(" Total rows: %d", analysis.DatabaseRows) t.Logf(" Row ID range: %d → %d", analysis.MinRowID, analysis.MaxRowID) gapCount := (analysis.MaxRowID - analysis.MinRowID + 1) - analysis.DatabaseRows if gapCount == 0 { t.Log(" Row continuity: ✓ No gaps (perfect)") } else { t.Logf(" Row continuity: %d gaps detected", gapCount) } t.Logf(" Final database size: %.2f MB", analysis.DatabaseSizeMB) if analysis.Duration.Seconds() > 0 { avgRate := float64(analysis.DatabaseRows) / analysis.Duration.Seconds() t.Logf(" Average write rate: %.1f rows/second", avgRate) } t.Log("") // Transaction range if analysis.MinTxID != "" && analysis.MaxTxID != "" { t.Log("Replication Range:") t.Logf(" First transaction: %s", analysis.MinTxID) t.Logf(" Last transaction: %s", analysis.MaxTxID) t.Log("") } // Plain English summary t.Log("================================================") t.Log("What This Test Validated") t.Log("================================================") t.Log("") t.Logf("✓ Long-term Stability") t.Logf(" Litestream ran flawlessly for %v under sustained load", analysis.Duration.Round(time.Minute)) t.Log("") t.Log("✓ Snapshot Generation") t.Logf(" %d snapshots created successfully", analysis.SnapshotCount) t.Log("") t.Log("✓ Compaction Efficiency") if analysis.TotalFilesCreated > 0 { reductionPct := 100.0 * float64(analysis.TotalFilesCreated-analysis.FinalFileCount) / float64(analysis.TotalFilesCreated) t.Logf(" Reduced %d files to %d (%.0f%% reduction through compaction)", analysis.TotalFilesCreated, analysis.FinalFileCount, reductionPct) } t.Log("") if analysis.DatabaseSizeMB > 1000 { t.Log("✓ Large Database Handling") t.Logf(" Successfully replicated %.1f GB database", analysis.DatabaseSizeMB/1024) t.Log("") } t.Log("✓ Restoration Capability") t.Log(" Full restore from replica completed successfully") t.Log("") t.Log("✓ Data Integrity") t.Log(" SQLite integrity check confirmed no corruption") if gapCount == 0 { t.Log(" All rows present with perfect continuity") } t.Log("") } ================================================ FILE: tests/integration/supabase-s3/docker-compose.yml ================================================ services: db: image: postgres:15 restart: unless-stopped environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres POSTGRES_DB: postgres volumes: - pg_data:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 5s timeout: 5s retries: 10 minio: image: minio/minio command: server /data --console-address ":9001" environment: MINIO_ROOT_USER: supa-storage MINIO_ROOT_PASSWORD: secret1234 volumes: - minio_data:/data healthcheck: test: ["CMD", "mc", "ready", "local"] interval: 5s timeout: 5s retries: 10 minio-createbucket: image: minio/mc depends_on: minio: condition: service_healthy entrypoint: > /bin/sh -c " mc alias set supabase http://minio:9000 supa-storage secret1234; mc mb --ignore-existing supabase/supa-storage-bucket; exit 0; " storage: image: supabase/storage-api:latest depends_on: db: condition: service_healthy minio-createbucket: condition: service_completed_successfully ports: - "5555:5000" environment: DATABASE_URL: postgres://postgres:postgres@db:5432/postgres DB_INSTALL_ROLES: "true" STORAGE_BACKEND: s3 STORAGE_S3_BUCKET: supa-storage-bucket STORAGE_S3_ENDPOINT: http://minio:9000 STORAGE_S3_FORCE_PATH_STYLE: "true" STORAGE_S3_REGION: us-east-1 AWS_ACCESS_KEY_ID: supa-storage AWS_SECRET_ACCESS_KEY: secret1234 AUTH_JWT_SECRET: super-secret-jwt-token-with-at-least-32-characters AUTH_JWT_ALGORITHM: HS256 S3_PROTOCOL_ACCESS_KEY_ID: supabase-s3-access-key S3_PROTOCOL_ACCESS_KEY_SECRET: supabase-s3-secret-key-that-is-long-enough ANON_KEY: eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJpc3MiOiAic3VwYWJhc2UiLCAicm9sZSI6ICJhbm9uIn0.y78PwHBHnrLnHZODMOjJfZZPlfKk0W3DucVCxvI6uF8 SERVICE_KEY: eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJpc3MiOiAic3VwYWJhc2UiLCAicm9sZSI6ICJzZXJ2aWNlX3JvbGUifQ.lRaC0LUy-3mILAj_17hVWOBnaft3QPpK-pqAs8h2MRI IS_MULTITENANT: "false" POSTGREST_URL: http://localhost:3000 REGION: us-east-1 TENANT_ID: stub healthcheck: test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://127.0.0.1:5000/status"] interval: 5s timeout: 5s retries: 15 start_period: 10s volumes: pg_data: minio_data: ================================================ FILE: tests/integration/supabase-s3/test.sh ================================================ #!/usr/bin/env bash set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)" ENDPOINT="http://localhost:5555/s3" STORAGE_API="http://localhost:5555" ACCESS_KEY="supabase-s3-access-key" SECRET_KEY="supabase-s3-secret-key-that-is-long-enough" SERVICE_KEY="eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJpc3MiOiAic3VwYWJhc2UiLCAicm9sZSI6ICJzZXJ2aWNlX3JvbGUifQ.lRaC0LUy-3mILAj_17hVWOBnaft3QPpK-pqAs8h2MRI" BUCKET="litestream-test" REGION="us-east-1" TMPDIR_BASE=$(mktemp -d) cleanup() { echo "" echo "=== Cleaning up ===" rm -rf "$TMPDIR_BASE" cd "$SCRIPT_DIR" docker compose down -v --remove-orphans 2>/dev/null || true } trap cleanup EXIT echo "================================================" echo "Supabase S3 Integration Test for Litestream" echo "================================================" echo "" # Step 1: Build litestream echo "[1/6] Building litestream..." cd "$PROJECT_ROOT" go build -o "$TMPDIR_BASE/litestream" ./cmd/litestream echo " OK: Binary built at $TMPDIR_BASE/litestream" echo "" # Step 2: Start Supabase stack echo "[2/6] Starting Supabase storage stack..." cd "$SCRIPT_DIR" docker compose up -d --wait --wait-timeout 90 echo " OK: Supabase storage running at $ENDPOINT" echo "" # Step 3: Create bucket via Supabase REST API echo "[3/6] Creating bucket '$BUCKET' via Supabase REST API..." BUCKET_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "$STORAGE_API/bucket" \ -H "Authorization: Bearer $SERVICE_KEY" \ -H "Content-Type: application/json" \ -d "{\"name\": \"$BUCKET\", \"public\": true}") BUCKET_HTTP_CODE=$(echo "$BUCKET_RESPONSE" | tail -1) BUCKET_BODY=$(echo "$BUCKET_RESPONSE" | sed '$d') if [ "$BUCKET_HTTP_CODE" = "200" ] || [ "$BUCKET_HTTP_CODE" = "201" ]; then echo " OK: Bucket created (HTTP $BUCKET_HTTP_CODE)" elif echo "$BUCKET_BODY" | grep -q "already exists"; then echo " OK: Bucket already exists" else echo " FAIL: Bucket creation failed (HTTP $BUCKET_HTTP_CODE): $BUCKET_BODY" exit 1 fi echo "" # Step 4: Create and populate test database echo "[4/6] Creating test SQLite database..." DB_PATH="$TMPDIR_BASE/test.db" sqlite3 "$DB_PATH" <<'SQL' PRAGMA journal_mode=WAL; CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT, created_at TEXT); INSERT INTO users VALUES (1, 'Alice', 'alice@example.com', datetime('now')); INSERT INTO users VALUES (2, 'Bob', 'bob@example.com', datetime('now')); INSERT INTO users VALUES (3, 'Charlie', 'charlie@example.com', datetime('now')); SQL ROW_COUNT=$(sqlite3 "$DB_PATH" "SELECT COUNT(*) FROM users;") echo " OK: Database created with $ROW_COUNT rows" echo "" # Step 5: Replicate to Supabase S3 echo "[5/6] Replicating database to Supabase S3..." REPLICA_URL="s3://${BUCKET}/testdb?endpoint=${ENDPOINT}®ion=${REGION}" echo " URL: $REPLICA_URL" echo " (Auto-detection should apply force-path-style=true and sign-payload=true)" AWS_ACCESS_KEY_ID="$ACCESS_KEY" \ AWS_SECRET_ACCESS_KEY="$SECRET_KEY" \ "$TMPDIR_BASE/litestream" replicate "$DB_PATH" "$REPLICA_URL" & LITESTREAM_PID=$! # Wait for initial sync, then write more data sleep 5 sqlite3 "$DB_PATH" <<'SQL' INSERT INTO users VALUES (4, 'Diana', 'diana@example.com', datetime('now')); INSERT INTO users VALUES (5, 'Eve', 'eve@example.com', datetime('now')); SQL echo " OK: Wrote 2 additional rows during replication" # Give time for WAL sync sleep 10 kill "$LITESTREAM_PID" 2>/dev/null || true wait "$LITESTREAM_PID" 2>/dev/null || true echo " OK: Replication completed" echo "" # Step 6: Restore and verify echo "[6/6] Restoring database from Supabase S3..." RESTORE_PATH="$TMPDIR_BASE/restored.db" AWS_ACCESS_KEY_ID="$ACCESS_KEY" \ AWS_SECRET_ACCESS_KEY="$SECRET_KEY" \ "$TMPDIR_BASE/litestream" restore -o "$RESTORE_PATH" "$REPLICA_URL" RESTORED_COUNT=$(sqlite3 "$RESTORE_PATH" "SELECT COUNT(*) FROM users;") echo " Restored row count: $RESTORED_COUNT" if [ "$RESTORED_COUNT" -ge 3 ]; then echo "" echo "================================================" echo "SUCCESS: Supabase S3 integration test passed!" echo " - Auto-detection: Working (no manual force-path-style/sign-payload needed)" echo " - Replication: Working" echo " - Restore: Working ($RESTORED_COUNT rows recovered)" echo "================================================" else echo "" echo "FAIL: Expected at least 3 rows, got $RESTORED_COUNT" exit 1 fi ================================================ FILE: tests/integration/upgrade_test.go ================================================ //go:build integration package integration import ( "database/sql" "fmt" "os" "os/exec" "path/filepath" "syscall" "testing" "time" _ "github.com/mattn/go-sqlite3" "github.com/benbjohnson/litestream" ) func TestUpgrade_V3ToV5(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } // Skip if v0.3 binary not provided. v3Bin := os.Getenv("LITESTREAM_V3_BIN") if v3Bin == "" { t.Skip("LITESTREAM_V3_BIN not set, skipping upgrade test") } // Verify v0.3 binary exists and is executable. if info, err := os.Stat(v3Bin); err != nil { t.Fatalf("v0.3 binary not found at %s: %v", v3Bin, err) } else if info.Mode()&0111 == 0 { t.Fatalf("v0.3 binary at %s is not executable", v3Bin) } // Verify current binary exists and is executable. v5Bin := getBinaryPath("litestream") if info, err := os.Stat(v5Bin); err != nil { t.Fatalf("current binary not found at %s: %v", v5Bin, err) } else if info.Mode()&0111 == 0 { t.Fatalf("current binary at %s is not executable", v5Bin) } // Set up temp directory with database and replica paths. tmpDir := t.TempDir() dbPath := filepath.Join(tmpDir, "upgrade.db") replicaPath := filepath.Join(tmpDir, "replica") replicaURL := fmt.Sprintf("file://%s", filepath.ToSlash(replicaPath)) restoredPath := filepath.Join(tmpDir, "restored.db") // Create WAL-mode database with test table. t.Log("Creating WAL-mode database with upgrade_test table") sqlDB, err := sql.Open("sqlite3", dbPath) if err != nil { t.Fatalf("open database: %v", err) } defer sqlDB.Close() if _, err := sqlDB.Exec("PRAGMA journal_mode=WAL"); err != nil { t.Fatalf("set WAL mode: %v", err) } if _, err := sqlDB.Exec("CREATE TABLE upgrade_test(id INTEGER PRIMARY KEY, phase TEXT, data BLOB)"); err != nil { t.Fatalf("create table: %v", err) } // ========================================================================= // Phase 1: v0.3.x replication // ========================================================================= t.Log("Phase 1: v0.3.x replication") // Insert 10 rows with phase='v3-initial'. t.Log(" Inserting 10 v3-initial rows") for i := 0; i < 10; i++ { if _, err := sqlDB.Exec("INSERT INTO upgrade_test(phase, data) VALUES(?, randomblob(100))", "v3-initial"); err != nil { t.Fatalf("insert v3-initial row %d: %v", i, err) } } // Start v0.3 replicate subprocess. t.Logf(" Starting v0.3 binary: %s replicate %s %s", v3Bin, dbPath, replicaURL) v3Cmd := exec.Command(v3Bin, "replicate", dbPath, replicaURL) v3LogFile, err := os.Create(filepath.Join(tmpDir, "v3-replicate.log")) if err != nil { t.Fatalf("create v3 log file: %v", err) } defer v3LogFile.Close() v3Cmd.Stdout = v3LogFile v3Cmd.Stderr = v3LogFile if err := v3Cmd.Start(); err != nil { t.Fatalf("start v0.3 replicate: %v", err) } t.Cleanup(func() { if v3Cmd.ProcessState == nil { v3Cmd.Process.Signal(syscall.SIGINT) v3Cmd.Wait() } }) // Wait for initial sync. t.Log(" Waiting 3s for initial sync") time.Sleep(3 * time.Second) // Insert 10 more rows with phase='v3-running' across 5 transactions (2 rows each). t.Log(" Inserting 10 v3-running rows across 5 transactions") for txn := 0; txn < 5; txn++ { tx, err := sqlDB.Begin() if err != nil { t.Fatalf("begin tx %d: %v", txn, err) } for j := 0; j < 2; j++ { if _, err := tx.Exec("INSERT INTO upgrade_test(phase, data) VALUES(?, randomblob(100))", "v3-running"); err != nil { tx.Rollback() t.Fatalf("insert v3-running tx %d row %d: %v", txn, j, err) } } if err := tx.Commit(); err != nil { t.Fatalf("commit tx %d: %v", txn, err) } } // Force checkpoint. t.Log(" Running PRAGMA wal_checkpoint(TRUNCATE)") if _, err := sqlDB.Exec("PRAGMA wal_checkpoint(TRUNCATE)"); err != nil { t.Fatalf("checkpoint: %v", err) } // Insert 5 more rows with phase='v3-post-checkpoint'. t.Log(" Inserting 5 v3-post-checkpoint rows") for i := 0; i < 5; i++ { if _, err := sqlDB.Exec("INSERT INTO upgrade_test(phase, data) VALUES(?, randomblob(100))", "v3-post-checkpoint"); err != nil { t.Fatalf("insert v3-post-checkpoint row %d: %v", i, err) } } // Wait for sync, then stop v0.3. t.Log(" Waiting 3s for sync") time.Sleep(3 * time.Second) t.Log(" Sending SIGINT to v0.3 process") if err := v3Cmd.Process.Signal(syscall.SIGINT); err != nil { t.Fatalf("signal v0.3 process: %v", err) } if err := v3Cmd.Wait(); err != nil { t.Logf(" v0.3 process exited with: %v (expected for SIGINT)", err) } // Verify generations/ directory exists (v0.3 layout). generationsDir := filepath.Join(replicaPath, "generations") if _, err := os.Stat(generationsDir); err != nil { t.Fatalf("v0.3 replica generations/ directory not found: %v", err) } t.Log(" Verified generations/ directory exists in replica") // ========================================================================= // Phase 2: v0.5.x replication // ========================================================================= t.Log("Phase 2: v0.5.x replication") // Start current binary replicate subprocess. t.Logf(" Starting v0.5 binary: %s replicate %s %s", v5Bin, dbPath, replicaURL) v5Cmd := exec.Command(v5Bin, "replicate", dbPath, replicaURL) v5LogFile, err := os.Create(filepath.Join(tmpDir, "v5-replicate.log")) if err != nil { t.Fatalf("create v5 log file: %v", err) } defer v5LogFile.Close() v5Cmd.Stdout = v5LogFile v5Cmd.Stderr = v5LogFile if err := v5Cmd.Start(); err != nil { t.Fatalf("start v0.5 replicate: %v", err) } t.Cleanup(func() { if v5Cmd.ProcessState == nil { v5Cmd.Process.Signal(syscall.SIGINT) v5Cmd.Wait() } }) // Wait up to 30s for ltx/9/ directory to contain at least one .ltx file (snapshot). t.Log(" Waiting up to 30s for snapshot in ltx/9/") snapshotDir := filepath.Join(replicaPath, "ltx", fmt.Sprintf("%d", litestream.SnapshotLevel)) deadline := time.Now().Add(30 * time.Second) snapshotFound := false for time.Now().Before(deadline) { matches, _ := filepath.Glob(filepath.Join(snapshotDir, "*.ltx")) if len(matches) > 0 { snapshotFound = true t.Logf(" Found %d snapshot file(s) in ltx/9/", len(matches)) break } time.Sleep(500 * time.Millisecond) } if !snapshotFound { t.Fatal("timeout waiting for v0.5.x snapshot in ltx/9/") } // Insert 10 rows with phase='v5-running' across 5 transactions. t.Log(" Inserting 10 v5-running rows across 5 transactions") for txn := 0; txn < 5; txn++ { tx, err := sqlDB.Begin() if err != nil { t.Fatalf("begin tx %d: %v", txn, err) } for j := 0; j < 2; j++ { if _, err := tx.Exec("INSERT INTO upgrade_test(phase, data) VALUES(?, randomblob(100))", "v5-running"); err != nil { tx.Rollback() t.Fatalf("insert v5-running tx %d row %d: %v", txn, j, err) } } if err := tx.Commit(); err != nil { t.Fatalf("commit tx %d: %v", txn, err) } } // Force checkpoint. t.Log(" Running PRAGMA wal_checkpoint(TRUNCATE)") if _, err := sqlDB.Exec("PRAGMA wal_checkpoint(TRUNCATE)"); err != nil { t.Fatalf("checkpoint: %v", err) } // Insert 5 more rows with phase='v5-post-checkpoint'. t.Log(" Inserting 5 v5-post-checkpoint rows") for i := 0; i < 5; i++ { if _, err := sqlDB.Exec("INSERT INTO upgrade_test(phase, data) VALUES(?, randomblob(100))", "v5-post-checkpoint"); err != nil { t.Fatalf("insert v5-post-checkpoint row %d: %v", i, err) } } // Wait for sync, then stop v0.5. t.Log(" Waiting 3s for sync") time.Sleep(3 * time.Second) t.Log(" Sending SIGINT to v0.5 process") if err := v5Cmd.Process.Signal(syscall.SIGINT); err != nil { t.Fatalf("signal v0.5 process: %v", err) } if err := v5Cmd.Wait(); err != nil { t.Logf(" v0.5 process exited with: %v (expected for SIGINT)", err) } // ========================================================================= // Phase 3: Restore // ========================================================================= t.Log("Phase 3: Restore using current binary") restoreCmd := exec.Command(v5Bin, "restore", "-o", restoredPath, replicaURL) restoreOutput, err := restoreCmd.CombinedOutput() if err != nil { t.Fatalf("restore failed: %v\nOutput: %s", err, restoreOutput) } t.Log(" Restore completed successfully") // ========================================================================= // Phase 4: Validate // ========================================================================= t.Log("Phase 4: Validate restored database") restoredDB, err := sql.Open("sqlite3", restoredPath) if err != nil { t.Fatalf("open restored database: %v", err) } defer restoredDB.Close() // Integrity check. var integrity string if err := restoredDB.QueryRow("PRAGMA integrity_check").Scan(&integrity); err != nil { t.Fatalf("integrity check query: %v", err) } if integrity != "ok" { t.Fatalf("integrity check failed: %s", integrity) } t.Log(" PRAGMA integrity_check: ok") // Count total rows. var totalCount int if err := restoredDB.QueryRow("SELECT COUNT(*) FROM upgrade_test").Scan(&totalCount); err != nil { t.Fatalf("count total rows: %v", err) } if totalCount != 40 { t.Fatalf("total row count: got %d, want 40", totalCount) } t.Logf(" Total rows: %d (expected 40)", totalCount) // Count rows per phase. The v3-initial rows were inserted before v0.3 // started, so their presence in the restore proves the v0.5.x snapshot // captured the full database state (not just changes made after v0.5.x started). phases := []struct { name string expected int }{ {"v3-initial", 10}, {"v3-running", 10}, {"v3-post-checkpoint", 5}, {"v5-running", 10}, {"v5-post-checkpoint", 5}, } for _, p := range phases { var count int if err := restoredDB.QueryRow("SELECT COUNT(*) FROM upgrade_test WHERE phase = ?", p.name).Scan(&count); err != nil { t.Fatalf("count phase %q: %v", p.name, err) } if count != p.expected { t.Errorf("phase %q: got %d rows, want %d", p.name, count, p.expected) } t.Logf(" Phase %q: %d rows", p.name, count) } } ================================================ FILE: v3.go ================================================ package litestream import ( "context" "fmt" "io" "path" "regexp" "strconv" "time" ) // PosV3 represents a position in a v0.3.x backup. type PosV3 struct { Generation string // 16-char hex string Index int // WAL index Offset int64 // Offset within WAL segment } // IsZero returns true if the position is the zero value. func (p PosV3) IsZero() bool { return p == (PosV3{}) } // String returns a string representation of the position. func (p PosV3) String() string { if p.IsZero() { return "" } return fmt.Sprintf("%s/%08x:%016x", p.Generation, p.Index, p.Offset) } // SnapshotInfoV3 contains metadata about a v0.3.x snapshot. type SnapshotInfoV3 struct { Generation string Index int Size int64 CreatedAt time.Time } // Pos returns the position of this snapshot. func (info SnapshotInfoV3) Pos() PosV3 { return PosV3{Generation: info.Generation, Index: info.Index, Offset: 0} } // WALSegmentInfoV3 contains metadata about a v0.3.x WAL segment. type WALSegmentInfoV3 struct { Generation string Index int Offset int64 Size int64 CreatedAt time.Time } // Pos returns the position of this WAL segment. func (info WALSegmentInfoV3) Pos() PosV3 { return PosV3{Generation: info.Generation, Index: info.Index, Offset: info.Offset} } // v0.3.x path constants. const ( GenerationsDirV3 = "generations" SnapshotsDirV3 = "snapshots" WALDirV3 = "wal" ) // GenerationsPathV3 returns the path to the generations directory. func GenerationsPathV3(root string) string { return path.Join(root, GenerationsDirV3) } // GenerationPathV3 returns the path to a specific generation. func GenerationPathV3(root, generation string) string { return path.Join(root, GenerationsDirV3, generation) } // SnapshotsPathV3 returns the path to snapshots within a generation. func SnapshotsPathV3(root, generation string) string { return path.Join(root, GenerationsDirV3, generation, SnapshotsDirV3) } // WALPathV3 returns the path to WAL segments within a generation. func WALPathV3(root, generation string) string { return path.Join(root, GenerationsDirV3, generation, WALDirV3) } // SnapshotPathV3 returns the full path to a v0.3.x snapshot file. func SnapshotPathV3(root, generation string, index int) string { return path.Join(SnapshotsPathV3(root, generation), FormatSnapshotFilenameV3(index)) } // WALSegmentPathV3 returns the full path to a v0.3.x WAL segment file. func WALSegmentPathV3(root, generation string, index int, offset int64) string { return path.Join(WALPathV3(root, generation), FormatWALSegmentFilenameV3(index, offset)) } // FormatSnapshotFilenameV3 returns the filename for a v0.3.x snapshot. // Format: {index:08x}.snapshot.lz4 func FormatSnapshotFilenameV3(index int) string { return fmt.Sprintf("%08x.snapshot.lz4", index) } // FormatWALSegmentFilenameV3 returns the filename for a v0.3.x WAL segment. // Format: {index:08x}_{offset:08x}.wal.lz4 func FormatWALSegmentFilenameV3(index int, offset int64) string { return fmt.Sprintf("%08x_%08x.wal.lz4", index, offset) } var ( snapshotRegexV3 = regexp.MustCompile(`^([0-9a-f]{8})\.snapshot\.lz4$`) walSegmentRegexV3 = regexp.MustCompile(`^([0-9a-f]{8})_([0-9a-f]{8})\.wal\.lz4$`) generationRegexV3 = regexp.MustCompile(`^[0-9a-f]{16}$`) ) // ParseSnapshotFilenameV3 parses a v0.3.x snapshot filename and returns the index. // Returns an error if the filename does not match the expected format. func ParseSnapshotFilenameV3(filename string) (index int, err error) { m := snapshotRegexV3.FindStringSubmatch(filename) if m == nil { return 0, fmt.Errorf("invalid v0.3.x snapshot filename: %q", filename) } idx, _ := strconv.ParseInt(m[1], 16, 64) return int(idx), nil } // ParseWALSegmentFilenameV3 parses a v0.3.x WAL segment filename. // Returns the WAL index and byte offset, or an error if the filename is invalid. func ParseWALSegmentFilenameV3(filename string) (index int, offset int64, err error) { m := walSegmentRegexV3.FindStringSubmatch(filename) if m == nil { return 0, 0, fmt.Errorf("invalid v0.3.x WAL segment filename: %q", filename) } idx, _ := strconv.ParseInt(m[1], 16, 64) off, _ := strconv.ParseInt(m[2], 16, 64) return int(idx), off, nil } // IsGenerationIDV3 returns true if s is a valid v0.3.x generation ID (16 hex chars). func IsGenerationIDV3(s string) bool { return generationRegexV3.MatchString(s) } // ReplicaClientV3 reads v0.3.x backup data. // ReplicaClient implementations that support v0.3.x restore should implement this interface. type ReplicaClientV3 interface { // GenerationsV3 returns a list of generation IDs in the replica. // Returns an empty slice if no v0.3.x backups exist. // Generation IDs are sorted in ascending order. GenerationsV3(ctx context.Context) ([]string, error) // SnapshotsV3 returns snapshots for a generation, sorted by index. // Returns an empty slice if no snapshots exist. SnapshotsV3(ctx context.Context, generation string) ([]SnapshotInfoV3, error) // WALSegmentsV3 returns WAL segments for a generation, sorted by index then offset. // Returns an empty slice if no WAL segments exist. WALSegmentsV3(ctx context.Context, generation string) ([]WALSegmentInfoV3, error) // OpenSnapshotV3 opens a v0.3.x snapshot for reading. // The returned reader provides LZ4-decompressed data. OpenSnapshotV3(ctx context.Context, generation string, index int) (io.ReadCloser, error) // OpenWALSegmentV3 opens a v0.3.x WAL segment for reading. // The returned reader provides LZ4-decompressed data. OpenWALSegmentV3(ctx context.Context, generation string, index int, offset int64) (io.ReadCloser, error) } ================================================ FILE: v3_test.go ================================================ package litestream_test import ( "testing" "github.com/benbjohnson/litestream" ) func TestPosV3_IsZero(t *testing.T) { if !(litestream.PosV3{}).IsZero() { t.Error("zero value should return true") } if (litestream.PosV3{Generation: "abc"}).IsZero() { t.Error("non-zero value should return false") } } func TestPosV3_String(t *testing.T) { tests := []struct { pos litestream.PosV3 want string }{ {litestream.PosV3{}, ""}, {litestream.PosV3{Generation: "0123456789abcdef", Index: 1, Offset: 4096}, "0123456789abcdef/00000001:0000000000001000"}, } for _, tt := range tests { if got := tt.pos.String(); got != tt.want { t.Errorf("PosV3%+v.String() = %q, want %q", tt.pos, got, tt.want) } } } func TestSnapshotInfoV3_Pos(t *testing.T) { info := litestream.SnapshotInfoV3{Generation: "abc", Index: 5} pos := info.Pos() if pos.Generation != "abc" || pos.Index != 5 || pos.Offset != 0 { t.Errorf("unexpected pos: %+v", pos) } } func TestWALSegmentInfoV3_Pos(t *testing.T) { info := litestream.WALSegmentInfoV3{Generation: "abc", Index: 5, Offset: 100} pos := info.Pos() if pos.Generation != "abc" || pos.Index != 5 || pos.Offset != 100 { t.Errorf("unexpected pos: %+v", pos) } } func TestFormatSnapshotFilenameV3(t *testing.T) { tests := []struct { index int want string }{ {0, "00000000.snapshot.lz4"}, {1, "00000001.snapshot.lz4"}, {255, "000000ff.snapshot.lz4"}, {0x12345678, "12345678.snapshot.lz4"}, } for _, tt := range tests { if got := litestream.FormatSnapshotFilenameV3(tt.index); got != tt.want { t.Errorf("FormatSnapshotFilenameV3(%d) = %q, want %q", tt.index, got, tt.want) } } } func TestParseSnapshotFilenameV3(t *testing.T) { t.Run("Valid", func(t *testing.T) { tests := []struct { filename string want int }{ {"00000000.snapshot.lz4", 0}, {"00000001.snapshot.lz4", 1}, {"000000ff.snapshot.lz4", 255}, {"12345678.snapshot.lz4", 0x12345678}, } for _, tt := range tests { index, err := litestream.ParseSnapshotFilenameV3(tt.filename) if err != nil { t.Errorf("ParseSnapshotFilenameV3(%q) error: %v", tt.filename, err) continue } if index != tt.want { t.Errorf("ParseSnapshotFilenameV3(%q) = %d, want %d", tt.filename, index, tt.want) } } }) t.Run("Invalid", func(t *testing.T) { invalids := []string{ "", "invalid.txt", "00000001.snapshot", // missing .lz4 "0000001.snapshot.lz4", // 7 chars, not 8 "000000001.snapshot.lz4", // 9 chars, not 8 "0000000g.snapshot.lz4", // invalid hex "00000001.SNAPSHOT.lz4", // uppercase "00000001.snapshot.lz4.bak", } for _, filename := range invalids { _, err := litestream.ParseSnapshotFilenameV3(filename) if err == nil { t.Errorf("ParseSnapshotFilenameV3(%q) expected error", filename) } } }) } func TestFormatWALSegmentFilenameV3(t *testing.T) { tests := []struct { index int offset int64 want string }{ {0, 0, "00000000_00000000.wal.lz4"}, {1, 4096, "00000001_00001000.wal.lz4"}, {255, 0x12345678, "000000ff_12345678.wal.lz4"}, } for _, tt := range tests { if got := litestream.FormatWALSegmentFilenameV3(tt.index, tt.offset); got != tt.want { t.Errorf("FormatWALSegmentFilenameV3(%d, %d) = %q, want %q", tt.index, tt.offset, got, tt.want) } } } func TestParseWALSegmentFilenameV3(t *testing.T) { t.Run("Valid", func(t *testing.T) { tests := []struct { filename string wantIndex int wantOffset int64 }{ {"00000000_00000000.wal.lz4", 0, 0}, {"00000001_00001000.wal.lz4", 1, 4096}, {"000000ff_12345678.wal.lz4", 255, 0x12345678}, } for _, tt := range tests { index, offset, err := litestream.ParseWALSegmentFilenameV3(tt.filename) if err != nil { t.Errorf("ParseWALSegmentFilenameV3(%q) error: %v", tt.filename, err) continue } if index != tt.wantIndex || offset != tt.wantOffset { t.Errorf("ParseWALSegmentFilenameV3(%q) = (%d, %d), want (%d, %d)", tt.filename, index, offset, tt.wantIndex, tt.wantOffset) } } }) t.Run("Invalid", func(t *testing.T) { invalids := []string{ "", "invalid.wal", "00000001.wal.lz4", // missing offset "00000001_00001000.wal", // missing .lz4 "0000001_00001000.wal.lz4", // 7 chars index "00000001_0001000.wal.lz4", // 7 chars offset } for _, filename := range invalids { _, _, err := litestream.ParseWALSegmentFilenameV3(filename) if err == nil { t.Errorf("ParseWALSegmentFilenameV3(%q) expected error", filename) } } }) } func TestIsGenerationIDV3(t *testing.T) { tests := []struct { s string want bool }{ {"0123456789abcdef", true}, {"abcdef0123456789", true}, {"aaaaaaaaaaaaaaaa", true}, {"0000000000000000", true}, {"ffffffffffffffff", true}, {"0123456789ABCDEF", false}, // uppercase not valid {"0123456789abcde", false}, // 15 chars, too short {"0123456789abcdeff", false}, // 17 chars, too long {"0123456789abcdeg", false}, // invalid hex char {"", false}, {"generations", false}, } for _, tt := range tests { if got := litestream.IsGenerationIDV3(tt.s); got != tt.want { t.Errorf("IsGenerationIDV3(%q) = %v, want %v", tt.s, got, tt.want) } } } func TestPathsV3(t *testing.T) { root := "/data/replica" gen := "0123456789abcdef" t.Run("GenerationsPath", func(t *testing.T) { got := litestream.GenerationsPathV3(root) want := "/data/replica/generations" if got != want { t.Errorf("GenerationsPathV3(%q) = %q, want %q", root, got, want) } }) t.Run("GenerationPath", func(t *testing.T) { got := litestream.GenerationPathV3(root, gen) want := "/data/replica/generations/0123456789abcdef" if got != want { t.Errorf("GenerationPathV3(%q, %q) = %q, want %q", root, gen, got, want) } }) t.Run("SnapshotsPath", func(t *testing.T) { got := litestream.SnapshotsPathV3(root, gen) want := "/data/replica/generations/0123456789abcdef/snapshots" if got != want { t.Errorf("SnapshotsPathV3(%q, %q) = %q, want %q", root, gen, got, want) } }) t.Run("WALPath", func(t *testing.T) { got := litestream.WALPathV3(root, gen) want := "/data/replica/generations/0123456789abcdef/wal" if got != want { t.Errorf("WALPathV3(%q, %q) = %q, want %q", root, gen, got, want) } }) t.Run("SnapshotPath", func(t *testing.T) { got := litestream.SnapshotPathV3(root, gen, 1) want := "/data/replica/generations/0123456789abcdef/snapshots/00000001.snapshot.lz4" if got != want { t.Errorf("SnapshotPathV3(%q, %q, 1) = %q, want %q", root, gen, got, want) } }) t.Run("WALSegmentPath", func(t *testing.T) { got := litestream.WALSegmentPathV3(root, gen, 1, 4096) want := "/data/replica/generations/0123456789abcdef/wal/00000001_00001000.wal.lz4" if got != want { t.Errorf("WALSegmentPathV3(%q, %q, 1, 4096) = %q, want %q", root, gen, got, want) } }) } // TestFormatParseRoundtrip verifies that Format and Parse are inverses. func TestFormatParseRoundtrip(t *testing.T) { t.Run("Snapshot", func(t *testing.T) { for _, index := range []int{0, 1, 255, 0x12345678} { filename := litestream.FormatSnapshotFilenameV3(index) got, err := litestream.ParseSnapshotFilenameV3(filename) if err != nil { t.Errorf("roundtrip failed for index %d: %v", index, err) continue } if got != index { t.Errorf("roundtrip: FormatSnapshotFilenameV3(%d) -> %q -> ParseSnapshotFilenameV3 -> %d", index, filename, got) } } }) t.Run("WALSegment", func(t *testing.T) { cases := []struct { index int offset int64 }{ {0, 0}, {1, 4096}, {255, 0x12345678}, } for _, tc := range cases { filename := litestream.FormatWALSegmentFilenameV3(tc.index, tc.offset) gotIndex, gotOffset, err := litestream.ParseWALSegmentFilenameV3(filename) if err != nil { t.Errorf("roundtrip failed for (%d, %d): %v", tc.index, tc.offset, err) continue } if gotIndex != tc.index || gotOffset != tc.offset { t.Errorf("roundtrip: FormatWALSegmentFilenameV3(%d, %d) -> %q -> (%d, %d)", tc.index, tc.offset, filename, gotIndex, gotOffset) } } }) } ================================================ FILE: vfs.go ================================================ //go:build vfs // +build vfs package litestream import ( "context" "crypto/rand" "errors" "fmt" "hash/fnv" "io" "log/slog" "os" "path/filepath" "slices" "strconv" "strings" "sync" "sync/atomic" "time" _ "unsafe" lru "github.com/hashicorp/golang-lru/v2" "github.com/markusmobius/go-dateparser" "github.com/psanford/sqlite3vfs" "github.com/superfly/ltx" ) const ( DefaultPollInterval = 1 * time.Second DefaultCacheSize = 10 * 1024 * 1024 // 10MB DefaultPageSize = 4096 // SQLite default page size pageFetchRetryAttempts = 6 pageFetchRetryDelay = 15 * time.Millisecond ) // ErrConflict is returned when the remote replica has newer transactions than expected. var ErrConflict = errors.New("remote has newer transactions than expected") var ( //go:linkname sqlite3vfsFileMap github.com/psanford/sqlite3vfs.fileMap sqlite3vfsFileMap map[uint64]sqlite3vfs.File //go:linkname sqlite3vfsFileMux github.com/psanford/sqlite3vfs.fileMux sqlite3vfsFileMux sync.Mutex vfsConnectionMap sync.Map // map[uintptr]uint64 ) // VFS implements the SQLite VFS interface for Litestream. // It is intended to be used for read replicas that read directly from S3. // When WriteEnabled is true, also supports writes with periodic sync. type VFS struct { client ReplicaClient logger *slog.Logger // PollInterval is the interval at which to poll the replica client for new // LTX files. The index will be fetched for the new files automatically. PollInterval time.Duration // CacheSize is the maximum size of the page cache in bytes. CacheSize int // WriteEnabled activates write support for the VFS. WriteEnabled bool // WriteSyncInterval is how often to sync dirty pages to remote storage. // If zero, defaults to DefaultSyncInterval (1 second). WriteSyncInterval time.Duration // WriteBufferPath is the path for local write buffer persistence. // If empty, uses a temp file. WriteBufferPath string // HydrationEnabled activates background hydration of the database to a local file. // When enabled, the VFS will restore the database in the background and serve // reads from the local file once complete, eliminating remote fetch latency. HydrationEnabled bool // HydrationPath is the file path for local hydration file. // If empty and HydrationEnabled is true, a temp file will be used. HydrationPath string // CompactionEnabled activates background compaction for the VFS. // Requires WriteEnabled to be true. CompactionEnabled bool // CompactionLevels defines the compaction intervals for each level. // If nil, uses default compaction levels. CompactionLevels CompactionLevels // SnapshotInterval is how often to create full database snapshots. // Set to 0 to disable automatic snapshots. SnapshotInterval time.Duration // SnapshotRetention is how long to keep old snapshots. // Set to 0 to keep all snapshots. SnapshotRetention time.Duration // L0Retention is how long to keep L0 files after compaction into L1. // Set to 0 to delete immediately after compaction. L0Retention time.Duration writeMu sync.Mutex writeFile *VFSFile // current RESERVED lock holder (nil if none) lastSyncedTXID ltx.TXID // highest TXID synced by any local connection writeSeq uint64 // atomic counter for unique buffer paths tempDirOnce sync.Once tempDir string tempDirErr error tempFiles sync.Map // canonical name -> absolute path tempNames sync.Map // canonical name -> struct{}{} } func NewVFS(client ReplicaClient, logger *slog.Logger) *VFS { return &VFS{ client: client, logger: logger.With("vfs", "true"), PollInterval: DefaultPollInterval, CacheSize: DefaultCacheSize, } } func (vfs *VFS) Open(name string, flags sqlite3vfs.OpenFlag) (sqlite3vfs.File, sqlite3vfs.OpenFlag, error) { slog.Debug("opening file", "name", name, "flags", flags) switch { case flags&sqlite3vfs.OpenMainDB != 0: return vfs.openMainDB(name, flags) case vfs.requiresTempFile(flags): return vfs.openTempFile(name, flags) default: return nil, flags, sqlite3vfs.CantOpenError } } func (vfs *VFS) openMainDB(name string, flags sqlite3vfs.OpenFlag) (sqlite3vfs.File, sqlite3vfs.OpenFlag, error) { f := NewVFSFile(vfs.client, name, vfs.logger.With("name", name)) f.PollInterval = vfs.PollInterval f.CacheSize = vfs.CacheSize f.vfs = vfs // Store reference to parent VFS for config access // Initialize write support if enabled if vfs.WriteEnabled { f.writeEnabled = true f.dirty = make(map[uint32]int64) f.syncInterval = vfs.WriteSyncInterval if f.syncInterval == 0 { f.syncInterval = DefaultSyncInterval } writeSeq := atomic.AddUint64(&vfs.writeSeq, 1) if vfs.WriteBufferPath != "" { if writeSeq == 1 { f.bufferPath = vfs.WriteBufferPath } else { f.bufferPath = vfs.WriteBufferPath + "." + strconv.FormatUint(writeSeq, 10) } } else { dir, err := vfs.ensureTempDir() if err != nil { return nil, 0, fmt.Errorf("create temp dir for write buffer: %w", err) } f.bufferPath = filepath.Join(dir, "write-buffer-"+strconv.FormatUint(writeSeq, 10)) } // Initialize compaction if enabled if vfs.CompactionEnabled { f.compactor = NewCompactor(vfs.client, f.logger) // VFS has no local files, so leave LocalFileOpener/LocalFileDeleter nil } } // Initialize hydration support if enabled if vfs.HydrationEnabled { if vfs.HydrationPath != "" { f.hydrationPath = vfs.HydrationPath f.hydrationPersistent = true } else { // Use a temp file if no path specified dir, err := vfs.ensureTempDir() if err != nil { return nil, 0, fmt.Errorf("create temp dir for hydration: %w", err) } f.hydrationPath = filepath.Join(dir, "hydration.db") } } if err := f.Open(); err != nil { return nil, 0, err } if vfs.WriteEnabled { vfs.writeMu.Lock() if f.expectedTXID > vfs.lastSyncedTXID { vfs.lastSyncedTXID = f.expectedTXID } vfs.writeMu.Unlock() } // When SQLite requests read-write access, always report ReadWrite in the // output flags so that cold enable via PRAGMA litestream_write_enabled // works. SQLite permanently marks databases as read-only based on the // output flags from xOpen (pager.c:readOnly, btree.c:BTS_READ_ONLY), // which would prevent write transactions even after enabling writes at // runtime. Read-only enforcement happens at the VFS layer (WriteAt, // Truncate, Lock) when writeEnabled is false. // // If the caller explicitly requested read-only, we respect that intent. if flags&sqlite3vfs.OpenReadOnly == 0 { flags &^= sqlite3vfs.OpenReadOnly flags |= sqlite3vfs.OpenReadWrite } return f, flags, nil } func (vfs *VFS) Delete(name string, dirSync bool) error { slog.Debug("deleting file", "name", name, "dirSync", dirSync) err := vfs.deleteTempFile(name) if err == nil { return nil } if errors.Is(err, os.ErrNotExist) { return nil } if errors.Is(err, errTempFileNotFound) { return fmt.Errorf("cannot delete vfs file") } return err } func (vfs *VFS) Access(name string, flag sqlite3vfs.AccessFlag) (bool, error) { slog.Debug("accessing file", "name", name, "flag", flag) if strings.HasSuffix(name, "-wal") { return vfs.accessWAL(name, flag) } if vfs.isTempFileName(name) { return vfs.accessTempFile(name, flag) } return false, nil } func (vfs *VFS) accessWAL(name string, flag sqlite3vfs.AccessFlag) (bool, error) { return false, nil } func (vfs *VFS) FullPathname(name string) string { slog.Debug("full pathname", "name", name) return name } func (vfs *VFS) requiresTempFile(flags sqlite3vfs.OpenFlag) bool { const tempMask = sqlite3vfs.OpenTempDB | sqlite3vfs.OpenTempJournal | sqlite3vfs.OpenSubJournal | sqlite3vfs.OpenSuperJournal | sqlite3vfs.OpenTransientDB | sqlite3vfs.OpenMainJournal if flags&tempMask != 0 { return true } return flags&sqlite3vfs.OpenDeleteOnClose != 0 } func (vfs *VFS) ensureTempDir() (string, error) { vfs.tempDirOnce.Do(func() { dir, err := os.MkdirTemp("", "litestream-vfs-*") if err != nil { vfs.tempDirErr = fmt.Errorf("create temp dir: %w", err) return } vfs.tempDir = dir }) return vfs.tempDir, vfs.tempDirErr } func (vfs *VFS) canonicalTempName(name string) string { if name == "" { return "" } name = filepath.Clean(name) if name == "." || name == string(filepath.Separator) { return "" } return name } func tempFilenameFromCanonical(canonical string) (string, error) { base := filepath.Base(canonical) if base == "." || base == string(filepath.Separator) { return "", fmt.Errorf("invalid temp file name: %q", canonical) } h := fnv.New64a() if _, err := h.Write([]byte(canonical)); err != nil { return "", fmt.Errorf("hash temp name: %w", err) } return fmt.Sprintf("%s-%016x", base, h.Sum64()), nil } func (vfs *VFS) openTempFile(name string, flags sqlite3vfs.OpenFlag) (sqlite3vfs.File, sqlite3vfs.OpenFlag, error) { dir, err := vfs.ensureTempDir() if err != nil { return nil, flags, err } deleteOnClose := flags&sqlite3vfs.OpenDeleteOnClose != 0 || name == "" var f *os.File var onClose func() if name == "" { f, err = os.CreateTemp(dir, "temp-*") if err != nil { return nil, flags, sqlite3vfs.CantOpenError } } else { canonical := vfs.canonicalTempName(name) if canonical == "" { return nil, flags, sqlite3vfs.CantOpenError } fname, err := tempFilenameFromCanonical(canonical) if err != nil { return nil, flags, sqlite3vfs.CantOpenError } path := filepath.Join(dir, fname) flag := openFlagToOSFlag(flags) if flag == 0 { flag = os.O_RDWR } f, err = os.OpenFile(path, flag|os.O_CREATE, 0o600) if err != nil { return nil, flags, sqlite3vfs.CantOpenError } onClose = vfs.trackTempFile(canonical, path) } return newLocalTempFile(f, deleteOnClose, onClose), flags, nil } func (vfs *VFS) deleteTempFile(name string) error { path, ok := vfs.loadTempFilePath(name) if !ok { if vfs.wasTempFileName(name) { vfs.unregisterTempFile(name) return os.ErrNotExist } return errTempFileNotFound } if err := os.Remove(path); err != nil { if !os.IsNotExist(err) { return err } } vfs.unregisterTempFile(name) return nil } func (vfs *VFS) isTempFileName(name string) bool { _, ok := vfs.loadTempFilePath(name) return ok } func (vfs *VFS) wasTempFileName(name string) bool { canonical := vfs.canonicalTempName(name) if canonical == "" { return false } _, ok := vfs.tempNames.Load(canonical) return ok } func (vfs *VFS) unregisterTempFile(name string) { canonical := vfs.canonicalTempName(name) if canonical == "" { return } vfs.tempFiles.Delete(canonical) } func (vfs *VFS) accessTempFile(name string, flag sqlite3vfs.AccessFlag) (bool, error) { path, ok := vfs.loadTempFilePath(name) if !ok { return false, nil } _, err := os.Stat(path) if err != nil { if os.IsNotExist(err) { return false, nil } return false, err } return true, nil } func (vfs *VFS) trackTempFile(canonical, path string) func() { if canonical == "" { return func() {} } vfs.tempFiles.Store(canonical, path) vfs.tempNames.Store(canonical, struct{}{}) return func() { vfs.tempFiles.Delete(canonical) } } func (vfs *VFS) loadTempFilePath(name string) (string, bool) { canonical := vfs.canonicalTempName(name) if canonical == "" { return "", false } if path, ok := vfs.tempFiles.Load(canonical); ok { return path.(string), true } return "", false } func openFlagToOSFlag(flag sqlite3vfs.OpenFlag) int { var v int if flag&sqlite3vfs.OpenReadWrite != 0 { v |= os.O_RDWR } else if flag&sqlite3vfs.OpenReadOnly != 0 { v |= os.O_RDONLY } if flag&sqlite3vfs.OpenCreate != 0 { v |= os.O_CREATE } if flag&sqlite3vfs.OpenExclusive != 0 { v |= os.O_EXCL } return v } var errTempFileNotFound = fmt.Errorf("temp file not tracked") // localTempFile fulfills sqlite3vfs.File solely for SQLite temp & transient files. // These files stay on the local filesystem and optionally delete themselves // when SQLite closes them (DeleteOnClose flag). type localTempFile struct { f *os.File deleteOnClose bool lockType atomic.Int32 onClose func() } func newLocalTempFile(f *os.File, deleteOnClose bool, onClose func()) *localTempFile { return &localTempFile{f: f, deleteOnClose: deleteOnClose, onClose: onClose} } func (tf *localTempFile) Close() error { err := tf.f.Close() if tf.deleteOnClose { if removeErr := os.Remove(tf.f.Name()); removeErr != nil && !os.IsNotExist(removeErr) && err == nil { err = removeErr } } if tf.onClose != nil { tf.onClose() } return err } func (tf *localTempFile) ReadAt(p []byte, off int64) (n int, err error) { return tf.f.ReadAt(p, off) } func (tf *localTempFile) WriteAt(b []byte, off int64) (n int, err error) { return tf.f.WriteAt(b, off) } func (tf *localTempFile) Truncate(size int64) error { return tf.f.Truncate(size) } func (tf *localTempFile) Sync(flag sqlite3vfs.SyncType) error { return tf.f.Sync() } func (tf *localTempFile) FileSize() (int64, error) { info, err := tf.f.Stat() if err != nil { return 0, err } return info.Size(), nil } func (tf *localTempFile) Lock(elock sqlite3vfs.LockType) error { if elock == sqlite3vfs.LockNone { return nil } tf.lockType.Store(int32(elock)) return nil } func (tf *localTempFile) Unlock(elock sqlite3vfs.LockType) error { tf.lockType.Store(int32(elock)) return nil } func (tf *localTempFile) CheckReservedLock() (bool, error) { return sqlite3vfs.LockType(tf.lockType.Load()) >= sqlite3vfs.LockReserved, nil } func (tf *localTempFile) SectorSize() int64 { return 0 } func (tf *localTempFile) DeviceCharacteristics() sqlite3vfs.DeviceCharacteristic { return 0 } // VFSFile implements the SQLite VFS file interface. type VFSFile struct { mu sync.Mutex client ReplicaClient name string pos ltx.Pos // Last TXID read from level 0 or 1 maxTXID1 ltx.TXID // Last TXID read from level 1 index map[uint32]ltx.PageIndexElem pending map[uint32]ltx.PageIndexElem pendingReplace bool cache *lru.Cache[uint32, []byte] // LRU cache for page data targetTime *time.Time // Target view time; nil means latest latestLTXTime time.Time // Timestamp of most recent LTX file lastPollSuccess time.Time // Time of last successful poll lockType sqlite3vfs.LockType // Current lock state pageSize uint32 commit uint32 // Write support fields (only used when writeEnabled is true) writeEnabled bool // Whether write support is enabled dirty map[uint32]int64 // Dirty pages: pgno -> offset in buffer file pendingTXID ltx.TXID // Next TXID to use for sync expectedTXID ltx.TXID // Expected remote TXID (for conflict detection) bufferFile *os.File // Temp file for durability bufferPath string // Path to buffer file bufferNextOff int64 // Next write offset in buffer file syncTicker *time.Ticker // Ticker for periodic sync syncInterval time.Duration // Interval for periodic sync syncStop chan struct{} // Signal to stop sync loop inTransaction bool // True during active write transaction disabling bool // True when write disable is in progress cond *sync.Cond // Signals transaction state changes hydrator *Hydrator // Background hydration (nil if disabled) hydrationPath string // Path for hydration file (set during Open) hydrationPersistent bool // True when using user-specified persistent path wg sync.WaitGroup ctx context.Context cancel context.CancelFunc logger *slog.Logger PollInterval time.Duration CacheSize int // Compaction support (only used when VFS.CompactionEnabled is true) vfs *VFS // Reference back to parent VFS for config compactor *Compactor // Shared compaction logic compactionWg sync.WaitGroup compactionCtx context.Context compactionCancel context.CancelFunc } // Hydrator handles background hydration of the database to a local file. type Hydrator struct { path string // Full path to hydration file persistent bool // True when file should survive across restarts file *os.File // Local database file complete atomic.Bool // True when restore completes txid ltx.TXID // TXID the hydrated file is at mu sync.Mutex // Protects hydration file writes err error // Stores fatal hydration error compactor *ltx.Compactor // Tracks compaction progress during restore pageSize uint32 // Page size of the database client ReplicaClient logger *slog.Logger } // NewHydrator creates a new Hydrator instance. func NewHydrator(path string, persistent bool, pageSize uint32, client ReplicaClient, logger *slog.Logger) *Hydrator { return &Hydrator{ path: path, persistent: persistent, pageSize: pageSize, client: client, logger: logger, } } // Init opens or creates the hydration file. func (h *Hydrator) Init() error { if err := os.MkdirAll(filepath.Dir(h.path), 0755); err != nil { return fmt.Errorf("create hydration directory: %w", err) } if h.persistent { if txid, err := h.loadMeta(); err == nil { if _, statErr := os.Stat(h.path); statErr == nil { file, err := os.OpenFile(h.path, os.O_RDWR, 0600) if err != nil { return fmt.Errorf("open persistent hydration file: %w", err) } h.file = file h.txid = txid return nil } } if err := os.Remove(h.metaPath()); err != nil && !os.IsNotExist(err) { return fmt.Errorf("remove stale hydration meta: %w", err) } } file, err := os.OpenFile(h.path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { return fmt.Errorf("create hydration file: %w", err) } h.file = file return nil } // Complete returns true if hydration has completed. func (h *Hydrator) Complete() bool { return h.complete.Load() } // SetComplete marks hydration as complete. func (h *Hydrator) SetComplete() { h.complete.Store(true) } // Disable temporarily disables hydrated reads (used during time travel). func (h *Hydrator) Disable() { h.complete.Store(false) } // TXID returns the current hydration TXID. func (h *Hydrator) TXID() ltx.TXID { h.mu.Lock() defer h.mu.Unlock() return h.txid } // SetTXID sets the hydration TXID. func (h *Hydrator) SetTXID(txid ltx.TXID) { h.mu.Lock() defer h.mu.Unlock() h.txid = txid } // Err returns any fatal hydration error. func (h *Hydrator) Err() error { h.mu.Lock() defer h.mu.Unlock() return h.err } // SetErr sets a fatal hydration error. func (h *Hydrator) SetErr(err error) { h.mu.Lock() defer h.mu.Unlock() h.err = err } // Status returns the current compaction progress during restore. func (h *Hydrator) Status() ltx.CompactorStatus { if h.compactor == nil { return ltx.CompactorStatus{} } return h.compactor.Status() } // Restore restores the database from LTX files to the hydration file. func (h *Hydrator) Restore(ctx context.Context, infos []*ltx.FileInfo) error { // Open all LTX files as readers rdrs := make([]io.Reader, 0, len(infos)) defer func() { for _, rd := range rdrs { if closer, ok := rd.(io.Closer); ok { _ = closer.Close() } } }() for _, info := range infos { h.logger.Debug("opening ltx file for hydration", "level", info.Level, "min", info.MinTXID, "max", info.MaxTXID) rc, err := h.client.OpenLTXFile(ctx, info.Level, info.MinTXID, info.MaxTXID, 0, 0) if err != nil { return fmt.Errorf("open ltx file: %w", err) } rdrs = append(rdrs, rc) } if len(rdrs) == 0 { return fmt.Errorf("no ltx files for hydration") } // Compact and decode using io.Pipe pattern pr, pw := io.Pipe() c, err := ltx.NewCompactor(pw, rdrs) if err != nil { return fmt.Errorf("new ltx compactor: %w", err) } c.HeaderFlags = ltx.HeaderFlagNoChecksum h.compactor = c go func() { _ = pw.CloseWithError(c.Compact(ctx)) }() h.mu.Lock() defer h.mu.Unlock() dec := ltx.NewDecoder(pr) if err := dec.DecodeDatabaseTo(h.file); err != nil { return fmt.Errorf("decode database: %w", err) } h.txid = infos[len(infos)-1].MaxTXID return nil } // CatchUp applies updates from LTX files between fromTXID and toTXID. func (h *Hydrator) CatchUp(ctx context.Context, fromTXID, toTXID ltx.TXID) error { h.logger.Debug("catching up hydration", "from", fromTXID, "to", toTXID) // Fetch LTX files from fromTXID+1 to toTXID itr, err := h.client.LTXFiles(ctx, 0, fromTXID+1, false) if err != nil { return fmt.Errorf("list ltx files for catch-up: %w", err) } defer itr.Close() for itr.Next() { info := itr.Item() if info.MaxTXID > toTXID { break } if err := h.ApplyLTX(ctx, info); err != nil { return fmt.Errorf("apply ltx to hydrated file: %w", err) } h.mu.Lock() h.txid = info.MaxTXID h.mu.Unlock() } return nil } // ApplyLTX fetches an entire LTX file and applies its pages to the hydration file. func (h *Hydrator) ApplyLTX(ctx context.Context, info *ltx.FileInfo) error { h.logger.Debug("applying ltx to hydration file", "level", info.Level, "min", info.MinTXID, "max", info.MaxTXID) // Fetch entire LTX file rc, err := h.client.OpenLTXFile(ctx, info.Level, info.MinTXID, info.MaxTXID, 0, 0) if err != nil { return fmt.Errorf("open ltx file: %w", err) } defer rc.Close() dec := ltx.NewDecoder(rc) if err := dec.DecodeHeader(); err != nil { return fmt.Errorf("decode header: %w", err) } h.mu.Lock() defer h.mu.Unlock() // Apply each page to the hydration file for { var phdr ltx.PageHeader data := make([]byte, h.pageSize) if err := dec.DecodePage(&phdr, data); err == io.EOF { break } else if err != nil { return fmt.Errorf("decode page: %w", err) } off := int64(phdr.Pgno-1) * int64(h.pageSize) if _, err := h.file.WriteAt(data, off); err != nil { return fmt.Errorf("write page %d: %w", phdr.Pgno, err) } } return nil } // ReadAt reads data from the hydrated local file. func (h *Hydrator) ReadAt(p []byte, off int64) (int, error) { h.mu.Lock() n, err := h.file.ReadAt(p, off) h.mu.Unlock() if err != nil && err != io.EOF { return n, fmt.Errorf("read hydrated file: %w", err) } // Update the first page to pretend like we are in journal mode if off == 0 && len(p) >= 28 { p[18], p[19] = 0x01, 0x01 _, _ = rand.Read(p[24:28]) } return n, nil } // ApplyUpdates fetches updated pages and writes them to the hydration file. func (h *Hydrator) ApplyUpdates(ctx context.Context, updates map[uint32]ltx.PageIndexElem) error { h.mu.Lock() defer h.mu.Unlock() for pgno, elem := range updates { _, data, err := FetchPage(ctx, h.client, elem.Level, elem.MinTXID, elem.MaxTXID, elem.Offset, elem.Size) if err != nil { return fmt.Errorf("fetch updated page %d: %w", pgno, err) } off := int64(pgno-1) * int64(h.pageSize) if _, err := h.file.WriteAt(data, off); err != nil { return fmt.Errorf("write updated page %d: %w", pgno, err) } } return nil } // WritePage writes a single page to the hydration file. func (h *Hydrator) WritePage(pgno uint32, data []byte) error { h.mu.Lock() defer h.mu.Unlock() off := int64(pgno-1) * int64(h.pageSize) if _, err := h.file.WriteAt(data, off); err != nil { return fmt.Errorf("write page %d to hydrated file: %w", pgno, err) } return nil } // Truncate truncates the hydration file to the specified size. func (h *Hydrator) Truncate(size int64) error { h.mu.Lock() defer h.mu.Unlock() return h.file.Truncate(size) } // Close closes the hydration file. For persistent hydrators, the file and a // companion .meta file are preserved so hydration can resume on the next open. func (h *Hydrator) Close() error { if h.file == nil { return nil } if h.persistent && h.txid > 0 { if err := h.file.Sync(); err != nil { h.logger.Warn("failed to sync hydration file", "error", err) } if err := h.saveMeta(); err != nil { h.logger.Warn("failed to save hydration meta", "error", err) } return h.file.Close() } if err := h.file.Close(); err != nil { return err } if err := os.Remove(h.path); err != nil && !os.IsNotExist(err) { return err } if err := os.Remove(h.metaPath()); err != nil && !os.IsNotExist(err) { return err } return nil } func (h *Hydrator) metaPath() string { return h.path + ".meta" } func (h *Hydrator) loadMeta() (ltx.TXID, error) { data, err := os.ReadFile(h.metaPath()) if err != nil { return 0, err } v, err := strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64) if err != nil { return 0, fmt.Errorf("parse hydration meta: %w", err) } return ltx.TXID(v), nil } func (h *Hydrator) saveMeta() error { h.mu.Lock() txid := h.txid h.mu.Unlock() dir := filepath.Dir(h.metaPath()) tmp, err := os.CreateTemp(dir, ".hydration-meta-*") if err != nil { return fmt.Errorf("create temp meta file: %w", err) } tmpPath := tmp.Name() if _, err := fmt.Fprintf(tmp, "%d\n", txid); err != nil { if closeErr := tmp.Close(); closeErr != nil { h.logger.Warn("failed to close temp meta file during cleanup", "error", closeErr) } if removeErr := os.Remove(tmpPath); removeErr != nil { h.logger.Warn("failed to remove temp meta file during cleanup", "error", removeErr) } return fmt.Errorf("write hydration meta: %w", err) } if err := tmp.Sync(); err != nil { if closeErr := tmp.Close(); closeErr != nil { h.logger.Warn("failed to close temp meta file during cleanup", "error", closeErr) } if removeErr := os.Remove(tmpPath); removeErr != nil { h.logger.Warn("failed to remove temp meta file during cleanup", "error", removeErr) } return fmt.Errorf("sync temp meta file: %w", err) } if err := tmp.Close(); err != nil { if removeErr := os.Remove(tmpPath); removeErr != nil { h.logger.Warn("failed to remove temp meta file during cleanup", "error", removeErr) } return fmt.Errorf("close temp meta file: %w", err) } if err := os.Rename(tmpPath, h.metaPath()); err != nil { if removeErr := os.Remove(tmpPath); removeErr != nil { h.logger.Warn("failed to remove temp meta file during cleanup", "error", removeErr) } return fmt.Errorf("rename hydration meta: %w", err) } if err := syncDir(filepath.Dir(h.metaPath())); err != nil { return fmt.Errorf("sync hydration meta directory: %w", err) } return nil } func syncDir(path string) error { dir, err := os.Open(path) if err != nil { return err } defer dir.Close() return dir.Sync() } func NewVFSFile(client ReplicaClient, name string, logger *slog.Logger) *VFSFile { f := &VFSFile{ client: client, name: name, index: make(map[uint32]ltx.PageIndexElem), pending: make(map[uint32]ltx.PageIndexElem), logger: logger, PollInterval: DefaultPollInterval, CacheSize: DefaultCacheSize, } f.ctx, f.cancel = context.WithCancel(context.Background()) f.cond = sync.NewCond(&f.mu) return f } // Pos returns the current position of the file. func (f *VFSFile) Pos() ltx.Pos { f.mu.Lock() defer f.mu.Unlock() return f.pos } // MaxTXID1 returns the last TXID read from level 1. func (f *VFSFile) MaxTXID1() ltx.TXID { f.mu.Lock() defer f.mu.Unlock() return f.maxTXID1 } // LockType returns the current lock type of the file. func (f *VFSFile) LockType() sqlite3vfs.LockType { f.mu.Lock() defer f.mu.Unlock() return f.lockType } // TargetTime returns the current target time for the VFS file (nil for latest). func (f *VFSFile) TargetTime() *time.Time { f.mu.Lock() defer f.mu.Unlock() if f.targetTime == nil { return nil } t := *f.targetTime return &t } // LatestLTXTime returns the timestamp of the most recent LTX file. func (f *VFSFile) LatestLTXTime() time.Time { f.mu.Lock() defer f.mu.Unlock() return f.latestLTXTime } // LastPollSuccess returns the time of the last successful poll. func (f *VFSFile) LastPollSuccess() time.Time { f.mu.Lock() defer f.mu.Unlock() return f.lastPollSuccess } func (f *VFSFile) hasTargetTime() bool { f.mu.Lock() defer f.mu.Unlock() return f.targetTime != nil } func (f *VFSFile) Open() error { f.logger.Debug("opening file") // Try to get restore plan. For write-enabled VFS, we can create a new database // if no LTX files exist yet. infos, err := f.waitForRestorePlan() if err != nil { // If write mode is enabled and no files exist, we can create a new database if f.writeEnabled && errors.Is(err, ErrTxNotAvailable) { f.logger.Info("no existing database found, creating new database") return f.openNewDatabase() } return err } pageSize, err := detectPageSizeFromInfos(f.ctx, f.client, infos) if err != nil { f.logger.Error("cannot detect page size", "error", err) return fmt.Errorf("detect page size: %w", err) } f.pageSize = pageSize // Initialize page cache. Convert byte size to number of pages. cacheEntries := f.CacheSize / int(pageSize) if cacheEntries < 1 { cacheEntries = 1 } cache, err := lru.New[uint32, []byte](cacheEntries) if err != nil { return fmt.Errorf("create page cache: %w", err) } f.cache = cache // Determine the current position based off the latest LTX file. var pos ltx.Pos if len(infos) > 0 { pos = ltx.Pos{TXID: infos[len(infos)-1].MaxTXID} } f.pos = pos // Initialize write support TXID tracking if f.writeEnabled { f.expectedTXID = pos.TXID f.pendingTXID = pos.TXID + 1 f.logger.Debug("write support enabled", "expectedTXID", f.expectedTXID, "pendingTXID", f.pendingTXID) // Initialize write buffer file for durability (discards any existing buffer) if err := f.initWriteBuffer(); err != nil { return fmt.Errorf("initialize write buffer: %w", err) } } // Build the page index so we can lookup individual pages. if err := f.buildIndex(f.ctx, infos); err != nil { f.logger.Error("cannot build index", "error", err) return fmt.Errorf("cannot build index: %w", err) } // Start background hydration if enabled if f.hydrationPath != "" { if err := f.initHydration(infos); err != nil { f.logger.Warn("hydration initialization failed, continuing without hydration", "error", err) f.hydrationPath = "" } } // Continuously monitor the replica client for new LTX files. f.wg.Add(1) go func() { defer f.wg.Done(); f.monitorReplicaClient(f.ctx) }() // Start periodic sync goroutine if write support is enabled if f.writeEnabled && f.syncInterval > 0 { f.syncTicker = time.NewTicker(f.syncInterval) f.syncStop = make(chan struct{}) stopCh := f.syncStop tickerCh := f.syncTicker.C f.wg.Add(1) go func() { defer f.wg.Done(); f.syncLoop(stopCh, tickerCh) }() } // Start compaction monitors if enabled if f.compactor != nil && f.vfs != nil { f.startCompactionMonitors() } return nil } // openNewDatabase initializes the VFSFile for a brand new database with no existing data. // This is called when write mode is enabled and no LTX files exist yet. func (f *VFSFile) openNewDatabase() error { f.logger.Debug("initializing new database") // Use default page size for new databases f.pageSize = DefaultPageSize // Initialize page cache cacheEntries := f.CacheSize / int(f.pageSize) if cacheEntries < 1 { cacheEntries = 1 } cache, err := lru.New[uint32, []byte](cacheEntries) if err != nil { return fmt.Errorf("create page cache: %w", err) } f.cache = cache // Initialize empty index - no pages exist yet f.index = make(map[uint32]ltx.PageIndexElem) f.pending = make(map[uint32]ltx.PageIndexElem) f.pos = ltx.Pos{TXID: 0} f.commit = 0 // Initialize write support for new database f.expectedTXID = 0 f.pendingTXID = 1 f.logger.Debug("write support enabled for new database", "expectedTXID", f.expectedTXID, "pendingTXID", f.pendingTXID) // Initialize write buffer file for durability if err := f.initWriteBuffer(); err != nil { f.logger.Warn("failed to initialize write buffer", "error", err) } // Start monitoring for new LTX files (in case another writer creates the database) f.wg.Add(1) go func() { defer f.wg.Done(); f.monitorReplicaClient(f.ctx) }() // Start periodic sync goroutine if f.syncInterval > 0 { f.syncTicker = time.NewTicker(f.syncInterval) f.syncStop = make(chan struct{}) stopCh := f.syncStop tickerCh := f.syncTicker.C f.wg.Add(1) go func() { defer f.wg.Done(); f.syncLoop(stopCh, tickerCh) }() } // Start compaction monitors if enabled if f.compactor != nil && f.vfs != nil { f.startCompactionMonitors() } return nil } // SetTargetTime rebuilds the page index to view the database at a specific time. func (f *VFSFile) SetTargetTime(ctx context.Context, timestamp time.Time) error { if timestamp.IsZero() { return fmt.Errorf("target time required") } infos, err := CalcRestorePlan(ctx, f.client, 0, timestamp, f.logger) if err != nil { return fmt.Errorf("cannot calc restore plan: %w", err) } else if len(infos) == 0 { return fmt.Errorf("no backup files available") } // Disable hydrated reads during time travel - hydrated file is at latest state if f.hydrator != nil && f.hydrator.Complete() { f.hydrator.Disable() f.logger.Debug("hydration disabled for time travel", "target", timestamp) } return f.rebuildIndex(ctx, infos, ×tamp) } // ResetTime rebuilds the page index to the latest available state. func (f *VFSFile) ResetTime(ctx context.Context) error { infos, err := CalcRestorePlan(ctx, f.client, 0, time.Time{}, f.logger) if err != nil { return fmt.Errorf("cannot calc restore plan: %w", err) } else if len(infos) == 0 { return fmt.Errorf("no backup files available") } return f.rebuildIndex(ctx, infos, nil) } // rebuildIndex constructs a fresh page index and swaps it into the VFSFile. func (f *VFSFile) rebuildIndex(ctx context.Context, infos []*ltx.FileInfo, target *time.Time) error { index, err := f.buildIndexMap(ctx, infos) if err != nil { return err } var pos ltx.Pos if len(infos) > 0 { pos = ltx.Pos{TXID: infos[len(infos)-1].MaxTXID} } maxTXID1 := maxLevelTXID(infos, 1) // Seed maxTXID1 from pos when there are no L1 files if maxTXID1 == 0 { maxTXID1 = pos.TXID } f.mu.Lock() defer f.mu.Unlock() f.index = index f.pending = make(map[uint32]ltx.PageIndexElem) f.pendingReplace = false f.pos = pos f.maxTXID1 = maxTXID1 if len(infos) > 0 { f.latestLTXTime = infos[len(infos)-1].CreatedAt } if f.cache != nil { f.cache.Purge() } if target == nil { f.targetTime = nil } else { t := *target f.targetTime = &t } return nil } func maxLevelTXID(infos []*ltx.FileInfo, level int) ltx.TXID { var maxTXID ltx.TXID for _, info := range infos { if info.Level == level && info.MaxTXID > maxTXID { maxTXID = info.MaxTXID } } return maxTXID } // buildIndexMap constructs a lookup of pgno to LTX file offsets. func (f *VFSFile) buildIndexMap(ctx context.Context, infos []*ltx.FileInfo) (map[uint32]ltx.PageIndexElem, error) { index := make(map[uint32]ltx.PageIndexElem) var commit uint32 for _, info := range infos { f.logger.Debug("opening page index", "level", info.Level, "min", info.MinTXID, "max", info.MaxTXID) // Read page index. idx, err := FetchPageIndex(ctx, f.client, info) if err != nil { return nil, fmt.Errorf("fetch page index: %w", err) } // Replace pages in overall index with new pages. for k, v := range idx { f.logger.Debug("adding page index", "page", k, "elem", v) index[k] = v } hdr, err := FetchLTXHeader(ctx, f.client, info) if err != nil { return nil, fmt.Errorf("fetch header: %w", err) } commit = hdr.Commit } f.mu.Lock() f.commit = commit f.mu.Unlock() return index, nil } // buildIndex constructs a lookup of pgno to LTX file offsets (legacy wrapper). func (f *VFSFile) buildIndex(ctx context.Context, infos []*ltx.FileInfo) error { return f.rebuildIndex(ctx, infos, nil) } // initHydration starts the background hydration process. func (f *VFSFile) initHydration(infos []*ltx.FileInfo) error { f.hydrator = NewHydrator(f.hydrationPath, f.hydrationPersistent, f.pageSize, f.client, f.logger) if err := f.hydrator.Init(); err != nil { return err } // Start background restore f.wg.Add(1) go f.runHydration(infos) return nil } // runHydration performs the background hydration process. func (f *VFSFile) runHydration(infos []*ltx.FileInfo) { defer f.wg.Done() hydrationTXID := f.hydrator.TXID() f.mu.Lock() currentTXID := f.pos.TXID f.mu.Unlock() if hydrationTXID > 0 && currentTXID >= hydrationTXID { f.logger.Debug("resuming hydration from persistent file", "txid", hydrationTXID.String()) } else { if hydrationTXID > 0 { f.logger.Warn("remote TXID regressed, discarding persistent hydration", "hydration_txid", hydrationTXID.String(), "current_txid", currentTXID.String()) if err := f.hydrator.Truncate(0); err != nil { f.hydrator.SetErr(err) f.logger.Error("hydration truncate failed", "error", err) return } } if err := f.hydrator.Restore(f.ctx, infos); err != nil { f.hydrator.SetErr(err) f.logger.Error("hydration failed", "error", err) return } hydrationTXID = f.hydrator.TXID() } if currentTXID > hydrationTXID { if err := f.hydrator.CatchUp(f.ctx, hydrationTXID, currentTXID); err != nil { f.hydrator.SetErr(err) f.logger.Error("hydration catch-up failed", "error", err) return } } f.hydrator.SetComplete() // Clear cache since we'll now read from hydration file f.cache.Purge() f.logger.Debug("hydration complete", "path", f.hydrationPath, "txid", f.hydrator.TXID().String()) } // applySyncedPagesToHydratedFile writes synced dirty pages to the hydrated file. // Must be called with f.mu held. func (f *VFSFile) applySyncedPagesToHydratedFile() error { for pgno, bufferOff := range f.dirty { data := make([]byte, f.pageSize) if _, err := f.bufferFile.ReadAt(data, bufferOff); err != nil { return fmt.Errorf("read dirty page %d from buffer: %w", pgno, err) } if err := f.hydrator.WritePage(pgno, data); err != nil { return err } } f.hydrator.SetTXID(f.expectedTXID) return nil } func (f *VFSFile) Close() error { f.logger.Debug("closing file") // Stop sync loop and ticker if running (need mutex for syncStop) f.mu.Lock() if f.syncStop != nil { close(f.syncStop) f.syncStop = nil } if f.syncTicker != nil { f.syncTicker.Stop() } f.mu.Unlock() // Stop compaction monitors if running if f.compactionCancel != nil { f.compactionCancel() f.compactionWg.Wait() } // Final sync of dirty pages before closing f.mu.Lock() if f.writeEnabled && len(f.dirty) > 0 { if err := f.syncToRemoteWithLock(); err != nil { f.logger.Error("failed to sync on close", "error", err) } } f.mu.Unlock() f.cancel() f.wg.Wait() // Close and remove buffer file if open if f.bufferFile != nil { f.bufferFile.Close() os.Remove(f.bufferPath) } // Close and remove hydration file if f.hydrator != nil { if err := f.hydrator.Close(); err != nil { f.logger.Warn("failed to close hydration file", "error", err) } } if f.writeEnabled && f.vfs != nil { f.vfs.writeMu.Lock() if f.vfs.writeFile == f { f.vfs.writeFile = nil } f.vfs.writeMu.Unlock() } return nil } func (f *VFSFile) ReadAt(p []byte, off int64) (n int, err error) { f.logger.Debug("reading at", "off", off, "len", len(p)) pageSize, err := f.pageSizeBytes() if err != nil { return 0, err } pgno := uint32(off/int64(pageSize)) + 1 pageOffset := int(off % int64(pageSize)) // Check dirty pages first (takes priority over cache and remote) f.mu.Lock() if f.writeEnabled { if bufferOff, ok := f.dirty[pgno]; ok { // Read page from buffer file data := make([]byte, pageSize) if _, err := f.bufferFile.ReadAt(data, bufferOff); err != nil { f.mu.Unlock() return 0, fmt.Errorf("read dirty page from buffer: %w", err) } n = copy(p, data[pageOffset:]) f.mu.Unlock() f.logger.Debug("dirty page hit", "page", pgno, "n", n) // Update the first page to pretend like we are in journal mode. if off == 0 && len(p) >= 28 { p[18], p[19] = 0x01, 0x01 _, _ = rand.Read(p[24:28]) } return n, nil } } f.mu.Unlock() // If hydration complete, read from local file if f.hydrator != nil && f.hydrator.Complete() { return f.hydrator.ReadAt(p, off) } // Check cache (cache is thread-safe) if data, ok := f.cache.Get(pgno); ok { n = copy(p, data[pageOffset:]) f.logger.Debug("cache hit", "page", pgno, "n", n) // Update the first page to pretend like we are in journal mode. if off == 0 { p[18], p[19] = 0x01, 0x01 _, _ = rand.Read(p[24:28]) } return n, nil } // Get page index element f.mu.Lock() elem, ok := f.index[pgno] writeEnabled := f.writeEnabled // capture while holding lock to avoid data race f.mu.Unlock() if !ok { // For write-enabled VFS with a new database (no existing pages), // return zeros to indicate empty page. SQLite will initialize the database. if writeEnabled { f.logger.Debug("page not found, returning zeros for new database", "page", pgno) for i := range p { p[i] = 0 } return len(p), nil } f.logger.Error("page not found", "page", pgno) return 0, fmt.Errorf("page not found: %d", pgno) } var data []byte var lastErr error ctx := f.ctx for attempt := 0; attempt < pageFetchRetryAttempts; attempt++ { _, data, lastErr = FetchPage(ctx, f.client, elem.Level, elem.MinTXID, elem.MaxTXID, elem.Offset, elem.Size) if lastErr == nil { break } if !isRetryablePageError(lastErr) { f.logger.Error("cannot fetch page", "page", pgno, "attempt", attempt+1, "error", lastErr) return 0, fmt.Errorf("fetch page: %w", lastErr) } if attempt == pageFetchRetryAttempts-1 { f.logger.Error("cannot fetch page after retries", "page", pgno, "attempts", pageFetchRetryAttempts, "error", lastErr) return 0, sqlite3vfs.BusyError } delay := pageFetchRetryDelay * time.Duration(attempt+1) f.logger.Warn("transient page fetch error, retrying", "page", pgno, "attempt", attempt+1, "delay", delay, "error", lastErr) timer := time.NewTimer(delay) select { case <-timer.C: case <-f.ctx.Done(): timer.Stop() return 0, fmt.Errorf("fetch page: %w", lastErr) } timer.Stop() } // Add to cache (cache is thread-safe) f.cache.Add(pgno, data) n = copy(p, data[pageOffset:]) f.logger.Debug("data read from storage", "page", pgno, "n", n, "data", len(data)) // Update the first page to pretend like we are in journal mode. if off == 0 { p[18], p[19] = 0x01, 0x01 _, _ = rand.Read(p[24:28]) } return n, nil } func (f *VFSFile) WriteAt(b []byte, off int64) (n int, err error) { f.logger.Debug("write at", "off", off, "len", len(b)) pageSize, err := f.pageSizeBytes() if err != nil { return 0, err } // Calculate page number and offset within page pgno := uint32(off/int64(pageSize)) + 1 pageOffset := int(off % int64(pageSize)) // Skip lock page if pgno == ltx.LockPgno(pageSize) { return 0, fmt.Errorf("cannot write to lock page") } f.mu.Lock() defer f.mu.Unlock() // If write support is not enabled, return read-only error if !f.writeEnabled { return 0, sqlite3vfs.ReadOnlyError } // Get page data - either from buffer file (if dirty) or from cache/remote page := make([]byte, pageSize) if bufferOff, ok := f.dirty[pgno]; ok { // Page is already dirty - read from buffer file if _, err := f.bufferFile.ReadAt(page, bufferOff); err != nil { return 0, fmt.Errorf("read dirty page from buffer: %w", err) } } else { // Page is not dirty - read from cache/remote if err := f.readPageForWrite(pgno, page); err != nil { // If page doesn't exist, use zero-filled page f.logger.Debug("page not found, using empty page", "pgno", pgno) } } // Apply write to page n = copy(page[pageOffset:], b) // Update commit count if this extends the database if pgno > f.commit { f.commit = pgno } // Write to buffer for durability (this updates f.dirty with the offset) if err := f.writeToBuffer(pgno, page); err != nil { f.logger.Error("failed to write to buffer", "error", err) return 0, fmt.Errorf("write to buffer: %w", err) } f.logger.Debug("wrote to dirty page", "pgno", pgno, "offset", pageOffset, "len", n, "commit", f.commit) return n, nil } // readPageForWrite reads a page into buf for modification. // Must be called with f.mu held. func (f *VFSFile) readPageForWrite(pgno uint32, buf []byte) error { pageSize := uint32(len(buf)) // Check cache first (cache is thread-safe, but we hold the lock anyway) if data, ok := f.cache.Get(pgno); ok { copy(buf, data) return nil } // Get page index element elem, ok := f.index[pgno] if !ok { return fmt.Errorf("page not found: %d", pgno) } // Fetch from remote _, data, err := FetchPage(f.ctx, f.client, elem.Level, elem.MinTXID, elem.MaxTXID, elem.Offset, elem.Size) if err != nil { return err } if uint32(len(data)) != pageSize { return fmt.Errorf("page size mismatch: got %d, expected %d", len(data), pageSize) } copy(buf, data) f.cache.Add(pgno, data) return nil } func (f *VFSFile) Truncate(size int64) error { f.logger.Debug("truncating file", "size", size) pageSize, err := f.pageSizeBytes() if err != nil { return err } newCommit := uint32(size / int64(pageSize)) f.mu.Lock() defer f.mu.Unlock() // If write support is not enabled, return read-only error if !f.writeEnabled { return sqlite3vfs.ReadOnlyError } // Remove dirty pages beyond new size for pgno := range f.dirty { if pgno > newCommit { delete(f.dirty, pgno) } } f.commit = newCommit f.logger.Debug("truncated", "newCommit", newCommit) // Truncate hydrated file if hydration is complete if f.hydrator != nil && f.hydrator.Complete() { if err := f.hydrator.Truncate(size); err != nil { f.logger.Error("failed to truncate hydration file", "error", err) // Don't fail the operation - continue with degraded performance } } return nil } func (f *VFSFile) Sync(flag sqlite3vfs.SyncType) error { f.logger.Debug("syncing file", "flag", flag) f.mu.Lock() defer f.mu.Unlock() // If write support is not enabled, no-op if !f.writeEnabled { return nil } // Skip sync if no dirty pages if len(f.dirty) == 0 { return nil } // Skip sync during active transaction if f.inTransaction { f.logger.Debug("skipping sync during transaction") return nil } return f.syncToRemoteWithLock() } // SetWriteEnabled enables or disables write support at runtime. // This is equivalent to SetWriteEnabledWithTimeout(enabled, 0) which waits indefinitely. func (f *VFSFile) SetWriteEnabled(enabled bool) error { return f.SetWriteEnabledWithTimeout(enabled, 0) } // SetWriteEnabledWithTimeout enables or disables write support at runtime with an optional timeout. // // When disabling (enabled=false): // - If called during an active transaction, waits for completion // - If timeout > 0, returns error after timeout if transaction doesn't complete // - If timeout == 0, waits indefinitely (or until context cancellation) // - Syncs all dirty pages to replica before returning // - Returns error if sync fails (writes remain enabled) // - Stops the sync ticker if running // // When enabling (enabled=true): // - Initializes buffer file if not already present (cold enable) // - Starts sync ticker using DefaultSyncInterval if not configured // - Starts sync ticker if syncInterval > 0 and not already running func (f *VFSFile) SetWriteEnabledWithTimeout(enabled bool, timeout time.Duration) error { f.mu.Lock() // No-op if already in the requested state if f.writeEnabled == enabled { f.mu.Unlock() return nil } if !enabled { // DISABLING writes // Set disabling flag to prevent new transactions from starting f.disabling = true // Start goroutine to wake us on context cancellation or timeout // This ensures we don't block forever if context is cancelled or timeout expires waitDone := make(chan struct{}) var timeoutCh <-chan time.Time if timeout > 0 { timer := time.NewTimer(timeout) defer timer.Stop() timeoutCh = timer.C } go func() { select { case <-f.ctx.Done(): f.cond.Broadcast() // Wake cond.Wait() case <-timeoutCh: f.cond.Broadcast() // Wake cond.Wait() on timeout case <-waitDone: // Normal completion, nothing to do } }() // Wait for active transaction to complete deadline := time.Now().Add(timeout) for f.inTransaction { // Check context before waiting select { case <-f.ctx.Done(): close(waitDone) f.disabling = false f.cond.Broadcast() // Wake any waiting Lock() calls f.mu.Unlock() return fmt.Errorf("context cancelled while waiting for transaction: %w", f.ctx.Err()) default: } // Check timeout if specified if timeout > 0 && time.Now().After(deadline) { close(waitDone) f.disabling = false f.cond.Broadcast() // Wake any waiting Lock() calls f.mu.Unlock() return fmt.Errorf("timeout waiting for transaction to complete (waited %v)", timeout) } f.cond.Wait() // Unlocks mu, waits for signal, relocks mu } close(waitDone) // Stop the watcher goroutine // Sync dirty pages if any exist if len(f.dirty) > 0 { if err := f.syncToRemoteWithLock(); err != nil { f.disabling = false f.cond.Broadcast() // Wake any waiting Lock() calls f.mu.Unlock() return fmt.Errorf("sync before disable: %w", err) } } // Stop sync loop and ticker if running if f.syncStop != nil { close(f.syncStop) f.syncStop = nil } if f.syncTicker != nil { f.syncTicker.Stop() f.syncTicker = nil } f.writeEnabled = false f.disabling = false f.cond.Broadcast() // Wake any Lock() calls waiting for disable to complete f.logger.Info("write support disabled") f.mu.Unlock() return nil } // ENABLING writes (cold enable supported) // Initialize dirty map if not present if f.dirty == nil { f.dirty = make(map[uint32]int64) } // Set sync interval from VFS config if not set, falling back to default // This mirrors the startup behavior where WriteSyncInterval==0 uses DefaultSyncInterval if f.syncInterval == 0 && f.vfs != nil { f.syncInterval = f.vfs.WriteSyncInterval if f.syncInterval == 0 { f.syncInterval = DefaultSyncInterval } } // Set buffer path if not set if f.bufferPath == "" { if f.vfs != nil && f.vfs.WriteBufferPath != "" { f.bufferPath = f.vfs.WriteBufferPath } else if f.vfs != nil { // Use VFS temp directory dir, err := f.vfs.ensureTempDir() if err != nil { f.mu.Unlock() return fmt.Errorf("create temp dir for write buffer: %w", err) } f.bufferPath = filepath.Join(dir, "write-buffer") } else { // Fallback to os.TempDir() for cold enable without VFS reference f.bufferPath = filepath.Join(os.TempDir(), "litestream-write-buffer") } } // Initialize buffer file if not present if f.bufferFile == nil { if err := f.initWriteBufferWithLock(); err != nil { f.mu.Unlock() return fmt.Errorf("init write buffer: %w", err) } } // Initialize write tracking state if this is a cold enable if f.pendingTXID == 0 { f.expectedTXID = f.pos.TXID f.pendingTXID = f.pos.TXID + 1 } // Start sync ticker if not running and interval > 0 // (syncInterval == 0 means no periodic sync, only manual Sync() calls) if f.syncTicker == nil && f.syncInterval > 0 { f.syncTicker = time.NewTicker(f.syncInterval) f.syncStop = make(chan struct{}) stopCh := f.syncStop tickerCh := f.syncTicker.C f.wg.Add(1) go func() { defer f.wg.Done(); f.syncLoop(stopCh, tickerCh) }() } f.writeEnabled = true f.logger.Info("write support enabled", "pendingTXID", f.pendingTXID) f.mu.Unlock() return nil } // syncLoop runs periodic sync in the background. // The stop channel and ticker channel are passed as parameters to avoid races // with SetWriteEnabled potentially nilling them out before this goroutine starts. func (f *VFSFile) syncLoop(stopCh <-chan struct{}, tickerCh <-chan time.Time) { f.logger.Debug("starting sync loop", "interval", f.syncInterval) for { select { case <-f.ctx.Done(): f.logger.Debug("sync loop stopped (context cancelled)") return case <-stopCh: f.logger.Debug("sync loop stopped (write disabled)") return case <-tickerCh: if err := f.Sync(0); err != nil { f.logger.Error("periodic sync failed", "error", err) } } } } // syncToRemote syncs dirty pages to the remote replica. // This function acquires f.mu internally. func (f *VFSFile) syncToRemote() error { f.mu.Lock() defer f.mu.Unlock() return f.syncToRemoteWithLock() } // syncToRemoteWithLock syncs dirty pages to the remote replica. // Caller must hold f.mu. func (f *VFSFile) syncToRemoteWithLock() error { // Double-check dirty pages exist if len(f.dirty) == 0 { return nil } ctx := f.ctx // Check for conflicts if err := f.checkForConflict(ctx); err != nil { return err } // Create LTX file from dirty pages ltxReader := f.createLTXFromDirty() // Upload LTX file to remote info, err := f.client.WriteLTXFile(ctx, 0, f.pendingTXID, f.pendingTXID, ltxReader) if err != nil { return fmt.Errorf("upload LTX: %w", err) } f.logger.Info("synced to remote", "txid", info.MaxTXID, "pages", len(f.dirty), "size", info.Size) f.expectedTXID = f.pendingTXID f.pendingTXID++ f.pos = ltx.Pos{TXID: f.expectedTXID} if f.vfs != nil { f.vfs.writeMu.Lock() if f.expectedTXID > f.vfs.lastSyncedTXID { f.vfs.lastSyncedTXID = f.expectedTXID } f.vfs.writeMu.Unlock() } // Update cache with synced pages (index will be populated naturally when pages are fetched) for pgno, bufferOff := range f.dirty { cachedData := make([]byte, f.pageSize) if _, err := f.bufferFile.ReadAt(cachedData, bufferOff); err != nil { return fmt.Errorf("read page %d from buffer for cache: %w", pgno, err) } f.cache.Add(pgno, cachedData) } // Apply synced pages to hydrated file if hydration is complete // Must be done before clearing f.dirty since we need the page offsets if f.hydrator != nil && f.hydrator.Complete() { if err := f.applySyncedPagesToHydratedFile(); err != nil { f.logger.Error("failed to apply synced pages to hydrated file", "error", err) // Don't fail the sync - hydration will catch up on next poll } } // Clear dirty pages f.dirty = make(map[uint32]int64) // Clear write buffer after successful sync if err := f.clearWriteBuffer(); err != nil { f.logger.Error("failed to clear write buffer", "error", err) return fmt.Errorf("clear write buffer: %w", err) } return nil } // checkForConflict checks if the remote has newer transactions than expected. // Must be called with f.mu held. func (f *VFSFile) checkForConflict(ctx context.Context) error { // Get latest remote position itr, err := f.client.LTXFiles(ctx, 0, f.expectedTXID, false) if err != nil { return fmt.Errorf("check remote position: %w", err) } defer itr.Close() var remoteTXID ltx.TXID for itr.Next() { info := itr.Item() if info.MaxTXID > remoteTXID { remoteTXID = info.MaxTXID } } if err := itr.Close(); err != nil { return fmt.Errorf("iterate remote files: %w", err) } // If remote has advanced beyond our expected position, we have a conflict if remoteTXID > f.expectedTXID { f.logger.Warn("conflict detected", "expected", f.expectedTXID, "remote", remoteTXID) return fmt.Errorf("%w: expected TXID %d but remote has %d", ErrConflict, f.expectedTXID, remoteTXID) } return nil } // createLTXFromDirty creates an LTX file from dirty pages. // Returns a streaming reader for the LTX data using io.Pipe to avoid loading // all data into memory at once. // Must be called with f.mu held. func (f *VFSFile) createLTXFromDirty() io.Reader { pr, pw := io.Pipe() // Sort page numbers (LTX encoder requires ordered pages) pgnos := make([]uint32, 0, len(f.dirty)) for pgno := range f.dirty { pgnos = append(pgnos, pgno) } slices.Sort(pgnos) // Copy dirty map offsets for goroutine access dirtyOffsets := make(map[uint32]int64, len(f.dirty)) for pgno, off := range f.dirty { dirtyOffsets[pgno] = off } // Capture values for goroutine pageSize := f.pageSize commit := f.commit pendingTXID := f.pendingTXID bufferFile := f.bufferFile go func() { var err error defer func() { pw.CloseWithError(err) }() enc, encErr := ltx.NewEncoder(pw) if encErr != nil { err = encErr return } // Encode header if err = enc.EncodeHeader(ltx.Header{ Version: ltx.Version, Flags: ltx.HeaderFlagNoChecksum, PageSize: pageSize, Commit: commit, MinTXID: pendingTXID, MaxTXID: pendingTXID, Timestamp: time.Now().UnixMilli(), }); err != nil { err = fmt.Errorf("encode header: %w", err) return } // Encode each dirty page lockPgno := ltx.LockPgno(pageSize) for _, pgno := range pgnos { if pgno == lockPgno { continue // Skip lock page } // Read page data from buffer file bufferOff := dirtyOffsets[pgno] data := make([]byte, pageSize) if _, err = bufferFile.ReadAt(data, bufferOff); err != nil { err = fmt.Errorf("read page %d from buffer: %w", pgno, err) return } if err = enc.EncodePage(ltx.PageHeader{Pgno: pgno}, data); err != nil { err = fmt.Errorf("encode page %d: %w", pgno, err) return } } // Close encoder (writes trailer and page index) if err = enc.Close(); err != nil { err = fmt.Errorf("close encoder: %w", err) return } }() return pr } // initWriteBuffer initializes the write buffer file for durability. // Any existing buffer content is discarded since unsync'd changes are lost on restart. // This function acquires f.mu internally. func (f *VFSFile) initWriteBuffer() error { f.mu.Lock() defer f.mu.Unlock() return f.initWriteBufferWithLock() } // initWriteBufferWithLock initializes the write buffer file for durability. // Any existing buffer content is discarded since unsync'd changes are lost on restart. // Caller must hold f.mu. func (f *VFSFile) initWriteBufferWithLock() error { // Ensure parent directory exists if err := os.MkdirAll(filepath.Dir(f.bufferPath), 0755); err != nil { return fmt.Errorf("create buffer directory: %w", err) } // Open or create buffer file, truncating any existing content file, err := os.OpenFile(f.bufferPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { return fmt.Errorf("open buffer file: %w", err) } f.bufferFile = file f.bufferNextOff = 0 return nil } // writeToBuffer writes a dirty page to the write buffer for durability. // If the page already exists in the buffer, it overwrites at the same offset. // Otherwise, it appends to the end of the file. // Must be called with f.mu held. func (f *VFSFile) writeToBuffer(pgno uint32, data []byte) error { var writeOffset int64 if existingOff, ok := f.dirty[pgno]; ok { // Page already exists - overwrite at same offset writeOffset = existingOff } else { // New page - append to end of file writeOffset = f.bufferNextOff f.bufferNextOff += int64(len(data)) } // Write page data (no header, just raw page data) if _, err := f.bufferFile.WriteAt(data, writeOffset); err != nil { return fmt.Errorf("write page to buffer: %w", err) } // Update dirty map with offset f.dirty[pgno] = writeOffset return nil } // clearWriteBuffer clears and resets the write buffer after successful sync. func (f *VFSFile) clearWriteBuffer() error { // Truncate file to zero if err := f.bufferFile.Truncate(0); err != nil { return fmt.Errorf("truncate buffer: %w", err) } // Reset next write offset f.bufferNextOff = 0 return nil } func (f *VFSFile) FileSize() (size int64, err error) { pageSize, err := f.pageSizeBytes() if err != nil { return 0, err } f.mu.Lock() for pgno := range f.index { if v := int64(pgno) * int64(pageSize); v > size { size = v } } for pgno := range f.pending { if v := int64(pgno) * int64(pageSize); v > size { size = v } } // Include dirty pages in size calculation for pgno := range f.dirty { if v := int64(pgno) * int64(pageSize); v > size { size = v } } f.mu.Unlock() f.logger.Debug("file size", "size", size) return size, nil } func (f *VFSFile) Lock(elock sqlite3vfs.LockType) error { f.logger.Debug("locking file", "lock", elock) f.mu.Lock() defer f.mu.Unlock() if elock < f.lockType { return fmt.Errorf("invalid lock downgrade: current=%s target=%s", f.lockType, elock) } if elock >= sqlite3vfs.LockReserved { // Wait for any disable operation to complete before allowing RESERVED lock. // This prevents new write transactions from starting during disable. for f.disabling { f.logger.Debug("waiting for disable to complete before acquiring RESERVED lock") f.cond.Wait() } // Reject write-intent locks when writes are disabled. Since we always // report OpenReadWrite to SQLite (to support cold enable), SQLite may // attempt write transactions even when writes are logically disabled. if !f.writeEnabled { return sqlite3vfs.ReadOnlyError } } if f.writeEnabled && elock >= sqlite3vfs.LockReserved && !f.inTransaction { if f.vfs != nil { f.vfs.writeMu.Lock() if f.vfs.writeFile != nil && f.vfs.writeFile != f { f.vfs.writeMu.Unlock() return sqlite3vfs.BusyError } f.vfs.writeFile = f if f.vfs.lastSyncedTXID > f.expectedTXID && len(f.dirty) == 0 { f.expectedTXID = f.vfs.lastSyncedTXID f.pendingTXID = f.vfs.lastSyncedTXID + 1 f.pos = ltx.Pos{TXID: f.expectedTXID} } f.vfs.writeMu.Unlock() } f.inTransaction = true f.logger.Debug("transaction started", "expectedTXID", f.expectedTXID) } f.lockType = elock return nil } func (f *VFSFile) Unlock(elock sqlite3vfs.LockType) error { f.logger.Debug("unlocking file", "lock", elock) f.mu.Lock() defer f.mu.Unlock() if elock != sqlite3vfs.LockShared && elock != sqlite3vfs.LockNone { return fmt.Errorf("invalid unlock target: %s", elock) } if f.writeEnabled && f.inTransaction && elock < sqlite3vfs.LockReserved { f.inTransaction = false if f.vfs != nil { f.vfs.writeMu.Lock() if f.vfs.writeFile == f { f.vfs.writeFile = nil } f.vfs.writeMu.Unlock() } f.logger.Debug("transaction ended", "dirtyPages", len(f.dirty)) f.cond.Broadcast() // Wake up SetWriteEnabledWithTimeout if waiting } f.lockType = elock // Copy pending index to main index and invalidate affected pages in cache. if f.pendingReplace { // Replace entire index count := len(f.index) f.index = f.pending f.logger.Debug("cache invalidated all pages", "count", count) // Invalidate entire cache since we replaced the index f.cache.Purge() } else if len(f.pending) > 0 { // Merge pending into index count := len(f.pending) for k, v := range f.pending { f.index[k] = v f.cache.Remove(k) } f.logger.Debug("cache invalidated pages", "count", count) } f.pending = make(map[uint32]ltx.PageIndexElem) f.pendingReplace = false return nil } func (f *VFSFile) CheckReservedLock() (bool, error) { f.logger.Debug("checking reserved lock") f.mu.Lock() defer f.mu.Unlock() if f.lockType >= sqlite3vfs.LockReserved { return true, nil } if f.vfs != nil { f.vfs.writeMu.Lock() held := f.vfs.writeFile != nil f.vfs.writeMu.Unlock() return held, nil } return false, nil } func (f *VFSFile) SectorSize() int64 { f.logger.Debug("sector size") return 0 } func (f *VFSFile) DeviceCharacteristics() sqlite3vfs.DeviceCharacteristic { f.logger.Debug("device characteristics") return 0 } // parseTimeValue parses a timestamp string, trying RFC3339 first, then relative expressions. func parseTimeValue(value string) (time.Time, error) { // Try RFC3339Nano first (existing behavior) if t, err := time.Parse(time.RFC3339Nano, value); err == nil { return t, nil } // Try RFC3339 (without nanoseconds) if t, err := time.Parse(time.RFC3339, value); err == nil { return t, nil } // Fall back to dateparser for relative expressions cfg := &dateparser.Configuration{ CurrentTime: time.Now().UTC(), } result, err := dateparser.Parse(cfg, value) if err != nil { return time.Time{}, fmt.Errorf("invalid timestamp (expected RFC3339 or relative time like '5 minutes ago'): %s", value) } if result.Time.IsZero() { return time.Time{}, fmt.Errorf("could not parse time: %s", value) } return result.Time.UTC(), nil } // FileControl handles file control operations, specifically PRAGMA commands for time travel. func (f *VFSFile) FileControl(op int, pragmaName string, pragmaValue *string) (*string, error) { const SQLITE_FCNTL_PRAGMA = 14 if op != SQLITE_FCNTL_PRAGMA { return nil, fmt.Errorf("unsupported file control op: %d", op) } name := strings.ToLower(pragmaName) f.logger.Debug("file control", "pragma", name, "value", pragmaValue) switch name { case "litestream_txid": if pragmaValue != nil { return nil, fmt.Errorf("litestream_txid is read-only") } txid := f.Pos().TXID result := txid.String() return &result, nil case "litestream_lag": if pragmaValue != nil { return nil, fmt.Errorf("litestream_lag is read-only") } lastPoll := f.LastPollSuccess() if lastPoll.IsZero() { result := "-1" // Never polled successfully return &result, nil } lag := int64(time.Since(lastPoll).Seconds()) result := strconv.FormatInt(lag, 10) return &result, nil case "litestream_time": if pragmaValue == nil { result := f.currentTimeString() return &result, nil } if strings.EqualFold(*pragmaValue, "latest") { if err := f.ResetTime(context.Background()); err != nil { return nil, err } return nil, nil } t, err := parseTimeValue(*pragmaValue) if err != nil { return nil, err } if err := f.SetTargetTime(context.Background(), t); err != nil { return nil, err } return nil, nil case "litestream_hydration_progress": if pragmaValue != nil { return nil, fmt.Errorf("litestream_hydration_progress is read-only") } if f.hydrator == nil { result := "0" return &result, nil } pct := f.hydrator.Status().Pct() * 100 result := strconv.FormatFloat(pct, 'f', 1, 64) return &result, nil case "litestream_hydration_file": if pragmaValue != nil { return nil, fmt.Errorf("litestream_hydration_file is read-only") } result := f.hydrationPath return &result, nil case "litestream_write_enabled": if pragmaValue == nil { // READ mode - return current state f.mu.Lock() enabled := f.writeEnabled f.mu.Unlock() if enabled { result := "1" return &result, nil } result := "0" return &result, nil } // WRITE mode - enable or disable switch strings.ToLower(*pragmaValue) { case "0", "false", "off": if err := f.SetWriteEnabled(false); err != nil { return nil, err } return nil, nil case "1", "true", "on": if err := f.SetWriteEnabled(true); err != nil { return nil, err } return nil, nil default: return nil, fmt.Errorf("invalid value for litestream_write_enabled: %s (use 0 or 1)", *pragmaValue) } default: return nil, sqlite3vfs.NotFoundError } } // currentTimeString returns the current target time as a string. func (f *VFSFile) currentTimeString() string { if t := f.TargetTime(); t != nil { return t.Format(time.RFC3339Nano) } if t := f.LatestLTXTime(); !t.IsZero() { return t.Format(time.RFC3339Nano) } return "latest" // Fallback if no LTX files loaded } func isRetryablePageError(err error) bool { if err == nil { return false } if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) { return true } if errors.Is(err, io.ErrUnexpectedEOF) { return true } // Some remote clients wrap EOF in custom errors so we fall back to string matching. if strings.Contains(err.Error(), "unexpected EOF") { return true } if errors.Is(err, os.ErrNotExist) { return true } return false } func (f *VFSFile) monitorReplicaClient(ctx context.Context) { ticker := time.NewTicker(f.PollInterval) defer ticker.Stop() for { select { case <-ctx.Done(): return case <-ticker.C: if f.hasTargetTime() { continue } if err := f.pollReplicaClient(ctx); err != nil { // Don't log context cancellation errors during shutdown if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { f.logger.Error("cannot fetch new ltx files", "error", err) } } else { // Track successful poll time f.mu.Lock() f.lastPollSuccess = time.Now() f.mu.Unlock() } } } } // pollReplicaClient fetches new LTX files from the replica client and updates // the page index & the current position. func (f *VFSFile) pollReplicaClient(ctx context.Context) error { pos := f.Pos() f.logger.Debug("polling replica client", "txid", pos.TXID.String()) combined := make(map[uint32]ltx.PageIndexElem) f.mu.Lock() baseCommit := f.commit maxTXID1Snapshot := f.maxTXID1 f.mu.Unlock() newCommit := baseCommit replaceIndex := false maxTXID0, idx0, commit0, replace0, err := f.pollLevel(ctx, 0, pos.TXID, baseCommit) if err != nil { return fmt.Errorf("poll L0: %w", err) } if replace0 { replaceIndex = true baseCommit = commit0 newCommit = commit0 combined = idx0 } else { if len(idx0) > 0 { baseCommit = commit0 } for k, v := range idx0 { combined[k] = v } if commit0 > newCommit { newCommit = commit0 } } maxTXID1, idx1, commit1, replace1, err := f.pollLevel(ctx, 1, maxTXID1Snapshot, baseCommit) if err != nil { return fmt.Errorf("poll L1: %w", err) } if replace1 { replaceIndex = true baseCommit = commit1 newCommit = commit1 combined = idx1 } else { for k, v := range idx1 { combined[k] = v } if commit1 > newCommit { newCommit = commit1 } } // Send updates to a pending list if there are active readers. f.mu.Lock() defer f.mu.Unlock() if f.targetTime != nil { // Skip applying updates while time travel is active to avoid // overwriting the historical snapshot state. return nil } // Apply updates and invalidate cache entries for updated pages invalidateN := 0 target := f.index targetIsMain := true if f.lockType >= sqlite3vfs.LockShared { target = f.pending targetIsMain = false } else { f.pendingReplace = false } if replaceIndex { if f.lockType < sqlite3vfs.LockShared { f.index = make(map[uint32]ltx.PageIndexElem) target = f.index targetIsMain = true f.pendingReplace = false } else { f.pending = make(map[uint32]ltx.PageIndexElem) target = f.pending targetIsMain = false f.pendingReplace = true } } for k, v := range combined { target[k] = v // Invalidate cache if we're updating the main index if targetIsMain { f.cache.Remove(k) invalidateN++ } } if invalidateN > 0 { f.logger.Debug("cache invalidated pages due to new ltx files", "count", invalidateN) } if replaceIndex { f.commit = newCommit } else if len(combined) > 0 && newCommit > f.commit { f.commit = newCommit } if maxTXID0 > maxTXID1 { f.pos.TXID = maxTXID0 } else { f.pos.TXID = maxTXID1 } f.maxTXID1 = maxTXID1 f.logger.Debug("txid updated", "txid", f.pos.TXID.String(), "maxTXID1", f.maxTXID1.String()) // Apply updates to hydrated file if hydration is complete if f.hydrator != nil && f.hydrator.Complete() && len(combined) > 0 { if err := f.hydrator.ApplyUpdates(f.ctx, combined); err != nil { f.logger.Error("failed to apply updates to hydrated file", "error", err) } } return nil } // pollLevel fetches LTX files for a specific level and returns the highest TXID seen, // any index updates, the latest commit value, and if the index should be replaced. func (f *VFSFile) pollLevel(ctx context.Context, level int, prevMaxTXID ltx.TXID, baseCommit uint32) (ltx.TXID, map[uint32]ltx.PageIndexElem, uint32, bool, error) { itr, err := f.client.LTXFiles(ctx, level, prevMaxTXID+1, false) if err != nil { return prevMaxTXID, nil, baseCommit, false, fmt.Errorf("ltx files: %w", err) } defer func() { _ = itr.Close() }() index := make(map[uint32]ltx.PageIndexElem) maxTXID := prevMaxTXID lastCommit := baseCommit newCommit := baseCommit replaceIndex := false for itr.Next() { info := itr.Item() f.mu.Lock() isNextTXID := info.MinTXID == maxTXID+1 f.mu.Unlock() if !isNextTXID { if level == 0 && info.MinTXID > maxTXID+1 { f.logger.Warn("ltx gap detected at L0, deferring to higher levels", "expected", maxTXID+1, "next", info.MinTXID) break } return maxTXID, nil, newCommit, replaceIndex, fmt.Errorf("non-contiguous ltx file: level=%d, current=%s, next=%s-%s", level, maxTXID, info.MinTXID, info.MaxTXID) } f.logger.Debug("new ltx file", "level", info.Level, "min", info.MinTXID, "max", info.MaxTXID) idx, err := FetchPageIndex(ctx, f.client, info) if err != nil { return maxTXID, nil, newCommit, replaceIndex, fmt.Errorf("fetch page index: %w", err) } hdr, err := FetchLTXHeader(ctx, f.client, info) if err != nil { return maxTXID, nil, newCommit, replaceIndex, fmt.Errorf("fetch header: %w", err) } if hdr.Commit < lastCommit { replaceIndex = true index = make(map[uint32]ltx.PageIndexElem) } lastCommit = hdr.Commit newCommit = hdr.Commit for k, v := range idx { f.logger.Debug("adding new page index", "page", k, "elem", v) index[k] = v } maxTXID = info.MaxTXID } return maxTXID, index, newCommit, replaceIndex, nil } func (f *VFSFile) pageSizeBytes() (uint32, error) { f.mu.Lock() pageSize := f.pageSize f.mu.Unlock() if pageSize == 0 { f.logger.Debug("page size not initialized", "pageSize", 0) return 0, &DBNotReadyError{Reason: "page size not initialized"} } return pageSize, nil } func detectPageSizeFromInfos(ctx context.Context, client ReplicaClient, infos []*ltx.FileInfo) (uint32, error) { var lastErr error for i := len(infos) - 1; i >= 0; i-- { pageSize, err := readPageSizeFromInfo(ctx, client, infos[i]) if err != nil { lastErr = err continue } if !isSupportedPageSize(pageSize) { return 0, fmt.Errorf("unsupported page size: %d", pageSize) } return pageSize, nil } if lastErr != nil { return 0, fmt.Errorf("read ltx header: %w", lastErr) } return 0, fmt.Errorf("no ltx file available to determine page size") } func readPageSizeFromInfo(ctx context.Context, client ReplicaClient, info *ltx.FileInfo) (uint32, error) { rc, err := client.OpenLTXFile(ctx, info.Level, info.MinTXID, info.MaxTXID, 0, ltx.HeaderSize) if err != nil { return 0, fmt.Errorf("open ltx file: %w", err) } defer rc.Close() dec := ltx.NewDecoder(rc) if err := dec.DecodeHeader(); err != nil { return 0, fmt.Errorf("decode ltx header: %w", err) } return dec.Header().PageSize, nil } func isSupportedPageSize(pageSize uint32) bool { switch pageSize { case 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536: return true default: return false } } func (f *VFSFile) waitForRestorePlan() ([]*ltx.FileInfo, error) { // If write mode is enabled, don't wait - return immediately so we can // create a new database if no files exist. if f.writeEnabled { infos, err := CalcRestorePlan(f.ctx, f.client, 0, time.Time{}, f.logger) if err != nil { return nil, err } return infos, nil } // For read-only mode, wait for files to become available for { infos, err := CalcRestorePlan(f.ctx, f.client, 0, time.Time{}, f.logger) if err == nil { return infos, nil } if !errors.Is(err, ErrTxNotAvailable) { return nil, fmt.Errorf("cannot calc restore plan: %w", err) } f.logger.Debug("no backup files available yet, waiting", "interval", f.PollInterval) select { case <-time.After(f.PollInterval): case <-f.ctx.Done(): return nil, fmt.Errorf("no backup files available: %w", f.ctx.Err()) } } } // RegisterVFSConnection maps a SQLite connection handle to its VFS file ID. func RegisterVFSConnection(dbPtr uintptr, fileID uint64) error { if _, ok := lookupVFSFile(fileID); !ok { return fmt.Errorf("vfs file not found: id=%d", fileID) } vfsConnectionMap.Store(dbPtr, fileID) return nil } // UnregisterVFSConnection removes a connection mapping. func UnregisterVFSConnection(dbPtr uintptr) { vfsConnectionMap.Delete(dbPtr) } // SetVFSConnectionTime rebuilds the VFS index for a connection at a timestamp. func SetVFSConnectionTime(dbPtr uintptr, timestamp string) error { file, err := vfsFileForConnection(dbPtr) if err != nil { return err } t, err := parseTimeValue(timestamp) if err != nil { return err } return file.SetTargetTime(context.Background(), t) } // ResetVFSConnectionTime rebuilds the VFS index to the latest state. func ResetVFSConnectionTime(dbPtr uintptr) error { file, err := vfsFileForConnection(dbPtr) if err != nil { return err } return file.ResetTime(context.Background()) } // GetVFSConnectionTime returns the current time for a connection. func GetVFSConnectionTime(dbPtr uintptr) (string, error) { file, err := vfsFileForConnection(dbPtr) if err != nil { return "", err } return file.currentTimeString(), nil } // GetVFSConnectionTXID returns the current transaction ID for a connection as a hex string. func GetVFSConnectionTXID(dbPtr uintptr) (string, error) { file, err := vfsFileForConnection(dbPtr) if err != nil { return "", err } return file.Pos().TXID.String(), nil } // GetVFSConnectionLag returns seconds since last successful poll for a connection. func GetVFSConnectionLag(dbPtr uintptr) (int64, error) { file, err := vfsFileForConnection(dbPtr) if err != nil { return 0, err } lastPoll := file.LastPollSuccess() if lastPoll.IsZero() { return -1, nil } return int64(time.Since(lastPoll).Seconds()), nil } func vfsFileForConnection(dbPtr uintptr) (*VFSFile, error) { v, ok := vfsConnectionMap.Load(dbPtr) if !ok { return nil, fmt.Errorf("connection not registered") } fileID, ok := v.(uint64) if !ok { return nil, fmt.Errorf("invalid connection mapping") } file, ok := lookupVFSFile(fileID) if !ok { return nil, fmt.Errorf("vfs file not found: id=%d", fileID) } return file, nil } func lookupVFSFile(fileID uint64) (*VFSFile, bool) { sqlite3vfsFileMux.Lock() defer sqlite3vfsFileMux.Unlock() file, ok := sqlite3vfsFileMap[fileID] if !ok { return nil, false } vfsFile, ok := file.(*VFSFile) return vfsFile, ok } // startCompactionMonitors starts background goroutines for compaction and snapshots. func (f *VFSFile) startCompactionMonitors() { f.compactionCtx, f.compactionCancel = context.WithCancel(f.ctx) // Use configured levels or defaults levels := f.vfs.CompactionLevels if levels == nil { levels = DefaultCompactionLevels } // Start compaction monitors for each level for _, lvl := range levels { if lvl.Level == 0 { continue // L0 doesn't need compaction (source level) } f.compactionWg.Add(1) go func(level *CompactionLevel) { defer f.compactionWg.Done() f.monitorCompaction(f.compactionCtx, level) }(lvl) } // Start snapshot monitor if configured if f.vfs.SnapshotInterval > 0 { f.compactionWg.Add(1) go func() { defer f.compactionWg.Done() f.monitorSnapshots(f.compactionCtx) }() } // Start L0 retention monitor if configured if f.vfs.L0Retention > 0 { f.compactionWg.Add(1) go func() { defer f.compactionWg.Done() f.monitorL0Retention(f.compactionCtx) }() } f.logger.Info("compaction monitors started", "levels", len(levels), "snapshotInterval", f.vfs.SnapshotInterval, "l0Retention", f.vfs.L0Retention) } // Compact compacts source level files into the destination level. // Returns ErrNoCompaction if there are no files to compact. func (f *VFSFile) Compact(ctx context.Context, level int) (*ltx.FileInfo, error) { if f.compactor == nil { return nil, fmt.Errorf("compaction not enabled") } return f.compactor.Compact(ctx, level) } // Snapshot creates a full database snapshot from remote LTX files. // Unlike DB.Snapshot(), this reads from remote rather than local WAL. func (f *VFSFile) Snapshot(ctx context.Context) (*ltx.FileInfo, error) { if f.compactor == nil { return nil, fmt.Errorf("compaction not enabled") } f.mu.Lock() pageSize := f.pageSize commit := f.commit pos := f.pos pages := make(map[uint32]ltx.PageIndexElem, len(f.index)) for pgno, elem := range f.index { pages[pgno] = elem } f.mu.Unlock() if pageSize == 0 { f.logger.Debug("snapshot skipped, page size not initialized", "pageSize", 0) return nil, &DBNotReadyError{Reason: "page size not initialized"} } // Sort page numbers for consistent output pgnos := make([]uint32, 0, len(pages)) for pgno := range pages { pgnos = append(pgnos, pgno) } slices.Sort(pgnos) // Stream snapshot creation pr, pw := io.Pipe() go func() { enc, err := ltx.NewEncoder(pw) if err != nil { pw.CloseWithError(err) return } if err := enc.EncodeHeader(ltx.Header{ Version: ltx.Version, Flags: ltx.HeaderFlagNoChecksum, PageSize: pageSize, Commit: commit, MinTXID: 1, MaxTXID: pos.TXID, Timestamp: time.Now().UnixMilli(), }); err != nil { pw.CloseWithError(fmt.Errorf("encode header: %w", err)) return } for _, pgno := range pgnos { elem := pages[pgno] _, data, err := FetchPage(ctx, f.client, elem.Level, elem.MinTXID, elem.MaxTXID, elem.Offset, elem.Size) if err != nil { pw.CloseWithError(fmt.Errorf("fetch page %d: %w", pgno, err)) return } if err := enc.EncodePage(ltx.PageHeader{Pgno: pgno}, data); err != nil { pw.CloseWithError(fmt.Errorf("encode page %d: %w", pgno, err)) return } } if err := enc.Close(); err != nil { pw.CloseWithError(fmt.Errorf("close encoder: %w", err)) return } pw.Close() }() return f.client.WriteLTXFile(ctx, SnapshotLevel, 1, pos.TXID, pr) } // monitorCompaction runs periodic compaction for a level. func (f *VFSFile) monitorCompaction(ctx context.Context, lvl *CompactionLevel) { f.logger.Info("starting VFS compaction monitor", "level", lvl.Level, "interval", lvl.Interval) ticker := time.NewTicker(lvl.Interval) defer ticker.Stop() for { select { case <-ctx.Done(): return case <-ticker.C: info, err := f.Compact(ctx, lvl.Level) if err != nil { if !errors.Is(err, ErrNoCompaction) && !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { f.logger.Error("compaction failed", "level", lvl.Level, "error", err) } } else { f.logger.Debug("compaction completed", "level", lvl.Level, "minTXID", info.MinTXID, "maxTXID", info.MaxTXID, "size", info.Size) } } } } // monitorSnapshots runs periodic snapshot creation. func (f *VFSFile) monitorSnapshots(ctx context.Context) { f.logger.Info("starting VFS snapshot monitor", "interval", f.vfs.SnapshotInterval) ticker := time.NewTicker(f.vfs.SnapshotInterval) defer ticker.Stop() for { select { case <-ctx.Done(): return case <-ticker.C: info, err := f.Snapshot(ctx) if err != nil { // DBNotReadyError is already logged at debug level in Snapshot() if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) && !errors.Is(err, ErrDBNotReady) { f.logger.Error("snapshot failed", "error", err) } } else { f.logger.Debug("snapshot created", "maxTXID", info.MaxTXID, "size", info.Size) // Enforce snapshot retention after creating new snapshot if f.vfs.SnapshotRetention > 0 { if _, err := f.compactor.EnforceSnapshotRetention(ctx, f.vfs.SnapshotRetention); err != nil { f.logger.Error("snapshot retention failed", "error", err) } } } } } } // monitorL0Retention runs periodic L0 retention enforcement. func (f *VFSFile) monitorL0Retention(ctx context.Context) { f.logger.Info("starting VFS L0 retention monitor", "retention", f.vfs.L0Retention) // Check more frequently than the retention period checkInterval := f.vfs.L0Retention / 4 if checkInterval < time.Minute { checkInterval = time.Minute } ticker := time.NewTicker(checkInterval) defer ticker.Stop() for { select { case <-ctx.Done(): return case <-ticker.C: if err := f.compactor.EnforceL0Retention(ctx, f.vfs.L0Retention); err != nil { if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { f.logger.Error("L0 retention enforcement failed", "error", err) } } } } } ================================================ FILE: vfs_compaction_test.go ================================================ //go:build vfs // +build vfs package litestream_test import ( "context" "log/slog" "testing" "time" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/file" ) func TestVFSFile_Compact(t *testing.T) { t.Run("ManualCompact", func(t *testing.T) { dir := t.TempDir() client := file.NewReplicaClient(dir) // Pre-create some L0 files to test compaction createTestLTXFile(t, client, 0, 1, 1) createTestLTXFile(t, client, 0, 2, 2) createTestLTXFile(t, client, 0, 3, 3) // Create VFS with compaction enabled vfs := litestream.NewVFS(client, slog.Default()) vfs.WriteEnabled = true vfs.CompactionEnabled = true // Create VFSFile directly to test Compact method f := litestream.NewVFSFile(client, "test.db", slog.Default()) f.PollInterval = time.Second f.CacheSize = litestream.DefaultCacheSize // Initialize the compactor manually compactor := litestream.NewCompactor(client, slog.Default()) // Compact L0 to L1 info, err := compactor.Compact(context.Background(), 1) if err != nil { t.Fatal(err) } if info.Level != 1 { t.Errorf("Level=%d, want 1", info.Level) } if info.MinTXID != 1 || info.MaxTXID != 3 { t.Errorf("TXID range=%d-%d, want 1-3", info.MinTXID, info.MaxTXID) } }) } func TestVFSFile_Snapshot(t *testing.T) { t.Run("MultiLevelCompaction", func(t *testing.T) { dir := t.TempDir() client := file.NewReplicaClient(dir) // Pre-create L0 files to simulate VFS writes createTestLTXFile(t, client, 0, 1, 1) createTestLTXFile(t, client, 0, 2, 2) createTestLTXFile(t, client, 0, 3, 3) compactor := litestream.NewCompactor(client, slog.Default()) // Compact L0 to L1 info, err := compactor.Compact(context.Background(), 1) if err != nil { t.Fatal(err) } if info.Level != 1 { t.Errorf("Level=%d, want 1", info.Level) } t.Logf("Compacted to L1: minTXID=%d, maxTXID=%d", info.MinTXID, info.MaxTXID) // Compact L1 to L2 info, err = compactor.Compact(context.Background(), 2) if err != nil { t.Fatal(err) } if info.Level != 2 { t.Errorf("Level=%d, want 2", info.Level) } t.Logf("Compacted to L2: minTXID=%d, maxTXID=%d", info.MinTXID, info.MaxTXID) // Verify L2 file exists itr, err := client.LTXFiles(context.Background(), 2, 0, false) if err != nil { t.Fatal(err) } defer itr.Close() var l2Count int for itr.Next() { l2Count++ } if l2Count != 1 { t.Errorf("L2 file count=%d, want 1", l2Count) } }) } func TestDefaultCompactionLevels(t *testing.T) { levels := litestream.DefaultCompactionLevels if len(levels) != 4 { t.Fatalf("DefaultCompactionLevels length=%d, want 4", len(levels)) } // Verify L0 (raw files, no interval) if levels[0].Level != 0 { t.Errorf("levels[0].Level=%d, want 0", levels[0].Level) } if levels[0].Interval != 0 { t.Errorf("levels[0].Interval=%v, want 0", levels[0].Interval) } // Verify L1 (30 second compaction) if levels[1].Level != 1 { t.Errorf("levels[1].Level=%d, want 1", levels[1].Level) } if levels[1].Interval != 30*time.Second { t.Errorf("levels[1].Interval=%v, want 30s", levels[1].Interval) } // Verify L2 (5 minute compaction) if levels[2].Level != 2 { t.Errorf("levels[2].Level=%d, want 2", levels[2].Level) } if levels[2].Interval != 5*time.Minute { t.Errorf("levels[2].Interval=%v, want 5m", levels[2].Interval) } // Verify L3 (hourly compaction) if levels[3].Level != 3 { t.Errorf("levels[3].Level=%d, want 3", levels[3].Level) } if levels[3].Interval != time.Hour { t.Errorf("levels[3].Interval=%v, want 1h", levels[3].Interval) } // Verify they validate if err := levels.Validate(); err != nil { t.Errorf("DefaultCompactionLevels.Validate()=%v, want nil", err) } } func TestVFS_CompactionConfig(t *testing.T) { t.Run("DefaultConfig", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) vfs := litestream.NewVFS(client, slog.Default()) // Default should have compaction disabled if vfs.CompactionEnabled { t.Error("CompactionEnabled should be false by default") } if vfs.CompactionLevels != nil { t.Error("CompactionLevels should be nil by default") } if vfs.SnapshotInterval != 0 { t.Error("SnapshotInterval should be 0 by default") } }) t.Run("WithCompactionConfig", func(t *testing.T) { client := file.NewReplicaClient(t.TempDir()) vfs := litestream.NewVFS(client, slog.Default()) vfs.WriteEnabled = true vfs.CompactionEnabled = true vfs.CompactionLevels = litestream.CompactionLevels{ {Level: 0, Interval: 0}, {Level: 1, Interval: time.Minute}, } vfs.SnapshotInterval = 24 * time.Hour vfs.SnapshotRetention = 7 * 24 * time.Hour vfs.L0Retention = 5 * time.Minute if !vfs.CompactionEnabled { t.Error("CompactionEnabled should be true") } if len(vfs.CompactionLevels) != 2 { t.Errorf("CompactionLevels length=%d, want 2", len(vfs.CompactionLevels)) } if vfs.SnapshotInterval != 24*time.Hour { t.Errorf("SnapshotInterval=%v, want 24h", vfs.SnapshotInterval) } if vfs.SnapshotRetention != 7*24*time.Hour { t.Errorf("SnapshotRetention=%v, want 168h", vfs.SnapshotRetention) } if vfs.L0Retention != 5*time.Minute { t.Errorf("L0Retention=%v, want 5m", vfs.L0Retention) } }) } ================================================ FILE: vfs_test.go ================================================ //go:build vfs package litestream import ( "bytes" "context" "errors" "fmt" "io" "log/slog" "os" "path/filepath" "strings" "sync" "sync/atomic" "testing" "time" "github.com/psanford/sqlite3vfs" "github.com/superfly/ltx" ) func TestVFSFile_LockStateMachine(t *testing.T) { f := &VFSFile{logger: slog.Default(), writeEnabled: true} f.cond = sync.NewCond(&f.mu) if err := f.Lock(sqlite3vfs.LockShared); err != nil { t.Fatalf("lock shared: %v", err) } if reserved, _ := f.CheckReservedLock(); reserved { t.Fatalf("shared lock should not report reserved") } if err := f.Lock(sqlite3vfs.LockReserved); err != nil { t.Fatalf("lock reserved: %v", err) } if reserved, _ := f.CheckReservedLock(); !reserved { t.Fatalf("reserved lock should report reserved") } if err := f.Lock(sqlite3vfs.LockShared); err == nil { t.Fatalf("expected downgrade via Lock to fail") } if err := f.Unlock(sqlite3vfs.LockShared); err != nil { t.Fatalf("unlock to shared: %v", err) } if reserved, _ := f.CheckReservedLock(); reserved { t.Fatalf("unlock to shared should clear reserved state") } if err := f.Unlock(sqlite3vfs.LockPending); err == nil { t.Fatalf("expected unlock to pending to fail") } if err := f.Lock(sqlite3vfs.LockExclusive); err != nil { t.Fatalf("lock exclusive: %v", err) } if err := f.Unlock(sqlite3vfs.LockNone); err != nil { t.Fatalf("unlock to none: %v", err) } } func TestVFSFile_PendingIndexIsolation(t *testing.T) { client := newMockReplicaClient() client.addFixture(t, buildLTXFixture(t, 1, 'a')) f := NewVFSFile(client, "test.db", slog.Default()) if err := f.Open(); err != nil { t.Fatalf("open vfs file: %v", err) } if err := f.Lock(sqlite3vfs.LockShared); err != nil { t.Fatalf("lock shared: %v", err) } client.addFixture(t, buildLTXFixture(t, 2, 'b')) if err := f.pollReplicaClient(context.Background()); err != nil { t.Fatalf("poll replica: %v", err) } f.mu.Lock() pendingLen := len(f.pending) current := f.index[1] f.mu.Unlock() if pendingLen == 0 { t.Fatalf("expected pending index entries while shared lock held") } if current.MinTXID != 1 { t.Fatalf("main index should still reference first txid, got %s", current.MinTXID) } buf := make([]byte, 4096) if _, err := f.ReadAt(buf, 0); err != nil { t.Fatalf("read during lock: %v", err) } if buf[0] != 'a' { t.Fatalf("expected old data during lock, got %q", buf[0]) } if err := f.Unlock(sqlite3vfs.LockNone); err != nil { t.Fatalf("unlock: %v", err) } if _, err := f.ReadAt(buf, 0); err != nil { t.Fatalf("read after unlock: %v", err) } if buf[0] != 'b' { t.Fatalf("expected updated data after unlock, got %q", buf[0]) } } func TestVFSFile_PendingIndexRace(t *testing.T) { client := newMockReplicaClient() client.addFixture(t, buildLTXFixture(t, 1, 'a')) f := NewVFSFile(client, "race.db", slog.Default()) if err := f.Open(); err != nil { t.Fatalf("open vfs file: %v", err) } if err := f.Lock(sqlite3vfs.LockShared); err != nil { t.Fatalf("lock shared: %v", err) } ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond) defer cancel() // continuously stream new fixtures go func() { txid := ltx.TXID(2) for { select { case <-ctx.Done(): return default: } client.addFixture(t, buildLTXFixture(t, txid, byte('a'+int(txid%26)))) if err := f.pollReplicaClient(context.Background()); err != nil { t.Errorf("poll replica: %v", err) return } txid++ time.Sleep(2 * time.Millisecond) } }() var wg sync.WaitGroup for i := 0; i < 8; i++ { wg.Add(1) go func(id int) { defer wg.Done() buf := make([]byte, 4096) for { select { case <-ctx.Done(): return default: } if _, err := f.ReadAt(buf, 0); err != nil { t.Errorf("reader %d: %v", id, err) return } } }(i) } <-ctx.Done() f.Unlock(sqlite3vfs.LockNone) wg.Wait() } func TestVFSFileMonitorStopsOnCancel(t *testing.T) { client := newCountingReplicaClient() f := &VFSFile{client: client, logger: slog.Default(), PollInterval: 5 * time.Millisecond} ctx, cancel := context.WithCancel(context.Background()) var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done(); f.monitorReplicaClient(ctx) }() deadline := time.Now().Add(200 * time.Millisecond) for time.Now().Before(deadline) { if client.calls.Load() > 0 { break } time.Sleep(1 * time.Millisecond) } if client.calls.Load() == 0 { t.Fatalf("monitor never invoked LTXFiles") } cancel() finished := make(chan struct{}) go func() { wg.Wait() close(finished) }() select { case <-finished: case <-time.After(200 * time.Millisecond): t.Fatalf("monitor goroutine did not exit after cancel") } } func TestVFSFile_NonContiguousTXIDError(t *testing.T) { client := newMockReplicaClient() client.addFixture(t, buildLTXFixture(t, 1, 'a')) f := NewVFSFile(client, "gap.db", slog.Default()) if err := f.Open(); err != nil { t.Fatalf("open vfs file: %v", err) } client.addFixture(t, buildLTXFixture(t, 3, 'c')) if err := f.pollReplicaClient(context.Background()); err != nil { t.Fatalf("poll replica: %v", err) } if pos := f.Pos(); pos.TXID != 1 { t.Fatalf("unexpected txid advance after gap: got %s", pos.TXID.String()) } } func TestVFSFile_IndexMemoryDoesNotGrowUnbounded(t *testing.T) { const pageLimit = 16 client := newMockReplicaClient() client.addFixture(t, buildLTXFixture(t, 1, 'a')) f := NewVFSFile(client, "mem.db", slog.Default()) if err := f.Open(); err != nil { t.Fatalf("open vfs file: %v", err) } for i := 0; i < 100; i++ { pgno := uint32(i%pageLimit) + 2 client.addFixture(t, buildLTXFixtureWithPages(t, ltx.TXID(i+2), 4096, []uint32{pgno}, byte('b'+byte(i%26)))) if err := f.pollReplicaClient(context.Background()); err != nil { t.Fatalf("poll replica: %v", err) } } f.mu.Lock() defer f.mu.Unlock() if l := len(f.index); l > pageLimit+1 { // +1 for initial page 1 t.Fatalf("index grew unexpectedly: got %d want <= %d", l, pageLimit+1) } } func TestVFSFile_AutoVacuumShrinksCommit(t *testing.T) { client := newMockReplicaClient() client.addFixture(t, buildLTXFixtureWithPages(t, 1, 4096, []uint32{1, 2, 3, 4}, 'a')) f := NewVFSFile(client, "autovac.db", slog.Default()) if err := f.Open(); err != nil { t.Fatalf("open vfs file: %v", err) } client.addFixture(t, buildLTXFixtureWithPages(t, 2, 4096, []uint32{1, 2}, 'b')) if err := f.pollReplicaClient(context.Background()); err != nil { t.Fatalf("poll replica: %v", err) } size, err := f.FileSize() if err != nil { t.Fatalf("file size: %v", err) } if size != int64(2*4096) { t.Fatalf("unexpected file size after vacuum: got %d want %d", size, 2*4096) } buf := make([]byte, 4096) lockOffset := int64(3-1) * 4096 if _, err := f.ReadAt(buf, lockOffset); err == nil || !strings.Contains(err.Error(), "page not found") { t.Fatalf("expected missing page after vacuum, got %v", err) } } func TestVFSFile_PendingIndexReplacementRemovesStalePages(t *testing.T) { client := newMockReplicaClient() client.addFixture(t, buildLTXFixtureWithPages(t, 1, 4096, []uint32{1, 2, 3, 4}, 'a')) f := NewVFSFile(client, "pending-replace.db", slog.Default()) if err := f.Open(); err != nil { t.Fatalf("open vfs file: %v", err) } if err := f.Lock(sqlite3vfs.LockShared); err != nil { t.Fatalf("lock shared: %v", err) } client.addFixture(t, buildLTXFixtureWithPages(t, 2, 4096, []uint32{1, 2}, 'b')) if err := f.pollReplicaClient(context.Background()); err != nil { t.Fatalf("poll replica: %v", err) } f.mu.Lock() if _, ok := f.index[4]; !ok { t.Fatalf("expected stale page to remain in main index while lock is held") } if !f.pendingReplace { t.Fatalf("expected pending replacement flag set") } f.mu.Unlock() if err := f.Unlock(sqlite3vfs.LockNone); err != nil { t.Fatalf("unlock: %v", err) } size, err := f.FileSize() if err != nil { t.Fatalf("file size: %v", err) } if size != int64(2*4096) { t.Fatalf("unexpected file size after pending replacement applied: got %d want %d", size, 2*4096) } buf := make([]byte, 4096) lockOffset := int64(3-1) * 4096 if _, err := f.ReadAt(buf, lockOffset); err == nil || !strings.Contains(err.Error(), "page not found") { t.Fatalf("expected missing page after pending replacement applied, got %v", err) } } func TestVFSFile_CorruptedPageIndexRecovery(t *testing.T) { client := newMockReplicaClient() client.addFixture(t, <xFixture{info: <x.FileInfo{Level: 0, MinTXID: 1, MaxTXID: 1, Size: 0}, data: []byte("bad-index")}) f := NewVFSFile(client, "corrupt.db", slog.Default()) if err := f.Open(); err == nil { t.Fatalf("expected open to fail on corrupted index") } } func TestVFSFile_OpenSeedsLevel1Position(t *testing.T) { client := newMockReplicaClient() snapshot := buildLTXFixture(t, 1, 's') snapshot.info.Level = SnapshotLevel client.addFixture(t, snapshot) l1 := buildLTXFixture(t, 2, 'l') l1.info.Level = 1 client.addFixture(t, l1) l0 := buildLTXFixture(t, 3, 'z') l0.info.Level = 0 client.addFixture(t, l0) f := NewVFSFile(client, "seed-level1.db", slog.Default()) if err := f.Open(); err != nil { t.Fatalf("open vfs file: %v", err) } defer f.Close() if got, want := f.maxTXID1, l1.info.MaxTXID; got != want { t.Fatalf("unexpected maxTXID1: got %s want %s", got, want) } if got, want := f.Pos().TXID, l0.info.MaxTXID; got != want { t.Fatalf("unexpected pos after open: got %s want %s", got, want) } } func TestVFSFile_OpenSeedsLevel1PositionFromPos(t *testing.T) { client := newMockReplicaClient() snapshot := buildLTXFixture(t, 1, 's') snapshot.info.Level = SnapshotLevel client.addFixture(t, snapshot) l0 := buildLTXFixture(t, 2, '0') l0.info.Level = 0 client.addFixture(t, l0) f := NewVFSFile(client, "seed-default.db", slog.Default()) if err := f.Open(); err != nil { t.Fatalf("open vfs file: %v", err) } defer f.Close() pos := f.Pos().TXID if pos == 0 { t.Fatalf("expected non-zero position") } if got := f.maxTXID1; got != pos { t.Fatalf("expected maxTXID1 to equal pos when no L1 files, got %s want %s", got, pos) } } func TestVFSFile_HeaderForcesDeleteJournal(t *testing.T) { client := newMockReplicaClient() client.addFixture(t, buildLTXFixture(t, 1, 'h')) f := NewVFSFile(client, "header.db", slog.Default()) if err := f.Open(); err != nil { t.Fatalf("open vfs file: %v", err) } defer f.Close() buf := make([]byte, 32) if _, err := f.ReadAt(buf, 0); err != nil { t.Fatalf("read header: %v", err) } if buf[18] != 0x01 || buf[19] != 0x01 { t.Fatalf("journal mode bytes not forced to DELETE, got %x %x", buf[18], buf[19]) } } func TestVFSFile_ReadAtLockPageBoundary(t *testing.T) { pageSizes := []uint32{512, 1024, 2048, 4096, 8192, 16384, 32768, 65536} for _, pageSize := range pageSizes { pageSize := pageSize t.Run(fmt.Sprintf("page_%d", pageSize), func(t *testing.T) { client := newMockReplicaClient() lockPgno := ltx.LockPgno(pageSize) before := lockPgno - 1 after := lockPgno + 1 client.addFixture(t, buildLTXFixtureWithPage(t, 1, pageSize, 1, 'z')) client.addFixture(t, buildLTXFixtureWithPage(t, 2, pageSize, before, 'b')) client.addFixture(t, buildLTXFixtureWithPage(t, 3, pageSize, after, 'a')) f := NewVFSFile(client, fmt.Sprintf("lock-boundary-%d.db", pageSize), slog.Default()) if err := f.Open(); err != nil { t.Fatalf("open vfs file: %v", err) } defer f.Close() buf := make([]byte, int(pageSize)) off := int64(before-1) * int64(pageSize) if _, err := f.ReadAt(buf, off); err != nil { t.Fatalf("read before lock page: %v", err) } if buf[0] != 'b' { t.Fatalf("unexpected data before lock page: got %q", buf[0]) } buf = make([]byte, int(pageSize)) off = int64(after-1) * int64(pageSize) if _, err := f.ReadAt(buf, off); err != nil { t.Fatalf("read after lock page: %v", err) } if buf[0] != 'a' { t.Fatalf("unexpected data after lock page: got %q", buf[0]) } buf = make([]byte, int(pageSize)) lockOffset := int64(lockPgno-1) * int64(pageSize) if _, err := f.ReadAt(buf, lockOffset); err == nil || !strings.Contains(err.Error(), "page not found") { t.Fatalf("expected missing lock page error, got %v", err) } }) } } func TestVFS_TempFileLifecycleStress(t *testing.T) { vfs := NewVFS(nil, slog.Default()) const ( workers = 8 iterations = 50 ) var wg sync.WaitGroup errCh := make(chan error, workers) for w := 0; w < workers; w++ { w := w wg.Add(1) go func() { defer wg.Done() for i := 0; i < iterations; i++ { name := fmt.Sprintf("temp-%02d-%02d.db", w, i) flags := sqlite3vfs.OpenTempDB | sqlite3vfs.OpenReadWrite | sqlite3vfs.OpenCreate deleteOnClose := (w+i)%2 == 0 if deleteOnClose { flags |= sqlite3vfs.OpenDeleteOnClose } file, _, err := vfs.openTempFile(name, flags) if err != nil { errCh <- fmt.Errorf("open temp file: %w", err) return } tf := file.(*localTempFile) if _, err := tf.WriteAt([]byte("hot-data"), 0); err != nil { errCh <- fmt.Errorf("write temp file: %w", err) return } path, tracked := vfs.loadTempFilePath(name) if !tracked && name != "" { errCh <- fmt.Errorf("temp file %s was not tracked", name) return } if err := tf.Close(); err != nil { errCh <- fmt.Errorf("close temp file: %w", err) return } if deleteOnClose { if path != "" { if _, err := os.Stat(path); err == nil || !os.IsNotExist(err) { errCh <- fmt.Errorf("delete-on-close leaked temp file %s", path) return } } } else { if path == "" { errCh <- fmt.Errorf("missing tracked path for %s", name) return } if _, err := os.Stat(path); err != nil { errCh <- fmt.Errorf("expected temp file on disk: %v", err) return } if err := os.Remove(path); err != nil { errCh <- fmt.Errorf("cleanup temp file: %v", err) return } } } }() } wg.Wait() close(errCh) for err := range errCh { if err != nil { t.Fatalf("temp file stress: %v", err) } } leak := false vfs.tempFiles.Range(func(key, value any) bool { leak = true return false }) if leak { t.Fatalf("temp files still tracked after stress run") } if dir := vfs.tempDir; dir != "" { entries, err := os.ReadDir(dir) if err != nil && !os.IsNotExist(err) { t.Fatalf("read temp dir: %v", err) } if err == nil && len(entries) > 0 { names := make([]string, 0, len(entries)) for _, entry := range entries { names = append(names, entry.Name()) } t.Fatalf("temp dir not cleaned: %v", names) } } } func TestVFS_TempFileNameCollision(t *testing.T) { vfs := NewVFS(nil, slog.Default()) name := "collision.db" flags := sqlite3vfs.OpenTempDB | sqlite3vfs.OpenReadWrite | sqlite3vfs.OpenCreate file1, _, err := vfs.openTempFile(name, flags) if err != nil { t.Fatalf("open temp file1: %v", err) } tf1 := file1.(*localTempFile) path1, ok := vfs.loadTempFilePath(name) if !ok { t.Fatalf("first temp file not tracked") } file2, _, err := vfs.openTempFile(name, flags|sqlite3vfs.OpenDeleteOnClose) if err != nil { t.Fatalf("open temp file2: %v", err) } tf2 := file2.(*localTempFile) path2, ok := vfs.loadTempFilePath(name) if !ok { t.Fatalf("second temp file not tracked") } if path1 != path2 { t.Fatalf("expected same canonical path, got %s vs %s", path1, path2) } if err := tf2.Close(); err != nil { t.Fatalf("close second file: %v", err) } if _, err := os.Stat(path2); err == nil || !os.IsNotExist(err) { t.Fatalf("expected file removed after delete-on-close") } if _, ok := vfs.loadTempFilePath(name); ok { t.Fatalf("canonical entry should be cleared after delete-on-close") } if err := tf1.Close(); err != nil { t.Fatalf("close first file: %v", err) } } func TestVFS_TempFileSameBasenameDifferentDirs(t *testing.T) { vfs := NewVFS(nil, slog.Default()) flags := sqlite3vfs.OpenTempDB | sqlite3vfs.OpenReadWrite | sqlite3vfs.OpenCreate name1 := filepath.Join("foo", "shared.db") name2 := filepath.Join("bar", "shared.db") file1, _, err := vfs.openTempFile(name1, flags) if err != nil { t.Fatalf("open first temp file: %v", err) } tf1 := file1.(*localTempFile) path1, ok := vfs.loadTempFilePath(name1) if !ok { t.Fatalf("first temp file not tracked") } file2, _, err := vfs.openTempFile(name2, flags|sqlite3vfs.OpenDeleteOnClose) if err != nil { t.Fatalf("open second temp file: %v", err) } tf2 := file2.(*localTempFile) path2, ok := vfs.loadTempFilePath(name2) if !ok { t.Fatalf("second temp file not tracked") } if path1 == path2 { t.Fatalf("expected unique paths for %s and %s", name1, name2) } if err := tf1.Close(); err != nil { t.Fatalf("close first file: %v", err) } if _, ok := vfs.loadTempFilePath(name2); !ok { t.Fatalf("closing first file should not unregister second") } if path1 != "" { if err := os.Remove(path1); err != nil && !os.IsNotExist(err) { t.Fatalf("cleanup first temp file: %v", err) } } if err := tf2.Close(); err != nil { t.Fatalf("close second file: %v", err) } if _, ok := vfs.loadTempFilePath(name2); ok { t.Fatalf("delete-on-close should clear second temp file") } } func TestVFS_TempFileDeleteOnClose(t *testing.T) { vfs := NewVFS(nil, slog.Default()) name := "delete-on-close.db" flags := sqlite3vfs.OpenTempDB | sqlite3vfs.OpenReadWrite | sqlite3vfs.OpenCreate | sqlite3vfs.OpenDeleteOnClose file, _, err := vfs.openTempFile(name, flags) if err != nil { t.Fatalf("open temp file: %v", err) } tf := file.(*localTempFile) path, ok := vfs.loadTempFilePath(name) if !ok { t.Fatalf("temp file not tracked") } if _, err := tf.WriteAt([]byte("x"), 0); err != nil { t.Fatalf("write temp file: %v", err) } if err := tf.Close(); err != nil { t.Fatalf("close temp file: %v", err) } if _, err := os.Stat(path); err == nil || !os.IsNotExist(err) { t.Fatalf("expected delete-on-close to remove temp file") } if _, ok := vfs.loadTempFilePath(name); ok { t.Fatalf("temp file tracking entry should be cleared") } if err := vfs.Delete(name, false); err != nil { t.Fatalf("delete should ignore missing temp files: %v", err) } if err := vfs.Delete(name, false); err != nil { t.Fatalf("delete should ignore repeated temp deletes: %v", err) } } func TestLocalTempFileLocking(t *testing.T) { f, err := os.CreateTemp(t.TempDir(), "local-temp-*") if err != nil { t.Fatalf("create temp: %v", err) } tf := newLocalTempFile(f, false, nil) defer tf.Close() assertReserved := func(want bool) { t.Helper() got, err := tf.CheckReservedLock() if err != nil { t.Fatalf("check reserved: %v", err) } if got != want { t.Fatalf("reserved lock state mismatch: got %v want %v", got, want) } } assertReserved(false) if err := tf.Lock(sqlite3vfs.LockShared); err != nil { t.Fatalf("lock shared: %v", err) } assertReserved(false) if err := tf.Lock(sqlite3vfs.LockReserved); err != nil { t.Fatalf("lock reserved: %v", err) } assertReserved(true) if err := tf.Unlock(sqlite3vfs.LockShared); err != nil { t.Fatalf("unlock shared: %v", err) } assertReserved(false) if err := tf.Lock(sqlite3vfs.LockExclusive); err != nil { t.Fatalf("lock exclusive: %v", err) } assertReserved(true) if err := tf.Unlock(sqlite3vfs.LockNone); err != nil { t.Fatalf("unlock none: %v", err) } assertReserved(false) } func TestVFS_DeleteIgnoresMissingTempFiles(t *testing.T) { vfs := NewVFS(nil, slog.Default()) t.Run("AlreadyRemovedEntry", func(t *testing.T) { name := "already-removed.db" flags := sqlite3vfs.OpenTempDB | sqlite3vfs.OpenReadWrite | sqlite3vfs.OpenCreate | sqlite3vfs.OpenDeleteOnClose file, _, err := vfs.openTempFile(name, flags) if err != nil { t.Fatalf("open temp file: %v", err) } tf := file.(*localTempFile) if err := tf.Close(); err != nil { t.Fatalf("close temp file: %v", err) } if err := vfs.Delete(name, false); err != nil { t.Fatalf("delete should ignore missing tracked entry: %v", err) } }) t.Run("MissingOnDisk", func(t *testing.T) { name := "missing-on-disk.db" flags := sqlite3vfs.OpenTempDB | sqlite3vfs.OpenReadWrite | sqlite3vfs.OpenCreate file, _, err := vfs.openTempFile(name, flags) if err != nil { t.Fatalf("open temp file: %v", err) } tf := file.(*localTempFile) path, ok := vfs.loadTempFilePath(name) if !ok { t.Fatalf("temp file not tracked") } if err := os.Remove(path); err != nil { t.Fatalf("remove backing file: %v", err) } if err := vfs.Delete(name, false); err != nil { t.Fatalf("delete should ignore missing file: %v", err) } if _, ok := vfs.loadTempFilePath(name); ok { t.Fatalf("temp file tracking entry should be cleared") } if err := tf.Close(); err != nil { t.Fatalf("close temp file: %v", err) } }) } func TestVFS_TempDirExhaustion(t *testing.T) { vfs := NewVFS(nil, slog.Default()) injected := fmt.Errorf("temp dir exhausted") vfs.tempDirOnce.Do(func() { vfs.tempDirErr = injected }) if _, err := vfs.ensureTempDir(); !errors.Is(err, injected) { t.Fatalf("expected ensureTempDir error, got %v", err) } if _, _, err := vfs.openTempFile("exhausted.db", sqlite3vfs.OpenTempDB); !errors.Is(err, injected) { t.Fatalf("openTempFile should surface exhaustion error, got %v", err) } } func TestVFSFile_PollingCancelsBlockedLTXFiles(t *testing.T) { client := newBlockingReplicaClient() client.addFixture(t, buildLTXFixture(t, 1, 'a')) f := NewVFSFile(client, "blocking.db", slog.Default()) f.PollInterval = 5 * time.Millisecond if err := f.Open(); err != nil { t.Fatalf("open vfs file: %v", err) } client.blockNext.Store(true) deadline := time.After(200 * time.Millisecond) select { case <-client.blocked: case <-deadline: t.Fatalf("expected monitor to block on LTXFiles") } done := make(chan struct{}) go func() { _ = f.Close() close(done) }() select { case <-done: case <-time.After(500 * time.Millisecond): t.Fatalf("close did not unblock blocked LTXFiles call") } if !client.cancelled.Load() { t.Fatalf("blocking client did not observe context cancellation") } } // mockReplicaClient implements ReplicaClient for deterministic LTX fixtures. type mockReplicaClient struct { mu sync.Mutex files []*ltx.FileInfo data map[string][]byte } type blockingReplicaClient struct { *mockReplicaClient blockNext atomic.Bool blocked chan struct{} cancelled atomic.Bool once sync.Once } type countingReplicaClient struct { calls atomic.Uint64 } func newCountingReplicaClient() *countingReplicaClient { return &countingReplicaClient{} } func (c *countingReplicaClient) Type() string { return "count" } func (c *countingReplicaClient) Init(context.Context) error { return nil } func (c *countingReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { c.calls.Add(1) return ltx.NewFileInfoSliceIterator(nil), nil } func (c *countingReplicaClient) OpenLTXFile(context.Context, int, ltx.TXID, ltx.TXID, int64, int64) (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(nil)), nil } func (c *countingReplicaClient) WriteLTXFile(context.Context, int, ltx.TXID, ltx.TXID, io.Reader) (*ltx.FileInfo, error) { return nil, fmt.Errorf("not implemented") } func (c *countingReplicaClient) DeleteLTXFiles(context.Context, []*ltx.FileInfo) error { return nil } func (c *countingReplicaClient) DeleteAll(context.Context) error { return nil } func newMockReplicaClient() *mockReplicaClient { return &mockReplicaClient{data: make(map[string][]byte)} } func newBlockingReplicaClient() *blockingReplicaClient { return &blockingReplicaClient{ mockReplicaClient: newMockReplicaClient(), blocked: make(chan struct{}), } } func (c *mockReplicaClient) Type() string { return "mock" } func (c *mockReplicaClient) Init(context.Context) error { return nil } func (c *mockReplicaClient) addFixture(tb testing.TB, fx *ltxFixture) { tb.Helper() c.mu.Lock() defer c.mu.Unlock() c.files = append(c.files, fx.info) c.data[c.key(fx.info)] = fx.data } func (c *mockReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { c.mu.Lock() defer c.mu.Unlock() var out []*ltx.FileInfo for _, info := range c.files { if info.Level == level && info.MinTXID >= seek { out = append(out, info) } } return ltx.NewFileInfoSliceIterator(out), nil } func (c *mockReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { c.mu.Lock() defer c.mu.Unlock() key := c.makeKey(level, minTXID, maxTXID) data, ok := c.data[key] if !ok { return nil, fmt.Errorf("ltx file not found") } if offset > int64(len(data)) { return nil, fmt.Errorf("offset beyond data") } slice := data[offset:] if size > 0 && size < int64(len(slice)) { slice = slice[:size] } return io.NopCloser(bytes.NewReader(slice)), nil } func (c *mockReplicaClient) WriteLTXFile(context.Context, int, ltx.TXID, ltx.TXID, io.Reader) (*ltx.FileInfo, error) { return nil, fmt.Errorf("not implemented") } func (c *mockReplicaClient) DeleteLTXFiles(context.Context, []*ltx.FileInfo) error { return fmt.Errorf("not implemented") } func (c *mockReplicaClient) DeleteAll(context.Context) error { return fmt.Errorf("not implemented") } func (c *blockingReplicaClient) Type() string { return "blocking" } func (c *blockingReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { if seek > 1 && c.blockNext.Load() { if c.blockNext.CompareAndSwap(true, false) { c.once.Do(func() { close(c.blocked) }) <-ctx.Done() c.cancelled.Store(true) return nil, ctx.Err() } } return c.mockReplicaClient.LTXFiles(ctx, level, seek, useMetadata) } func (c *blockingReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { return c.mockReplicaClient.OpenLTXFile(ctx, level, minTXID, maxTXID, offset, size) } func (c *blockingReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { return c.mockReplicaClient.WriteLTXFile(ctx, level, minTXID, maxTXID, r) } func (c *blockingReplicaClient) DeleteLTXFiles(ctx context.Context, files []*ltx.FileInfo) error { return c.mockReplicaClient.DeleteLTXFiles(ctx, files) } func (c *blockingReplicaClient) DeleteAll(ctx context.Context) error { return c.mockReplicaClient.DeleteAll(ctx) } func (c *mockReplicaClient) key(info *ltx.FileInfo) string { return c.makeKey(info.Level, info.MinTXID, info.MaxTXID) } func (c *mockReplicaClient) makeKey(level int, minTXID, maxTXID ltx.TXID) string { return fmt.Sprintf("%d:%s:%s", level, minTXID.String(), maxTXID.String()) } type ltxFixture struct { info *ltx.FileInfo data []byte } func buildLTXFixture(tb testing.TB, txid ltx.TXID, fill byte) *ltxFixture { return buildLTXFixtureWithPage(tb, txid, 4096, 1, fill) } func buildLTXFixtureWithPage(tb testing.TB, txid ltx.TXID, pageSize, pgno uint32, fill byte) *ltxFixture { return buildLTXFixtureWithPages(tb, txid, pageSize, []uint32{pgno}, fill) } func buildLTXFixtureWithPages(tb testing.TB, txid ltx.TXID, pageSize uint32, pgnos []uint32, fill byte) *ltxFixture { tb.Helper() if len(pgnos) == 0 { tb.Fatalf("pgnos required") } if txid == 1 { if len(pgnos) == 0 || pgnos[0] != 1 { tb.Fatalf("snapshot fixture must start at page 1") } } var buf bytes.Buffer enc, err := ltx.NewEncoder(&buf) if err != nil { tb.Fatalf("new encoder: %v", err) } maxPg := uint32(0) for _, pg := range pgnos { if pg > maxPg { maxPg = pg } } if maxPg == 0 { maxPg = 1 } hdr := ltx.Header{ Version: ltx.Version, PageSize: pageSize, Commit: maxPg, MinTXID: txid, MaxTXID: txid, Timestamp: time.Now().UnixMilli(), Flags: ltx.HeaderFlagNoChecksum, } if err := enc.EncodeHeader(hdr); err != nil { tb.Fatalf("encode header: %v", err) } for _, pg := range pgnos { if pg == 0 { pg = 1 } page := bytes.Repeat([]byte{fill}, int(pageSize)) if err := enc.EncodePage(ltx.PageHeader{Pgno: pg}, page); err != nil { tb.Fatalf("encode page %d: %v", pg, err) } } if err := enc.Close(); err != nil { tb.Fatalf("close encoder: %v", err) } info := <x.FileInfo{ Level: 0, MinTXID: txid, MaxTXID: txid, Size: int64(buf.Len()), CreatedAt: time.Now().UTC(), } return <xFixture{info: info, data: buf.Bytes()} } // TestVFSFile_Hydration_Basic tests that hydration completes and reads from local file. func TestVFSFile_Hydration_Basic(t *testing.T) { client := newMockReplicaClient() client.addFixture(t, buildLTXFixture(t, 1, 'a')) // Create temp directory for hydration hydrationDir := t.TempDir() // Create VFSFile with hydration enabled f := NewVFSFile(client, "test.db", slog.Default()) f.hydrationPath = filepath.Join(hydrationDir, "test.db.hydration.db") f.PollInterval = 100 * time.Millisecond if err := f.Open(); err != nil { t.Fatalf("open vfs file: %v", err) } defer f.Close() // Wait for hydration to complete deadline := time.Now().Add(5 * time.Second) for f.hydrator == nil || !f.hydrator.Complete() { if time.Now().After(deadline) { t.Fatalf("hydration did not complete in time") } time.Sleep(10 * time.Millisecond) } // Verify hydration file exists if _, err := os.Stat(f.hydrationPath); err != nil { t.Fatalf("hydration file not found: %v", err) } // Read a page - should come from hydrated file buf := make([]byte, 4096) if _, err := f.ReadAt(buf, 0); err != nil { t.Fatalf("read at: %v", err) } // Check that the data matches (excluding modified header bytes) for i := 28; i < len(buf); i++ { if buf[i] != 'a' { t.Fatalf("expected byte 'a' at position %d, got %q", i, buf[i]) } } } // TestVFSFile_Hydration_ReadsDuringHydration tests that reads work via cache/remote during hydration. func TestVFSFile_Hydration_ReadsDuringHydration(t *testing.T) { client := newMockReplicaClient() client.addFixture(t, buildLTXFixture(t, 1, 'b')) hydrationDir := t.TempDir() f := NewVFSFile(client, "test.db", slog.Default()) f.hydrationPath = filepath.Join(hydrationDir, "test.db.hydration.db") f.PollInterval = 100 * time.Millisecond if err := f.Open(); err != nil { t.Fatalf("open vfs file: %v", err) } defer f.Close() // Read immediately - should work even if hydration is still in progress buf := make([]byte, 4096) if _, err := f.ReadAt(buf, 0); err != nil { t.Fatalf("read at during hydration: %v", err) } // Data should be correct regardless of hydration status for i := 28; i < len(buf); i++ { if buf[i] != 'b' { t.Fatalf("expected byte 'b' at position %d, got %q", i, buf[i]) } } } // TestVFSFile_Hydration_CloseEarly tests clean shutdown during hydration. func TestVFSFile_Hydration_CloseEarly(t *testing.T) { client := newMockReplicaClient() client.addFixture(t, buildLTXFixture(t, 1, 'c')) hydrationDir := t.TempDir() f := NewVFSFile(client, "test.db", slog.Default()) f.hydrationPath = filepath.Join(hydrationDir, "test.db.hydration.db") f.PollInterval = 100 * time.Millisecond if err := f.Open(); err != nil { t.Fatalf("open vfs file: %v", err) } // Close immediately without waiting for hydration if err := f.Close(); err != nil { t.Fatalf("close: %v", err) } // Hydration file should be removed if _, err := os.Stat(f.hydrationPath); !os.IsNotExist(err) { t.Fatalf("hydration file should be removed after close") } } // TestVFSFile_Hydration_Disabled tests that hydration has no effect when disabled. func TestVFSFile_Hydration_Disabled(t *testing.T) { client := newMockReplicaClient() client.addFixture(t, buildLTXFixture(t, 1, 'd')) f := NewVFSFile(client, "test.db", slog.Default()) // hydrationPath is empty by default (hydration disabled) f.PollInterval = 100 * time.Millisecond if err := f.Open(); err != nil { t.Fatalf("open vfs file: %v", err) } defer f.Close() // Hydrator should be nil when hydration is disabled if f.hydrator != nil { t.Fatalf("hydrator should be nil when disabled") } // Reads should still work via cache/remote buf := make([]byte, 4096) if _, err := f.ReadAt(buf, 0); err != nil { t.Fatalf("read at: %v", err) } } // TestVFSFile_Hydration_IncrementalUpdates tests that new LTX files are applied to hydrated file. func TestVFSFile_Hydration_IncrementalUpdates(t *testing.T) { client := newMockReplicaClient() client.addFixture(t, buildLTXFixture(t, 1, 'e')) hydrationDir := t.TempDir() f := NewVFSFile(client, "test.db", slog.Default()) f.hydrationPath = filepath.Join(hydrationDir, "test.db.hydration.db") f.PollInterval = 50 * time.Millisecond if err := f.Open(); err != nil { t.Fatalf("open vfs file: %v", err) } defer f.Close() // Wait for hydration to complete deadline := time.Now().Add(5 * time.Second) for f.hydrator == nil || !f.hydrator.Complete() { if time.Now().After(deadline) { t.Fatalf("hydration did not complete in time") } time.Sleep(10 * time.Millisecond) } // Add a new LTX file client.addFixture(t, buildLTXFixture(t, 2, 'f')) // Wait for poll to pick up the update time.Sleep(200 * time.Millisecond) // Read the page - should have updated data buf := make([]byte, 4096) if _, err := f.ReadAt(buf, 0); err != nil { t.Fatalf("read at: %v", err) } // Data should be updated (excluding header bytes) for i := 28; i < len(buf); i++ { if buf[i] != 'f' { t.Fatalf("expected byte 'f' at position %d, got %q", i, buf[i]) } } } func TestHydrator_Close_Persistent(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "hydration.db") client := newMockReplicaClient() h := NewHydrator(path, true, 4096, client, slog.Default()) if err := h.Init(); err != nil { t.Fatalf("init: %v", err) } h.SetTXID(5) if err := h.Close(); err != nil { t.Fatalf("close: %v", err) } if _, err := os.Stat(path); err != nil { t.Fatalf("hydration file should be preserved: %v", err) } metaPath := path + ".meta" data, err := os.ReadFile(metaPath) if err != nil { t.Fatalf("meta file should exist: %v", err) } if got := strings.TrimSpace(string(data)); got != "5" { t.Fatalf("expected meta TXID=5, got %q", got) } } func TestHydrator_Init_Resume(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "hydration.db") client := newMockReplicaClient() if err := os.WriteFile(path, []byte("existing-db-data"), 0600); err != nil { t.Fatal(err) } if err := os.WriteFile(path+".meta", []byte("42\n"), 0600); err != nil { t.Fatal(err) } h := NewHydrator(path, true, 4096, client, slog.Default()) if err := h.Init(); err != nil { t.Fatalf("init: %v", err) } defer h.Close() if got := h.TXID(); got != 42 { t.Fatalf("expected TXID=42, got %d", got) } data := make([]byte, 16) n, err := h.file.ReadAt(data, 0) if err != nil && err != io.EOF { t.Fatalf("read: %v", err) } if string(data[:n]) != "existing-db-data" { t.Fatalf("file contents should be preserved, got %q", string(data[:n])) } } func TestHydrator_Close_TempFile(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "hydration.db") client := newMockReplicaClient() h := NewHydrator(path, false, 4096, client, slog.Default()) if err := h.Init(); err != nil { t.Fatalf("init: %v", err) } h.SetTXID(10) if err := h.Close(); err != nil { t.Fatalf("close: %v", err) } if _, err := os.Stat(path); !os.IsNotExist(err) { t.Fatalf("temp hydration file should be deleted") } if _, err := os.Stat(path + ".meta"); !os.IsNotExist(err) { t.Fatalf("meta file should not exist for temp hydrator") } } func TestHydrator_Init_StaleMeta(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "hydration.db") client := newMockReplicaClient() if err := os.WriteFile(path+".meta", []byte("99\n"), 0600); err != nil { t.Fatal(err) } h := NewHydrator(path, true, 4096, client, slog.Default()) if err := h.Init(); err != nil { t.Fatalf("init: %v", err) } defer h.Close() if got := h.TXID(); got != 0 { t.Fatalf("expected TXID=0 for stale meta, got %d", got) } if _, err := os.Stat(path + ".meta"); !os.IsNotExist(err) { t.Fatalf("stale meta file should be removed") } } func TestVFSFile_Hydration_PersistentResumeOnReopen(t *testing.T) { client := newMockReplicaClient() client.addFixture(t, buildLTXFixture(t, 1, 'g')) hydrationDir := t.TempDir() hydrationPath := filepath.Join(hydrationDir, "persistent-hydration.db") f := NewVFSFile(client, "test.db", slog.Default()) f.hydrationPath = hydrationPath f.hydrationPersistent = true f.PollInterval = 50 * time.Millisecond if err := f.Open(); err != nil { t.Fatalf("open vfs file (first): %v", err) } deadline := time.Now().Add(5 * time.Second) for f.hydrator == nil || !f.hydrator.Complete() { if time.Now().After(deadline) { t.Fatalf("first hydration did not complete in time") } time.Sleep(10 * time.Millisecond) } if err := f.Close(); err != nil { t.Fatalf("close vfs file (first): %v", err) } if _, err := os.Stat(hydrationPath); err != nil { t.Fatalf("persistent hydration file should exist after close: %v", err) } if _, err := os.Stat(hydrationPath + ".meta"); err != nil { t.Fatalf("persistent hydration meta should exist after close: %v", err) } initialInfo, err := os.Stat(hydrationPath) if err != nil { t.Fatalf("stat hydration file after first close: %v", err) } f2 := NewVFSFile(client, "test.db", slog.Default()) f2.hydrationPath = hydrationPath f2.hydrationPersistent = true f2.PollInterval = 50 * time.Millisecond if err := f2.Open(); err != nil { t.Fatalf("open vfs file (second): %v", err) } deadline = time.Now().Add(5 * time.Second) for f2.hydrator == nil || !f2.hydrator.Complete() { if time.Now().After(deadline) { t.Fatalf("second hydration did not complete in time") } time.Sleep(10 * time.Millisecond) } if got := f2.hydrator.TXID(); got != 1 { t.Fatalf("expected resumed hydration txid=1, got %d", got) } if err := f2.Close(); err != nil { t.Fatalf("close vfs file (second): %v", err) } reopenedInfo, err := os.Stat(hydrationPath) if err != nil { t.Fatalf("stat hydration file after second close: %v", err) } if !reopenedInfo.ModTime().Equal(initialInfo.ModTime()) { t.Fatalf("expected hydration file modtime unchanged on reopen resume") } } ================================================ FILE: vfs_write_test.go ================================================ //go:build vfs // +build vfs package litestream import ( "bytes" "context" "errors" "fmt" "io" "log/slog" "os" "sort" "strings" "sync" "testing" "time" "github.com/psanford/sqlite3vfs" "github.com/superfly/ltx" ) // writeTestReplicaClient is a mock ReplicaClient for testing write functionality. type writeTestReplicaClient struct { mu sync.Mutex ltxFiles map[int][]*ltx.FileInfo // level -> files ltxData map[string][]byte // "level/minTXID-maxTXID" -> data } func newWriteTestReplicaClient() *writeTestReplicaClient { return &writeTestReplicaClient{ ltxFiles: make(map[int][]*ltx.FileInfo), ltxData: make(map[string][]byte), } } func (c *writeTestReplicaClient) Type() string { return "test" } func (c *writeTestReplicaClient) Init(ctx context.Context) error { return nil } func (c *writeTestReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) { c.mu.Lock() defer c.mu.Unlock() var files []*ltx.FileInfo for _, f := range c.ltxFiles[level] { if f.MinTXID >= seek { files = append(files, f) } } return &writeTestFileIterator{files: files}, nil } func (c *writeTestReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { c.mu.Lock() defer c.mu.Unlock() key := ltxKey(level, minTXID, maxTXID) data, ok := c.ltxData[key] if !ok { return nil, io.EOF } if offset > 0 || size > 0 { end := int64(len(data)) if size > 0 && offset+size < end { end = offset + size } data = data[offset:end] } return io.NopCloser(bytes.NewReader(data)), nil } func (c *writeTestReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { data, err := io.ReadAll(r) if err != nil { return nil, err } c.mu.Lock() defer c.mu.Unlock() key := ltxKey(level, minTXID, maxTXID) c.ltxData[key] = data info := <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, CreatedAt: time.Now(), Size: int64(len(data)), } c.ltxFiles[level] = append(c.ltxFiles[level], info) return info, nil } func (c *writeTestReplicaClient) DeleteLTXFiles(ctx context.Context, a []*ltx.FileInfo) error { return nil } func (c *writeTestReplicaClient) DeleteAll(ctx context.Context) error { c.mu.Lock() defer c.mu.Unlock() c.ltxFiles = make(map[int][]*ltx.FileInfo) c.ltxData = make(map[string][]byte) return nil } func ltxKey(level int, minTXID, maxTXID ltx.TXID) string { return string(rune(level)) + "/" + minTXID.String() + "-" + maxTXID.String() } // writeTestFileIterator implements ltx.FileIterator for testing. type writeTestFileIterator struct { files []*ltx.FileInfo index int } func (itr *writeTestFileIterator) Next() bool { if itr.index >= len(itr.files) { return false } itr.index++ return true } func (itr *writeTestFileIterator) Item() *ltx.FileInfo { if itr.index == 0 || itr.index > len(itr.files) { return nil } return itr.files[itr.index-1] } func (itr *writeTestFileIterator) Close() error { return nil } func (itr *writeTestFileIterator) Err() error { return nil } // createTestLTXFile creates an LTX file with initial data for testing. func createTestLTXFile(t *testing.T, client *writeTestReplicaClient, txid ltx.TXID, pageSize uint32, commit uint32, pages map[uint32][]byte) { t.Helper() var buf bytes.Buffer enc, err := ltx.NewEncoder(&buf) if err != nil { t.Fatal(err) } if err := enc.EncodeHeader(ltx.Header{ Version: ltx.Version, Flags: ltx.HeaderFlagNoChecksum, PageSize: pageSize, Commit: commit, MinTXID: txid, MaxTXID: txid, Timestamp: time.Now().UnixMilli(), }); err != nil { t.Fatal(err) } // Sort page numbers to ensure proper encoding order (page 1 must be first for snapshots) pgnos := make([]uint32, 0, len(pages)) for pgno := range pages { pgnos = append(pgnos, pgno) } sort.Slice(pgnos, func(i, j int) bool { return pgnos[i] < pgnos[j] }) for _, pgno := range pgnos { if err := enc.EncodePage(ltx.PageHeader{Pgno: pgno}, pages[pgno]); err != nil { t.Fatal(err) } } if err := enc.Close(); err != nil { t.Fatal(err) } client.mu.Lock() key := ltxKey(0, txid, txid) client.ltxData[key] = buf.Bytes() client.ltxFiles[0] = append(client.ltxFiles[0], <x.FileInfo{ Level: 0, MinTXID: txid, MaxTXID: txid, CreatedAt: time.Now(), Size: int64(buf.Len()), }) client.mu.Unlock() } // setupWriteableVFSFile creates a VFSFile with write support enabled and a buffer file. func setupWriteableVFSFile(t *testing.T, client *writeTestReplicaClient) *VFSFile { t.Helper() logger := slog.Default() f := NewVFSFile(client, "test.db", logger) f.writeEnabled = true f.dirty = make(map[uint32]int64) f.syncInterval = 0 // Create a temporary buffer file tmpFile, err := os.CreateTemp("", "litestream-test-buffer-*") if err != nil { t.Fatal(err) } f.bufferFile = tmpFile f.bufferPath = tmpFile.Name() f.bufferNextOff = 0 t.Cleanup(func() { if f.bufferFile != nil { f.bufferFile.Close() } os.Remove(f.bufferPath) }) return f } func TestVFSFile_WriteEnabled(t *testing.T) { client := newWriteTestReplicaClient() // Create initial LTX file with page 1 pageSize := uint32(4096) initialPage := make([]byte, pageSize) copy(initialPage, "initial data") createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) // Create VFSFile directly with write enabled tmpDir := t.TempDir() bufferPath := tmpDir + "/write-buffer" logger := slog.Default() f := NewVFSFile(client, "test.db", logger) f.writeEnabled = true f.dirty = make(map[uint32]int64) f.syncInterval = 0 f.bufferPath = bufferPath if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() if !f.writeEnabled { t.Error("expected writeEnabled to be true") } if f.dirty == nil { t.Error("expected dirty map to be initialized") } } func TestVFSFile_WriteAt(t *testing.T) { client := newWriteTestReplicaClient() // Create initial LTX file pageSize := uint32(4096) initialPage := make([]byte, pageSize) copy(initialPage, "initial data") createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) // Create VFSFile with write support f := setupWriteableVFSFile(t, client) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Write some data at offset 100 (within page 1) writeData := []byte("hello world") n, err := f.WriteAt(writeData, 100) if err != nil { t.Fatal(err) } if n != len(writeData) { t.Errorf("expected %d bytes written, got %d", len(writeData), n) } // Check dirty page exists if len(f.dirty) != 1 { t.Errorf("expected 1 dirty page, got %d", len(f.dirty)) } if _, ok := f.dirty[1]; !ok { t.Error("expected page 1 to be dirty") } // Read back the written data readBuf := make([]byte, len(writeData)) n, err = f.ReadAt(readBuf, 100) if err != nil { t.Fatal(err) } if string(readBuf) != string(writeData) { t.Errorf("expected %q, got %q", writeData, readBuf) } } func TestVFSFile_SyncToRemote(t *testing.T) { client := newWriteTestReplicaClient() // Create initial LTX file pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) // Create VFSFile with write support f := setupWriteableVFSFile(t, client) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Write data writeData := []byte("synced data") if _, err := f.WriteAt(writeData, 0); err != nil { t.Fatal(err) } // Sync to remote if err := f.Sync(0); err != nil { t.Fatal(err) } // Check dirty pages are cleared if len(f.dirty) != 0 { t.Errorf("expected 0 dirty pages after sync, got %d", len(f.dirty)) } // Check TXID advanced if f.expectedTXID != 2 { t.Errorf("expected TXID 2, got %d", f.expectedTXID) } // Check LTX file was written to client client.mu.Lock() if len(client.ltxFiles[0]) != 2 { t.Errorf("expected 2 LTX files, got %d", len(client.ltxFiles[0])) } client.mu.Unlock() } func TestVFSFile_ConflictDetection(t *testing.T) { client := newWriteTestReplicaClient() // Create initial LTX file pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) // Create VFSFile with write support f := setupWriteableVFSFile(t, client) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Write data if _, err := f.WriteAt([]byte("data"), 0); err != nil { t.Fatal(err) } // Simulate remote advancement (another writer) createTestLTXFile(t, client, 2, pageSize, 1, map[uint32][]byte{1: initialPage}) // Try to sync - should fail with conflict err := f.Sync(0) if err == nil { t.Fatal("expected conflict error") } if err.Error() != "remote has newer transactions than expected: expected TXID 1 but remote has 2" { t.Errorf("unexpected error: %v", err) } } func TestVFSFile_TransactionTracking(t *testing.T) { client := newWriteTestReplicaClient() // Create initial LTX file pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) // Create VFSFile with write support f := setupWriteableVFSFile(t, client) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Acquire RESERVED lock (start transaction) if err := f.Lock(2); err != nil { // sqlite3vfs.LockReserved = 2 t.Fatal(err) } if !f.inTransaction { t.Error("expected inTransaction to be true after RESERVED lock") } // Write data if _, err := f.WriteAt([]byte("tx data"), 0); err != nil { t.Fatal(err) } // Sync should be skipped during transaction if err := f.Sync(0); err != nil { t.Fatal(err) } if len(f.dirty) == 0 { t.Error("expected dirty pages to remain during transaction") } // Release lock (end transaction) if err := f.Unlock(1); err != nil { // sqlite3vfs.LockShared = 1 t.Fatal(err) } if f.inTransaction { t.Error("expected inTransaction to be false after unlock") } // Now sync should work if err := f.Sync(0); err != nil { t.Fatal(err) } if len(f.dirty) != 0 { t.Error("expected dirty pages to be cleared after sync") } } func TestVFSFile_Truncate(t *testing.T) { client := newWriteTestReplicaClient() // Create initial LTX files with 2 pages pageSize := uint32(4096) page1 := make([]byte, pageSize) page2 := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 2, map[uint32][]byte{1: page1, 2: page2}) // Create VFSFile with write support f := setupWriteableVFSFile(t, client) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Write to page 2 if _, err := f.WriteAt([]byte("page2 data"), int64(pageSize)); err != nil { t.Fatal(err) } // Truncate to 1 page if err := f.Truncate(int64(pageSize)); err != nil { t.Fatal(err) } // Page 2 should no longer be dirty if _, ok := f.dirty[2]; ok { t.Error("expected page 2 to be removed from dirty pages") } // Commit should be 1 if f.commit != 1 { t.Errorf("expected commit 1, got %d", f.commit) } } func TestVFSFile_WriteBuffer(t *testing.T) { client := newWriteTestReplicaClient() // Create initial LTX file pageSize := uint32(4096) initialPage := make([]byte, pageSize) copy(initialPage, "initial data") createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) // Create temp directory for buffer tmpDir := t.TempDir() bufferPath := tmpDir + "/.litestream-write-buffer" // Create VFSFile with write buffer logger := slog.Default() f := NewVFSFile(client, "test.db", logger) f.writeEnabled = true f.dirty = make(map[uint32]int64) f.syncInterval = 0 f.bufferPath = bufferPath if err := f.Open(); err != nil { t.Fatal(err) } // Write some data writeData := []byte("buffered data") if _, err := f.WriteAt(writeData, 0); err != nil { t.Fatal(err) } // Check buffer file exists and has content stat, err := os.Stat(bufferPath) if err != nil { t.Fatalf("buffer file should exist: %v", err) } if stat.Size() == 0 { t.Error("buffer file should not be empty") } // Don't call f.Close() - simulate a crash by just abandoning the file handle // Close just the buffer file directly to release the handle if f.bufferFile != nil { f.bufferFile.Close() } f.cancel() // Stop any goroutines // Verify buffer file still has content (simulating crash before sync) stat, err = os.Stat(bufferPath) if err != nil { t.Fatalf("buffer file should still exist after crash: %v", err) } if stat.Size() == 0 { t.Error("buffer file should still have content after crash") } } func TestVFSFile_WriteBufferDiscardedOnOpen(t *testing.T) { // Test that unsync'd buffer contents are discarded on open (no recovery) client := newWriteTestReplicaClient() // Create initial LTX file pageSize := uint32(4096) initialPage := make([]byte, pageSize) copy(initialPage, "initial data") createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) // Create temp directory for buffer tmpDir := t.TempDir() bufferPath := tmpDir + "/.litestream-write-buffer" // First: create a VFSFile and write some data logger := slog.Default() f1 := NewVFSFile(client, "test.db", logger) f1.writeEnabled = true f1.dirty = make(map[uint32]int64) f1.syncInterval = 0 f1.bufferPath = bufferPath if err := f1.Open(); err != nil { t.Fatal(err) } // Write data (will be written to buffer) writeData := make([]byte, pageSize) copy(writeData, "unsync'd data that should be lost") if _, err := f1.WriteAt(writeData, 0); err != nil { t.Fatal(err) } // Simulate crash by abandoning the file handle without syncing if f1.bufferFile != nil { f1.bufferFile.Close() } f1.cancel() // Second: create a new VFSFile - buffer should be discarded f2 := NewVFSFile(client, "test.db", logger) f2.writeEnabled = true f2.dirty = make(map[uint32]int64) f2.syncInterval = 0 f2.bufferPath = bufferPath if err := f2.Open(); err != nil { t.Fatal(err) } defer f2.Close() // Dirty pages should NOT be recovered - buffer is discarded on open if len(f2.dirty) != 0 { t.Errorf("expected 0 dirty pages (buffer should be discarded), got %d", len(f2.dirty)) } // Reading should return original data from replica, not unsync'd data readBuf := make([]byte, pageSize) if _, err := f2.ReadAt(readBuf, 0); err != nil { t.Fatal(err) } if string(readBuf[:12]) != "initial data" { t.Errorf("expected 'initial data' (from replica), got %q", string(readBuf[:12])) } } func TestVFSFile_WriteBufferClearAfterSync(t *testing.T) { client := newWriteTestReplicaClient() // Create initial LTX file pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) // Create temp directory for buffer tmpDir := t.TempDir() bufferPath := tmpDir + "/.litestream-write-buffer" // Create VFSFile with write buffer logger := slog.Default() f := NewVFSFile(client, "test.db", logger) f.writeEnabled = true f.dirty = make(map[uint32]int64) f.syncInterval = 0 f.bufferPath = bufferPath if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Write data if _, err := f.WriteAt([]byte("sync test"), 0); err != nil { t.Fatal(err) } // Check buffer has content before sync stat, _ := os.Stat(bufferPath) if stat.Size() == 0 { t.Error("buffer should have content before sync") } // Sync to remote if err := f.Sync(0); err != nil { t.Fatal(err) } // Check buffer is cleared after sync stat, _ = os.Stat(bufferPath) if stat.Size() != 0 { t.Errorf("buffer should be empty after sync, got size %d", stat.Size()) } } func TestVFSFile_OpenFailsWithInvalidBufferPath(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) logger := slog.Default() f := NewVFSFile(client, "test.db", logger) f.writeEnabled = true f.dirty = make(map[uint32]int64) f.syncInterval = 0 f.bufferPath = "/nonexistent/path/that/cannot/be/created/buffer" err := f.Open() if err == nil { f.Close() t.Fatal("expected Open to fail with invalid buffer path") } } func TestVFSFile_BufferFileAlwaysCreatedWhenWriteEnabled(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) tmpDir := t.TempDir() bufferPath := tmpDir + "/write-buffer" logger := slog.Default() f := NewVFSFile(client, "test.db", logger) f.writeEnabled = true f.dirty = make(map[uint32]int64) f.syncInterval = 0 f.bufferPath = bufferPath if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() if f.bufferFile == nil { t.Fatal("bufferFile should never be nil when writeEnabled is true") } } func TestVFSFile_OpenNewDatabase(t *testing.T) { // Test opening a VFSFile with write mode enabled when no LTX files exist (new database) client := newWriteTestReplicaClient() // Note: No LTX files created - simulating a brand new database // Create temp directory for buffer tmpDir := t.TempDir() bufferPath := tmpDir + "/.litestream-write-buffer" // Create VFSFile with write support - no existing data logger := slog.Default() f := NewVFSFile(client, "new.db", logger) f.writeEnabled = true f.dirty = make(map[uint32]int64) f.syncInterval = 0 f.bufferPath = bufferPath if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Verify it opened successfully as a new database if f.pageSize != DefaultPageSize { t.Errorf("expected page size %d, got %d", DefaultPageSize, f.pageSize) } if f.pos.TXID != 0 { t.Errorf("expected TXID 0 for new database, got %d", f.pos.TXID) } if f.expectedTXID != 0 { t.Errorf("expected expectedTXID 0, got %d", f.expectedTXID) } if f.pendingTXID != 1 { t.Errorf("expected pendingTXID 1, got %d", f.pendingTXID) } if f.commit != 0 { t.Errorf("expected commit 0 for new database, got %d", f.commit) } } func TestVFSFile_NewDatabase_ReadReturnsZeros(t *testing.T) { // Test that reading from a new database returns zeros client := newWriteTestReplicaClient() tmpDir := t.TempDir() bufferPath := tmpDir + "/.litestream-write-buffer" logger := slog.Default() f := NewVFSFile(client, "new.db", logger) f.writeEnabled = true f.dirty = make(map[uint32]int64) f.syncInterval = 0 f.bufferPath = bufferPath if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Read page 1 - should return zeros for new database readBuf := make([]byte, 100) n, err := f.ReadAt(readBuf, 0) if err != nil { t.Fatalf("expected no error reading from new database, got: %v", err) } if n != len(readBuf) { t.Errorf("expected %d bytes, got %d", len(readBuf), n) } // Verify all zeros for i, b := range readBuf { if b != 0 { t.Errorf("expected zero at position %d, got %d", i, b) break } } } func TestVFSFile_NewDatabase_WriteAndSync(t *testing.T) { // Test writing to a new database and syncing to remote client := newWriteTestReplicaClient() tmpDir := t.TempDir() bufferPath := tmpDir + "/.litestream-write-buffer" logger := slog.Default() f := NewVFSFile(client, "new.db", logger) f.writeEnabled = true f.dirty = make(map[uint32]int64) f.syncInterval = 0 f.bufferPath = bufferPath if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Write data to page 1 writeData := []byte("new database content") n, err := f.WriteAt(writeData, 0) if err != nil { t.Fatal(err) } if n != len(writeData) { t.Errorf("expected %d bytes written, got %d", len(writeData), n) } // Verify dirty page exists if len(f.dirty) != 1 { t.Errorf("expected 1 dirty page, got %d", len(f.dirty)) } // Sync to remote if err := f.Sync(0); err != nil { t.Fatal(err) } // Verify TXID advanced if f.expectedTXID != 1 { t.Errorf("expected expectedTXID 1 after sync, got %d", f.expectedTXID) } if f.pendingTXID != 2 { t.Errorf("expected pendingTXID 2 after sync, got %d", f.pendingTXID) } // Verify LTX file was written client.mu.Lock() if len(client.ltxFiles[0]) != 1 { t.Errorf("expected 1 LTX file after sync, got %d", len(client.ltxFiles[0])) } if len(client.ltxFiles[0]) > 0 { info := client.ltxFiles[0][0] if info.MinTXID != 1 || info.MaxTXID != 1 { t.Errorf("expected TXID 1, got min=%d max=%d", info.MinTXID, info.MaxTXID) } } client.mu.Unlock() } func TestVFSFile_NewDatabase_FileSize(t *testing.T) { // Test that FileSize returns 0 for a new empty database client := newWriteTestReplicaClient() tmpDir := t.TempDir() bufferPath := tmpDir + "/.litestream-write-buffer" logger := slog.Default() f := NewVFSFile(client, "new.db", logger) f.writeEnabled = true f.dirty = make(map[uint32]int64) f.syncInterval = 0 f.bufferPath = bufferPath if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // FileSize should be 0 for empty database size, err := f.FileSize() if err != nil { t.Fatal(err) } if size != 0 { t.Errorf("expected size 0 for new database, got %d", size) } // Write a page data := make([]byte, DefaultPageSize) if _, err := f.WriteAt(data, 0); err != nil { t.Fatal(err) } // FileSize should now reflect the dirty page size, err = f.FileSize() if err != nil { t.Fatal(err) } if size != int64(DefaultPageSize) { t.Errorf("expected size %d after write, got %d", DefaultPageSize, size) } } func TestSetWriteEnabled_ReadValue(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) // Test with write disabled logger := slog.Default() f := NewVFSFile(client, "test.db", logger) f.writeEnabled = false if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Read via FileControl (simulates PRAGMA litestream_write_enabled) result, err := f.FileControl(14, "litestream_write_enabled", nil) if err != nil { t.Fatal(err) } if result == nil || *result != "0" { t.Errorf("expected '0' for disabled write support, got %v", result) } } func TestSetWriteEnabled_ReadValueEnabled(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) // Test with write enabled f := setupWriteableVFSFile(t, client) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Read via FileControl result, err := f.FileControl(14, "litestream_write_enabled", nil) if err != nil { t.Fatal(err) } if result == nil || *result != "1" { t.Errorf("expected '1' for enabled write support, got %v", result) } } func TestSetWriteEnabled_DisableSyncsDirtyPages(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) f := setupWriteableVFSFile(t, client) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Write data to create dirty pages writeData := []byte("dirty data") if _, err := f.WriteAt(writeData, 0); err != nil { t.Fatal(err) } if len(f.dirty) == 0 { t.Fatal("expected dirty pages") } // Disable writes via SetWriteEnabled if err := f.SetWriteEnabled(false); err != nil { t.Fatal(err) } // Dirty pages should be synced if len(f.dirty) != 0 { t.Errorf("expected 0 dirty pages after disable, got %d", len(f.dirty)) } // Write support should be disabled if f.writeEnabled { t.Error("expected writeEnabled to be false") } // LTX file should have been written client.mu.Lock() if len(client.ltxFiles[0]) != 2 { t.Errorf("expected 2 LTX files (initial + synced), got %d", len(client.ltxFiles[0])) } client.mu.Unlock() } func TestSetWriteEnabled_DisableWaitsForTransaction(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) f := setupWriteableVFSFile(t, client) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Start a transaction (acquire RESERVED lock) if err := f.Lock(2); err != nil { t.Fatal(err) } // Write some data if _, err := f.WriteAt([]byte("tx data"), 0); err != nil { t.Fatal(err) } // Start disable in a goroutine (it should wait for transaction) done := make(chan error, 1) go func() { done <- f.SetWriteEnabled(false) }() // Wait for SetWriteEnabled to set the disabling flag deadline := time.Now().Add(2 * time.Second) for { f.mu.Lock() disabling := f.disabling f.mu.Unlock() if disabling { break } if time.Now().After(deadline) { t.Fatal("timed out waiting for disabling flag") } time.Sleep(1 * time.Millisecond) } // Write should still be enabled (waiting for transaction) f.mu.Lock() stillEnabled := f.writeEnabled f.mu.Unlock() if !stillEnabled { t.Error("expected writeEnabled to still be true while in transaction") } // End transaction (release lock) if err := f.Unlock(1); err != nil { t.Fatal(err) } // Wait for disable to complete select { case err := <-done: if err != nil { t.Fatalf("SetWriteEnabled failed: %v", err) } case <-time.After(2 * time.Second): t.Fatal("SetWriteEnabled timed out") } // Write should now be disabled f.mu.Lock() enabled := f.writeEnabled f.mu.Unlock() if enabled { t.Error("expected writeEnabled to be false after transaction ended") } } func TestSetWriteEnabled_EnableAfterDisable(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) f := setupWriteableVFSFile(t, client) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Disable writes if err := f.SetWriteEnabled(false); err != nil { t.Fatal(err) } if f.writeEnabled { t.Error("expected writeEnabled to be false") } // Re-enable writes if err := f.SetWriteEnabled(true); err != nil { t.Fatal(err) } if !f.writeEnabled { t.Error("expected writeEnabled to be true") } // Verify we can write again writeData := []byte("after re-enable") if _, err := f.WriteAt(writeData, 0); err != nil { t.Fatal(err) } if len(f.dirty) == 0 { t.Error("expected dirty pages after write") } } func TestSetWriteEnabled_DisableWithTimeout(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) f := setupWriteableVFSFile(t, client) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Start a transaction (acquire RESERVED lock) if err := f.Lock(2); err != nil { t.Fatal(err) } // Write some data if _, err := f.WriteAt([]byte("tx data"), 0); err != nil { t.Fatal(err) } // Try to disable with a short timeout - should fail err := f.SetWriteEnabledWithTimeout(false, 50*time.Millisecond) if err == nil { t.Fatal("expected timeout error") } if !strings.Contains(err.Error(), "timeout waiting for transaction") { t.Errorf("unexpected error: %v", err) } // Write should still be enabled if !f.writeEnabled { t.Error("expected writeEnabled to still be true after timeout") } // End transaction if err := f.Unlock(1); err != nil { t.Fatal(err) } // Now disable should succeed (with or without timeout) if err := f.SetWriteEnabledWithTimeout(false, 1*time.Second); err != nil { t.Fatalf("SetWriteEnabledWithTimeout failed: %v", err) } if f.writeEnabled { t.Error("expected writeEnabled to be false") } } func TestSetWriteEnabled_ColdEnable(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) // Create VFSFile WITHOUT write enabled initially logger := slog.Default() f := NewVFSFile(client, "test.db", logger) f.writeEnabled = false // Note: dirty, bufferPath, etc. are NOT set - simulating cold start if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Verify writes are disabled if f.writeEnabled { t.Error("expected writeEnabled to be false initially") } // Enable writes via SetWriteEnabled (cold enable) if err := f.SetWriteEnabled(true); err != nil { t.Fatal(err) } // Verify writes are now enabled if !f.writeEnabled { t.Error("expected writeEnabled to be true after cold enable") } // Verify buffer was initialized if f.bufferFile == nil { t.Error("expected bufferFile to be initialized") } // Verify dirty map was initialized if f.dirty == nil { t.Error("expected dirty map to be initialized") } // Verify TXID state was initialized if f.pendingTXID == 0 { t.Error("expected pendingTXID to be initialized") } // Verify we can write writeData := []byte("cold enable test") if _, err := f.WriteAt(writeData, 0); err != nil { t.Fatal(err) } if len(f.dirty) == 0 { t.Error("expected dirty pages after write") } } func TestSetWriteEnabled_NoOpWhenAlreadyInState(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) f := setupWriteableVFSFile(t, client) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Enable when already enabled should be no-op if err := f.SetWriteEnabled(true); err != nil { t.Fatal(err) } if !f.writeEnabled { t.Error("expected writeEnabled to remain true") } // Disable if err := f.SetWriteEnabled(false); err != nil { t.Fatal(err) } // Disable when already disabled should be no-op if err := f.SetWriteEnabled(false); err != nil { t.Fatal(err) } if f.writeEnabled { t.Error("expected writeEnabled to remain false") } } func TestSetWriteEnabled_FileControlWrite(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) f := setupWriteableVFSFile(t, client) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Disable via FileControl (PRAGMA litestream_write_enabled = 0) value := "0" _, err := f.FileControl(14, "litestream_write_enabled", &value) if err != nil { t.Fatal(err) } if f.writeEnabled { t.Error("expected writeEnabled to be false after PRAGMA = 0") } // Enable via FileControl (PRAGMA litestream_write_enabled = 1) value = "1" _, err = f.FileControl(14, "litestream_write_enabled", &value) if err != nil { t.Fatal(err) } if !f.writeEnabled { t.Error("expected writeEnabled to be true after PRAGMA = 1") } // Test alternate values value = "off" _, err = f.FileControl(14, "litestream_write_enabled", &value) if err != nil { t.Fatal(err) } if f.writeEnabled { t.Error("expected writeEnabled to be false after PRAGMA = off") } value = "on" _, err = f.FileControl(14, "litestream_write_enabled", &value) if err != nil { t.Fatal(err) } if !f.writeEnabled { t.Error("expected writeEnabled to be true after PRAGMA = on") } value = "false" _, err = f.FileControl(14, "litestream_write_enabled", &value) if err != nil { t.Fatal(err) } if f.writeEnabled { t.Error("expected writeEnabled to be false after PRAGMA = false") } value = "true" _, err = f.FileControl(14, "litestream_write_enabled", &value) if err != nil { t.Fatal(err) } if !f.writeEnabled { t.Error("expected writeEnabled to be true after PRAGMA = true") } } func TestSetWriteEnabled_InvalidValue(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) f := setupWriteableVFSFile(t, client) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Invalid value should return error value := "invalid" _, err := f.FileControl(14, "litestream_write_enabled", &value) if err == nil { t.Error("expected error for invalid value") } if err.Error() != "invalid value for litestream_write_enabled: invalid (use 0 or 1)" { t.Errorf("unexpected error message: %v", err) } } // failingWriteClient wraps writeTestReplicaClient to fail writes after a certain count. type failingWriteClient struct { *writeTestReplicaClient failAfter int writeCount int } func newFailingWriteClient(failAfter int) *failingWriteClient { return &failingWriteClient{ writeTestReplicaClient: newWriteTestReplicaClient(), failAfter: failAfter, } } func (c *failingWriteClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { c.mu.Lock() c.writeCount++ count := c.writeCount c.mu.Unlock() if count > c.failAfter { return nil, errors.New("simulated write failure") } return c.writeTestReplicaClient.WriteLTXFile(ctx, level, minTXID, maxTXID, r) } func TestSetWriteEnabled_SyncFailureKeepsWritesEnabled(t *testing.T) { // Use a client that fails on the second write attempt (first is from setup/initial sync) client := newFailingWriteClient(0) // Fail on first write pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client.writeTestReplicaClient, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) logger := slog.Default() f := NewVFSFile(client, "test.db", logger) f.writeEnabled = true f.dirty = make(map[uint32]int64) f.syncInterval = 0 // Create a temporary buffer file tmpFile, err := os.CreateTemp("", "litestream-test-buffer-*") if err != nil { t.Fatal(err) } f.bufferFile = tmpFile f.bufferPath = tmpFile.Name() f.bufferNextOff = 0 t.Cleanup(func() { if f.bufferFile != nil { f.bufferFile.Close() } os.Remove(f.bufferPath) }) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Write data to create dirty pages writeData := []byte("dirty data") if _, err := f.WriteAt(writeData, 0); err != nil { t.Fatal(err) } if len(f.dirty) == 0 { t.Fatal("expected dirty pages") } // Try to disable writes - should fail because sync fails err = f.SetWriteEnabled(false) if err == nil { t.Fatal("expected error from sync failure") } if !strings.Contains(err.Error(), "sync before disable") { t.Errorf("unexpected error: %v", err) } // Write support should still be enabled because sync failed if !f.writeEnabled { t.Error("expected writeEnabled to remain true after sync failure") } // Dirty pages should still exist if len(f.dirty) == 0 { t.Error("expected dirty pages to remain after sync failure") } } func TestSetWriteEnabled_DisablingPreventsNewTransactions(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) f := setupWriteableVFSFile(t, client) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Start a transaction (acquire RESERVED lock) if err := f.Lock(2); err != nil { t.Fatal(err) } // Start disable in a goroutine disableDone := make(chan error, 1) go func() { disableDone <- f.SetWriteEnabledWithTimeout(false, 2*time.Second) }() // Wait for SetWriteEnabled to set the disabling flag deadline := time.Now().Add(2 * time.Second) for { f.mu.Lock() disabling := f.disabling f.mu.Unlock() if disabling { break } if time.Now().After(deadline) { t.Fatal("timed out waiting for disabling flag") } time.Sleep(1 * time.Millisecond) } // End the first transaction if err := f.Unlock(1); err != nil { t.Fatal(err) } // Wait for disable to complete select { case err := <-disableDone: if err != nil { t.Fatalf("SetWriteEnabled failed: %v", err) } case <-time.After(3 * time.Second): t.Fatal("SetWriteEnabled timed out") } // Verify disabling flag is cleared f.mu.Lock() disabling := f.disabling f.mu.Unlock() if disabling { t.Error("expected disabling flag to be false after completion") } // Verify writes are disabled f.mu.Lock() enabled := f.writeEnabled f.mu.Unlock() if enabled { t.Error("expected writeEnabled to be false") } } func TestSetWriteEnabled_ConcurrentEnableDisable(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) f := setupWriteableVFSFile(t, client) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Run multiple concurrent enable/disable operations var wg sync.WaitGroup errCh := make(chan error, 20) for i := 0; i < 10; i++ { wg.Add(2) go func() { defer wg.Done() if err := f.SetWriteEnabled(true); err != nil { errCh <- err } }() go func() { defer wg.Done() if err := f.SetWriteEnabled(false); err != nil { errCh <- err } }() } wg.Wait() close(errCh) // Check for errors for err := range errCh { t.Errorf("concurrent operation failed: %v", err) } // The final state should be valid (either enabled or disabled) f.mu.Lock() enabled := f.writeEnabled disabling := f.disabling f.mu.Unlock() // disabling should always be false when no operation is in progress if disabling { t.Error("expected disabling to be false after all operations complete") } t.Logf("Final writeEnabled state: %v", enabled) } func TestLock_BlocksDuringDisable(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) f := setupWriteableVFSFile(t, client) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Start a transaction (acquire RESERVED lock) if err := f.Lock(2); err != nil { t.Fatal(err) } // Write some data so there's something to sync if _, err := f.WriteAt([]byte("tx data"), 0); err != nil { t.Fatal(err) } // Start disable in a goroutine - it will wait for the transaction disableDone := make(chan error, 1) go func() { disableDone <- f.SetWriteEnabled(false) }() // Wait for SetWriteEnabled to set the disabling flag deadline := time.Now().Add(2 * time.Second) for { f.mu.Lock() disabling := f.disabling f.mu.Unlock() if disabling { break } if time.Now().After(deadline) { t.Fatal("timed out waiting for disabling flag") } time.Sleep(1 * time.Millisecond) } // End transaction to let disable proceed, then immediately try to // acquire RESERVED lock again - it should block until disable completes lockErrCh := make(chan error, 1) lockDone := make(chan struct{}) go func() { defer close(lockDone) if err := f.Unlock(1); err != nil { lockErrCh <- fmt.Errorf("unlock: %w", err) return } // Lock() should block while disabling is true, then fail because // writeEnabled will be false after disable completes lockErrCh <- f.Lock(2) }() // Wait for disable to complete select { case err := <-disableDone: if err != nil { t.Fatalf("SetWriteEnabled failed: %v", err) } case <-time.After(3 * time.Second): t.Fatal("SetWriteEnabled timed out") } select { case <-lockDone: case <-time.After(3 * time.Second): t.Fatal("Lock() timed out") } // Lock should have returned an error since writes are now disabled if err := <-lockErrCh; err == nil { t.Error("expected Lock(RESERVED) to fail when writes are disabled") } // Verify writeEnabled is now false f.mu.Lock() enabled := f.writeEnabled inTx := f.inTransaction f.mu.Unlock() if enabled { t.Error("expected writeEnabled to be false after disable completed") } if inTx { t.Error("expected inTransaction to be false when writeEnabled is false") } } func TestLock_BlocksDuringDisable_MultipleWaiters(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) f := setupWriteableVFSFile(t, client) if err := f.Open(); err != nil { t.Fatal(err) } defer f.Close() // Start a transaction (acquire RESERVED lock) if err := f.Lock(2); err != nil { t.Fatal(err) } // Write some data if _, err := f.WriteAt([]byte("tx data"), 0); err != nil { t.Fatal(err) } // Start disable in a goroutine disableDone := make(chan error, 1) go func() { disableDone <- f.SetWriteEnabled(false) }() // Wait for SetWriteEnabled to set the disabling flag deadline := time.Now().Add(2 * time.Second) for { f.mu.Lock() disabling := f.disabling f.mu.Unlock() if disabling { break } if time.Now().After(deadline) { t.Fatal("timed out waiting for disabling flag") } time.Sleep(1 * time.Millisecond) } // Simulate multiple waiters trying to acquire RESERVED lock. // They will block on cond.Wait() while disabling is true, then // fail with read-only error once disable completes. const numWaiters = 3 var wg sync.WaitGroup errCh := make(chan error, numWaiters) started := make(chan struct{}, numWaiters) for i := 0; i < numWaiters; i++ { wg.Add(1) go func() { defer wg.Done() started <- struct{}{} errCh <- f.Lock(2) }() } // Wait for all goroutines to start, then verify none have completed yet // (they should be blocked in cond.Wait() while disabling is true). for i := 0; i < numWaiters; i++ { <-started } time.Sleep(10 * time.Millisecond) if len(errCh) > 0 { t.Fatal("expected all Lock() calls to be blocked during disable, but some completed early") } // End the original transaction - this will trigger the disable to complete if err := f.Unlock(1); err != nil { t.Fatal(err) } // Wait for disable to complete select { case err := <-disableDone: if err != nil { t.Fatalf("SetWriteEnabled failed: %v", err) } case <-time.After(3 * time.Second): t.Fatal("SetWriteEnabled timed out") } // Wait for all Lock() calls to complete wg.Wait() close(errCh) // All Lock() calls should have returned errors (writes now disabled) for err := range errCh { if err == nil { t.Error("expected Lock(RESERVED) to fail when writes are disabled") } } // Verify writeEnabled is now false f.mu.Lock() enabled := f.writeEnabled f.mu.Unlock() if enabled { t.Error("expected writeEnabled to be false") } } func openWriteVFSFile(t *testing.T, vfs *VFS) *VFSFile { t.Helper() file, _, err := vfs.openMainDB("test.db", sqlite3vfs.OpenMainDB|sqlite3vfs.OpenReadWrite) if err != nil { t.Fatal(err) } f := file.(*VFSFile) t.Cleanup(func() { f.Close() }) return f } func TestVFS_MultipleConnections_NoFalseConflict(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) v := NewVFS(client, slog.Default()) v.WriteEnabled = true v.WriteSyncInterval = 0 f1 := openWriteVFSFile(t, v) f2 := openWriteVFSFile(t, v) // Connection 1: acquire RESERVED, write, sync if err := f1.Lock(sqlite3vfs.LockReserved); err != nil { t.Fatal(err) } if _, err := f1.WriteAt([]byte("data1"), 0); err != nil { t.Fatal(err) } if err := f1.Unlock(sqlite3vfs.LockShared); err != nil { t.Fatal(err) } if err := f1.Sync(0); err != nil { t.Fatalf("connection 1 sync failed: %v", err) } if f1.expectedTXID != 2 { t.Fatalf("expected f1.expectedTXID=2, got %d", f1.expectedTXID) } // Connection 2: acquire RESERVED (should refresh TXID), write, sync if err := f2.Lock(sqlite3vfs.LockReserved); err != nil { t.Fatal(err) } if f2.expectedTXID != 2 { t.Fatalf("expected f2.expectedTXID=2 after RESERVED lock refresh, got %d", f2.expectedTXID) } if _, err := f2.WriteAt([]byte("data2"), 0); err != nil { t.Fatal(err) } if err := f2.Unlock(sqlite3vfs.LockShared); err != nil { t.Fatal(err) } if err := f2.Sync(0); err != nil { t.Fatalf("connection 2 sync failed (false conflict): %v", err) } if f2.expectedTXID != 3 { t.Fatalf("expected f2.expectedTXID=3, got %d", f2.expectedTXID) } } func TestVFS_WriteLockBlocksConcurrentWriters(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) v := NewVFS(client, slog.Default()) v.WriteEnabled = true f1 := openWriteVFSFile(t, v) f2 := openWriteVFSFile(t, v) // f1 acquires RESERVED if err := f1.Lock(sqlite3vfs.LockReserved); err != nil { t.Fatal(err) } // f2 attempts RESERVED - should get BusyError err := f2.Lock(sqlite3vfs.LockReserved) if !errors.Is(err, sqlite3vfs.BusyError) { t.Fatalf("expected BusyError, got %v", err) } // f1 releases if err := f1.Unlock(sqlite3vfs.LockShared); err != nil { t.Fatal(err) } // f2 should now succeed if err := f2.Lock(sqlite3vfs.LockReserved); err != nil { t.Fatalf("expected f2 to acquire RESERVED after f1 released, got %v", err) } if err := f2.Unlock(sqlite3vfs.LockShared); err != nil { t.Fatal(err) } } func TestVFS_ConcurrentOpenAllSucceed(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) v := NewVFS(client, slog.Default()) v.WriteEnabled = true const n = 10 var wg sync.WaitGroup errs := make([]error, n) files := make([]sqlite3vfs.File, n) for i := range n { wg.Add(1) go func(idx int) { defer wg.Done() f, _, err := v.openMainDB("test.db", sqlite3vfs.OpenMainDB|sqlite3vfs.OpenReadWrite) errs[idx] = err files[idx] = f }(i) } wg.Wait() var opened int for i, err := range errs { if err != nil { t.Errorf("connection %d failed to open: %v", i, err) } else { opened++ files[i].(io.Closer).Close() } } if opened != n { t.Errorf("expected all %d connections to open, got %d", n, opened) } } func TestVFS_UniqueBufferPaths(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) v := NewVFS(client, slog.Default()) v.WriteEnabled = true f1 := openWriteVFSFile(t, v) f2 := openWriteVFSFile(t, v) if f1.bufferPath == f2.bufferPath { t.Errorf("buffer paths should be unique: both are %q", f1.bufferPath) } } func TestVFS_RealConflict_StillDetected(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) v := NewVFS(client, slog.Default()) v.WriteEnabled = true v.WriteSyncInterval = 0 f1 := openWriteVFSFile(t, v) // Write dirty data if _, err := f1.WriteAt([]byte("data"), 0); err != nil { t.Fatal(err) } // Simulate external writer advancing remote createTestLTXFile(t, client, 2, pageSize, 1, map[uint32][]byte{1: initialPage}) // Sync should fail with real conflict err := f1.Sync(0) if err == nil { t.Fatal("expected conflict error from external writer") } if !errors.Is(err, ErrConflict) { t.Fatalf("expected ErrConflict, got: %v", err) } } func TestVFS_CloseReleasesWriteSlot(t *testing.T) { client := newWriteTestReplicaClient() pageSize := uint32(4096) initialPage := make([]byte, pageSize) createTestLTXFile(t, client, 1, pageSize, 1, map[uint32][]byte{1: initialPage}) v := NewVFS(client, slog.Default()) v.WriteEnabled = true // Open and acquire RESERVED file1, _, err := v.openMainDB("test.db", sqlite3vfs.OpenMainDB|sqlite3vfs.OpenReadWrite) if err != nil { t.Fatal(err) } f1 := file1.(*VFSFile) if err := f1.Lock(sqlite3vfs.LockReserved); err != nil { t.Fatal(err) } // Close f1 (should release write slot) f1.Close() // New connection should be able to acquire RESERVED f2 := openWriteVFSFile(t, v) if err := f2.Lock(sqlite3vfs.LockReserved); err != nil { t.Fatalf("expected f2 to acquire RESERVED after f1 closed, got %v", err) } if err := f2.Unlock(sqlite3vfs.LockShared); err != nil { t.Fatal(err) } } ================================================ FILE: wal_reader.go ================================================ package litestream import ( "context" "encoding/binary" "errors" "fmt" "io" "log/slog" "github.com/benbjohnson/litestream/internal" ) // WALReader wraps an io.Reader and parses SQLite WAL frames. // // This reader verifies the salt & checksum integrity while it reads. It does // not enforce transaction boundaries (i.e. it may return uncommitted frames). // It is the responsibility of the caller to handle this. type WALReader struct { r io.ReaderAt frameN int bo binary.ByteOrder pageSize uint32 seq uint32 salt1, salt2 uint32 chksum1, chksum2 uint32 logger *slog.Logger } // NewWALReader returns a new instance of WALReader. func NewWALReader(rd io.ReaderAt, logger *slog.Logger) (*WALReader, error) { r := &WALReader{r: rd, logger: logger} if err := r.readHeader(); err != nil { return nil, err } return r, nil } // NewWALReaderWithOffset returns a new instance of WALReader at a given offset. // Salt must match or else no frames will be returned. Checksum calculated from // from previous page. func NewWALReaderWithOffset(ctx context.Context, rd io.ReaderAt, offset int64, salt1, salt2 uint32, logger *slog.Logger) (*WALReader, error) { // Ensure we are not starting on the first page since we need to read the previous. if offset <= WALHeaderSize { return nil, fmt.Errorf("offset (%d) must be greater than the wal header size (%d)", offset, WALHeaderSize) } r := &WALReader{r: rd, logger: logger} // Read header to determine page size & byte order. if err := r.readHeader(); err != nil { return nil, fmt.Errorf("read header: %w", err) } // Load in salt in case the beginning of the file has been overwritten. r.salt1, r.salt2 = salt1, salt2 // Ensure offset is positioned on a frame start. frameSize := int64(r.pageSize + WALFrameHeaderSize) if (offset-WALHeaderSize)%frameSize != 0 { return nil, fmt.Errorf("unaligned wal offset %d for page size %d", offset, r.pageSize) } r.frameN = int((offset - WALHeaderSize) / frameSize) // Read previous page to load checksum. r.frameN-- if _, _, err := r.readFrame(ctx, make([]byte, r.pageSize), false); err != nil { return nil, &PrevFrameMismatchError{Err: err} } return r, nil } // PageSize returns the page size from the header. Must call ReadHeader() first. func (r *WALReader) PageSize() uint32 { return r.pageSize } // Offset returns the file offset of the last read frame. // Returns zero if no frames have been read. func (r *WALReader) Offset() int64 { if r.frameN == 0 { return 0 } return WALHeaderSize + ((int64(r.frameN) - 1) * (WALFrameHeaderSize + int64(r.pageSize))) } // readHeader reads the WAL header into the reader. Returns io.EOF if WAL is invalid. func (r *WALReader) readHeader() error { // If we have a partial WAL, then mark WAL as done. hdr := make([]byte, WALHeaderSize) if n, err := r.r.ReadAt(hdr, 0); n < len(hdr) { return io.EOF } else if err != nil { return err } // Determine byte order of checksums. switch magic := binary.BigEndian.Uint32(hdr[0:]); magic { case 0x377f0682: r.bo = binary.LittleEndian case 0x377f0683: r.bo = binary.BigEndian default: return fmt.Errorf("invalid wal header magic: %x", magic) } // If the header checksum doesn't match then we may have failed with // a partial WAL header write during checkpointing. chksum1 := binary.BigEndian.Uint32(hdr[24:]) chksum2 := binary.BigEndian.Uint32(hdr[28:]) if v0, v1 := WALChecksum(r.bo, 0, 0, hdr[:24]); v0 != chksum1 || v1 != chksum2 { return io.EOF } // Verify version is correct. if version := binary.BigEndian.Uint32(hdr[4:]); version != 3007000 { return fmt.Errorf("unsupported wal version: %d", version) } r.pageSize = binary.BigEndian.Uint32(hdr[8:]) r.seq = binary.BigEndian.Uint32(hdr[12:]) r.salt1 = binary.BigEndian.Uint32(hdr[16:]) r.salt2 = binary.BigEndian.Uint32(hdr[20:]) r.chksum1, r.chksum2 = chksum1, chksum2 return nil } // ReadFrame reads the next frame from the WAL and returns the page number. // Returns io.EOF at the end of the valid WAL. func (r *WALReader) ReadFrame(ctx context.Context, data []byte) (pgno, commit uint32, err error) { return r.readFrame(ctx, data, true) } func (r *WALReader) readFrame(_ context.Context, data []byte, verifyChecksum bool) (pgno, commit uint32, err error) { if len(data) != int(r.pageSize) { return 0, 0, fmt.Errorf("WALReader.ReadFrame(): buffer size (%d) must match page size (%d)", len(data), r.pageSize) } frameSize := r.pageSize + WALFrameHeaderSize offset := WALHeaderSize + (int64(r.frameN) * int64(frameSize)) // Read WAL frame header. hdr := make([]byte, WALFrameHeaderSize) if n, err := r.r.ReadAt(hdr, offset); n != len(hdr) { return 0, 0, io.EOF } else if err != nil { return 0, 0, err } // Read WAL page data. if n, err := r.r.ReadAt(data, offset+WALFrameHeaderSize); n != len(data) { return 0, 0, io.EOF } else if err != nil { return 0, 0, err } // Verify salt matches the salt in the header. salt1 := binary.BigEndian.Uint32(hdr[8:]) salt2 := binary.BigEndian.Uint32(hdr[12:]) if r.salt1 != salt1 || r.salt2 != salt2 { return 0, 0, io.EOF } // Verify the checksum is valid. If checksum verification is disabled, it // is because we are jumping to an offset and not checksumming from the beginning. chksum1 := binary.BigEndian.Uint32(hdr[16:]) chksum2 := binary.BigEndian.Uint32(hdr[20:]) if verifyChecksum { r.chksum1, r.chksum2 = WALChecksum(r.bo, r.chksum1, r.chksum2, hdr[:8]) // frame header r.chksum1, r.chksum2 = WALChecksum(r.bo, r.chksum1, r.chksum2, data) // frame data if r.chksum1 != chksum1 || r.chksum2 != chksum2 { return 0, 0, io.EOF } } else { r.chksum1, r.chksum2 = chksum1, chksum2 } pgno = binary.BigEndian.Uint32(hdr[0:]) commit = binary.BigEndian.Uint32(hdr[4:]) r.frameN++ return pgno, commit, nil } // PageMap reads all committed frames until the end of the file and returns a // map of pgno to offset of the latest version of each page. Also returns the // max offset of the wal segment read, and the final database size, in pages. func (r *WALReader) PageMap(ctx context.Context) (m map[uint32]int64, maxOffset int64, commit uint32, err error) { m = make(map[uint32]int64) txMap := make(map[uint32]int64) data := make([]byte, r.pageSize) for i := 0; ; i++ { pgno, fcommit, err := r.ReadFrame(ctx, data) if errors.Is(err, io.EOF) { break } else if err != nil { return nil, 0, 0, err } // Update latest offset for the page for this transaction. // Pages should not be saved to full map until we know txn is committed. offset := r.Offset() txMap[pgno] = offset // For commit records, transfer offsets to full map and update db size. if fcommit != 0 { for pgno, offset := range txMap { m[pgno] = offset } commit = fcommit } } // Remove pages that exceed the final commit size. This can occur when the // database shrinks (e.g., via VACUUM) between transactions in the WAL. for pgno := range m { if pgno > commit { delete(m, pgno) } } // If full transactions available, return the original offset. if len(m) == 0 { return m, 0, 0, nil } // Compute the highest page offsets. var end int64 for _, offset := range m { if end == 0 || offset > end { end = offset } } // Extend to the end of the last frame read. end += WALFrameHeaderSize + int64(r.pageSize) r.logger.Log(ctx, internal.LevelTrace, "page map complete", "n", len(m), "end", end, "commit", commit) return m, end, commit, nil } // FrameSaltsUntil returns a set of all unique frame salts in the WAL file. func (r *WALReader) FrameSaltsUntil(ctx context.Context, until [2]uint32) (map[[2]uint32]struct{}, error) { m := make(map[[2]uint32]struct{}) for offset := int64(WALHeaderSize); ; offset += int64(WALFrameHeaderSize + r.pageSize) { hdr := make([]byte, WALFrameHeaderSize) if n, err := r.r.ReadAt(hdr, offset); n != len(hdr) { break } else if err != nil { return nil, err } salt1 := binary.BigEndian.Uint32(hdr[8:]) salt2 := binary.BigEndian.Uint32(hdr[12:]) // Track unique salts. m[[2]uint32{salt1, salt2}] = struct{}{} // Only read salts until the last one we expect. if salt1 == until[0] && salt2 == until[1] { break } } return m, nil } // WALChecksum computes a running SQLite WAL checksum over a byte slice. func WALChecksum(bo binary.ByteOrder, s0, s1 uint32, b []byte) (uint32, uint32) { assert(len(b)%8 == 0, "misaligned checksum byte slice") // Iterate over 8-byte units and compute checksum. for i := 0; i < len(b); i += 8 { s0 += bo.Uint32(b[i:]) + s1 s1 += bo.Uint32(b[i+4:]) + s0 } return s0, s1 } type PrevFrameMismatchError struct { Err error } func (e *PrevFrameMismatchError) Error() string { return fmt.Sprintf("prev frame mismatch: %s", e.Err) } func (e *PrevFrameMismatchError) Unwrap() error { return e.Err } ================================================ FILE: wal_reader_test.go ================================================ package litestream_test import ( "bytes" "context" "errors" "io" "log/slog" "os" "testing" "github.com/benbjohnson/litestream" ) func TestWALReader(t *testing.T) { t.Run("OK", func(t *testing.T) { buf := make([]byte, 4096) b, err := os.ReadFile("testdata/wal-reader/ok/wal") if err != nil { t.Fatal(err) } // Initialize reader with header info. r, err := litestream.NewWALReader(bytes.NewReader(b), slog.Default()) if err != nil { t.Fatal(err) } else if got, want := r.PageSize(), uint32(4096); got != want { t.Fatalf("PageSize()=%d, want %d", got, want) } else if got, want := r.Offset(), int64(0); got != want { t.Fatalf("Offset()=%d, want %d", got, want) } // Read first frame. if pgno, commit, err := r.ReadFrame(context.Background(), buf); err != nil { t.Fatal(err) } else if got, want := pgno, uint32(1); got != want { t.Fatalf("pgno=%d, want %d", got, want) } else if got, want := commit, uint32(0); got != want { t.Fatalf("commit=%d, want %d", got, want) } else if !bytes.Equal(buf, b[56:4152]) { t.Fatal("page data mismatch") } else if got, want := r.Offset(), int64(32); got != want { t.Fatalf("Offset()=%d, want %d", got, want) } // Read second frame. End of transaction. if pgno, commit, err := r.ReadFrame(context.Background(), buf); err != nil { t.Fatal(err) } else if got, want := pgno, uint32(2); got != want { t.Fatalf("pgno=%d, want %d", got, want) } else if got, want := commit, uint32(2); got != want { t.Fatalf("commit=%d, want %d", got, want) } else if !bytes.Equal(buf, b[4176:8272]) { t.Fatal("page data mismatch") } else if got, want := r.Offset(), int64(4152); got != want { t.Fatalf("Offset()=%d, want %d", got, want) } // Read third frame. if pgno, commit, err := r.ReadFrame(context.Background(), buf); err != nil { t.Fatal(err) } else if got, want := pgno, uint32(2); got != want { t.Fatalf("pgno=%d, want %d", got, want) } else if got, want := commit, uint32(2); got != want { t.Fatalf("commit=%d, want %d", got, want) } else if !bytes.Equal(buf, b[8296:12392]) { t.Fatal("page data mismatch") } else if got, want := r.Offset(), int64(8272); got != want { t.Fatalf("Offset()=%d, want %d", got, want) } if _, _, err := r.ReadFrame(context.Background(), buf); !errors.Is(err, io.EOF) { t.Fatalf("unexpected error: %s", err) } }) t.Run("SaltMismatch", func(t *testing.T) { buf := make([]byte, 4096) b, err := os.ReadFile("testdata/wal-reader/salt-mismatch/wal") if err != nil { t.Fatal(err) } // Initialize reader with header info. r, err := litestream.NewWALReader(bytes.NewReader(b), slog.Default()) if err != nil { t.Fatal(err) } else if got, want := r.PageSize(), uint32(4096); got != want { t.Fatalf("PageSize()=%d, want %d", got, want) } else if got, want := r.Offset(), int64(0); got != want { t.Fatalf("Offset()=%d, want %d", got, want) } // Read first frame. if pgno, commit, err := r.ReadFrame(context.Background(), buf); err != nil { t.Fatal(err) } else if got, want := pgno, uint32(1); got != want { t.Fatalf("pgno=%d, want %d", got, want) } else if got, want := commit, uint32(0); got != want { t.Fatalf("commit=%d, want %d", got, want) } else if !bytes.Equal(buf, b[56:4152]) { t.Fatal("page data mismatch") } // Read second frame. Salt has been altered so it doesn't match header. if _, _, err := r.ReadFrame(context.Background(), buf); !errors.Is(err, io.EOF) { t.Fatalf("unexpected error: %s", err) } }) t.Run("FrameChecksumMismatch", func(t *testing.T) { buf := make([]byte, 4096) b, err := os.ReadFile("testdata/wal-reader/frame-checksum-mismatch/wal") if err != nil { t.Fatal(err) } // Initialize reader with header info. r, err := litestream.NewWALReader(bytes.NewReader(b), slog.Default()) if err != nil { t.Fatal(err) } else if got, want := r.PageSize(), uint32(4096); got != want { t.Fatalf("PageSize()=%d, want %d", got, want) } else if got, want := r.Offset(), int64(0); got != want { t.Fatalf("Offset()=%d, want %d", got, want) } // Read first frame. if pgno, commit, err := r.ReadFrame(context.Background(), buf); err != nil { t.Fatal(err) } else if got, want := pgno, uint32(1); got != want { t.Fatalf("pgno=%d, want %d", got, want) } else if got, want := commit, uint32(0); got != want { t.Fatalf("commit=%d, want %d", got, want) } else if !bytes.Equal(buf, b[56:4152]) { t.Fatal("page data mismatch") } // Read second frame. Checksum has been altered so it doesn't match. if _, _, err := r.ReadFrame(context.Background(), buf); !errors.Is(err, io.EOF) { t.Fatalf("unexpected error: %s", err) } }) t.Run("ZeroLength", func(t *testing.T) { _, err := litestream.NewWALReader(bytes.NewReader(nil), slog.Default()) if !errors.Is(err, io.EOF) { t.Fatalf("unexpected error: %#v", err) } }) t.Run("PartialHeader", func(t *testing.T) { _, err := litestream.NewWALReader(bytes.NewReader(make([]byte, 10)), slog.Default()) if !errors.Is(err, io.EOF) { t.Fatalf("unexpected error: %#v", err) } }) t.Run("BadMagic", func(t *testing.T) { _, err := litestream.NewWALReader(bytes.NewReader(make([]byte, 32)), slog.Default()) if err == nil || err.Error() != `invalid wal header magic: 0` { t.Fatalf("unexpected error: %#v", err) } }) t.Run("BadHeaderChecksum", func(t *testing.T) { data := []byte{ 0x37, 0x7f, 0x06, 0x83, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} _, err := litestream.NewWALReader(bytes.NewReader(data), slog.Default()) if !errors.Is(err, io.EOF) { t.Fatalf("unexpected error: %#v", err) } }) t.Run("BadHeaderVersion", func(t *testing.T) { data := []byte{ 0x37, 0x7f, 0x06, 0x83, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x7b, 0x20, 0x92, 0xbb, 0xf8, 0x34, 0x1d} _, err := litestream.NewWALReader(bytes.NewReader(data), slog.Default()) if err == nil || err.Error() != `unsupported wal version: 1` { t.Fatalf("unexpected error: %#v", err) } }) t.Run("ErrBufferSize", func(t *testing.T) { b, err := os.ReadFile("testdata/wal-reader/ok/wal") if err != nil { t.Fatal(err) } // Initialize reader with header info. r, err := litestream.NewWALReader(bytes.NewReader(b), slog.Default()) if err != nil { t.Fatal(err) } if _, _, err := r.ReadFrame(context.Background(), make([]byte, 512)); err == nil || err.Error() != `WALReader.ReadFrame(): buffer size (512) must match page size (4096)` { t.Fatalf("unexpected error: %#v", err) } }) t.Run("ErrPartialFrameHeader", func(t *testing.T) { b, err := os.ReadFile("testdata/wal-reader/ok/wal") if err != nil { t.Fatal(err) } r, err := litestream.NewWALReader(bytes.NewReader(b[:40]), slog.Default()) if err != nil { t.Fatal(err) } else if _, _, err := r.ReadFrame(context.Background(), make([]byte, 4096)); !errors.Is(err, io.EOF) { t.Fatalf("unexpected error: %#v", err) } }) t.Run("ErrFrameHeaderOnly", func(t *testing.T) { b, err := os.ReadFile("testdata/wal-reader/ok/wal") if err != nil { t.Fatal(err) } r, err := litestream.NewWALReader(bytes.NewReader(b[:56]), slog.Default()) if err != nil { t.Fatal(err) } else if _, _, err := r.ReadFrame(context.Background(), make([]byte, 4096)); !errors.Is(err, io.EOF) { t.Fatalf("unexpected error: %#v", err) } }) t.Run("ErrPartialFrameData", func(t *testing.T) { b, err := os.ReadFile("testdata/wal-reader/ok/wal") if err != nil { t.Fatal(err) } r, err := litestream.NewWALReader(bytes.NewReader(b[:1000]), slog.Default()) if err != nil { t.Fatal(err) } else if _, _, err := r.ReadFrame(context.Background(), make([]byte, 4096)); !errors.Is(err, io.EOF) { t.Fatalf("unexpected error: %#v", err) } }) } func TestWALReader_FrameSaltsUntil(t *testing.T) { t.Run("OK", func(t *testing.T) { b, err := os.ReadFile("testdata/wal-reader/frame-salts/wal") if err != nil { t.Fatal(err) } r, err := litestream.NewWALReader(bytes.NewReader(b), slog.Default()) if err != nil { t.Fatal(err) } m, err := r.FrameSaltsUntil(context.Background(), [2]uint32{0x00000000, 0x00000000}) if err != nil { t.Fatal(err) } if got, want := len(m), 3; got != want { t.Fatalf("len(m)=%d, want %d", got, want) } if _, ok := m[[2]uint32{0x1b9a294b, 0x37f91916}]; !ok { t.Fatalf("salt 0 not found") } if _, ok := m[[2]uint32{0x1b9a294a, 0x031f195e}]; !ok { t.Fatalf("salt 1 not found") } if _, ok := m[[2]uint32{0x1b9a2949, 0x13b3dd67}]; !ok { t.Fatalf("salt 2 not found") } }) } ================================================ FILE: webdav/replica_client.go ================================================ package webdav import ( "bytes" "context" "fmt" "io" "log/slog" "net/url" "os" "path" "sort" "sync" "time" "github.com/studio-b12/gowebdav" "github.com/superfly/ltx" "github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream/internal" ) func init() { litestream.RegisterReplicaClientFactory("webdav", NewReplicaClientFromURL) litestream.RegisterReplicaClientFactory("webdavs", NewReplicaClientFromURL) } const ReplicaClientType = "webdav" const ( DefaultTimeout = 30 * time.Second ) var _ litestream.ReplicaClient = (*ReplicaClient)(nil) type ReplicaClient struct { mu sync.Mutex client *gowebdav.Client logger *slog.Logger URL string Username string Password string Path string Timeout time.Duration } func NewReplicaClient() *ReplicaClient { return &ReplicaClient{ logger: slog.Default().WithGroup(ReplicaClientType), Timeout: DefaultTimeout, } } func (c *ReplicaClient) SetLogger(logger *slog.Logger) { c.logger = logger.WithGroup(ReplicaClientType) } // NewReplicaClientFromURL creates a new ReplicaClient from URL components. // This is used by the replica client factory registration. // URL format: webdav://[user[:password]@]host[:port]/path or webdavs://... (for HTTPS) func NewReplicaClientFromURL(scheme, host, urlPath string, query url.Values, userinfo *url.Userinfo) (litestream.ReplicaClient, error) { client := NewReplicaClient() // Determine HTTP or HTTPS based on scheme httpScheme := "http" if scheme == "webdavs" { httpScheme = "https" } // Extract credentials from userinfo if userinfo != nil { client.Username = userinfo.Username() client.Password, _ = userinfo.Password() } if host == "" { return nil, fmt.Errorf("host required for webdav replica URL") } client.URL = fmt.Sprintf("%s://%s", httpScheme, host) client.Path = urlPath return client, nil } func (c *ReplicaClient) Type() string { return ReplicaClientType } func (c *ReplicaClient) Init(ctx context.Context) error { _, err := c.init(ctx) return err } // init initializes the connection and returns the WebDAV client. func (c *ReplicaClient) init(ctx context.Context) (_ *gowebdav.Client, err error) { c.mu.Lock() defer c.mu.Unlock() if c.client != nil { return c.client, nil } if c.URL == "" { return nil, fmt.Errorf("webdav url required") } c.client = gowebdav.NewClient(c.URL, c.Username, c.Password) c.client.SetTimeout(c.Timeout) if err := c.client.Connect(); err != nil { c.client = nil return nil, fmt.Errorf("webdav: cannot connect to server: %w", err) } return c.client, nil } func (c *ReplicaClient) DeleteAll(ctx context.Context) error { client, err := c.init(ctx) if err != nil { return err } if err := client.RemoveAll(c.Path); err != nil && !os.IsNotExist(err) && !gowebdav.IsErrNotFound(err) { return fmt.Errorf("webdav: cannot delete path %q: %w", c.Path, err) } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "DELETE").Inc() return nil } func (c *ReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, _ bool) (_ ltx.FileIterator, err error) { client, err := c.init(ctx) if err != nil { return nil, err } dir := litestream.LTXLevelDir(c.Path, level) files, err := client.ReadDir(dir) if err != nil { if os.IsNotExist(err) || gowebdav.IsErrNotFound(err) { return ltx.NewFileInfoSliceIterator(nil), nil } return nil, fmt.Errorf("webdav: cannot read directory %q: %w", dir, err) } infos := make([]*ltx.FileInfo, 0, len(files)) for _, fi := range files { if fi.IsDir() { continue } minTXID, maxTXID, err := ltx.ParseFilename(path.Base(fi.Name())) if err != nil { continue } else if minTXID < seek { continue } infos = append(infos, <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Size: fi.Size(), CreatedAt: fi.ModTime().UTC(), }) } sort.Slice(infos, func(i, j int) bool { if infos[i].MinTXID != infos[j].MinTXID { return infos[i].MinTXID < infos[j].MinTXID } return infos[i].MaxTXID < infos[j].MaxTXID }) return ltx.NewFileInfoSliceIterator(infos), nil } // WriteLTXFile writes an LTX file to the WebDAV server. // // WebDAV Upload Strategy - Temp File Approach: // // Unlike other replica backends (S3, SFTP, NATS, ABS) which stream directly using // internal.NewReadCounter, WebDAV requires a different approach due to library and // protocol constraints: // // 1. gowebdav Library Limitations: // // - WriteStream() buffers entire payload in memory for non-seekable readers // // - WriteStreamWithLength() requires both content-length AND seekable reader // // - No native support for HTTP chunked transfer encoding // // 2. Server Compatibility Issues: // Research shows HTTP chunked transfer encoding with WebDAV is unreliable: // // - Nginx + FastCGI: Discards request body → 0-byte files (silent data loss) // // - Lighttpd: Returns HTTP 411 (Length Required), rejects chunked requests // // - Apache + FastCGI: Request body never arrives at application // // - Only Apache + mod_php handles chunked encoding reliably (~30-40% of deployments) // // 3. LTX Header Requirement: // - Must peek at LTX header to extract timestamp before upload // - Peeking consumes data, making the reader non-seekable // - Cannot calculate content-length without fully reading stream // // Solution: Stage to temporary file // // To ensure universal compatibility and prevent silent data loss: // 1. Extract timestamp from LTX header (required for file metadata) // 2. Stream full contents to temporary file on disk // 3. Seek back to start of temp file (now seekable + known size) // 4. Upload using WriteStreamWithLength() with Content-Length header // 5. Clean up temp file // // Trade-offs: // - Universal compatibility with all WebDAV server configurations // - No risk of silent data loss or failed uploads // - Predictable, reliable behavior // - Additional disk I/O overhead // - Requires local disk space proportional to LTX file size // - Diverges from streaming pattern used by other backends // // References: // - https://github.com/studio-b12/gowebdav/issues/35 (chunked encoding issues) // - https://github.com/nextcloud/server/issues/7995 (0-byte file bug) // - https://evertpot.com/260/ (WebDAV chunked encoding compatibility) func (c *ReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, rd io.Reader) (info *ltx.FileInfo, err error) { client, err := c.init(ctx) if err != nil { return nil, err } filename := litestream.LTXFilePath(c.Path, level, minTXID, maxTXID) var buf bytes.Buffer teeReader := io.TeeReader(rd, &buf) hdr, _, err := ltx.PeekHeader(teeReader) if err != nil { return nil, fmt.Errorf("extract timestamp from LTX header: %w", err) } timestamp := time.UnixMilli(hdr.Timestamp).UTC() // Stage to temporary file to get seekable reader with known size. // This ensures compatibility with all WebDAV servers and avoids the // unreliable chunked transfer encoding that causes silent data loss // on common configurations (Nginx+FastCGI, Lighttpd, Apache+FastCGI). tmpFile, err := os.CreateTemp("", "litestream-webdav-*.ltx") if err != nil { return nil, fmt.Errorf("webdav: cannot create temp file: %w", err) } defer func() { _ = tmpFile.Close() _ = os.Remove(tmpFile.Name()) }() fullReader := io.MultiReader(&buf, rd) size, err := io.Copy(tmpFile, fullReader) if err != nil { return nil, fmt.Errorf("webdav: cannot copy to temp file: %w", err) } if _, err := tmpFile.Seek(0, io.SeekStart); err != nil { return nil, fmt.Errorf("webdav: cannot seek temp file: %w", err) } if err := client.MkdirAll(path.Dir(filename), 0755); err != nil { return nil, fmt.Errorf("webdav: cannot create parent directory %q: %w", path.Dir(filename), err) } // Upload with Content-Length header using seekable temp file. // WriteStreamWithLength requires both a seekable reader and known size, // which we now have from the temp file. This avoids chunked encoding // and ensures reliable uploads across all WebDAV server configurations. if err := client.WriteStreamWithLength(filename, tmpFile, size, 0644); err != nil { return nil, fmt.Errorf("webdav: cannot write file %q: %w", filename, err) } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "PUT").Inc() internal.OperationBytesCounterVec.WithLabelValues(ReplicaClientType, "PUT").Add(float64(size)) return <x.FileInfo{ Level: level, MinTXID: minTXID, MaxTXID: maxTXID, Size: size, CreatedAt: timestamp, }, nil } func (c *ReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (_ io.ReadCloser, err error) { client, err := c.init(ctx) if err != nil { return nil, err } filename := litestream.LTXFilePath(c.Path, level, minTXID, maxTXID) internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "GET").Inc() if size > 0 { rc, err := client.ReadStreamRange(filename, offset, size) if err != nil { if os.IsNotExist(err) || gowebdav.IsErrNotFound(err) { return nil, os.ErrNotExist } return nil, fmt.Errorf("webdav: cannot read file %q: %w", filename, err) } return internal.LimitReadCloser(rc, size), nil } if offset > 0 { rc, err := client.ReadStream(filename) if err != nil { if os.IsNotExist(err) || gowebdav.IsErrNotFound(err) { return nil, os.ErrNotExist } return nil, fmt.Errorf("webdav: cannot read file %q: %w", filename, err) } if _, err := io.CopyN(io.Discard, rc, offset); err != nil { if err == io.EOF || err == io.ErrUnexpectedEOF { _ = rc.Close() return io.NopCloser(bytes.NewReader(nil)), nil } _ = rc.Close() return nil, fmt.Errorf("webdav: cannot skip offset in file %q: %w", filename, err) } return rc, nil } rc, err := client.ReadStream(filename) if err != nil { if os.IsNotExist(err) || gowebdav.IsErrNotFound(err) { return nil, os.ErrNotExist } return nil, fmt.Errorf("webdav: cannot read file %q: %w", filename, err) } return rc, nil } func (c *ReplicaClient) DeleteLTXFiles(ctx context.Context, a []*ltx.FileInfo) error { client, err := c.init(ctx) if err != nil { return err } for _, info := range a { filename := litestream.LTXFilePath(c.Path, info.Level, info.MinTXID, info.MaxTXID) c.logger.Debug("deleting ltx file", "level", info.Level, "minTXID", info.MinTXID, "maxTXID", info.MaxTXID, "path", filename) if err := client.Remove(filename); err != nil && !os.IsNotExist(err) && !gowebdav.IsErrNotFound(err) { return fmt.Errorf("webdav: cannot delete ltx file %q: %w", filename, err) } internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "DELETE").Inc() } return nil } ================================================ FILE: webdav/replica_client_test.go ================================================ package webdav_test import ( "bytes" "context" "fmt" "io" "net/http" "net/http/httptest" "strconv" "strings" "sync" "testing" "time" "github.com/superfly/ltx" "github.com/benbjohnson/litestream/webdav" ) func TestReplicaClient_Type(t *testing.T) { c := webdav.NewReplicaClient() if got, want := c.Type(), "webdav"; got != want { t.Fatalf("Type()=%v, want %v", got, want) } } func TestReplicaClient_Init_RequiresURL(t *testing.T) { c := webdav.NewReplicaClient() c.URL = "" if err := c.Init(context.TODO()); err == nil { t.Fatal("expected error when URL is empty") } else if got, want := err.Error(), "webdav url required"; got != want { t.Fatalf("error=%v, want %v", got, want) } } func TestReplicaClient_DeleteAll_NotFound(t *testing.T) { srv := newFakeWebDAVServer() srv.deleteReturn404["/missing"] = true ts := httptest.NewServer(srv) defer ts.Close() c := newTestReplicaClient(ts.URL) c.Path = "/missing" if err := c.DeleteAll(context.Background()); err != nil { t.Fatalf("DeleteAll returned error: %v", err) } } func TestReplicaClient_LTXFiles_PathNotFound(t *testing.T) { srv := newFakeWebDAVServer() srv.propfindReturn404["/missing/ltx/0"] = true ts := httptest.NewServer(srv) defer ts.Close() c := newTestReplicaClient(ts.URL) c.Path = "/missing" itr, err := c.LTXFiles(context.Background(), 0, 0, false) if err != nil { t.Fatalf("LTXFiles returned error: %v", err) } defer itr.Close() if itr.Next() { t.Fatal("expected no iterator items when directory is missing") } } func TestReplicaClient_OpenLTXFile_RangeFallback(t *testing.T) { srv := newFakeWebDAVServer() srv.ignoreRange = true // simulate server ignoring Range requests ts := httptest.NewServer(srv) defer ts.Close() c := newTestReplicaClient(ts.URL) c.Path = "/db" payload := []byte("ABCDEFGHIJ") ltxData := buildLTXPayload(1, 2, payload) if _, err := c.WriteLTXFile(context.Background(), 0, 1, 2, bytes.NewReader(ltxData)); err != nil { t.Fatalf("WriteLTXFile failed: %v", err) } rc, err := c.OpenLTXFile(context.Background(), 0, 1, 2, 0, int64(len(payload))) if err != nil { t.Fatalf("OpenLTXFile failed: %v", err) } defer rc.Close() got, err := io.ReadAll(rc) if err != nil { t.Fatalf("ReadAll failed: %v", err) } if want := ltxData[:len(payload)]; !bytes.Equal(got, want) { t.Fatalf("OpenLTXFile range fallback returned %q, want %q", got, want) } } func TestReplicaClient_OpenLTXFile_OffsetOnly(t *testing.T) { srv := newFakeWebDAVServer() ts := httptest.NewServer(srv) defer ts.Close() c := newTestReplicaClient(ts.URL) c.Path = "/db" payload := []byte("ABCDEFGHIJ") ltxData := buildLTXPayload(10, 12, payload) if _, err := c.WriteLTXFile(context.Background(), 0, 10, 12, bytes.NewReader(ltxData)); err != nil { t.Fatalf("WriteLTXFile failed: %v", err) } offset := int64(len(ltxData) - len(payload)/2) rc, err := c.OpenLTXFile(context.Background(), 0, 10, 12, offset, 0) if err != nil { t.Fatalf("OpenLTXFile failed: %v", err) } defer rc.Close() got, err := io.ReadAll(rc) if err != nil { t.Fatalf("ReadAll failed: %v", err) } if want := ltxData[offset:]; !bytes.Equal(got, want) { t.Fatalf("OpenLTXFile offset reader returned %q, want %q", got, want) } } // newTestReplicaClient returns a replica client pointed at a test server. func newTestReplicaClient(baseURL string) *webdav.ReplicaClient { c := webdav.NewReplicaClient() c.URL = baseURL c.Timeout = time.Second return c } // buildLTXPayload constructs a minimal valid LTX file with payload data. func buildLTXPayload(minTXID, maxTXID ltx.TXID, payload []byte) []byte { hdr := ltx.Header{ Version: 1, PageSize: 4096, Commit: 1, MinTXID: minTXID, MaxTXID: maxTXID, Timestamp: time.Now().UnixMilli(), } headerBytes, _ := hdr.MarshalBinary() return append(headerBytes, payload...) } // fakeWebDAVServer provides a minimal in-memory WebDAV implementation for tests. type fakeWebDAVServer struct { mu sync.Mutex files map[string][]byte ignoreRange bool deleteReturn404 map[string]bool propfindReturn404 map[string]bool } func newFakeWebDAVServer() *fakeWebDAVServer { return &fakeWebDAVServer{ files: make(map[string][]byte), deleteReturn404: make(map[string]bool), propfindReturn404: make(map[string]bool), } } func (s *fakeWebDAVServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.mu.Lock() defer s.mu.Unlock() switch r.Method { case http.MethodOptions: w.WriteHeader(http.StatusOK) case "MKCOL": w.WriteHeader(http.StatusCreated) case http.MethodPut: body, _ := io.ReadAll(r.Body) s.files[r.URL.Path] = body w.WriteHeader(http.StatusCreated) case http.MethodGet: s.handleGet(w, r) case http.MethodDelete: s.handleDelete(w, r) case "PROPFIND": s.handlePropfind(w, r) default: w.WriteHeader(http.StatusNotImplemented) } } func (s *fakeWebDAVServer) handleGet(w http.ResponseWriter, r *http.Request) { data, ok := s.files[r.URL.Path] if !ok { http.NotFound(w, r) return } rangeHeader := r.Header.Get("Range") if rangeHeader != "" && !s.ignoreRange { start, end, err := parseRange(rangeHeader, len(data)) if err != nil { w.WriteHeader(http.StatusRequestedRangeNotSatisfiable) return } w.Header().Set("Content-Length", strconv.Itoa(end-start)) w.WriteHeader(http.StatusPartialContent) _, _ = w.Write(data[start:end]) return } w.Header().Set("Content-Length", strconv.Itoa(len(data))) w.WriteHeader(http.StatusOK) _, _ = w.Write(data) } func (s *fakeWebDAVServer) handleDelete(w http.ResponseWriter, r *http.Request) { trimmed := strings.TrimSuffix(r.URL.Path, "/") if s.deleteReturn404[trimmed] { w.WriteHeader(http.StatusNotFound) return } if _, ok := s.files[r.URL.Path]; ok { delete(s.files, r.URL.Path) w.WriteHeader(http.StatusNoContent) return } w.WriteHeader(http.StatusNotFound) } func (s *fakeWebDAVServer) handlePropfind(w http.ResponseWriter, r *http.Request) { trimmed := strings.TrimSuffix(r.URL.Path, "/") if s.propfindReturn404[trimmed] { w.WriteHeader(http.StatusNotFound) return } response := fmt.Sprintf(` %s/ 0 %s HTTP/1.1 200 OK `, trimmed, time.Now().UTC().Format(http.TimeFormat)) w.Header().Set("Content-Type", "application/xml; charset=utf-8") w.Header().Set("Content-Length", strconv.Itoa(len(response))) w.WriteHeader(207) _, _ = w.Write([]byte(response)) } func parseRange(header string, size int) (start, end int, err error) { if !strings.HasPrefix(header, "bytes=") { return 0, 0, fmt.Errorf("unsupported range header: %s", header) } parts := strings.Split(strings.TrimPrefix(header, "bytes="), "-") if len(parts) != 2 { return 0, 0, fmt.Errorf("invalid range header: %s", header) } start, err = strconv.Atoi(parts[0]) if err != nil { return 0, 0, err } if parts[1] == "" { end = size } else { end, err = strconv.Atoi(parts[1]) if err != nil { return 0, 0, err } end++ // range end is inclusive } if start < 0 || end > size || start >= end { return 0, 0, fmt.Errorf("invalid range [%d, %d) for size %d", start, end, size) } return start, end, nil }