wipe.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 snapshot
  17. import (
  18. "bytes"
  19. "time"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/rawdb"
  22. "github.com/ethereum/go-ethereum/ethdb"
  23. "github.com/ethereum/go-ethereum/log"
  24. "github.com/ethereum/go-ethereum/metrics"
  25. )
  26. // wipeSnapshot starts a goroutine to iterate over the entire key-value database
  27. // and delete all the data associated with the snapshot (accounts, storage,
  28. // metadata). After all is done, the snapshot range of the database is compacted
  29. // to free up unused data blocks.
  30. func wipeSnapshot(db ethdb.KeyValueStore, full bool) chan struct{} {
  31. // Wipe the snapshot root marker synchronously
  32. if full {
  33. rawdb.DeleteSnapshotRoot(db)
  34. }
  35. // Wipe everything else asynchronously
  36. wiper := make(chan struct{}, 1)
  37. go func() {
  38. if err := wipeContent(db); err != nil {
  39. log.Error("Failed to wipe state snapshot", "err", err) // Database close will trigger this
  40. return
  41. }
  42. close(wiper)
  43. }()
  44. return wiper
  45. }
  46. // wipeContent iterates over the entire key-value database and deletes all the
  47. // data associated with the snapshot (accounts, storage), but not the root hash
  48. // as the wiper is meant to run on a background thread but the root needs to be
  49. // removed in sync to avoid data races. After all is done, the snapshot range of
  50. // the database is compacted to free up unused data blocks.
  51. func wipeContent(db ethdb.KeyValueStore) error {
  52. if err := wipeKeyRange(db, "accounts", rawdb.SnapshotAccountPrefix, nil, nil, len(rawdb.SnapshotAccountPrefix)+common.HashLength, snapWipedAccountMeter, true); err != nil {
  53. return err
  54. }
  55. if err := wipeKeyRange(db, "storage", rawdb.SnapshotStoragePrefix, nil, nil, len(rawdb.SnapshotStoragePrefix)+2*common.HashLength, snapWipedStorageMeter, true); err != nil {
  56. return err
  57. }
  58. // Compact the snapshot section of the database to get rid of unused space
  59. start := time.Now()
  60. log.Info("Compacting snapshot account area ")
  61. end := common.CopyBytes(rawdb.SnapshotAccountPrefix)
  62. end[len(end)-1]++
  63. if err := db.Compact(rawdb.SnapshotAccountPrefix, end); err != nil {
  64. return err
  65. }
  66. log.Info("Compacting snapshot storage area ")
  67. end = common.CopyBytes(rawdb.SnapshotStoragePrefix)
  68. end[len(end)-1]++
  69. if err := db.Compact(rawdb.SnapshotStoragePrefix, end); err != nil {
  70. return err
  71. }
  72. log.Info("Compacted snapshot area in database", "elapsed", common.PrettyDuration(time.Since(start)))
  73. return nil
  74. }
  75. // wipeKeyRange deletes a range of keys from the database starting with prefix
  76. // and having a specific total key length. The start and limit is optional for
  77. // specifying a particular key range for deletion.
  78. //
  79. // Origin is included for wiping and limit is excluded if they are specified.
  80. func wipeKeyRange(db ethdb.KeyValueStore, kind string, prefix []byte, origin []byte, limit []byte, keylen int, meter metrics.Meter, report bool) error {
  81. // Batch deletions together to avoid holding an iterator for too long
  82. var (
  83. batch = db.NewBatch()
  84. items int
  85. )
  86. // Iterate over the key-range and delete all of them
  87. start, logged := time.Now(), time.Now()
  88. it := db.NewIterator(prefix, origin)
  89. var stop []byte
  90. if limit != nil {
  91. stop = append(prefix, limit...)
  92. }
  93. for it.Next() {
  94. // Skip any keys with the correct prefix but wrong length (trie nodes)
  95. key := it.Key()
  96. if !bytes.HasPrefix(key, prefix) {
  97. break
  98. }
  99. if len(key) != keylen {
  100. continue
  101. }
  102. if stop != nil && bytes.Compare(key, stop) >= 0 {
  103. break
  104. }
  105. // Delete the key and periodically recreate the batch and iterator
  106. batch.Delete(key)
  107. items++
  108. if items%10000 == 0 {
  109. // Batch too large (or iterator too long lived, flush and recreate)
  110. it.Release()
  111. if err := batch.Write(); err != nil {
  112. return err
  113. }
  114. batch.Reset()
  115. seekPos := key[len(prefix):]
  116. it = db.NewIterator(prefix, seekPos)
  117. if time.Since(logged) > 8*time.Second && report {
  118. log.Info("Deleting state snapshot leftovers", "kind", kind, "wiped", items, "elapsed", common.PrettyDuration(time.Since(start)))
  119. logged = time.Now()
  120. }
  121. }
  122. }
  123. it.Release()
  124. if err := batch.Write(); err != nil {
  125. return err
  126. }
  127. if meter != nil {
  128. meter.Mark(int64(items))
  129. }
  130. if report {
  131. log.Info("Deleted state snapshot leftovers", "kind", kind, "wiped", items, "elapsed", common.PrettyDuration(time.Since(start)))
  132. }
  133. return nil
  134. }