e3: log slow (unclosed) contexts to detect leaks (#7375)

This commit is contained in:
Alex Sharov 2023-04-24 12:52:15 +07:00 committed by GitHub
parent d70c9f0979
commit 367f3e9afa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 52 deletions

View File

@ -72,6 +72,7 @@ func New(db kv.RwDB, agg *state.AggregatorV3, cb1 tConvertV3toV2, cb2 tRestoreCo
return &DB{RwDB: db, agg: agg, convertV3toV2: cb1, restoreCodeHash: cb2, parseInc: cb3, systemContractLookup: systemContractLookup}, nil
}
func (db *DB) Agg() *state.AggregatorV3 { return db.agg }
func (db *DB) InternalDB() kv.RwDB { return db.RwDB }
func (db *DB) BeginTemporalRo(ctx context.Context) (kv.TemporalTx, error) {
kvTx, err := db.RwDB.BeginRo(ctx)
@ -105,55 +106,55 @@ func (db *DB) View(ctx context.Context, f func(tx kv.Tx) error) error {
return f(tx)
}
func (db *DB) BeginTemporalRw(ctx context.Context) (kv.RwTx, error) {
kvTx, err := db.RwDB.BeginRw(ctx) //nolint:gocritic
if err != nil {
return nil, err
}
tx := &Tx{MdbxTx: kvTx.(*mdbx.MdbxTx), db: db}
//func (db *DB) BeginTemporalRw(ctx context.Context) (kv.RwTx, error) {
// kvTx, err := db.RwDB.BeginRw(ctx) //nolint:gocritic
// if err != nil {
// return nil, err
// }
// tx := &Tx{MdbxTx: kvTx.(*mdbx.MdbxTx), db: db}
//
// //tx.agg = db.agg.MakeContext()
// return tx, nil
//}
//func (db *DB) BeginRw(ctx context.Context) (kv.RwTx, error) {
// return db.BeginTemporalRw(ctx)
//}
//func (db *DB) Update(ctx context.Context, f func(tx kv.RwTx) error) error {
// tx, err := db.BeginTemporalRw(ctx)
// if err != nil {
// return err
// }
// defer tx.Rollback()
// if err = f(tx); err != nil {
// return err
// }
// return tx.Commit()
//}
tx.agg = db.agg.MakeContext()
return tx, nil
}
func (db *DB) BeginRw(ctx context.Context) (kv.RwTx, error) {
return db.BeginTemporalRw(ctx)
}
func (db *DB) Update(ctx context.Context, f func(tx kv.RwTx) error) error {
tx, err := db.BeginTemporalRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
if err = f(tx); err != nil {
return err
}
return tx.Commit()
}
func (db *DB) BeginTemporalRwNosync(ctx context.Context) (kv.RwTx, error) {
kvTx, err := db.RwDB.BeginRwNosync(ctx) //nolint:gocritic
if err != nil {
return nil, err
}
tx := &Tx{MdbxTx: kvTx.(*mdbx.MdbxTx), db: db}
tx.agg = db.agg.MakeContext()
return tx, nil
}
func (db *DB) BeginRwNosync(ctx context.Context) (kv.RwTx, error) {
return db.BeginTemporalRwNosync(ctx) //nolint:gocritic
}
func (db *DB) UpdateNosync(ctx context.Context, f func(tx kv.RwTx) error) error {
tx, err := db.BeginTemporalRwNosync(ctx)
if err != nil {
return err
}
defer tx.Rollback()
if err = f(tx); err != nil {
return err
}
return tx.Commit()
}
//func (db *DB) BeginTemporalRwNosync(ctx context.Context) (kv.RwTx, error) {
// kvTx, err := db.RwDB.BeginRwNosync(ctx) //nolint:gocritic
// if err != nil {
// return nil, err
// }
// tx := &Tx{MdbxTx: kvTx.(*mdbx.MdbxTx), db: db}
//
// //tx.agg = db.agg.MakeContext()
// return tx, nil
//}
//func (db *DB) BeginRwNosync(ctx context.Context) (kv.RwTx, error) {
// return db.BeginTemporalRwNosync(ctx) //nolint:gocritic
//}
//func (db *DB) UpdateNosync(ctx context.Context, f func(tx kv.RwTx) error) error {
// tx, err := db.BeginTemporalRwNosync(ctx)
// if err != nil {
// return err
// }
// defer tx.Rollback()
// if err = f(tx); err != nil {
// return err
// }
// return tx.Commit()
//}
type Tx struct {
*mdbx.MdbxTx

View File

@ -27,6 +27,7 @@ import (
libstate "github.com/ledgerwatch/erigon-lib/state"
state2 "github.com/ledgerwatch/erigon-lib/state"
"github.com/ledgerwatch/erigon/common/math"
"github.com/ledgerwatch/erigon/core/state/temporal"
"github.com/ledgerwatch/log/v3"
"github.com/torquem-ch/mdbx-go/mdbx"
"golang.org/x/sync/errgroup"
@ -109,7 +110,13 @@ func ExecV3(ctx context.Context,
parallel bool, logPrefix string,
maxBlockNum uint64,
) error {
batchSize, chainDb := cfg.batchSize, cfg.db
batchSize := cfg.batchSize
chainDb := cfg.db
//TODO: re-think - how it must work
if casted, ok := chainDb.(*temporal.DB); ok {
chainDb = casted.InternalDB()
}
blockReader := cfg.blockReader
agg, engine := cfg.agg, cfg.engine
chainConfig, genesis := cfg.chainConfig, cfg.genesis
@ -295,6 +302,10 @@ func ExecV3(ctx context.Context,
return ctx.Err()
case <-logEvery.C:
if list := agg.SlowContextsList(); len(list) > 0 {
log.Info("[dbg] Active agg ctx", "list", list)
}
stepsInDB := rawdbhelpers.IdxStepsCountV3(tx)
progress.Log(rs, in, rws, rs.DoneCount(), inputBlockNum.Load(), outputBlockNum.Get(), outputTxNum.Load(), ExecRepeats.Get(), stepsInDB)
if agg.HasBackgroundFilesBuild() {

2
go.mod
View File

@ -3,7 +3,7 @@ module github.com/ledgerwatch/erigon
go 1.19
require (
github.com/ledgerwatch/erigon-lib v0.0.0-20230423044930-fc9dd74e6407
github.com/ledgerwatch/erigon-lib v0.0.0-20230424042211-39e97b6becde
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230404044759-5dec854ce336
github.com/ledgerwatch/log/v3 v3.7.0
github.com/ledgerwatch/secp256k1 v1.0.0

4
go.sum
View File

@ -438,8 +438,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758 h1:0D5M2HQSGD3PYPwICLl+/9oulQauOuETfgFvhBDffs0=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/ledgerwatch/erigon-lib v0.0.0-20230423044930-fc9dd74e6407 h1:PBcNdCUF0UtY+pkbzwXWeRsEIinVWGiCjyqD9WvV+H4=
github.com/ledgerwatch/erigon-lib v0.0.0-20230423044930-fc9dd74e6407/go.mod h1:D05f9OXc/2cnYxCyBexlu5HeIeQW9GKXynyWYzJ1F5I=
github.com/ledgerwatch/erigon-lib v0.0.0-20230424042211-39e97b6becde h1:6ldvFEx1d0gPIdiXYV3DGGvUjTkPn78m/5B9/6T/LUo=
github.com/ledgerwatch/erigon-lib v0.0.0-20230424042211-39e97b6becde/go.mod h1:D05f9OXc/2cnYxCyBexlu5HeIeQW9GKXynyWYzJ1F5I=
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230404044759-5dec854ce336 h1:Yxmt4Wyd0RCLr7UJJAl0ApCP/f5qkWfvHfgPbnI8ghM=
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230404044759-5dec854ce336/go.mod h1:3AuPxZc85jkehh/HA9h8gabv5MSi3kb/ddtzBsTVJFo=
github.com/ledgerwatch/log/v3 v3.7.0 h1:aFPEZdwZx4jzA3+/Pf8wNDN5tCI0cIolq/kfvgcM+og=