Skip to content

Commit

Permalink
refactor(v2): use non global logger
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Jan 10, 2025
1 parent b3c241c commit 533941d
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 93 deletions.
34 changes: 34 additions & 0 deletions v2/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package iavl

// 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 {
// 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) {}
2 changes: 1 addition & 1 deletion v2/multitree.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/cosmos/iavl/v2/metrics"
"github.com/dustin/go-humanize"
"github.com/rs/zerolog/log"
"golang.org/x/exp/slices"
)

Expand Down Expand Up @@ -108,7 +109,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
37 changes: 18 additions & 19 deletions v2/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/dustin/go-humanize"
api "github.com/kocubinski/costor-api"
"github.com/kocubinski/costor-api/logz"
"github.com/rs/zerolog"
)

type sqliteSnapshot struct {
Expand All @@ -35,7 +34,7 @@ type sqliteSnapshot struct {
version int64
getLeft func(*Node) *Node
getRight func(*Node) *Node
log zerolog.Logger
log Logger
}

func (sql *SqliteDb) Snapshot(ctx context.Context, tree *Tree) error {
Expand All @@ -51,7 +50,7 @@ func (sql *SqliteDb) Snapshot(ctx context.Context, tree *Tree) error {
sql: sql,
batchSize: 200_000,
version: version,
log: log.With().Str("path", filepath.Base(sql.opts.Path)).Logger(),
log: sql.logger,
getLeft: func(node *Node) *Node {
return node.left(tree)
},
Expand All @@ -68,7 +67,7 @@ func (sql *SqliteDb) Snapshot(ctx context.Context, tree *Tree) error {
if err = snapshot.flush(); err != nil {
return err
}
log.Info().Str("path", sql.opts.Path).Msgf("creating index on snapshot_%d", version)
sql.logger.Info(fmt.Sprintf("creating index on snapshot_%d", version), "path", sql.opts.Path)
err = sql.leafWrite.Exec(fmt.Sprintf("CREATE INDEX snapshot_%d_idx ON snapshot_%d (ordinal);", version, version))
return err
}
Expand All @@ -89,11 +88,11 @@ func NewIngestSnapshotConnection(snapshotDbPath string) (*sqlite3.Conn, error) {
}
pageSize := os.Getpagesize()
if newDb {
log.Info().Msgf("setting page size to %s", humanize.Bytes(uint64(pageSize)))
err = conn.Exec(fmt.Sprintf("PRAGMA page_size=%d; VACUUM;", pageSize))
if err != nil {
return nil, err
}

err = conn.Exec("PRAGMA journal_mode=WAL;")
if err != nil {
return nil, err
Expand Down Expand Up @@ -243,7 +242,7 @@ func (sql *SqliteDb) WriteSnapshot(
batchSize: 400_000,
version: version,
lastWrite: time.Now(),
log: log.With().Str("path", filepath.Base(sql.opts.Path)).Logger(),
log: sql.logger,
writeTree: true,
}
if opts.WriteCheckpoint {
Expand Down Expand Up @@ -277,7 +276,7 @@ func (sql *SqliteDb) WriteSnapshot(
return nil, err
}

var versions []int64
var versions []int64 // where is this used?
for v := range uniqueVersions {
versions = append(versions, v)
}
Expand All @@ -286,7 +285,7 @@ func (sql *SqliteDb) WriteSnapshot(
return nil, err
}

log.Info().Str("path", sql.opts.Path).Msg("creating table indexes")
sql.logger.Info("creating table indexes")
err = sql.leafWrite.Exec(fmt.Sprintf("CREATE INDEX snapshot_%d_idx ON snapshot_%d (ordinal);", version, version))
if err != nil {
return nil, err
Expand Down Expand Up @@ -329,7 +328,7 @@ func (sql *SqliteDb) ImportSnapshotFromTable(version int64, traverseOrder Traver
defer func(q *sqlite3.Stmt) {
err = q.Close()
if err != nil {
log.Error().Err(err).Msg("error closing import query")
sql.logger.Error("error closing import query", "error", err)
}
}(q)

Expand All @@ -338,7 +337,7 @@ func (sql *SqliteDb) ImportSnapshotFromTable(version int64, traverseOrder Traver
pool: sql.pool,
loadLeaves: loadLeaves,
since: time.Now(),
log: log.With().Str("path", sql.opts.Path).Logger(),
log: sql.logger,
}
var root *Node
if traverseOrder == PostOrder {
Expand Down Expand Up @@ -372,7 +371,7 @@ func (sql *SqliteDb) ImportMostRecentSnapshot(targetVersion int64, traverseOrder
defer func(q *sqlite3.Stmt) {
err = q.Close()
if err != nil {
log.Error().Err(err).Msg("error closing import query")
sql.logger.Error("error closing import query", "error", err)
}
}(q)
if err != nil {
Expand Down Expand Up @@ -474,7 +473,7 @@ func (snap *sqliteSnapshot) writeStep(node *Node) error {
func (snap *sqliteSnapshot) flush() error {
select {
case <-snap.ctx.Done():
snap.log.Info().Msgf("snapshot cancelled at ordinal=%s", humanize.Comma(int64(snap.ordinal)))
snap.log.Info(fmt.Sprintf("snapshot cancelled at ordinal=%s", humanize.Comma(int64(snap.ordinal))))
errs := errors.Join(
snap.snapshotInsert.Reset(),
snap.snapshotInsert.Close(),
Expand Down Expand Up @@ -510,12 +509,12 @@ func (snap *sqliteSnapshot) flush() error {
default:
}

snap.log.Info().Msgf("flush total=%s size=%s dur=%s wr/s=%s",
snap.log.Info(fmt.Sprintf("flush total=%s size=%s dur=%s wr/s=%s",
humanize.Comma(int64(snap.ordinal)),
humanize.Comma(int64(snap.batchSize)),
time.Since(snap.lastWrite).Round(time.Millisecond),
humanize.Comma(int64(float64(snap.batchSize)/time.Since(snap.lastWrite).Seconds())),
)
))

err := errors.Join(
snap.sql.leafWrite.Commit(),
Expand Down Expand Up @@ -745,16 +744,16 @@ type sqliteImport struct {

i int64
since time.Time
log zerolog.Logger
log Logger
}

func (sqlImport *sqliteImport) queryStepPreOrder() (node *Node, err error) {
sqlImport.i++
if sqlImport.i%1_000_000 == 0 {
sqlImport.log.Debug().Msgf("import: nodes=%s, node/s=%s",
sqlImport.log.Debug(fmt.Sprintf("import: nodes=%s, node/s=%s",
humanize.Comma(sqlImport.i),
humanize.Comma(int64(float64(1_000_000)/time.Since(sqlImport.since).Seconds())),
)
))
sqlImport.since = time.Now()
}

Expand Down Expand Up @@ -799,10 +798,10 @@ func (sqlImport *sqliteImport) queryStepPreOrder() (node *Node, err error) {
func (sqlImport *sqliteImport) queryStepPostOrder() (node *Node, err error) {
sqlImport.i++
if sqlImport.i%1_000_000 == 0 {
sqlImport.log.Debug().Msgf("import: nodes=%s, node/s=%s",
sqlImport.log.Debug(fmt.Sprintf("import: nodes=%s, node/s=%s",
humanize.Comma(sqlImport.i),
humanize.Comma(int64(float64(1_000_000)/time.Since(sqlImport.since).Seconds())),
)
))
sqlImport.since = time.Now()
}

Expand Down
Loading

0 comments on commit 533941d

Please sign in to comment.