-
Notifications
You must be signed in to change notification settings - Fork 324
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
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6dc29de
add fsnotify
huof6829 0bbce28
Merge branch 'master' into panic_no_space
huof6829 d0d57a4
modify as 5-minutes timer loop
huof6829 e4688a8
add comment
huof6829 7c3bf2d
add comment
huof6829 dc643c7
move to maintainment of StartServer
huof6829 2d5eac7
Merge branch 'master' into panic_no_space
huof6829 0fd1091
Merge branch 'master' into panic_no_space
huof6829 9f3b564
add BenchmarkCheckSpace
huof6829 2d38686
new struct
huof6829 259f084
Merge branch 'master' into panic_no_space
huof6829 7919ab0
modify comments
huof6829 41f1a09
Merge branch 'master' into panic_no_space
huof6829 b3d8b12
check db partion
huof6829 b6bbdd2
Merge branch 'master' into panic_no_space
huof6829 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// 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 disk | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/shirou/gopsutil/v3/disk" | ||
"go.uber.org/zap" | ||
|
||
"github.com/iotexproject/iotex-core/pkg/log" | ||
"github.com/iotexproject/iotex-core/pkg/routine" | ||
"github.com/iotexproject/iotex-core/pkg/util/fileutil" | ||
) | ||
|
||
// Monitor represents a timer task of checking disk | ||
type Monitor struct { | ||
task *routine.RecurringTask | ||
path string | ||
} | ||
|
||
// NewMonitor creates an instance of timer task | ||
func NewMonitor(path string, internal time.Duration) (*Monitor, error) { | ||
if !fileutil.FileExists(path) { | ||
return nil, errors.Errorf("invalid file path %s", path) | ||
} | ||
m := &Monitor{path: path} | ||
m.task = routine.NewRecurringTask(m.checkDiskSpace, internal) | ||
return m, nil | ||
} | ||
|
||
// Start starts timer task | ||
func (m *Monitor) Start(ctx context.Context) error { | ||
return m.task.Start(ctx) | ||
} | ||
|
||
// Stop stops timer task | ||
func (m *Monitor) Stop(ctx context.Context) error { | ||
return m.task.Stop(ctx) | ||
} | ||
|
||
func (m *Monitor) checkDiskSpace() { | ||
usage, err := disk.Usage(m.path) | ||
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.", zap.Float64("UsedPercent", usage.UsedPercent), zap.Float64("InodesUsedPercent", usage.InodesUsedPercent)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// 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 disk | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/iotexproject/iotex-core/testutil" | ||
) | ||
|
||
func TestMonitor(t *testing.T) { | ||
require := require.New(t) | ||
m, err := NewMonitor(t.TempDir(), 30*time.Millisecond) | ||
require.NoError(err) | ||
ctx := context.Background() | ||
err = m.Start(ctx) | ||
require.NoError(err) | ||
require.NoError(testutil.WaitUntil(100*time.Millisecond, 1*time.Second, func() (b bool, e error) { | ||
err = m.Stop(ctx) | ||
return err == nil, nil | ||
})) | ||
} | ||
|
||
func BenchmarkCheckSpace(b *testing.B) { | ||
m := &Monitor{path: b.TempDir()} | ||
// BenchmarkCheckSpace-4 522752 2069 ns/op | ||
for i := 0; i < b.N; i++ { | ||
m.checkDiskSpace() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if disk is 20TB, when there is %2=200GB of free disk, can't it be used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leave enough disk space is required when disk run out soon.