pruner.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2019 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. // pruning finds the sections that have been processed by all indexers
  58. // and deletes all historical chain data.
  59. // Note, if some indexers don't support pruning(e.g. eth.BloomIndexer),
  60. // pruning operations can be silently ignored.
  61. pruning := func() {
  62. min := uint64(math.MaxUint64)
  63. for _, indexer := range p.indexers {
  64. sections, _, _ := indexer.Sections()
  65. if sections < min {
  66. min = sections
  67. }
  68. }
  69. // Always keep the latest section data in database.
  70. if min < 2 || len(p.indexers) == 0 {
  71. return
  72. }
  73. for _, indexer := range p.indexers {
  74. if err := indexer.Prune(min - 2); err != nil {
  75. log.Debug("Failed to prune historical data", "err", err)
  76. return
  77. }
  78. }
  79. p.db.Compact(nil, nil) // Compact entire database, ensure all removed data are deleted.
  80. }
  81. for {
  82. pruning()
  83. select {
  84. case <-cleanTicker.C:
  85. case <-p.closeCh:
  86. return
  87. }
  88. }
  89. }