2022-02-18 11:12:53 +00:00
|
|
|
package snapshot
|
|
|
|
|
|
|
|
import (
|
2022-08-03 11:35:04 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2022-02-18 11:12:53 +00:00
|
|
|
"encoding/csv"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-03-11 14:28:32 +00:00
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2022-02-18 11:12:53 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2022-03-11 14:28:32 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-02-18 11:12:53 +00:00
|
|
|
|
2022-09-20 17:47:34 +00:00
|
|
|
iter "github.com/cerc-io/go-eth-state-node-iterator"
|
2022-02-18 11:12:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type trackedIter struct {
|
|
|
|
trie.NodeIterator
|
|
|
|
tracker *iteratorTracker
|
2022-08-03 11:35:04 +00:00
|
|
|
|
|
|
|
seekedPath []byte // latest path seeked from the tracked iterator
|
|
|
|
endPath []byte // endPath for the tracked iterator
|
2022-02-18 11:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (it *trackedIter) Next(descend bool) bool {
|
|
|
|
ret := it.NodeIterator.Next(descend)
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2022-02-18 11:12:53 +00:00
|
|
|
if !ret {
|
2022-06-08 12:02:43 +00:00
|
|
|
if it.tracker.running {
|
|
|
|
it.tracker.stopChan <- it
|
|
|
|
} else {
|
|
|
|
log.Errorf("iterator stopped after tracker halted: path=%x", it.Path())
|
|
|
|
}
|
2022-02-18 11:12:53 +00:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
type iteratorTracker struct {
|
2022-03-11 14:28:32 +00:00
|
|
|
recoveryFile string
|
|
|
|
|
2022-02-18 11:12:53 +00:00
|
|
|
startChan chan *trackedIter
|
|
|
|
stopChan chan *trackedIter
|
|
|
|
started map[*trackedIter]struct{}
|
|
|
|
stopped []*trackedIter
|
2022-06-08 12:02:43 +00:00
|
|
|
running bool
|
2022-02-18 11:12:53 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 14:28:32 +00:00
|
|
|
func newTracker(file string, buf int) iteratorTracker {
|
2022-02-18 11:12:53 +00:00
|
|
|
return iteratorTracker{
|
2022-03-11 14:28:32 +00:00
|
|
|
recoveryFile: file,
|
|
|
|
startChan: make(chan *trackedIter, buf),
|
|
|
|
stopChan: make(chan *trackedIter, buf),
|
|
|
|
started: map[*trackedIter]struct{}{},
|
2022-06-08 12:02:43 +00:00
|
|
|
running: true,
|
2022-02-18 11:12:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 11:35:04 +00:00
|
|
|
func (tr *iteratorTracker) captureSignal(cancelCtx context.CancelFunc) {
|
2022-03-11 14:28:32 +00:00
|
|
|
sigChan := make(chan os.Signal, 1)
|
|
|
|
|
2022-03-16 10:48:51 +00:00
|
|
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
2022-03-11 14:28:32 +00:00
|
|
|
go func() {
|
|
|
|
sig := <-sigChan
|
|
|
|
log.Errorf("Signal received (%v), stopping", sig)
|
2022-08-03 11:35:04 +00:00
|
|
|
// cancel context on receiving a signal
|
|
|
|
// on ctx cancellation, all the iterators complete processing of their current node before stopping
|
|
|
|
cancelCtx()
|
2022-03-11 14:28:32 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-06-08 12:02:43 +00:00
|
|
|
// Wraps an iterator in a trackedIter. This should not be called once halts are possible.
|
2022-08-03 11:35:04 +00:00
|
|
|
func (tr *iteratorTracker) tracked(it trie.NodeIterator, recoveredPath []byte) (ret *trackedIter) {
|
|
|
|
// create seeked path of max capacity (65)
|
|
|
|
iterSeekedPath := make([]byte, 0, 65)
|
|
|
|
// intially populate seeked path with the recovered path
|
|
|
|
// to be used in trie traversal
|
|
|
|
if recoveredPath != nil {
|
|
|
|
iterSeekedPath = append(iterSeekedPath, recoveredPath...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the iterator being tracked is a PrefixBoundIterator, capture it's end path
|
|
|
|
// to be used in trie traversal
|
|
|
|
var endPath []byte
|
|
|
|
if boundedIter, ok := it.(*iter.PrefixBoundIterator); ok {
|
|
|
|
endPath = boundedIter.EndPath
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = &trackedIter{it, tr, iterSeekedPath, endPath}
|
2022-02-18 11:12:53 +00:00
|
|
|
tr.startChan <- ret
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-03 11:35:04 +00:00
|
|
|
// explicitly stops an iterator
|
|
|
|
func (tr *iteratorTracker) stopIter(it *trackedIter) {
|
|
|
|
tr.stopChan <- it
|
|
|
|
}
|
|
|
|
|
2022-02-18 11:12:53 +00:00
|
|
|
// dumps iterator path and bounds to a text file so it can be restored later
|
2022-03-11 14:28:32 +00:00
|
|
|
func (tr *iteratorTracker) dump() error {
|
2022-06-08 12:08:17 +00:00
|
|
|
log.Debug("Dumping recovery state to: ", tr.recoveryFile)
|
2022-02-18 11:12:53 +00:00
|
|
|
var rows [][]string
|
2022-08-03 11:35:04 +00:00
|
|
|
for it := range tr.started {
|
|
|
|
var startPath []byte
|
2022-02-18 11:12:53 +00:00
|
|
|
var endPath []byte
|
|
|
|
if impl, ok := it.NodeIterator.(*iter.PrefixBoundIterator); ok {
|
2022-08-03 11:35:04 +00:00
|
|
|
// if the iterator being tracked is a PrefixBoundIterator,
|
|
|
|
// initialize start and end paths with its bounds
|
|
|
|
startPath = impl.StartPath
|
2022-02-18 11:12:53 +00:00
|
|
|
endPath = impl.EndPath
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
|
|
|
// if seeked path and iterator path are non-empty, use iterator's path as startpath
|
|
|
|
if !bytes.Equal(it.seekedPath, []byte{}) && !bytes.Equal(it.Path(), []byte{}) {
|
|
|
|
startPath = it.Path()
|
|
|
|
}
|
|
|
|
|
2022-02-18 11:12:53 +00:00
|
|
|
rows = append(rows, []string{
|
2022-08-03 11:35:04 +00:00
|
|
|
fmt.Sprintf("%x", startPath),
|
2022-02-18 11:12:53 +00:00
|
|
|
fmt.Sprintf("%x", endPath),
|
2022-08-03 11:35:04 +00:00
|
|
|
fmt.Sprintf("%x", it.seekedPath),
|
2022-02-18 11:12:53 +00:00
|
|
|
})
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2022-03-11 14:28:32 +00:00
|
|
|
file, err := os.Create(tr.recoveryFile)
|
2022-02-18 11:12:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
out := csv.NewWriter(file)
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2022-02-18 11:12:53 +00:00
|
|
|
return out.WriteAll(rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
// attempts to read iterator state from file
|
|
|
|
// if file doesn't exist, returns an empty slice with no error
|
2022-03-11 14:28:32 +00:00
|
|
|
func (tr *iteratorTracker) restore(tree state.Trie) ([]trie.NodeIterator, error) {
|
|
|
|
file, err := os.Open(tr.recoveryFile)
|
2022-02-18 11:12:53 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-08 12:08:17 +00:00
|
|
|
log.Debug("Restoring recovery state from: ", tr.recoveryFile)
|
2022-02-18 11:12:53 +00:00
|
|
|
defer file.Close()
|
|
|
|
in := csv.NewReader(file)
|
2022-08-03 11:35:04 +00:00
|
|
|
in.FieldsPerRecord = 3
|
2022-02-18 11:12:53 +00:00
|
|
|
rows, err := in.ReadAll()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2022-02-18 11:12:53 +00:00
|
|
|
var ret []trie.NodeIterator
|
|
|
|
for _, row := range rows {
|
|
|
|
// pick up where each interval left off
|
2022-08-03 11:35:04 +00:00
|
|
|
var startPath []byte
|
|
|
|
var endPath []byte
|
|
|
|
var recoveredPath []byte
|
|
|
|
|
|
|
|
if len(row[0]) != 0 {
|
|
|
|
if _, err = fmt.Sscanf(row[0], "%x", &startPath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(row[1]) != 0 {
|
|
|
|
if _, err = fmt.Sscanf(row[1], "%x", &endPath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(row[2]) != 0 {
|
|
|
|
if _, err = fmt.Sscanf(row[2], "%x", &recoveredPath); err != nil {
|
|
|
|
return nil, err
|
2022-02-18 11:12:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 11:35:04 +00:00
|
|
|
// force the lower bound path to an even length
|
|
|
|
// (required by HexToKeyBytes())
|
|
|
|
if len(startPath)&0b1 == 1 {
|
|
|
|
// decrement first to avoid skipped nodes
|
|
|
|
decrementPath(startPath)
|
|
|
|
startPath = append(startPath, 0)
|
2022-05-31 15:34:02 +00:00
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
|
|
|
it := iter.NewPrefixBoundIterator(tree.NodeIterator(iter.HexToKeyBytes(startPath)), startPath, endPath)
|
|
|
|
ret = append(ret, tr.tracked(it, recoveredPath))
|
2022-02-18 11:12:53 +00:00
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 14:28:32 +00:00
|
|
|
func (tr *iteratorTracker) haltAndDump() error {
|
2022-06-08 12:02:43 +00:00
|
|
|
tr.running = false
|
2022-02-18 11:12:53 +00:00
|
|
|
|
2022-08-03 11:35:04 +00:00
|
|
|
// drain any pending iterators
|
2022-02-18 11:12:53 +00:00
|
|
|
close(tr.startChan)
|
|
|
|
for start := range tr.startChan {
|
|
|
|
tr.started[start] = struct{}{}
|
|
|
|
}
|
|
|
|
close(tr.stopChan)
|
|
|
|
for stop := range tr.stopChan {
|
|
|
|
tr.stopped = append(tr.stopped, stop)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, stop := range tr.stopped {
|
|
|
|
delete(tr.started, stop)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tr.started) == 0 {
|
|
|
|
// if the tracker state is empty, erase any existing recovery file
|
2022-03-11 14:28:32 +00:00
|
|
|
err := os.Remove(tr.recoveryFile)
|
2022-02-18 11:12:53 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2022-08-03 11:35:04 +00:00
|
|
|
|
2022-03-11 14:28:32 +00:00
|
|
|
return tr.dump()
|
2022-02-18 11:12:53 +00:00
|
|
|
}
|