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

[pkg] panic if no space is left #3636

Merged
merged 15 commits into from
Oct 13, 2022
43 changes: 43 additions & 0 deletions pkg/watch/watch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

package watch
Copy link
Member

@Liuhaai Liuhaai Sep 27, 2022

Choose a reason for hiding this comment

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

package disk


import (
"context"
"time"

"github.com/shirou/gopsutil/v3/disk"
"go.uber.org/zap"

"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/routine"
)

// Start creates a timer task to check device per watchInternal
func Start(ctx context.Context, watchInternal time.Duration) func() {
Copy link
Member

Choose a reason for hiding this comment

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

func NewMonitor(time.Duration) *monitor
func (*monitor) Start()
func (*monitor) Stop()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK.

task := routine.NewRecurringTask(checkDiskSpace, watchInternal)
if err := task.Start(ctx); err != nil {
log.L().Panic("Failed to start watch disk space", zap.Error(err))
}
return func() {
Copy link
Member

Choose a reason for hiding this comment

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

why return a func() here?

if err := task.Stop(ctx); err != nil {
log.L().Panic("Failed to stop watch disk space.", zap.Error(err))
}
}
}

func checkDiskSpace() {
usage, err := disk.Usage("/")
if err != nil {
log.L().Error("Failed to get disk usage.", zap.Error(err))
return
}
// panic if left less than 2%
if usage.UsedPercent > 98.0 || usage.InodesUsedPercent > 98.0 {
log.L().Fatal("No space in device, pls clean up.", zap.Float64("UsedPercent", usage.UsedPercent), zap.Float64("InodesUsedPercent", usage.InodesUsedPercent))
}
}
26 changes: 26 additions & 0 deletions pkg/watch/watch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

package watch

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/iotexproject/iotex-core/testutil"
)

func TestStart(t *testing.T) {
require := require.New(t)
h := Start(context.Background(), 30*time.Millisecond)
require.NoError(testutil.WaitUntil(100*time.Millisecond, 1*time.Second, func() (b bool, e error) {
h()
return true, nil
}))
}
9 changes: 7 additions & 2 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os/signal"
"strings"
"syscall"
"time"

"github.com/iotexproject/go-pkgs/hash"
_ "go.uber.org/automaxprocs"
Expand All @@ -31,6 +32,7 @@ import (
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/probe"
"github.com/iotexproject/iotex-core/pkg/recovery"
"github.com/iotexproject/iotex-core/pkg/watch"
"github.com/iotexproject/iotex-core/server/itx"
)

Expand Down Expand Up @@ -74,8 +76,7 @@ func init() {

func main() {
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
signal.Notify(stop, syscall.SIGTERM)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
stopped := make(chan struct{})
livenessCtx, livenessCancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -103,6 +104,10 @@ func main() {
glog.Fatalln("Cannot config global logger, use default one: ", zap.Error(err))
}

// check device
stopWatch := watch.Start(ctx, 5*time.Minute)
defer stopWatch()
Copy link
Member

Choose a reason for hiding this comment

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

just call watch.Start() in server.Start(), and call watch.Stop() in server.Stop()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just check like heartbeat. server contains the major components like api blockchain etc...


if err = recovery.SetCrashlogDir(cfg.System.SystemLogDBPath); err != nil {
glog.Fatalln("Failed to set directory of crashlog: ", zap.Error(err))
}
Expand Down