triedb/pathdb: periodically reopen the iterator to unblock compactor

This commit is contained in:
Gary Rong 2026-03-13 14:51:03 +08:00
parent d1b5c4101f
commit eb6c814332

View file

@ -33,6 +33,12 @@ const (
// accumulate before triggering index pruning. This helps avoid scheduling // accumulate before triggering index pruning. This helps avoid scheduling
// index pruning too frequently. // index pruning too frequently.
indexPruningThreshold = 90000 indexPruningThreshold = 90000
// indexPruneReopenInterval is how long the iterator is kept open before
// being released and re-opened. Long-lived iterators hold a read snapshot
// that blocks LSM compaction; periodically re-opening avoids stalling the
// compactor during a large scan.
indexPruneReopenInterval = 3 * time.Minute
) )
// indexPruner is responsible for pruning stale index data from the tail side // indexPruner is responsible for pruning stale index data from the tail side
@ -163,28 +169,34 @@ func (p *indexPruner) process(tail uint64) error {
return nil return nil
} }
// prunePrefix scans up to indexPruneBatchSize metadata entries starting from // prunePrefix scans all metadata entries under the given prefix and prunes
// the cursor position and prunes leading index blocks below the tail. The // leading index blocks below the tail. The iterator is periodically released
// cursor advances after each cycle; when the prefix is fully scanned, the // and re-opened to avoid holding a read snapshot that blocks LSM compaction.
// cursor resets so the next cycle starts from the beginning.
// Returns (prunedBlocks, scannedEntries, error).
func (p *indexPruner) prunePrefix(prefix []byte, elemType elementType, tail uint64) (int, error) { func (p *indexPruner) prunePrefix(prefix []byte, elemType elementType, tail uint64) (int, error) {
var ( var (
pruned int pruned int
start []byte // iterator seek position
batch = p.disk.NewBatchWithSize(ethdb.IdealBatchSize) batch = p.disk.NewBatchWithSize(ethdb.IdealBatchSize)
) )
it := p.disk.NewIterator(prefix, nil) for {
defer it.Release() var (
reopen bool
opened = time.Now()
it = p.disk.NewIterator(prefix, start)
)
for it.Next() { for it.Next() {
// Check for shutdown // Check termination
select { select {
case <-p.closed: case <-p.closed:
it.Release()
if batch.ValueSize() > 0 {
return pruned, batch.Write()
}
return pruned, nil return pruned, nil
default: default:
} }
key, value := it.Key(), it.Value()
key, value := it.Key(), it.Value()
ident, bsize := p.identFromKey(key, prefix, elemType) ident, bsize := p.identFromKey(key, prefix, elemType)
n, err := p.pruneEntry(batch, ident, value, bsize, tail) n, err := p.pruneEntry(batch, ident, value, bsize, tail)
if err != nil { if err != nil {
@ -195,10 +207,24 @@ func (p *indexPruner) prunePrefix(prefix []byte, elemType elementType, tail uint
if batch.ValueSize() >= ethdb.IdealBatchSize { if batch.ValueSize() >= ethdb.IdealBatchSize {
if err := batch.Write(); err != nil { if err := batch.Write(); err != nil {
it.Release()
return 0, err return 0, err
} }
batch.Reset() batch.Reset()
} }
// Periodically release the iterator so the LSM compactor
// is not blocked by the read snapshot we hold.
if time.Since(opened) >= indexPruneReopenInterval {
reopen = true
start = common.CopyBytes(key[len(prefix):])
break
}
}
it.Release()
if !reopen {
break
}
} }
if batch.ValueSize() > 0 { if batch.ValueSize() > 0 {
if err := batch.Write(); err != nil { if err := batch.Write(); err != nil {