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/geth: Graceful shutdown if disk is full #22103

Merged
merged 10 commits into from
Jan 19, 2021
2 changes: 1 addition & 1 deletion cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func startNode(ctx *cli.Context, stack *node.Node, backend ethapi.Backend) {
debug.Memsize.Add("node", stack)

// Start up the node itself
utils.StartNode(stack)
utils.StartNode(ctx, stack, backend.ChainConfig().ChainID)

// Unlock any account specifically requested
unlockAccounts(ctx, stack)
Expand Down
33 changes: 31 additions & 2 deletions cmd/utils/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,31 @@ import (
"compress/gzip"
"fmt"
"io"
"math/big"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/internal/debug"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/rlp"
"gopkg.in/urfave/cli.v1"
)

const (
importBatchSize = 2500
importBatchSize = 2500
freeDiskSpaceCritical = 300 * 1024 * 1024
alex347 marked this conversation as resolved.
Show resolved Hide resolved
)

// Fatalf formats a message to standard error and exits the program.
Expand All @@ -63,14 +68,20 @@ func Fatalf(format string, args ...interface{}) {
os.Exit(1)
}

func StartNode(stack *node.Node) {
func StartNode(ctx *cli.Context, stack *node.Node, chainID *big.Int) {
if err := stack.Start(); err != nil {
Fatalf("Error starting protocol stack: %v", err)
}
go func() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(sigc)

syncMode := *GlobalTextMarshaler(ctx, SyncModeFlag.Name).(*downloader.SyncMode)
if chainID.Int64() == 1 && syncMode != downloader.FastSync {
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 the light sync mode we want to make an exception for.

Still, I'm not 100% sure this is the best approach, I'll think about it.

go monitorFreeDiskSpace(sigc, stack.InstanceDir())
}

<-sigc
log.Info("Got interrupt, shutting down...")
go stack.Close()
Expand All @@ -85,6 +96,24 @@ func StartNode(stack *node.Node) {
}()
}

func monitorFreeDiskSpace(sigc chan os.Signal, path string) {
for {
freeSpace, err := getFreeDiskSpace(path)
if err != nil {
log.Warn("Failed to get free disk space", "path", path, "err", err)
break
}
if freeSpace < freeDiskSpaceCritical {
log.Error("Low disk space. Gracefully shutting down Geth to prevent database corruption.", "available", common.StorageSize(freeSpace))
sigc <- syscall.SIGTERM
break
} else if freeSpace < 2*freeDiskSpaceCritical {
log.Warn("Disk space is running low. Geth will shutdown if disk space runs below critical level.", "available", common.StorageSize(freeSpace), "critical_level", common.StorageSize(freeDiskSpaceCritical))
}
time.Sleep(60 * time.Second)
}
}

func ImportChain(chain *core.BlockChain, fn string) error {
// Watch for Ctrl-C while the import is running.
// If a signal is received, the import will stop at the next batch.
Expand Down
37 changes: 37 additions & 0 deletions cmd/utils/diskusage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2021 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

// +build !windows

package utils

import (
"fmt"

"golang.org/x/sys/unix"
)

var stat unix.Statfs_t

func getFreeDiskSpace(path string) (uint64, error) {

if err := unix.Statfs(path, &stat); err != nil {
return 0, fmt.Errorf("failed to call Statfs: %v", err)
}

// Available blocks * size per block = available space in bytes
return stat.Bavail * uint64(stat.Bsize), nil
}
38 changes: 38 additions & 0 deletions cmd/utils/diskusage_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2021 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package utils

import (
"fmt"

"golang.org/x/sys/windows"
)

func getFreeDiskSpace(path string) (uint64, error) {

cwd, err := windows.UTF16PtrFromString(path)
if err != nil {
return 0, fmt.Errorf("failed to call UTF16PtrFromString: %v", err)
}

var freeBytesAvailableToCaller, totalNumberOfBytes, totalNumberOfFreeBytes uint64
if err := windows.GetDiskFreeSpaceEx(cwd, &freeBytesAvailableToCaller, &totalNumberOfBytes, &totalNumberOfFreeBytes); err != nil {
return 0, fmt.Errorf("failed to call GetDiskFreeSpaceEx: %v", err)
}

return freeBytesAvailableToCaller, nil
}