pruner.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package les
  17. import (
  18. "sync"
  19. "time"
  20. "github.com/ethereum/go-ethereum/common/math"
  21. "github.com/ethereum/go-ethereum/core"
  22. "github.com/ethereum/go-ethereum/ethdb"
  23. "github.com/ethereum/go-ethereum/log"
  24. )
  25. // pruner is responsible for pruning historical light chain data.
  26. type pruner struct {
  27. db ethdb.Database
  28. indexers []*core.ChainIndexer
  29. closeCh chan struct{}
  30. wg sync.WaitGroup
  31. }
  32. // newPruner returns a light chain pruner instance.
  33. func newPruner(db ethdb.Database, indexers ...*core.ChainIndexer) *pruner {
  34. pruner := &pruner{
  35. db: db,
  36. indexers: indexers,
  37. closeCh: make(chan struct{}),
  38. }
  39. pruner.wg.Add(1)
  40. go pruner.loop()
  41. return pruner
  42. }
  43. // close notifies all background goroutines belonging to pruner to exit.
  44. func (p *pruner) close() {
  45. close(p.closeCh)
  46. p.wg.Wait()
  47. }
  48. // loop periodically queries the status of chain indexers and prunes useless
  49. // historical chain data. Notably, whenever Geth restarts, it will iterate
  50. // all historical sections even they don't exist at all(below checkpoint) so
  51. // that light client can prune cached chain data that was ODRed after pruning
  52. // that section.
  53. func (p *pruner) loop() {
  54. defer p.wg.Done()
  55. // cleanTicker is the ticker used to trigger a history clean 2 times a day.
  56. var cleanTicker = time.NewTicker(12 * time.Hour)
  57. defer cleanTicker.Stop()
  58. // pruning finds the sections that have been processed by all indexers
  59. // and deletes all historical chain data.
  60. // Note, if some indexers don't support pruning(e.g. eth.BloomIndexer),
  61. // pruning operations can be silently ignored.
  62. pruning := func() {
  63. min := uint64(math.MaxUint64)
  64. for _, indexer := range p.indexers {
  65. sections, _, _ := indexer.Sections()
  66. if sections < min {
  67. min = sections
  68. }
  69. }
  70. // Always keep the latest section data in database.
  71. if min < 2 || len(p.indexers) == 0 {
  72. return
  73. }
  74. for _, indexer := range p.indexers {
  75. if err := indexer.Prune(min - 2); err != nil {
  76. log.Debug("Failed to prune historical data", "err", err)
  77. return
  78. }
  79. }
  80. p.db.Compact(nil, nil) // Compact entire database, ensure all removed data are deleted.
  81. }
  82. for {
  83. pruning()
  84. select {
  85. case <-cleanTicker.C:
  86. case <-p.closeCh:
  87. return
  88. }
  89. }
  90. }