2022-08-19 23:18:31 +00:00
|
|
|
package tracker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/csv"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2023-07-21 12:34:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2022-08-19 23:18:31 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
|
|
|
2023-07-21 12:34:58 +00:00
|
|
|
iter "github.com/cerc-io/eth-iterator-utils"
|
2022-08-19 23:18:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Tracker struct {
|
|
|
|
recoveryFile string
|
|
|
|
|
|
|
|
startChan chan *Iterator
|
|
|
|
stopChan chan *Iterator
|
|
|
|
started map[*Iterator]struct{}
|
|
|
|
stopped []*Iterator
|
|
|
|
running bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type Iterator struct {
|
|
|
|
trie.NodeIterator
|
|
|
|
tracker *Tracker
|
|
|
|
}
|
|
|
|
|
2023-07-21 12:34:58 +00:00
|
|
|
func New(file string, bufsize uint) Tracker {
|
2022-08-19 23:18:31 +00:00
|
|
|
return Tracker{
|
|
|
|
recoveryFile: file,
|
2023-07-21 12:34:58 +00:00
|
|
|
startChan: make(chan *Iterator, bufsize),
|
|
|
|
stopChan: make(chan *Iterator, bufsize),
|
2022-08-19 23:18:31 +00:00
|
|
|
started: map[*Iterator]struct{}{},
|
|
|
|
running: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tr *Tracker) CaptureSignal(cancelCtx context.CancelFunc) {
|
|
|
|
sigChan := make(chan os.Signal, 1)
|
|
|
|
|
|
|
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
sig := <-sigChan
|
2023-07-21 12:34:58 +00:00
|
|
|
log.Error("Signal received (%v), stopping", "signal", sig)
|
2023-08-29 09:00:18 +00:00
|
|
|
// Cancel context on receiving a signal. On cancellation, all tracked iterators complete
|
|
|
|
// processing of their current node before stopping.
|
2022-08-19 23:18:31 +00:00
|
|
|
cancelCtx()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-07-21 12:34:58 +00:00
|
|
|
// Tracked wraps an iterator in a Iterator. This should not be called once halts are possible.
|
|
|
|
func (tr *Tracker) Tracked(it trie.NodeIterator) (ret *Iterator) {
|
|
|
|
ret = &Iterator{it, tr}
|
2022-08-19 23:18:31 +00:00
|
|
|
tr.startChan <- ret
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-21 12:34:58 +00:00
|
|
|
// StopIterator explicitly stops an iterator
|
2022-08-19 23:18:31 +00:00
|
|
|
func (tr *Tracker) StopIterator(it *Iterator) {
|
|
|
|
tr.stopChan <- it
|
|
|
|
}
|
|
|
|
|
|
|
|
// dumps iterator path and bounds to a text file so it can be restored later
|
|
|
|
func (tr *Tracker) dump() error {
|
2023-07-21 12:34:58 +00:00
|
|
|
log.Debug("Dumping recovery state", "to", tr.recoveryFile)
|
2022-08-19 23:18:31 +00:00
|
|
|
var rows [][]string
|
|
|
|
for it := range tr.started {
|
|
|
|
var endPath []byte
|
|
|
|
if impl, ok := it.NodeIterator.(*iter.PrefixBoundIterator); ok {
|
|
|
|
endPath = impl.EndPath
|
|
|
|
}
|
|
|
|
|
|
|
|
rows = append(rows, []string{
|
2023-07-21 12:34:58 +00:00
|
|
|
fmt.Sprintf("%x", it.Path()),
|
2022-08-19 23:18:31 +00:00
|
|
|
fmt.Sprintf("%x", endPath),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Create(tr.recoveryFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
out := csv.NewWriter(file)
|
|
|
|
|
|
|
|
return out.WriteAll(rows)
|
|
|
|
}
|
|
|
|
|
2023-08-29 09:00:18 +00:00
|
|
|
// Restore attempts to read iterator state from the recovery file.
|
|
|
|
// If the file doesn't exist, returns an empty slice with no error.
|
|
|
|
// Restored iterators are constructed in the same order as in the returned slice.
|
2023-07-21 12:34:58 +00:00
|
|
|
func (tr *Tracker) Restore(makeIterator iter.IteratorConstructor) ([]trie.NodeIterator, error) {
|
2022-08-19 23:18:31 +00:00
|
|
|
file, err := os.Open(tr.recoveryFile)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2023-07-21 12:34:58 +00:00
|
|
|
log.Debug("Restoring recovery state", "from", tr.recoveryFile)
|
|
|
|
|
2022-08-19 23:18:31 +00:00
|
|
|
in := csv.NewReader(file)
|
2023-07-21 12:34:58 +00:00
|
|
|
in.FieldsPerRecord = 2
|
2022-08-19 23:18:31 +00:00
|
|
|
rows, err := in.ReadAll()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ret []trie.NodeIterator
|
|
|
|
for _, row := range rows {
|
2023-07-21 12:34:58 +00:00
|
|
|
// pick up where each recovered iterator left off
|
2022-08-19 23:18:31 +00:00
|
|
|
var recoveredPath []byte
|
2023-07-21 12:34:58 +00:00
|
|
|
var endPath []byte
|
2022-08-19 23:18:31 +00:00
|
|
|
|
|
|
|
if len(row[0]) != 0 {
|
2023-07-21 12:34:58 +00:00
|
|
|
if _, err = fmt.Sscanf(row[0], "%x", &recoveredPath); err != nil {
|
2022-08-19 23:18:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(row[1]) != 0 {
|
|
|
|
if _, err = fmt.Sscanf(row[1], "%x", &endPath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// force the lower bound path to an even length (required by geth API/HexToKeyBytes)
|
2023-07-21 12:34:58 +00:00
|
|
|
if len(recoveredPath)&0b1 == 1 {
|
2022-08-19 23:18:31 +00:00
|
|
|
// decrement first to avoid skipped nodes
|
2023-07-21 12:34:58 +00:00
|
|
|
decrementPath(recoveredPath)
|
|
|
|
recoveredPath = append(recoveredPath, 0)
|
2022-08-19 23:18:31 +00:00
|
|
|
}
|
2023-07-21 12:34:58 +00:00
|
|
|
it := makeIterator(iter.HexToKeyBytes(recoveredPath))
|
|
|
|
boundIt := iter.NewPrefixBoundIterator(it, endPath)
|
|
|
|
ret = append(ret, tr.Tracked(boundIt))
|
2022-08-19 23:18:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 12:34:58 +00:00
|
|
|
log.Debug("Restored iterators", "count", len(ret))
|
2022-08-19 23:18:31 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tr *Tracker) HaltAndDump() error {
|
|
|
|
tr.running = false
|
|
|
|
|
|
|
|
// drain any pending iterators
|
|
|
|
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
|
|
|
|
err := os.Remove(tr.recoveryFile)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tr.dump()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *Iterator) Next(descend bool) bool {
|
|
|
|
ret := it.NodeIterator.Next(descend)
|
|
|
|
|
|
|
|
if !ret {
|
|
|
|
if it.tracker.running {
|
|
|
|
it.tracker.stopChan <- it
|
|
|
|
} else {
|
2023-07-21 12:34:58 +00:00
|
|
|
log.Error("Iterator stopped after tracker halted", "path", it.Path())
|
2022-08-19 23:18:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subtracts 1 from the last byte in a path slice, carrying if needed.
|
2023-08-29 09:00:18 +00:00
|
|
|
// Does nothing, returning false, for all-zero inputs (underflow).
|
2022-08-19 23:18:31 +00:00
|
|
|
func decrementPath(path []byte) bool {
|
|
|
|
// check for all zeros
|
|
|
|
allzero := true
|
|
|
|
for i := 0; i < len(path); i++ {
|
|
|
|
allzero = allzero && path[i] == 0
|
|
|
|
}
|
|
|
|
if allzero {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := len(path) - 1; i >= 0; i-- {
|
|
|
|
val := path[i]
|
|
|
|
path[i]--
|
|
|
|
if val == 0 {
|
|
|
|
path[i] = 0xf
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|