simplify tracker, prevent send to closed chan

This commit is contained in:
Roy Crihfield 2022-06-08 20:02:43 +08:00
parent 838dabcba6
commit ef391d8d31
2 changed files with 10 additions and 27 deletions

View File

@ -109,8 +109,7 @@ func (s *Service) CreateSnapshot(params SnapshotParams) error {
} }
headerID := header.Hash().String() headerID := header.Hash().String()
s.tracker = newTracker(s.recoveryFile, int(params.Workers)) s.tracker = newTracker(s.recoveryFile, int(params.Workers))
go s.tracker.run() s.tracker.captureSignal()
go s.tracker.captureSignal()
log.Infof("after goroutines start") log.Infof("after goroutines start")

View File

@ -22,7 +22,11 @@ type trackedIter struct {
func (it *trackedIter) Next(descend bool) bool { func (it *trackedIter) Next(descend bool) bool {
ret := it.NodeIterator.Next(descend) ret := it.NodeIterator.Next(descend)
if !ret { if !ret {
it.tracker.stopChan <- it if it.tracker.running {
it.tracker.stopChan <- it
} else {
log.Errorf("iterator stopped after tracker halted: path=%x", it.Path())
}
} }
return ret return ret
} }
@ -34,9 +38,7 @@ type iteratorTracker struct {
stopChan chan *trackedIter stopChan chan *trackedIter
started map[*trackedIter]struct{} started map[*trackedIter]struct{}
stopped []*trackedIter stopped []*trackedIter
running bool
haltChan chan struct{}
done chan struct{}
} }
func newTracker(file string, buf int) iteratorTracker { func newTracker(file string, buf int) iteratorTracker {
@ -45,8 +47,7 @@ func newTracker(file string, buf int) iteratorTracker {
startChan: make(chan *trackedIter, buf), startChan: make(chan *trackedIter, buf),
stopChan: make(chan *trackedIter, buf), stopChan: make(chan *trackedIter, buf),
started: map[*trackedIter]struct{}{}, started: map[*trackedIter]struct{}{},
haltChan: make(chan struct{}), running: true,
done: make(chan struct{}),
} }
} }
@ -62,23 +63,7 @@ func (tr *iteratorTracker) captureSignal() {
}() }()
} }
// listens for starts/stops and manages current state // Wraps an iterator in a trackedIter. This should not be called once halts are possible.
func (tr *iteratorTracker) run() {
loop:
for {
select {
case start := <-tr.startChan:
tr.started[start] = struct{}{}
case stop := <-tr.stopChan:
tr.stopped = append(tr.stopped, stop)
case <-tr.haltChan:
break loop
default:
}
}
tr.done <- struct{}{}
}
func (tr *iteratorTracker) tracked(it trie.NodeIterator) (ret *trackedIter) { func (tr *iteratorTracker) tracked(it trie.NodeIterator) (ret *trackedIter) {
ret = &trackedIter{it, tr} ret = &trackedIter{it, tr}
tr.startChan <- ret tr.startChan <- ret
@ -149,8 +134,7 @@ func (tr *iteratorTracker) restore(tree state.Trie) ([]trie.NodeIterator, error)
} }
func (tr *iteratorTracker) haltAndDump() error { func (tr *iteratorTracker) haltAndDump() error {
tr.haltChan <- struct{}{} tr.running = false
<-tr.done
// drain any pending events // drain any pending events
close(tr.startChan) close(tr.startChan)