fix(traffic): 删除流量后把索引空间真正还给操作系统

删除流量只让数据不可见,磁盘占用从不下降。两处原因叠加:

1. SQLite 删行仅把页挂到 freelist,而索引库建库时未启用 auto_vacuum,
   文件永不收缩。256KB 以下的正文全部内联在这个库里,所以体积主体就在
   这里,且只增不减。
2. ex_fts 是 contentless_delete 全文索引,DELETE 只写 tombstone、不回收
   原 postings,从来没有合并过——删流量反而让索引变大(实测 exchanges
   清零后 ex_fts_data 仍占 3.3MB / 840 行)。

trigram 索引约为正文体积的 2 倍,加上内联正文,抓包量大的实例会为早已
删掉的流量长期占用数倍磁盘:实测抓 6MB 文本正文 → 索引 16.2MB,删光后
仍是 16.2MB。

修复:
- 新建索引库在建表前启用 auto_vacuum=incremental。该 pragma 只在库中尚
  无表时可设,且只有紧随的 VACUUM 才把它写入文件头,两步都必须落在同一
  条连接上,故新增 initIndex 固定一条 *sql.Conn 串起 pragma 与建表。
- 新增 reclaim/reclaimChunk:删除提交后在后台分块执行全文索引增量合并、
  incremental_vacuum 与 wal_checkpoint(TRUNCATE)(不做 TRUNCATE 则空间
  留在 WAL 里、文件大小不变,PASSIVE 也清不掉)。接入 DeleteHost、
  HostDeleteStage.Commit 与 RecoverHostDeleteStages 三条路径。
- 回收分块进行并在块间释放写锁:record() 由 go-mitmproxy 在把响应写回
  客户端之前同步调用,一次性回收数 GB 会连带卡住被测请求。Close 先发停止
  信号,剩余预算留给下次删除,不拖住进程退出。
- 终止条件按「是否有进展」判定:incremental_vacuum 只能释放挪得到文件
  末尾的页,残留少量空闲页是正常结果;另加步数上限兜底。fts5 无法查询
  索引是否已合并完(其特殊 INSERT 自身即计一次 changes),故改为固定合并
  预算,未做完的留给后续删除。
- busy_timeout 改由 DSN 提供:它是每连接设置,原先的 db.Exec 只配到池中
  碰巧被选中的那一条,其余连接在有写者时会立即失败而非等待。驱动按第一个
  '?' 切分 DSN,故数据目录路径含 '?' 时退回裸路径 DSN。

已有实例的索引库仍是旧模式(auto_vacuum 无法事后开启),incremental_vacuum
在其上为空操作,启动时打印提示;此类库的 tombstone 增长已止住,已占体积
待后续「存储压缩」入口一次性回收。

测试:新增 reclaim_test.go(空间回收回归、tombstone 合并、新库模式、旧库
不卡死)与 upgrade_test.go(旧装机升级后同库同数据、全文与正文可用、
legacy path 行不受影响;以及降级回旧二进制仍可读写)。
This commit is contained in:
Autumn-27 committed 2026-09-30 01:44:44 -04:00
1 parent ca5512b48f
commit 3227f2e54c
4 files changed
+583 -17

No files matched your search

+255 -17
View File
@@ -102,6 +102,30 @@ const (
// maxBlobRead caps one traffic_blob read so paging through a large body never
// floods the agent's context.
maxBlobRead = 8 * 1024
// autoVacuumIncremental is SQLite's numeric value for auto_vacuum=incremental.
autoVacuumIncremental = 2
// reclaimChunkPages bounds how much index space one locked step returns to the
// filesystem (pages are 4KiB, so ~32MB). Reclamation holds the write lock, and
// record() runs before go-mitmproxy replies to the client, so an unbounded
// pass would stall the very requests being recorded.
reclaimChunkPages = 8192
// reclaimMergePages bounds the full-text merge done alongside each chunk.
// Deleting from a contentless_delete index only writes tombstones; merging is
// what discards them, and left undone the index grows on every deletion.
reclaimMergePages = 256
// reclaimMergeSteps bounds how many merges one reclamation performs. fts5
// offers no way to ask whether an index has settled — its special INSERT
// registers a row change of its own, so change counting cannot tell a real
// merge from a no-op — so the budget is simply spent down, and whatever is
// left over falls to the next deletion.
reclaimMergeSteps = 16
// reclaimMaxSteps backstops the loop. Both merging and incremental_vacuum are
// documented to stop making progress eventually, but a reclamation that cannot
// converge must give up rather than spin holding the write lock.
reclaimMaxSteps = 512
// reclaimBudget caps one background reclamation. Deleting a busy host can free
// gigabytes, and the next deletion resumes wherever this one stopped.
reclaimBudget = 5 * time.Minute
)
// TrafficSearchDescription is persisted into the tool catalog for new and
@@ -121,9 +145,22 @@ type Traffic struct {
// build without FTS5: recording and metadata search still work, body search
// degrades to unsupported rather than erroring.
fts bool
// reaping tracks background reclamation of legacy trees, so shutdown and tests
// can wait for it instead of racing it.
// reaping tracks background reclamation of legacy trees and index space, so
// shutdown and tests can wait for it instead of racing it.
reaping sync.WaitGroup
// incrementalVacuum reports whether the index can return freed pages to the
// filesystem on its own. False on a database created before this was the
// default: PRAGMA incremental_vacuum is a silent no-op there, so deletions
// reclaim nothing until an explicit full compaction converts the file.
incrementalVacuum bool
// reclaiming keeps a single background reclamation in flight. Concurrent ones
// would only contend for wmu, never finish sooner.
reclaiming atomic.Bool
// closed is shut by Close so a reclamation abandons its remaining budget
// instead of holding shutdown open for minutes. Nil on the zero value, which
// stopping() treats as "not closing".
closed chan struct{}
closeOnce sync.Once
// pass is the set of hosts whose MITM interception failed for a proxy/protocol
// reason; connections to them are tunneled transparently (fail-open) so the
// request still reaches the target — unrecorded — instead of being killed.
@@ -142,26 +179,27 @@ func Open(dir, addr string) (*Traffic, error) {
return nil, err
}
}
db, err := sql.Open("sqlite", filepath.Join(dir, "_index", "index.sqlite"))
// busy_timeout is a per-connection setting, so it belongs in the DSN rather
// than in a one-off Exec: the pool opens connections on demand, and an Exec
// only configures whichever one happened to serve it — leaving every other
// connection to fail instantly the moment a writer holds the database.
// The driver splits the DSN at the first '?', so a data directory containing
// one would silently name a different file; that path falls back to a bare
// DSN, where initIndex still applies the pragmas to its own connection.
index := filepath.Join(dir, "_index", "index.sqlite")
dsn := index
if !strings.ContainsRune(index, '?') {
dsn = "file:" + index + "?_pragma=busy_timeout(5000)&_pragma=journal_mode(WAL)"
}
db, err := sql.Open("sqlite", dsn)
if err != nil {
return nil, err
}
for _, p := range []string{"PRAGMA journal_mode=WAL", "PRAGMA busy_timeout=5000"} {
if _, err := db.Exec(p); err != nil {
db.Close()
return nil, err
}
}
if _, err := db.Exec(indexSchema); err != nil {
t := &Traffic{dir: dir, addr: addr, db: db, closed: make(chan struct{})}
if err := t.initIndex(); err != nil {
db.Close()
return nil, err
}
t := &Traffic{dir: dir, addr: addr, db: db}
if _, err := db.Exec(ftsSchema); err != nil {
log.Printf("[traffic] 全文索引不可用,正文搜索将被禁用(元数据搜索不受影响):%v", err)
} else {
t.fts = true
}
p, err := mproxy.NewProxy(&mproxy.Options{
Addr: addr,
@@ -193,6 +231,66 @@ func Open(dir, addr string) (*Traffic, error) {
return t, nil
}
// initIndex applies the schema on a single pinned connection. Pinning is what
// makes auto_vacuum reliable: it can only be set while the database still holds
// no tables, and only the VACUUM that follows writes it into the file header —
// two steps a pooled *sql.DB is free to route to different connections, which
// would silently drop the setting.
//
// auto_vacuum=incremental is what lets a deletion hand freed pages back to the
// filesystem. Without it SQLite merely chains them onto its freelist, so the
// index file never shrinks no matter how much traffic is deleted — and since
// every body below maxInlineBody lives in that file, plus a trigram index
// roughly twice the size of the text it covers, a capture-heavy install ends up
// holding gigabytes for traffic it no longer has. On a database that already has
// tables the pragma is a documented no-op, so installs created before this keep
// auto_vacuum=0 until a full compaction converts the file; incrementalVacuum
// records that so reclaim can say so instead of pretending to reclaim.
func (t *Traffic) initIndex() error {
ctx := context.Background()
conn, err := t.db.Conn(ctx)
if err != nil {
return err
}
defer conn.Close()
// Re-applied rather than left to the DSN so the bare-DSN fallback in Open is
// still correct: journal_mode persists in the file header, which is what every
// later connection reads.
for _, p := range []string{`PRAGMA journal_mode=WAL`, `PRAGMA busy_timeout=5000`} {
if _, err := conn.ExecContext(ctx, p); err != nil {
return err
}
}
var tables int
if err := conn.QueryRowContext(ctx, `SELECT count(*) FROM sqlite_master WHERE type='table'`).Scan(&tables); err != nil {
return err
}
if tables == 0 {
for _, p := range []string{`PRAGMA auto_vacuum=incremental`, `VACUUM`} {
if _, err := conn.ExecContext(ctx, p); err != nil {
return err
}
}
}
var mode int
if err := conn.QueryRowContext(ctx, `PRAGMA auto_vacuum`).Scan(&mode); err != nil {
return err
}
t.incrementalVacuum = mode == autoVacuumIncremental
if !t.incrementalVacuum {
log.Printf("[traffic] 索引库未启用增量回收(auto_vacuum=%d):删除流量不会缩小 index.sqlite,需要执行一次存储压缩来转换", mode)
}
if _, err := conn.ExecContext(ctx, indexSchema); err != nil {
return err
}
if _, err := conn.ExecContext(ctx, ftsSchema); err != nil {
log.Printf("[traffic] 全文索引不可用,正文搜索将被禁用(元数据搜索不受影响):%v", err)
return nil
}
t.fts = true
return nil
}
// hostOnly strips an optional :port, so passthrough keys match whether the host
// arrives as "example.com:443" (CONNECT) or "example.com" (request URL).
func hostOnly(hostport string) string {
@@ -262,11 +360,27 @@ func (t *Traffic) Start() error { return t.proxy.Start() }
// Close waits for background tree reclamation to finish before closing the
// index, so shutdown never leaves a goroutine unlinking files out from under a
// removed data directory.
// removed data directory. Index-space reclamation is signalled to stop first:
// it holds a whole minutes-long budget, and finishing it is never worth delaying
// shutdown for — the next deletion resumes it.
func (t *Traffic) Close() error {
if t.closed != nil {
t.closeOnce.Do(func() { close(t.closed) })
}
t.reaping.Wait()
return t.db.Close()
}
// stopping reports whether Close has been called. A nil channel (the zero value)
// is never ready, so this reads as "not closing" without a separate guard.
func (t *Traffic) stopping() bool {
select {
case <-t.closed:
return true
default:
return false
}
}
func (t *Traffic) DB() *sql.DB { return t.db }
// sink is the go-mitmproxy addon that records completed exchanges.
@@ -944,6 +1058,7 @@ func (t *Traffic) DeleteHost(host string) (int64, error) {
}
t.reapStage(stageDir)
if n > 0 {
t.reclaim()
if err := t.gcBlobs(); err != nil {
return n, err
}
@@ -1345,6 +1460,7 @@ func (t *Traffic) RecoverHostDeleteStages(archiveCommitted func(int64, int64) (b
needsGC = true
}
if needsGC {
t.reclaim()
if err := t.gcBlobs(); err != nil {
errs = append(errs, err)
}
@@ -1436,6 +1552,7 @@ func (s *HostDeleteStage) Commit() error {
// it now runs in the background, while collection below only needs them to be
// out of the live tree — which the staging rename already guaranteed.
s.traffic.reapStage(s.stageDir)
s.traffic.reclaim()
var errs []error
if err := s.traffic.gcBlobs(); err != nil {
errs = append(errs, fmt.Errorf("回收流量 blob: %w", err))
@@ -1445,6 +1562,127 @@ func (s *HostDeleteStage) Commit() error {
return errors.Join(errs...)
}
// indexBytes is the index's real footprint on disk: the database plus its
// write-ahead log and shared-memory file, since those are what an operator sees
// the data directory holding.
func (t *Traffic) indexBytes() int64 {
base := filepath.Join(t.dir, "_index", "index.sqlite")
var total int64
for _, p := range []string{base, base + "-wal", base + "-shm"} {
if st, err := os.Stat(p); err == nil {
total += st.Size()
}
}
return total
}
// reclaim hands space freed by a deletion back to the filesystem. Deleting rows
// only makes them invisible: SQLite chains their pages onto a freelist, and
// deleting from the contentless_delete full-text index writes tombstones rather
// than removing the postings they hide. Neither shrinks a single byte on disk,
// and since bodies below maxInlineBody live in that same file, an install that
// captures heavily ends up holding far more than the traffic it still has.
//
// The work is done in the background, in bounded steps that drop the write lock
// between them, because it is proportional to what was deleted — freeing several
// gigabytes under one lock would stall record(), which go-mitmproxy calls before
// it replies to the client, and so would stall the requests being recorded.
// Safe to call with wmu held: the background pass simply waits for it.
//
// Best-effort throughout. Failing to reclaim costs disk space, never
// correctness, so errors are logged and the next deletion resumes the work.
func (t *Traffic) reclaim() {
if !t.reclaiming.CompareAndSwap(false, true) {
return // one pass at a time; a second would only contend for the lock
}
t.reaping.Go(func() {
defer t.reclaiming.Store(false)
// One pinned connection for the whole pass: a long reclamation issues
// hundreds of statements, and letting the pool hand each one a different
// connection would both churn connections and split the freelist readings
// that decide when to stop away from the vacuum they measure.
ctx := context.Background()
conn, err := t.db.Conn(ctx)
if err != nil {
log.Printf("[traffic] 回收索引空间失败(获取连接):%v", err)
return
}
defer conn.Close()
deadline := time.Now().Add(reclaimBudget)
merges := reclaimMergeSteps
for step := 0; ; step++ {
t.wmu.Lock()
progressed, err := t.reclaimChunk(ctx, conn, &merges)
t.wmu.Unlock()
if err != nil {
log.Printf("[traffic] 回收索引空间失败:%v", err)
return
}
if !progressed {
break
}
if t.stopping() {
return // shutdown must not wait out the remaining budget
}
if step+1 >= reclaimMaxSteps {
log.Printf("[traffic] 索引空间回收未做完(已用满 %d 步上限),下次删除时继续", reclaimMaxSteps)
return
}
if time.Now().After(deadline) {
log.Printf("[traffic] 索引空间回收未做完(已用满 %s 预算),下次删除时继续", reclaimBudget)
return
}
}
// Truncating the log is what makes the reclamation visible on disk: in WAL
// mode the freed pages are recorded there first, and a PASSIVE checkpoint
// would leave the log itself sitting at its high-water mark.
t.wmu.Lock()
defer t.wmu.Unlock()
if _, err := conn.ExecContext(ctx, `PRAGMA wal_checkpoint(TRUNCATE)`); err != nil {
log.Printf("[traffic] 截断 WAL 失败:%v", err)
}
})
}
// reclaimChunk does one bounded step and reports whether it made progress, which
// is what the caller loops on. merges carries the remaining full-text merge
// budget and is spent down here. Callers must hold wmu.
func (t *Traffic) reclaimChunk(ctx context.Context, conn *sql.Conn, merges *int) (bool, error) {
progressed := false
if t.fts && *merges > 0 {
// A negative rank is fts5's page budget for one incremental merge.
if _, err := conn.ExecContext(ctx, `INSERT INTO ex_fts(ex_fts, rank) VALUES('merge', ?)`, -reclaimMergePages); err != nil {
return false, fmt.Errorf("合并全文索引: %w", err)
}
*merges--
progressed = true
}
if !t.incrementalVacuum {
// incremental_vacuum is a silent no-op on a database created with
// auto_vacuum=0; only a full compaction can convert one. Merging the
// full-text index above still pays off, so stop here rather than earlier.
return progressed, nil
}
var before, after int
if err := conn.QueryRowContext(ctx, `PRAGMA freelist_count`).Scan(&before); err != nil {
return false, err
}
if before == 0 {
return progressed, nil
}
// The budget is a constant and PRAGMA arguments cannot be bound as parameters.
if _, err := conn.ExecContext(ctx, fmt.Sprintf(`PRAGMA incremental_vacuum(%d)`, reclaimChunkPages)); err != nil {
return false, fmt.Errorf("回收索引空闲页: %w", err)
}
if err := conn.QueryRowContext(ctx, `PRAGMA freelist_count`).Scan(&after); err != nil {
return false, err
}
// Progress, not an empty freelist, is the right stopping condition:
// incremental_vacuum can only release pages it manages to move to the end of
// the file, so a residual freelist it cannot shrink is a normal outcome.
return progressed || after < before, nil
}
// gcBlobs removes blobs that no remaining exchange references. Live references
// come from blob_refs, so collection is one query plus a sweep of the blob
// directory — no exchange body is ever read. Emptied bucket directories are