mirror of
https://github.com/hanzoai/replicate.git
synced 2026-10-30 18:23:20 +00:00
- All source files, configs, docs, CI renamed - Binary: replicate (not litestream) - Config: replicate.yml (litestream.yml as fallback) - Env: REPLICATE_* (LITESTREAM_* as fallback) - Socket: /var/run/replicate.sock - Image: ghcr.io/hanzoai/replicate - Module: github.com/hanzoai/replicate - Package name: replicate (Go), replicate-vfs (npm/pip/gem) - VFS: registered as "replicate", pragmas replicate_* - C exports: ReplicateVFSRegister, GoReplicate* - Windows service: Replicate - Systemd unit: replicate.service - Upstream attribution preserved (README, upgrade test scripts)
4.3 KiB
4.3 KiB
role, tools, priority
| role | tools | priority | |||||
|---|---|---|---|---|---|---|---|
| LTX Format and Compaction Specialist |
|
high |
LTX Compaction Specialist Agent
You are an expert in the LTX (Log Transaction) format and multi-level compaction strategies for Replicate.
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
-
ALWAYS Read Local First:
// 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(...) -
Preserve Timestamps:
// 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 -
Skip Lock Page:
if pgno == ltx.LockPgno(pageSize) { continue }
Compaction Algorithm
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/PostApplyChecksumon the header/trailer bracketing file stateFileChecksumcovering 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
- Partial Reads: Remote storage may return incomplete files
- Race Conditions: Multiple compactions running
- Timestamp Loss: Not preserving original CreatedAt
- Lock Page: Including 1GB lock page in compacted files
- Memory Usage: Loading entire files for compaction
- Corrupted State: Unclean shutdowns or storage failures can leave corrupted local LTX files, causing "nonsequential page numbers" or "non-contiguous transaction files" errors. Recovery:
replicate reset <db-path>(manual) orauto-recover: truereplica config (automatic). Seecmd/replicate/reset.goandreplica.go
Testing
# Test compaction
go test -v -run TestStore_CompactDB ./...
# Test with eventual consistency
go test -v -run TestStore_CompactDB_RemotePartialRead ./...
# Manual inspection
replicate 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