|
| 1 | +package pruner |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/binary" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "sync" |
| 9 | + "time" |
| 10 | + |
| 11 | + ds "github.com/ipfs/go-datastore" |
| 12 | + "github.com/rs/zerolog" |
| 13 | + |
| 14 | + coreexecutor "github.com/evstack/ev-node/core/execution" |
| 15 | + |
| 16 | + "github.com/evstack/ev-node/pkg/config" |
| 17 | + "github.com/evstack/ev-node/pkg/store" |
| 18 | +) |
| 19 | + |
| 20 | +const defaultPruneInterval = 15 * time.Minute |
| 21 | + |
| 22 | +// Pruner periodically removes old state and execution metadata entries. |
| 23 | +type Pruner struct { |
| 24 | + store store.Store |
| 25 | + execPruner coreexecutor.ExecPruner |
| 26 | + cfg config.NodeConfig |
| 27 | + logger zerolog.Logger |
| 28 | + lastPruned uint64 |
| 29 | + |
| 30 | + // Lifecycle |
| 31 | + ctx context.Context |
| 32 | + wg sync.WaitGroup |
| 33 | + cancel context.CancelFunc |
| 34 | +} |
| 35 | + |
| 36 | +// New creates a new Pruner instance. |
| 37 | +func New( |
| 38 | + logger zerolog.Logger, |
| 39 | + store store.Store, |
| 40 | + execPruner coreexecutor.ExecPruner, |
| 41 | + cfg config.NodeConfig, |
| 42 | +) *Pruner { |
| 43 | + return &Pruner{ |
| 44 | + store: store, |
| 45 | + execPruner: execPruner, |
| 46 | + cfg: cfg, |
| 47 | + logger: logger.With().Str("component", "prune").Logger(), |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// Start begins the pruning loop. |
| 52 | +func (p *Pruner) Start(ctx context.Context) error { |
| 53 | + p.ctx, p.cancel = context.WithCancel(ctx) |
| 54 | + |
| 55 | + // Start pruner loop |
| 56 | + p.wg.Go(p.pruneLoop) |
| 57 | + |
| 58 | + p.logger.Info().Msg("pruner started") |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +// Stop stops the pruning loop. |
| 63 | +func (p *Pruner) Stop() error { |
| 64 | + if p.cancel != nil { |
| 65 | + p.cancel() |
| 66 | + } |
| 67 | + p.wg.Wait() |
| 68 | + |
| 69 | + p.logger.Info().Msg("pruner stopped") |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func (p *Pruner) pruneLoop() { |
| 74 | + ticker := time.NewTicker(defaultPruneInterval) |
| 75 | + defer ticker.Stop() |
| 76 | + |
| 77 | + for { |
| 78 | + select { |
| 79 | + case <-ticker.C: |
| 80 | + if err := p.pruneRecoveryHistory(p.ctx, p.cfg.RecoveryHistoryDepth); err != nil { |
| 81 | + p.logger.Error().Err(err).Msg("failed to prune recovery history") |
| 82 | + } |
| 83 | + |
| 84 | + if err := p.pruneBlocks(); err != nil { |
| 85 | + p.logger.Error().Err(err).Msg("failed to prune old blocks") |
| 86 | + } |
| 87 | + |
| 88 | + // TODO: add pruning of old blocks // https://github.com/evstack/ev-node/pull/2984 |
| 89 | + case <-p.ctx.Done(): |
| 90 | + return |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func (p *Pruner) pruneBlocks() error { |
| 96 | + if !p.cfg.PruningEnabled || p.cfg.PruningKeepRecent == 0 || p.cfg.PruningInterval == 0 { |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + var currentDAIncluded uint64 |
| 101 | + currentDAIncludedBz, err := p.store.GetMetadata(p.ctx, store.DAIncludedHeightKey) |
| 102 | + if err == nil && len(currentDAIncludedBz) == 8 { |
| 103 | + currentDAIncluded = binary.LittleEndian.Uint64(currentDAIncludedBz) |
| 104 | + } else { |
| 105 | + // if we cannot get the current DA height, we cannot safely prune, so we skip pruning until we can get it. |
| 106 | + return nil |
| 107 | + } |
| 108 | + |
| 109 | + var lastPruned uint64 |
| 110 | + if bz, err := p.store.GetMetadata(p.ctx, store.LastPrunedBlockHeightKey); err == nil && len(bz) == 8 { |
| 111 | + lastPruned = binary.LittleEndian.Uint64(bz) |
| 112 | + } |
| 113 | + |
| 114 | + storeHeight, err := p.store.Height(p.ctx) |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("failed to get store height for pruning: %w", err) |
| 117 | + } |
| 118 | + if storeHeight <= lastPruned+p.cfg.PruningInterval { |
| 119 | + return nil |
| 120 | + } |
| 121 | + |
| 122 | + // Never prune blocks that are not DA included |
| 123 | + upperBound := min(storeHeight, currentDAIncluded) |
| 124 | + if upperBound <= p.cfg.PruningKeepRecent { |
| 125 | + // Not enough fully included blocks to prune |
| 126 | + return nil |
| 127 | + } |
| 128 | + |
| 129 | + targetHeight := upperBound - p.cfg.PruningKeepRecent |
| 130 | + |
| 131 | + if err := p.store.PruneBlocks(p.ctx, targetHeight); err != nil { |
| 132 | + p.logger.Error().Err(err).Uint64("target_height", targetHeight).Msg("failed to prune old block data") |
| 133 | + } |
| 134 | + |
| 135 | + if p.execPruner != nil { |
| 136 | + if err := p.execPruner.PruneExec(p.ctx, targetHeight); err != nil && !errors.Is(err, ds.ErrNotFound) { |
| 137 | + return err |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return nil |
| 142 | +} |
| 143 | + |
| 144 | +// pruneRecoveryHistory prunes old state and execution metadata entries based on the configured retention depth. |
| 145 | +// It does not prunes old blocks, as those are handled by the pruning logic. |
| 146 | +// Pruning old state does not lose history but limit the ability to recover (replay or rollback) to the last HEAD-N blocks, where N is the retention depth. |
| 147 | +func (p *Pruner) pruneRecoveryHistory(ctx context.Context, retention uint64) error { |
| 148 | + if p.cfg.RecoveryHistoryDepth == 0 { |
| 149 | + return nil |
| 150 | + } |
| 151 | + |
| 152 | + height, err := p.store.Height(ctx) |
| 153 | + if err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + |
| 157 | + if height <= retention { |
| 158 | + return nil |
| 159 | + } |
| 160 | + |
| 161 | + target := height - retention |
| 162 | + if target <= p.lastPruned { |
| 163 | + return nil |
| 164 | + } |
| 165 | + |
| 166 | + // maxPruneBatch limits how many heights we prune per cycle to bound work. |
| 167 | + // it is callibrated to prune the last N blocks in one cycle, where N is the number of blocks produced in the defaultPruneInterval. |
| 168 | + blockTime := p.cfg.BlockTime.Duration |
| 169 | + if blockTime == 0 { |
| 170 | + blockTime = 1 |
| 171 | + } |
| 172 | + |
| 173 | + maxPruneBatch := max(uint64(defaultPruneInterval/blockTime), (target-p.lastPruned)/5) |
| 174 | + |
| 175 | + start := p.lastPruned + 1 |
| 176 | + end := target |
| 177 | + if end-start+1 > maxPruneBatch { |
| 178 | + end = start + maxPruneBatch - 1 |
| 179 | + } |
| 180 | + |
| 181 | + for h := start; h <= end; h++ { |
| 182 | + if err := p.store.DeleteStateAtHeight(ctx, h); err != nil && !errors.Is(err, ds.ErrNotFound) { |
| 183 | + return err |
| 184 | + } |
| 185 | + |
| 186 | + if p.execPruner != nil { |
| 187 | + if err := p.execPruner.PruneExec(ctx, h); err != nil && !errors.Is(err, ds.ErrNotFound) { |
| 188 | + return err |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + p.lastPruned = end |
| 194 | + return nil |
| 195 | +} |
0 commit comments