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

cmd/utils: Handle graceful shutdown on low disk space #21884

Closed
wants to merge 2 commits into from
Closed
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
26 changes: 26 additions & 0 deletions cmd/utils/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func StartNode(stack *node.Node) {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(sigc)
go ensureSufficientMemory(sigc)
<-sigc
log.Info("Got interrupt, shutting down...")
go stack.Close()
Expand Down Expand Up @@ -312,3 +313,28 @@ func ExportPreimages(db ethdb.Database, fn string) error {
log.Info("Exported preimages", "file", fn)
return nil
}

func ensureSufficientMemory(sigc chan os.Signal) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's a bit confusing to have this method named ensureSufficientMemory, as it's disk-space, not RAM that's being checked.

Copy link
Author

@vyrwu vyrwu Nov 28, 2020

Choose a reason for hiding this comment

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

Addressed in c355ae4

for t := range time.Tick(5 * time.Second) {
select {
case <-sigc:
log.Info("Node is being terminated...")
default:
go func() {
var stat syscall.Statfs_t
wd, err := os.Getwd(); err != nil {
Fatalf("Error reading available memory of Node: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please avoid Fatalf -- that one causes an immediate os.Exit, which means it will almost certainly cause data loss and/or database corruption. We only use it in cases where things are already irrevocably broken beyond repair.
Just use log.Warn (and perhaps send a SIGTERM?)

Copy link
Author

Choose a reason for hiding this comment

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

Addressed in c355ae4

}
syscall.Statfs(wd, &stat)
MB = 1000000
avMemMB = stat.Bavail * uint64(stat.Bsize) / MB
if avMemMB < 100 {
log.Info("Available disk space is less than 100 MB. Gracefully shutting down to prevent database corruption.")
sigc <- syscall.SIGTERM
} else if avMemMB < 500 {
log.Warnf("Node is running low on memory. It will terminate if memory runs below 100MB. Remaining: %v MB.", avMemMB)
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, low on disk space ... if disk space runs below...

Copy link
Author

Choose a reason for hiding this comment

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

Addressed in c355ae4

}
}()
}
}
}