Skip to content

Commit

Permalink
Improve DB logging (#5471)
Browse files Browse the repository at this point in the history
## Motivation
When running migrations the node doesn't display for which DB it runs the migrations. This PR adds that information.

## Changes
- Add more info to the DB migration log: `current version`, `target version` and `uri` of the DB.

## Test Plan
existing tests pass

## TODO
<!-- This section should be removed when all items are complete -->
- [x] Explain motivation or link existing issue(s)
- [x] Test changes and document test plan
- [x] Update documentation as needed
- [x] Update [changelog](../CHANGELOG.md) as needed
  • Loading branch information
fasmat committed Jan 19, 2024
1 parent 3f752f9 commit e8189e4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
5 changes: 4 additions & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,9 @@ func (app *App) setupDBs(ctx context.Context, lg log.Log) error {
if err != nil {
return fmt.Errorf("failed to load migrations: %w", err)
}
dbLog := app.addLogger(StateDbLogger, lg)
dbopts := []sql.Opt{
sql.WithLogger(dbLog.Zap()),
sql.WithMigrations(migrations),
sql.WithConnections(app.Config.DatabaseConnections),
sql.WithLatencyMetering(app.Config.DatabaseLatencyMetering),
Expand All @@ -1617,7 +1619,7 @@ func (app *App) setupDBs(ctx context.Context, lg log.Log) error {
app.dbMetrics = dbmetrics.NewDBMetricsCollector(
ctx,
app.db,
app.addLogger(StateDbLogger, lg),
dbLog,
app.Config.DatabaseSizeMeteringInterval,
)
}
Expand Down Expand Up @@ -1647,6 +1649,7 @@ func (app *App) setupDBs(ctx context.Context, lg log.Log) error {
return fmt.Errorf("load local migrations: %w", err)
}
localDB, err := localsql.Open("file:"+filepath.Join(dbPath, localDbFile),
sql.WithLogger(dbLog.Zap()),
sql.WithMigrations(migrations),
sql.WithMigration(localsql.New0001Migration(app.Config.SMESHING.Opts.DataDir)),
sql.WithMigration(localsql.New0002Migration(app.Config.SMESHING.Opts.DataDir)),
Expand Down
21 changes: 18 additions & 3 deletions sql/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
sqlite "github.com/go-llsqlite/crawshaw"
"github.com/go-llsqlite/crawshaw/sqlitex"
"github.com/prometheus/client_golang/prometheus"

"github.com/spacemeshos/go-spacemesh/log"
"go.uber.org/zap"
)

var (
Expand Down Expand Up @@ -63,6 +62,7 @@ func defaultConf() *conf {
connections: 16,
migrations: migrations,
skipMigration: map[int]struct{}{},
logger: zap.NewNop(),
}
}

Expand All @@ -73,6 +73,7 @@ type conf struct {
vacuumState int
migrations []Migration
enableLatency bool
logger *zap.Logger
}

// WithConnections overwrites number of pooled connections.
Expand All @@ -82,6 +83,12 @@ func WithConnections(n int) Opt {
}
}

func WithLogger(logger *zap.Logger) Opt {
return func(c *conf) {
c.logger = logger
}
}

// WithMigrations overwrites embedded migrations.
// Migrations are sorted by order before applying.
func WithMigrations(migrations []Migration) Opt {
Expand Down Expand Up @@ -173,7 +180,15 @@ func Open(uri string, opts ...Opt) (*Database, error) {
if err != nil {
return nil, err
}
log.With().Info("running migrations", log.Int("current version", before))
after := 0
if len(config.migrations) > 0 {
after = config.migrations[len(config.migrations)-1].Order()
}
config.logger.Info("running migrations",
zap.String("uri", uri),
zap.Int("current version", before),
zap.Int("target version", after),
)
tx, err := db.Tx(context.Background())
if err != nil {
return nil, err
Expand Down

0 comments on commit e8189e4

Please sign in to comment.