Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth/downloader: flush state data before exit #16280

Merged
merged 2 commits into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions eth/downloader/statesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,19 @@ func (s *stateSync) Cancel() error {
// receive data from peers, rather those are buffered up in the downloader and
// pushed here async. The reason is to decouple processing from data receipt
// and timeouts.
func (s *stateSync) loop() error {
func (s *stateSync) loop() (err error) {
// Listen for new peer events to assign tasks to them
newPeer := make(chan *peerConnection, 1024)
peerSub := s.d.peers.SubscribeNewPeers(newPeer)
defer peerSub.Unsubscribe()
defer func() {
err = s.commit(true)
}()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will override any error being returned and most probably replace it with nil. You'd need to keep the old error intact and only override it if it was nil.


// Keep assigning new tasks until the sync completes or aborts
for s.sched.Pending() > 0 {
if err := s.commit(false); err != nil {
return err
if err = s.commit(false); err != nil {
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use naked returns, it's very hard to see what's being returned.

}
s.assignTasks()
// Tasks assigned, wait for something to happen
Expand All @@ -307,14 +310,14 @@ func (s *stateSync) loop() error {
s.d.dropPeer(req.peer.id)
}
// Process all the received blobs and check for stale delivery
if err := s.process(req); err != nil {
if err = s.process(req); err != nil {
log.Warn("Node data write error", "err", err)
return err
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use naked returns, it's very hard to see what's being returned.

}
req.peer.SetNodeDataIdle(len(req.response))
}
}
return s.commit(true)
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use naked returns, it's very hard to see what's being returned.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the previous logic called s.commit(true) when it reached this point, but you replaced it with s.commit(false) (in the defer), so the behavior changes.

Copy link
Member Author

@rjl493456442 rjl493456442 Mar 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry i can understand very well. I add the s.commit(true) in the defer because during the state node process procedure, some of the them can be invalid. If we return the error directly, we will lost the valid data committed before(which unreached the point will also be ignored).

}

func (s *stateSync) commit(force bool) error {
Expand All @@ -323,7 +326,10 @@ func (s *stateSync) commit(force bool) error {
}
start := time.Now()
b := s.d.stateDB.NewBatch()
s.sched.Commit(b)
// Ignore empty write.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this comment here, it's fairly obvious.

if written, err := s.sched.Commit(b); written == 0 || err != nil {
return err
}
if err := b.Write(); err != nil {
return fmt.Errorf("DB write error: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion trie/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (s *TrieSync) Process(results []SyncResult) (bool, int, error) {
}

// Commit flushes the data stored in the internal membatch out to persistent
// storage, returning th enumber of items written and any occurred error.
// storage, returning th number of items written and any occurred error.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"the number"

func (s *TrieSync) Commit(dbw ethdb.Putter) (int, error) {
// Dump the membatch into a database dbw
for i, key := range s.membatch.order {
Expand Down