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

[blockchain] Add database file size metric #4185

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var (
},
[]string{"type"},
)

// ErrInvalidTipHeight is the error returned when the block height is not valid
ErrInvalidTipHeight = errors.New("invalid tip height")
// ErrInvalidBlock is the error returned when the block is not valid
Expand Down Expand Up @@ -190,7 +191,6 @@ func NewBlockchain(cfg Config, g genesis.Genesis, dao blockdao.BlockDAO, bbf Blo
}
chain.lifecycle.Add(chain.dao)
chain.lifecycle.Add(chain.pubSubManager)

return chain
}

Expand Down
34 changes: 28 additions & 6 deletions db/db_bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import (
"bytes"
"context"
"os"
"syscall"
"time"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -18,10 +20,14 @@
"github.com/iotexproject/iotex-core/db/batch"
"github.com/iotexproject/iotex-core/pkg/lifecycle"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/routine"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
)

const _fileMode = 0600
const (
_fileMode = 0600
_fileSizeCollectInterval = 5 * time.Minute
)

var (
// ErrDBNotStarted represents the error when a db has not started
Expand All @@ -40,9 +46,10 @@
// BoltDB is KVStore implementation based bolt DB
type BoltDB struct {
lifecycle.Readiness
db *bolt.DB
path string
config Config
db *bolt.DB
path string
config Config
collectTask *routine.RecurringTask
}

// NewBoltDB instantiates an BoltDB with implements KVStore
Expand All @@ -55,7 +62,7 @@
}

// Start opens the BoltDB (creates new file if not existing yet)
func (b *BoltDB) Start(_ context.Context) error {
func (b *BoltDB) Start(ctx context.Context) error {
opts := *bolt.DefaultOptions
if b.config.ReadOnly {
opts.ReadOnly = true
Expand All @@ -65,14 +72,29 @@
return errors.Wrap(ErrIO, err.Error())
}
b.db = db
// start a recurring task to collect db filesize metrics
b.collectTask = routine.NewRecurringTask(func() {
info, err := os.Stat(b.path)
if err != nil {
log.L().Warn("Failed to get file size.", zap.Error(err))
return

Check warning on line 80 in db/db_bolt.go

View check run for this annotation

Codecov / codecov/patch

db/db_bolt.go#L77-L80

Added lines #L77 - L80 were not covered by tests
}
boltdbMtc.WithLabelValues(b.path, "fileSize").Set(float64(info.Size()))

Check warning on line 82 in db/db_bolt.go

View check run for this annotation

Codecov / codecov/patch

db/db_bolt.go#L82

Added line #L82 was not covered by tests
}, _fileSizeCollectInterval)
if err = b.collectTask.Start(ctx); err != nil {
return err
}
return b.TurnOn()
}

// Stop closes the BoltDB
func (b *BoltDB) Stop(_ context.Context) error {
func (b *BoltDB) Stop(ctx context.Context) error {
if err := b.TurnOff(); err != nil {
return err
}
if err := b.collectTask.Stop(ctx); err != nil {
return err
}
if err := b.db.Close(); err != nil {
return errors.Wrap(ErrIO, err.Error())
}
Expand Down
Loading