Skip to content

Commit

Permalink
Remove pebble avametrics (#1769)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronbuchwald authored Nov 15, 2024
1 parent 75a8ac0 commit 3472c5e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 45 deletions.
5 changes: 3 additions & 2 deletions api/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/prometheus/client_golang/prometheus"

"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/codec"
Expand Down Expand Up @@ -45,11 +46,11 @@ func NewIndexer(path string, parser chain.Parser, blockWindow uint64) (*Indexer,
if blockWindow > maxBlockWindow {
return nil, fmt.Errorf("block window %d exceeds maximum %d", blockWindow, maxBlockWindow)
}
txDB, _, err := pebble.New(filepath.Join(path, "tx"), pebble.NewDefaultConfig())
txDB, err := pebble.New(filepath.Join(path, "tx"), pebble.NewDefaultConfig(), prometheus.NewRegistry())
if err != nil {
return nil, err
}
blockDB, _, err := pebble.New(filepath.Join(path, "block"), pebble.NewDefaultConfig())
blockDB, err := pebble.New(filepath.Join(path, "block"), pebble.NewDefaultConfig(), prometheus.NewRegistry())
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cli

import (
"github.com/ava-labs/avalanchego/database"
"github.com/prometheus/client_golang/prometheus"

"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/codec"
Expand All @@ -26,7 +27,7 @@ type Handler struct {
}

func New(c Controller) (*Handler, error) {
db, _, err := pebble.New(c.DatabasePath(), pebble.NewDefaultConfig())
db, err := pebble.New(c.DatabasePath(), pebble.NewDefaultConfig(), prometheus.NewRegistry())
if err != nil {
return nil, err
}
Expand Down
58 changes: 33 additions & 25 deletions internal/pebble/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package pebble
import (
"time"

"github.com/ava-labs/avalanchego/utils/metric"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/cockroachdb/pebble"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -16,9 +15,12 @@ const metricsInterval = 10 * time.Second

type metrics struct {
delayStart time.Time
writeStall metric.Averager

getLatency metric.Averager
writeStallCount prometheus.Counter
writeStallSum prometheus.Gauge

getCount prometheus.Counter
getSum prometheus.Gauge

l0Compactions prometheus.Counter
otherCompactions prometheus.Counter
Expand All @@ -33,27 +35,28 @@ type metrics struct {
obsoleteWALCount prometheus.Gauge
}

func newMetrics() (*prometheus.Registry, *metrics, error) {
r := prometheus.NewRegistry()
writeStall, err := metric.NewAverager(
"pebble_write_stall",
"time spent waiting for disk write",
r,
)
if err != nil {
return nil, nil, err
}
getLatency, err := metric.NewAverager(
"pebble_read_latency",
"time spent waiting for db get",
r,
)
if err != nil {
return nil, nil, err
}
func newMetrics(r prometheus.Registerer) (*metrics, error) {
m := &metrics{
writeStall: writeStall,
getLatency: getLatency,
writeStallCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "pebble",
Name: "write_stall_count",
Help: "number of write stalls",
}),
writeStallSum: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "pebble",
Name: "write_stall_sum",
Help: "total time spent in write stalls",
}),
getCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "pebble",
Name: "get_count",
Help: "number of get operations",
}),
getSum: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "pebble",
Name: "get_sum",
Help: "total time spent in get operations",
}),
l0Compactions: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "pebble",
Name: "l0_compactions",
Expand Down Expand Up @@ -107,6 +110,10 @@ func newMetrics() (*prometheus.Registry, *metrics, error) {
}
errs := wrappers.Errs{}
errs.Add(
r.Register(m.writeStallCount),
r.Register(m.writeStallSum),
r.Register(m.getCount),
r.Register(m.getSum),
r.Register(m.l0Compactions),
r.Register(m.otherCompactions),
r.Register(m.activeCompactions),
Expand All @@ -118,7 +125,7 @@ func newMetrics() (*prometheus.Registry, *metrics, error) {
r.Register(m.obsoleteWALSize),
r.Register(m.obsoleteWALCount),
)
return r, m, errs.Err
return m, errs.Err
}

func (db *Database) onCompactionBegin(info pebble.CompactionInfo) {
Expand All @@ -140,7 +147,8 @@ func (db *Database) onWriteStallBegin(pebble.WriteStallBeginInfo) {
}

func (db *Database) onWriteStallEnd() {
db.metrics.writeStall.Observe(float64(time.Since(db.metrics.delayStart)))
db.metrics.writeStallCount.Inc()
db.metrics.writeStallSum.Add(float64(time.Since(db.metrics.delayStart)))
}

func (db *Database) collectMetrics() {
Expand Down
13 changes: 7 additions & 6 deletions internal/pebble/pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewDefaultConfig() Config {
}

// TODO: replace with AvalancheGo implementation (if still used for Vryx)
func New(file string, cfg Config) (*Database, *prometheus.Registry, error) {
func New(file string, cfg Config, registerer prometheus.Registerer) (*Database, error) {
// These default settings are based on https://github.com/ethereum/go-ethereum/blob/master/ethdb/pebble/pebble.go
d := &Database{closing: make(chan struct{})}
opts := &pebble.Options{
Expand Down Expand Up @@ -91,18 +91,18 @@ func New(file string, cfg Config) (*Database, *prometheus.Registry, error) {
}
}
opts.Experimental.ReadSamplingMultiplier = -1 // explicitly disable seek compaction
registry, metrics, err := newMetrics()
metrics, err := newMetrics(registerer)
if err != nil {
return nil, nil, err
return nil, err
}
d.metrics = metrics
db, err := pebble.Open(file, opts)
if err != nil {
return nil, nil, err
return nil, err
}
d.db = db
go d.collectMetrics()
return d, registry, nil
return d, nil
}

func (db *Database) Close() error {
Expand Down Expand Up @@ -133,7 +133,8 @@ func (db *Database) Has(key []byte) (bool, error) {
func (db *Database) Get(key []byte) ([]byte, error) {
start := time.Now()
data, closer, err := db.db.Get(key)
db.metrics.getLatency.Observe(float64(time.Since(start)))
db.metrics.getCount.Inc()
db.metrics.getSum.Add(float64(time.Since(start)))
if err != nil {
return nil, updateError(err)
}
Expand Down
10 changes: 3 additions & 7 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,24 @@
package storage

import (
"github.com/ava-labs/avalanchego/api/metrics"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/corruptabledb"
"github.com/prometheus/client_golang/prometheus"

"github.com/ava-labs/hypersdk/internal/pebble"
"github.com/ava-labs/hypersdk/utils"
)

func New(cfg pebble.Config, chainDataDir string, namespace string, gatherer metrics.MultiGatherer) (database.Database, error) {
func New(cfg pebble.Config, chainDataDir string, namespace string, registerer prometheus.Registerer) (database.Database, error) {
path, err := utils.InitSubDirectory(chainDataDir, namespace)
if err != nil {
return nil, err
}

db, registry, err := pebble.New(path, cfg)
db, err := pebble.New(path, cfg, registerer)
if err != nil {
return nil, err
}

if err := gatherer.Register(namespace, registry); err != nil {
return nil, err
}

return corruptabledb.New(db), nil
}
12 changes: 10 additions & 2 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,21 @@ func (vm *VM) Initialize(
return fmt.Errorf("failed to initialize p2p: %w", err)
}

blockDBRegistry := prometheus.NewRegistry()
if err := vm.snowCtx.Metrics.Register("blockdb", blockDBRegistry); err != nil {
return fmt.Errorf("failed to register blockdb metrics: %w", err)
}
pebbleConfig := pebble.NewDefaultConfig()
vm.vmDB, err = storage.New(pebbleConfig, vm.snowCtx.ChainDataDir, blockDB, vm.snowCtx.Metrics)
vm.vmDB, err = storage.New(pebbleConfig, vm.snowCtx.ChainDataDir, blockDB, blockDBRegistry)
if err != nil {
return err
}

vm.rawStateDB, err = storage.New(pebbleConfig, vm.snowCtx.ChainDataDir, stateDB, vm.snowCtx.Metrics)
rawStateDBRegistry := prometheus.NewRegistry()
if err := vm.snowCtx.Metrics.Register("rawstatedb", rawStateDBRegistry); err != nil {
return fmt.Errorf("failed to register rawstatedb metrics: %w", err)
}
vm.rawStateDB, err = storage.New(pebbleConfig, vm.snowCtx.ChainDataDir, stateDB, rawStateDBRegistry)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions x/dsmr/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"

"github.com/ava-labs/hypersdk/codec"
Expand Down Expand Up @@ -75,7 +76,7 @@ func createTestStorage(t *testing.T, numValidChunks, numInvalidChunks int) (

tempDir := t.TempDir()

db, _, err := pebble.New(tempDir, pebble.NewDefaultConfig())
db, err := pebble.New(tempDir, pebble.NewDefaultConfig(), prometheus.NewRegistry())
require.NoError(err)
validChunkIDs := make([]ids.ID, 0, numValidChunks)
for _, chunk := range validChunks {
Expand All @@ -91,7 +92,7 @@ func createTestStorage(t *testing.T, numValidChunks, numInvalidChunks int) (

restart := func() *chunkStorage[tx] {
require.NoError(db.Close())
db, _, err = pebble.New(tempDir, pebble.NewDefaultConfig())
db, err = pebble.New(tempDir, pebble.NewDefaultConfig(), prometheus.NewRegistry())
require.NoError(err)

storage, err := newChunkStorage[tx](
Expand Down

0 comments on commit 3472c5e

Please sign in to comment.