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

refactor(v2): use non global logger #1037

Merged
merged 4 commits into from
Jan 14, 2025
Merged
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
13 changes: 13 additions & 0 deletions v2/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog

## Unreleased

* [#1037](https://github.com/cosmos/iavl/pull/1037) Swap `zerolog` for internal logger interface

## [v2.0.0-alpha.4](https://github.com/cosmos/iavl/releases/tag/v2.0.0-alpha.4)

* Remove debug prints

## [v2.0.0-alpha.3](https://github.com/cosmos/iavl/releases/tag/v2.0.0-alpha.3)

* Initial tag.
1 change: 1 addition & 0 deletions v2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# IAVL v2
26 changes: 11 additions & 15 deletions v2/cmd/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@ import (
"github.com/dustin/go-humanize"
"github.com/kocubinski/costor-api/compact"
"github.com/kocubinski/costor-api/core"
"github.com/rs/zerolog"
zlog "github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

var log = zlog.Output(zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.Stamp,
})
var log = iavl.NewTestLogger()

func Command() *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -75,9 +70,10 @@ func emitCommand() *cobra.Command {
go func() {
stats, err := stream.Compact()
if err != nil {
log.Fatal().Err(err).Msg("failed to compact")
log.Error("failed to compact", "error", err)
os.Exit(1)
}
log.Info().Msgf(stats.Report())
log.Info(stats.Report())
wg.Done()
}()

Expand All @@ -95,13 +91,13 @@ func emitCommand() *cobra.Command {

if itr.Version() < int64(start) {
if cnt%5_000_000 == 0 {
log.Info().Msgf("fast forward version=%d nodes=%s", itr.Version(), humanize.Comma(cnt))
log.Info(fmt.Sprintf("fast forward version=%d nodes=%s", itr.Version(), humanize.Comma(cnt)))
}
continue
}

if cnt%500_000 == 0 {
log.Info().Msgf("version=%d nodes=%s", itr.Version(), humanize.Comma(cnt))
log.Info(fmt.Sprintf("version=%d nodes=%s", itr.Version(), humanize.Comma(cnt)))
}

select {
Expand Down Expand Up @@ -149,11 +145,11 @@ func treeCommand() *cobra.Command {
Use: "tree",
Short: "build and save a Tree to disk, taking generated changesets as input",
RunE: func(cmd *cobra.Command, args []string) error {
multiTree := iavl.NewMultiTree(dbPath, iavl.TreeOptions{StateStorage: true})
multiTree := iavl.NewMultiTree(iavl.NewTestLogger(), dbPath, iavl.TreeOptions{StateStorage: true})
defer func(mt *iavl.MultiTree) {
err := mt.Close()
if err != nil {
log.Error().Err(err).Msg("failed to close db")
log.Error("failed to close db", "error", err)
}
}(multiTree)

Expand Down Expand Up @@ -203,12 +199,12 @@ func treeCommand() *cobra.Command {

i++
if i%100_000 == 0 {
log.Info().Msgf("leaves=%s dur=%s rate=%s version=%d",
log.Info(fmt.Sprintf("leaves=%s dur=%s rate=%s version=%d",
humanize.Comma(i),
time.Since(start),
humanize.Comma(int64(100_000/time.Since(start).Seconds())),
itr.Version(),
)
))
start = time.Now()
}
}
Expand All @@ -219,7 +215,7 @@ func treeCommand() *cobra.Command {
}
}

log.Info().Msgf("last version=%d hash=%x", lastVersion, lastHash)
log.Info(fmt.Sprintf("last version=%d hash=%x", lastVersion, lastHash))

return nil
},
Expand Down
12 changes: 3 additions & 9 deletions v2/cmd/rollback/rollback.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package rollback

import (
"os"
"time"
"fmt"

"github.com/cosmos/iavl/v2"
"github.com/rs/zerolog"
zlog "github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

var log = zlog.Output(zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.Stamp,
})
var log = iavl.NewTestLogger()

func Command() *cobra.Command {
var (
Expand All @@ -29,7 +23,7 @@ func Command() *cobra.Command {
return err
}
for _, dbPath := range dbPaths {
log.Info().Msgf("revert db %s to version %d", dbPath, version)
log.Info(fmt.Sprintf("revert db %s to version %d", dbPath, version))
sql, err := iavl.NewSqliteDb(iavl.NewNodePool(), iavl.SqliteDbOptions{Path: dbPath})
if err != nil {
return err
Expand Down
12 changes: 3 additions & 9 deletions v2/cmd/snapshot/snapshot.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package snapshot

import (
"os"
"time"
"fmt"

"github.com/cosmos/iavl/v2"
"github.com/rs/zerolog"
zlog "github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

var log = zlog.Output(zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.Stamp,
})
var log = iavl.NewTestLogger()

func Command() *cobra.Command {
var (
Expand All @@ -28,7 +22,7 @@ func Command() *cobra.Command {
if err != nil {
return err
}
log.Info().Msgf("found db paths: %v", paths)
log.Info(fmt.Sprintf("found db paths: %v", paths))

var (
pool = iavl.NewNodePool()
Expand Down
6 changes: 3 additions & 3 deletions v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/emicklei/dot v1.6.0
github.com/kocubinski/costor-api v1.1.1
github.com/prometheus/client_golang v1.16.0
github.com/rs/zerolog v1.30.0
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
Expand All @@ -23,14 +22,15 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/rs/zerolog v1.33.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/sys v0.22.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
16 changes: 7 additions & 9 deletions v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ github.com/kocubinski/costor-api v1.1.1 h1:sgfJA7T/8IfZ59zxiMrED0xdjerAFuPNBTqyO
github.com/kocubinski/costor-api v1.1.1/go.mod h1:ESMBMDkKfN+9vvvhhNVdKLhbOmzI3O/i16iXvRM9Tuc=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand All @@ -52,8 +51,8 @@ github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+Pymzi
github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c=
github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
Expand All @@ -64,12 +63,11 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
Expand Down
55 changes: 55 additions & 0 deletions v2/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package iavl

import "log/slog"

// Logger defines basic logger that IAVL expects.
// It is a subset of the cosmossdk.io/core/log.Logger interface.
// cosmossdk.io/log/log.Logger implements this interface.
type Logger interface {
Copy link
Member

Choose a reason for hiding this comment

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

why not depend on .io/core/log?

// Info takes a message and a set of key/value pairs and logs with level INFO.
// The key of the tuple must be a string.
Info(msg string, keyVals ...any)

// Warn takes a message and a set of key/value pairs and logs with level WARN.
// The key of the tuple must be a string.
Warn(msg string, keyVals ...any)

// Error takes a message and a set of key/value pairs and logs with level ERR.
// The key of the tuple must be a string.
Error(msg string, keyVals ...any)

// Debug takes a message and a set of key/value pairs and logs with level DEBUG.
// The key of the tuple must be a string.
Debug(msg string, keyVals ...any)
}

// NewNopLogger returns a new logger that does nothing.
func NewNopLogger() Logger {
return &noopLogger{}
}

type noopLogger struct{}

func (l *noopLogger) Info(string, ...any) {}
func (l *noopLogger) Warn(string, ...any) {}
func (l *noopLogger) Error(string, ...any) {}
func (l *noopLogger) Debug(string, ...any) {}

func NewTestLogger() Logger {
return &testLogger{}
}

type testLogger struct{}

func (l *testLogger) Info(msg string, keys ...any) {
slog.Info(msg, keys...)
}
func (l *testLogger) Warn(msg string, keys ...any) {
slog.Warn(msg, keys...)
}
func (l *testLogger) Error(msg string, keys ...any) {
slog.Error(msg, keys...)
}
func (l *testLogger) Debug(msg string, keys ...any) {
slog.Debug(msg, keys...)
}
17 changes: 9 additions & 8 deletions v2/multitree.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
// MultiTree encapsulates multiple IAVL trees, each with its own "store key" in the context of the Cosmos SDK.
// Within IAVL v2 is only used to test the IAVL v2 implementation, and for import/export of IAVL v2 state.
type MultiTree struct {
logger Logger

Trees map[string]*Tree

pool *NodePool
Expand All @@ -26,7 +28,7 @@ type MultiTree struct {
errorCh chan error
}

func NewMultiTree(rootPath string, opts TreeOptions) *MultiTree {
func NewMultiTree(logger Logger, rootPath string, opts TreeOptions) *MultiTree {
return &MultiTree{
Trees: make(map[string]*Tree),
doneCh: make(chan saveVersionResult, 1000),
Expand All @@ -37,8 +39,8 @@ func NewMultiTree(rootPath string, opts TreeOptions) *MultiTree {
}
}

func ImportMultiTree(pool *NodePool, version int64, path string, treeOpts TreeOptions) (*MultiTree, error) {
mt := NewMultiTree(path, treeOpts)
func ImportMultiTree(logger Logger, pool *NodePool, version int64, path string, treeOpts TreeOptions) (*MultiTree, error) {
mt := NewMultiTree(logger, path, treeOpts)
paths, err := FindDbsInPath(path)
if err != nil {
return nil, err
Expand Down Expand Up @@ -78,7 +80,7 @@ func ImportMultiTree(pool *NodePool, version int64, path string, treeOpts TreeOp
return nil, err
case res := <-done:
prefix := filepath.Base(res.path)
log.Info().Msgf("imported %s", prefix)
logger.Info(fmt.Sprintf("imported %s", prefix))
mt.Trees[prefix] = res.tree
}
}
Expand Down Expand Up @@ -108,7 +110,6 @@ func (mt *MultiTree) MountTrees() error {
prefix := filepath.Base(dbPath)
sqlOpts := defaultSqliteDbOptions(SqliteDbOptions{})
sqlOpts.Path = dbPath
log.Info().Msgf("mounting %s; opts %v", prefix, sqlOpts)
sql, err := NewSqliteDb(mt.pool, sqlOpts)
if err != nil {
return err
Expand Down Expand Up @@ -173,7 +174,7 @@ func (mt *MultiTree) SaveVersionConcurrently() ([]byte, int64, error) {
for i := 0; i < treeCount; i++ {
select {
case err := <-mt.errorCh:
log.Error().Err(err).Msg("failed to save version")
mt.logger.Error("failed to save version", "error", err)
errs = append(errs, err)
case result := <-mt.doneCh:
if version != -1 && version != result.version {
Expand Down Expand Up @@ -218,7 +219,7 @@ func (mt *MultiTree) SnapshotConcurrently() error {
for i := 0; i < treeCount; i++ {
select {
case err := <-mt.errorCh:
log.Error().Err(err).Msg("failed to snapshot")
mt.logger.Error("failed to snapshot", "error", err)
errs = append(errs, err)
case <-mt.doneCh:
}
Expand Down Expand Up @@ -271,7 +272,7 @@ func (mt *MultiTree) WarmLeaves() error {
for i := 0; i < cnt; i++ {
select {
case err := <-mt.errorCh:
log.Error().Err(err).Msg("failed to warm leaves")
mt.logger.Error("failed to warm leaves", "error", err)
return err
case <-mt.doneCh:
}
Expand Down
Loading
Loading